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 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.BigOperators.Expect import Mathlib.Algebra.Order.BigOperators.Ring.Finset import Mathlib.Algebra.Order.Field.Canonical import Mathlib.Algebra.Order.Nonneg.Floor import Mathlib.Data.Real.Pointwise import Mathlib.Data.NNReal.Defs import Mathlib.Order.ConditionallyCompleteLattice.Group /-! # Basic results on nonnegative real numbers This file contains all results on `NNReal` that do not directly follow from its basic structure. As a consequence, it is a bit of a random collection of results, and is a good target for cleanup. ## Notations This file uses `ℝ≥0` as a localized notation for `NNReal`. -/ assert_not_exists Star open Function open scoped BigOperators namespace NNReal noncomputable instance : FloorSemiring ℝ≥0 := Nonneg.floorSemiring @[simp, norm_cast] theorem coe_indicator {α} (s : Set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (fun x => ↑(f x)) a := (toRealHom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[norm_cast] theorem coe_list_sum (l : List ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map (↑)).sum := map_list_sum toRealHom l @[norm_cast] theorem coe_list_prod (l : List ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map (↑)).prod := map_list_prod toRealHom l @[norm_cast] theorem coe_multiset_sum (s : Multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map (↑)).sum := map_multiset_sum toRealHom s @[norm_cast] theorem coe_multiset_prod (s : Multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map (↑)).prod := map_multiset_prod toRealHom s variable {ι : Type*} {s : Finset ι} {f : ι → ℝ} @[simp, norm_cast] theorem coe_sum (s : Finset ι) (f : ι → ℝ≥0) : ∑ i ∈ s, f i = ∑ i ∈ s, (f i : ℝ) := map_sum toRealHom _ _ @[simp, norm_cast] lemma coe_expect (s : Finset ι) (f : ι → ℝ≥0) : 𝔼 i ∈ s, f i = 𝔼 i ∈ s, (f i : ℝ) := map_expect toRealHom .. theorem _root_.Real.toNNReal_sum_of_nonneg (hf : ∀ i ∈ s, 0 ≤ f i) : Real.toNNReal (∑ a ∈ s, f a) = ∑ a ∈ s, Real.toNNReal (f a) := by rw [← coe_inj, NNReal.coe_sum, Real.coe_toNNReal _ (Finset.sum_nonneg hf)] exact Finset.sum_congr rfl fun x hxs => by rw [Real.coe_toNNReal _ (hf x hxs)] @[simp, norm_cast] theorem coe_prod (s : Finset ι) (f : ι → ℝ≥0) : ↑(∏ a ∈ s, f a) = ∏ a ∈ s, (f a : ℝ) := map_prod toRealHom _ _ theorem _root_.Real.toNNReal_prod_of_nonneg (hf : ∀ a, a ∈ s → 0 ≤ f a) : Real.toNNReal (∏ a ∈ s, f a) = ∏ a ∈ s, Real.toNNReal (f a) := by rw [← coe_inj, NNReal.coe_prod, Real.coe_toNNReal _ (Finset.prod_nonneg hf)] exact Finset.prod_congr rfl fun x hxs => by rw [Real.coe_toNNReal _ (hf x hxs)] theorem le_iInf_add_iInf {ι ι' : Sort*} [Nonempty ι] [Nonempty ι'] {f : ι → ℝ≥0} {g : ι' → ℝ≥0} {a : ℝ≥0} (h : ∀ i j, a ≤ f i + g j) : a ≤ (⨅ i, f i) + ⨅ j, g j := by rw [← NNReal.coe_le_coe, NNReal.coe_add, coe_iInf, coe_iInf] exact le_ciInf_add_ciInf h theorem mul_finset_sup {α} (r : ℝ≥0) (s : Finset α) (f : α → ℝ≥0) : r * s.sup f = s.sup fun a => r * f a := Finset.comp_sup_eq_sup_comp _ (NNReal.mul_sup r) (mul_zero r) theorem finset_sup_mul {α} (s : Finset α) (f : α → ℝ≥0) (r : ℝ≥0) : s.sup f * r = s.sup fun a => f a * r := Finset.comp_sup_eq_sup_comp (· * r) (fun x y => NNReal.sup_mul x y r) (zero_mul r) theorem finset_sup_div {α} {f : α → ℝ≥0} {s : Finset α} (r : ℝ≥0) : s.sup f / r = s.sup fun a => f a / r := by simp only [div_eq_inv_mul, mul_finset_sup] open Real section Sub /-! ### Lemmas about subtraction In this section we provide a few lemmas about subtraction that do not fit well into any other typeclass. For lemmas about subtraction and addition see lemmas about `OrderedSub` in the file `Mathlib.Algebra.Order.Sub.Basic`. See also `mul_tsub` and `tsub_mul`. -/ theorem sub_div (a b c : ℝ≥0) : (a - b) / c = a / c - b / c := tsub_div _ _ _ end Sub section Csupr open Set variable {ι : Sort*} {f : ι → ℝ≥0} theorem iInf_mul (f : ι → ℝ≥0) (a : ℝ≥0) : iInf f * a = ⨅ i, f i * a := by rw [← coe_inj, NNReal.coe_mul, coe_iInf, coe_iInf] exact Real.iInf_mul_of_nonneg (NNReal.coe_nonneg _) _ theorem mul_iInf (f : ι → ℝ≥0) (a : ℝ≥0) : a * iInf f = ⨅ i, a * f i := by simpa only [mul_comm] using iInf_mul f a theorem mul_iSup (f : ι → ℝ≥0) (a : ℝ≥0) : (a * ⨆ i, f i) = ⨆ i, a * f i := by rw [← coe_inj, NNReal.coe_mul, NNReal.coe_iSup, NNReal.coe_iSup] exact Real.mul_iSup_of_nonneg (NNReal.coe_nonneg _) _ theorem iSup_mul (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) * a = ⨆ i, f i * a := by rw [mul_comm, mul_iSup] simp_rw [mul_comm] theorem iSup_div (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) / a = ⨆ i, f i / a := by simp only [div_eq_mul_inv, iSup_mul] theorem mul_iSup_le {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, g * h j ≤ a) : g * iSup h ≤ a := by rw [mul_iSup] exact ciSup_le' H theorem iSup_mul_le {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, g i * h ≤ a) : iSup g * h ≤ a := by rw [iSup_mul] exact ciSup_le' H theorem iSup_mul_iSup_le {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, g i * h j ≤ a) : iSup g * iSup h ≤ a := iSup_mul_le fun _ => mul_iSup_le <| H _ variable [Nonempty ι] theorem le_mul_iInf {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, a ≤ g * h j) : a ≤ g * iInf h := by rw [mul_iInf] exact le_ciInf H theorem le_iInf_mul {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, a ≤ g i * h) : a ≤ iInf g * h := by rw [iInf_mul] exact le_ciInf H theorem le_iInf_mul_iInf {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, a ≤ g i * h j) : a ≤ iInf g * iInf h := le_iInf_mul fun i => le_mul_iInf <| H i end Csupr end NNReal
Mathlib/Data/NNReal/Basic.lean
696
698
/- 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, Kexing Ying -/ import Mathlib.Probability.Notation import Mathlib.Probability.Integration import Mathlib.MeasureTheory.Function.L2Space /-! # Variance of random variables We define the variance of a real-valued random variable as `Var[X] = 𝔼[(X - 𝔼[X])^2]` (in the `ProbabilityTheory` locale). ## Main definitions * `ProbabilityTheory.evariance`: the variance of a real-valued random variable as an extended non-negative real. * `ProbabilityTheory.variance`: the variance of a real-valued random variable as a real number. ## Main results * `ProbabilityTheory.variance_le_expectation_sq`: the inequality `Var[X] ≤ 𝔼[X^2]`. * `ProbabilityTheory.meas_ge_le_variance_div_sq`: Chebyshev's inequality, i.e., `ℙ {ω | c ≤ |X ω - 𝔼[X]|} ≤ ENNReal.ofReal (Var[X] / c ^ 2)`. * `ProbabilityTheory.meas_ge_le_evariance_div_sq`: Chebyshev's inequality formulated with `evariance` without requiring the random variables to be L². * `ProbabilityTheory.IndepFun.variance_add`: the variance of the sum of two independent random variables is the sum of the variances. * `ProbabilityTheory.IndepFun.variance_sum`: the variance of a finite sum of pairwise independent random variables is the sum of the variances. * `ProbabilityTheory.variance_le_sub_mul_sub`: the variance of a random variable `X` satisfying `a ≤ X ≤ b` almost everywhere is at most `(b - 𝔼 X) * (𝔼 X - a)`. * `ProbabilityTheory.variance_le_sq_of_bounded`: the variance of a random variable `X` satisfying `a ≤ X ≤ b` almost everywhere is at most`((b - a) / 2) ^ 2`. -/ open MeasureTheory Filter Finset noncomputable section open scoped MeasureTheory ProbabilityTheory ENNReal NNReal namespace ProbabilityTheory variable {Ω : Type*} {mΩ : MeasurableSpace Ω} {X : Ω → ℝ} {μ : Measure Ω} variable (X μ) in -- Porting note: Consider if `evariance` or `eVariance` is better. Also, -- consider `eVariationOn` in `Mathlib.Analysis.BoundedVariation`. /-- The `ℝ≥0∞`-valued variance of a real-valued random variable defined as the Lebesgue integral of `‖X - 𝔼[X]‖^2`. -/ def evariance : ℝ≥0∞ := ∫⁻ ω, ‖X ω - μ[X]‖ₑ ^ 2 ∂μ variable (X μ) in /-- The `ℝ`-valued variance of a real-valued random variable defined by applying `ENNReal.toReal` to `evariance`. -/ def variance : ℝ := (evariance X μ).toReal /-- The `ℝ≥0∞`-valued variance of the real-valued random variable `X` according to the measure `μ`. This is defined as the Lebesgue integral of `(X - 𝔼[X])^2`. -/ scoped notation "eVar[" X "; " μ "]" => ProbabilityTheory.evariance X μ /-- The `ℝ≥0∞`-valued variance of the real-valued random variable `X` according to the volume measure. This is defined as the Lebesgue integral of `(X - 𝔼[X])^2`. -/ scoped notation "eVar[" X "]" => eVar[X; MeasureTheory.MeasureSpace.volume] /-- The `ℝ`-valued variance of the real-valued random variable `X` according to the measure `μ`. It is set to `0` if `X` has infinite variance. -/ scoped notation "Var[" X "; " μ "]" => ProbabilityTheory.variance X μ /-- The `ℝ`-valued variance of the real-valued random variable `X` according to the volume measure. It is set to `0` if `X` has infinite variance. -/ scoped notation "Var[" X "]" => Var[X; MeasureTheory.MeasureSpace.volume] theorem evariance_lt_top [IsFiniteMeasure μ] (hX : MemLp X 2 μ) : evariance X μ < ∞ := by have := ENNReal.pow_lt_top (hX.sub <| memLp_const <| μ[X]).2 (n := 2) rw [eLpNorm_eq_lintegral_rpow_enorm two_ne_zero ENNReal.ofNat_ne_top, ← ENNReal.rpow_two] at this simp only [ENNReal.toReal_ofNat, Pi.sub_apply, ENNReal.toReal_one, one_div] at this rw [← ENNReal.rpow_mul, inv_mul_cancel₀ (two_ne_zero : (2 : ℝ) ≠ 0), ENNReal.rpow_one] at this simp_rw [ENNReal.rpow_two] at this exact this lemma evariance_ne_top [IsFiniteMeasure μ] (hX : MemLp X 2 μ) : evariance X μ ≠ ∞ := (evariance_lt_top hX).ne theorem evariance_eq_top [IsFiniteMeasure μ] (hXm : AEStronglyMeasurable X μ) (hX : ¬MemLp X 2 μ) : evariance X μ = ∞ := by by_contra h rw [← Ne, ← lt_top_iff_ne_top] at h have : MemLp (fun ω => X ω - μ[X]) 2 μ := by refine ⟨hXm.sub aestronglyMeasurable_const, ?_⟩ rw [eLpNorm_eq_lintegral_rpow_enorm two_ne_zero ENNReal.ofNat_ne_top] simp only [ENNReal.toReal_ofNat, ENNReal.toReal_one, ENNReal.rpow_two, Ne] exact ENNReal.rpow_lt_top_of_nonneg (by linarith) h.ne refine hX ?_ convert this.add (memLp_const μ[X]) ext ω rw [Pi.add_apply, sub_add_cancel] theorem evariance_lt_top_iff_memLp [IsFiniteMeasure μ] (hX : AEStronglyMeasurable X μ) : evariance X μ < ∞ ↔ MemLp X 2 μ where mp := by contrapose!; rw [top_le_iff]; exact evariance_eq_top hX mpr := evariance_lt_top @[deprecated (since := "2025-02-21")] alias evariance_lt_top_iff_memℒp := evariance_lt_top_iff_memLp lemma evariance_eq_top_iff [IsFiniteMeasure μ] (hX : AEStronglyMeasurable X μ) : evariance X μ = ∞ ↔ ¬ MemLp X 2 μ := by simp [← evariance_lt_top_iff_memLp hX] theorem ofReal_variance [IsFiniteMeasure μ] (hX : MemLp X 2 μ) : .ofReal (variance X μ) = evariance X μ := by rw [variance, ENNReal.ofReal_toReal] exact evariance_ne_top hX protected alias _root_.MeasureTheory.MemLp.evariance_lt_top := evariance_lt_top protected alias _root_.MeasureTheory.MemLp.evariance_ne_top := evariance_ne_top protected alias _root_.MeasureTheory.MemLp.ofReal_variance_eq := ofReal_variance @[deprecated (since := "2025-02-21")] protected alias _root_.MeasureTheory.Memℒp.evariance_lt_top := evariance_lt_top @[deprecated (since := "2025-02-21")] protected alias _root_.MeasureTheory.Memℒp.evariance_ne_top := evariance_ne_top @[deprecated (since := "2025-02-21")] protected alias _root_.MeasureTheory.Memℒp.ofReal_variance_eq := ofReal_variance variable (X μ) in theorem evariance_eq_lintegral_ofReal : evariance X μ = ∫⁻ ω, ENNReal.ofReal ((X ω - μ[X]) ^ 2) ∂μ := by simp [evariance, ← enorm_pow, Real.enorm_of_nonneg (sq_nonneg _)] lemma variance_eq_integral (hX : AEMeasurable X μ) : Var[X; μ] = ∫ ω, (X ω - μ[X]) ^ 2 ∂μ := by simp [variance, evariance, toReal_enorm, ← integral_toReal ((hX.sub_const _).enorm.pow_const _) <| .of_forall fun _ ↦ ENNReal.pow_lt_top enorm_lt_top] lemma variance_of_integral_eq_zero (hX : AEMeasurable X μ) (hXint : μ[X] = 0) : variance X μ = ∫ ω, X ω ^ 2 ∂μ := by simp [variance_eq_integral hX, hXint] @[deprecated (since := "2025-01-23")] alias _root_.MeasureTheory.Memℒp.variance_eq := variance_eq_integral @[deprecated (since := "2025-01-23")] alias _root_.MeasureTheory.Memℒp.variance_eq_of_integral_eq_zero := variance_of_integral_eq_zero @[simp] theorem evariance_zero : evariance 0 μ = 0 := by simp [evariance] theorem evariance_eq_zero_iff (hX : AEMeasurable X μ) : evariance X μ = 0 ↔ X =ᵐ[μ] fun _ => μ[X] := by simp [evariance, lintegral_eq_zero_iff' ((hX.sub_const _).enorm.pow_const _), EventuallyEq, sub_eq_zero] theorem evariance_mul (c : ℝ) (X : Ω → ℝ) (μ : Measure Ω) : evariance (fun ω => c * X ω) μ = ENNReal.ofReal (c ^ 2) * evariance X μ := by rw [evariance, evariance, ← lintegral_const_mul' _ _ ENNReal.ofReal_lt_top.ne] congr with ω rw [integral_const_mul, ← mul_sub, enorm_mul, mul_pow, ← enorm_pow, Real.enorm_of_nonneg (sq_nonneg _)] @[simp] theorem variance_zero (μ : Measure Ω) : variance 0 μ = 0 := by simp only [variance, evariance_zero, ENNReal.toReal_zero] theorem variance_nonneg (X : Ω → ℝ) (μ : Measure Ω) : 0 ≤ variance X μ := ENNReal.toReal_nonneg theorem variance_mul (c : ℝ) (X : Ω → ℝ) (μ : Measure Ω) : variance (fun ω => c * X ω) μ = c ^ 2 * variance X μ := by rw [variance, evariance_mul, ENNReal.toReal_mul, ENNReal.toReal_ofReal (sq_nonneg _)] rfl theorem variance_smul (c : ℝ) (X : Ω → ℝ) (μ : Measure Ω) : variance (c • X) μ = c ^ 2 * variance X μ := variance_mul c X μ theorem variance_smul' {A : Type*} [CommSemiring A] [Algebra A ℝ] (c : A) (X : Ω → ℝ) (μ : Measure Ω) : variance (c • X) μ = c ^ 2 • variance X μ := by convert variance_smul (algebraMap A ℝ c) X μ using 1 · congr; simp only [algebraMap_smul] · simp only [Algebra.smul_def, map_pow] theorem variance_def' [IsProbabilityMeasure μ] {X : Ω → ℝ} (hX : MemLp X 2 μ) : variance X μ = μ[X ^ 2] - μ[X] ^ 2 := by simp only [variance_eq_integral hX.aestronglyMeasurable.aemeasurable, sub_sq'] rw [integral_sub, integral_add]; rotate_left · exact hX.integrable_sq · apply integrable_const · apply hX.integrable_sq.add apply integrable_const · exact ((hX.integrable one_le_two).const_mul 2).mul_const' _ simp only [integral_const, measureReal_univ_eq_one, smul_eq_mul, one_mul, integral_mul_const, integral_const_mul, Pi.pow_apply] ring theorem variance_le_expectation_sq [IsProbabilityMeasure μ] {X : Ω → ℝ} (hm : AEStronglyMeasurable X μ) : variance X μ ≤ μ[X ^ 2] := by by_cases hX : MemLp X 2 μ · rw [variance_def' hX] simp only [sq_nonneg, sub_le_self_iff] rw [variance, evariance_eq_lintegral_ofReal, ← integral_eq_lintegral_of_nonneg_ae] · by_cases hint : Integrable X μ; swap · simp only [integral_undef hint, Pi.pow_apply, Pi.sub_apply, sub_zero] exact le_rfl · rw [integral_undef] · exact integral_nonneg fun a => sq_nonneg _ intro h have A : MemLp (X - fun ω : Ω => μ[X]) 2 μ := (memLp_two_iff_integrable_sq (hint.aestronglyMeasurable.sub aestronglyMeasurable_const)).2 h have B : MemLp (fun _ : Ω => μ[X]) 2 μ := memLp_const _ apply hX convert A.add B simp · exact Eventually.of_forall fun x => sq_nonneg _ · exact (AEMeasurable.pow_const (hm.aemeasurable.sub_const _) _).aestronglyMeasurable theorem evariance_def' [IsProbabilityMeasure μ] {X : Ω → ℝ} (hX : AEStronglyMeasurable X μ) : evariance X μ = (∫⁻ ω, ‖X ω‖ₑ ^ 2 ∂μ) - ENNReal.ofReal (μ[X] ^ 2) := by by_cases hℒ : MemLp X 2 μ · rw [← ofReal_variance hℒ, variance_def' hℒ, ENNReal.ofReal_sub _ (sq_nonneg _)] congr simp_rw [← enorm_pow, enorm] rw [lintegral_coe_eq_integral] · simp · simpa using hℒ.abs.integrable_sq · symm rw [evariance_eq_top hX hℒ, ENNReal.sub_eq_top_iff] refine ⟨?_, ENNReal.ofReal_ne_top⟩ rw [MemLp, not_and] at hℒ specialize hℒ hX simp only [eLpNorm_eq_lintegral_rpow_enorm two_ne_zero ENNReal.ofNat_ne_top, not_lt, top_le_iff, ENNReal.toReal_ofNat, one_div, ENNReal.rpow_eq_top_iff, inv_lt_zero, inv_pos, and_true, or_iff_not_imp_left, not_and_or, zero_lt_two] at hℒ exact mod_cast hℒ fun _ => zero_le_two /-- **Chebyshev's inequality** for `ℝ≥0∞`-valued variance. -/ theorem meas_ge_le_evariance_div_sq {X : Ω → ℝ} (hX : AEStronglyMeasurable X μ) {c : ℝ≥0} (hc : c ≠ 0) : μ {ω | ↑c ≤ |X ω - μ[X]|} ≤ evariance X μ / c ^ 2 := by have A : (c : ℝ≥0∞) ≠ 0 := by rwa [Ne, ENNReal.coe_eq_zero] have B : AEStronglyMeasurable (fun _ : Ω => μ[X]) μ := aestronglyMeasurable_const convert meas_ge_le_mul_pow_eLpNorm μ two_ne_zero ENNReal.ofNat_ne_top (hX.sub B) A using 1 · congr simp only [Pi.sub_apply, ENNReal.coe_le_coe, ← Real.norm_eq_abs, ← coe_nnnorm, NNReal.coe_le_coe, ENNReal.ofReal_coe_nnreal] · rw [eLpNorm_eq_lintegral_rpow_enorm two_ne_zero ENNReal.ofNat_ne_top] simp only [show ENNReal.ofNNReal (c ^ 2) = (ENNReal.ofNNReal c) ^ 2 by norm_cast, ENNReal.toReal_ofNat, one_div, Pi.sub_apply] rw [div_eq_mul_inv, ENNReal.inv_pow, mul_comm, ENNReal.rpow_two] congr simp_rw [← ENNReal.rpow_mul, inv_mul_cancel₀ (two_ne_zero : (2 : ℝ) ≠ 0), ENNReal.rpow_two, ENNReal.rpow_one, evariance] /-- **Chebyshev's inequality**: one can control the deviation probability of a real random variable from its expectation in terms of the variance. -/ theorem meas_ge_le_variance_div_sq [IsFiniteMeasure μ] {X : Ω → ℝ} (hX : MemLp X 2 μ) {c : ℝ} (hc : 0 < c) : μ {ω | c ≤ |X ω - μ[X]|} ≤ ENNReal.ofReal (variance X μ / c ^ 2) := by rw [ENNReal.ofReal_div_of_pos (sq_pos_of_ne_zero hc.ne.symm), hX.ofReal_variance_eq] convert @meas_ge_le_evariance_div_sq _ _ _ _ hX.1 c.toNNReal (by simp [hc]) using 1 · simp only [Real.coe_toNNReal', max_le_iff, abs_nonneg, and_true] · rw [ENNReal.ofReal_pow hc.le] rfl -- Porting note: supplied `MeasurableSpace Ω` argument of `h` by unification /-- The variance of the sum of two independent random variables is the sum of the variances. -/ theorem IndepFun.variance_add [IsProbabilityMeasure μ] {X Y : Ω → ℝ} (hX : MemLp X 2 μ) (hY : MemLp Y 2 μ) (h : IndepFun X Y μ) : variance (X + Y) μ = variance X μ + variance Y μ := calc variance (X + Y) μ = μ[fun a => X a ^ 2 + Y a ^ 2 + 2 * X a * Y a] - μ[X + Y] ^ 2 := by simp [variance_def' (hX.add hY), add_sq'] _ = μ[X ^ 2] + μ[Y ^ 2] + (2 : ℝ) * μ[X * Y] - (μ[X] + μ[Y]) ^ 2 := by simp only [Pi.add_apply, Pi.pow_apply, Pi.mul_apply, mul_assoc] rw [integral_add, integral_add, integral_add, integral_const_mul] · exact hX.integrable one_le_two · exact hY.integrable one_le_two · exact hX.integrable_sq · exact hY.integrable_sq · exact hX.integrable_sq.add hY.integrable_sq · apply Integrable.const_mul exact h.integrable_mul (hX.integrable one_le_two) (hY.integrable one_le_two) _ = μ[X ^ 2] + μ[Y ^ 2] + 2 * (μ[X] * μ[Y]) - (μ[X] + μ[Y]) ^ 2 := by
congr exact h.integral_mul_of_integrable (hX.integrable one_le_two) (hY.integrable one_le_two) _ = variance X μ + variance Y μ := by simp only [variance_def', hX, hY, Pi.pow_apply]; ring -- Porting note: supplied `MeasurableSpace Ω` argument of `hs`, `h` by unification /-- The variance of a finite sum of pairwise independent random variables is the sum of the variances. -/ theorem IndepFun.variance_sum [IsProbabilityMeasure μ] {ι : Type*} {X : ι → Ω → ℝ} {s : Finset ι} (hs : ∀ i ∈ s, MemLp (X i) 2 μ) (h : Set.Pairwise ↑s fun i j => IndepFun (X i) (X j) μ) : variance (∑ i ∈ s, X i) μ = ∑ i ∈ s, variance (X i) μ := by classical induction' s using Finset.induction_on with k s ks IH · simp only [Finset.sum_empty, variance_zero] rw [variance_def' (memLp_finset_sum' _ hs), sum_insert ks, sum_insert ks] simp only [add_sq'] calc μ[(X k ^ 2 + (∑ i ∈ s, X i) ^ 2 + 2 * X k * ∑ i ∈ s, X i : Ω → ℝ)] - μ[X k + ∑ i ∈ s, X i] ^ 2 = μ[X k ^ 2] + μ[(∑ i ∈ s, X i) ^ 2] + μ[2 * X k * ∑ i ∈ s, X i] -
Mathlib/Probability/Variance.lean
288
306
/- 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 · simp [h, toMatrix_apply, unitsSMul_apply, Units.smul_def] · simp [h, toMatrix_apply, unitsSMul_apply, Units.smul_def, Ne.symm h] /-- The basis constructed by `isUnitSMul` has vectors given by a diagonal matrix. -/ @[simp] theorem toMatrix_isUnitSMul [DecidableEq ι] (e : Basis ι R₂ M₂) {w : ι → R₂} (hw : ∀ i, IsUnit (w i)) : e.toMatrix (e.isUnitSMul hw) = diagonal w := e.toMatrix_unitsSMul _ theorem toMatrix_smul_left {G} [Group G] [DistribMulAction G M] [SMulCommClass G R M] (g : G) : (g • e).toMatrix v = e.toMatrix (g⁻¹ • v) := rfl @[simp] theorem sum_toMatrix_smul_self [Fintype ι] : ∑ i : ι, e.toMatrix v i j • e i = v j := by simp_rw [e.toMatrix_apply, e.sum_repr] theorem toMatrix_smul {R₁ S : Type*} [CommSemiring R₁] [Semiring S] [Algebra R₁ S] [Fintype ι] [DecidableEq ι] (x : S) (b : Basis ι R₁ S) (w : ι → S) : (b.toMatrix (x • w)) = (Algebra.leftMulMatrix b x) * (b.toMatrix w) := by ext rw [Basis.toMatrix_apply, Pi.smul_apply, smul_eq_mul, ← Algebra.leftMulMatrix_mulVec_repr] rfl theorem toMatrix_map_vecMul {S : Type*} [Semiring S] [Algebra R S] [Fintype ι] (b : Basis ι R S) (v : ι' → S) : b ᵥ* ((b.toMatrix v).map <| algebraMap R S) = v := by ext i simp_rw [vecMul, dotProduct, Matrix.map_apply, ← Algebra.commutes, ← Algebra.smul_def, sum_toMatrix_smul_self] @[simp] theorem toLin_toMatrix [Finite ι] [Fintype ι'] [DecidableEq ι'] (v : Basis ι' R M) : Matrix.toLin v e (e.toMatrix v) = LinearMap.id := v.ext fun i => by cases nonempty_fintype ι; rw [toLin_self, id_apply, e.sum_toMatrix_smul_self] /-- From a basis `e : ι → M`, build a linear equivalence between families of vectors `v : ι → M`, and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def toMatrixEquiv [Fintype ι] (e : Basis ι R M) : (ι → M) ≃ₗ[R] Matrix ι ι R where toFun := e.toMatrix map_add' v w := by
ext i j rw [Matrix.add_apply, e.toMatrix_apply, Pi.add_apply, LinearEquiv.map_add] rfl
Mathlib/LinearAlgebra/Matrix/Basis.lean
132
134
/- Copyright (c) 2018 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Bhavik Mehta -/ import Mathlib.CategoryTheory.Limits.HasLimits import Mathlib.CategoryTheory.Discrete.Basic /-! # Categorical (co)products This file defines (co)products as special cases of (co)limits. A product is the categorical generalization of the object `Π i, f i` where `f : ι → C`. It is a limit cone over the diagram formed by `f`, implemented by converting `f` into a functor `Discrete ι ⥤ C`. A coproduct is the dual concept. ## Main definitions * a `Fan` is a cone over a discrete category * `Fan.mk` constructs a fan from an indexed collection of maps * a `Pi` is a `limit (Discrete.functor f)` Each of these has a dual. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. -/ noncomputable section universe w w' w₂ w₃ v v₂ u u₂ open CategoryTheory namespace CategoryTheory.Limits variable {β : Type w} {α : Type w₂} {γ : Type w₃} variable {C : Type u} [Category.{v} C] -- We don't need an analogue of `Pair` (for binary products), `ParallelPair` (for equalizers), -- or `(Co)span`, since we already have `Discrete.functor`. /-- A fan over `f : β → C` consists of a collection of maps from an object `P` to every `f b`. -/ abbrev Fan (f : β → C) := Cone (Discrete.functor f) /-- A cofan over `f : β → C` consists of a collection of maps from every `f b` to an object `P`. -/ abbrev Cofan (f : β → C) := Cocone (Discrete.functor f) /-- A fan over `f : β → C` consists of a collection of maps from an object `P` to every `f b`. -/ @[simps! pt π_app] def Fan.mk {f : β → C} (P : C) (p : ∀ b, P ⟶ f b) : Fan f where pt := P π := Discrete.natTrans (fun X => p X.as) /-- A cofan over `f : β → C` consists of a collection of maps from every `f b` to an object `P`. -/ @[simps! pt ι_app] def Cofan.mk {f : β → C} (P : C) (p : ∀ b, f b ⟶ P) : Cofan f where pt := P ι := Discrete.natTrans (fun X => p X.as) /-- Get the `j`th "projection" in the fan. (Note that the initial letter of `proj` matches the greek letter in `Cone.π`.) -/ def Fan.proj {f : β → C} (p : Fan f) (j : β) : p.pt ⟶ f j := p.π.app (Discrete.mk j) /-- Get the `j`th "injection" in the cofan. (Note that the initial letter of `inj` matches the greek letter in `Cocone.ι`.) -/ def Cofan.inj {f : β → C} (p : Cofan f) (j : β) : f j ⟶ p.pt := p.ι.app (Discrete.mk j) @[simp] theorem fan_mk_proj {f : β → C} (P : C) (p : ∀ b, P ⟶ f b) : (Fan.mk P p).proj = p := rfl @[simp] theorem cofan_mk_inj {f : β → C} (P : C) (p : ∀ b, f b ⟶ P) : (Cofan.mk P p).inj = p := rfl /-- An abbreviation for `HasLimit (Discrete.functor f)`. -/ abbrev HasProduct (f : β → C) := HasLimit (Discrete.functor f) /-- An abbreviation for `HasColimit (Discrete.functor f)`. -/ abbrev HasCoproduct (f : β → C) := HasColimit (Discrete.functor f) lemma hasCoproduct_of_equiv_of_iso (f : α → C) (g : β → C) [HasCoproduct f] (e : β ≃ α) (iso : ∀ j, g j ≅ f (e j)) : HasCoproduct g := by have : HasColimit ((Discrete.equivalence e).functor ⋙ Discrete.functor f) := hasColimit_equivalence_comp _ have α : Discrete.functor g ≅ (Discrete.equivalence e).functor ⋙ Discrete.functor f := Discrete.natIso (fun ⟨j⟩ => iso j) exact hasColimit_of_iso α lemma hasProduct_of_equiv_of_iso (f : α → C) (g : β → C) [HasProduct f] (e : β ≃ α) (iso : ∀ j, g j ≅ f (e j)) : HasProduct g := by have : HasLimit ((Discrete.equivalence e).functor ⋙ Discrete.functor f) := hasLimitEquivalenceComp _ have α : Discrete.functor g ≅ (Discrete.equivalence e).functor ⋙ Discrete.functor f := Discrete.natIso (fun ⟨j⟩ => iso j) exact hasLimit_of_iso α.symm /-- Make a fan `f` into a limit fan by providing `lift`, `fac`, and `uniq` -- just a convenience lemma to avoid having to go through `Discrete` -/ @[simps] def mkFanLimit {f : β → C} (t : Fan f) (lift : ∀ s : Fan f, s.pt ⟶ t.pt) (fac : ∀ (s : Fan f) (j : β), lift s ≫ t.proj j = s.proj j := by aesop_cat) (uniq : ∀ (s : Fan f) (m : s.pt ⟶ t.pt) (_ : ∀ j : β, m ≫ t.proj j = s.proj j), m = lift s := by aesop_cat) : IsLimit t := { lift } /-- Constructor for morphisms to the point of a limit fan. -/ def Fan.IsLimit.desc {F : β → C} {c : Fan F} (hc : IsLimit c) {A : C} (f : ∀ i, A ⟶ F i) : A ⟶ c.pt := hc.lift (Fan.mk A f) @[reassoc (attr := simp)] lemma Fan.IsLimit.fac {F : β → C} {c : Fan F} (hc : IsLimit c) {A : C} (f : ∀ i, A ⟶ F i) (i : β) : Fan.IsLimit.desc hc f ≫ c.proj i = f i := hc.fac (Fan.mk A f) ⟨i⟩ lemma Fan.IsLimit.hom_ext {I : Type*} {F : I → C} {c : Fan F} (hc : IsLimit c) {A : C} (f g : A ⟶ c.pt) (h : ∀ i, f ≫ c.proj i = g ≫ c.proj i) : f = g := hc.hom_ext (fun ⟨i⟩ => h i) /-- Make a cofan `f` into a colimit cofan by providing `desc`, `fac`, and `uniq` -- just a convenience lemma to avoid having to go through `Discrete` -/ @[simps] def mkCofanColimit {f : β → C} (s : Cofan f) (desc : ∀ t : Cofan f, s.pt ⟶ t.pt) (fac : ∀ (t : Cofan f) (j : β), s.inj j ≫ desc t = t.inj j := by aesop_cat) (uniq : ∀ (t : Cofan f) (m : s.pt ⟶ t.pt) (_ : ∀ j : β, s.inj j ≫ m = t.inj j), m = desc t := by aesop_cat) : IsColimit s := { desc } /-- Constructor for morphisms from the point of a colimit cofan. -/ def Cofan.IsColimit.desc {F : β → C} {c : Cofan F} (hc : IsColimit c) {A : C} (f : ∀ i, F i ⟶ A) : c.pt ⟶ A := hc.desc (Cofan.mk A f) @[reassoc (attr := simp)] lemma Cofan.IsColimit.fac {F : β → C} {c : Cofan F} (hc : IsColimit c) {A : C} (f : ∀ i, F i ⟶ A) (i : β) : c.inj i ≫ Cofan.IsColimit.desc hc f = f i := hc.fac (Cofan.mk A f) ⟨i⟩ lemma Cofan.IsColimit.hom_ext {I : Type*} {F : I → C} {c : Cofan F} (hc : IsColimit c) {A : C} (f g : c.pt ⟶ A) (h : ∀ i, c.inj i ≫ f = c.inj i ≫ g) : f = g := hc.hom_ext (fun ⟨i⟩ => h i) section variable (C) /-- An abbreviation for `HasLimitsOfShape (Discrete f)`. -/ abbrev HasProductsOfShape (β : Type v) := HasLimitsOfShape.{v} (Discrete β) /-- An abbreviation for `HasColimitsOfShape (Discrete f)`. -/ abbrev HasCoproductsOfShape (β : Type v) := HasColimitsOfShape.{v} (Discrete β) end /-- `piObj f` computes the product of a family of elements `f`. (It is defined as an abbreviation for `limit (Discrete.functor f)`, so for most facts about `piObj f`, you will just use general facts about limits.) -/ abbrev piObj (f : β → C) [HasProduct f] := limit (Discrete.functor f) /-- `sigmaObj f` computes the coproduct of a family of elements `f`. (It is defined as an abbreviation for `colimit (Discrete.functor f)`, so for most facts about `sigmaObj f`, you will just use general facts about colimits.) -/ abbrev sigmaObj (f : β → C) [HasCoproduct f] := colimit (Discrete.functor f) /-- notation for categorical products. We need `ᶜ` to avoid conflict with `Finset.prod`. -/ notation "∏ᶜ " f:60 => piObj f /-- notation for categorical coproducts -/ notation "∐ " f:60 => sigmaObj f /-- The `b`-th projection from the pi object over `f` has the form `∏ᶜ f ⟶ f b`. -/ abbrev Pi.π (f : β → C) [HasProduct f] (b : β) : ∏ᶜ f ⟶ f b := limit.π (Discrete.functor f) (Discrete.mk b) /-- The `b`-th inclusion into the sigma object over `f` has the form `f b ⟶ ∐ f`. -/ abbrev Sigma.ι (f : β → C) [HasCoproduct f] (b : β) : f b ⟶ ∐ f := colimit.ι (Discrete.functor f) (Discrete.mk b) -- Porting note (https://github.com/leanprover-community/mathlib4/issues/10688): added the next two lemmas to ease automation; without these lemmas, -- `limit.hom_ext` would be applied, but the goal would involve terms -- in `Discrete β` rather than `β` itself @[ext 1050] lemma Pi.hom_ext {f : β → C} [HasProduct f] {X : C} (g₁ g₂ : X ⟶ ∏ᶜ f) (h : ∀ (b : β), g₁ ≫ Pi.π f b = g₂ ≫ Pi.π f b) : g₁ = g₂ := limit.hom_ext (fun ⟨j⟩ => h j) @[ext 1050] lemma Sigma.hom_ext {f : β → C} [HasCoproduct f] {X : C} (g₁ g₂ : ∐ f ⟶ X) (h : ∀ (b : β), Sigma.ι f b ≫ g₁ = Sigma.ι f b ≫ g₂) : g₁ = g₂ := colimit.hom_ext (fun ⟨j⟩ => h j) /-- The fan constructed of the projections from the product is limiting. -/ def productIsProduct (f : β → C) [HasProduct f] : IsLimit (Fan.mk _ (Pi.π f)) := IsLimit.ofIsoLimit (limit.isLimit (Discrete.functor f)) (Cones.ext (Iso.refl _)) /-- The cofan constructed of the inclusions from the coproduct is colimiting. -/ def coproductIsCoproduct (f : β → C) [HasCoproduct f] : IsColimit (Cofan.mk _ (Sigma.ι f)) := IsColimit.ofIsoColimit (colimit.isColimit (Discrete.functor f)) (Cocones.ext (Iso.refl _)) -- 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 `Pi.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem Pi.π_comp_eqToHom {J : Type*} (f : J → C) [HasProduct f] {j j' : J} (w : j = j') : Pi.π f j ≫ eqToHom (by simp [w]) = Pi.π 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 `Sigma.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem Sigma.eqToHom_comp_ι {J : Type*} (f : J → C) [HasCoproduct f] {j j' : J} (w : j = j') : eqToHom (by simp [w]) ≫ Sigma.ι f j' = Sigma.ι f j := by cases w simp /-- A collection of morphisms `P ⟶ f b` induces a morphism `P ⟶ ∏ᶜ f`. -/ abbrev Pi.lift {f : β → C} [HasProduct f] {P : C} (p : ∀ b, P ⟶ f b) : P ⟶ ∏ᶜ f := limit.lift _ (Fan.mk P p) theorem Pi.lift_π {β : Type w} {f : β → C} [HasProduct f] {P : C} (p : ∀ b, P ⟶ f b) (b : β) : Pi.lift p ≫ Pi.π f b = p b := by simp only [limit.lift_π, Fan.mk_pt, Fan.mk_π_app] /-- A version of `Cones.ext` for `Fan`s. -/ @[simps!] def Fan.ext {f : β → C} {c₁ c₂ : Fan f} (e : c₁.pt ≅ c₂.pt) (w : ∀ (b : β), c₁.proj b = e.hom ≫ c₂.proj b := by aesop_cat) : c₁ ≅ c₂ := Cones.ext e (fun ⟨j⟩ => w j) /-- A collection of morphisms `f b ⟶ P` induces a morphism `∐ f ⟶ P`. -/ abbrev Sigma.desc {f : β → C} [HasCoproduct f] {P : C} (p : ∀ b, f b ⟶ P) : ∐ f ⟶ P := colimit.desc _ (Cofan.mk P p) theorem Sigma.ι_desc {β : Type w} {f : β → C} [HasCoproduct f] {P : C} (p : ∀ b, f b ⟶ P) (b : β) : Sigma.ι f b ≫ Sigma.desc p = p b := by simp only [colimit.ι_desc, Cofan.mk_pt, Cofan.mk_ι_app] instance {f : β → C} [HasCoproduct f] : IsIso (Sigma.desc (fun a ↦ Sigma.ι f a)) := by convert IsIso.id _ ext simp /-- A version of `Cocones.ext` for `Cofan`s. -/ @[simps!] def Cofan.ext {f : β → C} {c₁ c₂ : Cofan f} (e : c₁.pt ≅ c₂.pt) (w : ∀ (b : β), c₁.inj b ≫ e.hom = c₂.inj b := by aesop_cat) : c₁ ≅ c₂ := Cocones.ext e (fun ⟨j⟩ => w j) /-- A cofan `c` on `f` such that the induced map `∐ f ⟶ c.pt` is an iso, is a coproduct. -/ def Cofan.isColimitOfIsIsoSigmaDesc {f : β → C} [HasCoproduct f] (c : Cofan f) [hc : IsIso (Sigma.desc c.inj)] : IsColimit c := IsColimit.ofIsoColimit (colimit.isColimit (Discrete.functor f)) (Cofan.ext (@asIso _ _ _ _ _ hc) (fun _ => colimit.ι_desc _ _)) lemma Cofan.isColimit_iff_isIso_sigmaDesc {f : β → C} [HasCoproduct f] (c : Cofan f) : IsIso (Sigma.desc c.inj) ↔ Nonempty (IsColimit c) := by refine ⟨fun h ↦ ⟨isColimitOfIsIsoSigmaDesc c⟩, fun ⟨hc⟩ ↦ ?_⟩ have : IsIso (((coproductIsCoproduct f).coconePointUniqueUpToIso hc).hom ≫ hc.desc c) := by simp; infer_instance convert this ext simp only [colimit.ι_desc, mk_pt, mk_ι_app, IsColimit.coconePointUniqueUpToIso, coproductIsCoproduct, colimit.cocone_x, Functor.mapIso_hom, IsColimit.uniqueUpToIso_hom, Cocones.forget_map, IsColimit.descCoconeMorphism_hom, IsColimit.ofIsoColimit_desc, Cocones.ext_inv_hom, Iso.refl_inv, colimit.isColimit_desc, Category.id_comp, IsColimit.desc_self, Category.comp_id] rfl /-- A coproduct of coproducts is a coproduct -/ def Cofan.isColimitTrans {X : α → C} (c : Cofan X) (hc : IsColimit c) {β : α → Type*} {Y : (a : α) → β a → C} (π : (a : α) → (b : β a) → Y a b ⟶ X a) (hs : ∀ a, IsColimit (Cofan.mk (X a) (π a))) : IsColimit (Cofan.mk (f := fun ⟨a,b⟩ => Y a b) c.pt (fun (⟨a, b⟩ : Σ a, _) ↦ π a b ≫ c.inj a)) := by refine mkCofanColimit _ ?_ ?_ ?_ · exact fun t ↦ hc.desc (Cofan.mk _ fun a ↦ (hs a).desc (Cofan.mk t.pt (fun b ↦ t.inj ⟨a, b⟩))) · intro t ⟨a, b⟩ simp only [mk_pt, cofan_mk_inj, Category.assoc] erw [hc.fac, (hs a).fac] rfl · intro t m h refine hc.hom_ext fun ⟨a⟩ ↦ (hs a).hom_ext fun ⟨b⟩ ↦ ?_ erw [hc.fac, (hs a).fac] simpa using h ⟨a, b⟩ /-- Construct a morphism between categorical products (indexed by the same type) from a family of morphisms between the factors. -/ abbrev Pi.map {f g : β → C} [HasProduct f] [HasProduct g] (p : ∀ b, f b ⟶ g b) : ∏ᶜ f ⟶ ∏ᶜ g := limMap (Discrete.natTrans fun X => p X.as) @[simp] lemma Pi.map_id {f : α → C} [HasProduct f] : Pi.map (fun a => 𝟙 (f a)) = 𝟙 (∏ᶜ f) := by ext; simp lemma Pi.map_comp_map {f g h : α → C} [HasProduct f] [HasProduct g] [HasProduct h] (q : ∀ (a : α), f a ⟶ g a) (q' : ∀ (a : α), g a ⟶ h a) : Pi.map q ≫ Pi.map q' = Pi.map (fun a => q a ≫ q' a) := by ext; simp instance Pi.map_mono {f g : β → C} [HasProduct f] [HasProduct g] (p : ∀ b, f b ⟶ g b) [∀ i, Mono (p i)] : Mono <| Pi.map p := @Limits.limMap_mono _ _ _ _ (Discrete.functor f) (Discrete.functor g) _ _ (Discrete.natTrans fun X => p X.as) (by dsimp; infer_instance) /-- Construct a morphism between categorical products from a family of morphisms between the factors. -/ def Pi.map' {f : α → C} {g : β → C} [HasProduct f] [HasProduct g] (p : β → α) (q : ∀ (b : β), f (p b) ⟶ g b) : ∏ᶜ f ⟶ ∏ᶜ g := Pi.lift (fun a => Pi.π _ _ ≫ q a) @[reassoc (attr := simp)] lemma Pi.map'_comp_π {f : α → C} {g : β → C} [HasProduct f] [HasProduct g] (p : β → α) (q : ∀ (b : β), f (p b) ⟶ g b) (b : β) : Pi.map' p q ≫ Pi.π g b = Pi.π f (p b) ≫ q b := limit.lift_π _ _ lemma Pi.map'_id_id {f : α → C} [HasProduct f] : Pi.map' id (fun a => 𝟙 (f a)) = 𝟙 (∏ᶜ f) := by ext; simp @[simp] lemma Pi.map'_id {f g : α → C} [HasProduct f] [HasProduct g] (p : ∀ b, f b ⟶ g b) : Pi.map' id p = Pi.map p := rfl lemma Pi.map'_comp_map' {f : α → C} {g : β → C} {h : γ → C} [HasProduct f] [HasProduct g] [HasProduct h] (p : β → α) (p' : γ → β) (q : ∀ (b : β), f (p b) ⟶ g b) (q' : ∀ (c : γ), g (p' c) ⟶ h c) : Pi.map' p q ≫ Pi.map' p' q' = Pi.map' (p ∘ p') (fun c => q (p' c) ≫ q' c) := by ext; simp lemma Pi.map'_comp_map {f : α → C} {g h : β → C} [HasProduct f] [HasProduct g] [HasProduct h] (p : β → α) (q : ∀ (b : β), f (p b) ⟶ g b) (q' : ∀ (b : β), g b ⟶ h b) : Pi.map' p q ≫ Pi.map q' = Pi.map' p (fun b => q b ≫ q' b) := by ext; simp lemma Pi.map_comp_map' {f g : α → C} {h : β → C} [HasProduct f] [HasProduct g] [HasProduct h] (p : β → α) (q : ∀ (a : α), f a ⟶ g a) (q' : ∀ (b : β), g (p b) ⟶ h b) : Pi.map q ≫ Pi.map' p q' = Pi.map' p (fun b => q (p b) ≫ q' b) := by ext; simp lemma Pi.map'_eq {f : α → C} {g : β → C} [HasProduct f] [HasProduct g] {p p' : β → α} {q : ∀ (b : β), f (p b) ⟶ g b} {q' : ∀ (b : β), f (p' b) ⟶ g b} (hp : p = p') (hq : ∀ (b : β), eqToHom (hp ▸ rfl) ≫ q b = q' b) : Pi.map' p q = Pi.map' p' q' := by aesop_cat /-- Construct an isomorphism between categorical products (indexed by the same type) from a family of isomorphisms between the factors. -/ abbrev Pi.mapIso {f g : β → C} [HasProductsOfShape β C] (p : ∀ b, f b ≅ g b) : ∏ᶜ f ≅ ∏ᶜ g := lim.mapIso (Discrete.natIso fun X => p X.as) instance Pi.map_isIso {f g : β → C} [HasProductsOfShape β C] (p : ∀ b, f b ⟶ g b) [∀ b, IsIso <| p b] : IsIso <| Pi.map p := inferInstanceAs (IsIso (Pi.mapIso (fun b ↦ asIso (p b))).hom) section /- In this section, we provide some API for products when we are given a functor `Discrete α ⥤ C` instead of a map `α → C`. -/ variable (X : Discrete α ⥤ C) [HasProduct (fun j => X.obj (Discrete.mk j))] /-- A limit cone for `X : Discrete α ⥤ C` that is given by `∏ᶜ (fun j => X.obj (Discrete.mk j))`. -/ @[simps] def Pi.cone : Cone X where pt := ∏ᶜ (fun j => X.obj (Discrete.mk j)) π := Discrete.natTrans (fun _ => Pi.π _ _) /-- The cone `Pi.cone X` is a limit cone. -/ def productIsProduct' : IsLimit (Pi.cone X) where lift s := Pi.lift (fun j => s.π.app ⟨j⟩) fac s := by simp uniq s m hm := by dsimp ext simp only [limit.lift_π, Fan.mk_pt, Fan.mk_π_app] apply hm variable [HasLimit X] /-- The isomorphism `∏ᶜ (fun j => X.obj (Discrete.mk j)) ≅ limit X`. -/ def Pi.isoLimit : ∏ᶜ (fun j => X.obj (Discrete.mk j)) ≅ limit X := IsLimit.conePointUniqueUpToIso (productIsProduct' X) (limit.isLimit X) @[reassoc (attr := simp)] lemma Pi.isoLimit_inv_π (j : α) : (Pi.isoLimit X).inv ≫ Pi.π _ j = limit.π _ (Discrete.mk j) := IsLimit.conePointUniqueUpToIso_inv_comp _ _ _ @[reassoc (attr := simp)] lemma Pi.isoLimit_hom_π (j : α) : (Pi.isoLimit X).hom ≫ limit.π _ (Discrete.mk j) = Pi.π _ j := IsLimit.conePointUniqueUpToIso_hom_comp _ _ _ end /-- Construct a morphism between categorical coproducts (indexed by the same type) from a family of morphisms between the factors. -/ abbrev Sigma.map {f g : β → C} [HasCoproduct f] [HasCoproduct g] (p : ∀ b, f b ⟶ g b) : ∐ f ⟶ ∐ g := colimMap (Discrete.natTrans fun X => p X.as) @[simp] lemma Sigma.map_id {f : α → C} [HasCoproduct f] : Sigma.map (fun a => 𝟙 (f a)) = 𝟙 (∐ f) := by ext; simp lemma Sigma.map_comp_map {f g h : α → C} [HasCoproduct f] [HasCoproduct g] [HasCoproduct h] (q : ∀ (a : α), f a ⟶ g a) (q' : ∀ (a : α), g a ⟶ h a) : Sigma.map q ≫ Sigma.map q' = Sigma.map (fun a => q a ≫ q' a) := by ext; simp instance Sigma.map_epi {f g : β → C} [HasCoproduct f] [HasCoproduct g] (p : ∀ b, f b ⟶ g b) [∀ i, Epi (p i)] : Epi <| Sigma.map p := @Limits.colimMap_epi _ _ _ _ (Discrete.functor f) (Discrete.functor g) _ _ (Discrete.natTrans fun X => p X.as) (by dsimp; infer_instance) /-- Construct a morphism between categorical coproducts from a family of morphisms between the factors. -/ def Sigma.map' {f : α → C} {g : β → C} [HasCoproduct f] [HasCoproduct g] (p : α → β) (q : ∀ (a : α), f a ⟶ g (p a)) : ∐ f ⟶ ∐ g := Sigma.desc (fun a => q a ≫ Sigma.ι _ _) @[reassoc (attr := simp)] lemma Sigma.ι_comp_map' {f : α → C} {g : β → C} [HasCoproduct f] [HasCoproduct g] (p : α → β) (q : ∀ (a : α), f a ⟶ g (p a)) (a : α) : Sigma.ι f a ≫ Sigma.map' p q = q a ≫ Sigma.ι g (p a) := colimit.ι_desc _ _ lemma Sigma.map'_id_id {f : α → C} [HasCoproduct f] : Sigma.map' id (fun a => 𝟙 (f a)) = 𝟙 (∐ f) := by ext; simp @[simp] lemma Sigma.map'_id {f g : α → C} [HasCoproduct f] [HasCoproduct g] (p : ∀ b, f b ⟶ g b) : Sigma.map' id p = Sigma.map p := rfl lemma Sigma.map'_comp_map' {f : α → C} {g : β → C} {h : γ → C} [HasCoproduct f] [HasCoproduct g] [HasCoproduct h] (p : α → β) (p' : β → γ) (q : ∀ (a : α), f a ⟶ g (p a)) (q' : ∀ (b : β), g b ⟶ h (p' b)) : Sigma.map' p q ≫ Sigma.map' p' q' = Sigma.map' (p' ∘ p) (fun a => q a ≫ q' (p a)) := by ext; simp lemma Sigma.map'_comp_map {f : α → C} {g h : β → C} [HasCoproduct f] [HasCoproduct g] [HasCoproduct h] (p : α → β) (q : ∀ (a : α), f a ⟶ g (p a)) (q' : ∀ (b : β), g b ⟶ h b) : Sigma.map' p q ≫ Sigma.map q' = Sigma.map' p (fun a => q a ≫ q' (p a)) := by ext; simp lemma Sigma.map_comp_map' {f g : α → C} {h : β → C} [HasCoproduct f] [HasCoproduct g] [HasCoproduct h] (p : α → β) (q : ∀ (a : α), f a ⟶ g a) (q' : ∀ (a : α), g a ⟶ h (p a)) : Sigma.map q ≫ Sigma.map' p q' = Sigma.map' p (fun a => q a ≫ q' a) := by ext; simp
lemma Sigma.map'_eq {f : α → C} {g : β → C} [HasCoproduct f] [HasCoproduct g] {p p' : α → β} {q : ∀ (a : α), f a ⟶ g (p a)} {q' : ∀ (a : α), f a ⟶ g (p' a)} (hp : p = p') (hq : ∀ (a : α), q a ≫ eqToHom (hp ▸ rfl) = q' a) : Sigma.map' p q = Sigma.map' p' q' := by
Mathlib/CategoryTheory/Limits/Shapes/Products.lean
482
486
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import Mathlib.Algebra.ContinuedFractions.Determinant import Mathlib.Algebra.ContinuedFractions.Computation.CorrectnessTerminating import Mathlib.Algebra.Order.Ring.Basic import Mathlib.Data.Nat.Fib.Basic import Mathlib.Tactic.Monotonicity /-! # Approximations for Continued Fraction Computations (`GenContFract.of`) ## Summary This file contains useful approximations for the values involved in the continued fractions computation `GenContFract.of`. In particular, we show that the generalized continued fraction given by `GenContFract.of` in fact is a (regular) continued fraction. Moreover, we derive some upper bounds for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (GenContFract.of v).convs n|`. The derived bounds will show us that the error term indeed gets smaller. As a corollary, we will be able to show that `(GenContFract.of v).convs` converges to `v` in `Algebra.ContinuedFractions.Computation.ApproximationCorollaries`. ## Main Theorems - `GenContFract.of_partNum_eq_one`: shows that all partial numerators `aᵢ` are equal to one. - `GenContFract.exists_int_eq_of_partDen`: shows that all partial denominators `bᵢ` correspond to an integer. - `GenContFract.of_one_le_get?_partDen`: shows that `1 ≤ bᵢ`. - `ContFract.of` returns the regular continued fraction of a value. - `GenContFract.succ_nth_fib_le_of_nthDen`: shows that the `n`th denominator `Bₙ` is greater than or equal to the `n + 1`th fibonacci number `Nat.fib (n + 1)`. - `GenContFract.le_of_succ_get?_den`: shows that `bₙ * Bₙ ≤ Bₙ₊₁`, where `bₙ` is the `n`th partial denominator of the continued fraction. - `GenContFract.abs_sub_convs_le`: shows that `|v - Aₙ / Bₙ| ≤ 1 / (Bₙ * Bₙ₊₁)`, where `Aₙ` is the `n`th partial numerator. ## References - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] -/ open GenContFract open GenContFract (of) open Int variable {K : Type*} {v : K} {n : ℕ} [Field K] [LinearOrder K] [IsStrictOrderedRing K] [FloorRing K] namespace GenContFract namespace IntFractPair /-! We begin with some lemmas about the stream of `IntFractPair`s, which presumably are not of great interest for the end user. -/ /-- Shows that the fractional parts of the stream are in `[0,1)`. -/ theorem nth_stream_fr_nonneg_lt_one {ifp_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr ∧ ifp_n.fr < 1 := by cases n with | zero => have : IntFractPair.of v = ifp_n := by injection nth_stream_eq rw [← this, IntFractPair.of] exact ⟨fract_nonneg _, fract_lt_one _⟩ | succ => rcases succ_nth_stream_eq_some_iff.1 nth_stream_eq with ⟨_, _, _, ifp_of_eq_ifp_n⟩ rw [← ifp_of_eq_ifp_n, IntFractPair.of] exact ⟨fract_nonneg _, fract_lt_one _⟩ /-- Shows that the fractional parts of the stream are nonnegative. -/ theorem nth_stream_fr_nonneg {ifp_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr := (nth_stream_fr_nonneg_lt_one nth_stream_eq).left /-- Shows that the fractional parts of the stream are smaller than one. -/ theorem nth_stream_fr_lt_one {ifp_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) : ifp_n.fr < 1 := (nth_stream_fr_nonneg_lt_one nth_stream_eq).right /-- Shows that the integer parts of the stream are at least one. -/ theorem one_le_succ_nth_stream_b {ifp_succ_n : IntFractPair K} (succ_nth_stream_eq : IntFractPair.stream v (n + 1) = some ifp_succ_n) : 1 ≤ ifp_succ_n.b := by obtain ⟨ifp_n, nth_stream_eq, stream_nth_fr_ne_zero, ⟨-⟩⟩ : ∃ ifp_n, IntFractPair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0 ∧ IntFractPair.of ifp_n.fr⁻¹ = ifp_succ_n := succ_nth_stream_eq_some_iff.1 succ_nth_stream_eq rw [IntFractPair.of, le_floor, cast_one, one_le_inv₀ ((nth_stream_fr_nonneg nth_stream_eq).lt_of_ne' stream_nth_fr_ne_zero)] exact (nth_stream_fr_lt_one nth_stream_eq).le omit [IsStrictOrderedRing K] in /-- Shows that the `n + 1`th integer part `bₙ₊₁` of the stream is smaller or equal than the inverse of the `n`th fractional part `frₙ` of the stream. This result is straight-forward as `bₙ₊₁` is defined as the floor of `1 / frₙ`. -/ theorem succ_nth_stream_b_le_nth_stream_fr_inv {ifp_n ifp_succ_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) (succ_nth_stream_eq : IntFractPair.stream v (n + 1) = some ifp_succ_n) : (ifp_succ_n.b : K) ≤ ifp_n.fr⁻¹ := by suffices (⌊ifp_n.fr⁻¹⌋ : K) ≤ ifp_n.fr⁻¹ by obtain ⟨_, ifp_n_fr⟩ := ifp_n have : ifp_n_fr ≠ 0 := by intro h simp [h, IntFractPair.stream, nth_stream_eq] at succ_nth_stream_eq have : IntFractPair.of ifp_n_fr⁻¹ = ifp_succ_n := by simpa [this, IntFractPair.stream, nth_stream_eq, Option.coe_def] using succ_nth_stream_eq rwa [← this] exact floor_le ifp_n.fr⁻¹ end IntFractPair /-! Next we translate above results about the stream of `IntFractPair`s to the computed continued fraction `GenContFract.of`. -/ /-- Shows that the integer parts of the continued fraction are at least one. -/ theorem of_one_le_get?_partDen {b : K} (nth_partDen_eq : (of v).partDens.get? n = some b) : 1 ≤ b := by obtain ⟨gp_n, nth_s_eq, ⟨-⟩⟩ : ∃ gp_n, (of v).s.get? n = some gp_n ∧ gp_n.b = b := exists_s_b_of_partDen nth_partDen_eq obtain ⟨ifp_n, succ_nth_stream_eq, ifp_n_b_eq_gp_n_b⟩ : ∃ ifp, IntFractPair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b := IntFractPair.exists_succ_get?_stream_of_gcf_of_get?_eq_some nth_s_eq rw [← ifp_n_b_eq_gp_n_b] exact mod_cast IntFractPair.one_le_succ_nth_stream_b succ_nth_stream_eq /-- Shows that the partial numerators `aᵢ` of the continued fraction are equal to one and the partial denominators `bᵢ` correspond to integers. -/ theorem of_partNum_eq_one_and_exists_int_partDen_eq {gp : GenContFract.Pair K} (nth_s_eq : (of v).s.get? n = some gp) : gp.a = 1 ∧ ∃ z : ℤ, gp.b = (z : K) := by obtain ⟨ifp, stream_succ_nth_eq, -⟩ : ∃ ifp, IntFractPair.stream v (n + 1) = some ifp ∧ _ := IntFractPair.exists_succ_get?_stream_of_gcf_of_get?_eq_some nth_s_eq have : gp = ⟨1, ifp.b⟩ := by have : (of v).s.get? n = some ⟨1, ifp.b⟩ := get?_of_eq_some_of_succ_get?_intFractPair_stream stream_succ_nth_eq have : some gp = some ⟨1, ifp.b⟩ := by rwa [nth_s_eq] at this injection this simp [this] /-- Shows that the partial numerators `aᵢ` are equal to one. -/ theorem of_partNum_eq_one {a : K} (nth_partNum_eq : (of v).partNums.get? n = some a) : a = 1 := by obtain ⟨gp, nth_s_eq, gp_a_eq_a_n⟩ : ∃ gp, (of v).s.get? n = some gp ∧ gp.a = a := exists_s_a_of_partNum nth_partNum_eq have : gp.a = 1 := (of_partNum_eq_one_and_exists_int_partDen_eq nth_s_eq).left rwa [gp_a_eq_a_n] at this /-- Shows that the partial denominators `bᵢ` correspond to an integer. -/ theorem exists_int_eq_of_partDen {b : K} (nth_partDen_eq : (of v).partDens.get? n = some b) : ∃ z : ℤ, b = (z : K) := by obtain ⟨gp, nth_s_eq, gp_b_eq_b_n⟩ : ∃ gp, (of v).s.get? n = some gp ∧ gp.b = b := exists_s_b_of_partDen nth_partDen_eq have : ∃ z : ℤ, gp.b = (z : K) := (of_partNum_eq_one_and_exists_int_partDen_eq nth_s_eq).right rwa [gp_b_eq_b_n] at this end GenContFract variable (v) theorem GenContFract.of_isSimpContFract : (of v).IsSimpContFract := fun _ _ nth_partNum_eq => of_partNum_eq_one nth_partNum_eq /-- Creates the simple continued fraction of a value. -/ nonrec def SimpContFract.of : SimpContFract K := ⟨of v, GenContFract.of_isSimpContFract v⟩ theorem SimpContFract.of_isContFract : (SimpContFract.of v).IsContFract := fun _ _ nth_partDen_eq => lt_of_lt_of_le zero_lt_one (of_one_le_get?_partDen nth_partDen_eq) /-- Creates the continued fraction of a value. -/ def ContFract.of : ContFract K := ⟨SimpContFract.of v, SimpContFract.of_isContFract v⟩ variable {v} namespace GenContFract /-! One of our next goals is to show that `bₙ * Bₙ ≤ Bₙ₊₁`. For this, we first show that the partial denominators `Bₙ` are bounded from below by the fibonacci sequence `Nat.fib`. This then implies that `0 ≤ Bₙ` and hence `Bₙ₊₂ = bₙ₊₁ * Bₙ₊₁ + Bₙ ≥ bₙ₊₁ * Bₙ₊₁ + 0 = bₙ₊₁ * Bₙ₊₁`. -/ -- open `Nat` as we will make use of fibonacci numbers. open Nat theorem fib_le_of_contsAux_b : n ≤ 1 ∨ ¬(of v).TerminatedAt (n - 2) → (fib n : K) ≤ ((of v).contsAux n).b := Nat.strong_induction_on n (by intro n IH hyp rcases n with (_ | _ | n) · simp [fib_add_two, contsAux] -- case n = 0 · simp [fib_add_two, contsAux] -- case n = 1 · let g := of v -- case 2 ≤ n have : ¬n + 2 ≤ 1 := by omega have not_terminatedAt_n : ¬g.TerminatedAt n := Or.resolve_left hyp this obtain ⟨gp, s_ppred_nth_eq⟩ : ∃ gp, g.s.get? n = some gp := Option.ne_none_iff_exists'.mp not_terminatedAt_n set pconts := g.contsAux (n + 1) with pconts_eq set ppconts := g.contsAux n with ppconts_eq -- use the recurrence of `contsAux` simp only [Nat.succ_eq_add_one, Nat.add_assoc, Nat.reduceAdd] suffices (fib n : K) + fib (n + 1) ≤ gp.a * ppconts.b + gp.b * pconts.b by simpa [g, fib_add_two, add_comm, contsAux_recurrence s_ppred_nth_eq ppconts_eq pconts_eq] -- make use of the fact that `gp.a = 1` suffices (fib n : K) + fib (n + 1) ≤ ppconts.b + gp.b * pconts.b by simpa [of_partNum_eq_one <| partNum_eq_s_a s_ppred_nth_eq] have not_terminatedAt_pred_n : ¬g.TerminatedAt (n - 1) := mt (terminated_stable <| Nat.sub_le n 1) not_terminatedAt_n have not_terminatedAt_ppred_n : ¬TerminatedAt g (n - 2) := mt (terminated_stable (n - 1).pred_le) not_terminatedAt_pred_n -- use the IH to get the inequalities for `pconts` and `ppconts` have ppred_nth_fib_le_ppconts_B : (fib n : K) ≤ ppconts.b := IH n (lt_trans (Nat.lt.base n) <| Nat.lt.base <| n + 1) (Or.inr not_terminatedAt_ppred_n) suffices (fib (n + 1) : K) ≤ gp.b * pconts.b by solve_by_elim [_root_.add_le_add ppred_nth_fib_le_ppconts_B] -- finally use the fact that `1 ≤ gp.b` to solve the goal suffices 1 * (fib (n + 1) : K) ≤ gp.b * pconts.b by rwa [one_mul] at this have one_le_gp_b : (1 : K) ≤ gp.b := of_one_le_get?_partDen (partDen_eq_s_b s_ppred_nth_eq) have : (0 : K) ≤ fib (n + 1) := mod_cast (fib (n + 1)).zero_le have : (0 : K) ≤ gp.b := le_trans zero_le_one one_le_gp_b mono · norm_num · tauto) /-- Shows that the `n`th denominator is greater than or equal to the `n + 1`th fibonacci number, that is `Nat.fib (n + 1) ≤ Bₙ`. -/ theorem succ_nth_fib_le_of_nth_den (hyp : n = 0 ∨ ¬(of v).TerminatedAt (n - 1)) : (fib (n + 1) : K) ≤ (of v).dens n := by rw [den_eq_conts_b, nth_cont_eq_succ_nth_contAux] have : n + 1 ≤ 1 ∨ ¬(of v).TerminatedAt (n - 1) := by cases n with | zero => exact Or.inl <| le_refl 1 | succ n => exact Or.inr (Or.resolve_left hyp n.succ_ne_zero) exact fib_le_of_contsAux_b this /-! As a simple consequence, we can now derive that all denominators are nonnegative. -/ theorem zero_le_of_contsAux_b : 0 ≤ ((of v).contsAux n).b := by let g := of v induction n with | zero => rfl | succ n IH => rcases Decidable.em <| g.TerminatedAt (n - 1) with terminated | not_terminated · -- terminating case rcases n with - | n · simp [zero_le_one] · have : g.contsAux (n + 2) = g.contsAux (n + 1) := contsAux_stable_step_of_terminated terminated simp only [g, this, IH] · -- non-terminating case calc (0 : K) ≤ fib (n + 1) := mod_cast (n + 1).fib.zero_le _ ≤ ((of v).contsAux (n + 1)).b := fib_le_of_contsAux_b (Or.inr not_terminated) /-- Shows that all denominators are nonnegative. -/ theorem zero_le_of_den : 0 ≤ (of v).dens n := by rw [den_eq_conts_b, nth_cont_eq_succ_nth_contAux]; exact zero_le_of_contsAux_b theorem le_of_succ_succ_get?_contsAux_b {b : K} (nth_partDen_eq : (of v).partDens.get? n = some b) : b * ((of v).contsAux <| n + 1).b ≤ ((of v).contsAux <| n + 2).b := by obtain ⟨gp_n, nth_s_eq, rfl⟩ : ∃ gp_n, (of v).s.get? n = some gp_n ∧ gp_n.b = b := exists_s_b_of_partDen nth_partDen_eq simp [of_partNum_eq_one (partNum_eq_s_a nth_s_eq), zero_le_of_contsAux_b, GenContFract.contsAux_recurrence nth_s_eq rfl rfl] /-- Shows that `bₙ * Bₙ ≤ Bₙ₊₁`, where `bₙ` is the `n`th partial denominator and `Bₙ₊₁` and `Bₙ` are the `n + 1`th and `n`th denominator of the continued fraction. -/ theorem le_of_succ_get?_den {b : K} (nth_partDenom_eq : (of v).partDens.get? n = some b) : b * (of v).dens n ≤ (of v).dens (n + 1) := by rw [den_eq_conts_b, nth_cont_eq_succ_nth_contAux] exact le_of_succ_succ_get?_contsAux_b nth_partDenom_eq /-- Shows that the sequence of denominators is monotone, that is `Bₙ ≤ Bₙ₊₁`. -/ theorem of_den_mono : (of v).dens n ≤ (of v).dens (n + 1) := by let g := of v rcases Decidable.em <| g.partDens.TerminatedAt n with terminated | not_terminated · have : g.partDens.get? n = none := by rwa [Stream'.Seq.TerminatedAt] at terminated have : g.TerminatedAt n := terminatedAt_iff_partDen_none.2 (by rwa [Stream'.Seq.TerminatedAt] at terminated) have : g.dens (n + 1) = g.dens n := dens_stable_of_terminated n.le_succ this rw [this] · obtain ⟨b, nth_partDen_eq⟩ : ∃ b, g.partDens.get? n = some b := Option.ne_none_iff_exists'.mp not_terminated have : 1 ≤ b := of_one_le_get?_partDen nth_partDen_eq calc g.dens n ≤ b * g.dens n := by simpa using mul_le_mul_of_nonneg_right this zero_le_of_den _ ≤ g.dens (n + 1) := le_of_succ_get?_den nth_partDen_eq section ErrorTerm /-! ### Approximation of Error Term Next we derive some approximations for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (GenContFract.of v).convs n|`. -/
/-- This lemma follows from the finite correctness proof, the determinant equality, and by simplifying the difference. -/ theorem sub_convs_eq {ifp : IntFractPair K} (stream_nth_eq : IntFractPair.stream v n = some ifp) : let g := of v let B := (g.contsAux (n + 1)).b let pB := (g.contsAux n).b v - g.convs n = if ifp.fr = 0 then 0 else (-1) ^ n / (B * (ifp.fr⁻¹ * B + pB)) := by -- set up some shorthand notation let g := of v let conts := g.contsAux (n + 1) let pred_conts := g.contsAux n have g_finite_correctness : v = GenContFract.compExactValue pred_conts conts ifp.fr := compExactValue_correctness_of_stream_eq_some stream_nth_eq obtain (ifp_fr_eq_zero | ifp_fr_ne_zero) := eq_or_ne ifp.fr 0 · suffices v - g.convs n = 0 by simpa [ifp_fr_eq_zero] replace g_finite_correctness : v = g.convs n := by simpa [GenContFract.compExactValue, ifp_fr_eq_zero] using g_finite_correctness exact sub_eq_zero.2 g_finite_correctness · -- more shorthand notation let A := conts.a let B := conts.b let pA := pred_conts.a let pB := pred_conts.b -- first, let's simplify the goal as `ifp.fr ≠ 0` suffices v - A / B = (-1) ^ n / (B * (ifp.fr⁻¹ * B + pB)) by simpa [ifp_fr_ne_zero] -- now we can unfold `g.compExactValue` to derive the following equality for `v` replace g_finite_correctness : v = (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B) := by simpa [GenContFract.compExactValue, ifp_fr_ne_zero, nextConts, nextNum, nextDen, add_comm] using g_finite_correctness -- let's rewrite this equality for `v` in our goal suffices (pA + ifp.fr⁻¹ * A) / (pB + ifp.fr⁻¹ * B) - A / B = (-1) ^ n / (B * (ifp.fr⁻¹ * B + pB)) by
Mathlib/Algebra/ContinuedFractions/Computation/Approximations.lean
322
357
/- 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.ConditionalExpectation.CondexpL2 import Mathlib.MeasureTheory.Measure.Real /-! # Conditional expectation in L1 This file contains two more steps of the construction of the conditional expectation, which is completed in `MeasureTheory.Function.ConditionalExpectation.Basic`. See that file for a description of the full process. The conditional expectation of an `L²` function is defined in `MeasureTheory.Function.ConditionalExpectation.CondexpL2`. In this file, we perform two steps. * Show that the conditional expectation of the indicator of a measurable set with finite measure is integrable and define a map `Set α → (E →L[ℝ] (α →₁[μ] E))` which to a set associates a linear map. That linear map sends `x ∈ E` to the conditional expectation of the indicator of the set with value `x`. * Extend that map to `condExpL1CLM : (α →₁[μ] E) →L[ℝ] (α →₁[μ] E)`. This is done using the same construction as the Bochner integral (see the file `MeasureTheory/Integral/SetToL1`). ## Main definitions * `condExpL1`: Conditional expectation of a function as a linear map from `L1` to itself. -/ noncomputable section open TopologicalSpace MeasureTheory.Lp Filter ContinuousLinearMap open scoped NNReal ENNReal Topology MeasureTheory namespace MeasureTheory variable {α F F' G G' 𝕜 : Type*} [RCLike 𝕜] -- 𝕜 for ℝ or ℂ -- F for a Lp submodule [NormedAddCommGroup F] [NormedSpace 𝕜 F] -- F' for integrals on a Lp submodule [NormedAddCommGroup F'] [NormedSpace 𝕜 F'] [NormedSpace ℝ F'] [CompleteSpace F'] -- G for a Lp add_subgroup [NormedAddCommGroup G] -- G' for integrals on a Lp add_subgroup [NormedAddCommGroup G'] [NormedSpace ℝ G'] [CompleteSpace G'] section CondexpInd /-! ## Conditional expectation of an indicator as a continuous linear map. The goal of this section is to build `condExpInd (hm : m ≤ m0) (μ : Measure α) (s : Set s) : G →L[ℝ] α →₁[μ] G`, which takes `x : G` to the conditional expectation of the indicator of the set `s` with value `x`, seen as an element of `α →₁[μ] G`. -/ variable {m m0 : MeasurableSpace α} {μ : Measure α} {s t : Set α} [NormedSpace ℝ G] section CondexpIndL1Fin /-- Conditional expectation of the indicator of a measurable set with finite measure, as a function in L1. -/ def condExpIndL1Fin (hm : m ≤ m0) [SigmaFinite (μ.trim hm)] (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : G) : α →₁[μ] G := (integrable_condExpIndSMul hm hs hμs x).toL1 _ @[deprecated (since := "2025-01-21")] noncomputable alias condexpIndL1Fin := condExpIndL1Fin theorem condExpIndL1Fin_ae_eq_condExpIndSMul (hm : m ≤ m0) [SigmaFinite (μ.trim hm)] (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : G) : condExpIndL1Fin hm hs hμs x =ᵐ[μ] condExpIndSMul hm hs hμs x := (integrable_condExpIndSMul hm hs hμs x).coeFn_toL1 @[deprecated (since := "2025-01-21")] alias condexpIndL1Fin_ae_eq_condexpIndSMul := condExpIndL1Fin_ae_eq_condExpIndSMul variable {hm : m ≤ m0} [SigmaFinite (μ.trim hm)] -- Porting note: this lemma fills the hole in `refine' (MemLp.coeFn_toLp _) ...` -- which is not automatically filled in Lean 4 private theorem q {hs : MeasurableSet s} {hμs : μ s ≠ ∞} {x : G} : MemLp (condExpIndSMul hm hs hμs x) 1 μ := by rw [memLp_one_iff_integrable]; apply integrable_condExpIndSMul theorem condExpIndL1Fin_add (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x y : G) : condExpIndL1Fin hm hs hμs (x + y) = condExpIndL1Fin hm hs hμs x + condExpIndL1Fin hm hs hμs y := by ext1 refine (MemLp.coeFn_toLp q).trans ?_ refine EventuallyEq.trans ?_ (Lp.coeFn_add _ _).symm refine EventuallyEq.trans ?_ (EventuallyEq.add (MemLp.coeFn_toLp q).symm (MemLp.coeFn_toLp q).symm) rw [condExpIndSMul_add] refine (Lp.coeFn_add _ _).trans (Eventually.of_forall fun a => ?_) rfl @[deprecated (since := "2025-01-21")] alias condexpIndL1Fin_add := condExpIndL1Fin_add theorem condExpIndL1Fin_smul (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (c : ℝ) (x : G) : condExpIndL1Fin hm hs hμs (c • x) = c • condExpIndL1Fin hm hs hμs x := by ext1 refine (MemLp.coeFn_toLp q).trans ?_ refine EventuallyEq.trans ?_ (Lp.coeFn_smul _ _).symm rw [condExpIndSMul_smul hs hμs c x] refine (Lp.coeFn_smul _ _).trans ?_ refine (condExpIndL1Fin_ae_eq_condExpIndSMul hm hs hμs x).mono fun y hy => ?_ simp only [Pi.smul_apply, hy] @[deprecated (since := "2025-01-21")] alias condexpIndL1Fin_smul := condExpIndL1Fin_smul theorem condExpIndL1Fin_smul' [NormedSpace ℝ F] [SMulCommClass ℝ 𝕜 F] (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (c : 𝕜) (x : F) : condExpIndL1Fin hm hs hμs (c • x) = c • condExpIndL1Fin hm hs hμs x := by ext1 refine (MemLp.coeFn_toLp q).trans ?_ refine EventuallyEq.trans ?_ (Lp.coeFn_smul _ _).symm rw [condExpIndSMul_smul' hs hμs c x] refine (Lp.coeFn_smul _ _).trans ?_ refine (condExpIndL1Fin_ae_eq_condExpIndSMul hm hs hμs x).mono fun y hy => ?_
simp only [Pi.smul_apply, hy] @[deprecated (since := "2025-01-21")] alias condexpIndL1Fin_smul' := condExpIndL1Fin_smul' theorem norm_condExpIndL1Fin_le (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : G) : ‖condExpIndL1Fin hm hs hμs x‖ ≤ μ.real s * ‖x‖ := by rw [L1.norm_eq_integral_norm, ← ENNReal.toReal_ofReal (norm_nonneg x), measureReal_def, ← ENNReal.toReal_mul, ← ENNReal.ofReal_le_iff_le_toReal (ENNReal.mul_ne_top hμs ENNReal.ofReal_ne_top), ofReal_integral_norm_eq_lintegral_enorm] swap; · rw [← memLp_one_iff_integrable]; exact Lp.memLp _ have h_eq : ∫⁻ a, ‖condExpIndL1Fin hm hs hμs x a‖ₑ ∂μ = ∫⁻ a, ‖condExpIndSMul hm hs hμs x a‖ₑ ∂μ := by refine lintegral_congr_ae ?_ refine (condExpIndL1Fin_ae_eq_condExpIndSMul hm hs hμs x).mono fun z hz => ?_ dsimp only
Mathlib/MeasureTheory/Function/ConditionalExpectation/CondexpL1.lean
128
143
/- Copyright (c) 2022 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Junyan Xu -/ import Mathlib.Data.DFinsupp.Defs /-! # Locus of unequal values of finitely supported dependent functions Let `N : α → Type*` be a type family, assume that `N a` has a `0` for all `a : α` and let `f g : Π₀ a, N a` be finitely supported dependent functions. ## Main definition * `DFinsupp.neLocus f g : Finset α`, the finite subset of `α` where `f` and `g` differ. In the case in which `N a` is an additive group for all `a`, `DFinsupp.neLocus f g` coincides with `DFinsupp.support (f - g)`. -/ variable {α : Type*} {N : α → Type*} namespace DFinsupp variable [DecidableEq α] section NHasZero variable [∀ a, DecidableEq (N a)] [∀ a, Zero (N a)] (f g : Π₀ a, N a) /-- Given two finitely supported functions `f g : α →₀ N`, `Finsupp.neLocus f g` is the `Finset` where `f` and `g` differ. This generalizes `(f - g).support` to situations without subtraction. -/ def neLocus (f g : Π₀ a, N a) : Finset α := (f.support ∪ g.support).filter fun x ↦ f x ≠ g x @[simp] theorem mem_neLocus {f g : Π₀ a, N a} {a : α} : a ∈ f.neLocus g ↔ f a ≠ g a := by simpa only [neLocus, Finset.mem_filter, Finset.mem_union, mem_support_iff, and_iff_right_iff_imp] using Ne.ne_or_ne _ theorem not_mem_neLocus {f g : Π₀ a, N a} {a : α} : a ∉ f.neLocus g ↔ f a = g a := mem_neLocus.not.trans not_ne_iff @[simp] theorem coe_neLocus : ↑(f.neLocus g) = { x | f x ≠ g x } := Set.ext fun _x ↦ mem_neLocus @[simp] theorem neLocus_eq_empty {f g : Π₀ a, N a} : f.neLocus g = ∅ ↔ f = g := ⟨fun h ↦ ext fun a ↦ not_not.mp (mem_neLocus.not.mp (Finset.eq_empty_iff_forall_not_mem.mp h a)), fun h ↦ h ▸ by simp only [neLocus, Ne, eq_self_iff_true, not_true, Finset.filter_False]⟩ @[simp] theorem nonempty_neLocus_iff {f g : Π₀ a, N a} : (f.neLocus g).Nonempty ↔ f ≠ g := Finset.nonempty_iff_ne_empty.trans neLocus_eq_empty.not theorem neLocus_comm : f.neLocus g = g.neLocus f := by simp_rw [neLocus, Finset.union_comm, ne_comm] @[simp] theorem neLocus_zero_right : f.neLocus 0 = f.support := by ext rw [mem_neLocus, mem_support_iff, coe_zero, Pi.zero_apply] @[simp] theorem neLocus_zero_left : (0 : Π₀ a, N a).neLocus f = f.support := (neLocus_comm _ _).trans (neLocus_zero_right _) end NHasZero section NeLocusAndMaps variable {M P : α → Type*} [∀ a, Zero (N a)] [∀ a, Zero (M a)] [∀ a, Zero (P a)] theorem subset_mapRange_neLocus [∀ a, DecidableEq (N a)] [∀ a, DecidableEq (M a)] (f g : Π₀ a, N a) {F : ∀ a, N a → M a} (F0 : ∀ a, F a 0 = 0) : (f.mapRange F F0).neLocus (g.mapRange F F0) ⊆ f.neLocus g := fun a ↦ by simpa only [mem_neLocus, mapRange_apply, not_imp_not] using congr_arg (F a) theorem zipWith_neLocus_eq_left [∀ a, DecidableEq (N a)] [∀ a, DecidableEq (P a)] {F : ∀ a, M a → N a → P a} (F0 : ∀ a, F a 0 0 = 0) (f : Π₀ a, M a) (g₁ g₂ : Π₀ a, N a) (hF : ∀ a f, Function.Injective fun g ↦ F a f g) : (zipWith F F0 f g₁).neLocus (zipWith F F0 f g₂) = g₁.neLocus g₂ := by ext a simpa only [mem_neLocus] using (hF a _).ne_iff theorem zipWith_neLocus_eq_right [∀ a, DecidableEq (M a)] [∀ a, DecidableEq (P a)] {F : ∀ a, M a → N a → P a} (F0 : ∀ a, F a 0 0 = 0) (f₁ f₂ : Π₀ a, M a) (g : Π₀ a, N a) (hF : ∀ a g, Function.Injective fun f ↦ F a f g) : (zipWith F F0 f₁ g).neLocus (zipWith F F0 f₂ g) = f₁.neLocus f₂ := by ext a simpa only [mem_neLocus] using (hF a _).ne_iff theorem mapRange_neLocus_eq [∀ a, DecidableEq (N a)] [∀ a, DecidableEq (M a)] (f g : Π₀ a, N a) {F : ∀ a, N a → M a} (F0 : ∀ a, F a 0 = 0) (hF : ∀ a, Function.Injective (F a)) : (f.mapRange F F0).neLocus (g.mapRange F F0) = f.neLocus g := by ext a simpa only [mem_neLocus] using (hF a).ne_iff end NeLocusAndMaps variable [∀ a, DecidableEq (N a)] @[simp] theorem neLocus_add_left [∀ a, AddLeftCancelMonoid (N a)] (f g h : Π₀ a, N a) : (f + g).neLocus (f + h) = g.neLocus h := zipWith_neLocus_eq_left _ _ _ _ fun _a ↦ add_right_injective @[simp] theorem neLocus_add_right [∀ a, AddRightCancelMonoid (N a)] (f g h : Π₀ a, N a) : (f + h).neLocus (g + h) = f.neLocus g := zipWith_neLocus_eq_right _ _ _ _ fun _a ↦ add_left_injective section AddGroup variable [∀ a, AddGroup (N a)] (f f₁ f₂ g g₁ g₂ : Π₀ a, N a) @[simp] theorem neLocus_neg_neg : neLocus (-f) (-g) = f.neLocus g := mapRange_neLocus_eq _ _ (fun _a ↦ neg_zero) fun _a ↦ neg_injective theorem neLocus_neg : neLocus (-f) g = f.neLocus (-g) := by rw [← neLocus_neg_neg, neg_neg] theorem neLocus_eq_support_sub : f.neLocus g = (f - g).support := by rw [← @neLocus_add_right α N _ _ _ _ _ (-g), add_neg_cancel, neLocus_zero_right, sub_eq_add_neg] @[simp] theorem neLocus_sub_left : neLocus (f - g₁) (f - g₂) = neLocus g₁ g₂ := by simp only [sub_eq_add_neg, @neLocus_add_left α N _ _ _, neLocus_neg_neg] @[simp] theorem neLocus_sub_right : neLocus (f₁ - g) (f₂ - g) = neLocus f₁ f₂ := by simpa only [sub_eq_add_neg] using @neLocus_add_right α N _ _ _ _ _ _ @[simp] theorem neLocus_self_add_right : neLocus f (f + g) = g.support := by rw [← neLocus_zero_left, ← @neLocus_add_left α N _ _ _ f 0 g, add_zero] @[simp] theorem neLocus_self_add_left : neLocus (f + g) f = g.support := by rw [neLocus_comm, neLocus_self_add_right] @[simp] theorem neLocus_self_sub_right : neLocus f (f - g) = g.support := by rw [sub_eq_add_neg, neLocus_self_add_right, support_neg] @[simp] theorem neLocus_self_sub_left : neLocus (f - g) f = g.support := by rw [neLocus_comm, neLocus_self_sub_right] end AddGroup end DFinsupp
Mathlib/Data/DFinsupp/NeLocus.lean
160
161
/- 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
Mathlib/Algebra/BigOperators/Intervals.lean
79
85
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Sébastien Gouëzel, Frédéric Dupuis -/ import Mathlib.Algebra.BigOperators.Field import Mathlib.Analysis.Complex.Basic import Mathlib.Analysis.InnerProductSpace.Defs import Mathlib.GroupTheory.MonoidLocalization.Basic /-! # Properties of inner product spaces This file proves many basic properties of inner product spaces (real or complex). ## Main results - `inner_mul_inner_self_le`: the Cauchy-Schwartz inequality (one of many variants). - `norm_inner_eq_norm_iff`: the equality criteion in the Cauchy-Schwartz inequality (also in many variants). - `inner_eq_sum_norm_sq_div_four`: the polarization identity. ## Tags inner product space, Hilbert space, norm -/ noncomputable section open RCLike Real Filter Topology ComplexConjugate Finsupp open LinearMap (BilinForm) variable {𝕜 E F : Type*} [RCLike 𝕜] section BasicProperties_Seminormed open scoped InnerProductSpace variable [SeminormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [SeminormedAddCommGroup F] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y local postfix:90 "†" => starRingEnd _ export InnerProductSpace (norm_sq_eq_re_inner) @[simp] theorem inner_conj_symm (x y : E) : ⟪y, x⟫† = ⟪x, y⟫ := InnerProductSpace.conj_inner_symm _ _ theorem real_inner_comm (x y : F) : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := @inner_conj_symm ℝ _ _ _ _ x y theorem inner_eq_zero_symm {x y : E} : ⟪x, y⟫ = 0 ↔ ⟪y, x⟫ = 0 := by rw [← inner_conj_symm] exact star_eq_zero @[simp] theorem inner_self_im (x : E) : im ⟪x, x⟫ = 0 := by rw [← @ofReal_inj 𝕜, im_eq_conj_sub]; simp theorem inner_add_left (x y z : E) : ⟪x + y, z⟫ = ⟪x, z⟫ + ⟪y, z⟫ := InnerProductSpace.add_left _ _ _ theorem inner_add_right (x y z : E) : ⟪x, y + z⟫ = ⟪x, y⟫ + ⟪x, z⟫ := by rw [← inner_conj_symm, inner_add_left, RingHom.map_add] simp only [inner_conj_symm] theorem inner_re_symm (x y : E) : re ⟪x, y⟫ = re ⟪y, x⟫ := by rw [← inner_conj_symm, conj_re] theorem inner_im_symm (x y : E) : im ⟪x, y⟫ = -im ⟪y, x⟫ := by rw [← inner_conj_symm, conj_im] section Algebra variable {𝕝 : Type*} [CommSemiring 𝕝] [StarRing 𝕝] [Algebra 𝕝 𝕜] [Module 𝕝 E] [IsScalarTower 𝕝 𝕜 E] [StarModule 𝕝 𝕜] /-- See `inner_smul_left` for the common special when `𝕜 = 𝕝`. -/ lemma inner_smul_left_eq_star_smul (x y : E) (r : 𝕝) : ⟪r • x, y⟫ = r† • ⟪x, y⟫ := by rw [← algebraMap_smul 𝕜 r, InnerProductSpace.smul_left, starRingEnd_apply, starRingEnd_apply, ← algebraMap_star_comm, ← smul_eq_mul, algebraMap_smul] /-- Special case of `inner_smul_left_eq_star_smul` when the acting ring has a trivial star (eg `ℕ`, `ℤ`, `ℚ≥0`, `ℚ`, `ℝ`). -/ lemma inner_smul_left_eq_smul [TrivialStar 𝕝] (x y : E) (r : 𝕝) : ⟪r • x, y⟫ = r • ⟪x, y⟫ := by rw [inner_smul_left_eq_star_smul, starRingEnd_apply, star_trivial] /-- See `inner_smul_right` for the common special when `𝕜 = 𝕝`. -/ lemma inner_smul_right_eq_smul (x y : E) (r : 𝕝) : ⟪x, r • y⟫ = r • ⟪x, y⟫ := by rw [← inner_conj_symm, inner_smul_left_eq_star_smul, starRingEnd_apply, starRingEnd_apply, star_smul, star_star, ← starRingEnd_apply, inner_conj_symm] end Algebra /-- See `inner_smul_left_eq_star_smul` for the case of a general algebra action. -/ theorem inner_smul_left (x y : E) (r : 𝕜) : ⟪r • x, y⟫ = r† * ⟪x, y⟫ := inner_smul_left_eq_star_smul .. theorem real_inner_smul_left (x y : F) (r : ℝ) : ⟪r • x, y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_left _ _ _ theorem inner_smul_real_left (x y : E) (r : ℝ) : ⟪(r : 𝕜) • x, y⟫ = r • ⟪x, y⟫ := by rw [inner_smul_left, conj_ofReal, Algebra.smul_def] /-- See `inner_smul_right_eq_smul` for the case of a general algebra action. -/ theorem inner_smul_right (x y : E) (r : 𝕜) : ⟪x, r • y⟫ = r * ⟪x, y⟫ := inner_smul_right_eq_smul .. theorem real_inner_smul_right (x y : F) (r : ℝ) : ⟪x, r • y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_right _ _ _ theorem inner_smul_real_right (x y : E) (r : ℝ) : ⟪x, (r : 𝕜) • y⟫ = r • ⟪x, y⟫ := by rw [inner_smul_right, Algebra.smul_def] /-- The inner product as a sesquilinear form. Note that in the case `𝕜 = ℝ` this is a bilinear form. -/ @[simps!] def sesqFormOfInner : E →ₗ[𝕜] E →ₗ⋆[𝕜] 𝕜 := LinearMap.mk₂'ₛₗ (RingHom.id 𝕜) (starRingEnd _) (fun x y => ⟪y, x⟫) (fun _x _y _z => inner_add_right _ _ _) (fun _r _x _y => inner_smul_right _ _ _) (fun _x _y _z => inner_add_left _ _ _) fun _r _x _y => inner_smul_left _ _ _ /-- The real inner product as a bilinear form. Note that unlike `sesqFormOfInner`, this does not reverse the order of the arguments. -/ @[simps!] def bilinFormOfRealInner : BilinForm ℝ F := sesqFormOfInner.flip /-- An inner product with a sum on the left. -/ theorem sum_inner {ι : Type*} (s : Finset ι) (f : ι → E) (x : E) : ⟪∑ i ∈ s, f i, x⟫ = ∑ i ∈ s, ⟪f i, x⟫ := map_sum (sesqFormOfInner (𝕜 := 𝕜) (E := E) x) _ _ /-- An inner product with a sum on the right. -/ theorem inner_sum {ι : Type*} (s : Finset ι) (f : ι → E) (x : E) : ⟪x, ∑ i ∈ s, f i⟫ = ∑ i ∈ s, ⟪x, f i⟫ := map_sum (LinearMap.flip sesqFormOfInner x) _ _ /-- An inner product with a sum on the left, `Finsupp` version. -/ protected theorem Finsupp.sum_inner {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪l.sum fun (i : ι) (a : 𝕜) => a • v i, x⟫ = l.sum fun (i : ι) (a : 𝕜) => conj a • ⟪v i, x⟫ := by convert sum_inner (𝕜 := 𝕜) l.support (fun a => l a • v a) x simp only [inner_smul_left, Finsupp.sum, smul_eq_mul] /-- An inner product with a sum on the right, `Finsupp` version. -/ protected theorem Finsupp.inner_sum {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪x, l.sum fun (i : ι) (a : 𝕜) => a • v i⟫ = l.sum fun (i : ι) (a : 𝕜) => a • ⟪x, v i⟫ := by convert inner_sum (𝕜 := 𝕜) l.support (fun a => l a • v a) x simp only [inner_smul_right, Finsupp.sum, smul_eq_mul] protected theorem DFinsupp.sum_inner {ι : Type*} [DecidableEq ι] {α : ι → Type*} [∀ i, AddZeroClass (α i)] [∀ (i) (x : α i), Decidable (x ≠ 0)] (f : ∀ i, α i → E) (l : Π₀ i, α i) (x : E) : ⟪l.sum f, x⟫ = l.sum fun i a => ⟪f i a, x⟫ := by simp +contextual only [DFinsupp.sum, sum_inner, smul_eq_mul] protected theorem DFinsupp.inner_sum {ι : Type*} [DecidableEq ι] {α : ι → Type*} [∀ i, AddZeroClass (α i)] [∀ (i) (x : α i), Decidable (x ≠ 0)] (f : ∀ i, α i → E) (l : Π₀ i, α i) (x : E) : ⟪x, l.sum f⟫ = l.sum fun i a => ⟪x, f i a⟫ := by simp +contextual only [DFinsupp.sum, inner_sum, smul_eq_mul] @[simp] theorem inner_zero_left (x : E) : ⟪0, x⟫ = 0 := by rw [← zero_smul 𝕜 (0 : E), inner_smul_left, RingHom.map_zero, zero_mul] theorem inner_re_zero_left (x : E) : re ⟪0, x⟫ = 0 := by simp only [inner_zero_left, AddMonoidHom.map_zero] @[simp] theorem inner_zero_right (x : E) : ⟪x, 0⟫ = 0 := by rw [← inner_conj_symm, inner_zero_left, RingHom.map_zero] theorem inner_re_zero_right (x : E) : re ⟪x, 0⟫ = 0 := by simp only [inner_zero_right, AddMonoidHom.map_zero] theorem inner_self_nonneg {x : E} : 0 ≤ re ⟪x, x⟫ := PreInnerProductSpace.toCore.re_inner_nonneg x theorem real_inner_self_nonneg {x : F} : 0 ≤ ⟪x, x⟫_ℝ := @inner_self_nonneg ℝ F _ _ _ x @[simp] theorem inner_self_ofReal_re (x : E) : (re ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := ((RCLike.is_real_TFAE (⟪x, x⟫ : 𝕜)).out 2 3).2 (inner_self_im (𝕜 := 𝕜) x) theorem inner_self_eq_norm_sq_to_K (x : E) : ⟪x, x⟫ = (‖x‖ : 𝕜) ^ 2 := by rw [← inner_self_ofReal_re, ← norm_sq_eq_re_inner, ofReal_pow] theorem inner_self_re_eq_norm (x : E) : re ⟪x, x⟫ = ‖⟪x, x⟫‖ := by conv_rhs => rw [← inner_self_ofReal_re] symm exact norm_of_nonneg inner_self_nonneg theorem inner_self_ofReal_norm (x : E) : (‖⟪x, x⟫‖ : 𝕜) = ⟪x, x⟫ := by rw [← inner_self_re_eq_norm] exact inner_self_ofReal_re _ theorem real_inner_self_abs (x : F) : |⟪x, x⟫_ℝ| = ⟪x, x⟫_ℝ := @inner_self_ofReal_norm ℝ F _ _ _ x theorem norm_inner_symm (x y : E) : ‖⟪x, y⟫‖ = ‖⟪y, x⟫‖ := by rw [← inner_conj_symm, norm_conj] @[simp] theorem inner_neg_left (x y : E) : ⟪-x, y⟫ = -⟪x, y⟫ := by rw [← neg_one_smul 𝕜 x, inner_smul_left] simp @[simp] theorem inner_neg_right (x y : E) : ⟪x, -y⟫ = -⟪x, y⟫ := by rw [← inner_conj_symm, inner_neg_left]; simp only [RingHom.map_neg, inner_conj_symm] theorem inner_neg_neg (x y : E) : ⟪-x, -y⟫ = ⟪x, y⟫ := by simp theorem inner_self_conj (x : E) : ⟪x, x⟫† = ⟪x, x⟫ := inner_conj_symm _ _ theorem inner_sub_left (x y z : E) : ⟪x - y, z⟫ = ⟪x, z⟫ - ⟪y, z⟫ := by simp [sub_eq_add_neg, inner_add_left] theorem inner_sub_right (x y z : E) : ⟪x, y - z⟫ = ⟪x, y⟫ - ⟪x, z⟫ := by simp [sub_eq_add_neg, inner_add_right] theorem inner_mul_symm_re_eq_norm (x y : E) : re (⟪x, y⟫ * ⟪y, x⟫) = ‖⟪x, y⟫ * ⟪y, x⟫‖ := by rw [← inner_conj_symm, mul_comm] exact re_eq_norm_of_mul_conj (inner y x) /-- Expand `⟪x + y, x + y⟫` -/ theorem inner_add_add_self (x y : E) : ⟪x + y, x + y⟫ = ⟪x, x⟫ + ⟪x, y⟫ + ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_add_left, inner_add_right]; ring /-- Expand `⟪x + y, x + y⟫_ℝ` -/ theorem real_inner_add_add_self (x y : F) : ⟪x + y, x + y⟫_ℝ = ⟪x, x⟫_ℝ + 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := by have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [← inner_conj_symm]; rfl simp only [inner_add_add_self, this, add_left_inj] ring -- Expand `⟪x - y, x - y⟫` theorem inner_sub_sub_self (x y : E) : ⟪x - y, x - y⟫ = ⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_sub_left, inner_sub_right]; ring /-- Expand `⟪x - y, x - y⟫_ℝ` -/ theorem real_inner_sub_sub_self (x y : F) : ⟪x - y, x - y⟫_ℝ = ⟪x, x⟫_ℝ - 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := by have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [← inner_conj_symm]; rfl simp only [inner_sub_sub_self, this, add_left_inj] ring /-- Parallelogram law -/ theorem parallelogram_law {x y : E} : ⟪x + y, x + y⟫ + ⟪x - y, x - y⟫ = 2 * (⟪x, x⟫ + ⟪y, y⟫) := by simp only [inner_add_add_self, inner_sub_sub_self] ring /-- **Cauchy–Schwarz inequality**. -/ theorem inner_mul_inner_self_le (x y : E) : ‖⟪x, y⟫‖ * ‖⟪y, x⟫‖ ≤ re ⟪x, x⟫ * re ⟪y, y⟫ := letI cd : PreInnerProductSpace.Core 𝕜 E := PreInnerProductSpace.toCore InnerProductSpace.Core.inner_mul_inner_self_le x y /-- Cauchy–Schwarz inequality for real inner products. -/ theorem real_inner_mul_inner_self_le (x y : F) : ⟪x, y⟫_ℝ * ⟪x, y⟫_ℝ ≤ ⟪x, x⟫_ℝ * ⟪y, y⟫_ℝ := calc ⟪x, y⟫_ℝ * ⟪x, y⟫_ℝ ≤ ‖⟪x, y⟫_ℝ‖ * ‖⟪y, x⟫_ℝ‖ := by rw [real_inner_comm y, ← norm_mul] exact le_abs_self _ _ ≤ ⟪x, x⟫_ℝ * ⟪y, y⟫_ℝ := @inner_mul_inner_self_le ℝ _ _ _ _ x y end BasicProperties_Seminormed section BasicProperties variable [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [NormedAddCommGroup F] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y export InnerProductSpace (norm_sq_eq_re_inner) @[simp] theorem inner_self_eq_zero {x : E} : ⟪x, x⟫ = 0 ↔ x = 0 := by rw [inner_self_eq_norm_sq_to_K, sq_eq_zero_iff, ofReal_eq_zero, norm_eq_zero] theorem inner_self_ne_zero {x : E} : ⟪x, x⟫ ≠ 0 ↔ x ≠ 0 := inner_self_eq_zero.not variable (𝕜) theorem ext_inner_left {x y : E} (h : ∀ v, ⟪v, x⟫ = ⟪v, y⟫) : x = y := by rw [← sub_eq_zero, ← @inner_self_eq_zero 𝕜, inner_sub_right, sub_eq_zero, h (x - y)] theorem ext_inner_right {x y : E} (h : ∀ v, ⟪x, v⟫ = ⟪y, v⟫) : x = y := by rw [← sub_eq_zero, ← @inner_self_eq_zero 𝕜, inner_sub_left, sub_eq_zero, h (x - y)] variable {𝕜} @[simp] theorem re_inner_self_nonpos {x : E} : re ⟪x, x⟫ ≤ 0 ↔ x = 0 := by rw [← norm_sq_eq_re_inner, (sq_nonneg _).le_iff_eq, sq_eq_zero_iff, norm_eq_zero] @[simp] lemma re_inner_self_pos {x : E} : 0 < re ⟪x, x⟫ ↔ x ≠ 0 := by simpa [-re_inner_self_nonpos] using re_inner_self_nonpos (𝕜 := 𝕜) (x := x).not @[deprecated (since := "2025-04-22")] alias inner_self_nonpos := re_inner_self_nonpos @[deprecated (since := "2025-04-22")] alias inner_self_pos := re_inner_self_pos open scoped InnerProductSpace in theorem real_inner_self_nonpos {x : F} : ⟪x, x⟫_ℝ ≤ 0 ↔ x = 0 := re_inner_self_nonpos (𝕜 := ℝ) open scoped InnerProductSpace in theorem real_inner_self_pos {x : F} : 0 < ⟪x, x⟫_ℝ ↔ x ≠ 0 := re_inner_self_pos (𝕜 := ℝ) /-- A family of vectors is linearly independent if they are nonzero and orthogonal. -/ theorem linearIndependent_of_ne_zero_of_inner_eq_zero {ι : Type*} {v : ι → E} (hz : ∀ i, v i ≠ 0) (ho : Pairwise fun i j => ⟪v i, v j⟫ = 0) : LinearIndependent 𝕜 v := by rw [linearIndependent_iff'] intro s g hg i hi have h' : g i * inner (v i) (v i) = inner (v i) (∑ j ∈ s, g j • v j) := by rw [inner_sum] symm convert Finset.sum_eq_single (M := 𝕜) i ?_ ?_ · rw [inner_smul_right] · intro j _hj hji rw [inner_smul_right, ho hji.symm, mul_zero] · exact fun h => False.elim (h hi) simpa [hg, hz] using h' end BasicProperties section Norm_Seminormed open scoped InnerProductSpace variable [SeminormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [SeminormedAddCommGroup F] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y local notation "IK" => @RCLike.I 𝕜 _ theorem norm_eq_sqrt_re_inner (x : E) : ‖x‖ = √(re ⟪x, x⟫) := calc ‖x‖ = √(‖x‖ ^ 2) := (sqrt_sq (norm_nonneg _)).symm _ = √(re ⟪x, x⟫) := congr_arg _ (norm_sq_eq_re_inner _) @[deprecated (since := "2025-04-22")] alias norm_eq_sqrt_inner := norm_eq_sqrt_re_inner theorem norm_eq_sqrt_real_inner (x : F) : ‖x‖ = √⟪x, x⟫_ℝ := @norm_eq_sqrt_re_inner ℝ _ _ _ _ x theorem inner_self_eq_norm_mul_norm (x : E) : re ⟪x, x⟫ = ‖x‖ * ‖x‖ := by rw [@norm_eq_sqrt_re_inner 𝕜, ← sqrt_mul inner_self_nonneg (re ⟪x, x⟫), sqrt_mul_self inner_self_nonneg] theorem inner_self_eq_norm_sq (x : E) : re ⟪x, x⟫ = ‖x‖ ^ 2 := by rw [pow_two, inner_self_eq_norm_mul_norm] theorem real_inner_self_eq_norm_mul_norm (x : F) : ⟪x, x⟫_ℝ = ‖x‖ * ‖x‖ := by have h := @inner_self_eq_norm_mul_norm ℝ F _ _ _ x simpa using h theorem real_inner_self_eq_norm_sq (x : F) : ⟪x, x⟫_ℝ = ‖x‖ ^ 2 := by rw [pow_two, real_inner_self_eq_norm_mul_norm] /-- Expand the square -/ theorem norm_add_sq (x y : E) : ‖x + y‖ ^ 2 = ‖x‖ ^ 2 + 2 * re ⟪x, y⟫ + ‖y‖ ^ 2 := by repeat' rw [sq (M := ℝ), ← @inner_self_eq_norm_mul_norm 𝕜] rw [inner_add_add_self, two_mul] simp only [add_assoc, add_left_inj, add_right_inj, AddMonoidHom.map_add] rw [← inner_conj_symm, conj_re] alias norm_add_pow_two := norm_add_sq /-- Expand the square -/ theorem norm_add_sq_real (x y : F) : ‖x + y‖ ^ 2 = ‖x‖ ^ 2 + 2 * ⟪x, y⟫_ℝ + ‖y‖ ^ 2 := by have h := @norm_add_sq ℝ _ _ _ _ x y simpa using h alias norm_add_pow_two_real := norm_add_sq_real /-- Expand the square -/ theorem norm_add_mul_self (x y : E) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + 2 * re ⟪x, y⟫ + ‖y‖ * ‖y‖ := by repeat' rw [← sq (M := ℝ)] exact norm_add_sq _ _ /-- Expand the square -/ theorem norm_add_mul_self_real (x y : F) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + 2 * ⟪x, y⟫_ℝ + ‖y‖ * ‖y‖ := by have h := @norm_add_mul_self ℝ _ _ _ _ x y simpa using h /-- Expand the square -/ theorem norm_sub_sq (x y : E) : ‖x - y‖ ^ 2 = ‖x‖ ^ 2 - 2 * re ⟪x, y⟫ + ‖y‖ ^ 2 := by rw [sub_eq_add_neg, @norm_add_sq 𝕜 _ _ _ _ x (-y), norm_neg, inner_neg_right, map_neg, mul_neg, sub_eq_add_neg] alias norm_sub_pow_two := norm_sub_sq /-- Expand the square -/ theorem norm_sub_sq_real (x y : F) : ‖x - y‖ ^ 2 = ‖x‖ ^ 2 - 2 * ⟪x, y⟫_ℝ + ‖y‖ ^ 2 := @norm_sub_sq ℝ _ _ _ _ _ _ alias norm_sub_pow_two_real := norm_sub_sq_real /-- Expand the square -/ theorem norm_sub_mul_self (x y : E) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ - 2 * re ⟪x, y⟫ + ‖y‖ * ‖y‖ := by repeat' rw [← sq (M := ℝ)] exact norm_sub_sq _ _ /-- Expand the square -/ theorem norm_sub_mul_self_real (x y : F) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ - 2 * ⟪x, y⟫_ℝ + ‖y‖ * ‖y‖ := by have h := @norm_sub_mul_self ℝ _ _ _ _ x y simpa using h /-- Cauchy–Schwarz inequality with norm -/ theorem norm_inner_le_norm (x y : E) : ‖⟪x, y⟫‖ ≤ ‖x‖ * ‖y‖ := by rw [norm_eq_sqrt_re_inner (𝕜 := 𝕜) x, norm_eq_sqrt_re_inner (𝕜 := 𝕜) y] letI : PreInnerProductSpace.Core 𝕜 E := PreInnerProductSpace.toCore exact InnerProductSpace.Core.norm_inner_le_norm x y theorem nnnorm_inner_le_nnnorm (x y : E) : ‖⟪x, y⟫‖₊ ≤ ‖x‖₊ * ‖y‖₊ := norm_inner_le_norm x y theorem re_inner_le_norm (x y : E) : re ⟪x, y⟫ ≤ ‖x‖ * ‖y‖ := le_trans (re_le_norm (inner x y)) (norm_inner_le_norm x y) /-- Cauchy–Schwarz inequality with norm -/ theorem abs_real_inner_le_norm (x y : F) : |⟪x, y⟫_ℝ| ≤ ‖x‖ * ‖y‖ := (Real.norm_eq_abs _).ge.trans (norm_inner_le_norm x y) /-- Cauchy–Schwarz inequality with norm -/ theorem real_inner_le_norm (x y : F) : ⟪x, y⟫_ℝ ≤ ‖x‖ * ‖y‖ := le_trans (le_abs_self _) (abs_real_inner_le_norm _ _) lemma inner_eq_zero_of_left {x : E} (y : E) (h : ‖x‖ = 0) : ⟪x, y⟫_𝕜 = 0 := by rw [← norm_eq_zero] refine le_antisymm ?_ (by positivity) exact norm_inner_le_norm _ _ |>.trans <| by simp [h] lemma inner_eq_zero_of_right (x : E) {y : E} (h : ‖y‖ = 0) : ⟪x, y⟫_𝕜 = 0 := by rw [inner_eq_zero_symm, inner_eq_zero_of_left _ h] variable (𝕜) include 𝕜 in theorem parallelogram_law_with_norm (x y : E) : ‖x + y‖ * ‖x + y‖ + ‖x - y‖ * ‖x - y‖ = 2 * (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) := by simp only [← @inner_self_eq_norm_mul_norm 𝕜] rw [← re.map_add, parallelogram_law, two_mul, two_mul] simp only [re.map_add] include 𝕜 in theorem parallelogram_law_with_nnnorm (x y : E) : ‖x + y‖₊ * ‖x + y‖₊ + ‖x - y‖₊ * ‖x - y‖₊ = 2 * (‖x‖₊ * ‖x‖₊ + ‖y‖₊ * ‖y‖₊) := Subtype.ext <| parallelogram_law_with_norm 𝕜 x y variable {𝕜} /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ theorem re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (‖x + y‖ * ‖x + y‖ - ‖x‖ * ‖x‖ - ‖y‖ * ‖y‖) / 2 := by rw [@norm_add_mul_self 𝕜] ring /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ theorem re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ - ‖x - y‖ * ‖x - y‖) / 2 := by rw [@norm_sub_mul_self 𝕜] ring /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ theorem re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four (x y : E) : re ⟪x, y⟫ = (‖x + y‖ * ‖x + y‖ - ‖x - y‖ * ‖x - y‖) / 4 := by rw [@norm_add_mul_self 𝕜, @norm_sub_mul_self 𝕜] ring /-- Polarization identity: The imaginary part of the inner product, in terms of the norm. -/ theorem im_inner_eq_norm_sub_i_smul_mul_self_sub_norm_add_i_smul_mul_self_div_four (x y : E) : im ⟪x, y⟫ = (‖x - IK • y‖ * ‖x - IK • y‖ - ‖x + IK • y‖ * ‖x + IK • y‖) / 4 := by simp only [@norm_add_mul_self 𝕜, @norm_sub_mul_self 𝕜, inner_smul_right, I_mul_re] ring /-- Polarization identity: The inner product, in terms of the norm. -/ theorem inner_eq_sum_norm_sq_div_four (x y : E) : ⟪x, y⟫ = ((‖x + y‖ : 𝕜) ^ 2 - (‖x - y‖ : 𝕜) ^ 2 + ((‖x - IK • y‖ : 𝕜) ^ 2 - (‖x + IK • y‖ : 𝕜) ^ 2) * IK) / 4 := by rw [← re_add_im ⟪x, y⟫, re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four, im_inner_eq_norm_sub_i_smul_mul_self_sub_norm_add_i_smul_mul_self_div_four] push_cast simp only [sq, ← mul_div_right_comm, ← add_div] /-- Polarization identity: The real inner product, in terms of the norm. -/ theorem real_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (‖x + y‖ * ‖x + y‖ - ‖x‖ * ‖x‖ - ‖y‖ * ‖y‖) / 2 := re_to_real.symm.trans <| re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two x y /-- Polarization identity: The real inner product, in terms of the norm. -/ theorem real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ - ‖x - y‖ * ‖x - y‖) / 2 := re_to_real.symm.trans <| re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two x y /-- Pythagorean theorem, if-and-only-if vector inner product form. -/ theorem norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ ↔ ⟪x, y⟫_ℝ = 0 := by rw [@norm_add_mul_self ℝ, add_right_cancel_iff, add_eq_left, mul_eq_zero] norm_num /-- Pythagorean theorem, if-and-if vector inner product form using square roots. -/ theorem norm_add_eq_sqrt_iff_real_inner_eq_zero {x y : F} : ‖x + y‖ = √(‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) ↔ ⟪x, y⟫_ℝ = 0 := by rw [← norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero, eq_comm, sqrt_eq_iff_mul_self_eq, eq_comm] <;> positivity /-- Pythagorean theorem, vector inner product form. -/ theorem norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (x y : E) (h : ⟪x, y⟫ = 0) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := by rw [@norm_add_mul_self 𝕜, add_right_cancel_iff, add_eq_left, mul_eq_zero] apply Or.inr simp only [h, zero_re'] /-- Pythagorean theorem, vector inner product form. -/ theorem norm_add_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := (norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector inner product form. -/ theorem norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ ↔ ⟪x, y⟫_ℝ = 0 := by rw [@norm_sub_mul_self ℝ, add_right_cancel_iff, sub_eq_add_neg, add_eq_left, neg_eq_zero, mul_eq_zero] norm_num /-- Pythagorean theorem, subtracting vectors, if-and-if vector inner product form using square roots. -/ theorem norm_sub_eq_sqrt_iff_real_inner_eq_zero {x y : F} : ‖x - y‖ = √(‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) ↔ ⟪x, y⟫_ℝ = 0 := by rw [← norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero, eq_comm, sqrt_eq_iff_mul_self_eq, eq_comm] <;> positivity /-- Pythagorean theorem, subtracting vectors, vector inner product form. -/ theorem norm_sub_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := (norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- The sum and difference of two vectors are orthogonal if and only if they have the same norm. -/ theorem real_inner_add_sub_eq_zero_iff (x y : F) : ⟪x + y, x - y⟫_ℝ = 0 ↔ ‖x‖ = ‖y‖ := by conv_rhs => rw [← mul_self_inj_of_nonneg (norm_nonneg _) (norm_nonneg _)] simp only [← @inner_self_eq_norm_mul_norm ℝ, inner_add_left, inner_sub_right, real_inner_comm y x, sub_eq_zero, re_to_real] constructor · intro h rw [add_comm] at h linarith · intro h linarith /-- Given two orthogonal vectors, their sum and difference have equal norms. -/ theorem norm_sub_eq_norm_add {v w : E} (h : ⟪v, w⟫ = 0) : ‖w - v‖ = ‖w + v‖ := by rw [← mul_self_inj_of_nonneg (norm_nonneg _) (norm_nonneg _)] simp only [h, ← @inner_self_eq_norm_mul_norm 𝕜, sub_neg_eq_add, sub_zero, map_sub, zero_re', zero_sub, add_zero, map_add, inner_add_right, inner_sub_left, inner_sub_right, inner_re_symm, zero_add] /-- The real inner product of two vectors, divided by the product of their norms, has absolute value at most 1. -/ theorem abs_real_inner_div_norm_mul_norm_le_one (x y : F) : |⟪x, y⟫_ℝ / (‖x‖ * ‖y‖)| ≤ 1 := by rw [abs_div, abs_mul, abs_norm, abs_norm] exact div_le_one_of_le₀ (abs_real_inner_le_norm x y) (by positivity) /-- The inner product of a vector with a multiple of itself. -/ theorem real_inner_smul_self_left (x : F) (r : ℝ) : ⟪r • x, x⟫_ℝ = r * (‖x‖ * ‖x‖) := by rw [real_inner_smul_left, ← real_inner_self_eq_norm_mul_norm] /-- The inner product of a vector with a multiple of itself. -/ theorem real_inner_smul_self_right (x : F) (r : ℝ) : ⟪x, r • x⟫_ℝ = r * (‖x‖ * ‖x‖) := by rw [inner_smul_right, ← real_inner_self_eq_norm_mul_norm] /-- The inner product of two weighted sums, where the weights in each sum add to 0, in terms of the norms of pairwise differences. -/ theorem inner_sum_smul_sum_smul_of_sum_eq_zero {ι₁ : Type*} {s₁ : Finset ι₁} {w₁ : ι₁ → ℝ} (v₁ : ι₁ → F) (h₁ : ∑ i ∈ s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : Finset ι₂} {w₂ : ι₂ → ℝ} (v₂ : ι₂ → F) (h₂ : ∑ i ∈ s₂, w₂ i = 0) : ⟪∑ i₁ ∈ s₁, w₁ i₁ • v₁ i₁, ∑ i₂ ∈ s₂, w₂ i₂ • v₂ i₂⟫_ℝ = (-∑ i₁ ∈ s₁, ∑ i₂ ∈ s₂, w₁ i₁ * w₂ i₂ * (‖v₁ i₁ - v₂ i₂‖ * ‖v₁ i₁ - v₂ i₂‖)) / 2 := by simp_rw [sum_inner, inner_sum, real_inner_smul_left, real_inner_smul_right, real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two, ← div_sub_div_same, ← div_add_div_same, mul_sub_left_distrib, left_distrib, Finset.sum_sub_distrib, Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.sum_mul, h₁, h₂, zero_mul, mul_zero, Finset.sum_const_zero, zero_add, zero_sub, Finset.mul_sum, neg_div, Finset.sum_div, mul_div_assoc, mul_assoc] end Norm_Seminormed section Norm open scoped InnerProductSpace variable [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [NormedAddCommGroup F] [InnerProductSpace ℝ F] variable {ι : Type*} local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y /-- Formula for the distance between the images of two nonzero points under an inversion with center zero. See also `EuclideanGeometry.dist_inversion_inversion` for inversions around a general point. -/ theorem dist_div_norm_sq_smul {x y : F} (hx : x ≠ 0) (hy : y ≠ 0) (R : ℝ) : dist ((R / ‖x‖) ^ 2 • x) ((R / ‖y‖) ^ 2 • y) = R ^ 2 / (‖x‖ * ‖y‖) * dist x y := calc dist ((R / ‖x‖) ^ 2 • x) ((R / ‖y‖) ^ 2 • y) = √(‖(R / ‖x‖) ^ 2 • x - (R / ‖y‖) ^ 2 • y‖ ^ 2) := by rw [dist_eq_norm, sqrt_sq (norm_nonneg _)] _ = √((R ^ 2 / (‖x‖ * ‖y‖)) ^ 2 * ‖x - y‖ ^ 2) := congr_arg sqrt <| by field_simp [sq, norm_sub_mul_self_real, norm_smul, real_inner_smul_left, inner_smul_right, Real.norm_of_nonneg (mul_self_nonneg _)] ring _ = R ^ 2 / (‖x‖ * ‖y‖) * dist x y := by rw [sqrt_mul, sqrt_sq, sqrt_sq, dist_eq_norm] <;> positivity /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ theorem norm_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : E} {r : 𝕜} (hx : x ≠ 0) (hr : r ≠ 0) : ‖⟪x, r • x⟫‖ / (‖x‖ * ‖r • x‖) = 1 := by have hx' : ‖x‖ ≠ 0 := by simp [hx] have hr' : ‖r‖ ≠ 0 := by simp [hr] rw [inner_smul_right, norm_mul, ← inner_self_re_eq_norm, inner_self_eq_norm_mul_norm, norm_smul] rw [← mul_assoc, ← div_div, mul_div_cancel_right₀ _ hx', ← div_div, mul_comm, mul_div_cancel_right₀ _ hr', div_self hx'] /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ theorem abs_real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : r ≠ 0) : |⟪x, r • x⟫_ℝ| / (‖x‖ * ‖r • x‖) = 1 := norm_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr /-- The inner product of a nonzero vector with a positive multiple of itself, divided by the product of their norms, has value 1. -/ theorem real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : 0 < r) : ⟪x, r • x⟫_ℝ / (‖x‖ * ‖r • x‖) = 1 := by rw [real_inner_smul_self_right, norm_smul, Real.norm_eq_abs, ← mul_assoc ‖x‖, mul_comm _ |r|, mul_assoc, abs_of_nonneg hr.le, div_self] exact mul_ne_zero hr.ne' (mul_self_ne_zero.2 (norm_ne_zero_iff.2 hx))
/-- The inner product of a nonzero vector with a negative multiple of itself, divided by the product of their norms, has value -1. -/ theorem real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : r < 0) : ⟪x, r • x⟫_ℝ / (‖x‖ * ‖r • x‖) = -1 := by rw [real_inner_smul_self_right, norm_smul, Real.norm_eq_abs, ← mul_assoc ‖x‖, mul_comm _ |r|,
Mathlib/Analysis/InnerProductSpace/Basic.lean
656
660
/- Copyright (c) 2021 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import Mathlib.GroupTheory.Transfer /-! # The Schur-Zassenhaus Theorem In this file we prove the Schur-Zassenhaus theorem. ## Main results - `Subgroup.exists_right_complement'_of_coprime`: The **Schur-Zassenhaus** theorem: If `H : Subgroup G` is normal and has order coprime to its index, then there exists a subgroup `K` which is a (right) complement of `H`. - `Subgroup.exists_left_complement'_of_coprime`: The **Schur-Zassenhaus** theorem: If `H : Subgroup G` is normal and has order coprime to its index, then there exists a subgroup `K` which is a (left) complement of `H`. -/ namespace Subgroup section SchurZassenhausAbelian open MulOpposite MulAction Subgroup.leftTransversals MemLeftTransversals variable {G : Type*} [Group G] (H : Subgroup G) [IsMulCommutative H] [FiniteIndex H] (α β : H.LeftTransversal) /-- The quotient of the transversals of an abelian normal `N` by the `diff` relation. -/ def QuotientDiff := Quotient (Setoid.mk (fun α β => diff (MonoidHom.id H) α β = 1) ⟨fun α => diff_self (MonoidHom.id H) α, fun h => by rw [← diff_inv, h, inv_one], fun h h' => by rw [← diff_mul_diff, h, h', one_mul]⟩) instance : Inhabited H.QuotientDiff := inferInstanceAs (Inhabited <| Quotient _) theorem smul_diff_smul' [hH : Normal H] (g : Gᵐᵒᵖ) : diff (MonoidHom.id H) (g • α) (g • β) = ⟨g.unop⁻¹ * (diff (MonoidHom.id H) α β : H) * g.unop, hH.mem_comm ((congr_arg (· ∈ H) (mul_inv_cancel_left _ _)).mpr (SetLike.coe_mem _))⟩ := by letI := H.fintypeQuotientOfFiniteIndex let ϕ : H →* H := { toFun := fun h => ⟨g.unop⁻¹ * h * g.unop, hH.mem_comm ((congr_arg (· ∈ H) (mul_inv_cancel_left _ _)).mpr (SetLike.coe_mem _))⟩ map_one' := by rw [Subtype.ext_iff, coe_mk, coe_one, mul_one, inv_mul_cancel] map_mul' := fun h₁ h₂ => by simp only [Subtype.ext_iff, coe_mk, coe_mul, mul_assoc, mul_inv_cancel_left] } refine (Fintype.prod_equiv (MulAction.toPerm g).symm _ _ fun x ↦ ?_).trans (map_prod ϕ _ _).symm simp only [ϕ, smul_apply_eq_smul_apply_inv_smul, smul_eq_mul_unop, mul_inv_rev, mul_assoc, MonoidHom.id_apply, toPerm_symm_apply, MonoidHom.coe_mk, OneHom.coe_mk] variable {H} variable [Normal H] noncomputable instance : MulAction G H.QuotientDiff where smul g := Quotient.map' (fun α => op g⁻¹ • α) fun α β h => Subtype.ext (by rwa [smul_diff_smul', coe_mk, coe_one, mul_eq_one_iff_eq_inv, mul_eq_left, ← coe_one, ← Subtype.ext_iff]) mul_smul g₁ g₂ q := Quotient.inductionOn' q fun T => congr_arg Quotient.mk'' (by rw [mul_inv_rev]; exact mul_smul (op g₁⁻¹) (op g₂⁻¹) T) one_smul q := Quotient.inductionOn' q fun T => congr_arg Quotient.mk'' (by rw [inv_one]; apply one_smul Gᵐᵒᵖ T) theorem smul_diff' (h : H) : diff (MonoidHom.id H) α (op (h : G) • β) = diff (MonoidHom.id H) α β * h ^ H.index := by letI := H.fintypeQuotientOfFiniteIndex rw [diff, diff, index_eq_card, Nat.card_eq_fintype_card, ← Finset.card_univ, ← Finset.prod_const, ← Finset.prod_mul_distrib] refine Finset.prod_congr rfl fun q _ => ?_ simp_rw [Subtype.ext_iff, MonoidHom.id_apply, coe_mul, mul_assoc, mul_right_inj] rw [smul_apply_eq_smul_apply_inv_smul, smul_eq_mul_unop, MulOpposite.unop_op, mul_left_inj, ← Subtype.ext_iff, Equiv.apply_eq_iff_eq, inv_smul_eq_iff] exact left_eq_mul.mpr ((QuotientGroup.eq_one_iff _).mpr h.2) theorem eq_one_of_smul_eq_one (hH : Nat.Coprime (Nat.card H) H.index) (α : H.QuotientDiff) (h : H) : h • α = α → h = 1 := Quotient.inductionOn' α fun α hα => (powCoprime hH).injective <| calc h ^ H.index = diff (MonoidHom.id H) (op ((h⁻¹ : H) : G) • α) α := by rw [← diff_inv, smul_diff', diff_self, one_mul, inv_pow, inv_inv] _ = 1 ^ H.index := (Quotient.exact' hα).trans (one_pow H.index).symm theorem exists_smul_eq (hH : Nat.Coprime (Nat.card H) H.index) (α β : H.QuotientDiff) : ∃ h : H, h • α = β := Quotient.inductionOn' α (Quotient.inductionOn' β fun β α => Exists.imp (fun _ => Quotient.sound') ⟨(powCoprime hH).symm (diff (MonoidHom.id H) β α), (diff_inv _ _ _).symm.trans (inv_eq_one.mpr ((smul_diff' β α ((powCoprime hH).symm (diff (MonoidHom.id H) β α))⁻¹).trans (by rw [inv_pow, ← powCoprime_apply hH, Equiv.apply_symm_apply, mul_inv_cancel])))⟩) theorem isComplement'_stabilizer_of_coprime {α : H.QuotientDiff} (hH : Nat.Coprime (Nat.card H) H.index) : IsComplement' H (stabilizer G α) := isComplement'_stabilizer α (eq_one_of_smul_eq_one hH α) fun g => exists_smul_eq hH (g • α) α /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem exists_right_complement'_of_coprime_aux (hH : Nat.Coprime (Nat.card H) H.index) : ∃ K : Subgroup G, IsComplement' H K := have ne : Nonempty (QuotientDiff H) := inferInstance ne.elim fun α => ⟨stabilizer G α, isComplement'_stabilizer_of_coprime hH⟩ end SchurZassenhausAbelian universe u namespace SchurZassenhausInduction /-! ## Proof of the Schur-Zassenhaus theorem In this section, we prove the Schur-Zassenhaus theorem. The proof is by contradiction. We assume that `G` is a minimal counterexample to the theorem. -/ variable {G : Type u} [Group G] {N : Subgroup G} [Normal N] (h1 : Nat.Coprime (Nat.card N) N.index) (h2 : ∀ (G' : Type u) [Group G'] [Finite G'], Nat.card G' < Nat.card G → ∀ {N' : Subgroup G'} [N'.Normal], Nat.Coprime (Nat.card N') N'.index → ∃ H' : Subgroup G', IsComplement' N' H') (h3 : ∀ H : Subgroup G, ¬IsComplement' N H) include h1 h3 /-! We will arrive at a contradiction via the following steps: * step 0: `N` (the normal Hall subgroup) is nontrivial. * step 1: If `K` is a subgroup of `G` with `K ⊔ N = ⊤`, then `K = ⊤`. * step 2: `N` is a minimal normal subgroup, phrased in terms of subgroups of `G`. * step 3: `N` is a minimal normal subgroup, phrased in terms of subgroups of `N`. * step 4: `p` (`min_fact (Fintype.card N)`) is prime (follows from step0). * step 5: `P` (a Sylow `p`-subgroup of `N`) is nontrivial. * step 6: `N` is a `p`-group (applies step 1 to the normalizer of `P` in `G`). * step 7: `N` is abelian (applies step 3 to the center of `N`). -/ /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem step0 : N ≠ ⊥ := by rintro rfl exact h3 ⊤ isComplement'_bot_top variable [Finite G] include h2 in /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem step1 (K : Subgroup G) (hK : K ⊔ N = ⊤) : K = ⊤ := by contrapose! h3 have h4 : (N.comap K.subtype).index = N.index := by rw [← N.relindex_top_right, ← hK] exact (relindex_sup_right K N).symm have h5 : Nat.card K < Nat.card G := by rw [← K.index_mul_card] exact lt_mul_of_one_lt_left Nat.card_pos (one_lt_index_of_ne_top h3) have h6 : Nat.Coprime (Nat.card (N.comap K.subtype)) (N.comap K.subtype).index := by rw [h4] exact h1.coprime_dvd_left (card_comap_dvd_of_injective N K.subtype Subtype.coe_injective) obtain ⟨H, hH⟩ := h2 K h5 h6 replace hH : Nat.card (H.map K.subtype) = N.index := by rw [← relindex_bot_left, ← relindex_comap, MonoidHom.comap_bot, Subgroup.ker_subtype, relindex_bot_left, ← IsComplement'.index_eq_card (IsComplement'.symm hH), index_comap, range_subtype, ← relindex_sup_right, hK, relindex_top_right] have h7 : Nat.card N * Nat.card (H.map K.subtype) = Nat.card G := by rw [hH, ← N.index_mul_card, mul_comm] have h8 : (Nat.card N).Coprime (Nat.card (H.map K.subtype)) := by rwa [hH] exact ⟨H.map K.subtype, isComplement'_of_coprime h7 h8⟩ include h2 in /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem step2 (K : Subgroup G) [K.Normal] (hK : K ≤ N) : K = ⊥ ∨ K = N := by have : Function.Surjective (QuotientGroup.mk' K) := Quotient.mk''_surjective have h4 := step1 h1 h2 h3 contrapose! h4 have h5 : Nat.card (G ⧸ K) < Nat.card G := by rw [← index_eq_card, ← K.index_mul_card] refine lt_mul_of_one_lt_right (Nat.pos_of_ne_zero index_ne_zero_of_finite) (K.one_lt_card_iff_ne_bot.mpr h4.1) have h6 : (Nat.card (N.map (QuotientGroup.mk' K))).Coprime (N.map (QuotientGroup.mk' K)).index := by have index_map := N.index_map_eq this (by rwa [QuotientGroup.ker_mk']) have index_pos : 0 < N.index := Nat.pos_of_ne_zero index_ne_zero_of_finite rw [index_map] refine h1.coprime_dvd_left ?_ rw [← Nat.mul_dvd_mul_iff_left index_pos, index_mul_card, ← index_map, index_mul_card] exact K.card_quotient_dvd_card obtain ⟨H, hH⟩ := h2 (G ⧸ K) h5 h6 refine ⟨H.comap (QuotientGroup.mk' K), ?_, ?_⟩ · have key : (N.map (QuotientGroup.mk' K)).comap (QuotientGroup.mk' K) = N := by refine comap_map_eq_self ?_ rwa [QuotientGroup.ker_mk'] rwa [← key, comap_sup_eq, hH.symm.sup_eq_top, comap_top] · rw [← comap_top (QuotientGroup.mk' K)] intro hH' rw [comap_injective this hH', isComplement'_top_right, map_eq_bot_iff, QuotientGroup.ker_mk'] at hH exact h4.2 (le_antisymm hK hH) include h2 in /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem step3 (K : Subgroup N) [(K.map N.subtype).Normal] : K = ⊥ ∨ K = ⊤ := by have key := step2 h1 h2 h3 (K.map N.subtype) (map_subtype_le K) rw [← map_bot N.subtype] at key conv at key => rhs rhs rw [← N.range_subtype, N.subtype.range_eq_map] have inj := map_injective N.subtype_injective rwa [inj.eq_iff, inj.eq_iff] at key /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem step4 : (Nat.card N).minFac.Prime := Nat.minFac_prime (N.one_lt_card_iff_ne_bot.mpr (step0 h1 h3)).ne' /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private theorem step5 {P : Sylow (Nat.card N).minFac N} : P.1 ≠ ⊥ := by haveI : Fact (Nat.card N).minFac.Prime := ⟨step4 h1 h3⟩ apply P.ne_bot_of_dvd_card exact (Nat.card N).minFac_dvd
include h2 in /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/
Mathlib/GroupTheory/SchurZassenhaus.lean
233
235
/- 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.Analysis.SpecialFunctions.Trigonometric.Basic import Mathlib.Topology.Order.ProjIcc /-! # Inverse trigonometric functions. See also `Analysis.SpecialFunctions.Trigonometric.Arctan` for the inverse tan function. (This is delayed as it is easier to set up after developing complex trigonometric functions.) Basic inequalities on trigonometric functions. -/ noncomputable section open Topology Filter Set Filter Real namespace Real variable {x y : ℝ} /-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x ≤ π / 2`. It defaults to `-π / 2` on `(-∞, -1)` and to `π / 2` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arcsin : ℝ → ℝ := Subtype.val ∘ IccExtend (neg_le_self zero_le_one) sinOrderIso.symm theorem arcsin_mem_Icc (x : ℝ) : arcsin x ∈ Icc (-(π / 2)) (π / 2) := Subtype.coe_prop _ @[simp] theorem range_arcsin : range arcsin = Icc (-(π / 2)) (π / 2) := by rw [arcsin, range_comp Subtype.val] simp [Icc] theorem arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := (arcsin_mem_Icc x).2 theorem neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := (arcsin_mem_Icc x).1 theorem arcsin_projIcc (x : ℝ) : arcsin (projIcc (-1) 1 (neg_le_self zero_le_one) x) = arcsin x := by rw [arcsin, Function.comp_apply, IccExtend_val, Function.comp_apply, IccExtend, Function.comp_apply] theorem sin_arcsin' {x : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) : sin (arcsin x) = x := by simpa [arcsin, IccExtend_of_mem _ _ hx, -OrderIso.apply_symm_apply] using Subtype.ext_iff.1 (sinOrderIso.apply_symm_apply ⟨x, hx⟩) theorem sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x := sin_arcsin' ⟨hx₁, hx₂⟩ theorem arcsin_sin' {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin (sin x) = x := injOn_sin (arcsin_mem_Icc _) hx <| by rw [sin_arcsin (neg_one_le_sin _) (sin_le_one _)] theorem arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x := arcsin_sin' ⟨hx₁, hx₂⟩ theorem strictMonoOn_arcsin : StrictMonoOn arcsin (Icc (-1) 1) := (Subtype.strictMono_coe _).comp_strictMonoOn <| sinOrderIso.symm.strictMono.strictMonoOn_IccExtend _ @[gcongr] theorem arcsin_lt_arcsin {x y : ℝ} (hx : -1 ≤ x) (hlt : x < y) (hy : y ≤ 1) : arcsin x < arcsin y := strictMonoOn_arcsin ⟨hx, hlt.le.trans hy⟩ ⟨hx.trans hlt.le, hy⟩ hlt theorem monotone_arcsin : Monotone arcsin := (Subtype.mono_coe _).comp <| sinOrderIso.symm.monotone.IccExtend _ @[gcongr] theorem arcsin_le_arcsin {x y : ℝ} (h : x ≤ y) : arcsin x ≤ arcsin y := monotone_arcsin h theorem injOn_arcsin : InjOn arcsin (Icc (-1) 1) := strictMonoOn_arcsin.injOn theorem arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arcsin x = arcsin y ↔ x = y := injOn_arcsin.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[continuity, fun_prop] theorem continuous_arcsin : Continuous arcsin := continuous_subtype_val.comp sinOrderIso.symm.continuous.Icc_extend' @[fun_prop] theorem continuousAt_arcsin {x : ℝ} : ContinuousAt arcsin x := continuous_arcsin.continuousAt theorem arcsin_eq_of_sin_eq {x y : ℝ} (h₁ : sin x = y) (h₂ : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin y = x := by subst y exact injOn_sin (arcsin_mem_Icc _) h₂ (sin_arcsin' (sin_mem_Icc x)) @[simp] theorem arcsin_zero : arcsin 0 = 0 := arcsin_eq_of_sin_eq sin_zero ⟨neg_nonpos.2 pi_div_two_pos.le, pi_div_two_pos.le⟩ @[simp] theorem arcsin_one : arcsin 1 = π / 2 := arcsin_eq_of_sin_eq sin_pi_div_two <| right_mem_Icc.2 (neg_le_self pi_div_two_pos.le) theorem arcsin_of_one_le {x : ℝ} (hx : 1 ≤ x) : arcsin x = π / 2 := by rw [← arcsin_projIcc, projIcc_of_right_le _ hx, Subtype.coe_mk, arcsin_one] theorem arcsin_neg_one : arcsin (-1) = -(π / 2) := arcsin_eq_of_sin_eq (by rw [sin_neg, sin_pi_div_two]) <| left_mem_Icc.2 (neg_le_self pi_div_two_pos.le) theorem arcsin_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arcsin x = -(π / 2) := by rw [← arcsin_projIcc, projIcc_of_le_left _ hx, Subtype.coe_mk, arcsin_neg_one] @[simp] theorem arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x := by rcases le_total x (-1) with hx₁ | hx₁ · rw [arcsin_of_le_neg_one hx₁, neg_neg, arcsin_of_one_le (le_neg.2 hx₁)] rcases le_total 1 x with hx₂ | hx₂ · rw [arcsin_of_one_le hx₂, arcsin_of_le_neg_one (neg_le_neg hx₂)] refine arcsin_eq_of_sin_eq ?_ ?_ · rw [sin_neg, sin_arcsin hx₁ hx₂] · exact ⟨neg_le_neg (arcsin_le_pi_div_two _), neg_le.2 (neg_pi_div_two_le_arcsin _)⟩ theorem arcsin_le_iff_le_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rw [← arcsin_sin' hy, strictMonoOn_arcsin.le_iff_le hx (sin_mem_Icc _), arcsin_sin' hy] theorem arcsin_le_iff_le_sin' {x y : ℝ} (hy : y ∈ Ico (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rcases le_total x (-1) with hx₁ | hx₁ · simp [arcsin_of_le_neg_one hx₁, hy.1, hx₁.trans (neg_one_le_sin _)] rcases lt_or_le 1 x with hx₂ | hx₂ · simp [arcsin_of_one_le hx₂.le, hy.2.not_le, (sin_le_one y).trans_lt hx₂] exact arcsin_le_iff_le_sin ⟨hx₁, hx₂⟩ (mem_Icc_of_Ico hy) theorem le_arcsin_iff_sin_le {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin ⟨neg_le_neg hy.2, neg_le.2 hy.1⟩ ⟨neg_le_neg hx.2, neg_le.2 hx.1⟩, sin_neg, neg_le_neg_iff] theorem le_arcsin_iff_sin_le' {x y : ℝ} (hx : x ∈ Ioc (-(π / 2)) (π / 2)) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin' ⟨neg_le_neg hx.2, neg_lt.2 hx.1⟩, sin_neg, neg_le_neg_iff] theorem arcsin_lt_iff_lt_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans <| (not_congr <| le_arcsin_iff_sin_le hy hx).trans not_le theorem arcsin_lt_iff_lt_sin' {x y : ℝ} (hy : y ∈ Ioc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans <| (not_congr <| le_arcsin_iff_sin_le' hy).trans not_le theorem lt_arcsin_iff_sin_lt {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x < arcsin y ↔ sin x < y := not_le.symm.trans <| (not_congr <| arcsin_le_iff_le_sin hy hx).trans not_le theorem lt_arcsin_iff_sin_lt' {x y : ℝ} (hx : x ∈ Ico (-(π / 2)) (π / 2)) : x < arcsin y ↔ sin x < y := not_le.symm.trans <| (not_congr <| arcsin_le_iff_le_sin' hx).trans not_le theorem arcsin_eq_iff_eq_sin {x y : ℝ} (hy : y ∈ Ioo (-(π / 2)) (π / 2)) : arcsin x = y ↔ x = sin y := by simp only [le_antisymm_iff, arcsin_le_iff_le_sin' (mem_Ico_of_Ioo hy), le_arcsin_iff_sin_le' (mem_Ioc_of_Ioo hy)] @[simp] theorem arcsin_nonneg {x : ℝ} : 0 ≤ arcsin x ↔ 0 ≤ x := (le_arcsin_iff_sin_le' ⟨neg_lt_zero.2 pi_div_two_pos, pi_div_two_pos.le⟩).trans <| by rw [sin_zero] @[simp] theorem arcsin_nonpos {x : ℝ} : arcsin x ≤ 0 ↔ x ≤ 0 := neg_nonneg.symm.trans <| arcsin_neg x ▸ arcsin_nonneg.trans neg_nonneg @[simp] theorem arcsin_eq_zero_iff {x : ℝ} : arcsin x = 0 ↔ x = 0 := by simp [le_antisymm_iff] @[simp] theorem zero_eq_arcsin_iff {x} : 0 = arcsin x ↔ x = 0 := eq_comm.trans arcsin_eq_zero_iff @[simp] theorem arcsin_pos {x : ℝ} : 0 < arcsin x ↔ 0 < x := lt_iff_lt_of_le_iff_le arcsin_nonpos @[simp] theorem arcsin_lt_zero {x : ℝ} : arcsin x < 0 ↔ x < 0 := lt_iff_lt_of_le_iff_le arcsin_nonneg @[simp] theorem arcsin_lt_pi_div_two {x : ℝ} : arcsin x < π / 2 ↔ x < 1 := (arcsin_lt_iff_lt_sin' (right_mem_Ioc.2 <| neg_lt_self pi_div_two_pos)).trans <| by rw [sin_pi_div_two] @[simp] theorem neg_pi_div_two_lt_arcsin {x : ℝ} : -(π / 2) < arcsin x ↔ -1 < x := (lt_arcsin_iff_sin_lt' <| left_mem_Ico.2 <| neg_lt_self pi_div_two_pos).trans <| by rw [sin_neg, sin_pi_div_two] @[simp] theorem arcsin_eq_pi_div_two {x : ℝ} : arcsin x = π / 2 ↔ 1 ≤ x := ⟨fun h => not_lt.1 fun h' => (arcsin_lt_pi_div_two.2 h').ne h, arcsin_of_one_le⟩ @[simp] theorem pi_div_two_eq_arcsin {x} : π / 2 = arcsin x ↔ 1 ≤ x := eq_comm.trans arcsin_eq_pi_div_two @[simp] theorem pi_div_two_le_arcsin {x} : π / 2 ≤ arcsin x ↔ 1 ≤ x := (arcsin_le_pi_div_two x).le_iff_eq.trans pi_div_two_eq_arcsin @[simp] theorem arcsin_eq_neg_pi_div_two {x : ℝ} : arcsin x = -(π / 2) ↔ x ≤ -1 := ⟨fun h => not_lt.1 fun h' => (neg_pi_div_two_lt_arcsin.2 h').ne' h, arcsin_of_le_neg_one⟩ @[simp] theorem neg_pi_div_two_eq_arcsin {x} : -(π / 2) = arcsin x ↔ x ≤ -1 := eq_comm.trans arcsin_eq_neg_pi_div_two @[simp] theorem arcsin_le_neg_pi_div_two {x} : arcsin x ≤ -(π / 2) ↔ x ≤ -1 := (neg_pi_div_two_le_arcsin x).le_iff_eq.trans arcsin_eq_neg_pi_div_two @[simp] theorem pi_div_four_le_arcsin {x} : π / 4 ≤ arcsin x ↔ √2 / 2 ≤ x := by rw [← sin_pi_div_four, le_arcsin_iff_sin_le'] have := pi_pos constructor <;> linarith theorem mapsTo_sin_Ioo : MapsTo sin (Ioo (-(π / 2)) (π / 2)) (Ioo (-1) 1) := fun x h => by rwa [mem_Ioo, ← arcsin_lt_pi_div_two, ← neg_pi_div_two_lt_arcsin, arcsin_sin h.1.le h.2.le] /-- `Real.sin` as a `PartialHomeomorph` between `(-π / 2, π / 2)` and `(-1, 1)`. -/ @[simp] def sinPartialHomeomorph : PartialHomeomorph ℝ ℝ where toFun := sin invFun := arcsin source := Ioo (-(π / 2)) (π / 2) target := Ioo (-1) 1 map_source' := mapsTo_sin_Ioo map_target' _ hy := ⟨neg_pi_div_two_lt_arcsin.2 hy.1, arcsin_lt_pi_div_two.2 hy.2⟩ left_inv' _ hx := arcsin_sin hx.1.le hx.2.le right_inv' _ hy := sin_arcsin hy.1.le hy.2.le open_source := isOpen_Ioo open_target := isOpen_Ioo continuousOn_toFun := continuous_sin.continuousOn continuousOn_invFun := continuous_arcsin.continuousOn theorem cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) := cos_nonneg_of_mem_Icc ⟨neg_pi_div_two_le_arcsin _, arcsin_le_pi_div_two _⟩ -- The junk values for `arcsin` and `sqrt` make this true even outside `[-1, 1]`. theorem cos_arcsin (x : ℝ) : cos (arcsin x) = √(1 - x ^ 2) := by by_cases hx₁ : -1 ≤ x; swap · rw [not_le] at hx₁ rw [arcsin_of_le_neg_one hx₁.le, cos_neg, cos_pi_div_two, sqrt_eq_zero_of_nonpos] nlinarith by_cases hx₂ : x ≤ 1; swap · rw [not_le] at hx₂ rw [arcsin_of_one_le hx₂.le, cos_pi_div_two, sqrt_eq_zero_of_nonpos] nlinarith have : sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x) rw [← eq_sub_iff_add_eq', ← sqrt_inj (sq_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))), sq, sqrt_mul_self (cos_arcsin_nonneg _)] at this rw [this, sin_arcsin hx₁ hx₂] -- The junk values for `arcsin` and `sqrt` make this true even outside `[-1, 1]`. theorem tan_arcsin (x : ℝ) : tan (arcsin x) = x / √(1 - x ^ 2) := by rw [tan_eq_sin_div_cos, cos_arcsin] by_cases hx₁ : -1 ≤ x; swap · have h : √(1 - x ^ 2) = 0 := sqrt_eq_zero_of_nonpos (by nlinarith) rw [h] simp by_cases hx₂ : x ≤ 1; swap · have h : √(1 - x ^ 2) = 0 := sqrt_eq_zero_of_nonpos (by nlinarith) rw [h] simp rw [sin_arcsin hx₁ hx₂] /-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`. It defaults to `π` on `(-∞, -1)` and to `0` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arccos (x : ℝ) : ℝ := π / 2 - arcsin x theorem arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl theorem arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x := by simp [arccos] theorem arccos_le_pi (x : ℝ) : arccos x ≤ π := by unfold arccos; linarith [neg_pi_div_two_le_arcsin x] theorem arccos_nonneg (x : ℝ) : 0 ≤ arccos x := by unfold arccos; linarith [arcsin_le_pi_div_two x] @[simp] theorem arccos_pos {x : ℝ} : 0 < arccos x ↔ x < 1 := by simp [arccos] theorem cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x := by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂] theorem arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x := by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin] <;> simp [sub_eq_add_neg] <;> linarith lemma arccos_eq_of_eq_cos (hy₀ : 0 ≤ y) (hy₁ : y ≤ π) (hxy : x = cos y) : arccos x = y := by rw [hxy, arccos_cos hy₀ hy₁] theorem strictAntiOn_arccos : StrictAntiOn arccos (Icc (-1) 1) := fun _ hx _ hy h => sub_lt_sub_left (strictMonoOn_arcsin hx hy h) _ @[gcongr] lemma arccos_lt_arccos {x y : ℝ} (hx : -1 ≤ x) (hlt : x < y) (hy : y ≤ 1) : arccos y < arccos x := by unfold arccos; gcongr <;> assumption @[gcongr] lemma arccos_le_arccos {x y : ℝ} (hlt : x ≤ y) : arccos y ≤ arccos x := by unfold arccos; gcongr theorem antitone_arccos : Antitone arccos := fun _ _ ↦ arccos_le_arccos theorem arccos_injOn : InjOn arccos (Icc (-1) 1) := strictAntiOn_arccos.injOn theorem arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arccos x = arccos y ↔ x = y := arccos_injOn.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[simp] theorem arccos_zero : arccos 0 = π / 2 := by simp [arccos] @[simp] theorem arccos_one : arccos 1 = 0 := by simp [arccos] @[simp] theorem arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves] @[simp] theorem arccos_eq_zero {x} : arccos x = 0 ↔ 1 ≤ x := by simp [arccos, sub_eq_zero] @[simp] theorem arccos_eq_pi_div_two {x} : arccos x = π / 2 ↔ x = 0 := by simp [arccos] @[simp] theorem arccos_eq_pi {x} : arccos x = π ↔ x ≤ -1 := by rw [arccos, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', div_two_sub_self, neg_pi_div_two_eq_arcsin] theorem arccos_neg (x : ℝ) : arccos (-x) = π - arccos x := by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self, sub_neg_eq_add] theorem arccos_of_one_le {x : ℝ} (hx : 1 ≤ x) : arccos x = 0 := by rw [arccos, arcsin_of_one_le hx, sub_self] theorem arccos_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arccos x = π := by rw [arccos, arcsin_of_le_neg_one hx, sub_neg_eq_add, add_halves] -- The junk values for `arccos` and `sqrt` make this true even outside `[-1, 1]`. theorem sin_arccos (x : ℝ) : sin (arccos x) = √(1 - x ^ 2) := by by_cases hx₁ : -1 ≤ x; swap
· rw [not_le] at hx₁ rw [arccos_of_le_neg_one hx₁.le, sin_pi, sqrt_eq_zero_of_nonpos]
Mathlib/Analysis/SpecialFunctions/Trigonometric/Inverse.lean
365
366
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Devon Tuma, Oliver Nash -/ import Mathlib.Algebra.Group.Action.Opposite import Mathlib.Algebra.Group.Submonoid.Membership import Mathlib.Algebra.GroupWithZero.Associated import Mathlib.Algebra.GroupWithZero.Opposite /-! # Non-zero divisors and smul-divisors In this file we define the submonoid `nonZeroDivisors` and `nonZeroSMulDivisors` of a `MonoidWithZero`. We also define `nonZeroDivisorsLeft` and `nonZeroDivisorsRight` for non-commutative monoids. ## Notations This file declares the notations: - `M₀⁰` for the submonoid of non-zero-divisors of `M₀`, in the locale `nonZeroDivisors`. - `M₀⁰[M]` for the submonoid of non-zero smul-divisors of `M₀` with respect to `M`, in the locale `nonZeroSMulDivisors` Use the statement `open scoped nonZeroDivisors nonZeroSMulDivisors` to access this notation in your own code. -/ assert_not_exists Ring open Function section variable (M₀ : Type*) [MonoidWithZero M₀] {x : M₀} /-- The collection of elements of a `MonoidWithZero` that are not left zero divisors form a `Submonoid`. -/ def nonZeroDivisorsLeft : Submonoid M₀ where carrier := {x | ∀ y, y * x = 0 → y = 0} one_mem' := by simp mul_mem' {x} {y} hx hy := fun z hz ↦ hx _ <| hy _ (mul_assoc z x y ▸ hz) @[simp] lemma mem_nonZeroDivisorsLeft_iff : x ∈ nonZeroDivisorsLeft M₀ ↔ ∀ y, y * x = 0 → y = 0 := .rfl lemma nmem_nonZeroDivisorsLeft_iff : x ∉ nonZeroDivisorsLeft M₀ ↔ {y | y * x = 0 ∧ y ≠ 0}.Nonempty := by simpa [mem_nonZeroDivisorsLeft_iff] using Set.nonempty_def.symm /-- The collection of elements of a `MonoidWithZero` that are not right zero divisors form a `Submonoid`. -/ def nonZeroDivisorsRight : Submonoid M₀ where carrier := {x | ∀ y, x * y = 0 → y = 0} one_mem' := by simp mul_mem' := fun {x} {y} hx hy z hz ↦ hy _ (hx _ ((mul_assoc x y z).symm ▸ hz)) @[simp] lemma mem_nonZeroDivisorsRight_iff : x ∈ nonZeroDivisorsRight M₀ ↔ ∀ y, x * y = 0 → y = 0 := .rfl lemma nmem_nonZeroDivisorsRight_iff : x ∉ nonZeroDivisorsRight M₀ ↔ {y | x * y = 0 ∧ y ≠ 0}.Nonempty := by simpa [mem_nonZeroDivisorsRight_iff] using Set.nonempty_def.symm lemma nonZeroDivisorsLeft_eq_right (M₀ : Type*) [CommMonoidWithZero M₀] : nonZeroDivisorsLeft M₀ = nonZeroDivisorsRight M₀ := by ext x; simp [mul_comm x] @[simp] lemma coe_nonZeroDivisorsLeft_eq [NoZeroDivisors M₀] [Nontrivial M₀] : nonZeroDivisorsLeft M₀ = {x : M₀ | x ≠ 0} := by ext x simp only [SetLike.mem_coe, mem_nonZeroDivisorsLeft_iff, mul_eq_zero, forall_eq_or_imp, true_and, Set.mem_setOf_eq] refine ⟨fun h ↦ ?_, fun hx y hx' ↦ by contradiction⟩ contrapose! h exact ⟨1, h, one_ne_zero⟩ @[simp] lemma coe_nonZeroDivisorsRight_eq [NoZeroDivisors M₀] [Nontrivial M₀] : nonZeroDivisorsRight M₀ = {x : M₀ | x ≠ 0} := by ext x simp only [SetLike.mem_coe, mem_nonZeroDivisorsRight_iff, mul_eq_zero, Set.mem_setOf_eq] refine ⟨fun h ↦ ?_, fun hx y hx' ↦ by aesop⟩ contrapose! h exact ⟨1, Or.inl h, one_ne_zero⟩ end /-- The submonoid of non-zero-divisors of a `MonoidWithZero` `M₀`. -/ def nonZeroDivisors (M₀ : Type*) [MonoidWithZero M₀] : Submonoid M₀ where carrier := { x | ∀ z, z * x = 0 → z = 0 } one_mem' _ hz := by rwa [mul_one] at hz mul_mem' hx₁ hx₂ _ hz := by rw [← mul_assoc] at hz exact hx₁ _ (hx₂ _ hz) /-- The notation for the submonoid of non-zero divisors. -/ scoped[nonZeroDivisors] notation:9000 M₀ "⁰" => nonZeroDivisors M₀ /-- Let `M₀` be a monoid with zero and `M` an additive monoid with an `M₀`-action, then the collection of non-zero smul-divisors forms a submonoid. These elements are also called `M`-regular. -/ def nonZeroSMulDivisors (M₀ : Type*) [MonoidWithZero M₀] (M : Type*) [Zero M] [MulAction M₀ M] : Submonoid M₀ where carrier := { r | ∀ m : M, r • m = 0 → m = 0} one_mem' m h := (one_smul M₀ m) ▸ h mul_mem' {r₁ r₂} h₁ h₂ m H := h₂ _ <| h₁ _ <| mul_smul r₁ r₂ m ▸ H /-- The notation for the submonoid of non-zero smul-divisors. -/ scoped[nonZeroSMulDivisors] notation:9000 M₀ "⁰[" M "]" => nonZeroSMulDivisors M₀ M open nonZeroDivisors section MonoidWithZero variable {F M₀ M₀' : Type*} [MonoidWithZero M₀] [MonoidWithZero M₀'] {r x y : M₀} -- this lemma reflects symmetry-breaking in the definition of `nonZeroDivisors` lemma nonZeroDivisorsLeft_eq_nonZeroDivisors : nonZeroDivisorsLeft M₀ = nonZeroDivisors M₀ := rfl lemma nonZeroDivisorsRight_eq_nonZeroSMulDivisors : nonZeroDivisorsRight M₀ = nonZeroSMulDivisors M₀ M₀ := rfl theorem mem_nonZeroDivisors_iff : r ∈ M₀⁰ ↔ ∀ x, x * r = 0 → x = 0 := Iff.rfl lemma nmem_nonZeroDivisors_iff : r ∉ M₀⁰ ↔ {s | s * r = 0 ∧ s ≠ 0}.Nonempty := by simpa [mem_nonZeroDivisors_iff] using Set.nonempty_def.symm theorem mul_right_mem_nonZeroDivisors_eq_zero_iff (hr : r ∈ M₀⁰) : x * r = 0 ↔ x = 0 :=
⟨hr _, by simp +contextual⟩
Mathlib/Algebra/GroupWithZero/NonZeroDivisors.lean
129
130
/- 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.Combinatorics.SimpleGraph.Basic import Mathlib.Data.Fintype.Sigma /-! # Darts in graphs A `Dart` or half-edge or bond in a graph is an ordered pair of adjacent vertices, regarded as an oriented edge. This file defines darts and proves some of their basic properties. -/ namespace SimpleGraph variable {V : Type*} (G : SimpleGraph V) /-- A `Dart` is an oriented edge, implemented as an ordered pair of adjacent vertices. This terminology comes from combinatorial maps, and they are also known as "half-edges" or "bonds." -/ structure Dart extends V × V where adj : G.Adj fst snd deriving DecidableEq initialize_simps_projections Dart (+toProd, -fst, -snd) attribute [simp] Dart.adj variable {G}
theorem Dart.ext_iff (d₁ d₂ : G.Dart) : d₁ = d₂ ↔ d₁.toProd = d₂.toProd := by cases d₁; cases d₂; simp
Mathlib/Combinatorics/SimpleGraph/Dart.lean
33
34
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn, Joachim Breitner -/ import Mathlib.Algebra.Group.Action.End import Mathlib.Algebra.Group.Action.Pointwise.Set.Basic import Mathlib.Algebra.Group.Submonoid.Membership import Mathlib.GroupTheory.Congruence.Basic import Mathlib.GroupTheory.FreeGroup.IsFreeGroup import Mathlib.SetTheory.Cardinal.Basic /-! # The coproduct (a.k.a. the free product) of groups or monoids Given an `ι`-indexed family `M` of monoids, we define their coproduct (a.k.a. free product) `Monoid.CoprodI M`. As usual, we use the suffix `I` for an indexed (co)product, leaving `Coprod` for the coproduct of two monoids. When `ι` and all `M i` have decidable equality, the free product bijects with the type `Monoid.CoprodI.Word M` of reduced words. This bijection is constructed by defining an action of `Monoid.CoprodI M` on `Monoid.CoprodI.Word M`. When `M i` are all groups, `Monoid.CoprodI M` is also a group (and the coproduct in the category of groups). ## Main definitions - `Monoid.CoprodI M`: the free product, defined as a quotient of a free monoid. - `Monoid.CoprodI.of {i} : M i →* Monoid.CoprodI M`. - `Monoid.CoprodI.lift : (∀ {i}, M i →* N) ≃ (Monoid.CoprodI M →* N)`: the universal property. - `Monoid.CoprodI.Word M`: the type of reduced words. - `Monoid.CoprodI.Word.equiv M : Monoid.CoprodI M ≃ word M`. - `Monoid.CoprodI.NeWord M i j`: an inductive description of non-empty words with first letter from `M i` and last letter from `M j`, together with an API (`singleton`, `append`, `head`, `tail`, `to_word`, `Prod`, `inv`). Used in the proof of the Ping-Pong-lemma. - `Monoid.CoprodI.lift_injective_of_ping_pong`: The Ping-Pong-lemma, proving injectivity of the `lift`. See the documentation of that theorem for more information. ## Remarks There are many answers to the question "what is the coproduct of a family `M` of monoids?", and they are all equivalent but not obviously equivalent. We provide two answers. The first, almost tautological answer is given by `Monoid.CoprodI M`, which is a quotient of the type of words in the alphabet `Σ i, M i`. It's straightforward to define and easy to prove its universal property. But this answer is not completely satisfactory, because it's difficult to tell when two elements `x y : Monoid.CoprodI M` are distinct since `Monoid.CoprodI M` is defined as a quotient. The second, maximally efficient answer is given by `Monoid.CoprodI.Word M`. An element of `Monoid.CoprodI.Word M` is a word in the alphabet `Σ i, M i`, where the letter `⟨i, 1⟩` doesn't occur and no adjacent letters share an index `i`. Since we only work with reduced words, there is no need for quotienting, and it is easy to tell when two elements are distinct. However it's not obvious that this is even a monoid! We prove that every element of `Monoid.CoprodI M` can be represented by a unique reduced word, i.e. `Monoid.CoprodI M` and `Monoid.CoprodI.Word M` are equivalent types. This means that `Monoid.CoprodI.Word M` can be given a monoid structure, and it lets us tell when two elements of `Monoid.CoprodI M` are distinct. There is also a completely tautological, maximally inefficient answer given by `MonCat.Colimits.ColimitType`. Whereas `Monoid.CoprodI M` at least ensures that (any instance of) associativity holds by reflexivity, in this answer associativity holds because of quotienting. Yet another answer, which is constructively more satisfying, could be obtained by showing that `Monoid.CoprodI.Rel` is confluent. ## References [van der Waerden, *Free products of groups*][MR25465] -/ open Set variable {ι : Type*} (M : ι → Type*) [∀ i, Monoid (M i)] /-- A relation on the free monoid on alphabet `Σ i, M i`, relating `⟨i, 1⟩` with `1` and `⟨i, x⟩ * ⟨i, y⟩` with `⟨i, x * y⟩`. -/ inductive Monoid.CoprodI.Rel : FreeMonoid (Σ i, M i) → FreeMonoid (Σ i, M i) → Prop | of_one (i : ι) : Monoid.CoprodI.Rel (FreeMonoid.of ⟨i, 1⟩) 1 | of_mul {i : ι} (x y : M i) : Monoid.CoprodI.Rel (FreeMonoid.of ⟨i, x⟩ * FreeMonoid.of ⟨i, y⟩) (FreeMonoid.of ⟨i, x * y⟩) /-- The free product (categorical coproduct) of an indexed family of monoids. -/ def Monoid.CoprodI : Type _ := (conGen (Monoid.CoprodI.Rel M)).Quotient -- The `Monoid` instance should be constructed by a deriving handler. -- https://github.com/leanprover-community/mathlib4/issues/380 instance : Monoid (Monoid.CoprodI M) := by delta Monoid.CoprodI; infer_instance instance : Inhabited (Monoid.CoprodI M) := ⟨1⟩ namespace Monoid.CoprodI /-- The type of reduced words. A reduced word cannot contain a letter `1`, and no two adjacent letters can come from the same summand. -/ @[ext] structure Word where /-- A `Word` is a `List (Σ i, M i)`, such that `1` is not in the list, and no two adjacent letters are from the same summand -/ toList : List (Σi, M i) /-- A reduced word does not contain `1` -/ ne_one : ∀ l ∈ toList, Sigma.snd l ≠ 1 /-- Adjacent letters are not from the same summand. -/ chain_ne : toList.Chain' fun l l' => Sigma.fst l ≠ Sigma.fst l' variable {M} /-- The inclusion of a summand into the free product. -/ def of {i : ι} : M i →* CoprodI M where toFun x := Con.mk' _ (FreeMonoid.of <| Sigma.mk i x) map_one' := (Con.eq _).mpr (ConGen.Rel.of _ _ (CoprodI.Rel.of_one i)) map_mul' x y := Eq.symm <| (Con.eq _).mpr (ConGen.Rel.of _ _ (CoprodI.Rel.of_mul x y)) theorem of_apply {i} (m : M i) : of m = Con.mk' _ (FreeMonoid.of <| Sigma.mk i m) := rfl variable {N : Type*} [Monoid N] /-- See note [partially-applied ext lemmas]. -/ -- Porting note: higher `ext` priority @[ext 1100] theorem ext_hom (f g : CoprodI M →* N) (h : ∀ i, f.comp (of : M i →* _) = g.comp of) : f = g := (MonoidHom.cancel_right Con.mk'_surjective).mp <| FreeMonoid.hom_eq fun ⟨i, x⟩ => by rw [MonoidHom.comp_apply, MonoidHom.comp_apply, ← of_apply] unfold CoprodI rw [← MonoidHom.comp_apply, ← MonoidHom.comp_apply, h] /-- A map out of the free product corresponds to a family of maps out of the summands. This is the universal property of the free product, characterizing it as a categorical coproduct. -/ @[simps symm_apply] def lift : (∀ i, M i →* N) ≃ (CoprodI M →* N) where toFun fi := Con.lift _ (FreeMonoid.lift fun p : Σi, M i => fi p.fst p.snd) <| Con.conGen_le <| by simp_rw [Con.ker_rel] rintro _ _ (i | ⟨x, y⟩) <;> simp invFun f _ := f.comp of left_inv := by intro fi ext i x rfl right_inv := by intro f ext i x rfl @[simp] theorem lift_comp_of {N} [Monoid N] (fi : ∀ i, M i →* N) i : (lift fi).comp of = fi i := congr_fun (lift.symm_apply_apply fi) i @[simp] theorem lift_of {N} [Monoid N] (fi : ∀ i, M i →* N) {i} (m : M i) : lift fi (of m) = fi i m := DFunLike.congr_fun (lift_comp_of ..) m @[simp] theorem lift_comp_of' {N} [Monoid N] (f : CoprodI M →* N) : lift (fun i ↦ f.comp (of (i := i))) = f := lift.apply_symm_apply f @[simp] theorem lift_of' : lift (fun i ↦ (of : M i →* CoprodI M)) = .id (CoprodI M) := lift_comp_of' (.id _) theorem of_leftInverse [DecidableEq ι] (i : ι) : Function.LeftInverse (lift <| Pi.mulSingle i (MonoidHom.id (M i))) of := fun x => by simp only [lift_of, Pi.mulSingle_eq_same, MonoidHom.id_apply] theorem of_injective (i : ι) : Function.Injective (of : M i →* _) := by classical exact (of_leftInverse i).injective theorem mrange_eq_iSup {N} [Monoid N] (f : ∀ i, M i →* N) : MonoidHom.mrange (lift f) = ⨆ i, MonoidHom.mrange (f i) := by rw [lift, Equiv.coe_fn_mk, Con.lift_range, FreeMonoid.mrange_lift, range_sigma_eq_iUnion_range, Submonoid.closure_iUnion] simp only [MonoidHom.mclosure_range] theorem lift_mrange_le {N} [Monoid N] (f : ∀ i, M i →* N) {s : Submonoid N} : MonoidHom.mrange (lift f) ≤ s ↔ ∀ i, MonoidHom.mrange (f i) ≤ s := by simp [mrange_eq_iSup] @[simp] theorem iSup_mrange_of : ⨆ i, MonoidHom.mrange (of : M i →* CoprodI M) = ⊤ := by simp [← mrange_eq_iSup] @[simp] theorem mclosure_iUnion_range_of : Submonoid.closure (⋃ i, Set.range (of : M i →* CoprodI M)) = ⊤ := by simp [Submonoid.closure_iUnion] @[elab_as_elim] theorem induction_left {motive : CoprodI M → Prop} (m : CoprodI M) (one : motive 1) (mul : ∀ {i} (m : M i) x, motive x → motive (of m * x)) : motive m := by induction m using Submonoid.induction_of_closure_eq_top_left mclosure_iUnion_range_of with | one => exact one | mul x hx y ihy => obtain ⟨i, m, rfl⟩ : ∃ (i : ι) (m : M i), of m = x := by simpa using hx exact mul m y ihy @[elab_as_elim] theorem induction_on {motive : CoprodI M → Prop} (m : CoprodI M) (one : motive 1) (of : ∀ (i) (m : M i), motive (of m)) (mul : ∀ x y, motive x → motive y → motive (x * y)) : motive m := by induction m using CoprodI.induction_left with | one => exact one | mul m x hx => exact mul _ _ (of _ _) hx section Group variable (G : ι → Type*) [∀ i, Group (G i)] instance : Inv (CoprodI G) where inv := MulOpposite.unop ∘ lift fun i => (of : G i →* _).op.comp (MulEquiv.inv' (G i)).toMonoidHom theorem inv_def (x : CoprodI G) : x⁻¹ = MulOpposite.unop (lift (fun i => (of : G i →* _).op.comp (MulEquiv.inv' (G i)).toMonoidHom) x) := rfl instance : Group (CoprodI G) := { inv_mul_cancel := by intro m rw [inv_def] induction m using CoprodI.induction_on with | one => rw [MonoidHom.map_one, MulOpposite.unop_one, one_mul] | of m ih => change of _⁻¹ * of _ = 1 rw [← of.map_mul, inv_mul_cancel, of.map_one] | mul x y ihx ihy => rw [MonoidHom.map_mul, MulOpposite.unop_mul, mul_assoc, ← mul_assoc _ x y, ihx, one_mul, ihy] } theorem lift_range_le {N} [Group N] (f : ∀ i, G i →* N) {s : Subgroup N} (h : ∀ i, (f i).range ≤ s) : (lift f).range ≤ s := by rintro _ ⟨x, rfl⟩ induction x using CoprodI.induction_on with | one => exact s.one_mem | of i x => simp only [lift_of, SetLike.mem_coe] exact h i (Set.mem_range_self x) | mul x y hx hy => simp only [map_mul, SetLike.mem_coe] exact s.mul_mem hx hy theorem range_eq_iSup {N} [Group N] (f : ∀ i, G i →* N) : (lift f).range = ⨆ i, (f i).range := by apply le_antisymm (lift_range_le _ f fun i => le_iSup (fun i => MonoidHom.range (f i)) i) apply iSup_le _ rintro i _ ⟨x, rfl⟩ exact ⟨of x, by simp only [lift_of]⟩ end Group namespace Word /-- The empty reduced word. -/ @[simps] def empty : Word M where toList := [] ne_one := by simp chain_ne := List.chain'_nil instance : Inhabited (Word M) := ⟨empty⟩ /-- A reduced word determines an element of the free product, given by multiplication. -/ def prod (w : Word M) : CoprodI M := List.prod (w.toList.map fun l => of l.snd) @[simp] theorem prod_empty : prod (empty : Word M) = 1 := rfl /-- `fstIdx w` is `some i` if the first letter of `w` is `⟨i, m⟩` with `m : M i`. If `w` is empty then it's `none`. -/ def fstIdx (w : Word M) : Option ι := w.toList.head?.map Sigma.fst theorem fstIdx_ne_iff {w : Word M} {i} : fstIdx w ≠ some i ↔ ∀ l ∈ w.toList.head?, i ≠ Sigma.fst l := not_iff_not.mp <| by simp [fstIdx] variable (M) /-- Given an index `i : ι`, `Pair M i` is the type of pairs `(head, tail)` where `head : M i` and `tail : Word M`, subject to the constraint that first letter of `tail` can't be `⟨i, m⟩`. By prepending `head` to `tail`, one obtains a new word. We'll show that any word can be uniquely obtained in this way. -/ @[ext] structure Pair (i : ι) where /-- An element of `M i`, the first letter of the word. -/ head : M i /-- The remaining letters of the word, excluding the first letter -/ tail : Word M /-- The index first letter of tail of a `Pair M i` is not equal to `i` -/ fstIdx_ne : fstIdx tail ≠ some i instance (i : ι) : Inhabited (Pair M i) := ⟨⟨1, empty, by tauto⟩⟩ variable {M} /-- Construct a new `Word` without any reduction. The underlying list of `cons m w _ _` is `⟨_, m⟩::w` -/ @[simps] def cons {i} (m : M i) (w : Word M) (hmw : w.fstIdx ≠ some i) (h1 : m ≠ 1) : Word M := { toList := ⟨i, m⟩ :: w.toList, ne_one := by simp only [List.mem_cons] rintro l (rfl | hl) · exact h1 · exact w.ne_one l hl chain_ne := w.chain_ne.cons' (fstIdx_ne_iff.mp hmw) } @[simp] theorem fstIdx_cons {i} (m : M i) (w : Word M) (hmw : w.fstIdx ≠ some i) (h1 : m ≠ 1) : fstIdx (cons m w hmw h1) = some i := by simp [cons, fstIdx] @[simp] theorem prod_cons (i) (m : M i) (w : Word M) (h1 : m ≠ 1) (h2 : w.fstIdx ≠ some i) : prod (cons m w h2 h1) = of m * prod w := by simp [cons, prod, List.map_cons, List.prod_cons] section variable [∀ i, DecidableEq (M i)] /-- Given a pair `(head, tail)`, we can form a word by prepending `head` to `tail`, except if `head` is `1 : M i` then we have to just return `Word` since we need the result to be reduced. -/ def rcons {i} (p : Pair M i) : Word M := if h : p.head = 1 then p.tail else cons p.head p.tail p.fstIdx_ne h @[simp] theorem prod_rcons {i} (p : Pair M i) : prod (rcons p) = of p.head * prod p.tail := if hm : p.head = 1 then by rw [rcons, dif_pos hm, hm, MonoidHom.map_one, one_mul] else by rw [rcons, dif_neg hm, cons, prod, List.map_cons, List.prod_cons, prod] theorem rcons_inj {i} : Function.Injective (rcons : Pair M i → Word M) := by rintro ⟨m, w, h⟩ ⟨m', w', h'⟩ he by_cases hm : m = 1 <;> by_cases hm' : m' = 1 · simp only [rcons, dif_pos hm, dif_pos hm'] at he aesop · exfalso simp only [rcons, dif_pos hm, dif_neg hm'] at he rw [he] at h exact h rfl · exfalso simp only [rcons, dif_pos hm', dif_neg hm] at he rw [← he] at h' exact h' rfl · have : m = m' ∧ w.toList = w'.toList := by simpa [cons, rcons, dif_neg hm, dif_neg hm', eq_self_iff_true, Subtype.mk_eq_mk, heq_iff_eq, ← Subtype.ext_iff_val] using he rcases this with ⟨rfl, h⟩ congr exact Word.ext h theorem mem_rcons_iff {i j : ι} (p : Pair M i) (m : M j) : ⟨_, m⟩ ∈ (rcons p).toList ↔ ⟨_, m⟩ ∈ p.tail.toList ∨ m ≠ 1 ∧ (∃ h : i = j, m = h ▸ p.head) := by simp only [rcons, cons, ne_eq] by_cases hij : i = j · subst i by_cases hm : m = p.head · subst m split_ifs <;> simp_all · split_ifs <;> simp_all · split_ifs <;> simp_all [Ne.symm hij] end /-- Induct on a word by adding letters one at a time without reduction, effectively inducting on the underlying `List`. -/ @[elab_as_elim] def consRecOn {motive : Word M → Sort*} (w : Word M) (empty : motive empty) (cons : ∀ (i) (m : M i) (w) h1 h2, motive w → motive (cons m w h1 h2)) : motive w := by rcases w with ⟨w, h1, h2⟩ induction w with | nil => exact empty | cons m w ih => refine cons m.1 m.2 ⟨w, fun _ hl => h1 _ (List.mem_cons_of_mem _ hl), h2.tail⟩ ?_ ?_ (ih _ _) · rw [List.chain'_cons'] at h2 simp only [fstIdx, ne_eq, Option.map_eq_some_iff, Sigma.exists, exists_and_right, exists_eq_right, not_exists] intro m' hm' exact h2.1 _ hm' rfl · exact h1 _ List.mem_cons_self @[simp] theorem consRecOn_empty {motive : Word M → Sort*} (h_empty : motive empty) (h_cons : ∀ (i) (m : M i) (w) h1 h2, motive w → motive (cons m w h1 h2)) : consRecOn empty h_empty h_cons = h_empty := rfl @[simp] theorem consRecOn_cons {motive : Word M → Sort*} (i) (m : M i) (w : Word M) h1 h2 (h_empty : motive empty) (h_cons : ∀ (i) (m : M i) (w) h1 h2, motive w → motive (cons m w h1 h2)) : consRecOn (cons m w h1 h2) h_empty h_cons = h_cons i m w h1 h2 (consRecOn w h_empty h_cons) := rfl variable [DecidableEq ι] [∀ i, DecidableEq (M i)] -- This definition is computable but not very nice to look at. Thankfully we don't have to inspect -- it, since `rcons` is known to be injective. /-- Given `i : ι`, any reduced word can be decomposed into a pair `p` such that `w = rcons p`. -/ private def equivPairAux (i) (w : Word M) : { p : Pair M i // rcons p = w } := consRecOn w ⟨⟨1, .empty, by simp [fstIdx, empty]⟩, by simp [rcons]⟩ <| fun j m w h1 h2 _ => if ij : i = j then { val := { head := ij ▸ m tail := w fstIdx_ne := ij ▸ h1 } property := by subst ij; simp [rcons, h2] } else ⟨⟨1, cons m w h1 h2, by simp [cons, fstIdx, Ne.symm ij]⟩, by simp [rcons]⟩ /-- The equivalence between words and pairs. Given a word, it decomposes it as a pair by removing the first letter if it comes from `M i`. Given a pair, it prepends the head to the tail. -/ def equivPair (i) : Word M ≃ Pair M i where toFun w := (equivPairAux i w).val invFun := rcons left_inv w := (equivPairAux i w).property right_inv _ := rcons_inj (equivPairAux i _).property theorem equivPair_symm (i) (p : Pair M i) : (equivPair i).symm p = rcons p := rfl theorem equivPair_eq_of_fstIdx_ne {i} {w : Word M} (h : fstIdx w ≠ some i) : equivPair i w = ⟨1, w, h⟩ := (equivPair i).apply_eq_iff_eq_symm_apply.mpr <| Eq.symm (dif_pos rfl) theorem mem_equivPair_tail_iff {i j : ι} {w : Word M} (m : M i) : (⟨i, m⟩ ∈ (equivPair j w).tail.toList) ↔ ⟨i, m⟩ ∈ w.toList.tail ∨ i ≠ j ∧ ∃ h : w.toList ≠ [], w.toList.head h = ⟨i, m⟩ := by simp only [equivPair, equivPairAux, ne_eq, Equiv.coe_fn_mk] induction w using consRecOn with | empty => simp | cons k g tail h1 h2 ih => simp only [consRecOn_cons] split_ifs with h · subst k by_cases hij : j = i <;> simp_all · by_cases hik : i = k · subst i; simp_all [@eq_comm _ m g, @eq_comm _ k j, or_comm] · simp [hik, Ne.symm hik] theorem mem_of_mem_equivPair_tail {i j : ι} {w : Word M} (m : M i) : (⟨i, m⟩ ∈ (equivPair j w).tail.toList) → ⟨i, m⟩ ∈ w.toList := by rw [mem_equivPair_tail_iff] rintro (h | h) · exact List.mem_of_mem_tail h · revert h; cases w.toList <;> simp +contextual theorem equivPair_head {i : ι} {w : Word M} : (equivPair i w).head = if h : ∃ (h : w.toList ≠ []), (w.toList.head h).1 = i then h.snd ▸ (w.toList.head h.1).2 else 1 := by simp only [equivPair, equivPairAux] induction w using consRecOn with | empty => simp | cons head => by_cases hi : i = head · subst hi; simp · simp [hi, Ne.symm hi] instance summandAction (i) : MulAction (M i) (Word M) where smul m w := rcons { equivPair i w with head := m * (equivPair i w).head } one_smul w := by apply (equivPair i).symm_apply_eq.mpr simp [equivPair] mul_smul m m' w := by dsimp [instHSMul] simp [mul_assoc, ← equivPair_symm, Equiv.apply_symm_apply] instance : MulAction (CoprodI M) (Word M) := MulAction.ofEndHom (lift fun _ => MulAction.toEndHom) theorem smul_def {i} (m : M i) (w : Word M) : m • w = rcons { equivPair i w with head := m * (equivPair i w).head } := rfl theorem of_smul_def (i) (w : Word M) (m : M i) : of m • w = rcons { equivPair i w with head := m * (equivPair i w).head } := rfl theorem equivPair_smul_same {i} (m : M i) (w : Word M) : equivPair i (of m • w) = ⟨m * (equivPair i w).head, (equivPair i w).tail, (equivPair i w).fstIdx_ne⟩ := by rw [of_smul_def, ← equivPair_symm] simp @[simp] theorem equivPair_tail {i} (p : Pair M i) : equivPair i p.tail = ⟨1, p.tail, p.fstIdx_ne⟩ := equivPair_eq_of_fstIdx_ne _ theorem smul_eq_of_smul {i} (m : M i) (w : Word M) : m • w = of m • w := rfl theorem mem_smul_iff {i j : ι} {m₁ : M i} {m₂ : M j} {w : Word M} : ⟨_, m₁⟩ ∈ (of m₂ • w).toList ↔ (¬i = j ∧ ⟨i, m₁⟩ ∈ w.toList) ∨ (m₁ ≠ 1 ∧ ∃ (hij : i = j),(⟨i, m₁⟩ ∈ w.toList.tail) ∨ (∃ m', ⟨j, m'⟩ ∈ w.toList.head? ∧ m₁ = hij ▸ (m₂ * m')) ∨ (w.fstIdx ≠ some j ∧ m₁ = hij ▸ m₂)) := by rw [of_smul_def, mem_rcons_iff, mem_equivPair_tail_iff, equivPair_head, or_assoc] by_cases hij : i = j · subst i simp only [not_true, ne_eq, false_and, exists_prop, true_and, false_or] by_cases hw : ⟨j, m₁⟩ ∈ w.toList.tail · simp [hw, show m₁ ≠ 1 from w.ne_one _ (List.mem_of_mem_tail hw)] · simp only [hw, false_or, Option.mem_def, ne_eq, and_congr_right_iff] intro hm1 split_ifs with h · rcases h with ⟨hnil, rfl⟩ simp only [List.head?_eq_head hnil, Option.some.injEq, ne_eq] constructor · rintro rfl exact Or.inl ⟨_, rfl, rfl⟩ · rintro (⟨_, h, rfl⟩ | hm') · simp only [Sigma.ext_iff, heq_eq_eq, true_and] at h subst h rfl · simp only [fstIdx, Option.map_eq_some_iff, Sigma.exists, exists_and_right, exists_eq_right, not_exists, ne_eq] at hm' exact (hm'.1 (w.toList.head hnil).2 (by rw [List.head?_eq_head])).elim · revert h rw [fstIdx] cases w.toList · simp · simp +contextual [Sigma.ext_iff] · rcases w with ⟨_ | _, _, _⟩ <;> simp [or_comm, hij, Ne.symm hij]; rw [eq_comm] theorem mem_smul_iff_of_ne {i j : ι} (hij : i ≠ j) {m₁ : M i} {m₂ : M j} {w : Word M} : ⟨_, m₁⟩ ∈ (of m₂ • w).toList ↔ ⟨i, m₁⟩ ∈ w.toList := by simp [mem_smul_iff, *] theorem cons_eq_smul {i} {m : M i} {ls h1 h2} : cons m ls h1 h2 = of m • ls := by rw [of_smul_def, equivPair_eq_of_fstIdx_ne _] · simp [cons, rcons, h2] · exact h1 theorem rcons_eq_smul {i} (p : Pair M i) : rcons p = of p.head • p.tail := by simp [of_smul_def] @[simp] theorem equivPair_head_smul_equivPair_tail {i : ι} (w : Word M) : of (equivPair i w).head • (equivPair i w).tail = w := by rw [← rcons_eq_smul, ← equivPair_symm, Equiv.symm_apply_apply] theorem equivPair_tail_eq_inv_smul {G : ι → Type*} [∀ i, Group (G i)] [∀ i, DecidableEq (G i)] {i} (w : Word G) : (equivPair i w).tail = (of (equivPair i w).head)⁻¹ • w := Eq.symm <| inv_smul_eq_iff.2 (equivPair_head_smul_equivPair_tail w).symm @[elab_as_elim] theorem smul_induction {motive : Word M → Prop} (empty : motive empty) (smul : ∀ (i) (m : M i) (w), motive w → motive (of m • w)) (w : Word M) : motive w := by induction w using consRecOn with | empty => exact empty | cons _ _ _ _ _ ih => rw [cons_eq_smul] exact smul _ _ _ ih @[simp] theorem prod_smul (m) : ∀ w : Word M, prod (m • w) = m * prod w := by induction m using CoprodI.induction_on with | one => intro rw [one_smul, one_mul] | of _ => intros rw [of_smul_def, prod_rcons, of.map_mul, mul_assoc, ← prod_rcons, ← equivPair_symm, Equiv.symm_apply_apply] | mul x y hx hy => intro w rw [mul_smul, hx, hy, mul_assoc] /-- Each element of the free product corresponds to a unique reduced word. -/ def equiv : CoprodI M ≃ Word M where toFun m := m • empty invFun w := prod w left_inv m := by dsimp only; rw [prod_smul, prod_empty, mul_one] right_inv := by apply smul_induction · dsimp only rw [prod_empty, one_smul] · dsimp only intro i m w ih rw [prod_smul, mul_smul, ih]
instance : DecidableEq (Word M) := Function.Injective.decidableEq fun _ _ => Word.ext instance : DecidableEq (CoprodI M) := Equiv.decidableEq Word.equiv
Mathlib/GroupTheory/CoprodI.lean
609
615
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.LinearAlgebra.AffineSpace.AffineMap import Mathlib.Tactic.FieldSimp /-! # Slope of a function In this file we define the slope of a function `f : k → PE` taking values in an affine space over `k` and prove some basic theorems about `slope`. The `slope` function naturally appears in the Mean Value Theorem, and in the proof of the fact that a function with nonnegative second derivative on an interval is convex on this interval. ## Tags affine space, slope -/ open AffineMap variable {k E PE : Type*} [Field k] [AddCommGroup E] [Module k E] [AddTorsor E PE] /-- `slope f a b = (b - a)⁻¹ • (f b -ᵥ f a)` is the slope of a function `f` on the interval `[a, b]`. Note that `slope f a a = 0`, not the derivative of `f` at `a`. -/ def slope (f : k → PE) (a b : k) : E := (b - a)⁻¹ • (f b -ᵥ f a) theorem slope_fun_def (f : k → PE) : slope f = fun a b => (b - a)⁻¹ • (f b -ᵥ f a) := rfl theorem slope_def_field (f : k → k) (a b : k) : slope f a b = (f b - f a) / (b - a) := (div_eq_inv_mul _ _).symm theorem slope_fun_def_field (f : k → k) (a : k) : slope f a = fun b => (f b - f a) / (b - a) := (div_eq_inv_mul _ _).symm @[simp] theorem slope_same (f : k → PE) (a : k) : (slope f a a : E) = 0 := by rw [slope, sub_self, inv_zero, zero_smul] theorem slope_def_module (f : k → E) (a b : k) : slope f a b = (b - a)⁻¹ • (f b - f a) := rfl @[simp] theorem sub_smul_slope (f : k → PE) (a b : k) : (b - a) • slope f a b = f b -ᵥ f a := by rcases eq_or_ne a b with (rfl | hne) · rw [sub_self, zero_smul, vsub_self] · rw [slope, smul_inv_smul₀ (sub_ne_zero.2 hne.symm)] theorem sub_smul_slope_vadd (f : k → PE) (a b : k) : (b - a) • slope f a b +ᵥ f a = f b := by rw [sub_smul_slope, vsub_vadd] @[simp] theorem slope_vadd_const (f : k → E) (c : PE) : (slope fun x => f x +ᵥ c) = slope f := by ext a b simp only [slope, vadd_vsub_vadd_cancel_right, vsub_eq_sub] @[simp] theorem slope_sub_smul (f : k → E) {a b : k} (h : a ≠ b) : slope (fun x => (x - a) • f x) a b = f b := by simp [slope, inv_smul_smul₀ (sub_ne_zero.2 h.symm)] theorem eq_of_slope_eq_zero {f : k → PE} {a b : k} (h : slope f a b = (0 : E)) : f a = f b := by rw [← sub_smul_slope_vadd f a b, h, smul_zero, zero_vadd] theorem AffineMap.slope_comp {F PF : Type*} [AddCommGroup F] [Module k F] [AddTorsor F PF] (f : PE →ᵃ[k] PF) (g : k → PE) (a b : k) : slope (f ∘ g) a b = f.linear (slope g a b) := by simp only [slope, (· ∘ ·), f.linear.map_smul, f.linearMap_vsub] theorem LinearMap.slope_comp {F : Type*} [AddCommGroup F] [Module k F] (f : E →ₗ[k] F) (g : k → E) (a b : k) : slope (f ∘ g) a b = f (slope g a b) := f.toAffineMap.slope_comp g a b theorem slope_comm (f : k → PE) (a b : k) : slope f a b = slope f b a := by rw [slope, slope, ← neg_vsub_eq_vsub_rev, smul_neg, ← neg_smul, neg_inv, neg_sub] @[simp] lemma slope_neg (f : k → E) (x y : k) : slope (fun t ↦ -f t) x y = -slope f x y := by simp only [slope_def_module, neg_sub_neg, ← smul_neg, neg_sub] @[simp] lemma slope_neg_fun (f : k → E) : slope (-f) = -slope f := by ext x y; exact slope_neg f x y /-- `slope f a c` is a linear combination of `slope f a b` and `slope f b c`. This version explicitly provides coefficients. If `a ≠ c`, then the sum of the coefficients is `1`, so it is actually an affine combination, see `lineMap_slope_slope_sub_div_sub`. -/ theorem sub_div_sub_smul_slope_add_sub_div_sub_smul_slope (f : k → PE) (a b c : k) : ((b - a) / (c - a)) • slope f a b + ((c - b) / (c - a)) • slope f b c = slope f a c := by by_cases hab : a = b · subst hab rw [sub_self, zero_div, zero_smul, zero_add] by_cases hac : a = c · simp [hac] · rw [div_self (sub_ne_zero.2 <| Ne.symm hac), one_smul] by_cases hbc : b = c · subst hbc simp [sub_ne_zero.2 (Ne.symm hab)] rw [add_comm] simp_rw [slope, div_eq_inv_mul, mul_smul, ← smul_add, smul_inv_smul₀ (sub_ne_zero.2 <| Ne.symm hab), smul_inv_smul₀ (sub_ne_zero.2 <| Ne.symm hbc), vsub_add_vsub_cancel] /-- `slope f a c` is an affine combination of `slope f a b` and `slope f b c`. This version uses `lineMap` to express this property. -/ theorem lineMap_slope_slope_sub_div_sub (f : k → PE) (a b c : k) (h : a ≠ c) : lineMap (slope f a b) (slope f b c) ((c - b) / (c - a)) = slope f a c := by field_simp [sub_ne_zero.2 h.symm, ← sub_div_sub_smul_slope_add_sub_div_sub_smul_slope f a b c, lineMap_apply_module] /-- `slope f a b` is an affine combination of `slope f a (lineMap a b r)` and `slope f (lineMap a b r) b`. We use `lineMap` to express this property. -/ theorem lineMap_slope_lineMap_slope_lineMap (f : k → PE) (a b r : k) : lineMap (slope f (lineMap a b r) b) (slope f a (lineMap a b r)) r = slope f a b := by obtain rfl | hab : a = b ∨ a ≠ b := Classical.em _; · simp rw [slope_comm _ a, slope_comm _ a, slope_comm _ _ b] convert lineMap_slope_slope_sub_div_sub f b (lineMap a b r) a hab.symm using 2 rw [lineMap_apply_ring, eq_div_iff (sub_ne_zero.2 hab), sub_mul, one_mul, mul_sub, ← sub_sub, sub_sub_cancel]
Mathlib/LinearAlgebra/AffineSpace/Slope.lean
124
127
/- 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 -/ import Mathlib.Data.Bundle import Mathlib.Data.Set.Image import Mathlib.Topology.CompactOpen import Mathlib.Topology.PartialHomeomorph import Mathlib.Topology.Order.Basic /-! # Trivializations ## Main definitions ### Basic definitions * `Trivialization F p` : structure extending partial homeomorphisms, defining a local trivialization of a topological space `Z` with projection `p` and fiber `F`. * `Pretrivialization F proj` : trivialization as a partial equivalence, mainly used when the topology on the total space has not yet been defined. ### Operations on bundles We provide the following operations on `Trivialization`s. * `Trivialization.compHomeomorph`: given a local trivialization `e` of a fiber bundle `p : Z → B` and a homeomorphism `h : Z' ≃ₜ Z`, returns a local trivialization of the fiber bundle `p ∘ h`. ## Implementation notes Previously, in mathlib, there was a structure `topological_vector_bundle.trivialization` which extended another structure `topological_fiber_bundle.trivialization` by a linearity hypothesis. As of PR https://github.com/leanprover-community/mathlib3/pull/17359, we have changed this to a single structure `Trivialization` (no namespace), together with a mixin class `Trivialization.IsLinear`. This permits all the *data* of a vector bundle to be held at the level of fiber bundles, so that the same trivializations can underlie an object's structure as (say) a vector bundle over `ℂ` and as a vector bundle over `ℝ`, as well as its structure simply as a fiber bundle. This might be a little surprising, given the general trend of the library to ever-increased bundling. But in this case the typical motivation for more bundling does not apply: there is no algebraic or order structure on the whole type of linear (say) trivializations of a bundle. Indeed, since trivializations only have meaning on their base sets (taking junk values outside), the type of linear trivializations is not even particularly well-behaved. -/ open TopologicalSpace Filter Set Bundle Function open scoped Topology variable {B : Type*} (F : Type*) {E : B → Type*} variable {Z : Type*} [TopologicalSpace B] [TopologicalSpace F] {proj : Z → B} /-- This structure contains the information left for a local trivialization (which is implemented below as `Trivialization F proj`) if the total space has not been given a topology, but we have a topology on both the fiber and the base space. Through the construction `topological_fiber_prebundle F proj` it will be possible to promote a `Pretrivialization F proj` to a `Trivialization F proj`. -/ structure Pretrivialization (proj : Z → B) extends PartialEquiv Z (B × F) where open_target : IsOpen target baseSet : Set B open_baseSet : IsOpen baseSet source_eq : source = proj ⁻¹' baseSet target_eq : target = baseSet ×ˢ univ proj_toFun : ∀ p ∈ source, (toFun p).1 = proj p namespace Pretrivialization variable {F} variable (e : Pretrivialization F proj) {x : Z} /-- Coercion of a pretrivialization to a function. We don't use `e.toFun` in the `CoeFun` instance because it is actually `e.toPartialEquiv.toFun`, so `simp` will apply lemmas about `toPartialEquiv`. While we may want to switch to this behavior later, doing it mid-port will break a lot of proofs. -/ @[coe] def toFun' : Z → (B × F) := e.toFun instance : CoeFun (Pretrivialization F proj) fun _ => Z → B × F := ⟨toFun'⟩ @[ext] lemma ext' (e e' : Pretrivialization F proj) (h₁ : e.toPartialEquiv = e'.toPartialEquiv) (h₂ : e.baseSet = e'.baseSet) : e = e' := by cases e; cases e'; congr -- TODO: move `ext` here? lemma ext {e e' : Pretrivialization F proj} (h₁ : ∀ x, e x = e' x) (h₂ : ∀ x, e.toPartialEquiv.symm x = e'.toPartialEquiv.symm x) (h₃ : e.baseSet = e'.baseSet) : e = e' := by ext1 <;> [ext1; exact h₃] · apply h₁ · apply h₂ · rw [e.source_eq, e'.source_eq, h₃] /-- If the fiber is nonempty, then the projection also is. -/ lemma toPartialEquiv_injective [Nonempty F] : Injective (toPartialEquiv : Pretrivialization F proj → PartialEquiv Z (B × F)) := by refine fun e e' h ↦ ext' _ _ h ?_ simpa only [fst_image_prod, univ_nonempty, target_eq] using congr_arg (Prod.fst '' PartialEquiv.target ·) h @[simp, mfld_simps] theorem coe_coe : ⇑e.toPartialEquiv = e := rfl @[simp, mfld_simps] theorem coe_fst (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_toFun x ex theorem mem_source : x ∈ e.source ↔ proj x ∈ e.baseSet := by rw [e.source_eq, mem_preimage] theorem coe_fst' (ex : proj x ∈ e.baseSet) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex) protected theorem eqOn : EqOn (Prod.fst ∘ e) proj e.source := fun _ hx => e.coe_fst hx theorem mk_proj_snd (ex : x ∈ e.source) : (proj x, (e x).2) = e x := Prod.ext (e.coe_fst ex).symm rfl theorem mk_proj_snd' (ex : proj x ∈ e.baseSet) : (proj x, (e x).2) = e x := Prod.ext (e.coe_fst' ex).symm rfl /-- Composition of inverse and coercion from the subtype of the target. -/ def setSymm : e.target → Z := e.target.restrict e.toPartialEquiv.symm theorem mem_target {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.baseSet := by rw [e.target_eq, prod_univ, mem_preimage] theorem proj_symm_apply {x : B × F} (hx : x ∈ e.target) : proj (e.toPartialEquiv.symm x) = x.1 := by have := (e.coe_fst (e.map_target hx)).symm rwa [← e.coe_coe, e.right_inv hx] at this theorem proj_symm_apply' {b : B} {x : F} (hx : b ∈ e.baseSet) : proj (e.toPartialEquiv.symm (b, x)) = b := e.proj_symm_apply (e.mem_target.2 hx) theorem proj_surjOn_baseSet [Nonempty F] : Set.SurjOn proj e.source e.baseSet := fun b hb => let ⟨y⟩ := ‹Nonempty F› ⟨e.toPartialEquiv.symm (b, y), e.toPartialEquiv.map_target <| e.mem_target.2 hb, e.proj_symm_apply' hb⟩ theorem apply_symm_apply {x : B × F} (hx : x ∈ e.target) : e (e.toPartialEquiv.symm x) = x := e.toPartialEquiv.right_inv hx theorem apply_symm_apply' {b : B} {x : F} (hx : b ∈ e.baseSet) : e (e.toPartialEquiv.symm (b, x)) = (b, x) := e.apply_symm_apply (e.mem_target.2 hx) theorem symm_apply_apply {x : Z} (hx : x ∈ e.source) : e.toPartialEquiv.symm (e x) = x := e.toPartialEquiv.left_inv hx @[simp, mfld_simps] theorem symm_apply_mk_proj {x : Z} (ex : x ∈ e.source) : e.toPartialEquiv.symm (proj x, (e x).2) = x := by rw [← e.coe_fst ex, ← e.coe_coe, e.left_inv ex] @[simp, mfld_simps] theorem preimage_symm_proj_baseSet : e.toPartialEquiv.symm ⁻¹' (proj ⁻¹' e.baseSet) ∩ e.target = e.target := by refine inter_eq_right.mpr fun x hx => ?_ simp only [mem_preimage, PartialEquiv.invFun_as_coe, e.proj_symm_apply hx] exact e.mem_target.mp hx @[simp, mfld_simps] theorem preimage_symm_proj_inter (s : Set B) : e.toPartialEquiv.symm ⁻¹' (proj ⁻¹' s) ∩ e.baseSet ×ˢ univ = (s ∩ e.baseSet) ×ˢ univ := by ext ⟨x, y⟩ suffices x ∈ e.baseSet → (proj (e.toPartialEquiv.symm (x, y)) ∈ s ↔ x ∈ s) by simpa only [prodMk_mem_set_prod_eq, mem_inter_iff, and_true, mem_univ, and_congr_left_iff] intro h rw [e.proj_symm_apply' h] theorem target_inter_preimage_symm_source_eq (e f : Pretrivialization F proj) : f.target ∩ f.toPartialEquiv.symm ⁻¹' e.source = (e.baseSet ∩ f.baseSet) ×ˢ univ := by rw [inter_comm, f.target_eq, e.source_eq, f.preimage_symm_proj_inter] theorem trans_source (e f : Pretrivialization F proj) : (f.toPartialEquiv.symm.trans e.toPartialEquiv).source = (e.baseSet ∩ f.baseSet) ×ˢ univ := by rw [PartialEquiv.trans_source, PartialEquiv.symm_source, e.target_inter_preimage_symm_source_eq] theorem symm_trans_symm (e e' : Pretrivialization F proj) : (e.toPartialEquiv.symm.trans e'.toPartialEquiv).symm = e'.toPartialEquiv.symm.trans e.toPartialEquiv := by rw [PartialEquiv.trans_symm_eq_symm_trans_symm, PartialEquiv.symm_symm] theorem symm_trans_source_eq (e e' : Pretrivialization F proj) : (e.toPartialEquiv.symm.trans e'.toPartialEquiv).source = (e.baseSet ∩ e'.baseSet) ×ˢ univ := by rw [PartialEquiv.trans_source, e'.source_eq, PartialEquiv.symm_source, e.target_eq, inter_comm, e.preimage_symm_proj_inter, inter_comm] theorem symm_trans_target_eq (e e' : Pretrivialization F proj) : (e.toPartialEquiv.symm.trans e'.toPartialEquiv).target = (e.baseSet ∩ e'.baseSet) ×ˢ univ := by rw [← PartialEquiv.symm_source, symm_trans_symm, symm_trans_source_eq, inter_comm] variable (e' : Pretrivialization F (π F E)) {b : B} {y : E b} @[simp] theorem coe_mem_source : ↑y ∈ e'.source ↔ b ∈ e'.baseSet := e'.mem_source
@[simp, mfld_simps] theorem coe_coe_fst (hb : b ∈ e'.baseSet) : (e' y).1 = b :=
Mathlib/Topology/FiberBundle/Trivialization.lean
203
205
/- Copyright (c) 2023 Luke Mantle. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Mantle, Jake Levinson -/ import Mathlib.RingTheory.Polynomial.Hermite.Basic import Mathlib.Analysis.Calculus.Deriv.Add import Mathlib.Analysis.Calculus.Deriv.Polynomial import Mathlib.Analysis.SpecialFunctions.Exp import Mathlib.Analysis.SpecialFunctions.ExpDeriv /-! # Hermite polynomials and Gaussians This file shows that the Hermite polynomial `hermite n` is (up to sign) the polynomial factor occurring in the `n`th derivative of a gaussian. ## Results * `Polynomial.deriv_gaussian_eq_hermite_mul_gaussian`: The Hermite polynomial is (up to sign) the polynomial factor occurring in the `n`th derivative of a gaussian. ## References * [Hermite Polynomials](https://en.wikipedia.org/wiki/Hermite_polynomials) -/ noncomputable section open Polynomial namespace Polynomial /-- `hermite n` is (up to sign) the factor appearing in `deriv^[n]` of a gaussian -/ theorem deriv_gaussian_eq_hermite_mul_gaussian (n : ℕ) (x : ℝ) : deriv^[n] (fun y => Real.exp (-(y ^ 2 / 2))) x = (-1 : ℝ) ^ n * aeval x (hermite n) * Real.exp (-(x ^ 2 / 2)) := by rw [mul_assoc] induction' n with n ih generalizing x · rw [Function.iterate_zero_apply, pow_zero, one_mul, hermite_zero, C_1, map_one, one_mul] · replace ih : deriv^[n] _ = _ := _root_.funext ih have deriv_gaussian : deriv (fun y => Real.exp (-(y ^ 2 / 2))) x = -x * Real.exp (-(x ^ 2 / 2)) := by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/10745): was `simp [mul_comm, ← neg_mul]` rw [deriv_exp (by simp)]; simp; ring rw [Function.iterate_succ_apply', ih, deriv_const_mul_field, deriv_mul, pow_succ (-1 : ℝ), deriv_gaussian, hermite_succ, map_sub, map_mul, aeval_X, Polynomial.deriv_aeval] · ring · apply Polynomial.differentiable_aeval · apply DifferentiableAt.exp; simp -- Porting note: was just `simp` theorem hermite_eq_deriv_gaussian (n : ℕ) (x : ℝ) : aeval x (hermite n) = (-1 : ℝ) ^ n * deriv^[n] (fun y => Real.exp (-(y ^ 2 / 2))) x / Real.exp (-(x ^ 2 / 2)) := by rw [deriv_gaussian_eq_hermite_mul_gaussian]
field_simp [Real.exp_ne_zero] rw [← @smul_eq_mul ℝ _ ((-1) ^ n), ← inv_smul_eq_iff₀, mul_assoc, smul_eq_mul, ← inv_pow, ← neg_inv, inv_one] exact pow_ne_zero _ (by norm_num) theorem hermite_eq_deriv_gaussian' (n : ℕ) (x : ℝ) : aeval x (hermite n) = (-1 : ℝ) ^ n * deriv^[n] (fun y => Real.exp (-(y ^ 2 / 2))) x * Real.exp (x ^ 2 / 2) := by
Mathlib/RingTheory/Polynomial/Hermite/Gaussian.lean
58
64
/- 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] split_ifs with h · apply ENNReal.rpow_lt_top_of_nonneg _ ENNReal.ofNat_ne_top rw [one_div, sub_nonneg, ← ENNReal.toReal_inv, ← ENNReal.toReal_one] exact ENNReal.toReal_mono (by simpa using h.1.ne') (ENNReal.one_le_inv.2 h.2.le) · exact ENNReal.one_lt_top theorem eLpNorm_add_le' (hf : AEStronglyMeasurable f μ) (hg : AEStronglyMeasurable g μ) (p : ℝ≥0∞) : eLpNorm (f + g) p μ ≤ LpAddConst p * (eLpNorm f p μ + eLpNorm g p μ) := by rcases eq_or_ne p 0 with (rfl | hp) · simp only [eLpNorm_exponent_zero, add_zero, mul_zero, le_zero_iff] rcases lt_or_le p 1 with (h'p | h'p) · simp only [eLpNorm_eq_eLpNorm' hp (h'p.trans ENNReal.one_lt_top).ne] convert eLpNorm'_add_le_of_le_one hf ENNReal.toReal_nonneg _ · have : p ∈ Set.Ioo (0 : ℝ≥0∞) 1 := ⟨hp.bot_lt, h'p⟩ simp only [LpAddConst, if_pos this] · simpa using ENNReal.toReal_mono ENNReal.one_ne_top h'p.le · simpa [LpAddConst_of_one_le h'p] using eLpNorm_add_le hf hg h'p variable (μ E) /-- Technical lemma to control the addition of functions in `L^p` even for `p < 1`: Given `δ > 0`, there exists `η` such that two functions bounded by `η` in `L^p` have a sum bounded by `δ`. One could take `η = δ / 2` for `p ≥ 1`, but the point of the lemma is that it works also for `p < 1`. -/ theorem exists_Lp_half (p : ℝ≥0∞) {δ : ℝ≥0∞} (hδ : δ ≠ 0) : ∃ η : ℝ≥0∞, 0 < η ∧ ∀ (f g : α → E), AEStronglyMeasurable f μ → AEStronglyMeasurable g μ → eLpNorm f p μ ≤ η → eLpNorm g p μ ≤ η → eLpNorm (f + g) p μ < δ := by have : Tendsto (fun η : ℝ≥0∞ => LpAddConst p * (η + η)) (𝓝[>] 0) (𝓝 (LpAddConst p * (0 + 0))) := (ENNReal.Tendsto.const_mul (tendsto_id.add tendsto_id) (Or.inr (LpAddConst_lt_top p).ne)).mono_left nhdsWithin_le_nhds simp only [add_zero, mul_zero] at this rcases (((tendsto_order.1 this).2 δ hδ.bot_lt).and self_mem_nhdsWithin).exists with ⟨η, hη, ηpos⟩ refine ⟨η, ηpos, fun f g hf hg Hf Hg => ?_⟩ calc eLpNorm (f + g) p μ ≤ LpAddConst p * (eLpNorm f p μ + eLpNorm g p μ) := eLpNorm_add_le' hf hg p _ ≤ LpAddConst p * (η + η) := by gcongr _ < δ := hη variable {μ E} theorem eLpNorm_sub_le' (hf : AEStronglyMeasurable f μ) (hg : AEStronglyMeasurable g μ) (p : ℝ≥0∞) : eLpNorm (f - g) p μ ≤ LpAddConst p * (eLpNorm f p μ + eLpNorm g p μ) := by simpa only [sub_eq_add_neg, eLpNorm_neg] using eLpNorm_add_le' hf hg.neg p theorem eLpNorm_sub_le {f g : α → E} (hf : AEStronglyMeasurable f μ) (hg : AEStronglyMeasurable g μ) (hp : 1 ≤ p) : eLpNorm (f - g) p μ ≤ eLpNorm f p μ + eLpNorm g p μ := by simpa [LpAddConst_of_one_le hp] using eLpNorm_sub_le' hf hg p theorem eLpNorm_add_lt_top {f g : α → E} (hf : MemLp f p μ) (hg : MemLp g p μ) : eLpNorm (f + g) p μ < ∞ := calc eLpNorm (f + g) p μ ≤ LpAddConst p * (eLpNorm f p μ + eLpNorm g p μ) := eLpNorm_add_le' hf.aestronglyMeasurable hg.aestronglyMeasurable p _ < ∞ := by apply ENNReal.mul_lt_top (LpAddConst_lt_top p) exact ENNReal.add_lt_top.2 ⟨hf.2, hg.2⟩ theorem eLpNorm'_sum_le {ι} {f : ι → α → E} {s : Finset ι} (hfs : ∀ i, i ∈ s → AEStronglyMeasurable (f i) μ) (hq1 : 1 ≤ q) : eLpNorm' (∑ i ∈ s, f i) q μ ≤ ∑ i ∈ s, eLpNorm' (f i) q μ := Finset.le_sum_of_subadditive_on_pred (fun f : α → E => eLpNorm' f q μ) (fun f => AEStronglyMeasurable f μ) (eLpNorm'_zero (zero_lt_one.trans_le hq1)) (fun _f _g hf hg => eLpNorm'_add_le hf hg hq1) (fun _f _g hf hg => hf.add hg) _ hfs theorem eLpNorm_sum_le {ι} {f : ι → α → E} {s : Finset ι} (hfs : ∀ i, i ∈ s → AEStronglyMeasurable (f i) μ) (hp1 : 1 ≤ p) : eLpNorm (∑ i ∈ s, f i) p μ ≤ ∑ i ∈ s, eLpNorm (f i) p μ := Finset.le_sum_of_subadditive_on_pred (fun f : α → E => eLpNorm f p μ) (fun f => AEStronglyMeasurable f μ) eLpNorm_zero (fun _f _g hf hg => eLpNorm_add_le hf hg hp1) (fun _f _g hf hg => hf.add hg) _ hfs theorem MemLp.add {f g : α → E} (hf : MemLp f p μ) (hg : MemLp g p μ) : MemLp (f + g) p μ := ⟨AEStronglyMeasurable.add hf.1 hg.1, eLpNorm_add_lt_top hf hg⟩ theorem MemLp.sub {f g : α → E} (hf : MemLp f p μ) (hg : MemLp g p μ) : MemLp (f - g) p μ := by rw [sub_eq_add_neg] exact hf.add hg.neg theorem memLp_finset_sum {ι} (s : Finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, MemLp (f i) p μ) : MemLp (fun a => ∑ i ∈ s, f i a) p μ := by haveI : DecidableEq ι := Classical.decEq _ revert hf refine Finset.induction_on s ?_ ?_ · simp only [MemLp.zero', Finset.sum_empty, imp_true_iff] · intro i s his ih hf simp only [his, Finset.sum_insert, not_false_iff] exact (hf i (s.mem_insert_self i)).add (ih fun j hj => hf j (Finset.mem_insert_of_mem hj)) theorem memLp_finset_sum' {ι} (s : Finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, MemLp (f i) p μ) : MemLp (∑ i ∈ s, f i) p μ := by convert memLp_finset_sum s hf using 1 ext x simp end MeasureTheory
Mathlib/MeasureTheory/Function/LpSeminorm/TriangleInequality.lean
185
193
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Sean Leather -/ import Batteries.Data.List.Perm import Mathlib.Data.List.Pairwise import Mathlib.Data.List.Nodup import Mathlib.Data.List.Lookmap import Mathlib.Data.Sigma.Basic /-! # Utilities for lists of sigmas This file includes several ways of interacting with `List (Sigma β)`, treated as a key-value store. If `α : Type*` and `β : α → Type*`, then we regard `s : Sigma β` as having key `s.1 : α` and value `s.2 : β s.1`. Hence, `List (Sigma β)` behaves like a key-value store. ## Main Definitions - `List.keys` extracts the list of keys. - `List.NodupKeys` determines if the store has duplicate keys. - `List.lookup`/`lookup_all` accesses the value(s) of a particular key. - `List.kreplace` replaces the first value with a given key by a given value. - `List.kerase` removes a value. - `List.kinsert` inserts a value. - `List.kunion` computes the union of two stores. - `List.kextract` returns a value with a given key and the rest of the values. -/ universe u u' v v' namespace List variable {α : Type u} {α' : Type u'} {β : α → Type v} {β' : α' → Type v'} {l l₁ l₂ : List (Sigma β)} /-! ### `keys` -/ /-- List of keys from a list of key-value pairs -/ def keys : List (Sigma β) → List α := map Sigma.fst @[simp] theorem keys_nil : @keys α β [] = [] := rfl @[simp] theorem keys_cons {s} {l : List (Sigma β)} : (s :: l).keys = s.1 :: l.keys := rfl theorem mem_keys_of_mem {s : Sigma β} {l : List (Sigma β)} : s ∈ l → s.1 ∈ l.keys := mem_map_of_mem theorem exists_of_mem_keys {a} {l : List (Sigma β)} (h : a ∈ l.keys) : ∃ b : β a, Sigma.mk a b ∈ l := let ⟨⟨_, b'⟩, m, e⟩ := exists_of_mem_map h Eq.recOn e (Exists.intro b' m) theorem mem_keys {a} {l : List (Sigma β)} : a ∈ l.keys ↔ ∃ b : β a, Sigma.mk a b ∈ l := ⟨exists_of_mem_keys, fun ⟨_, h⟩ => mem_keys_of_mem h⟩ theorem not_mem_keys {a} {l : List (Sigma β)} : a ∉ l.keys ↔ ∀ b : β a, Sigma.mk a b ∉ l := (not_congr mem_keys).trans not_exists theorem ne_key {a} {l : List (Sigma β)} : a ∉ l.keys ↔ ∀ s : Sigma β, s ∈ l → a ≠ s.1 := Iff.intro (fun h₁ s h₂ e => absurd (mem_keys_of_mem h₂) (by rwa [e] at h₁)) fun f h₁ => let ⟨_, h₂⟩ := exists_of_mem_keys h₁ f _ h₂ rfl @[deprecated (since := "2025-04-27")] alias not_eq_key := ne_key /-! ### `NodupKeys` -/ /-- Determines whether the store uses a key several times. -/ def NodupKeys (l : List (Sigma β)) : Prop := l.keys.Nodup theorem nodupKeys_iff_pairwise {l} : NodupKeys l ↔ Pairwise (fun s s' : Sigma β => s.1 ≠ s'.1) l := pairwise_map theorem NodupKeys.pairwise_ne {l} (h : NodupKeys l) : Pairwise (fun s s' : Sigma β => s.1 ≠ s'.1) l := nodupKeys_iff_pairwise.1 h @[simp] theorem nodupKeys_nil : @NodupKeys α β [] := Pairwise.nil @[simp] theorem nodupKeys_cons {s : Sigma β} {l : List (Sigma β)} : NodupKeys (s :: l) ↔ s.1 ∉ l.keys ∧ NodupKeys l := by simp [keys, NodupKeys] theorem not_mem_keys_of_nodupKeys_cons {s : Sigma β} {l : List (Sigma β)} (h : NodupKeys (s :: l)) : s.1 ∉ l.keys := (nodupKeys_cons.1 h).1 theorem nodupKeys_of_nodupKeys_cons {s : Sigma β} {l : List (Sigma β)} (h : NodupKeys (s :: l)) : NodupKeys l := (nodupKeys_cons.1 h).2 theorem NodupKeys.eq_of_fst_eq {l : List (Sigma β)} (nd : NodupKeys l) {s s' : Sigma β} (h : s ∈ l) (h' : s' ∈ l) : s.1 = s'.1 → s = s' := @Pairwise.forall_of_forall _ (fun s s' : Sigma β => s.1 = s'.1 → s = s') _ (fun _ _ H h => (H h.symm).symm) (fun _ _ _ => rfl) ((nodupKeys_iff_pairwise.1 nd).imp fun h h' => (h h').elim) _ h _ h' theorem NodupKeys.eq_of_mk_mem {a : α} {b b' : β a} {l : List (Sigma β)} (nd : NodupKeys l) (h : Sigma.mk a b ∈ l) (h' : Sigma.mk a b' ∈ l) : b = b' := by cases nd.eq_of_fst_eq h h' rfl; rfl theorem nodupKeys_singleton (s : Sigma β) : NodupKeys [s] := nodup_singleton _ theorem NodupKeys.sublist {l₁ l₂ : List (Sigma β)} (h : l₁ <+ l₂) : NodupKeys l₂ → NodupKeys l₁ := Nodup.sublist <| h.map _ protected theorem NodupKeys.nodup {l : List (Sigma β)} : NodupKeys l → Nodup l := Nodup.of_map _ theorem perm_nodupKeys {l₁ l₂ : List (Sigma β)} (h : l₁ ~ l₂) : NodupKeys l₁ ↔ NodupKeys l₂ := (h.map _).nodup_iff theorem nodupKeys_flatten {L : List (List (Sigma β))} : NodupKeys (flatten L) ↔ (∀ l ∈ L, NodupKeys l) ∧ Pairwise Disjoint (L.map keys) := by rw [nodupKeys_iff_pairwise, pairwise_flatten, pairwise_map] refine and_congr (forall₂_congr fun l _ => by simp [nodupKeys_iff_pairwise]) ?_ apply iff_of_eq; congr! with (l₁ l₂) simp [keys, disjoint_iff_ne, Sigma.forall] theorem nodup_zipIdx_map_snd (l : List α) : (l.zipIdx.map Prod.snd).Nodup := by simp [List.nodup_range'] @[deprecated (since := "2025-01-28")] alias nodup_enum_map_fst := nodup_zipIdx_map_snd theorem mem_ext {l₀ l₁ : List (Sigma β)} (nd₀ : l₀.Nodup) (nd₁ : l₁.Nodup) (h : ∀ x, x ∈ l₀ ↔ x ∈ l₁) : l₀ ~ l₁ := (perm_ext_iff_of_nodup nd₀ nd₁).2 h variable [DecidableEq α] [DecidableEq α'] /-! ### `dlookup` -/ /-- `dlookup a l` is the first value in `l` corresponding to the key `a`, or `none` if no such element exists. -/ def dlookup (a : α) : List (Sigma β) → Option (β a) | [] => none | ⟨a', b⟩ :: l => if h : a' = a then some (Eq.recOn h b) else dlookup a l @[simp] theorem dlookup_nil (a : α) : dlookup a [] = @none (β a) := rfl @[simp] theorem dlookup_cons_eq (l) (a : α) (b : β a) : dlookup a (⟨a, b⟩ :: l) = some b := dif_pos rfl @[simp] theorem dlookup_cons_ne (l) {a} : ∀ s : Sigma β, a ≠ s.1 → dlookup a (s :: l) = dlookup a l | ⟨_, _⟩, h => dif_neg h.symm theorem dlookup_isSome {a : α} : ∀ {l : List (Sigma β)}, (dlookup a l).isSome ↔ a ∈ l.keys | [] => by simp | ⟨a', b⟩ :: l => by by_cases h : a = a' · subst a' simp · simp [h, dlookup_isSome] theorem dlookup_eq_none {a : α} {l : List (Sigma β)} : dlookup a l = none ↔ a ∉ l.keys := by simp [← dlookup_isSome, Option.isNone_iff_eq_none] theorem of_mem_dlookup {a : α} {b : β a} : ∀ {l : List (Sigma β)}, b ∈ dlookup a l → Sigma.mk a b ∈ l | ⟨a', b'⟩ :: l, H => by by_cases h : a = a' · subst a' simp? at H says simp only [dlookup_cons_eq, Option.mem_def, Option.some.injEq] at H simp [H] · simp only [ne_eq, h, not_false_iff, dlookup_cons_ne] at H simp [of_mem_dlookup H] theorem mem_dlookup {a} {b : β a} {l : List (Sigma β)} (nd : l.NodupKeys) (h : Sigma.mk a b ∈ l) : b ∈ dlookup a l := by obtain ⟨b', h'⟩ := Option.isSome_iff_exists.mp (dlookup_isSome.mpr (mem_keys_of_mem h)) cases nd.eq_of_mk_mem h (of_mem_dlookup h') exact h' theorem map_dlookup_eq_find (a : α) : ∀ l : List (Sigma β), (dlookup a l).map (Sigma.mk a) = find? (fun s => a = s.1) l | [] => rfl | ⟨a', b'⟩ :: l => by by_cases h : a = a' · subst a' simp · simpa [h] using map_dlookup_eq_find a l theorem mem_dlookup_iff {a : α} {b : β a} {l : List (Sigma β)} (nd : l.NodupKeys) : b ∈ dlookup a l ↔ Sigma.mk a b ∈ l := ⟨of_mem_dlookup, mem_dlookup nd⟩ theorem perm_dlookup (a : α) {l₁ l₂ : List (Sigma β)} (nd₁ : l₁.NodupKeys) (nd₂ : l₂.NodupKeys) (p : l₁ ~ l₂) : dlookup a l₁ = dlookup a l₂ := by ext b; simp only [mem_dlookup_iff nd₁, mem_dlookup_iff nd₂]; exact p.mem_iff theorem lookup_ext {l₀ l₁ : List (Sigma β)} (nd₀ : l₀.NodupKeys) (nd₁ : l₁.NodupKeys) (h : ∀ x y, y ∈ l₀.dlookup x ↔ y ∈ l₁.dlookup x) : l₀ ~ l₁ := mem_ext nd₀.nodup nd₁.nodup fun ⟨a, b⟩ => by rw [← mem_dlookup_iff, ← mem_dlookup_iff, h] <;> assumption theorem dlookup_map (l : List (Sigma β)) {f : α → α'} (hf : Function.Injective f) (g : ∀ a, β a → β' (f a)) (a : α) : (l.map fun x => ⟨f x.1, g _ x.2⟩).dlookup (f a) = (l.dlookup a).map (g a) := by induction' l with b l IH · rw [map_nil, dlookup_nil, dlookup_nil, Option.map_none'] · rw [map_cons] obtain rfl | h := eq_or_ne a b.1 · rw [dlookup_cons_eq, dlookup_cons_eq, Option.map_some'] · rw [dlookup_cons_ne _ _ h, dlookup_cons_ne _ _ (fun he => h <| hf he), IH] theorem dlookup_map₁ {β : Type v} (l : List (Σ _ : α, β)) {f : α → α'} (hf : Function.Injective f) (a : α) : (l.map fun x => ⟨f x.1, x.2⟩ : List (Σ _ : α', β)).dlookup (f a) = l.dlookup a := by rw [dlookup_map (β' := fun _ => β) l hf (fun _ x => x) a, Option.map_id'] theorem dlookup_map₂ {γ δ : α → Type*} {l : List (Σ a, γ a)} {f : ∀ a, γ a → δ a} (a : α) : (l.map fun x => ⟨x.1, f _ x.2⟩ : List (Σ a, δ a)).dlookup a = (l.dlookup a).map (f a) := dlookup_map l Function.injective_id _ _ /-! ### `lookupAll` -/ /-- `lookup_all a l` is the list of all values in `l` corresponding to the key `a`. -/ def lookupAll (a : α) : List (Sigma β) → List (β a) | [] => [] | ⟨a', b⟩ :: l => if h : a' = a then Eq.recOn h b :: lookupAll a l else lookupAll a l @[simp] theorem lookupAll_nil (a : α) : lookupAll a [] = @nil (β a) := rfl @[simp] theorem lookupAll_cons_eq (l) (a : α) (b : β a) : lookupAll a (⟨a, b⟩ :: l) = b :: lookupAll a l := dif_pos rfl @[simp] theorem lookupAll_cons_ne (l) {a} : ∀ s : Sigma β, a ≠ s.1 → lookupAll a (s :: l) = lookupAll a l | ⟨_, _⟩, h => dif_neg h.symm theorem lookupAll_eq_nil {a : α} : ∀ {l : List (Sigma β)}, lookupAll a l = [] ↔ ∀ b : β a, Sigma.mk a b ∉ l | [] => by simp | ⟨a', b⟩ :: l => by by_cases h : a = a' · subst a' simp only [lookupAll_cons_eq, mem_cons, Sigma.mk.inj_iff, heq_eq_eq, true_and, not_or, false_iff, not_forall, not_and, not_not, reduceCtorEq] use b simp · simp [h, lookupAll_eq_nil] theorem head?_lookupAll (a : α) : ∀ l : List (Sigma β), head? (lookupAll a l) = dlookup a l | [] => by simp | ⟨a', b⟩ :: l => by by_cases h : a = a' · subst h; simp · rw [lookupAll_cons_ne, dlookup_cons_ne, head?_lookupAll a l] <;> assumption theorem mem_lookupAll {a : α} {b : β a} : ∀ {l : List (Sigma β)}, b ∈ lookupAll a l ↔ Sigma.mk a b ∈ l | [] => by simp | ⟨a', b'⟩ :: l => by by_cases h : a = a' · subst h simp [*, mem_lookupAll] · simp [*, mem_lookupAll] theorem lookupAll_sublist (a : α) : ∀ l : List (Sigma β), (lookupAll a l).map (Sigma.mk a) <+ l | [] => by simp | ⟨a', b'⟩ :: l => by by_cases h : a = a' · subst h simp only [ne_eq, not_true, lookupAll_cons_eq, List.map] exact (lookupAll_sublist a l).cons₂ _ · simp only [ne_eq, h, not_false_iff, lookupAll_cons_ne] exact (lookupAll_sublist a l).cons _ theorem lookupAll_length_le_one (a : α) {l : List (Sigma β)} (h : l.NodupKeys) : length (lookupAll a l) ≤ 1 := by have := Nodup.sublist ((lookupAll_sublist a l).map _) h rw [map_map] at this rwa [← nodup_replicate, ← map_const] theorem lookupAll_eq_dlookup (a : α) {l : List (Sigma β)} (h : l.NodupKeys) : lookupAll a l = (dlookup a l).toList := by rw [← head?_lookupAll] have h1 := lookupAll_length_le_one a h; revert h1 rcases lookupAll a l with (_ | ⟨b, _ | ⟨c, l⟩⟩) <;> intro h1 <;> try rfl exact absurd h1 (by simp) theorem lookupAll_nodup (a : α) {l : List (Sigma β)} (h : l.NodupKeys) : (lookupAll a l).Nodup := by (rw [lookupAll_eq_dlookup a h]; apply Option.toList_nodup) theorem perm_lookupAll (a : α) {l₁ l₂ : List (Sigma β)} (nd₁ : l₁.NodupKeys) (nd₂ : l₂.NodupKeys) (p : l₁ ~ l₂) : lookupAll a l₁ = lookupAll a l₂ := by simp [lookupAll_eq_dlookup, nd₁, nd₂, perm_dlookup a nd₁ nd₂ p] theorem dlookup_append (l₁ l₂ : List (Sigma β)) (a : α) : (l₁ ++ l₂).dlookup a = (l₁.dlookup a).or (l₂.dlookup a) := by induction l₁ with | nil => rfl | cons x l₁ IH => rw [cons_append] obtain rfl | hb := Decidable.eq_or_ne a x.1 · rw [dlookup_cons_eq, dlookup_cons_eq, Option.or] · rw [dlookup_cons_ne _ _ hb, dlookup_cons_ne _ _ hb, IH] /-! ### `kreplace` -/ /-- Replaces the first value with key `a` by `b`. -/ def kreplace (a : α) (b : β a) : List (Sigma β) → List (Sigma β) := lookmap fun s => if a = s.1 then some ⟨a, b⟩ else none theorem kreplace_of_forall_not (a : α) (b : β a) {l : List (Sigma β)} (H : ∀ b : β a, Sigma.mk a b ∉ l) : kreplace a b l = l := lookmap_of_forall_not _ <| by rintro ⟨a', b'⟩ h; dsimp; split_ifs · subst a' exact H _ h · rfl theorem kreplace_self {a : α} {b : β a} {l : List (Sigma β)} (nd : NodupKeys l) (h : Sigma.mk a b ∈ l) : kreplace a b l = l := by refine (lookmap_congr ?_).trans (lookmap_id' (Option.guard fun (s : Sigma β) => a = s.1) ?_ _) · rintro ⟨a', b'⟩ h' dsimp [Option.guard] split_ifs · subst a' simp [nd.eq_of_mk_mem h h'] · rfl · rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ dsimp [Option.guard] split_ifs · simp · rintro ⟨⟩ theorem keys_kreplace (a : α) (b : β a) : ∀ l : List (Sigma β), (kreplace a b l).keys = l.keys := lookmap_map_eq _ _ <| by rintro ⟨a₁, b₂⟩ ⟨a₂, b₂⟩ dsimp split_ifs with h <;> simp +contextual [h] theorem kreplace_nodupKeys (a : α) (b : β a) {l : List (Sigma β)} : (kreplace a b l).NodupKeys ↔ l.NodupKeys := by simp [NodupKeys, keys_kreplace] theorem Perm.kreplace {a : α} {b : β a} {l₁ l₂ : List (Sigma β)} (nd : l₁.NodupKeys) : l₁ ~ l₂ → kreplace a b l₁ ~ kreplace a b l₂ := perm_lookmap _ <| by refine nd.pairwise_ne.imp ?_ intro x y h z h₁ w h₂ split_ifs at h₁ h₂ with h_2 h_1 <;> cases h₁ <;> cases h₂ exact (h (h_2.symm.trans h_1)).elim /-! ### `kerase` -/ /-- Remove the first pair with the key `a`. -/ def kerase (a : α) : List (Sigma β) → List (Sigma β) := eraseP fun s => a = s.1 @[simp] theorem kerase_nil {a} : @kerase _ β _ a [] = [] := rfl @[simp] theorem kerase_cons_eq {a} {s : Sigma β} {l : List (Sigma β)} (h : a = s.1) : kerase a (s :: l) = l := by simp [kerase, h] @[simp] theorem kerase_cons_ne {a} {s : Sigma β} {l : List (Sigma β)} (h : a ≠ s.1) : kerase a (s :: l) = s :: kerase a l := by simp [kerase, h] @[simp] theorem kerase_of_not_mem_keys {a} {l : List (Sigma β)} (h : a ∉ l.keys) : kerase a l = l := by induction l with | nil => rfl | cons _ _ ih => simp [not_or] at h; simp [h.1, ih h.2] theorem kerase_sublist (a : α) (l : List (Sigma β)) : kerase a l <+ l := eraseP_sublist theorem kerase_keys_subset (a) (l : List (Sigma β)) : (kerase a l).keys ⊆ l.keys := ((kerase_sublist a l).map _).subset theorem mem_keys_of_mem_keys_kerase {a₁ a₂} {l : List (Sigma β)} : a₁ ∈ (kerase a₂ l).keys → a₁ ∈ l.keys := @kerase_keys_subset _ _ _ _ _ _ theorem exists_of_kerase {a : α} {l : List (Sigma β)} (h : a ∈ l.keys) : ∃ (b : β a) (l₁ l₂ : List (Sigma β)), a ∉ l₁.keys ∧ l = l₁ ++ ⟨a, b⟩ :: l₂ ∧ kerase a l = l₁ ++ l₂ := by induction l with | nil => cases h | cons hd tl ih => by_cases e : a = hd.1 · subst e exact ⟨hd.2, [], tl, by simp, by cases hd; rfl, by simp⟩ · simp only [keys_cons, mem_cons] at h rcases h with h | h · exact absurd h e rcases ih h with ⟨b, tl₁, tl₂, h₁, h₂, h₃⟩ exact ⟨b, hd :: tl₁, tl₂, not_mem_cons_of_ne_of_not_mem e h₁, by (rw [h₂]; rfl), by simp [e, h₃]⟩ @[simp] theorem mem_keys_kerase_of_ne {a₁ a₂} {l : List (Sigma β)} (h : a₁ ≠ a₂) : a₁ ∈ (kerase a₂ l).keys ↔ a₁ ∈ l.keys := (Iff.intro mem_keys_of_mem_keys_kerase) fun p => if q : a₂ ∈ l.keys then match l, kerase a₂ l, exists_of_kerase q, p with | _, _, ⟨_, _, _, _, rfl, rfl⟩, p => by simpa [keys, h] using p else by simp [q, p] theorem keys_kerase {a} {l : List (Sigma β)} : (kerase a l).keys = l.keys.erase a := by
rw [keys, kerase, erase_eq_eraseP, eraseP_map, Function.comp_def] congr theorem kerase_kerase {a a'} {l : List (Sigma β)} : (kerase a' l).kerase a = (kerase a l).kerase a' := by by_cases h : a = a' · subst a'; rfl induction' l with x xs · rfl · by_cases a' = x.1 · subst a' simp [kerase_cons_ne h, kerase_cons_eq rfl] by_cases h' : a = x.1 · subst a simp [kerase_cons_eq rfl, kerase_cons_ne (Ne.symm h)]
Mathlib/Data/List/Sigma.lean
429
443
/- 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.Order.Group.Unbundled.Int import Mathlib.Algebra.Order.Nonneg.Basic import Mathlib.Algebra.Order.Ring.Unbundled.Rat import Mathlib.Algebra.Ring.Rat import Mathlib.Data.Set.Operations import Mathlib.Order.Bounds.Defs import Mathlib.Order.GaloisConnection.Defs /-! # Nonnegative rationals This file defines the nonnegative rationals as a subtype of `Rat` and provides its basic algebraic order structure. Note that `NNRat` is not declared as a `Semifield` here. See `Mathlib.Algebra.Field.Rat` for that instance. We also define an instance `CanLift ℚ ℚ≥0`. This instance can be used by the `lift` tactic to replace `x : ℚ` and `hx : 0 ≤ x` in the proof context with `x : ℚ≥0` while replacing all occurrences of `x` with `↑x`. This tactic also works for a function `f : α → ℚ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notation `ℚ≥0` is notation for `NNRat` in locale `NNRat`. ## Huge warning Whenever you state a lemma about the coercion `ℚ≥0 → ℚ`, check that Lean inserts `NNRat.cast`, not `Subtype.val`. Else your lemma will never apply. -/ assert_not_exists CompleteLattice OrderedCommMonoid library_note "specialised high priority simp lemma" /-- It sometimes happens that a `@[simp]` lemma declared early in the library can be proved by `simp` using later, more general simp lemmas. In that case, the following reasons might be arguments for the early lemma to be tagged `@[simp high]` (rather than `@[simp, nolint simpNF]` or un``@[simp]``ed): 1. There is a significant portion of the library which needs the early lemma to be available via `simp` and which doesn't have access to the more general lemmas. 2. The more general lemmas have more complicated typeclass assumptions, causing rewrites with them to be slower. -/ open Function instance Rat.instZeroLEOneClass : ZeroLEOneClass ℚ where zero_le_one := rfl instance Rat.instPosMulMono : PosMulMono ℚ where elim := fun r p q h => by simp only [mul_comm] simpa [sub_mul, sub_nonneg] using Rat.mul_nonneg (sub_nonneg.2 h) r.2 deriving instance CommSemiring for NNRat deriving instance LinearOrder for NNRat deriving instance Sub for NNRat deriving instance Inhabited for NNRat namespace NNRat variable {p q : ℚ≥0} instance instNontrivial : Nontrivial ℚ≥0 where exists_pair_ne := ⟨1, 0, by decide⟩ instance instOrderBot : OrderBot ℚ≥0 where bot := 0 bot_le q := q.2 @[simp] lemma val_eq_cast (q : ℚ≥0) : q.1 = q := rfl instance instCharZero : CharZero ℚ≥0 where cast_injective a b hab := by simpa using congr_arg num hab instance canLift : CanLift ℚ ℚ≥0 (↑) fun q ↦ 0 ≤ q where prf q hq := ⟨⟨q, hq⟩, rfl⟩ @[ext] theorem ext : (p : ℚ) = (q : ℚ) → p = q := Subtype.ext protected theorem coe_injective : Injective ((↑) : ℚ≥0 → ℚ) := Subtype.coe_injective -- See note [specialised high priority simp lemma] @[simp high, norm_cast] theorem coe_inj : (p : ℚ) = q ↔ p = q := Subtype.coe_inj theorem ne_iff {x y : ℚ≥0} : (x : ℚ) ≠ (y : ℚ) ↔ x ≠ y := NNRat.coe_inj.not -- TODO: We have to write `NNRat.cast` explicitly, else the statement picks up `Subtype.val` instead @[simp, norm_cast] lemma coe_mk (q : ℚ) (hq) : NNRat.cast ⟨q, hq⟩ = q := rfl lemma «forall» {p : ℚ≥0 → Prop} : (∀ q, p q) ↔ ∀ q hq, p ⟨q, hq⟩ := Subtype.forall lemma «exists» {p : ℚ≥0 → Prop} : (∃ q, p q) ↔ ∃ q hq, p ⟨q, hq⟩ := Subtype.exists /-- Reinterpret a rational number `q` as a non-negative rational number. Returns `0` if `q ≤ 0`. -/ def _root_.Rat.toNNRat (q : ℚ) : ℚ≥0 := ⟨max q 0, le_max_right _ _⟩ theorem _root_.Rat.coe_toNNRat (q : ℚ) (hq : 0 ≤ q) : (q.toNNRat : ℚ) = q := max_eq_left hq theorem _root_.Rat.le_coe_toNNRat (q : ℚ) : q ≤ q.toNNRat := le_max_left _ _ open Rat (toNNRat) @[simp] theorem coe_nonneg (q : ℚ≥0) : (0 : ℚ) ≤ q := q.2 @[simp, norm_cast] lemma coe_zero : ((0 : ℚ≥0) : ℚ) = 0 := rfl @[simp] lemma num_zero : num 0 = 0 := rfl @[simp] lemma den_zero : den 0 = 1 := rfl @[simp, norm_cast] lemma coe_one : ((1 : ℚ≥0) : ℚ) = 1 := rfl @[simp] lemma num_one : num 1 = 1 := rfl @[simp] lemma den_one : den 1 = 1 := rfl @[simp, norm_cast] theorem coe_add (p q : ℚ≥0) : ((p + q : ℚ≥0) : ℚ) = p + q := rfl @[simp, norm_cast] theorem coe_mul (p q : ℚ≥0) : ((p * q : ℚ≥0) : ℚ) = p * q := rfl @[simp, norm_cast] lemma coe_pow (q : ℚ≥0) (n : ℕ) : (↑(q ^ n) : ℚ) = (q : ℚ) ^ n := rfl @[simp] lemma num_pow (q : ℚ≥0) (n : ℕ) : (q ^ n).num = q.num ^ n := by simp [num, Int.natAbs_pow] @[simp] lemma den_pow (q : ℚ≥0) (n : ℕ) : (q ^ n).den = q.den ^ n := rfl @[simp, norm_cast] theorem coe_sub (h : q ≤ p) : ((p - q : ℚ≥0) : ℚ) = p - q := max_eq_left <| le_sub_comm.2 <| by rwa [sub_zero] -- See note [specialised high priority simp lemma] @[simp high] theorem coe_eq_zero : (q : ℚ) = 0 ↔ q = 0 := by norm_cast theorem coe_ne_zero : (q : ℚ) ≠ 0 ↔ q ≠ 0 := coe_eq_zero.not @[norm_cast] theorem coe_le_coe : (p : ℚ) ≤ q ↔ p ≤ q := Iff.rfl @[norm_cast] theorem coe_lt_coe : (p : ℚ) < q ↔ p < q := Iff.rfl @[norm_cast] theorem coe_pos : (0 : ℚ) < q ↔ 0 < q := Iff.rfl theorem coe_mono : Monotone ((↑) : ℚ≥0 → ℚ) := fun _ _ ↦ coe_le_coe.2 theorem toNNRat_mono : Monotone toNNRat := fun _ _ h ↦ max_le_max h le_rfl @[simp] theorem toNNRat_coe (q : ℚ≥0) : toNNRat q = q := ext <| max_eq_left q.2 @[simp] theorem toNNRat_coe_nat (n : ℕ) : toNNRat n = n := ext <| by simp only [Nat.cast_nonneg', Rat.coe_toNNRat]; rfl /-- `toNNRat` and `(↑) : ℚ≥0 → ℚ` form a Galois insertion. -/ protected def gi : GaloisInsertion toNNRat (↑) := GaloisInsertion.monotoneIntro coe_mono toNNRat_mono Rat.le_coe_toNNRat toNNRat_coe /-- Coercion `ℚ≥0 → ℚ` as a `RingHom`. -/ def coeHom : ℚ≥0 →+* ℚ where toFun := (↑) map_one' := coe_one map_mul' := coe_mul map_zero' := coe_zero map_add' := coe_add @[simp, norm_cast] lemma coe_natCast (n : ℕ) : (↑(↑n : ℚ≥0) : ℚ) = n := rfl @[simp] theorem mk_natCast (n : ℕ) : @Eq ℚ≥0 (⟨(n : ℚ), Nat.cast_nonneg' n⟩ : ℚ≥0) n := rfl @[simp] theorem coe_coeHom : ⇑coeHom = ((↑) : ℚ≥0 → ℚ) := rfl @[norm_cast] theorem nsmul_coe (q : ℚ≥0) (n : ℕ) : ↑(n • q) = n • (q : ℚ) := coeHom.toAddMonoidHom.map_nsmul _ _ theorem bddAbove_coe {s : Set ℚ≥0} : BddAbove ((↑) '' s : Set ℚ) ↔ BddAbove s := ⟨fun ⟨b, hb⟩ ↦ ⟨toNNRat b, fun ⟨y, _⟩ hys ↦ show y ≤ max b 0 from (hb <| Set.mem_image_of_mem _ hys).trans <| le_max_left _ _⟩, fun ⟨b, hb⟩ ↦ ⟨b, fun _ ⟨_, hx, Eq⟩ ↦ Eq ▸ hb hx⟩⟩ theorem bddBelow_coe (s : Set ℚ≥0) : BddBelow (((↑) : ℚ≥0 → ℚ) '' s) := ⟨0, fun _ ⟨q, _, h⟩ ↦ h ▸ q.2⟩ @[norm_cast] theorem coe_max (x y : ℚ≥0) : ((max x y : ℚ≥0) : ℚ) = max (x : ℚ) (y : ℚ) := coe_mono.map_max @[norm_cast] theorem coe_min (x y : ℚ≥0) : ((min x y : ℚ≥0) : ℚ) = min (x : ℚ) (y : ℚ) := coe_mono.map_min theorem sub_def (p q : ℚ≥0) : p - q = toNNRat (p - q) := rfl @[simp] theorem abs_coe (q : ℚ≥0) : |(q : ℚ)| = q := abs_of_nonneg q.2 -- See note [specialised high priority simp lemma] @[simp high] theorem nonpos_iff_eq_zero (q : ℚ≥0) : q ≤ 0 ↔ q = 0 := ⟨fun h => le_antisymm h q.2, fun h => h.symm ▸ q.2⟩ end NNRat open NNRat namespace Rat variable {p q : ℚ} @[simp] theorem toNNRat_zero : toNNRat 0 = 0 := rfl @[simp] theorem toNNRat_one : toNNRat 1 = 1 := rfl @[simp] theorem toNNRat_pos : 0 < toNNRat q ↔ 0 < q := by simp [toNNRat, ← coe_lt_coe] @[simp] theorem toNNRat_eq_zero : toNNRat q = 0 ↔ q ≤ 0 := by simpa [-toNNRat_pos] using (@toNNRat_pos q).not alias ⟨_, toNNRat_of_nonpos⟩ := toNNRat_eq_zero @[simp] theorem toNNRat_le_toNNRat_iff (hp : 0 ≤ p) : toNNRat q ≤ toNNRat p ↔ q ≤ p := by simp [← coe_le_coe, toNNRat, hp] @[simp] theorem toNNRat_lt_toNNRat_iff' : toNNRat q < toNNRat p ↔ q < p ∧ 0 < p := by simp [← coe_lt_coe, toNNRat, lt_irrefl] theorem toNNRat_lt_toNNRat_iff (h : 0 < p) : toNNRat q < toNNRat p ↔ q < p := toNNRat_lt_toNNRat_iff'.trans (and_iff_left h) theorem toNNRat_lt_toNNRat_iff_of_nonneg (hq : 0 ≤ q) : toNNRat q < toNNRat p ↔ q < p := toNNRat_lt_toNNRat_iff'.trans ⟨And.left, fun h ↦ ⟨h, hq.trans_lt h⟩⟩ @[simp] theorem toNNRat_add (hq : 0 ≤ q) (hp : 0 ≤ p) : toNNRat (q + p) = toNNRat q + toNNRat p := NNRat.ext <| by simp [toNNRat, hq, hp, add_nonneg] theorem toNNRat_add_le : toNNRat (q + p) ≤ toNNRat q + toNNRat p := coe_le_coe.1 <| max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) <| coe_nonneg _ theorem toNNRat_le_iff_le_coe {p : ℚ≥0} : toNNRat q ≤ p ↔ q ≤ ↑p := NNRat.gi.gc q p theorem le_toNNRat_iff_coe_le {q : ℚ≥0} (hp : 0 ≤ p) : q ≤ toNNRat p ↔ ↑q ≤ p := by rw [← coe_le_coe, Rat.coe_toNNRat p hp] theorem le_toNNRat_iff_coe_le' {q : ℚ≥0} (hq : 0 < q) : q ≤ toNNRat p ↔ ↑q ≤ p := (le_or_lt 0 p).elim le_toNNRat_iff_coe_le fun hp ↦ by simp only [(hp.trans_le q.coe_nonneg).not_le, toNNRat_eq_zero.2 hp.le, hq.not_le] theorem toNNRat_lt_iff_lt_coe {p : ℚ≥0} (hq : 0 ≤ q) : toNNRat q < p ↔ q < ↑p := by rw [← coe_lt_coe, Rat.coe_toNNRat q hq] theorem lt_toNNRat_iff_coe_lt {q : ℚ≥0} : q < toNNRat p ↔ ↑q < p := NNRat.gi.gc.lt_iff_lt theorem toNNRat_mul (hp : 0 ≤ p) : toNNRat (p * q) = toNNRat p * toNNRat q := by rcases le_total 0 q with hq | hq · ext; simp [toNNRat, hp, hq, max_eq_left, mul_nonneg] · have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq rw [toNNRat_eq_zero.2 hq, toNNRat_eq_zero.2 hpq, mul_zero] end Rat /-- The absolute value on `ℚ` as a map to `ℚ≥0`. -/ @[pp_nodot] def Rat.nnabs (x : ℚ) : ℚ≥0 := ⟨abs x, abs_nonneg x⟩ @[norm_cast, simp] theorem Rat.coe_nnabs (x : ℚ) : (Rat.nnabs x : ℚ) = abs x := rfl /-! ### Numerator and denominator -/ namespace NNRat variable {p q : ℚ≥0} @[norm_cast] lemma num_coe (q : ℚ≥0) : (q : ℚ).num = q.num := by simp only [num, Int.natCast_natAbs, Rat.num_nonneg, coe_nonneg, abs_of_nonneg] theorem natAbs_num_coe : (q : ℚ).num.natAbs = q.num := rfl @[norm_cast] lemma den_coe : (q : ℚ).den = q.den := rfl @[simp] lemma num_ne_zero : q.num ≠ 0 ↔ q ≠ 0 := by simp [num] @[simp] lemma num_pos : 0 < q.num ↔ 0 < q := by simpa [num, -nonpos_iff_eq_zero] using nonpos_iff_eq_zero _ |>.not.symm @[simp] lemma den_pos (q : ℚ≥0) : 0 < q.den := Rat.den_pos _ @[simp] lemma den_ne_zero (q : ℚ≥0) : q.den ≠ 0 := Rat.den_ne_zero _ lemma coprime_num_den (q : ℚ≥0) : q.num.Coprime q.den := by simpa [num, den] using Rat.reduced _ -- TODO: Rename `Rat.coe_nat_num`, `Rat.intCast_den`, `Rat.ofNat_num`, `Rat.ofNat_den` @[simp, norm_cast] lemma num_natCast (n : ℕ) : num n = n := rfl @[simp, norm_cast] lemma den_natCast (n : ℕ) : den n = 1 := rfl @[simp] lemma num_ofNat (n : ℕ) [n.AtLeastTwo] : num ofNat(n) = OfNat.ofNat n := rfl @[simp] lemma den_ofNat (n : ℕ) [n.AtLeastTwo] : den ofNat(n) = 1 := rfl theorem ext_num_den (hn : p.num = q.num) (hd : p.den = q.den) : p = q := by refine ext <| Rat.ext ?_ hd simpa [num_coe] theorem ext_num_den_iff : p = q ↔ p.num = q.num ∧ p.den = q.den := ⟨by rintro rfl; exact ⟨rfl, rfl⟩, fun h ↦ ext_num_den h.1 h.2⟩ /-- Form the quotient `n / d` where `n d : ℕ`. See also `Rat.divInt` and `mkRat`. -/ def divNat (n d : ℕ) : ℚ≥0 := ⟨.divInt n d, Rat.divInt_nonneg (Int.ofNat_zero_le n) (Int.ofNat_zero_le d)⟩ variable {n₁ n₂ d₁ d₂ : ℕ} @[simp, norm_cast] lemma coe_divNat (n d : ℕ) : (divNat n d : ℚ) = .divInt n d := rfl lemma mk_divInt (n d : ℕ) : ⟨.divInt n d, Rat.divInt_nonneg (Int.ofNat_zero_le n) (Int.ofNat_zero_le d)⟩ = divNat n d := rfl lemma divNat_inj (h₁ : d₁ ≠ 0) (h₂ : d₂ ≠ 0) : divNat n₁ d₁ = divNat n₂ d₂ ↔ n₁ * d₂ = n₂ * d₁ := by rw [← coe_inj]; simp [Rat.mkRat_eq_iff, h₁, h₂]; norm_cast @[simp] lemma divNat_zero (n : ℕ) : divNat n 0 = 0 := by simp [divNat]; rfl @[simp] lemma num_divNat_den (q : ℚ≥0) : divNat q.num q.den = q := ext <| by rw [← (q : ℚ).mkRat_num_den']; simp [num_coe, den_coe] lemma natCast_eq_divNat (n : ℕ) : (n : ℚ≥0) = divNat n 1 := (num_divNat_den _).symm lemma divNat_mul_divNat (n₁ n₂ : ℕ) {d₁ d₂} (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) : divNat n₁ d₁ * divNat n₂ d₂ = divNat (n₁ * n₂) (d₁ * d₂) := by ext; push_cast; exact Rat.divInt_mul_divInt _ _ (mod_cast hd₁) (mod_cast hd₂) lemma divNat_mul_left {a : ℕ} (ha : a ≠ 0) (n d : ℕ) : divNat (a * n) (a * d) = divNat n d := by ext; push_cast; exact Rat.divInt_mul_left (mod_cast ha) lemma divNat_mul_right {a : ℕ} (ha : a ≠ 0) (n d : ℕ) : divNat (n * a) (d * a) = divNat n d := by ext; push_cast; exact Rat.divInt_mul_right (mod_cast ha) @[simp] lemma mul_den_eq_num (q : ℚ≥0) : q * q.den = q.num := by ext push_cast rw [← Int.cast_natCast, ← den_coe, ← Int.cast_natCast q.num, ← num_coe] exact Rat.mul_den_eq_num _ @[simp] lemma den_mul_eq_num (q : ℚ≥0) : q.den * q = q.num := by rw [mul_comm, mul_den_eq_num] /-- Define a (dependent) function or prove `∀ r : ℚ, p r` by dealing with nonnegative rational numbers of the form `n / d` with `d ≠ 0` and `n`, `d` coprime. -/ @[elab_as_elim] def numDenCasesOn.{u} {C : ℚ≥0 → Sort u} (q) (H : ∀ n d, d ≠ 0 → n.Coprime d → C (divNat n d)) : C q := by rw [← q.num_divNat_den]; exact H _ _ q.den_ne_zero q.coprime_num_den lemma add_def (q r : ℚ≥0) : q + r = divNat (q.num * r.den + r.num * q.den) (q.den * r.den) := by ext; simp [Rat.add_def', Rat.mkRat_eq_divInt, num_coe, den_coe] lemma mul_def (q r : ℚ≥0) : q * r = divNat (q.num * r.num) (q.den * r.den) := by ext; simp [Rat.mul_eq_mkRat, Rat.mkRat_eq_divInt, num_coe, den_coe] theorem lt_def {p q : ℚ≥0} : p < q ↔ p.num * q.den < q.num * p.den := by rw [← NNRat.coe_lt_coe, Rat.lt_def]; norm_cast theorem le_def {p q : ℚ≥0} : p ≤ q ↔ p.num * q.den ≤ q.num * p.den := by rw [← NNRat.coe_le_coe, Rat.le_def]; norm_cast end NNRat namespace Mathlib.Tactic.Qify @[qify_simps] lemma nnratCast_eq (a b : ℚ≥0) : a = b ↔ (a : ℚ) = (b : ℚ) := NNRat.coe_inj.symm @[qify_simps] lemma nnratCast_le (a b : ℚ≥0) : a ≤ b ↔ (a : ℚ) ≤ (b : ℚ) := NNRat.coe_le_coe.symm @[qify_simps] lemma nnratCast_lt (a b : ℚ≥0) : a < b ↔ (a : ℚ) < (b : ℚ) := NNRat.coe_lt_coe.symm @[qify_simps] lemma nnratCast_ne (a b : ℚ≥0) : a ≠ b ↔ (a : ℚ) ≠ (b : ℚ) := NNRat.ne_iff.symm end Mathlib.Tactic.Qify
Mathlib/Data/NNRat/Defs.lean
423
424
/- 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.Partrec import Mathlib.Data.Option.Basic /-! # Gödel Numbering for Partial Recursive Functions. This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors are primitive recursive with respect to the encoding. It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation of some code. ## Main Definitions * `Nat.Partrec.Code`: Inductive datatype for partial recursive codes. * `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers. * `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding. * `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function. ## Main Results * `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive. * `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable. * `Nat.Partrec.Code.smn`: The $S_n^m$ theorem. * `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code. * `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive. * `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem. * `Nat.Partrec.Code.fixed_point₂`: Kleene's second recursion theorem. ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open Encodable Denumerable namespace Nat.Partrec theorem rfind' {f} (hf : Nat.Partrec f) : Nat.Partrec (Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) := Partrec₂.unpaired'.2 <| by refine Partrec.map ((@Partrec₂.unpaired' fun a b : ℕ => Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1 ?_) (Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂ have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$> Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2))) (Nat.pair a n))) := rfind (Partrec₂.unpaired'.2 ((Partrec.nat_iff.2 hf).comp (Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst) (Primrec.nat_add.comp Primrec.snd (Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp)) simpa /-- Code for partial recursive functions from ℕ to ℕ. See `Nat.Partrec.Code.eval` for the interpretation of these constructors. -/ inductive Code : Type | zero : Code | succ : Code | left : Code | right : Code | pair : Code → Code → Code | comp : Code → Code → Code | prec : Code → Code → Code | rfind' : Code → Code compile_inductive% Code end Nat.Partrec namespace Nat.Partrec.Code instance instInhabited : Inhabited Code := ⟨zero⟩ /-- Returns a code for the constant function outputting a particular natural. -/ protected def const : ℕ → Code | 0 => zero | n + 1 => comp succ (Code.const n) theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂ | 0, 0, _ => by simp | n₁ + 1, n₂ + 1, h => by dsimp [Nat.Partrec.Code.const] at h injection h with h₁ h₂ simp only [const_inj h₂] /-- A code for the identity function. -/ protected def id : Code := pair left right /-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`. -/ def curry (c : Code) (n : ℕ) : Code := comp c (pair (Code.const n) Code.id) /-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/ def encodeCode : Code → ℕ | zero => 0 | succ => 1 | left => 2 | right => 3 | pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4 | comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4 | prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4 | rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4 /-- A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents. -/ def ofNatCode : ℕ → Code | 0 => zero | 1 => succ | 2 => left | 3 => right | n + 4 => let m := n.div2.div2 have hm : m < n + 4 := by simp only [m, div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm match n.bodd, n.div2.bodd with | false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2) | false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2) | true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2) | true , true => rfind' (ofNatCode m) /-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode` -/ private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n | 0 => by simp [ofNatCode, encodeCode] | 1 => by simp [ofNatCode, encodeCode] | 2 => by simp [ofNatCode, encodeCode] | 3 => by simp [ofNatCode, encodeCode] | n + 4 => by let m := n.div2.div2 have hm : m < n + 4 := by simp only [m, div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm have IH := encode_ofNatCode m have IH1 := encode_ofNatCode m.unpair.1 have IH2 := encode_ofNatCode m.unpair.2 conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2] simp only [ofNatCode.eq_5] cases n.bodd <;> cases n.div2.bodd <;> simp [m, encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val] instance instDenumerable : Denumerable Code := mk' ⟨encodeCode, ofNatCode, fun c => by induction c <;> simp [encodeCode, ofNatCode, Nat.div2_val, *], encode_ofNatCode⟩ theorem encodeCode_eq : encode = encodeCode := rfl theorem ofNatCode_eq : ofNat Code = ofNatCode := rfl theorem encode_lt_pair (cf cg) : encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by simp only [encodeCode_eq, encodeCode] have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2) rw [one_mul, mul_assoc] at this have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4)) exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩ theorem encode_lt_comp (cf cg) : encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by have : encode (pair cf cg) < encode (comp cf cg) := by simp [encodeCode_eq, encodeCode] exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this theorem encode_lt_prec (cf cg) : encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by have : encode (pair cf cg) < encode (prec cf cg) := by simp [encodeCode_eq, encodeCode] exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by simp only [encodeCode_eq, encodeCode] omega end Nat.Partrec.Code section open Primrec namespace Nat.Partrec.Code theorem pair_prim : Primrec₂ pair := Primrec₂.ofNat_iff.2 <| Primrec₂.encode_iff.1 <| nat_add.comp (nat_double.comp <| nat_double.comp <| Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst) (encode_iff.2 <| (Primrec.ofNat Code).comp snd)) (Primrec₂.const 4) theorem comp_prim : Primrec₂ comp := Primrec₂.ofNat_iff.2 <| Primrec₂.encode_iff.1 <| nat_add.comp (nat_double.comp <| nat_double_succ.comp <| Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst) (encode_iff.2 <| (Primrec.ofNat Code).comp snd)) (Primrec₂.const 4) theorem prec_prim : Primrec₂ prec := Primrec₂.ofNat_iff.2 <| Primrec₂.encode_iff.1 <| nat_add.comp (nat_double_succ.comp <| nat_double.comp <| Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst) (encode_iff.2 <| (Primrec.ofNat Code).comp snd)) (Primrec₂.const 4) theorem rfind_prim : Primrec rfind' := ofNat_iff.2 <| encode_iff.1 <| nat_add.comp (nat_double_succ.comp <| nat_double_succ.comp <| encode_iff.2 <| Primrec.ofNat Code) (const 4) theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ} (hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ} (hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) : let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg) let CO (a) cf cg hf hg := co a (cf, cg, hf, hg) let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg) let RF (a) cf hf := rf a (cf, hf) let F (a : α) (c : Code) : σ := Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a) Primrec (fun a => F a (c a) : α → σ) := by intros _ _ _ _ F let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p => letI a := p.1.1; letI IH := p.1.2; letI n := p.2.1; letI m := p.2.2 IH[m]?.bind fun s => IH[m.unpair.1]?.bind fun s₁ => IH[m.unpair.2]?.map fun s₂ => cond n.bodd (cond n.div2.bodd (rf a (ofNat Code m, s)) (pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))) (cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)) (pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))) have : Primrec G₁ := option_bind (list_getElem?.comp (snd.comp fst) (snd.comp snd)) <| .mk <| option_bind ((list_getElem?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) <| .mk <| option_map ((list_getElem?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) <| .mk <| have a := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Primrec.unpair.comp m) have m₂ := snd.comp (Primrec.unpair.comp m) have s := snd.comp (fst.comp fst) have s₁ := snd.comp fst have s₂ := snd (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s)) (hpc.comp a (((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Primrec.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))) let G : α → List σ → Option σ := fun a IH => IH.length.casesOn (some (z a)) fun n => n.casesOn (some (s a)) fun n => n.casesOn (some (l a)) fun n => n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2) have : Primrec₂ G := .mk <| nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| .mk <| nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| .mk <| nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| .mk <| nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) <| .mk <| this.comp <| ((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <| snd.pair <| nat_div2.comp <| nat_div2.comp snd refine (nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => ?_) |>.comp .id (encode_iff.2 hc) |>.of_eq fun a => by simp iterate 4 rcases n with - | n; · simp [ofNatCode_eq, ofNatCode]; rfl simp only [G]; rw [List.length_map, List.length_range] let m := n.div2.div2 show G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) = some (F a (ofNat Code (n + 4))) have hm : m < n + 4 := by simp only [m, div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self ..) (Nat.div_le_self ..)) (Nat.succ_le_succ (Nat.le_add_right ..)) have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm simp [G₁, m, List.getElem?_map, List.getElem?_range, hm, m1, m2] rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl] simp [ofNatCode] cases n.bodd <;> cases n.div2.bodd <;> rfl /-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/ theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ} (hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ} (hr : Primrec r) {pr : α → Code → Code → σ → σ → σ} (hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2) {co : α → Code → Code → σ → σ → σ} (hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2) {pc : α → Code → Code → σ → σ → σ} (hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2) {rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) : let F (a : α) (c : Code) : σ := Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) Primrec fun a => F a (c a) := rec_prim' hc hz hs hl hr (pr := fun a b => pr a b.1 b.2.1 b.2.2.1 b.2.2.2) (.mk hpr) (co := fun a b => co a b.1 b.2.1 b.2.2.1 b.2.2.2) (.mk hco) (pc := fun a b => pc a b.1 b.2.1 b.2.2.1 b.2.2.2) (.mk hpc) (rf := fun a b => rf a b.1 b.2) (.mk hrf) end Nat.Partrec.Code end namespace Nat.Partrec.Code section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ rf) : let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg) let CO (a) cf cg hf hg := co a (cf, cg, hf, hg) let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg) let RF (a) cf hf := rf a (cf, hf) let F (a : α) (c : Code) : σ := Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof intros _ _ _ _ F let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p => letI a := p.1.1; letI IH := p.1.2; letI n := p.2.1; letI m := p.2.2 IH[m]?.bind fun s => IH[m.unpair.1]?.bind fun s₁ => IH[m.unpair.2]?.map fun s₂ => cond n.bodd (cond n.div2.bodd (rf a (ofNat Code m, s)) (pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))) (cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)) (pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))) have : Computable G₁ := by refine option_bind (list_getElem?.comp (snd.comp fst) (snd.comp snd)) <| .mk ?_ refine option_bind ((list_getElem?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) <| .mk ?_ refine option_map ((list_getElem?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) <| .mk ?_ exact have a := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s := snd.comp (fst.comp fst) have s₁ := snd.comp fst have s₂ := snd (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) let G : α → List σ → Option σ := fun a IH => IH.length.casesOn (some (z a)) fun n => n.casesOn (some (s a)) fun n => n.casesOn (some (l a)) fun n => n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2) have : Computable₂ G := .mk <| nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| .mk <| nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| .mk <| nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| .mk <| nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) <| .mk <| this.comp <| ((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <| snd.pair <| nat_div2.comp <| nat_div2.comp snd refine (nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => ?_) |>.comp .id (encode_iff.2 hc) |>.of_eq fun a => by simp iterate 4 rcases n with - | n; · simp [ofNatCode_eq, ofNatCode]; rfl simp only [G]; rw [List.length_map, List.length_range] let m := n.div2.div2 show G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) = some (F a (ofNat Code (n + 4))) have hm : m < n + 4 := by simp only [m, div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self ..) (Nat.div_le_self ..)) (Nat.succ_le_succ (Nat.le_add_right ..)) have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm simp [G₁, m, List.getElem?_map, List.getElem?_range, hm, m1, m2] rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl] simp [ofNatCode] cases n.bodd <;> cases n.div2.bodd <;> rfl end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp instance : Membership (ℕ →. ℕ) Code := ⟨fun c f => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, _ => rfl | n + 1, m => by simp! [eval_const n m] @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq, Code.id] @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq, curry] theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ /-- A function is partial recursive if and only if there is a code implementing it. Therefore, `eval` is a **universal partial recursive function**. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := by
refine ⟨fun h => ?_, ?_⟩ · induction h with | zero => exact ⟨zero, rfl⟩
Mathlib/Computability/PartrecCode.lean
525
527
/- 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.Analysis.Calculus.SmoothSeries import Mathlib.Analysis.Calculus.BumpFunction.InnerProduct import Mathlib.Analysis.Convolution import Mathlib.Analysis.InnerProductSpace.EuclideanDist import Mathlib.Data.Set.Pointwise.Support import Mathlib.MeasureTheory.Measure.Haar.NormedSpace import Mathlib.MeasureTheory.Measure.Haar.Unique /-! # Bump functions in finite-dimensional vector spaces Let `E` be a finite-dimensional real normed vector space. We show that any open set `s` in `E` is exactly the support of a smooth function taking values in `[0, 1]`, in `IsOpen.exists_smooth_support_eq`. Then we use this construction to construct bump functions with nice behavior, by convolving the indicator function of `closedBall 0 1` with a function as above with `s = ball 0 D`. -/ noncomputable section open Set Metric TopologicalSpace Function Asymptotics MeasureTheory Module ContinuousLinearMap Filter MeasureTheory.Measure Bornology open scoped Pointwise Topology NNReal Convolution ContDiff variable {E : Type*} [NormedAddCommGroup E] section variable [NormedSpace ℝ E] [FiniteDimensional ℝ E] /-- If a set `s` is a neighborhood of `x`, then there exists a smooth function `f` taking values in `[0, 1]`, supported in `s` and with `f x = 1`. -/ theorem exists_smooth_tsupport_subset {s : Set E} {x : E} (hs : s ∈ 𝓝 x) : ∃ f : E → ℝ, tsupport f ⊆ s ∧ HasCompactSupport f ∧ ContDiff ℝ ∞ f ∧ range f ⊆ Icc 0 1 ∧ f x = 1 := by obtain ⟨d : ℝ, d_pos : 0 < d, hd : Euclidean.closedBall x d ⊆ s⟩ := Euclidean.nhds_basis_closedBall.mem_iff.1 hs let c : ContDiffBump (toEuclidean x) := { rIn := d / 2 rOut := d rIn_pos := half_pos d_pos rIn_lt_rOut := half_lt_self d_pos } let f : E → ℝ := c ∘ toEuclidean have f_supp : f.support ⊆ Euclidean.ball x d := by intro y hy have : toEuclidean y ∈ Function.support c := by simpa only [Function.mem_support, Function.comp_apply, Ne] using hy rwa [c.support_eq] at this have f_tsupp : tsupport f ⊆ Euclidean.closedBall x d := by rw [tsupport, ← Euclidean.closure_ball _ d_pos.ne'] exact closure_mono f_supp refine ⟨f, f_tsupp.trans hd, ?_, ?_, ?_, ?_⟩ · refine isCompact_of_isClosed_isBounded isClosed_closure ?_ have : IsBounded (Euclidean.closedBall x d) := Euclidean.isCompact_closedBall.isBounded refine this.subset (Euclidean.isClosed_closedBall.closure_subset_iff.2 ?_) exact f_supp.trans Euclidean.ball_subset_closedBall · apply c.contDiff.comp exact ContinuousLinearEquiv.contDiff _ · rintro t ⟨y, rfl⟩ exact ⟨c.nonneg, c.le_one⟩ · apply c.one_of_mem_closedBall apply mem_closedBall_self exact (half_pos d_pos).le /-- Given an open set `s` in a finite-dimensional real normed vector space, there exists a smooth function with values in `[0, 1]` whose support is exactly `s`. -/ theorem IsOpen.exists_smooth_support_eq {s : Set E} (hs : IsOpen s) : ∃ f : E → ℝ, f.support = s ∧ ContDiff ℝ ∞ f ∧ Set.range f ⊆ Set.Icc 0 1 := by /- For any given point `x` in `s`, one can construct a smooth function with support in `s` and nonzero at `x`. By second-countability, it follows that we may cover `s` with the supports of countably many such functions, say `g i`. Then `∑ i, r i • g i` will be the desired function if `r i` is a sequence of positive numbers tending quickly enough to zero. Indeed, this ensures that, for any `k ≤ i`, the `k`-th derivative of `r i • g i` is bounded by a prescribed (summable) sequence `u i`. From this, the summability of the series and of its successive derivatives follows. -/ rcases eq_empty_or_nonempty s with (rfl | h's) · exact ⟨fun _ => 0, Function.support_zero, contDiff_const, by simp only [range_const, singleton_subset_iff, left_mem_Icc, zero_le_one]⟩ let ι := { f : E → ℝ // f.support ⊆ s ∧ HasCompactSupport f ∧ ContDiff ℝ ∞ f ∧ range f ⊆ Icc 0 1 } obtain ⟨T, T_count, hT⟩ : ∃ T : Set ι, T.Countable ∧ ⋃ f ∈ T, support (f : E → ℝ) = s := by have : ⋃ f : ι, (f : E → ℝ).support = s := by refine Subset.antisymm (iUnion_subset fun f => f.2.1) ?_ intro x hx rcases exists_smooth_tsupport_subset (hs.mem_nhds hx) with ⟨f, hf⟩ let g : ι := ⟨f, (subset_tsupport f).trans hf.1, hf.2.1, hf.2.2.1, hf.2.2.2.1⟩ have : x ∈ support (g : E → ℝ) := by simp only [g, hf.2.2.2.2, Subtype.coe_mk, mem_support, Ne, one_ne_zero, not_false_iff] exact mem_iUnion_of_mem _ this simp_rw [← this] apply isOpen_iUnion_countable rintro ⟨f, hf⟩ exact hf.2.2.1.continuous.isOpen_support obtain ⟨g0, hg⟩ : ∃ g0 : ℕ → ι, T = range g0 := by apply Countable.exists_eq_range T_count rcases eq_empty_or_nonempty T with (rfl | hT) · simp only [ι, iUnion_false, iUnion_empty] at hT simp only [← hT, mem_empty_iff_false, iUnion_of_empty, iUnion_empty, Set.not_nonempty_empty] at h's · exact hT let g : ℕ → E → ℝ := fun n => (g0 n).1 have g_s : ∀ n, support (g n) ⊆ s := fun n => (g0 n).2.1 have s_g : ∀ x ∈ s, ∃ n, x ∈ support (g n) := fun x hx ↦ by rw [← hT] at hx obtain ⟨i, iT, hi⟩ : ∃ i ∈ T, x ∈ support (i : E → ℝ) := by simpa only [mem_iUnion, exists_prop] using hx rw [hg, mem_range] at iT rcases iT with ⟨n, hn⟩ rw [← hn] at hi exact ⟨n, hi⟩ have g_smooth : ∀ n, ContDiff ℝ ∞ (g n) := fun n => (g0 n).2.2.2.1 have g_comp_supp : ∀ n, HasCompactSupport (g n) := fun n => (g0 n).2.2.1 have g_nonneg : ∀ n x, 0 ≤ g n x := fun n x => ((g0 n).2.2.2.2 (mem_range_self x)).1 obtain ⟨δ, δpos, c, δc, c_lt⟩ : ∃ δ : ℕ → ℝ≥0, (∀ i : ℕ, 0 < δ i) ∧ ∃ c : NNReal, HasSum δ c ∧ c < 1 := NNReal.exists_pos_sum_of_countable one_ne_zero ℕ have : ∀ n : ℕ, ∃ r : ℝ, 0 < r ∧ ∀ i ≤ n, ∀ x, ‖iteratedFDeriv ℝ i (r • g n) x‖ ≤ δ n := by intro n have : ∀ i, ∃ R, ∀ x, ‖iteratedFDeriv ℝ i (fun x => g n x) x‖ ≤ R := by intro i have : BddAbove (range fun x => ‖iteratedFDeriv ℝ i (fun x : E => g n x) x‖) := by apply ((g_smooth n).continuous_iteratedFDeriv (mod_cast le_top)).norm.bddAbove_range_of_hasCompactSupport apply HasCompactSupport.comp_left _ norm_zero apply (g_comp_supp n).iteratedFDeriv rcases this with ⟨R, hR⟩ exact ⟨R, fun x => hR (mem_range_self _)⟩ choose R hR using this let M := max (((Finset.range (n + 1)).image R).max' (by simp)) 1 have δnpos : 0 < δ n := δpos n have IR : ∀ i ≤ n, R i ≤ M := by intro i hi refine le_trans ?_ (le_max_left _ _) apply Finset.le_max' apply Finset.mem_image_of_mem simp only [Finset.mem_range] omega refine ⟨M⁻¹ * δ n, by positivity, fun i hi x => ?_⟩ calc ‖iteratedFDeriv ℝ i ((M⁻¹ * δ n) • g n) x‖ = ‖(M⁻¹ * δ n) • iteratedFDeriv ℝ i (g n) x‖ := by rw [iteratedFDeriv_const_smul_apply] exact (g_smooth n).contDiffAt.of_le (mod_cast le_top) _ = M⁻¹ * δ n * ‖iteratedFDeriv ℝ i (g n) x‖ := by rw [norm_smul _ (iteratedFDeriv ℝ i (g n) x), Real.norm_of_nonneg]; positivity _ ≤ M⁻¹ * δ n * M := by gcongr; exact (hR i x).trans (IR i hi) _ = δ n := by field_simp choose r rpos hr using this have S : ∀ x, Summable fun n => (r n • g n) x := fun x ↦ by refine .of_nnnorm_bounded _ δc.summable fun n => ?_ rw [← NNReal.coe_le_coe, coe_nnnorm] simpa only [norm_iteratedFDeriv_zero] using hr n 0 (zero_le n) x refine ⟨fun x => ∑' n, (r n • g n) x, ?_, ?_, ?_⟩ · apply Subset.antisymm · intro x hx simp only [Pi.smul_apply, Algebra.id.smul_eq_mul, mem_support, Ne] at hx contrapose! hx have : ∀ n, g n x = 0 := by intro n contrapose! hx exact g_s n hx simp only [this, mul_zero, tsum_zero] · intro x hx obtain ⟨n, hn⟩ : ∃ n, x ∈ support (g n) := s_g x hx have I : 0 < r n * g n x := mul_pos (rpos n) (lt_of_le_of_ne (g_nonneg n x) (Ne.symm hn)) exact ne_of_gt ((S x).tsum_pos (fun i => mul_nonneg (rpos i).le (g_nonneg i x)) n I) · refine contDiff_tsum_of_eventually (fun n => (g_smooth n).const_smul (r n)) (fun k _ => (NNReal.hasSum_coe.2 δc).summable) ?_ intro i _ simp only [Nat.cofinite_eq_atTop, Pi.smul_apply, Algebra.id.smul_eq_mul, Filter.eventually_atTop] exact ⟨i, fun n hn x => hr _ _ hn _⟩ · rintro - ⟨y, rfl⟩ refine ⟨tsum_nonneg fun n => mul_nonneg (rpos n).le (g_nonneg n y), le_trans ?_ c_lt.le⟩ have A : HasSum (fun n => (δ n : ℝ)) c := NNReal.hasSum_coe.2 δc simp only [Pi.smul_apply, smul_eq_mul, NNReal.val_eq_coe, ← A.tsum_eq] apply Summable.tsum_le_tsum _ (S y) A.summable intro n apply (le_abs_self _).trans simpa only [norm_iteratedFDeriv_zero] using hr n 0 (zero_le n) y end section namespace ExistsContDiffBumpBase /-- An auxiliary function to construct partitions of unity on finite-dimensional real vector spaces. It is the characteristic function of the closed unit ball. -/ def φ : E → ℝ := (closedBall (0 : E) 1).indicator fun _ => (1 : ℝ) variable [NormedSpace ℝ E] [FiniteDimensional ℝ E] section HelperDefinitions variable (E) theorem u_exists : ∃ u : E → ℝ, ContDiff ℝ ∞ u ∧ (∀ x, u x ∈ Icc (0 : ℝ) 1) ∧ support u = ball 0 1 ∧ ∀ x, u (-x) = u x := by have A : IsOpen (ball (0 : E) 1) := isOpen_ball obtain ⟨f, f_support, f_smooth, f_range⟩ : ∃ f : E → ℝ, f.support = ball (0 : E) 1 ∧ ContDiff ℝ ∞ f ∧ Set.range f ⊆ Set.Icc 0 1 := A.exists_smooth_support_eq have B : ∀ x, f x ∈ Icc (0 : ℝ) 1 := fun x => f_range (mem_range_self x) refine ⟨fun x => (f x + f (-x)) / 2, ?_, ?_, ?_, ?_⟩ · exact (f_smooth.add (f_smooth.comp contDiff_neg)).div_const _ · intro x simp only [mem_Icc] constructor · linarith [(B x).1, (B (-x)).1] · linarith [(B x).2, (B (-x)).2] · refine support_eq_iff.2 ⟨fun x hx => ?_, fun x hx => ?_⟩ · apply ne_of_gt have : 0 < f x := by apply lt_of_le_of_ne (B x).1 (Ne.symm _) rwa [← f_support] at hx linarith [(B (-x)).1] · have I1 : x ∉ support f := by rwa [f_support] have I2 : -x ∉ support f := by rw [f_support] simpa using hx simp only [mem_support, Classical.not_not] at I1 I2 simp only [I1, I2, add_zero, zero_div] · intro x; simp only [add_comm, neg_neg] variable {E} in /-- An auxiliary function to construct partitions of unity on finite-dimensional real vector spaces, which is smooth, symmetric, and with support equal to the unit ball. -/ def u (x : E) : ℝ := Classical.choose (u_exists E) x theorem u_smooth : ContDiff ℝ ∞ (u : E → ℝ) := (Classical.choose_spec (u_exists E)).1 theorem u_continuous : Continuous (u : E → ℝ) := (u_smooth E).continuous theorem u_support : support (u : E → ℝ) = ball 0 1 := (Classical.choose_spec (u_exists E)).2.2.1 theorem u_compact_support : HasCompactSupport (u : E → ℝ) := by rw [hasCompactSupport_def, u_support, closure_ball (0 : E) one_ne_zero] exact isCompact_closedBall _ _ variable {E} theorem u_nonneg (x : E) : 0 ≤ u x := ((Classical.choose_spec (u_exists E)).2.1 x).1 theorem u_le_one (x : E) : u x ≤ 1 := ((Classical.choose_spec (u_exists E)).2.1 x).2 theorem u_neg (x : E) : u (-x) = u x := (Classical.choose_spec (u_exists E)).2.2.2 x variable [MeasurableSpace E] [BorelSpace E] local notation "μ" => MeasureTheory.Measure.addHaar variable (E) in theorem u_int_pos : 0 < ∫ x : E, u x ∂μ := by refine (integral_pos_iff_support_of_nonneg u_nonneg ?_).mpr ?_ · exact (u_continuous E).integrable_of_hasCompactSupport (u_compact_support E) · rw [u_support]; exact measure_ball_pos _ _ zero_lt_one /-- An auxiliary function to construct partitions of unity on finite-dimensional real vector spaces, which is smooth, symmetric, with support equal to the ball of radius `D` and integral `1`. -/ def w (D : ℝ) (x : E) : ℝ := ((∫ x : E, u x ∂μ) * |D| ^ finrank ℝ E)⁻¹ • u (D⁻¹ • x) theorem w_def (D : ℝ) : (w D : E → ℝ) = fun x => ((∫ x : E, u x ∂μ) * |D| ^ finrank ℝ E)⁻¹ • u (D⁻¹ • x) := by ext1 x; rfl theorem w_nonneg (D : ℝ) (x : E) : 0 ≤ w D x := by apply mul_nonneg _ (u_nonneg _) apply inv_nonneg.2 apply mul_nonneg (u_int_pos E).le norm_cast apply pow_nonneg (abs_nonneg D) theorem w_mul_φ_nonneg (D : ℝ) (x y : E) : 0 ≤ w D y * φ (x - y) := mul_nonneg (w_nonneg D y) (indicator_nonneg (by simp only [zero_le_one, imp_true_iff]) _) variable (E) theorem w_integral {D : ℝ} (Dpos : 0 < D) : ∫ x : E, w D x ∂μ = 1 := by simp_rw [w, integral_smul] rw [integral_comp_inv_smul_of_nonneg μ (u : E → ℝ) Dpos.le, abs_of_nonneg Dpos.le, mul_comm] field_simp [(u_int_pos E).ne'] theorem w_support {D : ℝ} (Dpos : 0 < D) : support (w D : E → ℝ) = ball 0 D := by have B : D • ball (0 : E) 1 = ball 0 D := by rw [smul_unitBall Dpos.ne', Real.norm_of_nonneg Dpos.le] have C : D ^ finrank ℝ E ≠ 0 := by norm_cast exact pow_ne_zero _ Dpos.ne' simp only [w_def, Algebra.id.smul_eq_mul, support_mul, support_inv, univ_inter, support_comp_inv_smul₀ Dpos.ne', u_support, B, support_const (u_int_pos E).ne', support_const C, abs_of_nonneg Dpos.le] theorem w_compact_support {D : ℝ} (Dpos : 0 < D) : HasCompactSupport (w D : E → ℝ) := by rw [hasCompactSupport_def, w_support E Dpos, closure_ball (0 : E) Dpos.ne'] exact isCompact_closedBall _ _ variable {E} /-- An auxiliary function to construct partitions of unity on finite-dimensional real vector spaces. It is the convolution between a smooth function of integral `1` supported in the ball of radius `D`, with the indicator function of the closed unit ball. Therefore, it is smooth, equal to `1` on the ball of radius `1 - D`, with support equal to the ball of radius `1 + D`. -/ def y (D : ℝ) : E → ℝ := w D ⋆[lsmul ℝ ℝ, μ] φ theorem y_neg (D : ℝ) (x : E) : y D (-x) = y D x := by apply convolution_neg_of_neg_eq · filter_upwards with x simp only [w_def, Real.rpow_natCast, mul_inv_rev, smul_neg, u_neg, smul_eq_mul, forall_const] · filter_upwards with x simp only [φ, indicator, mem_closedBall, dist_zero_right, norm_neg, forall_const] theorem y_eq_one_of_mem_closedBall {D : ℝ} {x : E} (Dpos : 0 < D) (hx : x ∈ closedBall (0 : E) (1 - D)) : y D x = 1 := by change (w D ⋆[lsmul ℝ ℝ, μ] φ) x = 1 have B : ∀ y : E, y ∈ ball x D → φ y = 1 := by have C : ball x D ⊆ ball 0 1 := by apply ball_subset_ball' simp only [mem_closedBall] at hx linarith only [hx] intro y hy simp only [φ, indicator, mem_closedBall, ite_eq_left_iff, not_le, zero_ne_one] intro h'y linarith only [mem_ball.1 (C hy), h'y] have Bx : φ x = 1 := B _ (mem_ball_self Dpos) have B' : ∀ y, y ∈ ball x D → φ y = φ x := by rw [Bx]; exact B rw [convolution_eq_right' _ (le_of_eq (w_support E Dpos)) B'] simp only [lsmul_apply, Algebra.id.smul_eq_mul, integral_mul_const, w_integral E Dpos, Bx, one_mul] theorem y_eq_zero_of_not_mem_ball {D : ℝ} {x : E} (Dpos : 0 < D) (hx : x ∉ ball (0 : E) (1 + D)) : y D x = 0 := by change (w D ⋆[lsmul ℝ ℝ, μ] φ) x = 0 have B : ∀ y, y ∈ ball x D → φ y = 0 := by intro y hy simp only [φ, indicator, mem_closedBall_zero_iff, ite_eq_right_iff, one_ne_zero] intro h'y have C : ball y D ⊆ ball 0 (1 + D) := by apply ball_subset_ball' rw [← dist_zero_right] at h'y linarith only [h'y] exact hx (C (mem_ball_comm.1 hy)) have Bx : φ x = 0 := B _ (mem_ball_self Dpos) have B' : ∀ y, y ∈ ball x D → φ y = φ x := by rw [Bx]; exact B rw [convolution_eq_right' _ (le_of_eq (w_support E Dpos)) B'] simp only [lsmul_apply, Algebra.id.smul_eq_mul, Bx, mul_zero, integral_const] theorem y_nonneg (D : ℝ) (x : E) : 0 ≤ y D x := integral_nonneg (w_mul_φ_nonneg D x) theorem y_le_one {D : ℝ} (x : E) (Dpos : 0 < D) : y D x ≤ 1 := by have A : (w D ⋆[lsmul ℝ ℝ, μ] φ) x ≤ (w D ⋆[lsmul ℝ ℝ, μ] 1) x := by apply convolution_mono_right_of_nonneg _ (w_nonneg D) (indicator_le_self' fun x _ => zero_le_one) fun _ => zero_le_one refine ((w_compact_support E Dpos).convolutionExists_left _ ?_ (locallyIntegrable_const (1 : ℝ)) x).integrable exact continuous_const.mul ((u_continuous E).comp (continuous_id.const_smul _)) have B : (w D ⋆[lsmul ℝ ℝ, μ] fun _ => (1 : ℝ)) x = 1 := by simp only [convolution, ContinuousLinearMap.map_smul, mul_inv_rev, coe_smul', mul_one, lsmul_apply, Algebra.id.smul_eq_mul, integral_const_mul, w_integral E Dpos, Pi.smul_apply] exact A.trans (le_of_eq B)
theorem y_pos_of_mem_ball {D : ℝ} {x : E} (Dpos : 0 < D) (D_lt_one : D < 1) (hx : x ∈ ball (0 : E) (1 + D)) : 0 < y D x := by simp only [mem_ball_zero_iff] at hx refine (integral_pos_iff_support_of_nonneg (w_mul_φ_nonneg D x) ?_).2 ?_ · have F_comp : HasCompactSupport (w D) := w_compact_support E Dpos have B : LocallyIntegrable (φ : E → ℝ) μ := (locallyIntegrable_const _).indicator measurableSet_closedBall have C : Continuous (w D : E → ℝ) := continuous_const.mul ((u_continuous E).comp (continuous_id.const_smul _)) exact (F_comp.convolutionExists_left (lsmul ℝ ℝ : ℝ →L[ℝ] ℝ →L[ℝ] ℝ) C B x).integrable · set z := (D / (1 + D)) • x with hz have B : 0 < 1 + D := by linarith have C : ball z (D * (1 + D - ‖x‖) / (1 + D)) ⊆ support fun y : E => w D y * φ (x - y) := by intro y hy simp only [support_mul, w_support E Dpos] simp only [φ, mem_inter_iff, mem_support, Ne, indicator_apply_eq_zero,
Mathlib/Analysis/Calculus/BumpFunction/FiniteDimension.lean
384
399
/- 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 <;> · intro h simpa using h.reverse @[simp] theorem isRotated_reverse_iff : l.reverse ~r l'.reverse ↔ l ~r l' := by simp [isRotated_reverse_comm_iff] theorem isRotated_iff_mod : l ~r l' ↔ ∃ n ≤ l.length, l.rotate n = l' := by refine ⟨fun h => ?_, fun ⟨n, _, h⟩ => ⟨n, h⟩⟩ obtain ⟨n, rfl⟩ := h rcases l with - | ⟨hd, tl⟩ · simp · refine ⟨n % (hd :: tl).length, ?_, rotate_mod _ _⟩ refine (Nat.mod_lt _ ?_).le simp theorem isRotated_iff_mem_map_range : l ~r l' ↔ l' ∈ (List.range (l.length + 1)).map l.rotate := by simp_rw [mem_map, mem_range, isRotated_iff_mod] exact ⟨fun ⟨n, hn, h⟩ => ⟨n, Nat.lt_succ_of_le hn, h⟩, fun ⟨n, hn, h⟩ => ⟨n, Nat.le_of_lt_succ hn, h⟩⟩ theorem IsRotated.map {β : Type*} {l₁ l₂ : List α} (h : l₁ ~r l₂) (f : α → β) : map f l₁ ~r map f l₂ := by obtain ⟨n, rfl⟩ := h rw [map_rotate] use n theorem IsRotated.cons_getLast_dropLast (L : List α) (hL : L ≠ []) : L.getLast hL :: L.dropLast ~r L := by induction L using List.reverseRecOn with | nil => simp at hL | append_singleton a L _ => simp only [getLast_append, dropLast_concat] apply IsRotated.symm apply isRotated_concat theorem IsRotated.dropLast_tail {α} {L : List α} (hL : L ≠ []) (hL' : L.head hL = L.getLast hL) : L.dropLast ~r L.tail := match L with | [] => by simp | [_] => by simp | a :: b :: L => by simp only [head_cons, ne_eq, reduceCtorEq, not_false_eq_true, getLast_cons] at hL' simp [hL', IsRotated.cons_getLast_dropLast] /-- List of all cyclic permutations of `l`. The `cyclicPermutations` of a nonempty list `l` will always contain `List.length l` elements. This implies that under certain conditions, there are duplicates in `List.cyclicPermutations l`. The `n`th entry is equal to `l.rotate n`, proven in `List.get_cyclicPermutations`. The proof that every cyclic permutant of `l` is in the list is `List.mem_cyclicPermutations_iff`. cyclicPermutations [1, 2, 3, 2, 4] = [[1, 2, 3, 2, 4], [2, 3, 2, 4, 1], [3, 2, 4, 1, 2], [2, 4, 1, 2, 3], [4, 1, 2, 3, 2]] -/ def cyclicPermutations : List α → List (List α) | [] => [[]] | l@(_ :: _) => dropLast (zipWith (· ++ ·) (tails l) (inits l)) @[simp] theorem cyclicPermutations_nil : cyclicPermutations ([] : List α) = [[]] := rfl theorem cyclicPermutations_cons (x : α) (l : List α) : cyclicPermutations (x :: l) = dropLast (zipWith (· ++ ·) (tails (x :: l)) (inits (x :: l))) := rfl theorem cyclicPermutations_of_ne_nil (l : List α) (h : l ≠ []) : cyclicPermutations l = dropLast (zipWith (· ++ ·) (tails l) (inits l)) := by obtain ⟨hd, tl, rfl⟩ := exists_cons_of_ne_nil h exact cyclicPermutations_cons _ _ theorem length_cyclicPermutations_cons (x : α) (l : List α) : length (cyclicPermutations (x :: l)) = length l + 1 := by simp [cyclicPermutations_cons] @[simp] theorem length_cyclicPermutations_of_ne_nil (l : List α) (h : l ≠ []) : length (cyclicPermutations l) = length l := by simp [cyclicPermutations_of_ne_nil _ h] @[simp] theorem cyclicPermutations_ne_nil : ∀ l : List α, cyclicPermutations l ≠ [] | a::l, h => by simpa using congr_arg length h @[simp] theorem getElem_cyclicPermutations (l : List α) (n : Nat) (h : n < length (cyclicPermutations l)) : (cyclicPermutations l)[n] = l.rotate n := by cases l with | nil => simp | cons a l => simp only [cyclicPermutations_cons, getElem_dropLast, getElem_zipWith, getElem_tails, getElem_inits] rw [rotate_eq_drop_append_take (by simpa using h.le)] theorem get_cyclicPermutations (l : List α) (n : Fin (length (cyclicPermutations l))) : (cyclicPermutations l).get n = l.rotate n := by simp @[simp] theorem head_cyclicPermutations (l : List α) : (cyclicPermutations l).head (cyclicPermutations_ne_nil l) = l := by have h : 0 < length (cyclicPermutations l) := length_pos_of_ne_nil (cyclicPermutations_ne_nil _) rw [← get_mk_zero h, get_cyclicPermutations, Fin.val_mk, rotate_zero] @[simp] theorem head?_cyclicPermutations (l : List α) : (cyclicPermutations l).head? = l := by rw [head?_eq_head, head_cyclicPermutations] theorem cyclicPermutations_injective : Function.Injective (@cyclicPermutations α) := fun l l' h ↦ by simpa using congr_arg head? h @[simp] theorem cyclicPermutations_inj {l l' : List α} : cyclicPermutations l = cyclicPermutations l' ↔ l = l' := cyclicPermutations_injective.eq_iff theorem length_mem_cyclicPermutations (l : List α) (h : l' ∈ cyclicPermutations l) : length l' = length l := by obtain ⟨k, hk, rfl⟩ := get_of_mem h simp theorem mem_cyclicPermutations_self (l : List α) : l ∈ cyclicPermutations l := by simpa using head_mem (cyclicPermutations_ne_nil l) @[simp] theorem cyclicPermutations_rotate (l : List α) (k : ℕ) : (l.rotate k).cyclicPermutations = l.cyclicPermutations.rotate k := by have : (l.rotate k).cyclicPermutations.length = length (l.cyclicPermutations.rotate k) := by cases l · simp · rw [length_cyclicPermutations_of_ne_nil] <;> simp refine ext_get this fun n hn hn' => ?_ rw [get_rotate, get_cyclicPermutations, rotate_rotate, ← rotate_mod, Nat.add_comm] cases l <;> simp @[simp] theorem mem_cyclicPermutations_iff : l ∈ cyclicPermutations l' ↔ l ~r l' := by constructor · simp_rw [mem_iff_get, get_cyclicPermutations] rintro ⟨k, rfl⟩ exact .forall _ _ · rintro ⟨k, rfl⟩ rw [cyclicPermutations_rotate, mem_rotate] apply mem_cyclicPermutations_self @[simp] theorem cyclicPermutations_eq_nil_iff {l : List α} : cyclicPermutations l = [[]] ↔ l = [] := cyclicPermutations_injective.eq_iff' rfl @[simp] theorem cyclicPermutations_eq_singleton_iff {l : List α} {x : α} : cyclicPermutations l = [[x]] ↔ l = [x] := cyclicPermutations_injective.eq_iff' rfl /-- If a `l : List α` is `Nodup l`, then all of its cyclic permutants are distinct. -/ protected theorem Nodup.cyclicPermutations {l : List α} (hn : Nodup l) : Nodup (cyclicPermutations l) := by rcases eq_or_ne l [] with rfl | hl · simp · rw [nodup_iff_injective_get] rintro ⟨i, hi⟩ ⟨j, hj⟩ h simp only [length_cyclicPermutations_of_ne_nil l hl] at hi hj simpa [hn.rotate_congr_iff, mod_eq_of_lt, *] using h protected theorem IsRotated.cyclicPermutations {l l' : List α} (h : l ~r l') : l.cyclicPermutations ~r l'.cyclicPermutations := by obtain ⟨k, rfl⟩ := h exact ⟨k, by simp⟩ @[simp] theorem isRotated_cyclicPermutations_iff {l l' : List α} : l.cyclicPermutations ~r l'.cyclicPermutations ↔ l ~r l' := by simp only [IsRotated, ← cyclicPermutations_rotate, cyclicPermutations_inj] section Decidable variable [DecidableEq α] instance isRotatedDecidable (l l' : List α) : Decidable (l ~r l') := decidable_of_iff' _ isRotated_iff_mem_map_range instance {l l' : List α} : Decidable (IsRotated.setoid α l l') := List.isRotatedDecidable _ _ end Decidable end IsRotated end List
Mathlib/Data/List/Rotate.lean
640
648
/- 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.Algebra.Ring.Divisibility.Basic import Mathlib.Data.Ordering.Lemmas import Mathlib.Data.PNat.Basic import Mathlib.SetTheory.Ordinal.Principal import Mathlib.Tactic.NormNum /-! # Ordinal notation Constructive ordinal arithmetic for ordinals below `ε₀`. We define a type `ONote`, with constructors `0 : ONote` and `ONote.oadd e n a` representing `ω ^ e * n + a`. We say that `o` is in Cantor normal form - `ONote.NF o` - if either `o = 0` or `o = ω ^ e * n + a` with `a < ω ^ e` and `a` in Cantor normal form. The type `NONote` is the type of ordinals below `ε₀` in Cantor normal form. Various operations (addition, subtraction, multiplication, exponentiation) are defined on `ONote` and `NONote`. -/ open Ordinal Order -- The generated theorem `ONote.zero.sizeOf_spec` is flagged by `simpNF`, -- and we don't otherwise need it. set_option genSizeOfSpec false in /-- Recursive definition of an ordinal notation. `zero` denotes the ordinal 0, and `oadd e n a` is intended to refer to `ω ^ e * n + a`. For this to be a valid Cantor normal form, we must have the exponents decrease to the right, but we can't state this condition until we've defined `repr`, so we make it a separate definition `NF`. -/ inductive ONote : Type | zero : ONote | oadd : ONote → ℕ+ → ONote → ONote deriving DecidableEq compile_inductive% ONote namespace ONote /-- Notation for 0 -/ instance : Zero ONote := ⟨zero⟩ @[simp] theorem zero_def : zero = 0 := rfl instance : Inhabited ONote := ⟨0⟩ /-- Notation for 1 -/ instance : One ONote := ⟨oadd 0 1 0⟩ /-- Notation for ω -/ def omega : ONote := oadd 1 1 0 /-- The ordinal denoted by a notation -/ noncomputable def repr : ONote → Ordinal.{0} | 0 => 0 | oadd e n a => ω ^ repr e * n + repr a @[simp] theorem repr_zero : repr 0 = 0 := rfl attribute [simp] repr.eq_1 repr.eq_2 /-- Print `ω^s*n`, omitting `s` if `e = 0` or `e = 1`, and omitting `n` if `n = 1` -/ private def toString_aux (e : ONote) (n : ℕ) (s : String) : String := if e = 0 then toString n else (if e = 1 then "ω" else "ω^(" ++ s ++ ")") ++ if n = 1 then "" else "*" ++ toString n /-- Print an ordinal notation -/ def toString : ONote → String | zero => "0" | oadd e n 0 => toString_aux e n (toString e) | oadd e n a => toString_aux e n (toString e) ++ " + " ++ toString a open Lean in /-- Print an ordinal notation -/ def repr' (prec : ℕ) : ONote → Format | zero => "0" | oadd e n a => Repr.addAppParen ("oadd " ++ (repr' max_prec e) ++ " " ++ Nat.repr (n : ℕ) ++ " " ++ (repr' max_prec a)) prec instance : ToString ONote := ⟨toString⟩ instance : Repr ONote where reprPrec o prec := repr' prec o instance : Preorder ONote where le x y := repr x ≤ repr y lt x y := repr x < repr y le_refl _ := @le_refl Ordinal _ _ le_trans _ _ _ := @le_trans Ordinal _ _ _ _ lt_iff_le_not_le _ _ := @lt_iff_le_not_le Ordinal _ _ _ theorem lt_def {x y : ONote} : x < y ↔ repr x < repr y := Iff.rfl theorem le_def {x y : ONote} : x ≤ y ↔ repr x ≤ repr y := Iff.rfl instance : WellFoundedRelation ONote := ⟨(· < ·), InvImage.wf repr Ordinal.lt_wf⟩ /-- Convert a `Nat` into an ordinal -/ @[coe] def ofNat : ℕ → ONote | 0 => 0 | Nat.succ n => oadd 0 n.succPNat 0 -- Porting note (https://github.com/leanprover-community/mathlib4/pull/11467): during the port we marked these lemmas with `@[eqns]` -- to emulate the old Lean 3 behaviour. @[simp] theorem ofNat_zero : ofNat 0 = 0 := rfl @[simp] theorem ofNat_succ (n) : ofNat (Nat.succ n) = oadd 0 n.succPNat 0 := rfl instance (priority := low) nat (n : ℕ) : OfNat ONote n where ofNat := ofNat n @[simp 1200] theorem ofNat_one : ofNat 1 = 1 := rfl @[simp] theorem repr_ofNat (n : ℕ) : repr (ofNat n) = n := by cases n <;> simp @[simp] theorem repr_one : repr 1 = (1 : ℕ) := repr_ofNat 1 theorem omega0_le_oadd (e n a) : ω ^ repr e ≤ repr (oadd e n a) := by refine le_trans ?_ (le_add_right _ _) simpa using (Ordinal.mul_le_mul_iff_left <| opow_pos (repr e) omega0_pos).2 (Nat.cast_le.2 n.2) theorem oadd_pos (e n a) : 0 < oadd e n a := @lt_of_lt_of_le _ _ _ (ω ^ repr e) _ (opow_pos (repr e) omega0_pos) (omega0_le_oadd e n a) /-- Comparison of ordinal notations: `ω ^ e₁ * n₁ + a₁` is less than `ω ^ e₂ * n₂ + a₂` when either `e₁ < e₂`, or `e₁ = e₂` and `n₁ < n₂`, or `e₁ = e₂`, `n₁ = n₂`, and `a₁ < a₂`. -/ def cmp : ONote → ONote → Ordering | 0, 0 => Ordering.eq | _, 0 => Ordering.gt | 0, _ => Ordering.lt | _o₁@(oadd e₁ n₁ a₁), _o₂@(oadd e₂ n₂ a₂) => (cmp e₁ e₂).then <| (_root_.cmp (n₁ : ℕ) n₂).then (cmp a₁ a₂) theorem eq_of_cmp_eq : ∀ {o₁ o₂}, cmp o₁ o₂ = Ordering.eq → o₁ = o₂ | 0, 0, _ => rfl | oadd e n a, 0, h => by injection h | 0, oadd e n a, h => by injection h | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, h => by revert h; simp only [cmp] cases h₁ : cmp e₁ e₂ <;> intro h <;> try cases h obtain rfl := eq_of_cmp_eq h₁ revert h; cases h₂ : _root_.cmp (n₁ : ℕ) n₂ <;> intro h <;> try cases h obtain rfl := eq_of_cmp_eq h rw [_root_.cmp, cmpUsing_eq_eq, not_lt, not_lt, ← le_antisymm_iff] at h₂ obtain rfl := Subtype.eq h₂ simp protected theorem zero_lt_one : (0 : ONote) < 1 := by simp only [lt_def, repr_zero, repr_one, Nat.cast_one, zero_lt_one] /-- `NFBelow o b` says that `o` is a normal form ordinal notation satisfying `repr o < ω ^ b`. -/ inductive NFBelow : ONote → Ordinal.{0} → Prop | zero {b} : NFBelow 0 b
| oadd' {e n a eb b} : NFBelow e eb → NFBelow a (repr e) → repr e < b → NFBelow (oadd e n a) b /-- A normal form ordinal notation has the form `ω ^ a₁ * n₁ + ω ^ a₂ * n₂ + ⋯ + ω ^ aₖ * nₖ` where `a₁ > a₂ > ⋯ > aₖ` and all the `aᵢ` are also in normal form. We will essentially only be interested in normal form ordinal notations, but to avoid complicating the algorithms, we define everything over general ordinal notations and only prove correctness with normal form as an invariant. -/ class NF (o : ONote) : Prop where out : Exists (NFBelow o)
Mathlib/SetTheory/Ordinal/Notation.lean
176
188
/- 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 -/ import Mathlib.Data.Bundle import Mathlib.Data.Set.Image import Mathlib.Topology.CompactOpen import Mathlib.Topology.PartialHomeomorph import Mathlib.Topology.Order.Basic /-! # Trivializations ## Main definitions ### Basic definitions * `Trivialization F p` : structure extending partial homeomorphisms, defining a local trivialization of a topological space `Z` with projection `p` and fiber `F`. * `Pretrivialization F proj` : trivialization as a partial equivalence, mainly used when the topology on the total space has not yet been defined. ### Operations on bundles We provide the following operations on `Trivialization`s. * `Trivialization.compHomeomorph`: given a local trivialization `e` of a fiber bundle `p : Z → B` and a homeomorphism `h : Z' ≃ₜ Z`, returns a local trivialization of the fiber bundle `p ∘ h`. ## Implementation notes Previously, in mathlib, there was a structure `topological_vector_bundle.trivialization` which extended another structure `topological_fiber_bundle.trivialization` by a linearity hypothesis. As of PR https://github.com/leanprover-community/mathlib3/pull/17359, we have changed this to a single structure `Trivialization` (no namespace), together with a mixin class `Trivialization.IsLinear`. This permits all the *data* of a vector bundle to be held at the level of fiber bundles, so that the same trivializations can underlie an object's structure as (say) a vector bundle over `ℂ` and as a vector bundle over `ℝ`, as well as its structure simply as a fiber bundle. This might be a little surprising, given the general trend of the library to ever-increased bundling. But in this case the typical motivation for more bundling does not apply: there is no algebraic or order structure on the whole type of linear (say) trivializations of a bundle. Indeed, since trivializations only have meaning on their base sets (taking junk values outside), the type of linear trivializations is not even particularly well-behaved. -/ open TopologicalSpace Filter Set Bundle Function open scoped Topology variable {B : Type*} (F : Type*) {E : B → Type*} variable {Z : Type*} [TopologicalSpace B] [TopologicalSpace F] {proj : Z → B} /-- This structure contains the information left for a local trivialization (which is implemented below as `Trivialization F proj`) if the total space has not been given a topology, but we have a topology on both the fiber and the base space. Through the construction `topological_fiber_prebundle F proj` it will be possible to promote a `Pretrivialization F proj` to a `Trivialization F proj`. -/ structure Pretrivialization (proj : Z → B) extends PartialEquiv Z (B × F) where open_target : IsOpen target baseSet : Set B open_baseSet : IsOpen baseSet source_eq : source = proj ⁻¹' baseSet target_eq : target = baseSet ×ˢ univ proj_toFun : ∀ p ∈ source, (toFun p).1 = proj p namespace Pretrivialization variable {F} variable (e : Pretrivialization F proj) {x : Z} /-- Coercion of a pretrivialization to a function. We don't use `e.toFun` in the `CoeFun` instance because it is actually `e.toPartialEquiv.toFun`, so `simp` will apply lemmas about `toPartialEquiv`. While we may want to switch to this behavior later, doing it mid-port will break a lot of proofs. -/ @[coe] def toFun' : Z → (B × F) := e.toFun instance : CoeFun (Pretrivialization F proj) fun _ => Z → B × F := ⟨toFun'⟩ @[ext] lemma ext' (e e' : Pretrivialization F proj) (h₁ : e.toPartialEquiv = e'.toPartialEquiv) (h₂ : e.baseSet = e'.baseSet) : e = e' := by cases e; cases e'; congr -- TODO: move `ext` here? lemma ext {e e' : Pretrivialization F proj} (h₁ : ∀ x, e x = e' x) (h₂ : ∀ x, e.toPartialEquiv.symm x = e'.toPartialEquiv.symm x) (h₃ : e.baseSet = e'.baseSet) : e = e' := by ext1 <;> [ext1; exact h₃] · apply h₁ · apply h₂ · rw [e.source_eq, e'.source_eq, h₃] /-- If the fiber is nonempty, then the projection also is. -/ lemma toPartialEquiv_injective [Nonempty F] : Injective (toPartialEquiv : Pretrivialization F proj → PartialEquiv Z (B × F)) := by refine fun e e' h ↦ ext' _ _ h ?_ simpa only [fst_image_prod, univ_nonempty, target_eq] using congr_arg (Prod.fst '' PartialEquiv.target ·) h @[simp, mfld_simps] theorem coe_coe : ⇑e.toPartialEquiv = e := rfl @[simp, mfld_simps] theorem coe_fst (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_toFun x ex theorem mem_source : x ∈ e.source ↔ proj x ∈ e.baseSet := by rw [e.source_eq, mem_preimage] theorem coe_fst' (ex : proj x ∈ e.baseSet) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex) protected theorem eqOn : EqOn (Prod.fst ∘ e) proj e.source := fun _ hx => e.coe_fst hx theorem mk_proj_snd (ex : x ∈ e.source) : (proj x, (e x).2) = e x := Prod.ext (e.coe_fst ex).symm rfl theorem mk_proj_snd' (ex : proj x ∈ e.baseSet) : (proj x, (e x).2) = e x := Prod.ext (e.coe_fst' ex).symm rfl /-- Composition of inverse and coercion from the subtype of the target. -/ def setSymm : e.target → Z := e.target.restrict e.toPartialEquiv.symm theorem mem_target {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.baseSet := by rw [e.target_eq, prod_univ, mem_preimage] theorem proj_symm_apply {x : B × F} (hx : x ∈ e.target) : proj (e.toPartialEquiv.symm x) = x.1 := by have := (e.coe_fst (e.map_target hx)).symm rwa [← e.coe_coe, e.right_inv hx] at this theorem proj_symm_apply' {b : B} {x : F} (hx : b ∈ e.baseSet) : proj (e.toPartialEquiv.symm (b, x)) = b := e.proj_symm_apply (e.mem_target.2 hx) theorem proj_surjOn_baseSet [Nonempty F] : Set.SurjOn proj e.source e.baseSet := fun b hb => let ⟨y⟩ := ‹Nonempty F› ⟨e.toPartialEquiv.symm (b, y), e.toPartialEquiv.map_target <| e.mem_target.2 hb, e.proj_symm_apply' hb⟩
theorem apply_symm_apply {x : B × F} (hx : x ∈ e.target) : e (e.toPartialEquiv.symm x) = x := e.toPartialEquiv.right_inv hx
Mathlib/Topology/FiberBundle/Trivialization.lean
145
147
/- 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.Data.Set.Finite.Basic import Mathlib.Order.Atoms import Mathlib.Order.Grade import Mathlib.Order.Nat /-! # Finsets and multisets form a graded order This file characterises atoms, coatoms and the covering relation in finsets and multisets. It also proves that they form a `ℕ`-graded order. ## Main declarations * `Multiset.instGradeMinOrder_nat`: Multisets are `ℕ`-graded * `Finset.instGradeMinOrder_nat`: Finsets are `ℕ`-graded -/ open Order variable {α : Type*} namespace Multiset variable {s t : Multiset α} {a : α} @[simp] lemma covBy_cons (s : Multiset α) (a : α) : s ⋖ a ::ₘ s := ⟨lt_cons_self _ _, fun t hst hts ↦ (covBy_succ _).2 (card_lt_card hst) <| by simpa using card_lt_card hts⟩ lemma _root_.CovBy.exists_multiset_cons (h : s ⋖ t) : ∃ a, a ::ₘ s = t := (lt_iff_cons_le.1 h.lt).imp fun _a ha ↦ ha.eq_of_not_lt <| h.2 <| lt_cons_self _ _ lemma covBy_iff : s ⋖ t ↔ ∃ a, a ::ₘ s = t := ⟨CovBy.exists_multiset_cons, by rintro ⟨a, rfl⟩; exact covBy_cons _ _⟩
lemma _root_.CovBy.card_multiset (h : s ⋖ t) : card s ⋖ card t := by obtain ⟨a, rfl⟩ := h.exists_multiset_cons; rw [card_cons]; exact covBy_succ _
Mathlib/Data/Finset/Grade.lean
40
41
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import Mathlib.Algebra.ContinuedFractions.Determinant import Mathlib.Algebra.ContinuedFractions.Computation.CorrectnessTerminating import Mathlib.Algebra.Order.Ring.Basic import Mathlib.Data.Nat.Fib.Basic import Mathlib.Tactic.Monotonicity /-! # Approximations for Continued Fraction Computations (`GenContFract.of`) ## Summary This file contains useful approximations for the values involved in the continued fractions computation `GenContFract.of`. In particular, we show that the generalized continued fraction given by `GenContFract.of` in fact is a (regular) continued fraction. Moreover, we derive some upper bounds for the error term when computing a continued fraction up a given position, i.e. bounds for the term `|v - (GenContFract.of v).convs n|`. The derived bounds will show us that the error term indeed gets smaller. As a corollary, we will be able to show that `(GenContFract.of v).convs` converges to `v` in `Algebra.ContinuedFractions.Computation.ApproximationCorollaries`. ## Main Theorems - `GenContFract.of_partNum_eq_one`: shows that all partial numerators `aᵢ` are equal to one. - `GenContFract.exists_int_eq_of_partDen`: shows that all partial denominators `bᵢ` correspond to an integer. - `GenContFract.of_one_le_get?_partDen`: shows that `1 ≤ bᵢ`. - `ContFract.of` returns the regular continued fraction of a value. - `GenContFract.succ_nth_fib_le_of_nthDen`: shows that the `n`th denominator `Bₙ` is greater than or equal to the `n + 1`th fibonacci number `Nat.fib (n + 1)`. - `GenContFract.le_of_succ_get?_den`: shows that `bₙ * Bₙ ≤ Bₙ₊₁`, where `bₙ` is the `n`th partial denominator of the continued fraction. - `GenContFract.abs_sub_convs_le`: shows that `|v - Aₙ / Bₙ| ≤ 1 / (Bₙ * Bₙ₊₁)`, where `Aₙ` is the `n`th partial numerator. ## References - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] -/ open GenContFract open GenContFract (of) open Int variable {K : Type*} {v : K} {n : ℕ} [Field K] [LinearOrder K] [IsStrictOrderedRing K] [FloorRing K] namespace GenContFract namespace IntFractPair /-! We begin with some lemmas about the stream of `IntFractPair`s, which presumably are not of great interest for the end user. -/ /-- Shows that the fractional parts of the stream are in `[0,1)`. -/ theorem nth_stream_fr_nonneg_lt_one {ifp_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr ∧ ifp_n.fr < 1 := by cases n with | zero => have : IntFractPair.of v = ifp_n := by injection nth_stream_eq rw [← this, IntFractPair.of] exact ⟨fract_nonneg _, fract_lt_one _⟩ | succ => rcases succ_nth_stream_eq_some_iff.1 nth_stream_eq with ⟨_, _, _, ifp_of_eq_ifp_n⟩ rw [← ifp_of_eq_ifp_n, IntFractPair.of] exact ⟨fract_nonneg _, fract_lt_one _⟩ /-- Shows that the fractional parts of the stream are nonnegative. -/ theorem nth_stream_fr_nonneg {ifp_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) : 0 ≤ ifp_n.fr := (nth_stream_fr_nonneg_lt_one nth_stream_eq).left /-- Shows that the fractional parts of the stream are smaller than one. -/ theorem nth_stream_fr_lt_one {ifp_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) : ifp_n.fr < 1 := (nth_stream_fr_nonneg_lt_one nth_stream_eq).right /-- Shows that the integer parts of the stream are at least one. -/ theorem one_le_succ_nth_stream_b {ifp_succ_n : IntFractPair K} (succ_nth_stream_eq : IntFractPair.stream v (n + 1) = some ifp_succ_n) : 1 ≤ ifp_succ_n.b := by obtain ⟨ifp_n, nth_stream_eq, stream_nth_fr_ne_zero, ⟨-⟩⟩ : ∃ ifp_n, IntFractPair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0 ∧ IntFractPair.of ifp_n.fr⁻¹ = ifp_succ_n := succ_nth_stream_eq_some_iff.1 succ_nth_stream_eq rw [IntFractPair.of, le_floor, cast_one, one_le_inv₀ ((nth_stream_fr_nonneg nth_stream_eq).lt_of_ne' stream_nth_fr_ne_zero)] exact (nth_stream_fr_lt_one nth_stream_eq).le omit [IsStrictOrderedRing K] in /-- Shows that the `n + 1`th integer part `bₙ₊₁` of the stream is smaller or equal than the inverse of the `n`th fractional part `frₙ` of the stream. This result is straight-forward as `bₙ₊₁` is defined as the floor of `1 / frₙ`. -/ theorem succ_nth_stream_b_le_nth_stream_fr_inv {ifp_n ifp_succ_n : IntFractPair K} (nth_stream_eq : IntFractPair.stream v n = some ifp_n) (succ_nth_stream_eq : IntFractPair.stream v (n + 1) = some ifp_succ_n) : (ifp_succ_n.b : K) ≤ ifp_n.fr⁻¹ := by suffices (⌊ifp_n.fr⁻¹⌋ : K) ≤ ifp_n.fr⁻¹ by obtain ⟨_, ifp_n_fr⟩ := ifp_n have : ifp_n_fr ≠ 0 := by intro h simp [h, IntFractPair.stream, nth_stream_eq] at succ_nth_stream_eq have : IntFractPair.of ifp_n_fr⁻¹ = ifp_succ_n := by simpa [this, IntFractPair.stream, nth_stream_eq, Option.coe_def] using succ_nth_stream_eq rwa [← this] exact floor_le ifp_n.fr⁻¹ end IntFractPair /-! Next we translate above results about the stream of `IntFractPair`s to the computed continued fraction `GenContFract.of`. -/ /-- Shows that the integer parts of the continued fraction are at least one. -/ theorem of_one_le_get?_partDen {b : K} (nth_partDen_eq : (of v).partDens.get? n = some b) : 1 ≤ b := by obtain ⟨gp_n, nth_s_eq, ⟨-⟩⟩ : ∃ gp_n, (of v).s.get? n = some gp_n ∧ gp_n.b = b := exists_s_b_of_partDen nth_partDen_eq obtain ⟨ifp_n, succ_nth_stream_eq, ifp_n_b_eq_gp_n_b⟩ : ∃ ifp, IntFractPair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b := IntFractPair.exists_succ_get?_stream_of_gcf_of_get?_eq_some nth_s_eq rw [← ifp_n_b_eq_gp_n_b] exact mod_cast IntFractPair.one_le_succ_nth_stream_b succ_nth_stream_eq /-- Shows that the partial numerators `aᵢ` of the continued fraction are equal to one and the partial denominators `bᵢ` correspond to integers. -/ theorem of_partNum_eq_one_and_exists_int_partDen_eq {gp : GenContFract.Pair K} (nth_s_eq : (of v).s.get? n = some gp) : gp.a = 1 ∧ ∃ z : ℤ, gp.b = (z : K) := by obtain ⟨ifp, stream_succ_nth_eq, -⟩ : ∃ ifp, IntFractPair.stream v (n + 1) = some ifp ∧ _ := IntFractPair.exists_succ_get?_stream_of_gcf_of_get?_eq_some nth_s_eq have : gp = ⟨1, ifp.b⟩ := by have : (of v).s.get? n = some ⟨1, ifp.b⟩ := get?_of_eq_some_of_succ_get?_intFractPair_stream stream_succ_nth_eq have : some gp = some ⟨1, ifp.b⟩ := by rwa [nth_s_eq] at this injection this simp [this] /-- Shows that the partial numerators `aᵢ` are equal to one. -/ theorem of_partNum_eq_one {a : K} (nth_partNum_eq : (of v).partNums.get? n = some a) : a = 1 := by obtain ⟨gp, nth_s_eq, gp_a_eq_a_n⟩ : ∃ gp, (of v).s.get? n = some gp ∧ gp.a = a := exists_s_a_of_partNum nth_partNum_eq have : gp.a = 1 := (of_partNum_eq_one_and_exists_int_partDen_eq nth_s_eq).left rwa [gp_a_eq_a_n] at this /-- Shows that the partial denominators `bᵢ` correspond to an integer. -/ theorem exists_int_eq_of_partDen {b : K} (nth_partDen_eq : (of v).partDens.get? n = some b) : ∃ z : ℤ, b = (z : K) := by obtain ⟨gp, nth_s_eq, gp_b_eq_b_n⟩ : ∃ gp, (of v).s.get? n = some gp ∧ gp.b = b := exists_s_b_of_partDen nth_partDen_eq have : ∃ z : ℤ, gp.b = (z : K) := (of_partNum_eq_one_and_exists_int_partDen_eq nth_s_eq).right rwa [gp_b_eq_b_n] at this end GenContFract variable (v) theorem GenContFract.of_isSimpContFract : (of v).IsSimpContFract := fun _ _ nth_partNum_eq => of_partNum_eq_one nth_partNum_eq /-- Creates the simple continued fraction of a value. -/ nonrec def SimpContFract.of : SimpContFract K := ⟨of v, GenContFract.of_isSimpContFract v⟩ theorem SimpContFract.of_isContFract : (SimpContFract.of v).IsContFract := fun _ _ nth_partDen_eq => lt_of_lt_of_le zero_lt_one (of_one_le_get?_partDen nth_partDen_eq) /-- Creates the continued fraction of a value. -/ def ContFract.of : ContFract K := ⟨SimpContFract.of v, SimpContFract.of_isContFract v⟩ variable {v} namespace GenContFract /-! One of our next goals is to show that `bₙ * Bₙ ≤ Bₙ₊₁`. For this, we first show that the partial denominators `Bₙ` are bounded from below by the fibonacci sequence `Nat.fib`. This then implies that `0 ≤ Bₙ` and hence `Bₙ₊₂ = bₙ₊₁ * Bₙ₊₁ + Bₙ ≥ bₙ₊₁ * Bₙ₊₁ + 0 = bₙ₊₁ * Bₙ₊₁`. -/ -- open `Nat` as we will make use of fibonacci numbers. open Nat theorem fib_le_of_contsAux_b : n ≤ 1 ∨ ¬(of v).TerminatedAt (n - 2) → (fib n : K) ≤ ((of v).contsAux n).b := Nat.strong_induction_on n (by intro n IH hyp rcases n with (_ | _ | n) · simp [fib_add_two, contsAux] -- case n = 0 · simp [fib_add_two, contsAux] -- case n = 1 · let g := of v -- case 2 ≤ n have : ¬n + 2 ≤ 1 := by omega have not_terminatedAt_n : ¬g.TerminatedAt n := Or.resolve_left hyp this obtain ⟨gp, s_ppred_nth_eq⟩ : ∃ gp, g.s.get? n = some gp := Option.ne_none_iff_exists'.mp not_terminatedAt_n set pconts := g.contsAux (n + 1) with pconts_eq set ppconts := g.contsAux n with ppconts_eq -- use the recurrence of `contsAux` simp only [Nat.succ_eq_add_one, Nat.add_assoc, Nat.reduceAdd] suffices (fib n : K) + fib (n + 1) ≤ gp.a * ppconts.b + gp.b * pconts.b by simpa [g, fib_add_two, add_comm, contsAux_recurrence s_ppred_nth_eq ppconts_eq pconts_eq] -- make use of the fact that `gp.a = 1` suffices (fib n : K) + fib (n + 1) ≤ ppconts.b + gp.b * pconts.b by simpa [of_partNum_eq_one <| partNum_eq_s_a s_ppred_nth_eq] have not_terminatedAt_pred_n : ¬g.TerminatedAt (n - 1) := mt (terminated_stable <| Nat.sub_le n 1) not_terminatedAt_n have not_terminatedAt_ppred_n : ¬TerminatedAt g (n - 2) := mt (terminated_stable (n - 1).pred_le) not_terminatedAt_pred_n -- use the IH to get the inequalities for `pconts` and `ppconts` have ppred_nth_fib_le_ppconts_B : (fib n : K) ≤ ppconts.b := IH n (lt_trans (Nat.lt.base n) <| Nat.lt.base <| n + 1) (Or.inr not_terminatedAt_ppred_n) suffices (fib (n + 1) : K) ≤ gp.b * pconts.b by solve_by_elim [_root_.add_le_add ppred_nth_fib_le_ppconts_B] -- finally use the fact that `1 ≤ gp.b` to solve the goal suffices 1 * (fib (n + 1) : K) ≤ gp.b * pconts.b by rwa [one_mul] at this have one_le_gp_b : (1 : K) ≤ gp.b := of_one_le_get?_partDen (partDen_eq_s_b s_ppred_nth_eq) have : (0 : K) ≤ fib (n + 1) := mod_cast (fib (n + 1)).zero_le have : (0 : K) ≤ gp.b := le_trans zero_le_one one_le_gp_b mono · norm_num · tauto) /-- Shows that the `n`th denominator is greater than or equal to the `n + 1`th fibonacci number, that is `Nat.fib (n + 1) ≤ Bₙ`. -/ theorem succ_nth_fib_le_of_nth_den (hyp : n = 0 ∨ ¬(of v).TerminatedAt (n - 1)) : (fib (n + 1) : K) ≤ (of v).dens n := by rw [den_eq_conts_b, nth_cont_eq_succ_nth_contAux] have : n + 1 ≤ 1 ∨ ¬(of v).TerminatedAt (n - 1) := by cases n with
| zero => exact Or.inl <| le_refl 1 | succ n => exact Or.inr (Or.resolve_left hyp n.succ_ne_zero) exact fib_le_of_contsAux_b this /-! As a simple consequence, we can now derive that all denominators are nonnegative. -/ theorem zero_le_of_contsAux_b : 0 ≤ ((of v).contsAux n).b := by let g := of v induction n with | zero => rfl | succ n IH => rcases Decidable.em <| g.TerminatedAt (n - 1) with terminated | not_terminated · -- terminating case rcases n with - | n · simp [zero_le_one]
Mathlib/Algebra/ContinuedFractions/Computation/Approximations.lean
252
267
/- Copyright (c) 2021 Stuart Presnell. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stuart Presnell -/ import Mathlib.Data.Nat.PrimeFin import Mathlib.Data.Nat.Factorization.Defs import Mathlib.Data.Nat.GCD.BigOperators import Mathlib.Order.Interval.Finset.Nat import Mathlib.Tactic.IntervalCases /-! # Basic lemmas on prime factorizations -/ open Finset List Finsupp namespace Nat variable {a b m n p : ℕ} /-! ### Basic facts about factorization -/ /-! ## Lemmas characterising when `n.factorization p = 0` -/ theorem factorization_eq_zero_of_lt {n p : ℕ} (h : n < p) : n.factorization p = 0 := Finsupp.not_mem_support_iff.mp (mt le_of_mem_primeFactors (not_le_of_lt h)) @[simp] theorem factorization_one_right (n : ℕ) : n.factorization 1 = 0 := factorization_eq_zero_of_non_prime _ not_prime_one theorem dvd_of_factorization_pos {n p : ℕ} (hn : n.factorization p ≠ 0) : p ∣ n := dvd_of_mem_primeFactorsList <| mem_primeFactors_iff_mem_primeFactorsList.1 <| mem_support_iff.2 hn theorem factorization_eq_zero_iff_remainder {p r : ℕ} (i : ℕ) (pp : p.Prime) (hr0 : r ≠ 0) : ¬p ∣ r ↔ (p * i + r).factorization p = 0 := by refine ⟨factorization_eq_zero_of_remainder i, fun h => ?_⟩ rw [factorization_eq_zero_iff] at h contrapose! h refine ⟨pp, ?_, ?_⟩ · rwa [← Nat.dvd_add_iff_right (dvd_mul_right p i)] · contrapose! hr0 exact (add_eq_zero.1 hr0).2 /-- The only numbers with empty prime factorization are `0` and `1` -/ theorem factorization_eq_zero_iff' (n : ℕ) : n.factorization = 0 ↔ n = 0 ∨ n = 1 := by rw [factorization_eq_primeFactorsList_multiset n] simp [factorization, AddEquiv.map_eq_zero_iff, Multiset.coe_eq_zero] /-! ## Lemmas about factorizations of products and powers -/ /-- A product over `n.factorization` can be written as a product over `n.primeFactors`; -/ lemma prod_factorization_eq_prod_primeFactors {β : Type*} [CommMonoid β] (f : ℕ → ℕ → β) : n.factorization.prod f = ∏ p ∈ n.primeFactors, f p (n.factorization p) := rfl /-- A product over `n.primeFactors` can be written as a product over `n.factorization`; -/ lemma prod_primeFactors_prod_factorization {β : Type*} [CommMonoid β] (f : ℕ → β) : ∏ p ∈ n.primeFactors, f p = n.factorization.prod (fun p _ ↦ f p) := rfl /-! ## Lemmas about factorizations of primes and prime powers -/ /-- The multiplicity of prime `p` in `p` is `1` -/ @[simp] theorem Prime.factorization_self {p : ℕ} (hp : Prime p) : p.factorization p = 1 := by simp [hp] /-- If the factorization of `n` contains just one number `p` then `n` is a power of `p` -/ theorem eq_pow_of_factorization_eq_single {n p k : ℕ} (hn : n ≠ 0) (h : n.factorization = Finsupp.single p k) : n = p ^ k := by rw [← Nat.factorization_prod_pow_eq_self hn, h] simp /-- The only prime factor of prime `p` is `p` itself. -/ theorem Prime.eq_of_factorization_pos {p q : ℕ} (hp : Prime p) (h : p.factorization q ≠ 0) : p = q := by simpa [hp.factorization, single_apply] using h /-! ### Equivalence between `ℕ+` and `ℕ →₀ ℕ` with support in the primes. -/ theorem eq_factorization_iff {n : ℕ} {f : ℕ →₀ ℕ} (hn : n ≠ 0) (hf : ∀ p ∈ f.support, Prime p) : f = n.factorization ↔ f.prod (· ^ ·) = n := ⟨fun h => by rw [h, factorization_prod_pow_eq_self hn], fun h => by rw [← h, prod_pow_factorization_eq_self hf]⟩ theorem factorizationEquiv_inv_apply {f : ℕ →₀ ℕ} (hf : ∀ p ∈ f.support, Prime p) : (factorizationEquiv.symm ⟨f, hf⟩).1 = f.prod (· ^ ·) := rfl @[simp] theorem ordProj_of_not_prime (n p : ℕ) (hp : ¬p.Prime) : ordProj[p] n = 1 := by simp [factorization_eq_zero_of_non_prime n hp] @[deprecated (since := "2024-10-24")] alias ord_proj_of_not_prime := ordProj_of_not_prime @[simp] theorem ordCompl_of_not_prime (n p : ℕ) (hp : ¬p.Prime) : ordCompl[p] n = n := by simp [factorization_eq_zero_of_non_prime n hp] @[deprecated (since := "2024-10-24")] alias ord_compl_of_not_prime := ordCompl_of_not_prime theorem ordCompl_dvd (n p : ℕ) : ordCompl[p] n ∣ n := div_dvd_of_dvd (ordProj_dvd n p) @[deprecated (since := "2024-10-24")] alias ord_compl_dvd := ordCompl_dvd theorem ordProj_pos (n p : ℕ) : 0 < ordProj[p] n := by if pp : p.Prime then simp [pow_pos pp.pos] else simp [pp] @[deprecated (since := "2024-10-24")] alias ord_proj_pos := ordProj_pos theorem ordProj_le {n : ℕ} (p : ℕ) (hn : n ≠ 0) : ordProj[p] n ≤ n := le_of_dvd hn.bot_lt (Nat.ordProj_dvd n p) @[deprecated (since := "2024-10-24")] alias ord_proj_le := ordProj_le theorem ordCompl_pos {n : ℕ} (p : ℕ) (hn : n ≠ 0) : 0 < ordCompl[p] n := by if pp : p.Prime then exact Nat.div_pos (ordProj_le p hn) (ordProj_pos n p) else simpa [Nat.factorization_eq_zero_of_non_prime n pp] using hn.bot_lt @[deprecated (since := "2024-10-24")] alias ord_compl_pos := ordCompl_pos theorem ordCompl_le (n p : ℕ) : ordCompl[p] n ≤ n := Nat.div_le_self _ _ @[deprecated (since := "2024-10-24")] alias ord_compl_le := ordCompl_le theorem ordProj_mul_ordCompl_eq_self (n p : ℕ) : ordProj[p] n * ordCompl[p] n = n := Nat.mul_div_cancel' (ordProj_dvd n p) @[deprecated (since := "2024-10-24")] alias ord_proj_mul_ord_compl_eq_self := ordProj_mul_ordCompl_eq_self theorem ordProj_mul {a b : ℕ} (p : ℕ) (ha : a ≠ 0) (hb : b ≠ 0) : ordProj[p] (a * b) = ordProj[p] a * ordProj[p] b := by simp [factorization_mul ha hb, pow_add] @[deprecated (since := "2024-10-24")] alias ord_proj_mul := ordProj_mul theorem ordCompl_mul (a b p : ℕ) : ordCompl[p] (a * b) = ordCompl[p] a * ordCompl[p] b := by if ha : a = 0 then simp [ha] else if hb : b = 0 then simp [hb] else simp only [ordProj_mul p ha hb] rw [div_mul_div_comm (ordProj_dvd a p) (ordProj_dvd b p)] @[deprecated (since := "2024-10-24")] alias ord_compl_mul := ordCompl_mul /-! ### Factorization and divisibility -/ /-- A crude upper bound on `n.factorization p` -/ theorem factorization_lt {n : ℕ} (p : ℕ) (hn : n ≠ 0) : n.factorization p < n := by by_cases pp : p.Prime · exact (Nat.pow_lt_pow_iff_right pp.one_lt).1 <| (ordProj_le p hn).trans_lt <| Nat.lt_pow_self pp.one_lt · simpa only [factorization_eq_zero_of_non_prime n pp] using hn.bot_lt /-- An upper bound on `n.factorization p` -/ theorem factorization_le_of_le_pow {n p b : ℕ} (hb : n ≤ p ^ b) : n.factorization p ≤ b := by if hn : n = 0 then simp [hn] else if pp : p.Prime then exact (Nat.pow_le_pow_iff_right pp.one_lt).1 ((ordProj_le p hn).trans hb) else simp [factorization_eq_zero_of_non_prime n pp] theorem factorization_prime_le_iff_dvd {d n : ℕ} (hd : d ≠ 0) (hn : n ≠ 0) : (∀ p : ℕ, p.Prime → d.factorization p ≤ n.factorization p) ↔ d ∣ n := by rw [← factorization_le_iff_dvd hd hn] refine ⟨fun h p => (em p.Prime).elim (h p) fun hp => ?_, fun h p _ => h p⟩ simp_rw [factorization_eq_zero_of_non_prime _ hp] rfl theorem factorization_le_factorization_mul_left {a b : ℕ} (hb : b ≠ 0) : a.factorization ≤ (a * b).factorization := by rcases eq_or_ne a 0 with (rfl | ha) · simp rw [factorization_le_iff_dvd ha <| mul_ne_zero ha hb] exact Dvd.intro b rfl theorem factorization_le_factorization_mul_right {a b : ℕ} (ha : a ≠ 0) : b.factorization ≤ (a * b).factorization := by rw [mul_comm] apply factorization_le_factorization_mul_left ha theorem Prime.pow_dvd_iff_le_factorization {p k n : ℕ} (pp : Prime p) (hn : n ≠ 0) : p ^ k ∣ n ↔ k ≤ n.factorization p := by rw [← factorization_le_iff_dvd (pow_pos pp.pos k).ne' hn, pp.factorization_pow, single_le_iff] theorem Prime.pow_dvd_iff_dvd_ordProj {p k n : ℕ} (pp : Prime p) (hn : n ≠ 0) : p ^ k ∣ n ↔ p ^ k ∣ ordProj[p] n := by rw [pow_dvd_pow_iff_le_right pp.one_lt, pp.pow_dvd_iff_le_factorization hn] @[deprecated (since := "2024-10-24")] alias Prime.pow_dvd_iff_dvd_ord_proj := Prime.pow_dvd_iff_dvd_ordProj theorem Prime.dvd_iff_one_le_factorization {p n : ℕ} (pp : Prime p) (hn : n ≠ 0) : p ∣ n ↔ 1 ≤ n.factorization p := Iff.trans (by simp) (pp.pow_dvd_iff_le_factorization hn) theorem exists_factorization_lt_of_lt {a b : ℕ} (ha : a ≠ 0) (hab : a < b) : ∃ p : ℕ, a.factorization p < b.factorization p := by have hb : b ≠ 0 := (ha.bot_lt.trans hab).ne' contrapose! hab rw [← Finsupp.le_def, factorization_le_iff_dvd hb ha] at hab exact le_of_dvd ha.bot_lt hab @[simp] theorem factorization_div {d n : ℕ} (h : d ∣ n) : (n / d).factorization = n.factorization - d.factorization := by rcases eq_or_ne d 0 with (rfl | hd); · simp [zero_dvd_iff.mp h] rcases eq_or_ne n 0 with (rfl | hn); · simp [tsub_eq_zero_of_le] apply add_left_injective d.factorization simp only rw [tsub_add_cancel_of_le <| (Nat.factorization_le_iff_dvd hd hn).mpr h, ← Nat.factorization_mul (Nat.div_pos (Nat.le_of_dvd hn.bot_lt h) hd.bot_lt).ne' hd, Nat.div_mul_cancel h] theorem dvd_ordProj_of_dvd {n p : ℕ} (hn : n ≠ 0) (pp : p.Prime) (h : p ∣ n) : p ∣ ordProj[p] n := dvd_pow_self p (Prime.factorization_pos_of_dvd pp hn h).ne' @[deprecated (since := "2024-10-24")] alias dvd_ord_proj_of_dvd := dvd_ordProj_of_dvd theorem not_dvd_ordCompl {n p : ℕ} (hp : Prime p) (hn : n ≠ 0) : ¬p ∣ ordCompl[p] n := by rw [Nat.Prime.dvd_iff_one_le_factorization hp (ordCompl_pos p hn).ne'] rw [Nat.factorization_div (Nat.ordProj_dvd n p)] simp [hp.factorization] @[deprecated (since := "2024-10-24")] alias not_dvd_ord_compl := not_dvd_ordCompl theorem coprime_ordCompl {n p : ℕ} (hp : Prime p) (hn : n ≠ 0) : Coprime p (ordCompl[p] n) := (or_iff_left (not_dvd_ordCompl hp hn)).mp <| coprime_or_dvd_of_prime hp _ @[deprecated (since := "2024-10-24")] alias coprime_ord_compl := coprime_ordCompl theorem factorization_ordCompl (n p : ℕ) : (ordCompl[p] n).factorization = n.factorization.erase p := by if hn : n = 0 then simp [hn] else if pp : p.Prime then ?_ else simp [pp] ext q rcases eq_or_ne q p with (rfl | hqp) · simp only [Finsupp.erase_same, factorization_eq_zero_iff, not_dvd_ordCompl pp hn] simp · rw [Finsupp.erase_ne hqp, factorization_div (ordProj_dvd n p)] simp [pp.factorization, hqp.symm] @[deprecated (since := "2024-10-24")] alias factorization_ord_compl := factorization_ordCompl -- `ordCompl[p] n` is the largest divisor of `n` not divisible by `p`. theorem dvd_ordCompl_of_dvd_not_dvd {p d n : ℕ} (hdn : d ∣ n) (hpd : ¬p ∣ d) :
d ∣ ordCompl[p] n := by
Mathlib/Data/Nat/Factorization/Basic.lean
253
253
/- 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, Yaël Dillies -/ import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.Data.Set.MulAntidiagonal import Mathlib.Algebra.Group.Pointwise.Set.Basic /-! # Multiplication antidiagonal as a `Finset`. We construct the `Finset` of all pairs of an element in `s` and an element in `t` that multiply to `a`, given that `s` and `t` are well-ordered. -/ namespace Set open Pointwise variable {α : Type*} {s t : Set α} @[to_additive] theorem IsPWO.mul [CommMonoid α] [PartialOrder α] [IsOrderedCancelMonoid α] (hs : s.IsPWO) (ht : t.IsPWO) : IsPWO (s * t) := by rw [← image_mul_prod] exact (hs.prod ht).image_of_monotone (monotone_fst.mul' monotone_snd) variable [CommMonoid α] [LinearOrder α] [IsOrderedCancelMonoid α] @[to_additive] theorem IsWF.mul (hs : s.IsWF) (ht : t.IsWF) : IsWF (s * t) := (hs.isPWO.mul ht.isPWO).isWF @[to_additive] theorem IsWF.min_mul (hs : s.IsWF) (ht : t.IsWF) (hsn : s.Nonempty) (htn : t.Nonempty) : (hs.mul ht).min (hsn.mul htn) = hs.min hsn * ht.min htn := by refine le_antisymm (IsWF.min_le _ _ (mem_mul.2 ⟨_, hs.min_mem _, _, ht.min_mem _, rfl⟩)) ?_ rw [IsWF.le_min_iff] rintro _ ⟨x, hx, y, hy, rfl⟩ exact mul_le_mul' (hs.min_le _ hx) (ht.min_le _ hy) end Set namespace Finset open Pointwise variable {α : Type*} variable [CommMonoid α] [PartialOrder α] [IsOrderedCancelMonoid α] {s t : Set α} (hs : s.IsPWO) (ht : t.IsPWO) (a : α) /-- `Finset.mulAntidiagonal hs ht a` is the set of all pairs of an element in `s` and an element in `t` that multiply to `a`, but its construction requires proofs that `s` and `t` are well-ordered. -/ @[to_additive "`Finset.addAntidiagonal hs ht a` is the set of all pairs of an element in `s` and an element in `t` that add to `a`, but its construction requires proofs that `s` and `t` are well-ordered."] noncomputable def mulAntidiagonal : Finset (α × α) := (Set.MulAntidiagonal.finite_of_isPWO hs ht a).toFinset variable {hs ht a} {u : Set α} {hu : u.IsPWO} {x : α × α} @[to_additive (attr := simp)] theorem mem_mulAntidiagonal : x ∈ mulAntidiagonal hs ht a ↔ x.1 ∈ s ∧ x.2 ∈ t ∧ x.1 * x.2 = a := by simp only [mulAntidiagonal, Set.Finite.mem_toFinset, Set.mem_mulAntidiagonal] @[to_additive] theorem mulAntidiagonal_mono_left (h : u ⊆ s) : mulAntidiagonal hu ht a ⊆ mulAntidiagonal hs ht a := Set.Finite.toFinset_mono <| Set.mulAntidiagonal_mono_left h
@[to_additive] theorem mulAntidiagonal_mono_right (h : u ⊆ t) :
Mathlib/Data/Finset/MulAntidiagonal.lean
72
73
/- Copyright (c) 2019 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo, Yaël Dillies, Moritz Doll -/ import Mathlib.Algebra.Order.Pi import Mathlib.Analysis.Convex.Function import Mathlib.Analysis.LocallyConvex.Basic import Mathlib.Data.Real.Pointwise /-! # Seminorms This file defines seminorms. A seminorm is a function to the reals which is positive-semidefinite, absolutely homogeneous, and subadditive. They are closely related to convex sets, and a topological vector space is locally convex if and only if its topology is induced by a family of seminorms. ## Main declarations For a module over a normed ring: * `Seminorm`: A function to the reals that is positive-semidefinite, absolutely homogeneous, and subadditive. * `normSeminorm 𝕜 E`: The norm on `E` as a seminorm. ## References * [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966] ## Tags seminorm, locally convex, LCTVS -/ assert_not_exists balancedCore open NormedField Set Filter open scoped NNReal Pointwise Topology Uniformity variable {R R' 𝕜 𝕜₂ 𝕜₃ 𝕝 E E₂ E₃ F ι : Type*} /-- A seminorm on a module over a normed ring is a function to the reals that is positive semidefinite, positive homogeneous, and subadditive. -/ structure Seminorm (𝕜 : Type*) (E : Type*) [SeminormedRing 𝕜] [AddGroup E] [SMul 𝕜 E] extends AddGroupSeminorm E where /-- The seminorm of a scalar multiplication is the product of the absolute value of the scalar and the original seminorm. -/ smul' : ∀ (a : 𝕜) (x : E), toFun (a • x) = ‖a‖ * toFun x attribute [nolint docBlame] Seminorm.toAddGroupSeminorm /-- `SeminormClass F 𝕜 E` states that `F` is a type of seminorms on the `𝕜`-module `E`. You should extend this class when you extend `Seminorm`. -/ class SeminormClass (F : Type*) (𝕜 E : outParam Type*) [SeminormedRing 𝕜] [AddGroup E] [SMul 𝕜 E] [FunLike F E ℝ] : Prop extends AddGroupSeminormClass F E ℝ where /-- The seminorm of a scalar multiplication is the product of the absolute value of the scalar and the original seminorm. -/ map_smul_eq_mul (f : F) (a : 𝕜) (x : E) : f (a • x) = ‖a‖ * f x export SeminormClass (map_smul_eq_mul) section Of /-- Alternative constructor for a `Seminorm` on an `AddCommGroup E` that is a module over a `SeminormedRing 𝕜`. -/ def Seminorm.of [SeminormedRing 𝕜] [AddCommGroup E] [Module 𝕜 E] (f : E → ℝ) (add_le : ∀ x y : E, f (x + y) ≤ f x + f y) (smul : ∀ (a : 𝕜) (x : E), f (a • x) = ‖a‖ * f x) : Seminorm 𝕜 E where toFun := f map_zero' := by rw [← zero_smul 𝕜 (0 : E), smul, norm_zero, zero_mul] add_le' := add_le smul' := smul neg' x := by rw [← neg_one_smul 𝕜, smul, norm_neg, ← smul, one_smul] /-- Alternative constructor for a `Seminorm` over a normed field `𝕜` that only assumes `f 0 = 0` and an inequality for the scalar multiplication. -/ def Seminorm.ofSMulLE [NormedField 𝕜] [AddCommGroup E] [Module 𝕜 E] (f : E → ℝ) (map_zero : f 0 = 0) (add_le : ∀ x y, f (x + y) ≤ f x + f y) (smul_le : ∀ (r : 𝕜) (x), f (r • x) ≤ ‖r‖ * f x) : Seminorm 𝕜 E := Seminorm.of f add_le fun r x => by refine le_antisymm (smul_le r x) ?_ by_cases h : r = 0 · simp [h, map_zero] rw [← mul_le_mul_left (inv_pos.mpr (norm_pos_iff.mpr h))] rw [inv_mul_cancel_left₀ (norm_ne_zero_iff.mpr h)] specialize smul_le r⁻¹ (r • x) rw [norm_inv] at smul_le convert smul_le simp [h] end Of namespace Seminorm section SeminormedRing variable [SeminormedRing 𝕜] section AddGroup variable [AddGroup E] section SMul variable [SMul 𝕜 E] instance instFunLike : FunLike (Seminorm 𝕜 E) E ℝ where coe f := f.toFun coe_injective' f g h := by rcases f with ⟨⟨_⟩⟩ rcases g with ⟨⟨_⟩⟩ congr instance instSeminormClass : SeminormClass (Seminorm 𝕜 E) 𝕜 E where map_zero f := f.map_zero' map_add_le_add f := f.add_le' map_neg_eq_map f := f.neg' map_smul_eq_mul f := f.smul' @[ext] theorem ext {p q : Seminorm 𝕜 E} (h : ∀ x, (p : E → ℝ) x = q x) : p = q := DFunLike.ext p q h instance instZero : Zero (Seminorm 𝕜 E) := ⟨{ AddGroupSeminorm.instZeroAddGroupSeminorm.zero with smul' := fun _ _ => (mul_zero _).symm }⟩ @[simp] theorem coe_zero : ⇑(0 : Seminorm 𝕜 E) = 0 := rfl @[simp] theorem zero_apply (x : E) : (0 : Seminorm 𝕜 E) x = 0 := rfl instance : Inhabited (Seminorm 𝕜 E) := ⟨0⟩ variable (p : Seminorm 𝕜 E) (x : E) (r : ℝ) /-- Any action on `ℝ` which factors through `ℝ≥0` applies to a seminorm. -/ instance instSMul [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : SMul R (Seminorm 𝕜 E) where smul r p := { r • p.toAddGroupSeminorm with toFun := fun x => r • p x smul' := fun _ _ => by simp only [← smul_one_smul ℝ≥0 r (_ : ℝ), NNReal.smul_def, smul_eq_mul] rw [map_smul_eq_mul, mul_left_comm] } instance [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] [SMul R' ℝ] [SMul R' ℝ≥0] [IsScalarTower R' ℝ≥0 ℝ] [SMul R R'] [IsScalarTower R R' ℝ] : IsScalarTower R R' (Seminorm 𝕜 E) where smul_assoc r a p := ext fun x => smul_assoc r a (p x) theorem coe_smul [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p : Seminorm 𝕜 E) : ⇑(r • p) = r • ⇑p := rfl @[simp] theorem smul_apply [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p : Seminorm 𝕜 E) (x : E) : (r • p) x = r • p x := rfl instance instAdd : Add (Seminorm 𝕜 E) where add p q := { p.toAddGroupSeminorm + q.toAddGroupSeminorm with toFun := fun x => p x + q x smul' := fun a x => by simp only [map_smul_eq_mul, map_smul_eq_mul, mul_add] } theorem coe_add (p q : Seminorm 𝕜 E) : ⇑(p + q) = p + q := rfl @[simp] theorem add_apply (p q : Seminorm 𝕜 E) (x : E) : (p + q) x = p x + q x := rfl instance instAddMonoid : AddMonoid (Seminorm 𝕜 E) := DFunLike.coe_injective.addMonoid _ rfl coe_add fun _ _ => by rfl instance instAddCommMonoid : AddCommMonoid (Seminorm 𝕜 E) := DFunLike.coe_injective.addCommMonoid _ rfl coe_add fun _ _ => by rfl instance instPartialOrder : PartialOrder (Seminorm 𝕜 E) := PartialOrder.lift _ DFunLike.coe_injective instance instIsOrderedCancelAddMonoid : IsOrderedCancelAddMonoid (Seminorm 𝕜 E) := DFunLike.coe_injective.isOrderedCancelAddMonoid _ rfl coe_add fun _ _ => rfl instance instMulAction [Monoid R] [MulAction R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : MulAction R (Seminorm 𝕜 E) := DFunLike.coe_injective.mulAction _ (by intros; rfl) variable (𝕜 E) /-- `coeFn` as an `AddMonoidHom`. Helper definition for showing that `Seminorm 𝕜 E` is a module. -/ @[simps] def coeFnAddMonoidHom : AddMonoidHom (Seminorm 𝕜 E) (E → ℝ) where toFun := (↑) map_zero' := coe_zero map_add' := coe_add theorem coeFnAddMonoidHom_injective : Function.Injective (coeFnAddMonoidHom 𝕜 E) := show @Function.Injective (Seminorm 𝕜 E) (E → ℝ) (↑) from DFunLike.coe_injective variable {𝕜 E} instance instDistribMulAction [Monoid R] [DistribMulAction R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : DistribMulAction R (Seminorm 𝕜 E) := (coeFnAddMonoidHom_injective 𝕜 E).distribMulAction _ (by intros; rfl) instance instModule [Semiring R] [Module R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : Module R (Seminorm 𝕜 E) := (coeFnAddMonoidHom_injective 𝕜 E).module R _ (by intros; rfl) instance instSup : Max (Seminorm 𝕜 E) where max p q := { p.toAddGroupSeminorm ⊔ q.toAddGroupSeminorm with toFun := p ⊔ q smul' := fun x v => (congr_arg₂ max (map_smul_eq_mul p x v) (map_smul_eq_mul q x v)).trans <| (mul_max_of_nonneg _ _ <| norm_nonneg x).symm } @[simp] theorem coe_sup (p q : Seminorm 𝕜 E) : ⇑(p ⊔ q) = (p : E → ℝ) ⊔ (q : E → ℝ) := rfl theorem sup_apply (p q : Seminorm 𝕜 E) (x : E) : (p ⊔ q) x = p x ⊔ q x := rfl theorem smul_sup [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p q : Seminorm 𝕜 E) : r • (p ⊔ q) = r • p ⊔ r • q := have real.smul_max : ∀ x y : ℝ, r • max x y = max (r • x) (r • y) := fun x y => by simpa only [← smul_eq_mul, ← NNReal.smul_def, smul_one_smul ℝ≥0 r (_ : ℝ)] using mul_max_of_nonneg x y (r • (1 : ℝ≥0) : ℝ≥0).coe_nonneg ext fun _ => real.smul_max _ _ @[simp, norm_cast] theorem coe_le_coe {p q : Seminorm 𝕜 E} : (p : E → ℝ) ≤ q ↔ p ≤ q := Iff.rfl @[simp, norm_cast] theorem coe_lt_coe {p q : Seminorm 𝕜 E} : (p : E → ℝ) < q ↔ p < q := Iff.rfl theorem le_def {p q : Seminorm 𝕜 E} : p ≤ q ↔ ∀ x, p x ≤ q x := Iff.rfl theorem lt_def {p q : Seminorm 𝕜 E} : p < q ↔ p ≤ q ∧ ∃ x, p x < q x := @Pi.lt_def _ _ _ p q instance instSemilatticeSup : SemilatticeSup (Seminorm 𝕜 E) := Function.Injective.semilatticeSup _ DFunLike.coe_injective coe_sup end SMul end AddGroup section Module variable [SeminormedRing 𝕜₂] [SeminormedRing 𝕜₃] variable {σ₁₂ : 𝕜 →+* 𝕜₂} [RingHomIsometric σ₁₂] variable {σ₂₃ : 𝕜₂ →+* 𝕜₃} [RingHomIsometric σ₂₃] variable {σ₁₃ : 𝕜 →+* 𝕜₃} [RingHomIsometric σ₁₃] variable [AddCommGroup E] [AddCommGroup E₂] [AddCommGroup E₃] variable [Module 𝕜 E] [Module 𝕜₂ E₂] [Module 𝕜₃ E₃] variable [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] /-- Composition of a seminorm with a linear map is a seminorm. -/ def comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) : Seminorm 𝕜 E := { p.toAddGroupSeminorm.comp f.toAddMonoidHom with toFun := fun x => p (f x) -- Porting note: the `simp only` below used to be part of the `rw`. -- I'm not sure why this change was needed, and am worried by it! -- Note: https://github.com/leanprover-community/mathlib4/pull/8386 had to change `map_smulₛₗ` to `map_smulₛₗ _` smul' := fun _ _ => by simp only [map_smulₛₗ _]; rw [map_smul_eq_mul, RingHomIsometric.is_iso] } theorem coe_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) : ⇑(p.comp f) = p ∘ f := rfl @[simp] theorem comp_apply (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (x : E) : (p.comp f) x = p (f x) := rfl @[simp] theorem comp_id (p : Seminorm 𝕜 E) : p.comp LinearMap.id = p := ext fun _ => rfl @[simp] theorem comp_zero (p : Seminorm 𝕜₂ E₂) : p.comp (0 : E →ₛₗ[σ₁₂] E₂) = 0 := ext fun _ => map_zero p @[simp] theorem zero_comp (f : E →ₛₗ[σ₁₂] E₂) : (0 : Seminorm 𝕜₂ E₂).comp f = 0 := ext fun _ => rfl theorem comp_comp [RingHomCompTriple σ₁₂ σ₂₃ σ₁₃] (p : Seminorm 𝕜₃ E₃) (g : E₂ →ₛₗ[σ₂₃] E₃) (f : E →ₛₗ[σ₁₂] E₂) : p.comp (g.comp f) = (p.comp g).comp f := ext fun _ => rfl theorem add_comp (p q : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) : (p + q).comp f = p.comp f + q.comp f := ext fun _ => rfl theorem comp_add_le (p : Seminorm 𝕜₂ E₂) (f g : E →ₛₗ[σ₁₂] E₂) : p.comp (f + g) ≤ p.comp f + p.comp g := fun _ => map_add_le_add p _ _ theorem smul_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (c : R) : (c • p).comp f = c • p.comp f := ext fun _ => rfl theorem comp_mono {p q : Seminorm 𝕜₂ E₂} (f : E →ₛₗ[σ₁₂] E₂) (hp : p ≤ q) : p.comp f ≤ q.comp f := fun _ => hp _ /-- The composition as an `AddMonoidHom`. -/ @[simps] def pullback (f : E →ₛₗ[σ₁₂] E₂) : Seminorm 𝕜₂ E₂ →+ Seminorm 𝕜 E where toFun := fun p => p.comp f map_zero' := zero_comp f map_add' := fun p q => add_comp p q f instance instOrderBot : OrderBot (Seminorm 𝕜 E) where bot := 0 bot_le := apply_nonneg @[simp] theorem coe_bot : ⇑(⊥ : Seminorm 𝕜 E) = 0 := rfl theorem bot_eq_zero : (⊥ : Seminorm 𝕜 E) = 0 := rfl theorem smul_le_smul {p q : Seminorm 𝕜 E} {a b : ℝ≥0} (hpq : p ≤ q) (hab : a ≤ b) : a • p ≤ b • q := by simp_rw [le_def] intro x exact mul_le_mul hab (hpq x) (apply_nonneg p x) (NNReal.coe_nonneg b) theorem finset_sup_apply (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) : s.sup p x = ↑(s.sup fun i => ⟨p i x, apply_nonneg (p i) x⟩ : ℝ≥0) := by induction' s using Finset.cons_induction_on with a s ha ih · rw [Finset.sup_empty, Finset.sup_empty, coe_bot, _root_.bot_eq_zero, Pi.zero_apply] norm_cast · rw [Finset.sup_cons, Finset.sup_cons, coe_sup, Pi.sup_apply, NNReal.coe_max, NNReal.coe_mk, ih] theorem exists_apply_eq_finset_sup (p : ι → Seminorm 𝕜 E) {s : Finset ι} (hs : s.Nonempty) (x : E) : ∃ i ∈ s, s.sup p x = p i x := by rcases Finset.exists_mem_eq_sup s hs (fun i ↦ (⟨p i x, apply_nonneg _ _⟩ : ℝ≥0)) with ⟨i, hi, hix⟩ rw [finset_sup_apply] exact ⟨i, hi, congr_arg _ hix⟩ theorem zero_or_exists_apply_eq_finset_sup (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) : s.sup p x = 0 ∨ ∃ i ∈ s, s.sup p x = p i x := by rcases Finset.eq_empty_or_nonempty s with (rfl|hs) · left; rfl · right; exact exists_apply_eq_finset_sup p hs x theorem finset_sup_smul (p : ι → Seminorm 𝕜 E) (s : Finset ι) (C : ℝ≥0) : s.sup (C • p) = C • s.sup p := by ext x rw [smul_apply, finset_sup_apply, finset_sup_apply] symm exact congr_arg ((↑) : ℝ≥0 → ℝ) (NNReal.mul_finset_sup C s (fun i ↦ ⟨p i x, apply_nonneg _ _⟩)) theorem finset_sup_le_sum (p : ι → Seminorm 𝕜 E) (s : Finset ι) : s.sup p ≤ ∑ i ∈ s, p i := by classical refine Finset.sup_le_iff.mpr ?_ intro i hi rw [Finset.sum_eq_sum_diff_singleton_add hi, le_add_iff_nonneg_left] exact bot_le theorem finset_sup_apply_le {p : ι → Seminorm 𝕜 E} {s : Finset ι} {x : E} {a : ℝ} (ha : 0 ≤ a) (h : ∀ i, i ∈ s → p i x ≤ a) : s.sup p x ≤ a := by lift a to ℝ≥0 using ha rw [finset_sup_apply, NNReal.coe_le_coe] exact Finset.sup_le h theorem le_finset_sup_apply {p : ι → Seminorm 𝕜 E} {s : Finset ι} {x : E} {i : ι} (hi : i ∈ s) : p i x ≤ s.sup p x := (Finset.le_sup hi : p i ≤ s.sup p) x theorem finset_sup_apply_lt {p : ι → Seminorm 𝕜 E} {s : Finset ι} {x : E} {a : ℝ} (ha : 0 < a) (h : ∀ i, i ∈ s → p i x < a) : s.sup p x < a := by lift a to ℝ≥0 using ha.le rw [finset_sup_apply, NNReal.coe_lt_coe, Finset.sup_lt_iff] · exact h · exact NNReal.coe_pos.mpr ha theorem norm_sub_map_le_sub (p : Seminorm 𝕜 E) (x y : E) : ‖p x - p y‖ ≤ p (x - y) := abs_sub_map_le_sub p x y end Module end SeminormedRing section SeminormedCommRing variable [SeminormedRing 𝕜] [SeminormedCommRing 𝕜₂] variable {σ₁₂ : 𝕜 →+* 𝕜₂} [RingHomIsometric σ₁₂] variable [AddCommGroup E] [AddCommGroup E₂] [Module 𝕜 E] [Module 𝕜₂ E₂] theorem comp_smul (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (c : 𝕜₂) : p.comp (c • f) = ‖c‖₊ • p.comp f := ext fun _ => by rw [comp_apply, smul_apply, LinearMap.smul_apply, map_smul_eq_mul, NNReal.smul_def, coe_nnnorm, smul_eq_mul, comp_apply] theorem comp_smul_apply (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (c : 𝕜₂) (x : E) : p.comp (c • f) x = ‖c‖ * p (f x) := map_smul_eq_mul p _ _ end SeminormedCommRing section NormedField variable [NormedField 𝕜] [AddCommGroup E] [Module 𝕜 E] {p q : Seminorm 𝕜 E} {x : E} /-- Auxiliary lemma to show that the infimum of seminorms is well-defined. -/ theorem bddBelow_range_add : BddBelow (range fun u => p u + q (x - u)) := ⟨0, by rintro _ ⟨x, rfl⟩ dsimp; positivity⟩ noncomputable instance instInf : Min (Seminorm 𝕜 E) where min p q := { p.toAddGroupSeminorm ⊓ q.toAddGroupSeminorm with toFun := fun x => ⨅ u : E, p u + q (x - u) smul' := by intro a x obtain rfl | ha := eq_or_ne a 0 · rw [norm_zero, zero_mul, zero_smul] refine ciInf_eq_of_forall_ge_of_forall_gt_exists_lt (fun i => by positivity) fun x hx => ⟨0, by rwa [map_zero, sub_zero, map_zero, add_zero]⟩ simp_rw [Real.mul_iInf_of_nonneg (norm_nonneg a), mul_add, ← map_smul_eq_mul p, ← map_smul_eq_mul q, smul_sub] refine Function.Surjective.iInf_congr ((a⁻¹ • ·) : E → E) (fun u => ⟨a • u, inv_smul_smul₀ ha u⟩) fun u => ?_ rw [smul_inv_smul₀ ha] } @[simp] theorem inf_apply (p q : Seminorm 𝕜 E) (x : E) : (p ⊓ q) x = ⨅ u : E, p u + q (x - u) := rfl noncomputable instance instLattice : Lattice (Seminorm 𝕜 E) := { Seminorm.instSemilatticeSup with inf := (· ⊓ ·) inf_le_left := fun p q x => ciInf_le_of_le bddBelow_range_add x <| by simp only [sub_self, map_zero, add_zero]; rfl inf_le_right := fun p q x => ciInf_le_of_le bddBelow_range_add 0 <| by simp only [sub_self, map_zero, zero_add, sub_zero]; rfl le_inf := fun a _ _ hab hac _ => le_ciInf fun _ => (le_map_add_map_sub a _ _).trans <| add_le_add (hab _) (hac _) } theorem smul_inf [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p q : Seminorm 𝕜 E) : r • (p ⊓ q) = r • p ⊓ r • q := by ext simp_rw [smul_apply, inf_apply, smul_apply, ← smul_one_smul ℝ≥0 r (_ : ℝ), NNReal.smul_def, smul_eq_mul, Real.mul_iInf_of_nonneg (NNReal.coe_nonneg _), mul_add] section Classical open Classical in /-- We define the supremum of an arbitrary subset of `Seminorm 𝕜 E` as follows: * if `s` is `BddAbove` *as a set of functions `E → ℝ`* (that is, if `s` is pointwise bounded above), we take the pointwise supremum of all elements of `s`, and we prove that it is indeed a seminorm. * otherwise, we take the zero seminorm `⊥`. There are two things worth mentioning here: * First, it is not trivial at first that `s` being bounded above *by a function* implies being bounded above *as a seminorm*. We show this in `Seminorm.bddAbove_iff` by using that the `Sup s` as defined here is then a bounding seminorm for `s`. So it is important to make the case disjunction on `BddAbove ((↑) '' s : Set (E → ℝ))` and not `BddAbove s`. * Since the pointwise `Sup` already gives `0` at points where a family of functions is not bounded above, one could hope that just using the pointwise `Sup` would work here, without the need for an additional case disjunction. As discussed on Zulip, this doesn't work because this can give a function which does *not* satisfy the seminorm axioms (typically sub-additivity). -/ noncomputable instance instSupSet : SupSet (Seminorm 𝕜 E) where sSup s := if h : BddAbove ((↑) '' s : Set (E → ℝ)) then { toFun := ⨆ p : s, ((p : Seminorm 𝕜 E) : E → ℝ) map_zero' := by rw [iSup_apply, ← @Real.iSup_const_zero s] congr! rename_i _ _ _ i exact map_zero i.1 add_le' := fun x y => by rcases h with ⟨q, hq⟩ obtain rfl | h := s.eq_empty_or_nonempty · simp [Real.iSup_of_isEmpty] haveI : Nonempty ↑s := h.coe_sort simp only [iSup_apply] refine ciSup_le fun i => ((i : Seminorm 𝕜 E).add_le' x y).trans <| add_le_add -- Porting note: `f` is provided to force `Subtype.val` to appear. -- A type ascription on `_` would have also worked, but would have been more verbose. (le_ciSup (f := fun i => (Subtype.val i : Seminorm 𝕜 E).toFun x) ⟨q x, ?_⟩ i) (le_ciSup (f := fun i => (Subtype.val i : Seminorm 𝕜 E).toFun y) ⟨q y, ?_⟩ i) <;> rw [mem_upperBounds, forall_mem_range] <;> exact fun j => hq (mem_image_of_mem _ j.2) _ neg' := fun x => by simp only [iSup_apply] congr! 2 rename_i _ _ _ i exact i.1.neg' _ smul' := fun a x => by simp only [iSup_apply] rw [← smul_eq_mul, Real.smul_iSup_of_nonneg (norm_nonneg a) fun i : s => (i : Seminorm 𝕜 E) x] congr! rename_i _ _ _ i exact i.1.smul' a x } else ⊥ protected theorem coe_sSup_eq' {s : Set <| Seminorm 𝕜 E} (hs : BddAbove ((↑) '' s : Set (E → ℝ))) : ↑(sSup s) = ⨆ p : s, ((p : Seminorm 𝕜 E) : E → ℝ) := congr_arg _ (dif_pos hs) protected theorem bddAbove_iff {s : Set <| Seminorm 𝕜 E} : BddAbove s ↔ BddAbove ((↑) '' s : Set (E → ℝ)) := ⟨fun ⟨q, hq⟩ => ⟨q, forall_mem_image.2 fun _ hp => hq hp⟩, fun H => ⟨sSup s, fun p hp x => by dsimp rw [Seminorm.coe_sSup_eq' H, iSup_apply] rcases H with ⟨q, hq⟩ exact le_ciSup ⟨q x, forall_mem_range.mpr fun i : s => hq (mem_image_of_mem _ i.2) x⟩ ⟨p, hp⟩⟩⟩ protected theorem bddAbove_range_iff {ι : Sort*} {p : ι → Seminorm 𝕜 E} : BddAbove (range p) ↔ ∀ x, BddAbove (range fun i ↦ p i x) := by rw [Seminorm.bddAbove_iff, ← range_comp, bddAbove_range_pi]; rfl protected theorem coe_sSup_eq {s : Set <| Seminorm 𝕜 E} (hs : BddAbove s) : ↑(sSup s) = ⨆ p : s, ((p : Seminorm 𝕜 E) : E → ℝ) := Seminorm.coe_sSup_eq' (Seminorm.bddAbove_iff.mp hs) protected theorem coe_iSup_eq {ι : Sort*} {p : ι → Seminorm 𝕜 E} (hp : BddAbove (range p)) : ↑(⨆ i, p i) = ⨆ i, ((p i : Seminorm 𝕜 E) : E → ℝ) := by rw [← sSup_range, Seminorm.coe_sSup_eq hp] exact iSup_range' (fun p : Seminorm 𝕜 E => (p : E → ℝ)) p protected theorem sSup_apply {s : Set (Seminorm 𝕜 E)} (hp : BddAbove s) {x : E} : (sSup s) x = ⨆ p : s, (p : E → ℝ) x := by rw [Seminorm.coe_sSup_eq hp, iSup_apply] protected theorem iSup_apply {ι : Sort*} {p : ι → Seminorm 𝕜 E} (hp : BddAbove (range p)) {x : E} : (⨆ i, p i) x = ⨆ i, p i x := by rw [Seminorm.coe_iSup_eq hp, iSup_apply] protected theorem sSup_empty : sSup (∅ : Set (Seminorm 𝕜 E)) = ⊥ := by ext rw [Seminorm.sSup_apply bddAbove_empty, Real.iSup_of_isEmpty] rfl private theorem isLUB_sSup (s : Set (Seminorm 𝕜 E)) (hs₁ : BddAbove s) (hs₂ : s.Nonempty) : IsLUB s (sSup s) := by refine ⟨fun p hp x => ?_, fun p hp x => ?_⟩ <;> haveI : Nonempty ↑s := hs₂.coe_sort <;> dsimp <;> rw [Seminorm.coe_sSup_eq hs₁, iSup_apply] · rcases hs₁ with ⟨q, hq⟩ exact le_ciSup ⟨q x, forall_mem_range.mpr fun i : s => hq i.2 x⟩ ⟨p, hp⟩ · exact ciSup_le fun q => hp q.2 x /-- `Seminorm 𝕜 E` is a conditionally complete lattice. Note that, while `inf`, `sup` and `sSup` have good definitional properties (corresponding to the instances given here for `Inf`, `Sup` and `SupSet` respectively), `sInf s` is just defined as the supremum of the lower bounds of `s`, which is not really useful in practice. If you need to use `sInf` on seminorms, then you should probably provide a more workable definition first, but this is unlikely to happen so we keep the "bad" definition for now. -/ noncomputable instance instConditionallyCompleteLattice : ConditionallyCompleteLattice (Seminorm 𝕜 E) := conditionallyCompleteLatticeOfLatticeOfsSup (Seminorm 𝕜 E) Seminorm.isLUB_sSup end Classical end NormedField /-! ### Seminorm ball -/ section SeminormedRing variable [SeminormedRing 𝕜] section AddCommGroup variable [AddCommGroup E] section SMul variable [SMul 𝕜 E] (p : Seminorm 𝕜 E) /-- The ball of radius `r` at `x` with respect to seminorm `p` is the set of elements `y` with `p (y - x) < r`. -/ def ball (x : E) (r : ℝ) := { y : E | p (y - x) < r } /-- The closed ball of radius `r` at `x` with respect to seminorm `p` is the set of elements `y` with `p (y - x) ≤ r`. -/ def closedBall (x : E) (r : ℝ) := { y : E | p (y - x) ≤ r } variable {x y : E} {r : ℝ} @[simp] theorem mem_ball : y ∈ ball p x r ↔ p (y - x) < r := Iff.rfl @[simp] theorem mem_closedBall : y ∈ closedBall p x r ↔ p (y - x) ≤ r := Iff.rfl theorem mem_ball_self (hr : 0 < r) : x ∈ ball p x r := by simp [hr] theorem mem_closedBall_self (hr : 0 ≤ r) : x ∈ closedBall p x r := by simp [hr] theorem mem_ball_zero : y ∈ ball p 0 r ↔ p y < r := by rw [mem_ball, sub_zero] theorem mem_closedBall_zero : y ∈ closedBall p 0 r ↔ p y ≤ r := by rw [mem_closedBall, sub_zero] theorem ball_zero_eq : ball p 0 r = { y : E | p y < r } := Set.ext fun _ => p.mem_ball_zero theorem closedBall_zero_eq : closedBall p 0 r = { y : E | p y ≤ r } := Set.ext fun _ => p.mem_closedBall_zero theorem ball_subset_closedBall (x r) : ball p x r ⊆ closedBall p x r := fun _ h => (mem_closedBall _).mpr ((mem_ball _).mp h).le theorem closedBall_eq_biInter_ball (x r) : closedBall p x r = ⋂ ρ > r, ball p x ρ := by ext y; simp_rw [mem_closedBall, mem_iInter₂, mem_ball, ← forall_lt_iff_le'] @[simp] theorem ball_zero' (x : E) (hr : 0 < r) : ball (0 : Seminorm 𝕜 E) x r = Set.univ := by rw [Set.eq_univ_iff_forall, ball] simp [hr] @[simp] theorem closedBall_zero' (x : E) (hr : 0 < r) : closedBall (0 : Seminorm 𝕜 E) x r = Set.univ := eq_univ_of_subset (ball_subset_closedBall _ _ _) (ball_zero' x hr) theorem ball_smul (p : Seminorm 𝕜 E) {c : NNReal} (hc : 0 < c) (r : ℝ) (x : E) : (c • p).ball x r = p.ball x (r / c) := by ext rw [mem_ball, mem_ball, smul_apply, NNReal.smul_def, smul_eq_mul, mul_comm, lt_div_iff₀ (NNReal.coe_pos.mpr hc)] theorem closedBall_smul (p : Seminorm 𝕜 E) {c : NNReal} (hc : 0 < c) (r : ℝ) (x : E) : (c • p).closedBall x r = p.closedBall x (r / c) := by ext rw [mem_closedBall, mem_closedBall, smul_apply, NNReal.smul_def, smul_eq_mul, mul_comm, le_div_iff₀ (NNReal.coe_pos.mpr hc)] theorem ball_sup (p : Seminorm 𝕜 E) (q : Seminorm 𝕜 E) (e : E) (r : ℝ) : ball (p ⊔ q) e r = ball p e r ∩ ball q e r := by simp_rw [ball, ← Set.setOf_and, coe_sup, Pi.sup_apply, sup_lt_iff] theorem closedBall_sup (p : Seminorm 𝕜 E) (q : Seminorm 𝕜 E) (e : E) (r : ℝ) : closedBall (p ⊔ q) e r = closedBall p e r ∩ closedBall q e r := by simp_rw [closedBall, ← Set.setOf_and, coe_sup, Pi.sup_apply, sup_le_iff] theorem ball_finset_sup' (p : ι → Seminorm 𝕜 E) (s : Finset ι) (H : s.Nonempty) (e : E) (r : ℝ) : ball (s.sup' H p) e r = s.inf' H fun i => ball (p i) e r := by induction H using Finset.Nonempty.cons_induction with | singleton => simp | cons _ _ _ hs ih => rw [Finset.sup'_cons hs, Finset.inf'_cons hs, ball_sup] -- Porting note: `rw` can't use `inf_eq_inter` here, but `simp` can? simp only [inf_eq_inter, ih] theorem closedBall_finset_sup' (p : ι → Seminorm 𝕜 E) (s : Finset ι) (H : s.Nonempty) (e : E) (r : ℝ) : closedBall (s.sup' H p) e r = s.inf' H fun i => closedBall (p i) e r := by induction H using Finset.Nonempty.cons_induction with | singleton => simp | cons _ _ _ hs ih => rw [Finset.sup'_cons hs, Finset.inf'_cons hs, closedBall_sup] -- Porting note: `rw` can't use `inf_eq_inter` here, but `simp` can? simp only [inf_eq_inter, ih] theorem ball_mono {p : Seminorm 𝕜 E} {r₁ r₂ : ℝ} (h : r₁ ≤ r₂) : p.ball x r₁ ⊆ p.ball x r₂ := fun _ (hx : _ < _) => hx.trans_le h theorem closedBall_mono {p : Seminorm 𝕜 E} {r₁ r₂ : ℝ} (h : r₁ ≤ r₂) : p.closedBall x r₁ ⊆ p.closedBall x r₂ := fun _ (hx : _ ≤ _) => hx.trans h theorem ball_antitone {p q : Seminorm 𝕜 E} (h : q ≤ p) : p.ball x r ⊆ q.ball x r := fun _ => (h _).trans_lt theorem closedBall_antitone {p q : Seminorm 𝕜 E} (h : q ≤ p) : p.closedBall x r ⊆ q.closedBall x r := fun _ => (h _).trans theorem ball_add_ball_subset (p : Seminorm 𝕜 E) (r₁ r₂ : ℝ) (x₁ x₂ : E) : p.ball (x₁ : E) r₁ + p.ball (x₂ : E) r₂ ⊆ p.ball (x₁ + x₂) (r₁ + r₂) := by rintro x ⟨y₁, hy₁, y₂, hy₂, rfl⟩ rw [mem_ball, add_sub_add_comm] exact (map_add_le_add p _ _).trans_lt (add_lt_add hy₁ hy₂) theorem closedBall_add_closedBall_subset (p : Seminorm 𝕜 E) (r₁ r₂ : ℝ) (x₁ x₂ : E) : p.closedBall (x₁ : E) r₁ + p.closedBall (x₂ : E) r₂ ⊆ p.closedBall (x₁ + x₂) (r₁ + r₂) := by rintro x ⟨y₁, hy₁, y₂, hy₂, rfl⟩ rw [mem_closedBall, add_sub_add_comm] exact (map_add_le_add p _ _).trans (add_le_add hy₁ hy₂) theorem sub_mem_ball (p : Seminorm 𝕜 E) (x₁ x₂ y : E) (r : ℝ) : x₁ - x₂ ∈ p.ball y r ↔ x₁ ∈ p.ball (x₂ + y) r := by simp_rw [mem_ball, sub_sub] theorem sub_mem_closedBall (p : Seminorm 𝕜 E) (x₁ x₂ y : E) (r : ℝ) : x₁ - x₂ ∈ p.closedBall y r ↔ x₁ ∈ p.closedBall (x₂ + y) r := by simp_rw [mem_closedBall, sub_sub] /-- The image of a ball under addition with a singleton is another ball. -/ theorem vadd_ball (p : Seminorm 𝕜 E) : x +ᵥ p.ball y r = p.ball (x +ᵥ y) r := letI := AddGroupSeminorm.toSeminormedAddCommGroup p.toAddGroupSeminorm Metric.vadd_ball x y r /-- The image of a closed ball under addition with a singleton is another closed ball. -/ theorem vadd_closedBall (p : Seminorm 𝕜 E) : x +ᵥ p.closedBall y r = p.closedBall (x +ᵥ y) r := letI := AddGroupSeminorm.toSeminormedAddCommGroup p.toAddGroupSeminorm Metric.vadd_closedBall x y r end SMul section Module variable [Module 𝕜 E] variable [SeminormedRing 𝕜₂] [AddCommGroup E₂] [Module 𝕜₂ E₂] variable {σ₁₂ : 𝕜 →+* 𝕜₂} [RingHomIsometric σ₁₂]
theorem ball_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (x : E) (r : ℝ) : (p.comp f).ball x r = f ⁻¹' p.ball (f x) r := by
Mathlib/Analysis/Seminorm.lean
736
738
/- Copyright (c) 2024 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Analysis.Seminorm import Mathlib.GroupTheory.GroupAction.Pointwise /-! # The Minkowski functional, normed field version In this file we define `(egauge 𝕜 s ·)` to be the Minkowski functional (gauge) of the set `s` in a topological vector space `E` over a normed field `𝕜`, as a function `E → ℝ≥0∞`. It is defined as the infimum of the norms of `c : 𝕜` such that `x ∈ c • s`. In particular, for `𝕜 = ℝ≥0` this definition gives an `ℝ≥0∞`-valued version of `gauge` defined in `Mathlib/Analysis/Convex/Gauge.lean`. This definition can be used to generalize the notion of Fréchet derivative to maps between topological vector spaces without norms. Currently, we can't reuse results about `egauge` for `gauge`, because we lack a theory of normed semifields. -/ open Function Set Filter Metric open scoped Topology Pointwise ENNReal NNReal section SMul /-- The Minkowski functional for vector spaces over normed fields. Given a set `s` in a vector space over a normed field `𝕜`, `egauge s` is the functional which sends `x : E` to the infimum of `‖c‖ₑ` over `c` such that `x` belongs to `s` scaled by `c`. The definition only requires `𝕜` to have a `ENorm` instance and `(· • ·) : 𝕜 → E → E` to be defined. This way the definition applies, e.g., to `𝕜 = ℝ≥0`. For `𝕜 = ℝ≥0`, the function is equal (up to conversion to `ℝ`) to the usual Minkowski functional defined in `gauge`. -/ noncomputable def egauge (𝕜 : Type*) [ENorm 𝕜] {E : Type*} [SMul 𝕜 E] (s : Set E) (x : E) : ℝ≥0∞ := ⨅ (c : 𝕜) (_ : x ∈ c • s), ‖c‖ₑ variable (𝕜 : Type*) [NNNorm 𝕜] {E : Type*} [SMul 𝕜 E] {c : 𝕜} {s t : Set E} {x : E} {r : ℝ≥0∞} lemma Set.MapsTo.egauge_le {E' F : Type*} [SMul 𝕜 E'] [FunLike F E E'] [MulActionHomClass F 𝕜 E E'] (f : F) {t : Set E'} (h : MapsTo f s t) (x : E) : egauge 𝕜 t (f x) ≤ egauge 𝕜 s x := iInf_mono fun c ↦ iInf_mono' fun hc ↦ ⟨h.smul_set c hc, le_rfl⟩ @[mono, gcongr] lemma egauge_anti (h : s ⊆ t) (x : E) : egauge 𝕜 t x ≤ egauge 𝕜 s x := MapsTo.egauge_le _ (MulActionHom.id ..) h _ @[simp] lemma egauge_empty (x : E) : egauge 𝕜 ∅ x = ∞ := by simp [egauge] variable {𝕜} lemma egauge_le_of_mem_smul (h : x ∈ c • s) : egauge 𝕜 s x ≤ ‖c‖ₑ := iInf₂_le c h lemma le_egauge_iff : r ≤ egauge 𝕜 s x ↔ ∀ c : 𝕜, x ∈ c • s → r ≤ ‖c‖ₑ := le_iInf₂_iff lemma egauge_eq_top : egauge 𝕜 s x = ∞ ↔ ∀ c : 𝕜, x ∉ c • s := by simp [egauge] lemma egauge_lt_iff : egauge 𝕜 s x < r ↔ ∃ c : 𝕜, x ∈ c • s ∧ ‖c‖ₑ < r := by simp [egauge, iInf_lt_iff] lemma egauge_union (s t : Set E) (x : E) : egauge 𝕜 (s ∪ t) x = egauge 𝕜 s x ⊓ egauge 𝕜 t x := by unfold egauge simp [smul_set_union, iInf_or, iInf_inf_eq] lemma le_egauge_inter (s t : Set E) (x : E) : egauge 𝕜 s x ⊔ egauge 𝕜 t x ≤ egauge 𝕜 (s ∩ t) x := max_le (egauge_anti _ inter_subset_left _) (egauge_anti _ inter_subset_right _) lemma le_egauge_pi {ι : Type*} {E : ι → Type*} [∀ i, SMul 𝕜 (E i)] {I : Set ι} {i : ι} (hi : i ∈ I) (s : ∀ i, Set (E i)) (x : ∀ i, E i) : egauge 𝕜 (s i) (x i) ≤ egauge 𝕜 (I.pi s) x := MapsTo.egauge_le _ (Pi.evalMulActionHom i) (fun x hx ↦ by exact hx i hi) _ variable {F : Type*} [SMul 𝕜 F]
lemma le_egauge_prod (s : Set E) (t : Set F) (a : E) (b : F) : max (egauge 𝕜 s a) (egauge 𝕜 t b) ≤ egauge 𝕜 (s ×ˢ t) (a, b) := max_le (mapsTo_fst_prod.egauge_le 𝕜 (MulActionHom.fst 𝕜 E F) (a, b)) (MapsTo.egauge_le 𝕜 (MulActionHom.snd 𝕜 E F) mapsTo_snd_prod (a, b)) end SMul
Mathlib/Analysis/Convex/EGauge.lean
83
89
/- 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, Mario Carneiro -/ import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.Ring.Rat import Mathlib.Algebra.Ring.Int.Parity import Mathlib.Data.PNat.Defs /-! # Further lemmas for the Rational Numbers -/ namespace Rat theorem num_dvd (a) {b : ℤ} (b0 : b ≠ 0) : (a /. b).num ∣ a := by rcases e : a /. b with ⟨n, d, h, c⟩ rw [Rat.mk'_eq_divInt, divInt_eq_iff b0 (mod_cast h)] at e refine Int.natAbs_dvd.1 <| Int.dvd_natAbs.1 <| Int.natCast_dvd_natCast.2 <| c.dvd_of_dvd_mul_right ?_ have := congr_arg Int.natAbs e simp only [Int.natAbs_mul, Int.natAbs_natCast] at this; simp [this] theorem den_dvd (a b : ℤ) : ((a /. b).den : ℤ) ∣ b := by by_cases b0 : b = 0; · simp [b0] rcases e : a /. b with ⟨n, d, h, c⟩ rw [mk'_eq_divInt, divInt_eq_iff b0 (ne_of_gt (Int.natCast_pos.2 (Nat.pos_of_ne_zero h)))] at e refine Int.dvd_natAbs.1 <| Int.natCast_dvd_natCast.2 <| c.symm.dvd_of_dvd_mul_left ?_ rw [← Int.natAbs_mul, ← Int.natCast_dvd_natCast, Int.dvd_natAbs, ← e]; simp
theorem num_den_mk {q : ℚ} {n d : ℤ} (hd : d ≠ 0) (qdf : q = n /. d) : ∃ c : ℤ, n = c * q.num ∧ d = c * q.den := by obtain rfl | hn := eq_or_ne n 0 · simp [qdf] have : q.num * d = n * ↑q.den := by
Mathlib/Data/Rat/Lemmas.lean
33
38
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux, Kim Morrison, Oliver Nash -/ import Mathlib.Algebra.Group.Action.Defs import Mathlib.Tactic.Abel /-! # The `noncomm_ring` tactic Solve goals in not necessarily commutative rings. This tactic is rudimentary, but useful for solving simple goals in noncommutative rings. One glaring flaw is that numeric powers are unfolded entirely with `pow_succ` and can easily exceed the maximum recursion depth. `noncomm_ring` is just a `simp only [some lemmas]` followed by `abel`. It automatically uses `abel1` to close the goal, and if that doesn't succeed, defaults to `abel_nf`. -/ namespace Mathlib.Tactic.NoncommRing section nat_lit_mul variable {R : Type*} [NonAssocSemiring R] (r : R) (n : ℕ) lemma nat_lit_mul_eq_nsmul [n.AtLeastTwo] : ofNat(n) * r = OfNat.ofNat n • r := by simp only [nsmul_eq_mul, Nat.cast_ofNat]
lemma mul_nat_lit_eq_nsmul [n.AtLeastTwo] : r * ofNat(n) = OfNat.ofNat n • r := by simp only [nsmul_eq_mul', Nat.cast_ofNat]
Mathlib/Tactic/NoncommRing.lean
28
29
/- 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
Mathlib/Data/List/Basic.lean
728
733
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann, Kyle Miller, Mario Carneiro -/ import Mathlib.Data.Finset.NatAntidiagonal import Mathlib.Data.Nat.GCD.Basic import Mathlib.Data.Nat.BinaryRec import Mathlib.Logic.Function.Iterate import Mathlib.Tactic.Ring import Mathlib.Tactic.Zify import Mathlib.Data.Nat.Choose.Basic import Mathlib.Algebra.BigOperators.Group.Finset.Basic /-! # Fibonacci Numbers This file defines the fibonacci series, proves results about it and introduces methods to compute it quickly. -/ /-! # The Fibonacci Sequence ## Summary Definition of the Fibonacci sequence `F₀ = 0, F₁ = 1, Fₙ₊₂ = Fₙ + Fₙ₊₁`. ## Main Definitions - `Nat.fib` returns the stream of Fibonacci numbers. ## Main Statements - `Nat.fib_add_two`: shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.`. - `Nat.fib_gcd`: `fib n` is a strong divisibility sequence. - `Nat.fib_succ_eq_sum_choose`: `fib` is given by the sum of `Nat.choose` along an antidiagonal. - `Nat.fib_succ_eq_succ_sum`: shows that `F₀ + F₁ + ⋯ + Fₙ = Fₙ₊₂ - 1`. - `Nat.fib_two_mul` and `Nat.fib_two_mul_add_one` are the basis for an efficient algorithm to compute `fib` (see `Nat.fastFib`). ## Implementation Notes For efficiency purposes, the sequence is defined using `Stream.iterate`. ## Tags fib, fibonacci -/ namespace Nat /-- Implementation of the fibonacci sequence satisfying `fib 0 = 0, fib 1 = 1, fib (n + 2) = fib n + fib (n + 1)`. *Note:* We use a stream iterator for better performance when compared to the naive recursive implementation. -/ @[pp_nodot] def fib (n : ℕ) : ℕ := ((fun p : ℕ × ℕ => (p.snd, p.fst + p.snd))^[n] (0, 1)).fst @[simp] theorem fib_zero : fib 0 = 0 := rfl @[simp] theorem fib_one : fib 1 = 1 := rfl @[simp] theorem fib_two : fib 2 = 1 := rfl /-- Shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.` -/ theorem fib_add_two {n : ℕ} : fib (n + 2) = fib n + fib (n + 1) := by simp [fib, Function.iterate_succ_apply'] lemma fib_add_one : ∀ {n}, n ≠ 0 → fib (n + 1) = fib (n - 1) + fib n | _n + 1, _ => fib_add_two theorem fib_le_fib_succ {n : ℕ} : fib n ≤ fib (n + 1) := by cases n <;> simp [fib_add_two] @[mono] theorem fib_mono : Monotone fib := monotone_nat_of_le_succ fun _ => fib_le_fib_succ @[simp] lemma fib_eq_zero : ∀ {n}, fib n = 0 ↔ n = 0 | 0 => Iff.rfl | 1 => Iff.rfl | n + 2 => by simp [fib_add_two, fib_eq_zero] @[simp] lemma fib_pos {n : ℕ} : 0 < fib n ↔ 0 < n := by simp [pos_iff_ne_zero] theorem fib_add_two_sub_fib_add_one {n : ℕ} : fib (n + 2) - fib (n + 1) = fib n := by rw [fib_add_two, add_tsub_cancel_right] theorem fib_lt_fib_succ {n : ℕ} (hn : 2 ≤ n) : fib n < fib (n + 1) := by rcases exists_add_of_le hn with ⟨n, rfl⟩ rw [← tsub_pos_iff_lt, add_comm 2, add_right_comm, fib_add_two, add_tsub_cancel_right, fib_pos] exact succ_pos n /-- `fib (n + 2)` is strictly monotone. -/ theorem fib_add_two_strictMono : StrictMono fun n => fib (n + 2) := by refine strictMono_nat_of_lt_succ fun n => ?_ rw [add_right_comm] exact fib_lt_fib_succ (self_le_add_left _ _) lemma fib_strictMonoOn : StrictMonoOn fib (Set.Ici 2) | _m + 2, _, _n + 2, _, hmn => fib_add_two_strictMono <| lt_of_add_lt_add_right hmn lemma fib_lt_fib {m : ℕ} (hm : 2 ≤ m) : ∀ {n}, fib m < fib n ↔ m < n | 0 => by simp [hm] | 1 => by simp [hm] | n + 2 => fib_strictMonoOn.lt_iff_lt hm <| by simp theorem le_fib_self {n : ℕ} (five_le_n : 5 ≤ n) : n ≤ fib n := by induction' five_le_n with n five_le_n IH · -- 5 ≤ fib 5 rfl · -- n + 1 ≤ fib (n + 1) for 5 ≤ n rw [succ_le_iff] calc n ≤ fib n := IH _ < fib (n + 1) := fib_lt_fib_succ (le_trans (by decide) five_le_n) lemma le_fib_add_one : ∀ n, n ≤ fib n + 1 | 0 => zero_le_one | 1 => one_le_two | 2 => le_rfl | 3 => le_rfl | 4 => le_rfl | _n + 5 => (le_fib_self le_add_self).trans <| le_succ _ /-- Subsequent Fibonacci numbers are coprime, see https://proofwiki.org/wiki/Consecutive_Fibonacci_Numbers_are_Coprime -/ theorem fib_coprime_fib_succ (n : ℕ) : Nat.Coprime (fib n) (fib (n + 1)) := by induction' n with n ih · simp · simp only [fib_add_two, coprime_add_self_right, Coprime, ih.symm] /-- See https://proofwiki.org/wiki/Fibonacci_Number_in_terms_of_Smaller_Fibonacci_Numbers -/ theorem fib_add (m n : ℕ) : fib (m + n + 1) = fib m * fib n + fib (m + 1) * fib (n + 1) := by induction' n with n ih generalizing m · simp · specialize ih (m + 1) rw [add_assoc m 1 n, add_comm 1 n] at ih simp only [fib_add_two, succ_eq_add_one, ih] ring theorem fib_two_mul (n : ℕ) : fib (2 * n) = fib n * (2 * fib (n + 1) - fib n) := by cases n · simp · rw [two_mul, ← add_assoc, fib_add, fib_add_two, two_mul] simp only [← add_assoc, add_tsub_cancel_right] ring theorem fib_two_mul_add_one (n : ℕ) : fib (2 * n + 1) = fib (n + 1) ^ 2 + fib n ^ 2 := by rw [two_mul, fib_add] ring theorem fib_two_mul_add_two (n : ℕ) : fib (2 * n + 2) = fib (n + 1) * (2 * fib n + fib (n + 1)) := by rw [fib_add_two, fib_two_mul, fib_two_mul_add_one] have : fib n ≤ 2 * fib (n + 1) := le_trans fib_le_fib_succ (mul_comm 2 _ ▸ Nat.le_mul_of_pos_right _ two_pos) zify [this] ring /-- Computes `(Nat.fib n, Nat.fib (n + 1))` using the binary representation of `n`. Supports `Nat.fastFib`. -/ def fastFibAux : ℕ → ℕ × ℕ := Nat.binaryRec (fib 0, fib 1) fun b _ p => if b then (p.2 ^ 2 + p.1 ^ 2, p.2 * (2 * p.1 + p.2)) else (p.1 * (2 * p.2 - p.1), p.2 ^ 2 + p.1 ^ 2) /-- Computes `Nat.fib n` using the binary representation of `n`. Proved to be equal to `Nat.fib` in `Nat.fast_fib_eq`. -/ def fastFib (n : ℕ) : ℕ := (fastFibAux n).1 theorem fast_fib_aux_bit_ff (n : ℕ) :
fastFibAux (bit false n) = let p := fastFibAux n (p.1 * (2 * p.2 - p.1), p.2 ^ 2 + p.1 ^ 2) := by rw [fastFibAux, binaryRec_eq] · rfl · simp theorem fast_fib_aux_bit_tt (n : ℕ) :
Mathlib/Data/Nat/Fib/Basic.lean
186
193
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Yuyang Zhao -/ import Mathlib.Algebra.Order.GroupWithZero.Unbundled.Basic import Mathlib.Algebra.Order.GroupWithZero.Unbundled.Defs import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module (since := "2025-04-13")
Mathlib/Algebra/Order/GroupWithZero/Unbundled.lean
1,085
1,086
/- Copyright (c) 2017 Johannes Hölzl. 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.Algebra.GroupWithZero.Divisibility import Mathlib.Data.Nat.SuccPred import Mathlib.Order.SuccPred.InitialSeg import Mathlib.SetTheory.Ordinal.Basic /-! # Ordinal arithmetic Ordinals have an addition (corresponding to disjoint union) that turns them into an additive monoid, and a multiplication (corresponding to the lexicographic order on the product) that turns them into a monoid. One can also define correspondingly a subtraction, a division, a successor function, a power function and a logarithm function. We also define limit ordinals and prove the basic induction principle on ordinals separating successor ordinals and limit ordinals, in `limitRecOn`. ## Main definitions and results * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. * `o₁ - o₂` is the unique ordinal `o` such that `o₂ + o = o₁`, when `o₂ ≤ o₁`. * `o₁ * o₂` is the lexicographic order on `o₂ × o₁`. * `o₁ / o₂` is the ordinal `o` such that `o₁ = o₂ * o + o'` with `o' < o₂`. We also define the divisibility predicate, and a modulo operation. * `Order.succ o = o + 1` is the successor of `o`. * `pred o` if the predecessor of `o`. If `o` is not a successor, we set `pred o = o`. We discuss the properties of casts of natural numbers of and of `ω` with respect to these operations. Some properties of the operations are also used to discuss general tools on ordinals: * `IsLimit o`: an ordinal is a limit ordinal if it is neither `0` nor a successor. * `limitRecOn` is the main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. * `IsNormal`: a function `f : Ordinal → Ordinal` satisfies `IsNormal` if it is strictly increasing and order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. Various other basic arithmetic results are given in `Principal.lean` instead. -/ assert_not_exists Field Module noncomputable section open Function Cardinal Set Equiv Order open scoped Ordinal universe u v w namespace Ordinal variable {α β γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Further properties of addition on ordinals -/ @[simp] theorem lift_add (a b : Ordinal.{v}) : lift.{u} (a + b) = lift.{u} a + lift.{u} b := Quotient.inductionOn₂ a b fun ⟨_α, _r, _⟩ ⟨_β, _s, _⟩ => Quotient.sound ⟨(RelIso.preimage Equiv.ulift _).trans (RelIso.sumLexCongr (RelIso.preimage Equiv.ulift _) (RelIso.preimage Equiv.ulift _)).symm⟩ @[simp] theorem lift_succ (a : Ordinal.{v}) : lift.{u} (succ a) = succ (lift.{u} a) := by rw [← add_one_eq_succ, lift_add, lift_one] rfl instance instAddLeftReflectLE : AddLeftReflectLE Ordinal.{u} where elim c a b := by refine inductionOn₃ a b c fun α r _ β s _ γ t _ ⟨f⟩ ↦ ?_ have H₁ a : f (Sum.inl a) = Sum.inl a := by simpa using ((InitialSeg.leAdd t r).trans f).eq (InitialSeg.leAdd t s) a have H₂ a : ∃ b, f (Sum.inr a) = Sum.inr b := by generalize hx : f (Sum.inr a) = x obtain x | x := x · rw [← H₁, f.inj] at hx contradiction · exact ⟨x, rfl⟩ choose g hg using H₂ refine (RelEmbedding.ofMonotone g fun _ _ h ↦ ?_).ordinal_type_le rwa [← @Sum.lex_inr_inr _ t _ s, ← hg, ← hg, f.map_rel_iff, Sum.lex_inr_inr] instance : IsLeftCancelAdd Ordinal where add_left_cancel a b c h := by simpa only [le_antisymm_iff, add_le_add_iff_left] using h @[deprecated add_left_cancel_iff (since := "2024-12-11")] protected theorem add_left_cancel (a) {b c : Ordinal} : a + b = a + c ↔ b = c := add_left_cancel_iff private theorem add_lt_add_iff_left' (a) {b c : Ordinal} : a + b < a + c ↔ b < c := by rw [← not_le, ← not_le, add_le_add_iff_left] instance instAddLeftStrictMono : AddLeftStrictMono Ordinal.{u} := ⟨fun a _b _c ↦ (add_lt_add_iff_left' a).2⟩ instance instAddLeftReflectLT : AddLeftReflectLT Ordinal.{u} := ⟨fun a _b _c ↦ (add_lt_add_iff_left' a).1⟩ instance instAddRightReflectLT : AddRightReflectLT Ordinal.{u} := ⟨fun _a _b _c ↦ lt_imp_lt_of_le_imp_le fun h => add_le_add_right h _⟩ theorem add_le_add_iff_right {a b : Ordinal} : ∀ n : ℕ, a + n ≤ b + n ↔ a ≤ b | 0 => by simp | n + 1 => by simp only [natCast_succ, add_succ, add_succ, succ_le_succ_iff, add_le_add_iff_right] theorem add_right_cancel {a b : Ordinal} (n : ℕ) : a + n = b + n ↔ a = b := by simp only [le_antisymm_iff, add_le_add_iff_right] theorem add_eq_zero_iff {a b : Ordinal} : a + b = 0 ↔ a = 0 ∧ b = 0 := inductionOn₂ a b fun α r _ β s _ => by simp_rw [← type_sum_lex, type_eq_zero_iff_isEmpty] exact isEmpty_sum theorem left_eq_zero_of_add_eq_zero {a b : Ordinal} (h : a + b = 0) : a = 0 := (add_eq_zero_iff.1 h).1 theorem right_eq_zero_of_add_eq_zero {a b : Ordinal} (h : a + b = 0) : b = 0 := (add_eq_zero_iff.1 h).2 /-! ### The predecessor of an ordinal -/ open Classical in /-- The ordinal predecessor of `o` is `o'` if `o = succ o'`, and `o` otherwise. -/ def pred (o : Ordinal) : Ordinal := if h : ∃ a, o = succ a then Classical.choose h else o @[simp] theorem pred_succ (o) : pred (succ o) = o := by have h : ∃ a, succ o = succ a := ⟨_, rfl⟩ simpa only [pred, dif_pos h] using (succ_injective <| Classical.choose_spec h).symm theorem pred_le_self (o) : pred o ≤ o := by classical exact if h : ∃ a, o = succ a then by let ⟨a, e⟩ := h rw [e, pred_succ]; exact le_succ a else by rw [pred, dif_neg h] theorem pred_eq_iff_not_succ {o} : pred o = o ↔ ¬∃ a, o = succ a := ⟨fun e ⟨a, e'⟩ => by rw [e', pred_succ] at e; exact (lt_succ a).ne e, fun h => dif_neg h⟩ theorem pred_eq_iff_not_succ' {o} : pred o = o ↔ ∀ a, o ≠ succ a := by simpa using pred_eq_iff_not_succ theorem pred_lt_iff_is_succ {o} : pred o < o ↔ ∃ a, o = succ a := Iff.trans (by simp only [le_antisymm_iff, pred_le_self, true_and, not_le]) (iff_not_comm.1 pred_eq_iff_not_succ).symm @[simp] theorem pred_zero : pred 0 = 0 := pred_eq_iff_not_succ'.2 fun a => (succ_ne_zero a).symm theorem succ_pred_iff_is_succ {o} : succ (pred o) = o ↔ ∃ a, o = succ a := ⟨fun e => ⟨_, e.symm⟩, fun ⟨a, e⟩ => by simp only [e, pred_succ]⟩ theorem succ_lt_of_not_succ {o b : Ordinal} (h : ¬∃ a, o = succ a) : succ b < o ↔ b < o := ⟨(lt_succ b).trans, fun l => lt_of_le_of_ne (succ_le_of_lt l) fun e => h ⟨_, e.symm⟩⟩ theorem lt_pred {a b} : a < pred b ↔ succ a < b := by classical exact if h : ∃ a, b = succ a then by let ⟨c, e⟩ := h rw [e, pred_succ, succ_lt_succ_iff] else by simp only [pred, dif_neg h, succ_lt_of_not_succ h] theorem pred_le {a b} : pred a ≤ b ↔ a ≤ succ b := le_iff_le_iff_lt_iff_lt.2 lt_pred @[simp] theorem lift_is_succ {o : Ordinal.{v}} : (∃ a, lift.{u} o = succ a) ↔ ∃ a, o = succ a := ⟨fun ⟨a, h⟩ => let ⟨b, e⟩ := mem_range_lift_of_le <| show a ≤ lift.{u} o from le_of_lt <| h.symm ▸ lt_succ a ⟨b, (lift_inj.{u,v}).1 <| by rw [h, ← e, lift_succ]⟩, fun ⟨a, h⟩ => ⟨lift.{u} a, by simp only [h, lift_succ]⟩⟩ @[simp] theorem lift_pred (o : Ordinal.{v}) : lift.{u} (pred o) = pred (lift.{u} o) := by classical exact if h : ∃ a, o = succ a then by obtain ⟨a, e⟩ := h; simp only [e, pred_succ, lift_succ] else by rw [pred_eq_iff_not_succ.2 h, pred_eq_iff_not_succ.2 (mt lift_is_succ.1 h)] /-! ### Limit ordinals -/ /-- A limit ordinal is an ordinal which is not zero and not a successor. TODO: deprecate this in favor of `Order.IsSuccLimit`. -/ def IsLimit (o : Ordinal) : Prop := IsSuccLimit o theorem isLimit_iff {o} : IsLimit o ↔ o ≠ 0 ∧ IsSuccPrelimit o := by simp [IsLimit, IsSuccLimit] theorem IsLimit.isSuccPrelimit {o} (h : IsLimit o) : IsSuccPrelimit o := IsSuccLimit.isSuccPrelimit h theorem IsLimit.succ_lt {o a : Ordinal} (h : IsLimit o) : a < o → succ a < o := IsSuccLimit.succ_lt h theorem isSuccPrelimit_zero : IsSuccPrelimit (0 : Ordinal) := isSuccPrelimit_bot theorem not_zero_isLimit : ¬IsLimit 0 := not_isSuccLimit_bot theorem not_succ_isLimit (o) : ¬IsLimit (succ o) := not_isSuccLimit_succ o theorem not_succ_of_isLimit {o} (h : IsLimit o) : ¬∃ a, o = succ a | ⟨a, e⟩ => not_succ_isLimit a (e ▸ h) theorem succ_lt_of_isLimit {o a : Ordinal} (h : IsLimit o) : succ a < o ↔ a < o := IsSuccLimit.succ_lt_iff h theorem le_succ_of_isLimit {o} (h : IsLimit o) {a} : o ≤ succ a ↔ o ≤ a := le_iff_le_iff_lt_iff_lt.2 <| succ_lt_of_isLimit h theorem limit_le {o} (h : IsLimit o) {a} : o ≤ a ↔ ∀ x < o, x ≤ a := ⟨fun h _x l => l.le.trans h, fun H => (le_succ_of_isLimit h).1 <| le_of_not_lt fun hn => not_lt_of_le (H _ hn) (lt_succ a)⟩ theorem lt_limit {o} (h : IsLimit o) {a} : a < o ↔ ∃ x < o, a < x := by -- Porting note: `bex_def` is required. simpa only [not_forall₂, not_le, bex_def] using not_congr (@limit_le _ h a) @[simp] theorem lift_isLimit (o : Ordinal.{v}) : IsLimit (lift.{u,v} o) ↔ IsLimit o := liftInitialSeg.isSuccLimit_apply_iff theorem IsLimit.pos {o : Ordinal} (h : IsLimit o) : 0 < o := IsSuccLimit.bot_lt h theorem IsLimit.ne_zero {o : Ordinal} (h : IsLimit o) : o ≠ 0 := h.pos.ne' theorem IsLimit.one_lt {o : Ordinal} (h : IsLimit o) : 1 < o := by simpa only [succ_zero] using h.succ_lt h.pos theorem IsLimit.nat_lt {o : Ordinal} (h : IsLimit o) : ∀ n : ℕ, (n : Ordinal) < o | 0 => h.pos | n + 1 => h.succ_lt (IsLimit.nat_lt h n) theorem zero_or_succ_or_limit (o : Ordinal) : o = 0 ∨ (∃ a, o = succ a) ∨ IsLimit o := by simpa [eq_comm] using isMin_or_mem_range_succ_or_isSuccLimit o theorem isLimit_of_not_succ_of_ne_zero {o : Ordinal} (h : ¬∃ a, o = succ a) (h' : o ≠ 0) : IsLimit o := ((zero_or_succ_or_limit o).resolve_left h').resolve_left h -- TODO: this is an iff with `IsSuccPrelimit` theorem IsLimit.sSup_Iio {o : Ordinal} (h : IsLimit o) : sSup (Iio o) = o := by apply (csSup_le' (fun a ha ↦ le_of_lt ha)).antisymm apply le_of_forall_lt intro a ha exact (lt_succ a).trans_le (le_csSup bddAbove_Iio (h.succ_lt ha)) theorem IsLimit.iSup_Iio {o : Ordinal} (h : IsLimit o) : ⨆ a : Iio o, a.1 = o := by rw [← sSup_eq_iSup', h.sSup_Iio] /-- Main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. -/ @[elab_as_elim] def limitRecOn {motive : Ordinal → Sort*} (o : Ordinal) (zero : motive 0) (succ : ∀ o, motive o → motive (succ o)) (isLimit : ∀ o, IsLimit o → (∀ o' < o, motive o') → motive o) : motive o := by refine SuccOrder.limitRecOn o (fun a ha ↦ ?_) (fun a _ ↦ succ a) isLimit convert zero simpa using ha @[simp] theorem limitRecOn_zero {motive} (H₁ H₂ H₃) : @limitRecOn motive 0 H₁ H₂ H₃ = H₁ := SuccOrder.limitRecOn_isMin _ _ _ isMin_bot @[simp] theorem limitRecOn_succ {motive} (o H₁ H₂ H₃) : @limitRecOn motive (succ o) H₁ H₂ H₃ = H₂ o (@limitRecOn motive o H₁ H₂ H₃) := SuccOrder.limitRecOn_succ .. @[simp] theorem limitRecOn_limit {motive} (o H₁ H₂ H₃ h) : @limitRecOn motive o H₁ H₂ H₃ = H₃ o h fun x _h => @limitRecOn motive x H₁ H₂ H₃ := SuccOrder.limitRecOn_of_isSuccLimit .. /-- Bounded recursion on ordinals. Similar to `limitRecOn`, with the assumption `o < l` added to all cases. The final term's domain is the ordinals below `l`. -/ @[elab_as_elim] def boundedLimitRecOn {l : Ordinal} (lLim : l.IsLimit) {motive : Iio l → Sort*} (o : Iio l) (zero : motive ⟨0, lLim.pos⟩) (succ : (o : Iio l) → motive o → motive ⟨succ o, lLim.succ_lt o.2⟩) (isLimit : (o : Iio l) → IsLimit o → (Π o' < o, motive o') → motive o) : motive o := limitRecOn (motive := fun p ↦ (h : p < l) → motive ⟨p, h⟩) o.1 (fun _ ↦ zero) (fun o ih h ↦ succ ⟨o, _⟩ <| ih <| (lt_succ o).trans h) (fun _o ho ih _ ↦ isLimit _ ho fun _o' h ↦ ih _ h _) o.2 @[simp] theorem boundedLimitRec_zero {l} (lLim : l.IsLimit) {motive} (H₁ H₂ H₃) : @boundedLimitRecOn l lLim motive ⟨0, lLim.pos⟩ H₁ H₂ H₃ = H₁ := by rw [boundedLimitRecOn, limitRecOn_zero] @[simp] theorem boundedLimitRec_succ {l} (lLim : l.IsLimit) {motive} (o H₁ H₂ H₃) : @boundedLimitRecOn l lLim motive ⟨succ o.1, lLim.succ_lt o.2⟩ H₁ H₂ H₃ = H₂ o (@boundedLimitRecOn l lLim motive o H₁ H₂ H₃) := by rw [boundedLimitRecOn, limitRecOn_succ] rfl theorem boundedLimitRec_limit {l} (lLim : l.IsLimit) {motive} (o H₁ H₂ H₃ oLim) : @boundedLimitRecOn l lLim motive o H₁ H₂ H₃ = H₃ o oLim (fun x _ ↦ @boundedLimitRecOn l lLim motive x H₁ H₂ H₃) := by rw [boundedLimitRecOn, limitRecOn_limit] rfl instance orderTopToTypeSucc (o : Ordinal) : OrderTop (succ o).toType := @OrderTop.mk _ _ (Top.mk _) le_enum_succ theorem enum_succ_eq_top {o : Ordinal} : enum (α := (succ o).toType) (· < ·) ⟨o, type_toType _ ▸ lt_succ o⟩ = ⊤ := rfl theorem has_succ_of_type_succ_lt {α} {r : α → α → Prop} [wo : IsWellOrder α r] (h : ∀ a < type r, succ a < type r) (x : α) : ∃ y, r x y := by use enum r ⟨succ (typein r x), h _ (typein_lt_type r x)⟩ convert enum_lt_enum.mpr _ · rw [enum_typein] · rw [Subtype.mk_lt_mk, lt_succ_iff] theorem toType_noMax_of_succ_lt {o : Ordinal} (ho : ∀ a < o, succ a < o) : NoMaxOrder o.toType := ⟨has_succ_of_type_succ_lt (type_toType _ ▸ ho)⟩ theorem bounded_singleton {r : α → α → Prop} [IsWellOrder α r] (hr : (type r).IsLimit) (x) : Bounded r {x} := by refine ⟨enum r ⟨succ (typein r x), hr.succ_lt (typein_lt_type r x)⟩, ?_⟩ intro b hb rw [mem_singleton_iff.1 hb] nth_rw 1 [← enum_typein r x] rw [@enum_lt_enum _ r, Subtype.mk_lt_mk] apply lt_succ @[simp] theorem typein_ordinal (o : Ordinal.{u}) : @typein Ordinal (· < ·) _ o = Ordinal.lift.{u + 1} o := by refine Quotient.inductionOn o ?_ rintro ⟨α, r, wo⟩; apply Quotient.sound constructor; refine ((RelIso.preimage Equiv.ulift r).trans (enum r).symm).symm theorem mk_Iio_ordinal (o : Ordinal.{u}) : #(Iio o) = Cardinal.lift.{u + 1} o.card := by rw [lift_card, ← typein_ordinal] rfl /-! ### Normal ordinal functions -/ /-- A normal ordinal function is a strictly increasing function which is order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. -/ def IsNormal (f : Ordinal → Ordinal) : Prop := (∀ o, f o < f (succ o)) ∧ ∀ o, IsLimit o → ∀ a, f o ≤ a ↔ ∀ b < o, f b ≤ a theorem IsNormal.limit_le {f} (H : IsNormal f) : ∀ {o}, IsLimit o → ∀ {a}, f o ≤ a ↔ ∀ b < o, f b ≤ a := @H.2 theorem IsNormal.limit_lt {f} (H : IsNormal f) {o} (h : IsLimit o) {a} : a < f o ↔ ∃ b < o, a < f b := not_iff_not.1 <| by simpa only [exists_prop, not_exists, not_and, not_lt] using H.2 _ h a theorem IsNormal.strictMono {f} (H : IsNormal f) : StrictMono f := fun a b => limitRecOn b (Not.elim (not_lt_of_le <| Ordinal.zero_le _)) (fun _b IH h => (lt_or_eq_of_le (le_of_lt_succ h)).elim (fun h => (IH h).trans (H.1 _)) fun e => e ▸ H.1 _) fun _b l _IH h => lt_of_lt_of_le (H.1 a) ((H.2 _ l _).1 le_rfl _ (l.succ_lt h)) theorem IsNormal.monotone {f} (H : IsNormal f) : Monotone f := H.strictMono.monotone theorem isNormal_iff_strictMono_limit (f : Ordinal → Ordinal) : IsNormal f ↔ StrictMono f ∧ ∀ o, IsLimit o → ∀ a, (∀ b < o, f b ≤ a) → f o ≤ a := ⟨fun hf => ⟨hf.strictMono, fun a ha c => (hf.2 a ha c).2⟩, fun ⟨hs, hl⟩ => ⟨fun a => hs (lt_succ a), fun a ha c => ⟨fun hac _b hba => ((hs hba).trans_le hac).le, hl a ha c⟩⟩⟩ theorem IsNormal.lt_iff {f} (H : IsNormal f) {a b} : f a < f b ↔ a < b := StrictMono.lt_iff_lt <| H.strictMono theorem IsNormal.le_iff {f} (H : IsNormal f) {a b} : f a ≤ f b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_iff theorem IsNormal.inj {f} (H : IsNormal f) {a b} : f a = f b ↔ a = b := by simp only [le_antisymm_iff, H.le_iff] theorem IsNormal.id_le {f} (H : IsNormal f) : id ≤ f := H.strictMono.id_le theorem IsNormal.le_apply {f} (H : IsNormal f) {a} : a ≤ f a := H.strictMono.le_apply theorem IsNormal.le_iff_eq {f} (H : IsNormal f) {a} : f a ≤ a ↔ f a = a := H.le_apply.le_iff_eq theorem IsNormal.le_set {f o} (H : IsNormal f) (p : Set Ordinal) (p0 : p.Nonempty) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f a ≤ o := ⟨fun h _ pa => (H.le_iff.2 ((H₂ _).1 le_rfl _ pa)).trans h, fun h => by induction b using limitRecOn with | zero => obtain ⟨x, px⟩ := p0 have := Ordinal.le_zero.1 ((H₂ _).1 (Ordinal.zero_le _) _ px) rw [this] at px exact h _ px | succ S _ => rcases not_forall₂.1 (mt (H₂ S).2 <| (lt_succ S).not_le) with ⟨a, h₁, h₂⟩ exact (H.le_iff.2 <| succ_le_of_lt <| not_le.1 h₂).trans (h _ h₁) | isLimit S L _ => refine (H.2 _ L _).2 fun a h' => ?_ rcases not_forall₂.1 (mt (H₂ a).2 h'.not_le) with ⟨b, h₁, h₂⟩ exact (H.le_iff.2 <| (not_le.1 h₂).le).trans (h _ h₁)⟩ theorem IsNormal.le_set' {f o} (H : IsNormal f) (p : Set α) (p0 : p.Nonempty) (g : α → Ordinal) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, g a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f (g a) ≤ o := by simpa [H₂] using H.le_set (g '' p) (p0.image g) b theorem IsNormal.refl : IsNormal id := ⟨lt_succ, fun _o l _a => Ordinal.limit_le l⟩ theorem IsNormal.trans {f g} (H₁ : IsNormal f) (H₂ : IsNormal g) : IsNormal (f ∘ g) := ⟨fun _x => H₁.lt_iff.2 (H₂.1 _), fun o l _a => H₁.le_set' (· < o) ⟨0, l.pos⟩ g _ fun _c => H₂.2 _ l _⟩ theorem IsNormal.isLimit {f} (H : IsNormal f) {o} (ho : IsLimit o) : IsLimit (f o) := by rw [isLimit_iff, isSuccPrelimit_iff_succ_lt] use (H.lt_iff.2 ho.pos).ne_bot intro a ha obtain ⟨b, hb, hab⟩ := (H.limit_lt ho).1 ha rw [← succ_le_iff] at hab apply hab.trans_lt rwa [H.lt_iff] theorem add_le_of_limit {a b c : Ordinal} (h : IsLimit b) : a + b ≤ c ↔ ∀ b' < b, a + b' ≤ c := ⟨fun h _ l => (add_le_add_left l.le _).trans h, fun H => le_of_not_lt <| by -- Porting note: `induction` tactics are required because of the parser bug. induction a using inductionOn with | H α r => induction b using inductionOn with | H β s => intro l suffices ∀ x : β, Sum.Lex r s (Sum.inr x) (enum _ ⟨_, l⟩) by -- Porting note: `revert` & `intro` is required because `cases'` doesn't replace -- `enum _ _ l` in `this`. revert this; rcases enum _ ⟨_, l⟩ with x | x <;> intro this · cases this (enum s ⟨0, h.pos⟩) · exact irrefl _ (this _) intro x rw [← typein_lt_typein (Sum.Lex r s), typein_enum] have := H _ (h.succ_lt (typein_lt_type s x)) rw [add_succ, succ_le_iff] at this refine (RelEmbedding.ofMonotone (fun a => ?_) fun a b => ?_).ordinal_type_le.trans_lt this · rcases a with ⟨a | b, h⟩ · exact Sum.inl a · exact Sum.inr ⟨b, by cases h; assumption⟩ · rcases a with ⟨a | a, h₁⟩ <;> rcases b with ⟨b | b, h₂⟩ <;> cases h₁ <;> cases h₂ <;> rintro ⟨⟩ <;> constructor <;> assumption⟩ theorem isNormal_add_right (a : Ordinal) : IsNormal (a + ·) := ⟨fun b => (add_lt_add_iff_left a).2 (lt_succ b), fun _b l _c => add_le_of_limit l⟩ theorem isLimit_add (a) {b} : IsLimit b → IsLimit (a + b) := (isNormal_add_right a).isLimit alias IsLimit.add := isLimit_add /-! ### Subtraction on ordinals -/ /-- The set in the definition of subtraction is nonempty. -/ private theorem sub_nonempty {a b : Ordinal} : { o | a ≤ b + o }.Nonempty := ⟨a, le_add_left _ _⟩ /-- `a - b` is the unique ordinal satisfying `b + (a - b) = a` when `b ≤ a`. -/ instance sub : Sub Ordinal := ⟨fun a b => sInf { o | a ≤ b + o }⟩ theorem le_add_sub (a b : Ordinal) : a ≤ b + (a - b) := csInf_mem sub_nonempty theorem sub_le {a b c : Ordinal} : a - b ≤ c ↔ a ≤ b + c := ⟨fun h => (le_add_sub a b).trans (add_le_add_left h _), fun h => csInf_le' h⟩ theorem lt_sub {a b c : Ordinal} : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le sub_le theorem add_sub_cancel (a b : Ordinal) : a + b - a = b := le_antisymm (sub_le.2 <| le_rfl) ((add_le_add_iff_left a).1 <| le_add_sub _ _) theorem sub_eq_of_add_eq {a b c : Ordinal} (h : a + b = c) : c - a = b := h ▸ add_sub_cancel _ _ theorem sub_le_self (a b : Ordinal) : a - b ≤ a := sub_le.2 <| le_add_left _ _ protected theorem add_sub_cancel_of_le {a b : Ordinal} (h : b ≤ a) : b + (a - b) = a := (le_add_sub a b).antisymm' (by rcases zero_or_succ_or_limit (a - b) with (e | ⟨c, e⟩ | l) · simp only [e, add_zero, h] · rw [e, add_succ, succ_le_iff, ← lt_sub, e] exact lt_succ c · exact (add_le_of_limit l).2 fun c l => (lt_sub.1 l).le) theorem le_sub_of_le {a b c : Ordinal} (h : b ≤ a) : c ≤ a - b ↔ b + c ≤ a := by rw [← add_le_add_iff_left b, Ordinal.add_sub_cancel_of_le h] theorem sub_lt_of_le {a b c : Ordinal} (h : b ≤ a) : a - b < c ↔ a < b + c := lt_iff_lt_of_le_iff_le (le_sub_of_le h) instance existsAddOfLE : ExistsAddOfLE Ordinal := ⟨fun h => ⟨_, (Ordinal.add_sub_cancel_of_le h).symm⟩⟩ @[simp] theorem sub_zero (a : Ordinal) : a - 0 = a := by simpa only [zero_add] using add_sub_cancel 0 a @[simp] theorem zero_sub (a : Ordinal) : 0 - a = 0 := by rw [← Ordinal.le_zero]; apply sub_le_self @[simp] theorem sub_self (a : Ordinal) : a - a = 0 := by simpa only [add_zero] using add_sub_cancel a 0 protected theorem sub_eq_zero_iff_le {a b : Ordinal} : a - b = 0 ↔ a ≤ b := ⟨fun h => by simpa only [h, add_zero] using le_add_sub a b, fun h => by rwa [← Ordinal.le_zero, sub_le, add_zero]⟩ protected theorem sub_ne_zero_iff_lt {a b : Ordinal} : a - b ≠ 0 ↔ b < a := by simpa using Ordinal.sub_eq_zero_iff_le.not theorem sub_sub (a b c : Ordinal) : a - b - c = a - (b + c) := eq_of_forall_ge_iff fun d => by rw [sub_le, sub_le, sub_le, add_assoc] @[simp] theorem add_sub_add_cancel (a b c : Ordinal) : a + b - (a + c) = b - c := by rw [← sub_sub, add_sub_cancel] theorem le_sub_of_add_le {a b c : Ordinal} (h : b + c ≤ a) : c ≤ a - b := by rw [← add_le_add_iff_left b] exact h.trans (le_add_sub a b) theorem sub_lt_of_lt_add {a b c : Ordinal} (h : a < b + c) (hc : 0 < c) : a - b < c := by obtain hab | hba := lt_or_le a b · rwa [Ordinal.sub_eq_zero_iff_le.2 hab.le] · rwa [sub_lt_of_le hba] theorem lt_add_iff {a b c : Ordinal} (hc : c ≠ 0) : a < b + c ↔ ∃ d < c, a ≤ b + d := by use fun h ↦ ⟨_, sub_lt_of_lt_add h hc.bot_lt, le_add_sub a b⟩ rintro ⟨d, hd, ha⟩ exact ha.trans_lt (add_lt_add_left hd b) theorem add_le_iff {a b c : Ordinal} (hb : b ≠ 0) : a + b ≤ c ↔ ∀ d < b, a + d < c := by simpa using (lt_add_iff hb).not @[deprecated add_le_iff (since := "2024-12-08")] theorem add_le_of_forall_add_lt {a b c : Ordinal} (hb : 0 < b) (h : ∀ d < b, a + d < c) : a + b ≤ c := (add_le_iff hb.ne').2 h theorem isLimit_sub {a b} (ha : IsLimit a) (h : b < a) : IsLimit (a - b) := by rw [isLimit_iff, Ordinal.sub_ne_zero_iff_lt, isSuccPrelimit_iff_succ_lt] refine ⟨h, fun c hc ↦ ?_⟩ rw [lt_sub] at hc ⊢ rw [add_succ] exact ha.succ_lt hc /-! ### Multiplication of ordinals -/ /-- The multiplication of ordinals `o₁` and `o₂` is the (well founded) lexicographic order on `o₂ × o₁`. -/ instance monoid : Monoid Ordinal.{u} where mul a b := Quotient.liftOn₂ a b (fun ⟨α, r, _⟩ ⟨β, s, _⟩ => ⟦⟨β × α, Prod.Lex s r, inferInstance⟩⟧ : WellOrder → WellOrder → Ordinal) fun ⟨_, _, _⟩ _ _ _ ⟨f⟩ ⟨g⟩ => Quot.sound ⟨RelIso.prodLexCongr g f⟩ one := 1 mul_assoc a b c := Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ => Eq.symm <| Quotient.sound ⟨⟨prodAssoc _ _ _, @fun a b => by rcases a with ⟨⟨a₁, a₂⟩, a₃⟩ rcases b with ⟨⟨b₁, b₂⟩, b₃⟩ simp [Prod.lex_def, and_or_left, or_assoc, and_assoc]⟩⟩ mul_one a := inductionOn a fun α r _ => Quotient.sound ⟨⟨punitProd _, @fun a b => by rcases a with ⟨⟨⟨⟩⟩, a⟩; rcases b with ⟨⟨⟨⟩⟩, b⟩ simp only [Prod.lex_def, EmptyRelation, false_or] simp only [eq_self_iff_true, true_and] rfl⟩⟩ one_mul a := inductionOn a fun α r _ => Quotient.sound ⟨⟨prodPUnit _, @fun a b => by rcases a with ⟨a, ⟨⟨⟩⟩⟩; rcases b with ⟨b, ⟨⟨⟩⟩⟩ simp only [Prod.lex_def, EmptyRelation, and_false, or_false] rfl⟩⟩ @[simp] theorem type_prod_lex {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [IsWellOrder α r] [IsWellOrder β s] : type (Prod.Lex s r) = type r * type s := rfl private theorem mul_eq_zero' {a b : Ordinal} : a * b = 0 ↔ a = 0 ∨ b = 0 := inductionOn a fun α _ _ => inductionOn b fun β _ _ => by simp_rw [← type_prod_lex, type_eq_zero_iff_isEmpty] rw [or_comm] exact isEmpty_prod instance monoidWithZero : MonoidWithZero Ordinal := { Ordinal.monoid with zero := 0 mul_zero := fun _a => mul_eq_zero'.2 <| Or.inr rfl zero_mul := fun _a => mul_eq_zero'.2 <| Or.inl rfl } instance noZeroDivisors : NoZeroDivisors Ordinal := ⟨fun {_ _} => mul_eq_zero'.1⟩ @[simp] theorem lift_mul (a b : Ordinal.{v}) : lift.{u} (a * b) = lift.{u} a * lift.{u} b := Quotient.inductionOn₂ a b fun ⟨_α, _r, _⟩ ⟨_β, _s, _⟩ => Quotient.sound ⟨(RelIso.preimage Equiv.ulift _).trans (RelIso.prodLexCongr (RelIso.preimage Equiv.ulift _) (RelIso.preimage Equiv.ulift _)).symm⟩ @[simp] theorem card_mul (a b) : card (a * b) = card a * card b := Quotient.inductionOn₂ a b fun ⟨α, _r, _⟩ ⟨β, _s, _⟩ => mul_comm #β #α instance leftDistribClass : LeftDistribClass Ordinal.{u} := ⟨fun a b c => Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ => Quotient.sound ⟨⟨sumProdDistrib _ _ _, by rintro ⟨a₁ | a₁, a₂⟩ ⟨b₁ | b₁, b₂⟩ <;> simp only [Prod.lex_def, Sum.lex_inl_inl, Sum.Lex.sep, Sum.lex_inr_inl, Sum.lex_inr_inr, sumProdDistrib_apply_left, sumProdDistrib_apply_right, reduceCtorEq] <;> -- Porting note: `Sum.inr.inj_iff` is required. simp only [Sum.inl.inj_iff, Sum.inr.inj_iff, true_or, false_and, false_or]⟩⟩⟩ theorem mul_succ (a b : Ordinal) : a * succ b = a * b + a := mul_add_one a b instance mulLeftMono : MulLeftMono Ordinal.{u} := ⟨fun c a b => Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ => by refine (RelEmbedding.ofMonotone (fun a : α × γ => (f a.1, a.2)) fun a b h => ?_).ordinal_type_le obtain ⟨-, -, h'⟩ | ⟨-, h'⟩ := h · exact Prod.Lex.left _ _ (f.toRelEmbedding.map_rel_iff.2 h') · exact Prod.Lex.right _ h'⟩ instance mulRightMono : MulRightMono Ordinal.{u} := ⟨fun c a b => Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ => by refine (RelEmbedding.ofMonotone (fun a : γ × α => (a.1, f a.2)) fun a b h => ?_).ordinal_type_le obtain ⟨-, -, h'⟩ | ⟨-, h'⟩ := h · exact Prod.Lex.left _ _ h' · exact Prod.Lex.right _ (f.toRelEmbedding.map_rel_iff.2 h')⟩ theorem le_mul_left (a : Ordinal) {b : Ordinal} (hb : 0 < b) : a ≤ a * b := by convert mul_le_mul_left' (one_le_iff_pos.2 hb) a rw [mul_one a] theorem le_mul_right (a : Ordinal) {b : Ordinal} (hb : 0 < b) : a ≤ b * a := by convert mul_le_mul_right' (one_le_iff_pos.2 hb) a rw [one_mul a] private theorem mul_le_of_limit_aux {α β r s} [IsWellOrder α r] [IsWellOrder β s] {c} (h : IsLimit (type s)) (H : ∀ b' < type s, type r * b' ≤ c) (l : c < type r * type s) : False := by suffices ∀ a b, Prod.Lex s r (b, a) (enum _ ⟨_, l⟩) by obtain ⟨b, a⟩ := enum _ ⟨_, l⟩ exact irrefl _ (this _ _) intro a b rw [← typein_lt_typein (Prod.Lex s r), typein_enum] have := H _ (h.succ_lt (typein_lt_type s b)) rw [mul_succ] at this have := ((add_lt_add_iff_left _).2 (typein_lt_type _ a)).trans_le this refine (RelEmbedding.ofMonotone (fun a => ?_) fun a b => ?_).ordinal_type_le.trans_lt this · rcases a with ⟨⟨b', a'⟩, h⟩ by_cases e : b = b' · refine Sum.inr ⟨a', ?_⟩ subst e obtain ⟨-, -, h⟩ | ⟨-, h⟩ := h · exact (irrefl _ h).elim · exact h · refine Sum.inl (⟨b', ?_⟩, a') obtain ⟨-, -, h⟩ | ⟨e, h⟩ := h · exact h · exact (e rfl).elim · rcases a with ⟨⟨b₁, a₁⟩, h₁⟩ rcases b with ⟨⟨b₂, a₂⟩, h₂⟩ intro h by_cases e₁ : b = b₁ <;> by_cases e₂ : b = b₂ · substs b₁ b₂ simpa only [subrel_val, Prod.lex_def, @irrefl _ s _ b, true_and, false_or, eq_self_iff_true, dif_pos, Sum.lex_inr_inr] using h · subst b₁ simp only [subrel_val, Prod.lex_def, e₂, Prod.lex_def, dif_pos, subrel_val, eq_self_iff_true, or_false, dif_neg, not_false_iff, Sum.lex_inr_inl, false_and] at h ⊢ obtain ⟨-, -, h₂_h⟩ | e₂ := h₂ <;> [exact asymm h h₂_h; exact e₂ rfl] · simp [e₂, dif_neg e₁, show b₂ ≠ b₁ from e₂ ▸ e₁] · simpa only [dif_neg e₁, dif_neg e₂, Prod.lex_def, subrel_val, Subtype.mk_eq_mk, Sum.lex_inl_inl] using h theorem mul_le_of_limit {a b c : Ordinal} (h : IsLimit b) : a * b ≤ c ↔ ∀ b' < b, a * b' ≤ c := ⟨fun h _ l => (mul_le_mul_left' l.le _).trans h, fun H => -- Porting note: `induction` tactics are required because of the parser bug. le_of_not_lt <| by induction a using inductionOn with | H α r => induction b using inductionOn with | H β s => exact mul_le_of_limit_aux h H⟩ theorem isNormal_mul_right {a : Ordinal} (h : 0 < a) : IsNormal (a * ·) := -- Porting note (https://github.com/leanprover-community/mathlib4/issues/12129): additional beta reduction needed ⟨fun b => by beta_reduce rw [mul_succ] simpa only [add_zero] using (add_lt_add_iff_left (a * b)).2 h, fun _ l _ => mul_le_of_limit l⟩ theorem lt_mul_of_limit {a b c : Ordinal} (h : IsLimit c) : a < b * c ↔ ∃ c' < c, a < b * c' := by -- Porting note: `bex_def` is required. simpa only [not_forall₂, not_le, bex_def] using not_congr (@mul_le_of_limit b c a h) theorem mul_lt_mul_iff_left {a b c : Ordinal} (a0 : 0 < a) : a * b < a * c ↔ b < c := (isNormal_mul_right a0).lt_iff theorem mul_le_mul_iff_left {a b c : Ordinal} (a0 : 0 < a) : a * b ≤ a * c ↔ b ≤ c := (isNormal_mul_right a0).le_iff theorem mul_lt_mul_of_pos_left {a b c : Ordinal} (h : a < b) (c0 : 0 < c) : c * a < c * b := (mul_lt_mul_iff_left c0).2 h theorem mul_pos {a b : Ordinal} (h₁ : 0 < a) (h₂ : 0 < b) : 0 < a * b := by simpa only [mul_zero] using mul_lt_mul_of_pos_left h₂ h₁ theorem mul_ne_zero {a b : Ordinal} : a ≠ 0 → b ≠ 0 → a * b ≠ 0 := by simpa only [Ordinal.pos_iff_ne_zero] using mul_pos theorem le_of_mul_le_mul_left {a b c : Ordinal} (h : c * a ≤ c * b) (h0 : 0 < c) : a ≤ b := le_imp_le_of_lt_imp_lt (fun h' => mul_lt_mul_of_pos_left h' h0) h theorem mul_right_inj {a b c : Ordinal} (a0 : 0 < a) : a * b = a * c ↔ b = c := (isNormal_mul_right a0).inj theorem isLimit_mul {a b : Ordinal} (a0 : 0 < a) : IsLimit b → IsLimit (a * b) := (isNormal_mul_right a0).isLimit theorem isLimit_mul_left {a b : Ordinal} (l : IsLimit a) (b0 : 0 < b) : IsLimit (a * b) := by rcases zero_or_succ_or_limit b with (rfl | ⟨b, rfl⟩ | lb) · exact b0.false.elim · rw [mul_succ] exact isLimit_add _ l · exact isLimit_mul l.pos lb theorem smul_eq_mul : ∀ (n : ℕ) (a : Ordinal), n • a = a * n | 0, a => by rw [zero_nsmul, Nat.cast_zero, mul_zero] | n + 1, a => by rw [succ_nsmul, Nat.cast_add, mul_add, Nat.cast_one, mul_one, smul_eq_mul n] private theorem add_mul_limit_aux {a b c : Ordinal} (ba : b + a = a) (l : IsLimit c) (IH : ∀ c' < c, (a + b) * succ c' = a * succ c' + b) : (a + b) * c = a * c := le_antisymm ((mul_le_of_limit l).2 fun c' h => by apply (mul_le_mul_left' (le_succ c') _).trans rw [IH _ h] apply (add_le_add_left _ _).trans · rw [← mul_succ] exact mul_le_mul_left' (succ_le_of_lt <| l.succ_lt h) _ · rw [← ba] exact le_add_right _ _) (mul_le_mul_right' (le_add_right _ _) _) theorem add_mul_succ {a b : Ordinal} (c) (ba : b + a = a) : (a + b) * succ c = a * succ c + b := by induction c using limitRecOn with | zero => simp only [succ_zero, mul_one] | succ c IH => rw [mul_succ, IH, ← add_assoc, add_assoc _ b, ba, ← mul_succ] | isLimit c l IH => rw [mul_succ, add_mul_limit_aux ba l IH, mul_succ, add_assoc] theorem add_mul_limit {a b c : Ordinal} (ba : b + a = a) (l : IsLimit c) : (a + b) * c = a * c := add_mul_limit_aux ba l fun c' _ => add_mul_succ c' ba /-! ### Division on ordinals -/ /-- The set in the definition of division is nonempty. -/ private theorem div_nonempty {a b : Ordinal} (h : b ≠ 0) : { o | a < b * succ o }.Nonempty := ⟨a, (succ_le_iff (a := a) (b := b * succ a)).1 <| by simpa only [succ_zero, one_mul] using mul_le_mul_right' (succ_le_of_lt (Ordinal.pos_iff_ne_zero.2 h)) (succ a)⟩ /-- `a / b` is the unique ordinal `o` satisfying `a = b * o + o'` with `o' < b`. -/ instance div : Div Ordinal := ⟨fun a b => if b = 0 then 0 else sInf { o | a < b * succ o }⟩ @[simp] theorem div_zero (a : Ordinal) : a / 0 = 0 := dif_pos rfl private theorem div_def (a) {b : Ordinal} (h : b ≠ 0) : a / b = sInf { o | a < b * succ o } := dif_neg h theorem lt_mul_succ_div (a) {b : Ordinal} (h : b ≠ 0) : a < b * succ (a / b) := by rw [div_def a h]; exact csInf_mem (div_nonempty h) theorem lt_mul_div_add (a) {b : Ordinal} (h : b ≠ 0) : a < b * (a / b) + b := by simpa only [mul_succ] using lt_mul_succ_div a h theorem div_le {a b c : Ordinal} (b0 : b ≠ 0) : a / b ≤ c ↔ a < b * succ c := ⟨fun h => (lt_mul_succ_div a b0).trans_le (mul_le_mul_left' (succ_le_succ_iff.2 h) _), fun h => by rw [div_def a b0]; exact csInf_le' h⟩ theorem lt_div {a b c : Ordinal} (h : c ≠ 0) : a < b / c ↔ c * succ a ≤ b := by rw [← not_le, div_le h, not_lt] theorem div_pos {b c : Ordinal} (h : c ≠ 0) : 0 < b / c ↔ c ≤ b := by simp [lt_div h] theorem le_div {a b c : Ordinal} (c0 : c ≠ 0) : a ≤ b / c ↔ c * a ≤ b := by induction a using limitRecOn with | zero => simp only [mul_zero, Ordinal.zero_le] | succ _ _ => rw [succ_le_iff, lt_div c0] | isLimit _ h₁ h₂ => revert h₁ h₂ simp +contextual only [mul_le_of_limit, limit_le, forall_true_iff] theorem div_lt {a b c : Ordinal} (b0 : b ≠ 0) : a / b < c ↔ a < b * c := lt_iff_lt_of_le_iff_le <| le_div b0 theorem div_le_of_le_mul {a b c : Ordinal} (h : a ≤ b * c) : a / b ≤ c := if b0 : b = 0 then by simp only [b0, div_zero, Ordinal.zero_le] else (div_le b0).2 <| h.trans_lt <| mul_lt_mul_of_pos_left (lt_succ c) (Ordinal.pos_iff_ne_zero.2 b0) theorem mul_lt_of_lt_div {a b c : Ordinal} : a < b / c → c * a < b := lt_imp_lt_of_le_imp_le div_le_of_le_mul @[simp] theorem zero_div (a : Ordinal) : 0 / a = 0 := Ordinal.le_zero.1 <| div_le_of_le_mul <| Ordinal.zero_le _ theorem mul_div_le (a b : Ordinal) : b * (a / b) ≤ a := if b0 : b = 0 then by simp only [b0, zero_mul, Ordinal.zero_le] else (le_div b0).1 le_rfl theorem div_le_left {a b : Ordinal} (h : a ≤ b) (c : Ordinal) : a / c ≤ b / c := by obtain rfl | hc := eq_or_ne c 0 · rw [div_zero, div_zero] · rw [le_div hc] exact (mul_div_le a c).trans h theorem mul_add_div (a) {b : Ordinal} (b0 : b ≠ 0) (c) : (b * a + c) / b = a + c / b := by apply le_antisymm · apply (div_le b0).2 rw [mul_succ, mul_add, add_assoc, add_lt_add_iff_left] apply lt_mul_div_add _ b0 · rw [le_div b0, mul_add, add_le_add_iff_left] apply mul_div_le theorem div_eq_zero_of_lt {a b : Ordinal} (h : a < b) : a / b = 0 := by rw [← Ordinal.le_zero, div_le <| Ordinal.pos_iff_ne_zero.1 <| (Ordinal.zero_le _).trans_lt h] simpa only [succ_zero, mul_one] using h @[simp] theorem mul_div_cancel (a) {b : Ordinal} (b0 : b ≠ 0) : b * a / b = a := by simpa only [add_zero, zero_div] using mul_add_div a b0 0 theorem mul_add_div_mul {a c : Ordinal} (hc : c < a) (b d : Ordinal) : (a * b + c) / (a * d) = b / d := by have ha : a ≠ 0 := ((Ordinal.zero_le c).trans_lt hc).ne' obtain rfl | hd := eq_or_ne d 0 · rw [mul_zero, div_zero, div_zero] · have H := mul_ne_zero ha hd apply le_antisymm · rw [← lt_succ_iff, div_lt H, mul_assoc] · apply (add_lt_add_left hc _).trans_le rw [← mul_succ] apply mul_le_mul_left' rw [succ_le_iff] exact lt_mul_succ_div b hd · rw [le_div H, mul_assoc] exact (mul_le_mul_left' (mul_div_le b d) a).trans (le_add_right _ c) theorem mul_div_mul_cancel {a : Ordinal} (ha : a ≠ 0) (b c) : a * b / (a * c) = b / c := by convert mul_add_div_mul (Ordinal.pos_iff_ne_zero.2 ha) b c using 1 rw [add_zero] @[simp] theorem div_one (a : Ordinal) : a / 1 = a := by simpa only [one_mul] using mul_div_cancel a Ordinal.one_ne_zero @[simp] theorem div_self {a : Ordinal} (h : a ≠ 0) : a / a = 1 := by simpa only [mul_one] using mul_div_cancel 1 h theorem mul_sub (a b c : Ordinal) : a * (b - c) = a * b - a * c := if a0 : a = 0 then by simp only [a0, zero_mul, sub_self] else eq_of_forall_ge_iff fun d => by rw [sub_le, ← le_div a0, sub_le, ← le_div a0, mul_add_div _ a0] theorem isLimit_add_iff {a b} : IsLimit (a + b) ↔ IsLimit b ∨ b = 0 ∧ IsLimit a := by constructor <;> intro h · by_cases h' : b = 0 · rw [h', add_zero] at h right exact ⟨h', h⟩ left rw [← add_sub_cancel a b] apply isLimit_sub h suffices a + 0 < a + b by simpa only [add_zero] using this rwa [add_lt_add_iff_left, Ordinal.pos_iff_ne_zero] rcases h with (h | ⟨rfl, h⟩) · exact isLimit_add a h · simpa only [add_zero] theorem dvd_add_iff : ∀ {a b c : Ordinal}, a ∣ b → (a ∣ b + c ↔ a ∣ c) | a, _, c, ⟨b, rfl⟩ => ⟨fun ⟨d, e⟩ => ⟨d - b, by rw [mul_sub, ← e, add_sub_cancel]⟩, fun ⟨d, e⟩ => by rw [e, ← mul_add] apply dvd_mul_right⟩ theorem div_mul_cancel : ∀ {a b : Ordinal}, a ≠ 0 → a ∣ b → a * (b / a) = b | a, _, a0, ⟨b, rfl⟩ => by rw [mul_div_cancel _ a0] theorem le_of_dvd : ∀ {a b : Ordinal}, b ≠ 0 → a ∣ b → a ≤ b -- Porting note: `⟨b, rfl⟩ => by` → `⟨b, e⟩ => by subst e` | a, _, b0, ⟨b, e⟩ => by subst e -- Porting note: `Ne` is required. simpa only [mul_one] using mul_le_mul_left' (one_le_iff_ne_zero.2 fun h : b = 0 => by simp only [h, mul_zero, Ne, not_true_eq_false] at b0) a theorem dvd_antisymm {a b : Ordinal} (h₁ : a ∣ b) (h₂ : b ∣ a) : a = b := if a0 : a = 0 then by subst a; exact (eq_zero_of_zero_dvd h₁).symm else if b0 : b = 0 then by subst b; exact eq_zero_of_zero_dvd h₂ else (le_of_dvd b0 h₁).antisymm (le_of_dvd a0 h₂) instance isAntisymm : IsAntisymm Ordinal (· ∣ ·) := ⟨@dvd_antisymm⟩ /-- `a % b` is the unique ordinal `o'` satisfying `a = b * o + o'` with `o' < b`. -/ instance mod : Mod Ordinal := ⟨fun a b => a - b * (a / b)⟩ theorem mod_def (a b : Ordinal) : a % b = a - b * (a / b) := rfl theorem mod_le (a b : Ordinal) : a % b ≤ a := sub_le_self a _ @[simp] theorem mod_zero (a : Ordinal) : a % 0 = a := by simp only [mod_def, div_zero, zero_mul, sub_zero] theorem mod_eq_of_lt {a b : Ordinal} (h : a < b) : a % b = a := by simp only [mod_def, div_eq_zero_of_lt h, mul_zero, sub_zero] @[simp] theorem zero_mod (b : Ordinal) : 0 % b = 0 := by simp only [mod_def, zero_div, mul_zero, sub_self] theorem div_add_mod (a b : Ordinal) : b * (a / b) + a % b = a := Ordinal.add_sub_cancel_of_le <| mul_div_le _ _ theorem mod_lt (a) {b : Ordinal} (h : b ≠ 0) : a % b < b := (add_lt_add_iff_left (b * (a / b))).1 <| by rw [div_add_mod]; exact lt_mul_div_add a h @[simp] theorem mod_self (a : Ordinal) : a % a = 0 := if a0 : a = 0 then by simp only [a0, zero_mod] else by simp only [mod_def, div_self a0, mul_one, sub_self] @[simp] theorem mod_one (a : Ordinal) : a % 1 = 0 := by simp only [mod_def, div_one, one_mul, sub_self] theorem dvd_of_mod_eq_zero {a b : Ordinal} (H : a % b = 0) : b ∣ a := ⟨a / b, by simpa [H] using (div_add_mod a b).symm⟩ theorem mod_eq_zero_of_dvd {a b : Ordinal} (H : b ∣ a) : a % b = 0 := by rcases H with ⟨c, rfl⟩ rcases eq_or_ne b 0 with (rfl | hb) · simp · simp [mod_def, hb] theorem dvd_iff_mod_eq_zero {a b : Ordinal} : b ∣ a ↔ a % b = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ @[simp] theorem mul_add_mod_self (x y z : Ordinal) : (x * y + z) % x = z % x := by rcases eq_or_ne x 0 with rfl | hx · simp · rwa [mod_def, mul_add_div, mul_add, ← sub_sub, add_sub_cancel, mod_def] @[simp] theorem mul_mod (x y : Ordinal) : x * y % x = 0 := by simpa using mul_add_mod_self x y 0 theorem mul_add_mod_mul {w x : Ordinal} (hw : w < x) (y z : Ordinal) : (x * y + w) % (x * z) = x * (y % z) + w := by rw [mod_def, mul_add_div_mul hw] apply sub_eq_of_add_eq rw [← add_assoc, mul_assoc, ← mul_add, div_add_mod] theorem mul_mod_mul (x y z : Ordinal) : (x * y) % (x * z) = x * (y % z) := by obtain rfl | hx := Ordinal.eq_zero_or_pos x · simp · convert mul_add_mod_mul hx y z using 1 <;> rw [add_zero] theorem mod_mod_of_dvd (a : Ordinal) {b c : Ordinal} (h : c ∣ b) : a % b % c = a % c := by nth_rw 2 [← div_add_mod a b] rcases h with ⟨d, rfl⟩ rw [mul_assoc, mul_add_mod_self] @[simp] theorem mod_mod (a b : Ordinal) : a % b % b = a % b := mod_mod_of_dvd a dvd_rfl /-! ### Casting naturals into ordinals, compatibility with operations -/ instance instCharZero : CharZero Ordinal := by refine ⟨fun a b h ↦ ?_⟩ rwa [← Cardinal.ord_nat, ← Cardinal.ord_nat, Cardinal.ord_inj, Nat.cast_inj] at h @[simp] theorem one_add_natCast (m : ℕ) : 1 + (m : Ordinal) = succ m := by rw [← Nat.cast_one, ← Nat.cast_add, add_comm] rfl @[simp] theorem one_add_ofNat (m : ℕ) [m.AtLeastTwo] : 1 + (ofNat(m) : Ordinal) = Order.succ (OfNat.ofNat m : Ordinal) := one_add_natCast m @[simp, norm_cast] theorem natCast_mul (m : ℕ) : ∀ n : ℕ, ((m * n : ℕ) : Ordinal) = m * n | 0 => by simp | n + 1 => by rw [Nat.mul_succ, Nat.cast_add, natCast_mul m n, Nat.cast_succ, mul_add_one] @[simp, norm_cast] theorem natCast_sub (m n : ℕ) : ((m - n : ℕ) : Ordinal) = m - n := by rcases le_total m n with h | h · rw [tsub_eq_zero_iff_le.2 h, Ordinal.sub_eq_zero_iff_le.2 (Nat.cast_le.2 h), Nat.cast_zero] · rw [← add_left_cancel_iff (a := ↑n), ← Nat.cast_add, add_tsub_cancel_of_le h, Ordinal.add_sub_cancel_of_le (Nat.cast_le.2 h)] @[simp, norm_cast] theorem natCast_div (m n : ℕ) : ((m / n : ℕ) : Ordinal) = m / n := by rcases eq_or_ne n 0 with (rfl | hn) · simp · have hn' : (n : Ordinal) ≠ 0 := Nat.cast_ne_zero.2 hn apply le_antisymm · rw [le_div hn', ← natCast_mul, Nat.cast_le, mul_comm] apply Nat.div_mul_le_self · rw [div_le hn', ← add_one_eq_succ, ← Nat.cast_succ, ← natCast_mul, Nat.cast_lt, mul_comm, ← Nat.div_lt_iff_lt_mul (Nat.pos_of_ne_zero hn)] apply Nat.lt_succ_self @[simp, norm_cast] theorem natCast_mod (m n : ℕ) : ((m % n : ℕ) : Ordinal) = m % n := by rw [← add_left_cancel_iff, div_add_mod, ← natCast_div, ← natCast_mul, ← Nat.cast_add, Nat.div_add_mod] @[simp] theorem lift_natCast : ∀ n : ℕ, lift.{u, v} n = n | 0 => by simp | n + 1 => by simp [lift_natCast n] @[simp] theorem lift_ofNat (n : ℕ) [n.AtLeastTwo] : lift.{u, v} ofNat(n) = OfNat.ofNat n := lift_natCast n theorem lt_omega0 {o : Ordinal} : o < ω ↔ ∃ n : ℕ, o = n := by simp_rw [← Cardinal.ord_aleph0, Cardinal.lt_ord, lt_aleph0, card_eq_nat] theorem nat_lt_omega0 (n : ℕ) : ↑n < ω := lt_omega0.2 ⟨_, rfl⟩ theorem eq_nat_or_omega0_le (o : Ordinal) : (∃ n : ℕ, o = n) ∨ ω ≤ o := by obtain ho | ho := lt_or_le o ω · exact Or.inl <| lt_omega0.1 ho · exact Or.inr ho theorem omega0_pos : 0 < ω := nat_lt_omega0 0 theorem omega0_ne_zero : ω ≠ 0 := omega0_pos.ne' theorem one_lt_omega0 : 1 < ω := by simpa only [Nat.cast_one] using nat_lt_omega0 1 theorem isLimit_omega0 : IsLimit ω := by rw [isLimit_iff, isSuccPrelimit_iff_succ_lt] refine ⟨omega0_ne_zero, fun o h => ?_⟩ obtain ⟨n, rfl⟩ := lt_omega0.1 h exact nat_lt_omega0 (n + 1) theorem omega0_le {o : Ordinal} : ω ≤ o ↔ ∀ n : ℕ, ↑n ≤ o := ⟨fun h n => (nat_lt_omega0 _).le.trans h, fun H => le_of_forall_lt fun a h => by let ⟨n, e⟩ := lt_omega0.1 h rw [e, ← succ_le_iff]; exact H (n + 1)⟩ theorem nat_lt_limit {o} (h : IsLimit o) : ∀ n : ℕ, ↑n < o | 0 => h.pos | n + 1 => h.succ_lt (nat_lt_limit h n) theorem omega0_le_of_isLimit {o} (h : IsLimit o) : ω ≤ o := omega0_le.2 fun n => le_of_lt <| nat_lt_limit h n theorem natCast_add_omega0 (n : ℕ) : n + ω = ω := by refine le_antisymm (le_of_forall_lt fun a ha ↦ ?_) (le_add_left _ _) obtain ⟨b, hb', hb⟩ := (lt_add_iff omega0_ne_zero).1 ha obtain ⟨m, rfl⟩ := lt_omega0.1 hb' apply hb.trans_lt exact_mod_cast nat_lt_omega0 (n + m) theorem one_add_omega0 : 1 + ω = ω := mod_cast natCast_add_omega0 1 theorem add_omega0 {a : Ordinal} (h : a < ω) : a + ω = ω := by obtain ⟨n, rfl⟩ := lt_omega0.1 h exact natCast_add_omega0 n @[simp] theorem natCast_add_of_omega0_le {o} (h : ω ≤ o) (n : ℕ) : n + o = o := by rw [← Ordinal.add_sub_cancel_of_le h, ← add_assoc, natCast_add_omega0] @[simp] theorem one_add_of_omega0_le {o} (h : ω ≤ o) : 1 + o = o := mod_cast natCast_add_of_omega0_le h 1 open Ordinal theorem isLimit_iff_omega0_dvd {a : Ordinal} : IsLimit a ↔ a ≠ 0 ∧ ω ∣ a := by refine ⟨fun l => ⟨l.ne_zero, ⟨a / ω, le_antisymm ?_ (mul_div_le _ _)⟩⟩, fun h => ?_⟩ · refine (limit_le l).2 fun x hx => le_of_lt ?_ rw [← div_lt omega0_ne_zero, ← succ_le_iff, le_div omega0_ne_zero, mul_succ, add_le_of_limit isLimit_omega0] intro b hb rcases lt_omega0.1 hb with ⟨n, rfl⟩ exact (add_le_add_right (mul_div_le _ _) _).trans (lt_sub.1 <| nat_lt_limit (isLimit_sub l hx) _).le · rcases h with ⟨a0, b, rfl⟩ refine isLimit_mul_left isLimit_omega0 (Ordinal.pos_iff_ne_zero.2 <| mt ?_ a0) intro e simp only [e, mul_zero] @[simp] theorem natCast_mod_omega0 (n : ℕ) : n % ω = n := mod_eq_of_lt (nat_lt_omega0 n) end Ordinal namespace Cardinal open Ordinal @[simp] theorem add_one_of_aleph0_le {c} (h : ℵ₀ ≤ c) : c + 1 = c := by rw [add_comm, ← card_ord c, ← card_one, ← card_add, one_add_of_omega0_le] rwa [← ord_aleph0, ord_le_ord] theorem isLimit_ord {c} (co : ℵ₀ ≤ c) : (ord c).IsLimit := by rw [isLimit_iff, isSuccPrelimit_iff_succ_lt] refine ⟨fun h => aleph0_ne_zero ?_, fun a => lt_imp_lt_of_le_imp_le fun h => ?_⟩ · rw [← Ordinal.le_zero, ord_le] at h simpa only [card_zero, nonpos_iff_eq_zero] using co.trans h · rw [ord_le] at h ⊢ rwa [← @add_one_of_aleph0_le (card a), ← card_succ] rw [← ord_le, ← le_succ_of_isLimit, ord_le] · exact co.trans h · rw [ord_aleph0] exact Ordinal.isLimit_omega0 theorem noMaxOrder {c} (h : ℵ₀ ≤ c) : NoMaxOrder c.ord.toType := toType_noMax_of_succ_lt fun _ ↦ (isLimit_ord h).succ_lt end Cardinal
Mathlib/SetTheory/Ordinal/Arithmetic.lean
1,599
1,601
/- Copyright (c) 2024 Josha Dekker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Josha Dekker -/ import Mathlib.Probability.Notation import Mathlib.Probability.CDF import Mathlib.Analysis.SpecialFunctions.Gamma.Basic /-! # Gamma distributions over ℝ Define the gamma measure over the reals. ## Main definitions * `gammaPDFReal`: the function `a r x ↦ r ^ a / (Gamma a) * x ^ (a-1) * exp (-(r * x))` for `0 ≤ x` or `0` else, which is the probability density function of a gamma distribution with shape `a` and rate `r` (when `ha : 0 < a ` and `hr : 0 < r`). * `gammaPDF`: `ℝ≥0∞`-valued pdf, `gammaPDF a r = ENNReal.ofReal (gammaPDFReal a r)`. * `gammaMeasure`: a gamma measure on `ℝ`, parametrized by its shape `a` and rate `r`. * `gammaCDFReal`: the CDF given by the definition of CDF in `ProbabilityTheory.CDF` applied to the gamma measure. -/ open scoped ENNReal NNReal open MeasureTheory Real Set Filter Topology
/-- A Lebesgue Integral from -∞ to y can be expressed as the sum of one from -∞ to 0 and 0 to x -/ lemma lintegral_Iic_eq_lintegral_Iio_add_Icc {y z : ℝ} (f : ℝ → ℝ≥0∞) (hzy : z ≤ y) : ∫⁻ x in Iic y, f x = (∫⁻ x in Iio z, f x) + ∫⁻ x in Icc z y, f x := by rw [← Iio_union_Icc_eq_Iic hzy, lintegral_union measurableSet_Icc] simp_rw [Set.disjoint_iff_forall_ne, mem_Iio, mem_Icc] intros linarith
Mathlib/Probability/Distributions/Gamma.lean
29
35
/- 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.Analysis.SpecificLimits.Basic import Mathlib.Analysis.SpecialFunctions.Pow.Real /-! # Results on discretized exponentials We state several auxiliary results pertaining to sequences of the form `⌊c^n⌋₊`. * `tendsto_div_of_monotone_of_tendsto_div_floor_pow`: If a monotone sequence `u` is such that `u ⌊c^n⌋₊ / ⌊c^n⌋₊` converges to a limit `l` for all `c > 1`, then `u n / n` tends to `l`. * `sum_div_nat_floor_pow_sq_le_div_sq`: The sum of `1/⌊c^i⌋₊^2` above a threshold `j` is comparable to `1/j^2`, up to a multiplicative constant. -/ open Filter Finset open Topology /-- If a monotone sequence `u` is such that `u n / n` tends to a limit `l` along subsequences with exponential growth rate arbitrarily close to `1`, then `u n / n` tends to `l`. -/ theorem tendsto_div_of_monotone_of_exists_subseq_tendsto_div (u : ℕ → ℝ) (l : ℝ) (hmono : Monotone u) (hlim : ∀ a : ℝ, 1 < a → ∃ c : ℕ → ℕ, (∀ᶠ n in atTop, (c (n + 1) : ℝ) ≤ a * c n) ∧ Tendsto c atTop atTop ∧ Tendsto (fun n => u (c n) / c n) atTop (𝓝 l)) : Tendsto (fun n => u n / n) atTop (𝓝 l) := by /- To check the result up to some `ε > 0`, we use a sequence `c` for which the ratio `c (N+1) / c N` is bounded by `1 + ε`. Sandwiching a given `n` between two consecutive values of `c`, say `c N` and `c (N+1)`, one can then bound `u n / n` from above by `u (c N) / c (N - 1)` and from below by `u (c (N - 1)) / c N` (using that `u` is monotone), which are both comparable to the limit `l` up to `1 + ε`. We give a version of this proof by clearing out denominators first, to avoid discussing the sign of different quantities. -/ have lnonneg : 0 ≤ l := by rcases hlim 2 one_lt_two with ⟨c, _, ctop, clim⟩ have : Tendsto (fun n => u 0 / c n) atTop (𝓝 0) := tendsto_const_nhds.div_atTop (tendsto_natCast_atTop_iff.2 ctop) apply le_of_tendsto_of_tendsto' this clim fun n => ?_ gcongr exact hmono (zero_le _) have A : ∀ ε : ℝ, 0 < ε → ∀ᶠ n in atTop, u n - n * l ≤ ε * (1 + ε + l) * n := by intro ε εpos rcases hlim (1 + ε) ((lt_add_iff_pos_right _).2 εpos) with ⟨c, cgrowth, ctop, clim⟩ have L : ∀ᶠ n in atTop, u (c n) - c n * l ≤ ε * c n := by rw [← tendsto_sub_nhds_zero_iff, ← Asymptotics.isLittleO_one_iff ℝ, Asymptotics.isLittleO_iff] at clim filter_upwards [clim εpos, ctop (Ioi_mem_atTop 0)] with n hn cnpos' have cnpos : 0 < c n := cnpos' calc u (c n) - c n * l = (u (c n) / c n - l) * c n := by simp only [cnpos.ne', Ne, Nat.cast_eq_zero, not_false_iff, field_simps] _ ≤ ε * c n := by gcongr refine (le_abs_self _).trans ?_ simpa using hn obtain ⟨a, ha⟩ : ∃ a : ℕ, ∀ b : ℕ, a ≤ b → (c (b + 1) : ℝ) ≤ (1 + ε) * c b ∧ u (c b) - c b * l ≤ ε * c b := eventually_atTop.1 (cgrowth.and L) let M := ((Finset.range (a + 1)).image fun i => c i).max' (by simp) filter_upwards [Ici_mem_atTop M] with n hn have exN : ∃ N, n < c N := by rcases (tendsto_atTop.1 ctop (n + 1)).exists with ⟨N, hN⟩ exact ⟨N, by omega⟩ let N := Nat.find exN have ncN : n < c N := Nat.find_spec exN have aN : a + 1 ≤ N := by by_contra! h have cNM : c N ≤ M := by apply le_max' apply mem_image_of_mem exact mem_range.2 h exact lt_irrefl _ ((cNM.trans hn).trans_lt ncN) have Npos : 0 < N := lt_of_lt_of_le Nat.succ_pos' aN have cNn : c (N - 1) ≤ n := by have : N - 1 < N := Nat.pred_lt Npos.ne' simpa only [not_lt] using Nat.find_min exN this have IcN : (c N : ℝ) ≤ (1 + ε) * c (N - 1) := by have A : a ≤ N - 1 := by apply @Nat.le_of_add_le_add_right a 1 (N - 1) rw [Nat.sub_add_cancel Npos] exact aN have B : N - 1 + 1 = N := Nat.succ_pred_eq_of_pos Npos have := (ha _ A).1 rwa [B] at this calc u n - n * l ≤ u (c N) - c (N - 1) * l := by gcongr; exact hmono ncN.le _ = u (c N) - c N * l + (c N - c (N - 1)) * l := by ring _ ≤ ε * c N + ε * c (N - 1) * l := by gcongr · exact (ha N (a.le_succ.trans aN)).2 · linarith only [IcN] _ ≤ ε * ((1 + ε) * c (N - 1)) + ε * c (N - 1) * l := by gcongr _ = ε * (1 + ε + l) * c (N - 1) := by ring _ ≤ ε * (1 + ε + l) * n := by gcongr have B : ∀ ε : ℝ, 0 < ε → ∀ᶠ n : ℕ in atTop, (n : ℝ) * l - u n ≤ ε * (1 + l) * n := by intro ε εpos rcases hlim (1 + ε) ((lt_add_iff_pos_right _).2 εpos) with ⟨c, cgrowth, ctop, clim⟩ have L : ∀ᶠ n : ℕ in atTop, (c n : ℝ) * l - u (c n) ≤ ε * c n := by rw [← tendsto_sub_nhds_zero_iff, ← Asymptotics.isLittleO_one_iff ℝ, Asymptotics.isLittleO_iff] at clim filter_upwards [clim εpos, ctop (Ioi_mem_atTop 0)] with n hn cnpos' have cnpos : 0 < c n := cnpos' calc (c n : ℝ) * l - u (c n) = -(u (c n) / c n - l) * c n := by simp only [cnpos.ne', Ne, Nat.cast_eq_zero, not_false_iff, neg_sub, field_simps] _ ≤ ε * c n := by gcongr refine le_trans (neg_le_abs _) ?_ simpa using hn obtain ⟨a, ha⟩ : ∃ a : ℕ, ∀ b : ℕ, a ≤ b → (c (b + 1) : ℝ) ≤ (1 + ε) * c b ∧ (c b : ℝ) * l - u (c b) ≤ ε * c b := eventually_atTop.1 (cgrowth.and L) let M := ((Finset.range (a + 1)).image fun i => c i).max' (by simp) filter_upwards [Ici_mem_atTop M] with n hn have exN : ∃ N, n < c N := by rcases (tendsto_atTop.1 ctop (n + 1)).exists with ⟨N, hN⟩ exact ⟨N, by omega⟩ let N := Nat.find exN have ncN : n < c N := Nat.find_spec exN have aN : a + 1 ≤ N := by by_contra! h have cNM : c N ≤ M := by apply le_max' apply mem_image_of_mem exact mem_range.2 h exact lt_irrefl _ ((cNM.trans hn).trans_lt ncN) have Npos : 0 < N := lt_of_lt_of_le Nat.succ_pos' aN have aN' : a ≤ N - 1 := by apply @Nat.le_of_add_le_add_right a 1 (N - 1) rw [Nat.sub_add_cancel Npos] exact aN have cNn : c (N - 1) ≤ n := by have : N - 1 < N := Nat.pred_lt Npos.ne' simpa only [not_lt] using Nat.find_min exN this calc (n : ℝ) * l - u n ≤ c N * l - u (c (N - 1)) := by gcongr exact hmono cNn _ ≤ (1 + ε) * c (N - 1) * l - u (c (N - 1)) := by gcongr have B : N - 1 + 1 = N := Nat.succ_pred_eq_of_pos Npos simpa [B] using (ha _ aN').1 _ = c (N - 1) * l - u (c (N - 1)) + ε * c (N - 1) * l := by ring _ ≤ ε * c (N - 1) + ε * c (N - 1) * l := add_le_add (ha _ aN').2 le_rfl _ = ε * (1 + l) * c (N - 1) := by ring _ ≤ ε * (1 + l) * n := by gcongr refine tendsto_order.2 ⟨fun d hd => ?_, fun d hd => ?_⟩ · obtain ⟨ε, hε, εpos⟩ : ∃ ε : ℝ, d + ε * (1 + l) < l ∧ 0 < ε := by have L : Tendsto (fun ε => d + ε * (1 + l)) (𝓝[>] 0) (𝓝 (d + 0 * (1 + l))) := by apply Tendsto.mono_left _ nhdsWithin_le_nhds exact tendsto_const_nhds.add (tendsto_id.mul tendsto_const_nhds) simp only [zero_mul, add_zero] at L exact (((tendsto_order.1 L).2 l hd).and self_mem_nhdsWithin).exists filter_upwards [B ε εpos, Ioi_mem_atTop 0] with n hn npos simp_rw [div_eq_inv_mul] calc d < (n : ℝ)⁻¹ * n * (l - ε * (1 + l)) := by rw [inv_mul_cancel₀, one_mul] · linarith only [hε] · exact Nat.cast_ne_zero.2 (ne_of_gt npos) _ = (n : ℝ)⁻¹ * (n * l - ε * (1 + l) * n) := by ring _ ≤ (n : ℝ)⁻¹ * u n := by gcongr; linarith only [hn] · obtain ⟨ε, hε, εpos⟩ : ∃ ε : ℝ, l + ε * (1 + ε + l) < d ∧ 0 < ε := by have L : Tendsto (fun ε => l + ε * (1 + ε + l)) (𝓝[>] 0) (𝓝 (l + 0 * (1 + 0 + l))) := by apply Tendsto.mono_left _ nhdsWithin_le_nhds exact tendsto_const_nhds.add (tendsto_id.mul ((tendsto_const_nhds.add tendsto_id).add tendsto_const_nhds)) simp only [zero_mul, add_zero] at L exact (((tendsto_order.1 L).2 d hd).and self_mem_nhdsWithin).exists filter_upwards [A ε εpos, Ioi_mem_atTop 0] with n hn (npos : 0 < n) calc u n / n ≤ (n * l + ε * (1 + ε + l) * n) / n := by gcongr; linarith only [hn] _ = (l + ε * (1 + ε + l)) := by field_simp; ring _ < d := hε /-- If a monotone sequence `u` is such that `u ⌊c^n⌋₊ / ⌊c^n⌋₊` converges to a limit `l` for all `c > 1`, then `u n / n` tends to `l`. It is even enough to have the assumption for a sequence of `c`s converging to `1`. -/ theorem tendsto_div_of_monotone_of_tendsto_div_floor_pow (u : ℕ → ℝ) (l : ℝ) (hmono : Monotone u) (c : ℕ → ℝ) (cone : ∀ k, 1 < c k) (clim : Tendsto c atTop (𝓝 1)) (hc : ∀ k, Tendsto (fun n : ℕ => u ⌊c k ^ n⌋₊ / ⌊c k ^ n⌋₊) atTop (𝓝 l)) : Tendsto (fun n => u n / n) atTop (𝓝 l) := by apply tendsto_div_of_monotone_of_exists_subseq_tendsto_div u l hmono intro a ha obtain ⟨k, hk⟩ : ∃ k, c k < a := ((tendsto_order.1 clim).2 a ha).exists refine ⟨fun n => ⌊c k ^ n⌋₊, ?_, (tendsto_nat_floor_atTop (α := ℝ)).comp (tendsto_pow_atTop_atTop_of_one_lt (cone k)), hc k⟩ have H : ∀ n : ℕ, (0 : ℝ) < ⌊c k ^ n⌋₊ := by intro n refine zero_lt_one.trans_le ?_ simp only [Real.rpow_natCast, Nat.one_le_cast, Nat.one_le_floor_iff, one_le_pow₀ (cone k).le] have A : Tendsto (fun n : ℕ => (⌊c k ^ (n + 1)⌋₊ : ℝ) / c k ^ (n + 1) * c k / (⌊c k ^ n⌋₊ / c k ^ n)) atTop (𝓝 (1 * c k / 1)) := by refine Tendsto.div (Tendsto.mul ?_ tendsto_const_nhds) ?_ one_ne_zero · refine tendsto_nat_floor_div_atTop.comp ?_ exact (tendsto_pow_atTop_atTop_of_one_lt (cone k)).comp (tendsto_add_atTop_nat 1) · refine tendsto_nat_floor_div_atTop.comp ?_ exact tendsto_pow_atTop_atTop_of_one_lt (cone k) have B : Tendsto (fun n : ℕ => (⌊c k ^ (n + 1)⌋₊ : ℝ) / ⌊c k ^ n⌋₊) atTop (𝓝 (c k)) := by simp only [one_mul, div_one] at A convert A using 1 ext1 n field_simp [(zero_lt_one.trans (cone k)).ne', (H n).ne'] ring filter_upwards [(tendsto_order.1 B).2 a hk] with n hn exact (div_le_iff₀ (H n)).1 hn.le /-- The sum of `1/(c^i)^2` above a threshold `j` is comparable to `1/j^2`, up to a multiplicative constant. -/ theorem sum_div_pow_sq_le_div_sq (N : ℕ) {j : ℝ} (hj : 0 < j) {c : ℝ} (hc : 1 < c) : (∑ i ∈ range N with j < c ^ i, (1 : ℝ) / (c ^ i) ^ 2) ≤ c ^ 3 * (c - 1)⁻¹ / j ^ 2 := by have cpos : 0 < c := zero_lt_one.trans hc have A : (0 : ℝ) < c⁻¹ ^ 2 := sq_pos_of_pos (inv_pos.2 cpos) have B : c ^ 2 * ((1 : ℝ) - c⁻¹ ^ 2)⁻¹ ≤ c ^ 3 * (c - 1)⁻¹ := by rw [← div_eq_mul_inv, ← div_eq_mul_inv, div_le_div_iff₀ _ (sub_pos.2 hc)] swap · exact sub_pos.2 (pow_lt_one₀ (inv_nonneg.2 cpos.le) (inv_lt_one_of_one_lt₀ hc) two_ne_zero) have : c ^ 3 = c ^ 2 * c := by ring simp only [mul_sub, this, mul_one, inv_pow, sub_le_sub_iff_left] rw [mul_assoc, mul_comm c, ← mul_assoc, mul_inv_cancel₀ (sq_pos_of_pos cpos).ne', one_mul] simpa using pow_right_mono₀ hc.le one_le_two have C : c⁻¹ ^ 2 < 1 := pow_lt_one₀ (inv_nonneg.2 cpos.le) (inv_lt_one_of_one_lt₀ hc) two_ne_zero calc (∑ i ∈ range N with j < c ^ i, (1 : ℝ) / (c ^ i) ^ 2) ≤ ∑ i ∈ Ico ⌊Real.log j / Real.log c⌋₊ N, (1 : ℝ) / (c ^ i) ^ 2 := by refine sum_le_sum_of_subset_of_nonneg (fun i hi ↦ ?_) (by intros; positivity) simp only [mem_filter, mem_range] at hi simp only [hi.1, mem_Ico, and_true] apply Nat.floor_le_of_le apply le_of_lt rw [div_lt_iff₀ (Real.log_pos hc), ← Real.log_pow] exact Real.log_lt_log hj hi.2 _ = ∑ i ∈ Ico ⌊Real.log j / Real.log c⌋₊ N, (c⁻¹ ^ 2) ^ i := by congr 1 with i simp [← pow_mul, mul_comm] _ ≤ (c⁻¹ ^ 2) ^ ⌊Real.log j / Real.log c⌋₊ / ((1 : ℝ) - c⁻¹ ^ 2) := geom_sum_Ico_le_of_lt_one (sq_nonneg _) C _ ≤ (c⁻¹ ^ 2) ^ (Real.log j / Real.log c - 1) / ((1 : ℝ) - c⁻¹ ^ 2) := by gcongr · exact sub_nonneg.2 C.le · rw [← Real.rpow_natCast] exact Real.rpow_le_rpow_of_exponent_ge A C.le (Nat.sub_one_lt_floor _).le _ = c ^ 2 * ((1 : ℝ) - c⁻¹ ^ 2)⁻¹ / j ^ 2 := by have I : (c⁻¹ ^ 2) ^ (Real.log j / Real.log c) = (1 : ℝ) / j ^ 2 := by apply Real.log_injOn_pos (Real.rpow_pos_of_pos A _) · rw [Set.mem_Ioi]; positivity rw [Real.log_rpow A] simp only [one_div, Real.log_inv, Real.log_pow, Nat.cast_one, mul_neg, neg_inj] field_simp [(Real.log_pos hc).ne'] ring rw [Real.rpow_sub A, I] have : c ^ 2 - 1 ≠ 0 := (sub_pos.2 (one_lt_pow₀ hc two_ne_zero)).ne' field_simp [hj.ne', (zero_lt_one.trans hc).ne'] ring _ ≤ c ^ 3 * (c - 1)⁻¹ / j ^ 2 := by gcongr theorem mul_pow_le_nat_floor_pow {c : ℝ} (hc : 1 < c) (i : ℕ) : (1 - c⁻¹) * c ^ i ≤ ⌊c ^ i⌋₊ := by have cpos : 0 < c := zero_lt_one.trans hc rcases eq_or_ne i 0 with (rfl | hi) · simp only [pow_zero, Nat.floor_one, Nat.cast_one, mul_one, sub_le_self_iff, inv_nonneg, cpos.le] calc (1 - c⁻¹) * c ^ i = c ^ i - c ^ i * c⁻¹ := by ring
_ ≤ c ^ i - 1 := by gcongr simpa only [← div_eq_mul_inv, one_le_div cpos, pow_one] using le_self_pow₀ hc.le hi _ ≤ ⌊c ^ i⌋₊ := (Nat.sub_one_lt_floor _).le /-- The sum of `1/⌊c^i⌋₊^2` above a threshold `j` is comparable to `1/j^2`, up to a multiplicative constant. -/ theorem sum_div_nat_floor_pow_sq_le_div_sq (N : ℕ) {j : ℝ} (hj : 0 < j) {c : ℝ} (hc : 1 < c) : (∑ i ∈ range N with j < ⌊c ^ i⌋₊, (1 : ℝ) / (⌊c ^ i⌋₊ : ℝ) ^ 2) ≤ c ^ 5 * (c - 1)⁻¹ ^ 3 / j ^ 2 := by
Mathlib/Analysis/SpecificLimits/FloorPow.lean
271
280
/- Copyright (c) 2024 Etienne Marion. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Etienne Marion -/ import Mathlib.Data.Finite.Prod import Mathlib.MeasureTheory.SetSemiring /-! # Algebra of sets In this file we define the notion of algebra of sets and give its basic properties. An algebra of sets is a family of sets containing the empty set and closed by complement and binary union. It is therefore similar to a `σ`-algebra, except that it is not necessarily closed by countable unions. We also define the algebra of sets generated by a family of sets and give its basic properties, and we prove that it is countable when it is generated by a countable family. We prove that the `σ`-algebra generated by a family of sets `𝒜` is the same as the one generated by the algebra of sets generated by `𝒜`. ## Main definitions * `MeasureTheory.IsSetAlgebra`: property of being an algebra of sets. * `MeasureTheory.generateSetAlgebra`: the algebra of sets generated by a family of sets. ## Main statements * `MeasureTheory.mem_generateSetAlgebra_elim`: If a set `s` belongs to the algebra of sets generated by `𝒜`, then it can be written as a finite union of finite intersections of sets which are in `𝒜` or have their complement in `𝒜`. * `MeasureTheory.countable_generateSetAlgebra`: If a family of sets is countable then so is the algebra of sets generated by it. ## References * <https://en.wikipedia.org/wiki/Field_of_sets> ## Tags algebra of sets, generated algebra of sets -/ open MeasurableSpace Set namespace MeasureTheory variable {α : Type*} {𝒜 : Set (Set α)} {s t : Set α} /-! ### Definition and basic properties of an algebra of sets -/ /-- An algebra of sets is a family of sets containing the empty set and closed by complement and union. Consequently it is also closed by difference (see `IsSetAlgebra.diff_mem`) and intersection (see `IsSetAlgebra.inter_mem`). -/ structure IsSetAlgebra (𝒜 : Set (Set α)) : Prop where empty_mem : ∅ ∈ 𝒜 compl_mem : ∀ ⦃s⦄, s ∈ 𝒜 → sᶜ ∈ 𝒜 union_mem : ∀ ⦃s t⦄, s ∈ 𝒜 → t ∈ 𝒜 → s ∪ t ∈ 𝒜 namespace IsSetAlgebra /-- An algebra of sets contains the whole set. -/ theorem univ_mem (h𝒜 : IsSetAlgebra 𝒜) : univ ∈ 𝒜 := compl_empty ▸ h𝒜.compl_mem h𝒜.empty_mem /-- An algebra of sets is closed by intersection. -/ theorem inter_mem (h𝒜 : IsSetAlgebra 𝒜) (s_mem : s ∈ 𝒜) (t_mem : t ∈ 𝒜) : s ∩ t ∈ 𝒜 := inter_eq_compl_compl_union_compl .. ▸ h𝒜.compl_mem (h𝒜.union_mem (h𝒜.compl_mem s_mem) (h𝒜.compl_mem t_mem)) /-- An algebra of sets is closed by difference. -/ theorem diff_mem (h𝒜 : IsSetAlgebra 𝒜) (s_mem : s ∈ 𝒜) (t_mem : t ∈ 𝒜) : s \ t ∈ 𝒜 := h𝒜.inter_mem s_mem (h𝒜.compl_mem t_mem) /-- An algebra of sets is a ring of sets. -/ theorem isSetRing (h𝒜 : IsSetAlgebra 𝒜) : IsSetRing 𝒜 where empty_mem := h𝒜.empty_mem union_mem := h𝒜.union_mem diff_mem := fun _ _ ↦ h𝒜.diff_mem /-- An algebra of sets is closed by finite unions. -/ theorem biUnion_mem {ι : Type*} (h𝒜 : IsSetAlgebra 𝒜) {s : ι → Set α} (S : Finset ι) (hs : ∀ i ∈ S, s i ∈ 𝒜) : ⋃ i ∈ S, s i ∈ 𝒜 := h𝒜.isSetRing.biUnion_mem S hs /-- An algebra of sets is closed by finite intersections. -/ theorem biInter_mem {ι : Type*} (h𝒜 : IsSetAlgebra 𝒜) {s : ι → Set α} (S : Finset ι) (hs : ∀ i ∈ S, s i ∈ 𝒜) : ⋂ i ∈ S, s i ∈ 𝒜 := by by_cases h : S = ∅ · rw [h, ← Finset.set_biInter_coe, Finset.coe_empty, biInter_empty] exact h𝒜.univ_mem · rw [← ne_eq, ← Finset.nonempty_iff_ne_empty] at h exact h𝒜.isSetRing.biInter_mem S h hs end IsSetAlgebra section generateSetAlgebra /-! ### Definition and properties of the algebra of sets generated by some family -/ /-- `generateSetAlgebra 𝒜` is the smallest algebra of sets containing `𝒜`. -/ inductive generateSetAlgebra {α : Type*} (𝒜 : Set (Set α)) : Set (Set α) | base (s : Set α) (s_mem : s ∈ 𝒜) : generateSetAlgebra 𝒜 s | empty : generateSetAlgebra 𝒜 ∅ | compl (s : Set α) (hs : generateSetAlgebra 𝒜 s) : generateSetAlgebra 𝒜 sᶜ | union (s t : Set α) (hs : generateSetAlgebra 𝒜 s) (ht : generateSetAlgebra 𝒜 t) : generateSetAlgebra 𝒜 (s ∪ t) /-- The algebra of sets generated by a family of sets is an algebra of sets. -/ theorem isSetAlgebra_generateSetAlgebra : IsSetAlgebra (generateSetAlgebra 𝒜) where empty_mem := generateSetAlgebra.empty compl_mem := fun _ hs ↦ generateSetAlgebra.compl _ hs union_mem := fun _ _ hs ht ↦ generateSetAlgebra.union _ _ hs ht /-- The algebra of sets generated by `𝒜` contains `𝒜`. -/ theorem self_subset_generateSetAlgebra : 𝒜 ⊆ generateSetAlgebra 𝒜 := fun _ ↦ generateSetAlgebra.base _ /-- The measurable space generated by a family of sets `𝒜` is the same as the one generated by the algebra of sets generated by `𝒜`. -/ @[simp] theorem generateFrom_generateSetAlgebra_eq : generateFrom (generateSetAlgebra 𝒜) = generateFrom 𝒜 := by refine le_antisymm (fun s ms ↦ ?_) (generateFrom_mono self_subset_generateSetAlgebra) induction s, ms using generateFrom_induction with | hC t ht h => clear h induction ht with | base u u_mem => exact measurableSet_generateFrom u_mem | empty => exact @MeasurableSet.empty _ (generateFrom 𝒜) | compl u _ mu => exact mu.compl | union u v _ _ mu mv => exact MeasurableSet.union mu mv | empty => exact MeasurableSpace.measurableSet_empty _ | compl t _ ht => exact ht.compl | iUnion t _ ht => exact .iUnion ht /-- If a family of sets `𝒜` is contained in `ℬ`, then the algebra of sets generated by `𝒜` is contained in the one generated by `ℬ`. -/ theorem generateSetAlgebra_mono {ℬ : Set (Set α)} (h : 𝒜 ⊆ ℬ) : generateSetAlgebra 𝒜 ⊆ generateSetAlgebra ℬ := by intro s hs induction hs with | base t t_mem => exact self_subset_generateSetAlgebra (h t_mem) | empty => exact isSetAlgebra_generateSetAlgebra.empty_mem | compl t _ t_mem => exact isSetAlgebra_generateSetAlgebra.compl_mem t_mem | union t u _ _ t_mem u_mem => exact isSetAlgebra_generateSetAlgebra.union_mem t_mem u_mem namespace IsSetAlgebra
/-- If a family of sets `𝒜` is contained in an algebra of sets `ℬ`, then so is the algebra of sets generated by `𝒜`. -/ theorem generateSetAlgebra_subset {ℬ : Set (Set α)} (h : 𝒜 ⊆ ℬ) (hℬ : IsSetAlgebra ℬ) : generateSetAlgebra 𝒜 ⊆ ℬ := by intro s hs induction hs with | base t t_mem => exact h t_mem | empty => exact hℬ.empty_mem
Mathlib/MeasureTheory/SetAlgebra.lean
151
158
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.CategoryTheory.Closed.Cartesian import Mathlib.CategoryTheory.Limits.Preserves.Shapes.BinaryProducts import Mathlib.CategoryTheory.Adjunction.FullyFaithful /-! # Cartesian closed functors Define the exponential comparison morphisms for a functor which preserves binary products, and use them to define a cartesian closed functor: one which (naturally) preserves exponentials. Define the Frobenius morphism, and show it is an isomorphism iff the exponential comparison is an isomorphism. ## TODO Some of the results here are true more generally for closed objects and for closed monoidal categories, and these could be generalised. ## References https://ncatlab.org/nlab/show/cartesian+closed+functor https://ncatlab.org/nlab/show/Frobenius+reciprocity ## Tags Frobenius reciprocity, cartesian closed functor -/ noncomputable section namespace CategoryTheory open Category CartesianClosed MonoidalCategory ChosenFiniteProducts TwoSquare universe v u u' variable {C : Type u} [Category.{v} C] variable {D : Type u'} [Category.{v} D] variable [ChosenFiniteProducts C] [ChosenFiniteProducts D] variable (F : C ⥤ D) {L : D ⥤ C} /-- The Frobenius morphism for an adjunction `L ⊣ F` at `A` is given by the morphism L(FA ⨯ B) ⟶ LFA ⨯ LB ⟶ A ⨯ LB natural in `B`, where the first morphism is the product comparison and the latter uses the counit of the adjunction. We will show that if `C` and `D` are cartesian closed, then this morphism is an isomorphism for all `A` iff `F` is a cartesian closed functor, i.e. it preserves exponentials. -/ def frobeniusMorphism (h : L ⊣ F) (A : C) : TwoSquare (tensorLeft (F.obj A)) L L (tensorLeft A) := prodComparisonNatTrans L (F.obj A) ≫ whiskerLeft _ ((curriedTensor C).map (h.counit.app _)) /-- If `F` is full and faithful and has a left adjoint `L` which preserves binary products, then the Frobenius morphism is an isomorphism. -/ instance frobeniusMorphism_iso_of_preserves_binary_products (h : L ⊣ F) (A : C) [Limits.PreservesLimitsOfShape (Discrete Limits.WalkingPair) L] [F.Full] [F.Faithful] : IsIso (frobeniusMorphism F h A).natTrans := suffices ∀ (X : D), IsIso ((frobeniusMorphism F h A).natTrans.app X) from NatIso.isIso_of_isIso_app _ fun B ↦ by dsimp [frobeniusMorphism]; infer_instance variable [CartesianClosed C] [CartesianClosed D] variable [Limits.PreservesLimitsOfShape (Discrete Limits.WalkingPair) F] /-- The exponential comparison map. `F` is a cartesian closed functor if this is an iso for all `A`. -/ def expComparison (A : C) : TwoSquare (exp A) F F (exp (F.obj A)) := mateEquiv (exp.adjunction A) (exp.adjunction (F.obj A)) (prodComparisonNatIso F A).inv theorem expComparison_ev (A B : C) : F.obj A ◁ ((expComparison F A).natTrans.app B) ≫ (exp.ev (F.obj A)).app (F.obj B) = inv (prodComparison F _ _) ≫ F.map ((exp.ev _).app _) := by convert mateEquiv_counit _ _ (prodComparisonNatIso F A).inv B using 2 apply IsIso.inv_eq_of_hom_inv_id -- Porting note: was `ext` simp only [prodComparisonNatTrans_app, prodComparisonNatIso_inv, asIso_inv, NatIso.isIso_inv_app, IsIso.hom_inv_id] theorem coev_expComparison (A B : C) : F.map ((exp.coev A).app B) ≫ (expComparison F A).natTrans.app (A ⊗ B) = (exp.coev _).app (F.obj B) ≫ (exp (F.obj A)).map (inv (prodComparison F A B)) := by convert unit_mateEquiv _ _ (prodComparisonNatIso F A).inv B using 3 apply IsIso.inv_eq_of_hom_inv_id -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): was `ext` dsimp simp theorem uncurry_expComparison (A B : C) : CartesianClosed.uncurry ((expComparison F A).natTrans.app B) = inv (prodComparison F _ _) ≫ F.map ((exp.ev _).app _) := by rw [uncurry_eq, expComparison_ev] /-- The exponential comparison map is natural in `A`. -/
theorem expComparison_whiskerLeft {A A' : C} (f : A' ⟶ A) : (expComparison F A).whiskerBottom (pre (F.map f)) = (expComparison F A').whiskerTop (pre f) := by unfold expComparison pre
Mathlib/CategoryTheory/Closed/Functor.lean
100
103
/- Copyright (c) 2024 Dagur Asgeirsson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dagur Asgeirsson -/ import Mathlib.CategoryTheory.Sites.Coherent.Comparison import Mathlib.CategoryTheory.Sites.Coherent.ExtensiveSheaves import Mathlib.CategoryTheory.Sites.Coherent.ReflectsPrecoherent import Mathlib.CategoryTheory.Sites.Coherent.ReflectsPreregular import Mathlib.CategoryTheory.Sites.DenseSubsite.InducedTopology import Mathlib.CategoryTheory.Sites.Whiskering /-! # Categories of coherent sheaves Given a fully faithful functor `F : C ⥤ D` into a precoherent category, which preserves and reflects finite effective epi families, and satisfies the property `F.EffectivelyEnough` (meaning that to every object in `C` there is an effective epi from an object in the image of `F`), the categories of coherent sheaves on `C` and `D` are equivalent (see `CategoryTheory.coherentTopology.equivalence`). The main application of this equivalence is the characterisation of condensed sets as coherent sheaves on either `CompHaus`, `Profinite` or `Stonean`. See the file `Condensed/Equivalence.lean` We give the corresponding result for the regular topology as well (see `CategoryTheory.regularTopology.equivalence`). -/ universe v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ namespace CategoryTheory open Limits Functor regularTopology variable {C D : Type*} [Category C] [Category D] (F : C ⥤ D) namespace coherentTopology variable [F.PreservesFiniteEffectiveEpiFamilies] [F.ReflectsFiniteEffectiveEpiFamilies] [F.Full] [F.Faithful] [F.EffectivelyEnough] [Precoherent D] instance : F.IsCoverDense (coherentTopology _) := by refine F.isCoverDense_of_generate_singleton_functor_π_mem _ fun B ↦ ⟨_, F.effectiveEpiOver B, ?_⟩ apply Coverage.Saturate.of refine ⟨Unit, inferInstance, fun _ => F.effectiveEpiOverObj B, fun _ => F.effectiveEpiOver B, ?_ , ?_⟩ · funext; ext -- Do we want `Presieve.ext`? refine ⟨fun ⟨⟩ ↦ ⟨()⟩, ?_⟩ rintro ⟨⟩ simp · rw [← effectiveEpi_iff_effectiveEpiFamily] infer_instance theorem exists_effectiveEpiFamily_iff_mem_induced (X : C) (S : Sieve X) : (∃ (α : Type) (_ : Finite α) (Y : α → C) (π : (a : α) → (Y a ⟶ X)), EffectiveEpiFamily Y π ∧ (∀ a : α, (S.arrows) (π a)) ) ↔ (S ∈ F.inducedTopology (coherentTopology _) X) := by refine ⟨fun ⟨α, _, Y, π, ⟨H₁, H₂⟩⟩ ↦ ?_, fun hS ↦ ?_⟩ · apply (mem_sieves_iff_hasEffectiveEpiFamily (Sieve.functorPushforward _ S)).mpr refine ⟨α, inferInstance, fun i => F.obj (Y i), fun i => F.map (π i), ⟨?_, fun a => Sieve.image_mem_functorPushforward F S (H₂ a)⟩⟩ exact F.map_finite_effectiveEpiFamily _ _ · obtain ⟨α, _, Y, π, ⟨H₁, H₂⟩⟩ := (mem_sieves_iff_hasEffectiveEpiFamily _).mp hS refine ⟨α, inferInstance, ?_⟩ let Z : α → C := fun a ↦ (Functor.EffectivelyEnough.presentation (F := F) (Y a)).some.p let g₀ : (a : α) → F.obj (Z a) ⟶ Y a := fun a ↦ F.effectiveEpiOver (Y a) have : EffectiveEpiFamily _ (fun a ↦ g₀ a ≫ π a) := inferInstance refine ⟨Z , fun a ↦ F.preimage (g₀ a ≫ π a), ?_, fun a ↦ (?_ : S.arrows (F.preimage _))⟩ · refine F.finite_effectiveEpiFamily_of_map _ _ ?_ simpa using this · obtain ⟨W, g₁, g₂, h₁, h₂⟩ := H₂ a rw [h₂] convert S.downward_closed h₁ (F.preimage (g₀ a ≫ g₂)) exact F.map_injective (by simp) lemma eq_induced : haveI := F.reflects_precoherent coherentTopology C = F.inducedTopology (coherentTopology _) := by ext X S have := F.reflects_precoherent rw [← exists_effectiveEpiFamily_iff_mem_induced F X] rw [← coherentTopology.mem_sieves_iff_hasEffectiveEpiFamily S] instance : haveI := F.reflects_precoherent; F.IsDenseSubsite (coherentTopology C) (coherentTopology D) where functorPushforward_mem_iff := by rw [eq_induced F] rfl lemma coverPreserving : haveI := F.reflects_precoherent CoverPreserving (coherentTopology _) (coherentTopology _) F := IsDenseSubsite.coverPreserving _ _ _ section SheafEquiv variable {C : Type u₁} {D : Type u₂} [Category.{v₁} C] [Category.{v₂} D] (F : C ⥤ D) [F.PreservesFiniteEffectiveEpiFamilies] [F.ReflectsFiniteEffectiveEpiFamilies] [F.Full] [F.Faithful] [Precoherent D] [F.EffectivelyEnough] /-- The equivalence from coherent sheaves on `C` to coherent sheaves on `D`, given a fully faithful functor `F : C ⥤ D` to a precoherent category, which preserves and reflects effective epimorphic families, and satisfies `F.EffectivelyEnough`. -/ noncomputable def equivalence (A : Type u₃) [Category.{v₃} A] [∀ X, HasLimitsOfShape (StructuredArrow X F.op) A] : haveI := F.reflects_precoherent Sheaf (coherentTopology C) A ≌ Sheaf (coherentTopology D) A := Functor.IsDenseSubsite.sheafEquiv F _ _ _ end SheafEquiv section RegularExtensive variable {C : Type u₁} {D : Type u₂} [Category.{v₁} C] [Category.{v₂} D] (F : C ⥤ D) [F.PreservesEffectiveEpis] [F.ReflectsEffectiveEpis] [F.Full] [F.Faithful] [FinitaryExtensive D] [Preregular D] [FinitaryPreExtensive C] [PreservesFiniteCoproducts F] [F.EffectivelyEnough] /-- The equivalence from coherent sheaves on `C` to coherent sheaves on `D`, given a fully faithful functor `F : C ⥤ D` to an extensive preregular category, which preserves and reflects effective epimorphisms and satisfies `F.EffectivelyEnough`. -/ noncomputable def equivalence' (A : Type u₃) [Category.{v₃} A] [∀ X, HasLimitsOfShape (StructuredArrow X F.op) A] : haveI := F.reflects_precoherent Sheaf (coherentTopology C) A ≌ Sheaf (coherentTopology D) A := Functor.IsDenseSubsite.sheafEquiv F _ _ _ end RegularExtensive end coherentTopology namespace regularTopology variable [F.PreservesEffectiveEpis] [F.ReflectsEffectiveEpis] [F.Full] [F.Faithful] [F.EffectivelyEnough] [Preregular D] instance : F.IsCoverDense (regularTopology _) := by refine F.isCoverDense_of_generate_singleton_functor_π_mem _ fun B ↦ ⟨_, F.effectiveEpiOver B, ?_⟩ apply Coverage.Saturate.of refine ⟨F.effectiveEpiOverObj B, F.effectiveEpiOver B, ?_, inferInstance⟩ funext; ext -- Do we want `Presieve.ext`? refine ⟨fun ⟨⟩ ↦ ⟨()⟩, ?_⟩ rintro ⟨⟩ simp theorem exists_effectiveEpi_iff_mem_induced (X : C) (S : Sieve X) : (∃ (Y : C) (π : Y ⟶ X), EffectiveEpi π ∧ S.arrows π) ↔ (S ∈ F.inducedTopology (regularTopology _) X) := by refine ⟨fun ⟨Y, π, ⟨H₁, H₂⟩⟩ ↦ ?_, fun hS ↦ ?_⟩ · apply (mem_sieves_iff_hasEffectiveEpi (Sieve.functorPushforward _ S)).mpr refine ⟨F.obj Y, F.map π, ⟨?_, Sieve.image_mem_functorPushforward F S H₂⟩⟩ exact F.map_effectiveEpi _ · obtain ⟨Y, π, ⟨H₁, H₂⟩⟩ := (mem_sieves_iff_hasEffectiveEpi _).mp hS let g₀ := F.effectiveEpiOver Y refine ⟨_, F.preimage (g₀ ≫ π), ?_, (?_ : S.arrows (F.preimage _))⟩ · refine F.effectiveEpi_of_map _ ?_ simp only [map_preimage] infer_instance · obtain ⟨W, g₁, g₂, h₁, h₂⟩ := H₂ rw [h₂] convert S.downward_closed h₁ (F.preimage (g₀ ≫ g₂)) exact F.map_injective (by simp) lemma eq_induced : haveI := F.reflects_preregular regularTopology C = F.inducedTopology (regularTopology _) := by ext X S have := F.reflects_preregular rw [← exists_effectiveEpi_iff_mem_induced F X] rw [← mem_sieves_iff_hasEffectiveEpi S] instance : haveI := F.reflects_preregular; F.IsDenseSubsite (regularTopology C) (regularTopology D) where functorPushforward_mem_iff := by rw [eq_induced F]
rfl lemma coverPreserving : haveI := F.reflects_preregular CoverPreserving (regularTopology _) (regularTopology _) F :=
Mathlib/CategoryTheory/Sites/Coherent/SheafComparison.lean
188
191
/- Copyright (c) 2023 Alex Keizer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex Keizer -/ import Mathlib.Data.Vector.Basic import Mathlib.Data.Vector.Snoc /-! This file establishes a set of normalization lemmas for `map`/`mapAccumr` operations on vectors -/ variable {α β γ ζ σ σ₁ σ₂ φ : Type*} {n : ℕ} {s : σ} {s₁ : σ₁} {s₂ : σ₂} namespace List namespace Vector /-! ## Fold nested `mapAccumr`s into one -/ section Fold section Unary variable (xs : Vector α n) (f₁ : β → σ₁ → σ₁ × γ) (f₂ : α → σ₂ → σ₂ × β) @[simp] theorem mapAccumr_mapAccumr : mapAccumr f₁ (mapAccumr f₂ xs s₂).snd s₁ = let m := (mapAccumr (fun x s => let r₂ := f₂ x s.snd let r₁ := f₁ r₂.snd s.fst ((r₁.fst, r₂.fst), r₁.snd) ) xs (s₁, s₂)) (m.fst.fst, m.snd) := by induction xs using Vector.revInductionOn generalizing s₁ s₂ <;> simp_all @[simp] theorem mapAccumr_map {s : σ₁} (f₂ : α → β) : (mapAccumr f₁ (map f₂ xs) s) = (mapAccumr (fun x s => f₁ (f₂ x) s) xs s) := by induction xs using Vector.revInductionOn generalizing s <;> simp_all @[simp] theorem map_mapAccumr {s : σ₂} (f₁ : β → γ) : (map f₁ (mapAccumr f₂ xs s).snd) = (mapAccumr (fun x s => let r := (f₂ x s); (r.fst, f₁ r.snd) ) xs s).snd := by induction xs using Vector.revInductionOn generalizing s <;> simp_all @[simp] theorem map_map (f₁ : β → γ) (f₂ : α → β) : map f₁ (map f₂ xs) = map (fun x => f₁ <| f₂ x) xs := by induction xs <;> simp_all theorem map_pmap {p : α → Prop} (f₁ : β → γ) (f₂ : (a : α) → p a → β) (H : ∀ x ∈ xs.toList, p x): map f₁ (pmap f₂ xs H) = pmap (fun x hx => f₁ <| f₂ x hx) xs H := by induction xs <;> simp_all theorem pmap_map {p : β → Prop} (f₁ : (b : β) → p b → γ) (f₂ : α → β) (H : ∀ x ∈ (xs.map f₂).toList, p x): pmap f₁ (map f₂ xs) H = pmap (fun x hx => f₁ (f₂ x) hx) xs (by simpa using H) := by induction xs <;> simp_all end Unary section Binary variable (xs : Vector α n) (ys : Vector β n) @[simp] theorem mapAccumr₂_mapAccumr_left (f₁ : γ → β → σ₁ → σ₁ × ζ) (f₂ : α → σ₂ → σ₂ × γ) : (mapAccumr₂ f₁ (mapAccumr f₂ xs s₂).snd ys s₁) = let m := (mapAccumr₂ (fun x y s => let r₂ := f₂ x s.snd let r₁ := f₁ r₂.snd y s.fst ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂)) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all @[simp] theorem map₂_map_left (f₁ : γ → β → ζ) (f₂ : α → γ) : map₂ f₁ (map f₂ xs) ys = map₂ (fun x y => f₁ (f₂ x) y) xs ys := by induction xs, ys using Vector.revInductionOn₂ <;> simp_all @[simp] theorem mapAccumr₂_mapAccumr_right (f₁ : α → γ → σ₁ → σ₁ × ζ) (f₂ : β → σ₂ → σ₂ × γ) : (mapAccumr₂ f₁ xs (mapAccumr f₂ ys s₂).snd s₁) = let m := (mapAccumr₂ (fun x y s => let r₂ := f₂ y s.snd let r₁ := f₁ x r₂.snd s.fst ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂)) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all @[simp] theorem map₂_map_right (f₁ : α → γ → ζ) (f₂ : β → γ) : map₂ f₁ xs (map f₂ ys) = map₂ (fun x y => f₁ x (f₂ y)) xs ys := by induction xs, ys using Vector.revInductionOn₂ <;> simp_all @[simp] theorem mapAccumr_mapAccumr₂ (f₁ : γ → σ₁ → σ₁ × ζ) (f₂ : α → β → σ₂ → σ₂ × γ) : (mapAccumr f₁ (mapAccumr₂ f₂ xs ys s₂).snd s₁) = let m := mapAccumr₂ (fun x y s => let r₂ := f₂ x y s.snd let r₁ := f₁ r₂.snd s.fst ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all @[simp] theorem map_map₂ (f₁ : γ → ζ) (f₂ : α → β → γ) : map f₁ (map₂ f₂ xs ys) = map₂ (fun x y => f₁ <| f₂ x y) xs ys := by induction xs, ys using Vector.revInductionOn₂ <;> simp_all @[simp] theorem mapAccumr₂_mapAccumr₂_left_left (f₁ : γ → α → σ₁ → σ₁ × φ) (f₂ : α → β → σ₂ → σ₂ × γ) : (mapAccumr₂ f₁ (mapAccumr₂ f₂ xs ys s₂).snd xs s₁) = let m := mapAccumr₂ (fun x y (s₁, s₂) => let r₂ := f₂ x y s₂ let r₁ := f₁ r₂.snd x s₁ ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all @[simp] theorem mapAccumr₂_mapAccumr₂_left_right (f₁ : γ → β → σ₁ → σ₁ × φ) (f₂ : α → β → σ₂ → σ₂ × γ) : (mapAccumr₂ f₁ (mapAccumr₂ f₂ xs ys s₂).snd ys s₁) = let m := mapAccumr₂ (fun x y (s₁, s₂) => let r₂ := f₂ x y s₂ let r₁ := f₁ r₂.snd y s₁ ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all @[simp] theorem mapAccumr₂_mapAccumr₂_right_left (f₁ : α → γ → σ₁ → σ₁ × φ) (f₂ : α → β → σ₂ → σ₂ × γ) : (mapAccumr₂ f₁ xs (mapAccumr₂ f₂ xs ys s₂).snd s₁) = let m := mapAccumr₂ (fun x y (s₁, s₂) => let r₂ := f₂ x y s₂ let r₁ := f₁ x r₂.snd s₁ ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all @[simp] theorem mapAccumr₂_mapAccumr₂_right_right (f₁ : β → γ → σ₁ → σ₁ × φ) (f₂ : α → β → σ₂ → σ₂ × γ) : (mapAccumr₂ f₁ ys (mapAccumr₂ f₂ xs ys s₂).snd s₁) = let m := mapAccumr₂ (fun x y (s₁, s₂) => let r₂ := f₂ x y s₂ let r₁ := f₁ y r₂.snd s₁ ((r₁.fst, r₂.fst), r₁.snd) ) xs ys (s₁, s₂) (m.fst.fst, m.snd) := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ <;> simp_all end Binary end Fold /-! ## Bisimulations We can prove two applications of `mapAccumr` equal by providing a bisimulation relation that relates the initial states. That is, by providing a relation `R : σ₁ → σ₁ → Prop` such that `R s₁ s₂` implies that `R` also relates any pair of states reachable by applying `f₁` to `s₁` and `f₂` to `s₂`, with any possible input values. -/ section Bisim variable {xs : Vector α n} theorem mapAccumr_bisim {f₁ : α → σ₁ → σ₁ × β} {f₂ : α → σ₂ → σ₂ × β} {s₁ : σ₁} {s₂ : σ₂} (R : σ₁ → σ₂ → Prop) (h₀ : R s₁ s₂) (hR : ∀ {s q} a, R s q → R (f₁ a s).1 (f₂ a q).1 ∧ (f₁ a s).2 = (f₂ a q).2) : R (mapAccumr f₁ xs s₁).fst (mapAccumr f₂ xs s₂).fst ∧ (mapAccumr f₁ xs s₁).snd = (mapAccumr f₂ xs s₂).snd := by induction xs using Vector.revInductionOn generalizing s₁ s₂ next => exact ⟨h₀, rfl⟩ next xs x ih => rcases (hR x h₀) with ⟨hR, _⟩ simp only [mapAccumr_snoc, ih hR, true_and] congr 1 theorem mapAccumr_bisim_tail {f₁ : α → σ₁ → σ₁ × β} {f₂ : α → σ₂ → σ₂ × β} {s₁ : σ₁} {s₂ : σ₂} (h : ∃ R : σ₁ → σ₂ → Prop, R s₁ s₂ ∧ ∀ {s q} a, R s q → R (f₁ a s).1 (f₂ a q).1 ∧ (f₁ a s).2 = (f₂ a q).2) : (mapAccumr f₁ xs s₁).snd = (mapAccumr f₂ xs s₂).snd := by rcases h with ⟨R, h₀, hR⟩ exact (mapAccumr_bisim R h₀ hR).2 theorem mapAccumr₂_bisim {ys : Vector β n} {f₁ : α → β → σ₁ → σ₁ × γ} {f₂ : α → β → σ₂ → σ₂ × γ} {s₁ : σ₁} {s₂ : σ₂} (R : σ₁ → σ₂ → Prop) (h₀ : R s₁ s₂) (hR : ∀ {s q} a b, R s q → R (f₁ a b s).1 (f₂ a b q).1 ∧ (f₁ a b s).2 = (f₂ a b q).2) : R (mapAccumr₂ f₁ xs ys s₁).1 (mapAccumr₂ f₂ xs ys s₂).1 ∧ (mapAccumr₂ f₁ xs ys s₁).2 = (mapAccumr₂ f₂ xs ys s₂).2 := by induction xs, ys using Vector.revInductionOn₂ generalizing s₁ s₂ next => exact ⟨h₀, rfl⟩ next xs ys x y ih => rcases (hR x y h₀) with ⟨hR, _⟩ simp only [mapAccumr₂_snoc, ih hR, true_and] congr 1 theorem mapAccumr₂_bisim_tail {ys : Vector β n} {f₁ : α → β → σ₁ → σ₁ × γ} {f₂ : α → β → σ₂ → σ₂ × γ} {s₁ : σ₁} {s₂ : σ₂} (h : ∃ R : σ₁ → σ₂ → Prop, R s₁ s₂ ∧ ∀ {s q} a b, R s q → R (f₁ a b s).1 (f₂ a b q).1 ∧ (f₁ a b s).2 = (f₂ a b q).2) : (mapAccumr₂ f₁ xs ys s₁).2 = (mapAccumr₂ f₂ xs ys s₂).2 := by rcases h with ⟨R, h₀, hR⟩ exact (mapAccumr₂_bisim R h₀ hR).2 end Bisim
/-! ## Redundant state optimization The following section are collection of rewrites to simplify, or even get rid, redundant
Mathlib/Data/Vector/MapLemmas.lean
224
227
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Divisibility.Hom import Mathlib.Algebra.Group.Even import Mathlib.Algebra.Group.Nat.Hom import Mathlib.Algebra.Ring.Hom.Defs import Mathlib.Algebra.Ring.Nat /-! # Cast of natural numbers (additional theorems) This file proves additional properties about the *canonical* homomorphism from the natural numbers into an additive monoid with a one (`Nat.cast`). ## Main declarations * `castAddMonoidHom`: `cast` bundled as an `AddMonoidHom`. * `castRingHom`: `cast` bundled as a `RingHom`. -/ assert_not_exists OrderedCommGroup Commute.zero_right Commute.add_right abs_eq_max_neg NeZero.natCast_ne -- TODO: `MulOpposite.op_natCast` was not intended to be imported -- assert_not_exists MulOpposite.op_natCast open Additive Multiplicative variable {α β : Type*} namespace Nat /-- `Nat.cast : ℕ → α` as an `AddMonoidHom`. -/ def castAddMonoidHom (α : Type*) [AddMonoidWithOne α] : ℕ →+ α where toFun := Nat.cast map_add' := cast_add map_zero' := cast_zero @[simp] theorem coe_castAddMonoidHom [AddMonoidWithOne α] : (castAddMonoidHom α : ℕ → α) = Nat.cast := rfl lemma _root_.Even.natCast [AddMonoidWithOne α] {n : ℕ} (hn : Even n) : Even (n : α) := hn.map <| Nat.castAddMonoidHom α section NonAssocSemiring variable [NonAssocSemiring α] @[simp, norm_cast] lemma cast_mul (m n : ℕ) : ((m * n : ℕ) : α) = m * n := by induction n <;> simp [mul_succ, mul_add, *] variable (α) in /-- `Nat.cast : ℕ → α` as a `RingHom` -/ def castRingHom : ℕ →+* α := { castAddMonoidHom α with toFun := Nat.cast, map_one' := cast_one, map_mul' := cast_mul } @[simp, norm_cast] lemma coe_castRingHom : (castRingHom α : ℕ → α) = Nat.cast := rfl
lemma _root_.nsmul_eq_mul' (a : α) (n : ℕ) : n • a = a * n := by induction n with
Mathlib/Data/Nat/Cast/Basic.lean
62
63
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Andrew Zipperer, Haitao Zhang, Minchao Wu, Yury Kudryashov -/ import Mathlib.Data.Set.Prod import Mathlib.Data.Set.Restrict /-! # Functions over sets This file contains basic results on the following predicates of functions and sets: * `Set.EqOn f₁ f₂ s` : functions `f₁` and `f₂` are equal at every point of `s`; * `Set.MapsTo f s t` : `f` sends every point of `s` to a point of `t`; * `Set.InjOn f s` : restriction of `f` to `s` is injective; * `Set.SurjOn f s t` : every point in `s` has a preimage in `s`; * `Set.BijOn f s t` : `f` is a bijection between `s` and `t`; * `Set.LeftInvOn f' f s` : for every `x ∈ s` we have `f' (f x) = x`; * `Set.RightInvOn f' f t` : for every `y ∈ t` we have `f (f' y) = y`; * `Set.InvOn f' f s t` : `f'` is a two-side inverse of `f` on `s` and `t`, i.e. we have `Set.LeftInvOn f' f s` and `Set.RightInvOn f' f t`. -/ variable {α β γ δ : Type*} {ι : Sort*} {π : α → Type*} open Equiv Equiv.Perm Function namespace Set /-! ### Equality on a set -/ section equality variable {s s₁ s₂ : Set α} {f₁ f₂ f₃ : α → β} {g : β → γ} {a : α} /-- This lemma exists for use by `aesop` as a forward rule. -/ @[aesop safe forward] lemma EqOn.eq_of_mem (h : s.EqOn f₁ f₂) (ha : a ∈ s) : f₁ a = f₂ a := h ha @[simp] theorem eqOn_empty (f₁ f₂ : α → β) : EqOn f₁ f₂ ∅ := fun _ => False.elim @[simp] theorem eqOn_singleton : Set.EqOn f₁ f₂ {a} ↔ f₁ a = f₂ a := by simp [Set.EqOn] @[simp] theorem eqOn_univ (f₁ f₂ : α → β) : EqOn f₁ f₂ univ ↔ f₁ = f₂ := by simp [EqOn, funext_iff] @[symm] theorem EqOn.symm (h : EqOn f₁ f₂ s) : EqOn f₂ f₁ s := fun _ hx => (h hx).symm theorem eqOn_comm : EqOn f₁ f₂ s ↔ EqOn f₂ f₁ s := ⟨EqOn.symm, EqOn.symm⟩ -- This can not be tagged as `@[refl]` with the current argument order. -- See note below at `EqOn.trans`. theorem eqOn_refl (f : α → β) (s : Set α) : EqOn f f s := fun _ _ => rfl -- Note: this was formerly tagged with `@[trans]`, and although the `trans` attribute accepted it -- the `trans` tactic could not use it. -- An update to the trans tactic coming in https://github.com/leanprover-community/mathlib4/pull/7014 will reject this attribute. -- It can be restored by changing the argument order from `EqOn f₁ f₂ s` to `EqOn s f₁ f₂`. -- This change will be made separately: [zulip](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Reordering.20arguments.20of.20.60Set.2EEqOn.60/near/390467581). theorem EqOn.trans (h₁ : EqOn f₁ f₂ s) (h₂ : EqOn f₂ f₃ s) : EqOn f₁ f₃ s := fun _ hx => (h₁ hx).trans (h₂ hx) theorem EqOn.image_eq (heq : EqOn f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq /-- Variant of `EqOn.image_eq`, for one function being the identity. -/ theorem EqOn.image_eq_self {f : α → α} (h : Set.EqOn f id s) : f '' s = s := by rw [h.image_eq, image_id] theorem EqOn.inter_preimage_eq (heq : EqOn f₁ f₂ s) (t : Set β) : s ∩ f₁ ⁻¹' t = s ∩ f₂ ⁻¹' t := ext fun x => and_congr_right_iff.2 fun hx => by rw [mem_preimage, mem_preimage, heq hx] theorem EqOn.mono (hs : s₁ ⊆ s₂) (hf : EqOn f₁ f₂ s₂) : EqOn f₁ f₂ s₁ := fun _ hx => hf (hs hx) @[simp] theorem eqOn_union : EqOn f₁ f₂ (s₁ ∪ s₂) ↔ EqOn f₁ f₂ s₁ ∧ EqOn f₁ f₂ s₂ := forall₂_or_left theorem EqOn.union (h₁ : EqOn f₁ f₂ s₁) (h₂ : EqOn f₁ f₂ s₂) : EqOn f₁ f₂ (s₁ ∪ s₂) := eqOn_union.2 ⟨h₁, h₂⟩ theorem EqOn.comp_left (h : s.EqOn f₁ f₂) : s.EqOn (g ∘ f₁) (g ∘ f₂) := fun _ ha => congr_arg _ <| h ha @[simp] theorem eqOn_range {ι : Sort*} {f : ι → α} {g₁ g₂ : α → β} : EqOn g₁ g₂ (range f) ↔ g₁ ∘ f = g₂ ∘ f := forall_mem_range.trans <| funext_iff.symm alias ⟨EqOn.comp_eq, _⟩ := eqOn_range end equality variable {s s₁ s₂ : Set α} {t t₁ t₂ : Set β} {p : Set γ} {f f₁ f₂ : α → β} {g g₁ g₂ : β → γ} {f' f₁' f₂' : β → α} {g' : γ → β} {a : α} {b : β} section MapsTo theorem mapsTo' : MapsTo f s t ↔ f '' s ⊆ t := image_subset_iff.symm theorem mapsTo_prodMap_diagonal : MapsTo (Prod.map f f) (diagonal α) (diagonal β) := diagonal_subset_iff.2 fun _ => rfl @[deprecated (since := "2025-04-18")] alias mapsTo_prod_map_diagonal := mapsTo_prodMap_diagonal theorem MapsTo.subset_preimage (hf : MapsTo f s t) : s ⊆ f ⁻¹' t := hf theorem mapsTo_iff_subset_preimage : MapsTo f s t ↔ s ⊆ f ⁻¹' t := Iff.rfl @[simp] theorem mapsTo_singleton {x : α} : MapsTo f {x} t ↔ f x ∈ t := singleton_subset_iff theorem mapsTo_empty (f : α → β) (t : Set β) : MapsTo f ∅ t := empty_subset _ @[simp] theorem mapsTo_empty_iff : MapsTo f s ∅ ↔ s = ∅ := by simp [mapsTo', subset_empty_iff] /-- If `f` maps `s` to `t` and `s` is non-empty, `t` is non-empty. -/ theorem MapsTo.nonempty (h : MapsTo f s t) (hs : s.Nonempty) : t.Nonempty := (hs.image f).mono (mapsTo'.mp h) theorem MapsTo.image_subset (h : MapsTo f s t) : f '' s ⊆ t := mapsTo'.1 h theorem MapsTo.congr (h₁ : MapsTo f₁ s t) (h : EqOn f₁ f₂ s) : MapsTo f₂ s t := fun _ hx => h hx ▸ h₁ hx theorem EqOn.comp_right (hg : t.EqOn g₁ g₂) (hf : s.MapsTo f t) : s.EqOn (g₁ ∘ f) (g₂ ∘ f) := fun _ ha => hg <| hf ha theorem EqOn.mapsTo_iff (H : EqOn f₁ f₂ s) : MapsTo f₁ s t ↔ MapsTo f₂ s t := ⟨fun h => h.congr H, fun h => h.congr H.symm⟩ theorem MapsTo.comp (h₁ : MapsTo g t p) (h₂ : MapsTo f s t) : MapsTo (g ∘ f) s p := fun _ h => h₁ (h₂ h) theorem mapsTo_id (s : Set α) : MapsTo id s s := fun _ => id theorem MapsTo.iterate {f : α → α} {s : Set α} (h : MapsTo f s s) : ∀ n, MapsTo f^[n] s s | 0 => fun _ => id | n + 1 => (MapsTo.iterate h n).comp h theorem MapsTo.iterate_restrict {f : α → α} {s : Set α} (h : MapsTo f s s) (n : ℕ) : (h.restrict f s s)^[n] = (h.iterate n).restrict _ _ _ := by funext x rw [Subtype.ext_iff, MapsTo.val_restrict_apply] induction n generalizing x with | zero => rfl | succ n ihn => simp [Nat.iterate, ihn] lemma mapsTo_of_subsingleton' [Subsingleton β] (f : α → β) (h : s.Nonempty → t.Nonempty) : MapsTo f s t := fun a ha ↦ Subsingleton.mem_iff_nonempty.2 <| h ⟨a, ha⟩ lemma mapsTo_of_subsingleton [Subsingleton α] (f : α → α) (s : Set α) : MapsTo f s s := mapsTo_of_subsingleton' _ id theorem MapsTo.mono (hf : MapsTo f s₁ t₁) (hs : s₂ ⊆ s₁) (ht : t₁ ⊆ t₂) : MapsTo f s₂ t₂ := fun _ hx => ht (hf <| hs hx) theorem MapsTo.mono_left (hf : MapsTo f s₁ t) (hs : s₂ ⊆ s₁) : MapsTo f s₂ t := fun _ hx => hf (hs hx) theorem MapsTo.mono_right (hf : MapsTo f s t₁) (ht : t₁ ⊆ t₂) : MapsTo f s t₂ := fun _ hx => ht (hf hx) theorem MapsTo.union_union (h₁ : MapsTo f s₁ t₁) (h₂ : MapsTo f s₂ t₂) : MapsTo f (s₁ ∪ s₂) (t₁ ∪ t₂) := fun _ hx => hx.elim (fun hx => Or.inl <| h₁ hx) fun hx => Or.inr <| h₂ hx theorem MapsTo.union (h₁ : MapsTo f s₁ t) (h₂ : MapsTo f s₂ t) : MapsTo f (s₁ ∪ s₂) t := union_self t ▸ h₁.union_union h₂ @[simp] theorem mapsTo_union : MapsTo f (s₁ ∪ s₂) t ↔ MapsTo f s₁ t ∧ MapsTo f s₂ t := ⟨fun h => ⟨h.mono subset_union_left (Subset.refl t), h.mono subset_union_right (Subset.refl t)⟩, fun h => h.1.union h.2⟩ theorem MapsTo.inter (h₁ : MapsTo f s t₁) (h₂ : MapsTo f s t₂) : MapsTo f s (t₁ ∩ t₂) := fun _ hx => ⟨h₁ hx, h₂ hx⟩ lemma MapsTo.insert (h : MapsTo f s t) (x : α) : MapsTo f (insert x s) (insert (f x) t) := by simpa [← singleton_union] using h.mono_right subset_union_right theorem MapsTo.inter_inter (h₁ : MapsTo f s₁ t₁) (h₂ : MapsTo f s₂ t₂) : MapsTo f (s₁ ∩ s₂) (t₁ ∩ t₂) := fun _ hx => ⟨h₁ hx.1, h₂ hx.2⟩ @[simp] theorem mapsTo_inter : MapsTo f s (t₁ ∩ t₂) ↔ MapsTo f s t₁ ∧ MapsTo f s t₂ := ⟨fun h => ⟨h.mono (Subset.refl s) inter_subset_left, h.mono (Subset.refl s) inter_subset_right⟩, fun h => h.1.inter h.2⟩ theorem mapsTo_univ (f : α → β) (s : Set α) : MapsTo f s univ := fun _ _ => trivial theorem mapsTo_range (f : α → β) (s : Set α) : MapsTo f s (range f) := (mapsTo_image f s).mono (Subset.refl s) (image_subset_range _ _) @[simp] theorem mapsTo_image_iff {f : α → β} {g : γ → α} {s : Set γ} {t : Set β} : MapsTo f (g '' s) t ↔ MapsTo (f ∘ g) s t := ⟨fun h c hc => h ⟨c, hc, rfl⟩, fun h _ ⟨_, hc⟩ => hc.2 ▸ h hc.1⟩ lemma MapsTo.comp_left (g : β → γ) (hf : MapsTo f s t) : MapsTo (g ∘ f) s (g '' t) := fun x hx ↦ ⟨f x, hf hx, rfl⟩ lemma MapsTo.comp_right {s : Set β} {t : Set γ} (hg : MapsTo g s t) (f : α → β) : MapsTo (g ∘ f) (f ⁻¹' s) t := fun _ hx ↦ hg hx @[simp] lemma mapsTo_univ_iff : MapsTo f univ t ↔ ∀ x, f x ∈ t := ⟨fun h _ => h (mem_univ _), fun h x _ => h x⟩ @[simp] lemma mapsTo_range_iff {g : ι → α} : MapsTo f (range g) t ↔ ∀ i, f (g i) ∈ t := forall_mem_range theorem MapsTo.mem_iff (h : MapsTo f s t) (hc : MapsTo f sᶜ tᶜ) {x} : f x ∈ t ↔ x ∈ s := ⟨fun ht => by_contra fun hs => hc hs ht, fun hx => h hx⟩ end MapsTo /-! ### Injectivity on a set -/ section injOn theorem Subsingleton.injOn (hs : s.Subsingleton) (f : α → β) : InjOn f s := fun _ hx _ hy _ => hs hx hy @[simp] theorem injOn_empty (f : α → β) : InjOn f ∅ := subsingleton_empty.injOn f @[simp] theorem injOn_singleton (f : α → β) (a : α) : InjOn f {a} := subsingleton_singleton.injOn f @[simp] lemma injOn_pair {b : α} : InjOn f {a, b} ↔ f a = f b → a = b := by unfold InjOn; aesop theorem InjOn.eq_iff {x y} (h : InjOn f s) (hx : x ∈ s) (hy : y ∈ s) : f x = f y ↔ x = y := ⟨h hx hy, fun h => h ▸ rfl⟩ theorem InjOn.ne_iff {x y} (h : InjOn f s) (hx : x ∈ s) (hy : y ∈ s) : f x ≠ f y ↔ x ≠ y := (h.eq_iff hx hy).not alias ⟨_, InjOn.ne⟩ := InjOn.ne_iff theorem InjOn.congr (h₁ : InjOn f₁ s) (h : EqOn f₁ f₂ s) : InjOn f₂ s := fun _ hx _ hy => h hx ▸ h hy ▸ h₁ hx hy theorem EqOn.injOn_iff (H : EqOn f₁ f₂ s) : InjOn f₁ s ↔ InjOn f₂ s := ⟨fun h => h.congr H, fun h => h.congr H.symm⟩ theorem InjOn.mono (h : s₁ ⊆ s₂) (ht : InjOn f s₂) : InjOn f s₁ := fun _ hx _ hy H => ht (h hx) (h hy) H theorem injOn_union (h : Disjoint s₁ s₂) : InjOn f (s₁ ∪ s₂) ↔ InjOn f s₁ ∧ InjOn f s₂ ∧ ∀ x ∈ s₁, ∀ y ∈ s₂, f x ≠ f y := by refine ⟨fun H => ⟨H.mono subset_union_left, H.mono subset_union_right, ?_⟩, ?_⟩ · intro x hx y hy hxy obtain rfl : x = y := H (Or.inl hx) (Or.inr hy) hxy exact h.le_bot ⟨hx, hy⟩ · rintro ⟨h₁, h₂, h₁₂⟩ rintro x (hx | hx) y (hy | hy) hxy exacts [h₁ hx hy hxy, (h₁₂ _ hx _ hy hxy).elim, (h₁₂ _ hy _ hx hxy.symm).elim, h₂ hx hy hxy] theorem injOn_insert {f : α → β} {s : Set α} {a : α} (has : a ∉ s) : Set.InjOn f (insert a s) ↔ Set.InjOn f s ∧ f a ∉ f '' s := by rw [← union_singleton, injOn_union (disjoint_singleton_right.2 has)] simp theorem injective_iff_injOn_univ : Injective f ↔ InjOn f univ := ⟨fun h _ _ _ _ hxy => h hxy, fun h _ _ heq => h trivial trivial heq⟩ theorem injOn_of_injective (h : Injective f) {s : Set α} : InjOn f s := fun _ _ _ _ hxy => h hxy alias _root_.Function.Injective.injOn := injOn_of_injective -- A specialization of `injOn_of_injective` for `Subtype.val`. theorem injOn_subtype_val {s : Set { x // p x }} : Set.InjOn Subtype.val s := Subtype.coe_injective.injOn lemma injOn_id (s : Set α) : InjOn id s := injective_id.injOn theorem InjOn.comp (hg : InjOn g t) (hf : InjOn f s) (h : MapsTo f s t) : InjOn (g ∘ f) s := fun _ hx _ hy heq => hf hx hy <| hg (h hx) (h hy) heq lemma InjOn.of_comp (h : InjOn (g ∘ f) s) : InjOn f s := fun _ hx _ hy heq ↦ h hx hy (by simp [heq]) lemma InjOn.image_of_comp (h : InjOn (g ∘ f) s) : InjOn g (f '' s) := forall_mem_image.2 fun _x hx ↦ forall_mem_image.2 fun _y hy heq ↦ congr_arg f <| h hx hy heq lemma InjOn.comp_iff (hf : InjOn f s) : InjOn (g ∘ f) s ↔ InjOn g (f '' s) := ⟨image_of_comp, fun h ↦ InjOn.comp h hf <| mapsTo_image f s⟩ lemma InjOn.iterate {f : α → α} {s : Set α} (h : InjOn f s) (hf : MapsTo f s s) : ∀ n, InjOn f^[n] s | 0 => injOn_id _ | (n + 1) => (h.iterate hf n).comp h hf lemma injOn_of_subsingleton [Subsingleton α] (f : α → β) (s : Set α) : InjOn f s := (injective_of_subsingleton _).injOn theorem _root_.Function.Injective.injOn_range (h : Injective (g ∘ f)) : InjOn g (range f) := by rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ H exact congr_arg f (h H) theorem _root_.Set.InjOn.injective_iff (s : Set β) (h : InjOn g s) (hs : range f ⊆ s) : Injective (g ∘ f) ↔ Injective f := ⟨(·.of_comp), fun h _ ↦ by aesop⟩ theorem exists_injOn_iff_injective [Nonempty β] : (∃ f : α → β, InjOn f s) ↔ ∃ f : s → β, Injective f := ⟨fun ⟨_, hf⟩ => ⟨_, hf.injective⟩, fun ⟨f, hf⟩ => by lift f to α → β using trivial exact ⟨f, injOn_iff_injective.2 hf⟩⟩ theorem injOn_preimage {B : Set (Set β)} (hB : B ⊆ 𝒫 range f) : InjOn (preimage f) B := fun _ hs _ ht hst => (preimage_eq_preimage' (hB hs) (hB ht)).1 hst theorem InjOn.mem_of_mem_image {x} (hf : InjOn f s) (hs : s₁ ⊆ s) (h : x ∈ s) (h₁ : f x ∈ f '' s₁) : x ∈ s₁ := let ⟨_, h', Eq⟩ := h₁ hf (hs h') h Eq ▸ h' theorem InjOn.mem_image_iff {x} (hf : InjOn f s) (hs : s₁ ⊆ s) (hx : x ∈ s) : f x ∈ f '' s₁ ↔ x ∈ s₁ := ⟨hf.mem_of_mem_image hs hx, mem_image_of_mem f⟩ theorem InjOn.preimage_image_inter (hf : InjOn f s) (hs : s₁ ⊆ s) : f ⁻¹' (f '' s₁) ∩ s = s₁ := ext fun _ => ⟨fun ⟨h₁, h₂⟩ => hf.mem_of_mem_image hs h₂ h₁, fun h => ⟨mem_image_of_mem _ h, hs h⟩⟩ theorem EqOn.cancel_left (h : s.EqOn (g ∘ f₁) (g ∘ f₂)) (hg : t.InjOn g) (hf₁ : s.MapsTo f₁ t) (hf₂ : s.MapsTo f₂ t) : s.EqOn f₁ f₂ := fun _ ha => hg (hf₁ ha) (hf₂ ha) (h ha) theorem InjOn.cancel_left (hg : t.InjOn g) (hf₁ : s.MapsTo f₁ t) (hf₂ : s.MapsTo f₂ t) : s.EqOn (g ∘ f₁) (g ∘ f₂) ↔ s.EqOn f₁ f₂ := ⟨fun h => h.cancel_left hg hf₁ hf₂, EqOn.comp_left⟩ lemma InjOn.image_inter {s t u : Set α} (hf : u.InjOn f) (hs : s ⊆ u) (ht : t ⊆ u) : f '' (s ∩ t) = f '' s ∩ f '' t := by apply Subset.antisymm (image_inter_subset _ _ _) intro x ⟨⟨y, ys, hy⟩, ⟨z, zt, hz⟩⟩ have : y = z := by apply hf (hs ys) (ht zt) rwa [← hz] at hy rw [← this] at zt exact ⟨y, ⟨ys, zt⟩, hy⟩ lemma InjOn.image (h : s.InjOn f) : s.powerset.InjOn (image f) := fun s₁ hs₁ s₂ hs₂ h' ↦ by rw [← h.preimage_image_inter hs₁, h', h.preimage_image_inter hs₂] theorem InjOn.image_eq_image_iff (h : s.InjOn f) (h₁ : s₁ ⊆ s) (h₂ : s₂ ⊆ s) : f '' s₁ = f '' s₂ ↔ s₁ = s₂ := h.image.eq_iff h₁ h₂ lemma InjOn.image_subset_image_iff (h : s.InjOn f) (h₁ : s₁ ⊆ s) (h₂ : s₂ ⊆ s) : f '' s₁ ⊆ f '' s₂ ↔ s₁ ⊆ s₂ := by refine ⟨fun h' ↦ ?_, image_subset _⟩ rw [← h.preimage_image_inter h₁, ← h.preimage_image_inter h₂] exact inter_subset_inter_left _ (preimage_mono h') lemma InjOn.image_ssubset_image_iff (h : s.InjOn f) (h₁ : s₁ ⊆ s) (h₂ : s₂ ⊆ s) : f '' s₁ ⊂ f '' s₂ ↔ s₁ ⊂ s₂ := by simp_rw [ssubset_def, h.image_subset_image_iff h₁ h₂, h.image_subset_image_iff h₂ h₁] -- TODO: can this move to a better place? theorem _root_.Disjoint.image {s t u : Set α} {f : α → β} (h : Disjoint s t) (hf : u.InjOn f) (hs : s ⊆ u) (ht : t ⊆ u) : Disjoint (f '' s) (f '' t) := by rw [disjoint_iff_inter_eq_empty] at h ⊢ rw [← hf.image_inter hs ht, h, image_empty] lemma InjOn.image_diff {t : Set α} (h : s.InjOn f) : f '' (s \ t) = f '' s \ f '' (s ∩ t) := by refine subset_antisymm (subset_diff.2 ⟨image_subset f diff_subset, ?_⟩) (diff_subset_iff.2 (by rw [← image_union, inter_union_diff])) exact Disjoint.image disjoint_sdiff_inter h diff_subset inter_subset_left lemma InjOn.image_diff_subset {f : α → β} {t : Set α} (h : InjOn f s) (hst : t ⊆ s) : f '' (s \ t) = f '' s \ f '' t := by rw [h.image_diff, inter_eq_self_of_subset_right hst] alias image_diff_of_injOn := InjOn.image_diff_subset theorem InjOn.imageFactorization_injective (h : InjOn f s) : Injective (s.imageFactorization f) := fun ⟨x, hx⟩ ⟨y, hy⟩ h' ↦ by simpa [imageFactorization, h.eq_iff hx hy] using h' @[simp] theorem imageFactorization_injective_iff : Injective (s.imageFactorization f) ↔ InjOn f s := ⟨fun h x hx y hy _ ↦ by simpa using @h ⟨x, hx⟩ ⟨y, hy⟩ (by simpa [imageFactorization]), InjOn.imageFactorization_injective⟩ end injOn section graphOn variable {x : α × β} lemma graphOn_univ_inj {g : α → β} : univ.graphOn f = univ.graphOn g ↔ f = g := by simp lemma graphOn_univ_injective : Injective (univ.graphOn : (α → β) → Set (α × β)) := fun _f _g ↦ graphOn_univ_inj.1 lemma exists_eq_graphOn_image_fst [Nonempty β] {s : Set (α × β)} : (∃ f : α → β, s = graphOn f (Prod.fst '' s)) ↔ InjOn Prod.fst s := by refine ⟨?_, fun h ↦ ?_⟩ · rintro ⟨f, hf⟩ rw [hf] exact InjOn.image_of_comp <| injOn_id _ · have : ∀ x ∈ Prod.fst '' s, ∃ y, (x, y) ∈ s := forall_mem_image.2 fun (x, y) h ↦ ⟨y, h⟩ choose! f hf using this rw [forall_mem_image] at hf use f rw [graphOn, image_image, EqOn.image_eq_self] exact fun x hx ↦ h (hf hx) hx rfl lemma exists_eq_graphOn [Nonempty β] {s : Set (α × β)} : (∃ f t, s = graphOn f t) ↔ InjOn Prod.fst s := .trans ⟨fun ⟨f, t, hs⟩ ↦ ⟨f, by rw [hs, image_fst_graphOn]⟩, fun ⟨f, hf⟩ ↦ ⟨f, _, hf⟩⟩ exists_eq_graphOn_image_fst end graphOn /-! ### Surjectivity on a set -/ section surjOn theorem SurjOn.subset_range (h : SurjOn f s t) : t ⊆ range f := Subset.trans h <| image_subset_range f s theorem surjOn_iff_exists_map_subtype : SurjOn f s t ↔ ∃ (t' : Set β) (g : s → t'), t ⊆ t' ∧ Surjective g ∧ ∀ x : s, f x = g x := ⟨fun h => ⟨_, (mapsTo_image f s).restrict f s _, h, surjective_mapsTo_image_restrict _ _, fun _ => rfl⟩, fun ⟨t', g, htt', hg, hfg⟩ y hy => let ⟨x, hx⟩ := hg ⟨y, htt' hy⟩ ⟨x, x.2, by rw [hfg, hx, Subtype.coe_mk]⟩⟩ theorem surjOn_empty (f : α → β) (s : Set α) : SurjOn f s ∅ := empty_subset _ @[simp] theorem surjOn_empty_iff : SurjOn f ∅ t ↔ t = ∅ := by simp [SurjOn, subset_empty_iff] @[simp] lemma surjOn_singleton : SurjOn f s {b} ↔ b ∈ f '' s := singleton_subset_iff theorem surjOn_image (f : α → β) (s : Set α) : SurjOn f s (f '' s) := Subset.rfl theorem SurjOn.comap_nonempty (h : SurjOn f s t) (ht : t.Nonempty) : s.Nonempty := (ht.mono h).of_image theorem SurjOn.congr (h : SurjOn f₁ s t) (H : EqOn f₁ f₂ s) : SurjOn f₂ s t := by rwa [SurjOn, ← H.image_eq] theorem EqOn.surjOn_iff (h : EqOn f₁ f₂ s) : SurjOn f₁ s t ↔ SurjOn f₂ s t := ⟨fun H => H.congr h, fun H => H.congr h.symm⟩ theorem SurjOn.mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) (hf : SurjOn f s₁ t₂) : SurjOn f s₂ t₁ := Subset.trans ht <| Subset.trans hf <| image_subset _ hs theorem SurjOn.union (h₁ : SurjOn f s t₁) (h₂ : SurjOn f s t₂) : SurjOn f s (t₁ ∪ t₂) := fun _ hx => hx.elim (fun hx => h₁ hx) fun hx => h₂ hx theorem SurjOn.union_union (h₁ : SurjOn f s₁ t₁) (h₂ : SurjOn f s₂ t₂) : SurjOn f (s₁ ∪ s₂) (t₁ ∪ t₂) := (h₁.mono subset_union_left (Subset.refl _)).union (h₂.mono subset_union_right (Subset.refl _)) theorem SurjOn.inter_inter (h₁ : SurjOn f s₁ t₁) (h₂ : SurjOn f s₂ t₂) (h : InjOn f (s₁ ∪ s₂)) : SurjOn f (s₁ ∩ s₂) (t₁ ∩ t₂) := by intro y hy rcases h₁ hy.1 with ⟨x₁, hx₁, rfl⟩ rcases h₂ hy.2 with ⟨x₂, hx₂, heq⟩ obtain rfl : x₁ = x₂ := h (Or.inl hx₁) (Or.inr hx₂) heq.symm exact mem_image_of_mem f ⟨hx₁, hx₂⟩ theorem SurjOn.inter (h₁ : SurjOn f s₁ t) (h₂ : SurjOn f s₂ t) (h : InjOn f (s₁ ∪ s₂)) : SurjOn f (s₁ ∩ s₂) t := inter_self t ▸ h₁.inter_inter h₂ h lemma surjOn_id (s : Set α) : SurjOn id s s := by simp [SurjOn] theorem SurjOn.comp (hg : SurjOn g t p) (hf : SurjOn f s t) : SurjOn (g ∘ f) s p := Subset.trans hg <| Subset.trans (image_subset g hf) <| image_comp g f s ▸ Subset.refl _ lemma SurjOn.of_comp (h : SurjOn (g ∘ f) s p) (hr : MapsTo f s t) : SurjOn g t p := by intro z hz obtain ⟨x, hx, rfl⟩ := h hz exact ⟨f x, hr hx, rfl⟩ lemma surjOn_comp_iff : SurjOn (g ∘ f) s p ↔ SurjOn g (f '' s) p := ⟨fun h ↦ h.of_comp <| mapsTo_image f s, fun h ↦ h.comp <| surjOn_image _ _⟩ lemma SurjOn.iterate {f : α → α} {s : Set α} (h : SurjOn f s s) : ∀ n, SurjOn f^[n] s s | 0 => surjOn_id _ | (n + 1) => (h.iterate n).comp h lemma SurjOn.comp_left (hf : SurjOn f s t) (g : β → γ) : SurjOn (g ∘ f) s (g '' t) := by rw [SurjOn, image_comp g f]; exact image_subset _ hf lemma SurjOn.comp_right {s : Set β} {t : Set γ} (hf : Surjective f) (hg : SurjOn g s t) : SurjOn (g ∘ f) (f ⁻¹' s) t := by rwa [SurjOn, image_comp g f, image_preimage_eq _ hf] lemma surjOn_of_subsingleton' [Subsingleton β] (f : α → β) (h : t.Nonempty → s.Nonempty) : SurjOn f s t := fun _ ha ↦ Subsingleton.mem_iff_nonempty.2 <| (h ⟨_, ha⟩).image _ lemma surjOn_of_subsingleton [Subsingleton α] (f : α → α) (s : Set α) : SurjOn f s s := surjOn_of_subsingleton' _ id theorem surjective_iff_surjOn_univ : Surjective f ↔ SurjOn f univ univ := by simp [Surjective, SurjOn, subset_def] theorem SurjOn.image_eq_of_mapsTo (h₁ : SurjOn f s t) (h₂ : MapsTo f s t) : f '' s = t := eq_of_subset_of_subset h₂.image_subset h₁ theorem image_eq_iff_surjOn_mapsTo : f '' s = t ↔ s.SurjOn f t ∧ s.MapsTo f t := by refine ⟨?_, fun h => h.1.image_eq_of_mapsTo h.2⟩ rintro rfl exact ⟨s.surjOn_image f, s.mapsTo_image f⟩ lemma SurjOn.image_preimage (h : Set.SurjOn f s t) (ht : t₁ ⊆ t) : f '' (f ⁻¹' t₁) = t₁ := image_preimage_eq_iff.2 fun _ hx ↦ mem_range_of_mem_image f s <| h <| ht hx theorem SurjOn.mapsTo_compl (h : SurjOn f s t) (h' : Injective f) : MapsTo f sᶜ tᶜ := fun _ hs ht => let ⟨_, hx', HEq⟩ := h ht hs <| h' HEq ▸ hx' theorem MapsTo.surjOn_compl (h : MapsTo f s t) (h' : Surjective f) : SurjOn f sᶜ tᶜ := h'.forall.2 fun _ ht => (mem_image_of_mem _) fun hs => ht (h hs) theorem EqOn.cancel_right (hf : s.EqOn (g₁ ∘ f) (g₂ ∘ f)) (hf' : s.SurjOn f t) : t.EqOn g₁ g₂ := by intro b hb obtain ⟨a, ha, rfl⟩ := hf' hb exact hf ha theorem SurjOn.cancel_right (hf : s.SurjOn f t) (hf' : s.MapsTo f t) : s.EqOn (g₁ ∘ f) (g₂ ∘ f) ↔ t.EqOn g₁ g₂ := ⟨fun h => h.cancel_right hf, fun h => h.comp_right hf'⟩ theorem eqOn_comp_right_iff : s.EqOn (g₁ ∘ f) (g₂ ∘ f) ↔ (f '' s).EqOn g₁ g₂ := (s.surjOn_image f).cancel_right <| s.mapsTo_image f theorem SurjOn.forall {p : β → Prop} (hf : s.SurjOn f t) (hf' : s.MapsTo f t) : (∀ y ∈ t, p y) ↔ (∀ x ∈ s, p (f x)) := ⟨fun H x hx ↦ H (f x) (hf' hx), fun H _y hy ↦ let ⟨x, hx, hxy⟩ := hf hy; hxy ▸ H x hx⟩ end surjOn /-! ### Bijectivity -/ section bijOn theorem BijOn.mapsTo (h : BijOn f s t) : MapsTo f s t := h.left theorem BijOn.injOn (h : BijOn f s t) : InjOn f s := h.right.left theorem BijOn.surjOn (h : BijOn f s t) : SurjOn f s t := h.right.right theorem BijOn.mk (h₁ : MapsTo f s t) (h₂ : InjOn f s) (h₃ : SurjOn f s t) : BijOn f s t := ⟨h₁, h₂, h₃⟩ theorem bijOn_empty (f : α → β) : BijOn f ∅ ∅ := ⟨mapsTo_empty f ∅, injOn_empty f, surjOn_empty f ∅⟩ @[simp] theorem bijOn_empty_iff_left : BijOn f s ∅ ↔ s = ∅ := ⟨fun h ↦ by simpa using h.mapsTo, by rintro rfl; exact bijOn_empty f⟩ @[simp] theorem bijOn_empty_iff_right : BijOn f ∅ t ↔ t = ∅ := ⟨fun h ↦ by simpa using h.surjOn, by rintro rfl; exact bijOn_empty f⟩ @[simp] lemma bijOn_singleton : BijOn f {a} {b} ↔ f a = b := by simp [BijOn, eq_comm] theorem BijOn.inter_mapsTo (h₁ : BijOn f s₁ t₁) (h₂ : MapsTo f s₂ t₂) (h₃ : s₁ ∩ f ⁻¹' t₂ ⊆ s₂) : BijOn f (s₁ ∩ s₂) (t₁ ∩ t₂) := ⟨h₁.mapsTo.inter_inter h₂, h₁.injOn.mono inter_subset_left, fun _ hy => let ⟨x, hx, hxy⟩ := h₁.surjOn hy.1 ⟨x, ⟨hx, h₃ ⟨hx, hxy.symm.subst hy.2⟩⟩, hxy⟩⟩ theorem MapsTo.inter_bijOn (h₁ : MapsTo f s₁ t₁) (h₂ : BijOn f s₂ t₂) (h₃ : s₂ ∩ f ⁻¹' t₁ ⊆ s₁) : BijOn f (s₁ ∩ s₂) (t₁ ∩ t₂) := inter_comm s₂ s₁ ▸ inter_comm t₂ t₁ ▸ h₂.inter_mapsTo h₁ h₃ theorem BijOn.inter (h₁ : BijOn f s₁ t₁) (h₂ : BijOn f s₂ t₂) (h : InjOn f (s₁ ∪ s₂)) : BijOn f (s₁ ∩ s₂) (t₁ ∩ t₂) := ⟨h₁.mapsTo.inter_inter h₂.mapsTo, h₁.injOn.mono inter_subset_left, h₁.surjOn.inter_inter h₂.surjOn h⟩ theorem BijOn.union (h₁ : BijOn f s₁ t₁) (h₂ : BijOn f s₂ t₂) (h : InjOn f (s₁ ∪ s₂)) : BijOn f (s₁ ∪ s₂) (t₁ ∪ t₂) := ⟨h₁.mapsTo.union_union h₂.mapsTo, h, h₁.surjOn.union_union h₂.surjOn⟩ theorem BijOn.subset_range (h : BijOn f s t) : t ⊆ range f := h.surjOn.subset_range theorem InjOn.bijOn_image (h : InjOn f s) : BijOn f s (f '' s) := BijOn.mk (mapsTo_image f s) h (Subset.refl _) theorem BijOn.congr (h₁ : BijOn f₁ s t) (h : EqOn f₁ f₂ s) : BijOn f₂ s t := BijOn.mk (h₁.mapsTo.congr h) (h₁.injOn.congr h) (h₁.surjOn.congr h) theorem EqOn.bijOn_iff (H : EqOn f₁ f₂ s) : BijOn f₁ s t ↔ BijOn f₂ s t := ⟨fun h => h.congr H, fun h => h.congr H.symm⟩ theorem BijOn.image_eq (h : BijOn f s t) : f '' s = t := h.surjOn.image_eq_of_mapsTo h.mapsTo lemma BijOn.forall {p : β → Prop} (hf : BijOn f s t) : (∀ b ∈ t, p b) ↔ ∀ a ∈ s, p (f a) where mp h _ ha := h _ <| hf.mapsTo ha mpr h b hb := by obtain ⟨a, ha, rfl⟩ := hf.surjOn hb; exact h _ ha lemma BijOn.exists {p : β → Prop} (hf : BijOn f s t) : (∃ b ∈ t, p b) ↔ ∃ a ∈ s, p (f a) where mp := by rintro ⟨b, hb, h⟩; obtain ⟨a, ha, rfl⟩ := hf.surjOn hb; exact ⟨a, ha, h⟩ mpr := by rintro ⟨a, ha, h⟩; exact ⟨f a, hf.mapsTo ha, h⟩ lemma _root_.Equiv.image_eq_iff_bijOn (e : α ≃ β) : e '' s = t ↔ BijOn e s t := ⟨fun h ↦ ⟨(mapsTo_image e s).mono_right h.subset, e.injective.injOn, h ▸ surjOn_image e s⟩, BijOn.image_eq⟩ lemma bijOn_id (s : Set α) : BijOn id s s := ⟨s.mapsTo_id, s.injOn_id, s.surjOn_id⟩ theorem BijOn.comp (hg : BijOn g t p) (hf : BijOn f s t) : BijOn (g ∘ f) s p := BijOn.mk (hg.mapsTo.comp hf.mapsTo) (hg.injOn.comp hf.injOn hf.mapsTo) (hg.surjOn.comp hf.surjOn) /-- If `f : α → β` and `g : β → γ` and if `f` is injective on `s`, then `f ∘ g` is a bijection on `s` iff `g` is a bijection on `f '' s`. -/ theorem bijOn_comp_iff (hf : InjOn f s) : BijOn (g ∘ f) s p ↔ BijOn g (f '' s) p := by simp only [BijOn, InjOn.comp_iff, surjOn_comp_iff, mapsTo_image_iff, hf] /-- If we have a commutative square ``` α --f--> β | | p₁ p₂ | | \/ \/ γ --g--> δ ``` and `f` induces a bijection from `s : Set α` to `t : Set β`, then `g` induces a bijection from the image of `s` to the image of `t`, as long as `g` is is injective on the image of `s`. -/ theorem bijOn_image_image {p₁ : α → γ} {p₂ : β → δ} {g : γ → δ} (comm : ∀ a, p₂ (f a) = g (p₁ a)) (hbij : BijOn f s t) (hinj: InjOn g (p₁ '' s)) : BijOn g (p₁ '' s) (p₂ '' t) := by obtain ⟨h1, h2, h3⟩ := hbij refine ⟨?_, hinj, ?_⟩ · rintro _ ⟨a, ha, rfl⟩ exact ⟨f a, h1 ha, by rw [comm a]⟩ · rintro _ ⟨b, hb, rfl⟩ obtain ⟨a, ha, rfl⟩ := h3 hb rw [← image_comp, comm] exact ⟨a, ha, rfl⟩ lemma BijOn.iterate {f : α → α} {s : Set α} (h : BijOn f s s) : ∀ n, BijOn f^[n] s s | 0 => s.bijOn_id | (n + 1) => (h.iterate n).comp h lemma bijOn_of_subsingleton' [Subsingleton α] [Subsingleton β] (f : α → β) (h : s.Nonempty ↔ t.Nonempty) : BijOn f s t := ⟨mapsTo_of_subsingleton' _ h.1, injOn_of_subsingleton _ _, surjOn_of_subsingleton' _ h.2⟩ lemma bijOn_of_subsingleton [Subsingleton α] (f : α → α) (s : Set α) : BijOn f s s := bijOn_of_subsingleton' _ Iff.rfl theorem BijOn.bijective (h : BijOn f s t) : Bijective (h.mapsTo.restrict f s t) := ⟨fun x y h' => Subtype.ext <| h.injOn x.2 y.2 <| Subtype.ext_iff.1 h', fun ⟨_, hy⟩ => let ⟨x, hx, hxy⟩ := h.surjOn hy ⟨⟨x, hx⟩, Subtype.eq hxy⟩⟩ theorem bijective_iff_bijOn_univ : Bijective f ↔ BijOn f univ univ := Iff.intro (fun h => let ⟨inj, surj⟩ := h ⟨mapsTo_univ f _, inj.injOn, Iff.mp surjective_iff_surjOn_univ surj⟩) fun h => let ⟨_map, inj, surj⟩ := h ⟨Iff.mpr injective_iff_injOn_univ inj, Iff.mpr surjective_iff_surjOn_univ surj⟩ alias ⟨_root_.Function.Bijective.bijOn_univ, _⟩ := bijective_iff_bijOn_univ theorem BijOn.compl (hst : BijOn f s t) (hf : Bijective f) : BijOn f sᶜ tᶜ := ⟨hst.surjOn.mapsTo_compl hf.1, hf.1.injOn, hst.mapsTo.surjOn_compl hf.2⟩ theorem BijOn.subset_right {r : Set β} (hf : BijOn f s t) (hrt : r ⊆ t) : BijOn f (s ∩ f ⁻¹' r) r := by refine ⟨inter_subset_right, hf.injOn.mono inter_subset_left, fun x hx ↦ ?_⟩ obtain ⟨y, hy, rfl⟩ := hf.surjOn (hrt hx) exact ⟨y, ⟨hy, hx⟩, rfl⟩ theorem BijOn.subset_left {r : Set α} (hf : BijOn f s t) (hrs : r ⊆ s) : BijOn f r (f '' r) := (hf.injOn.mono hrs).bijOn_image theorem BijOn.insert_iff (ha : a ∉ s) (hfa : f a ∉ t) : BijOn f (insert a s) (insert (f a) t) ↔ BijOn f s t where mp h := by have := congrArg (· \ {f a}) (image_insert_eq ▸ h.image_eq) simp only [mem_singleton_iff, insert_diff_of_mem] at this rw [diff_singleton_eq_self hfa, diff_singleton_eq_self] at this · exact ⟨by simp [← this, mapsTo'], h.injOn.mono (subset_insert ..), by simp [← this, surjOn_image]⟩ simp only [mem_image, not_exists, not_and] intro x hx rw [h.injOn.eq_iff (by simp [hx]) (by simp)] exact ha ∘ (· ▸ hx) mpr h := by repeat rw [insert_eq] refine (bijOn_singleton.mpr rfl).union h ?_ simp only [singleton_union, injOn_insert fun x ↦ (hfa (h.mapsTo x)), h.injOn, mem_image, not_exists, not_and, true_and] exact fun _ hx h₂ ↦ hfa (h₂ ▸ h.mapsTo hx) theorem BijOn.insert (h₁ : BijOn f s t) (h₂ : f a ∉ t) : BijOn f (insert a s) (insert (f a) t) := (insert_iff (h₂ <| h₁.mapsTo ·) h₂).mpr h₁ theorem BijOn.sdiff_singleton (h₁ : BijOn f s t) (h₂ : a ∈ s) : BijOn f (s \ {a}) (t \ {f a}) := by convert h₁.subset_left diff_subset simp [h₁.injOn.image_diff, h₁.image_eq, h₂, inter_eq_self_of_subset_right] end bijOn /-! ### left inverse -/ namespace LeftInvOn theorem eqOn (h : LeftInvOn f' f s) : EqOn (f' ∘ f) id s := h theorem eq (h : LeftInvOn f' f s) {x} (hx : x ∈ s) : f' (f x) = x := h hx theorem congr_left (h₁ : LeftInvOn f₁' f s) {t : Set β} (h₁' : MapsTo f s t) (heq : EqOn f₁' f₂' t) : LeftInvOn f₂' f s := fun _ hx => heq (h₁' hx) ▸ h₁ hx theorem congr_right (h₁ : LeftInvOn f₁' f₁ s) (heq : EqOn f₁ f₂ s) : LeftInvOn f₁' f₂ s := fun _ hx => heq hx ▸ h₁ hx theorem injOn (h : LeftInvOn f₁' f s) : InjOn f s := fun x₁ h₁ x₂ h₂ heq => calc x₁ = f₁' (f x₁) := Eq.symm <| h h₁ _ = f₁' (f x₂) := congr_arg f₁' heq _ = x₂ := h h₂ theorem surjOn (h : LeftInvOn f' f s) (hf : MapsTo f s t) : SurjOn f' t s := fun x hx => ⟨f x, hf hx, h hx⟩ theorem mapsTo (h : LeftInvOn f' f s) (hf : SurjOn f s t) : MapsTo f' t s := fun y hy => by let ⟨x, hs, hx⟩ := hf hy rwa [← hx, h hs] lemma _root_.Set.leftInvOn_id (s : Set α) : LeftInvOn id id s := fun _ _ ↦ rfl theorem comp (hf' : LeftInvOn f' f s) (hg' : LeftInvOn g' g t) (hf : MapsTo f s t) : LeftInvOn (f' ∘ g') (g ∘ f) s := fun x h => calc (f' ∘ g') ((g ∘ f) x) = f' (f x) := congr_arg f' (hg' (hf h)) _ = x := hf' h theorem mono (hf : LeftInvOn f' f s) (ht : s₁ ⊆ s) : LeftInvOn f' f s₁ := fun _ hx => hf (ht hx) theorem image_inter' (hf : LeftInvOn f' f s) : f '' (s₁ ∩ s) = f' ⁻¹' s₁ ∩ f '' s := by apply Subset.antisymm · rintro _ ⟨x, ⟨h₁, h⟩, rfl⟩ exact ⟨by rwa [mem_preimage, hf h], mem_image_of_mem _ h⟩ · rintro _ ⟨h₁, ⟨x, h, rfl⟩⟩ exact mem_image_of_mem _ ⟨by rwa [← hf h], h⟩ theorem image_inter (hf : LeftInvOn f' f s) : f '' (s₁ ∩ s) = f' ⁻¹' (s₁ ∩ s) ∩ f '' s := by rw [hf.image_inter'] refine Subset.antisymm ?_ (inter_subset_inter_left _ (preimage_mono inter_subset_left)) rintro _ ⟨h₁, x, hx, rfl⟩; exact ⟨⟨h₁, by rwa [hf hx]⟩, mem_image_of_mem _ hx⟩ theorem image_image (hf : LeftInvOn f' f s) : f' '' (f '' s) = s := by rw [Set.image_image, image_congr hf, image_id'] theorem image_image' (hf : LeftInvOn f' f s) (hs : s₁ ⊆ s) : f' '' (f '' s₁) = s₁ := (hf.mono hs).image_image end LeftInvOn /-! ### Right inverse -/ section RightInvOn namespace RightInvOn theorem eqOn (h : RightInvOn f' f t) : EqOn (f ∘ f') id t := h theorem eq (h : RightInvOn f' f t) {y} (hy : y ∈ t) : f (f' y) = y := h hy theorem _root_.Set.LeftInvOn.rightInvOn_image (h : LeftInvOn f' f s) : RightInvOn f' f (f '' s) := fun _y ⟨_x, hx, heq⟩ => heq ▸ (congr_arg f <| h.eq hx) theorem congr_left (h₁ : RightInvOn f₁' f t) (heq : EqOn f₁' f₂' t) : RightInvOn f₂' f t := h₁.congr_right heq theorem congr_right (h₁ : RightInvOn f' f₁ t) (hg : MapsTo f' t s) (heq : EqOn f₁ f₂ s) : RightInvOn f' f₂ t := LeftInvOn.congr_left h₁ hg heq theorem surjOn (hf : RightInvOn f' f t) (hf' : MapsTo f' t s) : SurjOn f s t := LeftInvOn.surjOn hf hf' theorem mapsTo (h : RightInvOn f' f t) (hf : SurjOn f' t s) : MapsTo f s t := LeftInvOn.mapsTo h hf lemma _root_.Set.rightInvOn_id (s : Set α) : RightInvOn id id s := fun _ _ ↦ rfl theorem comp (hf : RightInvOn f' f t) (hg : RightInvOn g' g p) (g'pt : MapsTo g' p t) : RightInvOn (f' ∘ g') (g ∘ f) p := LeftInvOn.comp hg hf g'pt theorem mono (hf : RightInvOn f' f t) (ht : t₁ ⊆ t) : RightInvOn f' f t₁ := LeftInvOn.mono hf ht end RightInvOn theorem InjOn.rightInvOn_of_leftInvOn (hf : InjOn f s) (hf' : LeftInvOn f f' t) (h₁ : MapsTo f s t) (h₂ : MapsTo f' t s) : RightInvOn f f' s := fun _ h => hf (h₂ <| h₁ h) h (hf' (h₁ h)) theorem eqOn_of_leftInvOn_of_rightInvOn (h₁ : LeftInvOn f₁' f s) (h₂ : RightInvOn f₂' f t) (h : MapsTo f₂' t s) : EqOn f₁' f₂' t := fun y hy => calc f₁' y = (f₁' ∘ f ∘ f₂') y := congr_arg f₁' (h₂ hy).symm _ = f₂' y := h₁ (h hy) theorem SurjOn.leftInvOn_of_rightInvOn (hf : SurjOn f s t) (hf' : RightInvOn f f' s) : LeftInvOn f f' t := fun y hy => by let ⟨x, hx, heq⟩ := hf hy rw [← heq, hf' hx] end RightInvOn
/-! ### Two-side inverses -/ namespace InvOn lemma _root_.Set.invOn_id (s : Set α) : InvOn id id s s := ⟨s.leftInvOn_id, s.rightInvOn_id⟩ lemma comp (hf : InvOn f' f s t) (hg : InvOn g' g t p) (fst : MapsTo f s t)
Mathlib/Data/Set/Function.lean
857
863
/- Copyright (c) 2022 Yaël Dillies, Ella Yu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Ella Yu -/ import Mathlib.Algebra.Order.BigOperators.Ring.Finset import Mathlib.Data.Finset.Prod import Mathlib.Data.Fintype.Prod import Mathlib.Algebra.Group.Pointwise.Finset.Basic /-! # Additive energy This file defines the additive energy of two finsets of a group. This is a central quantity in additive combinatorics. ## Main declarations * `Finset.addEnergy`: The additive energy of two finsets in an additive group. * `Finset.mulEnergy`: The multiplicative energy of two finsets in a group. ## Notation The following notations are defined in the `Combinatorics.Additive` scope: * `E[s, t]` for `Finset.addEnergy s t`. * `Eₘ[s, t]` for `Finset.mulEnergy s t`. * `E[s]` for `E[s, s]`. * `Eₘ[s]` for `Eₘ[s, s]`. ## TODO It's possibly interesting to have `(s ×ˢ s) ×ˢ t ×ˢ t).filter (fun x : (α × α) × α × α ↦ x.1.1 * x.2.1 = x.1.2 * x.2.2)` (whose `card` is `mulEnergy s t`) as a standalone definition. -/ open scoped Pointwise variable {α : Type*} [DecidableEq α] namespace Finset section Mul variable [Mul α] {s s₁ s₂ t t₁ t₂ : Finset α} /-- The multiplicative energy `Eₘ[s, t]` of two finsets `s` and `t` in a group is the number of quadruples `(a₁, a₂, b₁, b₂) ∈ s × s × t × t` such that `a₁ * b₁ = a₂ * b₂`. The notation `Eₘ[s, t]` is available in scope `Combinatorics.Additive`. -/ @[to_additive "The additive energy `E[s, t]` of two finsets `s` and `t` in a group is the number of quadruples `(a₁, a₂, b₁, b₂) ∈ s × s × t × t` such that `a₁ + b₁ = a₂ + b₂`. The notation `E[s, t]` is available in scope `Combinatorics.Additive`."] def mulEnergy (s t : Finset α) : ℕ := (((s ×ˢ s) ×ˢ t ×ˢ t).filter fun x : (α × α) × α × α => x.1.1 * x.2.1 = x.1.2 * x.2.2).card /-- The multiplicative energy of two finsets `s` and `t` in a group is the number of quadruples `(a₁, a₂, b₁, b₂) ∈ s × s × t × t` such that `a₁ * b₁ = a₂ * b₂`. -/ scoped[Combinatorics.Additive] notation3:max "Eₘ[" s ", " t "]" => Finset.mulEnergy s t /-- The additive energy of two finsets `s` and `t` in a group is the number of quadruples `(a₁, a₂, b₁, b₂) ∈ s × s × t × t` such that `a₁ + b₁ = a₂ + b₂`. -/ scoped[Combinatorics.Additive] notation3:max "E[" s ", " t "]" => Finset.addEnergy s t /-- The multiplicative energy of a finset `s` in a group is the number of quadruples `(a₁, a₂, b₁, b₂) ∈ s × s × s × s` such that `a₁ * b₁ = a₂ * b₂`. -/ scoped[Combinatorics.Additive] notation3:max "Eₘ[" s "]" => Finset.mulEnergy s s /-- The additive energy of a finset `s` in a group is the number of quadruples `(a₁, a₂, b₁, b₂) ∈ s × s × s × s` such that `a₁ + b₁ = a₂ + b₂`. -/ scoped[Combinatorics.Additive] notation3:max "E[" s "]" => Finset.addEnergy s s open scoped Combinatorics.Additive @[to_additive (attr := gcongr)] lemma mulEnergy_mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : Eₘ[s₁, t₁] ≤ Eₘ[s₂, t₂] := by unfold mulEnergy; gcongr @[to_additive] lemma mulEnergy_mono_left (hs : s₁ ⊆ s₂) : Eₘ[s₁, t] ≤ Eₘ[s₂, t] := mulEnergy_mono hs Subset.rfl @[to_additive] lemma mulEnergy_mono_right (ht : t₁ ⊆ t₂) : Eₘ[s, t₁] ≤ Eₘ[s, t₂] := mulEnergy_mono Subset.rfl ht @[to_additive] lemma le_mulEnergy : s.card * t.card ≤ Eₘ[s, t] := by rw [← card_product] refine card_le_card_of_injOn (@fun x => ((x.1, x.1), x.2, x.2)) (by simp) fun a _ b _ => ?_ simp only [Prod.mk_inj, and_self_iff, and_imp] exact Prod.ext @[to_additive] lemma mulEnergy_pos (hs : s.Nonempty) (ht : t.Nonempty) : 0 < Eₘ[s, t] := (mul_pos hs.card_pos ht.card_pos).trans_le le_mulEnergy variable (s t) @[to_additive (attr := simp)] lemma mulEnergy_empty_left : Eₘ[∅, t] = 0 := by simp [mulEnergy] @[to_additive (attr := simp)] lemma mulEnergy_empty_right : Eₘ[s, ∅] = 0 := by simp [mulEnergy] variable {s t} @[to_additive (attr := simp)] lemma mulEnergy_pos_iff : 0 < Eₘ[s, t] ↔ s.Nonempty ∧ t.Nonempty where mp h := of_not_not fun H => by simp_rw [not_and_or, not_nonempty_iff_eq_empty] at H obtain rfl | rfl := H <;> simp [Nat.not_lt_zero] at h mpr h := mulEnergy_pos h.1 h.2 @[to_additive (attr := simp)] lemma mulEnergy_eq_zero_iff : Eₘ[s, t] = 0 ↔ s = ∅ ∨ t = ∅ := by simp [← (Nat.zero_le _).not_gt_iff_eq, not_and_or, imp_iff_or_not, or_comm] @[to_additive] lemma mulEnergy_eq_card_filter (s t : Finset α) : Eₘ[s, t] = (((s ×ˢ t) ×ˢ s ×ˢ t).filter fun ((a, b), c, d) ↦ a * b = c * d).card := card_equiv (.prodProdProdComm _ _ _ _) (by simp [and_and_and_comm]) @[to_additive] lemma mulEnergy_eq_sum_sq' (s t : Finset α) : Eₘ[s, t] = ∑ a ∈ s * t, ((s ×ˢ t).filter fun (x, y) ↦ x * y = a).card ^ 2 := by simp_rw [mulEnergy_eq_card_filter, sq, ← card_product] rw [← card_disjiUnion] -- The `swap`, `ext` and `simp` calls significantly reduce heartbeats swap · simp only [Set.PairwiseDisjoint, Set.Pairwise, coe_mul, ne_eq, disjoint_left, mem_product, mem_filter, not_and, and_imp, Prod.forall] aesop · congr ext simp only [mem_filter, mem_product, disjiUnion_eq_biUnion, mem_biUnion] aesop (add unsafe mul_mem_mul) @[to_additive] lemma mulEnergy_eq_sum_sq [Fintype α] (s t : Finset α) : Eₘ[s, t] = ∑ a, ((s ×ˢ t).filter fun (x, y) ↦ x * y = a).card ^ 2 := by rw [mulEnergy_eq_sum_sq'] exact Fintype.sum_subset <| by aesop (add simp [filter_eq_empty_iff, mul_mem_mul]) @[to_additive card_sq_le_card_mul_addEnergy] lemma card_sq_le_card_mul_mulEnergy (s t u : Finset α) : ((s ×ˢ t).filter fun (a, b) ↦ a * b ∈ u).card ^ 2 ≤ u.card * Eₘ[s, t] := by calc _ = (∑ c ∈ u, ((s ×ˢ t).filter fun (a, b) ↦ a * b = c).card) ^ 2 := by rw [← sum_card_fiberwise_eq_card_filter] _ ≤ u.card * ∑ c ∈ u, ((s ×ˢ t).filter fun (a, b) ↦ a * b = c).card ^ 2 := by simpa using sum_mul_sq_le_sq_mul_sq (R := ℕ) _ 1 _ _ ≤ u.card * ∑ c ∈ s * t, ((s ×ˢ t).filter fun (a, b) ↦ a * b = c).card ^ 2 := by refine mul_le_mul_left' (sum_le_sum_of_ne_zero ?_) _ aesop (add simp [filter_eq_empty_iff]) (add unsafe mul_mem_mul) _ = u.card * Eₘ[s, t] := by rw [mulEnergy_eq_sum_sq'] @[to_additive le_card_add_mul_addEnergy] lemma le_card_add_mul_mulEnergy (s t : Finset α) : s.card ^ 2 * t.card ^ 2 ≤ (s * t).card * Eₘ[s, t] := calc _ = ((s ×ˢ t).filter fun (a, b) ↦ a * b ∈ s * t).card ^ 2 := by rw [filter_eq_self.2, card_product, mul_pow]; aesop (add unsafe mul_mem_mul) _ ≤ (s * t).card * Eₘ[s, t] := card_sq_le_card_mul_mulEnergy _ _ _ end Mul open scoped Combinatorics.Additive section CommMonoid
variable [CommMonoid α] @[to_additive] lemma mulEnergy_comm (s t : Finset α) : Eₘ[s, t] = Eₘ[t, s] := by rw [mulEnergy, ← Finset.card_map (Equiv.prodComm _ _).toEmbedding, map_filter] simp [-Finset.card_map, eq_comm, mulEnergy, mul_comm, map_eq_image, Function.comp_def] end CommMonoid section CommGroup variable [CommGroup α] [Fintype α] (s t : Finset α)
Mathlib/Combinatorics/Additive/Energy.lean
159
170
/- Copyright (c) 2024 Calle Sönne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Paul Lezeau, Calle Sönne -/ import Mathlib.CategoryTheory.Functor.Category import Mathlib.CategoryTheory.CommSq /-! # HomLift Given a functor `p : 𝒳 ⥤ 𝒮`, this file provides API for expressing the fact that `p(φ) = f` for given morphisms `φ` and `f`. The reason this API is needed is because, in general, `p.map φ = f` does not make sense when the domain and/or codomain of `φ` and `f` are not definitionally equal. ## Main definition Given morphism `φ : a ⟶ b` in `𝒳` and `f : R ⟶ S` in `𝒮`, `p.IsHomLift f φ` is a class, defined using the auxiliary inductive type `IsHomLiftAux` which expresses the fact that `f = p(φ)`. We also define a macro `subst_hom_lift p f φ` which can be used to substitute `f` with `p(φ)` in a goal, this tactic is just short for `obtain ⟨⟩ := Functor.IsHomLift.cond (p:=p) (f:=f) (φ:=φ)`, and it is used to make the code more readable. -/ universe u₁ v₁ u₂ v₂ open CategoryTheory Category variable {𝒮 : Type u₁} {𝒳 : Type u₂} [Category.{v₁} 𝒳] [Category.{v₂} 𝒮] (p : 𝒳 ⥤ 𝒮) namespace CategoryTheory /-- Helper-type for defining `IsHomLift`. -/ inductive IsHomLiftAux : ∀ {R S : 𝒮} {a b : 𝒳} (_ : R ⟶ S) (_ : a ⟶ b), Prop | map {a b : 𝒳} (φ : a ⟶ b) : IsHomLiftAux (p.map φ) φ /-- Given a functor `p : 𝒳 ⥤ 𝒮`, an arrow `φ : a ⟶ b` in `𝒳` and an arrow `f : R ⟶ S` in `𝒮`, `p.IsHomLift f φ` expresses the fact that `φ` lifts `f` through `p`. This is often drawn as: ``` a --φ--> b - - | | v v R --f--> S ``` -/ class Functor.IsHomLift {R S : 𝒮} {a b : 𝒳} (f : R ⟶ S) (φ : a ⟶ b) : Prop where cond : IsHomLiftAux p f φ /-- `subst_hom_lift p f φ` tries to substitute `f` with `p(φ)` by using `p.IsHomLift f φ` -/ macro "subst_hom_lift" p:term:max f:term:max φ:term:max : tactic => `(tactic| obtain ⟨⟩ := Functor.IsHomLift.cond (p := $p) (f := $f) (φ := $φ)) /-- For any arrow `φ : a ⟶ b` in `𝒳`, `φ` lifts the arrow `p.map φ` in the base `𝒮`. -/ @[simp] instance {a b : 𝒳} (φ : a ⟶ b) : p.IsHomLift (p.map φ) φ where cond := by constructor @[simp] instance (a : 𝒳) : p.IsHomLift (𝟙 (p.obj a)) (𝟙 a) := by rw [← p.map_id]; infer_instance namespace IsHomLift protected lemma id {p : 𝒳 ⥤ 𝒮} {R : 𝒮} {a : 𝒳} (ha : p.obj a = R) : p.IsHomLift (𝟙 R) (𝟙 a) := by cases ha; infer_instance section variable {R S : 𝒮} {a b : 𝒳} lemma domain_eq (f : R ⟶ S) (φ : a ⟶ b) [p.IsHomLift f φ] : p.obj a = R := by subst_hom_lift p f φ; rfl lemma codomain_eq (f : R ⟶ S) (φ : a ⟶ b) [p.IsHomLift f φ] : p.obj b = S := by subst_hom_lift p f φ; rfl variable (f : R ⟶ S) (φ : a ⟶ b) [p.IsHomLift f φ] lemma fac : f = eqToHom (domain_eq p f φ).symm ≫ p.map φ ≫ eqToHom (codomain_eq p f φ) := by
subst_hom_lift p f φ; simp
Mathlib/CategoryTheory/FiberedCategory/HomLift.lean
85
86
/- 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
507
508
/- 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 -/ import Mathlib.Data.Countable.Basic import Mathlib.Data.Fin.VecNotation import Mathlib.Order.Disjointed import Mathlib.MeasureTheory.OuterMeasure.Defs import Mathlib.Topology.Instances.ENNReal.Lemmas /-! # Outer Measures An outer measure is a function `μ : Set α → ℝ≥0∞`, from the powerset of a type to the extended nonnegative real numbers that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is monotone; 3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most the sum of the outer measure on the individual sets. Note that we do not need `α` to be measurable to define an outer measure. ## References <https://en.wikipedia.org/wiki/Outer_measure> ## Tags outer measure -/ noncomputable section open Set Function Filter open scoped NNReal Topology ENNReal namespace MeasureTheory section OuterMeasureClass variable {α ι F : Type*} [FunLike F (Set α) ℝ≥0∞] [OuterMeasureClass F α] {μ : F} {s t : Set α} @[simp] theorem measure_empty : μ ∅ = 0 := OuterMeasureClass.measure_empty μ @[mono, gcongr] theorem measure_mono (h : s ⊆ t) : μ s ≤ μ t := OuterMeasureClass.measure_mono μ h theorem measure_mono_null (h : s ⊆ t) (ht : μ t = 0) : μ s = 0 := eq_bot_mono (measure_mono h) ht lemma measure_eq_top_mono (h : s ⊆ t) (hs : μ s = ∞) : μ t = ∞ := eq_top_mono (measure_mono h) hs lemma measure_lt_top_mono (h : s ⊆ t) (ht : μ t < ∞) : μ s < ∞ := (measure_mono h).trans_lt ht theorem measure_pos_of_superset (h : s ⊆ t) (hs : μ s ≠ 0) : 0 < μ t := hs.bot_lt.trans_le (measure_mono h) theorem measure_iUnion_le [Countable ι] (s : ι → Set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := by refine rel_iSup_tsum μ measure_empty (· ≤ ·) (fun t ↦ ?_) _ calc μ (⋃ i, t i) = μ (⋃ i, disjointed t i) := by rw [iUnion_disjointed] _ ≤ ∑' i, μ (disjointed t i) := OuterMeasureClass.measure_iUnion_nat_le _ _ (disjoint_disjointed _) _ ≤ ∑' i, μ (t i) := by gcongr; exact disjointed_subset .. theorem measure_biUnion_le {I : Set ι} (μ : F) (hI : I.Countable) (s : ι → Set α) : μ (⋃ i ∈ I, s i) ≤ ∑' i : I, μ (s i) := by have := hI.to_subtype rw [biUnion_eq_iUnion] apply measure_iUnion_le theorem measure_biUnion_finset_le (I : Finset ι) (s : ι → Set α) : μ (⋃ i ∈ I, s i) ≤ ∑ i ∈ I, μ (s i) := (measure_biUnion_le μ I.countable_toSet s).trans_eq <| I.tsum_subtype (μ <| s ·) theorem measure_iUnion_fintype_le [Fintype ι] (μ : F) (s : ι → Set α) : μ (⋃ i, s i) ≤ ∑ i, μ (s i) := by simpa using measure_biUnion_finset_le Finset.univ s theorem measure_union_le (s t : Set α) : μ (s ∪ t) ≤ μ s + μ t := by simpa [union_eq_iUnion] using measure_iUnion_fintype_le μ (cond · s t) lemma measure_univ_le_add_compl (s : Set α) : μ univ ≤ μ s + μ sᶜ := s.union_compl_self ▸ measure_union_le s sᶜ theorem measure_le_inter_add_diff (μ : F) (s t : Set α) : μ s ≤ μ (s ∩ t) + μ (s \ t) := by simpa using measure_union_le (s ∩ t) (s \ t)
theorem measure_diff_null (ht : μ t = 0) : μ (s \ t) = μ s := (measure_mono diff_subset).antisymm <| calc
Mathlib/MeasureTheory/OuterMeasure/Basic.lean
93
94
/- 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.Algebra.Bilinear import Mathlib.Algebra.Algebra.Opposite import Mathlib.Algebra.Group.Pointwise.Finset.Basic import Mathlib.Algebra.Group.Pointwise.Set.BigOperators import Mathlib.Algebra.Module.Submodule.Pointwise import Mathlib.Algebra.Ring.NonZeroDivisors import Mathlib.Algebra.Ring.Submonoid.Pointwise import Mathlib.Data.Set.Semiring import Mathlib.GroupTheory.GroupAction.SubMulAction.Pointwise /-! # Multiplication and division of submodules of an algebra. An interface for multiplication and division of sub-R-modules of an R-algebra A is developed. ## Main definitions Let `R` be a commutative ring (or semiring) and let `A` be an `R`-algebra. * `1 : Submodule R A` : the R-submodule R of the R-algebra A * `Mul (Submodule R A)` : multiplication of two sub-R-modules M and N of A is defined to be the smallest submodule containing all the products `m * n`. * `Div (Submodule R A)` : `I / J` is defined to be the submodule consisting of all `a : A` such that `a • J ⊆ I` It is proved that `Submodule R A` is a semiring, and also an algebra over `Set A`. Additionally, in the `Pointwise` locale we promote `Submodule.pointwiseDistribMulAction` to a `MulSemiringAction` as `Submodule.pointwiseMulSemiringAction`. When `R` is not necessarily commutative, and `A` is merely a `R`-module with a ring structure such that `IsScalarTower R A A` holds (equivalent to the data of a ring homomorphism `R →+* A` by `ringHomEquivModuleIsScalarTower`), we can still define `1 : Submodule R A` and `Mul (Submodule R A)`, but `1` is only a left identity, not necessarily a right one. ## Tags multiplication of submodules, division of submodules, submodule semiring -/ universe uι u v open Algebra Set MulOpposite open Pointwise namespace SubMulAction variable {R : Type u} {A : Type v} [CommSemiring R] [Semiring A] [Algebra R A] theorem algebraMap_mem (r : R) : algebraMap R A r ∈ (1 : SubMulAction R A) := ⟨r, (algebraMap_eq_smul_one r).symm⟩ theorem mem_one' {x : A} : x ∈ (1 : SubMulAction R A) ↔ ∃ y, algebraMap R A y = x := exists_congr fun r => by rw [algebraMap_eq_smul_one] end SubMulAction namespace Submodule section Module variable {R : Type u} [Semiring R] {A : Type v} [Semiring A] [Module R A] -- TODO: Why is this in a file about `Algebra`? -- TODO: potentially change this back to `LinearMap.range (Algebra.linearMap R A)` -- once a version of `Algebra` without the `commutes'` field is introduced. -- See issue https://github.com/leanprover-community/mathlib4/issues/18110. /-- `1 : Submodule R A` is the submodule `R ∙ 1` of `A`. -/ instance one : One (Submodule R A) := ⟨LinearMap.range (LinearMap.toSpanSingleton R A 1)⟩ theorem one_eq_span : (1 : Submodule R A) = R ∙ 1 := (LinearMap.span_singleton_eq_range _ _ _).symm theorem le_one_toAddSubmonoid : 1 ≤ (1 : Submodule R A).toAddSubmonoid := by rintro x ⟨n, rfl⟩ exact ⟨n, show (n : R) • (1 : A) = n by rw [Nat.cast_smul_eq_nsmul, nsmul_one]⟩ @[simp] theorem toSubMulAction_one : (1 : Submodule R A).toSubMulAction = 1 := SetLike.ext fun _ ↦ by rw [one_eq_span, SubMulAction.mem_one]; exact mem_span_singleton theorem one_eq_span_one_set : (1 : Submodule R A) = span R 1 := one_eq_span @[simp] theorem one_le {P : Submodule R A} : (1 : Submodule R A) ≤ P ↔ (1 : A) ∈ P := by simp [one_eq_span] variable {M : Type*} [AddCommMonoid M] [Module R M] [Module A M] [IsScalarTower R A M] instance : SMul (Submodule R A) (Submodule R M) where smul A' M' := { __ := A'.toAddSubmonoid • M'.toAddSubmonoid smul_mem' := fun r m hm ↦ AddSubmonoid.smul_induction_on hm (fun a ha m hm ↦ by rw [← smul_assoc]; exact AddSubmonoid.smul_mem_smul (A'.smul_mem r ha) hm) fun m₁ m₂ h₁ h₂ ↦ by rw [smul_add]; exact (A'.1 • M'.1).add_mem h₁ h₂ } section variable {I J : Submodule R A} {N P : Submodule R M} theorem smul_toAddSubmonoid : (I • N).toAddSubmonoid = I.toAddSubmonoid • N.toAddSubmonoid := rfl theorem smul_mem_smul {r} {n} (hr : r ∈ I) (hn : n ∈ N) : r • n ∈ I • N := AddSubmonoid.smul_mem_smul hr hn theorem smul_le : I • N ≤ P ↔ ∀ r ∈ I, ∀ n ∈ N, r • n ∈ P := AddSubmonoid.smul_le @[simp, norm_cast] lemma coe_set_smul : (I : Set A) • N = I • N := set_smul_eq_of_le _ _ _ (fun _ _ hr hx ↦ smul_mem_smul hr hx) (smul_le.mpr fun _ hr _ hx ↦ mem_set_smul_of_mem_mem hr hx) @[elab_as_elim] theorem smul_induction_on {p : M → Prop} {x} (H : x ∈ I • N) (smul : ∀ r ∈ I, ∀ n ∈ N, p (r • n)) (add : ∀ x y, p x → p y → p (x + y)) : p x := AddSubmonoid.smul_induction_on H smul add /-- Dependent version of `Submodule.smul_induction_on`. -/ @[elab_as_elim] theorem smul_induction_on' {x : M} (hx : x ∈ I • N) {p : ∀ x, x ∈ I • N → Prop} (smul : ∀ (r : A) (hr : r ∈ I) (n : M) (hn : n ∈ N), p (r • n) (smul_mem_smul hr hn)) (add : ∀ x hx y hy, p x hx → p y hy → p (x + y) (add_mem ‹_› ‹_›)) : p x hx := by refine Exists.elim ?_ fun (h : x ∈ I • N) (H : p x h) ↦ H exact smul_induction_on hx (fun a ha x hx ↦ ⟨_, smul _ ha _ hx⟩) fun x y ⟨_, hx⟩ ⟨_, hy⟩ ↦ ⟨_, add _ _ _ _ hx hy⟩ theorem smul_mono (hij : I ≤ J) (hnp : N ≤ P) : I • N ≤ J • P := AddSubmonoid.smul_le_smul hij hnp theorem smul_mono_left (h : I ≤ J) : I • N ≤ J • N := smul_mono h le_rfl instance : CovariantClass (Submodule R A) (Submodule R M) HSMul.hSMul LE.le := ⟨fun _ _ => smul_mono le_rfl⟩ variable (I J N P) @[simp] theorem smul_bot : I • (⊥ : Submodule R M) = ⊥ := toAddSubmonoid_injective <| AddSubmonoid.addSubmonoid_smul_bot _ @[simp] theorem bot_smul : (⊥ : Submodule R A) • N = ⊥ := le_bot_iff.mp <| smul_le.mpr <| by rintro _ rfl _ _; rw [zero_smul]; exact zero_mem _ theorem smul_sup : I • (N ⊔ P) = I • N ⊔ I • P := toAddSubmonoid_injective <| by simp only [smul_toAddSubmonoid, sup_toAddSubmonoid, AddSubmonoid.addSubmonoid_smul_sup] theorem sup_smul : (I ⊔ J) • N = I • N ⊔ J • N := le_antisymm (smul_le.mpr fun mn hmn p hp ↦ by obtain ⟨m, hm, n, hn, rfl⟩ := mem_sup.mp hmn rw [add_smul]; exact add_mem_sup (smul_mem_smul hm hp) <| smul_mem_smul hn hp) (sup_le (smul_mono_left le_sup_left) <| smul_mono_left le_sup_right) protected theorem smul_assoc {B} [Semiring B] [Module R B] [Module A B] [Module B M] [IsScalarTower R A B] [IsScalarTower R B M] [IsScalarTower A B M] (I : Submodule R A) (J : Submodule R B) (N : Submodule R M) : (I • J) • N = I • J • N := le_antisymm (smul_le.2 fun _ hrsij t htn ↦ smul_induction_on hrsij (fun r hr s hs ↦ smul_assoc r s t ▸ smul_mem_smul hr (smul_mem_smul hs htn)) fun x y ↦ (add_smul x y t).symm ▸ add_mem) (smul_le.2 fun r hr _ hsn ↦ smul_induction_on hsn (fun j hj n hn ↦ (smul_assoc r j n).symm ▸ smul_mem_smul (smul_mem_smul hr hj) hn) fun m₁ m₂ ↦ (smul_add r m₁ m₂) ▸ add_mem) theorem smul_iSup {ι : Sort*} {I : Submodule R A} {t : ι → Submodule R M} : I • (⨆ i, t i)= ⨆ i, I • t i := toAddSubmonoid_injective <| by simp only [smul_toAddSubmonoid, iSup_toAddSubmonoid, AddSubmonoid.smul_iSup] theorem iSup_smul {ι : Sort*} {t : ι → Submodule R A} {N : Submodule R M} : (⨆ i, t i) • N = ⨆ i, t i • N := le_antisymm (smul_le.mpr fun t ht s hs ↦ iSup_induction _ (motive := (· • s ∈ _)) ht (fun i t ht ↦ mem_iSup_of_mem i <| smul_mem_smul ht hs) (by simp_rw [zero_smul]; apply zero_mem) fun x y ↦ by simp_rw [add_smul]; apply add_mem) (iSup_le fun i ↦ Submodule.smul_mono_left <| le_iSup _ i) protected theorem one_smul : (1 : Submodule R A) • N = N := by refine le_antisymm (smul_le.mpr fun r hr m hm ↦ ?_) fun m hm ↦ ?_ · obtain ⟨r, rfl⟩ := hr rw [LinearMap.toSpanSingleton_apply, smul_one_smul]; exact N.smul_mem r hm · rw [← one_smul A m]; exact smul_mem_smul (one_le.mp le_rfl) hm theorem smul_subset_smul : (↑I : Set A) • (↑N : Set M) ⊆ (↑(I • N) : Set M) := AddSubmonoid.smul_subset_smul end variable [IsScalarTower R A A] /-- Multiplication of sub-R-modules of an R-module A that is also a semiring. The submodule `M * N` consists of finite sums of elements `m * n` for `m ∈ M` and `n ∈ N`. -/ instance mul : Mul (Submodule R A) where mul := (· • ·) variable (S T : Set A) {M N P Q : Submodule R A} {m n : A} theorem mul_mem_mul (hm : m ∈ M) (hn : n ∈ N) : m * n ∈ M * N := smul_mem_smul hm hn theorem mul_le : M * N ≤ P ↔ ∀ m ∈ M, ∀ n ∈ N, m * n ∈ P := smul_le theorem mul_toAddSubmonoid (M N : Submodule R A) : (M * N).toAddSubmonoid = M.toAddSubmonoid * N.toAddSubmonoid := rfl @[elab_as_elim] protected theorem mul_induction_on {C : A → Prop} {r : A} (hr : r ∈ M * N) (hm : ∀ m ∈ M, ∀ n ∈ N, C (m * n)) (ha : ∀ x y, C x → C y → C (x + y)) : C r := smul_induction_on hr hm ha /-- A dependent version of `mul_induction_on`. -/ @[elab_as_elim] protected theorem mul_induction_on' {C : ∀ r, r ∈ M * N → Prop} (mem_mul_mem : ∀ m (hm : m ∈ M) n (hn : n ∈ N), C (m * n) (mul_mem_mul hm hn)) (add : ∀ x hx y hy, C x hx → C y hy → C (x + y) (add_mem hx hy)) {r : A} (hr : r ∈ M * N) : C r hr := smul_induction_on' hr mem_mul_mem add variable (M) @[simp] theorem mul_bot : M * ⊥ = ⊥ := smul_bot _ @[simp] theorem bot_mul : ⊥ * M = ⊥ := bot_smul _ protected theorem one_mul : (1 : Submodule R A) * M = M := Submodule.one_smul _ variable {M} @[mono] theorem mul_le_mul (hmp : M ≤ P) (hnq : N ≤ Q) : M * N ≤ P * Q := smul_mono hmp hnq theorem mul_le_mul_left (h : M ≤ N) : M * P ≤ N * P := smul_mono_left h theorem mul_le_mul_right (h : N ≤ P) : M * N ≤ M * P := smul_mono_right _ h theorem mul_comm_of_commute (h : ∀ m ∈ M, ∀ n ∈ N, Commute m n) : M * N = N * M := toAddSubmonoid_injective <| AddSubmonoid.mul_comm_of_commute h variable (M N P) theorem mul_sup : M * (N ⊔ P) = M * N ⊔ M * P := smul_sup _ _ _ theorem sup_mul : (M ⊔ N) * P = M * P ⊔ N * P := sup_smul _ _ _ theorem mul_subset_mul : (↑M : Set A) * (↑N : Set A) ⊆ (↑(M * N) : Set A) := smul_subset_smul _ _ lemma restrictScalars_mul {A B C} [Semiring A] [Semiring B] [Semiring C] [SMul A B] [Module A C] [Module B C] [IsScalarTower A C C] [IsScalarTower B C C] [IsScalarTower A B C] {I J : Submodule B C} : (I * J).restrictScalars A = I.restrictScalars A * J.restrictScalars A := rfl variable {ι : Sort uι} theorem iSup_mul (s : ι → Submodule R A) (t : Submodule R A) : (⨆ i, s i) * t = ⨆ i, s i * t := iSup_smul theorem mul_iSup (t : Submodule R A) (s : ι → Submodule R A) : (t * ⨆ i, s i) = ⨆ i, t * s i := smul_iSup /-- Sub-`R`-modules of an `R`-module form an idempotent semiring. -/ instance : NonUnitalSemiring (Submodule R A) where __ := toAddSubmonoid_injective.semigroup _ mul_toAddSubmonoid zero_mul := bot_mul mul_zero := mul_bot left_distrib := mul_sup right_distrib := sup_mul instance : Pow (Submodule R A) ℕ where pow s n := npowRec n s theorem pow_eq_npowRec {n : ℕ} : M ^ n = npowRec n M := rfl protected theorem pow_zero : M ^ 0 = 1 := rfl protected theorem pow_succ {n : ℕ} : M ^ (n + 1) = M ^ n * M := rfl protected theorem pow_add {m n : ℕ} (h : n ≠ 0) : M ^ (m + n) = M ^ m * M ^ n := npowRec_add m n h _ M.one_mul protected theorem pow_one : M ^ 1 = M := by rw [Submodule.pow_succ, Submodule.pow_zero, Submodule.one_mul] /-- `Submodule.pow_succ` with the right hand side commuted. -/ protected theorem pow_succ' {n : ℕ} (h : n ≠ 0) : M ^ (n + 1) = M * M ^ n := by rw [add_comm, M.pow_add h, Submodule.pow_one] theorem pow_toAddSubmonoid {n : ℕ} (h : n ≠ 0) : (M ^ n).toAddSubmonoid = M.toAddSubmonoid ^ n := by induction n with | zero => exact (h rfl).elim | succ n ih => rw [Submodule.pow_succ, pow_succ, mul_toAddSubmonoid] cases n with | zero => rw [Submodule.pow_zero, pow_zero, one_mul, ← mul_toAddSubmonoid, Submodule.one_mul] | succ n => rw [ih n.succ_ne_zero] theorem le_pow_toAddSubmonoid {n : ℕ} : M.toAddSubmonoid ^ n ≤ (M ^ n).toAddSubmonoid := by obtain rfl | hn := Decidable.eq_or_ne n 0 · rw [Submodule.pow_zero, pow_zero] exact le_one_toAddSubmonoid · exact (pow_toAddSubmonoid M hn).ge theorem pow_subset_pow {n : ℕ} : (↑M : Set A) ^ n ⊆ ↑(M ^ n : Submodule R A) := trans AddSubmonoid.pow_subset_pow (le_pow_toAddSubmonoid M) theorem pow_mem_pow {x : A} (hx : x ∈ M) (n : ℕ) : x ^ n ∈ M ^ n := pow_subset_pow _ <| Set.pow_mem_pow hx lemma restrictScalars_pow {A B C : Type*} [Semiring A] [Semiring B] [Semiring C] [SMul A B] [Module A C] [Module B C] [IsScalarTower A C C] [IsScalarTower B C C] [IsScalarTower A B C] {I : Submodule B C} : ∀ {n : ℕ}, (hn : n ≠ 0) → (I ^ n).restrictScalars A = I.restrictScalars A ^ n | 1, _ => by simp [Submodule.pow_one] | n + 2, _ => by simp [Submodule.pow_succ (n := n + 1), restrictScalars_mul, restrictScalars_pow n.succ_ne_zero] end Module variable {ι : Sort uι} variable {R : Type u} [CommSemiring R] section AlgebraSemiring variable {A : Type v} [Semiring A] [Algebra R A] variable (S T : Set A) {M N P Q : Submodule R A} {m n : A} theorem one_eq_range : (1 : Submodule R A) = LinearMap.range (Algebra.linearMap R A) := by rw [one_eq_span, LinearMap.span_singleton_eq_range, LinearMap.toSpanSingleton_eq_algebra_linearMap] theorem algebraMap_mem (r : R) : algebraMap R A r ∈ (1 : Submodule R A) := by simp [one_eq_range] @[simp] theorem mem_one {x : A} : x ∈ (1 : Submodule R A) ↔ ∃ y, algebraMap R A y = x := by simp [one_eq_range] protected theorem map_one {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') : map f.toLinearMap (1 : Submodule R A) = 1 := by ext simp @[simp] theorem map_op_one : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (1 : Submodule R A) = 1 := by ext x induction x simp @[simp] theorem comap_op_one : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (1 : Submodule R Aᵐᵒᵖ) = 1 := by ext simp @[simp] theorem map_unop_one : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (1 : Submodule R Aᵐᵒᵖ) = 1 := by rw [← comap_equiv_eq_map_symm, comap_op_one] @[simp] theorem comap_unop_one : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (1 : Submodule R A) = 1 := by rw [← map_equiv_eq_comap_symm, map_op_one] theorem mul_eq_map₂ : M * N = map₂ (LinearMap.mul R A) M N := le_antisymm (mul_le.mpr fun _m hm _n ↦ apply_mem_map₂ _ hm) (map₂_le.mpr fun _m hm _n ↦ mul_mem_mul hm) variable (R M N) theorem span_mul_span : span R S * span R T = span R (S * T) := by rw [mul_eq_map₂]; apply map₂_span_span lemma mul_def : M * N = span R (M * N : Set A) := by simp [← span_mul_span] variable {R} (P Q) protected theorem mul_one : M * 1 = M := by conv_lhs => rw [one_eq_span, ← span_eq M] rw [span_mul_span] simp protected theorem map_mul {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') : map f.toLinearMap (M * N) = map f.toLinearMap M * map f.toLinearMap N := calc map f.toLinearMap (M * N) = ⨆ i : M, (N.map (LinearMap.mul R A i)).map f.toLinearMap := by rw [mul_eq_map₂]; apply map_iSup _ = map f.toLinearMap M * map f.toLinearMap N := by rw [mul_eq_map₂] apply congr_arg sSup ext S constructor <;> rintro ⟨y, hy⟩ · use ⟨f y, mem_map.mpr ⟨y.1, y.2, rfl⟩⟩ refine Eq.trans ?_ hy ext simp · obtain ⟨y', hy', fy_eq⟩ := mem_map.mp y.2 use ⟨y', hy'⟩ refine Eq.trans ?_ hy rw [f.toLinearMap_apply] at fy_eq ext simp [fy_eq] theorem map_op_mul : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (M * N) = map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) N * map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) M := by apply le_antisymm · simp_rw [map_le_iff_le_comap] refine mul_le.2 fun m hm n hn => ?_ rw [mem_comap, map_equiv_eq_comap_symm, map_equiv_eq_comap_symm] show op n * op m ∈ _ exact mul_mem_mul hn hm · refine mul_le.2 (MulOpposite.rec' fun m hm => MulOpposite.rec' fun n hn => ?_) rw [Submodule.mem_map_equiv] at hm hn ⊢ exact mul_mem_mul hn hm theorem comap_unop_mul : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (M * N) = comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) N * comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) M := by simp_rw [← map_equiv_eq_comap_symm, map_op_mul] theorem map_unop_mul (M N : Submodule R Aᵐᵒᵖ) : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (M * N) = map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) N * map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) M := have : Function.Injective (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) := LinearEquiv.injective _ map_injective_of_injective this <| by rw [← map_comp, map_op_mul, ← map_comp, ← map_comp, LinearEquiv.comp_coe, LinearEquiv.symm_trans_self, LinearEquiv.refl_toLinearMap, map_id, map_id, map_id] theorem comap_op_mul (M N : Submodule R Aᵐᵒᵖ) : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (M * N) = comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) N * comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) M := by simp_rw [comap_equiv_eq_map_symm, map_unop_mul] section variable {α : Type*} [Monoid α] [DistribMulAction α A] [SMulCommClass α R A] instance [IsScalarTower α A A] : IsScalarTower α (Submodule R A) (Submodule R A) where smul_assoc a S T := by rw [← S.span_eq, ← T.span_eq, smul_span, smul_eq_mul, smul_eq_mul, span_mul_span, span_mul_span, smul_span, smul_mul_assoc] instance [SMulCommClass α A A] : SMulCommClass α (Submodule R A) (Submodule R A) where smul_comm a S T := by rw [← S.span_eq, ← T.span_eq, smul_span, smul_eq_mul, smul_eq_mul, span_mul_span, span_mul_span, smul_span, mul_smul_comm] instance [SMulCommClass A α A] : SMulCommClass (Submodule R A) α (Submodule R A) := have := SMulCommClass.symm A α A; .symm .. end section open Pointwise /-- `Submodule.pointwiseNeg` distributes over multiplication. This is available as an instance in the `Pointwise` locale. -/ protected def hasDistribPointwiseNeg {A} [Ring A] [Algebra R A] : HasDistribNeg (Submodule R A) := toAddSubmonoid_injective.hasDistribNeg _ neg_toAddSubmonoid mul_toAddSubmonoid scoped[Pointwise] attribute [instance] Submodule.hasDistribPointwiseNeg end section DecidableEq theorem mem_span_mul_finite_of_mem_span_mul {R A} [Semiring R] [AddCommMonoid A] [Mul A] [Module R A] {S : Set A} {S' : Set A} {x : A} (hx : x ∈ span R (S * S')) : ∃ T T' : Finset A, ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ x ∈ span R (T * T' : Set A) := by classical obtain ⟨U, h, hU⟩ := mem_span_finite_of_mem_span hx obtain ⟨T, T', hS, hS', h⟩ := Finset.subset_mul h use T, T', hS, hS' have h' : (U : Set A) ⊆ T * T' := by assumption_mod_cast have h'' := span_mono h' hU assumption end DecidableEq theorem mul_eq_span_mul_set (s t : Submodule R A) : s * t = span R ((s : Set A) * (t : Set A)) := by rw [mul_eq_map₂]; exact map₂_eq_span_image2 _ s t theorem mem_span_mul_finite_of_mem_mul {P Q : Submodule R A} {x : A} (hx : x ∈ P * Q) : ∃ T T' : Finset A, (T : Set A) ⊆ P ∧ (T' : Set A) ⊆ Q ∧ x ∈ span R (T * T' : Set A) := Submodule.mem_span_mul_finite_of_mem_span_mul (by rwa [← Submodule.span_eq P, ← Submodule.span_eq Q, Submodule.span_mul_span] at hx) variable {M N P} theorem mem_span_singleton_mul {x y : A} : x ∈ span R {y} * P ↔ ∃ z ∈ P, y * z = x := by simp_rw [mul_eq_map₂, map₂_span_singleton_eq_map, mem_map, LinearMap.mul_apply_apply] theorem mem_mul_span_singleton {x y : A} : x ∈ P * span R {y} ↔ ∃ z ∈ P, z * y = x := by simp_rw [mul_eq_map₂, map₂_span_singleton_eq_map_flip, mem_map, LinearMap.flip_apply, LinearMap.mul_apply_apply] lemma span_singleton_mul {x : A} {p : Submodule R A} : Submodule.span R {x} * p = x • p := ext fun _ ↦ mem_span_singleton_mul lemma mem_smul_iff_inv_mul_mem {S} [DivisionSemiring S] [Algebra R S] {x : S} {p : Submodule R S} {y : S} (hx : x ≠ 0) : y ∈ x • p ↔ x⁻¹ * y ∈ p := by constructor · rintro ⟨a, ha : a ∈ p, rfl⟩; simpa [inv_mul_cancel_left₀ hx] · exact fun h ↦ ⟨_, h, by simp [mul_inv_cancel_left₀ hx]⟩ lemma mul_mem_smul_iff {S} [CommRing S] [Algebra R S] {x : S} {p : Submodule R S} {y : S} (hx : x ∈ nonZeroDivisors S) : x * y ∈ x • p ↔ y ∈ p := by simp [mem_smul_pointwise_iff_exists, mul_cancel_left_mem_nonZeroDivisors hx] variable (M N) in theorem mul_smul_mul_eq_smul_mul_smul (x y : R) : (x * y) • (M * N) = (x • M) * (y • N) := by ext refine ⟨?_, fun hx ↦ Submodule.mul_induction_on hx ?_ fun _ _ hx hy ↦ Submodule.add_mem _ hx hy⟩ · rintro ⟨_, hx, rfl⟩ rw [DistribMulAction.toLinearMap_apply] refine Submodule.mul_induction_on hx (fun m hm n hn ↦ ?_) (fun _ _ hn hm ↦ ?_) · rw [mul_smul_mul_comm] exact mul_mem_mul (smul_mem_pointwise_smul m x M hm) (smul_mem_pointwise_smul n y N hn) · rw [smul_add] exact Submodule.add_mem _ hn hm · rintro _ ⟨m, hm, rfl⟩ _ ⟨n, hn, rfl⟩ simp_rw [DistribMulAction.toLinearMap_apply, smul_mul_smul_comm] exact smul_mem_pointwise_smul _ _ _ (mul_mem_mul hm hn) /-- Sub-R-modules of an R-algebra form an idempotent semiring. -/ instance idemSemiring : IdemSemiring (Submodule R A) where __ := instNonUnitalSemiring one_mul := Submodule.one_mul mul_one := Submodule.mul_one bot_le _ := bot_le variable (M) theorem span_pow (s : Set A) : ∀ n : ℕ, span R s ^ n = span R (s ^ n) | 0 => by rw [pow_zero, pow_zero, one_eq_span_one_set] | n + 1 => by rw [pow_succ, pow_succ, span_pow s n, span_mul_span] theorem pow_eq_span_pow_set (n : ℕ) : M ^ n = span R ((M : Set A) ^ n) := by rw [← span_pow, span_eq] /-- Dependent version of `Submodule.pow_induction_on_left`. -/ @[elab_as_elim] protected theorem pow_induction_on_left' {C : ∀ (n : ℕ) (x), x ∈ M ^ n → Prop} (algebraMap : ∀ r : R, C 0 (algebraMap _ _ r) (algebraMap_mem r)) (add : ∀ x y i hx hy, C i x hx → C i y hy → C i (x + y) (add_mem ‹_› ‹_›)) (mem_mul : ∀ m (hm : m ∈ M), ∀ (i x hx), C i x hx → C i.succ (m * x) ((pow_succ' M i).symm ▸ (mul_mem_mul hm hx))) {n : ℕ} {x : A} (hx : x ∈ M ^ n) : C n x hx := by induction n generalizing x with | zero => rw [pow_zero] at hx obtain ⟨r, rfl⟩ := mem_one.mp hx exact algebraMap r | succ n n_ih => revert hx simp_rw [pow_succ'] exact fun hx ↦ Submodule.mul_induction_on' (fun m hm x ih => mem_mul _ hm _ _ _ (n_ih ih)) (fun x hx y hy Cx Cy => add _ _ _ _ _ Cx Cy) hx /-- Dependent version of `Submodule.pow_induction_on_right`. -/ @[elab_as_elim] protected theorem pow_induction_on_right' {C : ∀ (n : ℕ) (x), x ∈ M ^ n → Prop} (algebraMap : ∀ r : R, C 0 (algebraMap _ _ r) (algebraMap_mem r)) (add : ∀ x y i hx hy, C i x hx → C i y hy → C i (x + y) (add_mem ‹_› ‹_›)) (mul_mem : ∀ i x hx, C i x hx → ∀ m (hm : m ∈ M), C i.succ (x * m) (mul_mem_mul hx hm)) {n : ℕ} {x : A} (hx : x ∈ M ^ n) : C n x hx := by induction n generalizing x with | zero => rw [pow_zero] at hx obtain ⟨r, rfl⟩ := mem_one.mp hx exact algebraMap r | succ n n_ih => revert hx simp_rw [pow_succ] exact fun hx ↦ Submodule.mul_induction_on' (fun m hm x ih => mul_mem _ _ hm (n_ih _) _ ih) (fun x hx y hy Cx Cy => add _ _ _ _ _ Cx Cy) hx /-- To show a property on elements of `M ^ n` holds, it suffices to show that it holds for scalars, is closed under addition, and holds for `m * x` where `m ∈ M` and it holds for `x` -/ @[elab_as_elim] protected theorem pow_induction_on_left {C : A → Prop} (hr : ∀ r : R, C (algebraMap _ _ r)) (hadd : ∀ x y, C x → C y → C (x + y)) (hmul : ∀ m ∈ M, ∀ (x), C x → C (m * x)) {x : A} {n : ℕ} (hx : x ∈ M ^ n) : C x := Submodule.pow_induction_on_left' M (C := fun _ a _ => C a) hr (fun x y _i _hx _hy => hadd x y) (fun _m hm _i _x _hx => hmul _ hm _) hx /-- To show a property on elements of `M ^ n` holds, it suffices to show that it holds for scalars, is closed under addition, and holds for `x * m` where `m ∈ M` and it holds for `x` -/ @[elab_as_elim] protected theorem pow_induction_on_right {C : A → Prop} (hr : ∀ r : R, C (algebraMap _ _ r)) (hadd : ∀ x y, C x → C y → C (x + y)) (hmul : ∀ x, C x → ∀ m ∈ M, C (x * m)) {x : A} {n : ℕ} (hx : x ∈ M ^ n) : C x := Submodule.pow_induction_on_right' (M := M) (C := fun _ a _ => C a) hr (fun x y _i _hx _hy => hadd x y) (fun _i _x _hx => hmul _) hx /-- `Submonoid.map` as a `RingHom`, when applied to an `AlgHom`. -/ @[simps] def mapHom {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') : Submodule R A →+* Submodule R A' where toFun := map f.toLinearMap map_zero' := Submodule.map_bot _ map_add' := (Submodule.map_sup · · _) map_one' := Submodule.map_one _ map_mul' := (Submodule.map_mul · · _) theorem mapHom_id : mapHom (.id R A) = .id _ := RingHom.ext map_id /-- The ring of submodules of the opposite algebra is isomorphic to the opposite ring of submodules. -/ @[simps apply symm_apply]
def equivOpposite : Submodule R Aᵐᵒᵖ ≃+* (Submodule R A)ᵐᵒᵖ where toFun p := op <| p.comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) invFun p := p.unop.comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A)
Mathlib/Algebra/Algebra/Operations.lean
652
654
/- 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, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import Mathlib.Analysis.SpecialFunctions.Pow.Real /-! # Power function on `ℝ≥0` and `ℝ≥0∞` We construct the power functions `x ^ y` where * `x` is a nonnegative real number and `y` is a real number; * `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable section open Real NNReal ENNReal ComplexConjugate Finset Function Set namespace NNReal variable {x : ℝ≥0} {w y z : ℝ} /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, Real.rpow_nonneg x.2 y⟩ noncomputable instance : Pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] theorem rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] theorem coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] theorem rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := NNReal.eq <| Real.rpow_zero _ @[simp] theorem rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by rw [← NNReal.coe_inj, coe_rpow, ← NNReal.coe_eq_zero] exact Real.rpow_eq_zero_iff_of_nonneg x.2 lemma rpow_eq_zero (hy : y ≠ 0) : x ^ y = 0 ↔ x = 0 := by simp [hy] @[simp] theorem zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := NNReal.eq <| Real.zero_rpow h @[simp] theorem rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := NNReal.eq <| Real.rpow_one _ lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ (-y) = (x ^ y)⁻¹ := NNReal.eq <| Real.rpow_neg x.2 _ @[simp, norm_cast] lemma rpow_natCast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := NNReal.eq <| by simpa only [coe_rpow, coe_pow] using Real.rpow_natCast x n @[simp, norm_cast] lemma rpow_intCast (x : ℝ≥0) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by cases n <;> simp only [Int.ofNat_eq_coe, Int.cast_natCast, rpow_natCast, zpow_natCast, Int.cast_negSucc, rpow_neg, zpow_negSucc] @[simp] theorem one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := NNReal.eq <| Real.one_rpow _ theorem rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := NNReal.eq <| Real.rpow_add ((NNReal.coe_pos.trans pos_iff_ne_zero).mpr hx) _ _ theorem rpow_add' (h : y + z ≠ 0) (x : ℝ≥0) : x ^ (y + z) = x ^ y * x ^ z := NNReal.eq <| Real.rpow_add' x.2 h lemma rpow_add_intCast (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_intCast (mod_cast hx) _ _ lemma rpow_add_natCast (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_natCast (mod_cast hx) _ _ lemma rpow_sub_intCast (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_intCast (mod_cast hx) _ _ lemma rpow_sub_natCast (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_natCast (mod_cast hx) _ _ lemma rpow_add_intCast' {n : ℤ} (h : y + n ≠ 0) (x : ℝ≥0) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_intCast' (mod_cast x.2) h lemma rpow_add_natCast' {n : ℕ} (h : y + n ≠ 0) (x : ℝ≥0) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_natCast' (mod_cast x.2) h lemma rpow_sub_intCast' {n : ℤ} (h : y - n ≠ 0) (x : ℝ≥0) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_intCast' (mod_cast x.2) h lemma rpow_sub_natCast' {n : ℕ} (h : y - n ≠ 0) (x : ℝ≥0) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_natCast' (mod_cast x.2) h lemma rpow_add_one (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_natCast hx y 1 lemma rpow_sub_one (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_natCast hx y 1 lemma rpow_add_one' (h : y + 1 ≠ 0) (x : ℝ≥0) : x ^ (y + 1) = x ^ y * x := by rw [rpow_add' h, rpow_one] lemma rpow_one_add' (h : 1 + y ≠ 0) (x : ℝ≥0) : x ^ (1 + y) = x * x ^ y := by rw [rpow_add' h, rpow_one] theorem rpow_add_of_nonneg (x : ℝ≥0) {y z : ℝ} (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := by ext; exact Real.rpow_add_of_nonneg x.2 hy hz /-- Variant of `NNReal.rpow_add'` that avoids having to prove `y + z = w` twice. -/ lemma rpow_of_add_eq (x : ℝ≥0) (hw : w ≠ 0) (h : y + z = w) : x ^ w = x ^ y * x ^ z := by rw [← h, rpow_add']; rwa [h] theorem rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := NNReal.eq <| Real.rpow_mul x.2 y z lemma rpow_natCast_mul (x : ℝ≥0) (n : ℕ) (z : ℝ) : x ^ (n * z) = (x ^ n) ^ z := by rw [rpow_mul, rpow_natCast] lemma rpow_mul_natCast (x : ℝ≥0) (y : ℝ) (n : ℕ) : x ^ (y * n) = (x ^ y) ^ n := by rw [rpow_mul, rpow_natCast] lemma rpow_intCast_mul (x : ℝ≥0) (n : ℤ) (z : ℝ) : x ^ (n * z) = (x ^ n) ^ z := by rw [rpow_mul, rpow_intCast] lemma rpow_mul_intCast (x : ℝ≥0) (y : ℝ) (n : ℤ) : x ^ (y * n) = (x ^ y) ^ n := by rw [rpow_mul, rpow_intCast] theorem rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x⁻¹ := by simp [rpow_neg] theorem rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := NNReal.eq <| Real.rpow_sub ((NNReal.coe_pos.trans pos_iff_ne_zero).mpr hx) y z theorem rpow_sub' (h : y - z ≠ 0) (x : ℝ≥0) : x ^ (y - z) = x ^ y / x ^ z := NNReal.eq <| Real.rpow_sub' x.2 h lemma rpow_sub_one' (h : y - 1 ≠ 0) (x : ℝ≥0) : x ^ (y - 1) = x ^ y / x := by rw [rpow_sub' h, rpow_one] lemma rpow_one_sub' (h : 1 - y ≠ 0) (x : ℝ≥0) : x ^ (1 - y) = x / x ^ y := by rw [rpow_sub' h, rpow_one] theorem rpow_inv_rpow_self {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ (1 / y) = x := by field_simp [← rpow_mul] theorem rpow_self_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ (1 / y)) ^ y = x := by field_simp [← rpow_mul] theorem inv_rpow (x : ℝ≥0) (y : ℝ) : x⁻¹ ^ y = (x ^ y)⁻¹ := NNReal.eq <| Real.inv_rpow x.2 y theorem div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := NNReal.eq <| Real.div_rpow x.2 y.2 z theorem sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1 / (2 : ℝ)) := by refine NNReal.eq ?_ push_cast exact Real.sqrt_eq_rpow x.1 @[simp] lemma rpow_ofNat (x : ℝ≥0) (n : ℕ) [n.AtLeastTwo] : x ^ (ofNat(n) : ℝ) = x ^ (OfNat.ofNat n : ℕ) := rpow_natCast x n theorem rpow_two (x : ℝ≥0) : x ^ (2 : ℝ) = x ^ 2 := rpow_ofNat x 2 theorem mul_rpow {x y : ℝ≥0} {z : ℝ} : (x * y) ^ z = x ^ z * y ^ z := NNReal.eq <| Real.mul_rpow x.2 y.2 /-- `rpow` as a `MonoidHom` -/ @[simps] def rpowMonoidHom (r : ℝ) : ℝ≥0 →* ℝ≥0 where toFun := (· ^ r) map_one' := one_rpow _ map_mul' _x _y := mul_rpow /-- `rpow` variant of `List.prod_map_pow` for `ℝ≥0` -/ theorem list_prod_map_rpow (l : List ℝ≥0) (r : ℝ) : (l.map (· ^ r)).prod = l.prod ^ r := l.prod_hom (rpowMonoidHom r) theorem list_prod_map_rpow' {ι} (l : List ι) (f : ι → ℝ≥0) (r : ℝ) : (l.map (f · ^ r)).prod = (l.map f).prod ^ r := by rw [← list_prod_map_rpow, List.map_map]; rfl /-- `rpow` version of `Multiset.prod_map_pow` for `ℝ≥0`. -/ lemma multiset_prod_map_rpow {ι} (s : Multiset ι) (f : ι → ℝ≥0) (r : ℝ) : (s.map (f · ^ r)).prod = (s.map f).prod ^ r := s.prod_hom' (rpowMonoidHom r) _ /-- `rpow` version of `Finset.prod_pow` for `ℝ≥0`. -/ lemma finset_prod_rpow {ι} (s : Finset ι) (f : ι → ℝ≥0) (r : ℝ) : (∏ i ∈ s, f i ^ r) = (∏ i ∈ s, f i) ^ r := multiset_prod_map_rpow _ _ _ -- note: these don't really belong here, but they're much easier to prove in terms of the above section Real /-- `rpow` version of `List.prod_map_pow` for `Real`. -/ theorem _root_.Real.list_prod_map_rpow (l : List ℝ) (hl : ∀ x ∈ l, (0 : ℝ) ≤ x) (r : ℝ) : (l.map (· ^ r)).prod = l.prod ^ r := by lift l to List ℝ≥0 using hl have := congr_arg ((↑) : ℝ≥0 → ℝ) (NNReal.list_prod_map_rpow l r) push_cast at this rw [List.map_map] at this ⊢ exact mod_cast this theorem _root_.Real.list_prod_map_rpow' {ι} (l : List ι) (f : ι → ℝ) (hl : ∀ i ∈ l, (0 : ℝ) ≤ f i) (r : ℝ) : (l.map (f · ^ r)).prod = (l.map f).prod ^ r := by rw [← Real.list_prod_map_rpow (l.map f) _ r, List.map_map] · rfl simpa using hl /-- `rpow` version of `Multiset.prod_map_pow`. -/ theorem _root_.Real.multiset_prod_map_rpow {ι} (s : Multiset ι) (f : ι → ℝ) (hs : ∀ i ∈ s, (0 : ℝ) ≤ f i) (r : ℝ) : (s.map (f · ^ r)).prod = (s.map f).prod ^ r := by induction' s using Quotient.inductionOn with l simpa using Real.list_prod_map_rpow' l f hs r /-- `rpow` version of `Finset.prod_pow`. -/ theorem _root_.Real.finset_prod_rpow {ι} (s : Finset ι) (f : ι → ℝ) (hs : ∀ i ∈ s, 0 ≤ f i) (r : ℝ) : (∏ i ∈ s, f i ^ r) = (∏ i ∈ s, f i) ^ r := Real.multiset_prod_map_rpow s.val f hs r end Real @[gcongr] theorem rpow_le_rpow {x y : ℝ≥0} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := Real.rpow_le_rpow x.2 h₁ h₂ @[gcongr] theorem rpow_lt_rpow {x y : ℝ≥0} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x ^ z < y ^ z := Real.rpow_lt_rpow x.2 h₁ h₂ theorem rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := Real.rpow_lt_rpow_iff x.2 y.2 hz theorem rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := Real.rpow_le_rpow_iff x.2 y.2 hz theorem le_rpow_inv_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ≤ y ^ z⁻¹ ↔ x ^ z ≤ y := by rw [← rpow_le_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz.ne'] theorem rpow_inv_le_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z⁻¹ ≤ y ↔ x ≤ y ^ z := by rw [← rpow_le_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz.ne'] theorem lt_rpow_inv_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x < y ^ z⁻¹ ↔ x ^z < y := by simp only [← not_le, rpow_inv_le_iff hz] theorem rpow_inv_lt_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z⁻¹ < y ↔ x < y ^ z := by simp only [← not_le, le_rpow_inv_iff hz] section variable {y : ℝ≥0} lemma rpow_lt_rpow_of_neg (hx : 0 < x) (hxy : x < y) (hz : z < 0) : y ^ z < x ^ z := Real.rpow_lt_rpow_of_neg hx hxy hz lemma rpow_le_rpow_of_nonpos (hx : 0 < x) (hxy : x ≤ y) (hz : z ≤ 0) : y ^ z ≤ x ^ z := Real.rpow_le_rpow_of_nonpos hx hxy hz lemma rpow_lt_rpow_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z < y ^ z ↔ y < x := Real.rpow_lt_rpow_iff_of_neg hx hy hz lemma rpow_le_rpow_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z ≤ y ^ z ↔ y ≤ x := Real.rpow_le_rpow_iff_of_neg hx hy hz lemma le_rpow_inv_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x ≤ y ^ z⁻¹ ↔ x ^ z ≤ y := Real.le_rpow_inv_iff_of_pos x.2 hy hz lemma rpow_inv_le_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x ^ z⁻¹ ≤ y ↔ x ≤ y ^ z := Real.rpow_inv_le_iff_of_pos x.2 hy hz lemma lt_rpow_inv_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x < y ^ z⁻¹ ↔ x ^ z < y := Real.lt_rpow_inv_iff_of_pos x.2 hy hz lemma rpow_inv_lt_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x ^ z⁻¹ < y ↔ x < y ^ z := Real.rpow_inv_lt_iff_of_pos x.2 hy hz lemma le_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ≤ y ^ z⁻¹ ↔ y ≤ x ^ z := Real.le_rpow_inv_iff_of_neg hx hy hz lemma lt_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x < y ^ z⁻¹ ↔ y < x ^ z := Real.lt_rpow_inv_iff_of_neg hx hy hz lemma rpow_inv_lt_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ < y ↔ y ^ z < x := Real.rpow_inv_lt_iff_of_neg hx hy hz lemma rpow_inv_le_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ ≤ y ↔ y ^ z ≤ x := Real.rpow_inv_le_iff_of_neg hx hy hz end @[gcongr] theorem rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x ^ y < x ^ z := Real.rpow_lt_rpow_of_exponent_lt hx hyz @[gcongr] theorem rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := Real.rpow_le_rpow_of_exponent_le hx hyz theorem rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := Real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz theorem rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := Real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz theorem rpow_pos {p : ℝ} {x : ℝ≥0} (hx_pos : 0 < x) : 0 < x ^ p := by have rpow_pos_of_nonneg : ∀ {p : ℝ}, 0 < p → 0 < x ^ p := by intro p hp_pos rw [← zero_rpow hp_pos.ne'] exact rpow_lt_rpow hx_pos hp_pos rcases lt_trichotomy (0 : ℝ) p with (hp_pos | rfl | hp_neg) · exact rpow_pos_of_nonneg hp_pos · simp only [zero_lt_one, rpow_zero] · rw [← neg_neg p, rpow_neg, inv_pos] exact rpow_pos_of_nonneg (neg_pos.mpr hp_neg) theorem rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x ^ z < 1 := Real.rpow_lt_one (coe_nonneg x) hx1 hz theorem rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := Real.rpow_le_one x.2 hx2 hz theorem rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := Real.rpow_lt_one_of_one_lt_of_neg hx hz theorem rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x ^ z ≤ 1 := Real.rpow_le_one_of_one_le_of_nonpos hx hz theorem one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := Real.one_lt_rpow hx hz theorem one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x ^ z := Real.one_le_rpow h h₁ theorem one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := Real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz theorem one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x ^ z := Real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz theorem rpow_le_self_of_le_one {x : ℝ≥0} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := by rcases eq_bot_or_bot_lt x with (rfl | (h : 0 < x)) · have : z ≠ 0 := by linarith simp [this] nth_rw 2 [← NNReal.rpow_one x] exact NNReal.rpow_le_rpow_of_exponent_ge h hx h_one_le theorem rpow_left_injective {x : ℝ} (hx : x ≠ 0) : Function.Injective fun y : ℝ≥0 => y ^ x := fun y z hyz => by simpa only [rpow_inv_rpow_self hx] using congr_arg (fun y => y ^ (1 / x)) hyz theorem rpow_eq_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y := (rpow_left_injective hz).eq_iff theorem rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : Function.Surjective fun y : ℝ≥0 => y ^ x := fun y => ⟨y ^ x⁻¹, by simp_rw [← rpow_mul, inv_mul_cancel₀ hx, rpow_one]⟩ theorem rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : Function.Bijective fun y : ℝ≥0 => y ^ x := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ theorem eq_rpow_inv_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x = y ^ z⁻¹ ↔ x ^ z = y := by rw [← rpow_eq_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz] theorem rpow_inv_eq_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z⁻¹ = y ↔ x = y ^ z := by rw [← rpow_eq_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz] @[simp] lemma rpow_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ y⁻¹ = x := by rw [← rpow_mul, mul_inv_cancel₀ hy, rpow_one] @[simp] lemma rpow_inv_rpow {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y⁻¹) ^ y = x := by rw [← rpow_mul, inv_mul_cancel₀ hy, rpow_one] theorem pow_rpow_inv_natCast (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by rw [← NNReal.coe_inj, coe_rpow, NNReal.coe_pow] exact Real.pow_rpow_inv_natCast x.2 hn theorem rpow_inv_natCast_pow (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by rw [← NNReal.coe_inj, NNReal.coe_pow, coe_rpow] exact Real.rpow_inv_natCast_pow x.2 hn theorem _root_.Real.toNNReal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : Real.toNNReal (x ^ y) = Real.toNNReal x ^ y := by nth_rw 1 [← Real.coe_toNNReal x hx] rw [← NNReal.coe_rpow, Real.toNNReal_coe] theorem strictMono_rpow_of_pos {z : ℝ} (h : 0 < z) : StrictMono fun x : ℝ≥0 => x ^ z := fun x y hxy => by simp only [NNReal.rpow_lt_rpow hxy h, coe_lt_coe] theorem monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : Monotone fun x : ℝ≥0 => x ^ z := h.eq_or_lt.elim (fun h0 => h0 ▸ by simp only [rpow_zero, monotone_const]) fun h0 => (strictMono_rpow_of_pos h0).monotone /-- Bundles `fun x : ℝ≥0 => x ^ y` into an order isomorphism when `y : ℝ` is positive, where the inverse is `fun x : ℝ≥0 => x ^ (1 / y)`. -/ @[simps! apply] def orderIsoRpow (y : ℝ) (hy : 0 < y) : ℝ≥0 ≃o ℝ≥0 := (strictMono_rpow_of_pos hy).orderIsoOfRightInverse (fun x => x ^ y) (fun x => x ^ (1 / y)) fun x => by dsimp rw [← rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] theorem orderIsoRpow_symm_eq (y : ℝ) (hy : 0 < y) : (orderIsoRpow y hy).symm = orderIsoRpow (1 / y) (one_div_pos.2 hy) := by simp only [orderIsoRpow, one_div_one_div]; rfl theorem _root_.Real.nnnorm_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : ‖x ^ y‖₊ = ‖x‖₊ ^ y := by ext; exact Real.norm_rpow_of_nonneg hx end NNReal namespace ENNReal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞ | some x, y => if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none, y => if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : Pow ℝ≥0∞ ℝ := ⟨rpow⟩ @[simp] theorem rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] theorem rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 := by cases x <;> · dsimp only [(· ^ ·), Pow.pow, rpow] simp [lt_irrefl] theorem top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] theorem top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] theorem top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] theorem zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 := by rw [← ENNReal.coe_zero, ← ENNReal.some_eq_coe] dsimp only [(· ^ ·), rpow, Pow.pow] simp [h, asymm h, ne_of_gt h] @[simp] theorem zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ := by rw [← ENNReal.coe_zero, ← ENNReal.some_eq_coe] dsimp only [(· ^ ·), rpow, Pow.pow] simp [h, ne_of_gt h] theorem zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := by rcases lt_trichotomy (0 : ℝ) y with (H | rfl | H) · simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] · simp [lt_irrefl] · simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] @[simp] theorem zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * (0 : ℝ≥0∞) ^ y = (0 : ℝ≥0∞) ^ y := by rw [zero_rpow_def] split_ifs exacts [zero_mul _, one_mul _, top_mul_top] @[norm_cast] theorem coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (↑(x ^ y) : ℝ≥0∞) = x ^ y := by rw [← ENNReal.some_eq_coe] dsimp only [(· ^ ·), Pow.pow, rpow] simp [h] @[norm_cast] theorem coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : ↑(x ^ y) = (x : ℝ≥0∞) ^ y := by by_cases hx : x = 0 · rcases le_iff_eq_or_lt.1 h with (H | H) · simp [hx, H.symm] · simp [hx, zero_rpow_of_pos H, NNReal.zero_rpow (ne_of_gt H)] · exact coe_rpow_of_ne_zero hx _ theorem coe_rpow_def (x : ℝ≥0) (y : ℝ) : (x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else ↑(x ^ y) := rfl theorem rpow_ofNNReal {M : ℝ≥0} {P : ℝ} (hP : 0 ≤ P) : (M : ℝ≥0∞) ^ P = ↑(M ^ P) := by rw [ENNReal.coe_rpow_of_nonneg _ hP, ← ENNReal.rpow_eq_pow] @[simp] theorem rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x := by cases x · exact dif_pos zero_lt_one · change ite _ _ _ = _ simp only [NNReal.rpow_one, some_eq_coe, ite_eq_right_iff, top_ne_coe, and_imp] exact fun _ => zero_le_one.not_lt @[simp] theorem one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 := by rw [← coe_one, ← coe_rpow_of_ne_zero one_ne_zero] simp @[simp] theorem rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ 0 < y ∨ x = ⊤ ∧ y < 0 := by cases x with | top => rcases lt_trichotomy y 0 with (H | H | H) <;> simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (H | H | H) <;> simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] · simp [← coe_rpow_of_ne_zero h, h] lemma rpow_eq_zero_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = 0 ↔ x = 0 := by simp [hy, hy.not_lt] @[simp] theorem rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = ⊤ ↔ x = 0 ∧ y < 0 ∨ x = ⊤ ∧ 0 < y := by cases x with | top => rcases lt_trichotomy y 0 with (H | H | H) <;> simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (H | H | H) <;> simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] · simp [← coe_rpow_of_ne_zero h, h] theorem rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := by simp [rpow_eq_top_iff, hy, asymm hy] lemma rpow_lt_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y < ∞ ↔ x < ∞ := by simp only [lt_top_iff_ne_top, Ne, rpow_eq_top_iff_of_pos hy] theorem rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := by rw [ENNReal.rpow_eq_top_iff] rintro (h|h) · exfalso rw [lt_iff_not_ge] at h exact h.right hy0 · exact h.left theorem rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (ENNReal.rpow_eq_top_of_nonneg x hy0) h theorem rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := lt_top_iff_ne_top.mpr (ENNReal.rpow_ne_top_of_nonneg hy0 h) theorem rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := by cases x with | top => exact (h'x rfl).elim | coe x => have : x ≠ 0 := fun h => by simp [h] at hx simp [← coe_rpow_of_ne_zero this, NNReal.rpow_add this] theorem rpow_add_of_nonneg {x : ℝ≥0∞} (y z : ℝ) (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := by induction x using recTopCoe · rcases hy.eq_or_lt with rfl|hy · rw [rpow_zero, one_mul, zero_add] rcases hz.eq_or_lt with rfl|hz · rw [rpow_zero, mul_one, add_zero] simp [top_rpow_of_pos, hy, hz, add_pos hy hz] simp [← coe_rpow_of_nonneg, hy, hz, add_nonneg hy hz, NNReal.rpow_add_of_nonneg _ hy hz] theorem rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ (-y) = (x ^ y)⁻¹ := by cases x with | top => rcases lt_trichotomy y 0 with (H | H | H) <;> simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (H | H | H) <;> simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] · have A : x ^ y ≠ 0 := by simp [h] simp [← coe_rpow_of_ne_zero h, ← coe_inv A, NNReal.rpow_neg] theorem rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv] theorem rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x⁻¹ := by simp [rpow_neg] theorem rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by cases x with | top => rcases lt_trichotomy y 0 with (Hy | Hy | Hy) <;> rcases lt_trichotomy z 0 with (Hz | Hz | Hz) <;> simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (Hy | Hy | Hy) <;> rcases lt_trichotomy z 0 with (Hz | Hz | Hz) <;> simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] · have : x ^ y ≠ 0 := by simp [h] simp [← coe_rpow_of_ne_zero, h, this, NNReal.rpow_mul] @[simp, norm_cast] theorem rpow_natCast (x : ℝ≥0∞) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by cases x · cases n <;> simp [top_rpow_of_pos (Nat.cast_add_one_pos _), top_pow (Nat.succ_ne_zero _)] · simp [← coe_rpow_of_nonneg _ (Nat.cast_nonneg n)] @[simp] lemma rpow_ofNat (x : ℝ≥0∞) (n : ℕ) [n.AtLeastTwo] : x ^ (ofNat(n) : ℝ) = x ^ (OfNat.ofNat n) := rpow_natCast x n @[simp, norm_cast] lemma rpow_intCast (x : ℝ≥0∞) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by cases n <;> simp only [Int.ofNat_eq_coe, Int.cast_natCast, rpow_natCast, zpow_natCast, Int.cast_negSucc, rpow_neg, zpow_negSucc] theorem rpow_two (x : ℝ≥0∞) : x ^ (2 : ℝ) = x ^ 2 := rpow_ofNat x 2 theorem mul_rpow_eq_ite (x y : ℝ≥0∞) (z : ℝ) : (x * y) ^ z = if (x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0) ∧ z < 0 then ⊤ else x ^ z * y ^ z := by rcases eq_or_ne z 0 with (rfl | hz); · simp replace hz := hz.lt_or_lt wlog hxy : x ≤ y · convert this y x z hz (le_of_not_le hxy) using 2 <;> simp only [mul_comm, and_comm, or_comm] rcases eq_or_ne x 0 with (rfl | hx0) · induction y <;> rcases hz with hz | hz <;> simp [*, hz.not_lt] rcases eq_or_ne y 0 with (rfl | hy0) · exact (hx0 (bot_unique hxy)).elim induction x · rcases hz with hz | hz <;> simp [hz, top_unique hxy] induction y · rw [ne_eq, coe_eq_zero] at hx0 rcases hz with hz | hz <;> simp [*] simp only [*, if_false] norm_cast at * rw [← coe_rpow_of_ne_zero (mul_ne_zero hx0 hy0), NNReal.mul_rpow] norm_cast theorem mul_rpow_of_ne_top {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := by simp [*, mul_rpow_eq_ite] @[norm_cast] theorem coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ℝ≥0∞) * y) ^ z = (x : ℝ≥0∞) ^ z * (y : ℝ≥0∞) ^ z := mul_rpow_of_ne_top coe_ne_top coe_ne_top z theorem prod_coe_rpow {ι} (s : Finset ι) (f : ι → ℝ≥0) (r : ℝ) : ∏ i ∈ s, (f i : ℝ≥0∞) ^ r = ((∏ i ∈ s, f i : ℝ≥0) : ℝ≥0∞) ^ r := by classical induction s using Finset.induction with | empty => simp | insert _ _ hi ih => simp_rw [prod_insert hi, ih, ← coe_mul_rpow, coe_mul] theorem mul_rpow_of_ne_zero {x y : ℝ≥0∞} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := by simp [*, mul_rpow_eq_ite] theorem mul_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := by simp [hz.not_lt, mul_rpow_eq_ite] theorem prod_rpow_of_ne_top {ι} {s : Finset ι} {f : ι → ℝ≥0∞} (hf : ∀ i ∈ s, f i ≠ ∞) (r : ℝ) : ∏ i ∈ s, f i ^ r = (∏ i ∈ s, f i) ^ r := by classical induction s using Finset.induction with | empty => simp | insert i s hi ih => have h2f : ∀ i ∈ s, f i ≠ ∞ := fun i hi ↦ hf i <| mem_insert_of_mem hi rw [prod_insert hi, prod_insert hi, ih h2f, ← mul_rpow_of_ne_top <| hf i <| mem_insert_self ..] apply prod_ne_top h2f theorem prod_rpow_of_nonneg {ι} {s : Finset ι} {f : ι → ℝ≥0∞} {r : ℝ} (hr : 0 ≤ r) : ∏ i ∈ s, f i ^ r = (∏ i ∈ s, f i) ^ r := by classical induction s using Finset.induction with | empty => simp | insert _ _ hi ih => simp_rw [prod_insert hi, ih, ← mul_rpow_of_nonneg _ _ hr] theorem inv_rpow (x : ℝ≥0∞) (y : ℝ) : x⁻¹ ^ y = (x ^ y)⁻¹ := by rcases eq_or_ne y 0 with (rfl | hy); · simp only [rpow_zero, inv_one] replace hy := hy.lt_or_lt rcases eq_or_ne x 0 with (rfl | h0); · cases hy <;> simp [*] rcases eq_or_ne x ⊤ with (rfl | h_top); · cases hy <;> simp [*] apply ENNReal.eq_inv_of_mul_eq_one_left rw [← mul_rpow_of_ne_zero (ENNReal.inv_ne_zero.2 h_top) h0, ENNReal.inv_mul_cancel h0 h_top, one_rpow] theorem div_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x / y) ^ z = x ^ z / y ^ z := by rw [div_eq_mul_inv, mul_rpow_of_nonneg _ _ hz, inv_rpow, div_eq_mul_inv] theorem strictMono_rpow_of_pos {z : ℝ} (h : 0 < z) : StrictMono fun x : ℝ≥0∞ => x ^ z := by intro x y hxy lift x to ℝ≥0 using ne_top_of_lt hxy rcases eq_or_ne y ∞ with (rfl | hy) · simp only [top_rpow_of_pos h, ← coe_rpow_of_nonneg _ h.le, coe_lt_top] · lift y to ℝ≥0 using hy simp only [← coe_rpow_of_nonneg _ h.le, NNReal.rpow_lt_rpow (coe_lt_coe.1 hxy) h, coe_lt_coe] theorem monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : Monotone fun x : ℝ≥0∞ => x ^ z := h.eq_or_lt.elim (fun h0 => h0 ▸ by simp only [rpow_zero, monotone_const]) fun h0 => (strictMono_rpow_of_pos h0).monotone /-- Bundles `fun x : ℝ≥0∞ => x ^ y` into an order isomorphism when `y : ℝ` is positive, where the inverse is `fun x : ℝ≥0∞ => x ^ (1 / y)`. -/ @[simps! apply] def orderIsoRpow (y : ℝ) (hy : 0 < y) : ℝ≥0∞ ≃o ℝ≥0∞ := (strictMono_rpow_of_pos hy).orderIsoOfRightInverse (fun x => x ^ y) (fun x => x ^ (1 / y)) fun x => by dsimp rw [← rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] theorem orderIsoRpow_symm_apply (y : ℝ) (hy : 0 < y) : (orderIsoRpow y hy).symm = orderIsoRpow (1 / y) (one_div_pos.2 hy) := by simp only [orderIsoRpow, one_div_one_div] rfl @[gcongr] theorem rpow_le_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := monotone_rpow_of_nonneg h₂ h₁ @[gcongr] theorem rpow_lt_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x ^ z < y ^ z := strictMono_rpow_of_pos h₂ h₁ theorem rpow_le_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := (strictMono_rpow_of_pos hz).le_iff_le theorem rpow_lt_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := (strictMono_rpow_of_pos hz).lt_iff_lt theorem le_rpow_inv_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ≤ y ^ z⁻¹ ↔ x ^ z ≤ y := by nth_rw 1 [← rpow_one x] nth_rw 1 [← @mul_inv_cancel₀ _ _ z hz.ne'] rw [rpow_mul, @rpow_le_rpow_iff _ _ z⁻¹ (by simp [hz])] theorem rpow_inv_lt_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z⁻¹ < y ↔ x < y ^ z := by simp only [← not_le, le_rpow_inv_iff hz] theorem lt_rpow_inv_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x < y ^ z⁻¹ ↔ x ^ z < y := by nth_rw 1 [← rpow_one x] nth_rw 1 [← @mul_inv_cancel₀ _ _ z (ne_of_lt hz).symm] rw [rpow_mul, @rpow_lt_rpow_iff _ _ z⁻¹ (by simp [hz])] theorem rpow_inv_le_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z⁻¹ ≤ y ↔ x ≤ y ^ z := by nth_rw 1 [← ENNReal.rpow_one y] nth_rw 1 [← @mul_inv_cancel₀ _ _ z hz.ne.symm] rw [ENNReal.rpow_mul, ENNReal.rpow_le_rpow_iff (inv_pos.2 hz)] theorem rpow_lt_rpow_of_exponent_lt {x : ℝ≥0∞} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x ^ y < x ^ z := by lift x to ℝ≥0 using hx' rw [one_lt_coe_iff] at hx simp [← coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), NNReal.rpow_lt_rpow_of_exponent_lt hx hyz] @[gcongr] theorem rpow_le_rpow_of_exponent_le {x : ℝ≥0∞} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := by cases x · rcases lt_trichotomy y 0 with (Hy | Hy | Hy) <;> rcases lt_trichotomy z 0 with (Hz | Hz | Hz) <;> simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl] <;> linarith · simp only [one_le_coe_iff, some_eq_coe] at hx simp [← coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), NNReal.rpow_le_rpow_of_exponent_le hx hyz] theorem rpow_lt_rpow_of_exponent_gt {x : ℝ≥0∞} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := by lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top) simp only [coe_lt_one_iff, coe_pos] at hx0 hx1 simp [← coe_rpow_of_ne_zero (ne_of_gt hx0), NNReal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] theorem rpow_le_rpow_of_exponent_ge {x : ℝ≥0∞} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := by lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top) by_cases h : x = 0 · rcases lt_trichotomy y 0 with (Hy | Hy | Hy) <;> rcases lt_trichotomy z 0 with (Hz | Hz | Hz) <;> simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl] <;> linarith · rw [coe_le_one_iff] at hx1 simp [← coe_rpow_of_ne_zero h, NNReal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] theorem rpow_le_self_of_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := by nth_rw 2 [← ENNReal.rpow_one x] exact ENNReal.rpow_le_rpow_of_exponent_ge hx h_one_le theorem le_rpow_self_of_one_le {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z := by nth_rw 1 [← ENNReal.rpow_one x] exact ENNReal.rpow_le_rpow_of_exponent_le hx h_one_le theorem rpow_pos_of_nonneg {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x ^ p := by by_cases hp_zero : p = 0 · simp [hp_zero, zero_lt_one] · rw [← Ne] at hp_zero have hp_pos := lt_of_le_of_ne hp_nonneg hp_zero.symm rw [← zero_rpow_of_pos hp_pos] exact rpow_lt_rpow hx_pos hp_pos theorem rpow_pos {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x ^ p := by rcases lt_or_le 0 p with hp_pos | hp_nonpos · exact rpow_pos_of_nonneg hx_pos (le_of_lt hp_pos) · rw [← neg_neg p, rpow_neg, ENNReal.inv_pos] exact rpow_ne_top_of_nonneg (Right.nonneg_neg_iff.mpr hp_nonpos) hx_ne_top theorem rpow_lt_one {x : ℝ≥0∞} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x ^ z < 1 := by lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top) simp only [coe_lt_one_iff] at hx simp [← coe_rpow_of_nonneg _ (le_of_lt hz), NNReal.rpow_lt_one hx hz] theorem rpow_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := by lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top) simp only [coe_le_one_iff] at hx simp [← coe_rpow_of_nonneg _ hz, NNReal.rpow_le_one hx hz] theorem rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := by cases x · simp [top_rpow_of_neg hz, zero_lt_one] · simp only [some_eq_coe, one_lt_coe_iff] at hx simp [← coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), NNReal.rpow_lt_one_of_one_lt_of_neg hx hz] theorem rpow_le_one_of_one_le_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x ^ z ≤ 1 := by cases x · simp [top_rpow_of_neg hz, zero_lt_one] · simp only [one_le_coe_iff, some_eq_coe] at hx simp [← coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), NNReal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] theorem one_lt_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := by cases x · simp [top_rpow_of_pos hz] · simp only [some_eq_coe, one_lt_coe_iff] at hx simp [← coe_rpow_of_nonneg _ (le_of_lt hz), NNReal.one_lt_rpow hx hz] theorem one_le_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x ^ z := by cases x · simp [top_rpow_of_pos hz] · simp only [one_le_coe_iff, some_eq_coe] at hx simp [← coe_rpow_of_nonneg _ (le_of_lt hz), NNReal.one_le_rpow hx (le_of_lt hz)]
theorem one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := by lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top) simp only [coe_lt_one_iff, coe_pos] at hx1 hx2 ⊢ simp [← coe_rpow_of_ne_zero (ne_of_gt hx1), NNReal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz]
Mathlib/Analysis/SpecialFunctions/Pow/NNReal.lean
857
861
/- Copyright (c) 2023 Jake Levinson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jake Levinson -/ import Mathlib.Data.Nat.Factorial.Basic import Mathlib.Tactic.Ring import Mathlib.Tactic.Positivity.Core import Mathlib.Algebra.BigOperators.Group.Finset.Basic /-! # Double factorials This file defines the double factorial, `n‼ := n * (n - 2) * (n - 4) * ...`. ## Main declarations * `Nat.doubleFactorial`: The double factorial. -/ open Nat namespace Nat /-- `Nat.doubleFactorial n` is the double factorial of `n`. -/ @[simp] def doubleFactorial : ℕ → ℕ | 0 => 1 | 1 => 1 | k + 2 => (k + 2) * doubleFactorial k -- This notation is `\!!` not two !'s @[inherit_doc] scoped notation:10000 n "‼" => Nat.doubleFactorial n lemma doubleFactorial_pos : ∀ n, 0 < n‼ | 0 | 1 => zero_lt_one | _n + 2 => mul_pos (succ_pos _) (doubleFactorial_pos _) theorem doubleFactorial_add_two (n : ℕ) : (n + 2)‼ = (n + 2) * n‼ := rfl theorem doubleFactorial_add_one (n : ℕ) : (n + 1)‼ = (n + 1) * (n - 1)‼ := by cases n <;> rfl theorem factorial_eq_mul_doubleFactorial : ∀ n : ℕ, (n + 1)! = (n + 1)‼ * n‼ | 0 => rfl | k + 1 => by rw [doubleFactorial_add_two, factorial, factorial_eq_mul_doubleFactorial _, mul_comm _ k‼, mul_assoc] lemma doubleFactorial_le_factorial : ∀ n, n‼ ≤ n ! | 0 => le_rfl | n + 1 => by rw [factorial_eq_mul_doubleFactorial]; exact Nat.le_mul_of_pos_right _ n.doubleFactorial_pos theorem doubleFactorial_two_mul : ∀ n : ℕ, (2 * n)‼ = 2 ^ n * n ! | 0 => rfl | n + 1 => by rw [mul_add, mul_one, doubleFactorial_add_two, factorial, pow_succ, doubleFactorial_two_mul _, succ_eq_add_one] ring theorem doubleFactorial_eq_prod_even : ∀ n : ℕ, (2 * n)‼ = ∏ i ∈ Finset.range n, 2 * (i + 1) | 0 => rfl | n + 1 => by rw [Finset.prod_range_succ, ← doubleFactorial_eq_prod_even _, mul_comm (2 * n)‼, (by ring : 2 * (n + 1) = 2 * n + 2)] rfl
theorem doubleFactorial_eq_prod_odd : ∀ n : ℕ, (2 * n + 1)‼ = ∏ i ∈ Finset.range n, (2 * (i + 1) + 1) | 0 => rfl | n + 1 => by rw [Finset.prod_range_succ, ← doubleFactorial_eq_prod_odd _, mul_comm (2 * n + 1)‼, (by ring : 2 * (n + 1) + 1 = 2 * n + 1 + 2)]
Mathlib/Data/Nat/Factorial/DoubleFactorial.lean
71
76
/- 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 -/ import Mathlib.Order.Filter.Prod import Mathlib.Order.ConditionallyCompleteLattice.Basic import Mathlib.Order.Filter.Finite import Mathlib.Order.Filter.Bases.Basic /-! # Lift filters along filter and set functions -/ open Set Filter Function namespace Filter variable {α β γ : Type*} {ι : Sort*} section lift variable {f f₁ f₂ : Filter α} {g g₁ g₂ : Set α → Filter β} @[simp] theorem lift_top (g : Set α → Filter β) : (⊤ : Filter α).lift g = g univ := by simp [Filter.lift] /-- If `(p : ι → Prop, s : ι → Set α)` is a basis of a filter `f`, `g` is a monotone function `Set α → Filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → Set α)` is a basis of the filter `g (s i)`, then `(fun (i : ι) (x : β i) ↦ p i ∧ pg i x, fun (i : ι) (x : β i) ↦ sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `Filter.HasBasis` one has to use `Σ i, β i` as the index type, see `Filter.HasBasis.lift`. This lemma states the corresponding `mem_iff` statement without using a sigma type. -/ theorem HasBasis.mem_lift_iff {ι} {p : ι → Prop} {s : ι → Set α} {f : Filter α} (hf : f.HasBasis p s) {β : ι → Type*} {pg : ∀ i, β i → Prop} {sg : ∀ i, β i → Set γ} {g : Set α → Filter γ} (hg : ∀ i, (g <| s i).HasBasis (pg i) (sg i)) (gm : Monotone g) {s : Set γ} : s ∈ f.lift g ↔ ∃ i, p i ∧ ∃ x, pg i x ∧ sg i x ⊆ s := by refine (mem_biInf_of_directed ?_ ⟨univ, univ_sets _⟩).trans ?_ · intro t₁ ht₁ t₂ ht₂ exact ⟨t₁ ∩ t₂, inter_mem ht₁ ht₂, gm inter_subset_left, gm inter_subset_right⟩ · simp only [← (hg _).mem_iff] exact hf.exists_iff fun t₁ t₂ ht H => gm ht H /-- If `(p : ι → Prop, s : ι → Set α)` is a basis of a filter `f`, `g` is a monotone function `Set α → Filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → Set α)` is a basis of the filter `g (s i)`, then `(fun (i : ι) (x : β i) ↦ p i ∧ pg i x, fun (i : ι) (x : β i) ↦ sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `has_basis` one has to use `Σ i, β i` as the index type. See also `Filter.HasBasis.mem_lift_iff` for the corresponding `mem_iff` statement formulated without using a sigma type. -/ theorem HasBasis.lift {ι} {p : ι → Prop} {s : ι → Set α} {f : Filter α} (hf : f.HasBasis p s) {β : ι → Type*} {pg : ∀ i, β i → Prop} {sg : ∀ i, β i → Set γ} {g : Set α → Filter γ} (hg : ∀ i, (g (s i)).HasBasis (pg i) (sg i)) (gm : Monotone g) : (f.lift g).HasBasis (fun i : Σi, β i => p i.1 ∧ pg i.1 i.2) fun i : Σi, β i => sg i.1 i.2 := by refine ⟨fun t => (hf.mem_lift_iff hg gm).trans ?_⟩ simp [Sigma.exists, and_assoc, exists_and_left] theorem mem_lift_sets (hg : Monotone g) {s : Set β} : s ∈ f.lift g ↔ ∃ t ∈ f, s ∈ g t := (f.basis_sets.mem_lift_iff (fun s => (g s).basis_sets) hg).trans <| by simp only [id, exists_mem_subset_iff] theorem sInter_lift_sets (hg : Monotone g) : ⋂₀ { s | s ∈ f.lift g } = ⋂ s ∈ f, ⋂₀ { t | t ∈ g s } := by simp only [sInter_eq_biInter, mem_setOf_eq, Filter.mem_sets, mem_lift_sets hg, iInter_exists, iInter_and, @iInter_comm _ (Set β)] theorem mem_lift {s : Set β} {t : Set α} (ht : t ∈ f) (hs : s ∈ g t) : s ∈ f.lift g := le_principal_iff.mp <| show f.lift g ≤ 𝓟 s from iInf_le_of_le t <| iInf_le_of_le ht <| le_principal_iff.mpr hs theorem lift_le {f : Filter α} {g : Set α → Filter β} {h : Filter β} {s : Set α} (hs : s ∈ f) (hg : g s ≤ h) : f.lift g ≤ h := iInf₂_le_of_le s hs hg theorem le_lift {f : Filter α} {g : Set α → Filter β} {h : Filter β} : h ≤ f.lift g ↔ ∀ s ∈ f, h ≤ g s := le_iInf₂_iff theorem lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := iInf_mono fun s => iInf_mono' fun hs => ⟨hf hs, hg s⟩ theorem lift_mono' (hg : ∀ s ∈ f, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := iInf₂_mono hg theorem tendsto_lift {m : γ → β} {l : Filter γ} : Tendsto m l (f.lift g) ↔ ∀ s ∈ f, Tendsto m l (g s) := by simp only [Filter.lift, tendsto_iInf] theorem map_lift_eq {m : β → γ} (hg : Monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have : Monotone (map m ∘ g) := map_mono.comp hg Filter.ext fun s => by simp only [mem_lift_sets hg, mem_lift_sets this, exists_prop, mem_map, Function.comp_apply] theorem comap_lift_eq {m : γ → β} : comap m (f.lift g) = f.lift (comap m ∘ g) := by simp only [Filter.lift, comap_iInf]; rfl theorem comap_lift_eq2 {m : β → α} {g : Set β → Filter γ} (hg : Monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_iInf₂ fun s hs => iInf₂_le (m ⁻¹' s) ⟨s, hs, Subset.rfl⟩) (le_iInf₂ fun _s ⟨s', hs', h_sub⟩ => iInf₂_le_of_le s' hs' <| hg h_sub) theorem lift_map_le {g : Set β → Filter γ} {m : α → β} : (map m f).lift g ≤ f.lift (g ∘ image m) := le_lift.2 fun _s hs => lift_le (image_mem_map hs) le_rfl theorem map_lift_eq2 {g : Set β → Filter γ} {m : α → β} (hg : Monotone g) : (map m f).lift g = f.lift (g ∘ image m) := lift_map_le.antisymm <| le_lift.2 fun _s hs => lift_le hs <| hg <| image_preimage_subset _ _ theorem lift_comm {g : Filter β} {h : Set α → Set β → Filter γ} : (f.lift fun s => g.lift (h s)) = g.lift fun t => f.lift fun s => h s t := le_antisymm (le_iInf fun i => le_iInf fun hi => le_iInf fun j => le_iInf fun hj => iInf_le_of_le j <| iInf_le_of_le hj <| iInf_le_of_le i <| iInf_le _ hi) (le_iInf fun i => le_iInf fun hi => le_iInf fun j => le_iInf fun hj => iInf_le_of_le j <| iInf_le_of_le hj <| iInf_le_of_le i <| iInf_le _ hi) theorem lift_assoc {h : Set β → Filter γ} (hg : Monotone g) : (f.lift g).lift h = f.lift fun s => (g s).lift h := le_antisymm (le_iInf₂ fun _s hs => le_iInf₂ fun t ht => iInf_le_of_le t <| iInf_le _ <| (mem_lift_sets hg).mpr ⟨_, hs, ht⟩) (le_iInf₂ fun t ht => let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht iInf_le_of_le s <| iInf_le_of_le hs <| iInf_le_of_le t <| iInf_le _ h') theorem lift_lift_same_le_lift {g : Set α → Set α → Filter β} : (f.lift fun s => f.lift (g s)) ≤ f.lift fun s => g s s := le_lift.2 fun _s hs => lift_le hs <| lift_le hs le_rfl theorem lift_lift_same_eq_lift {g : Set α → Set α → Filter β} (hg₁ : ∀ s, Monotone fun t => g s t) (hg₂ : ∀ t, Monotone fun s => g s t) : (f.lift fun s => f.lift (g s)) = f.lift fun s => g s s := lift_lift_same_le_lift.antisymm <| le_lift.2 fun s hs => le_lift.2 fun t ht => lift_le (inter_mem hs ht) <| calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) := hg₂ (s ∩ t) inter_subset_left _ ≤ g s t := hg₁ s inter_subset_right theorem lift_principal {s : Set α} (hg : Monotone g) : (𝓟 s).lift g = g s := (lift_le (mem_principal_self _) le_rfl).antisymm (le_lift.2 fun _t ht => hg ht) theorem monotone_lift [Preorder γ] {f : γ → Filter α} {g : γ → Set α → Filter β} (hf : Monotone f) (hg : Monotone g) : Monotone fun c => (f c).lift (g c) := fun _ _ h => lift_mono (hf h) (hg h) theorem lift_neBot_iff (hm : Monotone g) : (NeBot (f.lift g)) ↔ ∀ s ∈ f, NeBot (g s) := by simp only [neBot_iff, Ne, ← empty_mem_iff_bot, mem_lift_sets hm, not_exists, not_and] @[simp] theorem lift_const {f : Filter α} {g : Filter β} : (f.lift fun _ => g) = g := iInf_subtype'.trans iInf_const @[simp] theorem lift_inf {f : Filter α} {g h : Set α → Filter β} : (f.lift fun x => g x ⊓ h x) = f.lift g ⊓ f.lift h := by simp only [Filter.lift, iInf_inf_eq] @[simp] theorem lift_principal2 {f : Filter α} : f.lift 𝓟 = f := le_antisymm (fun s hs => mem_lift hs (mem_principal_self s)) (le_iInf fun s => le_iInf fun hs => by simp only [hs, le_principal_iff]) theorem lift_iInf_le {f : ι → Filter α} {g : Set α → Filter β} : (iInf f).lift g ≤ ⨅ i, (f i).lift g := le_iInf fun _ => lift_mono (iInf_le _ _) le_rfl theorem lift_iInf [Nonempty ι] {f : ι → Filter α} {g : Set α → Filter β} (hg : ∀ s t, g (s ∩ t) = g s ⊓ g t) : (iInf f).lift g = ⨅ i, (f i).lift g := by refine lift_iInf_le.antisymm fun s => ?_ have H : ∀ t ∈ iInf f, ⨅ i, (f i).lift g ≤ g t := by intro t ht refine iInf_sets_induct ht ?_ fun hs ht => ?_ · inhabit ι exact iInf₂_le_of_le default univ (iInf_le _ univ_mem) · rw [hg] exact le_inf (iInf₂_le_of_le _ _ <| iInf_le _ hs) ht simp only [mem_lift_sets (Monotone.of_map_inf hg), exists_imp, and_imp] exact fun t ht hs => H t ht hs theorem lift_iInf_of_directed [Nonempty ι] {f : ι → Filter α} {g : Set α → Filter β} (hf : Directed (· ≥ ·) f) (hg : Monotone g) : (iInf f).lift g = ⨅ i, (f i).lift g := lift_iInf_le.antisymm fun s => by simp only [mem_lift_sets hg, exists_imp, and_imp, mem_iInf_of_directed hf] exact fun t i ht hs => mem_iInf_of_mem i <| mem_lift ht hs theorem lift_iInf_of_map_univ {f : ι → Filter α} {g : Set α → Filter β} (hg : ∀ s t, g (s ∩ t) = g s ⊓ g t) (hg' : g univ = ⊤) : (iInf f).lift g = ⨅ i, (f i).lift g := by cases isEmpty_or_nonempty ι · simp [iInf_of_empty, hg'] · exact lift_iInf hg end lift section Lift' variable {f f₁ f₂ : Filter α} {h h₁ h₂ : Set α → Set β} @[simp] theorem lift'_top (h : Set α → Set β) : (⊤ : Filter α).lift' h = 𝓟 (h univ) := lift_top _ theorem mem_lift' {t : Set α} (ht : t ∈ f) : h t ∈ f.lift' h := le_principal_iff.mp <| show f.lift' h ≤ 𝓟 (h t) from iInf_le_of_le t <| iInf_le_of_le ht <| le_rfl theorem tendsto_lift' {m : γ → β} {l : Filter γ} : Tendsto m l (f.lift' h) ↔ ∀ s ∈ f, ∀ᶠ a in l, m a ∈ h s := by simp only [Filter.lift', tendsto_lift, tendsto_principal, comp] theorem HasBasis.lift' {ι} {p : ι → Prop} {s} (hf : f.HasBasis p s) (hh : Monotone h) : (f.lift' h).HasBasis p (h ∘ s) := ⟨fun t => (hf.mem_lift_iff (fun i => hasBasis_principal (h (s i))) (monotone_principal.comp hh)).trans <| by simp only [exists_const, true_and, comp]⟩ theorem mem_lift'_sets (hh : Monotone h) {s : Set β} : s ∈ f.lift' h ↔ ∃ t ∈ f, h t ⊆ s := mem_lift_sets <| monotone_principal.comp hh theorem eventually_lift'_iff (hh : Monotone h) {p : β → Prop} : (∀ᶠ y in f.lift' h, p y) ↔ ∃ t ∈ f, ∀ y ∈ h t, p y := mem_lift'_sets hh theorem sInter_lift'_sets (hh : Monotone h) : ⋂₀ { s | s ∈ f.lift' h } = ⋂ s ∈ f, h s := (sInter_lift_sets (monotone_principal.comp hh)).trans <| iInter₂_congr fun _ _ => csInf_Ici theorem lift'_le {f : Filter α} {g : Set α → Set β} {h : Filter β} {s : Set α} (hs : s ∈ f) (hg : 𝓟 (g s) ≤ h) : f.lift' g ≤ h := lift_le hs hg theorem lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ := lift_mono hf fun s => principal_mono.mpr <| hh s theorem lift'_mono' (hh : ∀ s ∈ f, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := iInf₂_mono fun s hs => principal_mono.mpr <| hh s hs theorem lift'_cong (hh : ∀ s ∈ f, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ := le_antisymm (lift'_mono' fun s hs => le_of_eq <| hh s hs) (lift'_mono' fun s hs => le_of_eq <| (hh s hs).symm) theorem map_lift'_eq {m : β → γ} (hh : Monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) := calc map m (f.lift' h) = f.lift (map m ∘ 𝓟 ∘ h) := map_lift_eq <| monotone_principal.comp hh _ = f.lift' (image m ∘ h) := by simp only [comp_def, Filter.lift', map_principal] theorem lift'_map_le {g : Set β → Set γ} {m : α → β} : (map m f).lift' g ≤ f.lift' (g ∘ image m) := lift_map_le theorem map_lift'_eq2 {g : Set β → Set γ} {m : α → β} (hg : Monotone g) : (map m f).lift' g = f.lift' (g ∘ image m) := map_lift_eq2 <| monotone_principal.comp hg theorem comap_lift'_eq {m : γ → β} : comap m (f.lift' h) = f.lift' (preimage m ∘ h) := by simp only [Filter.lift', comap_lift_eq, comp_def, comap_principal] theorem comap_lift'_eq2 {m : β → α} {g : Set β → Set γ} (hg : Monotone g) : (comap m f).lift' g = f.lift' (g ∘ preimage m) := comap_lift_eq2 <| monotone_principal.comp hg theorem lift'_principal {s : Set α} (hh : Monotone h) : (𝓟 s).lift' h = 𝓟 (h s) := lift_principal <| monotone_principal.comp hh theorem lift'_pure {a : α} (hh : Monotone h) : (pure a : Filter α).lift' h = 𝓟 (h {a}) := by rw [← principal_singleton, lift'_principal hh] theorem lift'_bot (hh : Monotone h) : (⊥ : Filter α).lift' h = 𝓟 (h ∅) := by rw [← principal_empty, lift'_principal hh] theorem le_lift' {f : Filter α} {h : Set α → Set β} {g : Filter β} : g ≤ f.lift' h ↔ ∀ s ∈ f, h s ∈ g := le_lift.trans <| forall₂_congr fun _ _ => le_principal_iff theorem principal_le_lift' {t : Set β} : 𝓟 t ≤ f.lift' h ↔ ∀ s ∈ f, t ⊆ h s := le_lift' theorem monotone_lift' [Preorder γ] {f : γ → Filter α} {g : γ → Set α → Set β} (hf : Monotone f) (hg : Monotone g) : Monotone fun c => (f c).lift' (g c) := fun _ _ h => lift'_mono (hf h) (hg h) theorem lift_lift'_assoc {g : Set α → Set β} {h : Set β → Filter γ} (hg : Monotone g) (hh : Monotone h) : (f.lift' g).lift h = f.lift fun s => h (g s) := calc (f.lift' g).lift h = f.lift fun s => (𝓟 (g s)).lift h := lift_assoc (monotone_principal.comp hg) _ = f.lift fun s => h (g s) := by simp only [lift_principal, hh, eq_self_iff_true] theorem lift'_lift'_assoc {g : Set α → Set β} {h : Set β → Set γ} (hg : Monotone g) (hh : Monotone h) : (f.lift' g).lift' h = f.lift' fun s => h (g s) := lift_lift'_assoc hg (monotone_principal.comp hh) theorem lift'_lift_assoc {g : Set α → Filter β} {h : Set β → Set γ} (hg : Monotone g) : (f.lift g).lift' h = f.lift fun s => (g s).lift' h := lift_assoc hg theorem lift_lift'_same_le_lift' {g : Set α → Set α → Set β} : (f.lift fun s => f.lift' (g s)) ≤ f.lift' fun s => g s s :=
lift_lift_same_le_lift theorem lift_lift'_same_eq_lift' {g : Set α → Set α → Set β} (hg₁ : ∀ s, Monotone fun t => g s t) (hg₂ : ∀ t, Monotone fun s => g s t) :
Mathlib/Order/Filter/Lift.lean
294
297
/- Copyright (c) 2022 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import Mathlib.Data.Fintype.Card import Mathlib.Algebra.BigOperators.Group.Finset.Basic /-! # Multiset coercion to type This module defines a `CoeSort` instance for multisets and gives it a `Fintype` instance. It also defines `Multiset.toEnumFinset`, which is another way to enumerate the elements of a multiset. These coercions and definitions make it easier to sum over multisets using existing `Finset` theory. ## Main definitions * A coercion from `m : Multiset α` to a `Type*`. Each `x : m` has two components. The first, `x.1`, can be obtained via the coercion `↑x : α`, and it yields the underlying element of the multiset. The second, `x.2`, is a term of `Fin (m.count x)`, and its function is to ensure each term appears with the correct multiplicity. Note that this coercion requires `DecidableEq α` due to the definition using `Multiset.count`. * `Multiset.toEnumFinset` is a `Finset` version of this. * `Multiset.coeEmbedding` is the embedding `m ↪ α × ℕ`, whose first component is the coercion and whose second component enumerates elements with multiplicity. * `Multiset.coeEquiv` is the equivalence `m ≃ m.toEnumFinset`. ## Tags multiset enumeration -/ variable {α β : Type*} [DecidableEq α] [DecidableEq β] {m : Multiset α} namespace Multiset /-- Auxiliary definition for the `CoeSort` instance. This prevents the `CoeOut m α` instance from inadvertently applying to other sigma types. -/ def ToType (m : Multiset α) : Type _ := (x : α) × Fin (m.count x) /-- Create a type that has the same number of elements as the multiset. Terms of this type are triples `⟨x, ⟨i, h⟩⟩` where `x : α`, `i : ℕ`, and `h : i < m.count x`. This way repeated elements of a multiset appear multiple times from different values of `i`. -/ instance : CoeSort (Multiset α) (Type _) := ⟨Multiset.ToType⟩ example : DecidableEq m := inferInstanceAs <| DecidableEq ((x : α) × Fin (m.count x)) /-- Constructor for terms of the coercion of `m` to a type. This helps Lean pick up the correct instances. -/ @[reducible, match_pattern] def mkToType (m : Multiset α) (x : α) (i : Fin (m.count x)) : m := ⟨x, i⟩ /-- As a convenience, there is a coercion from `m : Type*` to `α` by projecting onto the first component. -/ instance instCoeSortMultisetType.instCoeOutToType : CoeOut m α := ⟨fun x ↦ x.1⟩ theorem coe_mk {x : α} {i : Fin (m.count x)} : ↑(m.mkToType x i) = x := rfl @[simp] lemma coe_mem {x : m} : ↑x ∈ m := Multiset.count_pos.mp (by have := x.2.2; omega) @[simp] protected theorem forall_coe (p : m → Prop) : (∀ x : m, p x) ↔ ∀ (x : α) (i : Fin (m.count x)), p ⟨x, i⟩ := Sigma.forall @[simp] protected theorem exists_coe (p : m → Prop) : (∃ x : m, p x) ↔ ∃ (x : α) (i : Fin (m.count x)), p ⟨x, i⟩ := Sigma.exists instance : Fintype { p : α × ℕ | p.2 < m.count p.1 } := Fintype.ofFinset (m.toFinset.biUnion fun x ↦ (Finset.range (m.count x)).map ⟨_, Prod.mk_right_injective x⟩) (by rintro ⟨x, i⟩ simp only [Finset.mem_biUnion, Multiset.mem_toFinset, Finset.mem_map, Finset.mem_range, Function.Embedding.coeFn_mk, Prod.mk_inj, Set.mem_setOf_eq] simp only [← and_assoc, exists_eq_right, and_iff_right_iff_imp] exact fun h ↦ Multiset.count_pos.mp (by omega)) /-- Construct a finset whose elements enumerate the elements of the multiset `m`. The `ℕ` component is used to differentiate between equal elements: if `x` appears `n` times then `(x, 0)`, ..., and `(x, n-1)` appear in the `Finset`. -/ def toEnumFinset (m : Multiset α) : Finset (α × ℕ) := { p : α × ℕ | p.2 < m.count p.1 }.toFinset @[simp] theorem mem_toEnumFinset (m : Multiset α) (p : α × ℕ) : p ∈ m.toEnumFinset ↔ p.2 < m.count p.1 := Set.mem_toFinset theorem mem_of_mem_toEnumFinset {p : α × ℕ} (h : p ∈ m.toEnumFinset) : p.1 ∈ m := have := (m.mem_toEnumFinset p).mp h; Multiset.count_pos.mp (by omega) @[simp] lemma toEnumFinset_filter_eq (m : Multiset α) (a : α) : {x ∈ m.toEnumFinset | x.1 = a} = {a} ×ˢ Finset.range (m.count a) := by aesop @[simp] lemma map_toEnumFinset_fst (m : Multiset α) : m.toEnumFinset.val.map Prod.fst = m := by ext a; simp [count_map, ← Finset.filter_val, eq_comm (a := a)] @[simp] lemma image_toEnumFinset_fst (m : Multiset α) : m.toEnumFinset.image Prod.fst = m.toFinset := by rw [Finset.image, Multiset.map_toEnumFinset_fst] @[simp] lemma map_fst_le_of_subset_toEnumFinset {s : Finset (α × ℕ)} (hsm : s ⊆ m.toEnumFinset) : s.1.map Prod.fst ≤ m := by simp_rw [le_iff_count, count_map] rintro a obtain ha | ha := (s.1.filter fun x ↦ a = x.1).card.eq_zero_or_pos · rw [ha] exact Nat.zero_le _ obtain ⟨n, han, hn⟩ : ∃ n ≥ card (s.1.filter fun x ↦ a = x.1) - 1, (a, n) ∈ s := by by_contra! h replace h : {x ∈ s | x.1 = a} ⊆ {a} ×ˢ .range (card (s.1.filter fun x ↦ a = x.1) - 1) := by simpa +contextual [forall_swap (β := _ = a), Finset.subset_iff, imp_not_comm, not_le, Nat.lt_sub_iff_add_lt] using h have : card (s.1.filter fun x ↦ a = x.1) ≤ card (s.1.filter fun x ↦ a = x.1) - 1 := by simpa [Finset.card, eq_comm] using Finset.card_mono h omega exact Nat.le_of_pred_lt (han.trans_lt <| by simpa using hsm hn) @[mono] theorem toEnumFinset_mono {m₁ m₂ : Multiset α} (h : m₁ ≤ m₂) : m₁.toEnumFinset ⊆ m₂.toEnumFinset := by intro p simp only [Multiset.mem_toEnumFinset] exact gt_of_ge_of_gt (Multiset.le_iff_count.mp h p.1) @[simp] theorem toEnumFinset_subset_iff {m₁ m₂ : Multiset α} : m₁.toEnumFinset ⊆ m₂.toEnumFinset ↔ m₁ ≤ m₂ := ⟨fun h ↦ by simpa using map_fst_le_of_subset_toEnumFinset h, Multiset.toEnumFinset_mono⟩ /-- The embedding from a multiset into `α × ℕ` where the second coordinate enumerates repeats. If you are looking for the function `m → α`, that would be plain `(↑)`. -/ @[simps] def coeEmbedding (m : Multiset α) : m ↪ α × ℕ where toFun x := (x, x.2) inj' := by intro ⟨x, i, hi⟩ ⟨y, j, hj⟩ rintro ⟨⟩ rfl /-- Another way to coerce a `Multiset` to a type is to go through `m.toEnumFinset` and coerce that `Finset` to a type. -/ @[simps] def coeEquiv (m : Multiset α) : m ≃ m.toEnumFinset where toFun x := ⟨m.coeEmbedding x, by rw [Multiset.mem_toEnumFinset] exact x.2.2⟩ invFun x := ⟨x.1.1, x.1.2, by rw [← Multiset.mem_toEnumFinset] exact x.2⟩ left_inv := by rintro ⟨x, i, h⟩ rfl right_inv := by rintro ⟨⟨x, i⟩, h⟩ rfl @[simp] theorem toEmbedding_coeEquiv_trans (m : Multiset α) : m.coeEquiv.toEmbedding.trans (Function.Embedding.subtype _) = m.coeEmbedding := by ext <;> rfl @[irreducible] instance fintypeCoe : Fintype m := Fintype.ofEquiv m.toEnumFinset m.coeEquiv.symm theorem map_univ_coeEmbedding (m : Multiset α) : (Finset.univ : Finset m).map m.coeEmbedding = m.toEnumFinset := by ext ⟨x, i⟩ simp only [Fin.exists_iff, Finset.mem_map, Finset.mem_univ, Multiset.coeEmbedding_apply, Prod.mk_inj, exists_true_left, Multiset.exists_coe, Multiset.coe_mk, Fin.val_mk, exists_prop, exists_eq_right_right, exists_eq_right, Multiset.mem_toEnumFinset, true_and] @[simp] theorem map_univ_coe (m : Multiset α) : (Finset.univ : Finset m).val.map (fun x : m ↦ (x : α)) = m := by have := m.map_toEnumFinset_fst rw [← m.map_univ_coeEmbedding] at this simpa only [Finset.map_val, Multiset.coeEmbedding_apply, Multiset.map_map, Function.comp_apply] using this theorem map_univ_comp_coe {β : Type*} (m : Multiset α) (f : α → β) : ((Finset.univ : Finset m).val.map (f ∘ (fun x : m ↦ (x : α)))) = m.map f := by rw [← Multiset.map_map, Multiset.map_univ_coe] @[simp] theorem map_univ {β : Type*} (m : Multiset α) (f : α → β) : ((Finset.univ : Finset m).val.map fun (x : m) ↦ f (x : α)) = m.map f := by simp_rw [← Function.comp_apply (f := f)] exact map_univ_comp_coe m f @[simp] theorem card_toEnumFinset (m : Multiset α) : m.toEnumFinset.card = Multiset.card m := by rw [Finset.card, ← Multiset.card_map Prod.fst m.toEnumFinset.val] congr exact m.map_toEnumFinset_fst @[simp] theorem card_coe (m : Multiset α) : Fintype.card m = Multiset.card m := by rw [Fintype.card_congr m.coeEquiv] simp only [Fintype.card_coe, card_toEnumFinset] @[to_additive] theorem prod_eq_prod_coe [CommMonoid α] (m : Multiset α) : m.prod = ∏ x : m, (x : α) := by congr simp @[to_additive] theorem prod_eq_prod_toEnumFinset [CommMonoid α] (m : Multiset α) : m.prod = ∏ x ∈ m.toEnumFinset, x.1 := by congr simp @[to_additive] theorem prod_toEnumFinset {β : Type*} [CommMonoid β] (m : Multiset α) (f : α → ℕ → β) : ∏ x ∈ m.toEnumFinset, f x.1 x.2 = ∏ x : m, f x x.2 := by rw [Fintype.prod_equiv m.coeEquiv (fun x ↦ f x x.2) fun x ↦ f x.1.1 x.1.2] · rw [← m.toEnumFinset.prod_coe_sort fun x ↦ f x.1 x.2] · intro x rfl /-- If `s = t` then there's an equivalence between the appropriate types. -/ @[simps] def cast {s t : Multiset α} (h : s = t) : s ≃ t where toFun x := ⟨x.1, x.2.cast (by simp [h])⟩ invFun x := ⟨x.1, x.2.cast (by simp [h])⟩ left_inv x := rfl right_inv x := rfl instance : IsEmpty (0 : Multiset α) := Fintype.card_eq_zero_iff.mp (by simp) instance : IsEmpty (∅ : Multiset α) := Fintype.card_eq_zero_iff.mp (by simp) /-- `v ::ₘ m` is equivalent to `Option m` by mapping one `v` to `none` and everything else to `m`. -/ def consEquiv {v : α} : v ::ₘ m ≃ Option m where toFun x := if h : x.1 = v ∧ x.2.val = m.count v then none else some ⟨x.1, ⟨x.2, by by_cases hv : x.1 = v · simp only [hv, true_and] at h ⊢ apply lt_of_le_of_ne (Nat.le_of_lt_add_one _) h convert x.2.2 using 1 simp [hv] · convert x.2.2 using 1 exact (count_cons_of_ne hv _).symm ⟩⟩ invFun x := x.elim ⟨v, ⟨m.count v, by simp⟩⟩ (fun x ↦ ⟨x.1, x.2.castLE (count_le_count_cons ..)⟩) left_inv := by rintro ⟨x, hx⟩ dsimp only
split · rename_i h obtain ⟨rfl, h2⟩ := h simp [← h2] · simp right_inv := by
Mathlib/Data/Multiset/Fintype.lean
263
268
/- 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
1,364
1,367
/- Copyright (c) 2022 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import Mathlib.Algebra.Category.ModuleCat.Projective import Mathlib.AlgebraicTopology.ExtraDegeneracy import Mathlib.CategoryTheory.Abelian.Ext import Mathlib.GroupTheory.GroupAction.Ring import Mathlib.RepresentationTheory.Rep import Mathlib.RingTheory.TensorProduct.Free import Mathlib.CategoryTheory.Functor.ReflectsIso.Balanced /-! # The structure of the `k[G]`-module `k[Gⁿ]` This file contains facts about an important `k[G]`-module structure on `k[Gⁿ]`, where `k` is a commutative ring and `G` is a group. The module structure arises from the representation `G →* End(k[Gⁿ])` induced by the diagonal action of `G` on `Gⁿ.` In particular, we define an isomorphism of `k`-linear `G`-representations between `k[Gⁿ⁺¹]` and `k[G] ⊗ₖ k[Gⁿ]` (on which `G` acts by `ρ(g₁)(g₂ ⊗ x) = (g₁ * g₂) ⊗ x`). This allows us to define a `k[G]`-basis on `k[Gⁿ⁺¹]`, by mapping the natural `k[G]`-basis of `k[G] ⊗ₖ k[Gⁿ]` along the isomorphism. We then define the standard resolution of `k` as a trivial representation, by taking the alternating face map complex associated to an appropriate simplicial `k`-linear `G`-representation. This simplicial object is the `Rep.linearization` of the simplicial `G`-set given by the universal cover of the classifying space of `G`, `EG`. We prove this simplicial `G`-set `EG` is isomorphic to the Čech nerve of the natural arrow of `G`-sets `G ⟶ {pt}`. We then use this isomorphism to deduce that as a complex of `k`-modules, the standard resolution of `k` as a trivial `G`-representation is homotopy equivalent to the complex with `k` at 0 and 0 elsewhere. Putting this material together allows us to define `groupCohomology.projectiveResolution`, the standard projective resolution of `k` as a trivial `k`-linear `G`-representation. ## Main definitions * `groupCohomology.resolution.actionDiagonalSucc` * `groupCohomology.resolution.diagonalSucc` * `groupCohomology.resolution.ofMulActionBasis` * `classifyingSpaceUniversalCover` * `groupCohomology.resolution.forget₂ToModuleCatHomotopyEquiv` * `groupCohomology.projectiveResolution` ## Implementation notes We express `k[G]`-module structures on a module `k`-module `V` using the `Representation` definition. We avoid using instances `Module (G →₀ k) V` so that we do not run into possible scalar action diamonds. We also use the category theory library to bundle the type `k[Gⁿ]` - or more generally `k[H]` when `H` has `G`-action - and the representation together, as a term of type `Rep k G`, and call it `Rep.ofMulAction k G H.` This enables us to express the fact that certain maps are `G`-equivariant by constructing morphisms in the category `Rep k G`, i.e., representations of `G` over `k`. -/ /- Porting note: most altered proofs in this file involved changing `simp` to `rw` or `erw`, so https://github.com/leanprover-community/mathlib4/issues/5026 and https://github.com/leanprover-community/mathlib4/issues/5164 are relevant. -/ suppress_compilation noncomputable section universe u v w variable {k G : Type u} [CommRing k] {n : ℕ} open CategoryTheory Finsupp local notation "Gⁿ" => Fin n → G set_option quotPrecheck false local notation "Gⁿ⁺¹" => Fin (n + 1) → G namespace groupCohomology.resolution open Finsupp hiding lift open MonoidalCategory open Fin (partialProd) section Basis variable (k G n) [Group G] section Action open Action /-- An isomorphism of `G`-sets `Gⁿ⁺¹ ≅ G × Gⁿ`, where `G` acts by left multiplication on `Gⁿ⁺¹` and `G` but trivially on `Gⁿ`. The map sends `(g₀, ..., gₙ) ↦ (g₀, (g₀⁻¹g₁, g₁⁻¹g₂, ..., gₙ₋₁⁻¹gₙ))`, and the inverse is `(g₀, (g₁, ..., gₙ)) ↦ (g₀, g₀g₁, g₀g₁g₂, ..., g₀g₁...gₙ).` -/ def actionDiagonalSucc (G : Type u) [Group G] : ∀ n : ℕ, diagonal G (n + 1) ≅ leftRegular G ⊗ Action.mk (Fin n → G) 1 | 0 => diagonalOneIsoLeftRegular G ≪≫ (ρ_ _).symm ≪≫ tensorIso (Iso.refl _) (tensorUnitIso (Equiv.ofUnique PUnit _).toIso) | n + 1 => diagonalSucc _ _ ≪≫ tensorIso (Iso.refl _) (actionDiagonalSucc G n) ≪≫ leftRegularTensorIso _ _ ≪≫ tensorIso (Iso.refl _) (mkIso (Fin.insertNthEquiv (fun _ => G) 0).toIso fun _ => rfl) theorem actionDiagonalSucc_hom_apply {G : Type u} [Group G] {n : ℕ} (f : Fin (n + 1) → G) : (actionDiagonalSucc G n).hom.hom f = (f 0, fun i => (f (Fin.castSucc i))⁻¹ * f i.succ) := by induction n with | zero => exact Prod.ext rfl (funext fun x => Fin.elim0 x) | succ n hn => refine Prod.ext rfl (funext fun x => ?_) /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11039): broken proof was · dsimp only [actionDiagonalSucc] simp only [Iso.trans_hom, comp_hom, types_comp_apply, diagonalSucc_hom_hom, leftRegularTensorIso_hom_hom, tensorIso_hom, mkIso_hom_hom, Equiv.toIso_hom, Action.tensorHom, Equiv.piFinSuccAbove_symm_apply, tensor_apply, types_id_apply, tensor_rho, MonoidHom.one_apply, End.one_def, hn fun j : Fin (n + 1) => f j.succ, Fin.insertNth_zero'] refine' Fin.cases (Fin.cons_zero _ _) (fun i => _) x · simp only [Fin.cons_succ, mul_left_inj, inv_inj, Fin.castSucc_fin_succ] -/ dsimp [actionDiagonalSucc] erw [hn (fun (j : Fin (n + 1)) => f j.succ)] exact Fin.cases rfl (fun i => rfl) x theorem actionDiagonalSucc_inv_apply {G : Type u} [Group G] {n : ℕ} (g : G) (f : Fin n → G) : (actionDiagonalSucc G n).inv.hom (g, f) = (g • Fin.partialProd f : Fin (n + 1) → G) := by revert g induction n with | zero => intro g funext (x : Fin 1) simp only [Subsingleton.elim x 0, Pi.smul_apply, Fin.partialProd_zero, smul_eq_mul, mul_one] rfl | succ n hn => intro g /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11039): broken proof was ext dsimp only [actionDiagonalSucc] simp only [Iso.trans_inv, comp_hom, hn, diagonalSucc_inv_hom, types_comp_apply, tensorIso_inv, Iso.refl_inv, Action.tensorHom, id_hom, tensor_apply, types_id_apply, leftRegularTensorIso_inv_hom, tensor_ρ, leftRegular_ρ_apply, Pi.smul_apply, smul_eq_mul] refine' Fin.cases _ _ x · simp only [Fin.cons_zero, Fin.partialProd_zero, mul_one] · intro i simpa only [Fin.cons_succ, Pi.smul_apply, smul_eq_mul, Fin.partialProd_succ', mul_assoc] -/ funext x dsimp [actionDiagonalSucc] erw [hn, Fin.consEquiv_apply] refine Fin.cases ?_ (fun i => ?_) x · simp only [Fin.insertNth_zero, Fin.cons_zero, Fin.partialProd_zero, mul_one] · simp only [Fin.cons_succ, Pi.smul_apply, smul_eq_mul, Fin.partialProd_succ', ← mul_assoc] rfl end Action section Rep open Rep /-- An isomorphism of `k`-linear representations of `G` from `k[Gⁿ⁺¹]` to `k[G] ⊗ₖ k[Gⁿ]` (on which `G` acts by `ρ(g₁)(g₂ ⊗ x) = (g₁ * g₂) ⊗ x`) sending `(g₀, ..., gₙ)` to `g₀ ⊗ (g₀⁻¹g₁, g₁⁻¹g₂, ..., gₙ₋₁⁻¹gₙ)`. The inverse sends `g₀ ⊗ (g₁, ..., gₙ)` to `(g₀, g₀g₁, ..., g₀g₁...gₙ)`. -/ def diagonalSucc (n : ℕ) : diagonal k G (n + 1) ≅ leftRegular k G ⊗ trivial k G ((Fin n → G) →₀ k) := (linearization k G).mapIso (actionDiagonalSucc G n) ≪≫ (Functor.Monoidal.μIso (linearization k G) _ _).symm ≪≫ tensorIso (Iso.refl _) (linearizationTrivialIso k G (Fin n → G)) variable {k G n} theorem diagonalSucc_hom_single (f : Gⁿ⁺¹) (a : k) : (diagonalSucc k G n).hom.hom (single f a) = single (f 0) 1 ⊗ₜ single (fun i => (f (Fin.castSucc i))⁻¹ * f i.succ) a := by dsimp [diagonalSucc] erw [lmapDomain_apply, mapDomain_single, LinearEquiv.coe_toLinearMap, finsuppTensorFinsupp', LinearEquiv.trans_symm, LinearEquiv.trans_apply, lcongr_symm, Equiv.refl_symm] erw [lcongr_single] rw [TensorProduct.lid_symm_apply, actionDiagonalSucc_hom_apply, finsuppTensorFinsupp_symm_single] rfl theorem diagonalSucc_inv_single_single (g : G) (f : Gⁿ) (a b : k) : (diagonalSucc k G n).inv.hom (Finsupp.single g a ⊗ₜ Finsupp.single f b) = single (g • partialProd f) (a * b) := by /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11039): broken proof was dsimp only [diagonalSucc] simp only [Iso.trans_inv, Iso.symm_inv, Iso.refl_inv, tensorIso_inv, Action.tensorHom, Action.comp_hom, ModuleCat.comp_def, LinearMap.comp_apply, asIso_hom, Functor.mapIso_inv, ModuleCat.MonoidalCategory.hom_apply, linearizationTrivialIso_inv_hom_apply, linearization_μ_hom, Action.id_hom ((linearization k G).obj _), actionDiagonalSucc_inv_apply, ModuleCat.id_apply, LinearEquiv.coe_toLinearMap, finsuppTensorFinsupp'_single_tmul_single k (Action.leftRegular G).V, linearization_map_hom_single (actionDiagonalSucc G n).inv (g, f) (a * b)] -/ change mapDomain (actionDiagonalSucc G n).inv.hom (lcongr (Equiv.refl (G × (Fin n → G))) (TensorProduct.lid k k) (finsuppTensorFinsupp k k k k G (Fin n → G) (single g a ⊗ₜ[k] single f b))) = single (g • partialProd f) (a * b) rw [finsuppTensorFinsupp_single, lcongr_single, mapDomain_single, Equiv.refl_apply, actionDiagonalSucc_inv_apply] rfl theorem diagonalSucc_inv_single_left (g : G) (f : Gⁿ →₀ k) (r : k) : (diagonalSucc k G n).inv.hom (Finsupp.single g r ⊗ₜ f) = Finsupp.lift (Gⁿ⁺¹ →₀ k) k Gⁿ (fun f => single (g • partialProd f) r) f := by refine f.induction ?_ ?_ · simp only [TensorProduct.tmul_zero, map_zero] · intro a b x _ _ hx -- `simp` doesn't pick up on `diagonalSucc_inv_single_single` unless it has parentheses. simp only [lift_apply, smul_single', mul_one, TensorProduct.tmul_add, map_add, (diagonalSucc_inv_single_single), hx, Finsupp.sum_single_index, mul_comm b, zero_mul, single_zero] theorem diagonalSucc_inv_single_right (g : G →₀ k) (f : Gⁿ) (r : k) : (diagonalSucc k G n).inv.hom (g ⊗ₜ Finsupp.single f r) = Finsupp.lift _ k G (fun a => single (a • partialProd f) r) g := by refine g.induction ?_ ?_ · simp only [TensorProduct.zero_tmul, map_zero] · intro a b x _ _ hx -- `simp` doesn't pick up on `diagonalSucc_inv_single_single` unless it has parentheses. simp only [lift_apply, smul_single', map_add, hx, (diagonalSucc_inv_single_single), TensorProduct.add_tmul, Finsupp.sum_single_index, zero_mul, single_zero] end Rep open scoped TensorProduct open Representation /-- The `k[G]`-linear isomorphism `k[G] ⊗ₖ k[Gⁿ] ≃ k[Gⁿ⁺¹]`, where the `k[G]`-module structure on the lefthand side is `TensorProduct.leftModule`, whilst that of the righthand side comes from `Representation.asModule`. Allows us to use `Algebra.TensorProduct.basis` to get a `k[G]`-basis of the righthand side. -/ def ofMulActionBasisAux : MonoidAlgebra k G ⊗[k] ((Fin n → G) →₀ k) ≃ₗ[MonoidAlgebra k G] (ofMulAction k G (Fin (n + 1) → G)).asModule := haveI e := (Rep.equivalenceModuleMonoidAlgebra.1.mapIso (diagonalSucc k G n).symm).toLinearEquiv { e with map_smul' := fun r x => by rw [RingHom.id_apply, LinearEquiv.toFun_eq_coe, ← LinearEquiv.map_smul e] congr 1 /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11039): broken proof was refine' x.induction_on _ (fun x y => _) fun y z hy hz => _ · simp only [smul_zero] · simp only [TensorProduct.smul_tmul'] show (r * x) ⊗ₜ y = _ rw [← ofMulAction_self_smul_eq_mul, smul_tprod_one_asModule] · rw [smul_add, hz, hy, smul_add] -/ show _ = Representation.asAlgebraHom (tensorObj (Rep.leftRegular k G) (Rep.trivial k G ((Fin n → G) →₀ k))).ρ r _ refine x.induction_on ?_ (fun x y => ?_) fun y z hy hz => ?_ · rw [smul_zero, map_zero] · rw [TensorProduct.smul_tmul', smul_eq_mul, ← ofMulAction_self_smul_eq_mul] exact (smul_tprod_one_asModule (Representation.ofMulAction k G G) r x y).symm · rw [smul_add, hz, hy, map_add] } /-- A `k[G]`-basis of `k[Gⁿ⁺¹]`, coming from the `k[G]`-linear isomorphism `k[G] ⊗ₖ k[Gⁿ] ≃ k[Gⁿ⁺¹].` -/ def ofMulActionBasis : Basis (Fin n → G) (MonoidAlgebra k G) (ofMulAction k G (Fin (n + 1) → G)).asModule := Basis.map (Algebra.TensorProduct.basis (MonoidAlgebra k G) (Finsupp.basisSingleOne : Basis (Fin n → G) k ((Fin n → G) →₀ k))) (ofMulActionBasisAux k G n) theorem ofMulAction_free : Module.Free (MonoidAlgebra k G) (ofMulAction k G (Fin (n + 1) → G)).asModule := Module.Free.of_basis (ofMulActionBasis k G n) end Basis end groupCohomology.resolution namespace Rep variable (n) [Group G] (A : Rep k G) open groupCohomology.resolution /-- Given a `k`-linear `G`-representation `A`, the set of representation morphisms `Hom(k[Gⁿ⁺¹], A)` is `k`-linearly isomorphic to the set of functions `Gⁿ → A`. -/ noncomputable def diagonalHomEquiv : (Rep.diagonal k G (n + 1) ⟶ A) ≃ₗ[k] (Fin n → G) → A := Linear.homCongr k ((diagonalSucc k G n).trans ((Representation.ofMulAction k G G).repOfTprodIso 1)) (Iso.refl _) ≪≫ₗ (Rep.MonoidalClosed.linearHomEquivComm _ _ _ ≪≫ₗ Rep.leftRegularHomEquiv _) ≪≫ₗ (Finsupp.llift A k k (Fin n → G)).symm variable {n A} /-- Given a `k`-linear `G`-representation `A`, `diagonalHomEquiv` is a `k`-linear isomorphism of the set of representation morphisms `Hom(k[Gⁿ⁺¹], A)` with `Fun(Gⁿ, A)`. This lemma says that this sends a morphism of representations `f : k[Gⁿ⁺¹] ⟶ A` to the function `(g₁, ..., gₙ) ↦ f(1, g₁, g₁g₂, ..., g₁g₂...gₙ).` -/ theorem diagonalHomEquiv_apply (f : Rep.diagonal k G (n + 1) ⟶ A) (x : Fin n → G) : diagonalHomEquiv n A f x = f.hom (Finsupp.single (Fin.partialProd x) 1) := by /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11039): broken proof was unfold diagonalHomEquiv simpa only [LinearEquiv.trans_apply, Rep.leftRegularHomEquiv_apply, MonoidalClosed.linearHomEquivComm_hom, Finsupp.llift_symm_apply, TensorProduct.curry_apply, Linear.homCongr_apply, Iso.refl_hom, Iso.trans_inv, Action.comp_hom, ModuleCat.comp_def, LinearMap.comp_apply, Representation.repOfTprodIso_inv_apply, diagonalSucc_inv_single_single (1 : G) x, one_smul, one_mul] -/ change f.hom ((diagonalSucc k G n).inv.hom (Finsupp.single 1 1 ⊗ₜ[k] Finsupp.single x 1)) = _ rw [diagonalSucc_inv_single_single, one_smul, one_mul] /-- Given a `k`-linear `G`-representation `A`, `diagonalHomEquiv` is a `k`-linear isomorphism of the set of representation morphisms `Hom(k[Gⁿ⁺¹], A)` with `Fun(Gⁿ, A)`. This lemma says that the inverse map sends a function `f : Gⁿ → A` to the representation morphism sending `(g₀, ... gₙ) ↦ ρ(g₀)(f(g₀⁻¹g₁, g₁⁻¹g₂, ..., gₙ₋₁⁻¹gₙ))`, where `ρ` is the representation attached to `A`. -/ theorem diagonalHomEquiv_symm_apply (f : (Fin n → G) → A) (x : Fin (n + 1) → G) : ((diagonalHomEquiv n A).symm f).hom (Finsupp.single x 1) = A.ρ (x 0) (f fun i : Fin n => (x (Fin.castSucc i))⁻¹ * x i.succ) := by unfold diagonalHomEquiv /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11039): broken proof was simp only [LinearEquiv.trans_symm, LinearEquiv.symm_symm, LinearEquiv.trans_apply, Rep.leftRegularHomEquiv_symm_apply, Linear.homCongr_symm_apply, Action.comp_hom, Iso.refl_inv, Category.comp_id, Rep.MonoidalClosed.linearHomEquivComm_symm_hom, Iso.trans_hom, ModuleCat.comp_def, LinearMap.comp_apply, Representation.repOfTprodIso_apply, diagonalSucc_hom_single x (1 : k), TensorProduct.uncurry_apply, Rep.leftRegularHom_hom, Finsupp.lift_apply, ihom_obj_ρ_def, Rep.ihom_obj_ρ_apply, Finsupp.sum_single_index, zero_smul, one_smul, Rep.of_ρ, Rep.Action_ρ_eq_ρ, Rep.trivial_def (x 0)⁻¹, Finsupp.llift_apply A k k] -/ simp only [LinearEquiv.trans_symm, LinearEquiv.symm_symm, LinearEquiv.trans_apply, leftRegularHomEquiv_symm_apply, Linear.homCongr_symm_apply, Iso.trans_hom, Iso.refl_inv, Category.comp_id, Action.comp_hom, MonoidalClosed.linearHomEquivComm_symm_hom, ModuleCat.hom_comp, LinearMap.comp_apply] rw [diagonalSucc_hom_single] -- The prototype linter that checks if `erw` could be replaced with `rw` would time out -- if it replaces the next `erw`s with `rw`s. So we focus down on the relevant part. conv_lhs => erw [TensorProduct.uncurry_apply, Finsupp.lift_apply, Finsupp.sum_single_index] · simp only [one_smul] erw [Representation.linHom_apply] simp only [LinearMap.comp_apply, MonoidHom.one_apply, Module.End.one_apply] erw [Finsupp.llift_apply] rw [Finsupp.lift_apply] erw [Finsupp.sum_single_index] · rw [one_smul] · rw [zero_smul] · rw [zero_smul] /-- Auxiliary lemma for defining group cohomology, used to show that the isomorphism `diagonalHomEquiv` commutes with the differentials in two complexes which compute group cohomology. -/ theorem diagonalHomEquiv_symm_partialProd_succ (f : (Fin n → G) → A) (g : Fin (n + 1) → G) (a : Fin (n + 1)) : ((diagonalHomEquiv n A).symm f).hom (Finsupp.single (Fin.partialProd g ∘ a.succ.succAbove) 1) = f (Fin.contractNth a (· * ·) g) := by simp only [diagonalHomEquiv_symm_apply, Function.comp_apply, Fin.succ_succAbove_zero, Fin.partialProd_zero, map_one, Fin.succ_succAbove_succ, Module.End.one_apply, Fin.partialProd_succ] congr ext rw [← Fin.partialProd_succ, Fin.inv_partialProd_mul_eq_contractNth] end Rep variable (G) /-- The simplicial `G`-set sending `[n]` to `Gⁿ⁺¹` equipped with the diagonal action of `G`. -/ def classifyingSpaceUniversalCover [Monoid G] : SimplicialObject (Action (Type u) G) where obj n := Action.ofMulAction G (Fin (n.unop.len + 1) → G) map f := { hom := fun x => x ∘ f.unop.toOrderHom comm := fun _ => rfl } map_id _ := rfl map_comp _ _ := rfl namespace classifyingSpaceUniversalCover open CategoryTheory CategoryTheory.Limits variable [Monoid G] /-- When the category is `G`-Set, `cechNerveTerminalFrom` of `G` with the left regular action is isomorphic to `EG`, the universal cover of the classifying space of `G` as a simplicial `G`-set. -/ def cechNerveTerminalFromIso : cechNerveTerminalFrom (Action.ofMulAction G G) ≅ classifyingSpaceUniversalCover G := NatIso.ofComponents (fun _ => limit.isoLimitCone (Action.ofMulActionLimitCone _ _)) fun f => by refine IsLimit.hom_ext (Action.ofMulActionLimitCone.{u, 0} G fun _ => G).2 fun j => ?_ dsimp only [cechNerveTerminalFrom, Pi.lift] rw [Category.assoc, limit.isoLimitCone_hom_π, limit.lift_π, Category.assoc] exact (limit.isoLimitCone_hom_π _ _).symm /-- As a simplicial set, `cechNerveTerminalFrom` of a monoid `G` is isomorphic to the universal cover of the classifying space of `G` as a simplicial set. -/ def cechNerveTerminalFromIsoCompForget : cechNerveTerminalFrom G ≅ classifyingSpaceUniversalCover G ⋙ forget _ := NatIso.ofComponents (fun _ => Types.productIso _) fun _ => Matrix.ext fun _ _ => Types.Limit.lift_π_apply (Discrete.functor fun _ ↦ G) _ _ _ variable (k) open AlgebraicTopology SimplicialObject.Augmented SimplicialObject CategoryTheory.Arrow
/-- The universal cover of the classifying space of `G` as a simplicial set, augmented by the map from `Fin 1 → G` to the terminal object in `Type u`. -/ def compForgetAugmented : SimplicialObject.Augmented (Type u) := SimplicialObject.augment (classifyingSpaceUniversalCover G ⋙ forget _) (terminal _) (terminal.from _) fun _ _ _ => Subsingleton.elim _ _ /-- The augmented Čech nerve of the map from `Fin 1 → G` to the terminal object in `Type u` has an extra degeneracy. -/ def extraDegeneracyAugmentedCechNerve :
Mathlib/RepresentationTheory/GroupCohomology/Resolution.lean
401
410
/- 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
Mathlib/MeasureTheory/Function/ConditionalExpectation/AEMeasurable.lean
387
393
/- 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.Data.Finset.Lattice.Prod import Mathlib.Data.Fintype.Powerset import Mathlib.Data.Setoid.Basic import Mathlib.Order.Atoms import Mathlib.Order.SupIndep import Mathlib.Data.Set.Finite.Basic import Mathlib.Algebra.BigOperators.Group.Finset.Basic /-! # Finite partitions In this file, we define finite partitions. A finpartition of `a : α` is a finite set of pairwise disjoint parts `parts : Finset α` which does not contain `⊥` and whose supremum is `a`. Finpartitions of a finset are at the heart of Szemerédi's regularity lemma. They are also studied purely order theoretically in Sperner theory. ## Constructions We provide many ways to build finpartitions: * `Finpartition.ofErase`: Builds a finpartition by erasing `⊥` for you. * `Finpartition.ofSubset`: Builds a finpartition from a subset of the parts of a previous finpartition. * `Finpartition.empty`: The empty finpartition of `⊥`. * `Finpartition.indiscrete`: The indiscrete, aka trivial, aka pure, finpartition made of a single part. * `Finpartition.discrete`: The discrete finpartition of `s : Finset α` made of singletons. * `Finpartition.bind`: Puts together the finpartitions of the parts of a finpartition into a new finpartition. * `Finpartition.ofExistsUnique`: Builds a finpartition from a collection of parts such that each element is in exactly one part. * `Finpartition.ofSetoid`: With `Fintype α`, constructs the finpartition of `univ : Finset α` induced by the equivalence classes of `s : Setoid α`. * `Finpartition.atomise`: Makes a finpartition of `s : Finset α` by breaking `s` along all finsets in `F : Finset (Finset α)`. Two elements of `s` belong to the same part iff they belong to the same elements of `F`. `Finpartition.indiscrete` and `Finpartition.bind` together form the monadic structure of `Finpartition`. ## Implementation notes Forbidding `⊥` as a part follows mathematical tradition and is a pragmatic choice concerning operations on `Finpartition`. Not caring about `⊥` being a part or not breaks extensionality (it's not because the parts of `P` and the parts of `Q` have the same elements that `P = Q`). Enforcing `⊥` to be a part makes `Finpartition.bind` uglier and doesn't rid us of the need of `Finpartition.ofErase`. ## TODO The order is the wrong way around to make `Finpartition a` a graded order. Is it bad to depart from the literature and turn the order around? The specialisation to `Finset α` could be generalised to atomistic orders. -/ open Finset Function variable {α : Type*} /-- A finite partition of `a : α` is a pairwise disjoint finite set of elements whose supremum is `a`. We forbid `⊥` as a part. -/ @[ext] structure Finpartition [Lattice α] [OrderBot α] (a : α) where /-- The elements of the finite partition of `a` -/ parts : Finset α /-- The partition is supremum-independent -/ protected supIndep : parts.SupIndep id /-- The supremum of the partition is `a` -/ sup_parts : parts.sup id = a /-- No element of the partition is bottom -/ not_bot_mem : ⊥ ∉ parts deriving DecidableEq namespace Finpartition section Lattice variable [Lattice α] [OrderBot α] /-- A `Finpartition` constructor which does not insist on `⊥` not being a part. -/ @[simps] def ofErase [DecidableEq α] {a : α} (parts : Finset α) (sup_indep : parts.SupIndep id) (sup_parts : parts.sup id = a) : Finpartition a where parts := parts.erase ⊥ supIndep := sup_indep.subset (erase_subset _ _) sup_parts := (sup_erase_bot _).trans sup_parts not_bot_mem := not_mem_erase _ _ /-- A `Finpartition` constructor from a bigger existing finpartition. -/ @[simps] def ofSubset {a b : α} (P : Finpartition a) {parts : Finset α} (subset : parts ⊆ P.parts) (sup_parts : parts.sup id = b) : Finpartition b := { parts := parts supIndep := P.supIndep.subset subset sup_parts := sup_parts not_bot_mem := fun h ↦ P.not_bot_mem (subset h) } /-- Changes the type of a finpartition to an equal one. -/ @[simps] def copy {a b : α} (P : Finpartition a) (h : a = b) : Finpartition b where parts := P.parts supIndep := P.supIndep sup_parts := h ▸ P.sup_parts not_bot_mem := P.not_bot_mem /-- Transfer a finpartition over an order isomorphism. -/ def map {β : Type*} [Lattice β] [OrderBot β] {a : α} (e : α ≃o β) (P : Finpartition a) : Finpartition (e a) where parts := P.parts.map e supIndep u hu _ hb hbu _ hx hxu := by rw [← map_symm_subset] at hu simp only [mem_map_equiv] at hb have := P.supIndep hu hb (by simp [hbu]) (map_rel e.symm hx) ?_ · rw [← e.symm.map_bot] at this exact e.symm.map_rel_iff.mp this · convert e.symm.map_rel_iff.mpr hxu rw [map_finset_sup, sup_map] rfl sup_parts := by simp [← P.sup_parts] not_bot_mem := by rw [mem_map_equiv] convert P.not_bot_mem exact e.symm.map_bot @[simp] theorem parts_map {β : Type*} [Lattice β] [OrderBot β] {a : α} {e : α ≃o β} {P : Finpartition a} : (P.map e).parts = P.parts.map e := rfl variable (α) /-- The empty finpartition. -/ @[simps] protected def empty : Finpartition (⊥ : α) where parts := ∅ supIndep := supIndep_empty _ sup_parts := Finset.sup_empty not_bot_mem := not_mem_empty ⊥ instance : Inhabited (Finpartition (⊥ : α)) := ⟨Finpartition.empty α⟩ @[simp] theorem default_eq_empty : (default : Finpartition (⊥ : α)) = Finpartition.empty α := rfl variable {α} {a : α} /-- The finpartition in one part, aka indiscrete finpartition. -/ @[simps] def indiscrete (ha : a ≠ ⊥) : Finpartition a where parts := {a} supIndep := supIndep_singleton _ _ sup_parts := Finset.sup_singleton not_bot_mem h := ha (mem_singleton.1 h).symm variable (P : Finpartition a) protected theorem le {b : α} (hb : b ∈ P.parts) : b ≤ a := (le_sup hb).trans P.sup_parts.le theorem ne_bot {b : α} (hb : b ∈ P.parts) : b ≠ ⊥ := by intro h refine P.not_bot_mem (?_) rw [h] at hb exact hb protected theorem disjoint : (P.parts : Set α).PairwiseDisjoint id := P.supIndep.pairwiseDisjoint variable {P} @[simp] theorem parts_eq_empty_iff : P.parts = ∅ ↔ a = ⊥ := by simp_rw [← P.sup_parts] refine ⟨fun h ↦ ?_, fun h ↦ eq_empty_iff_forall_not_mem.2 fun b hb ↦ P.not_bot_mem ?_⟩ · rw [h] exact Finset.sup_empty · rwa [← le_bot_iff.1 ((le_sup hb).trans h.le)] @[simp] theorem parts_nonempty_iff : P.parts.Nonempty ↔ a ≠ ⊥ := by rw [nonempty_iff_ne_empty, not_iff_not, parts_eq_empty_iff] theorem parts_nonempty (P : Finpartition a) (ha : a ≠ ⊥) : P.parts.Nonempty := parts_nonempty_iff.2 ha instance : Unique (Finpartition (⊥ : α)) := { (inferInstance : Inhabited (Finpartition (⊥ : α))) with uniq := fun P ↦ by ext a exact iff_of_false (fun h ↦ P.ne_bot h <| le_bot_iff.1 <| P.le h) (not_mem_empty a) } -- See note [reducible non instances] /-- There's a unique partition of an atom. -/ abbrev _root_.IsAtom.uniqueFinpartition (ha : IsAtom a) : Unique (Finpartition a) where default := indiscrete ha.1 uniq P := by have h : ∀ b ∈ P.parts, b = a := fun _ hb ↦ (ha.le_iff.mp <| P.le hb).resolve_left (P.ne_bot hb) ext b refine Iff.trans ⟨h b, ?_⟩ mem_singleton.symm rintro rfl obtain ⟨c, hc⟩ := P.parts_nonempty ha.1 simp_rw [← h c hc] exact hc instance [Fintype α] [DecidableEq α] (a : α) : Fintype (Finpartition a) := @Fintype.ofSurjective { p : Finset α // p.SupIndep id ∧ p.sup id = a ∧ ⊥ ∉ p } (Finpartition a) _ (Subtype.fintype _) (fun i ↦ ⟨i.1, i.2.1, i.2.2.1, i.2.2.2⟩) fun ⟨_, y, z, w⟩ ↦ ⟨⟨_, y, z, w⟩, rfl⟩ /-! ### Refinement order -/ section Order /-- We say that `P ≤ Q` if `P` refines `Q`: each part of `P` is less than some part of `Q`. -/ instance : LE (Finpartition a) := ⟨fun P Q ↦ ∀ ⦃b⦄, b ∈ P.parts → ∃ c ∈ Q.parts, b ≤ c⟩ instance : PartialOrder (Finpartition a) := { (inferInstance : LE (Finpartition a)) with le_refl := fun _ b hb ↦ ⟨b, hb, le_rfl⟩ le_trans := fun _ Q R hPQ hQR b hb ↦ by obtain ⟨c, hc, hbc⟩ := hPQ hb obtain ⟨d, hd, hcd⟩ := hQR hc exact ⟨d, hd, hbc.trans hcd⟩ le_antisymm := fun P Q hPQ hQP ↦ by ext b refine ⟨fun hb ↦ ?_, fun hb ↦ ?_⟩ · obtain ⟨c, hc, hbc⟩ := hPQ hb obtain ⟨d, hd, hcd⟩ := hQP hc rwa [hbc.antisymm] rwa [P.disjoint.eq_of_le hb hd (P.ne_bot hb) (hbc.trans hcd)] · obtain ⟨c, hc, hbc⟩ := hQP hb obtain ⟨d, hd, hcd⟩ := hPQ hc rwa [hbc.antisymm] rwa [Q.disjoint.eq_of_le hb hd (Q.ne_bot hb) (hbc.trans hcd)] } instance [Decidable (a = ⊥)] : OrderTop (Finpartition a) where top := if ha : a = ⊥ then (Finpartition.empty α).copy ha.symm else indiscrete ha le_top P := by split_ifs with h · intro x hx simpa [h, P.ne_bot hx] using P.le hx · exact fun b hb ↦ ⟨a, mem_singleton_self _, P.le hb⟩ theorem parts_top_subset (a : α) [Decidable (a = ⊥)] : (⊤ : Finpartition a).parts ⊆ {a} := by intro b hb have hb : b ∈ Finpartition.parts (dite _ _ _) := hb split_ifs at hb · simp only [copy_parts, empty_parts, not_mem_empty] at hb · exact hb theorem parts_top_subsingleton (a : α) [Decidable (a = ⊥)] : ((⊤ : Finpartition a).parts : Set α).Subsingleton := Set.subsingleton_of_subset_singleton fun _ hb ↦ mem_singleton.1 <| parts_top_subset _ hb -- TODO: this instance takes double-exponential time to generate all partitions, find a faster way instance [DecidableEq α] {s : Finset α} : Fintype (Finpartition s) where elems := s.powerset.powerset.image fun ps ↦ if h : ps.sup id = s ∧ ⊥ ∉ ps ∧ ps.SupIndep id then ⟨ps, h.2.2, h.1, h.2.1⟩ else ⊤ complete P := by refine mem_image.mpr ⟨P.parts, ?_, ?_⟩ · rw [mem_powerset]; intro p hp; rw [mem_powerset]; exact P.le hp · simp [P.supIndep, P.sup_parts, P.not_bot_mem, -bot_eq_empty] end Order end Lattice section DistribLattice variable [DistribLattice α] [OrderBot α] section Inf variable [DecidableEq α] {a b c : α} instance : Min (Finpartition a) := ⟨fun P Q ↦ ofErase ((P.parts ×ˢ Q.parts).image fun bc ↦ bc.1 ⊓ bc.2) (by rw [supIndep_iff_disjoint_erase] simp only [mem_image, and_imp, exists_prop, forall_exists_index, id, Prod.exists, mem_product, Finset.disjoint_sup_right, mem_erase, Ne] rintro _ x₁ y₁ hx₁ hy₁ rfl _ h x₂ y₂ hx₂ hy₂ rfl rcases eq_or_ne x₁ x₂ with (rfl | xdiff) · refine Disjoint.mono inf_le_right inf_le_right (Q.disjoint hy₁ hy₂ ?_) intro t simp [t] at h exact Disjoint.mono inf_le_left inf_le_left (P.disjoint hx₁ hx₂ xdiff)) (by rw [sup_image, id_comp, sup_product_left] trans P.parts.sup id ⊓ Q.parts.sup id · simp_rw [Finset.sup_inf_distrib_right, Finset.sup_inf_distrib_left] rfl · rw [P.sup_parts, Q.sup_parts, inf_idem])⟩ @[simp] theorem parts_inf (P Q : Finpartition a) : (P ⊓ Q).parts = ((P.parts ×ˢ Q.parts).image fun bc : α × α ↦ bc.1 ⊓ bc.2).erase ⊥ := rfl instance : SemilatticeInf (Finpartition a) := { inf := Min.min inf_le_left := fun P Q b hb ↦ by obtain ⟨c, hc, rfl⟩ := mem_image.1 (mem_of_mem_erase hb) rw [mem_product] at hc exact ⟨c.1, hc.1, inf_le_left⟩ inf_le_right := fun P Q b hb ↦ by obtain ⟨c, hc, rfl⟩ := mem_image.1 (mem_of_mem_erase hb) rw [mem_product] at hc exact ⟨c.2, hc.2, inf_le_right⟩ le_inf := fun P Q R hPQ hPR b hb ↦ by obtain ⟨c, hc, hbc⟩ := hPQ hb obtain ⟨d, hd, hbd⟩ := hPR hb have h := _root_.le_inf hbc hbd refine ⟨c ⊓ d, mem_erase_of_ne_of_mem (ne_bot_of_le_ne_bot (P.ne_bot hb) h) (mem_image.2 ⟨(c, d), mem_product.2 ⟨hc, hd⟩, rfl⟩), h⟩ } end Inf theorem exists_le_of_le {a b : α} {P Q : Finpartition a} (h : P ≤ Q) (hb : b ∈ Q.parts) : ∃ c ∈ P.parts, c ≤ b := by by_contra H refine Q.ne_bot hb (disjoint_self.1 <| Disjoint.mono_right (Q.le hb) ?_) rw [← P.sup_parts, Finset.disjoint_sup_right] rintro c hc obtain ⟨d, hd, hcd⟩ := h hc refine (Q.disjoint hb hd ?_).mono_right hcd rintro rfl simp only [not_exists, not_and] at H exact H _ hc hcd theorem card_mono {a : α} {P Q : Finpartition a} (h : P ≤ Q) : #Q.parts ≤ #P.parts := by classical have : ∀ b ∈ Q.parts, ∃ c ∈ P.parts, c ≤ b := fun b ↦ exists_le_of_le h choose f hP hf using this rw [← card_attach] refine card_le_card_of_injOn (fun b ↦ f _ b.2) (fun b _ ↦ hP _ b.2) fun b _ c _ h ↦ ?_ exact Subtype.coe_injective (Q.disjoint.elim b.2 c.2 fun H ↦ P.ne_bot (hP _ b.2) <| disjoint_self.1 <| H.mono (hf _ b.2) <| h.le.trans <| hf _ c.2) variable [DecidableEq α] {a b c : α} section Bind variable {P : Finpartition a} {Q : ∀ i ∈ P.parts, Finpartition i} /-- Given a finpartition `P` of `a` and finpartitions of each part of `P`, this yields the finpartition of `a` obtained by juxtaposing all the subpartitions. -/ @[simps] def bind (P : Finpartition a) (Q : ∀ i ∈ P.parts, Finpartition i) : Finpartition a where parts := P.parts.attach.biUnion fun i ↦ (Q i.1 i.2).parts supIndep := by rw [supIndep_iff_pairwiseDisjoint] rintro a ha b hb h rw [Finset.mem_coe, Finset.mem_biUnion] at ha hb obtain ⟨⟨A, hA⟩, -, ha⟩ := ha obtain ⟨⟨B, hB⟩, -, hb⟩ := hb obtain rfl | hAB := eq_or_ne A B · exact (Q A hA).disjoint ha hb h · exact (P.disjoint hA hB hAB).mono ((Q A hA).le ha) ((Q B hB).le hb) sup_parts := by simp_rw [sup_biUnion] trans (sup P.parts id) · rw [eq_comm, ← Finset.sup_attach] exact sup_congr rfl fun b _hb ↦ (Q b.1 b.2).sup_parts.symm · exact P.sup_parts not_bot_mem h := by rw [Finset.mem_biUnion] at h obtain ⟨⟨A, hA⟩, -, h⟩ := h exact (Q A hA).not_bot_mem h theorem mem_bind : b ∈ (P.bind Q).parts ↔ ∃ A hA, b ∈ (Q A hA).parts := by rw [bind, mem_biUnion] constructor · rintro ⟨⟨A, hA⟩, -, h⟩ exact ⟨A, hA, h⟩ · rintro ⟨A, hA, h⟩ exact ⟨⟨A, hA⟩, mem_attach _ ⟨A, hA⟩, h⟩ theorem card_bind (Q : ∀ i ∈ P.parts, Finpartition i) : #(P.bind Q).parts = ∑ A ∈ P.parts.attach, #(Q _ A.2).parts := by apply card_biUnion rintro ⟨b, hb⟩ - ⟨c, hc⟩ - hbc rw [Function.onFun, Finset.disjoint_left] rintro d hdb hdc rw [Ne, Subtype.mk_eq_mk] at hbc exact (Q b hb).ne_bot hdb (eq_bot_iff.2 <| (le_inf ((Q b hb).le hdb) <| (Q c hc).le hdc).trans <| (P.disjoint hb hc hbc).le_bot) end Bind /-- Adds `b` to a finpartition of `a` to make a finpartition of `a ⊔ b`. -/ @[simps] def extend (P : Finpartition a) (hb : b ≠ ⊥) (hab : Disjoint a b) (hc : a ⊔ b = c) : Finpartition c where parts := insert b P.parts supIndep := by rw [supIndep_iff_pairwiseDisjoint, coe_insert] exact P.disjoint.insert fun d hd _ ↦ hab.symm.mono_right <| P.le hd sup_parts := by rwa [sup_insert, P.sup_parts, id, _root_.sup_comm] not_bot_mem h := (mem_insert.1 h).elim hb.symm P.not_bot_mem theorem card_extend (P : Finpartition a) (b c : α) {hb : b ≠ ⊥} {hab : Disjoint a b} {hc : a ⊔ b = c} : #(P.extend hb hab hc).parts = #P.parts + 1 := card_insert_of_not_mem fun h ↦ hb <| hab.symm.eq_bot_of_le <| P.le h end DistribLattice section GeneralizedBooleanAlgebra variable [GeneralizedBooleanAlgebra α] [DecidableEq α] {a b c : α} (P : Finpartition a) /-- Restricts a finpartition to avoid a given element. -/ @[simps!] def avoid (b : α) : Finpartition (a \ b) := ofErase (P.parts.image (· \ b)) (P.disjoint.image_finset_of_le fun _ ↦ sdiff_le).supIndep (by rw [sup_image, id_comp, Finset.sup_sdiff_right, ← Function.id_def, P.sup_parts]) @[simp] theorem mem_avoid : c ∈ (P.avoid b).parts ↔ ∃ d ∈ P.parts, ¬d ≤ b ∧ d \ b = c := by simp only [avoid, ofErase, mem_erase, Ne, mem_image, exists_prop, ← exists_and_left, @and_left_comm (c ≠ ⊥)] refine exists_congr fun d ↦ and_congr_right' <| and_congr_left ?_ rintro rfl rw [sdiff_eq_bot_iff] end GeneralizedBooleanAlgebra end Finpartition /-! ### Finite partitions of finsets -/ namespace Finpartition variable [DecidableEq α] {s t u : Finset α} (P : Finpartition s) {a : α} lemma subset {a : Finset α} (ha : a ∈ P.parts) : a ⊆ s := P.le ha theorem nonempty_of_mem_parts {a : Finset α} (ha : a ∈ P.parts) : a.Nonempty := nonempty_iff_ne_empty.2 <| P.ne_bot ha @[simp] theorem not_empty_mem_parts : ∅ ∉ P.parts := P.not_bot_mem theorem ne_empty (h : t ∈ P.parts) : t ≠ ∅ := P.ne_bot h lemma eq_of_mem_parts (ht : t ∈ P.parts) (hu : u ∈ P.parts) (hat : a ∈ t) (hau : a ∈ u) : t = u := P.disjoint.elim ht hu <| not_disjoint_iff.2 ⟨a, hat, hau⟩ theorem exists_mem (ha : a ∈ s) : ∃ t ∈ P.parts, a ∈ t := by simp_rw [← P.sup_parts] at ha exact mem_sup.1 ha theorem biUnion_parts : P.parts.biUnion id = s := (sup_eq_biUnion _ _).symm.trans P.sup_parts theorem existsUnique_mem (ha : a ∈ s) : ∃! t, t ∈ P.parts ∧ a ∈ t := by obtain ⟨t, ht, ht'⟩ := P.exists_mem ha refine ⟨t, ⟨ht, ht'⟩, ?_⟩ rintro u ⟨hu, hu'⟩ exact P.eq_of_mem_parts hu ht hu' ht' /-- Construct a `Finpartition s` from a finset of finsets `parts` such that each element of `s` is in exactly one member of `parts`. This provides a converse to `Finpartition.subset`, `Finpartition.not_empty_mem_parts` and `Finpartition.existsUnique_mem`. -/ @[simps] def ofExistsUnique (parts : Finset (Finset α)) (h : ∀ p ∈ parts, p ⊆ s) (h' : ∀ a ∈ s, ∃! t ∈ parts, a ∈ t) (h'' : ∅ ∉ parts) : Finpartition s where parts := parts supIndep := by simp only [supIndep_iff_pairwiseDisjoint] intro a ha b hb hab rw [Function.onFun, Finset.disjoint_left] intro x hx hx' exact hab ((h' x (h _ ha hx)).unique ⟨ha, hx⟩ ⟨hb, hx'⟩) sup_parts := by
ext i
Mathlib/Order/Partition/Finpartition.lean
501
501
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import Mathlib.Algebra.Order.Monoid.Canonical.Defs import Mathlib.Algebra.Order.Monoid.Unbundled.OrderDual import Mathlib.Algebra.BigOperators.Group.List.Basic /-! # Big operators on a list in ordered groups This file contains the results concerning the interaction of list big operators with ordered groups/monoids. -/ variable {ι α M N : Type*} namespace List section Monoid variable [Monoid M] @[to_additive sum_le_sum] lemma Forall₂.prod_le_prod' [Preorder M] [MulRightMono M] [MulLeftMono M] {l₁ l₂ : List M} (h : Forall₂ (· ≤ ·) l₁ l₂) : l₁.prod ≤ l₂.prod := by induction h with | nil => rfl | cons hab ih ih' => simpa only [prod_cons] using mul_le_mul' hab ih' /-- If `l₁` is a sublist of `l₂` and all elements of `l₂` are greater than or equal to one, then `l₁.prod ≤ l₂.prod`. One can prove a stronger version assuming `∀ a ∈ l₂.diff l₁, 1 ≤ a` instead of `∀ a ∈ l₂, 1 ≤ a` but this lemma is not yet in `mathlib`. -/ @[to_additive sum_le_sum "If `l₁` is a sublist of `l₂` and all elements of `l₂` are nonnegative, then `l₁.sum ≤ l₂.sum`. One can prove a stronger version assuming `∀ a ∈ l₂.diff l₁, 0 ≤ a` instead of `∀ a ∈ l₂, 0 ≤ a` but this lemma is not yet in `mathlib`."] lemma Sublist.prod_le_prod' [Preorder M] [MulRightMono M] [MulLeftMono M] {l₁ l₂ : List M} (h : l₁ <+ l₂) (h₁ : ∀ a ∈ l₂, (1 : M) ≤ a) : l₁.prod ≤ l₂.prod := by induction h with | slnil => rfl | cons a _ ih' => simp only [prod_cons, forall_mem_cons] at h₁ ⊢ exact (ih' h₁.2).trans (le_mul_of_one_le_left' h₁.1) | cons₂ a _ ih' => simp only [prod_cons, forall_mem_cons] at h₁ ⊢ exact mul_le_mul_left' (ih' h₁.2) _ @[to_additive sum_le_sum] lemma SublistForall₂.prod_le_prod' [Preorder M] [MulRightMono M] [MulLeftMono M] {l₁ l₂ : List M} (h : SublistForall₂ (· ≤ ·) l₁ l₂) (h₁ : ∀ a ∈ l₂, (1 : M) ≤ a) : l₁.prod ≤ l₂.prod := let ⟨_, hall, hsub⟩ := sublistForall₂_iff.1 h hall.prod_le_prod'.trans <| hsub.prod_le_prod' h₁ @[to_additive sum_le_sum] lemma prod_le_prod' [Preorder M] [MulRightMono M] [MulLeftMono M] {l : List ι} {f g : ι → M} (h : ∀ i ∈ l, f i ≤ g i) : (l.map f).prod ≤ (l.map g).prod := Forall₂.prod_le_prod' <| by simpa @[to_additive sum_lt_sum] lemma prod_lt_prod' [Preorder M] [MulLeftStrictMono M] [MulLeftMono M] [MulRightStrictMono M] [MulRightMono M] {l : List ι} (f g : ι → M) (h₁ : ∀ i ∈ l, f i ≤ g i) (h₂ : ∃ i ∈ l, f i < g i) : (l.map f).prod < (l.map g).prod := by induction' l with i l ihl · rcases h₂ with ⟨_, ⟨⟩, _⟩ simp only [forall_mem_cons, map_cons, prod_cons] at h₁ ⊢ simp only [mem_cons, exists_eq_or_imp] at h₂ cases h₂ · exact mul_lt_mul_of_lt_of_le ‹_› (prod_le_prod' h₁.2) · exact mul_lt_mul_of_le_of_lt h₁.1 <| ihl h₁.2 ‹_› @[to_additive] lemma prod_lt_prod_of_ne_nil [Preorder M] [MulLeftStrictMono M] [MulLeftMono M] [MulRightStrictMono M] [MulRightMono M] {l : List ι} (hl : l ≠ []) (f g : ι → M) (hlt : ∀ i ∈ l, f i < g i) : (l.map f).prod < (l.map g).prod := (prod_lt_prod' f g fun i hi => (hlt i hi).le) <| (exists_mem_of_ne_nil l hl).imp fun i hi => ⟨hi, hlt i hi⟩ @[to_additive sum_le_card_nsmul] lemma prod_le_pow_card [Preorder M] [MulRightMono M] [MulLeftMono M] (l : List M) (n : M) (h : ∀ x ∈ l, x ≤ n) : l.prod ≤ n ^ l.length := by simpa only [map_id', map_const', prod_replicate] using prod_le_prod' h @[to_additive card_nsmul_le_sum] lemma pow_card_le_prod [Preorder M] [MulRightMono M] [MulLeftMono M] (l : List M) (n : M) (h : ∀ x ∈ l, n ≤ x) : n ^ l.length ≤ l.prod := @prod_le_pow_card Mᵒᵈ _ _ _ _ l n h
@[to_additive exists_lt_of_sum_lt] lemma exists_lt_of_prod_lt' [LinearOrder M] [MulRightMono M] [MulLeftMono M] {l : List ι} (f g : ι → M) (h : (l.map f).prod < (l.map g).prod) : ∃ i ∈ l, f i < g i := by contrapose! h
Mathlib/Algebra/Order/BigOperators/Group/List.lean
97
101
/- 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.MvPolynomial.Expand import Mathlib.FieldTheory.Finite.Basic import Mathlib.LinearAlgebra.Dual.Lemmas import Mathlib.LinearAlgebra.FiniteDimensional.Lemmas import Mathlib.RingTheory.MvPolynomial.Basic /-! ## Polynomials over finite fields -/ namespace MvPolynomial variable {σ : Type*} /-- A polynomial over the integers is divisible by `n : ℕ` if and only if it is zero over `ZMod n`. -/ theorem C_dvd_iff_zmod (n : ℕ) (φ : MvPolynomial σ ℤ) : C (n : ℤ) ∣ φ ↔ map (Int.castRingHom (ZMod n)) φ = 0 := C_dvd_iff_map_hom_eq_zero _ _ (CharP.intCast_eq_zero_iff (ZMod n) n) _ section frobenius variable {p : ℕ} [Fact p.Prime] theorem frobenius_zmod (f : MvPolynomial σ (ZMod p)) : frobenius _ p f = expand p f := by apply induction_on f · intro a; rw [expand_C, frobenius_def, ← C_pow, ZMod.pow_card] · simp only [map_add]; intro _ _ hf hg; rw [hf, hg] · simp only [expand_X, map_mul] intro _ _ hf; rw [hf, frobenius_def] theorem expand_zmod (f : MvPolynomial σ (ZMod p)) : expand p f = f ^ p := (frobenius_zmod _).symm end frobenius end MvPolynomial namespace MvPolynomial noncomputable section open Set LinearMap Submodule variable {K : Type*} {σ : Type*} section Indicator variable [Fintype K] [Fintype σ] /-- Over a field, this is the indicator function as an `MvPolynomial`. -/ def indicator [CommRing K] (a : σ → K) : MvPolynomial σ K := ∏ n, (1 - (X n - C (a n)) ^ (Fintype.card K - 1)) section CommRing variable [CommRing K] theorem eval_indicator_apply_eq_one (a : σ → K) : eval a (indicator a) = 1 := by nontriviality have : 0 < Fintype.card K - 1 := tsub_pos_of_lt Fintype.one_lt_card simp only [indicator, map_prod, map_sub, map_one, map_pow, eval_X, eval_C, sub_self, zero_pow this.ne', sub_zero, Finset.prod_const_one] theorem degrees_indicator (c : σ → K) : degrees (indicator c) ≤ ∑ s : σ, (Fintype.card K - 1) • {s} := by rw [indicator] classical refine degrees_prod_le.trans <| Finset.sum_le_sum fun s _ ↦ degrees_sub_le.trans ?_ rw [degrees_one, Multiset.zero_union] refine le_trans degrees_pow_le (nsmul_le_nsmul_right ?_ _) refine degrees_sub_le.trans ?_ rw [degrees_C, Multiset.union_zero] exact degrees_X' _ theorem indicator_mem_restrictDegree (c : σ → K) : indicator c ∈ restrictDegree σ K (Fintype.card K - 1) := by classical rw [mem_restrictDegree_iff_sup, indicator] intro n refine le_trans (Multiset.count_le_of_le _ <| degrees_indicator _) (le_of_eq ?_) simp_rw [← Multiset.coe_countAddMonoidHom, map_sum, AddMonoidHom.map_nsmul, Multiset.coe_countAddMonoidHom, nsmul_eq_mul, Nat.cast_id] trans · refine Finset.sum_eq_single n ?_ ?_ · intro b _ ne simp [Multiset.count_singleton, ne, if_neg (Ne.symm _)] · intro h; exact (h <| Finset.mem_univ _).elim · rw [Multiset.count_singleton_self, mul_one] end CommRing variable [Field K] theorem eval_indicator_apply_eq_zero (a b : σ → K) (h : a ≠ b) : eval a (indicator b) = 0 := by obtain ⟨i, hi⟩ : ∃ i, a i ≠ b i := by rwa [Ne, funext_iff, not_forall] at h simp only [indicator, map_prod, map_sub, map_one, map_pow, eval_X, eval_C, sub_self, Finset.prod_eq_zero_iff] refine ⟨i, Finset.mem_univ _, ?_⟩ rw [FiniteField.pow_card_sub_one_eq_one, sub_self] rwa [Ne, sub_eq_zero] end Indicator
section variable (K σ) /-- `MvPolynomial.eval` as a `K`-linear map. -/ @[simps]
Mathlib/FieldTheory/Finite/Polynomial.lean
110
116
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Frédéric Dupuis, Heather Macbeth -/ import Mathlib.Analysis.Convex.Basic import Mathlib.Analysis.InnerProductSpace.Orthogonal import Mathlib.Analysis.InnerProductSpace.Symmetric import Mathlib.Analysis.NormedSpace.RCLike import Mathlib.Analysis.RCLike.Lemmas import Mathlib.Algebra.DirectSum.Decomposition /-! # The orthogonal projection Given a nonempty complete subspace `K` of an inner product space `E`, this file constructs `K.orthogonalProjection : E →L[𝕜] K`, the orthogonal projection of `E` onto `K`. This map satisfies: for any point `u` in `E`, the point `v = K.orthogonalProjection u` in `K` minimizes the distance `‖u - v‖` to `u`. Also a linear isometry equivalence `K.reflection : E ≃ₗᵢ[𝕜] E` is constructed, by choosing, for each `u : E`, the point `K.reflection u` to satisfy `u + (K.reflection u) = 2 • K.orthogonalProjection u`. Basic API for `orthogonalProjection` and `reflection` is developed. Next, the orthogonal projection is used to prove a series of more subtle lemmas about the orthogonal complement of complete subspaces of `E` (the orthogonal complement itself was defined in `Analysis.InnerProductSpace.Orthogonal`); the lemma `Submodule.sup_orthogonal_of_completeSpace`, stating that for a complete subspace `K` of `E` we have `K ⊔ Kᗮ = ⊤`, is a typical example. ## References The orthogonal projection construction is adapted from * [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*] * [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*] The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html> -/ noncomputable section open InnerProductSpace open RCLike Real Filter open LinearMap (ker range) open Topology Finsupp variable {𝕜 E F : Type*} [RCLike 𝕜] variable [NormedAddCommGroup E] [NormedAddCommGroup F] variable [InnerProductSpace 𝕜 E] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y local notation "absR" => abs /-! ### Orthogonal projection in inner product spaces -/ -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces, -- perhaps with individual statements for the three steps below. /-- **Existence of minimizers**, aka the **Hilbert projection theorem**. Let `u` be a point in a real inner product space, and let `K` be a nonempty complete convex subset. Then there exists a (unique) `v` in `K` that minimizes the distance `‖u - v‖` to `u`. -/ theorem exists_norm_eq_iInf_of_complete_convex {K : Set F} (ne : K.Nonempty) (h₁ : IsComplete K) (h₂ : Convex ℝ K) : ∀ u : F, ∃ v ∈ K, ‖u - v‖ = ⨅ w : K, ‖u - w‖ := fun u => by let δ := ⨅ w : K, ‖u - w‖ letI : Nonempty K := ne.to_subtype have zero_le_δ : 0 ≤ δ := le_ciInf fun _ => norm_nonneg _ have δ_le : ∀ w : K, δ ≤ ‖u - w‖ := ciInf_le ⟨0, Set.forall_mem_range.2 fun _ => norm_nonneg _⟩ have δ_le' : ∀ w ∈ K, δ ≤ ‖u - w‖ := fun w hw => δ_le ⟨w, hw⟩ -- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K` -- such that `‖u - w n‖ < δ + 1 / (n + 1)` (which implies `‖u - w n‖ --> δ`); -- maybe this should be a separate lemma have exists_seq : ∃ w : ℕ → K, ∀ n, ‖u - w n‖ < δ + 1 / (n + 1) := by have hδ : ∀ n : ℕ, δ < δ + 1 / (n + 1) := fun n => lt_add_of_le_of_pos le_rfl Nat.one_div_pos_of_nat have h := fun n => exists_lt_of_ciInf_lt (hδ n) let w : ℕ → K := fun n => Classical.choose (h n) exact ⟨w, fun n => Classical.choose_spec (h n)⟩ rcases exists_seq with ⟨w, hw⟩ have norm_tendsto : Tendsto (fun n => ‖u - w n‖) atTop (𝓝 δ) := by have h : Tendsto (fun _ : ℕ => δ) atTop (𝓝 δ) := tendsto_const_nhds have h' : Tendsto (fun n : ℕ => δ + 1 / (n + 1)) atTop (𝓝 δ) := by convert h.add tendsto_one_div_add_atTop_nhds_zero_nat simp only [add_zero] exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h' (fun x => δ_le _) fun x => le_of_lt (hw _) -- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence have seq_is_cauchy : CauchySeq fun n => (w n : F) := by rw [cauchySeq_iff_le_tendsto_0] -- splits into three goals let b := fun n : ℕ => 8 * δ * (1 / (n + 1)) + 4 * (1 / (n + 1)) * (1 / (n + 1)) use fun n => √(b n) constructor -- first goal : `∀ (n : ℕ), 0 ≤ √(b n)` · intro n exact sqrt_nonneg _ constructor -- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ √(b N)` · intro p q N hp hq let wp := (w p : F) let wq := (w q : F) let a := u - wq let b := u - wp let half := 1 / (2 : ℝ) let div := 1 / ((N : ℝ) + 1) have : 4 * ‖u - half • (wq + wp)‖ * ‖u - half • (wq + wp)‖ + ‖wp - wq‖ * ‖wp - wq‖ = 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) := calc 4 * ‖u - half • (wq + wp)‖ * ‖u - half • (wq + wp)‖ + ‖wp - wq‖ * ‖wp - wq‖ = 2 * ‖u - half • (wq + wp)‖ * (2 * ‖u - half • (wq + wp)‖) + ‖wp - wq‖ * ‖wp - wq‖ := by ring _ = absR (2 : ℝ) * ‖u - half • (wq + wp)‖ * (absR (2 : ℝ) * ‖u - half • (wq + wp)‖) + ‖wp - wq‖ * ‖wp - wq‖ := by rw [abs_of_nonneg] exact zero_le_two _ = ‖(2 : ℝ) • (u - half • (wq + wp))‖ * ‖(2 : ℝ) • (u - half • (wq + wp))‖ + ‖wp - wq‖ * ‖wp - wq‖ := by simp [norm_smul] _ = ‖a + b‖ * ‖a + b‖ + ‖a - b‖ * ‖a - b‖ := by rw [smul_sub, smul_smul, mul_one_div_cancel (_root_.two_ne_zero : (2 : ℝ) ≠ 0), ← one_add_one_eq_two, add_smul] simp only [one_smul] have eq₁ : wp - wq = a - b := (sub_sub_sub_cancel_left _ _ _).symm have eq₂ : u + u - (wq + wp) = a + b := by show u + u - (wq + wp) = u - wq + (u - wp) abel rw [eq₁, eq₂] _ = 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) := parallelogram_law_with_norm ℝ _ _ have eq : δ ≤ ‖u - half • (wq + wp)‖ := by rw [smul_add] apply δ_le' apply h₂ repeat' exact Subtype.mem _ repeat' exact le_of_lt one_half_pos exact add_halves 1 have eq₁ : 4 * δ * δ ≤ 4 * ‖u - half • (wq + wp)‖ * ‖u - half • (wq + wp)‖ := by simp_rw [mul_assoc] gcongr have eq₂ : ‖a‖ ≤ δ + div := le_trans (le_of_lt <| hw q) (add_le_add_left (Nat.one_div_le_one_div hq) _) have eq₂' : ‖b‖ ≤ δ + div := le_trans (le_of_lt <| hw p) (add_le_add_left (Nat.one_div_le_one_div hp) _) rw [dist_eq_norm] apply nonneg_le_nonneg_of_sq_le_sq · exact sqrt_nonneg _ rw [mul_self_sqrt] · calc ‖wp - wq‖ * ‖wp - wq‖ = 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) - 4 * ‖u - half • (wq + wp)‖ * ‖u - half • (wq + wp)‖ := by simp [← this] _ ≤ 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) - 4 * δ * δ := by gcongr _ ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ := by gcongr _ = 8 * δ * div + 4 * div * div := by ring positivity -- third goal : `Tendsto (fun (n : ℕ) => √(b n)) atTop (𝓝 0)` suffices Tendsto (fun x ↦ √(8 * δ * x + 4 * x * x) : ℝ → ℝ) (𝓝 0) (𝓝 0) from this.comp tendsto_one_div_add_atTop_nhds_zero_nat exact Continuous.tendsto' (by fun_prop) _ _ (by simp) -- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`. -- Prove that it satisfies all requirements. rcases cauchySeq_tendsto_of_isComplete h₁ (fun n => Subtype.mem _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩ use v use hv have h_cont : Continuous fun v => ‖u - v‖ := Continuous.comp continuous_norm (Continuous.sub continuous_const continuous_id) have : Tendsto (fun n => ‖u - w n‖) atTop (𝓝 ‖u - v‖) := by convert Tendsto.comp h_cont.continuousAt w_tendsto exact tendsto_nhds_unique this norm_tendsto /-- Characterization of minimizers for the projection on a convex set in a real inner product space. -/ theorem norm_eq_iInf_iff_real_inner_le_zero {K : Set F} (h : Convex ℝ K) {u : F} {v : F} (hv : v ∈ K) : (‖u - v‖ = ⨅ w : K, ‖u - w‖) ↔ ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0 := by letI : Nonempty K := ⟨⟨v, hv⟩⟩ constructor · intro eq w hw let δ := ⨅ w : K, ‖u - w‖ let p := ⟪u - v, w - v⟫_ℝ let q := ‖w - v‖ ^ 2 have δ_le (w : K) : δ ≤ ‖u - w‖ := ciInf_le ⟨0, fun _ ⟨_, h⟩ => h ▸ norm_nonneg _⟩ _ have δ_le' (w) (hw : w ∈ K) : δ ≤ ‖u - w‖ := δ_le ⟨w, hw⟩ have (θ : ℝ) (hθ₁ : 0 < θ) (hθ₂ : θ ≤ 1) : 2 * p ≤ θ * q := by have : ‖u - v‖ ^ 2 ≤ ‖u - v‖ ^ 2 - 2 * θ * ⟪u - v, w - v⟫_ℝ + θ * θ * ‖w - v‖ ^ 2 := calc ‖u - v‖ ^ 2 _ ≤ ‖u - (θ • w + (1 - θ) • v)‖ ^ 2 := by simp only [sq]; apply mul_self_le_mul_self (norm_nonneg _) rw [eq]; apply δ_le' apply h hw hv exacts [le_of_lt hθ₁, sub_nonneg.2 hθ₂, add_sub_cancel _ _] _ = ‖u - v - θ • (w - v)‖ ^ 2 := by have : u - (θ • w + (1 - θ) • v) = u - v - θ • (w - v) := by rw [smul_sub, sub_smul, one_smul] simp only [sub_eq_add_neg, add_comm, add_left_comm, add_assoc, neg_add_rev] rw [this] _ = ‖u - v‖ ^ 2 - 2 * θ * inner (u - v) (w - v) + θ * θ * ‖w - v‖ ^ 2 := by rw [@norm_sub_sq ℝ, inner_smul_right, norm_smul] simp only [sq] show ‖u - v‖ * ‖u - v‖ - 2 * (θ * inner (u - v) (w - v)) + absR θ * ‖w - v‖ * (absR θ * ‖w - v‖) = ‖u - v‖ * ‖u - v‖ - 2 * θ * inner (u - v) (w - v) + θ * θ * (‖w - v‖ * ‖w - v‖) rw [abs_of_pos hθ₁]; ring have eq₁ : ‖u - v‖ ^ 2 - 2 * θ * inner (u - v) (w - v) + θ * θ * ‖w - v‖ ^ 2 = ‖u - v‖ ^ 2 + (θ * θ * ‖w - v‖ ^ 2 - 2 * θ * inner (u - v) (w - v)) := by abel rw [eq₁, le_add_iff_nonneg_right] at this have eq₂ : θ * θ * ‖w - v‖ ^ 2 - 2 * θ * inner (u - v) (w - v) = θ * (θ * ‖w - v‖ ^ 2 - 2 * inner (u - v) (w - v)) := by ring rw [eq₂] at this exact le_of_sub_nonneg (nonneg_of_mul_nonneg_right this hθ₁) by_cases hq : q = 0 · rw [hq] at this have : p ≤ 0 := by have := this (1 : ℝ) (by norm_num) (by norm_num) linarith exact this · have q_pos : 0 < q := lt_of_le_of_ne (sq_nonneg _) fun h ↦ hq h.symm by_contra hp rw [not_le] at hp let θ := min (1 : ℝ) (p / q) have eq₁ : θ * q ≤ p := calc θ * q ≤ p / q * q := mul_le_mul_of_nonneg_right (min_le_right _ _) (sq_nonneg _) _ = p := div_mul_cancel₀ _ hq have : 2 * p ≤ p := calc 2 * p ≤ θ * q := by exact this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num [θ]) _ ≤ p := eq₁ linarith · intro h apply le_antisymm · apply le_ciInf intro w apply nonneg_le_nonneg_of_sq_le_sq (norm_nonneg _) have := h w w.2 calc ‖u - v‖ * ‖u - v‖ ≤ ‖u - v‖ * ‖u - v‖ - 2 * inner (u - v) ((w : F) - v) := by linarith _ ≤ ‖u - v‖ ^ 2 - 2 * inner (u - v) ((w : F) - v) + ‖(w : F) - v‖ ^ 2 := by rw [sq] refine le_add_of_nonneg_right ?_ exact sq_nonneg _ _ = ‖u - v - (w - v)‖ ^ 2 := (@norm_sub_sq ℝ _ _ _ _ _ _).symm _ = ‖u - w‖ * ‖u - w‖ := by have : u - v - (w - v) = u - w := by abel rw [this, sq] · show ⨅ w : K, ‖u - w‖ ≤ (fun w : K => ‖u - w‖) ⟨v, hv⟩ apply ciInf_le use 0 rintro y ⟨z, rfl⟩ exact norm_nonneg _ variable (K : Submodule 𝕜 E) namespace Submodule /-- Existence of projections on complete subspaces. Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a (unique) `v` in `K` that minimizes the distance `‖u - v‖` to `u`. This point `v` is usually called the orthogonal projection of `u` onto `K`. -/ theorem exists_norm_eq_iInf_of_complete_subspace (h : IsComplete (↑K : Set E)) : ∀ u : E, ∃ v ∈ K, ‖u - v‖ = ⨅ w : (K : Set E), ‖u - w‖ := by letI : InnerProductSpace ℝ E := InnerProductSpace.rclikeToReal 𝕜 E letI : Module ℝ E := RestrictScalars.module ℝ 𝕜 E let K' : Submodule ℝ E := Submodule.restrictScalars ℝ K exact exists_norm_eq_iInf_of_complete_convex ⟨0, K'.zero_mem⟩ h K'.convex /-- Characterization of minimizers in the projection on a subspace, in the real case. Let `u` be a point in a real inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `‖u - v‖` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`). This is superseded by `norm_eq_iInf_iff_inner_eq_zero` that gives the same conclusion over any `RCLike` field. -/ theorem norm_eq_iInf_iff_real_inner_eq_zero (K : Submodule ℝ F) {u : F} {v : F} (hv : v ∈ K) : (‖u - v‖ = ⨅ w : (↑K : Set F), ‖u - w‖) ↔ ∀ w ∈ K, ⟪u - v, w⟫_ℝ = 0 := Iff.intro (by intro h have h : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0 := by rwa [norm_eq_iInf_iff_real_inner_le_zero] at h exacts [K.convex, hv] intro w hw have le : ⟪u - v, w⟫_ℝ ≤ 0 := by let w' := w + v have : w' ∈ K := Submodule.add_mem _ hw hv have h₁ := h w' this have h₂ : w' - v = w := by simp only [w', add_neg_cancel_right, sub_eq_add_neg] rw [h₂] at h₁ exact h₁ have ge : ⟪u - v, w⟫_ℝ ≥ 0 := by let w'' := -w + v have : w'' ∈ K := Submodule.add_mem _ (Submodule.neg_mem _ hw) hv have h₁ := h w'' this have h₂ : w'' - v = -w := by simp only [w'', neg_inj, add_neg_cancel_right, sub_eq_add_neg] rw [h₂, inner_neg_right] at h₁ linarith exact le_antisymm le ge) (by intro h have : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0 := by intro w hw let w' := w - v have : w' ∈ K := Submodule.sub_mem _ hw hv have h₁ := h w' this exact le_of_eq h₁ rwa [norm_eq_iInf_iff_real_inner_le_zero] exacts [Submodule.convex _, hv]) /-- Characterization of minimizers in the projection on a subspace. Let `u` be a point in an inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `‖u - v‖` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`) -/ theorem norm_eq_iInf_iff_inner_eq_zero {u : E} {v : E} (hv : v ∈ K) : (‖u - v‖ = ⨅ w : K, ‖u - w‖) ↔ ∀ w ∈ K, ⟪u - v, w⟫ = 0 := by letI : InnerProductSpace ℝ E := InnerProductSpace.rclikeToReal 𝕜 E letI : Module ℝ E := RestrictScalars.module ℝ 𝕜 E let K' : Submodule ℝ E := K.restrictScalars ℝ constructor · intro H have A : ∀ w ∈ K, re ⟪u - v, w⟫ = 0 := (K'.norm_eq_iInf_iff_real_inner_eq_zero hv).1 H intro w hw apply RCLike.ext · simp [A w hw] · symm calc im (0 : 𝕜) = 0 := im.map_zero _ = re ⟪u - v, (-I : 𝕜) • w⟫ := (A _ (K.smul_mem (-I) hw)).symm _ = re (-I * ⟪u - v, w⟫) := by rw [inner_smul_right] _ = im ⟪u - v, w⟫ := by simp · intro H have : ∀ w ∈ K', ⟪u - v, w⟫_ℝ = 0 := by intro w hw rw [real_inner_eq_re_inner, H w hw] exact zero_re' exact (K'.norm_eq_iInf_iff_real_inner_eq_zero hv).2 this /-- A subspace `K : Submodule 𝕜 E` has an orthogonal projection if every vector `v : E` admits an orthogonal projection to `K`. -/ class HasOrthogonalProjection (K : Submodule 𝕜 E) : Prop where exists_orthogonal (v : E) : ∃ w ∈ K, v - w ∈ Kᗮ instance (priority := 100) HasOrthogonalProjection.ofCompleteSpace [CompleteSpace K] : K.HasOrthogonalProjection where exists_orthogonal v := by rcases K.exists_norm_eq_iInf_of_complete_subspace (completeSpace_coe_iff_isComplete.mp ‹_›) v with ⟨w, hwK, hw⟩ refine ⟨w, hwK, (K.mem_orthogonal' _).2 ?_⟩ rwa [← K.norm_eq_iInf_iff_inner_eq_zero hwK] instance [K.HasOrthogonalProjection] : Kᗮ.HasOrthogonalProjection where exists_orthogonal v := by rcases HasOrthogonalProjection.exists_orthogonal (K := K) v with ⟨w, hwK, hw⟩ refine ⟨_, hw, ?_⟩ rw [sub_sub_cancel] exact K.le_orthogonal_orthogonal hwK instance HasOrthogonalProjection.map_linearIsometryEquiv [K.HasOrthogonalProjection] {E' : Type*} [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') : (K.map (f.toLinearEquiv : E →ₗ[𝕜] E')).HasOrthogonalProjection where exists_orthogonal v := by rcases HasOrthogonalProjection.exists_orthogonal (K := K) (f.symm v) with ⟨w, hwK, hw⟩ refine ⟨f w, Submodule.mem_map_of_mem hwK, Set.forall_mem_image.2 fun u hu ↦ ?_⟩ erw [← f.symm.inner_map_map, f.symm_apply_apply, map_sub, f.symm_apply_apply, hw u hu] instance HasOrthogonalProjection.map_linearIsometryEquiv' [K.HasOrthogonalProjection] {E' : Type*} [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') : (K.map f.toLinearIsometry).HasOrthogonalProjection := HasOrthogonalProjection.map_linearIsometryEquiv K f instance : (⊤ : Submodule 𝕜 E).HasOrthogonalProjection := ⟨fun v ↦ ⟨v, trivial, by simp⟩⟩ section orthogonalProjection variable [K.HasOrthogonalProjection] /-- The orthogonal projection onto a complete subspace, as an unbundled function. This definition is only intended for use in setting up the bundled version `orthogonalProjection` and should not be used once that is defined. -/ def orthogonalProjectionFn (v : E) := (HasOrthogonalProjection.exists_orthogonal (K := K) v).choose variable {K} /-- The unbundled orthogonal projection is in the given subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ theorem orthogonalProjectionFn_mem (v : E) : K.orthogonalProjectionFn v ∈ K := (HasOrthogonalProjection.exists_orthogonal (K := K) v).choose_spec.left /-- The characterization of the unbundled orthogonal projection. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ theorem orthogonalProjectionFn_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - K.orthogonalProjectionFn v, w⟫ = 0 := (K.mem_orthogonal' _).1 (HasOrthogonalProjection.exists_orthogonal (K := K) v).choose_spec.right /-- The unbundled orthogonal projection is the unique point in `K` with the orthogonality property. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ theorem eq_orthogonalProjectionFn_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : K.orthogonalProjectionFn u = v := by rw [← sub_eq_zero, ← @inner_self_eq_zero 𝕜] have hvs : K.orthogonalProjectionFn u - v ∈ K := Submodule.sub_mem K (orthogonalProjectionFn_mem u) hvm have huo : ⟪u - K.orthogonalProjectionFn u, K.orthogonalProjectionFn u - v⟫ = 0 := orthogonalProjectionFn_inner_eq_zero u _ hvs have huv : ⟪u - v, K.orthogonalProjectionFn u - v⟫ = 0 := hvo _ hvs have houv : ⟪u - v - (u - K.orthogonalProjectionFn u), K.orthogonalProjectionFn u - v⟫ = 0 := by rw [inner_sub_left, huo, huv, sub_zero] rwa [sub_sub_sub_cancel_left] at houv variable (K) theorem orthogonalProjectionFn_norm_sq (v : E) : ‖v‖ * ‖v‖ = ‖v - K.orthogonalProjectionFn v‖ * ‖v - K.orthogonalProjectionFn v‖ + ‖K.orthogonalProjectionFn v‖ * ‖K.orthogonalProjectionFn v‖ := by set p := K.orthogonalProjectionFn v have h' : ⟪v - p, p⟫ = 0 := orthogonalProjectionFn_inner_eq_zero _ _ (orthogonalProjectionFn_mem v) convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (v - p) p h' using 2 <;> simp /-- The orthogonal projection onto a complete subspace. -/ def orthogonalProjection : E →L[𝕜] K := LinearMap.mkContinuous { toFun := fun v => ⟨K.orthogonalProjectionFn v, orthogonalProjectionFn_mem v⟩ map_add' := fun x y => by have hm : K.orthogonalProjectionFn x + K.orthogonalProjectionFn y ∈ K := Submodule.add_mem K (orthogonalProjectionFn_mem x) (orthogonalProjectionFn_mem y) have ho : ∀ w ∈ K, ⟪x + y - (K.orthogonalProjectionFn x + K.orthogonalProjectionFn y), w⟫ = 0 := by intro w hw rw [add_sub_add_comm, inner_add_left, orthogonalProjectionFn_inner_eq_zero _ w hw, orthogonalProjectionFn_inner_eq_zero _ w hw, add_zero] ext simp [eq_orthogonalProjectionFn_of_mem_of_inner_eq_zero hm ho] map_smul' := fun c x => by have hm : c • K.orthogonalProjectionFn x ∈ K := Submodule.smul_mem K _ (orthogonalProjectionFn_mem x) have ho : ∀ w ∈ K, ⟪c • x - c • K.orthogonalProjectionFn x, w⟫ = 0 := by intro w hw rw [← smul_sub, inner_smul_left, orthogonalProjectionFn_inner_eq_zero _ w hw, mul_zero] ext simp [eq_orthogonalProjectionFn_of_mem_of_inner_eq_zero hm ho] } 1 fun x => by simp only [one_mul, LinearMap.coe_mk] refine le_of_pow_le_pow_left₀ two_ne_zero (norm_nonneg _) ?_ change ‖K.orthogonalProjectionFn x‖ ^ 2 ≤ ‖x‖ ^ 2 nlinarith [K.orthogonalProjectionFn_norm_sq x] variable {K} @[simp] theorem orthogonalProjectionFn_eq (v : E) : K.orthogonalProjectionFn v = (K.orthogonalProjection v : E) := rfl /-- The characterization of the orthogonal projection. -/ @[simp] theorem orthogonalProjection_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - K.orthogonalProjection v, w⟫ = 0 := orthogonalProjectionFn_inner_eq_zero v /-- The difference of `v` from its orthogonal projection onto `K` is in `Kᗮ`. -/ @[simp] theorem sub_orthogonalProjection_mem_orthogonal (v : E) : v - K.orthogonalProjection v ∈ Kᗮ := by intro w hw rw [inner_eq_zero_symm] exact orthogonalProjection_inner_eq_zero _ _ hw /-- The orthogonal projection is the unique point in `K` with the orthogonality property. -/ theorem eq_orthogonalProjection_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : (K.orthogonalProjection u : E) = v := eq_orthogonalProjectionFn_of_mem_of_inner_eq_zero hvm hvo /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ theorem eq_orthogonalProjection_of_mem_orthogonal {u v : E} (hv : v ∈ K) (hvo : u - v ∈ Kᗮ) : (K.orthogonalProjection u : E) = v := eq_orthogonalProjectionFn_of_mem_of_inner_eq_zero hv <| (Submodule.mem_orthogonal' _ _).1 hvo /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ theorem eq_orthogonalProjection_of_mem_orthogonal' {u v z : E} (hv : v ∈ K) (hz : z ∈ Kᗮ) (hu : u = v + z) : (K.orthogonalProjection u : E) = v := eq_orthogonalProjection_of_mem_orthogonal hv (by simpa [hu] ) @[simp] theorem orthogonalProjection_orthogonal_val (u : E) : (Kᗮ.orthogonalProjection u : E) = u - K.orthogonalProjection u := eq_orthogonalProjection_of_mem_orthogonal' (sub_orthogonalProjection_mem_orthogonal _) (K.le_orthogonal_orthogonal (K.orthogonalProjection u).2) <| by simp theorem orthogonalProjection_orthogonal (u : E) : Kᗮ.orthogonalProjection u = ⟨u - K.orthogonalProjection u, sub_orthogonalProjection_mem_orthogonal _⟩ := Subtype.eq <| orthogonalProjection_orthogonal_val _ /-- The orthogonal projection of `y` on `U` minimizes the distance `‖y - x‖` for `x ∈ U`. -/ theorem orthogonalProjection_minimal {U : Submodule 𝕜 E} [U.HasOrthogonalProjection] (y : E) : ‖y - U.orthogonalProjection y‖ = ⨅ x : U, ‖y - x‖ := by rw [U.norm_eq_iInf_iff_inner_eq_zero (Submodule.coe_mem _)] exact orthogonalProjection_inner_eq_zero _ /-- The orthogonal projections onto equal subspaces are coerced back to the same point in `E`. -/ theorem eq_orthogonalProjection_of_eq_submodule {K' : Submodule 𝕜 E} [K'.HasOrthogonalProjection] (h : K = K') (u : E) : (K.orthogonalProjection u : E) = (K'.orthogonalProjection u : E) := by subst h; rfl /-- The orthogonal projection sends elements of `K` to themselves. -/ @[simp] theorem orthogonalProjection_mem_subspace_eq_self (v : K) : K.orthogonalProjection v = v := by ext apply eq_orthogonalProjection_of_mem_of_inner_eq_zero <;> simp /-- A point equals its orthogonal projection if and only if it lies in the subspace. -/ theorem orthogonalProjection_eq_self_iff {v : E} : (K.orthogonalProjection v : E) = v ↔ v ∈ K := by refine ⟨fun h => ?_, fun h => eq_orthogonalProjection_of_mem_of_inner_eq_zero h ?_⟩ · rw [← h] simp · simp @[simp] theorem orthogonalProjection_eq_zero_iff {v : E} : K.orthogonalProjection v = 0 ↔ v ∈ Kᗮ := by refine ⟨fun h ↦ ?_, fun h ↦ Subtype.eq <| eq_orthogonalProjection_of_mem_orthogonal (zero_mem _) ?_⟩ · simpa [h] using sub_orthogonalProjection_mem_orthogonal (K := K) v · simpa @[simp] theorem ker_orthogonalProjection : LinearMap.ker K.orthogonalProjection = Kᗮ := by ext; exact orthogonalProjection_eq_zero_iff theorem _root_.LinearIsometry.map_orthogonalProjection {E E' : Type*} [NormedAddCommGroup E] [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E] [InnerProductSpace 𝕜 E'] (f : E →ₗᵢ[𝕜] E') (p : Submodule 𝕜 E) [p.HasOrthogonalProjection] [(p.map f.toLinearMap).HasOrthogonalProjection] (x : E) : f (p.orthogonalProjection x) = (p.map f.toLinearMap).orthogonalProjection (f x) := by refine (eq_orthogonalProjection_of_mem_of_inner_eq_zero ?_ fun y hy => ?_).symm · refine Submodule.apply_coe_mem_map _ _ rcases hy with ⟨x', hx', rfl : f x' = y⟩ rw [← f.map_sub, f.inner_map_map, orthogonalProjection_inner_eq_zero x x' hx'] theorem _root_.LinearIsometry.map_orthogonalProjection' {E E' : Type*} [NormedAddCommGroup E] [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E] [InnerProductSpace 𝕜 E'] (f : E →ₗᵢ[𝕜] E') (p : Submodule 𝕜 E) [p.HasOrthogonalProjection] [(p.map f).HasOrthogonalProjection] (x : E) : f (p.orthogonalProjection x) = (p.map f).orthogonalProjection (f x) := have : (p.map f.toLinearMap).HasOrthogonalProjection := ‹_› f.map_orthogonalProjection p x /-- Orthogonal projection onto the `Submodule.map` of a subspace. -/ theorem orthogonalProjection_map_apply {E E' : Type*} [NormedAddCommGroup E] [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E] [InnerProductSpace 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') (p : Submodule 𝕜 E) [p.HasOrthogonalProjection] (x : E') : ((p.map (f.toLinearEquiv : E →ₗ[𝕜] E')).orthogonalProjection x : E') = f (p.orthogonalProjection (f.symm x)) := by simpa only [f.coe_toLinearIsometry, f.apply_symm_apply] using (f.toLinearIsometry.map_orthogonalProjection' p (f.symm x)).symm /-- The orthogonal projection onto the trivial submodule is the zero map. -/ @[simp] theorem orthogonalProjection_bot : (⊥ : Submodule 𝕜 E).orthogonalProjection = 0 := by ext variable (K) /-- The orthogonal projection has norm `≤ 1`. -/ theorem orthogonalProjection_norm_le : ‖K.orthogonalProjection‖ ≤ 1 := LinearMap.mkContinuous_norm_le _ (by norm_num) _ variable (𝕜) theorem smul_orthogonalProjection_singleton {v : E} (w : E) : ((‖v‖ ^ 2 : ℝ) : 𝕜) • ((𝕜 ∙ v).orthogonalProjection w : E) = ⟪v, w⟫ • v := by suffices (((𝕜 ∙ v).orthogonalProjection (((‖v‖ : 𝕜) ^ 2) • w)) : E) = ⟪v, w⟫ • v by simpa using this apply eq_orthogonalProjection_of_mem_of_inner_eq_zero · rw [Submodule.mem_span_singleton] use ⟪v, w⟫ · rw [← Submodule.mem_orthogonal', Submodule.mem_orthogonal_singleton_iff_inner_left] simp [inner_sub_left, inner_smul_left, inner_self_eq_norm_sq_to_K, mul_comm] /-- Formula for orthogonal projection onto a single vector. -/ theorem orthogonalProjection_singleton {v : E} (w : E) : ((𝕜 ∙ v).orthogonalProjection w : E) = (⟪v, w⟫ / ((‖v‖ ^ 2 : ℝ) : 𝕜)) • v := by by_cases hv : v = 0 · rw [hv, eq_orthogonalProjection_of_eq_submodule (Submodule.span_zero_singleton 𝕜)] simp have hv' : ‖v‖ ≠ 0 := ne_of_gt (norm_pos_iff.mpr hv) have key : (((‖v‖ ^ 2 : ℝ) : 𝕜)⁻¹ * ((‖v‖ ^ 2 : ℝ) : 𝕜)) • (((𝕜 ∙ v).orthogonalProjection w) : E) = (((‖v‖ ^ 2 : ℝ) : 𝕜)⁻¹ * ⟪v, w⟫) • v := by simp [mul_smul, smul_orthogonalProjection_singleton 𝕜 w, -map_pow] convert key using 1 <;> field_simp [hv'] /-- Formula for orthogonal projection onto a single unit vector. -/ theorem orthogonalProjection_unit_singleton {v : E} (hv : ‖v‖ = 1) (w : E) : ((𝕜 ∙ v).orthogonalProjection w : E) = ⟪v, w⟫ • v := by rw [← smul_orthogonalProjection_singleton 𝕜 w] simp [hv] end orthogonalProjection section reflection variable [K.HasOrthogonalProjection] /-- Auxiliary definition for `reflection`: the reflection as a linear equivalence. -/ def reflectionLinearEquiv : E ≃ₗ[𝕜] E := LinearEquiv.ofInvolutive (2 • (K.subtype.comp K.orthogonalProjection.toLinearMap) - LinearMap.id) fun x => by simp [two_smul] /-- Reflection in a complete subspace of an inner product space. The word "reflection" is sometimes understood to mean specifically reflection in a codimension-one subspace, and sometimes more generally to cover operations such as reflection in a point. The definition here, of reflection in a subspace, is a more general sense of the word that includes both those common cases. -/ def reflection : E ≃ₗᵢ[𝕜] E := { K.reflectionLinearEquiv with norm_map' := by intro x let w : K := K.orthogonalProjection x let v := x - w have : ⟪v, w⟫ = 0 := orthogonalProjection_inner_eq_zero x w w.2 convert norm_sub_eq_norm_add this using 2 · rw [LinearEquiv.coe_mk, reflectionLinearEquiv, LinearEquiv.toFun_eq_coe, LinearEquiv.coe_ofInvolutive, LinearMap.sub_apply, LinearMap.id_apply, two_smul, LinearMap.add_apply, LinearMap.comp_apply, Submodule.subtype_apply, ContinuousLinearMap.coe_coe] dsimp [v] abel · simp only [v, add_sub_cancel, eq_self_iff_true] } variable {K} /-- The result of reflecting. -/ theorem reflection_apply (p : E) : K.reflection p = 2 • (K.orthogonalProjection p : E) - p := rfl /-- Reflection is its own inverse. -/ @[simp] theorem reflection_symm : K.reflection.symm = K.reflection := rfl /-- Reflection is its own inverse. -/ @[simp] theorem reflection_inv : K.reflection⁻¹ = K.reflection := rfl variable (K) /-- Reflecting twice in the same subspace. -/ @[simp] theorem reflection_reflection (p : E) : K.reflection (K.reflection p) = p := K.reflection.left_inv p /-- Reflection is involutive. -/ theorem reflection_involutive : Function.Involutive K.reflection := K.reflection_reflection /-- Reflection is involutive. -/ @[simp] theorem reflection_trans_reflection : K.reflection.trans K.reflection = LinearIsometryEquiv.refl 𝕜 E := LinearIsometryEquiv.ext <| reflection_involutive K /-- Reflection is involutive. -/ @[simp] theorem reflection_mul_reflection : K.reflection * K.reflection = 1 := reflection_trans_reflection _ theorem reflection_orthogonal_apply (v : E) : Kᗮ.reflection v = -K.reflection v := by simp [reflection_apply]; abel theorem reflection_orthogonal : Kᗮ.reflection = .trans K.reflection (.neg _) := by ext; apply reflection_orthogonal_apply variable {K} theorem reflection_singleton_apply (u v : E) : reflection (𝕜 ∙ u) v = 2 • (⟪u, v⟫ / ((‖u‖ : 𝕜) ^ 2)) • u - v := by rw [reflection_apply, orthogonalProjection_singleton, ofReal_pow] /-- A point is its own reflection if and only if it is in the subspace. -/ theorem reflection_eq_self_iff (x : E) : K.reflection x = x ↔ x ∈ K := by rw [← orthogonalProjection_eq_self_iff, reflection_apply, sub_eq_iff_eq_add', ← two_smul 𝕜, two_smul ℕ, ← two_smul 𝕜] refine (smul_right_injective E ?_).eq_iff exact two_ne_zero theorem reflection_mem_subspace_eq_self {x : E} (hx : x ∈ K) : K.reflection x = x := (reflection_eq_self_iff x).mpr hx /-- Reflection in the `Submodule.map` of a subspace. -/ theorem reflection_map_apply {E E' : Type*} [NormedAddCommGroup E] [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E] [InnerProductSpace 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') (K : Submodule 𝕜 E) [K.HasOrthogonalProjection] (x : E') : reflection (K.map (f.toLinearEquiv : E →ₗ[𝕜] E')) x = f (K.reflection (f.symm x)) := by simp [two_smul, reflection_apply, orthogonalProjection_map_apply f K x] /-- Reflection in the `Submodule.map` of a subspace. -/ theorem reflection_map {E E' : Type*} [NormedAddCommGroup E] [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E] [InnerProductSpace 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') (K : Submodule 𝕜 E) [K.HasOrthogonalProjection] : reflection (K.map (f.toLinearEquiv : E →ₗ[𝕜] E')) = f.symm.trans (K.reflection.trans f) := LinearIsometryEquiv.ext <| reflection_map_apply f K /-- Reflection through the trivial subspace {0} is just negation. -/ @[simp] theorem reflection_bot : reflection (⊥ : Submodule 𝕜 E) = LinearIsometryEquiv.neg 𝕜 := by ext; simp [reflection_apply] end reflection end Submodule section Orthogonal namespace Submodule /-- If `K₁` is complete and contained in `K₂`, `K₁` and `K₁ᗮ ⊓ K₂` span `K₂`. -/ theorem sup_orthogonal_inf_of_completeSpace {K₁ K₂ : Submodule 𝕜 E} (h : K₁ ≤ K₂) [K₁.HasOrthogonalProjection] : K₁ ⊔ K₁ᗮ ⊓ K₂ = K₂ := by ext x rw [Submodule.mem_sup] let v : K₁ := orthogonalProjection K₁ x have hvm : x - v ∈ K₁ᗮ := sub_orthogonalProjection_mem_orthogonal x constructor · rintro ⟨y, hy, z, hz, rfl⟩ exact K₂.add_mem (h hy) hz.2 · exact fun hx => ⟨v, v.prop, x - v, ⟨hvm, K₂.sub_mem hx (h v.prop)⟩, add_sub_cancel _ _⟩ variable {K} in /-- If `K` is complete, `K` and `Kᗮ` span the whole space. -/ theorem sup_orthogonal_of_completeSpace [K.HasOrthogonalProjection] : K ⊔ Kᗮ = ⊤ := by convert Submodule.sup_orthogonal_inf_of_completeSpace (le_top : K ≤ ⊤) using 2 simp /-- If `K` is complete, any `v` in `E` can be expressed as a sum of elements of `K` and `Kᗮ`. -/ theorem exists_add_mem_mem_orthogonal [K.HasOrthogonalProjection] (v : E) : ∃ y ∈ K, ∃ z ∈ Kᗮ, v = y + z := ⟨K.orthogonalProjection v, Subtype.coe_prop _, v - K.orthogonalProjection v, sub_orthogonalProjection_mem_orthogonal _, by simp⟩ /-- If `K` admits an orthogonal projection, then the orthogonal complement of its orthogonal complement is itself. -/ @[simp] theorem orthogonal_orthogonal [K.HasOrthogonalProjection] : Kᗮᗮ = K := by ext v constructor · obtain ⟨y, hy, z, hz, rfl⟩ := K.exists_add_mem_mem_orthogonal v intro hv have hz' : z = 0 := by have hyz : ⟪z, y⟫ = 0 := by simp [hz y hy, inner_eq_zero_symm] simpa [inner_add_right, hyz] using hv z hz simp [hy, hz'] · intro hv w hw rw [inner_eq_zero_symm] exact hw v hv /-- In a Hilbert space, the orthogonal complement of the orthogonal complement of a subspace `K` is the topological closure of `K`. Note that the completeness assumption is necessary. Let `E` be the space `ℕ →₀ ℝ` with inner space structure inherited from `PiLp 2 (fun _ : ℕ ↦ ℝ)`. Let `K` be the subspace of sequences with the sum of all elements equal to zero. Then `Kᗮ = ⊥`, `Kᗮᗮ = ⊤`. -/ theorem orthogonal_orthogonal_eq_closure [CompleteSpace E] : Kᗮᗮ = K.topologicalClosure := by refine le_antisymm ?_ ?_ · convert Submodule.orthogonal_orthogonal_monotone K.le_topologicalClosure using 1 rw [K.topologicalClosure.orthogonal_orthogonal] · exact K.topologicalClosure_minimal K.le_orthogonal_orthogonal Kᗮ.isClosed_orthogonal variable {K} /-- If `K` admits an orthogonal projection, `K` and `Kᗮ` are complements of each other. -/ theorem isCompl_orthogonal_of_completeSpace [K.HasOrthogonalProjection] : IsCompl K Kᗮ := ⟨K.orthogonal_disjoint, codisjoint_iff.2 Submodule.sup_orthogonal_of_completeSpace⟩ @[simp] theorem orthogonalComplement_eq_orthogonalComplement {L : Submodule 𝕜 E} [K.HasOrthogonalProjection] [L.HasOrthogonalProjection] : Kᗮ = Lᗮ ↔ K = L := ⟨fun h ↦ by simpa using congr(Submodule.orthogonal $(h)), fun h ↦ congr(Submodule.orthogonal $(h))⟩ @[simp] theorem orthogonal_eq_bot_iff [K.HasOrthogonalProjection] : Kᗮ = ⊥ ↔ K = ⊤ := by refine ⟨?_, fun h => by rw [h, Submodule.top_orthogonal_eq_bot]⟩ intro h have : K ⊔ Kᗮ = ⊤ := Submodule.sup_orthogonal_of_completeSpace rwa [h, sup_comm, bot_sup_eq] at this /-- The orthogonal projection onto `K` of an element of `Kᗮ` is zero. -/ theorem orthogonalProjection_mem_subspace_orthogonalComplement_eq_zero [K.HasOrthogonalProjection]
{v : E} (hv : v ∈ Kᗮ) : K.orthogonalProjection v = 0 := by ext convert eq_orthogonalProjection_of_mem_orthogonal (K := K) _ _ <;> simp [hv] /-- The projection into `U` from an orthogonal submodule `V` is the zero map. -/ theorem IsOrtho.orthogonalProjection_comp_subtypeL {U V : Submodule 𝕜 E} [U.HasOrthogonalProjection] (h : U ⟂ V) : U.orthogonalProjection ∘L V.subtypeL = 0 := ContinuousLinearMap.ext fun v => orthogonalProjection_mem_subspace_orthogonalComplement_eq_zero <| h.symm v.prop /-- The projection into `U` from `V` is the zero map if and only if `U` and `V` are orthogonal. -/ theorem orthogonalProjection_comp_subtypeL_eq_zero_iff {U V : Submodule 𝕜 E}
Mathlib/Analysis/InnerProductSpace/Projection.lean
813
824
/- 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.Analysis.Normed.Group.Int import Mathlib.Analysis.Normed.Group.Subgroup import Mathlib.Analysis.Normed.Group.Uniform /-! # Normed groups homomorphisms This file gathers definitions and elementary constructions about bounded group homomorphisms between normed (abelian) groups (abbreviated to "normed group homs"). The main lemmas relate the boundedness condition to continuity and Lipschitzness. The main construction is to endow the type of normed group homs between two given normed groups with a group structure and a norm, giving rise to a normed group structure. We provide several simple constructions for normed group homs, like kernel, range and equalizer. Some easy other constructions are related to subgroups of normed groups. Since a lot of elementary properties don't require `‖x‖ = 0 → x = 0` we start setting up the theory of `SeminormedAddGroupHom` and we specialize to `NormedAddGroupHom` when needed. -/ noncomputable section open NNReal -- TODO: migrate to the new morphism / morphism_class style /-- A morphism of seminormed abelian groups is a bounded group homomorphism. -/ structure NormedAddGroupHom (V W : Type*) [SeminormedAddCommGroup V] [SeminormedAddCommGroup W] where /-- The function underlying a `NormedAddGroupHom` -/ toFun : V → W /-- A `NormedAddGroupHom` is additive. -/ map_add' : ∀ v₁ v₂, toFun (v₁ + v₂) = toFun v₁ + toFun v₂ /-- A `NormedAddGroupHom` is bounded. -/ bound' : ∃ C, ∀ v, ‖toFun v‖ ≤ C * ‖v‖ namespace AddMonoidHom variable {V W : Type*} [SeminormedAddCommGroup V] [SeminormedAddCommGroup W] {f g : NormedAddGroupHom V W} /-- Associate to a group homomorphism a bounded group homomorphism under a norm control condition. See `AddMonoidHom.mkNormedAddGroupHom'` for a version that uses `ℝ≥0` for the bound. -/ def mkNormedAddGroupHom (f : V →+ W) (C : ℝ) (h : ∀ v, ‖f v‖ ≤ C * ‖v‖) : NormedAddGroupHom V W := { f with bound' := ⟨C, h⟩ } /-- Associate to a group homomorphism a bounded group homomorphism under a norm control condition. See `AddMonoidHom.mkNormedAddGroupHom` for a version that uses `ℝ` for the bound. -/ def mkNormedAddGroupHom' (f : V →+ W) (C : ℝ≥0) (hC : ∀ x, ‖f x‖₊ ≤ C * ‖x‖₊) : NormedAddGroupHom V W := { f with bound' := ⟨C, hC⟩ } end AddMonoidHom theorem exists_pos_bound_of_bound {V W : Type*} [SeminormedAddCommGroup V] [SeminormedAddCommGroup W] {f : V → W} (M : ℝ) (h : ∀ x, ‖f x‖ ≤ M * ‖x‖) : ∃ N, 0 < N ∧ ∀ x, ‖f x‖ ≤ N * ‖x‖ := ⟨max M 1, lt_of_lt_of_le zero_lt_one (le_max_right _ _), fun x => calc ‖f x‖ ≤ M * ‖x‖ := h x _ ≤ max M 1 * ‖x‖ := by gcongr; apply le_max_left ⟩ namespace NormedAddGroupHom variable {V V₁ V₂ V₃ : Type*} [SeminormedAddCommGroup V] [SeminormedAddCommGroup V₁] [SeminormedAddCommGroup V₂] [SeminormedAddCommGroup V₃] variable {f g : NormedAddGroupHom V₁ V₂} /-- A Lipschitz continuous additive homomorphism is a normed additive group homomorphism. -/ def ofLipschitz (f : V₁ →+ V₂) {K : ℝ≥0} (h : LipschitzWith K f) : NormedAddGroupHom V₁ V₂ := f.mkNormedAddGroupHom K fun x ↦ by simpa only [map_zero, dist_zero_right] using h.dist_le_mul x 0 instance funLike : FunLike (NormedAddGroupHom V₁ V₂) V₁ V₂ where coe := toFun coe_injective' f g h := by cases f; cases g; congr instance toAddMonoidHomClass : AddMonoidHomClass (NormedAddGroupHom V₁ V₂) V₁ V₂ where map_add f := f.map_add' map_zero f := (AddMonoidHom.mk' f.toFun f.map_add').map_zero initialize_simps_projections NormedAddGroupHom (toFun → apply) theorem coe_inj (H : (f : V₁ → V₂) = g) : f = g := by cases f; cases g; congr theorem coe_injective : @Function.Injective (NormedAddGroupHom V₁ V₂) (V₁ → V₂) toFun := by apply coe_inj theorem coe_inj_iff : f = g ↔ (f : V₁ → V₂) = g := ⟨congr_arg _, coe_inj⟩ @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := coe_inj <| funext H variable (f g) @[simp] theorem toFun_eq_coe : f.toFun = f := rfl theorem coe_mk (f) (h₁) (h₂) (h₃) : ⇑(⟨f, h₁, h₂, h₃⟩ : NormedAddGroupHom V₁ V₂) = f := rfl @[simp] theorem coe_mkNormedAddGroupHom (f : V₁ →+ V₂) (C) (hC) : ⇑(f.mkNormedAddGroupHom C hC) = f := rfl @[simp] theorem coe_mkNormedAddGroupHom' (f : V₁ →+ V₂) (C) (hC) : ⇑(f.mkNormedAddGroupHom' C hC) = f := rfl /-- The group homomorphism underlying a bounded group homomorphism. -/ def toAddMonoidHom (f : NormedAddGroupHom V₁ V₂) : V₁ →+ V₂ := AddMonoidHom.mk' f f.map_add' @[simp] theorem coe_toAddMonoidHom : ⇑f.toAddMonoidHom = f := rfl theorem toAddMonoidHom_injective : Function.Injective (@NormedAddGroupHom.toAddMonoidHom V₁ V₂ _ _) := fun f g h => coe_inj <| by rw [← coe_toAddMonoidHom f, ← coe_toAddMonoidHom g, h] @[simp] theorem mk_toAddMonoidHom (f) (h₁) (h₂) : (⟨f, h₁, h₂⟩ : NormedAddGroupHom V₁ V₂).toAddMonoidHom = AddMonoidHom.mk' f h₁ := rfl theorem bound : ∃ C, 0 < C ∧ ∀ x, ‖f x‖ ≤ C * ‖x‖ := let ⟨_C, hC⟩ := f.bound' exists_pos_bound_of_bound _ hC theorem antilipschitz_of_norm_ge {K : ℝ≥0} (h : ∀ x, ‖x‖ ≤ K * ‖f x‖) : AntilipschitzWith K f := AntilipschitzWith.of_le_mul_dist fun x y => by simpa only [dist_eq_norm, map_sub] using h (x - y) /-- A normed group hom is surjective on the subgroup `K` with constant `C` if every element `x` of `K` has a preimage whose norm is bounded above by `C*‖x‖`. This is a more abstract version of `f` having a right inverse defined on `K` with operator norm at most `C`. -/ def SurjectiveOnWith (f : NormedAddGroupHom V₁ V₂) (K : AddSubgroup V₂) (C : ℝ) : Prop := ∀ h ∈ K, ∃ g, f g = h ∧ ‖g‖ ≤ C * ‖h‖ theorem SurjectiveOnWith.mono {f : NormedAddGroupHom V₁ V₂} {K : AddSubgroup V₂} {C C' : ℝ} (h : f.SurjectiveOnWith K C) (H : C ≤ C') : f.SurjectiveOnWith K C' := by intro k k_in rcases h k k_in with ⟨g, rfl, hg⟩ use g, rfl by_cases Hg : ‖f g‖ = 0 · simpa [Hg] using hg · exact hg.trans (by gcongr) theorem SurjectiveOnWith.exists_pos {f : NormedAddGroupHom V₁ V₂} {K : AddSubgroup V₂} {C : ℝ} (h : f.SurjectiveOnWith K C) : ∃ C' > 0, f.SurjectiveOnWith K C' := by refine ⟨|C| + 1, ?_, ?_⟩ · linarith [abs_nonneg C] · apply h.mono linarith [le_abs_self C] theorem SurjectiveOnWith.surjOn {f : NormedAddGroupHom V₁ V₂} {K : AddSubgroup V₂} {C : ℝ} (h : f.SurjectiveOnWith K C) : Set.SurjOn f Set.univ K := fun x hx => (h x hx).imp fun _a ⟨ha, _⟩ => ⟨Set.mem_univ _, ha⟩ /-! ### The operator norm -/ /-- The operator norm of a seminormed group homomorphism is the inf of all its bounds. -/ def opNorm (f : NormedAddGroupHom V₁ V₂) := sInf { c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖ } instance hasOpNorm : Norm (NormedAddGroupHom V₁ V₂) := ⟨opNorm⟩ theorem norm_def : ‖f‖ = sInf { c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖ } := rfl -- So that invocations of `le_csInf` make sense: we show that the set of -- bounds is nonempty and bounded below. theorem bounds_nonempty {f : NormedAddGroupHom V₁ V₂} : ∃ c, c ∈ { c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖ } := let ⟨M, hMp, hMb⟩ := f.bound ⟨M, le_of_lt hMp, hMb⟩ theorem bounds_bddBelow {f : NormedAddGroupHom V₁ V₂} : BddBelow { c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖ } := ⟨0, fun _ ⟨hn, _⟩ => hn⟩ theorem opNorm_nonneg : 0 ≤ ‖f‖ := le_csInf bounds_nonempty fun _ ⟨hx, _⟩ => hx /-- The fundamental property of the operator norm: `‖f x‖ ≤ ‖f‖ * ‖x‖`. -/ theorem le_opNorm (x : V₁) : ‖f x‖ ≤ ‖f‖ * ‖x‖ := by obtain ⟨C, _Cpos, hC⟩ := f.bound replace hC := hC x by_cases h : ‖x‖ = 0 · rwa [h, mul_zero] at hC ⊢ have hlt : 0 < ‖x‖ := lt_of_le_of_ne (norm_nonneg x) (Ne.symm h) exact (div_le_iff₀ hlt).mp (le_csInf bounds_nonempty fun c ⟨_, hc⟩ => (div_le_iff₀ hlt).mpr <| by apply hc) theorem le_opNorm_of_le {c : ℝ} {x} (h : ‖x‖ ≤ c) : ‖f x‖ ≤ ‖f‖ * c := le_trans (f.le_opNorm x) (by gcongr; exact f.opNorm_nonneg) theorem le_of_opNorm_le {c : ℝ} (h : ‖f‖ ≤ c) (x : V₁) : ‖f x‖ ≤ c * ‖x‖ := (f.le_opNorm x).trans (by gcongr) /-- continuous linear maps are Lipschitz continuous. -/ theorem lipschitz : LipschitzWith ⟨‖f‖, opNorm_nonneg f⟩ f := LipschitzWith.of_dist_le_mul fun x y => by rw [dist_eq_norm, dist_eq_norm, ← map_sub] apply le_opNorm protected theorem uniformContinuous (f : NormedAddGroupHom V₁ V₂) : UniformContinuous f := f.lipschitz.uniformContinuous @[continuity] protected theorem continuous (f : NormedAddGroupHom V₁ V₂) : Continuous f := f.uniformContinuous.continuous instance : ContinuousMapClass (NormedAddGroupHom V₁ V₂) V₁ V₂ where map_continuous := fun f => f.continuous theorem ratio_le_opNorm (x : V₁) : ‖f x‖ / ‖x‖ ≤ ‖f‖ := div_le_of_le_mul₀ (norm_nonneg _) f.opNorm_nonneg (le_opNorm _ _) /-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/ theorem opNorm_le_bound {M : ℝ} (hMp : 0 ≤ M) (hM : ∀ x, ‖f x‖ ≤ M * ‖x‖) : ‖f‖ ≤ M := csInf_le bounds_bddBelow ⟨hMp, hM⟩ theorem opNorm_eq_of_bounds {M : ℝ} (M_nonneg : 0 ≤ M) (h_above : ∀ x, ‖f x‖ ≤ M * ‖x‖) (h_below : ∀ N ≥ 0, (∀ x, ‖f x‖ ≤ N * ‖x‖) → M ≤ N) : ‖f‖ = M := le_antisymm (f.opNorm_le_bound M_nonneg h_above) ((le_csInf_iff NormedAddGroupHom.bounds_bddBelow ⟨M, M_nonneg, h_above⟩).mpr fun N ⟨N_nonneg, hN⟩ => h_below N N_nonneg hN) theorem opNorm_le_of_lipschitz {f : NormedAddGroupHom V₁ V₂} {K : ℝ≥0} (hf : LipschitzWith K f) : ‖f‖ ≤ K := f.opNorm_le_bound K.2 fun x => by simpa only [dist_zero_right, map_zero] using hf.dist_le_mul x 0 /-- If a bounded group homomorphism map is constructed from a group homomorphism via the constructor `AddMonoidHom.mkNormedAddGroupHom`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ theorem mkNormedAddGroupHom_norm_le (f : V₁ →+ V₂) {C : ℝ} (hC : 0 ≤ C) (h : ∀ x, ‖f x‖ ≤ C * ‖x‖) : ‖f.mkNormedAddGroupHom C h‖ ≤ C := opNorm_le_bound _ hC h /-- If a bounded group homomorphism map is constructed from a group homomorphism via the constructor `NormedAddGroupHom.ofLipschitz`, then its norm is bounded by the bound given to the constructor. -/ theorem ofLipschitz_norm_le (f : V₁ →+ V₂) {K : ℝ≥0} (h : LipschitzWith K f) : ‖ofLipschitz f h‖ ≤ K := mkNormedAddGroupHom_norm_le f K.coe_nonneg _ /-- If a bounded group homomorphism map is constructed from a group homomorphism via the constructor `AddMonoidHom.mkNormedAddGroupHom`, then its norm is bounded by the bound given to the constructor or zero if this bound is negative. -/ theorem mkNormedAddGroupHom_norm_le' (f : V₁ →+ V₂) {C : ℝ} (h : ∀ x, ‖f x‖ ≤ C * ‖x‖) : ‖f.mkNormedAddGroupHom C h‖ ≤ max C 0 := opNorm_le_bound _ (le_max_right _ _) fun x => (h x).trans <| by gcongr; apply le_max_left alias _root_.AddMonoidHom.mkNormedAddGroupHom_norm_le := mkNormedAddGroupHom_norm_le alias _root_.AddMonoidHom.mkNormedAddGroupHom_norm_le' := mkNormedAddGroupHom_norm_le' /-! ### Addition of normed group homs -/ /-- Addition of normed group homs. -/ instance add : Add (NormedAddGroupHom V₁ V₂) := ⟨fun f g => (f.toAddMonoidHom + g.toAddMonoidHom).mkNormedAddGroupHom (‖f‖ + ‖g‖) fun v => calc ‖f v + g v‖ ≤ ‖f v‖ + ‖g v‖ := norm_add_le _ _ _ ≤ ‖f‖ * ‖v‖ + ‖g‖ * ‖v‖ := by gcongr <;> apply le_opNorm _ = (‖f‖ + ‖g‖) * ‖v‖ := by rw [add_mul] ⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem opNorm_add_le : ‖f + g‖ ≤ ‖f‖ + ‖g‖ := mkNormedAddGroupHom_norm_le _ (add_nonneg (opNorm_nonneg _) (opNorm_nonneg _)) _ @[simp] theorem coe_add (f g : NormedAddGroupHom V₁ V₂) : ⇑(f + g) = f + g := rfl @[simp] theorem add_apply (f g : NormedAddGroupHom V₁ V₂) (v : V₁) : (f + g) v = f v + g v := rfl /-! ### The zero normed group hom -/ instance zero : Zero (NormedAddGroupHom V₁ V₂) := ⟨(0 : V₁ →+ V₂).mkNormedAddGroupHom 0 (by simp)⟩ instance inhabited : Inhabited (NormedAddGroupHom V₁ V₂) := ⟨0⟩ /-- The norm of the `0` operator is `0`. -/ theorem opNorm_zero : ‖(0 : NormedAddGroupHom V₁ V₂)‖ = 0 := le_antisymm (csInf_le bounds_bddBelow ⟨ge_of_eq rfl, fun _ => le_of_eq (by rw [zero_mul] exact norm_zero)⟩) (opNorm_nonneg _) /-- For normed groups, an operator is zero iff its norm vanishes. -/ theorem opNorm_zero_iff {V₁ V₂ : Type*} [NormedAddCommGroup V₁] [NormedAddCommGroup V₂] {f : NormedAddGroupHom V₁ V₂} : ‖f‖ = 0 ↔ f = 0 := Iff.intro (fun hn => ext fun x => norm_le_zero_iff.1 (calc _ ≤ ‖f‖ * ‖x‖ := le_opNorm _ _ _ = _ := by rw [hn, zero_mul] )) fun hf => by rw [hf, opNorm_zero] @[simp] theorem coe_zero : ⇑(0 : NormedAddGroupHom V₁ V₂) = 0 := rfl @[simp] theorem zero_apply (v : V₁) : (0 : NormedAddGroupHom V₁ V₂) v = 0 := rfl variable {f g} /-! ### The identity normed group hom -/ variable (V) /-- The identity as a continuous normed group hom. -/ @[simps!] def id : NormedAddGroupHom V V := (AddMonoidHom.id V).mkNormedAddGroupHom 1 (by simp [le_refl]) /-- The norm of the identity is at most `1`. It is in fact `1`, except when the norm of every element vanishes, where it is `0`. (Since we are working with seminorms this can happen even if the space is non-trivial.) It means that one can not do better than an inequality in general. -/ theorem norm_id_le : ‖(id V : NormedAddGroupHom V V)‖ ≤ 1 := opNorm_le_bound _ zero_le_one fun x => by simp /-- If there is an element with norm different from `0`, then the norm of the identity equals `1`. (Since we are working with seminorms supposing that the space is non-trivial is not enough.) -/ theorem norm_id_of_nontrivial_seminorm (h : ∃ x : V, ‖x‖ ≠ 0) : ‖id V‖ = 1 := le_antisymm (norm_id_le V) <| by let ⟨x, hx⟩ := h have := (id V).ratio_le_opNorm x rwa [id_apply, div_self hx] at this /-- If a normed space is non-trivial, then the norm of the identity equals `1`. -/ theorem norm_id {V : Type*} [NormedAddCommGroup V] [Nontrivial V] : ‖id V‖ = 1 := by refine norm_id_of_nontrivial_seminorm V ?_ obtain ⟨x, hx⟩ := exists_ne (0 : V) exact ⟨x, ne_of_gt (norm_pos_iff.2 hx)⟩ theorem coe_id : (NormedAddGroupHom.id V : V → V) = _root_.id := rfl /-! ### The negation of a normed group hom -/ /-- Opposite of a normed group hom. -/ instance neg : Neg (NormedAddGroupHom V₁ V₂) := ⟨fun f => (-f.toAddMonoidHom).mkNormedAddGroupHom ‖f‖ fun v => by simp [le_opNorm f v]⟩ @[simp] theorem coe_neg (f : NormedAddGroupHom V₁ V₂) : ⇑(-f) = -f := rfl @[simp] theorem neg_apply (f : NormedAddGroupHom V₁ V₂) (v : V₁) : (-f : NormedAddGroupHom V₁ V₂) v = -f v := rfl theorem opNorm_neg (f : NormedAddGroupHom V₁ V₂) : ‖-f‖ = ‖f‖ := by simp only [norm_def, coe_neg, norm_neg, Pi.neg_apply] /-! ### Subtraction of normed group homs -/ /-- Subtraction of normed group homs. -/ instance sub : Sub (NormedAddGroupHom V₁ V₂) := ⟨fun f g => { f.toAddMonoidHom - g.toAddMonoidHom with bound' := by simp only [AddMonoidHom.sub_apply, AddMonoidHom.toFun_eq_coe, sub_eq_add_neg] exact (f + -g).bound' }⟩ @[simp] theorem coe_sub (f g : NormedAddGroupHom V₁ V₂) : ⇑(f - g) = f - g := rfl @[simp] theorem sub_apply (f g : NormedAddGroupHom V₁ V₂) (v : V₁) : (f - g : NormedAddGroupHom V₁ V₂) v = f v - g v := rfl /-! ### Scalar actions on normed group homs -/ section SMul variable {R R' : Type*} [MonoidWithZero R] [DistribMulAction R V₂] [PseudoMetricSpace R] [IsBoundedSMul R V₂] [MonoidWithZero R'] [DistribMulAction R' V₂] [PseudoMetricSpace R'] [IsBoundedSMul R' V₂] instance smul : SMul R (NormedAddGroupHom V₁ V₂) where smul r f := { toFun := r • ⇑f map_add' := (r • f.toAddMonoidHom).map_add' bound' := let ⟨b, hb⟩ := f.bound' ⟨dist r 0 * b, fun x => by have := dist_smul_pair r (f x) (f 0) rw [map_zero, smul_zero, dist_zero_right, dist_zero_right] at this rw [mul_assoc] refine this.trans ?_ gcongr exact hb x⟩ } @[simp] theorem coe_smul (r : R) (f : NormedAddGroupHom V₁ V₂) : ⇑(r • f) = r • ⇑f := rfl @[simp] theorem smul_apply (r : R) (f : NormedAddGroupHom V₁ V₂) (v : V₁) : (r • f) v = r • f v := rfl instance smulCommClass [SMulCommClass R R' V₂] : SMulCommClass R R' (NormedAddGroupHom V₁ V₂) where smul_comm _ _ _ := ext fun _ => smul_comm _ _ _ instance isScalarTower [SMul R R'] [IsScalarTower R R' V₂] : IsScalarTower R R' (NormedAddGroupHom V₁ V₂) where smul_assoc _ _ _ := ext fun _ => smul_assoc _ _ _ instance isCentralScalar [DistribMulAction Rᵐᵒᵖ V₂] [IsCentralScalar R V₂] : IsCentralScalar R (NormedAddGroupHom V₁ V₂) where op_smul_eq_smul _ _ := ext fun _ => op_smul_eq_smul _ _ end SMul instance nsmul : SMul ℕ (NormedAddGroupHom V₁ V₂) where smul n f := { toFun := n • ⇑f map_add' := (n • f.toAddMonoidHom).map_add' bound' := let ⟨b, hb⟩ := f.bound' ⟨n • b, fun v => by rw [Pi.smul_apply, nsmul_eq_mul, mul_assoc] exact norm_nsmul_le.trans (by gcongr; apply hb)⟩ } @[simp] theorem coe_nsmul (r : ℕ) (f : NormedAddGroupHom V₁ V₂) : ⇑(r • f) = r • ⇑f := rfl @[simp] theorem nsmul_apply (r : ℕ) (f : NormedAddGroupHom V₁ V₂) (v : V₁) : (r • f) v = r • f v := rfl instance zsmul : SMul ℤ (NormedAddGroupHom V₁ V₂) where smul z f := { toFun := z • ⇑f map_add' := (z • f.toAddMonoidHom).map_add' bound' := let ⟨b, hb⟩ := f.bound' ⟨‖z‖ • b, fun v => by rw [Pi.smul_apply, smul_eq_mul, mul_assoc] exact (norm_zsmul_le _ _).trans (by gcongr; apply hb)⟩ } @[simp] theorem coe_zsmul (r : ℤ) (f : NormedAddGroupHom V₁ V₂) : ⇑(r • f) = r • ⇑f := rfl @[simp] theorem zsmul_apply (r : ℤ) (f : NormedAddGroupHom V₁ V₂) (v : V₁) : (r • f) v = r • f v := rfl /-! ### Normed group structure on normed group homs -/ /-- Homs between two given normed groups form a commutative additive group. -/ instance toAddCommGroup : AddCommGroup (NormedAddGroupHom V₁ V₂) := coe_injective.addCommGroup _ rfl (fun _ _ => rfl) (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl /-- Normed group homomorphisms themselves form a seminormed group with respect to the operator norm. -/ instance toSeminormedAddCommGroup : SeminormedAddCommGroup (NormedAddGroupHom V₁ V₂) := AddGroupSeminorm.toSeminormedAddCommGroup { toFun := opNorm map_zero' := opNorm_zero neg' := opNorm_neg add_le' := opNorm_add_le } /-- Normed group homomorphisms themselves form a normed group with respect to the operator norm. -/ instance toNormedAddCommGroup {V₁ V₂ : Type*} [NormedAddCommGroup V₁] [NormedAddCommGroup V₂] : NormedAddCommGroup (NormedAddGroupHom V₁ V₂) := AddGroupNorm.toNormedAddCommGroup { toFun := opNorm map_zero' := opNorm_zero neg' := opNorm_neg add_le' := opNorm_add_le eq_zero_of_map_eq_zero' := fun _f => opNorm_zero_iff.1 } /-- Coercion of a `NormedAddGroupHom` is an `AddMonoidHom`. Similar to `AddMonoidHom.coeFn`. -/ @[simps] def coeAddHom : NormedAddGroupHom V₁ V₂ →+ V₁ → V₂ where toFun := DFunLike.coe map_zero' := coe_zero map_add' := coe_add @[simp] theorem coe_sum {ι : Type*} (s : Finset ι) (f : ι → NormedAddGroupHom V₁ V₂) : ⇑(∑ i ∈ s, f i) = ∑ i ∈ s, (f i : V₁ → V₂) := map_sum coeAddHom f s theorem sum_apply {ι : Type*} (s : Finset ι) (f : ι → NormedAddGroupHom V₁ V₂) (v : V₁) : (∑ i ∈ s, f i) v = ∑ i ∈ s, f i v := by simp only [coe_sum, Finset.sum_apply] /-! ### Module structure on normed group homs -/ instance distribMulAction {R : Type*} [MonoidWithZero R] [DistribMulAction R V₂] [PseudoMetricSpace R] [IsBoundedSMul R V₂] : DistribMulAction R (NormedAddGroupHom V₁ V₂) := Function.Injective.distribMulAction coeAddHom coe_injective coe_smul instance module {R : Type*} [Semiring R] [Module R V₂] [PseudoMetricSpace R] [IsBoundedSMul R V₂] : Module R (NormedAddGroupHom V₁ V₂) := Function.Injective.module _ coeAddHom coe_injective coe_smul /-! ### Composition of normed group homs -/ /-- The composition of continuous normed group homs. -/ @[simps!] protected def comp (g : NormedAddGroupHom V₂ V₃) (f : NormedAddGroupHom V₁ V₂) : NormedAddGroupHom V₁ V₃ := (g.toAddMonoidHom.comp f.toAddMonoidHom).mkNormedAddGroupHom (‖g‖ * ‖f‖) fun v => calc ‖g (f v)‖ ≤ ‖g‖ * ‖f v‖ := le_opNorm _ _ _ ≤ ‖g‖ * (‖f‖ * ‖v‖) := by gcongr; apply le_opNorm _ = ‖g‖ * ‖f‖ * ‖v‖ := by rw [mul_assoc] theorem norm_comp_le (g : NormedAddGroupHom V₂ V₃) (f : NormedAddGroupHom V₁ V₂) : ‖g.comp f‖ ≤ ‖g‖ * ‖f‖ := mkNormedAddGroupHom_norm_le _ (mul_nonneg (opNorm_nonneg _) (opNorm_nonneg _)) _ theorem norm_comp_le_of_le {g : NormedAddGroupHom V₂ V₃} {C₁ C₂ : ℝ} (hg : ‖g‖ ≤ C₂) (hf : ‖f‖ ≤ C₁) : ‖g.comp f‖ ≤ C₂ * C₁ := le_trans (norm_comp_le g f) <| by gcongr; exact le_trans (norm_nonneg _) hg theorem norm_comp_le_of_le' {g : NormedAddGroupHom V₂ V₃} (C₁ C₂ C₃ : ℝ) (h : C₃ = C₂ * C₁) (hg : ‖g‖ ≤ C₂) (hf : ‖f‖ ≤ C₁) : ‖g.comp f‖ ≤ C₃ := by rw [h] exact norm_comp_le_of_le hg hf /-- Composition of normed groups hom as an additive group morphism. -/ def compHom : NormedAddGroupHom V₂ V₃ →+ NormedAddGroupHom V₁ V₂ →+ NormedAddGroupHom V₁ V₃ := AddMonoidHom.mk' (fun g => AddMonoidHom.mk' (fun f => g.comp f) (by intros ext exact map_add g _ _)) (by intros ext simp only [comp_apply, Pi.add_apply, Function.comp_apply, AddMonoidHom.add_apply, AddMonoidHom.mk'_apply, coe_add]) @[simp] theorem comp_zero (f : NormedAddGroupHom V₂ V₃) : f.comp (0 : NormedAddGroupHom V₁ V₂) = 0 := by ext exact map_zero f @[simp] theorem zero_comp (f : NormedAddGroupHom V₁ V₂) : (0 : NormedAddGroupHom V₂ V₃).comp f = 0 := by ext rfl theorem comp_assoc {V₄ : Type*} [SeminormedAddCommGroup V₄] (h : NormedAddGroupHom V₃ V₄) (g : NormedAddGroupHom V₂ V₃) (f : NormedAddGroupHom V₁ V₂) : (h.comp g).comp f = h.comp (g.comp f) := by ext rfl theorem coe_comp (f : NormedAddGroupHom V₁ V₂) (g : NormedAddGroupHom V₂ V₃) : (g.comp f : V₁ → V₃) = (g : V₂ → V₃) ∘ (f : V₁ → V₂) := rfl end NormedAddGroupHom namespace NormedAddGroupHom variable {V W V₁ V₂ V₃ : Type*} [SeminormedAddCommGroup V] [SeminormedAddCommGroup W] [SeminormedAddCommGroup V₁] [SeminormedAddCommGroup V₂] [SeminormedAddCommGroup V₃] /-- The inclusion of an `AddSubgroup`, as bounded group homomorphism. -/ @[simps!] def incl (s : AddSubgroup V) : NormedAddGroupHom s V where toFun := (Subtype.val : s → V) map_add' _ _ := AddSubgroup.coe_add _ _ _ bound' := ⟨1, fun v => by rw [one_mul, AddSubgroup.coe_norm]⟩ theorem norm_incl {V' : AddSubgroup V} (x : V') : ‖incl _ x‖ = ‖x‖ := rfl /-!### Kernel -/ section Kernels variable (f : NormedAddGroupHom V₁ V₂) (g : NormedAddGroupHom V₂ V₃) /-- The kernel of a bounded group homomorphism. Naturally endowed with a `SeminormedAddCommGroup` instance. -/ def ker : AddSubgroup V₁ := f.toAddMonoidHom.ker theorem mem_ker (v : V₁) : v ∈ f.ker ↔ f v = 0 := by rw [ker, f.toAddMonoidHom.mem_ker, coe_toAddMonoidHom] /-- Given a normed group hom `f : V₁ → V₂` satisfying `g.comp f = 0` for some `g : V₂ → V₃`, the corestriction of `f` to the kernel of `g`. -/ @[simps] def ker.lift (h : g.comp f = 0) : NormedAddGroupHom V₁ g.ker where toFun v := ⟨f v, by rw [g.mem_ker, ← comp_apply g f, h, zero_apply]⟩ map_add' v w := by simp only [map_add, AddMemClass.mk_add_mk] bound' := f.bound' @[simp] theorem ker.incl_comp_lift (h : g.comp f = 0) : (incl g.ker).comp (ker.lift f g h) = f := by ext rfl @[simp] theorem ker_zero : (0 : NormedAddGroupHom V₁ V₂).ker = ⊤ := by ext simp [mem_ker] theorem coe_ker : (f.ker : Set V₁) = (f : V₁ → V₂) ⁻¹' {0} := rfl theorem isClosed_ker {V₂ : Type*} [NormedAddCommGroup V₂] (f : NormedAddGroupHom V₁ V₂) : IsClosed (f.ker : Set V₁) := f.coe_ker ▸ IsClosed.preimage f.continuous (T1Space.t1 0) end Kernels /-! ### Range -/ section Range variable (f : NormedAddGroupHom V₁ V₂) (g : NormedAddGroupHom V₂ V₃) /-- The image of a bounded group homomorphism. Naturally endowed with a `SeminormedAddCommGroup` instance. -/ def range : AddSubgroup V₂ := f.toAddMonoidHom.range theorem mem_range (v : V₂) : v ∈ f.range ↔ ∃ w, f w = v := Iff.rfl @[simp] theorem mem_range_self (v : V₁) : f v ∈ f.range := ⟨v, rfl⟩ theorem comp_range : (g.comp f).range = AddSubgroup.map g.toAddMonoidHom f.range := by unfold range rw [AddMonoidHom.map_range] rfl theorem incl_range (s : AddSubgroup V₁) : (incl s).range = s := by ext x exact ⟨fun ⟨y, hy⟩ => by rw [← hy]; simp, fun hx => ⟨⟨x, hx⟩, by simp⟩⟩ @[simp] theorem range_comp_incl_top : (f.comp (incl (⊤ : AddSubgroup V₁))).range = f.range := by simp [comp_range, incl_range, ← AddMonoidHom.range_eq_map]; rfl end Range variable {f : NormedAddGroupHom V W} /-- A `NormedAddGroupHom` is *norm-nonincreasing* if `‖f v‖ ≤ ‖v‖` for all `v`. -/ def NormNoninc (f : NormedAddGroupHom V W) : Prop := ∀ v, ‖f v‖ ≤ ‖v‖ namespace NormNoninc theorem normNoninc_iff_norm_le_one : f.NormNoninc ↔ ‖f‖ ≤ 1 := by refine ⟨fun h => ?_, fun h => fun v => ?_⟩ · refine opNorm_le_bound _ zero_le_one fun v => ?_ simpa [one_mul] using h v · simpa using le_of_opNorm_le f h v theorem zero : (0 : NormedAddGroupHom V₁ V₂).NormNoninc := fun v => by simp theorem id : (id V).NormNoninc := fun _v => le_rfl theorem comp {g : NormedAddGroupHom V₂ V₃} {f : NormedAddGroupHom V₁ V₂} (hg : g.NormNoninc) (hf : f.NormNoninc) : (g.comp f).NormNoninc := fun v => (hg (f v)).trans (hf v) @[simp] theorem neg_iff {f : NormedAddGroupHom V₁ V₂} : (-f).NormNoninc ↔ f.NormNoninc := ⟨fun h x => by simpa using h x, fun h x => (norm_neg (f x)).le.trans (h x)⟩ end NormNoninc section Isometry theorem norm_eq_of_isometry {f : NormedAddGroupHom V W} (hf : Isometry f) (v : V) : ‖f v‖ = ‖v‖ := (AddMonoidHomClass.isometry_iff_norm f).mp hf v theorem isometry_id : @Isometry V V _ _ (id V) := _root_.isometry_id theorem isometry_comp {g : NormedAddGroupHom V₂ V₃} {f : NormedAddGroupHom V₁ V₂} (hg : Isometry g) (hf : Isometry f) : Isometry (g.comp f) := hg.comp hf theorem normNoninc_of_isometry (hf : Isometry f) : f.NormNoninc := fun v => le_of_eq <| norm_eq_of_isometry hf v end Isometry variable {W₁ W₂ W₃ : Type*} [SeminormedAddCommGroup W₁] [SeminormedAddCommGroup W₂] [SeminormedAddCommGroup W₃] variable (f) (g : NormedAddGroupHom V W) variable {f₁ g₁ : NormedAddGroupHom V₁ W₁} variable {f₂ g₂ : NormedAddGroupHom V₂ W₂} variable {f₃ g₃ : NormedAddGroupHom V₃ W₃} /-- The equalizer of two morphisms `f g : NormedAddGroupHom V W`. -/ def equalizer := (f - g).ker namespace Equalizer /-- The inclusion of `f.equalizer g` as a `NormedAddGroupHom`. -/ def ι : NormedAddGroupHom (f.equalizer g) V := incl _ theorem comp_ι_eq : f.comp (ι f g) = g.comp (ι f g) := by ext x rw [comp_apply, comp_apply, ← sub_eq_zero, ← NormedAddGroupHom.sub_apply] exact x.2 variable {f g} /-- If `φ : NormedAddGroupHom V₁ V` is such that `f.comp φ = g.comp φ`, the induced morphism `NormedAddGroupHom V₁ (f.equalizer g)`. -/ @[simps] def lift (φ : NormedAddGroupHom V₁ V) (h : f.comp φ = g.comp φ) : NormedAddGroupHom V₁ (f.equalizer g) where toFun v := ⟨φ v, show (f - g) (φ v) = 0 by rw [NormedAddGroupHom.sub_apply, sub_eq_zero, ← comp_apply, h, comp_apply]⟩ map_add' v₁ v₂ := by ext simp only [map_add, AddSubgroup.coe_add, Subtype.coe_mk] bound' := by obtain ⟨C, _C_pos, hC⟩ := φ.bound exact ⟨C, hC⟩ @[simp] theorem ι_comp_lift (φ : NormedAddGroupHom V₁ V) (h : f.comp φ = g.comp φ) : (ι _ _).comp (lift φ h) = φ := by ext rfl /-- The lifting property of the equalizer as an equivalence. -/ @[simps] def liftEquiv : { φ : NormedAddGroupHom V₁ V // f.comp φ = g.comp φ } ≃ NormedAddGroupHom V₁ (f.equalizer g) where toFun φ := lift φ φ.prop invFun ψ := ⟨(ι f g).comp ψ, by rw [← comp_assoc, ← comp_assoc, comp_ι_eq]⟩ left_inv φ := by simp right_inv ψ := by ext rfl /-- Given `φ : NormedAddGroupHom V₁ V₂` and `ψ : NormedAddGroupHom W₁ W₂` such that `ψ.comp f₁ = f₂.comp φ` and `ψ.comp g₁ = g₂.comp φ`, the induced morphism `NormedAddGroupHom (f₁.equalizer g₁) (f₂.equalizer g₂)`. -/ def map (φ : NormedAddGroupHom V₁ V₂) (ψ : NormedAddGroupHom W₁ W₂) (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) : NormedAddGroupHom (f₁.equalizer g₁) (f₂.equalizer g₂) := lift (φ.comp <| ι _ _) <| by simp only [← comp_assoc, ← hf, ← hg] simp only [comp_assoc, comp_ι_eq f₁ g₁] variable {φ : NormedAddGroupHom V₁ V₂} {ψ : NormedAddGroupHom W₁ W₂} variable {φ' : NormedAddGroupHom V₂ V₃} {ψ' : NormedAddGroupHom W₂ W₃} @[simp] theorem ι_comp_map (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) : (ι f₂ g₂).comp (map φ ψ hf hg) = φ.comp (ι f₁ g₁) := ι_comp_lift _ _ @[simp] theorem map_id : map (f₂ := f₁) (g₂ := g₁) (id V₁) (id W₁) rfl rfl = id (f₁.equalizer g₁) := by ext rfl theorem comm_sq₂ (hf : ψ.comp f₁ = f₂.comp φ) (hf' : ψ'.comp f₂ = f₃.comp φ') : (ψ'.comp ψ).comp f₁ = f₃.comp (φ'.comp φ) := by rw [comp_assoc, hf, ← comp_assoc, hf', comp_assoc] theorem map_comp_map (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) (hf' : ψ'.comp f₂ = f₃.comp φ') (hg' : ψ'.comp g₂ = g₃.comp φ') : (map φ' ψ' hf' hg').comp (map φ ψ hf hg) = map (φ'.comp φ) (ψ'.comp ψ) (comm_sq₂ hf hf') (comm_sq₂ hg hg') := by ext rfl theorem ι_normNoninc : (ι f g).NormNoninc := fun _v => le_rfl /-- The lifting of a norm nonincreasing morphism is norm nonincreasing. -/ theorem lift_normNoninc (φ : NormedAddGroupHom V₁ V) (h : f.comp φ = g.comp φ) (hφ : φ.NormNoninc) : (lift φ h).NormNoninc := hφ /-- If `φ` satisfies `‖φ‖ ≤ C`, then the same is true for the lifted morphism. -/ theorem norm_lift_le (φ : NormedAddGroupHom V₁ V) (h : f.comp φ = g.comp φ) (C : ℝ) (hφ : ‖φ‖ ≤ C) : ‖lift φ h‖ ≤ C := hφ theorem map_normNoninc (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) (hφ : φ.NormNoninc) : (map φ ψ hf hg).NormNoninc := lift_normNoninc _ _ <| hφ.comp ι_normNoninc theorem norm_map_le (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) (C : ℝ) (hφ : ‖φ.comp (ι f₁ g₁)‖ ≤ C) : ‖map φ ψ hf hg‖ ≤ C := norm_lift_le _ _ _ hφ end Equalizer end NormedAddGroupHom
Mathlib/Analysis/Normed/Group/Hom.lean
954
956
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Kim Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Degree.Domain import Mathlib.Algebra.Polynomial.Degree.Support import Mathlib.Algebra.Polynomial.Eval.Coeff import Mathlib.GroupTheory.GroupAction.Ring /-! # The derivative map on polynomials ## Main definitions * `Polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map. * `Polynomial.derivativeFinsupp`: Iterated derivatives as a finite support function. -/ noncomputable section open Finset open Polynomial open scoped Nat namespace Polynomial universe u v w y z variable {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {A : Type z} {a b : R} {n : ℕ} section Derivative section Semiring variable [Semiring R] /-- `derivative p` is the formal derivative of the polynomial `p` -/ def derivative : R[X] →ₗ[R] R[X] where toFun p := p.sum fun n a => C (a * n) * X ^ (n - 1) map_add' p q := by rw [sum_add_index] <;> simp only [add_mul, forall_const, RingHom.map_add, eq_self_iff_true, zero_mul, RingHom.map_zero] map_smul' a p := by dsimp; rw [sum_smul_index] <;> simp only [mul_sum, ← C_mul', mul_assoc, coeff_C_mul, RingHom.map_mul, forall_const, zero_mul, RingHom.map_zero, sum] theorem derivative_apply (p : R[X]) : derivative p = p.sum fun n a => C (a * n) * X ^ (n - 1) := rfl theorem coeff_derivative (p : R[X]) (n : ℕ) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) := by rw [derivative_apply] simp only [coeff_X_pow, coeff_sum, coeff_C_mul] rw [sum, Finset.sum_eq_single (n + 1)] · simp only [Nat.add_succ_sub_one, add_zero, mul_one, if_true, eq_self_iff_true]; norm_cast · intro b cases b · intros rw [Nat.cast_zero, mul_zero, zero_mul] · intro _ H rw [Nat.add_one_sub_one, if_neg (mt (congr_arg Nat.succ) H.symm), mul_zero] · rw [if_pos (add_tsub_cancel_right n 1).symm, mul_one, Nat.cast_add, Nat.cast_one, mem_support_iff] intro h push_neg at h simp [h] @[simp] theorem derivative_zero : derivative (0 : R[X]) = 0 := derivative.map_zero theorem iterate_derivative_zero {k : ℕ} : derivative^[k] (0 : R[X]) = 0 := iterate_map_zero derivative k theorem derivative_monomial (a : R) (n : ℕ) : derivative (monomial n a) = monomial (n - 1) (a * n) := by rw [derivative_apply, sum_monomial_index, C_mul_X_pow_eq_monomial] simp @[simp] theorem derivative_monomial_succ (a : R) (n : ℕ) : derivative (monomial (n + 1) a) = monomial n (a * (n + 1)) := by rw [derivative_monomial, add_tsub_cancel_right, Nat.cast_add, Nat.cast_one] theorem derivative_C_mul_X (a : R) : derivative (C a * X) = C a := by simp [C_mul_X_eq_monomial, derivative_monomial, Nat.cast_one, mul_one] theorem derivative_C_mul_X_pow (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X ^ (n - 1) := by rw [C_mul_X_pow_eq_monomial, C_mul_X_pow_eq_monomial, derivative_monomial] theorem derivative_C_mul_X_sq (a : R) : derivative (C a * X ^ 2) = C (a * 2) * X := by rw [derivative_C_mul_X_pow, Nat.cast_two, pow_one] theorem derivative_X_pow (n : ℕ) : derivative (X ^ n : R[X]) = C (n : R) * X ^ (n - 1) := by convert derivative_C_mul_X_pow (1 : R) n <;> simp @[simp] theorem derivative_X_pow_succ (n : ℕ) : derivative (X ^ (n + 1) : R[X]) = C (n + 1 : R) * X ^ n := by simp [derivative_X_pow] theorem derivative_X_sq : derivative (X ^ 2 : R[X]) = C 2 * X := by rw [derivative_X_pow, Nat.cast_two, pow_one] @[simp] theorem derivative_C {a : R} : derivative (C a) = 0 := by simp [derivative_apply] theorem derivative_of_natDegree_zero {p : R[X]} (hp : p.natDegree = 0) : derivative p = 0 := by rw [eq_C_of_natDegree_eq_zero hp, derivative_C] @[simp] theorem derivative_X : derivative (X : R[X]) = 1 := (derivative_monomial _ _).trans <| by simp @[simp] theorem derivative_one : derivative (1 : R[X]) = 0 := derivative_C @[simp] theorem derivative_add {f g : R[X]} : derivative (f + g) = derivative f + derivative g := derivative.map_add f g theorem derivative_X_add_C (c : R) : derivative (X + C c) = 1 := by rw [derivative_add, derivative_X, derivative_C, add_zero] theorem derivative_sum {s : Finset ι} {f : ι → R[X]} : derivative (∑ b ∈ s, f b) = ∑ b ∈ s, derivative (f b) := map_sum .. theorem iterate_derivative_sum (k : ℕ) (s : Finset ι) (f : ι → R[X]) : derivative^[k] (∑ b ∈ s, f b) = ∑ b ∈ s, derivative^[k] (f b) := by simp_rw [← Module.End.pow_apply, map_sum] theorem derivative_smul {S : Type*} [SMulZeroClass S R] [IsScalarTower S R R] (s : S) (p : R[X]) : derivative (s • p) = s • derivative p := derivative.map_smul_of_tower s p @[simp] theorem iterate_derivative_smul {S : Type*} [SMulZeroClass S R] [IsScalarTower S R R] (s : S) (p : R[X]) (k : ℕ) : derivative^[k] (s • p) = s • derivative^[k] p := by induction k generalizing p with | zero => simp | succ k ih => simp [ih] @[simp] theorem iterate_derivative_C_mul (a : R) (p : R[X]) (k : ℕ) : derivative^[k] (C a * p) = C a * derivative^[k] p := by simp_rw [← smul_eq_C_mul, iterate_derivative_smul] theorem derivative_C_mul (a : R) (p : R[X]) : derivative (C a * p) = C a * derivative p := iterate_derivative_C_mul _ _ 1 theorem of_mem_support_derivative {p : R[X]} {n : ℕ} (h : n ∈ p.derivative.support) : n + 1 ∈ p.support := mem_support_iff.2 fun h1 : p.coeff (n + 1) = 0 => mem_support_iff.1 h <| show p.derivative.coeff n = 0 by rw [coeff_derivative, h1, zero_mul] theorem degree_derivative_lt {p : R[X]} (hp : p ≠ 0) : p.derivative.degree < p.degree := (Finset.sup_lt_iff <| bot_lt_iff_ne_bot.2 <| mt degree_eq_bot.1 hp).2 fun n hp => lt_of_lt_of_le (WithBot.coe_lt_coe.2 n.lt_succ_self) <| Finset.le_sup <| of_mem_support_derivative hp theorem degree_derivative_le {p : R[X]} : p.derivative.degree ≤ p.degree := letI := Classical.decEq R if H : p = 0 then le_of_eq <| by rw [H, derivative_zero] else (degree_derivative_lt H).le theorem natDegree_derivative_lt {p : R[X]} (hp : p.natDegree ≠ 0) : p.derivative.natDegree < p.natDegree := by rcases eq_or_ne (derivative p) 0 with hp' | hp' · rw [hp', Polynomial.natDegree_zero] exact hp.bot_lt · rw [natDegree_lt_natDegree_iff hp'] exact degree_derivative_lt fun h => hp (h.symm ▸ natDegree_zero) theorem natDegree_derivative_le (p : R[X]) : p.derivative.natDegree ≤ p.natDegree - 1 := by by_cases p0 : p.natDegree = 0 · simp [p0, derivative_of_natDegree_zero] · exact Nat.le_sub_one_of_lt (natDegree_derivative_lt p0) theorem natDegree_iterate_derivative (p : R[X]) (k : ℕ) : (derivative^[k] p).natDegree ≤ p.natDegree - k := by induction k with | zero => rw [Function.iterate_zero_apply, Nat.sub_zero] | succ d hd => rw [Function.iterate_succ_apply', Nat.sub_succ'] exact (natDegree_derivative_le _).trans <| Nat.sub_le_sub_right hd 1 @[simp] theorem derivative_natCast {n : ℕ} : derivative (n : R[X]) = 0 := by rw [← map_natCast C n] exact derivative_C @[simp] theorem derivative_ofNat (n : ℕ) [n.AtLeastTwo] : derivative (ofNat(n) : R[X]) = 0 := derivative_natCast theorem iterate_derivative_eq_zero {p : R[X]} {x : ℕ} (hx : p.natDegree < x) : Polynomial.derivative^[x] p = 0 := by induction' h : p.natDegree using Nat.strong_induction_on with _ ih generalizing p x subst h obtain ⟨t, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (pos_of_gt hx).ne' rw [Function.iterate_succ_apply] by_cases hp : p.natDegree = 0 · rw [derivative_of_natDegree_zero hp, iterate_derivative_zero] have := natDegree_derivative_lt hp exact ih _ this (this.trans_le <| Nat.le_of_lt_succ hx) rfl @[simp] theorem iterate_derivative_C {k} (h : 0 < k) : derivative^[k] (C a : R[X]) = 0 := iterate_derivative_eq_zero <| (natDegree_C _).trans_lt h @[simp] theorem iterate_derivative_one {k} (h : 0 < k) : derivative^[k] (1 : R[X]) = 0 := iterate_derivative_C h @[simp] theorem iterate_derivative_X {k} (h : 1 < k) : derivative^[k] (X : R[X]) = 0 := iterate_derivative_eq_zero <| natDegree_X_le.trans_lt h theorem natDegree_eq_zero_of_derivative_eq_zero [NoZeroSMulDivisors ℕ R] {f : R[X]} (h : derivative f = 0) : f.natDegree = 0 := by rcases eq_or_ne f 0 with (rfl | hf) · exact natDegree_zero rw [natDegree_eq_zero_iff_degree_le_zero] by_contra! f_nat_degree_pos rw [← natDegree_pos_iff_degree_pos] at f_nat_degree_pos
let m := f.natDegree - 1 have hm : m + 1 = f.natDegree := tsub_add_cancel_of_le f_nat_degree_pos have h2 := coeff_derivative f m rw [Polynomial.ext_iff] at h rw [h m, coeff_zero, ← Nat.cast_add_one, ← nsmul_eq_mul', eq_comm, smul_eq_zero] at h2 replace h2 := h2.resolve_left m.succ_ne_zero rw [hm, ← leadingCoeff, leadingCoeff_eq_zero] at h2 exact hf h2 theorem eq_C_of_derivative_eq_zero [NoZeroSMulDivisors ℕ R] {f : R[X]} (h : derivative f = 0) :
Mathlib/Algebra/Polynomial/Derivative.lean
235
244
/- 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
2,373
2,378
/- 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.Limits.Shapes.FiniteProducts import Mathlib.CategoryTheory.Limits.Shapes.Kernels import Mathlib.CategoryTheory.Limits.Shapes.NormalMono.Equalizers import Mathlib.CategoryTheory.Abelian.Images import Mathlib.CategoryTheory.Preadditive.Basic /-! # Every NonPreadditiveAbelian category is preadditive In mathlib, we define an abelian category as a preadditive category with a zero object, kernels and cokernels, products and coproducts and in which every monomorphism and epimorphism is normal. While virtually every interesting abelian category has a natural preadditive structure (which is why it is included in the definition), preadditivity is not actually needed: Every category that has all of the other properties appearing in the definition of an abelian category admits a preadditive structure. This is the construction we carry out in this file. The proof proceeds in roughly five steps: 1. Prove some results (for example that all equalizers exist) that would be trivial if we already had the preadditive structure but are a bit of work without it. 2. Develop images and coimages to show that every monomorphism is the kernel of its cokernel. The results of the first two steps are also useful for the "normal" development of abelian categories, and will be used there. 3. For every object `A`, define a "subtraction" morphism `σ : A ⨯ A ⟶ A` and use it to define subtraction on morphisms as `f - g := prod.lift f g ≫ σ`. 4. Prove a small number of identities about this subtraction from the definition of `σ`. 5. From these identities, prove a large number of other identities that imply that defining `f + g := f - (0 - g)` indeed gives an abelian group structure on morphisms such that composition is bilinear. The construction is non-trivial and it is quite remarkable that this abelian group structure can be constructed purely from the existence of a few limits and colimits. Even more remarkably, since abelian categories admit exactly one preadditive structure (see `subsingletonPreadditiveOfHasBinaryBiproducts`), the construction manages to exactly reconstruct any natural preadditive structure the category may have. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable section open CategoryTheory open CategoryTheory.Limits namespace CategoryTheory section universe v u variable (C : Type u) [Category.{v} C] /-- We call a category `NonPreadditiveAbelian` if it has a zero object, kernels, cokernels, binary products and coproducts, and every monomorphism and every epimorphism is normal. -/ class NonPreadditiveAbelian extends HasZeroMorphisms C, IsNormalMonoCategory C, IsNormalEpiCategory C where [has_zero_object : HasZeroObject C] [has_kernels : HasKernels C] [has_cokernels : HasCokernels C] [has_finite_products : HasFiniteProducts C] [has_finite_coproducts : HasFiniteCoproducts C] attribute [instance] NonPreadditiveAbelian.has_zero_object attribute [instance] NonPreadditiveAbelian.has_kernels attribute [instance] NonPreadditiveAbelian.has_cokernels attribute [instance] NonPreadditiveAbelian.has_finite_products attribute [instance] NonPreadditiveAbelian.has_finite_coproducts end end CategoryTheory open CategoryTheory universe v u variable {C : Type u} [Category.{v} C] [NonPreadditiveAbelian C] namespace CategoryTheory.NonPreadditiveAbelian section Factor variable {P Q : C} (f : P ⟶ Q) /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : Epi (Abelian.factorThruImage f) := let I := Abelian.image f let p := Abelian.factorThruImage f let i := kernel.ι (cokernel.π f) -- It will suffice to consider some g : I ⟶ R such that p ≫ g = 0 and show that g = 0. NormalMonoCategory.epi_of_zero_cancel _ fun R (g : I ⟶ R) (hpg : p ≫ g = 0) => by -- Since C is abelian, u := ker g ≫ i is the kernel of some morphism h. let u := kernel.ι g ≫ i haveI hu := normalMonoOfMono u let h := hu.g -- By hypothesis, p factors through the kernel of g via some t. obtain ⟨t, ht⟩ := kernel.lift' g p hpg have fh : f ≫ h = 0 := calc f ≫ h = (p ≫ i) ≫ h := (Abelian.image.fac f).symm ▸ rfl _ = ((t ≫ kernel.ι g) ≫ i) ≫ h := ht ▸ rfl _ = t ≫ u ≫ h := by simp only [u, Category.assoc] _ = t ≫ 0 := hu.w ▸ rfl _ = 0 := HasZeroMorphisms.comp_zero _ _ -- h factors through the cokernel of f via some l. obtain ⟨l, hl⟩ := cokernel.desc' f h fh have hih : i ≫ h = 0 := calc i ≫ h = i ≫ cokernel.π f ≫ l := hl ▸ rfl _ = 0 ≫ l := by rw [← Category.assoc, kernel.condition] _ = 0 := zero_comp -- i factors through u = ker h via some s. obtain ⟨s, hs⟩ := NormalMono.lift' u i hih have hs' : (s ≫ kernel.ι g) ≫ i = 𝟙 I ≫ i := by rw [Category.assoc, hs, Category.id_comp] haveI : Epi (kernel.ι g) := epi_of_epi_fac ((cancel_mono _).1 hs') -- ker g is an epimorphism, but ker g ≫ g = 0 = ker g ≫ 0, so g = 0 as required. exact zero_of_epi_comp _ (kernel.condition g) instance isIso_factorThruImage [Mono f] : IsIso (Abelian.factorThruImage f) := isIso_of_mono_of_epi <| Abelian.factorThruImage f /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : Mono (Abelian.factorThruCoimage f) := let I := Abelian.coimage f let i := Abelian.factorThruCoimage f let p := cokernel.π (kernel.ι f) NormalEpiCategory.mono_of_cancel_zero _ fun R (g : R ⟶ I) (hgi : g ≫ i = 0) => by -- Since C is abelian, u := p ≫ coker g is the cokernel of some morphism h. let u := p ≫ cokernel.π g haveI hu := normalEpiOfEpi u let h := hu.g -- By hypothesis, i factors through the cokernel of g via some t. obtain ⟨t, ht⟩ := cokernel.desc' g i hgi have hf : h ≫ f = 0 := calc h ≫ f = h ≫ p ≫ i := (Abelian.coimage.fac f).symm ▸ rfl _ = h ≫ p ≫ cokernel.π g ≫ t := ht ▸ rfl _ = h ≫ u ≫ t := by simp only [u, Category.assoc] _ = 0 ≫ t := by rw [← Category.assoc, hu.w] _ = 0 := zero_comp -- h factors through the kernel of f via some l. obtain ⟨l, hl⟩ := kernel.lift' f h hf have hhp : h ≫ p = 0 := calc h ≫ p = (l ≫ kernel.ι f) ≫ p := hl ▸ rfl _ = l ≫ 0 := by rw [Category.assoc, cokernel.condition] _ = 0 := comp_zero -- p factors through u = coker h via some s. obtain ⟨s, hs⟩ := NormalEpi.desc' u p hhp have hs' : p ≫ cokernel.π g ≫ s = p ≫ 𝟙 I := by rw [← Category.assoc, hs, Category.comp_id] haveI : Mono (cokernel.π g) := mono_of_mono_fac ((cancel_epi _).1 hs') -- coker g is a monomorphism, but g ≫ coker g = 0 = 0 ≫ coker g, so g = 0 as required. exact zero_of_comp_mono _ (cokernel.condition g) instance isIso_factorThruCoimage [Epi f] : IsIso (Abelian.factorThruCoimage f) := isIso_of_mono_of_epi _ end Factor section CokernelOfKernel variable {X Y : C} {f : X ⟶ Y} /-- In a `NonPreadditiveAbelian` category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `Fork.ι s`. -/ def epiIsCokernelOfKernel [Epi f] (s : Fork f 0) (h : IsLimit s) : IsColimit (CokernelCofork.ofπ f (KernelFork.condition s)) := IsCokernel.cokernelIso _ _ (cokernel.ofIsoComp _ _ (Limits.IsLimit.conePointUniqueUpToIso (limit.isLimit _) h) (ConeMorphism.w (Limits.IsLimit.uniqueUpToIso (limit.isLimit _) h).hom _)) (asIso <| Abelian.factorThruCoimage f) (Abelian.coimage.fac f) /-- In a `NonPreadditiveAbelian` category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `Cofork.π s`. -/ def monoIsKernelOfCokernel [Mono f] (s : Cofork f 0) (h : IsColimit s) : IsLimit (KernelFork.ofι f (CokernelCofork.condition s)) := IsKernel.isoKernel _ _ (kernel.ofCompIso _ _ (Limits.IsColimit.coconePointUniqueUpToIso h (colimit.isColimit _)) (CoconeMorphism.w (Limits.IsColimit.uniqueUpToIso h <| colimit.isColimit _).hom _)) (asIso <| Abelian.factorThruImage f) (Abelian.image.fac f) end CokernelOfKernel section /-- The composite `A ⟶ A ⨯ A ⟶ cokernel (Δ A)`, where the first map is `(𝟙 A, 0)` and the second map is the canonical projection into the cokernel. -/ abbrev r (A : C) : A ⟶ cokernel (diag A) := prod.lift (𝟙 A) 0 ≫ cokernel.π (diag A) instance mono_Δ {A : C} : Mono (diag A) := mono_of_mono_fac <| prod.lift_fst _ _ instance mono_r {A : C} : Mono (r A) := by let hl : IsLimit (KernelFork.ofι (diag A) (cokernel.condition (diag A))) := monoIsKernelOfCokernel _ (colimit.isColimit _) apply NormalEpiCategory.mono_of_cancel_zero intro Z x hx have hxx : (x ≫ prod.lift (𝟙 A) (0 : A ⟶ A)) ≫ cokernel.π (diag A) = 0 := by rw [Category.assoc, hx] obtain ⟨y, hy⟩ := KernelFork.IsLimit.lift' hl _ hxx rw [KernelFork.ι_ofι] at hy have hyy : y = 0 := by erw [← Category.comp_id y, ← Limits.prod.lift_snd (𝟙 A) (𝟙 A), ← Category.assoc, hy, Category.assoc, prod.lift_snd, HasZeroMorphisms.comp_zero] haveI : Mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _) apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1 rw [← hy, hyy, zero_comp, zero_comp] instance epi_r {A : C} : Epi (r A) := by have hlp : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ Limits.prod.snd = 0 := prod.lift_snd _ _ let hp1 : IsLimit (KernelFork.ofι (prod.lift (𝟙 A) (0 : A ⟶ A)) hlp) := by refine Fork.IsLimit.mk _ (fun s => Fork.ι s ≫ Limits.prod.fst) ?_ ?_ · intro s apply Limits.prod.hom_ext <;> simp · intro s m h haveI : Mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _) apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1 convert h apply Limits.prod.hom_ext <;> simp let hp2 : IsColimit (CokernelCofork.ofπ (Limits.prod.snd : A ⨯ A ⟶ A) hlp) := epiIsCokernelOfKernel _ hp1 apply NormalMonoCategory.epi_of_zero_cancel intro Z z hz have h : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ cokernel.π (diag A) ≫ z = 0 := by rw [← Category.assoc, hz] obtain ⟨t, ht⟩ := CokernelCofork.IsColimit.desc' hp2 _ h rw [CokernelCofork.π_ofπ] at ht have htt : t = 0 := by rw [← Category.id_comp t] change 𝟙 A ≫ t = 0 rw [← Limits.prod.lift_snd (𝟙 A) (𝟙 A), Category.assoc, ht, ← Category.assoc, cokernel.condition, zero_comp] apply (cancel_epi (cokernel.π (diag A))).1 rw [← ht, htt, comp_zero, comp_zero] instance isIso_r {A : C} : IsIso (r A) := isIso_of_mono_of_epi _ /-- The composite `A ⨯ A ⟶ cokernel (diag A) ⟶ A` given by the natural projection into the cokernel followed by the inverse of `r`. In the category of modules, using the normal kernels and cokernels, this map is equal to the map `(a, b) ↦ a - b`, hence the name `σ` for "subtraction". -/ abbrev σ {A : C} : A ⨯ A ⟶ A := cokernel.π (diag A) ≫ inv (r A) end @[reassoc] theorem diag_σ {X : C} : diag X ≫ σ = 0 := by rw [cokernel.condition_assoc, zero_comp] @[reassoc (attr := simp)] theorem lift_σ {X : C} : prod.lift (𝟙 X) 0 ≫ σ = 𝟙 X := by rw [← Category.assoc, IsIso.hom_inv_id] @[reassoc] theorem lift_map {X Y : C} (f : X ⟶ Y) : prod.lift (𝟙 X) 0 ≫ Limits.prod.map f f = f ≫ prod.lift (𝟙 Y) 0 := by simp /-- σ is a cokernel of Δ X. -/ def isColimitσ {X : C} : IsColimit (CokernelCofork.ofπ (σ : X ⨯ X ⟶ X) diag_σ) := cokernel.cokernelIso _ σ (asIso (r X)).symm (by rw [Iso.symm_hom, asIso_inv]) /-- This is the key identity satisfied by `σ`. -/ theorem σ_comp {X Y : C} (f : X ⟶ Y) : σ ≫ f = Limits.prod.map f f ≫ σ := by obtain ⟨g, hg⟩ := CokernelCofork.IsColimit.desc' isColimitσ (Limits.prod.map f f ≫ σ) (by rw [prod.diag_map_assoc, diag_σ, comp_zero]) suffices hfg : f = g by rw [← hg, Cofork.π_ofπ, hfg] calc f = f ≫ prod.lift (𝟙 Y) 0 ≫ σ := by rw [lift_σ, Category.comp_id] _ = prod.lift (𝟙 X) 0 ≫ Limits.prod.map f f ≫ σ := by rw [lift_map_assoc] _ = prod.lift (𝟙 X) 0 ≫ σ ≫ g := by rw [← hg, CokernelCofork.π_ofπ] _ = g := by rw [← Category.assoc, lift_σ, Category.id_comp] section -- We write `f - g` for `prod.lift f g ≫ σ`. /-- Subtraction of morphisms in a `NonPreadditiveAbelian` category. -/ def hasSub {X Y : C} : Sub (X ⟶ Y) := ⟨fun f g => prod.lift f g ≫ σ⟩ attribute [local instance] hasSub -- We write `-f` for `0 - f`. /-- Negation of morphisms in a `NonPreadditiveAbelian` category. -/ def hasNeg {X Y : C} : Neg (X ⟶ Y) where neg := fun f => 0 - f attribute [local instance] hasNeg -- We write `f + g` for `f - (-g)`. /-- Addition of morphisms in a `NonPreadditiveAbelian` category. -/ def hasAdd {X Y : C} : Add (X ⟶ Y) := ⟨fun f g => f - -g⟩ attribute [local instance] hasAdd theorem sub_def {X Y : C} (a b : X ⟶ Y) : a - b = prod.lift a b ≫ σ := rfl theorem add_def {X Y : C} (a b : X ⟶ Y) : a + b = a - -b := rfl theorem neg_def {X Y : C} (a : X ⟶ Y) : -a = 0 - a := rfl theorem sub_zero {X Y : C} (a : X ⟶ Y) : a - 0 = a := by rw [sub_def] conv_lhs => congr; congr; rw [← Category.comp_id a] case a.g => rw [show 0 = a ≫ (0 : Y ⟶ Y) by simp] rw [← prod.comp_lift, Category.assoc, lift_σ, Category.comp_id] theorem sub_self {X Y : C} (a : X ⟶ Y) : a - a = 0 := by rw [sub_def, ← Category.comp_id a, ← prod.comp_lift, Category.assoc, diag_σ, comp_zero] theorem lift_sub_lift {X Y : C} (a b c d : X ⟶ Y) : prod.lift a b - prod.lift c d = prod.lift (a - c) (b - d) := by simp only [sub_def] ext · rw [Category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_fst, prod.lift_fst, prod.lift_fst] · rw [Category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_snd, prod.lift_snd, prod.lift_snd] theorem sub_sub_sub {X Y : C} (a b c d : X ⟶ Y) : a - c - (b - d) = a - b - (c - d) := by rw [sub_def, ← lift_sub_lift, sub_def, Category.assoc, σ_comp, prod.lift_map_assoc]; rfl theorem neg_sub {X Y : C} (a b : X ⟶ Y) : -a - b = -b - a := by conv_lhs => rw [neg_def, ← sub_zero b, sub_sub_sub, sub_zero, ← neg_def] theorem neg_neg {X Y : C} (a : X ⟶ Y) : - -a = a := by rw [neg_def, neg_def] conv_lhs => congr; rw [← sub_self a] rw [sub_sub_sub, sub_zero, sub_self, sub_zero] theorem add_comm {X Y : C} (a b : X ⟶ Y) : a + b = b + a := by rw [add_def] conv_lhs => rw [← neg_neg a] rw [neg_def, neg_def, neg_def, sub_sub_sub] conv_lhs => congr
next => skip rw [← neg_def, neg_sub]
Mathlib/CategoryTheory/Abelian/NonPreadditive.lean
358
359
/- Copyright (c) 2020 Kevin Buzzard, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Bhavik Mehta -/ import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Equalizers import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Products import Mathlib.CategoryTheory.Limits.Yoneda import Mathlib.CategoryTheory.Preadditive.FunctorCategory import Mathlib.CategoryTheory.Sites.SheafOfTypes import Mathlib.CategoryTheory.Sites.EqualizerSheafCondition import Mathlib.CategoryTheory.Limits.Constructions.EpiMono /-! # Sheaves taking values in a category If C is a category with a Grothendieck topology, we define the notion of a sheaf taking values in an arbitrary category `A`. We follow the definition in https://stacks.math.columbia.edu/tag/00VR, noting that the presheaf of sets "defined above" can be seen in the comments between tags 00VQ and 00VR on the page <https://stacks.math.columbia.edu/tag/00VL>. The advantage of this definition is that we need no assumptions whatsoever on `A` other than the assumption that the morphisms in `C` and `A` live in the same universe. * An `A`-valued presheaf `P : Cᵒᵖ ⥤ A` is defined to be a sheaf (for the topology `J`) iff for every `E : A`, the type-valued presheaves of sets given by sending `U : Cᵒᵖ` to `Hom_{A}(E, P U)` are all sheaves of sets, see `CategoryTheory.Presheaf.IsSheaf`. * When `A = Type`, this recovers the basic definition of sheaves of sets, see `CategoryTheory.isSheaf_iff_isSheaf_of_type`. * A alternate definition in terms of limits, unconditionally equivalent to the original one: see `CategoryTheory.Presheaf.isSheaf_iff_isLimit`. * An alternate definition when `C` is small, has pullbacks and `A` has products is given by an equalizer condition `CategoryTheory.Presheaf.IsSheaf'`. This is equivalent to the earlier definition, shown in `CategoryTheory.Presheaf.isSheaf_iff_isSheaf'`. * When `A = Type`, this is *definitionally* equal to the equalizer condition for presieves in `CategoryTheory.Sites.SheafOfTypes`. * When `A` has limits and there is a functor `s : A ⥤ Type` which is faithful, reflects isomorphisms and preserves limits, then `P : Cᵒᵖ ⥤ A` is a sheaf iff the underlying presheaf of types `P ⋙ s : Cᵒᵖ ⥤ Type` is a sheaf (`CategoryTheory.Presheaf.isSheaf_iff_isSheaf_forget`). Cf https://stacks.math.columbia.edu/tag/0073, which is a weaker version of this statement (it's only over spaces, not sites) and https://stacks.math.columbia.edu/tag/00YR (a), which additionally assumes filtered colimits. ## Implementation notes Occasionally we need to take a limit in `A` of a collection of morphisms of `C` indexed by a collection of objects in `C`. This turns out to force the morphisms of `A` to be in a sufficiently large universe. Rather than use `UnivLE` we prove some results for a category `A'` instead, whose morphism universe of `A'` is defined to be `max u₁ v₁`, where `u₁, v₁` are the universes for `C`. Perhaps after we get better at handling universe inequalities this can be changed. -/ universe w v₁ v₂ v₃ u₁ u₂ u₃ noncomputable section namespace CategoryTheory open Opposite CategoryTheory Category Limits Sieve namespace Presheaf variable {C : Type u₁} [Category.{v₁} C] variable {A : Type u₂} [Category.{v₂} A] variable (J : GrothendieckTopology C) -- We follow https://stacks.math.columbia.edu/tag/00VL definition 00VR /-- A sheaf of A is a presheaf P : Cᵒᵖ => A such that for every E : A, the presheaf of types given by sending U : C to Hom_{A}(E, P U) is a sheaf of types. -/ @[stacks 00VR] def IsSheaf (P : Cᵒᵖ ⥤ A) : Prop := ∀ E : A, Presieve.IsSheaf J (P ⋙ coyoneda.obj (op E)) /-- Condition that a presheaf with values in a concrete category is separated for a Grothendieck topology. -/ def IsSeparated (P : Cᵒᵖ ⥤ A) {FA : A → A → Type*} {CA : A → Type*} [∀ X Y, FunLike (FA X Y) (CA X) (CA Y)] [ConcreteCategory A FA] : Prop := ∀ (X : C) (S : Sieve X) (_ : S ∈ J X) (x y : ToType (P.obj (op X))), (∀ (Y : C) (f : Y ⟶ X) (_ : S f), P.map f.op x = P.map f.op y) → x = y section LimitSheafCondition open Presieve Presieve.FamilyOfElements Limits variable (P : Cᵒᵖ ⥤ A) {X : C} (S : Sieve X) (R : Presieve X) (E : Aᵒᵖ) /-- Given a sieve `S` on `X : C`, a presheaf `P : Cᵒᵖ ⥤ A`, and an object `E` of `A`, the cones over the natural diagram `S.arrows.diagram.op ⋙ P` associated to `S` and `P` with cone point `E` are in 1-1 correspondence with sieve_compatible family of elements for the sieve `S` and the presheaf of types `Hom (E, P -)`. -/ def conesEquivSieveCompatibleFamily : (S.arrows.diagram.op ⋙ P).cones.obj E ≃ { x : FamilyOfElements (P ⋙ coyoneda.obj E) (S : Presieve X) // x.SieveCompatible } where toFun π := ⟨fun _ f h => π.app (op ⟨Over.mk f, h⟩), fun X Y f g hf => by apply (id_comp _).symm.trans dsimp exact π.naturality (Quiver.Hom.op (Over.homMk _ (by rfl)))⟩ invFun x := { app := fun f => x.1 f.unop.1.hom f.unop.2 naturality := fun f f' g => by refine Eq.trans ?_ (x.2 f.unop.1.hom g.unop.left f.unop.2) dsimp rw [id_comp] convert rfl rw [Over.w] } left_inv _ := rfl right_inv _ := rfl variable {P S E} variable {x : FamilyOfElements (P ⋙ coyoneda.obj E) S.arrows} (hx : SieveCompatible x) /-- The cone corresponding to a sieve_compatible family of elements, dot notation enabled. -/ @[simp] def _root_.CategoryTheory.Presieve.FamilyOfElements.SieveCompatible.cone : Cone (S.arrows.diagram.op ⋙ P) where pt := E.unop π := (conesEquivSieveCompatibleFamily P S E).invFun ⟨x, hx⟩ /-- Cone morphisms from the cone corresponding to a sieve_compatible family to the natural cone associated to a sieve `S` and a presheaf `P` are in 1-1 correspondence with amalgamations of the family. -/ def homEquivAmalgamation : (hx.cone ⟶ P.mapCone S.arrows.cocone.op) ≃ { t // x.IsAmalgamation t } where toFun l := ⟨l.hom, fun _ f hf => l.w (op ⟨Over.mk f, hf⟩)⟩ invFun t := ⟨t.1, fun f => t.2 f.unop.1.hom f.unop.2⟩ left_inv _ := rfl right_inv _ := rfl variable (P S) /-- Given sieve `S` and presheaf `P : Cᵒᵖ ⥤ A`, their natural associated cone is a limit cone iff `Hom (E, P -)` is a sheaf of types for the sieve `S` and all `E : A`. -/ theorem isLimit_iff_isSheafFor : Nonempty (IsLimit (P.mapCone S.arrows.cocone.op)) ↔ ∀ E : Aᵒᵖ, IsSheafFor (P ⋙ coyoneda.obj E) S.arrows := by dsimp [IsSheafFor]; simp_rw [compatible_iff_sieveCompatible] rw [((Cone.isLimitEquivIsTerminal _).trans (isTerminalEquivUnique _ _)).nonempty_congr] rw [Classical.nonempty_pi]; constructor · intro hu E x hx specialize hu hx.cone rw [(homEquivAmalgamation hx).uniqueCongr.nonempty_congr] at hu exact (unique_subtype_iff_existsUnique _).1 hu · rintro h ⟨E, π⟩ let eqv := conesEquivSieveCompatibleFamily P S (op E) rw [← eqv.left_inv π] erw [(homEquivAmalgamation (eqv π).2).uniqueCongr.nonempty_congr] rw [unique_subtype_iff_existsUnique] exact h _ _ (eqv π).2 /-- Given sieve `S` and presheaf `P : Cᵒᵖ ⥤ A`, their natural associated cone admits at most one morphism from every cone in the same category (i.e. over the same diagram), iff `Hom (E, P -)`is separated for the sieve `S` and all `E : A`. -/ theorem subsingleton_iff_isSeparatedFor : (∀ c, Subsingleton (c ⟶ P.mapCone S.arrows.cocone.op)) ↔ ∀ E : Aᵒᵖ, IsSeparatedFor (P ⋙ coyoneda.obj E) S.arrows := by constructor · intro hs E x t₁ t₂ h₁ h₂ have hx := is_compatible_of_exists_amalgamation x ⟨t₁, h₁⟩ rw [compatible_iff_sieveCompatible] at hx specialize hs hx.cone rcases hs with ⟨hs⟩ simpa only [Subtype.mk.injEq] using (show Subtype.mk t₁ h₁ = ⟨t₂, h₂⟩ from (homEquivAmalgamation hx).symm.injective (hs _ _)) · rintro h ⟨E, π⟩ let eqv := conesEquivSieveCompatibleFamily P S (op E) constructor rw [← eqv.left_inv π] intro f₁ f₂ let eqv' := homEquivAmalgamation (eqv π).2 apply eqv'.injective ext apply h _ (eqv π).1 <;> exact (eqv' _).2 /-- A presheaf `P` is a sheaf for the Grothendieck topology `J` iff for every covering sieve `S` of `J`, the natural cone associated to `P` and `S` is a limit cone. -/ theorem isSheaf_iff_isLimit : IsSheaf J P ↔ ∀ ⦃X : C⦄ (S : Sieve X), S ∈ J X → Nonempty (IsLimit (P.mapCone S.arrows.cocone.op)) := ⟨fun h _ S hS => (isLimit_iff_isSheafFor P S).2 fun E => h E.unop S hS, fun h E _ S hS => (isLimit_iff_isSheafFor P S).1 (h S hS) (op E)⟩ /-- A presheaf `P` is separated for the Grothendieck topology `J` iff for every covering sieve `S` of `J`, the natural cone associated to `P` and `S` admits at most one morphism from every cone in the same category. -/ theorem isSeparated_iff_subsingleton : (∀ E : A, Presieve.IsSeparated J (P ⋙ coyoneda.obj (op E))) ↔ ∀ ⦃X : C⦄ (S : Sieve X), S ∈ J X → ∀ c, Subsingleton (c ⟶ P.mapCone S.arrows.cocone.op) := ⟨fun h _ S hS => (subsingleton_iff_isSeparatedFor P S).2 fun E => h E.unop S hS, fun h E _ S hS => (subsingleton_iff_isSeparatedFor P S).1 (h S hS) (op E)⟩ /-- Given presieve `R` and presheaf `P : Cᵒᵖ ⥤ A`, the natural cone associated to `P` and the sieve `Sieve.generate R` generated by `R` is a limit cone iff `Hom (E, P -)` is a sheaf of types for the presieve `R` and all `E : A`. -/ theorem isLimit_iff_isSheafFor_presieve : Nonempty (IsLimit (P.mapCone (generate R).arrows.cocone.op)) ↔ ∀ E : Aᵒᵖ, IsSheafFor (P ⋙ coyoneda.obj E) R := (isLimit_iff_isSheafFor P _).trans (forall_congr' fun _ => (isSheafFor_iff_generate _).symm) /-- A presheaf `P` is a sheaf for the Grothendieck topology generated by a pretopology `K` iff for every covering presieve `R` of `K`, the natural cone associated to `P` and `Sieve.generate R` is a limit cone. -/ theorem isSheaf_iff_isLimit_pretopology [HasPullbacks C] (K : Pretopology C) : IsSheaf (K.toGrothendieck C) P ↔ ∀ ⦃X : C⦄ (R : Presieve X), R ∈ K X → Nonempty (IsLimit (P.mapCone (generate R).arrows.cocone.op)) := by dsimp [IsSheaf] simp_rw [isSheaf_pretopology] exact ⟨fun h X R hR => (isLimit_iff_isSheafFor_presieve P R).2 fun E => h E.unop R hR, fun h E X R hR => (isLimit_iff_isSheafFor_presieve P R).1 (h R hR) (op E)⟩ end LimitSheafCondition variable {J} /-- This is a wrapper around `Presieve.IsSheafFor.amalgamate` to be used below. If `P`s a sheaf, `S` is a cover of `X`, and `x` is a collection of morphisms from `E` to `P` evaluated at terms in the cover which are compatible, then we can amalgamate the `x`s to obtain a single morphism `E ⟶ P.obj (op X)`. -/ def IsSheaf.amalgamate {A : Type u₂} [Category.{v₂} A] {E : A} {X : C} {P : Cᵒᵖ ⥤ A} (hP : Presheaf.IsSheaf J P) (S : J.Cover X) (x : ∀ I : S.Arrow, E ⟶ P.obj (op I.Y)) (hx : ∀ ⦃I₁ I₂ : S.Arrow⦄ (r : I₁.Relation I₂), x I₁ ≫ P.map r.g₁.op = x I₂ ≫ P.map r.g₂.op) : E ⟶ P.obj (op X) := (hP _ _ S.condition).amalgamate (fun Y f hf => x ⟨Y, f, hf⟩) fun _ _ _ _ _ _ _ h₁ h₂ w => @hx { hf := h₁, .. } { hf := h₂, .. } { w := w, .. } @[reassoc (attr := simp)] theorem IsSheaf.amalgamate_map {A : Type u₂} [Category.{v₂} A] {E : A} {X : C} {P : Cᵒᵖ ⥤ A} (hP : Presheaf.IsSheaf J P) (S : J.Cover X) (x : ∀ I : S.Arrow, E ⟶ P.obj (op I.Y)) (hx : ∀ ⦃I₁ I₂ : S.Arrow⦄ (r : I₁.Relation I₂), x I₁ ≫ P.map r.g₁.op = x I₂ ≫ P.map r.g₂.op) (I : S.Arrow) : hP.amalgamate S x hx ≫ P.map I.f.op = x _ := by apply (hP _ _ S.condition).valid_glue theorem IsSheaf.hom_ext {A : Type u₂} [Category.{v₂} A] {E : A} {X : C} {P : Cᵒᵖ ⥤ A} (hP : Presheaf.IsSheaf J P) (S : J.Cover X) (e₁ e₂ : E ⟶ P.obj (op X)) (h : ∀ I : S.Arrow, e₁ ≫ P.map I.f.op = e₂ ≫ P.map I.f.op) : e₁ = e₂ := (hP _ _ S.condition).isSeparatedFor.ext fun Y f hf => h ⟨Y, f, hf⟩ lemma IsSheaf.hom_ext_ofArrows {P : Cᵒᵖ ⥤ A} (hP : Presheaf.IsSheaf J P) {I : Type*} {S : C} {X : I → C} (f : ∀ i, X i ⟶ S) (hf : Sieve.ofArrows _ f ∈ J S) {E : A} {x y : E ⟶ P.obj (op S)} (h : ∀ i, x ≫ P.map (f i).op = y ≫ P.map (f i).op) : x = y := by apply hP.hom_ext ⟨_, hf⟩ rintro ⟨Z, _, _, g, _, ⟨i⟩, rfl⟩ dsimp rw [P.map_comp, reassoc_of% (h i)] section variable {P : Cᵒᵖ ⥤ A} (hP : Presheaf.IsSheaf J P) {I : Type*} {S : C} {X : I → C} (f : ∀ i, X i ⟶ S) (hf : Sieve.ofArrows _ f ∈ J S) {E : A} (x : ∀ i, E ⟶ P.obj (op (X i))) (hx : ∀ ⦃W : C⦄ ⦃i j : I⦄ (a : W ⟶ X i) (b : W ⟶ X j), a ≫ f i = b ≫ f j → x i ≫ P.map a.op = x j ≫ P.map b.op) include hP hf hx lemma IsSheaf.existsUnique_amalgamation_ofArrows : ∃! (g : E ⟶ P.obj (op S)), ∀ (i : I), g ≫ P.map (f i).op = x i := (Presieve.isSheafFor_arrows_iff _ _).1 ((Presieve.isSheafFor_iff_generate _).2 (hP E _ hf)) x (fun _ _ _ _ _ w => hx _ _ w) @[deprecated (since := "2024-12-17")] alias IsSheaf.exists_unique_amalgamation_ofArrows := IsSheaf.existsUnique_amalgamation_ofArrows /-- If `P : Cᵒᵖ ⥤ A` is a sheaf and `f i : X i ⟶ S` is a covering family, then a morphism `E ⟶ P.obj (op S)` can be constructed from a compatible family of morphisms `x : E ⟶ P.obj (op (X i))`. -/ def IsSheaf.amalgamateOfArrows : E ⟶ P.obj (op S) := (hP.existsUnique_amalgamation_ofArrows f hf x hx).choose @[reassoc (attr := simp)] lemma IsSheaf.amalgamateOfArrows_map (i : I) : hP.amalgamateOfArrows f hf x hx ≫ P.map (f i).op = x i := (hP.existsUnique_amalgamation_ofArrows f hf x hx).choose_spec.1 i end theorem isSheaf_of_iso_iff {P P' : Cᵒᵖ ⥤ A} (e : P ≅ P') : IsSheaf J P ↔ IsSheaf J P' := forall_congr' fun _ => ⟨Presieve.isSheaf_iso J (isoWhiskerRight e _), Presieve.isSheaf_iso J (isoWhiskerRight e.symm _)⟩ variable (J) theorem isSheaf_of_isTerminal {X : A} (hX : IsTerminal X) : Presheaf.IsSheaf J ((CategoryTheory.Functor.const _).obj X) := fun _ _ _ _ _ _ => ⟨hX.from _, fun _ _ _ => hX.hom_ext _ _, fun _ _ => hX.hom_ext _ _⟩ end Presheaf variable {C : Type u₁} [Category.{v₁} C] variable (J : GrothendieckTopology C) variable (A : Type u₂) [Category.{v₂} A] /-- The category of sheaves taking values in `A` on a grothendieck topology. -/ structure Sheaf where /-- the underlying presheaf -/ val : Cᵒᵖ ⥤ A /-- the condition that the presheaf is a sheaf -/ cond : Presheaf.IsSheaf J val namespace Sheaf variable {J A} /-- Morphisms between sheaves are just morphisms of presheaves. -/ @[ext] structure Hom (X Y : Sheaf J A) where /-- a morphism between the underlying presheaves -/ val : X.val ⟶ Y.val @[simps id_val comp_val] instance instCategorySheaf : Category (Sheaf J A) where Hom := Hom id _ := ⟨𝟙 _⟩ comp f g := ⟨f.val ≫ g.val⟩ id_comp _ := Hom.ext <| id_comp _ comp_id _ := Hom.ext <| comp_id _ assoc _ _ _ := Hom.ext <| assoc _ _ _ -- Let's make the inhabited linter happy.../sips instance (X : Sheaf J A) : Inhabited (Hom X X) := ⟨𝟙 X⟩ @[ext] lemma hom_ext {X Y : Sheaf J A} (x y : X ⟶ Y) (h : x.val = y.val) : x = y := Sheaf.Hom.ext h end Sheaf /-- The inclusion functor from sheaves to presheaves. -/ @[simps] def sheafToPresheaf : Sheaf J A ⥤ Cᵒᵖ ⥤ A where obj := Sheaf.val map f := f.val map_id _ := rfl map_comp _ _ := rfl /-- The sections of a sheaf (i.e. evaluation as a presheaf on `C`). -/ abbrev sheafSections : Cᵒᵖ ⥤ Sheaf J A ⥤ A := (sheafToPresheaf J A).flip /-- The sheaf sections functor on `X` is given by evaluation of presheaves on `X`. -/ @[simps!] def sheafSectionsNatIsoEvaluation {X : C} : (sheafSections J A).obj (op X) ≅ sheafToPresheaf J A ⋙ (evaluation _ _).obj (op X) := NatIso.ofComponents (fun _ ↦ Iso.refl _) /-- The functor `Sheaf J A ⥤ Cᵒᵖ ⥤ A` is fully faithful. -/ @[simps] def fullyFaithfulSheafToPresheaf : (sheafToPresheaf J A).FullyFaithful where preimage f := ⟨f⟩ variable {J A} in /-- The bijection `(X ⟶ Y) ≃ (X.val ⟶ Y.val)` when `X` and `Y` are sheaves. -/ abbrev Sheaf.homEquiv {X Y : Sheaf J A} : (X ⟶ Y) ≃ (X.val ⟶ Y.val) := (fullyFaithfulSheafToPresheaf J A).homEquiv instance : (sheafToPresheaf J A).Full := (fullyFaithfulSheafToPresheaf J A).full instance : (sheafToPresheaf J A).Faithful := (fullyFaithfulSheafToPresheaf J A).faithful instance : (sheafToPresheaf J A).ReflectsIsomorphisms := (fullyFaithfulSheafToPresheaf J A).reflectsIsomorphisms /-- This is stated as a lemma to prevent class search from forming a loop since a sheaf morphism is monic if and only if it is monic as a presheaf morphism (under suitable assumption). -/ theorem Sheaf.Hom.mono_of_presheaf_mono {F G : Sheaf J A} (f : F ⟶ G) [h : Mono f.1] : Mono f := (sheafToPresheaf J A).mono_of_mono_map h instance Sheaf.Hom.epi_of_presheaf_epi {F G : Sheaf J A} (f : F ⟶ G) [h : Epi f.1] : Epi f := (sheafToPresheaf J A).epi_of_epi_map h theorem isSheaf_iff_isSheaf_of_type (P : Cᵒᵖ ⥤ Type w) : Presheaf.IsSheaf J P ↔ Presieve.IsSheaf J P := by constructor · intro hP refine Presieve.isSheaf_iso J ?_ (hP PUnit) exact isoWhiskerLeft _ Coyoneda.punitIso ≪≫ P.rightUnitor · intro hP X Y S hS z hz refine ⟨fun x => (hP S hS).amalgamate (fun Z f hf => z f hf x) ?_, ?_, ?_⟩ · intro Y₁ Y₂ Z g₁ g₂ f₁ f₂ hf₁ hf₂ h exact congr_fun (hz g₁ g₂ hf₁ hf₂ h) x · intro Z f hf funext x apply Presieve.IsSheafFor.valid_glue · intro y hy funext x apply (hP S hS).isSeparatedFor.ext intro Y' f hf rw [Presieve.IsSheafFor.valid_glue _ _ _ hf, ← hy _ hf] rfl /-- The sheaf of sections guaranteed by the sheaf condition. -/ @[simps] def sheafOver {A : Type u₂} [Category.{v₂} A] {J : GrothendieckTopology C} (ℱ : Sheaf J A) (E : A) : Sheaf J (Type _) where val := ℱ.val ⋙ coyoneda.obj (op E) cond := by rw [isSheaf_iff_isSheaf_of_type] exact ℱ.cond E variable {J} in lemma Presheaf.IsSheaf.isSheafFor {P : Cᵒᵖ ⥤ Type w} (hP : Presheaf.IsSheaf J P) {X : C} (S : Sieve X) (hS : S ∈ J X) : Presieve.IsSheafFor P S.arrows := by rw [isSheaf_iff_isSheaf_of_type] at hP exact hP S hS variable {A} in lemma Presheaf.isSheaf_bot (P : Cᵒᵖ ⥤ A) : IsSheaf ⊥ P := fun _ ↦ Presieve.isSheaf_bot /-- The category of sheaves on the bottom (trivial) Grothendieck topology is equivalent to the category of presheaves. -/ @[simps] def sheafBotEquivalence : Sheaf (⊥ : GrothendieckTopology C) A ≌ Cᵒᵖ ⥤ A where functor := sheafToPresheaf _ _ inverse := { obj := fun P => ⟨P, Presheaf.isSheaf_bot P⟩ map := fun f => ⟨f⟩ } unitIso := Iso.refl _ counitIso := Iso.refl _ instance : Inhabited (Sheaf (⊥ : GrothendieckTopology C) (Type w)) := ⟨(sheafBotEquivalence _).inverse.obj ((Functor.const _).obj default)⟩ variable {J} {A} /-- If the empty sieve is a cover of `X`, then `F(X)` is terminal. -/ def Sheaf.isTerminalOfBotCover (F : Sheaf J A) (X : C) (H : ⊥ ∈ J X) : IsTerminal (F.1.obj (op X)) := by refine @IsTerminal.ofUnique _ _ _ ?_ intro Y choose t h using F.2 Y _ H (by tauto) (by tauto) exact ⟨⟨t⟩, fun a => h.2 a (by tauto)⟩ section Preadditive open Preadditive variable [Preadditive A] {P Q : Sheaf J A} instance sheafHomHasZSMul : SMul ℤ (P ⟶ Q) where smul n f := Sheaf.Hom.mk { app := fun U => n • f.1.app U naturality := fun U V i => by induction' n with n ih n ih · simp only [zero_smul, comp_zero, zero_comp] · simpa only [add_zsmul, one_zsmul, comp_add, NatTrans.naturality, add_comp, add_left_inj] · simpa only [sub_smul, one_zsmul, comp_sub, NatTrans.naturality, sub_comp, sub_left_inj] using ih } instance : Sub (P ⟶ Q) where sub f g := Sheaf.Hom.mk <| f.1 - g.1 instance : Neg (P ⟶ Q) where neg f := Sheaf.Hom.mk <| -f.1 instance sheafHomHasNSMul : SMul ℕ (P ⟶ Q) where smul n f := Sheaf.Hom.mk { app := fun U => n • f.1.app U naturality := fun U V i => by induction n with | zero => simp only [zero_smul, comp_zero, zero_comp] | succ n ih => simp only [Nat.succ_eq_add_one, add_smul, ih, one_nsmul, comp_add, NatTrans.naturality, add_comp] } instance : Zero (P ⟶ Q) where zero := Sheaf.Hom.mk 0 instance : Add (P ⟶ Q) where add f g := Sheaf.Hom.mk <| f.1 + g.1 @[simp] theorem Sheaf.Hom.add_app (f g : P ⟶ Q) (U) : (f + g).1.app U = f.1.app U + g.1.app U := rfl instance Sheaf.Hom.addCommGroup : AddCommGroup (P ⟶ Q) := Function.Injective.addCommGroup (fun f : Sheaf.Hom P Q => f.1) (fun _ _ h => Sheaf.Hom.ext h) rfl (fun _ _ => rfl) (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => by aesop_cat) (fun _ _ => by aesop_cat) instance : Preadditive (Sheaf J A) where homGroup _ _ := Sheaf.Hom.addCommGroup end Preadditive end CategoryTheory namespace CategoryTheory open Opposite CategoryTheory Category Limits Sieve namespace Presheaf -- Under here is the equalizer story, which is equivalent if A has products (and doesn't -- make sense otherwise). It's described in https://stacks.math.columbia.edu/tag/00VL, -- between 00VQ and 00VR. variable {C : Type u₁} [Category.{v₁} C] -- `A` is a general category; `A'` is a variant where the morphisms live in a large enough -- universe to guarantee that we can take limits in A of things coming from C. -- I would have liked to use something like `UnivLE.{max v₁ u₁, v₂}` as a hypothesis on -- `A`'s morphism universe rather than introducing `A'` but I can't get it to work. -- So, for now, results which need max v₁ u₁ ≤ v₂ are just stated for `A'` and `P' : Cᵒᵖ ⥤ A'` -- instead. variable {A : Type u₂} [Category.{v₂} A] variable {A' : Type u₂} [Category.{max v₁ u₁} A'] variable {B : Type u₃} [Category.{v₃} B] variable (J : GrothendieckTopology C) variable {U : C} (R : Presieve U) variable (P : Cᵒᵖ ⥤ A) (P' : Cᵒᵖ ⥤ A') section MultiequalizerConditions /-- When `P` is a sheaf and `S` is a cover, the associated multifork is a limit. -/ def isLimitOfIsSheaf {X : C} (S : J.Cover X) (hP : IsSheaf J P) : IsLimit (S.multifork P) where lift := fun E : Multifork _ => hP.amalgamate S (fun _ => E.ι _) (fun _ _ r => E.condition ⟨r⟩) fac := by rintro (E : Multifork _) (a | b) · apply hP.amalgamate_map · rw [← E.w (WalkingMulticospan.Hom.fst b), ← (S.multifork P).w (WalkingMulticospan.Hom.fst b), ← assoc] congr 1 apply hP.amalgamate_map uniq := by rintro (E : Multifork _) m hm apply hP.hom_ext S intro I erw [hm (WalkingMulticospan.left I)] symm apply hP.amalgamate_map theorem isSheaf_iff_multifork : IsSheaf J P ↔ ∀ (X : C) (S : J.Cover X), Nonempty (IsLimit (S.multifork P)) := by refine ⟨fun hP X S => ⟨isLimitOfIsSheaf _ _ _ hP⟩, ?_⟩ intro h E X S hS x hx let T : J.Cover X := ⟨S, hS⟩ obtain ⟨hh⟩ := h _ T let K : Multifork (T.index P) := Multifork.ofι _ E (fun I => x I.f I.hf) (fun I => hx _ _ _ _ I.r.w) use hh.lift K dsimp; constructor · intro Y f hf apply hh.fac K (WalkingMulticospan.left ⟨Y, f, hf⟩) · intro e he apply hh.uniq K rintro (a | b) · apply he · rw [← K.w (WalkingMulticospan.Hom.fst b), ← (T.multifork P).w (WalkingMulticospan.Hom.fst b), ← assoc] congr 1 apply he variable {J P} in /-- If `F : Cᵒᵖ ⥤ A` is a sheaf for a Grothendieck topology `J` on `C`, and `S` is a cover of `X : C`, then the multifork `S.multifork F` is limit. -/ def IsSheaf.isLimitMultifork (hP : Presheaf.IsSheaf J P) {X : C} (S : J.Cover X) : IsLimit (S.multifork P) := by rw [Presheaf.isSheaf_iff_multifork] at hP exact (hP X S).some theorem isSheaf_iff_multiequalizer [∀ (X : C) (S : J.Cover X), HasMultiequalizer (S.index P)] : IsSheaf J P ↔ ∀ (X : C) (S : J.Cover X), IsIso (S.toMultiequalizer P) := by rw [isSheaf_iff_multifork] refine forall₂_congr fun X S => ⟨?_, ?_⟩ · rintro ⟨h⟩ let e : P.obj (op X) ≅ multiequalizer (S.index P) := h.conePointUniqueUpToIso (limit.isLimit _) exact (inferInstance : IsIso e.hom) · intro h refine ⟨IsLimit.ofIsoLimit (limit.isLimit _) (Cones.ext ?_ ?_)⟩ · apply (@asIso _ _ _ _ _ h).symm · intro a symm simp end MultiequalizerConditions section variable [HasProducts.{max u₁ v₁} A] variable [HasProducts.{max u₁ v₁} A'] /-- The middle object of the fork diagram given in Equation (3) of [MM92], as well as the fork diagram of the Stacks entry. -/ @[stacks 00VM "The middle object of the fork diagram there."] def firstObj : A := ∏ᶜ fun f : ΣV, { f : V ⟶ U // R f } => P.obj (op f.1) /-- The left morphism of the fork diagram given in Equation (3) of [MM92], as well as the fork diagram of the Stacks entry. -/ @[stacks 00VM "The left morphism the fork diagram there."] def forkMap : P.obj (op U) ⟶ firstObj R P := Pi.lift fun f => P.map f.2.1.op variable [HasPullbacks C] /-- The rightmost object of the fork diagram of the Stacks entry, which contains the data used to check a family of elements for a presieve is compatible. -/ @[stacks 00VM "The rightmost object of the fork diagram there."] def secondObj : A := ∏ᶜ fun fg : (ΣV, { f : V ⟶ U // R f }) × ΣW, { g : W ⟶ U // R g } => P.obj (op (pullback fg.1.2.1 fg.2.2.1)) /-- The map `pr₀*` of the Stacks entry. -/ @[stacks 00VM "The map `pr₀*` there."] def firstMap : firstObj R P ⟶ secondObj R P := Pi.lift fun _ => Pi.π _ _ ≫ P.map (pullback.fst _ _).op /-- The map `pr₁*` of the Stacks entry. -/ @[stacks 00VM "The map `pr₁*` there."] def secondMap : firstObj R P ⟶ secondObj R P := Pi.lift fun _ => Pi.π _ _ ≫ P.map (pullback.snd _ _).op theorem w : forkMap R P ≫ firstMap R P = forkMap R P ≫ secondMap R P := by apply limit.hom_ext rintro ⟨⟨Y, f, hf⟩, ⟨Z, g, hg⟩⟩ simp only [firstMap, secondMap, forkMap, limit.lift_π, limit.lift_π_assoc, assoc, Fan.mk_π_app, Subtype.coe_mk] rw [← P.map_comp, ← op_comp, pullback.condition] simp /-- An alternative definition of the sheaf condition in terms of equalizers. This is shown to be equivalent in `CategoryTheory.Presheaf.isSheaf_iff_isSheaf'`. -/ def IsSheaf' (P : Cᵒᵖ ⥤ A) : Prop := ∀ (U : C) (R : Presieve U) (_ : generate R ∈ J U), Nonempty (IsLimit (Fork.ofι _ (w R P))) -- Again I wonder whether `UnivLE` can somehow be used to allow `s` to take -- values in a more general universe. /-- (Implementation). An auxiliary lemma to convert between sheaf conditions. -/ def isSheafForIsSheafFor' (P : Cᵒᵖ ⥤ A) (s : A ⥤ Type max v₁ u₁) [∀ J, PreservesLimitsOfShape (Discrete.{max v₁ u₁} J) s] (U : C) (R : Presieve U) : IsLimit (s.mapCone (Fork.ofι _ (w R P))) ≃ IsLimit (Fork.ofι _ (Equalizer.Presieve.w (P ⋙ s) R)) := by let e : parallelPair (s.map (firstMap R P)) (s.map (secondMap R P)) ≅ parallelPair (Equalizer.Presieve.firstMap (P ⋙ s) R) (Equalizer.Presieve.secondMap (P ⋙ s) R) := by refine parallelPair.ext (PreservesProduct.iso s _) ((PreservesProduct.iso s _)) (limit.hom_ext (fun j => ?_)) (limit.hom_ext (fun j => ?_)) · dsimp [Equalizer.Presieve.firstMap, firstMap] simp only [map_lift_piComparison, Functor.map_comp, limit.lift_π, Fan.mk_pt, Fan.mk_π_app, assoc, piComparison_comp_π_assoc] · dsimp [Equalizer.Presieve.secondMap, secondMap] simp only [map_lift_piComparison, Functor.map_comp, limit.lift_π, Fan.mk_pt, Fan.mk_π_app, assoc, piComparison_comp_π_assoc]
refine Equiv.trans (isLimitMapConeForkEquiv _ _) ?_ refine (IsLimit.postcomposeHomEquiv e _).symm.trans (IsLimit.equivIsoLimit (Fork.ext (Iso.refl _) ?_)) dsimp [Equalizer.forkMap, forkMap, e, Fork.ι] simp only [id_comp, map_lift_piComparison] -- Remark : this lemma uses `A'` not `A`; `A'` is `A` but with a universe
Mathlib/CategoryTheory/Sites/Sheaf.lean
657
663
/- Copyright (c) 2023 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou, Kim Morrison, Adam Topaz -/ import Mathlib.CategoryTheory.Limits.Preserves.Shapes.BinaryProducts import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Products import Mathlib.CategoryTheory.Limits.ConcreteCategory.Basic import Mathlib.CategoryTheory.Limits.Types.Shapes import Mathlib.CategoryTheory.Limits.Shapes.Multiequalizer import Mathlib.CategoryTheory.Limits.Shapes.Kernels import Mathlib.CategoryTheory.ConcreteCategory.EpiMono import Mathlib.CategoryTheory.Limits.Constructions.EpiMono /-! # Limits in concrete categories In this file, we combine the description of limits in `Types` and the API about the preservation of products and pullbacks in order to describe these limits in a concrete category `C`. If `F : J → C` is a family of objects in `C`, we define a bijection `Limits.Concrete.productEquiv F : ToType (∏ᶜ F) ≃ ∀ j, ToType (F j)`. Similarly, if `f₁ : X₁ ⟶ S` and `f₂ : X₂ ⟶ S` are two morphisms, the elements in `pullback f₁ f₂` are identified by `Limits.Concrete.pullbackEquiv` to compatible tuples of elements in `X₁ × X₂`. Some results are also obtained for the terminal object, binary products, wide-pullbacks, wide-pushouts, multiequalizers and cokernels. -/ universe w w' v u t r namespace CategoryTheory.Limits.Concrete variable {C : Type u} [Category.{v} C] section Products section ProductEquiv variable {FC : C → C → Type*} {CC : C → Type max w v} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] variable [ConcreteCategory.{max w v} C FC] {J : Type w} (F : J → C) [HasProduct F] [PreservesLimit (Discrete.functor F) (forget C)] /-- The equivalence `ToType (∏ᶜ F) ≃ ∀ j, ToType (F j)` if `F : J → C` is a family of objects in a concrete category `C`. -/ noncomputable def productEquiv : ToType (∏ᶜ F) ≃ ∀ j, ToType (F j) := ((PreservesProduct.iso (forget C) F) ≪≫ (Types.productIso.{w, v} fun j => ToType (F j))).toEquiv @[simp] lemma productEquiv_apply_apply (x : ToType (∏ᶜ F)) (j : J) : productEquiv F x j = Pi.π F j x := congr_fun (piComparison_comp_π (forget C) F j) x @[simp] lemma productEquiv_symm_apply_π (x : ∀ j, ToType (F j)) (j : J) : Pi.π F j ((productEquiv F).symm x) = x j := by rw [← productEquiv_apply_apply, Equiv.apply_symm_apply] end ProductEquiv section ProductExt variable {J : Type w} (f : J → C) [HasProduct f] {D : Type t} [Category.{r} D] variable {FD : D → D → Type*} {DD : D → Type max w r} [∀ X Y, FunLike (FD X Y) (DD X) (DD Y)] variable [ConcreteCategory.{max w r} D FD] (F : C ⥤ D) [PreservesLimit (Discrete.functor f) F] [HasProduct fun j => F.obj (f j)] [PreservesLimitsOfShape WalkingCospan (forget D)] [PreservesLimit (Discrete.functor fun b ↦ F.toPrefunctor.obj (f b)) (forget D)] lemma Pi.map_ext (x y : ToType (F.obj (∏ᶜ f : C))) (h : ∀ i, F.map (Pi.π f i) x = F.map (Pi.π f i) y) : x = y := by apply ConcreteCategory.injective_of_mono_of_preservesPullback (PreservesProduct.iso F f).hom apply Concrete.limit_ext _ (piComparison F _ x) (piComparison F _ y) intro ⟨j⟩ rw [← ConcreteCategory.comp_apply, ← ConcreteCategory.comp_apply, piComparison_comp_π] exact h j end ProductExt end Products section Terminal variable {FC : C → C → Type*} {CC : C → Type w} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] variable [ConcreteCategory.{w} C FC] /-- If `forget C` preserves terminals and `X` is terminal, then `ToType X` is a singleton. -/ noncomputable def uniqueOfTerminalOfPreserves [PreservesLimit (Functor.empty.{0} C) (forget C)] (X : C) (h : IsTerminal X) : Unique (ToType X) := Types.isTerminalEquivUnique (ToType X) <| IsTerminal.isTerminalObj (forget C) X h /-- If `forget C` reflects terminals and `ToType X` is a singleton, then `X` is terminal. -/ noncomputable def terminalOfUniqueOfReflects [ReflectsLimit (Functor.empty.{0} C) (forget C)] (X : C) (h : Unique (ToType X)) : IsTerminal X := IsTerminal.isTerminalOfObj (forget C) X <| (Types.isTerminalEquivUnique (ToType X)).symm h /-- The equivalence `IsTerminal X ≃ Unique (ToType X)` if the forgetful functor preserves and reflects terminals. -/ noncomputable def terminalIffUnique [PreservesLimit (Functor.empty.{0} C) (forget C)] [ReflectsLimit (Functor.empty.{0} C) (forget C)] (X : C) : IsTerminal X ≃ Unique (ToType X) := (IsTerminal.isTerminalIffObj (forget C) X).trans <| Types.isTerminalEquivUnique _ variable (C) variable [HasTerminal C] [PreservesLimit (Functor.empty.{0} C) (forget C)] /-- The equivalence `ToType (⊤_ C) ≃ PUnit` when `C` is a concrete category. -/ noncomputable def terminalEquiv : ToType (⊤_ C) ≃ PUnit := (PreservesTerminal.iso (forget C) ≪≫ Types.terminalIso).toEquiv noncomputable instance : Unique (ToType (⊤_ C)) where default := (terminalEquiv C).symm PUnit.unit uniq _ := (terminalEquiv C).injective (Subsingleton.elim _ _) end Terminal section Initial variable {FC : C → C → Type*} {CC : C → Type w} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] variable [ConcreteCategory.{w} C FC] /-- If `forget C` preserves initials and `X` is initial, then `ToType X` is empty. -/ lemma empty_of_initial_of_preserves [PreservesColimit (Functor.empty.{0} C) (forget C)] (X : C) (h : Nonempty (IsInitial X)) : IsEmpty (ToType X) := by rw [← Types.initial_iff_empty] exact Nonempty.map (IsInitial.isInitialObj (forget C) _) h /-- If `forget C` reflects initials and `ToType X` is empty, then `X` is initial. -/ lemma initial_of_empty_of_reflects [ReflectsColimit (Functor.empty.{0} C) (forget C)] (X : C) (h : IsEmpty (ToType X)) : Nonempty (IsInitial X) := Nonempty.map (IsInitial.isInitialOfObj (forget C) _) <| (Types.initial_iff_empty (ToType X)).mpr h /-- If `forget C` preserves and reflects initials, then `X` is initial if and only if `ToType X` is empty. -/ lemma initial_iff_empty_of_preserves_of_reflects [PreservesColimit (Functor.empty.{0} C) (forget C)] [ReflectsColimit (Functor.empty.{0} C) (forget C)] (X : C) : Nonempty (IsInitial X) ↔ IsEmpty (ToType X) := by rw [← Types.initial_iff_empty, (IsInitial.isInitialIffObj (forget C) X).nonempty_congr] rfl end Initial section BinaryProducts variable {FC : C → C → Type*} {CC : C → Type w} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] variable [ConcreteCategory.{w} C FC] (X₁ X₂ : C) [HasBinaryProduct X₁ X₂] [PreservesLimit (pair X₁ X₂) (forget C)] /-- The equivalence `ToType (X₁ ⨯ X₂) ≃ (ToType X₁) × (ToType X₂)` if `X₁` and `X₂` are objects in a concrete category `C`. -/ noncomputable def prodEquiv : ToType (X₁ ⨯ X₂) ≃ ToType X₁ × ToType X₂ := (PreservesLimitPair.iso (forget C) X₁ X₂ ≪≫ Types.binaryProductIso _ _).toEquiv @[simp] lemma prodEquiv_apply_fst (x : ToType (X₁ ⨯ X₂)) : (prodEquiv X₁ X₂ x).fst = (Limits.prod.fst : X₁ ⨯ X₂ ⟶ X₁) x := congr_fun (prodComparison_fst (forget C) X₁ X₂) x @[simp] lemma prodEquiv_apply_snd (x : ToType (X₁ ⨯ X₂)) : (prodEquiv X₁ X₂ x).snd = (Limits.prod.snd : X₁ ⨯ X₂ ⟶ X₂) x := congr_fun (prodComparison_snd (forget C) X₁ X₂) x @[simp] lemma prodEquiv_symm_apply_fst (x : ToType X₁ × ToType X₂) : (Limits.prod.fst : X₁ ⨯ X₂ ⟶ X₁) ((prodEquiv X₁ X₂).symm x) = x.1 := by obtain ⟨y, rfl⟩ := (prodEquiv X₁ X₂).surjective x simp @[simp] lemma prodEquiv_symm_apply_snd (x : ToType X₁ × ToType X₂) : (Limits.prod.snd : X₁ ⨯ X₂ ⟶ X₂) ((prodEquiv X₁ X₂).symm x) = x.2 := by obtain ⟨y, rfl⟩ := (prodEquiv X₁ X₂).surjective x simp end BinaryProducts section Pullbacks variable {FC : C → C → Type*} {CC : C → Type v} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] variable [ConcreteCategory.{v} C FC] variable {X₁ X₂ S : C} (f₁ : X₁ ⟶ S) (f₂ : X₂ ⟶ S) [HasPullback f₁ f₂] [PreservesLimit (cospan f₁ f₂) (forget C)] /-- In a concrete category `C`, given two morphisms `f₁ : X₁ ⟶ S` and `f₂ : X₂ ⟶ S`, the elements in `pullback f₁ f₁` can be identified to compatible tuples of elements in `X₁` and `X₂`. -/ noncomputable def pullbackEquiv : ToType (pullback f₁ f₂) ≃ { p : ToType X₁ × ToType X₂ // f₁ p.1 = f₂ p.2 } := (PreservesPullback.iso (forget C) f₁ f₂ ≪≫ Types.pullbackIsoPullback ⇑(ConcreteCategory.hom f₁) ⇑(ConcreteCategory.hom f₂)).toEquiv /-- Constructor for elements in a pullback in a concrete category. -/ noncomputable def pullbackMk (x₁ : ToType X₁) (x₂ : ToType X₂) (h : f₁ x₁ = f₂ x₂) : ToType (pullback f₁ f₂) := (pullbackEquiv f₁ f₂).symm ⟨⟨x₁, x₂⟩, h⟩ lemma pullbackMk_surjective (x : ToType (pullback f₁ f₂)) : ∃ (x₁ : ToType X₁) (x₂ : ToType X₂) (h : f₁ x₁ = f₂ x₂), x = pullbackMk f₁ f₂ x₁ x₂ h := by obtain ⟨⟨⟨x₁, x₂⟩, h⟩, rfl⟩ := (pullbackEquiv f₁ f₂).symm.surjective x exact ⟨x₁, x₂, h, rfl⟩ @[simp] lemma pullbackMk_fst (x₁ : ToType X₁) (x₂ : ToType X₂) (h : f₁ x₁ = f₂ x₂) : pullback.fst f₁ f₂ (pullbackMk f₁ f₂ x₁ x₂ h) = x₁ := (congr_fun (PreservesPullback.iso_inv_fst (forget C) f₁ f₂) _).trans (congr_fun (Types.pullbackIsoPullback_inv_fst ⇑(ConcreteCategory.hom f₁) ⇑(ConcreteCategory.hom f₂)) _) @[simp] lemma pullbackMk_snd (x₁ : ToType X₁) (x₂ : ToType X₂) (h : f₁ x₁ = f₂ x₂) : pullback.snd f₁ f₂ (pullbackMk f₁ f₂ x₁ x₂ h) = x₂ := (congr_fun (PreservesPullback.iso_inv_snd (forget C) f₁ f₂) _).trans (congr_fun (Types.pullbackIsoPullback_inv_snd ⇑(ConcreteCategory.hom f₁) ⇑(ConcreteCategory.hom f₂)) _) end Pullbacks section WidePullback variable {FC : C → C → Type*} {CC : C → Type (max v w)} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)]
variable [ConcreteCategory.{max v w} C FC] open WidePullback open WidePullbackShape theorem widePullback_ext {B : C} {ι : Type w} {X : ι → C} (f : ∀ j : ι, X j ⟶ B) [HasWidePullback B X f] [PreservesLimit (wideCospan B X f) (forget C)]
Mathlib/CategoryTheory/Limits/Shapes/ConcreteCategory.lean
229
236
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Yaël Dillies -/ import Mathlib.Data.List.Iterate import Mathlib.GroupTheory.Perm.Cycle.Basic import Mathlib.GroupTheory.NoncommPiCoprod import Mathlib.Tactic.Group /-! # Cycle factors of a permutation Let `β` be a `Fintype` and `f : Equiv.Perm β`. * `Equiv.Perm.cycleOf`: `f.cycleOf x` is the cycle of `f` that `x` belongs to. * `Equiv.Perm.cycleFactors`: `f.cycleFactors` is a list of disjoint cyclic permutations that multiply to `f`. -/ open Equiv Function Finset variable {ι α β : Type*} namespace Equiv.Perm /-! ### `cycleOf` -/ section CycleOf variable {f g : Perm α} {x y : α} /-- `f.cycleOf x` is the cycle of the permutation `f` to which `x` belongs. -/ def cycleOf (f : Perm α) [DecidableRel f.SameCycle] (x : α) : Perm α := ofSubtype (subtypePerm f fun _ => sameCycle_apply_right.symm : Perm { y // SameCycle f x y }) theorem cycleOf_apply (f : Perm α) [DecidableRel f.SameCycle] (x y : α) : cycleOf f x y = if SameCycle f x y then f y else y := by dsimp only [cycleOf] split_ifs with h · apply ofSubtype_apply_of_mem exact h · apply ofSubtype_apply_of_not_mem exact h theorem cycleOf_inv (f : Perm α) [DecidableRel f.SameCycle] (x : α) : (cycleOf f x)⁻¹ = cycleOf f⁻¹ x := Equiv.ext fun y => by rw [inv_eq_iff_eq, cycleOf_apply, cycleOf_apply] split_ifs <;> simp_all [sameCycle_inv, sameCycle_inv_apply_right] @[simp] theorem cycleOf_pow_apply_self (f : Perm α) [DecidableRel f.SameCycle] (x : α) : ∀ n : ℕ, (cycleOf f x ^ n) x = (f ^ n) x := by intro n induction n with | zero => rfl | succ n hn => rw [pow_succ', mul_apply, cycleOf_apply, hn, if_pos, pow_succ', mul_apply] exact ⟨n, rfl⟩ @[simp] theorem cycleOf_zpow_apply_self (f : Perm α) [DecidableRel f.SameCycle] (x : α) : ∀ n : ℤ, (cycleOf f x ^ n) x = (f ^ n) x := by intro z cases z with | ofNat z => exact cycleOf_pow_apply_self f x z | negSucc z => rw [zpow_negSucc, ← inv_pow, cycleOf_inv, zpow_negSucc, ← inv_pow, cycleOf_pow_apply_self] theorem SameCycle.cycleOf_apply [DecidableRel f.SameCycle] : SameCycle f x y → cycleOf f x y = f y := ofSubtype_apply_of_mem _ theorem cycleOf_apply_of_not_sameCycle [DecidableRel f.SameCycle] : ¬SameCycle f x y → cycleOf f x y = y := ofSubtype_apply_of_not_mem _ theorem SameCycle.cycleOf_eq [DecidableRel f.SameCycle] (h : SameCycle f x y) : cycleOf f x = cycleOf f y := by ext z rw [Equiv.Perm.cycleOf_apply] split_ifs with hz · exact (h.symm.trans hz).cycleOf_apply.symm · exact (cycleOf_apply_of_not_sameCycle (mt h.trans hz)).symm @[simp] theorem cycleOf_apply_apply_zpow_self (f : Perm α) [DecidableRel f.SameCycle] (x : α) (k : ℤ) : cycleOf f x ((f ^ k) x) = (f ^ (k + 1) : Perm α) x := by rw [SameCycle.cycleOf_apply] · rw [add_comm, zpow_add, zpow_one, mul_apply] · exact ⟨k, rfl⟩ @[simp] theorem cycleOf_apply_apply_pow_self (f : Perm α) [DecidableRel f.SameCycle] (x : α) (k : ℕ) : cycleOf f x ((f ^ k) x) = (f ^ (k + 1) : Perm α) x := by convert cycleOf_apply_apply_zpow_self f x k using 1 @[simp] theorem cycleOf_apply_apply_self (f : Perm α) [DecidableRel f.SameCycle] (x : α) : cycleOf f x (f x) = f (f x) := by convert cycleOf_apply_apply_pow_self f x 1 using 1 @[simp] theorem cycleOf_apply_self (f : Perm α) [DecidableRel f.SameCycle] (x : α) : cycleOf f x x = f x := SameCycle.rfl.cycleOf_apply theorem IsCycle.cycleOf_eq [DecidableRel f.SameCycle] (hf : IsCycle f) (hx : f x ≠ x) : cycleOf f x = f := Equiv.ext fun y => if h : SameCycle f x y then by rw [h.cycleOf_apply] else by rw [cycleOf_apply_of_not_sameCycle h, Classical.not_not.1 (mt ((isCycle_iff_sameCycle hx).1 hf).2 h)] @[simp] theorem cycleOf_eq_one_iff (f : Perm α) [DecidableRel f.SameCycle] : cycleOf f x = 1 ↔ f x = x := by simp_rw [Perm.ext_iff, cycleOf_apply, one_apply] refine ⟨fun h => (if_pos (SameCycle.refl f x)).symm.trans (h x), fun h y => ?_⟩ by_cases hy : f y = y · rw [hy, ite_self] · exact if_neg (mt SameCycle.apply_eq_self_iff (by tauto)) @[simp] theorem cycleOf_self_apply (f : Perm α) [DecidableRel f.SameCycle] (x : α) : cycleOf f (f x) = cycleOf f x := (sameCycle_apply_right.2 SameCycle.rfl).symm.cycleOf_eq @[simp] theorem cycleOf_self_apply_pow (f : Perm α) [DecidableRel f.SameCycle] (n : ℕ) (x : α) : cycleOf f ((f ^ n) x) = cycleOf f x := SameCycle.rfl.pow_left.cycleOf_eq @[simp] theorem cycleOf_self_apply_zpow (f : Perm α) [DecidableRel f.SameCycle] (n : ℤ) (x : α) : cycleOf f ((f ^ n) x) = cycleOf f x := SameCycle.rfl.zpow_left.cycleOf_eq protected theorem IsCycle.cycleOf [DecidableRel f.SameCycle] [DecidableEq α] (hf : IsCycle f) : cycleOf f x = if f x = x then 1 else f := by by_cases hx : f x = x · rwa [if_pos hx, cycleOf_eq_one_iff] · rwa [if_neg hx, hf.cycleOf_eq] theorem cycleOf_one [DecidableRel (1 : Perm α).SameCycle] (x : α) : cycleOf 1 x = 1 := (cycleOf_eq_one_iff 1).mpr rfl theorem isCycle_cycleOf (f : Perm α) [DecidableRel f.SameCycle] (hx : f x ≠ x) : IsCycle (cycleOf f x) := have : cycleOf f x x ≠ x := by rwa [SameCycle.rfl.cycleOf_apply] (isCycle_iff_sameCycle this).2 @fun y => ⟨fun h => mt h.apply_eq_self_iff.2 this, fun h => if hxy : SameCycle f x y then let ⟨i, hi⟩ := hxy ⟨i, by rw [cycleOf_zpow_apply_self, hi]⟩ else by rw [cycleOf_apply_of_not_sameCycle hxy] at h exact (h rfl).elim⟩ theorem pow_mod_orderOf_cycleOf_apply (f : Perm α) [DecidableRel f.SameCycle] (n : ℕ) (x : α) : (f ^ (n % orderOf (cycleOf f x))) x = (f ^ n) x := by rw [← cycleOf_pow_apply_self f, ← cycleOf_pow_apply_self f, pow_mod_orderOf] theorem cycleOf_mul_of_apply_right_eq_self [DecidableRel f.SameCycle] [DecidableRel (f * g).SameCycle] (h : Commute f g) (x : α) (hx : g x = x) : (f * g).cycleOf x = f.cycleOf x := by ext y by_cases hxy : (f * g).SameCycle x y · obtain ⟨z, rfl⟩ := hxy rw [cycleOf_apply_apply_zpow_self] simp [h.mul_zpow, zpow_apply_eq_self_of_apply_eq_self hx] · rw [cycleOf_apply_of_not_sameCycle hxy, cycleOf_apply_of_not_sameCycle] contrapose! hxy obtain ⟨z, rfl⟩ := hxy refine ⟨z, ?_⟩ simp [h.mul_zpow, zpow_apply_eq_self_of_apply_eq_self hx] theorem Disjoint.cycleOf_mul_distrib [DecidableRel f.SameCycle] [DecidableRel g.SameCycle] [DecidableRel (f * g).SameCycle] [DecidableRel (g * f).SameCycle] (h : f.Disjoint g) (x : α) : (f * g).cycleOf x = f.cycleOf x * g.cycleOf x := by rcases (disjoint_iff_eq_or_eq.mp h) x with hfx | hgx · simp [h.commute.eq, cycleOf_mul_of_apply_right_eq_self h.symm.commute, hfx] · simp [cycleOf_mul_of_apply_right_eq_self h.commute, hgx] private theorem mem_support_cycleOf_iff_aux [DecidableRel f.SameCycle] [DecidableEq α] [Fintype α] : y ∈ support (f.cycleOf x) ↔ SameCycle f x y ∧ x ∈ support f := by by_cases hx : f x = x · rw [(cycleOf_eq_one_iff _).mpr hx] simp [hx] · rw [mem_support, cycleOf_apply] split_ifs with hy · simp only [hx, hy, Ne, not_false_iff, and_self_iff, mem_support] rcases hy with ⟨k, rfl⟩ rw [← not_mem_support] simpa using hx · simpa [hx] using hy private theorem mem_support_cycleOf_iff'_aux (hx : f x ≠ x) [DecidableRel f.SameCycle] [DecidableEq α] [Fintype α] : y ∈ support (f.cycleOf x) ↔ SameCycle f x y := by rw [mem_support_cycleOf_iff_aux, and_iff_left (mem_support.2 hx)] /-- `x` is in the support of `f` iff `Equiv.Perm.cycle_of f x` is a cycle. -/ theorem isCycle_cycleOf_iff (f : Perm α) [DecidableRel f.SameCycle] : IsCycle (cycleOf f x) ↔ f x ≠ x := by refine ⟨fun hx => ?_, f.isCycle_cycleOf⟩ rw [Ne, ← cycleOf_eq_one_iff f] exact hx.ne_one private theorem isCycleOn_support_cycleOf_aux [DecidableEq α] [Fintype α] (f : Perm α) [DecidableRel f.SameCycle] (x : α) : f.IsCycleOn (f.cycleOf x).support := ⟨f.bijOn <| by refine fun _ ↦ ⟨fun h ↦ mem_support_cycleOf_iff_aux.2 ?_, fun h ↦ mem_support_cycleOf_iff_aux.2 ?_⟩ · exact ⟨sameCycle_apply_right.1 (mem_support_cycleOf_iff_aux.1 h).1, (mem_support_cycleOf_iff_aux.1 h).2⟩ · exact ⟨sameCycle_apply_right.2 (mem_support_cycleOf_iff_aux.1 h).1, (mem_support_cycleOf_iff_aux.1 h).2⟩ , fun a ha b hb => by rw [mem_coe, mem_support_cycleOf_iff_aux] at ha hb exact ha.1.symm.trans hb.1⟩ private theorem SameCycle.exists_pow_eq_of_mem_support_aux {f} [DecidableEq α] [Fintype α] [DecidableRel f.SameCycle] (h : SameCycle f x y) (hx : x ∈ f.support) : ∃ i < #(f.cycleOf x).support, (f ^ i) x = y := by rw [mem_support] at hx exact Equiv.Perm.IsCycleOn.exists_pow_eq (b := y) (f.isCycleOn_support_cycleOf_aux x) (by rw [mem_support_cycleOf_iff'_aux hx]) (by rwa [mem_support_cycleOf_iff'_aux hx]) instance instDecidableRelSameCycle [DecidableEq α] [Fintype α] (f : Perm α) : DecidableRel (SameCycle f) := fun x y => decidable_of_iff (y ∈ List.iterate f x (Fintype.card α)) <| by simp only [List.mem_iterate, iterate_eq_pow, eq_comm (a := y)] constructor · rintro ⟨n, _, hn⟩ exact ⟨n, hn⟩ · intro hxy by_cases hx : x ∈ f.support case pos => -- we can't invoke the aux lemmas above without obtaining the decidable instance we are -- already building; but now we've left the data, so we can do this non-constructively -- without sacrificing computability. let _inst (f : Perm α) : DecidableRel (SameCycle f) := Classical.decRel _ rcases hxy.exists_pow_eq_of_mem_support_aux hx with ⟨i, hixy, hi⟩ refine ⟨i, lt_of_lt_of_le hixy (card_le_univ _), hi⟩ case neg => haveI : Nonempty α := ⟨x⟩ rw [not_mem_support] at hx exact ⟨0, Fintype.card_pos, hxy.eq_of_left hx⟩ @[simp] theorem two_le_card_support_cycleOf_iff [DecidableEq α] [Fintype α] : 2 ≤ #(cycleOf f x).support ↔ f x ≠ x := by refine ⟨fun h => ?_, fun h => by simpa using (isCycle_cycleOf _ h).two_le_card_support⟩ contrapose! h rw [← cycleOf_eq_one_iff] at h simp [h] @[simp] lemma support_cycleOf_nonempty [DecidableEq α] [Fintype α] : (cycleOf f x).support.Nonempty ↔ f x ≠ x := by rw [← two_le_card_support_cycleOf_iff, ← card_pos, ← Nat.succ_le_iff] exact ⟨fun h => Or.resolve_left h.eq_or_lt (card_support_ne_one _).symm, zero_lt_two.trans_le⟩ theorem mem_support_cycleOf_iff [DecidableEq α] [Fintype α] : y ∈ support (f.cycleOf x) ↔ SameCycle f x y ∧ x ∈ support f := mem_support_cycleOf_iff_aux theorem mem_support_cycleOf_iff' (hx : f x ≠ x) [DecidableEq α] [Fintype α] : y ∈ support (f.cycleOf x) ↔ SameCycle f x y := mem_support_cycleOf_iff'_aux hx theorem sameCycle_iff_cycleOf_eq_of_mem_support [DecidableEq α] [Fintype α] {g : Perm α} {x y : α} (hx : x ∈ g.support) (hy : y ∈ g.support) : g.SameCycle x y ↔ g.cycleOf x = g.cycleOf y := by refine ⟨SameCycle.cycleOf_eq, fun h ↦ ?_⟩ rw [← mem_support_cycleOf_iff' (mem_support.mp hx), h, mem_support_cycleOf_iff' (mem_support.mp hy)] theorem support_cycleOf_eq_nil_iff [DecidableEq α] [Fintype α] : (f.cycleOf x).support = ∅ ↔ x ∉ f.support := by simp theorem isCycleOn_support_cycleOf [DecidableEq α] [Fintype α] (f : Perm α) (x : α) : f.IsCycleOn (f.cycleOf x).support := isCycleOn_support_cycleOf_aux f x theorem SameCycle.exists_pow_eq_of_mem_support {f} [DecidableEq α] [Fintype α] (h : SameCycle f x y) (hx : x ∈ f.support) : ∃ i < #(f.cycleOf x).support, (f ^ i) x = y := h.exists_pow_eq_of_mem_support_aux hx theorem support_cycleOf_le [DecidableEq α] [Fintype α] (f : Perm α) (x : α) : support (f.cycleOf x) ≤ support f := by intro y hy rw [mem_support, cycleOf_apply] at hy split_ifs at hy · exact mem_support.mpr hy · exact absurd rfl hy theorem SameCycle.mem_support_iff {f} [DecidableEq α] [Fintype α] (h : SameCycle f x y) : x ∈ support f ↔ y ∈ support f := ⟨fun hx => support_cycleOf_le f x (mem_support_cycleOf_iff.mpr ⟨h, hx⟩), fun hy => support_cycleOf_le f y (mem_support_cycleOf_iff.mpr ⟨h.symm, hy⟩)⟩ theorem pow_mod_card_support_cycleOf_self_apply [DecidableEq α] [Fintype α] (f : Perm α) (n : ℕ) (x : α) : (f ^ (n % #(f.cycleOf x).support)) x = (f ^ n) x := by by_cases hx : f x = x · rw [pow_apply_eq_self_of_apply_eq_self hx, pow_apply_eq_self_of_apply_eq_self hx] · rw [← cycleOf_pow_apply_self, ← cycleOf_pow_apply_self f, ← (isCycle_cycleOf f hx).orderOf, pow_mod_orderOf] theorem SameCycle.exists_pow_eq [DecidableEq α] [Fintype α] (f : Perm α) (h : SameCycle f x y) : ∃ i : ℕ, 0 < i ∧ i ≤ #(f.cycleOf x).support + 1 ∧ (f ^ i) x = y := by by_cases hx : x ∈ f.support · obtain ⟨k, hk, hk'⟩ := h.exists_pow_eq_of_mem_support hx rcases k with - | k · refine ⟨#(f.cycleOf x).support, ?_, self_le_add_right _ _, ?_⟩ · refine zero_lt_one.trans (one_lt_card_support_of_ne_one ?_) simpa using hx · simp only [pow_zero, coe_one, id_eq] at hk' subst hk' rw [← (isCycle_cycleOf _ <| mem_support.1 hx).orderOf, ← cycleOf_pow_apply_self, pow_orderOf_eq_one, one_apply] · exact ⟨k + 1, by simp, Nat.le_succ_of_le hk.le, hk'⟩ · refine ⟨1, zero_lt_one, by simp, ?_⟩ obtain ⟨k, rfl⟩ := h rw [not_mem_support] at hx rw [pow_apply_eq_self_of_apply_eq_self hx, zpow_apply_eq_self_of_apply_eq_self hx] theorem zpow_eq_zpow_on_iff [DecidableEq α] [Fintype α] (g : Perm α) {m n : ℤ} {x : α} (hx : g x ≠ x) : (g ^ m) x = (g ^ n) x ↔ m % #(g.cycleOf x).support = n % #(g.cycleOf x).support := by rw [Int.emod_eq_emod_iff_emod_sub_eq_zero] conv_lhs => rw [← Int.sub_add_cancel m n, Int.add_comm, zpow_add] simp only [coe_mul, Function.comp_apply, EmbeddingLike.apply_eq_iff_eq] rw [← Int.dvd_iff_emod_eq_zero] rw [← cycleOf_zpow_apply_self g x, cycle_zpow_mem_support_iff] · rw [← Int.dvd_iff_emod_eq_zero] · exact isCycle_cycleOf g hx · simp only [mem_support, cycleOf_apply_self]; exact hx end CycleOf /-! ### `cycleFactors` -/ section cycleFactors open scoped List in /-- Given a list `l : List α` and a permutation `f : Perm α` whose nonfixed points are all in `l`, recursively factors `f` into cycles. -/ def cycleFactorsAux [DecidableEq α] [Fintype α] (l : List α) (f : Perm α) (h : ∀ {x}, f x ≠ x → x ∈ l) : { pl : List (Perm α) // pl.prod = f ∧ (∀ g ∈ pl, IsCycle g) ∧ pl.Pairwise Disjoint } := go l f h (fun _ => rfl) where /-- The auxiliary of `cycleFactorsAux`. This functions separates cycles from `f` instead of `g` to prevent the process of a cycle gets complex. -/ go (l : List α) (g : Perm α) (hg : ∀ {x}, g x ≠ x → x ∈ l) (hfg : ∀ {x}, g x ≠ x → cycleOf f x = cycleOf g x) : { pl : List (Perm α) // pl.prod = g ∧ (∀ g' ∈ pl, IsCycle g') ∧ pl.Pairwise Disjoint } := match l with | [] => ⟨[], by { simp only [imp_false, List.Pairwise.nil, List.not_mem_nil, forall_const, and_true, forall_prop_of_false, Classical.not_not, not_false_iff, List.prod_nil] at * ext simp [*]}⟩ | x :: l => if hx : g x = x then go l g (by intro y hy; exact List.mem_of_ne_of_mem (fun h => hy (by rwa [h])) (hg hy)) hfg else let ⟨m, hm₁, hm₂, hm₃⟩ := go l ((cycleOf f x)⁻¹ * g) (by rw [hfg hx] intro y hy exact List.mem_of_ne_of_mem (fun h : y = x => by rw [h, mul_apply, Ne, inv_eq_iff_eq, cycleOf_apply_self] at hy exact hy rfl) (hg fun h : g y = y => by rw [mul_apply, h, Ne, inv_eq_iff_eq, cycleOf_apply] at hy split_ifs at hy <;> tauto)) (by rw [hfg hx] intro y hy simp [inv_eq_iff_eq, cycleOf_apply, eq_comm (a := g y)] at hy rw [hfg (Ne.symm hy.right), ← mul_inv_eq_one (a := g.cycleOf y), cycleOf_inv] simp_rw [mul_inv_rev] rw [inv_inv, cycleOf_mul_of_apply_right_eq_self, ← cycleOf_inv, mul_inv_eq_one] · rw [Commute.inv_left_iff, commute_iff_eq] ext z; by_cases hz : SameCycle g x z · simp [cycleOf_apply, hz] · simp [cycleOf_apply_of_not_sameCycle, hz] · exact cycleOf_apply_of_not_sameCycle hy.left) ⟨cycleOf f x :: m, by rw [hfg hx] at hm₁ ⊢ constructor · rw [List.prod_cons, hm₁] simp · exact ⟨fun g' hg' => ((List.mem_cons).1 hg').elim (fun hg' => hg'.symm ▸ isCycle_cycleOf _ hx) (hm₂ g'), List.pairwise_cons.2 ⟨fun g' hg' y => or_iff_not_imp_left.2 fun hgy => have hxy : SameCycle g x y := Classical.not_not.1 (mt cycleOf_apply_of_not_sameCycle hgy) have hg'm : (g' :: m.erase g') ~ m := List.cons_perm_iff_perm_erase.2 ⟨hg', List.Perm.refl _⟩ have : ∀ h ∈ m.erase g', Disjoint g' h := (List.pairwise_cons.1 ((hg'm.pairwise_iff Disjoint.symm).2 hm₃)).1 by_cases id fun hg'y : g' y ≠ y => (disjoint_prod_right _ this y).resolve_right <| by have hsc : SameCycle g⁻¹ x (g y) := by rwa [sameCycle_inv, sameCycle_apply_right] rw [disjoint_prod_perm hm₃ hg'm.symm, List.prod_cons, ← eq_inv_mul_iff_mul_eq] at hm₁ rwa [hm₁, mul_apply, mul_apply, cycleOf_inv, hsc.cycleOf_apply, inv_apply_self, inv_eq_iff_eq, eq_comm], hm₃⟩⟩⟩ theorem mem_list_cycles_iff {α : Type*} [Finite α] {l : List (Perm α)} (h1 : ∀ σ : Perm α, σ ∈ l → σ.IsCycle) (h2 : l.Pairwise Disjoint) {σ : Perm α} : σ ∈ l ↔ σ.IsCycle ∧ ∀ a, σ a ≠ a → σ a = l.prod a := by suffices σ.IsCycle → (σ ∈ l ↔ ∀ a, σ a ≠ a → σ a = l.prod a) by exact ⟨fun hσ => ⟨h1 σ hσ, (this (h1 σ hσ)).mp hσ⟩, fun hσ => (this hσ.1).mpr hσ.2⟩ intro h3 classical cases nonempty_fintype α constructor · intro h a ha exact eq_on_support_mem_disjoint h h2 _ (mem_support.mpr ha) · intro h have hσl : σ.support ⊆ l.prod.support := by intro x hx rw [mem_support] at hx rwa [mem_support, ← h _ hx] obtain ⟨a, ha, -⟩ := id h3 rw [← mem_support] at ha obtain ⟨τ, hτ, hτa⟩ := exists_mem_support_of_mem_support_prod (hσl ha) have hτl : ∀ x ∈ τ.support, τ x = l.prod x := eq_on_support_mem_disjoint hτ h2 have key : ∀ x ∈ σ.support ∩ τ.support, σ x = τ x := by intro x hx rw [h x (mem_support.mp (mem_of_mem_inter_left hx)), hτl x (mem_of_mem_inter_right hx)] convert hτ refine h3.eq_on_support_inter_nonempty_congr (h1 _ hτ) key ?_ ha exact key a (mem_inter_of_mem ha hτa) open scoped List in theorem list_cycles_perm_list_cycles {α : Type*} [Finite α] {l₁ l₂ : List (Perm α)} (h₀ : l₁.prod = l₂.prod) (h₁l₁ : ∀ σ : Perm α, σ ∈ l₁ → σ.IsCycle) (h₁l₂ : ∀ σ : Perm α, σ ∈ l₂ → σ.IsCycle) (h₂l₁ : l₁.Pairwise Disjoint) (h₂l₂ : l₂.Pairwise Disjoint) : l₁ ~ l₂ := by classical refine (List.perm_ext_iff_of_nodup (nodup_of_pairwise_disjoint_cycles h₁l₁ h₂l₁) (nodup_of_pairwise_disjoint_cycles h₁l₂ h₂l₂)).mpr fun σ => ?_ by_cases hσ : σ.IsCycle · obtain _ := not_forall.mp (mt ext hσ.ne_one) rw [mem_list_cycles_iff h₁l₁ h₂l₁, mem_list_cycles_iff h₁l₂ h₂l₂, h₀] · exact iff_of_false (mt (h₁l₁ σ) hσ) (mt (h₁l₂ σ) hσ) /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`. -/ def cycleFactors [Fintype α] [LinearOrder α] (f : Perm α) : { l : List (Perm α) // l.prod = f ∧ (∀ g ∈ l, IsCycle g) ∧ l.Pairwise Disjoint } := cycleFactorsAux (sort (α := α) (· ≤ ·) univ) f (fun {_ _} ↦ (mem_sort _).2 (mem_univ _)) /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`, without a linear order. -/ def truncCycleFactors [DecidableEq α] [Fintype α] (f : Perm α) : Trunc { l : List (Perm α) // l.prod = f ∧ (∀ g ∈ l, IsCycle g) ∧ l.Pairwise Disjoint } := Quotient.recOnSubsingleton (@univ α _).1 (fun l h => Trunc.mk (cycleFactorsAux l f (h _))) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1 from fun _ _ => mem_univ _) section CycleFactorsFinset variable [DecidableEq α] [Fintype α] (f : Perm α) /-- Factors a permutation `f` into a `Finset` of disjoint cyclic permutations that multiply to `f`. -/ def cycleFactorsFinset : Finset (Perm α) := (truncCycleFactors f).lift (fun l : { l : List (Perm α) // l.prod = f ∧ (∀ g ∈ l, IsCycle g) ∧ l.Pairwise Disjoint } => l.val.toFinset) fun ⟨_, hl⟩ ⟨_, hl'⟩ => List.toFinset_eq_of_perm _ _ (list_cycles_perm_list_cycles (hl'.left.symm ▸ hl.left) hl.right.left hl'.right.left hl.right.right hl'.right.right) open scoped List in theorem cycleFactorsFinset_eq_list_toFinset {σ : Perm α} {l : List (Perm α)} (hn : l.Nodup) : σ.cycleFactorsFinset = l.toFinset ↔ (∀ f : Perm α, f ∈ l → f.IsCycle) ∧ l.Pairwise Disjoint ∧ l.prod = σ := by obtain ⟨⟨l', hp', hc', hd'⟩, hl⟩ := Trunc.exists_rep σ.truncCycleFactors have ht : cycleFactorsFinset σ = l'.toFinset := by rw [cycleFactorsFinset, ← hl, Trunc.lift_mk] rw [ht] constructor · intro h have hn' : l'.Nodup := nodup_of_pairwise_disjoint_cycles hc' hd' have hperm : l ~ l' := List.perm_of_nodup_nodup_toFinset_eq hn hn' h.symm refine ⟨?_, ?_, ?_⟩ · exact fun _ h => hc' _ (hperm.subset h) · have := List.Perm.pairwise_iff (@Disjoint.symmetric _) hperm rwa [this] · rw [← hp', hperm.symm.prod_eq'] refine hd'.imp ?_ exact Disjoint.commute · rintro ⟨hc, hd, hp⟩ refine List.toFinset_eq_of_perm _ _ ?_ refine list_cycles_perm_list_cycles ?_ hc' hc hd' hd rw [hp, hp'] theorem cycleFactorsFinset_eq_finset {σ : Perm α} {s : Finset (Perm α)} : σ.cycleFactorsFinset = s ↔ (∀ f : Perm α, f ∈ s → f.IsCycle) ∧ ∃ h : (s : Set (Perm α)).Pairwise Disjoint, s.noncommProd id (h.mono' fun _ _ => Disjoint.commute) = σ := by obtain ⟨l, hl, rfl⟩ := s.exists_list_nodup_eq simp [cycleFactorsFinset_eq_list_toFinset, hl] theorem cycleFactorsFinset_pairwise_disjoint : (cycleFactorsFinset f : Set (Perm α)).Pairwise Disjoint := (cycleFactorsFinset_eq_finset.mp rfl).2.choose /-- Two cycles of a permutation commute. -/ theorem cycleFactorsFinset_mem_commute : (cycleFactorsFinset f : Set (Perm α)).Pairwise Commute := (cycleFactorsFinset_pairwise_disjoint _).mono' fun _ _ => Disjoint.commute /-- Two cycles of a permutation commute. -/ theorem cycleFactorsFinset_mem_commute' {g1 g2 : Perm α} (h1 : g1 ∈ f.cycleFactorsFinset) (h2 : g2 ∈ f.cycleFactorsFinset) : Commute g1 g2 := by rcases eq_or_ne g1 g2 with rfl | h · apply Commute.refl · exact Equiv.Perm.cycleFactorsFinset_mem_commute f h1 h2 h /-- The product of cycle factors is equal to the original `f : perm α`. -/ theorem cycleFactorsFinset_noncommProd (comm : (cycleFactorsFinset f : Set (Perm α)).Pairwise Commute := cycleFactorsFinset_mem_commute f) : f.cycleFactorsFinset.noncommProd id comm = f := (cycleFactorsFinset_eq_finset.mp rfl).2.choose_spec theorem mem_cycleFactorsFinset_iff {f p : Perm α} : p ∈ cycleFactorsFinset f ↔ p.IsCycle ∧ ∀ a ∈ p.support, p a = f a := by obtain ⟨l, hl, hl'⟩ := f.cycleFactorsFinset.exists_list_nodup_eq rw [← hl'] rw [eq_comm, cycleFactorsFinset_eq_list_toFinset hl] at hl' simpa [List.mem_toFinset, Ne, ← hl'.right.right] using
mem_list_cycles_iff hl'.left hl'.right.left
Mathlib/GroupTheory/Perm/Cycle/Factors.lean
556
557
/- 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 -/ import Mathlib.Data.Countable.Basic import Mathlib.Data.Fin.VecNotation import Mathlib.Order.Disjointed import Mathlib.MeasureTheory.OuterMeasure.Defs import Mathlib.Topology.Instances.ENNReal.Lemmas /-! # Outer Measures An outer measure is a function `μ : Set α → ℝ≥0∞`, from the powerset of a type to the extended nonnegative real numbers that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is monotone; 3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most the sum of the outer measure on the individual sets. Note that we do not need `α` to be measurable to define an outer measure. ## References <https://en.wikipedia.org/wiki/Outer_measure> ## Tags outer measure -/ noncomputable section open Set Function Filter open scoped NNReal Topology ENNReal namespace MeasureTheory section OuterMeasureClass variable {α ι F : Type*} [FunLike F (Set α) ℝ≥0∞] [OuterMeasureClass F α] {μ : F} {s t : Set α} @[simp] theorem measure_empty : μ ∅ = 0 := OuterMeasureClass.measure_empty μ @[mono, gcongr] theorem measure_mono (h : s ⊆ t) : μ s ≤ μ t := OuterMeasureClass.measure_mono μ h theorem measure_mono_null (h : s ⊆ t) (ht : μ t = 0) : μ s = 0 := eq_bot_mono (measure_mono h) ht lemma measure_eq_top_mono (h : s ⊆ t) (hs : μ s = ∞) : μ t = ∞ := eq_top_mono (measure_mono h) hs lemma measure_lt_top_mono (h : s ⊆ t) (ht : μ t < ∞) : μ s < ∞ := (measure_mono h).trans_lt ht theorem measure_pos_of_superset (h : s ⊆ t) (hs : μ s ≠ 0) : 0 < μ t := hs.bot_lt.trans_le (measure_mono h) theorem measure_iUnion_le [Countable ι] (s : ι → Set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := by
refine rel_iSup_tsum μ measure_empty (· ≤ ·) (fun t ↦ ?_) _ calc μ (⋃ i, t i) = μ (⋃ i, disjointed t i) := by rw [iUnion_disjointed] _ ≤ ∑' i, μ (disjointed t i) := OuterMeasureClass.measure_iUnion_nat_le _ _ (disjoint_disjointed _) _ ≤ ∑' i, μ (t i) := by gcongr; exact disjointed_subset ..
Mathlib/MeasureTheory/OuterMeasure/Basic.lean
63
69
/- Copyright (c) 2024 Theodore Hwa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kim Morrison, Violeta Hernández Palacios, Junyan Xu, Theodore Hwa -/ import Mathlib.Logic.Hydra import Mathlib.SetTheory.Surreal.Basic /-! ### Surreal multiplication In this file, we show that multiplication of surreal numbers is well-defined, and thus the surreal numbers form a linear ordered commutative ring. An inductive argument proves the following three main theorems: * P1: being numeric is closed under multiplication, * P2: multiplying a numeric pregame by equivalent numeric pregames results in equivalent pregames, * P3: the product of two positive numeric pregames is positive (`mul_pos`). This is Theorem 8 in [Conway2001], or Theorem 3.8 in [SchleicherStoll]. P1 allows us to define multiplication as an operation on numeric pregames, P2 says that this is well-defined as an operation on the quotient by `PGame.Equiv`, namely the surreal numbers, and P3 is an axiom that needs to be satisfied for the surreals to be a `OrderedRing`. We follow the proof in [SchleicherStoll], except that we use the well-foundedness of the hydra relation `CutExpand` on `Multiset PGame` instead of the argument based on a depth function in the paper. In the argument, P3 is stated with four variables `x₁`, `x₂`, `y₁`, `y₂` satisfying `x₁ < x₂` and `y₁ < y₂`, and says that `x₁ * y₂ + x₂ * x₁ < x₁ * y₁ + x₂ * y₂`, which is equivalent to `0 < x₂ - x₁ → 0 < y₂ - y₁ → 0 < (x₂ - x₁) * (y₂ - y₁)`, i.e. `@mul_pos PGame _ (x₂ - x₁) (y₂ - y₁)`. It has to be stated in this form and not in terms of `mul_pos` because we need to show P1, P2 and (a specialized form of) P3 simultaneously, and for example `P1 x y` will be deduced from P3 with variables taking values simpler than `x` or `y` (among other induction hypotheses), but if you subtract two pregames simpler than `x` or `y`, the result may no longer be simpler. The specialized version of P3 is called P4, which takes only three arguments `x₁`, `x₂`, `y` and requires that `y₂ = y` or `-y` and that `y₁` is a left option of `y₂`. After P1, P2 and P4 are shown, a further inductive argument (this time using the `GameAdd` relation) proves P3 in full. Implementation strategy of the inductive argument: we * extract specialized versions (`IH1`, `IH2`, `IH3`, `IH4` and `IH24`) of the induction hypothesis that are easier to apply (taking `IsOption` arguments directly), and * show they are invariant under certain symmetries (permutation and negation of arguments) and that the induction hypothesis indeed implies the specialized versions. * utilize the symmetries to minimize calculation. The whole proof features a clear separation into lemmas of different roles: * verification of symmetry properties of P and IH (`P3_comm`, `ih1_neg_left`, etc.), * calculations that connect P1, P2, P3, and inequalities between the product of two surreals and its options (`mulOption_lt_iff_P1`, etc.), * specializations of the induction hypothesis (`numeric_option_mul`, `ih1`, `ih1_swap`, `ih₁₂`, `ih4`, etc.), * application of specialized induction hypothesis (`P1_of_ih`, `mul_right_le_of_equiv`, `P3_of_lt`, etc.). ## References * [Conway, *On numbers and games*][Conway2001] * [Schleicher, Stoll, *An introduction to Conway's games and numbers*][SchleicherStoll] -/ universe u open SetTheory Game PGame WellFounded namespace Surreal.Multiplication /-- The nontrivial part of P1 in [SchleicherStoll] says that the left options of `x * y` are less than the right options, and this is the general form of these statements. -/ def P1 (x₁ x₂ x₃ y₁ y₂ y₃ : PGame) := ⟦x₁ * y₁⟧ + ⟦x₂ * y₂⟧ - ⟦x₁ * y₂⟧ < ⟦x₃ * y₁⟧ + ⟦x₂ * y₃⟧ - (⟦x₃ * y₃⟧ : Game) /-- The proposition P2, without numericity assumptions. -/ def P2 (x₁ x₂ y : PGame) := x₁ ≈ x₂ → ⟦x₁ * y⟧ = (⟦x₂ * y⟧ : Game) /-- The proposition P3, without the `x₁ < x₂` and `y₁ < y₂` assumptions. -/ def P3 (x₁ x₂ y₁ y₂ : PGame) := ⟦x₁ * y₂⟧ + ⟦x₂ * y₁⟧ < ⟦x₁ * y₁⟧ + (⟦x₂ * y₂⟧ : Game) /-- The proposition P4, without numericity assumptions. In the references, the second part of the conjunction is stated as `∀ j, P3 x₁ x₂ y (y.moveRight j)`, which is equivalent to our statement by `P3_comm` and `P3_neg`. We choose to state everything in terms of left options for uniform treatment. -/ def P4 (x₁ x₂ y : PGame) := x₁ < x₂ → (∀ i, P3 x₁ x₂ (y.moveLeft i) y) ∧ ∀ j, P3 x₁ x₂ ((-y).moveLeft j) (-y) /-- The conjunction of P2 and P4. -/ def P24 (x₁ x₂ y : PGame) : Prop := P2 x₁ x₂ y ∧ P4 x₁ x₂ y variable {x x₁ x₂ x₃ x' y y₁ y₂ y₃ y' : PGame.{u}} /-! #### Symmetry properties of P1, P2, P3, and P4 -/ lemma P3_comm : P3 x₁ x₂ y₁ y₂ ↔ P3 y₁ y₂ x₁ x₂ := by rw [P3, P3, add_comm] congr! 2 <;> rw [quot_mul_comm] lemma P3.trans (h₁ : P3 x₁ x₂ y₁ y₂) (h₂ : P3 x₂ x₃ y₁ y₂) : P3 x₁ x₃ y₁ y₂ := by rw [P3] at h₁ h₂ rw [P3, ← add_lt_add_iff_left (⟦x₂ * y₁⟧ + ⟦x₂ * y₂⟧)] convert add_lt_add h₁ h₂ using 1 <;> abel lemma P3_neg : P3 x₁ x₂ y₁ y₂ ↔ P3 (-x₂) (-x₁) y₁ y₂ := by simp_rw [P3, quot_neg_mul] rw [← _root_.neg_lt_neg_iff] abel_nf lemma P2_neg_left : P2 x₁ x₂ y ↔ P2 (-x₂) (-x₁) y := by rw [P2, P2] constructor · rw [quot_neg_mul, quot_neg_mul, eq_comm, neg_inj, neg_equiv_neg_iff, PGame.equiv_comm] exact (· ·) · rw [PGame.equiv_comm, neg_equiv_neg_iff, quot_neg_mul, quot_neg_mul, neg_inj, eq_comm] exact (· ·) lemma P2_neg_right : P2 x₁ x₂ y ↔ P2 x₁ x₂ (-y) := by rw [P2, P2, quot_mul_neg, quot_mul_neg, neg_inj] lemma P4_neg_left : P4 x₁ x₂ y ↔ P4 (-x₂) (-x₁) y := by simp_rw [P4, PGame.neg_lt_neg_iff, moveLeft_neg, ← P3_neg] lemma P4_neg_right : P4 x₁ x₂ y ↔ P4 x₁ x₂ (-y) := by rw [P4, P4, neg_neg, and_comm] lemma P24_neg_left : P24 x₁ x₂ y ↔ P24 (-x₂) (-x₁) y := by rw [P24, P24, P2_neg_left, P4_neg_left] lemma P24_neg_right : P24 x₁ x₂ y ↔ P24 x₁ x₂ (-y) := by rw [P24, P24, P2_neg_right, P4_neg_right] /-! #### Explicit calculations necessary for the main proof -/ lemma mulOption_lt_iff_P1 {i j k l} : (⟦mulOption x y i k⟧ : Game) < -⟦mulOption x (-y) j l⟧ ↔ P1 (x.moveLeft i) x (x.moveLeft j) y (y.moveLeft k) (-(-y).moveLeft l) := by dsimp only [P1, mulOption, quot_sub, quot_add] simp_rw [neg_sub', neg_add, quot_mul_neg, neg_neg] lemma mulOption_lt_mul_iff_P3 {i j} : ⟦mulOption x y i j⟧ < (⟦x * y⟧ : Game) ↔ P3 (x.moveLeft i) x (y.moveLeft j) y := by dsimp only [mulOption, quot_sub, quot_add] exact sub_lt_iff_lt_add' lemma P1_of_eq (he : x₁ ≈ x₃) (h₁ : P2 x₁ x₃ y₁) (h₃ : P2 x₁ x₃ y₃) (h3 : P3 x₁ x₂ y₂ y₃) : P1 x₁ x₂ x₃ y₁ y₂ y₃ := by rw [P1, ← h₁ he, ← h₃ he, sub_lt_sub_iff] convert add_lt_add_left h3 ⟦x₁ * y₁⟧ using 1 <;> abel lemma P1_of_lt (h₁ : P3 x₃ x₂ y₂ y₃) (h₂ : P3 x₁ x₃ y₂ y₁) : P1 x₁ x₂ x₃ y₁ y₂ y₃ := by rw [P1, sub_lt_sub_iff, ← add_lt_add_iff_left ⟦x₃ * y₂⟧] convert add_lt_add h₁ h₂ using 1 <;> abel /-- The type of lists of arguments for P1, P2, and P4. -/ inductive Args : Type (u + 1) | P1 (x y : PGame.{u}) : Args | P24 (x₁ x₂ y : PGame.{u}) : Args /-- The multiset associated to a list of arguments. -/ def Args.toMultiset : Args → Multiset PGame | (Args.P1 x y) => {x, y} | (Args.P24 x₁ x₂ y) => {x₁, x₂, y} /-- A list of arguments is numeric if all the arguments are. -/ def Args.Numeric (a : Args) := ∀ x ∈ a.toMultiset, SetTheory.PGame.Numeric x lemma Args.numeric_P1 {x y} : (Args.P1 x y).Numeric ↔ x.Numeric ∧ y.Numeric := by simp [Args.Numeric, Args.toMultiset] lemma Args.numeric_P24 {x₁ x₂ y} : (Args.P24 x₁ x₂ y).Numeric ↔ x₁.Numeric ∧ x₂.Numeric ∧ y.Numeric := by simp [Args.Numeric, Args.toMultiset] open Relation /-- The relation specifying when a list of (pregame) arguments is considered simpler than another: `ArgsRel a₁ a₂` is true if `a₁`, considered as a multiset, can be obtained from `a₂` by repeatedly removing a pregame from `a₂` and adding back one or two options of the pregame. -/ def ArgsRel := InvImage (TransGen <| CutExpand IsOption) Args.toMultiset /-- `ArgsRel` is well-founded. -/ theorem argsRel_wf : WellFounded ArgsRel := InvImage.wf _ wf_isOption.cutExpand.transGen /-- The statement that we will show by induction using the well-founded relation `ArgsRel`. -/ def P124 : Args → Prop | (Args.P1 x y) => Numeric (x * y) | (Args.P24 x₁ x₂ y) => P24 x₁ x₂ y /-- The property that all arguments are numeric is leftward-closed under `ArgsRel`. -/ lemma ArgsRel.numeric_closed {a' a} : ArgsRel a' a → a.Numeric → a'.Numeric := TransGen.closed' <| @cutExpand_closed _ IsOption ⟨wf_isOption.isIrrefl.1⟩ _ Numeric.isOption /-- A specialized induction hypothesis used to prove P1. -/ def IH1 (x y : PGame) : Prop := ∀ ⦃x₁ x₂ y'⦄, IsOption x₁ x → IsOption x₂ x → (y' = y ∨ IsOption y' y) → P24 x₁ x₂ y' /-! #### Symmetry properties of `IH1` -/ lemma ih1_neg_left : IH1 x y → IH1 (-x) y := fun h x₁ x₂ y' h₁ h₂ hy ↦ by rw [isOption_neg] at h₁ h₂ exact P24_neg_left.2 (h h₂ h₁ hy) lemma ih1_neg_right : IH1 x y → IH1 x (-y) := fun h x₁ x₂ y' ↦ by rw [← neg_eq_iff_eq_neg, isOption_neg, P24_neg_right] apply h /-! #### Specialize `ih` to obtain specialized induction hypotheses for P1 -/ lemma numeric_option_mul (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (h : IsOption x' x) : (x' * y).Numeric := ih (Args.P1 x' y) (TransGen.single <| cutExpand_pair_left h) lemma numeric_mul_option (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (h : IsOption y' y) : (x * y').Numeric := ih (Args.P1 x y') (TransGen.single <| cutExpand_pair_right h) lemma numeric_option_mul_option (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (hx : IsOption x' x) (hy : IsOption y' y) : (x' * y').Numeric := ih (Args.P1 x' y') ((TransGen.single <| cutExpand_pair_right hy).tail <| cutExpand_pair_left hx)
lemma ih1 (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) : IH1 x y := by rintro x₁ x₂ y' h₁ h₂ (rfl|hy) <;> apply ih (Args.P24 _ _ _) on_goal 2 => refine TransGen.tail ?_ (cutExpand_pair_right hy)
Mathlib/SetTheory/Surreal/Multiplication.lean
220
223
/- Copyright (c) 2021 Hunter Monroe. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hunter Monroe, Kyle Miller, Alena Gusakov -/ import Mathlib.Combinatorics.SimpleGraph.DeleteEdges import Mathlib.Data.Fintype.Powerset /-! # Subgraphs of a simple graph A subgraph of a simple graph consists of subsets of the graph's vertices and edges such that the endpoints of each edge are present in the vertex subset. The edge subset is formalized as a sub-relation of the adjacency relation of the simple graph. ## Main definitions * `Subgraph G` is the type of subgraphs of a `G : SimpleGraph V`. * `Subgraph.neighborSet`, `Subgraph.incidenceSet`, and `Subgraph.degree` are like their `SimpleGraph` counterparts, but they refer to vertices from `G` to avoid subtype coercions. * `Subgraph.coe` is the coercion from a `G' : Subgraph G` to a `SimpleGraph G'.verts`. (In Lean 3 this could not be a `Coe` instance since the destination type depends on `G'`.) * `Subgraph.IsSpanning` for whether a subgraph is a spanning subgraph and `Subgraph.IsInduced` for whether a subgraph is an induced subgraph. * Instances for `Lattice (Subgraph G)` and `BoundedOrder (Subgraph G)`. * `SimpleGraph.toSubgraph`: If a `SimpleGraph` is a subgraph of another, then you can turn it into a member of the larger graph's `SimpleGraph.Subgraph` type. * Graph homomorphisms from a subgraph to a graph (`Subgraph.map_top`) and between subgraphs (`Subgraph.map`). ## Implementation notes * Recall that subgraphs are not determined by their vertex sets, so `SetLike` does not apply to this kind of subobject. ## TODO * Images of graph homomorphisms as subgraphs. -/ universe u v namespace SimpleGraph /-- A subgraph of a `SimpleGraph` is a subset of vertices along with a restriction of the adjacency relation that is symmetric and is supported by the vertex subset. They also form a bounded lattice. Thinking of `V → V → Prop` as `Set (V × V)`, a set of darts (i.e., half-edges), then `Subgraph.adj_sub` is that the darts of a subgraph are a subset of the darts of `G`. -/ @[ext] structure Subgraph {V : Type u} (G : SimpleGraph V) where /-- Vertices of the subgraph -/ verts : Set V /-- Edges of the subgraph -/ Adj : V → V → Prop adj_sub : ∀ {v w : V}, Adj v w → G.Adj v w edge_vert : ∀ {v w : V}, Adj v w → v ∈ verts symm : Symmetric Adj := by aesop_graph -- Porting note: Originally `by obviously` initialize_simps_projections SimpleGraph.Subgraph (Adj → adj) variable {ι : Sort*} {V : Type u} {W : Type v} /-- The one-vertex subgraph. -/ @[simps] protected def singletonSubgraph (G : SimpleGraph V) (v : V) : G.Subgraph where verts := {v} Adj := ⊥ adj_sub := False.elim edge_vert := False.elim symm _ _ := False.elim /-- The one-edge subgraph. -/ @[simps] def subgraphOfAdj (G : SimpleGraph V) {v w : V} (hvw : G.Adj v w) : G.Subgraph where verts := {v, w} Adj a b := s(v, w) = s(a, b) adj_sub h := by rw [← G.mem_edgeSet, ← h] exact hvw edge_vert {a b} h := by apply_fun fun e ↦ a ∈ e at h simp only [Sym2.mem_iff, true_or, eq_iff_iff, iff_true] at h exact h namespace Subgraph variable {G : SimpleGraph V} {G₁ G₂ : G.Subgraph} {a b : V} protected theorem loopless (G' : Subgraph G) : Irreflexive G'.Adj := fun v h ↦ G.loopless v (G'.adj_sub h) theorem adj_comm (G' : Subgraph G) (v w : V) : G'.Adj v w ↔ G'.Adj w v := ⟨fun x ↦ G'.symm x, fun x ↦ G'.symm x⟩ @[symm] theorem adj_symm (G' : Subgraph G) {u v : V} (h : G'.Adj u v) : G'.Adj v u := G'.symm h protected theorem Adj.symm {G' : Subgraph G} {u v : V} (h : G'.Adj u v) : G'.Adj v u := G'.symm h protected theorem Adj.adj_sub {H : G.Subgraph} {u v : V} (h : H.Adj u v) : G.Adj u v := H.adj_sub h protected theorem Adj.fst_mem {H : G.Subgraph} {u v : V} (h : H.Adj u v) : u ∈ H.verts := H.edge_vert h protected theorem Adj.snd_mem {H : G.Subgraph} {u v : V} (h : H.Adj u v) : v ∈ H.verts := h.symm.fst_mem protected theorem Adj.ne {H : G.Subgraph} {u v : V} (h : H.Adj u v) : u ≠ v := h.adj_sub.ne theorem adj_congr_of_sym2 {H : G.Subgraph} {u v w x : V} (h2 : s(u, v) = s(w, x)) : H.Adj u v ↔ H.Adj w x := by simp only [Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk] at h2 rcases h2 with hl | hr · rw [hl.1, hl.2] · rw [hr.1, hr.2, Subgraph.adj_comm] /-- Coercion from `G' : Subgraph G` to a `SimpleGraph G'.verts`. -/ @[simps] protected def coe (G' : Subgraph G) : SimpleGraph G'.verts where Adj v w := G'.Adj v w symm _ _ h := G'.symm h loopless v h := loopless G v (G'.adj_sub h) @[simp] theorem coe_adj_sub (G' : Subgraph G) (u v : G'.verts) (h : G'.coe.Adj u v) : G.Adj u v := G'.adj_sub h -- Given `h : H.Adj u v`, then `h.coe : H.coe.Adj ⟨u, _⟩ ⟨v, _⟩`. protected theorem Adj.coe {H : G.Subgraph} {u v : V} (h : H.Adj u v) : H.coe.Adj ⟨u, H.edge_vert h⟩ ⟨v, H.edge_vert h.symm⟩ := h instance (G : SimpleGraph V) (H : Subgraph G) [DecidableRel H.Adj] : DecidableRel H.coe.Adj := fun a b ↦ ‹DecidableRel H.Adj› _ _ /-- A subgraph is called a *spanning subgraph* if it contains all the vertices of `G`. -/ def IsSpanning (G' : Subgraph G) : Prop := ∀ v : V, v ∈ G'.verts theorem isSpanning_iff {G' : Subgraph G} : G'.IsSpanning ↔ G'.verts = Set.univ := Set.eq_univ_iff_forall.symm protected alias ⟨IsSpanning.verts_eq_univ, _⟩ := isSpanning_iff /-- Coercion from `Subgraph G` to `SimpleGraph V`. If `G'` is a spanning subgraph, then `G'.spanningCoe` yields an isomorphic graph. In general, this adds in all vertices from `V` as isolated vertices. -/ @[simps] protected def spanningCoe (G' : Subgraph G) : SimpleGraph V where Adj := G'.Adj symm := G'.symm loopless v hv := G.loopless v (G'.adj_sub hv) @[simp] theorem Adj.of_spanningCoe {G' : Subgraph G} {u v : G'.verts} (h : G'.spanningCoe.Adj u v) : G.Adj u v := G'.adj_sub h lemma spanningCoe_le (G' : G.Subgraph) : G'.spanningCoe ≤ G := fun _ _ ↦ G'.3 theorem spanningCoe_inj : G₁.spanningCoe = G₂.spanningCoe ↔ G₁.Adj = G₂.Adj := by simp [Subgraph.spanningCoe] lemma mem_of_adj_spanningCoe {v w : V} {s : Set V} (G : SimpleGraph s) (hadj : G.spanningCoe.Adj v w) : v ∈ s := by aesop @[simp] lemma spanningCoe_subgraphOfAdj {v w : V} (hadj : G.Adj v w) : (G.subgraphOfAdj hadj).spanningCoe = fromEdgeSet {s(v, w)} := by ext v w aesop /-- `spanningCoe` is equivalent to `coe` for a subgraph that `IsSpanning`. -/ @[simps] def spanningCoeEquivCoeOfSpanning (G' : Subgraph G) (h : G'.IsSpanning) : G'.spanningCoe ≃g G'.coe where toFun v := ⟨v, h v⟩ invFun v := v left_inv _ := rfl right_inv _ := rfl map_rel_iff' := Iff.rfl /-- A subgraph is called an *induced subgraph* if vertices of `G'` are adjacent if they are adjacent in `G`. -/ def IsInduced (G' : Subgraph G) : Prop := ∀ ⦃v⦄, v ∈ G'.verts → ∀ ⦃w⦄, w ∈ G'.verts → G.Adj v w → G'.Adj v w @[simp] protected lemma IsInduced.adj {G' : G.Subgraph} (hG' : G'.IsInduced) {a b : G'.verts} : G'.Adj a b ↔ G.Adj a b := ⟨coe_adj_sub _ _ _, hG' a.2 b.2⟩ /-- `H.support` is the set of vertices that form edges in the subgraph `H`. -/ def support (H : Subgraph G) : Set V := Rel.dom H.Adj theorem mem_support (H : Subgraph G) {v : V} : v ∈ H.support ↔ ∃ w, H.Adj v w := Iff.rfl theorem support_subset_verts (H : Subgraph G) : H.support ⊆ H.verts := fun _ ⟨_, h⟩ ↦ H.edge_vert h /-- `G'.neighborSet v` is the set of vertices adjacent to `v` in `G'`. -/ def neighborSet (G' : Subgraph G) (v : V) : Set V := {w | G'.Adj v w} theorem neighborSet_subset (G' : Subgraph G) (v : V) : G'.neighborSet v ⊆ G.neighborSet v := fun _ ↦ G'.adj_sub theorem neighborSet_subset_verts (G' : Subgraph G) (v : V) : G'.neighborSet v ⊆ G'.verts := fun _ h ↦ G'.edge_vert (adj_symm G' h) @[simp] theorem mem_neighborSet (G' : Subgraph G) (v w : V) : w ∈ G'.neighborSet v ↔ G'.Adj v w := Iff.rfl /-- A subgraph as a graph has equivalent neighbor sets. -/ def coeNeighborSetEquiv {G' : Subgraph G} (v : G'.verts) : G'.coe.neighborSet v ≃ G'.neighborSet v where toFun w := ⟨w, w.2⟩ invFun w := ⟨⟨w, G'.edge_vert (G'.adj_symm w.2)⟩, w.2⟩ left_inv _ := rfl right_inv _ := rfl /-- The edge set of `G'` consists of a subset of edges of `G`. -/ def edgeSet (G' : Subgraph G) : Set (Sym2 V) := Sym2.fromRel G'.symm theorem edgeSet_subset (G' : Subgraph G) : G'.edgeSet ⊆ G.edgeSet := Sym2.ind (fun _ _ ↦ G'.adj_sub) @[simp] protected lemma mem_edgeSet {G' : Subgraph G} {v w : V} : s(v, w) ∈ G'.edgeSet ↔ G'.Adj v w := .rfl @[simp] lemma edgeSet_coe {G' : G.Subgraph} : G'.coe.edgeSet = Sym2.map (↑) ⁻¹' G'.edgeSet := by ext e; induction e using Sym2.ind; simp lemma image_coe_edgeSet_coe (G' : G.Subgraph) : Sym2.map (↑) '' G'.coe.edgeSet = G'.edgeSet := by rw [edgeSet_coe, Set.image_preimage_eq_iff] rintro e he induction e using Sym2.ind with | h a b => rw [Subgraph.mem_edgeSet] at he exact ⟨s(⟨a, edge_vert _ he⟩, ⟨b, edge_vert _ he.symm⟩), Sym2.map_pair_eq ..⟩ theorem mem_verts_of_mem_edge {G' : Subgraph G} {e : Sym2 V} {v : V} (he : e ∈ G'.edgeSet) (hv : v ∈ e) : v ∈ G'.verts := by induction e rcases Sym2.mem_iff.mp hv with (rfl | rfl) · exact G'.edge_vert he · exact G'.edge_vert (G'.symm he) /-- The `incidenceSet` is the set of edges incident to a given vertex. -/ def incidenceSet (G' : Subgraph G) (v : V) : Set (Sym2 V) := {e ∈ G'.edgeSet | v ∈ e} theorem incidenceSet_subset_incidenceSet (G' : Subgraph G) (v : V) : G'.incidenceSet v ⊆ G.incidenceSet v := fun _ h ↦ ⟨G'.edgeSet_subset h.1, h.2⟩ theorem incidenceSet_subset (G' : Subgraph G) (v : V) : G'.incidenceSet v ⊆ G'.edgeSet := fun _ h ↦ h.1 /-- Give a vertex as an element of the subgraph's vertex type. -/ abbrev vert (G' : Subgraph G) (v : V) (h : v ∈ G'.verts) : G'.verts := ⟨v, h⟩ /-- Create an equal copy of a subgraph (see `copy_eq`) with possibly different definitional equalities. See Note [range copy pattern]. -/ def copy (G' : Subgraph G) (V'' : Set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.Adj) : Subgraph G where verts := V'' Adj := adj' adj_sub := hadj.symm ▸ G'.adj_sub edge_vert := hV.symm ▸ hadj.symm ▸ G'.edge_vert symm := hadj.symm ▸ G'.symm theorem copy_eq (G' : Subgraph G) (V'' : Set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.Adj) : G'.copy V'' hV adj' hadj = G' := Subgraph.ext hV hadj /-- The union of two subgraphs. -/ instance : Max G.Subgraph where max G₁ G₂ := { verts := G₁.verts ∪ G₂.verts Adj := G₁.Adj ⊔ G₂.Adj adj_sub := fun hab => Or.elim hab (fun h => G₁.adj_sub h) fun h => G₂.adj_sub h edge_vert := Or.imp (fun h => G₁.edge_vert h) fun h => G₂.edge_vert h symm := fun _ _ => Or.imp G₁.adj_symm G₂.adj_symm } /-- The intersection of two subgraphs. -/ instance : Min G.Subgraph where min G₁ G₂ := { verts := G₁.verts ∩ G₂.verts Adj := G₁.Adj ⊓ G₂.Adj adj_sub := fun hab => G₁.adj_sub hab.1 edge_vert := And.imp (fun h => G₁.edge_vert h) fun h => G₂.edge_vert h symm := fun _ _ => And.imp G₁.adj_symm G₂.adj_symm } /-- The `top` subgraph is `G` as a subgraph of itself. -/ instance : Top G.Subgraph where top := { verts := Set.univ Adj := G.Adj adj_sub := id edge_vert := @fun v _ _ => Set.mem_univ v symm := G.symm } /-- The `bot` subgraph is the subgraph with no vertices or edges. -/ instance : Bot G.Subgraph where bot := { verts := ∅ Adj := ⊥ adj_sub := False.elim edge_vert := False.elim symm := fun _ _ => id } instance : SupSet G.Subgraph where sSup s := { verts := ⋃ G' ∈ s, verts G' Adj := fun a b => ∃ G' ∈ s, Adj G' a b adj_sub := by rintro a b ⟨G', -, hab⟩ exact G'.adj_sub hab edge_vert := by rintro a b ⟨G', hG', hab⟩ exact Set.mem_iUnion₂_of_mem hG' (G'.edge_vert hab) symm := fun a b h => by simpa [adj_comm] using h } instance : InfSet G.Subgraph where sInf s := { verts := ⋂ G' ∈ s, verts G' Adj := fun a b => (∀ ⦃G'⦄, G' ∈ s → Adj G' a b) ∧ G.Adj a b adj_sub := And.right edge_vert := fun hab => Set.mem_iInter₂_of_mem fun G' hG' => G'.edge_vert <| hab.1 hG' symm := fun _ _ => And.imp (forall₂_imp fun _ _ => Adj.symm) G.adj_symm } @[simp] theorem sup_adj : (G₁ ⊔ G₂).Adj a b ↔ G₁.Adj a b ∨ G₂.Adj a b := Iff.rfl @[simp] theorem inf_adj : (G₁ ⊓ G₂).Adj a b ↔ G₁.Adj a b ∧ G₂.Adj a b := Iff.rfl @[simp] theorem top_adj : (⊤ : Subgraph G).Adj a b ↔ G.Adj a b := Iff.rfl @[simp] theorem not_bot_adj : ¬ (⊥ : Subgraph G).Adj a b := not_false @[simp] theorem verts_sup (G₁ G₂ : G.Subgraph) : (G₁ ⊔ G₂).verts = G₁.verts ∪ G₂.verts := rfl @[simp] theorem verts_inf (G₁ G₂ : G.Subgraph) : (G₁ ⊓ G₂).verts = G₁.verts ∩ G₂.verts := rfl @[simp] theorem verts_top : (⊤ : G.Subgraph).verts = Set.univ := rfl @[simp] theorem verts_bot : (⊥ : G.Subgraph).verts = ∅ := rfl @[simp] theorem sSup_adj {s : Set G.Subgraph} : (sSup s).Adj a b ↔ ∃ G ∈ s, Adj G a b := Iff.rfl @[simp] theorem sInf_adj {s : Set G.Subgraph} : (sInf s).Adj a b ↔ (∀ G' ∈ s, Adj G' a b) ∧ G.Adj a b := Iff.rfl @[simp] theorem iSup_adj {f : ι → G.Subgraph} : (⨆ i, f i).Adj a b ↔ ∃ i, (f i).Adj a b := by simp [iSup] @[simp] theorem iInf_adj {f : ι → G.Subgraph} : (⨅ i, f i).Adj a b ↔ (∀ i, (f i).Adj a b) ∧ G.Adj a b := by simp [iInf] theorem sInf_adj_of_nonempty {s : Set G.Subgraph} (hs : s.Nonempty) : (sInf s).Adj a b ↔ ∀ G' ∈ s, Adj G' a b := sInf_adj.trans <| and_iff_left_of_imp <| by obtain ⟨G', hG'⟩ := hs exact fun h => G'.adj_sub (h _ hG') theorem iInf_adj_of_nonempty [Nonempty ι] {f : ι → G.Subgraph} : (⨅ i, f i).Adj a b ↔ ∀ i, (f i).Adj a b := by rw [iInf, sInf_adj_of_nonempty (Set.range_nonempty _)] simp @[simp] theorem verts_sSup (s : Set G.Subgraph) : (sSup s).verts = ⋃ G' ∈ s, verts G' := rfl @[simp] theorem verts_sInf (s : Set G.Subgraph) : (sInf s).verts = ⋂ G' ∈ s, verts G' := rfl @[simp] theorem verts_iSup {f : ι → G.Subgraph} : (⨆ i, f i).verts = ⋃ i, (f i).verts := by simp [iSup] @[simp] theorem verts_iInf {f : ι → G.Subgraph} : (⨅ i, f i).verts = ⋂ i, (f i).verts := by simp [iInf] @[simp] lemma coe_bot : (⊥ : G.Subgraph).coe = ⊥ := rfl @[simp] lemma IsInduced.top : (⊤ : G.Subgraph).IsInduced := fun _ _ _ _ ↦ id /-- The graph isomorphism between the top element of `G.subgraph` and `G`. -/ def topIso : (⊤ : G.Subgraph).coe ≃g G where toFun := (↑) invFun a := ⟨a, Set.mem_univ _⟩ left_inv _ := Subtype.eta .. right_inv _ := rfl map_rel_iff' := .rfl theorem verts_spanningCoe_injective : (fun G' : Subgraph G => (G'.verts, G'.spanningCoe)).Injective := by intro G₁ G₂ h rw [Prod.ext_iff] at h exact Subgraph.ext h.1 (spanningCoe_inj.1 h.2) /-- For subgraphs `G₁`, `G₂`, `G₁ ≤ G₂` iff `G₁.verts ⊆ G₂.verts` and `∀ a b, G₁.adj a b → G₂.adj a b`. -/ instance distribLattice : DistribLattice G.Subgraph := { show DistribLattice G.Subgraph from verts_spanningCoe_injective.distribLattice _ (fun _ _ => rfl) fun _ _ => rfl with le := fun x y => x.verts ⊆ y.verts ∧ ∀ ⦃v w : V⦄, x.Adj v w → y.Adj v w } instance : BoundedOrder (Subgraph G) where top := ⊤ bot := ⊥ le_top x := ⟨Set.subset_univ _, fun _ _ => x.adj_sub⟩ bot_le _ := ⟨Set.empty_subset _, fun _ _ => False.elim⟩ /-- Note that subgraphs do not form a Boolean algebra, because of `verts`. -/ def completelyDistribLatticeMinimalAxioms : CompletelyDistribLattice.MinimalAxioms G.Subgraph := { Subgraph.distribLattice with le := (· ≤ ·) sup := (· ⊔ ·) inf := (· ⊓ ·) top := ⊤ bot := ⊥ le_top := fun G' => ⟨Set.subset_univ _, fun _ _ => G'.adj_sub⟩ bot_le := fun _ => ⟨Set.empty_subset _, fun _ _ => False.elim⟩ sSup := sSup -- Porting note: needed `apply` here to modify elaboration; previously the term itself was fine. le_sSup := fun s G' hG' => ⟨by apply Set.subset_iUnion₂ G' hG', fun _ _ hab => ⟨G', hG', hab⟩⟩ sSup_le := fun s G' hG' => ⟨Set.iUnion₂_subset fun _ hH => (hG' _ hH).1, by rintro a b ⟨H, hH, hab⟩ exact (hG' _ hH).2 hab⟩ sInf := sInf sInf_le := fun _ G' hG' => ⟨Set.iInter₂_subset G' hG', fun _ _ hab => hab.1 hG'⟩ le_sInf := fun _ G' hG' => ⟨Set.subset_iInter₂ fun _ hH => (hG' _ hH).1, fun _ _ hab => ⟨fun _ hH => (hG' _ hH).2 hab, G'.adj_sub hab⟩⟩ iInf_iSup_eq := fun f => Subgraph.ext (by simpa using iInf_iSup_eq) (by ext; simp [Classical.skolem]) } instance : CompletelyDistribLattice G.Subgraph := .ofMinimalAxioms completelyDistribLatticeMinimalAxioms @[gcongr] lemma verts_mono {H H' : G.Subgraph} (h : H ≤ H') : H.verts ⊆ H'.verts := h.1 lemma verts_monotone : Monotone (verts : G.Subgraph → Set V) := fun _ _ h ↦ h.1 @[simps] instance subgraphInhabited : Inhabited (Subgraph G) := ⟨⊥⟩ @[simp] theorem neighborSet_sup {H H' : G.Subgraph} (v : V) : (H ⊔ H').neighborSet v = H.neighborSet v ∪ H'.neighborSet v := rfl @[simp] theorem neighborSet_inf {H H' : G.Subgraph} (v : V) : (H ⊓ H').neighborSet v = H.neighborSet v ∩ H'.neighborSet v := rfl @[simp] theorem neighborSet_top (v : V) : (⊤ : G.Subgraph).neighborSet v = G.neighborSet v := rfl @[simp] theorem neighborSet_bot (v : V) : (⊥ : G.Subgraph).neighborSet v = ∅ := rfl @[simp] theorem neighborSet_sSup (s : Set G.Subgraph) (v : V) : (sSup s).neighborSet v = ⋃ G' ∈ s, neighborSet G' v := by ext simp @[simp] theorem neighborSet_sInf (s : Set G.Subgraph) (v : V) : (sInf s).neighborSet v = (⋂ G' ∈ s, neighborSet G' v) ∩ G.neighborSet v := by ext simp @[simp] theorem neighborSet_iSup (f : ι → G.Subgraph) (v : V) : (⨆ i, f i).neighborSet v = ⋃ i, (f i).neighborSet v := by simp [iSup] @[simp] theorem neighborSet_iInf (f : ι → G.Subgraph) (v : V) : (⨅ i, f i).neighborSet v = (⋂ i, (f i).neighborSet v) ∩ G.neighborSet v := by simp [iInf] @[simp] theorem edgeSet_top : (⊤ : Subgraph G).edgeSet = G.edgeSet := rfl @[simp] theorem edgeSet_bot : (⊥ : Subgraph G).edgeSet = ∅ := Set.ext <| Sym2.ind (by simp) @[simp] theorem edgeSet_inf {H₁ H₂ : Subgraph G} : (H₁ ⊓ H₂).edgeSet = H₁.edgeSet ∩ H₂.edgeSet := Set.ext <| Sym2.ind (by simp) @[simp] theorem edgeSet_sup {H₁ H₂ : Subgraph G} : (H₁ ⊔ H₂).edgeSet = H₁.edgeSet ∪ H₂.edgeSet := Set.ext <| Sym2.ind (by simp) @[simp] theorem edgeSet_sSup (s : Set G.Subgraph) : (sSup s).edgeSet = ⋃ G' ∈ s, edgeSet G' := by ext e induction e simp @[simp] theorem edgeSet_sInf (s : Set G.Subgraph) : (sInf s).edgeSet = (⋂ G' ∈ s, edgeSet G') ∩ G.edgeSet := by ext e induction e simp @[simp] theorem edgeSet_iSup (f : ι → G.Subgraph) : (⨆ i, f i).edgeSet = ⋃ i, (f i).edgeSet := by simp [iSup] @[simp] theorem edgeSet_iInf (f : ι → G.Subgraph) : (⨅ i, f i).edgeSet = (⋂ i, (f i).edgeSet) ∩ G.edgeSet := by simp [iInf] @[simp] theorem spanningCoe_top : (⊤ : Subgraph G).spanningCoe = G := rfl @[simp] theorem spanningCoe_bot : (⊥ : Subgraph G).spanningCoe = ⊥ := rfl /-- Turn a subgraph of a `SimpleGraph` into a member of its subgraph type. -/ @[simps] def _root_.SimpleGraph.toSubgraph (H : SimpleGraph V) (h : H ≤ G) : G.Subgraph where verts := Set.univ Adj := H.Adj adj_sub e := h e edge_vert _ := Set.mem_univ _ symm := H.symm theorem support_mono {H H' : Subgraph G} (h : H ≤ H') : H.support ⊆ H'.support := Rel.dom_mono h.2 theorem _root_.SimpleGraph.toSubgraph.isSpanning (H : SimpleGraph V) (h : H ≤ G) : (toSubgraph H h).IsSpanning := Set.mem_univ theorem spanningCoe_le_of_le {H H' : Subgraph G} (h : H ≤ H') : H.spanningCoe ≤ H'.spanningCoe := h.2 @[simp] lemma sup_spanningCoe (H H' : Subgraph G) : (H ⊔ H').spanningCoe = H.spanningCoe ⊔ H'.spanningCoe := rfl /-- The top of the `Subgraph G` lattice is equivalent to the graph itself. -/ def topEquiv : (⊤ : Subgraph G).coe ≃g G where toFun v := ↑v invFun v := ⟨v, trivial⟩ left_inv _ := rfl right_inv _ := rfl map_rel_iff' := Iff.rfl /-- The bottom of the `Subgraph G` lattice is equivalent to the empty graph on the empty vertex type. -/ def botEquiv : (⊥ : Subgraph G).coe ≃g (⊥ : SimpleGraph Empty) where toFun v := v.property.elim invFun v := v.elim left_inv := fun ⟨_, h⟩ ↦ h.elim right_inv v := v.elim map_rel_iff' := Iff.rfl theorem edgeSet_mono {H₁ H₂ : Subgraph G} (h : H₁ ≤ H₂) : H₁.edgeSet ≤ H₂.edgeSet := Sym2.ind h.2 theorem _root_.Disjoint.edgeSet {H₁ H₂ : Subgraph G} (h : Disjoint H₁ H₂) : Disjoint H₁.edgeSet H₂.edgeSet := disjoint_iff_inf_le.mpr <| by simpa using edgeSet_mono h.le_bot section map variable {G' : SimpleGraph W} {f : G →g G'} /-- Graph homomorphisms induce a covariant function on subgraphs. -/ @[simps] protected def map (f : G →g G') (H : G.Subgraph) : G'.Subgraph where verts := f '' H.verts Adj := Relation.Map H.Adj f f adj_sub := by rintro _ _ ⟨u, v, h, rfl, rfl⟩ exact f.map_rel (H.adj_sub h) edge_vert := by rintro _ _ ⟨u, v, h, rfl, rfl⟩ exact Set.mem_image_of_mem _ (H.edge_vert h) symm := by rintro _ _ ⟨u, v, h, rfl, rfl⟩ exact ⟨v, u, H.symm h, rfl, rfl⟩ @[simp] lemma map_id (H : G.Subgraph) : H.map Hom.id = H := by ext <;> simp lemma map_comp {U : Type*} {G'' : SimpleGraph U} (H : G.Subgraph) (f : G →g G') (g : G' →g G'') : H.map (g.comp f) = (H.map f).map g := by ext <;> simp [Subgraph.map] @[gcongr] lemma map_mono {H₁ H₂ : G.Subgraph} (hH : H₁ ≤ H₂) : H₁.map f ≤ H₂.map f := by constructor · intro simp only [map_verts, Set.mem_image, forall_exists_index, and_imp] rintro v hv rfl exact ⟨_, hH.1 hv, rfl⟩ · rintro _ _ ⟨u, v, ha, rfl, rfl⟩ exact ⟨_, _, hH.2 ha, rfl, rfl⟩ lemma map_monotone : Monotone (Subgraph.map f) := fun _ _ ↦ map_mono theorem map_sup (f : G →g G') (H₁ H₂ : G.Subgraph) : (H₁ ⊔ H₂).map f = H₁.map f ⊔ H₂.map f := by ext <;> simp [Set.image_union, map_adj, sup_adj, Relation.Map, or_and_right, exists_or] @[simp] lemma map_iso_top {H : SimpleGraph W} (e : G ≃g H) : Subgraph.map e.toHom ⊤ = ⊤ := by ext <;> simp [Relation.Map, e.apply_eq_iff_eq_symm_apply, ← e.map_rel_iff] @[simp] lemma edgeSet_map (f : G →g G') (H : G.Subgraph) : (H.map f).edgeSet = Sym2.map f '' H.edgeSet := Sym2.fromRel_relationMap .. end map /-- Graph homomorphisms induce a contravariant function on subgraphs. -/ @[simps] protected def comap {G' : SimpleGraph W} (f : G →g G') (H : G'.Subgraph) : G.Subgraph where verts := f ⁻¹' H.verts Adj u v := G.Adj u v ∧ H.Adj (f u) (f v) adj_sub h := h.1 edge_vert h := Set.mem_preimage.1 (H.edge_vert h.2) symm _ _ h := ⟨G.symm h.1, H.symm h.2⟩ theorem comap_monotone {G' : SimpleGraph W} (f : G →g G') : Monotone (Subgraph.comap f) := by intro H H' h constructor · intro simp only [comap_verts, Set.mem_preimage] apply h.1 · intro v w simp +contextual only [comap_adj, and_imp, true_and] intro apply h.2 @[simp] lemma comap_equiv_top {H : SimpleGraph W} (f : G →g H) : Subgraph.comap f ⊤ = ⊤ := by ext <;> simp +contextual [f.map_adj] theorem map_le_iff_le_comap {G' : SimpleGraph W} (f : G →g G') (H : G.Subgraph) (H' : G'.Subgraph) : H.map f ≤ H' ↔ H ≤ H'.comap f := by refine ⟨fun h ↦ ⟨fun v hv ↦ ?_, fun v w hvw ↦ ?_⟩, fun h ↦ ⟨fun v ↦ ?_, fun v w ↦ ?_⟩⟩ · simp only [comap_verts, Set.mem_preimage] exact h.1 ⟨v, hv, rfl⟩ · simp only [H.adj_sub hvw, comap_adj, true_and] exact h.2 ⟨v, w, hvw, rfl, rfl⟩ · simp only [map_verts, Set.mem_image, forall_exists_index, and_imp] rintro w hw rfl exact h.1 hw · simp only [Relation.Map, map_adj, forall_exists_index, and_imp] rintro u u' hu rfl rfl exact (h.2 hu).2 instance [DecidableEq V] [Fintype V] [DecidableRel G.Adj] : Fintype G.Subgraph := by refine .ofBijective (α := {H : Finset V × (V → V → Bool) // (∀ a b, H.2 a b → G.Adj a b) ∧ (∀ a b, H.2 a b → a ∈ H.1) ∧ ∀ a b, H.2 a b = H.2 b a}) (fun H ↦ ⟨H.1.1, fun a b ↦ H.1.2 a b, @H.2.1, @H.2.2.1, by simp [Symmetric, H.2.2.2]⟩) ⟨?_, fun H ↦ ?_⟩ · rintro ⟨⟨_, _⟩, -⟩ ⟨⟨_, _⟩, -⟩ simp [funext_iff] · classical exact ⟨⟨(H.verts.toFinset, fun a b ↦ H.Adj a b), fun a b ↦ by simpa using H.adj_sub, fun a b ↦ by simpa using H.edge_vert, by simp [H.adj_comm]⟩, by simp⟩ instance [Finite V] : Finite G.Subgraph := by classical cases nonempty_fintype V; infer_instance /-- Given two subgraphs, one a subgraph of the other, there is an induced injective homomorphism of the subgraphs as graphs. -/ @[simps] def inclusion {x y : Subgraph G} (h : x ≤ y) : x.coe →g y.coe where toFun v := ⟨↑v, And.left h v.property⟩ map_rel' hvw := h.2 hvw theorem inclusion.injective {x y : Subgraph G} (h : x ≤ y) : Function.Injective (inclusion h) := by intro v w h rw [inclusion, DFunLike.coe, Subtype.mk_eq_mk] at h exact Subtype.ext h /-- There is an induced injective homomorphism of a subgraph of `G` into `G`. -/ @[simps] protected def hom (x : Subgraph G) : x.coe →g G where toFun v := v map_rel' := x.adj_sub @[simp] lemma coe_hom (x : Subgraph G) : (x.hom : x.verts → V) = (fun (v : x.verts) => (v : V)) := rfl theorem hom_injective {x : Subgraph G} : Function.Injective x.hom := fun _ _ ↦ Subtype.ext @[deprecated (since := "2025-03-15")] alias hom.injective := hom_injective @[simp] lemma map_hom_top (G' : G.Subgraph) : Subgraph.map G'.hom ⊤ = G' := by aesop (add unfold safe Relation.Map, unsafe G'.edge_vert, unsafe Adj.symm) /-- There is an induced injective homomorphism of a subgraph of `G` as a spanning subgraph into `G`. -/ @[simps] def spanningHom (x : Subgraph G) : x.spanningCoe →g G where toFun := id map_rel' := x.adj_sub theorem spanningHom_injective {x : Subgraph G} : Function.Injective x.spanningHom := fun _ _ ↦ id @[deprecated (since := "2025-03-15")] alias spanningHom.injective := spanningHom_injective theorem neighborSet_subset_of_subgraph {x y : Subgraph G} (h : x ≤ y) (v : V) : x.neighborSet v ⊆ y.neighborSet v := fun _ h' ↦ h.2 h' instance neighborSet.decidablePred (G' : Subgraph G) [h : DecidableRel G'.Adj] (v : V) : DecidablePred (· ∈ G'.neighborSet v) := h v /-- If a graph is locally finite at a vertex, then so is a subgraph of that graph. -/ instance finiteAt {G' : Subgraph G} (v : G'.verts) [DecidableRel G'.Adj] [Fintype (G.neighborSet v)] : Fintype (G'.neighborSet v) := Set.fintypeSubset (G.neighborSet v) (G'.neighborSet_subset v) /-- If a subgraph is locally finite at a vertex, then so are subgraphs of that subgraph. This is not an instance because `G''` cannot be inferred. -/ def finiteAtOfSubgraph {G' G'' : Subgraph G} [DecidableRel G'.Adj] (h : G' ≤ G'') (v : G'.verts) [Fintype (G''.neighborSet v)] : Fintype (G'.neighborSet v) := Set.fintypeSubset (G''.neighborSet v) (neighborSet_subset_of_subgraph h v) instance (G' : Subgraph G) [Fintype G'.verts] (v : V) [DecidablePred (· ∈ G'.neighborSet v)] : Fintype (G'.neighborSet v) := Set.fintypeSubset G'.verts (neighborSet_subset_verts G' v) instance coeFiniteAt {G' : Subgraph G} (v : G'.verts) [Fintype (G'.neighborSet v)] : Fintype (G'.coe.neighborSet v) := Fintype.ofEquiv _ (coeNeighborSetEquiv v).symm theorem IsSpanning.card_verts [Fintype V] {G' : Subgraph G} [Fintype G'.verts] (h : G'.IsSpanning) : G'.verts.toFinset.card = Fintype.card V := by simp only [isSpanning_iff.1 h, Set.toFinset_univ] congr /-- The degree of a vertex in a subgraph. It's zero for vertices outside the subgraph. -/ def degree (G' : Subgraph G) (v : V) [Fintype (G'.neighborSet v)] : ℕ := Fintype.card (G'.neighborSet v) theorem finset_card_neighborSet_eq_degree {G' : Subgraph G} {v : V} [Fintype (G'.neighborSet v)] : (G'.neighborSet v).toFinset.card = G'.degree v := by rw [degree, Set.toFinset_card] theorem degree_le (G' : Subgraph G) (v : V) [Fintype (G'.neighborSet v)] [Fintype (G.neighborSet v)] : G'.degree v ≤ G.degree v := by rw [← card_neighborSet_eq_degree] exact Set.card_le_card (G'.neighborSet_subset v) theorem degree_le' (G' G'' : Subgraph G) (h : G' ≤ G'') (v : V) [Fintype (G'.neighborSet v)] [Fintype (G''.neighborSet v)] : G'.degree v ≤ G''.degree v := Set.card_le_card (neighborSet_subset_of_subgraph h v) @[simp] theorem coe_degree (G' : Subgraph G) (v : G'.verts) [Fintype (G'.coe.neighborSet v)] [Fintype (G'.neighborSet v)] : G'.coe.degree v = G'.degree v := by rw [← card_neighborSet_eq_degree] exact Fintype.card_congr (coeNeighborSetEquiv v) @[simp] theorem degree_spanningCoe {G' : G.Subgraph} (v : V) [Fintype (G'.neighborSet v)] [Fintype (G'.spanningCoe.neighborSet v)] : G'.spanningCoe.degree v = G'.degree v := by rw [← card_neighborSet_eq_degree, Subgraph.degree] congr! theorem degree_eq_one_iff_unique_adj {G' : Subgraph G} {v : V} [Fintype (G'.neighborSet v)] : G'.degree v = 1 ↔ ∃! w : V, G'.Adj v w := by rw [← finset_card_neighborSet_eq_degree, Finset.card_eq_one, Finset.singleton_iff_unique_mem] simp only [Set.mem_toFinset, mem_neighborSet] lemma neighborSet_eq_of_equiv {v : V} {H : Subgraph G} (h : G.neighborSet v ≃ H.neighborSet v) (hfin : (G.neighborSet v).Finite) : H.neighborSet v = G.neighborSet v := by lift H.neighborSet v to Finset V using h.set_finite_iff.mp hfin with s hs lift G.neighborSet v to Finset V using hfin with t ht refine congrArg _ <| Finset.eq_of_subset_of_card_le ?_ (Finset.card_eq_of_equiv h).le rw [← Finset.coe_subset, hs, ht] exact H.neighborSet_subset _ lemma adj_iff_of_neighborSet_equiv {v : V} {H : Subgraph G} (h : G.neighborSet v ≃ H.neighborSet v) (hfin : (G.neighborSet v).Finite) : ∀ {w}, H.Adj v w ↔ G.Adj v w := Set.ext_iff.mp (neighborSet_eq_of_equiv h hfin) _ end Subgraph section MkProperties /-! ### Properties of `singletonSubgraph` and `subgraphOfAdj` -/ variable {G : SimpleGraph V} {G' : SimpleGraph W} instance nonempty_singletonSubgraph_verts (v : V) : Nonempty (G.singletonSubgraph v).verts := ⟨⟨v, Set.mem_singleton v⟩⟩ @[simp] theorem singletonSubgraph_le_iff (v : V) (H : G.Subgraph) : G.singletonSubgraph v ≤ H ↔ v ∈ H.verts := by refine ⟨fun h ↦ h.1 (Set.mem_singleton v), ?_⟩ intro h constructor · rwa [singletonSubgraph_verts, Set.singleton_subset_iff] · exact fun _ _ ↦ False.elim @[simp] theorem map_singletonSubgraph (f : G →g G') {v : V} : Subgraph.map f (G.singletonSubgraph v) = G'.singletonSubgraph (f v) := by ext <;> simp only [Relation.Map, Subgraph.map_adj, singletonSubgraph_adj, Pi.bot_apply, exists_and_left, and_iff_left_iff_imp, IsEmpty.forall_iff, Subgraph.map_verts, singletonSubgraph_verts, Set.image_singleton] exact False.elim @[simp] theorem neighborSet_singletonSubgraph (v w : V) : (G.singletonSubgraph v).neighborSet w = ∅ := rfl @[simp] theorem edgeSet_singletonSubgraph (v : V) : (G.singletonSubgraph v).edgeSet = ∅ := Sym2.fromRel_bot theorem eq_singletonSubgraph_iff_verts_eq (H : G.Subgraph) {v : V} : H = G.singletonSubgraph v ↔ H.verts = {v} := by refine ⟨fun h ↦ by rw [h, singletonSubgraph_verts], fun h ↦ ?_⟩ ext · rw [h, singletonSubgraph_verts] · simp only [Prop.bot_eq_false, singletonSubgraph_adj, Pi.bot_apply, iff_false] intro ha have ha1 := ha.fst_mem have ha2 := ha.snd_mem rw [h, Set.mem_singleton_iff] at ha1 ha2 subst_vars exact ha.ne rfl instance nonempty_subgraphOfAdj_verts {v w : V} (hvw : G.Adj v w) : Nonempty (G.subgraphOfAdj hvw).verts := ⟨⟨v, by simp⟩⟩ @[simp] theorem edgeSet_subgraphOfAdj {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).edgeSet = {s(v, w)} := by ext e refine e.ind ?_ simp only [eq_comm, Set.mem_singleton_iff, Subgraph.mem_edgeSet, subgraphOfAdj_adj, forall₂_true_iff] lemma subgraphOfAdj_le_of_adj {v w : V} (H : G.Subgraph) (h : H.Adj v w) : G.subgraphOfAdj (H.adj_sub h) ≤ H := by constructor · intro x rintro (rfl | rfl) <;> simp [H.edge_vert h, H.edge_vert h.symm] · simp only [subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff] rintro _ _ (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩) <;> simp [h, h.symm] theorem subgraphOfAdj_symm {v w : V} (hvw : G.Adj v w) : G.subgraphOfAdj hvw.symm = G.subgraphOfAdj hvw := by ext <;> simp [or_comm, and_comm] @[simp] theorem map_subgraphOfAdj (f : G →g G') {v w : V} (hvw : G.Adj v w) : Subgraph.map f (G.subgraphOfAdj hvw) = G'.subgraphOfAdj (f.map_adj hvw) := by ext · simp only [Subgraph.map_verts, subgraphOfAdj_verts, Set.mem_image, Set.mem_insert_iff, Set.mem_singleton_iff] constructor · rintro ⟨u, rfl | rfl, rfl⟩ <;> simp · rintro (rfl | rfl) · use v simp · use w simp · simp only [Relation.Map, Subgraph.map_adj, subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff] constructor · rintro ⟨a, b, ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, rfl, rfl⟩ <;> simp · rintro (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩) · use v, w simp · use w, v simp theorem neighborSet_subgraphOfAdj_subset {u v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet u ⊆ {v, w} := (G.subgraphOfAdj hvw).neighborSet_subset_verts _ @[simp] theorem neighborSet_fst_subgraphOfAdj {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet v = {w} := by ext u suffices w = u ↔ u = w by simpa [hvw.ne.symm] using this rw [eq_comm] @[simp] theorem neighborSet_snd_subgraphOfAdj {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet w = {v} := by rw [subgraphOfAdj_symm hvw.symm] exact neighborSet_fst_subgraphOfAdj hvw.symm @[simp] theorem neighborSet_subgraphOfAdj_of_ne_of_ne {u v w : V} (hvw : G.Adj v w) (hv : u ≠ v) (hw : u ≠ w) : (G.subgraphOfAdj hvw).neighborSet u = ∅ := by ext simp [hv.symm, hw.symm] theorem neighborSet_subgraphOfAdj [DecidableEq V] {u v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet u = (if u = v then {w} else ∅) ∪ if u = w then {v} else ∅ := by split_ifs <;> subst_vars <;> simp [*] theorem singletonSubgraph_fst_le_subgraphOfAdj {u v : V} {h : G.Adj u v} : G.singletonSubgraph u ≤ G.subgraphOfAdj h := by simp theorem singletonSubgraph_snd_le_subgraphOfAdj {u v : V} {h : G.Adj u v} : G.singletonSubgraph v ≤ G.subgraphOfAdj h := by simp @[simp] lemma support_subgraphOfAdj {u v : V} (h : G.Adj u v) : (G.subgraphOfAdj h).support = {u , v} := by ext rw [Subgraph.mem_support] simp only [subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk] refine ⟨?_, fun h ↦ h.elim (fun hl ↦ ⟨v, .inl ⟨hl.symm, rfl⟩⟩) fun hr ↦ ⟨u, .inr ⟨rfl, hr.symm⟩⟩⟩ rintro ⟨_, hw⟩ exact hw.elim (fun h1 ↦ .inl h1.1.symm) fun hr ↦ .inr hr.2.symm end MkProperties namespace Subgraph variable {G : SimpleGraph V} /-! ### Subgraphs of subgraphs -/ /-- Given a subgraph of a subgraph of `G`, construct a subgraph of `G`. -/ protected abbrev coeSubgraph {G' : G.Subgraph} : G'.coe.Subgraph → G.Subgraph := Subgraph.map G'.hom /-- Given a subgraph of `G`, restrict it to being a subgraph of another subgraph `G'` by taking the portion of `G` that intersects `G'`. -/ protected abbrev restrict {G' : G.Subgraph} : G.Subgraph → G'.coe.Subgraph := Subgraph.comap G'.hom @[simp] lemma verts_coeSubgraph {G' : Subgraph G} (G'' : Subgraph G'.coe) : (Subgraph.coeSubgraph G'').verts = (G''.verts : Set V) := rfl lemma coeSubgraph_adj {G' : G.Subgraph} (G'' : G'.coe.Subgraph) (v w : V) : (G'.coeSubgraph G'').Adj v w ↔ ∃ (hv : v ∈ G'.verts) (hw : w ∈ G'.verts), G''.Adj ⟨v, hv⟩ ⟨w, hw⟩ := by simp [Relation.Map] lemma restrict_adj {G' G'' : G.Subgraph} (v w : G'.verts) : (G'.restrict G'').Adj v w ↔ G'.Adj v w ∧ G''.Adj v w := Iff.rfl theorem restrict_coeSubgraph {G' : G.Subgraph} (G'' : G'.coe.Subgraph) : Subgraph.restrict (Subgraph.coeSubgraph G'') = G'' := by ext · simp · rw [restrict_adj, coeSubgraph_adj] simpa using G''.adj_sub theorem coeSubgraph_injective (G' : G.Subgraph) : Function.Injective (Subgraph.coeSubgraph : G'.coe.Subgraph → G.Subgraph) := Function.LeftInverse.injective restrict_coeSubgraph lemma coeSubgraph_le {H : G.Subgraph} (H' : H.coe.Subgraph) : Subgraph.coeSubgraph H' ≤ H := by constructor · simp · rintro v w ⟨_, _, h, rfl, rfl⟩ exact H'.adj_sub h lemma coeSubgraph_restrict_eq {H : G.Subgraph} (H' : G.Subgraph) : Subgraph.coeSubgraph (H.restrict H') = H ⊓ H' := by ext · simp [and_comm] · simp_rw [coeSubgraph_adj, restrict_adj] simp only [exists_and_left, exists_prop, inf_adj, and_congr_right_iff] intro h simp [H.edge_vert h, H.edge_vert h.symm] /-! ### Edge deletion -/ /-- Given a subgraph `G'` and a set of vertex pairs, remove all of the corresponding edges from its edge set, if present. See also: `SimpleGraph.deleteEdges`. -/ def deleteEdges (G' : G.Subgraph) (s : Set (Sym2 V)) : G.Subgraph where verts := G'.verts Adj := G'.Adj \ Sym2.ToRel s adj_sub h' := G'.adj_sub h'.1 edge_vert h' := G'.edge_vert h'.1 symm a b := by simp [G'.adj_comm, Sym2.eq_swap] section DeleteEdges variable {G' : G.Subgraph} (s : Set (Sym2 V)) @[simp] theorem deleteEdges_verts : (G'.deleteEdges s).verts = G'.verts := rfl @[simp] theorem deleteEdges_adj (v w : V) : (G'.deleteEdges s).Adj v w ↔ G'.Adj v w ∧ ¬s(v, w) ∈ s := Iff.rfl @[simp] theorem deleteEdges_deleteEdges (s s' : Set (Sym2 V)) : (G'.deleteEdges s).deleteEdges s' = G'.deleteEdges (s ∪ s') := by ext <;> simp [and_assoc, not_or] @[simp] theorem deleteEdges_empty_eq : G'.deleteEdges ∅ = G' := by ext <;> simp @[simp] theorem deleteEdges_spanningCoe_eq : G'.spanningCoe.deleteEdges s = (G'.deleteEdges s).spanningCoe := by ext simp theorem deleteEdges_coe_eq (s : Set (Sym2 G'.verts)) : G'.coe.deleteEdges s = (G'.deleteEdges (Sym2.map (↑) '' s)).coe := by ext ⟨v, hv⟩ ⟨w, hw⟩ simp only [SimpleGraph.deleteEdges_adj, coe_adj, deleteEdges_adj, Set.mem_image, not_exists, not_and, and_congr_right_iff] intro constructor · intro hs refine Sym2.ind ?_ rintro ⟨v', hv'⟩ ⟨w', hw'⟩ simp only [Sym2.map_pair_eq, Sym2.eq] contrapose! rintro (_ | _) <;> simpa only [Sym2.eq_swap] · intro h' hs exact h' _ hs rfl theorem coe_deleteEdges_eq (s : Set (Sym2 V)) : (G'.deleteEdges s).coe = G'.coe.deleteEdges (Sym2.map (↑) ⁻¹' s) := by ext ⟨v, hv⟩ ⟨w, hw⟩
simp theorem deleteEdges_le : G'.deleteEdges s ≤ G' := by constructor <;> simp +contextual [subset_rfl] theorem deleteEdges_le_of_le {s s' : Set (Sym2 V)} (h : s ⊆ s') : G'.deleteEdges s' ≤ G'.deleteEdges s := by constructor <;> simp +contextual only [deleteEdges_verts, deleteEdges_adj, true_and, and_imp, subset_rfl] exact fun _ _ _ hs' hs ↦ hs' (h hs) @[simp] theorem deleteEdges_inter_edgeSet_left_eq : G'.deleteEdges (G'.edgeSet ∩ s) = G'.deleteEdges s := by ext <;> simp +contextual [imp_false]
Mathlib/Combinatorics/SimpleGraph/Subgraph.lean
1,084
1,098
/- 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 -/ import Mathlib.Data.Set.Piecewise import Mathlib.Logic.Equiv.Defs import Mathlib.Tactic.Core import Mathlib.Tactic.Attr.Core /-! # Partial equivalences This files defines equivalences between subsets of given types. An element `e` of `PartialEquiv α β` is made of two maps `e.toFun` and `e.invFun` respectively from α to β and from β to α (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively α and β. They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two partial equivalences by restricting the source and target to the maximal set where the composition makes sense. As for equivs, we register a coercion to functions and use it in our simp normal form: we write `e x` and `e.symm y` instead of `e.toFun x` and `e.invFun y`. ## Main definitions * `Equiv.toPartialEquiv`: associating a partial equiv to an equiv, with source = target = univ * `PartialEquiv.symm`: the inverse of a partial equivalence * `PartialEquiv.trans`: the composition of two partial equivalences * `PartialEquiv.refl`: the identity partial equivalence * `PartialEquiv.ofSet`: the identity on a set `s` * `EqOnSource`: equivalence relation describing the "right" notion of equality for partial equivalences (see below in implementation notes) ## Implementation notes There are at least three possible implementations of partial equivalences: * equivs on subtypes * pairs of functions taking values in `Option α` and `Option β`, equal to none where the partial equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `PEquiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The `PartialEquiv` version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `toFun` and `invFun` outside of `source` and `target`). In particular, the equality notion between partial equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no partial equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal partial equivs, this is not an issue in practice. Still, we introduce an equivalence relation `EqOnSource` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. ### Local coding conventions If a lemma deals with the intersection of a set with either source or target of a `PartialEquiv`, then it should use `e.source ∩ s` or `e.target ∩ t`, not `s ∩ e.source` or `t ∩ e.target`. -/ open Lean Meta Elab Tactic /-! Implementation of the `mfld_set_tac` tactic for working with the domains of partially-defined functions (`PartialEquiv`, `PartialHomeomorph`, etc). This is in a separate file from `Mathlib.Logic.Equiv.MfldSimpsAttr` because attributes need a new file to become functional. -/ /-- Common `@[simps]` configuration options used for manifold-related declarations. -/ def mfld_cfg : Simps.Config where attrs := [`mfld_simps] fullyApplied := false namespace Tactic.MfldSetTac /-- A very basic tactic to show that sets showing up in manifolds coincide or are included in one another. -/ elab (name := mfldSetTac) "mfld_set_tac" : tactic => withMainContext do let g ← getMainGoal let goalTy := (← instantiateMVars (← g.getDecl).type).getAppFnArgs match goalTy with | (``Eq, #[_ty, _e₁, _e₂]) => evalTactic (← `(tactic| ( apply Set.ext; intro my_y constructor <;> · intro h_my_y try simp only [*, mfld_simps] at h_my_y try simp only [*, mfld_simps]))) | (``Subset, #[_ty, _inst, _e₁, _e₂]) => evalTactic (← `(tactic| ( intro my_y h_my_y try simp only [*, mfld_simps] at h_my_y try simp only [*, mfld_simps]))) | _ => throwError "goal should be an equality or an inclusion" attribute [mfld_simps] and_true eq_self_iff_true Function.comp_apply end Tactic.MfldSetTac open Function Set variable {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Local equivalence between subsets `source` and `target` of `α` and `β` respectively. The (global) maps `toFun : α → β` and `invFun : β → α` map `source` to `target` and conversely, and are inverse to each other there. The values of `toFun` outside of `source` and of `invFun` outside of `target` are irrelevant. -/ structure PartialEquiv (α : Type*) (β : Type*) where /-- The global function which has a partial inverse. Its value outside of the `source` subset is irrelevant. -/ toFun : α → β /-- The partial inverse to `toFun`. Its value outside of the `target` subset is irrelevant. -/ invFun : β → α /-- The domain of the partial equivalence. -/ source : Set α /-- The codomain of the partial equivalence. -/ target : Set β /-- The proposition that elements of `source` are mapped to elements of `target`. -/ map_source' : ∀ ⦃x⦄, x ∈ source → toFun x ∈ target /-- The proposition that elements of `target` are mapped to elements of `source`. -/ map_target' : ∀ ⦃x⦄, x ∈ target → invFun x ∈ source /-- The proposition that `invFun` is a left-inverse of `toFun` on `source`. -/ left_inv' : ∀ ⦃x⦄, x ∈ source → invFun (toFun x) = x /-- The proposition that `invFun` is a right-inverse of `toFun` on `target`. -/ right_inv' : ∀ ⦃x⦄, x ∈ target → toFun (invFun x) = x attribute [coe] PartialEquiv.toFun namespace PartialEquiv variable (e : PartialEquiv α β) (e' : PartialEquiv β γ) instance [Inhabited α] [Inhabited β] : Inhabited (PartialEquiv α β) := ⟨⟨const α default, const β default, ∅, ∅, mapsTo_empty _ _, mapsTo_empty _ _, eqOn_empty _ _, eqOn_empty _ _⟩⟩ /-- The inverse of a partial equivalence -/ @[symm] protected def symm : PartialEquiv β α where toFun := e.invFun invFun := e.toFun source := e.target target := e.source map_source' := e.map_target' map_target' := e.map_source' left_inv' := e.right_inv' right_inv' := e.left_inv' instance : CoeFun (PartialEquiv α β) fun _ => α → β := ⟨PartialEquiv.toFun⟩ /-- See Note [custom simps projection] -/ def Simps.symm_apply (e : PartialEquiv α β) : β → α := e.symm initialize_simps_projections PartialEquiv (toFun → apply, invFun → symm_apply) theorem coe_mk (f : α → β) (g s t ml mr il ir) : (PartialEquiv.mk f g s t ml mr il ir : α → β) = f := rfl @[simp, mfld_simps] theorem coe_symm_mk (f : α → β) (g s t ml mr il ir) : ((PartialEquiv.mk f g s t ml mr il ir).symm : β → α) = g := rfl @[simp, mfld_simps] theorem invFun_as_coe : e.invFun = e.symm := rfl @[simp, mfld_simps] theorem map_source {x : α} (h : x ∈ e.source) : e x ∈ e.target := e.map_source' h /-- Variant of `e.map_source` and `map_source'`, stated for images of subsets of `source`. -/ lemma map_source'' : e '' e.source ⊆ e.target := fun _ ⟨_, hx, hex⟩ ↦ mem_of_eq_of_mem (id hex.symm) (e.map_source' hx) @[simp, mfld_simps] theorem map_target {x : β} (h : x ∈ e.target) : e.symm x ∈ e.source := e.map_target' h @[simp, mfld_simps] theorem left_inv {x : α} (h : x ∈ e.source) : e.symm (e x) = x := e.left_inv' h @[simp, mfld_simps] theorem right_inv {x : β} (h : x ∈ e.target) : e (e.symm x) = x := e.right_inv' h theorem eq_symm_apply {x : α} {y : β} (hx : x ∈ e.source) (hy : y ∈ e.target) : x = e.symm y ↔ e x = y := ⟨fun h => by rw [← e.right_inv hy, h], fun h => by rw [← e.left_inv hx, h]⟩ protected theorem mapsTo : MapsTo e e.source e.target := fun _ => e.map_source theorem symm_mapsTo : MapsTo e.symm e.target e.source := e.symm.mapsTo protected theorem leftInvOn : LeftInvOn e.symm e e.source := fun _ => e.left_inv protected theorem rightInvOn : RightInvOn e.symm e e.target := fun _ => e.right_inv protected theorem invOn : InvOn e.symm e e.source e.target := ⟨e.leftInvOn, e.rightInvOn⟩ protected theorem injOn : InjOn e e.source := e.leftInvOn.injOn protected theorem bijOn : BijOn e e.source e.target := e.invOn.bijOn e.mapsTo e.symm_mapsTo protected theorem surjOn : SurjOn e e.source e.target := e.bijOn.surjOn /-- Interpret an `Equiv` as a `PartialEquiv` by restricting it to `s` in the domain and to `t` in the codomain. -/ @[simps -fullyApplied] def _root_.Equiv.toPartialEquivOfImageEq (e : α ≃ β) (s : Set α) (t : Set β) (h : e '' s = t) : PartialEquiv α β where toFun := e invFun := e.symm source := s target := t map_source' _ hx := h ▸ mem_image_of_mem _ hx map_target' x hx := by subst t rcases hx with ⟨x, hx, rfl⟩ rwa [e.symm_apply_apply] left_inv' x _ := e.symm_apply_apply x right_inv' x _ := e.apply_symm_apply x /-- Associate a `PartialEquiv` to an `Equiv`. -/ @[simps! (config := mfld_cfg)] def _root_.Equiv.toPartialEquiv (e : α ≃ β) : PartialEquiv α β := e.toPartialEquivOfImageEq univ univ <| by rw [image_univ, e.surjective.range_eq] instance inhabitedOfEmpty [IsEmpty α] [IsEmpty β] : Inhabited (PartialEquiv α β) := ⟨((Equiv.equivEmpty α).trans (Equiv.equivEmpty β).symm).toPartialEquiv⟩ /-- Create a copy of a `PartialEquiv` providing better definitional equalities. -/ @[simps -fullyApplied] def copy (e : PartialEquiv α β) (f : α → β) (hf : ⇑e = f) (g : β → α) (hg : ⇑e.symm = g) (s : Set α) (hs : e.source = s) (t : Set β) (ht : e.target = t) : PartialEquiv α β where toFun := f invFun := g source := s target := t map_source' _ := ht ▸ hs ▸ hf ▸ e.map_source map_target' _ := hs ▸ ht ▸ hg ▸ e.map_target left_inv' _ := hs ▸ hf ▸ hg ▸ e.left_inv right_inv' _ := ht ▸ hf ▸ hg ▸ e.right_inv theorem copy_eq (e : PartialEquiv α β) (f : α → β) (hf : ⇑e = f) (g : β → α) (hg : ⇑e.symm = g) (s : Set α) (hs : e.source = s) (t : Set β) (ht : e.target = t) : e.copy f hf g hg s hs t ht = e := by substs f g s t cases e rfl /-- Associate to a `PartialEquiv` an `Equiv` between the source and the target. -/ protected def toEquiv : e.source ≃ e.target where toFun x := ⟨e x, e.map_source x.mem⟩ invFun y := ⟨e.symm y, e.map_target y.mem⟩ left_inv := fun ⟨_, hx⟩ => Subtype.eq <| e.left_inv hx right_inv := fun ⟨_, hy⟩ => Subtype.eq <| e.right_inv hy @[simp, mfld_simps] theorem symm_source : e.symm.source = e.target := rfl @[simp, mfld_simps] theorem symm_target : e.symm.target = e.source := rfl @[simp, mfld_simps] theorem symm_symm : e.symm.symm = e := rfl theorem symm_bijective : Function.Bijective (PartialEquiv.symm : PartialEquiv α β → PartialEquiv β α) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ theorem image_source_eq_target : e '' e.source = e.target := e.bijOn.image_eq theorem forall_mem_target {p : β → Prop} : (∀ y ∈ e.target, p y) ↔ ∀ x ∈ e.source, p (e x) := by rw [← image_source_eq_target, forall_mem_image] theorem exists_mem_target {p : β → Prop} : (∃ y ∈ e.target, p y) ↔ ∃ x ∈ e.source, p (e x) := by rw [← image_source_eq_target, exists_mem_image] /-- We say that `t : Set β` is an image of `s : Set α` under a partial equivalence if any of the following equivalent conditions hold: * `e '' (e.source ∩ s) = e.target ∩ t`; * `e.source ∩ e ⁻¹ t = e.source ∩ s`; * `∀ x ∈ e.source, e x ∈ t ↔ x ∈ s` (this one is used in the definition). -/ def IsImage (s : Set α) (t : Set β) : Prop := ∀ ⦃x⦄, x ∈ e.source → (e x ∈ t ↔ x ∈ s) namespace IsImage variable {e} {s : Set α} {t : Set β} {x : α} theorem apply_mem_iff (h : e.IsImage s t) (hx : x ∈ e.source) : e x ∈ t ↔ x ∈ s := h hx theorem symm_apply_mem_iff (h : e.IsImage s t) : ∀ ⦃y⦄, y ∈ e.target → (e.symm y ∈ s ↔ y ∈ t) := e.forall_mem_target.mpr fun x hx => by rw [e.left_inv hx, h hx] protected theorem symm (h : e.IsImage s t) : e.symm.IsImage t s := h.symm_apply_mem_iff @[simp] theorem symm_iff : e.symm.IsImage t s ↔ e.IsImage s t := ⟨fun h => h.symm, fun h => h.symm⟩ protected theorem mapsTo (h : e.IsImage s t) : MapsTo e (e.source ∩ s) (e.target ∩ t) := fun _ hx => ⟨e.mapsTo hx.1, (h hx.1).2 hx.2⟩ theorem symm_mapsTo (h : e.IsImage s t) : MapsTo e.symm (e.target ∩ t) (e.source ∩ s) := h.symm.mapsTo /-- Restrict a `PartialEquiv` to a pair of corresponding sets. -/ @[simps -fullyApplied] def restr (h : e.IsImage s t) : PartialEquiv α β where toFun := e invFun := e.symm source := e.source ∩ s target := e.target ∩ t map_source' := h.mapsTo map_target' := h.symm_mapsTo left_inv' := e.leftInvOn.mono inter_subset_left right_inv' := e.rightInvOn.mono inter_subset_left theorem image_eq (h : e.IsImage s t) : e '' (e.source ∩ s) = e.target ∩ t := h.restr.image_source_eq_target theorem symm_image_eq (h : e.IsImage s t) : e.symm '' (e.target ∩ t) = e.source ∩ s := h.symm.image_eq theorem iff_preimage_eq : e.IsImage s t ↔ e.source ∩ e ⁻¹' t = e.source ∩ s := by simp only [IsImage, Set.ext_iff, mem_inter_iff, mem_preimage, and_congr_right_iff] alias ⟨preimage_eq, of_preimage_eq⟩ := iff_preimage_eq theorem iff_symm_preimage_eq : e.IsImage s t ↔ e.target ∩ e.symm ⁻¹' s = e.target ∩ t := symm_iff.symm.trans iff_preimage_eq alias ⟨symm_preimage_eq, of_symm_preimage_eq⟩ := iff_symm_preimage_eq theorem of_image_eq (h : e '' (e.source ∩ s) = e.target ∩ t) : e.IsImage s t := of_symm_preimage_eq <| Eq.trans (of_symm_preimage_eq rfl).image_eq.symm h theorem of_symm_image_eq (h : e.symm '' (e.target ∩ t) = e.source ∩ s) : e.IsImage s t := of_preimage_eq <| Eq.trans (iff_preimage_eq.2 rfl).symm_image_eq.symm h protected theorem compl (h : e.IsImage s t) : e.IsImage sᶜ tᶜ := fun _ hx => not_congr (h hx) protected theorem inter {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s ∩ s') (t ∩ t') := fun _ hx => and_congr (h hx) (h' hx)
protected theorem union {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') :
Mathlib/Logic/Equiv/PartialEquiv.lean
372
373
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Moritz Doll -/ import Mathlib.LinearAlgebra.Prod /-! # Partially defined linear maps A `LinearPMap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. We define a `SemilatticeInf` with `OrderBot` instance on this, and define three operations: * `mkSpanSingleton` defines a partial linear map defined on the span of a singleton. * `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that extends both `f` and `g`. * `sSup` takes a `DirectedOn (· ≤ ·)` set of partial linear maps, and returns the unique partial linear map on the `sSup` of their domains that extends all these maps. Moreover, we define * `LinearPMap.graph` is the graph of the partial linear map viewed as a submodule of `E × F`. Partially defined maps are currently used in `Mathlib` to prove Hahn-Banach theorem and its variations. Namely, `LinearPMap.sSup` implies that every chain of `LinearPMap`s is bounded above. They are also the basis for the theory of unbounded operators. -/ universe u v w /-- A `LinearPMap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. -/ structure LinearPMap (R : Type u) [Ring R] (E : Type v) [AddCommGroup E] [Module R E] (F : Type w) [AddCommGroup F] [Module R F] where domain : Submodule R E toFun : domain →ₗ[R] F @[inherit_doc] notation:25 E " →ₗ.[" R:25 "] " F:0 => LinearPMap R E F variable {R : Type*} [Ring R] {E : Type*} [AddCommGroup E] [Module R E] {F : Type*} [AddCommGroup F] [Module R F] {G : Type*} [AddCommGroup G] [Module R G] namespace LinearPMap open Submodule @[coe] def toFun' (f : E →ₗ.[R] F) : f.domain → F := f.toFun instance : CoeFun (E →ₗ.[R] F) fun f : E →ₗ.[R] F => f.domain → F := ⟨toFun'⟩ @[simp] theorem toFun_eq_coe (f : E →ₗ.[R] F) (x : f.domain) : f.toFun x = f x := rfl @[ext (iff := false)] theorem ext {f g : E →ₗ.[R] F} (h : f.domain = g.domain) (h' : ∀ ⦃x : E⦄ ⦃hf : x ∈ f.domain⦄ ⦃hg : x ∈ g.domain⦄, f ⟨x, hf⟩ = g ⟨x, hg⟩) : f = g := by rcases f with ⟨f_dom, f⟩ rcases g with ⟨g_dom, g⟩ obtain rfl : f_dom = g_dom := h congr apply LinearMap.ext intro x apply h' /-- A dependent version of `ext`. -/ theorem dExt {f g : E →ₗ.[R] F} (h : f.domain = g.domain) (h' : ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (_h : (x : E) = y), f x = g y) : f = g := ext h fun _ _ _ ↦ h' rfl @[simp] theorem map_zero (f : E →ₗ.[R] F) : f 0 = 0 := f.toFun.map_zero theorem ext_iff {f g : E →ₗ.[R] F} : f = g ↔ f.domain = g.domain ∧ ∀ ⦃x : E⦄ ⦃hf : x ∈ f.domain⦄ ⦃hg : x ∈ g.domain⦄, f ⟨x, hf⟩ = g ⟨x, hg⟩ := ⟨by rintro rfl; simp, fun ⟨deq, feq⟩ ↦ ext deq feq⟩ theorem dExt_iff {f g : E →ₗ.[R] F} : f = g ↔ ∃ _domain_eq : f.domain = g.domain, ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (_h : (x : E) = y), f x = g y := ⟨fun EQ => EQ ▸ ⟨rfl, fun x y h => by congr exact mod_cast h⟩, fun ⟨deq, feq⟩ => dExt deq feq⟩ theorem ext' {s : Submodule R E} {f g : s →ₗ[R] F} (h : f = g) : mk s f = mk s g := h ▸ rfl theorem map_add (f : E →ₗ.[R] F) (x y : f.domain) : f (x + y) = f x + f y := f.toFun.map_add x y theorem map_neg (f : E →ₗ.[R] F) (x : f.domain) : f (-x) = -f x := f.toFun.map_neg x theorem map_sub (f : E →ₗ.[R] F) (x y : f.domain) : f (x - y) = f x - f y := f.toFun.map_sub x y theorem map_smul (f : E →ₗ.[R] F) (c : R) (x : f.domain) : f (c • x) = c • f x := f.toFun.map_smul c x @[simp] theorem mk_apply (p : Submodule R E) (f : p →ₗ[R] F) (x : p) : mk p f x = f x := rfl /-- The unique `LinearPMap` on `R ∙ x` that sends `x` to `y`. This version works for modules over rings, and requires a proof of `∀ c, c • x = 0 → c • y = 0`. -/ noncomputable def mkSpanSingleton' (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : E →ₗ.[R] F where domain := R ∙ x toFun := have H : ∀ c₁ c₂ : R, c₁ • x = c₂ • x → c₁ • y = c₂ • y := by intro c₁ c₂ h rw [← sub_eq_zero, ← sub_smul] at h ⊢ exact H _ h { toFun z := Classical.choose (mem_span_singleton.1 z.prop) • y map_add' y z := by rw [← add_smul, H] have (w : R ∙ x) := Classical.choose_spec (mem_span_singleton.1 w.prop) simp only [add_smul, sub_smul, this, ← coe_add] map_smul' c z := by rw [smul_smul, H] have (w : R ∙ x) := Classical.choose_spec (mem_span_singleton.1 w.prop) simp only [mul_smul, this] apply coe_smul } @[simp] theorem domain_mkSpanSingleton (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : (mkSpanSingleton' x y H).domain = R ∙ x := rfl @[simp] theorem mkSpanSingleton'_apply (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) (c : R) (h) : mkSpanSingleton' x y H ⟨c • x, h⟩ = c • y := by dsimp [mkSpanSingleton'] rw [← sub_eq_zero, ← sub_smul] apply H simp only [sub_smul, one_smul, sub_eq_zero] apply Classical.choose_spec (mem_span_singleton.1 h) @[simp] theorem mkSpanSingleton'_apply_self (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) (h) : mkSpanSingleton' x y H ⟨x, h⟩ = y := by conv_rhs => rw [← one_smul R y] rw [← mkSpanSingleton'_apply x y H 1 ?_] · congr rw [one_smul] · rwa [one_smul] /-- The unique `LinearPMap` on `span R {x}` that sends a non-zero vector `x` to `y`. This version works for modules over division rings. -/ noncomputable abbrev mkSpanSingleton {K E F : Type*} [DivisionRing K] [AddCommGroup E] [Module K E] [AddCommGroup F] [Module K F] (x : E) (y : F) (hx : x ≠ 0) : E →ₗ.[K] F := mkSpanSingleton' x y fun c hc => (smul_eq_zero.1 hc).elim (fun hc => by rw [hc, zero_smul]) fun hx' => absurd hx' hx theorem mkSpanSingleton_apply (K : Type*) {E F : Type*} [DivisionRing K] [AddCommGroup E] [Module K E] [AddCommGroup F] [Module K F] {x : E} (hx : x ≠ 0) (y : F) : mkSpanSingleton x y hx ⟨x, (Submodule.mem_span_singleton_self x : x ∈ Submodule.span K {x})⟩ = y := LinearPMap.mkSpanSingleton'_apply_self _ _ _ _ /-- Projection to the first coordinate as a `LinearPMap` -/ protected def fst (p : Submodule R E) (p' : Submodule R F) : E × F →ₗ.[R] E where domain := p.prod p' toFun := (LinearMap.fst R E F).comp (p.prod p').subtype @[simp] theorem fst_apply (p : Submodule R E) (p' : Submodule R F) (x : p.prod p') : LinearPMap.fst p p' x = (x : E × F).1 := rfl /-- Projection to the second coordinate as a `LinearPMap` -/ protected def snd (p : Submodule R E) (p' : Submodule R F) : E × F →ₗ.[R] F where domain := p.prod p' toFun := (LinearMap.snd R E F).comp (p.prod p').subtype @[simp] theorem snd_apply (p : Submodule R E) (p' : Submodule R F) (x : p.prod p') : LinearPMap.snd p p' x = (x : E × F).2 := rfl instance le : LE (E →ₗ.[R] F) := ⟨fun f g => f.domain ≤ g.domain ∧ ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (_h : (x : E) = y), f x = g y⟩ theorem apply_comp_inclusion {T S : E →ₗ.[R] F} (h : T ≤ S) (x : T.domain) : T x = S (Submodule.inclusion h.1 x) := h.2 rfl theorem exists_of_le {T S : E →ₗ.[R] F} (h : T ≤ S) (x : T.domain) : ∃ y : S.domain, (x : E) = y ∧ T x = S y := ⟨⟨x.1, h.1 x.2⟩, ⟨rfl, h.2 rfl⟩⟩ theorem eq_of_le_of_domain_eq {f g : E →ₗ.[R] F} (hle : f ≤ g) (heq : f.domain = g.domain) : f = g := dExt heq hle.2 /-- Given two partial linear maps `f`, `g`, the set of points `x` such that both `f` and `g` are defined at `x` and `f x = g x` form a submodule. -/ def eqLocus (f g : E →ₗ.[R] F) : Submodule R E where carrier := { x | ∃ (hf : x ∈ f.domain) (hg : x ∈ g.domain), f ⟨x, hf⟩ = g ⟨x, hg⟩ } zero_mem' := ⟨zero_mem _, zero_mem _, f.map_zero.trans g.map_zero.symm⟩ add_mem' {x y} := fun ⟨hfx, hgx, hx⟩ ⟨hfy, hgy, hy⟩ ↦ ⟨add_mem hfx hfy, add_mem hgx hgy, by simp_all [← AddMemClass.mk_add_mk, f.map_add, g.map_add]⟩ smul_mem' c x := fun ⟨hfx, hgx, hx⟩ ↦ ⟨smul_mem _ c hfx, smul_mem _ c hgx, by have {f : E →ₗ.[R] F} (hfx) : (⟨c • x, smul_mem _ c hfx⟩ : f.domain) = c • ⟨x, hfx⟩ := by simp rw [this hfx, this hgx, f.map_smul, g.map_smul, hx]⟩ instance bot : Bot (E →ₗ.[R] F) := ⟨⟨⊥, 0⟩⟩ instance inhabited : Inhabited (E →ₗ.[R] F) := ⟨⊥⟩ instance semilatticeInf : SemilatticeInf (E →ₗ.[R] F) where le := (· ≤ ·) le_refl f := ⟨le_refl f.domain, fun _ _ h => Subtype.eq h ▸ rfl⟩ le_trans := fun _ _ _ ⟨fg_le, fg_eq⟩ ⟨gh_le, gh_eq⟩ => ⟨le_trans fg_le gh_le, fun x _ hxz => have hxy : (x : E) = inclusion fg_le x := rfl (fg_eq hxy).trans (gh_eq <| hxy.symm.trans hxz)⟩ le_antisymm _ _ fg gf := eq_of_le_of_domain_eq fg (le_antisymm fg.1 gf.1) inf f g := ⟨f.eqLocus g, f.toFun.comp <| inclusion fun _x hx => hx.fst⟩ le_inf := by intro f g h ⟨fg_le, fg_eq⟩ ⟨fh_le, fh_eq⟩ exact ⟨fun x hx => ⟨fg_le hx, fh_le hx, (fg_eq (x := ⟨x, hx⟩) rfl).symm.trans (fh_eq rfl)⟩, fun x ⟨y, yg, hy⟩ h => fg_eq h⟩ inf_le_left f _ := ⟨fun _ hx => hx.fst, fun _ _ h => congr_arg f <| Subtype.eq <| h⟩ inf_le_right _ g := ⟨fun _ hx => hx.snd.fst, fun ⟨_, _, _, hx⟩ _ h => hx.trans <| congr_arg g <| Subtype.eq <| h⟩ instance orderBot : OrderBot (E →ₗ.[R] F) where bot := ⊥ bot_le f := ⟨bot_le, fun x y h => by have hx : x = 0 := Subtype.eq ((mem_bot R).1 x.2) have hy : y = 0 := Subtype.eq (h.symm.trans (congr_arg _ hx)) rw [hx, hy, map_zero, map_zero]⟩ theorem le_of_eqLocus_ge {f g : E →ₗ.[R] F} (H : f.domain ≤ f.eqLocus g) : f ≤ g := suffices f ≤ f ⊓ g from le_trans this inf_le_right ⟨H, fun _x _y hxy => ((inf_le_left : f ⊓ g ≤ f).2 hxy.symm).symm⟩ theorem domain_mono : StrictMono (@domain R _ E _ _ F _ _) := fun _f _g hlt => lt_of_le_of_ne hlt.1.1 fun heq => ne_of_lt hlt <| eq_of_le_of_domain_eq (le_of_lt hlt) heq private theorem sup_aux (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) : ∃ fg : ↥(f.domain ⊔ g.domain) →ₗ[R] F, ∀ (x : f.domain) (y : g.domain) (z : ↥(f.domain ⊔ g.domain)), (x : E) + y = ↑z → fg z = f x + g y := by choose x hx y hy hxy using fun z : ↥(f.domain ⊔ g.domain) => mem_sup.1 z.prop set fg := fun z => f ⟨x z, hx z⟩ + g ⟨y z, hy z⟩ have fg_eq : ∀ (x' : f.domain) (y' : g.domain) (z' : ↥(f.domain ⊔ g.domain)) (_H : (x' : E) + y' = z'), fg z' = f x' + g y' := by intro x' y' z' H dsimp [fg] rw [add_comm, ← sub_eq_sub_iff_add_eq_add, eq_comm, ← map_sub, ← map_sub] apply h simp only [← eq_sub_iff_add_eq] at hxy simp only [AddSubgroupClass.coe_sub, coe_mk, coe_mk, hxy, ← sub_add, ← sub_sub, sub_self, zero_sub, ← H] apply neg_add_eq_sub use { toFun := fg, map_add' := ?_, map_smul' := ?_ }, fg_eq · rintro ⟨z₁, hz₁⟩ ⟨z₂, hz₂⟩ rw [← add_assoc, add_right_comm (f _), ← map_add, add_assoc, ← map_add] apply fg_eq simp only [coe_add, coe_mk, ← add_assoc] rw [add_right_comm (x _), hxy, add_assoc, hxy, coe_mk, coe_mk] · intro c z rw [smul_add, ← map_smul, ← map_smul] apply fg_eq simp only [coe_smul, coe_mk, ← smul_add, hxy, RingHom.id_apply] /-- Given two partial linear maps that agree on the intersection of their domains, `f.sup g h` is the unique partial linear map on `f.domain ⊔ g.domain` that agrees with `f` and `g`. -/ protected noncomputable def sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) : E →ₗ.[R] F := ⟨_, Classical.choose (sup_aux f g h)⟩ @[simp] theorem domain_sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) : (f.sup g h).domain = f.domain ⊔ g.domain := rfl theorem sup_apply {f g : E →ₗ.[R] F} (H : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) (x : f.domain) (y : g.domain) (z : ↥(f.domain ⊔ g.domain)) (hz : (↑x : E) + ↑y = ↑z) : f.sup g H z = f x + g y := Classical.choose_spec (sup_aux f g H) x y z hz protected theorem left_le_sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) : f ≤ f.sup g h := by refine ⟨le_sup_left, fun z₁ z₂ hz => ?_⟩ rw [← add_zero (f _), ← g.map_zero] refine (sup_apply h _ _ _ ?_).symm simpa protected theorem right_le_sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) : g ≤ f.sup g h := by refine ⟨le_sup_right, fun z₁ z₂ hz => ?_⟩ rw [← zero_add (g _), ← f.map_zero] refine (sup_apply h _ _ _ ?_).symm simpa protected theorem sup_le {f g h : E →ₗ.[R] F} (H : ∀ (x : f.domain) (y : g.domain), (x : E) = y → f x = g y) (fh : f ≤ h) (gh : g ≤ h) : f.sup g H ≤ h := have Hf : f ≤ f.sup g H ⊓ h := le_inf (f.left_le_sup g H) fh have Hg : g ≤ f.sup g H ⊓ h := le_inf (f.right_le_sup g H) gh le_of_eqLocus_ge <| sup_le Hf.1 Hg.1 /-- Hypothesis for `LinearPMap.sup` holds, if `f.domain` is disjoint with `g.domain`. -/ theorem sup_h_of_disjoint (f g : E →ₗ.[R] F) (h : Disjoint f.domain g.domain) (x : f.domain) (y : g.domain) (hxy : (x : E) = y) : f x = g y := by rw [disjoint_def] at h have hy : y = 0 := Subtype.eq (h y (hxy ▸ x.2) y.2) have hx : x = 0 := Subtype.eq (hxy.trans <| congr_arg _ hy) simp [*] /-! ### Algebraic operations -/ section Zero instance instZero : Zero (E →ₗ.[R] F) := ⟨⊤, 0⟩ @[simp] theorem zero_domain : (0 : E →ₗ.[R] F).domain = ⊤ := rfl @[simp] theorem zero_apply (x : (⊤ : Submodule R E)) : (0 : E →ₗ.[R] F) x = 0 := rfl end Zero section SMul variable {M N : Type*} [Monoid M] [DistribMulAction M F] [SMulCommClass R M F] variable [Monoid N] [DistribMulAction N F] [SMulCommClass R N F] instance instSMul : SMul M (E →ₗ.[R] F) := ⟨fun a f => { domain := f.domain toFun := a • f.toFun }⟩ @[simp] theorem smul_domain (a : M) (f : E →ₗ.[R] F) : (a • f).domain = f.domain := rfl theorem smul_apply (a : M) (f : E →ₗ.[R] F) (x : (a • f).domain) : (a • f) x = a • f x := rfl @[simp] theorem coe_smul (a : M) (f : E →ₗ.[R] F) : ⇑(a • f) = a • ⇑f := rfl instance instSMulCommClass [SMulCommClass M N F] : SMulCommClass M N (E →ₗ.[R] F) := ⟨fun a b f => ext' <| smul_comm a b f.toFun⟩ instance instIsScalarTower [SMul M N] [IsScalarTower M N F] : IsScalarTower M N (E →ₗ.[R] F) := ⟨fun a b f => ext' <| smul_assoc a b f.toFun⟩ instance instMulAction : MulAction M (E →ₗ.[R] F) where smul := (· • ·) one_smul := fun ⟨_s, f⟩ => ext' <| one_smul M f mul_smul a b f := ext' <| mul_smul a b f.toFun end SMul instance instNeg : Neg (E →ₗ.[R] F) := ⟨fun f => ⟨f.domain, -f.toFun⟩⟩ @[simp] theorem neg_domain (f : E →ₗ.[R] F) : (-f).domain = f.domain := rfl @[simp] theorem neg_apply (f : E →ₗ.[R] F) (x) : (-f) x = -f x := rfl instance instInvolutiveNeg : InvolutiveNeg (E →ₗ.[R] F) := ⟨fun f => by ext x y hxy · rfl · simp only [neg_apply, neg_neg]⟩ section Add instance instAdd : Add (E →ₗ.[R] F) := ⟨fun f g => { domain := f.domain ⊓ g.domain toFun := f.toFun.comp (inclusion (inf_le_left : f.domain ⊓ g.domain ≤ _)) + g.toFun.comp (inclusion (inf_le_right : f.domain ⊓ g.domain ≤ _)) }⟩ theorem add_domain (f g : E →ₗ.[R] F) : (f + g).domain = f.domain ⊓ g.domain := rfl theorem add_apply (f g : E →ₗ.[R] F) (x : (f.domain ⊓ g.domain : Submodule R E)) : (f + g) x = f ⟨x, x.prop.1⟩ + g ⟨x, x.prop.2⟩ := rfl instance instAddSemigroup : AddSemigroup (E →ₗ.[R] F) := ⟨fun f g h => by ext x y hxy · simp only [add_domain, inf_assoc] · simp only [add_apply, hxy, add_assoc]⟩ instance instAddZeroClass : AddZeroClass (E →ₗ.[R] F) := ⟨fun f => by ext x y hxy · simp [add_domain] · simp only [add_apply, hxy, zero_apply, zero_add], fun f => by ext x y hxy · simp [add_domain] · simp only [add_apply, hxy, zero_apply, add_zero]⟩ instance instAddMonoid : AddMonoid (E →ₗ.[R] F) where zero_add f := by simp add_zero := by simp nsmul := nsmulRec instance instAddCommMonoid : AddCommMonoid (E →ₗ.[R] F) := ⟨fun f g => by ext x y hxy · simp only [add_domain, inf_comm] · simp only [add_apply, hxy, add_comm]⟩ end Add section VAdd instance instVAdd : VAdd (E →ₗ[R] F) (E →ₗ.[R] F) := ⟨fun f g => { domain := g.domain toFun := f.comp g.domain.subtype + g.toFun }⟩ @[simp] theorem vadd_domain (f : E →ₗ[R] F) (g : E →ₗ.[R] F) : (f +ᵥ g).domain = g.domain := rfl theorem vadd_apply (f : E →ₗ[R] F) (g : E →ₗ.[R] F) (x : (f +ᵥ g).domain) : (f +ᵥ g) x = f x + g x := rfl @[simp] theorem coe_vadd (f : E →ₗ[R] F) (g : E →ₗ.[R] F) : ⇑(f +ᵥ g) = ⇑(f.comp g.domain.subtype) + ⇑g := rfl instance instAddAction : AddAction (E →ₗ[R] F) (E →ₗ.[R] F) where vadd := (· +ᵥ ·) zero_vadd := fun ⟨_s, _f⟩ => ext' <| zero_add _ add_vadd := fun _f₁ _f₂ ⟨_s, _g⟩ => ext' <| LinearMap.ext fun _x => add_assoc _ _ _ end VAdd section Sub instance instSub : Sub (E →ₗ.[R] F) := ⟨fun f g => { domain := f.domain ⊓ g.domain toFun := f.toFun.comp (inclusion (inf_le_left : f.domain ⊓ g.domain ≤ _)) - g.toFun.comp (inclusion (inf_le_right : f.domain ⊓ g.domain ≤ _)) }⟩ theorem sub_domain (f g : E →ₗ.[R] F) : (f - g).domain = f.domain ⊓ g.domain := rfl theorem sub_apply (f g : E →ₗ.[R] F) (x : (f.domain ⊓ g.domain : Submodule R E)) : (f - g) x = f ⟨x, x.prop.1⟩ - g ⟨x, x.prop.2⟩ := rfl instance instSubtractionCommMonoid : SubtractionCommMonoid (E →ₗ.[R] F) where add_comm := add_comm sub_eq_add_neg f g := by ext x _ h · rfl simp [sub_apply, add_apply, neg_apply, ← sub_eq_add_neg, h] neg_neg := neg_neg neg_add_rev f g := by ext x _ h · simp [add_domain, sub_domain, neg_domain, And.comm] simp [sub_apply, add_apply, neg_apply, ← sub_eq_add_neg, h] neg_eq_of_add f g h' := by ext x hf hg · have : (0 : E →ₗ.[R] F).domain = ⊤ := zero_domain simp only [← h', add_domain, inf_eq_top_iff] at this rw [neg_domain, this.1, this.2] simp only [neg_domain, neg_apply, neg_eq_iff_add_eq_zero] rw [ext_iff] at h' rcases h' with ⟨hdom, h'⟩ rw [zero_domain] at hdom simp only [hdom, neg_domain, zero_domain, mem_top, zero_apply, forall_true_left] at h' apply h' zsmul := zsmulRec end Sub section variable {K : Type*} [DivisionRing K] [Module K E] [Module K F] /-- Extend a `LinearPMap` to `f.domain ⊔ K ∙ x`. -/ noncomputable def supSpanSingleton (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) : E →ₗ.[K] F := f.sup (mkSpanSingleton x y fun h₀ => hx <| h₀.symm ▸ f.domain.zero_mem) <| sup_h_of_disjoint _ _ <| by simpa [disjoint_span_singleton] using fun h ↦ False.elim <| hx h @[simp] theorem domain_supSpanSingleton (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) : (f.supSpanSingleton x y hx).domain = f.domain ⊔ K ∙ x := rfl @[simp] theorem supSpanSingleton_apply_mk (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) (x' : E) (hx' : x' ∈ f.domain) (c : K) : f.supSpanSingleton x y hx ⟨x' + c • x, mem_sup.2 ⟨x', hx', _, mem_span_singleton.2 ⟨c, rfl⟩, rfl⟩⟩ = f ⟨x', hx'⟩ + c • y := by unfold supSpanSingleton rw [sup_apply _ ⟨x', hx'⟩ ⟨c • x, _⟩, mkSpanSingleton'_apply] · exact mem_span_singleton.2 ⟨c, rfl⟩ · rfl end private theorem sSup_aux (c : Set (E →ₗ.[R] F)) (hc : DirectedOn (· ≤ ·) c) : ∃ f : ↥(sSup (domain '' c)) →ₗ[R] F, (⟨_, f⟩ : E →ₗ.[R] F) ∈ upperBounds c := by rcases c.eq_empty_or_nonempty with ceq | cne · subst c simp have hdir : DirectedOn (· ≤ ·) (domain '' c) := directedOn_image.2 (hc.mono @(domain_mono.monotone)) have P : ∀ x : ↥(sSup (domain '' c)), { p : c // (x : E) ∈ p.val.domain } := by rintro x apply Classical.indefiniteDescription have := (mem_sSup_of_directed (cne.image _) hdir).1 x.2 rwa [Set.exists_mem_image, ← bex_def, SetCoe.exists'] at this set f : ↥(sSup (domain '' c)) → F := fun x => (P x).val.val ⟨x, (P x).property⟩ have f_eq : ∀ (p : c) (x : ↥(sSup (domain '' c))) (y : p.1.1) (_hxy : (x : E) = y), f x = p.1 y := by intro p x y hxy rcases hc (P x).1.1 (P x).1.2 p.1 p.2 with ⟨q, _hqc, ⟨hxq1, hxq2⟩, ⟨hpq1, hpq2⟩⟩ exact (hxq2 (y := ⟨y, hpq1 y.2⟩) hxy).trans (hpq2 rfl).symm use { toFun := f, map_add' := ?_, map_smul' := ?_ }, ?_ · intro x y rcases hc (P x).1.1 (P x).1.2 (P y).1.1 (P y).1.2 with ⟨p, hpc, hpx, hpy⟩ set x' := inclusion hpx.1 ⟨x, (P x).2⟩ set y' := inclusion hpy.1 ⟨y, (P y).2⟩ rw [f_eq ⟨p, hpc⟩ x x' rfl, f_eq ⟨p, hpc⟩ y y' rfl, f_eq ⟨p, hpc⟩ (x + y) (x' + y') rfl, map_add] · intro c x simp only [RingHom.id_apply] rw [f_eq (P x).1 (c • x) (c • ⟨x, (P x).2⟩) rfl, ← map_smul] · intro p hpc refine ⟨le_sSup <| Set.mem_image_of_mem domain hpc, fun x y hxy => Eq.symm ?_⟩ exact f_eq ⟨p, hpc⟩ _ _ hxy.symm protected noncomputable def sSup (c : Set (E →ₗ.[R] F)) (hc : DirectedOn (· ≤ ·) c) : E →ₗ.[R] F := ⟨_, Classical.choose <| sSup_aux c hc⟩ protected theorem le_sSup {c : Set (E →ₗ.[R] F)} (hc : DirectedOn (· ≤ ·) c) {f : E →ₗ.[R] F} (hf : f ∈ c) : f ≤ LinearPMap.sSup c hc := Classical.choose_spec (sSup_aux c hc) hf protected theorem sSup_le {c : Set (E →ₗ.[R] F)} (hc : DirectedOn (· ≤ ·) c) {g : E →ₗ.[R] F} (hg : ∀ f ∈ c, f ≤ g) : LinearPMap.sSup c hc ≤ g := le_of_eqLocus_ge <| sSup_le fun _ ⟨f, hf, Eq⟩ => Eq ▸ have : f ≤ LinearPMap.sSup c hc ⊓ g := le_inf (LinearPMap.le_sSup _ hf) (hg f hf) this.1 protected theorem sSup_apply {c : Set (E →ₗ.[R] F)} (hc : DirectedOn (· ≤ ·) c) {l : E →ₗ.[R] F} (hl : l ∈ c) (x : l.domain) : (LinearPMap.sSup c hc) ⟨x, (LinearPMap.le_sSup hc hl).1 x.2⟩ = l x := by symm apply (Classical.choose_spec (sSup_aux c hc) hl).2 rfl end LinearPMap namespace LinearMap /-- Restrict a linear map to a submodule, reinterpreting the result as a `LinearPMap`. -/ def toPMap (f : E →ₗ[R] F) (p : Submodule R E) : E →ₗ.[R] F := ⟨p, f.comp p.subtype⟩ @[simp] theorem toPMap_apply (f : E →ₗ[R] F) (p : Submodule R E) (x : p) : f.toPMap p x = f x := rfl @[simp] theorem toPMap_domain (f : E →ₗ[R] F) (p : Submodule R E) : (f.toPMap p).domain = p := rfl /-- Compose a linear map with a `LinearPMap` -/ def compPMap (g : F →ₗ[R] G) (f : E →ₗ.[R] F) : E →ₗ.[R] G where domain := f.domain toFun := g.comp f.toFun @[simp] theorem compPMap_apply (g : F →ₗ[R] G) (f : E →ₗ.[R] F) (x) : g.compPMap f x = g (f x) := rfl end LinearMap namespace LinearPMap /-- Restrict codomain of a `LinearPMap` -/ def codRestrict (f : E →ₗ.[R] F) (p : Submodule R F) (H : ∀ x, f x ∈ p) : E →ₗ.[R] p where domain := f.domain toFun := f.toFun.codRestrict p H /-- Compose two `LinearPMap`s -/ def comp (g : F →ₗ.[R] G) (f : E →ₗ.[R] F) (H : ∀ x : f.domain, f x ∈ g.domain) : E →ₗ.[R] G := g.toFun.compPMap <| f.codRestrict _ H /-- `f.coprod g` is the partially defined linear map defined on `f.domain × g.domain`, and sending `p` to `f p.1 + g p.2`. -/ def coprod (f : E →ₗ.[R] G) (g : F →ₗ.[R] G) : E × F →ₗ.[R] G where domain := f.domain.prod g.domain toFun := -- Porting note: This is just -- `(f.comp (LinearPMap.fst f.domain g.domain) fun x => x.2.1).toFun +` -- ` (g.comp (LinearPMap.snd f.domain g.domain) fun x => x.2.2).toFun`, HAdd.hAdd (α := f.domain.prod g.domain →ₗ[R] G) (β := f.domain.prod g.domain →ₗ[R] G) (f.comp (LinearPMap.fst f.domain g.domain) fun x => x.2.1).toFun (g.comp (LinearPMap.snd f.domain g.domain) fun x => x.2.2).toFun @[simp] theorem coprod_apply (f : E →ₗ.[R] G) (g : F →ₗ.[R] G) (x) : f.coprod g x = f ⟨(x : E × F).1, x.2.1⟩ + g ⟨(x : E × F).2, x.2.2⟩ := rfl /-- Restrict a partially defined linear map to a submodule of `E` contained in `f.domain`. -/ def domRestrict (f : E →ₗ.[R] F) (S : Submodule R E) : E →ₗ.[R] F := ⟨S ⊓ f.domain, f.toFun.comp (Submodule.inclusion (by simp))⟩ @[simp] theorem domRestrict_domain (f : E →ₗ.[R] F) {S : Submodule R E} : (f.domRestrict S).domain = S ⊓ f.domain := rfl theorem domRestrict_apply {f : E →ₗ.[R] F} {S : Submodule R E} ⦃x : ↥(S ⊓ f.domain)⦄ ⦃y : f.domain⦄ (h : (x : E) = y) : f.domRestrict S x = f y := by have : Submodule.inclusion (by simp) x = y := by ext simp [h] rw [← this] exact LinearPMap.mk_apply _ _ _ theorem domRestrict_le {f : E →ₗ.[R] F} {S : Submodule R E} : f.domRestrict S ≤ f := ⟨by simp, fun _ _ hxy => domRestrict_apply hxy⟩ /-! ### Graph -/ section Graph /-- The graph of a `LinearPMap` viewed as a submodule on `E × F`. -/ def graph (f : E →ₗ.[R] F) : Submodule R (E × F) := f.toFun.graph.map (f.domain.subtype.prodMap (LinearMap.id : F →ₗ[R] F)) theorem mem_graph_iff' (f : E →ₗ.[R] F) {x : E × F} : x ∈ f.graph ↔ ∃ y : f.domain, (↑y, f y) = x := by simp [graph] @[simp] theorem mem_graph_iff (f : E →ₗ.[R] F) {x : E × F} : x ∈ f.graph ↔ ∃ y : f.domain, (↑y : E) = x.1 ∧ f y = x.2 := by cases x simp_rw [mem_graph_iff', Prod.mk_inj] /-- The tuple `(x, f x)` is contained in the graph of `f`. -/ theorem mem_graph (f : E →ₗ.[R] F) (x : domain f) : ((x : E), f x) ∈ f.graph := by simp theorem graph_map_fst_eq_domain (f : E →ₗ.[R] F) : f.graph.map (LinearMap.fst R E F) = f.domain := by ext x simp only [Submodule.mem_map, mem_graph_iff, Subtype.exists, exists_and_left, exists_eq_left, LinearMap.fst_apply, Prod.exists, exists_and_right, exists_eq_right] constructor <;> intro h · rcases h with ⟨x, hx, _⟩ exact hx · use f ⟨x, h⟩ simp only [h, exists_const] theorem graph_map_snd_eq_range (f : E →ₗ.[R] F) : f.graph.map (LinearMap.snd R E F) = LinearMap.range f.toFun := by ext; simp variable {M : Type*} [Monoid M] [DistribMulAction M F] [SMulCommClass R M F] (y : M) /-- The graph of `z • f` as a pushforward. -/ theorem smul_graph (f : E →ₗ.[R] F) (z : M) : (z • f).graph = f.graph.map ((LinearMap.id : E →ₗ[R] E).prodMap (z • (LinearMap.id : F →ₗ[R] F))) := by ext ⟨x_fst, x_snd⟩ constructor <;> intro h · rw [mem_graph_iff] at h rcases h with ⟨y, hy, h⟩ rw [LinearPMap.smul_apply] at h rw [Submodule.mem_map] simp only [mem_graph_iff, LinearMap.prodMap_apply, LinearMap.id_coe, id, LinearMap.smul_apply, Prod.mk_inj, Prod.exists, exists_exists_and_eq_and] use x_fst, y, hy rw [Submodule.mem_map] at h rcases h with ⟨x', hx', h⟩ cases x' simp only [LinearMap.prodMap_apply, LinearMap.id_coe, id, LinearMap.smul_apply, Prod.mk_inj] at h rw [mem_graph_iff] at hx' ⊢ rcases hx' with ⟨y, hy, hx'⟩ use y rw [← h.1, ← h.2] simp [hy, hx'] /-- The graph of `-f` as a pushforward. -/ theorem neg_graph (f : E →ₗ.[R] F) : (-f).graph = f.graph.map ((LinearMap.id : E →ₗ[R] E).prodMap (-(LinearMap.id : F →ₗ[R] F))) := by ext ⟨x_fst, x_snd⟩ constructor <;> intro h · rw [mem_graph_iff] at h rcases h with ⟨y, hy, h⟩ rw [LinearPMap.neg_apply] at h rw [Submodule.mem_map] simp only [mem_graph_iff, LinearMap.prodMap_apply, LinearMap.id_coe, id, LinearMap.neg_apply, Prod.mk_inj, Prod.exists, exists_exists_and_eq_and] use x_fst, y, hy rw [Submodule.mem_map] at h rcases h with ⟨x', hx', h⟩ cases x' simp only [LinearMap.prodMap_apply, LinearMap.id_coe, id, LinearMap.neg_apply, Prod.mk_inj] at h rw [mem_graph_iff] at hx' ⊢ rcases hx' with ⟨y, hy, hx'⟩ use y rw [← h.1, ← h.2] simp [hy, hx'] theorem mem_graph_snd_inj (f : E →ₗ.[R] F) {x y : E} {x' y' : F} (hx : (x, x') ∈ f.graph) (hy : (y, y') ∈ f.graph) (hxy : x = y) : x' = y' := by rw [mem_graph_iff] at hx hy rcases hx with ⟨x'', hx1, hx2⟩ rcases hy with ⟨y'', hy1, hy2⟩ simp only at hx1 hx2 hy1 hy2 rw [← hx1, ← hy1, SetLike.coe_eq_coe] at hxy rw [← hx2, ← hy2, hxy] theorem mem_graph_snd_inj' (f : E →ₗ.[R] F) {x y : E × F} (hx : x ∈ f.graph) (hy : y ∈ f.graph) (hxy : x.1 = y.1) : x.2 = y.2 := by cases x cases y exact f.mem_graph_snd_inj hx hy hxy /-- The property that `f 0 = 0` in terms of the graph. -/ theorem graph_fst_eq_zero_snd (f : E →ₗ.[R] F) {x : E} {x' : F} (h : (x, x') ∈ f.graph) (hx : x = 0) : x' = 0 := f.mem_graph_snd_inj h f.graph.zero_mem hx theorem mem_domain_iff {f : E →ₗ.[R] F} {x : E} : x ∈ f.domain ↔ ∃ y : F, (x, y) ∈ f.graph := by constructor <;> intro h · use f ⟨x, h⟩ exact f.mem_graph ⟨x, h⟩ obtain ⟨y, h⟩ := h rw [mem_graph_iff] at h obtain ⟨x', h⟩ := h simp only at h rw [← h.1] simp theorem mem_domain_of_mem_graph {f : E →ₗ.[R] F} {x : E} {y : F} (h : (x, y) ∈ f.graph) : x ∈ f.domain := by rw [mem_domain_iff] exact ⟨y, h⟩ theorem image_iff {f : E →ₗ.[R] F} {x : E} {y : F} (hx : x ∈ f.domain) : y = f ⟨x, hx⟩ ↔ (x, y) ∈ f.graph := by rw [mem_graph_iff] constructor <;> intro h · use ⟨x, hx⟩ simp [h] rcases h with ⟨⟨x', hx'⟩, ⟨h1, h2⟩⟩ simp only [Submodule.coe_mk] at h1 h2 simp only [← h2, h1] theorem mem_range_iff {f : E →ₗ.[R] F} {y : F} : y ∈ Set.range f ↔ ∃ x : E, (x, y) ∈ f.graph := by constructor <;> intro h · rw [Set.mem_range] at h rcases h with ⟨⟨x, hx⟩, h⟩ use x rw [← h] exact f.mem_graph ⟨x, hx⟩ obtain ⟨x, h⟩ := h rw [mem_graph_iff] at h obtain ⟨x, h⟩ := h rw [Set.mem_range] use x simp only at h rw [h.2] theorem mem_domain_iff_of_eq_graph {f g : E →ₗ.[R] F} (h : f.graph = g.graph) {x : E} : x ∈ f.domain ↔ x ∈ g.domain := by simp_rw [mem_domain_iff, h] theorem le_of_le_graph {f g : E →ₗ.[R] F} (h : f.graph ≤ g.graph) : f ≤ g := by constructor · intro x hx rw [mem_domain_iff] at hx ⊢ obtain ⟨y, hx⟩ := hx use y exact h hx rintro ⟨x, hx⟩ ⟨y, hy⟩ hxy rw [image_iff] refine h ?_ simp only [Submodule.coe_mk] at hxy rw [hxy] at hx rw [← image_iff hx] simp [hxy] theorem le_graph_of_le {f g : E →ₗ.[R] F} (h : f ≤ g) : f.graph ≤ g.graph := by intro x hx rw [mem_graph_iff] at hx ⊢ obtain ⟨y, hx⟩ := hx use ⟨y, h.1 y.2⟩ simp only [hx, Submodule.coe_mk, eq_self_iff_true, true_and] convert hx.2 using 1 refine (h.2 ?_).symm simp only [hx.1, Submodule.coe_mk] theorem le_graph_iff {f g : E →ₗ.[R] F} : f.graph ≤ g.graph ↔ f ≤ g := ⟨le_of_le_graph, le_graph_of_le⟩ theorem eq_of_eq_graph {f g : E →ₗ.[R] F} (h : f.graph = g.graph) : f = g := by apply dExt · ext exact mem_domain_iff_of_eq_graph h · apply (le_of_le_graph h.le).2 end Graph end LinearPMap namespace Submodule section SubmoduleToLinearPMap theorem existsUnique_from_graph {g : Submodule R (E × F)} (hg : ∀ {x : E × F} (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) {a : E} (ha : a ∈ g.map (LinearMap.fst R E F)) : ∃! b : F, (a, b) ∈ g := by refine existsUnique_of_exists_of_unique ?_ ?_ · convert ha simp intro y₁ y₂ hy₁ hy₂ have hy : ((0 : E), y₁ - y₂) ∈ g := by convert g.sub_mem hy₁ hy₂ exact (sub_self _).symm exact sub_eq_zero.mp (hg hy (by simp)) /-- Auxiliary definition to unfold the existential quantifier. -/ noncomputable def valFromGraph {g : Submodule R (E × F)} (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) {a : E} (ha : a ∈ g.map (LinearMap.fst R E F)) : F := (ExistsUnique.exists (existsUnique_from_graph @hg ha)).choose theorem valFromGraph_mem {g : Submodule R (E × F)} (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) {a : E} (ha : a ∈ g.map (LinearMap.fst R E F)) : (a, valFromGraph hg ha) ∈ g := (ExistsUnique.exists (existsUnique_from_graph @hg ha)).choose_spec /-- Define a `LinearMap` from its graph. Helper definition for `LinearPMap`. -/ noncomputable def toLinearPMapAux (g : Submodule R (E × F)) (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) : g.map (LinearMap.fst R E F) →ₗ[R] F where toFun := fun x => valFromGraph hg x.2 map_add' := fun v w => by have hadd := (g.map (LinearMap.fst R E F)).add_mem v.2 w.2 have hvw := valFromGraph_mem hg hadd have hvw' := g.add_mem (valFromGraph_mem hg v.2) (valFromGraph_mem hg w.2) rw [Prod.mk_add_mk] at hvw' exact (existsUnique_from_graph @hg hadd).unique hvw hvw' map_smul' := fun a v => by have hsmul := (g.map (LinearMap.fst R E F)).smul_mem a v.2 have hav := valFromGraph_mem hg hsmul have hav' := g.smul_mem a (valFromGraph_mem hg v.2) rw [Prod.smul_mk] at hav' exact (existsUnique_from_graph @hg hsmul).unique hav hav' open scoped Classical in /-- Define a `LinearPMap` from its graph. In the case that the submodule is not a graph of a `LinearPMap` then the underlying linear map is just the zero map. -/ noncomputable def toLinearPMap (g : Submodule R (E × F)) : E →ₗ.[R] F where domain := g.map (LinearMap.fst R E F) toFun := if hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0 then g.toLinearPMapAux hg else 0 theorem toLinearPMap_domain (g : Submodule R (E × F)) : g.toLinearPMap.domain = g.map (LinearMap.fst R E F) := rfl theorem toLinearPMap_apply_aux {g : Submodule R (E × F)} (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) (x : g.map (LinearMap.fst R E F)) : g.toLinearPMap x = valFromGraph hg x.2 := by classical change (if hg : _ then g.toLinearPMapAux hg else 0) x = _ rw [dif_pos] · rfl · exact hg theorem mem_graph_toLinearPMap {g : Submodule R (E × F)} (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) (x : g.map (LinearMap.fst R E F)) : (x.val, g.toLinearPMap x) ∈ g := by rw [toLinearPMap_apply_aux hg] exact valFromGraph_mem hg x.2 @[simp] theorem toLinearPMap_graph_eq (g : Submodule R (E × F)) (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) : g.toLinearPMap.graph = g := by ext ⟨x_fst, x_snd⟩ constructor <;> intro hx · rw [LinearPMap.mem_graph_iff] at hx rcases hx with ⟨y, hx1, hx2⟩ convert g.mem_graph_toLinearPMap hg y using 1 exact Prod.ext hx1.symm hx2.symm rw [LinearPMap.mem_graph_iff] have hx_fst : x_fst ∈ g.map (LinearMap.fst R E F) := by simp only [mem_map, LinearMap.fst_apply, Prod.exists, exists_and_right, exists_eq_right] exact ⟨x_snd, hx⟩ refine ⟨⟨x_fst, hx_fst⟩, Subtype.coe_mk x_fst hx_fst, ?_⟩ rw [toLinearPMap_apply_aux hg] exact (existsUnique_from_graph @hg hx_fst).unique (valFromGraph_mem hg hx_fst) hx theorem toLinearPMap_range (g : Submodule R (E × F)) (hg : ∀ (x : E × F) (_hx : x ∈ g) (_hx' : x.fst = 0), x.snd = 0) : LinearMap.range g.toLinearPMap.toFun = g.map (LinearMap.snd R E F) := by rwa [← LinearPMap.graph_map_snd_eq_range, toLinearPMap_graph_eq] end SubmoduleToLinearPMap end Submodule namespace LinearPMap section inverse /-- The inverse of a `LinearPMap`. -/ noncomputable def inverse (f : E →ₗ.[R] F) : F →ₗ.[R] E := (f.graph.map (LinearEquiv.prodComm R E F)).toLinearPMap variable {f : E →ₗ.[R] F}
theorem inverse_domain : (inverse f).domain = LinearMap.range f.toFun := by rw [inverse, Submodule.toLinearPMap_domain, ← graph_map_snd_eq_range, ← LinearEquiv.fst_comp_prodComm, Submodule.map_comp] rfl variable (hf : LinearMap.ker f.toFun = ⊥) include hf /-- The graph of the inverse generates a `LinearPMap`. -/ theorem mem_inverse_graph_snd_eq_zero (x : F × E) (hv : x ∈ (graph f).map (LinearEquiv.prodComm R E F))
Mathlib/LinearAlgebra/LinearPMap.lean
967
977
/- 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
1,912
1,916
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import Mathlib.MeasureTheory.Integral.Bochner.ContinuousLinearMap import Mathlib.MeasureTheory.Measure.HasOuterApproxClosed import Mathlib.MeasureTheory.Measure.Prod import Mathlib.Topology.Algebra.Module.WeakDual /-! # Finite measures This file defines the type of finite measures on a given measurable space. When the underlying space has a topology and the measurable space structure (sigma algebra) is finer than the Borel sigma algebra, then the type of finite measures is equipped with the topology of weak convergence of measures. The topology of weak convergence is the coarsest topology w.r.t. which for every bounded continuous `ℝ≥0`-valued function `f`, the integration of `f` against the measure is continuous. ## Main definitions The main definitions are * `MeasureTheory.FiniteMeasure Ω`: The type of finite measures on `Ω` with the topology of weak convergence of measures. * `MeasureTheory.FiniteMeasure.toWeakDualBCNN : FiniteMeasure Ω → (WeakDual ℝ≥0 (Ω →ᵇ ℝ≥0))`: Interpret a finite measure as a continuous linear functional on the space of bounded continuous nonnegative functions on `Ω`. This is used for the definition of the topology of weak convergence. * `MeasureTheory.FiniteMeasure.map`: The push-forward `f* μ` of a finite measure `μ` on `Ω` along a measurable function `f : Ω → Ω'`. * `MeasureTheory.FiniteMeasure.mapCLM`: The push-forward along a given continuous `f : Ω → Ω'` as a continuous linear map `f* : FiniteMeasure Ω →L[ℝ≥0] FiniteMeasure Ω'`. ## Main results * Finite measures `μ` on `Ω` give rise to continuous linear functionals on the space of bounded continuous nonnegative functions on `Ω` via integration: `MeasureTheory.FiniteMeasure.toWeakDualBCNN : FiniteMeasure Ω → (WeakDual ℝ≥0 (Ω →ᵇ ℝ≥0))` * `MeasureTheory.FiniteMeasure.tendsto_iff_forall_integral_tendsto`: Convergence of finite measures is characterized by the convergence of integrals of all bounded continuous functions. This shows that the chosen definition of topology coincides with the common textbook definition of weak convergence of measures. A similar characterization by the convergence of integrals (in the `MeasureTheory.lintegral` sense) of all bounded continuous nonnegative functions is `MeasureTheory.FiniteMeasure.tendsto_iff_forall_lintegral_tendsto`. * `MeasureTheory.FiniteMeasure.continuous_map`: For a continuous function `f : Ω → Ω'`, the push-forward of finite measures `f* : FiniteMeasure Ω → FiniteMeasure Ω'` is continuous. * `MeasureTheory.FiniteMeasure.t2Space`: The topology of weak convergence of finite Borel measures is Hausdorff on spaces where indicators of closed sets have continuous decreasing approximating sequences (in particular on any pseudo-metrizable spaces). ## Implementation notes The topology of weak convergence of finite Borel measures is defined using a mapping from `MeasureTheory.FiniteMeasure Ω` to `WeakDual ℝ≥0 (Ω →ᵇ ℝ≥0)`, inheriting the topology from the latter. The implementation of `MeasureTheory.FiniteMeasure Ω` and is directly as a subtype of `MeasureTheory.Measure Ω`, and the coercion to a function is the composition `ENNReal.toNNReal` and the coercion to function of `MeasureTheory.Measure Ω`. Another alternative would have been to use a bijection with `MeasureTheory.VectorMeasure Ω ℝ≥0` as an intermediate step. Some considerations: * Potential advantages of using the `NNReal`-valued vector measure alternative: * The coercion to function would avoid need to compose with `ENNReal.toNNReal`, the `NNReal`-valued API could be more directly available. * Potential drawbacks of the vector measure alternative: * The coercion to function would lose monotonicity, as non-measurable sets would be defined to have measure 0. * No integration theory directly. E.g., the topology definition requires `MeasureTheory.lintegral` w.r.t. a coercion to `MeasureTheory.Measure Ω` in any case. ## References * [Billingsley, *Convergence of probability measures*][billingsley1999] ## Tags weak convergence of measures, finite measure -/ noncomputable section open BoundedContinuousFunction Filter MeasureTheory Set Topology open scoped ENNReal NNReal namespace MeasureTheory namespace FiniteMeasure section FiniteMeasure /-! ### Finite measures In this section we define the `Type` of `MeasureTheory.FiniteMeasure Ω`, when `Ω` is a measurable space. Finite measures on `Ω` are a module over `ℝ≥0`. If `Ω` is moreover a topological space and the sigma algebra on `Ω` is finer than the Borel sigma algebra (i.e. `[OpensMeasurableSpace Ω]`), then `MeasureTheory.FiniteMeasure Ω` is equipped with the topology of weak convergence of measures. This is implemented by defining a pairing of finite measures `μ` on `Ω` with continuous bounded nonnegative functions `f : Ω →ᵇ ℝ≥0` via integration, and using the associated weak topology (essentially the weak-star topology on the dual of `Ω →ᵇ ℝ≥0`). -/ variable {Ω : Type*} [MeasurableSpace Ω] /-- Finite measures are defined as the subtype of measures that have the property of being finite measures (i.e., their total mass is finite). -/ def _root_.MeasureTheory.FiniteMeasure (Ω : Type*) [MeasurableSpace Ω] : Type _ := { μ : Measure Ω // IsFiniteMeasure μ } /-- Coercion from `MeasureTheory.FiniteMeasure Ω` to `MeasureTheory.Measure Ω`. -/ @[coe] def toMeasure : FiniteMeasure Ω → Measure Ω := Subtype.val /-- A finite measure can be interpreted as a measure. -/ instance instCoe : Coe (FiniteMeasure Ω) (MeasureTheory.Measure Ω) := { coe := toMeasure } instance isFiniteMeasure (μ : FiniteMeasure Ω) : IsFiniteMeasure (μ : Measure Ω) := μ.prop @[simp] theorem val_eq_toMeasure (ν : FiniteMeasure Ω) : ν.val = (ν : Measure Ω) := rfl theorem toMeasure_injective : Function.Injective ((↑) : FiniteMeasure Ω → Measure Ω) := Subtype.coe_injective instance instFunLike : FunLike (FiniteMeasure Ω) (Set Ω) ℝ≥0 where coe μ s := ((μ : Measure Ω) s).toNNReal coe_injective' μ ν h := toMeasure_injective <| Measure.ext fun s _ ↦ by simpa [ENNReal.toNNReal_eq_toNNReal_iff, measure_ne_top] using congr_fun h s lemma coeFn_def (μ : FiniteMeasure Ω) : μ = fun s ↦ ((μ : Measure Ω) s).toNNReal := rfl lemma coeFn_mk (μ : Measure Ω) (hμ) : DFunLike.coe (F := FiniteMeasure Ω) ⟨μ, hμ⟩ = fun s ↦ (μ s).toNNReal := rfl @[simp, norm_cast] lemma mk_apply (μ : Measure Ω) (hμ) (s : Set Ω) : DFunLike.coe (F := FiniteMeasure Ω) ⟨μ, hμ⟩ s = (μ s).toNNReal := rfl @[simp] theorem ennreal_coeFn_eq_coeFn_toMeasure (ν : FiniteMeasure Ω) (s : Set Ω) : (ν s : ℝ≥0∞) = (ν : Measure Ω) s := ENNReal.coe_toNNReal (measure_lt_top (↑ν) s).ne @[simp] theorem null_iff_toMeasure_null (ν : FiniteMeasure Ω) (s : Set Ω) : ν s = 0 ↔ (ν : Measure Ω) s = 0 := ⟨fun h ↦ by rw [← ennreal_coeFn_eq_coeFn_toMeasure, h, ENNReal.coe_zero], fun h ↦ congrArg ENNReal.toNNReal h⟩ theorem apply_mono (μ : FiniteMeasure Ω) {s₁ s₂ : Set Ω} (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := ENNReal.toNNReal_mono (measure_ne_top _ s₂) ((μ : Measure Ω).mono h) /-- Continuity from below: the measure of the union of a sequence of (not necessarily measurable) sets is the limit of the measures of the partial unions. -/ protected lemma tendsto_measure_iUnion_accumulate {ι : Type*} [Preorder ι] [IsCountablyGenerated (atTop : Filter ι)] {μ : FiniteMeasure Ω} {f : ι → Set Ω} : Tendsto (fun i ↦ μ (Accumulate f i)) atTop (𝓝 (μ (⋃ i, f i))) := by simpa [← ennreal_coeFn_eq_coeFn_toMeasure] using tendsto_measure_iUnion_accumulate (μ := μ.toMeasure) (ι := ι) /-- The (total) mass of a finite measure `μ` is `μ univ`, i.e., the cast to `NNReal` of `(μ : measure Ω) univ`. -/
def mass (μ : FiniteMeasure Ω) : ℝ≥0 := μ univ @[simp] theorem apply_le_mass (μ : FiniteMeasure Ω) (s : Set Ω) : μ s ≤ μ.mass := by simpa using apply_mono μ (subset_univ s)
Mathlib/MeasureTheory/Measure/FiniteMeasure.lean
168
171
/- 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, Floris van Doorn, Heather Macbeth -/ import Mathlib.Topology.FiberBundle.Trivialization import Mathlib.Topology.Order.LeftRightNhds /-! # Fiber bundles Mathematically, a (topological) fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the fibers are all homeomorphic to `F`, such that the local situation around each point is a direct product. In our formalism, a fiber bundle is by definition the type `Bundle.TotalSpace F E` where `E : B → Type*` is a function associating to `x : B` the fiber over `x`. This type `Bundle.TotalSpace F E` is a type of pairs `⟨proj : B, snd : E proj⟩`. To have a fiber bundle structure on `Bundle.TotalSpace F E`, one should additionally have the following data: * `F` should be a topological space; * There should be a topology on `Bundle.TotalSpace F E`, for which the projection to `B` is a fiber bundle with fiber `F` (in particular, each fiber `E x` is homeomorphic to `F`); * For each `x`, the fiber `E x` should be a topological space, and the injection from `E x` to `Bundle.TotalSpace F E` should be an embedding; * There should be a distinguished set of bundle trivializations, the "trivialization atlas" * There should be a choice of bundle trivialization at each point, which belongs to this atlas. If all these conditions are satisfied, we register the typeclass `FiberBundle F E`. It is in general nontrivial to construct a fiber bundle. A way is to start from the knowledge of how changes of local trivializations act on the fiber. From this, one can construct the total space of the bundle and its topology by a suitable gluing construction. The main content of this file is an implementation of this construction: starting from an object of type `FiberBundleCore` registering the trivialization changes, one gets the corresponding fiber bundle and projection. Similarly we implement the object `FiberPrebundle` which allows to define a topological fiber bundle from trivializations given as partial equivalences with minimum additional properties. ## Main definitions ### Basic definitions * `FiberBundle F E` : Structure saying that `E : B → Type*` is a fiber bundle with fiber `F`. ### Construction of a bundle from trivializations * `Bundle.TotalSpace F E` is the type of pairs `(proj : B, snd : E proj)`. We can use the extra argument `F` to construct topology on the total space. * `FiberBundleCore ι B F` : structure registering how changes of coordinates act on the fiber `F` above open subsets of `B`, where local trivializations are indexed by `ι`. Let `Z : FiberBundleCore ι B F`. Then we define * `Z.Fiber x` : the fiber above `x`, homeomorphic to `F` (and defeq to `F` as a type). * `Z.TotalSpace` : the total space of `Z`, defined as `Bundle.TotalSpace F Z.Fiber` with a custom topology. * `Z.proj` : projection from `Z.TotalSpace` to `B`. It is continuous. * `Z.localTriv i` : for `i : ι`, bundle trivialization above the set `Z.baseSet i`, which is an open set in `B`. * `FiberPrebundle F E` : structure registering a cover of prebundle trivializations and requiring that the relative transition maps are partial homeomorphisms. * `FiberPrebundle.totalSpaceTopology a` : natural topology of the total space, making the prebundle into a bundle. ## Implementation notes ### Data vs mixins For both fiber and vector bundles, one faces a choice: should the definition state the *existence* of local trivializations (a propositional typeclass), or specify a fixed atlas of trivializations (a typeclass containing data)? In their initial mathlib implementations, both fiber and vector bundles were defined propositionally. For vector bundles, this turns out to be mathematically wrong: in infinite dimension, the transition function between two trivializations is not automatically continuous as a map from the base `B` to the endomorphisms `F →L[R] F` of the fiber (considered with the operator-norm topology), and so the definition needs to be modified by restricting consideration to a family of trivializations (constituting the data) which are all mutually-compatible in this sense. The PRs https://github.com/leanprover-community/mathlib4/pull/13052 and https://github.com/leanprover-community/mathlib4/pull/13175 implemented this change. There is still the choice about whether to hold this data at the level of fiber bundles or of vector bundles. As of PR https://github.com/leanprover-community/mathlib4/pull/17505, the data is all held in `FiberBundle`, with `VectorBundle` a (propositional) mixin stating fiberwise-linearity. This allows bundles to carry instances of typeclasses in which the scalar field, `R`, does not appear as a parameter. Notably, we would like a vector bundle over `R` with fiber `F` over base `B` to be a `ChartedSpace (B × F)`, with the trivializations providing the charts. This would be a dangerous instance for typeclass inference, because `R` does not appear as a parameter in `ChartedSpace (B × F)`. But if the data of the trivializations is held in `FiberBundle`, then a fiber bundle with fiber `F` over base `B` can be a `ChartedSpace (B × F)`, and this is safe for typeclass inference. We expect that this choice of definition will also streamline constructions of fiber bundles with similar underlying structure (e.g., the same bundle being both a real and complex vector bundle). ### Core construction A fiber bundle with fiber `F` over a base `B` is a family of spaces isomorphic to `F`, indexed by `B`, which is locally trivial in the following sense: there is a covering of `B` by open sets such that, on each such open set `s`, the bundle is isomorphic to `s × F`. To construct a fiber bundle formally, the main data is what happens when one changes trivializations from `s × F` to `s' × F` on `s ∩ s'`: one should get a family of homeomorphisms of `F`, depending continuously on the base point, satisfying basic compatibility conditions (cocycle property). Useful classes of bundles can then be specified by requiring that these homeomorphisms of `F` belong to some subgroup, preserving some structure (the "structure group of the bundle"): then these structures are inherited by the fibers of the bundle. Given such trivialization change data (encoded below in a structure called `FiberBundleCore`), one can construct the fiber bundle. The intrinsic canonical mathematical construction is the following. The fiber above `x` is the disjoint union of `F` over all trivializations, modulo the gluing identifications: one gets a fiber which is isomorphic to `F`, but non-canonically (each choice of one of the trivializations around `x` gives such an isomorphism). Given a trivialization over a set `s`, one gets an isomorphism between `s × F` and `proj^{-1} s`, by using the identification corresponding to this trivialization. One chooses the topology on the bundle that makes all of these into homeomorphisms. For the practical implementation, it turns out to be more convenient to avoid completely the gluing and quotienting construction above, and to declare above each `x` that the fiber is `F`, but thinking that it corresponds to the `F` coming from the choice of one trivialization around `x`. This has several practical advantages: * without any work, one gets a topological space structure on the fiber. And if `F` has more structure it is inherited for free by the fiber. * In the case of the tangent bundle of manifolds, this implies that on vector spaces the derivative (from `F` to `F`) and the manifold derivative (from `TangentSpace I x` to `TangentSpace I' (f x)`) are equal. A drawback is that some silly constructions will typecheck: in the case of the tangent bundle, one can add two vectors in different tangent spaces (as they both are elements of `F` from the point of view of Lean). To solve this, one could mark the tangent space as irreducible, but then one would lose the identification of the tangent space to `F` with `F`. There is however a big advantage of this situation: even if Lean can not check that two basepoints are defeq, it will accept the fact that the tangent spaces are the same. For instance, if two maps `f` and `g` are locally inverse to each other, one can express that the composition of their derivatives is the identity of `TangentSpace I x`. One could fear issues as this composition goes from `TangentSpace I x` to `TangentSpace I (g (f x))` (which should be the same, but should not be obvious to Lean as it does not know that `g (f x) = x`). As these types are the same to Lean (equal to `F`), there are in fact no dependent type difficulties here! For this construction of a fiber bundle from a `FiberBundleCore`, we should thus choose for each `x` one specific trivialization around it. We include this choice in the definition of the `FiberBundleCore`, as it makes some constructions more functorial and it is a nice way to say that the trivializations cover the whole space `B`. With this definition, the type of the fiber bundle space constructed from the core data is `Bundle.TotalSpace F (fun b : B ↦ F)`, but the topology is not the product one, in general. We also take the indexing type (indexing all the trivializations) as a parameter to the fiber bundle core: it could always be taken as a subtype of all the maps from open subsets of `B` to continuous maps of `F`, but in practice it will sometimes be something else. For instance, on a manifold, one will use the set of charts as a good parameterization for the trivializations of the tangent bundle. Or for the pullback of a `FiberBundleCore`, the indexing type will be the same as for the initial bundle. ## Tags Fiber bundle, topological bundle, structure group -/ variable {ι B F X : Type*} [TopologicalSpace X] open TopologicalSpace Filter Set Bundle Topology /-! ### General definition of fiber bundles -/ section FiberBundle variable (F) variable [TopologicalSpace B] [TopologicalSpace F] (E : B → Type*) [TopologicalSpace (TotalSpace F E)] [∀ b, TopologicalSpace (E b)] /-- A (topological) fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the fibers are all homeomorphic to `F`, such that the local situation around each point is a direct product. -/ class FiberBundle where totalSpaceMk_isInducing' : ∀ b : B, IsInducing (@TotalSpace.mk B F E b) trivializationAtlas' : Set (Trivialization F (π F E)) trivializationAt' : B → Trivialization F (π F E) mem_baseSet_trivializationAt' : ∀ b : B, b ∈ (trivializationAt' b).baseSet trivialization_mem_atlas' : ∀ b : B, trivializationAt' b ∈ trivializationAtlas' namespace FiberBundle variable [FiberBundle F E] (b : B) theorem totalSpaceMk_isInducing : IsInducing (@TotalSpace.mk B F E b) := totalSpaceMk_isInducing' b @[deprecated (since := "2024-10-28")] alias totalSpaceMk_inducing := totalSpaceMk_isInducing /-- Atlas of a fiber bundle. -/ abbrev trivializationAtlas : Set (Trivialization F (π F E)) := trivializationAtlas' /-- Trivialization of a fiber bundle at a point. -/ abbrev trivializationAt : Trivialization F (π F E) := trivializationAt' b theorem mem_baseSet_trivializationAt : b ∈ (trivializationAt F E b).baseSet := mem_baseSet_trivializationAt' b theorem trivialization_mem_atlas : trivializationAt F E b ∈ trivializationAtlas F E := trivialization_mem_atlas' b end FiberBundle export FiberBundle (totalSpaceMk_isInducing trivializationAtlas trivializationAt mem_baseSet_trivializationAt trivialization_mem_atlas) variable {F} variable {E} /-- Given a type `E` equipped with a fiber bundle structure, this is a `Prop` typeclass for trivializations of `E`, expressing that a trivialization is in the designated atlas for the bundle. This is needed because lemmas about the linearity of trivializations or the continuity (as functions to `F →L[R] F`, where `F` is the model fiber) of the transition functions are only expected to hold for trivializations in the designated atlas. -/ @[mk_iff] class MemTrivializationAtlas [FiberBundle F E] (e : Trivialization F (π F E)) : Prop where out : e ∈ trivializationAtlas F E instance [FiberBundle F E] (b : B) : MemTrivializationAtlas (trivializationAt F E b) where out := trivialization_mem_atlas F E b namespace FiberBundle variable (F) variable [FiberBundle F E] theorem map_proj_nhds (x : TotalSpace F E) : map (π F E) (𝓝 x) = 𝓝 x.proj := (trivializationAt F E x.proj).map_proj_nhds <| (trivializationAt F E x.proj).mem_source.2 <| mem_baseSet_trivializationAt F E x.proj variable (E) /-- The projection from a fiber bundle to its base is continuous. -/ @[continuity] theorem continuous_proj : Continuous (π F E) := continuous_iff_continuousAt.2 fun x => (map_proj_nhds F x).le /-- The projection from a fiber bundle to its base is an open map. -/ theorem isOpenMap_proj : IsOpenMap (π F E) := IsOpenMap.of_nhds_le fun x => (map_proj_nhds F x).ge /-- The projection from a fiber bundle with a nonempty fiber to its base is a surjective map. -/ theorem surjective_proj [Nonempty F] : Function.Surjective (π F E) := fun b => let ⟨p, _, hpb⟩ := (trivializationAt F E b).proj_surjOn_baseSet (mem_baseSet_trivializationAt F E b) ⟨p, hpb⟩ /-- The projection from a fiber bundle with a nonempty fiber to its base is a quotient map. -/ theorem isQuotientMap_proj [Nonempty F] : IsQuotientMap (π F E) := (isOpenMap_proj F E).isQuotientMap (continuous_proj F E) (surjective_proj F E) @[deprecated (since := "2024-10-22")] alias quotientMap_proj := isQuotientMap_proj theorem continuous_totalSpaceMk (x : B) : Continuous (@TotalSpace.mk B F E x) := (totalSpaceMk_isInducing F E x).continuous theorem totalSpaceMk_isEmbedding (x : B) : IsEmbedding (@TotalSpace.mk B F E x) := ⟨totalSpaceMk_isInducing F E x, TotalSpace.mk_injective x⟩ @[deprecated (since := "2024-10-26")] alias totalSpaceMk_embedding := totalSpaceMk_isEmbedding
theorem totalSpaceMk_isClosedEmbedding [T1Space B] (x : B) : IsClosedEmbedding (@TotalSpace.mk B F E x) := ⟨totalSpaceMk_isEmbedding F E x, by rw [TotalSpace.range_mk] exact isClosed_singleton.preimage <| continuous_proj F E⟩
Mathlib/Topology/FiberBundle/Basic.lean
272
276
/- 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 -/ import Mathlib.Data.Set.Countable import Mathlib.Order.ConditionallyCompleteLattice.Basic import Mathlib.Tactic.FunProp.Attr import Mathlib.Tactic.Measurability /-! # Measurable spaces and measurable functions This file defines measurable spaces and measurable functions. A measurable space is a set equipped with a σ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. σ-algebras on a fixed set `α` form a complete lattice. Here we order σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any collection of subsets of `α` generates a smallest σ-algebra which contains all of them. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, σ-algebra, measurable function -/ assert_not_exists Covariant MonoidWithZero open Set Encodable Function Equiv variable {α β γ δ δ' : Type*} {ι : Sort*} {s t u : Set α} /-- A measurable space is a space equipped with a σ-algebra. -/ @[class] structure MeasurableSpace (α : Type*) where /-- Predicate saying that a given set is measurable. Use `MeasurableSet` in the root namespace instead. -/ MeasurableSet' : Set α → Prop /-- The empty set is a measurable set. Use `MeasurableSet.empty` instead. -/ measurableSet_empty : MeasurableSet' ∅ /-- The complement of a measurable set is a measurable set. Use `MeasurableSet.compl` instead. -/ measurableSet_compl : ∀ s, MeasurableSet' s → MeasurableSet' sᶜ /-- The union of a sequence of measurable sets is a measurable set. Use a more general `MeasurableSet.iUnion` instead. -/ measurableSet_iUnion : ∀ f : ℕ → Set α, (∀ i, MeasurableSet' (f i)) → MeasurableSet' (⋃ i, f i) instance [h : MeasurableSpace α] : MeasurableSpace αᵒᵈ := h /-- `MeasurableSet s` means that `s` is measurable (in the ambient measure space on `α`) -/ def MeasurableSet [MeasurableSpace α] (s : Set α) : Prop := ‹MeasurableSpace α›.MeasurableSet' s /-- Notation for `MeasurableSet` with respect to a non-standard σ-algebra. -/ scoped[MeasureTheory] notation "MeasurableSet[" m "]" => @MeasurableSet _ m open MeasureTheory section open scoped symmDiff @[simp, measurability] theorem MeasurableSet.empty [MeasurableSpace α] : MeasurableSet (∅ : Set α) := MeasurableSpace.measurableSet_empty _ variable {m : MeasurableSpace α} @[measurability] protected theorem MeasurableSet.compl : MeasurableSet s → MeasurableSet sᶜ := MeasurableSpace.measurableSet_compl _ s protected theorem MeasurableSet.of_compl (h : MeasurableSet sᶜ) : MeasurableSet s := compl_compl s ▸ h.compl @[simp] theorem MeasurableSet.compl_iff : MeasurableSet sᶜ ↔ MeasurableSet s := ⟨.of_compl, .compl⟩ @[simp, measurability] protected theorem MeasurableSet.univ : MeasurableSet (univ : Set α) := .of_compl <| by simp @[nontriviality, measurability] theorem Subsingleton.measurableSet [Subsingleton α] {s : Set α} : MeasurableSet s := Subsingleton.set_cases MeasurableSet.empty MeasurableSet.univ s theorem MeasurableSet.congr {s t : Set α} (hs : MeasurableSet s) (h : s = t) : MeasurableSet t := by rwa [← h] @[measurability] protected theorem MeasurableSet.iUnion [Countable ι] ⦃f : ι → Set α⦄ (h : ∀ b, MeasurableSet (f b)) : MeasurableSet (⋃ b, f b) := by cases isEmpty_or_nonempty ι · simp · rcases exists_surjective_nat ι with ⟨e, he⟩ rw [← iUnion_congr_of_surjective _ he (fun _ => rfl)] exact m.measurableSet_iUnion _ fun _ => h _ protected theorem MeasurableSet.biUnion {f : β → Set α} {s : Set β} (hs : s.Countable) (h : ∀ b ∈ s, MeasurableSet (f b)) : MeasurableSet (⋃ b ∈ s, f b) := by rw [biUnion_eq_iUnion] have := hs.to_subtype exact MeasurableSet.iUnion (by simpa using h) theorem Set.Finite.measurableSet_biUnion {f : β → Set α} {s : Set β} (hs : s.Finite) (h : ∀ b ∈ s, MeasurableSet (f b)) : MeasurableSet (⋃ b ∈ s, f b) := .biUnion hs.countable h theorem Finset.measurableSet_biUnion {f : β → Set α} (s : Finset β) (h : ∀ b ∈ s, MeasurableSet (f b)) : MeasurableSet (⋃ b ∈ s, f b) := s.finite_toSet.measurableSet_biUnion h protected theorem MeasurableSet.sUnion {s : Set (Set α)} (hs : s.Countable) (h : ∀ t ∈ s, MeasurableSet t) : MeasurableSet (⋃₀ s) := by rw [sUnion_eq_biUnion] exact .biUnion hs h theorem Set.Finite.measurableSet_sUnion {s : Set (Set α)} (hs : s.Finite) (h : ∀ t ∈ s, MeasurableSet t) : MeasurableSet (⋃₀ s) := MeasurableSet.sUnion hs.countable h @[measurability] theorem MeasurableSet.iInter [Countable ι] {f : ι → Set α} (h : ∀ b, MeasurableSet (f b)) : MeasurableSet (⋂ b, f b) := .of_compl <| by rw [compl_iInter]; exact .iUnion fun b => (h b).compl theorem MeasurableSet.biInter {f : β → Set α} {s : Set β} (hs : s.Countable) (h : ∀ b ∈ s, MeasurableSet (f b)) : MeasurableSet (⋂ b ∈ s, f b) := .of_compl <| by rw [compl_iInter₂]; exact .biUnion hs fun b hb => (h b hb).compl theorem Set.Finite.measurableSet_biInter {f : β → Set α} {s : Set β} (hs : s.Finite) (h : ∀ b ∈ s, MeasurableSet (f b)) : MeasurableSet (⋂ b ∈ s, f b) := .biInter hs.countable h theorem Finset.measurableSet_biInter {f : β → Set α} (s : Finset β) (h : ∀ b ∈ s, MeasurableSet (f b)) : MeasurableSet (⋂ b ∈ s, f b) := s.finite_toSet.measurableSet_biInter h theorem MeasurableSet.sInter {s : Set (Set α)} (hs : s.Countable) (h : ∀ t ∈ s, MeasurableSet t) : MeasurableSet (⋂₀ s) := by rw [sInter_eq_biInter] exact MeasurableSet.biInter hs h theorem Set.Finite.measurableSet_sInter {s : Set (Set α)} (hs : s.Finite) (h : ∀ t ∈ s, MeasurableSet t) : MeasurableSet (⋂₀ s) := MeasurableSet.sInter hs.countable h @[simp, measurability] protected theorem MeasurableSet.union {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (s₁ ∪ s₂) := by rw [union_eq_iUnion] exact .iUnion (Bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp, measurability] protected theorem MeasurableSet.inter {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (s₁ ∩ s₂) := by rw [inter_eq_compl_compl_union_compl] exact (h₁.compl.union h₂.compl).compl @[simp, measurability] protected theorem MeasurableSet.diff {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (s₁ \ s₂) := h₁.inter h₂.compl @[simp, measurability] protected lemma MeasurableSet.himp {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (s₁ ⇨ s₂) := by rw [himp_eq]; exact h₂.union h₁.compl @[simp, measurability] protected theorem MeasurableSet.symmDiff {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (s₁ ∆ s₂) := (h₁.diff h₂).union (h₂.diff h₁) @[simp, measurability] protected lemma MeasurableSet.bihimp {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (s₁ ⇔ s₂) := (h₂.himp h₁).inter (h₁.himp h₂) @[simp, measurability] protected theorem MeasurableSet.ite {t s₁ s₂ : Set α} (ht : MeasurableSet t) (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) : MeasurableSet (t.ite s₁ s₂) := (h₁.inter ht).union (h₂.diff ht) open Classical in theorem MeasurableSet.ite' {s t : Set α} {p : Prop} (hs : p → MeasurableSet s) (ht : ¬p → MeasurableSet t) : MeasurableSet (ite p s t) := by split_ifs with h exacts [hs h, ht h] @[simp, measurability] protected theorem MeasurableSet.cond {s₁ s₂ : Set α} (h₁ : MeasurableSet s₁) (h₂ : MeasurableSet s₂) {i : Bool} : MeasurableSet (cond i s₁ s₂) := by cases i exacts [h₂, h₁] protected theorem MeasurableSet.const (p : Prop) : MeasurableSet { _a : α | p } := by by_cases p <;> simp [*] /-- Every set has a measurable superset. Declare this as local instance as needed. -/ theorem nonempty_measurable_superset (s : Set α) : Nonempty { t // s ⊆ t ∧ MeasurableSet t } := ⟨⟨univ, subset_univ s, MeasurableSet.univ⟩⟩ end theorem MeasurableSpace.measurableSet_injective : Injective (@MeasurableSet α) | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩, _ => by congr @[ext] theorem MeasurableSpace.ext {m₁ m₂ : MeasurableSpace α} (h : ∀ s : Set α, MeasurableSet[m₁] s ↔ MeasurableSet[m₂] s) : m₁ = m₂ := measurableSet_injective <| funext fun s => propext (h s) /-- A typeclass mixin for `MeasurableSpace`s such that each singleton is measurable. -/ class MeasurableSingletonClass (α : Type*) [MeasurableSpace α] : Prop where /-- A singleton is a measurable set. -/ measurableSet_singleton : ∀ x, MeasurableSet ({x} : Set α) export MeasurableSingletonClass (measurableSet_singleton) @[simp] lemma MeasurableSet.singleton [MeasurableSpace α] [MeasurableSingletonClass α] (a : α) : MeasurableSet {a} := measurableSet_singleton a section MeasurableSingletonClass variable [MeasurableSpace α] [MeasurableSingletonClass α] @[measurability] theorem measurableSet_eq {a : α} : MeasurableSet { x | x = a } := .singleton a @[measurability] protected theorem MeasurableSet.insert {s : Set α} (hs : MeasurableSet s) (a : α) : MeasurableSet (insert a s) := .union (.singleton a) hs @[simp] theorem measurableSet_insert {a : α} {s : Set α} : MeasurableSet (insert a s) ↔ MeasurableSet s := by classical exact ⟨fun h => if ha : a ∈ s then by rwa [← insert_eq_of_mem ha] else insert_diff_self_of_not_mem ha ▸ h.diff (.singleton _), fun h => h.insert a⟩ theorem Set.Subsingleton.measurableSet {s : Set α} (hs : s.Subsingleton) : MeasurableSet s := hs.induction_on .empty .singleton theorem Set.Finite.measurableSet {s : Set α} (hs : s.Finite) : MeasurableSet s := Finite.induction_on _ hs .empty fun _ _ hsm => hsm.insert _ @[measurability] protected theorem Finset.measurableSet (s : Finset α) : MeasurableSet (↑s : Set α) := s.finite_toSet.measurableSet theorem Set.Countable.measurableSet {s : Set α} (hs : s.Countable) : MeasurableSet s := by rw [← biUnion_of_singleton s] exact .biUnion hs fun b _ => .singleton b end MeasurableSingletonClass namespace MeasurableSpace /-- Copy of a `MeasurableSpace` with a new `MeasurableSet` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (m : MeasurableSpace α) (p : Set α → Prop) (h : ∀ s, p s ↔ MeasurableSet[m] s) : MeasurableSpace α where MeasurableSet' := p measurableSet_empty := by simpa only [h] using m.measurableSet_empty measurableSet_compl := by simpa only [h] using m.measurableSet_compl measurableSet_iUnion := by simpa only [h] using m.measurableSet_iUnion lemma measurableSet_copy {m : MeasurableSpace α} {p : Set α → Prop} (h : ∀ s, p s ↔ MeasurableSet[m] s) {s} : MeasurableSet[.copy m p h] s ↔ p s := Iff.rfl lemma copy_eq {m : MeasurableSpace α} {p : Set α → Prop} (h : ∀ s, p s ↔ MeasurableSet[m] s) : m.copy p h = m := ext h section CompleteLattice instance : LE (MeasurableSpace α) where le m₁ m₂ := ∀ s, MeasurableSet[m₁] s → MeasurableSet[m₂] s theorem le_def {α} {a b : MeasurableSpace α} : a ≤ b ↔ a.MeasurableSet' ≤ b.MeasurableSet' := Iff.rfl instance : PartialOrder (MeasurableSpace α) := { PartialOrder.lift (@MeasurableSet α) measurableSet_injective with le := LE.le lt := fun m₁ m₂ => m₁ ≤ m₂ ∧ ¬m₂ ≤ m₁ } /-- The smallest σ-algebra containing a collection `s` of basic sets -/ inductive GenerateMeasurable (s : Set (Set α)) : Set α → Prop | protected basic : ∀ u ∈ s, GenerateMeasurable s u | protected empty : GenerateMeasurable s ∅ | protected compl : ∀ t, GenerateMeasurable s t → GenerateMeasurable s tᶜ | protected iUnion : ∀ f : ℕ → Set α, (∀ n, GenerateMeasurable s (f n)) → GenerateMeasurable s (⋃ i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generateFrom (s : Set (Set α)) : MeasurableSpace α where MeasurableSet' := GenerateMeasurable s measurableSet_empty := .empty measurableSet_compl := .compl measurableSet_iUnion := .iUnion theorem measurableSet_generateFrom {s : Set (Set α)} {t : Set α} (ht : t ∈ s) : MeasurableSet[generateFrom s] t := .basic t ht @[elab_as_elim] theorem generateFrom_induction (C : Set (Set α)) (p : ∀ s : Set α, MeasurableSet[generateFrom C] s → Prop) (hC : ∀ t ∈ C, ∀ ht, p t ht) (empty : p ∅ (measurableSet_empty _)) (compl : ∀ t ht, p t ht → p tᶜ ht.compl) (iUnion : ∀ (s : ℕ → Set α) (hs : ∀ n, MeasurableSet[generateFrom C] (s n)), (∀ n, p (s n) (hs n)) → p (⋃ i, s i) (.iUnion hs)) (s : Set α) (hs : MeasurableSet[generateFrom C] s) : p s hs := by induction hs exacts [hC _ ‹_› _, empty, compl _ ‹_› ‹_›, iUnion ‹_› ‹_› ‹_›] theorem generateFrom_le {s : Set (Set α)} {m : MeasurableSpace α} (h : ∀ t ∈ s, MeasurableSet[m] t) : generateFrom s ≤ m := fun t (ht : GenerateMeasurable s t) => ht.recOn h .empty (fun _ _ => .compl) fun _ _ hf => .iUnion hf theorem generateFrom_le_iff {s : Set (Set α)} (m : MeasurableSpace α) : generateFrom s ≤ m ↔ s ⊆ { t | MeasurableSet[m] t } := Iff.intro (fun h _ hu => h _ <| measurableSet_generateFrom hu) fun h => generateFrom_le h @[simp] theorem generateFrom_measurableSet [MeasurableSpace α] : generateFrom { s : Set α | MeasurableSet s } = ‹_› := le_antisymm (generateFrom_le fun _ => id) fun _ => measurableSet_generateFrom theorem forall_generateFrom_mem_iff_mem_iff {S : Set (Set α)} {x y : α} : (∀ s, MeasurableSet[generateFrom S] s → (x ∈ s ↔ y ∈ s)) ↔ (∀ s ∈ S, x ∈ s ↔ y ∈ s) := by refine ⟨fun H s hs ↦ H s (.basic s hs), fun H s ↦ ?_⟩ apply generateFrom_induction · exact fun s hs _ ↦ H s hs · rfl · exact fun _ _ ↦ Iff.not · intro f _ hf simp only [mem_iUnion, hf] /-- If `g` is a collection of subsets of `α` such that the `σ`-algebra generated from `g` contains the same sets as `g`, then `g` was already a `σ`-algebra. -/ protected def mkOfClosure (g : Set (Set α)) (hg : { t | MeasurableSet[generateFrom g] t } = g) : MeasurableSpace α := (generateFrom g).copy (· ∈ g) <| Set.ext_iff.1 hg.symm theorem mkOfClosure_sets {s : Set (Set α)} {hs : { t | MeasurableSet[generateFrom s] t } = s} : MeasurableSpace.mkOfClosure s hs = generateFrom s := copy_eq _ /-- We get a Galois insertion between `σ`-algebras on `α` and `Set (Set α)` by using `generate_from` on one side and the collection of measurable sets on the other side. -/ def giGenerateFrom : GaloisInsertion (@generateFrom α) fun m => { t | MeasurableSet[m] t } where gc _ := generateFrom_le_iff le_l_u _ _ := measurableSet_generateFrom choice g hg := MeasurableSpace.mkOfClosure g <| le_antisymm hg <| (generateFrom_le_iff _).1 le_rfl choice_eq _ _ := mkOfClosure_sets instance : CompleteLattice (MeasurableSpace α) := giGenerateFrom.liftCompleteLattice instance : Inhabited (MeasurableSpace α) := ⟨⊤⟩ @[mono] theorem generateFrom_mono {s t : Set (Set α)} (h : s ⊆ t) : generateFrom s ≤ generateFrom t := giGenerateFrom.gc.monotone_l h theorem generateFrom_sup_generateFrom {s t : Set (Set α)} : generateFrom s ⊔ generateFrom t = generateFrom (s ∪ t) := (@giGenerateFrom α).gc.l_sup.symm lemma iSup_generateFrom (s : ι → Set (Set α)) : ⨆ i, generateFrom (s i) = generateFrom (⋃ i, s i) := (@MeasurableSpace.giGenerateFrom α).gc.l_iSup.symm @[simp] lemma generateFrom_empty : generateFrom (∅ : Set (Set α)) = ⊥ := le_bot_iff.mp (generateFrom_le (by simp)) theorem generateFrom_singleton_empty : generateFrom {∅} = (⊥ : MeasurableSpace α) := bot_unique <| generateFrom_le <| by simp [@MeasurableSet.empty α ⊥] theorem generateFrom_singleton_univ : generateFrom {Set.univ} = (⊥ : MeasurableSpace α) := bot_unique <| generateFrom_le <| by simp @[simp] theorem generateFrom_insert_univ (S : Set (Set α)) : generateFrom (insert Set.univ S) = generateFrom S := by rw [insert_eq, ← generateFrom_sup_generateFrom, generateFrom_singleton_univ, bot_sup_eq] @[simp] theorem generateFrom_insert_empty (S : Set (Set α)) : generateFrom (insert ∅ S) = generateFrom S := by rw [insert_eq, ← generateFrom_sup_generateFrom, generateFrom_singleton_empty, bot_sup_eq] theorem measurableSet_bot_iff {s : Set α} : MeasurableSet[⊥] s ↔ s = ∅ ∨ s = univ := let b : MeasurableSpace α := { MeasurableSet' := fun s => s = ∅ ∨ s = univ measurableSet_empty := Or.inl rfl measurableSet_compl := by simp +contextual [or_imp] measurableSet_iUnion := fun _ hf => sUnion_mem_empty_univ (forall_mem_range.2 hf) } have : b = ⊥ := bot_unique fun _ hs => hs.elim (fun s => s.symm ▸ @measurableSet_empty _ ⊥) fun s => s.symm ▸ @MeasurableSet.univ _ ⊥ this ▸ Iff.rfl @[simp, measurability] theorem measurableSet_top {s : Set α} : MeasurableSet[⊤] s := trivial @[simp] -- The `m₁` parameter gets filled in by typeclass instance synthesis (for some reason...) -- so we have to order it *after* `m₂`. Otherwise `simp` can't apply this lemma. theorem measurableSet_inf {m₂ m₁ : MeasurableSpace α} {s : Set α} : MeasurableSet[m₁ ⊓ m₂] s ↔ MeasurableSet[m₁] s ∧ MeasurableSet[m₂] s := Iff.rfl @[simp] theorem measurableSet_sInf {ms : Set (MeasurableSpace α)} {s : Set α} : MeasurableSet[sInf ms] s ↔ ∀ m ∈ ms, MeasurableSet[m] s := show s ∈ ⋂₀ _ ↔ _ by simp theorem measurableSet_iInf {ι} {m : ι → MeasurableSpace α} {s : Set α} : MeasurableSet[iInf m] s ↔ ∀ i, MeasurableSet[m i] s := by rw [iInf, measurableSet_sInf, forall_mem_range] theorem measurableSet_sup {m₁ m₂ : MeasurableSpace α} {s : Set α} : MeasurableSet[m₁ ⊔ m₂] s ↔ GenerateMeasurable (MeasurableSet[m₁] ∪ MeasurableSet[m₂]) s := Iff.rfl theorem measurableSet_sSup {ms : Set (MeasurableSpace α)} {s : Set α} : MeasurableSet[sSup ms] s ↔ GenerateMeasurable { s : Set α | ∃ m ∈ ms, MeasurableSet[m] s } s := by change GenerateMeasurable (⋃₀ _) _ ↔ _ simp [← setOf_exists] theorem measurableSet_iSup {ι} {m : ι → MeasurableSpace α} {s : Set α} : MeasurableSet[iSup m] s ↔ GenerateMeasurable { s : Set α | ∃ i, MeasurableSet[m i] s } s := by simp only [iSup, measurableSet_sSup, exists_range_iff] theorem measurableSpace_iSup_eq (m : ι → MeasurableSpace α) : ⨆ n, m n = generateFrom { s | ∃ n, MeasurableSet[m n] s } := by ext s rw [measurableSet_iSup] rfl theorem generateFrom_iUnion_measurableSet (m : ι → MeasurableSpace α) : generateFrom (⋃ n, { t | MeasurableSet[m n] t }) = ⨆ n, m n := (@giGenerateFrom α).l_iSup_u m end CompleteLattice end MeasurableSpace /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ @[fun_prop] def Measurable [MeasurableSpace α] [MeasurableSpace β] (f : α → β) : Prop := ∀ ⦃t : Set β⦄, MeasurableSet t → MeasurableSet (f ⁻¹' t) namespace MeasureTheory set_option quotPrecheck false in /-- Notation for `Measurable` with respect to a non-standard σ-algebra in the domain. -/ scoped notation "Measurable[" m "]" => @Measurable _ _ m _ /-- Notation for `Measurable` with respect to a non-standard σ-algebra in the domain and codomain. -/
scoped notation "Measurable[" mα ", " mβ "]" => @Measurable _ _ mα mβ end MeasureTheory section MeasurableFunctions @[measurability] theorem measurable_id {_ : MeasurableSpace α} : Measurable (@id α) := fun _ => id @[fun_prop, measurability] theorem measurable_id' {_ : MeasurableSpace α} : Measurable fun a : α => a := measurable_id
Mathlib/MeasureTheory/MeasurableSpace/Defs.lean
481
491
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.WSeq.Basic import Mathlib.Data.WSeq.Defs import Mathlib.Data.WSeq.Productive import Mathlib.Data.WSeq.Relation deprecated_module (since := "2025-04-13")
Mathlib/Data/Seq/WSeq.lean
1,431
1,434
/- 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.Analysis.SpecialFunctions.Gamma.Basic import Mathlib.Analysis.SpecialFunctions.PolarCoord import Mathlib.Analysis.Complex.Convex import Mathlib.Data.Nat.Factorial.DoubleFactorial /-! # Gaussian integral We prove various versions of the formula for the Gaussian integral: * `integral_gaussian`: for real `b` we have `∫ x:ℝ, exp (-b * x^2) = √(π / b)`. * `integral_gaussian_complex`: for complex `b` with `0 < re b` we have `∫ x:ℝ, exp (-b * x^2) = (π / b) ^ (1 / 2)`. * `integral_gaussian_Ioi` and `integral_gaussian_complex_Ioi`: variants for integrals over `Ioi 0`. * `Complex.Gamma_one_half_eq`: the formula `Γ (1 / 2) = √π`. -/ noncomputable section open Real Set MeasureTheory Filter Asymptotics open scoped Real Topology open Complex hiding exp abs_of_nonneg theorem exp_neg_mul_rpow_isLittleO_exp_neg {p b : ℝ} (hb : 0 < b) (hp : 1 < p) : (fun x : ℝ => exp (- b * x ^ p)) =o[atTop] fun x : ℝ => exp (-x) := by rw [isLittleO_exp_comp_exp_comp] suffices Tendsto (fun x => x * (b * x ^ (p - 1) + -1)) atTop atTop by refine Tendsto.congr' ?_ this refine eventuallyEq_of_mem (Ioi_mem_atTop (0 : ℝ)) (fun x hx => ?_) rw [mem_Ioi] at hx rw [rpow_sub_one hx.ne'] field_simp [hx.ne'] ring apply tendsto_id.atTop_mul_atTop₀ refine tendsto_atTop_add_const_right atTop (-1 : ℝ) ?_ exact Tendsto.const_mul_atTop hb (tendsto_rpow_atTop (by linarith)) theorem exp_neg_mul_sq_isLittleO_exp_neg {b : ℝ} (hb : 0 < b) : (fun x : ℝ => exp (-b * x ^ 2)) =o[atTop] fun x : ℝ => exp (-x) := by simp_rw [← rpow_two] exact exp_neg_mul_rpow_isLittleO_exp_neg hb one_lt_two theorem rpow_mul_exp_neg_mul_rpow_isLittleO_exp_neg (s : ℝ) {b p : ℝ} (hp : 1 < p) (hb : 0 < b) : (fun x : ℝ => x ^ s * exp (- b * x ^ p)) =o[atTop] fun x : ℝ => exp (-(1 / 2) * x) := by apply ((isBigO_refl (fun x : ℝ => x ^ s) atTop).mul_isLittleO (exp_neg_mul_rpow_isLittleO_exp_neg hb hp)).trans simpa only [mul_comm] using Real.Gamma_integrand_isLittleO s theorem rpow_mul_exp_neg_mul_sq_isLittleO_exp_neg {b : ℝ} (hb : 0 < b) (s : ℝ) : (fun x : ℝ => x ^ s * exp (-b * x ^ 2)) =o[atTop] fun x : ℝ => exp (-(1 / 2) * x) := by simp_rw [← rpow_two] exact rpow_mul_exp_neg_mul_rpow_isLittleO_exp_neg s one_lt_two hb theorem integrableOn_rpow_mul_exp_neg_rpow {p s : ℝ} (hs : -1 < s) (hp : 1 ≤ p) : IntegrableOn (fun x : ℝ => x ^ s * exp (- x ^ p)) (Ioi 0) := by obtain hp | hp := le_iff_lt_or_eq.mp hp · have h_exp : ∀ x, ContinuousAt (fun x => exp (- x)) x := fun x => continuousAt_neg.rexp rw [← Ioc_union_Ioi_eq_Ioi zero_le_one, integrableOn_union] constructor · rw [← integrableOn_Icc_iff_integrableOn_Ioc] refine IntegrableOn.mul_continuousOn ?_ ?_ isCompact_Icc · refine (intervalIntegrable_iff_integrableOn_Icc_of_le zero_le_one).mp ?_ exact intervalIntegral.intervalIntegrable_rpow' hs · intro x _ change ContinuousWithinAt ((fun x => exp (- x)) ∘ (fun x => x ^ p)) (Icc 0 1) x refine ContinuousAt.comp_continuousWithinAt (h_exp _) ?_ exact continuousWithinAt_id.rpow_const (Or.inr (le_of_lt (lt_trans zero_lt_one hp))) · have h_rpow : ∀ (x r : ℝ), x ∈ Ici 1 → ContinuousWithinAt (fun x => x ^ r) (Ici 1) x := by intro _ _ hx refine continuousWithinAt_id.rpow_const (Or.inl ?_) exact ne_of_gt (lt_of_lt_of_le zero_lt_one hx) refine integrable_of_isBigO_exp_neg (by norm_num : (0 : ℝ) < 1 / 2) (ContinuousOn.mul (fun x hx => h_rpow x s hx) (fun x hx => ?_)) (IsLittleO.isBigO ?_) · change ContinuousWithinAt ((fun x => exp (- x)) ∘ (fun x => x ^ p)) (Ici 1) x exact ContinuousAt.comp_continuousWithinAt (h_exp _) (h_rpow x p hx) · convert rpow_mul_exp_neg_mul_rpow_isLittleO_exp_neg s hp (by norm_num : (0 : ℝ) < 1) using 3 rw [neg_mul, one_mul] · simp_rw [← hp, Real.rpow_one] convert Real.GammaIntegral_convergent (by linarith : 0 < s + 1) using 2 rw [add_sub_cancel_right, mul_comm] theorem integrableOn_rpow_mul_exp_neg_mul_rpow {p s b : ℝ} (hs : -1 < s) (hp : 1 ≤ p) (hb : 0 < b) : IntegrableOn (fun x : ℝ => x ^ s * exp (- b * x ^ p)) (Ioi 0) := by have hib : 0 < b ^ (-p⁻¹) := rpow_pos_of_pos hb _ suffices IntegrableOn (fun x ↦ (b ^ (-p⁻¹)) ^ s * (x ^ s * exp (-x ^ p))) (Ioi 0) by rw [show 0 = b ^ (-p⁻¹) * 0 by rw [mul_zero], ← integrableOn_Ioi_comp_mul_left_iff _ _ hib] refine this.congr_fun (fun _ hx => ?_) measurableSet_Ioi rw [← mul_assoc, mul_rpow, mul_rpow, ← rpow_mul (z := p), neg_mul, neg_mul, inv_mul_cancel₀, rpow_neg_one, mul_inv_cancel_left₀] all_goals linarith [mem_Ioi.mp hx] refine Integrable.const_mul ?_ _ rw [← IntegrableOn] exact integrableOn_rpow_mul_exp_neg_rpow hs hp theorem integrableOn_rpow_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) {s : ℝ} (hs : -1 < s) : IntegrableOn (fun x : ℝ => x ^ s * exp (-b * x ^ 2)) (Ioi 0) := by simp_rw [← rpow_two] exact integrableOn_rpow_mul_exp_neg_mul_rpow hs one_le_two hb theorem integrable_rpow_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) {s : ℝ} (hs : -1 < s) : Integrable fun x : ℝ => x ^ s * exp (-b * x ^ 2) := by rw [← integrableOn_univ, ← @Iio_union_Ici _ _ (0 : ℝ), integrableOn_union, integrableOn_Ici_iff_integrableOn_Ioi] refine ⟨?_, integrableOn_rpow_mul_exp_neg_mul_sq hb hs⟩ rw [← (Measure.measurePreserving_neg (volume : Measure ℝ)).integrableOn_comp_preimage (Homeomorph.neg ℝ).measurableEmbedding] simp only [Function.comp_def, neg_sq, neg_preimage, neg_Iio, neg_neg, neg_zero] apply Integrable.mono' (integrableOn_rpow_mul_exp_neg_mul_sq hb hs) · apply Measurable.aestronglyMeasurable exact (measurable_id'.neg.pow measurable_const).mul ((measurable_id'.pow measurable_const).const_mul (-b)).exp · have : MeasurableSet (Ioi (0 : ℝ)) := measurableSet_Ioi filter_upwards [ae_restrict_mem this] with x hx have h'x : 0 ≤ x := le_of_lt hx rw [Real.norm_eq_abs, abs_mul, abs_of_nonneg (exp_pos _).le] apply mul_le_mul_of_nonneg_right _ (exp_pos _).le simpa [abs_of_nonneg h'x] using abs_rpow_le_abs_rpow (-x) s theorem integrable_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) : Integrable fun x : ℝ => exp (-b * x ^ 2) := by simpa using integrable_rpow_mul_exp_neg_mul_sq hb (by norm_num : (-1 : ℝ) < 0) theorem integrableOn_Ioi_exp_neg_mul_sq_iff {b : ℝ} : IntegrableOn (fun x : ℝ => exp (-b * x ^ 2)) (Ioi 0) ↔ 0 < b := by refine ⟨fun h => ?_, fun h => (integrable_exp_neg_mul_sq h).integrableOn⟩ by_contra! hb have : ∫⁻ _ : ℝ in Ioi 0, 1 ≤ ∫⁻ x : ℝ in Ioi 0, ‖exp (-b * x ^ 2)‖₊ := by apply lintegral_mono (fun x ↦ _) simp only [neg_mul, ENNReal.one_le_coe_iff, ← toNNReal_one, toNNReal_le_iff_le_coe, Real.norm_of_nonneg (exp_pos _).le, coe_nnnorm, one_le_exp_iff, Right.nonneg_neg_iff] exact fun x ↦ mul_nonpos_of_nonpos_of_nonneg hb (sq_nonneg x) simpa using this.trans_lt h.2 theorem integrable_exp_neg_mul_sq_iff {b : ℝ} : (Integrable fun x : ℝ => exp (-b * x ^ 2)) ↔ 0 < b := ⟨fun h => integrableOn_Ioi_exp_neg_mul_sq_iff.mp h.integrableOn, integrable_exp_neg_mul_sq⟩ theorem integrable_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) : Integrable fun x : ℝ => x * exp (-b * x ^ 2) := by simpa using integrable_rpow_mul_exp_neg_mul_sq hb (by norm_num : (-1 : ℝ) < 1) theorem norm_cexp_neg_mul_sq (b : ℂ) (x : ℝ) : ‖Complex.exp (-b * (x : ℂ) ^ 2)‖ = exp (-b.re * x ^ 2) := by rw [norm_exp, ← ofReal_pow, mul_comm (-b) _, re_ofReal_mul, neg_re, mul_comm]
theorem integrable_cexp_neg_mul_sq {b : ℂ} (hb : 0 < b.re) : Integrable fun x : ℝ => cexp (-b * (x : ℂ) ^ 2) := by refine ⟨(Complex.continuous_exp.comp
Mathlib/Analysis/SpecialFunctions/Gaussian/GaussianIntegral.lean
152
154
/- 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.Linear import Mathlib.Analysis.Calculus.FDeriv.Comp /-! # Additive operations on derivatives 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 * sum of finitely many functions * multiplication of a function by a scalar constant * negative of a function * subtraction of two functions -/ open Filter Asymptotics ContinuousLinearMap noncomputable section section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] variable {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] variable {f g : E → F} variable {f' g' : E →L[𝕜] F} variable {x : E} variable {s : Set E} variable {L : Filter E} section ConstSMul variable {R : Type*} [Semiring R] [Module R F] [SMulCommClass 𝕜 R F] [ContinuousConstSMul R F] /-! ### Derivative of a function multiplied by a constant -/ @[fun_prop] theorem HasStrictFDerivAt.const_smul (h : HasStrictFDerivAt f f' x) (c : R) : HasStrictFDerivAt (fun x => c • f x) (c • f') x := (c • (1 : F →L[𝕜] F)).hasStrictFDerivAt.comp x h theorem HasFDerivAtFilter.const_smul (h : HasFDerivAtFilter f f' x L) (c : R) : HasFDerivAtFilter (fun x => c • f x) (c • f') x L := (c • (1 : F →L[𝕜] F)).hasFDerivAtFilter.comp x h tendsto_map @[fun_prop] nonrec theorem HasFDerivWithinAt.const_smul (h : HasFDerivWithinAt f f' s x) (c : R) : HasFDerivWithinAt (fun x => c • f x) (c • f') s x := h.const_smul c @[fun_prop] nonrec theorem HasFDerivAt.const_smul (h : HasFDerivAt f f' x) (c : R) : HasFDerivAt (fun x => c • f x) (c • f') x := h.const_smul c @[fun_prop] theorem DifferentiableWithinAt.const_smul (h : DifferentiableWithinAt 𝕜 f s x) (c : R) : DifferentiableWithinAt 𝕜 (fun y => c • f y) s x := (h.hasFDerivWithinAt.const_smul c).differentiableWithinAt @[fun_prop] theorem DifferentiableAt.const_smul (h : DifferentiableAt 𝕜 f x) (c : R) : DifferentiableAt 𝕜 (fun y => c • f y) x := (h.hasFDerivAt.const_smul c).differentiableAt @[fun_prop] theorem DifferentiableOn.const_smul (h : DifferentiableOn 𝕜 f s) (c : R) : DifferentiableOn 𝕜 (fun y => c • f y) s := fun x hx => (h x hx).const_smul c @[fun_prop] theorem Differentiable.const_smul (h : Differentiable 𝕜 f) (c : R) : Differentiable 𝕜 fun y => c • f y := fun x => (h x).const_smul c theorem fderivWithin_const_smul (hxs : UniqueDiffWithinAt 𝕜 s x) (h : DifferentiableWithinAt 𝕜 f s x) (c : R) : fderivWithin 𝕜 (fun y => c • f y) s x = c • fderivWithin 𝕜 f s x := (h.hasFDerivWithinAt.const_smul c).fderivWithin hxs /-- Version of `fderivWithin_const_smul` written with `c • f` instead of `fun y ↦ c • f y`. -/ theorem fderivWithin_const_smul' (hxs : UniqueDiffWithinAt 𝕜 s x) (h : DifferentiableWithinAt 𝕜 f s x) (c : R) : fderivWithin 𝕜 (c • f) s x = c • fderivWithin 𝕜 f s x := fderivWithin_const_smul hxs h c theorem fderiv_const_smul (h : DifferentiableAt 𝕜 f x) (c : R) : fderiv 𝕜 (fun y => c • f y) x = c • fderiv 𝕜 f x := (h.hasFDerivAt.const_smul c).fderiv /-- Version of `fderiv_const_smul` written with `c • f` instead of `fun y ↦ c • f y`. -/ theorem fderiv_const_smul' (h : DifferentiableAt 𝕜 f x) (c : R) : fderiv 𝕜 (c • f) x = c • fderiv 𝕜 f x := (h.hasFDerivAt.const_smul c).fderiv end ConstSMul section Add /-! ### Derivative of the sum of two functions -/ @[fun_prop] nonrec theorem HasStrictFDerivAt.add (hf : HasStrictFDerivAt f f' x) (hg : HasStrictFDerivAt g g' x) : HasStrictFDerivAt (fun y => f y + g y) (f' + g') x := .of_isLittleO <| (hf.isLittleO.add hg.isLittleO).congr_left fun y => by simp only [LinearMap.sub_apply, LinearMap.add_apply, map_sub, map_add, add_apply] abel theorem HasFDerivAtFilter.add (hf : HasFDerivAtFilter f f' x L) (hg : HasFDerivAtFilter g g' x L) : HasFDerivAtFilter (fun y => f y + g y) (f' + g') x L := .of_isLittleO <| (hf.isLittleO.add hg.isLittleO).congr_left fun _ => by simp only [LinearMap.sub_apply, LinearMap.add_apply, map_sub, map_add, add_apply] abel @[fun_prop] nonrec theorem HasFDerivWithinAt.add (hf : HasFDerivWithinAt f f' s x) (hg : HasFDerivWithinAt g g' s x) : HasFDerivWithinAt (fun y => f y + g y) (f' + g') s x := hf.add hg @[fun_prop] nonrec theorem HasFDerivAt.add (hf : HasFDerivAt f f' x) (hg : HasFDerivAt g g' x) : HasFDerivAt (fun x => f x + g x) (f' + g') x := hf.add hg @[fun_prop] theorem DifferentiableWithinAt.add (hf : DifferentiableWithinAt 𝕜 f s x) (hg : DifferentiableWithinAt 𝕜 g s x) : DifferentiableWithinAt 𝕜 (fun y => f y + g y) s x := (hf.hasFDerivWithinAt.add hg.hasFDerivWithinAt).differentiableWithinAt @[simp, fun_prop] theorem DifferentiableAt.add (hf : DifferentiableAt 𝕜 f x) (hg : DifferentiableAt 𝕜 g x) : DifferentiableAt 𝕜 (fun y => f y + g y) x := (hf.hasFDerivAt.add hg.hasFDerivAt).differentiableAt @[fun_prop] theorem DifferentiableOn.add (hf : DifferentiableOn 𝕜 f s) (hg : DifferentiableOn 𝕜 g s) : DifferentiableOn 𝕜 (fun y => f y + g y) s := fun x hx => (hf x hx).add (hg x hx) @[simp, fun_prop] theorem Differentiable.add (hf : Differentiable 𝕜 f) (hg : Differentiable 𝕜 g) : Differentiable 𝕜 fun y => f y + g y := fun x => (hf x).add (hg x) theorem fderivWithin_add (hxs : UniqueDiffWithinAt 𝕜 s x) (hf : DifferentiableWithinAt 𝕜 f s x) (hg : DifferentiableWithinAt 𝕜 g s x) : fderivWithin 𝕜 (fun y => f y + g y) s x = fderivWithin 𝕜 f s x + fderivWithin 𝕜 g s x := (hf.hasFDerivWithinAt.add hg.hasFDerivWithinAt).fderivWithin hxs /-- Version of `fderivWithin_add` where the function is written as `f + g` instead of `fun y ↦ f y + g y`. -/ theorem fderivWithin_add' (hxs : UniqueDiffWithinAt 𝕜 s x) (hf : DifferentiableWithinAt 𝕜 f s x) (hg : DifferentiableWithinAt 𝕜 g s x) : fderivWithin 𝕜 (f + g) s x = fderivWithin 𝕜 f s x + fderivWithin 𝕜 g s x := fderivWithin_add hxs hf hg theorem fderiv_add (hf : DifferentiableAt 𝕜 f x) (hg : DifferentiableAt 𝕜 g x) : fderiv 𝕜 (fun y => f y + g y) x = fderiv 𝕜 f x + fderiv 𝕜 g x := (hf.hasFDerivAt.add hg.hasFDerivAt).fderiv /-- Version of `fderiv_add` where the function is written as `f + g` instead of `fun y ↦ f y + g y`. -/ theorem fderiv_add' (hf : DifferentiableAt 𝕜 f x) (hg : DifferentiableAt 𝕜 g x) : fderiv 𝕜 (f + g) x = fderiv 𝕜 f x + fderiv 𝕜 g x := fderiv_add hf hg @[simp] theorem hasFDerivAtFilter_add_const_iff (c : F) : HasFDerivAtFilter (f · + c) f' x L ↔ HasFDerivAtFilter f f' x L := by simp [hasFDerivAtFilter_iff_isLittleOTVS] alias ⟨_, HasFDerivAtFilter.add_const⟩ := hasFDerivAtFilter_add_const_iff @[simp] theorem hasStrictFDerivAt_add_const_iff (c : F) : HasStrictFDerivAt (f · + c) f' x ↔ HasStrictFDerivAt f f' x := by simp [hasStrictFDerivAt_iff_isLittleO] @[fun_prop] alias ⟨_, HasStrictFDerivAt.add_const⟩ := hasStrictFDerivAt_add_const_iff @[simp] theorem hasFDerivWithinAt_add_const_iff (c : F) : HasFDerivWithinAt (f · + c) f' s x ↔ HasFDerivWithinAt f f' s x := hasFDerivAtFilter_add_const_iff c @[fun_prop] alias ⟨_, HasFDerivWithinAt.add_const⟩ := hasFDerivWithinAt_add_const_iff @[simp] theorem hasFDerivAt_add_const_iff (c : F) : HasFDerivAt (f · + c) f' x ↔ HasFDerivAt f f' x := hasFDerivAtFilter_add_const_iff c @[fun_prop] alias ⟨_, HasFDerivAt.add_const⟩ := hasFDerivAt_add_const_iff @[simp] theorem differentiableWithinAt_add_const_iff (c : F) : DifferentiableWithinAt 𝕜 (fun y => f y + c) s x ↔ DifferentiableWithinAt 𝕜 f s x := exists_congr fun _ ↦ hasFDerivWithinAt_add_const_iff c @[fun_prop] alias ⟨_, DifferentiableWithinAt.add_const⟩ := differentiableWithinAt_add_const_iff @[simp] theorem differentiableAt_add_const_iff (c : F) : DifferentiableAt 𝕜 (fun y => f y + c) x ↔ DifferentiableAt 𝕜 f x := exists_congr fun _ ↦ hasFDerivAt_add_const_iff c @[fun_prop] alias ⟨_, DifferentiableAt.add_const⟩ := differentiableAt_add_const_iff @[simp] theorem differentiableOn_add_const_iff (c : F) : DifferentiableOn 𝕜 (fun y => f y + c) s ↔ DifferentiableOn 𝕜 f s := forall₂_congr fun _ _ ↦ differentiableWithinAt_add_const_iff c @[fun_prop] alias ⟨_, DifferentiableOn.add_const⟩ := differentiableOn_add_const_iff @[simp] theorem differentiable_add_const_iff (c : F) : (Differentiable 𝕜 fun y => f y + c) ↔ Differentiable 𝕜 f := forall_congr' fun _ ↦ differentiableAt_add_const_iff c @[fun_prop] alias ⟨_, Differentiable.add_const⟩ := differentiable_add_const_iff @[simp] theorem fderivWithin_add_const (c : F) : fderivWithin 𝕜 (fun y => f y + c) s x = fderivWithin 𝕜 f s x := by classical simp [fderivWithin] @[simp] theorem fderiv_add_const (c : F) : fderiv 𝕜 (fun y => f y + c) x = fderiv 𝕜 f x := by simp only [← fderivWithin_univ, fderivWithin_add_const] @[simp] theorem hasFDerivAtFilter_const_add_iff (c : F) : HasFDerivAtFilter (c + f ·) f' x L ↔ HasFDerivAtFilter f f' x L := by simpa only [add_comm] using hasFDerivAtFilter_add_const_iff c alias ⟨_, HasFDerivAtFilter.const_add⟩ := hasFDerivAtFilter_const_add_iff @[simp] theorem hasStrictFDerivAt_const_add_iff (c : F) : HasStrictFDerivAt (c + f ·) f' x ↔ HasStrictFDerivAt f f' x := by simpa only [add_comm] using hasStrictFDerivAt_add_const_iff c @[fun_prop] alias ⟨_, HasStrictFDerivAt.const_add⟩ := hasStrictFDerivAt_const_add_iff @[simp] theorem hasFDerivWithinAt_const_add_iff (c : F) : HasFDerivWithinAt (c + f ·) f' s x ↔ HasFDerivWithinAt f f' s x := hasFDerivAtFilter_const_add_iff c @[fun_prop] alias ⟨_, HasFDerivWithinAt.const_add⟩ := hasFDerivWithinAt_const_add_iff @[simp] theorem hasFDerivAt_const_add_iff (c : F) : HasFDerivAt (c + f ·) f' x ↔ HasFDerivAt f f' x := hasFDerivAtFilter_const_add_iff c @[fun_prop] alias ⟨_, HasFDerivAt.const_add⟩ := hasFDerivAt_const_add_iff @[simp] theorem differentiableWithinAt_const_add_iff (c : F) : DifferentiableWithinAt 𝕜 (fun y => c + f y) s x ↔ DifferentiableWithinAt 𝕜 f s x := exists_congr fun _ ↦ hasFDerivWithinAt_const_add_iff c @[fun_prop] alias ⟨_, DifferentiableWithinAt.const_add⟩ := differentiableWithinAt_const_add_iff @[simp] theorem differentiableAt_const_add_iff (c : F) : DifferentiableAt 𝕜 (fun y => c + f y) x ↔ DifferentiableAt 𝕜 f x := exists_congr fun _ ↦ hasFDerivAt_const_add_iff c @[fun_prop] alias ⟨_, DifferentiableAt.const_add⟩ := differentiableAt_const_add_iff @[simp] theorem differentiableOn_const_add_iff (c : F) : DifferentiableOn 𝕜 (fun y => c + f y) s ↔ DifferentiableOn 𝕜 f s := forall₂_congr fun _ _ ↦ differentiableWithinAt_const_add_iff c @[fun_prop] alias ⟨_, DifferentiableOn.const_add⟩ := differentiableOn_const_add_iff @[simp] theorem differentiable_const_add_iff (c : F) : (Differentiable 𝕜 fun y => c + f y) ↔ Differentiable 𝕜 f := forall_congr' fun _ ↦ differentiableAt_const_add_iff c @[fun_prop] alias ⟨_, Differentiable.const_add⟩ := differentiable_const_add_iff @[simp] theorem fderivWithin_const_add (c : F) : fderivWithin 𝕜 (fun y => c + f y) s x = fderivWithin 𝕜 f s x := by simpa only [add_comm] using fderivWithin_add_const c @[simp] theorem fderiv_const_add (c : F) : fderiv 𝕜 (fun y => c + f y) x = fderiv 𝕜 f x := by simp only [add_comm c, fderiv_add_const] end Add section Sum /-! ### Derivative of a finite sum of functions -/ variable {ι : Type*} {u : Finset ι} {A : ι → E → F} {A' : ι → E →L[𝕜] F} @[fun_prop] theorem HasStrictFDerivAt.sum (h : ∀ i ∈ u, HasStrictFDerivAt (A i) (A' i) x) : HasStrictFDerivAt (fun y => ∑ i ∈ u, A i y) (∑ i ∈ u, A' i) x := by simp only [hasStrictFDerivAt_iff_isLittleO] at * convert IsLittleO.sum h simp [Finset.sum_sub_distrib, ContinuousLinearMap.sum_apply] theorem HasFDerivAtFilter.sum (h : ∀ i ∈ u, HasFDerivAtFilter (A i) (A' i) x L) : HasFDerivAtFilter (fun y => ∑ i ∈ u, A i y) (∑ i ∈ u, A' i) x L := by
simp only [hasFDerivAtFilter_iff_isLittleO] at * convert IsLittleO.sum h
Mathlib/Analysis/Calculus/FDeriv/Add.lean
332
333
/- 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.MeasureTheory.Measure.Content import Mathlib.MeasureTheory.Group.Prod import Mathlib.Topology.Algebra.Group.Compact /-! # Haar measure In this file we prove the existence of Haar measure for a locally compact Hausdorff topological group. We follow the write-up by Jonathan Gleason, *Existence and Uniqueness of Haar Measure*. This is essentially the same argument as in https://en.wikipedia.org/wiki/Haar_measure#A_construction_using_compact_subsets. We construct the Haar measure first on compact sets. For this we define `(K : U)` as the (smallest) number of left-translates of `U` that are needed to cover `K` (`index` in the formalization). Then we define a function `h` on compact sets as `lim_U (K : U) / (K₀ : U)`, where `U` becomes a smaller and smaller open neighborhood of `1`, and `K₀` is a fixed compact set with nonempty interior. This function is `chaar` in the formalization, and we define the limit formally using Tychonoff's theorem. This function `h` forms a content, which we can extend to an outer measure and then a measure (`haarMeasure`). We normalize the Haar measure so that the measure of `K₀` is `1`. Note that `μ` need not coincide with `h` on compact sets, according to [halmos1950measure, ch. X, §53 p.233]. However, we know that `h(K)` lies between `μ(Kᵒ)` and `μ(K)`, where `ᵒ` denotes the interior. We also give a form of uniqueness of Haar measure, for σ-finite measures on second-countable locally compact groups. For more involved statements not assuming second-countability, see the file `Mathlib/MeasureTheory/Measure/Haar/Unique.lean`. ## Main Declarations * `haarMeasure`: the Haar measure on a locally compact Hausdorff group. This is a left invariant regular measure. It takes as argument a compact set of the group (with non-empty interior), and is normalized so that the measure of the given set is 1. * `haarMeasure_self`: the Haar measure is normalized. * `isMulLeftInvariant_haarMeasure`: the Haar measure is left invariant. * `regular_haarMeasure`: the Haar measure is a regular measure. * `isHaarMeasure_haarMeasure`: the Haar measure satisfies the `IsHaarMeasure` typeclass, i.e., it is invariant and gives finite mass to compact sets and positive mass to nonempty open sets. * `haar` : some choice of a Haar measure, on a locally compact Hausdorff group, constructed as `haarMeasure K` where `K` is some arbitrary choice of a compact set with nonempty interior. * `haarMeasure_unique`: Every σ-finite left invariant measure on a second-countable locally compact Hausdorff group is a scalar multiple of the Haar measure. ## References * Paul Halmos (1950), Measure Theory, §53 * Jonathan Gleason, Existence and Uniqueness of Haar Measure - Note: step 9, page 8 contains a mistake: the last defined `μ` does not extend the `μ` on compact sets, see Halmos (1950) p. 233, bottom of the page. This makes some other steps (like step 11) invalid. * https://en.wikipedia.org/wiki/Haar_measure -/ noncomputable section open Set Inv Function TopologicalSpace MeasurableSpace open scoped NNReal ENNReal Pointwise Topology namespace MeasureTheory namespace Measure section Group variable {G : Type*} [Group G] /-! We put the internal functions in the construction of the Haar measure in a namespace, so that the chosen names don't clash with other declarations. We first define a couple of the functions before proving the properties (that require that `G` is a topological group). -/ namespace haar /-- The index or Haar covering number or ratio of `K` w.r.t. `V`, denoted `(K : V)`: it is the smallest number of (left) translates of `V` that is necessary to cover `K`. It is defined to be 0 if no finite number of translates cover `K`. -/ @[to_additive addIndex "additive version of `MeasureTheory.Measure.haar.index`"] noncomputable def index (K V : Set G) : ℕ := sInf <| Finset.card '' { t : Finset G | K ⊆ ⋃ g ∈ t, (fun h => g * h) ⁻¹' V } @[to_additive addIndex_empty] theorem index_empty {V : Set G} : index ∅ V = 0 := by simp [index] variable [TopologicalSpace G] /-- `prehaar K₀ U K` is a weighted version of the index, defined as `(K : U)/(K₀ : U)`. In the applications `K₀` is compact with non-empty interior, `U` is open containing `1`, and `K` is any compact set. The argument `K` is a (bundled) compact set, so that we can consider `prehaar K₀ U` as an element of `haarProduct` (below). -/ @[to_additive "additive version of `MeasureTheory.Measure.haar.prehaar`"] noncomputable def prehaar (K₀ U : Set G) (K : Compacts G) : ℝ := (index (K : Set G) U : ℝ) / index K₀ U @[to_additive] theorem prehaar_empty (K₀ : PositiveCompacts G) {U : Set G} : prehaar (K₀ : Set G) U ⊥ = 0 := by rw [prehaar, Compacts.coe_bot, index_empty, Nat.cast_zero, zero_div] @[to_additive] theorem prehaar_nonneg (K₀ : PositiveCompacts G) {U : Set G} (K : Compacts G) : 0 ≤ prehaar (K₀ : Set G) U K := by apply div_nonneg <;> norm_cast <;> apply zero_le /-- `haarProduct K₀` is the product of intervals `[0, (K : K₀)]`, for all compact sets `K`. For all `U`, we can show that `prehaar K₀ U ∈ haarProduct K₀`. -/ @[to_additive "additive version of `MeasureTheory.Measure.haar.haarProduct`"] def haarProduct (K₀ : Set G) : Set (Compacts G → ℝ) := pi univ fun K => Icc 0 <| index (K : Set G) K₀ @[to_additive (attr := simp)] theorem mem_prehaar_empty {K₀ : Set G} {f : Compacts G → ℝ} : f ∈ haarProduct K₀ ↔ ∀ K : Compacts G, f K ∈ Icc (0 : ℝ) (index (K : Set G) K₀) := by simp only [haarProduct, Set.pi, forall_prop_of_true, mem_univ, mem_setOf_eq] /-- The closure of the collection of elements of the form `prehaar K₀ U`, for `U` open neighbourhoods of `1`, contained in `V`. The closure is taken in the space `compacts G → ℝ`, with the topology of pointwise convergence. We show that the intersection of all these sets is nonempty, and the Haar measure on compact sets is defined to be an element in the closure of this intersection. -/ @[to_additive "additive version of `MeasureTheory.Measure.haar.clPrehaar`"] def clPrehaar (K₀ : Set G) (V : OpenNhdsOf (1 : G)) : Set (Compacts G → ℝ) := closure <| prehaar K₀ '' { U : Set G | U ⊆ V.1 ∧ IsOpen U ∧ (1 : G) ∈ U } variable [IsTopologicalGroup G] /-! ### Lemmas about `index` -/ /-- If `K` is compact and `V` has nonempty interior, then the index `(K : V)` is well-defined, there is a finite set `t` satisfying the desired properties. -/ @[to_additive addIndex_defined "If `K` is compact and `V` has nonempty interior, then the index `(K : V)` is well-defined, there is a finite set `t` satisfying the desired properties."] theorem index_defined {K V : Set G} (hK : IsCompact K) (hV : (interior V).Nonempty) : ∃ n : ℕ, n ∈ Finset.card '' { t : Finset G | K ⊆ ⋃ g ∈ t, (fun h => g * h) ⁻¹' V } := by rcases compact_covered_by_mul_left_translates hK hV with ⟨t, ht⟩; exact ⟨t.card, t, ht, rfl⟩ @[to_additive addIndex_elim] theorem index_elim {K V : Set G} (hK : IsCompact K) (hV : (interior V).Nonempty) : ∃ t : Finset G, (K ⊆ ⋃ g ∈ t, (fun h => g * h) ⁻¹' V) ∧ Finset.card t = index K V := by have := Nat.sInf_mem (index_defined hK hV); rwa [mem_image] at this @[to_additive le_addIndex_mul] theorem le_index_mul (K₀ : PositiveCompacts G) (K : Compacts G) {V : Set G} (hV : (interior V).Nonempty) : index (K : Set G) V ≤ index (K : Set G) K₀ * index (K₀ : Set G) V := by classical obtain ⟨s, h1s, h2s⟩ := index_elim K.isCompact K₀.interior_nonempty obtain ⟨t, h1t, h2t⟩ := index_elim K₀.isCompact hV rw [← h2s, ← h2t, mul_comm] refine le_trans ?_ Finset.card_mul_le apply Nat.sInf_le; refine ⟨_, ?_, rfl⟩; rw [mem_setOf_eq]; refine Subset.trans h1s ?_ apply iUnion₂_subset; intro g₁ hg₁; rw [preimage_subset_iff]; intro g₂ hg₂ have := h1t hg₂ rcases this with ⟨_, ⟨g₃, rfl⟩, A, ⟨hg₃, rfl⟩, h2V⟩; rw [mem_preimage, ← mul_assoc] at h2V exact mem_biUnion (Finset.mul_mem_mul hg₃ hg₁) h2V @[to_additive addIndex_pos] theorem index_pos (K : PositiveCompacts G) {V : Set G} (hV : (interior V).Nonempty) : 0 < index (K : Set G) V := by classical rw [index, Nat.sInf_def, Nat.find_pos, mem_image] · rintro ⟨t, h1t, h2t⟩; rw [Finset.card_eq_zero] at h2t; subst h2t obtain ⟨g, hg⟩ := K.interior_nonempty show g ∈ (∅ : Set G) convert h1t (interior_subset hg); symm simp only [Finset.not_mem_empty, iUnion_of_empty, iUnion_empty] · exact index_defined K.isCompact hV @[to_additive addIndex_mono] theorem index_mono {K K' V : Set G} (hK' : IsCompact K') (h : K ⊆ K') (hV : (interior V).Nonempty) : index K V ≤ index K' V := by rcases index_elim hK' hV with ⟨s, h1s, h2s⟩ apply Nat.sInf_le; rw [mem_image]; exact ⟨s, Subset.trans h h1s, h2s⟩ @[to_additive addIndex_union_le] theorem index_union_le (K₁ K₂ : Compacts G) {V : Set G} (hV : (interior V).Nonempty) : index (K₁.1 ∪ K₂.1) V ≤ index K₁.1 V + index K₂.1 V := by classical rcases index_elim K₁.2 hV with ⟨s, h1s, h2s⟩ rcases index_elim K₂.2 hV with ⟨t, h1t, h2t⟩ rw [← h2s, ← h2t] refine le_trans ?_ (Finset.card_union_le _ _) apply Nat.sInf_le; refine ⟨_, ?_, rfl⟩; rw [mem_setOf_eq] apply union_subset <;> refine Subset.trans (by assumption) ?_ <;> apply biUnion_subset_biUnion_left <;> intro g hg <;> simp only [mem_def] at hg <;> simp only [mem_def, Multiset.mem_union, Finset.union_val, hg, or_true, true_or] @[to_additive addIndex_union_eq] theorem index_union_eq (K₁ K₂ : Compacts G) {V : Set G} (hV : (interior V).Nonempty) (h : Disjoint (K₁.1 * V⁻¹) (K₂.1 * V⁻¹)) : index (K₁.1 ∪ K₂.1) V = index K₁.1 V + index K₂.1 V := by classical apply le_antisymm (index_union_le K₁ K₂ hV) rcases index_elim (K₁.2.union K₂.2) hV with ⟨s, h1s, h2s⟩; rw [← h2s] have (K : Set G) (hK : K ⊆ ⋃ g ∈ s, (g * ·) ⁻¹' V) : index K V ≤ {g ∈ s | ((g * ·) ⁻¹' V ∩ K).Nonempty}.card := by apply Nat.sInf_le; refine ⟨_, ?_, rfl⟩; rw [mem_setOf_eq] intro g hg; rcases hK hg with ⟨_, ⟨g₀, rfl⟩, _, ⟨h1g₀, rfl⟩, h2g₀⟩ simp only [mem_preimage] at h2g₀ simp only [mem_iUnion]; use g₀; constructor; swap · simp only [Finset.mem_filter, h1g₀, true_and]; use g simp [hg, h2g₀] exact h2g₀ refine le_trans (add_le_add (this K₁.1 <| Subset.trans subset_union_left h1s) (this K₂.1 <| Subset.trans subset_union_right h1s)) ?_ rw [← Finset.card_union_of_disjoint, Finset.filter_union_right] · exact s.card_filter_le _ apply Finset.disjoint_filter.mpr rintro g₁ _ ⟨g₂, h1g₂, h2g₂⟩ ⟨g₃, h1g₃, h2g₃⟩ simp only [mem_preimage] at h1g₃ h1g₂ refine h.le_bot (?_ : g₁⁻¹ ∈ _) constructor <;> simp only [Set.mem_inv, Set.mem_mul, exists_exists_and_eq_and, exists_and_left] · refine ⟨_, h2g₂, (g₁ * g₂)⁻¹, ?_, ?_⟩ · simp only [inv_inv, h1g₂] · simp only [mul_inv_rev, mul_inv_cancel_left] · refine ⟨_, h2g₃, (g₁ * g₃)⁻¹, ?_, ?_⟩ · simp only [inv_inv, h1g₃] · simp only [mul_inv_rev, mul_inv_cancel_left] @[to_additive add_left_addIndex_le] theorem mul_left_index_le {K : Set G} (hK : IsCompact K) {V : Set G} (hV : (interior V).Nonempty) (g : G) : index ((fun h => g * h) '' K) V ≤ index K V := by rcases index_elim hK hV with ⟨s, h1s, h2s⟩; rw [← h2s] apply Nat.sInf_le; rw [mem_image] refine ⟨s.map (Equiv.mulRight g⁻¹).toEmbedding, ?_, Finset.card_map _⟩ simp only [mem_setOf_eq]; refine Subset.trans (image_subset _ h1s) ?_ rintro _ ⟨g₁, ⟨_, ⟨g₂, rfl⟩, ⟨_, ⟨hg₂, rfl⟩, hg₁⟩⟩, rfl⟩ simp only [mem_preimage] at hg₁ simp only [exists_prop, mem_iUnion, Finset.mem_map, Equiv.coe_mulRight, exists_exists_and_eq_and, mem_preimage, Equiv.toEmbedding_apply] refine ⟨_, hg₂, ?_⟩; simp only [mul_assoc, hg₁, inv_mul_cancel_left] @[to_additive is_left_invariant_addIndex] theorem is_left_invariant_index {K : Set G} (hK : IsCompact K) (g : G) {V : Set G} (hV : (interior V).Nonempty) : index ((fun h => g * h) '' K) V = index K V := by refine le_antisymm (mul_left_index_le hK hV g) ?_ convert mul_left_index_le (hK.image <| continuous_mul_left g) hV g⁻¹ rw [image_image]; symm; convert image_id' _ with h; apply inv_mul_cancel_left /-! ### Lemmas about `prehaar` -/ @[to_additive add_prehaar_le_addIndex] theorem prehaar_le_index (K₀ : PositiveCompacts G) {U : Set G} (K : Compacts G) (hU : (interior U).Nonempty) : prehaar (K₀ : Set G) U K ≤ index (K : Set G) K₀ := by unfold prehaar; rw [div_le_iff₀] <;> norm_cast · apply le_index_mul K₀ K hU · exact index_pos K₀ hU @[to_additive] theorem prehaar_pos (K₀ : PositiveCompacts G) {U : Set G} (hU : (interior U).Nonempty) {K : Set G} (h1K : IsCompact K) (h2K : (interior K).Nonempty) : 0 < prehaar (K₀ : Set G) U ⟨K, h1K⟩ := by apply div_pos <;> norm_cast · apply index_pos ⟨⟨K, h1K⟩, h2K⟩ hU · exact index_pos K₀ hU @[to_additive] theorem prehaar_mono {K₀ : PositiveCompacts G} {U : Set G} (hU : (interior U).Nonempty) {K₁ K₂ : Compacts G} (h : (K₁ : Set G) ⊆ K₂.1) : prehaar (K₀ : Set G) U K₁ ≤ prehaar (K₀ : Set G) U K₂ := by simp only [prehaar]; rw [div_le_div_iff_of_pos_right] · exact mod_cast index_mono K₂.2 h hU · exact mod_cast index_pos K₀ hU @[to_additive] theorem prehaar_self {K₀ : PositiveCompacts G} {U : Set G} (hU : (interior U).Nonempty) : prehaar (K₀ : Set G) U K₀.toCompacts = 1 := div_self <| ne_of_gt <| mod_cast index_pos K₀ hU @[to_additive] theorem prehaar_sup_le {K₀ : PositiveCompacts G} {U : Set G} (K₁ K₂ : Compacts G) (hU : (interior U).Nonempty) : prehaar (K₀ : Set G) U (K₁ ⊔ K₂) ≤ prehaar (K₀ : Set G) U K₁ + prehaar (K₀ : Set G) U K₂ := by simp only [prehaar]; rw [div_add_div_same, div_le_div_iff_of_pos_right] · exact mod_cast index_union_le K₁ K₂ hU · exact mod_cast index_pos K₀ hU @[to_additive] theorem prehaar_sup_eq {K₀ : PositiveCompacts G} {U : Set G} {K₁ K₂ : Compacts G} (hU : (interior U).Nonempty) (h : Disjoint (K₁.1 * U⁻¹) (K₂.1 * U⁻¹)) : prehaar (K₀ : Set G) U (K₁ ⊔ K₂) = prehaar (K₀ : Set G) U K₁ + prehaar (K₀ : Set G) U K₂ := by simp only [prehaar]; rw [div_add_div_same] -- Porting note: Here was `congr`, but `to_additive` failed to generate a theorem. refine congr_arg (fun x : ℝ => x / index K₀ U) ?_ exact mod_cast index_union_eq K₁ K₂ hU h @[to_additive] theorem is_left_invariant_prehaar {K₀ : PositiveCompacts G} {U : Set G} (hU : (interior U).Nonempty) (g : G) (K : Compacts G) : prehaar (K₀ : Set G) U (K.map _ <| continuous_mul_left g) = prehaar (K₀ : Set G) U K := by simp only [prehaar, Compacts.coe_map, is_left_invariant_index K.isCompact _ hU] /-! ### Lemmas about `haarProduct` -/ @[to_additive] theorem prehaar_mem_haarProduct (K₀ : PositiveCompacts G) {U : Set G} (hU : (interior U).Nonempty) : prehaar (K₀ : Set G) U ∈ haarProduct (K₀ : Set G) := by rintro ⟨K, hK⟩ _; rw [mem_Icc]; exact ⟨prehaar_nonneg K₀ _, prehaar_le_index K₀ _ hU⟩ @[to_additive] theorem nonempty_iInter_clPrehaar (K₀ : PositiveCompacts G) : (haarProduct (K₀ : Set G) ∩ ⋂ V : OpenNhdsOf (1 : G), clPrehaar K₀ V).Nonempty := by have : IsCompact (haarProduct (K₀ : Set G)) := by apply isCompact_univ_pi; intro K; apply isCompact_Icc refine this.inter_iInter_nonempty (clPrehaar K₀) (fun s => isClosed_closure) fun t => ?_ let V₀ := ⋂ V ∈ t, (V : OpenNhdsOf (1 : G)).carrier have h1V₀ : IsOpen V₀ := isOpen_biInter_finset <| by rintro ⟨⟨V, hV₁⟩, hV₂⟩ _; exact hV₁ have h2V₀ : (1 : G) ∈ V₀ := by simp only [V₀, mem_iInter]; rintro ⟨⟨V, hV₁⟩, hV₂⟩ _; exact hV₂ refine ⟨prehaar K₀ V₀, ?_⟩ constructor · apply prehaar_mem_haarProduct K₀; use 1; rwa [h1V₀.interior_eq] · simp only [mem_iInter]; rintro ⟨V, hV⟩ h2V; apply subset_closure apply mem_image_of_mem; rw [mem_setOf_eq] exact ⟨Subset.trans (iInter_subset _ ⟨V, hV⟩) (iInter_subset _ h2V), h1V₀, h2V₀⟩ /-! ### Lemmas about `chaar` -/ /-- This is the "limit" of `prehaar K₀ U K` as `U` becomes a smaller and smaller open neighborhood of `(1 : G)`. More precisely, it is defined to be an arbitrary element in the intersection of all the sets `clPrehaar K₀ V` in `haarProduct K₀`. This is roughly equal to the Haar measure on compact sets, but it can differ slightly. We do know that `haarMeasure K₀ (interior K) ≤ chaar K₀ K ≤ haarMeasure K₀ K`. -/ @[to_additive addCHaar "additive version of `MeasureTheory.Measure.haar.chaar`"] noncomputable def chaar (K₀ : PositiveCompacts G) (K : Compacts G) : ℝ := Classical.choose (nonempty_iInter_clPrehaar K₀) K @[to_additive addCHaar_mem_addHaarProduct] theorem chaar_mem_haarProduct (K₀ : PositiveCompacts G) : chaar K₀ ∈ haarProduct (K₀ : Set G) := (Classical.choose_spec (nonempty_iInter_clPrehaar K₀)).1 @[to_additive addCHaar_mem_clAddPrehaar] theorem chaar_mem_clPrehaar (K₀ : PositiveCompacts G) (V : OpenNhdsOf (1 : G)) : chaar K₀ ∈ clPrehaar (K₀ : Set G) V := by have := (Classical.choose_spec (nonempty_iInter_clPrehaar K₀)).2; rw [mem_iInter] at this exact this V @[to_additive addCHaar_nonneg] theorem chaar_nonneg (K₀ : PositiveCompacts G) (K : Compacts G) : 0 ≤ chaar K₀ K := by have := chaar_mem_haarProduct K₀ K (mem_univ _); rw [mem_Icc] at this; exact this.1 @[to_additive addCHaar_empty] theorem chaar_empty (K₀ : PositiveCompacts G) : chaar K₀ ⊥ = 0 := by let eval : (Compacts G → ℝ) → ℝ := fun f => f ⊥ have : Continuous eval := continuous_apply ⊥ show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)} apply mem_of_subset_of_mem _ (chaar_mem_clPrehaar K₀ ⊤)
unfold clPrehaar; rw [IsClosed.closure_subset_iff] · rintro _ ⟨U, _, rfl⟩; apply prehaar_empty · apply continuous_iff_isClosed.mp this; exact isClosed_singleton
Mathlib/MeasureTheory/Measure/Haar/Basic.lean
370
372
/- Copyright (c) 2022 Michael Stoll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Stoll -/ import Mathlib.Data.Fin.Tuple.Sort import Mathlib.Order.WellFounded import Mathlib.Order.PiLex import Mathlib.Data.Finite.Prod /-! # "Bubble sort" induction We implement the following induction principle `Tuple.bubble_sort_induction` on tuples with values in a linear order `α`. Let `f : Fin n → α` and let `P` be a predicate on `Fin n → α`. Then we can show that `f ∘ sort f` satisfies `P` if `f` satisfies `P`, and whenever some `g : Fin n → α` satisfies `P` and `g i > g j` for some `i < j`, then `g ∘ swap i j` also satisfies `P`. We deduce it from a stronger variant `Tuple.bubble_sort_induction'`, which requires the assumption only for `g` that are permutations of `f`. The latter is proved by well-founded induction via `WellFounded.induction_bot'` with respect to the lexicographic ordering on the finite set of all permutations of `f`. -/ namespace Tuple /-- *Bubble sort induction*: Prove that the sorted version of `f` has some property `P` if `f` satisfies `P` and `P` is preserved on permutations of `f` when swapping two antitone values. -/
theorem bubble_sort_induction' {n : ℕ} {α : Type*} [LinearOrder α] {f : Fin n → α} {P : (Fin n → α) → Prop} (hf : P f) (h : ∀ (σ : Equiv.Perm (Fin n)) (i j : Fin n), i < j → (f ∘ σ) j < (f ∘ σ) i → P (f ∘ σ) → P (f ∘ σ ∘ Equiv.swap i j)) : P (f ∘ sort f) := by letI := @Preorder.lift _ (Lex (Fin n → α)) _ fun σ : Equiv.Perm (Fin n) => toLex (f ∘ σ) refine @WellFounded.induction_bot' _ _ _ (IsWellFounded.wf : WellFounded (· < ·)) (Equiv.refl _) (sort f) P (fun σ => f ∘ σ) (fun σ hσ hfσ => ?_) hf obtain ⟨i, j, hij₁, hij₂⟩ := antitone_pair_of_not_sorted' hσ exact ⟨σ * Equiv.swap i j, Pi.lex_desc hij₁.le hij₂, h σ i j hij₁ hij₂ hfσ⟩
Mathlib/Data/Fin/Tuple/BubbleSortInduction.lean
34
44
/- Copyright (c) 2023 Peter Nelson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Peter Nelson -/ import Mathlib.Data.Finite.Prod import Mathlib.Data.Matroid.Init import Mathlib.Data.Set.Card import Mathlib.Data.Set.Finite.Powerset import Mathlib.Order.UpperLower.Closure /-! # Matroids A `Matroid` is a structure that combinatorially abstracts the notion of linear independence and dependence; matroids have connections with graph theory, discrete optimization, additive combinatorics and algebraic geometry. Mathematically, a matroid `M` is a structure on a set `E` comprising a collection of subsets of `E` called the bases of `M`, where the bases are required to obey certain axioms. This file gives a definition of a matroid `M` in terms of its bases, and some API relating independent sets (subsets of bases) and the notion of a basis of a set `X` (a maximal independent subset of `X`). ## Main definitions * a `Matroid α` on a type `α` is a structure comprising a 'ground set' and a suitably behaved 'base' predicate. Given `M : Matroid α` ... * `M.E` denotes the ground set of `M`, which has type `Set α` * For `B : Set α`, `M.IsBase B` means that `B` is a base of `M`. * For `I : Set α`, `M.Indep I` means that `I` is independent in `M` (that is, `I` is contained in a base of `M`). * For `D : Set α`, `M.Dep D` means that `D` is contained in the ground set of `M` but isn't independent. * For `I : Set α` and `X : Set α`, `M.IsBasis I X` means that `I` is a maximal independent subset of `X`. * `M.Finite` means that `M` has finite ground set. * `M.Nonempty` means that the ground set of `M` is nonempty. * `RankFinite M` means that the bases of `M` are finite. * `RankInfinite M` means that the bases of `M` are infinite. * `RankPos M` means that the bases of `M` are nonempty. * `Finitary M` means that a set is independent if and only if all its finite subsets are independent. * `aesop_mat` : a tactic designed to prove `X ⊆ M.E` for some set `X` and matroid `M`. ## Implementation details There are a few design decisions worth discussing. ### Finiteness The first is that our matroids are allowed to be infinite. Unlike with many mathematical structures, this isn't such an obvious choice. Finite matroids have been studied since the 1930's, and there was never controversy as to what is and isn't an example of a finite matroid - in fact, surprisingly many apparently different definitions of a matroid give rise to the same class of objects. However, generalizing different definitions of a finite matroid to the infinite in the obvious way (i.e. by simply allowing the ground set to be infinite) gives a number of different notions of 'infinite matroid' that disagree with each other, and that all lack nice properties. Many different competing notions of infinite matroid were studied through the years; in fact, the problem of which definition is the best was only really solved in 2013, when Bruhn et al. [2] showed that there is a unique 'reasonable' notion of an infinite matroid (these objects had previously defined by Higgs under the name 'B-matroid'). These are defined by adding one carefully chosen axiom to the standard set, and adapting existing axioms to not mention set cardinalities; they enjoy nearly all the nice properties of standard finite matroids. Even though at least 90% of the literature is on finite matroids, B-matroids are the definition we use, because they allow for additional generality, nearly all theorems are still true and just as easy to state, and (hopefully) the more general definition will prevent the need for a costly future refactor. The disadvantage is that developing API for the finite case is harder work (for instance, it is harder to prove that something is a matroid in the first place, and one must deal with `ℕ∞` rather than `ℕ`). For serious work on finite matroids, we provide the typeclasses `[M.Finite]` and `[RankFinite M]` and associated API. ### Cardinality Just as with bases of a vector space, all bases of a finite matroid `M` are finite and have the same cardinality; this cardinality is an important invariant known as the 'rank' of `M`. For infinite matroids, bases are not in general equicardinal; in fact the equicardinality of bases of infinite matroids is independent of ZFC [3]. What is still true is that either all bases are finite and equicardinal, or all bases are infinite. This means that the natural notion of 'size' for a set in matroid theory is given by the function `Set.encard`, which is the cardinality as a term in `ℕ∞`. We use this function extensively in building the API; it is preferable to both `Set.ncard` and `Finset.card` because it allows infinite sets to be handled without splitting into cases. ### The ground `Set` A last place where we make a consequential choice is making the ground set of a matroid a structure field of type `Set α` (where `α` is the type of 'possible matroid elements') rather than just having a type `α` of all the matroid elements. This is because of how common it is to simultaneously consider a number of matroids on different but related ground sets. For example, a matroid `M` on ground set `E` can have its structure 'restricted' to some subset `R ⊆ E` to give a smaller matroid `M ↾ R` with ground set `R`. A statement like `(M ↾ R₁) ↾ R₂ = M ↾ R₂` is mathematically obvious. But if the ground set of a matroid is a type, this doesn't typecheck, and is only true up to canonical isomorphism. Restriction is just the tip of the iceberg here; one can also 'contract' and 'delete' elements and sets of elements in a matroid to give a smaller matroid, and in practice it is common to make statements like `M₁.E = M₂.E ∩ M₃.E` and `((M ⟋ e) ↾ R) ⟋ C = M ⟋ (C ∪ {e}) ↾ R`. Such things are a nightmare to work with unless `=` is actually propositional equality (especially because the relevant coercions are usually between sets and not just elements). So the solution is that the ground set `M.E` has type `Set α`, and there are elements of type `α` that aren't in the matroid. The tradeoff is that for many statements, one now has to add hypotheses of the form `X ⊆ M.E` to make sure than `X` is actually 'in the matroid', rather than letting a 'type of matroid elements' take care of this invisibly. It still seems that this is worth it. The tactic `aesop_mat` exists specifically to discharge such goals with minimal fuss (using default values). The tactic works fairly well, but has room for improvement. A related decision is to not have matroids themselves be a typeclass. This would make things be notationally simpler (having `Base` in the presence of `[Matroid α]` rather than `M.Base` for a term `M : Matroid α`) but is again just too awkward when one has multiple matroids on the same type. In fact, in regular written mathematics, it is normal to explicitly indicate which matroid something is happening in, so our notation mirrors common practice. ### Notation We use a few nonstandard conventions in theorem names that are related to the above. First, we mirror common informal practice by referring explicitly to the `ground` set rather than the notation `E`. (Writing `ground` everywhere in a proof term would be unwieldy, and writing `E` in theorem names would be unnatural to read.) Second, because we are typically interested in subsets of the ground set `M.E`, using `Set.compl` is inconvenient, since `Xᶜ ⊆ M.E` is typically false for `X ⊆ M.E`. On the other hand (especially when duals arise), it is common to complement a set `X ⊆ M.E` *within* the ground set, giving `M.E \ X`. For this reason, we use the term `compl` in theorem names to refer to taking a set difference with respect to the ground set, rather than a complement within a type. The lemma `compl_isBase_dual` is one of the many examples of this. Finally, in theorem names, matroid predicates that apply to sets (such as `Base`, `Indep`, `IsBasis`) are typically used as suffixes rather than prefixes. For instance, we have `ground_indep_iff_isBase` rather than `indep_ground_iff_isBase`. ## References * [J. Oxley, Matroid Theory][oxley2011] * [H. Bruhn, R. Diestel, M. Kriesell, R. Pendavingh, P. Wollan, Axioms for infinite matroids, Adv. Math 239 (2013), 18-46][bruhnDiestelKriesselPendavinghWollan2013] * [N. Bowler, S. Geschke, Self-dual uniform matroids on infinite sets, Proc. Amer. Math. Soc. 144 (2016), 459-471][bowlerGeschke2015] -/ assert_not_exists Field open Set /-- A predicate `P` on sets satisfies the **exchange property** if, for all `X` and `Y` satisfying `P` and all `a ∈ X \ Y`, there exists `b ∈ Y \ X` so that swapping `a` for `b` in `X` maintains `P`. -/ def Matroid.ExchangeProperty {α : Type*} (P : Set α → Prop) : Prop := ∀ X Y, P X → P Y → ∀ a ∈ X \ Y, ∃ b ∈ Y \ X, P (insert b (X \ {a})) /-- A set `X` has the maximal subset property for a predicate `P` if every subset of `X` satisfying `P` is contained in a maximal subset of `X` satisfying `P`. -/ def Matroid.ExistsMaximalSubsetProperty {α : Type*} (P : Set α → Prop) (X : Set α) : Prop := ∀ I, P I → I ⊆ X → ∃ J, I ⊆ J ∧ Maximal (fun K ↦ P K ∧ K ⊆ X) J /-- A `Matroid α` is a ground set `E` of type `Set α`, and a nonempty collection of its subsets satisfying the exchange property and the maximal subset property. Each such set is called a `Base` of `M`. An `Indep`endent set is just a set contained in a base, but we include this predicate as a structure field for better definitional properties. In most cases, using this definition directly is not the best way to construct a matroid, since it requires specifying both the bases and independent sets. If the bases are known, use `Matroid.ofBase` or a variant. If just the independent sets are known, define an `IndepMatroid`, and then use `IndepMatroid.matroid`. -/ structure Matroid (α : Type*) where /-- `M` has a ground set `E`. -/ (E : Set α) /-- `M` has a predicate `Base` defining its bases. -/ (IsBase : Set α → Prop) /-- `M` has a predicate `Indep` defining its independent sets. -/ (Indep : Set α → Prop) /-- The `Indep`endent sets are those contained in `Base`s. -/ (indep_iff' : ∀ ⦃I⦄, Indep I ↔ ∃ B, IsBase B ∧ I ⊆ B) /-- There is at least one `Base`. -/ (exists_isBase : ∃ B, IsBase B) /-- For any bases `B`, `B'` and `e ∈ B \ B'`, there is some `f ∈ B' \ B` for which `B-e+f` is a base. -/ (isBase_exchange : Matroid.ExchangeProperty IsBase) /-- Every independent subset `I` of a set `X` for is contained in a maximal independent subset of `X`. -/ (maximality : ∀ X, X ⊆ E → Matroid.ExistsMaximalSubsetProperty Indep X) /-- Every base is contained in the ground set. -/ (subset_ground : ∀ B, IsBase B → B ⊆ E) attribute [local ext] Matroid namespace Matroid variable {α : Type*} {M : Matroid α} @[deprecated (since := "2025-02-14")] alias Base := IsBase instance (M : Matroid α) : Nonempty {B // M.IsBase B} := nonempty_subtype.2 M.exists_isBase /-- Typeclass for a matroid having finite ground set. Just a wrapper for `M.E.Finite`. -/ @[mk_iff] protected class Finite (M : Matroid α) : Prop where /-- The ground set is finite -/ (ground_finite : M.E.Finite) /-- Typeclass for a matroid having nonempty ground set. Just a wrapper for `M.E.Nonempty`. -/ protected class Nonempty (M : Matroid α) : Prop where /-- The ground set is nonempty -/ (ground_nonempty : M.E.Nonempty) theorem ground_nonempty (M : Matroid α) [M.Nonempty] : M.E.Nonempty := Nonempty.ground_nonempty theorem ground_nonempty_iff (M : Matroid α) : M.E.Nonempty ↔ M.Nonempty := ⟨fun h ↦ ⟨h⟩, fun ⟨h⟩ ↦ h⟩ lemma nonempty_type (M : Matroid α) [h : M.Nonempty] : Nonempty α := ⟨M.ground_nonempty.some⟩ theorem ground_finite (M : Matroid α) [M.Finite] : M.E.Finite := Finite.ground_finite theorem set_finite (M : Matroid α) [M.Finite] (X : Set α) (hX : X ⊆ M.E := by aesop) : X.Finite := M.ground_finite.subset hX instance finite_of_finite [Finite α] {M : Matroid α} : M.Finite := ⟨Set.toFinite _⟩ /-- A `RankFinite` matroid is one whose bases are finite -/ @[mk_iff] class RankFinite (M : Matroid α) : Prop where /-- There is a finite base -/ exists_finite_isBase : ∃ B, M.IsBase B ∧ B.Finite @[deprecated (since := "2025-02-09")] alias FiniteRk := RankFinite instance rankFinite_of_finite (M : Matroid α) [M.Finite] : RankFinite M := ⟨M.exists_isBase.imp (fun B hB ↦ ⟨hB, M.set_finite B (M.subset_ground _ hB)⟩)⟩ /-- An `RankInfinite` matroid is one whose bases are infinite. -/ @[mk_iff] class RankInfinite (M : Matroid α) : Prop where /-- There is an infinite base -/ exists_infinite_isBase : ∃ B, M.IsBase B ∧ B.Infinite @[deprecated (since := "2025-02-09")] alias InfiniteRk := RankInfinite /-- A `RankPos` matroid is one whose bases are nonempty. -/ @[mk_iff] class RankPos (M : Matroid α) : Prop where /-- The empty set isn't a base -/ empty_not_isBase : ¬M.IsBase ∅ @[deprecated (since := "2025-02-09")] alias RkPos := RankPos instance rankPos_nonempty {M : Matroid α} [M.RankPos] : M.Nonempty := by obtain ⟨B, hB⟩ := M.exists_isBase obtain rfl | ⟨e, heB⟩ := B.eq_empty_or_nonempty · exact False.elim <| RankPos.empty_not_isBase hB exact ⟨e, M.subset_ground B hB heB ⟩ @[deprecated (since := "2025-01-20")] alias rkPos_iff_empty_not_base := rankPos_iff section exchange namespace ExchangeProperty variable {IsBase : Set α → Prop} {B B' : Set α} /-- A family of sets with the exchange property is an antichain. -/ theorem antichain (exch : ExchangeProperty IsBase) (hB : IsBase B) (hB' : IsBase B') (h : B ⊆ B') : B = B' := h.antisymm (fun x hx ↦ by_contra (fun hxB ↦ let ⟨_, hy, _⟩ := exch B' B hB' hB x ⟨hx, hxB⟩; hy.2 <| h hy.1)) theorem encard_diff_le_aux {B₁ B₂ : Set α} (exch : ExchangeProperty IsBase) (hB₁ : IsBase B₁) (hB₂ : IsBase B₂) : (B₁ \ B₂).encard ≤ (B₂ \ B₁).encard := by obtain (he | hinf | ⟨e, he, hcard⟩) := (B₂ \ B₁).eq_empty_or_encard_eq_top_or_encard_diff_singleton_lt · rw [exch.antichain hB₂ hB₁ (diff_eq_empty.mp he)] · exact le_top.trans_eq hinf.symm obtain ⟨f, hf, hB'⟩ := exch B₂ B₁ hB₂ hB₁ e he have : encard (insert f (B₂ \ {e}) \ B₁) < encard (B₂ \ B₁) := by rw [insert_diff_of_mem _ hf.1, diff_diff_comm]; exact hcard have hencard := encard_diff_le_aux exch hB₁ hB' rw [insert_diff_of_mem _ hf.1, diff_diff_comm, ← union_singleton, ← diff_diff, diff_diff_right, inter_singleton_eq_empty.mpr he.2, union_empty] at hencard rw [← encard_diff_singleton_add_one he, ← encard_diff_singleton_add_one hf] exact add_le_add_right hencard 1 termination_by (B₂ \ B₁).encard variable {B₁ B₂ : Set α} /-- For any two sets `B₁`, `B₂` in a family with the exchange property, the differences `B₁ \ B₂` and `B₂ \ B₁` have the same `ℕ∞`-cardinality. -/ theorem encard_diff_eq (exch : ExchangeProperty IsBase) (hB₁ : IsBase B₁) (hB₂ : IsBase B₂) : (B₁ \ B₂).encard = (B₂ \ B₁).encard := (encard_diff_le_aux exch hB₁ hB₂).antisymm (encard_diff_le_aux exch hB₂ hB₁) /-- Any two sets `B₁`, `B₂` in a family with the exchange property have the same `ℕ∞`-cardinality. -/ theorem encard_isBase_eq (exch : ExchangeProperty IsBase) (hB₁ : IsBase B₁) (hB₂ : IsBase B₂) : B₁.encard = B₂.encard := by rw [← encard_diff_add_encard_inter B₁ B₂, exch.encard_diff_eq hB₁ hB₂, inter_comm, encard_diff_add_encard_inter] end ExchangeProperty end exchange section aesop /-- The `aesop_mat` tactic attempts to prove a set is contained in the ground set of a matroid. It uses a `[Matroid]` ruleset, and is allowed to fail. -/ macro (name := aesop_mat) "aesop_mat" c:Aesop.tactic_clause* : tactic => `(tactic| aesop $c* (config := { terminal := true }) (rule_sets := [$(Lean.mkIdent `Matroid):ident])) /- We add a number of trivial lemmas (deliberately specialized to statements in terms of the ground set of a matroid) to the ruleset `Matroid` for `aesop`. -/ variable {X Y : Set α} {e : α} @[aesop unsafe 5% (rule_sets := [Matroid])] private theorem inter_right_subset_ground (hX : X ⊆ M.E) : X ∩ Y ⊆ M.E := inter_subset_left.trans hX @[aesop unsafe 5% (rule_sets := [Matroid])] private theorem inter_left_subset_ground (hX : X ⊆ M.E) : Y ∩ X ⊆ M.E := inter_subset_right.trans hX @[aesop unsafe 5% (rule_sets := [Matroid])] private theorem diff_subset_ground (hX : X ⊆ M.E) : X \ Y ⊆ M.E := diff_subset.trans hX @[aesop unsafe 10% (rule_sets := [Matroid])] private theorem ground_diff_subset_ground : M.E \ X ⊆ M.E := diff_subset_ground rfl.subset @[aesop unsafe 10% (rule_sets := [Matroid])] private theorem singleton_subset_ground (he : e ∈ M.E) : {e} ⊆ M.E := singleton_subset_iff.mpr he @[aesop unsafe 5% (rule_sets := [Matroid])] private theorem subset_ground_of_subset (hXY : X ⊆ Y) (hY : Y ⊆ M.E) : X ⊆ M.E := hXY.trans hY @[aesop unsafe 5% (rule_sets := [Matroid])] private theorem mem_ground_of_mem_of_subset (hX : X ⊆ M.E) (heX : e ∈ X) : e ∈ M.E := hX heX @[aesop safe (rule_sets := [Matroid])] private theorem insert_subset_ground {e : α} {X : Set α} {M : Matroid α} (he : e ∈ M.E) (hX : X ⊆ M.E) : insert e X ⊆ M.E := insert_subset he hX @[aesop safe (rule_sets := [Matroid])] private theorem ground_subset_ground {M : Matroid α} : M.E ⊆ M.E := rfl.subset attribute [aesop safe (rule_sets := [Matroid])] empty_subset union_subset iUnion_subset end aesop section IsBase variable {B B₁ B₂ : Set α} @[aesop unsafe 10% (rule_sets := [Matroid])] theorem IsBase.subset_ground (hB : M.IsBase B) : B ⊆ M.E := M.subset_ground B hB theorem IsBase.exchange {e : α} (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) (hx : e ∈ B₁ \ B₂) : ∃ y ∈ B₂ \ B₁, M.IsBase (insert y (B₁ \ {e})) := M.isBase_exchange B₁ B₂ hB₁ hB₂ _ hx theorem IsBase.exchange_mem {e : α} (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) (hxB₁ : e ∈ B₁) (hxB₂ : e ∉ B₂) : ∃ y, (y ∈ B₂ ∧ y ∉ B₁) ∧ M.IsBase (insert y (B₁ \ {e})) := by simpa using hB₁.exchange hB₂ ⟨hxB₁, hxB₂⟩ theorem IsBase.eq_of_subset_isBase (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) (hB₁B₂ : B₁ ⊆ B₂) : B₁ = B₂ := M.isBase_exchange.antichain hB₁ hB₂ hB₁B₂ theorem IsBase.not_isBase_of_ssubset {X : Set α} (hB : M.IsBase B) (hX : X ⊂ B) : ¬ M.IsBase X := fun h ↦ hX.ne (h.eq_of_subset_isBase hB hX.subset) theorem IsBase.insert_not_isBase {e : α} (hB : M.IsBase B) (heB : e ∉ B) : ¬ M.IsBase (insert e B) := fun h ↦ h.not_isBase_of_ssubset (ssubset_insert heB) hB theorem IsBase.encard_diff_comm (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) : (B₁ \ B₂).encard = (B₂ \ B₁).encard := M.isBase_exchange.encard_diff_eq hB₁ hB₂ theorem IsBase.ncard_diff_comm (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) : (B₁ \ B₂).ncard = (B₂ \ B₁).ncard := by rw [ncard_def, hB₁.encard_diff_comm hB₂, ← ncard_def] theorem IsBase.encard_eq_encard_of_isBase (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) : B₁.encard = B₂.encard := by rw [M.isBase_exchange.encard_isBase_eq hB₁ hB₂] theorem IsBase.ncard_eq_ncard_of_isBase (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) : B₁.ncard = B₂.ncard := by rw [ncard_def B₁, hB₁.encard_eq_encard_of_isBase hB₂, ← ncard_def] theorem IsBase.finite_of_finite {B' : Set α} (hB : M.IsBase B) (h : B.Finite) (hB' : M.IsBase B') : B'.Finite := (finite_iff_finite_of_encard_eq_encard (hB.encard_eq_encard_of_isBase hB')).mp h theorem IsBase.infinite_of_infinite (hB : M.IsBase B) (h : B.Infinite) (hB₁ : M.IsBase B₁) : B₁.Infinite := by_contra (fun hB_inf ↦ (hB₁.finite_of_finite (not_infinite.mp hB_inf) hB).not_infinite h) theorem IsBase.finite [RankFinite M] (hB : M.IsBase B) : B.Finite := let ⟨_,hB₀⟩ := ‹RankFinite M›.exists_finite_isBase hB₀.1.finite_of_finite hB₀.2 hB theorem IsBase.infinite [RankInfinite M] (hB : M.IsBase B) : B.Infinite := let ⟨_,hB₀⟩ := ‹RankInfinite M›.exists_infinite_isBase hB₀.1.infinite_of_infinite hB₀.2 hB theorem empty_not_isBase [h : RankPos M] : ¬M.IsBase ∅ := h.empty_not_isBase theorem IsBase.nonempty [RankPos M] (hB : M.IsBase B) : B.Nonempty := by rw [nonempty_iff_ne_empty]; rintro rfl; exact M.empty_not_isBase hB theorem IsBase.rankPos_of_nonempty (hB : M.IsBase B) (h : B.Nonempty) : M.RankPos := by rw [rankPos_iff] intro he obtain rfl := he.eq_of_subset_isBase hB (empty_subset B) simp at h theorem IsBase.rankFinite_of_finite (hB : M.IsBase B) (hfin : B.Finite) : RankFinite M := ⟨⟨B, hB, hfin⟩⟩ theorem IsBase.rankInfinite_of_infinite (hB : M.IsBase B) (h : B.Infinite) : RankInfinite M := ⟨⟨B, hB, h⟩⟩ theorem not_rankFinite (M : Matroid α) [RankInfinite M] : ¬ RankFinite M := by intro h; obtain ⟨B,hB⟩ := M.exists_isBase; exact hB.infinite hB.finite theorem not_rankInfinite (M : Matroid α) [RankFinite M] : ¬ RankInfinite M := by intro h; obtain ⟨B,hB⟩ := M.exists_isBase; exact hB.infinite hB.finite theorem rankFinite_or_rankInfinite (M : Matroid α) : RankFinite M ∨ RankInfinite M := let ⟨B, hB⟩ := M.exists_isBase B.finite_or_infinite.imp hB.rankFinite_of_finite hB.rankInfinite_of_infinite @[deprecated (since := "2025-03-27")] alias finite_or_rankInfinite := rankFinite_or_rankInfinite @[simp] theorem not_rankFinite_iff (M : Matroid α) : ¬ RankFinite M ↔ RankInfinite M := M.rankFinite_or_rankInfinite.elim (fun h ↦ iff_of_false (by simpa) M.not_rankInfinite) fun h ↦ iff_of_true M.not_rankFinite h @[simp] theorem not_rankInfinite_iff (M : Matroid α) : ¬ RankInfinite M ↔ RankFinite M := by rw [← not_rankFinite_iff, not_not] theorem IsBase.diff_finite_comm (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) : (B₁ \ B₂).Finite ↔ (B₂ \ B₁).Finite := finite_iff_finite_of_encard_eq_encard (hB₁.encard_diff_comm hB₂)
theorem IsBase.diff_infinite_comm (hB₁ : M.IsBase B₁) (hB₂ : M.IsBase B₂) : (B₁ \ B₂).Infinite ↔ (B₂ \ B₁).Infinite := infinite_iff_infinite_of_encard_eq_encard (hB₁.encard_diff_comm hB₂)
Mathlib/Data/Matroid/Basic.lean
488
490
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau -/ import Mathlib.Data.List.Forall2 /-! # zip & unzip This file provides results about `List.zipWith`, `List.zip` and `List.unzip` (definitions are in core Lean). `zipWith f l₁ l₂` applies `f : α → β → γ` pointwise to a list `l₁ : List α` and `l₂ : List β`. It applies, until one of the lists is exhausted. For example, `zipWith f [0, 1, 2] [6.28, 31] = [f 0 6.28, f 1 31]`. `zip` is `zipWith` applied to `Prod.mk`. For example, `zip [a₁, a₂] [b₁, b₂, b₃] = [(a₁, b₁), (a₂, b₂)]`. `unzip` undoes `zip`. For example, `unzip [(a₁, b₁), (a₂, b₂)] = ([a₁, a₂], [b₁, b₂])`. -/ -- Make sure we don't import algebra assert_not_exists Monoid universe u open Nat namespace List variable {α : Type u} {β γ δ ε : Type*} @[simp] theorem zip_swap : ∀ (l₁ : List α) (l₂ : List β), (zip l₁ l₂).map Prod.swap = zip l₂ l₁ | [], _ => zip_nil_right.symm | l₁, [] => by rw [zip_nil_right]; rfl | a :: l₁, b :: l₂ => by simp only [zip_cons_cons, map_cons, zip_swap l₁ l₂, Prod.swap_prod_mk] theorem forall_zipWith {f : α → β → γ} {p : γ → Prop} : ∀ {l₁ : List α} {l₂ : List β}, length l₁ = length l₂ → (Forall p (zipWith f l₁ l₂) ↔ Forall₂ (fun x y => p (f x y)) l₁ l₂) | [], [], _ => by simp | a :: l₁, b :: l₂, h => by simp only [length_cons, succ_inj] at h simp [forall_zipWith h] theorem unzip_swap (l : List (α × β)) : unzip (l.map Prod.swap) = (unzip l).swap := by simp only [unzip_eq_map, map_map] rfl @[congr] theorem zipWith_congr (f g : α → β → γ) (la : List α) (lb : List β) (h : List.Forall₂ (fun a b => f a b = g a b) la lb) : zipWith f la lb = zipWith g la lb := by induction h with | nil => rfl | cons hfg _ ih => exact congr_arg₂ _ hfg ih theorem zipWith_zipWith_left (f : δ → γ → ε) (g : α → β → δ) : ∀ (la : List α) (lb : List β) (lc : List γ), zipWith f (zipWith g la lb) lc = zipWith3 (fun a b c => f (g a b) c) la lb lc | [], _, _ => rfl | _ :: _, [], _ => rfl | _ :: _, _ :: _, [] => rfl | _ :: as, _ :: bs, _ :: cs => congr_arg (cons _) <| zipWith_zipWith_left f g as bs cs theorem zipWith_zipWith_right (f : α → δ → ε) (g : β → γ → δ) : ∀ (la : List α) (lb : List β) (lc : List γ), zipWith f la (zipWith g lb lc) = zipWith3 (fun a b c => f a (g b c)) la lb lc | [], _, _ => rfl | _ :: _, [], _ => rfl | _ :: _, _ :: _, [] => rfl | _ :: as, _ :: bs, _ :: cs => congr_arg (cons _) <| zipWith_zipWith_right f g as bs cs @[simp] theorem zipWith3_same_left (f : α → α → β → γ) : ∀ (la : List α) (lb : List β), zipWith3 f la la lb = zipWith (fun a b => f a a b) la lb | [], _ => rfl | _ :: _, [] => rfl | _ :: as, _ :: bs => congr_arg (cons _) <| zipWith3_same_left f as bs @[simp] theorem zipWith3_same_mid (f : α → β → α → γ) : ∀ (la : List α) (lb : List β), zipWith3 f la lb la = zipWith (fun a b => f a b a) la lb | [], _ => rfl | _ :: _, [] => rfl | _ :: as, _ :: bs => congr_arg (cons _) <| zipWith3_same_mid f as bs @[simp] theorem zipWith3_same_right (f : α → β → β → γ) : ∀ (la : List α) (lb : List β), zipWith3 f la lb lb = zipWith (fun a b => f a b b) la lb | [], _ => rfl | _ :: _, [] => rfl | _ :: as, _ :: bs => congr_arg (cons _) <| zipWith3_same_right f as bs instance (f : α → α → β) [IsSymmOp f] : IsSymmOp (zipWith f) := ⟨fun _ _ => zipWith_comm_of_comm IsSymmOp.symm_op⟩ @[simp] theorem length_revzip (l : List α) : length (revzip l) = length l := by simp only [revzip, length_zip, length_reverse, min_self] @[simp] theorem unzip_revzip (l : List α) : (revzip l).unzip = (l, l.reverse) := unzip_zip length_reverse.symm @[simp] theorem revzip_map_fst (l : List α) : (revzip l).map Prod.fst = l := by rw [← unzip_fst, unzip_revzip] @[simp] theorem revzip_map_snd (l : List α) : (revzip l).map Prod.snd = l.reverse := by rw [← unzip_snd, unzip_revzip] theorem reverse_revzip (l : List α) : reverse l.revzip = revzip l.reverse := by rw [← zip_unzip (revzip l).reverse] simp [unzip_eq_map, revzip, map_reverse, map_fst_zip, map_snd_zip] theorem revzip_swap (l : List α) : (revzip l).map Prod.swap = revzip l.reverse := by simp [revzip] @[deprecated (since := "2025-02-14")] alias get?_zipWith' := getElem?_zipWith' @[deprecated (since := "2025-02-14")] alias get?_zipWith_eq_some := getElem?_zipWith_eq_some @[deprecated (since := "2025-02-14")] alias get?_zip_eq_some := getElem?_zip_eq_some theorem mem_zip_inits_tails {l : List α} {init tail : List α} : (init, tail) ∈ zip l.inits l.tails ↔ init ++ tail = l := by induction' l with hd tl ih generalizing init tail <;> simp_rw [tails, inits, zip_cons_cons] · simp · constructor <;> rw [mem_cons, zip_map_left, mem_map, Prod.exists] · rintro (⟨rfl, rfl⟩ | ⟨_, _, h, rfl, rfl⟩) · simp · simp [ih.mp h] · rcases init with - | ⟨hd', tl'⟩ · rintro rfl simp · intro h right use tl', tail simp_all end List
Mathlib/Data/List/Zip.lean
262
268
/- 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, Patrick Massot, Yury Kudryashov -/ import Mathlib.Tactic.ApplyFun import Mathlib.Topology.Separation.Regular import Mathlib.Topology.UniformSpace.Basic /-! # Hausdorff properties of uniform spaces. Separation quotient. Two points of a topological space are called `Inseparable`, if their neighborhoods filter are equal. Equivalently, `Inseparable x y` means that any open set that contains `x` must contain `y` and vice versa. In a uniform space, points `x` and `y` are inseparable if and only if `(x, y)` belongs to all entourages, see `inseparable_iff_ker_uniformity`. A uniform space is a regular topological space, hence separation axioms `T0Space`, `T1Space`, `T2Space`, and `T3Space` are equivalent for uniform spaces, and Lean typeclass search can automatically convert from one assumption to another. We say that a uniform space is *separated*, if it satisfies these axioms. If you need an `Iff` statement (e.g., to rewrite), then see `R1Space.t0Space_iff_t2Space` and `RegularSpace.t0Space_iff_t3Space`. In this file we prove several facts that relate `Inseparable` and `Specializes` to the uniformity filter. Most of them are simple corollaries of `Filter.HasBasis.inseparable_iff_uniformity` for different filter bases of `𝓤 α`. Then we study the Kolmogorov quotient `SeparationQuotient X` of a uniform space. For a general topological space, this quotient is defined as the quotient by `Inseparable` equivalence relation. It is the maximal T₀ quotient of a topological space. In case of a uniform space, we equip this quotient with a `UniformSpace` structure that agrees with the quotient topology. We also prove that the quotient map induces uniformity on the original space. Finally, we turn `SeparationQuotient` into a functor (not in terms of `CategoryTheory.Functor` to avoid extra imports) by defining `SeparationQuotient.lift'` and `SeparationQuotient.map` operations. ## Main definitions * `SeparationQuotient.instUniformSpace`: uniform space structure on `SeparationQuotient α`, where `α` is a uniform space; * `SeparationQuotient.lift'`: given a map `f : α → β` from a uniform space to a separated uniform space, lift it to a map `SeparationQuotient α → β`; if the original map is not uniformly continuous, then returns a constant map. * `SeparationQuotient.map`: given a map `f : α → β` between uniform spaces, returns a map `SeparationQuotient α → SeparationQuotient β`. If the original map is not uniformly continuous, then returns a constant map. Otherwise, `SeparationQuotient.map f (SeparationQuotient.mk x) = SeparationQuotient.mk (f x)`. ## Main results * `SeparationQuotient.uniformity_eq`: the uniformity filter on `SeparationQuotient α` is the push forward of the uniformity filter on `α`. * `SeparationQuotient.comap_mk_uniformity`: the quotient map `α → SeparationQuotient α` induces uniform space structure on the original space. * `SeparationQuotient.uniformContinuous_lift'`: factoring a uniformly continuous map through the separation quotient gives a uniformly continuous map. * `SeparationQuotient.uniformContinuous_map`: maps induced between separation quotients are uniformly continuous. ## Implementation notes This files used to contain definitions of `separationRel α` and `UniformSpace.SeparationQuotient α`. These definitions were equal (but not definitionally equal) to `{x : α × α | Inseparable x.1 x.2}` and `SeparationQuotient α`, respectively, and were added to the library before their geneeralizations to topological spaces. In https://github.com/leanprover-community/mathlib4/pull/10644, we migrated from these definitions to more general `Inseparable` and `SeparationQuotient`. ## TODO Definitions `SeparationQuotient.lift'` and `SeparationQuotient.map` rely on `UniformSpace` structures in the domain and in the codomain. We should generalize them to topological spaces. This generalization will drop `UniformContinuous` assumptions in some lemmas, and add these assumptions in other lemmas, so it was not done in https://github.com/leanprover-community/mathlib4/pull/10644 to keep it reasonably sized. ## Keywords uniform space, separated space, Hausdorff space, separation quotient -/ open Filter Set Function Topology Uniformity UniformSpace noncomputable section universe u v w variable {α : Type u} {β : Type v} {γ : Type w} variable [UniformSpace α] [UniformSpace β] [UniformSpace γ] /-! ### Separated uniform spaces -/ instance (priority := 100) UniformSpace.to_regularSpace : RegularSpace α := .of_hasBasis (fun _ ↦ nhds_basis_uniformity' uniformity_hasBasis_closed) fun a _V hV ↦ isClosed_ball a hV.2 theorem Filter.HasBasis.specializes_iff_uniformity {ι : Sort*} {p : ι → Prop} {s : ι → Set (α × α)} (h : (𝓤 α).HasBasis p s) {x y : α} : x ⤳ y ↔ ∀ i, p i → (x, y) ∈ s i := (nhds_basis_uniformity h).specializes_iff theorem Filter.HasBasis.inseparable_iff_uniformity {ι : Sort*} {p : ι → Prop} {s : ι → Set (α × α)} (h : (𝓤 α).HasBasis p s) {x y : α} : Inseparable x y ↔ ∀ i, p i → (x, y) ∈ s i := specializes_iff_inseparable.symm.trans h.specializes_iff_uniformity theorem inseparable_iff_ker_uniformity {x y : α} : Inseparable x y ↔ (x, y) ∈ (𝓤 α).ker := (𝓤 α).basis_sets.inseparable_iff_uniformity protected theorem Inseparable.nhds_le_uniformity {x y : α} (h : Inseparable x y) : 𝓝 (x, y) ≤ 𝓤 α := by rw [h.prod rfl] apply nhds_le_uniformity theorem inseparable_iff_clusterPt_uniformity {x y : α} : Inseparable x y ↔ ClusterPt (x, y) (𝓤 α) := by refine ⟨fun h ↦ .of_nhds_le h.nhds_le_uniformity, fun h ↦ ?_⟩ simp_rw [uniformity_hasBasis_closed.inseparable_iff_uniformity, isClosed_iff_clusterPt] exact fun U ⟨hU, hUc⟩ ↦ hUc _ <| h.mono <| le_principal_iff.2 hU theorem t0Space_iff_uniformity : T0Space α ↔ ∀ x y, (∀ r ∈ 𝓤 α, (x, y) ∈ r) → x = y := by simp only [t0Space_iff_inseparable, inseparable_iff_ker_uniformity, mem_ker, id] theorem t0Space_iff_uniformity' : T0Space α ↔ Pairwise fun x y ↦ ∃ r ∈ 𝓤 α, (x, y) ∉ r := by simp [t0Space_iff_not_inseparable, inseparable_iff_ker_uniformity] theorem t0Space_iff_ker_uniformity : T0Space α ↔ (𝓤 α).ker = diagonal α := by simp_rw [t0Space_iff_uniformity, subset_antisymm_iff, diagonal_subset_iff, subset_def, Prod.forall, Filter.mem_ker, mem_diagonal_iff, iff_self_and] exact fun _ x s hs ↦ refl_mem_uniformity hs theorem eq_of_uniformity {α : Type*} [UniformSpace α] [T0Space α] {x y : α} (h : ∀ {V}, V ∈ 𝓤 α → (x, y) ∈ V) : x = y := t0Space_iff_uniformity.mp ‹T0Space α› x y @h theorem eq_of_uniformity_basis {α : Type*} [UniformSpace α] [T0Space α] {ι : Sort*} {p : ι → Prop} {s : ι → Set (α × α)} (hs : (𝓤 α).HasBasis p s) {x y : α} (h : ∀ {i}, p i → (x, y) ∈ s i) : x = y := (hs.inseparable_iff_uniformity.2 @h).eq theorem eq_of_forall_symmetric {α : Type*} [UniformSpace α] [T0Space α] {x y : α} (h : ∀ {V}, V ∈ 𝓤 α → IsSymmetricRel V → (x, y) ∈ V) : x = y := eq_of_uniformity_basis hasBasis_symmetric (by simpa) theorem eq_of_clusterPt_uniformity [T0Space α] {x y : α} (h : ClusterPt (x, y) (𝓤 α)) : x = y := (inseparable_iff_clusterPt_uniformity.2 h).eq theorem Filter.Tendsto.inseparable_iff_uniformity {β} {l : Filter β} [NeBot l] {f g : β → α} {a b : α} (ha : Tendsto f l (𝓝 a)) (hb : Tendsto g l (𝓝 b)) : Inseparable a b ↔ Tendsto (fun x ↦ (f x, g x)) l (𝓤 α) := by refine ⟨fun h ↦ (ha.prodMk_nhds hb).mono_right h.nhds_le_uniformity, fun h ↦ ?_⟩ rw [inseparable_iff_clusterPt_uniformity] exact (ClusterPt.of_le_nhds (ha.prodMk_nhds hb)).mono h theorem isClosed_of_spaced_out [T0Space α] {V₀ : Set (α × α)} (V₀_in : V₀ ∈ 𝓤 α) {s : Set α} (hs : s.Pairwise fun x y => (x, y) ∉ V₀) : IsClosed s := by rcases comp_symm_mem_uniformity_sets V₀_in with ⟨V₁, V₁_in, V₁_symm, h_comp⟩ apply isClosed_of_closure_subset intro x hx rw [mem_closure_iff_ball] at hx rcases hx V₁_in with ⟨y, hy, hy'⟩ suffices x = y by rwa [this] apply eq_of_forall_symmetric intro V V_in _ rcases hx (inter_mem V₁_in V_in) with ⟨z, hz, hz'⟩ obtain rfl : z = y := by by_contra hzy exact hs hz' hy' hzy (h_comp <| mem_comp_of_mem_ball V₁_symm (ball_inter_left x _ _ hz) hy) exact ball_inter_right x _ _ hz theorem isClosed_range_of_spaced_out {ι} [T0Space α] {V₀ : Set (α × α)} (V₀_in : V₀ ∈ 𝓤 α) {f : ι → α} (hf : Pairwise fun x y => (f x, f y) ∉ V₀) : IsClosed (range f) := isClosed_of_spaced_out V₀_in <| by rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ h exact hf (ne_of_apply_ne f h) /-! ### Separation quotient -/ namespace SeparationQuotient theorem comap_map_mk_uniformity : comap (Prod.map mk mk) (map (Prod.map mk mk) (𝓤 α)) = 𝓤 α := by refine le_antisymm ?_ le_comap_map refine ((((𝓤 α).basis_sets.map _).comap _).le_basis_iff uniformity_hasBasis_open).2 fun U hU ↦ ?_ refine ⟨U, hU.1, fun (x₁, x₂) ⟨(y₁, y₂), hyU, hxy⟩ ↦ ?_⟩ simp only [Prod.map, Prod.ext_iff, mk_eq_mk] at hxy exact ((hxy.1.prod hxy.2).mem_open_iff hU.2).1 hyU instance instUniformSpace : UniformSpace (SeparationQuotient α) where uniformity := map (Prod.map mk mk) (𝓤 α) symm := tendsto_map' <| tendsto_map.comp tendsto_swap_uniformity comp := fun t ht ↦ by rcases comp_open_symm_mem_uniformity_sets ht with ⟨U, hU, hUo, -, hUt⟩ refine mem_of_superset (mem_lift' <| image_mem_map hU) ?_ simp only [subset_def, Prod.forall, mem_compRel, mem_image, Prod.ext_iff] rintro _ _ ⟨_, ⟨⟨x, y⟩, hxyU, rfl, rfl⟩, ⟨⟨y', z⟩, hyzU, hy, rfl⟩⟩ have : y' ⤳ y := (mk_eq_mk.1 hy).specializes exact @hUt (x, z) ⟨y', this.mem_open (UniformSpace.isOpen_ball _ hUo) hxyU, hyzU⟩ nhds_eq_comap_uniformity := surjective_mk.forall.2 fun x ↦ comap_injective surjective_mk <| by conv_lhs => rw [comap_mk_nhds_mk, nhds_eq_comap_uniformity, ← comap_map_mk_uniformity] simp only [Filter.comap_comap, Function.comp_def, Prod.map_apply] theorem uniformity_eq : 𝓤 (SeparationQuotient α) = (𝓤 α).map (Prod.map mk mk) := rfl theorem uniformContinuous_mk : UniformContinuous (mk : α → SeparationQuotient α) := le_rfl theorem uniformContinuous_dom {f : SeparationQuotient α → β} : UniformContinuous f ↔ UniformContinuous (f ∘ mk) := .rfl theorem uniformContinuous_dom₂ {f : SeparationQuotient α × SeparationQuotient β → γ} : UniformContinuous f ↔ UniformContinuous fun p : α × β ↦ f (mk p.1, mk p.2) := by simp only [UniformContinuous, uniformity_prod_eq_prod, uniformity_eq, prod_map_map_eq, tendsto_map'_iff] rfl theorem uniformContinuous_lift {f : α → β} (h : ∀ a b, Inseparable a b → f a = f b) : UniformContinuous (lift f h) ↔ UniformContinuous f := .rfl theorem uniformContinuous_uncurry_lift₂ {f : α → β → γ} (h : ∀ a c b d, Inseparable a b → Inseparable c d → f a c = f b d) : UniformContinuous (uncurry <| lift₂ f h) ↔ UniformContinuous (uncurry f) := uniformContinuous_dom₂ theorem comap_mk_uniformity : (𝓤 (SeparationQuotient α)).comap (Prod.map mk mk) = 𝓤 α := comap_map_mk_uniformity open Classical in /-- Factoring functions to a separated space through the separation quotient. TODO: unify with `SeparationQuotient.lift`. -/ def lift' [T0Space β] (f : α → β) : SeparationQuotient α → β := if hc : UniformContinuous f then lift f fun _ _ h => (h.map hc.continuous).eq else fun x => f (Nonempty.some ⟨x.out⟩) theorem lift'_mk [T0Space β] {f : α → β} (h : UniformContinuous f) (a : α) : lift' f (mk a) = f a := by rw [lift', dif_pos h, lift_mk] theorem uniformContinuous_lift' [T0Space β] (f : α → β) : UniformContinuous (lift' f) := by by_cases hf : UniformContinuous f · rwa [lift', dif_pos hf, uniformContinuous_lift] · rw [lift', dif_neg hf] exact uniformContinuous_of_const fun a _ => rfl /-- The separation quotient functor acting on functions. -/ def map (f : α → β) : SeparationQuotient α → SeparationQuotient β := lift' (mk ∘ f) theorem map_mk {f : α → β} (h : UniformContinuous f) (a : α) : map f (mk a) = mk (f a) := by rw [map, lift'_mk (uniformContinuous_mk.comp h)]; rfl theorem uniformContinuous_map (f : α → β) : UniformContinuous (map f) := uniformContinuous_lift' _ theorem map_unique {f : α → β} (hf : UniformContinuous f) {g : SeparationQuotient α → SeparationQuotient β} (comm : mk ∘ f = g ∘ mk) : map f = g := by ext ⟨a⟩ calc map f ⟦a⟧ = ⟦f a⟧ := map_mk hf a _ = g ⟦a⟧ := congr_fun comm a @[simp] theorem map_id : map (@id α) = id := map_unique uniformContinuous_id rfl theorem map_comp {f : α → β} {g : β → γ} (hf : UniformContinuous f) (hg : UniformContinuous g) : map g ∘ map f = map (g ∘ f) := (map_unique (hg.comp hf) <| by simp only [Function.comp_def, map_mk, hf, hg]).symm end SeparationQuotient
Mathlib/Topology/UniformSpace/Separation.lean
329
334
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro -/ import Mathlib.Algebra.Module.Submodule.Bilinear import Mathlib.Algebra.Module.Equiv.Basic import Mathlib.GroupTheory.Congruence.Hom import Mathlib.Tactic.Abel import Mathlib.Tactic.SuppressCompilation /-! # Tensor product of modules over commutative semirings. This file constructs the tensor product of modules over commutative semirings. Given a semiring `R` and modules over it `M` and `N`, the standard construction of the tensor product is `TensorProduct R M N`. It is also a module over `R`. It comes with a canonical bilinear map `TensorProduct.mk R M N : M →ₗ[R] N →ₗ[R] TensorProduct R M N`. Given any bilinear map `f : M →ₗ[R] N →ₗ[R] P`, there is a unique linear map `TensorProduct.lift f : TensorProduct R M N →ₗ[R] P` whose composition with the canonical bilinear map `TensorProduct.mk` is the given bilinear map `f`. Uniqueness is shown in the theorem `TensorProduct.lift.unique`. ## Notation * This file introduces the notation `M ⊗ N` and `M ⊗[R] N` for the tensor product space `TensorProduct R M N`. * It introduces the notation `m ⊗ₜ n` and `m ⊗ₜ[R] n` for the tensor product of two elements, otherwise written as `TensorProduct.tmul R m n`. ## Tags bilinear, tensor, tensor product -/ suppress_compilation section Semiring variable {R : Type*} [CommSemiring R] variable {R' : Type*} [Monoid R'] variable {R'' : Type*} [Semiring R''] variable {A M N P Q S T : Type*} variable [AddCommMonoid M] [AddCommMonoid N] [AddCommMonoid P] variable [AddCommMonoid Q] [AddCommMonoid S] [AddCommMonoid T] variable [Module R M] [Module R N] [Module R Q] [Module R S] [Module R T] variable [DistribMulAction R' M] variable [Module R'' M] variable (M N) namespace TensorProduct section variable (R) /-- The relation on `FreeAddMonoid (M × N)` that generates a congruence whose quotient is the tensor product. -/ inductive Eqv : FreeAddMonoid (M × N) → FreeAddMonoid (M × N) → Prop | of_zero_left : ∀ n : N, Eqv (.of (0, n)) 0 | of_zero_right : ∀ m : M, Eqv (.of (m, 0)) 0 | of_add_left : ∀ (m₁ m₂ : M) (n : N), Eqv (.of (m₁, n) + .of (m₂, n)) (.of (m₁ + m₂, n)) | of_add_right : ∀ (m : M) (n₁ n₂ : N), Eqv (.of (m, n₁) + .of (m, n₂)) (.of (m, n₁ + n₂)) | of_smul : ∀ (r : R) (m : M) (n : N), Eqv (.of (r • m, n)) (.of (m, r • n)) | add_comm : ∀ x y, Eqv (x + y) (y + x) end end TensorProduct variable (R) in /-- The tensor product of two modules `M` and `N` over the same commutative semiring `R`. The localized notations are `M ⊗ N` and `M ⊗[R] N`, accessed by `open scoped TensorProduct`. -/ def TensorProduct : Type _ := (addConGen (TensorProduct.Eqv R M N)).Quotient set_option quotPrecheck false in @[inherit_doc TensorProduct] scoped[TensorProduct] infixl:100 " ⊗ " => TensorProduct _ @[inherit_doc] scoped[TensorProduct] notation:100 M " ⊗[" R "] " N:100 => TensorProduct R M N namespace TensorProduct section Module protected instance zero : Zero (M ⊗[R] N) := (addConGen (TensorProduct.Eqv R M N)).zero protected instance add : Add (M ⊗[R] N) := (addConGen (TensorProduct.Eqv R M N)).hasAdd instance addZeroClass : AddZeroClass (M ⊗[R] N) := { (addConGen (TensorProduct.Eqv R M N)).addMonoid with /- The `toAdd` field is given explicitly as `TensorProduct.add` for performance reasons. This avoids any need to unfold `Con.addMonoid` when the type checker is checking that instance diagrams commute -/ toAdd := TensorProduct.add _ _ toZero := TensorProduct.zero _ _ } instance addSemigroup : AddSemigroup (M ⊗[R] N) := { (addConGen (TensorProduct.Eqv R M N)).addMonoid with toAdd := TensorProduct.add _ _ } instance addCommSemigroup : AddCommSemigroup (M ⊗[R] N) := { (addConGen (TensorProduct.Eqv R M N)).addMonoid with toAddSemigroup := TensorProduct.addSemigroup _ _ add_comm := fun x y => AddCon.induction_on₂ x y fun _ _ => Quotient.sound' <| AddConGen.Rel.of _ _ <| Eqv.add_comm _ _ } instance : Inhabited (M ⊗[R] N) := ⟨0⟩ variable {M N} variable (R) in /-- The canonical function `M → N → M ⊗ N`. The localized notations are `m ⊗ₜ n` and `m ⊗ₜ[R] n`, accessed by `open scoped TensorProduct`. -/ def tmul (m : M) (n : N) : M ⊗[R] N := AddCon.mk' _ <| FreeAddMonoid.of (m, n) /-- The canonical function `M → N → M ⊗ N`. -/ infixl:100 " ⊗ₜ " => tmul _ /-- The canonical function `M → N → M ⊗ N`. -/ notation:100 x " ⊗ₜ[" R "] " y:100 => tmul R x y @[elab_as_elim, induction_eliminator] protected theorem induction_on {motive : M ⊗[R] N → Prop} (z : M ⊗[R] N) (zero : motive 0) (tmul : ∀ x y, motive <| x ⊗ₜ[R] y) (add : ∀ x y, motive x → motive y → motive (x + y)) : motive z := AddCon.induction_on z fun x => FreeAddMonoid.recOn x zero fun ⟨m, n⟩ y ih => by rw [AddCon.coe_add] exact add _ _ (tmul ..) ih /-- Lift an `R`-balanced map to the tensor product. A map `f : M →+ N →+ P` additive in both components is `R`-balanced, or middle linear with respect to `R`, if scalar multiplication in either argument is equivalent, `f (r • m) n = f m (r • n)`. Note that strictly the first action should be a right-action by `R`, but for now `R` is commutative so it doesn't matter. -/ -- TODO: use this to implement `lift` and `SMul.aux`. For now we do not do this as it causes -- performance issues elsewhere. def liftAddHom (f : M →+ N →+ P) (hf : ∀ (r : R) (m : M) (n : N), f (r • m) n = f m (r • n)) : M ⊗[R] N →+ P := (addConGen (TensorProduct.Eqv R M N)).lift (FreeAddMonoid.lift (fun mn : M × N => f mn.1 mn.2)) <| AddCon.addConGen_le fun x y hxy => match x, y, hxy with | _, _, .of_zero_left n => (AddCon.ker_rel _).2 <| by simp_rw [map_zero, FreeAddMonoid.lift_eval_of, map_zero, AddMonoidHom.zero_apply] | _, _, .of_zero_right m => (AddCon.ker_rel _).2 <| by simp_rw [map_zero, FreeAddMonoid.lift_eval_of, map_zero] | _, _, .of_add_left m₁ m₂ n => (AddCon.ker_rel _).2 <| by simp_rw [map_add, FreeAddMonoid.lift_eval_of, map_add, AddMonoidHom.add_apply] | _, _, .of_add_right m n₁ n₂ => (AddCon.ker_rel _).2 <| by simp_rw [map_add, FreeAddMonoid.lift_eval_of, map_add] | _, _, .of_smul s m n => (AddCon.ker_rel _).2 <| by rw [FreeAddMonoid.lift_eval_of, FreeAddMonoid.lift_eval_of, hf] | _, _, .add_comm x y => (AddCon.ker_rel _).2 <| by simp_rw [map_add, add_comm] @[simp] theorem liftAddHom_tmul (f : M →+ N →+ P) (hf : ∀ (r : R) (m : M) (n : N), f (r • m) n = f m (r • n)) (m : M) (n : N) : liftAddHom f hf (m ⊗ₜ n) = f m n := rfl variable (M) in @[simp] theorem zero_tmul (n : N) : (0 : M) ⊗ₜ[R] n = 0 := Quotient.sound' <| AddConGen.Rel.of _ _ <| Eqv.of_zero_left _ theorem add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n := Eq.symm <| Quotient.sound' <| AddConGen.Rel.of _ _ <| Eqv.of_add_left _ _ _ variable (N) in @[simp] theorem tmul_zero (m : M) : m ⊗ₜ[R] (0 : N) = 0 := Quotient.sound' <| AddConGen.Rel.of _ _ <| Eqv.of_zero_right _ theorem tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ := Eq.symm <| Quotient.sound' <| AddConGen.Rel.of _ _ <| Eqv.of_add_right _ _ _ instance uniqueLeft [Subsingleton M] : Unique (M ⊗[R] N) where default := 0 uniq z := z.induction_on rfl (fun x y ↦ by rw [Subsingleton.elim x 0, zero_tmul]) <| by rintro _ _ rfl rfl; apply add_zero instance uniqueRight [Subsingleton N] : Unique (M ⊗[R] N) where default := 0 uniq z := z.induction_on rfl (fun x y ↦ by rw [Subsingleton.elim y 0, tmul_zero]) <| by rintro _ _ rfl rfl; apply add_zero section variable (R R' M N) /-- A typeclass for `SMul` structures which can be moved across a tensor product. This typeclass is generated automatically from an `IsScalarTower` instance, but exists so that we can also add an instance for `AddCommGroup.toIntModule`, allowing `z •` to be moved even if `R` does not support negation. Note that `Module R' (M ⊗[R] N)` is available even without this typeclass on `R'`; it's only needed if `TensorProduct.smul_tmul`, `TensorProduct.smul_tmul'`, or `TensorProduct.tmul_smul` is used. -/ class CompatibleSMul [DistribMulAction R' N] : Prop where smul_tmul : ∀ (r : R') (m : M) (n : N), (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) end /-- Note that this provides the default `CompatibleSMul R R M N` instance through `IsScalarTower.left`. -/ instance (priority := 100) CompatibleSMul.isScalarTower [SMul R' R] [IsScalarTower R' R M] [DistribMulAction R' N] [IsScalarTower R' R N] : CompatibleSMul R R' M N := ⟨fun r m n => by conv_lhs => rw [← one_smul R m] conv_rhs => rw [← one_smul R n] rw [← smul_assoc, ← smul_assoc] exact Quotient.sound' <| AddConGen.Rel.of _ _ <| Eqv.of_smul _ _ _⟩ /-- `smul` can be moved from one side of the product to the other . -/ theorem smul_tmul [DistribMulAction R' N] [CompatibleSMul R R' M N] (r : R') (m : M) (n : N) : (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) := CompatibleSMul.smul_tmul _ _ _ private def addMonoidWithWrongNSMul : AddMonoid (M ⊗[R] N) := { (addConGen (TensorProduct.Eqv R M N)).addMonoid with } attribute [local instance] addMonoidWithWrongNSMul in /-- Auxiliary function to defining scalar multiplication on tensor product. -/ def SMul.aux {R' : Type*} [SMul R' M] (r : R') : FreeAddMonoid (M × N) →+ M ⊗[R] N := FreeAddMonoid.lift fun p : M × N => (r • p.1) ⊗ₜ p.2 theorem SMul.aux_of {R' : Type*} [SMul R' M] (r : R') (m : M) (n : N) : SMul.aux r (.of (m, n)) = (r • m) ⊗ₜ[R] n := rfl variable [SMulCommClass R R' M] [SMulCommClass R R'' M] /-- Given two modules over a commutative semiring `R`, if one of the factors carries a (distributive) action of a second type of scalars `R'`, which commutes with the action of `R`, then the tensor product (over `R`) carries an action of `R'`. This instance defines this `R'` action in the case that it is the left module which has the `R'` action. Two natural ways in which this situation arises are: * Extension of scalars * A tensor product of a group representation with a module not carrying an action Note that in the special case that `R = R'`, since `R` is commutative, we just get the usual scalar action on a tensor product of two modules. This special case is important enough that, for performance reasons, we define it explicitly below. -/ instance leftHasSMul : SMul R' (M ⊗[R] N) := ⟨fun r => (addConGen (TensorProduct.Eqv R M N)).lift (SMul.aux r : _ →+ M ⊗[R] N) <| AddCon.addConGen_le fun x y hxy => match x, y, hxy with | _, _, .of_zero_left n => (AddCon.ker_rel _).2 <| by simp_rw [map_zero, SMul.aux_of, smul_zero, zero_tmul] | _, _, .of_zero_right m => (AddCon.ker_rel _).2 <| by simp_rw [map_zero, SMul.aux_of, tmul_zero] | _, _, .of_add_left m₁ m₂ n => (AddCon.ker_rel _).2 <| by simp_rw [map_add, SMul.aux_of, smul_add, add_tmul] | _, _, .of_add_right m n₁ n₂ => (AddCon.ker_rel _).2 <| by simp_rw [map_add, SMul.aux_of, tmul_add] | _, _, .of_smul s m n => (AddCon.ker_rel _).2 <| by rw [SMul.aux_of, SMul.aux_of, ← smul_comm, smul_tmul] | _, _, .add_comm x y => (AddCon.ker_rel _).2 <| by simp_rw [map_add, add_comm]⟩ instance : SMul R (M ⊗[R] N) := TensorProduct.leftHasSMul protected theorem smul_zero (r : R') : r • (0 : M ⊗[R] N) = 0 := AddMonoidHom.map_zero _ protected theorem smul_add (r : R') (x y : M ⊗[R] N) : r • (x + y) = r • x + r • y := AddMonoidHom.map_add _ _ _ protected theorem zero_smul (x : M ⊗[R] N) : (0 : R'') • x = 0 := have : ∀ (r : R'') (m : M) (n : N), r • m ⊗ₜ[R] n = (r • m) ⊗ₜ n := fun _ _ _ => rfl x.induction_on (by rw [TensorProduct.smul_zero]) (fun m n => by rw [this, zero_smul, zero_tmul]) fun x y ihx ihy => by rw [TensorProduct.smul_add, ihx, ihy, add_zero] protected theorem one_smul (x : M ⊗[R] N) : (1 : R') • x = x := have : ∀ (r : R') (m : M) (n : N), r • m ⊗ₜ[R] n = (r • m) ⊗ₜ n := fun _ _ _ => rfl x.induction_on (by rw [TensorProduct.smul_zero]) (fun m n => by rw [this, one_smul]) fun x y ihx ihy => by rw [TensorProduct.smul_add, ihx, ihy] protected theorem add_smul (r s : R'') (x : M ⊗[R] N) : (r + s) • x = r • x + s • x := have : ∀ (r : R'') (m : M) (n : N), r • m ⊗ₜ[R] n = (r • m) ⊗ₜ n := fun _ _ _ => rfl x.induction_on (by simp_rw [TensorProduct.smul_zero, add_zero]) (fun m n => by simp_rw [this, add_smul, add_tmul]) fun x y ihx ihy => by simp_rw [TensorProduct.smul_add] rw [ihx, ihy, add_add_add_comm] instance addMonoid : AddMonoid (M ⊗[R] N) := { TensorProduct.addZeroClass _ _ with toAddSemigroup := TensorProduct.addSemigroup _ _ toZero := TensorProduct.zero _ _ nsmul := fun n v => n • v nsmul_zero := by simp [TensorProduct.zero_smul] nsmul_succ := by simp only [TensorProduct.one_smul, TensorProduct.add_smul, add_comm, forall_const] } instance addCommMonoid : AddCommMonoid (M ⊗[R] N) := { TensorProduct.addCommSemigroup _ _ with toAddMonoid := TensorProduct.addMonoid } instance leftDistribMulAction : DistribMulAction R' (M ⊗[R] N) := have : ∀ (r : R') (m : M) (n : N), r • m ⊗ₜ[R] n = (r • m) ⊗ₜ n := fun _ _ _ => rfl { smul_add := fun r x y => TensorProduct.smul_add r x y mul_smul := fun r s x => x.induction_on (by simp_rw [TensorProduct.smul_zero]) (fun m n => by simp_rw [this, mul_smul]) fun x y ihx ihy => by simp_rw [TensorProduct.smul_add] rw [ihx, ihy] one_smul := TensorProduct.one_smul smul_zero := TensorProduct.smul_zero } instance : DistribMulAction R (M ⊗[R] N) := TensorProduct.leftDistribMulAction theorem smul_tmul' (r : R') (m : M) (n : N) : r • m ⊗ₜ[R] n = (r • m) ⊗ₜ n := rfl @[simp] theorem tmul_smul [DistribMulAction R' N] [CompatibleSMul R R' M N] (r : R') (x : M) (y : N) : x ⊗ₜ (r • y) = r • x ⊗ₜ[R] y := (smul_tmul _ _ _).symm theorem smul_tmul_smul (r s : R) (m : M) (n : N) : (r • m) ⊗ₜ[R] (s • n) = (r * s) • m ⊗ₜ[R] n := by simp_rw [smul_tmul, tmul_smul, mul_smul] instance leftModule : Module R'' (M ⊗[R] N) := { add_smul := TensorProduct.add_smul zero_smul := TensorProduct.zero_smul } instance : Module R (M ⊗[R] N) := TensorProduct.leftModule instance [Module R''ᵐᵒᵖ M] [IsCentralScalar R'' M] : IsCentralScalar R'' (M ⊗[R] N) where op_smul_eq_smul r x := x.induction_on (by rw [smul_zero, smul_zero]) (fun x y => by rw [smul_tmul', smul_tmul', op_smul_eq_smul]) fun x y hx hy => by rw [smul_add, smul_add, hx, hy] section -- Like `R'`, `R'₂` provides a `DistribMulAction R'₂ (M ⊗[R] N)` variable {R'₂ : Type*} [Monoid R'₂] [DistribMulAction R'₂ M] variable [SMulCommClass R R'₂ M] /-- `SMulCommClass R' R'₂ M` implies `SMulCommClass R' R'₂ (M ⊗[R] N)` -/ instance smulCommClass_left [SMulCommClass R' R'₂ M] : SMulCommClass R' R'₂ (M ⊗[R] N) where smul_comm r' r'₂ x := TensorProduct.induction_on x (by simp_rw [TensorProduct.smul_zero]) (fun m n => by simp_rw [smul_tmul', smul_comm]) fun x y ihx ihy => by simp_rw [TensorProduct.smul_add]; rw [ihx, ihy] variable [SMul R'₂ R'] /-- `IsScalarTower R'₂ R' M` implies `IsScalarTower R'₂ R' (M ⊗[R] N)` -/ instance isScalarTower_left [IsScalarTower R'₂ R' M] : IsScalarTower R'₂ R' (M ⊗[R] N) := ⟨fun s r x => x.induction_on (by simp) (fun m n => by rw [smul_tmul', smul_tmul', smul_tmul', smul_assoc]) fun x y ihx ihy => by rw [smul_add, smul_add, smul_add, ihx, ihy]⟩ variable [DistribMulAction R'₂ N] [DistribMulAction R' N] variable [CompatibleSMul R R'₂ M N] [CompatibleSMul R R' M N] /-- `IsScalarTower R'₂ R' N` implies `IsScalarTower R'₂ R' (M ⊗[R] N)` -/ instance isScalarTower_right [IsScalarTower R'₂ R' N] : IsScalarTower R'₂ R' (M ⊗[R] N) := ⟨fun s r x => x.induction_on (by simp) (fun m n => by rw [← tmul_smul, ← tmul_smul, ← tmul_smul, smul_assoc]) fun x y ihx ihy => by rw [smul_add, smul_add, smul_add, ihx, ihy]⟩ end /-- A short-cut instance for the common case, where the requirements for the `compatible_smul` instances are sufficient. -/ instance isScalarTower [SMul R' R] [IsScalarTower R' R M] : IsScalarTower R' R (M ⊗[R] N) := TensorProduct.isScalarTower_left -- or right variable (R M N) in /-- The canonical bilinear map `M → N → M ⊗[R] N`. -/ def mk : M →ₗ[R] N →ₗ[R] M ⊗[R] N := LinearMap.mk₂ R (· ⊗ₜ ·) add_tmul (fun c m n => by simp_rw [smul_tmul, tmul_smul]) tmul_add tmul_smul @[simp] theorem mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl theorem ite_tmul (x₁ : M) (x₂ : N) (P : Prop) [Decidable P] : (if P then x₁ else 0) ⊗ₜ[R] x₂ = if P then x₁ ⊗ₜ x₂ else 0 := by split_ifs <;> simp theorem tmul_ite (x₁ : M) (x₂ : N) (P : Prop) [Decidable P] : (x₁ ⊗ₜ[R] if P then x₂ else 0) = if P then x₁ ⊗ₜ x₂ else 0 := by split_ifs <;> simp lemma tmul_single {ι : Type*} [DecidableEq ι] {M : ι → Type*} [∀ i, AddCommMonoid (M i)] [∀ i, Module R (M i)] (i : ι) (x : N) (m : M i) (j : ι) : x ⊗ₜ[R] Pi.single i m j = (Pi.single i (x ⊗ₜ[R] m) : ∀ i, N ⊗[R] M i) j := by by_cases h : i = j <;> aesop lemma single_tmul {ι : Type*} [DecidableEq ι] {M : ι → Type*} [∀ i, AddCommMonoid (M i)] [∀ i, Module R (M i)] (i : ι) (x : N) (m : M i) (j : ι) : Pi.single i m j ⊗ₜ[R] x = (Pi.single i (m ⊗ₜ[R] x) : ∀ i, M i ⊗[R] N) j := by by_cases h : i = j <;> aesop section theorem sum_tmul {α : Type*} (s : Finset α) (m : α → M) (n : N) : (∑ a ∈ s, m a) ⊗ₜ[R] n = ∑ a ∈ s, m a ⊗ₜ[R] n := by classical induction s using Finset.induction with | empty => simp | insert _ _ has ih => simp [Finset.sum_insert has, add_tmul, ih] theorem tmul_sum (m : M) {α : Type*} (s : Finset α) (n : α → N) : (m ⊗ₜ[R] ∑ a ∈ s, n a) = ∑ a ∈ s, m ⊗ₜ[R] n a := by classical induction s using Finset.induction with | empty => simp | insert _ _ has ih => simp [Finset.sum_insert has, tmul_add, ih] end variable (R M N) /-- The simple (aka pure) elements span the tensor product. -/ theorem span_tmul_eq_top : Submodule.span R { t : M ⊗[R] N | ∃ m n, m ⊗ₜ n = t } = ⊤ := by ext t; simp only [Submodule.mem_top, iff_true] refine t.induction_on ?_ ?_ ?_ · exact Submodule.zero_mem _ · intro m n apply Submodule.subset_span use m, n · intro t₁ t₂ ht₁ ht₂ exact Submodule.add_mem _ ht₁ ht₂ @[simp] theorem map₂_mk_top_top_eq_top : Submodule.map₂ (mk R M N) ⊤ ⊤ = ⊤ := by rw [← top_le_iff, ← span_tmul_eq_top, Submodule.map₂_eq_span_image2] exact Submodule.span_mono fun _ ⟨m, n, h⟩ => ⟨m, trivial, n, trivial, h⟩ theorem exists_eq_tmul_of_forall (x : TensorProduct R M N) (h : ∀ (m₁ m₂ : M) (n₁ n₂ : N), ∃ m n, m₁ ⊗ₜ n₁ + m₂ ⊗ₜ n₂ = m ⊗ₜ[R] n) : ∃ m n, x = m ⊗ₜ n := by induction x with | zero => use 0, 0 rw [TensorProduct.zero_tmul] | tmul m n => use m, n | add x y h₁ h₂ => obtain ⟨m₁, n₁, rfl⟩ := h₁ obtain ⟨m₂, n₂, rfl⟩ := h₂ apply h end Module variable [Module R P] section UniversalProperty variable {M N} variable (f : M →ₗ[R] N →ₗ[R] P) /-- Auxiliary function to constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def liftAux : M ⊗[R] N →+ P := liftAddHom (LinearMap.toAddMonoidHom'.comp <| f.toAddMonoidHom) fun r m n => by dsimp; rw [LinearMap.map_smul₂, map_smul] theorem liftAux_tmul (m n) : liftAux f (m ⊗ₜ n) = f m n := rfl variable {f} @[simp] theorem liftAux.smul (r : R) (x) : liftAux f (r • x) = r • liftAux f x := TensorProduct.induction_on x (smul_zero _).symm (fun p q => by simp_rw [← tmul_smul, liftAux_tmul, (f p).map_smul]) fun p q ih1 ih2 => by simp_rw [smul_add, (liftAux f).map_add, ih1, ih2, smul_add] variable (f) in /-- Constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def lift : M ⊗[R] N →ₗ[R] P := { liftAux f with map_smul' := liftAux.smul } @[simp] theorem lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y := rfl @[simp] theorem lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y := rfl theorem ext' {g h : M ⊗[R] N →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := LinearMap.ext fun z => TensorProduct.induction_on z (by simp_rw [LinearMap.map_zero]) H fun x y ihx ihy => by rw [g.map_add, h.map_add, ihx, ihy] theorem lift.unique {g : M ⊗[R] N →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) : g = lift f := ext' fun m n => by rw [H, lift.tmul] theorem lift_mk : lift (mk R M N) = LinearMap.id := Eq.symm <| lift.unique fun _ _ => rfl theorem lift_compr₂ (g : P →ₗ[R] Q) : lift (f.compr₂ g) = g.comp (lift f) := Eq.symm <| lift.unique fun _ _ => by simp theorem lift_mk_compr₂ (f : M ⊗ N →ₗ[R] P) : lift ((mk R M N).compr₂ f) = f := by rw [lift_compr₂ f, lift_mk, LinearMap.comp_id] /-- This used to be an `@[ext]` lemma, but it fails very slowly when the `ext` tactic tries to apply it in some cases, notably when one wants to show equality of two linear maps. The `@[ext]` attribute is now added locally where it is needed. Using this as the `@[ext]` lemma instead of `TensorProduct.ext'` allows `ext` to apply lemmas specific to `M →ₗ _` and `N →ₗ _`. See note [partially-applied ext lemmas]. -/ theorem ext {g h : M ⊗ N →ₗ[R] P} (H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h := by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂] attribute [local ext high] ext example : M → N → (M → N → P) → P := fun m => flip fun f => f m variable (R M N P) in /-- Linearly constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def uncurry : (M →ₗ[R] N →ₗ[R] P) →ₗ[R] M ⊗[R] N →ₗ[R] P := LinearMap.flip <| lift <| LinearMap.lflip.comp (LinearMap.flip LinearMap.id) @[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) : uncurry R M N P f (m ⊗ₜ n) = f m n := by rw [uncurry, LinearMap.flip_apply, lift.tmul]; rfl variable (R M N P) /-- A linear equivalence constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def lift.equiv : (M →ₗ[R] N →ₗ[R] P) ≃ₗ[R] M ⊗[R] N →ₗ[R] P := { uncurry R M N P with invFun := fun f => (mk R M N).compr₂ f left_inv := fun _ => LinearMap.ext₂ fun _ _ => lift.tmul _ _ right_inv := fun _ => ext' fun _ _ => lift.tmul _ _ } @[simp] theorem lift.equiv_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) : lift.equiv R M N P f (m ⊗ₜ n) = f m n := uncurry_apply f m n @[simp] theorem lift.equiv_symm_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) : (lift.equiv R M N P).symm f m n = f (m ⊗ₜ n) := rfl /-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to form a bilinear map `M → N → P`. -/ def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P := (lift.equiv R M N P).symm variable {R M N P} @[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) : lcurry R M N P f m n = f (m ⊗ₜ n) := rfl /-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to form a bilinear map `M → N → P`. -/ def curry (f : M ⊗[R] N →ₗ[R] P) : M →ₗ[R] N →ₗ[R] P := lcurry R M N P f @[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) : curry f m n = f (m ⊗ₜ n) := rfl theorem curry_injective : Function.Injective (curry : (M ⊗[R] N →ₗ[R] P) → M →ₗ[R] N →ₗ[R] P) := fun _ _ H => ext H theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q} (H : ∀ x y z, g (x ⊗ₜ y ⊗ₜ z) = h (x ⊗ₜ y ⊗ₜ z)) : g = h := by ext x y z exact H x y z -- We'll need this one for checking the pentagon identity! theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S} (H : ∀ w x y z, g (w ⊗ₜ x ⊗ₜ y ⊗ₜ z) = h (w ⊗ₜ x ⊗ₜ y ⊗ₜ z)) : g = h := by ext w x y z exact H w x y z /-- Two linear maps (M ⊗ N) ⊗ (P ⊗ Q) → S which agree on all elements of the form (m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q) are equal. -/ theorem ext_fourfold' {φ ψ : (M ⊗[R] N) ⊗[R] P ⊗[R] Q →ₗ[R] S} (H : ∀ w x y z, φ (w ⊗ₜ x ⊗ₜ (y ⊗ₜ z)) = ψ (w ⊗ₜ x ⊗ₜ (y ⊗ₜ z))) : φ = ψ := by ext m n p q exact H m n p q end UniversalProperty variable {M N} section variable (R M N) /-- The tensor product of modules is commutative, up to linear equivalence. -/ protected def comm : M ⊗[R] N ≃ₗ[R] N ⊗[R] M := LinearEquiv.ofLinear (lift (mk R N M).flip) (lift (mk R M N).flip) (ext' fun _ _ => rfl) (ext' fun _ _ => rfl) @[simp] theorem comm_tmul (m : M) (n : N) : (TensorProduct.comm R M N) (m ⊗ₜ n) = n ⊗ₜ m := rfl @[simp] theorem comm_symm_tmul (m : M) (n : N) : (TensorProduct.comm R M N).symm (n ⊗ₜ m) = m ⊗ₜ n := rfl lemma lift_comp_comm_eq (f : M →ₗ[R] N →ₗ[R] P) : lift f ∘ₗ TensorProduct.comm R N M = lift f.flip := ext rfl end section CompatibleSMul variable (R A M N) [CommSemiring A] [Module A M] [Module A N] [SMulCommClass R A M] [CompatibleSMul R A M N] /-- If M and N are both R- and A-modules and their actions on them commute, and if the A-action on `M ⊗[R] N` can switch between the two factors, then there is a canonical A-linear map from `M ⊗[A] N` to `M ⊗[R] N`. -/ def mapOfCompatibleSMul : M ⊗[A] N →ₗ[A] M ⊗[R] N := lift { toFun := fun m ↦ { __ := mk R M N m map_smul' := fun _ _ ↦ (smul_tmul _ _ _).symm } map_add' := fun _ _ ↦ LinearMap.ext <| by simp map_smul' := fun _ _ ↦ rfl } @[simp] theorem mapOfCompatibleSMul_tmul (m n) : mapOfCompatibleSMul R A M N (m ⊗ₜ n) = m ⊗ₜ n := rfl theorem mapOfCompatibleSMul_surjective : Function.Surjective (mapOfCompatibleSMul R A M N) := fun x ↦ x.induction_on (⟨0, map_zero _⟩) (fun m n ↦ ⟨_, mapOfCompatibleSMul_tmul ..⟩) fun _ _ ⟨x, hx⟩ ⟨y, hy⟩ ↦ ⟨x + y, by simpa using congr($hx + $hy)⟩ attribute [local instance] SMulCommClass.symm /-- `mapOfCompatibleSMul R A M N` is also R-linear. -/ def mapOfCompatibleSMul' : M ⊗[A] N →ₗ[R] M ⊗[R] N where __ := mapOfCompatibleSMul R A M N map_smul' _ x := x.induction_on (map_zero _) (fun _ _ ↦ by simp [smul_tmul']) fun _ _ h h' ↦ by simpa using congr($h + $h') /-- If the R- and A-actions on M and N satisfy `CompatibleSMul` both ways, then `M ⊗[A] N` is canonically isomorphic to `M ⊗[R] N`. -/ def equivOfCompatibleSMul [CompatibleSMul A R M N] : M ⊗[A] N ≃ₗ[A] M ⊗[R] N where __ := mapOfCompatibleSMul R A M N invFun := mapOfCompatibleSMul A R M N left_inv x := x.induction_on (map_zero _) (fun _ _ ↦ rfl) fun _ _ h h' ↦ by simpa using congr($h + $h') right_inv x := x.induction_on (map_zero _) (fun _ _ ↦ rfl) fun _ _ h h' ↦ by simpa using congr($h + $h') omit [SMulCommClass R A M] end CompatibleSMul open LinearMap /-- The tensor product of a pair of linear maps between modules. -/ def map (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : M ⊗[R] N →ₗ[R] P ⊗[R] Q := lift <| comp (compl₂ (mk _ _ _) g) f @[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) : map f g (m ⊗ₜ n) = f m ⊗ₜ g n := rfl /-- Given linear maps `f : M → P`, `g : N → Q`, if we identify `M ⊗ N` with `N ⊗ M` and `P ⊗ Q` with `Q ⊗ P`, then this lemma states that `f ⊗ g = g ⊗ f`. -/ lemma map_comp_comm_eq (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map f g ∘ₗ TensorProduct.comm R N M = TensorProduct.comm R Q P ∘ₗ map g f := ext rfl lemma map_comm (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (x : N ⊗[R] M) : map f g (TensorProduct.comm R N M x) = TensorProduct.comm R Q P (map g f x) := DFunLike.congr_fun (map_comp_comm_eq _ _) _ theorem map_range_eq_span_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : range (map f g) = Submodule.span R { t | ∃ m n, f m ⊗ₜ g n = t } := by simp only [← Submodule.map_top, ← span_tmul_eq_top, Submodule.map_span, Set.mem_image, Set.mem_setOf_eq] congr; ext t constructor · rintro ⟨_, ⟨⟨m, n, rfl⟩, rfl⟩⟩ use m, n simp only [map_tmul] · rintro ⟨m, n, rfl⟩ refine ⟨_, ⟨⟨m, n, rfl⟩, ?_⟩⟩ simp only [map_tmul] /-- Given submodules `p ⊆ P` and `q ⊆ Q`, this is the natural map: `p ⊗ q → P ⊗ Q`. -/ @[simp] def mapIncl (p : Submodule R P) (q : Submodule R Q) : p ⊗[R] q →ₗ[R] P ⊗[R] Q := map p.subtype q.subtype lemma range_mapIncl (p : Submodule R P) (q : Submodule R Q) : LinearMap.range (mapIncl p q) = Submodule.span R (Set.image2 (· ⊗ₜ ·) p q) := by rw [mapIncl, map_range_eq_span_tmul] congr; ext; simp theorem map₂_eq_range_lift_comp_mapIncl (f : P →ₗ[R] Q →ₗ[R] M) (p : Submodule R P) (q : Submodule R Q) : Submodule.map₂ f p q = LinearMap.range (lift f ∘ₗ mapIncl p q) := by simp_rw [LinearMap.range_comp, range_mapIncl, Submodule.map_span, Set.image_image2, Submodule.map₂_eq_span_image2, lift.tmul] section variable {P' Q' : Type*} variable [AddCommMonoid P'] [Module R P'] variable [AddCommMonoid Q'] [Module R Q'] theorem map_comp (f₂ : P →ₗ[R] P') (f₁ : M →ₗ[R] P) (g₂ : Q →ₗ[R] Q') (g₁ : N →ₗ[R] Q) : map (f₂.comp f₁) (g₂.comp g₁) = (map f₂ g₂).comp (map f₁ g₁) := ext' fun _ _ => rfl lemma range_mapIncl_mono {p p' : Submodule R P} {q q' : Submodule R Q} (hp : p ≤ p') (hq : q ≤ q') : LinearMap.range (mapIncl p q) ≤ LinearMap.range (mapIncl p' q') := by simp_rw [range_mapIncl] exact Submodule.span_mono (Set.image2_subset hp hq) theorem lift_comp_map (i : P →ₗ[R] Q →ₗ[R] Q') (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : (lift i).comp (map f g) = lift ((i.comp f).compl₂ g) := ext' fun _ _ => rfl attribute [local ext high] ext @[simp] theorem map_id : map (id : M →ₗ[R] M) (id : N →ₗ[R] N) = .id := by ext simp only [mk_apply, id_coe, compr₂_apply, _root_.id, map_tmul] @[simp] protected theorem map_one : map (1 : M →ₗ[R] M) (1 : N →ₗ[R] N) = 1 := map_id protected theorem map_mul (f₁ f₂ : M →ₗ[R] M) (g₁ g₂ : N →ₗ[R] N) : map (f₁ * f₂) (g₁ * g₂) = map f₁ g₁ * map f₂ g₂ := map_comp f₁ f₂ g₁ g₂ @[simp] protected theorem map_pow (f : M →ₗ[R] M) (g : N →ₗ[R] N) (n : ℕ) : map f g ^ n = map (f ^ n) (g ^ n) := by induction n with | zero => simp only [pow_zero, TensorProduct.map_one] | succ n ih => simp only [pow_succ', ih, TensorProduct.map_mul] theorem map_add_left (f₁ f₂ : M →ₗ[R] P) (g : N →ₗ[R] Q) : map (f₁ + f₂) g = map f₁ g + map f₂ g := by ext simp only [add_tmul, compr₂_apply, mk_apply, map_tmul, add_apply] theorem map_add_right (f : M →ₗ[R] P) (g₁ g₂ : N →ₗ[R] Q) : map f (g₁ + g₂) = map f g₁ + map f g₂ := by ext simp only [tmul_add, compr₂_apply, mk_apply, map_tmul, add_apply] theorem map_smul_left (r : R) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map (r • f) g = r • map f g := by ext simp only [smul_tmul, compr₂_apply, mk_apply, map_tmul, smul_apply, tmul_smul] theorem map_smul_right (r : R) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map f (r • g) = r • map f g := by ext simp only [smul_tmul, compr₂_apply, mk_apply, map_tmul, smul_apply, tmul_smul] variable (R M N P Q) /-- The tensor product of a pair of linear maps between modules, bilinear in both maps. -/ def mapBilinear : (M →ₗ[R] P) →ₗ[R] (N →ₗ[R] Q) →ₗ[R] M ⊗[R] N →ₗ[R] P ⊗[R] Q := LinearMap.mk₂ R map map_add_left map_smul_left map_add_right map_smul_right /-- The canonical linear map from `P ⊗[R] (M →ₗ[R] Q)` to `(M →ₗ[R] P ⊗[R] Q)` -/ def lTensorHomToHomLTensor : P ⊗[R] (M →ₗ[R] Q) →ₗ[R] M →ₗ[R] P ⊗[R] Q := TensorProduct.lift (llcomp R M Q _ ∘ₗ mk R P Q) /-- The canonical linear map from `(M →ₗ[R] P) ⊗[R] Q` to `(M →ₗ[R] P ⊗[R] Q)` -/ def rTensorHomToHomRTensor : (M →ₗ[R] P) ⊗[R] Q →ₗ[R] M →ₗ[R] P ⊗[R] Q := TensorProduct.lift (llcomp R M P _ ∘ₗ (mk R P Q).flip).flip /-- The linear map from `(M →ₗ P) ⊗ (N →ₗ Q)` to `(M ⊗ N →ₗ P ⊗ Q)` sending `f ⊗ₜ g` to the `TensorProduct.map f g`, the tensor product of the two maps. -/ def homTensorHomMap : (M →ₗ[R] P) ⊗[R] (N →ₗ[R] Q) →ₗ[R] M ⊗[R] N →ₗ[R] P ⊗[R] Q := lift (mapBilinear R M N P Q) variable {R M N P Q} /-- This is a binary version of `TensorProduct.map`: Given a bilinear map `f : M ⟶ P ⟶ Q` and a bilinear map `g : N ⟶ S ⟶ T`, if we think `f` and `g` as linear maps with two inputs, then `map₂ f g` is a bilinear map taking two inputs `M ⊗ N → P ⊗ S → Q ⊗ S` defined by `map₂ f g (m ⊗ n) (p ⊗ s) = f m p ⊗ g n s`. Mathematically, `TensorProduct.map₂` is defined as the composition `M ⊗ N -map→ Hom(P, Q) ⊗ Hom(S, T) -homTensorHomMap→ Hom(P ⊗ S, Q ⊗ T)`. -/ def map₂ (f : M →ₗ[R] P →ₗ[R] Q) (g : N →ₗ[R] S →ₗ[R] T) : M ⊗[R] N →ₗ[R] P ⊗[R] S →ₗ[R] Q ⊗[R] T := homTensorHomMap R _ _ _ _ ∘ₗ map f g @[simp] theorem mapBilinear_apply (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : mapBilinear R M N P Q f g = map f g := rfl @[simp] theorem lTensorHomToHomLTensor_apply (p : P) (f : M →ₗ[R] Q) (m : M) : lTensorHomToHomLTensor R M P Q (p ⊗ₜ f) m = p ⊗ₜ f m := rfl @[simp] theorem rTensorHomToHomRTensor_apply (f : M →ₗ[R] P) (q : Q) (m : M) : rTensorHomToHomRTensor R M P Q (f ⊗ₜ q) m = f m ⊗ₜ q := rfl
@[simp] theorem homTensorHomMap_apply (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : homTensorHomMap R M N P Q (f ⊗ₜ g) = map f g := rfl @[simp] theorem map₂_apply_tmul (f : M →ₗ[R] P →ₗ[R] Q) (g : N →ₗ[R] S →ₗ[R] T) (m : M) (n : N) : map₂ f g (m ⊗ₜ n) = map (f m) (g n) := rfl @[simp] theorem map_zero_left (g : N →ₗ[R] Q) : map (0 : M →ₗ[R] P) g = 0 :=
Mathlib/LinearAlgebra/TensorProduct/Basic.lean
848
859
/- 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.Degree.Support /-! # Interactions between `R[X]` and `Rᵐᵒᵖ[X]` This file contains the basic API for "pushing through" the isomorphism `opRingEquiv : R[X]ᵐᵒᵖ ≃+* Rᵐᵒᵖ[X]`. It allows going back and forth between a polynomial ring over a semiring and the polynomial ring over the opposite semiring. -/ open Polynomial open Polynomial MulOpposite variable {R : Type*} [Semiring R] noncomputable section namespace Polynomial /-- Ring isomorphism between `R[X]ᵐᵒᵖ` and `Rᵐᵒᵖ[X]` sending each coefficient of a polynomial to the corresponding element of the opposite ring. -/ def opRingEquiv (R : Type*) [Semiring R] : R[X]ᵐᵒᵖ ≃+* Rᵐᵒᵖ[X] := ((toFinsuppIso R).op.trans AddMonoidAlgebra.opRingEquiv).trans (toFinsuppIso _).symm /-! Lemmas to get started, using `opRingEquiv R` on the various expressions of `Finsupp.single`: `monomial`, `C a`, `X`, `C a * X ^ n`. -/ @[simp] theorem opRingEquiv_op_monomial (n : ℕ) (r : R) : opRingEquiv R (op (monomial n r : R[X])) = monomial n (op r) := by simp only [opRingEquiv, RingEquiv.coe_trans, Function.comp_apply, AddMonoidAlgebra.opRingEquiv_apply, RingEquiv.op_apply_apply, toFinsuppIso_apply, unop_op, toFinsupp_monomial, Finsupp.mapRange_single, toFinsuppIso_symm_apply, ofFinsupp_single] @[simp] theorem opRingEquiv_op_C (a : R) : opRingEquiv R (op (C a)) = C (op a) := opRingEquiv_op_monomial 0 a @[simp] theorem opRingEquiv_op_X : opRingEquiv R (op (X : R[X])) = X := opRingEquiv_op_monomial 1 1 theorem opRingEquiv_op_C_mul_X_pow (r : R) (n : ℕ) : opRingEquiv R (op (C r * X ^ n : R[X])) = C (op r) * X ^ n := by simp only [X_pow_mul, op_mul, op_pow, map_mul, map_pow, opRingEquiv_op_X, opRingEquiv_op_C] /-! Lemmas to get started, using `(opRingEquiv R).symm` on the various expressions of `Finsupp.single`: `monomial`, `C a`, `X`, `C a * X ^ n`. -/ @[simp] theorem opRingEquiv_symm_monomial (n : ℕ) (r : Rᵐᵒᵖ) : (opRingEquiv R).symm (monomial n r) = op (monomial n (unop r)) := (opRingEquiv R).injective (by simp) @[simp] theorem opRingEquiv_symm_C (a : Rᵐᵒᵖ) : (opRingEquiv R).symm (C a) = op (C (unop a)) := opRingEquiv_symm_monomial 0 a @[simp] theorem opRingEquiv_symm_X : (opRingEquiv R).symm (X : Rᵐᵒᵖ[X]) = op X :=
opRingEquiv_symm_monomial 1 1 theorem opRingEquiv_symm_C_mul_X_pow (r : Rᵐᵒᵖ) (n : ℕ) :
Mathlib/RingTheory/Polynomial/Opposites.lean
68
70
/- 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.Cardinal.Arithmetic import Mathlib.SetTheory.Ordinal.FixedPoint /-! # Cofinality This file contains the definition of cofinality of an order and an ordinal number. ## Main Definitions * `Order.cof r` is the cofinality of a reflexive order. This is the smallest cardinality of a subset `s` that is *cofinal*, i.e. `∀ x, ∃ y ∈ s, r x y`. * `Ordinal.cof o` is the cofinality of the ordinal `o` when viewed as a linear order. ## Main Statements * `Cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for `c ≥ ℵ₀`. ## Implementation Notes * The cofinality is defined for ordinals. If `c` is a cardinal number, its cofinality is `c.ord.cof`. -/ noncomputable section open Function Cardinal Set Order open scoped Ordinal universe u v w variable {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} /-! ### Cofinality of orders -/ attribute [local instance] IsRefl.swap namespace Order /-- Cofinality of a reflexive order `≼`. This is the smallest cardinality of a subset `S : Set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/ def cof (r : α → α → Prop) : Cardinal := sInf { c | ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c } /-- The set in the definition of `Order.cof` is nonempty. -/ private theorem cof_nonempty (r : α → α → Prop) [IsRefl α r] : { c | ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c }.Nonempty := ⟨_, Set.univ, fun a => ⟨a, ⟨⟩, refl _⟩, rfl⟩ theorem cof_le (r : α → α → Prop) {S : Set α} (h : ∀ a, ∃ b ∈ S, r a b) : cof r ≤ #S := csInf_le' ⟨S, h, rfl⟩ theorem le_cof [IsRefl α r] (c : Cardinal) : c ≤ cof r ↔ ∀ {S : Set α}, (∀ a, ∃ b ∈ S, r a b) → c ≤ #S := by rw [cof, le_csInf_iff'' (cof_nonempty r)] use fun H S h => H _ ⟨S, h, rfl⟩ rintro H d ⟨S, h, rfl⟩ exact H h end Order namespace RelIso private theorem cof_le_lift [IsRefl β s] (f : r ≃r s) : Cardinal.lift.{v} (Order.cof r) ≤ Cardinal.lift.{u} (Order.cof s) := by rw [Order.cof, Order.cof, lift_sInf, lift_sInf, le_csInf_iff'' ((Order.cof_nonempty s).image _)] rintro - ⟨-, ⟨u, H, rfl⟩, rfl⟩ apply csInf_le' refine ⟨_, ⟨f.symm '' u, fun a => ?_, rfl⟩, lift_mk_eq'.2 ⟨(f.symm.toEquiv.image u).symm⟩⟩ rcases H (f a) with ⟨b, hb, hb'⟩ refine ⟨f.symm b, mem_image_of_mem _ hb, f.map_rel_iff.1 ?_⟩ rwa [RelIso.apply_symm_apply] theorem cof_eq_lift [IsRefl β s] (f : r ≃r s) : Cardinal.lift.{v} (Order.cof r) = Cardinal.lift.{u} (Order.cof s) := have := f.toRelEmbedding.isRefl (f.cof_le_lift).antisymm (f.symm.cof_le_lift) theorem cof_eq {α β : Type u} {r : α → α → Prop} {s} [IsRefl β s] (f : r ≃r s) : Order.cof r = Order.cof s := lift_inj.1 (f.cof_eq_lift) end RelIso /-! ### Cofinality of ordinals -/ namespace Ordinal /-- Cofinality of an ordinal. This is the smallest cardinal of a subset `S` of the ordinal which is unbounded, in the sense `∀ a, ∃ b ∈ S, a ≤ b`. In particular, `cof 0 = 0` and `cof (succ o) = 1`. -/ def cof (o : Ordinal.{u}) : Cardinal.{u} := o.liftOn (fun a ↦ Order.cof (swap a.rᶜ)) fun _ _ ⟨f⟩ ↦ f.compl.swap.cof_eq theorem cof_type (r : α → α → Prop) [IsWellOrder α r] : (type r).cof = Order.cof (swap rᶜ) := rfl theorem cof_type_lt [LinearOrder α] [IsWellOrder α (· < ·)] : (@type α (· < ·) _).cof = @Order.cof α (· ≤ ·) := by rw [cof_type, compl_lt, swap_ge] theorem cof_eq_cof_toType (o : Ordinal) : o.cof = @Order.cof o.toType (· ≤ ·) := by conv_lhs => rw [← type_toType o, cof_type_lt] theorem le_cof_type [IsWellOrder α r] {c} : c ≤ cof (type r) ↔ ∀ S, Unbounded r S → c ≤ #S := (le_csInf_iff'' (Order.cof_nonempty _)).trans ⟨fun H S h => H _ ⟨S, h, rfl⟩, by rintro H d ⟨S, h, rfl⟩ exact H _ h⟩ theorem cof_type_le [IsWellOrder α r] {S : Set α} (h : Unbounded r S) : cof (type r) ≤ #S := le_cof_type.1 le_rfl S h theorem lt_cof_type [IsWellOrder α r] {S : Set α} : #S < cof (type r) → Bounded r S := by simpa using not_imp_not.2 cof_type_le theorem cof_eq (r : α → α → Prop) [IsWellOrder α r] : ∃ S, Unbounded r S ∧ #S = cof (type r) := csInf_mem (Order.cof_nonempty (swap rᶜ)) theorem ord_cof_eq (r : α → α → Prop) [IsWellOrder α r] : ∃ S, Unbounded r S ∧ type (Subrel r (· ∈ S)) = (cof (type r)).ord := by let ⟨S, hS, e⟩ := cof_eq r let ⟨s, _, e'⟩ := Cardinal.ord_eq S let T : Set α := { a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a } suffices Unbounded r T by refine ⟨T, this, le_antisymm ?_ (Cardinal.ord_le.2 <| cof_type_le this)⟩ rw [← e, e'] refine (RelEmbedding.ofMonotone (fun a : T => (⟨a, let ⟨aS, _⟩ := a.2 aS⟩ : S)) fun a b h => ?_).ordinal_type_le rcases a with ⟨a, aS, ha⟩ rcases b with ⟨b, bS, hb⟩ change s ⟨a, _⟩ ⟨b, _⟩ refine ((trichotomous_of s _ _).resolve_left fun hn => ?_).resolve_left ?_ · exact asymm h (ha _ hn) · intro e injection e with e subst b exact irrefl _ h intro a have : { b : S | ¬r b a }.Nonempty := let ⟨b, bS, ba⟩ := hS a ⟨⟨b, bS⟩, ba⟩ let b := (IsWellFounded.wf : WellFounded s).min _ this have ba : ¬r b a := IsWellFounded.wf.min_mem _ this refine ⟨b, ⟨b.2, fun c => not_imp_not.1 fun h => ?_⟩, ba⟩ rw [show ∀ b : S, (⟨b, b.2⟩ : S) = b by intro b; cases b; rfl] exact IsWellFounded.wf.not_lt_min _ this (IsOrderConnected.neg_trans h ba) /-! ### Cofinality of suprema and least strict upper bounds -/ private theorem card_mem_cof {o} : ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = o.card := ⟨_, _, lsub_typein o, mk_toType o⟩ /-- The set in the `lsub` characterization of `cof` is nonempty. -/ theorem cof_lsub_def_nonempty (o) : { a : Cardinal | ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = a }.Nonempty := ⟨_, card_mem_cof⟩ theorem cof_eq_sInf_lsub (o : Ordinal.{u}) : cof o = sInf { a : Cardinal | ∃ (ι : Type u) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = a } := by refine le_antisymm (le_csInf (cof_lsub_def_nonempty o) ?_) (csInf_le' ?_) · rintro a ⟨ι, f, hf, rfl⟩ rw [← type_toType o] refine (cof_type_le fun a => ?_).trans (@mk_le_of_injective _ _ (fun s : typein ((· < ·) : o.toType → o.toType → Prop) ⁻¹' Set.range f => Classical.choose s.prop) fun s t hst => by let H := congr_arg f hst rwa [Classical.choose_spec s.prop, Classical.choose_spec t.prop, typein_inj, Subtype.coe_inj] at H) have := typein_lt_self a simp_rw [← hf, lt_lsub_iff] at this obtain ⟨i, hi⟩ := this refine ⟨enum (α := o.toType) (· < ·) ⟨f i, ?_⟩, ?_, ?_⟩ · rw [type_toType, ← hf] apply lt_lsub · rw [mem_preimage, typein_enum] exact mem_range_self i · rwa [← typein_le_typein, typein_enum] · rcases cof_eq (α := o.toType) (· < ·) with ⟨S, hS, hS'⟩ let f : S → Ordinal := fun s => typein LT.lt s.val refine ⟨S, f, le_antisymm (lsub_le fun i => typein_lt_self (o := o) i) (le_of_forall_lt fun a ha => ?_), by rwa [type_toType o] at hS'⟩ rw [← type_toType o] at ha rcases hS (enum (· < ·) ⟨a, ha⟩) with ⟨b, hb, hb'⟩ rw [← typein_le_typein, typein_enum] at hb' exact hb'.trans_lt (lt_lsub.{u, u} f ⟨b, hb⟩) @[simp] theorem lift_cof (o) : Cardinal.lift.{u, v} (cof o) = cof (Ordinal.lift.{u, v} o) := by refine inductionOn o fun α r _ ↦ ?_ rw [← type_uLift, cof_type, cof_type, ← Cardinal.lift_id'.{v, u} (Order.cof _), ← Cardinal.lift_umax] apply RelIso.cof_eq_lift ⟨Equiv.ulift.symm, _⟩ simp [swap] theorem cof_le_card (o) : cof o ≤ card o := by rw [cof_eq_sInf_lsub] exact csInf_le' card_mem_cof theorem cof_ord_le (c : Cardinal) : c.ord.cof ≤ c := by simpa using cof_le_card c.ord theorem ord_cof_le (o : Ordinal.{u}) : o.cof.ord ≤ o := (ord_le_ord.2 (cof_le_card o)).trans (ord_card_le o) theorem exists_lsub_cof (o : Ordinal) : ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = cof o := by rw [cof_eq_sInf_lsub] exact csInf_mem (cof_lsub_def_nonempty o) theorem cof_lsub_le {ι} (f : ι → Ordinal) : cof (lsub.{u, u} f) ≤ #ι := by rw [cof_eq_sInf_lsub] exact csInf_le' ⟨ι, f, rfl, rfl⟩ theorem cof_lsub_le_lift {ι} (f : ι → Ordinal) : cof (lsub.{u, v} f) ≤ Cardinal.lift.{v, u} #ι := by rw [← mk_uLift.{u, v}] convert cof_lsub_le.{max u v} fun i : ULift.{v, u} ι => f i.down exact lsub_eq_of_range_eq.{u, max u v, max u v} (Set.ext fun x => ⟨fun ⟨i, hi⟩ => ⟨ULift.up.{v, u} i, hi⟩, fun ⟨i, hi⟩ => ⟨_, hi⟩⟩) theorem le_cof_iff_lsub {o : Ordinal} {a : Cardinal} : a ≤ cof o ↔ ∀ {ι} (f : ι → Ordinal), lsub.{u, u} f = o → a ≤ #ι := by rw [cof_eq_sInf_lsub] exact (le_csInf_iff'' (cof_lsub_def_nonempty o)).trans ⟨fun H ι f hf => H _ ⟨ι, f, hf, rfl⟩, fun H b ⟨ι, f, hf, hb⟩ => by rw [← hb] exact H _ hf⟩ theorem lsub_lt_ord_lift {ι} {f : ι → Ordinal} {c : Ordinal} (hι : Cardinal.lift.{v, u} #ι < c.cof) (hf : ∀ i, f i < c) : lsub.{u, v} f < c := lt_of_le_of_ne (lsub_le hf) fun h => by subst h exact (cof_lsub_le_lift.{u, v} f).not_lt hι theorem lsub_lt_ord {ι} {f : ι → Ordinal} {c : Ordinal} (hι : #ι < c.cof) : (∀ i, f i < c) → lsub.{u, u} f < c := lsub_lt_ord_lift (by rwa [(#ι).lift_id]) theorem cof_iSup_le_lift {ι} {f : ι → Ordinal} (H : ∀ i, f i < iSup f) : cof (iSup f) ≤ Cardinal.lift.{v, u} #ι := by rw [← Ordinal.sup] at * rw [← sup_eq_lsub_iff_lt_sup.{u, v}] at H rw [H] exact cof_lsub_le_lift f theorem cof_iSup_le {ι} {f : ι → Ordinal} (H : ∀ i, f i < iSup f) : cof (iSup f) ≤ #ι := by rw [← (#ι).lift_id] exact cof_iSup_le_lift H theorem iSup_lt_ord_lift {ι} {f : ι → Ordinal} {c : Ordinal} (hι : Cardinal.lift.{v, u} #ι < c.cof) (hf : ∀ i, f i < c) : iSup f < c := (sup_le_lsub.{u, v} f).trans_lt (lsub_lt_ord_lift hι hf) theorem iSup_lt_ord {ι} {f : ι → Ordinal} {c : Ordinal} (hι : #ι < c.cof) : (∀ i, f i < c) → iSup f < c := iSup_lt_ord_lift (by rwa [(#ι).lift_id]) theorem iSup_lt_lift {ι} {f : ι → Cardinal} {c : Cardinal} (hι : Cardinal.lift.{v, u} #ι < c.ord.cof) (hf : ∀ i, f i < c) : iSup f < c := by rw [← ord_lt_ord, iSup_ord (Cardinal.bddAbove_range _)] refine iSup_lt_ord_lift hι fun i => ?_ rw [ord_lt_ord] apply hf theorem iSup_lt {ι} {f : ι → Cardinal} {c : Cardinal} (hι : #ι < c.ord.cof) : (∀ i, f i < c) → iSup f < c := iSup_lt_lift (by rwa [(#ι).lift_id]) theorem nfpFamily_lt_ord_lift {ι} {f : ι → Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hc' : Cardinal.lift.{v, u} #ι < cof c) (hf : ∀ (i), ∀ b < c, f i b < c) {a} (ha : a < c) : nfpFamily f a < c := by refine iSup_lt_ord_lift ((Cardinal.lift_le.2 (mk_list_le_max ι)).trans_lt ?_) fun l => ?_ · rw [lift_max] apply max_lt _ hc' rwa [Cardinal.lift_aleph0] · induction' l with i l H · exact ha · exact hf _ _ H theorem nfpFamily_lt_ord {ι} {f : ι → Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hc' : #ι < cof c) (hf : ∀ (i), ∀ b < c, f i b < c) {a} : a < c → nfpFamily.{u, u} f a < c := nfpFamily_lt_ord_lift hc (by rwa [(#ι).lift_id]) hf theorem nfp_lt_ord {f : Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hf : ∀ i < c, f i < c) {a} : a < c → nfp f a < c := nfpFamily_lt_ord_lift hc (by simpa using Cardinal.one_lt_aleph0.trans hc) fun _ => hf theorem exists_blsub_cof (o : Ordinal) : ∃ f : ∀ a < (cof o).ord, Ordinal, blsub.{u, u} _ f = o := by rcases exists_lsub_cof o with ⟨ι, f, hf, hι⟩ rcases Cardinal.ord_eq ι with ⟨r, hr, hι'⟩ rw [← @blsub_eq_lsub' ι r hr] at hf rw [← hι, hι'] exact ⟨_, hf⟩ theorem le_cof_iff_blsub {b : Ordinal} {a : Cardinal} : a ≤ cof b ↔ ∀ {o} (f : ∀ a < o, Ordinal), blsub.{u, u} o f = b → a ≤ o.card := le_cof_iff_lsub.trans ⟨fun H o f hf => by simpa using H _ hf, fun H ι f hf => by rcases Cardinal.ord_eq ι with ⟨r, hr, hι'⟩ rw [← @blsub_eq_lsub' ι r hr] at hf simpa using H _ hf⟩ theorem cof_blsub_le_lift {o} (f : ∀ a < o, Ordinal) : cof (blsub.{u, v} o f) ≤ Cardinal.lift.{v, u} o.card := by rw [← mk_toType o] exact cof_lsub_le_lift _ theorem cof_blsub_le {o} (f : ∀ a < o, Ordinal) : cof (blsub.{u, u} o f) ≤ o.card := by rw [← o.card.lift_id] exact cof_blsub_le_lift f theorem blsub_lt_ord_lift {o : Ordinal.{u}} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : Cardinal.lift.{v, u} o.card < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u, v} o f < c := lt_of_le_of_ne (blsub_le hf) fun h => ho.not_le (by simpa [← iSup_ord, hf, h] using cof_blsub_le_lift.{u, v} f) theorem blsub_lt_ord {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : o.card < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u, u} o f < c := blsub_lt_ord_lift (by rwa [o.card.lift_id]) hf theorem cof_bsup_le_lift {o : Ordinal} {f : ∀ a < o, Ordinal} (H : ∀ i h, f i h < bsup.{u, v} o f) : cof (bsup.{u, v} o f) ≤ Cardinal.lift.{v, u} o.card := by rw [← bsup_eq_blsub_iff_lt_bsup.{u, v}] at H rw [H] exact cof_blsub_le_lift.{u, v} f theorem cof_bsup_le {o : Ordinal} {f : ∀ a < o, Ordinal} : (∀ i h, f i h < bsup.{u, u} o f) → cof (bsup.{u, u} o f) ≤ o.card := by rw [← o.card.lift_id] exact cof_bsup_le_lift theorem bsup_lt_ord_lift {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : Cardinal.lift.{v, u} o.card < c.cof) (hf : ∀ i hi, f i hi < c) : bsup.{u, v} o f < c := (bsup_le_blsub f).trans_lt (blsub_lt_ord_lift ho hf) theorem bsup_lt_ord {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : o.card < c.cof) : (∀ i hi, f i hi < c) → bsup.{u, u} o f < c := bsup_lt_ord_lift (by rwa [o.card.lift_id]) /-! ### Basic results -/ @[simp] theorem cof_zero : cof 0 = 0 := by refine LE.le.antisymm ?_ (Cardinal.zero_le _) rw [← card_zero] exact cof_le_card 0 @[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 := ⟨inductionOn o fun _ r _ z => let ⟨_, hl, e⟩ := cof_eq r type_eq_zero_iff_isEmpty.2 <| ⟨fun a => let ⟨_, h, _⟩ := hl a (mk_eq_zero_iff.1 (e.trans z)).elim' ⟨_, h⟩⟩, fun e => by simp [e]⟩ theorem cof_ne_zero {o} : cof o ≠ 0 ↔ o ≠ 0 := cof_eq_zero.not @[simp] theorem cof_succ (o) : cof (succ o) = 1 := by apply le_antisymm · refine inductionOn o fun α r _ => ?_ change cof (type _) ≤ _ rw [← (_ : #_ = 1)] · apply cof_type_le refine fun a => ⟨Sum.inr PUnit.unit, Set.mem_singleton _, ?_⟩ rcases a with (a | ⟨⟨⟨⟩⟩⟩) <;> simp [EmptyRelation] · rw [Cardinal.mk_fintype, Set.card_singleton] simp · rw [← Cardinal.succ_zero, succ_le_iff] simpa [lt_iff_le_and_ne, Cardinal.zero_le] using fun h => succ_ne_zero o (cof_eq_zero.1 (Eq.symm h)) @[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a := ⟨inductionOn o fun α r _ z => by rcases cof_eq r with ⟨S, hl, e⟩; rw [z] at e obtain ⟨a⟩ := mk_ne_zero_iff.1 (by rw [e]; exact one_ne_zero) refine ⟨typein r a, Eq.symm <| Quotient.sound ⟨RelIso.ofSurjective (RelEmbedding.ofMonotone ?_ fun x y => ?_) fun x => ?_⟩⟩ · apply Sum.rec <;> [exact Subtype.val; exact fun _ => a] · rcases x with (x | ⟨⟨⟨⟩⟩⟩) <;> rcases y with (y | ⟨⟨⟨⟩⟩⟩) <;> simp [Subrel, Order.Preimage, EmptyRelation] exact x.2 · suffices r x a ∨ ∃ _ : PUnit.{u}, ↑a = x by convert this dsimp [RelEmbedding.ofMonotone]; simp rcases trichotomous_of r x a with (h | h | h) · exact Or.inl h · exact Or.inr ⟨PUnit.unit, h.symm⟩ · rcases hl x with ⟨a', aS, hn⟩ refine absurd h ?_ convert hn change (a : α) = ↑(⟨a', aS⟩ : S) have := le_one_iff_subsingleton.1 (le_of_eq e) congr!, fun ⟨a, e⟩ => by simp [e]⟩ /-! ### Fundamental sequences -/ -- TODO: move stuff about fundamental sequences to their own file. /-- A fundamental sequence for `a` is an increasing sequence of length `o = cof a` that converges at `a`. We provide `o` explicitly in order to avoid type rewrites. -/ def IsFundamentalSequence (a o : Ordinal.{u}) (f : ∀ b < o, Ordinal.{u}) : Prop := o ≤ a.cof.ord ∧ (∀ {i j} (hi hj), i < j → f i hi < f j hj) ∧ blsub.{u, u} o f = a namespace IsFundamentalSequence variable {a o : Ordinal.{u}} {f : ∀ b < o, Ordinal.{u}} protected theorem cof_eq (hf : IsFundamentalSequence a o f) : a.cof.ord = o := hf.1.antisymm' <| by rw [← hf.2.2] exact (ord_le_ord.2 (cof_blsub_le f)).trans (ord_card_le o) protected theorem strict_mono (hf : IsFundamentalSequence a o f) {i j} : ∀ hi hj, i < j → f i hi < f j hj := hf.2.1 theorem blsub_eq (hf : IsFundamentalSequence a o f) : blsub.{u, u} o f = a := hf.2.2 theorem ord_cof (hf : IsFundamentalSequence a o f) : IsFundamentalSequence a a.cof.ord fun i hi => f i (hi.trans_le (by rw [hf.cof_eq])) := by have H := hf.cof_eq subst H exact hf theorem id_of_le_cof (h : o ≤ o.cof.ord) : IsFundamentalSequence o o fun a _ => a := ⟨h, @fun _ _ _ _ => id, blsub_id o⟩ protected theorem zero {f : ∀ b < (0 : Ordinal), Ordinal} : IsFundamentalSequence 0 0 f := ⟨by rw [cof_zero, ord_zero], @fun i _ hi => (Ordinal.not_lt_zero i hi).elim, blsub_zero f⟩ protected theorem succ : IsFundamentalSequence (succ o) 1 fun _ _ => o := by refine ⟨?_, @fun i j hi hj h => ?_, blsub_const Ordinal.one_ne_zero o⟩ · rw [cof_succ, ord_one] · rw [lt_one_iff_zero] at hi hj rw [hi, hj] at h exact h.false.elim protected theorem monotone (hf : IsFundamentalSequence a o f) {i j : Ordinal} (hi : i < o) (hj : j < o) (hij : i ≤ j) : f i hi ≤ f j hj := by rcases lt_or_eq_of_le hij with (hij | rfl) · exact (hf.2.1 hi hj hij).le · rfl theorem trans {a o o' : Ordinal.{u}} {f : ∀ b < o, Ordinal.{u}} (hf : IsFundamentalSequence a o f) {g : ∀ b < o', Ordinal.{u}} (hg : IsFundamentalSequence o o' g) : IsFundamentalSequence a o' fun i hi => f (g i hi) (by rw [← hg.2.2]; apply lt_blsub) := by refine ⟨?_, @fun i j _ _ h => hf.2.1 _ _ (hg.2.1 _ _ h), ?_⟩ · rw [hf.cof_eq] exact hg.1.trans (ord_cof_le o) · rw [@blsub_comp.{u, u, u} o _ f (@IsFundamentalSequence.monotone _ _ f hf)] · exact hf.2.2 · exact hg.2.2 protected theorem lt {a o : Ordinal} {s : Π p < o, Ordinal} (h : IsFundamentalSequence a o s) {p : Ordinal} (hp : p < o) : s p hp < a := h.blsub_eq ▸ lt_blsub s p hp end IsFundamentalSequence /-- Every ordinal has a fundamental sequence. -/ theorem exists_fundamental_sequence (a : Ordinal.{u}) : ∃ f, IsFundamentalSequence a a.cof.ord f := by suffices h : ∃ o f, IsFundamentalSequence a o f by rcases h with ⟨o, f, hf⟩ exact ⟨_, hf.ord_cof⟩ rcases exists_lsub_cof a with ⟨ι, f, hf, hι⟩ rcases ord_eq ι with ⟨r, wo, hr⟩ haveI := wo let r' := Subrel r fun i ↦ ∀ j, r j i → f j < f i let hrr' : r' ↪r r := Subrel.relEmbedding _ _ haveI := hrr'.isWellOrder refine ⟨_, _, hrr'.ordinal_type_le.trans ?_, @fun i j _ h _ => (enum r' ⟨j, h⟩).prop _ ?_, le_antisymm (blsub_le fun i hi => lsub_le_iff.1 hf.le _) ?_⟩ · rw [← hι, hr] · change r (hrr'.1 _) (hrr'.1 _) rwa [hrr'.2, @enum_lt_enum _ r'] · rw [← hf, lsub_le_iff] intro i suffices h : ∃ i' hi', f i ≤ bfamilyOfFamily' r' (fun i => f i) i' hi' by rcases h with ⟨i', hi', hfg⟩ exact hfg.trans_lt (lt_blsub _ _ _) by_cases h : ∀ j, r j i → f j < f i · refine ⟨typein r' ⟨i, h⟩, typein_lt_type _ _, ?_⟩ rw [bfamilyOfFamily'_typein] · push_neg at h obtain ⟨hji, hij⟩ := wo.wf.min_mem _ h refine ⟨typein r' ⟨_, fun k hkj => lt_of_lt_of_le ?_ hij⟩, typein_lt_type _ _, ?_⟩ · by_contra! H exact (wo.wf.not_lt_min _ h ⟨IsTrans.trans _ _ _ hkj hji, H⟩) hkj · rwa [bfamilyOfFamily'_typein] @[simp] theorem cof_cof (a : Ordinal.{u}) : cof (cof a).ord = cof a := by obtain ⟨f, hf⟩ := exists_fundamental_sequence a obtain ⟨g, hg⟩ := exists_fundamental_sequence a.cof.ord exact ord_injective (hf.trans hg).cof_eq.symm protected theorem IsNormal.isFundamentalSequence {f : Ordinal.{u} → Ordinal.{u}} (hf : IsNormal f) {a o} (ha : IsLimit a) {g} (hg : IsFundamentalSequence a o g) : IsFundamentalSequence (f a) o fun b hb => f (g b hb) := by refine ⟨?_, @fun i j _ _ h => hf.strictMono (hg.2.1 _ _ h), ?_⟩ · rcases exists_lsub_cof (f a) with ⟨ι, f', hf', hι⟩ rw [← hg.cof_eq, ord_le_ord, ← hι] suffices (lsub.{u, u} fun i => sInf { b : Ordinal | f' i ≤ f b }) = a by rw [← this] apply cof_lsub_le have H : ∀ i, ∃ b < a, f' i ≤ f b := fun i => by have := lt_lsub.{u, u} f' i rw [hf', ← IsNormal.blsub_eq.{u, u} hf ha, lt_blsub_iff] at this simpa using this refine (lsub_le fun i => ?_).antisymm (le_of_forall_lt fun b hb => ?_) · rcases H i with ⟨b, hb, hb'⟩ exact lt_of_le_of_lt (csInf_le' hb') hb · have := hf.strictMono hb rw [← hf', lt_lsub_iff] at this obtain ⟨i, hi⟩ := this rcases H i with ⟨b, _, hb⟩ exact ((le_csInf_iff'' ⟨b, by exact hb⟩).2 fun c hc => hf.strictMono.le_iff_le.1 (hi.trans hc)).trans_lt (lt_lsub _ i) · rw [@blsub_comp.{u, u, u} a _ (fun b _ => f b) (@fun i j _ _ h => hf.strictMono.monotone h) g hg.2.2] exact IsNormal.blsub_eq.{u, u} hf ha theorem IsNormal.cof_eq {f} (hf : IsNormal f) {a} (ha : IsLimit a) : cof (f a) = cof a := let ⟨_, hg⟩ := exists_fundamental_sequence a ord_injective (hf.isFundamentalSequence ha hg).cof_eq theorem IsNormal.cof_le {f} (hf : IsNormal f) (a) : cof a ≤ cof (f a) := by rcases zero_or_succ_or_limit a with (rfl | ⟨b, rfl⟩ | ha) · rw [cof_zero] exact zero_le _ · rw [cof_succ, Cardinal.one_le_iff_ne_zero, cof_ne_zero, ← Ordinal.pos_iff_ne_zero] exact (Ordinal.zero_le (f b)).trans_lt (hf.1 b) · rw [hf.cof_eq ha] @[simp] theorem cof_add (a b : Ordinal) : b ≠ 0 → cof (a + b) = cof b := fun h => by rcases zero_or_succ_or_limit b with (rfl | ⟨c, rfl⟩ | hb) · contradiction · rw [add_succ, cof_succ, cof_succ] · exact (isNormal_add_right a).cof_eq hb theorem aleph0_le_cof {o} : ℵ₀ ≤ cof o ↔ IsLimit o := by rcases zero_or_succ_or_limit o with (rfl | ⟨o, rfl⟩ | l) · simp [not_zero_isLimit, Cardinal.aleph0_ne_zero] · simp [not_succ_isLimit, Cardinal.one_lt_aleph0] · simp only [l, iff_true] refine le_of_not_lt fun h => ?_ obtain ⟨n, e⟩ := Cardinal.lt_aleph0.1 h have := cof_cof o rw [e, ord_nat] at this cases n · simp at e simp [e, not_zero_isLimit] at l · rw [natCast_succ, cof_succ] at this rw [← this, cof_eq_one_iff_is_succ] at e rcases e with ⟨a, rfl⟩ exact not_succ_isLimit _ l @[simp] theorem cof_preOmega {o : Ordinal} (ho : IsSuccPrelimit o) : (preOmega o).cof = o.cof := by by_cases h : IsMin o · simp [h.eq_bot] · exact isNormal_preOmega.cof_eq ⟨h, ho⟩ @[simp] theorem cof_omega {o : Ordinal} (ho : o.IsLimit) : (ω_ o).cof = o.cof := isNormal_omega.cof_eq ho @[simp] theorem cof_omega0 : cof ω = ℵ₀ := (aleph0_le_cof.2 isLimit_omega0).antisymm' <| by rw [← card_omega0] apply cof_le_card theorem cof_eq' (r : α → α → Prop) [IsWellOrder α r] (h : IsLimit (type r)) : ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = cof (type r) := let ⟨S, H, e⟩ := cof_eq r ⟨S, fun a => let a' := enum r ⟨_, h.succ_lt (typein_lt_type r a)⟩ let ⟨b, h, ab⟩ := H a' ⟨b, h, (IsOrderConnected.conn a b a' <| (typein_lt_typein r).1 (by rw [typein_enum] exact lt_succ (typein _ _))).resolve_right ab⟩, e⟩ @[simp] theorem cof_univ : cof univ.{u, v} = Cardinal.univ.{u, v} := le_antisymm (cof_le_card _) (by refine le_of_forall_lt fun c h => ?_ rcases lt_univ'.1 h with ⟨c, rfl⟩ rcases @cof_eq Ordinal.{u} (· < ·) _ with ⟨S, H, Se⟩ rw [univ, ← lift_cof, ← Cardinal.lift_lift.{u+1, v, u}, Cardinal.lift_lt, ← Se] refine lt_of_not_ge fun h => ?_ obtain ⟨a, e⟩ := Cardinal.mem_range_lift_of_le h refine Quotient.inductionOn a (fun α e => ?_) e obtain ⟨f⟩ := Quotient.exact e have f := Equiv.ulift.symm.trans f let g a := (f a).1 let o := succ (iSup g) rcases H o with ⟨b, h, l⟩ refine l (lt_succ_iff.2 ?_) rw [← show g (f.symm ⟨b, h⟩) = b by simp [g]] apply Ordinal.le_iSup) end Ordinal namespace Cardinal open Ordinal /-! ### Results on sets -/ theorem mk_bounded_subset {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) {r : α → α → Prop} [IsWellOrder α r] (hr : (#α).ord = type r) : #{ s : Set α // Bounded r s } = #α := by rcases eq_or_ne #α 0 with (ha | ha) · rw [ha] haveI := mk_eq_zero_iff.1 ha rw [mk_eq_zero_iff] constructor rintro ⟨s, hs⟩ exact (not_unbounded_iff s).2 hs (unbounded_of_isEmpty s) have h' : IsStrongLimit #α := ⟨ha, @h⟩ have ha := h'.aleph0_le apply le_antisymm · have : { s : Set α | Bounded r s } = ⋃ i, 𝒫{ j | r j i } := setOf_exists _ rw [← coe_setOf, this] refine mk_iUnion_le_sum_mk.trans ((sum_le_iSup (fun i => #(𝒫{ j | r j i }))).trans ((mul_le_max_of_aleph0_le_left ha).trans ?_)) rw [max_eq_left] apply ciSup_le' _ intro i rw [mk_powerset] apply (h'.two_power_lt _).le rw [coe_setOf, card_typein, ← lt_ord, hr] apply typein_lt_type · refine @mk_le_of_injective α _ (fun x => Subtype.mk {x} ?_) ?_ · apply bounded_singleton rw [← hr] apply isLimit_ord ha · intro a b hab simpa [singleton_eq_singleton_iff] using hab theorem mk_subset_mk_lt_cof {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) : #{ s : Set α // #s < cof (#α).ord } = #α := by rcases eq_or_ne #α 0 with (ha | ha) · simp [ha] have h' : IsStrongLimit #α := ⟨ha, @h⟩ rcases ord_eq α with ⟨r, wo, hr⟩ haveI := wo apply le_antisymm · conv_rhs => rw [← mk_bounded_subset h hr] apply mk_le_mk_of_subset intro s hs rw [hr] at hs exact lt_cof_type hs · refine @mk_le_of_injective α _ (fun x => Subtype.mk {x} ?_) ?_ · rw [mk_singleton] exact one_lt_aleph0.trans_le (aleph0_le_cof.2 (isLimit_ord h'.aleph0_le)) · intro a b hab simpa [singleton_eq_singleton_iff] using hab /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : IsWellOrder α r] {s : Set (Set α)} (h₁ : Unbounded r <| ⋃₀ s) (h₂ : #s < Order.cof (swap rᶜ)) : ∃ x ∈ s, Unbounded r x := by by_contra! h simp_rw [not_unbounded_iff] at h let f : s → α := fun x : s => wo.wf.sup x (h x.1 x.2) refine h₂.not_le (le_trans (csInf_le' ⟨range f, fun x => ?_, rfl⟩) mk_range_le) rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩ exact ⟨f ⟨c, hc⟩, mem_range_self _, fun hxz => hxy (Trans.trans (wo.wf.lt_sup _ hy) hxz)⟩ /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_iUnion {α β : Type u} (r : α → α → Prop) [wo : IsWellOrder α r] (s : β → Set α) (h₁ : Unbounded r <| ⋃ x, s x) (h₂ : #β < Order.cof (swap rᶜ)) : ∃ x : β, Unbounded r (s x) := by rw [← sUnion_range] at h₁ rcases unbounded_of_unbounded_sUnion r h₁ (mk_range_le.trans_lt h₂) with ⟨_, ⟨x, rfl⟩, u⟩ exact ⟨x, u⟩ /-! ### Consequences of König's lemma -/ theorem lt_power_cof {c : Cardinal.{u}} : ℵ₀ ≤ c → c < c ^ c.ord.cof := Cardinal.inductionOn c fun α h => by rcases ord_eq α with ⟨r, wo, re⟩ have := isLimit_ord h rw [re] at this ⊢ rcases cof_eq' r this with ⟨S, H, Se⟩ have := sum_lt_prod (fun a : S => #{ x // r x a }) (fun _ => #α) fun i => ?_ · simp only [Cardinal.prod_const, Cardinal.lift_id, ← Se, ← mk_sigma, power_def] at this ⊢ refine lt_of_le_of_lt ?_ this refine ⟨Embedding.ofSurjective ?_ ?_⟩ · exact fun x => x.2.1 · exact fun a => let ⟨b, h, ab⟩ := H a ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ · have := typein_lt_type r i rwa [← re, lt_ord] at this theorem lt_cof_power {a b : Cardinal} (ha : ℵ₀ ≤ a) (b1 : 1 < b) : a < (b ^ a).ord.cof := by have b0 : b ≠ 0 := (zero_lt_one.trans b1).ne' apply lt_imp_lt_of_le_imp_le (power_le_power_left <| power_ne_zero a b0) rw [← power_mul, mul_eq_self ha] exact lt_power_cof (ha.trans <| (cantor' _ b1).le) end Cardinal
Mathlib/SetTheory/Cardinal/Cofinality.lean
1,244
1,248
/- Copyright (c) 2022 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import Mathlib.MeasureTheory.Integral.IntervalIntegral.Basic /-! # The layer cake formula / Cavalieri's principle / tail probability formula In this file we prove the following layer cake formula. Consider a non-negative measurable function `f` on a measure space. Apply pointwise to it an increasing absolutely continuous function `G : ℝ≥0 → ℝ≥0` vanishing at the origin, with derivative `G' = g` on the positive real line (in other words, `G` a primitive of a non-negative locally integrable function `g` on the positive real line). Then the integral of the result, `∫ G ∘ f`, can be written as the integral over the positive real line of the "tail measures" of `f` (i.e., a function giving the measures of the sets on which `f` exceeds different positive real values) weighted by `g`. In probability theory contexts, the "tail measures" could be referred to as "tail probabilities" of the random variable `f`, or as values of the "complementary cumulative distribution function" of the random variable `f`. The terminology "tail probability formula" is therefore occasionally used for the layer cake formula (or a standard application of it). The essence of the (mathematical) proof is Fubini's theorem. We also give the most common application of the layer cake formula - a representation of the integral of a nonnegative function f: ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) ≥ t} dt Variants of the formulas with measures of sets of the form {ω | f(ω) > t} instead of {ω | f(ω) ≥ t} are also included. ## Main results * `MeasureTheory.lintegral_comp_eq_lintegral_meas_le_mul` and `MeasureTheory.lintegral_comp_eq_lintegral_meas_lt_mul`: The general layer cake formulas with Lebesgue integrals, written in terms of measures of sets of the forms {ω | t ≤ f(ω)} and {ω | t < f(ω)}, respectively. * `MeasureTheory.lintegral_eq_lintegral_meas_le` and `MeasureTheory.lintegral_eq_lintegral_meas_lt`: The most common special cases of the layer cake formulas, stating that for a nonnegative function f we have ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) ≥ t} dt and ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) > t} dt, respectively. * `Integrable.integral_eq_integral_meas_lt`: A Bochner integral version of the most common special case of the layer cake formulas, stating that for an integrable and a.e.-nonnegative function f we have ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) > t} dt. ## See also Another common application, a representation of the integral of a real power of a nonnegative function, is given in `Mathlib.Analysis.SpecialFunctions.Pow.Integral`. ## Tags layer cake representation, Cavalieri's principle, tail probability formula -/ noncomputable section open scoped ENNReal MeasureTheory Topology open Set MeasureTheory Filter Measure namespace MeasureTheory section variable {α R : Type*} [MeasurableSpace α] (μ : Measure α) [LinearOrder R] theorem countable_meas_le_ne_meas_lt (g : α → R) : {t : R | μ {a : α | t ≤ g a} ≠ μ {a : α | t < g a}}.Countable := by -- the target set is contained in the set of points where the function `t ↦ μ {a : α | t ≤ g a}` -- jumps down on the right of `t`. This jump set is countable for any function. let F : R → ℝ≥0∞ := fun t ↦ μ {a : α | t ≤ g a} apply (countable_image_gt_image_Ioi F).mono intro t ht have : μ {a | t < g a} < μ {a | t ≤ g a} := lt_of_le_of_ne (measure_mono (fun a ha ↦ le_of_lt ha)) (Ne.symm ht) exact ⟨μ {a | t < g a}, this, fun s hs ↦ measure_mono (fun a ha ↦ hs.trans_le ha)⟩ theorem meas_le_ae_eq_meas_lt {R : Type*} [LinearOrder R] [MeasurableSpace R] (ν : Measure R) [NoAtoms ν] (g : α → R) : (fun t => μ {a : α | t ≤ g a}) =ᵐ[ν] fun t => μ {a : α | t < g a} := Set.Countable.measure_zero (countable_meas_le_ne_meas_lt μ g) _ end /-! ### Layercake formula -/ section Layercake variable {α : Type*} [MeasurableSpace α] {f : α → ℝ} {g : ℝ → ℝ} /-- An auxiliary version of the layer cake formula (Cavalieri's principle, tail probability formula), with a measurability assumption that would also essentially follow from the integrability assumptions, and a sigma-finiteness assumption. See `MeasureTheory.lintegral_comp_eq_lintegral_meas_le_mul` and `MeasureTheory.lintegral_comp_eq_lintegral_meas_lt_mul` for the main formulations of the layer cake formula. -/ theorem lintegral_comp_eq_lintegral_meas_le_mul_of_measurable_of_sigmaFinite (μ : Measure α) [SFinite μ] (f_nn : 0 ≤ f) (f_mble : Measurable f) (g_intble : ∀ t > 0, IntervalIntegrable g volume 0 t) (g_mble : Measurable g) (g_nn : ∀ t > 0, 0 ≤ g t) : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by have g_intble' : ∀ t : ℝ, 0 ≤ t → IntervalIntegrable g volume 0 t := by intro t ht rcases eq_or_lt_of_le ht with h | h · simp [← h] · exact g_intble t h have integrand_eq : ∀ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) = ∫⁻ t in Ioc 0 (f ω), ENNReal.ofReal (g t) := by intro ω have g_ae_nn : 0 ≤ᵐ[volume.restrict (Ioc 0 (f ω))] g := by filter_upwards [self_mem_ae_restrict (measurableSet_Ioc : MeasurableSet (Ioc 0 (f ω)))] with x hx using g_nn x hx.1 rw [← ofReal_integral_eq_lintegral_ofReal (g_intble' (f ω) (f_nn ω)).1 g_ae_nn] congr exact intervalIntegral.integral_of_le (f_nn ω) rw [lintegral_congr integrand_eq] simp_rw [← lintegral_indicator measurableSet_Ioc] rw [← lintegral_indicator measurableSet_Ioi, lintegral_lintegral_swap] · apply congr_arg funext s have aux₁ : (fun x => (Ioc 0 (f x)).indicator (fun t : ℝ => ENNReal.ofReal (g t)) s) = fun x => ENNReal.ofReal (g s) * (Ioi (0 : ℝ)).indicator (fun _ => 1) s * (Ici s).indicator (fun _ : ℝ => (1 : ℝ≥0∞)) (f x) := by funext a by_cases h : s ∈ Ioc (0 : ℝ) (f a) · simp only [h, show s ∈ Ioi (0 : ℝ) from h.1, show f a ∈ Ici s from h.2, indicator_of_mem, mul_one] · have h_copy := h simp only [mem_Ioc, not_and, not_le] at h by_cases h' : 0 < s · simp only [h_copy, h h', indicator_of_not_mem, not_false_iff, mem_Ici, not_le, mul_zero] · have : s ∉ Ioi (0 : ℝ) := h' simp only [this, h', indicator_of_not_mem, not_false_iff, mul_zero, zero_mul, mem_Ioc, false_and] simp_rw [aux₁] rw [lintegral_const_mul'] swap · apply ENNReal.mul_ne_top ENNReal.ofReal_ne_top by_cases h : (0 : ℝ) < s <;> · simp [h] simp_rw [show (fun a => (Ici s).indicator (fun _ : ℝ => (1 : ℝ≥0∞)) (f a)) = fun a => {a : α | s ≤ f a}.indicator (fun _ => 1) a by funext a; by_cases h : s ≤ f a <;> simp [h]] rw [lintegral_indicator₀] swap; · exact f_mble.nullMeasurable measurableSet_Ici rw [lintegral_one, Measure.restrict_apply MeasurableSet.univ, univ_inter, indicator_mul_left, mul_assoc, show (Ioi 0).indicator (fun _x : ℝ => (1 : ℝ≥0∞)) s * μ {a : α | s ≤ f a} = (Ioi 0).indicator (fun _x : ℝ => 1 * μ {a : α | s ≤ f a}) s by by_cases h : 0 < s <;> simp [h]] simp_rw [mul_comm _ (ENNReal.ofReal _), one_mul] rfl have aux₂ : (Function.uncurry fun (x : α) (y : ℝ) => (Ioc 0 (f x)).indicator (fun t : ℝ => ENNReal.ofReal (g t)) y) = {p : α × ℝ | p.2 ∈ Ioc 0 (f p.1)}.indicator fun p => ENNReal.ofReal (g p.2) := by funext p cases p with | mk p_fst p_snd => ?_ rw [Function.uncurry_apply_pair] by_cases h : p_snd ∈ Ioc 0 (f p_fst) · have h' : (p_fst, p_snd) ∈ {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := h rw [Set.indicator_of_mem h', Set.indicator_of_mem h] · have h' : (p_fst, p_snd) ∉ {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := h rw [Set.indicator_of_not_mem h', Set.indicator_of_not_mem h] rw [aux₂] have mble₀ : MeasurableSet {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := by simpa only [mem_univ, Pi.zero_apply, true_and] using measurableSet_region_between_oc measurable_zero f_mble MeasurableSet.univ exact (ENNReal.measurable_ofReal.comp (g_mble.comp measurable_snd)).aemeasurable.indicator₀ mble₀.nullMeasurableSet /-- An auxiliary version of the layer cake formula (Cavalieri's principle, tail probability formula), with a measurability assumption that would also essentially follow from the integrability assumptions. Compared to `lintegral_comp_eq_lintegral_meas_le_mul_of_measurable_of_sigmaFinite`, we remove the sigma-finite assumption. See `MeasureTheory.lintegral_comp_eq_lintegral_meas_le_mul` and `MeasureTheory.lintegral_comp_eq_lintegral_meas_lt_mul` for the main formulations of the layer cake formula. -/ theorem lintegral_comp_eq_lintegral_meas_le_mul_of_measurable (μ : Measure α) (f_nn : 0 ≤ f) (f_mble : Measurable f) (g_intble : ∀ t > 0, IntervalIntegrable g volume 0 t) (g_mble : Measurable g) (g_nn : ∀ t > 0, 0 ≤ g t) : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by /- We will reduce to the sigma-finite case, after excluding two easy cases where the result is more or less obvious. -/ have f_nonneg : ∀ ω, 0 ≤ f ω := fun ω ↦ f_nn ω -- trivial case where `g` is ae zero. Then both integrals vanish. by_cases H1 : g =ᵐ[volume.restrict (Ioi (0 : ℝ))] 0 · have A : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = 0 := by have : ∀ ω, ∫ t in (0)..f ω, g t = ∫ t in (0)..f ω, 0 := by intro ω simp_rw [intervalIntegral.integral_of_le (f_nonneg ω)] apply integral_congr_ae exact ae_restrict_of_ae_restrict_of_subset Ioc_subset_Ioi_self H1 simp [this] have B : ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) = 0 := by have : (fun t ↦ μ {a : α | t ≤ f a} * ENNReal.ofReal (g t)) =ᵐ[volume.restrict (Ioi (0 : ℝ))] 0 := by filter_upwards [H1] with t ht using by simp [ht] simp [lintegral_congr_ae this] rw [A, B] -- easy case where both sides are obviously infinite: for some `s`, one has -- `μ {a : α | s < f a} = ∞` and moreover `g` is not ae zero on `[0, s]`. by_cases H2 : ∃ s > 0, 0 < ∫ t in (0)..s, g t ∧ μ {a : α | s < f a} = ∞ · rcases H2 with ⟨s, s_pos, hs, h's⟩ rw [intervalIntegral.integral_of_le s_pos.le] at hs /- The first integral is infinite, as for `t ∈ [0, s]` one has `μ {a : α | t ≤ f a} = ∞`, and moreover the additional integral `g` is not uniformly zero. -/ have A : ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) = ∞ := by rw [eq_top_iff] calc ∞ = ∫⁻ t in Ioc 0 s, ∞ * ENNReal.ofReal (g t) := by have I_pos : ∫⁻ (a : ℝ) in Ioc 0 s, ENNReal.ofReal (g a) ≠ 0 := by rw [← ofReal_integral_eq_lintegral_ofReal (g_intble s s_pos).1] · simpa only [not_lt, ne_eq, ENNReal.ofReal_eq_zero, not_le] using hs · filter_upwards [ae_restrict_mem measurableSet_Ioc] with t ht using g_nn _ ht.1 rw [lintegral_const_mul, ENNReal.top_mul I_pos] exact ENNReal.measurable_ofReal.comp g_mble _ ≤ ∫⁻ t in Ioc 0 s, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by apply setLIntegral_mono' measurableSet_Ioc (fun x hx ↦ ?_) rw [← h's] gcongr exact fun a ha ↦ hx.2.trans (le_of_lt ha) _ ≤ ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) := lintegral_mono_set Ioc_subset_Ioi_self /- The second integral is infinite, as one integrates among other things on those `ω` where `f ω > s`: this is an infinite measure set, and on it the integrand is bounded below by `∫ t in 0..s, g t` which is positive. -/ have B : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = ∞ := by rw [eq_top_iff] calc ∞ = ∫⁻ _ in {a | s < f a}, ENNReal.ofReal (∫ t in (0)..s, g t) ∂μ := by simp only [lintegral_const, MeasurableSet.univ, Measure.restrict_apply, univ_inter, h's, ne_eq, ENNReal.ofReal_eq_zero, not_le] rw [ENNReal.mul_top] simpa [intervalIntegral.integral_of_le s_pos.le] using hs _ ≤ ∫⁻ ω in {a | s < f a}, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ := by apply setLIntegral_mono' (measurableSet_lt measurable_const f_mble) (fun a ha ↦ ?_) apply ENNReal.ofReal_le_ofReal apply intervalIntegral.integral_mono_interval le_rfl s_pos.le (le_of_lt ha) · filter_upwards [ae_restrict_mem measurableSet_Ioc] with t ht using g_nn _ ht.1 · exact g_intble _ (s_pos.trans ha) _ ≤ ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ := setLIntegral_le_lintegral _ _ rw [A, B] /- It remains to handle the interesting case, where `g` is not zero, but both integrals are not obviously infinite. Let `M` be the largest number such that `g = 0` on `[0, M]`. Then we may restrict `μ` to the points where `f ω > M` (as the other ones do not contribute to the integral). The restricted measure `ν` is sigma-finite, as `μ` gives finite measure to `{ω | f ω > a}` for any `a > M` (otherwise, we would be in the easy case above), so that one can write (a full measure subset of) the space as the countable union of the finite measure sets `{ω | f ω > uₙ}` for `uₙ` a sequence decreasing to `M`. Therefore, this case follows from the case where the measure is sigma-finite, applied to `ν`. -/ push_neg at H2 have M_bdd : BddAbove {s : ℝ | g =ᵐ[volume.restrict (Ioc (0 : ℝ) s)] 0} := by contrapose! H1 have : ∀ (n : ℕ), g =ᵐ[volume.restrict (Ioc (0 : ℝ) n)] 0 := by intro n rcases not_bddAbove_iff.1 H1 n with ⟨s, hs, ns⟩ exact ae_restrict_of_ae_restrict_of_subset (Ioc_subset_Ioc_right ns.le) hs have Hg : g =ᵐ[volume.restrict (⋃ (n : ℕ), (Ioc (0 : ℝ) n))] 0 := (ae_restrict_iUnion_iff _ _).2 this have : (⋃ (n : ℕ), (Ioc (0 : ℝ) n)) = Ioi 0 := iUnion_Ioc_eq_Ioi_self_iff.2 (fun x _ ↦ exists_nat_ge x) rwa [this] at Hg -- let `M` be the largest number such that `g` vanishes ae on `(0, M]`. let M : ℝ := sSup {s : ℝ | g =ᵐ[volume.restrict (Ioc (0 : ℝ) s)] 0} have zero_mem : 0 ∈ {s : ℝ | g =ᵐ[volume.restrict (Ioc (0 : ℝ) s)] 0} := by simpa using trivial have M_nonneg : 0 ≤ M := le_csSup M_bdd zero_mem -- Then the function `g` indeed vanishes ae on `(0, M]`. have hgM : g =ᵐ[volume.restrict (Ioc (0 : ℝ) M)] 0 := by rw [← restrict_Ioo_eq_restrict_Ioc] obtain ⟨u, -, uM, ulim⟩ : ∃ u, StrictMono u ∧ (∀ (n : ℕ), u n < M) ∧ Tendsto u atTop (𝓝 M) := exists_seq_strictMono_tendsto M have I : ∀ n, g =ᵐ[volume.restrict (Ioc (0 : ℝ) (u n))] 0 := by intro n obtain ⟨s, hs, uns⟩ : ∃ s, g =ᶠ[ae (Measure.restrict volume (Ioc 0 s))] 0 ∧ u n < s := exists_lt_of_lt_csSup (Set.nonempty_of_mem zero_mem) (uM n) exact ae_restrict_of_ae_restrict_of_subset (Ioc_subset_Ioc_right uns.le) hs have : g =ᵐ[volume.restrict (⋃ n, Ioc (0 : ℝ) (u n))] 0 := (ae_restrict_iUnion_iff _ _).2 I apply ae_restrict_of_ae_restrict_of_subset _ this rintro x ⟨x_pos, xM⟩ obtain ⟨n, hn⟩ : ∃ n, x < u n := ((tendsto_order.1 ulim).1 _ xM).exists exact mem_iUnion.2 ⟨n, ⟨x_pos, hn.le⟩⟩ -- Let `ν` be the restriction of `μ` to those points where `f a > M`. let ν := μ.restrict {a : α | M < f a} -- This measure is sigma-finite (this is the whole point of the argument). have : SigmaFinite ν := by obtain ⟨u, -, uM, ulim⟩ : ∃ u, StrictAnti u ∧ (∀ (n : ℕ), M < u n) ∧ Tendsto u atTop (𝓝 M) := exists_seq_strictAnti_tendsto M let s : ν.FiniteSpanningSetsIn univ := { set := fun n ↦ {a | f a ≤ M} ∪ {a | u n < f a} set_mem := fun _ ↦ trivial finite := by intro n have I : ν {a | f a ≤ M} = 0 := by rw [Measure.restrict_apply (measurableSet_le f_mble measurable_const)] convert measure_empty (μ := μ) rw [← disjoint_iff_inter_eq_empty] exact disjoint_left.mpr (fun a ha ↦ by simpa using ha) have J : μ {a | u n < f a} < ∞ := by rw [lt_top_iff_ne_top] apply H2 _ (M_nonneg.trans_lt (uM n)) by_contra H3 rw [not_lt, intervalIntegral.integral_of_le (M_nonneg.trans (uM n).le)] at H3 have g_nn_ae : ∀ᵐ t ∂(volume.restrict (Ioc 0 (u n))), 0 ≤ g t := by filter_upwards [ae_restrict_mem measurableSet_Ioc] with s hs using g_nn _ hs.1 have Ig : ∫ (t : ℝ) in Ioc 0 (u n), g t = 0 := le_antisymm H3 (integral_nonneg_of_ae g_nn_ae) have J : ∀ᵐ t ∂(volume.restrict (Ioc 0 (u n))), g t = 0 := (integral_eq_zero_iff_of_nonneg_ae g_nn_ae (g_intble (u n) (M_nonneg.trans_lt (uM n))).1).1 Ig have : u n ≤ M := le_csSup M_bdd J exact lt_irrefl _ (this.trans_lt (uM n)) refine lt_of_le_of_lt (measure_union_le _ _) ?_ rw [I, zero_add] apply lt_of_le_of_lt _ J exact restrict_le_self _ spanning := by apply eq_univ_iff_forall.2 (fun a ↦ ?_) rcases le_or_lt (f a) M with ha|ha · exact mem_iUnion.2 ⟨0, Or.inl ha⟩ · obtain ⟨n, hn⟩ : ∃ n, u n < f a := ((tendsto_order.1 ulim).2 _ ha).exists exact mem_iUnion.2 ⟨n, Or.inr hn⟩ } exact ⟨⟨s⟩⟩ -- the first integrals with respect to `μ` and to `ν` coincide, as points with `f a ≤ M` are -- weighted by zero as `g` vanishes there. have A : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂ν := by have meas : MeasurableSet {a | M < f a} := measurableSet_lt measurable_const f_mble have I : ∫⁻ ω in {a | M < f a}ᶜ, ENNReal.ofReal (∫ t in (0).. f ω, g t) ∂μ = ∫⁻ _ in {a | M < f a}ᶜ, 0 ∂μ := by apply setLIntegral_congr_fun meas.compl (Eventually.of_forall (fun s hs ↦ ?_)) have : ∫ (t : ℝ) in (0)..f s, g t = ∫ (t : ℝ) in (0)..f s, 0 := by simp_rw [intervalIntegral.integral_of_le (f_nonneg s)] apply integral_congr_ae apply ae_mono (restrict_mono ?_ le_rfl) hgM apply Ioc_subset_Ioc_right simpa using hs simp [this] simp only [lintegral_const, zero_mul] at I rw [← lintegral_add_compl _ meas, I, add_zero] -- the second integrals with respect to `μ` and to `ν` coincide, as points with `f a ≤ M` do not -- contribute to either integral since the weight `g` vanishes. have B : ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) = ∫⁻ t in Ioi 0, ν {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by have B1 : ∫⁻ t in Ioc 0 M, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) = ∫⁻ t in Ioc 0 M, ν {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by apply lintegral_congr_ae filter_upwards [hgM] with t ht simp [ht] have B2 : ∫⁻ t in Ioi M, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) = ∫⁻ t in Ioi M, ν {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by apply setLIntegral_congr_fun measurableSet_Ioi (Eventually.of_forall (fun t ht ↦ ?_)) rw [Measure.restrict_apply (measurableSet_le measurable_const f_mble)] congr 3 exact (inter_eq_left.2 (fun a ha ↦ (mem_Ioi.1 ht).trans_le ha)).symm have I : Ioi (0 : ℝ) = Ioc (0 : ℝ) M ∪ Ioi M := (Ioc_union_Ioi_eq_Ioi M_nonneg).symm have J : Disjoint (Ioc 0 M) (Ioi M) := Ioc_disjoint_Ioi le_rfl rw [I, lintegral_union measurableSet_Ioi J, lintegral_union measurableSet_Ioi J, B1, B2] -- therefore, we may replace the integrals wrt `μ` with integrals wrt `ν`, and apply the -- result for sigma-finite measures. rw [A, B] exact lintegral_comp_eq_lintegral_meas_le_mul_of_measurable_of_sigmaFinite ν f_nn f_mble g_intble g_mble g_nn /-- The layer cake formula / **Cavalieri's principle** / tail probability formula: Let `f` be a non-negative measurable function on a measure space. Let `G` be an increasing absolutely continuous function on the positive real line, vanishing at the origin, with derivative `G' = g`. Then the integral of the composition `G ∘ f` can be written as the integral over the positive real line of the "tail measures" `μ {ω | f(ω) ≥ t}` of `f` weighted by `g`. Roughly speaking, the statement is: `∫⁻ (G ∘ f) ∂μ = ∫⁻ t in 0..∞, g(t) * μ {ω | f(ω) ≥ t}`. See `MeasureTheory.lintegral_comp_eq_lintegral_meas_lt_mul` for a version with sets of the form `{ω | f(ω) > t}` instead. -/ theorem lintegral_comp_eq_lintegral_meas_le_mul (μ : Measure α) (f_nn : 0 ≤ᵐ[μ] f) (f_mble : AEMeasurable f μ) (g_intble : ∀ t > 0, IntervalIntegrable g volume 0 t) (g_nn : ∀ᵐ t ∂volume.restrict (Ioi 0), 0 ≤ g t) : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) := by obtain ⟨G, G_mble, G_nn, g_eq_G⟩ : ∃ G : ℝ → ℝ, Measurable G ∧ 0 ≤ G ∧ g =ᵐ[volume.restrict (Ioi 0)] G := by refine AEMeasurable.exists_measurable_nonneg ?_ g_nn exact aemeasurable_Ioi_of_forall_Ioc fun t ht => (g_intble t ht).1.1.aemeasurable have g_eq_G_on : ∀ t, g =ᵐ[volume.restrict (Ioc 0 t)] G := fun t => ae_mono (Measure.restrict_mono Ioc_subset_Ioi_self le_rfl) g_eq_G have G_intble : ∀ t > 0, IntervalIntegrable G volume 0 t := by refine fun t t_pos => ⟨(g_intble t t_pos).1.congr_fun_ae (g_eq_G_on t), ?_⟩ rw [Ioc_eq_empty_of_le t_pos.lt.le] exact integrableOn_empty obtain ⟨F, F_mble, F_nn, f_eq_F⟩ : ∃ F : α → ℝ, Measurable F ∧ 0 ≤ F ∧ f =ᵐ[μ] F := by refine ⟨fun ω ↦ max (f_mble.mk f ω) 0, f_mble.measurable_mk.max measurable_const, fun ω ↦ le_max_right _ _, ?_⟩ filter_upwards [f_mble.ae_eq_mk, f_nn] with ω hω h'ω rw [← hω] exact (max_eq_left h'ω).symm have eq₁ : ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ENNReal.ofReal (g t) = ∫⁻ t in Ioi 0, μ {a : α | t ≤ F a} * ENNReal.ofReal (G t) := by apply lintegral_congr_ae filter_upwards [g_eq_G] with t ht rw [ht] congr 1 apply measure_congr filter_upwards [f_eq_F] with a ha using by simp [setOf, ha] have eq₂ : ∀ᵐ ω ∂μ, ENNReal.ofReal (∫ t in (0)..f ω, g t) = ENNReal.ofReal (∫ t in (0)..F ω, G t) := by filter_upwards [f_eq_F] with ω fω_nn rw [fω_nn] congr 1 refine intervalIntegral.integral_congr_ae ?_ have fω_nn : 0 ≤ F ω := F_nn ω rw [uIoc_of_le fω_nn, ← ae_restrict_iff' (measurableSet_Ioc : MeasurableSet (Ioc (0 : ℝ) (F ω)))] exact g_eq_G_on (F ω) simp_rw [lintegral_congr_ae eq₂, eq₁] exact lintegral_comp_eq_lintegral_meas_le_mul_of_measurable μ F_nn F_mble G_intble G_mble (fun t _ => G_nn t) /-- The standard case of the layer cake formula / Cavalieri's principle / tail probability formula: For a nonnegative function `f` on a measure space, the Lebesgue integral of `f` can be written (roughly speaking) as: `∫⁻ f ∂μ = ∫⁻ t in 0..∞, μ {ω | f(ω) ≥ t}`. See `MeasureTheory.lintegral_eq_lintegral_meas_lt` for a version with sets of the form `{ω | f(ω) > t}` instead. -/ theorem lintegral_eq_lintegral_meas_le (μ : Measure α) (f_nn : 0 ≤ᵐ[μ] f) (f_mble : AEMeasurable f μ) : ∫⁻ ω, ENNReal.ofReal (f ω) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} := by set cst := fun _ : ℝ => (1 : ℝ) have cst_intble : ∀ t > 0, IntervalIntegrable cst volume 0 t := fun _ _ => intervalIntegrable_const have key := lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble cst_intble (Eventually.of_forall fun _ => zero_le_one) simp_rw [cst, ENNReal.ofReal_one, mul_one] at key rw [← key] congr with ω simp only [intervalIntegral.integral_const, sub_zero, Algebra.id.smul_eq_mul, mul_one] end Layercake section LayercakeLT variable {α : Type*} [MeasurableSpace α] variable {f : α → ℝ} {g : ℝ → ℝ} /-- The layer cake formula / Cavalieri's principle / tail probability formula: Let `f` be a non-negative measurable function on a measure space. Let `G` be an increasing absolutely continuous function on the positive real line, vanishing at the origin, with derivative `G' = g`. Then the integral of the composition `G ∘ f` can be written as the integral over the positive real line of the "tail measures" `μ {ω | f(ω) > t}` of `f` weighted by `g`. Roughly speaking, the statement is: `∫⁻ (G ∘ f) ∂μ = ∫⁻ t in 0..∞, g(t) * μ {ω | f(ω) > t}`. See `lintegral_comp_eq_lintegral_meas_le_mul` for a version with sets of the form `{ω | f(ω) ≥ t}` instead. -/ theorem lintegral_comp_eq_lintegral_meas_lt_mul (μ : Measure α) (f_nn : 0 ≤ᵐ[μ] f) (f_mble : AEMeasurable f μ) (g_intble : ∀ t > 0, IntervalIntegrable g volume 0 t) (g_nn : ∀ᵐ t ∂volume.restrict (Ioi 0), 0 ≤ g t) : ∫⁻ ω, ENNReal.ofReal (∫ t in (0)..f ω, g t) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t < f a} * ENNReal.ofReal (g t) := by rw [lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble g_intble g_nn] apply lintegral_congr_ae filter_upwards [meas_le_ae_eq_meas_lt μ (volume.restrict (Ioi 0)) f] with t ht rw [ht] /-- The standard case of the layer cake formula / Cavalieri's principle / tail probability formula: For a nonnegative function `f` on a measure space, the Lebesgue integral of `f` can be written (roughly speaking) as: `∫⁻ f ∂μ = ∫⁻ t in 0..∞, μ {ω | f(ω) > t}`. See `lintegral_eq_lintegral_meas_le` for a version with sets of the form `{ω | f(ω) ≥ t}` instead. -/ theorem lintegral_eq_lintegral_meas_lt (μ : Measure α) (f_nn : 0 ≤ᵐ[μ] f) (f_mble : AEMeasurable f μ) : ∫⁻ ω, ENNReal.ofReal (f ω) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t < f a} := by rw [lintegral_eq_lintegral_meas_le μ f_nn f_mble] apply lintegral_congr_ae filter_upwards [meas_le_ae_eq_meas_lt μ (volume.restrict (Ioi 0)) f] with t ht rw [ht] end LayercakeLT section LayercakeIntegral variable {α : Type*} [MeasurableSpace α] {μ : Measure α} {f : α → ℝ} /-- The standard case of the layer cake formula / Cavalieri's principle / tail probability formula: For an integrable a.e.-nonnegative real-valued function `f`, the Bochner integral of `f` can be written (roughly speaking) as: `∫ f ∂μ = ∫ t in 0..∞, μ {ω | f(ω) > t}`. See `MeasureTheory.lintegral_eq_lintegral_meas_lt` for a version with Lebesgue integral `∫⁻` instead. -/ theorem Integrable.integral_eq_integral_meas_lt (f_intble : Integrable f μ) (f_nn : 0 ≤ᵐ[μ] f) : ∫ ω, f ω ∂μ = ∫ t in Set.Ioi 0, μ.real {a : α | t < f a} := by have key := lintegral_eq_lintegral_meas_lt μ f_nn f_intble.aemeasurable have lhs_finite : ∫⁻ (ω : α), ENNReal.ofReal (f ω) ∂μ < ∞ := Integrable.lintegral_lt_top f_intble have rhs_finite : ∫⁻ (t : ℝ) in Set.Ioi 0, μ {a | t < f a} < ∞ := by simp only [← key, lhs_finite] have rhs_integrand_finite : ∀ (t : ℝ), t > 0 → μ {a | t < f a} < ∞ := fun t ht ↦ measure_gt_lt_top f_intble ht convert (ENNReal.toReal_eq_toReal lhs_finite.ne rhs_finite.ne).mpr key · exact integral_eq_lintegral_of_nonneg_ae f_nn f_intble.aestronglyMeasurable · have aux := @integral_eq_lintegral_of_nonneg_ae _ _ ((volume : Measure ℝ).restrict (Set.Ioi 0)) (fun t ↦ μ.real {a : α | t < f a}) ?_ ?_ · rw [aux] congr 1 apply setLIntegral_congr_fun measurableSet_Ioi (Eventually.of_forall _) exact fun t t_pos ↦ ENNReal.ofReal_toReal (rhs_integrand_finite t t_pos).ne · exact Eventually.of_forall (fun x ↦ by positivity) · apply Measurable.aestronglyMeasurable refine Measurable.ennreal_toReal ?_ exact Antitone.measurable (fun _ _ hst ↦ measure_mono (fun _ h ↦ lt_of_le_of_lt hst h)) theorem Integrable.integral_eq_integral_meas_le (f_intble : Integrable f μ) (f_nn : 0 ≤ᵐ[μ] f) : ∫ ω, f ω ∂μ = ∫ t in Set.Ioi 0, μ.real {a : α | t ≤ f a} := by rw [Integrable.integral_eq_integral_meas_lt f_intble f_nn] apply integral_congr_ae filter_upwards [meas_le_ae_eq_meas_lt μ (volume.restrict (Ioi 0)) f] with t ht exact congrArg ENNReal.toReal ht.symm lemma Integrable.integral_eq_integral_Ioc_meas_le {f : α → ℝ} {M : ℝ} (f_intble : Integrable f μ) (f_nn : 0 ≤ᵐ[μ] f) (f_bdd : f ≤ᵐ[μ] (fun _ ↦ M)) : ∫ ω, f ω ∂μ = ∫ t in Ioc 0 M, μ.real {a : α | t ≤ f a} := by rw [f_intble.integral_eq_integral_meas_le f_nn] rw [setIntegral_eq_of_subset_of_ae_diff_eq_zero nullMeasurableSet_Ioi Ioc_subset_Ioi_self ?_] apply Eventually.of_forall (fun t ht ↦ ?_) have htM : M < t := by simp_all only [mem_diff, mem_Ioi, mem_Ioc, not_and, not_le] have obs : μ {a | M < f a} = 0 := by
rw [measure_zero_iff_ae_nmem] filter_upwards [f_bdd] with a ha using not_lt.mpr ha rw [measureReal_def, ENNReal.toReal_eq_zero_iff] exact Or.inl <| measure_mono_null (fun a ha ↦ lt_of_lt_of_le htM ha) obs end LayercakeIntegral end MeasureTheory
Mathlib/MeasureTheory/Integral/Layercake.lean
553
565
/- 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 -/ import Mathlib.Data.Set.Piecewise import Mathlib.Logic.Equiv.Defs import Mathlib.Tactic.Core import Mathlib.Tactic.Attr.Core /-! # Partial equivalences This files defines equivalences between subsets of given types. An element `e` of `PartialEquiv α β` is made of two maps `e.toFun` and `e.invFun` respectively from α to β and from β to α (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively α and β. They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two partial equivalences by restricting the source and target to the maximal set where the composition makes sense. As for equivs, we register a coercion to functions and use it in our simp normal form: we write `e x` and `e.symm y` instead of `e.toFun x` and `e.invFun y`. ## Main definitions * `Equiv.toPartialEquiv`: associating a partial equiv to an equiv, with source = target = univ * `PartialEquiv.symm`: the inverse of a partial equivalence * `PartialEquiv.trans`: the composition of two partial equivalences * `PartialEquiv.refl`: the identity partial equivalence * `PartialEquiv.ofSet`: the identity on a set `s` * `EqOnSource`: equivalence relation describing the "right" notion of equality for partial equivalences (see below in implementation notes) ## Implementation notes There are at least three possible implementations of partial equivalences: * equivs on subtypes * pairs of functions taking values in `Option α` and `Option β`, equal to none where the partial equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `PEquiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The `PartialEquiv` version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `toFun` and `invFun` outside of `source` and `target`). In particular, the equality notion between partial equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no partial equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal partial equivs, this is not an issue in practice. Still, we introduce an equivalence relation `EqOnSource` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. ### Local coding conventions If a lemma deals with the intersection of a set with either source or target of a `PartialEquiv`, then it should use `e.source ∩ s` or `e.target ∩ t`, not `s ∩ e.source` or `t ∩ e.target`. -/ open Lean Meta Elab Tactic /-! Implementation of the `mfld_set_tac` tactic for working with the domains of partially-defined functions (`PartialEquiv`, `PartialHomeomorph`, etc). This is in a separate file from `Mathlib.Logic.Equiv.MfldSimpsAttr` because attributes need a new file to become functional. -/ /-- Common `@[simps]` configuration options used for manifold-related declarations. -/ def mfld_cfg : Simps.Config where attrs := [`mfld_simps] fullyApplied := false namespace Tactic.MfldSetTac /-- A very basic tactic to show that sets showing up in manifolds coincide or are included in one another. -/ elab (name := mfldSetTac) "mfld_set_tac" : tactic => withMainContext do let g ← getMainGoal let goalTy := (← instantiateMVars (← g.getDecl).type).getAppFnArgs match goalTy with | (``Eq, #[_ty, _e₁, _e₂]) => evalTactic (← `(tactic| ( apply Set.ext; intro my_y constructor <;> · intro h_my_y try simp only [*, mfld_simps] at h_my_y try simp only [*, mfld_simps]))) | (``Subset, #[_ty, _inst, _e₁, _e₂]) => evalTactic (← `(tactic| ( intro my_y h_my_y try simp only [*, mfld_simps] at h_my_y try simp only [*, mfld_simps]))) | _ => throwError "goal should be an equality or an inclusion" attribute [mfld_simps] and_true eq_self_iff_true Function.comp_apply end Tactic.MfldSetTac open Function Set variable {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Local equivalence between subsets `source` and `target` of `α` and `β` respectively. The (global) maps `toFun : α → β` and `invFun : β → α` map `source` to `target` and conversely, and are inverse to each other there. The values of `toFun` outside of `source` and of `invFun` outside of `target` are irrelevant. -/ structure PartialEquiv (α : Type*) (β : Type*) where /-- The global function which has a partial inverse. Its value outside of the `source` subset is irrelevant. -/ toFun : α → β /-- The partial inverse to `toFun`. Its value outside of the `target` subset is irrelevant. -/ invFun : β → α /-- The domain of the partial equivalence. -/ source : Set α /-- The codomain of the partial equivalence. -/ target : Set β /-- The proposition that elements of `source` are mapped to elements of `target`. -/ map_source' : ∀ ⦃x⦄, x ∈ source → toFun x ∈ target /-- The proposition that elements of `target` are mapped to elements of `source`. -/ map_target' : ∀ ⦃x⦄, x ∈ target → invFun x ∈ source /-- The proposition that `invFun` is a left-inverse of `toFun` on `source`. -/ left_inv' : ∀ ⦃x⦄, x ∈ source → invFun (toFun x) = x /-- The proposition that `invFun` is a right-inverse of `toFun` on `target`. -/ right_inv' : ∀ ⦃x⦄, x ∈ target → toFun (invFun x) = x attribute [coe] PartialEquiv.toFun namespace PartialEquiv variable (e : PartialEquiv α β) (e' : PartialEquiv β γ) instance [Inhabited α] [Inhabited β] : Inhabited (PartialEquiv α β) := ⟨⟨const α default, const β default, ∅, ∅, mapsTo_empty _ _, mapsTo_empty _ _, eqOn_empty _ _, eqOn_empty _ _⟩⟩ /-- The inverse of a partial equivalence -/ @[symm] protected def symm : PartialEquiv β α where toFun := e.invFun invFun := e.toFun source := e.target target := e.source map_source' := e.map_target' map_target' := e.map_source' left_inv' := e.right_inv' right_inv' := e.left_inv' instance : CoeFun (PartialEquiv α β) fun _ => α → β := ⟨PartialEquiv.toFun⟩ /-- See Note [custom simps projection] -/ def Simps.symm_apply (e : PartialEquiv α β) : β → α := e.symm initialize_simps_projections PartialEquiv (toFun → apply, invFun → symm_apply) theorem coe_mk (f : α → β) (g s t ml mr il ir) : (PartialEquiv.mk f g s t ml mr il ir : α → β) = f := rfl @[simp, mfld_simps] theorem coe_symm_mk (f : α → β) (g s t ml mr il ir) : ((PartialEquiv.mk f g s t ml mr il ir).symm : β → α) = g := rfl @[simp, mfld_simps] theorem invFun_as_coe : e.invFun = e.symm := rfl @[simp, mfld_simps] theorem map_source {x : α} (h : x ∈ e.source) : e x ∈ e.target := e.map_source' h /-- Variant of `e.map_source` and `map_source'`, stated for images of subsets of `source`. -/ lemma map_source'' : e '' e.source ⊆ e.target := fun _ ⟨_, hx, hex⟩ ↦ mem_of_eq_of_mem (id hex.symm) (e.map_source' hx) @[simp, mfld_simps] theorem map_target {x : β} (h : x ∈ e.target) : e.symm x ∈ e.source := e.map_target' h @[simp, mfld_simps] theorem left_inv {x : α} (h : x ∈ e.source) : e.symm (e x) = x := e.left_inv' h @[simp, mfld_simps] theorem right_inv {x : β} (h : x ∈ e.target) : e (e.symm x) = x := e.right_inv' h theorem eq_symm_apply {x : α} {y : β} (hx : x ∈ e.source) (hy : y ∈ e.target) : x = e.symm y ↔ e x = y := ⟨fun h => by rw [← e.right_inv hy, h], fun h => by rw [← e.left_inv hx, h]⟩ protected theorem mapsTo : MapsTo e e.source e.target := fun _ => e.map_source theorem symm_mapsTo : MapsTo e.symm e.target e.source := e.symm.mapsTo protected theorem leftInvOn : LeftInvOn e.symm e e.source := fun _ => e.left_inv protected theorem rightInvOn : RightInvOn e.symm e e.target := fun _ => e.right_inv protected theorem invOn : InvOn e.symm e e.source e.target := ⟨e.leftInvOn, e.rightInvOn⟩ protected theorem injOn : InjOn e e.source := e.leftInvOn.injOn protected theorem bijOn : BijOn e e.source e.target := e.invOn.bijOn e.mapsTo e.symm_mapsTo protected theorem surjOn : SurjOn e e.source e.target := e.bijOn.surjOn /-- Interpret an `Equiv` as a `PartialEquiv` by restricting it to `s` in the domain and to `t` in the codomain. -/ @[simps -fullyApplied] def _root_.Equiv.toPartialEquivOfImageEq (e : α ≃ β) (s : Set α) (t : Set β) (h : e '' s = t) : PartialEquiv α β where toFun := e invFun := e.symm source := s target := t map_source' _ hx := h ▸ mem_image_of_mem _ hx map_target' x hx := by subst t rcases hx with ⟨x, hx, rfl⟩ rwa [e.symm_apply_apply] left_inv' x _ := e.symm_apply_apply x right_inv' x _ := e.apply_symm_apply x /-- Associate a `PartialEquiv` to an `Equiv`. -/ @[simps! (config := mfld_cfg)] def _root_.Equiv.toPartialEquiv (e : α ≃ β) : PartialEquiv α β := e.toPartialEquivOfImageEq univ univ <| by rw [image_univ, e.surjective.range_eq] instance inhabitedOfEmpty [IsEmpty α] [IsEmpty β] : Inhabited (PartialEquiv α β) := ⟨((Equiv.equivEmpty α).trans (Equiv.equivEmpty β).symm).toPartialEquiv⟩ /-- Create a copy of a `PartialEquiv` providing better definitional equalities. -/ @[simps -fullyApplied] def copy (e : PartialEquiv α β) (f : α → β) (hf : ⇑e = f) (g : β → α) (hg : ⇑e.symm = g) (s : Set α) (hs : e.source = s) (t : Set β) (ht : e.target = t) : PartialEquiv α β where toFun := f invFun := g source := s target := t map_source' _ := ht ▸ hs ▸ hf ▸ e.map_source map_target' _ := hs ▸ ht ▸ hg ▸ e.map_target left_inv' _ := hs ▸ hf ▸ hg ▸ e.left_inv right_inv' _ := ht ▸ hf ▸ hg ▸ e.right_inv theorem copy_eq (e : PartialEquiv α β) (f : α → β) (hf : ⇑e = f) (g : β → α) (hg : ⇑e.symm = g) (s : Set α) (hs : e.source = s) (t : Set β) (ht : e.target = t) : e.copy f hf g hg s hs t ht = e := by substs f g s t cases e rfl /-- Associate to a `PartialEquiv` an `Equiv` between the source and the target. -/ protected def toEquiv : e.source ≃ e.target where toFun x := ⟨e x, e.map_source x.mem⟩ invFun y := ⟨e.symm y, e.map_target y.mem⟩ left_inv := fun ⟨_, hx⟩ => Subtype.eq <| e.left_inv hx right_inv := fun ⟨_, hy⟩ => Subtype.eq <| e.right_inv hy @[simp, mfld_simps] theorem symm_source : e.symm.source = e.target := rfl @[simp, mfld_simps] theorem symm_target : e.symm.target = e.source := rfl @[simp, mfld_simps] theorem symm_symm : e.symm.symm = e := rfl theorem symm_bijective : Function.Bijective (PartialEquiv.symm : PartialEquiv α β → PartialEquiv β α) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ theorem image_source_eq_target : e '' e.source = e.target := e.bijOn.image_eq theorem forall_mem_target {p : β → Prop} : (∀ y ∈ e.target, p y) ↔ ∀ x ∈ e.source, p (e x) := by rw [← image_source_eq_target, forall_mem_image] theorem exists_mem_target {p : β → Prop} : (∃ y ∈ e.target, p y) ↔ ∃ x ∈ e.source, p (e x) := by rw [← image_source_eq_target, exists_mem_image] /-- We say that `t : Set β` is an image of `s : Set α` under a partial equivalence if any of the following equivalent conditions hold: * `e '' (e.source ∩ s) = e.target ∩ t`; * `e.source ∩ e ⁻¹ t = e.source ∩ s`; * `∀ x ∈ e.source, e x ∈ t ↔ x ∈ s` (this one is used in the definition). -/ def IsImage (s : Set α) (t : Set β) : Prop := ∀ ⦃x⦄, x ∈ e.source → (e x ∈ t ↔ x ∈ s) namespace IsImage variable {e} {s : Set α} {t : Set β} {x : α} theorem apply_mem_iff (h : e.IsImage s t) (hx : x ∈ e.source) : e x ∈ t ↔ x ∈ s := h hx theorem symm_apply_mem_iff (h : e.IsImage s t) : ∀ ⦃y⦄, y ∈ e.target → (e.symm y ∈ s ↔ y ∈ t) := e.forall_mem_target.mpr fun x hx => by rw [e.left_inv hx, h hx] protected theorem symm (h : e.IsImage s t) : e.symm.IsImage t s := h.symm_apply_mem_iff @[simp] theorem symm_iff : e.symm.IsImage t s ↔ e.IsImage s t := ⟨fun h => h.symm, fun h => h.symm⟩ protected theorem mapsTo (h : e.IsImage s t) : MapsTo e (e.source ∩ s) (e.target ∩ t) := fun _ hx => ⟨e.mapsTo hx.1, (h hx.1).2 hx.2⟩ theorem symm_mapsTo (h : e.IsImage s t) : MapsTo e.symm (e.target ∩ t) (e.source ∩ s) := h.symm.mapsTo /-- Restrict a `PartialEquiv` to a pair of corresponding sets. -/ @[simps -fullyApplied] def restr (h : e.IsImage s t) : PartialEquiv α β where toFun := e invFun := e.symm source := e.source ∩ s target := e.target ∩ t map_source' := h.mapsTo map_target' := h.symm_mapsTo left_inv' := e.leftInvOn.mono inter_subset_left right_inv' := e.rightInvOn.mono inter_subset_left theorem image_eq (h : e.IsImage s t) : e '' (e.source ∩ s) = e.target ∩ t := h.restr.image_source_eq_target theorem symm_image_eq (h : e.IsImage s t) : e.symm '' (e.target ∩ t) = e.source ∩ s := h.symm.image_eq theorem iff_preimage_eq : e.IsImage s t ↔ e.source ∩ e ⁻¹' t = e.source ∩ s := by simp only [IsImage, Set.ext_iff, mem_inter_iff, mem_preimage, and_congr_right_iff] alias ⟨preimage_eq, of_preimage_eq⟩ := iff_preimage_eq theorem iff_symm_preimage_eq : e.IsImage s t ↔ e.target ∩ e.symm ⁻¹' s = e.target ∩ t := symm_iff.symm.trans iff_preimage_eq alias ⟨symm_preimage_eq, of_symm_preimage_eq⟩ := iff_symm_preimage_eq theorem of_image_eq (h : e '' (e.source ∩ s) = e.target ∩ t) : e.IsImage s t := of_symm_preimage_eq <| Eq.trans (of_symm_preimage_eq rfl).image_eq.symm h theorem of_symm_image_eq (h : e.symm '' (e.target ∩ t) = e.source ∩ s) : e.IsImage s t := of_preimage_eq <| Eq.trans (iff_preimage_eq.2 rfl).symm_image_eq.symm h protected theorem compl (h : e.IsImage s t) : e.IsImage sᶜ tᶜ := fun _ hx => not_congr (h hx) protected theorem inter {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s ∩ s') (t ∩ t') := fun _ hx => and_congr (h hx) (h' hx) protected theorem union {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s ∪ s') (t ∪ t') := fun _ hx => or_congr (h hx) (h' hx) protected theorem diff {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s \ s') (t \ t') := h.inter h'.compl theorem leftInvOn_piecewise {e' : PartialEquiv α β} [∀ i, Decidable (i ∈ s)] [∀ i, Decidable (i ∈ t)] (h : e.IsImage s t) (h' : e'.IsImage s t) : LeftInvOn (t.piecewise e.symm e'.symm) (s.piecewise e e') (s.ite e.source e'.source) := by rintro x (⟨he, hs⟩ | ⟨he, hs : x ∉ s⟩) · rw [piecewise_eq_of_mem _ _ _ hs, piecewise_eq_of_mem _ _ _ ((h he).2 hs), e.left_inv he] · rw [piecewise_eq_of_not_mem _ _ _ hs, piecewise_eq_of_not_mem _ _ _ ((h'.compl he).2 hs), e'.left_inv he] theorem inter_eq_of_inter_eq_of_eqOn {e' : PartialEquiv α β} (h : e.IsImage s t) (h' : e'.IsImage s t) (hs : e.source ∩ s = e'.source ∩ s) (heq : EqOn e e' (e.source ∩ s)) : e.target ∩ t = e'.target ∩ t := by rw [← h.image_eq, ← h'.image_eq, ← hs, heq.image_eq] theorem symm_eq_on_of_inter_eq_of_eqOn {e' : PartialEquiv α β} (h : e.IsImage s t) (hs : e.source ∩ s = e'.source ∩ s) (heq : EqOn e e' (e.source ∩ s)) : EqOn e.symm e'.symm (e.target ∩ t) := by rw [← h.image_eq] rintro y ⟨x, hx, rfl⟩ have hx' := hx; rw [hs] at hx' rw [e.left_inv hx.1, heq hx, e'.left_inv hx'.1] end IsImage theorem isImage_source_target : e.IsImage e.source e.target := fun x hx => by simp [hx] theorem isImage_source_target_of_disjoint (e' : PartialEquiv α β) (hs : Disjoint e.source e'.source) (ht : Disjoint e.target e'.target) : e.IsImage e'.source e'.target := IsImage.of_image_eq <| by rw [hs.inter_eq, ht.inter_eq, image_empty] theorem image_source_inter_eq' (s : Set α) : e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' s := by rw [inter_comm, e.leftInvOn.image_inter', image_source_eq_target, inter_comm] theorem image_source_inter_eq (s : Set α) : e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' (e.source ∩ s) := by rw [inter_comm, e.leftInvOn.image_inter, image_source_eq_target, inter_comm] theorem image_eq_target_inter_inv_preimage {s : Set α} (h : s ⊆ e.source) : e '' s = e.target ∩ e.symm ⁻¹' s := by rw [← e.image_source_inter_eq', inter_eq_self_of_subset_right h] theorem symm_image_eq_source_inter_preimage {s : Set β} (h : s ⊆ e.target) : e.symm '' s = e.source ∩ e ⁻¹' s := e.symm.image_eq_target_inter_inv_preimage h theorem symm_image_target_inter_eq (s : Set β) : e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' (e.target ∩ s) := e.symm.image_source_inter_eq _ theorem symm_image_target_inter_eq' (s : Set β) : e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' s := e.symm.image_source_inter_eq' _ theorem source_inter_preimage_inv_preimage (s : Set α) : e.source ∩ e ⁻¹' (e.symm ⁻¹' s) = e.source ∩ s := Set.ext fun x => and_congr_right_iff.2 fun hx => by simp only [mem_preimage, e.left_inv hx] theorem source_inter_preimage_target_inter (s : Set β) : e.source ∩ e ⁻¹' (e.target ∩ s) = e.source ∩ e ⁻¹' s := ext fun _ => ⟨fun hx => ⟨hx.1, hx.2.2⟩, fun hx => ⟨hx.1, e.map_source hx.1, hx.2⟩⟩ theorem target_inter_inv_preimage_preimage (s : Set β) : e.target ∩ e.symm ⁻¹' (e ⁻¹' s) = e.target ∩ s := e.symm.source_inter_preimage_inv_preimage _ theorem symm_image_image_of_subset_source {s : Set α} (h : s ⊆ e.source) : e.symm '' (e '' s) = s := (e.leftInvOn.mono h).image_image theorem image_symm_image_of_subset_target {s : Set β} (h : s ⊆ e.target) : e '' (e.symm '' s) = s := e.symm.symm_image_image_of_subset_source h theorem source_subset_preimage_target : e.source ⊆ e ⁻¹' e.target := e.mapsTo theorem symm_image_target_eq_source : e.symm '' e.target = e.source := e.symm.image_source_eq_target theorem target_subset_preimage_source : e.target ⊆ e.symm ⁻¹' e.source := e.symm_mapsTo /-- Two partial equivs that have the same `source`, same `toFun` and same `invFun`, coincide. -/ @[ext] protected theorem ext {e e' : PartialEquiv α β} (h : ∀ x, e x = e' x) (hsymm : ∀ x, e.symm x = e'.symm x) (hs : e.source = e'.source) : e = e' := by have A : (e : α → β) = e' := by ext x exact h x have B : (e.symm : β → α) = e'.symm := by ext x exact hsymm x have I : e '' e.source = e.target := e.image_source_eq_target have I' : e' '' e'.source = e'.target := e'.image_source_eq_target rw [A, hs, I'] at I cases e; cases e' simp_all /-- Restricting a partial equivalence to `e.source ∩ s` -/ protected def restr (s : Set α) : PartialEquiv α β := (@IsImage.of_symm_preimage_eq α β e s (e.symm ⁻¹' s) rfl).restr @[simp, mfld_simps] theorem restr_coe (s : Set α) : (e.restr s : α → β) = e := rfl @[simp, mfld_simps] theorem restr_coe_symm (s : Set α) : ((e.restr s).symm : β → α) = e.symm := rfl @[simp, mfld_simps] theorem restr_source (s : Set α) : (e.restr s).source = e.source ∩ s := rfl theorem source_restr_subset_source (s : Set α) : (e.restr s).source ⊆ e.source := inter_subset_left @[simp, mfld_simps] theorem restr_target (s : Set α) : (e.restr s).target = e.target ∩ e.symm ⁻¹' s := rfl theorem restr_eq_of_source_subset {e : PartialEquiv α β} {s : Set α} (h : e.source ⊆ s) : e.restr s = e := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [inter_eq_self_of_subset_left h]) @[simp, mfld_simps] theorem restr_univ {e : PartialEquiv α β} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) /-- The identity partial equiv -/ protected def refl (α : Type*) : PartialEquiv α α := (Equiv.refl α).toPartialEquiv @[simp, mfld_simps] theorem refl_source : (PartialEquiv.refl α).source = univ := rfl @[simp, mfld_simps] theorem refl_target : (PartialEquiv.refl α).target = univ := rfl @[simp, mfld_simps] theorem refl_coe : (PartialEquiv.refl α : α → α) = id := rfl @[simp, mfld_simps] theorem refl_symm : (PartialEquiv.refl α).symm = PartialEquiv.refl α := rfl @[mfld_simps] theorem refl_restr_source (s : Set α) : ((PartialEquiv.refl α).restr s).source = s := by simp @[mfld_simps] theorem refl_restr_target (s : Set α) : ((PartialEquiv.refl α).restr s).target = s := by simp /-- The identity partial equivalence on a set `s` -/ def ofSet (s : Set α) : PartialEquiv α α where toFun := id invFun := id source := s target := s map_source' _ hx := hx map_target' _ hx := hx left_inv' _ _ := rfl right_inv' _ _ := rfl @[simp, mfld_simps] theorem ofSet_source (s : Set α) : (PartialEquiv.ofSet s).source = s := rfl @[simp, mfld_simps] theorem ofSet_target (s : Set α) : (PartialEquiv.ofSet s).target = s := rfl @[simp, mfld_simps] theorem ofSet_coe (s : Set α) : (PartialEquiv.ofSet s : α → α) = id := rfl @[simp, mfld_simps] theorem ofSet_symm (s : Set α) : (PartialEquiv.ofSet s).symm = PartialEquiv.ofSet s := rfl /-- `Function.const` as a `PartialEquiv`. It consists of two constant maps in opposite directions. -/ @[simps] def single (a : α) (b : β) : PartialEquiv α β where toFun := Function.const α b invFun := Function.const β a source := {a} target := {b} map_source' _ _ := rfl map_target' _ _ := rfl left_inv' a' ha' := by rw [eq_of_mem_singleton ha', const_apply] right_inv' b' hb' := by rw [eq_of_mem_singleton hb', const_apply] /-- Composing two partial equivs if the target of the first coincides with the source of the second. -/ @[simps] protected def trans' (e' : PartialEquiv β γ) (h : e.target = e'.source) : PartialEquiv α γ where toFun := e' ∘ e invFun := e.symm ∘ e'.symm source := e.source target := e'.target map_source' x hx := by simp [← h, hx] map_target' y hy := by simp [h, hy] left_inv' x hx := by simp [hx, ← h] right_inv' y hy := by simp [hy, h] /-- Composing two partial equivs, by restricting to the maximal domain where their composition is well defined. Within the `Manifold` namespace, there is the notation `e ≫ f` for this. -/ @[trans] protected def trans : PartialEquiv α γ := PartialEquiv.trans' (e.symm.restr e'.source).symm (e'.restr e.target) (inter_comm _ _) @[simp, mfld_simps] theorem coe_trans : (e.trans e' : α → γ) = e' ∘ e := rfl @[simp, mfld_simps] theorem coe_trans_symm : ((e.trans e').symm : γ → α) = e.symm ∘ e'.symm := rfl theorem trans_apply {x : α} : (e.trans e') x = e' (e x) := rfl theorem trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; rfl @[simp, mfld_simps] theorem trans_source : (e.trans e').source = e.source ∩ e ⁻¹' e'.source := rfl theorem trans_source' : (e.trans e').source = e.source ∩ e ⁻¹' (e.target ∩ e'.source) := by mfld_set_tac theorem trans_source'' : (e.trans e').source = e.symm '' (e.target ∩ e'.source) := by rw [e.trans_source', e.symm_image_target_inter_eq] theorem image_trans_source : e '' (e.trans e').source = e.target ∩ e'.source := (e.symm.restr e'.source).symm.image_source_eq_target @[simp, mfld_simps] theorem trans_target : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' e.target := rfl theorem trans_target' : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm theorem trans_target'' : (e.trans e').target = e' '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm theorem inv_image_trans_target : e'.symm '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm theorem trans_assoc (e'' : PartialEquiv γ δ) : (e.trans e').trans e'' = e.trans (e'.trans e'') := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source, @preimage_comp α β γ, inter_assoc]) @[simp, mfld_simps] theorem trans_refl : e.trans (PartialEquiv.refl β) = e := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source]) @[simp, mfld_simps] theorem refl_trans : (PartialEquiv.refl α).trans e = e := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source, preimage_id]) theorem trans_ofSet (s : Set β) : e.trans (ofSet s) = e.restr (e ⁻¹' s) := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) rfl theorem trans_refl_restr (s : Set β) : e.trans ((PartialEquiv.refl β).restr s) = e.restr (e ⁻¹' s) := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source]) theorem trans_refl_restr' (s : Set β) : e.trans ((PartialEquiv.refl β).restr s) = e.restr (e.source ∩ e ⁻¹' s) := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) <| by simp only [trans_source, restr_source, refl_source, univ_inter] rw [← inter_assoc, inter_self] theorem restr_trans (s : Set α) : (e.restr s).trans e' = (e.trans e').restr s := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) <| by simp [trans_source, inter_comm, inter_assoc] /-- A lemma commonly useful when `e` and `e'` are charts of a manifold. -/ theorem mem_symm_trans_source {e' : PartialEquiv α γ} {x : α} (he : x ∈ e.source) (he' : x ∈ e'.source) : e x ∈ (e.symm.trans e').source := ⟨e.mapsTo he, by rwa [mem_preimage, PartialEquiv.symm_symm, e.left_inv he]⟩ /-- `EqOnSource e e'` means that `e` and `e'` have the same source, and coincide there. Then `e` and `e'` should really be considered the same partial equiv. -/ def EqOnSource (e e' : PartialEquiv α β) : Prop := e.source = e'.source ∧ e.source.EqOn e e' /-- `EqOnSource` is an equivalence relation. This instance provides the `≈` notation between two `PartialEquiv`s. -/ instance eqOnSourceSetoid : Setoid (PartialEquiv α β) where r := EqOnSource iseqv := by constructor <;> simp only [Equivalence, EqOnSource, EqOn] <;> aesop theorem eqOnSource_refl : e ≈ e := Setoid.refl _ /-- Two equivalent partial equivs have the same source. -/ theorem EqOnSource.source_eq {e e' : PartialEquiv α β} (h : e ≈ e') : e.source = e'.source := h.1 /-- Two equivalent partial equivs coincide on the source. -/ theorem EqOnSource.eqOn {e e' : PartialEquiv α β} (h : e ≈ e') : e.source.EqOn e e' := h.2 /-- Two equivalent partial equivs have the same target. -/ theorem EqOnSource.target_eq {e e' : PartialEquiv α β} (h : e ≈ e') : e.target = e'.target := by simp only [← image_source_eq_target, ← source_eq h, h.2.image_eq] /-- If two partial equivs are equivalent, so are their inverses. -/ theorem EqOnSource.symm' {e e' : PartialEquiv α β} (h : e ≈ e') : e.symm ≈ e'.symm := by refine ⟨target_eq h, eqOn_of_leftInvOn_of_rightInvOn e.leftInvOn ?_ ?_⟩ <;> simp only [symm_source, target_eq h, source_eq h, e'.symm_mapsTo] exact e'.rightInvOn.congr_right e'.symm_mapsTo (source_eq h ▸ h.eqOn.symm) /-- Two equivalent partial equivs have coinciding inverses on the target. -/ theorem EqOnSource.symm_eqOn {e e' : PartialEquiv α β} (h : e ≈ e') : EqOn e.symm e'.symm e.target := -- Porting note: `h.symm'` dot notation doesn't work anymore because `h` is not recognised as -- `PartialEquiv.EqOnSource` for some reason. eqOn (symm' h) /-- Composition of partial equivs respects equivalence. -/ theorem EqOnSource.trans' {e e' : PartialEquiv α β} {f f' : PartialEquiv β γ} (he : e ≈ e') (hf : f ≈ f') : e.trans f ≈ e'.trans f' := by constructor · rw [trans_source'', trans_source'', ← target_eq he, ← hf.1] exact (he.symm'.eqOn.mono inter_subset_left).image_eq · intro x hx rw [trans_source] at hx simp [Function.comp_apply, PartialEquiv.coe_trans, (he.2 hx.1).symm, hf.2 hx.2] /-- Restriction of partial equivs respects equivalence. -/ theorem EqOnSource.restr {e e' : PartialEquiv α β} (he : e ≈ e') (s : Set α) : e.restr s ≈ e'.restr s := by constructor · simp [he.1] · intro x hx simp only [mem_inter_iff, restr_source] at hx exact he.2 hx.1 /-- Preimages are respected by equivalence. -/ theorem EqOnSource.source_inter_preimage_eq {e e' : PartialEquiv α β} (he : e ≈ e') (s : Set β) : e.source ∩ e ⁻¹' s = e'.source ∩ e' ⁻¹' s := by rw [he.eqOn.inter_preimage_eq, source_eq he] /-- Composition of a partial equivalence and its inverse is equivalent to the restriction of the identity to the source. -/ theorem self_trans_symm : e.trans e.symm ≈ ofSet e.source := by have A : (e.trans e.symm).source = e.source := by mfld_set_tac refine ⟨by rw [A, ofSet_source], fun x hx => ?_⟩ rw [A] at hx simp only [hx, mfld_simps] /-- Composition of the inverse of a partial equivalence and this partial equivalence is equivalent to the restriction of the identity to the target. -/ theorem symm_trans_self : e.symm.trans e ≈ ofSet e.target := self_trans_symm e.symm /-- Two equivalent partial equivs are equal when the source and target are `univ`. -/ theorem eq_of_eqOnSource_univ (e e' : PartialEquiv α β) (h : e ≈ e') (s : e.source = univ) (t : e.target = univ) : e = e' := by refine PartialEquiv.ext (fun x => ?_) (fun x => ?_) h.1 · apply h.2 rw [s] exact mem_univ _ · apply h.symm'.2 rw [symm_source, t] exact mem_univ _ section Prod /-- The product of two partial equivalences, as a partial equivalence on the product. -/ def prod (e : PartialEquiv α β) (e' : PartialEquiv γ δ) : PartialEquiv (α × γ) (β × δ) where source := e.source ×ˢ e'.source target := e.target ×ˢ e'.target toFun p := (e p.1, e' p.2) invFun p := (e.symm p.1, e'.symm p.2) map_source' p hp := by simp_all map_target' p hp := by simp_all left_inv' p hp := by simp_all right_inv' p hp := by simp_all @[simp, mfld_simps] theorem prod_source (e : PartialEquiv α β) (e' : PartialEquiv γ δ) : (e.prod e').source = e.source ×ˢ e'.source := rfl @[simp, mfld_simps] theorem prod_target (e : PartialEquiv α β) (e' : PartialEquiv γ δ) : (e.prod e').target = e.target ×ˢ e'.target := rfl @[simp, mfld_simps] theorem prod_coe (e : PartialEquiv α β) (e' : PartialEquiv γ δ) : (e.prod e' : α × γ → β × δ) = fun p => (e p.1, e' p.2) := rfl theorem prod_coe_symm (e : PartialEquiv α β) (e' : PartialEquiv γ δ) : ((e.prod e').symm : β × δ → α × γ) = fun p => (e.symm p.1, e'.symm p.2) := rfl @[simp, mfld_simps] theorem prod_symm (e : PartialEquiv α β) (e' : PartialEquiv γ δ) : (e.prod e').symm = e.symm.prod e'.symm := by ext x <;> simp [prod_coe_symm] @[simp, mfld_simps] theorem refl_prod_refl : (PartialEquiv.refl α).prod (PartialEquiv.refl β) = PartialEquiv.refl (α × β) := by ext ⟨x, y⟩ <;> simp @[simp, mfld_simps] theorem prod_trans {η : Type*} {ε : Type*} (e : PartialEquiv α β) (f : PartialEquiv β γ) (e' : PartialEquiv δ η) (f' : PartialEquiv η ε) : (e.prod e').trans (f.prod f') = (e.trans f).prod (e'.trans f') := by ext ⟨x, y⟩ <;> simp [Set.ext_iff]; tauto end Prod /-- Combine two `PartialEquiv`s using `Set.piecewise`. The source of the new `PartialEquiv` is `s.ite e.source e'.source = e.source ∩ s ∪ e'.source \ s`, and similarly for target. The function sends `e.source ∩ s` to `e.target ∩ t` using `e` and `e'.source \ s` to `e'.target \ t` using `e'`, and similarly for the inverse function. The definition assumes `e.isImage s t` and `e'.isImage s t`. -/ @[simps -fullyApplied] def piecewise (e e' : PartialEquiv α β) (s : Set α) (t : Set β) [∀ x, Decidable (x ∈ s)] [∀ y, Decidable (y ∈ t)] (H : e.IsImage s t) (H' : e'.IsImage s t) : PartialEquiv α β where toFun := s.piecewise e e' invFun := t.piecewise e.symm e'.symm source := s.ite e.source e'.source target := t.ite e.target e'.target map_source' := H.mapsTo.piecewise_ite H'.compl.mapsTo map_target' := H.symm.mapsTo.piecewise_ite H'.symm.compl.mapsTo left_inv' := H.leftInvOn_piecewise H' right_inv' := H.symm.leftInvOn_piecewise H'.symm theorem symm_piecewise (e e' : PartialEquiv α β) {s : Set α} {t : Set β} [∀ x, Decidable (x ∈ s)] [∀ y, Decidable (y ∈ t)] (H : e.IsImage s t) (H' : e'.IsImage s t) : (e.piecewise e' s t H H').symm = e.symm.piecewise e'.symm t s H.symm H'.symm := rfl /-- Combine two `PartialEquiv`s with disjoint sources and disjoint targets. We reuse `PartialEquiv.piecewise`, then override `source` and `target` to ensure better definitional equalities. -/ @[simps! -fullyApplied] def disjointUnion (e e' : PartialEquiv α β) (hs : Disjoint e.source e'.source) (ht : Disjoint e.target e'.target) [∀ x, Decidable (x ∈ e.source)] [∀ y, Decidable (y ∈ e.target)] : PartialEquiv α β := (e.piecewise e' e.source e.target e.isImage_source_target <| e'.isImage_source_target_of_disjoint _ hs.symm ht.symm).copy _ rfl _ rfl (e.source ∪ e'.source) (ite_left _ _) (e.target ∪ e'.target) (ite_left _ _) theorem disjointUnion_eq_piecewise (e e' : PartialEquiv α β) (hs : Disjoint e.source e'.source) (ht : Disjoint e.target e'.target) [∀ x, Decidable (x ∈ e.source)] [∀ y, Decidable (y ∈ e.target)] : e.disjointUnion e' hs ht = e.piecewise e' e.source e.target e.isImage_source_target (e'.isImage_source_target_of_disjoint _ hs.symm ht.symm) := copy_eq .. section Pi variable {ι : Type*} {αi βi γi : ι → Type*} /-- The product of a family of partial equivalences, as a partial equivalence on the pi type. -/ @[simps (config := mfld_cfg) apply source target] protected def pi (ei : ∀ i, PartialEquiv (αi i) (βi i)) : PartialEquiv (∀ i, αi i) (∀ i, βi i) where toFun := Pi.map fun i ↦ ei i invFun := Pi.map fun i ↦ (ei i).symm source := pi univ fun i => (ei i).source target := pi univ fun i => (ei i).target map_source' _ hf i hi := (ei i).map_source (hf i hi) map_target' _ hf i hi := (ei i).map_target (hf i hi) left_inv' _ hf := funext fun i => (ei i).left_inv (hf i trivial) right_inv' _ hf := funext fun i => (ei i).right_inv (hf i trivial) @[simp, mfld_simps] theorem pi_symm (ei : ∀ i, PartialEquiv (αi i) (βi i)) : (PartialEquiv.pi ei).symm = .pi fun i ↦ (ei i).symm := rfl theorem pi_symm_apply (ei : ∀ i, PartialEquiv (αi i) (βi i)) : ⇑(PartialEquiv.pi ei).symm = fun f i ↦ (ei i).symm (f i) := rfl @[simp, mfld_simps] theorem pi_refl : (PartialEquiv.pi fun i ↦ PartialEquiv.refl (αi i)) = .refl (∀ i, αi i) := by ext <;> simp @[simp, mfld_simps] theorem pi_trans (ei : ∀ i, PartialEquiv (αi i) (βi i)) (ei' : ∀ i, PartialEquiv (βi i) (γi i)) : (PartialEquiv.pi ei).trans (PartialEquiv.pi ei') = .pi fun i ↦ (ei i).trans (ei' i) := by ext <;> simp [forall_and] end Pi end PartialEquiv namespace Set -- All arguments are explicit to avoid missing information in the pretty printer output /-- A bijection between two sets `s : Set α` and `t : Set β` provides a partial equivalence between `α` and `β`. -/ @[simps -fullyApplied] noncomputable def BijOn.toPartialEquiv [Nonempty α] (f : α → β) (s : Set α) (t : Set β) (hf : BijOn f s t) : PartialEquiv α β where toFun := f invFun := invFunOn f s source := s target := t map_source' := hf.mapsTo map_target' := hf.surjOn.mapsTo_invFunOn left_inv' := hf.invOn_invFunOn.1 right_inv' := hf.invOn_invFunOn.2 /-- A map injective on a subset of its domain provides a partial equivalence. -/ @[simp, mfld_simps] noncomputable def InjOn.toPartialEquiv [Nonempty α] (f : α → β) (s : Set α) (hf : InjOn f s) : PartialEquiv α β := hf.bijOn_image.toPartialEquiv f s (f '' s) end Set namespace Equiv /- `Equiv`s give rise to `PartialEquiv`s. We set up simp lemmas to reduce most properties of the `PartialEquiv` to that of the `Equiv`. -/ variable (e : α ≃ β) (e' : β ≃ γ) @[simp, mfld_simps] theorem refl_toPartialEquiv : (Equiv.refl α).toPartialEquiv = PartialEquiv.refl α := rfl @[simp, mfld_simps] theorem symm_toPartialEquiv : e.symm.toPartialEquiv = e.toPartialEquiv.symm := rfl @[simp, mfld_simps] theorem trans_toPartialEquiv : (e.trans e').toPartialEquiv = e.toPartialEquiv.trans e'.toPartialEquiv := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [PartialEquiv.trans_source, Equiv.toPartialEquiv]) /-- Precompose a partial equivalence with an equivalence. We modify the source and target to have better definitional behavior. -/ @[simps!] def transPartialEquiv (e : α ≃ β) (f' : PartialEquiv β γ) : PartialEquiv α γ := (e.toPartialEquiv.trans f').copy _ rfl _ rfl (e ⁻¹' f'.source) (univ_inter _) f'.target (inter_univ _) theorem transPartialEquiv_eq_trans (e : α ≃ β) (f' : PartialEquiv β γ) : e.transPartialEquiv f' = e.toPartialEquiv.trans f' := PartialEquiv.copy_eq ..
@[simp, mfld_simps] theorem transPartialEquiv_trans (e : α ≃ β) (f' : PartialEquiv β γ) (f'' : PartialEquiv γ δ) : (e.transPartialEquiv f').trans f'' = e.transPartialEquiv (f'.trans f'') := by
Mathlib/Logic/Equiv/PartialEquiv.lean
936
939
/- 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
Mathlib/Data/List/Iterate.lean
38
40
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Oliver Nash -/ import Mathlib.Data.Finset.Card import Mathlib.Data.Finset.Union /-! # Finsets in product types This file defines finset constructions on the product type `α × β`. Beware not to confuse with the `Finset.prod` operation which computes the multiplicative product. ## Main declarations * `Finset.product`: Turns `s : Finset α`, `t : Finset β` into their product in `Finset (α × β)`. * `Finset.diag`: For `s : Finset α`, `s.diag` is the `Finset (α × α)` of pairs `(a, a)` with `a ∈ s`. * `Finset.offDiag`: For `s : Finset α`, `s.offDiag` is the `Finset (α × α)` of pairs `(a, b)` with `a, b ∈ s` and `a ≠ b`. -/ assert_not_exists MonoidWithZero open Multiset variable {α β γ : Type*} namespace Finset /-! ### prod -/ section Prod variable {s s' : Finset α} {t t' : Finset β} {a : α} {b : β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : Finset α) (t : Finset β) : Finset (α × β) := ⟨_, s.nodup.product t.nodup⟩ instance instSProd : SProd (Finset α) (Finset β) (Finset (α × β)) where sprod := Finset.product @[simp] theorem product_eq_sprod : Finset.product s t = s ×ˢ t := rfl @[simp] theorem product_val : (s ×ˢ t).1 = s.1 ×ˢ t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s ×ˢ t ↔ p.1 ∈ s ∧ p.2 ∈ t := Multiset.mem_product theorem mk_mem_product (ha : a ∈ s) (hb : b ∈ t) : (a, b) ∈ s ×ˢ t := mem_product.2 ⟨ha, hb⟩ @[simp, norm_cast] theorem coe_product (s : Finset α) (t : Finset β) : (↑(s ×ˢ t) : Set (α × β)) = (s : Set α) ×ˢ t := Set.ext fun _ => Finset.mem_product theorem subset_product_image_fst [DecidableEq α] : (s ×ˢ t).image Prod.fst ⊆ s := fun i => by simp +contextual [mem_image] theorem subset_product_image_snd [DecidableEq β] : (s ×ˢ t).image Prod.snd ⊆ t := fun i => by simp +contextual [mem_image]
theorem product_image_fst [DecidableEq α] (ht : t.Nonempty) : (s ×ˢ t).image Prod.fst = s := by ext i
Mathlib/Data/Finset/Prod.lean
72
73
/- 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.Order.Group.Multiset import Mathlib.Data.Setoid.Basic import Mathlib.Data.Vector.Basic import Mathlib.Logic.Nontrivial.Basic import Mathlib.Tactic.ApplyFun /-! # Symmetric powers This file defines symmetric powers of a type. The nth symmetric power consists of homogeneous n-tuples modulo permutations by the symmetric group. The special case of 2-tuples is called the symmetric square, which is addressed in more detail in `Data.Sym.Sym2`. TODO: This was created as supporting material for `Sym2`; it needs a fleshed-out interface. ## Tags symmetric powers -/ assert_not_exists MonoidWithZero open List (Vector) open Function /-- The nth symmetric power is n-tuples up to permutation. We define it as a subtype of `Multiset` since these are well developed in the library. We also give a definition `Sym.sym'` in terms of vectors, and we show these are equivalent in `Sym.symEquivSym'`. -/ def Sym (α : Type*) (n : ℕ) := { s : Multiset α // Multiset.card s = n } /-- The canonical map to `Multiset α` that forgets that `s` has length `n` -/ @[coe] def Sym.toMultiset {α : Type*} {n : ℕ} (s : Sym α n) : Multiset α := s.1 instance Sym.hasCoe (α : Type*) (n : ℕ) : CoeOut (Sym α n) (Multiset α) := ⟨Sym.toMultiset⟩ -- The following instance should be constructed by a deriving handler. -- https://github.com/leanprover-community/mathlib4/issues/380 instance {α : Type*} {n : ℕ} [DecidableEq α] : DecidableEq (Sym α n) := inferInstanceAs <| DecidableEq <| Subtype _ /-- This is the `List.Perm` setoid lifted to `Vector`. See note [reducible non-instances]. -/ abbrev List.Vector.Perm.isSetoid (α : Type*) (n : ℕ) : Setoid (Vector α n) := (List.isSetoid α).comap Subtype.val attribute [local instance] Vector.Perm.isSetoid -- Copy over the `DecidableRel` instance across the definition. -- (Although `List.Vector.Perm.isSetoid` is an `abbrev`, `List.isSetoid` is not.) instance {α : Type*} {n : ℕ} [DecidableEq α] : DecidableRel (· ≈ · : List.Vector α n → List.Vector α n → Prop) := fun _ _ => List.decidablePerm _ _ namespace Sym variable {α β : Type*} {n n' m : ℕ} {s : Sym α n} {a b : α} theorem coe_injective : Injective ((↑) : Sym α n → Multiset α) := Subtype.coe_injective @[simp, norm_cast] theorem coe_inj {s₁ s₂ : Sym α n} : (s₁ : Multiset α) = s₂ ↔ s₁ = s₂ := coe_injective.eq_iff @[ext] theorem ext {s₁ s₂ : Sym α n} (h : (s₁ : Multiset α) = ↑s₂) : s₁ = s₂ := coe_injective h @[simp] theorem val_eq_coe (s : Sym α n) : s.1 = ↑s := rfl /-- Construct an element of the `n`th symmetric power from a multiset of cardinality `n`. -/ @[match_pattern] abbrev mk (m : Multiset α) (h : Multiset.card m = n) : Sym α n := ⟨m, h⟩ /-- The unique element in `Sym α 0`. -/ @[match_pattern] def nil : Sym α 0 := ⟨0, Multiset.card_zero⟩ @[simp] theorem coe_nil : ↑(@Sym.nil α) = (0 : Multiset α) := rfl /-- Inserts an element into the term of `Sym α n`, increasing the length by one. -/ @[match_pattern] def cons (a : α) (s : Sym α n) : Sym α n.succ := ⟨a ::ₘ s.1, by rw [Multiset.card_cons, s.2]⟩ @[inherit_doc] infixr:67 " ::ₛ " => cons @[simp] theorem cons_inj_right (a : α) (s s' : Sym α n) : a ::ₛ s = a ::ₛ s' ↔ s = s' := Subtype.ext_iff.trans <| (Multiset.cons_inj_right _).trans Subtype.ext_iff.symm @[simp] theorem cons_inj_left (a a' : α) (s : Sym α n) : a ::ₛ s = a' ::ₛ s ↔ a = a' := Subtype.ext_iff.trans <| Multiset.cons_inj_left _ theorem cons_swap (a b : α) (s : Sym α n) : a ::ₛ b ::ₛ s = b ::ₛ a ::ₛ s := Subtype.ext <| Multiset.cons_swap a b s.1 theorem coe_cons (s : Sym α n) (a : α) : (a ::ₛ s : Multiset α) = a ::ₘ s := rfl /-- This is the quotient map that takes a list of n elements as an n-tuple and produces an nth symmetric power. -/ def ofVector : List.Vector α n → Sym α n := fun x => ⟨↑x.val, (Multiset.coe_card _).trans x.2⟩ /-- This is the quotient map that takes a list of n elements as an n-tuple and produces an nth symmetric power. -/ instance : Coe (List.Vector α n) (Sym α n) where coe x := ofVector x @[simp] theorem ofVector_nil : ↑(Vector.nil : List.Vector α 0) = (Sym.nil : Sym α 0) := rfl @[simp] theorem ofVector_cons (a : α) (v : List.Vector α n) : ↑(Vector.cons a v) = a ::ₛ (↑v : Sym α n) := by cases v rfl @[simp] theorem card_coe : Multiset.card (s : Multiset α) = n := s.prop /-- `α ∈ s` means that `a` appears as one of the factors in `s`. -/ instance : Membership α (Sym α n) := ⟨fun s a => a ∈ s.1⟩
instance decidableMem [DecidableEq α] (a : α) (s : Sym α n) : Decidable (a ∈ s) := s.1.decidableMem _
Mathlib/Data/Sym/Basic.lean
154
156
/- Copyright (c) 2023 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.Analysis.Calculus.Deriv.Comp import Mathlib.Analysis.Calculus.Deriv.Add import Mathlib.Analysis.Calculus.Deriv.Mul import Mathlib.Analysis.Calculus.Deriv.Slope /-! # Line derivatives We define the line derivative of a function `f : E → F`, at a point `x : E` along a vector `v : E`, as the element `f' : F` such that `f (x + t • v) = f x + t • f' + o (t)` as `t` tends to `0` in the scalar field `𝕜`, if it exists. It is denoted by `lineDeriv 𝕜 f x v`. This notion is generally less well behaved than the full Fréchet derivative (for instance, the composition of functions which are line-differentiable is not line-differentiable in general). The Fréchet derivative should therefore be favored over this one in general, although the line derivative may sometimes prove handy. The line derivative in direction `v` is also called the Gateaux derivative in direction `v`, although the term "Gateaux derivative" is sometimes reserved for the situation where there is such a derivative in all directions, for the map `v ↦ lineDeriv 𝕜 f x v` (which doesn't have to be linear in general). ## Main definition and results We mimic the definitions and statements for the Fréchet derivative and the one-dimensional derivative. We define in particular the following objects: * `LineDifferentiableWithinAt 𝕜 f s x v` * `LineDifferentiableAt 𝕜 f x v` * `HasLineDerivWithinAt 𝕜 f f' s x v` * `HasLineDerivAt 𝕜 f s x v` * `lineDerivWithin 𝕜 f s x v` * `lineDeriv 𝕜 f x v` and develop about them a basic API inspired by the one for the Fréchet derivative. We depart from the Fréchet derivative in two places, as the dependence of the following predicates on the direction would make them barely usable: * We do not define an analogue of the predicate `UniqueDiffOn`; * We do not define `LineDifferentiableOn` nor `LineDifferentiable`. -/ noncomputable section open scoped Topology Filter ENNReal NNReal open Filter Asymptotics Set variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] variable {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] section Module /-! Results that do not rely on a topological structure on `E` -/ variable (𝕜) variable {E : Type*} [AddCommGroup E] [Module 𝕜 E] /-- `f` has the derivative `f'` at the point `x` along the direction `v` in the set `s`. That is, `f (x + t v) = f x + t • f' + o (t)` when `t` tends to `0` and `x + t v ∈ s`. Note that this definition is less well behaved than the total Fréchet derivative, which should generally be favored over this one. -/ def HasLineDerivWithinAt (f : E → F) (f' : F) (s : Set E) (x : E) (v : E) := HasDerivWithinAt (fun t ↦ f (x + t • v)) f' ((fun t ↦ x + t • v) ⁻¹' s) (0 : 𝕜) /-- `f` has the derivative `f'` at the point `x` along the direction `v`. That is, `f (x + t v) = f x + t • f' + o (t)` when `t` tends to `0`. Note that this definition is less well behaved than the total Fréchet derivative, which should generally be favored over this one. -/ def HasLineDerivAt (f : E → F) (f' : F) (x : E) (v : E) := HasDerivAt (fun t ↦ f (x + t • v)) f' (0 : 𝕜) /-- `f` is line-differentiable at the point `x` in the direction `v` in the set `s` if there exists `f'` such that `f (x + t v) = f x + t • f' + o (t)` when `t` tends to `0` and `x + t v ∈ s`. -/ def LineDifferentiableWithinAt (f : E → F) (s : Set E) (x : E) (v : E) : Prop := DifferentiableWithinAt 𝕜 (fun t ↦ f (x + t • v)) ((fun t ↦ x + t • v) ⁻¹' s) (0 : 𝕜) /-- `f` is line-differentiable at the point `x` in the direction `v` if there exists `f'` such that `f (x + t v) = f x + t • f' + o (t)` when `t` tends to `0`. -/ def LineDifferentiableAt (f : E → F) (x : E) (v : E) : Prop := DifferentiableAt 𝕜 (fun t ↦ f (x + t • v)) (0 : 𝕜) /-- Line derivative of `f` at the point `x` in the direction `v` within the set `s`, if it exists. Zero otherwise. If the line derivative exists (i.e., `∃ f', HasLineDerivWithinAt 𝕜 f f' s x v`), then `f (x + t v) = f x + t lineDerivWithin 𝕜 f s x v + o (t)` when `t` tends to `0` and `x + t v ∈ s`. -/ def lineDerivWithin (f : E → F) (s : Set E) (x : E) (v : E) : F := derivWithin (fun t ↦ f (x + t • v)) ((fun t ↦ x + t • v) ⁻¹' s) (0 : 𝕜) /-- Line derivative of `f` at the point `x` in the direction `v`, if it exists. Zero otherwise. If the line derivative exists (i.e., `∃ f', HasLineDerivAt 𝕜 f f' x v`), then `f (x + t v) = f x + t lineDeriv 𝕜 f x v + o (t)` when `t` tends to `0`. -/ def lineDeriv (f : E → F) (x : E) (v : E) : F := deriv (fun t ↦ f (x + t • v)) (0 : 𝕜) variable {𝕜} variable {f f₁ : E → F} {f' f₀' f₁' : F} {s t : Set E} {x v : E} lemma HasLineDerivWithinAt.mono (hf : HasLineDerivWithinAt 𝕜 f f' s x v) (hst : t ⊆ s) : HasLineDerivWithinAt 𝕜 f f' t x v := HasDerivWithinAt.mono hf (preimage_mono hst) lemma HasLineDerivAt.hasLineDerivWithinAt (hf : HasLineDerivAt 𝕜 f f' x v) (s : Set E) : HasLineDerivWithinAt 𝕜 f f' s x v := HasDerivAt.hasDerivWithinAt hf lemma HasLineDerivWithinAt.lineDifferentiableWithinAt (hf : HasLineDerivWithinAt 𝕜 f f' s x v) : LineDifferentiableWithinAt 𝕜 f s x v := HasDerivWithinAt.differentiableWithinAt hf theorem HasLineDerivAt.lineDifferentiableAt (hf : HasLineDerivAt 𝕜 f f' x v) : LineDifferentiableAt 𝕜 f x v := HasDerivAt.differentiableAt hf theorem LineDifferentiableWithinAt.hasLineDerivWithinAt (h : LineDifferentiableWithinAt 𝕜 f s x v) : HasLineDerivWithinAt 𝕜 f (lineDerivWithin 𝕜 f s x v) s x v := DifferentiableWithinAt.hasDerivWithinAt h theorem LineDifferentiableAt.hasLineDerivAt (h : LineDifferentiableAt 𝕜 f x v) : HasLineDerivAt 𝕜 f (lineDeriv 𝕜 f x v) x v := DifferentiableAt.hasDerivAt h @[simp] lemma hasLineDerivWithinAt_univ : HasLineDerivWithinAt 𝕜 f f' univ x v ↔ HasLineDerivAt 𝕜 f f' x v := by simp only [HasLineDerivWithinAt, HasLineDerivAt, preimage_univ, hasDerivWithinAt_univ] theorem lineDerivWithin_zero_of_not_lineDifferentiableWithinAt (h : ¬LineDifferentiableWithinAt 𝕜 f s x v) : lineDerivWithin 𝕜 f s x v = 0 := derivWithin_zero_of_not_differentiableWithinAt h theorem lineDeriv_zero_of_not_lineDifferentiableAt (h : ¬LineDifferentiableAt 𝕜 f x v) : lineDeriv 𝕜 f x v = 0 := deriv_zero_of_not_differentiableAt h theorem hasLineDerivAt_iff_isLittleO_nhds_zero : HasLineDerivAt 𝕜 f f' x v ↔ (fun t : 𝕜 => f (x + t • v) - f x - t • f') =o[𝓝 0] fun t => t := by simp only [HasLineDerivAt, hasDerivAt_iff_isLittleO_nhds_zero, zero_add, zero_smul, add_zero] theorem HasLineDerivAt.unique (h₀ : HasLineDerivAt 𝕜 f f₀' x v) (h₁ : HasLineDerivAt 𝕜 f f₁' x v) : f₀' = f₁' := HasDerivAt.unique h₀ h₁ protected theorem HasLineDerivAt.lineDeriv (h : HasLineDerivAt 𝕜 f f' x v) : lineDeriv 𝕜 f x v = f' := by rw [h.unique h.lineDifferentiableAt.hasLineDerivAt] theorem lineDifferentiableWithinAt_univ : LineDifferentiableWithinAt 𝕜 f univ x v ↔ LineDifferentiableAt 𝕜 f x v := by simp only [LineDifferentiableWithinAt, LineDifferentiableAt, preimage_univ, differentiableWithinAt_univ] theorem LineDifferentiableAt.lineDifferentiableWithinAt (h : LineDifferentiableAt 𝕜 f x v) : LineDifferentiableWithinAt 𝕜 f s x v := (differentiableWithinAt_univ.2 h).mono (subset_univ _) @[simp] theorem lineDerivWithin_univ : lineDerivWithin 𝕜 f univ x v = lineDeriv 𝕜 f x v := by simp [lineDerivWithin, lineDeriv] theorem LineDifferentiableWithinAt.mono (h : LineDifferentiableWithinAt 𝕜 f t x v) (st : s ⊆ t) : LineDifferentiableWithinAt 𝕜 f s x v := (h.hasLineDerivWithinAt.mono st).lineDifferentiableWithinAt theorem HasLineDerivWithinAt.congr_mono (h : HasLineDerivWithinAt 𝕜 f f' s x v) (ht : EqOn f₁ f t) (hx : f₁ x = f x) (h₁ : t ⊆ s) : HasLineDerivWithinAt 𝕜 f₁ f' t x v := HasDerivWithinAt.congr_mono h (fun _ hy ↦ ht hy) (by simpa using hx) (preimage_mono h₁) theorem HasLineDerivWithinAt.congr (h : HasLineDerivWithinAt 𝕜 f f' s x v) (hs : EqOn f₁ f s) (hx : f₁ x = f x) : HasLineDerivWithinAt 𝕜 f₁ f' s x v := h.congr_mono hs hx (Subset.refl _) theorem HasLineDerivWithinAt.congr' (h : HasLineDerivWithinAt 𝕜 f f' s x v) (hs : EqOn f₁ f s) (hx : x ∈ s) : HasLineDerivWithinAt 𝕜 f₁ f' s x v := h.congr hs (hs hx) theorem LineDifferentiableWithinAt.congr_mono (h : LineDifferentiableWithinAt 𝕜 f s x v) (ht : EqOn f₁ f t) (hx : f₁ x = f x) (h₁ : t ⊆ s) : LineDifferentiableWithinAt 𝕜 f₁ t x v := (HasLineDerivWithinAt.congr_mono h.hasLineDerivWithinAt ht hx h₁).differentiableWithinAt theorem LineDifferentiableWithinAt.congr (h : LineDifferentiableWithinAt 𝕜 f s x v) (ht : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : LineDifferentiableWithinAt 𝕜 f₁ s x v := LineDifferentiableWithinAt.congr_mono h ht hx (Subset.refl _) theorem lineDerivWithin_congr (hs : EqOn f₁ f s) (hx : f₁ x = f x) : lineDerivWithin 𝕜 f₁ s x v = lineDerivWithin 𝕜 f s x v := derivWithin_congr (fun _ hy ↦ hs hy) (by simpa using hx) theorem lineDerivWithin_congr' (hs : EqOn f₁ f s) (hx : x ∈ s) : lineDerivWithin 𝕜 f₁ s x v = lineDerivWithin 𝕜 f s x v := lineDerivWithin_congr hs (hs hx) theorem hasLineDerivAt_iff_tendsto_slope_zero : HasLineDerivAt 𝕜 f f' x v ↔ Tendsto (fun (t : 𝕜) ↦ t⁻¹ • (f (x + t • v) - f x)) (𝓝[≠] 0) (𝓝 f') := by simp only [HasLineDerivAt, hasDerivAt_iff_tendsto_slope_zero, zero_add, zero_smul, add_zero] alias ⟨HasLineDerivAt.tendsto_slope_zero, _⟩ := hasLineDerivAt_iff_tendsto_slope_zero theorem HasLineDerivAt.tendsto_slope_zero_right [Preorder 𝕜] (h : HasLineDerivAt 𝕜 f f' x v) : Tendsto (fun (t : 𝕜) ↦ t⁻¹ • (f (x + t • v) - f x)) (𝓝[>] 0) (𝓝 f') := h.tendsto_slope_zero.mono_left (nhdsGT_le_nhdsNE 0) theorem HasLineDerivAt.tendsto_slope_zero_left [Preorder 𝕜] (h : HasLineDerivAt 𝕜 f f' x v) : Tendsto (fun (t : 𝕜) ↦ t⁻¹ • (f (x + t • v) - f x)) (𝓝[<] 0) (𝓝 f') := h.tendsto_slope_zero.mono_left (nhdsLT_le_nhdsNE 0) theorem HasLineDerivWithinAt.hasLineDerivAt' (h : HasLineDerivWithinAt 𝕜 f f' s x v) (hs : ∀ᶠ t : 𝕜 in 𝓝 0, x + t • v ∈ s) : HasLineDerivAt 𝕜 f f' x v := h.hasDerivAt hs end Module section NormedSpace /-! Results that need a normed space structure on `E` -/ variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {f f₀ f₁ : E → F} {f' : F} {s t : Set E} {x v : E} {L : E →L[𝕜] F} theorem HasLineDerivWithinAt.mono_of_mem_nhdsWithin (h : HasLineDerivWithinAt 𝕜 f f' t x v) (hst : t ∈ 𝓝[s] x) : HasLineDerivWithinAt 𝕜 f f' s x v := by apply HasDerivWithinAt.mono_of_mem_nhdsWithin h apply ContinuousWithinAt.preimage_mem_nhdsWithin'' _ hst (by simp) apply Continuous.continuousWithinAt; fun_prop @[deprecated (since := "2024-10-31")] alias HasLineDerivWithinAt.mono_of_mem := HasLineDerivWithinAt.mono_of_mem_nhdsWithin theorem HasLineDerivWithinAt.hasLineDerivAt (h : HasLineDerivWithinAt 𝕜 f f' s x v) (hs : s ∈ 𝓝 x) : HasLineDerivAt 𝕜 f f' x v := h.hasLineDerivAt' <| (Continuous.tendsto' (by fun_prop) 0 _ (by simp)).eventually hs theorem LineDifferentiableWithinAt.lineDifferentiableAt (h : LineDifferentiableWithinAt 𝕜 f s x v) (hs : s ∈ 𝓝 x) : LineDifferentiableAt 𝕜 f x v := (h.hasLineDerivWithinAt.hasLineDerivAt hs).lineDifferentiableAt lemma HasFDerivWithinAt.hasLineDerivWithinAt (hf : HasFDerivWithinAt f L s x) (v : E) : HasLineDerivWithinAt 𝕜 f (L v) s x v := by let F := fun (t : 𝕜) ↦ x + t • v rw [show x = F (0 : 𝕜) by simp [F]] at hf have A : HasDerivWithinAt F (0 + (1 : 𝕜) • v) (F ⁻¹' s) 0 := ((hasDerivAt_const (0 : 𝕜) x).add ((hasDerivAt_id' (0 : 𝕜)).smul_const v)).hasDerivWithinAt simp only [one_smul, zero_add] at A exact hf.comp_hasDerivWithinAt (x := (0 : 𝕜)) A (mapsTo_preimage F s) lemma HasFDerivAt.hasLineDerivAt (hf : HasFDerivAt f L x) (v : E) : HasLineDerivAt 𝕜 f (L v) x v := by rw [← hasLineDerivWithinAt_univ] exact hf.hasFDerivWithinAt.hasLineDerivWithinAt v lemma DifferentiableAt.lineDeriv_eq_fderiv (hf : DifferentiableAt 𝕜 f x) : lineDeriv 𝕜 f x v = fderiv 𝕜 f x v := (hf.hasFDerivAt.hasLineDerivAt v).lineDeriv theorem LineDifferentiableWithinAt.mono_of_mem_nhdsWithin (h : LineDifferentiableWithinAt 𝕜 f s x v) (hst : s ∈ 𝓝[t] x) : LineDifferentiableWithinAt 𝕜 f t x v := (h.hasLineDerivWithinAt.mono_of_mem_nhdsWithin hst).lineDifferentiableWithinAt @[deprecated (since := "2024-10-31")] alias LineDifferentiableWithinAt.mono_of_mem := LineDifferentiableWithinAt.mono_of_mem_nhdsWithin theorem lineDerivWithin_of_mem_nhds (h : s ∈ 𝓝 x) : lineDerivWithin 𝕜 f s x v = lineDeriv 𝕜 f x v := by apply derivWithin_of_mem_nhds apply (Continuous.continuousAt _).preimage_mem_nhds (by simpa using h) fun_prop theorem lineDerivWithin_of_isOpen (hs : IsOpen s) (hx : x ∈ s) : lineDerivWithin 𝕜 f s x v = lineDeriv 𝕜 f x v := lineDerivWithin_of_mem_nhds (hs.mem_nhds hx) theorem hasLineDerivWithinAt_congr_set (h : s =ᶠ[𝓝 x] t) : HasLineDerivWithinAt 𝕜 f f' s x v ↔ HasLineDerivWithinAt 𝕜 f f' t x v := by apply hasDerivWithinAt_congr_set let F := fun (t : 𝕜) ↦ x + t • v have B : ContinuousAt F 0 := by apply Continuous.continuousAt; fun_prop have : s =ᶠ[𝓝 (F 0)] t := by convert h; simp [F] exact B.preimage_mem_nhds this theorem lineDifferentiableWithinAt_congr_set (h : s =ᶠ[𝓝 x] t) : LineDifferentiableWithinAt 𝕜 f s x v ↔ LineDifferentiableWithinAt 𝕜 f t x v := ⟨fun h' ↦ ((hasLineDerivWithinAt_congr_set h).1 h'.hasLineDerivWithinAt).lineDifferentiableWithinAt, fun h' ↦ ((hasLineDerivWithinAt_congr_set h.symm).1 h'.hasLineDerivWithinAt).lineDifferentiableWithinAt⟩ theorem lineDerivWithin_congr_set (h : s =ᶠ[𝓝 x] t) : lineDerivWithin 𝕜 f s x v = lineDerivWithin 𝕜 f t x v := by apply derivWithin_congr_set let F := fun (t : 𝕜) ↦ x + t • v have B : ContinuousAt F 0 := by apply Continuous.continuousAt; fun_prop have : s =ᶠ[𝓝 (F 0)] t := by convert h; simp [F] exact B.preimage_mem_nhds this theorem Filter.EventuallyEq.hasLineDerivAt_iff (h : f₀ =ᶠ[𝓝 x] f₁) : HasLineDerivAt 𝕜 f₀ f' x v ↔ HasLineDerivAt 𝕜 f₁ f' x v := by apply hasDerivAt_iff let F := fun (t : 𝕜) ↦ x + t • v have B : ContinuousAt F 0 := by apply Continuous.continuousAt; fun_prop have : f₀ =ᶠ[𝓝 (F 0)] f₁ := by convert h; simp [F] exact B.preimage_mem_nhds this theorem Filter.EventuallyEq.lineDifferentiableAt_iff (h : f₀ =ᶠ[𝓝 x] f₁) : LineDifferentiableAt 𝕜 f₀ x v ↔ LineDifferentiableAt 𝕜 f₁ x v := ⟨fun h' ↦ (h.hasLineDerivAt_iff.1 h'.hasLineDerivAt).lineDifferentiableAt, fun h' ↦ (h.hasLineDerivAt_iff.2 h'.hasLineDerivAt).lineDifferentiableAt⟩ theorem Filter.EventuallyEq.hasLineDerivWithinAt_iff (h : f₀ =ᶠ[𝓝[s] x] f₁) (hx : f₀ x = f₁ x) : HasLineDerivWithinAt 𝕜 f₀ f' s x v ↔ HasLineDerivWithinAt 𝕜 f₁ f' s x v := by apply hasDerivWithinAt_iff · have A : Continuous (fun (t : 𝕜) ↦ x + t • v) := by fun_prop exact A.continuousWithinAt.preimage_mem_nhdsWithin'' h (by simp) · simpa using hx theorem Filter.EventuallyEq.hasLineDerivWithinAt_iff_of_mem (h : f₀ =ᶠ[𝓝[s] x] f₁) (hx : x ∈ s) : HasLineDerivWithinAt 𝕜 f₀ f' s x v ↔ HasLineDerivWithinAt 𝕜 f₁ f' s x v := h.hasLineDerivWithinAt_iff (h.eq_of_nhdsWithin hx) theorem Filter.EventuallyEq.lineDifferentiableWithinAt_iff (h : f₀ =ᶠ[𝓝[s] x] f₁) (hx : f₀ x = f₁ x) : LineDifferentiableWithinAt 𝕜 f₀ s x v ↔ LineDifferentiableWithinAt 𝕜 f₁ s x v := ⟨fun h' ↦ ((h.hasLineDerivWithinAt_iff hx).1 h'.hasLineDerivWithinAt).lineDifferentiableWithinAt, fun h' ↦ ((h.hasLineDerivWithinAt_iff hx).2 h'.hasLineDerivWithinAt).lineDifferentiableWithinAt⟩ theorem Filter.EventuallyEq.lineDifferentiableWithinAt_iff_of_mem (h : f₀ =ᶠ[𝓝[s] x] f₁) (hx : x ∈ s) : LineDifferentiableWithinAt 𝕜 f₀ s x v ↔ LineDifferentiableWithinAt 𝕜 f₁ s x v := h.lineDifferentiableWithinAt_iff (h.eq_of_nhdsWithin hx) lemma HasLineDerivWithinAt.congr_of_eventuallyEq (hf : HasLineDerivWithinAt 𝕜 f f' s x v) (h'f : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : HasLineDerivWithinAt 𝕜 f₁ f' s x v := by apply HasDerivWithinAt.congr_of_eventuallyEq hf _ (by simp [hx]) have A : Continuous (fun (t : 𝕜) ↦ x + t • v) := by fun_prop exact A.continuousWithinAt.preimage_mem_nhdsWithin'' h'f (by simp) theorem HasLineDerivAt.congr_of_eventuallyEq (h : HasLineDerivAt 𝕜 f f' x v) (h₁ : f₁ =ᶠ[𝓝 x] f) : HasLineDerivAt 𝕜 f₁ f' x v := by apply HasDerivAt.congr_of_eventuallyEq h let F := fun (t : 𝕜) ↦ x + t • v rw [show x = F 0 by simp [F]] at h₁ exact (Continuous.continuousAt (by fun_prop)).preimage_mem_nhds h₁ theorem LineDifferentiableWithinAt.congr_of_eventuallyEq (h : LineDifferentiableWithinAt 𝕜 f s x v) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : LineDifferentiableWithinAt 𝕜 f₁ s x v := (h.hasLineDerivWithinAt.congr_of_eventuallyEq h₁ hx).differentiableWithinAt theorem LineDifferentiableAt.congr_of_eventuallyEq (h : LineDifferentiableAt 𝕜 f x v) (hL : f₁ =ᶠ[𝓝 x] f) : LineDifferentiableAt 𝕜 f₁ x v := by apply DifferentiableAt.congr_of_eventuallyEq h let F := fun (t : 𝕜) ↦ x + t • v rw [show x = F 0 by simp [F]] at hL exact (Continuous.continuousAt (by fun_prop)).preimage_mem_nhds hL theorem Filter.EventuallyEq.lineDerivWithin_eq (hs : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : lineDerivWithin 𝕜 f₁ s x v = lineDerivWithin 𝕜 f s x v := by apply derivWithin_eq ?_ (by simpa using hx) have A : Continuous (fun (t : 𝕜) ↦ x + t • v) := by fun_prop exact A.continuousWithinAt.preimage_mem_nhdsWithin'' hs (by simp) theorem Filter.EventuallyEq.lineDerivWithin_eq_nhds (h : f₁ =ᶠ[𝓝 x] f) : lineDerivWithin 𝕜 f₁ s x v = lineDerivWithin 𝕜 f s x v := (h.filter_mono nhdsWithin_le_nhds).lineDerivWithin_eq h.self_of_nhds theorem Filter.EventuallyEq.lineDeriv_eq (h : f₁ =ᶠ[𝓝 x] f) : lineDeriv 𝕜 f₁ x v = lineDeriv 𝕜 f x v := by rw [← lineDerivWithin_univ, ← lineDerivWithin_univ, h.lineDerivWithin_eq_nhds] /-- Converse to the mean value inequality: if `f` is line differentiable at `x₀` and `C`-lipschitz on a neighborhood of `x₀` then its line derivative at `x₀` in the direction `v` has norm bounded by `C * ‖v‖`. This version only assumes that `‖f x - f x₀‖ ≤ C * ‖x - x₀‖` in a neighborhood of `x`. -/ theorem HasLineDerivAt.le_of_lip' {f : E → F} {f' : F} {x₀ : E} (hf : HasLineDerivAt 𝕜 f f' x₀ v) {C : ℝ} (hC₀ : 0 ≤ C) (hlip : ∀ᶠ x in 𝓝 x₀, ‖f x - f x₀‖ ≤ C * ‖x - x₀‖) : ‖f'‖ ≤ C * ‖v‖ := by apply HasDerivAt.le_of_lip' hf (by positivity) have A : Continuous (fun (t : 𝕜) ↦ x₀ + t • v) := by fun_prop have : ∀ᶠ x in 𝓝 (x₀ + (0 : 𝕜) • v), ‖f x - f x₀‖ ≤ C * ‖x - x₀‖ := by simpa using hlip filter_upwards [(A.continuousAt (x := 0)).preimage_mem_nhds this] with t ht simp only [preimage_setOf_eq, add_sub_cancel_left, norm_smul, mem_setOf_eq, mul_comm (‖t‖)] at ht simpa [mul_assoc] using ht /-- Converse to the mean value inequality: if `f` is line differentiable at `x₀` and `C`-lipschitz on a neighborhood of `x₀` then its line derivative at `x₀` in the direction `v` has norm bounded by `C * ‖v‖`. This version only assumes that `‖f x - f x₀‖ ≤ C * ‖x - x₀‖` in a neighborhood of `x`. -/ theorem HasLineDerivAt.le_of_lipschitzOn {f : E → F} {f' : F} {x₀ : E} (hf : HasLineDerivAt 𝕜 f f' x₀ v) {s : Set E} (hs : s ∈ 𝓝 x₀) {C : ℝ≥0} (hlip : LipschitzOnWith C f s) : ‖f'‖ ≤ C * ‖v‖ := by refine hf.le_of_lip' C.coe_nonneg ?_ filter_upwards [hs] with x hx using hlip.norm_sub_le hx (mem_of_mem_nhds hs) /-- Converse to the mean value inequality: if `f` is line differentiable at `x₀` and `C`-lipschitz then its line derivative at `x₀` in the direction `v` has norm bounded by `C * ‖v‖`. -/ theorem HasLineDerivAt.le_of_lipschitz {f : E → F} {f' : F} {x₀ : E} (hf : HasLineDerivAt 𝕜 f f' x₀ v) {C : ℝ≥0} (hlip : LipschitzWith C f) : ‖f'‖ ≤ C * ‖v‖ := hf.le_of_lipschitzOn univ_mem (lipschitzOnWith_univ.2 hlip) variable (𝕜) /-- Converse to the mean value inequality: if `f` is `C`-lipschitz on a neighborhood of `x₀` then its line derivative at `x₀` in the direction `v` has norm bounded by `C * ‖v‖`. This version only assumes that `‖f x - f x₀‖ ≤ C * ‖x - x₀‖` in a neighborhood of `x`. Version using `lineDeriv`. -/ theorem norm_lineDeriv_le_of_lip' {f : E → F} {x₀ : E} {C : ℝ} (hC₀ : 0 ≤ C) (hlip : ∀ᶠ x in 𝓝 x₀, ‖f x - f x₀‖ ≤ C * ‖x - x₀‖) : ‖lineDeriv 𝕜 f x₀ v‖ ≤ C * ‖v‖ := by apply norm_deriv_le_of_lip' (by positivity) have A : Continuous (fun (t : 𝕜) ↦ x₀ + t • v) := by fun_prop have : ∀ᶠ x in 𝓝 (x₀ + (0 : 𝕜) • v), ‖f x - f x₀‖ ≤ C * ‖x - x₀‖ := by simpa using hlip filter_upwards [(A.continuousAt (x := 0)).preimage_mem_nhds this] with t ht simp only [preimage_setOf_eq, add_sub_cancel_left, norm_smul, mem_setOf_eq, mul_comm (‖t‖)] at ht simpa [mul_assoc] using ht /-- Converse to the mean value inequality: if `f` is `C`-lipschitz on a neighborhood of `x₀` then its line derivative at `x₀` in the direction `v` has norm bounded by `C * ‖v‖`. Version using `lineDeriv`. -/ theorem norm_lineDeriv_le_of_lipschitzOn {f : E → F} {x₀ : E} {s : Set E} (hs : s ∈ 𝓝 x₀) {C : ℝ≥0} (hlip : LipschitzOnWith C f s) : ‖lineDeriv 𝕜 f x₀ v‖ ≤ C * ‖v‖ := by refine norm_lineDeriv_le_of_lip' 𝕜 C.coe_nonneg ?_ filter_upwards [hs] with x hx using hlip.norm_sub_le hx (mem_of_mem_nhds hs) /-- Converse to the mean value inequality: if `f` is `C`-lipschitz then its line derivative at `x₀` in the direction `v` has norm bounded by `C * ‖v‖`. Version using `lineDeriv`. -/ theorem norm_lineDeriv_le_of_lipschitz {f : E → F} {x₀ : E} {C : ℝ≥0} (hlip : LipschitzWith C f) : ‖lineDeriv 𝕜 f x₀ v‖ ≤ C * ‖v‖ := norm_lineDeriv_le_of_lipschitzOn 𝕜 univ_mem (lipschitzOnWith_univ.2 hlip) variable {𝕜} end NormedSpace section Zero variable {E : Type*} [AddCommGroup E] [Module 𝕜 E] {f : E → F} {s : Set E} {x : E} theorem hasLineDerivWithinAt_zero : HasLineDerivWithinAt 𝕜 f 0 s x 0 := by simp [HasLineDerivWithinAt, hasDerivWithinAt_const] theorem hasLineDerivAt_zero : HasLineDerivAt 𝕜 f 0 x 0 := by simp [HasLineDerivAt, hasDerivAt_const] theorem lineDifferentiableWithinAt_zero : LineDifferentiableWithinAt 𝕜 f s x 0 := hasLineDerivWithinAt_zero.lineDifferentiableWithinAt theorem lineDifferentiableAt_zero : LineDifferentiableAt 𝕜 f x 0 := hasLineDerivAt_zero.lineDifferentiableAt theorem lineDeriv_zero : lineDeriv 𝕜 f x 0 = 0 := hasLineDerivAt_zero.lineDeriv end Zero section CompRight variable {E : Type*} [AddCommGroup E] [Module 𝕜 E] {E' : Type*} [AddCommGroup E'] [Module 𝕜 E'] {f : E → F} {f' : F} {x : E'} {L : E' →ₗ[𝕜] E} theorem HasLineDerivAt.of_comp {v : E'} (hf : HasLineDerivAt 𝕜 (f ∘ L) f' x v) : HasLineDerivAt 𝕜 f f' (L x) (L v) := by simpa [HasLineDerivAt] using hf theorem LineDifferentiableAt.of_comp {v : E'} (hf : LineDifferentiableAt 𝕜 (f ∘ L) x v) : LineDifferentiableAt 𝕜 f (L x) (L v) := hf.hasLineDerivAt.of_comp.lineDifferentiableAt end CompRight section SMul variable {E : Type*} [AddCommGroup E] [Module 𝕜 E] {f : E → F} {s : Set E} {x v : E} {f' : F} theorem HasLineDerivWithinAt.smul (h : HasLineDerivWithinAt 𝕜 f f' s x v) (c : 𝕜) : HasLineDerivWithinAt 𝕜 f (c • f') s x (c • v) := by simp only [HasLineDerivWithinAt] at h ⊢ let g := fun (t : 𝕜) ↦ c • t let s' := (fun (t : 𝕜) ↦ x + t • v) ⁻¹' s have A : HasDerivAt g c 0 := by simpa using (hasDerivAt_id (0 : 𝕜)).const_smul c have B : HasDerivWithinAt (fun t ↦ f (x + t • v)) f' s' (g 0) := by simpa [g] using h have Z := B.scomp (0 : 𝕜) A.hasDerivWithinAt (mapsTo_preimage g s') simp only [g, s', Function.comp_def, smul_eq_mul, mul_comm c, ← smul_smul] at Z convert Z ext t simp [← smul_smul] theorem hasLineDerivWithinAt_smul_iff {c : 𝕜} (hc : c ≠ 0) : HasLineDerivWithinAt 𝕜 f (c • f') s x (c • v) ↔ HasLineDerivWithinAt 𝕜 f f' s x v := ⟨fun h ↦ by simpa [smul_smul, inv_mul_cancel₀ hc] using h.smul (c ⁻¹), fun h ↦ h.smul c⟩ theorem HasLineDerivAt.smul (h : HasLineDerivAt 𝕜 f f' x v) (c : 𝕜) : HasLineDerivAt 𝕜 f (c • f') x (c • v) := by simp only [← hasLineDerivWithinAt_univ] at h ⊢ exact HasLineDerivWithinAt.smul h c theorem hasLineDerivAt_smul_iff {c : 𝕜} (hc : c ≠ 0) : HasLineDerivAt 𝕜 f (c • f') x (c • v) ↔ HasLineDerivAt 𝕜 f f' x v := ⟨fun h ↦ by simpa [smul_smul, inv_mul_cancel₀ hc] using h.smul (c ⁻¹), fun h ↦ h.smul c⟩ theorem LineDifferentiableWithinAt.smul (h : LineDifferentiableWithinAt 𝕜 f s x v) (c : 𝕜) : LineDifferentiableWithinAt 𝕜 f s x (c • v) := (h.hasLineDerivWithinAt.smul c).lineDifferentiableWithinAt theorem lineDifferentiableWithinAt_smul_iff {c : 𝕜} (hc : c ≠ 0) : LineDifferentiableWithinAt 𝕜 f s x (c • v) ↔ LineDifferentiableWithinAt 𝕜 f s x v := ⟨fun h ↦ by simpa [smul_smul, inv_mul_cancel₀ hc] using h.smul (c ⁻¹), fun h ↦ h.smul c⟩ theorem LineDifferentiableAt.smul (h : LineDifferentiableAt 𝕜 f x v) (c : 𝕜) : LineDifferentiableAt 𝕜 f x (c • v) := (h.hasLineDerivAt.smul c).lineDifferentiableAt theorem lineDifferentiableAt_smul_iff {c : 𝕜} (hc : c ≠ 0) : LineDifferentiableAt 𝕜 f x (c • v) ↔ LineDifferentiableAt 𝕜 f x v := ⟨fun h ↦ by simpa [smul_smul, inv_mul_cancel₀ hc] using h.smul (c ⁻¹), fun h ↦ h.smul c⟩
theorem lineDeriv_smul {c : 𝕜} : lineDeriv 𝕜 f x (c • v) = c • lineDeriv 𝕜 f x v := by rcases eq_or_ne c 0 with rfl|hc
Mathlib/Analysis/Calculus/LineDeriv/Basic.lean
542
543
/- Copyright (c) 2022 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.Analysis.Normed.Group.Quotient import Mathlib.Analysis.NormedSpace.Pointwise import Mathlib.Topology.Instances.AddCircle /-! # The additive circle as a normed group We define the normed group structure on `AddCircle p`, for `p : ℝ`. For example if `p = 1` then: `‖(x : AddCircle 1)‖ = |x - round x|` for any `x : ℝ` (see `UnitAddCircle.norm_eq`). ## Main definitions: * `AddCircle.norm_eq`: a characterisation of the norm on `AddCircle p` ## TODO * The fact `InnerProductGeometry.angle (Real.cos θ) (Real.sin θ) = ‖(θ : Real.Angle)‖` -/ noncomputable section open Metric QuotientAddGroup Set open Int hiding mem_zmultiples_iff open AddSubgroup namespace AddCircle variable (p : ℝ) instance : NormedAddCommGroup (AddCircle p) := QuotientAddGroup.instNormedAddCommGroup _ @[simp] theorem norm_coe_mul (x : ℝ) (t : ℝ) : ‖(↑(t * x) : AddCircle (t * p))‖ = |t| * ‖(x : AddCircle p)‖ := by obtain rfl | ht := eq_or_ne t 0 · simp simp only [norm_eq_infDist, Real.norm_eq_abs, ← Real.norm_eq_abs, ← infDist_smul₀ ht, smul_zero] congr with m simp only [zmultiples, eq_iff_sub_mem, zsmul_eq_mul, mem_mk, mem_setOf_eq, mem_smul_set_iff_inv_smul_mem₀ ht, smul_eq_mul] simp_rw [mul_left_comm, ← smul_eq_mul, Set.range_smul, mem_smul_set_iff_inv_smul_mem₀ ht] simp [mul_sub, ht, -mem_range] theorem norm_neg_period (x : ℝ) : ‖(x : AddCircle (-p))‖ = ‖(x : AddCircle p)‖ := by suffices ‖(↑(-1 * x) : AddCircle (-1 * p))‖ = ‖(x : AddCircle p)‖ by rw [← this, neg_one_mul] simp simp only [norm_coe_mul, abs_neg, abs_one, one_mul] @[simp] theorem norm_eq_of_zero {x : ℝ} : ‖(x : AddCircle (0 : ℝ))‖ = |x| := by suffices { y : ℝ | (y : AddCircle (0 : ℝ)) = (x : AddCircle (0 : ℝ)) } = {x} by simp [norm_eq_infDist, this] ext y simp [eq_iff_sub_mem, mem_zmultiples_iff, sub_eq_zero] theorem norm_eq {x : ℝ} : ‖(x : AddCircle p)‖ = |x - round (p⁻¹ * x) * p| := by suffices ∀ x : ℝ, ‖(x : AddCircle (1 : ℝ))‖ = |x - round x| by rcases eq_or_ne p 0 with (rfl | hp) · simp have hx := norm_coe_mul p x p⁻¹ rw [abs_inv, eq_inv_mul_iff_mul_eq₀ ((not_congr abs_eq_zero).mpr hp)] at hx rw [← hx, inv_mul_cancel₀ hp, this, ← abs_mul, mul_sub, mul_inv_cancel_left₀ hp, mul_comm p] clear! x p intros x simp only [le_antisymm_iff, le_norm_iff, Real.norm_eq_abs] refine ⟨le_of_forall_le fun r hr ↦ ?_, ?_⟩ · rw [abs_sub_round_eq_min, le_inf_iff] rw [le_norm_iff] at hr constructor · simpa [abs_of_nonneg] using hr (fract x) · simpa [abs_sub_comm (fract x)] using hr (fract x - 1) (by simp [← self_sub_floor, ← sub_eq_zero, sub_sub]; simp) · simpa [zmultiples, QuotientAddGroup.eq, zsmul_eq_mul, mul_one, mem_mk, mem_range, and_imp, forall_exists_index, eq_neg_add_iff_add_eq, ← eq_sub_iff_add_eq, forall_swap (α := ℕ)] using round_le _ theorem norm_eq' (hp : 0 < p) {x : ℝ} : ‖(x : AddCircle p)‖ = p * |p⁻¹ * x - round (p⁻¹ * x)| := by conv_rhs => congr rw [← abs_eq_self.mpr hp.le] rw [← abs_mul, mul_sub, mul_inv_cancel_left₀ hp.ne.symm, norm_eq, mul_comm p] theorem norm_le_half_period {x : AddCircle p} (hp : p ≠ 0) : ‖x‖ ≤ |p| / 2 := by obtain ⟨x⟩ := x change ‖(x : AddCircle p)‖ ≤ |p| / 2 rw [norm_eq, ← mul_le_mul_left (abs_pos.mpr (inv_ne_zero hp)), ← abs_mul, mul_sub, mul_left_comm, ← mul_div_assoc, ← abs_mul, inv_mul_cancel₀ hp, mul_one, abs_one] exact abs_sub_round (p⁻¹ * x) @[simp] theorem norm_half_period_eq : ‖(↑(p / 2) : AddCircle p)‖ = |p| / 2 := by rcases eq_or_ne p 0 with (rfl | hp); · simp rw [norm_eq, ← mul_div_assoc, inv_mul_cancel₀ hp, one_div, round_two_inv, Int.cast_one, one_mul, (by linarith : p / 2 - p = -(p / 2)), abs_neg, abs_div, abs_two] theorem norm_coe_eq_abs_iff {x : ℝ} (hp : p ≠ 0) : ‖(x : AddCircle p)‖ = |x| ↔ |x| ≤ |p| / 2 := by refine ⟨fun hx => hx ▸ norm_le_half_period p hp, fun hx => ?_⟩ suffices ∀ p : ℝ, 0 < p → |x| ≤ p / 2 → ‖(x : AddCircle p)‖ = |x| by rcases hp.symm.lt_or_lt with (hp | hp) · rw [abs_eq_self.mpr hp.le] at hx exact this p hp hx · rw [← norm_neg_period] rw [abs_eq_neg_self.mpr hp.le] at hx exact this (-p) (neg_pos.mpr hp) hx clear hx intro p hp hx rcases eq_or_ne x (p / (2 : ℝ)) with (rfl | hx') · simp [abs_div, abs_two] suffices round (p⁻¹ * x) = 0 by simp [norm_eq, this] rw [round_eq_zero_iff] obtain ⟨hx₁, hx₂⟩ := abs_le.mp hx replace hx₂ := Ne.lt_of_le hx' hx₂ constructor · rwa [← mul_le_mul_left hp, ← mul_assoc, mul_inv_cancel₀ hp.ne.symm, one_mul, mul_neg, ← mul_div_assoc, mul_one] · rwa [← mul_lt_mul_left hp, ← mul_assoc, mul_inv_cancel₀ hp.ne.symm, one_mul, ← mul_div_assoc, mul_one] open Metric theorem closedBall_eq_univ_of_half_period_le (hp : p ≠ 0) (x : AddCircle p) {ε : ℝ} (hε : |p| / 2 ≤ ε) : closedBall x ε = univ := eq_univ_iff_forall.mpr fun x => by simpa only [mem_closedBall, dist_eq_norm] using (norm_le_half_period p hp).trans hε @[simp] theorem coe_real_preimage_closedBall_period_zero (x ε : ℝ) : (↑) ⁻¹' closedBall (x : AddCircle (0 : ℝ)) ε = closedBall x ε := by ext y simp [dist_eq_norm, ← QuotientAddGroup.mk_sub]
theorem coe_real_preimage_closedBall_eq_iUnion (x ε : ℝ) : (↑) ⁻¹' closedBall (x : AddCircle p) ε = ⋃ z : ℤ, closedBall (x + z • p) ε := by rcases eq_or_ne p 0 with (rfl | hp) · simp [iUnion_const] ext y simp only [dist_eq_norm, mem_preimage, mem_closedBall, zsmul_eq_mul, mem_iUnion, Real.norm_eq_abs, ← QuotientAddGroup.mk_sub, norm_eq, ← sub_sub] refine ⟨fun h => ⟨round (p⁻¹ * (y - x)), h⟩, ?_⟩ rintro ⟨n, hn⟩ rw [← mul_le_mul_left (abs_pos.mpr <| inv_ne_zero hp), ← abs_mul, mul_sub, mul_comm _ p, inv_mul_cancel_left₀ hp] at hn ⊢ exact (round_le (p⁻¹ * (y - x)) n).trans hn theorem coe_real_preimage_closedBall_inter_eq {x ε : ℝ} (s : Set ℝ) (hs : s ⊆ closedBall x (|p| / 2)) : (↑) ⁻¹' closedBall (x : AddCircle p) ε ∩ s = if ε < |p| / 2 then closedBall x ε ∩ s else s := by rcases le_or_lt (|p| / 2) ε with hε | hε · rcases eq_or_ne p 0 with (rfl | hp) · simp only [abs_zero, zero_div] at hε simp only [not_lt.mpr hε, coe_real_preimage_closedBall_period_zero, abs_zero, zero_div, if_false, inter_eq_right] exact hs.trans (closedBall_subset_closedBall <| by simp [hε]) simp [closedBall_eq_univ_of_half_period_le p hp (↑x) hε, not_lt.mpr hε]
Mathlib/Analysis/Normed/Group/AddCircle.lean
142
164
/- 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.Tape import Mathlib.Data.Fintype.Option import Mathlib.Data.Fintype.Prod import Mathlib.Data.Fintype.Pi import Mathlib.Data.PFun import Mathlib.Computability.PostTuringMachine /-! # Turing machines The files `PostTuringMachine.lean` and `TuringMachine.lean` define a sequence of simple machine languages, starting with Turing machines and working up to more complex languages based on Wang B-machines. `PostTuringMachine.lean` covers the TM0 model and TM1 model; `TuringMachine.lean` adds the TM2 model. ## Naming conventions Each model of computation in this file shares a naming convention for the elements of a model of computation. These are the parameters for the language: * `Γ` is the alphabet on the tape. * `Λ` is the set of labels, or internal machine states. * `σ` is the type of internal memory, not on the tape. This does not exist in the TM0 model, and later models achieve this by mixing it into `Λ`. * `K` is used in the TM2 model, which has multiple stacks, and denotes the number of such stacks. All of these variables denote "essentially finite" types, but for technical reasons it is convenient to allow them to be infinite anyway. When using an infinite type, we will be interested to prove that only finitely many values of the type are ever interacted with. Given these parameters, there are a few common structures for the model that arise: * `Stmt` is the set of all actions that can be performed in one step. For the TM0 model this set is finite, and for later models it is an infinite inductive type representing "possible program texts". * `Cfg` is the set of instantaneous configurations, that is, the state of the machine together with its environment. * `Machine` is the set of all machines in the model. Usually this is approximately a function `Λ → Stmt`, although different models have different ways of halting and other actions. * `step : Cfg → Option Cfg` is the function that describes how the state evolves over one step. If `step c = none`, then `c` is a terminal state, and the result of the computation is read off from `c`. Because of the type of `step`, these models are all deterministic by construction. * `init : Input → Cfg` sets up the initial state. The type `Input` depends on the model; in most cases it is `List Γ`. * `eval : Machine → Input → Part Output`, given a machine `M` and input `i`, starts from `init i`, runs `step` until it reaches an output, and then applies a function `Cfg → Output` to the final state to obtain the result. The type `Output` depends on the model. * `Supports : Machine → Finset Λ → Prop` asserts that a machine `M` starts in `S : Finset Λ`, and can only ever jump to other states inside `S`. This implies that the behavior of `M` on any input cannot depend on its values outside `S`. We use this to allow `Λ` to be an infinite set when convenient, and prove that only finitely many of these states are actually accessible. This formalizes "essentially finite" mentioned above. -/ assert_not_exists MonoidWithZero open List (Vector) open Relation open Nat (iterate) open Function (update iterate_succ iterate_succ_apply iterate_succ' iterate_succ_apply' iterate_zero_apply) namespace Turing /-! ## The TM2 model The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks, each with elements of different types (the alphabet of stack `k : K` is `Γ k`). The statements are: * `push k (f : σ → Γ k) q` puts `f a` on the `k`-th stack, then does `q`. * `pop k (f : σ → Option (Γ k) → σ) q` changes the state to `f a (S k).head`, where `S k` is the value of the `k`-th stack, and removes this element from the stack, then does `q`. * `peek k (f : σ → Option (Γ k) → σ) q` changes the state to `f a (S k).head`, where `S k` is the value of the `k`-th stack, then does `q`. * `load (f : σ → σ) q` reads nothing but applies `f` to the internal state, then does `q`. * `branch (f : σ → Bool) qtrue qfalse` does `qtrue` or `qfalse` according to `f a`. * `goto (f : σ → Λ)` jumps to label `f a`. * `halt` halts on the next step. The configuration is a tuple `(l, var, stk)` where `l : Option Λ` is the current label to run or `none` for the halting state, `var : σ` is the (finite) internal state, and `stk : ∀ k, List (Γ k)` is the collection of stacks. (Note that unlike the `TM0` and `TM1` models, these are not `ListBlank`s, they have definite ends that can be detected by the `pop` command.) Given a designated stack `k` and a value `L : List (Γ k)`, the initial configuration has all the stacks empty except the designated "input" stack; in `eval` this designated stack also functions as the output stack. -/ namespace TM2 variable {K : Type*} -- Index type of stacks variable (Γ : K → Type*) -- Type of stack elements variable (Λ : Type*) -- Type of function labels variable (σ : Type*) -- Type of variable settings /-- The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks. The operation `push` puts an element on one of the stacks, and `pop` removes an element from a stack (and modifying the internal state based on the result). `peek` modifies the internal state but does not remove an element. -/ inductive Stmt | push : ∀ k, (σ → Γ k) → Stmt → Stmt | peek : ∀ k, (σ → Option (Γ k) → σ) → Stmt → Stmt | pop : ∀ k, (σ → Option (Γ k) → σ) → Stmt → Stmt | load : (σ → σ) → Stmt → Stmt | branch : (σ → Bool) → Stmt → Stmt → Stmt | goto : (σ → Λ) → Stmt | halt : Stmt open Stmt instance Stmt.inhabited : Inhabited (Stmt Γ Λ σ) := ⟨halt⟩ /-- A configuration in the TM2 model is a label (or `none` for the halt state), the state of local variables, and the stacks. (Note that the stacks are not `ListBlank`s, they have a definite size.) -/ structure Cfg where /-- The current label to run (or `none` for the halting state) -/ l : Option Λ /-- The internal state -/ var : σ /-- The (finite) collection of internal stacks -/ stk : ∀ k, List (Γ k) instance Cfg.inhabited [Inhabited σ] : Inhabited (Cfg Γ Λ σ) := ⟨⟨default, default, default⟩⟩ variable {Γ Λ σ} section variable [DecidableEq K] /-- The step function for the TM2 model. -/ def stepAux : Stmt Γ Λ σ → σ → (∀ k, List (Γ k)) → Cfg Γ Λ σ | push k f q, v, S => stepAux q v (update S k (f v :: S k)) | peek k f q, v, S => stepAux q (f v (S k).head?) S | pop k f q, v, S => stepAux q (f v (S k).head?) (update S k (S k).tail) | load a q, v, S => stepAux q (a v) S | branch f q₁ q₂, v, S => cond (f v) (stepAux q₁ v S) (stepAux q₂ v S) | goto f, v, S => ⟨some (f v), v, S⟩ | halt, v, S => ⟨none, v, S⟩ /-- The step function for the TM2 model. -/ def step (M : Λ → Stmt Γ Λ σ) : Cfg Γ Λ σ → Option (Cfg Γ Λ σ) | ⟨none, _, _⟩ => none | ⟨some l, v, S⟩ => some (stepAux (M l) v S) attribute [simp] stepAux.eq_1 stepAux.eq_2 stepAux.eq_3 stepAux.eq_4 stepAux.eq_5 stepAux.eq_6 stepAux.eq_7 step.eq_1 step.eq_2 /-- The (reflexive) reachability relation for the TM2 model. -/ def Reaches (M : Λ → Stmt Γ Λ σ) : Cfg Γ Λ σ → Cfg Γ Λ σ → Prop := ReflTransGen fun a b ↦ b ∈ step M a end /-- Given a set `S` of states, `SupportsStmt S q` means that `q` only jumps to states in `S`. -/ def SupportsStmt (S : Finset Λ) : Stmt Γ Λ σ → Prop | push _ _ q => SupportsStmt S q | peek _ _ q => SupportsStmt S q | pop _ _ q => SupportsStmt S q | load _ q => SupportsStmt S q | branch _ q₁ q₂ => SupportsStmt S q₁ ∧ SupportsStmt S q₂ | goto l => ∀ v, l v ∈ S | halt => True section open scoped Classical in /-- The set of subtree statements in a statement. -/ noncomputable def stmts₁ : Stmt Γ Λ σ → Finset (Stmt Γ Λ σ) | Q@(push _ _ q) => insert Q (stmts₁ q) | Q@(peek _ _ q) => insert Q (stmts₁ q) | Q@(pop _ _ q) => insert Q (stmts₁ q) | Q@(load _ q) => insert Q (stmts₁ q) | Q@(branch _ q₁ q₂) => insert Q (stmts₁ q₁ ∪ stmts₁ q₂) | Q@(goto _) => {Q} | Q@halt => {Q} theorem stmts₁_self {q : Stmt Γ Λ σ} : q ∈ stmts₁ q := by cases q <;> simp only [Finset.mem_insert_self, Finset.mem_singleton_self, stmts₁] theorem stmts₁_trans {q₁ q₂ : Stmt Γ Λ σ} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := by classical intro h₁₂ q₀ h₀₁ induction q₂ with ( simp only [stmts₁] at h₁₂ ⊢ simp only [Finset.mem_insert, Finset.mem_singleton, Finset.mem_union] at h₁₂) | branch f q₁ q₂ IH₁ IH₂ => rcases h₁₂ with (rfl | h₁₂ | h₁₂) · unfold stmts₁ at h₀₁ exact h₀₁ · exact Finset.mem_insert_of_mem (Finset.mem_union_left _ (IH₁ h₁₂)) · exact Finset.mem_insert_of_mem (Finset.mem_union_right _ (IH₂ h₁₂)) | goto l => subst h₁₂; exact h₀₁ | halt => subst h₁₂; exact h₀₁ | load _ q IH | _ _ _ q IH => rcases h₁₂ with (rfl | h₁₂) · unfold stmts₁ at h₀₁ exact h₀₁ · exact Finset.mem_insert_of_mem (IH h₁₂) theorem stmts₁_supportsStmt_mono {S : Finset Λ} {q₁ q₂ : Stmt Γ Λ σ} (h : q₁ ∈ stmts₁ q₂) (hs : SupportsStmt S q₂) : SupportsStmt S q₁ := by induction q₂ with simp only [stmts₁, SupportsStmt, Finset.mem_insert, Finset.mem_union, Finset.mem_singleton] at h hs | branch f q₁ q₂ IH₁ IH₂ => rcases h with (rfl | h | h); exacts [hs, IH₁ h hs.1, IH₂ h hs.2] | goto l => subst h; exact hs | halt => subst h; trivial | load _ _ IH | _ _ _ _ IH => rcases h with (rfl | h) <;> [exact hs; exact IH h hs] open scoped Classical in /-- The set of statements accessible from initial set `S` of labels. -/ noncomputable def stmts (M : Λ → Stmt Γ Λ σ) (S : Finset Λ) : Finset (Option (Stmt Γ Λ σ)) := Finset.insertNone (S.biUnion fun q ↦ stmts₁ (M q)) theorem stmts_trans {M : Λ → Stmt Γ Λ σ} {S : Finset Λ} {q₁ q₂ : Stmt Γ Λ σ} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := by simp only [stmts, Finset.mem_insertNone, Finset.mem_biUnion, Option.mem_def, Option.some.injEq, forall_eq', exists_imp, and_imp] exact fun l ls h₂ ↦ ⟨_, ls, stmts₁_trans h₂ h₁⟩ end variable [Inhabited Λ] /-- Given a TM2 machine `M` and a set `S` of states, `Supports M S` means that all states in `S` jump only to other states in `S`. -/ def Supports (M : Λ → Stmt Γ Λ σ) (S : Finset Λ) := default ∈ S ∧ ∀ q ∈ S, SupportsStmt S (M q) theorem stmts_supportsStmt {M : Λ → Stmt Γ Λ σ} {S : Finset Λ} {q : Stmt Γ Λ σ} (ss : Supports M S) : some q ∈ stmts M S → SupportsStmt S q := by simp only [stmts, Finset.mem_insertNone, Finset.mem_biUnion, Option.mem_def, Option.some.injEq, forall_eq', exists_imp, and_imp] exact fun l ls h ↦ stmts₁_supportsStmt_mono h (ss.2 _ ls) variable [DecidableEq K] theorem step_supports (M : Λ → Stmt Γ Λ σ) {S : Finset Λ} (ss : Supports M S) : ∀ {c c' : Cfg Γ Λ σ}, c' ∈ step M c → c.l ∈ Finset.insertNone S → c'.l ∈ Finset.insertNone S | ⟨some l₁, v, T⟩, c', h₁, h₂ => by replace h₂ := ss.2 _ (Finset.some_mem_insertNone.1 h₂) simp only [step, Option.mem_def, Option.some.injEq] at h₁; subst c' revert h₂; induction M l₁ generalizing v T with intro hs | branch p q₁' q₂' IH₁ IH₂ => unfold stepAux; cases p v · exact IH₂ _ _ hs.2 · exact IH₁ _ _ hs.1 | goto => exact Finset.some_mem_insertNone.2 (hs _) | halt => apply Multiset.mem_cons_self | load _ _ IH | _ _ _ _ IH => exact IH _ _ hs variable [Inhabited σ] /-- The initial state of the TM2 model. The input is provided on a designated stack. -/ def init (k : K) (L : List (Γ k)) : Cfg Γ Λ σ := ⟨some default, default, update (fun _ ↦ []) k L⟩ /-- Evaluates a TM2 program to completion, with the output on the same stack as the input. -/ def eval (M : Λ → Stmt Γ Λ σ) (k : K) (L : List (Γ k)) : Part (List (Γ k)) := (Turing.eval (step M) (init k L)).map fun c ↦ c.stk k end TM2 /-! ## TM2 emulator in TM1 To prove that TM2 computable functions are TM1 computable, we need to reduce each TM2 program to a TM1 program. So suppose a TM2 program is given. This program has to maintain a whole collection of stacks, but we have only one tape, so we must "multiplex" them all together. Pictorially, if stack 1 contains `[a, b]` and stack 2 contains `[c, d, e, f]` then the tape looks like this: ``` bottom: ... | _ | T | _ | _ | _ | _ | ... stack 1: ... | _ | b | a | _ | _ | _ | ... stack 2: ... | _ | f | e | d | c | _ | ... ``` where a tape element is a vertical slice through the diagram. Here the alphabet is `Γ' := Bool × ∀ k, Option (Γ k)`, where: * `bottom : Bool` is marked only in one place, the initial position of the TM, and represents the tail of all stacks. It is never modified. * `stk k : Option (Γ k)` is the value of the `k`-th stack, if in range, otherwise `none` (which is the blank value). Note that the head of the stack is at the far end; this is so that push and pop don't have to do any shifting. In "resting" position, the TM is sitting at the position marked `bottom`. For non-stack actions, it operates in place, but for the stack actions `push`, `peek`, and `pop`, it must shuttle to the end of the appropriate stack, make its changes, and then return to the bottom. So the states are: * `normal (l : Λ)`: waiting at `bottom` to execute function `l` * `go k (s : StAct k) (q : Stmt₂)`: travelling to the right to get to the end of stack `k` in order to perform stack action `s`, and later continue with executing `q` * `ret (q : Stmt₂)`: travelling to the left after having performed a stack action, and executing `q` once we arrive Because of the shuttling, emulation overhead is `O(n)`, where `n` is the current maximum of the length of all stacks. Therefore a program that takes `k` steps to run in TM2 takes `O((m+k)k)` steps to run when emulated in TM1, where `m` is the length of the input. -/ namespace TM2to1 -- A displaced lemma proved in unnecessary generality theorem stk_nth_val {K : Type*} {Γ : K → Type*} {L : ListBlank (∀ k, Option (Γ k))} {k S} (n) (hL : ListBlank.map (proj k) L = ListBlank.mk (List.map some S).reverse) : L.nth n k = S.reverse[n]? := by rw [← proj_map_nth, hL, ← List.map_reverse, ListBlank.nth_mk, List.getI_eq_iget_getElem?, List.getElem?_map] cases S.reverse[n]? <;> rfl variable (K : Type*) variable (Γ : K → Type*) variable {Λ σ : Type*} /-- The alphabet of the TM2 simulator on TM1 is a marker for the stack bottom, plus a vector of stack elements for each stack, or none if the stack does not extend this far. -/ def Γ' := Bool × ∀ k, Option (Γ k) variable {K Γ} instance Γ'.inhabited : Inhabited (Γ' K Γ) := ⟨⟨false, fun _ ↦ none⟩⟩ instance Γ'.fintype [DecidableEq K] [Fintype K] [∀ k, Fintype (Γ k)] : Fintype (Γ' K Γ) := instFintypeProd _ _ /-- The bottom marker is fixed throughout the calculation, so we use the `addBottom` function to express the program state in terms of a tape with only the stacks themselves. -/ def addBottom (L : ListBlank (∀ k, Option (Γ k))) : ListBlank (Γ' K Γ) := ListBlank.cons (true, L.head) (L.tail.map ⟨Prod.mk false, rfl⟩) theorem addBottom_map (L : ListBlank (∀ k, Option (Γ k))) : (addBottom L).map ⟨Prod.snd, by rfl⟩ = L := by simp only [addBottom, ListBlank.map_cons] convert ListBlank.cons_head_tail L generalize ListBlank.tail L = L' refine L'.induction_on fun l ↦ ?_; simp theorem addBottom_modifyNth (f : (∀ k, Option (Γ k)) → ∀ k, Option (Γ k)) (L : ListBlank (∀ k, Option (Γ k))) (n : ℕ) : (addBottom L).modifyNth (fun a ↦ (a.1, f a.2)) n = addBottom (L.modifyNth f n) := by cases n <;> simp only [addBottom, ListBlank.head_cons, ListBlank.modifyNth, ListBlank.tail_cons] congr; symm; apply ListBlank.map_modifyNth; intro; rfl theorem addBottom_nth_snd (L : ListBlank (∀ k, Option (Γ k))) (n : ℕ) : ((addBottom L).nth n).2 = L.nth n := by conv => rhs; rw [← addBottom_map L, ListBlank.nth_map] theorem addBottom_nth_succ_fst (L : ListBlank (∀ k, Option (Γ k))) (n : ℕ) : ((addBottom L).nth (n + 1)).1 = false := by rw [ListBlank.nth_succ, addBottom, ListBlank.tail_cons, ListBlank.nth_map] theorem addBottom_head_fst (L : ListBlank (∀ k, Option (Γ k))) : (addBottom L).head.1 = true := by rw [addBottom, ListBlank.head_cons] variable (K Γ σ) in /-- A stack action is a command that interacts with the top of a stack. Our default position is at the bottom of all the stacks, so we have to hold on to this action while going to the end to modify the stack. -/ inductive StAct (k : K) | push : (σ → Γ k) → StAct k | peek : (σ → Option (Γ k) → σ) → StAct k | pop : (σ → Option (Γ k) → σ) → StAct k instance StAct.inhabited {k : K} : Inhabited (StAct K Γ σ k) := ⟨StAct.peek fun s _ ↦ s⟩ section open StAct /-- The TM2 statement corresponding to a stack action. -/ def stRun {k : K} : StAct K Γ σ k → TM2.Stmt Γ Λ σ → TM2.Stmt Γ Λ σ | push f => TM2.Stmt.push k f | peek f => TM2.Stmt.peek k f | pop f => TM2.Stmt.pop k f /-- The effect of a stack action on the local variables, given the value of the stack. -/ def stVar {k : K} (v : σ) (l : List (Γ k)) : StAct K Γ σ k → σ | push _ => v | peek f => f v l.head? | pop f => f v l.head? /-- The effect of a stack action on the stack. -/ def stWrite {k : K} (v : σ) (l : List (Γ k)) : StAct K Γ σ k → List (Γ k) | push f => f v :: l | peek _ => l | pop _ => l.tail /-- We have partitioned the TM2 statements into "stack actions", which require going to the end of the stack, and all other actions, which do not. This is a modified recursor which lumps the stack actions into one. -/ @[elab_as_elim] def stmtStRec.{l} {motive : TM2.Stmt Γ Λ σ → Sort l} (run : ∀ (k) (s : StAct K Γ σ k) (q) (_ : motive q), motive (stRun s q)) (load : ∀ (a q) (_ : motive q), motive (TM2.Stmt.load a q)) (branch : ∀ (p q₁ q₂) (_ : motive q₁) (_ : motive q₂), motive (TM2.Stmt.branch p q₁ q₂)) (goto : ∀ l, motive (TM2.Stmt.goto l)) (halt : motive TM2.Stmt.halt) : ∀ n, motive n | TM2.Stmt.push _ f q => run _ (push f) _ (stmtStRec run load branch goto halt q) | TM2.Stmt.peek _ f q => run _ (peek f) _ (stmtStRec run load branch goto halt q) | TM2.Stmt.pop _ f q => run _ (pop f) _ (stmtStRec run load branch goto halt q) | TM2.Stmt.load _ q => load _ _ (stmtStRec run load branch goto halt q) | TM2.Stmt.branch _ q₁ q₂ => branch _ _ _ (stmtStRec run load branch goto halt q₁) (stmtStRec run load branch goto halt q₂) | TM2.Stmt.goto _ => goto _ | TM2.Stmt.halt => halt theorem supports_run (S : Finset Λ) {k : K} (s : StAct K Γ σ k) (q : TM2.Stmt Γ Λ σ) : TM2.SupportsStmt S (stRun s q) ↔ TM2.SupportsStmt S q := by cases s <;> rfl end variable (K Γ Λ σ) /-- The machine states of the TM2 emulator. We can either be in a normal state when waiting for the next TM2 action, or we can be in the "go" and "return" states to go to the top of the stack and return to the bottom, respectively. -/ inductive Λ' | normal : Λ → Λ' | go (k : K) : StAct K Γ σ k → TM2.Stmt Γ Λ σ → Λ' | ret : TM2.Stmt Γ Λ σ → Λ' variable {K Γ Λ σ} open Λ' instance Λ'.inhabited [Inhabited Λ] : Inhabited (Λ' K Γ Λ σ) := ⟨normal default⟩ open TM1.Stmt section variable [DecidableEq K] /-- The program corresponding to state transitions at the end of a stack. Here we start out just after the top of the stack, and should end just after the new top of the stack. -/ def trStAct {k : K} (q : TM1.Stmt (Γ' K Γ) (Λ' K Γ Λ σ) σ) : StAct K Γ σ k → TM1.Stmt (Γ' K Γ) (Λ' K Γ Λ σ) σ | StAct.push f => (write fun a s ↦ (a.1, update a.2 k <| some <| f s)) <| move Dir.right q | StAct.peek f => move Dir.left <| (load fun a s ↦ f s (a.2 k)) <| move Dir.right q | StAct.pop f => branch (fun a _ ↦ a.1) (load (fun _ s ↦ f s none) q) (move Dir.left <| (load fun a s ↦ f s (a.2 k)) <| write (fun a _ ↦ (a.1, update a.2 k none)) q) /-- The initial state for the TM2 emulator, given an initial TM2 state. All stacks start out empty except for the input stack, and the stack bottom mark is set at the head. -/ def trInit (k : K) (L : List (Γ k)) : List (Γ' K Γ) := let L' : List (Γ' K Γ) := L.reverse.map fun a ↦ (false, update (fun _ ↦ none) k (some a)) (true, L'.headI.2) :: L'.tail theorem step_run {k : K} (q : TM2.Stmt Γ Λ σ) (v : σ) (S : ∀ k, List (Γ k)) : ∀ s : StAct K Γ σ k, TM2.stepAux (stRun s q) v S = TM2.stepAux q (stVar v (S k) s) (update S k (stWrite v (S k) s)) | StAct.push _ => rfl | StAct.peek f => by unfold stWrite; rw [Function.update_eq_self]; rfl | StAct.pop _ => rfl end /-- The translation of TM2 statements to TM1 statements. regular actions have direct equivalents, but stack actions are deferred by going to the corresponding `go` state, so that we can find the appropriate stack top. -/ def trNormal : TM2.Stmt Γ Λ σ → TM1.Stmt (Γ' K Γ) (Λ' K Γ Λ σ) σ | TM2.Stmt.push k f q => goto fun _ _ ↦ go k (StAct.push f) q | TM2.Stmt.peek k f q => goto fun _ _ ↦ go k (StAct.peek f) q | TM2.Stmt.pop k f q => goto fun _ _ ↦ go k (StAct.pop f) q | TM2.Stmt.load a q => load (fun _ ↦ a) (trNormal q) | TM2.Stmt.branch f q₁ q₂ => branch (fun _ ↦ f) (trNormal q₁) (trNormal q₂) | TM2.Stmt.goto l => goto fun _ s ↦ normal (l s) | TM2.Stmt.halt => halt theorem trNormal_run {k : K} (s : StAct K Γ σ k) (q : TM2.Stmt Γ Λ σ) : trNormal (stRun s q) = goto fun _ _ ↦ go k s q := by cases s <;> rfl section open scoped Classical in /-- The set of machine states accessible from an initial TM2 statement. -/ noncomputable def trStmts₁ : TM2.Stmt Γ Λ σ → Finset (Λ' K Γ Λ σ) | TM2.Stmt.push k f q => {go k (StAct.push f) q, ret q} ∪ trStmts₁ q | TM2.Stmt.peek k f q => {go k (StAct.peek f) q, ret q} ∪ trStmts₁ q | TM2.Stmt.pop k f q => {go k (StAct.pop f) q, ret q} ∪ trStmts₁ q | TM2.Stmt.load _ q => trStmts₁ q | TM2.Stmt.branch _ q₁ q₂ => trStmts₁ q₁ ∪ trStmts₁ q₂ | _ => ∅ theorem trStmts₁_run {k : K} {s : StAct K Γ σ k} {q : TM2.Stmt Γ Λ σ} : open scoped Classical in trStmts₁ (stRun s q) = {go k s q, ret q} ∪ trStmts₁ q := by cases s <;> simp only [trStmts₁, stRun] theorem tr_respects_aux₂ [DecidableEq K] {k : K} {q : TM1.Stmt (Γ' K Γ) (Λ' K Γ Λ σ) σ} {v : σ} {S : ∀ k, List (Γ k)} {L : ListBlank (∀ k, Option (Γ k))} (hL : ∀ k, L.map (proj k) = ListBlank.mk ((S k).map some).reverse) (o : StAct K Γ σ k) : let v' := stVar v (S k) o let Sk' := stWrite v (S k) o let S' := update S k Sk' ∃ L' : ListBlank (∀ k, Option (Γ k)), (∀ k, L'.map (proj k) = ListBlank.mk ((S' k).map some).reverse) ∧ TM1.stepAux (trStAct q o) v ((Tape.move Dir.right)^[(S k).length] (Tape.mk' ∅ (addBottom L))) = TM1.stepAux q v' ((Tape.move Dir.right)^[(S' k).length] (Tape.mk' ∅ (addBottom L'))) := by simp only [Function.update_self]; cases o with simp only [stWrite, stVar, trStAct, TM1.stepAux] | push f => have := Tape.write_move_right_n fun a : Γ' K Γ ↦ (a.1, update a.2 k (some (f v))) refine ⟨_, fun k' ↦ ?_, by -- Porting note: `rw [...]` to `erw [...]; rfl`. -- https://github.com/leanprover-community/mathlib4/issues/5164 rw [Tape.move_right_n_head, List.length, Tape.mk'_nth_nat, this] erw [addBottom_modifyNth fun a ↦ update a k (some (f v))] rw [Nat.add_one, iterate_succ'] rfl⟩ refine ListBlank.ext fun i ↦ ?_ rw [ListBlank.nth_map, ListBlank.nth_modifyNth, proj, PointedMap.mk_val] by_cases h' : k' = k · subst k' split_ifs with h <;> simp only [List.reverse_cons, Function.update_self, ListBlank.nth_mk, List.map] · rw [List.getI_eq_getElem _, List.getElem_append_right] <;> simp only [List.length_append, List.length_reverse, List.length_map, ← h, Nat.sub_self, List.length_singleton, List.getElem_singleton, le_refl, Nat.lt_succ_self] rw [← proj_map_nth, hL, ListBlank.nth_mk] rcases lt_or_gt_of_ne h with h | h · rw [List.getI_append] simpa only [List.length_map, List.length_reverse] using h · rw [gt_iff_lt] at h rw [List.getI_eq_default, List.getI_eq_default] <;> simp only [Nat.add_one_le_iff, h, List.length, le_of_lt, List.length_reverse, List.length_append, List.length_map] · split_ifs <;> rw [Function.update_of_ne h', ← proj_map_nth, hL] rw [Function.update_of_ne h'] | peek f => rw [Function.update_eq_self] use L, hL; rw [Tape.move_left_right]; congr cases e : S k; · rfl rw [List.length_cons, iterate_succ', Function.comp, Tape.move_right_left, Tape.move_right_n_head, Tape.mk'_nth_nat, addBottom_nth_snd, stk_nth_val _ (hL k), e, List.reverse_cons, ← List.length_reverse, List.getElem?_concat_length] rfl | pop f => rcases e : S k with - | ⟨hd, tl⟩ · simp only [Tape.mk'_head, ListBlank.head_cons, Tape.move_left_mk', List.length, Tape.write_mk', List.head?, iterate_zero_apply, List.tail_nil] rw [← e, Function.update_eq_self] exact ⟨L, hL, by rw [addBottom_head_fst, cond]⟩ · refine ⟨_, fun k' ↦ ?_, by erw [List.length_cons, Tape.move_right_n_head, Tape.mk'_nth_nat, addBottom_nth_succ_fst, cond_false, iterate_succ', Function.comp, Tape.move_right_left, Tape.move_right_n_head, Tape.mk'_nth_nat, Tape.write_move_right_n fun a : Γ' K Γ ↦ (a.1, update a.2 k none), addBottom_modifyNth fun a ↦ update a k none, addBottom_nth_snd, stk_nth_val _ (hL k), e, show (List.cons hd tl).reverse[tl.length]? = some hd by rw [List.reverse_cons, ← List.length_reverse, List.getElem?_concat_length], List.head?, List.tail]⟩ refine ListBlank.ext fun i ↦ ?_ rw [ListBlank.nth_map, ListBlank.nth_modifyNth, proj, PointedMap.mk_val] by_cases h' : k' = k · subst k' split_ifs with h <;> simp only [Function.update_self, ListBlank.nth_mk, List.tail] · rw [List.getI_eq_default] · rfl rw [h, List.length_reverse, List.length_map] rw [← proj_map_nth, hL, ListBlank.nth_mk, e, List.map, List.reverse_cons] rcases lt_or_gt_of_ne h with h | h · rw [List.getI_append] simpa only [List.length_map, List.length_reverse] using h · rw [gt_iff_lt] at h rw [List.getI_eq_default, List.getI_eq_default] <;> simp only [Nat.add_one_le_iff, h, List.length, le_of_lt, List.length_reverse, List.length_append, List.length_map] · split_ifs <;> rw [Function.update_of_ne h', ← proj_map_nth, hL] rw [Function.update_of_ne h'] end variable [DecidableEq K] variable (M : Λ → TM2.Stmt Γ Λ σ) /-- The TM2 emulator machine states written as a TM1 program. This handles the `go` and `ret` states, which shuttle to and from a stack top. -/ def tr : Λ' K Γ Λ σ → TM1.Stmt (Γ' K Γ) (Λ' K Γ Λ σ) σ | normal q => trNormal (M q) | go k s q => branch (fun a _ ↦ (a.2 k).isNone) (trStAct (goto fun _ _ ↦ ret q) s) (move Dir.right <| goto fun _ _ ↦ go k s q) | ret q => branch (fun a _ ↦ a.1) (trNormal q) (move Dir.left <| goto fun _ _ ↦ ret q) /-- The relation between TM2 configurations and TM1 configurations of the TM2 emulator. -/ inductive TrCfg : TM2.Cfg Γ Λ σ → TM1.Cfg (Γ' K Γ) (Λ' K Γ Λ σ) σ → Prop | mk {q : Option Λ} {v : σ} {S : ∀ k, List (Γ k)} (L : ListBlank (∀ k, Option (Γ k))) : (∀ k, L.map (proj k) = ListBlank.mk ((S k).map some).reverse) → TrCfg ⟨q, v, S⟩ ⟨q.map normal, v, Tape.mk' ∅ (addBottom L)⟩ theorem tr_respects_aux₁ {k} (o q v) {S : List (Γ k)} {L : ListBlank (∀ k, Option (Γ k))} (hL : L.map (proj k) = ListBlank.mk (S.map some).reverse) (n) (H : n ≤ S.length) : Reaches₀ (TM1.step (tr M)) ⟨some (go k o q), v, Tape.mk' ∅ (addBottom L)⟩ ⟨some (go k o q), v, (Tape.move Dir.right)^[n] (Tape.mk' ∅ (addBottom L))⟩ := by induction' n with n IH; · rfl apply (IH (le_of_lt H)).tail rw [iterate_succ_apply'] simp only [TM1.step, TM1.stepAux, tr, Tape.mk'_nth_nat, Tape.move_right_n_head, addBottom_nth_snd, Option.mem_def] rw [stk_nth_val _ hL, List.getElem?_eq_getElem] · rfl · rwa [List.length_reverse] theorem tr_respects_aux₃ {q v} {L : ListBlank (∀ k, Option (Γ k))} (n) : Reaches₀ (TM1.step (tr M)) ⟨some (ret q), v, (Tape.move Dir.right)^[n] (Tape.mk' ∅ (addBottom L))⟩ ⟨some (ret q), v, Tape.mk' ∅ (addBottom L)⟩ := by induction' n with n IH; · rfl refine Reaches₀.head ?_ IH simp only [Option.mem_def, TM1.step] rw [Option.some_inj, tr, TM1.stepAux, Tape.move_right_n_head, Tape.mk'_nth_nat, addBottom_nth_succ_fst, TM1.stepAux, iterate_succ', Function.comp_apply, Tape.move_right_left] rfl theorem tr_respects_aux {q v T k} {S : ∀ k, List (Γ k)} (hT : ∀ k, ListBlank.map (proj k) T = ListBlank.mk ((S k).map some).reverse) (o : StAct K Γ σ k) (IH : ∀ {v : σ} {S : ∀ k : K, List (Γ k)} {T : ListBlank (∀ k, Option (Γ k))}, (∀ k, ListBlank.map (proj k) T = ListBlank.mk ((S k).map some).reverse) → ∃ b, TrCfg (TM2.stepAux q v S) b ∧ Reaches (TM1.step (tr M)) (TM1.stepAux (trNormal q) v (Tape.mk' ∅ (addBottom T))) b) : ∃ b, TrCfg (TM2.stepAux (stRun o q) v S) b ∧ Reaches (TM1.step (tr M)) (TM1.stepAux (trNormal (stRun o q)) v (Tape.mk' ∅ (addBottom T))) b := by simp only [trNormal_run, step_run] have hgo := tr_respects_aux₁ M o q v (hT k) _ le_rfl obtain ⟨T', hT', hrun⟩ := tr_respects_aux₂ (Λ := Λ) hT o have := hgo.tail' rfl rw [tr, TM1.stepAux, Tape.move_right_n_head, Tape.mk'_nth_nat, addBottom_nth_snd, stk_nth_val _ (hT k), List.getElem?_eq_none (le_of_eq List.length_reverse), Option.isNone, cond, hrun, TM1.stepAux] at this obtain ⟨c, gc, rc⟩ := IH hT' refine ⟨c, gc, (this.to₀.trans (tr_respects_aux₃ M _) c (TransGen.head' rfl ?_)).to_reflTransGen⟩ rw [tr, TM1.stepAux, Tape.mk'_head, addBottom_head_fst] exact rc attribute [local simp] Respects TM2.step TM2.stepAux trNormal theorem tr_respects : Respects (TM2.step M) (TM1.step (tr M)) TrCfg := by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/12129): additional beta reduction needed intro c₁ c₂ h obtain @⟨- | l, v, S, L, hT⟩ := h; · constructor rsuffices ⟨b, c, r⟩ : ∃ b, _ ∧ Reaches (TM1.step (tr M)) _ _ · exact ⟨b, c, TransGen.head' rfl r⟩ simp only [tr] generalize M l = N induction N using stmtStRec generalizing v S L hT with | run k s q IH => exact tr_respects_aux M hT s @IH | load a _ IH => exact IH _ hT | branch p q₁ q₂ IH₁ IH₂ => unfold TM2.stepAux trNormal TM1.stepAux beta_reduce cases p v <;> [exact IH₂ _ hT; exact IH₁ _ hT] | goto => exact ⟨_, ⟨_, hT⟩, ReflTransGen.refl⟩ | halt => exact ⟨_, ⟨_, hT⟩, ReflTransGen.refl⟩ section variable [Inhabited Λ] [Inhabited σ] theorem trCfg_init (k) (L : List (Γ k)) : TrCfg (TM2.init k L) (TM1.init (trInit k L) : TM1.Cfg (Γ' K Γ) (Λ' K Γ Λ σ) σ) := by rw [(_ : TM1.init _ = _)] · refine ⟨ListBlank.mk (L.reverse.map fun a ↦ update default k (some a)), fun k' ↦ ?_⟩ refine ListBlank.ext fun i ↦ ?_ rw [ListBlank.map_mk, ListBlank.nth_mk, List.getI_eq_iget_getElem?, List.map_map] have : ((proj k').f ∘ fun a => update (β := fun k => Option (Γ k)) default k (some a)) = fun a => (proj k').f (update (β := fun k => Option (Γ k)) default k (some a)) := rfl rw [this, List.getElem?_map, proj, PointedMap.mk_val] simp only [] by_cases h : k' = k · subst k' simp only [Function.update_self] rw [ListBlank.nth_mk, List.getI_eq_iget_getElem?, ← List.map_reverse, List.getElem?_map] · simp only [Function.update_of_ne h] rw [ListBlank.nth_mk, List.getI_eq_iget_getElem?, List.map, List.reverse_nil] cases L.reverse[i]? <;> rfl · rw [trInit, TM1.init] congr <;> cases L.reverse <;> try rfl simp only [List.map_map, List.tail_cons, List.map] rfl theorem tr_eval_dom (k) (L : List (Γ k)) : (TM1.eval (tr M) (trInit k L)).Dom ↔ (TM2.eval M k L).Dom := Turing.tr_eval_dom (tr_respects M) (trCfg_init k L) theorem tr_eval (k) (L : List (Γ k)) {L₁ L₂} (H₁ : L₁ ∈ TM1.eval (tr M) (trInit k L)) (H₂ : L₂ ∈ TM2.eval M k L) : ∃ (S : ∀ k, List (Γ k)) (L' : ListBlank (∀ k, Option (Γ k))), addBottom L' = L₁ ∧ (∀ k, L'.map (proj k) = ListBlank.mk ((S k).map some).reverse) ∧ S k = L₂ := by obtain ⟨c₁, h₁, rfl⟩ := (Part.mem_map_iff _).1 H₁ obtain ⟨c₂, h₂, rfl⟩ := (Part.mem_map_iff _).1 H₂ obtain ⟨_, ⟨L', hT⟩, h₃⟩ := Turing.tr_eval (tr_respects M) (trCfg_init k L) h₂ cases Part.mem_unique h₁ h₃ exact ⟨_, L', by simp only [Tape.mk'_right₀], hT, rfl⟩ end section variable [Inhabited Λ] open scoped Classical in /-- The support of a set of TM2 states in the TM2 emulator. -/ noncomputable def trSupp (S : Finset Λ) : Finset (Λ' K Γ Λ σ) := S.biUnion fun l ↦ insert (normal l) (trStmts₁ (M l)) open scoped Classical in theorem tr_supports {S} (ss : TM2.Supports M S) : TM1.Supports (tr M) (trSupp M S) := ⟨Finset.mem_biUnion.2 ⟨_, ss.1, Finset.mem_insert.2 <| Or.inl rfl⟩, fun l' h ↦ by suffices ∀ (q) (_ : TM2.SupportsStmt S q) (_ : ∀ x ∈ trStmts₁ q, x ∈ trSupp M S), TM1.SupportsStmt (trSupp M S) (trNormal q) ∧ ∀ l' ∈ trStmts₁ q, TM1.SupportsStmt (trSupp M S) (tr M l') by rcases Finset.mem_biUnion.1 h with ⟨l, lS, h⟩ have := this _ (ss.2 l lS) fun x hx ↦ Finset.mem_biUnion.2 ⟨_, lS, Finset.mem_insert_of_mem hx⟩ rcases Finset.mem_insert.1 h with (rfl | h) <;> [exact this.1; exact this.2 _ h] clear h l' refine stmtStRec ?_ ?_ ?_ ?_ ?_ · intro _ s _ IH ss' sub -- stack op rw [TM2to1.supports_run] at ss' simp only [TM2to1.trStmts₁_run, Finset.mem_union, Finset.mem_insert, Finset.mem_singleton] at sub have hgo := sub _ (Or.inl <| Or.inl rfl) have hret := sub _ (Or.inl <| Or.inr rfl) obtain ⟨IH₁, IH₂⟩ := IH ss' fun x hx ↦ sub x <| Or.inr hx refine ⟨by simp only [trNormal_run, TM1.SupportsStmt]; intros; exact hgo, fun l h ↦ ?_⟩ rw [trStmts₁_run] at h simp only [TM2to1.trStmts₁_run, Finset.mem_union, Finset.mem_insert, Finset.mem_singleton] at h rcases h with (⟨rfl | rfl⟩ | h) · cases s · exact ⟨fun _ _ ↦ hret, fun _ _ ↦ hgo⟩ · exact ⟨fun _ _ ↦ hret, fun _ _ ↦ hgo⟩ · exact ⟨⟨fun _ _ ↦ hret, fun _ _ ↦ hret⟩, fun _ _ ↦ hgo⟩ · unfold TM1.SupportsStmt TM2to1.tr exact ⟨IH₁, fun _ _ ↦ hret⟩ · exact IH₂ _ h · intro _ _ IH ss' sub -- load unfold TM2to1.trStmts₁ at sub ⊢ exact IH ss' sub · intro _ _ _ IH₁ IH₂ ss' sub -- branch unfold TM2to1.trStmts₁ at sub obtain ⟨IH₁₁, IH₁₂⟩ := IH₁ ss'.1 fun x hx ↦ sub x <| Finset.mem_union_left _ hx obtain ⟨IH₂₁, IH₂₂⟩ := IH₂ ss'.2 fun x hx ↦ sub x <| Finset.mem_union_right _ hx refine ⟨⟨IH₁₁, IH₂₁⟩, fun l h ↦ ?_⟩ rw [trStmts₁] at h rcases Finset.mem_union.1 h with (h | h) <;> [exact IH₁₂ _ h; exact IH₂₂ _ h] · intro _ ss' _ -- goto simp only [trStmts₁, Finset.not_mem_empty]; refine ⟨?_, fun _ ↦ False.elim⟩ exact fun _ v ↦ Finset.mem_biUnion.2 ⟨_, ss' v, Finset.mem_insert_self _ _⟩ · intro _ _ -- halt simp only [trStmts₁, Finset.not_mem_empty] exact ⟨trivial, fun _ ↦ False.elim⟩⟩ end end TM2to1 end Turing
Mathlib/Computability/TuringMachine.lean
2,390
2,392
/- Copyright (c) 2022 Xavier Roblot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Xavier Roblot -/ import Mathlib.Algebra.Algebra.Hom.Rat import Mathlib.Analysis.Complex.Polynomial.Basic import Mathlib.NumberTheory.NumberField.Norm import Mathlib.RingTheory.RootsOfUnity.PrimitiveRoots import Mathlib.Topology.Instances.Complex /-! # Embeddings of number fields This file defines the embeddings of a number field into an algebraic closed field. ## Main Definitions and Results * `NumberField.Embeddings.range_eval_eq_rootSet_minpoly`: let `x ∈ K` with `K` number field and let `A` be an algebraic closed field of char. 0, then the images of `x` by the embeddings of `K` in `A` are exactly the roots in `A` of the minimal polynomial of `x` over `ℚ`. * `NumberField.Embeddings.pow_eq_one_of_norm_eq_one`: an algebraic integer whose conjugates are all of norm one is a root of unity. * `NumberField.InfinitePlace`: the type of infinite places of a number field `K`. * `NumberField.InfinitePlace.mk_eq_iff`: two complex embeddings define the same infinite place iff they are equal or complex conjugates. * `NumberField.InfinitePlace.prod_eq_abs_norm`: the infinite part of the product formula, that is for `x ∈ K`, we have `Π_w ‖x‖_w = |norm(x)|` where the product is over the infinite place `w` and `‖·‖_w` is the normalized absolute value for `w`. ## Tags number field, embeddings, places, infinite places -/ open scoped Finset namespace NumberField.Embeddings section Fintype open Module variable (K : Type*) [Field K] [NumberField K] variable (A : Type*) [Field A] [CharZero A] /-- There are finitely many embeddings of a number field. -/ noncomputable instance : Fintype (K →+* A) := Fintype.ofEquiv (K →ₐ[ℚ] A) RingHom.equivRatAlgHom.symm variable [IsAlgClosed A] /-- The number of embeddings of a number field is equal to its finrank. -/ theorem card : Fintype.card (K →+* A) = finrank ℚ K := by rw [Fintype.ofEquiv_card RingHom.equivRatAlgHom.symm, AlgHom.card] instance : Nonempty (K →+* A) := by rw [← Fintype.card_pos_iff, NumberField.Embeddings.card K A] exact Module.finrank_pos end Fintype section Roots open Set Polynomial variable (K A : Type*) [Field K] [NumberField K] [Field A] [Algebra ℚ A] [IsAlgClosed A] (x : K) /-- Let `A` be an algebraically closed field and let `x ∈ K`, with `K` a number field. The images of `x` by the embeddings of `K` in `A` are exactly the roots in `A` of the minimal polynomial of `x` over `ℚ`. -/ theorem range_eval_eq_rootSet_minpoly : (range fun φ : K →+* A => φ x) = (minpoly ℚ x).rootSet A := by convert (NumberField.isAlgebraic K).range_eval_eq_rootSet_minpoly A x using 1 ext a exact ⟨fun ⟨φ, hφ⟩ => ⟨φ.toRatAlgHom, hφ⟩, fun ⟨φ, hφ⟩ => ⟨φ.toRingHom, hφ⟩⟩ end Roots section Bounded open Module Polynomial Set variable {K : Type*} [Field K] [NumberField K] variable {A : Type*} [NormedField A] [IsAlgClosed A] [NormedAlgebra ℚ A] theorem coeff_bdd_of_norm_le {B : ℝ} {x : K} (h : ∀ φ : K →+* A, ‖φ x‖ ≤ B) (i : ℕ) : ‖(minpoly ℚ x).coeff i‖ ≤ max B 1 ^ finrank ℚ K * (finrank ℚ K).choose (finrank ℚ K / 2) := by have hx := Algebra.IsSeparable.isIntegral ℚ x rw [← norm_algebraMap' A, ← coeff_map (algebraMap ℚ A)] refine coeff_bdd_of_roots_le _ (minpoly.monic hx) (IsAlgClosed.splits_codomain _) (minpoly.natDegree_le x) (fun z hz => ?_) i classical rw [← Multiset.mem_toFinset] at hz obtain ⟨φ, rfl⟩ := (range_eval_eq_rootSet_minpoly K A x).symm.subset hz exact h φ variable (K A) /-- Let `B` be a real number. The set of algebraic integers in `K` whose conjugates are all smaller in norm than `B` is finite. -/ theorem finite_of_norm_le (B : ℝ) : {x : K | IsIntegral ℤ x ∧ ∀ φ : K →+* A, ‖φ x‖ ≤ B}.Finite := by classical let C := Nat.ceil (max B 1 ^ finrank ℚ K * (finrank ℚ K).choose (finrank ℚ K / 2)) have := bUnion_roots_finite (algebraMap ℤ K) (finrank ℚ K) (finite_Icc (-C : ℤ) C) refine this.subset fun x hx => ?_; simp_rw [mem_iUnion] have h_map_ℚ_minpoly := minpoly.isIntegrallyClosed_eq_field_fractions' ℚ hx.1 refine ⟨_, ⟨?_, fun i => ?_⟩, mem_rootSet.2 ⟨minpoly.ne_zero hx.1, minpoly.aeval ℤ x⟩⟩ · rw [← (minpoly.monic hx.1).natDegree_map (algebraMap ℤ ℚ), ← h_map_ℚ_minpoly] exact minpoly.natDegree_le x rw [mem_Icc, ← abs_le, ← @Int.cast_le ℝ] refine (Eq.trans_le ?_ <| coeff_bdd_of_norm_le hx.2 i).trans (Nat.le_ceil _) rw [h_map_ℚ_minpoly, coeff_map, eq_intCast, Int.norm_cast_rat, Int.norm_eq_abs, Int.cast_abs] /-- An algebraic integer whose conjugates are all of norm one is a root of unity. -/ theorem pow_eq_one_of_norm_eq_one {x : K} (hxi : IsIntegral ℤ x) (hx : ∀ φ : K →+* A, ‖φ x‖ = 1) : ∃ (n : ℕ) (_ : 0 < n), x ^ n = 1 := by obtain ⟨a, -, b, -, habne, h⟩ := @Set.Infinite.exists_ne_map_eq_of_mapsTo _ _ _ _ (x ^ · : ℕ → K) Set.infinite_univ (by exact fun a _ => ⟨hxi.pow a, fun φ => by simp [hx φ]⟩) (finite_of_norm_le K A (1 : ℝ)) wlog hlt : b < a · exact this K A hxi hx b a habne.symm h.symm (habne.lt_or_lt.resolve_right hlt) refine ⟨a - b, tsub_pos_of_lt hlt, ?_⟩ rw [← Nat.sub_add_cancel hlt.le, pow_add, mul_left_eq_self₀] at h refine h.resolve_right fun hp => ?_ specialize hx (IsAlgClosed.lift (R := ℚ)).toRingHom rw [pow_eq_zero hp, map_zero, norm_zero] at hx; norm_num at hx end Bounded end NumberField.Embeddings section Place variable {K : Type*} [Field K] {A : Type*} [NormedDivisionRing A] [Nontrivial A] (φ : K →+* A) /-- An embedding into a normed division ring defines a place of `K` -/ def NumberField.place : AbsoluteValue K ℝ := (IsAbsoluteValue.toAbsoluteValue (norm : A → ℝ)).comp φ.injective @[simp] theorem NumberField.place_apply (x : K) : (NumberField.place φ) x = norm (φ x) := rfl end Place namespace NumberField.ComplexEmbedding open Complex NumberField open scoped ComplexConjugate variable {K : Type*} [Field K] {k : Type*} [Field k] variable (K) in /-- A (random) lift of the complex embedding `φ : k →+* ℂ` to an extension `K` of `k`. -/ noncomputable def lift [Algebra k K] [Algebra.IsAlgebraic k K] (φ : k →+* ℂ) : K →+* ℂ := by letI := φ.toAlgebra exact (IsAlgClosed.lift (R := k)).toRingHom @[simp] theorem lift_comp_algebraMap [Algebra k K] [Algebra.IsAlgebraic k K] (φ : k →+* ℂ) : (lift K φ).comp (algebraMap k K) = φ := by unfold lift letI := φ.toAlgebra rw [AlgHom.toRingHom_eq_coe, AlgHom.comp_algebraMap_of_tower, RingHom.algebraMap_toAlgebra'] @[simp] theorem lift_algebraMap_apply [Algebra k K] [Algebra.IsAlgebraic k K] (φ : k →+* ℂ) (x : k) : lift K φ (algebraMap k K x) = φ x := RingHom.congr_fun (lift_comp_algebraMap φ) x /-- The conjugate of a complex embedding as a complex embedding. -/ abbrev conjugate (φ : K →+* ℂ) : K →+* ℂ := star φ @[simp] theorem conjugate_coe_eq (φ : K →+* ℂ) (x : K) : (conjugate φ) x = conj (φ x) := rfl theorem place_conjugate (φ : K →+* ℂ) : place (conjugate φ) = place φ := by ext; simp only [place_apply, norm_conj, conjugate_coe_eq] /-- An embedding into `ℂ` is real if it is fixed by complex conjugation. -/ abbrev IsReal (φ : K →+* ℂ) : Prop := IsSelfAdjoint φ theorem isReal_iff {φ : K →+* ℂ} : IsReal φ ↔ conjugate φ = φ := isSelfAdjoint_iff theorem isReal_conjugate_iff {φ : K →+* ℂ} : IsReal (conjugate φ) ↔ IsReal φ := IsSelfAdjoint.star_iff /-- A real embedding as a ring homomorphism from `K` to `ℝ` . -/ def IsReal.embedding {φ : K →+* ℂ} (hφ : IsReal φ) : K →+* ℝ where toFun x := (φ x).re map_one' := by simp only [map_one, one_re] map_mul' := by simp only [Complex.conj_eq_iff_im.mp (RingHom.congr_fun hφ _), map_mul, mul_re, mul_zero, tsub_zero, eq_self_iff_true, forall_const] map_zero' := by simp only [map_zero, zero_re] map_add' := by simp only [map_add, add_re, eq_self_iff_true, forall_const] @[simp] theorem IsReal.coe_embedding_apply {φ : K →+* ℂ} (hφ : IsReal φ) (x : K) : (hφ.embedding x : ℂ) = φ x := by apply Complex.ext · rfl · rw [ofReal_im, eq_comm, ← Complex.conj_eq_iff_im] exact RingHom.congr_fun hφ x lemma IsReal.comp (f : k →+* K) {φ : K →+* ℂ} (hφ : IsReal φ) : IsReal (φ.comp f) := by ext1 x; simpa using RingHom.congr_fun hφ (f x) lemma isReal_comp_iff {f : k ≃+* K} {φ : K →+* ℂ} : IsReal (φ.comp (f : k →+* K)) ↔ IsReal φ := ⟨fun H ↦ by convert H.comp f.symm.toRingHom; ext1; simp, IsReal.comp _⟩ lemma exists_comp_symm_eq_of_comp_eq [Algebra k K] [IsGalois k K] (φ ψ : K →+* ℂ) (h : φ.comp (algebraMap k K) = ψ.comp (algebraMap k K)) : ∃ σ : K ≃ₐ[k] K, φ.comp σ.symm = ψ := by letI := (φ.comp (algebraMap k K)).toAlgebra letI := φ.toAlgebra have : IsScalarTower k K ℂ := IsScalarTower.of_algebraMap_eq' rfl let ψ' : K →ₐ[k] ℂ := { ψ with commutes' := fun r ↦ (RingHom.congr_fun h r).symm } use (AlgHom.restrictNormal' ψ' K).symm ext1 x exact AlgHom.restrictNormal_commutes ψ' K x variable [Algebra k K] (φ : K →+* ℂ) (σ : K ≃ₐ[k] K) /-- `IsConj φ σ` states that `σ : K ≃ₐ[k] K` is the conjugation under the embedding `φ : K →+* ℂ`. -/ def IsConj : Prop := conjugate φ = φ.comp σ variable {φ σ} lemma IsConj.eq (h : IsConj φ σ) (x) : φ (σ x) = star (φ x) := RingHom.congr_fun h.symm x lemma IsConj.ext {σ₁ σ₂ : K ≃ₐ[k] K} (h₁ : IsConj φ σ₁) (h₂ : IsConj φ σ₂) : σ₁ = σ₂ := AlgEquiv.ext fun x ↦ φ.injective ((h₁.eq x).trans (h₂.eq x).symm) lemma IsConj.ext_iff {σ₁ σ₂ : K ≃ₐ[k] K} (h₁ : IsConj φ σ₁) : σ₁ = σ₂ ↔ IsConj φ σ₂ := ⟨fun e ↦ e ▸ h₁, h₁.ext⟩ lemma IsConj.isReal_comp (h : IsConj φ σ) : IsReal (φ.comp (algebraMap k K)) := by ext1 x simp only [conjugate_coe_eq, RingHom.coe_comp, Function.comp_apply, ← h.eq, starRingEnd_apply, AlgEquiv.commutes] lemma isConj_one_iff : IsConj φ (1 : K ≃ₐ[k] K) ↔ IsReal φ := Iff.rfl alias ⟨_, IsReal.isConjGal_one⟩ := ComplexEmbedding.isConj_one_iff lemma IsConj.symm (hσ : IsConj φ σ) : IsConj φ σ.symm := RingHom.ext fun x ↦ by simpa using congr_arg star (hσ.eq (σ.symm x)) lemma isConj_symm : IsConj φ σ.symm ↔ IsConj φ σ := ⟨IsConj.symm, IsConj.symm⟩ end NumberField.ComplexEmbedding section InfinitePlace open NumberField variable {k : Type*} [Field k] (K : Type*) [Field K] {F : Type*} [Field F] /-- An infinite place of a number field `K` is a place associated to a complex embedding. -/ def NumberField.InfinitePlace := { w : AbsoluteValue K ℝ // ∃ φ : K →+* ℂ, place φ = w } instance [NumberField K] : Nonempty (NumberField.InfinitePlace K) := Set.instNonemptyRange _ variable {K} /-- Return the infinite place defined by a complex embedding `φ`. -/ noncomputable def NumberField.InfinitePlace.mk (φ : K →+* ℂ) : NumberField.InfinitePlace K := ⟨place φ, ⟨φ, rfl⟩⟩ namespace NumberField.InfinitePlace open NumberField instance {K : Type*} [Field K] : FunLike (InfinitePlace K) K ℝ where coe w x := w.1 x coe_injective' _ _ h := Subtype.eq (AbsoluteValue.ext fun x => congr_fun h x) lemma coe_apply {K : Type*} [Field K] (v : InfinitePlace K) (x : K) : v x = v.1 x := rfl @[ext] lemma ext {K : Type*} [Field K] (v₁ v₂ : InfinitePlace K) (h : ∀ k, v₁ k = v₂ k) : v₁ = v₂ := Subtype.ext <| AbsoluteValue.ext h instance : MonoidWithZeroHomClass (InfinitePlace K) K ℝ where map_mul w _ _ := w.1.map_mul _ _ map_one w := w.1.map_one map_zero w := w.1.map_zero instance : NonnegHomClass (InfinitePlace K) K ℝ where apply_nonneg w _ := w.1.nonneg _ @[simp] theorem apply (φ : K →+* ℂ) (x : K) : (mk φ) x = ‖φ x‖ := rfl /-- For an infinite place `w`, return an embedding `φ` such that `w = infinite_place φ` . -/ noncomputable def embedding (w : InfinitePlace K) : K →+* ℂ := w.2.choose @[simp] theorem mk_embedding (w : InfinitePlace K) : mk (embedding w) = w := Subtype.ext w.2.choose_spec @[simp] theorem mk_conjugate_eq (φ : K →+* ℂ) : mk (ComplexEmbedding.conjugate φ) = mk φ := by refine DFunLike.ext _ _ (fun x => ?_) rw [apply, apply, ComplexEmbedding.conjugate_coe_eq, Complex.norm_conj] theorem norm_embedding_eq (w : InfinitePlace K) (x : K) : ‖(embedding w) x‖ = w x := by nth_rewrite 2 [← mk_embedding w] rfl theorem eq_iff_eq (x : K) (r : ℝ) : (∀ w : InfinitePlace K, w x = r) ↔ ∀ φ : K →+* ℂ, ‖φ x‖ = r := ⟨fun hw φ => hw (mk φ), by rintro hφ ⟨w, ⟨φ, rfl⟩⟩; exact hφ φ⟩ theorem le_iff_le (x : K) (r : ℝ) : (∀ w : InfinitePlace K, w x ≤ r) ↔ ∀ φ : K →+* ℂ, ‖φ x‖ ≤ r := ⟨fun hw φ => hw (mk φ), by rintro hφ ⟨w, ⟨φ, rfl⟩⟩; exact hφ φ⟩ theorem pos_iff {w : InfinitePlace K} {x : K} : 0 < w x ↔ x ≠ 0 := AbsoluteValue.pos_iff w.1 @[simp] theorem mk_eq_iff {φ ψ : K →+* ℂ} : mk φ = mk ψ ↔ φ = ψ ∨ ComplexEmbedding.conjugate φ = ψ := by constructor · -- We prove that the map ψ ∘ φ⁻¹ between φ(K) and ℂ is uniform continuous, thus it is either the -- inclusion or the complex conjugation using `Complex.uniformContinuous_ringHom_eq_id_or_conj` intro h₀ obtain ⟨j, hiφ⟩ := (φ.injective).hasLeftInverse let ι := RingEquiv.ofLeftInverse hiφ have hlip : LipschitzWith 1 (RingHom.comp ψ ι.symm.toRingHom) := by change LipschitzWith 1 (ψ ∘ ι.symm) apply LipschitzWith.of_dist_le_mul intro x y rw [NNReal.coe_one, one_mul, NormedField.dist_eq, Function.comp_apply, Function.comp_apply, ← map_sub, ← map_sub] apply le_of_eq suffices ‖φ (ι.symm (x - y))‖ = ‖ψ (ι.symm (x - y))‖ by rw [← this, ← RingEquiv.ofLeftInverse_apply hiφ _, RingEquiv.apply_symm_apply ι _] rfl exact congrFun (congrArg (↑) h₀) _ cases Complex.uniformContinuous_ringHom_eq_id_or_conj φ.fieldRange hlip.uniformContinuous with | inl h => left; ext1 x conv_rhs => rw [← hiφ x] exact (congrFun h (ι x)).symm | inr h => right; ext1 x conv_rhs => rw [← hiφ x] exact (congrFun h (ι x)).symm · rintro (⟨h⟩ | ⟨h⟩) · exact congr_arg mk h · rw [← mk_conjugate_eq] exact congr_arg mk h /-- An infinite place is real if it is defined by a real embedding. -/ def IsReal (w : InfinitePlace K) : Prop := ∃ φ : K →+* ℂ, ComplexEmbedding.IsReal φ ∧ mk φ = w /-- An infinite place is complex if it is defined by a complex (ie. not real) embedding. -/ def IsComplex (w : InfinitePlace K) : Prop := ∃ φ : K →+* ℂ, ¬ComplexEmbedding.IsReal φ ∧ mk φ = w theorem embedding_mk_eq (φ : K →+* ℂ) : embedding (mk φ) = φ ∨ embedding (mk φ) = ComplexEmbedding.conjugate φ := by rw [@eq_comm _ _ φ, @eq_comm _ _ (ComplexEmbedding.conjugate φ), ← mk_eq_iff, mk_embedding] @[simp] theorem embedding_mk_eq_of_isReal {φ : K →+* ℂ} (h : ComplexEmbedding.IsReal φ) : embedding (mk φ) = φ := by have := embedding_mk_eq φ rwa [ComplexEmbedding.isReal_iff.mp h, or_self] at this theorem isReal_iff {w : InfinitePlace K} : IsReal w ↔ ComplexEmbedding.IsReal (embedding w) := by refine ⟨?_, fun h => ⟨embedding w, h, mk_embedding w⟩⟩ rintro ⟨φ, ⟨hφ, rfl⟩⟩ rwa [embedding_mk_eq_of_isReal hφ] theorem isComplex_iff {w : InfinitePlace K} : IsComplex w ↔ ¬ComplexEmbedding.IsReal (embedding w) := by refine ⟨?_, fun h => ⟨embedding w, h, mk_embedding w⟩⟩ rintro ⟨φ, ⟨hφ, rfl⟩⟩ contrapose! hφ cases mk_eq_iff.mp (mk_embedding (mk φ)) with | inl h => rwa [h] at hφ | inr h => rwa [← ComplexEmbedding.isReal_conjugate_iff, h] at hφ @[simp] theorem conjugate_embedding_eq_of_isReal {w : InfinitePlace K} (h : IsReal w) : ComplexEmbedding.conjugate (embedding w) = embedding w := ComplexEmbedding.isReal_iff.mpr (isReal_iff.mp h) @[simp] theorem not_isReal_iff_isComplex {w : InfinitePlace K} : ¬IsReal w ↔ IsComplex w := by rw [isComplex_iff, isReal_iff] @[simp] theorem not_isComplex_iff_isReal {w : InfinitePlace K} : ¬IsComplex w ↔ IsReal w := by rw [isComplex_iff, isReal_iff, not_not] theorem isReal_or_isComplex (w : InfinitePlace K) : IsReal w ∨ IsComplex w := by rw [← not_isReal_iff_isComplex]; exact em _ theorem ne_of_isReal_isComplex {w w' : InfinitePlace K} (h : IsReal w) (h' : IsComplex w') : w ≠ w' := fun h_eq ↦ not_isReal_iff_isComplex.mpr h' (h_eq ▸ h) variable (K) in theorem disjoint_isReal_isComplex : Disjoint {(w : InfinitePlace K) | IsReal w} {(w : InfinitePlace K) | IsComplex w} := Set.disjoint_iff.2 <| fun _ hw ↦ not_isReal_iff_isComplex.2 hw.2 hw.1 /-- The real embedding associated to a real infinite place. -/ noncomputable def embedding_of_isReal {w : InfinitePlace K} (hw : IsReal w) : K →+* ℝ := ComplexEmbedding.IsReal.embedding (isReal_iff.mp hw) @[simp] theorem embedding_of_isReal_apply {w : InfinitePlace K} (hw : IsReal w) (x : K) : ((embedding_of_isReal hw) x : ℂ) = (embedding w) x := ComplexEmbedding.IsReal.coe_embedding_apply (isReal_iff.mp hw) x theorem norm_embedding_of_isReal {w : InfinitePlace K} (hw : IsReal w) (x : K) : ‖embedding_of_isReal hw x‖ = w x := by rw [← norm_embedding_eq, ← embedding_of_isReal_apply hw, Complex.norm_real] @[simp] theorem isReal_of_mk_isReal {φ : K →+* ℂ} (h : IsReal (mk φ)) : ComplexEmbedding.IsReal φ := by contrapose! h rw [not_isReal_iff_isComplex] exact ⟨φ, h, rfl⟩ lemma isReal_mk_iff {φ : K →+* ℂ} : IsReal (mk φ) ↔ ComplexEmbedding.IsReal φ := ⟨isReal_of_mk_isReal, fun H ↦ ⟨_, H, rfl⟩⟩ lemma isComplex_mk_iff {φ : K →+* ℂ} : IsComplex (mk φ) ↔ ¬ ComplexEmbedding.IsReal φ := not_isReal_iff_isComplex.symm.trans isReal_mk_iff.not @[simp] theorem not_isReal_of_mk_isComplex {φ : K →+* ℂ} (h : IsComplex (mk φ)) : ¬ ComplexEmbedding.IsReal φ := by rwa [← isComplex_mk_iff] open scoped Classical in /-- The multiplicity of an infinite place, that is the number of distinct complex embeddings that define it, see `card_filter_mk_eq`. -/ noncomputable def mult (w : InfinitePlace K) : ℕ := if (IsReal w) then 1 else 2 @[simp] theorem mult_isReal (w : {w : InfinitePlace K // IsReal w}) : mult w.1 = 1 := by rw [mult, if_pos w.prop] @[simp] theorem mult_isComplex (w : {w : InfinitePlace K // IsComplex w}) : mult w.1 = 2 := by rw [mult, if_neg (not_isReal_iff_isComplex.mpr w.prop)] theorem mult_pos {w : InfinitePlace K} : 0 < mult w := by rw [mult] split_ifs <;> norm_num @[simp] theorem mult_ne_zero {w : InfinitePlace K} : mult w ≠ 0 := ne_of_gt mult_pos theorem mult_coe_ne_zero {w : InfinitePlace K} : (mult w : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr mult_ne_zero theorem one_le_mult {w : InfinitePlace K} : (1 : ℝ) ≤ mult w := by rw [← Nat.cast_one, Nat.cast_le] exact mult_pos open scoped Classical in theorem card_filter_mk_eq [NumberField K] (w : InfinitePlace K) : #{φ | mk φ = w} = mult w := by conv_lhs => congr; congr; ext rw [← mk_embedding w, mk_eq_iff, ComplexEmbedding.conjugate, star_involutive.eq_iff] simp_rw [Finset.filter_or, Finset.filter_eq' _ (embedding w), Finset.filter_eq' _ (ComplexEmbedding.conjugate (embedding w)), Finset.mem_univ, ite_true, mult] split_ifs with hw · rw [ComplexEmbedding.isReal_iff.mp (isReal_iff.mp hw), Finset.union_idempotent, Finset.card_singleton] · refine Finset.card_pair ?_ rwa [Ne, eq_comm, ← ComplexEmbedding.isReal_iff, ← isReal_iff] open scoped Classical in noncomputable instance NumberField.InfinitePlace.fintype [NumberField K] : Fintype (InfinitePlace K) := Set.fintypeRange _ open scoped Classical in @[to_additive] theorem prod_eq_prod_mul_prod {α : Type*} [CommMonoid α] [NumberField K] (f : InfinitePlace K → α) : ∏ w, f w = (∏ w : {w // IsReal w}, f w.1) * (∏ w : {w // IsComplex w}, f w.1) := by rw [← Equiv.prod_comp (Equiv.subtypeEquivRight (fun _ ↦ not_isReal_iff_isComplex))] simp [Fintype.prod_subtype_mul_prod_subtype] theorem sum_mult_eq [NumberField K] : ∑ w : InfinitePlace K, mult w = Module.finrank ℚ K := by classical rw [← Embeddings.card K ℂ, Fintype.card, Finset.card_eq_sum_ones, ← Finset.univ.sum_fiberwise (fun φ => InfinitePlace.mk φ)] exact Finset.sum_congr rfl (fun _ _ => by rw [Finset.sum_const, smul_eq_mul, mul_one, card_filter_mk_eq]) /-- The map from real embeddings to real infinite places as an equiv -/ noncomputable def mkReal : { φ : K →+* ℂ // ComplexEmbedding.IsReal φ } ≃ { w : InfinitePlace K // IsReal w } := by refine (Equiv.ofBijective (fun φ => ⟨mk φ, ?_⟩) ⟨fun φ ψ h => ?_, fun w => ?_⟩) · exact ⟨φ, φ.prop, rfl⟩ · rwa [Subtype.mk.injEq, mk_eq_iff, ComplexEmbedding.isReal_iff.mp φ.prop, or_self, ← Subtype.ext_iff] at h · exact ⟨⟨embedding w, isReal_iff.mp w.prop⟩, by simp⟩ /-- The map from nonreal embeddings to complex infinite places -/ noncomputable def mkComplex : { φ : K →+* ℂ // ¬ComplexEmbedding.IsReal φ } → { w : InfinitePlace K // IsComplex w } := Subtype.map mk fun φ hφ => ⟨φ, hφ, rfl⟩ @[simp] theorem mkReal_coe (φ : { φ : K →+* ℂ // ComplexEmbedding.IsReal φ }) : (mkReal φ : InfinitePlace K) = mk (φ : K →+* ℂ) := rfl @[simp] theorem mkComplex_coe (φ : { φ : K →+* ℂ // ¬ComplexEmbedding.IsReal φ }) : (mkComplex φ : InfinitePlace K) = mk (φ : K →+* ℂ) := rfl section NumberField variable [NumberField K] /-- The infinite part of the product formula : for `x ∈ K`, we have `Π_w ‖x‖_w = |norm(x)|` where `‖·‖_w` is the normalized absolute value for `w`. -/ theorem prod_eq_abs_norm (x : K) : ∏ w : InfinitePlace K, w x ^ mult w = abs (Algebra.norm ℚ x) := by classical convert (congr_arg (‖·‖) (@Algebra.norm_eq_prod_embeddings ℚ _ _ _ _ ℂ _ _ _ _ _ x)).symm · rw [norm_prod, ← Fintype.prod_equiv RingHom.equivRatAlgHom (fun f => ‖f x‖) (fun φ => ‖φ x‖) fun _ => by simp [RingHom.equivRatAlgHom_apply]] rw [← Finset.prod_fiberwise Finset.univ mk (fun φ => ‖φ x‖)] have (w : InfinitePlace K) (φ) (hφ : φ ∈ ({φ | mk φ = w} : Finset _)) : ‖φ x‖ = w x := by rw [← (Finset.mem_filter.mp hφ).2, apply] simp_rw [Finset.prod_congr rfl (this _), Finset.prod_const, card_filter_mk_eq] · rw [eq_ratCast, Rat.cast_abs, ← Real.norm_eq_abs, ← Complex.norm_real, Complex.ofReal_ratCast] theorem one_le_of_lt_one {w : InfinitePlace K} {a : (𝓞 K)} (ha : a ≠ 0) (h : ∀ ⦃z⦄, z ≠ w → z a < 1) : 1 ≤ w a := by suffices (1 : ℝ) ≤ |Algebra.norm ℚ (a : K)| by contrapose! this rw [← InfinitePlace.prod_eq_abs_norm, ← Finset.prod_const_one] refine Finset.prod_lt_prod_of_nonempty (fun _ _ ↦ ?_) (fun z _ ↦ ?_) Finset.univ_nonempty · exact pow_pos (pos_iff.mpr ((Subalgebra.coe_eq_zero _).not.mpr ha)) _ · refine pow_lt_one₀ (apply_nonneg _ _) ?_ (by rw [mult]; split_ifs <;> norm_num) by_cases hz : z = w · rwa [hz] · exact h hz rw [← Algebra.coe_norm_int, ← Int.cast_one, ← Int.cast_abs, Rat.cast_intCast, Int.cast_le] exact Int.one_le_abs (Algebra.norm_ne_zero_iff.mpr ha) open scoped IntermediateField in theorem _root_.NumberField.is_primitive_element_of_infinitePlace_lt {x : 𝓞 K} {w : InfinitePlace K} (h₁ : x ≠ 0) (h₂ : ∀ ⦃w'⦄, w' ≠ w → w' x < 1) (h₃ : IsReal w ∨ |(w.embedding x).re| < 1) : ℚ⟮(x : K)⟯ = ⊤ := by rw [Field.primitive_element_iff_algHom_eq_of_eval ℚ ℂ ?_ _ w.embedding.toRatAlgHom] · intro ψ hψ have h : 1 ≤ w x := one_le_of_lt_one h₁ h₂ have main : w = InfinitePlace.mk ψ.toRingHom := by simp at hψ rw [← norm_embedding_eq, hψ] at h contrapose! h exact h₂ h.symm rw [(mk_embedding w).symm, mk_eq_iff] at main cases h₃ with | inl hw => rw [conjugate_embedding_eq_of_isReal hw, or_self] at main exact congr_arg RingHom.toRatAlgHom main | inr hw => refine congr_arg RingHom.toRatAlgHom (main.resolve_right fun h' ↦ hw.not_le ?_) have : (embedding w x).im = 0 := by rw [← Complex.conj_eq_iff_im] have := RingHom.congr_fun h' x simp at this rw [this] exact hψ.symm rwa [← norm_embedding_eq, ← Complex.re_add_im (embedding w x), this, Complex.ofReal_zero, zero_mul, add_zero, Complex.norm_real] at h · exact fun x ↦ IsAlgClosed.splits_codomain (minpoly ℚ x) theorem _root_.NumberField.adjoin_eq_top_of_infinitePlace_lt {x : 𝓞 K} {w : InfinitePlace K} (h₁ : x ≠ 0) (h₂ : ∀ ⦃w'⦄, w' ≠ w → w' x < 1) (h₃ : IsReal w ∨ |(w.embedding x).re| < 1) : Algebra.adjoin ℚ {(x : K)} = ⊤ := by rw [← IntermediateField.adjoin_simple_toSubalgebra_of_integral (IsIntegral.of_finite ℚ _)] exact congr_arg IntermediateField.toSubalgebra <| NumberField.is_primitive_element_of_infinitePlace_lt h₁ h₂ h₃ end NumberField open Fintype Module variable (K)
section NumberField variable [NumberField K] open scoped Classical in /-- The number of infinite real places of the number field `K`. -/ noncomputable abbrev nrRealPlaces := card { w : InfinitePlace K // IsReal w } @[deprecated (since := "2024-10-24")] alias NrRealPlaces := nrRealPlaces open scoped Classical in /-- The number of infinite complex places of the number field `K`. -/ noncomputable abbrev nrComplexPlaces := card { w : InfinitePlace K // IsComplex w } @[deprecated (since := "2024-10-24")] alias NrComplexPlaces := nrComplexPlaces
Mathlib/NumberTheory/NumberField/Embeddings.lean
604
619
/- 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 simp [Iic] @[simp] theorem Ioc_inter_Iic (a b c : α) : Ioc a b ∩ Iic c = Ioc a (b ⊓ c) := by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, inter_assoc, Iic_inter_Iic] end Inf section Sup variable [SemilatticeSup α] @[simp] theorem Ici_inter_Ici {a b : α} : Ici a ∩ Ici b = Ici (a ⊔ b) := by ext x simp [Ici] @[simp] theorem Ico_inter_Ici (a b c : α) : Ico a b ∩ Ici c = Ico (a ⊔ c) b := by rw [← Ici_inter_Iio, ← Ici_inter_Iio, ← Ici_inter_Ici, inter_right_comm] end Sup section Both variable [Lattice α] {a b c a₁ a₂ b₁ b₂ : α} theorem Icc_inter_Icc : Icc a₁ b₁ ∩ Icc a₂ b₂ = Icc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iic.symm, Ici_inter_Ici.symm, Iic_inter_Iic.symm]; ac_rfl end Both end Lattice /-! ### Closed intervals in `α × β` -/ section Prod variable {β : Type*} [Preorder α] [Preorder β] @[simp] theorem Iic_prod_Iic (a : α) (b : β) : Iic a ×ˢ Iic b = Iic (a, b) := rfl @[simp] theorem Ici_prod_Ici (a : α) (b : β) : Ici a ×ˢ Ici b = Ici (a, b) := rfl theorem Ici_prod_eq (a : α × β) : Ici a = Ici a.1 ×ˢ Ici a.2 := rfl theorem Iic_prod_eq (a : α × β) : Iic a = Iic a.1 ×ˢ Iic a.2 := rfl @[simp] theorem Icc_prod_Icc (a₁ a₂ : α) (b₁ b₂ : β) : Icc a₁ a₂ ×ˢ Icc b₁ b₂ = Icc (a₁, b₁) (a₂, b₂) := by ext ⟨x, y⟩ simp [and_assoc, and_comm, and_left_comm] theorem Icc_prod_eq (a b : α × β) : Icc a b = Icc a.1 b.1 ×ˢ Icc a.2 b.2 := by simp end Prod end Set /-! ### Lemmas about intervals in dense orders -/ section Dense variable (α) [Preorder α] [DenselyOrdered α] {x y : α} instance : NoMinOrder (Set.Ioo x y) := ⟨fun ⟨a, ha₁, ha₂⟩ => by rcases exists_between ha₁ with ⟨b, hb₁, hb₂⟩ exact ⟨⟨b, hb₁, hb₂.trans ha₂⟩, hb₂⟩⟩ instance : NoMinOrder (Set.Ioc x y) := ⟨fun ⟨a, ha₁, ha₂⟩ => by rcases exists_between ha₁ with ⟨b, hb₁, hb₂⟩ exact ⟨⟨b, hb₁, hb₂.le.trans ha₂⟩, hb₂⟩⟩ instance : NoMinOrder (Set.Ioi x) := ⟨fun ⟨a, ha⟩ => by rcases exists_between ha with ⟨b, hb₁, hb₂⟩ exact ⟨⟨b, hb₁⟩, hb₂⟩⟩ instance : NoMaxOrder (Set.Ioo x y) := ⟨fun ⟨a, ha₁, ha₂⟩ => by rcases exists_between ha₂ with ⟨b, hb₁, hb₂⟩ exact ⟨⟨b, ha₁.trans hb₁, hb₂⟩, hb₁⟩⟩ instance : NoMaxOrder (Set.Ico x y) := ⟨fun ⟨a, ha₁, ha₂⟩ => by rcases exists_between ha₂ with ⟨b, hb₁, hb₂⟩ exact ⟨⟨b, ha₁.trans hb₁.le, hb₂⟩, hb₁⟩⟩ instance : NoMaxOrder (Set.Iio x) := ⟨fun ⟨a, ha⟩ => by rcases exists_between ha with ⟨b, hb₁, hb₂⟩ exact ⟨⟨b, hb₂⟩, hb₁⟩⟩ end Dense /-! ### Intervals in `Prop` -/ namespace Set @[simp] lemma Iic_False : Iic False = {False} := by aesop @[simp] lemma Iic_True : Iic True = univ := by aesop @[simp] lemma Ici_False : Ici False = univ := by aesop @[simp] lemma Ici_True : Ici True = {True} := by aesop lemma Iio_False : Iio False = ∅ := by aesop @[simp] lemma Iio_True : Iio True = {False} := by aesop (add simp [Ioi, lt_iff_le_not_le]) @[simp] lemma Ioi_False : Ioi False = {True} := by aesop (add simp [Ioi, lt_iff_le_not_le]) lemma Ioi_True : Ioi True = ∅ := by aesop end Set
Mathlib/Order/Interval/Set/Basic.lean
1,872
1,873
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudryashov -/ import Mathlib.Analysis.Convex.Combination import Mathlib.Analysis.Convex.Function import Mathlib.Tactic.FieldSimp /-! # Jensen's inequality and maximum principle for convex functions In this file, we prove the finite Jensen inequality and the finite maximum principle for convex functions. The integral versions are to be found in `Analysis.Convex.Integral`. ## Main declarations Jensen's inequalities: * `ConvexOn.map_centerMass_le`, `ConvexOn.map_sum_le`: Convex Jensen's inequality. The image of a convex combination of points under a convex function is less than the convex combination of the images. * `ConcaveOn.le_map_centerMass`, `ConcaveOn.le_map_sum`: Concave Jensen's inequality. * `StrictConvexOn.map_sum_lt`: Convex strict Jensen inequality. * `StrictConcaveOn.lt_map_sum`: Concave strict Jensen inequality. As corollaries, we get: * `StrictConvexOn.map_sum_eq_iff`: Equality case of the convex Jensen inequality. * `StrictConcaveOn.map_sum_eq_iff`: Equality case of the concave Jensen inequality. * `ConvexOn.exists_ge_of_mem_convexHull`: Maximum principle for convex functions. * `ConcaveOn.exists_le_of_mem_convexHull`: Minimum principle for concave functions. -/ open Finset LinearMap Set Convex Pointwise variable {𝕜 E F β ι : Type*} /-! ### Jensen's inequality -/ section Jensen variable [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] [AddCommGroup E] [AddCommGroup β] [PartialOrder β] [IsOrderedAddMonoid β] [Module 𝕜 E] [Module 𝕜 β] [OrderedSMul 𝕜 β] {s : Set E} {f : E → β} {t : Finset ι} {w : ι → 𝕜} {p : ι → E} {v : 𝕜} {q : E} /-- Convex **Jensen's inequality**, `Finset.centerMass` version. -/ theorem ConvexOn.map_centerMass_le (hf : ConvexOn 𝕜 s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : 0 < ∑ i ∈ t, w i) (hmem : ∀ i ∈ t, p i ∈ s) : f (t.centerMass w p) ≤ t.centerMass w (f ∘ p) := by have hmem' : ∀ i ∈ t, (p i, (f ∘ p) i) ∈ { p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2 } := fun i hi => ⟨hmem i hi, le_rfl⟩ convert (hf.convex_epigraph.centerMass_mem h₀ h₁ hmem').2 <;> simp only [centerMass, Function.comp, Prod.smul_fst, Prod.fst_sum, Prod.smul_snd, Prod.snd_sum] /-- Concave **Jensen's inequality**, `Finset.centerMass` version. -/ theorem ConcaveOn.le_map_centerMass (hf : ConcaveOn 𝕜 s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : 0 < ∑ i ∈ t, w i) (hmem : ∀ i ∈ t, p i ∈ s) : t.centerMass w (f ∘ p) ≤ f (t.centerMass w p) := ConvexOn.map_centerMass_le (β := βᵒᵈ) hf h₀ h₁ hmem /-- Convex **Jensen's inequality**, `Finset.sum` version. -/ theorem ConvexOn.map_sum_le (hf : ConvexOn 𝕜 s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i ∈ t, w i = 1) (hmem : ∀ i ∈ t, p i ∈ s) : f (∑ i ∈ t, w i • p i) ≤ ∑ i ∈ t, w i • f (p i) := by simpa only [centerMass, h₁, inv_one, one_smul] using hf.map_centerMass_le h₀ (h₁.symm ▸ zero_lt_one) hmem /-- Concave **Jensen's inequality**, `Finset.sum` version. -/
theorem ConcaveOn.le_map_sum (hf : ConcaveOn 𝕜 s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i ∈ t, w i = 1) (hmem : ∀ i ∈ t, p i ∈ s) : (∑ i ∈ t, w i • f (p i)) ≤ f (∑ i ∈ t, w i • p i) := ConvexOn.map_sum_le (β := βᵒᵈ) hf h₀ h₁ hmem
Mathlib/Analysis/Convex/Jensen.lean
69
72
/- Copyright (c) 2018 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov, Winston Yin -/ import Mathlib.Algebra.Group.End import Mathlib.Topology.EMetricSpace.Diam /-! # Lipschitz continuous functions A map `f : α → β` between two (extended) metric spaces is called *Lipschitz continuous* with constant `K ≥ 0` if for all `x, y` we have `edist (f x) (f y) ≤ K * edist x y`. For a metric space, the latter inequality is equivalent to `dist (f x) (f y) ≤ K * dist x y`. There is also a version asserting this inequality only for `x` and `y` in some set `s`. Finally, `f : α → β` is called *locally Lipschitz continuous* if each `x : α` has a neighbourhood on which `f` is Lipschitz continuous (with some constant). In this file we provide various ways to prove that various combinations of Lipschitz continuous functions are Lipschitz continuous. We also prove that Lipschitz continuous functions are uniformly continuous, and that locally Lipschitz functions are continuous. ## Main definitions and lemmas * `LipschitzWith K f`: states that `f` is Lipschitz with constant `K : ℝ≥0` * `LipschitzOnWith K f s`: states that `f` is Lipschitz with constant `K : ℝ≥0` on a set `s` * `LipschitzWith.uniformContinuous`: a Lipschitz function is uniformly continuous * `LipschitzOnWith.uniformContinuousOn`: a function which is Lipschitz on a set `s` is uniformly continuous on `s`. * `LocallyLipschitz f`: states that `f` is locally Lipschitz * `LocallyLipschitzOn f s`: states that `f` is locally Lipschitz on `s`. * `LocallyLipschitz.continuous`: a locally Lipschitz function is continuous. ## Implementation notes The parameter `K` has type `ℝ≥0`. This way we avoid conjunction in the definition and have coercions both to `ℝ` and `ℝ≥0∞`. Constructors whose names end with `'` take `K : ℝ` as an argument, and return `LipschitzWith (Real.toNNReal K) f`. -/ universe u v w x open Filter Function Set Topology NNReal ENNReal Bornology variable {α : Type u} {β : Type v} {γ : Type w} {ι : Type x} section PseudoEMetricSpace variable [PseudoEMetricSpace α] [PseudoEMetricSpace β] {K : ℝ≥0} {s t : Set α} {f : α → β} /-- A function `f` is **Lipschitz continuous** with constant `K ≥ 0` if for all `x, y` we have `dist (f x) (f y) ≤ K * dist x y`. -/ def LipschitzWith (K : ℝ≥0) (f : α → β) := ∀ x y, edist (f x) (f y) ≤ K * edist x y /-- A function `f` is **Lipschitz continuous** with constant `K ≥ 0` **on `s`** if for all `x, y` in `s` we have `dist (f x) (f y) ≤ K * dist x y`. -/ def LipschitzOnWith (K : ℝ≥0) (f : α → β) (s : Set α) := ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → edist (f x) (f y) ≤ K * edist x y /-- `f : α → β` is called **locally Lipschitz continuous** iff every point `x` has a neighbourhood on which `f` is Lipschitz. -/ def LocallyLipschitz (f : α → β) : Prop := ∀ x, ∃ K, ∃ t ∈ 𝓝 x, LipschitzOnWith K f t /-- `f : α → β` is called **locally Lipschitz continuous** on `s` iff every point `x` of `s` has a neighbourhood within `s` on which `f` is Lipschitz. -/ def LocallyLipschitzOn (s : Set α) (f : α → β) : Prop := ∀ ⦃x⦄, x ∈ s → ∃ K, ∃ t ∈ 𝓝[s] x, LipschitzOnWith K f t /-- Every function is Lipschitz on the empty set (with any Lipschitz constant). -/ @[simp] theorem lipschitzOnWith_empty (K : ℝ≥0) (f : α → β) : LipschitzOnWith K f ∅ := fun _ => False.elim @[simp] lemma locallyLipschitzOn_empty (f : α → β) : LocallyLipschitzOn ∅ f := fun _ ↦ False.elim /-- Being Lipschitz on a set is monotone w.r.t. that set. -/ theorem LipschitzOnWith.mono (hf : LipschitzOnWith K f t) (h : s ⊆ t) : LipschitzOnWith K f s := fun _x x_in _y y_in => hf (h x_in) (h y_in) lemma LocallyLipschitzOn.mono (hf : LocallyLipschitzOn t f) (h : s ⊆ t) : LocallyLipschitzOn s f := fun x hx ↦ by obtain ⟨K, u, hu, hfu⟩ := hf (h hx); exact ⟨K, u, nhdsWithin_mono _ h hu, hfu⟩ /-- `f` is Lipschitz iff it is Lipschitz on the entire space. -/ @[simp] lemma lipschitzOnWith_univ : LipschitzOnWith K f univ ↔ LipschitzWith K f := by simp [LipschitzOnWith, LipschitzWith]
@[simp] lemma locallyLipschitzOn_univ : LocallyLipschitzOn univ f ↔ LocallyLipschitz f := by simp [LocallyLipschitzOn, LocallyLipschitz]
Mathlib/Topology/EMetricSpace/Lipschitz.lean
86
88