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) 2023 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import Mathlib.CategoryTheory.Sites.Sheaf /-! # Coverages A coverage `K` on a category `C` is a set of presieves associated to every object `X : C`, called "covering presieves". This collection must satisfy a certain "pullback compatibility" condition, saying that whenever `S` is a covering presieve on `X` and `f : Y ⟶ X` is a morphism, then there exists some covering sieve `T` on `Y` such that `T` factors through `S` along `f`. The main difference between a coverage and a Grothendieck pretopology is that we *do not* require `C` to have pullbacks. This is useful, for example, when we want to consider the Grothendieck topology on the category of extremally disconnected sets in the context of condensed mathematics. A more concrete example: If `ℬ` is a basis for a topology on a type `X` (in the sense of `TopologicalSpace.IsTopologicalBasis`) then it naturally induces a coverage on `Opens X` whose associated Grothendieck topology is the one induced by the topology on `X` generated by `ℬ`. (Project: Formalize this!) ## Main Definitions and Results: All definitions are in the `CategoryTheory` namespace. - `Coverage C`: The type of coverages on `C`. - `Coverage.ofGrothendieck C`: A function which associates a coverage to any Grothendieck topology. - `Coverage.toGrothendieck C`: A function which associates a Grothendieck topology to any coverage. - `Coverage.gi`: The two functions above form a Galois insertion. - `Presieve.isSheaf_coverage`: Given `K : Coverage C` with associated Grothendieck topology `J`, a `Type*`-valued presheaf on `C` is a sheaf for `K` if and only if it is a sheaf for `J`. # References We don't follow any particular reference, but the arguments can probably be distilled from the following sources: - [Elephant]: *Sketches of an Elephant*, P. T. Johnstone: C2.1. - [nLab, *Coverage*](https://ncatlab.org/nlab/show/coverage) -/ namespace CategoryTheory variable {C D : Type _} [Category C] [Category D] open Limits namespace Presieve /-- Given a morphism `f : Y ⟶ X`, a presieve `S` on `Y` and presieve `T` on `X`, we say that *`S` factors through `T` along `f`*, written `S.FactorsThruAlong T f`, provided that for any morphism `g : Z ⟶ Y` in `S`, there exists some morphism `e : W ⟶ X` in `T` and some morphism `i : Z ⟶ W` such that the obvious square commutes: `i ≫ e = g ≫ f`. This is used in the definition of a coverage. -/ def FactorsThruAlong {X Y : C} (S : Presieve Y) (T : Presieve X) (f : Y ⟶ X) : Prop := ∀ ⦃Z : C⦄ ⦃g : Z ⟶ Y⦄, S g → ∃ (W : C) (i : Z ⟶ W) (e : W ⟶ X), T e ∧ i ≫ e = g ≫ f /-- Given `S T : Presieve X`, we say that `S` factors through `T` if any morphism in `S` factors through some morphism in `T`. The lemma `Presieve.isSheafFor_of_factorsThru` gives a *sufficient* condition for a presheaf to be a sheaf for a presieve `T`, in terms of `S.FactorsThru T`, provided that the presheaf is a sheaf for `S`. -/ def FactorsThru {X : C} (S T : Presieve X) : Prop := ∀ ⦃Z : C⦄ ⦃g : Z ⟶ X⦄, S g → ∃ (W : C) (i : Z ⟶ W) (e : W ⟶ X), T e ∧ i ≫ e = g @[simp] lemma factorsThruAlong_id {X : C} (S T : Presieve X) : S.FactorsThruAlong T (𝟙 X) ↔ S.FactorsThru T := by simp [FactorsThruAlong, FactorsThru] lemma factorsThru_of_le {X : C} (S T : Presieve X) (h : S ≤ T) : S.FactorsThru T := fun Y g hg => ⟨Y, 𝟙 _, g, h _ hg, by simp⟩ lemma le_of_factorsThru_sieve {X : C} (S : Presieve X) (T : Sieve X) (h : S.FactorsThru T) : S ≤ T := by rintro Y f hf obtain ⟨W, i, e, h1, rfl⟩ := h hf exact T.downward_closed h1 _ lemma factorsThru_top {X : C} (S : Presieve X) : S.FactorsThru ⊤ := factorsThru_of_le _ _ le_top lemma isSheafFor_of_factorsThru {X : C} {S T : Presieve X} (P : Cᵒᵖ ⥤ Type*) (H : S.FactorsThru T) (hS : S.IsSheafFor P) (h : ∀ ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, T f → ∃ (R : Presieve Y), R.IsSeparatedFor P ∧ R.FactorsThruAlong S f) : T.IsSheafFor P := by simp only [← Presieve.isSeparatedFor_and_exists_isAmalgamation_iff_isSheafFor] at * choose W i e h1 h2 using H refine ⟨?_, fun x hx => ?_⟩ · intro x y₁ y₂ h₁ h₂ refine hS.1.ext (fun Y g hg => ?_) simp only [← h2 hg, op_comp, P.map_comp, types_comp_apply, h₁ _ (h1 _ ), h₂ _ (h1 _)] let y : S.FamilyOfElements P := fun Y g hg => P.map (i _).op (x (e hg) (h1 _)) have hy : y.Compatible := by intro Y₁ Y₂ Z g₁ g₂ f₁ f₂ h₁ h₂ h rw [← types_comp_apply (P.map (i h₁).op) (P.map g₁.op), ← types_comp_apply (P.map (i h₂).op) (P.map g₂.op), ← P.map_comp, ← op_comp, ← P.map_comp, ← op_comp] apply hx simp only [h2, h, Category.assoc] let ⟨_, h2'⟩ := hS obtain ⟨z, hz⟩ := h2' y hy refine ⟨z, fun Y g hg => ?_⟩ obtain ⟨R, hR1, hR2⟩ := h hg choose WW ii ee hh1 hh2 using hR2 refine hR1.ext (fun Q t ht => ?_) rw [← types_comp_apply (P.map g.op) (P.map t.op), ← P.map_comp, ← op_comp, ← hh2 ht, op_comp, P.map_comp, types_comp_apply, hz _ (hh1 _), ← types_comp_apply _ (P.map (ii ht).op), ← P.map_comp, ← op_comp] apply hx simp only [Category.assoc, h2, hh2] end Presieve variable (C) in /-- The type `Coverage C` of coverages on `C`. A coverage is a collection of *covering* presieves on every object `X : C`, which satisfies a *pullback compatibility* condition. Explicitly, this condition says that whenever `S` is a covering presieve for `X` and `f : Y ⟶ X` is a morphism, then there exists some covering presieve `T` for `Y` such that `T` factors through `S` along `f`. -/ @[ext] structure Coverage where /-- The collection of covering presieves for an object `X`. -/ covering : ∀ (X : C), Set (Presieve X) /-- Given any covering sieve `S` on `X` and a morphism `f : Y ⟶ X`, there exists some covering sieve `T` on `Y` such that `T` factors through `S` along `f`. -/ pullback : ∀ ⦃X Y : C⦄ (f : Y ⟶ X) (S : Presieve X) (_ : S ∈ covering X), ∃ (T : Presieve Y), T ∈ covering Y ∧ T.FactorsThruAlong S f namespace Coverage instance : CoeFun (Coverage C) (fun _ => (X : C) → Set (Presieve X)) where coe := covering variable (C) in /-- Associate a coverage to any Grothendieck topology. If `J` is a Grothendieck topology, and `K` is the associated coverage, then a presieve `S` is a covering presieve for `K` if and only if the sieve that it generates is a covering sieve for `J`. -/ def ofGrothendieck (J : GrothendieckTopology C) : Coverage C where covering X := { S | Sieve.generate S ∈ J X } pullback := by intro X Y f S (hS : Sieve.generate S ∈ J X) refine ⟨(Sieve.generate S).pullback f, ?_, fun Z g h => h⟩ dsimp rw [Sieve.generate_sieve] exact J.pullback_stable _ hS lemma ofGrothendieck_iff {X : C} {S : Presieve X} (J : GrothendieckTopology C) : S ∈ ofGrothendieck _ J X ↔ Sieve.generate S ∈ J X := Iff.rfl /-- An auxiliary definition used to define the Grothendieck topology associated to a coverage. See `Coverage.toGrothendieck`. -/ inductive Saturate (K : Coverage C) : (X : C) → Sieve X → Prop where | of (X : C) (S : Presieve X) (hS : S ∈ K X) : Saturate K X (Sieve.generate S) | top (X : C) : Saturate K X ⊤ | transitive (X : C) (R S : Sieve X) : Saturate K X R → (∀ ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, R f → Saturate K Y (S.pullback f)) → Saturate K X S lemma eq_top_pullback {X Y : C} {S T : Sieve X} (h : S ≤ T) (f : Y ⟶ X) (hf : S f) : T.pullback f = ⊤ := by ext Z g simp only [Sieve.pullback_apply, Sieve.top_apply, iff_true] apply h apply S.downward_closed exact hf lemma saturate_of_superset (K : Coverage C) {X : C} {S T : Sieve X} (h : S ≤ T) (hS : Saturate K X S) : Saturate K X T := by apply Saturate.transitive _ _ _ hS intro Y g hg rw [eq_top_pullback (h := h)] · apply Saturate.top · assumption variable (C) in /-- The Grothendieck topology associated to a coverage `K`. It is defined *inductively* as follows: 1. If `S` is a covering presieve for `K`, then the sieve generated by `S` is a covering sieve for the associated Grothendieck topology. 2. The top sieves are in the associated Grothendieck topology. 3. Add all sieves required by the *local character* axiom of a Grothendieck topology. The pullback compatibility condition for a coverage ensures that the associated Grothendieck topology is pullback stable, and so an additional constructor in the inductive construction is not needed. -/ def toGrothendieck (K : Coverage C) : GrothendieckTopology C where sieves := Saturate K top_mem' := .top pullback_stable' := by intro X Y S f hS induction hS generalizing Y with | of X S hS => obtain ⟨R,hR1,hR2⟩ := K.pullback f S hS suffices Sieve.generate R ≤ (Sieve.generate S).pullback f from saturate_of_superset _ this (Saturate.of _ _ hR1) rintro Z g ⟨W, i, e, h1, h2⟩ obtain ⟨WW, ii, ee, hh1, hh2⟩ := hR2 h1 refine ⟨WW, i ≫ ii, ee, hh1, ?_⟩ simp only [hh2, reassoc_of% h2, Category.assoc] | top X => apply Saturate.top | transitive X R S _ hS H1 _ => apply Saturate.transitive · apply H1 f intro Z g hg rw [← Sieve.pullback_comp] exact hS hg transitive' _ _ hS _ hR := .transitive _ _ _ hS hR instance : PartialOrder (Coverage C) where le A B := A.covering ≤ B.covering le_refl _ _ := le_refl _ le_trans _ _ _ h1 h2 X := le_trans (h1 X) (h2 X) le_antisymm _ _ h1 h2 := Coverage.ext <| funext <| fun X => le_antisymm (h1 X) (h2 X) variable (C) in /-- The two constructions `Coverage.toGrothendieck` and `Coverage.ofGrothendieck` form a Galois insertion. -/ def gi : GaloisInsertion (toGrothendieck C) (ofGrothendieck C) where choice K _ := toGrothendieck _ K choice_eq := fun _ _ => rfl le_l_u J X S hS := by rw [← Sieve.generate_sieve S] apply Saturate.of dsimp [ofGrothendieck] rwa [Sieve.generate_sieve S] gc K J := by constructor · intro H X S hS exact H _ <| Saturate.of _ _ hS · intro H X S hS induction hS with | of X S hS => exact H _ hS | top => apply J.top_mem | transitive X R S _ _ H1 H2 => exact J.transitive H1 _ H2 /-- An alternative characterization of the Grothendieck topology associated to a coverage `K`: it is the infimum of all Grothendieck topologies whose associated coverage contains `K`. -/ theorem toGrothendieck_eq_sInf (K : Coverage C) : toGrothendieck _ K = sInf {J | K ≤ ofGrothendieck _ J } := by apply le_antisymm · apply le_sInf; intro J hJ intro X S hS induction hS with | of X S hS => apply hJ; assumption | top => apply J.top_mem | transitive X R S _ _ H1 H2 => exact J.transitive H1 _ H2 · apply sInf_le intro X S hS apply Saturate.of _ _ hS instance : SemilatticeSup (Coverage C) where sup x y := { covering := fun B ↦ x.covering B ∪ y.covering B pullback := by rintro X Y f S (hx | hy) · obtain ⟨T, hT⟩ := x.pullback f S hx exact ⟨T, Or.inl hT.1, hT.2⟩ · obtain ⟨T, hT⟩ := y.pullback f S hy exact ⟨T, Or.inr hT.1, hT.2⟩ } toPartialOrder := inferInstance le_sup_left _ _ _ := Set.subset_union_left le_sup_right _ _ _ := Set.subset_union_right sup_le _ _ _ hx hy X := Set.union_subset_iff.mpr ⟨hx X, hy X⟩ @[simp] lemma sup_covering (x y : Coverage C) (B : C) : (x ⊔ y).covering B = x.covering B ∪ y.covering B := rfl /-- Any sieve that contains a covering presieve for a coverage is a covering sieve for the associated Grothendieck topology. -/ theorem mem_toGrothendieck_sieves_of_superset (K : Coverage C) {X : C} {S : Sieve X} {R : Presieve X} (h : R ≤ S) (hR : R ∈ K.covering X) : S ∈ (K.toGrothendieck C) X := K.saturate_of_superset ((Sieve.generate_le_iff _ _).mpr h) (Coverage.Saturate.of X _ hR) end Coverage open Coverage namespace Presieve /-- The main theorem of this file: Given a coverage `K` on `C`, a `Type*`-valued presheaf on `C` is a sheaf for `K` if and only if it is a sheaf for the associated Grothendieck topology. -/
theorem isSheaf_coverage (K : Coverage C) (P : Cᵒᵖ ⥤ Type*) : Presieve.IsSheaf (toGrothendieck _ K) P ↔ (∀ {X : C} (R : Presieve X), R ∈ K X → Presieve.IsSheafFor P R) := by constructor · intro H X R hR rw [Presieve.isSheafFor_iff_generate] apply H _ <| Saturate.of _ _ hR · intro H X S hS -- This is the key point of the proof: -- We must generalize the induction in the correct way. suffices ∀ ⦃Y : C⦄ (f : Y ⟶ X), Presieve.IsSheafFor P (S.pullback f).arrows by simpa using this (f := 𝟙 _) induction hS with | of X S hS => intro Y f obtain ⟨T, hT1, hT2⟩ := K.pullback f S hS apply Presieve.isSheafFor_of_factorsThru (S := T) · intro Z g hg obtain ⟨W, i, e, h1, h2⟩ := hT2 hg exact ⟨Z, 𝟙 _, g, ⟨W, i, e, h1, h2⟩, by simp⟩ · apply H; assumption · intro Z g _ obtain ⟨R, hR1, hR2⟩ := K.pullback g _ hT1 exact ⟨R, (H _ hR1).isSeparatedFor, hR2⟩ | top => intros; simpa using Presieve.isSheafFor_top_sieve _ | transitive X R S _ _ H1 H2 => intro Y f simp only [← Presieve.isSeparatedFor_and_exists_isAmalgamation_iff_isSheafFor] at * choose H1 H1' using H1 choose H2 H2' using H2 refine ⟨?_, fun x hx => ?_⟩ · intro x t₁ t₂ h₁ h₂ refine (H1 f).ext (fun Z g hg => ?_) refine (H2 hg (𝟙 _)).ext (fun ZZ gg hgg => ?_) simp only [Sieve.pullback_id, Sieve.pullback_apply] at hgg simp only [← types_comp_apply] rw [← P.map_comp, ← op_comp, h₁, h₂] simpa only [Sieve.pullback_apply, Category.assoc] using hgg let y : ∀ ⦃Z : C⦄ (g : Z ⟶ Y), ((S.pullback (g ≫ f)).pullback (𝟙 _)).arrows.FamilyOfElements P := fun Z g ZZ gg hgg => x (gg ≫ g) (by simpa using hgg) have hy : ∀ ⦃Z : C⦄ (g : Z ⟶ Y), (y g).Compatible := by intro Z g Y₁ Y₂ ZZ g₁ g₂ f₁ f₂ h₁ h₂ h rw [hx] rw [reassoc_of% h] choose z hz using fun ⦃Z : C⦄ ⦃g : Z ⟶ Y⦄ (hg : R.pullback f g) => H2' hg (𝟙 _) (y g) (hy g) let q : (R.pullback f).arrows.FamilyOfElements P := fun Z g hg => z hg have hq : q.Compatible := by intro Y₁ Y₂ Z g₁ g₂ f₁ f₂ h₁ h₂ h apply (H2 h₁ g₁).ext intro ZZ gg hgg simp only [← types_comp_apply] rw [← P.map_comp, ← P.map_comp, ← op_comp, ← op_comp, hz, hz] · dsimp [y]; congr 1; simp only [Category.assoc, h] · simpa [reassoc_of% h] using hgg · simpa using hgg obtain ⟨t, ht⟩ := H1' f q hq refine ⟨t, fun Z g hg => ?_⟩ refine (H1 (g ≫ f)).ext (fun ZZ gg hgg => ?_) rw [← types_comp_apply _ (P.map gg.op), ← P.map_comp, ← op_comp, ht] on_goal 2 => simpa using hgg refine (H2 hgg (𝟙 _)).ext (fun ZZZ ggg hggg => ?_) rw [← types_comp_apply _ (P.map ggg.op), ← P.map_comp, ← op_comp, hz] on_goal 2 => simpa using hggg refine (H2 hgg ggg).ext (fun ZZZZ gggg _ => ?_) rw [← types_comp_apply _ (P.map gggg.op), ← P.map_comp, ← op_comp] apply hx simp
Mathlib/CategoryTheory/Sites/Coverage.lean
326
394
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Wrenna Robson -/ import Mathlib.Algebra.BigOperators.Group.Finset.Pi import Mathlib.Algebra.Polynomial.FieldDivision import Mathlib.LinearAlgebra.Vandermonde import Mathlib.RingTheory.Polynomial.Basic /-! # Lagrange interpolation ## Main definitions * In everything that follows, `s : Finset ι` is a finite set of indexes, with `v : ι → F` an indexing of the field over some type. We call the image of v on s the interpolation nodes, though strictly unique nodes are only defined when v is injective on s. * `Lagrange.basisDivisor x y`, with `x y : F`. These are the normalised irreducible factors of the Lagrange basis polynomials. They evaluate to `1` at `x` and `0` at `y` when `x` and `y` are distinct. * `Lagrange.basis v i` with `i : ι`: the Lagrange basis polynomial that evaluates to `1` at `v i` and `0` at `v j` for `i ≠ j`. * `Lagrange.interpolate v r` where `r : ι → F` is a function from the fintype to the field: the Lagrange interpolant that evaluates to `r i` at `x i` for all `i : ι`. The `r i` are the _values_ associated with the _nodes_`x i`. -/ open Polynomial section PolynomialDetermination namespace Polynomial variable {R : Type*} [CommRing R] [IsDomain R] {f g : R[X]} section Finset open Function Fintype open scoped Finset variable (s : Finset R) theorem eq_zero_of_degree_lt_of_eval_finset_eq_zero (degree_f_lt : f.degree < #s) (eval_f : ∀ x ∈ s, f.eval x = 0) : f = 0 := by rw [← mem_degreeLT] at degree_f_lt simp_rw [eval_eq_sum_degreeLTEquiv degree_f_lt] at eval_f rw [← degreeLTEquiv_eq_zero_iff_eq_zero degree_f_lt] exact Matrix.eq_zero_of_forall_index_sum_mul_pow_eq_zero (Injective.comp (Embedding.subtype _).inj' (equivFinOfCardEq (card_coe _)).symm.injective) fun _ => eval_f _ (Finset.coe_mem _) theorem eq_of_degree_sub_lt_of_eval_finset_eq (degree_fg_lt : (f - g).degree < #s) (eval_fg : ∀ x ∈ s, f.eval x = g.eval x) : f = g := by rw [← sub_eq_zero] refine eq_zero_of_degree_lt_of_eval_finset_eq_zero _ degree_fg_lt ?_ simp_rw [eval_sub, sub_eq_zero] exact eval_fg theorem eq_of_degrees_lt_of_eval_finset_eq (degree_f_lt : f.degree < #s) (degree_g_lt : g.degree < #s) (eval_fg : ∀ x ∈ s, f.eval x = g.eval x) : f = g := by rw [← mem_degreeLT] at degree_f_lt degree_g_lt refine eq_of_degree_sub_lt_of_eval_finset_eq _ ?_ eval_fg rw [← mem_degreeLT]; exact Submodule.sub_mem _ degree_f_lt degree_g_lt /-- Two polynomials, with the same degree and leading coefficient, which have the same evaluation on a set of distinct values with cardinality equal to the degree, are equal. -/ theorem eq_of_degree_le_of_eval_finset_eq (h_deg_le : f.degree ≤ #s) (h_deg_eq : f.degree = g.degree) (hlc : f.leadingCoeff = g.leadingCoeff) (h_eval : ∀ x ∈ s, f.eval x = g.eval x) : f = g := by rcases eq_or_ne f 0 with rfl | hf · rwa [degree_zero, eq_comm, degree_eq_bot, eq_comm] at h_deg_eq · exact eq_of_degree_sub_lt_of_eval_finset_eq s (lt_of_lt_of_le (degree_sub_lt h_deg_eq hf hlc) h_deg_le) h_eval end Finset section Indexed open Finset variable {ι : Type*} {v : ι → R} (s : Finset ι) theorem eq_zero_of_degree_lt_of_eval_index_eq_zero (hvs : Set.InjOn v s) (degree_f_lt : f.degree < #s) (eval_f : ∀ i ∈ s, f.eval (v i) = 0) : f = 0 := by classical rw [← card_image_of_injOn hvs] at degree_f_lt refine eq_zero_of_degree_lt_of_eval_finset_eq_zero _ degree_f_lt ?_ intro x hx rcases mem_image.mp hx with ⟨_, hj, rfl⟩ exact eval_f _ hj theorem eq_of_degree_sub_lt_of_eval_index_eq (hvs : Set.InjOn v s) (degree_fg_lt : (f - g).degree < #s) (eval_fg : ∀ i ∈ s, f.eval (v i) = g.eval (v i)) : f = g := by rw [← sub_eq_zero] refine eq_zero_of_degree_lt_of_eval_index_eq_zero _ hvs degree_fg_lt ?_ simp_rw [eval_sub, sub_eq_zero] exact eval_fg theorem eq_of_degrees_lt_of_eval_index_eq (hvs : Set.InjOn v s) (degree_f_lt : f.degree < #s) (degree_g_lt : g.degree < #s) (eval_fg : ∀ i ∈ s, f.eval (v i) = g.eval (v i)) : f = g := by refine eq_of_degree_sub_lt_of_eval_index_eq _ hvs ?_ eval_fg rw [← mem_degreeLT] at degree_f_lt degree_g_lt ⊢ exact Submodule.sub_mem _ degree_f_lt degree_g_lt theorem eq_of_degree_le_of_eval_index_eq (hvs : Set.InjOn v s) (h_deg_le : f.degree ≤ #s) (h_deg_eq : f.degree = g.degree) (hlc : f.leadingCoeff = g.leadingCoeff) (h_eval : ∀ i ∈ s, f.eval (v i) = g.eval (v i)) : f = g := by rcases eq_or_ne f 0 with rfl | hf · rwa [degree_zero, eq_comm, degree_eq_bot, eq_comm] at h_deg_eq · exact eq_of_degree_sub_lt_of_eval_index_eq s hvs (lt_of_lt_of_le (degree_sub_lt h_deg_eq hf hlc) h_deg_le) h_eval end Indexed end Polynomial end PolynomialDetermination noncomputable section namespace Lagrange open Polynomial section BasisDivisor variable {F : Type*} [Field F] variable {x y : F} /-- `basisDivisor x y` is the unique linear or constant polynomial such that when evaluated at `x` it gives `1` and `y` it gives `0` (where when `x = y` it is identically `0`). Such polynomials are the building blocks for the Lagrange interpolants. -/ def basisDivisor (x y : F) : F[X] := C (x - y)⁻¹ * (X - C y) theorem basisDivisor_self : basisDivisor x x = 0 := by simp only [basisDivisor, sub_self, inv_zero, map_zero, zero_mul] theorem basisDivisor_inj (hxy : basisDivisor x y = 0) : x = y := by simp_rw [basisDivisor, mul_eq_zero, X_sub_C_ne_zero, or_false, C_eq_zero, inv_eq_zero, sub_eq_zero] at hxy exact hxy @[simp] theorem basisDivisor_eq_zero_iff : basisDivisor x y = 0 ↔ x = y := ⟨basisDivisor_inj, fun H => H ▸ basisDivisor_self⟩ theorem basisDivisor_ne_zero_iff : basisDivisor x y ≠ 0 ↔ x ≠ y := by rw [Ne, basisDivisor_eq_zero_iff] theorem degree_basisDivisor_of_ne (hxy : x ≠ y) : (basisDivisor x y).degree = 1 := by rw [basisDivisor, degree_mul, degree_X_sub_C, degree_C, zero_add] exact inv_ne_zero (sub_ne_zero_of_ne hxy) @[simp] theorem degree_basisDivisor_self : (basisDivisor x x).degree = ⊥ := by rw [basisDivisor_self, degree_zero] theorem natDegree_basisDivisor_self : (basisDivisor x x).natDegree = 0 := by rw [basisDivisor_self, natDegree_zero] theorem natDegree_basisDivisor_of_ne (hxy : x ≠ y) : (basisDivisor x y).natDegree = 1 := natDegree_eq_of_degree_eq_some (degree_basisDivisor_of_ne hxy) @[simp] theorem eval_basisDivisor_right : eval y (basisDivisor x y) = 0 := by simp only [basisDivisor, eval_mul, eval_C, eval_sub, eval_X, sub_self, mul_zero] theorem eval_basisDivisor_left_of_ne (hxy : x ≠ y) : eval x (basisDivisor x y) = 1 := by simp only [basisDivisor, eval_mul, eval_C, eval_sub, eval_X] exact inv_mul_cancel₀ (sub_ne_zero_of_ne hxy) end BasisDivisor section Basis variable {F : Type*} [Field F] {ι : Type*} [DecidableEq ι] variable {s : Finset ι} {v : ι → F} {i j : ι} open Finset /-- Lagrange basis polynomials indexed by `s : Finset ι`, defined at nodes `v i` for a map `v : ι → F`. For `i, j ∈ s`, `basis s v i` evaluates to 0 at `v j` for `i ≠ j`. When `v` is injective on `s`, `basis s v i` evaluates to 1 at `v i`. -/ protected def basis (s : Finset ι) (v : ι → F) (i : ι) : F[X] := ∏ j ∈ s.erase i, basisDivisor (v i) (v j) @[simp] theorem basis_empty : Lagrange.basis ∅ v i = 1 := rfl @[simp] theorem basis_singleton (i : ι) : Lagrange.basis {i} v i = 1 := by rw [Lagrange.basis, erase_singleton, prod_empty] @[simp] theorem basis_pair_left (hij : i ≠ j) : Lagrange.basis {i, j} v i = basisDivisor (v i) (v j) := by simp only [Lagrange.basis, hij, erase_insert_eq_erase, erase_eq_of_not_mem, mem_singleton, not_false_iff, prod_singleton] @[simp] theorem basis_pair_right (hij : i ≠ j) : Lagrange.basis {i, j} v j = basisDivisor (v j) (v i) := by rw [pair_comm] exact basis_pair_left hij.symm theorem basis_ne_zero (hvs : Set.InjOn v s) (hi : i ∈ s) : Lagrange.basis s v i ≠ 0 := by simp_rw [Lagrange.basis, prod_ne_zero_iff, Ne, mem_erase] rintro j ⟨hij, hj⟩ rw [basisDivisor_eq_zero_iff, hvs.eq_iff hi hj] exact hij.symm @[simp] theorem eval_basis_self (hvs : Set.InjOn v s) (hi : i ∈ s) : (Lagrange.basis s v i).eval (v i) = 1 := by rw [Lagrange.basis, eval_prod] refine prod_eq_one fun j H => ?_ rw [eval_basisDivisor_left_of_ne] rcases mem_erase.mp H with ⟨hij, hj⟩ exact mt (hvs hi hj) hij.symm @[simp] theorem eval_basis_of_ne (hij : i ≠ j) (hj : j ∈ s) : (Lagrange.basis s v i).eval (v j) = 0 := by simp_rw [Lagrange.basis, eval_prod, prod_eq_zero_iff] exact ⟨j, ⟨mem_erase.mpr ⟨hij.symm, hj⟩, eval_basisDivisor_right⟩⟩ @[simp] theorem natDegree_basis (hvs : Set.InjOn v s) (hi : i ∈ s) : (Lagrange.basis s v i).natDegree = #s - 1 := by have H : ∀ j, j ∈ s.erase i → basisDivisor (v i) (v j) ≠ 0 := by simp_rw [Ne, mem_erase, basisDivisor_eq_zero_iff] exact fun j ⟨hij₁, hj⟩ hij₂ => hij₁ (hvs hj hi hij₂.symm) rw [← card_erase_of_mem hi, card_eq_sum_ones] convert natDegree_prod _ _ H using 1 refine sum_congr rfl fun j hj => (natDegree_basisDivisor_of_ne ?_).symm rw [Ne, ← basisDivisor_eq_zero_iff] exact H _ hj theorem degree_basis (hvs : Set.InjOn v s) (hi : i ∈ s) : (Lagrange.basis s v i).degree = ↑(#s - 1) := by rw [degree_eq_natDegree (basis_ne_zero hvs hi), natDegree_basis hvs hi] theorem sum_basis (hvs : Set.InjOn v s) (hs : s.Nonempty) : ∑ j ∈ s, Lagrange.basis s v j = 1 := by refine eq_of_degrees_lt_of_eval_index_eq s hvs (lt_of_le_of_lt (degree_sum_le _ _) ?_) ?_ ?_ · rw [Nat.cast_withBot, Finset.sup_lt_iff (WithBot.bot_lt_coe #s)] intro i hi rw [degree_basis hvs hi, Nat.cast_withBot, WithBot.coe_lt_coe] exact Nat.pred_lt (card_ne_zero_of_mem hi) · rw [degree_one, ← WithBot.coe_zero, Nat.cast_withBot, WithBot.coe_lt_coe] exact Nonempty.card_pos hs · intro i hi rw [eval_finset_sum, eval_one, ← add_sum_erase _ _ hi, eval_basis_self hvs hi, add_eq_left] refine sum_eq_zero fun j hj => ?_ rcases mem_erase.mp hj with ⟨hij, _⟩ rw [eval_basis_of_ne hij hi] theorem basisDivisor_add_symm {x y : F} (hxy : x ≠ y) : basisDivisor x y + basisDivisor y x = 1 := by classical rw [← sum_basis Function.injective_id.injOn ⟨x, mem_insert_self _ {y}⟩, sum_insert (not_mem_singleton.mpr hxy), sum_singleton, basis_pair_left hxy, basis_pair_right hxy, id, id] end Basis section Interpolate variable {F : Type*} [Field F] {ι : Type*} [DecidableEq ι] variable {s t : Finset ι} {i j : ι} {v : ι → F} (r r' : ι → F) open Finset /-- Lagrange interpolation: given a finset `s : Finset ι`, a nodal map `v : ι → F` injective on `s` and a value function `r : ι → F`, `interpolate s v r` is the unique polynomial of degree `< #s` that takes value `r i` on `v i` for all `i` in `s`. -/ @[simps] def interpolate (s : Finset ι) (v : ι → F) : (ι → F) →ₗ[F] F[X] where toFun r := ∑ i ∈ s, C (r i) * Lagrange.basis s v i map_add' f g := by simp_rw [← Finset.sum_add_distrib] have h : (fun x => C (f x) * Lagrange.basis s v x + C (g x) * Lagrange.basis s v x) = (fun x => C ((f + g) x) * Lagrange.basis s v x) := by simp_rw [← add_mul, ← C_add, Pi.add_apply] rw [h] map_smul' c f := by simp_rw [Finset.smul_sum, C_mul', smul_smul, Pi.smul_apply, RingHom.id_apply, smul_eq_mul] theorem interpolate_empty : interpolate ∅ v r = 0 := by rw [interpolate_apply, sum_empty] theorem interpolate_singleton : interpolate {i} v r = C (r i) := by rw [interpolate_apply, sum_singleton, basis_singleton, mul_one] theorem interpolate_one (hvs : Set.InjOn v s) (hs : s.Nonempty) : interpolate s v 1 = 1 := by simp_rw [interpolate_apply, Pi.one_apply, map_one, one_mul] exact sum_basis hvs hs theorem eval_interpolate_at_node (hvs : Set.InjOn v s) (hi : i ∈ s) : eval (v i) (interpolate s v r) = r i := by rw [interpolate_apply, eval_finset_sum, ← add_sum_erase _ _ hi] simp_rw [eval_mul, eval_C, eval_basis_self hvs hi, mul_one, add_eq_left] refine sum_eq_zero fun j H => ?_ rw [eval_basis_of_ne (mem_erase.mp H).1 hi, mul_zero] theorem degree_interpolate_le (hvs : Set.InjOn v s) : (interpolate s v r).degree ≤ ↑(#s - 1) := by refine (degree_sum_le _ _).trans ?_ rw [Finset.sup_le_iff] intro i hi rw [degree_mul, degree_basis hvs hi] by_cases hr : r i = 0 · simpa only [hr, map_zero, degree_zero, WithBot.bot_add] using bot_le · rw [degree_C hr, zero_add] theorem degree_interpolate_lt (hvs : Set.InjOn v s) : (interpolate s v r).degree < #s := by rw [Nat.cast_withBot] rcases eq_empty_or_nonempty s with (rfl | h) · rw [interpolate_empty, degree_zero, card_empty] exact WithBot.bot_lt_coe _ · refine lt_of_le_of_lt (degree_interpolate_le _ hvs) ?_ rw [Nat.cast_withBot, WithBot.coe_lt_coe] exact Nat.sub_lt (Nonempty.card_pos h) zero_lt_one theorem degree_interpolate_erase_lt (hvs : Set.InjOn v s) (hi : i ∈ s) : (interpolate (s.erase i) v r).degree < ↑(#s - 1) := by rw [← Finset.card_erase_of_mem hi] exact degree_interpolate_lt _ (Set.InjOn.mono (coe_subset.mpr (erase_subset _ _)) hvs) theorem values_eq_on_of_interpolate_eq (hvs : Set.InjOn v s) (hrr' : interpolate s v r = interpolate s v r') : ∀ i ∈ s, r i = r' i := fun _ hi => by rw [← eval_interpolate_at_node r hvs hi, hrr', eval_interpolate_at_node r' hvs hi] theorem interpolate_eq_of_values_eq_on (hrr' : ∀ i ∈ s, r i = r' i) : interpolate s v r = interpolate s v r' := sum_congr rfl fun i hi => by rw [hrr' _ hi] theorem interpolate_eq_iff_values_eq_on (hvs : Set.InjOn v s) : interpolate s v r = interpolate s v r' ↔ ∀ i ∈ s, r i = r' i := ⟨values_eq_on_of_interpolate_eq _ _ hvs, interpolate_eq_of_values_eq_on _ _⟩ theorem eq_interpolate {f : F[X]} (hvs : Set.InjOn v s) (degree_f_lt : f.degree < #s) : f = interpolate s v fun i => f.eval (v i) := eq_of_degrees_lt_of_eval_index_eq _ hvs degree_f_lt (degree_interpolate_lt _ hvs) fun _ hi => (eval_interpolate_at_node (fun x ↦ eval (v x) f) hvs hi).symm theorem eq_interpolate_of_eval_eq {f : F[X]} (hvs : Set.InjOn v s) (degree_f_lt : f.degree < #s) (eval_f : ∀ i ∈ s, f.eval (v i) = r i) : f = interpolate s v r := by rw [eq_interpolate hvs degree_f_lt] exact interpolate_eq_of_values_eq_on _ _ eval_f /-- This is the characteristic property of the interpolation: the interpolation is the unique polynomial of `degree < Fintype.card ι` which takes the value of the `r i` on the `v i`. -/ theorem eq_interpolate_iff {f : F[X]} (hvs : Set.InjOn v s) : (f.degree < #s ∧ ∀ i ∈ s, eval (v i) f = r i) ↔ f = interpolate s v r := by constructor <;> intro h · exact eq_interpolate_of_eval_eq _ hvs h.1 h.2 · rw [h] exact ⟨degree_interpolate_lt _ hvs, fun _ hi => eval_interpolate_at_node _ hvs hi⟩ /-- Lagrange interpolation induces isomorphism between functions from `s` and polynomials of degree less than `Fintype.card ι`. -/ def funEquivDegreeLT (hvs : Set.InjOn v s) : degreeLT F #s ≃ₗ[F] s → F where toFun f i := f.1.eval (v i) map_add' _ _ := funext fun _ => eval_add map_smul' c f := funext <| by simp invFun r := ⟨interpolate s v fun x => if hx : x ∈ s then r ⟨x, hx⟩ else 0, mem_degreeLT.2 <| degree_interpolate_lt _ hvs⟩ left_inv := by rintro ⟨f, hf⟩ simp only [Subtype.mk_eq_mk, Subtype.coe_mk, dite_eq_ite] rw [mem_degreeLT] at hf conv => rhs; rw [eq_interpolate hvs hf] exact interpolate_eq_of_values_eq_on _ _ fun _ hi => if_pos hi right_inv := by intro f ext ⟨i, hi⟩ simp only [Subtype.coe_mk, eval_interpolate_at_node _ hvs hi] exact dif_pos hi theorem interpolate_eq_sum_interpolate_insert_sdiff (hvt : Set.InjOn v t) (hs : s.Nonempty) (hst : s ⊆ t) : interpolate t v r = ∑ i ∈ s, interpolate (insert i (t \ s)) v r * Lagrange.basis s v i := by symm refine eq_interpolate_of_eval_eq _ hvt (lt_of_le_of_lt (degree_sum_le _ _) ?_) fun i hi => ?_ · simp_rw [Nat.cast_withBot, Finset.sup_lt_iff (WithBot.bot_lt_coe #t), degree_mul] intro i hi have hs : 1 ≤ #s := Nonempty.card_pos ⟨_, hi⟩ have hst' : #s ≤ #t := card_le_card hst have H : #t = 1 + (#t - #s) + (#s - 1) := by rw [add_assoc, tsub_add_tsub_cancel hst' hs, ← add_tsub_assoc_of_le (hs.trans hst'), Nat.succ_add_sub_one, zero_add] rw [degree_basis (Set.InjOn.mono hst hvt) hi, H, WithBot.coe_add, Nat.cast_withBot, WithBot.add_lt_add_iff_right (@WithBot.coe_ne_bot _ (#s - 1))] convert degree_interpolate_lt _ (hvt.mono (coe_subset.mpr (insert_subset_iff.mpr ⟨hst hi, sdiff_subset⟩))) rw [card_insert_of_not_mem (not_mem_sdiff_of_mem_right hi), card_sdiff hst, add_comm] · simp_rw [eval_finset_sum, eval_mul] by_cases hi' : i ∈ s · rw [← add_sum_erase _ _ hi', eval_basis_self (hvt.mono hst) hi', eval_interpolate_at_node _ (hvt.mono (coe_subset.mpr (insert_subset_iff.mpr ⟨hi, sdiff_subset⟩))) (mem_insert_self _ _), mul_one, add_eq_left] refine sum_eq_zero fun j hj => ?_ rcases mem_erase.mp hj with ⟨hij, _⟩ rw [eval_basis_of_ne hij hi', mul_zero] · have H : (∑ j ∈ s, eval (v i) (Lagrange.basis s v j)) = 1 := by rw [← eval_finset_sum, sum_basis (hvt.mono hst) hs, eval_one] rw [← mul_one (r i), ← H, mul_sum] refine sum_congr rfl fun j hj => ?_ congr exact eval_interpolate_at_node _ (hvt.mono (insert_subset_iff.mpr ⟨hst hj, sdiff_subset⟩)) (mem_insert.mpr (Or.inr (mem_sdiff.mpr ⟨hi, hi'⟩))) theorem interpolate_eq_add_interpolate_erase (hvs : Set.InjOn v s) (hi : i ∈ s) (hj : j ∈ s) (hij : i ≠ j) : interpolate s v r = interpolate (s.erase j) v r * basisDivisor (v i) (v j) + interpolate (s.erase i) v r * basisDivisor (v j) (v i) := by rw [interpolate_eq_sum_interpolate_insert_sdiff _ hvs ⟨i, mem_insert_self i {j}⟩ _, sum_insert (not_mem_singleton.mpr hij), sum_singleton, basis_pair_left hij, basis_pair_right hij, sdiff_insert_insert_of_mem_of_not_mem hi (not_mem_singleton.mpr hij), sdiff_singleton_eq_erase, pair_comm, sdiff_insert_insert_of_mem_of_not_mem hj (not_mem_singleton.mpr hij.symm), sdiff_singleton_eq_erase] exact insert_subset_iff.mpr ⟨hi, singleton_subset_iff.mpr hj⟩ end Interpolate section Nodal variable {R : Type*} [CommRing R] {ι : Type*} variable {s : Finset ι} {v : ι → R} open Finset Polynomial /-- `nodal s v` is the unique monic polynomial whose roots are the nodes defined by `v` and `s`. That is, the roots of `nodal s v` are exactly the image of `v` on `s`, with appropriate multiplicity. We can use `nodal` to define the barycentric forms of the evaluated interpolant. -/ def nodal (s : Finset ι) (v : ι → R) : R[X] := ∏ i ∈ s, (X - C (v i)) theorem nodal_eq (s : Finset ι) (v : ι → R) : nodal s v = ∏ i ∈ s, (X - C (v i)) := rfl @[simp] theorem nodal_empty : nodal ∅ v = 1 := by rfl @[simp] theorem natDegree_nodal [Nontrivial R] : (nodal s v).natDegree = #s := by simp_rw [nodal, natDegree_prod_of_monic (h := fun i _ => monic_X_sub_C (v i)), natDegree_X_sub_C, sum_const, smul_eq_mul, mul_one] theorem nodal_ne_zero [Nontrivial R] : nodal s v ≠ 0 := by rcases s.eq_empty_or_nonempty with (rfl | h) · exact one_ne_zero · apply ne_zero_of_natDegree_gt (n := 0) simp only [natDegree_nodal, h.card_pos] @[simp] theorem degree_nodal [Nontrivial R] : (nodal s v).degree = #s := by simp_rw [degree_eq_natDegree nodal_ne_zero, natDegree_nodal] theorem nodal_monic : (nodal s v).Monic := monic_prod_of_monic s (fun i ↦ X - C (v i)) fun i _ ↦ monic_X_sub_C (v i) theorem eval_nodal {x : R} : (nodal s v).eval x = ∏ i ∈ s, (x - v i) := by simp_rw [nodal, eval_prod, eval_sub, eval_X, eval_C] theorem eval_nodal_at_node {i : ι} (hi : i ∈ s) : eval (v i) (nodal s v) = 0 := by rw [eval_nodal] exact s.prod_eq_zero hi (sub_self (v i)) theorem eval_nodal_not_at_node [Nontrivial R] [NoZeroDivisors R] {x : R} (hx : ∀ i ∈ s, x ≠ v i) : eval x (nodal s v) ≠ 0 := by simp_rw [nodal, eval_prod, prod_ne_zero_iff, eval_sub, eval_X, eval_C, sub_ne_zero] exact hx theorem nodal_eq_mul_nodal_erase [DecidableEq ι] {i : ι} (hi : i ∈ s) : nodal s v = (X - C (v i)) * nodal (s.erase i) v := by simp_rw [nodal, Finset.mul_prod_erase _ (fun x => X - C (v x)) hi] theorem X_sub_C_dvd_nodal (v : ι → R) {i : ι} (hi : i ∈ s) : X - C (v i) ∣ nodal s v := by classical exact ⟨nodal (s.erase i) v, nodal_eq_mul_nodal_erase hi⟩ theorem nodal_insert_eq_nodal [DecidableEq ι] {i : ι} (hi : i ∉ s) : nodal (insert i s) v = (X - C (v i)) * nodal s v := by simp_rw [nodal, prod_insert hi] theorem derivative_nodal [DecidableEq ι] : derivative (nodal s v) = ∑ i ∈ s, nodal (s.erase i) v := by refine s.induction_on ?_ fun i t hit IH => ?_ · rw [nodal_empty, derivative_one, sum_empty] · rw [nodal_insert_eq_nodal hit, derivative_mul, IH, derivative_sub, derivative_X, derivative_C, sub_zero, one_mul, sum_insert hit, mul_sum, erase_insert hit, add_right_inj] refine sum_congr rfl fun j hjt => ?_ rw [t.erase_insert_of_ne (ne_of_mem_of_not_mem hjt hit).symm, nodal_insert_eq_nodal (mem_of_mem_erase.mt hit)]
theorem eval_nodal_derivative_eval_node_eq [DecidableEq ι] {i : ι} (hi : i ∈ s) : eval (v i) (derivative (nodal s v)) = eval (v i) (nodal (s.erase i) v) := by
Mathlib/LinearAlgebra/Lagrange.lean
520
521
/- 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.Group.Nat.Even import Mathlib.Data.Nat.Cast.Basic import Mathlib.Data.Nat.Cast.Commute import Mathlib.Data.Set.Operations import Mathlib.Logic.Function.Iterate /-! # Even and odd elements in rings This file defines odd elements and proves some general facts about even and odd elements of rings. As opposed to `Even`, `Odd` does not have a multiplicative counterpart. ## TODO Try to generalize `Even` lemmas further. For example, there are still a few lemmas whose `Semiring` assumptions I (DT) am not convinced are necessary. If that turns out to be true, they could be moved to `Mathlib.Algebra.Group.Even`. ## See also `Mathlib.Algebra.Group.Even` for the definition of even elements. -/ assert_not_exists DenselyOrdered OrderedRing open MulOpposite variable {F α β : Type*} section Monoid variable [Monoid α] [HasDistribNeg α] {n : ℕ} {a : α} @[simp] lemma Even.neg_pow : Even n → ∀ a : α, (-a) ^ n = a ^ n := by rintro ⟨c, rfl⟩ a simp_rw [← two_mul, pow_mul, neg_sq] lemma Even.neg_one_pow (h : Even n) : (-1 : α) ^ n = 1 := by rw [h.neg_pow, one_pow] end Monoid section DivisionMonoid variable [DivisionMonoid α] [HasDistribNeg α] {a : α} {n : ℤ} lemma Even.neg_zpow : Even n → ∀ a : α, (-a) ^ n = a ^ n := by rintro ⟨c, rfl⟩ a; simp_rw [← Int.two_mul, zpow_mul, zpow_two, neg_mul_neg] lemma Even.neg_one_zpow (h : Even n) : (-1 : α) ^ n = 1 := by rw [h.neg_zpow, one_zpow] end DivisionMonoid @[simp] lemma IsSquare.zero [MulZeroClass α] : IsSquare (0 : α) := ⟨0, (mul_zero _).symm⟩ section Semiring variable [Semiring α] [Semiring β] {a b : α} {m n : ℕ} lemma even_iff_exists_two_mul : Even a ↔ ∃ b, a = 2 * b := by simp [even_iff_exists_two_nsmul] lemma even_iff_two_dvd : Even a ↔ 2 ∣ a := by simp [Even, Dvd.dvd, two_mul] alias ⟨Even.two_dvd, _⟩ := even_iff_two_dvd lemma Even.trans_dvd (ha : Even a) (hab : a ∣ b) : Even b := even_iff_two_dvd.2 <| ha.two_dvd.trans hab lemma Dvd.dvd.even (hab : a ∣ b) (ha : Even a) : Even b := ha.trans_dvd hab @[simp] lemma range_two_mul (α) [NonAssocSemiring α] : Set.range (fun x : α ↦ 2 * x) = {a | Even a} := by ext x simp [eq_comm, two_mul, Even] @[simp] lemma even_two : Even (2 : α) := ⟨1, by rw [one_add_one_eq_two]⟩ @[simp] lemma Even.mul_left (ha : Even a) (b) : Even (b * a) := ha.map (AddMonoidHom.mulLeft _) @[simp] lemma Even.mul_right (ha : Even a) (b) : Even (a * b) := ha.map (AddMonoidHom.mulRight _) lemma even_two_mul (a : α) : Even (2 * a) := ⟨a, two_mul _⟩ lemma Even.pow_of_ne_zero (ha : Even a) : ∀ {n : ℕ}, n ≠ 0 → Even (a ^ n) | n + 1, _ => by rw [pow_succ]; exact ha.mul_left _ /-- An element `a` of a semiring is odd if there exists `k` such `a = 2*k + 1`. -/ def Odd (a : α) : Prop := ∃ k, a = 2 * k + 1 lemma odd_iff_exists_bit1 : Odd a ↔ ∃ b, a = 2 * b + 1 := exists_congr fun b ↦ by rw [two_mul] alias ⟨Odd.exists_bit1, _⟩ := odd_iff_exists_bit1 @[simp] lemma range_two_mul_add_one (α : Type*) [Semiring α] : Set.range (fun x : α ↦ 2 * x + 1) = {a | Odd a} := by ext x; simp [Odd, eq_comm] lemma Even.add_odd : Even a → Odd b → Odd (a + b) := by rintro ⟨a, rfl⟩ ⟨b, rfl⟩; exact ⟨a + b, by rw [mul_add, ← two_mul, add_assoc]⟩ lemma Even.odd_add (ha : Even a) (hb : Odd b) : Odd (b + a) := add_comm a b ▸ ha.add_odd hb lemma Odd.add_even (ha : Odd a) (hb : Even b) : Odd (a + b) := add_comm a b ▸ hb.add_odd ha lemma Odd.add_odd : Odd a → Odd b → Even (a + b) := by rintro ⟨a, rfl⟩ ⟨b, rfl⟩ refine ⟨a + b + 1, ?_⟩ rw [two_mul, two_mul] ac_rfl @[simp] lemma odd_one : Odd (1 : α) := ⟨0, (zero_add _).symm.trans (congr_arg (· + (1 : α)) (mul_zero _).symm)⟩ @[simp] lemma Even.add_one (h : Even a) : Odd (a + 1) := h.add_odd odd_one @[simp] lemma Even.one_add (h : Even a) : Odd (1 + a) := h.odd_add odd_one @[simp] lemma Odd.add_one (h : Odd a) : Even (a + 1) := h.add_odd odd_one @[simp] lemma Odd.one_add (h : Odd a) : Even (1 + a) := odd_one.add_odd h lemma odd_two_mul_add_one (a : α) : Odd (2 * a + 1) := ⟨_, rfl⟩ @[simp] lemma odd_add_self_one' : Odd (a + (a + 1)) := by simp [← add_assoc] @[simp] lemma odd_add_one_self : Odd (a + 1 + a) := by simp [add_comm _ a] @[simp] lemma odd_add_one_self' : Odd (a + (1 + a)) := by simp [add_comm 1 a] lemma Odd.map [FunLike F α β] [RingHomClass F α β] (f : F) : Odd a → Odd (f a) := by rintro ⟨a, rfl⟩; exact ⟨f a, by simp [two_mul]⟩ lemma Odd.natCast {R : Type*} [Semiring R] {n : ℕ} (hn : Odd n) : Odd (n : R) := hn.map <| Nat.castRingHom R @[simp] lemma Odd.mul : Odd a → Odd b → Odd (a * b) := by rintro ⟨a, rfl⟩ ⟨b, rfl⟩ refine ⟨2 * a * b + b + a, ?_⟩ rw [mul_add, add_mul, mul_one, ← add_assoc, one_mul, mul_assoc, ← mul_add, ← mul_add, ← mul_assoc, ← Nat.cast_two, ← Nat.cast_comm] lemma Odd.pow (ha : Odd a) : ∀ {n : ℕ}, Odd (a ^ n) | 0 => by rw [pow_zero] exact odd_one | n + 1 => by rw [pow_succ]; exact ha.pow.mul ha lemma Odd.pow_add_pow_eq_zero [IsCancelAdd α] (hn : Odd n) (hab : a + b = 0) : a ^ n + b ^ n = 0 := by obtain ⟨k, rfl⟩ := hn induction k with | zero => simpa | succ k ih => ?_ have : a ^ 2 = b ^ 2 := add_right_cancel <| calc a ^ 2 + a * b = 0 := by rw [sq, ← mul_add, hab, mul_zero] _ = b ^ 2 + a * b := by rw [sq, ← add_mul, add_comm, hab, zero_mul] refine add_right_cancel (b := b ^ (2 * k + 1) * a ^ 2) ?_ calc _ = (a ^ (2 * k + 1) + b ^ (2 * k + 1)) * a ^ 2 + b ^ (2 * k + 3) := by rw [add_mul, ← pow_add, add_right_comm]; rfl _ = _ := by rw [ih, zero_mul, zero_add, zero_add, this, ← pow_add] end Semiring section Monoid variable [Monoid α] [HasDistribNeg α] {n : ℕ} lemma Odd.neg_pow : Odd n → ∀ a : α, (-a) ^ n = -a ^ n := by rintro ⟨c, rfl⟩ a; simp_rw [pow_add, pow_mul, neg_sq, pow_one, mul_neg] @[simp] lemma Odd.neg_one_pow (h : Odd n) : (-1 : α) ^ n = -1 := by rw [h.neg_pow, one_pow] end Monoid section Ring variable [Ring α] {a b : α} {n : ℕ} lemma even_neg_two : Even (-2 : α) := by simp only [even_neg, even_two] lemma Odd.neg (hp : Odd a) : Odd (-a) := by obtain ⟨k, hk⟩ := hp use -(k + 1) rw [mul_neg, mul_add, neg_add, add_assoc, two_mul (1 : α), neg_add, neg_add_cancel_right, ← neg_add, hk] @[simp] lemma odd_neg : Odd (-a) ↔ Odd a := ⟨fun h ↦ neg_neg a ▸ h.neg, Odd.neg⟩ lemma odd_neg_one : Odd (-1 : α) := by simp lemma Odd.sub_even (ha : Odd a) (hb : Even b) : Odd (a - b) := by rw [sub_eq_add_neg]; exact ha.add_even hb.neg lemma Even.sub_odd (ha : Even a) (hb : Odd b) : Odd (a - b) := by rw [sub_eq_add_neg]; exact ha.add_odd hb.neg lemma Odd.sub_odd (ha : Odd a) (hb : Odd b) : Even (a - b) := by rw [sub_eq_add_neg]; exact ha.add_odd hb.neg end Ring namespace Nat variable {m n : ℕ} lemma odd_iff : Odd n ↔ n % 2 = 1 := ⟨fun ⟨m, hm⟩ ↦ by omega, fun h ↦ ⟨n / 2, (mod_add_div n 2).symm.trans (by rw [h, add_comm])⟩⟩ instance : DecidablePred (Odd : ℕ → Prop) := fun _ ↦ decidable_of_iff _ odd_iff.symm lemma not_odd_iff : ¬Odd n ↔ n % 2 = 0 := by rw [odd_iff, mod_two_not_eq_one] @[simp] lemma not_odd_iff_even : ¬Odd n ↔ Even n := by rw [not_odd_iff, even_iff] @[simp] lemma not_even_iff_odd : ¬Even n ↔ Odd n := by rw [not_even_iff, odd_iff] @[simp] lemma not_odd_zero : ¬Odd 0 := not_odd_iff.mpr rfl lemma _root_.Odd.not_two_dvd_nat (h : Odd n) : ¬(2 ∣ n) := by rwa [← even_iff_two_dvd, not_even_iff_odd] lemma even_xor_odd (n : ℕ) : Xor' (Even n) (Odd n) := by simp [Xor', ← not_even_iff_odd, Decidable.em (Even n)] lemma even_or_odd (n : ℕ) : Even n ∨ Odd n := (even_xor_odd n).or lemma even_or_odd' (n : ℕ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 := by simpa only [← two_mul, exists_or, Odd, Even] using even_or_odd n lemma even_xor_odd' (n : ℕ) : ∃ k, Xor' (n = 2 * k) (n = 2 * k + 1) := by obtain ⟨k, rfl⟩ | ⟨k, rfl⟩ := even_or_odd n <;> use k · simpa only [← two_mul, eq_self_iff_true, xor_true] using (succ_ne_self (2 * k)).symm · simpa only [xor_true, xor_comm] using (succ_ne_self _) lemma odd_add_one {n : ℕ} : Odd (n + 1) ↔ ¬ Odd n := by rw [← not_even_iff_odd, Nat.even_add_one, not_even_iff_odd] lemma mod_two_add_add_odd_mod_two (m : ℕ) {n : ℕ} (hn : Odd n) : m % 2 + (m + n) % 2 = 1 := ((even_or_odd m).elim fun hm ↦ by rw [even_iff.1 hm, odd_iff.1 (hm.add_odd hn)]) fun hm ↦ by rw [odd_iff.1 hm, even_iff.1 (hm.add_odd hn)] @[simp] lemma mod_two_add_succ_mod_two (m : ℕ) : m % 2 + (m + 1) % 2 = 1 := mod_two_add_add_odd_mod_two m odd_one @[simp] lemma succ_mod_two_add_mod_two (m : ℕ) : (m + 1) % 2 + m % 2 = 1 := by rw [add_comm, mod_two_add_succ_mod_two] lemma even_add' : Even (m + n) ↔ (Odd m ↔ Odd n) := by rw [even_add, ← not_odd_iff_even, ← not_odd_iff_even, not_iff_not] @[simp] lemma not_even_bit1 (n : ℕ) : ¬Even (2 * n + 1) := by simp [parity_simps] lemma not_even_two_mul_add_one (n : ℕ) : ¬ Even (2 * n + 1) := not_even_iff_odd.2 <| odd_two_mul_add_one n lemma even_sub' (h : n ≤ m) : Even (m - n) ↔ (Odd m ↔ Odd n) := by rw [even_sub h, ← not_odd_iff_even, ← not_odd_iff_even, not_iff_not] lemma Odd.sub_odd (hm : Odd m) (hn : Odd n) : Even (m - n) := (le_total n m).elim (fun h ↦ by simp only [even_sub' h, *]) fun h ↦ by simp only [Nat.sub_eq_zero_iff_le.2 h, Even.zero] alias _root_.Odd.tsub_odd := Nat.Odd.sub_odd lemma odd_mul : Odd (m * n) ↔ Odd m ∧ Odd n := by simp [not_or, even_mul, ← not_even_iff_odd] lemma Odd.of_mul_left (h : Odd (m * n)) : Odd m := (odd_mul.mp h).1 lemma Odd.of_mul_right (h : Odd (m * n)) : Odd n := (odd_mul.mp h).2 lemma even_div : Even (m / n) ↔ m % (2 * n) / n = 0 := by rw [even_iff_two_dvd, dvd_iff_mod_eq_zero, ← Nat.mod_mul_right_div_self, mul_comm] @[parity_simps] lemma odd_add : Odd (m + n) ↔ (Odd m ↔ Even n) := by rw [← not_even_iff_odd, even_add, not_iff, ← not_even_iff_odd] lemma odd_add' : Odd (m + n) ↔ (Odd n ↔ Even m) := by rw [add_comm, odd_add] lemma ne_of_odd_add (h : Odd (m + n)) : m ≠ n := by rintro rfl; simp [← not_even_iff_odd] at h @[parity_simps] lemma odd_sub (h : n ≤ m) : Odd (m - n) ↔ (Odd m ↔ Even n) := by rw [← not_even_iff_odd, even_sub h, not_iff, ← not_even_iff_odd] lemma Odd.sub_even (h : n ≤ m) (hm : Odd m) (hn : Even n) : Odd (m - n) := (odd_sub h).mpr <| iff_of_true hm hn lemma odd_sub' (h : n ≤ m) : Odd (m - n) ↔ (Odd n ↔ Even m) := by rw [← not_even_iff_odd, even_sub h, not_iff, not_iff_comm, ← not_even_iff_odd] lemma Even.sub_odd (h : n ≤ m) (hm : Even m) (hn : Odd n) : Odd (m - n) := (odd_sub' h).mpr <| iff_of_true hn hm lemma two_mul_div_two_add_one_of_odd (h : Odd n) : 2 * (n / 2) + 1 = n := by rw [← odd_iff.mp h, div_add_mod] lemma div_two_mul_two_add_one_of_odd (h : Odd n) : n / 2 * 2 + 1 = n := by rw [← odd_iff.mp h, div_add_mod'] lemma one_add_div_two_mul_two_of_odd (h : Odd n) : 1 + n / 2 * 2 = n := by rw [← odd_iff.mp h, mod_add_div'] -- Here are examples of how `parity_simps` can be used with `Nat`. example (m n : ℕ) (h : Even m) : ¬Even (n + 3) ↔ Even (m ^ 2 + m + n) := by simp [*, two_ne_zero, parity_simps] example : ¬Even 25394535 := by decide end Nat open Nat namespace Function namespace Involutive variable {α : Type*} {f : α → α} {n : ℕ} section lemma iterate_bit0 (hf : Involutive f) (n : ℕ) : f^[2 * n] = id := by rw [iterate_mul, involutive_iff_iter_2_eq_id.1 hf, iterate_id] lemma iterate_bit1 (hf : Involutive f) (n : ℕ) : f^[2 * n + 1] = f := by rw [← succ_eq_add_one, iterate_succ, hf.iterate_bit0, id_comp] end lemma iterate_two_mul (hf : Involutive f) (n : ℕ) : f^[2 * n] = id := by rw [iterate_mul, involutive_iff_iter_2_eq_id.1 hf, iterate_id] lemma iterate_even (hf : Involutive f) (hn : Even n) : f^[n] = id := by obtain ⟨m, rfl⟩ := hn rw [← two_mul, hf.iterate_two_mul] lemma iterate_odd (hf : Involutive f) (hn : Odd n) : f^[n] = f := by obtain ⟨m, rfl⟩ := hn rw [iterate_add, hf.iterate_two_mul, id_comp, iterate_one] lemma iterate_eq_self (hf : Involutive f) (hne : f ≠ id) : f^[n] = f ↔ Odd n := ⟨fun H ↦ not_even_iff_odd.1 fun hn ↦ hne <| by rwa [hf.iterate_even hn, eq_comm] at H, hf.iterate_odd⟩ lemma iterate_eq_id (hf : Involutive f) (hne : f ≠ id) : f^[n] = id ↔ Even n :=
⟨fun H ↦ not_odd_iff_even.1 fun hn ↦ hne <| by rwa [hf.iterate_odd hn] at H, hf.iterate_even⟩
Mathlib/Algebra/Ring/Parity.lean
337
338
/- Copyright (c) 2024 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import Mathlib.CategoryTheory.GradedObject /-! # The graded object in a single degree In this file, we define the functor `GradedObject.single j : C ⥤ GradedObject J C` which sends an object `X : C` to the graded object which is `X` in degree `j` and the initial object of `C` in other degrees. -/ namespace CategoryTheory open Limits namespace GradedObject variable {J : Type*} {C : Type*} [Category C] [HasInitial C] [DecidableEq J] /-- The functor which sends `X : C` to the graded object which is `X` in degree `j` and the initial object in other degrees. -/ noncomputable def single (j : J) : C ⥤ GradedObject J C where obj X i := if i = j then X else ⊥_ C map {X₁ X₂} f i := if h : i = j then eqToHom (if_pos h) ≫ f ≫ eqToHom (if_pos h).symm else eqToHom (by dsimp; rw [if_neg h, if_neg h]) variable (J) in /-- The functor which sends `X : C` to the graded object which is `X` in degree `0` and the initial object in nonzero degrees. -/ noncomputable abbrev single₀ [Zero J] : C ⥤ GradedObject J C := single 0 /-- The canonical isomorphism `(single j).obj X i ≅ X` when `i = j`. -/ noncomputable def singleObjApplyIsoOfEq (j : J) (X : C) (i : J) (h : i = j) : (single j).obj X i ≅ X := eqToIso (if_pos h) /-- The canonical isomorphism `(single j).obj X j ≅ X`. -/ noncomputable abbrev singleObjApplyIso (j : J) (X : C) : (single j).obj X j ≅ X := singleObjApplyIsoOfEq j X j rfl /-- The object `(single j).obj X i` is initial when `i ≠ j`. -/ noncomputable def isInitialSingleObjApply (j : J) (X : C) (i : J) (h : i ≠ j) : IsInitial ((single j).obj X i) := by dsimp [single] rw [if_neg h] exact initialIsInitial lemma singleObjApplyIsoOfEq_inv_single_map (j : J) {X Y : C} (f : X ⟶ Y) (i : J) (h : i = j) : (singleObjApplyIsoOfEq j X i h).inv ≫ (single j).map f i = f ≫ (singleObjApplyIsoOfEq j Y i h).inv := by subst h simp [singleObjApplyIsoOfEq, single] lemma single_map_singleObjApplyIsoOfEq_hom (j : J) {X Y : C} (f : X ⟶ Y) (i : J) (h : i = j) : (single j).map f i ≫ (singleObjApplyIsoOfEq j Y i h).hom = (singleObjApplyIsoOfEq j X i h).hom ≫ f := by subst h simp [singleObjApplyIsoOfEq, single]
@[reassoc (attr := simp)] lemma singleObjApplyIso_inv_single_map (j : J) {X Y : C} (f : X ⟶ Y) : (singleObjApplyIso j X).inv ≫ (single j).map f j = f ≫ (singleObjApplyIso j Y).inv := by apply singleObjApplyIsoOfEq_inv_single_map
Mathlib/CategoryTheory/GradedObject/Single.lean
65
68
/- 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.Order.Interval.Set.Basic import Mathlib.Order.Hom.Set /-! # Lemmas about images of intervals under order isomorphisms. -/ open Set namespace OrderIso section Preorder variable {α β : Type*} [Preorder α] [Preorder β] @[simp] theorem preimage_Iic (e : α ≃o β) (b : β) : e ⁻¹' Iic b = Iic (e.symm b) := by ext x simp [← e.le_iff_le] @[simp] theorem preimage_Ici (e : α ≃o β) (b : β) : e ⁻¹' Ici b = Ici (e.symm b) := by ext x simp [← e.le_iff_le] @[simp] theorem preimage_Iio (e : α ≃o β) (b : β) : e ⁻¹' Iio b = Iio (e.symm b) := by ext x simp [← e.lt_iff_lt] @[simp] theorem preimage_Ioi (e : α ≃o β) (b : β) : e ⁻¹' Ioi b = Ioi (e.symm b) := by ext x simp [← e.lt_iff_lt] @[simp] theorem preimage_Icc (e : α ≃o β) (a b : β) : e ⁻¹' Icc a b = Icc (e.symm a) (e.symm b) := by simp [← Ici_inter_Iic] @[simp] theorem preimage_Ico (e : α ≃o β) (a b : β) : e ⁻¹' Ico a b = Ico (e.symm a) (e.symm b) := by simp [← Ici_inter_Iio]
@[simp]
Mathlib/Order/Interval/Set/OrderIso.lean
48
49
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Johannes Hölzl, Rémy Degenne -/ import Mathlib.Order.ConditionallyCompleteLattice.Indexed import Mathlib.Order.Filter.IsBounded import Mathlib.Order.Hom.CompleteLattice /-! # liminfs and limsups of functions and filters Defines the liminf/limsup of a function taking values in a conditionally complete lattice, with respect to an arbitrary filter. We define `limsSup f` (`limsInf f`) where `f` is a filter taking values in a conditionally complete lattice. `limsSup f` is the smallest element `a` such that, eventually, `u ≤ a` (and vice versa for `limsInf f`). To work with the Limsup along a function `u` use `limsSup (map u f)`. Usually, one defines the Limsup as `inf (sup s)` where the Inf is taken over all sets in the filter. For instance, in ℕ along a function `u`, this is `inf_n (sup_{k ≥ n} u k)` (and the latter quantity decreases with `n`, so this is in fact a limit.). There is however a difficulty: it is well possible that `u` is not bounded on the whole space, only eventually (think of `limsup (fun x ↦ 1/x)` on ℝ. Then there is no guarantee that the quantity above really decreases (the value of the `sup` beforehand is not really well defined, as one can not use ∞), so that the Inf could be anything. So one can not use this `inf sup ...` definition in conditionally complete lattices, and one has to use a less tractable definition. In conditionally complete lattices, the definition is only useful for filters which are eventually bounded above (otherwise, the Limsup would morally be +∞, which does not belong to the space) and which are frequently bounded below (otherwise, the Limsup would morally be -∞, which is not in the space either). We start with definitions of these concepts for arbitrary filters, before turning to the definitions of Limsup and Liminf. In complete lattices, however, it coincides with the `Inf Sup` definition. -/ open Filter Set Function variable {α β γ ι ι' : Type*} namespace Filter section ConditionallyCompleteLattice variable [ConditionallyCompleteLattice α] {s : Set α} {u : β → α} /-- The `limsSup` of a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `x ≤ a`. -/ def limsSup (f : Filter α) : α := sInf { a | ∀ᶠ n in f, n ≤ a } /-- The `limsInf` of a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `x ≥ a`. -/ def limsInf (f : Filter α) : α := sSup { a | ∀ᶠ n in f, a ≤ n } /-- The `limsup` of a function `u` along a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `u x ≤ a`. -/ def limsup (u : β → α) (f : Filter β) : α := limsSup (map u f) /-- The `liminf` of a function `u` along a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `u x ≥ a`. -/ def liminf (u : β → α) (f : Filter β) : α := limsInf (map u f) /-- The `blimsup` of a function `u` along a filter `f`, bounded by a predicate `p`, is the infimum of the `a` such that, eventually for `f`, `u x ≤ a` whenever `p x` holds. -/ def blimsup (u : β → α) (f : Filter β) (p : β → Prop) := sInf { a | ∀ᶠ x in f, p x → u x ≤ a } /-- The `bliminf` of a function `u` along a filter `f`, bounded by a predicate `p`, is the supremum of the `a` such that, eventually for `f`, `a ≤ u x` whenever `p x` holds. -/ def bliminf (u : β → α) (f : Filter β) (p : β → Prop) := sSup { a | ∀ᶠ x in f, p x → a ≤ u x } section variable {f : Filter β} {u : β → α} {p : β → Prop} theorem limsup_eq : limsup u f = sInf { a | ∀ᶠ n in f, u n ≤ a } := rfl theorem liminf_eq : liminf u f = sSup { a | ∀ᶠ n in f, a ≤ u n } := rfl theorem blimsup_eq : blimsup u f p = sInf { a | ∀ᶠ x in f, p x → u x ≤ a } := rfl theorem bliminf_eq : bliminf u f p = sSup { a | ∀ᶠ x in f, p x → a ≤ u x } := rfl lemma liminf_comp (u : β → α) (v : γ → β) (f : Filter γ) : liminf (u ∘ v) f = liminf u (map v f) := rfl lemma limsup_comp (u : β → α) (v : γ → β) (f : Filter γ) : limsup (u ∘ v) f = limsup u (map v f) := rfl end @[simp] theorem blimsup_true (f : Filter β) (u : β → α) : (blimsup u f fun _ => True) = limsup u f := by simp [blimsup_eq, limsup_eq] @[simp] theorem bliminf_true (f : Filter β) (u : β → α) : (bliminf u f fun _ => True) = liminf u f := by simp [bliminf_eq, liminf_eq] lemma blimsup_eq_limsup {f : Filter β} {u : β → α} {p : β → Prop} : blimsup u f p = limsup u (f ⊓ 𝓟 {x | p x}) := by simp only [blimsup_eq, limsup_eq, eventually_inf_principal, mem_setOf_eq] lemma bliminf_eq_liminf {f : Filter β} {u : β → α} {p : β → Prop} : bliminf u f p = liminf u (f ⊓ 𝓟 {x | p x}) := blimsup_eq_limsup (α := αᵒᵈ) theorem blimsup_eq_limsup_subtype {f : Filter β} {u : β → α} {p : β → Prop} : blimsup u f p = limsup (u ∘ ((↑) : { x | p x } → β)) (comap (↑) f) := by rw [blimsup_eq_limsup, limsup, limsup, ← map_map, map_comap_setCoe_val] theorem bliminf_eq_liminf_subtype {f : Filter β} {u : β → α} {p : β → Prop} : bliminf u f p = liminf (u ∘ ((↑) : { x | p x } → β)) (comap (↑) f) := blimsup_eq_limsup_subtype (α := αᵒᵈ) theorem limsSup_le_of_le {f : Filter α} {a} (hf : f.IsCobounded (· ≤ ·) := by isBoundedDefault) (h : ∀ᶠ n in f, n ≤ a) : limsSup f ≤ a := csInf_le hf h theorem le_limsInf_of_le {f : Filter α} {a} (hf : f.IsCobounded (· ≥ ·) := by isBoundedDefault) (h : ∀ᶠ n in f, a ≤ n) : a ≤ limsInf f := le_csSup hf h theorem limsup_le_of_le {f : Filter β} {u : β → α} {a} (hf : f.IsCoboundedUnder (· ≤ ·) u := by isBoundedDefault) (h : ∀ᶠ n in f, u n ≤ a) : limsup u f ≤ a := csInf_le hf h theorem le_liminf_of_le {f : Filter β} {u : β → α} {a} (hf : f.IsCoboundedUnder (· ≥ ·) u := by isBoundedDefault) (h : ∀ᶠ n in f, a ≤ u n) : a ≤ liminf u f := le_csSup hf h theorem le_limsSup_of_le {f : Filter α} {a} (hf : f.IsBounded (· ≤ ·) := by isBoundedDefault) (h : ∀ b, (∀ᶠ n in f, n ≤ b) → a ≤ b) : a ≤ limsSup f := le_csInf hf h theorem limsInf_le_of_le {f : Filter α} {a} (hf : f.IsBounded (· ≥ ·) := by isBoundedDefault) (h : ∀ b, (∀ᶠ n in f, b ≤ n) → b ≤ a) : limsInf f ≤ a := csSup_le hf h theorem le_limsup_of_le {f : Filter β} {u : β → α} {a} (hf : f.IsBoundedUnder (· ≤ ·) u := by isBoundedDefault) (h : ∀ b, (∀ᶠ n in f, u n ≤ b) → a ≤ b) : a ≤ limsup u f := le_csInf hf h theorem liminf_le_of_le {f : Filter β} {u : β → α} {a} (hf : f.IsBoundedUnder (· ≥ ·) u := by isBoundedDefault) (h : ∀ b, (∀ᶠ n in f, b ≤ u n) → b ≤ a) : liminf u f ≤ a := csSup_le hf h theorem limsInf_le_limsSup {f : Filter α} [NeBot f] (h₁ : f.IsBounded (· ≤ ·) := by isBoundedDefault) (h₂ : f.IsBounded (· ≥ ·) := by isBoundedDefault) : limsInf f ≤ limsSup f := liminf_le_of_le h₂ fun a₀ ha₀ => le_limsup_of_le h₁ fun a₁ ha₁ => show a₀ ≤ a₁ from let ⟨_, hb₀, hb₁⟩ := (ha₀.and ha₁).exists le_trans hb₀ hb₁ theorem liminf_le_limsup {f : Filter β} [NeBot f] {u : β → α} (h : f.IsBoundedUnder (· ≤ ·) u := by isBoundedDefault) (h' : f.IsBoundedUnder (· ≥ ·) u := by isBoundedDefault) : liminf u f ≤ limsup u f := limsInf_le_limsSup h h' theorem limsSup_le_limsSup {f g : Filter α} (hf : f.IsCobounded (· ≤ ·) := by isBoundedDefault) (hg : g.IsBounded (· ≤ ·) := by isBoundedDefault) (h : ∀ a, (∀ᶠ n in g, n ≤ a) → ∀ᶠ n in f, n ≤ a) : limsSup f ≤ limsSup g := csInf_le_csInf hf hg h theorem limsInf_le_limsInf {f g : Filter α} (hf : f.IsBounded (· ≥ ·) := by isBoundedDefault) (hg : g.IsCobounded (· ≥ ·) := by isBoundedDefault) (h : ∀ a, (∀ᶠ n in f, a ≤ n) → ∀ᶠ n in g, a ≤ n) : limsInf f ≤ limsInf g := csSup_le_csSup hg hf h theorem limsup_le_limsup {α : Type*} [ConditionallyCompleteLattice β] {f : Filter α} {u v : α → β} (h : u ≤ᶠ[f] v) (hu : f.IsCoboundedUnder (· ≤ ·) u := by isBoundedDefault) (hv : f.IsBoundedUnder (· ≤ ·) v := by isBoundedDefault) : limsup u f ≤ limsup v f := limsSup_le_limsSup hu hv fun _ => h.trans theorem liminf_le_liminf {α : Type*} [ConditionallyCompleteLattice β] {f : Filter α} {u v : α → β} (h : ∀ᶠ a in f, u a ≤ v a) (hu : f.IsBoundedUnder (· ≥ ·) u := by isBoundedDefault) (hv : f.IsCoboundedUnder (· ≥ ·) v := by isBoundedDefault) : liminf u f ≤ liminf v f := limsup_le_limsup (β := βᵒᵈ) h hv hu theorem limsSup_le_limsSup_of_le {f g : Filter α} (h : f ≤ g) (hf : f.IsCobounded (· ≤ ·) := by isBoundedDefault) (hg : g.IsBounded (· ≤ ·) := by isBoundedDefault) : limsSup f ≤ limsSup g := limsSup_le_limsSup hf hg fun _ ha => h ha theorem limsInf_le_limsInf_of_le {f g : Filter α} (h : g ≤ f) (hf : f.IsBounded (· ≥ ·) := by isBoundedDefault) (hg : g.IsCobounded (· ≥ ·) := by isBoundedDefault) : limsInf f ≤ limsInf g := limsInf_le_limsInf hf hg fun _ ha => h ha theorem limsup_le_limsup_of_le {α β} [ConditionallyCompleteLattice β] {f g : Filter α} (h : f ≤ g) {u : α → β} (hf : f.IsCoboundedUnder (· ≤ ·) u := by isBoundedDefault) (hg : g.IsBoundedUnder (· ≤ ·) u := by isBoundedDefault) : limsup u f ≤ limsup u g := limsSup_le_limsSup_of_le (map_mono h) hf hg theorem liminf_le_liminf_of_le {α β} [ConditionallyCompleteLattice β] {f g : Filter α} (h : g ≤ f) {u : α → β} (hf : f.IsBoundedUnder (· ≥ ·) u := by isBoundedDefault) (hg : g.IsCoboundedUnder (· ≥ ·) u := by isBoundedDefault) : liminf u f ≤ liminf u g := limsInf_le_limsInf_of_le (map_mono h) hf hg lemma limsSup_principal_eq_csSup (h : BddAbove s) (hs : s.Nonempty) : limsSup (𝓟 s) = sSup s := by simp only [limsSup, eventually_principal]; exact csInf_upperBounds_eq_csSup h hs lemma limsInf_principal_eq_csSup (h : BddBelow s) (hs : s.Nonempty) : limsInf (𝓟 s) = sInf s := limsSup_principal_eq_csSup (α := αᵒᵈ) h hs lemma limsup_top_eq_ciSup [Nonempty β] (hu : BddAbove (range u)) : limsup u ⊤ = ⨆ i, u i := by rw [limsup, map_top, limsSup_principal_eq_csSup hu (range_nonempty _), sSup_range] lemma liminf_top_eq_ciInf [Nonempty β] (hu : BddBelow (range u)) : liminf u ⊤ = ⨅ i, u i := by rw [liminf, map_top, limsInf_principal_eq_csSup hu (range_nonempty _), sInf_range] theorem limsup_congr {α : Type*} [ConditionallyCompleteLattice β] {f : Filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : limsup u f = limsup v f := by rw [limsup_eq] congr with b exact eventually_congr (h.mono fun x hx => by simp [hx]) theorem blimsup_congr {f : Filter β} {u v : β → α} {p : β → Prop} (h : ∀ᶠ a in f, p a → u a = v a) : blimsup u f p = blimsup v f p := by simpa only [blimsup_eq_limsup] using limsup_congr <| eventually_inf_principal.2 h theorem bliminf_congr {f : Filter β} {u v : β → α} {p : β → Prop} (h : ∀ᶠ a in f, p a → u a = v a) : bliminf u f p = bliminf v f p := blimsup_congr (α := αᵒᵈ) h theorem liminf_congr {α : Type*} [ConditionallyCompleteLattice β] {f : Filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : liminf u f = liminf v f := limsup_congr (β := βᵒᵈ) h @[simp] theorem limsup_const {α : Type*} [ConditionallyCompleteLattice β] {f : Filter α} [NeBot f] (b : β) : limsup (fun _ => b) f = b := by simpa only [limsup_eq, eventually_const] using csInf_Ici @[simp] theorem liminf_const {α : Type*} [ConditionallyCompleteLattice β] {f : Filter α} [NeBot f] (b : β) : liminf (fun _ => b) f = b := limsup_const (β := βᵒᵈ) b theorem HasBasis.liminf_eq_sSup_iUnion_iInter {ι ι' : Type*} {f : ι → α} {v : Filter ι} {p : ι' → Prop} {s : ι' → Set ι} (hv : v.HasBasis p s) : liminf f v = sSup (⋃ (j : Subtype p), ⋂ (i : s j), Iic (f i)) := by simp_rw [liminf_eq, hv.eventually_iff] congr ext x simp only [mem_setOf_eq, iInter_coe_set, mem_iUnion, mem_iInter, mem_Iic, Subtype.exists, exists_prop] theorem HasBasis.liminf_eq_sSup_univ_of_empty {f : ι → α} {v : Filter ι} {p : ι' → Prop} {s : ι' → Set ι} (hv : v.HasBasis p s) (i : ι') (hi : p i) (h'i : s i = ∅) : liminf f v = sSup univ := by simp [hv.eq_bot_iff.2 ⟨i, hi, h'i⟩, liminf_eq] theorem HasBasis.limsup_eq_sInf_iUnion_iInter {ι ι' : Type*} {f : ι → α} {v : Filter ι} {p : ι' → Prop} {s : ι' → Set ι} (hv : v.HasBasis p s) : limsup f v = sInf (⋃ (j : Subtype p), ⋂ (i : s j), Ici (f i)) := HasBasis.liminf_eq_sSup_iUnion_iInter (α := αᵒᵈ) hv theorem HasBasis.limsup_eq_sInf_univ_of_empty {f : ι → α} {v : Filter ι} {p : ι' → Prop} {s : ι' → Set ι} (hv : v.HasBasis p s) (i : ι') (hi : p i) (h'i : s i = ∅) : limsup f v = sInf univ := HasBasis.liminf_eq_sSup_univ_of_empty (α := αᵒᵈ) hv i hi h'i @[simp] theorem liminf_nat_add (f : ℕ → α) (k : ℕ) : liminf (fun i => f (i + k)) atTop = liminf f atTop := by rw [← Function.comp_def, liminf, liminf, ← map_map, map_add_atTop_eq_nat] @[simp] theorem limsup_nat_add (f : ℕ → α) (k : ℕ) : limsup (fun i => f (i + k)) atTop = limsup f atTop := @liminf_nat_add αᵒᵈ _ f k end ConditionallyCompleteLattice section CompleteLattice variable [CompleteLattice α] @[simp] theorem limsSup_bot : limsSup (⊥ : Filter α) = ⊥ := bot_unique <| sInf_le <| by simp @[simp] theorem limsup_bot (f : β → α) : limsup f ⊥ = ⊥ := by simp [limsup] @[simp] theorem limsInf_bot : limsInf (⊥ : Filter α) = ⊤ := top_unique <| le_sSup <| by simp @[simp] theorem liminf_bot (f : β → α) : liminf f ⊥ = ⊤ := by simp [liminf] @[simp] theorem limsSup_top : limsSup (⊤ : Filter α) = ⊤ := top_unique <| le_sInf <| by simpa [eq_univ_iff_forall] using fun b hb => top_unique <| hb _ @[simp] theorem limsInf_top : limsInf (⊤ : Filter α) = ⊥ := bot_unique <| sSup_le <| by simpa [eq_univ_iff_forall] using fun b hb => bot_unique <| hb _ @[simp] theorem blimsup_false {f : Filter β} {u : β → α} : (blimsup u f fun _ => False) = ⊥ := by simp [blimsup_eq] @[simp] theorem bliminf_false {f : Filter β} {u : β → α} : (bliminf u f fun _ => False) = ⊤ := by simp [bliminf_eq] /-- Same as limsup_const applied to `⊥` but without the `NeBot f` assumption -/ @[simp] theorem limsup_const_bot {f : Filter β} : limsup (fun _ : β => (⊥ : α)) f = (⊥ : α) := by rw [limsup_eq, eq_bot_iff] exact sInf_le (Eventually.of_forall fun _ => le_rfl) /-- Same as limsup_const applied to `⊤` but without the `NeBot f` assumption -/ @[simp] theorem liminf_const_top {f : Filter β} : liminf (fun _ : β => (⊤ : α)) f = (⊤ : α) := limsup_const_bot (α := αᵒᵈ) theorem HasBasis.limsSup_eq_iInf_sSup {ι} {p : ι → Prop} {s} {f : Filter α} (h : f.HasBasis p s) : limsSup f = ⨅ (i) (_ : p i), sSup (s i) := le_antisymm (le_iInf₂ fun i hi => sInf_le <| h.eventually_iff.2 ⟨i, hi, fun _ => le_sSup⟩) (le_sInf fun _ ha => let ⟨_, hi, ha⟩ := h.eventually_iff.1 ha iInf₂_le_of_le _ hi <| sSup_le ha) theorem HasBasis.limsInf_eq_iSup_sInf {p : ι → Prop} {s : ι → Set α} {f : Filter α} (h : f.HasBasis p s) : limsInf f = ⨆ (i) (_ : p i), sInf (s i) := HasBasis.limsSup_eq_iInf_sSup (α := αᵒᵈ) h theorem limsSup_eq_iInf_sSup {f : Filter α} : limsSup f = ⨅ s ∈ f, sSup s := f.basis_sets.limsSup_eq_iInf_sSup theorem limsInf_eq_iSup_sInf {f : Filter α} : limsInf f = ⨆ s ∈ f, sInf s := limsSup_eq_iInf_sSup (α := αᵒᵈ)
theorem limsup_le_iSup {f : Filter β} {u : β → α} : limsup u f ≤ ⨆ n, u n := limsup_le_of_le (by isBoundedDefault) (Eventually.of_forall (le_iSup u)) theorem iInf_le_liminf {f : Filter β} {u : β → α} : ⨅ n, u n ≤ liminf u f := le_liminf_of_le (by isBoundedDefault) (Eventually.of_forall (iInf_le u))
Mathlib/Order/LiminfLimsup.lean
368
373
/- 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.Topology.Sets.Opens import Mathlib.Topology.Clopen /-! # Closed sets We define a few types of closed sets in a topological space. ## Main Definitions For a topological space `α`, * `TopologicalSpace.Closeds α`: The type of closed sets. * `TopologicalSpace.Clopens α`: The type of clopen sets. -/ open Order OrderDual Set variable {ι α β : Type*} [TopologicalSpace α] [TopologicalSpace β] namespace TopologicalSpace /-! ### Closed sets -/ /-- The type of closed subsets of a topological space. -/ structure Closeds (α : Type*) [TopologicalSpace α] where /-- the carrier set, i.e. the points in this set -/ carrier : Set α isClosed' : IsClosed carrier namespace Closeds instance : SetLike (Closeds α) α where coe := Closeds.carrier coe_injective' s t h := by cases s; cases t; congr instance : CanLift (Set α) (Closeds α) (↑) IsClosed where prf s hs := ⟨⟨s, hs⟩, rfl⟩ theorem isClosed (s : Closeds α) : IsClosed (s : Set α) := s.isClosed' @[deprecated (since := "2025-04-20")] alias closed := isClosed /-- See Note [custom simps projection]. -/ def Simps.coe (s : Closeds α) : Set α := s initialize_simps_projections Closeds (carrier → coe, as_prefix coe) @[ext] protected theorem ext {s t : Closeds α} (h : (s : Set α) = t) : s = t := SetLike.ext' h @[simp] theorem coe_mk (s : Set α) (h) : (mk s h : Set α) = s := rfl /-- The closure of a set, as an element of `TopologicalSpace.Closeds`. -/ @[simps] protected def closure (s : Set α) : Closeds α := ⟨closure s, isClosed_closure⟩ @[simp] theorem mem_closure {s : Set α} {x : α} : x ∈ Closeds.closure s ↔ x ∈ closure s := .rfl theorem gc : GaloisConnection Closeds.closure ((↑) : Closeds α → Set α) := fun _ U => ⟨subset_closure.trans, fun h => closure_minimal h U.isClosed⟩ /-- The galois insertion between sets and closeds. -/ def gi : GaloisInsertion (@Closeds.closure α _) (↑) where choice s hs := ⟨s, closure_eq_iff_isClosed.1 <| hs.antisymm subset_closure⟩ gc := gc le_l_u _ := subset_closure choice_eq _s hs := SetLike.coe_injective <| subset_closure.antisymm hs instance instCompleteLattice : CompleteLattice (Closeds α) := CompleteLattice.copy (GaloisInsertion.liftCompleteLattice gi) -- le _ rfl -- top ⟨univ, isClosed_univ⟩ rfl -- bot ⟨∅, isClosed_empty⟩ (SetLike.coe_injective closure_empty.symm) -- sup (fun s t => ⟨s ∪ t, s.2.union t.2⟩) (funext fun s => funext fun t => SetLike.coe_injective (s.2.union t.2).closure_eq.symm) -- inf (fun s t => ⟨s ∩ t, s.2.inter t.2⟩) rfl -- sSup _ rfl -- sInf (fun S => ⟨⋂ s ∈ S, ↑s, isClosed_biInter fun s _ => s.2⟩) (funext fun _ => SetLike.coe_injective sInf_image.symm) /-- The type of closed sets is inhabited, with default element the empty set. -/ instance : Inhabited (Closeds α) := ⟨⊥⟩ @[simp, norm_cast] theorem coe_sup (s t : Closeds α) : (↑(s ⊔ t) : Set α) = ↑s ∪ ↑t := by rfl @[simp, norm_cast] theorem coe_inf (s t : Closeds α) : (↑(s ⊓ t) : Set α) = ↑s ∩ ↑t := rfl @[simp, norm_cast] theorem coe_top : (↑(⊤ : Closeds α) : Set α) = univ := rfl @[simp, norm_cast] theorem coe_eq_univ {s : Closeds α} : (s : Set α) = univ ↔ s = ⊤ := SetLike.coe_injective.eq_iff' rfl @[simp, norm_cast] theorem coe_bot : (↑(⊥ : Closeds α) : Set α) = ∅ := rfl @[simp, norm_cast] theorem coe_eq_empty {s : Closeds α} : (s : Set α) = ∅ ↔ s = ⊥ := SetLike.coe_injective.eq_iff' rfl theorem coe_nonempty {s : Closeds α} : (s : Set α).Nonempty ↔ s ≠ ⊥ := nonempty_iff_ne_empty.trans coe_eq_empty.not @[simp, norm_cast] theorem coe_sInf {S : Set (Closeds α)} : (↑(sInf S) : Set α) = ⋂ i ∈ S, ↑i := rfl @[simp] lemma coe_sSup {S : Set (Closeds α)} : ((sSup S : Closeds α) : Set α) = closure (⋃₀ ((↑) '' S)) := by rfl @[simp, norm_cast] theorem coe_finset_sup (f : ι → Closeds α) (s : Finset ι) : (↑(s.sup f) : Set α) = s.sup ((↑) ∘ f) := map_finset_sup (⟨⟨(↑), coe_sup⟩, coe_bot⟩ : SupBotHom (Closeds α) (Set α)) _ _ @[simp, norm_cast] theorem coe_finset_inf (f : ι → Closeds α) (s : Finset ι) : (↑(s.inf f) : Set α) = s.inf ((↑) ∘ f) := map_finset_inf (⟨⟨(↑), coe_inf⟩, coe_top⟩ : InfTopHom (Closeds α) (Set α)) _ _ @[simp] theorem mem_sInf {S : Set (Closeds α)} {x : α} : x ∈ sInf S ↔ ∀ s ∈ S, x ∈ s := mem_iInter₂ @[simp] theorem mem_iInf {ι} {x : α} {s : ι → Closeds α} : x ∈ iInf s ↔ ∀ i, x ∈ s i := by simp [iInf] @[simp, norm_cast] theorem coe_iInf {ι} (s : ι → Closeds α) : ((⨅ i, s i : Closeds α) : Set α) = ⋂ i, s i := by ext; simp theorem iInf_def {ι} (s : ι → Closeds α) : ⨅ i, s i = ⟨⋂ i, s i, isClosed_iInter fun i => (s i).2⟩ := by ext1; simp @[simp] theorem iInf_mk {ι} (s : ι → Set α) (h : ∀ i, IsClosed (s i)) : (⨅ i, ⟨s i, h i⟩ : Closeds α) = ⟨⋂ i, s i, isClosed_iInter h⟩ := iInf_def _
Mathlib/Topology/Sets/Closeds.lean
168
168
/- Copyright (c) 2022 Alex J. Best. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Yaël Dillies -/ import Mathlib.Algebra.Order.Archimedean.Hom import Mathlib.Algebra.Order.Group.Pointwise.CompleteLattice /-! # Conditionally complete linear ordered fields This file shows that the reals are unique, or, more formally, given a type satisfying the common axioms of the reals (field, conditionally complete, linearly ordered) that there is an isomorphism preserving these properties to the reals. This is `LinearOrderedField.inducedOrderRingIso` for `ℚ`. Moreover this isomorphism is unique. We introduce definitions of conditionally complete linear ordered fields, and show all such are archimedean. We also construct the natural map from a `LinearOrderedField` to such a field. ## Main definitions * `ConditionallyCompleteLinearOrderedField`: A field satisfying the standard axiomatization of the real numbers, being a Dedekind complete and linear ordered field. * `LinearOrderedField.inducedMap`: A (unique) map from any archimedean linear ordered field to a conditionally complete linear ordered field. Various bundlings are available. ## Main results * `LinearOrderedField.uniqueOrderRingHom` : Uniqueness of `OrderRingHom`s from an archimedean linear ordered field to a conditionally complete linear ordered field. * `LinearOrderedField.uniqueOrderRingIso` : Uniqueness of `OrderRingIso`s between two conditionally complete linearly ordered fields. ## References * https://mathoverflow.net/questions/362991/ who-first-characterized-the-real-numbers-as-the-unique-complete-ordered-field ## Tags reals, conditionally complete, ordered field, uniqueness -/ variable {F α β γ : Type*} noncomputable section open Function Rat Set open scoped Pointwise /-- A field which is both linearly ordered and conditionally complete with respect to the order. This axiomatizes the reals. -/ -- @[protect_proj] -- Porting note: does not exist anymore class ConditionallyCompleteLinearOrderedField (α : Type*) extends Field α, ConditionallyCompleteLinearOrder α where -- extends `IsStrictOrderedRing α` produces -- (kernel) declaration has free variables -- 'ConditionallyCompleteLinearOrderedField.toIsStrictOrderedRing' [toIsStrictOrderedRing : IsStrictOrderedRing α] attribute [instance] ConditionallyCompleteLinearOrderedField.toIsStrictOrderedRing -- see Note [lower instance priority] /-- Any conditionally complete linearly ordered field is archimedean. -/ instance (priority := 100) ConditionallyCompleteLinearOrderedField.to_archimedean [ConditionallyCompleteLinearOrderedField α] : Archimedean α := archimedean_iff_nat_lt.2 (by by_contra! h obtain ⟨x, h⟩ := h have := csSup_le (range_nonempty Nat.cast) (forall_mem_range.2 fun m => le_sub_iff_add_le.2 <| le_csSup ⟨x, forall_mem_range.2 h⟩ ⟨m+1, Nat.cast_succ m⟩) linarith) namespace LinearOrderedField /-! ### Rational cut map The idea is that a conditionally complete linear ordered field is fully characterized by its copy of the rationals. Hence we define `LinearOrderedField.cutMap β : α → Set β` which sends `a : α` to the "rationals in `β`" that are less than `a`. -/ section CutMap variable [Field α] [LinearOrder α] section DivisionRing variable (β) [DivisionRing β] {a a₁ a₂ : α} {b : β} {q : ℚ} /-- The lower cut of rationals inside a linear ordered field that are less than a given element of another linear ordered field. -/ def cutMap (a : α) : Set β := (Rat.cast : ℚ → β) '' {t | ↑t < a} theorem cutMap_mono (h : a₁ ≤ a₂) : cutMap β a₁ ⊆ cutMap β a₂ := image_subset _ fun _ => h.trans_lt' variable {β} @[simp] theorem mem_cutMap_iff : b ∈ cutMap β a ↔ ∃ q : ℚ, (q : α) < a ∧ (q : β) = b := Iff.rfl theorem coe_mem_cutMap_iff [CharZero β] : (q : β) ∈ cutMap β a ↔ (q : α) < a := Rat.cast_injective.mem_set_image theorem cutMap_self (a : α) : cutMap α a = Iio a ∩ range (Rat.cast : ℚ → α) := by ext constructor · rintro ⟨q, h, rfl⟩ exact ⟨h, q, rfl⟩ · rintro ⟨h, q, rfl⟩ exact ⟨q, h, rfl⟩ end DivisionRing variable (β) [IsStrictOrderedRing α] [Field β] [LinearOrder β] [IsStrictOrderedRing β] {a a₁ a₂ : α} {b : β} {q : ℚ} theorem cutMap_coe (q : ℚ) : cutMap β (q : α) = Rat.cast '' {r : ℚ | (r : β) < q} := by simp_rw [cutMap, Rat.cast_lt] variable [Archimedean α] omit [LinearOrder β] [IsStrictOrderedRing β] in theorem cutMap_nonempty (a : α) : (cutMap β a).Nonempty := Nonempty.image _ <| exists_rat_lt a theorem cutMap_bddAbove (a : α) : BddAbove (cutMap β a) := by obtain ⟨q, hq⟩ := exists_rat_gt a exact ⟨q, forall_mem_image.2 fun r hr => mod_cast (hq.trans' hr).le⟩ theorem cutMap_add (a b : α) : cutMap β (a + b) = cutMap β a + cutMap β b := by refine (image_subset_iff.2 fun q hq => ?_).antisymm ?_ · rw [mem_setOf_eq, ← sub_lt_iff_lt_add] at hq obtain ⟨q₁, hq₁q, hq₁ab⟩ := exists_rat_btwn hq refine ⟨q₁, by rwa [coe_mem_cutMap_iff], q - q₁, ?_, add_sub_cancel _ _⟩ norm_cast rw [coe_mem_cutMap_iff] exact mod_cast sub_lt_comm.mp hq₁q · rintro _ ⟨_, ⟨qa, ha, rfl⟩, _, ⟨qb, hb, rfl⟩, rfl⟩ -- After https://github.com/leanprover/lean4/pull/2734, `norm_cast` needs help with beta reduction. refine ⟨qa + qb, ?_, by beta_reduce; norm_cast⟩ rw [mem_setOf_eq, cast_add] exact add_lt_add ha hb end CutMap /-! ### Induced map `LinearOrderedField.cutMap` spits out a `Set β`. To get something in `β`, we now take the supremum. -/ section InducedMap variable (α β γ) [Field α] [LinearOrder α] [IsStrictOrderedRing α] [ConditionallyCompleteLinearOrderedField β] [ConditionallyCompleteLinearOrderedField γ] /-- The induced order preserving function from a linear ordered field to a conditionally complete linear ordered field, defined by taking the Sup in the codomain of all the rationals less than the input. -/ def inducedMap (x : α) : β := sSup <| cutMap β x variable [Archimedean α] theorem inducedMap_mono : Monotone (inducedMap α β) := fun _ _ h => csSup_le_csSup (cutMap_bddAbove β _) (cutMap_nonempty β _) (cutMap_mono β h) theorem inducedMap_rat (q : ℚ) : inducedMap α β (q : α) = q := by refine csSup_eq_of_forall_le_of_forall_lt_exists_gt (cutMap_nonempty β (q : α)) (fun x h => ?_) fun w h => ?_ · rw [cutMap_coe] at h obtain ⟨r, h, rfl⟩ := h exact le_of_lt h · obtain ⟨q', hwq, hq⟩ := exists_rat_btwn h rw [cutMap_coe] exact ⟨q', ⟨_, hq, rfl⟩, hwq⟩ @[simp] theorem inducedMap_zero : inducedMap α β 0 = 0 := mod_cast inducedMap_rat α β 0 @[simp] theorem inducedMap_one : inducedMap α β 1 = 1 := mod_cast inducedMap_rat α β 1 variable {α β} {a : α} {b : β} {q : ℚ} theorem inducedMap_nonneg (ha : 0 ≤ a) : 0 ≤ inducedMap α β a := (inducedMap_zero α _).ge.trans <| inducedMap_mono _ _ ha theorem coe_lt_inducedMap_iff : (q : β) < inducedMap α β a ↔ (q : α) < a := by refine ⟨fun h => ?_, fun hq => ?_⟩ · rw [← inducedMap_rat α] at h exact (inducedMap_mono α β).reflect_lt h · obtain ⟨q', hq, hqa⟩ := exists_rat_btwn hq apply lt_csSup_of_lt (cutMap_bddAbove β a) (coe_mem_cutMap_iff.mpr hqa) exact mod_cast hq theorem lt_inducedMap_iff : b < inducedMap α β a ↔ ∃ q : ℚ, b < q ∧ (q : α) < a := ⟨fun h => (exists_rat_btwn h).imp fun _ => And.imp_right coe_lt_inducedMap_iff.1, fun ⟨q, hbq, hqa⟩ => hbq.trans <| by rwa [coe_lt_inducedMap_iff]⟩ @[simp] theorem inducedMap_self (b : β) : inducedMap β β b = b := eq_of_forall_rat_lt_iff_lt fun _ => coe_lt_inducedMap_iff variable (α β) @[simp] theorem inducedMap_inducedMap (a : α) : inducedMap β γ (inducedMap α β a) = inducedMap α γ a := eq_of_forall_rat_lt_iff_lt fun q => by rw [coe_lt_inducedMap_iff, coe_lt_inducedMap_iff, Iff.comm, coe_lt_inducedMap_iff] theorem inducedMap_inv_self (b : β) : inducedMap γ β (inducedMap β γ b) = b := by rw [inducedMap_inducedMap, inducedMap_self] theorem inducedMap_add (x y : α) : inducedMap α β (x + y) = inducedMap α β x + inducedMap α β y := by rw [inducedMap, cutMap_add] exact csSup_add (cutMap_nonempty β x) (cutMap_bddAbove β x) (cutMap_nonempty β y) (cutMap_bddAbove β y) variable {α β} /-- Preparatory lemma for `inducedOrderRingHom`. -/ theorem le_inducedMap_mul_self_of_mem_cutMap (ha : 0 < a) (b : β) (hb : b ∈ cutMap β (a * a)) : b ≤ inducedMap α β a * inducedMap α β a := by obtain ⟨q, hb, rfl⟩ := hb obtain ⟨q', hq', hqq', hqa⟩ := exists_rat_pow_btwn two_ne_zero hb (mul_self_pos.2 ha.ne') trans (q' : β) ^ 2 · exact mod_cast hqq'.le · rw [pow_two] at hqa ⊢ exact mul_self_le_mul_self (mod_cast hq'.le) (le_csSup (cutMap_bddAbove β a) <| coe_mem_cutMap_iff.2 <| lt_of_mul_self_lt_mul_self₀ ha.le hqa) /-- Preparatory lemma for `inducedOrderRingHom`. -/ theorem exists_mem_cutMap_mul_self_of_lt_inducedMap_mul_self (ha : 0 < a) (b : β) (hba : b < inducedMap α β a * inducedMap α β a) : ∃ c ∈ cutMap β (a * a), b < c := by obtain hb | hb := lt_or_le b 0 · refine ⟨0, ?_, hb⟩ rw [← Rat.cast_zero, coe_mem_cutMap_iff, Rat.cast_zero] exact mul_self_pos.2 ha.ne' obtain ⟨q, hq, hbq, hqa⟩ := exists_rat_pow_btwn two_ne_zero hba (hb.trans_lt hba) rw [← cast_pow] at hbq refine ⟨(q ^ 2 : ℚ), coe_mem_cutMap_iff.2 ?_, hbq⟩ rw [pow_two] at hqa ⊢ push_cast obtain ⟨q', hq', hqa'⟩ := lt_inducedMap_iff.1 (lt_of_mul_self_lt_mul_self₀ (inducedMap_nonneg ha.le) hqa) exact mul_self_lt_mul_self (mod_cast hq.le) (hqa'.trans' <| by assumption_mod_cast)
variable (α β) /-- `inducedMap` as an additive homomorphism. -/ def inducedAddHom : α →+ β := ⟨⟨inducedMap α β, inducedMap_zero α β⟩, inducedMap_add α β⟩ /-- `inducedMap` as an `OrderRingHom`. -/ @[simps!] def inducedOrderRingHom : α →+*o β :=
Mathlib/Algebra/Order/CompleteField.lean
258
267
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yaël Dillies -/ import Mathlib.Algebra.Group.Action.Pointwise.Set.Basic import Mathlib.Algebra.GroupWithZero.Action.Defs import Mathlib.Algebra.Order.Group.OrderIso import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.Algebra.Ring.Defs import Mathlib.Order.Filter.AtTopBot.Map import Mathlib.Order.Filter.Finite import Mathlib.Order.Filter.NAry import Mathlib.Order.Filter.Ultrafilter.Defs /-! # Pointwise operations on filters This file defines pointwise operations on filters. This is useful because usual algebraic operations distribute over pointwise operations. For example, * `(f₁ * f₂).map m = f₁.map m * f₂.map m` * `𝓝 (x * y) = 𝓝 x * 𝓝 y` ## Main declarations * `0` (`Filter.instZero`): Pure filter at `0 : α`, or alternatively principal filter at `0 : Set α`. * `1` (`Filter.instOne`): Pure filter at `1 : α`, or alternatively principal filter at `1 : Set α`. * `f + g` (`Filter.instAdd`): Addition, filter generated by all `s + t` where `s ∈ f` and `t ∈ g`. * `f * g` (`Filter.instMul`): Multiplication, filter generated by all `s * t` where `s ∈ f` and `t ∈ g`. * `-f` (`Filter.instNeg`): Negation, filter of all `-s` where `s ∈ f`. * `f⁻¹` (`Filter.instInv`): Inversion, filter of all `s⁻¹` where `s ∈ f`. * `f - g` (`Filter.instSub`): Subtraction, filter generated by all `s - t` where `s ∈ f` and `t ∈ g`. * `f / g` (`Filter.instDiv`): Division, filter generated by all `s / t` where `s ∈ f` and `t ∈ g`. * `f +ᵥ g` (`Filter.instVAdd`): Scalar addition, filter generated by all `s +ᵥ t` where `s ∈ f` and `t ∈ g`. * `f -ᵥ g` (`Filter.instVSub`): Scalar subtraction, filter generated by all `s -ᵥ t` where `s ∈ f` and `t ∈ g`. * `f • g` (`Filter.instSMul`): Scalar multiplication, filter generated by all `s • t` where `s ∈ f` and `t ∈ g`. * `a +ᵥ f` (`Filter.instVAddFilter`): Translation, filter of all `a +ᵥ s` where `s ∈ f`. * `a • f` (`Filter.instSMulFilter`): Scaling, filter of all `a • s` where `s ∈ f`. For `α` a semigroup/monoid, `Filter α` is a semigroup/monoid. As an unfortunate side effect, this means that `n • f`, where `n : ℕ`, is ambiguous between pointwise scaling and repeated pointwise addition. See note [pointwise nat action]. ## Implementation notes We put all instances in the locale `Pointwise`, so that these instances are not available by default. Note that we do not mark them as reducible (as argued by note [reducible non-instances]) since we expect the locale to be open whenever the instances are actually used (and making the instances reducible changes the behavior of `simp`). ## Tags filter multiplication, filter addition, pointwise addition, pointwise multiplication, -/ open Function Set Filter Pointwise variable {F α β γ δ ε : Type*} namespace Filter /-! ### `0`/`1` as filters -/ section One variable [One α] {f : Filter α} {s : Set α} /-- `1 : Filter α` is defined as the filter of sets containing `1 : α` in locale `Pointwise`. -/ @[to_additive "`0 : Filter α` is defined as the filter of sets containing `0 : α` in locale `Pointwise`."] protected def instOne : One (Filter α) := ⟨pure 1⟩ scoped[Pointwise] attribute [instance] Filter.instOne Filter.instZero @[to_additive (attr := simp)] theorem mem_one : s ∈ (1 : Filter α) ↔ (1 : α) ∈ s := mem_pure @[to_additive] theorem one_mem_one : (1 : Set α) ∈ (1 : Filter α) := mem_pure.2 Set.one_mem_one @[to_additive (attr := simp)] theorem pure_one : pure 1 = (1 : Filter α) := rfl @[to_additive (attr := simp) zero_prod] theorem one_prod {l : Filter β} : (1 : Filter α) ×ˢ l = map (1, ·) l := pure_prod @[to_additive (attr := simp) prod_zero] theorem prod_one {l : Filter β} : l ×ˢ (1 : Filter α) = map (·, 1) l := prod_pure @[to_additive (attr := simp)] theorem principal_one : 𝓟 1 = (1 : Filter α) := principal_singleton _ @[to_additive] theorem one_neBot : (1 : Filter α).NeBot := Filter.pure_neBot scoped[Pointwise] attribute [instance] one_neBot zero_neBot @[to_additive (attr := simp)] protected theorem map_one' (f : α → β) : (1 : Filter α).map f = pure (f 1) := rfl @[to_additive (attr := simp)] theorem le_one_iff : f ≤ 1 ↔ (1 : Set α) ∈ f := le_pure_iff @[to_additive] protected theorem NeBot.le_one_iff (h : f.NeBot) : f ≤ 1 ↔ f = 1 := h.le_pure_iff @[to_additive (attr := simp)] theorem eventually_one {p : α → Prop} : (∀ᶠ x in 1, p x) ↔ p 1 := eventually_pure @[to_additive (attr := simp)] theorem tendsto_one {a : Filter β} {f : β → α} : Tendsto f a 1 ↔ ∀ᶠ x in a, f x = 1 := tendsto_pure @[to_additive zero_prod_zero] theorem one_prod_one [One β] : (1 : Filter α) ×ˢ (1 : Filter β) = 1 := prod_pure_pure /-- `pure` as a `OneHom`. -/ @[to_additive "`pure` as a `ZeroHom`."] def pureOneHom : OneHom α (Filter α) where toFun := pure; map_one' := pure_one @[to_additive (attr := simp)] theorem coe_pureOneHom : (pureOneHom : α → Filter α) = pure := rfl @[to_additive (attr := simp)] theorem pureOneHom_apply (a : α) : pureOneHom a = pure a := rfl variable [One β] @[to_additive] protected theorem map_one [FunLike F α β] [OneHomClass F α β] (φ : F) : map φ 1 = 1 := by simp end One /-! ### Filter negation/inversion -/ section Inv variable [Inv α] {f g : Filter α} {s : Set α} {a : α} /-- The inverse of a filter is the pointwise preimage under `⁻¹` of its sets. -/ @[to_additive "The negation of a filter is the pointwise preimage under `-` of its sets."] instance instInv : Inv (Filter α) := ⟨map Inv.inv⟩ @[to_additive (attr := simp)] protected theorem map_inv : f.map Inv.inv = f⁻¹ := rfl @[to_additive] theorem mem_inv : s ∈ f⁻¹ ↔ Inv.inv ⁻¹' s ∈ f := Iff.rfl @[to_additive] protected theorem inv_le_inv (hf : f ≤ g) : f⁻¹ ≤ g⁻¹ := map_mono hf @[to_additive (attr := simp)] theorem inv_pure : (pure a : Filter α)⁻¹ = pure a⁻¹ := rfl @[to_additive (attr := simp)] theorem inv_eq_bot_iff : f⁻¹ = ⊥ ↔ f = ⊥ := map_eq_bot_iff @[to_additive (attr := simp)] theorem neBot_inv_iff : f⁻¹.NeBot ↔ NeBot f := map_neBot_iff _ @[to_additive] protected theorem NeBot.inv : f.NeBot → f⁻¹.NeBot := fun h => h.map _ @[to_additive neg.instNeBot] lemma inv.instNeBot [NeBot f] : NeBot f⁻¹ := .inv ‹_› scoped[Pointwise] attribute [instance] inv.instNeBot neg.instNeBot end Inv section InvolutiveInv variable [InvolutiveInv α] {f g : Filter α} {s : Set α} @[to_additive (attr := simp)] protected lemma comap_inv : comap Inv.inv f = f⁻¹ := .symm <| map_eq_comap_of_inverse (inv_comp_inv _) (inv_comp_inv _) @[to_additive] theorem inv_mem_inv (hs : s ∈ f) : s⁻¹ ∈ f⁻¹ := by rwa [mem_inv, inv_preimage, inv_inv] /-- Inversion is involutive on `Filter α` if it is on `α`. -/ @[to_additive "Negation is involutive on `Filter α` if it is on `α`."] protected def instInvolutiveInv : InvolutiveInv (Filter α) := { Filter.instInv with inv_inv := fun f => map_map.trans <| by rw [inv_involutive.comp_self, map_id] } scoped[Pointwise] attribute [instance] Filter.instInvolutiveInv Filter.instInvolutiveNeg @[to_additive (attr := simp)] protected theorem inv_le_inv_iff : f⁻¹ ≤ g⁻¹ ↔ f ≤ g := ⟨fun h => inv_inv f ▸ inv_inv g ▸ Filter.inv_le_inv h, Filter.inv_le_inv⟩ @[to_additive] theorem inv_le_iff_le_inv : f⁻¹ ≤ g ↔ f ≤ g⁻¹ := by rw [← Filter.inv_le_inv_iff, inv_inv] @[to_additive (attr := simp)] theorem inv_le_self : f⁻¹ ≤ f ↔ f⁻¹ = f := ⟨fun h => h.antisymm <| inv_le_iff_le_inv.1 h, Eq.le⟩ end InvolutiveInv @[to_additive (attr := simp)] lemma inv_atTop {G : Type*} [CommGroup G] [PartialOrder G] [IsOrderedMonoid G] : (atTop : Filter G)⁻¹ = atBot := (OrderIso.inv G).map_atTop /-! ### Filter addition/multiplication -/ section Mul variable [Mul α] [Mul β] {f f₁ f₂ g g₁ g₂ h : Filter α} {s t : Set α} {a b : α} /-- The filter `f * g` is generated by `{s * t | s ∈ f, t ∈ g}` in locale `Pointwise`. -/ @[to_additive "The filter `f + g` is generated by `{s + t | s ∈ f, t ∈ g}` in locale `Pointwise`."] protected def instMul : Mul (Filter α) := ⟨/- This is defeq to `map₂ (· * ·) f g`, but the hypothesis unfolds to `t₁ * t₂ ⊆ s` rather
than all the way to `Set.image2 (· * ·) t₁ t₂ ⊆ s`. -/
Mathlib/Order/Filter/Pointwise.lean
249
249
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import Mathlib.MeasureTheory.Integral.Lebesgue.Basic import Mathlib.MeasureTheory.Integral.Lebesgue.Countable import Mathlib.MeasureTheory.Integral.Lebesgue.MeasurePreserving import Mathlib.MeasureTheory.Integral.Lebesgue.Norm deprecated_module (since := "2025-04-13")
Mathlib/MeasureTheory/Integral/Lebesgue.lean
1,384
1,388
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro -/ import Mathlib.Data.Subtype import Mathlib.Order.Defs.LinearOrder import Mathlib.Order.Notation import Mathlib.Tactic.GCongr.Core import Mathlib.Tactic.Spread import Mathlib.Tactic.Convert import Mathlib.Tactic.Inhabit import Mathlib.Tactic.SimpRw /-! # Basic definitions about `≤` and `<` This file proves basic results about orders, provides extensive dot notation, defines useful order classes and allows to transfer order instances. ## Type synonyms * `OrderDual α` : A type synonym reversing the meaning of all inequalities, with notation `αᵒᵈ`. * `AsLinearOrder α`: A type synonym to promote `PartialOrder α` to `LinearOrder α` using `IsTotal α (≤)`. ### Transferring orders - `Order.Preimage`, `Preorder.lift`: Transfers a (pre)order on `β` to an order on `α` using a function `f : α → β`. - `PartialOrder.lift`, `LinearOrder.lift`: Transfers a partial (resp., linear) order on `β` to a partial (resp., linear) order on `α` using an injective function `f`. ### Extra class * `DenselyOrdered`: An order with no gap, i.e. for any two elements `a < b` there exists `c` such that `a < c < b`. ## Notes `≤` and `<` are highly favored over `≥` and `>` in mathlib. The reason is that we can formulate all lemmas using `≤`/`<`, and `rw` has trouble unifying `≤` and `≥`. Hence choosing one direction spares us useless duplication. This is enforced by a linter. See Note [nolint_ge] for more infos. Dot notation is particularly useful on `≤` (`LE.le`) and `<` (`LT.lt`). To that end, we provide many aliases to dot notation-less lemmas. For example, `le_trans` is aliased with `LE.le.trans` and can be used to construct `hab.trans hbc : a ≤ c` when `hab : a ≤ b`, `hbc : b ≤ c`, `lt_of_le_of_lt` is aliased as `LE.le.trans_lt` and can be used to construct `hab.trans hbc : a < c` when `hab : a ≤ b`, `hbc : b < c`. ## TODO - expand module docs - automatic construction of dual definitions / theorems ## Tags preorder, order, partial order, poset, linear order, chain -/ open Function variable {ι α β : Type*} {π : ι → Type*} /-! ### Bare relations -/ attribute [ext] LE protected lemma LE.le.ge [LE α] {x y : α} (h : x ≤ y) : y ≥ x := h protected lemma GE.ge.le [LE α] {x y : α} (h : x ≥ y) : y ≤ x := h protected lemma LT.lt.gt [LT α] {x y : α} (h : x < y) : y > x := h protected lemma GT.gt.lt [LT α] {x y : α} (h : x > y) : y < x := h /-- Given a relation `R` on `β` and a function `f : α → β`, the preimage relation on `α` is defined by `x ≤ y ↔ f x ≤ f y`. It is the unique relation on `α` making `f` a `RelEmbedding` (assuming `f` is injective). -/ @[simp] def Order.Preimage (f : α → β) (s : β → β → Prop) (x y : α) : Prop := s (f x) (f y) @[inherit_doc] infixl:80 " ⁻¹'o " => Order.Preimage /-- The preimage of a decidable order is decidable. -/ instance Order.Preimage.decidable (f : α → β) (s : β → β → Prop) [H : DecidableRel s] : DecidableRel (f ⁻¹'o s) := fun _ _ ↦ H _ _ /-! ### Preorders -/ section Preorder variable [Preorder α] {a b c d : α} theorem le_trans' : b ≤ c → a ≤ b → a ≤ c := flip le_trans theorem lt_trans' : b < c → a < b → a < c := flip lt_trans theorem lt_of_le_of_lt' : b ≤ c → a < b → a < c := flip lt_of_lt_of_le theorem lt_of_lt_of_le' : b < c → a ≤ b → a < c := flip lt_of_le_of_lt theorem le_of_le_of_eq' : b ≤ c → a = b → a ≤ c := flip le_of_eq_of_le theorem le_of_eq_of_le' : b = c → a ≤ b → a ≤ c := flip le_of_le_of_eq theorem lt_of_lt_of_eq' : b < c → a = b → a < c := flip lt_of_eq_of_lt theorem lt_of_eq_of_lt' : b = c → a < b → a < c := flip lt_of_lt_of_eq theorem not_lt_iff_not_le_or_ge : ¬a < b ↔ ¬a ≤ b ∨ b ≤ a := by rw [lt_iff_le_not_le, Classical.not_and_iff_not_or_not, Classical.not_not] -- Unnecessary brackets are here for readability lemma not_lt_iff_le_imp_le : ¬ a < b ↔ (a ≤ b → b ≤ a) := by simp [not_lt_iff_not_le_or_ge, or_iff_not_imp_left] /-- If `x = y` then `y ≤ x`. Note: this lemma uses `y ≤ x` instead of `x ≥ y`, because `le` is used almost exclusively in mathlib. -/ lemma ge_of_eq (h : a = b) : b ≤ a := le_of_eq h.symm @[simp] lemma lt_self_iff_false (x : α) : x < x ↔ False := ⟨lt_irrefl x, False.elim⟩ alias LE.le.trans := le_trans alias LE.le.trans' := le_trans' alias LT.lt.trans := lt_trans alias LT.lt.trans' := lt_trans' alias LE.le.trans_lt := lt_of_le_of_lt alias LE.le.trans_lt' := lt_of_le_of_lt' alias LT.lt.trans_le := lt_of_lt_of_le alias LT.lt.trans_le' := lt_of_lt_of_le' alias LE.le.trans_eq := le_of_le_of_eq alias LE.le.trans_eq' := le_of_le_of_eq' alias LT.lt.trans_eq := lt_of_lt_of_eq alias LT.lt.trans_eq' := lt_of_lt_of_eq' alias Eq.trans_le := le_of_eq_of_le alias Eq.trans_ge := le_of_eq_of_le' alias Eq.trans_lt := lt_of_eq_of_lt alias Eq.trans_gt := lt_of_eq_of_lt' alias LE.le.lt_of_not_le := lt_of_le_not_le alias LE.le.lt_or_eq_dec := Decidable.lt_or_eq_of_le alias LT.lt.le := le_of_lt alias LT.lt.ne := ne_of_lt alias Eq.le := le_of_eq @[inherit_doc ge_of_eq] alias Eq.ge := ge_of_eq alias LT.lt.asymm := lt_asymm alias LT.lt.not_lt := lt_asymm theorem ne_of_not_le (h : ¬a ≤ b) : a ≠ b := fun hab ↦ h (le_of_eq hab) protected lemma Eq.not_lt (hab : a = b) : ¬a < b := fun h' ↦ h'.ne hab protected lemma Eq.not_gt (hab : a = b) : ¬b < a := hab.symm.not_lt @[simp] lemma le_of_subsingleton [Subsingleton α] : a ≤ b := (Subsingleton.elim a b).le -- Making this a @[simp] lemma causes confluence problems downstream. lemma not_lt_of_subsingleton [Subsingleton α] : ¬a < b := (Subsingleton.elim a b).not_lt namespace LT.lt protected theorem false : a < a → False := lt_irrefl a theorem ne' (h : a < b) : b ≠ a := h.ne.symm end LT.lt theorem le_of_forall_le (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ le_rfl theorem le_of_forall_ge (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ le_rfl @[deprecated (since := "2025-01-30")] alias le_of_forall_le' := le_of_forall_ge theorem forall_le_iff_le : (∀ ⦃c⦄, c ≤ a → c ≤ b) ↔ a ≤ b := ⟨le_of_forall_le, fun h _ hca ↦ le_trans hca h⟩ theorem forall_le_iff_ge : (∀ ⦃c⦄, a ≤ c → b ≤ c) ↔ b ≤ a := ⟨le_of_forall_ge, fun h _ hca ↦ le_trans h hca⟩ /-- monotonicity of `≤` with respect to `→` -/ theorem le_implies_le_of_le_of_le (hca : c ≤ a) (hbd : b ≤ d) : a ≤ b → c ≤ d := fun hab ↦ (hca.trans hab).trans hbd end Preorder /-! ### Partial order -/ section PartialOrder variable [PartialOrder α] {a b : α} theorem ge_antisymm : a ≤ b → b ≤ a → b = a := flip le_antisymm theorem lt_of_le_of_ne' : a ≤ b → b ≠ a → a < b := fun h₁ h₂ ↦ lt_of_le_of_ne h₁ h₂.symm theorem Ne.lt_of_le : a ≠ b → a ≤ b → a < b := flip lt_of_le_of_ne theorem Ne.lt_of_le' : b ≠ a → a ≤ b → a < b := flip lt_of_le_of_ne' alias LE.le.antisymm := le_antisymm alias LE.le.antisymm' := ge_antisymm alias LE.le.lt_of_ne := lt_of_le_of_ne alias LE.le.lt_of_ne' := lt_of_le_of_ne' alias LE.le.lt_or_eq := lt_or_eq_of_le -- Unnecessary brackets are here for readability lemma le_imp_eq_iff_le_imp_le : (a ≤ b → b = a) ↔ (a ≤ b → b ≤ a) where mp h hab := (h hab).le mpr h hab := (h hab).antisymm hab -- Unnecessary brackets are here for readability lemma ge_imp_eq_iff_le_imp_le : (a ≤ b → a = b) ↔ (a ≤ b → b ≤ a) where mp h hab := (h hab).ge mpr h hab := hab.antisymm (h hab) namespace LE.le theorem lt_iff_ne (h : a ≤ b) : a < b ↔ a ≠ b := ⟨fun h ↦ h.ne, h.lt_of_ne⟩ theorem gt_iff_ne (h : a ≤ b) : a < b ↔ b ≠ a := ⟨fun h ↦ h.ne.symm, h.lt_of_ne'⟩ theorem not_lt_iff_eq (h : a ≤ b) : ¬a < b ↔ a = b := h.lt_iff_ne.not_left theorem not_gt_iff_eq (h : a ≤ b) : ¬a < b ↔ b = a := h.gt_iff_ne.not_left theorem le_iff_eq (h : a ≤ b) : b ≤ a ↔ b = a := ⟨fun h' ↦ h'.antisymm h, Eq.le⟩ theorem ge_iff_eq (h : a ≤ b) : b ≤ a ↔ a = b := ⟨h.antisymm, Eq.ge⟩ end LE.le -- See Note [decidable namespace] protected theorem Decidable.le_iff_eq_or_lt [DecidableLE α] : a ≤ b ↔ a = b ∨ a < b := Decidable.le_iff_lt_or_eq.trans or_comm theorem le_iff_eq_or_lt : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or_comm theorem lt_iff_le_and_ne : a < b ↔ a ≤ b ∧ a ≠ b := ⟨fun h ↦ ⟨le_of_lt h, ne_of_lt h⟩, fun ⟨h1, h2⟩ ↦ h1.lt_of_ne h2⟩ lemma eq_iff_not_lt_of_le (hab : a ≤ b) : a = b ↔ ¬ a < b := by simp [hab, lt_iff_le_and_ne] alias LE.le.eq_iff_not_lt := eq_iff_not_lt_of_le -- See Note [decidable namespace] protected theorem Decidable.eq_iff_le_not_lt [DecidableLE α] : a = b ↔ a ≤ b ∧ ¬a < b := ⟨fun h ↦ ⟨h.le, h ▸ lt_irrefl _⟩, fun ⟨h₁, h₂⟩ ↦ h₁.antisymm <| Decidable.byContradiction fun h₃ ↦ h₂ (h₁.lt_of_not_le h₃)⟩ theorem eq_iff_le_not_lt : a = b ↔ a ≤ b ∧ ¬a < b := haveI := Classical.dec Decidable.eq_iff_le_not_lt theorem eq_or_lt_of_le (h : a ≤ b) : a = b ∨ a < b := h.lt_or_eq.symm theorem eq_or_gt_of_le (h : a ≤ b) : b = a ∨ a < b := h.lt_or_eq.symm.imp Eq.symm id theorem gt_or_eq_of_le (h : a ≤ b) : a < b ∨ b = a := (eq_or_gt_of_le h).symm alias LE.le.eq_or_lt_dec := Decidable.eq_or_lt_of_le alias LE.le.eq_or_lt := eq_or_lt_of_le alias LE.le.eq_or_gt := eq_or_gt_of_le alias LE.le.gt_or_eq := gt_or_eq_of_le theorem eq_of_le_of_not_lt (hab : a ≤ b) (hba : ¬a < b) : a = b := hab.eq_or_lt.resolve_right hba theorem eq_of_ge_of_not_gt (hab : a ≤ b) (hba : ¬a < b) : b = a := (eq_of_le_of_not_lt hab hba).symm alias LE.le.eq_of_not_lt := eq_of_le_of_not_lt alias LE.le.eq_of_not_gt := eq_of_ge_of_not_gt theorem Ne.le_iff_lt (h : a ≠ b) : a ≤ b ↔ a < b := ⟨fun h' ↦ lt_of_le_of_ne h' h, fun h ↦ h.le⟩ theorem Ne.not_le_or_not_le (h : a ≠ b) : ¬a ≤ b ∨ ¬b ≤ a := not_and_or.1 <| le_antisymm_iff.not.1 h -- See Note [decidable namespace] protected theorem Decidable.ne_iff_lt_iff_le [DecidableEq α] : (a ≠ b ↔ a < b) ↔ a ≤ b := ⟨fun h ↦ Decidable.byCases le_of_eq (le_of_lt ∘ h.mp), fun h ↦ ⟨lt_of_le_of_ne h, ne_of_lt⟩⟩ @[simp] theorem ne_iff_lt_iff_le : (a ≠ b ↔ a < b) ↔ a ≤ b := haveI := Classical.dec Decidable.ne_iff_lt_iff_le lemma eq_of_forall_le_iff (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := ((H _).1 le_rfl).antisymm ((H _).2 le_rfl) lemma eq_of_forall_ge_iff (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := ((H _).2 le_rfl).antisymm ((H _).1 le_rfl) /-- To prove commutativity of a binary operation `○`, we only to check `a ○ b ≤ b ○ a` for all `a`, `b`. -/ lemma commutative_of_le {f : β → β → α} (comm : ∀ a b, f a b ≤ f b a) : ∀ a b, f a b = f b a := fun _ _ ↦ (comm _ _).antisymm <| comm _ _ /-- To prove associativity of a commutative binary operation `○`, we only to check `(a ○ b) ○ c ≤ a ○ (b ○ c)` for all `a`, `b`, `c`. -/ lemma associative_of_commutative_of_le {f : α → α → α} (comm : Std.Commutative f) (assoc : ∀ a b c, f (f a b) c ≤ f a (f b c)) : Std.Associative f where assoc a b c := le_antisymm (assoc _ _ _) <| by rw [comm.comm, comm.comm b, comm.comm _ c, comm.comm a] exact assoc .. end PartialOrder section LinearOrder variable [LinearOrder α] {a b : α} namespace LE.le lemma lt_or_le (h : a ≤ b) (c : α) : a < c ∨ c ≤ b := (lt_or_ge a c).imp id h.trans' lemma le_or_lt (h : a ≤ b) (c : α) : a ≤ c ∨ c < b := (le_or_gt a c).imp id h.trans_lt' lemma le_or_le (h : a ≤ b) (c : α) : a ≤ c ∨ c ≤ b := (h.lt_or_le c).imp le_of_lt id end LE.le namespace LT.lt lemma lt_or_lt (h : a < b) (c : α) : a < c ∨ c < b := (le_or_gt b c).imp h.trans_le id end LT.lt -- Variant of `min_def` with the branches reversed. theorem min_def' (a b : α) : min a b = if b ≤ a then b else a := by rw [min_def] rcases lt_trichotomy a b with (lt | eq | gt) · rw [if_pos lt.le, if_neg (not_le.mpr lt)] · rw [if_pos eq.le, if_pos eq.ge, eq] · rw [if_neg (not_le.mpr gt.gt), if_pos gt.le] -- Variant of `min_def` with the branches reversed. -- This is sometimes useful as it used to be the default. theorem max_def' (a b : α) : max a b = if b ≤ a then a else b := by rw [max_def] rcases lt_trichotomy a b with (lt | eq | gt) · rw [if_pos lt.le, if_neg (not_le.mpr lt)] · rw [if_pos eq.le, if_pos eq.ge, eq] · rw [if_neg (not_le.mpr gt.gt), if_pos gt.le] theorem lt_of_not_le (h : ¬b ≤ a) : a < b := ((le_total _ _).resolve_right h).lt_of_not_le h theorem lt_iff_not_le : a < b ↔ ¬b ≤ a := ⟨not_le_of_lt, lt_of_not_le⟩ theorem Ne.lt_or_lt (h : a ≠ b) : a < b ∨ b < a := lt_or_gt_of_ne h /-- A version of `ne_iff_lt_or_gt` with LHS and RHS reversed. -/ @[simp] theorem lt_or_lt_iff_ne : a < b ∨ b < a ↔ a ≠ b := ne_iff_lt_or_gt.symm theorem not_lt_iff_eq_or_lt : ¬a < b ↔ a = b ∨ b < a := not_lt.trans <| Decidable.le_iff_eq_or_lt.trans <| or_congr eq_comm Iff.rfl theorem exists_ge_of_linear (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | Or.inl h => ⟨_, h, le_rfl⟩ | Or.inr h => ⟨_, le_rfl, h⟩ lemma exists_forall_ge_and {p q : α → Prop} : (∃ i, ∀ j ≥ i, p j) → (∃ i, ∀ j ≥ i, q j) → ∃ i, ∀ j ≥ i, p j ∧ q j | ⟨a, ha⟩, ⟨b, hb⟩ => let ⟨c, hac, hbc⟩ := exists_ge_of_linear a b ⟨c, fun _d hcd ↦ ⟨ha _ <| hac.trans hcd, hb _ <| hbc.trans hcd⟩⟩ theorem le_of_forall_lt (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt fun h ↦ lt_irrefl _ (H _ h) theorem forall_lt_iff_le : (∀ ⦃c⦄, c < a → c < b) ↔ a ≤ b := ⟨le_of_forall_lt, fun h _ hca ↦ lt_of_lt_of_le hca h⟩ theorem le_of_forall_lt' (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt fun h ↦ lt_irrefl _ (H _ h) theorem forall_lt_iff_le' : (∀ ⦃c⦄, a < c → b < c) ↔ b ≤ a := ⟨le_of_forall_lt', fun h _ hac ↦ lt_of_le_of_lt h hac⟩ theorem eq_of_forall_lt_iff (h : ∀ c, c < a ↔ c < b) : a = b := (le_of_forall_lt fun _ ↦ (h _).1).antisymm <| le_of_forall_lt fun _ ↦ (h _).2 theorem eq_of_forall_gt_iff (h : ∀ c, a < c ↔ b < c) : a = b := (le_of_forall_lt' fun _ ↦ (h _).2).antisymm <| le_of_forall_lt' fun _ ↦ (h _).1 section ltByCases variable {P : Sort*} {x y : α} @[simp] lemma ltByCases_lt (h : x < y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₁ h := dif_pos h @[simp] lemma ltByCases_gt (h : y < x) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₃ h := (dif_neg h.not_lt).trans (dif_pos h) @[simp] lemma ltByCases_eq (h : x = y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₂ h := (dif_neg h.not_lt).trans (dif_neg h.not_gt) lemma ltByCases_not_lt (h : ¬ x < y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ y < x → x = y := fun h' => (le_antisymm (le_of_not_gt h') (le_of_not_gt h))) : ltByCases x y h₁ h₂ h₃ = if h' : y < x then h₃ h' else h₂ (p h') := dif_neg h lemma ltByCases_not_gt (h : ¬ y < x) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ x < y → x = y := fun h' => (le_antisymm (le_of_not_gt h) (le_of_not_gt h'))) : ltByCases x y h₁ h₂ h₃ = if h' : x < y then h₁ h' else h₂ (p h') := dite_congr rfl (fun _ => rfl) (fun _ => dif_neg h) lemma ltByCases_ne (h : x ≠ y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ x < y → y < x := fun h' => h.lt_or_lt.resolve_left h') : ltByCases x y h₁ h₂ h₃ = if h' : x < y then h₁ h' else h₃ (p h') := dite_congr rfl (fun _ => rfl) (fun _ => dif_pos _) lemma ltByCases_comm {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : y = x → x = y := fun h' => h'.symm) : ltByCases x y h₁ h₂ h₃ = ltByCases y x h₃ (h₂ ∘ p) h₁ := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · rw [ltByCases_lt h, ltByCases_gt h] · rw [ltByCases_eq h, ltByCases_eq h.symm, comp_apply] · rw [ltByCases_lt h, ltByCases_gt h] lemma eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt {x' y' : α} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) : x = y ↔ x' = y' := by simp_rw [eq_iff_le_not_lt, ← not_lt, ltc, gtc] lemma ltByCases_rec {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : P) (hlt : (h : x < y) → h₁ h = p) (heq : (h : x = y) → h₂ h = p) (hgt : (h : y < x) → h₃ h = p) : ltByCases x y h₁ h₂ h₃ = p := ltByCases x y (fun h => ltByCases_lt h ▸ hlt h) (fun h => ltByCases_eq h ▸ heq h) (fun h => ltByCases_gt h ▸ hgt h) lemma ltByCases_eq_iff {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} {p : P} : ltByCases x y h₁ h₂ h₃ = p ↔ (∃ h, h₁ h = p) ∨ (∃ h, h₂ h = p) ∨ (∃ h, h₃ h = p) := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · simp only [ltByCases_lt, exists_prop_of_true, h, h.not_lt, not_false_eq_true, exists_prop_of_false, or_false, h.ne] · simp only [h, lt_self_iff_false, ltByCases_eq, not_false_eq_true, exists_prop_of_false, exists_prop_of_true, or_false, false_or] · simp only [ltByCases_gt, exists_prop_of_true, h, h.not_lt, not_false_eq_true, exists_prop_of_false, false_or, h.ne'] lemma ltByCases_congr {x' y' : α} {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} {h₁' : x' < y' → P} {h₂' : x' = y' → P} {h₃' : y' < x' → P} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) (hh'₁ : ∀ (h : x' < y'), h₁ (ltc.mpr h) = h₁' h) (hh'₂ : ∀ (h : x' = y'), h₂ ((eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt ltc gtc).mpr h) = h₂' h) (hh'₃ : ∀ (h : y' < x'), h₃ (gtc.mpr h) = h₃' h) : ltByCases x y h₁ h₂ h₃ = ltByCases x' y' h₁' h₂' h₃' := by refine ltByCases_rec _ (fun h => ?_) (fun h => ?_) (fun h => ?_) · rw [ltByCases_lt (ltc.mp h), hh'₁] · rw [eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt ltc gtc] at h rw [ltByCases_eq h, hh'₂] · rw [ltByCases_gt (gtc.mp h), hh'₃] /-- Perform a case-split on the ordering of `x` and `y` in a decidable linear order, non-dependently. -/ abbrev ltTrichotomy (x y : α) (p q r : P) := ltByCases x y (fun _ => p) (fun _ => q) (fun _ => r) variable {p q r s : P} @[simp] lemma ltTrichotomy_lt (h : x < y) : ltTrichotomy x y p q r = p := ltByCases_lt h @[simp] lemma ltTrichotomy_gt (h : y < x) : ltTrichotomy x y p q r = r := ltByCases_gt h @[simp] lemma ltTrichotomy_eq (h : x = y) : ltTrichotomy x y p q r = q := ltByCases_eq h lemma ltTrichotomy_not_lt (h : ¬ x < y) : ltTrichotomy x y p q r = if y < x then r else q := ltByCases_not_lt h lemma ltTrichotomy_not_gt (h : ¬ y < x) : ltTrichotomy x y p q r = if x < y then p else q := ltByCases_not_gt h lemma ltTrichotomy_ne (h : x ≠ y) : ltTrichotomy x y p q r = if x < y then p else r := ltByCases_ne h lemma ltTrichotomy_comm : ltTrichotomy x y p q r = ltTrichotomy y x r q p := ltByCases_comm lemma ltTrichotomy_self {p : P} : ltTrichotomy x y p p p = p := ltByCases_rec p (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) lemma ltTrichotomy_eq_iff : ltTrichotomy x y p q r = s ↔ (x < y ∧ p = s) ∨ (x = y ∧ q = s) ∨ (y < x ∧ r = s) := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · simp only [ltTrichotomy_lt, false_and, true_and, or_false, h, h.not_lt, h.ne] · simp only [ltTrichotomy_eq, false_and, true_and, or_false, false_or, h, lt_irrefl] · simp only [ltTrichotomy_gt, false_and, true_and, false_or, h, h.not_lt, h.ne'] lemma ltTrichotomy_congr {x' y' : α} {p' q' r' : P} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) (hh'₁ : x' < y' → p = p') (hh'₂ : x' = y' → q = q') (hh'₃ : y' < x' → r = r') : ltTrichotomy x y p q r = ltTrichotomy x' y' p' q' r' := ltByCases_congr ltc gtc hh'₁ hh'₂ hh'₃ end ltByCases /-! #### `min`/`max` recursors -/ section MinMaxRec variable {p : α → Prop} lemma min_rec (ha : a ≤ b → p a) (hb : b ≤ a → p b) : p (min a b) := by obtain hab | hba := le_total a b <;> simp [min_eq_left, min_eq_right, *] lemma max_rec (ha : b ≤ a → p a) (hb : a ≤ b → p b) : p (max a b) := by obtain hab | hba := le_total a b <;> simp [max_eq_left, max_eq_right, *] lemma min_rec' (p : α → Prop) (ha : p a) (hb : p b) : p (min a b) := min_rec (fun _ ↦ ha) fun _ ↦ hb lemma max_rec' (p : α → Prop) (ha : p a) (hb : p b) : p (max a b) := max_rec (fun _ ↦ ha) fun _ ↦ hb lemma min_def_lt (a b : α) : min a b = if a < b then a else b := by rw [min_comm, min_def, ← ite_not]; simp only [not_le] lemma max_def_lt (a b : α) : max a b = if a < b then b else a := by rw [max_comm, max_def, ← ite_not]; simp only [not_le] end MinMaxRec end LinearOrder /-! ### Implications -/ lemma lt_imp_lt_of_le_imp_le {β} [LinearOrder α] [Preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_le fun h' ↦ (H h').not_lt h lemma le_imp_le_iff_lt_imp_lt {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} : a ≤ b → c ≤ d ↔ d < c → b < a := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [Preorder α] [Preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans <| (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans <| (not_congr H).trans <| not_le lemma le_iff_le_iff_lt_iff_lt {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, fun H ↦ not_lt.symm.trans <| (not_congr H).trans <| not_lt⟩ /-- A symmetric relation implies two values are equal, when it implies they're less-equal. -/ lemma rel_imp_eq_of_rel_imp_le [PartialOrder β] (r : α → α → Prop) [IsSymm α r] {f : α → β} (h : ∀ a b, r a b → f a ≤ f b) {a b : α} : r a b → f a = f b := fun hab ↦ le_antisymm (h a b hab) (h b a <| symm hab) /-! ### Extensionality lemmas -/ @[ext] lemma Preorder.toLE_injective : Function.Injective (@Preorder.toLE α) := fun | { lt := A_lt, lt_iff_le_not_le := A_iff, .. }, { lt := B_lt, lt_iff_le_not_le := B_iff, .. } => by rintro ⟨⟩ have : A_lt = B_lt := by funext a b rw [A_iff, B_iff] cases this congr @[ext] lemma PartialOrder.toPreorder_injective : Function.Injective (@PartialOrder.toPreorder α) := by rintro ⟨⟩ ⟨⟩ ⟨⟩; congr @[ext] lemma LinearOrder.toPartialOrder_injective : Function.Injective (@LinearOrder.toPartialOrder α) := fun | { le := A_le, lt := A_lt, toDecidableLE := A_decidableLE, toDecidableEq := A_decidableEq, toDecidableLT := A_decidableLT min := A_min, max := A_max, min_def := A_min_def, max_def := A_max_def, compare := A_compare, compare_eq_compareOfLessAndEq := A_compare_canonical, .. }, { le := B_le, lt := B_lt, toDecidableLE := B_decidableLE, toDecidableEq := B_decidableEq, toDecidableLT := B_decidableLT min := B_min, max := B_max, min_def := B_min_def, max_def := B_max_def, compare := B_compare, compare_eq_compareOfLessAndEq := B_compare_canonical, .. } => by rintro ⟨⟩ obtain rfl : A_decidableLE = B_decidableLE := Subsingleton.elim _ _ obtain rfl : A_decidableEq = B_decidableEq := Subsingleton.elim _ _ obtain rfl : A_decidableLT = B_decidableLT := Subsingleton.elim _ _ have : A_min = B_min := by funext a b exact (A_min_def _ _).trans (B_min_def _ _).symm cases this have : A_max = B_max := by funext a b exact (A_max_def _ _).trans (B_max_def _ _).symm cases this have : A_compare = B_compare := by funext a b exact (A_compare_canonical _ _).trans (B_compare_canonical _ _).symm congr lemma Preorder.ext {A B : Preorder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma PartialOrder.ext {A B : PartialOrder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma PartialOrder.ext_lt {A B : PartialOrder α} (H : ∀ x y : α, (haveI := A; x < y) ↔ x < y) : A = B := by ext x y; rw [le_iff_lt_or_eq, @le_iff_lt_or_eq _ A, H] lemma LinearOrder.ext {A B : LinearOrder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma LinearOrder.ext_lt {A B : LinearOrder α} (H : ∀ x y : α, (haveI := A; x < y) ↔ x < y) : A = B := LinearOrder.toPartialOrder_injective (PartialOrder.ext_lt H) /-! ### Order dual -/ /-- Type synonym to equip a type with the dual order: `≤` means `≥` and `<` means `>`. `αᵒᵈ` is notation for `OrderDual α`. -/ def OrderDual (α : Type*) : Type _ := α @[inherit_doc] notation:max α "ᵒᵈ" => OrderDual α namespace OrderDual instance (α : Type*) [h : Nonempty α] : Nonempty αᵒᵈ := h instance (α : Type*) [h : Subsingleton α] : Subsingleton αᵒᵈ := h instance (α : Type*) [LE α] : LE αᵒᵈ := ⟨fun x y : α ↦ y ≤ x⟩ instance (α : Type*) [LT α] : LT αᵒᵈ := ⟨fun x y : α ↦ y < x⟩ instance instOrd (α : Type*) [Ord α] : Ord αᵒᵈ where compare := fun (a b : α) ↦ compare b a instance instSup (α : Type*) [Min α] : Max αᵒᵈ := ⟨((· ⊓ ·) : α → α → α)⟩ instance instInf (α : Type*) [Max α] : Min αᵒᵈ := ⟨((· ⊔ ·) : α → α → α)⟩ instance instPreorder (α : Type*) [Preorder α] : Preorder αᵒᵈ where le_refl := fun _ ↦ le_refl _ le_trans := fun _ _ _ hab hbc ↦ hbc.trans hab lt_iff_le_not_le := fun _ _ ↦ lt_iff_le_not_le instance instPartialOrder (α : Type*) [PartialOrder α] : PartialOrder αᵒᵈ where __ := inferInstanceAs (Preorder αᵒᵈ) le_antisymm := fun a b hab hba ↦ @le_antisymm α _ a b hba hab instance instLinearOrder (α : Type*) [LinearOrder α] : LinearOrder αᵒᵈ where __ := inferInstanceAs (PartialOrder αᵒᵈ) __ := inferInstanceAs (Ord αᵒᵈ) le_total := fun a b : α ↦ le_total b a max := fun a b ↦ (min a b : α) min := fun a b ↦ (max a b : α) min_def := fun a b ↦ show (max .. : α) = _ by rw [max_comm, max_def]; rfl max_def := fun a b ↦ show (min .. : α) = _ by rw [min_comm, min_def]; rfl toDecidableLE := (inferInstance : DecidableRel (fun a b : α ↦ b ≤ a)) toDecidableLT := (inferInstance : DecidableRel (fun a b : α ↦ b < a)) toDecidableEq := (inferInstance : DecidableEq α) compare_eq_compareOfLessAndEq a b := by simp only [compare, LinearOrder.compare_eq_compareOfLessAndEq, compareOfLessAndEq, eq_comm] rfl /-- The opposite linear order to a given linear order -/ def _root_.LinearOrder.swap (α : Type*) (_ : LinearOrder α) : LinearOrder α := inferInstanceAs <| LinearOrder (OrderDual α) instance : ∀ [Inhabited α], Inhabited αᵒᵈ := fun [x : Inhabited α] => x theorem Ord.dual_dual (α : Type*) [H : Ord α] : OrderDual.instOrd αᵒᵈ = H := rfl theorem Preorder.dual_dual (α : Type*) [H : Preorder α] : OrderDual.instPreorder αᵒᵈ = H := rfl theorem instPartialOrder.dual_dual (α : Type*) [H : PartialOrder α] : OrderDual.instPartialOrder αᵒᵈ = H := rfl theorem instLinearOrder.dual_dual (α : Type*) [H : LinearOrder α] : OrderDual.instLinearOrder αᵒᵈ = H := rfl end OrderDual /-! ### `HasCompl` -/ instance Prop.hasCompl : HasCompl Prop := ⟨Not⟩ instance Pi.hasCompl [∀ i, HasCompl (π i)] : HasCompl (∀ i, π i) := ⟨fun x i ↦ (x i)ᶜ⟩ theorem Pi.compl_def [∀ i, HasCompl (π i)] (x : ∀ i, π i) : xᶜ = fun i ↦ (x i)ᶜ := rfl @[simp] theorem Pi.compl_apply [∀ i, HasCompl (π i)] (x : ∀ i, π i) (i : ι) : xᶜ i = (x i)ᶜ := rfl instance IsIrrefl.compl (r) [IsIrrefl α r] : IsRefl α rᶜ := ⟨@irrefl α r _⟩ instance IsRefl.compl (r) [IsRefl α r] : IsIrrefl α rᶜ := ⟨fun a ↦ not_not_intro (refl a)⟩ theorem compl_lt [LinearOrder α] : (· < · : α → α → _)ᶜ = (· ≥ ·) := by ext; simp [compl] theorem compl_le [LinearOrder α] : (· ≤ · : α → α → _)ᶜ = (· > ·) := by ext; simp [compl] theorem compl_gt [LinearOrder α] : (· > · : α → α → _)ᶜ = (· ≤ ·) := by ext; simp [compl] theorem compl_ge [LinearOrder α] : (· ≥ · : α → α → _)ᶜ = (· < ·) := by ext; simp [compl] instance Ne.instIsEquiv_compl : IsEquiv α (· ≠ ·)ᶜ := by convert eq_isEquiv α simp [compl] /-! ### Order instances on the function space -/ instance Pi.hasLe [∀ i, LE (π i)] : LE (∀ i, π i) where le x y := ∀ i, x i ≤ y i theorem Pi.le_def [∀ i, LE (π i)] {x y : ∀ i, π i} : x ≤ y ↔ ∀ i, x i ≤ y i := Iff.rfl instance Pi.preorder [∀ i, Preorder (π i)] : Preorder (∀ i, π i) where __ := inferInstanceAs (LE (∀ i, π i)) le_refl := fun a i ↦ le_refl (a i) le_trans := fun _ _ _ h₁ h₂ i ↦ le_trans (h₁ i) (h₂ i) theorem Pi.lt_def [∀ i, Preorder (π i)] {x y : ∀ i, π i} : x < y ↔ x ≤ y ∧ ∃ i, x i < y i := by simp +contextual [lt_iff_le_not_le, Pi.le_def] instance Pi.partialOrder [∀ i, PartialOrder (π i)] : PartialOrder (∀ i, π i) where __ := Pi.preorder le_antisymm := fun _ _ h1 h2 ↦ funext fun b ↦ (h1 b).antisymm (h2 b) namespace Sum variable {α₁ α₂ : Type*} [LE β] @[simp] lemma elim_le_elim_iff {u₁ v₁ : α₁ → β} {u₂ v₂ : α₂ → β} : Sum.elim u₁ u₂ ≤ Sum.elim v₁ v₂ ↔ u₁ ≤ v₁ ∧ u₂ ≤ v₂ := Sum.forall lemma const_le_elim_iff {b : β} {v₁ : α₁ → β} {v₂ : α₂ → β} : Function.const _ b ≤ Sum.elim v₁ v₂ ↔ Function.const _ b ≤ v₁ ∧ Function.const _ b ≤ v₂ := elim_const_const b ▸ elim_le_elim_iff .. lemma elim_le_const_iff {b : β} {u₁ : α₁ → β} {u₂ : α₂ → β} : Sum.elim u₁ u₂ ≤ Function.const _ b ↔ u₁ ≤ Function.const _ b ∧ u₂ ≤ Function.const _ b := elim_const_const b ▸ elim_le_elim_iff .. end Sum section Pi /-- A function `a` is strongly less than a function `b` if `a i < b i` for all `i`. -/ def StrongLT [∀ i, LT (π i)] (a b : ∀ i, π i) : Prop := ∀ i, a i < b i @[inherit_doc] local infixl:50 " ≺ " => StrongLT variable [∀ i, Preorder (π i)] {a b c : ∀ i, π i} theorem le_of_strongLT (h : a ≺ b) : a ≤ b := fun _ ↦ (h _).le theorem lt_of_strongLT [Nonempty ι] (h : a ≺ b) : a < b := by inhabit ι exact Pi.lt_def.2 ⟨le_of_strongLT h, default, h _⟩ theorem strongLT_of_strongLT_of_le (hab : a ≺ b) (hbc : b ≤ c) : a ≺ c := fun _ ↦ (hab _).trans_le <| hbc _ theorem strongLT_of_le_of_strongLT (hab : a ≤ b) (hbc : b ≺ c) : a ≺ c := fun _ ↦ (hab _).trans_lt <| hbc _ alias StrongLT.le := le_of_strongLT alias StrongLT.lt := lt_of_strongLT alias StrongLT.trans_le := strongLT_of_strongLT_of_le alias LE.le.trans_strongLT := strongLT_of_le_of_strongLT end Pi section Function variable [DecidableEq ι] [∀ i, Preorder (π i)] {x y : ∀ i, π i} {i : ι} {a b : π i} theorem le_update_iff : x ≤ Function.update y i a ↔ x i ≤ a ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := Function.forall_update_iff _ fun j z ↦ x j ≤ z theorem update_le_iff : Function.update x i a ≤ y ↔ a ≤ y i ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := Function.forall_update_iff _ fun j z ↦ z ≤ y j theorem update_le_update_iff : Function.update x i a ≤ Function.update y i b ↔ a ≤ b ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := by simp +contextual [update_le_iff] @[simp] theorem update_le_update_iff' : update x i a ≤ update x i b ↔ a ≤ b := by simp [update_le_update_iff] @[simp] theorem update_lt_update_iff : update x i a < update x i b ↔ a < b := lt_iff_lt_of_le_iff_le' update_le_update_iff' update_le_update_iff' @[simp] theorem le_update_self_iff : x ≤ update x i a ↔ x i ≤ a := by simp [le_update_iff] @[simp] theorem update_le_self_iff : update x i a ≤ x ↔ a ≤ x i := by simp [update_le_iff] @[simp] theorem lt_update_self_iff : x < update x i a ↔ x i < a := by simp [lt_iff_le_not_le] @[simp] theorem update_lt_self_iff : update x i a < x ↔ a < x i := by simp [lt_iff_le_not_le] end Function instance Pi.sdiff [∀ i, SDiff (π i)] : SDiff (∀ i, π i) := ⟨fun x y i ↦ x i \ y i⟩ theorem Pi.sdiff_def [∀ i, SDiff (π i)] (x y : ∀ i, π i) : x \ y = fun i ↦ x i \ y i := rfl @[simp] theorem Pi.sdiff_apply [∀ i, SDiff (π i)] (x y : ∀ i, π i) (i : ι) : (x \ y) i = x i \ y i := rfl namespace Function variable [Preorder α] [Nonempty β] {a b : α} @[simp] theorem const_le_const : const β a ≤ const β b ↔ a ≤ b := by simp [Pi.le_def] @[simp] theorem const_lt_const : const β a < const β b ↔ a < b := by simpa [Pi.lt_def] using le_of_lt end Function /-! ### Lifts of order instances -/ /-- Transfer a `Preorder` on `β` to a `Preorder` on `α` using a function `f : α → β`. See note [reducible non-instances]. -/ abbrev Preorder.lift [Preorder β] (f : α → β) : Preorder α where le x y := f x ≤ f y le_refl _ := le_rfl le_trans _ _ _ := _root_.le_trans lt x y := f x < f y lt_iff_le_not_le _ _ := _root_.lt_iff_le_not_le /-- Transfer a `PartialOrder` on `β` to a `PartialOrder` on `α` using an injective function `f : α → β`. See note [reducible non-instances]. -/ abbrev PartialOrder.lift [PartialOrder β] (f : α → β) (inj : Injective f) : PartialOrder α := { Preorder.lift f with le_antisymm := fun _ _ h₁ h₂ ↦ inj (h₁.antisymm h₂) } theorem compare_of_injective_eq_compareOfLessAndEq (a b : α) [LinearOrder β] [DecidableEq α] (f : α → β) (inj : Injective f) [Decidable (LT.lt (self := PartialOrder.lift f inj |>.toLT) a b)] : compare (f a) (f b) = @compareOfLessAndEq _ a b (PartialOrder.lift f inj |>.toLT) _ _ := by have h := LinearOrder.compare_eq_compareOfLessAndEq (f a) (f b) simp only [h, compareOfLessAndEq] split_ifs <;> try (first | rfl | contradiction) · have : ¬ f a = f b := by rename_i h; exact inj.ne h contradiction · have : f a = f b := by rename_i h; exact congrArg f h contradiction /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version takes `[Max α]` and `[Min α]` as arguments, then uses them for `max` and `min` fields. See `LinearOrder.lift'` for a version that autogenerates `min` and `max` fields, and `LinearOrder.liftWithOrd` for one that does not auto-generate `compare` fields. See note [reducible non-instances]. -/ abbrev LinearOrder.lift [LinearOrder β] [Max α] [Min α] (f : α → β) (inj : Injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrder α := letI instOrdα : Ord α := ⟨fun a b ↦ compare (f a) (f b)⟩ letI decidableLE := fun x y ↦ (inferInstance : Decidable (f x ≤ f y)) letI decidableLT := fun x y ↦ (inferInstance : Decidable (f x < f y)) letI decidableEq := fun x y ↦ decidable_of_iff (f x = f y) inj.eq_iff { PartialOrder.lift f inj, instOrdα with le_total := fun x y ↦ le_total (f x) (f y) toDecidableLE := decidableLE toDecidableLT := decidableLT toDecidableEq := decidableEq min := (· ⊓ ·) max := (· ⊔ ·) min_def := by intros x y apply inj rw [apply_ite f] exact (hinf _ _).trans (min_def _ _) max_def := by intros x y apply inj rw [apply_ite f] exact (hsup _ _).trans (max_def _ _) compare_eq_compareOfLessAndEq := fun a b ↦ compare_of_injective_eq_compareOfLessAndEq a b f inj } /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version autogenerates `min` and `max` fields. See `LinearOrder.lift` for a version that takes `[Max α]` and `[Min α]`, then uses them as `max` and `min`. See `LinearOrder.liftWithOrd'` for a version which does not auto-generate `compare` fields. See note [reducible non-instances]. -/ abbrev LinearOrder.lift' [LinearOrder β] (f : α → β) (inj : Injective f) : LinearOrder α := @LinearOrder.lift α β _ ⟨fun x y ↦ if f x ≤ f y then y else x⟩ ⟨fun x y ↦ if f x ≤ f y then x else y⟩ f inj (fun _ _ ↦ (apply_ite f _ _ _).trans (max_def _ _).symm) fun _ _ ↦ (apply_ite f _ _ _).trans (min_def _ _).symm /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version takes `[Max α]` and `[Min α]` as arguments, then uses them for `max` and `min` fields. It also takes `[Ord α]` as an argument and uses them for `compare` fields. See `LinearOrder.lift` for a version that autogenerates `compare` fields, and `LinearOrder.liftWithOrd'` for one that auto-generates `min` and `max` fields. fields. See note [reducible non-instances]. -/ abbrev LinearOrder.liftWithOrd [LinearOrder β] [Max α] [Min α] [Ord α] (f : α → β) (inj : Injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) (compare_f : ∀ a b : α, compare a b = compare (f a) (f b)) : LinearOrder α := letI decidableLE := fun x y ↦ (inferInstance : Decidable (f x ≤ f y)) letI decidableLT := fun x y ↦ (inferInstance : Decidable (f x < f y)) letI decidableEq := fun x y ↦ decidable_of_iff (f x = f y) inj.eq_iff { PartialOrder.lift f inj with le_total := fun x y ↦ le_total (f x) (f y) toDecidableLE := decidableLE toDecidableLT := decidableLT toDecidableEq := decidableEq min := (· ⊓ ·) max := (· ⊔ ·) min_def := by intros x y apply inj rw [apply_ite f] exact (hinf _ _).trans (min_def _ _) max_def := by intros x y apply inj rw [apply_ite f] exact (hsup _ _).trans (max_def _ _) compare_eq_compareOfLessAndEq := fun a b ↦ (compare_f a b).trans <| compare_of_injective_eq_compareOfLessAndEq a b f inj } /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version auto-generates `min` and `max` fields. It also takes `[Ord α]` as an argument and uses them for `compare` fields. See `LinearOrder.lift` for a version that autogenerates `compare` fields, and `LinearOrder.liftWithOrd` for one that doesn't auto-generate `min` and `max` fields. fields. See note [reducible non-instances]. -/ abbrev LinearOrder.liftWithOrd' [LinearOrder β] [Ord α] (f : α → β) (inj : Injective f) (compare_f : ∀ a b : α, compare a b = compare (f a) (f b)) : LinearOrder α := @LinearOrder.liftWithOrd α β _ ⟨fun x y ↦ if f x ≤ f y then y else x⟩ ⟨fun x y ↦ if f x ≤ f y then x else y⟩ _ f inj (fun _ _ ↦ (apply_ite f _ _ _).trans (max_def _ _).symm) (fun _ _ ↦ (apply_ite f _ _ _).trans (min_def _ _).symm) compare_f /-! ### Subtype of an order -/ namespace Subtype instance le [LE α] {p : α → Prop} : LE (Subtype p) := ⟨fun x y ↦ (x : α) ≤ y⟩ instance lt [LT α] {p : α → Prop} : LT (Subtype p) := ⟨fun x y ↦ (x : α) < y⟩ @[simp] theorem mk_le_mk [LE α] {p : α → Prop} {x y : α} {hx : p x} {hy : p y} : (⟨x, hx⟩ : Subtype p) ≤ ⟨y, hy⟩ ↔ x ≤ y := Iff.rfl @[simp] theorem mk_lt_mk [LT α] {p : α → Prop} {x y : α} {hx : p x} {hy : p y} : (⟨x, hx⟩ : Subtype p) < ⟨y, hy⟩ ↔ x < y := Iff.rfl @[simp, norm_cast] theorem coe_le_coe [LE α] {p : α → Prop} {x y : Subtype p} : (x : α) ≤ y ↔ x ≤ y := Iff.rfl @[gcongr] alias ⟨_, GCongr.coe_le_coe⟩ := coe_le_coe @[simp, norm_cast] theorem coe_lt_coe [LT α] {p : α → Prop} {x y : Subtype p} : (x : α) < y ↔ x < y := Iff.rfl @[gcongr] alias ⟨_, GCongr.coe_lt_coe⟩ := coe_lt_coe instance preorder [Preorder α] (p : α → Prop) : Preorder (Subtype p) := Preorder.lift (fun (a : Subtype p) ↦ (a : α)) instance partialOrder [PartialOrder α] (p : α → Prop) : PartialOrder (Subtype p) := PartialOrder.lift (fun (a : Subtype p) ↦ (a : α)) Subtype.coe_injective instance decidableLE [Preorder α] [h : DecidableLE α] {p : α → Prop} : DecidableLE (Subtype p) := fun a b ↦ h a b instance decidableLT [Preorder α] [h : DecidableLT α] {p : α → Prop} : DecidableLT (Subtype p) := fun a b ↦ h a b /-- A subtype of a linear order is a linear order. We explicitly give the proofs of decidable equality and decidable order in order to ensure the decidability instances are all definitionally equal. -/ instance instLinearOrder [LinearOrder α] (p : α → Prop) : LinearOrder (Subtype p) := @LinearOrder.lift (Subtype p) _ _ ⟨fun x y ↦ ⟨max x y, max_rec' _ x.2 y.2⟩⟩ ⟨fun x y ↦ ⟨min x y, min_rec' _ x.2 y.2⟩⟩ (fun (a : Subtype p) ↦ (a : α)) Subtype.coe_injective (fun _ _ ↦ rfl) fun _ _ ↦ rfl end Subtype /-! ### Pointwise order on `α × β` The lexicographic order is defined in `Data.Prod.Lex`, and the instances are available via the type synonym `α ×ₗ β = α × β`. -/ namespace Prod section LE variable [LE α] [LE β] {x y : α × β} {a a₁ a₂ : α} {b b₁ b₂ : β} instance : LE (α × β) where le p q := p.1 ≤ q.1 ∧ p.2 ≤ q.2 instance instDecidableLE [Decidable (x.1 ≤ y.1)] [Decidable (x.2 ≤ y.2)] : Decidable (x ≤ y) := inferInstanceAs (Decidable (x.1 ≤ y.1 ∧ x.2 ≤ y.2)) lemma le_def : x ≤ y ↔ x.1 ≤ y.1 ∧ x.2 ≤ y.2 := .rfl @[simp] lemma mk_le_mk : (a₁, b₁) ≤ (a₂, b₂) ↔ a₁ ≤ a₂ ∧ b₁ ≤ b₂ := .rfl @[simp] lemma swap_le_swap : x.swap ≤ y.swap ↔ x ≤ y := and_comm @[simp] lemma swap_le_mk : x.swap ≤ (b, a) ↔ x ≤ (a, b) := and_comm @[simp] lemma mk_le_swap : (b, a) ≤ x.swap ↔ (a, b) ≤ x := and_comm end LE section Preorder variable [Preorder α] [Preorder β] {a a₁ a₂ : α} {b b₁ b₂ : β} {x y : α × β} instance : Preorder (α × β) where __ := inferInstanceAs (LE (α × β)) le_refl := fun ⟨a, b⟩ ↦ ⟨le_refl a, le_refl b⟩ le_trans := fun ⟨_, _⟩ ⟨_, _⟩ ⟨_, _⟩ ⟨hac, hbd⟩ ⟨hce, hdf⟩ ↦ ⟨le_trans hac hce, le_trans hbd hdf⟩ @[simp] theorem swap_lt_swap : x.swap < y.swap ↔ x < y := and_congr swap_le_swap (not_congr swap_le_swap) @[simp] lemma swap_lt_mk : x.swap < (b, a) ↔ x < (a, b) := by rw [← swap_lt_swap]; simp @[simp] lemma mk_lt_swap : (b, a) < x.swap ↔ (a, b) < x := by rw [← swap_lt_swap]; simp theorem mk_le_mk_iff_left : (a₁, b) ≤ (a₂, b) ↔ a₁ ≤ a₂ := and_iff_left le_rfl theorem mk_le_mk_iff_right : (a, b₁) ≤ (a, b₂) ↔ b₁ ≤ b₂ := and_iff_right le_rfl theorem mk_lt_mk_iff_left : (a₁, b) < (a₂, b) ↔ a₁ < a₂ := lt_iff_lt_of_le_iff_le' mk_le_mk_iff_left mk_le_mk_iff_left theorem mk_lt_mk_iff_right : (a, b₁) < (a, b₂) ↔ b₁ < b₂ := lt_iff_lt_of_le_iff_le' mk_le_mk_iff_right mk_le_mk_iff_right theorem lt_iff : x < y ↔ x.1 < y.1 ∧ x.2 ≤ y.2 ∨ x.1 ≤ y.1 ∧ x.2 < y.2 := by refine ⟨fun h ↦ ?_, ?_⟩ · by_cases h₁ : y.1 ≤ x.1 · exact Or.inr ⟨h.1.1, LE.le.lt_of_not_le h.1.2 fun h₂ ↦ h.2 ⟨h₁, h₂⟩⟩ · exact Or.inl ⟨LE.le.lt_of_not_le h.1.1 h₁, h.1.2⟩ · rintro (⟨h₁, h₂⟩ | ⟨h₁, h₂⟩) · exact ⟨⟨h₁.le, h₂⟩, fun h ↦ h₁.not_le h.1⟩ · exact ⟨⟨h₁, h₂.le⟩, fun h ↦ h₂.not_le h.2⟩ @[simp] theorem mk_lt_mk : (a₁, b₁) < (a₂, b₂) ↔ a₁ < a₂ ∧ b₁ ≤ b₂ ∨ a₁ ≤ a₂ ∧ b₁ < b₂ := lt_iff protected lemma lt_of_lt_of_le (h₁ : x.1 < y.1) (h₂ : x.2 ≤ y.2) : x < y := by simp [lt_iff, *] protected lemma lt_of_le_of_lt (h₁ : x.1 ≤ y.1) (h₂ : x.2 < y.2) : x < y := by simp [lt_iff, *] lemma mk_lt_mk_of_lt_of_le (h₁ : a₁ < a₂) (h₂ : b₁ ≤ b₂) : (a₁, b₁) < (a₂, b₂) := by simp [lt_iff, *] lemma mk_lt_mk_of_le_of_lt (h₁ : a₁ ≤ a₂) (h₂ : b₁ < b₂) : (a₁, b₁) < (a₂, b₂) := by simp [lt_iff, *] end Preorder /-- The pointwise partial order on a product. (The lexicographic ordering is defined in `Order.Lexicographic`, and the instances are available via the type synonym `α ×ₗ β = α × β`.) -/ instance instPartialOrder (α β : Type*) [PartialOrder α] [PartialOrder β] : PartialOrder (α × β) where __ := inferInstanceAs (Preorder (α × β)) le_antisymm := fun _ _ ⟨hac, hbd⟩ ⟨hca, hdb⟩ ↦ Prod.ext (hac.antisymm hca) (hbd.antisymm hdb) end Prod /-! ### Additional order classes -/ /-- An order is dense if there is an element between any pair of distinct comparable elements. -/ class DenselyOrdered (α : Type*) [LT α] : Prop where /-- An order is dense if there is an element between any pair of distinct elements. -/ dense : ∀ a₁ a₂ : α, a₁ < a₂ → ∃ a, a₁ < a ∧ a < a₂ theorem exists_between [LT α] [DenselyOrdered α] : ∀ {a₁ a₂ : α}, a₁ < a₂ → ∃ a, a₁ < a ∧ a < a₂ := DenselyOrdered.dense _ _ instance OrderDual.denselyOrdered (α : Type*) [LT α] [h : DenselyOrdered α] : DenselyOrdered αᵒᵈ := ⟨fun _ _ ha ↦ (@exists_between α _ h _ _ ha).imp fun _ ↦ And.symm⟩ @[simp] theorem denselyOrdered_orderDual [LT α] : DenselyOrdered αᵒᵈ ↔ DenselyOrdered α := ⟨by convert @OrderDual.denselyOrdered αᵒᵈ _, @OrderDual.denselyOrdered α _⟩ /-- Any ordered subsingleton is densely ordered. Not an instance to avoid a heavy subsingleton typeclass search. -/ lemma Subsingleton.instDenselyOrdered {X : Type*} [Subsingleton X] [Preorder X] : DenselyOrdered X := ⟨fun _ _ h ↦ (not_lt_of_subsingleton h).elim⟩ instance [Preorder α] [Preorder β] [DenselyOrdered α] [DenselyOrdered β] : DenselyOrdered (α × β) := ⟨fun a b ↦ by simp_rw [Prod.lt_iff] rintro (⟨h₁, h₂⟩ | ⟨h₁, h₂⟩) · obtain ⟨c, ha, hb⟩ := exists_between h₁ exact ⟨(c, _), Or.inl ⟨ha, h₂⟩, Or.inl ⟨hb, le_rfl⟩⟩ · obtain ⟨c, ha, hb⟩ := exists_between h₂ exact ⟨(_, c), Or.inr ⟨h₁, ha⟩, Or.inr ⟨le_rfl, hb⟩⟩⟩ instance [∀ i, Preorder (π i)] [∀ i, DenselyOrdered (π i)] : DenselyOrdered (∀ i, π i) := ⟨fun a b ↦ by classical simp_rw [Pi.lt_def] rintro ⟨hab, i, hi⟩ obtain ⟨c, ha, hb⟩ := exists_between hi exact ⟨Function.update a i c, ⟨le_update_iff.2 ⟨ha.le, fun _ _ ↦ le_rfl⟩, i, by rwa [update_self]⟩, update_le_iff.2 ⟨hb.le, fun _ _ ↦ hab _⟩, i, by rwa [update_self]⟩⟩ section LinearOrder variable [LinearOrder α] [DenselyOrdered α] {a₁ a₂ : α} theorem le_of_forall_gt_imp_ge_of_dense (h : ∀ a, a₂ < a → a₁ ≤ a) : a₁ ≤ a₂ := le_of_not_gt fun ha ↦ let ⟨a, ha₁, ha₂⟩ := exists_between ha lt_irrefl a <| lt_of_lt_of_le ‹a < a₁› (h _ ‹a₂ < a›) lemma forall_gt_imp_ge_iff_le_of_dense : (∀ a, a₂ < a → a₁ ≤ a) ↔ a₁ ≤ a₂ := ⟨le_of_forall_gt_imp_ge_of_dense, fun ha _a ha₂ ↦ ha.trans ha₂.le⟩ lemma eq_of_le_of_forall_lt_imp_le_of_dense (h₁ : a₂ ≤ a₁) (h₂ : ∀ a, a₂ < a → a₁ ≤ a) : a₁ = a₂ := le_antisymm (le_of_forall_gt_imp_ge_of_dense h₂) h₁ theorem le_of_forall_lt_imp_le_of_dense (h : ∀ a < a₁, a ≤ a₂) : a₁ ≤ a₂ := le_of_not_gt fun ha ↦ let ⟨a, ha₁, ha₂⟩ := exists_between ha lt_irrefl a <| lt_of_le_of_lt (h _ ‹a < a₁›) ‹a₂ < a› lemma forall_lt_imp_le_iff_le_of_dense : (∀ a < a₁, a ≤ a₂) ↔ a₁ ≤ a₂ := ⟨le_of_forall_lt_imp_le_of_dense, fun ha _a ha₁ ↦ ha₁.le.trans ha⟩ theorem eq_of_le_of_forall_gt_imp_ge_of_dense (h₁ : a₂ ≤ a₁) (h₂ : ∀ a < a₁, a ≤ a₂) : a₁ = a₂ := (le_of_forall_lt_imp_le_of_dense h₂).antisymm h₁ @[deprecated (since := "2025-01-21")] alias le_of_forall_le_of_dense := le_of_forall_gt_imp_ge_of_dense @[deprecated (since := "2025-01-21")] alias le_of_forall_ge_of_dense := le_of_forall_lt_imp_le_of_dense @[deprecated (since := "2025-01-21")] alias forall_lt_le_iff := forall_lt_imp_le_iff_le_of_dense @[deprecated (since := "2025-01-21")] alias forall_gt_ge_iff := forall_gt_imp_ge_iff_le_of_dense @[deprecated (since := "2025-01-21")] alias eq_of_le_of_forall_le_of_dense := eq_of_le_of_forall_lt_imp_le_of_dense @[deprecated (since := "2025-01-21")] alias eq_of_le_of_forall_ge_of_dense := eq_of_le_of_forall_gt_imp_ge_of_dense end LinearOrder theorem dense_or_discrete [LinearOrder α] (a₁ a₂ : α) : (∃ a, a₁ < a ∧ a < a₂) ∨ (∀ a, a₁ < a → a₂ ≤ a) ∧ ∀ a < a₂, a ≤ a₁ := or_iff_not_imp_left.2 fun h ↦ ⟨fun a ha₁ ↦ le_of_not_gt fun ha₂ ↦ h ⟨a, ha₁, ha₂⟩, fun a ha₂ ↦ le_of_not_gt fun ha₁ ↦ h ⟨a, ha₁, ha₂⟩⟩ /-- If a linear order has no elements `x < y < z`, then it has at most two elements. -/ lemma eq_or_eq_or_eq_of_forall_not_lt_lt [LinearOrder α] (h : ∀ ⦃x y z : α⦄, x < y → y < z → False) (x y z : α) : x = y ∨ y = z ∨ x = z := by by_contra hne simp only [not_or, ← Ne.eq_def] at hne rcases hne.1.lt_or_lt with h₁ | h₁ <;> rcases hne.2.1.lt_or_lt with h₂ | h₂ <;> rcases hne.2.2.lt_or_lt with h₃ | h₃ exacts [h h₁ h₂, h h₂ h₃, h h₃ h₂, h h₃ h₁, h h₁ h₃, h h₂ h₃, h h₁ h₃, h h₂ h₁] namespace PUnit variable (a b : PUnit) instance instLinearOrder : LinearOrder PUnit where le := fun _ _ ↦ True lt := fun _ _ ↦ False max := fun _ _ ↦ unit min := fun _ _ ↦ unit toDecidableEq := inferInstance toDecidableLE := fun _ _ ↦ Decidable.isTrue trivial toDecidableLT := fun _ _ ↦ Decidable.isFalse id le_refl := by intros; trivial le_trans := by intros; trivial le_total := by intros; exact Or.inl trivial le_antisymm := by intros; rfl lt_iff_le_not_le := by simp only [not_true, and_false, forall_const] theorem max_eq : max a b = unit := rfl theorem min_eq : min a b = unit := rfl protected theorem le : a ≤ b := trivial theorem not_lt : ¬a < b := not_false instance : DenselyOrdered PUnit := ⟨fun _ _ ↦ False.elim⟩ end PUnit section «Prop» /-- Propositions form a complete boolean algebra, where the `≤` relation is given by implication. -/ instance Prop.le : LE Prop := ⟨(· → ·)⟩ @[simp] theorem le_Prop_eq : ((· ≤ ·) : Prop → Prop → Prop) = (· → ·) := rfl theorem subrelation_iff_le {r s : α → α → Prop} : Subrelation r s ↔ r ≤ s := Iff.rfl instance Prop.partialOrder : PartialOrder Prop where __ := Prop.le le_refl _ := id le_trans _ _ _ f g := g ∘ f le_antisymm _ _ Hab Hba := propext ⟨Hab, Hba⟩ end «Prop» /-! ### Linear order from a total partial order -/ /-- Type synonym to create an instance of `LinearOrder` from a `PartialOrder` and `IsTotal α (≤)` -/ def AsLinearOrder (α : Type*) := α instance [Inhabited α] : Inhabited (AsLinearOrder α) := ⟨(default : α)⟩ noncomputable instance AsLinearOrder.linearOrder [PartialOrder α] [IsTotal α (· ≤ ·)] : LinearOrder (AsLinearOrder α) where __ := inferInstanceAs (PartialOrder α) le_total := @total_of α (· ≤ ·) _ toDecidableLE := Classical.decRel _
Mathlib/Order/Basic.lean
1,556
1,557
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau -/ import Mathlib.Data.DFinsupp.Submonoid import Mathlib.Data.Finsupp.ToDFinsupp import Mathlib.LinearAlgebra.Finsupp.SumProd import Mathlib.LinearAlgebra.LinearIndependent.Lemmas /-! # Properties of the module `Π₀ i, M i` Given an indexed collection of `R`-modules `M i`, the `R`-module structure on `Π₀ i, M i` is defined in `Mathlib.Data.DFinsupp.Module`. In this file we define `LinearMap` versions of various maps: * `DFinsupp.lsingle a : M →ₗ[R] Π₀ i, M i`: `DFinsupp.single a` as a linear map; * `DFinsupp.lmk s : (Π i : (↑s : Set ι), M i) →ₗ[R] Π₀ i, M i`: `DFinsupp.mk` as a linear map; * `DFinsupp.lapply i : (Π₀ i, M i) →ₗ[R] M`: the map `fun f ↦ f i` as a linear map; * `DFinsupp.lsum`: `DFinsupp.sum` or `DFinsupp.liftAddHom` as a `LinearMap`. ## Implementation notes This file should try to mirror `LinearAlgebra.Finsupp` where possible. The API of `Finsupp` is much more developed, but many lemmas in that file should be eligible to copy over. ## Tags function with finite support, module, linear algebra -/ variable {ι : Type*} {R : Type*} {S : Type*} {M : ι → Type*} {N : Type*} namespace DFinsupp variable [Semiring R] [∀ i, AddCommMonoid (M i)] [∀ i, Module R (M i)] variable [AddCommMonoid N] [Module R N] section DecidableEq variable [DecidableEq ι] /-- `DFinsupp.mk` as a `LinearMap`. -/ def lmk (s : Finset ι) : (∀ i : (↑s : Set ι), M i) →ₗ[R] Π₀ i, M i where toFun := mk s map_add' _ _ := mk_add map_smul' c x := mk_smul c x /-- `DFinsupp.single` as a `LinearMap` -/ def lsingle (i) : M i →ₗ[R] Π₀ i, M i := { DFinsupp.singleAddHom _ _ with toFun := single i map_smul' := single_smul } /-- Two `R`-linear maps from `Π₀ i, M i` which agree on each `single i x` agree everywhere. -/ theorem lhom_ext ⦃φ ψ : (Π₀ i, M i) →ₗ[R] N⦄ (h : ∀ i x, φ (single i x) = ψ (single i x)) : φ = ψ := LinearMap.toAddMonoidHom_injective <| addHom_ext h /-- Two `R`-linear maps from `Π₀ i, M i` which agree on each `single i x` agree everywhere. See note [partially-applied ext lemmas]. After applying this lemma, if `M = R` then it suffices to verify `φ (single a 1) = ψ (single a 1)`. -/ @[ext 1100] theorem lhom_ext' ⦃φ ψ : (Π₀ i, M i) →ₗ[R] N⦄ (h : ∀ i, φ.comp (lsingle i) = ψ.comp (lsingle i)) : φ = ψ := lhom_ext fun i => LinearMap.congr_fun (h i) theorem lmk_apply (s : Finset ι) (x) : (lmk s : _ →ₗ[R] Π₀ i, M i) x = mk s x := rfl @[simp] theorem lsingle_apply (i : ι) (x : M i) : (lsingle i : (M i) →ₗ[R] _) x = single i x := rfl end DecidableEq /-- Interpret `fun (f : Π₀ i, M i) ↦ f i` as a linear map. -/ def lapply (i : ι) : (Π₀ i, M i) →ₗ[R] M i where toFun f := f i map_add' f g := add_apply f g i map_smul' c f := smul_apply c f i @[simp] theorem lapply_apply (i : ι) (f : Π₀ i, M i) : (lapply i : (Π₀ i, M i) →ₗ[R] _) f = f i := rfl theorem injective_pi_lapply : Function.Injective (LinearMap.pi (R := R) <| lapply (M := M)) := fun _ _ h ↦ ext fun _ ↦ congr_fun h _ @[simp] theorem lapply_comp_lsingle_same [DecidableEq ι] (i : ι) : lapply i ∘ₗ lsingle i = (.id : M i →ₗ[R] M i) := by ext; simp @[simp] theorem lapply_comp_lsingle_of_ne [DecidableEq ι] (i i' : ι) (h : i ≠ i') : lapply i ∘ₗ lsingle i' = (0 : M i' →ₗ[R] M i) := by ext; simp [h.symm] section Lsum variable (S) variable [DecidableEq ι] instance {R : Type*} {S : Type*} [Semiring R] [Semiring S] (σ : R →+* S) {σ' : S →+* R} [RingHomInvPair σ σ'] [RingHomInvPair σ' σ] (M : Type*) (M₂ : Type*) [AddCommMonoid M] [AddCommMonoid M₂] [Module R M] [Module S M₂] : EquivLike (LinearEquiv σ M M₂) M M₂ := inferInstance /-- The `DFinsupp` version of `Finsupp.lsum`. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def lsum [Semiring S] [Module S N] [SMulCommClass R S N] : (∀ i, M i →ₗ[R] N) ≃ₗ[S] (Π₀ i, M i) →ₗ[R] N where toFun F := { toFun := sumAddHom fun i => (F i).toAddMonoidHom map_add' := (DFinsupp.liftAddHom fun (i : ι) => (F i).toAddMonoidHom).map_add map_smul' := fun c f => by dsimp apply DFinsupp.induction f · rw [smul_zero, AddMonoidHom.map_zero, smul_zero] · intro a b f _ _ hf rw [smul_add, AddMonoidHom.map_add, AddMonoidHom.map_add, smul_add, hf, ← single_smul, sumAddHom_single, sumAddHom_single, LinearMap.toAddMonoidHom_coe, LinearMap.map_smul] } invFun F i := F.comp (lsingle i) left_inv F := by ext simp right_inv F := by refine DFinsupp.lhom_ext' (fun i ↦ ?_) ext simp map_add' F G := by refine DFinsupp.lhom_ext' (fun i ↦ ?_) ext simp map_smul' c F := by refine DFinsupp.lhom_ext' (fun i ↦ ?_) ext simp /-- While `simp` can prove this, it is often convenient to avoid unfolding `lsum` into `sumAddHom` with `DFinsupp.lsum_apply_apply`. -/ theorem lsum_single [Semiring S] [Module S N] [SMulCommClass R S N] (F : ∀ i, M i →ₗ[R] N) (i) (x : M i) : lsum S F (single i x) = F i x := by simp theorem lsum_lsingle [Semiring S] [∀ i, Module S (M i)] [∀ i, SMulCommClass R S (M i)] : lsum S (lsingle (R := R) (M := M)) = .id := lhom_ext (lsum_single _ _) theorem iSup_range_lsingle : ⨆ i, LinearMap.range (lsingle (R := R) (M := M) i) = ⊤ := top_le_iff.mp fun m _ ↦ by rw [← LinearMap.id_apply (R := R) m, ← lsum_lsingle ℕ] exact dfinsuppSumAddHom_mem _ _ _ fun i _ ↦ Submodule.mem_iSup_of_mem i ⟨_, rfl⟩ end Lsum /-! ### Bundled versions of `DFinsupp.mapRange` The names should match the equivalent bundled `Finsupp.mapRange` definitions. -/ section mapRange variable {β β₁ β₂ : ι → Type*} section AddCommMonoid variable [∀ i, AddCommMonoid (β i)] [∀ i, AddCommMonoid (β₁ i)] [∀ i, AddCommMonoid (β₂ i)] variable [∀ i, Module R (β i)] [∀ i, Module R (β₁ i)] [∀ i, Module R (β₂ i)] lemma mker_mapRangeAddMonoidHom (f : ∀ i, β₁ i →+ β₂ i) : AddMonoidHom.mker (mapRange.addMonoidHom f) = (AddSubmonoid.pi Set.univ (fun i ↦ AddMonoidHom.mker (f i))).comap coeFnAddMonoidHom := by ext simp [AddSubmonoid.pi, DFinsupp.ext_iff] lemma mrange_mapRangeAddMonoidHom (f : ∀ i, β₁ i →+ β₂ i) : AddMonoidHom.mrange (mapRange.addMonoidHom f) = (AddSubmonoid.pi Set.univ (fun i ↦ AddMonoidHom.mrange (f i))).comap coeFnAddMonoidHom := by classical ext x simp only [AddSubmonoid.mem_comap, mapRange.addMonoidHom_apply, coeFnAddMonoidHom_apply] refine ⟨fun ⟨y, hy⟩ i hi ↦ ?_, fun h ↦ ?_⟩ · simp [← hy] · choose g hg using fun i => h i (Set.mem_univ _) use DFinsupp.mk x.support (g ·) ext i simp only [Finset.coe_sort_coe, mapRange.addMonoidHom_apply, mapRange_apply] by_cases mem : i ∈ x.support · rw [mk_of_mem mem, hg] · rw [DFinsupp.not_mem_support_iff.mp mem, mk_of_not_mem mem, map_zero] theorem mapRange_smul (f : ∀ i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (r : R) (hf' : ∀ i x, f i (r • x) = r • f i x) (g : Π₀ i, β₁ i) : mapRange f hf (r • g) = r • mapRange f hf g := by ext simp only [mapRange_apply f, coe_smul, Pi.smul_apply, hf'] /-- `DFinsupp.mapRange` as a `LinearMap`. -/ @[simps! apply] def mapRange.linearMap (f : ∀ i, β₁ i →ₗ[R] β₂ i) : (Π₀ i, β₁ i) →ₗ[R] Π₀ i, β₂ i := { mapRange.addMonoidHom fun i => (f i).toAddMonoidHom with toFun := mapRange (fun i x => f i x) fun i => (f i).map_zero map_smul' := fun r => mapRange_smul _ (fun i => (f i).map_zero) _ fun i => (f i).map_smul r } @[simp] theorem mapRange.linearMap_id : (mapRange.linearMap fun i => (LinearMap.id : β₂ i →ₗ[R] _)) = LinearMap.id := by ext simp [linearMap] theorem mapRange.linearMap_comp (f : ∀ i, β₁ i →ₗ[R] β₂ i) (f₂ : ∀ i, β i →ₗ[R] β₁ i) : (mapRange.linearMap fun i => (f i).comp (f₂ i)) = (mapRange.linearMap f).comp (mapRange.linearMap f₂) := LinearMap.ext <| mapRange_comp (fun i x => f i x) (fun i x => f₂ i x) (fun i => (f i).map_zero) (fun i => (f₂ i).map_zero) (by simp) theorem sum_mapRange_index.linearMap [DecidableEq ι] {f : ∀ i, β₁ i →ₗ[R] β₂ i} {h : ∀ i, β₂ i →ₗ[R] N} {l : Π₀ i, β₁ i} : DFinsupp.lsum ℕ h (mapRange.linearMap f l) = DFinsupp.lsum ℕ (fun i => (h i).comp (f i)) l := by classical simpa [DFinsupp.sumAddHom_apply] using sum_mapRange_index fun i => by simp lemma ker_mapRangeLinearMap (f : ∀ i, β₁ i →ₗ[R] β₂ i) : LinearMap.ker (mapRange.linearMap f) = (Submodule.pi Set.univ (fun i ↦ LinearMap.ker (f i))).comap (coeFnLinearMap R) := Submodule.toAddSubmonoid_injective <| mker_mapRangeAddMonoidHom (f · |>.toAddMonoidHom) lemma range_mapRangeLinearMap (f : ∀ i, β₁ i →ₗ[R] β₂ i) : LinearMap.range (mapRange.linearMap f) = (Submodule.pi Set.univ (LinearMap.range <| f ·)).comap (coeFnLinearMap R) := Submodule.toAddSubmonoid_injective <| mrange_mapRangeAddMonoidHom (f · |>.toAddMonoidHom) /-- `DFinsupp.mapRange.linearMap` as a `LinearEquiv`. -/ @[simps apply] def mapRange.linearEquiv (e : ∀ i, β₁ i ≃ₗ[R] β₂ i) : (Π₀ i, β₁ i) ≃ₗ[R] Π₀ i, β₂ i := { mapRange.addEquiv fun i => (e i).toAddEquiv, mapRange.linearMap fun i => (e i).toLinearMap with toFun := mapRange (fun i x => e i x) fun i => (e i).map_zero invFun := mapRange (fun i x => (e i).symm x) fun i => (e i).symm.map_zero } @[simp] theorem mapRange.linearEquiv_refl : (mapRange.linearEquiv fun i => LinearEquiv.refl R (β₁ i)) = LinearEquiv.refl _ _ := LinearEquiv.ext mapRange_id theorem mapRange.linearEquiv_trans (f : ∀ i, β i ≃ₗ[R] β₁ i) (f₂ : ∀ i, β₁ i ≃ₗ[R] β₂ i) : (mapRange.linearEquiv fun i => (f i).trans (f₂ i)) = (mapRange.linearEquiv f).trans (mapRange.linearEquiv f₂) := LinearEquiv.ext <| mapRange_comp (fun i x => f₂ i x) (fun i x => f i x) (fun i => (f₂ i).map_zero) (fun i => (f i).map_zero) (by simp) @[simp] theorem mapRange.linearEquiv_symm (e : ∀ i, β₁ i ≃ₗ[R] β₂ i) : (mapRange.linearEquiv e).symm = mapRange.linearEquiv fun i => (e i).symm := rfl end AddCommMonoid section AddCommGroup lemma ker_mapRangeAddMonoidHom [∀ i, AddCommGroup (β₁ i)] [∀ i, AddCommMonoid (β₂ i)] (f : ∀ i, β₁ i →+ β₂ i) : (mapRange.addMonoidHom f).ker = (AddSubgroup.pi Set.univ (f · |>.ker)).comap coeFnAddMonoidHom := AddSubgroup.toAddSubmonoid_injective <| mker_mapRangeAddMonoidHom f lemma range_mapRangeAddMonoidHom [∀ i, AddCommGroup (β₁ i)] [∀ i, AddCommGroup (β₂ i)] (f : ∀ i, β₂ i →+ β₁ i) : (mapRange.addMonoidHom f).range = (AddSubgroup.pi Set.univ (f · |>.range)).comap coeFnAddMonoidHom := AddSubgroup.toAddSubmonoid_injective <| mrange_mapRangeAddMonoidHom f end AddCommGroup end mapRange section CoprodMap variable [DecidableEq ι] /-- Given a family of linear maps `f i : M i →ₗ[R] N`, we can form a linear map `(Π₀ i, M i) →ₗ[R] N` which sends `x : Π₀ i, M i` to the sum over `i` of `f i` applied to `x i`. This is the map coming from the universal property of `Π₀ i, M i` as the coproduct of the `M i`. See also `LinearMap.coprod` for the binary product version. -/ def coprodMap (f : ∀ i : ι, M i →ₗ[R] N) : (Π₀ i, M i) →ₗ[R] N := (DFinsupp.lsum ℕ fun _ : ι => LinearMap.id) ∘ₗ DFinsupp.mapRange.linearMap f theorem coprodMap_apply [∀ x : N, Decidable (x ≠ 0)] (f : ∀ i : ι, M i →ₗ[R] N) (x : Π₀ i, M i) : coprodMap f x = DFinsupp.sum (mapRange (fun i => f i) (fun _ => LinearMap.map_zero _) x) fun _ => id := DFinsupp.sumAddHom_apply _ _ theorem coprodMap_apply_single (f : ∀ i : ι, M i →ₗ[R] N) (i : ι) (x : M i) : coprodMap f (single i x) = f i x := by simp [coprodMap] end CoprodMap end DFinsupp namespace Submodule variable [Semiring R] [AddCommMonoid N] [Module R N] open DFinsupp section DecidableEq variable [DecidableEq ι] theorem dfinsuppSum_mem {β : ι → Type*} [∀ i, Zero (β i)] [∀ (i) (x : β i), Decidable (x ≠ 0)] (S : Submodule R N) (f : Π₀ i, β i) (g : ∀ i, β i → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ S) : f.sum g ∈ S := _root_.dfinsuppSum_mem S f g h @[deprecated (since := "2025-04-06")] alias dfinsupp_sum_mem := dfinsuppSum_mem theorem dfinsuppSumAddHom_mem {β : ι → Type*} [∀ i, AddZeroClass (β i)] (S : Submodule R N) (f : Π₀ i, β i) (g : ∀ i, β i →+ N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ S) : DFinsupp.sumAddHom g f ∈ S := _root_.dfinsuppSumAddHom_mem S f g h @[deprecated (since := "2025-04-06")] alias dfinsupp_sumAddHom_mem := dfinsuppSumAddHom_mem /-- The supremum of a family of submodules is equal to the range of `DFinsupp.lsum`; that is every element in the `iSup` can be produced from taking a finite number of non-zero elements of `p i`, coercing them to `N`, and summing them. -/ theorem iSup_eq_range_dfinsupp_lsum (p : ι → Submodule R N) : iSup p = LinearMap.range (DFinsupp.lsum ℕ fun i => (p i).subtype) := by apply le_antisymm · apply iSup_le _ intro i y hy simp only [LinearMap.mem_range, lsum_apply_apply] exact ⟨DFinsupp.single i ⟨y, hy⟩, DFinsupp.sumAddHom_single _ _ _⟩ · rintro x ⟨v, rfl⟩ exact dfinsuppSumAddHom_mem _ v _ fun i _ => (le_iSup p i : p i ≤ _) (v i).2 /-- The bounded supremum of a family of commutative additive submonoids is equal to the range of `DFinsupp.sumAddHom` composed with `DFinsupp.filter_add_monoid_hom`; that is, every element in the bounded `iSup` can be produced from taking a finite number of non-zero elements from the `S i` that satisfy `p i`, coercing them to `γ`, and summing them. -/ theorem biSup_eq_range_dfinsupp_lsum (p : ι → Prop) [DecidablePred p] (S : ι → Submodule R N) : ⨆ (i) (_ : p i), S i = LinearMap.range (LinearMap.comp (DFinsupp.lsum ℕ (fun i => (S i).subtype)) (DFinsupp.filterLinearMap R _ p)) := by apply le_antisymm · refine iSup₂_le fun i hi y hy => ⟨DFinsupp.single i ⟨y, hy⟩, ?_⟩ rw [LinearMap.comp_apply, filterLinearMap_apply, filter_single_pos _ _ hi] simp only [lsum_apply_apply, sumAddHom_single, LinearMap.toAddMonoidHom_coe, coe_subtype] · rintro x ⟨v, rfl⟩ refine dfinsuppSumAddHom_mem _ _ _ fun i _ => ?_ refine mem_iSup_of_mem i ?_ by_cases hp : p i · simp [hp] · simp [hp] /-- A characterisation of the span of a family of submodules. See also `Submodule.mem_iSup_iff_exists_finsupp`. -/ theorem mem_iSup_iff_exists_dfinsupp (p : ι → Submodule R N) (x : N) : x ∈ iSup p ↔ ∃ f : Π₀ i, p i, DFinsupp.lsum ℕ (fun i => (p i).subtype) f = x := SetLike.ext_iff.mp (iSup_eq_range_dfinsupp_lsum p) x /-- A variant of `Submodule.mem_iSup_iff_exists_dfinsupp` with the RHS fully unfolded. See also `Submodule.mem_iSup_iff_exists_finsupp`. -/ theorem mem_iSup_iff_exists_dfinsupp' (p : ι → Submodule R N) [∀ (i) (x : p i), Decidable (x ≠ 0)] (x : N) : x ∈ iSup p ↔ ∃ f : Π₀ i, p i, (f.sum fun _ xi => ↑xi) = x := by rw [mem_iSup_iff_exists_dfinsupp] simp_rw [DFinsupp.lsum_apply_apply, DFinsupp.sumAddHom_apply, LinearMap.toAddMonoidHom_coe, coe_subtype] theorem mem_biSup_iff_exists_dfinsupp (p : ι → Prop) [DecidablePred p] (S : ι → Submodule R N) (x : N) : (x ∈ ⨆ (i) (_ : p i), S i) ↔ ∃ f : Π₀ i, S i, DFinsupp.lsum ℕ (fun i => (S i).subtype) (f.filter p) = x := SetLike.ext_iff.mp (biSup_eq_range_dfinsupp_lsum p S) x end DecidableEq lemma mem_iSup_iff_exists_finsupp (p : ι → Submodule R N) (x : N) : x ∈ iSup p ↔ ∃ (f : ι →₀ N), (∀ i, f i ∈ p i) ∧ (f.sum fun _i xi ↦ xi) = x := by classical rw [mem_iSup_iff_exists_dfinsupp'] refine ⟨fun ⟨f, hf⟩ ↦ ⟨⟨f.support, fun i ↦ (f i : N), by simp⟩, by simp, hf⟩, ?_⟩ rintro ⟨f, hf, rfl⟩ refine ⟨DFinsupp.mk f.support fun i ↦ ⟨f i, hf i⟩, Finset.sum_congr ?_ fun i hi ↦ ?_⟩ · ext; simp [mk_eq_zero] · simp [Finsupp.mem_support_iff.mp hi] theorem mem_iSup_finset_iff_exists_sum {s : Finset ι} (p : ι → Submodule R N) (a : N) : (a ∈ ⨆ i ∈ s, p i) ↔ ∃ μ : ∀ i, p i, (∑ i ∈ s, (μ i : N)) = a := by classical rw [Submodule.mem_iSup_iff_exists_dfinsupp'] constructor <;> rintro ⟨μ, hμ⟩ · use fun i => ⟨μ i, (iSup_const_le : _ ≤ p i) (coe_mem <| μ i)⟩ rw [← hμ] symm apply Finset.sum_subset · intro x contrapose intro hx rw [mem_support_iff, not_ne_iff] ext rw [coe_zero, ← mem_bot R] suffices ⊥ = ⨆ (_ : x ∈ s), p x from this.symm ▸ coe_mem (μ x) exact (iSup_neg hx).symm · intro x _ hx rw [mem_support_iff, not_ne_iff] at hx rw [hx] rfl · refine ⟨DFinsupp.mk s ?_, ?_⟩ · rintro ⟨i, hi⟩ refine ⟨μ i, ?_⟩ rw [iSup_pos] · exact coe_mem _ · exact hi simp only [DFinsupp.sum] rw [Finset.sum_subset support_mk_subset, ← hμ] · exact Finset.sum_congr rfl fun x hx => by rw [mk_of_mem hx] · intro x _ hx rw [mem_support_iff, not_ne_iff] at hx rw [hx] rfl end Submodule open DFinsupp section Semiring variable [DecidableEq ι] [Semiring R] [AddCommMonoid N] [Module R N] /-- Independence of a family of submodules can be expressed as a quantifier over `DFinsupp`s. This is an intermediate result used to prove `iSupIndep_of_dfinsupp_lsum_injective` and `iSupIndep.dfinsupp_lsum_injective`. -/ theorem iSupIndep_iff_forall_dfinsupp (p : ι → Submodule R N) : iSupIndep p ↔ ∀ (i) (x : p i) (v : Π₀ i : ι, ↥(p i)), lsum ℕ (fun i => (p i).subtype) (erase i v) = x → x = 0 := by simp_rw [iSupIndep_def, Submodule.disjoint_def, Submodule.mem_biSup_iff_exists_dfinsupp, exists_imp, filter_ne_eq_erase] refine forall_congr' fun i => Subtype.forall'.trans ?_ simp_rw [Submodule.coe_eq_zero] @[deprecated (since := "2024-11-24")] alias independent_iff_forall_dfinsupp := iSupIndep_iff_forall_dfinsupp /- If `DFinsupp.lsum` applied with `Submodule.subtype` is injective then the submodules are iSupIndep. -/ theorem iSupIndep_of_dfinsupp_lsum_injective (p : ι → Submodule R N) (h : Function.Injective (lsum ℕ fun i => (p i).subtype)) : iSupIndep p := by rw [iSupIndep_iff_forall_dfinsupp] intro i x v hv replace hv : lsum ℕ (fun i => (p i).subtype) (erase i v) = lsum ℕ (fun i => (p i).subtype) (single i x) := by simpa only [lsum_single] using hv have := DFunLike.ext_iff.mp (h hv) i simpa [eq_comm] using this @[deprecated (since := "2024-11-24")] alias independent_of_dfinsupp_lsum_injective := iSupIndep_of_dfinsupp_lsum_injective /- If `DFinsupp.sumAddHom` applied with `AddSubmonoid.subtype` is injective then the additive submonoids are independent. -/ theorem iSupIndep_of_dfinsuppSumAddHom_injective (p : ι → AddSubmonoid N) (h : Function.Injective (sumAddHom fun i => (p i).subtype)) : iSupIndep p := by
rw [← iSupIndep_map_orderIso_iff (AddSubmonoid.toNatSubmodule : AddSubmonoid N ≃o _)] exact iSupIndep_of_dfinsupp_lsum_injective _ h @[deprecated (since := "2025-04-06")]
Mathlib/LinearAlgebra/DFinsupp.lean
482
485
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.CharP.Algebra import Mathlib.Data.Multiset.Fintype import Mathlib.FieldTheory.IsAlgClosed.Basic import Mathlib.FieldTheory.SplittingField.Construction /-! # Algebraic Closure In this file we construct the algebraic closure of a field ## Main Definitions - `AlgebraicClosure k` is an algebraic closure of `k` (in the same universe). It is constructed by taking the polynomial ring generated by indeterminates $X_{f,1}, \dots, X_{f,\deg f}$ corresponding to roots of monic irreducible polynomials `f` with coefficients in `k`, and quotienting out by a maximal ideal containing every $f - \prod_i (X - X_{f,i})$. The proof follows https://kconrad.math.uconn.edu/blurbs/galoistheory/algclosureshorter.pdf. ## Tags algebraic closure, algebraically closed -/ universe u v w noncomputable section open Polynomial variable (k : Type u) [Field k] namespace AlgebraicClosure /-- The subtype of monic polynomials. -/ def Monics : Type u := {f : k[X] // f.Monic} /-- `Vars k` provides `n` variables $X_{f,1}, \dots, X_{f,n}$ for each monic polynomial `f : k[X]` of degree `n`. -/ def Vars : Type u := Σ f : Monics k, Fin f.1.natDegree variable {k} in /-- Given a monic polynomial `f : k[X]`, `subProdXSubC f` is the polynomial $f - \prod_i (X - X_{f,i})$. -/ def subProdXSubC (f : Monics k) : (MvPolynomial (Vars k) k)[X] := f.1.map (algebraMap _ _) - ∏ i : Fin f.1.natDegree, (X - C (MvPolynomial.X ⟨f, i⟩)) /-- The span of all coefficients of `subProdXSubC f` as `f` ranges all polynomials in `k[X]`. -/ def spanCoeffs : Ideal (MvPolynomial (Vars k) k) := Ideal.span <| Set.range fun fn : Monics k × ℕ ↦ (subProdXSubC fn.1).coeff fn.2 variable {k} /-- If a monic polynomial `f : k[X]` splits in `K`, then it has as many roots (counting multiplicity) as its degree. -/ def finEquivRoots {K} [Field K] [DecidableEq K] {i : k →+* K} {f : Monics k} (hf : f.1.Splits i) : Fin f.1.natDegree ≃ (f.1.map i).roots.toEnumFinset := .symm <| Finset.equivFinOfCardEq <| by rwa [← splits_id_iff_splits, splits_iff_card_roots, ← Multiset.card_toEnumFinset, f.2.natDegree_map] at hf lemma Monics.splits_finsetProd {s : Finset (Monics k)} {f : Monics k} (hf : f ∈ s) : f.1.Splits (algebraMap k (SplittingField (∏ f ∈ s, f.1))) := (splits_prod_iff _ fun j _ ↦ j.2.ne_zero).1 (SplittingField.splits _) _ hf open Classical in /-- Given a finite set of monic polynomials, construct an algebra homomorphism to the splitting field of the product of the polynomials sending indeterminates $X_{f_i}$ to the distinct roots of `f`. -/ def toSplittingField (s : Finset (Monics k)) : MvPolynomial (Vars k) k →ₐ[k] SplittingField (∏ f ∈ s, f.1) :=
MvPolynomial.aeval fun fi ↦ if hf : fi.1 ∈ s then (finEquivRoots (Monics.splits_finsetProd hf) fi.2).1.1 else 37 theorem toSplittingField_coeff {s : Finset (Monics k)} {f} (h : f ∈ s) (n) : toSplittingField s ((subProdXSubC f).coeff n) = 0 := by
Mathlib/FieldTheory/IsAlgClosed/AlgebraicClosure.lean
77
81
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Heather Macbeth -/ import Mathlib.Analysis.InnerProductSpace.TwoDim import Mathlib.Geometry.Euclidean.Angle.Unoriented.Basic /-! # Oriented angles. This file defines oriented angles in real inner product spaces. ## Main definitions * `Orientation.oangle` is the oriented angle between two vectors with respect to an orientation. ## Implementation notes The definitions here use the `Real.angle` type, angles modulo `2 * π`. For some purposes, angles modulo `π` are more convenient, because results are true for such angles with less configuration dependence. Results that are only equalities modulo `π` can be represented modulo `2 * π` as equalities of `(2 : ℤ) • θ`. ## References * Evan Chen, Euclidean Geometry in Mathematical Olympiads. -/ noncomputable section open Module Complex open scoped Real RealInnerProductSpace ComplexConjugate namespace Orientation attribute [local instance] Complex.finrank_real_complex_fact variable {V V' : Type*} variable [NormedAddCommGroup V] [NormedAddCommGroup V'] variable [InnerProductSpace ℝ V] [InnerProductSpace ℝ V'] variable [Fact (finrank ℝ V = 2)] [Fact (finrank ℝ V' = 2)] (o : Orientation ℝ V (Fin 2)) local notation "ω" => o.areaForm /-- The oriented angle from `x` to `y`, modulo `2 * π`. If either vector is 0, this is 0. See `InnerProductGeometry.angle` for the corresponding unoriented angle definition. -/ def oangle (x y : V) : Real.Angle := Complex.arg (o.kahler x y) /-- Oriented angles are continuous when the vectors involved are nonzero. -/ @[fun_prop] theorem continuousAt_oangle {x : V × V} (hx1 : x.1 ≠ 0) (hx2 : x.2 ≠ 0) : ContinuousAt (fun y : V × V => o.oangle y.1 y.2) x := by refine (Complex.continuousAt_arg_coe_angle ?_).comp ?_ · exact o.kahler_ne_zero hx1 hx2 exact ((continuous_ofReal.comp continuous_inner).add ((continuous_ofReal.comp o.areaForm'.continuous₂).mul continuous_const)).continuousAt /-- If the first vector passed to `oangle` is 0, the result is 0. -/ @[simp] theorem oangle_zero_left (x : V) : o.oangle 0 x = 0 := by simp [oangle] /-- If the second vector passed to `oangle` is 0, the result is 0. -/ @[simp] theorem oangle_zero_right (x : V) : o.oangle x 0 = 0 := by simp [oangle] /-- If the two vectors passed to `oangle` are the same, the result is 0. -/ @[simp] theorem oangle_self (x : V) : o.oangle x x = 0 := by rw [oangle, kahler_apply_self, ← ofReal_pow] convert QuotientAddGroup.mk_zero (AddSubgroup.zmultiples (2 * π)) apply arg_ofReal_of_nonneg positivity /-- If the angle between two vectors is nonzero, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : x ≠ 0 := by rintro rfl; simp at h /-- If the angle between two vectors is nonzero, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : y ≠ 0 := by rintro rfl; simp at h /-- If the angle between two vectors is nonzero, the vectors are not equal. -/ theorem ne_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : x ≠ y := by rintro rfl; simp at h /-- If the angle between two vectors is `π`, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π`, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π`, the vectors are not equal. -/ theorem ne_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the vectors are not equal. -/ theorem ne_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the vectors are not equal. -/ theorem ne_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ Real.Angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the sign of the angle between two vectors is nonzero, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (Real.Angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is nonzero, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (Real.Angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is nonzero, the vectors are not equal. -/ theorem ne_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : x ≠ y := o.ne_of_oangle_ne_zero (Real.Angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is positive, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : x ≠ 0 := o.left_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is positive, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : y ≠ 0 := o.right_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is positive, the vectors are not equal. -/ theorem ne_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : x ≠ y := o.ne_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : x ≠ 0 := o.left_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : y ≠ 0 := o.right_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the vectors are not equal. -/ theorem ne_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : x ≠ y := o.ne_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- Swapping the two vectors passed to `oangle` negates the angle. -/ theorem oangle_rev (x y : V) : o.oangle y x = -o.oangle x y := by simp only [oangle, o.kahler_swap y x, Complex.arg_conj_coe_angle] /-- Adding the angles between two vectors in each order results in 0. -/ @[simp] theorem oangle_add_oangle_rev (x y : V) : o.oangle x y + o.oangle y x = 0 := by simp [o.oangle_rev y x] /-- Negating the first vector passed to `oangle` adds `π` to the angle. -/ theorem oangle_neg_left {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : o.oangle (-x) y = o.oangle x y + π := by simp only [oangle, map_neg] convert Complex.arg_neg_coe_angle _ exact o.kahler_ne_zero hx hy /-- Negating the second vector passed to `oangle` adds `π` to the angle. -/ theorem oangle_neg_right {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : o.oangle x (-y) = o.oangle x y + π := by simp only [oangle, map_neg] convert Complex.arg_neg_coe_angle _ exact o.kahler_ne_zero hx hy /-- Negating the first vector passed to `oangle` does not change twice the angle. -/ @[simp] theorem two_zsmul_oangle_neg_left (x y : V) : (2 : ℤ) • o.oangle (-x) y = (2 : ℤ) • o.oangle x y := by by_cases hx : x = 0 · simp [hx] · by_cases hy : y = 0 · simp [hy] · simp [o.oangle_neg_left hx hy] /-- Negating the second vector passed to `oangle` does not change twice the angle. -/ @[simp] theorem two_zsmul_oangle_neg_right (x y : V) : (2 : ℤ) • o.oangle x (-y) = (2 : ℤ) • o.oangle x y := by by_cases hx : x = 0 · simp [hx] · by_cases hy : y = 0 · simp [hy] · simp [o.oangle_neg_right hx hy] /-- Negating both vectors passed to `oangle` does not change the angle. -/ @[simp] theorem oangle_neg_neg (x y : V) : o.oangle (-x) (-y) = o.oangle x y := by simp [oangle] /-- Negating the first vector produces the same angle as negating the second vector. -/ theorem oangle_neg_left_eq_neg_right (x y : V) : o.oangle (-x) y = o.oangle x (-y) := by rw [← neg_neg y, oangle_neg_neg, neg_neg] /-- The angle between the negation of a nonzero vector and that vector is `π`. -/ @[simp] theorem oangle_neg_self_left {x : V} (hx : x ≠ 0) : o.oangle (-x) x = π := by simp [oangle_neg_left, hx] /-- The angle between a nonzero vector and its negation is `π`. -/ @[simp] theorem oangle_neg_self_right {x : V} (hx : x ≠ 0) : o.oangle x (-x) = π := by simp [oangle_neg_right, hx] /-- Twice the angle between the negation of a vector and that vector is 0. -/ theorem two_zsmul_oangle_neg_self_left (x : V) : (2 : ℤ) • o.oangle (-x) x = 0 := by by_cases hx : x = 0 <;> simp [hx] /-- Twice the angle between a vector and its negation is 0. -/ theorem two_zsmul_oangle_neg_self_right (x : V) : (2 : ℤ) • o.oangle x (-x) = 0 := by by_cases hx : x = 0 <;> simp [hx] /-- Adding the angles between two vectors in each order, with the first vector in each angle negated, results in 0. -/ @[simp] theorem oangle_add_oangle_rev_neg_left (x y : V) : o.oangle (-x) y + o.oangle (-y) x = 0 := by rw [oangle_neg_left_eq_neg_right, oangle_rev, neg_add_cancel] /-- Adding the angles between two vectors in each order, with the second vector in each angle negated, results in 0. -/ @[simp] theorem oangle_add_oangle_rev_neg_right (x y : V) : o.oangle x (-y) + o.oangle y (-x) = 0 := by rw [o.oangle_rev (-x), oangle_neg_left_eq_neg_right, add_neg_cancel] /-- Multiplying the first vector passed to `oangle` by a positive real does not change the angle. -/ @[simp] theorem oangle_smul_left_of_pos (x y : V) {r : ℝ} (hr : 0 < r) : o.oangle (r • x) y = o.oangle x y := by simp [oangle, Complex.arg_real_mul _ hr] /-- Multiplying the second vector passed to `oangle` by a positive real does not change the angle. -/ @[simp] theorem oangle_smul_right_of_pos (x y : V) {r : ℝ} (hr : 0 < r) : o.oangle x (r • y) = o.oangle x y := by simp [oangle, Complex.arg_real_mul _ hr] /-- Multiplying the first vector passed to `oangle` by a negative real produces the same angle
as negating that vector. -/ @[simp]
Mathlib/Geometry/Euclidean/Angle/Oriented/Basic.lean
259
260
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth -/ import Mathlib.Topology.Homeomorph.Defs import Mathlib.Topology.Order.LeftRightNhds /-! # Continuity of monotone functions In this file we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuousWithinAt_of_monotoneOn_of_image_mem_nhds`, as well as several similar facts. We also prove that an `OrderIso` is continuous. ## Tags continuous, monotone -/ open Set Filter open Topology section LinearOrder variable {α β : Type*} [LinearOrder α] [TopologicalSpace α] [OrderTopology α] variable [LinearOrder β] [TopologicalSpace β] [OrderTopology β] /-- If `f` is a function strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ theorem StrictMonoOn.continuousWithinAt_right_of_exists_between {f : α → β} {s : Set α} {a : α} (h_mono : StrictMonoOn f s) (hs : s ∈ 𝓝[≥] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : ContinuousWithinAt f (Ici a) a := by have ha : a ∈ Ici a := left_mem_Ici have has : a ∈ s := mem_of_mem_nhdsWithin ha hs refine tendsto_order.2 ⟨fun b hb => ?_, fun b hb => ?_⟩ · filter_upwards [hs, @self_mem_nhdsWithin _ _ a (Ici a)] with _ hxs hxa using hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) · rcases hfs b hb with ⟨c, hcs, hac, hcb⟩ rw [h_mono.lt_iff_lt has hcs] at hac filter_upwards [hs, Ico_mem_nhdsGE hac] rintro x hx ⟨_, hxc⟩ exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb /-- If `f` is a monotone function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/ theorem continuousWithinAt_right_of_monotoneOn_of_exists_between {f : α → β} {s : Set α} {a : α} (h_mono : MonotoneOn f s) (hs : s ∈ 𝓝[≥] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : ContinuousWithinAt f (Ici a) a := by
have ha : a ∈ Ici a := left_mem_Ici have has : a ∈ s := mem_of_mem_nhdsWithin ha hs refine tendsto_order.2 ⟨fun b hb => ?_, fun b hb => ?_⟩ · filter_upwards [hs, @self_mem_nhdsWithin _ _ a (Ici a)] with _ hxs hxa using hb.trans_le (h_mono has hxs hxa) · rcases hfs b hb with ⟨c, hcs, hac, hcb⟩ have : a < c := not_le.1 fun h => hac.not_le <| h_mono hcs has h filter_upwards [hs, Ico_mem_nhdsGE this] rintro x hx ⟨_, hxc⟩ exact (h_mono hx hcs hxc.le).trans_lt hcb /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f`
Mathlib/Topology/Order/MonotoneContinuity.lean
63
75
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import Mathlib.Algebra.Algebra.Subalgebra.Lattice import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.MonoidAlgebra.Basic import Mathlib.Algebra.MonoidAlgebra.Support import Mathlib.Algebra.Regular.Pow import Mathlib.Data.Finsupp.Antidiagonal import Mathlib.Order.SymmDiff /-! # Multivariate polynomials This file defines polynomial rings over a base ring (or even semiring), with variables from a general type `σ` (which could be infinite). ## Important definitions Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary type. This file creates the type `MvPolynomial σ R`, which mathematicians might denote $R[X_i : i \in σ]$. It is the type of multivariate (a.k.a. multivariable) polynomials, with variables corresponding to the terms in `σ`, and coefficients in `R`. ### Notation In the definitions below, we use the following notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[CommSemiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : MvPolynomial σ R` ### Definitions * `MvPolynomial σ R` : the type of polynomials with variables of type `σ` and coefficients in the commutative semiring `R` * `monomial s a` : the monomial which mathematically would be denoted `a * X^s` * `C a` : the constant polynomial with value `a` * `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`. * `coeff s p` : the coefficient of `s` in `p`. ## Implementation notes Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`. The definition of `MvPolynomial σ R` is `(σ →₀ ℕ) →₀ R`; here `σ →₀ ℕ` denotes the space of all monomials in the variables, and the function to `R` sends a monomial to its coefficient in the polynomial being represented. ## Tags polynomial, multivariate polynomial, multivariable polynomial -/ noncomputable section open Set Function Finsupp AddMonoidAlgebra open scoped Pointwise universe u v w x variable {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `R` is the coefficient ring -/ def MvPolynomial (σ : Type*) (R : Type*) [CommSemiring R] := AddMonoidAlgebra R (σ →₀ ℕ) namespace MvPolynomial -- Porting note: because of `MvPolynomial.C` and `MvPolynomial.X` this linter throws -- tons of warnings in this file, and it's easier to just disable them globally in the file variable {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section CommSemiring section Instances instance decidableEqMvPolynomial [CommSemiring R] [DecidableEq σ] [DecidableEq R] : DecidableEq (MvPolynomial σ R) := Finsupp.instDecidableEq instance commSemiring [CommSemiring R] : CommSemiring (MvPolynomial σ R) := AddMonoidAlgebra.commSemiring instance inhabited [CommSemiring R] : Inhabited (MvPolynomial σ R) := ⟨0⟩ instance distribuMulAction [Monoid R] [CommSemiring S₁] [DistribMulAction R S₁] : DistribMulAction R (MvPolynomial σ S₁) := AddMonoidAlgebra.distribMulAction instance smulZeroClass [CommSemiring S₁] [SMulZeroClass R S₁] : SMulZeroClass R (MvPolynomial σ S₁) := AddMonoidAlgebra.smulZeroClass instance faithfulSMul [CommSemiring S₁] [SMulZeroClass R S₁] [FaithfulSMul R S₁] : FaithfulSMul R (MvPolynomial σ S₁) := AddMonoidAlgebra.faithfulSMul instance module [Semiring R] [CommSemiring S₁] [Module R S₁] : Module R (MvPolynomial σ S₁) := AddMonoidAlgebra.module instance isScalarTower [CommSemiring S₂] [SMul R S₁] [SMulZeroClass R S₂] [SMulZeroClass S₁ S₂] [IsScalarTower R S₁ S₂] : IsScalarTower R S₁ (MvPolynomial σ S₂) := AddMonoidAlgebra.isScalarTower instance smulCommClass [CommSemiring S₂] [SMulZeroClass R S₂] [SMulZeroClass S₁ S₂] [SMulCommClass R S₁ S₂] : SMulCommClass R S₁ (MvPolynomial σ S₂) := AddMonoidAlgebra.smulCommClass instance isCentralScalar [CommSemiring S₁] [SMulZeroClass R S₁] [SMulZeroClass Rᵐᵒᵖ S₁] [IsCentralScalar R S₁] : IsCentralScalar R (MvPolynomial σ S₁) := AddMonoidAlgebra.isCentralScalar instance algebra [CommSemiring R] [CommSemiring S₁] [Algebra R S₁] : Algebra R (MvPolynomial σ S₁) := AddMonoidAlgebra.algebra instance isScalarTower_right [CommSemiring S₁] [DistribSMul R S₁] [IsScalarTower R S₁ S₁] : IsScalarTower R (MvPolynomial σ S₁) (MvPolynomial σ S₁) := AddMonoidAlgebra.isScalarTower_self _ instance smulCommClass_right [CommSemiring S₁] [DistribSMul R S₁] [SMulCommClass R S₁ S₁] : SMulCommClass R (MvPolynomial σ S₁) (MvPolynomial σ S₁) := AddMonoidAlgebra.smulCommClass_self _ /-- If `R` is a subsingleton, then `MvPolynomial σ R` has a unique element -/ instance unique [CommSemiring R] [Subsingleton R] : Unique (MvPolynomial σ R) := AddMonoidAlgebra.unique end Instances variable [CommSemiring R] [CommSemiring S₁] {p q : MvPolynomial σ R} /-- `monomial s a` is the monomial with coefficient `a` and exponents given by `s` -/ def monomial (s : σ →₀ ℕ) : R →ₗ[R] MvPolynomial σ R := AddMonoidAlgebra.lsingle s theorem one_def : (1 : MvPolynomial σ R) = monomial 0 1 := rfl theorem single_eq_monomial (s : σ →₀ ℕ) (a : R) : Finsupp.single s a = monomial s a := rfl theorem mul_def : p * q = p.sum fun m a => q.sum fun n b => monomial (m + n) (a * b) := AddMonoidAlgebra.mul_def /-- `C a` is the constant polynomial with value `a` -/ def C : R →+* MvPolynomial σ R := { singleZeroRingHom with toFun := monomial 0 } variable (R σ) @[simp] theorem algebraMap_eq : algebraMap R (MvPolynomial σ R) = C := rfl variable {R σ} /-- `X n` is the degree `1` monomial $X_n$. -/ def X (n : σ) : MvPolynomial σ R := monomial (Finsupp.single n 1) 1 theorem monomial_left_injective {r : R} (hr : r ≠ 0) : Function.Injective fun s : σ →₀ ℕ => monomial s r := Finsupp.single_left_injective hr @[simp] theorem monomial_left_inj {s t : σ →₀ ℕ} {r : R} (hr : r ≠ 0) : monomial s r = monomial t r ↔ s = t := Finsupp.single_left_inj hr theorem C_apply : (C a : MvPolynomial σ R) = monomial 0 a := rfl @[simp] theorem C_0 : C 0 = (0 : MvPolynomial σ R) := map_zero _ @[simp] theorem C_1 : C 1 = (1 : MvPolynomial σ R) := rfl theorem C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by -- Porting note: this `show` feels like defeq abuse, but I can't find the appropriate lemmas show AddMonoidAlgebra.single _ _ * AddMonoidAlgebra.single _ _ = AddMonoidAlgebra.single _ _ simp [C_apply, single_mul_single] @[simp] theorem C_add : (C (a + a') : MvPolynomial σ R) = C a + C a' := Finsupp.single_add _ _ _ @[simp] theorem C_mul : (C (a * a') : MvPolynomial σ R) = C a * C a' := C_mul_monomial.symm @[simp] theorem C_pow (a : R) (n : ℕ) : (C (a ^ n) : MvPolynomial σ R) = C a ^ n := map_pow _ _ _ theorem C_injective (σ : Type*) (R : Type*) [CommSemiring R] : Function.Injective (C : R → MvPolynomial σ R) := Finsupp.single_injective _ theorem C_surjective {R : Type*} [CommSemiring R] (σ : Type*) [IsEmpty σ] : Function.Surjective (C : R → MvPolynomial σ R) := by refine fun p => ⟨p.toFun 0, Finsupp.ext fun a => ?_⟩ simp only [C_apply, ← single_eq_monomial, (Finsupp.ext isEmptyElim (α := σ) : a = 0), single_eq_same] rfl @[simp] theorem C_inj {σ : Type*} (R : Type*) [CommSemiring R] (r s : R) : (C r : MvPolynomial σ R) = C s ↔ r = s := (C_injective σ R).eq_iff @[simp] lemma C_eq_zero : (C a : MvPolynomial σ R) = 0 ↔ a = 0 := by rw [← map_zero C, C_inj] lemma C_ne_zero : (C a : MvPolynomial σ R) ≠ 0 ↔ a ≠ 0 := C_eq_zero.ne instance nontrivial_of_nontrivial (σ : Type*) (R : Type*) [CommSemiring R] [Nontrivial R] : Nontrivial (MvPolynomial σ R) := inferInstanceAs (Nontrivial <| AddMonoidAlgebra R (σ →₀ ℕ)) instance infinite_of_infinite (σ : Type*) (R : Type*) [CommSemiring R] [Infinite R] : Infinite (MvPolynomial σ R) := Infinite.of_injective C (C_injective _ _) instance infinite_of_nonempty (σ : Type*) (R : Type*) [Nonempty σ] [CommSemiring R] [Nontrivial R] : Infinite (MvPolynomial σ R) := Infinite.of_injective ((fun s : σ →₀ ℕ => monomial s 1) ∘ Finsupp.single (Classical.arbitrary σ)) <| (monomial_left_injective one_ne_zero).comp (Finsupp.single_injective _) theorem C_eq_coe_nat (n : ℕ) : (C ↑n : MvPolynomial σ R) = n := by induction n <;> simp [*] theorem C_mul' : MvPolynomial.C a * p = a • p := (Algebra.smul_def a p).symm theorem smul_eq_C_mul (p : MvPolynomial σ R) (a : R) : a • p = C a * p := C_mul'.symm theorem C_eq_smul_one : (C a : MvPolynomial σ R) = a • (1 : MvPolynomial σ R) := by rw [← C_mul', mul_one] theorem smul_monomial {S₁ : Type*} [SMulZeroClass S₁ R] (r : S₁) : r • monomial s a = monomial s (r • a) := Finsupp.smul_single _ _ _ theorem X_injective [Nontrivial R] : Function.Injective (X : σ → MvPolynomial σ R) := (monomial_left_injective one_ne_zero).comp (Finsupp.single_left_injective one_ne_zero) @[simp] theorem X_inj [Nontrivial R] (m n : σ) : X m = (X n : MvPolynomial σ R) ↔ m = n := X_injective.eq_iff theorem monomial_pow : monomial s a ^ e = monomial (e • s) (a ^ e) := AddMonoidAlgebra.single_pow e @[simp] theorem monomial_mul {s s' : σ →₀ ℕ} {a b : R} : monomial s a * monomial s' b = monomial (s + s') (a * b) := AddMonoidAlgebra.single_mul_single variable (σ R) /-- `fun s ↦ monomial s 1` as a homomorphism. -/ def monomialOneHom : Multiplicative (σ →₀ ℕ) →* MvPolynomial σ R := AddMonoidAlgebra.of _ _ variable {σ R} @[simp] theorem monomialOneHom_apply : monomialOneHom R σ s = (monomial s 1 : MvPolynomial σ R) := rfl theorem X_pow_eq_monomial : X n ^ e = monomial (Finsupp.single n e) (1 : R) := by simp [X, monomial_pow] theorem monomial_add_single : monomial (s + Finsupp.single n e) a = monomial s a * X n ^ e := by rw [X_pow_eq_monomial, monomial_mul, mul_one] theorem monomial_single_add : monomial (Finsupp.single n e + s) a = X n ^ e * monomial s a := by rw [X_pow_eq_monomial, monomial_mul, one_mul] theorem C_mul_X_pow_eq_monomial {s : σ} {a : R} {n : ℕ} : C a * X s ^ n = monomial (Finsupp.single s n) a := by rw [← zero_add (Finsupp.single s n), monomial_add_single, C_apply] theorem C_mul_X_eq_monomial {s : σ} {a : R} : C a * X s = monomial (Finsupp.single s 1) a := by rw [← C_mul_X_pow_eq_monomial, pow_one] @[simp] theorem monomial_zero {s : σ →₀ ℕ} : monomial s (0 : R) = 0 := Finsupp.single_zero _ @[simp] theorem monomial_zero' : (monomial (0 : σ →₀ ℕ) : R → MvPolynomial σ R) = C := rfl @[simp] theorem monomial_eq_zero {s : σ →₀ ℕ} {b : R} : monomial s b = 0 ↔ b = 0 := Finsupp.single_eq_zero @[simp] theorem sum_monomial_eq {A : Type*} [AddCommMonoid A] {u : σ →₀ ℕ} {r : R} {b : (σ →₀ ℕ) → R → A} (w : b u 0 = 0) : sum (monomial u r) b = b u r := Finsupp.sum_single_index w @[simp] theorem sum_C {A : Type*} [AddCommMonoid A] {b : (σ →₀ ℕ) → R → A} (w : b 0 0 = 0) : sum (C a) b = b 0 a := sum_monomial_eq w theorem monomial_sum_one {α : Type*} (s : Finset α) (f : α → σ →₀ ℕ) : (monomial (∑ i ∈ s, f i) 1 : MvPolynomial σ R) = ∏ i ∈ s, monomial (f i) 1 := map_prod (monomialOneHom R σ) (fun i => Multiplicative.ofAdd (f i)) s theorem monomial_sum_index {α : Type*} (s : Finset α) (f : α → σ →₀ ℕ) (a : R) : monomial (∑ i ∈ s, f i) a = C a * ∏ i ∈ s, monomial (f i) 1 := by rw [← monomial_sum_one, C_mul', ← (monomial _).map_smul, smul_eq_mul, mul_one] theorem monomial_finsupp_sum_index {α β : Type*} [Zero β] (f : α →₀ β) (g : α → β → σ →₀ ℕ) (a : R) : monomial (f.sum g) a = C a * f.prod fun a b => monomial (g a b) 1 := monomial_sum_index _ _ _ theorem monomial_eq_monomial_iff {α : Type*} (a₁ a₂ : α →₀ ℕ) (b₁ b₂ : R) : monomial a₁ b₁ = monomial a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ ∨ b₁ = 0 ∧ b₂ = 0 := Finsupp.single_eq_single_iff _ _ _ _ theorem monomial_eq : monomial s a = C a * (s.prod fun n e => X n ^ e : MvPolynomial σ R) := by simp only [X_pow_eq_monomial, ← monomial_finsupp_sum_index, Finsupp.sum_single] @[simp] lemma prod_X_pow_eq_monomial : ∏ x ∈ s.support, X x ^ s x = monomial s (1 : R) := by simp only [monomial_eq, map_one, one_mul, Finsupp.prod] @[elab_as_elim] theorem induction_on_monomial {motive : MvPolynomial σ R → Prop} (C : ∀ a, motive (C a)) (mul_X : ∀ p n, motive p → motive (p * X n)) : ∀ s a, motive (monomial s a) := by intro s a apply @Finsupp.induction σ ℕ _ _ s · show motive (monomial 0 a) exact C a · intro n e p _hpn _he ih have : ∀ e : ℕ, motive (monomial p a * X n ^ e) := by intro e induction e with | zero => simp [ih] | succ e e_ih => simp [ih, pow_succ, (mul_assoc _ _ _).symm, mul_X, e_ih] simp [add_comm, monomial_add_single, this] /-- Analog of `Polynomial.induction_on'`. To prove something about mv_polynomials, it suffices to show the condition is closed under taking sums, and it holds for monomials. -/ @[elab_as_elim] theorem induction_on' {P : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (monomial : ∀ (u : σ →₀ ℕ) (a : R), P (monomial u a)) (add : ∀ p q : MvPolynomial σ R, P p → P q → P (p + q)) : P p := Finsupp.induction p (suffices P (MvPolynomial.monomial 0 0) by rwa [monomial_zero] at this show P (MvPolynomial.monomial 0 0) from monomial 0 0) fun _ _ _ _ha _hb hPf => add _ _ (monomial _ _) hPf /-- Similar to `MvPolynomial.induction_on` but only a weak form of `h_add` is required. In particular, this version only requires us to show that `motive` is closed under addition of nontrivial monomials not present in the support. -/ @[elab_as_elim] theorem monomial_add_induction_on {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (C : ∀ a, motive (C a)) (monomial_add : ∀ (a : σ →₀ ℕ) (b : R) (f : MvPolynomial σ R), a ∉ f.support → b ≠ 0 → motive f → motive ((monomial a b) + f)) : motive p := Finsupp.induction p (C_0.rec <| C 0) monomial_add @[deprecated (since := "2025-03-11")] alias induction_on''' := monomial_add_induction_on /-- Similar to `MvPolynomial.induction_on` but only a yet weaker form of `h_add` is required. In particular, this version only requires us to show that `motive` is closed under addition of monomials not present in the support for which `motive` is already known to hold. -/ theorem induction_on'' {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (C : ∀ a, motive (C a)) (monomial_add : ∀ (a : σ →₀ ℕ) (b : R) (f : MvPolynomial σ R), a ∉ f.support → b ≠ 0 → motive f → motive (monomial a b) → motive ((monomial a b) + f)) (mul_X : ∀ (p : MvPolynomial σ R) (n : σ), motive p → motive (p * MvPolynomial.X n)) : motive p := monomial_add_induction_on p C fun a b f ha hb hf => monomial_add a b f ha hb hf <| induction_on_monomial C mul_X a b /-- Analog of `Polynomial.induction_on`. If a property holds for any constant polynomial and is preserved under addition and multiplication by variables then it holds for all multivariate polynomials. -/ @[recursor 5] theorem induction_on {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (C : ∀ a, motive (C a)) (add : ∀ p q, motive p → motive q → motive (p + q)) (mul_X : ∀ p n, motive p → motive (p * X n)) : motive p := induction_on'' p C (fun a b f _ha _hb hf hm => add (monomial a b) f hm hf) mul_X theorem ringHom_ext {A : Type*} [Semiring A] {f g : MvPolynomial σ R →+* A} (hC : ∀ r, f (C r) = g (C r)) (hX : ∀ i, f (X i) = g (X i)) : f = g := by refine AddMonoidAlgebra.ringHom_ext' ?_ ?_ -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): this has high priority, but Lean still chooses `RingHom.ext`, why? -- probably because of the type synonym · ext x exact hC _ · apply Finsupp.mulHom_ext'; intros x -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `Finsupp.mulHom_ext'` needs to have increased priority apply MonoidHom.ext_mnat exact hX _ /-- See note [partially-applied ext lemmas]. -/ @[ext 1100] theorem ringHom_ext' {A : Type*} [Semiring A] {f g : MvPolynomial σ R →+* A} (hC : f.comp C = g.comp C) (hX : ∀ i, f (X i) = g (X i)) : f = g := ringHom_ext (RingHom.ext_iff.1 hC) hX theorem hom_eq_hom [Semiring S₂] (f g : MvPolynomial σ R →+* S₂) (hC : f.comp C = g.comp C) (hX : ∀ n : σ, f (X n) = g (X n)) (p : MvPolynomial σ R) : f p = g p := RingHom.congr_fun (ringHom_ext' hC hX) p theorem is_id (f : MvPolynomial σ R →+* MvPolynomial σ R) (hC : f.comp C = C) (hX : ∀ n : σ, f (X n) = X n) (p : MvPolynomial σ R) : f p = p := hom_eq_hom f (RingHom.id _) hC hX p @[ext 1100] theorem algHom_ext' {A B : Type*} [CommSemiring A] [CommSemiring B] [Algebra R A] [Algebra R B] {f g : MvPolynomial σ A →ₐ[R] B} (h₁ : f.comp (IsScalarTower.toAlgHom R A (MvPolynomial σ A)) = g.comp (IsScalarTower.toAlgHom R A (MvPolynomial σ A))) (h₂ : ∀ i, f (X i) = g (X i)) : f = g := AlgHom.coe_ringHom_injective (MvPolynomial.ringHom_ext' (congr_arg AlgHom.toRingHom h₁) h₂) @[ext 1200] theorem algHom_ext {A : Type*} [Semiring A] [Algebra R A] {f g : MvPolynomial σ R →ₐ[R] A} (hf : ∀ i : σ, f (X i) = g (X i)) : f = g := AddMonoidAlgebra.algHom_ext' (mulHom_ext' fun X : σ => MonoidHom.ext_mnat (hf X)) @[simp] theorem algHom_C {A : Type*} [Semiring A] [Algebra R A] (f : MvPolynomial σ R →ₐ[R] A) (r : R) : f (C r) = algebraMap R A r := f.commutes r @[simp] theorem adjoin_range_X : Algebra.adjoin R (range (X : σ → MvPolynomial σ R)) = ⊤ := by set S := Algebra.adjoin R (range (X : σ → MvPolynomial σ R)) refine top_unique fun p hp => ?_; clear hp induction p using MvPolynomial.induction_on with | C => exact S.algebraMap_mem _ | add p q hp hq => exact S.add_mem hp hq | mul_X p i hp => exact S.mul_mem hp (Algebra.subset_adjoin <| mem_range_self _) @[ext] theorem linearMap_ext {M : Type*} [AddCommMonoid M] [Module R M] {f g : MvPolynomial σ R →ₗ[R] M} (h : ∀ s, f ∘ₗ monomial s = g ∘ₗ monomial s) : f = g := Finsupp.lhom_ext' h section Support /-- The finite set of all `m : σ →₀ ℕ` such that `X^m` has a non-zero coefficient. -/ def support (p : MvPolynomial σ R) : Finset (σ →₀ ℕ) := Finsupp.support p theorem finsupp_support_eq_support (p : MvPolynomial σ R) : Finsupp.support p = p.support := rfl theorem support_monomial [h : Decidable (a = 0)] : (monomial s a).support = if a = 0 then ∅ else {s} := by rw [← Subsingleton.elim (Classical.decEq R a 0) h] rfl theorem support_monomial_subset : (monomial s a).support ⊆ {s} := support_single_subset theorem support_add [DecidableEq σ] : (p + q).support ⊆ p.support ∪ q.support := Finsupp.support_add theorem support_X [Nontrivial R] : (X n : MvPolynomial σ R).support = {Finsupp.single n 1} := by classical rw [X, support_monomial, if_neg]; exact one_ne_zero theorem support_X_pow [Nontrivial R] (s : σ) (n : ℕ) : (X s ^ n : MvPolynomial σ R).support = {Finsupp.single s n} := by classical rw [X_pow_eq_monomial, support_monomial, if_neg (one_ne_zero' R)] @[simp] theorem support_zero : (0 : MvPolynomial σ R).support = ∅ := rfl theorem support_smul {S₁ : Type*} [SMulZeroClass S₁ R] {a : S₁} {f : MvPolynomial σ R} : (a • f).support ⊆ f.support := Finsupp.support_smul theorem support_sum {α : Type*} [DecidableEq σ] {s : Finset α} {f : α → MvPolynomial σ R} : (∑ x ∈ s, f x).support ⊆ s.biUnion fun x => (f x).support := Finsupp.support_finset_sum end Support section Coeff /-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/ def coeff (m : σ →₀ ℕ) (p : MvPolynomial σ R) : R := @DFunLike.coe ((σ →₀ ℕ) →₀ R) _ _ _ p m @[simp] theorem mem_support_iff {p : MvPolynomial σ R} {m : σ →₀ ℕ} : m ∈ p.support ↔ p.coeff m ≠ 0 := by simp [support, coeff] theorem not_mem_support_iff {p : MvPolynomial σ R} {m : σ →₀ ℕ} : m ∉ p.support ↔ p.coeff m = 0 := by simp theorem sum_def {A} [AddCommMonoid A] {p : MvPolynomial σ R} {b : (σ →₀ ℕ) → R → A} : p.sum b = ∑ m ∈ p.support, b m (p.coeff m) := by simp [support, Finsupp.sum, coeff] theorem support_mul [DecidableEq σ] (p q : MvPolynomial σ R) : (p * q).support ⊆ p.support + q.support := AddMonoidAlgebra.support_mul p q @[ext] theorem ext (p q : MvPolynomial σ R) : (∀ m, coeff m p = coeff m q) → p = q := Finsupp.ext @[simp] theorem coeff_add (m : σ →₀ ℕ) (p q : MvPolynomial σ R) : coeff m (p + q) = coeff m p + coeff m q := add_apply p q m @[simp] theorem coeff_smul {S₁ : Type*} [SMulZeroClass S₁ R] (m : σ →₀ ℕ) (C : S₁) (p : MvPolynomial σ R) : coeff m (C • p) = C • coeff m p := smul_apply C p m @[simp] theorem coeff_zero (m : σ →₀ ℕ) : coeff m (0 : MvPolynomial σ R) = 0 := rfl @[simp] theorem coeff_zero_X (i : σ) : coeff 0 (X i : MvPolynomial σ R) = 0 := single_eq_of_ne fun h => by cases Finsupp.single_eq_zero.1 h /-- `MvPolynomial.coeff m` but promoted to an `AddMonoidHom`. -/ @[simps] def coeffAddMonoidHom (m : σ →₀ ℕ) : MvPolynomial σ R →+ R where toFun := coeff m map_zero' := coeff_zero m map_add' := coeff_add m variable (R) in /-- `MvPolynomial.coeff m` but promoted to a `LinearMap`. -/ @[simps] def lcoeff (m : σ →₀ ℕ) : MvPolynomial σ R →ₗ[R] R where toFun := coeff m map_add' := coeff_add m map_smul' := coeff_smul m theorem coeff_sum {X : Type*} (s : Finset X) (f : X → MvPolynomial σ R) (m : σ →₀ ℕ) : coeff m (∑ x ∈ s, f x) = ∑ x ∈ s, coeff m (f x) := map_sum (@coeffAddMonoidHom R σ _ _) _ s theorem monic_monomial_eq (m) : monomial m (1 : R) = (m.prod fun n e => X n ^ e : MvPolynomial σ R) := by simp [monomial_eq] @[simp] theorem coeff_monomial [DecidableEq σ] (m n) (a) : coeff m (monomial n a : MvPolynomial σ R) = if n = m then a else 0 := Finsupp.single_apply
@[simp]
Mathlib/Algebra/MvPolynomial/Basic.lean
592
593
/- Copyright (c) 2022 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import Mathlib.Analysis.InnerProductSpace.Projection import Mathlib.Analysis.Normed.Lp.lpSpace import Mathlib.Analysis.InnerProductSpace.PiL2 /-! # Hilbert sum of a family of inner product spaces Given a family `(G : ι → Type*) [Π i, InnerProductSpace 𝕜 (G i)]` of inner product spaces, this file equips `lp G 2` with an inner product space structure, where `lp G 2` consists of those dependent functions `f : Π i, G i` for which `∑' i, ‖f i‖ ^ 2`, the sum of the norms-squared, is summable. This construction is sometimes called the *Hilbert sum* of the family `G`. By choosing `G` to be `ι → 𝕜`, the Hilbert space `ℓ²(ι, 𝕜)` may be seen as a special case of this construction. We also define a *predicate* `IsHilbertSum 𝕜 G V`, where `V : Π i, G i →ₗᵢ[𝕜] E`, expressing that `V` is an `OrthogonalFamily` and that the associated map `lp G 2 →ₗᵢ[𝕜] E` is surjective. ## Main definitions * `OrthogonalFamily.linearIsometry`: Given a Hilbert space `E`, a family `G` of inner product spaces and a family `V : Π i, G i →ₗᵢ[𝕜] E` of isometric embeddings of the `G i` into `E` with mutually-orthogonal images, there is an induced isometric embedding of the Hilbert sum of `G` into `E`. * `IsHilbertSum`: Given a Hilbert space `E`, a family `G` of inner product spaces and a family `V : Π i, G i →ₗᵢ[𝕜] E` of isometric embeddings of the `G i` into `E`, `IsHilbertSum 𝕜 G V` means that `V` is an `OrthogonalFamily` and that the above linear isometry is surjective. * `IsHilbertSum.linearIsometryEquiv`: If a Hilbert space `E` is a Hilbert sum of the inner product spaces `G i` with respect to the family `V : Π i, G i →ₗᵢ[𝕜] E`, then the corresponding `OrthogonalFamily.linearIsometry` can be upgraded to a `LinearIsometryEquiv`. * `HilbertBasis`: We define a *Hilbert basis* of a Hilbert space `E` to be a structure whose single field `HilbertBasis.repr` is an isometric isomorphism of `E` with `ℓ²(ι, 𝕜)` (i.e., the Hilbert sum of `ι` copies of `𝕜`). This parallels the definition of `Basis`, in `LinearAlgebra.Basis`, as an isomorphism of an `R`-module with `ι →₀ R`. * `HilbertBasis.instCoeFun`: More conventionally a Hilbert basis is thought of as a family `ι → E` of vectors in `E` satisfying certain properties (orthonormality, completeness). We obtain this interpretation of a Hilbert basis `b` by defining `⇑b`, of type `ι → E`, to be the image under `b.repr` of `lp.single 2 i (1:𝕜)`. This parallels the definition `Basis.coeFun` in `LinearAlgebra.Basis`. * `HilbertBasis.mk`: Make a Hilbert basis of `E` from an orthonormal family `v : ι → E` of vectors in `E` whose span is dense. This parallels the definition `Basis.mk` in `LinearAlgebra.Basis`. * `HilbertBasis.mkOfOrthogonalEqBot`: Make a Hilbert basis of `E` from an orthonormal family `v : ι → E` of vectors in `E` whose span has trivial orthogonal complement. ## Main results * `lp.instInnerProductSpace`: Construction of the inner product space instance on the Hilbert sum `lp G 2`. Note that from the file `Analysis.Normed.Lp.lpSpace`, the space `lp G 2` already held a normed space instance (`lp.normedSpace`), and if each `G i` is a Hilbert space (i.e., complete), then `lp G 2` was already known to be complete (`lp.completeSpace`). So the work here is to define the inner product and show it is compatible. * `OrthogonalFamily.range_linearIsometry`: Given a family `G` of inner product spaces and a family `V : Π i, G i →ₗᵢ[𝕜] E` of isometric embeddings of the `G i` into `E` with mutually-orthogonal images, the image of the embedding `OrthogonalFamily.linearIsometry` of the Hilbert sum of `G` into `E` is the closure of the span of the images of the `G i`. * `HilbertBasis.repr_apply_apply`: Given a Hilbert basis `b` of `E`, the entry `b.repr x i` of `x`'s representation in `ℓ²(ι, 𝕜)` is the inner product `⟪b i, x⟫`. * `HilbertBasis.hasSum_repr`: Given a Hilbert basis `b` of `E`, a vector `x` in `E` can be expressed as the "infinite linear combination" `∑' i, b.repr x i • b i` of the basis vectors `b i`, with coefficients given by the entries `b.repr x i` of `x`'s representation in `ℓ²(ι, 𝕜)`. * `exists_hilbertBasis`: A Hilbert space admits a Hilbert basis. ## Keywords Hilbert space, Hilbert sum, l2, Hilbert basis, unitary equivalence, isometric isomorphism -/ open RCLike Submodule Filter open scoped NNReal ENNReal ComplexConjugate Topology noncomputable section variable {ι 𝕜 : Type*} [RCLike 𝕜] {E : Type*} variable [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable {G : ι → Type*} [∀ i, NormedAddCommGroup (G i)] [∀ i, InnerProductSpace 𝕜 (G i)] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y /-- `ℓ²(ι, 𝕜)` is the Hilbert space of square-summable functions `ι → 𝕜`, herein implemented as `lp (fun i : ι => 𝕜) 2`. -/ notation "ℓ²(" ι ", " 𝕜 ")" => lp (fun i : ι => 𝕜) 2 /-! ### Inner product space structure on `lp G 2` -/ namespace lp theorem summable_inner (f g : lp G 2) : Summable fun i => ⟪f i, g i⟫ := by -- Apply the Direct Comparison Test, comparing with ∑' i, ‖f i‖ * ‖g i‖ (summable by Hölder) refine .of_norm_bounded (fun i => ‖f i‖ * ‖g i‖) (lp.summable_mul ?_ f g) ?_ · rw [Real.holderConjugate_iff]; norm_num intro i -- Then apply Cauchy-Schwarz pointwise exact norm_inner_le_norm (𝕜 := 𝕜) _ _ instance instInnerProductSpace : InnerProductSpace 𝕜 (lp G 2) := { lp.normedAddCommGroup (E := G) (p := 2) with inner := fun f g => ∑' i, ⟪f i, g i⟫ norm_sq_eq_re_inner := fun f => by calc ‖f‖ ^ 2 = ‖f‖ ^ (2 : ℝ≥0∞).toReal := by norm_cast _ = ∑' i, ‖f i‖ ^ (2 : ℝ≥0∞).toReal := lp.norm_rpow_eq_tsum ?_ f _ = ∑' i, ‖f i‖ ^ (2 : ℕ) := by norm_cast _ = ∑' i, re ⟪f i, f i⟫ := by simp [norm_sq_eq_re_inner (𝕜 := 𝕜)] _ = re (∑' i, ⟪f i, f i⟫) := (RCLike.reCLM.map_tsum ?_).symm · norm_num · exact summable_inner f f conj_inner_symm := fun f g => by calc conj _ = conj (∑' i, ⟪g i, f i⟫) := by congr _ = ∑' i, conj ⟪g i, f i⟫ := RCLike.conjCLE.map_tsum _ = ∑' i, ⟪f i, g i⟫ := by simp only [inner_conj_symm] _ = _ := by congr add_left := fun f₁ f₂ g => by calc _ = ∑' i, ⟪(f₁ + f₂) i, g i⟫ := ?_ _ = ∑' i, (⟪f₁ i, g i⟫ + ⟪f₂ i, g i⟫) := by simp only [inner_add_left, Pi.add_apply, coeFn_add] _ = (∑' i, ⟪f₁ i, g i⟫) + ∑' i, ⟪f₂ i, g i⟫ := Summable.tsum_add ?_ ?_ _ = _ := by congr · congr · exact summable_inner f₁ g · exact summable_inner f₂ g smul_left := fun f g c => by calc _ = ∑' i, ⟪c • f i, g i⟫ := ?_ _ = ∑' i, conj c * ⟪f i, g i⟫ := by simp only [inner_smul_left] _ = conj c * ∑' i, ⟪f i, g i⟫ := tsum_mul_left _ = _ := ?_ · simp only [coeFn_smul, Pi.smul_apply] · congr } theorem inner_eq_tsum (f g : lp G 2) : ⟪f, g⟫ = ∑' i, ⟪f i, g i⟫ := rfl theorem hasSum_inner (f g : lp G 2) : HasSum (fun i => ⟪f i, g i⟫) ⟪f, g⟫ := (summable_inner f g).hasSum theorem inner_single_left [DecidableEq ι] (i : ι) (a : G i) (f : lp G 2) : ⟪lp.single 2 i a, f⟫ = ⟪a, f i⟫ := by refine (hasSum_inner (lp.single 2 i a) f).unique ?_ simp_rw [lp.coeFn_single] convert hasSum_ite_eq i ⟪a, f i⟫ using 1 ext j split_ifs with h · subst h; rw [Pi.single_eq_same] · simp [Pi.single_eq_of_ne h] theorem inner_single_right [DecidableEq ι] (i : ι) (a : G i) (f : lp G 2) : ⟪f, lp.single 2 i a⟫ = ⟪f i, a⟫ := by simpa [inner_conj_symm] using congr_arg conj (inner_single_left (𝕜 := 𝕜) i a f) end lp /-! ### Identification of a general Hilbert space `E` with a Hilbert sum -/ namespace OrthogonalFamily variable [CompleteSpace E] {V : ∀ i, G i →ₗᵢ[𝕜] E} (hV : OrthogonalFamily 𝕜 G V) include hV protected theorem summable_of_lp (f : lp G 2) : Summable fun i => V i (f i) := by rw [hV.summable_iff_norm_sq_summable] convert (lp.memℓp f).summable _ · norm_cast · norm_num /-- A mutually orthogonal family of subspaces of `E` induce a linear isometry from `lp 2` of the subspaces into `E`. -/ protected def linearIsometry (hV : OrthogonalFamily 𝕜 G V) : lp G 2 →ₗᵢ[𝕜] E where toFun f := ∑' i, V i (f i) map_add' f g := by simp only [(hV.summable_of_lp f).tsum_add (hV.summable_of_lp g), lp.coeFn_add, Pi.add_apply, LinearIsometry.map_add] map_smul' c f := by simpa only [LinearIsometry.map_smul, Pi.smul_apply, lp.coeFn_smul] using (hV.summable_of_lp f).tsum_const_smul c norm_map' f := by classical -- needed for lattice instance on `Finset ι`, for `Filter.atTop_neBot` have H : 0 < (2 : ℝ≥0∞).toReal := by norm_num suffices ‖∑' i : ι, V i (f i)‖ ^ (2 : ℝ≥0∞).toReal = ‖f‖ ^ (2 : ℝ≥0∞).toReal by exact Real.rpow_left_injOn H.ne' (norm_nonneg _) (norm_nonneg _) this refine tendsto_nhds_unique ?_ (lp.hasSum_norm H f) convert (hV.summable_of_lp f).hasSum.norm.rpow_const (Or.inr H.le) using 1 ext s exact mod_cast (hV.norm_sum f s).symm protected theorem linearIsometry_apply (f : lp G 2) : hV.linearIsometry f = ∑' i, V i (f i) := rfl protected theorem hasSum_linearIsometry (f : lp G 2) : HasSum (fun i => V i (f i)) (hV.linearIsometry f) := (hV.summable_of_lp f).hasSum @[simp] protected theorem linearIsometry_apply_single [DecidableEq ι] {i : ι} (x : G i) : hV.linearIsometry (lp.single 2 i x) = V i x := by rw [hV.linearIsometry_apply, ← tsum_ite_eq i (V i x)] congr ext j rw [lp.single_apply] split_ifs with h · subst h; simp · simp [h] protected theorem linearIsometry_apply_dfinsupp_sum_single [DecidableEq ι] [∀ i, DecidableEq (G i)] (W₀ : Π₀ i : ι, G i) : hV.linearIsometry (W₀.sum (lp.single 2)) = W₀.sum fun i => V i := by simp /-- The canonical linear isometry from the `lp 2` of a mutually orthogonal family of subspaces of `E` into E, has range the closure of the span of the subspaces. -/ protected theorem range_linearIsometry [∀ i, CompleteSpace (G i)] : LinearMap.range hV.linearIsometry.toLinearMap = (⨆ i, LinearMap.range (V i).toLinearMap).topologicalClosure := by classical refine le_antisymm ?_ ?_ · rintro x ⟨f, rfl⟩ refine mem_closure_of_tendsto (hV.hasSum_linearIsometry f) (Eventually.of_forall ?_) intro s rw [SetLike.mem_coe] refine sum_mem ?_ intro i _ refine mem_iSup_of_mem i ?_ exact LinearMap.mem_range_self _ (f i) · apply topologicalClosure_minimal · refine iSup_le ?_ rintro i x ⟨x, rfl⟩ use lp.single 2 i x exact hV.linearIsometry_apply_single x exact hV.linearIsometry.isometry.isUniformInducing.isComplete_range.isClosed end OrthogonalFamily section IsHilbertSum variable (𝕜 G) variable [CompleteSpace E] (V : ∀ i, G i →ₗᵢ[𝕜] E) (F : ι → Submodule 𝕜 E) /-- Given a family of Hilbert spaces `G : ι → Type*`, a Hilbert sum of `G` consists of a Hilbert space `E` and an orthogonal family `V : Π i, G i →ₗᵢ[𝕜] E` such that the induced isometry `Φ : lp G 2 → E` is surjective. Keeping in mind that `lp G 2` is "the" external Hilbert sum of `G : ι → Type*`, this is analogous to `DirectSum.IsInternal`, except that we don't express it in terms of actual submodules. -/ structure IsHilbertSum : Prop where ofSurjective :: /-- The orthogonal family constituting the summands in the Hilbert sum. -/ protected OrthogonalFamily : OrthogonalFamily 𝕜 G V /-- The isometry `lp G 2 → E` induced by the orthogonal family is surjective. -/ protected surjective_isometry : Function.Surjective OrthogonalFamily.linearIsometry variable {𝕜 G V} /-- If `V : Π i, G i →ₗᵢ[𝕜] E` is an orthogonal family such that the supremum of the ranges of `V i` is dense, then `(E, V)` is a Hilbert sum of `G`. -/ theorem IsHilbertSum.mk [∀ i, CompleteSpace <| G i] (hVortho : OrthogonalFamily 𝕜 G V) (hVtotal : ⊤ ≤ (⨆ i, LinearMap.range (V i).toLinearMap).topologicalClosure) : IsHilbertSum 𝕜 G V := { OrthogonalFamily := hVortho surjective_isometry := by rw [← LinearIsometry.coe_toLinearMap] exact LinearMap.range_eq_top.mp (eq_top_iff.mpr <| hVtotal.trans_eq hVortho.range_linearIsometry.symm) } /-- This is `Orthonormal.isHilbertSum` in the case of actual inclusions from subspaces. -/ theorem IsHilbertSum.mkInternal [∀ i, CompleteSpace <| F i] (hFortho : OrthogonalFamily 𝕜 (fun i => F i) fun i => (F i).subtypeₗᵢ) (hFtotal : ⊤ ≤ (⨆ i, F i).topologicalClosure) : IsHilbertSum 𝕜 (fun i => F i) fun i => (F i).subtypeₗᵢ := IsHilbertSum.mk hFortho (by simpa [subtypeₗᵢ_toLinearMap, range_subtype] using hFtotal) /-- *A* Hilbert sum `(E, V)` of `G` is canonically isomorphic to *the* Hilbert sum of `G`, i.e `lp G 2`. Note that this goes in the opposite direction from `OrthogonalFamily.linearIsometry`. -/ noncomputable def IsHilbertSum.linearIsometryEquiv (hV : IsHilbertSum 𝕜 G V) : E ≃ₗᵢ[𝕜] lp G 2 := LinearIsometryEquiv.symm <| LinearIsometryEquiv.ofSurjective hV.OrthogonalFamily.linearIsometry hV.surjective_isometry /-- In the canonical isometric isomorphism between a Hilbert sum `E` of `G` and `lp G 2`, a vector `w : lp G 2` is the image of the infinite sum of the associated elements in `E`. -/ protected theorem IsHilbertSum.linearIsometryEquiv_symm_apply (hV : IsHilbertSum 𝕜 G V) (w : lp G 2) : hV.linearIsometryEquiv.symm w = ∑' i, V i (w i) := by simp [IsHilbertSum.linearIsometryEquiv, OrthogonalFamily.linearIsometry_apply] /-- In the canonical isometric isomorphism between a Hilbert sum `E` of `G` and `lp G 2`, a vector `w : lp G 2` is the image of the infinite sum of the associated elements in `E`, and this sum indeed converges. -/ protected theorem IsHilbertSum.hasSum_linearIsometryEquiv_symm (hV : IsHilbertSum 𝕜 G V) (w : lp G 2) : HasSum (fun i => V i (w i)) (hV.linearIsometryEquiv.symm w) := by simp [IsHilbertSum.linearIsometryEquiv, OrthogonalFamily.hasSum_linearIsometry] /-- In the canonical isometric isomorphism between a Hilbert sum `E` of `G : ι → Type*` and `lp G 2`, an "elementary basis vector" in `lp G 2` supported at `i : ι` is the image of the associated element in `E`. -/ @[simp] protected theorem IsHilbertSum.linearIsometryEquiv_symm_apply_single [DecidableEq ι] (hV : IsHilbertSum 𝕜 G V) {i : ι} (x : G i) : hV.linearIsometryEquiv.symm (lp.single 2 i x) = V i x := by simp [IsHilbertSum.linearIsometryEquiv, OrthogonalFamily.linearIsometry_apply_single] /-- In the canonical isometric isomorphism between a Hilbert sum `E` of `G : ι → Type*` and `lp G 2`, a finitely-supported vector in `lp G 2` is the image of the associated finite sum of elements of `E`. -/ protected theorem IsHilbertSum.linearIsometryEquiv_symm_apply_dfinsupp_sum_single [DecidableEq ι] [∀ i, DecidableEq (G i)] (hV : IsHilbertSum 𝕜 G V) (W₀ : Π₀ i : ι, G i) : hV.linearIsometryEquiv.symm (W₀.sum (lp.single 2)) = W₀.sum fun i => V i := by simp only [map_dfinsuppSum, IsHilbertSum.linearIsometryEquiv_symm_apply_single] /-- In the canonical isometric isomorphism between a Hilbert sum `E` of `G : ι → Type*` and `lp G 2`, a finitely-supported vector in `lp G 2` is the image of the associated finite sum of elements of `E`. -/ @[simp] protected theorem IsHilbertSum.linearIsometryEquiv_apply_dfinsupp_sum_single [DecidableEq ι] [∀ i, DecidableEq (G i)] (hV : IsHilbertSum 𝕜 G V) (W₀ : Π₀ i : ι, G i) : ((W₀.sum (γ := lp G 2) fun a b ↦ hV.linearIsometryEquiv (V a b)) : ∀ i, G i) = W₀ := by rw [← map_dfinsuppSum] rw [← hV.linearIsometryEquiv_symm_apply_dfinsupp_sum_single]
rw [LinearIsometryEquiv.apply_symm_apply] ext i simp +contextual [DFinsupp.sum, lp.single_apply]
Mathlib/Analysis/InnerProductSpace/l2Space.lean
336
338
/- 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, Kim Morrison -/ import Mathlib.Algebra.Group.Indicator import Mathlib.Algebra.Group.InjSurj import Mathlib.Data.Set.Finite.Basic import Mathlib.Tactic.FastInstance import Mathlib.Algebra.Group.Equiv.Defs /-! # Type of functions with finite support For any type `α` and any type `M` with zero, we define the type `Finsupp α M` (notation: `α →₀ M`) of finitely supported functions from `α` to `M`, i.e. the functions which are zero everywhere on `α` except on a finite set. Functions with finite support are used (at least) in the following parts of the library: * `MonoidAlgebra R M` and `AddMonoidAlgebra R M` are defined as `M →₀ R`; * polynomials and multivariate polynomials are defined as `AddMonoidAlgebra`s, hence they use `Finsupp` under the hood; * the linear combination of a family of vectors `v i` with coefficients `f i` (as used, e.g., to define linearly independent family `LinearIndependent`) is defined as a map `Finsupp.linearCombination : (ι → M) → (ι →₀ R) →ₗ[R] M`. Some other constructions are naturally equivalent to `α →₀ M` with some `α` and `M` but are defined in a different way in the library: * `Multiset α ≃+ α →₀ ℕ`; * `FreeAbelianGroup α ≃+ α →₀ ℤ`. Most of the theory assumes that the range is a commutative additive monoid. This gives us the big sum operator as a powerful way to construct `Finsupp` elements, which is defined in `Mathlib.Algebra.BigOperators.Finsupp.Basic`. Many constructions based on `α →₀ M` are `def`s rather than `abbrev`s to avoid reusing unwanted type class instances. E.g., `MonoidAlgebra`, `AddMonoidAlgebra`, and types based on these two have non-pointwise multiplication. ## Main declarations * `Finsupp`: The type of finitely supported functions from `α` to `β`. * `Finsupp.onFinset`: The restriction of a function to a `Finset` as a `Finsupp`. * `Finsupp.mapRange`: Composition of a `ZeroHom` with a `Finsupp`. * `Finsupp.embDomain`: Maps the domain of a `Finsupp` by an embedding. * `Finsupp.zipWith`: Postcomposition of two `Finsupp`s with a function `f` such that `f 0 0 = 0`. ## Notations This file adds `α →₀ M` as a global notation for `Finsupp α M`. We also use the following convention for `Type*` variables in this file * `α`, `β`, `γ`: types with no additional structure that appear as the first argument to `Finsupp` somewhere in the statement; * `ι` : an auxiliary index type; * `M`, `M'`, `N`, `P`: types with `Zero` or `(Add)(Comm)Monoid` structure; `M` is also used for a (semi)module over a (semi)ring. * `G`, `H`: groups (commutative or not, multiplicative or additive); * `R`, `S`: (semi)rings. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## TODO * Expand the list of definitions and important lemmas to the module docstring. -/ assert_not_exists CompleteLattice Submonoid noncomputable section open Finset Function variable {α β γ ι M M' N P G H R S : Type*} /-- `Finsupp α M`, denoted `α →₀ M`, is the type of functions `f : α → M` such that `f x = 0` for all but finitely many `x`. -/ structure Finsupp (α : Type*) (M : Type*) [Zero M] where /-- The support of a finitely supported function (aka `Finsupp`). -/ support : Finset α /-- The underlying function of a bundled finitely supported function (aka `Finsupp`). -/ toFun : α → M /-- The witness that the support of a `Finsupp` is indeed the exact locus where its underlying function is nonzero. -/ mem_support_toFun : ∀ a, a ∈ support ↔ toFun a ≠ 0 @[inherit_doc] infixr:25 " →₀ " => Finsupp namespace Finsupp /-! ### Basic declarations about `Finsupp` -/ section Basic variable [Zero M] instance instFunLike : FunLike (α →₀ M) α M := ⟨toFun, by rintro ⟨s, f, hf⟩ ⟨t, g, hg⟩ (rfl : f = g) congr ext a exact (hf _).trans (hg _).symm⟩ @[ext] theorem ext {f g : α →₀ M} (h : ∀ a, f a = g a) : f = g := DFunLike.ext _ _ h lemma ne_iff {f g : α →₀ M} : f ≠ g ↔ ∃ a, f a ≠ g a := DFunLike.ne_iff @[simp, norm_cast] theorem coe_mk (f : α → M) (s : Finset α) (h : ∀ a, a ∈ s ↔ f a ≠ 0) : ⇑(⟨s, f, h⟩ : α →₀ M) = f := rfl instance instZero : Zero (α →₀ M) := ⟨⟨∅, 0, fun _ => ⟨fun h ↦ (not_mem_empty _ h).elim, fun H => (H rfl).elim⟩⟩⟩ @[simp, norm_cast] lemma coe_zero : ⇑(0 : α →₀ M) = 0 := rfl theorem zero_apply {a : α} : (0 : α →₀ M) a = 0 := rfl @[simp] theorem support_zero : (0 : α →₀ M).support = ∅ := rfl instance instInhabited : Inhabited (α →₀ M) := ⟨0⟩ @[simp] theorem mem_support_iff {f : α →₀ M} : ∀ {a : α}, a ∈ f.support ↔ f a ≠ 0 := @(f.mem_support_toFun) @[simp, norm_cast] theorem fun_support_eq (f : α →₀ M) : Function.support f = f.support := Set.ext fun _x => mem_support_iff.symm theorem not_mem_support_iff {f : α →₀ M} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm @[simp, norm_cast] theorem coe_eq_zero {f : α →₀ M} : (f : α → M) = 0 ↔ f = 0 := by rw [← coe_zero, DFunLike.coe_fn_eq] theorem ext_iff' {f g : α →₀ M} : f = g ↔ f.support = g.support ∧ ∀ x ∈ f.support, f x = g x := ⟨fun h => h ▸ ⟨rfl, fun _ _ => rfl⟩, fun ⟨h₁, h₂⟩ => ext fun a => by classical exact if h : a ∈ f.support then h₂ a h else by have hf : f a = 0 := not_mem_support_iff.1 h have hg : g a = 0 := by rwa [h₁, not_mem_support_iff] at h rw [hf, hg]⟩ @[simp] theorem support_eq_empty {f : α →₀ M} : f.support = ∅ ↔ f = 0 := mod_cast @Function.support_eq_empty_iff _ _ _ f theorem support_nonempty_iff {f : α →₀ M} : f.support.Nonempty ↔ f ≠ 0 := by simp only [Finsupp.support_eq_empty, Finset.nonempty_iff_ne_empty, Ne] theorem card_support_eq_zero {f : α →₀ M} : #f.support = 0 ↔ f = 0 := by simp instance instDecidableEq [DecidableEq α] [DecidableEq M] : DecidableEq (α →₀ M) := fun f g => decidable_of_iff (f.support = g.support ∧ ∀ a ∈ f.support, f a = g a) ext_iff'.symm theorem finite_support (f : α →₀ M) : Set.Finite (Function.support f) := f.fun_support_eq.symm ▸ f.support.finite_toSet theorem support_subset_iff {s : Set α} {f : α →₀ M} : ↑f.support ⊆ s ↔ ∀ a ∉ s, f a = 0 := by simp only [Set.subset_def, mem_coe, mem_support_iff]; exact forall_congr' fun a => not_imp_comm /-- Given `Finite α`, `equivFunOnFinite` is the `Equiv` between `α →₀ β` and `α → β`. (All functions on a finite type are finitely supported.) -/ @[simps] def equivFunOnFinite [Finite α] : (α →₀ M) ≃ (α → M) where toFun := (⇑) invFun f := mk (Function.support f).toFinite.toFinset f fun _a => Set.Finite.mem_toFinset _ left_inv _f := ext fun _x => rfl right_inv _f := rfl @[simp] theorem equivFunOnFinite_symm_coe {α} [Finite α] (f : α →₀ M) : equivFunOnFinite.symm f = f := equivFunOnFinite.symm_apply_apply f @[simp] lemma coe_equivFunOnFinite_symm {α} [Finite α] (f : α → M) : ⇑(equivFunOnFinite.symm f) = f := rfl /-- If `α` has a unique term, the type of finitely supported functions `α →₀ β` is equivalent to `β`. -/ @[simps!] noncomputable def _root_.Equiv.finsuppUnique {ι : Type*} [Unique ι] : (ι →₀ M) ≃ M := Finsupp.equivFunOnFinite.trans (Equiv.funUnique ι M) @[ext] theorem unique_ext [Unique α] {f g : α →₀ M} (h : f default = g default) : f = g := ext fun a => by rwa [Unique.eq_default a] end Basic /-! ### Declarations about `onFinset` -/ section OnFinset variable [Zero M] /-- `Finsupp.onFinset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function must be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def onFinset (s : Finset α) (f : α → M) (hf : ∀ a, f a ≠ 0 → a ∈ s) : α →₀ M where support := haveI := Classical.decEq M {a ∈ s | f a ≠ 0} toFun := f mem_support_toFun := by classical simpa @[simp, norm_cast] lemma coe_onFinset (s : Finset α) (f : α → M) (hf) : onFinset s f hf = f := rfl @[simp] theorem onFinset_apply {s : Finset α} {f : α → M} {hf a} : (onFinset s f hf : α →₀ M) a = f a := rfl @[simp] theorem support_onFinset_subset {s : Finset α} {f : α → M} {hf} : (onFinset s f hf).support ⊆ s := by classical convert filter_subset (f · ≠ 0) s theorem mem_support_onFinset {s : Finset α} {f : α → M} (hf : ∀ a : α, f a ≠ 0 → a ∈ s) {a : α} : a ∈ (Finsupp.onFinset s f hf).support ↔ f a ≠ 0 := by rw [Finsupp.mem_support_iff, Finsupp.onFinset_apply] theorem support_onFinset [DecidableEq M] {s : Finset α} {f : α → M} (hf : ∀ a : α, f a ≠ 0 → a ∈ s) : (Finsupp.onFinset s f hf).support = {a ∈ s | f a ≠ 0} := by dsimp [onFinset]; congr end OnFinset section OfSupportFinite variable [Zero M] /-- The natural `Finsupp` induced by the function `f` given that it has finite support. -/ noncomputable def ofSupportFinite (f : α → M) (hf : (Function.support f).Finite) : α →₀ M where support := hf.toFinset toFun := f mem_support_toFun _ := hf.mem_toFinset theorem ofSupportFinite_coe {f : α → M} {hf : (Function.support f).Finite} : (ofSupportFinite f hf : α → M) = f := rfl instance instCanLift : CanLift (α → M) (α →₀ M) (⇑) fun f => (Function.support f).Finite where prf f hf := ⟨ofSupportFinite f hf, rfl⟩ end OfSupportFinite /-! ### Declarations about `mapRange` -/ section MapRange variable [Zero M] [Zero N] [Zero P] /-- The composition of `f : M → N` and `g : α →₀ M` is `mapRange f hf g : α →₀ N`, which is well-defined when `f 0 = 0`. This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself bundled (defined in `Mathlib/Data/Finsupp/Basic.lean`): * `Finsupp.mapRange.equiv` * `Finsupp.mapRange.zeroHom` * `Finsupp.mapRange.addMonoidHom` * `Finsupp.mapRange.addEquiv` * `Finsupp.mapRange.linearMap` * `Finsupp.mapRange.linearEquiv` -/ def mapRange (f : M → N) (hf : f 0 = 0) (g : α →₀ M) : α →₀ N := onFinset g.support (f ∘ g) fun a => by rw [mem_support_iff, not_imp_not]; exact fun H => (congr_arg f H).trans hf @[simp] theorem mapRange_apply {f : M → N} {hf : f 0 = 0} {g : α →₀ M} {a : α} : mapRange f hf g a = f (g a) := rfl @[simp] theorem mapRange_zero {f : M → N} {hf : f 0 = 0} : mapRange f hf (0 : α →₀ M) = 0 := ext fun _ => by simp only [hf, zero_apply, mapRange_apply] @[simp] theorem mapRange_id (g : α →₀ M) : mapRange id rfl g = g := ext fun _ => rfl theorem mapRange_comp (f : N → P) (hf : f 0 = 0) (f₂ : M → N) (hf₂ : f₂ 0 = 0) (h : (f ∘ f₂) 0 = 0) (g : α →₀ M) : mapRange (f ∘ f₂) h g = mapRange f hf (mapRange f₂ hf₂ g) := ext fun _ => rfl @[simp] lemma mapRange_mapRange (e₁ : N → P) (e₂ : M → N) (he₁ he₂) (f : α →₀ M) : mapRange e₁ he₁ (mapRange e₂ he₂ f) = mapRange (e₁ ∘ e₂) (by simp [*]) f := ext fun _ ↦ rfl theorem support_mapRange {f : M → N} {hf : f 0 = 0} {g : α →₀ M} : (mapRange f hf g).support ⊆ g.support := support_onFinset_subset theorem support_mapRange_of_injective {e : M → N} (he0 : e 0 = 0) (f : ι →₀ M) (he : Function.Injective e) : (Finsupp.mapRange e he0 f).support = f.support := by ext simp only [Finsupp.mem_support_iff, Ne, Finsupp.mapRange_apply] exact he.ne_iff' he0 lemma range_mapRange (e : M → N) (he₀ : e 0 = 0) : Set.range (Finsupp.mapRange (α := α) e he₀) = {g | ∀ i, g i ∈ Set.range e} := by ext g simp only [Set.mem_range, Set.mem_setOf] constructor · rintro ⟨g, rfl⟩ i simp · intro h classical choose f h using h use onFinset g.support (Set.indicator g.support f) (by aesop) ext i simp only [mapRange_apply, onFinset_apply, Set.indicator_apply] split_ifs <;> simp_all /-- `Finsupp.mapRange` of a injective function is injective. -/ lemma mapRange_injective (e : M → N) (he₀ : e 0 = 0) (he : Injective e) : Injective (Finsupp.mapRange (α := α) e he₀) := by intro a b h rw [Finsupp.ext_iff] at h ⊢ simpa only [mapRange_apply, he.eq_iff] using h /-- `Finsupp.mapRange` of a surjective function is surjective. -/ lemma mapRange_surjective (e : M → N) (he₀ : e 0 = 0) (he : Surjective e) : Surjective (Finsupp.mapRange (α := α) e he₀) := by rw [← Set.range_eq_univ, range_mapRange, he.range_eq] simp end MapRange /-! ### Declarations about `embDomain` -/ section EmbDomain variable [Zero M] [Zero N] /-- Given `f : α ↪ β` and `v : α →₀ M`, `Finsupp.embDomain f v : β →₀ M` is the finitely supported function whose value at `f a : β` is `v a`. For a `b : β` outside the range of `f`, it is zero. -/ def embDomain (f : α ↪ β) (v : α →₀ M) : β →₀ M where support := v.support.map f toFun a₂ := haveI := Classical.decEq β if h : a₂ ∈ v.support.map f then v (v.support.choose (fun a₁ => f a₁ = a₂) (by rcases Finset.mem_map.1 h with ⟨a, ha, rfl⟩ exact ExistsUnique.intro a ⟨ha, rfl⟩ fun b ⟨_, hb⟩ => f.injective hb)) else 0 mem_support_toFun a₂ := by dsimp split_ifs with h
· simp only [h, true_iff, Ne] rw [← not_mem_support_iff, not_not] classical apply Finset.choose_mem · simp only [h, Ne, ne_self_iff_false, not_true_eq_false] @[simp] theorem support_embDomain (f : α ↪ β) (v : α →₀ M) : (embDomain f v).support = v.support.map f := rfl @[simp] theorem embDomain_zero (f : α ↪ β) : (embDomain f 0 : β →₀ M) = 0 := rfl @[simp] theorem embDomain_apply (f : α ↪ β) (v : α →₀ M) (a : α) : embDomain f v (f a) = v a := by
Mathlib/Data/Finsupp/Defs.lean
381
395
/- Copyright (c) 2021 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, David Kurniadi Angdinata -/ import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.CubicDiscriminant import Mathlib.RingTheory.Nilpotent.Defs import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.LinearCombination /-! # Weierstrass equations of elliptic curves This file defines the structure of an elliptic curve as a nonsingular Weierstrass curve given by a Weierstrass equation, which is mathematically accurate in many cases but also good for computation. ## Mathematical background Let `S` be a scheme. The actual category of elliptic curves over `S` is a large category, whose objects are schemes `E` equipped with a map `E → S`, a section `S → E`, and some axioms (the map is smooth and proper and the fibres are geometrically-connected one-dimensional group varieties). In the special case where `S` is the spectrum of some commutative ring `R` whose Picard group is zero (this includes all fields, all PIDs, and many other commutative rings) it can be shown (using a lot of algebro-geometric machinery) that every elliptic curve `E` is a projective plane cubic isomorphic to a Weierstrass curve given by the equation `Y² + a₁XY + a₃Y = X³ + a₂X² + a₄X + a₆` for some `aᵢ` in `R`, and such that a certain quantity called the discriminant of `E` is a unit in `R`. If `R` is a field, this quantity divides the discriminant of a cubic polynomial whose roots over a splitting field of `R` are precisely the `X`-coordinates of the non-zero 2-torsion points of `E`. ## Main definitions * `WeierstrassCurve`: a Weierstrass curve over a commutative ring. * `WeierstrassCurve.Δ`: the discriminant of a Weierstrass curve. * `WeierstrassCurve.map`: the Weierstrass curve mapped over a ring homomorphism. * `WeierstrassCurve.twoTorsionPolynomial`: the 2-torsion polynomial of a Weierstrass curve. * `WeierstrassCurve.IsElliptic`: typeclass asserting that a Weierstrass curve is an elliptic curve. * `WeierstrassCurve.j`: the j-invariant of an elliptic curve. ## Main statements * `WeierstrassCurve.twoTorsionPolynomial_disc`: the discriminant of a Weierstrass curve is a constant factor of the cubic discriminant of its 2-torsion polynomial. ## Implementation notes The definition of elliptic curves in this file makes sense for all commutative rings `R`, but it only gives a type which can be beefed up to a category which is equivalent to the category of elliptic curves over the spectrum `Spec(R)` of `R` in the case that `R` has trivial Picard group `Pic(R)` or, slightly more generally, when its 12-torsion is trivial. The issue is that for a general ring `R`, there might be elliptic curves over `Spec(R)` in the sense of algebraic geometry which are not globally defined by a cubic equation valid over the entire base. ## References * [N Katz and B Mazur, *Arithmetic Moduli of Elliptic Curves*][katz_mazur] * [P Deligne, *Courbes Elliptiques: Formulaire (d'après J. Tate)*][deligne_formulaire] * [J Silverman, *The Arithmetic of Elliptic Curves*][silverman2009] ## Tags elliptic curve, weierstrass equation, j invariant -/ local macro "map_simp" : tactic => `(tactic| simp only [map_ofNat, map_neg, map_add, map_sub, map_mul, map_pow]) universe s u v w /-! ## Weierstrass curves -/ /-- A Weierstrass curve `Y² + a₁XY + a₃Y = X³ + a₂X² + a₄X + a₆` with parameters `aᵢ`. -/ @[ext] structure WeierstrassCurve (R : Type u) where /-- The `a₁` coefficient of a Weierstrass curve. -/ a₁ : R /-- The `a₂` coefficient of a Weierstrass curve. -/ a₂ : R /-- The `a₃` coefficient of a Weierstrass curve. -/ a₃ : R /-- The `a₄` coefficient of a Weierstrass curve. -/ a₄ : R /-- The `a₆` coefficient of a Weierstrass curve. -/ a₆ : R namespace WeierstrassCurve instance {R : Type u} [Inhabited R] : Inhabited <| WeierstrassCurve R := ⟨⟨default, default, default, default, default⟩⟩ variable {R : Type u} [CommRing R] (W : WeierstrassCurve R) section Quantity /-! ### Standard quantities -/ /-- The `b₂` coefficient of a Weierstrass curve. -/ def b₂ : R := W.a₁ ^ 2 + 4 * W.a₂ /-- The `b₄` coefficient of a Weierstrass curve. -/ def b₄ : R := 2 * W.a₄ + W.a₁ * W.a₃ /-- The `b₆` coefficient of a Weierstrass curve. -/ def b₆ : R := W.a₃ ^ 2 + 4 * W.a₆ /-- The `b₈` coefficient of a Weierstrass curve. -/ def b₈ : R := W.a₁ ^ 2 * W.a₆ + 4 * W.a₂ * W.a₆ - W.a₁ * W.a₃ * W.a₄ + W.a₂ * W.a₃ ^ 2 - W.a₄ ^ 2 lemma b_relation : 4 * W.b₈ = W.b₂ * W.b₆ - W.b₄ ^ 2 := by simp only [b₂, b₄, b₆, b₈] ring1 /-- The `c₄` coefficient of a Weierstrass curve. -/ def c₄ : R := W.b₂ ^ 2 - 24 * W.b₄ /-- The `c₆` coefficient of a Weierstrass curve. -/ def c₆ : R := -W.b₂ ^ 3 + 36 * W.b₂ * W.b₄ - 216 * W.b₆ /-- The discriminant `Δ` of a Weierstrass curve. If `R` is a field, then this polynomial vanishes if and only if the cubic curve cut out by this equation is singular. Sometimes only defined up to sign in the literature; we choose the sign used by the LMFDB. For more discussion, see [the LMFDB page on discriminants](https://www.lmfdb.org/knowledge/show/ec.discriminant). -/ def Δ : R := -W.b₂ ^ 2 * W.b₈ - 8 * W.b₄ ^ 3 - 27 * W.b₆ ^ 2 + 9 * W.b₂ * W.b₄ * W.b₆ lemma c_relation : 1728 * W.Δ = W.c₄ ^ 3 - W.c₆ ^ 2 := by simp only [b₂, b₄, b₆, b₈, c₄, c₆, Δ] ring1 section CharTwo variable [CharP R 2] lemma b₂_of_char_two : W.b₂ = W.a₁ ^ 2 := by rw [b₂] linear_combination 2 * W.a₂ * CharP.cast_eq_zero R 2 lemma b₄_of_char_two : W.b₄ = W.a₁ * W.a₃ := by rw [b₄] linear_combination W.a₄ * CharP.cast_eq_zero R 2 lemma b₆_of_char_two : W.b₆ = W.a₃ ^ 2 := by rw [b₆] linear_combination 2 * W.a₆ * CharP.cast_eq_zero R 2 lemma b₈_of_char_two : W.b₈ = W.a₁ ^ 2 * W.a₆ + W.a₁ * W.a₃ * W.a₄ + W.a₂ * W.a₃ ^ 2 + W.a₄ ^ 2 := by rw [b₈] linear_combination (2 * W.a₂ * W.a₆ - W.a₁ * W.a₃ * W.a₄ - W.a₄ ^ 2) * CharP.cast_eq_zero R 2 lemma c₄_of_char_two : W.c₄ = W.a₁ ^ 4 := by rw [c₄, b₂_of_char_two] linear_combination -12 * W.b₄ * CharP.cast_eq_zero R 2 lemma c₆_of_char_two : W.c₆ = W.a₁ ^ 6 := by rw [c₆, b₂_of_char_two] linear_combination (18 * W.a₁ ^ 2 * W.b₄ - 108 * W.b₆ - W.a₁ ^ 6) * CharP.cast_eq_zero R 2 lemma Δ_of_char_two : W.Δ = W.a₁ ^ 4 * W.b₈ + W.a₃ ^ 4 + W.a₁ ^ 3 * W.a₃ ^ 3 := by rw [Δ, b₂_of_char_two, b₄_of_char_two, b₆_of_char_two] linear_combination (-W.a₁ ^ 4 * W.b₈ - 14 * W.a₃ ^ 4) * CharP.cast_eq_zero R 2 lemma b_relation_of_char_two : W.b₂ * W.b₆ = W.b₄ ^ 2 := by linear_combination -W.b_relation + 2 * W.b₈ * CharP.cast_eq_zero R 2 lemma c_relation_of_char_two : W.c₄ ^ 3 = W.c₆ ^ 2 := by linear_combination -W.c_relation + 864 * W.Δ * CharP.cast_eq_zero R 2 end CharTwo section CharThree variable [CharP R 3] lemma b₂_of_char_three : W.b₂ = W.a₁ ^ 2 + W.a₂ := by rw [b₂] linear_combination W.a₂ * CharP.cast_eq_zero R 3 lemma b₄_of_char_three : W.b₄ = -W.a₄ + W.a₁ * W.a₃ := by rw [b₄] linear_combination W.a₄ * CharP.cast_eq_zero R 3 lemma b₆_of_char_three : W.b₆ = W.a₃ ^ 2 + W.a₆ := by rw [b₆] linear_combination W.a₆ * CharP.cast_eq_zero R 3 lemma b₈_of_char_three : W.b₈ = W.a₁ ^ 2 * W.a₆ + W.a₂ * W.a₆ - W.a₁ * W.a₃ * W.a₄ + W.a₂ * W.a₃ ^ 2 - W.a₄ ^ 2 := by rw [b₈] linear_combination W.a₂ * W.a₆ * CharP.cast_eq_zero R 3 lemma c₄_of_char_three : W.c₄ = W.b₂ ^ 2 := by rw [c₄] linear_combination -8 * W.b₄ * CharP.cast_eq_zero R 3 lemma c₆_of_char_three : W.c₆ = -W.b₂ ^ 3 := by rw [c₆] linear_combination (12 * W.b₂ * W.b₄ - 72 * W.b₆) * CharP.cast_eq_zero R 3 lemma Δ_of_char_three : W.Δ = -W.b₂ ^ 2 * W.b₈ - 8 * W.b₄ ^ 3 := by rw [Δ] linear_combination (-9 * W.b₆ ^ 2 + 3 * W.b₂ * W.b₄ * W.b₆) * CharP.cast_eq_zero R 3 lemma b_relation_of_char_three : W.b₈ = W.b₂ * W.b₆ - W.b₄ ^ 2 := by linear_combination W.b_relation - W.b₈ * CharP.cast_eq_zero R 3 lemma c_relation_of_char_three : W.c₄ ^ 3 = W.c₆ ^ 2 := by linear_combination -W.c_relation + 576 * W.Δ * CharP.cast_eq_zero R 3 end CharThree end Quantity section BaseChange /-! ### Maps and base changes -/ variable {A : Type v} [CommRing A] (f : R →+* A) /-- The Weierstrass curve mapped over a ring homomorphism `f : R →+* A`. -/ @[simps] def map : WeierstrassCurve A := ⟨f W.a₁, f W.a₂, f W.a₃, f W.a₄, f W.a₆⟩ variable (A) in /-- The Weierstrass curve base changed to an algebra `A` over `R`. -/ abbrev baseChange [Algebra R A] : WeierstrassCurve A := W.map <| algebraMap R A @[simp] lemma map_b₂ : (W.map f).b₂ = f W.b₂ := by simp only [b₂, map_a₁, map_a₂] map_simp @[simp] lemma map_b₄ : (W.map f).b₄ = f W.b₄ := by simp only [b₄, map_a₁, map_a₃, map_a₄] map_simp @[simp] lemma map_b₆ : (W.map f).b₆ = f W.b₆ := by simp only [b₆, map_a₃, map_a₆] map_simp @[simp] lemma map_b₈ : (W.map f).b₈ = f W.b₈ := by simp only [b₈, map_a₁, map_a₂, map_a₃, map_a₄, map_a₆] map_simp @[simp] lemma map_c₄ : (W.map f).c₄ = f W.c₄ := by simp only [c₄, map_b₂, map_b₄] map_simp @[simp] lemma map_c₆ : (W.map f).c₆ = f W.c₆ := by simp only [c₆, map_b₂, map_b₄, map_b₆] map_simp @[simp] lemma map_Δ : (W.map f).Δ = f W.Δ := by simp only [Δ, map_b₂, map_b₄, map_b₆, map_b₈] map_simp @[simp] lemma map_id : W.map (RingHom.id R) = W := rfl lemma map_map {B : Type w} [CommRing B] (g : A →+* B) : (W.map f).map g = W.map (g.comp f) := rfl @[simp] lemma map_baseChange {S : Type s} [CommRing S] [Algebra R S] {A : Type v} [CommRing A] [Algebra R A] [Algebra S A] [IsScalarTower R S A] {B : Type w} [CommRing B] [Algebra R B] [Algebra S B] [IsScalarTower R S B] (g : A →ₐ[S] B) : (W.baseChange A).map g = W.baseChange B := congr_arg W.map <| g.comp_algebraMap_of_tower R lemma map_injective {f : R →+* A} (hf : Function.Injective f) : Function.Injective <| map (f := f) := fun _ _ h => by rcases mk.inj h with ⟨_, _, _, _, _⟩ ext <;> apply_fun _ using hf <;> assumption end BaseChange section TorsionPolynomial /-! ### 2-torsion polynomials -/ /-- A cubic polynomial whose discriminant is a multiple of the Weierstrass curve discriminant. If `W` is an elliptic curve over a field `R` of characteristic different from 2, then its roots over a splitting field of `R` are precisely the `X`-coordinates of the non-zero 2-torsion points of `W`. -/ def twoTorsionPolynomial : Cubic R := ⟨4, W.b₂, 2 * W.b₄, W.b₆⟩ lemma twoTorsionPolynomial_disc : W.twoTorsionPolynomial.disc = 16 * W.Δ := by simp only [b₂, b₄, b₆, b₈, Δ, twoTorsionPolynomial, Cubic.disc] ring1 section CharTwo variable [CharP R 2] lemma twoTorsionPolynomial_of_char_two : W.twoTorsionPolynomial = ⟨0, W.b₂, 0, W.b₆⟩ := by rw [twoTorsionPolynomial] ext <;> dsimp · linear_combination 2 * CharP.cast_eq_zero R 2 · linear_combination W.b₄ * CharP.cast_eq_zero R 2 lemma twoTorsionPolynomial_disc_of_char_two : W.twoTorsionPolynomial.disc = 0 := by linear_combination W.twoTorsionPolynomial_disc + 8 * W.Δ * CharP.cast_eq_zero R 2 end CharTwo section CharThree variable [CharP R 3] lemma twoTorsionPolynomial_of_char_three : W.twoTorsionPolynomial = ⟨1, W.b₂, -W.b₄, W.b₆⟩ := by rw [twoTorsionPolynomial] ext <;> dsimp · linear_combination CharP.cast_eq_zero R 3 · linear_combination W.b₄ * CharP.cast_eq_zero R 3 lemma twoTorsionPolynomial_disc_of_char_three : W.twoTorsionPolynomial.disc = W.Δ := by linear_combination W.twoTorsionPolynomial_disc + 5 * W.Δ * CharP.cast_eq_zero R 3 end CharThree -- TODO: change to `[IsUnit ...]` once #17458 is merged lemma twoTorsionPolynomial_disc_isUnit (hu : IsUnit (2 : R)) : IsUnit W.twoTorsionPolynomial.disc ↔ IsUnit W.Δ := by rw [twoTorsionPolynomial_disc, IsUnit.mul_iff, show (16 : R) = 2 ^ 4 by norm_num1] exact and_iff_right <| hu.pow 4 -- TODO: change to `[IsUnit ...]` once #17458 is merged -- TODO: In this case `IsUnit W.Δ` is just `W.IsElliptic`, consider removing/rephrasing this result lemma twoTorsionPolynomial_disc_ne_zero [Nontrivial R] (hu : IsUnit (2 : R)) (hΔ : IsUnit W.Δ) : W.twoTorsionPolynomial.disc ≠ 0 := ((W.twoTorsionPolynomial_disc_isUnit hu).mpr hΔ).ne_zero end TorsionPolynomial /-! ## Elliptic curves -/ -- TODO: change to `protected abbrev IsElliptic := IsUnit W.Δ` once #17458 is merged /-- `WeierstrassCurve.IsElliptic` is a typeclass which asserts that a Weierstrass curve is an elliptic curve: that its discriminant is a unit. Note that this definition is only mathematically accurate for certain rings whose Picard group has trivial 12-torsion, such as a field or a PID. -/ @[mk_iff] protected class IsElliptic : Prop where isUnit : IsUnit W.Δ variable [W.IsElliptic] lemma isUnit_Δ : IsUnit W.Δ := IsElliptic.isUnit /-- The discriminant `Δ'` of an elliptic curve over `R`, which is given as a unit in `R`. Note that to prove two equal elliptic curves have the same `Δ'`, you need to use `simp_rw`, as `rw` cannot transfer instance `WeierstrassCurve.IsElliptic` automatically. -/ noncomputable def Δ' : Rˣ := W.isUnit_Δ.unit /-- The discriminant `Δ'` of an elliptic curve is equal to the discriminant `Δ` of it as a Weierstrass curve. -/ @[simp] lemma coe_Δ' : W.Δ' = W.Δ := rfl /-- The j-invariant `j` of an elliptic curve, which is invariant under isomorphisms over `R`. Note that to prove two equal elliptic curves have the same `j`, you need to use `simp_rw`, as `rw` cannot transfer instance `WeierstrassCurve.IsElliptic` automatically. -/ noncomputable def j : R := W.Δ'⁻¹ * W.c₄ ^ 3 /-- A variant of `WeierstrassCurve.j_eq_zero_iff` without assuming a reduced ring. -/ lemma j_eq_zero_iff' : W.j = 0 ↔ W.c₄ ^ 3 = 0 := by rw [j, Units.mul_right_eq_zero] lemma j_eq_zero (h : W.c₄ = 0) : W.j = 0 := by rw [j_eq_zero_iff', h, zero_pow three_ne_zero] lemma j_eq_zero_iff [IsReduced R] : W.j = 0 ↔ W.c₄ = 0 := by rw [j_eq_zero_iff', IsReduced.pow_eq_zero_iff three_ne_zero] section CharTwo variable [CharP R 2] lemma j_of_char_two : W.j = W.Δ'⁻¹ * W.a₁ ^ 12 := by rw [j, W.c₄_of_char_two, ← pow_mul] /-- A variant of `WeierstrassCurve.j_eq_zero_iff_of_char_two` without assuming a reduced ring. -/ lemma j_eq_zero_iff_of_char_two' : W.j = 0 ↔ W.a₁ ^ 12 = 0 := by rw [j_of_char_two, Units.mul_right_eq_zero] lemma j_eq_zero_of_char_two (h : W.a₁ = 0) : W.j = 0 := by rw [j_eq_zero_iff_of_char_two', h, zero_pow (Nat.succ_ne_zero _)] lemma j_eq_zero_iff_of_char_two [IsReduced R] : W.j = 0 ↔ W.a₁ = 0 := by rw [j_eq_zero_iff_of_char_two', IsReduced.pow_eq_zero_iff (Nat.succ_ne_zero _)] end CharTwo section CharThree variable [CharP R 3] lemma j_of_char_three : W.j = W.Δ'⁻¹ * W.b₂ ^ 6 := by rw [j, W.c₄_of_char_three, ← pow_mul] /-- A variant of `WeierstrassCurve.j_eq_zero_iff_of_char_three` without assuming a reduced ring. -/ lemma j_eq_zero_iff_of_char_three' : W.j = 0 ↔ W.b₂ ^ 6 = 0 := by rw [j_of_char_three, Units.mul_right_eq_zero] lemma j_eq_zero_of_char_three (h : W.b₂ = 0) : W.j = 0 := by rw [j_eq_zero_iff_of_char_three', h, zero_pow (Nat.succ_ne_zero _)] lemma j_eq_zero_iff_of_char_three [IsReduced R] : W.j = 0 ↔ W.b₂ = 0 := by rw [j_eq_zero_iff_of_char_three', IsReduced.pow_eq_zero_iff (Nat.succ_ne_zero _)] end CharThree -- TODO: this is defeq to `twoTorsionPolynomial_disc_ne_zero` once #17458 is merged, -- TODO: consider removing/rephrasing this result lemma twoTorsionPolynomial_disc_ne_zero_of_isElliptic [Nontrivial R] (hu : IsUnit (2 : R)) : W.twoTorsionPolynomial.disc ≠ 0 := W.twoTorsionPolynomial_disc_ne_zero hu W.isUnit_Δ section BaseChange /-! ### Maps and base changes -/ variable {A : Type v} [CommRing A] (f : R →+* A) instance : (W.map f).IsElliptic := by simp only [isElliptic_iff, map_Δ, W.isUnit_Δ.map] set_option linter.docPrime false in lemma coe_map_Δ' : (W.map f).Δ' = f W.Δ' := by rw [coe_Δ', map_Δ, coe_Δ'] set_option linter.docPrime false in @[simp] lemma map_Δ' : (W.map f).Δ' = Units.map f W.Δ' := by ext exact W.coe_map_Δ' f set_option linter.docPrime false in lemma coe_inv_map_Δ' : (W.map f).Δ'⁻¹ = f ↑W.Δ'⁻¹ := by simp set_option linter.docPrime false in lemma inv_map_Δ' : (W.map f).Δ'⁻¹ = Units.map f W.Δ'⁻¹ := by simp @[simp] lemma map_j : (W.map f).j = f W.j := by rw [j, coe_inv_map_Δ', map_c₄, j, map_mul, map_pow] end BaseChange end WeierstrassCurve
Mathlib/AlgebraicGeometry/EllipticCurve/Weierstrass.lean
725
726
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.Nat.ModEq import Mathlib.Data.Nat.Prime.Basic import Mathlib.NumberTheory.Zsqrtd.Basic /-! # Pell's equation and Matiyasevic's theorem This file solves Pell's equation, i.e. integer solutions to `x ^ 2 - d * y ^ 2 = 1` *in the special case that `d = a ^ 2 - 1`*. This is then applied to prove Matiyasevic's theorem that the power function is Diophantine, which is the last key ingredient in the solution to Hilbert's tenth problem. For the definition of Diophantine function, see `NumberTheory.Dioph`. For results on Pell's equation for arbitrary (positive, non-square) `d`, see `NumberTheory.Pell`. ## Main definition * `pell` is a function assigning to a natural number `n` the `n`-th solution to Pell's equation constructed recursively from the initial solution `(0, 1)`. ## Main statements * `eq_pell` shows that every solution to Pell's equation is recursively obtained using `pell` * `matiyasevic` shows that a certain system of Diophantine equations has a solution if and only if the first variable is the `x`-component in a solution to Pell's equation - the key step towards Hilbert's tenth problem in Davis' version of Matiyasevic's theorem. * `eq_pow_of_pell` shows that the power function is Diophantine. ## Implementation notes The proof of Matiyasevic's theorem doesn't follow Matiyasevic's original account of using Fibonacci numbers but instead Davis' variant of using solutions to Pell's equation. ## References * [M. Carneiro, _A Lean formalization of Matiyasevič's theorem_][carneiro2018matiyasevic] * [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916] ## Tags Pell's equation, Matiyasevic's theorem, Hilbert's tenth problem -/ namespace Pell open Nat section variable {d : ℤ} /-- The property of being a solution to the Pell equation, expressed as a property of elements of `ℤ√d`. -/ def IsPell : ℤ√d → Prop | ⟨x, y⟩ => x * x - d * y * y = 1 theorem isPell_norm : ∀ {b : ℤ√d}, IsPell b ↔ b * star b = 1 | ⟨x, y⟩ => by simp [Zsqrtd.ext_iff, IsPell, mul_comm]; ring_nf theorem isPell_iff_mem_unitary : ∀ {b : ℤ√d}, IsPell b ↔ b ∈ unitary (ℤ√d) | ⟨x, y⟩ => by rw [unitary.mem_iff, isPell_norm, mul_comm (star _), and_self_iff] theorem isPell_mul {b c : ℤ√d} (hb : IsPell b) (hc : IsPell c) : IsPell (b * c) := isPell_norm.2 (by simp [mul_comm, mul_left_comm c, mul_assoc, star_mul, isPell_norm.1 hb, isPell_norm.1 hc]) theorem isPell_star : ∀ {b : ℤ√d}, IsPell b ↔ IsPell (star b) | ⟨x, y⟩ => by simp [IsPell, Zsqrtd.star_mk] end section variable {a : ℕ} (a1 : 1 < a) private def d (_a1 : 1 < a) := a * a - 1 @[simp] theorem d_pos : 0 < d a1 := tsub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) (by decide) (Nat.zero_le _) : 1 * 1 < a * a) -- TODO(lint): Fix double namespace issue /-- The Pell sequences, i.e. the sequence of integer solutions to `x ^ 2 - d * y ^ 2 = 1`, where `d = a ^ 2 - 1`, defined together in mutual recursion. -/ --@[nolint dup_namespace] def pell : ℕ → ℕ × ℕ | 0 => (1, 0) | n+1 => ((pell n).1 * a + d a1 * (pell n).2, (pell n).1 + (pell n).2 * a) /-- The Pell `x` sequence. -/ def xn (n : ℕ) : ℕ := (pell a1 n).1 /-- The Pell `y` sequence. -/ def yn (n : ℕ) : ℕ := (pell a1 n).2 @[simp] theorem pell_val (n : ℕ) : pell a1 n = (xn a1 n, yn a1 n) := show pell a1 n = ((pell a1 n).1, (pell a1 n).2) from match pell a1 n with | (_, _) => rfl @[simp] theorem xn_zero : xn a1 0 = 1 := rfl @[simp] theorem yn_zero : yn a1 0 = 0 := rfl @[simp] theorem xn_succ (n : ℕ) : xn a1 (n + 1) = xn a1 n * a + d a1 * yn a1 n := rfl @[simp] theorem yn_succ (n : ℕ) : yn a1 (n + 1) = xn a1 n + yn a1 n * a := rfl theorem xn_one : xn a1 1 = a := by simp theorem yn_one : yn a1 1 = 1 := by simp /-- The Pell `x` sequence, considered as an integer sequence. -/ def xz (n : ℕ) : ℤ := xn a1 n /-- The Pell `y` sequence, considered as an integer sequence. -/ def yz (n : ℕ) : ℤ := yn a1 n section /-- The element `a` such that `d = a ^ 2 - 1`, considered as an integer. -/ def az (a : ℕ) : ℤ := a end include a1 in theorem asq_pos : 0 < a * a := le_trans (le_of_lt a1) (by have := @Nat.mul_le_mul_left 1 a a (le_of_lt a1); rwa [mul_one] at this) theorem dz_val : ↑(d a1) = az a * az a - 1 := have : 1 ≤ a * a := asq_pos a1 by rw [Pell.d, Int.ofNat_sub this]; rfl @[simp] theorem xz_succ (n : ℕ) : (xz a1 (n + 1)) = xz a1 n * az a + d a1 * yz a1 n := rfl @[simp] theorem yz_succ (n : ℕ) : yz a1 (n + 1) = xz a1 n + yz a1 n * az a := rfl /-- The Pell sequence can also be viewed as an element of `ℤ√d` -/ def pellZd (n : ℕ) : ℤ√(d a1) := ⟨xn a1 n, yn a1 n⟩ @[simp] theorem pellZd_re (n : ℕ) : (pellZd a1 n).re = xn a1 n := rfl @[simp] theorem pellZd_im (n : ℕ) : (pellZd a1 n).im = yn a1 n := rfl theorem isPell_nat {x y : ℕ} : IsPell (⟨x, y⟩ : ℤ√(d a1)) ↔ x * x - d a1 * y * y = 1 := ⟨fun h => (Nat.cast_inj (R := ℤ)).1 (by rw [Int.ofNat_sub (Int.le_of_ofNat_le_ofNat <| Int.le.intro_sub _ h)]; exact h), fun h => show ((x * x : ℕ) - (d a1 * y * y : ℕ) : ℤ) = 1 by rw [← Int.ofNat_sub <| le_of_lt <| Nat.lt_of_sub_eq_succ h, h]; rfl⟩ @[simp] theorem pellZd_succ (n : ℕ) : pellZd a1 (n + 1) = pellZd a1 n * ⟨a, 1⟩ := by ext <;> simp theorem isPell_one : IsPell (⟨a, 1⟩ : ℤ√(d a1)) := show az a * az a - d a1 * 1 * 1 = 1 by simp [dz_val] theorem isPell_pellZd : ∀ n : ℕ, IsPell (pellZd a1 n) | 0 => rfl | n + 1 => by let o := isPell_one a1 simpa using Pell.isPell_mul (isPell_pellZd n) o @[simp] theorem pell_eqz (n : ℕ) : xz a1 n * xz a1 n - d a1 * yz a1 n * yz a1 n = 1 := isPell_pellZd a1 n @[simp] theorem pell_eq (n : ℕ) : xn a1 n * xn a1 n - d a1 * yn a1 n * yn a1 n = 1 := let pn := pell_eqz a1 n have h : (↑(xn a1 n * xn a1 n) : ℤ) - ↑(d a1 * yn a1 n * yn a1 n) = 1 := by repeat' rw [Int.natCast_mul]; exact pn have hl : d a1 * yn a1 n * yn a1 n ≤ xn a1 n * xn a1 n := Nat.cast_le.1 <| Int.le.intro _ <| add_eq_of_eq_sub' <| Eq.symm h (Nat.cast_inj (R := ℤ)).1 (by rw [Int.ofNat_sub hl]; exact h) instance dnsq : Zsqrtd.Nonsquare (d a1) := ⟨fun n h => have : n * n + 1 = a * a := by rw [← h]; exact Nat.succ_pred_eq_of_pos (asq_pos a1) have na : n < a := Nat.mul_self_lt_mul_self_iff.1 (by rw [← this]; exact Nat.lt_succ_self _) have : (n + 1) * (n + 1) ≤ n * n + 1 := by rw [this]; exact Nat.mul_self_le_mul_self na have : n + n ≤ 0 := @Nat.le_of_add_le_add_right _ (n * n + 1) _ (by ring_nf at this ⊢; assumption) Nat.ne_of_gt (d_pos a1) <| by rwa [Nat.eq_zero_of_le_zero ((Nat.le_add_left _ _).trans this)] at h⟩ theorem xn_ge_a_pow : ∀ n : ℕ, a ^ n ≤ xn a1 n | 0 => le_refl 1 | n + 1 => by simp only [_root_.pow_succ, xn_succ] exact le_trans (Nat.mul_le_mul_right _ (xn_ge_a_pow n)) (Nat.le_add_right _ _) theorem n_lt_xn (n) : n < xn a1 n := lt_of_lt_of_le (Nat.lt_pow_self a1) (xn_ge_a_pow a1 n) theorem x_pos (n) : 0 < xn a1 n := lt_of_le_of_lt (Nat.zero_le n) (n_lt_xn a1 n) theorem eq_pell_lem : ∀ (n) (b : ℤ√(d a1)), 1 ≤ b → IsPell b → b ≤ pellZd a1 n → ∃ n, b = pellZd a1 n | 0, _ => fun h1 _ hl => ⟨0, @Zsqrtd.le_antisymm _ (dnsq a1) _ _ hl h1⟩ | n + 1, b => fun h1 hp h => have a1p : (0 : ℤ√(d a1)) ≤ ⟨a, 1⟩ := trivial have am1p : (0 : ℤ√(d a1)) ≤ ⟨a, -1⟩ := show (_ : Nat) ≤ _ by simp; exact Nat.pred_le _ have a1m : (⟨a, 1⟩ * ⟨a, -1⟩ : ℤ√(d a1)) = 1 := isPell_norm.1 (isPell_one a1) if ha : (⟨↑a, 1⟩ : ℤ√(d a1)) ≤ b then let ⟨m, e⟩ := eq_pell_lem n (b * ⟨a, -1⟩) (by rw [← a1m]; exact mul_le_mul_of_nonneg_right ha am1p) (isPell_mul hp (isPell_star.1 (isPell_one a1))) (by have t := mul_le_mul_of_nonneg_right h am1p rwa [pellZd_succ, mul_assoc, a1m, mul_one] at t) ⟨m + 1, by rw [show b = b * ⟨a, -1⟩ * ⟨a, 1⟩ by rw [mul_assoc, Eq.trans (mul_comm _ _) a1m]; simp, pellZd_succ, e]⟩ else suffices ¬1 < b from ⟨0, show b = 1 from (Or.resolve_left (lt_or_eq_of_le h1) this).symm⟩ fun h1l => by obtain ⟨x, y⟩ := b exact by have bm : (_ * ⟨_, _⟩ : ℤ√d a1) = 1 := Pell.isPell_norm.1 hp have y0l : (0 : ℤ√d a1) < ⟨x - x, y - -y⟩ := sub_lt_sub h1l fun hn : (1 : ℤ√d a1) ≤ ⟨x, -y⟩ => by have t := mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1) rw [bm, mul_one] at t exact h1l t have yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩ := show (⟨x, y⟩ - ⟨x, -y⟩ : ℤ√d a1) < ⟨a, 1⟩ - ⟨a, -1⟩ from sub_lt_sub ha fun hn : (⟨x, -y⟩ : ℤ√d a1) ≤ ⟨a, -1⟩ => by have t := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1)) a1p rw [bm, one_mul, mul_assoc, Eq.trans (mul_comm _ _) a1m, mul_one] at t exact ha t simp only [sub_self, sub_neg_eq_add] at y0l; simp only [Zsqrtd.neg_re, add_neg_cancel, Zsqrtd.neg_im, neg_neg] at yl2 exact match y, y0l, (yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩) with | 0, y0l, _ => y0l (le_refl 0) | (y + 1 : ℕ), _, yl2 => yl2 (Zsqrtd.le_of_le_le (by simp [sub_eq_add_neg]) (let t := Int.ofNat_le_ofNat_of_le (Nat.succ_pos y) add_le_add t t)) | Int.negSucc _, y0l, _ => y0l trivial theorem eq_pellZd (b : ℤ√(d a1)) (b1 : 1 ≤ b) (hp : IsPell b) : ∃ n, b = pellZd a1 n := let ⟨n, h⟩ := @Zsqrtd.le_arch (d a1) b eq_pell_lem a1 n b b1 hp <| h.trans <| by rw [Zsqrtd.natCast_val] exact Zsqrtd.le_of_le_le (Int.ofNat_le_ofNat_of_le <| le_of_lt <| n_lt_xn _ _)
(Int.ofNat_zero_le _) /-- Every solution to **Pell's equation** is recursively obtained from the initial solution `(1,0)` using the recursion `pell`. -/ theorem eq_pell {x y : ℕ} (hp : x * x - d a1 * y * y = 1) : ∃ n, x = xn a1 n ∧ y = yn a1 n := have : (1 : ℤ√(d a1)) ≤ ⟨x, y⟩ := match x, hp with | 0, (hp : 0 - _ = 1) => by rw [zero_tsub] at hp; contradiction | x + 1, _hp => Zsqrtd.le_of_le_le (Int.ofNat_le_ofNat_of_le <| Nat.succ_pos x) (Int.ofNat_zero_le _) let ⟨m, e⟩ := eq_pellZd a1 ⟨x, y⟩ this ((isPell_nat a1).2 hp) ⟨m, match x, y, e with | _, _, rfl => ⟨rfl, rfl⟩⟩ theorem pellZd_add (m) : ∀ n, pellZd a1 (m + n) = pellZd a1 m * pellZd a1 n | 0 => (mul_one _).symm | n + 1 => by rw [← add_assoc, pellZd_succ, pellZd_succ, pellZd_add _ n, ← mul_assoc] theorem xn_add (m n) : xn a1 (m + n) = xn a1 m * xn a1 n + d a1 * yn a1 m * yn a1 n := by injection pellZd_add a1 m n with h _ zify rw [h] simp [pellZd] theorem yn_add (m n) : yn a1 (m + n) = xn a1 m * yn a1 n + yn a1 m * xn a1 n := by injection pellZd_add a1 m n with _ h zify rw [h] simp [pellZd] theorem pellZd_sub {m n} (h : n ≤ m) : pellZd a1 (m - n) = pellZd a1 m * star (pellZd a1 n) := by let t := pellZd_add a1 n (m - n) rw [add_tsub_cancel_of_le h] at t rw [t, mul_comm (pellZd _ n) _, mul_assoc, isPell_norm.1 (isPell_pellZd _ _), mul_one] theorem xz_sub {m n} (h : n ≤ m) : xz a1 (m - n) = xz a1 m * xz a1 n - d a1 * yz a1 m * yz a1 n := by rw [sub_eq_add_neg, ← mul_neg] exact congr_arg Zsqrtd.re (pellZd_sub a1 h) theorem yz_sub {m n} (h : n ≤ m) : yz a1 (m - n) = xz a1 n * yz a1 m - xz a1 m * yz a1 n := by rw [sub_eq_add_neg, ← mul_neg, mul_comm, add_comm] exact congr_arg Zsqrtd.im (pellZd_sub a1 h) theorem xy_coprime (n) : (xn a1 n).Coprime (yn a1 n) :=
Mathlib/NumberTheory/PellMatiyasevic.lean
287
332
/- Copyright (c) 2022 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Riccardo Brasca, Eric Rodriguez -/ import Mathlib.Data.PNat.Prime import Mathlib.NumberTheory.Cyclotomic.Basic import Mathlib.RingTheory.Adjoin.PowerBasis import Mathlib.RingTheory.Norm.Basic import Mathlib.RingTheory.Polynomial.Cyclotomic.Eval import Mathlib.RingTheory.Polynomial.Cyclotomic.Expand import Mathlib.RingTheory.SimpleModule.Basic /-! # Primitive roots in cyclotomic fields If `IsCyclotomicExtension {n} A B`, we define an element `zeta n A B : B` that is a primitive `n`th-root of unity in `B` and we study its properties. We also prove related theorems under the more general assumption of just being a primitive root, for reasons described in the implementation details section. ## Main definitions * `IsCyclotomicExtension.zeta n A B`: if `IsCyclotomicExtension {n} A B`, than `zeta n A B` is a primitive `n`-th root of unity in `B`. * `IsPrimitiveRoot.powerBasis`: if `K` and `L` are fields such that `IsCyclotomicExtension {n} K L`, then `IsPrimitiveRoot.powerBasis` gives a `K`-power basis for `L` given a primitive root `ζ`. * `IsPrimitiveRoot.embeddingsEquivPrimitiveRoots`: the equivalence between `L →ₐ[K] A` and `primitiveroots n A` given by the choice of `ζ`. ## Main results * `IsCyclotomicExtension.zeta_spec`: `zeta n A B` is a primitive `n`-th root of unity. * `IsCyclotomicExtension.finrank`: if `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the `finrank` of a cyclotomic extension is `n.totient`. * `IsPrimitiveRoot.norm_eq_one`: if `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), the norm of a primitive root is `1` if `n ≠ 2`. * `IsPrimitiveRoot.sub_one_norm_eq_eval_cyclotomic`: if `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `ζ - 1` is `eval 1 (cyclotomic n ℤ)`, for a primitive root `ζ`. We also prove the analogous of this result for `zeta`. * `IsPrimitiveRoot.norm_pow_sub_one_of_prime_pow_ne_two` : if `Irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` `p ^ (k - s + 1) ≠ 2`. See the following lemmas for similar results. We also prove the analogous of this result for `zeta`. * `IsPrimitiveRoot.norm_sub_one_of_prime_ne_two` : if `Irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `ζ - 1` is `p`. We also prove the analogous of this result for `zeta`. * `IsPrimitiveRoot.embeddingsEquivPrimitiveRoots`: the equivalence between `L →ₐ[K] A` and `primitiveRoots n A` given by the choice of `ζ`. ## Implementation details `zeta n A B` is defined as any primitive root of unity in `B`, - this must exist, by definition of `IsCyclotomicExtension`. It is not true in general that it is a root of `cyclotomic n B`, but this holds if `isDomain B` and `NeZero (↑n : B)`. `zeta n A B` is defined using `Exists.choose`, which means we cannot control it. For example, in normal mathematics, we can demand that `(zeta p ℤ ℤ[ζₚ] : ℚ(ζₚ))` is equal to `zeta p ℚ ℚ(ζₚ)`, as we are just choosing "an arbitrary primitive root" and we can internally specify that our choices agree. This is not the case here, and it is indeed impossible to prove that these two are equal. Therefore, whenever possible, we prove our results for any primitive root, and only at the "final step", when we need to provide an "explicit" primitive root, we use `zeta`. -/ open Polynomial Algebra Finset Module IsCyclotomicExtension Nat PNat Set open scoped IntermediateField universe u v w z variable {p n : ℕ+} (A : Type w) (B : Type z) (K : Type u) {L : Type v} (C : Type w) variable [CommRing A] [CommRing B] [Algebra A B] [IsCyclotomicExtension {n} A B] section Zeta namespace IsCyclotomicExtension variable (n) /-- If `B` is an `n`-th cyclotomic extension of `A`, then `zeta n A B` is a primitive root of unity in `B`. -/ noncomputable def zeta : B := (exists_prim_root A <| Set.mem_singleton n : ∃ r : B, IsPrimitiveRoot r n).choose /-- `zeta n A B` is a primitive `n`-th root of unity. -/ @[simp] theorem zeta_spec : IsPrimitiveRoot (zeta n A B) n := Classical.choose_spec (exists_prim_root A (Set.mem_singleton n) : ∃ r : B, IsPrimitiveRoot r n) theorem aeval_zeta [IsDomain B] [NeZero ((n : ℕ) : B)] : aeval (zeta n A B) (cyclotomic n A) = 0 := by rw [aeval_def, ← eval_map, ← IsRoot.def, map_cyclotomic, isRoot_cyclotomic_iff] exact zeta_spec n A B theorem zeta_isRoot [IsDomain B] [NeZero ((n : ℕ) : B)] : IsRoot (cyclotomic n B) (zeta n A B) := by convert aeval_zeta n A B using 0 rw [IsRoot.def, aeval_def, eval₂_eq_eval_map, map_cyclotomic] theorem zeta_pow : zeta n A B ^ (n : ℕ) = 1 := (zeta_spec n A B).pow_eq_one end IsCyclotomicExtension end Zeta section NoOrder variable [Field K] [CommRing L] [IsDomain L] [Algebra K L] [IsCyclotomicExtension {n} K L] {ζ : L} (hζ : IsPrimitiveRoot ζ n) namespace IsPrimitiveRoot variable {C} /-- The `PowerBasis` given by a primitive root `η`. -/ @[simps!] protected noncomputable def powerBasis : PowerBasis K L := -- this is purely an optimization letI pb := Algebra.adjoin.powerBasis <| (integral {n} K L).isIntegral ζ pb.map <| (Subalgebra.equivOfEq _ _ (IsCyclotomicExtension.adjoin_primitive_root_eq_top hζ)).trans Subalgebra.topEquiv theorem powerBasis_gen_mem_adjoin_zeta_sub_one : (hζ.powerBasis K).gen ∈ adjoin K ({ζ - 1} : Set L) := by rw [powerBasis_gen, adjoin_singleton_eq_range_aeval, AlgHom.mem_range] exact ⟨X + 1, by simp⟩ /-- The `PowerBasis` given by `η - 1`. -/ @[simps!] noncomputable def subOnePowerBasis : PowerBasis K L := (hζ.powerBasis K).ofGenMemAdjoin (((integral {n} K L).isIntegral ζ).sub isIntegral_one) (hζ.powerBasis_gen_mem_adjoin_zeta_sub_one _) variable {K} (C) -- We are not using @[simps] to avoid a timeout. /-- The equivalence between `L →ₐ[K] C` and `primitiveRoots n C` given by a primitive root `ζ`. -/ noncomputable def embeddingsEquivPrimitiveRoots (C : Type*) [CommRing C] [IsDomain C] [Algebra K C] (hirr : Irreducible (cyclotomic n K)) : (L →ₐ[K] C) ≃ primitiveRoots n C := (hζ.powerBasis K).liftEquiv.trans { toFun := fun x => by haveI := IsCyclotomicExtension.neZero' n K L haveI hn := NeZero.of_faithfulSMul K C n refine ⟨x.1, ?_⟩ cases x rwa [mem_primitiveRoots n.pos, ← isRoot_cyclotomic_iff, IsRoot.def, ← map_cyclotomic _ (algebraMap K C), hζ.minpoly_eq_cyclotomic_of_irreducible hirr, ← eval₂_eq_eval_map, ← aeval_def] invFun := fun x => by haveI := IsCyclotomicExtension.neZero' n K L haveI hn := NeZero.of_faithfulSMul K C n refine ⟨x.1, ?_⟩ cases x rwa [aeval_def, eval₂_eq_eval_map, hζ.powerBasis_gen K, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, map_cyclotomic, ← IsRoot.def, isRoot_cyclotomic_iff, ← mem_primitiveRoots n.pos] left_inv := fun _ => Subtype.ext rfl right_inv := fun _ => Subtype.ext rfl } -- Porting note: renamed argument `φ`: "expected '_' or identifier" @[simp] theorem embeddingsEquivPrimitiveRoots_apply_coe (C : Type*) [CommRing C] [IsDomain C] [Algebra K C] (hirr : Irreducible (cyclotomic n K)) (φ' : L →ₐ[K] C) : (hζ.embeddingsEquivPrimitiveRoots C hirr φ' : C) = φ' ζ := rfl end IsPrimitiveRoot namespace IsCyclotomicExtension variable {K} (L) /-- If `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the `finrank` of a cyclotomic extension is `n.totient`. -/ theorem finrank (hirr : Irreducible (cyclotomic n K)) : finrank K L = (n : ℕ).totient := by haveI := IsCyclotomicExtension.neZero' n K L rw [((zeta_spec n K L).powerBasis K).finrank, IsPrimitiveRoot.powerBasis_dim, ← (zeta_spec n K L).minpoly_eq_cyclotomic_of_irreducible hirr, natDegree_cyclotomic] variable {L} in /-- If `L` contains both a primitive `p`-th root of unity and `q`-th root of unity, and `Irreducible (cyclotomic (lcm p q) K)` (in particular for `K = ℚ`), then the `finrank K L` is at least `(lcm p q).totient`. -/ theorem _root_.IsPrimitiveRoot.lcm_totient_le_finrank [FiniteDimensional K L] {p q : ℕ} {x y : L} (hx : IsPrimitiveRoot x p) (hy : IsPrimitiveRoot y q) (hirr : Irreducible (cyclotomic (Nat.lcm p q) K)) : (Nat.lcm p q).totient ≤ Module.finrank K L := by rcases Nat.eq_zero_or_pos p with (rfl | hppos) · simp rcases Nat.eq_zero_or_pos q with (rfl | hqpos) · simp let z := x ^ (p / factorizationLCMLeft p q) * y ^ (q / factorizationLCMRight p q) let k := PNat.lcm ⟨p, hppos⟩ ⟨q, hqpos⟩ have : IsPrimitiveRoot z k := hx.pow_mul_pow_lcm hy hppos.ne' hqpos.ne' haveI := IsPrimitiveRoot.adjoin_isCyclotomicExtension K this convert Submodule.finrank_le (Subalgebra.toSubmodule (adjoin K {z})) rw [show Nat.lcm p q = (k : ℕ) from rfl] at hirr simpa using (IsCyclotomicExtension.finrank (Algebra.adjoin K {z}) hirr).symm end IsCyclotomicExtension end NoOrder section Norm namespace IsPrimitiveRoot section Field variable {K} [Field K] [NumberField K] variable (n) in /-- If a `n`-th cyclotomic extension of `ℚ` contains a primitive `l`-th root of unity, then `l ∣ 2 * n`. -/ theorem dvd_of_isCyclotomicExtension [IsCyclotomicExtension {n} ℚ K] {ζ : K} {l : ℕ} (hζ : IsPrimitiveRoot ζ l) (hl : l ≠ 0) : l ∣ 2 * n := by have hl : NeZero l := ⟨hl⟩ have hroot := IsCyclotomicExtension.zeta_spec n ℚ K have key := IsPrimitiveRoot.lcm_totient_le_finrank hζ hroot (cyclotomic.irreducible_rat <| Nat.lcm_pos (Nat.pos_of_ne_zero hl.1) n.2) rw [IsCyclotomicExtension.finrank K (cyclotomic.irreducible_rat n.2)] at key rcases _root_.dvd_lcm_right l n with ⟨r, hr⟩ have ineq := Nat.totient_super_multiplicative n r rw [← hr] at ineq replace key := (mul_le_iff_le_one_right (Nat.totient_pos.2 n.2)).mp (le_trans ineq key) have rpos : 0 < r := by refine Nat.pos_of_ne_zero (fun h ↦ ?_) simp only [h, mul_zero, _root_.lcm_eq_zero_iff, PNat.ne_zero, or_false] at hr exact hl.1 hr replace key := (Nat.dvd_prime Nat.prime_two).1 (Nat.dvd_two_of_totient_le_one rpos key) rcases key with (key | key) · rw [key, mul_one] at hr rw [← hr] exact dvd_mul_of_dvd_right (_root_.dvd_lcm_left l ↑n) 2 · rw [key, mul_comm] at hr simpa [← hr] using _root_.dvd_lcm_left _ _ /-- If `x` is a root of unity (spelled as `IsOfFinOrder x`) in an `n`-th cyclotomic extension of `ℚ`, where `n` is odd, and `ζ` is a primitive `n`-th root of unity, then there exist `r` such that `x = (-ζ)^r`. -/ theorem exists_neg_pow_of_isOfFinOrder [IsCyclotomicExtension {n} ℚ K] (hno : Odd (n : ℕ)) {ζ x : K} (hζ : IsPrimitiveRoot ζ n) (hx : IsOfFinOrder x) : ∃ r : ℕ, x = (-ζ) ^ r := by have hnegζ : IsPrimitiveRoot (-ζ) (2 * n) := by convert IsPrimitiveRoot.orderOf (-ζ) rw [neg_eq_neg_one_mul, (Commute.all _ _).orderOf_mul_eq_mul_orderOf_of_coprime] · simp [hζ.eq_orderOf] · simp [← hζ.eq_orderOf, hno] obtain ⟨k, hkpos, hkn⟩ := isOfFinOrder_iff_pow_eq_one.1 hx obtain ⟨l, hl, hlroot⟩ := (isRoot_of_unity_iff hkpos _).1 hkn have hlzero : NeZero l := ⟨fun h ↦ by simp [h] at hl⟩ have : NeZero (l : K) := ⟨NeZero.natCast_ne l K⟩ rw [isRoot_cyclotomic_iff] at hlroot obtain ⟨a, ha⟩ := hlroot.dvd_of_isCyclotomicExtension n hlzero.1 replace hlroot : x ^ (2 * (n : ℕ)) = 1 := by rw [ha, pow_mul, hlroot.pow_eq_one, one_pow] obtain ⟨s, -, hs⟩ := hnegζ.eq_pow_of_pow_eq_one hlroot exact ⟨s, hs.symm⟩ /-- If `x` is a root of unity (spelled as `IsOfFinOrder x`) in an `n`-th cyclotomic extension of `ℚ`, where `n` is odd, and `ζ` is a primitive `n`-th root of unity, then there exists `r < n` such that `x = ζ^r` or `x = -ζ^r`. -/ theorem exists_pow_or_neg_mul_pow_of_isOfFinOrder [IsCyclotomicExtension {n} ℚ K] (hno : Odd (n : ℕ)) {ζ x : K} (hζ : IsPrimitiveRoot ζ n) (hx : IsOfFinOrder x) : ∃ r : ℕ, r < n ∧ (x = ζ ^ r ∨ x = -ζ ^ r) := by obtain ⟨r, hr⟩ := hζ.exists_neg_pow_of_isOfFinOrder hno hx refine ⟨r % n, Nat.mod_lt _ n.2, ?_⟩ rw [show ζ ^ (r % ↑n) = ζ ^ r from (IsPrimitiveRoot.eq_orderOf hζ).symm ▸ pow_mod_orderOf .., hr] rcases Nat.even_or_odd r with (h | h) <;> simp [neg_pow, h.neg_one_pow] end Field section CommRing variable [CommRing L] {ζ : L} variable {K} [Field K] [Algebra K L] /-- This mathematically trivial result is complementary to `norm_eq_one` below. -/ theorem norm_eq_neg_one_pow (hζ : IsPrimitiveRoot ζ 2) [IsDomain L] : norm K ζ = (-1 : K) ^ finrank K L := by rw [hζ.eq_neg_one_of_two_right, show -1 = algebraMap K L (-1) by simp, Algebra.norm_algebraMap] variable (hζ : IsPrimitiveRoot ζ n) include hζ /-- If `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), the norm of a primitive root is `1` if `n ≠ 2`. -/ theorem norm_eq_one [IsDomain L] [IsCyclotomicExtension {n} K L] (hn : n ≠ 2) (hirr : Irreducible (cyclotomic n K)) : norm K ζ = 1 := by haveI := IsCyclotomicExtension.neZero' n K L by_cases h1 : n = 1 · rw [h1, one_coe, one_right_iff] at hζ rw [hζ, show 1 = algebraMap K L 1 by simp, Algebra.norm_algebraMap, one_pow] · replace h1 : 2 ≤ n := by by_contra! h exact h1 (PNat.eq_one_of_lt_two h) -- Porting note: specifying the type of `cyclotomic_coeff_zero K h1` was not needed. rw [← hζ.powerBasis_gen K, PowerBasis.norm_gen_eq_coeff_zero_minpoly, hζ.powerBasis_gen K, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, (cyclotomic_coeff_zero K h1 : coeff (cyclotomic n K) 0 = 1), mul_one, hζ.powerBasis_dim K, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, natDegree_cyclotomic] exact (totient_even <| h1.lt_of_ne hn.symm).neg_one_pow /-- If `K` is linearly ordered, the norm of a primitive root is `1` if `n` is odd. -/ theorem norm_eq_one_of_linearly_ordered {K : Type*} [Field K] [LinearOrder K] [IsStrictOrderedRing K] [Algebra K L] (hodd : Odd (n : ℕ)) : norm K ζ = 1 := by have hz := congr_arg (norm K) ((IsPrimitiveRoot.iff_def _ n).1 hζ).1 rw [← (algebraMap K L).map_one, Algebra.norm_algebraMap, one_pow, map_pow, ← one_pow ↑n] at hz exact StrictMono.injective hodd.strictMono_pow hz theorem norm_of_cyclotomic_irreducible [IsDomain L] [IsCyclotomicExtension {n} K L] (hirr : Irreducible (cyclotomic n K)) : norm K ζ = ite (n = 2) (-1) 1 := by split_ifs with hn · subst hn rw [norm_eq_neg_one_pow (K := K) hζ, IsCyclotomicExtension.finrank _ hirr] norm_cast · exact hζ.norm_eq_one hn hirr end CommRing section Field variable [Field L] {ζ : L} variable {K} [Field K] [Algebra K L] section variable (hζ : IsPrimitiveRoot ζ n) include hζ /-- If `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `ζ - 1` is `eval 1 (cyclotomic n ℤ)`. -/ theorem sub_one_norm_eq_eval_cyclotomic [IsCyclotomicExtension {n} K L] (h : 2 < (n : ℕ)) (hirr : Irreducible (cyclotomic n K)) : norm K (ζ - 1) = ↑(eval 1 (cyclotomic n ℤ)) := by haveI := IsCyclotomicExtension.neZero' n K L let E := AlgebraicClosure L obtain ⟨z, hz⟩ := IsAlgClosed.exists_root _ (degree_cyclotomic_pos n E n.pos).ne.symm apply (algebraMap K E).injective letI := IsCyclotomicExtension.finiteDimensional {n} K L letI := IsCyclotomicExtension.isGalois n K L rw [norm_eq_prod_embeddings] conv_lhs => congr rfl ext rw [← neg_sub, map_neg, map_sub, map_one, neg_eq_neg_one_mul] rw [prod_mul_distrib, prod_const, Finset.card_univ, AlgHom.card, IsCyclotomicExtension.finrank L hirr, (totient_even h).neg_one_pow, one_mul] have Hprod : (Finset.univ.prod fun σ : L →ₐ[K] E => 1 - σ ζ) = eval 1 (cyclotomic' n E) := by rw [cyclotomic', eval_prod, ← @Finset.prod_attach E E, ← univ_eq_attach] refine Fintype.prod_equiv (hζ.embeddingsEquivPrimitiveRoots E hirr) _ _ fun σ => ?_ simp haveI : NeZero ((n : ℕ) : E) := NeZero.of_faithfulSMul K _ (n : ℕ) rw [Hprod, cyclotomic', ← cyclotomic_eq_prod_X_sub_primitiveRoots (isRoot_cyclotomic_iff.1 hz), ← map_cyclotomic_int, _root_.map_intCast, ← Int.cast_one, eval_intCast_map, eq_intCast, Int.cast_id] /-- If `IsPrimePow (n : ℕ)`, `n ≠ 2` and `Irreducible (cyclotomic n K)` (in particular for `K = ℚ`), then the norm of `ζ - 1` is `(n : ℕ).minFac`. -/ theorem sub_one_norm_isPrimePow (hn : IsPrimePow (n : ℕ)) [IsCyclotomicExtension {n} K L] (hirr : Irreducible (cyclotomic (n : ℕ) K)) (h : n ≠ 2) : norm K (ζ - 1) = (n : ℕ).minFac := by have := (coe_lt_coe 2 _).1 (lt_of_le_of_ne (succ_le_of_lt (IsPrimePow.one_lt hn)) (Function.Injective.ne PNat.coe_injective h).symm) letI hprime : Fact (n : ℕ).minFac.Prime := ⟨minFac_prime (IsPrimePow.ne_one hn)⟩ rw [sub_one_norm_eq_eval_cyclotomic hζ this hirr] nth_rw 1 [← IsPrimePow.minFac_pow_factorization_eq hn] obtain ⟨k, hk⟩ : ∃ k, (n : ℕ).factorization (n : ℕ).minFac = k + 1 := exists_eq_succ_of_ne_zero (((n : ℕ).factorization.mem_support_toFun (n : ℕ).minFac).1 <| mem_primeFactors_iff_mem_primeFactorsList.2 <| (mem_primeFactorsList (IsPrimePow.ne_zero hn)).2 ⟨hprime.out, minFac_dvd _⟩) simp [hk, sub_one_norm_eq_eval_cyclotomic hζ this hirr] end variable {A} theorem minpoly_sub_one_eq_cyclotomic_comp [Algebra K A] [IsDomain A] {ζ : A} [IsCyclotomicExtension {n} K A] (hζ : IsPrimitiveRoot ζ n) (h : Irreducible (Polynomial.cyclotomic n K)) : minpoly K (ζ - 1) = (cyclotomic n K).comp (X + 1) := by haveI := IsCyclotomicExtension.neZero' n K A rw [show ζ - 1 = ζ + algebraMap K A (-1) by simp [sub_eq_add_neg], minpoly.add_algebraMap ζ, hζ.minpoly_eq_cyclotomic_of_irreducible h] simp open scoped Cyclotomic /-- If `Irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` if `p ^ (k - s + 1) ≠ 2`. See the next lemmas for similar results. -/ theorem norm_pow_sub_one_of_prime_pow_ne_two {k s : ℕ} (hζ : IsPrimitiveRoot ζ ↑(p ^ (k + 1))) [hpri : Fact (p : ℕ).Prime] [IsCyclotomicExtension {p ^ (k + 1)} K L] (hirr : Irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (hs : s ≤ k) (htwo : p ^ (k - s + 1) ≠ 2) : norm K (ζ ^ (p : ℕ) ^ s - 1) = (p : K) ^ (p : ℕ) ^ s := by have hirr₁ : Irreducible (cyclotomic ((p : ℕ) ^ (k - s + 1)) K) := cyclotomic_irreducible_pow_of_irreducible_pow hpri.1 (by omega) hirr rw [← PNat.pow_coe] at hirr₁ set η := ζ ^ (p : ℕ) ^ s - 1 let η₁ : K⟮η⟯ := IntermediateField.AdjoinSimple.gen K η have hη : IsPrimitiveRoot (η + 1) ((p : ℕ) ^ (k + 1 - s)) := by rw [sub_add_cancel] refine IsPrimitiveRoot.pow (p ^ (k + 1)).pos hζ ?_ rw [PNat.pow_coe, ← pow_add, add_comm s, Nat.sub_add_cancel (le_trans hs (Nat.le_succ k))] have : IsCyclotomicExtension {p ^ (k - s + 1)} K K⟮η⟯ := by have HKη : K⟮η⟯ = K⟮η + 1⟯ := by refine le_antisymm ?_ ?_ all_goals rw [IntermediateField.adjoin_simple_le_iff] · nth_rw 2 [← add_sub_cancel_right η 1] exact sub_mem (IntermediateField.mem_adjoin_simple_self K (η + 1)) (one_mem _) · exact add_mem (IntermediateField.mem_adjoin_simple_self K η) (one_mem _) rw [HKη] have H := IntermediateField.adjoin_simple_toSubalgebra_of_integral ((integral {p ^ (k + 1)} K L).isIntegral (η + 1)) refine IsCyclotomicExtension.equiv _ _ _ (h := ?_) (.refl : K⟮η + 1⟯.toSubalgebra ≃ₐ[K] _) rw [H] have hη' : IsPrimitiveRoot (η + 1) ↑(p ^ (k + 1 - s)) := by simpa using hη convert hη'.adjoin_isCyclotomicExtension K using 1 rw [Nat.sub_add_comm hs] replace hη : IsPrimitiveRoot (η₁ + 1) ↑(p ^ (k - s + 1)) := by apply coe_submonoidClass_iff.1 convert hη using 1 rw [Nat.sub_add_comm hs, pow_coe] have := IsCyclotomicExtension.finiteDimensional {p ^ (k + 1)} K L have := IsCyclotomicExtension.isGalois (p ^ (k + 1)) K L rw [norm_eq_norm_adjoin K] have H := hη.sub_one_norm_isPrimePow ?_ hirr₁ htwo swap; · rw [PNat.pow_coe]; exact hpri.1.isPrimePow.pow (Nat.succ_ne_zero _) rw [add_sub_cancel_right] at H rw [H] congr · rw [PNat.pow_coe, Nat.pow_minFac, hpri.1.minFac_eq] exact Nat.succ_ne_zero _ have := Module.finrank_mul_finrank K K⟮η⟯ L rw [IsCyclotomicExtension.finrank L hirr, IsCyclotomicExtension.finrank K⟮η⟯ hirr₁, PNat.pow_coe, PNat.pow_coe, Nat.totient_prime_pow hpri.out (k - s).succ_pos, Nat.totient_prime_pow hpri.out k.succ_pos, mul_comm _ ((p : ℕ) - 1), mul_assoc, mul_comm ((p : ℕ) ^ (k.succ - 1))] at this replace this := mul_left_cancel₀ (tsub_pos_iff_lt.2 hpri.out.one_lt).ne' this have Hex : k.succ - 1 = (k - s).succ - 1 + s := by simp only [Nat.succ_sub_succ_eq_sub, tsub_zero] exact (Nat.sub_add_cancel hs).symm rw [Hex, pow_add] at this exact mul_left_cancel₀ (pow_ne_zero _ hpri.out.ne_zero) this /-- If `Irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is a prime, then the norm of `ζ ^ (p ^ s) - 1` is `p ^ (p ^ s)` if `p ≠ 2`. -/ theorem norm_pow_sub_one_of_prime_ne_two {k : ℕ} (hζ : IsPrimitiveRoot ζ ↑(p ^ (k + 1))) [hpri : Fact (p : ℕ).Prime] [IsCyclotomicExtension {p ^ (k + 1)} K L] (hirr : Irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) {s : ℕ} (hs : s ≤ k) (hodd : p ≠ 2) : norm K (ζ ^ (p : ℕ) ^ s - 1) = (p : K) ^ (p : ℕ) ^ s := by refine hζ.norm_pow_sub_one_of_prime_pow_ne_two hirr hs fun h => ?_ have coe_two : ((2 : ℕ+) : ℕ) = 2 := by norm_cast rw [← PNat.coe_inj, coe_two, PNat.pow_coe, ← pow_one 2] at h replace h := eq_of_prime_pow_eq (prime_iff.1 hpri.out) (prime_iff.1 Nat.prime_two) (k - s).succ_pos h exact hodd (PNat.coe_injective h) /-- If `Irreducible (cyclotomic (p ^ (k + 1)) K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `ζ - 1` is `p`. -/ theorem norm_sub_one_of_prime_ne_two {k : ℕ} (hζ : IsPrimitiveRoot ζ ↑(p ^ (k + 1))) [hpri : Fact (p : ℕ).Prime] [IsCyclotomicExtension {p ^ (k + 1)} K L] (hirr : Irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (h : p ≠ 2) : norm K (ζ - 1) = p := by simpa using hζ.norm_pow_sub_one_of_prime_ne_two hirr k.zero_le h /-- If `Irreducible (cyclotomic p K)` (in particular for `K = ℚ`) and `p` is an odd prime, then the norm of `ζ - 1` is `p`. -/ theorem norm_sub_one_of_prime_ne_two' [hpri : Fact (p : ℕ).Prime] [hcyc : IsCyclotomicExtension {p} K L] (hζ : IsPrimitiveRoot ζ p) (hirr : Irreducible (cyclotomic p K)) (h : p ≠ 2) : norm K (ζ - 1) = p := by replace hirr : Irreducible (cyclotomic (p ^ (0 + 1) : ℕ) K) := by simp [hirr] replace hζ : IsPrimitiveRoot ζ (p ^ (0 + 1) : ℕ) := by simp [hζ] haveI : IsCyclotomicExtension {p ^ (0 + 1)} K L := by simp [hcyc] simpa using norm_sub_one_of_prime_ne_two hζ hirr h /-- If `Irreducible (cyclotomic (2 ^ (k + 1)) K)` (in particular for `K = ℚ`), then the norm of `ζ ^ (2 ^ k) - 1` is `(-2) ^ (2 ^ k)`. -/ theorem norm_pow_sub_one_two {k : ℕ} (hζ : IsPrimitiveRoot ζ (2 ^ (k + 1))) [IsCyclotomicExtension {2 ^ (k + 1)} K L] (hirr : Irreducible (cyclotomic (2 ^ (k + 1)) K)) : norm K (ζ ^ 2 ^ k - 1) = (-2 : K) ^ 2 ^ k := by have := hζ.pow_of_dvd (fun h => two_ne_zero (pow_eq_zero h)) (pow_dvd_pow 2 (le_succ k)) rw [Nat.pow_div (le_succ k) zero_lt_two, Nat.succ_sub (le_refl k), Nat.sub_self, pow_one] at this have H : (-1 : L) - (1 : L) = algebraMap K L (-2) := by simp only [map_neg, map_ofNat] ring replace hirr : Irreducible (cyclotomic (2 ^ (k + 1) : ℕ+) K) := by simp [hirr] rw [this.eq_neg_one_of_two_right, H, Algebra.norm_algebraMap, IsCyclotomicExtension.finrank L hirr, pow_coe, show ((2 : ℕ+) : ℕ) = 2 from rfl, totient_prime_pow Nat.prime_two (zero_lt_succ k), succ_sub_succ_eq_sub, tsub_zero]
simp /-- If `Irreducible (cyclotomic (2 ^ k) K)` (in particular for `K = ℚ`) and `k` is at least `2`, then the norm of `ζ - 1` is `2`. -/ theorem norm_sub_one_two {k : ℕ} (hζ : IsPrimitiveRoot ζ (2 ^ k)) (hk : 2 ≤ k) [H : IsCyclotomicExtension {2 ^ k} K L] (hirr : Irreducible (cyclotomic (2 ^ k) K)) : norm K (ζ - 1) = 2 := by
Mathlib/NumberTheory/Cyclotomic/PrimitiveRoots.lean
492
498
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Malo Jaffré -/ import Mathlib.Analysis.Convex.Function import Mathlib.Tactic.AdaptationNote import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.Linarith /-! # Slopes of convex functions This file relates convexity/concavity of functions in a linearly ordered field and the monotonicity of their slopes. The main use is to show convexity/concavity from monotonicity of the derivative. -/ variable {𝕜 : Type*} [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] {s : Set 𝕜} {f : 𝕜 → 𝕜} /-- If `f : 𝕜 → 𝕜` is convex, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is less than the slope of the secant line of `f` on `[y, z]`. -/ theorem ConvexOn.slope_mono_adjacent (hf : ConvexOn 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := by have hxz := hxy.trans hyz rw [← sub_pos] at hxy hxz hyz suffices f y / (y - x) + f y / (z - y) ≤ f x / (y - x) + f z / (z - y) by ring_nf at this ⊢ linarith set a := (z - y) / (z - x) set b := (y - x) / (z - x) have hy : a • x + b • z = y := by field_simp [a, b]; ring have key := hf.2 hx hz (show 0 ≤ a by apply div_nonneg <;> linarith) (show 0 ≤ b by apply div_nonneg <;> linarith) (show a + b = 1 by field_simp [a, b]) rw [hy] at key replace key := mul_le_mul_of_nonneg_left key hxz.le field_simp [a, b, mul_comm (z - x) _] at key ⊢ rw [div_le_div_iff_of_pos_right] · linarith · positivity /-- If `f : 𝕜 → 𝕜` is concave, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is greater than the slope of the secant line of `f` on `[y, z]`. -/ theorem ConcaveOn.slope_anti_adjacent (hf : ConcaveOn 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := by have := neg_le_neg (ConvexOn.slope_mono_adjacent hf.neg hx hz hxy hyz) simp only [Pi.neg_apply, ← neg_div, neg_sub', neg_neg] at this exact this /-- If `f : 𝕜 → 𝕜` is strictly convex, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is strictly less than the slope of the secant line of `f` on `[y, z]`. -/ theorem StrictConvexOn.slope_strict_mono_adjacent (hf : StrictConvexOn 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) < (f z - f y) / (z - y) := by have hxz := hxy.trans hyz have hxz' := hxz.ne rw [← sub_pos] at hxy hxz hyz suffices f y / (y - x) + f y / (z - y) < f x / (y - x) + f z / (z - y) by ring_nf at this ⊢ linarith set a := (z - y) / (z - x) set b := (y - x) / (z - x) have hy : a • x + b • z = y := by field_simp [a, b]; ring have key := hf.2 hx hz hxz' (div_pos hyz hxz) (div_pos hxy hxz) (show a + b = 1 by field_simp [a, b]) rw [hy] at key replace key := mul_lt_mul_of_pos_left key hxz field_simp [mul_comm (z - x) _] at key ⊢ rw [div_lt_div_iff_of_pos_right] · linarith · positivity /-- If `f : 𝕜 → 𝕜` is strictly concave, then for any three points `x < y < z` the slope of the secant line of `f` on `[x, y]` is strictly greater than the slope of the secant line of `f` on `[y, z]`. -/ theorem StrictConcaveOn.slope_anti_adjacent (hf : StrictConcaveOn 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) < (f y - f x) / (y - x) := by have := neg_lt_neg (StrictConvexOn.slope_strict_mono_adjacent hf.neg hx hz hxy hyz) simp only [Pi.neg_apply, ← neg_div, neg_sub', neg_neg] at this exact this /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is less than the slope of the secant line of `f` on `[y, z]`, then `f` is convex. -/ theorem convexOn_of_slope_mono_adjacent (hs : Convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : ConvexOn 𝕜 s f := LinearOrder.convexOn_of_lt hs fun x hx z hz hxz a b ha hb hab => by let y := a * x + b * z have hxy : x < y := by rw [← one_mul x, ← hab, add_mul] exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ have hyz : y < z := by rw [← one_mul z, ← hab, add_mul] exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x) := (div_le_div_iff₀ (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz) have hxz : 0 < z - x := sub_pos.2 (hxy.trans hyz) have ha : (z - y) / (z - x) = a := by rw [eq_comm, ← sub_eq_iff_eq_add'] at hab dsimp [y] simp_rw [div_eq_iff hxz.ne', ← hab] ring have hb : (y - x) / (z - x) = b := by rw [eq_comm, ← sub_eq_iff_eq_add] at hab dsimp [y] simp_rw [div_eq_iff hxz.ne', ← hab] ring rwa [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, sub_add_sub_cancel, ← le_div_iff₀ hxz, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z), ha, hb] at this /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is greater than the slope of the secant line of `f` on `[y, z]`, then `f` is concave. -/ theorem concaveOn_of_slope_anti_adjacent (hs : Convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : ConcaveOn 𝕜 s f := by rw [← neg_convexOn_iff] refine convexOn_of_slope_mono_adjacent hs fun hx hz hxy hyz => ?_
rw [← neg_le_neg_iff] simp_rw [← neg_div, neg_sub, Pi.neg_apply, neg_sub_neg] exact hf hx hz hxy hyz /-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is strictly less than the slope of the secant line of `f` on `[y, z]`, then `f` is strictly convex. -/ theorem strictConvexOn_of_slope_strict_mono_adjacent (hs : Convex 𝕜 s) (hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) < (f z - f y) / (z - y)) :
Mathlib/Analysis/Convex/Slope.lean
128
137
/- 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 -/ import Mathlib.Algebra.Homology.ShortComplex.RightHomology /-! # Homology of short complexes In this file, we shall define the homology of short complexes `S`, i.e. diagrams `f : X₁ ⟶ X₂` and `g : X₂ ⟶ X₃` such that `f ≫ g = 0`. We shall say that `[S.HasHomology]` when there exists `h : S.HomologyData`. A homology data for `S` consists of compatible left/right homology data `left` and `right`. The left homology data `left` involves an object `left.H` that is a cokernel of the canonical map `S.X₁ ⟶ K` where `K` is a kernel of `g`. On the other hand, the dual notion `right.H` is a kernel of the canonical morphism `Q ⟶ S.X₃` when `Q` is a cokernel of `f`. The compatibility that is required involves an isomorphism `left.H ≅ right.H` which makes a certain pentagon commute. When such a homology data exists, `S.homology` shall be defined as `h.left.H` for a chosen `h : S.HomologyData`. This definition requires very little assumption on the category (only the existence of zero morphisms). We shall prove that in abelian categories, all short complexes have homology data. Note: This definition arose by the end of the Liquid Tensor Experiment which contained a structure `has_homology` which is quite similar to `S.HomologyData`. After the category `ShortComplex C` was introduced by J. Riou, A. Topaz suggested such a structure could be used as a basis for the *definition* of homology. -/ universe v u namespace CategoryTheory open Category Limits variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] (S : ShortComplex C) {S₁ S₂ S₃ S₄ : ShortComplex C} namespace ShortComplex /-- A homology data for a short complex consists of two compatible left and right homology data -/ structure HomologyData where /-- a left homology data -/ left : S.LeftHomologyData /-- a right homology data -/ right : S.RightHomologyData /-- the compatibility isomorphism relating the two dual notions of `LeftHomologyData` and `RightHomologyData` -/ iso : left.H ≅ right.H /-- the pentagon relation expressing the compatibility of the left and right homology data -/ comm : left.π ≫ iso.hom ≫ right.ι = left.i ≫ right.p := by aesop_cat attribute [reassoc (attr := simp)] HomologyData.comm variable (φ : S₁ ⟶ S₂) (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) /-- A homology map data for a morphism `φ : S₁ ⟶ S₂` where both `S₁` and `S₂` are equipped with homology data consists of left and right homology map data. -/ structure HomologyMapData where /-- a left homology map data -/ left : LeftHomologyMapData φ h₁.left h₂.left /-- a right homology map data -/ right : RightHomologyMapData φ h₁.right h₂.right namespace HomologyMapData variable {φ h₁ h₂} @[reassoc] lemma comm (h : HomologyMapData φ h₁ h₂) : h.left.φH ≫ h₂.iso.hom = h₁.iso.hom ≫ h.right.φH := by simp only [← cancel_epi h₁.left.π, ← cancel_mono h₂.right.ι, assoc, LeftHomologyMapData.commπ_assoc, HomologyData.comm, LeftHomologyMapData.commi_assoc, RightHomologyMapData.commι, HomologyData.comm_assoc, RightHomologyMapData.commp] instance : Subsingleton (HomologyMapData φ h₁ h₂) := ⟨by rintro ⟨left₁, right₁⟩ ⟨left₂, right₂⟩ simp only [mk.injEq, eq_iff_true_of_subsingleton, and_self]⟩ instance : Inhabited (HomologyMapData φ h₁ h₂) := ⟨⟨default, default⟩⟩ instance : Unique (HomologyMapData φ h₁ h₂) := Unique.mk' _ variable (φ h₁ h₂) /-- A choice of the (unique) homology map data associated with a morphism `φ : S₁ ⟶ S₂` where both short complexes `S₁` and `S₂` are equipped with homology data. -/ def homologyMapData : HomologyMapData φ h₁ h₂ := default variable {φ h₁ h₂} lemma congr_left_φH {γ₁ γ₂ : HomologyMapData φ h₁ h₂} (eq : γ₁ = γ₂) : γ₁.left.φH = γ₂.left.φH := by rw [eq] end HomologyMapData namespace HomologyData /-- When the first map `S.f` is zero, this is the homology data on `S` given by any limit kernel fork of `S.g` -/ @[simps] def ofIsLimitKernelFork (hf : S.f = 0) (c : KernelFork S.g) (hc : IsLimit c) : S.HomologyData where left := LeftHomologyData.ofIsLimitKernelFork S hf c hc right := RightHomologyData.ofIsLimitKernelFork S hf c hc iso := Iso.refl _ /-- When the first map `S.f` is zero, this is the homology data on `S` given by the chosen `kernel S.g` -/ @[simps] noncomputable def ofHasKernel (hf : S.f = 0) [HasKernel S.g] : S.HomologyData where left := LeftHomologyData.ofHasKernel S hf right := RightHomologyData.ofHasKernel S hf iso := Iso.refl _ /-- When the second map `S.g` is zero, this is the homology data on `S` given by any colimit cokernel cofork of `S.f` -/ @[simps] def ofIsColimitCokernelCofork (hg : S.g = 0) (c : CokernelCofork S.f) (hc : IsColimit c) : S.HomologyData where left := LeftHomologyData.ofIsColimitCokernelCofork S hg c hc right := RightHomologyData.ofIsColimitCokernelCofork S hg c hc iso := Iso.refl _ /-- When the second map `S.g` is zero, this is the homology data on `S` given by the chosen `cokernel S.f` -/ @[simps] noncomputable def ofHasCokernel (hg : S.g = 0) [HasCokernel S.f] : S.HomologyData where left := LeftHomologyData.ofHasCokernel S hg right := RightHomologyData.ofHasCokernel S hg iso := Iso.refl _ /-- When both `S.f` and `S.g` are zero, the middle object `S.X₂` gives a homology data on S -/ @[simps] noncomputable def ofZeros (hf : S.f = 0) (hg : S.g = 0) : S.HomologyData where left := LeftHomologyData.ofZeros S hf hg right := RightHomologyData.ofZeros S hf hg iso := Iso.refl _ /-- If `φ : S₁ ⟶ S₂` is a morphism of short complexes such that `φ.τ₁` is epi, `φ.τ₂` is an iso and `φ.τ₃` is mono, then a homology data for `S₁` induces a homology data for `S₂`. The inverse construction is `ofEpiOfIsIsoOfMono'`. -/ @[simps] noncomputable def ofEpiOfIsIsoOfMono (φ : S₁ ⟶ S₂) (h : HomologyData S₁) [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : HomologyData S₂ where left := LeftHomologyData.ofEpiOfIsIsoOfMono φ h.left right := RightHomologyData.ofEpiOfIsIsoOfMono φ h.right iso := h.iso /-- If `φ : S₁ ⟶ S₂` is a morphism of short complexes such that `φ.τ₁` is epi, `φ.τ₂` is an iso and `φ.τ₃` is mono, then a homology data for `S₂` induces a homology data for `S₁`. The inverse construction is `ofEpiOfIsIsoOfMono`. -/ @[simps] noncomputable def ofEpiOfIsIsoOfMono' (φ : S₁ ⟶ S₂) (h : HomologyData S₂) [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : HomologyData S₁ where left := LeftHomologyData.ofEpiOfIsIsoOfMono' φ h.left right := RightHomologyData.ofEpiOfIsIsoOfMono' φ h.right iso := h.iso /-- If `e : S₁ ≅ S₂` is an isomorphism of short complexes and `h₁ : HomologyData S₁`, this is the homology data for `S₂` deduced from the isomorphism. -/ @[simps!] noncomputable def ofIso (e : S₁ ≅ S₂) (h : HomologyData S₁) := h.ofEpiOfIsIsoOfMono e.hom variable {S} /-- A homology data for a short complex `S` induces a homology data for `S.op`. -/ @[simps] def op (h : S.HomologyData) : S.op.HomologyData where left := h.right.op right := h.left.op iso := h.iso.op comm := Quiver.Hom.unop_inj (by simp) /-- A homology data for a short complex `S` in the opposite category induces a homology data for `S.unop`. -/ @[simps] def unop {S : ShortComplex Cᵒᵖ} (h : S.HomologyData) : S.unop.HomologyData where left := h.right.unop right := h.left.unop iso := h.iso.unop comm := Quiver.Hom.op_inj (by simp) end HomologyData /-- A short complex `S` has homology when there exists a `S.HomologyData` -/ class HasHomology : Prop where /-- the condition that there exists a homology data -/ condition : Nonempty S.HomologyData /-- A chosen `S.HomologyData` for a short complex `S` that has homology -/ noncomputable def homologyData [HasHomology S] : S.HomologyData := HasHomology.condition.some variable {S} lemma HasHomology.mk' (h : S.HomologyData) : HasHomology S := ⟨Nonempty.intro h⟩ instance [HasHomology S] : HasHomology S.op := HasHomology.mk' S.homologyData.op instance (S : ShortComplex Cᵒᵖ) [HasHomology S] : HasHomology S.unop := HasHomology.mk' S.homologyData.unop instance hasLeftHomology_of_hasHomology [S.HasHomology] : S.HasLeftHomology := HasLeftHomology.mk' S.homologyData.left instance hasRightHomology_of_hasHomology [S.HasHomology] : S.HasRightHomology := HasRightHomology.mk' S.homologyData.right instance hasHomology_of_hasCokernel {X Y : C} (f : X ⟶ Y) (Z : C) [HasCokernel f] : (ShortComplex.mk f (0 : Y ⟶ Z) comp_zero).HasHomology := HasHomology.mk' (HomologyData.ofHasCokernel _ rfl) instance hasHomology_of_hasKernel {Y Z : C} (g : Y ⟶ Z) (X : C) [HasKernel g] : (ShortComplex.mk (0 : X ⟶ Y) g zero_comp).HasHomology := HasHomology.mk' (HomologyData.ofHasKernel _ rfl) instance hasHomology_of_zeros (X Y Z : C) : (ShortComplex.mk (0 : X ⟶ Y) (0 : Y ⟶ Z) zero_comp).HasHomology := HasHomology.mk' (HomologyData.ofZeros _ rfl rfl) lemma hasHomology_of_epi_of_isIso_of_mono (φ : S₁ ⟶ S₂) [HasHomology S₁] [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : HasHomology S₂ := HasHomology.mk' (HomologyData.ofEpiOfIsIsoOfMono φ S₁.homologyData) lemma hasHomology_of_epi_of_isIso_of_mono' (φ : S₁ ⟶ S₂) [HasHomology S₂] [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : HasHomology S₁ := HasHomology.mk' (HomologyData.ofEpiOfIsIsoOfMono' φ S₂.homologyData) lemma hasHomology_of_iso (e : S₁ ≅ S₂) [HasHomology S₁] : HasHomology S₂ := HasHomology.mk' (HomologyData.ofIso e S₁.homologyData) namespace HomologyMapData /-- The homology map data associated to the identity morphism of a short complex. -/ @[simps] def id (h : S.HomologyData) : HomologyMapData (𝟙 S) h h where left := LeftHomologyMapData.id h.left right := RightHomologyMapData.id h.right /-- The homology map data associated to the zero morphism between two short complexes. -/ @[simps] def zero (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) : HomologyMapData 0 h₁ h₂ where left := LeftHomologyMapData.zero h₁.left h₂.left right := RightHomologyMapData.zero h₁.right h₂.right /-- The composition of homology map data. -/ @[simps] def comp {φ : S₁ ⟶ S₂} {φ' : S₂ ⟶ S₃} {h₁ : S₁.HomologyData} {h₂ : S₂.HomologyData} {h₃ : S₃.HomologyData} (ψ : HomologyMapData φ h₁ h₂) (ψ' : HomologyMapData φ' h₂ h₃) : HomologyMapData (φ ≫ φ') h₁ h₃ where left := ψ.left.comp ψ'.left right := ψ.right.comp ψ'.right /-- A homology map data for a morphism of short complexes induces a homology map data in the opposite category. -/ @[simps] def op {φ : S₁ ⟶ S₂} {h₁ : S₁.HomologyData} {h₂ : S₂.HomologyData} (ψ : HomologyMapData φ h₁ h₂) : HomologyMapData (opMap φ) h₂.op h₁.op where left := ψ.right.op right := ψ.left.op /-- A homology map data for a morphism of short complexes in the opposite category induces a homology map data in the original category. -/ @[simps] def unop {S₁ S₂ : ShortComplex Cᵒᵖ} {φ : S₁ ⟶ S₂} {h₁ : S₁.HomologyData} {h₂ : S₂.HomologyData} (ψ : HomologyMapData φ h₁ h₂) : HomologyMapData (unopMap φ) h₂.unop h₁.unop where left := ψ.right.unop right := ψ.left.unop /-- When `S₁.f`, `S₁.g`, `S₂.f` and `S₂.g` are all zero, the action on homology of a morphism `φ : S₁ ⟶ S₂` is given by the action `φ.τ₂` on the middle objects. -/ @[simps] def ofZeros (φ : S₁ ⟶ S₂) (hf₁ : S₁.f = 0) (hg₁ : S₁.g = 0) (hf₂ : S₂.f = 0) (hg₂ : S₂.g = 0) : HomologyMapData φ (HomologyData.ofZeros S₁ hf₁ hg₁) (HomologyData.ofZeros S₂ hf₂ hg₂) where left := LeftHomologyMapData.ofZeros φ hf₁ hg₁ hf₂ hg₂ right := RightHomologyMapData.ofZeros φ hf₁ hg₁ hf₂ hg₂ /-- When `S₁.g` and `S₂.g` are zero and we have chosen colimit cokernel coforks `c₁` and `c₂` for `S₁.f` and `S₂.f` respectively, the action on homology of a morphism `φ : S₁ ⟶ S₂` of short complexes is given by the unique morphism `f : c₁.pt ⟶ c₂.pt` such that `φ.τ₂ ≫ c₂.π = c₁.π ≫ f`. -/ @[simps] def ofIsColimitCokernelCofork (φ : S₁ ⟶ S₂) (hg₁ : S₁.g = 0) (c₁ : CokernelCofork S₁.f) (hc₁ : IsColimit c₁) (hg₂ : S₂.g = 0) (c₂ : CokernelCofork S₂.f) (hc₂ : IsColimit c₂) (f : c₁.pt ⟶ c₂.pt) (comm : φ.τ₂ ≫ c₂.π = c₁.π ≫ f) : HomologyMapData φ (HomologyData.ofIsColimitCokernelCofork S₁ hg₁ c₁ hc₁) (HomologyData.ofIsColimitCokernelCofork S₂ hg₂ c₂ hc₂) where left := LeftHomologyMapData.ofIsColimitCokernelCofork φ hg₁ c₁ hc₁ hg₂ c₂ hc₂ f comm right := RightHomologyMapData.ofIsColimitCokernelCofork φ hg₁ c₁ hc₁ hg₂ c₂ hc₂ f comm /-- When `S₁.f` and `S₂.f` are zero and we have chosen limit kernel forks `c₁` and `c₂` for `S₁.g` and `S₂.g` respectively, the action on homology of a morphism `φ : S₁ ⟶ S₂` of short complexes is given by the unique morphism `f : c₁.pt ⟶ c₂.pt` such that `c₁.ι ≫ φ.τ₂ = f ≫ c₂.ι`. -/ @[simps] def ofIsLimitKernelFork (φ : S₁ ⟶ S₂) (hf₁ : S₁.f = 0) (c₁ : KernelFork S₁.g) (hc₁ : IsLimit c₁) (hf₂ : S₂.f = 0) (c₂ : KernelFork S₂.g) (hc₂ : IsLimit c₂) (f : c₁.pt ⟶ c₂.pt) (comm : c₁.ι ≫ φ.τ₂ = f ≫ c₂.ι) : HomologyMapData φ (HomologyData.ofIsLimitKernelFork S₁ hf₁ c₁ hc₁) (HomologyData.ofIsLimitKernelFork S₂ hf₂ c₂ hc₂) where left := LeftHomologyMapData.ofIsLimitKernelFork φ hf₁ c₁ hc₁ hf₂ c₂ hc₂ f comm right := RightHomologyMapData.ofIsLimitKernelFork φ hf₁ c₁ hc₁ hf₂ c₂ hc₂ f comm /-- When both maps `S.f` and `S.g` of a short complex `S` are zero, this is the homology map data (for the identity of `S`) which relates the homology data `ofZeros` and `ofIsColimitCokernelCofork`. -/ def compatibilityOfZerosOfIsColimitCokernelCofork (hf : S.f = 0) (hg : S.g = 0) (c : CokernelCofork S.f) (hc : IsColimit c) : HomologyMapData (𝟙 S) (HomologyData.ofZeros S hf hg) (HomologyData.ofIsColimitCokernelCofork S hg c hc) where left := LeftHomologyMapData.compatibilityOfZerosOfIsColimitCokernelCofork S hf hg c hc right := RightHomologyMapData.compatibilityOfZerosOfIsColimitCokernelCofork S hf hg c hc /-- When both maps `S.f` and `S.g` of a short complex `S` are zero, this is the homology map data (for the identity of `S`) which relates the homology data `HomologyData.ofIsLimitKernelFork` and `ofZeros` . -/ @[simps] def compatibilityOfZerosOfIsLimitKernelFork (hf : S.f = 0) (hg : S.g = 0) (c : KernelFork S.g) (hc : IsLimit c) : HomologyMapData (𝟙 S) (HomologyData.ofIsLimitKernelFork S hf c hc) (HomologyData.ofZeros S hf hg) where left := LeftHomologyMapData.compatibilityOfZerosOfIsLimitKernelFork S hf hg c hc right := RightHomologyMapData.compatibilityOfZerosOfIsLimitKernelFork S hf hg c hc /-- This homology map data expresses compatibilities of the homology data constructed by `HomologyData.ofEpiOfIsIsoOfMono` -/ noncomputable def ofEpiOfIsIsoOfMono (φ : S₁ ⟶ S₂) (h : HomologyData S₁) [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : HomologyMapData φ h (HomologyData.ofEpiOfIsIsoOfMono φ h) where left := LeftHomologyMapData.ofEpiOfIsIsoOfMono φ h.left right := RightHomologyMapData.ofEpiOfIsIsoOfMono φ h.right /-- This homology map data expresses compatibilities of the homology data constructed by `HomologyData.ofEpiOfIsIsoOfMono'` -/ noncomputable def ofEpiOfIsIsoOfMono' (φ : S₁ ⟶ S₂) (h : HomologyData S₂) [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : HomologyMapData φ (HomologyData.ofEpiOfIsIsoOfMono' φ h) h where left := LeftHomologyMapData.ofEpiOfIsIsoOfMono' φ h.left right := RightHomologyMapData.ofEpiOfIsIsoOfMono' φ h.right end HomologyMapData variable (S) /-- The homology of a short complex is the `left.H` field of a chosen homology data. -/ noncomputable def homology [HasHomology S] : C := S.homologyData.left.H /-- When a short complex has homology, this is the canonical isomorphism `S.leftHomology ≅ S.homology`. -/ noncomputable def leftHomologyIso [S.HasHomology] : S.leftHomology ≅ S.homology := leftHomologyMapIso' (Iso.refl _) _ _ /-- When a short complex has homology, this is the canonical isomorphism `S.rightHomology ≅ S.homology`. -/ noncomputable def rightHomologyIso [S.HasHomology] : S.rightHomology ≅ S.homology := rightHomologyMapIso' (Iso.refl _) _ _ ≪≫ S.homologyData.iso.symm variable {S} /-- When a short complex has homology, its homology can be computed using any left homology data. -/ noncomputable def LeftHomologyData.homologyIso (h : S.LeftHomologyData) [S.HasHomology] : S.homology ≅ h.H := S.leftHomologyIso.symm ≪≫ h.leftHomologyIso /-- When a short complex has homology, its homology can be computed using any right homology data. -/ noncomputable def RightHomologyData.homologyIso (h : S.RightHomologyData) [S.HasHomology] : S.homology ≅ h.H := S.rightHomologyIso.symm ≪≫ h.rightHomologyIso variable (S) @[simp] lemma LeftHomologyData.homologyIso_leftHomologyData [S.HasHomology] : S.leftHomologyData.homologyIso = S.leftHomologyIso.symm := by ext dsimp [homologyIso, leftHomologyIso, ShortComplex.leftHomologyIso] rw [← leftHomologyMap'_comp, comp_id] @[simp] lemma RightHomologyData.homologyIso_rightHomologyData [S.HasHomology] : S.rightHomologyData.homologyIso = S.rightHomologyIso.symm := by ext simp [homologyIso, rightHomologyIso] variable {S} /-- Given a morphism `φ : S₁ ⟶ S₂` of short complexes and homology data `h₁` and `h₂` for `S₁` and `S₂` respectively, this is the induced homology map `h₁.left.H ⟶ h₁.left.H`. -/ def homologyMap' (φ : S₁ ⟶ S₂) (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) : h₁.left.H ⟶ h₂.left.H := leftHomologyMap' φ _ _ /-- The homology map `S₁.homology ⟶ S₂.homology` induced by a morphism `S₁ ⟶ S₂` of short complexes. -/ noncomputable def homologyMap (φ : S₁ ⟶ S₂) [HasHomology S₁] [HasHomology S₂] : S₁.homology ⟶ S₂.homology := homologyMap' φ _ _ namespace HomologyMapData variable {φ : S₁ ⟶ S₂} {h₁ : S₁.HomologyData} {h₂ : S₂.HomologyData} (γ : HomologyMapData φ h₁ h₂) lemma homologyMap'_eq : homologyMap' φ h₁ h₂ = γ.left.φH := LeftHomologyMapData.congr_φH (Subsingleton.elim _ _) lemma cyclesMap'_eq : cyclesMap' φ h₁.left h₂.left = γ.left.φK := LeftHomologyMapData.congr_φK (Subsingleton.elim _ _) lemma opcyclesMap'_eq : opcyclesMap' φ h₁.right h₂.right = γ.right.φQ := RightHomologyMapData.congr_φQ (Subsingleton.elim _ _) end HomologyMapData namespace LeftHomologyMapData variable {h₁ : S₁.LeftHomologyData} {h₂ : S₂.LeftHomologyData} (γ : LeftHomologyMapData φ h₁ h₂) [S₁.HasHomology] [S₂.HasHomology] lemma homologyMap_eq : homologyMap φ = h₁.homologyIso.hom ≫ γ.φH ≫ h₂.homologyIso.inv := by dsimp [homologyMap, LeftHomologyData.homologyIso, leftHomologyIso, LeftHomologyData.leftHomologyIso, homologyMap'] simp only [← γ.leftHomologyMap'_eq, ← leftHomologyMap'_comp, id_comp, comp_id] lemma homologyMap_comm : homologyMap φ ≫ h₂.homologyIso.hom = h₁.homologyIso.hom ≫ γ.φH := by simp only [γ.homologyMap_eq, assoc, Iso.inv_hom_id, comp_id] end LeftHomologyMapData namespace RightHomologyMapData variable {h₁ : S₁.RightHomologyData} {h₂ : S₂.RightHomologyData} (γ : RightHomologyMapData φ h₁ h₂) [S₁.HasHomology] [S₂.HasHomology] lemma homologyMap_eq : homologyMap φ = h₁.homologyIso.hom ≫ γ.φH ≫ h₂.homologyIso.inv := by dsimp [homologyMap, homologyMap', RightHomologyData.homologyIso, rightHomologyIso, RightHomologyData.rightHomologyIso] have γ' : HomologyMapData φ S₁.homologyData S₂.homologyData := default simp only [← γ.rightHomologyMap'_eq, assoc, ← rightHomologyMap'_comp_assoc, id_comp, comp_id, γ'.left.leftHomologyMap'_eq, γ'.right.rightHomologyMap'_eq, ← γ'.comm_assoc, Iso.hom_inv_id] lemma homologyMap_comm : homologyMap φ ≫ h₂.homologyIso.hom = h₁.homologyIso.hom ≫ γ.φH := by simp only [γ.homologyMap_eq, assoc, Iso.inv_hom_id, comp_id] end RightHomologyMapData @[simp] lemma homologyMap'_id (h : S.HomologyData) : homologyMap' (𝟙 S) h h = 𝟙 _ := (HomologyMapData.id h).homologyMap'_eq variable (S) @[simp] lemma homologyMap_id [HasHomology S] : homologyMap (𝟙 S) = 𝟙 _ := homologyMap'_id _ @[simp] lemma homologyMap'_zero (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) : homologyMap' 0 h₁ h₂ = 0 := (HomologyMapData.zero h₁ h₂).homologyMap'_eq variable (S₁ S₂) @[simp] lemma homologyMap_zero [S₁.HasHomology] [S₂.HasHomology] : homologyMap (0 : S₁ ⟶ S₂) = 0 := homologyMap'_zero _ _ variable {S₁ S₂} lemma homologyMap'_comp (φ₁ : S₁ ⟶ S₂) (φ₂ : S₂ ⟶ S₃) (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) (h₃ : S₃.HomologyData) : homologyMap' (φ₁ ≫ φ₂) h₁ h₃ = homologyMap' φ₁ h₁ h₂ ≫ homologyMap' φ₂ h₂ h₃ := leftHomologyMap'_comp _ _ _ _ _ @[simp] lemma homologyMap_comp [HasHomology S₁] [HasHomology S₂] [HasHomology S₃] (φ₁ : S₁ ⟶ S₂) (φ₂ : S₂ ⟶ S₃) : homologyMap (φ₁ ≫ φ₂) = homologyMap φ₁ ≫ homologyMap φ₂ := homologyMap'_comp _ _ _ _ _ /-- Given an isomorphism `S₁ ≅ S₂` of short complexes and homology data `h₁` and `h₂` for `S₁` and `S₂` respectively, this is the induced homology isomorphism `h₁.left.H ≅ h₁.left.H`. -/ @[simps] def homologyMapIso' (e : S₁ ≅ S₂) (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) : h₁.left.H ≅ h₂.left.H where hom := homologyMap' e.hom h₁ h₂ inv := homologyMap' e.inv h₂ h₁ hom_inv_id := by rw [← homologyMap'_comp, e.hom_inv_id, homologyMap'_id] inv_hom_id := by rw [← homologyMap'_comp, e.inv_hom_id, homologyMap'_id] instance isIso_homologyMap'_of_isIso (φ : S₁ ⟶ S₂) [IsIso φ] (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) : IsIso (homologyMap' φ h₁ h₂) := (inferInstance : IsIso (homologyMapIso' (asIso φ) h₁ h₂).hom) /-- The homology isomorphism `S₁.homology ⟶ S₂.homology` induced by an isomorphism `S₁ ≅ S₂` of short complexes. -/ @[simps] noncomputable def homologyMapIso (e : S₁ ≅ S₂) [S₁.HasHomology] [S₂.HasHomology] : S₁.homology ≅ S₂.homology where hom := homologyMap e.hom inv := homologyMap e.inv hom_inv_id := by rw [← homologyMap_comp, e.hom_inv_id, homologyMap_id] inv_hom_id := by rw [← homologyMap_comp, e.inv_hom_id, homologyMap_id] instance isIso_homologyMap_of_iso (φ : S₁ ⟶ S₂) [IsIso φ] [S₁.HasHomology] [S₂.HasHomology] : IsIso (homologyMap φ) := (inferInstance : IsIso (homologyMapIso (asIso φ)).hom) variable {S} section variable (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) /-- If a short complex `S` has both a left homology data `h₁` and a right homology data `h₂`, this is the canonical morphism `h₁.H ⟶ h₂.H`. -/ def leftRightHomologyComparison' : h₁.H ⟶ h₂.H := h₂.liftH (h₁.descH (h₁.i ≫ h₂.p) (by simp)) (by rw [← cancel_epi h₁.π, LeftHomologyData.π_descH_assoc, assoc, RightHomologyData.p_g', LeftHomologyData.wi, comp_zero]) lemma leftRightHomologyComparison'_eq_liftH : leftRightHomologyComparison' h₁ h₂ = h₂.liftH (h₁.descH (h₁.i ≫ h₂.p) (by simp)) (by rw [← cancel_epi h₁.π, LeftHomologyData.π_descH_assoc, assoc, RightHomologyData.p_g', LeftHomologyData.wi, comp_zero]) := rfl @[reassoc (attr := simp)] lemma π_leftRightHomologyComparison'_ι : h₁.π ≫ leftRightHomologyComparison' h₁ h₂ ≫ h₂.ι = h₁.i ≫ h₂.p := by simp only [leftRightHomologyComparison'_eq_liftH, RightHomologyData.liftH_ι, LeftHomologyData.π_descH] lemma leftRightHomologyComparison'_eq_descH : leftRightHomologyComparison' h₁ h₂ = h₁.descH (h₂.liftH (h₁.i ≫ h₂.p) (by simp)) (by rw [← cancel_mono h₂.ι, assoc, RightHomologyData.liftH_ι, LeftHomologyData.f'_i_assoc, RightHomologyData.wp, zero_comp]) := by simp only [← cancel_mono h₂.ι, ← cancel_epi h₁.π, π_leftRightHomologyComparison'_ι, LeftHomologyData.π_descH_assoc, RightHomologyData.liftH_ι] end variable (S) /-- If a short complex `S` has both a left and right homology, this is the canonical morphism `S.leftHomology ⟶ S.rightHomology`. -/ noncomputable def leftRightHomologyComparison [S.HasLeftHomology] [S.HasRightHomology] : S.leftHomology ⟶ S.rightHomology := leftRightHomologyComparison' _ _ @[reassoc (attr := simp)] lemma π_leftRightHomologyComparison_ι [S.HasLeftHomology] [S.HasRightHomology] : S.leftHomologyπ ≫ S.leftRightHomologyComparison ≫ S.rightHomologyι = S.iCycles ≫ S.pOpcycles := π_leftRightHomologyComparison'_ι _ _ @[reassoc] lemma leftRightHomologyComparison'_naturality (φ : S₁ ⟶ S₂) (h₁ : S₁.LeftHomologyData) (h₂ : S₁.RightHomologyData) (h₁' : S₂.LeftHomologyData) (h₂' : S₂.RightHomologyData) : leftHomologyMap' φ h₁ h₁' ≫ leftRightHomologyComparison' h₁' h₂' = leftRightHomologyComparison' h₁ h₂ ≫ rightHomologyMap' φ h₂ h₂' := by simp only [← cancel_epi h₁.π, ← cancel_mono h₂'.ι, assoc, leftHomologyπ_naturality'_assoc, rightHomologyι_naturality', π_leftRightHomologyComparison'_ι, π_leftRightHomologyComparison'_ι_assoc, cyclesMap'_i_assoc, p_opcyclesMap'] variable {S} lemma leftRightHomologyComparison'_compatibility (h₁ h₁' : S.LeftHomologyData) (h₂ h₂' : S.RightHomologyData) : leftRightHomologyComparison' h₁ h₂ = leftHomologyMap' (𝟙 S) h₁ h₁' ≫ leftRightHomologyComparison' h₁' h₂' ≫ rightHomologyMap' (𝟙 S) _ _ := by rw [leftRightHomologyComparison'_naturality_assoc (𝟙 S) h₁ h₂ h₁' h₂', ← rightHomologyMap'_comp, comp_id, rightHomologyMap'_id, comp_id] lemma leftRightHomologyComparison_eq [S.HasLeftHomology] [S.HasRightHomology] (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) : S.leftRightHomologyComparison = h₁.leftHomologyIso.hom ≫ leftRightHomologyComparison' h₁ h₂ ≫ h₂.rightHomologyIso.inv := leftRightHomologyComparison'_compatibility _ _ _ _ @[simp] lemma HomologyData.leftRightHomologyComparison'_eq (h : S.HomologyData) : leftRightHomologyComparison' h.left h.right = h.iso.hom := by simp only [← cancel_epi h.left.π, ← cancel_mono h.right.ι, assoc, π_leftRightHomologyComparison'_ι, comm] instance isIso_leftRightHomologyComparison'_of_homologyData (h : S.HomologyData) : IsIso (leftRightHomologyComparison' h.left h.right) := by rw [h.leftRightHomologyComparison'_eq] infer_instance instance isIso_leftRightHomologyComparison' [S.HasHomology] (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) : IsIso (leftRightHomologyComparison' h₁ h₂) := by rw [leftRightHomologyComparison'_compatibility h₁ S.homologyData.left h₂ S.homologyData.right] infer_instance instance isIso_leftRightHomologyComparison [S.HasHomology] : IsIso S.leftRightHomologyComparison := by dsimp only [leftRightHomologyComparison] infer_instance namespace HomologyData /-- This is the homology data for a short complex `S` that is obtained from a left homology data `h₁` and a right homology data `h₂` when the comparison morphism `leftRightHomologyComparison' h₁ h₂ : h₁.H ⟶ h₂.H` is an isomorphism. -/ @[simps] noncomputable def ofIsIsoLeftRightHomologyComparison' (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) [IsIso (leftRightHomologyComparison' h₁ h₂)] : S.HomologyData where left := h₁ right := h₂ iso := asIso (leftRightHomologyComparison' h₁ h₂) end HomologyData lemma leftRightHomologyComparison'_eq_leftHomologpMap'_comp_iso_hom_comp_rightHomologyMap' (h : S.HomologyData) (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) : leftRightHomologyComparison' h₁ h₂ = leftHomologyMap' (𝟙 S) h₁ h.left ≫ h.iso.hom ≫ rightHomologyMap' (𝟙 S) h.right h₂ := by simpa only [h.leftRightHomologyComparison'_eq] using leftRightHomologyComparison'_compatibility h₁ h.left h₂ h.right @[reassoc] lemma leftRightHomologyComparison'_fac (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) [S.HasHomology] : leftRightHomologyComparison' h₁ h₂ = h₁.homologyIso.inv ≫ h₂.homologyIso.hom := by rw [leftRightHomologyComparison'_eq_leftHomologpMap'_comp_iso_hom_comp_rightHomologyMap' S.homologyData h₁ h₂] dsimp only [LeftHomologyData.homologyIso, LeftHomologyData.leftHomologyIso, Iso.symm, Iso.trans, Iso.refl, leftHomologyMapIso', leftHomologyIso, RightHomologyData.homologyIso, RightHomologyData.rightHomologyIso, rightHomologyMapIso', rightHomologyIso] simp only [assoc, ← leftHomologyMap'_comp_assoc, id_comp, ← rightHomologyMap'_comp] variable (S) @[reassoc] lemma leftRightHomologyComparison_fac [S.HasHomology] : S.leftRightHomologyComparison = S.leftHomologyIso.hom ≫ S.rightHomologyIso.inv := by simpa only [LeftHomologyData.homologyIso_leftHomologyData, Iso.symm_inv, RightHomologyData.homologyIso_rightHomologyData, Iso.symm_hom] using leftRightHomologyComparison'_fac S.leftHomologyData S.rightHomologyData variable {S} lemma HomologyData.right_homologyIso_eq_left_homologyIso_trans_iso (h : S.HomologyData) [S.HasHomology] : h.right.homologyIso = h.left.homologyIso ≪≫ h.iso := by suffices h.iso = h.left.homologyIso.symm ≪≫ h.right.homologyIso by rw [this, Iso.self_symm_id_assoc] ext dsimp rw [← leftRightHomologyComparison'_fac, leftRightHomologyComparison'_eq] lemma hasHomology_of_isIso_leftRightHomologyComparison' (h₁ : S.LeftHomologyData) (h₂ : S.RightHomologyData) [IsIso (leftRightHomologyComparison' h₁ h₂)] : S.HasHomology := HasHomology.mk' (HomologyData.ofIsIsoLeftRightHomologyComparison' h₁ h₂) lemma hasHomology_of_isIsoLeftRightHomologyComparison [S.HasLeftHomology] [S.HasRightHomology] [h : IsIso S.leftRightHomologyComparison] : S.HasHomology := by haveI : IsIso (leftRightHomologyComparison' S.leftHomologyData S.rightHomologyData) := h exact hasHomology_of_isIso_leftRightHomologyComparison' S.leftHomologyData S.rightHomologyData section variable [S₁.HasHomology] [S₂.HasHomology] (φ : S₁ ⟶ S₂) @[reassoc] lemma LeftHomologyData.leftHomologyIso_hom_naturality (h₁ : S₁.LeftHomologyData) (h₂ : S₂.LeftHomologyData) : h₁.homologyIso.hom ≫ leftHomologyMap' φ h₁ h₂ = homologyMap φ ≫ h₂.homologyIso.hom := by dsimp [homologyIso, ShortComplex.leftHomologyIso, homologyMap, homologyMap', leftHomologyIso] simp only [← leftHomologyMap'_comp, id_comp, comp_id] @[reassoc] lemma LeftHomologyData.leftHomologyIso_inv_naturality (h₁ : S₁.LeftHomologyData) (h₂ : S₂.LeftHomologyData) : h₁.homologyIso.inv ≫ homologyMap φ = leftHomologyMap' φ h₁ h₂ ≫ h₂.homologyIso.inv := by dsimp [homologyIso, ShortComplex.leftHomologyIso, homologyMap, homologyMap', leftHomologyIso] simp only [← leftHomologyMap'_comp, id_comp, comp_id] @[reassoc] lemma leftHomologyIso_hom_naturality : S₁.leftHomologyIso.hom ≫ homologyMap φ = leftHomologyMap φ ≫ S₂.leftHomologyIso.hom := by simpa only [LeftHomologyData.homologyIso_leftHomologyData, Iso.symm_inv] using LeftHomologyData.leftHomologyIso_inv_naturality φ S₁.leftHomologyData S₂.leftHomologyData @[reassoc] lemma leftHomologyIso_inv_naturality : S₁.leftHomologyIso.inv ≫ leftHomologyMap φ = homologyMap φ ≫ S₂.leftHomologyIso.inv := by simpa only [LeftHomologyData.homologyIso_leftHomologyData, Iso.symm_inv] using LeftHomologyData.leftHomologyIso_hom_naturality φ S₁.leftHomologyData S₂.leftHomologyData @[reassoc] lemma RightHomologyData.rightHomologyIso_hom_naturality (h₁ : S₁.RightHomologyData) (h₂ : S₂.RightHomologyData) : h₁.homologyIso.hom ≫ rightHomologyMap' φ h₁ h₂ = homologyMap φ ≫ h₂.homologyIso.hom := by rw [← cancel_epi h₁.homologyIso.inv, Iso.inv_hom_id_assoc, ← cancel_epi (leftRightHomologyComparison' S₁.leftHomologyData h₁), ← leftRightHomologyComparison'_naturality φ S₁.leftHomologyData h₁ S₂.leftHomologyData h₂, ← cancel_epi (S₁.leftHomologyData.homologyIso.hom), LeftHomologyData.leftHomologyIso_hom_naturality_assoc, leftRightHomologyComparison'_fac, leftRightHomologyComparison'_fac, assoc, Iso.hom_inv_id_assoc, Iso.hom_inv_id_assoc, Iso.hom_inv_id_assoc] @[reassoc] lemma RightHomologyData.rightHomologyIso_inv_naturality (h₁ : S₁.RightHomologyData) (h₂ : S₂.RightHomologyData) : h₁.homologyIso.inv ≫ homologyMap φ = rightHomologyMap' φ h₁ h₂ ≫ h₂.homologyIso.inv := by simp only [← cancel_mono h₂.homologyIso.hom, assoc, Iso.inv_hom_id_assoc, comp_id, ← RightHomologyData.rightHomologyIso_hom_naturality φ h₁ h₂, Iso.inv_hom_id] @[reassoc] lemma rightHomologyIso_hom_naturality : S₁.rightHomologyIso.hom ≫ homologyMap φ = rightHomologyMap φ ≫ S₂.rightHomologyIso.hom := by simpa only [RightHomologyData.homologyIso_rightHomologyData, Iso.symm_inv] using RightHomologyData.rightHomologyIso_inv_naturality φ S₁.rightHomologyData S₂.rightHomologyData @[reassoc] lemma rightHomologyIso_inv_naturality : S₁.rightHomologyIso.inv ≫ rightHomologyMap φ = homologyMap φ ≫ S₂.rightHomologyIso.inv := by simpa only [RightHomologyData.homologyIso_rightHomologyData, Iso.symm_inv] using RightHomologyData.rightHomologyIso_hom_naturality φ S₁.rightHomologyData S₂.rightHomologyData end variable (C) /-- We shall say that a category `C` is a category with homology when all short complexes have homology. -/ class _root_.CategoryTheory.CategoryWithHomology : Prop where hasHomology : ∀ (S : ShortComplex C), S.HasHomology attribute [instance] CategoryWithHomology.hasHomology instance [CategoryWithHomology C] : CategoryWithHomology Cᵒᵖ := ⟨fun S => HasHomology.mk' S.unop.homologyData.op⟩ /-- The homology functor `ShortComplex C ⥤ C` for a category `C` with homology. -/ @[simps] noncomputable def homologyFunctor [CategoryWithHomology C] : ShortComplex C ⥤ C where obj S := S.homology map f := homologyMap f variable {C} instance isIso_homologyMap'_of_epi_of_isIso_of_mono (φ : S₁ ⟶ S₂) (h₁ : S₁.HomologyData) (h₂ : S₂.HomologyData) [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : IsIso (homologyMap' φ h₁ h₂) := by dsimp only [homologyMap'] infer_instance lemma isIso_homologyMap_of_epi_of_isIso_of_mono' (φ : S₁ ⟶ S₂) [S₁.HasHomology] [S₂.HasHomology] (h₁ : Epi φ.τ₁) (h₂ : IsIso φ.τ₂) (h₃ : Mono φ.τ₃) : IsIso (homologyMap φ) := by dsimp only [homologyMap] infer_instance instance isIso_homologyMap_of_epi_of_isIso_of_mono (φ : S₁ ⟶ S₂) [S₁.HasHomology] [S₂.HasHomology] [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : IsIso (homologyMap φ) := isIso_homologyMap_of_epi_of_isIso_of_mono' φ inferInstance inferInstance inferInstance instance isIso_homologyFunctor_map_of_epi_of_isIso_of_mono (φ : S₁ ⟶ S₂) [CategoryWithHomology C] [Epi φ.τ₁] [IsIso φ.τ₂] [Mono φ.τ₃] : IsIso ((homologyFunctor C).map φ) := (inferInstance : IsIso (homologyMap φ)) instance isIso_homologyMap_of_isIso (φ : S₁ ⟶ S₂) [S₁.HasHomology] [S₂.HasHomology] [IsIso φ] : IsIso (homologyMap φ) := by dsimp only [homologyMap, homologyMap'] infer_instance section variable (S) {A : C} variable [HasHomology S] /-- The canonical morphism `S.cycles ⟶ S.homology` for a short complex `S` that has homology. -/ noncomputable def homologyπ : S.cycles ⟶ S.homology := S.leftHomologyπ ≫ S.leftHomologyIso.hom /-- The canonical morphism `S.homology ⟶ S.opcycles` for a short complex `S` that has homology. -/ noncomputable def homologyι : S.homology ⟶ S.opcycles := S.rightHomologyIso.inv ≫ S.rightHomologyι @[reassoc (attr := simp)] lemma homologyπ_comp_leftHomologyIso_inv : S.homologyπ ≫ S.leftHomologyIso.inv = S.leftHomologyπ := by dsimp only [homologyπ] simp only [assoc, Iso.hom_inv_id, comp_id] @[reassoc (attr := simp)] lemma rightHomologyIso_hom_comp_homologyι : S.rightHomologyIso.hom ≫ S.homologyι = S.rightHomologyι := by dsimp only [homologyι] simp only [Iso.hom_inv_id_assoc] @[reassoc (attr := simp)] lemma toCycles_comp_homologyπ : S.toCycles ≫ S.homologyπ = 0 := by dsimp only [homologyπ] simp only [toCycles_comp_leftHomologyπ_assoc, zero_comp] @[reassoc (attr := simp)] lemma homologyι_comp_fromOpcycles : S.homologyι ≫ S.fromOpcycles = 0 := by dsimp only [homologyι] simp only [assoc, rightHomologyι_comp_fromOpcycles, comp_zero] /-- The homology `S.homology` of a short complex is the cokernel of the morphism `S.toCycles : S.X₁ ⟶ S.cycles`. -/ noncomputable def homologyIsCokernel : IsColimit (CokernelCofork.ofπ S.homologyπ S.toCycles_comp_homologyπ) := IsColimit.ofIsoColimit S.leftHomologyIsCokernel (Cofork.ext S.leftHomologyIso rfl) /-- The homology `S.homology` of a short complex is the kernel of the morphism `S.fromOpcycles : S.opcycles ⟶ S.X₃`. -/ noncomputable def homologyIsKernel : IsLimit (KernelFork.ofι S.homologyι S.homologyι_comp_fromOpcycles) := IsLimit.ofIsoLimit S.rightHomologyIsKernel (Fork.ext S.rightHomologyIso (by simp)) instance : Epi S.homologyπ := Limits.epi_of_isColimit_cofork (S.homologyIsCokernel) instance : Mono S.homologyι := Limits.mono_of_isLimit_fork (S.homologyIsKernel) /-- Given a morphism `k : S.cycles ⟶ A` such that `S.toCycles ≫ k = 0`, this is the induced morphism `S.homology ⟶ A`. -/ noncomputable def descHomology (k : S.cycles ⟶ A) (hk : S.toCycles ≫ k = 0) : S.homology ⟶ A := S.homologyIsCokernel.desc (CokernelCofork.ofπ k hk) /-- Given a morphism `k : A ⟶ S.opcycles` such that `k ≫ S.fromOpcycles = 0`, this is the induced morphism `A ⟶ S.homology`. -/ noncomputable def liftHomology (k : A ⟶ S.opcycles) (hk : k ≫ S.fromOpcycles = 0) : A ⟶ S.homology := S.homologyIsKernel.lift (KernelFork.ofι k hk) @[reassoc (attr := simp)] lemma π_descHomology (k : S.cycles ⟶ A) (hk : S.toCycles ≫ k = 0) : S.homologyπ ≫ S.descHomology k hk = k := Cofork.IsColimit.π_desc S.homologyIsCokernel @[reassoc (attr := simp)] lemma liftHomology_ι (k : A ⟶ S.opcycles) (hk : k ≫ S.fromOpcycles = 0) : S.liftHomology k hk ≫ S.homologyι = k := Fork.IsLimit.lift_ι S.homologyIsKernel @[reassoc (attr := simp)] lemma homologyπ_naturality (φ : S₁ ⟶ S₂) [S₁.HasHomology] [S₂.HasHomology] : S₁.homologyπ ≫ homologyMap φ = cyclesMap φ ≫ S₂.homologyπ := by simp only [← cancel_mono S₂.leftHomologyIso.inv, assoc, ← leftHomologyIso_inv_naturality φ, homologyπ_comp_leftHomologyIso_inv] simp only [homologyπ, assoc, Iso.hom_inv_id_assoc, leftHomologyπ_naturality] @[reassoc (attr := simp)] lemma homologyι_naturality (φ : S₁ ⟶ S₂) [S₁.HasHomology] [S₂.HasHomology] : homologyMap φ ≫ S₂.homologyι = S₁.homologyι ≫ S₁.opcyclesMap φ := by
simp only [← cancel_epi S₁.rightHomologyIso.hom, rightHomologyIso_hom_naturality_assoc φ, rightHomologyIso_hom_comp_homologyι, rightHomologyι_naturality] simp only [homologyι, assoc, Iso.hom_inv_id_assoc] @[reassoc (attr := simp)] lemma homology_π_ι :
Mathlib/Algebra/Homology/ShortComplex/Homology.lean
914
919
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Wen Yang -/ import Mathlib.LinearAlgebra.Matrix.Adjugate import Mathlib.LinearAlgebra.Matrix.ToLin import Mathlib.LinearAlgebra.Matrix.Transvection import Mathlib.RingTheory.RootsOfUnity.Basic /-! # The Special Linear group $SL(n, R)$ This file defines the elements of the Special Linear group `SpecialLinearGroup n R`, consisting of all square `R`-matrices with determinant `1` on the fintype `n` by `n`. In addition, we define the group structure on `SpecialLinearGroup n R` and the embedding into the general linear group `GeneralLinearGroup R (n → R)`. ## Main definitions * `Matrix.SpecialLinearGroup` is the type of matrices with determinant 1 * `Matrix.SpecialLinearGroup.group` gives the group structure (under multiplication) * `Matrix.SpecialLinearGroup.toGL` is the embedding `SLₙ(R) → GLₙ(R)` ## Notation For `m : ℕ`, we introduce the notation `SL(m,R)` for the special linear group on the fintype `n = Fin m`, in the locale `MatrixGroups`. ## Implementation notes The inverse operation in the `SpecialLinearGroup` is defined to be the adjugate matrix, so that `SpecialLinearGroup n R` has a group structure for all `CommRing R`. We define the elements of `SpecialLinearGroup` to be matrices, since we need to compute their determinant. This is in contrast with `GeneralLinearGroup R M`, which consists of invertible `R`-linear maps on `M`. We provide `Matrix.SpecialLinearGroup.hasCoeToFun` for convenience, but do not state any lemmas about it, and use `Matrix.SpecialLinearGroup.coeFn_eq_coe` to eliminate it `⇑` in favor of a regular `↑` coercion. ## References * https://en.wikipedia.org/wiki/Special_linear_group ## Tags matrix group, group, matrix inverse -/ namespace Matrix universe u v open LinearMap section variable (n : Type u) [DecidableEq n] [Fintype n] (R : Type v) [CommRing R] /-- `SpecialLinearGroup n R` is the group of `n` by `n` `R`-matrices with determinant equal to 1. -/ def SpecialLinearGroup := { A : Matrix n n R // A.det = 1 } end @[inherit_doc] scoped[MatrixGroups] notation "SL(" n ", " R ")" => Matrix.SpecialLinearGroup (Fin n) R namespace SpecialLinearGroup variable {n : Type u} [DecidableEq n] [Fintype n] {R : Type v} [CommRing R] instance hasCoeToMatrix : Coe (SpecialLinearGroup n R) (Matrix n n R) := ⟨fun A => A.val⟩ /-- In this file, Lean often has a hard time working out the values of `n` and `R` for an expression like `det ↑A`. Rather than writing `(A : Matrix n n R)` everywhere in this file which is annoyingly verbose, or `A.val` which is not the simp-normal form for subtypes, we create a local notation `↑ₘA`. This notation references the local `n` and `R` variables, so is not valid as a global notation. -/ local notation:1024 "↑ₘ" A:1024 => ((A : SpecialLinearGroup n R) : Matrix n n R) section CoeFnInstance /-- This instance is here for convenience, but is literally the same as the coercion from `hasCoeToMatrix`. -/ instance instCoeFun : CoeFun (SpecialLinearGroup n R) fun _ => n → n → R where coe A := ↑ₘA end CoeFnInstance theorem ext_iff (A B : SpecialLinearGroup n R) : A = B ↔ ∀ i j, A i j = B i j := Subtype.ext_iff.trans Matrix.ext_iff.symm @[ext] theorem ext (A B : SpecialLinearGroup n R) : (∀ i j, A i j = B i j) → A = B := (SpecialLinearGroup.ext_iff A B).mpr instance subsingleton_of_subsingleton [Subsingleton n] : Subsingleton (SpecialLinearGroup n R) := by refine ⟨fun ⟨A, hA⟩ ⟨B, hB⟩ ↦ ?_⟩ ext i j rcases isEmpty_or_nonempty n with hn | hn; · exfalso; exact IsEmpty.false i rw [det_eq_elem_of_subsingleton _ i] at hA hB simp only [Subsingleton.elim j i, hA, hB] instance hasInv : Inv (SpecialLinearGroup n R) := ⟨fun A => ⟨adjugate A, by rw [det_adjugate, A.prop, one_pow]⟩⟩ instance hasMul : Mul (SpecialLinearGroup n R) := ⟨fun A B => ⟨A * B, by rw [det_mul, A.prop, B.prop, one_mul]⟩⟩ instance hasOne : One (SpecialLinearGroup n R) := ⟨⟨1, det_one⟩⟩ instance : Pow (SpecialLinearGroup n R) ℕ where pow x n := ⟨x ^ n, (det_pow _ _).trans <| x.prop.symm ▸ one_pow _⟩ instance : Inhabited (SpecialLinearGroup n R) := ⟨1⟩ instance [Fintype R] [DecidableEq R] : Fintype (SpecialLinearGroup n R) := Subtype.fintype _ instance [Finite R] : Finite (SpecialLinearGroup n R) := Subtype.finite /-- The transpose of a matrix in `SL(n, R)` -/ def transpose (A : SpecialLinearGroup n R) : SpecialLinearGroup n R := ⟨A.1.transpose, A.1.det_transpose ▸ A.2⟩ @[inherit_doc] scoped postfix:1024 "ᵀ" => SpecialLinearGroup.transpose section CoeLemmas variable (A B : SpecialLinearGroup n R) theorem coe_mk (A : Matrix n n R) (h : det A = 1) : ↑(⟨A, h⟩ : SpecialLinearGroup n R) = A := rfl @[simp] theorem coe_inv : ↑ₘ(A⁻¹) = adjugate A := rfl @[simp] theorem coe_mul : ↑ₘ(A * B) = ↑ₘA * ↑ₘB := rfl @[simp] theorem coe_one : (1 : SpecialLinearGroup n R) = (1 : Matrix n n R) := rfl @[simp] theorem det_coe : det ↑ₘA = 1 := A.2 @[simp] theorem coe_pow (m : ℕ) : ↑ₘ(A ^ m) = ↑ₘA ^ m := rfl @[simp] lemma coe_transpose (A : SpecialLinearGroup n R) : ↑ₘAᵀ = (↑ₘA)ᵀ := rfl theorem det_ne_zero [Nontrivial R] (g : SpecialLinearGroup n R) : det ↑ₘg ≠ 0 := by rw [g.det_coe] norm_num theorem row_ne_zero [Nontrivial R] (g : SpecialLinearGroup n R) (i : n) : g i ≠ 0 := fun h => g.det_ne_zero <| det_eq_zero_of_row_eq_zero i <| by simp [h] end CoeLemmas instance monoid : Monoid (SpecialLinearGroup n R) := Function.Injective.monoid _ Subtype.coe_injective coe_one coe_mul coe_pow instance : Group (SpecialLinearGroup n R) := { SpecialLinearGroup.monoid, SpecialLinearGroup.hasInv with inv_mul_cancel := fun A => by ext1 simp [adjugate_mul] } /-- A version of `Matrix.toLin' A` that produces linear equivalences. -/ def toLin' : SpecialLinearGroup n R →* (n → R) ≃ₗ[R] n → R where toFun A := LinearEquiv.ofLinear (Matrix.toLin' ↑ₘA) (Matrix.toLin' ↑ₘA⁻¹) (by rw [← toLin'_mul, ← coe_mul, mul_inv_cancel, coe_one, toLin'_one]) (by rw [← toLin'_mul, ← coe_mul, inv_mul_cancel, coe_one, toLin'_one]) map_one' := LinearEquiv.toLinearMap_injective Matrix.toLin'_one map_mul' A B := LinearEquiv.toLinearMap_injective <| Matrix.toLin'_mul ↑ₘA ↑ₘB theorem toLin'_apply (A : SpecialLinearGroup n R) (v : n → R) : SpecialLinearGroup.toLin' A v = Matrix.toLin' (↑ₘA) v := rfl theorem toLin'_to_linearMap (A : SpecialLinearGroup n R) : ↑(SpecialLinearGroup.toLin' A) = Matrix.toLin' ↑ₘA := rfl theorem toLin'_symm_apply (A : SpecialLinearGroup n R) (v : n → R) : A.toLin'.symm v = Matrix.toLin' (↑ₘA⁻¹) v := rfl theorem toLin'_symm_to_linearMap (A : SpecialLinearGroup n R) : ↑A.toLin'.symm = Matrix.toLin' ↑ₘA⁻¹ := rfl theorem toLin'_injective : Function.Injective ↑(toLin' : SpecialLinearGroup n R →* (n → R) ≃ₗ[R] n → R) := fun _ _ h => Subtype.coe_injective <| Matrix.toLin'.injective <| LinearEquiv.toLinearMap_injective.eq_iff.mpr h variable {S : Type*} [CommRing S] /-- A ring homomorphism from `R` to `S` induces a group homomorphism from `SpecialLinearGroup n R` to `SpecialLinearGroup n S`. -/ @[simps] def map (f : R →+* S) : SpecialLinearGroup n R →* SpecialLinearGroup n S where toFun g := ⟨f.mapMatrix ↑ₘg, by rw [← f.map_det] simp [g.prop]⟩ map_one' := Subtype.ext <| f.mapMatrix.map_one map_mul' x y := Subtype.ext <| f.mapMatrix.map_mul ↑ₘx ↑ₘy section center open Subgroup @[simp] theorem center_eq_bot_of_subsingleton [Subsingleton n] : center (SpecialLinearGroup n R) = ⊥ := eq_bot_iff.mpr fun x _ ↦ by rw [mem_bot, Subsingleton.elim x 1] theorem scalar_eq_self_of_mem_center {A : SpecialLinearGroup n R} (hA : A ∈ center (SpecialLinearGroup n R)) (i : n) : scalar n (A i i) = A := by obtain ⟨r : R, hr : scalar n r = A⟩ := mem_range_scalar_of_commute_transvectionStruct fun t ↦ Subtype.ext_iff.mp <| Subgroup.mem_center_iff.mp hA ⟨t.toMatrix, by simp⟩ simp [← congr_fun₂ hr i i, ← hr] theorem scalar_eq_coe_self_center (A : center (SpecialLinearGroup n R)) (i : n) : scalar n ((A : Matrix n n R) i i) = A := scalar_eq_self_of_mem_center A.property i /-- The center of a special linear group of degree `n` is the subgroup of scalar matrices, for which the scalars are the `n`-th roots of unity. -/ theorem mem_center_iff {A : SpecialLinearGroup n R} : A ∈ center (SpecialLinearGroup n R) ↔ ∃ (r : R), r ^ (Fintype.card n) = 1 ∧ scalar n r = A := by rcases isEmpty_or_nonempty n with hn | ⟨⟨i⟩⟩; · exact ⟨by aesop, by simp [Subsingleton.elim A 1]⟩ refine ⟨fun h ↦ ⟨A i i, ?_, ?_⟩, fun ⟨r, _, hr⟩ ↦ Subgroup.mem_center_iff.mpr fun B ↦ ?_⟩ · have : det ((scalar n) (A i i)) = 1 := (scalar_eq_self_of_mem_center h i).symm ▸ A.property simpa using this · exact scalar_eq_self_of_mem_center h i · suffices ↑ₘ(B * A) = ↑ₘ(A * B) from Subtype.val_injective this simpa only [coe_mul, ← hr] using (scalar_commute (n := n) r (Commute.all r) B).symm /-- An equivalence of groups, from the center of the special linear group to the roots of unity. -/ @[simps] def center_equiv_rootsOfUnity' (i : n) : center (SpecialLinearGroup n R) ≃* rootsOfUnity (Fintype.card n) R where toFun A := haveI : Nonempty n := ⟨i⟩ rootsOfUnity.mkOfPowEq (↑ₘA i i) <| by obtain ⟨r, hr, hr'⟩ := mem_center_iff.mp A.property replace hr' : A.val i i = r := by simp only [← hr', scalar_apply, diagonal_apply_eq] simp only [hr', hr] invFun a := ⟨⟨a • (1 : Matrix n n R), by aesop⟩, Subgroup.mem_center_iff.mpr fun B ↦ Subtype.val_injective <| by simp [coe_mul]⟩ left_inv A := by refine SetCoe.ext <| SetCoe.ext ?_ obtain ⟨r, _, hr⟩ := mem_center_iff.mp A.property simpa [← hr, Submonoid.smul_def, Units.smul_def] using smul_one_eq_diagonal r right_inv a := by obtain ⟨⟨a, _⟩, ha⟩ := a exact SetCoe.ext <| Units.eq_iff.mp <| by simp map_mul' A B := by dsimp ext simp only [rootsOfUnity.val_mkOfPowEq_coe, Subgroup.coe_mul, Units.val_mul] rw [← scalar_eq_coe_self_center A i, ← scalar_eq_coe_self_center B i] simp open scoped Classical in /-- An equivalence of groups, from the center of the special linear group to the roots of unity. See also `center_equiv_rootsOfUnity'`. -/ noncomputable def center_equiv_rootsOfUnity : center (SpecialLinearGroup n R) ≃* rootsOfUnity (max (Fintype.card n) 1) R := (isEmpty_or_nonempty n).by_cases (fun hn ↦ by rw [center_eq_bot_of_subsingleton, Fintype.card_eq_zero, max_eq_right_of_lt zero_lt_one, rootsOfUnity_one] exact MulEquiv.ofUnique) (fun _ ↦ (max_eq_left (NeZero.one_le : 1 ≤ Fintype.card n)).symm ▸ center_equiv_rootsOfUnity' (Classical.arbitrary n)) end center section cast /-- Coercion of SL `n` `ℤ` to SL `n` `R` for a commutative ring `R`. -/ instance : Coe (SpecialLinearGroup n ℤ) (SpecialLinearGroup n R) := ⟨fun x => map (Int.castRingHom R) x⟩ @[simp] theorem coe_matrix_coe (g : SpecialLinearGroup n ℤ) : ↑(g : SpecialLinearGroup n R) = (↑g : Matrix n n ℤ).map (Int.castRingHom R) := map_apply_coe (Int.castRingHom R) g end cast section Neg variable [Fact (Even (Fintype.card n))] /-- Formal operation of negation on special linear group on even cardinality `n` given by negating each element. -/ instance instNeg : Neg (SpecialLinearGroup n R) := ⟨fun g => ⟨-g, by simpa [(@Fact.out <| Even <| Fintype.card n).neg_one_pow, g.det_coe] using det_smul (↑ₘg) (-1)⟩⟩ @[simp] theorem coe_neg (g : SpecialLinearGroup n R) : ↑(-g) = -(g : Matrix n n R) := rfl instance : HasDistribNeg (SpecialLinearGroup n R) := Function.Injective.hasDistribNeg _ Subtype.coe_injective coe_neg coe_mul @[simp] theorem coe_int_neg (g : SpecialLinearGroup n ℤ) : ↑(-g) = (-↑g : SpecialLinearGroup n R) := Subtype.ext <| (@RingHom.mapMatrix n _ _ _ _ _ _ (Int.castRingHom R)).map_neg ↑g end Neg section SpecialCases open scoped MatrixGroups theorem SL2_inv_expl_det (A : SL(2, R)) : det ![![A.1 1 1, -A.1 0 1], ![-A.1 1 0, A.1 0 0]] = 1 := by simpa [-det_coe, Matrix.det_fin_two, mul_comm] using A.2 theorem SL2_inv_expl (A : SL(2, R)) : A⁻¹ = ⟨![![A.1 1 1, -A.1 0 1], ![-A.1 1 0, A.1 0 0]], SL2_inv_expl_det A⟩ := by ext have := Matrix.adjugate_fin_two A.1 rw [coe_inv, this] simp theorem fin_two_induction (P : SL(2, R) → Prop) (h : ∀ (a b c d : R) (hdet : a * d - b * c = 1), P ⟨!![a, b; c, d], by rwa [det_fin_two_of]⟩) (g : SL(2, R)) : P g := by obtain ⟨m, hm⟩ := g convert h (m 0 0) (m 0 1) (m 1 0) (m 1 1) (by rwa [det_fin_two] at hm) ext i j; fin_cases i <;> fin_cases j <;> rfl theorem fin_two_exists_eq_mk_of_apply_zero_one_eq_zero {R : Type*} [Field R] (g : SL(2, R)) (hg : g 1 0 = 0) : ∃ (a b : R) (h : a ≠ 0), g = (⟨!![a, b; 0, a⁻¹], by simp [h]⟩ : SL(2, R)) := by induction g using Matrix.SpecialLinearGroup.fin_two_induction with | h a b c d h_det => replace hg : c = 0 := by simpa using hg have had : a * d = 1 := by rwa [hg, mul_zero, sub_zero] at h_det refine ⟨a, b, left_ne_zero_of_mul_eq_one had, ?_⟩ simp_rw [eq_inv_of_mul_eq_one_right had, hg] lemma isCoprime_row (A : SL(2, R)) (i : Fin 2) : IsCoprime (A i 0) (A i 1) := by refine match i with | 0 => ⟨A 1 1, -(A 1 0), ?_⟩ | 1 => ⟨-(A 0 1), A 0 0, ?_⟩ <;> · simp_rw [det_coe A ▸ det_fin_two A.1] ring lemma isCoprime_col (A : SL(2, R)) (j : Fin 2) : IsCoprime (A 0 j) (A 1 j) := by refine match j with | 0 => ⟨A 1 1, -(A 0 1), ?_⟩ | 1 => ⟨-(A 1 0), A 0 0, ?_⟩ <;> · simp_rw [det_coe A ▸ det_fin_two A.1] ring end SpecialCases end SpecialLinearGroup end Matrix namespace IsCoprime open Matrix MatrixGroups SpecialLinearGroup variable {R : Type*} [CommRing R] /-- Given any pair of coprime elements of `R`, there exists a matrix in `SL(2, R)` having those entries as its left or right column. -/ lemma exists_SL2_col {a b : R} (hab : IsCoprime a b) (j : Fin 2) : ∃ g : SL(2, R), g 0 j = a ∧ g 1 j = b := by obtain ⟨u, v, h⟩ := hab refine match j with | 0 => ⟨⟨!![a, -v; b, u], ?_⟩, rfl, rfl⟩ | 1 => ⟨⟨!![v, a; -u, b], ?_⟩, rfl, rfl⟩ <;> · rw [Matrix.det_fin_two_of, ← h] ring /-- Given any pair of coprime elements of `R`, there exists a matrix in `SL(2, R)` having those entries as its top or bottom row. -/ lemma exists_SL2_row {a b : R} (hab : IsCoprime a b) (i : Fin 2) : ∃ g : SL(2, R), g i 0 = a ∧ g i 1 = b := by obtain ⟨u, v, h⟩ := hab refine match i with | 0 => ⟨⟨!![a, b; -v, u], ?_⟩, rfl, rfl⟩ | 1 => ⟨⟨!![v, -u; a, b], ?_⟩, rfl, rfl⟩ <;> · rw [Matrix.det_fin_two_of, ← h] ring /-- A vector with coprime entries, right-multiplied by a matrix in `SL(2, R)`, has coprime entries. -/ lemma vecMulSL {v : Fin 2 → R} (hab : IsCoprime (v 0) (v 1)) (A : SL(2, R)) : IsCoprime ((v ᵥ* A.1) 0) ((v ᵥ* A.1) 1) := by obtain ⟨g, hg⟩ := hab.exists_SL2_row 0 have : v = g 0 := funext fun t ↦ by { fin_cases t <;> tauto } simpa only [this] using isCoprime_row (g * A) 0 /-- A vector with coprime entries, left-multiplied by a matrix in `SL(2, R)`, has coprime entries. -/ lemma mulVecSL {v : Fin 2 → R} (hab : IsCoprime (v 0) (v 1)) (A : SL(2, R)) : IsCoprime ((A.1 *ᵥ v) 0) ((A.1 *ᵥ v) 1) := by simpa only [← vecMul_transpose] using hab.vecMulSL A.transpose end IsCoprime namespace ModularGroup open MatrixGroups open Matrix Matrix.SpecialLinearGroup /-- The matrix `S = [[0, -1], [1, 0]]` as an element of `SL(2, ℤ)`. This element acts naturally on the Euclidean plane as a rotation about the origin by `π / 2`. This element also acts naturally on the hyperbolic plane as rotation about `i` by `π`. It represents the Mobiüs transformation `z ↦ -1/z` and is an involutive elliptic isometry. -/ def S : SL(2, ℤ) := ⟨!![0, -1; 1, 0], by norm_num [Matrix.det_fin_two_of]⟩ /-- The matrix `T = [[1, 1], [0, 1]]` as an element of `SL(2, ℤ)`. -/ def T : SL(2, ℤ) := ⟨!![1, 1; 0, 1], by norm_num [Matrix.det_fin_two_of]⟩ theorem coe_S : ↑S = !![0, -1; 1, 0] := rfl theorem coe_T : ↑T = (!![1, 1; 0, 1] : Matrix _ _ ℤ) := rfl theorem coe_T_inv : ↑(T⁻¹) = !![1, -1; 0, 1] := by simp [coe_inv, coe_T, adjugate_fin_two] theorem coe_T_zpow (n : ℤ) : (T ^ n).1 = !![1, n; 0, 1] := by induction n with | hz => rw [zpow_zero, coe_one, Matrix.one_fin_two] | hp n h => simp_rw [zpow_add, zpow_one, coe_mul, h, coe_T, Matrix.mul_fin_two] congrm !![_, ?_; _, _] rw [mul_one, mul_one, add_comm] | hn n h => simp_rw [zpow_sub, zpow_one, coe_mul, h, coe_T_inv, Matrix.mul_fin_two] congrm !![?_, ?_; _, _] <;> ring @[simp] theorem T_pow_mul_apply_one (n : ℤ) (g : SL(2, ℤ)) : (T ^ n * g) 1 = g 1 := by ext j simp [coe_T_zpow, Matrix.vecMul, dotProduct, Fin.sum_univ_succ, vecTail] @[simp] theorem T_mul_apply_one (g : SL(2, ℤ)) : (T * g) 1 = g 1 := by simpa using T_pow_mul_apply_one 1 g @[simp] theorem T_inv_mul_apply_one (g : SL(2, ℤ)) : (T⁻¹ * g) 1 = g 1 := by simpa using T_pow_mul_apply_one (-1) g lemma S_mul_S_eq : (S : Matrix (Fin 2) (Fin 2) ℤ) * S = -1 := by simp only [S, Int.reduceNeg, pow_two, coe_mul, cons_mul, Nat.succ_eq_add_one, Nat.reduceAdd, vecMul_cons, head_cons, zero_smul, tail_cons, neg_smul, one_smul, neg_cons, neg_zero, neg_empty, empty_vecMul, add_zero, zero_add, empty_mul, Equiv.symm_apply_apply] exact Eq.symm (eta_fin_two (-1)) lemma T_S_rel : S • S • S • T • S • T • S = T⁻¹ := by ext i j fin_cases i <;> fin_cases j <;> rfl end ModularGroup
Mathlib/LinearAlgebra/Matrix/SpecialLinearGroup.lean
511
511
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin, Lu-Ming Zhang -/ import Mathlib.Algebra.Algebra.Opposite import Mathlib.Algebra.Algebra.Pi import Mathlib.Algebra.BigOperators.RingEquiv import Mathlib.Data.Finite.Prod import Mathlib.Data.Matrix.Mul import Mathlib.LinearAlgebra.Pi /-! # Matrices This file contains basic results on matrices including bundled versions of matrix operators. ## Implementation notes For convenience, `Matrix m n α` is defined as `m → n → α`, as this allows elements of the matrix to be accessed with `A i j`. However, it is not advisable to _construct_ matrices using terms of the form `fun i j ↦ _` or even `(fun i j ↦ _ : Matrix m n α)`, as these are not recognized by Lean as having the right type. Instead, `Matrix.of` should be used. ## TODO Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented. -/ assert_not_exists Star universe u u' v w variable {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variable {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*} namespace Matrix instance decidableEq [DecidableEq α] [Fintype m] [Fintype n] : DecidableEq (Matrix m n α) := Fintype.decidablePiFintype instance {n m} [Fintype m] [DecidableEq m] [Fintype n] [DecidableEq n] (α) [Fintype α] : Fintype (Matrix m n α) := inferInstanceAs (Fintype (m → n → α)) instance {n m} [Finite m] [Finite n] (α) [Finite α] : Finite (Matrix m n α) := inferInstanceAs (Finite (m → n → α)) section variable (R) /-- This is `Matrix.of` bundled as a linear equivalence. -/ def ofLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : (m → n → α) ≃ₗ[R] Matrix m n α where __ := ofAddEquiv map_smul' _ _ := rfl @[simp] lemma coe_ofLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : ⇑(ofLinearEquiv _ : (m → n → α) ≃ₗ[R] Matrix m n α) = of := rfl @[simp] lemma coe_ofLinearEquiv_symm [Semiring R] [AddCommMonoid α] [Module R α] : ⇑((ofLinearEquiv _).symm : Matrix m n α ≃ₗ[R] (m → n → α)) = of.symm := rfl end theorem sum_apply [AddCommMonoid α] (i : m) (j : n) (s : Finset β) (g : β → Matrix m n α) : (∑ c ∈ s, g c) i j = ∑ c ∈ s, g c i j := (congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _) end Matrix open Matrix namespace Matrix section Diagonal variable [DecidableEq n] variable (n α) /-- `Matrix.diagonal` as an `AddMonoidHom`. -/ @[simps] def diagonalAddMonoidHom [AddZeroClass α] : (n → α) →+ Matrix n n α where toFun := diagonal map_zero' := diagonal_zero map_add' x y := (diagonal_add x y).symm variable (R) /-- `Matrix.diagonal` as a `LinearMap`. -/ @[simps] def diagonalLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : (n → α) →ₗ[R] Matrix n n α := { diagonalAddMonoidHom n α with map_smul' := diagonal_smul } variable {n α R} section One variable [Zero α] [One α] lemma zero_le_one_elem [Preorder α] [ZeroLEOneClass α] (i j : n) : 0 ≤ (1 : Matrix n n α) i j := by by_cases hi : i = j · subst hi simp · simp [hi] lemma zero_le_one_row [Preorder α] [ZeroLEOneClass α] (i : n) : 0 ≤ (1 : Matrix n n α) i := zero_le_one_elem i end One end Diagonal section Diag variable (n α) /-- `Matrix.diag` as an `AddMonoidHom`. -/ @[simps] def diagAddMonoidHom [AddZeroClass α] : Matrix n n α →+ n → α where toFun := diag map_zero' := diag_zero map_add' := diag_add variable (R) /-- `Matrix.diag` as a `LinearMap`. -/ @[simps] def diagLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : Matrix n n α →ₗ[R] n → α := { diagAddMonoidHom n α with map_smul' := diag_smul } variable {n α R} @[simp] theorem diag_list_sum [AddMonoid α] (l : List (Matrix n n α)) : diag l.sum = (l.map diag).sum := map_list_sum (diagAddMonoidHom n α) l @[simp] theorem diag_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix n n α)) : diag s.sum = (s.map diag).sum := map_multiset_sum (diagAddMonoidHom n α) s @[simp] theorem diag_sum {ι} [AddCommMonoid α] (s : Finset ι) (f : ι → Matrix n n α) : diag (∑ i ∈ s, f i) = ∑ i ∈ s, diag (f i) := map_sum (diagAddMonoidHom n α) f s end Diag open Matrix section AddCommMonoid variable [AddCommMonoid α] [Mul α] end AddCommMonoid section NonAssocSemiring variable [NonAssocSemiring α] variable (α n) /-- `Matrix.diagonal` as a `RingHom`. -/ @[simps] def diagonalRingHom [Fintype n] [DecidableEq n] : (n → α) →+* Matrix n n α := { diagonalAddMonoidHom n α with toFun := diagonal map_one' := diagonal_one map_mul' := fun _ _ => (diagonal_mul_diagonal' _ _).symm } end NonAssocSemiring section Semiring variable [Semiring α] theorem diagonal_pow [Fintype n] [DecidableEq n] (v : n → α) (k : ℕ) : diagonal v ^ k = diagonal (v ^ k) := (map_pow (diagonalRingHom n α) v k).symm /-- The ring homomorphism `α →+* Matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [DecidableEq n] [Fintype n] : α →+* Matrix n n α := (diagonalRingHom n α).comp <| Pi.constRingHom n α section Scalar variable [DecidableEq n] [Fintype n] @[simp] theorem scalar_apply (a : α) : scalar n a = diagonal fun _ => a := rfl theorem scalar_inj [Nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := (diagonal_injective.comp Function.const_injective).eq_iff theorem scalar_commute_iff {r : α} {M : Matrix n n α} : Commute (scalar n r) M ↔ r • M = MulOpposite.op r • M := by simp_rw [Commute, SemiconjBy, scalar_apply, ← smul_eq_diagonal_mul, ← op_smul_eq_mul_diagonal] theorem scalar_commute (r : α) (hr : ∀ r', Commute r r') (M : Matrix n n α) : Commute (scalar n r) M := scalar_commute_iff.2 <| ext fun _ _ => hr _ end Scalar end Semiring section Algebra variable [Fintype n] [DecidableEq n] variable [CommSemiring R] [Semiring α] [Semiring β] [Algebra R α] [Algebra R β] instance instAlgebra : Algebra R (Matrix n n α) where algebraMap := (Matrix.scalar n).comp (algebraMap R α) commutes' _ _ := scalar_commute _ (fun _ => Algebra.commutes _ _) _ smul_def' r x := by ext; simp [Matrix.scalar, Algebra.smul_def r] theorem algebraMap_matrix_apply {r : R} {i j : n} : algebraMap R (Matrix n n α) r i j = if i = j then algebraMap R α r else 0 := by dsimp [algebraMap, Algebra.algebraMap, Matrix.scalar] split_ifs with h <;> simp [h, Matrix.one_apply_ne] theorem algebraMap_eq_diagonal (r : R) : algebraMap R (Matrix n n α) r = diagonal (algebraMap R (n → α) r) := rfl theorem algebraMap_eq_diagonalRingHom : algebraMap R (Matrix n n α) = (diagonalRingHom n α).comp (algebraMap R _) := rfl @[simp] theorem map_algebraMap (r : R) (f : α → β) (hf : f 0 = 0) (hf₂ : f (algebraMap R α r) = algebraMap R β r) : (algebraMap R (Matrix n n α) r).map f = algebraMap R (Matrix n n β) r := by rw [algebraMap_eq_diagonal, algebraMap_eq_diagonal, diagonal_map hf] simp [hf₂] variable (R) /-- `Matrix.diagonal` as an `AlgHom`. -/ @[simps] def diagonalAlgHom : (n → α) →ₐ[R] Matrix n n α := { diagonalRingHom n α with toFun := diagonal commutes' := fun r => (algebraMap_eq_diagonal r).symm } end Algebra section AddHom variable [Add α] variable (R α) in /-- Extracting entries from a matrix as an additive homomorphism. -/ @[simps] def entryAddHom (i : m) (j : n) : AddHom (Matrix m n α) α where toFun M := M i j map_add' _ _ := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryAddHom_eq_comp {i : m} {j : n} : entryAddHom α i j = ((Pi.evalAddHom (fun _ => α) j).comp (Pi.evalAddHom _ i)).comp (AddHomClass.toAddHom ofAddEquiv.symm) := rfl end AddHom section AddMonoidHom variable [AddZeroClass α] variable (R α) in /-- Extracting entries from a matrix as an additive monoid homomorphism. Note this cannot be upgraded to a ring homomorphism, as it does not respect multiplication. -/ @[simps] def entryAddMonoidHom (i : m) (j : n) : Matrix m n α →+ α where toFun M := M i j map_add' _ _ := rfl map_zero' := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryAddMonoidHom_eq_comp {i : m} {j : n} : entryAddMonoidHom α i j = ((Pi.evalAddMonoidHom (fun _ => α) j).comp (Pi.evalAddMonoidHom _ i)).comp (AddMonoidHomClass.toAddMonoidHom ofAddEquiv.symm) := by rfl @[simp] lemma evalAddMonoidHom_comp_diagAddMonoidHom (i : m) : (Pi.evalAddMonoidHom _ i).comp (diagAddMonoidHom m α) = entryAddMonoidHom α i i := by simp [AddMonoidHom.ext_iff] @[simp] lemma entryAddMonoidHom_toAddHom {i : m} {j : n} : (entryAddMonoidHom α i j : AddHom _ _) = entryAddHom α i j := rfl end AddMonoidHom section LinearMap variable [Semiring R] [AddCommMonoid α] [Module R α] variable (R α) in /-- Extracting entries from a matrix as a linear map. Note this cannot be upgraded to an algebra homomorphism, as it does not respect multiplication. -/ @[simps] def entryLinearMap (i : m) (j : n) : Matrix m n α →ₗ[R] α where toFun M := M i j map_add' _ _ := rfl map_smul' _ _ := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryLinearMap_eq_comp {i : m} {j : n} : entryLinearMap R α i j = LinearMap.proj j ∘ₗ LinearMap.proj i ∘ₗ (ofLinearEquiv R).symm.toLinearMap := by rfl @[simp] lemma proj_comp_diagLinearMap (i : m) : LinearMap.proj i ∘ₗ diagLinearMap m R α = entryLinearMap R α i i := by simp [LinearMap.ext_iff] @[simp] lemma entryLinearMap_toAddMonoidHom {i : m} {j : n} : (entryLinearMap R α i j : _ →+ _) = entryAddMonoidHom α i j := rfl @[simp] lemma entryLinearMap_toAddHom {i : m} {j : n} : (entryLinearMap R α i j : AddHom _ _) = entryAddHom α i j := rfl end LinearMap end Matrix /-! ### Bundled versions of `Matrix.map` -/ namespace Equiv /-- The `Equiv` between spaces of matrices induced by an `Equiv` between their coefficients. This is `Matrix.map` as an `Equiv`. -/ @[simps apply] def mapMatrix (f : α ≃ β) : Matrix m n α ≃ Matrix m n β where toFun M := M.map f invFun M := M.map f.symm left_inv _ := Matrix.ext fun _ _ => f.symm_apply_apply _ right_inv _ := Matrix.ext fun _ _ => f.apply_symm_apply _ @[simp] theorem mapMatrix_refl : (Equiv.refl α).mapMatrix = Equiv.refl (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃ _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ β) (g : β ≃ γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃ _) := rfl end Equiv namespace AddMonoidHom variable [AddZeroClass α] [AddZeroClass β] [AddZeroClass γ] /-- The `AddMonoidHom` between spaces of matrices induced by an `AddMonoidHom` between their coefficients. This is `Matrix.map` as an `AddMonoidHom`. -/ @[simps] def mapMatrix (f : α →+ β) : Matrix m n α →+ Matrix m n β where toFun M := M.map f map_zero' := Matrix.map_zero f f.map_zero map_add' := Matrix.map_add f f.map_add @[simp] theorem mapMatrix_id : (AddMonoidHom.id α).mapMatrix = AddMonoidHom.id (Matrix m n α) := rfl @[simp] theorem mapMatrix_comp (f : β →+ γ) (g : α →+ β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m n α →+ _) := rfl @[simp] lemma entryAddMonoidHom_comp_mapMatrix (f : α →+ β) (i : m) (j : n) : (entryAddMonoidHom β i j).comp f.mapMatrix = f.comp (entryAddMonoidHom α i j) := rfl end AddMonoidHom namespace AddEquiv variable [Add α] [Add β] [Add γ] /-- The `AddEquiv` between spaces of matrices induced by an `AddEquiv` between their coefficients. This is `Matrix.map` as an `AddEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃+ β) : Matrix m n α ≃+ Matrix m n β := { f.toEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm map_add' := Matrix.map_add f (map_add f) } @[simp] theorem mapMatrix_refl : (AddEquiv.refl α).mapMatrix = AddEquiv.refl (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃+ β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃+ _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃+ β) (g : β ≃+ γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃+ _) := rfl @[simp] lemma entryAddHom_comp_mapMatrix (f : α ≃+ β) (i : m) (j : n) : (entryAddHom β i j).comp (AddHomClass.toAddHom f.mapMatrix) = (f : AddHom α β).comp (entryAddHom _ i j) := rfl end AddEquiv namespace LinearMap variable [Semiring R] [AddCommMonoid α] [AddCommMonoid β] [AddCommMonoid γ] variable [Module R α] [Module R β] [Module R γ] /-- The `LinearMap` between spaces of matrices induced by a `LinearMap` between their coefficients. This is `Matrix.map` as a `LinearMap`. -/ @[simps] def mapMatrix (f : α →ₗ[R] β) : Matrix m n α →ₗ[R] Matrix m n β where toFun M := M.map f map_add' := Matrix.map_add f f.map_add map_smul' r := Matrix.map_smul f r (f.map_smul r) @[simp] theorem mapMatrix_id : LinearMap.id.mapMatrix = (LinearMap.id : Matrix m n α →ₗ[R] _) := rfl @[simp] theorem mapMatrix_comp (f : β →ₗ[R] γ) (g : α →ₗ[R] β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m n α →ₗ[R] _) := rfl @[simp] lemma entryLinearMap_comp_mapMatrix (f : α →ₗ[R] β) (i : m) (j : n) : entryLinearMap R _ i j ∘ₗ f.mapMatrix = f ∘ₗ entryLinearMap R _ i j := rfl end LinearMap namespace LinearEquiv variable [Semiring R] [AddCommMonoid α] [AddCommMonoid β] [AddCommMonoid γ] variable [Module R α] [Module R β] [Module R γ] /-- The `LinearEquiv` between spaces of matrices induced by a `LinearEquiv` between their coefficients. This is `Matrix.map` as a `LinearEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃ₗ[R] β) : Matrix m n α ≃ₗ[R] Matrix m n β := { f.toEquiv.mapMatrix, f.toLinearMap.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : (LinearEquiv.refl R α).mapMatrix = LinearEquiv.refl R (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ₗ[R] β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃ₗ[R] _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ₗ[R] β) (g : β ≃ₗ[R] γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃ₗ[R] _) := rfl @[simp] lemma mapMatrix_toLinearMap (f : α ≃ₗ[R] β) : (f.mapMatrix : _ ≃ₗ[R] Matrix m n β).toLinearMap = f.toLinearMap.mapMatrix := by rfl @[simp] lemma entryLinearMap_comp_mapMatrix (f : α ≃ₗ[R] β) (i : m) (j : n) : entryLinearMap R _ i j ∘ₗ f.mapMatrix.toLinearMap = f.toLinearMap ∘ₗ entryLinearMap R _ i j := by simp only [mapMatrix_toLinearMap, LinearMap.entryLinearMap_comp_mapMatrix] end LinearEquiv namespace RingHom variable [Fintype m] [DecidableEq m] variable [NonAssocSemiring α] [NonAssocSemiring β] [NonAssocSemiring γ] /-- The `RingHom` between spaces of square matrices induced by a `RingHom` between their coefficients. This is `Matrix.map` as a `RingHom`. -/ @[simps] def mapMatrix (f : α →+* β) : Matrix m m α →+* Matrix m m β := { f.toAddMonoidHom.mapMatrix with toFun := fun M => M.map f map_one' := by simp map_mul' := fun _ _ => Matrix.map_mul } @[simp] theorem mapMatrix_id : (RingHom.id α).mapMatrix = RingHom.id (Matrix m m α) := rfl @[simp] theorem mapMatrix_comp (f : β →+* γ) (g : α →+* β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m m α →+* _) := rfl end RingHom namespace RingEquiv variable [Fintype m] [DecidableEq m] variable [NonAssocSemiring α] [NonAssocSemiring β] [NonAssocSemiring γ] /-- The `RingEquiv` between spaces of square matrices induced by a `RingEquiv` between their coefficients. This is `Matrix.map` as a `RingEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃+* β) : Matrix m m α ≃+* Matrix m m β := { f.toRingHom.mapMatrix, f.toAddEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : (RingEquiv.refl α).mapMatrix = RingEquiv.refl (Matrix m m α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃+* β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m m β ≃+* _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃+* β) (g : β ≃+* γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m m α ≃+* _) := rfl open MulOpposite in /-- For any ring `R`, we have ring isomorphism `Matₙₓₙ(Rᵒᵖ) ≅ (Matₙₓₙ(R))ᵒᵖ` given by transpose. -/ @[simps apply symm_apply] def mopMatrix : Matrix m m αᵐᵒᵖ ≃+* (Matrix m m α)ᵐᵒᵖ where toFun M := op (M.transpose.map unop) invFun M := M.unop.transpose.map op left_inv _ := by aesop right_inv _ := by aesop map_mul' _ _ := unop_injective <| by ext; simp [transpose, mul_apply] map_add' _ _ := by aesop end RingEquiv namespace AlgHom variable [Fintype m] [DecidableEq m] variable [CommSemiring R] [Semiring α] [Semiring β] [Semiring γ] variable [Algebra R α] [Algebra R β] [Algebra R γ] /-- The `AlgHom` between spaces of square matrices induced by an `AlgHom` between their coefficients. This is `Matrix.map` as an `AlgHom`. -/ @[simps] def mapMatrix (f : α →ₐ[R] β) : Matrix m m α →ₐ[R] Matrix m m β := { f.toRingHom.mapMatrix with toFun := fun M => M.map f commutes' := fun r => Matrix.map_algebraMap r f (map_zero _) (f.commutes r) } @[simp] theorem mapMatrix_id : (AlgHom.id R α).mapMatrix = AlgHom.id R (Matrix m m α) := rfl @[simp] theorem mapMatrix_comp (f : β →ₐ[R] γ) (g : α →ₐ[R] β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m m α →ₐ[R] _) := rfl end AlgHom namespace AlgEquiv variable [Fintype m] [DecidableEq m] variable [CommSemiring R] [Semiring α] [Semiring β] [Semiring γ] variable [Algebra R α] [Algebra R β] [Algebra R γ] /-- The `AlgEquiv` between spaces of square matrices induced by an `AlgEquiv` between their coefficients. This is `Matrix.map` as an `AlgEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃ₐ[R] β) : Matrix m m α ≃ₐ[R] Matrix m m β := { f.toAlgHom.mapMatrix, f.toRingEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : AlgEquiv.refl.mapMatrix = (AlgEquiv.refl : Matrix m m α ≃ₐ[R] _) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ₐ[R] β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m m β ≃ₐ[R] _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ₐ[R] β) (g : β ≃ₐ[R] γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m m α ≃ₐ[R] _) := rfl /-- For any algebra `α` over a ring `R`, we have an `R`-algebra isomorphism `Matₙₓₙ(αᵒᵖ) ≅ (Matₙₓₙ(R))ᵒᵖ` given by transpose. If `α` is commutative, we can get rid of the `ᵒᵖ` in the left-hand side, see `Matrix.transposeAlgEquiv`. -/ @[simps!] def mopMatrix : Matrix m m αᵐᵒᵖ ≃ₐ[R] (Matrix m m α)ᵐᵒᵖ where __ := RingEquiv.mopMatrix commutes' _ := MulOpposite.unop_injective <| by ext; simp [algebraMap_matrix_apply, eq_comm, apply_ite MulOpposite.unop] end AlgEquiv open Matrix namespace Matrix section Transpose open Matrix variable (m n α) /-- `Matrix.transpose` as an `AddEquiv` -/ @[simps apply] def transposeAddEquiv [Add α] : Matrix m n α ≃+ Matrix n m α where toFun := transpose invFun := transpose left_inv := transpose_transpose right_inv := transpose_transpose map_add' := transpose_add @[simp] theorem transposeAddEquiv_symm [Add α] : (transposeAddEquiv m n α).symm = transposeAddEquiv n m α := rfl variable {m n α} theorem transpose_list_sum [AddMonoid α] (l : List (Matrix m n α)) : l.sumᵀ = (l.map transpose).sum := map_list_sum (transposeAddEquiv m n α) l theorem transpose_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix m n α)) : s.sumᵀ = (s.map transpose).sum := (transposeAddEquiv m n α).toAddMonoidHom.map_multiset_sum s theorem transpose_sum [AddCommMonoid α] {ι : Type*} (s : Finset ι) (M : ι → Matrix m n α) : (∑ i ∈ s, M i)ᵀ = ∑ i ∈ s, (M i)ᵀ := map_sum (transposeAddEquiv m n α) _ s variable (m n R α) /-- `Matrix.transpose` as a `LinearMap` -/ @[simps apply] def transposeLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : Matrix m n α ≃ₗ[R] Matrix n m α := { transposeAddEquiv m n α with map_smul' := transpose_smul } @[simp] theorem transposeLinearEquiv_symm [Semiring R] [AddCommMonoid α] [Module R α] : (transposeLinearEquiv m n R α).symm = transposeLinearEquiv n m R α := rfl variable {m n R α} variable (m α) /-- `Matrix.transpose` as a `RingEquiv` to the opposite ring -/ @[simps] def transposeRingEquiv [AddCommMonoid α] [CommSemigroup α] [Fintype m] : Matrix m m α ≃+* (Matrix m m α)ᵐᵒᵖ := { (transposeAddEquiv m m α).trans MulOpposite.opAddEquiv with toFun := fun M => MulOpposite.op Mᵀ invFun := fun M => M.unopᵀ map_mul' := fun M N => (congr_arg MulOpposite.op (transpose_mul M N)).trans (MulOpposite.op_mul _ _) left_inv := fun M => transpose_transpose M right_inv := fun M => MulOpposite.unop_injective <| transpose_transpose M.unop } variable {m α} @[simp] theorem transpose_pow [CommSemiring α] [Fintype m] [DecidableEq m] (M : Matrix m m α) (k : ℕ) : (M ^ k)ᵀ = Mᵀ ^ k := MulOpposite.op_injective <| map_pow (transposeRingEquiv m α) M k theorem transpose_list_prod [CommSemiring α] [Fintype m] [DecidableEq m] (l : List (Matrix m m α)) : l.prodᵀ = (l.map transpose).reverse.prod := (transposeRingEquiv m α).unop_map_list_prod l variable (R m α) /-- `Matrix.transpose` as an `AlgEquiv` to the opposite ring -/ @[simps] def transposeAlgEquiv [CommSemiring R] [CommSemiring α] [Fintype m] [DecidableEq m] [Algebra R α] : Matrix m m α ≃ₐ[R] (Matrix m m α)ᵐᵒᵖ := { (transposeAddEquiv m m α).trans MulOpposite.opAddEquiv, transposeRingEquiv m α with toFun := fun M => MulOpposite.op Mᵀ commutes' := fun r => by simp only [algebraMap_eq_diagonal, diagonal_transpose, MulOpposite.algebraMap_apply] } variable {R m α} end Transpose end Matrix
Mathlib/Data/Matrix/Basic.lean
2,287
2,291
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin, Lu-Ming Zhang -/ import Mathlib.Algebra.Algebra.Opposite import Mathlib.Algebra.Algebra.Pi import Mathlib.Algebra.BigOperators.RingEquiv import Mathlib.Data.Finite.Prod import Mathlib.Data.Matrix.Mul import Mathlib.LinearAlgebra.Pi /-! # Matrices This file contains basic results on matrices including bundled versions of matrix operators. ## Implementation notes For convenience, `Matrix m n α` is defined as `m → n → α`, as this allows elements of the matrix to be accessed with `A i j`. However, it is not advisable to _construct_ matrices using terms of the form `fun i j ↦ _` or even `(fun i j ↦ _ : Matrix m n α)`, as these are not recognized by Lean as having the right type. Instead, `Matrix.of` should be used. ## TODO Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented. -/ assert_not_exists Star universe u u' v w variable {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variable {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*} namespace Matrix instance decidableEq [DecidableEq α] [Fintype m] [Fintype n] : DecidableEq (Matrix m n α) := Fintype.decidablePiFintype instance {n m} [Fintype m] [DecidableEq m] [Fintype n] [DecidableEq n] (α) [Fintype α] : Fintype (Matrix m n α) := inferInstanceAs (Fintype (m → n → α)) instance {n m} [Finite m] [Finite n] (α) [Finite α] : Finite (Matrix m n α) := inferInstanceAs (Finite (m → n → α)) section variable (R) /-- This is `Matrix.of` bundled as a linear equivalence. -/ def ofLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : (m → n → α) ≃ₗ[R] Matrix m n α where __ := ofAddEquiv map_smul' _ _ := rfl @[simp] lemma coe_ofLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : ⇑(ofLinearEquiv _ : (m → n → α) ≃ₗ[R] Matrix m n α) = of := rfl @[simp] lemma coe_ofLinearEquiv_symm [Semiring R] [AddCommMonoid α] [Module R α] : ⇑((ofLinearEquiv _).symm : Matrix m n α ≃ₗ[R] (m → n → α)) = of.symm := rfl end theorem sum_apply [AddCommMonoid α] (i : m) (j : n) (s : Finset β) (g : β → Matrix m n α) : (∑ c ∈ s, g c) i j = ∑ c ∈ s, g c i j := (congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _) end Matrix open Matrix namespace Matrix section Diagonal variable [DecidableEq n] variable (n α) /-- `Matrix.diagonal` as an `AddMonoidHom`. -/ @[simps] def diagonalAddMonoidHom [AddZeroClass α] : (n → α) →+ Matrix n n α where toFun := diagonal map_zero' := diagonal_zero map_add' x y := (diagonal_add x y).symm variable (R) /-- `Matrix.diagonal` as a `LinearMap`. -/ @[simps] def diagonalLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : (n → α) →ₗ[R] Matrix n n α := { diagonalAddMonoidHom n α with map_smul' := diagonal_smul } variable {n α R} section One variable [Zero α] [One α] lemma zero_le_one_elem [Preorder α] [ZeroLEOneClass α] (i j : n) : 0 ≤ (1 : Matrix n n α) i j := by by_cases hi : i = j · subst hi simp · simp [hi] lemma zero_le_one_row [Preorder α] [ZeroLEOneClass α] (i : n) : 0 ≤ (1 : Matrix n n α) i := zero_le_one_elem i end One end Diagonal section Diag variable (n α) /-- `Matrix.diag` as an `AddMonoidHom`. -/ @[simps] def diagAddMonoidHom [AddZeroClass α] : Matrix n n α →+ n → α where toFun := diag map_zero' := diag_zero map_add' := diag_add variable (R) /-- `Matrix.diag` as a `LinearMap`. -/ @[simps] def diagLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : Matrix n n α →ₗ[R] n → α := { diagAddMonoidHom n α with map_smul' := diag_smul } variable {n α R} @[simp] theorem diag_list_sum [AddMonoid α] (l : List (Matrix n n α)) : diag l.sum = (l.map diag).sum := map_list_sum (diagAddMonoidHom n α) l @[simp] theorem diag_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix n n α)) : diag s.sum = (s.map diag).sum := map_multiset_sum (diagAddMonoidHom n α) s @[simp] theorem diag_sum {ι} [AddCommMonoid α] (s : Finset ι) (f : ι → Matrix n n α) : diag (∑ i ∈ s, f i) = ∑ i ∈ s, diag (f i) := map_sum (diagAddMonoidHom n α) f s end Diag open Matrix section AddCommMonoid variable [AddCommMonoid α] [Mul α] end AddCommMonoid section NonAssocSemiring variable [NonAssocSemiring α] variable (α n) /-- `Matrix.diagonal` as a `RingHom`. -/ @[simps] def diagonalRingHom [Fintype n] [DecidableEq n] : (n → α) →+* Matrix n n α := { diagonalAddMonoidHom n α with toFun := diagonal map_one' := diagonal_one map_mul' := fun _ _ => (diagonal_mul_diagonal' _ _).symm } end NonAssocSemiring section Semiring variable [Semiring α] theorem diagonal_pow [Fintype n] [DecidableEq n] (v : n → α) (k : ℕ) : diagonal v ^ k = diagonal (v ^ k) := (map_pow (diagonalRingHom n α) v k).symm /-- The ring homomorphism `α →+* Matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [DecidableEq n] [Fintype n] : α →+* Matrix n n α := (diagonalRingHom n α).comp <| Pi.constRingHom n α section Scalar variable [DecidableEq n] [Fintype n] @[simp] theorem scalar_apply (a : α) : scalar n a = diagonal fun _ => a := rfl theorem scalar_inj [Nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := (diagonal_injective.comp Function.const_injective).eq_iff theorem scalar_commute_iff {r : α} {M : Matrix n n α} : Commute (scalar n r) M ↔ r • M = MulOpposite.op r • M := by simp_rw [Commute, SemiconjBy, scalar_apply, ← smul_eq_diagonal_mul, ← op_smul_eq_mul_diagonal] theorem scalar_commute (r : α) (hr : ∀ r', Commute r r') (M : Matrix n n α) : Commute (scalar n r) M := scalar_commute_iff.2 <| ext fun _ _ => hr _ end Scalar end Semiring section Algebra variable [Fintype n] [DecidableEq n] variable [CommSemiring R] [Semiring α] [Semiring β] [Algebra R α] [Algebra R β] instance instAlgebra : Algebra R (Matrix n n α) where algebraMap := (Matrix.scalar n).comp (algebraMap R α) commutes' _ _ := scalar_commute _ (fun _ => Algebra.commutes _ _) _ smul_def' r x := by ext; simp [Matrix.scalar, Algebra.smul_def r] theorem algebraMap_matrix_apply {r : R} {i j : n} : algebraMap R (Matrix n n α) r i j = if i = j then algebraMap R α r else 0 := by dsimp [algebraMap, Algebra.algebraMap, Matrix.scalar] split_ifs with h <;> simp [h, Matrix.one_apply_ne] theorem algebraMap_eq_diagonal (r : R) : algebraMap R (Matrix n n α) r = diagonal (algebraMap R (n → α) r) := rfl theorem algebraMap_eq_diagonalRingHom : algebraMap R (Matrix n n α) = (diagonalRingHom n α).comp (algebraMap R _) := rfl @[simp] theorem map_algebraMap (r : R) (f : α → β) (hf : f 0 = 0) (hf₂ : f (algebraMap R α r) = algebraMap R β r) : (algebraMap R (Matrix n n α) r).map f = algebraMap R (Matrix n n β) r := by rw [algebraMap_eq_diagonal, algebraMap_eq_diagonal, diagonal_map hf] simp [hf₂] variable (R) /-- `Matrix.diagonal` as an `AlgHom`. -/ @[simps] def diagonalAlgHom : (n → α) →ₐ[R] Matrix n n α := { diagonalRingHom n α with toFun := diagonal commutes' := fun r => (algebraMap_eq_diagonal r).symm } end Algebra section AddHom variable [Add α] variable (R α) in /-- Extracting entries from a matrix as an additive homomorphism. -/ @[simps] def entryAddHom (i : m) (j : n) : AddHom (Matrix m n α) α where toFun M := M i j map_add' _ _ := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryAddHom_eq_comp {i : m} {j : n} : entryAddHom α i j = ((Pi.evalAddHom (fun _ => α) j).comp (Pi.evalAddHom _ i)).comp (AddHomClass.toAddHom ofAddEquiv.symm) := rfl end AddHom section AddMonoidHom variable [AddZeroClass α] variable (R α) in /-- Extracting entries from a matrix as an additive monoid homomorphism. Note this cannot be upgraded to a ring homomorphism, as it does not respect multiplication. -/ @[simps] def entryAddMonoidHom (i : m) (j : n) : Matrix m n α →+ α where toFun M := M i j map_add' _ _ := rfl map_zero' := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryAddMonoidHom_eq_comp {i : m} {j : n} : entryAddMonoidHom α i j = ((Pi.evalAddMonoidHom (fun _ => α) j).comp (Pi.evalAddMonoidHom _ i)).comp (AddMonoidHomClass.toAddMonoidHom ofAddEquiv.symm) := by rfl @[simp] lemma evalAddMonoidHom_comp_diagAddMonoidHom (i : m) : (Pi.evalAddMonoidHom _ i).comp (diagAddMonoidHom m α) = entryAddMonoidHom α i i := by simp [AddMonoidHom.ext_iff] @[simp] lemma entryAddMonoidHom_toAddHom {i : m} {j : n} : (entryAddMonoidHom α i j : AddHom _ _) = entryAddHom α i j := rfl end AddMonoidHom section LinearMap variable [Semiring R] [AddCommMonoid α] [Module R α] variable (R α) in /-- Extracting entries from a matrix as a linear map. Note this cannot be upgraded to an algebra homomorphism, as it does not respect multiplication. -/ @[simps] def entryLinearMap (i : m) (j : n) : Matrix m n α →ₗ[R] α where toFun M := M i j map_add' _ _ := rfl map_smul' _ _ := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryLinearMap_eq_comp {i : m} {j : n} : entryLinearMap R α i j = LinearMap.proj j ∘ₗ LinearMap.proj i ∘ₗ (ofLinearEquiv R).symm.toLinearMap := by rfl @[simp] lemma proj_comp_diagLinearMap (i : m) : LinearMap.proj i ∘ₗ diagLinearMap m R α = entryLinearMap R α i i := by simp [LinearMap.ext_iff] @[simp] lemma entryLinearMap_toAddMonoidHom {i : m} {j : n} : (entryLinearMap R α i j : _ →+ _) = entryAddMonoidHom α i j := rfl @[simp] lemma entryLinearMap_toAddHom {i : m} {j : n} : (entryLinearMap R α i j : AddHom _ _) = entryAddHom α i j := rfl end LinearMap end Matrix /-! ### Bundled versions of `Matrix.map` -/ namespace Equiv /-- The `Equiv` between spaces of matrices induced by an `Equiv` between their coefficients. This is `Matrix.map` as an `Equiv`. -/ @[simps apply] def mapMatrix (f : α ≃ β) : Matrix m n α ≃ Matrix m n β where toFun M := M.map f invFun M := M.map f.symm left_inv _ := Matrix.ext fun _ _ => f.symm_apply_apply _ right_inv _ := Matrix.ext fun _ _ => f.apply_symm_apply _ @[simp] theorem mapMatrix_refl : (Equiv.refl α).mapMatrix = Equiv.refl (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃ _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ β) (g : β ≃ γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃ _) := rfl end Equiv namespace AddMonoidHom variable [AddZeroClass α] [AddZeroClass β] [AddZeroClass γ] /-- The `AddMonoidHom` between spaces of matrices induced by an `AddMonoidHom` between their coefficients. This is `Matrix.map` as an `AddMonoidHom`. -/ @[simps] def mapMatrix (f : α →+ β) : Matrix m n α →+ Matrix m n β where toFun M := M.map f map_zero' := Matrix.map_zero f f.map_zero map_add' := Matrix.map_add f f.map_add @[simp] theorem mapMatrix_id : (AddMonoidHom.id α).mapMatrix = AddMonoidHom.id (Matrix m n α) := rfl @[simp] theorem mapMatrix_comp (f : β →+ γ) (g : α →+ β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m n α →+ _) := rfl @[simp] lemma entryAddMonoidHom_comp_mapMatrix (f : α →+ β) (i : m) (j : n) : (entryAddMonoidHom β i j).comp f.mapMatrix = f.comp (entryAddMonoidHom α i j) := rfl end AddMonoidHom namespace AddEquiv variable [Add α] [Add β] [Add γ] /-- The `AddEquiv` between spaces of matrices induced by an `AddEquiv` between their coefficients. This is `Matrix.map` as an `AddEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃+ β) : Matrix m n α ≃+ Matrix m n β := { f.toEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm map_add' := Matrix.map_add f (map_add f) } @[simp] theorem mapMatrix_refl : (AddEquiv.refl α).mapMatrix = AddEquiv.refl (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃+ β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃+ _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃+ β) (g : β ≃+ γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃+ _) := rfl @[simp] lemma entryAddHom_comp_mapMatrix (f : α ≃+ β) (i : m) (j : n) : (entryAddHom β i j).comp (AddHomClass.toAddHom f.mapMatrix) = (f : AddHom α β).comp (entryAddHom _ i j) := rfl end AddEquiv namespace LinearMap variable [Semiring R] [AddCommMonoid α] [AddCommMonoid β] [AddCommMonoid γ] variable [Module R α] [Module R β] [Module R γ] /-- The `LinearMap` between spaces of matrices induced by a `LinearMap` between their coefficients. This is `Matrix.map` as a `LinearMap`. -/ @[simps] def mapMatrix (f : α →ₗ[R] β) : Matrix m n α →ₗ[R] Matrix m n β where toFun M := M.map f map_add' := Matrix.map_add f f.map_add map_smul' r := Matrix.map_smul f r (f.map_smul r) @[simp] theorem mapMatrix_id : LinearMap.id.mapMatrix = (LinearMap.id : Matrix m n α →ₗ[R] _) := rfl @[simp] theorem mapMatrix_comp (f : β →ₗ[R] γ) (g : α →ₗ[R] β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m n α →ₗ[R] _) := rfl @[simp] lemma entryLinearMap_comp_mapMatrix (f : α →ₗ[R] β) (i : m) (j : n) : entryLinearMap R _ i j ∘ₗ f.mapMatrix = f ∘ₗ entryLinearMap R _ i j := rfl end LinearMap namespace LinearEquiv variable [Semiring R] [AddCommMonoid α] [AddCommMonoid β] [AddCommMonoid γ] variable [Module R α] [Module R β] [Module R γ] /-- The `LinearEquiv` between spaces of matrices induced by a `LinearEquiv` between their coefficients. This is `Matrix.map` as a `LinearEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃ₗ[R] β) : Matrix m n α ≃ₗ[R] Matrix m n β := { f.toEquiv.mapMatrix, f.toLinearMap.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : (LinearEquiv.refl R α).mapMatrix = LinearEquiv.refl R (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ₗ[R] β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃ₗ[R] _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ₗ[R] β) (g : β ≃ₗ[R] γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃ₗ[R] _) := rfl @[simp] lemma mapMatrix_toLinearMap (f : α ≃ₗ[R] β) : (f.mapMatrix : _ ≃ₗ[R] Matrix m n β).toLinearMap = f.toLinearMap.mapMatrix := by rfl @[simp] lemma entryLinearMap_comp_mapMatrix (f : α ≃ₗ[R] β) (i : m) (j : n) : entryLinearMap R _ i j ∘ₗ f.mapMatrix.toLinearMap = f.toLinearMap ∘ₗ entryLinearMap R _ i j := by simp only [mapMatrix_toLinearMap, LinearMap.entryLinearMap_comp_mapMatrix] end LinearEquiv namespace RingHom variable [Fintype m] [DecidableEq m] variable [NonAssocSemiring α] [NonAssocSemiring β] [NonAssocSemiring γ] /-- The `RingHom` between spaces of square matrices induced by a `RingHom` between their coefficients. This is `Matrix.map` as a `RingHom`. -/ @[simps] def mapMatrix (f : α →+* β) : Matrix m m α →+* Matrix m m β := { f.toAddMonoidHom.mapMatrix with toFun := fun M => M.map f map_one' := by simp map_mul' := fun _ _ => Matrix.map_mul } @[simp] theorem mapMatrix_id : (RingHom.id α).mapMatrix = RingHom.id (Matrix m m α) := rfl @[simp] theorem mapMatrix_comp (f : β →+* γ) (g : α →+* β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m m α →+* _) := rfl end RingHom namespace RingEquiv variable [Fintype m] [DecidableEq m] variable [NonAssocSemiring α] [NonAssocSemiring β] [NonAssocSemiring γ] /-- The `RingEquiv` between spaces of square matrices induced by a `RingEquiv` between their coefficients. This is `Matrix.map` as a `RingEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃+* β) : Matrix m m α ≃+* Matrix m m β := { f.toRingHom.mapMatrix, f.toAddEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : (RingEquiv.refl α).mapMatrix = RingEquiv.refl (Matrix m m α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃+* β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m m β ≃+* _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃+* β) (g : β ≃+* γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m m α ≃+* _) := rfl open MulOpposite in /-- For any ring `R`, we have ring isomorphism `Matₙₓₙ(Rᵒᵖ) ≅ (Matₙₓₙ(R))ᵒᵖ` given by transpose. -/ @[simps apply symm_apply] def mopMatrix : Matrix m m αᵐᵒᵖ ≃+* (Matrix m m α)ᵐᵒᵖ where toFun M := op (M.transpose.map unop) invFun M := M.unop.transpose.map op left_inv _ := by aesop right_inv _ := by aesop map_mul' _ _ := unop_injective <| by ext; simp [transpose, mul_apply] map_add' _ _ := by aesop end RingEquiv namespace AlgHom variable [Fintype m] [DecidableEq m] variable [CommSemiring R] [Semiring α] [Semiring β] [Semiring γ] variable [Algebra R α] [Algebra R β] [Algebra R γ] /-- The `AlgHom` between spaces of square matrices induced by an `AlgHom` between their coefficients. This is `Matrix.map` as an `AlgHom`. -/ @[simps] def mapMatrix (f : α →ₐ[R] β) : Matrix m m α →ₐ[R] Matrix m m β := { f.toRingHom.mapMatrix with toFun := fun M => M.map f commutes' := fun r => Matrix.map_algebraMap r f (map_zero _) (f.commutes r) } @[simp] theorem mapMatrix_id : (AlgHom.id R α).mapMatrix = AlgHom.id R (Matrix m m α) := rfl @[simp] theorem mapMatrix_comp (f : β →ₐ[R] γ) (g : α →ₐ[R] β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m m α →ₐ[R] _) := rfl end AlgHom namespace AlgEquiv variable [Fintype m] [DecidableEq m] variable [CommSemiring R] [Semiring α] [Semiring β] [Semiring γ] variable [Algebra R α] [Algebra R β] [Algebra R γ] /-- The `AlgEquiv` between spaces of square matrices induced by an `AlgEquiv` between their coefficients. This is `Matrix.map` as an `AlgEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃ₐ[R] β) : Matrix m m α ≃ₐ[R] Matrix m m β := { f.toAlgHom.mapMatrix, f.toRingEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : AlgEquiv.refl.mapMatrix = (AlgEquiv.refl : Matrix m m α ≃ₐ[R] _) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ₐ[R] β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m m β ≃ₐ[R] _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ₐ[R] β) (g : β ≃ₐ[R] γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m m α ≃ₐ[R] _) := rfl /-- For any algebra `α` over a ring `R`, we have an `R`-algebra isomorphism `Matₙₓₙ(αᵒᵖ) ≅ (Matₙₓₙ(R))ᵒᵖ` given by transpose. If `α` is commutative, we can get rid of the `ᵒᵖ` in the left-hand side, see `Matrix.transposeAlgEquiv`. -/ @[simps!] def mopMatrix : Matrix m m αᵐᵒᵖ ≃ₐ[R] (Matrix m m α)ᵐᵒᵖ where __ := RingEquiv.mopMatrix commutes' _ := MulOpposite.unop_injective <| by ext; simp [algebraMap_matrix_apply, eq_comm, apply_ite MulOpposite.unop] end AlgEquiv open Matrix namespace Matrix section Transpose open Matrix variable (m n α) /-- `Matrix.transpose` as an `AddEquiv` -/ @[simps apply] def transposeAddEquiv [Add α] : Matrix m n α ≃+ Matrix n m α where toFun := transpose invFun := transpose left_inv := transpose_transpose right_inv := transpose_transpose map_add' := transpose_add @[simp] theorem transposeAddEquiv_symm [Add α] : (transposeAddEquiv m n α).symm = transposeAddEquiv n m α := rfl variable {m n α} theorem transpose_list_sum [AddMonoid α] (l : List (Matrix m n α)) : l.sumᵀ = (l.map transpose).sum := map_list_sum (transposeAddEquiv m n α) l theorem transpose_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix m n α)) : s.sumᵀ = (s.map transpose).sum := (transposeAddEquiv m n α).toAddMonoidHom.map_multiset_sum s theorem transpose_sum [AddCommMonoid α] {ι : Type*} (s : Finset ι) (M : ι → Matrix m n α) : (∑ i ∈ s, M i)ᵀ = ∑ i ∈ s, (M i)ᵀ := map_sum (transposeAddEquiv m n α) _ s variable (m n R α) /-- `Matrix.transpose` as a `LinearMap` -/ @[simps apply] def transposeLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : Matrix m n α ≃ₗ[R] Matrix n m α := { transposeAddEquiv m n α with map_smul' := transpose_smul } @[simp] theorem transposeLinearEquiv_symm [Semiring R] [AddCommMonoid α] [Module R α] : (transposeLinearEquiv m n R α).symm = transposeLinearEquiv n m R α := rfl variable {m n R α} variable (m α) /-- `Matrix.transpose` as a `RingEquiv` to the opposite ring -/ @[simps] def transposeRingEquiv [AddCommMonoid α] [CommSemigroup α] [Fintype m] : Matrix m m α ≃+* (Matrix m m α)ᵐᵒᵖ := { (transposeAddEquiv m m α).trans MulOpposite.opAddEquiv with toFun := fun M => MulOpposite.op Mᵀ invFun := fun M => M.unopᵀ map_mul' := fun M N => (congr_arg MulOpposite.op (transpose_mul M N)).trans (MulOpposite.op_mul _ _) left_inv := fun M => transpose_transpose M right_inv := fun M => MulOpposite.unop_injective <| transpose_transpose M.unop } variable {m α} @[simp] theorem transpose_pow [CommSemiring α] [Fintype m] [DecidableEq m] (M : Matrix m m α) (k : ℕ) : (M ^ k)ᵀ = Mᵀ ^ k := MulOpposite.op_injective <| map_pow (transposeRingEquiv m α) M k theorem transpose_list_prod [CommSemiring α] [Fintype m] [DecidableEq m] (l : List (Matrix m m α)) : l.prodᵀ = (l.map transpose).reverse.prod := (transposeRingEquiv m α).unop_map_list_prod l variable (R m α) /-- `Matrix.transpose` as an `AlgEquiv` to the opposite ring -/ @[simps] def transposeAlgEquiv [CommSemiring R] [CommSemiring α] [Fintype m] [DecidableEq m] [Algebra R α] : Matrix m m α ≃ₐ[R] (Matrix m m α)ᵐᵒᵖ := { (transposeAddEquiv m m α).trans MulOpposite.opAddEquiv, transposeRingEquiv m α with toFun := fun M => MulOpposite.op Mᵀ commutes' := fun r => by simp only [algebraMap_eq_diagonal, diagonal_transpose, MulOpposite.algebraMap_apply] } variable {R m α} end Transpose end Matrix
Mathlib/Data/Matrix/Basic.lean
1,901
1,902
/- 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, Johan Commelin, Amelia Livingston, Anne Baanen -/ import Mathlib.GroupTheory.MonoidLocalization.Away import Mathlib.RingTheory.Ideal.Quotient.Operations import Mathlib.RingTheory.Localization.Defs import Mathlib.Algebra.Algebra.Tower /-! # Ideals in localizations of commutative rings ## Implementation notes See `Mathlib/RingTheory/Localization/Basic.lean` for a design overview. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ namespace IsLocalization section CommSemiring variable {R : Type*} [CommSemiring R] (M : Submonoid R) (S : Type*) [CommSemiring S] variable [Algebra R S] [IsLocalization M S] variable {M S} in theorem mk'_mem_iff {x} {y : M} {I : Ideal S} : mk' S x y ∈ I ↔ algebraMap R S x ∈ I := by constructor <;> intro h · rw [← mk'_spec S x y, mul_comm] exact I.mul_mem_left ((algebraMap R S) y) h · rw [← mk'_spec S x y] at h obtain ⟨b, hb⟩ := isUnit_iff_exists_inv.1 (map_units S y) have := I.mul_mem_left b h rwa [mul_comm, mul_assoc, hb, mul_one] at this /-- Explicit characterization of the ideal given by `Ideal.map (algebraMap R S) I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_algebraMap_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ -- TODO: golf this using `Submodule.localized'` private def map_ideal (I : Ideal R) : Ideal S where carrier := { z : S | ∃ x : I × M, z * algebraMap R S x.2 = algebraMap R S x.1 } zero_mem' := ⟨⟨0, 1⟩, by simp⟩ add_mem' := by rintro a b ⟨a', ha⟩ ⟨b', hb⟩ let Z : { x // x ∈ I } := ⟨(a'.2 : R) * (b'.1 : R) + (b'.2 : R) * (a'.1 : R), I.add_mem (I.mul_mem_left _ b'.1.2) (I.mul_mem_left _ a'.1.2)⟩ use ⟨Z, a'.2 * b'.2⟩ simp only [Z, RingHom.map_add, Submodule.coe_mk, Submonoid.coe_mul, RingHom.map_mul] rw [add_mul, ← mul_assoc a, ha, mul_comm (algebraMap R S a'.2) (algebraMap R S b'.2), ← mul_assoc b, hb] ring smul_mem' := by rintro c x ⟨x', hx⟩ obtain ⟨c', hc⟩ := IsLocalization.surj M c let Z : { x // x ∈ I } := ⟨c'.1 * x'.1, I.mul_mem_left c'.1 x'.1.2⟩ use ⟨Z, c'.2 * x'.2⟩ simp only [Z, ← hx, ← hc, smul_eq_mul, Submodule.coe_mk, Submonoid.coe_mul, RingHom.map_mul] ring theorem mem_map_algebraMap_iff {I : Ideal R} {z} : z ∈ Ideal.map (algebraMap R S) I ↔ ∃ x : I × M, z * algebraMap R S x.2 = algebraMap R S x.1 := by constructor · change _ → z ∈ map_ideal M S I refine fun h => Ideal.mem_sInf.1 h fun z hz => ?_ obtain ⟨y, hy⟩ := hz let Z : { x // x ∈ I } := ⟨y, hy.left⟩ use ⟨Z, 1⟩ simp [Z, hy.right] · rintro ⟨⟨a, s⟩, h⟩ rw [← Ideal.unit_mul_mem_iff_mem _ (map_units S s), mul_comm] exact h.symm ▸ Ideal.mem_map_of_mem _ a.2 lemma mk'_mem_map_algebraMap_iff (I : Ideal R) (x : R) (s : M) : IsLocalization.mk' S x s ∈ I.map (algebraMap R S) ↔ ∃ s ∈ M, s * x ∈ I := by rw [← Ideal.unit_mul_mem_iff_mem _ (IsLocalization.map_units S s), IsLocalization.mk'_spec', IsLocalization.mem_map_algebraMap_iff M] simp_rw [← map_mul, IsLocalization.eq_iff_exists M, mul_comm x, ← mul_assoc, ← Submonoid.coe_mul] exact ⟨fun ⟨⟨y, t⟩, c, h⟩ ↦ ⟨_, (c * t).2, h ▸ I.mul_mem_left c.1 y.2⟩, fun ⟨s, hs, h⟩ ↦ ⟨⟨⟨_, h⟩, ⟨s, hs⟩⟩, 1, by simp⟩⟩ lemma algebraMap_mem_map_algebraMap_iff (I : Ideal R) (x : R) : algebraMap R S x ∈ I.map (algebraMap R S) ↔ ∃ m ∈ M, m * x ∈ I := by rw [← IsLocalization.mk'_one (M := M), mk'_mem_map_algebraMap_iff] lemma map_algebraMap_ne_top_iff_disjoint (I : Ideal R) : I.map (algebraMap R S) ≠ ⊤ ↔ Disjoint (M : Set R) (I : Set R) := by simp only [ne_eq, Ideal.eq_top_iff_one, ← map_one (algebraMap R S), not_iff_comm, IsLocalization.algebraMap_mem_map_algebraMap_iff M] simp [Set.disjoint_left] include M in theorem map_comap (J : Ideal S) : Ideal.map (algebraMap R S) (Ideal.comap (algebraMap R S) J) = J := le_antisymm (Ideal.map_le_iff_le_comap.2 le_rfl) fun x hJ => by obtain ⟨r, s, hx⟩ := mk'_surjective M x rw [← hx] at hJ ⊢ exact Ideal.mul_mem_right _ _ (Ideal.mem_map_of_mem _ (show (algebraMap R S) r ∈ J from mk'_spec S r s ▸ J.mul_mem_right ((algebraMap R S) s) hJ)) theorem comap_map_of_isPrime_disjoint (I : Ideal R) (hI : I.IsPrime) (hM : Disjoint (M : Set R) I) :
Ideal.comap (algebraMap R S) (Ideal.map (algebraMap R S) I) = I := by refine le_antisymm ?_ Ideal.le_comap_map refine (fun a ha => ?_) obtain ⟨⟨b, s⟩, h⟩ := (mem_map_algebraMap_iff M S).1 (Ideal.mem_comap.1 ha) replace h : algebraMap R S (s * a) = algebraMap R S b := by simpa only [← map_mul, mul_comm] using h obtain ⟨c, hc⟩ := (eq_iff_exists M S).1 h have : ↑c * ↑s * a ∈ I := by rw [mul_assoc, hc] exact I.mul_mem_left c b.2 exact (hI.mem_or_mem this).resolve_left fun hsc => hM.le_bot ⟨(c * s).2, hsc⟩ /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def orderEmbedding : Ideal S ↪o Ideal R where toFun J := Ideal.comap (algebraMap R S) J inj' := Function.LeftInverse.injective (map_comap M S) map_rel_iff' := by rintro J₁ J₂ constructor · exact fun hJ => (map_comap M S) J₁ ▸ (map_comap M S) J₂ ▸ Ideal.map_mono hJ · exact fun hJ => Ideal.comap_mono hJ /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`.
Mathlib/RingTheory/Localization/Ideal.lean
108
132
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import Mathlib.Control.Basic import Mathlib.Data.Nat.Basic import Mathlib.Data.Option.Basic import Mathlib.Data.List.Defs import Mathlib.Data.List.Monad import Mathlib.Logic.OpClass import Mathlib.Logic.Unique import Mathlib.Order.Basic import Mathlib.Tactic.Common /-! # Basic properties of lists -/ assert_not_exists GroupWithZero assert_not_exists Lattice assert_not_exists Prod.swap_eq_iff_eq_swap assert_not_exists Ring assert_not_exists Set.range open Function open Nat hiding one_pos namespace List universe u v w variable {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {l₁ l₂ : List α} /-- There is only one list of an empty type -/ instance uniqueOfIsEmpty [IsEmpty α] : Unique (List α) := { instInhabitedList with uniq := fun l => match l with | [] => rfl | a :: _ => isEmptyElim a } instance : Std.LawfulIdentity (α := List α) Append.append [] where left_id := nil_append right_id := append_nil instance : Std.Associative (α := List α) Append.append where assoc := append_assoc @[simp] theorem cons_injective {a : α} : Injective (cons a) := fun _ _ => tail_eq_of_cons_eq theorem singleton_injective : Injective fun a : α => [a] := fun _ _ h => (cons_eq_cons.1 h).1 theorem set_of_mem_cons (l : List α) (a : α) : { x | x ∈ a :: l } = insert a { x | x ∈ l } := Set.ext fun _ => mem_cons /-! ### mem -/ theorem _root_.Decidable.List.eq_or_ne_mem_of_mem [DecidableEq α] {a b : α} {l : List α} (h : a ∈ b :: l) : a = b ∨ a ≠ b ∧ a ∈ l := by by_cases hab : a = b · exact Or.inl hab · exact ((List.mem_cons.1 h).elim Or.inl (fun h => Or.inr ⟨hab, h⟩)) lemma mem_pair {a b c : α} : a ∈ [b, c] ↔ a = b ∨ a = c := by rw [mem_cons, mem_singleton] -- The simpNF linter says that the LHS can be simplified via `List.mem_map`. -- However this is a higher priority lemma. -- It seems the side condition `hf` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Injective f) {a : α} {l : List α} : f a ∈ map f l ↔ a ∈ l := ⟨fun m => let ⟨_, m', e⟩ := exists_of_mem_map m; H e ▸ m', mem_map_of_mem⟩ @[simp] theorem _root_.Function.Involutive.exists_mem_and_apply_eq_iff {f : α → α} (hf : Function.Involutive f) (x : α) (l : List α) : (∃ y : α, y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by rintro ⟨y, h, rfl⟩; rwa [hf y], fun h => ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : α → α} (hf : Involutive f) {a : α} {l : List α} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] /-! ### length -/ alias ⟨_, length_pos_of_ne_nil⟩ := length_pos_iff theorem length_pos_iff_ne_nil {l : List α} : 0 < length l ↔ l ≠ [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ theorem exists_of_length_succ {n} : ∀ l : List α, l.length = n + 1 → ∃ h t, l = h :: t | [], H => absurd H.symm <| succ_ne_zero n | h :: t, _ => ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : Injective (List.length : List α → ℕ) ↔ Subsingleton α := by constructor · intro h; refine ⟨fun x y => ?_⟩; (suffices [x] = [y] by simpa using this); apply h; rfl · intros hα l1 l2 hl induction l1 generalizing l2 <;> cases l2 · rfl · cases hl · cases hl · next ih _ _ => congr · subsingleton · apply ih; simpa using hl @[simp default+1] -- Raise priority above `length_injective_iff`. lemma length_injective [Subsingleton α] : Injective (length : List α → ℕ) := length_injective_iff.mpr inferInstance theorem length_eq_two {l : List α} : l.length = 2 ↔ ∃ a b, l = [a, b] := ⟨fun _ => let [a, b] := l; ⟨a, b, rfl⟩, fun ⟨_, _, e⟩ => e ▸ rfl⟩ theorem length_eq_three {l : List α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] := ⟨fun _ => let [a, b, c] := l; ⟨a, b, c, rfl⟩, fun ⟨_, _, _, e⟩ => e ▸ rfl⟩ /-! ### set-theoretic notation of lists -/ instance instSingletonList : Singleton α (List α) := ⟨fun x => [x]⟩ instance [DecidableEq α] : Insert α (List α) := ⟨List.insert⟩ instance [DecidableEq α] : LawfulSingleton α (List α) := { insert_empty_eq := fun x => show (if x ∈ ([] : List α) then [] else [x]) = [x] from if_neg not_mem_nil } theorem singleton_eq (x : α) : ({x} : List α) = [x] := rfl theorem insert_neg [DecidableEq α] {x : α} {l : List α} (h : x ∉ l) : Insert.insert x l = x :: l := insert_of_not_mem h theorem insert_pos [DecidableEq α] {x : α} {l : List α} (h : x ∈ l) : Insert.insert x l = l := insert_of_mem h theorem doubleton_eq [DecidableEq α] {x y : α} (h : x ≠ y) : ({x, y} : List α) = [x, y] := by rw [insert_neg, singleton_eq] rwa [singleton_eq, mem_singleton] /-! ### bounded quantifiers over lists -/ theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : List α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : List α) (h : p a) : ∃ x ∈ a :: l, p x := ⟨a, mem_cons_self, h⟩ theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ l, p x) → ∃ x ∈ a :: l, p x := fun ⟨x, xl, px⟩ => ⟨x, mem_cons_of_mem _ xl, px⟩ theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ a :: l, p x) → p a ∨ ∃ x ∈ l, p x := fun ⟨x, xal, px⟩ => Or.elim (eq_or_mem_of_mem_cons xal) (fun h : x = a => by rw [← h]; left; exact px) fun h : x ∈ l => Or.inr ⟨x, h, px⟩ theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : List α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := Iff.intro or_exists_of_exists_mem_cons fun h => Or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists /-! ### list subset -/ theorem cons_subset_of_subset_of_mem {a : α} {l m : List α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ l₂ l : List α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := fun _ h ↦ (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem map_subset_iff {l₁ l₂ : List α} (f : α → β) (h : Injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := by refine ⟨?_, map_subset f⟩; intro h2 x hx rcases mem_map.1 (h2 (mem_map_of_mem hx)) with ⟨x', hx', hxx'⟩ cases h hxx'; exact hx' /-! ### append -/ theorem append_eq_has_append {L₁ L₂ : List α} : List.append L₁ L₂ = L₁ ++ L₂ := rfl theorem append_right_injective (s : List α) : Injective fun t ↦ s ++ t := fun _ _ ↦ append_cancel_left theorem append_left_injective (t : List α) : Injective fun s ↦ s ++ t := fun _ _ ↦ append_cancel_right /-! ### replicate -/ theorem eq_replicate_length {a : α} : ∀ {l : List α}, l = replicate l.length a ↔ ∀ b ∈ l, b = a | [] => by simp | (b :: l) => by simp [eq_replicate_length, replicate_succ] theorem replicate_add (m n) (a : α) : replicate (m + n) a = replicate m a ++ replicate n a := by rw [replicate_append_replicate] theorem replicate_subset_singleton (n) (a : α) : replicate n a ⊆ [a] := fun _ h => mem_singleton.2 (eq_of_mem_replicate h) theorem subset_singleton_iff {a : α} {L : List α} : L ⊆ [a] ↔ ∃ n, L = replicate n a := by simp only [eq_replicate_iff, subset_def, mem_singleton, exists_eq_left'] theorem replicate_right_injective {n : ℕ} (hn : n ≠ 0) : Injective (@replicate α n) := fun _ _ h => (eq_replicate_iff.1 h).2 _ <| mem_replicate.2 ⟨hn, rfl⟩ theorem replicate_right_inj {a b : α} {n : ℕ} (hn : n ≠ 0) : replicate n a = replicate n b ↔ a = b := (replicate_right_injective hn).eq_iff theorem replicate_right_inj' {a b : α} : ∀ {n}, replicate n a = replicate n b ↔ n = 0 ∨ a = b | 0 => by simp | n + 1 => (replicate_right_inj n.succ_ne_zero).trans <| by simp only [n.succ_ne_zero, false_or] theorem replicate_left_injective (a : α) : Injective (replicate · a) := LeftInverse.injective (length_replicate (n := ·)) theorem replicate_left_inj {a : α} {n m : ℕ} : replicate n a = replicate m a ↔ n = m := (replicate_left_injective a).eq_iff @[simp] theorem head?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.head? = l.head? := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h induction l <;> simp [replicate] @[simp] theorem getLast?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.getLast? = l.getLast? := by rw [← List.head?_reverse, ← List.head?_reverse, List.reverse_flatten, List.map_replicate, List.reverse_replicate, head?_flatten_replicate h] /-! ### pure -/ theorem mem_pure (x y : α) : x ∈ (pure y : List α) ↔ x = y := by simp /-! ### bind -/ @[simp] theorem bind_eq_flatMap {α β} (f : α → List β) (l : List α) : l >>= f = l.flatMap f := rfl /-! ### concat -/ /-! ### reverse -/ theorem reverse_cons' (a : α) (l : List α) : reverse (a :: l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] theorem reverse_concat' (l : List α) (a : α) : (l ++ [a]).reverse = a :: l.reverse := by rw [reverse_append]; rfl @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_involutive : Involutive (@reverse α) := reverse_reverse @[simp] theorem reverse_injective : Injective (@reverse α) := reverse_involutive.injective theorem reverse_surjective : Surjective (@reverse α) := reverse_involutive.surjective theorem reverse_bijective : Bijective (@reverse α) := reverse_involutive.bijective theorem concat_eq_reverse_cons (a : α) (l : List α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] theorem map_reverseAux (f : α → β) (l₁ l₂ : List α) : map f (reverseAux l₁ l₂) = reverseAux (map f l₁) (map f l₂) := by simp only [reverseAux_eq, map_append, map_reverse] -- TODO: Rename `List.reverse_perm` to `List.reverse_perm_self` @[simp] lemma reverse_perm' : l₁.reverse ~ l₂ ↔ l₁ ~ l₂ where mp := l₁.reverse_perm.symm.trans mpr := l₁.reverse_perm.trans @[simp] lemma perm_reverse : l₁ ~ l₂.reverse ↔ l₁ ~ l₂ where mp hl := hl.trans l₂.reverse_perm mpr hl := hl.trans l₂.reverse_perm.symm /-! ### getLast -/ attribute [simp] getLast_cons theorem getLast_append_singleton {a : α} (l : List α) : getLast (l ++ [a]) (append_ne_nil_of_right_ne_nil l (cons_ne_nil a _)) = a := by simp [getLast_append] theorem getLast_append_of_right_ne_nil (l₁ l₂ : List α) (h : l₂ ≠ []) : getLast (l₁ ++ l₂) (append_ne_nil_of_right_ne_nil l₁ h) = getLast l₂ h := by induction l₁ with | nil => simp | cons _ _ ih => simp only [cons_append]; rw [List.getLast_cons]; exact ih @[deprecated (since := "2025-02-06")] alias getLast_append' := getLast_append_of_right_ne_nil theorem getLast_concat' {a : α} (l : List α) : getLast (concat l a) (by simp) = a := by simp @[simp] theorem getLast_singleton' (a : α) : getLast [a] (cons_ne_nil a []) = a := rfl @[simp] theorem getLast_cons_cons (a₁ a₂ : α) (l : List α) : getLast (a₁ :: a₂ :: l) (cons_ne_nil _ _) = getLast (a₂ :: l) (cons_ne_nil a₂ l) := rfl theorem dropLast_append_getLast : ∀ {l : List α} (h : l ≠ []), dropLast l ++ [getLast l h] = l | [], h => absurd rfl h | [_], _ => rfl | a :: b :: l, h => by rw [dropLast_cons₂, cons_append, getLast_cons (cons_ne_nil _ _)] congr exact dropLast_append_getLast (cons_ne_nil b l) theorem getLast_congr {l₁ l₂ : List α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : getLast l₁ h₁ = getLast l₂ h₂ := by subst l₁; rfl theorem getLast_replicate_succ (m : ℕ) (a : α) : (replicate (m + 1) a).getLast (ne_nil_of_length_eq_add_one length_replicate) = a := by simp only [replicate_succ'] exact getLast_append_singleton _ @[deprecated (since := "2025-02-07")] alias getLast_filter' := getLast_filter_of_pos /-! ### getLast? -/ theorem mem_getLast?_eq_getLast : ∀ {l : List α} {x : α}, x ∈ l.getLast? → ∃ h, x = getLast l h | [], x, hx => False.elim <| by simp at hx | [a], x, hx => have : a = x := by simpa using hx this ▸ ⟨cons_ne_nil a [], rfl⟩ | a :: b :: l, x, hx => by rw [getLast?_cons_cons] at hx rcases mem_getLast?_eq_getLast hx with ⟨_, h₂⟩ use cons_ne_nil _ _ assumption theorem getLast?_eq_getLast_of_ne_nil : ∀ {l : List α} (h : l ≠ []), l.getLast? = some (l.getLast h) | [], h => (h rfl).elim | [_], _ => rfl | _ :: b :: l, _ => @getLast?_eq_getLast_of_ne_nil (b :: l) (cons_ne_nil _ _) theorem mem_getLast?_cons {x y : α} : ∀ {l : List α}, x ∈ l.getLast? → x ∈ (y :: l).getLast? | [], _ => by contradiction | _ :: _, h => h theorem dropLast_append_getLast? : ∀ {l : List α}, ∀ a ∈ l.getLast?, dropLast l ++ [a] = l | [], a, ha => (Option.not_mem_none a ha).elim | [a], _, rfl => rfl | a :: b :: l, c, hc => by rw [getLast?_cons_cons] at hc rw [dropLast_cons₂, cons_append, dropLast_append_getLast? _ hc] theorem getLastI_eq_getLast? [Inhabited α] : ∀ l : List α, l.getLastI = l.getLast?.iget | [] => by simp [getLastI, Inhabited.default] | [_] => rfl | [_, _] => rfl | [_, _, _] => rfl | _ :: _ :: c :: l => by simp [getLastI, getLastI_eq_getLast? (c :: l)] theorem getLast?_append_cons : ∀ (l₁ : List α) (a : α) (l₂ : List α), getLast? (l₁ ++ a :: l₂) = getLast? (a :: l₂) | [], _, _ => rfl | [_], _, _ => rfl | b :: c :: l₁, a, l₂ => by rw [cons_append, cons_append, getLast?_cons_cons, ← cons_append, getLast?_append_cons (c :: l₁)] theorem getLast?_append_of_ne_nil (l₁ : List α) : ∀ {l₂ : List α} (_ : l₂ ≠ []), getLast? (l₁ ++ l₂) = getLast? l₂ | [], hl₂ => by contradiction | b :: l₂, _ => getLast?_append_cons l₁ b l₂ theorem mem_getLast?_append_of_mem_getLast? {l₁ l₂ : List α} {x : α} (h : x ∈ l₂.getLast?) : x ∈ (l₁ ++ l₂).getLast? := by cases l₂ · contradiction · rw [List.getLast?_append_cons] exact h /-! ### head(!?) and tail -/ @[simp] theorem head!_nil [Inhabited α] : ([] : List α).head! = default := rfl @[simp] theorem head_cons_tail (x : List α) (h : x ≠ []) : x.head h :: x.tail = x := by cases x <;> simp at h ⊢ theorem head_eq_getElem_zero {l : List α} (hl : l ≠ []) : l.head hl = l[0]'(length_pos_iff.2 hl) := (getElem_zero _).symm theorem head!_eq_head? [Inhabited α] (l : List α) : head! l = (head? l).iget := by cases l <;> rfl theorem surjective_head! [Inhabited α] : Surjective (@head! α _) := fun x => ⟨[x], rfl⟩ theorem surjective_head? : Surjective (@head? α) := Option.forall.2 ⟨⟨[], rfl⟩, fun x => ⟨[x], rfl⟩⟩ theorem surjective_tail : Surjective (@tail α) | [] => ⟨[], rfl⟩ | a :: l => ⟨a :: a :: l, rfl⟩ theorem eq_cons_of_mem_head? {x : α} : ∀ {l : List α}, x ∈ l.head? → l = x :: tail l | [], h => (Option.not_mem_none _ h).elim | a :: l, h => by simp only [head?, Option.mem_def, Option.some_inj] at h exact h ▸ rfl @[simp] theorem head!_cons [Inhabited α] (a : α) (l : List α) : head! (a :: l) = a := rfl @[simp] theorem head!_append [Inhabited α] (t : List α) {s : List α} (h : s ≠ []) : head! (s ++ t) = head! s := by induction s · contradiction · rfl theorem mem_head?_append_of_mem_head? {s t : List α} {x : α} (h : x ∈ s.head?) : x ∈ (s ++ t).head? := by cases s · contradiction · exact h theorem head?_append_of_ne_nil : ∀ (l₁ : List α) {l₂ : List α} (_ : l₁ ≠ []), head? (l₁ ++ l₂) = head? l₁ | _ :: _, _, _ => rfl theorem tail_append_singleton_of_ne_nil {a : α} {l : List α} (h : l ≠ nil) : tail (l ++ [a]) = tail l ++ [a] := by induction l · contradiction · rw [tail, cons_append, tail] theorem cons_head?_tail : ∀ {l : List α} {a : α}, a ∈ head? l → a :: tail l = l | [], a, h => by contradiction | b :: l, a, h => by simp? at h says simp only [head?_cons, Option.mem_def, Option.some.injEq] at h simp [h] theorem head!_mem_head? [Inhabited α] : ∀ {l : List α}, l ≠ [] → head! l ∈ head? l | [], h => by contradiction | _ :: _, _ => rfl theorem cons_head!_tail [Inhabited α] {l : List α} (h : l ≠ []) : head! l :: tail l = l := cons_head?_tail (head!_mem_head? h) theorem head!_mem_self [Inhabited α] {l : List α} (h : l ≠ nil) : l.head! ∈ l := by have h' : l.head! ∈ l.head! :: l.tail := mem_cons_self rwa [cons_head!_tail h] at h' theorem get_eq_getElem? (l : List α) (i : Fin l.length) : l.get i = l[i]?.get (by simp [getElem?_eq_getElem]) := by simp @[deprecated (since := "2025-02-15")] alias get_eq_get? := get_eq_getElem? theorem exists_mem_iff_getElem {l : List α} {p : α → Prop} : (∃ x ∈ l, p x) ↔ ∃ (i : ℕ) (_ : i < l.length), p l[i] := by simp only [mem_iff_getElem] exact ⟨fun ⟨_x, ⟨i, hi, hix⟩, hxp⟩ ↦ ⟨i, hi, hix ▸ hxp⟩, fun ⟨i, hi, hp⟩ ↦ ⟨_, ⟨i, hi, rfl⟩, hp⟩⟩ theorem forall_mem_iff_getElem {l : List α} {p : α → Prop} : (∀ x ∈ l, p x) ↔ ∀ (i : ℕ) (_ : i < l.length), p l[i] := by simp [mem_iff_getElem, @forall_swap α] theorem get_tail (l : List α) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := (by simp only [length_tail] at h; omega)) : l.tail.get ⟨i, h⟩ = l.get ⟨i + 1, h'⟩ := by cases l <;> [cases h; rfl] /-! ### sublists -/ attribute [refl] List.Sublist.refl theorem Sublist.cons_cons {l₁ l₂ : List α} (a : α) (s : l₁ <+ l₂) : a :: l₁ <+ a :: l₂ := Sublist.cons₂ _ s lemma cons_sublist_cons' {a b : α} : a :: l₁ <+ b :: l₂ ↔ a :: l₁ <+ l₂ ∨ a = b ∧ l₁ <+ l₂ := by constructor · rintro (_ | _) · exact Or.inl ‹_› · exact Or.inr ⟨rfl, ‹_›⟩ · rintro (h | ⟨rfl, h⟩) · exact h.cons _ · rwa [cons_sublist_cons] theorem sublist_cons_of_sublist (a : α) (h : l₁ <+ l₂) : l₁ <+ a :: l₂ := h.cons _ @[deprecated (since := "2025-02-07")] alias sublist_nil_iff_eq_nil := sublist_nil @[simp] lemma sublist_singleton {l : List α} {a : α} : l <+ [a] ↔ l = [] ∨ l = [a] := by constructor <;> rintro (_ | _) <;> aesop theorem Sublist.antisymm (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := s₁.eq_of_length_le s₂.length_le /-- If the first element of two lists are different, then a sublist relation can be reduced. -/ theorem Sublist.of_cons_of_ne {a b} (h₁ : a ≠ b) (h₂ : a :: l₁ <+ b :: l₂) : a :: l₁ <+ l₂ := match h₁, h₂ with | _, .cons _ h => h /-! ### indexOf -/ section IndexOf variable [DecidableEq α] theorem idxOf_cons_eq {a b : α} (l : List α) : b = a → idxOf a (b :: l) = 0 | e => by rw [← e]; exact idxOf_cons_self @[deprecated (since := "2025-01-30")] alias indexOf_cons_eq := idxOf_cons_eq @[simp] theorem idxOf_cons_ne {a b : α} (l : List α) : b ≠ a → idxOf a (b :: l) = succ (idxOf a l) | h => by simp only [idxOf_cons, Bool.cond_eq_ite, beq_iff_eq, if_neg h] @[deprecated (since := "2025-01-30")] alias indexOf_cons_ne := idxOf_cons_ne theorem idxOf_eq_length_iff {a : α} {l : List α} : idxOf a l = length l ↔ a ∉ l := by induction l with | nil => exact iff_of_true rfl not_mem_nil | cons b l ih => simp only [length, mem_cons, idxOf_cons, eq_comm] rw [cond_eq_if] split_ifs with h <;> simp at h · exact iff_of_false (by rintro ⟨⟩) fun H => H <| Or.inl h.symm · simp only [Ne.symm h, false_or] rw [← ih] exact succ_inj @[simp] theorem idxOf_of_not_mem {l : List α} {a : α} : a ∉ l → idxOf a l = length l := idxOf_eq_length_iff.2 @[deprecated (since := "2025-01-30")] alias indexOf_of_not_mem := idxOf_of_not_mem theorem idxOf_le_length {a : α} {l : List α} : idxOf a l ≤ length l := by induction l with | nil => rfl | cons b l ih => ?_ simp only [length, idxOf_cons, cond_eq_if, beq_iff_eq] by_cases h : b = a · rw [if_pos h]; exact Nat.zero_le _ · rw [if_neg h]; exact succ_le_succ ih @[deprecated (since := "2025-01-30")] alias indexOf_le_length := idxOf_le_length theorem idxOf_lt_length_iff {a} {l : List α} : idxOf a l < length l ↔ a ∈ l := ⟨fun h => Decidable.byContradiction fun al => Nat.ne_of_lt h <| idxOf_eq_length_iff.2 al, fun al => (lt_of_le_of_ne idxOf_le_length) fun h => idxOf_eq_length_iff.1 h al⟩ @[deprecated (since := "2025-01-30")] alias indexOf_lt_length_iff := idxOf_lt_length_iff theorem idxOf_append_of_mem {a : α} (h : a ∈ l₁) : idxOf a (l₁ ++ l₂) = idxOf a l₁ := by induction l₁ with | nil => exfalso exact not_mem_nil h | cons d₁ t₁ ih => rw [List.cons_append] by_cases hh : d₁ = a · iterate 2 rw [idxOf_cons_eq _ hh] rw [idxOf_cons_ne _ hh, idxOf_cons_ne _ hh, ih (mem_of_ne_of_mem (Ne.symm hh) h)] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_mem := idxOf_append_of_mem theorem idxOf_append_of_not_mem {a : α} (h : a ∉ l₁) : idxOf a (l₁ ++ l₂) = l₁.length + idxOf a l₂ := by induction l₁ with | nil => rw [List.nil_append, List.length, Nat.zero_add] | cons d₁ t₁ ih => rw [List.cons_append, idxOf_cons_ne _ (ne_of_not_mem_cons h).symm, List.length, ih (not_mem_of_not_mem_cons h), Nat.succ_add] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_not_mem := idxOf_append_of_not_mem end IndexOf /-! ### nth element -/ section deprecated @[simp] theorem getElem?_length (l : List α) : l[l.length]? = none := getElem?_eq_none le_rfl /-- A version of `getElem_map` that can be used for rewriting. -/ theorem getElem_map_rev (f : α → β) {l} {n : Nat} {h : n < l.length} : f l[n] = (map f l)[n]'((l.length_map f).symm ▸ h) := Eq.symm (getElem_map _) theorem get_length_sub_one {l : List α} (h : l.length - 1 < l.length) : l.get ⟨l.length - 1, h⟩ = l.getLast (by rintro rfl; exact Nat.lt_irrefl 0 h) := (getLast_eq_getElem _).symm theorem take_one_drop_eq_of_lt_length {l : List α} {n : ℕ} (h : n < l.length) : (l.drop n).take 1 = [l.get ⟨n, h⟩] := by rw [drop_eq_getElem_cons h, take, take] simp theorem ext_getElem?' {l₁ l₂ : List α} (h' : ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]?) : l₁ = l₂ := by apply ext_getElem? intro n rcases Nat.lt_or_ge n <| max l₁.length l₂.length with hn | hn · exact h' n hn · simp_all [Nat.max_le, getElem?_eq_none] @[deprecated (since := "2025-02-15")] alias ext_get?' := ext_getElem?' @[deprecated (since := "2025-02-15")] alias ext_get?_iff := List.ext_getElem?_iff theorem ext_get_iff {l₁ l₂ : List α} : l₁ = l₂ ↔ l₁.length = l₂.length ∧ ∀ n h₁ h₂, get l₁ ⟨n, h₁⟩ = get l₂ ⟨n, h₂⟩ := by constructor · rintro rfl exact ⟨rfl, fun _ _ _ ↦ rfl⟩ · intro ⟨h₁, h₂⟩ exact ext_get h₁ h₂ theorem ext_getElem?_iff' {l₁ l₂ : List α} : l₁ = l₂ ↔ ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]? := ⟨by rintro rfl _ _; rfl, ext_getElem?'⟩ @[deprecated (since := "2025-02-15")] alias ext_get?_iff' := ext_getElem?_iff' /-- If two lists `l₁` and `l₂` are the same length and `l₁[n]! = l₂[n]!` for all `n`, then the lists are equal. -/ theorem ext_getElem! [Inhabited α] (hl : length l₁ = length l₂) (h : ∀ n : ℕ, l₁[n]! = l₂[n]!) : l₁ = l₂ := ext_getElem hl fun n h₁ h₂ ↦ by simpa only [← getElem!_pos] using h n @[simp] theorem getElem_idxOf [DecidableEq α] {a : α} : ∀ {l : List α} (h : idxOf a l < l.length), l[idxOf a l] = a | b :: l, h => by by_cases h' : b = a <;> simp [h', if_pos, if_false, getElem_idxOf] @[deprecated (since := "2025-01-30")] alias getElem_indexOf := getElem_idxOf -- This is incorrectly named and should be `get_idxOf`; -- this already exists, so will require a deprecation dance. theorem idxOf_get [DecidableEq α] {a : α} {l : List α} (h) : get l ⟨idxOf a l, h⟩ = a := by simp @[deprecated (since := "2025-01-30")] alias indexOf_get := idxOf_get @[simp] theorem getElem?_idxOf [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : l[idxOf a l]? = some a := by rw [getElem?_eq_getElem, getElem_idxOf (idxOf_lt_length_iff.2 h)] @[deprecated (since := "2025-01-30")] alias getElem?_indexOf := getElem?_idxOf @[deprecated (since := "2025-02-15")] alias idxOf_get? := getElem?_idxOf @[deprecated (since := "2025-01-30")] alias indexOf_get? := getElem?_idxOf theorem idxOf_inj [DecidableEq α] {l : List α} {x y : α} (hx : x ∈ l) (hy : y ∈ l) : idxOf x l = idxOf y l ↔ x = y := ⟨fun h => by have x_eq_y : get l ⟨idxOf x l, idxOf_lt_length_iff.2 hx⟩ = get l ⟨idxOf y l, idxOf_lt_length_iff.2 hy⟩ := by simp only [h] simp only [idxOf_get] at x_eq_y; exact x_eq_y, fun h => by subst h; rfl⟩ @[deprecated (since := "2025-01-30")] alias indexOf_inj := idxOf_inj theorem get_reverse' (l : List α) (n) (hn') : l.reverse.get n = l.get ⟨l.length - 1 - n, hn'⟩ := by simp theorem eq_cons_of_length_one {l : List α} (h : l.length = 1) : l = [l.get ⟨0, by omega⟩] := by refine ext_get (by convert h) fun n h₁ h₂ => ?_ simp congr omega end deprecated @[simp] theorem getElem_set_of_ne {l : List α} {i j : ℕ} (h : i ≠ j) (a : α) (hj : j < (l.set i a).length) : (l.set i a)[j] = l[j]'(by simpa using hj) := by rw [← Option.some_inj, ← List.getElem?_eq_getElem, List.getElem?_set_ne h, List.getElem?_eq_getElem] /-! ### map -/ -- `List.map_const` (the version with `Function.const` instead of a lambda) is already tagged -- `simp` in Core -- TODO: Upstream the tagging to Core? attribute [simp] map_const' theorem flatMap_pure_eq_map (f : α → β) (l : List α) : l.flatMap (pure ∘ f) = map f l := .symm <| map_eq_flatMap .. theorem flatMap_congr {l : List α} {f g : α → List β} (h : ∀ x ∈ l, f x = g x) : l.flatMap f = l.flatMap g := (congr_arg List.flatten <| map_congr_left h :) theorem infix_flatMap_of_mem {a : α} {as : List α} (h : a ∈ as) (f : α → List α) : f a <:+: as.flatMap f := infix_of_mem_flatten (mem_map_of_mem h) @[simp] theorem map_eq_map {α β} (f : α → β) (l : List α) : f <$> l = map f l := rfl /-- A single `List.map` of a composition of functions is equal to composing a `List.map` with another `List.map`, fully applied. This is the reverse direction of `List.map_map`. -/ theorem comp_map (h : β → γ) (g : α → β) (l : List α) : map (h ∘ g) l = map h (map g l) := map_map.symm /-- Composing a `List.map` with another `List.map` is equal to a single `List.map` of composed functions. -/ @[simp] theorem map_comp_map (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := by ext l; rw [comp_map, Function.comp_apply] section map_bijectivity theorem _root_.Function.LeftInverse.list_map {f : α → β} {g : β → α} (h : LeftInverse f g) : LeftInverse (map f) (map g) | [] => by simp_rw [map_nil] | x :: xs => by simp_rw [map_cons, h x, h.list_map xs] nonrec theorem _root_.Function.RightInverse.list_map {f : α → β} {g : β → α} (h : RightInverse f g) : RightInverse (map f) (map g) := h.list_map nonrec theorem _root_.Function.Involutive.list_map {f : α → α} (h : Involutive f) : Involutive (map f) := Function.LeftInverse.list_map h @[simp] theorem map_leftInverse_iff {f : α → β} {g : β → α} : LeftInverse (map f) (map g) ↔ LeftInverse f g := ⟨fun h x => by injection h [x], (·.list_map)⟩ @[simp] theorem map_rightInverse_iff {f : α → β} {g : β → α} : RightInverse (map f) (map g) ↔ RightInverse f g := map_leftInverse_iff @[simp] theorem map_involutive_iff {f : α → α} : Involutive (map f) ↔ Involutive f := map_leftInverse_iff theorem _root_.Function.Injective.list_map {f : α → β} (h : Injective f) : Injective (map f) | [], [], _ => rfl | x :: xs, y :: ys, hxy => by injection hxy with hxy hxys rw [h hxy, h.list_map hxys] @[simp] theorem map_injective_iff {f : α → β} : Injective (map f) ↔ Injective f := by refine ⟨fun h x y hxy => ?_, (·.list_map)⟩ suffices [x] = [y] by simpa using this apply h simp [hxy] theorem _root_.Function.Surjective.list_map {f : α → β} (h : Surjective f) : Surjective (map f) := let ⟨_, h⟩ := h.hasRightInverse; h.list_map.surjective @[simp] theorem map_surjective_iff {f : α → β} : Surjective (map f) ↔ Surjective f := by refine ⟨fun h x => ?_, (·.list_map)⟩ let ⟨[y], hxy⟩ := h [x] exact ⟨_, List.singleton_injective hxy⟩ theorem _root_.Function.Bijective.list_map {f : α → β} (h : Bijective f) : Bijective (map f) := ⟨h.1.list_map, h.2.list_map⟩ @[simp] theorem map_bijective_iff {f : α → β} : Bijective (map f) ↔ Bijective f := by simp_rw [Function.Bijective, map_injective_iff, map_surjective_iff] end map_bijectivity theorem eq_of_mem_map_const {b₁ b₂ : β} {l : List α} (h : b₁ ∈ map (const α b₂) l) : b₁ = b₂ := by rw [map_const] at h; exact eq_of_mem_replicate h /-- `eq_nil_or_concat` in simp normal form -/ lemma eq_nil_or_concat' (l : List α) : l = [] ∨ ∃ L b, l = L ++ [b] := by simpa using l.eq_nil_or_concat /-! ### foldl, foldr -/ theorem foldl_ext (f g : α → β → α) (a : α) {l : List β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := by induction l generalizing a with | nil => rfl | cons hd tl ih => unfold foldl rw [ih _ fun a b bin => H a b <| mem_cons_of_mem _ bin, H a hd mem_cons_self] theorem foldr_ext (f g : α → β → β) (b : β) {l : List α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := by induction l with | nil => rfl | cons hd tl ih => ?_ simp only [mem_cons, or_imp, forall_and, forall_eq] at H simp only [foldr, ih H.2, H.1] theorem foldl_concat (f : β → α → β) (b : β) (x : α) (xs : List α) : List.foldl f b (xs ++ [x]) = f (List.foldl f b xs) x := by simp only [List.foldl_append, List.foldl] theorem foldr_concat (f : α → β → β) (b : β) (x : α) (xs : List α) : List.foldr f b (xs ++ [x]) = (List.foldr f (f x b) xs) := by simp only [List.foldr_append, List.foldr] theorem foldl_fixed' {f : α → β → α} {a : α} (hf : ∀ b, f a b = a) : ∀ l : List β, foldl f a l = a | [] => rfl | b :: l => by rw [foldl_cons, hf b, foldl_fixed' hf l] theorem foldr_fixed' {f : α → β → β} {b : β} (hf : ∀ a, f a b = b) : ∀ l : List α, foldr f b l = b | [] => rfl | a :: l => by rw [foldr_cons, foldr_fixed' hf l, hf a] @[simp] theorem foldl_fixed {a : α} : ∀ l : List β, foldl (fun a _ => a) a l = a := foldl_fixed' fun _ => rfl @[simp] theorem foldr_fixed {b : β} : ∀ l : List α, foldr (fun _ b => b) b l = b := foldr_fixed' fun _ => rfl @[deprecated foldr_cons_nil (since := "2025-02-10")] theorem foldr_eta (l : List α) : foldr cons [] l = l := foldr_cons_nil theorem reverse_foldl {l : List α} : reverse (foldl (fun t h => h :: t) [] l) = l := by simp theorem foldl_hom₂ (l : List ι) (f : α → β → γ) (op₁ : α → ι → α) (op₂ : β → ι → β) (op₃ : γ → ι → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ a i) (op₂ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl op₂ b l) := Eq.symm <| by revert a b induction l <;> intros <;> [rfl; simp only [*, foldl]] theorem foldr_hom₂ (l : List ι) (f : α → β → γ) (op₁ : ι → α → α) (op₂ : ι → β → β) (op₃ : ι → γ → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ i a) (op₂ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr op₂ b l) := by revert a induction l <;> intros <;> [rfl; simp only [*, foldr]] theorem injective_foldl_comp {l : List (α → α)} {f : α → α} (hl : ∀ f ∈ l, Function.Injective f) (hf : Function.Injective f) : Function.Injective (@List.foldl (α → α) (α → α) Function.comp f l) := by induction l generalizing f with | nil => exact hf | cons lh lt l_ih => apply l_ih fun _ h => hl _ (List.mem_cons_of_mem _ h) apply Function.Injective.comp hf apply hl _ mem_cons_self /-- Consider two lists `l₁` and `l₂` with designated elements `a₁` and `a₂` somewhere in them: `l₁ = x₁ ++ [a₁] ++ z₁` and `l₂ = x₂ ++ [a₂] ++ z₂`. Assume the designated element `a₂` is present in neither `x₁` nor `z₁`. We conclude that the lists are equal (`l₁ = l₂`) if and only if their respective parts are equal (`x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂`). -/ lemma append_cons_inj_of_not_mem {x₁ x₂ z₁ z₂ : List α} {a₁ a₂ : α} (notin_x : a₂ ∉ x₁) (notin_z : a₂ ∉ z₁) : x₁ ++ a₁ :: z₁ = x₂ ++ a₂ :: z₂ ↔ x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂ := by constructor · simp only [append_eq_append_iff, cons_eq_append_iff, cons_eq_cons] rintro (⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩ | ⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩) <;> simp_all · rintro ⟨rfl, rfl, rfl⟩ rfl section FoldlEqFoldr -- foldl and foldr coincide when f is commutative and associative variable {f : α → α → α} theorem foldl1_eq_foldr1 [hassoc : Std.Associative f] : ∀ a b l, foldl f a (l ++ [b]) = foldr f b (a :: l) | _, _, nil => rfl | a, b, c :: l => by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l] rw [hassoc.assoc] theorem foldl_eq_of_comm_of_assoc [hcomm : Std.Commutative f] [hassoc : Std.Associative f] : ∀ a b l, foldl f a (b :: l) = f b (foldl f a l) | a, b, nil => hcomm.comm a b | a, b, c :: l => by simp only [foldl_cons] have : RightCommutative f := inferInstance rw [← foldl_eq_of_comm_of_assoc .., this.right_comm, foldl_cons] theorem foldl_eq_foldr [Std.Commutative f] [Std.Associative f] : ∀ a l, foldl f a l = foldr f a l | _, nil => rfl | a, b :: l => by simp only [foldr_cons, foldl_eq_of_comm_of_assoc] rw [foldl_eq_foldr a l] end FoldlEqFoldr section FoldlEqFoldlr' variable {f : α → β → α} variable (hf : ∀ a b c, f (f a b) c = f (f a c) b) include hf theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b :: l) = f (foldl f a l) b | _, _, [] => rfl | a, b, c :: l => by rw [foldl, foldl, foldl, ← foldl_eq_of_comm' .., foldl, hf] theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l | _, [] => rfl | a, b :: l => by rw [foldl_eq_of_comm' hf, foldr, foldl_eq_foldr' ..]; rfl end FoldlEqFoldlr' section FoldlEqFoldlr' variable {f : α → β → β} theorem foldr_eq_of_comm' (hf : ∀ a b c, f a (f b c) = f b (f a c)) : ∀ a b l, foldr f a (b :: l) = foldr f (f b a) l | _, _, [] => rfl | a, b, c :: l => by rw [foldr, foldr, foldr, hf, ← foldr_eq_of_comm' hf ..]; rfl end FoldlEqFoldlr' section variable {op : α → α → α} [ha : Std.Associative op] /-- Notation for `op a b`. -/ local notation a " ⋆ " b => op a b /-- Notation for `foldl op a l`. -/ local notation l " <*> " a => foldl op a l theorem foldl_op_eq_op_foldr_assoc : ∀ {l : List α} {a₁ a₂}, ((l <*> a₁) ⋆ a₂) = a₁ ⋆ l.foldr (· ⋆ ·) a₂ | [], _, _ => rfl | a :: l, a₁, a₂ => by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] variable [hc : Std.Commutative op] theorem foldl_assoc_comm_cons {l : List α} {a₁ a₂} : ((a₁ :: l) <*> a₂) = a₁ ⋆ l <*> a₂ := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### foldlM, foldrM, mapM -/ section FoldlMFoldrM variable {m : Type v → Type w} [Monad m] variable [LawfulMonad m] theorem foldrM_eq_foldr (f : α → β → m β) (b l) : foldrM f b l = foldr (fun a mb => mb >>= f a) (pure b) l := by induction l <;> simp [*] theorem foldlM_eq_foldl (f : β → α → m β) (b l) : List.foldlM f b l = foldl (fun mb a => mb >>= fun b => f b a) (pure b) l := by suffices h : ∀ mb : m β, (mb >>= fun b => List.foldlM f b l) = foldl (fun mb a => mb >>= fun b => f b a) mb l by simp [← h (pure b)] induction l with | nil => intro; simp | cons _ _ l_ih => intro; simp only [List.foldlM, foldl, ← l_ih, functor_norm] end FoldlMFoldrM /-! ### intersperse -/ @[deprecated (since := "2025-02-07")] alias intersperse_singleton := intersperse_single @[deprecated (since := "2025-02-07")] alias intersperse_cons_cons := intersperse_cons₂ /-! ### map for partial functions -/ @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {l : List α} (hx : x ∈ l) : SizeOf.sizeOf x < SizeOf.sizeOf l := by induction l with | nil => ?_ | cons h t ih => ?_ <;> cases hx <;> rw [cons.sizeOf_spec] · omega · specialize ih ‹_› omega /-! ### filter -/ theorem length_eq_length_filter_add {l : List (α)} (f : α → Bool) : l.length = (l.filter f).length + (l.filter (! f ·)).length := by simp_rw [← List.countP_eq_length_filter, l.length_eq_countP_add_countP f, Bool.not_eq_true, Bool.decide_eq_false] /-! ### filterMap -/ theorem filterMap_eq_flatMap_toList (f : α → Option β) (l : List α) : l.filterMap f = l.flatMap fun a ↦ (f a).toList := by induction l with | nil => ?_ | cons a l ih => ?_ <;> simp [filterMap_cons] rcases f a <;> simp [ih] theorem filterMap_congr {f g : α → Option β} {l : List α} (h : ∀ x ∈ l, f x = g x) : l.filterMap f = l.filterMap g := by induction l <;> simp_all [filterMap_cons] theorem filterMap_eq_map_iff_forall_eq_some {f : α → Option β} {g : α → β} {l : List α} : l.filterMap f = l.map g ↔ ∀ x ∈ l, f x = some (g x) where mp := by induction l with | nil => simp | cons a l ih => ?_ rcases ha : f a with - | b <;> simp [ha, filterMap_cons] · intro h simpa [show (filterMap f l).length = l.length + 1 from by simp[h], Nat.add_one_le_iff] using List.length_filterMap_le f l · rintro rfl h exact ⟨rfl, ih h⟩ mpr h := Eq.trans (filterMap_congr <| by simpa) (congr_fun filterMap_eq_map _) /-! ### filter -/ section Filter variable {p : α → Bool} theorem filter_singleton {a : α} : [a].filter p = bif p a then [a] else [] := rfl theorem filter_eq_foldr (p : α → Bool) (l : List α) : filter p l = foldr (fun a out => bif p a then a :: out else out) [] l := by induction l <;> simp [*, filter]; rfl #adaptation_note /-- nightly-2024-07-27 This has to be temporarily renamed to avoid an unintentional collision. The prime should be removed at nightly-2024-07-27. -/ @[simp] theorem filter_subset' (l : List α) : filter p l ⊆ l := filter_sublist.subset theorem of_mem_filter {a : α} {l} (h : a ∈ filter p l) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset' l h theorem mem_filter_of_mem {a : α} {l} (h₁ : a ∈ l) (h₂ : p a) : a ∈ filter p l := mem_filter.2 ⟨h₁, h₂⟩ @[deprecated (since := "2025-02-07")] alias monotone_filter_left := filter_subset variable (p) theorem monotone_filter_right (l : List α) ⦃p q : α → Bool⦄ (h : ∀ a, p a → q a) : l.filter p <+ l.filter q := by induction l with | nil => rfl | cons hd tl IH => by_cases hp : p hd · rw [filter_cons_of_pos hp, filter_cons_of_pos (h _ hp)] exact IH.cons_cons hd · rw [filter_cons_of_neg hp] by_cases hq : q hd · rw [filter_cons_of_pos hq] exact sublist_cons_of_sublist hd IH · rw [filter_cons_of_neg hq] exact IH lemma map_filter {f : α → β} (hf : Injective f) (l : List α) [DecidablePred fun b => ∃ a, p a ∧ f a = b] : (l.filter p).map f = (l.map f).filter fun b => ∃ a, p a ∧ f a = b := by simp [comp_def, filter_map, hf.eq_iff] @[deprecated (since := "2025-02-07")] alias map_filter' := map_filter lemma filter_attach' (l : List α) (p : {a // a ∈ l} → Bool) [DecidableEq α] : l.attach.filter p = (l.filter fun x => ∃ h, p ⟨x, h⟩).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := by classical refine map_injective_iff.2 Subtype.coe_injective ?_ simp [comp_def, map_filter _ Subtype.coe_injective] lemma filter_attach (l : List α) (p : α → Bool) : (l.attach.filter fun x => p x : List {x // x ∈ l}) = (l.filter p).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := map_injective_iff.2 Subtype.coe_injective <| by simp_rw [map_map, comp_def, Subtype.map, id, ← Function.comp_apply (g := Subtype.val), ← filter_map, attach_map_subtype_val] lemma filter_comm (q) (l : List α) : filter p (filter q l) = filter q (filter p l) := by simp [Bool.and_comm] @[simp] theorem filter_true (l : List α) : filter (fun _ => true) l = l := by induction l <;> simp [*, filter] @[simp] theorem filter_false (l : List α) : filter (fun _ => false) l = [] := by induction l <;> simp [*, filter] end Filter /-! ### eraseP -/ section eraseP variable {p : α → Bool} @[simp] theorem length_eraseP_add_one {l : List α} {a} (al : a ∈ l) (pa : p a) : (l.eraseP p).length + 1 = l.length := by let ⟨_, l₁, l₂, _, _, h₁, h₂⟩ := exists_of_eraseP al pa rw [h₂, h₁, length_append, length_append] rfl end eraseP /-! ### erase -/ section Erase variable [DecidableEq α] @[simp] theorem length_erase_add_one {a : α} {l : List α} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_eraseP, length_eraseP_add_one h (decide_eq_true rfl)] theorem map_erase [DecidableEq β] {f : α → β} (finj : Injective f) {a : α} (l : List α) : map f (l.erase a) = (map f l).erase (f a) := by have this : (a == ·) = (f a == f ·) := by ext b; simp [beq_eq_decide, finj.eq_iff] rw [erase_eq_eraseP, erase_eq_eraseP, eraseP_map, this]; rfl theorem map_foldl_erase [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (foldl List.erase l₁ l₂) = foldl (fun l a => l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁ <;> [rfl; simp only [foldl_cons, map_erase finj, *]] theorem erase_getElem [DecidableEq ι] {l : List ι} {i : ℕ} (hi : i < l.length) : Perm (l.erase l[i]) (l.eraseIdx i) := by induction l generalizing i with | nil => simp | cons a l IH => cases i with | zero => simp | succ i => have hi' : i < l.length := by simpa using hi if ha : a = l[i] then simpa [ha] using .trans (perm_cons_erase (getElem_mem _)) (.cons _ (IH hi')) else simpa [ha] using IH hi' theorem length_eraseIdx_add_one {l : List ι} {i : ℕ} (h : i < l.length) : (l.eraseIdx i).length + 1 = l.length := by rw [length_eraseIdx] split <;> omega end Erase /-! ### diff -/ section Diff variable [DecidableEq α] @[simp] theorem map_diff [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] @[deprecated (since := "2025-04-10")] alias erase_diff_erase_sublist_of_sublist := Sublist.erase_diff_erase_sublist end Diff section Choose variable (p : α → Prop) [DecidablePred p] (l : List α) theorem choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose /-! ### Forall -/ section Forall variable {p q : α → Prop} {l : List α} @[simp] theorem forall_cons (p : α → Prop) (x : α) : ∀ l : List α, Forall p (x :: l) ↔ p x ∧ Forall p l | [] => (and_iff_left_of_imp fun _ ↦ trivial).symm | _ :: _ => Iff.rfl @[simp] theorem forall_append {p : α → Prop} : ∀ {xs ys : List α}, Forall p (xs ++ ys) ↔ Forall p xs ∧ Forall p ys | [] => by simp | _ :: _ => by simp [forall_append, and_assoc] theorem forall_iff_forall_mem : ∀ {l : List α}, Forall p l ↔ ∀ x ∈ l, p x | [] => (iff_true_intro <| forall_mem_nil _).symm | x :: l => by rw [forall_mem_cons, forall_cons, forall_iff_forall_mem] theorem Forall.imp (h : ∀ x, p x → q x) : ∀ {l : List α}, Forall p l → Forall q l | [] => id | x :: l => by simp only [forall_cons, and_imp] rw [← and_imp] exact And.imp (h x) (Forall.imp h) @[simp] theorem forall_map_iff {p : β → Prop} (f : α → β) : Forall p (l.map f) ↔ Forall (p ∘ f) l := by induction l <;> simp [*] instance (p : α → Prop) [DecidablePred p] : DecidablePred (Forall p) := fun _ => decidable_of_iff' _ forall_iff_forall_mem end Forall /-! ### Miscellaneous lemmas -/ theorem get_attach (l : List α) (i) : (l.attach.get i).1 = l.get ⟨i, length_attach (l := l) ▸ i.2⟩ := by simp section Disjoint /-- The images of disjoint lists under a partially defined map are disjoint -/ theorem disjoint_pmap {p : α → Prop} {f : ∀ a : α, p a → β} {s t : List α} (hs : ∀ a ∈ s, p a) (ht : ∀ a ∈ t, p a) (hf : ∀ (a a' : α) (ha : p a) (ha' : p a'), f a ha = f a' ha' → a = a') (h : Disjoint s t) : Disjoint (s.pmap f hs) (t.pmap f ht) := by simp only [Disjoint, mem_pmap] rintro b ⟨a, ha, rfl⟩ ⟨a', ha', ha''⟩ apply h ha rwa [hf a a' (hs a ha) (ht a' ha') ha''.symm] /-- The images of disjoint lists under an injective map are disjoint -/ theorem disjoint_map {f : α → β} {s t : List α} (hf : Function.Injective f) (h : Disjoint s t) : Disjoint (s.map f) (t.map f) := by rw [← pmap_eq_map (fun _ _ ↦ trivial), ← pmap_eq_map (fun _ _ ↦ trivial)] exact disjoint_pmap _ _ (fun _ _ _ _ h' ↦ hf h') h alias Disjoint.map := disjoint_map theorem Disjoint.of_map {f : α → β} {s t : List α} (h : Disjoint (s.map f) (t.map f)) : Disjoint s t := fun _a has hat ↦ h (mem_map_of_mem has) (mem_map_of_mem hat) theorem Disjoint.map_iff {f : α → β} {s t : List α} (hf : Function.Injective f) : Disjoint (s.map f) (t.map f) ↔ Disjoint s t := ⟨fun h ↦ h.of_map, fun h ↦ h.map hf⟩ theorem Perm.disjoint_left {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l₁ l ↔ Disjoint l₂ l := by simp_rw [List.disjoint_left, p.mem_iff] theorem Perm.disjoint_right {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l l₁ ↔ Disjoint l l₂ := by simp_rw [List.disjoint_right, p.mem_iff] @[simp] theorem disjoint_reverse_left {l₁ l₂ : List α} : Disjoint l₁.reverse l₂ ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_left @[simp] theorem disjoint_reverse_right {l₁ l₂ : List α} : Disjoint l₁ l₂.reverse ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_right end Disjoint section lookup variable [BEq α] [LawfulBEq α] lemma lookup_graph (f : α → β) {a : α} {as : List α} (h : a ∈ as) : lookup a (as.map fun x => (x, f x)) = some (f a) := by induction as with | nil => exact (not_mem_nil h).elim | cons a' as ih => by_cases ha : a = a' · simp [ha, lookup_cons] · simpa [lookup_cons, beq_false_of_ne ha] using ih (List.mem_of_ne_of_mem ha h) end lookup section range' @[simp] lemma range'_0 (a b : ℕ) : range' a b 0 = replicate b a := by induction b with | zero => simp | succ b ih => simp [range'_succ, ih, replicate_succ] lemma left_le_of_mem_range' {a b s x : ℕ} (hx : x ∈ List.range' a b s) : a ≤ x := by obtain ⟨i, _, rfl⟩ := List.mem_range'.mp hx exact le_add_right a (s * i) end range' end List
Mathlib/Data/List/Basic.lean
2,393
2,403
/- Copyright (c) 2020 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 import Mathlib.Data.ENat.Basic /-! # Trailing degree of univariate polynomials ## Main definitions * `trailingDegree p`: the multiplicity of `X` in the polynomial `p` * `natTrailingDegree`: a variant of `trailingDegree` that takes values in the natural numbers * `trailingCoeff`: the coefficient at index `natTrailingDegree p` Converts most results about `degree`, `natDegree` and `leadingCoeff` to results about the bottom end of a polynomial -/ noncomputable section open Function Polynomial Finsupp Finset open scoped Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b : R} {n m : ℕ} section Semiring variable [Semiring R] {p q r : R[X]} /-- `trailingDegree p` is the multiplicity of `x` in the polynomial `p`, i.e. the smallest `X`-exponent in `p`. `trailingDegree p = some n` when `p ≠ 0` and `n` is the smallest power of `X` that appears in `p`, otherwise `trailingDegree 0 = ⊤`. -/ def trailingDegree (p : R[X]) : ℕ∞ := p.support.min theorem trailingDegree_lt_wf : WellFounded fun p q : R[X] => trailingDegree p < trailingDegree q := InvImage.wf trailingDegree wellFounded_lt /-- `natTrailingDegree p` forces `trailingDegree p` to `ℕ`, by defining `natTrailingDegree ⊤ = 0`. -/ def natTrailingDegree (p : R[X]) : ℕ := ENat.toNat (trailingDegree p) /-- `trailingCoeff p` gives the coefficient of the smallest power of `X` in `p`. -/ def trailingCoeff (p : R[X]) : R := coeff p (natTrailingDegree p) /-- a polynomial is `monic_at` if its trailing coefficient is 1 -/ def TrailingMonic (p : R[X]) := trailingCoeff p = (1 : R) theorem TrailingMonic.def : TrailingMonic p ↔ trailingCoeff p = 1 := Iff.rfl instance TrailingMonic.decidable [DecidableEq R] : Decidable (TrailingMonic p) := inferInstanceAs <| Decidable (trailingCoeff p = (1 : R)) @[simp] theorem TrailingMonic.trailingCoeff {p : R[X]} (hp : p.TrailingMonic) : trailingCoeff p = 1 := hp @[simp] theorem trailingDegree_zero : trailingDegree (0 : R[X]) = ⊤ := rfl @[simp] theorem trailingCoeff_zero : trailingCoeff (0 : R[X]) = 0 := rfl @[simp] theorem natTrailingDegree_zero : natTrailingDegree (0 : R[X]) = 0 := rfl @[simp] theorem trailingDegree_eq_top : trailingDegree p = ⊤ ↔ p = 0 := ⟨fun h => support_eq_empty.1 (Finset.min_eq_top.1 h), fun h => by simp [h]⟩ theorem trailingDegree_eq_natTrailingDegree (hp : p ≠ 0) : trailingDegree p = (natTrailingDegree p : ℕ∞) := .symm <| ENat.coe_toNat <| mt trailingDegree_eq_top.1 hp theorem trailingDegree_eq_iff_natTrailingDegree_eq {p : R[X]} {n : ℕ} (hp : p ≠ 0) : p.trailingDegree = n ↔ p.natTrailingDegree = n := by rw [trailingDegree_eq_natTrailingDegree hp, Nat.cast_inj] theorem trailingDegree_eq_iff_natTrailingDegree_eq_of_pos {p : R[X]} {n : ℕ} (hn : n ≠ 0) : p.trailingDegree = n ↔ p.natTrailingDegree = n := by rw [natTrailingDegree, ENat.toNat_eq_iff hn] theorem natTrailingDegree_eq_of_trailingDegree_eq_some {p : R[X]} {n : ℕ} (h : trailingDegree p = n) : natTrailingDegree p = n := by simp [natTrailingDegree, h] @[simp] theorem natTrailingDegree_le_trailingDegree : ↑(natTrailingDegree p) ≤ trailingDegree p := ENat.coe_toNat_le_self _ theorem natTrailingDegree_eq_of_trailingDegree_eq [Semiring S] {q : S[X]} (h : trailingDegree p = trailingDegree q) : natTrailingDegree p = natTrailingDegree q := by unfold natTrailingDegree rw [h] theorem trailingDegree_le_of_ne_zero (h : coeff p n ≠ 0) : trailingDegree p ≤ n := min_le (mem_support_iff.2 h) theorem natTrailingDegree_le_of_ne_zero (h : coeff p n ≠ 0) : natTrailingDegree p ≤ n := ENat.toNat_le_of_le_coe <| trailingDegree_le_of_ne_zero h @[simp] lemma coeff_natTrailingDegree_eq_zero : coeff p p.natTrailingDegree = 0 ↔ p = 0 := by constructor · rintro h by_contra hp obtain ⟨n, hpn, hn⟩ := by simpa using min_mem_image_coe <| support_nonempty.2 hp obtain rfl := (trailingDegree_eq_iff_natTrailingDegree_eq hp).1 hn.symm exact hpn h · rintro rfl simp lemma coeff_natTrailingDegree_ne_zero : coeff p p.natTrailingDegree ≠ 0 ↔ p ≠ 0 := coeff_natTrailingDegree_eq_zero.not @[simp] lemma trailingDegree_eq_zero : trailingDegree p = 0 ↔ coeff p 0 ≠ 0 := Finset.min_eq_bot.trans mem_support_iff @[simp] lemma natTrailingDegree_eq_zero : natTrailingDegree p = 0 ↔ p = 0 ∨ coeff p 0 ≠ 0 := by simp [natTrailingDegree, or_comm] lemma natTrailingDegree_ne_zero : natTrailingDegree p ≠ 0 ↔ p ≠ 0 ∧ coeff p 0 = 0 := natTrailingDegree_eq_zero.not.trans <| by rw [not_or, not_ne_iff] lemma trailingDegree_ne_zero : trailingDegree p ≠ 0 ↔ coeff p 0 = 0 := trailingDegree_eq_zero.not_left @[simp] theorem trailingDegree_le_trailingDegree (h : coeff q (natTrailingDegree p) ≠ 0) : trailingDegree q ≤ trailingDegree p := (trailingDegree_le_of_ne_zero h).trans natTrailingDegree_le_trailingDegree theorem trailingDegree_ne_of_natTrailingDegree_ne {n : ℕ} : p.natTrailingDegree ≠ n → trailingDegree p ≠ n := mt fun h => by rw [natTrailingDegree, h, ENat.toNat_coe] theorem natTrailingDegree_le_of_trailingDegree_le {n : ℕ} {hp : p ≠ 0} (H : (n : ℕ∞) ≤ trailingDegree p) : n ≤ natTrailingDegree p := by rwa [trailingDegree_eq_natTrailingDegree hp, Nat.cast_le] at H theorem natTrailingDegree_le_natTrailingDegree (hq : q ≠ 0) (hpq : p.trailingDegree ≤ q.trailingDegree) : p.natTrailingDegree ≤ q.natTrailingDegree := ENat.toNat_le_toNat hpq <| by simpa @[simp] theorem trailingDegree_monomial (ha : a ≠ 0) : trailingDegree (monomial n a) = n := by rw [trailingDegree, support_monomial n ha, min_singleton] rfl theorem natTrailingDegree_monomial (ha : a ≠ 0) : natTrailingDegree (monomial n a) = n := by rw [natTrailingDegree, trailingDegree_monomial ha] rfl theorem natTrailingDegree_monomial_le : natTrailingDegree (monomial n a) ≤ n := letI := Classical.decEq R if ha : a = 0 then by simp [ha] else (natTrailingDegree_monomial ha).le theorem le_trailingDegree_monomial : ↑n ≤ trailingDegree (monomial n a) := letI := Classical.decEq R if ha : a = 0 then by simp [ha] else (trailingDegree_monomial ha).ge @[simp] theorem trailingDegree_C (ha : a ≠ 0) : trailingDegree (C a) = (0 : ℕ∞) := trailingDegree_monomial ha theorem le_trailingDegree_C : (0 : ℕ∞) ≤ trailingDegree (C a) := le_trailingDegree_monomial theorem trailingDegree_one_le : (0 : ℕ∞) ≤ trailingDegree (1 : R[X]) := by rw [← C_1] exact le_trailingDegree_C @[simp] theorem natTrailingDegree_C (a : R) : natTrailingDegree (C a) = 0 := nonpos_iff_eq_zero.1 natTrailingDegree_monomial_le @[simp] theorem natTrailingDegree_one : natTrailingDegree (1 : R[X]) = 0 := natTrailingDegree_C 1 @[simp] theorem natTrailingDegree_natCast (n : ℕ) : natTrailingDegree (n : R[X]) = 0 := by simp only [← C_eq_natCast, natTrailingDegree_C] @[simp] theorem trailingDegree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) : trailingDegree (C a * X ^ n) = n := by rw [C_mul_X_pow_eq_monomial, trailingDegree_monomial ha] theorem le_trailingDegree_C_mul_X_pow (n : ℕ) (a : R) : (n : ℕ∞) ≤ trailingDegree (C a * X ^ n) := by rw [C_mul_X_pow_eq_monomial] exact le_trailingDegree_monomial theorem coeff_eq_zero_of_lt_trailingDegree (h : (n : ℕ∞) < trailingDegree p) : coeff p n = 0 := Classical.not_not.1 (mt trailingDegree_le_of_ne_zero (not_le_of_gt h)) theorem coeff_eq_zero_of_lt_natTrailingDegree {p : R[X]} {n : ℕ} (h : n < p.natTrailingDegree) : p.coeff n = 0 := by apply coeff_eq_zero_of_lt_trailingDegree by_cases hp : p = 0 · rw [hp, trailingDegree_zero] exact WithTop.coe_lt_top n · rw [trailingDegree_eq_natTrailingDegree hp] exact WithTop.coe_lt_coe.2 h @[simp] theorem coeff_natTrailingDegree_pred_eq_zero {p : R[X]} {hp : (0 : ℕ∞) < natTrailingDegree p} : p.coeff (p.natTrailingDegree - 1) = 0 := coeff_eq_zero_of_lt_natTrailingDegree <| Nat.sub_lt (WithTop.coe_pos.mp hp) Nat.one_pos theorem le_trailingDegree_X_pow (n : ℕ) : (n : ℕ∞) ≤ trailingDegree (X ^ n : R[X]) := by simpa only [C_1, one_mul] using le_trailingDegree_C_mul_X_pow n (1 : R) theorem le_trailingDegree_X : (1 : ℕ∞) ≤ trailingDegree (X : R[X]) := le_trailingDegree_monomial theorem natTrailingDegree_X_le : (X : R[X]).natTrailingDegree ≤ 1 := natTrailingDegree_monomial_le @[simp] theorem trailingCoeff_eq_zero : trailingCoeff p = 0 ↔ p = 0 := ⟨fun h => _root_.by_contradiction fun hp => mt mem_support_iff.1 (Classical.not_not.2 h) (mem_of_min (trailingDegree_eq_natTrailingDegree hp)), fun h => h.symm ▸ leadingCoeff_zero⟩ theorem trailingCoeff_nonzero_iff_nonzero : trailingCoeff p ≠ 0 ↔ p ≠ 0 := not_congr trailingCoeff_eq_zero theorem natTrailingDegree_mem_support_of_nonzero : p ≠ 0 → natTrailingDegree p ∈ p.support := mem_support_iff.mpr ∘ trailingCoeff_nonzero_iff_nonzero.mpr theorem natTrailingDegree_le_of_mem_supp (a : ℕ) : a ∈ p.support → natTrailingDegree p ≤ a := natTrailingDegree_le_of_ne_zero ∘ mem_support_iff.mp theorem natTrailingDegree_eq_support_min' (h : p ≠ 0) : natTrailingDegree p = p.support.min' (nonempty_support_iff.mpr h) := by rw [natTrailingDegree, trailingDegree, ← Finset.coe_min', ENat.some_eq_coe, ENat.toNat_coe] theorem le_natTrailingDegree (hp : p ≠ 0) (hn : ∀ m < n, p.coeff m = 0) : n ≤ p.natTrailingDegree := by rw [natTrailingDegree_eq_support_min' hp] exact Finset.le_min' _ _ _ fun m hm => not_lt.1 fun hmn => mem_support_iff.1 hm <| hn _ hmn theorem natTrailingDegree_le_natDegree (p : R[X]) : p.natTrailingDegree ≤ p.natDegree := by by_cases hp : p = 0 · rw [hp, natDegree_zero, natTrailingDegree_zero] · exact le_natDegree_of_ne_zero (mt trailingCoeff_eq_zero.mp hp) theorem natTrailingDegree_mul_X_pow {p : R[X]} (hp : p ≠ 0) (n : ℕ) : (p * X ^ n).natTrailingDegree = p.natTrailingDegree + n := by apply le_antisymm · refine natTrailingDegree_le_of_ne_zero fun h => mt trailingCoeff_eq_zero.mp hp ?_ rwa [trailingCoeff, ← coeff_mul_X_pow] · rw [natTrailingDegree_eq_support_min' fun h => hp (mul_X_pow_eq_zero h), Finset.le_min'_iff] intro y hy have key : n ≤ y := by rw [mem_support_iff, coeff_mul_X_pow'] at hy exact by_contra fun h => hy (if_neg h) rw [mem_support_iff, coeff_mul_X_pow', if_pos key] at hy exact (le_tsub_iff_right key).mp (natTrailingDegree_le_of_ne_zero hy) theorem le_trailingDegree_mul : p.trailingDegree + q.trailingDegree ≤ (p * q).trailingDegree := by refine Finset.le_min fun n hn => ?_ rw [mem_support_iff, coeff_mul] at hn obtain ⟨⟨i, j⟩, hij, hpq⟩ := exists_ne_zero_of_sum_ne_zero hn refine (add_le_add (min_le (mem_support_iff.mpr (left_ne_zero_of_mul hpq))) (min_le (mem_support_iff.mpr (right_ne_zero_of_mul hpq)))).trans_eq ?_ rwa [← WithTop.coe_add, WithTop.coe_eq_coe, ← mem_antidiagonal] theorem le_natTrailingDegree_mul (h : p * q ≠ 0) : p.natTrailingDegree + q.natTrailingDegree ≤ (p * q).natTrailingDegree := by have hp : p ≠ 0 := fun hp => h (by rw [hp, zero_mul]) have hq : q ≠ 0 := fun hq => h (by rw [hq, mul_zero]) rw [← WithTop.coe_le_coe, WithTop.coe_add, ← Nat.cast_withTop (natTrailingDegree p), ← Nat.cast_withTop (natTrailingDegree q), ← Nat.cast_withTop (natTrailingDegree (p * q)), ← trailingDegree_eq_natTrailingDegree hp, ← trailingDegree_eq_natTrailingDegree hq, ← trailingDegree_eq_natTrailingDegree h] exact le_trailingDegree_mul theorem coeff_mul_natTrailingDegree_add_natTrailingDegree : (p * q).coeff (p.natTrailingDegree + q.natTrailingDegree) = p.trailingCoeff * q.trailingCoeff := by rw [coeff_mul] refine Finset.sum_eq_single (p.natTrailingDegree, q.natTrailingDegree) ?_ fun h => (h (mem_antidiagonal.mpr rfl)).elim rintro ⟨i, j⟩ h₁ h₂ rw [mem_antidiagonal] at h₁ by_cases hi : i < p.natTrailingDegree · rw [coeff_eq_zero_of_lt_natTrailingDegree hi, zero_mul] by_cases hj : j < q.natTrailingDegree · rw [coeff_eq_zero_of_lt_natTrailingDegree hj, mul_zero] rw [not_lt] at hi hj refine (h₂ (Prod.ext_iff.mpr ?_).symm).elim exact (add_eq_add_iff_eq_and_eq hi hj).mp h₁.symm theorem trailingDegree_mul' (h : p.trailingCoeff * q.trailingCoeff ≠ 0) : (p * q).trailingDegree = p.trailingDegree + q.trailingDegree := by have hp : p ≠ 0 := fun hp => h (by rw [hp, trailingCoeff_zero, zero_mul]) have hq : q ≠ 0 := fun hq => h (by rw [hq, trailingCoeff_zero, mul_zero]) refine le_antisymm ?_ le_trailingDegree_mul rw [trailingDegree_eq_natTrailingDegree hp, trailingDegree_eq_natTrailingDegree hq, ← ENat.coe_add] apply trailingDegree_le_of_ne_zero rwa [coeff_mul_natTrailingDegree_add_natTrailingDegree] theorem natTrailingDegree_mul' (h : p.trailingCoeff * q.trailingCoeff ≠ 0) : (p * q).natTrailingDegree = p.natTrailingDegree + q.natTrailingDegree := by have hp : p ≠ 0 := fun hp => h (by rw [hp, trailingCoeff_zero, zero_mul]) have hq : q ≠ 0 := fun hq => h (by rw [hq, trailingCoeff_zero, mul_zero]) apply natTrailingDegree_eq_of_trailingDegree_eq_some rw [trailingDegree_mul' h, Nat.cast_withTop (natTrailingDegree p + natTrailingDegree q), WithTop.coe_add, ← Nat.cast_withTop, ← Nat.cast_withTop, ← trailingDegree_eq_natTrailingDegree hp, ← trailingDegree_eq_natTrailingDegree hq] theorem natTrailingDegree_mul [NoZeroDivisors R] (hp : p ≠ 0) (hq : q ≠ 0) : (p * q).natTrailingDegree = p.natTrailingDegree + q.natTrailingDegree := natTrailingDegree_mul' (mul_ne_zero (mt trailingCoeff_eq_zero.mp hp) (mt trailingCoeff_eq_zero.mp hq)) end Semiring section NonzeroSemiring variable [Semiring R] [Nontrivial R] {p q : R[X]} @[simp] theorem trailingDegree_one : trailingDegree (1 : R[X]) = (0 : ℕ∞) := trailingDegree_C one_ne_zero @[simp] theorem trailingDegree_X : trailingDegree (X : R[X]) = 1 := trailingDegree_monomial one_ne_zero @[simp] theorem natTrailingDegree_X : (X : R[X]).natTrailingDegree = 1 := natTrailingDegree_monomial one_ne_zero @[simp] lemma trailingDegree_X_pow (n : ℕ) : (X ^ n : R[X]).trailingDegree = n := by rw [X_pow_eq_monomial, trailingDegree_monomial one_ne_zero] @[simp] lemma natTrailingDegree_X_pow (n : ℕ) : (X ^ n : R[X]).natTrailingDegree = n := by rw [X_pow_eq_monomial, natTrailingDegree_monomial one_ne_zero] end NonzeroSemiring section Ring variable [Ring R] @[simp] theorem trailingDegree_neg (p : R[X]) : trailingDegree (-p) = trailingDegree p := by unfold trailingDegree rw [support_neg] @[simp] theorem natTrailingDegree_neg (p : R[X]) : natTrailingDegree (-p) = natTrailingDegree p := by simp [natTrailingDegree] @[simp] theorem natTrailingDegree_intCast (n : ℤ) : natTrailingDegree (n : R[X]) = 0 := by simp only [← C_eq_intCast, natTrailingDegree_C] end Ring section Semiring variable [Semiring R] /-- The second-lowest coefficient, or 0 for constants -/ def nextCoeffUp (p : R[X]) : R := if p.natTrailingDegree = 0 then 0 else p.coeff (p.natTrailingDegree + 1) @[simp] lemma nextCoeffUp_zero : nextCoeffUp (0 : R[X]) = 0 := by simp [nextCoeffUp] @[simp] theorem nextCoeffUp_C_eq_zero (c : R) : nextCoeffUp (C c) = 0 := by rw [nextCoeffUp] simp theorem nextCoeffUp_of_constantCoeff_eq_zero (p : R[X]) (hp : coeff p 0 = 0) : nextCoeffUp p = p.coeff (p.natTrailingDegree + 1) := by obtain rfl | hp₀ := eq_or_ne p 0 · simp · rw [nextCoeffUp, if_neg (natTrailingDegree_ne_zero.2 ⟨hp₀, hp⟩)] end Semiring section Semiring variable [Semiring R] {p q : R[X]} theorem coeff_natTrailingDegree_eq_zero_of_trailingDegree_lt (h : trailingDegree p < trailingDegree q) : coeff q (natTrailingDegree p) = 0 := coeff_eq_zero_of_lt_trailingDegree <| natTrailingDegree_le_trailingDegree.trans_lt h theorem ne_zero_of_trailingDegree_lt {n : ℕ∞} (h : trailingDegree p < n) : p ≠ 0 := fun h₀ => h.not_le (by simp [h₀]) lemma natTrailingDegree_eq_zero_of_constantCoeff_ne_zero (h : constantCoeff p ≠ 0) : p.natTrailingDegree = 0 := le_antisymm (natTrailingDegree_le_of_ne_zero h) zero_le' namespace Monic lemma eq_X_pow_iff_natDegree_le_natTrailingDegree (h₁ : p.Monic) : p = X ^ p.natDegree ↔ p.natDegree ≤ p.natTrailingDegree := by refine ⟨fun h => ?_, fun h => ?_⟩ · nontriviality R rw [h, natTrailingDegree_X_pow, ← h] · ext n rw [coeff_X_pow] obtain hn | rfl | hn := lt_trichotomy n p.natDegree · rw [if_neg hn.ne, coeff_eq_zero_of_lt_natTrailingDegree (hn.trans_le h)] · simpa only [if_pos rfl] using h₁.leadingCoeff · rw [if_neg hn.ne', coeff_eq_zero_of_natDegree_lt hn] lemma eq_X_pow_iff_natTrailingDegree_eq_natDegree (h₁ : p.Monic) : p = X ^ p.natDegree ↔ p.natTrailingDegree = p.natDegree := h₁.eq_X_pow_iff_natDegree_le_natTrailingDegree.trans (natTrailingDegree_le_natDegree p).ge_iff_eq end Monic end Semiring end Polynomial
Mathlib/Algebra/Polynomial/Degree/TrailingDegree.lean
464
465
/- 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, Johan Commelin, Amelia Livingston, Anne Baanen -/ import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.Field.IsField import Mathlib.Algebra.GroupWithZero.NonZeroDivisors import Mathlib.GroupTheory.MonoidLocalization.MonoidWithZero import Mathlib.RingTheory.Localization.Defs import Mathlib.RingTheory.OreLocalization.Ring /-! # Localizations of commutative rings This file contains various basic results on localizations. We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R` such that `f x = f y`, there exists `c ∈ M` such that `x * c = y * c`. (The converse is a consequence of 1.) In the following, let `R, P` be commutative rings, `S, Q` be `R`- and `P`-algebras and `M, T` be submonoids of `R` and `P` respectively, e.g.: ``` variable (R S P Q : Type*) [CommRing R] [CommRing S] [CommRing P] [CommRing Q] variable [Algebra R S] [Algebra P Q] (M : Submonoid R) (T : Submonoid P) ``` ## Main definitions * `IsLocalization.algEquiv`: if `Q` is another localization of `R` at `M`, then `S` and `Q` are isomorphic as `R`-algebras ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A previous version of this file used a fully bundled type of ring localization maps, then used a type synonym `f.codomain` for `f : LocalizationMap M S` to instantiate the `R`-algebra structure on `S`. This results in defining ad-hoc copies for everything already defined on `S`. By making `IsLocalization` a predicate on the `algebraMap R S`, we can ensure the localization map commutes nicely with other `algebraMap`s. To prove most lemmas about a localization map `algebraMap R S` in this file we invoke the corresponding proof for the underlying `CommMonoid` localization map `IsLocalization.toLocalizationMap M S`, which can be found in `GroupTheory.MonoidLocalization` and the namespace `Submonoid.LocalizationMap`. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → Localization M` equals the surjection `LocalizationMap.mk'` induced by the map `algebraMap : R →+* Localization M`. The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `LocalizationMap.mk'` induced by any localization map. The proof that "a `CommRing` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[Field K]` instead of just `[CommRing K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ assert_not_exists Ideal open Function namespace Localization open IsLocalization variable {ι : Type*} {R : ι → Type*} [∀ i, CommSemiring (R i)] variable {i : ι} (S : Submonoid (R i)) /-- `IsLocalization.map` applied to a projection homomorphism from a product ring. -/ noncomputable abbrev mapPiEvalRingHom : Localization (S.comap <| Pi.evalRingHom R i) →+* Localization S := map (T := S) _ (Pi.evalRingHom R i) le_rfl open Function in theorem mapPiEvalRingHom_bijective : Bijective (mapPiEvalRingHom S) := by let T := S.comap (Pi.evalRingHom R i) classical refine ⟨fun x₁ x₂ eq ↦ ?_, fun x ↦ ?_⟩ · obtain ⟨r₁, s₁, rfl⟩ := mk'_surjective T x₁ obtain ⟨r₂, s₂, rfl⟩ := mk'_surjective T x₂ simp_rw [map_mk'] at eq rw [IsLocalization.eq] at eq ⊢ obtain ⟨s, hs⟩ := eq refine ⟨⟨update 0 i s, by apply update_self i s.1 0 ▸ s.2⟩, funext fun j ↦ ?_⟩ obtain rfl | ne := eq_or_ne j i · simpa using hs · simp [update_of_ne ne] · obtain ⟨r, s, rfl⟩ := mk'_surjective S x exact ⟨mk' (M := T) _ (update 0 i r) ⟨update 0 i s, by apply update_self i s.1 0 ▸ s.2⟩, by simp [map_mk']⟩ end Localization section CommSemiring variable {R : Type*} [CommSemiring R] {M N : Submonoid R} {S : Type*} [CommSemiring S] variable [Algebra R S] {P : Type*} [CommSemiring P] namespace IsLocalization section IsLocalization variable [IsLocalization M S] variable (M S) in include M in theorem linearMap_compatibleSMul (N₁ N₂) [AddCommMonoid N₁] [AddCommMonoid N₂] [Module R N₁] [Module S N₁] [Module R N₂] [Module S N₂] [IsScalarTower R S N₁] [IsScalarTower R S N₂] : LinearMap.CompatibleSMul N₁ N₂ S R where map_smul f s s' := by obtain ⟨r, m, rfl⟩ := mk'_surjective M s rw [← (map_units S m).smul_left_cancel] simp_rw [algebraMap_smul, ← map_smul, ← smul_assoc, smul_mk'_self, algebraMap_smul, map_smul] variable {g : R →+* P} (hg : ∀ y : M, IsUnit (g y)) variable (M) in include M in -- This is not an instance since the submonoid `M` would become a metavariable in typeclass search. theorem algHom_subsingleton [Algebra R P] : Subsingleton (S →ₐ[R] P) := ⟨fun f g => AlgHom.coe_ringHom_injective <| IsLocalization.ringHom_ext M <| by rw [f.comp_algebraMap, g.comp_algebraMap]⟩ section AlgEquiv variable {Q : Type*} [CommSemiring Q] [Algebra R Q] [IsLocalization M Q] section variable (M S Q) /-- If `S`, `Q` are localizations of `R` at the submonoid `M` respectively, there is an isomorphism of localizations `S ≃ₐ[R] Q`. -/ @[simps!] noncomputable def algEquiv : S ≃ₐ[R] Q := { ringEquivOfRingEquiv S Q (RingEquiv.refl R) M.map_id with commutes' := ringEquivOfRingEquiv_eq _ } end theorem algEquiv_mk' (x : R) (y : M) : algEquiv M S Q (mk' S x y) = mk' Q x y := by simp theorem algEquiv_symm_mk' (x : R) (y : M) : (algEquiv M S Q).symm (mk' Q x y) = mk' S x y := by simp variable (M) in include M in protected lemma bijective (f : S →+* Q) (hf : f.comp (algebraMap R S) = algebraMap R Q) : Function.Bijective f := (show f = IsLocalization.algEquiv M S Q by apply IsLocalization.ringHom_ext M; rw [hf]; ext; simp) ▸ (IsLocalization.algEquiv M S Q).toEquiv.bijective end AlgEquiv section liftAlgHom variable {A : Type*} [CommSemiring A] {R : Type*} [CommSemiring R] [Algebra A R] {M : Submonoid R} {S : Type*} [CommSemiring S] [Algebra A S] [Algebra R S] [IsScalarTower A R S] {P : Type*} [CommSemiring P] [Algebra A P] [IsLocalization M S] {f : R →ₐ[A] P} (hf : ∀ y : M, IsUnit (f y)) (x : S) include hf /-- `AlgHom` version of `IsLocalization.lift`. -/ noncomputable def liftAlgHom : S →ₐ[A] P where __ := lift hf commutes' r := show lift hf (algebraMap A S r) = _ by simp [IsScalarTower.algebraMap_apply A R S] theorem liftAlgHom_toRingHom : (liftAlgHom hf : S →ₐ[A] P).toRingHom = lift hf := rfl @[simp] theorem coe_liftAlgHom : ⇑(liftAlgHom hf : S →ₐ[A] P) = lift hf := rfl theorem liftAlgHom_apply : liftAlgHom hf x = lift hf x := rfl end liftAlgHom section AlgEquivOfAlgEquiv variable {A : Type*} [CommSemiring A] {R : Type*} [CommSemiring R] [Algebra A R] {M : Submonoid R} (S : Type*) [CommSemiring S] [Algebra A S] [Algebra R S] [IsScalarTower A R S] [IsLocalization M S] {P : Type*} [CommSemiring P] [Algebra A P] {T : Submonoid P} (Q : Type*) [CommSemiring Q] [Algebra A Q] [Algebra P Q] [IsScalarTower A P Q] [IsLocalization T Q] (h : R ≃ₐ[A] P) (H : Submonoid.map h M = T) include H /-- If `S`, `Q` are localizations of `R` and `P` at submonoids `M`, `T` respectively, an isomorphism `h : R ≃ₐ[A] P` such that `h(M) = T` induces an isomorphism of localizations `S ≃ₐ[A] Q`. -/ @[simps!] noncomputable def algEquivOfAlgEquiv : S ≃ₐ[A] Q where __ := ringEquivOfRingEquiv S Q h.toRingEquiv H commutes' _ := by dsimp; rw [IsScalarTower.algebraMap_apply A R S, map_eq, RingHom.coe_coe, AlgEquiv.commutes, IsScalarTower.algebraMap_apply A P Q] variable {S Q h} theorem algEquivOfAlgEquiv_eq_map : (algEquivOfAlgEquiv S Q h H : S →+* Q) = map Q (h : R →+* P) (M.le_comap_of_map_le (le_of_eq H)) := rfl theorem algEquivOfAlgEquiv_eq (x : R) : algEquivOfAlgEquiv S Q h H ((algebraMap R S) x) = algebraMap P Q (h x) := by simp set_option linter.docPrime false in theorem algEquivOfAlgEquiv_mk' (x : R) (y : M) : algEquivOfAlgEquiv S Q h H (mk' S x y) = mk' Q (h x) ⟨h y, show h y ∈ T from H ▸ Set.mem_image_of_mem h y.2⟩ := by simp [map_mk'] theorem algEquivOfAlgEquiv_symm : (algEquivOfAlgEquiv S Q h H).symm = algEquivOfAlgEquiv Q S h.symm (show Submonoid.map h.symm T = M by rw [← H, ← Submonoid.map_coe_toMulEquiv, AlgEquiv.symm_toMulEquiv, ← Submonoid.comap_equiv_eq_map_symm, ← Submonoid.map_coe_toMulEquiv, Submonoid.comap_map_eq_of_injective (h : R ≃* P).injective]) := rfl end AlgEquivOfAlgEquiv section at_units variable (R M) /-- The localization at a module of units is isomorphic to the ring. -/ noncomputable def atUnits (H : M ≤ IsUnit.submonoid R) : R ≃ₐ[R] S := by refine AlgEquiv.ofBijective (Algebra.ofId R S) ⟨?_, ?_⟩ · intro x y hxy obtain ⟨c, eq⟩ := (IsLocalization.eq_iff_exists M S).mp hxy obtain ⟨u, hu⟩ := H c.prop rwa [← hu, Units.mul_right_inj] at eq · intro y obtain ⟨⟨x, s⟩, eq⟩ := IsLocalization.surj M y obtain ⟨u, hu⟩ := H s.prop use x * u.inv dsimp [Algebra.ofId, RingHom.toFun_eq_coe, AlgHom.coe_mks] rw [RingHom.map_mul, ← eq, ← hu, mul_assoc, ← RingHom.map_mul] simp end at_units end IsLocalization section variable (M N) theorem isLocalization_of_algEquiv [Algebra R P] [IsLocalization M S] (h : S ≃ₐ[R] P) : IsLocalization M P := by constructor · intro y convert (IsLocalization.map_units S y).map h.toAlgHom.toRingHom.toMonoidHom exact (h.commutes y).symm · intro y obtain ⟨⟨x, s⟩, e⟩ := IsLocalization.surj M (h.symm y) apply_fun (show S → P from h) at e simp only [map_mul, h.apply_symm_apply, h.commutes] at e exact ⟨⟨x, s⟩, e⟩ · intro x y rw [← h.symm.toEquiv.injective.eq_iff, ← IsLocalization.eq_iff_exists M S, ← h.symm.commutes, ← h.symm.commutes] exact id theorem isLocalization_iff_of_algEquiv [Algebra R P] (h : S ≃ₐ[R] P) : IsLocalization M S ↔ IsLocalization M P := ⟨fun _ => isLocalization_of_algEquiv M h, fun _ => isLocalization_of_algEquiv M h.symm⟩ theorem isLocalization_iff_of_ringEquiv (h : S ≃+* P) : IsLocalization M S ↔ haveI := (h.toRingHom.comp <| algebraMap R S).toAlgebra; IsLocalization M P := letI := (h.toRingHom.comp <| algebraMap R S).toAlgebra isLocalization_iff_of_algEquiv M { h with commutes' := fun _ => rfl } variable (S) in /-- If an algebra is simultaneously localizations for two submonoids, then an arbitrary algebra is a localization of one submonoid iff it is a localization of the other. -/ theorem isLocalization_iff_of_isLocalization [IsLocalization M S] [IsLocalization N S] [Algebra R P] : IsLocalization M P ↔ IsLocalization N P := ⟨fun _ ↦ isLocalization_of_algEquiv N (algEquiv M S P), fun _ ↦ isLocalization_of_algEquiv M (algEquiv N S P)⟩ theorem iff_of_le_of_exists_dvd (N : Submonoid R) (h₁ : M ≤ N) (h₂ : ∀ n ∈ N, ∃ m ∈ M, n ∣ m) : IsLocalization M S ↔ IsLocalization N S := have : IsLocalization N (Localization M) := of_le_of_exists_dvd _ _ h₁ h₂ isLocalization_iff_of_isLocalization _ _ (Localization M) end variable (M) /-- If `S₁` is the localization of `R` at `M₁` and `S₂` is the localization of `R` at `M₂`, then every localization `T` of `S₂` at `M₁` is also a localization of `S₁` at `M₂`, in other words `M₁⁻¹M₂⁻¹R` can be identified with `M₂⁻¹M₁⁻¹R`. -/ lemma commutes (S₁ S₂ T : Type*) [CommSemiring S₁] [CommSemiring S₂] [CommSemiring T] [Algebra R S₁] [Algebra R S₂] [Algebra R T] [Algebra S₁ T] [Algebra S₂ T] [IsScalarTower R S₁ T] [IsScalarTower R S₂ T] (M₁ M₂ : Submonoid R) [IsLocalization M₁ S₁] [IsLocalization M₂ S₂] [IsLocalization (Algebra.algebraMapSubmonoid S₂ M₁) T] : IsLocalization (Algebra.algebraMapSubmonoid S₁ M₂) T where map_units' := by rintro ⟨m, ⟨a, ha, rfl⟩⟩ rw [← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₂ T] exact IsUnit.map _ (IsLocalization.map_units' ⟨a, ha⟩) surj' a := by obtain ⟨⟨y, -, m, hm, rfl⟩, hy⟩ := surj (M := Algebra.algebraMapSubmonoid S₂ M₁) a rw [← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₁ T] at hy obtain ⟨⟨z, n, hn⟩, hz⟩ := IsLocalization.surj (M := M₂) y have hunit : IsUnit (algebraMap R S₁ m) := map_units' ⟨m, hm⟩ use ⟨algebraMap R S₁ z * hunit.unit⁻¹, ⟨algebraMap R S₁ n, n, hn, rfl⟩⟩ rw [map_mul, ← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₂ T] conv_rhs => rw [← IsScalarTower.algebraMap_apply] rw [IsScalarTower.algebraMap_apply R S₂ T, ← hz, map_mul, ← hy] convert_to _ = a * (algebraMap S₂ T) ((algebraMap R S₂) n) * (algebraMap S₁ T) (((algebraMap R S₁) m) * hunit.unit⁻¹.val) · rw [map_mul] ring simp exists_of_eq {x y} hxy := by obtain ⟨r, s, d, hr, hs⟩ := IsLocalization.surj₂ M₁ S₁ x y apply_fun (· * algebraMap S₁ T (algebraMap R S₁ d)) at hxy simp_rw [← map_mul, hr, hs, ← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₂ T] at hxy obtain ⟨⟨-, c, hmc, rfl⟩, hc⟩ := exists_of_eq (M := Algebra.algebraMapSubmonoid S₂ M₁) hxy simp_rw [← map_mul] at hc obtain ⟨a, ha⟩ := IsLocalization.exists_of_eq (M := M₂) hc use ⟨algebraMap R S₁ a, a, a.property, rfl⟩ apply (map_units S₁ d).mul_right_cancel rw [mul_assoc, hr, mul_assoc, hs] apply (map_units S₁ ⟨c, hmc⟩).mul_right_cancel rw [← map_mul, ← map_mul, mul_assoc, mul_comm _ c, ha, map_mul, map_mul] ring end IsLocalization namespace Localization open IsLocalization theorem mk_natCast (m : ℕ) : (mk m 1 : Localization M) = m := by simpa using mk_algebraMap (R := R) (A := ℕ) _ variable [IsLocalization M S] section variable (S) (M) /-- The localization of `R` at `M` as a quotient type is isomorphic to any other localization. -/ @[simps!] noncomputable def algEquiv : Localization M ≃ₐ[R] S := IsLocalization.algEquiv M _ _ /-- The localization of a singleton is a singleton. Cannot be an instance due to metavariables. -/ noncomputable def _root_.IsLocalization.unique (R Rₘ) [CommSemiring R] [CommSemiring Rₘ] (M : Submonoid R) [Subsingleton R] [Algebra R Rₘ] [IsLocalization M Rₘ] : Unique Rₘ := have : Inhabited Rₘ := ⟨1⟩ (algEquiv M Rₘ).symm.injective.unique end nonrec theorem algEquiv_mk' (x : R) (y : M) : algEquiv M S (mk' (Localization M) x y) = mk' S x y := algEquiv_mk' _ _ nonrec theorem algEquiv_symm_mk' (x : R) (y : M) : (algEquiv M S).symm (mk' S x y) = mk' (Localization M) x y := algEquiv_symm_mk' _ _ theorem algEquiv_mk (x y) : algEquiv M S (mk x y) = mk' S x y := by rw [mk_eq_mk', algEquiv_mk'] theorem algEquiv_symm_mk (x : R) (y : M) : (algEquiv M S).symm (mk' S x y) = mk x y := by rw [mk_eq_mk', algEquiv_symm_mk'] lemma coe_algEquiv : (Localization.algEquiv M S : Localization M →+* S) = IsLocalization.map (M := M) (T := M) _ (RingHom.id R) le_rfl := rfl lemma coe_algEquiv_symm : ((Localization.algEquiv M S).symm : S →+* Localization M) = IsLocalization.map (M := M) (T := M) _ (RingHom.id R) le_rfl := rfl end Localization end CommSemiring section CommRing variable {R : Type*} [CommRing R] {M : Submonoid R} (S : Type*) [CommRing S] variable [Algebra R S] {P : Type*} [CommRing P] namespace Localization theorem mk_intCast (m : ℤ) : (mk m 1 : Localization M) = m := by simpa using mk_algebraMap (R := R) (A := ℤ) _ end Localization open IsLocalization /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ theorem IsField.localization_map_bijective {R Rₘ : Type*} [CommRing R] [CommRing Rₘ] {M : Submonoid R} (hM : (0 : R) ∉ M) (hR : IsField R) [Algebra R Rₘ] [IsLocalization M Rₘ] : Function.Bijective (algebraMap R Rₘ) := by letI := hR.toField replace hM := le_nonZeroDivisors_of_noZeroDivisors hM refine ⟨IsLocalization.injective _ hM, fun x => ?_⟩ obtain ⟨r, ⟨m, hm⟩, rfl⟩ := mk'_surjective M x obtain ⟨n, hn⟩ := hR.mul_inv_cancel (nonZeroDivisors.ne_zero <| hM hm) exact ⟨r * n, by rw [eq_mk'_iff_mul_eq, ← map_mul, mul_assoc, _root_.mul_comm n, hn, mul_one]⟩ /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ theorem Field.localization_map_bijective {K Kₘ : Type*} [Field K] [CommRing Kₘ] {M : Submonoid K} (hM : (0 : K) ∉ M) [Algebra K Kₘ] [IsLocalization M Kₘ] : Function.Bijective (algebraMap K Kₘ) := (Field.toIsField K).localization_map_bijective hM -- this looks weird due to the `letI` inside the above lemma, but trying to do it the other -- way round causes issues with defeq of instances, so this is actually easier. section Algebra variable {S} {Rₘ Sₘ : Type*} [CommRing Rₘ] [CommRing Sₘ] variable [Algebra R Rₘ] [IsLocalization M Rₘ] variable [Algebra S Sₘ] [i : IsLocalization (Algebra.algebraMapSubmonoid S M) Sₘ] include S section variable (S M) /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebraMap R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps. This instance can be helpful if you define `Sₘ := Localization (Algebra.algebraMapSubmonoid S M)`, however we will instead use the hypotheses `[Algebra Rₘ Sₘ] [IsScalarTower R Rₘ Sₘ]` in lemmas since the algebra structure may arise in different ways. -/ noncomputable def localizationAlgebra : Algebra Rₘ Sₘ := (map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) : Rₘ →+* Sₘ).toAlgebra end section variable [Algebra Rₘ Sₘ] [Algebra R Sₘ] [IsScalarTower R Rₘ Sₘ] [IsScalarTower R S Sₘ] variable (S Rₘ Sₘ) theorem IsLocalization.map_units_map_submonoid (y : M) : IsUnit (algebraMap R Sₘ y) := by rw [IsScalarTower.algebraMap_apply _ S] exact IsLocalization.map_units Sₘ ⟨algebraMap R S y, Algebra.mem_algebraMapSubmonoid_of_mem y⟩ -- can't be simp, as `S` only appears on the RHS theorem IsLocalization.algebraMap_mk' (x : R) (y : M) : algebraMap Rₘ Sₘ (IsLocalization.mk' Rₘ x y) = IsLocalization.mk' Sₘ (algebraMap R S x) ⟨algebraMap R S y, Algebra.mem_algebraMapSubmonoid_of_mem y⟩ := by rw [IsLocalization.eq_mk'_iff_mul_eq, Subtype.coe_mk, ← IsScalarTower.algebraMap_apply, ← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R Rₘ Sₘ, IsScalarTower.algebraMap_apply R Rₘ Sₘ, ← map_mul, mul_comm, IsLocalization.mul_mk'_eq_mk'_of_mul] exact congr_arg (algebraMap Rₘ Sₘ) (IsLocalization.mk'_mul_cancel_left x y) variable (M) /-- If the square below commutes, the bottom map is uniquely specified: ``` R → S ↓ ↓ Rₘ → Sₘ ``` -/ theorem IsLocalization.algebraMap_eq_map_map_submonoid : algebraMap Rₘ Sₘ = map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) := Eq.symm <| IsLocalization.map_unique _ (algebraMap Rₘ Sₘ) fun x => by rw [← IsScalarTower.algebraMap_apply R S Sₘ, ← IsScalarTower.algebraMap_apply R Rₘ Sₘ] /-- If the square below commutes, the bottom map is uniquely specified: ``` R → S ↓ ↓ Rₘ → Sₘ ``` -/ theorem IsLocalization.algebraMap_apply_eq_map_map_submonoid (x) : algebraMap Rₘ Sₘ x = map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) x := DFunLike.congr_fun (IsLocalization.algebraMap_eq_map_map_submonoid _ _ _ _) x theorem IsLocalization.lift_algebraMap_eq_algebraMap : IsLocalization.lift (M := M) (IsLocalization.map_units_map_submonoid S Sₘ) = algebraMap Rₘ Sₘ := IsLocalization.lift_unique _ fun _ => (IsScalarTower.algebraMap_apply _ _ _ _).symm end variable (Rₘ Sₘ) theorem localizationAlgebraMap_def : @algebraMap Rₘ Sₘ _ _ (localizationAlgebra M S) = map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) := rfl /-- Injectivity of the underlying `algebraMap` descends to the algebra induced by localization. -/ theorem localizationAlgebra_injective (hRS : Function.Injective (algebraMap R S)) : Function.Injective (@algebraMap Rₘ Sₘ _ _ (localizationAlgebra M S)) := have : IsLocalization (M.map (algebraMap R S)) Sₘ := i IsLocalization.map_injective_of_injective _ _ _ hRS end Algebra end CommRing
Mathlib/RingTheory/Localization/Basic.lean
1,140
1,141
/- 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.Analysis.InnerProductSpace.Continuous import Mathlib.Analysis.Normed.Module.Dual import Mathlib.MeasureTheory.Function.AEEqOfLIntegral import Mathlib.MeasureTheory.Function.StronglyMeasurable.Lp import Mathlib.MeasureTheory.Integral.Bochner.ContinuousLinearMap import Mathlib.Order.Filter.Ring /-! # From equality of integrals to equality of functions This file provides various statements of the general form "if two functions have the same integral on all sets, then they are equal almost everywhere". The different lemmas use various hypotheses on the class of functions, on the target space or on the possible finiteness of the measure. This file is about Bochner integrals. See the file `AEEqOfLIntegral` for Lebesgue integrals. ## Main statements All results listed below apply to two functions `f, g`, together with two main hypotheses, * `f` and `g` are integrable on all measurable sets with finite measure, * for all measurable sets `s` with finite measure, `∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ`. The conclusion is then `f =ᵐ[μ] g`. The main lemmas are: * `ae_eq_of_forall_setIntegral_eq_of_sigmaFinite`: case of a sigma-finite measure. * `AEFinStronglyMeasurable.ae_eq_of_forall_setIntegral_eq`: for functions which are `AEFinStronglyMeasurable`. * `Lp.ae_eq_of_forall_setIntegral_eq`: for elements of `Lp`, for `0 < p < ∞`. * `Integrable.ae_eq_of_forall_setIntegral_eq`: for integrable functions. For each of these results, we also provide a lemma about the equality of one function and 0. For example, `Lp.ae_eq_zero_of_forall_setIntegral_eq_zero`. Generally useful lemmas which are not related to integrals: * `ae_eq_zero_of_forall_inner`: if for all constants `c`, `fun x => inner c (f x) =ᵐ[μ] 0` then `f =ᵐ[μ] 0`. * `ae_eq_zero_of_forall_dual`: if for all constants `c` in the dual space, `fun x => c (f x) =ᵐ[μ] 0` then `f =ᵐ[μ] 0`. -/ open MeasureTheory TopologicalSpace NormedSpace Filter open scoped ENNReal NNReal MeasureTheory Topology namespace MeasureTheory section AeEqOfForall variable {α E 𝕜 : Type*} {m : MeasurableSpace α} {μ : Measure α} [RCLike 𝕜] theorem ae_eq_zero_of_forall_inner [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] [SecondCountableTopology E] {f : α → E} (hf : ∀ c : E, (fun x => (inner c (f x) : 𝕜)) =ᵐ[μ] 0) : f =ᵐ[μ] 0 := by let s := denseSeq E have hs : DenseRange s := denseRange_denseSeq E have hf' : ∀ᵐ x ∂μ, ∀ n : ℕ, inner (s n) (f x) = (0 : 𝕜) := ae_all_iff.mpr fun n => hf (s n) refine hf'.mono fun x hx => ?_ rw [Pi.zero_apply, ← @inner_self_eq_zero 𝕜] have h_closed : IsClosed {c : E | inner c (f x) = (0 : 𝕜)} := isClosed_eq (continuous_id.inner continuous_const) continuous_const exact @isClosed_property ℕ E _ s (fun c => inner c (f x) = (0 : 𝕜)) hs h_closed hx _ local notation "⟪" x ", " y "⟫" => y x variable (𝕜) theorem ae_eq_zero_of_forall_dual_of_isSeparable [NormedAddCommGroup E] [NormedSpace 𝕜 E] {t : Set E} (ht : TopologicalSpace.IsSeparable t) {f : α → E} (hf : ∀ c : Dual 𝕜 E, (fun x => ⟪f x, c⟫) =ᵐ[μ] 0) (h't : ∀ᵐ x ∂μ, f x ∈ t) : f =ᵐ[μ] 0 := by rcases ht with ⟨d, d_count, hd⟩ haveI : Encodable d := d_count.toEncodable have : ∀ x : d, ∃ g : E →L[𝕜] 𝕜, ‖g‖ ≤ 1 ∧ g x = ‖(x : E)‖ := fun x => exists_dual_vector'' 𝕜 (x : E) choose s hs using this have A : ∀ a : E, a ∈ t → (∀ x, ⟪a, s x⟫ = (0 : 𝕜)) → a = 0 := by intro a hat ha contrapose! ha have a_pos : 0 < ‖a‖ := by simp only [ha, norm_pos_iff, Ne, not_false_iff] have a_mem : a ∈ closure d := hd hat obtain ⟨x, hx⟩ : ∃ x : d, dist a x < ‖a‖ / 2 := by rcases Metric.mem_closure_iff.1 a_mem (‖a‖ / 2) (half_pos a_pos) with ⟨x, h'x, hx⟩ exact ⟨⟨x, h'x⟩, hx⟩ use x have I : ‖a‖ / 2 < ‖(x : E)‖ := by have : ‖a‖ ≤ ‖(x : E)‖ + ‖a - x‖ := norm_le_insert' _ _ have : ‖a - x‖ < ‖a‖ / 2 := by rwa [dist_eq_norm] at hx linarith intro h apply lt_irrefl ‖s x x‖ calc ‖s x x‖ = ‖s x (x - a)‖ := by simp only [h, sub_zero, ContinuousLinearMap.map_sub] _ ≤ 1 * ‖(x : E) - a‖ := ContinuousLinearMap.le_of_opNorm_le _ (hs x).1 _ _ < ‖a‖ / 2 := by rw [one_mul]; rwa [dist_eq_norm'] at hx _ < ‖(x : E)‖ := I _ = ‖s x x‖ := by rw [(hs x).2, RCLike.norm_coe_norm] have hfs : ∀ y : d, ∀ᵐ x ∂μ, ⟪f x, s y⟫ = (0 : 𝕜) := fun y => hf (s y) have hf' : ∀ᵐ x ∂μ, ∀ y : d, ⟪f x, s y⟫ = (0 : 𝕜) := by rwa [ae_all_iff] filter_upwards [hf', h't] with x hx h'x exact A (f x) h'x hx theorem ae_eq_zero_of_forall_dual [NormedAddCommGroup E] [NormedSpace 𝕜 E] [SecondCountableTopology E] {f : α → E} (hf : ∀ c : Dual 𝕜 E, (fun x => ⟪f x, c⟫) =ᵐ[μ] 0) : f =ᵐ[μ] 0 := ae_eq_zero_of_forall_dual_of_isSeparable 𝕜 (.of_separableSpace Set.univ) hf (Eventually.of_forall fun _ => Set.mem_univ _) variable {𝕜} end AeEqOfForall variable {α E : Type*} {m m0 : MeasurableSpace α} {μ : Measure α} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E] {p : ℝ≥0∞} section AeEqOfForallSetIntegralEq section Real variable {f : α → ℝ} theorem ae_nonneg_of_forall_setIntegral_nonneg (hf : Integrable f μ) (hf_zero : ∀ s, MeasurableSet s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := by simp_rw [EventuallyLE, Pi.zero_apply] rw [ae_const_le_iff_forall_lt_measure_zero] intro b hb_neg let s := {x | f x ≤ b} have hs : NullMeasurableSet s μ := nullMeasurableSet_le hf.1.aemeasurable aemeasurable_const have mus : μ s < ∞ := Integrable.measure_le_lt_top hf hb_neg have h_int_gt : (∫ x in s, f x ∂μ) ≤ b * μ.real s := by have h_const_le : (∫ x in s, f x ∂μ) ≤ ∫ _ in s, b ∂μ := by refine setIntegral_mono_ae_restrict hf.integrableOn (integrableOn_const.mpr (Or.inr mus)) ?_ rw [EventuallyLE, ae_restrict_iff₀ (hs.mono μ.restrict_le_self)] exact Eventually.of_forall fun x hxs => hxs rwa [setIntegral_const, smul_eq_mul, mul_comm] at h_const_le contrapose! h_int_gt with H calc b * μ.real s < 0 := mul_neg_of_neg_of_pos hb_neg <| ENNReal.toReal_pos H mus.ne _ ≤ ∫ x in s, f x ∂μ := by rw [← μ.restrict_toMeasurable mus.ne] exact hf_zero _ (measurableSet_toMeasurable ..) (by rwa [measure_toMeasurable]) theorem ae_le_of_forall_setIntegral_le {f g : α → ℝ} (hf : Integrable f μ) (hg : Integrable g μ) (hf_le : ∀ s, MeasurableSet s → μ s < ∞ → (∫ x in s, f x ∂μ) ≤ ∫ x in s, g x ∂μ) : f ≤ᵐ[μ] g := by rw [← eventually_sub_nonneg] refine ae_nonneg_of_forall_setIntegral_nonneg (hg.sub hf) fun s hs => ?_ rw [integral_sub' hg.integrableOn hf.integrableOn, sub_nonneg] exact hf_le s hs theorem ae_nonneg_restrict_of_forall_setIntegral_nonneg_inter {f : α → ℝ} {t : Set α} (hf : IntegrableOn f t μ) (hf_zero : ∀ s, MeasurableSet s → μ (s ∩ t) < ∞ → 0 ≤ ∫ x in s ∩ t, f x ∂μ) : 0 ≤ᵐ[μ.restrict t] f := by refine ae_nonneg_of_forall_setIntegral_nonneg hf fun s hs h's => ?_ simp_rw [Measure.restrict_restrict hs] apply hf_zero s hs rwa [Measure.restrict_apply hs] at h's theorem ae_nonneg_of_forall_setIntegral_nonneg_of_sigmaFinite [SigmaFinite μ] {f : α → ℝ} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s, MeasurableSet s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := by apply ae_of_forall_measure_lt_top_ae_restrict intro t t_meas t_lt_top apply ae_nonneg_restrict_of_forall_setIntegral_nonneg_inter (hf_int_finite t t_meas t_lt_top) intro s s_meas _ exact hf_zero _ (s_meas.inter t_meas) (lt_of_le_of_lt (measure_mono (Set.inter_subset_right)) t_lt_top) theorem AEFinStronglyMeasurable.ae_nonneg_of_forall_setIntegral_nonneg {f : α → ℝ} (hf : AEFinStronglyMeasurable f μ) (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s, MeasurableSet s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := by let t := hf.sigmaFiniteSet suffices 0 ≤ᵐ[μ.restrict t] f from ae_of_ae_restrict_of_ae_restrict_compl _ this hf.ae_eq_zero_compl.symm.le haveI : SigmaFinite (μ.restrict t) := hf.sigmaFinite_restrict refine ae_nonneg_of_forall_setIntegral_nonneg_of_sigmaFinite (fun s hs hμts => ?_) fun s hs hμts => ?_ · rw [IntegrableOn, Measure.restrict_restrict hs] rw [Measure.restrict_apply hs] at hμts exact hf_int_finite (s ∩ t) (hs.inter hf.measurableSet) hμts · rw [Measure.restrict_restrict hs] rw [Measure.restrict_apply hs] at hμts exact hf_zero (s ∩ t) (hs.inter hf.measurableSet) hμts theorem ae_nonneg_restrict_of_forall_setIntegral_nonneg {f : α → ℝ} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s, MeasurableSet s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) {t : Set α} (ht : MeasurableSet t) (hμt : μ t ≠ ∞) : 0 ≤ᵐ[μ.restrict t] f := by refine ae_nonneg_restrict_of_forall_setIntegral_nonneg_inter (hf_int_finite t ht (lt_top_iff_ne_top.mpr hμt)) fun s hs _ => ?_ refine hf_zero (s ∩ t) (hs.inter ht) ?_ exact (measure_mono Set.inter_subset_right).trans_lt (lt_top_iff_ne_top.mpr hμt) theorem ae_eq_zero_restrict_of_forall_setIntegral_eq_zero_real {f : α → ℝ} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) {t : Set α} (ht : MeasurableSet t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] 0 := by suffices h_and : f ≤ᵐ[μ.restrict t] 0 ∧ 0 ≤ᵐ[μ.restrict t] f from h_and.1.mp (h_and.2.mono fun x hx1 hx2 => le_antisymm hx2 hx1) refine ⟨?_, ae_nonneg_restrict_of_forall_setIntegral_nonneg hf_int_finite (fun s hs hμs => (hf_zero s hs hμs).symm.le) ht hμt⟩ suffices h_neg : 0 ≤ᵐ[μ.restrict t] -f by refine h_neg.mono fun x hx => ?_ rw [Pi.neg_apply] at hx simpa using hx refine ae_nonneg_restrict_of_forall_setIntegral_nonneg (fun s hs hμs => (hf_int_finite s hs hμs).neg) (fun s hs hμs => ?_) ht hμt simp_rw [Pi.neg_apply] rw [integral_neg, neg_nonneg] exact (hf_zero s hs hμs).le end Real theorem ae_eq_zero_restrict_of_forall_setIntegral_eq_zero {f : α → E} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) {t : Set α} (ht : MeasurableSet t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] 0 := by rcases (hf_int_finite t ht hμt.lt_top).aestronglyMeasurable.isSeparable_ae_range with ⟨u, u_sep, hu⟩ refine ae_eq_zero_of_forall_dual_of_isSeparable ℝ u_sep (fun c => ?_) hu refine ae_eq_zero_restrict_of_forall_setIntegral_eq_zero_real ?_ ?_ ht hμt · intro s hs hμs exact ContinuousLinearMap.integrable_comp c (hf_int_finite s hs hμs) · intro s hs hμs rw [ContinuousLinearMap.integral_comp_comm c (hf_int_finite s hs hμs), hf_zero s hs hμs] exact ContinuousLinearMap.map_zero _ theorem ae_eq_restrict_of_forall_setIntegral_eq {f g : α → E} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hg_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn g s μ) (hfg_zero : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) {t : Set α} (ht : MeasurableSet t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] g := by rw [← sub_ae_eq_zero] have hfg' : ∀ s : Set α, MeasurableSet s → μ s < ∞ → (∫ x in s, (f - g) x ∂μ) = 0 := by intro s hs hμs rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs)] exact sub_eq_zero.mpr (hfg_zero s hs hμs) have hfg_int : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn (f - g) s μ := fun s hs hμs => (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs) exact ae_eq_zero_restrict_of_forall_setIntegral_eq_zero hfg_int hfg' ht hμt theorem ae_eq_zero_of_forall_setIntegral_eq_of_sigmaFinite [SigmaFinite μ] {f : α → E} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := by let S := spanningSets μ rw [← @Measure.restrict_univ _ _ μ, ← iUnion_spanningSets μ, EventuallyEq, ae_iff, Measure.restrict_apply' (MeasurableSet.iUnion (measurableSet_spanningSets μ))] rw [Set.inter_iUnion, measure_iUnion_null_iff] intro n have h_meas_n : MeasurableSet (S n) := measurableSet_spanningSets μ n have hμn : μ (S n) < ∞ := measure_spanningSets_lt_top μ n rw [← Measure.restrict_apply' h_meas_n] exact ae_eq_zero_restrict_of_forall_setIntegral_eq_zero hf_int_finite hf_zero h_meas_n hμn.ne theorem ae_eq_of_forall_setIntegral_eq_of_sigmaFinite [SigmaFinite μ] {f g : α → E} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hg_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn g s μ) (hfg_eq : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := by rw [← sub_ae_eq_zero] have hfg : ∀ s : Set α, MeasurableSet s → μ s < ∞ → (∫ x in s, (f - g) x ∂μ) = 0 := by intro s hs hμs rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), sub_eq_zero.mpr (hfg_eq s hs hμs)] have hfg_int : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn (f - g) s μ := fun s hs hμs => (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs) exact ae_eq_zero_of_forall_setIntegral_eq_of_sigmaFinite hfg_int hfg theorem AEFinStronglyMeasurable.ae_eq_zero_of_forall_setIntegral_eq_zero {f : α → E} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf : AEFinStronglyMeasurable f μ) : f =ᵐ[μ] 0 := by let t := hf.sigmaFiniteSet suffices f =ᵐ[μ.restrict t] 0 from ae_of_ae_restrict_of_ae_restrict_compl _ this hf.ae_eq_zero_compl haveI : SigmaFinite (μ.restrict t) := hf.sigmaFinite_restrict refine ae_eq_zero_of_forall_setIntegral_eq_of_sigmaFinite ?_ ?_ · intro s hs hμs rw [IntegrableOn, Measure.restrict_restrict hs] rw [Measure.restrict_apply hs] at hμs exact hf_int_finite _ (hs.inter hf.measurableSet) hμs · intro s hs hμs rw [Measure.restrict_restrict hs] rw [Measure.restrict_apply hs] at hμs exact hf_zero _ (hs.inter hf.measurableSet) hμs theorem AEFinStronglyMeasurable.ae_eq_of_forall_setIntegral_eq {f g : α → E} (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hg_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn g s μ) (hfg_eq : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hf : AEFinStronglyMeasurable f μ) (hg : AEFinStronglyMeasurable g μ) : f =ᵐ[μ] g := by rw [← sub_ae_eq_zero] have hfg : ∀ s : Set α, MeasurableSet s → μ s < ∞ → (∫ x in s, (f - g) x ∂μ) = 0 := by intro s hs hμs rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), sub_eq_zero.mpr (hfg_eq s hs hμs)] have hfg_int : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn (f - g) s μ := fun s hs hμs => (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs) exact (hf.sub hg).ae_eq_zero_of_forall_setIntegral_eq_zero hfg_int hfg theorem Lp.ae_eq_zero_of_forall_setIntegral_eq_zero (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := AEFinStronglyMeasurable.ae_eq_zero_of_forall_setIntegral_eq_zero hf_int_finite hf_zero (Lp.finStronglyMeasurable _ hp_ne_zero hp_ne_top).aefinStronglyMeasurable theorem Lp.ae_eq_of_forall_setIntegral_eq (f g : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn f s μ) (hg_int_finite : ∀ s, MeasurableSet s → μ s < ∞ → IntegrableOn g s μ) (hfg : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := AEFinStronglyMeasurable.ae_eq_of_forall_setIntegral_eq hf_int_finite hg_int_finite hfg (Lp.finStronglyMeasurable _ hp_ne_zero hp_ne_top).aefinStronglyMeasurable (Lp.finStronglyMeasurable _ hp_ne_zero hp_ne_top).aefinStronglyMeasurable theorem ae_eq_zero_of_forall_setIntegral_eq_of_finStronglyMeasurable_trim (hm : m ≤ m0) {f : α → E} (hf_int_finite : ∀ s, MeasurableSet[m] s → μ s < ∞ → IntegrableOn f s μ) (hf_zero : ∀ s : Set α, MeasurableSet[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf : FinStronglyMeasurable f (μ.trim hm)) : f =ᵐ[μ] 0 := by obtain ⟨t, ht_meas, htf_zero, htμ⟩ := hf.exists_set_sigmaFinite haveI : SigmaFinite ((μ.restrict t).trim hm) := by rwa [restrict_trim hm μ ht_meas] at htμ have htf_zero : f =ᵐ[μ.restrict tᶜ] 0 := by rw [EventuallyEq, ae_restrict_iff' (MeasurableSet.compl (hm _ ht_meas))] exact Eventually.of_forall htf_zero have hf_meas_m : StronglyMeasurable[m] f := hf.stronglyMeasurable suffices f =ᵐ[μ.restrict t] 0 from ae_of_ae_restrict_of_ae_restrict_compl _ this htf_zero refine measure_eq_zero_of_trim_eq_zero hm ?_ refine ae_eq_zero_of_forall_setIntegral_eq_of_sigmaFinite ?_ ?_ · intro s hs hμs unfold IntegrableOn rw [restrict_trim hm (μ.restrict t) hs, Measure.restrict_restrict (hm s hs)] rw [← restrict_trim hm μ ht_meas, Measure.restrict_apply hs, trim_measurableSet_eq hm (hs.inter ht_meas)] at hμs refine Integrable.trim hm ?_ hf_meas_m exact hf_int_finite _ (hs.inter ht_meas) hμs · intro s hs hμs rw [restrict_trim hm (μ.restrict t) hs, Measure.restrict_restrict (hm s hs)] rw [← restrict_trim hm μ ht_meas, Measure.restrict_apply hs, trim_measurableSet_eq hm (hs.inter ht_meas)] at hμs rw [← integral_trim hm hf_meas_m] exact hf_zero _ (hs.inter ht_meas) hμs theorem Integrable.ae_eq_zero_of_forall_setIntegral_eq_zero {f : α → E} (hf : Integrable f μ) (hf_zero : ∀ s, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := by have hf_Lp : MemLp f 1 μ := memLp_one_iff_integrable.mpr hf let f_Lp := hf_Lp.toLp f have hf_f_Lp : f =ᵐ[μ] f_Lp := (MemLp.coeFn_toLp hf_Lp).symm refine hf_f_Lp.trans ?_ refine Lp.ae_eq_zero_of_forall_setIntegral_eq_zero f_Lp one_ne_zero ENNReal.coe_ne_top ?_ ?_ · exact fun s _ _ => Integrable.integrableOn (L1.integrable_coeFn _) · intro s hs hμs rw [integral_congr_ae (ae_restrict_of_ae hf_f_Lp.symm)] exact hf_zero s hs hμs theorem Integrable.ae_eq_of_forall_setIntegral_eq (f g : α → E) (hf : Integrable f μ) (hg : Integrable g μ) (hfg : ∀ s : Set α, MeasurableSet s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := by rw [← sub_ae_eq_zero] have hfg' : ∀ s : Set α, MeasurableSet s → μ s < ∞ → (∫ x in s, (f - g) x ∂μ) = 0 := by intro s hs hμs rw [integral_sub' hf.integrableOn hg.integrableOn] exact sub_eq_zero.mpr (hfg s hs hμs) exact Integrable.ae_eq_zero_of_forall_setIntegral_eq_zero (hf.sub hg) hfg' variable {β : Type*} [TopologicalSpace β] [MeasurableSpace β] [BorelSpace β] /-- If an integrable function has zero integral on all closed sets, then it is zero almost everywhere. -/ lemma ae_eq_zero_of_forall_setIntegral_isClosed_eq_zero {μ : Measure β} {f : β → E} (hf : Integrable f μ) (h'f : ∀ (s : Set β), IsClosed s → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := by suffices ∀ s, MeasurableSet s → ∫ x in s, f x ∂μ = 0 from hf.ae_eq_zero_of_forall_setIntegral_eq_zero (fun s hs _ ↦ this s hs) have A : ∀ (t : Set β), MeasurableSet t → ∫ (x : β) in t, f x ∂μ = 0 → ∫ (x : β) in tᶜ, f x ∂μ = 0 := by intro t t_meas ht have I : ∫ x, f x ∂μ = 0 := by rw [← setIntegral_univ]; exact h'f _ isClosed_univ simpa [ht, I] using integral_add_compl t_meas hf intro s hs induction s, hs using MeasurableSet.induction_on_open with | isOpen U hU => exact compl_compl U ▸ A _ hU.measurableSet.compl (h'f _ hU.isClosed_compl) | compl s hs ihs => exact A s hs ihs | iUnion g g_disj g_meas hg => simp [integral_iUnion g_meas g_disj hf.integrableOn, hg] /-- If an integrable function has zero integral on all compact sets in a sigma-compact space, then it is zero almost everywhere. -/ lemma ae_eq_zero_of_forall_setIntegral_isCompact_eq_zero [SigmaCompactSpace β] [R1Space β] {μ : Measure β} {f : β → E} (hf : Integrable f μ) (h'f : ∀ (s : Set β), IsCompact s → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := by apply ae_eq_zero_of_forall_setIntegral_isClosed_eq_zero hf (fun s hs ↦ ?_) let t : ℕ → Set β := fun n ↦ closure (compactCovering β n) ∩ s suffices H : Tendsto (fun n ↦ ∫ x in t n, f x ∂μ) atTop (𝓝 (∫ x in s, f x ∂μ)) by have A : ∀ n, ∫ x in t n, f x ∂μ = 0 := fun n ↦ h'f _ ((isCompact_compactCovering β n).closure.inter_right hs) simp_rw [A, tendsto_const_nhds_iff] at H exact H.symm have B : s = ⋃ n, t n := by rw [← Set.iUnion_inter, iUnion_closure_compactCovering, Set.univ_inter] rw [B] apply tendsto_setIntegral_of_monotone · intros n exact (isClosed_closure.inter hs).measurableSet · intros m n hmn simp only [t, Set.le_iff_subset] gcongr · exact hf.integrableOn /-- If a locally integrable function has zero integral on all compact sets in a sigma-compact space, then it is zero almost everywhere. -/ lemma ae_eq_zero_of_forall_setIntegral_isCompact_eq_zero' [SigmaCompactSpace β] [R1Space β] {μ : Measure β} {f : β → E} (hf : LocallyIntegrable f μ) (h'f : ∀ (s : Set β), IsCompact s → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := by rw [← μ.restrict_univ, ← iUnion_closure_compactCovering] apply (ae_restrict_iUnion_iff _ _).2 (fun n ↦ ?_) apply ae_eq_zero_of_forall_setIntegral_isCompact_eq_zero · exact hf.integrableOn_isCompact (isCompact_compactCovering β n).closure · intro s hs rw [Measure.restrict_restrict' measurableSet_closure] exact h'f _ (hs.inter_right isClosed_closure) end AeEqOfForallSetIntegralEq end MeasureTheory
Mathlib/MeasureTheory/Function/AEEqOfIntegral.lean
489
501
/- 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) instance NF.zero : NF 0 := ⟨⟨0, NFBelow.zero⟩⟩ theorem NFBelow.oadd {e n a b} : NF e → NFBelow a (repr e) → repr e < b → NFBelow (oadd e n a) b | ⟨⟨_, h⟩⟩ => NFBelow.oadd' h theorem NFBelow.fst {e n a b} (h : NFBelow (ONote.oadd e n a) b) : NF e := by obtain - | ⟨h₁, h₂, h₃⟩ := h; exact ⟨⟨_, h₁⟩⟩ theorem NF.fst {e n a} : NF (oadd e n a) → NF e | ⟨⟨_, h⟩⟩ => h.fst theorem NFBelow.snd {e n a b} (h : NFBelow (ONote.oadd e n a) b) : NFBelow a (repr e) := by obtain - | ⟨h₁, h₂, h₃⟩ := h; exact h₂ theorem NF.snd' {e n a} : NF (oadd e n a) → NFBelow a (repr e) | ⟨⟨_, h⟩⟩ => h.snd theorem NF.snd {e n a} (h : NF (oadd e n a)) : NF a := ⟨⟨_, h.snd'⟩⟩ theorem NF.oadd {e a} (h₁ : NF e) (n) (h₂ : NFBelow a (repr e)) : NF (oadd e n a) := ⟨⟨_, NFBelow.oadd h₁ h₂ (lt_succ _)⟩⟩ instance NF.oadd_zero (e n) [h : NF e] : NF (ONote.oadd e n 0) := h.oadd _ NFBelow.zero theorem NFBelow.lt {e n a b} (h : NFBelow (ONote.oadd e n a) b) : repr e < b := by obtain - | ⟨h₁, h₂, h₃⟩ := h; exact h₃ theorem NFBelow_zero : ∀ {o}, NFBelow o 0 ↔ o = 0 | 0 => ⟨fun _ => rfl, fun _ => NFBelow.zero⟩ | oadd _ _ _ => ⟨fun h => (not_le_of_lt h.lt).elim (Ordinal.zero_le _), fun e => e.symm ▸ NFBelow.zero⟩ theorem NF.zero_of_zero {e n a} (h : NF (ONote.oadd e n a)) (e0 : e = 0) : a = 0 := by simpa [e0, NFBelow_zero] using h.snd' theorem NFBelow.repr_lt {o b} (h : NFBelow o b) : repr o < ω ^ b := by induction h with | zero => exact opow_pos _ omega0_pos | oadd' _ _ h₃ _ IH => rw [repr] apply ((add_lt_add_iff_left _).2 IH).trans_le rw [← mul_succ] apply (mul_le_mul_left' (succ_le_of_lt (nat_lt_omega0 _)) _).trans rw [← opow_succ] exact opow_le_opow_right omega0_pos (succ_le_of_lt h₃) theorem NFBelow.mono {o b₁ b₂} (bb : b₁ ≤ b₂) (h : NFBelow o b₁) : NFBelow o b₂ := by induction h with | zero => exact zero | oadd' h₁ h₂ h₃ _ _ => constructor; exacts [h₁, h₂, lt_of_lt_of_le h₃ bb] theorem NF.below_of_lt {e n a b} (H : repr e < b) : NF (ONote.oadd e n a) → NFBelow (ONote.oadd e n a) b | ⟨⟨b', h⟩⟩ => by (obtain - | ⟨h₁, h₂, h₃⟩ := h; exact NFBelow.oadd' h₁ h₂ H) theorem NF.below_of_lt' : ∀ {o b}, repr o < ω ^ b → NF o → NFBelow o b | 0, _, _, _ => NFBelow.zero | ONote.oadd _ _ _, _, H, h => h.below_of_lt <| (opow_lt_opow_iff_right one_lt_omega0).1 <| lt_of_le_of_lt (omega0_le_oadd _ _ _) H theorem nfBelow_ofNat : ∀ n, NFBelow (ofNat n) 1 | 0 => NFBelow.zero | Nat.succ _ => NFBelow.oadd NF.zero NFBelow.zero zero_lt_one instance nf_ofNat (n) : NF (ofNat n) := ⟨⟨_, nfBelow_ofNat n⟩⟩ instance nf_one : NF 1 := by rw [← ofNat_one]; infer_instance theorem oadd_lt_oadd_1 {e₁ n₁ o₁ e₂ n₂ o₂} (h₁ : NF (oadd e₁ n₁ o₁)) (h : e₁ < e₂) : oadd e₁ n₁ o₁ < oadd e₂ n₂ o₂ := @lt_of_lt_of_le _ _ (repr (oadd e₁ n₁ o₁)) _ _ (NF.below_of_lt h h₁).repr_lt (omega0_le_oadd e₂ n₂ o₂) theorem oadd_lt_oadd_2 {e o₁ o₂ : ONote} {n₁ n₂ : ℕ+} (h₁ : NF (oadd e n₁ o₁)) (h : (n₁ : ℕ) < n₂) : oadd e n₁ o₁ < oadd e n₂ o₂ := by simp only [lt_def, repr] refine lt_of_lt_of_le ((add_lt_add_iff_left _).2 h₁.snd'.repr_lt) (le_trans ?_ (le_add_right _ _)) rwa [← mul_succ,Ordinal.mul_le_mul_iff_left (opow_pos _ omega0_pos), succ_le_iff, Nat.cast_lt] theorem oadd_lt_oadd_3 {e n a₁ a₂} (h : a₁ < a₂) : oadd e n a₁ < oadd e n a₂ := by rw [lt_def]; unfold repr exact @add_lt_add_left _ _ _ _ (repr a₁) _ h _ theorem cmp_compares : ∀ (a b : ONote) [NF a] [NF b], (cmp a b).Compares a b | 0, 0, _, _ => rfl | oadd _ _ _, 0, _, _ => oadd_pos _ _ _ | 0, oadd _ _ _, _, _ => oadd_pos _ _ _ | o₁@(oadd e₁ n₁ a₁), o₂@(oadd e₂ n₂ a₂), h₁, h₂ => by -- TODO: golf rw [cmp] have IHe := @cmp_compares _ _ h₁.fst h₂.fst simp only [Ordering.Compares, gt_iff_lt] at IHe; revert IHe cases cmp e₁ e₂ case lt => intro IHe; exact oadd_lt_oadd_1 h₁ IHe case gt => intro IHe; exact oadd_lt_oadd_1 h₂ IHe case eq => intro IHe; dsimp at IHe; subst IHe unfold _root_.cmp; cases nh : cmpUsing (· < ·) (n₁ : ℕ) n₂ <;> rw [cmpUsing, ite_eq_iff, not_lt] at nh case lt => rcases nh with nh | nh · exact oadd_lt_oadd_2 h₁ nh.left · rw [ite_eq_iff] at nh; rcases nh.right with nh | nh <;> cases nh <;> contradiction case gt => rcases nh with nh | nh · cases nh; contradiction · obtain ⟨_, nh⟩ := nh rw [ite_eq_iff] at nh; rcases nh with nh | nh · exact oadd_lt_oadd_2 h₂ nh.left · cases nh; contradiction rcases nh with nh | nh · cases nh; contradiction obtain ⟨nhl, nhr⟩ := nh rw [ite_eq_iff] at nhr rcases nhr with nhr | nhr · cases nhr; contradiction obtain rfl := Subtype.eq (nhl.eq_of_not_lt nhr.1) have IHa := @cmp_compares _ _ h₁.snd h₂.snd revert IHa; cases cmp a₁ a₂ <;> intro IHa <;> dsimp at IHa case lt => exact oadd_lt_oadd_3 IHa case gt => exact oadd_lt_oadd_3 IHa subst IHa; exact rfl theorem repr_inj {a b} [NF a] [NF b] : repr a = repr b ↔ a = b := ⟨fun e => match cmp a b, cmp_compares a b with | Ordering.lt, (h : repr a < repr b) => (ne_of_lt h e).elim | Ordering.gt, (h : repr a > repr b)=> (ne_of_gt h e).elim | Ordering.eq, h => h, congr_arg _⟩ theorem NF.of_dvd_omega0_opow {b e n a} (h : NF (ONote.oadd e n a)) (d : ω ^ b ∣ repr (ONote.oadd e n a)) : b ≤ repr e ∧ ω ^ b ∣ repr a := by have := mt repr_inj.1 (fun h => by injection h : ONote.oadd e n a ≠ 0) have L := le_of_not_lt fun l => not_le_of_lt (h.below_of_lt l).repr_lt (le_of_dvd this d) simp only [repr] at d exact ⟨L, (dvd_add_iff <| (opow_dvd_opow _ L).mul_right _).1 d⟩ theorem NF.of_dvd_omega0 {e n a} (h : NF (ONote.oadd e n a)) : ω ∣ repr (ONote.oadd e n a) → repr e ≠ 0 ∧ ω ∣ repr a := by (rw [← opow_one ω, ← one_le_iff_ne_zero]; exact h.of_dvd_omega0_opow) /-- `TopBelow b o` asserts that the largest exponent in `o`, if it exists, is less than `b`. This is an auxiliary definition for decidability of `NF`. -/ def TopBelow (b : ONote) : ONote → Prop | 0 => True | oadd e _ _ => cmp e b = Ordering.lt instance decidableTopBelow : DecidableRel TopBelow := by intro b o cases o <;> delta TopBelow <;> infer_instance theorem nfBelow_iff_topBelow {b} [NF b] : ∀ {o}, NFBelow o (repr b) ↔ NF o ∧ TopBelow b o | 0 => ⟨fun h => ⟨⟨⟨_, h⟩⟩, trivial⟩, fun _ => NFBelow.zero⟩ | oadd _ _ _ => ⟨fun h => ⟨⟨⟨_, h⟩⟩, (@cmp_compares _ b h.fst _).eq_lt.2 h.lt⟩, fun ⟨h₁, h₂⟩ => h₁.below_of_lt <| (@cmp_compares _ b h₁.fst _).eq_lt.1 h₂⟩ instance decidableNF : DecidablePred NF | 0 => isTrue NF.zero | oadd e n a => by have := decidableNF e have := decidableNF a apply decidable_of_iff (NF e ∧ NF a ∧ TopBelow e a) rw [← and_congr_right fun h => @nfBelow_iff_topBelow _ h _] exact ⟨fun ⟨h₁, h₂⟩ => NF.oadd h₁ n h₂, fun h => ⟨h.fst, h.snd'⟩⟩ /-- Auxiliary definition for `add` -/ def addAux (e : ONote) (n : ℕ+) (o : ONote) : ONote := match o with | 0 => oadd e n 0 | o'@(oadd e' n' a') => match cmp e e' with | Ordering.lt => o' | Ordering.eq => oadd e (n + n') a' | Ordering.gt => oadd e n o' /-- Addition of ordinal notations (correct only for normal input) -/ def add : ONote → ONote → ONote | 0, o => o | oadd e n a, o => addAux e n (add a o) instance : Add ONote := ⟨add⟩ @[simp] theorem zero_add (o : ONote) : 0 + o = o := rfl theorem oadd_add (e n a o) : oadd e n a + o = addAux e n (a + o) := rfl /-- Subtraction of ordinal notations (correct only for normal input) -/ def sub : ONote → ONote → ONote | 0, _ => 0 | o, 0 => o | o₁@(oadd e₁ n₁ a₁), oadd e₂ n₂ a₂ => match cmp e₁ e₂ with | Ordering.lt => 0 | Ordering.gt => o₁ | Ordering.eq => match (n₁ : ℕ) - n₂ with | 0 => if n₁ = n₂ then sub a₁ a₂ else 0 | Nat.succ k => oadd e₁ k.succPNat a₁ instance : Sub ONote := ⟨sub⟩ theorem add_nfBelow {b} : ∀ {o₁ o₂}, NFBelow o₁ b → NFBelow o₂ b → NFBelow (o₁ + o₂) b | 0, _, _, h₂ => h₂ | oadd e n a, o, h₁, h₂ => by have h' := add_nfBelow (h₁.snd.mono <| le_of_lt h₁.lt) h₂ simp only [oadd_add]; revert h'; obtain - | ⟨e', n', a'⟩ := a + o <;> intro h' · exact NFBelow.oadd h₁.fst NFBelow.zero h₁.lt have : ((e.cmp e').Compares e e') := @cmp_compares _ _ h₁.fst h'.fst cases h : cmp e e' <;> dsimp [addAux] <;> simp only [h] · exact h' · simp only [h] at this subst e' exact NFBelow.oadd h'.fst h'.snd h'.lt · simp only [h] at this exact NFBelow.oadd h₁.fst (NF.below_of_lt this ⟨⟨_, h'⟩⟩) h₁.lt instance add_nf (o₁ o₂) : ∀ [NF o₁] [NF o₂], NF (o₁ + o₂) | ⟨⟨b₁, h₁⟩⟩, ⟨⟨b₂, h₂⟩⟩ => ⟨(le_total b₁ b₂).elim (fun h => ⟨b₂, add_nfBelow (h₁.mono h) h₂⟩) fun h => ⟨b₁, add_nfBelow h₁ (h₂.mono h)⟩⟩ @[simp] theorem repr_add : ∀ (o₁ o₂) [NF o₁] [NF o₂], repr (o₁ + o₂) = repr o₁ + repr o₂ | 0, o, _, _ => by simp | oadd e n a, o, h₁, h₂ => by haveI := h₁.snd; have h' := repr_add a o conv_lhs at h' => simp [HAdd.hAdd, Add.add] have nf := ONote.add_nf a o conv at nf => simp [HAdd.hAdd, Add.add] conv in _ + o => simp [HAdd.hAdd, Add.add] rcases h : add a o with - | ⟨e', n', a'⟩ <;> simp only [Add.add, add, addAux, h'.symm, h, add_assoc, repr_zero, repr] at nf h₁ ⊢ have := h₁.fst; haveI := nf.fst; have ee := cmp_compares e e' cases he : cmp e e' <;> simp only [he, Ordering.compares_gt, Ordering.compares_lt, Ordering.compares_eq, repr, gt_iff_lt, PNat.add_coe, Nat.cast_add] at ee ⊢ · rw [← add_assoc, @add_absorp _ (repr e') (ω ^ repr e' * (n' : ℕ))] · have := (h₁.below_of_lt ee).repr_lt unfold repr at this cases he' : e' <;> simp only [he', zero_def, opow_zero, repr, gt_iff_lt] at this ⊢ <;> exact lt_of_le_of_lt (le_add_right _ _) this · simpa using (Ordinal.mul_le_mul_iff_left <| opow_pos (repr e') omega0_pos).2 (Nat.cast_le.2 n'.pos) · rw [ee, ← add_assoc, ← mul_add] theorem sub_nfBelow : ∀ {o₁ o₂ b}, NFBelow o₁ b → NF o₂ → NFBelow (o₁ - o₂) b | 0, o, b, _, h₂ => by cases o <;> exact NFBelow.zero | oadd _ _ _, 0, _, h₁, _ => h₁ | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, b, h₁, h₂ => by have h' := sub_nfBelow h₁.snd h₂.snd simp only [HSub.hSub, Sub.sub, sub] at h' ⊢ have := @cmp_compares _ _ h₁.fst h₂.fst cases h : cmp e₁ e₂ · apply NFBelow.zero · rw [Nat.sub_eq] simp only [h, Ordering.compares_eq] at this subst e₂ cases (n₁ : ℕ) - n₂ · by_cases en : n₁ = n₂ <;> simp only [en, ↓reduceIte] · exact h'.mono (le_of_lt h₁.lt) · exact NFBelow.zero · exact NFBelow.oadd h₁.fst h₁.snd h₁.lt · exact h₁ instance sub_nf (o₁ o₂) : ∀ [NF o₁] [NF o₂], NF (o₁ - o₂) | ⟨⟨b₁, h₁⟩⟩, h₂ => ⟨⟨b₁, sub_nfBelow h₁ h₂⟩⟩ @[simp] theorem repr_sub : ∀ (o₁ o₂) [NF o₁] [NF o₂], repr (o₁ - o₂) = repr o₁ - repr o₂ | 0, o, _, h₂ => by cases o <;> exact (Ordinal.zero_sub _).symm | oadd _ _ _, 0, _, _ => (Ordinal.sub_zero _).symm | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, h₁, h₂ => by haveI := h₁.snd; haveI := h₂.snd; have h' := repr_sub a₁ a₂ conv_lhs at h' => dsimp [HSub.hSub, Sub.sub, sub] conv_lhs => dsimp only [HSub.hSub, Sub.sub]; dsimp only [sub] have ee := @cmp_compares _ _ h₁.fst h₂.fst cases h : cmp e₁ e₂ <;> simp only [h] at ee · rw [Ordinal.sub_eq_zero_iff_le.2] · rfl exact le_of_lt (oadd_lt_oadd_1 h₁ ee) · change e₁ = e₂ at ee subst e₂ dsimp only cases mn : (n₁ : ℕ) - n₂ <;> dsimp only · by_cases en : n₁ = n₂ · simpa [en] · simp only [en, ite_false] exact (Ordinal.sub_eq_zero_iff_le.2 <| le_of_lt <| oadd_lt_oadd_2 h₁ <| lt_of_le_of_ne (tsub_eq_zero_iff_le.1 mn) (mt PNat.eq en)).symm · simp [Nat.succPNat] rw [(tsub_eq_iff_eq_add_of_le <| le_of_lt <| Nat.lt_of_sub_eq_succ mn).1 mn, add_comm, Nat.cast_add, mul_add, add_assoc, add_sub_add_cancel] refine (Ordinal.sub_eq_of_add_eq <| add_absorp h₂.snd'.repr_lt <| le_trans ?_ (le_add_right _ _)).symm exact Ordinal.le_mul_left _ (Nat.cast_lt.2 <| Nat.succ_pos _) · exact (Ordinal.sub_eq_of_add_eq <| add_absorp (h₂.below_of_lt ee).repr_lt <| omega0_le_oadd _ _ _).symm /-- Multiplication of ordinal notations (correct only for normal input) -/ def mul : ONote → ONote → ONote | 0, _ => 0 | _, 0 => 0 | o₁@(oadd e₁ n₁ a₁), oadd e₂ n₂ a₂ => if e₂ = 0 then oadd e₁ (n₁ * n₂) a₁ else oadd (e₁ + e₂) n₂ (mul o₁ a₂) instance : Mul ONote := ⟨mul⟩ instance : MulZeroClass ONote where mul := (· * ·) zero := 0 zero_mul o := by cases o <;> rfl mul_zero o := by cases o <;> rfl theorem oadd_mul (e₁ n₁ a₁ e₂ n₂ a₂) : oadd e₁ n₁ a₁ * oadd e₂ n₂ a₂ = if e₂ = 0 then oadd e₁ (n₁ * n₂) a₁ else oadd (e₁ + e₂) n₂ (oadd e₁ n₁ a₁ * a₂) := rfl theorem oadd_mul_nfBelow {e₁ n₁ a₁ b₁} (h₁ : NFBelow (oadd e₁ n₁ a₁) b₁) : ∀ {o₂ b₂}, NFBelow o₂ b₂ → NFBelow (oadd e₁ n₁ a₁ * o₂) (repr e₁ + b₂) | 0, _, _ => NFBelow.zero | oadd e₂ n₂ a₂, b₂, h₂ => by have IH := oadd_mul_nfBelow h₁ h₂.snd by_cases e0 : e₂ = 0 <;> simp only [e0, oadd_mul, ↓reduceIte] · apply NFBelow.oadd h₁.fst h₁.snd simpa using (add_lt_add_iff_left (repr e₁)).2 (lt_of_le_of_lt (Ordinal.zero_le _) h₂.lt) · haveI := h₁.fst haveI := h₂.fst apply NFBelow.oadd · infer_instance · rwa [repr_add] · rw [repr_add, add_lt_add_iff_left] exact h₂.lt instance mul_nf : ∀ (o₁ o₂) [NF o₁] [NF o₂], NF (o₁ * o₂) | 0, o, _, h₂ => by cases o <;> exact NF.zero | oadd _ _ _, _, ⟨⟨_, hb₁⟩⟩, ⟨⟨_, hb₂⟩⟩ => ⟨⟨_, oadd_mul_nfBelow hb₁ hb₂⟩⟩ @[simp] theorem repr_mul : ∀ (o₁ o₂) [NF o₁] [NF o₂], repr (o₁ * o₂) = repr o₁ * repr o₂ | 0, o, _, h₂ => by cases o <;> exact (zero_mul _).symm | oadd _ _ _, 0, _, _ => (mul_zero _).symm | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, h₁, h₂ => by have IH : repr (mul _ _) = _ := @repr_mul _ _ h₁ h₂.snd conv => lhs simp [(· * ·)] have ao : repr a₁ + ω ^ repr e₁ * (n₁ : ℕ) = ω ^ repr e₁ * (n₁ : ℕ) := by apply add_absorp h₁.snd'.repr_lt simpa using (Ordinal.mul_le_mul_iff_left <| opow_pos _ omega0_pos).2 (Nat.cast_le.2 n₁.2) by_cases e0 : e₂ = 0 · obtain ⟨x, xe⟩ := Nat.exists_eq_succ_of_ne_zero n₂.ne_zero simp only [Mul.mul, mul, e0, ↓reduceIte, repr, PNat.mul_coe, natCast_mul, opow_zero, one_mul] simp only [xe, h₂.zero_of_zero e0, repr, add_zero] rw [natCast_succ x, add_mul_succ _ ao, mul_assoc] · simp only [repr] haveI := h₁.fst haveI := h₂.fst simp only [Mul.mul, mul, e0, ite_false, repr.eq_2, repr_add, opow_add, IH, repr, mul_add] rw [← mul_assoc] congr 2 have := mt repr_inj.1 e0 rw [add_mul_limit ao (isLimit_opow_left isLimit_omega0 this), mul_assoc, mul_omega0_dvd (Nat.cast_pos'.2 n₁.pos) (nat_lt_omega0 _)] simpa using opow_dvd_opow ω (one_le_iff_ne_zero.2 this) /-- Calculate division and remainder of `o` mod `ω`: `split' o = (a, n)` means `o = ω * a + n`. -/ def split' : ONote → ONote × ℕ | 0 => (0, 0) | oadd e n a => if e = 0 then (0, n) else let (a', m) := split' a (oadd (e - 1) n a', m) /-- Calculate division and remainder of `o` mod `ω`: `split o = (a, n)` means `o = a + n`, where `ω ∣ a`. -/ def split : ONote → ONote × ℕ | 0 => (0, 0) | oadd e n a => if e = 0 then (0, n) else let (a', m) := split a (oadd e n a', m) /-- `scale x o` is the ordinal notation for `ω ^ x * o`. -/ def scale (x : ONote) : ONote → ONote | 0 => 0 | oadd e n a => oadd (x + e) n (scale x a) /-- `mulNat o n` is the ordinal notation for `o * n`. -/ def mulNat : ONote → ℕ → ONote | 0, _ => 0 | _, 0 => 0 | oadd e n a, m + 1 => oadd e (n * m.succPNat) a /-- Auxiliary definition to compute the ordinal notation for the ordinal exponentiation in `opow` -/ def opowAux (e a0 a : ONote) : ℕ → ℕ → ONote | _, 0 => 0 | 0, m + 1 => oadd e m.succPNat 0 | k + 1, m => scale (e + mulNat a0 k) a + (opowAux e a0 a k m) /-- Auxiliary definition to compute the ordinal notation for the ordinal exponentiation in `opow` -/ def opowAux2 (o₂ : ONote) (o₁ : ONote × ℕ) : ONote := match o₁ with | (0, 0) => if o₂ = 0 then 1 else 0 | (0, 1) => 1 | (0, m + 1) => let (b', k) := split' o₂ oadd b' (m.succPNat ^ k) 0 | (a@(oadd a0 _ _), m) => match split o₂ with | (b, 0) => oadd (a0 * b) 1 0 | (b, k + 1) => let eb := a0 * b scale (eb + mulNat a0 k) a + opowAux eb a0 (mulNat a m) k m /-- `opow o₁ o₂` calculates the ordinal notation for the ordinal exponential `o₁ ^ o₂`. -/ def opow (o₁ o₂ : ONote) : ONote := opowAux2 o₂ (split o₁) instance : Pow ONote ONote := ⟨opow⟩ theorem opow_def (o₁ o₂ : ONote) : o₁ ^ o₂ = opowAux2 o₂ (split o₁) := rfl theorem split_eq_scale_split' : ∀ {o o' m} [NF o], split' o = (o', m) → split o = (scale 1 o', m) | 0, o', m, _, p => by injection p; substs o' m; rfl | oadd e n a, o', m, h, p => by by_cases e0 : e = 0 <;> simp only [split', e0, ↓reduceIte, Prod.mk.injEq, split] at p ⊢ · rcases p with ⟨rfl, rfl⟩ exact ⟨rfl, rfl⟩ · revert p rcases h' : split' a with ⟨a', m'⟩ haveI := h.fst haveI := h.snd simp only [split_eq_scale_split' h', and_imp] have : 1 + (e - 1) = e := by refine repr_inj.1 ?_ simp only [repr_add, repr_one, Nat.cast_one, repr_sub] have := mt repr_inj.1 e0 exact Ordinal.add_sub_cancel_of_le <| one_le_iff_ne_zero.2 this intros substs o' m simp [scale, this] theorem nf_repr_split' : ∀ {o o' m} [NF o], split' o = (o', m) → NF o' ∧ repr o = ω * repr o' + m | 0, o', m, _, p => by injection p; substs o' m; simp [NF.zero] | oadd e n a, o', m, h, p => by by_cases e0 : e = 0 <;> simp [e0, split, split'] at p ⊢ · rcases p with ⟨rfl, rfl⟩ simp [h.zero_of_zero e0, NF.zero] · revert p rcases h' : split' a with ⟨a', m'⟩ haveI := h.fst haveI := h.snd obtain ⟨IH₁, IH₂⟩ := nf_repr_split' h' simp only [IH₂, and_imp] intros substs o' m have : (ω : Ordinal.{0}) ^ repr e = ω ^ (1 : Ordinal.{0}) * ω ^ (repr e - 1) := by have := mt repr_inj.1 e0 rw [← opow_add, Ordinal.add_sub_cancel_of_le (one_le_iff_ne_zero.2 this)] refine ⟨NF.oadd (by infer_instance) _ ?_, ?_⟩ · simp only [opow_one, repr_sub, repr_one, Nat.cast_one] at this ⊢ refine IH₁.below_of_lt' ((Ordinal.mul_lt_mul_iff_left omega0_pos).1 <| lt_of_le_of_lt (le_add_right _ m') ?_) rw [← this, ← IH₂] exact h.snd'.repr_lt · rw [this] simp [mul_add, mul_assoc, add_assoc] theorem scale_eq_mul (x) [NF x] : ∀ (o) [NF o], scale x o = oadd x 1 0 * o | 0, _ => rfl | oadd e n a, h => by simp only [HMul.hMul]; simp only [scale] haveI := h.snd by_cases e0 : e = 0 · simp_rw [scale_eq_mul] simp [Mul.mul, mul, scale_eq_mul, e0, h.zero_of_zero, show x + 0 = x from repr_inj.1 (by simp)] · simp [e0, Mul.mul, mul, scale_eq_mul, (· * ·)] instance nf_scale (x) [NF x] (o) [NF o] : NF (scale x o) := by rw [scale_eq_mul] infer_instance @[simp] theorem repr_scale (x) [NF x] (o) [NF o] : repr (scale x o) = ω ^ repr x * repr o := by simp only [scale_eq_mul, repr_mul, repr, PNat.one_coe, Nat.cast_one, mul_one, add_zero] theorem nf_repr_split {o o' m} [NF o] (h : split o = (o', m)) : NF o' ∧ repr o = repr o' + m := by rcases e : split' o with ⟨a, n⟩ obtain ⟨s₁, s₂⟩ := nf_repr_split' e rw [split_eq_scale_split' e] at h injection h; substs o' n simp only [repr_scale, repr_one, Nat.cast_one, opow_one, ← s₂, and_true] infer_instance theorem split_dvd {o o' m} [NF o] (h : split o = (o', m)) : ω ∣ repr o' := by rcases e : split' o with ⟨a, n⟩ rw [split_eq_scale_split' e] at h
injection h; subst o' cases nf_repr_split' e; simp theorem split_add_lt {o e n a m} [NF o] (h : split o = (oadd e n a, m)) : repr a + m < ω ^ repr e := by obtain ⟨h₁, h₂⟩ := nf_repr_split h obtain ⟨e0, d⟩ := h₁.of_dvd_omega0 (split_dvd h) apply principal_add_omega0_opow _ h₁.snd'.repr_lt (lt_of_lt_of_le (nat_lt_omega0 _) _) simpa using opow_le_opow_right omega0_pos (one_le_iff_ne_zero.2 e0) @[simp] theorem mulNat_eq_mul (n o) : mulNat o n = o * ofNat n := by cases o <;> cases n <;> rfl instance nf_mulNat (o) [NF o] (n) : NF (mulNat o n) := by simpa using ONote.mul_nf o (ofNat n) instance nf_opowAux (e a0 a) [NF e] [NF a0] [NF a] : ∀ k m, NF (opowAux e a0 a k m) := by intro k m unfold opowAux cases m with | zero => cases k <;> exact NF.zero | succ m => cases k with
Mathlib/SetTheory/Ordinal/Notation.lean
711
732
/- 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, Antoine Chambert-Loir -/ import Mathlib.Algebra.DirectSum.Finsupp import Mathlib.LinearAlgebra.DirectSum.TensorProduct import Mathlib.LinearAlgebra.Finsupp.SumProd /-! # Results on finitely supported functions. * `TensorProduct.finsuppLeft`, the tensor product of `ι →₀ M` and `N` is linearly equivalent to `ι →₀ M ⊗[R] N` * `TensorProduct.finsuppScalarLeft`, the tensor product of `ι →₀ R` and `N` is linearly equivalent to `ι →₀ N` * `TensorProduct.finsuppRight`, the tensor product of `M` and `ι →₀ N` is linearly equivalent to `ι →₀ M ⊗[R] N` * `TensorProduct.finsuppScalarRight`, the tensor product of `M` and `ι →₀ R` is linearly equivalent to `ι →₀ N` * `TensorProduct.finsuppLeft'`, if `M` is an `S`-module, then the tensor product of `ι →₀ M` and `N` is `S`-linearly equivalent to `ι →₀ M ⊗[R] N` * `finsuppTensorFinsupp`, the tensor product of `ι →₀ M` and `κ →₀ N` is linearly equivalent to `(ι × κ) →₀ (M ⊗ N)`. ## Case of MvPolynomial These functions apply to `MvPolynomial`, one can define ``` noncomputable def MvPolynomial.rTensor' : MvPolynomial σ S ⊗[R] N ≃ₗ[S] (σ →₀ ℕ) →₀ (S ⊗[R] N) := TensorProduct.finsuppLeft' noncomputable def MvPolynomial.rTensor : MvPolynomial σ R ⊗[R] N ≃ₗ[R] (σ →₀ ℕ) →₀ N := TensorProduct.finsuppScalarLeft ``` However, to be actually usable, these definitions need lemmas to be given in companion PR. ## Case of `Polynomial` `Polynomial` is a structure containing a `Finsupp`, so these functions can't be applied directly to `Polynomial`. Some linear equivs need to be added to mathlib for that. This belongs to a companion PR. ## TODO * generalize to `MonoidAlgebra`, `AlgHom ` * reprove `TensorProduct.finsuppLeft'` using existing heterobasic version of `TensorProduct.congr` -/ noncomputable section open DirectSum TensorProduct open Set LinearMap Submodule section TensorProduct variable (R : Type*) [CommSemiring R] (M : Type*) [AddCommMonoid M] [Module R M] (N : Type*) [AddCommMonoid N] [Module R N] namespace TensorProduct variable (ι : Type*) [DecidableEq ι] /-- The tensor product of `ι →₀ M` and `N` is linearly equivalent to `ι →₀ M ⊗[R] N` -/ noncomputable def finsuppLeft : (ι →₀ M) ⊗[R] N ≃ₗ[R] ι →₀ M ⊗[R] N := congr (finsuppLEquivDirectSum R M ι) (.refl R N) ≪≫ₗ directSumLeft R (fun _ ↦ M) N ≪≫ₗ (finsuppLEquivDirectSum R _ ι).symm variable {R M N ι} lemma finsuppLeft_apply_tmul (p : ι →₀ M) (n : N) : finsuppLeft R M N ι (p ⊗ₜ[R] n) = p.sum fun i m ↦ Finsupp.single i (m ⊗ₜ[R] n) := by induction p using Finsupp.induction_linear with | zero => simp | add f g hf hg => simp [add_tmul, map_add, hf, hg, Finsupp.sum_add_index] | single => simp [finsuppLeft] @[simp] lemma finsuppLeft_apply_tmul_apply (p : ι →₀ M) (n : N) (i : ι) : finsuppLeft R M N ι (p ⊗ₜ[R] n) i = p i ⊗ₜ[R] n := by rw [finsuppLeft_apply_tmul, Finsupp.sum_apply, Finsupp.sum_eq_single i (fun _ _ ↦ Finsupp.single_eq_of_ne) (by simp), Finsupp.single_eq_same] theorem finsuppLeft_apply (t : (ι →₀ M) ⊗[R] N) (i : ι) : finsuppLeft R M N ι t i = rTensor N (Finsupp.lapply i) t := by induction t with | zero => simp | tmul f n => simp only [finsuppLeft_apply_tmul_apply, rTensor_tmul, Finsupp.lapply_apply] | add x y hx hy => simp [map_add, hx, hy] @[simp] lemma finsuppLeft_symm_apply_single (i : ι) (m : M) (n : N) : (finsuppLeft R M N ι).symm (Finsupp.single i (m ⊗ₜ[R] n)) = Finsupp.single i m ⊗ₜ[R] n := by simp [finsuppLeft, Finsupp.lsum] variable (R M N ι) /-- The tensor product of `M` and `ι →₀ N` is linearly equivalent to `ι →₀ M ⊗[R] N` -/ noncomputable def finsuppRight : M ⊗[R] (ι →₀ N) ≃ₗ[R] ι →₀ M ⊗[R] N := congr (.refl R M) (finsuppLEquivDirectSum R N ι) ≪≫ₗ directSumRight R M (fun _ : ι ↦ N) ≪≫ₗ (finsuppLEquivDirectSum R _ ι).symm variable {R M N ι} lemma finsuppRight_apply_tmul (m : M) (p : ι →₀ N) : finsuppRight R M N ι (m ⊗ₜ[R] p) = p.sum fun i n ↦ Finsupp.single i (m ⊗ₜ[R] n) := by induction p using Finsupp.induction_linear with | zero => simp | add f g hf hg => simp [tmul_add, map_add, hf, hg, Finsupp.sum_add_index] | single => simp [finsuppRight] @[simp] lemma finsuppRight_apply_tmul_apply (m : M) (p : ι →₀ N) (i : ι) : finsuppRight R M N ι (m ⊗ₜ[R] p) i = m ⊗ₜ[R] p i := by rw [finsuppRight_apply_tmul, Finsupp.sum_apply, Finsupp.sum_eq_single i (fun _ _ ↦ Finsupp.single_eq_of_ne) (by simp), Finsupp.single_eq_same] theorem finsuppRight_apply (t : M ⊗[R] (ι →₀ N)) (i : ι) : finsuppRight R M N ι t i = lTensor M (Finsupp.lapply i) t := by induction t with | zero => simp | tmul m f => simp [finsuppRight_apply_tmul_apply] | add x y hx hy => simp [map_add, hx, hy] @[simp] lemma finsuppRight_symm_apply_single (i : ι) (m : M) (n : N) :
(finsuppRight R M N ι).symm (Finsupp.single i (m ⊗ₜ[R] n)) = m ⊗ₜ[R] Finsupp.single i n := by simp [finsuppRight, Finsupp.lsum] variable {S : Type*} [CommSemiring S] [Algebra R S]
Mathlib/LinearAlgebra/DirectSum/Finsupp.lean
144
148
/- Copyright (c) 2021 Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Christopher Hoskin -/ import Mathlib.Algebra.Jordan.Basic import Mathlib.Algebra.Module.Defs /-! # Symmetrized algebra A commutative multiplication on a real or complex space can be constructed from any multiplication by "symmetrization" i.e $$ a \circ b = \frac{1}{2}(ab + ba) $$ We provide the symmetrized version of a type `α` as `SymAlg α`, with notation `αˢʸᵐ`. ## Implementation notes The approach taken here is inspired by `Mathlib/Algebra/Opposites.lean`. We use Oxford Spellings (IETF en-GB-oxendict). ## References * [Hanche-Olsen and Størmer, Jordan Operator Algebras][hancheolsenstormer1984] -/ open Function /-- The symmetrized algebra (denoted as `αˢʸᵐ`) has the same underlying space as the original algebra `α`. -/ def SymAlg (α : Type*) : Type _ := α @[inherit_doc] postfix:max "ˢʸᵐ" => SymAlg namespace SymAlg variable {α : Type*} /-- The element of `SymAlg α` that represents `a : α`. -/ @[match_pattern] def sym : α ≃ αˢʸᵐ := Equiv.refl _ /-- The element of `α` represented by `x : αˢʸᵐ`. -/ -- Porting note (kmill): `pp_nodot` has no affect here -- unless RFC https://github.com/leanprover/lean4/issues/6178 leads to dot notation pp for CoeFun @[pp_nodot] def unsym : αˢʸᵐ ≃ α := Equiv.refl _ @[simp] theorem unsym_sym (a : α) : unsym (sym a) = a := rfl @[simp] theorem sym_unsym (a : α) : sym (unsym a) = a := rfl @[simp] theorem sym_comp_unsym : (sym : α → αˢʸᵐ) ∘ unsym = id := rfl @[simp] theorem unsym_comp_sym : (unsym : αˢʸᵐ → α) ∘ sym = id := rfl @[simp] theorem sym_symm : (@sym α).symm = unsym := rfl @[simp] theorem unsym_symm : (@unsym α).symm = sym := rfl theorem sym_bijective : Bijective (sym : α → αˢʸᵐ) := sym.bijective theorem unsym_bijective : Bijective (unsym : αˢʸᵐ → α) := unsym.symm.bijective theorem sym_injective : Injective (sym : α → αˢʸᵐ) := sym.injective theorem sym_surjective : Surjective (sym : α → αˢʸᵐ) := sym.surjective theorem unsym_injective : Injective (unsym : αˢʸᵐ → α) := unsym.injective theorem unsym_surjective : Surjective (unsym : αˢʸᵐ → α) := unsym.surjective theorem sym_inj {a b : α} : sym a = sym b ↔ a = b := sym_injective.eq_iff theorem unsym_inj {a b : αˢʸᵐ} : unsym a = unsym b ↔ a = b := unsym_injective.eq_iff instance [Nontrivial α] : Nontrivial αˢʸᵐ := sym_injective.nontrivial instance [Inhabited α] : Inhabited αˢʸᵐ := ⟨sym default⟩ instance [Subsingleton α] : Subsingleton αˢʸᵐ := unsym_injective.subsingleton instance [Unique α] : Unique αˢʸᵐ := Unique.mk' _ instance [IsEmpty α] : IsEmpty αˢʸᵐ := Function.isEmpty unsym @[to_additive] instance [One α] : One αˢʸᵐ where one := sym 1 instance [Add α] : Add αˢʸᵐ where add a b := sym (unsym a + unsym b) instance [Sub α] : Sub αˢʸᵐ where sub a b := sym (unsym a - unsym b) instance [Neg α] : Neg αˢʸᵐ where neg a := sym (-unsym a) -- Introduce the symmetrized multiplication instance [Add α] [Mul α] [One α] [OfNat α 2] [Invertible (2 : α)] : Mul αˢʸᵐ where mul a b := sym (⅟ 2 * (unsym a * unsym b + unsym b * unsym a)) @[to_additive existing] instance [Inv α] : Inv αˢʸᵐ where inv a := sym <| (unsym a)⁻¹ instance (R : Type*) [SMul R α] : SMul R αˢʸᵐ where smul r a := sym (r • unsym a) @[to_additive (attr := simp)] theorem sym_one [One α] : sym (1 : α) = 1 := rfl @[to_additive (attr := simp)] theorem unsym_one [One α] : unsym (1 : αˢʸᵐ) = 1 := rfl @[simp] theorem sym_add [Add α] (a b : α) : sym (a + b) = sym a + sym b := rfl @[simp] theorem unsym_add [Add α] (a b : αˢʸᵐ) : unsym (a + b) = unsym a + unsym b := rfl @[simp] theorem sym_sub [Sub α] (a b : α) : sym (a - b) = sym a - sym b := rfl @[simp] theorem unsym_sub [Sub α] (a b : αˢʸᵐ) : unsym (a - b) = unsym a - unsym b := rfl @[simp] theorem sym_neg [Neg α] (a : α) : sym (-a) = -sym a := rfl @[simp] theorem unsym_neg [Neg α] (a : αˢʸᵐ) : unsym (-a) = -unsym a := rfl theorem mul_def [Add α] [Mul α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : αˢʸᵐ) : a * b = sym (⅟ 2 * (unsym a * unsym b + unsym b * unsym a)) := rfl theorem unsym_mul [Mul α] [Add α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : αˢʸᵐ) : unsym (a * b) = ⅟ 2 * (unsym a * unsym b + unsym b * unsym a) := rfl theorem sym_mul_sym [Mul α] [Add α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : α) : sym a * sym b = sym (⅟ 2 * (a * b + b * a)) := rfl set_option linter.existingAttributeWarning false in @[simp, to_additive existing] theorem sym_inv [Inv α] (a : α) : sym a⁻¹ = (sym a)⁻¹ := rfl set_option linter.existingAttributeWarning false in @[simp, to_additive existing] theorem unsym_inv [Inv α] (a : αˢʸᵐ) : unsym a⁻¹ = (unsym a)⁻¹ := rfl @[simp] theorem sym_smul {R : Type*} [SMul R α] (c : R) (a : α) : sym (c • a) = c • sym a := rfl @[simp] theorem unsym_smul {R : Type*} [SMul R α] (c : R) (a : αˢʸᵐ) : unsym (c • a) = c • unsym a := rfl @[to_additive (attr := simp)] theorem unsym_eq_one_iff [One α] (a : αˢʸᵐ) : unsym a = 1 ↔ a = 1 := unsym_injective.eq_iff' rfl @[to_additive (attr := simp)] theorem sym_eq_one_iff [One α] (a : α) : sym a = 1 ↔ a = 1 := sym_injective.eq_iff' rfl @[to_additive] theorem unsym_ne_one_iff [One α] (a : αˢʸᵐ) : unsym a ≠ (1 : α) ↔ a ≠ (1 : αˢʸᵐ) := not_congr <| unsym_eq_one_iff a @[to_additive] theorem sym_ne_one_iff [One α] (a : α) : sym a ≠ (1 : αˢʸᵐ) ↔ a ≠ (1 : α) := not_congr <| sym_eq_one_iff a instance addCommSemigroup [AddCommSemigroup α] : AddCommSemigroup αˢʸᵐ := unsym_injective.addCommSemigroup _ unsym_add instance addMonoid [AddMonoid α] : AddMonoid αˢʸᵐ := unsym_injective.addMonoid _ unsym_zero unsym_add fun _ _ => rfl instance addGroup [AddGroup α] : AddGroup αˢʸᵐ := unsym_injective.addGroup _ unsym_zero unsym_add unsym_neg unsym_sub (fun _ _ => rfl) fun _ _ => rfl instance addCommMonoid [AddCommMonoid α] : AddCommMonoid αˢʸᵐ := { SymAlg.addCommSemigroup, SymAlg.addMonoid with } instance addCommGroup [AddCommGroup α] : AddCommGroup αˢʸᵐ := { SymAlg.addCommMonoid, SymAlg.addGroup with } instance {R : Type*} [Semiring R] [AddCommMonoid α] [Module R α] : Module R αˢʸᵐ := Function.Injective.module R ⟨⟨unsym, unsym_zero⟩, unsym_add⟩ unsym_injective unsym_smul instance [Mul α] [AddMonoidWithOne α] [Invertible (2 : α)] (a : α) [Invertible a] : Invertible (sym a) where invOf := sym (⅟ a) invOf_mul_self := by rw [sym_mul_sym, mul_invOf_self, invOf_mul_self, one_add_one_eq_two, invOf_mul_self, sym_one] mul_invOf_self := by rw [sym_mul_sym, mul_invOf_self, invOf_mul_self, one_add_one_eq_two, invOf_mul_self, sym_one] @[simp] theorem invOf_sym [Mul α] [AddMonoidWithOne α] [Invertible (2 : α)] (a : α) [Invertible a] : ⅟ (sym a) = sym (⅟ a) := rfl instance nonAssocSemiring [Semiring α] [Invertible (2 : α)] : NonAssocSemiring αˢʸᵐ := { SymAlg.addCommMonoid with one := 1 mul := (· * ·) zero_mul := fun _ => by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero] mul_zero := fun _ => by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero] mul_one := fun _ => by rw [mul_def, unsym_one, mul_one, one_mul, ← two_mul, invOf_mul_cancel_left, sym_unsym] one_mul := fun _ => by rw [mul_def, unsym_one, mul_one, one_mul, ← two_mul, invOf_mul_cancel_left, sym_unsym] left_distrib := fun a b c => by rw [mul_def, mul_def, mul_def, ← sym_add, ← mul_add, unsym_add, add_mul] congr 2 rw [mul_add] abel right_distrib := fun a b c => by rw [mul_def, mul_def, mul_def, ← sym_add, ← mul_add, unsym_add, add_mul] congr 2 rw [mul_add] abel } /-- The symmetrization of a real (unital, associative) algebra is a non-associative ring. -/ instance [Ring α] [Invertible (2 : α)] : NonAssocRing αˢʸᵐ := { SymAlg.nonAssocSemiring, SymAlg.addCommGroup with } /-! The squaring operation coincides for both multiplications -/ theorem unsym_mul_self [Semiring α] [Invertible (2 : α)] (a : αˢʸᵐ) : unsym (a * a) = unsym a * unsym a := by rw [mul_def, unsym_sym, ← two_mul, invOf_mul_cancel_left] theorem sym_mul_self [Semiring α] [Invertible (2 : α)] (a : α) : sym (a * a) = sym a * sym a := by rw [sym_mul_sym, ← two_mul, invOf_mul_cancel_left] theorem mul_comm [Mul α] [AddCommSemigroup α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : αˢʸᵐ) : a * b = b * a := by rw [mul_def, mul_def, add_comm] instance [Ring α] [Invertible (2 : α)] : CommMagma αˢʸᵐ where mul_comm := SymAlg.mul_comm instance [Ring α] [Invertible (2 : α)] : IsCommJordan αˢʸᵐ where lmul_comm_rmul_rmul a b := by have commute_half_left := fun a : α => by have := (Commute.one_left a).add_left (Commute.one_left a) rw [one_add_one_eq_two] at this exact this.invOf_left.eq calc a * b * (a * a) _ = sym (⅟2 * ⅟2 * (unsym a * unsym b * unsym (a * a) + unsym b * unsym a * unsym (a * a) + unsym (a * a) * unsym a * unsym b + unsym (a * a) * unsym b * unsym a)) := ?_ _ = sym (⅟ 2 * (unsym a * unsym (sym (⅟ 2 * (unsym b * unsym (a * a) + unsym (a * a) * unsym b))) + unsym (sym (⅟ 2 * (unsym b * unsym (a * a) + unsym (a * a) * unsym b))) * unsym a)) := ?_ _ = a * (b * (a * a)) := ?_ -- Rearrange LHS · rw [mul_def, mul_def a b, unsym_sym, ← mul_assoc, ← commute_half_left (unsym (a * a)), mul_assoc, mul_assoc, ← mul_add, ← mul_assoc, add_mul, mul_add (unsym (a * a)), ← add_assoc, ← mul_assoc, ← mul_assoc] · rw [unsym_sym, sym_inj, ← mul_assoc, ← commute_half_left (unsym a), mul_assoc (⅟ 2) (unsym a), mul_assoc (⅟ 2) _ (unsym a), ← mul_add, ← mul_assoc] conv_rhs => rw [mul_add (unsym a)] rw [add_mul, ← add_assoc, ← mul_assoc, ← mul_assoc] rw [unsym_mul_self] rw [← mul_assoc, ← mul_assoc, ← mul_assoc, ← mul_assoc, ← sub_eq_zero, ← mul_sub] convert mul_zero (⅟ (2 : α) * ⅟ (2 : α)) rw [add_sub_add_right_eq_sub, add_assoc, add_assoc, add_sub_add_left_eq_sub, add_comm, add_sub_add_right_eq_sub, sub_eq_zero] -- Rearrange RHS · rw [← mul_def, ← mul_def] end SymAlg
Mathlib/Algebra/Symmetrized.lean
330
331
/- 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 -/ import Mathlib.Algebra.Group.TypeTags.Basic import Mathlib.Data.Fin.VecNotation import Mathlib.Data.Finset.Piecewise import Mathlib.Order.Filter.Cofinite import Mathlib.Order.Filter.Curry import Mathlib.Topology.Constructions.SumProd import Mathlib.Topology.NhdsSet /-! # Constructions of new topological spaces from old ones This file constructs pi types, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X → Y × Z` is continuous if and only if both projections `X → Y`, `X → Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, subspace, quotient space -/ noncomputable section open Topology TopologicalSpace Set Filter Function open scoped Set.Notation universe u v u' v' variable {X : Type u} {Y : Type v} {Z W ε ζ : Type*} section Constructions instance {r : X → X → Prop} [t : TopologicalSpace X] : TopologicalSpace (Quot r) := coinduced (Quot.mk r) t instance instTopologicalSpaceQuotient {s : Setoid X} [t : TopologicalSpace X] : TopologicalSpace (Quotient s) := coinduced Quotient.mk' t instance instTopologicalSpaceSigma {ι : Type*} {X : ι → Type v} [t₂ : ∀ i, TopologicalSpace (X i)] : TopologicalSpace (Sigma X) := ⨆ i, coinduced (Sigma.mk i) (t₂ i) instance Pi.topologicalSpace {ι : Type*} {Y : ι → Type v} [t₂ : (i : ι) → TopologicalSpace (Y i)] : TopologicalSpace ((i : ι) → Y i) := ⨅ i, induced (fun f => f i) (t₂ i) instance ULift.topologicalSpace [t : TopologicalSpace X] : TopologicalSpace (ULift.{v, u} X) := t.induced ULift.down /-! ### `Additive`, `Multiplicative` The topology on those type synonyms is inherited without change. -/ section variable [TopologicalSpace X] open Additive Multiplicative instance : TopologicalSpace (Additive X) := ‹TopologicalSpace X› instance : TopologicalSpace (Multiplicative X) := ‹TopologicalSpace X› instance [DiscreteTopology X] : DiscreteTopology (Additive X) := ‹DiscreteTopology X› instance [DiscreteTopology X] : DiscreteTopology (Multiplicative X) := ‹DiscreteTopology X› theorem continuous_ofMul : Continuous (ofMul : X → Additive X) := continuous_id theorem continuous_toMul : Continuous (toMul : Additive X → X) := continuous_id theorem continuous_ofAdd : Continuous (ofAdd : X → Multiplicative X) := continuous_id theorem continuous_toAdd : Continuous (toAdd : Multiplicative X → X) := continuous_id theorem isOpenMap_ofMul : IsOpenMap (ofMul : X → Additive X) := IsOpenMap.id theorem isOpenMap_toMul : IsOpenMap (toMul : Additive X → X) := IsOpenMap.id theorem isOpenMap_ofAdd : IsOpenMap (ofAdd : X → Multiplicative X) := IsOpenMap.id theorem isOpenMap_toAdd : IsOpenMap (toAdd : Multiplicative X → X) := IsOpenMap.id theorem isClosedMap_ofMul : IsClosedMap (ofMul : X → Additive X) := IsClosedMap.id theorem isClosedMap_toMul : IsClosedMap (toMul : Additive X → X) := IsClosedMap.id theorem isClosedMap_ofAdd : IsClosedMap (ofAdd : X → Multiplicative X) := IsClosedMap.id theorem isClosedMap_toAdd : IsClosedMap (toAdd : Multiplicative X → X) := IsClosedMap.id theorem nhds_ofMul (x : X) : 𝓝 (ofMul x) = map ofMul (𝓝 x) := rfl theorem nhds_ofAdd (x : X) : 𝓝 (ofAdd x) = map ofAdd (𝓝 x) := rfl theorem nhds_toMul (x : Additive X) : 𝓝 x.toMul = map toMul (𝓝 x) := rfl theorem nhds_toAdd (x : Multiplicative X) : 𝓝 x.toAdd = map toAdd (𝓝 x) := rfl end /-! ### Order dual The topology on this type synonym is inherited without change. -/ section variable [TopologicalSpace X] open OrderDual instance OrderDual.instTopologicalSpace : TopologicalSpace Xᵒᵈ := ‹_› instance OrderDual.instDiscreteTopology [DiscreteTopology X] : DiscreteTopology Xᵒᵈ := ‹_› theorem continuous_toDual : Continuous (toDual : X → Xᵒᵈ) := continuous_id theorem continuous_ofDual : Continuous (ofDual : Xᵒᵈ → X) := continuous_id theorem isOpenMap_toDual : IsOpenMap (toDual : X → Xᵒᵈ) := IsOpenMap.id theorem isOpenMap_ofDual : IsOpenMap (ofDual : Xᵒᵈ → X) := IsOpenMap.id theorem isClosedMap_toDual : IsClosedMap (toDual : X → Xᵒᵈ) := IsClosedMap.id theorem isClosedMap_ofDual : IsClosedMap (ofDual : Xᵒᵈ → X) := IsClosedMap.id theorem nhds_toDual (x : X) : 𝓝 (toDual x) = map toDual (𝓝 x) := rfl theorem nhds_ofDual (x : X) : 𝓝 (ofDual x) = map ofDual (𝓝 x) := rfl variable [Preorder X] {x : X} instance OrderDual.instNeBotNhdsWithinIoi [(𝓝[<] x).NeBot] : (𝓝[>] toDual x).NeBot := ‹_› instance OrderDual.instNeBotNhdsWithinIio [(𝓝[>] x).NeBot] : (𝓝[<] toDual x).NeBot := ‹_› end theorem Quotient.preimage_mem_nhds [TopologicalSpace X] [s : Setoid X] {V : Set <| Quotient s} {x : X} (hs : V ∈ 𝓝 (Quotient.mk' x)) : Quotient.mk' ⁻¹' V ∈ 𝓝 x := preimage_nhds_coinduced hs /-- The image of a dense set under `Quotient.mk'` is a dense set. -/ theorem Dense.quotient [Setoid X] [TopologicalSpace X] {s : Set X} (H : Dense s) : Dense (Quotient.mk' '' s) := Quotient.mk''_surjective.denseRange.dense_image continuous_coinduced_rng H /-- The composition of `Quotient.mk'` and a function with dense range has dense range. -/ theorem DenseRange.quotient [Setoid X] [TopologicalSpace X] {f : Y → X} (hf : DenseRange f) : DenseRange (Quotient.mk' ∘ f) := Quotient.mk''_surjective.denseRange.comp hf continuous_coinduced_rng theorem continuous_map_of_le {α : Type*} [TopologicalSpace α] {s t : Setoid α} (h : s ≤ t) : Continuous (Setoid.map_of_le h) := continuous_coinduced_rng theorem continuous_map_sInf {α : Type*} [TopologicalSpace α] {S : Set (Setoid α)} {s : Setoid α} (h : s ∈ S) : Continuous (Setoid.map_sInf h) := continuous_coinduced_rng instance {p : X → Prop} [TopologicalSpace X] [DiscreteTopology X] : DiscreteTopology (Subtype p) := ⟨bot_unique fun s _ => ⟨(↑) '' s, isOpen_discrete _, preimage_image_eq _ Subtype.val_injective⟩⟩ instance Sum.discreteTopology [TopologicalSpace X] [TopologicalSpace Y] [h : DiscreteTopology X] [hY : DiscreteTopology Y] : DiscreteTopology (X ⊕ Y) := ⟨sup_eq_bot_iff.2 <| by simp [h.eq_bot, hY.eq_bot]⟩ instance Sigma.discreteTopology {ι : Type*} {Y : ι → Type v} [∀ i, TopologicalSpace (Y i)] [h : ∀ i, DiscreteTopology (Y i)] : DiscreteTopology (Sigma Y) := ⟨iSup_eq_bot.2 fun _ => by simp only [(h _).eq_bot, coinduced_bot]⟩ @[simp] lemma comap_nhdsWithin_range {α β} [TopologicalSpace β] (f : α → β) (y : β) : comap f (𝓝[range f] y) = comap f (𝓝 y) := comap_inf_principal_range section Top variable [TopologicalSpace X] /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype (s : Set X) (x : { x // x ∈ s }) (t : Set { x // x ∈ s }) : t ∈ 𝓝 x ↔ ∃ u ∈ 𝓝 (x : X), Subtype.val ⁻¹' u ⊆ t := mem_nhds_induced _ x t theorem nhds_subtype (s : Set X) (x : { x // x ∈ s }) : 𝓝 x = comap (↑) (𝓝 (x : X)) := nhds_induced _ x lemma nhds_subtype_eq_comap_nhdsWithin (s : Set X) (x : { x // x ∈ s }) : 𝓝 x = comap (↑) (𝓝[s] (x : X)) := by rw [nhds_subtype, ← comap_nhdsWithin_range, Subtype.range_val] theorem nhdsWithin_subtype_eq_bot_iff {s t : Set X} {x : s} : 𝓝[((↑) : s → X) ⁻¹' t] x = ⊥ ↔ 𝓝[t] (x : X) ⊓ 𝓟 s = ⊥ := by rw [inf_principal_eq_bot_iff_comap, nhdsWithin, nhdsWithin, comap_inf, comap_principal, nhds_induced] theorem nhds_ne_subtype_eq_bot_iff {S : Set X} {x : S} : 𝓝[≠] x = ⊥ ↔ 𝓝[≠] (x : X) ⊓ 𝓟 S = ⊥ := by rw [← nhdsWithin_subtype_eq_bot_iff, preimage_compl, ← image_singleton, Subtype.coe_injective.preimage_image] theorem nhds_ne_subtype_neBot_iff {S : Set X} {x : S} : (𝓝[≠] x).NeBot ↔ (𝓝[≠] (x : X) ⊓ 𝓟 S).NeBot := by rw [neBot_iff, neBot_iff, not_iff_not, nhds_ne_subtype_eq_bot_iff] theorem discreteTopology_subtype_iff {S : Set X} : DiscreteTopology S ↔ ∀ x ∈ S, 𝓝[≠] x ⊓ 𝓟 S = ⊥ := by simp_rw [discreteTopology_iff_nhds_ne, SetCoe.forall', nhds_ne_subtype_eq_bot_iff] end Top /-- A type synonym equipped with the topology whose open sets are the empty set and the sets with finite complements. -/ def CofiniteTopology (X : Type*) := X namespace CofiniteTopology /-- The identity equivalence between `` and `CofiniteTopology `. -/ def of : X ≃ CofiniteTopology X := Equiv.refl X instance [Inhabited X] : Inhabited (CofiniteTopology X) where default := of default instance : TopologicalSpace (CofiniteTopology X) where IsOpen s := s.Nonempty → Set.Finite sᶜ isOpen_univ := by simp isOpen_inter s t := by rintro hs ht ⟨x, hxs, hxt⟩ rw [compl_inter] exact (hs ⟨x, hxs⟩).union (ht ⟨x, hxt⟩) isOpen_sUnion := by rintro s h ⟨x, t, hts, hzt⟩ rw [compl_sUnion] exact Finite.sInter (mem_image_of_mem _ hts) (h t hts ⟨x, hzt⟩) theorem isOpen_iff {s : Set (CofiniteTopology X)} : IsOpen s ↔ s.Nonempty → sᶜ.Finite := Iff.rfl theorem isOpen_iff' {s : Set (CofiniteTopology X)} : IsOpen s ↔ s = ∅ ∨ sᶜ.Finite := by simp only [isOpen_iff, nonempty_iff_ne_empty, or_iff_not_imp_left] theorem isClosed_iff {s : Set (CofiniteTopology X)} : IsClosed s ↔ s = univ ∨ s.Finite := by simp only [← isOpen_compl_iff, isOpen_iff', compl_compl, compl_empty_iff] theorem nhds_eq (x : CofiniteTopology X) : 𝓝 x = pure x ⊔ cofinite := by ext U rw [mem_nhds_iff] constructor · rintro ⟨V, hVU, V_op, haV⟩ exact mem_sup.mpr ⟨hVU haV, mem_of_superset (V_op ⟨_, haV⟩) hVU⟩ · rintro ⟨hU : x ∈ U, hU' : Uᶜ.Finite⟩ exact ⟨U, Subset.rfl, fun _ => hU', hU⟩ theorem mem_nhds_iff {x : CofiniteTopology X} {s : Set (CofiniteTopology X)} : s ∈ 𝓝 x ↔ x ∈ s ∧ sᶜ.Finite := by simp [nhds_eq] end CofiniteTopology end Constructions section Prod variable [TopologicalSpace X] [TopologicalSpace Y] theorem MapClusterPt.curry_prodMap {α β : Type*} {f : α → X} {g : β → Y} {la : Filter α} {lb : Filter β} {x : X} {y : Y} (hf : MapClusterPt x la f) (hg : MapClusterPt y lb g) : MapClusterPt (x, y) (la.curry lb) (.map f g) := by rw [mapClusterPt_iff_frequently] at hf hg rw [((𝓝 x).basis_sets.prod_nhds (𝓝 y).basis_sets).mapClusterPt_iff_frequently] rintro ⟨s, t⟩ ⟨hs, ht⟩ rw [frequently_curry_iff] exact (hf s hs).mono fun x hx ↦ (hg t ht).mono fun y hy ↦ ⟨hx, hy⟩ theorem MapClusterPt.prodMap {α β : Type*} {f : α → X} {g : β → Y} {la : Filter α} {lb : Filter β} {x : X} {y : Y} (hf : MapClusterPt x la f) (hg : MapClusterPt y lb g) : MapClusterPt (x, y) (la ×ˢ lb) (.map f g) := (hf.curry_prodMap hg).mono <| map_mono curry_le_prod end Prod section Bool lemma continuous_bool_rng [TopologicalSpace X] {f : X → Bool} (b : Bool) : Continuous f ↔ IsClopen (f ⁻¹' {b}) := by rw [continuous_discrete_rng, Bool.forall_bool' b, IsClopen, ← isOpen_compl_iff, ← preimage_compl, Bool.compl_singleton, and_comm] end Bool section Subtype variable [TopologicalSpace X] [TopologicalSpace Y] {p : X → Prop} lemma Topology.IsInducing.subtypeVal {t : Set Y} : IsInducing ((↑) : t → Y) := ⟨rfl⟩ @[deprecated (since := "2024-10-28")] alias inducing_subtype_val := IsInducing.subtypeVal lemma Topology.IsInducing.of_codRestrict {f : X → Y} {t : Set Y} (ht : ∀ x, f x ∈ t) (h : IsInducing (t.codRestrict f ht)) : IsInducing f := subtypeVal.comp h @[deprecated (since := "2024-10-28")] alias Inducing.of_codRestrict := IsInducing.of_codRestrict lemma Topology.IsEmbedding.subtypeVal : IsEmbedding ((↑) : Subtype p → X) := ⟨.subtypeVal, Subtype.coe_injective⟩ @[deprecated (since := "2024-10-26")] alias embedding_subtype_val := IsEmbedding.subtypeVal theorem Topology.IsClosedEmbedding.subtypeVal (h : IsClosed {a | p a}) : IsClosedEmbedding ((↑) : Subtype p → X) := ⟨.subtypeVal, by rwa [Subtype.range_coe_subtype]⟩ @[continuity, fun_prop] theorem continuous_subtype_val : Continuous (@Subtype.val X p) := continuous_induced_dom theorem Continuous.subtype_val {f : Y → Subtype p} (hf : Continuous f) : Continuous fun x => (f x : X) := continuous_subtype_val.comp hf theorem IsOpen.isOpenEmbedding_subtypeVal {s : Set X} (hs : IsOpen s) : IsOpenEmbedding ((↑) : s → X) := ⟨.subtypeVal, (@Subtype.range_coe _ s).symm ▸ hs⟩ theorem IsOpen.isOpenMap_subtype_val {s : Set X} (hs : IsOpen s) : IsOpenMap ((↑) : s → X) := hs.isOpenEmbedding_subtypeVal.isOpenMap theorem IsOpenMap.restrict {f : X → Y} (hf : IsOpenMap f) {s : Set X} (hs : IsOpen s) : IsOpenMap (s.restrict f) := hf.comp hs.isOpenMap_subtype_val lemma IsClosed.isClosedEmbedding_subtypeVal {s : Set X} (hs : IsClosed s) : IsClosedEmbedding ((↑) : s → X) := .subtypeVal hs theorem IsClosed.isClosedMap_subtype_val {s : Set X} (hs : IsClosed s) : IsClosedMap ((↑) : s → X) := hs.isClosedEmbedding_subtypeVal.isClosedMap @[continuity, fun_prop] theorem Continuous.subtype_mk {f : Y → X} (h : Continuous f) (hp : ∀ x, p (f x)) : Continuous fun x => (⟨f x, hp x⟩ : Subtype p) := continuous_induced_rng.2 h theorem Continuous.subtype_map {f : X → Y} (h : Continuous f) {q : Y → Prop} (hpq : ∀ x, p x → q (f x)) : Continuous (Subtype.map f hpq) := (h.comp continuous_subtype_val).subtype_mk _ theorem continuous_inclusion {s t : Set X} (h : s ⊆ t) : Continuous (inclusion h) := continuous_id.subtype_map h theorem continuousAt_subtype_val {p : X → Prop} {x : Subtype p} : ContinuousAt ((↑) : Subtype p → X) x := continuous_subtype_val.continuousAt theorem Subtype.dense_iff {s : Set X} {t : Set s} : Dense t ↔ s ⊆ closure ((↑) '' t) := by rw [IsInducing.subtypeVal.dense_iff, SetCoe.forall] rfl theorem map_nhds_subtype_val {s : Set X} (x : s) : map ((↑) : s → X) (𝓝 x) = 𝓝[s] ↑x := by rw [IsInducing.subtypeVal.map_nhds_eq, Subtype.range_val] theorem map_nhds_subtype_coe_eq_nhds {x : X} (hx : p x) (h : ∀ᶠ x in 𝓝 x, p x) : map ((↑) : Subtype p → X) (𝓝 ⟨x, hx⟩) = 𝓝 x := map_nhds_induced_of_mem <| by rw [Subtype.range_val]; exact h theorem nhds_subtype_eq_comap {x : X} {h : p x} : 𝓝 (⟨x, h⟩ : Subtype p) = comap (↑) (𝓝 x) := nhds_induced _ _ theorem tendsto_subtype_rng {Y : Type*} {p : X → Prop} {l : Filter Y} {f : Y → Subtype p} : ∀ {x : Subtype p}, Tendsto f l (𝓝 x) ↔ Tendsto (fun x => (f x : X)) l (𝓝 (x : X)) | ⟨a, ha⟩ => by rw [nhds_subtype_eq_comap, tendsto_comap_iff]; rfl theorem closure_subtype {x : { a // p a }} {s : Set { a // p a }} : x ∈ closure s ↔ (x : X) ∈ closure (((↑) : _ → X) '' s) := closure_induced @[simp] theorem continuousAt_codRestrict_iff {f : X → Y} {t : Set Y} (h1 : ∀ x, f x ∈ t) {x : X} : ContinuousAt (codRestrict f t h1) x ↔ ContinuousAt f x := IsInducing.subtypeVal.continuousAt_iff alias ⟨_, ContinuousAt.codRestrict⟩ := continuousAt_codRestrict_iff theorem ContinuousAt.restrict {f : X → Y} {s : Set X} {t : Set Y} (h1 : MapsTo f s t) {x : s} (h2 : ContinuousAt f x) : ContinuousAt (h1.restrict f s t) x := (h2.comp continuousAt_subtype_val).codRestrict _ theorem ContinuousAt.restrictPreimage {f : X → Y} {s : Set Y} {x : f ⁻¹' s} (h : ContinuousAt f x) : ContinuousAt (s.restrictPreimage f) x := h.restrict _ @[continuity, fun_prop] theorem Continuous.codRestrict {f : X → Y} {s : Set Y} (hf : Continuous f) (hs : ∀ a, f a ∈ s) : Continuous (s.codRestrict f hs) := hf.subtype_mk hs @[continuity, fun_prop] theorem Continuous.restrict {f : X → Y} {s : Set X} {t : Set Y} (h1 : MapsTo f s t) (h2 : Continuous f) : Continuous (h1.restrict f s t) := (h2.comp continuous_subtype_val).codRestrict _ @[continuity, fun_prop] theorem Continuous.restrictPreimage {f : X → Y} {s : Set Y} (h : Continuous f) : Continuous (s.restrictPreimage f) := h.restrict _ lemma Topology.IsEmbedding.restrict {f : X → Y} (hf : IsEmbedding f) {s : Set X} {t : Set Y} (H : s.MapsTo f t) : IsEmbedding H.restrict := .of_comp (hf.continuous.restrict H) continuous_subtype_val (hf.comp .subtypeVal) lemma Topology.IsOpenEmbedding.restrict {f : X → Y} (hf : IsOpenEmbedding f) {s : Set X} {t : Set Y} (H : s.MapsTo f t) (hs : IsOpen s) : IsOpenEmbedding H.restrict := ⟨hf.isEmbedding.restrict H, (by rw [MapsTo.range_restrict] exact continuous_subtype_val.1 _ (hf.isOpenMap _ hs))⟩ theorem Topology.IsInducing.codRestrict {e : X → Y} (he : IsInducing e) {s : Set Y} (hs : ∀ x, e x ∈ s) : IsInducing (codRestrict e s hs) := he.of_comp (he.continuous.codRestrict hs) continuous_subtype_val @[deprecated (since := "2024-10-28")] alias Inducing.codRestrict := IsInducing.codRestrict protected lemma Topology.IsEmbedding.codRestrict {e : X → Y} (he : IsEmbedding e) (s : Set Y) (hs : ∀ x, e x ∈ s) : IsEmbedding (codRestrict e s hs) := he.of_comp (he.continuous.codRestrict hs) continuous_subtype_val @[deprecated (since := "2024-10-26")] alias Embedding.codRestrict := IsEmbedding.codRestrict variable {s t : Set X} protected lemma Topology.IsEmbedding.inclusion (h : s ⊆ t) : IsEmbedding (inclusion h) := IsEmbedding.subtypeVal.codRestrict _ _ protected lemma Topology.IsOpenEmbedding.inclusion (hst : s ⊆ t) (hs : IsOpen (t ↓∩ s)) : IsOpenEmbedding (inclusion hst) where toIsEmbedding := .inclusion _ isOpen_range := by rwa [range_inclusion] protected lemma Topology.IsClosedEmbedding.inclusion (hst : s ⊆ t) (hs : IsClosed (t ↓∩ s)) : IsClosedEmbedding (inclusion hst) where toIsEmbedding := .inclusion _ isClosed_range := by rwa [range_inclusion] @[deprecated (since := "2024-10-26")] alias embedding_inclusion := IsEmbedding.inclusion /-- Let `s, t ⊆ X` be two subsets of a topological space `X`. If `t ⊆ s` and the topology induced by `X`on `s` is discrete, then also the topology induces on `t` is discrete. -/ theorem DiscreteTopology.of_subset {X : Type*} [TopologicalSpace X] {s t : Set X} (_ : DiscreteTopology s) (ts : t ⊆ s) : DiscreteTopology t := (IsEmbedding.inclusion ts).discreteTopology /-- Let `s` be a discrete subset of a topological space. Then the preimage of `s` by a continuous injective map is also discrete. -/ theorem DiscreteTopology.preimage_of_continuous_injective {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y] (s : Set Y) [DiscreteTopology s] {f : X → Y} (hc : Continuous f) (hinj : Function.Injective f) : DiscreteTopology (f ⁻¹' s) := DiscreteTopology.of_continuous_injective (β := s) (Continuous.restrict (by exact fun _ x ↦ x) hc) ((MapsTo.restrict_inj _).mpr hinj.injOn) /-- If `f : X → Y` is a quotient map, then its restriction to the preimage of an open set is a quotient map too. -/ theorem Topology.IsQuotientMap.restrictPreimage_isOpen {f : X → Y} (hf : IsQuotientMap f) {s : Set Y} (hs : IsOpen s) : IsQuotientMap (s.restrictPreimage f) := by refine isQuotientMap_iff.2 ⟨hf.surjective.restrictPreimage _, fun U ↦ ?_⟩ rw [hs.isOpenEmbedding_subtypeVal.isOpen_iff_image_isOpen, ← hf.isOpen_preimage, (hs.preimage hf.continuous).isOpenEmbedding_subtypeVal.isOpen_iff_image_isOpen, image_val_preimage_restrictPreimage] @[deprecated (since := "2024-10-22")] alias QuotientMap.restrictPreimage_isOpen := IsQuotientMap.restrictPreimage_isOpen open scoped Set.Notation in lemma isClosed_preimage_val {s t : Set X} : IsClosed (s ↓∩ t) ↔ s ∩ closure (s ∩ t) ⊆ t := by rw [← closure_eq_iff_isClosed, IsEmbedding.subtypeVal.closure_eq_preimage_closure_image, ← Subtype.val_injective.image_injective.eq_iff, Subtype.image_preimage_coe, Subtype.image_preimage_coe, subset_antisymm_iff, and_iff_left, Set.subset_inter_iff, and_iff_right] exacts [Set.inter_subset_left, Set.subset_inter Set.inter_subset_left subset_closure] theorem frontier_inter_open_inter {s t : Set X} (ht : IsOpen t) : frontier (s ∩ t) ∩ t = frontier s ∩ t := by simp only [Set.inter_comm _ t, ← Subtype.preimage_coe_eq_preimage_coe_iff, ht.isOpenMap_subtype_val.preimage_frontier_eq_frontier_preimage continuous_subtype_val, Subtype.preimage_coe_self_inter] section SetNotation open scoped Set.Notation lemma IsOpen.preimage_val {s t : Set X} (ht : IsOpen t) : IsOpen (s ↓∩ t) := ht.preimage continuous_subtype_val lemma IsClosed.preimage_val {s t : Set X} (ht : IsClosed t) : IsClosed (s ↓∩ t) := ht.preimage continuous_subtype_val @[simp] lemma IsOpen.inter_preimage_val_iff {s t : Set X} (hs : IsOpen s) : IsOpen (s ↓∩ t) ↔ IsOpen (s ∩ t) := ⟨fun h ↦ by simpa using hs.isOpenMap_subtype_val _ h, fun h ↦ (Subtype.preimage_coe_self_inter _ _).symm ▸ h.preimage_val⟩ @[simp] lemma IsClosed.inter_preimage_val_iff {s t : Set X} (hs : IsClosed s) : IsClosed (s ↓∩ t) ↔ IsClosed (s ∩ t) := ⟨fun h ↦ by simpa using hs.isClosedMap_subtype_val _ h, fun h ↦ (Subtype.preimage_coe_self_inter _ _).symm ▸ h.preimage_val⟩ end SetNotation end Subtype section Quotient variable [TopologicalSpace X] [TopologicalSpace Y] variable {r : X → X → Prop} {s : Setoid X} theorem isQuotientMap_quot_mk : IsQuotientMap (@Quot.mk X r) := ⟨Quot.exists_rep, rfl⟩ @[deprecated (since := "2024-10-22")] alias quotientMap_quot_mk := isQuotientMap_quot_mk @[continuity, fun_prop] theorem continuous_quot_mk : Continuous (@Quot.mk X r) := continuous_coinduced_rng @[continuity, fun_prop] theorem continuous_quot_lift {f : X → Y} (hr : ∀ a b, r a b → f a = f b) (h : Continuous f) : Continuous (Quot.lift f hr : Quot r → Y) := continuous_coinduced_dom.2 h theorem isQuotientMap_quotient_mk' : IsQuotientMap (@Quotient.mk' X s) := isQuotientMap_quot_mk @[deprecated (since := "2024-10-22")] alias quotientMap_quotient_mk' := isQuotientMap_quotient_mk' theorem continuous_quotient_mk' : Continuous (@Quotient.mk' X s) := continuous_coinduced_rng theorem Continuous.quotient_lift {f : X → Y} (h : Continuous f) (hs : ∀ a b, a ≈ b → f a = f b) : Continuous (Quotient.lift f hs : Quotient s → Y) := continuous_coinduced_dom.2 h theorem Continuous.quotient_liftOn' {f : X → Y} (h : Continuous f) (hs : ∀ a b, s a b → f a = f b) : Continuous (fun x => Quotient.liftOn' x f hs : Quotient s → Y) := h.quotient_lift hs open scoped Relator in @[continuity, fun_prop] theorem Continuous.quotient_map' {t : Setoid Y} {f : X → Y} (hf : Continuous f) (H : (s.r ⇒ t.r) f f) : Continuous (Quotient.map' f H) := (continuous_quotient_mk'.comp hf).quotient_lift _ end Quotient section Pi variable {ι : Type*} {π : ι → Type*} {κ : Type*} [TopologicalSpace X] [T : ∀ i, TopologicalSpace (π i)] {f : X → ∀ i : ι, π i} theorem continuous_pi_iff : Continuous f ↔ ∀ i, Continuous fun a => f a i := by simp only [continuous_iInf_rng, continuous_induced_rng, comp_def] @[continuity, fun_prop] theorem continuous_pi (h : ∀ i, Continuous fun a => f a i) : Continuous f := continuous_pi_iff.2 h @[continuity, fun_prop] theorem continuous_apply (i : ι) : Continuous fun p : ∀ i, π i => p i := continuous_iInf_dom continuous_induced_dom @[continuity] theorem continuous_apply_apply {ρ : κ → ι → Type*} [∀ j i, TopologicalSpace (ρ j i)] (j : κ) (i : ι) : Continuous fun p : ∀ j, ∀ i, ρ j i => p j i := (continuous_apply i).comp (continuous_apply j) theorem continuousAt_apply (i : ι) (x : ∀ i, π i) : ContinuousAt (fun p : ∀ i, π i => p i) x := (continuous_apply i).continuousAt theorem Filter.Tendsto.apply_nhds {l : Filter Y} {f : Y → ∀ i, π i} {x : ∀ i, π i} (h : Tendsto f l (𝓝 x)) (i : ι) : Tendsto (fun a => f a i) l (𝓝 <| x i) := (continuousAt_apply i _).tendsto.comp h @[fun_prop] protected theorem Continuous.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} (hf : ∀ i, Continuous (f i)) : Continuous (Pi.map f) := continuous_pi fun i ↦ (hf i).comp (continuous_apply i) theorem nhds_pi {a : ∀ i, π i} : 𝓝 a = pi fun i => 𝓝 (a i) := by simp only [nhds_iInf, nhds_induced, Filter.pi] protected theorem IsOpenMap.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} (hfo : ∀ i, IsOpenMap (f i)) (hsurj : ∀ᶠ i in cofinite, Surjective (f i)) : IsOpenMap (Pi.map f) := by refine IsOpenMap.of_nhds_le fun x ↦ ?_ rw [nhds_pi, nhds_pi, map_piMap_pi hsurj] exact Filter.pi_mono fun i ↦ (hfo i).nhds_le _ protected theorem IsOpenQuotientMap.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} (hf : ∀ i, IsOpenQuotientMap (f i)) : IsOpenQuotientMap (Pi.map f) := ⟨.piMap fun i ↦ (hf i).1, .piMap fun i ↦ (hf i).2, .piMap (fun i ↦ (hf i).3) <| .of_forall fun i ↦ (hf i).1⟩ theorem tendsto_pi_nhds {f : Y → ∀ i, π i} {g : ∀ i, π i} {u : Filter Y} : Tendsto f u (𝓝 g) ↔ ∀ x, Tendsto (fun i => f i x) u (𝓝 (g x)) := by rw [nhds_pi, Filter.tendsto_pi] theorem continuousAt_pi {f : X → ∀ i, π i} {x : X} : ContinuousAt f x ↔ ∀ i, ContinuousAt (fun y => f y i) x := tendsto_pi_nhds @[fun_prop] theorem continuousAt_pi' {f : X → ∀ i, π i} {x : X} (hf : ∀ i, ContinuousAt (fun y => f y i) x) : ContinuousAt f x := continuousAt_pi.2 hf @[fun_prop] protected theorem ContinuousAt.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} {x : ∀ i, π i} (hf : ∀ i, ContinuousAt (f i) (x i)) : ContinuousAt (Pi.map f) x := continuousAt_pi.2 fun i ↦ (hf i).comp (continuousAt_apply i x) theorem Pi.continuous_precomp' {ι' : Type*} (φ : ι' → ι) : Continuous (fun (f : (∀ i, π i)) (j : ι') ↦ f (φ j)) := continuous_pi fun j ↦ continuous_apply (φ j) theorem Pi.continuous_precomp {ι' : Type*} (φ : ι' → ι) : Continuous (· ∘ φ : (ι → X) → (ι' → X)) := Pi.continuous_precomp' φ theorem Pi.continuous_postcomp' {X : ι → Type*} [∀ i, TopologicalSpace (X i)] {g : ∀ i, π i → X i} (hg : ∀ i, Continuous (g i)) : Continuous (fun (f : (∀ i, π i)) (i : ι) ↦ g i (f i)) := continuous_pi fun i ↦ (hg i).comp <| continuous_apply i theorem Pi.continuous_postcomp [TopologicalSpace Y] {g : X → Y} (hg : Continuous g) : Continuous (g ∘ · : (ι → X) → (ι → Y)) := Pi.continuous_postcomp' fun _ ↦ hg lemma Pi.induced_precomp' {ι' : Type*} (φ : ι' → ι) : induced (fun (f : (∀ i, π i)) (j : ι') ↦ f (φ j)) Pi.topologicalSpace = ⨅ i', induced (eval (φ i')) (T (φ i')) := by simp [Pi.topologicalSpace, induced_iInf, induced_compose, comp_def] lemma Pi.induced_precomp [TopologicalSpace Y] {ι' : Type*} (φ : ι' → ι) : induced (· ∘ φ) Pi.topologicalSpace = ⨅ i', induced (eval (φ i')) ‹TopologicalSpace Y› := induced_precomp' φ @[continuity, fun_prop] lemma Pi.continuous_restrict (S : Set ι) : Continuous (S.restrict : (∀ i : ι, π i) → (∀ i : S, π i)) := Pi.continuous_precomp' ((↑) : S → ι) @[continuity, fun_prop] lemma Pi.continuous_restrict₂ {s t : Set ι} (hst : s ⊆ t) : Continuous (restrict₂ (π := π) hst) := continuous_pi fun _ ↦ continuous_apply _ @[continuity, fun_prop] theorem Finset.continuous_restrict (s : Finset ι) : Continuous (s.restrict (π := π)) := continuous_pi fun _ ↦ continuous_apply _ @[continuity, fun_prop] theorem Finset.continuous_restrict₂ {s t : Finset ι} (hst : s ⊆ t) : Continuous (Finset.restrict₂ (π := π) hst) := continuous_pi fun _ ↦ continuous_apply _ variable [TopologicalSpace Z] @[continuity, fun_prop] theorem Pi.continuous_restrict_apply (s : Set X) {f : X → Z} (hf : Continuous f) : Continuous (s.restrict f) := hf.comp continuous_subtype_val @[continuity, fun_prop] theorem Pi.continuous_restrict₂_apply {s t : Set X} (hst : s ⊆ t) {f : t → Z} (hf : Continuous f) : Continuous (restrict₂ (π := fun _ ↦ Z) hst f) := hf.comp (continuous_inclusion hst) @[continuity, fun_prop] theorem Finset.continuous_restrict_apply (s : Finset X) {f : X → Z} (hf : Continuous f) : Continuous (s.restrict f) := hf.comp continuous_subtype_val @[continuity, fun_prop] theorem Finset.continuous_restrict₂_apply {s t : Finset X} (hst : s ⊆ t) {f : t → Z} (hf : Continuous f) : Continuous (restrict₂ (π := fun _ ↦ Z) hst f) := hf.comp (continuous_inclusion hst) lemma Pi.induced_restrict (S : Set ι) : induced (S.restrict) Pi.topologicalSpace = ⨅ i ∈ S, induced (eval i) (T i) := by simp +unfoldPartialApp [← iInf_subtype'', ← induced_precomp' ((↑) : S → ι), restrict] lemma Pi.induced_restrict_sUnion (𝔖 : Set (Set ι)) : induced (⋃₀ 𝔖).restrict (Pi.topologicalSpace (Y := fun i : (⋃₀ 𝔖) ↦ π i)) = ⨅ S ∈ 𝔖, induced S.restrict Pi.topologicalSpace := by simp_rw [Pi.induced_restrict, iInf_sUnion] theorem Filter.Tendsto.update [DecidableEq ι] {l : Filter Y} {f : Y → ∀ i, π i} {x : ∀ i, π i} (hf : Tendsto f l (𝓝 x)) (i : ι) {g : Y → π i} {xi : π i} (hg : Tendsto g l (𝓝 xi)) : Tendsto (fun a => update (f a) i (g a)) l (𝓝 <| update x i xi) := tendsto_pi_nhds.2 fun j => by rcases eq_or_ne j i with (rfl | hj) <;> simp [*, hf.apply_nhds] theorem ContinuousAt.update [DecidableEq ι] {x : X} (hf : ContinuousAt f x) (i : ι) {g : X → π i} (hg : ContinuousAt g x) : ContinuousAt (fun a => update (f a) i (g a)) x := hf.tendsto.update i hg theorem Continuous.update [DecidableEq ι] (hf : Continuous f) (i : ι) {g : X → π i} (hg : Continuous g) : Continuous fun a => update (f a) i (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.update i hg.continuousAt /-- `Function.update f i x` is continuous in `(f, x)`. -/ @[continuity, fun_prop] theorem continuous_update [DecidableEq ι] (i : ι) : Continuous fun f : (∀ j, π j) × π i => update f.1 i f.2 := continuous_fst.update i continuous_snd /-- `Pi.mulSingle i x` is continuous in `x`. -/ @[to_additive (attr := continuity) "`Pi.single i x` is continuous in `x`."] theorem continuous_mulSingle [∀ i, One (π i)] [DecidableEq ι] (i : ι) : Continuous fun x => (Pi.mulSingle i x : ∀ i, π i) := continuous_const.update _ continuous_id section Fin variable {n : ℕ} {π : Fin (n + 1) → Type*} [∀ i, TopologicalSpace (π i)] theorem Filter.Tendsto.finCons {f : Y → π 0} {g : Y → ∀ j : Fin n, π j.succ} {l : Filter Y} {x : π 0} {y : ∀ j, π (Fin.succ j)} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => Fin.cons (f a) (g a)) l (𝓝 <| Fin.cons x y) := tendsto_pi_nhds.2 fun j => Fin.cases (by simpa) (by simpa using tendsto_pi_nhds.1 hg) j theorem ContinuousAt.finCons {f : X → π 0} {g : X → ∀ j : Fin n, π (Fin.succ j)} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => Fin.cons (f a) (g a)) x := hf.tendsto.finCons hg theorem Continuous.finCons {f : X → π 0} {g : X → ∀ j : Fin n, π (Fin.succ j)} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => Fin.cons (f a) (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.finCons hg.continuousAt theorem Filter.Tendsto.matrixVecCons {f : Y → Z} {g : Y → Fin n → Z} {l : Filter Y} {x : Z} {y : Fin n → Z} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => Matrix.vecCons (f a) (g a)) l (𝓝 <| Matrix.vecCons x y) := hf.finCons hg theorem ContinuousAt.matrixVecCons {f : X → Z} {g : X → Fin n → Z} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => Matrix.vecCons (f a) (g a)) x := hf.finCons hg theorem Continuous.matrixVecCons {f : X → Z} {g : X → Fin n → Z} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => Matrix.vecCons (f a) (g a) := hf.finCons hg theorem Filter.Tendsto.finSnoc {f : Y → ∀ j : Fin n, π j.castSucc} {g : Y → π (Fin.last _)} {l : Filter Y} {x : ∀ j, π (Fin.castSucc j)} {y : π (Fin.last _)} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => Fin.snoc (f a) (g a)) l (𝓝 <| Fin.snoc x y) := tendsto_pi_nhds.2 fun j => Fin.lastCases (by simpa) (by simpa using tendsto_pi_nhds.1 hf) j theorem ContinuousAt.finSnoc {f : X → ∀ j : Fin n, π j.castSucc} {g : X → π (Fin.last _)} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => Fin.snoc (f a) (g a)) x := hf.tendsto.finSnoc hg theorem Continuous.finSnoc {f : X → ∀ j : Fin n, π j.castSucc} {g : X → π (Fin.last _)} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => Fin.snoc (f a) (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.finSnoc hg.continuousAt theorem Filter.Tendsto.finInsertNth (i : Fin (n + 1)) {f : Y → π i} {g : Y → ∀ j : Fin n, π (i.succAbove j)} {l : Filter Y} {x : π i} {y : ∀ j, π (i.succAbove j)} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => i.insertNth (f a) (g a)) l (𝓝 <| i.insertNth x y) := tendsto_pi_nhds.2 fun j => Fin.succAboveCases i (by simpa) (by simpa using tendsto_pi_nhds.1 hg) j @[deprecated (since := "2025-01-02")] alias Filter.Tendsto.fin_insertNth := Filter.Tendsto.finInsertNth theorem ContinuousAt.finInsertNth (i : Fin (n + 1)) {f : X → π i} {g : X → ∀ j : Fin n, π (i.succAbove j)} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => i.insertNth (f a) (g a)) x := hf.tendsto.finInsertNth i hg @[deprecated (since := "2025-01-02")] alias ContinuousAt.fin_insertNth := ContinuousAt.finInsertNth theorem Continuous.finInsertNth (i : Fin (n + 1)) {f : X → π i} {g : X → ∀ j : Fin n, π (i.succAbove j)} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => i.insertNth (f a) (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.finInsertNth i hg.continuousAt @[deprecated (since := "2025-01-02")] alias Continuous.fin_insertNth := Continuous.finInsertNth theorem Filter.Tendsto.finInit {f : Y → ∀ j : Fin (n + 1), π j} {l : Filter Y} {x : ∀ j, π j} (hg : Tendsto f l (𝓝 x)) : Tendsto (fun a ↦ Fin.init (f a)) l (𝓝 <| Fin.init x) := tendsto_pi_nhds.2 fun j ↦ apply_nhds hg j.castSucc
@[fun_prop] theorem ContinuousAt.finInit {f : X → ∀ j : Fin (n + 1), π j} {x : X}
Mathlib/Topology/Constructions.lean
829
830
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Manuel Candales -/ import Mathlib.Geometry.Euclidean.PerpBisector import Mathlib.Algebra.QuadraticDiscriminant /-! # Euclidean spaces This file makes some definitions and proves very basic geometrical results about real inner product spaces and Euclidean affine spaces. Results about real inner product spaces that involve the norm and inner product but not angles generally go in `Analysis.NormedSpace.InnerProduct`. Results with longer proofs or more geometrical content generally go in separate files. ## Implementation notes To declare `P` as the type of points in a Euclidean affine space with `V` as the type of vectors, use `[NormedAddCommGroup V] [InnerProductSpace ℝ V] [MetricSpace P] [NormedAddTorsor V P]`. This works better with `outParam` to make `V` implicit in most cases than having a separate type alias for Euclidean affine spaces. Rather than requiring Euclidean affine spaces to be finite-dimensional (as in the definition on Wikipedia), this is specified only for those theorems that need it. ## References * https://en.wikipedia.org/wiki/Euclidean_space -/ noncomputable section open RealInnerProductSpace namespace EuclideanGeometry /-! ### Geometrical results on Euclidean affine spaces This section develops some geometrical definitions and results on Euclidean affine spaces. -/ variable {V : Type*} {P : Type*} variable [NormedAddCommGroup V] [InnerProductSpace ℝ V] [MetricSpace P] variable [NormedAddTorsor V P] /-- The inner product of two vectors given with `weightedVSub`, in terms of the pairwise distances. -/ theorem inner_weightedVSub {ι₁ : Type*} {s₁ : Finset ι₁} {w₁ : ι₁ → ℝ} (p₁ : ι₁ → P) (h₁ : ∑ i ∈ s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : Finset ι₂} {w₂ : ι₂ → ℝ} (p₂ : ι₂ → P) (h₂ : ∑ i ∈ s₂, w₂ i = 0) : ⟪s₁.weightedVSub p₁ w₁, s₂.weightedVSub p₂ w₂⟫ = (-∑ i₁ ∈ s₁, ∑ i₂ ∈ s₂, w₁ i₁ * w₂ i₂ * (dist (p₁ i₁) (p₂ i₂) * dist (p₁ i₁) (p₂ i₂))) / 2 := by rw [Finset.weightedVSub_apply, Finset.weightedVSub_apply, inner_sum_smul_sum_smul_of_sum_eq_zero _ h₁ _ h₂] simp_rw [vsub_sub_vsub_cancel_right] rcongr (i₁ i₂) <;> rw [dist_eq_norm_vsub V (p₁ i₁) (p₂ i₂)] /-- The distance between two points given with `affineCombination`, in terms of the pairwise distances between the points in that combination. -/ theorem dist_affineCombination {ι : Type*} {s : Finset ι} {w₁ w₂ : ι → ℝ} (p : ι → P) (h₁ : ∑ i ∈ s, w₁ i = 1) (h₂ : ∑ i ∈ s, w₂ i = 1) : by have a₁ := s.affineCombination ℝ p w₁ have a₂ := s.affineCombination ℝ p w₂ exact dist a₁ a₂ * dist a₁ a₂ = (-∑ i₁ ∈ s, ∑ i₂ ∈ s, (w₁ - w₂) i₁ * (w₁ - w₂) i₂ * (dist (p i₁) (p i₂) * dist (p i₁) (p i₂))) / 2 := by dsimp only rw [dist_eq_norm_vsub V (s.affineCombination ℝ p w₁) (s.affineCombination ℝ p w₂), ← @inner_self_eq_norm_mul_norm ℝ, Finset.affineCombination_vsub] have h : (∑ i ∈ s, (w₁ - w₂) i) = 0 := by simp_rw [Pi.sub_apply, Finset.sum_sub_distrib, h₁, h₂, sub_self] exact inner_weightedVSub p h p h -- Porting note: `inner_vsub_vsub_of_dist_eq_of_dist_eq` moved to `PerpendicularBisector` /-- The squared distance between points on a line (expressed as a multiple of a fixed vector added to a point) and another point, expressed as a quadratic. -/ theorem dist_smul_vadd_sq (r : ℝ) (v : V) (p₁ p₂ : P) : dist (r • v +ᵥ p₁) p₂ * dist (r • v +ᵥ p₁) p₂ = ⟪v, v⟫ * r * r + 2 * ⟪v, p₁ -ᵥ p₂⟫ * r + ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ := by rw [dist_eq_norm_vsub V _ p₂, ← real_inner_self_eq_norm_mul_norm, vadd_vsub_assoc, real_inner_add_add_self, real_inner_smul_left, real_inner_smul_left, real_inner_smul_right] ring /-- The condition for two points on a line to be equidistant from another point. -/ theorem dist_smul_vadd_eq_dist {v : V} (p₁ p₂ : P) (hv : v ≠ 0) (r : ℝ) : dist (r • v +ᵥ p₁) p₂ = dist p₁ p₂ ↔ r = 0 ∨ r = -2 * ⟪v, p₁ -ᵥ p₂⟫ / ⟪v, v⟫ := by conv_lhs => rw [← mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_smul_vadd_sq, mul_assoc, ← sub_eq_zero, add_sub_assoc, dist_eq_norm_vsub V p₁ p₂, ← real_inner_self_eq_norm_mul_norm, sub_self] have hvi : ⟪v, v⟫ ≠ 0 := by simpa using hv have hd : discrim ⟪v, v⟫ (2 * ⟪v, p₁ -ᵥ p₂⟫) 0 = 2 * ⟪v, p₁ -ᵥ p₂⟫ * (2 * ⟪v, p₁ -ᵥ p₂⟫) := by rw [discrim] ring rw [quadratic_eq_zero_iff hvi hd, neg_add_cancel, zero_div, neg_mul_eq_neg_mul, ← mul_sub_right_distrib, sub_eq_add_neg, ← mul_two, mul_assoc, mul_div_assoc, mul_div_mul_left, mul_div_assoc] norm_num open AffineSubspace Module /-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at most two points `p₁` `p₂` in a two-dimensional subspace containing those points (two circles intersect in at most two points). -/ theorem eq_of_dist_eq_of_dist_eq_of_mem_of_finrank_eq_two {s : AffineSubspace ℝ P} [FiniteDimensional ℝ s.direction] (hd : finrank ℝ s.direction = 2) {c₁ c₂ p₁ p₂ p : P} (hc₁s : c₁ ∈ s) (hc₂s : c₂ ∈ s) (hp₁s : p₁ ∈ s) (hp₂s : p₂ ∈ s) (hps : p ∈ s) {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ := by have ho : ⟪c₂ -ᵥ c₁, p₂ -ᵥ p₁⟫ = 0 := inner_vsub_vsub_of_dist_eq_of_dist_eq (hp₁c₁.trans hp₂c₁.symm) (hp₁c₂.trans hp₂c₂.symm) have hop : ⟪c₂ -ᵥ c₁, p -ᵥ p₁⟫ = 0 := inner_vsub_vsub_of_dist_eq_of_dist_eq (hp₁c₁.trans hpc₁.symm) (hp₁c₂.trans hpc₂.symm) let b : Fin 2 → V := ![c₂ -ᵥ c₁, p₂ -ᵥ p₁] have hb : LinearIndependent ℝ b := by refine linearIndependent_of_ne_zero_of_inner_eq_zero ?_ ?_ · intro i fin_cases i <;> simp [b, hc.symm, hp.symm] · intro i j hij fin_cases i <;> fin_cases j <;> try exact False.elim (hij rfl) · exact ho · rw [real_inner_comm] exact ho have hbs : Submodule.span ℝ (Set.range b) = s.direction := by refine Submodule.eq_of_le_of_finrank_eq ?_ ?_ · rw [Submodule.span_le, Set.range_subset_iff] intro i fin_cases i · exact vsub_mem_direction hc₂s hc₁s · exact vsub_mem_direction hp₂s hp₁s · rw [finrank_span_eq_card hb, Fintype.card_fin, hd] have hv : ∀ v ∈ s.direction, ∃ t₁ t₂ : ℝ, v = t₁ • (c₂ -ᵥ c₁) + t₂ • (p₂ -ᵥ p₁) := by intro v hv have hr : Set.range b = {c₂ -ᵥ c₁, p₂ -ᵥ p₁} := by have hu : (Finset.univ : Finset (Fin 2)) = {0, 1} := by decide classical rw [← Fintype.coe_image_univ, hu] simp [b] rw [← hbs, hr, Submodule.mem_span_insert] at hv rcases hv with ⟨t₁, v', hv', hv⟩ rw [Submodule.mem_span_singleton] at hv' rcases hv' with ⟨t₂, rfl⟩ exact ⟨t₁, t₂, hv⟩ rcases hv (p -ᵥ p₁) (vsub_mem_direction hps hp₁s) with ⟨t₁, t₂, hpt⟩ simp only [hpt, inner_add_right, inner_smul_right, ho, mul_zero, add_zero, mul_eq_zero, inner_self_eq_zero, vsub_eq_zero_iff_eq, hc.symm, or_false] at hop rw [hop, zero_smul, zero_add, ← eq_vadd_iff_vsub_eq] at hpt subst hpt have hp' : (p₂ -ᵥ p₁ : V) ≠ 0 := by simp [hp.symm] have hp₂ : dist ((1 : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁) c₁ = r₁ := by simp [hp₂c₁] rw [← hp₁c₁, dist_smul_vadd_eq_dist _ _ hp'] at hpc₁ hp₂ simp only [one_ne_zero, false_or] at hp₂ rw [hp₂.symm] at hpc₁ rcases hpc₁ with hpc₁ | hpc₁ <;> simp [hpc₁] /-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at most two points `p₁` `p₂` in two-dimensional space (two circles intersect in at most two points). -/ theorem eq_of_dist_eq_of_dist_eq_of_finrank_eq_two [FiniteDimensional ℝ V] (hd : finrank ℝ V = 2) {c₁ c₂ p₁ p₂ p : P} {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ := haveI hd' : finrank ℝ (⊤ : AffineSubspace ℝ P).direction = 2 := by rw [direction_top, finrank_top] exact hd eq_of_dist_eq_of_dist_eq_of_mem_of_finrank_eq_two hd' (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) hc hp hp₁c₁ hp₂c₁ hpc₁ hp₁c₂ hp₂c₂ hpc₂ end EuclideanGeometry
Mathlib/Geometry/Euclidean/Basic.lean
632
636
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Order.Group.Pointwise.Interval import Mathlib.Analysis.SpecificLimits.Basic import Mathlib.Data.Rat.Cardinal import Mathlib.SetTheory.Cardinal.Continuum /-! # The cardinality of the reals This file shows that the real numbers have cardinality continuum, i.e. `#ℝ = 𝔠`. We show that `#ℝ ≤ 𝔠` by noting that every real number is determined by a Cauchy-sequence of the form `ℕ → ℚ`, which has cardinality `𝔠`. To show that `#ℝ ≥ 𝔠` we define an injection from `{0, 1} ^ ℕ` to `ℝ` with `f ↦ Σ n, f n * (1 / 3) ^ n`. We conclude that all intervals with distinct endpoints have cardinality continuum. ## Main definitions * `Cardinal.cantorFunction` is the function that sends `f` in `{0, 1} ^ ℕ` to `ℝ` by `f ↦ Σ' n, f n * (1 / 3) ^ n` ## Main statements * `Cardinal.mk_real : #ℝ = 𝔠`: the reals have cardinality continuum. * `Cardinal.not_countable_real`: the universal set of real numbers is not countable. We can use this same proof to show that all the other sets in this file are not countable. * 8 lemmas of the form `mk_Ixy_real` for `x,y ∈ {i,o,c}` state that intervals on the reals have cardinality continuum. ## Notation * `𝔠` : notation for `Cardinal.continuum` in locale `Cardinal`, defined in `SetTheory.Continuum`. ## Tags continuum, cardinality, reals, cardinality of the reals -/ open Nat Set open Cardinal noncomputable section namespace Cardinal variable {c : ℝ} {f g : ℕ → Bool} {n : ℕ} /-- The body of the sum in `cantorFunction`. `cantorFunctionAux c f n = c ^ n` if `f n = true`; `cantorFunctionAux c f n = 0` if `f n = false`. -/ def cantorFunctionAux (c : ℝ) (f : ℕ → Bool) (n : ℕ) : ℝ := cond (f n) (c ^ n) 0 @[simp] theorem cantorFunctionAux_true (h : f n = true) : cantorFunctionAux c f n = c ^ n := by simp [cantorFunctionAux, h] @[simp] theorem cantorFunctionAux_false (h : f n = false) : cantorFunctionAux c f n = 0 := by simp [cantorFunctionAux, h] theorem cantorFunctionAux_nonneg (h : 0 ≤ c) : 0 ≤ cantorFunctionAux c f n := by cases h' : f n · simp [h'] · simpa [h'] using pow_nonneg h _ theorem cantorFunctionAux_eq (h : f n = g n) : cantorFunctionAux c f n = cantorFunctionAux c g n := by simp [cantorFunctionAux, h] theorem cantorFunctionAux_zero (f : ℕ → Bool) : cantorFunctionAux c f 0 = cond (f 0) 1 0 := by cases h : f 0 <;> simp [h] theorem cantorFunctionAux_succ (f : ℕ → Bool) : (fun n => cantorFunctionAux c f (n + 1)) = fun n => c * cantorFunctionAux c (fun n => f (n + 1)) n := by ext n cases h : f (n + 1) <;> simp [h, _root_.pow_succ'] theorem summable_cantor_function (f : ℕ → Bool) (h1 : 0 ≤ c) (h2 : c < 1) : Summable (cantorFunctionAux c f) := by apply (summable_geometric_of_lt_one h1 h2).summable_of_eq_zero_or_self intro n; cases h : f n <;> simp [h] /-- `cantorFunction c (f : ℕ → Bool)` is `Σ n, f n * c ^ n`, where `true` is interpreted as `1` and `false` is interpreted as `0`. It is implemented using `cantorFunctionAux`. -/ def cantorFunction (c : ℝ) (f : ℕ → Bool) : ℝ := ∑' n, cantorFunctionAux c f n theorem cantorFunction_le (h1 : 0 ≤ c) (h2 : c < 1) (h3 : ∀ n, f n → g n) : cantorFunction c f ≤ cantorFunction c g := by apply (summable_cantor_function f h1 h2).tsum_le_tsum _ (summable_cantor_function g h1 h2) intro n; cases h : f n · simp [h, cantorFunctionAux_nonneg h1] replace h3 : g n = true := h3 n h; simp [h, h3] theorem cantorFunction_succ (f : ℕ → Bool) (h1 : 0 ≤ c) (h2 : c < 1) : cantorFunction c f = cond (f 0) 1 0 + c * cantorFunction c fun n => f (n + 1) := by rw [cantorFunction, (summable_cantor_function f h1 h2).tsum_eq_zero_add] rw [cantorFunctionAux_succ, tsum_mul_left, cantorFunctionAux, pow_zero, cantorFunction] /-- `cantorFunction c` is strictly increasing with if `0 < c < 1/2`, if we endow `ℕ → Bool` with a lexicographic order. The lexicographic order doesn't exist for these infinitary products, so we explicitly write out what it means. -/ theorem increasing_cantorFunction (h1 : 0 < c) (h2 : c < 1 / 2) {n : ℕ} {f g : ℕ → Bool} (hn : ∀ k < n, f k = g k) (fn : f n = false) (gn : g n = true) : cantorFunction c f < cantorFunction c g := by have h3 : c < 1 := by apply h2.trans norm_num induction' n with n ih generalizing f g · let f_max : ℕ → Bool := fun n => Nat.rec false (fun _ _ => true) n have hf_max : ∀ n, f n → f_max n := by intro n hn cases n · rw [fn] at hn contradiction simp [f_max] let g_min : ℕ → Bool := fun n => Nat.rec true (fun _ _ => false) n have hg_min : ∀ n, g_min n → g n := by intro n hn cases n · rw [gn] simp at hn apply (cantorFunction_le (le_of_lt h1) h3 hf_max).trans_lt refine lt_of_lt_of_le ?_ (cantorFunction_le (le_of_lt h1) h3 hg_min) have : c / (1 - c) < 1 := by rw [div_lt_one, lt_sub_iff_add_lt] · convert _root_.add_lt_add h2 h2 norm_num rwa [sub_pos] convert this · rw [cantorFunction_succ _ (le_of_lt h1) h3, div_eq_mul_inv, ← tsum_geometric_of_lt_one (le_of_lt h1) h3] apply zero_add · refine (tsum_eq_single 0 ?_).trans ?_ · intro n hn cases n · contradiction simp [g_min] · exact cantorFunctionAux_zero _ rw [cantorFunction_succ f (le_of_lt h1) h3, cantorFunction_succ g (le_of_lt h1) h3] rw [hn 0 <| zero_lt_succ n] apply add_lt_add_left rw [mul_lt_mul_left h1] exact ih (fun k hk => hn _ <| Nat.succ_lt_succ hk) fn gn /-- `cantorFunction c` is injective if `0 < c < 1/2`. -/ theorem cantorFunction_injective (h1 : 0 < c) (h2 : c < 1 / 2) : Function.Injective (cantorFunction c) := by intro f g hfg classical contrapose hfg with h have : ∃ n, f n ≠ g n := Function.ne_iff.mp h let n := Nat.find this have hn : ∀ k : ℕ, k < n → f k = g k := by intro k hk apply of_not_not exact Nat.find_min this hk cases fn : f n · apply _root_.ne_of_lt refine increasing_cantorFunction h1 h2 hn fn ?_ apply Bool.eq_true_of_not_eq_false rw [← fn] apply Ne.symm exact Nat.find_spec this · apply _root_.ne_of_gt refine increasing_cantorFunction h1 h2 (fun k hk => (hn k hk).symm) ?_ fn apply Bool.eq_false_of_not_eq_true rw [← fn] apply Ne.symm exact Nat.find_spec this /-- The cardinality of the reals, as a type. -/ theorem mk_real : #ℝ = 𝔠 := by apply le_antisymm · rw [Real.equivCauchy.cardinal_eq] apply mk_quotient_le.trans apply (mk_subtype_le _).trans_eq rw [← power_def, mk_nat, mkRat, aleph0_power_aleph0] · convert mk_le_of_injective (cantorFunction_injective _ _) · rw [← power_def, mk_bool, mk_nat, two_power_aleph0] · exact 1 / 3 · norm_num · norm_num /-- The cardinality of the reals, as a set. -/ theorem mk_univ_real : #(Set.univ : Set ℝ) = 𝔠 := by rw [mk_univ, mk_real] /-- **Non-Denumerability of the Continuum**: The reals are not countable. -/ instance : Uncountable ℝ := by rw [← aleph0_lt_mk_iff, mk_real] exact aleph0_lt_continuum theorem not_countable_real : ¬(Set.univ : Set ℝ).Countable := not_countable_univ /-- The cardinality of the interval (a, ∞). -/ theorem mk_Ioi_real (a : ℝ) : #(Ioi a) = 𝔠 := by refine le_antisymm (mk_real ▸ mk_set_le _) ?_ rw [← not_lt] intro h refine _root_.ne_of_lt ?_ mk_univ_real have hu : Iio a ∪ {a} ∪ Ioi a = Set.univ := by convert @Iic_union_Ioi ℝ _ _ exact Iio_union_right rw [← hu] refine lt_of_le_of_lt (mk_union_le _ _) ?_ refine lt_of_le_of_lt (add_le_add_right (mk_union_le _ _) _) ?_ have h2 : (fun x => a + a - x) '' Ioi a = Iio a := by convert @image_const_sub_Ioi ℝ _ _ _ simp rw [← h2] refine add_lt_of_lt (cantor _).le ?_ h refine add_lt_of_lt (cantor _).le (mk_image_le.trans_lt h) ?_ rw [mk_singleton] exact one_lt_aleph0.trans (cantor _) /-- The cardinality of the interval [a, ∞). -/ theorem mk_Ici_real (a : ℝ) : #(Ici a) = 𝔠 := le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioi_real a ▸ mk_le_mk_of_subset Ioi_subset_Ici_self) /-- The cardinality of the interval (-∞, a). -/ theorem mk_Iio_real (a : ℝ) : #(Iio a) = 𝔠 := by refine le_antisymm (mk_real ▸ mk_set_le _) ?_ have h2 : (fun x => a + a - x) '' Iio a = Ioi a := by simp only [image_const_sub_Iio, add_sub_cancel_right] exact mk_Ioi_real a ▸ h2 ▸ mk_image_le /-- The cardinality of the interval (-∞, a]. -/ theorem mk_Iic_real (a : ℝ) : #(Iic a) = 𝔠 := le_antisymm (mk_real ▸ mk_set_le _) (mk_Iio_real a ▸ mk_le_mk_of_subset Iio_subset_Iic_self) /-- The cardinality of the interval (a, b). -/ theorem mk_Ioo_real {a b : ℝ} (h : a < b) : #(Ioo a b) = 𝔠 := by refine le_antisymm (mk_real ▸ mk_set_le _) ?_ have h1 : #((fun x => x - a) '' Ioo a b) ≤ #(Ioo a b) := mk_image_le refine le_trans ?_ h1 rw [image_sub_const_Ioo, sub_self] replace h := sub_pos_of_lt h have h2 : #(Inv.inv '' Ioo 0 (b - a)) ≤ #(Ioo 0 (b - a)) := mk_image_le refine le_trans ?_ h2 rw [image_inv_eq_inv, inv_Ioo_0_left h, mk_Ioi_real] /-- The cardinality of the interval [a, b). -/ theorem mk_Ico_real {a b : ℝ} (h : a < b) : #(Ico a b) = 𝔠 :=
le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioo_real h ▸ mk_le_mk_of_subset Ioo_subset_Ico_self) /-- The cardinality of the interval [a, b]. -/ theorem mk_Icc_real {a b : ℝ} (h : a < b) : #(Icc a b) = 𝔠 := le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioo_real h ▸ mk_le_mk_of_subset Ioo_subset_Icc_self)
Mathlib/Data/Real/Cardinality.lean
252
256
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro -/ import Mathlib.Data.List.AList import Mathlib.Data.Finset.Sigma import Mathlib.Data.Part /-! # Finite maps over `Multiset` -/ universe u v w open List variable {α : Type u} {β : α → Type v} /-! ### Multisets of sigma types -/ namespace Multiset /-- Multiset of keys of an association multiset. -/ def keys (s : Multiset (Sigma β)) : Multiset α := s.map Sigma.fst @[simp] theorem coe_keys {l : List (Sigma β)} : keys (l : Multiset (Sigma β)) = (l.keys : Multiset α) := rfl @[simp] theorem keys_zero : keys (0 : Multiset (Sigma β)) = 0 := rfl @[simp] theorem keys_cons {a : α} {b : β a} {s : Multiset (Sigma β)} : keys (⟨a, b⟩ ::ₘ s) = a ::ₘ keys s := by simp [keys] @[simp] theorem keys_singleton {a : α} {b : β a} : keys ({⟨a, b⟩} : Multiset (Sigma β)) = {a} := rfl /-- `NodupKeys s` means that `s` has no duplicate keys. -/ def NodupKeys (s : Multiset (Sigma β)) : Prop := Quot.liftOn s List.NodupKeys fun _ _ p => propext <| perm_nodupKeys p @[simp] theorem coe_nodupKeys {l : List (Sigma β)} : @NodupKeys α β l ↔ l.NodupKeys := Iff.rfl lemma nodup_keys {m : Multiset (Σ a, β a)} : m.keys.Nodup ↔ m.NodupKeys := by rcases m with ⟨l⟩; rfl alias ⟨_, NodupKeys.nodup_keys⟩ := nodup_keys protected lemma NodupKeys.nodup {m : Multiset (Σ a, β a)} (h : m.NodupKeys) : m.Nodup := h.nodup_keys.of_map _ end Multiset /-! ### Finmap -/ /-- `Finmap β` is the type of finite maps over a multiset. It is effectively a quotient of `AList β` by permutation of the underlying list. -/ structure Finmap (β : α → Type v) : Type max u v where /-- The underlying `Multiset` of a `Finmap` -/ entries : Multiset (Sigma β) /-- There are no duplicate keys in `entries` -/ nodupKeys : entries.NodupKeys /-- The quotient map from `AList` to `Finmap`. -/ def AList.toFinmap (s : AList β) : Finmap β := ⟨s.entries, s.nodupKeys⟩ local notation:arg "⟦" a "⟧" => AList.toFinmap a theorem AList.toFinmap_eq {s₁ s₂ : AList β} : toFinmap s₁ = toFinmap s₂ ↔ s₁.entries ~ s₂.entries := by cases s₁ cases s₂ simp [AList.toFinmap] @[simp] theorem AList.toFinmap_entries (s : AList β) : ⟦s⟧.entries = s.entries := rfl /-- Given `l : List (Sigma β)`, create a term of type `Finmap β` by removing entries with duplicate keys. -/ def List.toFinmap [DecidableEq α] (s : List (Sigma β)) : Finmap β := s.toAList.toFinmap namespace Finmap open AList lemma nodup_entries (f : Finmap β) : f.entries.Nodup := f.nodupKeys.nodup /-! ### Lifting from AList -/ /-- Lift a permutation-respecting function on `AList` to `Finmap`. -/ def liftOn {γ} (s : Finmap β) (f : AList β → γ) (H : ∀ a b : AList β, a.entries ~ b.entries → f a = f b) : γ := by refine (Quotient.liftOn s.entries (fun (l : List (Sigma β)) => (⟨_, fun nd => f ⟨l, nd⟩⟩ : Part γ)) (fun l₁ l₂ p => Part.ext' (perm_nodupKeys p) ?_) : Part γ).get ?_ · exact fun h1 h2 => H _ _ p · have := s.nodupKeys revert this rcases s.entries with ⟨l⟩ exact id @[simp] theorem liftOn_toFinmap {γ} (s : AList β) (f : AList β → γ) (H) : liftOn ⟦s⟧ f H = f s := by cases s rfl /-- Lift a permutation-respecting function on 2 `AList`s to 2 `Finmap`s. -/ def liftOn₂ {γ} (s₁ s₂ : Finmap β) (f : AList β → AList β → γ) (H : ∀ a₁ b₁ a₂ b₂ : AList β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries → f a₁ b₁ = f a₂ b₂) : γ := liftOn s₁ (fun l₁ => liftOn s₂ (f l₁) fun _ _ p => H _ _ _ _ (Perm.refl _) p) fun a₁ a₂ p => by have H' : f a₁ = f a₂ := funext fun _ => H _ _ _ _ p (Perm.refl _) simp only [H'] @[simp] theorem liftOn₂_toFinmap {γ} (s₁ s₂ : AList β) (f : AList β → AList β → γ) (H) : liftOn₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ := by cases s₁; cases s₂; rfl /-! ### Induction -/ @[elab_as_elim] theorem induction_on {C : Finmap β → Prop} (s : Finmap β) (H : ∀ a : AList β, C ⟦a⟧) : C s := by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩ @[elab_as_elim] theorem induction_on₂ {C : Finmap β → Finmap β → Prop} (s₁ s₂ : Finmap β) (H : ∀ a₁ a₂ : AList β, C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ := induction_on s₁ fun l₁ => induction_on s₂ fun l₂ => H l₁ l₂ @[elab_as_elim] theorem induction_on₃ {C : Finmap β → Finmap β → Finmap β → Prop} (s₁ s₂ s₃ : Finmap β) (H : ∀ a₁ a₂ a₃ : AList β, C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ := induction_on₂ s₁ s₂ fun l₁ l₂ => induction_on s₃ fun l₃ => H l₁ l₂ l₃ /-! ### extensionality -/ @[ext] theorem ext : ∀ {s t : Finmap β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩, ⟨l₂, _⟩, H => by congr @[simp] theorem ext_iff' {s t : Finmap β} : s.entries = t.entries ↔ s = t := Finmap.ext_iff.symm /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : Membership α (Finmap β) := ⟨fun s a => a ∈ s.entries.keys⟩ theorem mem_def {a : α} {s : Finmap β} : a ∈ s ↔ a ∈ s.entries.keys := Iff.rfl @[simp] theorem mem_toFinmap {a : α} {s : AList β} : a ∈ toFinmap s ↔ a ∈ s := Iff.rfl /-! ### keys -/ /-- The set of keys of a finite map. -/ def keys (s : Finmap β) : Finset α := ⟨s.entries.keys, s.nodupKeys.nodup_keys⟩ @[simp] theorem keys_val (s : AList β) : (keys ⟦s⟧).val = s.keys := rfl @[simp] theorem keys_ext {s₁ s₂ : AList β} : keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys := by simp [keys, AList.keys] theorem mem_keys {a : α} {s : Finmap β} : a ∈ s.keys ↔ a ∈ s := induction_on s fun _ => AList.mem_keys /-! ### empty -/ /-- The empty map. -/ instance : EmptyCollection (Finmap β) := ⟨⟨0, nodupKeys_nil⟩⟩ instance : Inhabited (Finmap β) := ⟨∅⟩ @[simp] theorem empty_toFinmap : (⟦∅⟧ : Finmap β) = ∅ := rfl @[simp] theorem toFinmap_nil [DecidableEq α] : ([].toFinmap : Finmap β) = ∅ := rfl theorem not_mem_empty {a : α} : a ∉ (∅ : Finmap β) := Multiset.not_mem_zero a @[simp] theorem keys_empty : (∅ : Finmap β).keys = ∅ := rfl /-! ### singleton -/ /-- The singleton map. -/ def singleton (a : α) (b : β a) : Finmap β := ⟦AList.singleton a b⟧ @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = {a} := rfl @[simp] theorem mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y := by simp [singleton, mem_def] section variable [DecidableEq α] instance decidableEq [∀ a, DecidableEq (β a)] : DecidableEq (Finmap β) | _, _ => decidable_of_iff _ Finmap.ext_iff.symm /-! ### lookup -/ /-- Look up the value associated to a key in a map. -/ def lookup (a : α) (s : Finmap β) : Option (β a) := liftOn s (AList.lookup a) fun _ _ => perm_lookup @[simp] theorem lookup_toFinmap (a : α) (s : AList β) : lookup a ⟦s⟧ = s.lookup a := rfl @[simp] theorem dlookup_list_toFinmap (a : α) (s : List (Sigma β)) : lookup a s.toFinmap = s.dlookup a := by rw [List.toFinmap, lookup_toFinmap, lookup_to_alist] @[simp] theorem lookup_empty (a) : lookup a (∅ : Finmap β) = none := rfl theorem lookup_isSome {a : α} {s : Finmap β} : (s.lookup a).isSome ↔ a ∈ s := induction_on s fun _ => AList.lookup_isSome theorem lookup_eq_none {a} {s : Finmap β} : lookup a s = none ↔ a ∉ s := induction_on s fun _ => AList.lookup_eq_none lemma mem_lookup_iff {s : Finmap β} {a : α} {b : β a} : b ∈ s.lookup a ↔ Sigma.mk a b ∈ s.entries := by rcases s with ⟨⟨l⟩, hl⟩; exact List.mem_dlookup_iff hl lemma lookup_eq_some_iff {s : Finmap β} {a : α} {b : β a} : s.lookup a = b ↔ Sigma.mk a b ∈ s.entries := mem_lookup_iff @[simp] lemma sigma_keys_lookup (s : Finmap β) : s.keys.sigma (fun i => (s.lookup i).toFinset) = ⟨s.entries, s.nodup_entries⟩ := by ext x have : x ∈ s.entries → x.1 ∈ s.keys := Multiset.mem_map_of_mem _ simpa [lookup_eq_some_iff] @[simp] theorem lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b := by rw [singleton, lookup_toFinmap, AList.singleton, AList.lookup, dlookup_cons_eq] instance (a : α) (s : Finmap β) : Decidable (a ∈ s) := decidable_of_iff _ lookup_isSome theorem mem_iff {a : α} {s : Finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b := induction_on s fun s => Iff.trans List.mem_keys <| exists_congr fun _ => (mem_dlookup_iff s.nodupKeys).symm theorem mem_of_lookup_eq_some {a : α} {b : β a} {s : Finmap β} (h : s.lookup a = some b) : a ∈ s := mem_iff.mpr ⟨_, h⟩ theorem ext_lookup {s₁ s₂ : Finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ := induction_on₂ s₁ s₂ fun s₁ s₂ h => by simp only [AList.lookup, lookup_toFinmap] at h rw [AList.toFinmap_eq] apply lookup_ext s₁.nodupKeys s₂.nodupKeys intro x y rw [h] /-- An equivalence between `Finmap β` and pairs `(keys : Finset α, lookup : ∀ a, Option (β a))` such that `(lookup a).isSome ↔ a ∈ keys`. -/ @[simps apply_coe_fst apply_coe_snd] def keysLookupEquiv : Finmap β ≃ { f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1 } where toFun s := ⟨(s.keys, fun i => s.lookup i), fun _ => lookup_isSome⟩ invFun f := mk (f.1.1.sigma fun i => (f.1.2 i).toFinset).val <| by refine Multiset.nodup_keys.1 ((Finset.nodup _).map_on ?_) simp only [Finset.mem_val, Finset.mem_sigma, Option.mem_toFinset, Option.mem_def] rintro ⟨i, x⟩ ⟨_, hx⟩ ⟨j, y⟩ ⟨_, hy⟩ (rfl : i = j) simpa using hx.symm.trans hy left_inv f := ext <| by simp right_inv := fun ⟨(s, f), hf⟩ => by dsimp only at hf ext · simp [keys, Multiset.keys, ← hf, Option.isSome_iff_exists] · simp +contextual [lookup_eq_some_iff, ← hf] @[simp] lemma keysLookupEquiv_symm_apply_keys : ∀ f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}, (keysLookupEquiv.symm f).keys = f.1.1 := keysLookupEquiv.surjective.forall.2 fun _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_fst] @[simp] lemma keysLookupEquiv_symm_apply_lookup : ∀ (f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}) a, (keysLookupEquiv.symm f).lookup a = f.1.2 a := keysLookupEquiv.surjective.forall.2 fun _ _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_snd] /-! ### replace -/ /-- Replace a key with a given value in a finite map. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.replace a b t)) fun _ _ p => toFinmap_eq.2 <| perm_replace p @[simp] theorem replace_toFinmap (a : α) (b : β a) (s : AList β) : replace a b ⟦s⟧ = (⟦s.replace a b⟧ : Finmap β) := by simp [replace] @[simp] theorem keys_replace (a : α) (b : β a) (s : Finmap β) : (replace a b s).keys = s.keys := induction_on s fun s => by simp @[simp] theorem mem_replace {a a' : α} {b : β a} {s : Finmap β} : a' ∈ replace a b s ↔ a' ∈ s := induction_on s fun s => by simp end /-! ### foldl -/ /-- Fold a commutative function over the key-value pairs in the map -/ def foldl {δ : Type w} (f : δ → ∀ a, β a → δ) (H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁) (d : δ) (m : Finmap β) : δ := letI : RightCommutative fun d (s : Sigma β) ↦ f d s.1 s.2 := ⟨fun _ _ _ ↦ H _ _ _ _ _⟩ m.entries.foldl (fun d s => f d s.1 s.2) d /-- `any f s` returns `true` iff there exists a value `v` in `s` such that `f v = true`. -/ def any (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x || f y z) (fun _ _ _ _ => by simp_rw [Bool.or_assoc, Bool.or_comm, imp_true_iff]) false /-- `all f s` returns `true` iff `f v = true` for all values `v` in `s`. -/ def all (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x && f y z) (fun _ _ _ _ => by simp_rw [Bool.and_assoc, Bool.and_comm, imp_true_iff]) true /-! ### erase -/ section variable [DecidableEq α] /-- Erase a key from the map. If the key is not present it does nothing. -/ def erase (a : α) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.erase a t)) fun _ _ p => toFinmap_eq.2 <| perm_erase p @[simp] theorem erase_toFinmap (a : α) (s : AList β) : erase a ⟦s⟧ = AList.toFinmap (s.erase a) := by simp [erase] @[simp] theorem keys_erase_toFinset (a : α) (s : AList β) : keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a := by simp [Finset.erase, keys, AList.erase, keys_kerase] @[simp] theorem keys_erase (a : α) (s : Finmap β) : (erase a s).keys = s.keys.erase a := induction_on s fun s => by simp @[simp] theorem mem_erase {a a' : α} {s : Finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := induction_on s fun s => by simp theorem not_mem_erase_self {a : α} {s : Finmap β} : ¬a ∈ erase a s := by rw [mem_erase, not_and_or, not_not] left rfl @[simp] theorem lookup_erase (a) (s : Finmap β) : lookup a (erase a s) = none := induction_on s <| AList.lookup_erase a @[simp] theorem lookup_erase_ne {a a'} {s : Finmap β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := induction_on s fun _ => AList.lookup_erase_ne h theorem erase_erase {a a' : α} {s : Finmap β} : erase a (erase a' s) = erase a' (erase a s) := induction_on s fun s => ext (by simp only [AList.erase_erase, erase_toFinmap]) /-! ### sdiff -/ /-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or `s'` but not both. -/ def sdiff (s s' : Finmap β) : Finmap β := s'.foldl (fun s x _ => s.erase x) (fun _ _ _ _ _ => erase_erase) s instance : SDiff (Finmap β) := ⟨sdiff⟩ /-! ### insert -/ /-- Insert a key-value pair into a finite map, replacing any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.insert a b t)) fun _ _ p => toFinmap_eq.2 <| perm_insert p @[simp] theorem insert_toFinmap (a : α) (b : β a) (s : AList β) : insert a b (AList.toFinmap s) = AList.toFinmap (s.insert a b) := by simp [insert] theorem entries_insert_of_not_mem {a : α} {b : β a} {s : Finmap β} : a ∉ s → (insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries :=
induction_on s fun s h => by simp [AList.entries_insert_of_not_mem (mt mem_toFinmap.1 h), -entries_insert]
Mathlib/Data/Finmap.lean
429
430
/- Copyright (c) 2023 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Topology.Algebra.Module.Multilinear.Bounded import Mathlib.Topology.Algebra.Module.UniformConvergence import Mathlib.Topology.Algebra.SeparationQuotient.Section import Mathlib.Topology.Hom.ContinuousEvalConst import Mathlib.Topology.Algebra.InfiniteSum.Basic /-! # Topology on continuous multilinear maps In this file we define `TopologicalSpace` and `UniformSpace` structures on `ContinuousMultilinearMap 𝕜 E F`, where `E i` is a family of vector spaces over `𝕜` with topologies and `F` is a topological vector space. -/ open Bornology Function Set Topology open scoped UniformConvergence Filter namespace ContinuousMultilinearMap variable {𝕜 ι : Type*} {E : ι → Type*} {F : Type*} [NormedField 𝕜] [∀ i, TopologicalSpace (E i)] [∀ i, AddCommGroup (E i)] [∀ i, Module 𝕜 (E i)] [AddCommGroup F] [Module 𝕜 F] /-- An auxiliary definition used to define topology on `ContinuousMultilinearMap 𝕜 E F`. -/ def toUniformOnFun [TopologicalSpace F] (f : ContinuousMultilinearMap 𝕜 E F) : (Π i, E i) →ᵤ[{s | IsVonNBounded 𝕜 s}] F := UniformOnFun.ofFun _ f open UniformOnFun in lemma range_toUniformOnFun [DecidableEq ι] [TopologicalSpace F] : range toUniformOnFun = {f : (Π i, E i) →ᵤ[{s | IsVonNBounded 𝕜 s}] F | Continuous (toFun _ f) ∧ (∀ (m : Π i, E i) i x y, toFun _ f (update m i (x + y)) = toFun _ f (update m i x) + toFun _ f (update m i y)) ∧ (∀ (m : Π i, E i) i (c : 𝕜) x, toFun _ f (update m i (c • x)) = c • toFun _ f (update m i x))} := by ext f constructor · rintro ⟨f, rfl⟩ exact ⟨f.cont, f.map_update_add, f.map_update_smul⟩ · rintro ⟨hcont, hadd, hsmul⟩ exact ⟨⟨⟨f, by intro; convert hadd, by intro; convert hsmul⟩, hcont⟩, rfl⟩ @[simp] lemma toUniformOnFun_toFun [TopologicalSpace F] (f : ContinuousMultilinearMap 𝕜 E F) : UniformOnFun.toFun _ f.toUniformOnFun = f := rfl instance instTopologicalSpace [TopologicalSpace F] [IsTopologicalAddGroup F] : TopologicalSpace (ContinuousMultilinearMap 𝕜 E F) := .induced toUniformOnFun <| @UniformOnFun.topologicalSpace _ _ (IsTopologicalAddGroup.toUniformSpace F) _ instance instUniformSpace [UniformSpace F] [IsUniformAddGroup F] : UniformSpace (ContinuousMultilinearMap 𝕜 E F) := .replaceTopology (.comap toUniformOnFun <| UniformOnFun.uniformSpace _ _ _) <| by rw [instTopologicalSpace, IsUniformAddGroup.toUniformSpace_eq]; rfl section IsUniformAddGroup variable [UniformSpace F] [IsUniformAddGroup F] lemma isUniformInducing_toUniformOnFun : IsUniformInducing (toUniformOnFun : ContinuousMultilinearMap 𝕜 E F → ((Π i, E i) →ᵤ[{s | IsVonNBounded 𝕜 s}] F)) := ⟨rfl⟩ lemma isUniformEmbedding_toUniformOnFun : IsUniformEmbedding (toUniformOnFun : ContinuousMultilinearMap 𝕜 E F → _) := ⟨isUniformInducing_toUniformOnFun, DFunLike.coe_injective⟩ lemma isEmbedding_toUniformOnFun : IsEmbedding (toUniformOnFun : ContinuousMultilinearMap 𝕜 E F → ((Π i, E i) →ᵤ[{s | IsVonNBounded 𝕜 s}] F)) := isUniformEmbedding_toUniformOnFun.isEmbedding @[deprecated (since := "2024-10-26")] alias embedding_toUniformOnFun := isEmbedding_toUniformOnFun theorem uniformContinuous_coe_fun [∀ i, ContinuousSMul 𝕜 (E i)] : UniformContinuous (DFunLike.coe : ContinuousMultilinearMap 𝕜 E F → (Π i, E i) → F) := (UniformOnFun.uniformContinuous_toFun isVonNBounded_covers).comp isUniformEmbedding_toUniformOnFun.uniformContinuous theorem uniformContinuous_eval_const [∀ i, ContinuousSMul 𝕜 (E i)] (x : Π i, E i) : UniformContinuous fun f : ContinuousMultilinearMap 𝕜 E F ↦ f x := uniformContinuous_pi.1 uniformContinuous_coe_fun x instance instIsUniformAddGroup : IsUniformAddGroup (ContinuousMultilinearMap 𝕜 E F) := let φ : ContinuousMultilinearMap 𝕜 E F →+ (Π i, E i) →ᵤ[{s | IsVonNBounded 𝕜 s}] F := { toFun := toUniformOnFun, map_add' := fun _ _ ↦ rfl, map_zero' := rfl } isUniformEmbedding_toUniformOnFun.isUniformAddGroup φ instance instUniformContinuousConstSMul {M : Type*} [Monoid M] [DistribMulAction M F] [SMulCommClass 𝕜 M F] [ContinuousConstSMul M F] : UniformContinuousConstSMul M (ContinuousMultilinearMap 𝕜 E F) := haveI := uniformContinuousConstSMul_of_continuousConstSMul M F isUniformEmbedding_toUniformOnFun.uniformContinuousConstSMul fun _ _ ↦ rfl theorem isUniformInducing_postcomp {G : Type*} [AddCommGroup G] [UniformSpace G] [IsUniformAddGroup G] [Module 𝕜 G] (g : F →L[𝕜] G) (hg : IsUniformInducing g) : IsUniformInducing (g.compContinuousMultilinearMap : ContinuousMultilinearMap 𝕜 E F → ContinuousMultilinearMap 𝕜 E G) := by rw [← isUniformInducing_toUniformOnFun.of_comp_iff] exact (UniformOnFun.postcomp_isUniformInducing hg).comp isUniformInducing_toUniformOnFun section CompleteSpace variable [∀ i, ContinuousSMul 𝕜 (E i)] [ContinuousConstSMul 𝕜 F] [CompleteSpace F] open UniformOnFun in theorem completeSpace (h : IsCoherentWith {s : Set (Π i, E i) | IsVonNBounded 𝕜 s}) : CompleteSpace (ContinuousMultilinearMap 𝕜 E F) := by classical wlog hF : T2Space F generalizing F · rw [(isUniformInducing_postcomp (SeparationQuotient.mkCLM _ _) SeparationQuotient.isUniformInducing_mk).completeSpace_congr] · exact this inferInstance · intro f use (SeparationQuotient.outCLM _ _).compContinuousMultilinearMap f simp [DFunLike.ext_iff] have H : ∀ {m : Π i, E i}, Continuous fun f : (Π i, E i) →ᵤ[{s | IsVonNBounded 𝕜 s}] F ↦ toFun _ f m := (uniformContinuous_eval (isVonNBounded_covers) _).continuous rw [completeSpace_iff_isComplete_range isUniformInducing_toUniformOnFun, range_toUniformOnFun] simp only [setOf_and, setOf_forall] apply_rules [IsClosed.isComplete, IsClosed.inter] · exact UniformOnFun.isClosed_setOf_continuous h · exact isClosed_iInter fun m ↦ isClosed_iInter fun i ↦ isClosed_iInter fun x ↦ isClosed_iInter fun y ↦ isClosed_eq H (H.add H)
· exact isClosed_iInter fun m ↦ isClosed_iInter fun i ↦ isClosed_iInter fun c ↦ isClosed_iInter fun x ↦ isClosed_eq H (H.const_smul _) instance instCompleteSpace [∀ i, IsTopologicalAddGroup (E i)] [SequentialSpace (Π i, E i)] : CompleteSpace (ContinuousMultilinearMap 𝕜 E F) := completeSpace <| .of_seq fun _u x hux ↦ (hux.isVonNBounded_range 𝕜).insert x end CompleteSpace section RestrictScalars
Mathlib/Topology/Algebra/Module/Multilinear/Topology.lean
139
149
/- Copyright (c) 2022 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Kevin Klinge, Andrew Yang -/ import Mathlib.Algebra.Group.Submonoid.DistribMulAction import Mathlib.GroupTheory.OreLocalization.Basic import Mathlib.Algebra.GroupWithZero.Defs /-! # Localization over left Ore sets. This file proves results on the localization of rings (monoids with zeros) over a left Ore set. ## References * <https://ncatlab.org/nlab/show/Ore+localization> * [Zoran Škoda, *Noncommutative localization in noncommutative geometry*][skoda2006] ## Tags localization, Ore, non-commutative -/ assert_not_exists RelIso universe u open OreLocalization namespace OreLocalization section MonoidWithZero variable {R : Type*} [MonoidWithZero R] {S : Submonoid R} [OreSet S] @[simp] theorem zero_oreDiv' (s : S) : (0 : R) /ₒ s = 0 := by rw [OreLocalization.zero_def, oreDiv_eq_iff] exact ⟨s, 1, by simp [Submonoid.smul_def]⟩ instance : MonoidWithZero R[S⁻¹] where zero_mul x := by induction' x using OreLocalization.ind with r s rw [OreLocalization.zero_def, oreDiv_mul_char 0 r 1 s 0 1 (by simp), zero_mul, one_mul] mul_zero x := by induction' x using OreLocalization.ind with r s rw [OreLocalization.zero_def, mul_div_one, mul_zero, zero_oreDiv', zero_oreDiv'] end MonoidWithZero section CommMonoidWithZero variable {R : Type*} [CommMonoidWithZero R] {S : Submonoid R} [OreSet S] instance : CommMonoidWithZero R[S⁻¹] where __ := inferInstanceAs (MonoidWithZero R[S⁻¹]) __ := inferInstanceAs (CommMonoid R[S⁻¹]) end CommMonoidWithZero section DistribMulAction variable {R : Type*} [Monoid R] {S : Submonoid R} [OreSet S] {X : Type*} [AddMonoid X] variable [DistribMulAction R X] private def add'' (r₁ : X) (s₁ : S) (r₂ : X) (s₂ : S) : X[S⁻¹] := (oreDenom (s₁ : R) s₂ • r₁ + oreNum (s₁ : R) s₂ • r₂) /ₒ (oreDenom (s₁ : R) s₂ * s₁) private theorem add''_char (r₁ : X) (s₁ : S) (r₂ : X) (s₂ : S) (rb : R) (sb : R) (hb : sb * s₁ = rb * s₂) (h : sb * s₁ ∈ S) : add'' r₁ s₁ r₂ s₂ = (sb • r₁ + rb • r₂) /ₒ ⟨sb * s₁, h⟩ := by simp only [add''] have ha := ore_eq (s₁ : R) s₂ generalize oreNum (s₁ : R) s₂ = ra at * generalize oreDenom (s₁ : R) s₂ = sa at * rw [oreDiv_eq_iff] rcases oreCondition sb sa with ⟨rc, sc, hc⟩ have : sc * rb * s₂ = rc * ra * s₂ := by rw [mul_assoc rc, ← ha, ← mul_assoc, ← hc, mul_assoc, mul_assoc, hb] rcases ore_right_cancel _ _ s₂ this with ⟨sd, hd⟩ use sd * sc use sd * rc simp only [smul_add, smul_smul, Submonoid.smul_def, Submonoid.coe_mul] constructor · rw [mul_assoc _ _ rb, hd, mul_assoc, hc, mul_assoc, mul_assoc] · rw [mul_assoc, ← mul_assoc (sc : R), hc, mul_assoc, mul_assoc] attribute [local instance] OreLocalization.oreEqv private def add' (r₂ : X) (s₂ : S) : X[S⁻¹] → X[S⁻¹] := (--plus tilde Quotient.lift fun r₁s₁ : X × S => add'' r₁s₁.1 r₁s₁.2 r₂ s₂) <| by -- Porting note: `assoc_rw` & `noncomm_ring` were not ported yet rintro ⟨r₁', s₁'⟩ ⟨r₁, s₁⟩ ⟨sb, rb, hb, hb'⟩ -- s*, r* rcases oreCondition (s₁' : R) s₂ with ⟨rc, sc, hc⟩ --s~~, r~~ rcases oreCondition rb sc with ⟨rd, sd, hd⟩ -- s#, r# dsimp at * rw [add''_char _ _ _ _ rc sc hc (sc * s₁').2] have : sd * sb * s₁ = rd * rc * s₂ := by rw [mul_assoc, hb', ← mul_assoc, hd, mul_assoc, hc, ← mul_assoc] rw [add''_char _ _ _ _ (rd * rc : R) (sd * sb) this (sd * sb * s₁).2] rw [mul_smul, ← Submonoid.smul_def sb, hb, smul_smul, hd, oreDiv_eq_iff] use 1 use rd simp only [mul_smul, smul_add, one_smul, OneMemClass.coe_one, one_mul, true_and] rw [this, hc, mul_assoc] /-- The addition on the Ore localization. -/ @[irreducible] private def add : X[S⁻¹] → X[S⁻¹] → X[S⁻¹] := fun x => Quotient.lift (fun rs : X × S => add' rs.1 rs.2 x) (by rintro ⟨r₁, s₁⟩ ⟨r₂, s₂⟩ ⟨sb, rb, hb, hb'⟩ induction' x with r₃ s₃ show add'' _ _ _ _ = add'' _ _ _ _ dsimp only at * rcases oreCondition (s₃ : R) s₂ with ⟨rc, sc, hc⟩ rcases oreCondition rc sb with ⟨rd, sd, hd⟩ have : rd * rb * s₁ = sd * sc * s₃ := by rw [mul_assoc, ← hb', ← mul_assoc, ← hd, mul_assoc, ← hc, mul_assoc] rw [add''_char _ _ _ _ rc sc hc (sc * s₃).2] rw [add''_char _ _ _ _ _ _ this.symm (sd * sc * s₃).2] refine oreDiv_eq_iff.mpr ?_ simp only [Submonoid.mk_smul, smul_add] use sd, 1 simp only [one_smul, one_mul, mul_smul, ← hb, Submonoid.smul_def, ← mul_assoc, and_true] simp only [smul_smul, hd]) instance : Add X[S⁻¹] := ⟨add⟩ theorem oreDiv_add_oreDiv {r r' : X} {s s' : S} :
r /ₒ s + r' /ₒ s' = (oreDenom (s : R) s' • r + oreNum (s : R) s' • r') /ₒ (oreDenom (s : R) s' * s) := by with_unfolding_all rfl theorem oreDiv_add_char' {r r' : X} (s s' : S) (rb : R) (sb : R) (h : sb * s = rb * s') (h' : sb * s ∈ S) : r /ₒ s + r' /ₒ s' = (sb • r + rb • r') /ₒ ⟨sb * s, h'⟩ := by with_unfolding_all exact add''_char r s r' s' rb sb h h' /-- A characterization of the addition on the Ore localizaion, allowing for arbitrary Ore numerator and Ore denominator. -/ theorem oreDiv_add_char {r r' : X} (s s' : S) (rb : R) (sb : S) (h : sb * s = rb * s') : r /ₒ s + r' /ₒ s' = (sb • r + rb • r') /ₒ (sb * s) := oreDiv_add_char' s s' rb sb h (sb * s).2
Mathlib/RingTheory/OreLocalization/Basic.lean
140
153
/- Copyright (c) 2022 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri, Sébastien Gouëzel, Heather Macbeth, Floris van Doorn -/ import Mathlib.Topology.FiberBundle.Basic /-! # Standard constructions on fiber bundles This file contains several standard constructions on fiber bundles: * `Bundle.Trivial.fiberBundle 𝕜 B F`: the trivial fiber bundle with model fiber `F` over the base `B` * `FiberBundle.prod`: for fiber bundles `E₁` and `E₂` over a common base, a fiber bundle structure on their fiberwise product `E₁ ×ᵇ E₂` (the notation stands for `fun x ↦ E₁ x × E₂ x`). * `FiberBundle.pullback`: for a fiber bundle `E` over `B`, a fiber bundle structure on its pullback `f *ᵖ E` by a map `f : B' → B` (the notation is a type synonym for `E ∘ f`). ## Tags fiber bundle, fibre bundle, fiberwise product, pullback -/ open Bundle Filter Set TopologicalSpace Topology /-! ### The trivial bundle -/ namespace Bundle namespace Trivial variable (B : Type*) (F : Type*) -- TODO: use `TotalSpace.toProd` instance topologicalSpace [t₁ : TopologicalSpace B] [t₂ : TopologicalSpace F] : TopologicalSpace (TotalSpace F (Trivial B F)) := induced TotalSpace.proj t₁ ⊓ induced (TotalSpace.trivialSnd B F) t₂ variable [TopologicalSpace B] [TopologicalSpace F] theorem isInducing_toProd : IsInducing (TotalSpace.toProd B F) := ⟨by simp only [instTopologicalSpaceProd, induced_inf, induced_compose]; rfl⟩ @[deprecated (since := "2024-10-28")] alias inducing_toProd := isInducing_toProd /-- Homeomorphism between the total space of the trivial bundle and the Cartesian product. -/ def homeomorphProd : TotalSpace F (Trivial B F) ≃ₜ B × F := (TotalSpace.toProd _ _).toHomeomorphOfIsInducing (isInducing_toProd B F)
/-- Local trivialization for trivial bundle. -/ def trivialization : Trivialization F (π F (Bundle.Trivial B F)) where
Mathlib/Topology/FiberBundle/Constructions.lean
54
55
/- 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, Alexander Bentkamp -/ import Mathlib.LinearAlgebra.FreeModule.Basic import Mathlib.LinearAlgebra.LinearIndependent.Lemmas import Mathlib.LinearAlgebra.LinearPMap import Mathlib.LinearAlgebra.Projection /-! # Bases in a vector space This file provides results for bases of a vector space. Some of these results should be merged with the results on free modules. We state these results in a separate file to the results on modules to avoid an import cycle. ## Main statements * `Basis.ofVectorSpace` states that every vector space has a basis. * `Module.Free.of_divisionRing` states that every vector space is a free module. ## Tags basis, bases -/ open Function Set Submodule variable {ι : Type*} {ι' : Type*} {K : Type*} {V : Type*} {V' : Type*} section DivisionRing variable [DivisionRing K] [AddCommGroup V] [AddCommGroup V'] [Module K V] [Module K V'] variable {v : ι → V} {s t : Set V} {x y z : V} open Submodule namespace Basis section ExistsBasis /-- If `s` is a linear independent set of vectors, we can extend it to a basis. -/ noncomputable def extend (hs : LinearIndepOn K id s) : Basis (hs.extend (subset_univ s)) K V := Basis.mk (hs.linearIndepOn_extend _).linearIndependent_restrict (SetLike.coe_subset_coe.mp <| by simpa using hs.subset_span_extend (subset_univ s)) theorem extend_apply_self (hs : LinearIndepOn K id s) (x : hs.extend _) : Basis.extend hs x = x := Basis.mk_apply _ _ _ @[simp] theorem coe_extend (hs : LinearIndepOn K id s) : ⇑(Basis.extend hs) = ((↑) : _ → _) := funext (extend_apply_self hs) theorem range_extend (hs : LinearIndepOn K id s) : range (Basis.extend hs) = hs.extend (subset_univ _) := by rw [coe_extend, Subtype.range_coe_subtype, setOf_mem_eq] /-- Auxiliary definition: the index for the new basis vectors in `Basis.sumExtend`. The specific value of this definition should be considered an implementation detail. -/ def sumExtendIndex (hs : LinearIndependent K v) : Set V := LinearIndepOn.extend hs.linearIndepOn_id (subset_univ _) \ range v /-- If `v` is a linear independent family of vectors, extend it to a basis indexed by a sum type. -/ noncomputable def sumExtend (hs : LinearIndependent K v) : Basis (ι ⊕ sumExtendIndex hs) K V := let s := Set.range v let e : ι ≃ s := Equiv.ofInjective v hs.injective let b := hs.linearIndepOn_id.extend (subset_univ (Set.range v)) (Basis.extend hs.linearIndepOn_id).reindex <| Equiv.symm <| calc ι ⊕ (b \ s : Set V) ≃ s ⊕ (b \ s : Set V) := Equiv.sumCongr e (Equiv.refl _) _ ≃ b := haveI := Classical.decPred (· ∈ s) Equiv.Set.sumDiffSubset (hs.linearIndepOn_id.subset_extend _) theorem subset_extend {s : Set V} (hs : LinearIndepOn K id s) : s ⊆ hs.extend (Set.subset_univ _) := hs.subset_extend _ /-- If `s` is a family of linearly independent vectors contained in a set `t` spanning `V`, then one can get a basis of `V` containing `s` and contained in `t`. -/ noncomputable def extendLe (hs : LinearIndepOn K id s) (hst : s ⊆ t) (ht : ⊤ ≤ span K t) : Basis (hs.extend hst) K V := Basis.mk ((hs.linearIndepOn_extend _).linearIndependent ..) (le_trans ht <| Submodule.span_le.2 <| by simpa using hs.subset_span_extend hst) theorem extendLe_apply_self (hs : LinearIndepOn K id s) (hst : s ⊆ t) (ht : ⊤ ≤ span K t) (x : hs.extend hst) : Basis.extendLe hs hst ht x = x := Basis.mk_apply _ _ _ @[simp] theorem coe_extendLe (hs : LinearIndepOn K id s) (hst : s ⊆ t) (ht : ⊤ ≤ span K t) : ⇑(Basis.extendLe hs hst ht) = ((↑) : _ → _) := funext (extendLe_apply_self hs hst ht) theorem range_extendLe (hs : LinearIndepOn K id s) (hst : s ⊆ t) (ht : ⊤ ≤ span K t) : range (Basis.extendLe hs hst ht) = hs.extend hst := by rw [coe_extendLe, Subtype.range_coe_subtype, setOf_mem_eq] theorem subset_extendLe (hs : LinearIndepOn K id s) (hst : s ⊆ t) (ht : ⊤ ≤ span K t) : s ⊆ range (Basis.extendLe hs hst ht) := (range_extendLe hs hst ht).symm ▸ hs.subset_extend hst theorem extendLe_subset (hs : LinearIndepOn K id s) (hst : s ⊆ t) (ht : ⊤ ≤ span K t) : range (Basis.extendLe hs hst ht) ⊆ t := (range_extendLe hs hst ht).symm ▸ hs.extend_subset hst /-- If a set `s` spans the space, this is a basis contained in `s`. -/ noncomputable def ofSpan (hs : ⊤ ≤ span K s) : Basis ((linearIndepOn_empty K id).extend (empty_subset s)) K V := extendLe (linearIndependent_empty K V) (empty_subset s) hs theorem ofSpan_apply_self (hs : ⊤ ≤ span K s) (x : (linearIndepOn_empty K id).extend (empty_subset s)) : Basis.ofSpan hs x = x := extendLe_apply_self (linearIndependent_empty K V) (empty_subset s) hs x @[simp] theorem coe_ofSpan (hs : ⊤ ≤ span K s) : ⇑(ofSpan hs) = ((↑) : _ → _) := funext (ofSpan_apply_self hs) theorem range_ofSpan (hs : ⊤ ≤ span K s) : range (ofSpan hs) = (linearIndepOn_empty K id).extend (empty_subset s) := by rw [coe_ofSpan, Subtype.range_coe_subtype, setOf_mem_eq] theorem ofSpan_subset (hs : ⊤ ≤ span K s) : range (ofSpan hs) ⊆ s := extendLe_subset (linearIndependent_empty K V) (empty_subset s) hs section variable (K V) /-- A set used to index `Basis.ofVectorSpace`. -/ noncomputable def ofVectorSpaceIndex : Set V := (linearIndepOn_empty K id).extend (subset_univ _) /-- Each vector space has a basis. -/ noncomputable def ofVectorSpace : Basis (ofVectorSpaceIndex K V) K V := Basis.extend (linearIndependent_empty K V) @[stacks 09FN "Generalized from fields to division rings."] instance (priority := 100) _root_.Module.Free.of_divisionRing : Module.Free K V := Module.Free.of_basis (ofVectorSpace K V)
theorem ofVectorSpace_apply_self (x : ofVectorSpaceIndex K V) : ofVectorSpace K V x = x := by unfold ofVectorSpace exact Basis.mk_apply _ _ _
Mathlib/LinearAlgebra/Basis/VectorSpace.lean
152
154
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Control.Combinators import Mathlib.Data.Option.Defs import Mathlib.Logic.IsEmpty import Mathlib.Logic.Relator import Mathlib.Util.CompileInductive import Aesop /-! # Option of a type This file develops the basic theory of option types. If `α` is a type, then `Option α` can be understood as the type with one more element than `α`. `Option α` has terms `some a`, where `a : α`, and `none`, which is the added element. This is useful in multiple ways: * It is the prototype of addition of terms to a type. See for example `WithBot α` which uses `none` as an element smaller than all others. * It can be used to define failsafe partial functions, which return `some the_result_we_expect` if we can find `the_result_we_expect`, and `none` if there is no meaningful result. This forces any subsequent use of the partial function to explicitly deal with the exceptions that make it return `none`. * `Option` is a monad. We love monads. `Part` is an alternative to `Option` that can be seen as the type of `True`/`False` values along with a term `a : α` if the value is `True`. -/ universe u namespace Option variable {α β γ δ : Type*} theorem coe_def : (fun a ↦ ↑a : α → Option α) = some := rfl theorem mem_map {f : α → β} {y : β} {o : Option α} : y ∈ o.map f ↔ ∃ x ∈ o, f x = y := by simp -- The simpNF linter says that the LHS can be simplified via `Option.mem_def`. -- However this is a higher priority lemma. -- It seems the side condition `H` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Function.Injective f) {a : α} {o : Option α} : f a ∈ o.map f ↔ a ∈ o := by aesop theorem forall_mem_map {f : α → β} {o : Option α} {p : β → Prop} : (∀ y ∈ o.map f, p y) ↔ ∀ x ∈ o, p (f x) := by simp theorem exists_mem_map {f : α → β} {o : Option α} {p : β → Prop} : (∃ y ∈ o.map f, p y) ↔ ∃ x ∈ o, p (f x) := by simp theorem coe_get {o : Option α} (h : o.isSome) : ((Option.get _ h : α) : Option α) = o := Option.some_get h theorem eq_of_mem_of_mem {a : α} {o1 o2 : Option α} (h1 : a ∈ o1) (h2 : a ∈ o2) : o1 = o2 := h1.trans h2.symm theorem Mem.leftUnique : Relator.LeftUnique ((· ∈ ·) : α → Option α → Prop) := fun _ _ _=> mem_unique theorem some_injective (α : Type*) : Function.Injective (@some α) := fun _ _ ↦ some_inj.mp /-- `Option.map f` is injective if `f` is injective. -/ theorem map_injective {f : α → β} (Hf : Function.Injective f) : Function.Injective (Option.map f) | none, none, _ => rfl | some a₁, some a₂, H => by rw [Hf (Option.some.inj H)] @[simp] theorem map_comp_some (f : α → β) : Option.map f ∘ some = some ∘ f := rfl @[simp] theorem none_bind' (f : α → Option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → Option β) : (some a).bind f = f a := rfl theorem bind_eq_some' {x : Option α} {f : α → Option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x <;> simp @[congr] theorem bind_congr' {f g : α → Option β} {x y : Option α} (hx : x = y) (hf : ∀ a ∈ y, f a = g a) : x.bind f = y.bind g := hx.symm ▸ bind_congr hf @[deprecated bind_congr (since := "2025-03-20")] -- This was renamed from `bind_congr` after https://github.com/leanprover/lean4/pull/7529 -- upstreamed it with a slightly different statement. theorem bind_congr'' {f g : α → Option β} {x : Option α} (h : ∀ a ∈ x, f a = g a) : x.bind f = x.bind g := by cases x <;> simp only [some_bind, none_bind, mem_def, h] theorem joinM_eq_join : joinM = @join α := funext fun _ ↦ rfl theorem bind_eq_bind' {α β : Type u} {f : α → Option β} {x : Option α} : x >>= f = x.bind f := rfl theorem map_coe {α β} {a : α} {f : α → β} : f <$> (a : Option α) = ↑(f a) := rfl @[simp] theorem map_coe' {a : α} {f : α → β} : Option.map f (a : Option α) = ↑(f a) := rfl /-- `Option.map` as a function between functions is injective. -/ theorem map_injective' : Function.Injective (@Option.map α β) := fun f g h ↦ funext fun x ↦ some_injective _ <| by simp only [← map_some', h] @[simp] theorem map_inj {f g : α → β} : Option.map f = Option.map g ↔ f = g := map_injective'.eq_iff attribute [simp] map_id @[simp] theorem map_eq_id {f : α → α} : Option.map f = id ↔ f = id := map_injective'.eq_iff' map_id theorem map_comm {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ} (h : g₁ ∘ f₁ = g₂ ∘ f₂) (a : α) : (Option.map f₁ a).map g₁ = (Option.map f₂ a).map g₂ := by rw [map_map, h, ← map_map] section pmap variable {p : α → Prop} (f : ∀ a : α, p a → β) (x : Option α) @[simp] theorem pbind_eq_bind (f : α → Option β) (x : Option α) : (x.pbind fun a _ ↦ f a) = x.bind f := by cases x <;> simp only [pbind, none_bind', some_bind'] theorem map_bind' (f : β → γ) (x : Option α) (g : α → Option β) : Option.map f (x.bind g) = x.bind fun a ↦ Option.map f (g a) := by cases x <;> simp theorem pbind_map (f : α → β) (x : Option α) (g : ∀ b : β, b ∈ x.map f → Option γ) : pbind (Option.map f x) g = x.pbind fun a h ↦ g (f a) (mem_map_of_mem _ h) := by cases x <;> rfl theorem mem_pmem {a : α} (h : ∀ a ∈ x, p a) (ha : a ∈ x) : f a (h a ha) ∈ pmap f x h := by rw [mem_def] at ha ⊢ subst ha rfl theorem pmap_bind {α β γ} {x : Option α} {g : α → Option β} {p : β → Prop} {f : ∀ b, p b → γ} (H) (H' : ∀ (a : α), ∀ b ∈ g a, b ∈ x >>= g) : pmap f (x >>= g) H = x >>= fun a ↦ pmap f (g a) fun _ h ↦ H _ (H' a _ h) := by cases x <;> simp only [pmap, bind_eq_bind, none_bind, some_bind] theorem bind_pmap {α β γ} {p : α → Prop} (f : ∀ a, p a → β) (x : Option α) (g : β → Option γ) (H) : pmap f x H >>= g = x.pbind fun a h ↦ g (f a (H _ h)) := by cases x <;> simp only [pmap, bind_eq_bind, none_bind, some_bind, pbind] variable {f x} theorem pbind_eq_none {f : ∀ a : α, a ∈ x → Option β} (h' : ∀ a (H : a ∈ x), f a H = none → x = none) : x.pbind f = none ↔ x = none := by cases x · simp · simp only [pbind, iff_false, reduceCtorEq] intro h cases h' _ rfl h theorem pbind_eq_some {f : ∀ a : α, a ∈ x → Option β} {y : β} : x.pbind f = some y ↔ ∃ (z : α) (H : z ∈ x), f z H = some y := by rcases x with (_|x) · simp · simp only [pbind] refine ⟨fun h ↦ ⟨x, rfl, h⟩, ?_⟩ rintro ⟨z, H, hz⟩ simp only [mem_def, Option.some_inj] at H simpa [H] using hz theorem join_pmap_eq_pmap_join {f : ∀ a, p a → β} {x : Option (Option α)} (H) : (pmap (pmap f) x H).join = pmap f x.join fun a h ↦ H (some a) (mem_of_mem_join h) _ rfl := by rcases x with (_ | _ | x) <;> simp /-- `simp`-normal form of `join_pmap_eq_pmap_join` -/ @[simp] theorem pmap_bind_id_eq_pmap_join {f : ∀ a, p a → β} {x : Option (Option α)} (H) : ((pmap (pmap f) x H).bind fun a ↦ a) = pmap f x.join fun a h ↦ H (some a) (mem_of_mem_join h) _ rfl := by rcases x with (_ | _ | x) <;> simp end pmap @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orElse' (a : α) (x : Option α) : (some a).orElse (fun _ ↦ x) = some a := rfl @[simp] theorem none_orElse' (x : Option α) : none.orElse (fun _ ↦ x) = x := by cases x <;> rfl @[simp] theorem orElse_none' (x : Option α) : x.orElse (fun _ ↦ none) = x := by cases x <;> rfl theorem exists_ne_none {p : Option α → Prop} : (∃ x ≠ none, p x) ↔ (∃ x : α, p x) := by simp only [← exists_prop, bex_ne_none] theorem iget_mem [Inhabited α] : ∀ {o : Option α}, isSome o → o.iget ∈ o | some _, _ => rfl theorem iget_of_mem [Inhabited α] {a : α} : ∀ {o : Option α}, a ∈ o → o.iget = a | _, rfl => rfl theorem getD_default_eq_iget [Inhabited α] (o : Option α) : o.getD default = o.iget := by cases o <;> rfl @[simp] theorem guard_eq_some' {p : Prop} [Decidable p] (u) : _root_.guard p = some u ↔ p := by cases u by_cases h : p <;> simp [_root_.guard, h] theorem liftOrGet_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, liftOrGet f o₁ o₂ = o₁ ∨ liftOrGet f o₁ o₂ = o₂ | none, none => Or.inl rfl | some _, none => Or.inl rfl | none, some _ => Or.inr rfl | some a, some b => by simpa [liftOrGet] using h a b /-- Given an element of `a : Option α`, a default element `b : β` and a function `α → β`, apply this function to `a` if it comes from `α`, and return `b` otherwise. -/ def casesOn' : Option α → β → (α → β) → β | none, n, _ => n | some a, _, s => s a @[simp] theorem casesOn'_none (x : β) (f : α → β) : casesOn' none x f = x := rfl @[simp] theorem casesOn'_some (x : β) (f : α → β) (a : α) : casesOn' (some a) x f = f a := rfl @[simp] theorem casesOn'_coe (x : β) (f : α → β) (a : α) : casesOn' (a : Option α) x f = f a := rfl @[simp] theorem casesOn'_none_coe (f : Option α → β) (o : Option α) : casesOn' o (f none) (f ∘ (fun a ↦ ↑a)) = f o := by cases o <;> rfl lemma casesOn'_eq_elim (b : β) (f : α → β) (a : Option α) : Option.casesOn' a b f = Option.elim a b f := by cases a <;> rfl theorem orElse_eq_some (o o' : Option α) (x : α) : (o <|> o') = some x ↔ o = some x ∨ o = none ∧ o' = some x := by cases o · simp only [true_and, false_or, eq_self_iff_true, none_orElse, reduceCtorEq] · simp only [some_orElse, or_false, false_and, reduceCtorEq] theorem orElse_eq_some' (o o' : Option α) (x : α) : o.orElse (fun _ ↦ o') = some x ↔ o = some x ∨ o = none ∧ o' = some x := Option.orElse_eq_some o o' x @[simp] theorem orElse_eq_none (o o' : Option α) : (o <|> o') = none ↔ o = none ∧ o' = none := by cases o · simp only [true_and, none_orElse, eq_self_iff_true] · simp only [some_orElse, reduceCtorEq, false_and]
@[simp] theorem orElse_eq_none' (o o' : Option α) : o.orElse (fun _ ↦ o') = none ↔ o = none ∧ o' = none := Option.orElse_eq_none o o'
Mathlib/Data/Option/Basic.lean
275
277
/- Copyright (c) 2024 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.MvPolynomial.Monad import Mathlib.LinearAlgebra.Charpoly.ToMatrix import Mathlib.LinearAlgebra.FreeModule.StrongRankCondition import Mathlib.LinearAlgebra.Matrix.Charpoly.Univ import Mathlib.RingTheory.TensorProduct.Finite import Mathlib.RingTheory.TensorProduct.Free /-! # Characteristic polynomials of linear families of endomorphisms The coefficients of the characteristic polynomials of a linear family of endomorphisms are homogeneous polynomials in the parameters. This result is used in Lie theory to establish the existence of regular elements and Cartan subalgebras, and ultimately a well-defined notion of rank for Lie algebras. In this file we prove this result about characteristic polynomials. Let `L` and `M` be modules over a nontrivial commutative ring `R`, and let `φ : L →ₗ[R] Module.End R M` be a linear map. Let `b` be a basis of `L`, indexed by `ι`. Then we define a multivariate polynomial with variables indexed by `ι` that evaluates on elements `x` of `L` to the characteristic polynomial of `φ x`. ## Main declarations * `Matrix.toMvPolynomial M i`: the family of multivariate polynomials that evaluates on `c : n → R` to the dot product of the `i`-th row of `M` with `c`. `Matrix.toMvPolynomial M i` is the sum of the monomials `C (M i j) * X j`. * `LinearMap.toMvPolynomial b₁ b₂ f`: a version of `Matrix.toMvPolynomial` for linear maps `f` with respect to bases `b₁` and `b₂` of the domain and codomain. * `LinearMap.polyCharpoly`: the multivariate polynomial that evaluates on elements `x` of `L` to the characteristic polynomial of `φ x`. * `LinearMap.polyCharpoly_map_eq_charpoly`: the evaluation of `polyCharpoly` on elements `x` of `L` is the characteristic polynomial of `φ x`. * `LinearMap.polyCharpoly_coeff_isHomogeneous`: the coefficients of `polyCharpoly` are homogeneous polynomials in the parameters. * `LinearMap.nilRank`: the smallest index at which `polyCharpoly` has a non-zero coefficient, which is independent of the choice of basis for `L`. * `LinearMap.IsNilRegular`: an element `x` of `L` is *nil-regular* with respect to `φ` if the `n`-th coefficient of the characteristic polynomial of `φ x` is non-zero, where `n` denotes the nil-rank of `φ`. ## Implementation details We show that `LinearMap.polyCharpoly` does not depend on the choice of basis of the target module. This is done via `LinearMap.polyCharpoly_eq_polyCharpolyAux` and `LinearMap.polyCharpolyAux_basisIndep`. The latter is proven by considering the base change of the `R`-linear map `φ : L →ₗ[R] End R M` to the multivariate polynomial ring `MvPolynomial ι R`, and showing that `polyCharpolyAux φ` is equal to the characteristic polynomial of this base change. The proof concludes because characteristic polynomials are independent of the chosen basis. ## References * [barnes1967]: "On Cartan subalgebras of Lie algebras" by D.W. Barnes. -/ open scoped Matrix namespace Matrix variable {m n o R S : Type*} variable [Fintype n] [Fintype o] [CommSemiring R] [CommSemiring S] open MvPolynomial /-- Let `M` be an `(m × n)`-matrix over `R`. Then `Matrix.toMvPolynomial M` is the family (indexed by `i : m`) of multivariate polynomials in `n` variables over `R` that evaluates on `c : n → R` to the dot product of the `i`-th row of `M` with `c`: `Matrix.toMvPolynomial M i` is the sum of the monomials `C (M i j) * X j`. -/ noncomputable def toMvPolynomial (M : Matrix m n R) (i : m) : MvPolynomial n R := ∑ j, monomial (.single j 1) (M i j) lemma toMvPolynomial_eval_eq_apply (M : Matrix m n R) (i : m) (c : n → R) : eval c (M.toMvPolynomial i) = (M *ᵥ c) i := by simp only [toMvPolynomial, map_sum, eval_monomial, pow_zero, Finsupp.prod_single_index, pow_one, mulVec, dotProduct] lemma toMvPolynomial_map (f : R →+* S) (M : Matrix m n R) (i : m) : (M.map f).toMvPolynomial i = MvPolynomial.map f (M.toMvPolynomial i) := by simp only [toMvPolynomial, map_apply, map_sum, map_monomial] lemma toMvPolynomial_isHomogeneous (M : Matrix m n R) (i : m) : (M.toMvPolynomial i).IsHomogeneous 1 := by apply MvPolynomial.IsHomogeneous.sum rintro j - apply MvPolynomial.isHomogeneous_monomial _ _ simp [Finsupp.degree, Finsupp.support_single_ne_zero _ one_ne_zero, Finset.sum_singleton, Finsupp.single_eq_same] lemma toMvPolynomial_totalDegree_le (M : Matrix m n R) (i : m) : (M.toMvPolynomial i).totalDegree ≤ 1 := by apply (toMvPolynomial_isHomogeneous _ _).totalDegree_le @[simp] lemma toMvPolynomial_constantCoeff (M : Matrix m n R) (i : m) : constantCoeff (M.toMvPolynomial i) = 0 := by simp only [toMvPolynomial, ← C_mul_X_eq_monomial, map_sum, map_mul, constantCoeff_X, mul_zero, Finset.sum_const_zero] @[simp] lemma toMvPolynomial_zero : (0 : Matrix m n R).toMvPolynomial = 0 := by ext; simp only [toMvPolynomial, zero_apply, map_zero, Finset.sum_const_zero, Pi.zero_apply] @[simp] lemma toMvPolynomial_one [DecidableEq n] : (1 : Matrix n n R).toMvPolynomial = X := by ext i : 1 rw [toMvPolynomial, Finset.sum_eq_single i] · simp only [one_apply_eq, ← C_mul_X_eq_monomial, C_1, one_mul] · rintro j - hj simp only [one_apply_ne hj.symm, map_zero] · intro h exact (h (Finset.mem_univ _)).elim lemma toMvPolynomial_add (M N : Matrix m n R) : (M + N).toMvPolynomial = M.toMvPolynomial + N.toMvPolynomial := by ext i : 1 simp only [toMvPolynomial, add_apply, map_add, Finset.sum_add_distrib, Pi.add_apply] lemma toMvPolynomial_mul (M : Matrix m n R) (N : Matrix n o R) (i : m) : (M * N).toMvPolynomial i = bind₁ N.toMvPolynomial (M.toMvPolynomial i) := by simp only [toMvPolynomial, mul_apply, map_sum, Finset.sum_comm (γ := o), bind₁, aeval, AlgHom.coe_mk, coe_eval₂Hom, eval₂_monomial, algebraMap_apply, Algebra.id.map_eq_id, RingHom.id_apply, C_apply, pow_zero, Finsupp.prod_single_index, pow_one, Finset.mul_sum, monomial_mul, zero_add] end Matrix namespace LinearMap open MvPolynomial section variable {R M₁ M₂ ι₁ ι₂ : Type*} variable [CommRing R] [AddCommGroup M₁] [AddCommGroup M₂] variable [Module R M₁] [Module R M₂] variable [Fintype ι₁] [Finite ι₂] variable [DecidableEq ι₁] variable (b₁ : Basis ι₁ R M₁) (b₂ : Basis ι₂ R M₂) /-- Let `f : M₁ →ₗ[R] M₂` be an `R`-linear map between modules `M₁` and `M₂` with bases `b₁` and `b₂` respectively. Then `LinearMap.toMvPolynomial b₁ b₂ f` is the family of multivariate polynomials over `R` that evaluates on an element `x` of `M₁` (represented on the basis `b₁`) to the element `f x` of `M₂` (represented on the basis `b₂`). -/ noncomputable def toMvPolynomial (f : M₁ →ₗ[R] M₂) (i : ι₂) : MvPolynomial ι₁ R := (toMatrix b₁ b₂ f).toMvPolynomial i lemma toMvPolynomial_eval_eq_apply (f : M₁ →ₗ[R] M₂) (i : ι₂) (c : ι₁ →₀ R) : eval c (f.toMvPolynomial b₁ b₂ i) = b₂.repr (f (b₁.repr.symm c)) i := by rw [toMvPolynomial, Matrix.toMvPolynomial_eval_eq_apply, ← LinearMap.toMatrix_mulVec_repr b₁ b₂, LinearEquiv.apply_symm_apply] open Algebra.TensorProduct in lemma toMvPolynomial_baseChange (f : M₁ →ₗ[R] M₂) (i : ι₂) (A : Type*) [CommRing A] [Algebra R A] : (f.baseChange A).toMvPolynomial (basis A b₁) (basis A b₂) i = MvPolynomial.map (algebraMap R A) (f.toMvPolynomial b₁ b₂ i) := by simp only [toMvPolynomial, toMatrix_baseChange, Matrix.toMvPolynomial_map] lemma toMvPolynomial_isHomogeneous (f : M₁ →ₗ[R] M₂) (i : ι₂) : (f.toMvPolynomial b₁ b₂ i).IsHomogeneous 1 := Matrix.toMvPolynomial_isHomogeneous _ _ lemma toMvPolynomial_totalDegree_le (f : M₁ →ₗ[R] M₂) (i : ι₂) : (f.toMvPolynomial b₁ b₂ i).totalDegree ≤ 1 := Matrix.toMvPolynomial_totalDegree_le _ _ @[simp] lemma toMvPolynomial_constantCoeff (f : M₁ →ₗ[R] M₂) (i : ι₂) : constantCoeff (f.toMvPolynomial b₁ b₂ i) = 0 := Matrix.toMvPolynomial_constantCoeff _ _ @[simp] lemma toMvPolynomial_zero : (0 : M₁ →ₗ[R] M₂).toMvPolynomial b₁ b₂ = 0 := by unfold toMvPolynomial; simp only [map_zero, Matrix.toMvPolynomial_zero] @[simp] lemma toMvPolynomial_id : (id : M₁ →ₗ[R] M₁).toMvPolynomial b₁ b₁ = X := by unfold toMvPolynomial; simp only [toMatrix_id, Matrix.toMvPolynomial_one] lemma toMvPolynomial_add (f g : M₁ →ₗ[R] M₂) : (f + g).toMvPolynomial b₁ b₂ = f.toMvPolynomial b₁ b₂ + g.toMvPolynomial b₁ b₂ := by unfold toMvPolynomial; simp only [map_add, Matrix.toMvPolynomial_add] end variable {R M₁ M₂ M₃ ι₁ ι₂ ι₃ : Type*} variable [CommRing R] [AddCommGroup M₁] [AddCommGroup M₂] [AddCommGroup M₃] variable [Module R M₁] [Module R M₂] [Module R M₃] variable [Fintype ι₁] [Fintype ι₂] [Finite ι₃] variable [DecidableEq ι₁] [DecidableEq ι₂] variable (b₁ : Basis ι₁ R M₁) (b₂ : Basis ι₂ R M₂) (b₃ : Basis ι₃ R M₃) lemma toMvPolynomial_comp (g : M₂ →ₗ[R] M₃) (f : M₁ →ₗ[R] M₂) (i : ι₃) : (g ∘ₗ f).toMvPolynomial b₁ b₃ i = bind₁ (f.toMvPolynomial b₁ b₂) (g.toMvPolynomial b₂ b₃ i) := by simp only [toMvPolynomial, toMatrix_comp b₁ b₂ b₃, Matrix.toMvPolynomial_mul] rfl end LinearMap variable {R L M n ι ι' ιM : Type*} variable [CommRing R] [AddCommGroup L] [Module R L] [AddCommGroup M] [Module R M] variable (φ : L →ₗ[R] Module.End R M) variable [Fintype ι] [Fintype ι'] [Fintype ιM] [DecidableEq ι] [DecidableEq ι'] namespace LinearMap section aux variable [DecidableEq ιM] (b : Basis ι R L) (bₘ : Basis ιM R M) open Matrix /-- (Implementation detail, see `LinearMap.polyCharpoly`.) Let `L` and `M` be finite free modules over `R`, and let `φ : L →ₗ[R] Module.End R M` be a linear map. Let `b` be a basis of `L` and `bₘ` a basis of `M`. Then `LinearMap.polyCharpolyAux φ b bₘ` is the polynomial that evaluates on elements `x` of `L` to the characteristic polynomial of `φ x` acting on `M`. This definition does not depend on the choice of `bₘ` (see `LinearMap.polyCharpolyAux_basisIndep`). -/ noncomputable def polyCharpolyAux : Polynomial (MvPolynomial ι R) := (charpoly.univ R ιM).map <| MvPolynomial.bind₁ (φ.toMvPolynomial b bₘ.end) open Algebra.TensorProduct MvPolynomial in lemma polyCharpolyAux_baseChange (A : Type*) [CommRing A] [Algebra R A] : polyCharpolyAux (tensorProduct _ _ _ _ ∘ₗ φ.baseChange A) (basis A b) (basis A bₘ) = (polyCharpolyAux φ b bₘ).map (MvPolynomial.map (algebraMap R A)) := by simp only [polyCharpolyAux] rw [← charpoly.univ_map_map _ (algebraMap R A)] simp only [Polynomial.map_map] congr 1 apply ringHom_ext · intro r simp only [RingHom.coe_comp, RingHom.coe_coe, Function.comp_apply, map_C, bind₁_C_right] · rintro ij simp only [RingHom.coe_comp, RingHom.coe_coe, Function.comp_apply, map_X, bind₁_X_right] classical rw [toMvPolynomial_comp _ (basis A (Basis.end bₘ)), ← toMvPolynomial_baseChange] suffices toMvPolynomial (M₂ := (Module.End A (TensorProduct R A M))) (basis A bₘ.end) (basis A bₘ).end (tensorProduct R A M M) ij = X ij by rw [this, bind₁_X_right] simp only [toMvPolynomial, Matrix.toMvPolynomial] suffices ∀ kl, (toMatrix (basis A bₘ.end) (basis A bₘ).end) (tensorProduct R A M M) ij kl = if kl = ij then 1 else 0 by rw [Finset.sum_eq_single ij] · rw [this, if_pos rfl, X] · rintro kl - H rw [this, if_neg H, map_zero] · intro h exact (h (Finset.mem_univ _)).elim intro kl rw [toMatrix_apply, tensorProduct, TensorProduct.AlgebraTensorModule.lift_apply, basis_apply, TensorProduct.lift.tmul, coe_restrictScalars] dsimp only [coe_mk, AddHom.coe_mk, smul_apply, baseChangeHom_apply] rw [one_smul, Basis.baseChange_end, Basis.repr_self_apply] open LinearMap in lemma polyCharpolyAux_map_eq_toMatrix_charpoly (x : L) : (polyCharpolyAux φ b bₘ).map (MvPolynomial.eval (b.repr x)) = (toMatrix bₘ bₘ (φ x)).charpoly := by rw [polyCharpolyAux, Polynomial.map_map, ← MvPolynomial.eval₂Hom_C_eq_bind₁, MvPolynomial.comp_eval₂Hom, charpoly.univ_map_eval₂Hom] congr ext rw [of_apply, Function.curry_apply, toMvPolynomial_eval_eq_apply, LinearEquiv.symm_apply_apply] rfl open LinearMap in lemma polyCharpolyAux_eval_eq_toMatrix_charpoly_coeff (x : L) (i : ℕ) : MvPolynomial.eval (b.repr x) ((polyCharpolyAux φ b bₘ).coeff i) = (toMatrix bₘ bₘ (φ x)).charpoly.coeff i := by simp [← polyCharpolyAux_map_eq_toMatrix_charpoly φ b bₘ x] @[simp] lemma polyCharpolyAux_map_eq_charpoly [Module.Finite R M] [Module.Free R M] (x : L) : (polyCharpolyAux φ b bₘ).map (MvPolynomial.eval (b.repr x)) = (φ x).charpoly := by nontriviality R rw [polyCharpolyAux_map_eq_toMatrix_charpoly, LinearMap.charpoly_toMatrix] @[simp] lemma polyCharpolyAux_coeff_eval [Module.Finite R M] [Module.Free R M] (x : L) (i : ℕ) : MvPolynomial.eval (b.repr x) ((polyCharpolyAux φ b bₘ).coeff i) = (φ x).charpoly.coeff i := by nontriviality R rw [← polyCharpolyAux_map_eq_charpoly φ b bₘ x, Polynomial.coeff_map] lemma polyCharpolyAux_map_eval [Module.Finite R M] [Module.Free R M] (x : ι → R) : (polyCharpolyAux φ b bₘ).map (MvPolynomial.eval x) = (φ (b.repr.symm (Finsupp.equivFunOnFinite.symm x))).charpoly := by simp only [← polyCharpolyAux_map_eq_charpoly φ b bₘ, LinearEquiv.apply_symm_apply, Finsupp.equivFunOnFinite, Equiv.coe_fn_symm_mk, Finsupp.coe_mk] open Algebra.TensorProduct TensorProduct in lemma polyCharpolyAux_map_aeval (A : Type*) [CommRing A] [Algebra R A] [Module.Finite A (A ⊗[R] M)] [Module.Free A (A ⊗[R] M)] (x : ι → A) : (polyCharpolyAux φ b bₘ).map (MvPolynomial.aeval x).toRingHom = LinearMap.charpoly ((tensorProduct R A M M).comp (baseChange A φ) ((basis A b).repr.symm (Finsupp.equivFunOnFinite.symm x))) := by rw [← polyCharpolyAux_map_eval (tensorProduct R A M M ∘ₗ baseChange A φ) _ (basis A bₘ), polyCharpolyAux_baseChange, Polynomial.map_map] congr exact DFunLike.ext _ _ fun f ↦ (MvPolynomial.eval_map (algebraMap R A) x f).symm open Algebra.TensorProduct MvPolynomial in /-- `LinearMap.polyCharpolyAux` is independent of the choice of basis of the target module. Proof strategy: 1. Rewrite `polyCharpolyAux` as the (honest, ordinary) characteristic polynomial of the basechange of `φ` to the multivariate polynomial ring `MvPolynomial ι R`. 2. Use that the characteristic polynomial of a linear map is independent of the choice of basis. This independence result is used transitively via `LinearMap.polyCharpolyAux_map_aeval` and `LinearMap.polyCharpolyAux_map_eq_charpoly`. -/ lemma polyCharpolyAux_basisIndep {ιM' : Type*} [Fintype ιM'] [DecidableEq ιM'] (bₘ' : Basis ιM' R M) : polyCharpolyAux φ b bₘ = polyCharpolyAux φ b bₘ' := by let f : Polynomial (MvPolynomial ι R) → Polynomial (MvPolynomial ι R) := Polynomial.map (MvPolynomial.aeval X).toRingHom have hf : Function.Injective f := by simp only [f, aeval_X_left, AlgHom.toRingHom_eq_coe, AlgHom.id_toRingHom, Polynomial.map_id] exact Polynomial.map_injective (RingHom.id _) Function.injective_id apply hf let _h1 : Module.Finite (MvPolynomial ι R) (TensorProduct R (MvPolynomial ι R) M) := Module.Finite.of_basis (basis (MvPolynomial ι R) bₘ) let _h2 : Module.Free (MvPolynomial ι R) (TensorProduct R (MvPolynomial ι R) M) := Module.Free.of_basis (basis (MvPolynomial ι R) bₘ) simp only [f, polyCharpolyAux_map_aeval, polyCharpolyAux_map_aeval] end aux open Module Matrix variable [Module.Free R M] [Module.Finite R M] (b : Basis ι R L) /-- Let `L` and `M` be finite free modules over `R`, and let `φ : L →ₗ[R] Module.End R M` be a linear family of endomorphisms. Let `b` be a basis of `L` and `bₘ` a basis of `M`. Then `LinearMap.polyCharpoly φ b` is the polynomial that evaluates on elements `x` of `L` to the characteristic polynomial of `φ x` acting on `M`. -/ noncomputable def polyCharpoly : Polynomial (MvPolynomial ι R) := φ.polyCharpolyAux b (Module.Free.chooseBasis R M) lemma polyCharpoly_eq_of_basis [DecidableEq ιM] (bₘ : Basis ιM R M) : polyCharpoly φ b = (charpoly.univ R ιM).map (MvPolynomial.bind₁ (φ.toMvPolynomial b bₘ.end)) := by rw [polyCharpoly, φ.polyCharpolyAux_basisIndep b (Module.Free.chooseBasis R M) bₘ, polyCharpolyAux] lemma polyCharpoly_monic : (polyCharpoly φ b).Monic := (charpoly.univ_monic R _).map _ lemma polyCharpoly_ne_zero [Nontrivial R] : (polyCharpoly φ b) ≠ 0 := (polyCharpoly_monic _ _).ne_zero @[simp] lemma polyCharpoly_natDegree [Nontrivial R] : (polyCharpoly φ b).natDegree = finrank R M := by rw [polyCharpoly, polyCharpolyAux, (charpoly.univ_monic _ _).natDegree_map, charpoly.univ_natDegree, finrank_eq_card_chooseBasisIndex] lemma polyCharpoly_coeff_isHomogeneous (i j : ℕ) (hij : i + j = finrank R M) [Nontrivial R] : ((polyCharpoly φ b).coeff i).IsHomogeneous j := by rw [finrank_eq_card_chooseBasisIndex] at hij rw [polyCharpoly, polyCharpolyAux, Polynomial.coeff_map, ← one_mul j] apply (charpoly.univ_coeff_isHomogeneous _ _ _ _ hij).eval₂ · exact fun r ↦ MvPolynomial.isHomogeneous_C _ _ · exact LinearMap.toMvPolynomial_isHomogeneous _ _ _ open Algebra.TensorProduct MvPolynomial in lemma polyCharpoly_baseChange (A : Type*) [CommRing A] [Algebra R A] : polyCharpoly (tensorProduct _ _ _ _ ∘ₗ φ.baseChange A) (basis A b) = (polyCharpoly φ b).map (MvPolynomial.map (algebraMap R A)) := by unfold polyCharpoly rw [← φ.polyCharpolyAux_baseChange] apply polyCharpolyAux_basisIndep @[simp] lemma polyCharpoly_map_eq_charpoly (x : L) : (polyCharpoly φ b).map (MvPolynomial.eval (b.repr x)) = (φ x).charpoly := by rw [polyCharpoly, polyCharpolyAux_map_eq_charpoly] @[simp] lemma polyCharpoly_coeff_eval (x : L) (i : ℕ) : MvPolynomial.eval (b.repr x) ((polyCharpoly φ b).coeff i) = (φ x).charpoly.coeff i := by rw [polyCharpoly, polyCharpolyAux_coeff_eval] lemma polyCharpoly_coeff_eq_zero_of_basis (b : Basis ι R L) (b' : Basis ι' R L) (k : ℕ) (H : (polyCharpoly φ b).coeff k = 0) : (polyCharpoly φ b').coeff k = 0 := by rw [polyCharpoly, polyCharpolyAux, Polynomial.coeff_map] at H ⊢ set B := (Module.Free.chooseBasis R M).end set g := toMvPolynomial b' b LinearMap.id apply_fun (MvPolynomial.bind₁ g) at H have : toMvPolynomial b' B φ = fun i ↦ (MvPolynomial.bind₁ g) (toMvPolynomial b B φ i) := funext <| toMvPolynomial_comp b' b B φ LinearMap.id rwa [map_zero, RingHom.coe_coe, MvPolynomial.bind₁_bind₁, ← this] at H lemma polyCharpoly_coeff_eq_zero_iff_of_basis (b : Basis ι R L) (b' : Basis ι' R L) (k : ℕ) : (polyCharpoly φ b).coeff k = 0 ↔ (polyCharpoly φ b').coeff k = 0 := by constructor <;> apply polyCharpoly_coeff_eq_zero_of_basis section aux /-- (Implementation detail, see `LinearMap.nilRank`.) Let `L` and `M` be finite free modules over `R`, and let `φ : L →ₗ[R] Module.End R M` be a linear family of endomorphisms. Then `LinearMap.nilRankAux φ b` is the smallest index at which `LinearMap.polyCharpoly φ b` has a non-zero coefficient. This number does not depend on the choice of `b`, see `nilRankAux_basis_indep`. -/ noncomputable def nilRankAux (φ : L →ₗ[R] Module.End R M) (b : Basis ι R L) : ℕ := (polyCharpoly φ b).natTrailingDegree lemma polyCharpoly_coeff_nilRankAux_ne_zero [Nontrivial R] : (polyCharpoly φ b).coeff (nilRankAux φ b) ≠ 0 := by apply Polynomial.trailingCoeff_nonzero_iff_nonzero.mpr apply polyCharpoly_ne_zero lemma nilRankAux_le [Nontrivial R] (b : Basis ι R L) (b' : Basis ι' R L) : nilRankAux φ b ≤ nilRankAux φ b' := by apply Polynomial.natTrailingDegree_le_of_ne_zero rw [Ne, (polyCharpoly_coeff_eq_zero_iff_of_basis φ b b' _).not] apply polyCharpoly_coeff_nilRankAux_ne_zero lemma nilRankAux_basis_indep [Nontrivial R] (b : Basis ι R L) (b' : Basis ι' R L) : nilRankAux φ b = (polyCharpoly φ b').natTrailingDegree := by apply le_antisymm <;> apply nilRankAux_le end aux variable [Module.Finite R L] [Module.Free R L] /-- Let `L` and `M` be finite free modules over `R`, and let `φ : L →ₗ[R] Module.End R M` be a linear family of endomorphisms. Then `LinearMap.nilRank φ b` is the smallest index at which `LinearMap.polyCharpoly φ b` has a non-zero coefficient. This number does not depend on the choice of `b`, see `LinearMap.nilRank_eq_polyCharpoly_natTrailingDegree`. -/ noncomputable def nilRank (φ : L →ₗ[R] Module.End R M) : ℕ := nilRankAux φ (Module.Free.chooseBasis R L) section variable [Nontrivial R] lemma nilRank_eq_polyCharpoly_natTrailingDegree (b : Basis ι R L) : nilRank φ = (polyCharpoly φ b).natTrailingDegree := by apply nilRankAux_basis_indep lemma polyCharpoly_coeff_nilRank_ne_zero : (polyCharpoly φ b).coeff (nilRank φ) ≠ 0 := by rw [nilRank_eq_polyCharpoly_natTrailingDegree _ b] apply polyCharpoly_coeff_nilRankAux_ne_zero open Module Module.Free lemma nilRank_le_card {ι : Type*} [Fintype ι] (b : Basis ι R M) : nilRank φ ≤ Fintype.card ι := by apply Polynomial.natTrailingDegree_le_of_ne_zero
rw [← Module.finrank_eq_card_basis b, ← polyCharpoly_natDegree φ (chooseBasis R L), Polynomial.coeff_natDegree, (polyCharpoly_monic _ _).leadingCoeff] apply one_ne_zero lemma nilRank_le_finrank : nilRank φ ≤ finrank R M := by
Mathlib/Algebra/Module/LinearMap/Polynomial.lean
482
486
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import Mathlib.Data.Finset.Attach import Mathlib.Data.Finset.Disjoint import Mathlib.Data.Finset.Erase import Mathlib.Data.Finset.Filter import Mathlib.Data.Finset.Range import Mathlib.Data.Finset.SDiff import Mathlib.Data.Multiset.Basic import Mathlib.Logic.Equiv.Set import Mathlib.Order.Directed import Mathlib.Order.Interval.Set.Defs import Mathlib.Data.Set.SymmDiff /-! # Basic lemmas on finite sets This file contains lemmas on the interaction of various definitions on the `Finset` type. For an explanation of `Finset` design decisions, please see `Mathlib/Data/Finset/Defs.lean`. ## Main declarations ### Main definitions * `Finset.choose`: Given a proof `h` of existence and uniqueness of a certain element satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate. ### Equivalences between finsets * The `Mathlib/Logic/Equiv/Defs.lean` file describes a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`. TODO: examples ## Tags finite sets, finset -/ -- Assert that we define `Finset` without the material on `List.sublists`. -- Note that we cannot use `List.sublists` itself as that is defined very early. assert_not_exists List.sublistsLen Multiset.powerset CompleteLattice Monoid open Multiset Subtype Function universe u variable {α : Type*} {β : Type*} {γ : Type*} namespace Finset -- TODO: these should be global attributes, but this will require fixing other files attribute [local trans] Subset.trans Superset.trans set_option linter.deprecated false in @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {s : Finset α} (hx : x ∈ s) : SizeOf.sizeOf x < SizeOf.sizeOf s := by cases s dsimp [SizeOf.sizeOf, SizeOf.sizeOf, Multiset.sizeOf] rw [Nat.add_comm] refine lt_trans ?_ (Nat.lt_succ_self _) exact Multiset.sizeOf_lt_sizeOf_of_mem hx /-! ### Lattice structure -/ section Lattice variable [DecidableEq α] {s s₁ s₂ t t₁ t₂ u v : Finset α} {a b : α} /-! #### union -/ @[simp] theorem disjUnion_eq_union (s t h) : @disjUnion α s t h = s ∪ t := ext fun a => by simp @[simp] theorem disjoint_union_left : Disjoint (s ∪ t) u ↔ Disjoint s u ∧ Disjoint t u := by simp only [disjoint_left, mem_union, or_imp, forall_and] @[simp] theorem disjoint_union_right : Disjoint s (t ∪ u) ↔ Disjoint s t ∧ Disjoint s u := by simp only [disjoint_right, mem_union, or_imp, forall_and] /-! #### inter -/ theorem not_disjoint_iff_nonempty_inter : ¬Disjoint s t ↔ (s ∩ t).Nonempty := not_disjoint_iff.trans <| by simp [Finset.Nonempty] alias ⟨_, Nonempty.not_disjoint⟩ := not_disjoint_iff_nonempty_inter theorem disjoint_or_nonempty_inter (s t : Finset α) : Disjoint s t ∨ (s ∩ t).Nonempty := by rw [← not_disjoint_iff_nonempty_inter] exact em _ omit [DecidableEq α] in theorem disjoint_of_subset_iff_left_eq_empty (h : s ⊆ t) : Disjoint s t ↔ s = ∅ := disjoint_of_le_iff_left_eq_bot h lemma pairwiseDisjoint_iff {ι : Type*} {s : Set ι} {f : ι → Finset α} : s.PairwiseDisjoint f ↔ ∀ ⦃i⦄, i ∈ s → ∀ ⦃j⦄, j ∈ s → (f i ∩ f j).Nonempty → i = j := by simp [Set.PairwiseDisjoint, Set.Pairwise, Function.onFun, not_imp_comm (a := _ = _), not_disjoint_iff_nonempty_inter] end Lattice instance isDirected_le : IsDirected (Finset α) (· ≤ ·) := by classical infer_instance instance isDirected_subset : IsDirected (Finset α) (· ⊆ ·) := isDirected_le /-! ### erase -/ section Erase variable [DecidableEq α] {s t u v : Finset α} {a b : α} @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl protected lemma Nontrivial.erase_nonempty (hs : s.Nontrivial) : (s.erase a).Nonempty := (hs.exists_ne a).imp <| by aesop @[simp] lemma erase_nonempty (ha : a ∈ s) : (s.erase a).Nonempty ↔ s.Nontrivial := by simp only [Finset.Nonempty, mem_erase, and_comm (b := _ ∈ _)] refine ⟨?_, fun hs ↦ hs.exists_ne a⟩ rintro ⟨b, hb, hba⟩ exact ⟨_, hb, _, ha, hba⟩ @[simp] theorem erase_singleton (a : α) : ({a} : Finset α).erase a = ∅ := by ext x simp @[simp] theorem erase_insert_eq_erase (s : Finset α) (a : α) : (insert a s).erase a = s.erase a := ext fun x => by simp +contextual only [mem_erase, mem_insert, and_congr_right_iff, false_or, iff_self, imp_true_iff] theorem erase_insert {a : α} {s : Finset α} (h : a ∉ s) : erase (insert a s) a = s := by rw [erase_insert_eq_erase, erase_eq_of_not_mem h] theorem erase_insert_of_ne {a b : α} {s : Finset α} (h : a ≠ b) : erase (insert a s) b = insert a (erase s b) := ext fun x => by have : x ≠ b ∧ x = a ↔ x = a := and_iff_right_of_imp fun hx => hx.symm ▸ h simp only [mem_erase, mem_insert, and_or_left, this] theorem erase_cons_of_ne {a b : α} {s : Finset α} (ha : a ∉ s) (hb : a ≠ b) : erase (cons a s ha) b = cons a (erase s b) fun h => ha <| erase_subset _ _ h := by simp only [cons_eq_insert, erase_insert_of_ne hb] @[simp] theorem insert_erase (h : a ∈ s) : insert a (erase s a) = s := ext fun x => by simp only [mem_insert, mem_erase, or_and_left, dec_em, true_and] apply or_iff_right_of_imp rintro rfl exact h lemma erase_eq_iff_eq_insert (hs : a ∈ s) (ht : a ∉ t) : erase s a = t ↔ s = insert a t := by aesop lemma insert_erase_invOn : Set.InvOn (insert a) (fun s ↦ erase s a) {s : Finset α | a ∈ s} {s : Finset α | a ∉ s} := ⟨fun _s ↦ insert_erase, fun _s ↦ erase_insert⟩ theorem erase_ssubset {a : α} {s : Finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) := ssubset_insert <| not_mem_erase _ _ _ = _ := insert_erase h theorem ssubset_iff_exists_subset_erase {s t : Finset α} : s ⊂ t ↔ ∃ a ∈ t, s ⊆ t.erase a := by refine ⟨fun h => ?_, fun ⟨a, ha, h⟩ => ssubset_of_subset_of_ssubset h <| erase_ssubset ha⟩ obtain ⟨a, ht, hs⟩ := not_subset.1 h.2 exact ⟨a, ht, subset_erase.2 ⟨h.1, hs⟩⟩ theorem erase_ssubset_insert (s : Finset α) (a : α) : s.erase a ⊂ insert a s := ssubset_iff_exists_subset_erase.2 ⟨a, mem_insert_self _ _, erase_subset_erase _ <| subset_insert _ _⟩ theorem erase_cons {s : Finset α} {a : α} (h : a ∉ s) : (s.cons a h).erase a = s := by rw [cons_eq_insert, erase_insert_eq_erase, erase_eq_of_not_mem h] theorem subset_insert_iff {a : α} {s t : Finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp] exact forall_congr' fun x => forall_swap theorem erase_insert_subset (a : α) (s : Finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 <| Subset.rfl theorem insert_erase_subset (a : α) (s : Finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 <| Subset.rfl theorem subset_insert_iff_of_not_mem (h : a ∉ s) : s ⊆ insert a t ↔ s ⊆ t := by rw [subset_insert_iff, erase_eq_of_not_mem h] theorem erase_subset_iff_of_mem (h : a ∈ t) : s.erase a ⊆ t ↔ s ⊆ t := by rw [← subset_insert_iff, insert_eq_of_mem h] theorem erase_injOn' (a : α) : { s : Finset α | a ∈ s }.InjOn fun s => erase s a := fun s hs t ht (h : s.erase a = _) => by rw [← insert_erase hs, ← insert_erase ht, h] end Erase lemma Nontrivial.exists_cons_eq {s : Finset α} (hs : s.Nontrivial) : ∃ t a ha b hb hab, (cons b t hb).cons a (mem_cons.not.2 <| not_or_intro hab ha) = s := by classical obtain ⟨a, ha, b, hb, hab⟩ := hs have : b ∈ s.erase a := mem_erase.2 ⟨hab.symm, hb⟩ refine ⟨(s.erase a).erase b, a, ?_, b, ?_, ?_, ?_⟩ <;> simp [insert_erase this, insert_erase ha, *] /-! ### sdiff -/ section Sdiff variable [DecidableEq α] {s t u v : Finset α} {a b : α} lemma erase_sdiff_erase (hab : a ≠ b) (hb : b ∈ s) : s.erase a \ s.erase b = {b} := by ext; aesop -- TODO: Do we want to delete this lemma and `Finset.disjUnion_singleton`, -- or instead add `Finset.union_singleton`/`Finset.singleton_union`? theorem sdiff_singleton_eq_erase (a : α) (s : Finset α) : s \ {a} = erase s a := by ext rw [mem_erase, mem_sdiff, mem_singleton, and_comm] -- This lemma matches `Finset.insert_eq` in functionality. theorem erase_eq (s : Finset α) (a : α) : s.erase a = s \ {a} := (sdiff_singleton_eq_erase _ _).symm theorem disjoint_erase_comm : Disjoint (s.erase a) t ↔ Disjoint s (t.erase a) := by simp_rw [erase_eq, disjoint_sdiff_comm] lemma disjoint_insert_erase (ha : a ∉ t) : Disjoint (s.erase a) (insert a t) ↔ Disjoint s t := by rw [disjoint_erase_comm, erase_insert ha] lemma disjoint_erase_insert (ha : a ∉ s) : Disjoint (insert a s) (t.erase a) ↔ Disjoint s t := by rw [← disjoint_erase_comm, erase_insert ha] theorem disjoint_of_erase_left (ha : a ∉ t) (hst : Disjoint (s.erase a) t) : Disjoint s t := by rw [← erase_insert ha, ← disjoint_erase_comm, disjoint_insert_right] exact ⟨not_mem_erase _ _, hst⟩ theorem disjoint_of_erase_right (ha : a ∉ s) (hst : Disjoint s (t.erase a)) : Disjoint s t := by rw [← erase_insert ha, disjoint_erase_comm, disjoint_insert_left] exact ⟨not_mem_erase _ _, hst⟩ theorem inter_erase (a : α) (s t : Finset α) : s ∩ t.erase a = (s ∩ t).erase a := by simp only [erase_eq, inter_sdiff_assoc] @[simp] theorem erase_inter (a : α) (s t : Finset α) : s.erase a ∩ t = (s ∩ t).erase a := by simpa only [inter_comm t] using inter_erase a t s theorem erase_sdiff_comm (s t : Finset α) (a : α) : s.erase a \ t = (s \ t).erase a := by simp_rw [erase_eq, sdiff_right_comm] theorem erase_inter_comm (s t : Finset α) (a : α) : s.erase a ∩ t = s ∩ t.erase a := by rw [erase_inter, inter_erase] theorem erase_union_distrib (s t : Finset α) (a : α) : (s ∪ t).erase a = s.erase a ∪ t.erase a := by simp_rw [erase_eq, union_sdiff_distrib] theorem insert_inter_distrib (s t : Finset α) (a : α) : insert a (s ∩ t) = insert a s ∩ insert a t := by simp_rw [insert_eq, union_inter_distrib_left] theorem erase_sdiff_distrib (s t : Finset α) (a : α) : (s \ t).erase a = s.erase a \ t.erase a := by simp_rw [erase_eq, sdiff_sdiff, sup_sdiff_eq_sup le_rfl, sup_comm] theorem erase_union_of_mem (ha : a ∈ t) (s : Finset α) : s.erase a ∪ t = s ∪ t := by rw [← insert_erase (mem_union_right s ha), erase_union_distrib, ← union_insert, insert_erase ha] theorem union_erase_of_mem (ha : a ∈ s) (t : Finset α) : s ∪ t.erase a = s ∪ t := by rw [← insert_erase (mem_union_left t ha), erase_union_distrib, ← insert_union, insert_erase ha] theorem sdiff_union_erase_cancel (hts : t ⊆ s) (ha : a ∈ t) : s \ t ∪ t.erase a = s.erase a := by simp_rw [erase_eq, sdiff_union_sdiff_cancel hts (singleton_subset_iff.2 ha)] theorem sdiff_insert (s t : Finset α) (x : α) : s \ insert x t = (s \ t).erase x := by simp_rw [← sdiff_singleton_eq_erase, insert_eq, sdiff_sdiff_left', sdiff_union_distrib, inter_comm] theorem sdiff_insert_insert_of_mem_of_not_mem {s t : Finset α} {x : α} (hxs : x ∈ s) (hxt : x ∉ t) : insert x (s \ insert x t) = s \ t := by rw [sdiff_insert, insert_erase (mem_sdiff.mpr ⟨hxs, hxt⟩)] theorem sdiff_erase (h : a ∈ s) : s \ t.erase a = insert a (s \ t) := by rw [← sdiff_singleton_eq_erase, sdiff_sdiff_eq_sdiff_union (singleton_subset_iff.2 h), insert_eq, union_comm] theorem sdiff_erase_self (ha : a ∈ s) : s \ s.erase a = {a} := by rw [sdiff_erase ha, Finset.sdiff_self, insert_empty_eq] theorem erase_eq_empty_iff (s : Finset α) (a : α) : s.erase a = ∅ ↔ s = ∅ ∨ s = {a} := by rw [← sdiff_singleton_eq_erase, sdiff_eq_empty_iff_subset, subset_singleton_iff] --TODO@Yaël: Kill lemmas duplicate with `BooleanAlgebra` theorem sdiff_disjoint : Disjoint (t \ s) s := disjoint_left.2 fun _a ha => (mem_sdiff.1 ha).2 theorem disjoint_sdiff : Disjoint s (t \ s) := sdiff_disjoint.symm theorem disjoint_sdiff_inter (s t : Finset α) : Disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right inter_subset_right sdiff_disjoint end Sdiff /-! ### attach -/ @[simp] theorem attach_empty : attach (∅ : Finset α) = ∅ := rfl @[simp] theorem attach_nonempty_iff {s : Finset α} : s.attach.Nonempty ↔ s.Nonempty := by simp [Finset.Nonempty] @[aesop safe apply (rule_sets := [finsetNonempty])] protected alias ⟨_, Nonempty.attach⟩ := attach_nonempty_iff @[simp] theorem attach_eq_empty_iff {s : Finset α} : s.attach = ∅ ↔ s = ∅ := by simp [eq_empty_iff_forall_not_mem] /-! ### filter -/ section Filter variable (p q : α → Prop) [DecidablePred p] [DecidablePred q] {s t : Finset α} theorem filter_singleton (a : α) : filter p {a} = if p a then {a} else ∅ := by classical ext x simp only [mem_singleton, forall_eq, mem_filter] split_ifs with h <;> by_cases h' : x = a <;> simp [h, h'] theorem filter_cons_of_pos (a : α) (s : Finset α) (ha : a ∉ s) (hp : p a) : filter p (cons a s ha) = cons a (filter p s) ((mem_of_mem_filter _).mt ha) := eq_of_veq <| Multiset.filter_cons_of_pos s.val hp theorem filter_cons_of_neg (a : α) (s : Finset α) (ha : a ∉ s) (hp : ¬p a) : filter p (cons a s ha) = filter p s := eq_of_veq <| Multiset.filter_cons_of_neg s.val hp theorem disjoint_filter {s : Finset α} {p q : α → Prop} [DecidablePred p] [DecidablePred q] : Disjoint (s.filter p) (s.filter q) ↔ ∀ x ∈ s, p x → ¬q x := by constructor <;> simp +contextual [disjoint_left] theorem disjoint_filter_filter' (s t : Finset α) {p q : α → Prop} [DecidablePred p] [DecidablePred q] (h : Disjoint p q) : Disjoint (s.filter p) (t.filter q) := by simp_rw [disjoint_left, mem_filter] rintro a ⟨_, hp⟩ ⟨_, hq⟩ rw [Pi.disjoint_iff] at h simpa [hp, hq] using h a theorem disjoint_filter_filter_neg (s t : Finset α) (p : α → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] : Disjoint (s.filter p) (t.filter fun a => ¬p a) := disjoint_filter_filter' s t disjoint_compl_right theorem filter_disj_union (s : Finset α) (t : Finset α) (h : Disjoint s t) : filter p (disjUnion s t h) = (filter p s).disjUnion (filter p t) (disjoint_filter_filter h) := eq_of_veq <| Multiset.filter_add _ _ _ theorem filter_cons {a : α} (s : Finset α) (ha : a ∉ s) : filter p (cons a s ha) = if p a then cons a (filter p s) ((mem_of_mem_filter _).mt ha) else filter p s := by split_ifs with h · rw [filter_cons_of_pos _ _ _ ha h] · rw [filter_cons_of_neg _ _ _ ha h] section variable [DecidableEq α] theorem filter_union (s₁ s₂ : Finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext fun _ => by simp only [mem_filter, mem_union, or_and_right] theorem filter_union_right (s : Finset α) : s.filter p ∪ s.filter q = s.filter fun x => p x ∨ q x := ext fun x => by simp [mem_filter, mem_union, ← and_or_left] theorem filter_mem_eq_inter {s t : Finset α} [∀ i, Decidable (i ∈ t)] : (s.filter fun i => i ∈ t) = s ∩ t := ext fun i => by simp [mem_filter, mem_inter] theorem filter_inter_distrib (s t : Finset α) : (s ∩ t).filter p = s.filter p ∩ t.filter p := by ext simp [mem_filter, mem_inter, and_assoc] theorem filter_inter (s t : Finset α) : filter p s ∩ t = filter p (s ∩ t) := by ext simp only [mem_inter, mem_filter, and_right_comm] theorem inter_filter (s t : Finset α) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : Finset α) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by ext x split_ifs with h <;> by_cases h' : x = a <;> simp [h, h'] theorem filter_erase (a : α) (s : Finset α) : filter p (erase s a) = erase (filter p s) a := by ext x simp only [and_assoc, mem_filter, iff_self, mem_erase] theorem filter_or (s : Finset α) : (s.filter fun a => p a ∨ q a) = s.filter p ∪ s.filter q := ext fun _ => by simp [mem_filter, mem_union, and_or_left] theorem filter_and (s : Finset α) : (s.filter fun a => p a ∧ q a) = s.filter p ∩ s.filter q := ext fun _ => by simp [mem_filter, mem_inter, and_comm, and_left_comm, and_self_iff, and_assoc] theorem filter_not (s : Finset α) : (s.filter fun a => ¬p a) = s \ s.filter p := ext fun a => by simp only [Bool.decide_coe, Bool.not_eq_true', mem_filter, and_comm, mem_sdiff, not_and_or, Bool.not_eq_true, and_or_left, and_not_self, or_false] lemma filter_and_not (s : Finset α) (p q : α → Prop) [DecidablePred p] [DecidablePred q] : s.filter (fun a ↦ p a ∧ ¬ q a) = s.filter p \ s.filter q := by rw [filter_and, filter_not, ← inter_sdiff_assoc, inter_eq_left.2 (filter_subset _ _)] theorem sdiff_eq_filter (s₁ s₂ : Finset α) : s₁ \ s₂ = filter (· ∉ s₂) s₁ := ext fun _ => by simp [mem_sdiff, mem_filter] theorem subset_union_elim {s : Finset α} {t₁ t₂ : Set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃ s₁ s₂ : Finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := by classical refine ⟨s.filter (· ∈ t₁), s.filter (· ∉ t₁), ?_, ?_, ?_⟩ · simp [filter_union_right, em] · intro x simp · intro x simp only [not_not, coe_filter, Set.mem_setOf_eq, Set.mem_diff, and_imp] intro hx hx₂ exact ⟨Or.resolve_left (h hx) hx₂, hx₂⟩ -- This is not a good simp lemma, as it would prevent `Finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter (Eq b)`. /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ theorem filter_eq [DecidableEq β] (s : Finset β) (b : β) : s.filter (Eq b) = ite (b ∈ s) {b} ∅ := by split_ifs with h · ext simp only [mem_filter, mem_singleton, decide_eq_true_eq] refine ⟨fun h => h.2.symm, ?_⟩ rintro rfl exact ⟨h, rfl⟩ · ext simp only [mem_filter, not_and, iff_false, not_mem_empty, decide_eq_true_eq] rintro m rfl exact h m /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ theorem filter_eq' [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => a = b) = ite (b ∈ s) {b} ∅ := _root_.trans (filter_congr fun _ _ => by simp_rw [@eq_comm _ b]) (filter_eq s b) theorem filter_ne [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => b ≠ a) = s.erase b := by ext simp only [mem_filter, mem_erase, Ne, decide_not, Bool.not_eq_true', decide_eq_false_iff_not] tauto theorem filter_ne' [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => a ≠ b) = s.erase b := _root_.trans (filter_congr fun _ _ => by simp_rw [@ne_comm _ b]) (filter_ne s b) theorem filter_union_filter_of_codisjoint (s : Finset α) (h : Codisjoint p q) : s.filter p ∪ s.filter q = s := (filter_or _ _ _).symm.trans <| filter_true_of_mem fun x _ => h.top_le x trivial theorem filter_union_filter_neg_eq [∀ x, Decidable (¬p x)] (s : Finset α) : (s.filter p ∪ s.filter fun a => ¬p a) = s := filter_union_filter_of_codisjoint _ _ _ <| @codisjoint_hnot_right _ _ p end end Filter /-! ### range -/ section Range open Nat variable {n m l : ℕ} @[simp] theorem range_filter_eq {n m : ℕ} : (range n).filter (· = m) = if m < n then {m} else ∅ := by convert filter_eq (range n) m using 2 · ext rw [eq_comm] · simp end Range end Finset /-! ### dedup on list and multiset -/ namespace Multiset variable [DecidableEq α] {s t : Multiset α} @[simp] theorem toFinset_add (s t : Multiset α) : toFinset (s + t) = toFinset s ∪ toFinset t := Finset.ext <| by simp @[simp] theorem toFinset_inter (s t : Multiset α) : toFinset (s ∩ t) = toFinset s ∩ toFinset t := Finset.ext <| by simp @[simp] theorem toFinset_union (s t : Multiset α) : (s ∪ t).toFinset = s.toFinset ∪ t.toFinset := by ext; simp @[simp] theorem toFinset_eq_empty {m : Multiset α} : m.toFinset = ∅ ↔ m = 0 := Finset.val_inj.symm.trans Multiset.dedup_eq_zero @[simp] theorem toFinset_nonempty : s.toFinset.Nonempty ↔ s ≠ 0 := by simp only [toFinset_eq_empty, Ne, Finset.nonempty_iff_ne_empty] @[aesop safe apply (rule_sets := [finsetNonempty])] protected alias ⟨_, Aesop.toFinset_nonempty_of_ne⟩ := toFinset_nonempty @[simp] theorem toFinset_filter (s : Multiset α) (p : α → Prop) [DecidablePred p] : Multiset.toFinset (s.filter p) = s.toFinset.filter p := by ext; simp end Multiset namespace List variable [DecidableEq α] {l l' : List α} {a : α} {f : α → β} {s : Finset α} {t : Set β} {t' : Finset β} @[simp] theorem toFinset_union (l l' : List α) : (l ∪ l').toFinset = l.toFinset ∪ l'.toFinset := by ext simp @[simp] theorem toFinset_inter (l l' : List α) : (l ∩ l').toFinset = l.toFinset ∩ l'.toFinset := by ext simp @[aesop safe apply (rule_sets := [finsetNonempty])] alias ⟨_, Aesop.toFinset_nonempty_of_ne⟩ := toFinset_nonempty_iff @[simp] theorem toFinset_filter (s : List α) (p : α → Bool) : (s.filter p).toFinset = s.toFinset.filter (p ·) := by ext; simp [List.mem_filter] end List namespace Finset section ToList @[simp] theorem toList_eq_nil {s : Finset α} : s.toList = [] ↔ s = ∅ := Multiset.toList_eq_nil.trans val_eq_zero theorem empty_toList {s : Finset α} : s.toList.isEmpty ↔ s = ∅ := by simp @[simp] theorem toList_empty : (∅ : Finset α).toList = [] := toList_eq_nil.mpr rfl theorem Nonempty.toList_ne_nil {s : Finset α} (hs : s.Nonempty) : s.toList ≠ [] := mt toList_eq_nil.mp hs.ne_empty theorem Nonempty.not_empty_toList {s : Finset α} (hs : s.Nonempty) : ¬s.toList.isEmpty := mt empty_toList.mp hs.ne_empty end ToList /-! ### choose -/ section Choose variable (p : α → Prop) [DecidablePred p] (l : Finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def chooseX (hp : ∃! a, a ∈ l ∧ p a) : { a // a ∈ l ∧ p a } := Multiset.chooseX p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := chooseX p l hp theorem choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose end Finset namespace Equiv variable [DecidableEq α] {s t : Finset α} open Finset /-- The disjoint union of finsets is a sum -/ def Finset.union (s t : Finset α) (h : Disjoint s t) : s ⊕ t ≃ (s ∪ t : Finset α) := Equiv.setCongr (coe_union _ _) |>.trans (Equiv.Set.union (disjoint_coe.mpr h)) |>.symm @[simp] theorem Finset.union_symm_inl (h : Disjoint s t) (x : s) : Equiv.Finset.union s t h (Sum.inl x) = ⟨x, Finset.mem_union.mpr <| Or.inl x.2⟩ := rfl @[simp] theorem Finset.union_symm_inr (h : Disjoint s t) (y : t) : Equiv.Finset.union s t h (Sum.inr y) = ⟨y, Finset.mem_union.mpr <| Or.inr y.2⟩ := rfl /-- The type of dependent functions on the disjoint union of finsets `s ∪ t` is equivalent to the type of pairs of functions on `s` and on `t`. This is similar to `Equiv.sumPiEquivProdPi`. -/ def piFinsetUnion {ι} [DecidableEq ι] (α : ι → Type*) {s t : Finset ι} (h : Disjoint s t) : ((∀ i : s, α i) × ∀ i : t, α i) ≃ ∀ i : (s ∪ t : Finset ι), α i := let e := Equiv.Finset.union s t h sumPiEquivProdPi (fun b ↦ α (e b)) |>.symm.trans (.piCongrLeft (fun i : ↥(s ∪ t) ↦ α i) e) /-- A finset is equivalent to its coercion as a set. -/ def _root_.Finset.equivToSet (s : Finset α) : s ≃ s.toSet where toFun a := ⟨a.1, mem_coe.2 a.2⟩ invFun a := ⟨a.1, mem_coe.1 a.2⟩ left_inv := fun _ ↦ rfl right_inv := fun _ ↦ rfl end Equiv namespace Multiset variable [DecidableEq α] @[simp] lemma toFinset_replicate (n : ℕ) (a : α) : (replicate n a).toFinset = if n = 0 then ∅ else {a} := by ext x simp only [mem_toFinset, Finset.mem_singleton, mem_replicate] split_ifs with hn <;> simp [hn] end Multiset
Mathlib/Data/Finset/Basic.lean
2,762
2,763
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.Algebra.Lie.OfAssociative import Mathlib.Algebra.Lie.IdealOperations /-! # Trivial Lie modules and Abelian Lie algebras The action of a Lie algebra `L` on a module `M` is trivial if `⁅x, m⁆ = 0` for all `x ∈ L` and `m ∈ M`. In the special case that `M = L` with the adjoint action, triviality corresponds to the concept of an Abelian Lie algebra. In this file we define these concepts and provide some related definitions and results. ## Main definitions * `LieModule.IsTrivial` * `IsLieAbelian` * `commutative_ring_iff_abelian_lie_ring` * `LieModule.ker` * `LieModule.maxTrivSubmodule` * `LieAlgebra.center` ## Tags lie algebra, abelian, commutative, center -/ universe u v w w₁ w₂ /-- A Lie (ring) module is trivial iff all brackets vanish. -/ class LieModule.IsTrivial (L : Type v) (M : Type w) [Bracket L M] [Zero M] : Prop where trivial : ∀ (x : L) (m : M), ⁅x, m⁆ = 0 @[simp] theorem trivial_lie_zero (L : Type v) (M : Type w) [Bracket L M] [Zero M] [LieModule.IsTrivial L M] (x : L) (m : M) : ⁅x, m⁆ = 0 := LieModule.IsTrivial.trivial x m instance LieModule.instIsTrivialOfSubsingleton {L M : Type*} [LieRing L] [AddCommGroup M] [LieRingModule L M] [Subsingleton L] : LieModule.IsTrivial L M := ⟨fun x m ↦ by rw [Subsingleton.eq_zero x, zero_lie]⟩ instance LieModule.instIsTrivialOfSubsingleton' {L M : Type*} [LieRing L] [AddCommGroup M] [LieRingModule L M] [Subsingleton M] : LieModule.IsTrivial L M := ⟨fun x m ↦ by simp_rw [Subsingleton.eq_zero m, lie_zero]⟩ /-- A Lie algebra is Abelian iff it is trivial as a Lie module over itself. -/ abbrev IsLieAbelian (L : Type v) [Bracket L L] [Zero L] : Prop := LieModule.IsTrivial L L instance LieIdeal.isLieAbelian_of_trivial (R : Type u) (L : Type v) [CommRing R] [LieRing L] [LieAlgebra R L] (I : LieIdeal R L) [h : LieModule.IsTrivial L I] : IsLieAbelian I where trivial x y := by apply h.trivial theorem Function.Injective.isLieAbelian {R : Type u} {L₁ : Type v} {L₂ : Type w} [CommRing R] [LieRing L₁] [LieRing L₂] [LieAlgebra R L₁] [LieAlgebra R L₂] {f : L₁ →ₗ⁅R⁆ L₂} (h₁ : Function.Injective f) (_ : IsLieAbelian L₂) : IsLieAbelian L₁ := { trivial := fun x y => h₁ <| calc f ⁅x, y⁆ = ⁅f x, f y⁆ := LieHom.map_lie f x y _ = 0 := trivial_lie_zero _ _ _ _ _ = f 0 := f.map_zero.symm} theorem Function.Surjective.isLieAbelian {R : Type u} {L₁ : Type v} {L₂ : Type w} [CommRing R] [LieRing L₁] [LieRing L₂] [LieAlgebra R L₁] [LieAlgebra R L₂] {f : L₁ →ₗ⁅R⁆ L₂} (h₁ : Function.Surjective f) (h₂ : IsLieAbelian L₁) : IsLieAbelian L₂ := { trivial := fun x y => by obtain ⟨u, rfl⟩ := h₁ x obtain ⟨v, rfl⟩ := h₁ y rw [← LieHom.map_lie, trivial_lie_zero, LieHom.map_zero] } theorem lie_abelian_iff_equiv_lie_abelian {R : Type u} {L₁ : Type v} {L₂ : Type w} [CommRing R] [LieRing L₁] [LieRing L₂] [LieAlgebra R L₁] [LieAlgebra R L₂] (e : L₁ ≃ₗ⁅R⁆ L₂) : IsLieAbelian L₁ ↔ IsLieAbelian L₂ := ⟨e.symm.injective.isLieAbelian, e.injective.isLieAbelian⟩ theorem commutative_ring_iff_abelian_lie_ring {A : Type v} [Ring A] : Std.Commutative (α := A) (· * ·) ↔ IsLieAbelian A := by have h₁ : Std.Commutative (α := A) (· * ·) ↔ ∀ a b : A, a * b = b * a := ⟨fun h => h.1, fun h => ⟨h⟩⟩ have h₂ : IsLieAbelian A ↔ ∀ a b : A, ⁅a, b⁆ = 0 := ⟨fun h => h.1, fun h => ⟨h⟩⟩ simp only [h₁, h₂, LieRing.of_associative_ring_bracket, sub_eq_zero] section Center variable (R : Type u) (L : Type v) (M : Type w) (N : Type w₁) variable [CommRing R] [LieRing L] [LieAlgebra R L] variable [AddCommGroup M] [Module R M] [LieRingModule L M] [LieModule R L M] variable [AddCommGroup N] [Module R N] [LieRingModule L N] [LieModule R L N] namespace LieModule /-- The kernel of the action of a Lie algebra `L` on a Lie module `M` as a Lie ideal in `L`. -/ protected def ker : LieIdeal R L := (toEnd R L M).ker @[simp] protected theorem mem_ker (x : L) : x ∈ LieModule.ker R L M ↔ ∀ m : M, ⁅x, m⁆ = 0 := by simp only [LieModule.ker, LieHom.mem_ker, LinearMap.ext_iff, LinearMap.zero_apply, toEnd_apply_apply] lemma isFaithful_iff_ker_eq_bot : IsFaithful R L M ↔ LieModule.ker R L M = ⊥ := by rw [isFaithful_iff', LieSubmodule.ext_iff] aesop @[simp] lemma ker_eq_bot [IsFaithful R L M] : LieModule.ker R L M = ⊥ := (isFaithful_iff_ker_eq_bot R L M).mp inferInstance /-- The largest submodule of a Lie module `M` on which the Lie algebra `L` acts trivially. -/ def maxTrivSubmodule : LieSubmodule R L M where carrier := { m | ∀ x : L, ⁅x, m⁆ = 0 } zero_mem' x := lie_zero x add_mem' {x y} hx hy z := by rw [lie_add, hx, hy, add_zero] smul_mem' c x hx y := by rw [lie_smul, hx, smul_zero] lie_mem {x m} hm y := by rw [hm, lie_zero] @[simp] theorem mem_maxTrivSubmodule (m : M) : m ∈ maxTrivSubmodule R L M ↔ ∀ x : L, ⁅x, m⁆ = 0 := Iff.rfl instance : IsTrivial L (maxTrivSubmodule R L M) where trivial x m := Subtype.ext (m.property x) @[simp] theorem ideal_oper_maxTrivSubmodule_eq_bot (I : LieIdeal R L) : ⁅I, maxTrivSubmodule R L M⁆ = ⊥ := by rw [← LieSubmodule.toSubmodule_inj, LieSubmodule.lieIdeal_oper_eq_linear_span, LieSubmodule.bot_toSubmodule, Submodule.span_eq_bot] rintro m ⟨⟨x, hx⟩, ⟨⟨m, hm⟩, rfl⟩⟩ exact hm x theorem le_max_triv_iff_bracket_eq_bot {N : LieSubmodule R L M} : N ≤ maxTrivSubmodule R L M ↔ ⁅(⊤ : LieIdeal R L), N⁆ = ⊥ := by refine ⟨fun h => ?_, fun h m hm => ?_⟩ · rw [← le_bot_iff, ← ideal_oper_maxTrivSubmodule_eq_bot R L M ⊤] exact LieSubmodule.mono_lie_right ⊤ h · rw [mem_maxTrivSubmodule] rw [LieSubmodule.lie_eq_bot_iff] at h exact fun x => h x (LieSubmodule.mem_top x) m hm theorem trivial_iff_le_maximal_trivial (N : LieSubmodule R L M) : IsTrivial L N ↔ N ≤ maxTrivSubmodule R L M := ⟨fun h m hm x => IsTrivial.casesOn h fun h => Subtype.ext_iff.mp (h x ⟨m, hm⟩), fun h => { trivial := fun x m => Subtype.ext (h m.2 x) }⟩ theorem isTrivial_iff_max_triv_eq_top : IsTrivial L M ↔ maxTrivSubmodule R L M = ⊤ := by constructor · rintro ⟨h⟩; ext; simp only [mem_maxTrivSubmodule, h, forall_const, LieSubmodule.mem_top] · intro h; constructor; intro x m; revert x rw [← mem_maxTrivSubmodule R L M, h]; exact LieSubmodule.mem_top m variable {R L M N} /-- `maxTrivSubmodule` is functorial. -/ def maxTrivHom (f : M →ₗ⁅R,L⁆ N) : maxTrivSubmodule R L M →ₗ⁅R,L⁆ maxTrivSubmodule R L N where toFun m := ⟨f m, fun x => (LieModuleHom.map_lie _ _ _).symm.trans <| (congr_arg f (m.property x)).trans (LieModuleHom.map_zero _)⟩ map_add' m n := by ext; simp map_smul' t m := by ext; simp map_lie' {x m} := by simp @[norm_cast, simp] theorem coe_maxTrivHom_apply (f : M →ₗ⁅R,L⁆ N) (m : maxTrivSubmodule R L M) : (maxTrivHom f m : N) = f m := rfl /-- The maximal trivial submodules of Lie-equivalent Lie modules are Lie-equivalent. -/ def maxTrivEquiv (e : M ≃ₗ⁅R,L⁆ N) : maxTrivSubmodule R L M ≃ₗ⁅R,L⁆ maxTrivSubmodule R L N := { maxTrivHom (e : M →ₗ⁅R,L⁆ N) with toFun := maxTrivHom (e : M →ₗ⁅R,L⁆ N) invFun := maxTrivHom (e.symm : N →ₗ⁅R,L⁆ M) left_inv := fun m => by ext; simp [LieModuleEquiv.coe_toLieModuleHom] right_inv := fun n => by ext; simp [LieModuleEquiv.coe_toLieModuleHom] } @[norm_cast, simp] theorem coe_maxTrivEquiv_apply (e : M ≃ₗ⁅R,L⁆ N) (m : maxTrivSubmodule R L M) : (maxTrivEquiv e m : N) = e ↑m := rfl @[simp] theorem maxTrivEquiv_of_refl_eq_refl : maxTrivEquiv (LieModuleEquiv.refl : M ≃ₗ⁅R,L⁆ M) = LieModuleEquiv.refl := by ext; simp only [coe_maxTrivEquiv_apply, LieModuleEquiv.refl_apply] @[simp] theorem maxTrivEquiv_of_equiv_symm_eq_symm (e : M ≃ₗ⁅R,L⁆ N) : (maxTrivEquiv e).symm = maxTrivEquiv e.symm := rfl /-- A linear map between two Lie modules is a morphism of Lie modules iff the Lie algebra action on it is trivial. -/ def maxTrivLinearMapEquivLieModuleHom : maxTrivSubmodule R L (M →ₗ[R] N) ≃ₗ[R] M →ₗ⁅R,L⁆ N where toFun f := { toLinearMap := f.val map_lie' := fun {x m} => by have hf : ⁅x, f.val⁆ m = 0 := by rw [f.property x, LinearMap.zero_apply] rw [LieHom.lie_apply, sub_eq_zero, ← LinearMap.toFun_eq_coe] at hf; exact hf.symm} map_add' f g := by ext; simp map_smul' F G := by ext; simp invFun F := ⟨F, fun x => by ext; simp⟩ left_inv f := by simp right_inv F := by simp @[simp] theorem coe_maxTrivLinearMapEquivLieModuleHom (f : maxTrivSubmodule R L (M →ₗ[R] N)) : (maxTrivLinearMapEquivLieModuleHom (M := M) (N := N) f : M → N) = f := by ext; rfl @[simp] theorem coe_maxTrivLinearMapEquivLieModuleHom_symm (f : M →ₗ⁅R,L⁆ N) : (maxTrivLinearMapEquivLieModuleHom (M := M) (N := N) |>.symm f : M → N) = f := rfl @[simp] theorem toLinearMap_maxTrivLinearMapEquivLieModuleHom (f : maxTrivSubmodule R L (M →ₗ[R] N)) : (maxTrivLinearMapEquivLieModuleHom (M := M) (N := N) f : M →ₗ[R] N) = (f : M →ₗ[R] N) := by ext; rfl @[deprecated (since := "2024-12-30")] alias coe_linearMap_maxTrivLinearMapEquivLieModuleHom := toLinearMap_maxTrivLinearMapEquivLieModuleHom @[simp] theorem toLinearMap_maxTrivLinearMapEquivLieModuleHom_symm (f : M →ₗ⁅R,L⁆ N) : (maxTrivLinearMapEquivLieModuleHom (M := M) (N := N) |>.symm f : M →ₗ[R] N) = (f : M →ₗ[R] N) := rfl @[deprecated (since := "2024-12-30")] alias coe_linearMap_maxTrivLinearMapEquivLieModuleHom_symm := toLinearMap_maxTrivLinearMapEquivLieModuleHom_symm end LieModule namespace LieAlgebra /-- The center of a Lie algebra is the set of elements that commute with everything. It can be viewed as the maximal trivial submodule of the Lie algebra as a Lie module over itself via the adjoint representation. -/ abbrev center : LieIdeal R L := LieModule.maxTrivSubmodule R L L instance : IsLieAbelian (center R L) := inferInstance @[simp] theorem ad_ker_eq_self_module_ker : (ad R L).ker = LieModule.ker R L L := rfl @[simp] theorem self_module_ker_eq_center : LieModule.ker R L L = center R L := by ext y simp only [LieModule.mem_maxTrivSubmodule, LieModule.mem_ker, ← lie_skew _ y, neg_eq_zero] theorem abelian_of_le_center (I : LieIdeal R L) (h : I ≤ center R L) : IsLieAbelian I := haveI : LieModule.IsTrivial L I := (LieModule.trivial_iff_le_maximal_trivial R L L I).mpr h LieIdeal.isLieAbelian_of_trivial R L I theorem isLieAbelian_iff_center_eq_top : IsLieAbelian L ↔ center R L = ⊤ := LieModule.isTrivial_iff_max_triv_eq_top R L L theorem isFaithful_self_iff : LieModule.IsFaithful R L L ↔ center R L = ⊥ := by rw [LieModule.isFaithful_iff_ker_eq_bot, self_module_ker_eq_center]
@[simp] theorem center_eq_bot [LieModule.IsFaithful R L L] : center R L = ⊥ :=
Mathlib/Algebra/Lie/Abelian.lean
269
271
/- 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
Mathlib/MeasureTheory/Function/ConditionalExpectation/CondexpL1.lean
88
90
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import Mathlib.Algebra.Order.Ring.Abs /-! # Lemmas about units in `ℤ`, which interact with the order structure. -/ namespace Int theorem isUnit_iff_abs_eq {x : ℤ} : IsUnit x ↔ abs x = 1 := by rw [isUnit_iff_natAbs_eq, abs_eq_natAbs, ← Int.ofNat_one, natCast_inj] theorem isUnit_sq {a : ℤ} (ha : IsUnit a) : a ^ 2 = 1 := by rw [sq, isUnit_mul_self ha] @[simp] theorem units_sq (u : ℤˣ) : u ^ 2 = 1 := by rw [Units.ext_iff, Units.val_pow_eq_pow_val, Units.val_one, isUnit_sq u.isUnit] alias units_pow_two := units_sq
@[simp]
Mathlib/Data/Int/Order/Units.lean
25
26
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Kim Morrison -/ import Mathlib.CategoryTheory.Opposites /-! # Morphisms from equations between objects. When working categorically, sometimes one encounters an equation `h : X = Y` between objects. Your initial aversion to this is natural and appropriate: you're in for some trouble, and if there is another way to approach the problem that won't rely on this equality, it may be worth pursuing. You have two options: 1. Use the equality `h` as one normally would in Lean (e.g. using `rw` and `subst`). This may immediately cause difficulties, because in category theory everything is dependently typed, and equations between objects quickly lead to nasty goals with `eq.rec`. 2. Promote `h` to a morphism using `eqToHom h : X ⟶ Y`, or `eqToIso h : X ≅ Y`. This file introduces various `simp` lemmas which in favourable circumstances result in the various `eqToHom` morphisms to drop out at the appropriate moment! -/ universe v₁ v₂ v₃ u₁ u₂ u₃ -- morphism levels before object levels. See note [CategoryTheory universes]. namespace CategoryTheory open Opposite variable {C : Type u₁} [Category.{v₁} C] /-- An equality `X = Y` gives us a morphism `X ⟶ Y`. It is typically better to use this, rather than rewriting by the equality then using `𝟙 _` which usually leads to dependent type theory hell. -/ def eqToHom {X Y : C} (p : X = Y) : X ⟶ Y := by rw [p]; exact 𝟙 _ @[simp] theorem eqToHom_refl (X : C) (p : X = X) : eqToHom p = 𝟙 X := rfl @[reassoc (attr := simp)] theorem eqToHom_trans {X Y Z : C} (p : X = Y) (q : Y = Z) : eqToHom p ≫ eqToHom q = eqToHom (p.trans q) := by cases p cases q simp /-- `eqToHom h` is heterogeneously equal to the identity of its domain. -/ lemma eqToHom_heq_id_dom (X Y : C) (h : X = Y) : HEq (eqToHom h) (𝟙 X) := by subst h; rfl /-- `eqToHom h` is heterogeneously equal to the identity of its codomain. -/ lemma eqToHom_heq_id_cod (X Y : C) (h : X = Y) : HEq (eqToHom h) (𝟙 Y) := by subst h; rfl /-- Two morphisms are conjugate via eqToHom if and only if they are heterogeneously equal. Note this used to be in the Functor namespace, where it doesn't belong. -/ theorem conj_eqToHom_iff_heq {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) (h : W = Y) (h' : X = Z) : f = eqToHom h ≫ g ≫ eqToHom h'.symm ↔ HEq f g := by cases h cases h' simp theorem conj_eqToHom_iff_heq' {C} [Category C] {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) (h : W = Y) (h' : Z = X) : f = eqToHom h ≫ g ≫ eqToHom h' ↔ HEq f g := conj_eqToHom_iff_heq _ _ _ h'.symm theorem comp_eqToHom_iff {X Y Y' : C} (p : Y = Y') (f : X ⟶ Y) (g : X ⟶ Y') : f ≫ eqToHom p = g ↔ f = g ≫ eqToHom p.symm := { mp := fun h => h ▸ by simp mpr := fun h => by simp [eq_whisker h (eqToHom p)] } theorem eqToHom_comp_iff {X X' Y : C} (p : X = X') (f : X ⟶ Y) (g : X' ⟶ Y) : eqToHom p ≫ g = f ↔ g = eqToHom p.symm ≫ f := { mp := fun h => h ▸ by simp mpr := fun h => h ▸ by simp [whisker_eq _ h] } theorem eqToHom_comp_heq {C} [Category C] {W X Y : C} (f : Y ⟶ X) (h : W = Y) : HEq (eqToHom h ≫ f) f := by rw [← conj_eqToHom_iff_heq _ _ h rfl, eqToHom_refl, Category.comp_id] @[simp] theorem eqToHom_comp_heq_iff {C} [Category C] {W X Y Z Z' : C} (f : Y ⟶ X) (g : Z ⟶ Z') (h : W = Y) : HEq (eqToHom h ≫ f) g ↔ HEq f g := ⟨(eqToHom_comp_heq ..).symm.trans, (eqToHom_comp_heq ..).trans⟩ @[simp] theorem heq_eqToHom_comp_iff {C} [Category C] {W X Y Z Z' : C} (f : Y ⟶ X) (g : Z ⟶ Z') (h : W = Y) : HEq g (eqToHom h ≫ f) ↔ HEq g f := ⟨(·.trans (eqToHom_comp_heq ..)), (·.trans (eqToHom_comp_heq ..).symm)⟩ theorem comp_eqToHom_heq {C} [Category C] {X Y Z : C} (f : X ⟶ Y) (h : Y = Z) : HEq (f ≫ eqToHom h) f := by rw [← conj_eqToHom_iff_heq' _ _ rfl h, eqToHom_refl, Category.id_comp] @[simp] theorem comp_eqToHom_heq_iff {C} [Category C] {W X Y Z Z' : C} (f : X ⟶ Y) (g : Z ⟶ Z') (h : Y = W) : HEq (f ≫ eqToHom h) g ↔ HEq f g := ⟨(comp_eqToHom_heq ..).symm.trans, (comp_eqToHom_heq ..).trans⟩ @[simp] theorem heq_comp_eqToHom_iff {C} [Category C] {W X Y Z Z' : C} (f : X ⟶ Y) (g : Z ⟶ Z') (h : Y = W) : HEq g (f ≫ eqToHom h) ↔ HEq g f := ⟨(·.trans (comp_eqToHom_heq ..)), (·.trans (comp_eqToHom_heq ..).symm)⟩ theorem heq_comp {C} [Category C] {X Y Z X' Y' Z' : C} {f : X ⟶ Y} {g : Y ⟶ Z} {f' : X' ⟶ Y'} {g' : Y' ⟶ Z'} (eq1 : X = X') (eq2 : Y = Y') (eq3 : Z = Z') (H1 : HEq f f') (H2 : HEq g g') : HEq (f ≫ g) (f' ≫ g') := by cases eq1; cases eq2; cases eq3; cases H1; cases H2; rfl variable {β : Sort*} /-- We can push `eqToHom` to the left through families of morphisms. -/ -- The simpNF linter incorrectly claims that this will never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 @[reassoc (attr := simp, nolint simpNF)] theorem eqToHom_naturality {f g : β → C} (z : ∀ b, f b ⟶ g b) {j j' : β} (w : j = j') : z j ≫ eqToHom (by simp [w]) = eqToHom (by simp [w]) ≫ z j' := by cases w simp /-- A variant on `eqToHom_naturality` that helps Lean identify the families `f` and `g`. -/ -- The simpNF linter incorrectly claims that this will never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 @[reassoc (attr := simp, nolint simpNF)] theorem eqToHom_iso_hom_naturality {f g : β → C} (z : ∀ b, f b ≅ g b) {j j' : β} (w : j = j') : (z j).hom ≫ eqToHom (by simp [w]) = eqToHom (by simp [w]) ≫ (z j').hom := by cases w simp /-- A variant on `eqToHom_naturality` that helps Lean identify the families `f` and `g`. -/ -- The simpNF linter incorrectly claims that this will never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 @[reassoc (attr := simp, nolint simpNF)] theorem eqToHom_iso_inv_naturality {f g : β → C} (z : ∀ b, f b ≅ g b) {j j' : β} (w : j = j') : (z j).inv ≫ eqToHom (by simp [w]) = eqToHom (by simp [w]) ≫ (z j').inv := by cases w simp /-- Reducible form of congrArg_mpr_hom_left -/ @[simp] theorem congrArg_cast_hom_left {X Y Z : C} (p : X = Y) (q : Y ⟶ Z) : cast (congrArg (fun W : C => W ⟶ Z) p.symm) q = eqToHom p ≫ q := by cases p simp /-- If we (perhaps unintentionally) perform equational rewriting on the source object of a morphism, we can replace the resulting `_.mpr f` term by a composition with an `eqToHom`. It may be advisable to introduce any necessary `eqToHom` morphisms manually, rather than relying on this lemma firing. -/ theorem congrArg_mpr_hom_left {X Y Z : C} (p : X = Y) (q : Y ⟶ Z) : (congrArg (fun W : C => W ⟶ Z) p).mpr q = eqToHom p ≫ q := by cases p simp /-- Reducible form of `congrArg_mpr_hom_right` -/ @[simp] theorem congrArg_cast_hom_right {X Y Z : C} (p : X ⟶ Y) (q : Z = Y) : cast (congrArg (fun W : C => X ⟶ W) q.symm) p = p ≫ eqToHom q.symm := by cases q simp /-- If we (perhaps unintentionally) perform equational rewriting on the target object of a morphism, we can replace the resulting `_.mpr f` term by a composition with an `eqToHom`. It may be advisable to introduce any necessary `eqToHom` morphisms manually, rather than relying on this lemma firing. -/ theorem congrArg_mpr_hom_right {X Y Z : C} (p : X ⟶ Y) (q : Z = Y) : (congrArg (fun W : C => X ⟶ W) q).mpr p = p ≫ eqToHom q.symm := by cases q simp /-- An equality `X = Y` gives us an isomorphism `X ≅ Y`. It is typically better to use this, rather than rewriting by the equality then using `Iso.refl _` which usually leads to dependent type theory hell. -/ def eqToIso {X Y : C} (p : X = Y) : X ≅ Y := ⟨eqToHom p, eqToHom p.symm, by simp, by simp⟩ @[simp] theorem eqToIso.hom {X Y : C} (p : X = Y) : (eqToIso p).hom = eqToHom p := rfl @[simp] theorem eqToIso.inv {X Y : C} (p : X = Y) : (eqToIso p).inv = eqToHom p.symm := rfl @[simp] theorem eqToIso_refl {X : C} (p : X = X) : eqToIso p = Iso.refl X := rfl @[simp] theorem eqToIso_trans {X Y Z : C} (p : X = Y) (q : Y = Z) : eqToIso p ≪≫ eqToIso q = eqToIso (p.trans q) := by ext; simp @[simp] theorem eqToHom_op {X Y : C} (h : X = Y) : (eqToHom h).op = eqToHom (congr_arg op h.symm) := by cases h rfl @[simp] theorem eqToHom_unop {X Y : Cᵒᵖ} (h : X = Y) : (eqToHom h).unop = eqToHom (congr_arg unop h.symm) := by cases h rfl instance {X Y : C} (h : X = Y) : IsIso (eqToHom h) := (eqToIso h).isIso_hom @[simp] theorem inv_eqToHom {X Y : C} (h : X = Y) : inv (eqToHom h) = eqToHom h.symm := by aesop_cat variable {D : Type u₂} [Category.{v₂} D] namespace Functor /-- Proving equality between functors. This isn't an extensionality lemma, because usually you don't really want to do this. -/ theorem ext {F G : C ⥤ D} (h_obj : ∀ X, F.obj X = G.obj X) (h_map : ∀ X Y f, F.map f = eqToHom (h_obj X) ≫ G.map f ≫ eqToHom (h_obj Y).symm := by aesop_cat) : F = G := by match F, G with | mk F_pre _ _ , mk G_pre _ _ => match F_pre, G_pre with | Prefunctor.mk F_obj _ , Prefunctor.mk G_obj _ =>
obtain rfl : F_obj = G_obj := by ext X apply h_obj congr funext X Y f simpa using h_map X Y f
Mathlib/CategoryTheory/EqToHom.lean
245
250
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, María Inés de Frutos-Fernández, Filippo A. E. Nuccio -/ import Mathlib.Data.Int.Interval import Mathlib.FieldTheory.RatFunc.AsPolynomial import Mathlib.RingTheory.Binomial import Mathlib.RingTheory.HahnSeries.PowerSeries import Mathlib.RingTheory.HahnSeries.Summable import Mathlib.RingTheory.PowerSeries.Inverse import Mathlib.RingTheory.PowerSeries.Trunc import Mathlib.RingTheory.Localization.FractionRing import Mathlib.Topology.UniformSpace.DiscreteUniformity import Mathlib.Algebra.Group.Int.TypeTags /-! # Laurent Series In this file we define `LaurentSeries R`, the formal Laurent series over `R`, here an *arbitrary* type with a zero. They are denoted `R⸨X⸩`. ## Main Definitions * Defines `LaurentSeries` as an abbreviation for `HahnSeries ℤ`. * Defines `hasseDeriv` of a Laurent series with coefficients in a module over a ring. * Provides a coercion from power series `R⟦X⟧` into `R⸨X⸩` given by `HahnSeries.ofPowerSeries`. * Defines `LaurentSeries.powerSeriesPart` * Defines the localization map `LaurentSeries.of_powerSeries_localization` which evaluates to `HahnSeries.ofPowerSeries`. * Embedding of rational functions into Laurent series, provided as a coercion, utilizing the underlying `RatFunc.coeAlgHom`. * Study of the `X`-Adic valuation on the ring of Laurent series over a field * In `LaurentSeries.uniformContinuous_coeff` we show that sending a Laurent series to its `d`th coefficient is uniformly continuous, ensuring that it sends a Cauchy filter `ℱ` in `K⸨X⸩` to a Cauchy filter in `K`: since this latter is given the discrete topology, this provides an element `LaurentSeries.Cauchy.coeff ℱ d` in `K` that serves as `d`th coefficient of the Laurent series to which the filter `ℱ` converges. ## Main Results * Basic properties of Hasse derivatives ### About the `X`-Adic valuation: * The (integral) valuation of a power series is the order of the first non-zero coefficient, see `LaurentSeries.intValuation_le_iff_coeff_lt_eq_zero`. * The valuation of a Laurent series is the order of the first non-zero coefficient, see `LaurentSeries.valuation_le_iff_coeff_lt_eq_zero`. * Every Laurent series of valuation less than `(1 : ℤₘ₀)` comes from a power series, see `LaurentSeries.val_le_one_iff_eq_coe`. * The uniform space of `LaurentSeries` over a field is complete, formalized in the instance `instLaurentSeriesComplete`. * The field of rational functions is dense in `LaurentSeries`: this is the declaration `LaurentSeries.coe_range_dense` and relies principally upon `LaurentSeries.exists_ratFunc_val_lt`, stating that for every Laurent series `f` and every `γ : ℤₘ₀` one can find a rational function `Q` such that the `X`-adic valuation `v` satisfies `v (f - Q) < γ`. * In `LaurentSeries.valuation_compare` we prove that the extension of the `X`-adic valuation from `RatFunc K` up to its abstract completion coincides, modulo the isomorphism with `K⸨X⸩`, with the `X`-adic valuation on `K⸨X⸩`. * The two declarations `LaurentSeries.mem_integers_of_powerSeries` and `LaurentSeries.exists_powerSeries_of_memIntegers` show that an element in the completion of `RatFunc K` is in the unit ball if and only if it comes from a power series through the isomorphism `LaurentSeriesRingEquiv`. * `LaurentSeries.powerSeriesAlgEquiv` is the `K`-algebra isomorphism between `K⟦X⟧` and the unit ball inside the `X`-adic completion of `RatFunc K`. ## Implementation details * Since `LaurentSeries` is just an abbreviation of `HahnSeries ℤ R`, the definition of the coefficients is given in terms of `HahnSeries.coeff` and this forces sometimes to go back-and-forth from `X : R⸨X⸩` to `single 1 1 : HahnSeries ℤ R`. * To prove the isomorphism between the `X`-adic completion of `RatFunc K` and `K⸨X⸩` we construct two completions of `RatFunc K`: the first (`LaurentSeries.ratfuncAdicComplPkg`) is its abstract uniform completion; the second (`LaurentSeries.LaurentSeriesPkg`) is simply `K⸨X⸩`, once we prove that it is complete and contains `RatFunc K` as a dense subspace. The isomorphism is the comparison equivalence, expressing the mathematical idea that the completion "is unique". It is `LaurentSeries.comparePkg`. * For applications to `K⟦X⟧` it is actually more handy to use the *inverse* of the above equivalence: `LaurentSeries.LaurentSeriesAlgEquiv` is the *topological, algebra equivalence* `K⸨X⸩ ≃ₐ[K] RatFuncAdicCompl K`. * In order to compare `K⟦X⟧` with the valuation subring in the `X`-adic completion of `RatFunc K` we consider its alias `LaurentSeries.powerSeries_as_subring` as a subring of `K⸨X⸩`, that is itself clearly isomorphic (via the inverse of `LaurentSeries.powerSeriesEquivSubring`) to `K⟦X⟧`. -/ universe u open scoped PowerSeries open HahnSeries Polynomial noncomputable section /-- `LaurentSeries R` is the type of formal Laurent series with coefficients in `R`, denoted `R⸨X⸩`. It is implemented as a `HahnSeries` with value group `ℤ`. -/ abbrev LaurentSeries (R : Type u) [Zero R] := HahnSeries ℤ R variable {R : Type*} namespace LaurentSeries section /-- `R⸨X⸩` is notation for `LaurentSeries R`. -/ scoped notation:9000 R "⸨X⸩" => LaurentSeries R end section HasseDeriv /-- The Hasse derivative of Laurent series, as a linear map. -/ def hasseDeriv (R : Type*) {V : Type*} [AddCommGroup V] [Semiring R] [Module R V] (k : ℕ) : V⸨X⸩ →ₗ[R] V⸨X⸩ where toFun f := HahnSeries.ofSuppBddBelow (fun (n : ℤ) => (Ring.choose (n + k) k) • f.coeff (n + k)) (forallLTEqZero_supp_BddBelow _ (f.order - k : ℤ) (fun _ h_lt ↦ by rw [coeff_eq_zero_of_lt_order <| lt_sub_iff_add_lt.mp h_lt, smul_zero])) map_add' f g := by ext simp only [ofSuppBddBelow, coeff_add', Pi.add_apply, smul_add] map_smul' r f := by ext simp only [ofSuppBddBelow, HahnSeries.coeff_smul, RingHom.id_apply, smul_comm r] variable [Semiring R] {V : Type*} [AddCommGroup V] [Module R V] @[simp] theorem hasseDeriv_coeff (k : ℕ) (f : LaurentSeries V) (n : ℤ) : (hasseDeriv R k f).coeff n = Ring.choose (n + k) k • f.coeff (n + k) := rfl @[simp] theorem hasseDeriv_zero : hasseDeriv R 0 = LinearMap.id (M := LaurentSeries V) := by ext f n simp theorem hasseDeriv_single_add (k : ℕ) (n : ℤ) (x : V) : hasseDeriv R k (single (n + k) x) = single n ((Ring.choose (n + k) k) • x) := by ext m dsimp only [hasseDeriv_coeff] by_cases h : m = n · simp [h] · simp [h, show m + k ≠ n + k by omega] @[simp] theorem hasseDeriv_single (k : ℕ) (n : ℤ) (x : V) : hasseDeriv R k (single n x) = single (n - k) ((Ring.choose n k) • x) := by rw [← Int.sub_add_cancel n k, hasseDeriv_single_add, Int.sub_add_cancel n k] theorem hasseDeriv_comp_coeff (k l : ℕ) (f : LaurentSeries V) (n : ℤ) : (hasseDeriv R k (hasseDeriv R l f)).coeff n = ((Nat.choose (k + l) k) • hasseDeriv R (k + l) f).coeff n := by rw [coeff_nsmul] simp only [hasseDeriv_coeff, Pi.smul_apply, Nat.cast_add] rw [smul_smul, mul_comm, ← Ring.choose_add_smul_choose (n + k), add_assoc, Nat.choose_symm_add, smul_assoc] @[simp] theorem hasseDeriv_comp (k l : ℕ) (f : LaurentSeries V) : hasseDeriv R k (hasseDeriv R l f) = (k + l).choose k • hasseDeriv R (k + l) f := by ext n simp [hasseDeriv_comp_coeff k l f n] /-- The derivative of a Laurent series. -/ def derivative (R : Type*) {V : Type*} [AddCommGroup V] [Semiring R] [Module R V] : LaurentSeries V →ₗ[R] LaurentSeries V := hasseDeriv R 1 @[simp] theorem derivative_apply (f : LaurentSeries V) : derivative R f = hasseDeriv R 1 f := by exact rfl theorem derivative_iterate (k : ℕ) (f : LaurentSeries V) : (derivative R)^[k] f = k.factorial • (hasseDeriv R k f) := by ext n induction k generalizing f with | zero => simp | succ k ih => rw [Function.iterate_succ, Function.comp_apply, ih, derivative_apply, hasseDeriv_comp, Nat.choose_symm_add, Nat.choose_one_right, Nat.factorial, mul_nsmul] @[simp] theorem derivative_iterate_coeff (k : ℕ) (f : LaurentSeries V) (n : ℤ) : ((derivative R)^[k] f).coeff n = (descPochhammer ℤ k).smeval (n + k) • f.coeff (n + k) := by rw [derivative_iterate, coeff_nsmul, Pi.smul_apply, hasseDeriv_coeff, Ring.descPochhammer_eq_factorial_smul_choose, smul_assoc] end HasseDeriv section Semiring variable [Semiring R] instance : Coe R⟦X⟧ R⸨X⸩ := ⟨HahnSeries.ofPowerSeries ℤ R⟩ @[simp] theorem coeff_coe_powerSeries (x : R⟦X⟧) (n : ℕ) : HahnSeries.coeff (x : R⸨X⸩) n = PowerSeries.coeff R n x := by rw [ofPowerSeries_apply_coeff] /-- This is a power series that can be multiplied by an integer power of `X` to give our Laurent series. If the Laurent series is nonzero, `powerSeriesPart` has a nonzero constant term. -/ def powerSeriesPart (x : R⸨X⸩) : R⟦X⟧ := PowerSeries.mk fun n => x.coeff (x.order + n) @[simp] theorem powerSeriesPart_coeff (x : R⸨X⸩) (n : ℕ) : PowerSeries.coeff R n x.powerSeriesPart = x.coeff (x.order + n) := PowerSeries.coeff_mk _ _ @[simp] theorem powerSeriesPart_zero : powerSeriesPart (0 : R⸨X⸩) = 0 := by ext simp [(PowerSeries.coeff _ _).map_zero] -- Note: this doesn't get picked up any more @[simp] theorem powerSeriesPart_eq_zero (x : R⸨X⸩) : x.powerSeriesPart = 0 ↔ x = 0 := by constructor · contrapose! simp only [ne_eq] intro h rw [PowerSeries.ext_iff, not_forall] refine ⟨0, ?_⟩ simp [coeff_order_ne_zero h] · rintro rfl simp @[simp] theorem single_order_mul_powerSeriesPart (x : R⸨X⸩) : (single x.order 1 : R⸨X⸩) * x.powerSeriesPart = x := by ext n rw [← sub_add_cancel n x.order, coeff_single_mul_add, sub_add_cancel, one_mul] by_cases h : x.order ≤ n · rw [Int.eq_natAbs_of_nonneg (sub_nonneg_of_le h), coeff_coe_powerSeries, powerSeriesPart_coeff, ← Int.eq_natAbs_of_nonneg (sub_nonneg_of_le h), add_sub_cancel] · rw [ofPowerSeries_apply, embDomain_notin_range] · contrapose! h exact order_le_of_coeff_ne_zero h.symm · contrapose! h simp only [Set.mem_range, RelEmbedding.coe_mk, Function.Embedding.coeFn_mk] at h obtain ⟨m, hm⟩ := h rw [← sub_nonneg, ← hm] simp only [Nat.cast_nonneg] theorem ofPowerSeries_powerSeriesPart (x : R⸨X⸩) : ofPowerSeries ℤ R x.powerSeriesPart = single (-x.order) 1 * x := by refine Eq.trans ?_ (congr rfl x.single_order_mul_powerSeriesPart) rw [← mul_assoc, single_mul_single, neg_add_cancel, mul_one, ← C_apply, C_one, one_mul] theorem X_order_mul_powerSeriesPart {n : ℕ} {f : R⸨X⸩} (hn : n = f.order) : (PowerSeries.X ^ n * f.powerSeriesPart : R⟦X⟧) = f := by simp only [map_mul, map_pow, ofPowerSeries_X, single_pow, nsmul_eq_mul, mul_one, one_pow, hn, single_order_mul_powerSeriesPart] end Semiring instance [CommSemiring R] : Algebra R⟦X⟧ R⸨X⸩ := (HahnSeries.ofPowerSeries ℤ R).toAlgebra @[simp] theorem coe_algebraMap [CommSemiring R] : ⇑(algebraMap R⟦X⟧ R⸨X⸩) = HahnSeries.ofPowerSeries ℤ R := rfl /-- The localization map from power series to Laurent series. -/ @[simps (config := { rhsMd := .all, simpRhs := true })] instance of_powerSeries_localization [CommRing R] : IsLocalization (Submonoid.powers (PowerSeries.X : R⟦X⟧)) R⸨X⸩ where map_units' := by rintro ⟨_, n, rfl⟩ refine ⟨⟨single (n : ℤ) 1, single (-n : ℤ) 1, ?_, ?_⟩, ?_⟩ · simp · simp · dsimp; rw [ofPowerSeries_X_pow] surj' z := by by_cases h : 0 ≤ z.order · refine ⟨⟨PowerSeries.X ^ Int.natAbs z.order * powerSeriesPart z, 1⟩, ?_⟩ simp only [RingHom.map_one, mul_one, RingHom.map_mul, coe_algebraMap, ofPowerSeries_X_pow, Submonoid.coe_one] rw [Int.natAbs_of_nonneg h, single_order_mul_powerSeriesPart] · refine ⟨⟨powerSeriesPart z, PowerSeries.X ^ Int.natAbs z.order, ⟨_, rfl⟩⟩, ?_⟩ simp only [coe_algebraMap, ofPowerSeries_powerSeriesPart] rw [mul_comm _ z] refine congr rfl ?_ rw [ofPowerSeries_X_pow, Int.ofNat_natAbs_of_nonpos] exact le_of_not_ge h exists_of_eq {x y} := by rw [coe_algebraMap, ofPowerSeries_injective.eq_iff] rintro rfl exact ⟨1, rfl⟩ instance {K : Type*} [Field K] : IsFractionRing K⟦X⟧ K⸨X⸩ := IsLocalization.of_le (Submonoid.powers (PowerSeries.X : K⟦X⟧)) _ (powers_le_nonZeroDivisors_of_noZeroDivisors PowerSeries.X_ne_zero) fun _ hf => isUnit_of_mem_nonZeroDivisors <| map_mem_nonZeroDivisors _ HahnSeries.ofPowerSeries_injective hf end LaurentSeries namespace PowerSeries open LaurentSeries variable {R' : Type*} [Semiring R] [Ring R'] (f g : R⟦X⟧) (f' g' : R'⟦X⟧) @[norm_cast] theorem coe_zero : ((0 : R⟦X⟧) : R⸨X⸩) = 0 := (ofPowerSeries ℤ R).map_zero @[norm_cast] theorem coe_one : ((1 : R⟦X⟧) : R⸨X⸩) = 1 := (ofPowerSeries ℤ R).map_one @[norm_cast] theorem coe_add : ((f + g : R⟦X⟧) : R⸨X⸩) = f + g := (ofPowerSeries ℤ R).map_add _ _ @[norm_cast] theorem coe_sub : ((f' - g' : R'⟦X⟧) : R'⸨X⸩) = f' - g' := (ofPowerSeries ℤ R').map_sub _ _ @[norm_cast] theorem coe_neg : ((-f' : R'⟦X⟧) : R'⸨X⸩) = -f' := (ofPowerSeries ℤ R').map_neg _ @[norm_cast] theorem coe_mul : ((f * g : R⟦X⟧) : R⸨X⸩) = f * g := (ofPowerSeries ℤ R).map_mul _ _ theorem coeff_coe (i : ℤ) : ((f : R⟦X⟧) : R⸨X⸩).coeff i = if i < 0 then 0 else PowerSeries.coeff R i.natAbs f := by cases i · rw [Int.ofNat_eq_coe, coeff_coe_powerSeries, if_neg (Int.natCast_nonneg _).not_lt, Int.natAbs_natCast] · rw [ofPowerSeries_apply, embDomain_notin_image_support, if_pos (Int.negSucc_lt_zero _)] simp only [not_exists, RelEmbedding.coe_mk, Set.mem_image, not_and, Function.Embedding.coeFn_mk, Ne, toPowerSeries_symm_apply_coeff, mem_support, imp_true_iff, not_false_iff, reduceCtorEq] theorem coe_C (r : R) : ((C R r : R⟦X⟧) : R⸨X⸩) = HahnSeries.C r := ofPowerSeries_C _ theorem coe_X : ((X : R⟦X⟧) : R⸨X⸩) = single 1 1 := ofPowerSeries_X @[simp, norm_cast] theorem coe_smul {S : Type*} [Semiring S] [Module R S] (r : R) (x : S⟦X⟧) : ((r • x : S⟦X⟧) : S⸨X⸩) = r • (ofPowerSeries ℤ S x) := by ext simp [coeff_coe, coeff_smul, smul_ite] @[norm_cast] theorem coe_pow (n : ℕ) : ((f ^ n : R⟦X⟧) : R⸨X⸩) = (ofPowerSeries ℤ R f) ^ n := (ofPowerSeries ℤ R).map_pow _ _ end PowerSeries namespace RatFunc open scoped LaurentSeries variable {F : Type u} [Field F] (p q : F[X]) (f g : RatFunc F) /-- The coercion `RatFunc F → F⸨X⸩` as bundled alg hom. -/ def coeAlgHom (F : Type u) [Field F] : RatFunc F →ₐ[F[X]] F⸨X⸩ := liftAlgHom (Algebra.ofId _ _) <| nonZeroDivisors_le_comap_nonZeroDivisors_of_injective _ <| Polynomial.algebraMap_hahnSeries_injective _ /-- The coercion `RatFunc F → F⸨X⸩` as a function. This is the implementation of `coeToLaurentSeries`. -/ @[coe] def coeToLaurentSeries_fun {F : Type u} [Field F] : RatFunc F → F⸨X⸩ := coeAlgHom F instance coeToLaurentSeries : Coe (RatFunc F) F⸨X⸩ := ⟨coeToLaurentSeries_fun⟩ theorem coe_def : (f : F⸨X⸩) = coeAlgHom F f := rfl attribute [-instance] RatFunc.instCoePolynomial in -- avoids a diamond, see https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/compiling.20behaviour.20within.20one.20file theorem coe_num_denom : (f : F⸨X⸩) = f.num / f.denom := liftAlgHom_apply _ _ f
theorem coe_injective : Function.Injective ((↑) : RatFunc F → F⸨X⸩) := liftAlgHom_injective _ (Polynomial.algebraMap_hahnSeries_injective _) -- Porting note: removed the `norm_cast` tag: -- `norm_cast: badly shaped lemma, rhs can't start with coe `↑(coeAlgHom F) f`
Mathlib/RingTheory/LaurentSeries.lean
392
397
/- 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.Data.Fin.VecNotation import Mathlib.Logic.Small.Basic import Mathlib.SetTheory.ZFC.PSet /-! # A model of ZFC In this file, we model Zermelo-Fraenkel set theory (+ choice) using Lean's underlying type theory, building on the pre-sets defined in `Mathlib.SetTheory.ZFC.PSet`. The theory of classes is developed in `Mathlib.SetTheory.ZFC.Class`. ## Main definitions * `ZFSet`: ZFC set. Defined as `PSet` quotiented by `PSet.Equiv`, the extensional equivalence. * `ZFSet.choice`: Axiom of choice. Proved from Lean's axiom of choice. * `ZFSet.omega`: The von Neumann ordinal `ω` as a `Set`. * `Classical.allZFSetDefinable`: All functions are classically definable. * `ZFSet.IsFunc` : Predicate that a ZFC set is a subset of `x × y` that can be considered as a ZFC function `x → y`. That is, each member of `x` is related by the ZFC set to exactly one member of `y`. * `ZFSet.funs`: ZFC set of ZFC functions `x → y`. * `ZFSet.Hereditarily p x`: Predicate that every set in the transitive closure of `x` has property `p`. ## Notes To avoid confusion between the Lean `Set` and the ZFC `Set`, docstrings in this file refer to them respectively as "`Set`" and "ZFC set". -/ universe u /-- The ZFC universe of sets consists of the type of pre-sets, quotiented by extensional equivalence. -/ @[pp_with_univ] def ZFSet : Type (u + 1) := Quotient PSet.setoid.{u} namespace ZFSet /-- Turns a pre-set into a ZFC set. -/ def mk : PSet → ZFSet := Quotient.mk'' @[simp] theorem mk_eq (x : PSet) : @Eq ZFSet ⟦x⟧ (mk x) := rfl @[simp] theorem mk_out : ∀ x : ZFSet, mk x.out = x := Quotient.out_eq /-- A set function is "definable" if it is the image of some n-ary `PSet` function. This isn't exactly definability, but is useful as a sufficient condition for functions that have a computable image. -/ class Definable (n) (f : (Fin n → ZFSet.{u}) → ZFSet.{u}) where /-- Turns a definable function into an n-ary `PSet` function. -/ out : (Fin n → PSet.{u}) → PSet.{u} /-- A set function `f` is the image of `Definable.out f`. -/ mk_out xs : mk (out xs) = f (mk <| xs ·) := by simp attribute [simp] Definable.mk_out /-- An abbrev of `ZFSet.Definable` for unary functions. -/ abbrev Definable₁ (f : ZFSet.{u} → ZFSet.{u}) := Definable 1 (fun s ↦ f (s 0)) /-- A simpler constructor for `ZFSet.Definable₁`. -/ abbrev Definable₁.mk {f : ZFSet.{u} → ZFSet.{u}} (out : PSet.{u} → PSet.{u}) (mk_out : ∀ x, ⟦out x⟧ = f ⟦x⟧) : Definable₁ f where out xs := out (xs 0) mk_out xs := mk_out (xs 0) /-- Turns a unary definable function into a unary `PSet` function. -/ abbrev Definable₁.out (f : ZFSet.{u} → ZFSet.{u}) [Definable₁ f] : PSet.{u} → PSet.{u} := fun x ↦ Definable.out (fun s ↦ f (s 0)) ![x] lemma Definable₁.mk_out {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x : PSet} : .mk (out f x) = f (.mk x) := Definable.mk_out ![x] /-- An abbrev of `ZFSet.Definable` for binary functions. -/ abbrev Definable₂ (f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}) := Definable 2 (fun s ↦ f (s 0) (s 1)) /-- A simpler constructor for `ZFSet.Definable₂`. -/ abbrev Definable₂.mk {f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}} (out : PSet.{u} → PSet.{u} → PSet.{u}) (mk_out : ∀ x y, ⟦out x y⟧ = f ⟦x⟧ ⟦y⟧) : Definable₂ f where out xs := out (xs 0) (xs 1) mk_out xs := mk_out (xs 0) (xs 1) /-- Turns a binary definable function into a binary `PSet` function. -/ abbrev Definable₂.out (f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}) [Definable₂ f] : PSet.{u} → PSet.{u} → PSet.{u} := fun x y ↦ Definable.out (fun s ↦ f (s 0) (s 1)) ![x, y] lemma Definable₂.mk_out {f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}} [Definable₂ f] {x y : PSet} : .mk (out f x y) = f (.mk x) (.mk y) := Definable.mk_out ![x, y] instance (f) [Definable₁ f] (n g) [Definable n g] : Definable n (fun s ↦ f (g s)) where out xs := Definable₁.out f (Definable.out g xs) instance (f) [Definable₂ f] (n g₁ g₂) [Definable n g₁] [Definable n g₂] : Definable n (fun s ↦ f (g₁ s) (g₂ s)) where out xs := Definable₂.out f (Definable.out g₁ xs) (Definable.out g₂ xs) instance (n) (i) : Definable n (fun s ↦ s i) where out s := s i lemma Definable.out_equiv {n} (f : (Fin n → ZFSet.{u}) → ZFSet.{u}) [Definable n f] {xs ys : Fin n → PSet} (h : ∀ i, xs i ≈ ys i) : out f xs ≈ out f ys := by rw [← Quotient.eq_iff_equiv, mk_eq, mk_eq, mk_out, mk_out] exact congrArg _ (funext fun i ↦ Quotient.sound (h i)) lemma Definable₁.out_equiv (f : ZFSet.{u} → ZFSet.{u}) [Definable₁ f] {x y : PSet} (h : x ≈ y) : out f x ≈ out f y := Definable.out_equiv _ (by simp [h]) lemma Definable₂.out_equiv (f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}) [Definable₂ f] {x₁ y₁ x₂ y₂ : PSet} (h₁ : x₁ ≈ y₁) (h₂ : x₂ ≈ y₂) : out f x₁ x₂ ≈ out f y₁ y₂ := Definable.out_equiv _ (by simp [Fin.forall_fin_succ, h₁, h₂]) end ZFSet namespace Classical open PSet ZFSet /-- All functions are classically definable. -/ noncomputable def allZFSetDefinable {n} (F : (Fin n → ZFSet.{u}) → ZFSet.{u}) : Definable n F where out xs := (F (mk <| xs ·)).out end Classical namespace ZFSet open PSet theorem eq {x y : PSet} : mk x = mk y ↔ Equiv x y := Quotient.eq theorem sound {x y : PSet} (h : PSet.Equiv x y) : mk x = mk y := Quotient.sound h theorem exact {x y : PSet} : mk x = mk y → PSet.Equiv x y := Quotient.exact /-- The membership relation for ZFC sets is inherited from the membership relation for pre-sets. -/ protected def Mem : ZFSet → ZFSet → Prop := Quotient.lift₂ (· ∈ ·) fun _ _ _ _ hx hy => propext ((Mem.congr_left hx).trans (Mem.congr_right hy)) instance : Membership ZFSet ZFSet where mem t s := ZFSet.Mem s t @[simp] theorem mk_mem_iff {x y : PSet} : mk x ∈ mk y ↔ x ∈ y := Iff.rfl /-- Convert a ZFC set into a `Set` of ZFC sets -/ def toSet (u : ZFSet.{u}) : Set ZFSet.{u} := { x | x ∈ u } @[simp] theorem mem_toSet (a u : ZFSet.{u}) : a ∈ u.toSet ↔ a ∈ u := Iff.rfl instance small_toSet (x : ZFSet.{u}) : Small.{u} x.toSet := Quotient.inductionOn x fun a => by let f : a.Type → (mk a).toSet := fun i => ⟨mk <| a.Func i, func_mem a i⟩ suffices Function.Surjective f by exact small_of_surjective this rintro ⟨y, hb⟩ induction y using Quotient.inductionOn obtain ⟨i, h⟩ := hb exact ⟨i, Subtype.coe_injective (Quotient.sound h.symm)⟩ /-- A nonempty set is one that contains some element. -/ protected def Nonempty (u : ZFSet) : Prop := u.toSet.Nonempty theorem nonempty_def (u : ZFSet) : u.Nonempty ↔ ∃ x, x ∈ u := Iff.rfl theorem nonempty_of_mem {x u : ZFSet} (h : x ∈ u) : u.Nonempty := ⟨x, h⟩ @[simp] theorem nonempty_toSet_iff {u : ZFSet} : u.toSet.Nonempty ↔ u.Nonempty := Iff.rfl /-- `x ⊆ y` as ZFC sets means that all members of `x` are members of `y`. -/ protected def Subset (x y : ZFSet.{u}) := ∀ ⦃z⦄, z ∈ x → z ∈ y instance hasSubset : HasSubset ZFSet := ⟨ZFSet.Subset⟩ theorem subset_def {x y : ZFSet.{u}} : x ⊆ y ↔ ∀ ⦃z⦄, z ∈ x → z ∈ y := Iff.rfl instance : IsRefl ZFSet (· ⊆ ·) := ⟨fun _ _ => id⟩ instance : IsTrans ZFSet (· ⊆ ·) := ⟨fun _ _ _ hxy hyz _ ha => hyz (hxy ha)⟩ @[simp] theorem subset_iff : ∀ {x y : PSet}, mk x ⊆ mk y ↔ x ⊆ y | ⟨_, A⟩, ⟨_, _⟩ => ⟨fun h a => @h ⟦A a⟧ (Mem.mk A a), fun h z => Quotient.inductionOn z fun _ ⟨a, za⟩ => let ⟨b, ab⟩ := h a ⟨b, za.trans ab⟩⟩ @[simp] theorem toSet_subset_iff {x y : ZFSet} : x.toSet ⊆ y.toSet ↔ x ⊆ y := by simp [subset_def, Set.subset_def] @[ext] theorem ext {x y : ZFSet.{u}} : (∀ z : ZFSet.{u}, z ∈ x ↔ z ∈ y) → x = y := Quotient.inductionOn₂ x y fun _ _ h => Quotient.sound (Mem.ext fun w => h ⟦w⟧) theorem toSet_injective : Function.Injective toSet := fun _ _ h => ext <| Set.ext_iff.1 h @[simp] theorem toSet_inj {x y : ZFSet} : x.toSet = y.toSet ↔ x = y := toSet_injective.eq_iff instance : IsAntisymm ZFSet (· ⊆ ·) := ⟨fun _ _ hab hba => ext fun c => ⟨@hab c, @hba c⟩⟩ /-- The empty ZFC set -/ protected def empty : ZFSet := mk ∅ instance : EmptyCollection ZFSet := ⟨ZFSet.empty⟩ instance : Inhabited ZFSet := ⟨∅⟩ @[simp] theorem not_mem_empty (x) : x ∉ (∅ : ZFSet.{u}) := Quotient.inductionOn x PSet.not_mem_empty @[simp] theorem toSet_empty : toSet ∅ = ∅ := by simp [toSet] @[simp] theorem empty_subset (x : ZFSet.{u}) : (∅ : ZFSet) ⊆ x := Quotient.inductionOn x fun y => subset_iff.2 <| PSet.empty_subset y @[simp] theorem not_nonempty_empty : ¬ZFSet.Nonempty ∅ := by simp [ZFSet.Nonempty] @[simp] theorem nonempty_mk_iff {x : PSet} : (mk x).Nonempty ↔ x.Nonempty := by refine ⟨?_, fun ⟨a, h⟩ => ⟨mk a, h⟩⟩ rintro ⟨a, h⟩ induction a using Quotient.inductionOn exact ⟨_, h⟩ theorem eq_empty (x : ZFSet.{u}) : x = ∅ ↔ ∀ y : ZFSet.{u}, y ∉ x := by simp [ZFSet.ext_iff] theorem eq_empty_or_nonempty (u : ZFSet) : u = ∅ ∨ u.Nonempty := by rw [eq_empty, ← not_exists] apply em' /-- `Insert x y` is the set `{x} ∪ y` -/ protected def Insert : ZFSet → ZFSet → ZFSet := Quotient.map₂ PSet.insert fun _ _ uv ⟨_, _⟩ ⟨_, _⟩ ⟨αβ, βα⟩ => ⟨fun o => match o with | some a => let ⟨b, hb⟩ := αβ a ⟨some b, hb⟩ | none => ⟨none, uv⟩, fun o => match o with | some b => let ⟨a, ha⟩ := βα b ⟨some a, ha⟩ | none => ⟨none, uv⟩⟩ instance : Insert ZFSet ZFSet := ⟨ZFSet.Insert⟩ instance : Singleton ZFSet ZFSet := ⟨fun x => insert x ∅⟩ instance : LawfulSingleton ZFSet ZFSet := ⟨fun _ => rfl⟩ @[simp] theorem mem_insert_iff {x y z : ZFSet.{u}} : x ∈ insert y z ↔ x = y ∨ x ∈ z := Quotient.inductionOn₃ x y z fun _ _ _ => PSet.mem_insert_iff.trans (or_congr_left eq.symm) theorem mem_insert (x y : ZFSet) : x ∈ insert x y := mem_insert_iff.2 <| Or.inl rfl theorem mem_insert_of_mem {y z : ZFSet} (x) (h : z ∈ y) : z ∈ insert x y := mem_insert_iff.2 <| Or.inr h @[simp] theorem toSet_insert (x y : ZFSet) : (insert x y).toSet = insert x y.toSet := by ext simp @[simp] theorem mem_singleton {x y : ZFSet.{u}} : x ∈ @singleton ZFSet.{u} ZFSet.{u} _ y ↔ x = y := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_singleton.trans eq.symm @[simp] theorem toSet_singleton (x : ZFSet) : ({x} : ZFSet).toSet = {x} := by ext simp theorem insert_nonempty (u v : ZFSet) : (insert u v).Nonempty := ⟨u, mem_insert u v⟩ theorem singleton_nonempty (u : ZFSet) : ZFSet.Nonempty {u} := insert_nonempty u ∅ theorem mem_pair {x y z : ZFSet.{u}} : x ∈ ({y, z} : ZFSet) ↔ x = y ∨ x = z := by simp @[simp] theorem pair_eq_singleton (x : ZFSet) : {x, x} = ({x} : ZFSet) := by ext simp @[simp] theorem pair_eq_singleton_iff {x y z : ZFSet} : ({x, y} : ZFSet) = {z} ↔ x = z ∧ y = z := by refine ⟨fun h ↦ ?_, ?_⟩ · rw [← mem_singleton, ← mem_singleton] simp [← h] · rintro ⟨rfl, rfl⟩ exact pair_eq_singleton y @[simp] theorem singleton_eq_pair_iff {x y z : ZFSet} : ({x} : ZFSet) = {y, z} ↔ x = y ∧ x = z := by rw [eq_comm, pair_eq_singleton_iff] simp_rw [eq_comm] /-- `omega` is the first infinite von Neumann ordinal -/ def omega : ZFSet := mk PSet.omega @[simp] theorem omega_zero : ∅ ∈ omega := ⟨⟨0⟩, Equiv.rfl⟩ @[simp] theorem omega_succ {n} : n ∈ omega.{u} → insert n n ∈ omega.{u} := Quotient.inductionOn n fun x ⟨⟨n⟩, h⟩ => ⟨⟨n + 1⟩, ZFSet.exact <| show insert (mk x) (mk x) = insert (mk <| ofNat n) (mk <| ofNat n) by rw [ZFSet.sound h] rfl⟩ /-- `{x ∈ a | p x}` is the set of elements in `a` satisfying `p` -/ protected def sep (p : ZFSet → Prop) : ZFSet → ZFSet := Quotient.map (PSet.sep fun y => p (mk y)) fun ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩ => ⟨fun ⟨a, pa⟩ => let ⟨b, hb⟩ := αβ a ⟨⟨b, by simpa only [mk_func, ← ZFSet.sound hb]⟩, hb⟩, fun ⟨b, pb⟩ => let ⟨a, ha⟩ := βα b ⟨⟨a, by simpa only [mk_func, ZFSet.sound ha]⟩, ha⟩⟩ -- Porting note: the { x | p x } notation appears to be disabled in Lean 4. instance : Sep ZFSet ZFSet := ⟨ZFSet.sep⟩ @[simp] theorem mem_sep {p : ZFSet.{u} → Prop} {x y : ZFSet.{u}} : y ∈ ZFSet.sep p x ↔ y ∈ x ∧ p y := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_sep (p := p ∘ mk) fun _ _ h => (Quotient.sound h).subst @[simp] theorem sep_empty (p : ZFSet → Prop) : (∅ : ZFSet).sep p = ∅ := (eq_empty _).mpr fun _ h ↦ not_mem_empty _ (mem_sep.mp h).1 @[simp] theorem toSet_sep (a : ZFSet) (p : ZFSet → Prop) : (ZFSet.sep p a).toSet = { x ∈ a.toSet | p x } := by ext simp /-- The powerset operation, the collection of subsets of a ZFC set -/ def powerset : ZFSet → ZFSet := Quotient.map PSet.powerset fun ⟨_, A⟩ ⟨_, B⟩ ⟨αβ, βα⟩ => ⟨fun p => ⟨{ b | ∃ a, p a ∧ Equiv (A a) (B b) }, fun ⟨a, pa⟩ => let ⟨b, ab⟩ := αβ a ⟨⟨b, a, pa, ab⟩, ab⟩, fun ⟨_, a, pa, ab⟩ => ⟨⟨a, pa⟩, ab⟩⟩, fun q => ⟨{ a | ∃ b, q b ∧ Equiv (A a) (B b) }, fun ⟨_, b, qb, ab⟩ => ⟨⟨b, qb⟩, ab⟩, fun ⟨b, qb⟩ => let ⟨a, ab⟩ := βα b ⟨⟨a, b, qb, ab⟩, ab⟩⟩⟩ @[simp] theorem mem_powerset {x y : ZFSet.{u}} : y ∈ powerset x ↔ y ⊆ x := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_powerset.trans subset_iff.symm theorem sUnion_lem {α β : Type u} (A : α → PSet) (B : β → PSet) (αβ : ∀ a, ∃ b, Equiv (A a) (B b)) : ∀ a, ∃ b, Equiv ((sUnion ⟨α, A⟩).Func a) ((sUnion ⟨β, B⟩).Func b) | ⟨a, c⟩ => by let ⟨b, hb⟩ := αβ a induction' ea : A a with γ Γ induction' eb : B b with δ Δ rw [ea, eb] at hb obtain ⟨γδ, δγ⟩ := hb let c : (A a).Type := c let ⟨d, hd⟩ := γδ (by rwa [ea] at c) use ⟨b, Eq.ndrec d (Eq.symm eb)⟩ change PSet.Equiv ((A a).Func c) ((B b).Func (Eq.ndrec d eb.symm)) match A a, B b, ea, eb, c, d, hd with | _, _, rfl, rfl, _, _, hd => exact hd /-- The union operator, the collection of elements of elements of a ZFC set -/ def sUnion : ZFSet → ZFSet := Quotient.map PSet.sUnion fun ⟨_, A⟩ ⟨_, B⟩ ⟨αβ, βα⟩ => ⟨sUnion_lem A B αβ, fun a => Exists.elim (sUnion_lem B A (fun b => Exists.elim (βα b) fun c hc => ⟨c, PSet.Equiv.symm hc⟩) a) fun b hb => ⟨b, PSet.Equiv.symm hb⟩⟩ @[inherit_doc] prefix:110 "⋃₀ " => ZFSet.sUnion /-- The intersection operator, the collection of elements in all of the elements of a ZFC set. We define `⋂₀ ∅ = ∅`. -/ def sInter (x : ZFSet) : ZFSet := (⋃₀ x).sep (fun y => ∀ z ∈ x, y ∈ z) @[inherit_doc] prefix:110 "⋂₀ " => ZFSet.sInter @[simp] theorem mem_sUnion {x y : ZFSet.{u}} : y ∈ ⋃₀ x ↔ ∃ z ∈ x, y ∈ z := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_sUnion.trans ⟨fun ⟨z, h⟩ => ⟨⟦z⟧, h⟩, fun ⟨z, h⟩ => Quotient.inductionOn z (fun z h => ⟨z, h⟩) h⟩ theorem mem_sInter {x y : ZFSet} (h : x.Nonempty) : y ∈ ⋂₀ x ↔ ∀ z ∈ x, y ∈ z := by unfold sInter simp only [and_iff_right_iff_imp, mem_sep] intro mem apply mem_sUnion.mpr replace ⟨s, h⟩ := h exact ⟨_, h, mem _ h⟩ @[simp] theorem sUnion_empty : ⋃₀ (∅ : ZFSet.{u}) = ∅ := by ext simp @[simp] theorem sInter_empty : ⋂₀ (∅ : ZFSet) = ∅ := by simp [sInter] theorem mem_of_mem_sInter {x y z : ZFSet} (hy : y ∈ ⋂₀ x) (hz : z ∈ x) : y ∈ z := by rcases eq_empty_or_nonempty x with (rfl | hx) · exact (not_mem_empty z hz).elim · exact (mem_sInter hx).1 hy z hz theorem mem_sUnion_of_mem {x y z : ZFSet} (hy : y ∈ z) (hz : z ∈ x) : y ∈ ⋃₀ x := mem_sUnion.2 ⟨z, hz, hy⟩ theorem not_mem_sInter_of_not_mem {x y z : ZFSet} (hy : ¬y ∈ z) (hz : z ∈ x) : ¬y ∈ ⋂₀ x := fun hx => hy <| mem_of_mem_sInter hx hz @[simp] theorem sUnion_singleton {x : ZFSet.{u}} : ⋃₀ ({x} : ZFSet) = x := ext fun y => by simp_rw [mem_sUnion, mem_singleton, exists_eq_left] @[simp] theorem sInter_singleton {x : ZFSet.{u}} : ⋂₀ ({x} : ZFSet) = x := ext fun y => by simp_rw [mem_sInter (singleton_nonempty x), mem_singleton, forall_eq] @[simp] theorem toSet_sUnion (x : ZFSet.{u}) : (⋃₀ x).toSet = ⋃₀ (toSet '' x.toSet) := by ext simp theorem toSet_sInter {x : ZFSet.{u}} (h : x.Nonempty) : (⋂₀ x).toSet = ⋂₀ (toSet '' x.toSet) := by ext simp [mem_sInter h] theorem singleton_injective : Function.Injective (@singleton ZFSet ZFSet _) := fun x y H => by let this := congr_arg sUnion H rwa [sUnion_singleton, sUnion_singleton] at this @[simp] theorem singleton_inj {x y : ZFSet} : ({x} : ZFSet) = {y} ↔ x = y := singleton_injective.eq_iff /-- The binary union operation -/ protected def union (x y : ZFSet.{u}) : ZFSet.{u} := ⋃₀ {x, y} /-- The binary intersection operation -/ protected def inter (x y : ZFSet.{u}) : ZFSet.{u} := ZFSet.sep (fun z => z ∈ y) x -- { z ∈ x | z ∈ y } /-- The set difference operation -/ protected def diff (x y : ZFSet.{u}) : ZFSet.{u} := ZFSet.sep (fun z => z ∉ y) x -- { z ∈ x | z ∉ y } instance : Union ZFSet := ⟨ZFSet.union⟩ instance : Inter ZFSet := ⟨ZFSet.inter⟩ instance : SDiff ZFSet := ⟨ZFSet.diff⟩ @[simp] theorem toSet_union (x y : ZFSet.{u}) : (x ∪ y).toSet = x.toSet ∪ y.toSet := by change (⋃₀ {x, y}).toSet = _ simp @[simp] theorem toSet_inter (x y : ZFSet.{u}) : (x ∩ y).toSet = x.toSet ∩ y.toSet := by change (ZFSet.sep (fun z => z ∈ y) x).toSet = _ ext simp @[simp] theorem toSet_sdiff (x y : ZFSet.{u}) : (x \ y).toSet = x.toSet \ y.toSet := by change (ZFSet.sep (fun z => z ∉ y) x).toSet = _ ext simp @[simp] theorem mem_union {x y z : ZFSet.{u}} : z ∈ x ∪ y ↔ z ∈ x ∨ z ∈ y := by rw [← mem_toSet] simp @[simp] theorem mem_inter {x y z : ZFSet.{u}} : z ∈ x ∩ y ↔ z ∈ x ∧ z ∈ y := @mem_sep (fun z : ZFSet.{u} => z ∈ y) x z @[simp] theorem mem_diff {x y z : ZFSet.{u}} : z ∈ x \ y ↔ z ∈ x ∧ z ∉ y := @mem_sep (fun z : ZFSet.{u} => z ∉ y) x z @[simp] theorem sUnion_pair {x y : ZFSet.{u}} : ⋃₀ ({x, y} : ZFSet.{u}) = x ∪ y := rfl theorem mem_wf : @WellFounded ZFSet (· ∈ ·) := (wellFounded_lift₂_iff (H := fun a b c d hx hy => propext ((@Mem.congr_left a c hx).trans (@Mem.congr_right b d hy _)))).mpr PSet.mem_wf /-- Induction on the `∈` relation. -/ @[elab_as_elim] theorem inductionOn {p : ZFSet → Prop} (x) (h : ∀ x, (∀ y ∈ x, p y) → p x) : p x := mem_wf.induction x h instance : IsWellFounded ZFSet (· ∈ ·) := ⟨mem_wf⟩ instance : WellFoundedRelation ZFSet := ⟨_, mem_wf⟩ theorem mem_asymm {x y : ZFSet} : x ∈ y → y ∉ x := asymm_of (· ∈ ·) theorem mem_irrefl (x : ZFSet) : x ∉ x := irrefl_of (· ∈ ·) x theorem not_subset_of_mem {x y : ZFSet} (h : x ∈ y) : ¬ y ⊆ x := fun h' ↦ mem_irrefl _ (h' h) theorem not_mem_of_subset {x y : ZFSet} (h : x ⊆ y) : y ∉ x := imp_not_comm.2 not_subset_of_mem h theorem regularity (x : ZFSet.{u}) (h : x ≠ ∅) : ∃ y ∈ x, x ∩ y = ∅ := by_contradiction fun ne => h <| (eq_empty x).2 fun y => @inductionOn (fun z => z ∉ x) y fun z IH zx => ne ⟨z, zx, (eq_empty _).2 fun w wxz => let ⟨wx, wz⟩ := mem_inter.1 wxz IH w wz wx⟩ /-- The image of a (definable) ZFC set function -/ def image (f : ZFSet → ZFSet) [Definable₁ f] : ZFSet → ZFSet := let r := Definable₁.out f Quotient.map (PSet.image r) fun _ _ e => Mem.ext fun _ => (mem_image (fun _ _ ↦ Definable₁.out_equiv _)).trans <| Iff.trans ⟨fun ⟨w, h1, h2⟩ => ⟨w, (Mem.congr_right e).1 h1, h2⟩, fun ⟨w, h1, h2⟩ => ⟨w, (Mem.congr_right e).2 h1, h2⟩⟩ <| (mem_image (fun _ _ ↦ Definable₁.out_equiv _)).symm theorem image.mk (f : ZFSet.{u} → ZFSet.{u}) [Definable₁ f] (x) {y} : y ∈ x → f y ∈ image f x := Quotient.inductionOn₂ x y fun ⟨_, _⟩ _ ⟨a, ya⟩ => by simp only [mk_eq, ← Definable₁.mk_out (f := f)] exact ⟨a, Definable₁.out_equiv f ya⟩ @[simp] theorem mem_image {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x y : ZFSet.{u}} : y ∈ image f x ↔ ∃ z ∈ x, f z = y := Quotient.inductionOn₂ x y fun ⟨_, A⟩ _ => ⟨fun ⟨a, ya⟩ => ⟨⟦A a⟧, Mem.mk A a, ((Quotient.sound ya).trans Definable₁.mk_out).symm⟩, fun ⟨_, hz, e⟩ => e ▸ image.mk _ _ hz⟩ @[simp] theorem toSet_image (f : ZFSet → ZFSet) [Definable₁ f] (x : ZFSet) : (image f x).toSet = f '' x.toSet := by ext simp /-- The range of a type-indexed family of sets. -/ noncomputable def range {α} [Small.{u} α] (f : α → ZFSet.{u}) : ZFSet.{u} := ⟦⟨_, Quotient.out ∘ f ∘ (equivShrink α).symm⟩⟧ @[simp] theorem mem_range {α} [Small.{u} α] {f : α → ZFSet.{u}} {x : ZFSet.{u}} : x ∈ range f ↔ x ∈ Set.range f := Quotient.inductionOn x fun y => by constructor · rintro ⟨z, hz⟩ exact ⟨(equivShrink α).symm z, Quotient.eq_mk_iff_out.2 hz.symm⟩ · rintro ⟨z, hz⟩ use equivShrink α z simpa [hz] using PSet.Equiv.symm (Quotient.mk_out y) @[simp] theorem toSet_range {α} [Small.{u} α] (f : α → ZFSet.{u}) : (range f).toSet = Set.range f := by ext simp /-- Kuratowski ordered pair -/ def pair (x y : ZFSet.{u}) : ZFSet.{u} := {{x}, {x, y}} @[simp] theorem toSet_pair (x y : ZFSet.{u}) : (pair x y).toSet = {{x}, {x, y}} := by simp [pair] /-- A subset of pairs `{(a, b) ∈ x × y | p a b}` -/ def pairSep (p : ZFSet.{u} → ZFSet.{u} → Prop) (x y : ZFSet.{u}) : ZFSet.{u} := (powerset (powerset (x ∪ y))).sep fun z => ∃ a ∈ x, ∃ b ∈ y, z = pair a b ∧ p a b @[simp] theorem mem_pairSep {p} {x y z : ZFSet.{u}} : z ∈ pairSep p x y ↔ ∃ a ∈ x, ∃ b ∈ y, z = pair a b ∧ p a b := by refine mem_sep.trans ⟨And.right, fun e => ⟨?_, e⟩⟩ rcases e with ⟨a, ax, b, bY, rfl, pab⟩ simp only [mem_powerset, subset_def, mem_union, pair, mem_pair] rintro u (rfl | rfl) v <;> simp only [mem_singleton, mem_pair] · rintro rfl exact Or.inl ax · rintro (rfl | rfl) <;> [left; right] <;> assumption theorem pair_injective : Function.Injective2 pair := by intro x x' y y' H simp_rw [ZFSet.ext_iff, pair, mem_pair] at H obtain rfl : x = x' := And.left <| by simpa [or_and_left] using (H {x}).1 (Or.inl rfl) have he : y = x → y = y' := by rintro rfl simpa [eq_comm] using H {y, y'} have hx := H {x, y} simp_rw [pair_eq_singleton_iff, true_and, or_true, true_iff] at hx refine ⟨rfl, hx.elim he fun hy ↦ Or.elim ?_ he id⟩ simpa using ZFSet.ext_iff.1 hy y @[simp] theorem pair_inj {x y x' y' : ZFSet} : pair x y = pair x' y' ↔ x = x' ∧ y = y' := pair_injective.eq_iff /-- The cartesian product, `{(a, b) | a ∈ x, b ∈ y}` -/ def prod : ZFSet.{u} → ZFSet.{u} → ZFSet.{u} := pairSep fun _ _ => True @[simp] theorem mem_prod {x y z : ZFSet.{u}} : z ∈ prod x y ↔ ∃ a ∈ x, ∃ b ∈ y, z = pair a b := by simp [prod] theorem pair_mem_prod {x y a b : ZFSet.{u}} : pair a b ∈ prod x y ↔ a ∈ x ∧ b ∈ y := by simp /-- `isFunc x y f` is the assertion that `f` is a subset of `x × y` which relates to each element of `x` a unique element of `y`, so that we can consider `f` as a ZFC function `x → y`. -/ def IsFunc (x y f : ZFSet.{u}) : Prop := f ⊆ prod x y ∧ ∀ z : ZFSet.{u}, z ∈ x → ∃! w, pair z w ∈ f /-- `funs x y` is `y ^ x`, the set of all set functions `x → y` -/ def funs (x y : ZFSet.{u}) : ZFSet.{u} := ZFSet.sep (IsFunc x y) (powerset (prod x y)) @[simp] theorem mem_funs {x y f : ZFSet.{u}} : f ∈ funs x y ↔ IsFunc x y f := by simp [funs, IsFunc] instance : Definable₁ ({·}) := .mk ({·}) (fun _ ↦ rfl) instance : Definable₂ insert := .mk insert (fun _ _ ↦ rfl) instance : Definable₂ pair := by unfold pair; infer_instance /-- Graph of a function: `map f x` is the ZFC function which maps `a ∈ x` to `f a` -/ def map (f : ZFSet → ZFSet) [Definable₁ f] : ZFSet → ZFSet := image fun y => pair y (f y) @[simp] theorem mem_map {f : ZFSet → ZFSet} [Definable₁ f] {x y : ZFSet} : y ∈ map f x ↔ ∃ z ∈ x, pair z (f z) = y := mem_image theorem map_unique {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x z : ZFSet.{u}} (zx : z ∈ x) : ∃! w, pair z w ∈ map f x := ⟨f z, image.mk _ _ zx, fun y yx => by let ⟨w, _, we⟩ := mem_image.1 yx let ⟨wz, fy⟩ := pair_injective we rw [← fy, wz]⟩ @[simp] theorem map_isFunc {f : ZFSet → ZFSet} [Definable₁ f] {x y : ZFSet} : IsFunc x y (map f x) ↔ ∀ z ∈ x, f z ∈ y := ⟨fun ⟨ss, h⟩ z zx => let ⟨_, t1, t2⟩ := h z zx (t2 (f z) (image.mk _ _ zx)).symm ▸ (pair_mem_prod.1 (ss t1)).right, fun h => ⟨fun _ yx => let ⟨z, zx, ze⟩ := mem_image.1 yx ze ▸ pair_mem_prod.2 ⟨zx, h z zx⟩, fun _ => map_unique⟩⟩ /-- Given a predicate `p` on ZFC sets. `Hereditarily p x` means that `x` has property `p` and the members of `x` are all `Hereditarily p`. -/ def Hereditarily (p : ZFSet → Prop) (x : ZFSet) : Prop := p x ∧ ∀ y ∈ x, Hereditarily p y termination_by x section Hereditarily variable {p : ZFSet.{u} → Prop} {x y : ZFSet.{u}} theorem hereditarily_iff : Hereditarily p x ↔ p x ∧ ∀ y ∈ x, Hereditarily p y := by rw [← Hereditarily] alias ⟨Hereditarily.def, _⟩ := hereditarily_iff theorem Hereditarily.self (h : x.Hereditarily p) : p x := h.def.1 theorem Hereditarily.mem (h : x.Hereditarily p) (hy : y ∈ x) : y.Hereditarily p := h.def.2 _ hy theorem Hereditarily.empty : Hereditarily p x → p ∅ := by apply @ZFSet.inductionOn _ x intro y IH h rcases ZFSet.eq_empty_or_nonempty y with (rfl | ⟨a, ha⟩) · exact h.self · exact IH a ha (h.mem ha) end Hereditarily end ZFSet
Mathlib/SetTheory/ZFC/Basic.lean
812
814
/- 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)]
Mathlib/Data/List/Iterate.lean
28
31
/- Copyright (c) 2023 Mark Andrew Gerads. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mark Andrew Gerads, Junyan Xu, Eric Wieser -/ import Mathlib.Tactic.Ring /-! # Hyperoperation sequence This file defines the Hyperoperation sequence. `hyperoperation 0 m k = k + 1` `hyperoperation 1 m k = m + k` `hyperoperation 2 m k = m * k` `hyperoperation 3 m k = m ^ k` `hyperoperation (n + 3) m 0 = 1` `hyperoperation (n + 1) m (k + 1) = hyperoperation n m (hyperoperation (n + 1) m k)` ## References * <https://en.wikipedia.org/wiki/Hyperoperation> ## Tags hyperoperation -/ /-- Implementation of the hyperoperation sequence where `hyperoperation n m k` is the `n`th hyperoperation between `m` and `k`. -/ def hyperoperation : ℕ → ℕ → ℕ → ℕ | 0, _, k => k + 1 | 1, m, 0 => m | 2, _, 0 => 0 | _ + 3, _, 0 => 1 | n + 1, m, k + 1 => hyperoperation n m (hyperoperation (n + 1) m k) -- Basic hyperoperation lemmas @[simp] theorem hyperoperation_zero (m : ℕ) : hyperoperation 0 m = Nat.succ := funext fun k => by rw [hyperoperation, Nat.succ_eq_add_one] theorem hyperoperation_ge_three_eq_one (n m : ℕ) : hyperoperation (n + 3) m 0 = 1 := by rw [hyperoperation] theorem hyperoperation_recursion (n m k : ℕ) : hyperoperation (n + 1) m (k + 1) = hyperoperation n m (hyperoperation (n + 1) m k) := by rw [hyperoperation] -- Interesting hyperoperation lemmas @[simp] theorem hyperoperation_one : hyperoperation 1 = (· + ·) := by ext m k induction' k with bn bih · rw [Nat.add_zero m, hyperoperation] · rw [hyperoperation_recursion, bih, hyperoperation_zero] exact Nat.add_assoc m bn 1 @[simp] theorem hyperoperation_two : hyperoperation 2 = (· * ·) := by ext m k induction' k with bn bih · rw [hyperoperation] exact (Nat.mul_zero m).symm · rw [hyperoperation_recursion, hyperoperation_one, bih] ring @[simp] theorem hyperoperation_three : hyperoperation 3 = (· ^ ·) := by ext m k induction' k with bn bih · rw [hyperoperation_ge_three_eq_one] exact (pow_zero m).symm · rw [hyperoperation_recursion, hyperoperation_two, bih] exact (pow_succ' m bn).symm theorem hyperoperation_ge_two_eq_self (n m : ℕ) : hyperoperation (n + 2) m 1 = m := by induction' n with nn nih · rw [hyperoperation_two] ring
· rw [hyperoperation_recursion, hyperoperation_ge_three_eq_one, nih] theorem hyperoperation_two_two_eq_four (n : ℕ) : hyperoperation (n + 1) 2 2 = 4 := by induction' n with nn nih · rw [hyperoperation_one] · rw [hyperoperation_recursion, hyperoperation_ge_two_eq_self, nih]
Mathlib/Data/Nat/Hyperoperation.lean
82
88
/- Copyright (c) 2022 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import Mathlib.Algebra.Order.Group.Nat import Mathlib.Data.Countable.Basic import Mathlib.Data.Finset.Max import Mathlib.Data.Fintype.Pigeonhole import Mathlib.Logic.Encodable.Basic import Mathlib.Order.Interval.Finset.Defs import Mathlib.Order.SuccPred.Archimedean /-! # Linear locally finite orders We prove that a `LinearOrder` which is a `LocallyFiniteOrder` also verifies * `SuccOrder` * `PredOrder` * `IsSuccArchimedean` * `IsPredArchimedean` * `Countable` Furthermore, we show that there is an `OrderIso` between such an order and a subset of `ℤ`. ## Main definitions * `toZ i0 i`: in a linear order on which we can define predecessors and successors and which is succ-archimedean, we can assign a unique integer `toZ i0 i` to each element `i : ι` while respecting the order, starting from `toZ i0 i0 = 0`. ## Main results Results about linear locally finite orders: * `LinearLocallyFiniteOrder.SuccOrder`: a linear locally finite order has a successor function. * `LinearLocallyFiniteOrder.PredOrder`: a linear locally finite order has a predecessor function. * `LinearLocallyFiniteOrder.isSuccArchimedean`: a linear locally finite order is succ-archimedean. * `LinearOrder.pred_archimedean_of_succ_archimedean`: a succ-archimedean linear order is also pred-archimedean. * `countable_of_linear_succ_pred_arch` : a succ-archimedean linear order is countable. About `toZ`: * `orderIsoRangeToZOfLinearSuccPredArch`: `toZ` defines an `OrderIso` between `ι` and its range. * `orderIsoNatOfLinearSuccPredArch`: if the order has a bot but no top, `toZ` defines an `OrderIso` between `ι` and `ℕ`. * `orderIsoIntOfLinearSuccPredArch`: if the order has neither bot nor top, `toZ` defines an `OrderIso` between `ι` and `ℤ`. * `orderIsoRangeOfLinearSuccPredArch`: if the order has both a bot and a top, `toZ` gives an `OrderIso` between `ι` and `Finset.range ((toZ ⊥ ⊤).toNat + 1)`. -/ open Order variable {ι : Type*} [LinearOrder ι] namespace LinearOrder variable [SuccOrder ι] [PredOrder ι] instance (priority := 100) isPredArchimedean_of_isSuccArchimedean [IsSuccArchimedean ι] : IsPredArchimedean ι where exists_pred_iterate_of_le {i j} hij := by have h_exists := exists_succ_iterate_of_le hij obtain ⟨n, hn_eq, hn_lt_ne⟩ : ∃ n, succ^[n] i = j ∧ ∀ m < n, succ^[m] i ≠ j := ⟨Nat.find h_exists, Nat.find_spec h_exists, fun m hmn ↦ Nat.find_min h_exists hmn⟩ refine ⟨n, ?_⟩ rw [← hn_eq] cases n with | zero => simp only [Function.iterate_zero, id] | succ n => rw [pred_succ_iterate_of_not_isMax] rw [Nat.succ_sub_succ_eq_sub, tsub_zero] suffices succ^[n] i < succ^[n.succ] i from not_isMax_of_lt this refine lt_of_le_of_ne ?_ ?_ · rw [Function.iterate_succ_apply'] exact le_succ _ · rw [hn_eq] exact hn_lt_ne _ (Nat.lt_succ_self n) instance isSuccArchimedean_of_isPredArchimedean [IsPredArchimedean ι] : IsSuccArchimedean ι := inferInstanceAs (IsSuccArchimedean ιᵒᵈᵒᵈ) /-- In a linear `SuccOrder` that's also a `PredOrder`, `IsSuccArchimedean` and `IsPredArchimedean` are equivalent. -/ theorem isSuccArchimedean_iff_isPredArchimedean : IsSuccArchimedean ι ↔ IsPredArchimedean ι where mp _ := isPredArchimedean_of_isSuccArchimedean mpr _ := isSuccArchimedean_of_isPredArchimedean end LinearOrder namespace LinearLocallyFiniteOrder /-- Successor in a linear order. This defines a true successor only when `i` is isolated from above, i.e. when `i` is not the greatest lower bound of `(i, ∞)`. -/ noncomputable def succFn (i : ι) : ι := (exists_glb_Ioi i).choose theorem succFn_spec (i : ι) : IsGLB (Set.Ioi i) (succFn i) := (exists_glb_Ioi i).choose_spec theorem le_succFn (i : ι) : i ≤ succFn i := by rw [le_isGLB_iff (succFn_spec i), mem_lowerBounds] exact fun x hx ↦ le_of_lt hx theorem isGLB_Ioc_of_isGLB_Ioi {i j k : ι} (hij_lt : i < j) (h : IsGLB (Set.Ioi i) k) : IsGLB (Set.Ioc i j) k := by simp_rw [IsGLB, IsGreatest, mem_upperBounds, mem_lowerBounds] at h ⊢ refine ⟨fun x hx ↦ h.1 x hx.1, fun x hx ↦ h.2 x ?_⟩ intro y hy rcases le_or_lt y j with h_le | h_lt · exact hx y ⟨hy, h_le⟩ · exact le_trans (hx j ⟨hij_lt, le_rfl⟩) h_lt.le theorem isMax_of_succFn_le [LocallyFiniteOrder ι] (i : ι) (hi : succFn i ≤ i) : IsMax i := by refine fun j _ ↦ not_lt.mp fun hij_lt ↦ ?_ have h_succFn_eq : succFn i = i := le_antisymm hi (le_succFn i) have h_glb : IsGLB (Finset.Ioc i j : Set ι) i := by rw [Finset.coe_Ioc] have h := succFn_spec i rw [h_succFn_eq] at h exact isGLB_Ioc_of_isGLB_Ioi hij_lt h have hi_mem : i ∈ Finset.Ioc i j := by refine Finset.isGLB_mem _ h_glb ?_ exact ⟨_, Finset.mem_Ioc.mpr ⟨hij_lt, le_rfl⟩⟩ rw [Finset.mem_Ioc] at hi_mem exact lt_irrefl i hi_mem.1 theorem succFn_le_of_lt (i j : ι) (hij : i < j) : succFn i ≤ j := by have h := succFn_spec i rw [IsGLB, IsGreatest, mem_lowerBounds] at h exact h.1 j hij theorem le_of_lt_succFn (j i : ι) (hij : j < succFn i) : j ≤ i := by rw [lt_isGLB_iff (succFn_spec i)] at hij obtain ⟨k, hk_lb, hk⟩ := hij rw [mem_lowerBounds] at hk_lb exact not_lt.mp fun hi_lt_j ↦ not_le.mpr hk (hk_lb j hi_lt_j) variable (ι) in /-- A locally finite order is a `SuccOrder`. This is not an instance, because its `succ` field conflicts with computable `SuccOrder` structures on `ℕ` and `ℤ`. -/ noncomputable def succOrder [LocallyFiniteOrder ι] : SuccOrder ι where succ := succFn le_succ := le_succFn max_of_succ_le h := isMax_of_succFn_le _ h succ_le_of_lt h := succFn_le_of_lt _ _ h variable (ι) in /-- A locally finite order is a `PredOrder`. This is not an instance, because its `succ` field conflicts with computable `PredOrder` structures on `ℕ` and `ℤ`. -/ noncomputable def predOrder [LocallyFiniteOrder ι] : PredOrder ι := letI := succOrder (ι := ιᵒᵈ) inferInstanceAs (PredOrder ιᵒᵈᵒᵈ) instance (priority := 100) [LocallyFiniteOrder ι] [SuccOrder ι] : IsSuccArchimedean ι where exists_succ_iterate_of_le := by intro i j hij rw [le_iff_lt_or_eq] at hij rcases hij with hij | hij swap · refine ⟨0, ?_⟩ simpa only [Function.iterate_zero, id] using hij by_contra! h have h_lt : ∀ n, succ^[n] i < j := by intro n induction' n with n hn · simpa only [Function.iterate_zero, id] using hij · refine lt_of_le_of_ne ?_ (h _) rw [Function.iterate_succ', Function.comp_apply] exact succ_le_of_lt hn have h_mem : ∀ n, succ^[n] i ∈ Finset.Icc i j := fun n ↦ Finset.mem_Icc.mpr ⟨le_succ_iterate n i, (h_lt n).le⟩ obtain ⟨n, m, hnm, h_eq⟩ : ∃ n m, n < m ∧ succ^[n] i = succ^[m] i := by let f : ℕ → Finset.Icc i j := fun n ↦ ⟨succ^[n] i, h_mem n⟩ obtain ⟨n, m, hnm_ne, hfnm⟩ : ∃ n m, n ≠ m ∧ f n = f m := Finite.exists_ne_map_eq_of_infinite f have hnm_eq : succ^[n] i = succ^[m] i := by simpa only [f, Subtype.mk_eq_mk] using hfnm rcases le_total n m with h_le | h_le · exact ⟨n, m, lt_of_le_of_ne h_le hnm_ne, hnm_eq⟩ · exact ⟨m, n, lt_of_le_of_ne h_le hnm_ne.symm, hnm_eq.symm⟩ have h_max : IsMax (succ^[n] i) := isMax_iterate_succ_of_eq_of_ne h_eq hnm.ne exact not_le.mpr (h_lt n) (h_max (h_lt n).le) instance (priority := 100) [LocallyFiniteOrder ι] [PredOrder ι] : IsPredArchimedean ι := inferInstanceAs (IsPredArchimedean ιᵒᵈᵒᵈ) end LinearLocallyFiniteOrder section toZ -- Requiring either of `IsSuccArchimedean` or `IsPredArchimedean` is equivalent. variable [SuccOrder ι] [IsSuccArchimedean ι] [PredOrder ι] {i0 i : ι} -- For "to_Z" /-- `toZ` numbers elements of `ι` according to their order, starting from `i0`. We prove in `orderIsoRangeToZOfLinearSuccPredArch` that this defines an `OrderIso` between `ι` and the range of `toZ`. -/ def toZ (i0 i : ι) : ℤ := dite (i0 ≤ i) (fun hi ↦ Nat.find (exists_succ_iterate_of_le hi)) fun hi ↦ -Nat.find (exists_pred_iterate_of_le (α := ι) (not_le.mp hi).le) theorem toZ_of_ge (hi : i0 ≤ i) : toZ i0 i = Nat.find (exists_succ_iterate_of_le hi) := dif_pos hi theorem toZ_of_lt (hi : i < i0) : toZ i0 i = -Nat.find (exists_pred_iterate_of_le (α := ι) hi.le) := dif_neg (not_le.mpr hi) @[simp] theorem toZ_of_eq : toZ i0 i0 = 0 := by rw [toZ_of_ge le_rfl] norm_cast refine le_antisymm (Nat.find_le ?_) (zero_le _) rw [Function.iterate_zero, id] theorem iterate_succ_toZ (i : ι) (hi : i0 ≤ i) : succ^[(toZ i0 i).toNat] i0 = i := by rw [toZ_of_ge hi, Int.toNat_natCast] exact Nat.find_spec (exists_succ_iterate_of_le hi) theorem iterate_pred_toZ (i : ι) (hi : i < i0) : pred^[(-toZ i0 i).toNat] i0 = i := by rw [toZ_of_lt hi, neg_neg, Int.toNat_natCast] exact Nat.find_spec (exists_pred_iterate_of_le hi.le) lemma toZ_nonneg (hi : i0 ≤ i) : 0 ≤ toZ i0 i := by rw [toZ_of_ge hi]; exact Int.natCast_nonneg _
theorem toZ_neg (hi : i < i0) : toZ i0 i < 0 := by refine lt_of_le_of_ne ?_ ?_ · rw [toZ_of_lt hi] omega
Mathlib/Order/SuccPred/LinearLocallyFinite.lean
234
237
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.CharP.Lemmas import Mathlib.FieldTheory.Perfect /-! # The perfect closure of a characteristic `p` ring ## Main definitions - `PerfectClosure`: the perfect closure of a characteristic `p` ring, which is the smallest extension that makes frobenius surjective. - `PerfectClosure.mk K p (n, x)`: for `n : ℕ` and `x : K` this is `x ^ (p ^ -n)` viewed as an element of `PerfectClosure K p`. Every element of `PerfectClosure K p` is of this form (`PerfectClosure.mk_surjective`). - `PerfectClosure.of`: the structure map from `K` to `PerfectClosure K p`. - `PerfectClosure.lift`: given a ring `K` of characteristic `p` and a perfect ring `L` of the same characteristic, any homomorphism `K →+* L` can be lifted to `PerfectClosure K p`. ## Main results - `PerfectClosure.induction_on`: to prove a result for all elements of the prefect closure, one only needs to prove it for all elements of the form `x ^ (p ^ -n)`. - `PerfectClosure.mk_mul_mk`, `PerfectClosure.one_def`, `PerfectClosure.mk_add_mk`, `PerfectClosure.neg_mk`, `PerfectClosure.zero_def`, `PerfectClosure.mk_zero_zero`, `PerfectClosure.mk_zero`, `PerfectClosure.mk_inv`, `PerfectClosure.mk_pow`: how to do multiplication, addition, etc. on elements of form `x ^ (p ^ -n)`. - `PerfectClosure.mk_eq_iff`: when does `x ^ (p ^ -n)` equal. - `PerfectClosure.eq_iff`: same as `PerfectClosure.mk_eq_iff` but with additional assumption that `K` being reduced, hence gives a simpler criterion. - `PerfectClosure.instPerfectRing`: `PerfectClosure K p` is a perfect ring. ## Tags perfect ring, perfect closure -/ universe u v open Function section variable (K : Type u) [CommRing K] (p : ℕ) [Fact p.Prime] [CharP K p] /-- `PerfectClosure.R` is the relation `(n, x) ∼ (n + 1, x ^ p)` for `n : ℕ` and `x : K`. `PerfectClosure K p` is the quotient by this relation. -/ @[mk_iff] inductive PerfectClosure.R : ℕ × K → ℕ × K → Prop | intro : ∀ n x, PerfectClosure.R (n, x) (n + 1, frobenius K p x) /-- The perfect closure is the smallest extension that makes frobenius surjective. -/ def PerfectClosure : Type u := Quot (PerfectClosure.R K p) end namespace PerfectClosure variable (K : Type u) section Ring variable [CommRing K] (p : ℕ) [Fact p.Prime] [CharP K p] /-- `PerfectClosure.mk K p (n, x)` for `n : ℕ` and `x : K` is an element of `PerfectClosure K p`, viewed as `x ^ (p ^ -n)`. Every element of `PerfectClosure K p` is of this form (`PerfectClosure.mk_surjective`). -/ def mk (x : ℕ × K) : PerfectClosure K p := Quot.mk (R K p) x theorem mk_surjective : Function.Surjective (mk K p) := Quot.mk_surjective @[simp] theorem mk_succ_pow (m : ℕ) (x : K) : mk K p ⟨m + 1, x ^ p⟩ = mk K p ⟨m, x⟩ := Eq.symm <| Quot.sound (R.intro m x) @[simp] theorem quot_mk_eq_mk (x : ℕ × K) : (Quot.mk (R K p) x : PerfectClosure K p) = mk K p x := rfl variable {K p} /-- Lift a function `ℕ × K → L` to a function on `PerfectClosure K p`. -/ def liftOn {L : Type*} (x : PerfectClosure K p) (f : ℕ × K → L) (hf : ∀ x y, R K p x y → f x = f y) : L := Quot.liftOn x f hf @[simp] theorem liftOn_mk {L : Sort _} (f : ℕ × K → L) (hf : ∀ x y, R K p x y → f x = f y) (x : ℕ × K) : (mk K p x).liftOn f hf = f x := rfl @[elab_as_elim] theorem induction_on (x : PerfectClosure K p) {q : PerfectClosure K p → Prop} (h : ∀ x, q (mk K p x)) : q x := Quot.inductionOn x h variable (K p) private theorem mul_aux_left (x1 x2 y : ℕ × K) (H : R K p x1 x2) : mk K p (x1.1 + y.1, (frobenius K p)^[y.1] x1.2 * (frobenius K p)^[x1.1] y.2) = mk K p (x2.1 + y.1, (frobenius K p)^[y.1] x2.2 * (frobenius K p)^[x2.1] y.2) := match x1, x2, H with | _, _, R.intro n x => Quot.sound <| by rw [← iterate_succ_apply, iterate_succ_apply', iterate_succ_apply', ← frobenius_mul, Nat.succ_add] apply R.intro private theorem mul_aux_right (x y1 y2 : ℕ × K) (H : R K p y1 y2) : mk K p (x.1 + y1.1, (frobenius K p)^[y1.1] x.2 * (frobenius K p)^[x.1] y1.2) = mk K p (x.1 + y2.1, (frobenius K p)^[y2.1] x.2 * (frobenius K p)^[x.1] y2.2) := match y1, y2, H with | _, _, R.intro n y => Quot.sound <| by rw [← iterate_succ_apply, iterate_succ_apply', iterate_succ_apply', ← frobenius_mul] apply R.intro instance instMul : Mul (PerfectClosure K p) := ⟨Quot.lift (fun x : ℕ × K => Quot.lift (fun y : ℕ × K => mk K p (x.1 + y.1, (frobenius K p)^[y.1] x.2 * (frobenius K p)^[x.1] y.2)) (mul_aux_right K p x)) fun x1 x2 (H : R K p x1 x2) => funext fun e => Quot.inductionOn e fun y => mul_aux_left K p x1 x2 y H⟩ @[simp] theorem mk_mul_mk (x y : ℕ × K) : mk K p x * mk K p y = mk K p (x.1 + y.1, (frobenius K p)^[y.1] x.2 * (frobenius K p)^[x.1] y.2) := rfl instance instCommMonoid : CommMonoid (PerfectClosure K p) := { (inferInstance : Mul (PerfectClosure K p)) with mul_assoc := fun e f g => Quot.inductionOn e fun ⟨m, x⟩ => Quot.inductionOn f fun ⟨n, y⟩ => Quot.inductionOn g fun ⟨s, z⟩ => by simp only [quot_mk_eq_mk, mk_mul_mk] -- Porting note: added this line apply congr_arg (Quot.mk _) simp only [add_assoc, mul_assoc, iterate_map_mul, ← iterate_add_apply, add_comm, add_left_comm] one := mk K p (0, 1) one_mul := fun e => Quot.inductionOn e fun ⟨n, x⟩ => congr_arg (Quot.mk _) <| by simp only [iterate_map_one, iterate_zero_apply, one_mul, zero_add] mul_one := fun e => Quot.inductionOn e fun ⟨n, x⟩ => congr_arg (Quot.mk _) <| by simp only [iterate_map_one, iterate_zero_apply, mul_one, add_zero] mul_comm := fun e f => Quot.inductionOn e fun ⟨m, x⟩ => Quot.inductionOn f fun ⟨n, y⟩ => congr_arg (Quot.mk _) <| by simp only [add_comm, mul_comm] } theorem one_def : (1 : PerfectClosure K p) = mk K p (0, 1) := rfl instance instInhabited : Inhabited (PerfectClosure K p) := ⟨1⟩ private theorem add_aux_left (x1 x2 y : ℕ × K) (H : R K p x1 x2) : mk K p (x1.1 + y.1, (frobenius K p)^[y.1] x1.2 + (frobenius K p)^[x1.1] y.2) = mk K p (x2.1 + y.1, (frobenius K p)^[y.1] x2.2 + (frobenius K p)^[x2.1] y.2) := match x1, x2, H with | _, _, R.intro n x => Quot.sound <| by rw [← iterate_succ_apply, iterate_succ_apply', iterate_succ_apply', ← frobenius_add, Nat.succ_add] apply R.intro private theorem add_aux_right (x y1 y2 : ℕ × K) (H : R K p y1 y2) : mk K p (x.1 + y1.1, (frobenius K p)^[y1.1] x.2 + (frobenius K p)^[x.1] y1.2) = mk K p (x.1 + y2.1, (frobenius K p)^[y2.1] x.2 + (frobenius K p)^[x.1] y2.2) := match y1, y2, H with | _, _, R.intro n y => Quot.sound <| by rw [← iterate_succ_apply, iterate_succ_apply', iterate_succ_apply', ← frobenius_add] apply R.intro instance instAdd : Add (PerfectClosure K p) := ⟨Quot.lift (fun x : ℕ × K => Quot.lift (fun y : ℕ × K => mk K p (x.1 + y.1, (frobenius K p)^[y.1] x.2 + (frobenius K p)^[x.1] y.2)) (add_aux_right K p x)) fun x1 x2 (H : R K p x1 x2) => funext fun e => Quot.inductionOn e fun y => add_aux_left K p x1 x2 y H⟩ @[simp] theorem mk_add_mk (x y : ℕ × K) : mk K p x + mk K p y = mk K p (x.1 + y.1, (frobenius K p)^[y.1] x.2 + (frobenius K p)^[x.1] y.2) := rfl instance instNeg : Neg (PerfectClosure K p) := ⟨Quot.lift (fun x : ℕ × K => mk K p (x.1, -x.2)) fun x y (H : R K p x y) => match x, y, H with | _, _, R.intro n x => Quot.sound <| by rw [← frobenius_neg]; apply R.intro⟩ @[simp] theorem neg_mk (x : ℕ × K) : -mk K p x = mk K p (x.1, -x.2) := rfl instance instZero : Zero (PerfectClosure K p) := ⟨mk K p (0, 0)⟩ theorem zero_def : (0 : PerfectClosure K p) = mk K p (0, 0) := rfl /-- Prior to https://github.com/leanprover-community/mathlib4/pull/15862, this lemma was called `mk_zero_zero`. See `mk_zero_right` for the lemma used to be called `mk_zero`. -/ @[simp] theorem mk_zero : mk K p 0 = 0 := rfl @[simp] theorem mk_zero_right (n : ℕ) : mk K p (n, 0) = 0 := by induction' n with n ih · rfl rw [← ih] symm apply Quot.sound have := R.intro (p := p) n (0 : K) rwa [frobenius_zero K p] at this theorem R.sound (m n : ℕ) (x y : K) (H : (frobenius K p)^[m] x = y) : mk K p (n, x) = mk K p (m + n, y) := by subst H induction' m with m ih · simp only [zero_add, iterate_zero_apply] rw [ih, Nat.succ_add, iterate_succ'] apply Quot.sound apply R.intro instance instAddCommGroup : AddCommGroup (PerfectClosure K p) := { (inferInstance : Add (PerfectClosure K p)), (inferInstance : Neg (PerfectClosure K p)) with add_assoc := fun e f g => Quot.inductionOn e fun ⟨m, x⟩ => Quot.inductionOn f fun ⟨n, y⟩ => Quot.inductionOn g fun ⟨s, z⟩ => by simp only [quot_mk_eq_mk, mk_add_mk] -- Porting note: added this line apply congr_arg (Quot.mk _) simp only [iterate_map_add, ← iterate_add_apply, add_assoc, add_comm s _] zero := 0 zero_add := fun e => Quot.inductionOn e fun ⟨n, x⟩ => congr_arg (Quot.mk _) <| by simp only [iterate_map_zero, iterate_zero_apply, zero_add] add_zero := fun e => Quot.inductionOn e fun ⟨n, x⟩ => congr_arg (Quot.mk _) <| by simp only [iterate_map_zero, iterate_zero_apply, add_zero] sub_eq_add_neg := fun _ _ => rfl neg_add_cancel := fun e => Quot.inductionOn e fun ⟨n, x⟩ => by simp only [quot_mk_eq_mk, neg_mk, mk_add_mk, iterate_map_neg, neg_add_cancel, mk_zero_right] add_comm := fun e f => Quot.inductionOn e fun ⟨m, x⟩ => Quot.inductionOn f fun ⟨n, y⟩ => congr_arg (Quot.mk _) <| by simp only [add_comm] nsmul := nsmulRec zsmul := zsmulRec } instance instCommRing : CommRing (PerfectClosure K p) := { instAddCommGroup K p, AddMonoidWithOne.unary, (inferInstance : CommMonoid (PerfectClosure K p)) with -- Porting note: added `zero_mul`, `mul_zero` zero_mul := fun a => by refine Quot.inductionOn a fun ⟨m, x⟩ => ?_ rw [zero_def, quot_mk_eq_mk, mk_mul_mk] simp only [zero_add, iterate_zero, id_eq, iterate_map_zero, zero_mul, mk_zero_right] mul_zero := fun a => by refine Quot.inductionOn a fun ⟨m, x⟩ => ?_ rw [zero_def, quot_mk_eq_mk, mk_mul_mk] simp only [zero_add, iterate_zero, id_eq, iterate_map_zero, mul_zero, mk_zero_right] left_distrib := fun e f g => Quot.inductionOn e fun ⟨m, x⟩ => Quot.inductionOn f fun ⟨n, y⟩ => Quot.inductionOn g fun ⟨s, z⟩ => by simp only [quot_mk_eq_mk, mk_add_mk, mk_mul_mk] -- Porting note: added this line simp only [add_assoc, add_comm, add_left_comm] apply R.sound simp only [iterate_map_mul, iterate_map_add, ← iterate_add_apply, mul_add, add_comm, add_left_comm] right_distrib := fun e f g => Quot.inductionOn e fun ⟨m, x⟩ => Quot.inductionOn f fun ⟨n, y⟩ => Quot.inductionOn g fun ⟨s, z⟩ => by simp only [quot_mk_eq_mk, mk_add_mk, mk_mul_mk] -- Porting note: added this line simp only [add_assoc, add_comm _ s, add_left_comm _ s] apply R.sound simp only [iterate_map_mul, iterate_map_add, ← iterate_add_apply, add_mul, add_comm, add_left_comm] } theorem mk_eq_iff (x y : ℕ × K) : mk K p x = mk K p y ↔ ∃ z, (frobenius K p)^[y.1 + z] x.2 = (frobenius K p)^[x.1 + z] y.2 := by constructor · intro H replace H := Quot.eqvGen_exact H induction H with | rel x y H => obtain ⟨n, x⟩ := H; exact ⟨0, rfl⟩ | refl H => exact ⟨0, rfl⟩ | symm x y H ih => obtain ⟨w, ih⟩ := ih; exact ⟨w, ih.symm⟩ | trans x y z H1 H2 ih1 ih2 => obtain ⟨z1, ih1⟩ := ih1 obtain ⟨z2, ih2⟩ := ih2 exists z2 + (y.1 + z1) rw [← add_assoc, iterate_add_apply, ih1] rw [← iterate_add_apply, add_comm, iterate_add_apply, ih2] rw [← iterate_add_apply]
simp only [add_comm, add_left_comm] intro H obtain ⟨m, x⟩ := x obtain ⟨n, y⟩ := y obtain ⟨z, H⟩ := H; dsimp only at H rw [R.sound K p (n + z) m x _ rfl, R.sound K p (m + z) n y _ rfl, H] rw [add_assoc, add_comm, add_comm z] @[simp] theorem mk_pow (x : ℕ × K) (n : ℕ) : mk K p x ^ n = mk K p (x.1, x.2 ^ n) := by induction n with | zero => rw [pow_zero, pow_zero, one_def, mk_eq_iff] exact ⟨0, by simp_rw [← coe_iterateFrobenius, map_one]⟩ | succ n ih => rw [pow_succ, pow_succ, ih, mk_mul_mk, mk_eq_iff] exact ⟨0, by simp_rw [iterate_frobenius, add_zero, mul_pow, ← pow_mul, ← pow_add, mul_assoc, ← pow_add]⟩ theorem natCast (n x : ℕ) : (x : PerfectClosure K p) = mk K p (n, x) := by induction' n with n ih · induction' x with x ih · simp
Mathlib/FieldTheory/PerfectClosure.lean
328
350
/- 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, Yury Kudryashov -/ import Mathlib.Order.Filter.Tendsto import Mathlib.Data.Set.Accumulate import Mathlib.Topology.Bornology.Basic import Mathlib.Topology.ContinuousOn import Mathlib.Topology.Ultrafilter import Mathlib.Topology.Defs.Ultrafilter /-! # Compact sets and compact spaces ## Main results * `isCompact_univ_pi`: **Tychonov's theorem** - an arbitrary product of compact sets is compact. -/ open Set Filter Topology TopologicalSpace Function universe u v variable {X : Type u} {Y : Type v} {ι : Type*} variable [TopologicalSpace X] [TopologicalSpace Y] {s t : Set X} {f : X → Y} -- compact sets section Compact lemma IsCompact.exists_clusterPt (hs : IsCompact s) {f : Filter X} [NeBot f] (hf : f ≤ 𝓟 s) : ∃ x ∈ s, ClusterPt x f := hs hf lemma IsCompact.exists_mapClusterPt {ι : Type*} (hs : IsCompact s) {f : Filter ι} [NeBot f] {u : ι → X} (hf : Filter.map u f ≤ 𝓟 s) : ∃ x ∈ s, MapClusterPt x f u := hs hf lemma IsCompact.exists_clusterPt_of_frequently {l : Filter X} (hs : IsCompact s) (hl : ∃ᶠ x in l, x ∈ s) : ∃ a ∈ s, ClusterPt a l := let ⟨a, has, ha⟩ := @hs _ (frequently_mem_iff_neBot.mp hl) inf_le_right ⟨a, has, ha.mono inf_le_left⟩ lemma IsCompact.exists_mapClusterPt_of_frequently {l : Filter ι} {f : ι → X} (hs : IsCompact s) (hf : ∃ᶠ x in l, f x ∈ s) : ∃ a ∈ s, MapClusterPt a l f := hs.exists_clusterPt_of_frequently hf /-- The complement to a compact set belongs to a filter `f` if it belongs to each filter `𝓝 x ⊓ f`, `x ∈ s`. -/ theorem IsCompact.compl_mem_sets (hs : IsCompact s) {f : Filter X} (hf : ∀ x ∈ s, sᶜ ∈ 𝓝 x ⊓ f) : sᶜ ∈ f := by contrapose! hf simp only [not_mem_iff_inf_principal_compl, compl_compl, inf_assoc] at hf ⊢ exact @hs _ hf inf_le_right /-- The complement to a compact set belongs to a filter `f` if each `x ∈ s` has a neighborhood `t` within `s` such that `tᶜ` belongs to `f`. -/ theorem IsCompact.compl_mem_sets_of_nhdsWithin (hs : IsCompact s) {f : Filter X} (hf : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, tᶜ ∈ f) : sᶜ ∈ f := by refine hs.compl_mem_sets fun x hx => ?_ rcases hf x hx with ⟨t, ht, hst⟩ replace ht := mem_inf_principal.1 ht apply mem_inf_of_inter ht hst rintro x ⟨h₁, h₂⟩ hs exact h₂ (h₁ hs) /-- If `p : Set X → Prop` is stable under restriction and union, and each point `x` of a compact set `s` has a neighborhood `t` within `s` such that `p t`, then `p s` holds. -/ @[elab_as_elim] theorem IsCompact.induction_on (hs : IsCompact s) {p : Set X → Prop} (he : p ∅) (hmono : ∀ ⦃s t⦄, s ⊆ t → p t → p s) (hunion : ∀ ⦃s t⦄, p s → p t → p (s ∪ t)) (hnhds : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, p t) : p s := by let f : Filter X := comk p he (fun _t ht _s hsub ↦ hmono hsub ht) (fun _s hs _t ht ↦ hunion hs ht) have : sᶜ ∈ f := hs.compl_mem_sets_of_nhdsWithin (by simpa [f] using hnhds) rwa [← compl_compl s] /-- The intersection of a compact set and a closed set is a compact set. -/ theorem IsCompact.inter_right (hs : IsCompact s) (ht : IsClosed t) : IsCompact (s ∩ t) := by intro f hnf hstf obtain ⟨x, hsx, hx⟩ : ∃ x ∈ s, ClusterPt x f := hs (le_trans hstf (le_principal_iff.2 inter_subset_left)) have : x ∈ t := ht.mem_of_nhdsWithin_neBot <| hx.mono <| le_trans hstf (le_principal_iff.2 inter_subset_right) exact ⟨x, ⟨hsx, this⟩, hx⟩ /-- The intersection of a closed set and a compact set is a compact set. -/ theorem IsCompact.inter_left (ht : IsCompact t) (hs : IsClosed s) : IsCompact (s ∩ t) := inter_comm t s ▸ ht.inter_right hs /-- The set difference of a compact set and an open set is a compact set. -/ theorem IsCompact.diff (hs : IsCompact s) (ht : IsOpen t) : IsCompact (s \ t) := hs.inter_right (isClosed_compl_iff.mpr ht) /-- A closed subset of a compact set is a compact set. -/ theorem IsCompact.of_isClosed_subset (hs : IsCompact s) (ht : IsClosed t) (h : t ⊆ s) : IsCompact t := inter_eq_self_of_subset_right h ▸ hs.inter_right ht theorem IsCompact.image_of_continuousOn {f : X → Y} (hs : IsCompact s) (hf : ContinuousOn f s) : IsCompact (f '' s) := by intro l lne ls have : NeBot (l.comap f ⊓ 𝓟 s) := comap_inf_principal_neBot_of_image_mem lne (le_principal_iff.1 ls) obtain ⟨x, hxs, hx⟩ : ∃ x ∈ s, ClusterPt x (l.comap f ⊓ 𝓟 s) := @hs _ this inf_le_right haveI := hx.neBot use f x, mem_image_of_mem f hxs have : Tendsto f (𝓝 x ⊓ (comap f l ⊓ 𝓟 s)) (𝓝 (f x) ⊓ l) := by convert (hf x hxs).inf (@tendsto_comap _ _ f l) using 1 rw [nhdsWithin] ac_rfl exact this.neBot theorem IsCompact.image {f : X → Y} (hs : IsCompact s) (hf : Continuous f) : IsCompact (f '' s) := hs.image_of_continuousOn hf.continuousOn theorem IsCompact.adherence_nhdset {f : Filter X} (hs : IsCompact s) (hf₂ : f ≤ 𝓟 s) (ht₁ : IsOpen t) (ht₂ : ∀ x ∈ s, ClusterPt x f → x ∈ t) : t ∈ f := Classical.by_cases mem_of_eq_bot fun (this : f ⊓ 𝓟 tᶜ ≠ ⊥) => let ⟨x, hx, (hfx : ClusterPt x <| f ⊓ 𝓟 tᶜ)⟩ := @hs _ ⟨this⟩ <| inf_le_of_left_le hf₂ have : x ∈ t := ht₂ x hx hfx.of_inf_left have : tᶜ ∩ t ∈ 𝓝[tᶜ] x := inter_mem_nhdsWithin _ (IsOpen.mem_nhds ht₁ this) have A : 𝓝[tᶜ] x = ⊥ := empty_mem_iff_bot.1 <| compl_inter_self t ▸ this have : 𝓝[tᶜ] x ≠ ⊥ := hfx.of_inf_right.ne absurd A this theorem isCompact_iff_ultrafilter_le_nhds : IsCompact s ↔ ∀ f : Ultrafilter X, ↑f ≤ 𝓟 s → ∃ x ∈ s, ↑f ≤ 𝓝 x := by refine (forall_neBot_le_iff ?_).trans ?_ · rintro f g hle ⟨x, hxs, hxf⟩ exact ⟨x, hxs, hxf.mono hle⟩ · simp only [Ultrafilter.clusterPt_iff] alias ⟨IsCompact.ultrafilter_le_nhds, _⟩ := isCompact_iff_ultrafilter_le_nhds theorem isCompact_iff_ultrafilter_le_nhds' : IsCompact s ↔ ∀ f : Ultrafilter X, s ∈ f → ∃ x ∈ s, ↑f ≤ 𝓝 x := by simp only [isCompact_iff_ultrafilter_le_nhds, le_principal_iff, Ultrafilter.mem_coe] alias ⟨IsCompact.ultrafilter_le_nhds', _⟩ := isCompact_iff_ultrafilter_le_nhds' /-- If a compact set belongs to a filter and this filter has a unique cluster point `y` in this set, then the filter is less than or equal to `𝓝 y`. -/ lemma IsCompact.le_nhds_of_unique_clusterPt (hs : IsCompact s) {l : Filter X} {y : X} (hmem : s ∈ l) (h : ∀ x ∈ s, ClusterPt x l → x = y) : l ≤ 𝓝 y := by refine le_iff_ultrafilter.2 fun f hf ↦ ?_ rcases hs.ultrafilter_le_nhds' f (hf hmem) with ⟨x, hxs, hx⟩ convert ← hx exact h x hxs (.mono (.of_le_nhds hx) hf) /-- If values of `f : Y → X` belong to a compact set `s` eventually along a filter `l` and `y` is a unique `MapClusterPt` for `f` along `l` in `s`, then `f` tends to `𝓝 y` along `l`. -/ lemma IsCompact.tendsto_nhds_of_unique_mapClusterPt {Y} {l : Filter Y} {y : X} {f : Y → X} (hs : IsCompact s) (hmem : ∀ᶠ x in l, f x ∈ s) (h : ∀ x ∈ s, MapClusterPt x l f → x = y) : Tendsto f l (𝓝 y) := hs.le_nhds_of_unique_clusterPt (mem_map.2 hmem) h /-- For every open directed cover of a compact set, there exists a single element of the cover which itself includes the set. -/ theorem IsCompact.elim_directed_cover {ι : Type v} [hι : Nonempty ι] (hs : IsCompact s) (U : ι → Set X) (hUo : ∀ i, IsOpen (U i)) (hsU : s ⊆ ⋃ i, U i) (hdU : Directed (· ⊆ ·) U) : ∃ i, s ⊆ U i := hι.elim fun i₀ => IsCompact.induction_on hs ⟨i₀, empty_subset _⟩ (fun _ _ hs ⟨i, hi⟩ => ⟨i, hs.trans hi⟩) (fun _ _ ⟨i, hi⟩ ⟨j, hj⟩ => let ⟨k, hki, hkj⟩ := hdU i j ⟨k, union_subset (Subset.trans hi hki) (Subset.trans hj hkj)⟩) fun _x hx => let ⟨i, hi⟩ := mem_iUnion.1 (hsU hx) ⟨U i, mem_nhdsWithin_of_mem_nhds (IsOpen.mem_nhds (hUo i) hi), i, Subset.refl _⟩ /-- For every open cover of a compact set, there exists a finite subcover. -/ theorem IsCompact.elim_finite_subcover {ι : Type v} (hs : IsCompact s) (U : ι → Set X) (hUo : ∀ i, IsOpen (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : Finset ι, s ⊆ ⋃ i ∈ t, U i := hs.elim_directed_cover _ (fun _ => isOpen_biUnion fun i _ => hUo i) (iUnion_eq_iUnion_finset U ▸ hsU) (directed_of_isDirected_le fun _ _ h => biUnion_subset_biUnion_left h) lemma IsCompact.elim_nhds_subcover_nhdsSet' (hs : IsCompact s) (U : ∀ x ∈ s, Set X) (hU : ∀ x hx, U x hx ∈ 𝓝 x) : ∃ t : Finset s, (⋃ x ∈ t, U x.1 x.2) ∈ 𝓝ˢ s := by rcases hs.elim_finite_subcover (fun x : s ↦ interior (U x x.2)) (fun _ ↦ isOpen_interior) fun x hx ↦ mem_iUnion.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 <| hU _ _⟩ with ⟨t, hst⟩ refine ⟨t, mem_nhdsSet_iff_forall.2 fun x hx ↦ ?_⟩ rcases mem_iUnion₂.1 (hst hx) with ⟨y, hyt, hy⟩ refine mem_of_superset ?_ (subset_biUnion_of_mem hyt) exact mem_interior_iff_mem_nhds.1 hy lemma IsCompact.elim_nhds_subcover_nhdsSet (hs : IsCompact s) {U : X → Set X} (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ t : Finset X, (∀ x ∈ t, x ∈ s) ∧ (⋃ x ∈ t, U x) ∈ 𝓝ˢ s := by let ⟨t, ht⟩ := hs.elim_nhds_subcover_nhdsSet' (fun x _ => U x) hU classical exact ⟨t.image (↑), fun x hx => let ⟨y, _, hyx⟩ := Finset.mem_image.1 hx hyx ▸ y.2, by rwa [Finset.set_biUnion_finset_image]⟩ theorem IsCompact.elim_nhds_subcover' (hs : IsCompact s) (U : ∀ x ∈ s, Set X) (hU : ∀ x (hx : x ∈ s), U x ‹x ∈ s› ∈ 𝓝 x) : ∃ t : Finset s, s ⊆ ⋃ x ∈ t, U (x : s) x.2 := (hs.elim_nhds_subcover_nhdsSet' U hU).imp fun _ ↦ subset_of_mem_nhdsSet theorem IsCompact.elim_nhds_subcover (hs : IsCompact s) (U : X → Set X) (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ t : Finset X, (∀ x ∈ t, x ∈ s) ∧ s ⊆ ⋃ x ∈ t, U x := (hs.elim_nhds_subcover_nhdsSet hU).imp fun _ h ↦ h.imp_right subset_of_mem_nhdsSet theorem IsCompact.elim_nhdsWithin_subcover' (hs : IsCompact s) (U : ∀ x ∈ s, Set X) (hU : ∀ x (hx : x ∈ s), U x hx ∈ 𝓝[s] x) : ∃ t : Finset s, s ⊆ ⋃ x ∈ t, U x x.2 := by choose V V_nhds hV using fun x hx => mem_nhdsWithin_iff_exists_mem_nhds_inter.1 (hU x hx) refine (hs.elim_nhds_subcover' V V_nhds).imp fun t ht => subset_trans ?_ (iUnion₂_mono fun x _ => hV x x.2) simpa [← iUnion_inter, ← iUnion_coe_set] theorem IsCompact.elim_nhdsWithin_subcover (hs : IsCompact s) (U : X → Set X) (hU : ∀ x ∈ s, U x ∈ 𝓝[s] x) : ∃ t : Finset X, (∀ x ∈ t, x ∈ s) ∧ s ⊆ ⋃ x ∈ t, U x := by choose! V V_nhds hV using fun x hx => mem_nhdsWithin_iff_exists_mem_nhds_inter.1 (hU x hx) refine (hs.elim_nhds_subcover V V_nhds).imp fun t ⟨t_sub_s, ht⟩ => ⟨t_sub_s, subset_trans ?_ (iUnion₂_mono fun x hx => hV x (t_sub_s x hx))⟩ simpa [← iUnion_inter] /-- The neighborhood filter of a compact set is disjoint with a filter `l` if and only if the neighborhood filter of each point of this set is disjoint with `l`. -/ theorem IsCompact.disjoint_nhdsSet_left {l : Filter X} (hs : IsCompact s) : Disjoint (𝓝ˢ s) l ↔ ∀ x ∈ s, Disjoint (𝓝 x) l := by refine ⟨fun h x hx => h.mono_left <| nhds_le_nhdsSet hx, fun H => ?_⟩ choose! U hxU hUl using fun x hx => (nhds_basis_opens x).disjoint_iff_left.1 (H x hx) choose hxU hUo using hxU rcases hs.elim_nhds_subcover U fun x hx => (hUo x hx).mem_nhds (hxU x hx) with ⟨t, hts, hst⟩ refine (hasBasis_nhdsSet _).disjoint_iff_left.2 ⟨⋃ x ∈ t, U x, ⟨isOpen_biUnion fun x hx => hUo x (hts x hx), hst⟩, ?_⟩ rw [compl_iUnion₂, biInter_finset_mem] exact fun x hx => hUl x (hts x hx) /-- A filter `l` is disjoint with the neighborhood filter of a compact set if and only if it is disjoint with the neighborhood filter of each point of this set. -/ theorem IsCompact.disjoint_nhdsSet_right {l : Filter X} (hs : IsCompact s) : Disjoint l (𝓝ˢ s) ↔ ∀ x ∈ s, Disjoint l (𝓝 x) := by simpa only [disjoint_comm] using hs.disjoint_nhdsSet_left -- TODO: reformulate using `Disjoint` /-- For every directed family of closed sets whose intersection avoids a compact set, there exists a single element of the family which itself avoids this compact set. -/ theorem IsCompact.elim_directed_family_closed {ι : Type v} [Nonempty ι] (hs : IsCompact s) (t : ι → Set X) (htc : ∀ i, IsClosed (t i)) (hst : (s ∩ ⋂ i, t i) = ∅) (hdt : Directed (· ⊇ ·) t) : ∃ i : ι, s ∩ t i = ∅ := let ⟨t, ht⟩ := hs.elim_directed_cover (compl ∘ t) (fun i => (htc i).isOpen_compl) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_iUnion, exists_prop, mem_inter_iff, not_and, mem_iInter, mem_compl_iff] using hst) (hdt.mono_comp _ fun _ _ => compl_subset_compl.mpr) ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_iUnion, exists_prop, mem_inter_iff, not_and, mem_iInter, mem_compl_iff] using ht⟩ -- TODO: reformulate using `Disjoint` /-- For every family of closed sets whose intersection avoids a compact set, there exists a finite subfamily whose intersection avoids this compact set. -/ theorem IsCompact.elim_finite_subfamily_closed {ι : Type v} (hs : IsCompact s) (t : ι → Set X) (htc : ∀ i, IsClosed (t i)) (hst : (s ∩ ⋂ i, t i) = ∅) : ∃ u : Finset ι, (s ∩ ⋂ i ∈ u, t i) = ∅ := hs.elim_directed_family_closed _ (fun _ ↦ isClosed_biInter fun _ _ ↦ htc _) (by rwa [← iInter_eq_iInter_finset]) (directed_of_isDirected_le fun _ _ h ↦ biInter_subset_biInter_left h) /-- To show that a compact set intersects the intersection of a family of closed sets, it is sufficient to show that it intersects every finite subfamily. -/ theorem IsCompact.inter_iInter_nonempty {ι : Type v} (hs : IsCompact s) (t : ι → Set X) (htc : ∀ i, IsClosed (t i)) (hst : ∀ u : Finset ι, (s ∩ ⋂ i ∈ u, t i).Nonempty) : (s ∩ ⋂ i, t i).Nonempty := by contrapose! hst exact hs.elim_finite_subfamily_closed t htc hst /-- Cantor's intersection theorem for `iInter`: the intersection of a directed family of nonempty compact closed sets is nonempty. -/ theorem IsCompact.nonempty_iInter_of_directed_nonempty_isCompact_isClosed {ι : Type v} [hι : Nonempty ι] (t : ι → Set X) (htd : Directed (· ⊇ ·) t) (htn : ∀ i, (t i).Nonempty) (htc : ∀ i, IsCompact (t i)) (htcl : ∀ i, IsClosed (t i)) : (⋂ i, t i).Nonempty := by let i₀ := hι.some suffices (t i₀ ∩ ⋂ i, t i).Nonempty by rwa [inter_eq_right.mpr (iInter_subset _ i₀)] at this simp only [nonempty_iff_ne_empty] at htn ⊢ apply mt ((htc i₀).elim_directed_family_closed t htcl) push_neg simp only [← nonempty_iff_ne_empty] at htn ⊢ refine ⟨htd, fun i => ?_⟩ rcases htd i₀ i with ⟨j, hji₀, hji⟩ exact (htn j).mono (subset_inter hji₀ hji) /-- Cantor's intersection theorem for `sInter`: the intersection of a directed family of nonempty compact closed sets is nonempty. -/ theorem IsCompact.nonempty_sInter_of_directed_nonempty_isCompact_isClosed {S : Set (Set X)} [hS : Nonempty S] (hSd : DirectedOn (· ⊇ ·) S) (hSn : ∀ U ∈ S, U.Nonempty) (hSc : ∀ U ∈ S, IsCompact U) (hScl : ∀ U ∈ S, IsClosed U) : (⋂₀ S).Nonempty := by rw [sInter_eq_iInter] exact IsCompact.nonempty_iInter_of_directed_nonempty_isCompact_isClosed _ (DirectedOn.directed_val hSd) (fun i ↦ hSn i i.2) (fun i ↦ hSc i i.2) (fun i ↦ hScl i i.2) /-- Cantor's intersection theorem for sequences indexed by `ℕ`: the intersection of a decreasing sequence of nonempty compact closed sets is nonempty. -/ theorem IsCompact.nonempty_iInter_of_sequence_nonempty_isCompact_isClosed (t : ℕ → Set X) (htd : ∀ i, t (i + 1) ⊆ t i) (htn : ∀ i, (t i).Nonempty) (ht0 : IsCompact (t 0)) (htcl : ∀ i, IsClosed (t i)) : (⋂ i, t i).Nonempty := have tmono : Antitone t := antitone_nat_of_succ_le htd have htd : Directed (· ⊇ ·) t := tmono.directed_ge have : ∀ i, t i ⊆ t 0 := fun i => tmono <| Nat.zero_le i have htc : ∀ i, IsCompact (t i) := fun i => ht0.of_isClosed_subset (htcl i) (this i) IsCompact.nonempty_iInter_of_directed_nonempty_isCompact_isClosed t htd htn htc htcl /-- For every open cover of a compact set, there exists a finite subcover. -/ theorem IsCompact.elim_finite_subcover_image {b : Set ι} {c : ι → Set X} (hs : IsCompact s) (hc₁ : ∀ i ∈ b, IsOpen (c i)) (hc₂ : s ⊆ ⋃ i ∈ b, c i) : ∃ b', b' ⊆ b ∧ Set.Finite b' ∧ s ⊆ ⋃ i ∈ b', c i := by simp only [Subtype.forall', biUnion_eq_iUnion] at hc₁ hc₂ rcases hs.elim_finite_subcover (fun i => c i : b → Set X) hc₁ hc₂ with ⟨d, hd⟩ refine ⟨Subtype.val '' d.toSet, ?_, d.finite_toSet.image _, ?_⟩ · simp · rwa [biUnion_image] /-- A set `s` is compact if for every open cover of `s`, there exists a finite subcover. -/ theorem isCompact_of_finite_subcover (h : ∀ {ι : Type u} (U : ι → Set X), (∀ i, IsOpen (U i)) → (s ⊆ ⋃ i, U i) → ∃ t : Finset ι, s ⊆ ⋃ i ∈ t, U i) : IsCompact s := fun f hf hfs => by contrapose! h simp only [ClusterPt, not_neBot, ← disjoint_iff, SetCoe.forall', (nhds_basis_opens _).disjoint_iff_left] at h choose U hU hUf using h refine ⟨s, U, fun x => (hU x).2, fun x hx => mem_iUnion.2 ⟨⟨x, hx⟩, (hU _).1⟩, fun t ht => ?_⟩ refine compl_not_mem (le_principal_iff.1 hfs) ?_ refine mem_of_superset ((biInter_finset_mem t).2 fun x _ => hUf x) ?_ rw [subset_compl_comm, compl_iInter₂] simpa only [compl_compl] -- TODO: reformulate using `Disjoint` /-- A set `s` is compact if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem isCompact_of_finite_subfamily_closed (h : ∀ {ι : Type u} (t : ι → Set X), (∀ i, IsClosed (t i)) → (s ∩ ⋂ i, t i) = ∅ → ∃ u : Finset ι, (s ∩ ⋂ i ∈ u, t i) = ∅) : IsCompact s := isCompact_of_finite_subcover fun U hUo hsU => by rw [← disjoint_compl_right_iff_subset, compl_iUnion, disjoint_iff] at hsU rcases h (fun i => (U i)ᶜ) (fun i => (hUo _).isClosed_compl) hsU with ⟨t, ht⟩ refine ⟨t, ?_⟩ rwa [← disjoint_compl_right_iff_subset, compl_iUnion₂, disjoint_iff] /-- A set `s` is compact if and only if for every open cover of `s`, there exists a finite subcover. -/ theorem isCompact_iff_finite_subcover : IsCompact s ↔ ∀ {ι : Type u} (U : ι → Set X), (∀ i, IsOpen (U i)) → (s ⊆ ⋃ i, U i) → ∃ t : Finset ι, s ⊆ ⋃ i ∈ t, U i := ⟨fun hs => hs.elim_finite_subcover, isCompact_of_finite_subcover⟩ /-- A set `s` is compact if and only if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem isCompact_iff_finite_subfamily_closed : IsCompact s ↔ ∀ {ι : Type u} (t : ι → Set X), (∀ i, IsClosed (t i)) → (s ∩ ⋂ i, t i) = ∅ → ∃ u : Finset ι, (s ∩ ⋂ i ∈ u, t i) = ∅ := ⟨fun hs => hs.elim_finite_subfamily_closed, isCompact_of_finite_subfamily_closed⟩ /-- If `s : Set (X × Y)` belongs to `𝓝 x ×ˢ l` for all `x` from a compact set `K`, then it belongs to `(𝓝ˢ K) ×ˢ l`, i.e., there exist an open `U ⊇ K` and `t ∈ l` such that `U ×ˢ t ⊆ s`. -/ theorem IsCompact.mem_nhdsSet_prod_of_forall {K : Set X} {Y} {l : Filter Y} {s : Set (X × Y)} (hK : IsCompact K) (hs : ∀ x ∈ K, s ∈ 𝓝 x ×ˢ l) : s ∈ (𝓝ˢ K) ×ˢ l := by refine hK.induction_on (by simp) (fun t t' ht hs ↦ ?_) (fun t t' ht ht' ↦ ?_) fun x hx ↦ ?_ · exact prod_mono (nhdsSet_mono ht) le_rfl hs · simp [sup_prod, *] · rcases ((nhds_basis_opens _).prod l.basis_sets).mem_iff.1 (hs x hx) with ⟨⟨u, v⟩, ⟨⟨hx, huo⟩, hv⟩, hs⟩ refine ⟨u, nhdsWithin_le_nhds (huo.mem_nhds hx), mem_of_superset ?_ hs⟩ exact prod_mem_prod (huo.mem_nhdsSet.2 Subset.rfl) hv theorem IsCompact.nhdsSet_prod_eq_biSup {K : Set X} (hK : IsCompact K) {Y} (l : Filter Y) : (𝓝ˢ K) ×ˢ l = ⨆ x ∈ K, 𝓝 x ×ˢ l := le_antisymm (fun s hs ↦ hK.mem_nhdsSet_prod_of_forall <| by simpa using hs) (iSup₂_le fun _ hx ↦ prod_mono (nhds_le_nhdsSet hx) le_rfl) theorem IsCompact.prod_nhdsSet_eq_biSup {K : Set Y} (hK : IsCompact K) {X} (l : Filter X) : l ×ˢ (𝓝ˢ K) = ⨆ y ∈ K, l ×ˢ 𝓝 y := by simp only [prod_comm (f := l), hK.nhdsSet_prod_eq_biSup, map_iSup] /-- If `s : Set (X × Y)` belongs to `l ×ˢ 𝓝 y` for all `y` from a compact set `K`, then it belongs to `l ×ˢ (𝓝ˢ K)`, i.e., there exist `t ∈ l` and an open `U ⊇ K` such that `t ×ˢ U ⊆ s`. -/ theorem IsCompact.mem_prod_nhdsSet_of_forall {K : Set Y} {X} {l : Filter X} {s : Set (X × Y)} (hK : IsCompact K) (hs : ∀ y ∈ K, s ∈ l ×ˢ 𝓝 y) : s ∈ l ×ˢ 𝓝ˢ K := (hK.prod_nhdsSet_eq_biSup l).symm ▸ by simpa using hs -- TODO: Is there a way to prove directly the `inf` version and then deduce the `Prod` one ? -- That would seem a bit more natural. theorem IsCompact.nhdsSet_inf_eq_biSup {K : Set X} (hK : IsCompact K) (l : Filter X) : (𝓝ˢ K) ⊓ l = ⨆ x ∈ K, 𝓝 x ⊓ l := by have : ∀ f : Filter X, f ⊓ l = comap (fun x ↦ (x, x)) (f ×ˢ l) := fun f ↦ by
simpa only [comap_prod] using congrArg₂ (· ⊓ ·) comap_id.symm comap_id.symm simp_rw [this, ← comap_iSup, hK.nhdsSet_prod_eq_biSup] theorem IsCompact.inf_nhdsSet_eq_biSup {K : Set X} (hK : IsCompact K) (l : Filter X) : l ⊓ (𝓝ˢ K) = ⨆ x ∈ K, l ⊓ 𝓝 x := by simp only [inf_comm l, hK.nhdsSet_inf_eq_biSup] /-- If `s : Set X` belongs to `𝓝 x ⊓ l` for all `x` from a compact set `K`, then it belongs to `(𝓝ˢ K) ⊓ l`,
Mathlib/Topology/Compactness/Compact.lean
396
404
/- Copyright (c) 2023 Wrenna Robson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wrenna Robson -/ import Mathlib.Algebra.Group.Submonoid.Operations import Mathlib.Algebra.Group.Submonoid.Pointwise import Mathlib.Algebra.Group.Subgroup.Lattice /-! # Submonoid of units Given a submonoid `S` of a monoid `M`, we define the subgroup `S.units` as the units of `S` as a subgroup of `Mˣ`. That is to say, `S.units` contains all members of `S` which have a two-sided inverse within `S`, as terms of type `Mˣ`. We also define, for subgroups `S` of `Mˣ`, `S.ofUnits`, which is `S` considered as a submonoid of `M`. `Submonoid.units` and `Subgroup.ofUnits` form a Galois coinsertion. We also make the equivalent additive definitions. # Implementation details There are a number of other constructions which are multiplicatively equivalent to `S.units` but which have a different type. | Definition | Type | |----------------------|---------------| | `S.units` | `Subgroup Mˣ` | | `Sˣ` | `Type u` | | `IsUnit.submonoid S` | `Submonoid S` | | `S.units.ofUnits` | `Submonoid M` | All of these are distinct from `S.leftInv`, which is the submonoid of `M` which contains every member of `M` with a right inverse in `S`. -/ variable {M : Type*} [Monoid M] open Units open Pointwise in /-- The units of `S`, packaged as a subgroup of `Mˣ`. -/ @[to_additive "The additive units of `S`, packaged as an additive subgroup of `AddUnits M`."] def Submonoid.units (S : Submonoid M) : Subgroup Mˣ where toSubmonoid := S.comap (coeHom M) ⊓ (S.comap (coeHom M))⁻¹ inv_mem' ha := ⟨ha.2, ha.1⟩ /-- A subgroup of units represented as a submonoid of `M`. -/ @[to_additive "A additive subgroup of additive units represented as a additive submonoid of `M`."] def Subgroup.ofUnits (S : Subgroup Mˣ) : Submonoid M := S.toSubmonoid.map (coeHom M) @[to_additive] lemma Submonoid.units_mono : Monotone (Submonoid.units (M := M)) := fun _ _ hST _ ⟨h₁, h₂⟩ => ⟨hST h₁, hST h₂⟩ @[to_additive (attr := simp)] lemma Submonoid.ofUnits_units_le (S : Submonoid M) : S.units.ofUnits ≤ S := fun _ ⟨_, hm, he⟩ => he ▸ hm.1 @[to_additive] lemma Subgroup.ofUnits_mono : Monotone (Subgroup.ofUnits (M := M)) := fun _ _ hST _ ⟨x, hx, hy⟩ => ⟨x, hST hx, hy⟩ @[to_additive (attr := simp)] lemma Subgroup.units_ofUnits_eq (S : Subgroup Mˣ) : S.ofUnits.units = S := Subgroup.ext (fun _ => ⟨fun ⟨⟨_, hm, he⟩, _⟩ => (Units.ext he) ▸ hm, fun hm => ⟨⟨_, hm, rfl⟩, _, S.inv_mem hm, rfl⟩⟩) /-- A Galois coinsertion exists between the coercion from a subgroup of units to a submonoid and the reduction from a submonoid to its unit group. -/ @[to_additive "A Galois coinsertion exists between the coercion from a additive subgroup of additive units to a additive submonoid and the reduction from a additive submonoid to its unit group."] def ofUnits_units_gci : GaloisCoinsertion (Subgroup.ofUnits (M := M)) (Submonoid.units) := GaloisCoinsertion.monotoneIntro Submonoid.units_mono Subgroup.ofUnits_mono Submonoid.ofUnits_units_le Subgroup.units_ofUnits_eq @[to_additive] lemma ofUnits_units_gc : GaloisConnection (Subgroup.ofUnits (M := M)) (Submonoid.units) := ofUnits_units_gci.gc @[to_additive] lemma ofUnits_le_iff_le_units (S : Submonoid M) (H : Subgroup Mˣ) : H.ofUnits ≤ S ↔ H ≤ S.units := ofUnits_units_gc _ _ namespace Submonoid section Units @[to_additive] lemma mem_units_iff (S : Submonoid M) (x : Mˣ) : x ∈ S.units ↔ ((x : M) ∈ S ∧ ((x⁻¹ : Mˣ) : M) ∈ S) := Iff.rfl @[to_additive] lemma mem_units_of_val_mem_inv_val_mem (S : Submonoid M) {x : Mˣ} (h₁ : (x : M) ∈ S) (h₂ : ((x⁻¹ : Mˣ) : M) ∈ S) : x ∈ S.units := ⟨h₁, h₂⟩ @[to_additive] lemma val_mem_of_mem_units (S : Submonoid M) {x : Mˣ} (h : x ∈ S.units) : (x : M) ∈ S := h.1 @[to_additive] lemma inv_val_mem_of_mem_units (S : Submonoid M) {x : Mˣ} (h : x ∈ S.units) : ((x⁻¹ : Mˣ) : M) ∈ S := h.2 @[to_additive] lemma coe_inv_val_mul_coe_val (S : Submonoid M) {x : Sˣ} : ((x⁻¹ : Sˣ) : M) * ((x : Sˣ) : M) = 1 := DFunLike.congr_arg S.subtype x.inv_mul @[to_additive] lemma coe_val_mul_coe_inv_val (S : Submonoid M) {x : Sˣ} : ((x : Sˣ) : M) * ((x⁻¹ : Sˣ) : M) = 1 := DFunLike.congr_arg S.subtype x.mul_inv @[to_additive] lemma mk_inv_mul_mk_eq_one (S : Submonoid M) {x : Mˣ} (h : x ∈ S.units) : (⟨_, h.2⟩ : S) * ⟨_, h.1⟩ = 1 := Subtype.ext x.inv_mul @[to_additive] lemma mk_mul_mk_inv_eq_one (S : Submonoid M) {x : Mˣ} (h : x ∈ S.units) : (⟨_, h.1⟩ : S) * ⟨_, h.2⟩ = 1 := Subtype.ext x.mul_inv @[to_additive] lemma mul_mem_units (S : Submonoid M) {x y : Mˣ} (h₁ : x ∈ S.units) (h₂ : y ∈ S.units) : x * y ∈ S.units := mul_mem h₁ h₂ @[to_additive] lemma inv_mem_units (S : Submonoid M) {x : Mˣ} (h : x ∈ S.units) : x⁻¹ ∈ S.units := inv_mem h @[to_additive] lemma inv_mem_units_iff (S : Submonoid M) {x : Mˣ} : x⁻¹ ∈ S.units ↔ x ∈ S.units := inv_mem_iff /-- The equivalence between the subgroup of units of `S` and the type of units of `S`. -/ @[to_additive "The equivalence between the additive subgroup of additive units of `S` and the type of additive units of `S`."] def unitsEquivUnitsType (S : Submonoid M) : S.units ≃* Sˣ where toFun := fun ⟨_, h⟩ => ⟨⟨_, h.1⟩, ⟨_, h.2⟩, S.mk_mul_mk_inv_eq_one h, S.mk_inv_mul_mk_eq_one h⟩ invFun := fun x => ⟨⟨_, _, S.coe_val_mul_coe_inv_val, S.coe_inv_val_mul_coe_val⟩, ⟨x.1.2, x.2.2⟩⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl map_mul' := fun _ _ => rfl @[to_additive (attr := simp)] lemma units_top : (⊤ : Submonoid M).units = ⊤ := ofUnits_units_gc.u_top @[to_additive] lemma units_inf (S T : Submonoid M) : (S ⊓ T).units = S.units ⊓ T.units := ofUnits_units_gc.u_inf @[to_additive] lemma units_sInf {s : Set (Submonoid M)} : (sInf s).units = ⨅ S ∈ s, S.units := ofUnits_units_gc.u_sInf @[to_additive] lemma units_iInf {ι : Sort*} (f : ι → Submonoid M) : (iInf f).units = ⨅ (i : ι), (f i).units := ofUnits_units_gc.u_iInf @[to_additive] lemma units_iInf₂ {ι : Sort*} {κ : ι → Sort*} (f : (i : ι) → κ i → Submonoid M) : (⨅ (i : ι), ⨅ (j : κ i), f i j).units = ⨅ (i : ι), ⨅ (j : κ i), (f i j).units := ofUnits_units_gc.u_iInf₂ @[to_additive (attr := simp)] lemma units_bot : (⊥ : Submonoid M).units = ⊥ := ofUnits_units_gci.u_bot @[to_additive] lemma units_surjective : Function.Surjective (units (M := M)) := ofUnits_units_gci.u_surjective @[to_additive] lemma units_left_inverse : Function.LeftInverse (units (M := M)) (Subgroup.ofUnits (M := M)) := ofUnits_units_gci.u_l_leftInverse /-- The equivalence between the subgroup of units of `S` and the submonoid of unit elements of `S`. -/ @[to_additive "The equivalence between the additive subgroup of additive units of `S` and the additive submonoid of additive unit elements of `S`."] noncomputable def unitsEquivIsUnitSubmonoid (S : Submonoid M) : S.units ≃* IsUnit.submonoid S := S.unitsEquivUnitsType.trans unitsTypeEquivIsUnitSubmonoid end Units end Submonoid namespace Subgroup @[to_additive] lemma mem_ofUnits_iff (S : Subgroup Mˣ) (x : M) : x ∈ S.ofUnits ↔ ∃ y ∈ S, y = x := Iff.rfl @[to_additive] lemma mem_ofUnits (S : Subgroup Mˣ) {x : M} {y : Mˣ} (h₁ : y ∈ S) (h₂ : y = x) : x ∈ S.ofUnits := ⟨_, h₁, h₂⟩ @[to_additive] lemma exists_mem_ofUnits_val_eq (S : Subgroup Mˣ) {x : M} (h : x ∈ S.ofUnits) : ∃ y ∈ S, y = x := h @[to_additive] lemma mem_of_mem_val_ofUnits (S : Subgroup Mˣ) {y : Mˣ} (hy : (y : M) ∈ S.ofUnits) : y ∈ S := match hy with | ⟨_, hm, he⟩ => (Units.ext he) ▸ hm @[to_additive] lemma isUnit_of_mem_ofUnits (S : Subgroup Mˣ) {x : M} (hx : x ∈ S.ofUnits) : IsUnit x := match hx with | ⟨_, _, h⟩ => ⟨_, h⟩ /-- Given some `x : M` which is a member of the submonoid of unit elements corresponding to a subgroup of units, produce a unit of `M` whose coercion is equal to `x`. -/ @[to_additive "Given some `x : M` which is a member of the additive submonoid of additive unit elements corresponding to a subgroup of units, produce a unit of `M` whose coercion is equal to `x`."] noncomputable def unit_of_mem_ofUnits (S : Subgroup Mˣ) {x : M} (h : x ∈ S.ofUnits) : Mˣ := (Classical.choose h).copy x (Classical.choose_spec h).2.symm _ rfl @[to_additive] lemma unit_of_mem_ofUnits_spec_eq_of_val_mem (S : Subgroup Mˣ) {x : Mˣ} (h : (x : M) ∈ S.ofUnits) : S.unit_of_mem_ofUnits h = x := Units.ext rfl @[to_additive] lemma unit_of_mem_ofUnits_spec_val_eq_of_mem (S : Subgroup Mˣ) {x : M} (h : x ∈ S.ofUnits) : S.unit_of_mem_ofUnits h = x := rfl @[to_additive] lemma unit_of_mem_ofUnits_spec_mem (S : Subgroup Mˣ) {x : M} {h : x ∈ S.ofUnits} : S.unit_of_mem_ofUnits h ∈ S := S.mem_of_mem_val_ofUnits h @[to_additive] lemma unit_eq_unit_of_mem_ofUnits (S : Subgroup Mˣ) {x : M} (h₁ : IsUnit x) (h₂ : x ∈ S.ofUnits) : h₁.unit = S.unit_of_mem_ofUnits h₂ := Units.ext rfl @[to_additive] lemma unit_mem_of_mem_ofUnits (S : Subgroup Mˣ) {x : M} {h₁ : IsUnit x} (h₂ : x ∈ S.ofUnits) : h₁.unit ∈ S := S.unit_eq_unit_of_mem_ofUnits h₁ h₂ ▸ (S.unit_of_mem_ofUnits_spec_mem) @[to_additive] lemma mem_ofUnits_of_isUnit_of_unit_mem (S : Subgroup Mˣ) {x : M} (h₁ : IsUnit x) (h₂ : h₁.unit ∈ S) : x ∈ S.ofUnits := S.mem_ofUnits h₂ h₁.unit_spec @[to_additive] lemma mem_ofUnits_iff_exists_isUnit (S : Subgroup Mˣ) (x : M) : x ∈ S.ofUnits ↔ ∃ h : IsUnit x, h.unit ∈ S := ⟨fun h => ⟨S.isUnit_of_mem_ofUnits h, S.unit_mem_of_mem_ofUnits h⟩, fun ⟨hm, he⟩ => S.mem_ofUnits_of_isUnit_of_unit_mem hm he⟩ /-- The equivalence between the coercion of a subgroup `S` of `Mˣ` to a submonoid of `M` and the subgroup itself as a type. -/ @[to_additive "The equivalence between the coercion of an additive subgroup `S` of `Mˣ` to an additive submonoid of `M` and the additive subgroup itself as a type."] noncomputable def ofUnitsEquivType (S : Subgroup Mˣ) : S.ofUnits ≃* S where toFun := fun x => ⟨S.unit_of_mem_ofUnits x.2, S.unit_of_mem_ofUnits_spec_mem⟩ invFun := fun x => ⟨x.1, ⟨x.1, x.2, rfl⟩⟩ left_inv := fun _ => rfl right_inv := fun _ => Subtype.ext (Units.ext rfl) map_mul' := fun _ _ => Subtype.ext (Units.ext rfl) @[to_additive (attr := simp)] lemma ofUnits_bot : (⊥ : Subgroup Mˣ).ofUnits = ⊥ := ofUnits_units_gc.l_bot @[to_additive] lemma ofUnits_inf (S T : Subgroup Mˣ) : (S ⊔ T).ofUnits = S.ofUnits ⊔ T.ofUnits := ofUnits_units_gc.l_sup @[to_additive] lemma ofUnits_sSup (s : Set (Subgroup Mˣ)) : (sSup s).ofUnits = ⨆ S ∈ s, S.ofUnits := ofUnits_units_gc.l_sSup @[to_additive] lemma ofUnits_iSup {ι : Sort*} {f : ι → Subgroup Mˣ} : (iSup f).ofUnits = ⨆ (i : ι), (f i).ofUnits := ofUnits_units_gc.l_iSup @[to_additive] lemma ofUnits_iSup₂ {ι : Sort*} {κ : ι → Sort*} (f : (i : ι) → κ i → Subgroup Mˣ) : (⨆ (i : ι), ⨆ (j : κ i), f i j).ofUnits = ⨆ (i : ι), ⨆ (j : κ i), (f i j).ofUnits := ofUnits_units_gc.l_iSup₂ @[to_additive] lemma ofUnits_injective : Function.Injective (ofUnits (M := M)) := ofUnits_units_gci.l_injective @[to_additive (attr := simp)] lemma ofUnits_sup_units (S T : Subgroup Mˣ) : (S.ofUnits ⊔ T.ofUnits).units = S ⊔ T := ofUnits_units_gci.u_sup_l _ _ @[to_additive (attr := simp)] lemma ofUnits_inf_units (S T : Subgroup Mˣ) : (S.ofUnits ⊓ T.ofUnits).units = S ⊓ T := ofUnits_units_gci.u_inf_l _ _ @[to_additive] lemma ofUnits_right_inverse : Function.RightInverse (ofUnits (M := M)) (Submonoid.units (M := M)) := ofUnits_units_gci.u_l_leftInverse @[to_additive] lemma ofUnits_strictMono : StrictMono (ofUnits (M := M)) := ofUnits_units_gci.strictMono_l lemma ofUnits_le_ofUnits_iff {S T : Subgroup Mˣ} : S.ofUnits ≤ T.ofUnits ↔ S ≤ T := ofUnits_units_gci.l_le_l_iff /-- The equivalence between the top subgroup of `Mˣ` coerced to a submonoid `M` and the units of `M`. -/ @[to_additive "The equivalence between the additive subgroup of additive units of `S` and the additive submonoid of additive unit elements of `S`."] noncomputable def ofUnitsTopEquiv : (⊤ : Subgroup Mˣ).ofUnits ≃* Mˣ := (⊤ : Subgroup Mˣ).ofUnitsEquivType.trans topEquiv variable {G : Type*} [Group G] @[to_additive] lemma mem_units_iff_val_mem (H : Subgroup G) (x : Gˣ) : x ∈ H.units ↔ (x : G) ∈ H := by simp_rw [Submonoid.mem_units_iff, mem_toSubmonoid, val_inv_eq_inv_val, inv_mem_iff, and_self] @[to_additive] lemma mem_ofUnits_iff_toUnits_mem (H : Subgroup Gˣ) (x : G) : x ∈ H.ofUnits ↔ (toUnits x) ∈ H := by simp_rw [mem_ofUnits_iff, toUnits.surjective.exists, val_toUnits_apply, exists_eq_right]
@[to_additive (attr := simp)] lemma mem_iff_toUnits_mem_units (H : Subgroup G) (x : G) : toUnits x ∈ H.units ↔ x ∈ H := by simp_rw [mem_units_iff_val_mem, val_toUnits_apply]
Mathlib/Algebra/Group/Submonoid/Units.lean
318
320
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Floris van Doorn -/ import Mathlib.Geometry.Manifold.ChartedSpace /-! # Local properties invariant under a groupoid We study properties of a triple `(g, s, x)` where `g` is a function between two spaces `H` and `H'`, `s` is a subset of `H` and `x` is a point of `H`. Our goal is to register how such a property should behave to make sense in charted spaces modelled on `H` and `H'`. The main examples we have in mind are the properties "`g` is differentiable at `x` within `s`", or "`g` is smooth at `x` within `s`". We want to develop general results that, when applied in these specific situations, say that the notion of smooth function in a manifold behaves well under restriction, intersection, is local, and so on. ## Main definitions * `LocalInvariantProp G G' P` says that a property `P` of a triple `(g, s, x)` is local, and invariant under composition by elements of the groupoids `G` and `G'` of `H` and `H'` respectively. * `ChartedSpace.LiftPropWithinAt` (resp. `LiftPropAt`, `LiftPropOn` and `LiftProp`): given a property `P` of `(g, s, x)` where `g : H → H'`, define the corresponding property for functions `M → M'` where `M` and `M'` are charted spaces modelled respectively on `H` and `H'`. We define these properties within a set at a point, or at a point, or on a set, or in the whole space. This lifting process (obtained by restricting to suitable chart domains) can always be done, but it only behaves well under locality and invariance assumptions. Given `hG : LocalInvariantProp G G' P`, we deduce many properties of the lifted property on the charted spaces. For instance, `hG.liftPropWithinAt_inter` says that `P g s x` is equivalent to `P g (s ∩ t) x` whenever `t` is a neighborhood of `x`. ## Implementation notes We do not use dot notation for properties of the lifted property. For instance, we have `hG.liftPropWithinAt_congr` saying that if `LiftPropWithinAt P g s x` holds, and `g` and `g'` coincide on `s`, then `LiftPropWithinAt P g' s x` holds. We can't call it `LiftPropWithinAt.congr` as it is in the namespace associated to `LocalInvariantProp`, not in the one for `LiftPropWithinAt`. -/ noncomputable section open Set Filter TopologicalSpace open scoped Manifold Topology variable {H M H' M' X : Type*} variable [TopologicalSpace H] [TopologicalSpace M] [ChartedSpace H M] variable [TopologicalSpace H'] [TopologicalSpace M'] [ChartedSpace H' M'] variable [TopologicalSpace X] namespace StructureGroupoid variable (G : StructureGroupoid H) (G' : StructureGroupoid H') /-- Structure recording good behavior of a property of a triple `(f, s, x)` where `f` is a function, `s` a set and `x` a point. Good behavior here means locality and invariance under given groupoids (both in the source and in the target). Given such a good behavior, the lift of this property to charted spaces admitting these groupoids will inherit the good behavior. -/ structure LocalInvariantProp (P : (H → H') → Set H → H → Prop) : Prop where is_local : ∀ {s x u} {f : H → H'}, IsOpen u → x ∈ u → (P f s x ↔ P f (s ∩ u) x) right_invariance' : ∀ {s x f} {e : PartialHomeomorph H H}, e ∈ G → x ∈ e.source → P f s x → P (f ∘ e.symm) (e.symm ⁻¹' s) (e x) congr_of_forall : ∀ {s x} {f g : H → H'}, (∀ y ∈ s, f y = g y) → f x = g x → P f s x → P g s x left_invariance' : ∀ {s x f} {e' : PartialHomeomorph H' H'}, e' ∈ G' → s ⊆ f ⁻¹' e'.source → f x ∈ e'.source → P f s x → P (e' ∘ f) s x variable {G G'} {P : (H → H') → Set H → H → Prop} variable (hG : G.LocalInvariantProp G' P) include hG namespace LocalInvariantProp theorem congr_set {s t : Set H} {x : H} {f : H → H'} (hu : s =ᶠ[𝓝 x] t) : P f s x ↔ P f t x := by obtain ⟨o, host, ho, hxo⟩ := mem_nhds_iff.mp hu.mem_iff simp_rw [subset_def, mem_setOf, ← and_congr_left_iff, ← mem_inter_iff, ← Set.ext_iff] at host rw [hG.is_local ho hxo, host, ← hG.is_local ho hxo] theorem is_local_nhds {s u : Set H} {x : H} {f : H → H'} (hu : u ∈ 𝓝[s] x) : P f s x ↔ P f (s ∩ u) x := hG.congr_set <| mem_nhdsWithin_iff_eventuallyEq.mp hu theorem congr_iff_nhdsWithin {s : Set H} {x : H} {f g : H → H'} (h1 : f =ᶠ[𝓝[s] x] g) (h2 : f x = g x) : P f s x ↔ P g s x := by simp_rw [hG.is_local_nhds h1] exact ⟨hG.congr_of_forall (fun y hy ↦ hy.2) h2, hG.congr_of_forall (fun y hy ↦ hy.2.symm) h2.symm⟩ theorem congr_nhdsWithin {s : Set H} {x : H} {f g : H → H'} (h1 : f =ᶠ[𝓝[s] x] g) (h2 : f x = g x) (hP : P f s x) : P g s x := (hG.congr_iff_nhdsWithin h1 h2).mp hP theorem congr_nhdsWithin' {s : Set H} {x : H} {f g : H → H'} (h1 : f =ᶠ[𝓝[s] x] g) (h2 : f x = g x) (hP : P g s x) : P f s x := (hG.congr_iff_nhdsWithin h1 h2).mpr hP theorem congr_iff {s : Set H} {x : H} {f g : H → H'} (h : f =ᶠ[𝓝 x] g) : P f s x ↔ P g s x := hG.congr_iff_nhdsWithin (mem_nhdsWithin_of_mem_nhds h) (mem_of_mem_nhds h :) theorem congr {s : Set H} {x : H} {f g : H → H'} (h : f =ᶠ[𝓝 x] g) (hP : P f s x) : P g s x := (hG.congr_iff h).mp hP theorem congr' {s : Set H} {x : H} {f g : H → H'} (h : f =ᶠ[𝓝 x] g) (hP : P g s x) : P f s x := hG.congr h.symm hP theorem left_invariance {s : Set H} {x : H} {f : H → H'} {e' : PartialHomeomorph H' H'} (he' : e' ∈ G') (hfs : ContinuousWithinAt f s x) (hxe' : f x ∈ e'.source) : P (e' ∘ f) s x ↔ P f s x := by have h2f := hfs.preimage_mem_nhdsWithin (e'.open_source.mem_nhds hxe') have h3f := ((e'.continuousAt hxe').comp_continuousWithinAt hfs).preimage_mem_nhdsWithin <| e'.symm.open_source.mem_nhds <| e'.mapsTo hxe' constructor · intro h rw [hG.is_local_nhds h3f] at h have h2 := hG.left_invariance' (G'.symm he') inter_subset_right (e'.mapsTo hxe') h rw [← hG.is_local_nhds h3f] at h2 refine hG.congr_nhdsWithin ?_ (e'.left_inv hxe') h2
exact eventually_of_mem h2f fun x' ↦ e'.left_inv · simp_rw [hG.is_local_nhds h2f] exact hG.left_invariance' he' inter_subset_right hxe' theorem right_invariance {s : Set H} {x : H} {f : H → H'} {e : PartialHomeomorph H H} (he : e ∈ G) (hxe : x ∈ e.source) : P (f ∘ e.symm) (e.symm ⁻¹' s) (e x) ↔ P f s x := by refine ⟨fun h ↦ ?_, hG.right_invariance' he hxe⟩ have := hG.right_invariance' (G.symm he) (e.mapsTo hxe) h rw [e.symm_symm, e.left_inv hxe] at this refine hG.congr ?_ ((hG.congr_set ?_).mp this) · refine eventually_of_mem (e.open_source.mem_nhds hxe) fun x' hx' ↦ ?_ simp_rw [Function.comp_apply, e.left_inv hx'] · rw [eventuallyEq_set] refine eventually_of_mem (e.open_source.mem_nhds hxe) fun x' hx' ↦ ?_ simp_rw [mem_preimage, e.left_inv hx']
Mathlib/Geometry/Manifold/LocalInvariantProperties.lean
121
136
/- Copyright (c) 2017 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import Mathlib.Data.PFunctor.Univariate.Basic /-! # M-types M types are potentially infinite tree-like structures. They are defined as the greatest fixpoint of a polynomial functor. -/ universe u v w open Nat Function open List variable (F : PFunctor.{u}) namespace PFunctor namespace Approx /-- `CofixA F n` is an `n` level approximation of an M-type -/ inductive CofixA : ℕ → Type u | continue : CofixA 0 | intro {n} : ∀ a, (F.B a → CofixA n) → CofixA (succ n) /-- default inhabitant of `CofixA` -/ protected def CofixA.default [Inhabited F.A] : ∀ n, CofixA F n | 0 => CofixA.continue | succ n => CofixA.intro default fun _ => CofixA.default n instance [Inhabited F.A] {n} : Inhabited (CofixA F n) := ⟨CofixA.default F n⟩ theorem cofixA_eq_zero : ∀ x y : CofixA F 0, x = y | CofixA.continue, CofixA.continue => rfl variable {F} /-- The label of the root of the tree for a non-trivial approximation of the cofix of a pfunctor. -/ def head' : ∀ {n}, CofixA F (succ n) → F.A | _, CofixA.intro i _ => i /-- for a non-trivial approximation, return all the subtrees of the root -/ def children' : ∀ {n} (x : CofixA F (succ n)), F.B (head' x) → CofixA F n | _, CofixA.intro _ f => f theorem approx_eta {n : ℕ} (x : CofixA F (n + 1)) : x = CofixA.intro (head' x) (children' x) := by cases x; rfl /-- Relation between two approximations of the cofix of a pfunctor that state they both contain the same data until one of them is truncated -/ inductive Agree : ∀ {n : ℕ}, CofixA F n → CofixA F (n + 1) → Prop | continu (x : CofixA F 0) (y : CofixA F 1) : Agree x y | intro {n} {a} (x : F.B a → CofixA F n) (x' : F.B a → CofixA F (n + 1)) : (∀ i : F.B a, Agree (x i) (x' i)) → Agree (CofixA.intro a x) (CofixA.intro a x') /-- Given an infinite series of approximations `approx`, `AllAgree approx` states that they are all consistent with each other. -/ def AllAgree (x : ∀ n, CofixA F n) := ∀ n, Agree (x n) (x (succ n)) @[simp] theorem agree_trivial {x : CofixA F 0} {y : CofixA F 1} : Agree x y := by constructor @[deprecated (since := "2024-12-25")] alias agree_trival := agree_trivial theorem agree_children {n : ℕ} (x : CofixA F (succ n)) (y : CofixA F (succ n + 1)) {i j} (h₀ : HEq i j) (h₁ : Agree x y) : Agree (children' x i) (children' y j) := by obtain - | ⟨_, _, hagree⟩ := h₁; cases h₀ apply hagree /-- `truncate a` turns `a` into a more limited approximation -/ def truncate : ∀ {n : ℕ}, CofixA F (n + 1) → CofixA F n | 0, CofixA.intro _ _ => CofixA.continue | succ _, CofixA.intro i f => CofixA.intro i <| truncate ∘ f theorem truncate_eq_of_agree {n : ℕ} (x : CofixA F n) (y : CofixA F (succ n)) (h : Agree x y) : truncate y = x := by induction n <;> cases x <;> cases y · rfl · -- cases' h with _ _ _ _ _ h₀ h₁ cases h simp only [truncate, Function.comp_def, eq_self_iff_true, heq_iff_eq] -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): used to be `ext y` rename_i n_ih a f y h₁ suffices (fun x => truncate (y x)) = f by simp [this] funext y apply n_ih apply h₁ variable {X : Type w} variable (f : X → F X) /-- `sCorec f i n` creates an approximation of height `n` of the final coalgebra of `f` -/ def sCorec : X → ∀ n, CofixA F n | _, 0 => CofixA.continue | j, succ _ => CofixA.intro (f j).1 fun i => sCorec ((f j).2 i) _ theorem P_corec (i : X) (n : ℕ) : Agree (sCorec f i n) (sCorec f i (succ n)) := by induction' n with n n_ih generalizing i constructor obtain ⟨y, g⟩ := f i constructor introv apply n_ih /-- `Path F` provides indices to access internal nodes in `Corec F` -/ def Path (F : PFunctor.{u}) := List F.Idx instance Path.inhabited : Inhabited (Path F) := ⟨[]⟩ open List Nat instance CofixA.instSubsingleton : Subsingleton (CofixA F 0) := ⟨by rintro ⟨⟩ ⟨⟩; rfl⟩ theorem head_succ' (n m : ℕ) (x : ∀ n, CofixA F n) (Hconsistent : AllAgree x) : head' (x (succ n)) = head' (x (succ m)) := by suffices ∀ n, head' (x (succ n)) = head' (x 1) by simp [this] clear m n intro n rcases h₀ : x (succ n) with - | ⟨_, f₀⟩ cases h₁ : x 1 dsimp only [head'] induction' n with n n_ih · rw [h₁] at h₀ cases h₀ trivial · have H := Hconsistent (succ n) cases h₂ : x (succ n) rw [h₀, h₂] at H apply n_ih (truncate ∘ f₀) rw [h₂] obtain - | ⟨_, _, hagree⟩ := H congr funext j dsimp only [comp_apply] rw [truncate_eq_of_agree] apply hagree end Approx open Approx /-- Internal definition for `M`. It is needed to avoid name clashes between `M.mk` and `M.casesOn` and the declarations generated for the structure -/ structure MIntl where /-- An `n`-th level approximation, for each depth `n` -/ approx : ∀ n, CofixA F n /-- Each approximation agrees with the next -/ consistent : AllAgree approx /-- For polynomial functor `F`, `M F` is its final coalgebra -/ def M := MIntl F theorem M.default_consistent [Inhabited F.A] : ∀ n, Agree (default : CofixA F n) default | 0 => Agree.continu _ _ | succ n => Agree.intro _ _ fun _ => M.default_consistent n instance M.inhabited [Inhabited F.A] : Inhabited (M F) := ⟨{ approx := default consistent := M.default_consistent _ }⟩ instance MIntl.inhabited [Inhabited F.A] : Inhabited (MIntl F) := show Inhabited (M F) by infer_instance namespace M theorem ext' (x y : M F) (H : ∀ i : ℕ, x.approx i = y.approx i) : x = y := by cases x cases y congr with n apply H variable {X : Type*} variable (f : X → F X) variable {F} /-- Corecursor for the M-type defined by `F`. -/ protected def corec (i : X) : M F where approx := sCorec f i consistent := P_corec _ _ /-- given a tree generated by `F`, `head` gives us the first piece of data it contains -/ def head (x : M F) := head' (x.1 1) /-- return all the subtrees of the root of a tree `x : M F` -/ def children (x : M F) (i : F.B (head x)) : M F := let H := fun n : ℕ => @head_succ' _ n 0 x.1 x.2 { approx := fun n => children' (x.1 _) (cast (congr_arg _ <| by simp only [head, H]) i) consistent := by intro n have P' := x.2 (succ n) apply agree_children _ _ _ P' trans i · apply cast_heq symm apply cast_heq } /-- select a subtree using an `i : F.Idx` or return an arbitrary tree if `i` designates no subtree of `x` -/ def ichildren [Inhabited (M F)] [DecidableEq F.A] (i : F.Idx) (x : M F) : M F := if H' : i.1 = head x then children x (cast (congr_arg _ <| by simp only [head, H']) i.2) else default theorem head_succ (n m : ℕ) (x : M F) : head' (x.approx (succ n)) = head' (x.approx (succ m)) := head_succ' n m _ x.consistent theorem head_eq_head' : ∀ (x : M F) (n : ℕ), head x = head' (x.approx <| n + 1) | ⟨_, h⟩, _ => head_succ' _ _ _ h theorem head'_eq_head : ∀ (x : M F) (n : ℕ), head' (x.approx <| n + 1) = head x | ⟨_, h⟩, _ => head_succ' _ _ _ h theorem truncate_approx (x : M F) (n : ℕ) : truncate (x.approx <| n + 1) = x.approx n := truncate_eq_of_agree _ _ (x.consistent _) /-- unfold an M-type -/ def dest : M F → F (M F) | x => ⟨head x, fun i => children x i⟩ namespace Approx /-- generates the approximations needed for `M.mk` -/ protected def sMk (x : F (M F)) : ∀ n, CofixA F n | 0 => CofixA.continue | succ n => CofixA.intro x.1 fun i => (x.2 i).approx n protected theorem P_mk (x : F (M F)) : AllAgree (Approx.sMk x) | 0 => by constructor | succ n => by constructor introv apply (x.2 i).consistent end Approx /-- constructor for M-types -/ protected def mk (x : F (M F)) : M F where approx := Approx.sMk x consistent := Approx.P_mk x /-- `Agree' n` relates two trees of type `M F` that are the same up to depth `n` -/ inductive Agree' : ℕ → M F → M F → Prop | trivial (x y : M F) : Agree' 0 x y | step {n : ℕ} {a} (x y : F.B a → M F) {x' y'} : x' = M.mk ⟨a, x⟩ → y' = M.mk ⟨a, y⟩ → (∀ i, Agree' n (x i) (y i)) → Agree' (succ n) x' y' @[simp] theorem dest_mk (x : F (M F)) : dest (M.mk x) = x := rfl @[simp] theorem mk_dest (x : M F) : M.mk (dest x) = x := by apply ext' intro n dsimp only [M.mk] induction' n with n · apply @Subsingleton.elim _ CofixA.instSubsingleton dsimp only [Approx.sMk, dest, head] rcases h : x.approx (succ n) with - | ⟨hd, ch⟩ have h' : hd = head' (x.approx 1) := by rw [← head_succ' n, h, head'] apply x.consistent revert ch rw [h'] intros ch h congr ext a dsimp only [children] generalize hh : cast _ a = a'' rw [cast_eq_iff_heq] at hh revert a'' rw [h] intros _ hh cases hh rfl theorem mk_inj {x y : F (M F)} (h : M.mk x = M.mk y) : x = y := by rw [← dest_mk x, h, dest_mk] /-- destructor for M-types -/ protected def cases {r : M F → Sort w} (f : ∀ x : F (M F), r (M.mk x)) (x : M F) : r x := suffices r (M.mk (dest x)) by rw [← mk_dest x] exact this f _ /-- destructor for M-types -/ protected def casesOn {r : M F → Sort w} (x : M F) (f : ∀ x : F (M F), r (M.mk x)) : r x := M.cases f x /-- destructor for M-types, similar to `casesOn` but also gives access directly to the root and subtrees on an M-type -/ protected def casesOn' {r : M F → Sort w} (x : M F) (f : ∀ a f, r (M.mk ⟨a, f⟩)) : r x := M.casesOn x (fun ⟨a, g⟩ => f a g) theorem approx_mk (a : F.A) (f : F.B a → M F) (i : ℕ) : (M.mk ⟨a, f⟩).approx (succ i) = CofixA.intro a fun j => (f j).approx i := rfl @[simp] theorem agree'_refl {n : ℕ} (x : M F) : Agree' n x x := by induction' n with _ n_ih generalizing x <;> induction x using PFunctor.M.casesOn' <;> constructor <;> try rfl intros apply n_ih theorem agree_iff_agree' {n : ℕ} (x y : M F) : Agree (x.approx n) (y.approx <| n + 1) ↔ Agree' n x y := by constructor <;> intro h · induction' n with _ n_ih generalizing x y · constructor · induction x using PFunctor.M.casesOn' induction y using PFunctor.M.casesOn' simp only [approx_mk] at h obtain - | ⟨_, _, hagree⟩ := h constructor <;> try rfl intro i apply n_ih apply hagree · induction' n with _ n_ih generalizing x y · constructor · obtain - | @⟨_, a, x', y'⟩ := h induction' x using PFunctor.M.casesOn' with x_a x_f induction' y using PFunctor.M.casesOn' with y_a y_f simp only [approx_mk] have h_a_1 := mk_inj ‹M.mk ⟨x_a, x_f⟩ = M.mk ⟨a, x'⟩› cases h_a_1 replace h_a_2 := mk_inj ‹M.mk ⟨y_a, y_f⟩ = M.mk ⟨a, y'⟩› cases h_a_2 constructor intro i apply n_ih simp [*] @[simp] theorem cases_mk {r : M F → Sort*} (x : F (M F)) (f : ∀ x : F (M F), r (M.mk x)) : PFunctor.M.cases f (M.mk x) = f x := by dsimp only [M.mk, PFunctor.M.cases, dest, head, Approx.sMk, head'] cases x; dsimp only [Approx.sMk] simp only [Eq.mpr] apply congrFun rfl @[simp] theorem casesOn_mk {r : M F → Sort*} (x : F (M F)) (f : ∀ x : F (M F), r (M.mk x)) : PFunctor.M.casesOn (M.mk x) f = f x := cases_mk x f @[simp] theorem casesOn_mk' {r : M F → Sort*} {a} (x : F.B a → M F) (f : ∀ (a) (f : F.B a → M F), r (M.mk ⟨a, f⟩)) : PFunctor.M.casesOn' (M.mk ⟨a, x⟩) f = f a x := @cases_mk F r ⟨a, x⟩ (fun ⟨a, g⟩ => f a g) /-- `IsPath p x` tells us if `p` is a valid path through `x` -/ inductive IsPath : Path F → M F → Prop | nil (x : M F) : IsPath [] x | cons (xs : Path F) {a} (x : M F) (f : F.B a → M F) (i : F.B a) : x = M.mk ⟨a, f⟩ → IsPath xs (f i) → IsPath (⟨a, i⟩ :: xs) x theorem isPath_cons {xs : Path F} {a a'} {f : F.B a → M F} {i : F.B a'} : IsPath (⟨a', i⟩ :: xs) (M.mk ⟨a, f⟩) → a = a' := by generalize h : M.mk ⟨a, f⟩ = x rintro (_ | ⟨_, _, _, _, rfl, _⟩) cases mk_inj h rfl theorem isPath_cons' {xs : Path F} {a} {f : F.B a → M F} {i : F.B a} : IsPath (⟨a, i⟩ :: xs) (M.mk ⟨a, f⟩) → IsPath xs (f i) := by generalize h : M.mk ⟨a, f⟩ = x rintro (_ | ⟨_, _, _, _, rfl, hp⟩) cases mk_inj h exact hp /-- follow a path through a value of `M F` and return the subtree found at the end of the path if it is a valid path for that value and return a default tree -/ def isubtree [DecidableEq F.A] [Inhabited (M F)] : Path F → M F → M F | [], x => x | ⟨a, i⟩ :: ps, x => PFunctor.M.casesOn' (r := fun _ => M F) x (fun a' f => if h : a = a' then isubtree ps (f <| cast (by rw [h]) i) else default (α := M F) ) /-- similar to `isubtree` but returns the data at the end of the path instead of the whole subtree -/ def iselect [DecidableEq F.A] [Inhabited (M F)] (ps : Path F) : M F → F.A := fun x : M F => head <| isubtree ps x theorem iselect_eq_default [DecidableEq F.A] [Inhabited (M F)] (ps : Path F) (x : M F) (h : ¬IsPath ps x) : iselect ps x = head default := by induction' ps with ps_hd ps_tail ps_ih generalizing x · exfalso apply h constructor · obtain ⟨a, i⟩ := ps_hd induction' x using PFunctor.M.casesOn' with x_a x_f simp only [iselect, isubtree] at ps_ih ⊢ by_cases h'' : a = x_a · subst x_a simp only [dif_pos, eq_self_iff_true, casesOn_mk'] rw [ps_ih] intro h' apply h constructor <;> try rfl apply h' · simp [*] @[simp] theorem head_mk (x : F (M F)) : head (M.mk x) = x.1 := Eq.symm <|
calc x.1 = (dest (M.mk x)).1 := by rw [dest_mk] _ = head (M.mk x) := rfl theorem children_mk {a} (x : F.B a → M F) (i : F.B (head (M.mk ⟨a, x⟩))) : children (M.mk ⟨a, x⟩) i = x (cast (by rw [head_mk]) i) := by apply ext'; intro n; rfl
Mathlib/Data/PFunctor/Univariate/M.lean
435
441
/- 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.SetTheory.Cardinal.Finite import Mathlib.Data.Set.Finite.Powerset /-! # Noncomputable Set Cardinality We define the cardinality of set `s` as a term `Set.encard s : ℕ∞` and a term `Set.ncard s : ℕ`. The latter takes the junk value of zero if `s` is infinite. Both functions are noncomputable, and are defined in terms of `ENat.card` (which takes a type as its argument); this file can be seen as an API for the same function in the special case where the type is a coercion of a `Set`, allowing for smoother interactions with the `Set` API. `Set.encard` never takes junk values, so is more mathematically natural than `Set.ncard`, even though it takes values in a less convenient type. It is probably the right choice in settings where one is concerned with the cardinalities of sets that may or may not be infinite. `Set.ncard` has a nicer codomain, but when using it, `Set.Finite` hypotheses are normally needed to make sure its values are meaningful. More generally, `Set.ncard` is intended to be used over the obvious alternative `Finset.card` when finiteness is 'propositional' rather than 'structural'. When working with sets that are finite by virtue of their definition, then `Finset.card` probably makes more sense. One setting where `Set.ncard` works nicely is in a type `α` with `[Finite α]`, where every set is automatically finite. In this setting, we use default arguments and a simple tactic so that finiteness goals are discharged automatically in `Set.ncard` theorems. ## Main Definitions * `Set.encard s` is the cardinality of the set `s` as an extended natural number, with value `⊤` if `s` is infinite. * `Set.ncard s` is the cardinality of the set `s` as a natural number, provided `s` is Finite. If `s` is Infinite, then `Set.ncard s = 0`. * `toFinite_tac` is a tactic that tries to synthesize a `Set.Finite s` argument with `Set.toFinite`. This will work for `s : Set α` where there is a `Finite α` instance. ## Implementation Notes The theorems in this file are very similar to those in `Data.Finset.Card`, but with `Set` operations instead of `Finset`. We first prove all the theorems for `Set.encard`, and then derive most of the `Set.ncard` results as a consequence. Things are done this way to avoid reliance on the `Finset` API for theorems about infinite sets, and to allow for a refactor that removes or modifies `Set.ncard` in the future. Nearly all the theorems for `Set.ncard` require finiteness of one or more of their arguments. We provide this assumption with a default argument of the form `(hs : s.Finite := by toFinite_tac)`, where `toFinite_tac` will find an `s.Finite` term in the cases where `s` is a set in a `Finite` type. Often, where there are two set arguments `s` and `t`, the finiteness of one follows from the other in the context of the theorem, in which case we only include the ones that are needed, and derive the other inside the proof. A few of the theorems, such as `ncard_union_le` do not require finiteness arguments; they are true by coincidence due to junk values. -/ namespace Set variable {α β : Type*} {s t : Set α} /-- The cardinality of a set as a term in `ℕ∞` -/ noncomputable def encard (s : Set α) : ℕ∞ := ENat.card s @[simp] theorem encard_univ_coe (s : Set α) : encard (univ : Set s) = encard s := by rw [encard, encard, ENat.card_congr (Equiv.Set.univ ↑s)] theorem encard_univ (α : Type*) : encard (univ : Set α) = ENat.card α := by rw [encard, ENat.card_congr (Equiv.Set.univ α)] theorem Finite.encard_eq_coe_toFinset_card (h : s.Finite) : s.encard = h.toFinset.card := by have := h.fintype rw [encard, ENat.card_eq_coe_fintype_card, toFinite_toFinset, toFinset_card] theorem encard_eq_coe_toFinset_card (s : Set α) [Fintype s] : encard s = s.toFinset.card := by have h := toFinite s rw [h.encard_eq_coe_toFinset_card, toFinite_toFinset] @[simp] theorem toENat_cardinalMk (s : Set α) : (Cardinal.mk s).toENat = s.encard := rfl theorem toENat_cardinalMk_subtype (P : α → Prop) : (Cardinal.mk {x // P x}).toENat = {x | P x}.encard := rfl @[simp] theorem coe_fintypeCard (s : Set α) [Fintype s] : Fintype.card s = s.encard := by simp [encard_eq_coe_toFinset_card] @[simp, norm_cast] theorem encard_coe_eq_coe_finsetCard (s : Finset α) : encard (s : Set α) = s.card := by rw [Finite.encard_eq_coe_toFinset_card (Finset.finite_toSet s)]; simp @[simp] theorem Infinite.encard_eq {s : Set α} (h : s.Infinite) : s.encard = ⊤ := by have := h.to_subtype rw [encard, ENat.card_eq_top_of_infinite] @[simp] theorem encard_eq_zero : s.encard = 0 ↔ s = ∅ := by rw [encard, ENat.card_eq_zero_iff_empty, isEmpty_subtype, eq_empty_iff_forall_not_mem] @[simp] theorem encard_empty : (∅ : Set α).encard = 0 := by rw [encard_eq_zero] theorem nonempty_of_encard_ne_zero (h : s.encard ≠ 0) : s.Nonempty := by rwa [nonempty_iff_ne_empty, Ne, ← encard_eq_zero] theorem encard_ne_zero : s.encard ≠ 0 ↔ s.Nonempty := by rw [ne_eq, encard_eq_zero, nonempty_iff_ne_empty] @[simp] theorem encard_pos : 0 < s.encard ↔ s.Nonempty := by rw [pos_iff_ne_zero, encard_ne_zero] protected alias ⟨_, Nonempty.encard_pos⟩ := encard_pos @[simp] theorem encard_singleton (e : α) : ({e} : Set α).encard = 1 := by rw [encard, ENat.card_eq_coe_fintype_card, Fintype.card_ofSubsingleton, Nat.cast_one] theorem encard_union_eq (h : Disjoint s t) : (s ∪ t).encard = s.encard + t.encard := by classical simp [encard, ENat.card_congr (Equiv.Set.union h)] theorem encard_insert_of_not_mem {a : α} (has : a ∉ s) : (insert a s).encard = s.encard + 1 := by rw [← union_singleton, encard_union_eq (by simpa), encard_singleton] theorem Finite.encard_lt_top (h : s.Finite) : s.encard < ⊤ := by induction s, h using Set.Finite.induction_on with | empty => simp | insert hat _ ht' => rw [encard_insert_of_not_mem hat] exact lt_tsub_iff_right.1 ht' theorem Finite.encard_eq_coe (h : s.Finite) : s.encard = ENat.toNat s.encard := (ENat.coe_toNat h.encard_lt_top.ne).symm theorem Finite.exists_encard_eq_coe (h : s.Finite) : ∃ (n : ℕ), s.encard = n := ⟨_, h.encard_eq_coe⟩ @[simp] theorem encard_lt_top_iff : s.encard < ⊤ ↔ s.Finite := ⟨fun h ↦ by_contra fun h' ↦ h.ne (Infinite.encard_eq h'), Finite.encard_lt_top⟩ @[simp] theorem encard_eq_top_iff : s.encard = ⊤ ↔ s.Infinite := by rw [← not_iff_not, ← Ne, ← lt_top_iff_ne_top, encard_lt_top_iff, not_infinite] alias ⟨_, encard_eq_top⟩ := encard_eq_top_iff theorem encard_ne_top_iff : s.encard ≠ ⊤ ↔ s.Finite := by simp theorem finite_of_encard_le_coe {k : ℕ} (h : s.encard ≤ k) : s.Finite := by rw [← encard_lt_top_iff]; exact h.trans_lt (WithTop.coe_lt_top _) theorem finite_of_encard_eq_coe {k : ℕ} (h : s.encard = k) : s.Finite := finite_of_encard_le_coe h.le theorem encard_le_coe_iff {k : ℕ} : s.encard ≤ k ↔ s.Finite ∧ ∃ (n₀ : ℕ), s.encard = n₀ ∧ n₀ ≤ k := ⟨fun h ↦ ⟨finite_of_encard_le_coe h, by rwa [ENat.le_coe_iff] at h⟩, fun ⟨_,⟨n₀,hs, hle⟩⟩ ↦ by rwa [hs, Nat.cast_le]⟩ @[simp] theorem encard_prod : (s ×ˢ t).encard = s.encard * t.encard := by simp [Set.encard, ENat.card_congr (Equiv.Set.prod ..)] section Lattice theorem encard_le_encard (h : s ⊆ t) : s.encard ≤ t.encard := by rw [← union_diff_cancel h, encard_union_eq disjoint_sdiff_right]; exact le_self_add @[deprecated (since := "2025-01-05")] alias encard_le_card := encard_le_encard theorem encard_mono {α : Type*} : Monotone (encard : Set α → ℕ∞) := fun _ _ ↦ encard_le_encard theorem encard_diff_add_encard_of_subset (h : s ⊆ t) : (t \ s).encard + s.encard = t.encard := by rw [← encard_union_eq disjoint_sdiff_left, diff_union_self, union_eq_self_of_subset_right h] @[simp] theorem one_le_encard_iff_nonempty : 1 ≤ s.encard ↔ s.Nonempty := by rw [nonempty_iff_ne_empty, Ne, ← encard_eq_zero, ENat.one_le_iff_ne_zero] theorem encard_diff_add_encard_inter (s t : Set α) : (s \ t).encard + (s ∩ t).encard = s.encard := by rw [← encard_union_eq (disjoint_of_subset_right inter_subset_right disjoint_sdiff_left), diff_union_inter] theorem encard_union_add_encard_inter (s t : Set α) : (s ∪ t).encard + (s ∩ t).encard = s.encard + t.encard := by rw [← diff_union_self, encard_union_eq disjoint_sdiff_left, add_right_comm, encard_diff_add_encard_inter] theorem encard_eq_encard_iff_encard_diff_eq_encard_diff (h : (s ∩ t).Finite) : s.encard = t.encard ↔ (s \ t).encard = (t \ s).encard := by rw [← encard_diff_add_encard_inter s t, ← encard_diff_add_encard_inter t s, inter_comm t s, WithTop.add_right_inj h.encard_lt_top.ne] theorem encard_le_encard_iff_encard_diff_le_encard_diff (h : (s ∩ t).Finite) : s.encard ≤ t.encard ↔ (s \ t).encard ≤ (t \ s).encard := by rw [← encard_diff_add_encard_inter s t, ← encard_diff_add_encard_inter t s, inter_comm t s, WithTop.add_le_add_iff_right h.encard_lt_top.ne] theorem encard_lt_encard_iff_encard_diff_lt_encard_diff (h : (s ∩ t).Finite) : s.encard < t.encard ↔ (s \ t).encard < (t \ s).encard := by rw [← encard_diff_add_encard_inter s t, ← encard_diff_add_encard_inter t s, inter_comm t s, WithTop.add_lt_add_iff_right h.encard_lt_top.ne] theorem encard_union_le (s t : Set α) : (s ∪ t).encard ≤ s.encard + t.encard := by rw [← encard_union_add_encard_inter]; exact le_self_add theorem finite_iff_finite_of_encard_eq_encard (h : s.encard = t.encard) : s.Finite ↔ t.Finite := by rw [← encard_lt_top_iff, ← encard_lt_top_iff, h] theorem infinite_iff_infinite_of_encard_eq_encard (h : s.encard = t.encard) : s.Infinite ↔ t.Infinite := by rw [← encard_eq_top_iff, h, encard_eq_top_iff] theorem Finite.finite_of_encard_le {s : Set α} {t : Set β} (hs : s.Finite) (h : t.encard ≤ s.encard) : t.Finite := encard_lt_top_iff.1 (h.trans_lt hs.encard_lt_top) lemma Finite.eq_of_subset_of_encard_le' (ht : t.Finite) (hst : s ⊆ t) (hts : t.encard ≤ s.encard) : s = t := by rw [← zero_add (a := encard s), ← encard_diff_add_encard_of_subset hst] at hts have hdiff := WithTop.le_of_add_le_add_right (ht.subset hst).encard_lt_top.ne hts rw [nonpos_iff_eq_zero, encard_eq_zero, diff_eq_empty] at hdiff exact hst.antisymm hdiff theorem Finite.eq_of_subset_of_encard_le (hs : s.Finite) (hst : s ⊆ t) (hts : t.encard ≤ s.encard) : s = t := (hs.finite_of_encard_le hts).eq_of_subset_of_encard_le' hst hts theorem Finite.encard_lt_encard (hs : s.Finite) (h : s ⊂ t) : s.encard < t.encard := (encard_mono h.subset).lt_of_ne fun he ↦ h.ne (hs.eq_of_subset_of_encard_le h.subset he.symm.le) theorem encard_strictMono [Finite α] : StrictMono (encard : Set α → ℕ∞) := fun _ _ h ↦ (toFinite _).encard_lt_encard h theorem encard_diff_add_encard (s t : Set α) : (s \ t).encard + t.encard = (s ∪ t).encard := by rw [← encard_union_eq disjoint_sdiff_left, diff_union_self] theorem encard_le_encard_diff_add_encard (s t : Set α) : s.encard ≤ (s \ t).encard + t.encard := (encard_mono subset_union_left).trans_eq (encard_diff_add_encard _ _).symm theorem tsub_encard_le_encard_diff (s t : Set α) : s.encard - t.encard ≤ (s \ t).encard := by rw [tsub_le_iff_left, add_comm]; apply encard_le_encard_diff_add_encard theorem encard_add_encard_compl (s : Set α) : s.encard + sᶜ.encard = (univ : Set α).encard := by rw [← encard_union_eq disjoint_compl_right, union_compl_self] end Lattice
section InsertErase variable {a b : α}
Mathlib/Data/Set/Card.lean
247
250
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import Mathlib.Algebra.Order.Group.Abs import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Order.Interval.Set.Basic import Mathlib.Logic.Pairwise /-! ### Lemmas about arithmetic operations and intervals. -/ variable {α : Type*} namespace Set section OrderedCommGroup variable [CommGroup α] [PartialOrder α] [IsOrderedMonoid α] {a c d : α} /-! `inv_mem_Ixx_iff`, `sub_mem_Ixx_iff` -/ @[to_additive] theorem inv_mem_Icc_iff : a⁻¹ ∈ Set.Icc c d ↔ a ∈ Set.Icc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' le_inv' @[to_additive] theorem inv_mem_Ico_iff : a⁻¹ ∈ Set.Ico c d ↔ a ∈ Set.Ioc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' le_inv' @[to_additive] theorem inv_mem_Ioc_iff : a⁻¹ ∈ Set.Ioc c d ↔ a ∈ Set.Ico d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' lt_inv' @[to_additive] theorem inv_mem_Ioo_iff : a⁻¹ ∈ Set.Ioo c d ↔ a ∈ Set.Ioo d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' lt_inv' end OrderedCommGroup section OrderedAddCommGroup variable [AddCommGroup α] [PartialOrder α] [IsOrderedAddMonoid α] {a b c d : α} /-! `add_mem_Ixx_iff_left` -/ theorem add_mem_Icc_iff_left : a + b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c - b) (d - b) := (and_congr sub_le_iff_le_add le_sub_iff_add_le).symm theorem add_mem_Ico_iff_left : a + b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c - b) (d - b) := (and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm theorem add_mem_Ioc_iff_left : a + b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c - b) (d - b) := (and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm theorem add_mem_Ioo_iff_left : a + b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c - b) (d - b) := (and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm /-! `add_mem_Ixx_iff_right` -/ theorem add_mem_Icc_iff_right : a + b ∈ Set.Icc c d ↔ b ∈ Set.Icc (c - a) (d - a) := (and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm theorem add_mem_Ico_iff_right : a + b ∈ Set.Ico c d ↔ b ∈ Set.Ico (c - a) (d - a) := (and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm theorem add_mem_Ioc_iff_right : a + b ∈ Set.Ioc c d ↔ b ∈ Set.Ioc (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' le_sub_iff_add_le').symm theorem add_mem_Ioo_iff_right : a + b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm /-! `sub_mem_Ixx_iff_left` -/ theorem sub_mem_Icc_iff_left : a - b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c + b) (d + b) := and_congr le_sub_iff_add_le sub_le_iff_le_add theorem sub_mem_Ico_iff_left : a - b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c + b) (d + b) := and_congr le_sub_iff_add_le sub_lt_iff_lt_add theorem sub_mem_Ioc_iff_left : a - b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_le_iff_le_add theorem sub_mem_Ioo_iff_left : a - b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add /-! `sub_mem_Ixx_iff_right` -/ theorem sub_mem_Icc_iff_right : a - b ∈ Set.Icc c d ↔ b ∈ Set.Icc (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm le_sub_comm theorem sub_mem_Ico_iff_right : a - b ∈ Set.Ico c d ↔ b ∈ Set.Ioc (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm le_sub_comm theorem sub_mem_Ioc_iff_right : a - b ∈ Set.Ioc c d ↔ b ∈ Set.Ico (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm lt_sub_comm theorem sub_mem_Ioo_iff_right : a - b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm lt_sub_comm -- I think that symmetric intervals deserve attention and API: they arise all the time, -- for instance when considering metric balls in `ℝ`. theorem mem_Icc_iff_abs_le {R : Type*} [AddCommGroup R] [LinearOrder R] [IsOrderedAddMonoid R] {x y z : R} : |x - y| ≤ z ↔ y ∈ Icc (x - z) (x + z) := abs_le.trans <| and_comm.trans <| and_congr sub_le_comm neg_le_sub_iff_le_add /-! `sub_mem_Ixx_zero_right` and `sub_mem_Ixx_zero_iff_right`; this specializes the previous lemmas to the case of reflecting the interval. -/ theorem sub_mem_Icc_zero_iff_right : b - a ∈ Icc 0 b ↔ a ∈ Icc 0 b := by simp only [sub_mem_Icc_iff_right, sub_self, sub_zero] theorem sub_mem_Ico_zero_iff_right : b - a ∈ Ico 0 b ↔ a ∈ Ioc 0 b := by simp only [sub_mem_Ico_iff_right, sub_self, sub_zero] theorem sub_mem_Ioc_zero_iff_right : b - a ∈ Ioc 0 b ↔ a ∈ Ico 0 b := by simp only [sub_mem_Ioc_iff_right, sub_self, sub_zero] theorem sub_mem_Ioo_zero_iff_right : b - a ∈ Ioo 0 b ↔ a ∈ Ioo 0 b := by simp only [sub_mem_Ioo_iff_right, sub_self, sub_zero] end OrderedAddCommGroup section LinearOrderedAddCommGroup variable [AddCommGroup α] [LinearOrder α] [IsOrderedAddMonoid α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ theorem nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : Nonempty ↑(Ico x (x + dx) \ Ico y (y + dy)) := by rcases lt_or_le x y with h' | h' · use x simp [*, not_le.2 h'] · use max x (x + dy) simp [*, le_refl] end LinearOrderedAddCommGroup /-! ### Lemmas about disjointness of translates of intervals -/ open scoped Function -- required for scoped `on` notation section PairwiseDisjoint section OrderedCommGroup variable [CommGroup α] [PartialOrder α] [IsOrderedMonoid α] (a b : α) @[to_additive] theorem pairwise_disjoint_Ioc_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (a * b ^ n) (a * b ^ (n + 1))) := by simp +unfoldPartialApp only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_le hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_le hx.2.2 have i2 := hx.2.1.trans_le hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff_right hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ico_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (a * b ^ n) (a * b ^ (n + 1))) := by simp +unfoldPartialApp only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_lt hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_lt hx.2.2 have i2 := hx.2.1.trans_lt hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff_right hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ioo_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioo (a * b ^ n) (a * b ^ (n + 1))) := fun _ _ hmn => (pairwise_disjoint_Ioc_mul_zpow a b hmn).mono Ioo_subset_Ioc_self Ioo_subset_Ioc_self @[to_additive] theorem pairwise_disjoint_Ioc_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ioc_mul_zpow 1 b @[to_additive] theorem pairwise_disjoint_Ico_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ico_mul_zpow 1 b @[to_additive] theorem pairwise_disjoint_Ioo_zpow : Pairwise (Disjoint on fun n : ℤ => Ioo (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ioo_mul_zpow 1 b end OrderedCommGroup section OrderedRing variable [Ring α] [PartialOrder α] [IsOrderedRing α] (a : α) theorem pairwise_disjoint_Ioc_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ioc (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ioc_add_zsmul a (1 : α)
theorem pairwise_disjoint_Ico_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ico (a + n) (a + n + 1)) := by
Mathlib/Algebra/Order/Interval/Set/Group.lean
212
214
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro -/ import Mathlib.Data.Subtype import Mathlib.Order.Defs.LinearOrder import Mathlib.Order.Notation import Mathlib.Tactic.GCongr.Core import Mathlib.Tactic.Spread import Mathlib.Tactic.Convert import Mathlib.Tactic.Inhabit import Mathlib.Tactic.SimpRw /-! # Basic definitions about `≤` and `<` This file proves basic results about orders, provides extensive dot notation, defines useful order classes and allows to transfer order instances. ## Type synonyms * `OrderDual α` : A type synonym reversing the meaning of all inequalities, with notation `αᵒᵈ`. * `AsLinearOrder α`: A type synonym to promote `PartialOrder α` to `LinearOrder α` using `IsTotal α (≤)`. ### Transferring orders - `Order.Preimage`, `Preorder.lift`: Transfers a (pre)order on `β` to an order on `α` using a function `f : α → β`. - `PartialOrder.lift`, `LinearOrder.lift`: Transfers a partial (resp., linear) order on `β` to a partial (resp., linear) order on `α` using an injective function `f`. ### Extra class * `DenselyOrdered`: An order with no gap, i.e. for any two elements `a < b` there exists `c` such that `a < c < b`. ## Notes `≤` and `<` are highly favored over `≥` and `>` in mathlib. The reason is that we can formulate all lemmas using `≤`/`<`, and `rw` has trouble unifying `≤` and `≥`. Hence choosing one direction spares us useless duplication. This is enforced by a linter. See Note [nolint_ge] for more infos. Dot notation is particularly useful on `≤` (`LE.le`) and `<` (`LT.lt`). To that end, we provide many aliases to dot notation-less lemmas. For example, `le_trans` is aliased with `LE.le.trans` and can be used to construct `hab.trans hbc : a ≤ c` when `hab : a ≤ b`, `hbc : b ≤ c`, `lt_of_le_of_lt` is aliased as `LE.le.trans_lt` and can be used to construct `hab.trans hbc : a < c` when `hab : a ≤ b`, `hbc : b < c`. ## TODO - expand module docs - automatic construction of dual definitions / theorems ## Tags preorder, order, partial order, poset, linear order, chain -/ open Function variable {ι α β : Type*} {π : ι → Type*} /-! ### Bare relations -/ attribute [ext] LE protected lemma LE.le.ge [LE α] {x y : α} (h : x ≤ y) : y ≥ x := h protected lemma GE.ge.le [LE α] {x y : α} (h : x ≥ y) : y ≤ x := h protected lemma LT.lt.gt [LT α] {x y : α} (h : x < y) : y > x := h protected lemma GT.gt.lt [LT α] {x y : α} (h : x > y) : y < x := h /-- Given a relation `R` on `β` and a function `f : α → β`, the preimage relation on `α` is defined by `x ≤ y ↔ f x ≤ f y`. It is the unique relation on `α` making `f` a `RelEmbedding` (assuming `f` is injective). -/ @[simp] def Order.Preimage (f : α → β) (s : β → β → Prop) (x y : α) : Prop := s (f x) (f y) @[inherit_doc] infixl:80 " ⁻¹'o " => Order.Preimage /-- The preimage of a decidable order is decidable. -/ instance Order.Preimage.decidable (f : α → β) (s : β → β → Prop) [H : DecidableRel s] : DecidableRel (f ⁻¹'o s) := fun _ _ ↦ H _ _ /-! ### Preorders -/ section Preorder variable [Preorder α] {a b c d : α} theorem le_trans' : b ≤ c → a ≤ b → a ≤ c := flip le_trans theorem lt_trans' : b < c → a < b → a < c := flip lt_trans theorem lt_of_le_of_lt' : b ≤ c → a < b → a < c := flip lt_of_lt_of_le theorem lt_of_lt_of_le' : b < c → a ≤ b → a < c := flip lt_of_le_of_lt theorem le_of_le_of_eq' : b ≤ c → a = b → a ≤ c := flip le_of_eq_of_le theorem le_of_eq_of_le' : b = c → a ≤ b → a ≤ c := flip le_of_le_of_eq theorem lt_of_lt_of_eq' : b < c → a = b → a < c := flip lt_of_eq_of_lt theorem lt_of_eq_of_lt' : b = c → a < b → a < c := flip lt_of_lt_of_eq theorem not_lt_iff_not_le_or_ge : ¬a < b ↔ ¬a ≤ b ∨ b ≤ a := by rw [lt_iff_le_not_le, Classical.not_and_iff_not_or_not, Classical.not_not] -- Unnecessary brackets are here for readability lemma not_lt_iff_le_imp_le : ¬ a < b ↔ (a ≤ b → b ≤ a) := by simp [not_lt_iff_not_le_or_ge, or_iff_not_imp_left] /-- If `x = y` then `y ≤ x`. Note: this lemma uses `y ≤ x` instead of `x ≥ y`, because `le` is used almost exclusively in mathlib. -/ lemma ge_of_eq (h : a = b) : b ≤ a := le_of_eq h.symm @[simp] lemma lt_self_iff_false (x : α) : x < x ↔ False := ⟨lt_irrefl x, False.elim⟩ alias LE.le.trans := le_trans alias LE.le.trans' := le_trans' alias LT.lt.trans := lt_trans alias LT.lt.trans' := lt_trans' alias LE.le.trans_lt := lt_of_le_of_lt alias LE.le.trans_lt' := lt_of_le_of_lt' alias LT.lt.trans_le := lt_of_lt_of_le alias LT.lt.trans_le' := lt_of_lt_of_le' alias LE.le.trans_eq := le_of_le_of_eq alias LE.le.trans_eq' := le_of_le_of_eq' alias LT.lt.trans_eq := lt_of_lt_of_eq alias LT.lt.trans_eq' := lt_of_lt_of_eq' alias Eq.trans_le := le_of_eq_of_le alias Eq.trans_ge := le_of_eq_of_le' alias Eq.trans_lt := lt_of_eq_of_lt alias Eq.trans_gt := lt_of_eq_of_lt' alias LE.le.lt_of_not_le := lt_of_le_not_le alias LE.le.lt_or_eq_dec := Decidable.lt_or_eq_of_le alias LT.lt.le := le_of_lt alias LT.lt.ne := ne_of_lt alias Eq.le := le_of_eq @[inherit_doc ge_of_eq] alias Eq.ge := ge_of_eq alias LT.lt.asymm := lt_asymm alias LT.lt.not_lt := lt_asymm theorem ne_of_not_le (h : ¬a ≤ b) : a ≠ b := fun hab ↦ h (le_of_eq hab) protected lemma Eq.not_lt (hab : a = b) : ¬a < b := fun h' ↦ h'.ne hab protected lemma Eq.not_gt (hab : a = b) : ¬b < a := hab.symm.not_lt @[simp] lemma le_of_subsingleton [Subsingleton α] : a ≤ b := (Subsingleton.elim a b).le -- Making this a @[simp] lemma causes confluence problems downstream. lemma not_lt_of_subsingleton [Subsingleton α] : ¬a < b := (Subsingleton.elim a b).not_lt namespace LT.lt protected theorem false : a < a → False := lt_irrefl a theorem ne' (h : a < b) : b ≠ a := h.ne.symm end LT.lt theorem le_of_forall_le (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ le_rfl theorem le_of_forall_ge (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ le_rfl @[deprecated (since := "2025-01-30")] alias le_of_forall_le' := le_of_forall_ge theorem forall_le_iff_le : (∀ ⦃c⦄, c ≤ a → c ≤ b) ↔ a ≤ b := ⟨le_of_forall_le, fun h _ hca ↦ le_trans hca h⟩ theorem forall_le_iff_ge : (∀ ⦃c⦄, a ≤ c → b ≤ c) ↔ b ≤ a := ⟨le_of_forall_ge, fun h _ hca ↦ le_trans h hca⟩ /-- monotonicity of `≤` with respect to `→` -/ theorem le_implies_le_of_le_of_le (hca : c ≤ a) (hbd : b ≤ d) : a ≤ b → c ≤ d := fun hab ↦ (hca.trans hab).trans hbd end Preorder /-! ### Partial order -/ section PartialOrder variable [PartialOrder α] {a b : α} theorem ge_antisymm : a ≤ b → b ≤ a → b = a := flip le_antisymm theorem lt_of_le_of_ne' : a ≤ b → b ≠ a → a < b := fun h₁ h₂ ↦ lt_of_le_of_ne h₁ h₂.symm theorem Ne.lt_of_le : a ≠ b → a ≤ b → a < b := flip lt_of_le_of_ne theorem Ne.lt_of_le' : b ≠ a → a ≤ b → a < b := flip lt_of_le_of_ne' alias LE.le.antisymm := le_antisymm alias LE.le.antisymm' := ge_antisymm alias LE.le.lt_of_ne := lt_of_le_of_ne alias LE.le.lt_of_ne' := lt_of_le_of_ne' alias LE.le.lt_or_eq := lt_or_eq_of_le -- Unnecessary brackets are here for readability lemma le_imp_eq_iff_le_imp_le : (a ≤ b → b = a) ↔ (a ≤ b → b ≤ a) where mp h hab := (h hab).le mpr h hab := (h hab).antisymm hab -- Unnecessary brackets are here for readability lemma ge_imp_eq_iff_le_imp_le : (a ≤ b → a = b) ↔ (a ≤ b → b ≤ a) where mp h hab := (h hab).ge mpr h hab := hab.antisymm (h hab) namespace LE.le theorem lt_iff_ne (h : a ≤ b) : a < b ↔ a ≠ b := ⟨fun h ↦ h.ne, h.lt_of_ne⟩ theorem gt_iff_ne (h : a ≤ b) : a < b ↔ b ≠ a := ⟨fun h ↦ h.ne.symm, h.lt_of_ne'⟩ theorem not_lt_iff_eq (h : a ≤ b) : ¬a < b ↔ a = b := h.lt_iff_ne.not_left theorem not_gt_iff_eq (h : a ≤ b) : ¬a < b ↔ b = a := h.gt_iff_ne.not_left theorem le_iff_eq (h : a ≤ b) : b ≤ a ↔ b = a := ⟨fun h' ↦ h'.antisymm h, Eq.le⟩ theorem ge_iff_eq (h : a ≤ b) : b ≤ a ↔ a = b := ⟨h.antisymm, Eq.ge⟩ end LE.le -- See Note [decidable namespace] protected theorem Decidable.le_iff_eq_or_lt [DecidableLE α] : a ≤ b ↔ a = b ∨ a < b := Decidable.le_iff_lt_or_eq.trans or_comm theorem le_iff_eq_or_lt : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or_comm theorem lt_iff_le_and_ne : a < b ↔ a ≤ b ∧ a ≠ b := ⟨fun h ↦ ⟨le_of_lt h, ne_of_lt h⟩, fun ⟨h1, h2⟩ ↦ h1.lt_of_ne h2⟩ lemma eq_iff_not_lt_of_le (hab : a ≤ b) : a = b ↔ ¬ a < b := by simp [hab, lt_iff_le_and_ne] alias LE.le.eq_iff_not_lt := eq_iff_not_lt_of_le -- See Note [decidable namespace] protected theorem Decidable.eq_iff_le_not_lt [DecidableLE α] : a = b ↔ a ≤ b ∧ ¬a < b := ⟨fun h ↦ ⟨h.le, h ▸ lt_irrefl _⟩, fun ⟨h₁, h₂⟩ ↦ h₁.antisymm <| Decidable.byContradiction fun h₃ ↦ h₂ (h₁.lt_of_not_le h₃)⟩ theorem eq_iff_le_not_lt : a = b ↔ a ≤ b ∧ ¬a < b := haveI := Classical.dec Decidable.eq_iff_le_not_lt theorem eq_or_lt_of_le (h : a ≤ b) : a = b ∨ a < b := h.lt_or_eq.symm theorem eq_or_gt_of_le (h : a ≤ b) : b = a ∨ a < b := h.lt_or_eq.symm.imp Eq.symm id theorem gt_or_eq_of_le (h : a ≤ b) : a < b ∨ b = a := (eq_or_gt_of_le h).symm alias LE.le.eq_or_lt_dec := Decidable.eq_or_lt_of_le alias LE.le.eq_or_lt := eq_or_lt_of_le alias LE.le.eq_or_gt := eq_or_gt_of_le alias LE.le.gt_or_eq := gt_or_eq_of_le theorem eq_of_le_of_not_lt (hab : a ≤ b) (hba : ¬a < b) : a = b := hab.eq_or_lt.resolve_right hba theorem eq_of_ge_of_not_gt (hab : a ≤ b) (hba : ¬a < b) : b = a := (eq_of_le_of_not_lt hab hba).symm alias LE.le.eq_of_not_lt := eq_of_le_of_not_lt alias LE.le.eq_of_not_gt := eq_of_ge_of_not_gt theorem Ne.le_iff_lt (h : a ≠ b) : a ≤ b ↔ a < b := ⟨fun h' ↦ lt_of_le_of_ne h' h, fun h ↦ h.le⟩ theorem Ne.not_le_or_not_le (h : a ≠ b) : ¬a ≤ b ∨ ¬b ≤ a := not_and_or.1 <| le_antisymm_iff.not.1 h -- See Note [decidable namespace] protected theorem Decidable.ne_iff_lt_iff_le [DecidableEq α] : (a ≠ b ↔ a < b) ↔ a ≤ b := ⟨fun h ↦ Decidable.byCases le_of_eq (le_of_lt ∘ h.mp), fun h ↦ ⟨lt_of_le_of_ne h, ne_of_lt⟩⟩ @[simp] theorem ne_iff_lt_iff_le : (a ≠ b ↔ a < b) ↔ a ≤ b := haveI := Classical.dec Decidable.ne_iff_lt_iff_le lemma eq_of_forall_le_iff (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := ((H _).1 le_rfl).antisymm ((H _).2 le_rfl) lemma eq_of_forall_ge_iff (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := ((H _).2 le_rfl).antisymm ((H _).1 le_rfl) /-- To prove commutativity of a binary operation `○`, we only to check `a ○ b ≤ b ○ a` for all `a`, `b`. -/ lemma commutative_of_le {f : β → β → α} (comm : ∀ a b, f a b ≤ f b a) : ∀ a b, f a b = f b a := fun _ _ ↦ (comm _ _).antisymm <| comm _ _ /-- To prove associativity of a commutative binary operation `○`, we only to check `(a ○ b) ○ c ≤ a ○ (b ○ c)` for all `a`, `b`, `c`. -/ lemma associative_of_commutative_of_le {f : α → α → α} (comm : Std.Commutative f) (assoc : ∀ a b c, f (f a b) c ≤ f a (f b c)) : Std.Associative f where assoc a b c := le_antisymm (assoc _ _ _) <| by rw [comm.comm, comm.comm b, comm.comm _ c, comm.comm a] exact assoc .. end PartialOrder section LinearOrder variable [LinearOrder α] {a b : α} namespace LE.le lemma lt_or_le (h : a ≤ b) (c : α) : a < c ∨ c ≤ b := (lt_or_ge a c).imp id h.trans' lemma le_or_lt (h : a ≤ b) (c : α) : a ≤ c ∨ c < b := (le_or_gt a c).imp id h.trans_lt' lemma le_or_le (h : a ≤ b) (c : α) : a ≤ c ∨ c ≤ b := (h.lt_or_le c).imp le_of_lt id end LE.le namespace LT.lt lemma lt_or_lt (h : a < b) (c : α) : a < c ∨ c < b := (le_or_gt b c).imp h.trans_le id end LT.lt -- Variant of `min_def` with the branches reversed. theorem min_def' (a b : α) : min a b = if b ≤ a then b else a := by rw [min_def] rcases lt_trichotomy a b with (lt | eq | gt) · rw [if_pos lt.le, if_neg (not_le.mpr lt)] · rw [if_pos eq.le, if_pos eq.ge, eq] · rw [if_neg (not_le.mpr gt.gt), if_pos gt.le] -- Variant of `min_def` with the branches reversed. -- This is sometimes useful as it used to be the default. theorem max_def' (a b : α) : max a b = if b ≤ a then a else b := by rw [max_def] rcases lt_trichotomy a b with (lt | eq | gt) · rw [if_pos lt.le, if_neg (not_le.mpr lt)] · rw [if_pos eq.le, if_pos eq.ge, eq] · rw [if_neg (not_le.mpr gt.gt), if_pos gt.le] theorem lt_of_not_le (h : ¬b ≤ a) : a < b := ((le_total _ _).resolve_right h).lt_of_not_le h theorem lt_iff_not_le : a < b ↔ ¬b ≤ a := ⟨not_le_of_lt, lt_of_not_le⟩ theorem Ne.lt_or_lt (h : a ≠ b) : a < b ∨ b < a := lt_or_gt_of_ne h /-- A version of `ne_iff_lt_or_gt` with LHS and RHS reversed. -/ @[simp] theorem lt_or_lt_iff_ne : a < b ∨ b < a ↔ a ≠ b := ne_iff_lt_or_gt.symm theorem not_lt_iff_eq_or_lt : ¬a < b ↔ a = b ∨ b < a := not_lt.trans <| Decidable.le_iff_eq_or_lt.trans <| or_congr eq_comm Iff.rfl theorem exists_ge_of_linear (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | Or.inl h => ⟨_, h, le_rfl⟩ | Or.inr h => ⟨_, le_rfl, h⟩ lemma exists_forall_ge_and {p q : α → Prop} : (∃ i, ∀ j ≥ i, p j) → (∃ i, ∀ j ≥ i, q j) → ∃ i, ∀ j ≥ i, p j ∧ q j | ⟨a, ha⟩, ⟨b, hb⟩ => let ⟨c, hac, hbc⟩ := exists_ge_of_linear a b ⟨c, fun _d hcd ↦ ⟨ha _ <| hac.trans hcd, hb _ <| hbc.trans hcd⟩⟩ theorem le_of_forall_lt (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt fun h ↦ lt_irrefl _ (H _ h) theorem forall_lt_iff_le : (∀ ⦃c⦄, c < a → c < b) ↔ a ≤ b := ⟨le_of_forall_lt, fun h _ hca ↦ lt_of_lt_of_le hca h⟩ theorem le_of_forall_lt' (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt fun h ↦ lt_irrefl _ (H _ h) theorem forall_lt_iff_le' : (∀ ⦃c⦄, a < c → b < c) ↔ b ≤ a := ⟨le_of_forall_lt', fun h _ hac ↦ lt_of_le_of_lt h hac⟩ theorem eq_of_forall_lt_iff (h : ∀ c, c < a ↔ c < b) : a = b := (le_of_forall_lt fun _ ↦ (h _).1).antisymm <| le_of_forall_lt fun _ ↦ (h _).2 theorem eq_of_forall_gt_iff (h : ∀ c, a < c ↔ b < c) : a = b := (le_of_forall_lt' fun _ ↦ (h _).2).antisymm <| le_of_forall_lt' fun _ ↦ (h _).1 section ltByCases variable {P : Sort*} {x y : α} @[simp] lemma ltByCases_lt (h : x < y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₁ h := dif_pos h @[simp] lemma ltByCases_gt (h : y < x) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₃ h := (dif_neg h.not_lt).trans (dif_pos h) @[simp] lemma ltByCases_eq (h : x = y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₂ h := (dif_neg h.not_lt).trans (dif_neg h.not_gt) lemma ltByCases_not_lt (h : ¬ x < y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ y < x → x = y := fun h' => (le_antisymm (le_of_not_gt h') (le_of_not_gt h))) : ltByCases x y h₁ h₂ h₃ = if h' : y < x then h₃ h' else h₂ (p h') := dif_neg h lemma ltByCases_not_gt (h : ¬ y < x) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ x < y → x = y := fun h' => (le_antisymm (le_of_not_gt h) (le_of_not_gt h'))) : ltByCases x y h₁ h₂ h₃ = if h' : x < y then h₁ h' else h₂ (p h') := dite_congr rfl (fun _ => rfl) (fun _ => dif_neg h) lemma ltByCases_ne (h : x ≠ y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ x < y → y < x := fun h' => h.lt_or_lt.resolve_left h') : ltByCases x y h₁ h₂ h₃ = if h' : x < y then h₁ h' else h₃ (p h') := dite_congr rfl (fun _ => rfl) (fun _ => dif_pos _) lemma ltByCases_comm {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : y = x → x = y := fun h' => h'.symm) : ltByCases x y h₁ h₂ h₃ = ltByCases y x h₃ (h₂ ∘ p) h₁ := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · rw [ltByCases_lt h, ltByCases_gt h] · rw [ltByCases_eq h, ltByCases_eq h.symm, comp_apply] · rw [ltByCases_lt h, ltByCases_gt h] lemma eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt {x' y' : α} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) : x = y ↔ x' = y' := by simp_rw [eq_iff_le_not_lt, ← not_lt, ltc, gtc] lemma ltByCases_rec {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : P) (hlt : (h : x < y) → h₁ h = p) (heq : (h : x = y) → h₂ h = p) (hgt : (h : y < x) → h₃ h = p) : ltByCases x y h₁ h₂ h₃ = p := ltByCases x y (fun h => ltByCases_lt h ▸ hlt h) (fun h => ltByCases_eq h ▸ heq h) (fun h => ltByCases_gt h ▸ hgt h) lemma ltByCases_eq_iff {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} {p : P} : ltByCases x y h₁ h₂ h₃ = p ↔ (∃ h, h₁ h = p) ∨ (∃ h, h₂ h = p) ∨ (∃ h, h₃ h = p) := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · simp only [ltByCases_lt, exists_prop_of_true, h, h.not_lt, not_false_eq_true, exists_prop_of_false, or_false, h.ne] · simp only [h, lt_self_iff_false, ltByCases_eq, not_false_eq_true, exists_prop_of_false, exists_prop_of_true, or_false, false_or] · simp only [ltByCases_gt, exists_prop_of_true, h, h.not_lt, not_false_eq_true, exists_prop_of_false, false_or, h.ne'] lemma ltByCases_congr {x' y' : α} {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} {h₁' : x' < y' → P} {h₂' : x' = y' → P} {h₃' : y' < x' → P} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) (hh'₁ : ∀ (h : x' < y'), h₁ (ltc.mpr h) = h₁' h) (hh'₂ : ∀ (h : x' = y'), h₂ ((eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt ltc gtc).mpr h) = h₂' h) (hh'₃ : ∀ (h : y' < x'), h₃ (gtc.mpr h) = h₃' h) : ltByCases x y h₁ h₂ h₃ = ltByCases x' y' h₁' h₂' h₃' := by refine ltByCases_rec _ (fun h => ?_) (fun h => ?_) (fun h => ?_) · rw [ltByCases_lt (ltc.mp h), hh'₁] · rw [eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt ltc gtc] at h rw [ltByCases_eq h, hh'₂] · rw [ltByCases_gt (gtc.mp h), hh'₃] /-- Perform a case-split on the ordering of `x` and `y` in a decidable linear order, non-dependently. -/ abbrev ltTrichotomy (x y : α) (p q r : P) := ltByCases x y (fun _ => p) (fun _ => q) (fun _ => r) variable {p q r s : P} @[simp] lemma ltTrichotomy_lt (h : x < y) : ltTrichotomy x y p q r = p := ltByCases_lt h @[simp] lemma ltTrichotomy_gt (h : y < x) : ltTrichotomy x y p q r = r := ltByCases_gt h @[simp] lemma ltTrichotomy_eq (h : x = y) : ltTrichotomy x y p q r = q := ltByCases_eq h lemma ltTrichotomy_not_lt (h : ¬ x < y) : ltTrichotomy x y p q r = if y < x then r else q := ltByCases_not_lt h lemma ltTrichotomy_not_gt (h : ¬ y < x) : ltTrichotomy x y p q r = if x < y then p else q := ltByCases_not_gt h lemma ltTrichotomy_ne (h : x ≠ y) : ltTrichotomy x y p q r = if x < y then p else r := ltByCases_ne h lemma ltTrichotomy_comm : ltTrichotomy x y p q r = ltTrichotomy y x r q p := ltByCases_comm lemma ltTrichotomy_self {p : P} : ltTrichotomy x y p p p = p := ltByCases_rec p (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) lemma ltTrichotomy_eq_iff : ltTrichotomy x y p q r = s ↔ (x < y ∧ p = s) ∨ (x = y ∧ q = s) ∨ (y < x ∧ r = s) := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · simp only [ltTrichotomy_lt, false_and, true_and, or_false, h, h.not_lt, h.ne] · simp only [ltTrichotomy_eq, false_and, true_and, or_false, false_or, h, lt_irrefl] · simp only [ltTrichotomy_gt, false_and, true_and, false_or, h, h.not_lt, h.ne'] lemma ltTrichotomy_congr {x' y' : α} {p' q' r' : P} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) (hh'₁ : x' < y' → p = p') (hh'₂ : x' = y' → q = q') (hh'₃ : y' < x' → r = r') : ltTrichotomy x y p q r = ltTrichotomy x' y' p' q' r' := ltByCases_congr ltc gtc hh'₁ hh'₂ hh'₃ end ltByCases /-! #### `min`/`max` recursors -/ section MinMaxRec variable {p : α → Prop} lemma min_rec (ha : a ≤ b → p a) (hb : b ≤ a → p b) : p (min a b) := by obtain hab | hba := le_total a b <;> simp [min_eq_left, min_eq_right, *] lemma max_rec (ha : b ≤ a → p a) (hb : a ≤ b → p b) : p (max a b) := by obtain hab | hba := le_total a b <;> simp [max_eq_left, max_eq_right, *] lemma min_rec' (p : α → Prop) (ha : p a) (hb : p b) : p (min a b) := min_rec (fun _ ↦ ha) fun _ ↦ hb lemma max_rec' (p : α → Prop) (ha : p a) (hb : p b) : p (max a b) := max_rec (fun _ ↦ ha) fun _ ↦ hb lemma min_def_lt (a b : α) : min a b = if a < b then a else b := by rw [min_comm, min_def, ← ite_not]; simp only [not_le] lemma max_def_lt (a b : α) : max a b = if a < b then b else a := by rw [max_comm, max_def, ← ite_not]; simp only [not_le] end MinMaxRec end LinearOrder /-! ### Implications -/ lemma lt_imp_lt_of_le_imp_le {β} [LinearOrder α] [Preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_le fun h' ↦ (H h').not_lt h lemma le_imp_le_iff_lt_imp_lt {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} : a ≤ b → c ≤ d ↔ d < c → b < a := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [Preorder α] [Preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans <| (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans <| (not_congr H).trans <| not_le lemma le_iff_le_iff_lt_iff_lt {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, fun H ↦ not_lt.symm.trans <| (not_congr H).trans <| not_lt⟩ /-- A symmetric relation implies two values are equal, when it implies they're less-equal. -/ lemma rel_imp_eq_of_rel_imp_le [PartialOrder β] (r : α → α → Prop) [IsSymm α r] {f : α → β} (h : ∀ a b, r a b → f a ≤ f b) {a b : α} : r a b → f a = f b := fun hab ↦ le_antisymm (h a b hab) (h b a <| symm hab) /-! ### Extensionality lemmas -/ @[ext] lemma Preorder.toLE_injective : Function.Injective (@Preorder.toLE α) := fun | { lt := A_lt, lt_iff_le_not_le := A_iff, .. }, { lt := B_lt, lt_iff_le_not_le := B_iff, .. } => by rintro ⟨⟩ have : A_lt = B_lt := by funext a b rw [A_iff, B_iff] cases this congr @[ext] lemma PartialOrder.toPreorder_injective : Function.Injective (@PartialOrder.toPreorder α) := by rintro ⟨⟩ ⟨⟩ ⟨⟩; congr @[ext] lemma LinearOrder.toPartialOrder_injective : Function.Injective (@LinearOrder.toPartialOrder α) := fun | { le := A_le, lt := A_lt, toDecidableLE := A_decidableLE, toDecidableEq := A_decidableEq, toDecidableLT := A_decidableLT min := A_min, max := A_max, min_def := A_min_def, max_def := A_max_def, compare := A_compare, compare_eq_compareOfLessAndEq := A_compare_canonical, .. }, { le := B_le, lt := B_lt, toDecidableLE := B_decidableLE, toDecidableEq := B_decidableEq, toDecidableLT := B_decidableLT min := B_min, max := B_max, min_def := B_min_def, max_def := B_max_def, compare := B_compare, compare_eq_compareOfLessAndEq := B_compare_canonical, .. } => by rintro ⟨⟩ obtain rfl : A_decidableLE = B_decidableLE := Subsingleton.elim _ _ obtain rfl : A_decidableEq = B_decidableEq := Subsingleton.elim _ _ obtain rfl : A_decidableLT = B_decidableLT := Subsingleton.elim _ _ have : A_min = B_min := by funext a b exact (A_min_def _ _).trans (B_min_def _ _).symm cases this have : A_max = B_max := by funext a b exact (A_max_def _ _).trans (B_max_def _ _).symm cases this have : A_compare = B_compare := by funext a b exact (A_compare_canonical _ _).trans (B_compare_canonical _ _).symm congr lemma Preorder.ext {A B : Preorder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma PartialOrder.ext {A B : PartialOrder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma PartialOrder.ext_lt {A B : PartialOrder α} (H : ∀ x y : α, (haveI := A; x < y) ↔ x < y) : A = B := by ext x y; rw [le_iff_lt_or_eq, @le_iff_lt_or_eq _ A, H] lemma LinearOrder.ext {A B : LinearOrder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma LinearOrder.ext_lt {A B : LinearOrder α} (H : ∀ x y : α, (haveI := A; x < y) ↔ x < y) : A = B := LinearOrder.toPartialOrder_injective (PartialOrder.ext_lt H) /-! ### Order dual -/ /-- Type synonym to equip a type with the dual order: `≤` means `≥` and `<` means `>`. `αᵒᵈ` is notation for `OrderDual α`. -/ def OrderDual (α : Type*) : Type _ := α @[inherit_doc] notation:max α "ᵒᵈ" => OrderDual α namespace OrderDual instance (α : Type*) [h : Nonempty α] : Nonempty αᵒᵈ := h instance (α : Type*) [h : Subsingleton α] : Subsingleton αᵒᵈ := h instance (α : Type*) [LE α] : LE αᵒᵈ := ⟨fun x y : α ↦ y ≤ x⟩ instance (α : Type*) [LT α] : LT αᵒᵈ := ⟨fun x y : α ↦ y < x⟩ instance instOrd (α : Type*) [Ord α] : Ord αᵒᵈ where compare := fun (a b : α) ↦ compare b a instance instSup (α : Type*) [Min α] : Max αᵒᵈ := ⟨((· ⊓ ·) : α → α → α)⟩ instance instInf (α : Type*) [Max α] : Min αᵒᵈ := ⟨((· ⊔ ·) : α → α → α)⟩ instance instPreorder (α : Type*) [Preorder α] : Preorder αᵒᵈ where le_refl := fun _ ↦ le_refl _ le_trans := fun _ _ _ hab hbc ↦ hbc.trans hab lt_iff_le_not_le := fun _ _ ↦ lt_iff_le_not_le instance instPartialOrder (α : Type*) [PartialOrder α] : PartialOrder αᵒᵈ where __ := inferInstanceAs (Preorder αᵒᵈ) le_antisymm := fun a b hab hba ↦ @le_antisymm α _ a b hba hab instance instLinearOrder (α : Type*) [LinearOrder α] : LinearOrder αᵒᵈ where __ := inferInstanceAs (PartialOrder αᵒᵈ) __ := inferInstanceAs (Ord αᵒᵈ) le_total := fun a b : α ↦ le_total b a max := fun a b ↦ (min a b : α) min := fun a b ↦ (max a b : α) min_def := fun a b ↦ show (max .. : α) = _ by rw [max_comm, max_def]; rfl max_def := fun a b ↦ show (min .. : α) = _ by rw [min_comm, min_def]; rfl toDecidableLE := (inferInstance : DecidableRel (fun a b : α ↦ b ≤ a)) toDecidableLT := (inferInstance : DecidableRel (fun a b : α ↦ b < a)) toDecidableEq := (inferInstance : DecidableEq α) compare_eq_compareOfLessAndEq a b := by simp only [compare, LinearOrder.compare_eq_compareOfLessAndEq, compareOfLessAndEq, eq_comm] rfl /-- The opposite linear order to a given linear order -/ def _root_.LinearOrder.swap (α : Type*) (_ : LinearOrder α) : LinearOrder α := inferInstanceAs <| LinearOrder (OrderDual α) instance : ∀ [Inhabited α], Inhabited αᵒᵈ := fun [x : Inhabited α] => x theorem Ord.dual_dual (α : Type*) [H : Ord α] : OrderDual.instOrd αᵒᵈ = H := rfl theorem Preorder.dual_dual (α : Type*) [H : Preorder α] : OrderDual.instPreorder αᵒᵈ = H := rfl theorem instPartialOrder.dual_dual (α : Type*) [H : PartialOrder α] : OrderDual.instPartialOrder αᵒᵈ = H := rfl theorem instLinearOrder.dual_dual (α : Type*) [H : LinearOrder α] : OrderDual.instLinearOrder αᵒᵈ = H := rfl end OrderDual /-! ### `HasCompl` -/ instance Prop.hasCompl : HasCompl Prop := ⟨Not⟩ instance Pi.hasCompl [∀ i, HasCompl (π i)] : HasCompl (∀ i, π i) := ⟨fun x i ↦ (x i)ᶜ⟩ theorem Pi.compl_def [∀ i, HasCompl (π i)] (x : ∀ i, π i) : xᶜ = fun i ↦ (x i)ᶜ := rfl @[simp] theorem Pi.compl_apply [∀ i, HasCompl (π i)] (x : ∀ i, π i) (i : ι) : xᶜ i = (x i)ᶜ := rfl instance IsIrrefl.compl (r) [IsIrrefl α r] : IsRefl α rᶜ := ⟨@irrefl α r _⟩ instance IsRefl.compl (r) [IsRefl α r] : IsIrrefl α rᶜ := ⟨fun a ↦ not_not_intro (refl a)⟩ theorem compl_lt [LinearOrder α] : (· < · : α → α → _)ᶜ = (· ≥ ·) := by ext; simp [compl] theorem compl_le [LinearOrder α] : (· ≤ · : α → α → _)ᶜ = (· > ·) := by ext; simp [compl] theorem compl_gt [LinearOrder α] : (· > · : α → α → _)ᶜ = (· ≤ ·) := by ext; simp [compl] theorem compl_ge [LinearOrder α] : (· ≥ · : α → α → _)ᶜ = (· < ·) := by ext; simp [compl] instance Ne.instIsEquiv_compl : IsEquiv α (· ≠ ·)ᶜ := by convert eq_isEquiv α simp [compl] /-! ### Order instances on the function space -/ instance Pi.hasLe [∀ i, LE (π i)] : LE (∀ i, π i) where le x y := ∀ i, x i ≤ y i theorem Pi.le_def [∀ i, LE (π i)] {x y : ∀ i, π i} : x ≤ y ↔ ∀ i, x i ≤ y i := Iff.rfl instance Pi.preorder [∀ i, Preorder (π i)] : Preorder (∀ i, π i) where __ := inferInstanceAs (LE (∀ i, π i)) le_refl := fun a i ↦ le_refl (a i) le_trans := fun _ _ _ h₁ h₂ i ↦ le_trans (h₁ i) (h₂ i) theorem Pi.lt_def [∀ i, Preorder (π i)] {x y : ∀ i, π i} : x < y ↔ x ≤ y ∧ ∃ i, x i < y i := by simp +contextual [lt_iff_le_not_le, Pi.le_def] instance Pi.partialOrder [∀ i, PartialOrder (π i)] : PartialOrder (∀ i, π i) where __ := Pi.preorder le_antisymm := fun _ _ h1 h2 ↦ funext fun b ↦ (h1 b).antisymm (h2 b) namespace Sum variable {α₁ α₂ : Type*} [LE β] @[simp] lemma elim_le_elim_iff {u₁ v₁ : α₁ → β} {u₂ v₂ : α₂ → β} : Sum.elim u₁ u₂ ≤ Sum.elim v₁ v₂ ↔ u₁ ≤ v₁ ∧ u₂ ≤ v₂ := Sum.forall lemma const_le_elim_iff {b : β} {v₁ : α₁ → β} {v₂ : α₂ → β} : Function.const _ b ≤ Sum.elim v₁ v₂ ↔ Function.const _ b ≤ v₁ ∧ Function.const _ b ≤ v₂ := elim_const_const b ▸ elim_le_elim_iff .. lemma elim_le_const_iff {b : β} {u₁ : α₁ → β} {u₂ : α₂ → β} : Sum.elim u₁ u₂ ≤ Function.const _ b ↔ u₁ ≤ Function.const _ b ∧ u₂ ≤ Function.const _ b := elim_const_const b ▸ elim_le_elim_iff .. end Sum section Pi /-- A function `a` is strongly less than a function `b` if `a i < b i` for all `i`. -/ def StrongLT [∀ i, LT (π i)] (a b : ∀ i, π i) : Prop := ∀ i, a i < b i @[inherit_doc] local infixl:50 " ≺ " => StrongLT variable [∀ i, Preorder (π i)] {a b c : ∀ i, π i} theorem le_of_strongLT (h : a ≺ b) : a ≤ b := fun _ ↦ (h _).le theorem lt_of_strongLT [Nonempty ι] (h : a ≺ b) : a < b := by inhabit ι exact Pi.lt_def.2 ⟨le_of_strongLT h, default, h _⟩ theorem strongLT_of_strongLT_of_le (hab : a ≺ b) (hbc : b ≤ c) : a ≺ c := fun _ ↦ (hab _).trans_le <| hbc _ theorem strongLT_of_le_of_strongLT (hab : a ≤ b) (hbc : b ≺ c) : a ≺ c := fun _ ↦ (hab _).trans_lt <| hbc _ alias StrongLT.le := le_of_strongLT alias StrongLT.lt := lt_of_strongLT alias StrongLT.trans_le := strongLT_of_strongLT_of_le alias LE.le.trans_strongLT := strongLT_of_le_of_strongLT end Pi section Function variable [DecidableEq ι] [∀ i, Preorder (π i)] {x y : ∀ i, π i} {i : ι} {a b : π i} theorem le_update_iff : x ≤ Function.update y i a ↔ x i ≤ a ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := Function.forall_update_iff _ fun j z ↦ x j ≤ z theorem update_le_iff : Function.update x i a ≤ y ↔ a ≤ y i ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := Function.forall_update_iff _ fun j z ↦ z ≤ y j theorem update_le_update_iff : Function.update x i a ≤ Function.update y i b ↔ a ≤ b ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := by simp +contextual [update_le_iff] @[simp] theorem update_le_update_iff' : update x i a ≤ update x i b ↔ a ≤ b := by simp [update_le_update_iff] @[simp] theorem update_lt_update_iff : update x i a < update x i b ↔ a < b := lt_iff_lt_of_le_iff_le' update_le_update_iff' update_le_update_iff' @[simp] theorem le_update_self_iff : x ≤ update x i a ↔ x i ≤ a := by simp [le_update_iff] @[simp] theorem update_le_self_iff : update x i a ≤ x ↔ a ≤ x i := by simp [update_le_iff] @[simp] theorem lt_update_self_iff : x < update x i a ↔ x i < a := by simp [lt_iff_le_not_le] @[simp] theorem update_lt_self_iff : update x i a < x ↔ a < x i := by simp [lt_iff_le_not_le] end Function instance Pi.sdiff [∀ i, SDiff (π i)] : SDiff (∀ i, π i) := ⟨fun x y i ↦ x i \ y i⟩ theorem Pi.sdiff_def [∀ i, SDiff (π i)] (x y : ∀ i, π i) : x \ y = fun i ↦ x i \ y i := rfl @[simp] theorem Pi.sdiff_apply [∀ i, SDiff (π i)] (x y : ∀ i, π i) (i : ι) : (x \ y) i = x i \ y i := rfl namespace Function variable [Preorder α] [Nonempty β] {a b : α} @[simp] theorem const_le_const : const β a ≤ const β b ↔ a ≤ b := by simp [Pi.le_def] @[simp] theorem const_lt_const : const β a < const β b ↔ a < b := by simpa [Pi.lt_def] using le_of_lt end Function /-! ### Lifts of order instances -/ /-- Transfer a `Preorder` on `β` to a `Preorder` on `α` using a function `f : α → β`. See note [reducible non-instances]. -/ abbrev Preorder.lift [Preorder β] (f : α → β) : Preorder α where le x y := f x ≤ f y le_refl _ := le_rfl le_trans _ _ _ := _root_.le_trans lt x y := f x < f y lt_iff_le_not_le _ _ := _root_.lt_iff_le_not_le /-- Transfer a `PartialOrder` on `β` to a `PartialOrder` on `α` using an injective function `f : α → β`. See note [reducible non-instances]. -/ abbrev PartialOrder.lift [PartialOrder β] (f : α → β) (inj : Injective f) : PartialOrder α := { Preorder.lift f with le_antisymm := fun _ _ h₁ h₂ ↦ inj (h₁.antisymm h₂) } theorem compare_of_injective_eq_compareOfLessAndEq (a b : α) [LinearOrder β] [DecidableEq α] (f : α → β) (inj : Injective f) [Decidable (LT.lt (self := PartialOrder.lift f inj |>.toLT) a b)] : compare (f a) (f b) = @compareOfLessAndEq _ a b (PartialOrder.lift f inj |>.toLT) _ _ := by have h := LinearOrder.compare_eq_compareOfLessAndEq (f a) (f b) simp only [h, compareOfLessAndEq] split_ifs <;> try (first | rfl | contradiction) · have : ¬ f a = f b := by rename_i h; exact inj.ne h contradiction · have : f a = f b := by rename_i h; exact congrArg f h contradiction /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version takes `[Max α]` and `[Min α]` as arguments, then uses them for `max` and `min` fields. See `LinearOrder.lift'` for a version that autogenerates `min` and `max` fields, and `LinearOrder.liftWithOrd` for one that does not auto-generate `compare` fields. See note [reducible non-instances]. -/ abbrev LinearOrder.lift [LinearOrder β] [Max α] [Min α] (f : α → β) (inj : Injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrder α := letI instOrdα : Ord α := ⟨fun a b ↦ compare (f a) (f b)⟩ letI decidableLE := fun x y ↦ (inferInstance : Decidable (f x ≤ f y)) letI decidableLT := fun x y ↦ (inferInstance : Decidable (f x < f y)) letI decidableEq := fun x y ↦ decidable_of_iff (f x = f y) inj.eq_iff { PartialOrder.lift f inj, instOrdα with le_total := fun x y ↦ le_total (f x) (f y) toDecidableLE := decidableLE toDecidableLT := decidableLT toDecidableEq := decidableEq min := (· ⊓ ·) max := (· ⊔ ·) min_def := by intros x y apply inj rw [apply_ite f] exact (hinf _ _).trans (min_def _ _) max_def := by intros x y apply inj rw [apply_ite f] exact (hsup _ _).trans (max_def _ _) compare_eq_compareOfLessAndEq := fun a b ↦ compare_of_injective_eq_compareOfLessAndEq a b f inj } /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version autogenerates `min` and `max` fields. See `LinearOrder.lift` for a version that takes `[Max α]` and `[Min α]`, then uses them as `max` and `min`. See `LinearOrder.liftWithOrd'` for a version which does not auto-generate `compare` fields. See note [reducible non-instances]. -/ abbrev LinearOrder.lift' [LinearOrder β] (f : α → β) (inj : Injective f) : LinearOrder α := @LinearOrder.lift α β _ ⟨fun x y ↦ if f x ≤ f y then y else x⟩ ⟨fun x y ↦ if f x ≤ f y then x else y⟩ f inj (fun _ _ ↦ (apply_ite f _ _ _).trans (max_def _ _).symm) fun _ _ ↦ (apply_ite f _ _ _).trans (min_def _ _).symm /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version takes `[Max α]` and `[Min α]` as arguments, then uses them for `max` and `min` fields. It also takes `[Ord α]` as an argument and uses them for `compare` fields. See `LinearOrder.lift` for a version that autogenerates `compare` fields, and `LinearOrder.liftWithOrd'` for one that auto-generates `min` and `max` fields. fields. See note [reducible non-instances]. -/ abbrev LinearOrder.liftWithOrd [LinearOrder β] [Max α] [Min α] [Ord α] (f : α → β) (inj : Injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) (compare_f : ∀ a b : α, compare a b = compare (f a) (f b)) : LinearOrder α := letI decidableLE := fun x y ↦ (inferInstance : Decidable (f x ≤ f y)) letI decidableLT := fun x y ↦ (inferInstance : Decidable (f x < f y)) letI decidableEq := fun x y ↦ decidable_of_iff (f x = f y) inj.eq_iff { PartialOrder.lift f inj with le_total := fun x y ↦ le_total (f x) (f y) toDecidableLE := decidableLE toDecidableLT := decidableLT toDecidableEq := decidableEq min := (· ⊓ ·) max := (· ⊔ ·) min_def := by intros x y apply inj rw [apply_ite f] exact (hinf _ _).trans (min_def _ _) max_def := by intros x y apply inj rw [apply_ite f] exact (hsup _ _).trans (max_def _ _) compare_eq_compareOfLessAndEq := fun a b ↦ (compare_f a b).trans <| compare_of_injective_eq_compareOfLessAndEq a b f inj } /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version auto-generates `min` and `max` fields. It also takes `[Ord α]` as an argument and uses them for `compare` fields. See `LinearOrder.lift` for a version that autogenerates `compare` fields, and `LinearOrder.liftWithOrd` for one that doesn't auto-generate `min` and `max` fields. fields. See note [reducible non-instances]. -/ abbrev LinearOrder.liftWithOrd' [LinearOrder β] [Ord α] (f : α → β) (inj : Injective f) (compare_f : ∀ a b : α, compare a b = compare (f a) (f b)) : LinearOrder α := @LinearOrder.liftWithOrd α β _ ⟨fun x y ↦ if f x ≤ f y then y else x⟩ ⟨fun x y ↦ if f x ≤ f y then x else y⟩ _ f inj (fun _ _ ↦ (apply_ite f _ _ _).trans (max_def _ _).symm) (fun _ _ ↦ (apply_ite f _ _ _).trans (min_def _ _).symm) compare_f
/-! ### Subtype of an order -/
Mathlib/Order/Basic.lean
991
992
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Anne Baanen -/ import Mathlib.Data.Matrix.Basic import Mathlib.Data.Matrix.Block import Mathlib.Data.Matrix.Notation import Mathlib.Data.Matrix.RowCol import Mathlib.GroupTheory.GroupAction.Ring import Mathlib.GroupTheory.Perm.Fin import Mathlib.LinearAlgebra.Alternating.Basic import Mathlib.LinearAlgebra.Matrix.SemiringInverse /-! # Determinant of a matrix This file defines the determinant of a matrix, `Matrix.det`, and its essential properties. ## Main definitions - `Matrix.det`: the determinant of a square matrix, as a sum over permutations - `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix ## Main results - `det_mul`: the determinant of `A * B` is the product of determinants - `det_zero_of_row_eq`: the determinant is zero if there is a repeated row - `det_block_diagonal`: the determinant of a block diagonal matrix is a product of the blocks' determinants ## Implementation notes It is possible to configure `simp` to compute determinants. See the file `MathlibTest/matrix.lean` for some examples. -/ universe u v w z open Equiv Equiv.Perm Finset Function namespace Matrix variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m] variable {R : Type v} [CommRing R] local notation "ε " σ:arg => ((sign σ : ℤ) : R) /-- `det` is an `AlternatingMap` in the rows of the matrix. -/ def detRowAlternating : (n → R) [⋀^n]→ₗ[R] R := MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj) /-- The determinant of a matrix given by the Leibniz formula. -/ abbrev det (M : Matrix n n R) : R := detRowAlternating M theorem det_apply (M : Matrix n n R) : M.det = ∑ σ : Perm n, Equiv.Perm.sign σ • ∏ i, M (σ i) i := MultilinearMap.alternatization_apply _ M -- This is what the old definition was. We use it to avoid having to change the old proofs below theorem det_apply' (M : Matrix n n R) : M.det = ∑ σ : Perm n, ε σ * ∏ i, M (σ i) i := by simp [det_apply, Units.smul_def] theorem det_eq_detp_sub_detp (M : Matrix n n R) : M.det = M.detp 1 - M.detp (-1) := by rw [det_apply, ← Equiv.sum_comp (Equiv.inv (Perm n)), ← ofSign_disjUnion, sum_disjUnion] simp_rw [inv_apply, sign_inv, sub_eq_add_neg, detp, ← sum_neg_distrib] refine congr_arg₂ (· + ·) (sum_congr rfl fun σ hσ ↦ ?_) (sum_congr rfl fun σ hσ ↦ ?_) <;> rw [mem_ofSign.mp hσ, ← Equiv.prod_comp σ] <;> simp @[simp] theorem det_diagonal {d : n → R} : det (diagonal d) = ∏ i, d i := by rw [det_apply'] refine (Finset.sum_eq_single 1 ?_ ?_).trans ?_ · rintro σ - h2 obtain ⟨x, h3⟩ := not_forall.1 (mt Equiv.ext h2) convert mul_zero (ε σ) apply Finset.prod_eq_zero (mem_univ x) exact if_neg h3 · simp · simp theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 := (detRowAlternating : (n → R) [⋀^n]→ₗ[R] R).map_zero @[simp] theorem det_one : det (1 : Matrix n n R) = 1 := by rw [← diagonal_one]; simp [-diagonal_one] theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply] @[simp] theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R → R) = Function.const _ 1 := by ext exact det_isEmpty theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 := haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h det_isEmpty /-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element. Although `Unique` implies `DecidableEq` and `Fintype`, the instances might not be syntactically equal. Thus, we need to fill in the args explicitly. -/ @[simp] theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) : det A = A default default := by simp [det_apply, univ_unique] theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) : det A = A k k := by have := uniqueOfSubsingleton k convert det_unique A theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) : det A = A k k := haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le det_eq_elem_of_subsingleton _ _ theorem det_mul_aux {M N : Matrix n n R} {p : n → n} (H : ¬Bijective p) : (∑ σ : Perm n, ε σ * ∏ x, M (σ x) (p x) * N (p x) x) = 0 := by obtain ⟨i, j, hpij, hij⟩ : ∃ i j, p i = p j ∧ i ≠ j := by rw [← Finite.injective_iff_bijective, Injective] at H push_neg at H exact H exact sum_involution (fun σ _ => σ * Equiv.swap i j) (fun σ _ => by have : (∏ x, M (σ x) (p x)) = ∏ x, M ((σ * Equiv.swap i j) x) (p x) := Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij]) simp [this, sign_swap hij, -sign_swap', prod_mul_distrib]) (fun σ _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun σ _ => mul_swap_involutive i j σ @[simp] theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N := calc det (M * N) = ∑ p : n → n, ∑ σ : Perm n, ε σ * ∏ i, M (σ i) (p i) * N (p i) i := by simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ] rw [Finset.sum_comm] _ = ∑ p : n → n with Bijective p, ∑ σ : Perm n, ε σ * ∏ i, M (σ i) (p i) * N (p i) i := by refine (sum_subset (filter_subset _ _) fun f _ hbij ↦ det_mul_aux ?_).symm simpa only [true_and, mem_filter, mem_univ] using hbij _ = ∑ τ : Perm n, ∑ σ : Perm n, ε σ * ∏ i, M (σ i) (τ i) * N (τ i) i := sum_bij (fun p h ↦ Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ ↦ mem_univ _) (fun _ _ _ _ h ↦ by injection h) (fun b _ ↦ ⟨b, mem_filter.2 ⟨mem_univ _, b.bijective⟩, coe_fn_injective rfl⟩) fun _ _ ↦ rfl _ = ∑ σ : Perm n, ∑ τ : Perm n, (∏ i, N (σ i) i) * ε τ * ∏ j, M (τ j) (σ j) := by simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc] _ = ∑ σ : Perm n, ∑ τ : Perm n, (∏ i, N (σ i) i) * (ε σ * ε τ) * ∏ i, M (τ i) i := (sum_congr rfl fun σ _ => Fintype.sum_equiv (Equiv.mulRight σ⁻¹) _ _ fun τ => by have : (∏ j, M (τ j) (σ j)) = ∏ j, M ((τ * σ⁻¹) j) j := by rw [← (σ⁻¹ : _ ≃ _).prod_comp] simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply] have h : ε σ * ε (τ * σ⁻¹) = ε τ := calc ε σ * ε (τ * σ⁻¹) = ε (τ * σ⁻¹ * σ) := by rw [mul_comm, sign_mul (τ * σ⁻¹)] simp only [Int.cast_mul, Units.val_mul] _ = ε τ := by simp only [inv_mul_cancel_right] simp_rw [Equiv.coe_mulRight, h] simp only [this]) _ = det M * det N := by simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc] /-- The determinant of a matrix, as a monoid homomorphism. -/ def detMonoidHom : Matrix n n R →* R where toFun := det map_one' := det_one map_mul' := det_mul @[simp] theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R → R) = det := rfl /-- On square matrices, `mul_comm` applies under `det`. -/ theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by rw [det_mul, det_mul, mul_comm] /-- On square matrices, `mul_left_comm` applies under `det`. -/ theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by rw [← Matrix.mul_assoc, ← Matrix.mul_assoc, det_mul, det_mul_comm M N, ← det_mul] /-- On square matrices, `mul_right_comm` applies under `det`. -/ theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, ← det_mul] -- TODO(https://github.com/leanprover-community/mathlib4/issues/6607): fix elaboration so `val` isn't needed theorem det_units_conj (M : (Matrix m m R)ˣ) (N : Matrix m m R) : det (M.val * N * M⁻¹.val) = det N := by rw [det_mul_right_comm, Units.mul_inv, one_mul] -- TODO(https://github.com/leanprover-community/mathlib4/issues/6607): fix elaboration so `val` isn't needed theorem det_units_conj' (M : (Matrix m m R)ˣ) (N : Matrix m m R) : det (M⁻¹.val * N * ↑M.val) = det N := det_units_conj M⁻¹ N /-- Transposing a matrix preserves the determinant. -/ @[simp] theorem det_transpose (M : Matrix n n R) : Mᵀ.det = M.det := by rw [det_apply', det_apply'] refine Fintype.sum_bijective _ inv_involutive.bijective _ _ ?_ intro σ rw [sign_inv] congr 1 apply Fintype.prod_equiv σ simp
/-- Permuting the columns changes the sign of the determinant. -/ theorem det_permute (σ : Perm n) (M : Matrix n n R) : (M.submatrix σ id).det = Perm.sign σ * M.det :=
Mathlib/LinearAlgebra/Matrix/Determinant/Basic.lean
209
211
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Alex Kontorovich -/ import Mathlib.Data.Set.Piecewise import Mathlib.Order.Filter.Tendsto import Mathlib.Order.Filter.Bases.Finite /-! # (Co)product of a family of filters In this file we define two filters on `Π i, α i` and prove some basic properties of these filters. * `Filter.pi (f : Π i, Filter (α i))` to be the maximal filter on `Π i, α i` such that `∀ i, Filter.Tendsto (Function.eval i) (Filter.pi f) (f i)`. It is defined as `Π i, Filter.comap (Function.eval i) (f i)`. This is a generalization of `Filter.prod` to indexed products. * `Filter.coprodᵢ (f : Π i, Filter (α i))`: a generalization of `Filter.coprod`; it is the supremum of `comap (eval i) (f i)`. -/ open Set Function Filter namespace Filter variable {ι : Type*} {α : ι → Type*} {f f₁ f₂ : (i : ι) → Filter (α i)} {s : (i : ι) → Set (α i)} {p : ∀ i, α i → Prop} section Pi theorem tendsto_eval_pi (f : ∀ i, Filter (α i)) (i : ι) : Tendsto (eval i) (pi f) (f i) := tendsto_iInf' i tendsto_comap theorem tendsto_pi {β : Type*} {m : β → ∀ i, α i} {l : Filter β} : Tendsto m l (pi f) ↔ ∀ i, Tendsto (fun x => m x i) l (f i) := by simp only [pi, tendsto_iInf, tendsto_comap_iff]; rfl /-- If a function tends to a product `Filter.pi f` of filters, then its `i`-th component tends to `f i`. See also `Filter.Tendsto.apply_nhds` for the special case of converging to a point in a product of topological spaces. -/ alias ⟨Tendsto.apply, _⟩ := tendsto_pi theorem le_pi {g : Filter (∀ i, α i)} : g ≤ pi f ↔ ∀ i, Tendsto (eval i) g (f i) := tendsto_pi @[mono] theorem pi_mono (h : ∀ i, f₁ i ≤ f₂ i) : pi f₁ ≤ pi f₂ := iInf_mono fun i => comap_mono <| h i theorem mem_pi_of_mem (i : ι) {s : Set (α i)} (hs : s ∈ f i) : eval i ⁻¹' s ∈ pi f := mem_iInf_of_mem i <| preimage_mem_comap hs theorem pi_mem_pi {I : Set ι} (hI : I.Finite) (h : ∀ i ∈ I, s i ∈ f i) : I.pi s ∈ pi f := by rw [pi_def, biInter_eq_iInter] refine mem_iInf_of_iInter hI (fun i => ?_) Subset.rfl exact preimage_mem_comap (h i i.2) theorem mem_pi {s : Set (∀ i, α i)} : s ∈ pi f ↔ ∃ I : Set ι, I.Finite ∧ ∃ t : ∀ i, Set (α i), (∀ i, t i ∈ f i) ∧ I.pi t ⊆ s := by constructor · simp only [pi, mem_iInf', mem_comap, pi_def] rintro ⟨I, If, V, hVf, -, rfl, -⟩ choose t htf htV using hVf exact ⟨I, If, t, htf, iInter₂_mono fun i _ => htV i⟩ · rintro ⟨I, If, t, htf, hts⟩ exact mem_of_superset (pi_mem_pi If fun i _ => htf i) hts theorem mem_pi' {s : Set (∀ i, α i)} : s ∈ pi f ↔ ∃ I : Finset ι, ∃ t : ∀ i, Set (α i), (∀ i, t i ∈ f i) ∧ Set.pi (↑I) t ⊆ s := mem_pi.trans exists_finite_iff_finset theorem mem_of_pi_mem_pi [∀ i, NeBot (f i)] {I : Set ι} (h : I.pi s ∈ pi f) {i : ι} (hi : i ∈ I) : s i ∈ f i := by classical rcases mem_pi.1 h with ⟨I', -, t, htf, hts⟩ refine mem_of_superset (htf i) fun x hx => ?_ have : ∀ i, (t i).Nonempty := fun i => nonempty_of_mem (htf i) choose g hg using this have : update g i x ∈ I'.pi t := fun j _ => by rcases eq_or_ne j i with (rfl | hne) <;> simp [*] simpa using hts this i hi @[simp] theorem pi_mem_pi_iff [∀ i, NeBot (f i)] {I : Set ι} (hI : I.Finite) : I.pi s ∈ pi f ↔ ∀ i ∈ I, s i ∈ f i := ⟨fun h _i hi => mem_of_pi_mem_pi h hi, pi_mem_pi hI⟩ theorem Eventually.eval_pi {i : ι} (hf : ∀ᶠ x : α i in f i, p i x) : ∀ᶠ x : ∀ i : ι, α i in pi f, p i (x i) := (tendsto_eval_pi _ _).eventually hf theorem eventually_pi [Finite ι] (hf : ∀ i, ∀ᶠ x in f i, p i x) : ∀ᶠ x : ∀ i, α i in pi f, ∀ i, p i (x i) := eventually_all.2 fun _i => (hf _).eval_pi theorem hasBasis_pi {ι' : ι → Type*} {s : ∀ i, ι' i → Set (α i)} {p : ∀ i, ι' i → Prop} (h : ∀ i, (f i).HasBasis (p i) (s i)) : (pi f).HasBasis (fun If : Set ι × ∀ i, ι' i => If.1.Finite ∧ ∀ i ∈ If.1, p i (If.2 i)) fun If : Set ι × ∀ i, ι' i => If.1.pi fun i => s i <| If.2 i := by simpa [Set.pi_def] using hasBasis_iInf' fun i => (h i).comap (eval i : (∀ j, α j) → α i) theorem hasBasis_pi_same_index {κ : Type*} {p : κ → Prop} {s : Π i : ι, κ → Set (α i)} (h : ∀ i : ι, (f i).HasBasis p (s i)) (h_dir : ∀ I : Set ι, ∀ k : ι → κ, I.Finite → (∀ i ∈ I, p (k i)) → ∃ k₀, p k₀ ∧ ∀ i ∈ I, s i k₀ ⊆ s i (k i)) : (pi f).HasBasis (fun Ik : Set ι × κ ↦ Ik.1.Finite ∧ p Ik.2) (fun Ik ↦ Ik.1.pi (fun i ↦ s i Ik.2)) := by refine hasBasis_pi h |>.to_hasBasis ?_ ?_ · rintro ⟨I, k⟩ ⟨hI, hk⟩ rcases h_dir I k hI hk with ⟨k₀, hk₀, hk₀'⟩ exact ⟨⟨I, k₀⟩, ⟨hI, hk₀⟩, Set.pi_mono hk₀'⟩ · rintro ⟨I, k⟩ ⟨hI, hk⟩ exact ⟨⟨I, fun _ ↦ k⟩, ⟨hI, fun _ _ ↦ hk⟩, subset_rfl⟩ theorem HasBasis.pi_self {α : Type*} {κ : Type*} {f : Filter α} {p : κ → Prop} {s : κ → Set α} (h : f.HasBasis p s) : (pi fun _ ↦ f).HasBasis (fun Ik : Set ι × κ ↦ Ik.1.Finite ∧ p Ik.2) (fun Ik ↦ Ik.1.pi (fun _ ↦ s Ik.2)) := by refine hasBasis_pi_same_index (fun _ ↦ h) (fun I k hI hk ↦ ?_) rcases h.mem_iff.mp (biInter_mem hI |>.mpr fun i hi ↦ h.mem_of_mem (hk i hi)) with ⟨k₀, hk₀, hk₀'⟩ exact ⟨k₀, hk₀, fun i hi ↦ hk₀'.trans (biInter_subset_of_mem hi)⟩ theorem le_pi_principal (s : (i : ι) → Set (α i)) : 𝓟 (univ.pi s) ≤ pi fun i ↦ 𝓟 (s i) := le_pi.2 fun i ↦ tendsto_principal_principal.2 fun _f hf ↦ hf i trivial /-- The indexed product of finitely many principal filters is the principal filter corresponding to the cylinder `Set.univ.pi s`. If the index type is infinite, then `mem_pi_principal` and `hasBasis_pi_principal` may be useful. -/ @[simp] theorem pi_principal [Finite ι] (s : (i : ι) → Set (α i)) : pi (fun i ↦ 𝓟 (s i)) = 𝓟 (univ.pi s) := by simp [Filter.pi, Set.pi_def] /-- The indexed product of a (possibly, infinite) family of principal filters is generated by the finite `Set.pi` cylinders. If the index type is finite, then the indexed product of principal filters is a pricipal filter, see `pi_principal`. -/ theorem mem_pi_principal {t : Set ((i : ι) → α i)} : t ∈ pi (fun i ↦ 𝓟 (s i)) ↔ ∃ I : Set ι, I.Finite ∧ I.pi s ⊆ t := (hasBasis_pi (fun i ↦ hasBasis_principal _)).mem_iff.trans <| by simp /-- The indexed product of a (possibly, infinite) family of principal filters is generated by the finite `Set.pi` cylinders. If the index type is finite, then the indexed product of principal filters is a pricipal filter, see `pi_principal`. -/ theorem hasBasis_pi_principal (s : (i : ι) → Set (α i)) : HasBasis (pi fun i ↦ 𝓟 (s i)) Set.Finite (Set.pi · s) := ⟨fun _ ↦ mem_pi_principal⟩ /-- The indexed product of finitely many pure filters `pure (f i)` is the pure filter `pure f`. If the index type is infinite, then `mem_pi_pure` and `hasBasis_pi_pure` below may be useful. -/ @[simp] theorem pi_pure [Finite ι] (f : (i : ι) → α i) : pi (pure <| f ·) = pure f := by simp only [← principal_singleton, pi_principal, univ_pi_singleton] /-- The indexed product of a (possibly, infinite) family of pure filters `pure (f i)` is generated by the sets of functions that are equal to `f` on a finite set. If the index type is finite, then the indexed product of pure filters is a pure filter, see `pi_pure`. -/ theorem mem_pi_pure {f : (i : ι) → α i} {s : Set ((i : ι) → α i)} : s ∈ pi (fun i ↦ pure (f i)) ↔ ∃ I : Set ι, I.Finite ∧ ∀ g, (∀ i ∈ I, g i = f i) → g ∈ s := by
simp only [← principal_singleton, mem_pi_principal] simp [subset_def]
Mathlib/Order/Filter/Pi.lean
170
171
/- 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.MonoidAlgebra.Degree import Mathlib.Algebra.Order.Ring.WithTop import Mathlib.Algebra.Polynomial.Basic import Mathlib.Data.Nat.Cast.WithTop import Mathlib.Data.Nat.SuccPred import Mathlib.Order.SuccPred.WithBot /-! # Degree of univariate polynomials ## Main definitions * `Polynomial.degree`: the degree of a polynomial, where `0` has degree `⊥` * `Polynomial.natDegree`: the degree of a polynomial, where `0` has degree `0` * `Polynomial.leadingCoeff`: the leading coefficient of a polynomial * `Polynomial.Monic`: a polynomial is monic if its leading coefficient is 0 * `Polynomial.nextCoeff`: the next coefficient after the leading coefficient ## Main results * `Polynomial.degree_eq_natDegree`: the degree and natDegree coincide for nonzero polynomials -/ noncomputable section open Finsupp Finset open Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b c d : R} {n m : ℕ} section Semiring variable [Semiring R] {p q r : R[X]} /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : R[X]) : WithBot ℕ := p.support.max /-- `natDegree p` forces `degree p` to ℕ, by defining `natDegree 0 = 0`. -/ def natDegree (p : R[X]) : ℕ := (degree p).unbotD 0 /-- `leadingCoeff p` gives the coefficient of the highest power of `X` in `p`. -/ def leadingCoeff (p : R[X]) : R := coeff p (natDegree p) /-- a polynomial is `Monic` if its leading coefficient is 1 -/ def Monic (p : R[X]) := leadingCoeff p = (1 : R) theorem Monic.def : Monic p ↔ leadingCoeff p = 1 := Iff.rfl instance Monic.decidable [DecidableEq R] : Decidable (Monic p) := by unfold Monic; infer_instance @[simp] theorem Monic.leadingCoeff {p : R[X]} (hp : p.Monic) : leadingCoeff p = 1 := hp theorem Monic.coeff_natDegree {p : R[X]} (hp : p.Monic) : p.coeff p.natDegree = 1 := hp @[simp] theorem degree_zero : degree (0 : R[X]) = ⊥ := rfl @[simp] theorem natDegree_zero : natDegree (0 : R[X]) = 0 := rfl @[simp] theorem coeff_natDegree : coeff p (natDegree p) = leadingCoeff p := rfl @[simp] theorem degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨fun h => support_eq_empty.1 (Finset.max_eq_bot.1 h), fun h => h.symm ▸ rfl⟩ theorem degree_ne_bot : degree p ≠ ⊥ ↔ p ≠ 0 := degree_eq_bot.not theorem degree_eq_natDegree (hp : p ≠ 0) : degree p = (natDegree p : WithBot ℕ) := by let ⟨n, hn⟩ := not_forall.1 (mt Option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) have hn : degree p = some n := Classical.not_not.1 hn rw [natDegree, hn]; rfl theorem degree_eq_iff_natDegree_eq {p : R[X]} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.natDegree = n := by rw [degree_eq_natDegree hp]; exact WithBot.coe_eq_coe theorem degree_eq_iff_natDegree_eq_of_pos {p : R[X]} {n : ℕ} (hn : 0 < n) : p.degree = n ↔ p.natDegree = n := by obtain rfl|h := eq_or_ne p 0 · simp [hn.ne] · exact degree_eq_iff_natDegree_eq h theorem natDegree_eq_of_degree_eq_some {p : R[X]} {n : ℕ} (h : degree p = n) : natDegree p = n := by rw [natDegree, h, Nat.cast_withBot, WithBot.unbotD_coe] theorem degree_ne_of_natDegree_ne {n : ℕ} : p.natDegree ≠ n → degree p ≠ n := mt natDegree_eq_of_degree_eq_some @[simp] theorem degree_le_natDegree : degree p ≤ natDegree p := WithBot.giUnbotDBot.gc.le_u_l _ theorem natDegree_eq_of_degree_eq [Semiring S] {q : S[X]} (h : degree p = degree q) : natDegree p = natDegree q := by unfold natDegree; rw [h] theorem le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : WithBot ℕ) ≤ degree p := by rw [Nat.cast_withBot] exact Finset.le_sup (mem_support_iff.2 h) theorem degree_mono [Semiring S] {f : R[X]} {g : S[X]} (h : f.support ⊆ g.support) : f.degree ≤ g.degree := Finset.sup_mono h theorem degree_le_degree (h : coeff q (natDegree p) ≠ 0) : degree p ≤ degree q := by by_cases hp : p = 0 · rw [hp, degree_zero] exact bot_le · rw [degree_eq_natDegree hp] exact le_degree_of_ne_zero h theorem natDegree_le_iff_degree_le {n : ℕ} : natDegree p ≤ n ↔ degree p ≤ n := WithBot.unbotD_le_iff (fun _ ↦ bot_le)
theorem natDegree_lt_iff_degree_lt (hp : p ≠ 0) : p.natDegree < n ↔ p.degree < ↑n := WithBot.unbotD_lt_iff (absurd · (degree_eq_bot.not.mpr hp)) alias ⟨degree_le_of_natDegree_le, natDegree_le_of_degree_le⟩ := natDegree_le_iff_degree_le theorem natDegree_le_natDegree [Semiring S] {q : S[X]} (hpq : p.degree ≤ q.degree) : p.natDegree ≤ q.natDegree :=
Mathlib/Algebra/Polynomial/Degree/Definitions.lean
138
144
/- Copyright (c) 2021 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.MeasureTheory.Covering.VitaliFamily import Mathlib.MeasureTheory.Function.AEMeasurableOrder import Mathlib.MeasureTheory.Integral.Average import Mathlib.MeasureTheory.Measure.Decomposition.Lebesgue import Mathlib.MeasureTheory.Measure.Regular /-! # Differentiation of measures On a second countable metric space with a measure `μ`, consider a Vitali family (i.e., for each `x` one has a family of sets shrinking to `x`, with a good behavior with respect to covering theorems). Consider also another measure `ρ`. Then, for almost every `x`, the ratio `ρ a / μ a` converges when `a` shrinks to `x` along the Vitali family, towards the Radon-Nikodym derivative of `ρ` with respect to `μ`. This is the main theorem on differentiation of measures. This theorem is proved in this file, under the name `VitaliFamily.ae_tendsto_rnDeriv`. Note that, almost surely, `μ a` is eventually positive and finite (see `VitaliFamily.ae_eventually_measure_pos` and `VitaliFamily.eventually_measure_lt_top`), so the ratio really makes sense. For concrete applications, one needs concrete instances of Vitali families, as provided for instance by `Besicovitch.vitaliFamily` (for balls) or by `Vitali.vitaliFamily` (for doubling measures). Specific applications to Lebesgue density points and the Lebesgue differentiation theorem are also derived: * `VitaliFamily.ae_tendsto_measure_inter_div` states that, for almost every point `x ∈ s`, then `μ (s ∩ a) / μ a` tends to `1` as `a` shrinks to `x` along a Vitali family. * `VitaliFamily.ae_tendsto_average_norm_sub` states that, for almost every point `x`, then the average of `y ↦ ‖f y - f x‖` on `a` tends to `0` as `a` shrinks to `x` along a Vitali family. ## Sketch of proof Let `v` be a Vitali family for `μ`. Assume for simplicity that `ρ` is absolutely continuous with respect to `μ`, as the case of a singular measure is easier. It is easy to see that a set `s` on which `liminf ρ a / μ a < q` satisfies `ρ s ≤ q * μ s`, by using a disjoint subcovering provided by the definition of Vitali families. Similarly for the limsup. It follows that a set on which `ρ a / μ a` oscillates has measure `0`, and therefore that `ρ a / μ a` converges almost surely (`VitaliFamily.ae_tendsto_div`). Moreover, on a set where the limit is close to a constant `c`, one gets `ρ s ∼ c μ s`, using again a covering lemma as above. It follows that `ρ` is equal to `μ.withDensity (v.limRatio ρ x)`, where `v.limRatio ρ x` is the limit of `ρ a / μ a` at `x` (which is well defined almost everywhere). By uniqueness of the Radon-Nikodym derivative, one gets `v.limRatio ρ x = ρ.rnDeriv μ x` almost everywhere, completing the proof. There is a difficulty in this sketch: this argument works well when `v.limRatio ρ` is measurable, but there is no guarantee that this is the case, especially if one doesn't make further assumptions on the Vitali family. We use an indirect argument to show that `v.limRatio ρ` is always almost everywhere measurable, again based on the disjoint subcovering argument (see `VitaliFamily.exists_measurable_supersets_limRatio`), and then proceed as sketched above but replacing `v.limRatio ρ` by a measurable version called `v.limRatioMeas ρ`. ## Counterexample The standing assumption in this file is that spaces are second countable. Without this assumption, measures may be zero locally but nonzero globally, which is not compatible with differentiation theory (which deduces global information from local one). Here is an example displaying this behavior. Define a measure `μ` by `μ s = 0` if `s` is covered by countably many balls of radius `1`, and `μ s = ∞` otherwise. This is indeed a countably additive measure, which is moreover locally finite and doubling at small scales. It vanishes on every ball of radius `1`, so all the quantities in differentiation theory (defined as ratios of measures as the radius tends to zero) make no sense. However, the measure is not globally zero if the space is big enough. ## References * [Herbert Federer, Geometric Measure Theory, Chapter 2.9][Federer1996] -/ open MeasureTheory Metric Set Filter TopologicalSpace MeasureTheory.Measure open scoped Filter ENNReal MeasureTheory NNReal Topology variable {α : Type*} [PseudoMetricSpace α] {m0 : MeasurableSpace α} {μ : Measure α} (v : VitaliFamily μ) {E : Type*} [NormedAddCommGroup E] namespace VitaliFamily /-- The limit along a Vitali family of `ρ a / μ a` where it makes sense, and garbage otherwise. Do *not* use this definition: it is only a temporary device to show that this ratio tends almost everywhere to the Radon-Nikodym derivative. -/ noncomputable def limRatio (ρ : Measure α) (x : α) : ℝ≥0∞ := limUnder (v.filterAt x) fun a => ρ a / μ a /-- For almost every point `x`, sufficiently small sets in a Vitali family around `x` have positive measure. (This is a nontrivial result, following from the covering property of Vitali families). -/ theorem ae_eventually_measure_pos [SecondCountableTopology α] : ∀ᵐ x ∂μ, ∀ᶠ a in v.filterAt x, 0 < μ a := by set s := {x | ¬∀ᶠ a in v.filterAt x, 0 < μ a} with hs simp -zeta only [not_lt, not_eventually, nonpos_iff_eq_zero] at hs change μ s = 0 let f : α → Set (Set α) := fun _ => {a | μ a = 0} have h : v.FineSubfamilyOn f s := by intro x hx ε εpos rw [hs] at hx simp only [frequently_filterAt_iff, exists_prop, gt_iff_lt, mem_setOf_eq] at hx rcases hx ε εpos with ⟨a, a_sets, ax, μa⟩ exact ⟨a, ⟨a_sets, μa⟩, ax⟩ refine le_antisymm ?_ bot_le calc μ s ≤ ∑' x : h.index, μ (h.covering x) := h.measure_le_tsum _ = ∑' x : h.index, 0 := by congr; ext1 x; exact h.covering_mem x.2 _ = 0 := by simp only [tsum_zero, add_zero] /-- For every point `x`, sufficiently small sets in a Vitali family around `x` have finite measure. (This is a trivial result, following from the fact that the measure is locally finite). -/ theorem eventually_measure_lt_top [IsLocallyFiniteMeasure μ] (x : α) : ∀ᶠ a in v.filterAt x, μ a < ∞ := (μ.finiteAt_nhds x).eventually.filter_mono inf_le_left /-- If two measures `ρ` and `ν` have, at every point of a set `s`, arbitrarily small sets in a Vitali family satisfying `ρ a ≤ ν a`, then `ρ s ≤ ν s` if `ρ ≪ μ`. -/ theorem measure_le_of_frequently_le [SecondCountableTopology α] [BorelSpace α] {ρ : Measure α} (ν : Measure α) [IsLocallyFiniteMeasure ν] (hρ : ρ ≪ μ) (s : Set α) (hs : ∀ x ∈ s, ∃ᶠ a in v.filterAt x, ρ a ≤ ν a) : ρ s ≤ ν s := by -- this follows from a covering argument using the sets satisfying `ρ a ≤ ν a`. apply ENNReal.le_of_forall_pos_le_add fun ε εpos _ => ?_ obtain ⟨U, sU, U_open, νU⟩ : ∃ (U : Set α), s ⊆ U ∧ IsOpen U ∧ ν U ≤ ν s + ε := exists_isOpen_le_add s ν (ENNReal.coe_pos.2 εpos).ne' let f : α → Set (Set α) := fun _ => {a | ρ a ≤ ν a ∧ a ⊆ U} have h : v.FineSubfamilyOn f s := by apply v.fineSubfamilyOn_of_frequently f s fun x hx => ?_ have := (hs x hx).and_eventually ((v.eventually_filterAt_mem_setsAt x).and (v.eventually_filterAt_subset_of_nhds (U_open.mem_nhds (sU hx)))) apply Frequently.mono this rintro a ⟨ρa, _, aU⟩ exact ⟨ρa, aU⟩ haveI : Encodable h.index := h.index_countable.toEncodable calc ρ s ≤ ∑' x : h.index, ρ (h.covering x) := h.measure_le_tsum_of_absolutelyContinuous hρ _ ≤ ∑' x : h.index, ν (h.covering x) := ENNReal.tsum_le_tsum fun x => (h.covering_mem x.2).1 _ = ν (⋃ x : h.index, h.covering x) := by rw [measure_iUnion h.covering_disjoint_subtype fun i => h.measurableSet_u i.2] _ ≤ ν U := (measure_mono (iUnion_subset fun i => (h.covering_mem i.2).2)) _ ≤ ν s + ε := νU theorem eventually_filterAt_integrableOn (x : α) {f : α → E} (hf : LocallyIntegrable f μ) : ∀ᶠ a in v.filterAt x, IntegrableOn f a μ := by rcases hf x with ⟨w, w_nhds, hw⟩ filter_upwards [v.eventually_filterAt_subset_of_nhds w_nhds] with a ha exact hw.mono_set ha section variable [SecondCountableTopology α] [BorelSpace α] [IsLocallyFiniteMeasure μ] {ρ : Measure α} [IsLocallyFiniteMeasure ρ] /-- If a measure `ρ` is singular with respect to `μ`, then for `μ` almost every `x`, the ratio `ρ a / μ a` tends to zero when `a` shrinks to `x` along the Vitali family. This makes sense as `μ a` is eventually positive by `ae_eventually_measure_pos`. -/ theorem ae_eventually_measure_zero_of_singular (hρ : ρ ⟂ₘ μ) : ∀ᵐ x ∂μ, Tendsto (fun a => ρ a / μ a) (v.filterAt x) (𝓝 0) := by have A : ∀ ε > (0 : ℝ≥0), ∀ᵐ x ∂μ, ∀ᶠ a in v.filterAt x, ρ a < ε * μ a := by intro ε εpos set s := {x | ¬∀ᶠ a in v.filterAt x, ρ a < ε * μ a} with hs change μ s = 0 obtain ⟨o, _, ρo, μo⟩ : ∃ o : Set α, MeasurableSet o ∧ ρ o = 0 ∧ μ oᶜ = 0 := hρ apply le_antisymm _ bot_le calc μ s ≤ μ (s ∩ o ∪ oᶜ) := by conv_lhs => rw [← inter_union_compl s o] gcongr apply inter_subset_right _ ≤ μ (s ∩ o) + μ oᶜ := measure_union_le _ _ _ = μ (s ∩ o) := by rw [μo, add_zero] _ = (ε : ℝ≥0∞)⁻¹ * (ε • μ) (s ∩ o) := by simp only [coe_nnreal_smul_apply, ← mul_assoc, mul_comm _ (ε : ℝ≥0∞)] rw [ENNReal.mul_inv_cancel (ENNReal.coe_pos.2 εpos).ne' ENNReal.coe_ne_top, one_mul] _ ≤ (ε : ℝ≥0∞)⁻¹ * ρ (s ∩ o) := by gcongr refine v.measure_le_of_frequently_le ρ smul_absolutelyContinuous _ ?_ intro x hx rw [hs] at hx simp only [mem_inter_iff, not_lt, not_eventually, mem_setOf_eq] at hx exact hx.1 _ ≤ (ε : ℝ≥0∞)⁻¹ * ρ o := by gcongr; apply inter_subset_right _ = 0 := by rw [ρo, mul_zero] obtain ⟨u, _, u_pos, u_lim⟩ : ∃ u : ℕ → ℝ≥0, StrictAnti u ∧ (∀ n : ℕ, 0 < u n) ∧ Tendsto u atTop (𝓝 0) := exists_seq_strictAnti_tendsto (0 : ℝ≥0) have B : ∀ᵐ x ∂μ, ∀ n, ∀ᶠ a in v.filterAt x, ρ a < u n * μ a := ae_all_iff.2 fun n => A (u n) (u_pos n) filter_upwards [B, v.ae_eventually_measure_pos] intro x hx h'x refine tendsto_order.2 ⟨fun z hz => (ENNReal.not_lt_zero hz).elim, fun z hz => ?_⟩ obtain ⟨w, w_pos, w_lt⟩ : ∃ w : ℝ≥0, (0 : ℝ≥0∞) < w ∧ (w : ℝ≥0∞) < z := ENNReal.lt_iff_exists_nnreal_btwn.1 hz obtain ⟨n, hn⟩ : ∃ n, u n < w := ((tendsto_order.1 u_lim).2 w (ENNReal.coe_pos.1 w_pos)).exists filter_upwards [hx n, h'x, v.eventually_measure_lt_top x] intro a ha μa_pos μa_lt_top rw [ENNReal.div_lt_iff (Or.inl μa_pos.ne') (Or.inl μa_lt_top.ne)] exact ha.trans_le (mul_le_mul_right' ((ENNReal.coe_le_coe.2 hn.le).trans w_lt.le) _) section AbsolutelyContinuous variable (hρ : ρ ≪ μ) include hρ /-- A set of points `s` satisfying both `ρ a ≤ c * μ a` and `ρ a ≥ d * μ a` at arbitrarily small sets in a Vitali family has measure `0` if `c < d`. Indeed, the first inequality should imply that `ρ s ≤ c * μ s`, and the second one that `ρ s ≥ d * μ s`, a contradiction if `0 < μ s`. -/
theorem null_of_frequently_le_of_frequently_ge {c d : ℝ≥0} (hcd : c < d) (s : Set α) (hc : ∀ x ∈ s, ∃ᶠ a in v.filterAt x, ρ a ≤ c * μ a) (hd : ∀ x ∈ s, ∃ᶠ a in v.filterAt x, (d : ℝ≥0∞) * μ a ≤ ρ a) : μ s = 0 := by apply measure_null_of_locally_null s fun x _ => ?_ obtain ⟨o, xo, o_open, μo⟩ : ∃ o : Set α, x ∈ o ∧ IsOpen o ∧ μ o < ∞ := Measure.exists_isOpen_measure_lt_top μ x refine ⟨s ∩ o, inter_mem_nhdsWithin _ (o_open.mem_nhds xo), ?_⟩ let s' := s ∩ o by_contra h apply lt_irrefl (ρ s') calc ρ s' ≤ c * μ s' := v.measure_le_of_frequently_le (c • μ) hρ s' fun x hx => hc x hx.1 _ < d * μ s' := by apply (ENNReal.mul_lt_mul_right h _).2 (ENNReal.coe_lt_coe.2 hcd) exact (lt_of_le_of_lt (measure_mono inter_subset_right) μo).ne _ ≤ ρ s' := v.measure_le_of_frequently_le ρ smul_absolutelyContinuous s' fun x hx ↦ hd x hx.1 /-- If `ρ` is absolutely continuous with respect to `μ`, then for almost every `x`,
Mathlib/MeasureTheory/Covering/Differentiation.lean
211
228
/- Copyright (c) 2023 Claus Clausen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Claus Clausen, Patrick Massot -/ import Mathlib.Probability.Notation import Mathlib.Probability.CDF import Mathlib.Probability.Distributions.Gamma /-! # Exponential distributions over ℝ Define the Exponential measure over the reals. ## Main definitions * `exponentialPDFReal`: the function `r x ↦ r * exp (-(r * x)` for `0 ≤ x` or `0` else, which is the probability density function of a exponential distribution with rate `r` (when `hr : 0 < r`). * `exponentialPDF`: `ℝ≥0∞`-valued pdf, `exponentialPDF r = ENNReal.ofReal (exponentialPDFReal r)`. * `expMeasure`: an exponential measure on `ℝ`, parametrized by its rate `r`. * `exponentialCDFReal`: the CDF given by the definition of CDF in `ProbabilityTheory.CDF` applied to the exponential measure. ## Main results * `exponentialCDFReal_eq`: Proof that the `exponentialCDFReal` given by the definition equals the known function given as `r x ↦ 1 - exp (- (r * x))` for `0 ≤ x` or `0` else. -/ open scoped ENNReal NNReal open MeasureTheory Real Set Filter Topology namespace ProbabilityTheory section ExponentialPDF /-- The pdf of the exponential distribution depending on its rate -/ noncomputable def exponentialPDFReal (r x : ℝ) : ℝ := gammaPDFReal 1 r x /-- The pdf of the exponential distribution, as a function valued in `ℝ≥0∞` -/ noncomputable def exponentialPDF (r x : ℝ) : ℝ≥0∞ := ENNReal.ofReal (exponentialPDFReal r x) lemma exponentialPDF_eq (r x : ℝ) : exponentialPDF r x = ENNReal.ofReal (if 0 ≤ x then r * exp (-(r * x)) else 0) := by rw [exponentialPDF, exponentialPDFReal, gammaPDFReal] simp only [rpow_one, Gamma_one, div_one, sub_self, rpow_zero, mul_one] lemma exponentialPDF_of_neg {r x : ℝ} (hx : x < 0) : exponentialPDF r x = 0 := gammaPDF_of_neg hx
lemma exponentialPDF_of_nonneg {r x : ℝ} (hx : 0 ≤ x) : exponentialPDF r x = ENNReal.ofReal (r * rexp (-(r * x))) := by simp only [exponentialPDF_eq, if_pos hx]
Mathlib/Probability/Distributions/Exponential.lean
54
56
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Manuel Candales -/ import Mathlib.Geometry.Euclidean.PerpBisector import Mathlib.Algebra.QuadraticDiscriminant /-! # Euclidean spaces This file makes some definitions and proves very basic geometrical results about real inner product spaces and Euclidean affine spaces. Results about real inner product spaces that involve the norm and inner product but not angles generally go in `Analysis.NormedSpace.InnerProduct`. Results with longer proofs or more geometrical content generally go in separate files. ## Implementation notes To declare `P` as the type of points in a Euclidean affine space with `V` as the type of vectors, use `[NormedAddCommGroup V] [InnerProductSpace ℝ V] [MetricSpace P] [NormedAddTorsor V P]`. This works better with `outParam` to make `V` implicit in most cases than having a separate type alias for Euclidean affine spaces. Rather than requiring Euclidean affine spaces to be finite-dimensional (as in the definition on Wikipedia), this is specified only for those theorems that need it. ## References * https://en.wikipedia.org/wiki/Euclidean_space -/ noncomputable section open RealInnerProductSpace namespace EuclideanGeometry /-! ### Geometrical results on Euclidean affine spaces This section develops some geometrical definitions and results on Euclidean affine spaces. -/ variable {V : Type*} {P : Type*} variable [NormedAddCommGroup V] [InnerProductSpace ℝ V] [MetricSpace P] variable [NormedAddTorsor V P] /-- The inner product of two vectors given with `weightedVSub`, in terms of the pairwise distances. -/ theorem inner_weightedVSub {ι₁ : Type*} {s₁ : Finset ι₁} {w₁ : ι₁ → ℝ} (p₁ : ι₁ → P) (h₁ : ∑ i ∈ s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : Finset ι₂} {w₂ : ι₂ → ℝ} (p₂ : ι₂ → P) (h₂ : ∑ i ∈ s₂, w₂ i = 0) : ⟪s₁.weightedVSub p₁ w₁, s₂.weightedVSub p₂ w₂⟫ = (-∑ i₁ ∈ s₁, ∑ i₂ ∈ s₂, w₁ i₁ * w₂ i₂ * (dist (p₁ i₁) (p₂ i₂) * dist (p₁ i₁) (p₂ i₂))) / 2 := by rw [Finset.weightedVSub_apply, Finset.weightedVSub_apply, inner_sum_smul_sum_smul_of_sum_eq_zero _ h₁ _ h₂] simp_rw [vsub_sub_vsub_cancel_right] rcongr (i₁ i₂) <;> rw [dist_eq_norm_vsub V (p₁ i₁) (p₂ i₂)] /-- The distance between two points given with `affineCombination`, in terms of the pairwise distances between the points in that combination. -/ theorem dist_affineCombination {ι : Type*} {s : Finset ι} {w₁ w₂ : ι → ℝ} (p : ι → P) (h₁ : ∑ i ∈ s, w₁ i = 1) (h₂ : ∑ i ∈ s, w₂ i = 1) : by have a₁ := s.affineCombination ℝ p w₁ have a₂ := s.affineCombination ℝ p w₂ exact dist a₁ a₂ * dist a₁ a₂ = (-∑ i₁ ∈ s, ∑ i₂ ∈ s, (w₁ - w₂) i₁ * (w₁ - w₂) i₂ * (dist (p i₁) (p i₂) * dist (p i₁) (p i₂))) / 2 := by dsimp only rw [dist_eq_norm_vsub V (s.affineCombination ℝ p w₁) (s.affineCombination ℝ p w₂), ← @inner_self_eq_norm_mul_norm ℝ, Finset.affineCombination_vsub] have h : (∑ i ∈ s, (w₁ - w₂) i) = 0 := by simp_rw [Pi.sub_apply, Finset.sum_sub_distrib, h₁, h₂, sub_self] exact inner_weightedVSub p h p h -- Porting note: `inner_vsub_vsub_of_dist_eq_of_dist_eq` moved to `PerpendicularBisector` /-- The squared distance between points on a line (expressed as a multiple of a fixed vector added to a point) and another point, expressed as a quadratic. -/ theorem dist_smul_vadd_sq (r : ℝ) (v : V) (p₁ p₂ : P) : dist (r • v +ᵥ p₁) p₂ * dist (r • v +ᵥ p₁) p₂ = ⟪v, v⟫ * r * r + 2 * ⟪v, p₁ -ᵥ p₂⟫ * r + ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ := by rw [dist_eq_norm_vsub V _ p₂, ← real_inner_self_eq_norm_mul_norm, vadd_vsub_assoc, real_inner_add_add_self, real_inner_smul_left, real_inner_smul_left, real_inner_smul_right] ring /-- The condition for two points on a line to be equidistant from another point. -/ theorem dist_smul_vadd_eq_dist {v : V} (p₁ p₂ : P) (hv : v ≠ 0) (r : ℝ) : dist (r • v +ᵥ p₁) p₂ = dist p₁ p₂ ↔ r = 0 ∨ r = -2 * ⟪v, p₁ -ᵥ p₂⟫ / ⟪v, v⟫ := by conv_lhs => rw [← mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_smul_vadd_sq, mul_assoc, ← sub_eq_zero, add_sub_assoc, dist_eq_norm_vsub V p₁ p₂, ← real_inner_self_eq_norm_mul_norm, sub_self] have hvi : ⟪v, v⟫ ≠ 0 := by simpa using hv have hd : discrim ⟪v, v⟫ (2 * ⟪v, p₁ -ᵥ p₂⟫) 0 = 2 * ⟪v, p₁ -ᵥ p₂⟫ * (2 * ⟪v, p₁ -ᵥ p₂⟫) := by rw [discrim] ring rw [quadratic_eq_zero_iff hvi hd, neg_add_cancel, zero_div, neg_mul_eq_neg_mul, ← mul_sub_right_distrib, sub_eq_add_neg, ← mul_two, mul_assoc, mul_div_assoc, mul_div_mul_left, mul_div_assoc] norm_num open AffineSubspace Module /-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at most two points `p₁` `p₂` in a two-dimensional subspace containing those points (two circles intersect in at most two points). -/ theorem eq_of_dist_eq_of_dist_eq_of_mem_of_finrank_eq_two {s : AffineSubspace ℝ P} [FiniteDimensional ℝ s.direction] (hd : finrank ℝ s.direction = 2) {c₁ c₂ p₁ p₂ p : P} (hc₁s : c₁ ∈ s) (hc₂s : c₂ ∈ s) (hp₁s : p₁ ∈ s) (hp₂s : p₂ ∈ s) (hps : p ∈ s) {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ := by have ho : ⟪c₂ -ᵥ c₁, p₂ -ᵥ p₁⟫ = 0 := inner_vsub_vsub_of_dist_eq_of_dist_eq (hp₁c₁.trans hp₂c₁.symm) (hp₁c₂.trans hp₂c₂.symm) have hop : ⟪c₂ -ᵥ c₁, p -ᵥ p₁⟫ = 0 := inner_vsub_vsub_of_dist_eq_of_dist_eq (hp₁c₁.trans hpc₁.symm) (hp₁c₂.trans hpc₂.symm) let b : Fin 2 → V := ![c₂ -ᵥ c₁, p₂ -ᵥ p₁] have hb : LinearIndependent ℝ b := by refine linearIndependent_of_ne_zero_of_inner_eq_zero ?_ ?_ · intro i fin_cases i <;> simp [b, hc.symm, hp.symm] · intro i j hij fin_cases i <;> fin_cases j <;> try exact False.elim (hij rfl) · exact ho · rw [real_inner_comm] exact ho have hbs : Submodule.span ℝ (Set.range b) = s.direction := by refine Submodule.eq_of_le_of_finrank_eq ?_ ?_ · rw [Submodule.span_le, Set.range_subset_iff] intro i fin_cases i · exact vsub_mem_direction hc₂s hc₁s · exact vsub_mem_direction hp₂s hp₁s · rw [finrank_span_eq_card hb, Fintype.card_fin, hd] have hv : ∀ v ∈ s.direction, ∃ t₁ t₂ : ℝ, v = t₁ • (c₂ -ᵥ c₁) + t₂ • (p₂ -ᵥ p₁) := by intro v hv have hr : Set.range b = {c₂ -ᵥ c₁, p₂ -ᵥ p₁} := by have hu : (Finset.univ : Finset (Fin 2)) = {0, 1} := by decide classical rw [← Fintype.coe_image_univ, hu] simp [b] rw [← hbs, hr, Submodule.mem_span_insert] at hv rcases hv with ⟨t₁, v', hv', hv⟩ rw [Submodule.mem_span_singleton] at hv' rcases hv' with ⟨t₂, rfl⟩ exact ⟨t₁, t₂, hv⟩ rcases hv (p -ᵥ p₁) (vsub_mem_direction hps hp₁s) with ⟨t₁, t₂, hpt⟩ simp only [hpt, inner_add_right, inner_smul_right, ho, mul_zero, add_zero, mul_eq_zero, inner_self_eq_zero, vsub_eq_zero_iff_eq, hc.symm, or_false] at hop rw [hop, zero_smul, zero_add, ← eq_vadd_iff_vsub_eq] at hpt subst hpt have hp' : (p₂ -ᵥ p₁ : V) ≠ 0 := by simp [hp.symm] have hp₂ : dist ((1 : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁) c₁ = r₁ := by simp [hp₂c₁] rw [← hp₁c₁, dist_smul_vadd_eq_dist _ _ hp'] at hpc₁ hp₂ simp only [one_ne_zero, false_or] at hp₂ rw [hp₂.symm] at hpc₁ rcases hpc₁ with hpc₁ | hpc₁ <;> simp [hpc₁] /-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at most two points `p₁` `p₂` in two-dimensional space (two circles intersect in at most two points). -/ theorem eq_of_dist_eq_of_dist_eq_of_finrank_eq_two [FiniteDimensional ℝ V] (hd : finrank ℝ V = 2) {c₁ c₂ p₁ p₂ p : P} {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ := haveI hd' : finrank ℝ (⊤ : AffineSubspace ℝ P).direction = 2 := by rw [direction_top, finrank_top] exact hd eq_of_dist_eq_of_dist_eq_of_mem_of_finrank_eq_two hd' (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) hc hp hp₁c₁ hp₂c₁ hpc₁ hp₁c₂ hp₂c₂ hpc₂ end EuclideanGeometry
Mathlib/Geometry/Euclidean/Basic.lean
585
599
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import Mathlib.Topology.MetricSpace.Pseudo.Constructions import Mathlib.Topology.Order.DenselyOrdered import Mathlib.Topology.UniformSpace.Compact /-! # Extra lemmas about pseudo-metric spaces -/ open Bornology Filter Metric Set open scoped NNReal Topology variable {ι α : Type*} [PseudoMetricSpace α] instance : OrderTopology ℝ := orderTopology_of_nhds_abs fun x => by simp only [nhds_basis_ball.eq_biInf, ball, Real.dist_eq, abs_sub_comm]
lemma Real.singleton_eq_inter_Icc (b : ℝ) : {b} = ⋂ (r > 0), Icc (b - r) (b + r) := by simp [Icc_eq_closedBall, biInter_basis_nhds Metric.nhds_basis_closedBall]
Mathlib/Topology/MetricSpace/Pseudo/Lemmas.lean
23
24
/- 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 -/ import Mathlib.Algebra.Homology.Homotopy import Mathlib.Algebra.Ring.NegOnePow import Mathlib.Algebra.Category.Grp.Preadditive import Mathlib.Tactic.Linarith import Mathlib.CategoryTheory.Linear.LinearFunctor /-! The cochain complex of homomorphisms between cochain complexes If `F` and `G` are cochain complexes (indexed by `ℤ`) in a preadditive category, there is a cochain complex of abelian groups whose `0`-cocycles identify to morphisms `F ⟶ G`. Informally, in degree `n`, this complex shall consist of cochains of degree `n` from `F` to `G`, i.e. arbitrary families for morphisms `F.X p ⟶ G.X (p + n)`. This complex shall be denoted `HomComplex F G`. In order to avoid type theoretic issues, a cochain of degree `n : ℤ` (i.e. a term of type of `Cochain F G n`) shall be defined here as the data of a morphism `F.X p ⟶ G.X q` for all triplets `⟨p, q, hpq⟩` where `p` and `q` are integers and `hpq : p + n = q`. If `α : Cochain F G n`, we shall define `α.v p q hpq : F.X p ⟶ G.X q`. We follow the signs conventions appearing in the introduction of [Brian Conrad's book *Grothendieck duality and base change*][conrad2000]. ## References * [Brian Conrad, Grothendieck duality and base change][conrad2000] -/ assert_not_exists TwoSidedIdeal open CategoryTheory Category Limits Preadditive universe v u variable {C : Type u} [Category.{v} C] [Preadditive C] {R : Type*} [Ring R] [Linear R C] namespace CochainComplex variable {F G K L : CochainComplex C ℤ} (n m : ℤ) namespace HomComplex /-- A term of type `HomComplex.Triplet n` consists of two integers `p` and `q` such that `p + n = q`. (This type is introduced so that the instance `AddCommGroup (Cochain F G n)` defined below can be found automatically.) -/ structure Triplet (n : ℤ) where /-- a first integer -/ p : ℤ /-- a second integer -/ q : ℤ /-- the condition on the two integers -/ hpq : p + n = q variable (F G) /-- A cochain of degree `n : ℤ` between to cochain complexes `F` and `G` consists of a family of morphisms `F.X p ⟶ G.X q` whenever `p + n = q`, i.e. for all triplets in `HomComplex.Triplet n`. -/ def Cochain := ∀ (T : Triplet n), F.X T.p ⟶ G.X T.q instance : AddCommGroup (Cochain F G n) := by dsimp only [Cochain] infer_instance instance : Module R (Cochain F G n) := by dsimp only [Cochain] infer_instance namespace Cochain variable {F G n} /-- A practical constructor for cochains. -/ def mk (v : ∀ (p q : ℤ) (_ : p + n = q), F.X p ⟶ G.X q) : Cochain F G n := fun ⟨p, q, hpq⟩ => v p q hpq /-- The value of a cochain on a triplet `⟨p, q, hpq⟩`. -/ def v (γ : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : F.X p ⟶ G.X q := γ ⟨p, q, hpq⟩ @[simp] lemma mk_v (v : ∀ (p q : ℤ) (_ : p + n = q), F.X p ⟶ G.X q) (p q : ℤ) (hpq : p + n = q) : (Cochain.mk v).v p q hpq = v p q hpq := rfl lemma congr_v {z₁ z₂ : Cochain F G n} (h : z₁ = z₂) (p q : ℤ) (hpq : p + n = q) : z₁.v p q hpq = z₂.v p q hpq := by subst h; rfl @[ext] lemma ext (z₁ z₂ : Cochain F G n) (h : ∀ (p q hpq), z₁.v p q hpq = z₂.v p q hpq) : z₁ = z₂ := by funext ⟨p, q, hpq⟩ apply h @[ext 1100] lemma ext₀ (z₁ z₂ : Cochain F G 0) (h : ∀ (p : ℤ), z₁.v p p (add_zero p) = z₂.v p p (add_zero p)) : z₁ = z₂ := by ext p q hpq obtain rfl : q = p := by rw [← hpq, add_zero] exact h q @[simp] lemma zero_v {n : ℤ} (p q : ℤ) (hpq : p + n = q) : (0 : Cochain F G n).v p q hpq = 0 := rfl @[simp] lemma add_v {n : ℤ} (z₁ z₂ : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (z₁ + z₂).v p q hpq = z₁.v p q hpq + z₂.v p q hpq := rfl @[simp] lemma sub_v {n : ℤ} (z₁ z₂ : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (z₁ - z₂).v p q hpq = z₁.v p q hpq - z₂.v p q hpq := rfl @[simp] lemma neg_v {n : ℤ} (z : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (-z).v p q hpq = - (z.v p q hpq) := rfl @[simp] lemma smul_v {n : ℤ} (k : R) (z : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (k • z).v p q hpq = k • (z.v p q hpq) := rfl @[simp] lemma units_smul_v {n : ℤ} (k : Rˣ) (z : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (k • z).v p q hpq = k • (z.v p q hpq) := rfl /-- A cochain of degree `0` from `F` to `G` can be constructed from a family of morphisms `F.X p ⟶ G.X p` for all `p : ℤ`. -/ def ofHoms (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) : Cochain F G 0 := Cochain.mk (fun p q hpq => ψ p ≫ eqToHom (by rw [← hpq, add_zero])) @[simp] lemma ofHoms_v (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) (p : ℤ) : (ofHoms ψ).v p p (add_zero p) = ψ p := by simp only [ofHoms, mk_v, eqToHom_refl, comp_id] @[simp] lemma ofHoms_zero : ofHoms (fun p => (0 : F.X p ⟶ G.X p)) = 0 := by aesop_cat @[simp] lemma ofHoms_v_comp_d (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) (p q q' : ℤ) (hpq : p + 0 = q) : (ofHoms ψ).v p q hpq ≫ G.d q q' = ψ p ≫ G.d p q' := by rw [add_zero] at hpq subst hpq rw [ofHoms_v] @[simp] lemma d_comp_ofHoms_v (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) (p' p q : ℤ) (hpq : p + 0 = q) : F.d p' p ≫ (ofHoms ψ).v p q hpq = F.d p' q ≫ ψ q := by rw [add_zero] at hpq subst hpq rw [ofHoms_v] /-- The `0`-cochain attached to a morphism of cochain complexes. -/ def ofHom (φ : F ⟶ G) : Cochain F G 0 := ofHoms (fun p => φ.f p) variable (F G) @[simp] lemma ofHom_zero : ofHom (0 : F ⟶ G) = 0 := by simp only [ofHom, HomologicalComplex.zero_f_apply, ofHoms_zero] variable {F G} @[simp] lemma ofHom_v (φ : F ⟶ G) (p : ℤ) : (ofHom φ).v p p (add_zero p) = φ.f p := by simp only [ofHom, ofHoms_v] @[simp] lemma ofHom_v_comp_d (φ : F ⟶ G) (p q q' : ℤ) (hpq : p + 0 = q) : (ofHom φ).v p q hpq ≫ G.d q q' = φ.f p ≫ G.d p q' := by simp only [ofHom, ofHoms_v_comp_d] @[simp] lemma d_comp_ofHom_v (φ : F ⟶ G) (p' p q : ℤ) (hpq : p + 0 = q) : F.d p' p ≫ (ofHom φ).v p q hpq = F.d p' q ≫ φ.f q := by simp only [ofHom, d_comp_ofHoms_v] @[simp] lemma ofHom_add (φ₁ φ₂ : F ⟶ G) : Cochain.ofHom (φ₁ + φ₂) = Cochain.ofHom φ₁ + Cochain.ofHom φ₂ := by aesop_cat @[simp] lemma ofHom_sub (φ₁ φ₂ : F ⟶ G) : Cochain.ofHom (φ₁ - φ₂) = Cochain.ofHom φ₁ - Cochain.ofHom φ₂ := by aesop_cat @[simp] lemma ofHom_neg (φ : F ⟶ G) : Cochain.ofHom (-φ) = -Cochain.ofHom φ := by aesop_cat /-- The cochain of degree `-1` given by an homotopy between two morphism of complexes. -/ def ofHomotopy {φ₁ φ₂ : F ⟶ G} (ho : Homotopy φ₁ φ₂) : Cochain F G (-1) := Cochain.mk (fun p q _ => ho.hom p q) @[simp] lemma ofHomotopy_ofEq {φ₁ φ₂ : F ⟶ G} (h : φ₁ = φ₂) : ofHomotopy (Homotopy.ofEq h) = 0 := rfl @[simp] lemma ofHomotopy_refl (φ : F ⟶ G) : ofHomotopy (Homotopy.refl φ) = 0 := rfl @[reassoc] lemma v_comp_XIsoOfEq_hom (γ : Cochain F G n) (p q q' : ℤ) (hpq : p + n = q) (hq' : q = q') : γ.v p q hpq ≫ (HomologicalComplex.XIsoOfEq G hq').hom = γ.v p q' (by rw [← hq', hpq]) := by subst hq' simp only [HomologicalComplex.XIsoOfEq, eqToIso_refl, Iso.refl_hom, comp_id] @[reassoc] lemma v_comp_XIsoOfEq_inv (γ : Cochain F G n) (p q q' : ℤ) (hpq : p + n = q) (hq' : q' = q) : γ.v p q hpq ≫ (HomologicalComplex.XIsoOfEq G hq').inv = γ.v p q' (by rw [hq', hpq]) := by subst hq' simp only [HomologicalComplex.XIsoOfEq, eqToIso_refl, Iso.refl_inv, comp_id] /-- The composition of cochains. -/ def comp {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : Cochain F K n₁₂ := Cochain.mk (fun p q hpq => z₁.v p (p + n₁) rfl ≫ z₂.v (p + n₁) q (by omega)) /-! If `z₁` is a cochain of degree `n₁` and `z₂` is a cochain of degree `n₂`, and that we have a relation `h : n₁ + n₂ = n₁₂`, then `z₁.comp z₂ h` is a cochain of degree `n₁₂`. The following lemma `comp_v` computes the value of this composition `z₁.comp z₂ h` on a triplet `⟨p₁, p₃, _⟩` (with `p₁ + n₁₂ = p₃`). In order to use this lemma, we need to provide an intermediate integer `p₂` such that `p₁ + n₁ = p₂`. It is advisable to use a `p₂` that has good definitional properties (i.e. `p₁ + n₁` is not always the best choice.) When `z₁` or `z₂` is a `0`-cochain, there is a better choice of `p₂`, and this leads to the two simplification lemmas `comp_zero_cochain_v` and `zero_cochain_comp_v`. -/ lemma comp_v {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) (p₁ p₂ p₃ : ℤ) (h₁ : p₁ + n₁ = p₂) (h₂ : p₂ + n₂ = p₃) : (z₁.comp z₂ h).v p₁ p₃ (by rw [← h₂, ← h₁, ← h, add_assoc]) = z₁.v p₁ p₂ h₁ ≫ z₂.v p₂ p₃ h₂ := by subst h₁; rfl @[simp] lemma comp_zero_cochain_v (z₁ : Cochain F G n) (z₂ : Cochain G K 0) (p q : ℤ) (hpq : p + n = q) : (z₁.comp z₂ (add_zero n)).v p q hpq = z₁.v p q hpq ≫ z₂.v q q (add_zero q) := comp_v z₁ z₂ (add_zero n) p q q hpq (add_zero q) @[simp] lemma zero_cochain_comp_v (z₁ : Cochain F G 0) (z₂ : Cochain G K n) (p q : ℤ) (hpq : p + n = q) : (z₁.comp z₂ (zero_add n)).v p q hpq = z₁.v p p (add_zero p) ≫ z₂.v p q hpq := comp_v z₁ z₂ (zero_add n) p p q (add_zero p) hpq /-- The associativity of the composition of cochains. -/ lemma comp_assoc {n₁ n₂ n₃ n₁₂ n₂₃ n₁₂₃ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (z₃ : Cochain K L n₃) (h₁₂ : n₁ + n₂ = n₁₂) (h₂₃ : n₂ + n₃ = n₂₃) (h₁₂₃ : n₁ + n₂ + n₃ = n₁₂₃) : (z₁.comp z₂ h₁₂).comp z₃ (show n₁₂ + n₃ = n₁₂₃ by rw [← h₁₂, h₁₂₃]) = z₁.comp (z₂.comp z₃ h₂₃) (by rw [← h₂₃, ← h₁₂₃, add_assoc]) := by substs h₁₂ h₂₃ h₁₂₃ ext p q hpq rw [comp_v _ _ rfl p (p + n₁ + n₂) q (add_assoc _ _ _).symm (by omega), comp_v z₁ z₂ rfl p (p + n₁) (p + n₁ + n₂) (by omega) (by omega), comp_v z₁ (z₂.comp z₃ rfl) (add_assoc n₁ n₂ n₃).symm p (p + n₁) q (by omega) (by omega), comp_v z₂ z₃ rfl (p + n₁) (p + n₁ + n₂) q (by omega) (by omega), assoc] /-! The formulation of the associativity of the composition of cochains given by the lemma `comp_assoc` often requires a careful selection of degrees with good definitional properties. In a few cases, like when one of the three cochains is a `0`-cochain, there are better choices, which provides the following simplification lemmas. -/ @[simp] lemma comp_assoc_of_first_is_zero_cochain {n₂ n₃ n₂₃ : ℤ} (z₁ : Cochain F G 0) (z₂ : Cochain G K n₂) (z₃ : Cochain K L n₃) (h₂₃ : n₂ + n₃ = n₂₃) : (z₁.comp z₂ (zero_add n₂)).comp z₃ h₂₃ = z₁.comp (z₂.comp z₃ h₂₃) (zero_add n₂₃) := comp_assoc _ _ _ _ _ (by omega) @[simp] lemma comp_assoc_of_second_is_zero_cochain {n₁ n₃ n₁₃ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K 0) (z₃ : Cochain K L n₃) (h₁₃ : n₁ + n₃ = n₁₃) : (z₁.comp z₂ (add_zero n₁)).comp z₃ h₁₃ = z₁.comp (z₂.comp z₃ (zero_add n₃)) h₁₃ := comp_assoc _ _ _ _ _ (by omega) @[simp] lemma comp_assoc_of_third_is_zero_cochain {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (z₃ : Cochain K L 0) (h₁₂ : n₁ + n₂ = n₁₂) : (z₁.comp z₂ h₁₂).comp z₃ (add_zero n₁₂) = z₁.comp (z₂.comp z₃ (add_zero n₂)) h₁₂ := comp_assoc _ _ _ _ _ (by omega) @[simp] lemma comp_assoc_of_second_degree_eq_neg_third_degree {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K (-n₂)) (z₃ : Cochain K L n₂) (h₁₂ : n₁ + (-n₂) = n₁₂) : (z₁.comp z₂ h₁₂).comp z₃ (show n₁₂ + n₂ = n₁ by rw [← h₁₂, add_assoc, neg_add_cancel, add_zero]) = z₁.comp (z₂.comp z₃ (neg_add_cancel n₂)) (add_zero n₁) := comp_assoc _ _ _ _ _ (by omega) @[simp] protected lemma zero_comp {n₁ n₂ n₁₂ : ℤ} (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (0 : Cochain F G n₁).comp z₂ h = 0 := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), zero_v, zero_comp] @[simp] protected lemma add_comp {n₁ n₂ n₁₂ : ℤ} (z₁ z₁' : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (z₁ + z₁').comp z₂ h = z₁.comp z₂ h + z₁'.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), add_v, add_comp] @[simp] protected lemma sub_comp {n₁ n₂ n₁₂ : ℤ} (z₁ z₁' : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (z₁ - z₁').comp z₂ h = z₁.comp z₂ h - z₁'.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), sub_v, sub_comp] @[simp] protected lemma neg_comp {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (-z₁).comp z₂ h = -z₁.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), neg_v, neg_comp] @[simp] protected lemma smul_comp {n₁ n₂ n₁₂ : ℤ} (k : R) (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (k • z₁).comp z₂ h = k • (z₁.comp z₂ h) := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), smul_v, Linear.smul_comp] @[simp] lemma units_smul_comp {n₁ n₂ n₁₂ : ℤ} (k : Rˣ) (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (k • z₁).comp z₂ h = k • (z₁.comp z₂ h) := by apply Cochain.smul_comp @[simp] protected lemma id_comp {n : ℤ} (z₂ : Cochain F G n) : (Cochain.ofHom (𝟙 F)).comp z₂ (zero_add n) = z₂ := by ext p q hpq simp only [zero_cochain_comp_v, ofHom_v, HomologicalComplex.id_f, id_comp] @[simp] protected lemma comp_zero {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (h : n₁ + n₂ = n₁₂) : z₁.comp (0 : Cochain G K n₂) h = 0 := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), zero_v, comp_zero] @[simp] protected lemma comp_add {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ z₂' : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : z₁.comp (z₂ + z₂') h = z₁.comp z₂ h + z₁.comp z₂' h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), add_v, comp_add] @[simp] protected lemma comp_sub {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ z₂' : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : z₁.comp (z₂ - z₂') h = z₁.comp z₂ h - z₁.comp z₂' h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), sub_v, comp_sub] @[simp] protected lemma comp_neg {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : z₁.comp (-z₂) h = -z₁.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), neg_v, comp_neg] @[simp] protected lemma comp_smul {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (k : R) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂ ) : z₁.comp (k • z₂) h = k • (z₁.comp z₂ h) := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), smul_v, Linear.comp_smul] @[simp] lemma comp_units_smul {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (k : Rˣ) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂ ) : z₁.comp (k • z₂) h = k • (z₁.comp z₂ h) := by apply Cochain.comp_smul @[simp] protected lemma comp_id {n : ℤ} (z₁ : Cochain F G n) : z₁.comp (Cochain.ofHom (𝟙 G)) (add_zero n) = z₁ := by ext p q hpq simp only [comp_zero_cochain_v, ofHom_v, HomologicalComplex.id_f, comp_id] @[simp] lemma ofHoms_comp (φ : ∀ (p : ℤ), F.X p ⟶ G.X p) (ψ : ∀ (p : ℤ), G.X p ⟶ K.X p) : (ofHoms φ).comp (ofHoms ψ) (zero_add 0) = ofHoms (fun p => φ p ≫ ψ p) := by aesop_cat @[simp] lemma ofHom_comp (f : F ⟶ G) (g : G ⟶ K) : ofHom (f ≫ g) = (ofHom f).comp (ofHom g) (zero_add 0) := by simp only [ofHom, HomologicalComplex.comp_f, ofHoms_comp] variable (K) /-- The differential on a cochain complex, as a cochain of degree `1`. -/ def diff : Cochain K K 1 := Cochain.mk (fun p q _ => K.d p q) @[simp] lemma diff_v (p q : ℤ) (hpq : p + 1 = q) : (diff K).v p q hpq = K.d p q := rfl end Cochain variable {F G} /-- The differential on the complex of morphisms between cochain complexes. -/ def δ (z : Cochain F G n) : Cochain F G m := Cochain.mk (fun p q hpq => z.v p (p + n) rfl ≫ G.d (p + n) q + m.negOnePow • F.d p (p + m - n) ≫ z.v (p + m - n) q (by rw [hpq, sub_add_cancel])) /-! Similarly as for the composition of cochains, if `z : Cochain F G n`, we usually need to carefully select intermediate indices with good definitional properties in order to obtain a suitable expansion of the morphisms which constitute `δ n m z : Cochain F G m` (when `n + 1 = m`, otherwise it shall be zero). The basic equational lemma is `δ_v` below. -/ lemma δ_v (hnm : n + 1 = m) (z : Cochain F G n) (p q : ℤ) (hpq : p + m = q) (q₁ q₂ : ℤ) (hq₁ : q₁ = q - 1) (hq₂ : p + 1 = q₂) : (δ n m z).v p q hpq = z.v p q₁ (by rw [hq₁, ← hpq, ← hnm, ← add_assoc, add_sub_cancel_right]) ≫ G.d q₁ q + m.negOnePow • F.d p q₂ ≫ z.v q₂ q (by rw [← hq₂, add_assoc, add_comm 1, hnm, hpq]) := by obtain rfl : q₁ = p + n := by omega obtain rfl : q₂ = p + m - n := by omega rfl lemma δ_shape (hnm : ¬ n + 1 = m) (z : Cochain F G n) : δ n m z = 0 := by ext p q hpq dsimp only [δ] rw [Cochain.mk_v, Cochain.zero_v, F.shape, G.shape, comp_zero, zero_add, zero_comp, smul_zero] all_goals simp only [ComplexShape.up_Rel] exact fun _ => hnm (by omega) variable (F G) (R) /-- The differential on the complex of morphisms between cochain complexes, as a linear map. -/ @[simps!] def δ_hom : Cochain F G n →ₗ[R] Cochain F G m where toFun := δ n m map_add' α β := by by_cases h : n + 1 = m · ext p q hpq dsimp simp only [δ_v n m h _ p q hpq _ _ rfl rfl, Cochain.add_v, add_comp, comp_add, smul_add] abel · simp only [δ_shape _ _ h, add_zero] map_smul' r a := by by_cases h : n + 1 = m · ext p q hpq dsimp simp only [δ_v n m h _ p q hpq _ _ rfl rfl, Cochain.smul_v, Linear.comp_smul, Linear.smul_comp, smul_add, add_right_inj, smul_comm m.negOnePow r] · simp only [δ_shape _ _ h, smul_zero] variable {F G R} @[simp] lemma δ_add (z₁ z₂ : Cochain F G n) : δ n m (z₁ + z₂) = δ n m z₁ + δ n m z₂ := (δ_hom ℤ F G n m).map_add z₁ z₂ @[simp] lemma δ_sub (z₁ z₂ : Cochain F G n) : δ n m (z₁ - z₂) = δ n m z₁ - δ n m z₂ := (δ_hom ℤ F G n m).map_sub z₁ z₂ @[simp] lemma δ_zero : δ n m (0 : Cochain F G n) = 0 := (δ_hom ℤ F G n m).map_zero @[simp] lemma δ_neg (z : Cochain F G n) : δ n m (-z) = - δ n m z := (δ_hom ℤ F G n m).map_neg z @[simp] lemma δ_smul (k : R) (z : Cochain F G n) : δ n m (k • z) = k • δ n m z := (δ_hom R F G n m).map_smul k z @[simp] lemma δ_units_smul (k : Rˣ) (z : Cochain F G n) : δ n m (k • z) = k • δ n m z := δ_smul .. lemma δ_δ (n₀ n₁ n₂ : ℤ) (z : Cochain F G n₀) : δ n₁ n₂ (δ n₀ n₁ z) = 0 := by by_cases h₁₂ : n₁ + 1 = n₂; swap · rw [δ_shape _ _ h₁₂] by_cases h₀₁ : n₀ + 1 = n₁; swap · rw [δ_shape _ _ h₀₁, δ_zero] ext p q hpq dsimp simp only [δ_v n₁ n₂ h₁₂ _ p q hpq _ _ rfl rfl, δ_v n₀ n₁ h₀₁ z p (q-1) (by omega) (q-2) _ (by omega) rfl, δ_v n₀ n₁ h₀₁ z (p+1) q (by omega) _ (p+2) rfl (by omega), ← h₁₂, Int.negOnePow_succ, add_comp, assoc, HomologicalComplex.d_comp_d, comp_zero, zero_add, comp_add, HomologicalComplex.d_comp_d_assoc, zero_comp, smul_zero, add_zero, add_neg_cancel, Units.neg_smul, Linear.units_smul_comp, Linear.comp_units_smul] lemma δ_comp {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) (m₁ m₂ m₁₂ : ℤ) (h₁₂ : n₁₂ + 1 = m₁₂) (h₁ : n₁ + 1 = m₁) (h₂ : n₂ + 1 = m₂) : δ n₁₂ m₁₂ (z₁.comp z₂ h) = z₁.comp (δ n₂ m₂ z₂) (by rw [← h₁₂, ← h₂, ← h, add_assoc]) + n₂.negOnePow • (δ n₁ m₁ z₁).comp z₂ (by rw [← h₁₂, ← h₁, ← h, add_assoc, add_comm 1, add_assoc]) := by subst h₁₂ h₁ h₂ h ext p q hpq dsimp rw [z₁.comp_v _ (add_assoc n₁ n₂ 1).symm p _ q rfl (by omega), Cochain.comp_v _ _ (show n₁ + 1 + n₂ = n₁ + n₂ + 1 by omega) p (p+n₁+1) q (by omega) (by omega), δ_v (n₁ + n₂) _ rfl (z₁.comp z₂ rfl) p q hpq (p + n₁ + n₂) _ (by omega) rfl, z₁.comp_v z₂ rfl p _ _ rfl rfl, z₁.comp_v z₂ rfl (p+1) (p+n₁+1) q (by omega) (by omega), δ_v n₂ (n₂+1) rfl z₂ (p+n₁) q (by omega) (p+n₁+n₂) _ (by omega) rfl, δ_v n₁ (n₁+1) rfl z₁ p (p+n₁+1) (by omega) (p+n₁) _ (by omega) rfl] simp only [assoc, comp_add, add_comp, Int.negOnePow_succ, Int.negOnePow_add n₁ n₂, Units.neg_smul, comp_neg, neg_comp, smul_neg, smul_smul, Linear.units_smul_comp, mul_comm n₁.negOnePow n₂.negOnePow, Linear.comp_units_smul, smul_add] abel lemma δ_zero_cochain_comp {n₂ : ℤ} (z₁ : Cochain F G 0) (z₂ : Cochain G K n₂) (m₂ : ℤ) (h₂ : n₂ + 1 = m₂) : δ n₂ m₂ (z₁.comp z₂ (zero_add n₂)) = z₁.comp (δ n₂ m₂ z₂) (zero_add m₂) + n₂.negOnePow • ((δ 0 1 z₁).comp z₂ (by rw [add_comm, h₂])) := δ_comp z₁ z₂ (zero_add n₂) 1 m₂ m₂ h₂ (zero_add 1) h₂ lemma δ_comp_zero_cochain {n₁ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K 0) (m₁ : ℤ) (h₁ : n₁ + 1 = m₁) : δ n₁ m₁ (z₁.comp z₂ (add_zero n₁)) = z₁.comp (δ 0 1 z₂) h₁ + (δ n₁ m₁ z₁).comp z₂ (add_zero m₁) := by simp only [δ_comp z₁ z₂ (add_zero n₁) m₁ 1 m₁ h₁ h₁ (zero_add 1), one_smul, Int.negOnePow_zero] @[simp] lemma δ_zero_cochain_v (z : Cochain F G 0) (p q : ℤ) (hpq : p + 1 = q) : (δ 0 1 z).v p q hpq = z.v p p (add_zero p) ≫ G.d p q - F.d p q ≫ z.v q q (add_zero q) := by simp only [δ_v 0 1 (zero_add 1) z p q hpq p q (by omega) hpq, zero_add, Int.negOnePow_one, Units.neg_smul, one_smul, sub_eq_add_neg] @[simp] lemma δ_ofHom {p : ℤ} (φ : F ⟶ G) : δ 0 p (Cochain.ofHom φ) = 0 := by by_cases h : p = 1 · subst h ext simp · rw [δ_shape] omega
@[simp] lemma δ_ofHomotopy {φ₁ φ₂ : F ⟶ G} (h : Homotopy φ₁ φ₂) : δ (-1) 0 (Cochain.ofHomotopy h) = Cochain.ofHom φ₁ - Cochain.ofHom φ₂ := by ext p have eq := h.comm p rw [dNext_eq h.hom (show (ComplexShape.up ℤ).Rel p (p+1) by simp), prevD_eq h.hom (show (ComplexShape.up ℤ).Rel (p-1) p by simp)] at eq rw [Cochain.ofHomotopy, δ_v (-1) 0 (neg_add_cancel 1) _ p p (add_zero p) (p-1) (p+1) rfl rfl] simp only [Cochain.mk_v, neg_add_cancel, one_smul, Int.negOnePow_zero, Cochain.sub_v, Cochain.ofHom_v, eq]
Mathlib/Algebra/Homology/HomotopyCategory/HomComplex.lean
535
545
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl, Patrick Massot -/ import Mathlib.Data.Set.Image import Mathlib.Data.SProd /-! # Sets in product and pi types This file proves basic properties of product of sets in `α × β` and in `Π i, α i`, and of the diagonal of a type. ## Main declarations This file contains basic results on the following notions, which are defined in `Set.Operations`. * `Set.prod`: Binary product of sets. For `s : Set α`, `t : Set β`, we have `s.prod t : Set (α × β)`. Denoted by `s ×ˢ t`. * `Set.diagonal`: Diagonal of a type. `Set.diagonal α = {(x, x) | x : α}`. * `Set.offDiag`: Off-diagonal. `s ×ˢ s` without the diagonal. * `Set.pi`: Arbitrary product of sets. -/ open Function namespace Set /-! ### Cartesian binary product of sets -/ section Prod variable {α β γ δ : Type*} {s s₁ s₂ : Set α} {t t₁ t₂ : Set β} {a : α} {b : β} theorem Subsingleton.prod (hs : s.Subsingleton) (ht : t.Subsingleton) : (s ×ˢ t).Subsingleton := fun _x hx _y hy ↦ Prod.ext (hs hx.1 hy.1) (ht hx.2 hy.2) noncomputable instance decidableMemProd [DecidablePred (· ∈ s)] [DecidablePred (· ∈ t)] : DecidablePred (· ∈ s ×ˢ t) := fun x => inferInstanceAs (Decidable (x.1 ∈ s ∧ x.2 ∈ t)) @[gcongr] theorem prod_mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁ ×ˢ t₁ ⊆ s₂ ×ˢ t₂ := fun _ ⟨h₁, h₂⟩ => ⟨hs h₁, ht h₂⟩ @[gcongr] theorem prod_mono_left (hs : s₁ ⊆ s₂) : s₁ ×ˢ t ⊆ s₂ ×ˢ t := prod_mono hs Subset.rfl @[gcongr] theorem prod_mono_right (ht : t₁ ⊆ t₂) : s ×ˢ t₁ ⊆ s ×ˢ t₂ := prod_mono Subset.rfl ht @[simp] theorem prod_self_subset_prod_self : s₁ ×ˢ s₁ ⊆ s₂ ×ˢ s₂ ↔ s₁ ⊆ s₂ := ⟨fun h _ hx => (h (mk_mem_prod hx hx)).1, fun h _ hx => ⟨h hx.1, h hx.2⟩⟩ @[simp] theorem prod_self_ssubset_prod_self : s₁ ×ˢ s₁ ⊂ s₂ ×ˢ s₂ ↔ s₁ ⊂ s₂ := and_congr prod_self_subset_prod_self <| not_congr prod_self_subset_prod_self theorem prod_subset_iff {P : Set (α × β)} : s ×ˢ t ⊆ P ↔ ∀ x ∈ s, ∀ y ∈ t, (x, y) ∈ P := ⟨fun h _ hx _ hy => h (mk_mem_prod hx hy), fun h ⟨_, _⟩ hp => h _ hp.1 _ hp.2⟩ theorem forall_prod_set {p : α × β → Prop} : (∀ x ∈ s ×ˢ t, p x) ↔ ∀ x ∈ s, ∀ y ∈ t, p (x, y) := prod_subset_iff theorem exists_prod_set {p : α × β → Prop} : (∃ x ∈ s ×ˢ t, p x) ↔ ∃ x ∈ s, ∃ y ∈ t, p (x, y) := by simp [and_assoc] @[simp] theorem prod_empty : s ×ˢ (∅ : Set β) = ∅ := by ext exact iff_of_eq (and_false _) @[simp] theorem empty_prod : (∅ : Set α) ×ˢ t = ∅ := by ext exact iff_of_eq (false_and _) @[simp, mfld_simps] theorem univ_prod_univ : @univ α ×ˢ @univ β = univ := by ext exact iff_of_eq (true_and _) theorem univ_prod {t : Set β} : (univ : Set α) ×ˢ t = Prod.snd ⁻¹' t := by simp [prod_eq] theorem prod_univ {s : Set α} : s ×ˢ (univ : Set β) = Prod.fst ⁻¹' s := by simp [prod_eq] @[simp] lemma prod_eq_univ [Nonempty α] [Nonempty β] : s ×ˢ t = univ ↔ s = univ ∧ t = univ := by simp [eq_univ_iff_forall, forall_and] theorem singleton_prod : ({a} : Set α) ×ˢ t = Prod.mk a '' t := by ext ⟨x, y⟩ simp [and_left_comm, eq_comm] theorem prod_singleton : s ×ˢ ({b} : Set β) = (fun a => (a, b)) '' s := by ext ⟨x, y⟩ simp [and_left_comm, eq_comm] @[simp] theorem singleton_prod_singleton : ({a} : Set α) ×ˢ ({b} : Set β) = {(a, b)} := by ext ⟨c, d⟩; simp @[simp] theorem union_prod : (s₁ ∪ s₂) ×ˢ t = s₁ ×ˢ t ∪ s₂ ×ˢ t := by ext ⟨x, y⟩ simp [or_and_right] @[simp] theorem prod_union : s ×ˢ (t₁ ∪ t₂) = s ×ˢ t₁ ∪ s ×ˢ t₂ := by ext ⟨x, y⟩ simp [and_or_left] theorem inter_prod : (s₁ ∩ s₂) ×ˢ t = s₁ ×ˢ t ∩ s₂ ×ˢ t := by ext ⟨x, y⟩ simp only [← and_and_right, mem_inter_iff, mem_prod] theorem prod_inter : s ×ˢ (t₁ ∩ t₂) = s ×ˢ t₁ ∩ s ×ˢ t₂ := by ext ⟨x, y⟩ simp only [← and_and_left, mem_inter_iff, mem_prod] @[mfld_simps] theorem prod_inter_prod : s₁ ×ˢ t₁ ∩ s₂ ×ˢ t₂ = (s₁ ∩ s₂) ×ˢ (t₁ ∩ t₂) := by ext ⟨x, y⟩ simp [and_assoc, and_left_comm] lemma compl_prod_eq_union {α β : Type*} (s : Set α) (t : Set β) : (s ×ˢ t)ᶜ = (sᶜ ×ˢ univ) ∪ (univ ×ˢ tᶜ) := by ext p simp only [mem_compl_iff, mem_prod, not_and, mem_union, mem_univ, and_true, true_and] constructor <;> intro h · by_cases fst_in_s : p.fst ∈ s · exact Or.inr (h fst_in_s) · exact Or.inl fst_in_s · intro fst_in_s simpa only [fst_in_s, not_true, false_or] using h @[simp] theorem disjoint_prod : Disjoint (s₁ ×ˢ t₁) (s₂ ×ˢ t₂) ↔ Disjoint s₁ s₂ ∨ Disjoint t₁ t₂ := by simp_rw [disjoint_left, mem_prod, not_and_or, Prod.forall, and_imp, ← @forall_or_right α, ← @forall_or_left β, ← @forall_or_right (_ ∈ s₁), ← @forall_or_left (_ ∈ t₁)] theorem Disjoint.set_prod_left (hs : Disjoint s₁ s₂) (t₁ t₂ : Set β) : Disjoint (s₁ ×ˢ t₁) (s₂ ×ˢ t₂) := disjoint_left.2 fun ⟨_a, _b⟩ ⟨ha₁, _⟩ ⟨ha₂, _⟩ => disjoint_left.1 hs ha₁ ha₂ theorem Disjoint.set_prod_right (ht : Disjoint t₁ t₂) (s₁ s₂ : Set α) : Disjoint (s₁ ×ˢ t₁) (s₂ ×ˢ t₂) := disjoint_left.2 fun ⟨_a, _b⟩ ⟨_, hb₁⟩ ⟨_, hb₂⟩ => disjoint_left.1 ht hb₁ hb₂ theorem prodMap_image_prod (f : α → β) (g : γ → δ) (s : Set α) (t : Set γ) : (Prod.map f g) '' (s ×ˢ t) = (f '' s) ×ˢ (g '' t) := by ext aesop theorem insert_prod : insert a s ×ˢ t = Prod.mk a '' t ∪ s ×ˢ t := by simp only [insert_eq, union_prod, singleton_prod] theorem prod_insert : s ×ˢ insert b t = (fun a => (a, b)) '' s ∪ s ×ˢ t := by simp only [insert_eq, prod_union, prod_singleton] theorem prod_preimage_eq {f : γ → α} {g : δ → β} : (f ⁻¹' s) ×ˢ (g ⁻¹' t) = (fun p : γ × δ => (f p.1, g p.2)) ⁻¹' s ×ˢ t := rfl theorem prod_preimage_left {f : γ → α} : (f ⁻¹' s) ×ˢ t = (fun p : γ × β => (f p.1, p.2)) ⁻¹' s ×ˢ t := rfl theorem prod_preimage_right {g : δ → β} : s ×ˢ (g ⁻¹' t) = (fun p : α × δ => (p.1, g p.2)) ⁻¹' s ×ˢ t := rfl theorem preimage_prod_map_prod (f : α → β) (g : γ → δ) (s : Set β) (t : Set δ) : Prod.map f g ⁻¹' s ×ˢ t = (f ⁻¹' s) ×ˢ (g ⁻¹' t) := rfl theorem mk_preimage_prod (f : γ → α) (g : γ → β) : (fun x => (f x, g x)) ⁻¹' s ×ˢ t = f ⁻¹' s ∩ g ⁻¹' t := rfl @[simp] theorem mk_preimage_prod_left (hb : b ∈ t) : (fun a => (a, b)) ⁻¹' s ×ˢ t = s := by ext a simp [hb] @[simp] theorem mk_preimage_prod_right (ha : a ∈ s) : Prod.mk a ⁻¹' s ×ˢ t = t := by ext b simp [ha] @[simp] theorem mk_preimage_prod_left_eq_empty (hb : b ∉ t) : (fun a => (a, b)) ⁻¹' s ×ˢ t = ∅ := by ext a simp [hb] @[simp] theorem mk_preimage_prod_right_eq_empty (ha : a ∉ s) : Prod.mk a ⁻¹' s ×ˢ t = ∅ := by ext b simp [ha] theorem mk_preimage_prod_left_eq_if [DecidablePred (· ∈ t)] : (fun a => (a, b)) ⁻¹' s ×ˢ t = if b ∈ t then s else ∅ := by split_ifs with h <;> simp [h] theorem mk_preimage_prod_right_eq_if [DecidablePred (· ∈ s)] : Prod.mk a ⁻¹' s ×ˢ t = if a ∈ s then t else ∅ := by split_ifs with h <;> simp [h] theorem mk_preimage_prod_left_fn_eq_if [DecidablePred (· ∈ t)] (f : γ → α) : (fun a => (f a, b)) ⁻¹' s ×ˢ t = if b ∈ t then f ⁻¹' s else ∅ := by rw [← mk_preimage_prod_left_eq_if, prod_preimage_left, preimage_preimage] theorem mk_preimage_prod_right_fn_eq_if [DecidablePred (· ∈ s)] (g : δ → β) : (fun b => (a, g b)) ⁻¹' s ×ˢ t = if a ∈ s then g ⁻¹' t else ∅ := by rw [← mk_preimage_prod_right_eq_if, prod_preimage_right, preimage_preimage] @[simp] theorem preimage_swap_prod (s : Set α) (t : Set β) : Prod.swap ⁻¹' s ×ˢ t = t ×ˢ s := by ext ⟨x, y⟩ simp [and_comm] @[simp] theorem image_swap_prod (s : Set α) (t : Set β) : Prod.swap '' s ×ˢ t = t ×ˢ s := by rw [image_swap_eq_preimage_swap, preimage_swap_prod] theorem mapsTo_swap_prod (s : Set α) (t : Set β) : MapsTo Prod.swap (s ×ˢ t) (t ×ˢ s) := fun _ ⟨hx, hy⟩ ↦ ⟨hy, hx⟩ theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : (m₁ '' s) ×ˢ (m₂ '' t) = (fun p : α × β => (m₁ p.1, m₂ p.2)) '' s ×ˢ t := ext <| by simp [-exists_and_right, exists_and_right.symm, and_left_comm, and_assoc, and_comm] theorem prod_range_range_eq {m₁ : α → γ} {m₂ : β → δ} : range m₁ ×ˢ range m₂ = range fun p : α × β => (m₁ p.1, m₂ p.2) := ext <| by simp [range] @[simp, mfld_simps] theorem range_prodMap {m₁ : α → γ} {m₂ : β → δ} : range (Prod.map m₁ m₂) = range m₁ ×ˢ range m₂ := prod_range_range_eq.symm @[deprecated (since := "2025-04-10")] alias range_prod_map := range_prodMap theorem prod_range_univ_eq {m₁ : α → γ} : range m₁ ×ˢ (univ : Set β) = range fun p : α × β => (m₁ p.1, p.2) := ext <| by simp [range] theorem prod_univ_range_eq {m₂ : β → δ} : (univ : Set α) ×ˢ range m₂ = range fun p : α × β => (p.1, m₂ p.2) := ext <| by simp [range] theorem range_pair_subset (f : α → β) (g : α → γ) : (range fun x => (f x, g x)) ⊆ range f ×ˢ range g := by have : (fun x => (f x, g x)) = Prod.map f g ∘ fun x => (x, x) := funext fun x => rfl rw [this, ← range_prodMap] apply range_comp_subset_range theorem Nonempty.prod : s.Nonempty → t.Nonempty → (s ×ˢ t).Nonempty := fun ⟨x, hx⟩ ⟨y, hy⟩ => ⟨(x, y), ⟨hx, hy⟩⟩ theorem Nonempty.fst : (s ×ˢ t).Nonempty → s.Nonempty := fun ⟨x, hx⟩ => ⟨x.1, hx.1⟩ theorem Nonempty.snd : (s ×ˢ t).Nonempty → t.Nonempty := fun ⟨x, hx⟩ => ⟨x.2, hx.2⟩
@[simp] theorem prod_nonempty_iff : (s ×ˢ t).Nonempty ↔ s.Nonempty ∧ t.Nonempty := ⟨fun h => ⟨h.fst, h.snd⟩, fun h => h.1.prod h.2⟩
Mathlib/Data/Set/Prod.lean
267
269
/- 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, Yury Kudryashov -/ import Mathlib.Data.Set.SymmDiff import Mathlib.Order.SuccPred.Relation import Mathlib.Topology.Irreducible /-! # Connected subsets of topological spaces In this file we define connected subsets of a topological spaces and various other properties and classes related to connectivity. ## Main definitions We define the following properties for sets in a topological space: * `IsConnected`: a nonempty set that has no non-trivial open partition. See also the section below in the module doc. * `connectedComponent` is the connected component of an element in the space. We also have a class stating that the whole space satisfies that property: `ConnectedSpace` ## On the definition of connected sets/spaces In informal mathematics, connected spaces are assumed to be nonempty. We formalise the predicate without that assumption as `IsPreconnected`. In other words, the only difference is whether the empty space counts as connected. There are good reasons to consider the empty space to be “too simple to be simple” See also https://ncatlab.org/nlab/show/too+simple+to+be+simple, and in particular https://ncatlab.org/nlab/show/too+simple+to+be+simple#relationship_to_biased_definitions. -/ open Set Function Topology TopologicalSpace Relation universe u v variable {α : Type u} {β : Type v} {ι : Type*} {π : ι → Type*} [TopologicalSpace α] {s t u v : Set α} section Preconnected /-- A preconnected set is one where there is no non-trivial open partition. -/ def IsPreconnected (s : Set α) : Prop := ∀ u v : Set α, IsOpen u → IsOpen v → s ⊆ u ∪ v → (s ∩ u).Nonempty → (s ∩ v).Nonempty → (s ∩ (u ∩ v)).Nonempty /-- A connected set is one that is nonempty and where there is no non-trivial open partition. -/ def IsConnected (s : Set α) : Prop := s.Nonempty ∧ IsPreconnected s theorem IsConnected.nonempty {s : Set α} (h : IsConnected s) : s.Nonempty := h.1 theorem IsConnected.isPreconnected {s : Set α} (h : IsConnected s) : IsPreconnected s := h.2 theorem IsPreirreducible.isPreconnected {s : Set α} (H : IsPreirreducible s) : IsPreconnected s := fun _ _ hu hv _ => H _ _ hu hv theorem IsIrreducible.isConnected {s : Set α} (H : IsIrreducible s) : IsConnected s := ⟨H.nonempty, H.isPreirreducible.isPreconnected⟩ theorem isPreconnected_empty : IsPreconnected (∅ : Set α) := isPreirreducible_empty.isPreconnected theorem isConnected_singleton {x} : IsConnected ({x} : Set α) := isIrreducible_singleton.isConnected theorem isPreconnected_singleton {x} : IsPreconnected ({x} : Set α) := isConnected_singleton.isPreconnected theorem Set.Subsingleton.isPreconnected {s : Set α} (hs : s.Subsingleton) : IsPreconnected s := hs.induction_on isPreconnected_empty fun _ => isPreconnected_singleton /-- If any point of a set is joined to a fixed point by a preconnected subset, then the original set is preconnected as well. -/ theorem isPreconnected_of_forall {s : Set α} (x : α) (H : ∀ y ∈ s, ∃ t, t ⊆ s ∧ x ∈ t ∧ y ∈ t ∧ IsPreconnected t) : IsPreconnected s := by rintro u v hu hv hs ⟨z, zs, zu⟩ ⟨y, ys, yv⟩ have xs : x ∈ s := by rcases H y ys with ⟨t, ts, xt, -, -⟩ exact ts xt -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: use `wlog xu : x ∈ u := hs xs using u v y z, v u z y` cases hs xs with | inl xu => rcases H y ys with ⟨t, ts, xt, yt, ht⟩ have := ht u v hu hv (ts.trans hs) ⟨x, xt, xu⟩ ⟨y, yt, yv⟩ exact this.imp fun z hz => ⟨ts hz.1, hz.2⟩ | inr xv => rcases H z zs with ⟨t, ts, xt, zt, ht⟩ have := ht v u hv hu (ts.trans <| by rwa [union_comm]) ⟨x, xt, xv⟩ ⟨z, zt, zu⟩ exact this.imp fun _ h => ⟨ts h.1, h.2.2, h.2.1⟩ /-- If any two points of a set are contained in a preconnected subset, then the original set is preconnected as well. -/ theorem isPreconnected_of_forall_pair {s : Set α} (H : ∀ x ∈ s, ∀ y ∈ s, ∃ t, t ⊆ s ∧ x ∈ t ∧ y ∈ t ∧ IsPreconnected t) : IsPreconnected s := by rcases eq_empty_or_nonempty s with (rfl | ⟨x, hx⟩) exacts [isPreconnected_empty, isPreconnected_of_forall x fun y => H x hx y] /-- A union of a family of preconnected sets with a common point is preconnected as well. -/ theorem isPreconnected_sUnion (x : α) (c : Set (Set α)) (H1 : ∀ s ∈ c, x ∈ s) (H2 : ∀ s ∈ c, IsPreconnected s) : IsPreconnected (⋃₀ c) := by apply isPreconnected_of_forall x rintro y ⟨s, sc, ys⟩ exact ⟨s, subset_sUnion_of_mem sc, H1 s sc, ys, H2 s sc⟩ theorem isPreconnected_iUnion {ι : Sort*} {s : ι → Set α} (h₁ : (⋂ i, s i).Nonempty) (h₂ : ∀ i, IsPreconnected (s i)) : IsPreconnected (⋃ i, s i) := Exists.elim h₁ fun f hf => isPreconnected_sUnion f _ hf (forall_mem_range.2 h₂) theorem IsPreconnected.union (x : α) {s t : Set α} (H1 : x ∈ s) (H2 : x ∈ t) (H3 : IsPreconnected s) (H4 : IsPreconnected t) : IsPreconnected (s ∪ t) := sUnion_pair s t ▸ isPreconnected_sUnion x {s, t} (by rintro r (rfl | rfl | h) <;> assumption) (by rintro r (rfl | rfl | h) <;> assumption) theorem IsPreconnected.union' {s t : Set α} (H : (s ∩ t).Nonempty) (hs : IsPreconnected s) (ht : IsPreconnected t) : IsPreconnected (s ∪ t) := by rcases H with ⟨x, hxs, hxt⟩ exact hs.union x hxs hxt ht theorem IsConnected.union {s t : Set α} (H : (s ∩ t).Nonempty) (Hs : IsConnected s) (Ht : IsConnected t) : IsConnected (s ∪ t) := by rcases H with ⟨x, hx⟩ refine ⟨⟨x, mem_union_left t (mem_of_mem_inter_left hx)⟩, ?_⟩ exact Hs.isPreconnected.union x (mem_of_mem_inter_left hx) (mem_of_mem_inter_right hx) Ht.isPreconnected /-- The directed sUnion of a set S of preconnected subsets is preconnected. -/ theorem IsPreconnected.sUnion_directed {S : Set (Set α)} (K : DirectedOn (· ⊆ ·) S) (H : ∀ s ∈ S, IsPreconnected s) : IsPreconnected (⋃₀ S) := by rintro u v hu hv Huv ⟨a, ⟨s, hsS, has⟩, hau⟩ ⟨b, ⟨t, htS, hbt⟩, hbv⟩ obtain ⟨r, hrS, hsr, htr⟩ : ∃ r ∈ S, s ⊆ r ∧ t ⊆ r := K s hsS t htS have Hnuv : (r ∩ (u ∩ v)).Nonempty := H _ hrS u v hu hv ((subset_sUnion_of_mem hrS).trans Huv) ⟨a, hsr has, hau⟩ ⟨b, htr hbt, hbv⟩ have Kruv : r ∩ (u ∩ v) ⊆ ⋃₀ S ∩ (u ∩ v) := inter_subset_inter_left _ (subset_sUnion_of_mem hrS) exact Hnuv.mono Kruv /-- The biUnion of a family of preconnected sets is preconnected if the graph determined by whether two sets intersect is preconnected. -/ theorem IsPreconnected.biUnion_of_reflTransGen {ι : Type*} {t : Set ι} {s : ι → Set α} (H : ∀ i ∈ t, IsPreconnected (s i)) (K : ∀ i, i ∈ t → ∀ j, j ∈ t → ReflTransGen (fun i j => (s i ∩ s j).Nonempty ∧ i ∈ t) i j) : IsPreconnected (⋃ n ∈ t, s n) := by let R := fun i j : ι => (s i ∩ s j).Nonempty ∧ i ∈ t have P : ∀ i, i ∈ t → ∀ j, j ∈ t → ReflTransGen R i j → ∃ p, p ⊆ t ∧ i ∈ p ∧ j ∈ p ∧ IsPreconnected (⋃ j ∈ p, s j) := fun i hi j hj h => by induction h with | refl => refine ⟨{i}, singleton_subset_iff.mpr hi, mem_singleton i, mem_singleton i, ?_⟩ rw [biUnion_singleton] exact H i hi | @tail j k _ hjk ih => obtain ⟨p, hpt, hip, hjp, hp⟩ := ih hjk.2 refine ⟨insert k p, insert_subset_iff.mpr ⟨hj, hpt⟩, mem_insert_of_mem k hip, mem_insert k p, ?_⟩ rw [biUnion_insert] refine (H k hj).union' (hjk.1.mono ?_) hp rw [inter_comm] exact inter_subset_inter_right _ (subset_biUnion_of_mem hjp) refine isPreconnected_of_forall_pair ?_ intro x hx y hy obtain ⟨i : ι, hi : i ∈ t, hxi : x ∈ s i⟩ := mem_iUnion₂.1 hx obtain ⟨j : ι, hj : j ∈ t, hyj : y ∈ s j⟩ := mem_iUnion₂.1 hy obtain ⟨p, hpt, hip, hjp, hp⟩ := P i hi j hj (K i hi j hj) exact ⟨⋃ j ∈ p, s j, biUnion_subset_biUnion_left hpt, mem_biUnion hip hxi, mem_biUnion hjp hyj, hp⟩ /-- The biUnion of a family of preconnected sets is preconnected if the graph determined by whether two sets intersect is preconnected. -/ theorem IsConnected.biUnion_of_reflTransGen {ι : Type*} {t : Set ι} {s : ι → Set α} (ht : t.Nonempty) (H : ∀ i ∈ t, IsConnected (s i)) (K : ∀ i, i ∈ t → ∀ j, j ∈ t → ReflTransGen (fun i j : ι => (s i ∩ s j).Nonempty ∧ i ∈ t) i j) : IsConnected (⋃ n ∈ t, s n) := ⟨nonempty_biUnion.2 <| ⟨ht.some, ht.some_mem, (H _ ht.some_mem).nonempty⟩, IsPreconnected.biUnion_of_reflTransGen (fun i hi => (H i hi).isPreconnected) K⟩ /-- Preconnectedness of the iUnion of a family of preconnected sets indexed by the vertices of a preconnected graph, where two vertices are joined when the corresponding sets intersect. -/ theorem IsPreconnected.iUnion_of_reflTransGen {ι : Type*} {s : ι → Set α} (H : ∀ i, IsPreconnected (s i)) (K : ∀ i j, ReflTransGen (fun i j : ι => (s i ∩ s j).Nonempty) i j) : IsPreconnected (⋃ n, s n) := by rw [← biUnion_univ] exact IsPreconnected.biUnion_of_reflTransGen (fun i _ => H i) fun i _ j _ => by simpa [mem_univ] using K i j theorem IsConnected.iUnion_of_reflTransGen {ι : Type*} [Nonempty ι] {s : ι → Set α} (H : ∀ i, IsConnected (s i)) (K : ∀ i j, ReflTransGen (fun i j : ι => (s i ∩ s j).Nonempty) i j) : IsConnected (⋃ n, s n) := ⟨nonempty_iUnion.2 <| Nonempty.elim ‹_› fun i : ι => ⟨i, (H _).nonempty⟩, IsPreconnected.iUnion_of_reflTransGen (fun i => (H i).isPreconnected) K⟩ section SuccOrder open Order variable [LinearOrder β] [SuccOrder β] [IsSuccArchimedean β] /-- The iUnion of connected sets indexed by a type with an archimedean successor (like `ℕ` or `ℤ`) such that any two neighboring sets meet is preconnected. -/ theorem IsPreconnected.iUnion_of_chain {s : β → Set α} (H : ∀ n, IsPreconnected (s n)) (K : ∀ n, (s n ∩ s (succ n)).Nonempty) : IsPreconnected (⋃ n, s n) := IsPreconnected.iUnion_of_reflTransGen H fun _ _ => reflTransGen_of_succ _ (fun i _ => K i) fun i _ => by rw [inter_comm] exact K i /-- The iUnion of connected sets indexed by a type with an archimedean successor (like `ℕ` or `ℤ`) such that any two neighboring sets meet is connected. -/ theorem IsConnected.iUnion_of_chain [Nonempty β] {s : β → Set α} (H : ∀ n, IsConnected (s n)) (K : ∀ n, (s n ∩ s (succ n)).Nonempty) : IsConnected (⋃ n, s n) := IsConnected.iUnion_of_reflTransGen H fun _ _ => reflTransGen_of_succ _ (fun i _ => K i) fun i _ => by rw [inter_comm] exact K i /-- The iUnion of preconnected sets indexed by a subset of a type with an archimedean successor (like `ℕ` or `ℤ`) such that any two neighboring sets meet is preconnected. -/ theorem IsPreconnected.biUnion_of_chain {s : β → Set α} {t : Set β} (ht : OrdConnected t) (H : ∀ n ∈ t, IsPreconnected (s n)) (K : ∀ n : β, n ∈ t → succ n ∈ t → (s n ∩ s (succ n)).Nonempty) : IsPreconnected (⋃ n ∈ t, s n) := by have h1 : ∀ {i j k : β}, i ∈ t → j ∈ t → k ∈ Ico i j → k ∈ t := fun hi hj hk => ht.out hi hj (Ico_subset_Icc_self hk) have h2 : ∀ {i j k : β}, i ∈ t → j ∈ t → k ∈ Ico i j → succ k ∈ t := fun hi hj hk => ht.out hi hj ⟨hk.1.trans <| le_succ _, succ_le_of_lt hk.2⟩ have h3 : ∀ {i j k : β}, i ∈ t → j ∈ t → k ∈ Ico i j → (s k ∩ s (succ k)).Nonempty := fun hi hj hk => K _ (h1 hi hj hk) (h2 hi hj hk) refine IsPreconnected.biUnion_of_reflTransGen H fun i hi j hj => ?_ exact reflTransGen_of_succ _ (fun k hk => ⟨h3 hi hj hk, h1 hi hj hk⟩) fun k hk => ⟨by rw [inter_comm]; exact h3 hj hi hk, h2 hj hi hk⟩ /-- The iUnion of connected sets indexed by a subset of a type with an archimedean successor (like `ℕ` or `ℤ`) such that any two neighboring sets meet is preconnected. -/ theorem IsConnected.biUnion_of_chain {s : β → Set α} {t : Set β} (hnt : t.Nonempty) (ht : OrdConnected t) (H : ∀ n ∈ t, IsConnected (s n)) (K : ∀ n : β, n ∈ t → succ n ∈ t → (s n ∩ s (succ n)).Nonempty) : IsConnected (⋃ n ∈ t, s n) := ⟨nonempty_biUnion.2 <| ⟨hnt.some, hnt.some_mem, (H _ hnt.some_mem).nonempty⟩, IsPreconnected.biUnion_of_chain ht (fun i hi => (H i hi).isPreconnected) K⟩ end SuccOrder /-- Theorem of bark and tree: if a set is within a preconnected set and its closure, then it is preconnected as well. See also `IsConnected.subset_closure`. -/ protected theorem IsPreconnected.subset_closure {s : Set α} {t : Set α} (H : IsPreconnected s) (Kst : s ⊆ t) (Ktcs : t ⊆ closure s) : IsPreconnected t := fun u v hu hv htuv ⟨_y, hyt, hyu⟩ ⟨_z, hzt, hzv⟩ => let ⟨p, hpu, hps⟩ := mem_closure_iff.1 (Ktcs hyt) u hu hyu let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 (Ktcs hzt) v hv hzv let ⟨r, hrs, hruv⟩ := H u v hu hv (Subset.trans Kst htuv) ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ ⟨r, Kst hrs, hruv⟩ /-- Theorem of bark and tree: if a set is within a connected set and its closure, then it is connected as well. See also `IsPreconnected.subset_closure`. -/ protected theorem IsConnected.subset_closure {s : Set α} {t : Set α} (H : IsConnected s) (Kst : s ⊆ t) (Ktcs : t ⊆ closure s) : IsConnected t := ⟨Nonempty.mono Kst H.left, IsPreconnected.subset_closure H.right Kst Ktcs⟩ /-- The closure of a preconnected set is preconnected as well. -/ protected theorem IsPreconnected.closure {s : Set α} (H : IsPreconnected s) : IsPreconnected (closure s) := IsPreconnected.subset_closure H subset_closure Subset.rfl /-- The closure of a connected set is connected as well. -/ protected theorem IsConnected.closure {s : Set α} (H : IsConnected s) : IsConnected (closure s) := IsConnected.subset_closure H subset_closure <| Subset.rfl /-- The image of a preconnected set is preconnected as well. -/ protected theorem IsPreconnected.image [TopologicalSpace β] {s : Set α} (H : IsPreconnected s) (f : α → β) (hf : ContinuousOn f s) : IsPreconnected (f '' s) := by -- Unfold/destruct definitions in hypotheses rintro u v hu hv huv ⟨_, ⟨x, xs, rfl⟩, xu⟩ ⟨_, ⟨y, ys, rfl⟩, yv⟩ rcases continuousOn_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩ rcases continuousOn_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩ -- Reformulate `huv : f '' s ⊆ u ∪ v` in terms of `u'` and `v'` replace huv : s ⊆ u' ∪ v' := by rw [image_subset_iff, preimage_union] at huv replace huv := subset_inter huv Subset.rfl rw [union_inter_distrib_right, u'_eq, v'_eq, ← union_inter_distrib_right] at huv exact (subset_inter_iff.1 huv).1 -- Now `s ⊆ u' ∪ v'`, so we can apply `‹IsPreconnected s›` obtain ⟨z, hz⟩ : (s ∩ (u' ∩ v')).Nonempty := by refine H u' v' hu' hv' huv ⟨x, ?_⟩ ⟨y, ?_⟩ <;> rw [inter_comm] exacts [u'_eq ▸ ⟨xu, xs⟩, v'_eq ▸ ⟨yv, ys⟩] rw [← inter_self s, inter_assoc, inter_left_comm s u', ← inter_assoc, inter_comm s, inter_comm s, ← u'_eq, ← v'_eq] at hz exact ⟨f z, ⟨z, hz.1.2, rfl⟩, hz.1.1, hz.2.1⟩ /-- The image of a connected set is connected as well. -/ protected theorem IsConnected.image [TopologicalSpace β] {s : Set α} (H : IsConnected s) (f : α → β) (hf : ContinuousOn f s) : IsConnected (f '' s) := ⟨image_nonempty.mpr H.nonempty, H.isPreconnected.image f hf⟩ theorem isPreconnected_closed_iff {s : Set α} : IsPreconnected s ↔ ∀ t t', IsClosed t → IsClosed t' → s ⊆ t ∪ t' → (s ∩ t).Nonempty → (s ∩ t').Nonempty → (s ∩ (t ∩ t')).Nonempty := ⟨by rintro h t t' ht ht' htt' ⟨x, xs, xt⟩ ⟨y, ys, yt'⟩ rw [← not_disjoint_iff_nonempty_inter, ← subset_compl_iff_disjoint_right, compl_inter] intro h' have xt' : x ∉ t' := (h' xs).resolve_left (absurd xt) have yt : y ∉ t := (h' ys).resolve_right (absurd yt') have := h _ _ ht.isOpen_compl ht'.isOpen_compl h' ⟨y, ys, yt⟩ ⟨x, xs, xt'⟩ rw [← compl_union] at this exact this.ne_empty htt'.disjoint_compl_right.inter_eq, by rintro h u v hu hv huv ⟨x, xs, xu⟩ ⟨y, ys, yv⟩ rw [← not_disjoint_iff_nonempty_inter, ← subset_compl_iff_disjoint_right, compl_inter] intro h' have xv : x ∉ v := (h' xs).elim (absurd xu) id have yu : y ∉ u := (h' ys).elim id (absurd yv) have := h _ _ hu.isClosed_compl hv.isClosed_compl h' ⟨y, ys, yu⟩ ⟨x, xs, xv⟩ rw [← compl_union] at this exact this.ne_empty huv.disjoint_compl_right.inter_eq⟩ theorem Topology.IsInducing.isPreconnected_image [TopologicalSpace β] {s : Set α} {f : α → β} (hf : IsInducing f) : IsPreconnected (f '' s) ↔ IsPreconnected s := by refine ⟨fun h => ?_, fun h => h.image _ hf.continuous.continuousOn⟩ rintro u v hu' hv' huv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ rcases hf.isOpen_iff.1 hu' with ⟨u, hu, rfl⟩ rcases hf.isOpen_iff.1 hv' with ⟨v, hv, rfl⟩ replace huv : f '' s ⊆ u ∪ v := by rwa [image_subset_iff] rcases h u v hu hv huv ⟨f x, mem_image_of_mem _ hxs, hxu⟩ ⟨f y, mem_image_of_mem _ hys, hyv⟩ with ⟨_, ⟨z, hzs, rfl⟩, hzuv⟩ exact ⟨z, hzs, hzuv⟩ @[deprecated (since := "2024-10-28")] alias Inducing.isPreconnected_image := IsInducing.isPreconnected_image /- TODO: The following lemmas about connection of preimages hold more generally for strict maps (the quotient and subspace topologies of the image agree) whose fibers are preconnected. -/ theorem IsPreconnected.preimage_of_isOpenMap [TopologicalSpace β] {f : α → β} {s : Set β} (hs : IsPreconnected s) (hinj : Function.Injective f) (hf : IsOpenMap f) (hsf : s ⊆ range f) : IsPreconnected (f ⁻¹' s) := fun u v hu hv hsuv hsu hsv => by replace hsf : f '' (f ⁻¹' s) = s := image_preimage_eq_of_subset hsf obtain ⟨_, has, ⟨a, hau, rfl⟩, hav⟩ : (s ∩ (f '' u ∩ f '' v)).Nonempty := by refine hs (f '' u) (f '' v) (hf u hu) (hf v hv) ?_ ?_ ?_ · simpa only [hsf, image_union] using image_subset f hsuv · simpa only [image_preimage_inter] using hsu.image f · simpa only [image_preimage_inter] using hsv.image f · exact ⟨a, has, hau, hinj.mem_set_image.1 hav⟩ theorem IsPreconnected.preimage_of_isClosedMap [TopologicalSpace β] {s : Set β} (hs : IsPreconnected s) {f : α → β} (hinj : Function.Injective f) (hf : IsClosedMap f) (hsf : s ⊆ range f) : IsPreconnected (f ⁻¹' s) := isPreconnected_closed_iff.2 fun u v hu hv hsuv hsu hsv => by replace hsf : f '' (f ⁻¹' s) = s := image_preimage_eq_of_subset hsf obtain ⟨_, has, ⟨a, hau, rfl⟩, hav⟩ : (s ∩ (f '' u ∩ f '' v)).Nonempty := by refine isPreconnected_closed_iff.1 hs (f '' u) (f '' v) (hf u hu) (hf v hv) ?_ ?_ ?_ · simpa only [hsf, image_union] using image_subset f hsuv · simpa only [image_preimage_inter] using hsu.image f · simpa only [image_preimage_inter] using hsv.image f · exact ⟨a, has, hau, hinj.mem_set_image.1 hav⟩ theorem IsConnected.preimage_of_isOpenMap [TopologicalSpace β] {s : Set β} (hs : IsConnected s) {f : α → β} (hinj : Function.Injective f) (hf : IsOpenMap f) (hsf : s ⊆ range f) : IsConnected (f ⁻¹' s) := ⟨hs.nonempty.preimage' hsf, hs.isPreconnected.preimage_of_isOpenMap hinj hf hsf⟩ theorem IsConnected.preimage_of_isClosedMap [TopologicalSpace β] {s : Set β} (hs : IsConnected s) {f : α → β} (hinj : Function.Injective f) (hf : IsClosedMap f) (hsf : s ⊆ range f) : IsConnected (f ⁻¹' s) := ⟨hs.nonempty.preimage' hsf, hs.isPreconnected.preimage_of_isClosedMap hinj hf hsf⟩ theorem IsPreconnected.subset_or_subset (hu : IsOpen u) (hv : IsOpen v) (huv : Disjoint u v) (hsuv : s ⊆ u ∪ v) (hs : IsPreconnected s) : s ⊆ u ∨ s ⊆ v := by specialize hs u v hu hv hsuv obtain hsu | hsu := (s ∩ u).eq_empty_or_nonempty · exact Or.inr ((Set.disjoint_iff_inter_eq_empty.2 hsu).subset_right_of_subset_union hsuv) · replace hs := mt (hs hsu) simp_rw [Set.not_nonempty_iff_eq_empty, ← Set.disjoint_iff_inter_eq_empty, disjoint_iff_inter_eq_empty.1 huv] at hs exact Or.inl ((hs s.disjoint_empty).subset_left_of_subset_union hsuv) theorem IsPreconnected.subset_left_of_subset_union (hu : IsOpen u) (hv : IsOpen v) (huv : Disjoint u v) (hsuv : s ⊆ u ∪ v) (hsu : (s ∩ u).Nonempty) (hs : IsPreconnected s) : s ⊆ u := Disjoint.subset_left_of_subset_union hsuv (by by_contra hsv rw [not_disjoint_iff_nonempty_inter] at hsv obtain ⟨x, _, hx⟩ := hs u v hu hv hsuv hsu hsv exact Set.disjoint_iff.1 huv hx) theorem IsPreconnected.subset_right_of_subset_union (hu : IsOpen u) (hv : IsOpen v) (huv : Disjoint u v) (hsuv : s ⊆ u ∪ v) (hsv : (s ∩ v).Nonempty) (hs : IsPreconnected s) : s ⊆ v := hs.subset_left_of_subset_union hv hu huv.symm (union_comm u v ▸ hsuv) hsv /-- If a preconnected set `s` intersects an open set `u`, and limit points of `u` inside `s` are contained in `u`, then the whole set `s` is contained in `u`. -/ theorem IsPreconnected.subset_of_closure_inter_subset (hs : IsPreconnected s) (hu : IsOpen u) (h'u : (s ∩ u).Nonempty) (h : closure u ∩ s ⊆ u) : s ⊆ u := by have A : s ⊆ u ∪ (closure u)ᶜ := by intro x hx by_cases xu : x ∈ u · exact Or.inl xu · right intro h'x exact xu (h (mem_inter h'x hx)) apply hs.subset_left_of_subset_union hu isClosed_closure.isOpen_compl _ A h'u exact disjoint_compl_right.mono_right (compl_subset_compl.2 subset_closure) theorem IsPreconnected.prod [TopologicalSpace β] {s : Set α} {t : Set β} (hs : IsPreconnected s) (ht : IsPreconnected t) : IsPreconnected (s ×ˢ t) := by apply isPreconnected_of_forall_pair rintro ⟨a₁, b₁⟩ ⟨ha₁, hb₁⟩ ⟨a₂, b₂⟩ ⟨ha₂, hb₂⟩ refine ⟨Prod.mk a₁ '' t ∪ flip Prod.mk b₂ '' s, ?_, .inl ⟨b₁, hb₁, rfl⟩, .inr ⟨a₂, ha₂, rfl⟩, ?_⟩ · rintro _ (⟨y, hy, rfl⟩ | ⟨x, hx, rfl⟩) exacts [⟨ha₁, hy⟩, ⟨hx, hb₂⟩] · exact (ht.image _ (by fun_prop)).union (a₁, b₂) ⟨b₂, hb₂, rfl⟩ ⟨a₁, ha₁, rfl⟩ (hs.image _ (Continuous.prodMk_left _).continuousOn) theorem IsConnected.prod [TopologicalSpace β] {s : Set α} {t : Set β} (hs : IsConnected s) (ht : IsConnected t) : IsConnected (s ×ˢ t) := ⟨hs.1.prod ht.1, hs.2.prod ht.2⟩ theorem isPreconnected_univ_pi [∀ i, TopologicalSpace (π i)] {s : ∀ i, Set (π i)} (hs : ∀ i, IsPreconnected (s i)) : IsPreconnected (pi univ s) := by rintro u v uo vo hsuv ⟨f, hfs, hfu⟩ ⟨g, hgs, hgv⟩ classical rcases exists_finset_piecewise_mem_of_mem_nhds (uo.mem_nhds hfu) g with ⟨I, hI⟩ induction I using Finset.induction_on with | empty => refine ⟨g, hgs, ⟨?_, hgv⟩⟩ simpa using hI | insert i I _ ihI => rw [Finset.piecewise_insert] at hI have := I.piecewise_mem_set_pi hfs hgs refine (hsuv this).elim ihI fun h => ?_ set S := update (I.piecewise f g) i '' s i have hsub : S ⊆ pi univ s := by refine image_subset_iff.2 fun z hz => ?_ rwa [update_preimage_univ_pi] exact fun j _ => this j trivial have hconn : IsPreconnected S := (hs i).image _ (continuous_const.update i continuous_id).continuousOn have hSu : (S ∩ u).Nonempty := ⟨_, mem_image_of_mem _ (hfs _ trivial), hI⟩ have hSv : (S ∩ v).Nonempty := ⟨_, ⟨_, this _ trivial, update_eq_self _ _⟩, h⟩ refine (hconn u v uo vo (hsub.trans hsuv) hSu hSv).mono ?_ exact inter_subset_inter_left _ hsub @[simp] theorem isConnected_univ_pi [∀ i, TopologicalSpace (π i)] {s : ∀ i, Set (π i)} : IsConnected (pi univ s) ↔ ∀ i, IsConnected (s i) := by simp only [IsConnected, ← univ_pi_nonempty_iff, forall_and, and_congr_right_iff] refine fun hne => ⟨fun hc i => ?_, isPreconnected_univ_pi⟩ rw [← eval_image_univ_pi hne] exact hc.image _ (continuous_apply _).continuousOn /-- The connected component of a point is the maximal connected set that contains this point. -/ def connectedComponent (x : α) : Set α := ⋃₀ { s : Set α | IsPreconnected s ∧ x ∈ s } open Classical in /-- Given a set `F` in a topological space `α` and a point `x : α`, the connected component of `x` in `F` is the connected component of `x` in the subtype `F` seen as a set in `α`. This definition does not make sense if `x` is not in `F` so we return the empty set in this case. -/ def connectedComponentIn (F : Set α) (x : α) : Set α := if h : x ∈ F then (↑) '' connectedComponent (⟨x, h⟩ : F) else ∅ theorem connectedComponentIn_eq_image {F : Set α} {x : α} (h : x ∈ F) : connectedComponentIn F x = (↑) '' connectedComponent (⟨x, h⟩ : F) := dif_pos h theorem connectedComponentIn_eq_empty {F : Set α} {x : α} (h : x ∉ F) : connectedComponentIn F x = ∅ := dif_neg h theorem mem_connectedComponent {x : α} : x ∈ connectedComponent x := mem_sUnion_of_mem (mem_singleton x) ⟨isPreconnected_singleton, mem_singleton x⟩ theorem mem_connectedComponentIn {x : α} {F : Set α} (hx : x ∈ F) : x ∈ connectedComponentIn F x := by simp [connectedComponentIn_eq_image hx, mem_connectedComponent, hx] theorem connectedComponent_nonempty {x : α} : (connectedComponent x).Nonempty := ⟨x, mem_connectedComponent⟩ theorem connectedComponentIn_nonempty_iff {x : α} {F : Set α} : (connectedComponentIn F x).Nonempty ↔ x ∈ F := by rw [connectedComponentIn] split_ifs <;> simp [connectedComponent_nonempty, *] theorem connectedComponentIn_subset (F : Set α) (x : α) : connectedComponentIn F x ⊆ F := by rw [connectedComponentIn] split_ifs <;> simp theorem isPreconnected_connectedComponent {x : α} : IsPreconnected (connectedComponent x) := isPreconnected_sUnion x _ (fun _ => And.right) fun _ => And.left theorem isPreconnected_connectedComponentIn {x : α} {F : Set α} : IsPreconnected (connectedComponentIn F x) := by rw [connectedComponentIn]; split_ifs · exact IsInducing.subtypeVal.isPreconnected_image.mpr isPreconnected_connectedComponent · exact isPreconnected_empty theorem isConnected_connectedComponent {x : α} : IsConnected (connectedComponent x) := ⟨⟨x, mem_connectedComponent⟩, isPreconnected_connectedComponent⟩ theorem isConnected_connectedComponentIn_iff {x : α} {F : Set α} : IsConnected (connectedComponentIn F x) ↔ x ∈ F := by simp_rw [← connectedComponentIn_nonempty_iff, IsConnected, isPreconnected_connectedComponentIn, and_true] theorem IsPreconnected.subset_connectedComponent {x : α} {s : Set α} (H1 : IsPreconnected s) (H2 : x ∈ s) : s ⊆ connectedComponent x := fun _z hz => mem_sUnion_of_mem hz ⟨H1, H2⟩ theorem IsPreconnected.subset_connectedComponentIn {x : α} {F : Set α} (hs : IsPreconnected s) (hxs : x ∈ s) (hsF : s ⊆ F) : s ⊆ connectedComponentIn F x := by have : IsPreconnected (((↑) : F → α) ⁻¹' s) := by refine IsInducing.subtypeVal.isPreconnected_image.mp ?_ rwa [Subtype.image_preimage_coe, inter_eq_right.mpr hsF] have h2xs : (⟨x, hsF hxs⟩ : F) ∈ (↑) ⁻¹' s := by rw [mem_preimage] exact hxs have := this.subset_connectedComponent h2xs rw [connectedComponentIn_eq_image (hsF hxs)] refine Subset.trans ?_ (image_subset _ this) rw [Subtype.image_preimage_coe, inter_eq_right.mpr hsF] theorem IsConnected.subset_connectedComponent {x : α} {s : Set α} (H1 : IsConnected s) (H2 : x ∈ s) : s ⊆ connectedComponent x := H1.2.subset_connectedComponent H2 theorem IsPreconnected.connectedComponentIn {x : α} {F : Set α} (h : IsPreconnected F) (hx : x ∈ F) : connectedComponentIn F x = F := (connectedComponentIn_subset F x).antisymm (h.subset_connectedComponentIn hx subset_rfl) theorem connectedComponent_eq {x y : α} (h : y ∈ connectedComponent x) : connectedComponent x = connectedComponent y := eq_of_subset_of_subset (isConnected_connectedComponent.subset_connectedComponent h) (isConnected_connectedComponent.subset_connectedComponent (Set.mem_of_mem_of_subset mem_connectedComponent (isConnected_connectedComponent.subset_connectedComponent h))) theorem connectedComponent_eq_iff_mem {x y : α} : connectedComponent x = connectedComponent y ↔ x ∈ connectedComponent y := ⟨fun h => h ▸ mem_connectedComponent, fun h => (connectedComponent_eq h).symm⟩ theorem connectedComponentIn_eq {x y : α} {F : Set α} (h : y ∈ connectedComponentIn F x) : connectedComponentIn F x = connectedComponentIn F y := by have hx : x ∈ F := connectedComponentIn_nonempty_iff.mp ⟨y, h⟩ simp_rw [connectedComponentIn_eq_image hx] at h ⊢ obtain ⟨⟨y, hy⟩, h2y, rfl⟩ := h simp_rw [connectedComponentIn_eq_image hy, connectedComponent_eq h2y] theorem connectedComponentIn_univ (x : α) : connectedComponentIn univ x = connectedComponent x := subset_antisymm (isPreconnected_connectedComponentIn.subset_connectedComponent <| mem_connectedComponentIn trivial) (isPreconnected_connectedComponent.subset_connectedComponentIn mem_connectedComponent <| subset_univ _) theorem connectedComponent_disjoint {x y : α} (h : connectedComponent x ≠ connectedComponent y) : Disjoint (connectedComponent x) (connectedComponent y) := Set.disjoint_left.2 fun _ h1 h2 => h ((connectedComponent_eq h1).trans (connectedComponent_eq h2).symm) theorem isClosed_connectedComponent {x : α} : IsClosed (connectedComponent x) := closure_subset_iff_isClosed.1 <| isConnected_connectedComponent.closure.subset_connectedComponent <| subset_closure mem_connectedComponent theorem Continuous.image_connectedComponent_subset [TopologicalSpace β] {f : α → β} (h : Continuous f) (a : α) : f '' connectedComponent a ⊆ connectedComponent (f a) := (isConnected_connectedComponent.image f h.continuousOn).subset_connectedComponent ((mem_image f (connectedComponent a) (f a)).2 ⟨a, mem_connectedComponent, rfl⟩) theorem Continuous.image_connectedComponentIn_subset [TopologicalSpace β] {f : α → β} {s : Set α} {a : α} (hf : Continuous f) (hx : a ∈ s) : f '' connectedComponentIn s a ⊆ connectedComponentIn (f '' s) (f a) := (isPreconnected_connectedComponentIn.image _ hf.continuousOn).subset_connectedComponentIn (mem_image_of_mem _ <| mem_connectedComponentIn hx) (image_subset _ <| connectedComponentIn_subset _ _) theorem Continuous.mapsTo_connectedComponent [TopologicalSpace β] {f : α → β} (h : Continuous f) (a : α) : MapsTo f (connectedComponent a) (connectedComponent (f a)) := mapsTo'.2 <| h.image_connectedComponent_subset a theorem Continuous.mapsTo_connectedComponentIn [TopologicalSpace β] {f : α → β} {s : Set α} (h : Continuous f) {a : α} (hx : a ∈ s) : MapsTo f (connectedComponentIn s a) (connectedComponentIn (f '' s) (f a)) := mapsTo'.2 <| image_connectedComponentIn_subset h hx theorem irreducibleComponent_subset_connectedComponent {x : α} : irreducibleComponent x ⊆ connectedComponent x := isIrreducible_irreducibleComponent.isConnected.subset_connectedComponent mem_irreducibleComponent @[mono] theorem connectedComponentIn_mono (x : α) {F G : Set α} (h : F ⊆ G) : connectedComponentIn F x ⊆ connectedComponentIn G x := by by_cases hx : x ∈ F · rw [connectedComponentIn_eq_image hx, connectedComponentIn_eq_image (h hx), ← show ((↑) : G → α) ∘ inclusion h = (↑) from rfl, image_comp] exact image_subset _ ((continuous_inclusion h).image_connectedComponent_subset ⟨x, hx⟩) · rw [connectedComponentIn_eq_empty hx] exact Set.empty_subset _ /-- A preconnected space is one where there is no non-trivial open partition. -/ class PreconnectedSpace (α : Type u) [TopologicalSpace α] : Prop where /-- The universal set `Set.univ` in a preconnected space is a preconnected set. -/ isPreconnected_univ : IsPreconnected (univ : Set α) export PreconnectedSpace (isPreconnected_univ) /-- A connected space is a nonempty one where there is no non-trivial open partition. -/ class ConnectedSpace (α : Type u) [TopologicalSpace α] : Prop extends PreconnectedSpace α where /-- A connected space is nonempty. -/ toNonempty : Nonempty α attribute [instance 50] ConnectedSpace.toNonempty -- see Note [lower instance priority] -- see Note [lower instance priority] theorem isConnected_univ [ConnectedSpace α] : IsConnected (univ : Set α) := ⟨univ_nonempty, isPreconnected_univ⟩ lemma preconnectedSpace_iff_univ : PreconnectedSpace α ↔ IsPreconnected (univ : Set α) := ⟨fun h ↦ h.1, fun h ↦ ⟨h⟩⟩ lemma connectedSpace_iff_univ : ConnectedSpace α ↔ IsConnected (univ : Set α) := ⟨fun h ↦ ⟨univ_nonempty, h.1.1⟩, fun h ↦ ConnectedSpace.mk (toPreconnectedSpace := ⟨h.2⟩) ⟨h.1.some⟩⟩ theorem isPreconnected_range [TopologicalSpace β] [PreconnectedSpace α] {f : α → β} (h : Continuous f) : IsPreconnected (range f) := @image_univ _ _ f ▸ isPreconnected_univ.image _ h.continuousOn theorem isConnected_range [TopologicalSpace β] [ConnectedSpace α] {f : α → β} (h : Continuous f) : IsConnected (range f) := ⟨range_nonempty f, isPreconnected_range h⟩ theorem Function.Surjective.connectedSpace [ConnectedSpace α] [TopologicalSpace β] {f : α → β} (hf : Surjective f) (hf' : Continuous f) : ConnectedSpace β := by rw [connectedSpace_iff_univ, ← hf.range_eq] exact isConnected_range hf' instance Quotient.instConnectedSpace {s : Setoid α} [ConnectedSpace α] : ConnectedSpace (Quotient s) := Quotient.mk'_surjective.connectedSpace continuous_coinduced_rng theorem DenseRange.preconnectedSpace [TopologicalSpace β] [PreconnectedSpace α] {f : α → β} (hf : DenseRange f) (hc : Continuous f) : PreconnectedSpace β := ⟨hf.closure_eq ▸ (isPreconnected_range hc).closure⟩ theorem connectedSpace_iff_connectedComponent : ConnectedSpace α ↔ ∃ x : α, connectedComponent x = univ := by constructor · rintro ⟨⟨x⟩⟩ exact ⟨x, eq_univ_of_univ_subset <| isPreconnected_univ.subset_connectedComponent (mem_univ x)⟩ · rintro ⟨x, h⟩ haveI : PreconnectedSpace α := ⟨by rw [← h]; exact isPreconnected_connectedComponent⟩ exact ⟨⟨x⟩⟩ theorem preconnectedSpace_iff_connectedComponent : PreconnectedSpace α ↔ ∀ x : α, connectedComponent x = univ := by constructor · intro h x exact eq_univ_of_univ_subset <| isPreconnected_univ.subset_connectedComponent (mem_univ x) · intro h rcases isEmpty_or_nonempty α with hα | hα · exact ⟨by rw [univ_eq_empty_iff.mpr hα]; exact isPreconnected_empty⟩ · exact ⟨by rw [← h (Classical.choice hα)]; exact isPreconnected_connectedComponent⟩ @[simp] theorem PreconnectedSpace.connectedComponent_eq_univ {X : Type*} [TopologicalSpace X] [h : PreconnectedSpace X] (x : X) : connectedComponent x = univ := preconnectedSpace_iff_connectedComponent.mp h x instance [TopologicalSpace β] [PreconnectedSpace α] [PreconnectedSpace β] : PreconnectedSpace (α × β) := ⟨by rw [← univ_prod_univ] exact isPreconnected_univ.prod isPreconnected_univ⟩ instance [TopologicalSpace β] [ConnectedSpace α] [ConnectedSpace β] : ConnectedSpace (α × β) := ⟨inferInstance⟩ instance [∀ i, TopologicalSpace (π i)] [∀ i, PreconnectedSpace (π i)] : PreconnectedSpace (∀ i, π i) := ⟨by rw [← pi_univ univ]; exact isPreconnected_univ_pi fun i => isPreconnected_univ⟩ instance [∀ i, TopologicalSpace (π i)] [∀ i, ConnectedSpace (π i)] : ConnectedSpace (∀ i, π i) := ⟨inferInstance⟩ -- see Note [lower instance priority] instance (priority := 100) PreirreducibleSpace.preconnectedSpace (α : Type u) [TopologicalSpace α] [PreirreducibleSpace α] : PreconnectedSpace α := ⟨isPreirreducible_univ.isPreconnected⟩ -- see Note [lower instance priority] instance (priority := 100) IrreducibleSpace.connectedSpace (α : Type u) [TopologicalSpace α] [IrreducibleSpace α] : ConnectedSpace α where toNonempty := IrreducibleSpace.toNonempty theorem Subtype.preconnectedSpace {s : Set α} (h : IsPreconnected s) : PreconnectedSpace s where isPreconnected_univ := by rwa [← IsInducing.subtypeVal.isPreconnected_image, image_univ, Subtype.range_val] theorem Subtype.connectedSpace {s : Set α} (h : IsConnected s) : ConnectedSpace s where toPreconnectedSpace := Subtype.preconnectedSpace h.isPreconnected toNonempty := h.nonempty.to_subtype theorem isPreconnected_iff_preconnectedSpace {s : Set α} : IsPreconnected s ↔ PreconnectedSpace s := ⟨Subtype.preconnectedSpace, fun h => by simpa using isPreconnected_univ.image ((↑) : s → α) continuous_subtype_val.continuousOn⟩ theorem isConnected_iff_connectedSpace {s : Set α} : IsConnected s ↔ ConnectedSpace s := ⟨Subtype.connectedSpace, fun h => ⟨nonempty_subtype.mp h.2, isPreconnected_iff_preconnectedSpace.mpr h.1⟩⟩ end Preconnected
Mathlib/Topology/Connected/Basic.lean
996
1,011
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import Mathlib.MeasureTheory.Integral.FinMeasAdditive /-! # Extension of a linear function from indicators to L1 Given `T : Set α → E →L[ℝ] F` with `DominatedFinMeasAdditive μ T C`, we construct an extension of `T` to integrable simple functions, which are finite sums of indicators of measurable sets with finite measure, then to integrable functions, which are limits of integrable simple functions. The main result is a continuous linear map `(α →₁[μ] E) →L[ℝ] F`. This extension process is used to define the Bochner integral in the `Mathlib.MeasureTheory.Integral.Bochner.Basic` file and the conditional expectation of an integrable function in `Mathlib.MeasureTheory.Function.ConditionalExpectation.CondexpL1`. ## Main definitions - `setToL1 (hT : DominatedFinMeasAdditive μ T C) : (α →₁[μ] E) →L[ℝ] F`: the extension of `T` from indicators to L1. - `setToFun μ T (hT : DominatedFinMeasAdditive μ T C) (f : α → E) : F`: a version of the extension which applies to functions (with value 0 if the function is not integrable). ## Properties For most properties of `setToFun`, we provide two lemmas. One version uses hypotheses valid on all sets, like `T = T'`, and a second version which uses a primed name uses hypotheses on measurable sets with finite measure, like `∀ s, MeasurableSet s → μ s < ∞ → T s = T' s`. The lemmas listed here don't show all hypotheses. Refer to the actual lemmas for details. Linearity: - `setToFun_zero_left : setToFun μ 0 hT f = 0` - `setToFun_add_left : setToFun μ (T + T') _ f = setToFun μ T hT f + setToFun μ T' hT' f` - `setToFun_smul_left : setToFun μ (fun s ↦ c • (T s)) (hT.smul c) f = c • setToFun μ T hT f` - `setToFun_zero : setToFun μ T hT (0 : α → E) = 0` - `setToFun_neg : setToFun μ T hT (-f) = - setToFun μ T hT f` If `f` and `g` are integrable: - `setToFun_add : setToFun μ T hT (f + g) = setToFun μ T hT f + setToFun μ T hT g` - `setToFun_sub : setToFun μ T hT (f - g) = setToFun μ T hT f - setToFun μ T hT g` If `T` is verifies `∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x`: - `setToFun_smul : setToFun μ T hT (c • f) = c • setToFun μ T hT f` Other: - `setToFun_congr_ae (h : f =ᵐ[μ] g) : setToFun μ T hT f = setToFun μ T hT g` - `setToFun_measure_zero (h : μ = 0) : setToFun μ T hT f = 0` If the space is also an ordered additive group with an order closed topology and `T` is such that `0 ≤ T s x` for `0 ≤ x`, we also prove order-related properties: - `setToFun_mono_left (h : ∀ s x, T s x ≤ T' s x) : setToFun μ T hT f ≤ setToFun μ T' hT' f` - `setToFun_nonneg (hf : 0 ≤ᵐ[μ] f) : 0 ≤ setToFun μ T hT f` - `setToFun_mono (hfg : f ≤ᵐ[μ] g) : setToFun μ T hT f ≤ setToFun μ T hT g` -/ noncomputable section open scoped Topology NNReal open Set Filter TopologicalSpace ENNReal namespace MeasureTheory variable {α E F F' G 𝕜 : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [NormedAddCommGroup F] [NormedSpace ℝ F] [NormedAddCommGroup F'] [NormedSpace ℝ F'] [NormedAddCommGroup G] {m : MeasurableSpace α} {μ : Measure α} namespace L1 open AEEqFun Lp.simpleFunc Lp namespace SimpleFunc theorem norm_eq_sum_mul (f : α →₁ₛ[μ] G) : ‖f‖ = ∑ x ∈ (toSimpleFunc f).range, μ.real (toSimpleFunc f ⁻¹' {x}) * ‖x‖ := by rw [norm_toSimpleFunc, eLpNorm_one_eq_lintegral_enorm] have h_eq := SimpleFunc.map_apply (‖·‖ₑ) (toSimpleFunc f) simp_rw [← h_eq, measureReal_def] rw [SimpleFunc.lintegral_eq_lintegral, SimpleFunc.map_lintegral, ENNReal.toReal_sum] · congr ext1 x rw [ENNReal.toReal_mul, mul_comm, ← ofReal_norm_eq_enorm, ENNReal.toReal_ofReal (norm_nonneg _)] · intro x _ by_cases hx0 : x = 0 · rw [hx0]; simp · exact ENNReal.mul_ne_top ENNReal.coe_ne_top (SimpleFunc.measure_preimage_lt_top_of_integrable _ (SimpleFunc.integrable f) hx0).ne section SetToL1S variable [NormedField 𝕜] [NormedSpace 𝕜 E] attribute [local instance] Lp.simpleFunc.module attribute [local instance] Lp.simpleFunc.normedSpace /-- Extend `Set α → (E →L[ℝ] F')` to `(α →₁ₛ[μ] E) → F'`. -/ def setToL1S (T : Set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : F := (toSimpleFunc f).setToSimpleFunc T theorem setToL1S_eq_setToSimpleFunc (T : Set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : setToL1S T f = (toSimpleFunc f).setToSimpleFunc T := rfl @[simp] theorem setToL1S_zero_left (f : α →₁ₛ[μ] E) : setToL1S (0 : Set α → E →L[ℝ] F) f = 0 := SimpleFunc.setToSimpleFunc_zero _ theorem setToL1S_zero_left' {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) (f : α →₁ₛ[μ] E) : setToL1S T f = 0 := SimpleFunc.setToSimpleFunc_zero' h_zero _ (SimpleFunc.integrable f) theorem setToL1S_congr (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) {f g : α →₁ₛ[μ] E} (h : toSimpleFunc f =ᵐ[μ] toSimpleFunc g) : setToL1S T f = setToL1S T g := SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable f) h theorem setToL1S_congr_left (T T' : Set α → E →L[ℝ] F) (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α →₁ₛ[μ] E) : setToL1S T f = setToL1S T' f := SimpleFunc.setToSimpleFunc_congr_left T T' h (simpleFunc.toSimpleFunc f) (SimpleFunc.integrable f) /-- `setToL1S` does not change if we replace the measure `μ` by `μ'` with `μ ≪ μ'`. The statement uses two functions `f` and `f'` because they have to belong to different types, but morally these are the same function (we have `f =ᵐ[μ] f'`). -/ theorem setToL1S_congr_measure {μ' : Measure α} (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hμ : μ ≪ μ') (f : α →₁ₛ[μ] E) (f' : α →₁ₛ[μ'] E) (h : (f : α → E) =ᵐ[μ] f') : setToL1S T f = setToL1S T f' := by refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable f) ?_ refine (toSimpleFunc_eq_toFun f).trans ?_ suffices (f' : α → E) =ᵐ[μ] simpleFunc.toSimpleFunc f' from h.trans this have goal' : (f' : α → E) =ᵐ[μ'] simpleFunc.toSimpleFunc f' := (toSimpleFunc_eq_toFun f').symm exact hμ.ae_eq goal' theorem setToL1S_add_left (T T' : Set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : setToL1S (T + T') f = setToL1S T f + setToL1S T' f := SimpleFunc.setToSimpleFunc_add_left T T' theorem setToL1S_add_left' (T T' T'' : Set α → E →L[ℝ] F) (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁ₛ[μ] E) : setToL1S T'' f = setToL1S T f + setToL1S T' f := SimpleFunc.setToSimpleFunc_add_left' T T' T'' h_add (SimpleFunc.integrable f) theorem setToL1S_smul_left (T : Set α → E →L[ℝ] F) (c : ℝ) (f : α →₁ₛ[μ] E) : setToL1S (fun s => c • T s) f = c • setToL1S T f := SimpleFunc.setToSimpleFunc_smul_left T c _ theorem setToL1S_smul_left' (T T' : Set α → E →L[ℝ] F) (c : ℝ) (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α →₁ₛ[μ] E) : setToL1S T' f = c • setToL1S T f := SimpleFunc.setToSimpleFunc_smul_left' T T' c h_smul (SimpleFunc.integrable f) theorem setToL1S_add (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (f g : α →₁ₛ[μ] E) : setToL1S T (f + g) = setToL1S T f + setToL1S T g := by simp_rw [setToL1S] rw [← SimpleFunc.setToSimpleFunc_add T h_add (SimpleFunc.integrable f) (SimpleFunc.integrable g)] exact SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) (add_toSimpleFunc f g) theorem setToL1S_neg {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (f : α →₁ₛ[μ] E) : setToL1S T (-f) = -setToL1S T f := by simp_rw [setToL1S] have : simpleFunc.toSimpleFunc (-f) =ᵐ[μ] ⇑(-simpleFunc.toSimpleFunc f) := neg_toSimpleFunc f rw [SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) this] exact SimpleFunc.setToSimpleFunc_neg T h_add (SimpleFunc.integrable f) theorem setToL1S_sub {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (f g : α →₁ₛ[μ] E) : setToL1S T (f - g) = setToL1S T f - setToL1S T g := by rw [sub_eq_add_neg, setToL1S_add T h_zero h_add, setToL1S_neg h_zero h_add, sub_eq_add_neg] theorem setToL1S_smul_real (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (c : ℝ) (f : α →₁ₛ[μ] E) : setToL1S T (c • f) = c • setToL1S T f := by simp_rw [setToL1S] rw [← SimpleFunc.setToSimpleFunc_smul_real T h_add c (SimpleFunc.integrable f)] refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) ?_ exact smul_toSimpleFunc c f theorem setToL1S_smul {E} [NormedAddCommGroup E] [NormedSpace ℝ E] [NormedSpace 𝕜 E] [DistribSMul 𝕜 F] (T : Set α → E →L[ℝ] F) (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α →₁ₛ[μ] E) : setToL1S T (c • f) = c • setToL1S T f := by simp_rw [setToL1S] rw [← SimpleFunc.setToSimpleFunc_smul T h_add h_smul c (SimpleFunc.integrable f)] refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) ?_ exact smul_toSimpleFunc c f theorem norm_setToL1S_le (T : Set α → E →L[ℝ] F) {C : ℝ} (hT_norm : ∀ s, MeasurableSet s → μ s < ∞ → ‖T s‖ ≤ C * μ.real s) (f : α →₁ₛ[μ] E) : ‖setToL1S T f‖ ≤ C * ‖f‖ := by rw [setToL1S, norm_eq_sum_mul f] exact SimpleFunc.norm_setToSimpleFunc_le_sum_mul_norm_of_integrable T hT_norm _ (SimpleFunc.integrable f) theorem setToL1S_indicatorConst {T : Set α → E →L[ℝ] F} {s : Set α} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hs : MeasurableSet s) (hμs : μ s < ∞) (x : E) : setToL1S T (simpleFunc.indicatorConst 1 hs hμs.ne x) = T s x := by have h_empty : T ∅ = 0 := h_zero _ MeasurableSet.empty measure_empty rw [setToL1S_eq_setToSimpleFunc] refine Eq.trans ?_ (SimpleFunc.setToSimpleFunc_indicator T h_empty hs x) refine SimpleFunc.setToSimpleFunc_congr T h_zero h_add (SimpleFunc.integrable _) ?_ exact toSimpleFunc_indicatorConst hs hμs.ne x theorem setToL1S_const [IsFiniteMeasure μ] {T : Set α → E →L[ℝ] F} (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (x : E) : setToL1S T (simpleFunc.indicatorConst 1 MeasurableSet.univ (measure_ne_top μ _) x) = T univ x := setToL1S_indicatorConst h_zero h_add MeasurableSet.univ (measure_lt_top _ _) x section Order variable {G'' G' : Type*} [NormedAddCommGroup G'] [PartialOrder G'] [IsOrderedAddMonoid G'] [NormedSpace ℝ G'] [NormedAddCommGroup G''] [PartialOrder G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] {T : Set α → G'' →L[ℝ] G'} theorem setToL1S_mono_left {T T' : Set α → E →L[ℝ] G''} (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1S T f ≤ setToL1S T' f := SimpleFunc.setToSimpleFunc_mono_left T T' hTT' _ theorem setToL1S_mono_left' {T T' : Set α → E →L[ℝ] G''} (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1S T f ≤ setToL1S T' f := SimpleFunc.setToSimpleFunc_mono_left' T T' hTT' _ (SimpleFunc.integrable f) omit [IsOrderedAddMonoid G''] in theorem setToL1S_nonneg (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁ₛ[μ] G''} (hf : 0 ≤ f) : 0 ≤ setToL1S T f := by simp_rw [setToL1S] obtain ⟨f', hf', hff'⟩ := exists_simpleFunc_nonneg_ae_eq hf replace hff' : simpleFunc.toSimpleFunc f =ᵐ[μ] f' := (Lp.simpleFunc.toSimpleFunc_eq_toFun f).trans hff' rw [SimpleFunc.setToSimpleFunc_congr _ h_zero h_add (SimpleFunc.integrable _) hff'] exact SimpleFunc.setToSimpleFunc_nonneg' T hT_nonneg _ hf' ((SimpleFunc.integrable f).congr hff') theorem setToL1S_mono (h_zero : ∀ s, MeasurableSet s → μ s = 0 → T s = 0) (h_add : FinMeasAdditive μ T) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁ₛ[μ] G''} (hfg : f ≤ g) : setToL1S T f ≤ setToL1S T g := by rw [← sub_nonneg] at hfg ⊢ rw [← setToL1S_sub h_zero h_add] exact setToL1S_nonneg h_zero h_add hT_nonneg hfg end Order variable [NormedSpace 𝕜 F] variable (α E μ 𝕜) /-- Extend `Set α → E →L[ℝ] F` to `(α →₁ₛ[μ] E) →L[𝕜] F`. -/ def setToL1SCLM' {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) : (α →₁ₛ[μ] E) →L[𝕜] F := LinearMap.mkContinuous ⟨⟨setToL1S T, setToL1S_add T (fun _ => hT.eq_zero_of_measure_zero) hT.1⟩, setToL1S_smul T (fun _ => hT.eq_zero_of_measure_zero) hT.1 h_smul⟩ C fun f => norm_setToL1S_le T hT.2 f /-- Extend `Set α → E →L[ℝ] F` to `(α →₁ₛ[μ] E) →L[ℝ] F`. -/ def setToL1SCLM {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) : (α →₁ₛ[μ] E) →L[ℝ] F := LinearMap.mkContinuous ⟨⟨setToL1S T, setToL1S_add T (fun _ => hT.eq_zero_of_measure_zero) hT.1⟩, setToL1S_smul_real T (fun _ => hT.eq_zero_of_measure_zero) hT.1⟩ C fun f => norm_setToL1S_le T hT.2 f variable {α E μ 𝕜} variable {T T' T'' : Set α → E →L[ℝ] F} {C C' C'' : ℝ} @[simp] theorem setToL1SCLM_zero_left (hT : DominatedFinMeasAdditive μ (0 : Set α → E →L[ℝ] F) C) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = 0 := setToL1S_zero_left _ theorem setToL1SCLM_zero_left' (hT : DominatedFinMeasAdditive μ T C) (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = 0 := setToL1S_zero_left' h_zero f theorem setToL1SCLM_congr_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : T = T') (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = setToL1SCLM α E μ hT' f := setToL1S_congr_left T T' (fun _ _ _ => by rw [h]) f theorem setToL1SCLM_congr_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f = setToL1SCLM α E μ hT' f := setToL1S_congr_left T T' h f theorem setToL1SCLM_congr_measure {μ' : Measure α} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ' T C') (hμ : μ ≪ μ') (f : α →₁ₛ[μ] E) (f' : α →₁ₛ[μ'] E) (h : (f : α → E) =ᵐ[μ] f') : setToL1SCLM α E μ hT f = setToL1SCLM α E μ' hT' f' := setToL1S_congr_measure T (fun _ => hT.eq_zero_of_measure_zero) hT.1 hμ _ _ h theorem setToL1SCLM_add_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ (hT.add hT') f = setToL1SCLM α E μ hT f + setToL1SCLM α E μ hT' f := setToL1S_add_left T T' f theorem setToL1SCLM_add_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hT'' : DominatedFinMeasAdditive μ T'' C'') (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT'' f = setToL1SCLM α E μ hT f + setToL1SCLM α E μ hT' f := setToL1S_add_left' T T' T'' h_add f theorem setToL1SCLM_smul_left (c : ℝ) (hT : DominatedFinMeasAdditive μ T C) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ (hT.smul c) f = c • setToL1SCLM α E μ hT f := setToL1S_smul_left T c f theorem setToL1SCLM_smul_left' (c : ℝ) (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT' f = c • setToL1SCLM α E μ hT f := setToL1S_smul_left' T T' c h_smul f theorem norm_setToL1SCLM_le {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hC : 0 ≤ C) : ‖setToL1SCLM α E μ hT‖ ≤ C := LinearMap.mkContinuous_norm_le _ hC _ theorem norm_setToL1SCLM_le' {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) : ‖setToL1SCLM α E μ hT‖ ≤ max C 0 := LinearMap.mkContinuous_norm_le' _ _ theorem setToL1SCLM_const [IsFiniteMeasure μ] {T : Set α → E →L[ℝ] F} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (x : E) : setToL1SCLM α E μ hT (simpleFunc.indicatorConst 1 MeasurableSet.univ (measure_ne_top μ _) x) = T univ x := setToL1S_const (fun _ => hT.eq_zero_of_measure_zero) hT.1 x section Order variable {G' G'' : Type*} [NormedAddCommGroup G''] [PartialOrder G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] [NormedAddCommGroup G'] [PartialOrder G'] [IsOrderedAddMonoid G'] [NormedSpace ℝ G'] theorem setToL1SCLM_mono_left {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f ≤ setToL1SCLM α E μ hT' f := SimpleFunc.setToSimpleFunc_mono_left T T' hTT' _ theorem setToL1SCLM_mono_left' {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : setToL1SCLM α E μ hT f ≤ setToL1SCLM α E μ hT' f := SimpleFunc.setToSimpleFunc_mono_left' T T' hTT' _ (SimpleFunc.integrable f) omit [IsOrderedAddMonoid G'] in theorem setToL1SCLM_nonneg {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁ₛ[μ] G'} (hf : 0 ≤ f) : 0 ≤ setToL1SCLM α G' μ hT f := setToL1S_nonneg (fun _ => hT.eq_zero_of_measure_zero) hT.1 hT_nonneg hf theorem setToL1SCLM_mono {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁ₛ[μ] G'} (hfg : f ≤ g) : setToL1SCLM α G' μ hT f ≤ setToL1SCLM α G' μ hT g := setToL1S_mono (fun _ => hT.eq_zero_of_measure_zero) hT.1 hT_nonneg hfg end Order end SetToL1S end SimpleFunc open SimpleFunc section SetToL1 attribute [local instance] Lp.simpleFunc.module attribute [local instance] Lp.simpleFunc.normedSpace variable (𝕜) [NontriviallyNormedField 𝕜] [NormedSpace 𝕜 E] [NormedSpace 𝕜 F] [CompleteSpace F] {T T' T'' : Set α → E →L[ℝ] F} {C C' C'' : ℝ} /-- Extend `Set α → (E →L[ℝ] F)` to `(α →₁[μ] E) →L[𝕜] F`. -/ def setToL1' (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) : (α →₁[μ] E) →L[𝕜] F := (setToL1SCLM' α E 𝕜 μ hT h_smul).extend (coeToLp α E 𝕜) (simpleFunc.denseRange one_ne_top) simpleFunc.isUniformInducing variable {𝕜} /-- Extend `Set α → E →L[ℝ] F` to `(α →₁[μ] E) →L[ℝ] F`. -/ def setToL1 (hT : DominatedFinMeasAdditive μ T C) : (α →₁[μ] E) →L[ℝ] F := (setToL1SCLM α E μ hT).extend (coeToLp α E ℝ) (simpleFunc.denseRange one_ne_top) simpleFunc.isUniformInducing theorem setToL1_eq_setToL1SCLM (hT : DominatedFinMeasAdditive μ T C) (f : α →₁ₛ[μ] E) : setToL1 hT f = setToL1SCLM α E μ hT f := uniformly_extend_of_ind simpleFunc.isUniformInducing (simpleFunc.denseRange one_ne_top) (setToL1SCLM α E μ hT).uniformContinuous _ theorem setToL1_eq_setToL1' (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (f : α →₁[μ] E) : setToL1 hT f = setToL1' 𝕜 hT h_smul f := rfl @[simp] theorem setToL1_zero_left (hT : DominatedFinMeasAdditive μ (0 : Set α → E →L[ℝ] F) C) (f : α →₁[μ] E) : setToL1 hT f = 0 := by suffices setToL1 hT = 0 by rw [this]; simp refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f rw [setToL1SCLM_zero_left hT f, ContinuousLinearMap.zero_comp, ContinuousLinearMap.zero_apply] theorem setToL1_zero_left' (hT : DominatedFinMeasAdditive μ T C) (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) (f : α →₁[μ] E) : setToL1 hT f = 0 := by suffices setToL1 hT = 0 by rw [this]; simp refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f rw [setToL1SCLM_zero_left' hT h_zero f, ContinuousLinearMap.zero_comp, ContinuousLinearMap.zero_apply] theorem setToL1_congr_left (T T' : Set α → E →L[ℝ] F) {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : T = T') (f : α →₁[μ] E) : setToL1 hT f = setToL1 hT' f := by suffices setToL1 hT = setToL1 hT' by rw [this] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f suffices setToL1 hT' f = setToL1SCLM α E μ hT f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM] exact setToL1SCLM_congr_left hT' hT h.symm f theorem setToL1_congr_left' (T T' : Set α → E →L[ℝ] F) {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α →₁[μ] E) : setToL1 hT f = setToL1 hT' f := by suffices setToL1 hT = setToL1 hT' by rw [this] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT) _ _ _ _ ?_ ext1 f suffices setToL1 hT' f = setToL1SCLM α E μ hT f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM] exact (setToL1SCLM_congr_left' hT hT' h f).symm theorem setToL1_add_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (f : α →₁[μ] E) : setToL1 (hT.add hT') f = setToL1 hT f + setToL1 hT' f := by suffices setToL1 (hT.add hT') = setToL1 hT + setToL1 hT' by rw [this, ContinuousLinearMap.add_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ (hT.add hT')) _ _ _ _ ?_ ext1 f suffices setToL1 hT f + setToL1 hT' f = setToL1SCLM α E μ (hT.add hT') f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1_eq_setToL1SCLM, setToL1SCLM_add_left hT hT'] theorem setToL1_add_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hT'' : DominatedFinMeasAdditive μ T'' C'') (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁[μ] E) : setToL1 hT'' f = setToL1 hT f + setToL1 hT' f := by suffices setToL1 hT'' = setToL1 hT + setToL1 hT' by rw [this, ContinuousLinearMap.add_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT'') _ _ _ _ ?_ ext1 f suffices setToL1 hT f + setToL1 hT' f = setToL1SCLM α E μ hT'' f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1_eq_setToL1SCLM, setToL1SCLM_add_left' hT hT' hT'' h_add] theorem setToL1_smul_left (hT : DominatedFinMeasAdditive μ T C) (c : ℝ) (f : α →₁[μ] E) : setToL1 (hT.smul c) f = c • setToL1 hT f := by suffices setToL1 (hT.smul c) = c • setToL1 hT by rw [this, ContinuousLinearMap.smul_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ (hT.smul c)) _ _ _ _ ?_ ext1 f suffices c • setToL1 hT f = setToL1SCLM α E μ (hT.smul c) f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1SCLM_smul_left c hT] theorem setToL1_smul_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (c : ℝ) (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α →₁[μ] E) : setToL1 hT' f = c • setToL1 hT f := by suffices setToL1 hT' = c • setToL1 hT by rw [this, ContinuousLinearMap.smul_apply] refine ContinuousLinearMap.extend_unique (setToL1SCLM α E μ hT') _ _ _ _ ?_ ext1 f suffices c • setToL1 hT f = setToL1SCLM α E μ hT' f by rw [← this]; simp [coeToLp] rw [setToL1_eq_setToL1SCLM, setToL1SCLM_smul_left' c hT hT' h_smul] theorem setToL1_smul (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α →₁[μ] E) : setToL1 hT (c • f) = c • setToL1 hT f := by rw [setToL1_eq_setToL1' hT h_smul, setToL1_eq_setToL1' hT h_smul] exact ContinuousLinearMap.map_smul _ _ _ theorem setToL1_simpleFunc_indicatorConst (hT : DominatedFinMeasAdditive μ T C) {s : Set α} (hs : MeasurableSet s) (hμs : μ s < ∞) (x : E) : setToL1 hT (simpleFunc.indicatorConst 1 hs hμs.ne x) = T s x := by rw [setToL1_eq_setToL1SCLM] exact setToL1S_indicatorConst (fun s => hT.eq_zero_of_measure_zero) hT.1 hs hμs x theorem setToL1_indicatorConstLp (hT : DominatedFinMeasAdditive μ T C) {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : E) : setToL1 hT (indicatorConstLp 1 hs hμs x) = T s x := by rw [← Lp.simpleFunc.coe_indicatorConst hs hμs x] exact setToL1_simpleFunc_indicatorConst hT hs hμs.lt_top x theorem setToL1_const [IsFiniteMeasure μ] (hT : DominatedFinMeasAdditive μ T C) (x : E) : setToL1 hT (indicatorConstLp 1 MeasurableSet.univ (measure_ne_top _ _) x) = T univ x := setToL1_indicatorConstLp hT MeasurableSet.univ (measure_ne_top _ _) x section Order variable {G' G'' : Type*} [NormedAddCommGroup G''] [PartialOrder G''] [OrderClosedTopology G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] [CompleteSpace G''] [NormedAddCommGroup G'] [PartialOrder G'] [NormedSpace ℝ G'] theorem setToL1_mono_left' {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁[μ] E) : setToL1 hT f ≤ setToL1 hT' f := by induction f using Lp.induction (hp_ne_top := one_ne_top) with | @indicatorConst c s hs hμs => rw [setToL1_simpleFunc_indicatorConst hT hs hμs, setToL1_simpleFunc_indicatorConst hT' hs hμs] exact hTT' s hs hμs c | @add f g hf hg _ hf_le hg_le => rw [(setToL1 hT).map_add, (setToL1 hT').map_add] exact add_le_add hf_le hg_le | isClosed => exact isClosed_le (setToL1 hT).continuous (setToL1 hT').continuous theorem setToL1_mono_left {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁[μ] E) : setToL1 hT f ≤ setToL1 hT' f := setToL1_mono_left' hT hT' (fun s _ _ x => hTT' s x) f theorem setToL1_nonneg {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁[μ] G'} (hf : 0 ≤ f) : 0 ≤ setToL1 hT f := by suffices ∀ f : { g : α →₁[μ] G' // 0 ≤ g }, 0 ≤ setToL1 hT f from this (⟨f, hf⟩ : { g : α →₁[μ] G' // 0 ≤ g }) refine fun g => @isClosed_property { g : α →₁ₛ[μ] G' // 0 ≤ g } { g : α →₁[μ] G' // 0 ≤ g } _ _ (fun g => 0 ≤ setToL1 hT g) (denseRange_coeSimpleFuncNonnegToLpNonneg 1 μ G' one_ne_top) ?_ ?_ g · exact isClosed_le continuous_zero ((setToL1 hT).continuous.comp continuous_induced_dom) · intro g have : (coeSimpleFuncNonnegToLpNonneg 1 μ G' g : α →₁[μ] G') = (g : α →₁ₛ[μ] G') := rfl rw [this, setToL1_eq_setToL1SCLM] exact setToL1S_nonneg (fun s => hT.eq_zero_of_measure_zero) hT.1 hT_nonneg g.2 theorem setToL1_mono [IsOrderedAddMonoid G'] {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁[μ] G'} (hfg : f ≤ g) : setToL1 hT f ≤ setToL1 hT g := by rw [← sub_nonneg] at hfg ⊢ rw [← (setToL1 hT).map_sub] exact setToL1_nonneg hT hT_nonneg hfg end Order theorem norm_setToL1_le_norm_setToL1SCLM (hT : DominatedFinMeasAdditive μ T C) : ‖setToL1 hT‖ ≤ ‖setToL1SCLM α E μ hT‖ := calc ‖setToL1 hT‖ ≤ (1 : ℝ≥0) * ‖setToL1SCLM α E μ hT‖ := by refine ContinuousLinearMap.opNorm_extend_le (setToL1SCLM α E μ hT) (coeToLp α E ℝ) (simpleFunc.denseRange one_ne_top) fun x => le_of_eq ?_ rw [NNReal.coe_one, one_mul] simp [coeToLp] _ = ‖setToL1SCLM α E μ hT‖ := by rw [NNReal.coe_one, one_mul] theorem norm_setToL1_le_mul_norm (hT : DominatedFinMeasAdditive μ T C) (hC : 0 ≤ C) (f : α →₁[μ] E) : ‖setToL1 hT f‖ ≤ C * ‖f‖ := calc ‖setToL1 hT f‖ ≤ ‖setToL1SCLM α E μ hT‖ * ‖f‖ := ContinuousLinearMap.le_of_opNorm_le _ (norm_setToL1_le_norm_setToL1SCLM hT) _ _ ≤ C * ‖f‖ := mul_le_mul (norm_setToL1SCLM_le hT hC) le_rfl (norm_nonneg _) hC theorem norm_setToL1_le_mul_norm' (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) : ‖setToL1 hT f‖ ≤ max C 0 * ‖f‖ := calc ‖setToL1 hT f‖ ≤ ‖setToL1SCLM α E μ hT‖ * ‖f‖ := ContinuousLinearMap.le_of_opNorm_le _ (norm_setToL1_le_norm_setToL1SCLM hT) _ _ ≤ max C 0 * ‖f‖ := mul_le_mul (norm_setToL1SCLM_le' hT) le_rfl (norm_nonneg _) (le_max_right _ _) theorem norm_setToL1_le (hT : DominatedFinMeasAdditive μ T C) (hC : 0 ≤ C) : ‖setToL1 hT‖ ≤ C := ContinuousLinearMap.opNorm_le_bound _ hC (norm_setToL1_le_mul_norm hT hC) theorem norm_setToL1_le' (hT : DominatedFinMeasAdditive μ T C) : ‖setToL1 hT‖ ≤ max C 0 := ContinuousLinearMap.opNorm_le_bound _ (le_max_right _ _) (norm_setToL1_le_mul_norm' hT) theorem setToL1_lipschitz (hT : DominatedFinMeasAdditive μ T C) : LipschitzWith (Real.toNNReal C) (setToL1 hT) := (setToL1 hT).lipschitz.weaken (norm_setToL1_le' hT) /-- If `fs i → f` in `L1`, then `setToL1 hT (fs i) → setToL1 hT f`. -/ theorem tendsto_setToL1 (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) {ι} (fs : ι → α →₁[μ] E) {l : Filter ι} (hfs : Tendsto fs l (𝓝 f)) : Tendsto (fun i => setToL1 hT (fs i)) l (𝓝 <| setToL1 hT f) := ((setToL1 hT).continuous.tendsto _).comp hfs end SetToL1 end L1 section Function variable [CompleteSpace F] {T T' T'' : Set α → E →L[ℝ] F} {C C' C'' : ℝ} {f g : α → E} variable (μ T) open Classical in /-- Extend `T : Set α → E →L[ℝ] F` to `(α → E) → F` (for integrable functions `α → E`). We set it to 0 if the function is not integrable. -/ def setToFun (hT : DominatedFinMeasAdditive μ T C) (f : α → E) : F := if hf : Integrable f μ then L1.setToL1 hT (hf.toL1 f) else 0 variable {μ T} theorem setToFun_eq (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) : setToFun μ T hT f = L1.setToL1 hT (hf.toL1 f) := dif_pos hf theorem L1.setToFun_eq_setToL1 (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) : setToFun μ T hT f = L1.setToL1 hT f := by rw [setToFun_eq hT (L1.integrable_coeFn f), Integrable.toL1_coeFn] theorem setToFun_undef (hT : DominatedFinMeasAdditive μ T C) (hf : ¬Integrable f μ) : setToFun μ T hT f = 0 := dif_neg hf theorem setToFun_non_aestronglyMeasurable (hT : DominatedFinMeasAdditive μ T C) (hf : ¬AEStronglyMeasurable f μ) : setToFun μ T hT f = 0 := setToFun_undef hT (not_and_of_not_left _ hf) @[deprecated (since := "2025-04-09")] alias setToFun_non_aEStronglyMeasurable := setToFun_non_aestronglyMeasurable theorem setToFun_congr_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : T = T') (f : α → E) : setToFun μ T hT f = setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_congr_left T T' hT hT' h] · simp_rw [setToFun_undef _ hf] theorem setToFun_congr_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (h : ∀ s, MeasurableSet s → μ s < ∞ → T s = T' s) (f : α → E) : setToFun μ T hT f = setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_congr_left' T T' hT hT' h] · simp_rw [setToFun_undef _ hf] theorem setToFun_add_left (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (f : α → E) : setToFun μ (T + T') (hT.add hT') f = setToFun μ T hT f + setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_add_left hT hT'] · simp_rw [setToFun_undef _ hf, add_zero] theorem setToFun_add_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hT'' : DominatedFinMeasAdditive μ T'' C'') (h_add : ∀ s, MeasurableSet s → μ s < ∞ → T'' s = T s + T' s) (f : α → E) : setToFun μ T'' hT'' f = setToFun μ T hT f + setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_add_left' hT hT' hT'' h_add] · simp_rw [setToFun_undef _ hf, add_zero] theorem setToFun_smul_left (hT : DominatedFinMeasAdditive μ T C) (c : ℝ) (f : α → E) : setToFun μ (fun s => c • T s) (hT.smul c) f = c • setToFun μ T hT f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_smul_left hT c] · simp_rw [setToFun_undef _ hf, smul_zero] theorem setToFun_smul_left' (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (c : ℝ) (h_smul : ∀ s, MeasurableSet s → μ s < ∞ → T' s = c • T s) (f : α → E) : setToFun μ T' hT' f = c • setToFun μ T hT f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf, L1.setToL1_smul_left' hT hT' c h_smul] · simp_rw [setToFun_undef _ hf, smul_zero] @[simp] theorem setToFun_zero (hT : DominatedFinMeasAdditive μ T C) : setToFun μ T hT (0 : α → E) = 0 := by rw [Pi.zero_def, setToFun_eq hT (integrable_zero _ _ _)] simp only [← Pi.zero_def] rw [Integrable.toL1_zero, ContinuousLinearMap.map_zero] @[simp] theorem setToFun_zero_left {hT : DominatedFinMeasAdditive μ (0 : Set α → E →L[ℝ] F) C} : setToFun μ 0 hT f = 0 := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf]; exact L1.setToL1_zero_left hT _ · exact setToFun_undef hT hf theorem setToFun_zero_left' (hT : DominatedFinMeasAdditive μ T C) (h_zero : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0) : setToFun μ T hT f = 0 := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf]; exact L1.setToL1_zero_left' hT h_zero _ · exact setToFun_undef hT hf theorem setToFun_add (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) (hg : Integrable g μ) : setToFun μ T hT (f + g) = setToFun μ T hT f + setToFun μ T hT g := by rw [setToFun_eq hT (hf.add hg), setToFun_eq hT hf, setToFun_eq hT hg, Integrable.toL1_add, (L1.setToL1 hT).map_add] theorem setToFun_finset_sum' (hT : DominatedFinMeasAdditive μ T C) {ι} (s : Finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, Integrable (f i) μ) : setToFun μ T hT (∑ i ∈ s, f i) = ∑ i ∈ s, setToFun μ T hT (f i) := by classical revert hf refine Finset.induction_on s ?_ ?_ · intro _ simp only [setToFun_zero, Finset.sum_empty] · intro i s his ih hf simp only [his, Finset.sum_insert, not_false_iff] rw [setToFun_add hT (hf i (Finset.mem_insert_self i s)) _] · rw [ih fun i hi => hf i (Finset.mem_insert_of_mem hi)] · convert integrable_finset_sum s fun i hi => hf i (Finset.mem_insert_of_mem hi) with x simp theorem setToFun_finset_sum (hT : DominatedFinMeasAdditive μ T C) {ι} (s : Finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, Integrable (f i) μ) : (setToFun μ T hT fun a => ∑ i ∈ s, f i a) = ∑ i ∈ s, setToFun μ T hT (f i) := by convert setToFun_finset_sum' hT s hf with a; simp theorem setToFun_neg (hT : DominatedFinMeasAdditive μ T C) (f : α → E) : setToFun μ T hT (-f) = -setToFun μ T hT f := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf, setToFun_eq hT hf.neg, Integrable.toL1_neg, (L1.setToL1 hT).map_neg] · rw [setToFun_undef hT hf, setToFun_undef hT, neg_zero] rwa [← integrable_neg_iff] at hf theorem setToFun_sub (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) (hg : Integrable g μ) : setToFun μ T hT (f - g) = setToFun μ T hT f - setToFun μ T hT g := by rw [sub_eq_add_neg, sub_eq_add_neg, setToFun_add hT hf hg.neg, setToFun_neg hT g] theorem setToFun_smul [NontriviallyNormedField 𝕜] [NormedSpace 𝕜 E] [NormedSpace 𝕜 F] (hT : DominatedFinMeasAdditive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α → E) : setToFun μ T hT (c • f) = c • setToFun μ T hT f := by by_cases hf : Integrable f μ · rw [setToFun_eq hT hf, setToFun_eq hT, Integrable.toL1_smul', L1.setToL1_smul hT h_smul c _] · by_cases hr : c = 0 · rw [hr]; simp · have hf' : ¬Integrable (c • f) μ := by rwa [integrable_smul_iff hr f] rw [setToFun_undef hT hf, setToFun_undef hT hf', smul_zero] theorem setToFun_congr_ae (hT : DominatedFinMeasAdditive μ T C) (h : f =ᵐ[μ] g) : setToFun μ T hT f = setToFun μ T hT g := by by_cases hfi : Integrable f μ · have hgi : Integrable g μ := hfi.congr h rw [setToFun_eq hT hfi, setToFun_eq hT hgi, (Integrable.toL1_eq_toL1_iff f g hfi hgi).2 h] · have hgi : ¬Integrable g μ := by rw [integrable_congr h] at hfi; exact hfi rw [setToFun_undef hT hfi, setToFun_undef hT hgi] theorem setToFun_measure_zero (hT : DominatedFinMeasAdditive μ T C) (h : μ = 0) : setToFun μ T hT f = 0 := by have : f =ᵐ[μ] 0 := by simp [h, EventuallyEq] rw [setToFun_congr_ae hT this, setToFun_zero] theorem setToFun_measure_zero' (hT : DominatedFinMeasAdditive μ T C) (h : ∀ s, MeasurableSet s → μ s < ∞ → μ s = 0) : setToFun μ T hT f = 0 := setToFun_zero_left' hT fun s hs hμs => hT.eq_zero_of_measure_zero hs (h s hs hμs) theorem setToFun_toL1 (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) : setToFun μ T hT (hf.toL1 f) = setToFun μ T hT f := setToFun_congr_ae hT hf.coeFn_toL1 theorem setToFun_indicator_const (hT : DominatedFinMeasAdditive μ T C) {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (x : E) : setToFun μ T hT (s.indicator fun _ => x) = T s x := by rw [setToFun_congr_ae hT (@indicatorConstLp_coeFn _ _ _ 1 _ _ _ hs hμs x).symm] rw [L1.setToFun_eq_setToL1 hT] exact L1.setToL1_indicatorConstLp hT hs hμs x theorem setToFun_const [IsFiniteMeasure μ] (hT : DominatedFinMeasAdditive μ T C) (x : E) : (setToFun μ T hT fun _ => x) = T univ x := by have : (fun _ : α => x) = Set.indicator univ fun _ => x := (indicator_univ _).symm rw [this] exact setToFun_indicator_const hT MeasurableSet.univ (measure_ne_top _ _) x section Order variable {G' G'' : Type*} [NormedAddCommGroup G''] [PartialOrder G''] [OrderClosedTopology G''] [IsOrderedAddMonoid G''] [NormedSpace ℝ G''] [CompleteSpace G''] [NormedAddCommGroup G'] [PartialOrder G'] [NormedSpace ℝ G'] theorem setToFun_mono_left' {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α → E) : setToFun μ T hT f ≤ setToFun μ T' hT' f := by by_cases hf : Integrable f μ · simp_rw [setToFun_eq _ hf]; exact L1.setToL1_mono_left' hT hT' hTT' _ · simp_rw [setToFun_undef _ hf, le_rfl] theorem setToFun_mono_left {T T' : Set α → E →L[ℝ] G''} {C C' : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁[μ] E) : setToFun μ T hT f ≤ setToFun μ T' hT' f := setToFun_mono_left' hT hT' (fun s _ _ x => hTT' s x) f theorem setToFun_nonneg {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α → G'} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ setToFun μ T hT f := by by_cases hfi : Integrable f μ · simp_rw [setToFun_eq _ hfi] refine L1.setToL1_nonneg hT hT_nonneg ?_ rw [← Lp.coeFn_le] have h0 := Lp.coeFn_zero G' 1 μ have h := Integrable.coeFn_toL1 hfi filter_upwards [h0, h, hf] with _ h0a ha hfa rw [h0a, ha] exact hfa · simp_rw [setToFun_undef _ hfi, le_rfl] theorem setToFun_mono [IsOrderedAddMonoid G'] {T : Set α → G' →L[ℝ] G''} {C : ℝ} (hT : DominatedFinMeasAdditive μ T C) (hT_nonneg : ∀ s, MeasurableSet s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α → G'} (hf : Integrable f μ) (hg : Integrable g μ) (hfg : f ≤ᵐ[μ] g) : setToFun μ T hT f ≤ setToFun μ T hT g := by rw [← sub_nonneg, ← setToFun_sub hT hg hf] refine setToFun_nonneg hT hT_nonneg (hfg.mono fun a ha => ?_) rw [Pi.sub_apply, Pi.zero_apply, sub_nonneg] exact ha end Order @[continuity] theorem continuous_setToFun (hT : DominatedFinMeasAdditive μ T C) : Continuous fun f : α →₁[μ] E => setToFun μ T hT f := by simp_rw [L1.setToFun_eq_setToL1 hT]; exact ContinuousLinearMap.continuous _ /-- If `F i → f` in `L1`, then `setToFun μ T hT (F i) → setToFun μ T hT f`. -/ theorem tendsto_setToFun_of_L1 (hT : DominatedFinMeasAdditive μ T C) {ι} (f : α → E) (hfi : Integrable f μ) {fs : ι → α → E} {l : Filter ι} (hfsi : ∀ᶠ i in l, Integrable (fs i) μ) (hfs : Tendsto (fun i => ∫⁻ x, ‖fs i x - f x‖ₑ ∂μ) l (𝓝 0)) : Tendsto (fun i => setToFun μ T hT (fs i)) l (𝓝 <| setToFun μ T hT f) := by classical let f_lp := hfi.toL1 f let F_lp i := if hFi : Integrable (fs i) μ then hFi.toL1 (fs i) else 0 have tendsto_L1 : Tendsto F_lp l (𝓝 f_lp) := by rw [Lp.tendsto_Lp_iff_tendsto_eLpNorm'] simp_rw [eLpNorm_one_eq_lintegral_enorm, Pi.sub_apply] refine (tendsto_congr' ?_).mp hfs filter_upwards [hfsi] with i hi refine lintegral_congr_ae ?_ filter_upwards [hi.coeFn_toL1, hfi.coeFn_toL1] with x hxi hxf simp_rw [F_lp, dif_pos hi, hxi, f_lp, hxf] suffices Tendsto (fun i => setToFun μ T hT (F_lp i)) l (𝓝 (setToFun μ T hT f)) by refine (tendsto_congr' ?_).mp this filter_upwards [hfsi] with i hi suffices h_ae_eq : F_lp i =ᵐ[μ] fs i from setToFun_congr_ae hT h_ae_eq simp_rw [F_lp, dif_pos hi] exact hi.coeFn_toL1 rw [setToFun_congr_ae hT hfi.coeFn_toL1.symm] exact ((continuous_setToFun hT).tendsto f_lp).comp tendsto_L1 theorem tendsto_setToFun_approxOn_of_measurable (hT : DominatedFinMeasAdditive μ T C) [MeasurableSpace E] [BorelSpace E] {f : α → E} {s : Set E} [SeparableSpace s] (hfi : Integrable f μ) (hfm : Measurable f) (hs : ∀ᵐ x ∂μ, f x ∈ closure s) {y₀ : E} (h₀ : y₀ ∈ s) (h₀i : Integrable (fun _ => y₀) μ) : Tendsto (fun n => setToFun μ T hT (SimpleFunc.approxOn f hfm s y₀ h₀ n)) atTop (𝓝 <| setToFun μ T hT f) := tendsto_setToFun_of_L1 hT _ hfi (Eventually.of_forall (SimpleFunc.integrable_approxOn hfm hfi h₀ h₀i)) (SimpleFunc.tendsto_approxOn_L1_enorm hfm _ hs (hfi.sub h₀i).2) theorem tendsto_setToFun_approxOn_of_measurable_of_range_subset (hT : DominatedFinMeasAdditive μ T C) [MeasurableSpace E] [BorelSpace E] {f : α → E} (fmeas : Measurable f) (hf : Integrable f μ) (s : Set E) [SeparableSpace s] (hs : range f ∪ {0} ⊆ s) : Tendsto (fun n => setToFun μ T hT (SimpleFunc.approxOn f fmeas s 0 (hs <| by simp) n)) atTop (𝓝 <| setToFun μ T hT f) := by refine tendsto_setToFun_approxOn_of_measurable hT hf fmeas ?_ _ (integrable_zero _ _ _) exact Eventually.of_forall fun x => subset_closure (hs (Set.mem_union_left _ (mem_range_self _))) /-- Auxiliary lemma for `setToFun_congr_measure`: the function sending `f : α →₁[μ] G` to `f : α →₁[μ'] G` is continuous when `μ' ≤ c' • μ` for `c' ≠ ∞`. -/ theorem continuous_L1_toL1 {μ' : Measure α} (c' : ℝ≥0∞) (hc' : c' ≠ ∞) (hμ'_le : μ' ≤ c' • μ) : Continuous fun f : α →₁[μ] G => (Integrable.of_measure_le_smul hc' hμ'_le (L1.integrable_coeFn f)).toL1 f := by by_cases hc'0 : c' = 0 · have hμ'0 : μ' = 0 := by rw [← Measure.nonpos_iff_eq_zero']; refine hμ'_le.trans ?_; simp [hc'0] have h_im_zero : (fun f : α →₁[μ] G => (Integrable.of_measure_le_smul hc' hμ'_le (L1.integrable_coeFn f)).toL1 f) = 0 := by ext1 f; ext1; simp_rw [hμ'0]; simp only [ae_zero, EventuallyEq, eventually_bot] rw [h_im_zero] exact continuous_zero rw [Metric.continuous_iff] intro f ε hε_pos use ε / 2 / c'.toReal refine ⟨div_pos (half_pos hε_pos) (toReal_pos hc'0 hc'), ?_⟩ intro g hfg rw [Lp.dist_def] at hfg ⊢ let h_int := fun f' : α →₁[μ] G => (L1.integrable_coeFn f').of_measure_le_smul hc' hμ'_le have : eLpNorm (⇑(Integrable.toL1 g (h_int g)) - ⇑(Integrable.toL1 f (h_int f))) 1 μ' = eLpNorm (⇑g - ⇑f) 1 μ' := eLpNorm_congr_ae ((Integrable.coeFn_toL1 _).sub (Integrable.coeFn_toL1 _)) rw [this] have h_eLpNorm_ne_top : eLpNorm (⇑g - ⇑f) 1 μ ≠ ∞ := by rw [← eLpNorm_congr_ae (Lp.coeFn_sub _ _)]; exact Lp.eLpNorm_ne_top _ calc (eLpNorm (⇑g - ⇑f) 1 μ').toReal ≤ (c' * eLpNorm (⇑g - ⇑f) 1 μ).toReal := by refine toReal_mono (ENNReal.mul_ne_top hc' h_eLpNorm_ne_top) ?_ refine (eLpNorm_mono_measure (⇑g - ⇑f) hμ'_le).trans_eq ?_ rw [eLpNorm_smul_measure_of_ne_zero hc'0, smul_eq_mul] simp _ = c'.toReal * (eLpNorm (⇑g - ⇑f) 1 μ).toReal := toReal_mul _ ≤ c'.toReal * (ε / 2 / c'.toReal) := by gcongr _ = ε / 2 := by refine mul_div_cancel₀ (ε / 2) ?_; rw [Ne, toReal_eq_zero_iff]; simp [hc', hc'0] _ < ε := half_lt_self hε_pos theorem setToFun_congr_measure_of_integrable {μ' : Measure α} (c' : ℝ≥0∞) (hc' : c' ≠ ∞) (hμ'_le : μ' ≤ c' • μ) (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ' T C') (f : α → E) (hfμ : Integrable f μ) : setToFun μ T hT f = setToFun μ' T hT' f := by -- integrability for `μ` implies integrability for `μ'`. have h_int : ∀ g : α → E, Integrable g μ → Integrable g μ' := fun g hg => Integrable.of_measure_le_smul hc' hμ'_le hg -- We use `Integrable.induction` apply hfμ.induction (P := fun f => setToFun μ T hT f = setToFun μ' T hT' f) · intro c s hs hμs have hμ's : μ' s ≠ ∞ := by refine ((hμ'_le s).trans_lt ?_).ne rw [Measure.smul_apply, smul_eq_mul] exact ENNReal.mul_lt_top hc'.lt_top hμs rw [setToFun_indicator_const hT hs hμs.ne, setToFun_indicator_const hT' hs hμ's] · intro f₂ g₂ _ hf₂ hg₂ h_eq_f h_eq_g rw [setToFun_add hT hf₂ hg₂, setToFun_add hT' (h_int f₂ hf₂) (h_int g₂ hg₂), h_eq_f, h_eq_g] · refine isClosed_eq (continuous_setToFun hT) ?_ have : (fun f : α →₁[μ] E => setToFun μ' T hT' f) = fun f : α →₁[μ] E => setToFun μ' T hT' ((h_int f (L1.integrable_coeFn f)).toL1 f) := by ext1 f; exact setToFun_congr_ae hT' (Integrable.coeFn_toL1 _).symm rw [this] exact (continuous_setToFun hT').comp (continuous_L1_toL1 c' hc' hμ'_le) · intro f₂ g₂ hfg _ hf_eq have hfg' : f₂ =ᵐ[μ'] g₂ := (Measure.absolutelyContinuous_of_le_smul hμ'_le).ae_eq hfg rw [← setToFun_congr_ae hT hfg, hf_eq, setToFun_congr_ae hT' hfg'] theorem setToFun_congr_measure {μ' : Measure α} (c c' : ℝ≥0∞) (hc : c ≠ ∞) (hc' : c' ≠ ∞) (hμ_le : μ ≤ c • μ') (hμ'_le : μ' ≤ c' • μ) (hT : DominatedFinMeasAdditive μ T C) (hT' : DominatedFinMeasAdditive μ' T C') (f : α → E) : setToFun μ T hT f = setToFun μ' T hT' f := by by_cases hf : Integrable f μ · exact setToFun_congr_measure_of_integrable c' hc' hμ'_le hT hT' f hf · -- if `f` is not integrable, both `setToFun` are 0. have h_int : ∀ g : α → E, ¬Integrable g μ → ¬Integrable g μ' := fun g => mt fun h => h.of_measure_le_smul hc hμ_le simp_rw [setToFun_undef _ hf, setToFun_undef _ (h_int f hf)] theorem setToFun_congr_measure_of_add_right {μ' : Measure α} (hT_add : DominatedFinMeasAdditive (μ + μ') T C') (hT : DominatedFinMeasAdditive μ T C) (f : α → E) (hf : Integrable f (μ + μ')) : setToFun (μ + μ') T hT_add f = setToFun μ T hT f := by refine setToFun_congr_measure_of_integrable 1 one_ne_top ?_ hT_add hT f hf rw [one_smul] nth_rw 1 [← add_zero μ] exact add_le_add le_rfl bot_le theorem setToFun_congr_measure_of_add_left {μ' : Measure α} (hT_add : DominatedFinMeasAdditive (μ + μ') T C') (hT : DominatedFinMeasAdditive μ' T C) (f : α → E) (hf : Integrable f (μ + μ')) : setToFun (μ + μ') T hT_add f = setToFun μ' T hT f := by refine setToFun_congr_measure_of_integrable 1 one_ne_top ?_ hT_add hT f hf rw [one_smul] nth_rw 1 [← zero_add μ'] exact add_le_add_right bot_le μ' theorem setToFun_top_smul_measure (hT : DominatedFinMeasAdditive (∞ • μ) T C) (f : α → E) : setToFun (∞ • μ) T hT f = 0 := by refine setToFun_measure_zero' hT fun s _ hμs => ?_ rw [lt_top_iff_ne_top] at hμs simp only [true_and, Measure.smul_apply, ENNReal.mul_eq_top, eq_self_iff_true, top_ne_zero, Ne, not_false_iff, not_or, Classical.not_not, smul_eq_mul] at hμs simp only [hμs.right, Measure.smul_apply, mul_zero, smul_eq_mul] theorem setToFun_congr_smul_measure (c : ℝ≥0∞) (hc_ne_top : c ≠ ∞) (hT : DominatedFinMeasAdditive μ T C) (hT_smul : DominatedFinMeasAdditive (c • μ) T C') (f : α → E) : setToFun μ T hT f = setToFun (c • μ) T hT_smul f := by by_cases hc0 : c = 0 · simp [hc0] at hT_smul have h : ∀ s, MeasurableSet s → μ s < ∞ → T s = 0 := fun s hs _ => hT_smul.eq_zero hs rw [setToFun_zero_left' _ h, setToFun_measure_zero] simp [hc0] refine setToFun_congr_measure c⁻¹ c ?_ hc_ne_top (le_of_eq ?_) le_rfl hT hT_smul f · simp [hc0] · rw [smul_smul, ENNReal.inv_mul_cancel hc0 hc_ne_top, one_smul] theorem norm_setToFun_le_mul_norm (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) (hC : 0 ≤ C) : ‖setToFun μ T hT f‖ ≤ C * ‖f‖ := by rw [L1.setToFun_eq_setToL1]; exact L1.norm_setToL1_le_mul_norm hT hC f theorem norm_setToFun_le_mul_norm' (hT : DominatedFinMeasAdditive μ T C) (f : α →₁[μ] E) : ‖setToFun μ T hT f‖ ≤ max C 0 * ‖f‖ := by rw [L1.setToFun_eq_setToL1]; exact L1.norm_setToL1_le_mul_norm' hT f theorem norm_setToFun_le (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) (hC : 0 ≤ C) : ‖setToFun μ T hT f‖ ≤ C * ‖hf.toL1 f‖ := by rw [setToFun_eq hT hf]; exact L1.norm_setToL1_le_mul_norm hT hC _ theorem norm_setToFun_le' (hT : DominatedFinMeasAdditive μ T C) (hf : Integrable f μ) : ‖setToFun μ T hT f‖ ≤ max C 0 * ‖hf.toL1 f‖ := by rw [setToFun_eq hT hf]; exact L1.norm_setToL1_le_mul_norm' hT _ /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their image by `setToFun`. We could weaken the condition `bound_integrable` to require `HasFiniteIntegral bound μ` instead (i.e. not requiring that `bound` is measurable), but in all applications proving integrability is easier. -/ theorem tendsto_setToFun_of_dominated_convergence (hT : DominatedFinMeasAdditive μ T C) {fs : ℕ → α → E} {f : α → E} (bound : α → ℝ) (fs_measurable : ∀ n, AEStronglyMeasurable (fs n) μ) (bound_integrable : Integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖fs n a‖ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, Tendsto (fun n => fs n a) atTop (𝓝 (f a))) : Tendsto (fun n => setToFun μ T hT (fs n)) atTop (𝓝 <| setToFun μ T hT f) := by -- `f` is a.e.-measurable, since it is the a.e.-pointwise limit of a.e.-measurable functions. have f_measurable : AEStronglyMeasurable f μ := aestronglyMeasurable_of_tendsto_ae _ fs_measurable h_lim -- all functions we consider are integrable have fs_int : ∀ n, Integrable (fs n) μ := fun n => bound_integrable.mono' (fs_measurable n) (h_bound _) have f_int : Integrable f μ := ⟨f_measurable, hasFiniteIntegral_of_dominated_convergence bound_integrable.hasFiniteIntegral h_bound h_lim⟩ -- it suffices to prove the result for the corresponding L1 functions suffices Tendsto (fun n => L1.setToL1 hT ((fs_int n).toL1 (fs n))) atTop (𝓝 (L1.setToL1 hT (f_int.toL1 f))) by convert this with n · exact setToFun_eq hT (fs_int n) · exact setToFun_eq hT f_int -- the convergence of setToL1 follows from the convergence of the L1 functions refine L1.tendsto_setToL1 hT _ _ ?_ -- up to some rewriting, what we need to prove is `h_lim` rw [tendsto_iff_norm_sub_tendsto_zero] have lintegral_norm_tendsto_zero : Tendsto (fun n => ENNReal.toReal <| ∫⁻ a, ENNReal.ofReal ‖fs n a - f a‖ ∂μ) atTop (𝓝 0) := (tendsto_toReal zero_ne_top).comp (tendsto_lintegral_norm_of_dominated_convergence fs_measurable bound_integrable.hasFiniteIntegral h_bound h_lim) convert lintegral_norm_tendsto_zero with n rw [L1.norm_def] congr 1 refine lintegral_congr_ae ?_ rw [← Integrable.toL1_sub] refine ((fs_int n).sub f_int).coeFn_toL1.mono fun x hx => ?_ dsimp only rw [hx, ofReal_norm_eq_enorm, Pi.sub_apply] /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ theorem tendsto_setToFun_filter_of_dominated_convergence (hT : DominatedFinMeasAdditive μ T C) {ι} {l : Filter ι} [l.IsCountablyGenerated] {fs : ι → α → E} {f : α → E} (bound : α → ℝ) (hfs_meas : ∀ᶠ n in l, AEStronglyMeasurable (fs n) μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ‖fs n a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_lim : ∀ᵐ a ∂μ, Tendsto (fun n => fs n a) l (𝓝 (f a))) : Tendsto (fun n => setToFun μ T hT (fs n)) l (𝓝 <| setToFun μ T hT f) := by rw [tendsto_iff_seq_tendsto] intro x xl have hxl : ∀ s ∈ l, ∃ a, ∀ b ≥ a, x b ∈ s := by rwa [tendsto_atTop'] at xl have h : { x : ι | (fun n => AEStronglyMeasurable (fs n) μ) x } ∩ { x : ι | (fun n => ∀ᵐ a ∂μ, ‖fs n a‖ ≤ bound a) x } ∈ l := inter_mem hfs_meas h_bound obtain ⟨k, h⟩ := hxl _ h rw [← tendsto_add_atTop_iff_nat k] refine tendsto_setToFun_of_dominated_convergence hT bound ?_ bound_integrable ?_ ?_ · exact fun n => (h _ (self_le_add_left _ _)).1 · exact fun n => (h _ (self_le_add_left _ _)).2 · filter_upwards [h_lim] refine fun a h_lin => @Tendsto.comp _ _ _ (fun n => x (n + k)) (fun n => fs n a) _ _ _ h_lin ?_ rwa [tendsto_add_atTop_iff_nat] variable {X : Type*} [TopologicalSpace X] [FirstCountableTopology X] theorem continuousWithinAt_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {x₀ : X} {bound : α → ℝ} {s : Set X} (hfs_meas : ∀ᶠ x in 𝓝[s] x₀, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, ContinuousWithinAt (fun x => fs x a) s x₀) : ContinuousWithinAt (fun x => setToFun μ T hT (fs x)) s x₀ := tendsto_setToFun_filter_of_dominated_convergence hT bound ‹_› ‹_› ‹_› ‹_› theorem continuousAt_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {x₀ : X} {bound : α → ℝ} (hfs_meas : ∀ᶠ x in 𝓝 x₀, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, ContinuousAt (fun x => fs x a) x₀) : ContinuousAt (fun x => setToFun μ T hT (fs x)) x₀ := tendsto_setToFun_filter_of_dominated_convergence hT bound ‹_› ‹_› ‹_› ‹_› theorem continuousOn_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {bound : α → ℝ} {s : Set X} (hfs_meas : ∀ x ∈ s, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ x ∈ s, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, ContinuousOn (fun x => fs x a) s) : ContinuousOn (fun x => setToFun μ T hT (fs x)) s := by intro x hx refine continuousWithinAt_setToFun_of_dominated hT ?_ ?_ bound_integrable ?_ · filter_upwards [self_mem_nhdsWithin] with x hx using hfs_meas x hx · filter_upwards [self_mem_nhdsWithin] with x hx using h_bound x hx · filter_upwards [h_cont] with a ha using ha x hx theorem continuous_setToFun_of_dominated (hT : DominatedFinMeasAdditive μ T C) {fs : X → α → E} {bound : α → ℝ} (hfs_meas : ∀ x, AEStronglyMeasurable (fs x) μ) (h_bound : ∀ x, ∀ᵐ a ∂μ, ‖fs x a‖ ≤ bound a) (bound_integrable : Integrable bound μ) (h_cont : ∀ᵐ a ∂μ, Continuous fun x => fs x a) : Continuous fun x => setToFun μ T hT (fs x) := continuous_iff_continuousAt.mpr fun _ => continuousAt_setToFun_of_dominated hT (Eventually.of_forall hfs_meas) (Eventually.of_forall h_bound) ‹_› <| h_cont.mono fun _ => Continuous.continuousAt end Function end MeasureTheory
Mathlib/MeasureTheory/Integral/SetToL1.lean
1,636
1,643
/- 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
2,435
2,440
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro -/ import Mathlib.Data.List.AList import Mathlib.Data.Finset.Sigma import Mathlib.Data.Part /-! # Finite maps over `Multiset` -/ universe u v w open List variable {α : Type u} {β : α → Type v} /-! ### Multisets of sigma types -/ namespace Multiset /-- Multiset of keys of an association multiset. -/ def keys (s : Multiset (Sigma β)) : Multiset α := s.map Sigma.fst @[simp] theorem coe_keys {l : List (Sigma β)} : keys (l : Multiset (Sigma β)) = (l.keys : Multiset α) := rfl @[simp] theorem keys_zero : keys (0 : Multiset (Sigma β)) = 0 := rfl @[simp] theorem keys_cons {a : α} {b : β a} {s : Multiset (Sigma β)} : keys (⟨a, b⟩ ::ₘ s) = a ::ₘ keys s := by simp [keys] @[simp] theorem keys_singleton {a : α} {b : β a} : keys ({⟨a, b⟩} : Multiset (Sigma β)) = {a} := rfl /-- `NodupKeys s` means that `s` has no duplicate keys. -/ def NodupKeys (s : Multiset (Sigma β)) : Prop := Quot.liftOn s List.NodupKeys fun _ _ p => propext <| perm_nodupKeys p @[simp] theorem coe_nodupKeys {l : List (Sigma β)} : @NodupKeys α β l ↔ l.NodupKeys := Iff.rfl lemma nodup_keys {m : Multiset (Σ a, β a)} : m.keys.Nodup ↔ m.NodupKeys := by rcases m with ⟨l⟩; rfl alias ⟨_, NodupKeys.nodup_keys⟩ := nodup_keys protected lemma NodupKeys.nodup {m : Multiset (Σ a, β a)} (h : m.NodupKeys) : m.Nodup := h.nodup_keys.of_map _ end Multiset /-! ### Finmap -/ /-- `Finmap β` is the type of finite maps over a multiset. It is effectively a quotient of `AList β` by permutation of the underlying list. -/ structure Finmap (β : α → Type v) : Type max u v where /-- The underlying `Multiset` of a `Finmap` -/ entries : Multiset (Sigma β) /-- There are no duplicate keys in `entries` -/ nodupKeys : entries.NodupKeys /-- The quotient map from `AList` to `Finmap`. -/ def AList.toFinmap (s : AList β) : Finmap β := ⟨s.entries, s.nodupKeys⟩ local notation:arg "⟦" a "⟧" => AList.toFinmap a theorem AList.toFinmap_eq {s₁ s₂ : AList β} : toFinmap s₁ = toFinmap s₂ ↔ s₁.entries ~ s₂.entries := by cases s₁ cases s₂ simp [AList.toFinmap] @[simp] theorem AList.toFinmap_entries (s : AList β) : ⟦s⟧.entries = s.entries := rfl /-- Given `l : List (Sigma β)`, create a term of type `Finmap β` by removing entries with duplicate keys. -/ def List.toFinmap [DecidableEq α] (s : List (Sigma β)) : Finmap β := s.toAList.toFinmap namespace Finmap open AList lemma nodup_entries (f : Finmap β) : f.entries.Nodup := f.nodupKeys.nodup /-! ### Lifting from AList -/ /-- Lift a permutation-respecting function on `AList` to `Finmap`. -/ def liftOn {γ} (s : Finmap β) (f : AList β → γ) (H : ∀ a b : AList β, a.entries ~ b.entries → f a = f b) : γ := by refine (Quotient.liftOn s.entries (fun (l : List (Sigma β)) => (⟨_, fun nd => f ⟨l, nd⟩⟩ : Part γ)) (fun l₁ l₂ p => Part.ext' (perm_nodupKeys p) ?_) : Part γ).get ?_ · exact fun h1 h2 => H _ _ p · have := s.nodupKeys revert this rcases s.entries with ⟨l⟩ exact id @[simp] theorem liftOn_toFinmap {γ} (s : AList β) (f : AList β → γ) (H) : liftOn ⟦s⟧ f H = f s := by cases s rfl /-- Lift a permutation-respecting function on 2 `AList`s to 2 `Finmap`s. -/ def liftOn₂ {γ} (s₁ s₂ : Finmap β) (f : AList β → AList β → γ) (H : ∀ a₁ b₁ a₂ b₂ : AList β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries → f a₁ b₁ = f a₂ b₂) : γ := liftOn s₁ (fun l₁ => liftOn s₂ (f l₁) fun _ _ p => H _ _ _ _ (Perm.refl _) p) fun a₁ a₂ p => by have H' : f a₁ = f a₂ := funext fun _ => H _ _ _ _ p (Perm.refl _) simp only [H'] @[simp] theorem liftOn₂_toFinmap {γ} (s₁ s₂ : AList β) (f : AList β → AList β → γ) (H) : liftOn₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ := by cases s₁; cases s₂; rfl /-! ### Induction -/ @[elab_as_elim] theorem induction_on {C : Finmap β → Prop} (s : Finmap β) (H : ∀ a : AList β, C ⟦a⟧) : C s := by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩ @[elab_as_elim] theorem induction_on₂ {C : Finmap β → Finmap β → Prop} (s₁ s₂ : Finmap β) (H : ∀ a₁ a₂ : AList β, C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ := induction_on s₁ fun l₁ => induction_on s₂ fun l₂ => H l₁ l₂ @[elab_as_elim] theorem induction_on₃ {C : Finmap β → Finmap β → Finmap β → Prop} (s₁ s₂ s₃ : Finmap β) (H : ∀ a₁ a₂ a₃ : AList β, C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ := induction_on₂ s₁ s₂ fun l₁ l₂ => induction_on s₃ fun l₃ => H l₁ l₂ l₃ /-! ### extensionality -/ @[ext] theorem ext : ∀ {s t : Finmap β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩, ⟨l₂, _⟩, H => by congr @[simp] theorem ext_iff' {s t : Finmap β} : s.entries = t.entries ↔ s = t := Finmap.ext_iff.symm /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : Membership α (Finmap β) := ⟨fun s a => a ∈ s.entries.keys⟩ theorem mem_def {a : α} {s : Finmap β} : a ∈ s ↔ a ∈ s.entries.keys := Iff.rfl @[simp] theorem mem_toFinmap {a : α} {s : AList β} : a ∈ toFinmap s ↔ a ∈ s := Iff.rfl /-! ### keys -/ /-- The set of keys of a finite map. -/ def keys (s : Finmap β) : Finset α := ⟨s.entries.keys, s.nodupKeys.nodup_keys⟩ @[simp] theorem keys_val (s : AList β) : (keys ⟦s⟧).val = s.keys := rfl @[simp] theorem keys_ext {s₁ s₂ : AList β} : keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys := by simp [keys, AList.keys] theorem mem_keys {a : α} {s : Finmap β} : a ∈ s.keys ↔ a ∈ s := induction_on s fun _ => AList.mem_keys /-! ### empty -/ /-- The empty map. -/ instance : EmptyCollection (Finmap β) := ⟨⟨0, nodupKeys_nil⟩⟩ instance : Inhabited (Finmap β) := ⟨∅⟩ @[simp] theorem empty_toFinmap : (⟦∅⟧ : Finmap β) = ∅ := rfl @[simp] theorem toFinmap_nil [DecidableEq α] : ([].toFinmap : Finmap β) = ∅ := rfl theorem not_mem_empty {a : α} : a ∉ (∅ : Finmap β) := Multiset.not_mem_zero a @[simp] theorem keys_empty : (∅ : Finmap β).keys = ∅ := rfl /-! ### singleton -/ /-- The singleton map. -/ def singleton (a : α) (b : β a) : Finmap β := ⟦AList.singleton a b⟧ @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = {a} := rfl @[simp] theorem mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y := by simp [singleton, mem_def] section variable [DecidableEq α] instance decidableEq [∀ a, DecidableEq (β a)] : DecidableEq (Finmap β) | _, _ => decidable_of_iff _ Finmap.ext_iff.symm /-! ### lookup -/ /-- Look up the value associated to a key in a map. -/ def lookup (a : α) (s : Finmap β) : Option (β a) := liftOn s (AList.lookup a) fun _ _ => perm_lookup @[simp] theorem lookup_toFinmap (a : α) (s : AList β) : lookup a ⟦s⟧ = s.lookup a := rfl @[simp] theorem dlookup_list_toFinmap (a : α) (s : List (Sigma β)) : lookup a s.toFinmap = s.dlookup a := by rw [List.toFinmap, lookup_toFinmap, lookup_to_alist] @[simp] theorem lookup_empty (a) : lookup a (∅ : Finmap β) = none := rfl theorem lookup_isSome {a : α} {s : Finmap β} : (s.lookup a).isSome ↔ a ∈ s := induction_on s fun _ => AList.lookup_isSome theorem lookup_eq_none {a} {s : Finmap β} : lookup a s = none ↔ a ∉ s := induction_on s fun _ => AList.lookup_eq_none lemma mem_lookup_iff {s : Finmap β} {a : α} {b : β a} : b ∈ s.lookup a ↔ Sigma.mk a b ∈ s.entries := by rcases s with ⟨⟨l⟩, hl⟩; exact List.mem_dlookup_iff hl lemma lookup_eq_some_iff {s : Finmap β} {a : α} {b : β a} : s.lookup a = b ↔ Sigma.mk a b ∈ s.entries := mem_lookup_iff @[simp] lemma sigma_keys_lookup (s : Finmap β) : s.keys.sigma (fun i => (s.lookup i).toFinset) = ⟨s.entries, s.nodup_entries⟩ := by ext x have : x ∈ s.entries → x.1 ∈ s.keys := Multiset.mem_map_of_mem _ simpa [lookup_eq_some_iff] @[simp] theorem lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b := by rw [singleton, lookup_toFinmap, AList.singleton, AList.lookup, dlookup_cons_eq] instance (a : α) (s : Finmap β) : Decidable (a ∈ s) := decidable_of_iff _ lookup_isSome theorem mem_iff {a : α} {s : Finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b := induction_on s fun s => Iff.trans List.mem_keys <| exists_congr fun _ => (mem_dlookup_iff s.nodupKeys).symm theorem mem_of_lookup_eq_some {a : α} {b : β a} {s : Finmap β} (h : s.lookup a = some b) : a ∈ s := mem_iff.mpr ⟨_, h⟩ theorem ext_lookup {s₁ s₂ : Finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ := induction_on₂ s₁ s₂ fun s₁ s₂ h => by simp only [AList.lookup, lookup_toFinmap] at h rw [AList.toFinmap_eq] apply lookup_ext s₁.nodupKeys s₂.nodupKeys intro x y rw [h] /-- An equivalence between `Finmap β` and pairs `(keys : Finset α, lookup : ∀ a, Option (β a))` such that `(lookup a).isSome ↔ a ∈ keys`. -/ @[simps apply_coe_fst apply_coe_snd] def keysLookupEquiv : Finmap β ≃ { f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1 } where toFun s := ⟨(s.keys, fun i => s.lookup i), fun _ => lookup_isSome⟩ invFun f := mk (f.1.1.sigma fun i => (f.1.2 i).toFinset).val <| by refine Multiset.nodup_keys.1 ((Finset.nodup _).map_on ?_) simp only [Finset.mem_val, Finset.mem_sigma, Option.mem_toFinset, Option.mem_def] rintro ⟨i, x⟩ ⟨_, hx⟩ ⟨j, y⟩ ⟨_, hy⟩ (rfl : i = j) simpa using hx.symm.trans hy left_inv f := ext <| by simp right_inv := fun ⟨(s, f), hf⟩ => by dsimp only at hf ext · simp [keys, Multiset.keys, ← hf, Option.isSome_iff_exists] · simp +contextual [lookup_eq_some_iff, ← hf] @[simp] lemma keysLookupEquiv_symm_apply_keys : ∀ f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}, (keysLookupEquiv.symm f).keys = f.1.1 := keysLookupEquiv.surjective.forall.2 fun _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_fst] @[simp] lemma keysLookupEquiv_symm_apply_lookup : ∀ (f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}) a, (keysLookupEquiv.symm f).lookup a = f.1.2 a := keysLookupEquiv.surjective.forall.2 fun _ _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_snd] /-! ### replace -/ /-- Replace a key with a given value in a finite map. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.replace a b t)) fun _ _ p => toFinmap_eq.2 <| perm_replace p @[simp] theorem replace_toFinmap (a : α) (b : β a) (s : AList β) : replace a b ⟦s⟧ = (⟦s.replace a b⟧ : Finmap β) := by simp [replace] @[simp] theorem keys_replace (a : α) (b : β a) (s : Finmap β) : (replace a b s).keys = s.keys := induction_on s fun s => by simp @[simp] theorem mem_replace {a a' : α} {b : β a} {s : Finmap β} : a' ∈ replace a b s ↔ a' ∈ s := induction_on s fun s => by simp end /-! ### foldl -/ /-- Fold a commutative function over the key-value pairs in the map -/ def foldl {δ : Type w} (f : δ → ∀ a, β a → δ) (H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁) (d : δ) (m : Finmap β) : δ := letI : RightCommutative fun d (s : Sigma β) ↦ f d s.1 s.2 := ⟨fun _ _ _ ↦ H _ _ _ _ _⟩ m.entries.foldl (fun d s => f d s.1 s.2) d /-- `any f s` returns `true` iff there exists a value `v` in `s` such that `f v = true`. -/ def any (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x || f y z) (fun _ _ _ _ => by simp_rw [Bool.or_assoc, Bool.or_comm, imp_true_iff]) false /-- `all f s` returns `true` iff `f v = true` for all values `v` in `s`. -/ def all (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x && f y z) (fun _ _ _ _ => by simp_rw [Bool.and_assoc, Bool.and_comm, imp_true_iff]) true /-! ### erase -/ section variable [DecidableEq α] /-- Erase a key from the map. If the key is not present it does nothing. -/ def erase (a : α) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.erase a t)) fun _ _ p => toFinmap_eq.2 <| perm_erase p @[simp] theorem erase_toFinmap (a : α) (s : AList β) : erase a ⟦s⟧ = AList.toFinmap (s.erase a) := by simp [erase] @[simp] theorem keys_erase_toFinset (a : α) (s : AList β) : keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a := by simp [Finset.erase, keys, AList.erase, keys_kerase] @[simp] theorem keys_erase (a : α) (s : Finmap β) : (erase a s).keys = s.keys.erase a := induction_on s fun s => by simp @[simp] theorem mem_erase {a a' : α} {s : Finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := induction_on s fun s => by simp theorem not_mem_erase_self {a : α} {s : Finmap β} : ¬a ∈ erase a s := by rw [mem_erase, not_and_or, not_not] left rfl @[simp] theorem lookup_erase (a) (s : Finmap β) : lookup a (erase a s) = none := induction_on s <| AList.lookup_erase a @[simp] theorem lookup_erase_ne {a a'} {s : Finmap β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := induction_on s fun _ => AList.lookup_erase_ne h theorem erase_erase {a a' : α} {s : Finmap β} : erase a (erase a' s) = erase a' (erase a s) := induction_on s fun s => ext (by simp only [AList.erase_erase, erase_toFinmap]) /-! ### sdiff -/ /-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or `s'` but not both. -/ def sdiff (s s' : Finmap β) : Finmap β := s'.foldl (fun s x _ => s.erase x) (fun _ _ _ _ _ => erase_erase) s instance : SDiff (Finmap β) := ⟨sdiff⟩ /-! ### insert -/ /-- Insert a key-value pair into a finite map, replacing any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.insert a b t)) fun _ _ p => toFinmap_eq.2 <| perm_insert p @[simp] theorem insert_toFinmap (a : α) (b : β a) (s : AList β) : insert a b (AList.toFinmap s) = AList.toFinmap (s.insert a b) := by simp [insert] theorem entries_insert_of_not_mem {a : α} {b : β a} {s : Finmap β} : a ∉ s → (insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries := induction_on s fun s h => by simp [AList.entries_insert_of_not_mem (mt mem_toFinmap.1 h), -entries_insert] @[deprecated (since := "2024-12-14")] alias insert_entries_of_neg := entries_insert_of_not_mem @[simp] theorem mem_insert {a a' : α} {b' : β a'} {s : Finmap β} : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := induction_on s AList.mem_insert @[simp] theorem lookup_insert {a} {b : β a} (s : Finmap β) : lookup a (insert a b s) = some b := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, AList.lookup_insert] @[simp] theorem lookup_insert_of_ne {a a'} {b : β a} (s : Finmap β) (h : a' ≠ a) : lookup a' (insert a b s) = lookup a' s := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, lookup_insert_ne h] @[simp] theorem insert_insert {a} {b b' : β a} (s : Finmap β) : (s.insert a b).insert a b' = s.insert a b' := induction_on s fun s => by simp only [insert_toFinmap, AList.insert_insert] theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : Finmap β) (h : a ≠ a') : (s.insert a b).insert a' b' = (s.insert a' b').insert a b := induction_on s fun s => by simp only [insert_toFinmap, AList.toFinmap_eq, AList.insert_insert_of_ne _ h] theorem toFinmap_cons (a : α) (b : β a) (xs : List (Sigma β)) : List.toFinmap (⟨a, b⟩ :: xs) = insert a b xs.toFinmap := rfl theorem mem_list_toFinmap (a : α) (xs : List (Sigma β)) : a ∈ xs.toFinmap ↔ ∃ b : β a, Sigma.mk a b ∈ xs := by induction' xs with x xs · simp only [toFinmap_nil, not_mem_empty, find?, not_mem_nil, exists_false] obtain ⟨fst_i, snd_i⟩ := x simp only [toFinmap_cons, *, exists_or, mem_cons, mem_insert, exists_and_left, Sigma.mk.inj_iff] refine (or_congr_left <| and_iff_left_of_imp ?_).symm rintro rfl simp only [exists_eq, heq_iff_eq] @[simp] theorem insert_singleton_eq {a : α} {b b' : β a} : insert a b (singleton a b') = singleton a b := by simp only [singleton, Finmap.insert_toFinmap, AList.insert_singleton_eq] /-! ### extract -/ /-- Erase a key from the map, and return the corresponding value, if found. -/ def extract (a : α) (s : Finmap β) : Option (β a) × Finmap β := (liftOn s fun t => Prod.map id AList.toFinmap (AList.extract a t)) fun s₁ s₂ p => by simp [perm_lookup p, toFinmap_eq, perm_erase p] @[simp] theorem extract_eq_lookup_erase (a : α) (s : Finmap β) : extract a s = (lookup a s, erase a s) := induction_on s fun s => by simp [extract] /-! ### union -/ /-- `s₁ ∪ s₂` is the key-based union of two finite maps. It is left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/ def union (s₁ s₂ : Finmap β) : Finmap β := (liftOn₂ s₁ s₂ fun s₁ s₂ => (AList.toFinmap (s₁ ∪ s₂))) fun _ _ _ _ p₁₃ p₂₄ => toFinmap_eq.mpr <| perm_union p₁₃ p₂₄ instance : Union (Finmap β) := ⟨union⟩ @[simp] theorem mem_union {a} {s₁ s₂ : Finmap β} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_union @[simp] theorem union_toFinmap (s₁ s₂ : AList β) : (toFinmap s₁) ∪ (toFinmap s₂) = toFinmap (s₁ ∪ s₂) := by simp [(· ∪ ·), union] theorem keys_union {s₁ s₂ : Finmap β} : (s₁ ∪ s₂).keys = s₁.keys ∪ s₂.keys := induction_on₂ s₁ s₂ fun s₁ s₂ => Finset.ext <| by simp [keys] @[simp] theorem lookup_union_left {a} {s₁ s₂ : Finmap β} : a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ := induction_on₂ s₁ s₂ fun _ _ => AList.lookup_union_left @[simp] theorem lookup_union_right {a} {s₁ s₂ : Finmap β} : a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.lookup_union_right theorem lookup_union_left_of_not_in {a} {s₁ s₂ : Finmap β} (h : a ∉ s₂) : lookup a (s₁ ∪ s₂) = lookup a s₁ := by by_cases h' : a ∈ s₁ · rw [lookup_union_left h'] · rw [lookup_union_right h', lookup_eq_none.mpr h, lookup_eq_none.mpr h'] /-- `simp`-normal form of `mem_lookup_union` -/ @[simp] theorem mem_lookup_union' {a} {b : β a} {s₁ s₂ : Finmap β} : lookup a (s₁ ∪ s₂) = some b ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_lookup_union theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : Finmap β} : b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_lookup_union theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : Finmap β} : b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) := induction_on₃ s₁ s₂ s₃ fun _ _ _ => AList.mem_lookup_union_middle theorem insert_union {a} {b : β a} {s₁ s₂ : Finmap β} : insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ := induction_on₂ s₁ s₂ fun a₁ a₂ => by simp [AList.insert_union]
theorem union_assoc {s₁ s₂ s₃ : Finmap β} : s₁ ∪ s₂ ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
Mathlib/Data/Finmap.lean
538
539
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import Mathlib.Data.PFunctor.Multivariate.Basic /-! # Multivariate quotients of polynomial functors. Basic definition of multivariate QPF. QPFs form a compositional framework for defining inductive and coinductive types, their quotients and nesting. The idea is based on building ever larger functors. For instance, we can define a list using a shape functor: ```lean inductive ListShape (a b : Type) | nil : ListShape | cons : a -> b -> ListShape ``` This shape can itself be decomposed as a sum of product which are themselves QPFs. It follows that the shape is a QPF and we can take its fixed point and create the list itself: ```lean def List (a : Type) := fix ListShape a -- not the actual notation ``` We can continue and define the quotient on permutation of lists and create the multiset type: ```lean def Multiset (a : Type) := QPF.quot List.perm List a -- not the actual notion ``` And `Multiset` is also a QPF. We can then create a novel data type (for Lean): ```lean inductive Tree (a : Type) | node : a -> Multiset Tree -> Tree ``` An unordered tree. This is currently not supported by Lean because it nests an inductive type inside of a quotient. We can go further and define unordered, possibly infinite trees: ```lean coinductive Tree' (a : Type) | node : a -> Multiset Tree' -> Tree' ``` by using the `cofix` construct. Those options can all be mixed and matched because they preserve the properties of QPF. The latter example, `Tree'`, combines fixed point, co-fixed point and quotients. ## Related modules * constructions * Fix * Cofix * Quot * Comp * Sigma / Pi * Prj * Const each proves that some operations on functors preserves the QPF structure -/ set_option linter.style.longLine false in /-! ## Reference [Jeremy Avigad, Mario M. Carneiro and Simon Hudon, *Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universe u open MvFunctor /-- Multivariate quotients of polynomial functors. -/ class MvQPF {n : ℕ} (F : TypeVec.{u} n → Type*) extends MvFunctor F where P : MvPFunctor.{u} n abs : ∀ {α}, P α → F α repr : ∀ {α}, F α → P α abs_repr : ∀ {α} (x : F α), abs (repr x) = x abs_map : ∀ {α β} (f : α ⟹ β) (p : P α), abs (f <$$> p) = f <$$> abs p namespace MvQPF variable {n : ℕ} {F : TypeVec.{u} n → Type*} [q : MvQPF F] open MvFunctor (LiftP LiftR) /-! ### Show that every MvQPF is a lawful MvFunctor. -/
protected theorem id_map {α : TypeVec n} (x : F α) : TypeVec.id <$$> x = x := by rw [← abs_repr x, ← abs_map] rfl
Mathlib/Data/QPF/Multivariate/Basic.lean
104
106
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison -/ import Mathlib.SetTheory.Game.State /-! # Domineering as a combinatorial game. We define the game of Domineering, played on a chessboard of arbitrary shape (possibly even disconnected). Left moves by placing a domino vertically, while Right moves by placing a domino horizontally. This is only a fragment of a full development; in order to successfully analyse positions we would need some more theorems. Most importantly, we need a general statement that allows us to discard irrelevant moves. Specifically to domineering, we need the fact that disjoint parts of the chessboard give sums of games. -/ namespace SetTheory namespace PGame namespace Domineering open Function /-- The equivalence `(x, y) ↦ (x, y+1)`. -/ @[simps!] def shiftUp : ℤ × ℤ ≃ ℤ × ℤ := (Equiv.refl ℤ).prodCongr (Equiv.addRight (1 : ℤ)) /-- The equivalence `(x, y) ↦ (x+1, y)`. -/ @[simps!] def shiftRight : ℤ × ℤ ≃ ℤ × ℤ := (Equiv.addRight (1 : ℤ)).prodCongr (Equiv.refl ℤ) /-- A Domineering board is an arbitrary finite subset of `ℤ × ℤ`. -/ -- Porting note: reducibility cannot be `local`. For now there are no dependents of this file so -- being globally reducible is fine. abbrev Board := Finset (ℤ × ℤ) /-- Left can play anywhere that a square and the square below it are open. -/ def left (b : Board) : Finset (ℤ × ℤ) := b ∩ b.map shiftUp /-- Right can play anywhere that a square and the square to the left are open. -/ def right (b : Board) : Finset (ℤ × ℤ) := b ∩ b.map shiftRight theorem mem_left {b : Board} (x : ℤ × ℤ) : x ∈ left b ↔ x ∈ b ∧ (x.1, x.2 - 1) ∈ b := Finset.mem_inter.trans (and_congr Iff.rfl Finset.mem_map_equiv) theorem mem_right {b : Board} (x : ℤ × ℤ) : x ∈ right b ↔ x ∈ b ∧ (x.1 - 1, x.2) ∈ b := Finset.mem_inter.trans (and_congr Iff.rfl Finset.mem_map_equiv) /-- After Left moves, two vertically adjacent squares are removed from the board. -/ def moveLeft (b : Board) (m : ℤ × ℤ) : Board := (b.erase m).erase (m.1, m.2 - 1) /-- After Left moves, two horizontally adjacent squares are removed from the board. -/ def moveRight (b : Board) (m : ℤ × ℤ) : Board := (b.erase m).erase (m.1 - 1, m.2) theorem fst_pred_mem_erase_of_mem_right {b : Board} {m : ℤ × ℤ} (h : m ∈ right b) : (m.1 - 1, m.2) ∈ b.erase m := by rw [mem_right] at h apply Finset.mem_erase_of_ne_of_mem _ h.2 exact ne_of_apply_ne Prod.fst (pred_ne_self m.1) theorem snd_pred_mem_erase_of_mem_left {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : (m.1, m.2 - 1) ∈ b.erase m := by rw [mem_left] at h apply Finset.mem_erase_of_ne_of_mem _ h.2 exact ne_of_apply_ne Prod.snd (pred_ne_self m.2) theorem card_of_mem_left {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : 2 ≤ Finset.card b := by have w₁ : m ∈ b := (Finset.mem_inter.1 h).1 have w₂ : (m.1, m.2 - 1) ∈ b.erase m := snd_pred_mem_erase_of_mem_left h have i₁ := Finset.card_erase_lt_of_mem w₁ have i₂ := Nat.lt_of_le_of_lt (Nat.zero_le _) (Finset.card_erase_lt_of_mem w₂) exact Nat.lt_of_le_of_lt i₂ i₁ theorem card_of_mem_right {b : Board} {m : ℤ × ℤ} (h : m ∈ right b) : 2 ≤ Finset.card b := by have w₁ : m ∈ b := (Finset.mem_inter.1 h).1 have w₂ := fst_pred_mem_erase_of_mem_right h have i₁ := Finset.card_erase_lt_of_mem w₁ have i₂ := Nat.lt_of_le_of_lt (Nat.zero_le _) (Finset.card_erase_lt_of_mem w₂) exact Nat.lt_of_le_of_lt i₂ i₁
theorem moveLeft_card {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : Finset.card (moveLeft b m) + 2 = Finset.card b := by dsimp only [moveLeft] rw [Finset.card_erase_of_mem (snd_pred_mem_erase_of_mem_left h)] rw [Finset.card_erase_of_mem (Finset.mem_of_mem_inter_left h)]
Mathlib/SetTheory/Game/Domineering.lean
93
98
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import Mathlib.Data.PFunctor.Multivariate.Basic /-! # The W construction as a multivariate polynomial functor. W types are well-founded tree-like structures. They are defined as the least fixpoint of a polynomial functor. ## Main definitions * `W_mk` - constructor * `W_dest - destructor * `W_rec` - recursor: basis for defining functions by structural recursion on `P.W α` * `W_rec_eq` - defining equation for `W_rec` * `W_ind` - induction principle for `P.W α` ## Implementation notes Three views of M-types: * `wp`: polynomial functor * `W`: data type inductively defined by a triple: shape of the root, data in the root and children of the root * `W`: least fixed point of a polynomial functor Specifically, we define the polynomial functor `wp` as: * A := a tree-like structure without information in the nodes * B := given the tree-like structure `t`, `B t` is a valid path (specified inductively by `W_path`) from the root of `t` to any given node. As a result `wp α` is made of a dataless tree and a function from its valid paths to values of `α` ## Reference * Jeremy Avigad, Mario M. Carneiro and Simon Hudon. [*Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universe u v namespace MvPFunctor open TypeVec open MvFunctor variable {n : ℕ} (P : MvPFunctor.{u} (n + 1)) /-- A path from the root of a tree to one of its node -/ inductive WPath : P.last.W → Fin2 n → Type u | root (a : P.A) (f : P.last.B a → P.last.W) (i : Fin2 n) (c : P.drop.B a i) : WPath ⟨a, f⟩ i | child (a : P.A) (f : P.last.B a → P.last.W) (i : Fin2 n) (j : P.last.B a) (c : WPath (f j) i) : WPath ⟨a, f⟩ i instance WPath.inhabited (x : P.last.W) {i} [I : Inhabited (P.drop.B x.head i)] : Inhabited (WPath P x i) := ⟨match x, I with | ⟨a, f⟩, I => WPath.root a f i (@default _ I)⟩ /-- Specialized destructor on `WPath` -/ def wPathCasesOn {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : P.WPath ⟨a, f⟩ ⟹ α := by intro i x match x with | WPath.root _ _ i c => exact g' i c | WPath.child _ _ i j c => exact g j i c /-- Specialized destructor on `WPath` -/ def wPathDestLeft {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.WPath ⟨a, f⟩ ⟹ α) : P.drop.B a ⟹ α := fun i c => h i (WPath.root a f i c) /-- Specialized destructor on `WPath` -/ def wPathDestRight {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.WPath ⟨a, f⟩ ⟹ α) : ∀ j : P.last.B a, P.WPath (f j) ⟹ α := fun j i c => h i (WPath.child a f i j c) theorem wPathDestLeft_wPathCasesOn {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : P.wPathDestLeft (P.wPathCasesOn g' g) = g' := rfl theorem wPathDestRight_wPathCasesOn {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : P.wPathDestRight (P.wPathCasesOn g' g) = g := rfl theorem wPathCasesOn_eta {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.WPath ⟨a, f⟩ ⟹ α) : P.wPathCasesOn (P.wPathDestLeft h) (P.wPathDestRight h) = h := by ext i x; cases x <;> rfl theorem comp_wPathCasesOn {α β : TypeVec n} (h : α ⟹ β) {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : h ⊚ P.wPathCasesOn g' g = P.wPathCasesOn (h ⊚ g') fun i => h ⊚ g i := by ext i x; cases x <;> rfl /-- Polynomial functor for the W-type of `P`. `A` is a data-less well-founded tree whereas, for a given `a : A`, `B a` is a valid path in tree `a` so that `Wp.obj α` is made of a tree and a function from its valid paths to the values it contains -/ def wp : MvPFunctor n where A := P.last.W B := P.WPath
/-- W-type of `P` -/ def W (α : TypeVec n) : Type _ :=
Mathlib/Data/PFunctor/Multivariate/W.lean
109
111
/- Copyright (c) 2018 Guy Leroy. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sangwoo Jo (aka Jason), Guy Leroy, Johannes Hölzl, Mario Carneiro -/ import Mathlib.Algebra.GroupWithZero.Semiconj import Mathlib.Algebra.Group.Commute.Units import Mathlib.Data.Nat.GCD.Basic import Mathlib.Data.Set.Operations import Mathlib.Order.Basic import Mathlib.Order.Bounds.Defs import Mathlib.Algebra.Group.Int.Defs import Mathlib.Data.Int.Basic /-! # Extended GCD and divisibility over ℤ ## Main definitions * Given `x y : ℕ`, `xgcd x y` computes the pair of integers `(a, b)` such that `gcd x y = x * a + y * b`. `gcdA x y` and `gcdB x y` are defined to be `a` and `b`, respectively. ## Main statements * `gcd_eq_gcd_ab`: Bézout's lemma, given `x y : ℕ`, `gcd x y = x * gcdA x y + y * gcdB x y`. ## Tags Bézout's lemma, Bezout's lemma -/ /-! ### Extended Euclidean algorithm -/ namespace Nat /-- Helper function for the extended GCD algorithm (`Nat.xgcd`). -/ def xgcdAux : ℕ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ | 0, _, _, r', s', t' => (r', s', t') | succ k, s, t, r', s', t' => let q := r' / succ k xgcdAux (r' % succ k) (s' - q * s) (t' - q * t) (succ k) s t termination_by k => k decreasing_by exact mod_lt _ <| (succ_pos _).gt @[simp] theorem xgcd_zero_left {s t r' s' t'} : xgcdAux 0 s t r' s' t' = (r', s', t') := by simp [xgcdAux] theorem xgcdAux_rec {r s t r' s' t'} (h : 0 < r) : xgcdAux r s t r' s' t' = xgcdAux (r' % r) (s' - r' / r * s) (t' - r' / r * t) r s t := by obtain ⟨r, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h.ne' simp [xgcdAux] /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : ℕ) : ℤ × ℤ := (xgcdAux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcdA (x y : ℕ) : ℤ := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcdB (x y : ℕ) : ℤ := (xgcd x y).2 @[simp] theorem gcdA_zero_left {s : ℕ} : gcdA 0 s = 0 := by unfold gcdA rw [xgcd, xgcd_zero_left] @[simp] theorem gcdB_zero_left {s : ℕ} : gcdB 0 s = 1 := by unfold gcdB rw [xgcd, xgcd_zero_left] @[simp] theorem gcdA_zero_right {s : ℕ} (h : s ≠ 0) : gcdA s 0 = 1 := by unfold gcdA xgcd obtain ⟨s, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h rw [xgcdAux] simp @[simp] theorem gcdB_zero_right {s : ℕ} (h : s ≠ 0) : gcdB s 0 = 0 := by unfold gcdB xgcd obtain ⟨s, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h rw [xgcdAux] simp @[simp] theorem xgcdAux_fst (x y) : ∀ s t s' t', (xgcdAux x s t y s' t').1 = gcd x y := gcd.induction x y (by simp) fun x y h IH s t s' t' => by simp only [h, xgcdAux_rec, IH] rw [← gcd_rec] theorem xgcdAux_val (x y) : xgcdAux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcdAux_fst x y 1 0 0 1] theorem xgcd_val (x y) : xgcd x y = (gcdA x y, gcdB x y) := by unfold gcdA gcdB; cases xgcd x y; rfl section variable (x y : ℕ) private def P : ℕ × ℤ × ℤ → Prop | (r, s, t) => (r : ℤ) = x * s + y * t theorem xgcdAux_P {r r'} : ∀ {s t s' t'}, P x y (r, s, t) → P x y (r', s', t') → P x y (xgcdAux r s t r' s' t') := by induction r, r' using gcd.induction with | H0 => simp | H1 a b h IH => intro s t s' t' p p' rw [xgcdAux_rec h]; refine IH ?_ p; dsimp [P] at * rw [Int.emod_def]; generalize (b / a : ℤ) = k rw [p, p', Int.mul_sub, sub_add_eq_add_sub, Int.mul_sub, Int.add_mul, mul_comm k t, mul_comm k s, ← mul_assoc, ← mul_assoc, add_comm (x * s * k), ← add_sub_assoc, sub_sub] /-- **Bézout's lemma**: given `x y : ℕ`, `gcd x y = x * a + y * b`, where `a = gcd_a x y` and `b = gcd_b x y` are computed by the extended Euclidean algorithm. -/ theorem gcd_eq_gcd_ab : (gcd x y : ℤ) = x * gcdA x y + y * gcdB x y := by have := @xgcdAux_P x y x y 1 0 0 1 (by simp [P]) (by simp [P]) rwa [xgcdAux_val, xgcd_val] at this end theorem exists_mul_emod_eq_gcd {k n : ℕ} (hk : gcd n k < k) : ∃ m, n * m % k = gcd n k := by have hk' := Int.ofNat_ne_zero.2 (ne_of_gt (lt_of_le_of_lt (zero_le (gcd n k)) hk)) have key := congr_arg (fun (m : ℤ) => (m % k).toNat) (gcd_eq_gcd_ab n k) simp only at key rw [Int.add_mul_emod_self_left, ← Int.natCast_mod, Int.toNat_natCast, mod_eq_of_lt hk] at key refine ⟨(n.gcdA k % k).toNat, Eq.trans (Int.ofNat.inj ?_) key.symm⟩ rw [Int.ofNat_eq_coe, Int.natCast_mod, Int.natCast_mul, Int.toNat_of_nonneg (Int.emod_nonneg _ hk'), Int.ofNat_eq_coe, Int.toNat_of_nonneg (Int.emod_nonneg _ hk'), Int.mul_emod, Int.emod_emod, ← Int.mul_emod] theorem exists_mul_emod_eq_one_of_coprime {k n : ℕ} (hkn : Coprime n k) (hk : 1 < k) : ∃ m, n * m % k = 1 := Exists.recOn (exists_mul_emod_eq_gcd (lt_of_le_of_lt (le_of_eq hkn) hk)) fun m hm ↦ ⟨m, hm.trans hkn⟩ end Nat /-! ### Divisibility over ℤ -/ namespace Int theorem gcd_def (i j : ℤ) : gcd i j = Nat.gcd i.natAbs j.natAbs := rfl @[simp, norm_cast] protected lemma gcd_natCast_natCast (m n : ℕ) : gcd ↑m ↑n = m.gcd n := rfl /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcdA : ℤ → ℤ → ℤ | ofNat m, n => m.gcdA n.natAbs | -[m+1], n => -m.succ.gcdA n.natAbs /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcdB : ℤ → ℤ → ℤ | m, ofNat n => m.natAbs.gcdB n | m, -[n+1] => -m.natAbs.gcdB n.succ /-- **Bézout's lemma** -/ theorem gcd_eq_gcd_ab : ∀ x y : ℤ, (gcd x y : ℤ) = x * gcdA x y + y * gcdB x y | (m : ℕ), (n : ℕ) => Nat.gcd_eq_gcd_ab _ _ | (m : ℕ), -[n+1] => show (_ : ℤ) = _ + -(n + 1) * -_ by rw [Int.neg_mul_neg]; apply Nat.gcd_eq_gcd_ab | -[m+1], (n : ℕ) => show (_ : ℤ) = -(m + 1) * -_ + _ by rw [Int.neg_mul_neg]; apply Nat.gcd_eq_gcd_ab | -[m+1], -[n+1] => show (_ : ℤ) = -(m + 1) * -_ + -(n + 1) * -_ by rw [Int.neg_mul_neg, Int.neg_mul_neg] apply Nat.gcd_eq_gcd_ab theorem lcm_def (i j : ℤ) : lcm i j = Nat.lcm (natAbs i) (natAbs j) := rfl protected theorem coe_nat_lcm (m n : ℕ) : Int.lcm ↑m ↑n = Nat.lcm m n := rfl theorem dvd_coe_gcd {i j k : ℤ} (h1 : k ∣ i) (h2 : k ∣ j) : k ∣ gcd i j := natAbs_dvd.1 <| natCast_dvd_natCast.2 <| Nat.dvd_gcd (natAbs_dvd_natAbs.2 h1) (natAbs_dvd_natAbs.2 h2) @[deprecated (since := "2025-04-27")] alias dvd_gcd := dvd_coe_gcd theorem gcd_mul_lcm (i j : ℤ) : gcd i j * lcm i j = natAbs (i * j) := by rw [Int.gcd, Int.lcm, Nat.gcd_mul_lcm, natAbs_mul] theorem gcd_comm (i j : ℤ) : gcd i j = gcd j i := Nat.gcd_comm _ _ theorem gcd_assoc (i j k : ℤ) : gcd (gcd i j) k = gcd i (gcd j k) := Nat.gcd_assoc _ _ _ @[simp] theorem gcd_self (i : ℤ) : gcd i i = natAbs i := by simp [gcd] @[simp] theorem gcd_zero_left (i : ℤ) : gcd 0 i = natAbs i := by simp [gcd] @[simp] theorem gcd_zero_right (i : ℤ) : gcd i 0 = natAbs i := by simp [gcd] theorem gcd_mul_left (i j k : ℤ) : gcd (i * j) (i * k) = natAbs i * gcd j k := by rw [Int.gcd, Int.gcd, natAbs_mul, natAbs_mul] apply Nat.gcd_mul_left theorem gcd_mul_right (i j k : ℤ) : gcd (i * j) (k * j) = gcd i k * natAbs j := by rw [Int.gcd, Int.gcd, natAbs_mul, natAbs_mul] apply Nat.gcd_mul_right theorem gcd_pos_of_ne_zero_left {i : ℤ} (j : ℤ) (hi : i ≠ 0) : 0 < gcd i j := Nat.gcd_pos_of_pos_left _ <| natAbs_pos.2 hi theorem gcd_pos_of_ne_zero_right (i : ℤ) {j : ℤ} (hj : j ≠ 0) : 0 < gcd i j := Nat.gcd_pos_of_pos_right _ <| natAbs_pos.2 hj theorem gcd_eq_zero_iff {i j : ℤ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := by rw [gcd, Nat.gcd_eq_zero_iff, natAbs_eq_zero, natAbs_eq_zero] theorem gcd_pos_iff {i j : ℤ} : 0 < gcd i j ↔ i ≠ 0 ∨ j ≠ 0 := Nat.pos_iff_ne_zero.trans <| gcd_eq_zero_iff.not.trans not_and_or theorem gcd_div {i j k : ℤ} (H1 : k ∣ i) (H2 : k ∣ j) : gcd (i / k) (j / k) = gcd i j / natAbs k := by rw [gcd, natAbs_ediv_of_dvd i k H1, natAbs_ediv_of_dvd j k H2] exact Nat.gcd_div (natAbs_dvd_natAbs.mpr H1) (natAbs_dvd_natAbs.mpr H2) theorem gcd_div_gcd_div_gcd {i j : ℤ} (H : 0 < gcd i j) : gcd (i / gcd i j) (j / gcd i j) = 1 := by rw [gcd_div gcd_dvd_left gcd_dvd_right, natAbs_ofNat, Nat.div_self H] theorem gcd_dvd_gcd_of_dvd_left {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd i j ∣ gcd k j := Int.natCast_dvd_natCast.1 <| dvd_coe_gcd (gcd_dvd_left.trans H) gcd_dvd_right theorem gcd_dvd_gcd_of_dvd_right {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd j i ∣ gcd j k := Int.natCast_dvd_natCast.1 <| dvd_coe_gcd gcd_dvd_left (gcd_dvd_right.trans H) theorem gcd_dvd_gcd_mul_left (i j k : ℤ) : gcd i j ∣ gcd (k * i) j := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (i j k : ℤ) : gcd i j ∣ gcd (i * k) j := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (i j k : ℤ) : gcd i j ∣ gcd i (k * j) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (i j k : ℤ) : gcd i j ∣ gcd i (j * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) /-- If `gcd a (m * n) = 1`, then `gcd a m = 1`. -/ theorem gcd_eq_one_of_gcd_mul_right_eq_one_left {a : ℤ} {m n : ℕ} (h : a.gcd (m * n) = 1) : a.gcd m = 1 := Nat.dvd_one.mp <| h ▸ gcd_dvd_gcd_mul_right_right a m n /-- If `gcd a (m * n) = 1`, then `gcd a n = 1`. -/ theorem gcd_eq_one_of_gcd_mul_right_eq_one_right {a : ℤ} {m n : ℕ} (h : a.gcd (m * n) = 1) : a.gcd n = 1 := Nat.dvd_one.mp <| h ▸ gcd_dvd_gcd_mul_left_right a n m theorem gcd_eq_left {i j : ℤ} (H : i ∣ j) : gcd i j = natAbs i := Nat.dvd_antisymm (Nat.gcd_dvd_left _ _) (Nat.dvd_gcd dvd_rfl (natAbs_dvd_natAbs.mpr H)) theorem gcd_eq_right {i j : ℤ} (H : j ∣ i) : gcd i j = natAbs j := by rw [gcd_comm, gcd_eq_left H] theorem ne_zero_of_gcd {x y : ℤ} (hc : gcd x y ≠ 0) : x ≠ 0 ∨ y ≠ 0 := by contrapose! hc rw [hc.left, hc.right, gcd_zero_right, natAbs_zero] theorem exists_gcd_one {m n : ℤ} (H : 0 < gcd m n) : ∃ m' n' : ℤ, gcd m' n' = 1 ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, gcd_div_gcd_div_gcd H, (Int.ediv_mul_cancel gcd_dvd_left).symm, (Int.ediv_mul_cancel gcd_dvd_right).symm⟩ theorem exists_gcd_one' {m n : ℤ} (H : 0 < gcd m n) : ∃ (g : ℕ) (m' n' : ℤ), 0 < g ∧ gcd m' n' = 1 ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_gcd_one H ⟨_, m', n', H, h⟩ theorem pow_dvd_pow_iff {m n : ℤ} {k : ℕ} (k0 : k ≠ 0) : m ^ k ∣ n ^ k ↔ m ∣ n := by refine ⟨fun h => ?_, fun h => pow_dvd_pow_of_dvd h _⟩ rwa [← natAbs_dvd_natAbs, ← Nat.pow_dvd_pow_iff k0, ← Int.natAbs_pow, ← Int.natAbs_pow, natAbs_dvd_natAbs] theorem gcd_dvd_iff {a b : ℤ} {n : ℕ} : gcd a b ∣ n ↔ ∃ x y : ℤ, ↑n = a * x + b * y := by constructor · intro h rw [← Nat.mul_div_cancel' h, Int.ofNat_mul, gcd_eq_gcd_ab, Int.add_mul, mul_assoc, mul_assoc] exact ⟨_, _, rfl⟩ · rintro ⟨x, y, h⟩ rw [← Int.natCast_dvd_natCast, h] exact Int.dvd_add (dvd_mul_of_dvd_left gcd_dvd_left _) (dvd_mul_of_dvd_left gcd_dvd_right y) theorem gcd_greatest {a b d : ℤ} (hd_pos : 0 ≤ d) (hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : ℤ, e ∣ a → e ∣ b → e ∣ d) : d = gcd a b := dvd_antisymm hd_pos (ofNat_zero_le (gcd a b)) (dvd_coe_gcd hda hdb) (hd _ gcd_dvd_left gcd_dvd_right) /-- Euclid's lemma: if `a ∣ b * c` and `gcd a c = 1` then `a ∣ b`. Compare with `IsCoprime.dvd_of_dvd_mul_left` and `UniqueFactorizationMonoid.dvd_of_dvd_mul_left_of_no_prime_factors` -/ theorem dvd_of_dvd_mul_left_of_gcd_one {a b c : ℤ} (habc : a ∣ b * c) (hab : gcd a c = 1) : a ∣ b := by have := gcd_eq_gcd_ab a c simp only [hab, Int.ofNat_zero, Int.ofNat_succ, zero_add] at this have : b * a * gcdA a c + b * c * gcdB a c = b := by simp [mul_assoc, ← Int.mul_add, ← this] rw [← this] exact Int.dvd_add (dvd_mul_of_dvd_left (dvd_mul_left a b) _) (dvd_mul_of_dvd_left habc _) /-- Euclid's lemma: if `a ∣ b * c` and `gcd a b = 1` then `a ∣ c`. Compare with `IsCoprime.dvd_of_dvd_mul_right` and `UniqueFactorizationMonoid.dvd_of_dvd_mul_right_of_no_prime_factors` -/ theorem dvd_of_dvd_mul_right_of_gcd_one {a b c : ℤ} (habc : a ∣ b * c) (hab : gcd a b = 1) : a ∣ c := by rw [mul_comm] at habc exact dvd_of_dvd_mul_left_of_gcd_one habc hab /-- For nonzero integers `a` and `b`, `gcd a b` is the smallest positive natural number that can be written in the form `a * x + b * y` for some pair of integers `x` and `y` -/ theorem gcd_least_linear {a b : ℤ} (ha : a ≠ 0) : IsLeast { n : ℕ | 0 < n ∧ ∃ x y : ℤ, ↑n = a * x + b * y } (a.gcd b) := by simp_rw [← gcd_dvd_iff] constructor · simpa [and_true, dvd_refl, Set.mem_setOf_eq] using gcd_pos_of_ne_zero_left b ha · simp only [lowerBounds, and_imp, Set.mem_setOf_eq] exact fun n hn_pos hn => Nat.le_of_dvd hn_pos hn /-! ### lcm -/ theorem lcm_comm (i j : ℤ) : lcm i j = lcm j i := by rw [Int.lcm, Int.lcm] exact Nat.lcm_comm _ _ theorem lcm_assoc (i j k : ℤ) : lcm (lcm i j) k = lcm i (lcm j k) := by rw [Int.lcm, Int.lcm, Int.lcm, Int.lcm, natAbs_ofNat, natAbs_ofNat] apply Nat.lcm_assoc @[simp] theorem lcm_zero_left (i : ℤ) : lcm 0 i = 0 := by rw [Int.lcm] apply Nat.lcm_zero_left @[simp] theorem lcm_zero_right (i : ℤ) : lcm i 0 = 0 := by rw [Int.lcm] apply Nat.lcm_zero_right @[simp] theorem lcm_one_left (i : ℤ) : lcm 1 i = natAbs i := by rw [Int.lcm] apply Nat.lcm_one_left @[simp] theorem lcm_one_right (i : ℤ) : lcm i 1 = natAbs i := by rw [Int.lcm] apply Nat.lcm_one_right theorem coe_lcm_dvd {i j k : ℤ} : i ∣ k → j ∣ k → (lcm i j : ℤ) ∣ k := by rw [Int.lcm] intro hi hj exact natCast_dvd.mpr (Nat.lcm_dvd (natAbs_dvd_natAbs.mpr hi) (natAbs_dvd_natAbs.mpr hj)) @[deprecated (since := "2025-04-27")] alias lcm_dvd := coe_lcm_dvd theorem lcm_mul_left {m n k : ℤ} : (m * n).lcm (m * k) = natAbs m * n.lcm k := by simp_rw [Int.lcm, natAbs_mul, Nat.lcm_mul_left] theorem lcm_mul_right {m n k : ℤ} : (m * n).lcm (k * n) = m.lcm k * natAbs n := by simp_rw [Int.lcm, natAbs_mul, Nat.lcm_mul_right] end Int @[to_additive gcd_nsmul_eq_zero] theorem pow_gcd_eq_one {M : Type*} [Monoid M] (x : M) {m n : ℕ} (hm : x ^ m = 1) (hn : x ^ n = 1) : x ^ m.gcd n = 1 := by rcases m with (rfl | m); · simp [hn] obtain ⟨y, rfl⟩ := IsUnit.of_pow_eq_one hm m.succ_ne_zero rw [← Units.val_pow_eq_pow_val, ← Units.val_one (α := M), ← zpow_natCast, ← Units.ext_iff] at * rw [Nat.gcd_eq_gcd_ab, zpow_add, zpow_mul, zpow_mul, hn, hm, one_zpow, one_zpow, one_mul] variable {α : Type*} section GroupWithZero variable [GroupWithZero α] {a b : α} {m n : ℕ} protected lemma Commute.pow_eq_pow_iff_of_coprime (hab : Commute a b) (hmn : m.Coprime n) : a ^ m = b ^ n ↔ ∃ c, a = c ^ n ∧ b = c ^ m := by refine ⟨fun h ↦ ?_, by rintro ⟨c, rfl, rfl⟩; rw [← pow_mul, ← pow_mul']⟩ by_cases m = 0; · aesop by_cases n = 0; · aesop by_cases hb : b = 0; · exact ⟨0, by aesop⟩ by_cases ha : a = 0; · exact ⟨0, by have := h.symm; aesop⟩ refine ⟨a ^ Nat.gcdB m n * b ^ Nat.gcdA m n, ?_, ?_⟩ <;> · refine (pow_one _).symm.trans ?_ conv_lhs => rw [← zpow_natCast, ← hmn, Nat.gcd_eq_gcd_ab] simp only [zpow_add₀ ha, zpow_add₀ hb, ← zpow_natCast, (hab.zpow_zpow₀ _ _).mul_zpow, ← zpow_mul, mul_comm (Nat.gcdB m n), mul_comm (Nat.gcdA m n)] simp only [zpow_mul, zpow_natCast, h] exact ((Commute.pow_pow (by aesop) _ _).zpow_zpow₀ _ _).symm end GroupWithZero section CommGroupWithZero variable [CommGroupWithZero α] {a b : α} {m n : ℕ} lemma pow_eq_pow_iff_of_coprime (hmn : m.Coprime n) : a ^ m = b ^ n ↔ ∃ c, a = c ^ n ∧ b = c ^ m := (Commute.all _ _).pow_eq_pow_iff_of_coprime hmn lemma pow_mem_range_pow_of_coprime (hmn : m.Coprime n) (a : α) : a ^ m ∈ Set.range (· ^ n : α → α) ↔ a ∈ Set.range (· ^ n : α → α) := by simp [pow_eq_pow_iff_of_coprime hmn.symm]; aesop end CommGroupWithZero
Mathlib/Data/Int/GCD.lean
468
481
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.Polynomial.GroupRingAction import Mathlib.Algebra.Ring.Action.Field import Mathlib.Algebra.Ring.Action.Invariant import Mathlib.FieldTheory.Finiteness import Mathlib.FieldTheory.Normal.Defs import Mathlib.FieldTheory.Separable import Mathlib.LinearAlgebra.Dual.Lemmas import Mathlib.LinearAlgebra.FreeModule.Finite.Matrix /-! # Fixed field under a group action. This is the basis of the Fundamental Theorem of Galois Theory. Given a (finite) group `G` that acts on a field `F`, we define `FixedPoints.subfield G F`, the subfield consisting of elements of `F` fixed_points by every element of `G`. This subfield is then normal and separable, and in addition if `G` acts faithfully on `F` then `finrank (FixedPoints.subfield G F) F = Fintype.card G`. ## Main Definitions - `FixedPoints.subfield G F`, the subfield consisting of elements of `F` fixed_points by every element of `G`, where `G` is a group that acts on `F`. -/ noncomputable section open MulAction Finset Module universe u v w variable {M : Type u} [Monoid M] variable (G : Type u) [Group G] variable (F : Type v) [Field F] [MulSemiringAction M F] [MulSemiringAction G F] (m : M) /-- The subfield of F fixed by the field endomorphism `m`. -/ def FixedBy.subfield : Subfield F where carrier := fixedBy F m zero_mem' := smul_zero m add_mem' hx hy := (smul_add m _ _).trans <| congr_arg₂ _ hx hy neg_mem' hx := (smul_neg m _).trans <| congr_arg _ hx one_mem' := smul_one m mul_mem' hx hy := (smul_mul' m _ _).trans <| congr_arg₂ _ hx hy inv_mem' x hx := (smul_inv'' m x).trans <| congr_arg _ hx section InvariantSubfields variable (M) {F} /-- A typeclass for subrings invariant under a `MulSemiringAction`. -/ class IsInvariantSubfield (S : Subfield F) : Prop where smul_mem : ∀ (m : M) {x : F}, x ∈ S → m • x ∈ S variable (S : Subfield F) instance IsInvariantSubfield.toMulSemiringAction [IsInvariantSubfield M S] : MulSemiringAction M S where smul m x := ⟨m • x.1, IsInvariantSubfield.smul_mem m x.2⟩ one_smul s := Subtype.eq <| one_smul M s.1 mul_smul m₁ m₂ s := Subtype.eq <| mul_smul m₁ m₂ s.1 smul_add m s₁ s₂ := Subtype.eq <| smul_add m s₁.1 s₂.1 smul_zero m := Subtype.eq <| smul_zero m smul_one m := Subtype.eq <| smul_one m smul_mul m s₁ s₂ := Subtype.eq <| smul_mul' m s₁.1 s₂.1 instance [IsInvariantSubfield M S] : IsInvariantSubring M S.toSubring where smul_mem := IsInvariantSubfield.smul_mem end InvariantSubfields namespace FixedPoints variable (M) -- we use `Subfield.copy` so that the underlying set is `fixedPoints M F` /-- The subfield of fixed points by a monoid action. -/ def subfield : Subfield F := Subfield.copy (⨅ m : M, FixedBy.subfield F m) (fixedPoints M F) (by ext z; simp [fixedPoints, FixedBy.subfield, iInf, Subfield.mem_sInf]; rfl) instance : IsInvariantSubfield M (FixedPoints.subfield M F) where smul_mem g x hx g' := by rw [hx, hx] instance : SMulCommClass M (FixedPoints.subfield M F) F where smul_comm m f f' := show m • (↑f * f') = f * m • f' by rw [smul_mul', f.prop m] instance smulCommClass' : SMulCommClass (FixedPoints.subfield M F) M F := SMulCommClass.symm _ _ _ @[simp] theorem smul (m : M) (x : FixedPoints.subfield M F) : m • x = x := Subtype.eq <| x.2 m -- Why is this so slow? @[simp] theorem smul_polynomial (m : M) (p : Polynomial (FixedPoints.subfield M F)) : m • p = p := Polynomial.induction_on p (fun x => by rw [Polynomial.smul_C, smul]) (fun p q ihp ihq => by rw [smul_add, ihp, ihq]) fun n x _ => by rw [smul_mul', Polynomial.smul_C, smul, smul_pow', Polynomial.smul_X] instance : Algebra (FixedPoints.subfield M F) F := by infer_instance theorem coe_algebraMap : algebraMap (FixedPoints.subfield M F) F = Subfield.subtype (FixedPoints.subfield M F) := rfl theorem linearIndependent_smul_of_linearIndependent {s : Finset F} : (LinearIndepOn (FixedPoints.subfield G F) id (s : Set F)) → LinearIndepOn F (MulAction.toFun G F) s := by classical have : IsEmpty ((∅ : Finset F) : Set F) := by simp refine Finset.induction_on s (fun _ => linearIndependent_empty_type) fun a s has ih hs => ?_ rw [coe_insert] at hs ⊢ rw [linearIndepOn_insert (mt mem_coe.1 has)] at hs rw [linearIndepOn_insert (mt mem_coe.1 has)]; refine ⟨ih hs.1, fun ha => ?_⟩ rw [Finsupp.mem_span_image_iff_linearCombination] at ha; rcases ha with ⟨l, hl, hla⟩ rw [Finsupp.linearCombination_apply_of_mem_supported F hl] at hla suffices ∀ i ∈ s, l i ∈ FixedPoints.subfield G F by replace hla := (sum_apply _ _ fun i => l i • toFun G F i).symm.trans (congr_fun hla 1) simp_rw [Pi.smul_apply, toFun_apply, one_smul] at hla refine hs.2 (hla ▸ Submodule.sum_mem _ fun c hcs => ?_) change (⟨l c, this c hcs⟩ : FixedPoints.subfield G F) • c ∈ _ exact Submodule.smul_mem _ _ <| Submodule.subset_span <| by simpa intro i his g refine eq_of_sub_eq_zero (linearIndependent_iff'.1 (ih hs.1) s.attach (fun i => g • l i - l i) ?_ ⟨i, his⟩ (mem_attach _ _) : _) refine (sum_attach s fun i ↦ (g • l i - l i) • MulAction.toFun G F i).trans ?_ ext g'; dsimp only conv_lhs => rw [sum_apply] congr · skip · ext rw [Pi.smul_apply, sub_smul, smul_eq_mul] rw [sum_sub_distrib, Pi.zero_apply, sub_eq_zero] conv_lhs => congr · skip · ext x rw [toFun_apply, ← mul_inv_cancel_left g g', mul_smul, ← smul_mul', ← toFun_apply _ x] show (∑ x ∈ s, g • (fun y => l y • MulAction.toFun G F y) x (g⁻¹ * g')) = ∑ x ∈ s, (fun y => l y • MulAction.toFun G F y) x g' rw [← smul_sum, ← sum_apply _ _ fun y => l y • toFun G F y, ← sum_apply _ _ fun y => l y • toFun G F y] rw [hla, toFun_apply, toFun_apply, smul_smul, mul_inv_cancel_left] section Fintype variable [Fintype G] (x : F) /-- `minpoly G F x` is the minimal polynomial of `(x : F)` over `FixedPoints.subfield G F`. -/ def minpoly : Polynomial (FixedPoints.subfield G F) := (prodXSubSMul G F x).toSubring (FixedPoints.subfield G F).toSubring fun _ hc g => let ⟨n, _, hn⟩ := Polynomial.mem_coeffs_iff.1 hc hn.symm ▸ prodXSubSMul.coeff G F x g n namespace minpoly theorem monic : (minpoly G F x).Monic := by simp only [minpoly] rw [Polynomial.monic_toSubring] exact prodXSubSMul.monic G F x theorem eval₂ : Polynomial.eval₂ (Subring.subtype <| (FixedPoints.subfield G F).toSubring) x (minpoly G F x) = 0 := by rw [← prodXSubSMul.eval G F x, Polynomial.eval₂_eq_eval_map]
simp only [minpoly, Polynomial.map_toSubring] theorem eval₂' :
Mathlib/FieldTheory/Fixed.lean
180
182
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro -/ import Mathlib.Data.Subtype import Mathlib.Order.Defs.LinearOrder import Mathlib.Order.Notation import Mathlib.Tactic.GCongr.Core import Mathlib.Tactic.Spread import Mathlib.Tactic.Convert import Mathlib.Tactic.Inhabit import Mathlib.Tactic.SimpRw /-! # Basic definitions about `≤` and `<` This file proves basic results about orders, provides extensive dot notation, defines useful order classes and allows to transfer order instances. ## Type synonyms * `OrderDual α` : A type synonym reversing the meaning of all inequalities, with notation `αᵒᵈ`. * `AsLinearOrder α`: A type synonym to promote `PartialOrder α` to `LinearOrder α` using `IsTotal α (≤)`. ### Transferring orders - `Order.Preimage`, `Preorder.lift`: Transfers a (pre)order on `β` to an order on `α` using a function `f : α → β`. - `PartialOrder.lift`, `LinearOrder.lift`: Transfers a partial (resp., linear) order on `β` to a partial (resp., linear) order on `α` using an injective function `f`. ### Extra class * `DenselyOrdered`: An order with no gap, i.e. for any two elements `a < b` there exists `c` such that `a < c < b`. ## Notes `≤` and `<` are highly favored over `≥` and `>` in mathlib. The reason is that we can formulate all lemmas using `≤`/`<`, and `rw` has trouble unifying `≤` and `≥`. Hence choosing one direction spares us useless duplication. This is enforced by a linter. See Note [nolint_ge] for more infos. Dot notation is particularly useful on `≤` (`LE.le`) and `<` (`LT.lt`). To that end, we provide many aliases to dot notation-less lemmas. For example, `le_trans` is aliased with `LE.le.trans` and can be used to construct `hab.trans hbc : a ≤ c` when `hab : a ≤ b`, `hbc : b ≤ c`, `lt_of_le_of_lt` is aliased as `LE.le.trans_lt` and can be used to construct `hab.trans hbc : a < c` when `hab : a ≤ b`, `hbc : b < c`. ## TODO - expand module docs - automatic construction of dual definitions / theorems ## Tags preorder, order, partial order, poset, linear order, chain -/ open Function variable {ι α β : Type*} {π : ι → Type*} /-! ### Bare relations -/ attribute [ext] LE protected lemma LE.le.ge [LE α] {x y : α} (h : x ≤ y) : y ≥ x := h protected lemma GE.ge.le [LE α] {x y : α} (h : x ≥ y) : y ≤ x := h protected lemma LT.lt.gt [LT α] {x y : α} (h : x < y) : y > x := h protected lemma GT.gt.lt [LT α] {x y : α} (h : x > y) : y < x := h /-- Given a relation `R` on `β` and a function `f : α → β`, the preimage relation on `α` is defined by `x ≤ y ↔ f x ≤ f y`. It is the unique relation on `α` making `f` a `RelEmbedding` (assuming `f` is injective). -/ @[simp] def Order.Preimage (f : α → β) (s : β → β → Prop) (x y : α) : Prop := s (f x) (f y) @[inherit_doc] infixl:80 " ⁻¹'o " => Order.Preimage /-- The preimage of a decidable order is decidable. -/ instance Order.Preimage.decidable (f : α → β) (s : β → β → Prop) [H : DecidableRel s] : DecidableRel (f ⁻¹'o s) := fun _ _ ↦ H _ _ /-! ### Preorders -/ section Preorder variable [Preorder α] {a b c d : α} theorem le_trans' : b ≤ c → a ≤ b → a ≤ c := flip le_trans theorem lt_trans' : b < c → a < b → a < c := flip lt_trans theorem lt_of_le_of_lt' : b ≤ c → a < b → a < c := flip lt_of_lt_of_le theorem lt_of_lt_of_le' : b < c → a ≤ b → a < c := flip lt_of_le_of_lt theorem le_of_le_of_eq' : b ≤ c → a = b → a ≤ c := flip le_of_eq_of_le theorem le_of_eq_of_le' : b = c → a ≤ b → a ≤ c := flip le_of_le_of_eq theorem lt_of_lt_of_eq' : b < c → a = b → a < c := flip lt_of_eq_of_lt theorem lt_of_eq_of_lt' : b = c → a < b → a < c := flip lt_of_lt_of_eq theorem not_lt_iff_not_le_or_ge : ¬a < b ↔ ¬a ≤ b ∨ b ≤ a := by rw [lt_iff_le_not_le, Classical.not_and_iff_not_or_not, Classical.not_not] -- Unnecessary brackets are here for readability lemma not_lt_iff_le_imp_le : ¬ a < b ↔ (a ≤ b → b ≤ a) := by simp [not_lt_iff_not_le_or_ge, or_iff_not_imp_left] /-- If `x = y` then `y ≤ x`. Note: this lemma uses `y ≤ x` instead of `x ≥ y`, because `le` is used almost exclusively in mathlib. -/ lemma ge_of_eq (h : a = b) : b ≤ a := le_of_eq h.symm @[simp] lemma lt_self_iff_false (x : α) : x < x ↔ False := ⟨lt_irrefl x, False.elim⟩ alias LE.le.trans := le_trans alias LE.le.trans' := le_trans' alias LT.lt.trans := lt_trans alias LT.lt.trans' := lt_trans' alias LE.le.trans_lt := lt_of_le_of_lt alias LE.le.trans_lt' := lt_of_le_of_lt' alias LT.lt.trans_le := lt_of_lt_of_le alias LT.lt.trans_le' := lt_of_lt_of_le' alias LE.le.trans_eq := le_of_le_of_eq alias LE.le.trans_eq' := le_of_le_of_eq' alias LT.lt.trans_eq := lt_of_lt_of_eq alias LT.lt.trans_eq' := lt_of_lt_of_eq' alias Eq.trans_le := le_of_eq_of_le alias Eq.trans_ge := le_of_eq_of_le' alias Eq.trans_lt := lt_of_eq_of_lt alias Eq.trans_gt := lt_of_eq_of_lt' alias LE.le.lt_of_not_le := lt_of_le_not_le alias LE.le.lt_or_eq_dec := Decidable.lt_or_eq_of_le alias LT.lt.le := le_of_lt alias LT.lt.ne := ne_of_lt alias Eq.le := le_of_eq @[inherit_doc ge_of_eq] alias Eq.ge := ge_of_eq alias LT.lt.asymm := lt_asymm alias LT.lt.not_lt := lt_asymm theorem ne_of_not_le (h : ¬a ≤ b) : a ≠ b := fun hab ↦ h (le_of_eq hab) protected lemma Eq.not_lt (hab : a = b) : ¬a < b := fun h' ↦ h'.ne hab protected lemma Eq.not_gt (hab : a = b) : ¬b < a := hab.symm.not_lt @[simp] lemma le_of_subsingleton [Subsingleton α] : a ≤ b := (Subsingleton.elim a b).le -- Making this a @[simp] lemma causes confluence problems downstream. lemma not_lt_of_subsingleton [Subsingleton α] : ¬a < b := (Subsingleton.elim a b).not_lt namespace LT.lt protected theorem false : a < a → False := lt_irrefl a theorem ne' (h : a < b) : b ≠ a := h.ne.symm end LT.lt theorem le_of_forall_le (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ le_rfl theorem le_of_forall_ge (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ le_rfl @[deprecated (since := "2025-01-30")] alias le_of_forall_le' := le_of_forall_ge theorem forall_le_iff_le : (∀ ⦃c⦄, c ≤ a → c ≤ b) ↔ a ≤ b := ⟨le_of_forall_le, fun h _ hca ↦ le_trans hca h⟩ theorem forall_le_iff_ge : (∀ ⦃c⦄, a ≤ c → b ≤ c) ↔ b ≤ a := ⟨le_of_forall_ge, fun h _ hca ↦ le_trans h hca⟩ /-- monotonicity of `≤` with respect to `→` -/ theorem le_implies_le_of_le_of_le (hca : c ≤ a) (hbd : b ≤ d) : a ≤ b → c ≤ d := fun hab ↦ (hca.trans hab).trans hbd end Preorder /-! ### Partial order -/ section PartialOrder variable [PartialOrder α] {a b : α} theorem ge_antisymm : a ≤ b → b ≤ a → b = a := flip le_antisymm theorem lt_of_le_of_ne' : a ≤ b → b ≠ a → a < b := fun h₁ h₂ ↦ lt_of_le_of_ne h₁ h₂.symm theorem Ne.lt_of_le : a ≠ b → a ≤ b → a < b := flip lt_of_le_of_ne theorem Ne.lt_of_le' : b ≠ a → a ≤ b → a < b := flip lt_of_le_of_ne' alias LE.le.antisymm := le_antisymm alias LE.le.antisymm' := ge_antisymm alias LE.le.lt_of_ne := lt_of_le_of_ne alias LE.le.lt_of_ne' := lt_of_le_of_ne' alias LE.le.lt_or_eq := lt_or_eq_of_le -- Unnecessary brackets are here for readability lemma le_imp_eq_iff_le_imp_le : (a ≤ b → b = a) ↔ (a ≤ b → b ≤ a) where mp h hab := (h hab).le mpr h hab := (h hab).antisymm hab -- Unnecessary brackets are here for readability lemma ge_imp_eq_iff_le_imp_le : (a ≤ b → a = b) ↔ (a ≤ b → b ≤ a) where mp h hab := (h hab).ge mpr h hab := hab.antisymm (h hab) namespace LE.le theorem lt_iff_ne (h : a ≤ b) : a < b ↔ a ≠ b := ⟨fun h ↦ h.ne, h.lt_of_ne⟩ theorem gt_iff_ne (h : a ≤ b) : a < b ↔ b ≠ a := ⟨fun h ↦ h.ne.symm, h.lt_of_ne'⟩ theorem not_lt_iff_eq (h : a ≤ b) : ¬a < b ↔ a = b := h.lt_iff_ne.not_left theorem not_gt_iff_eq (h : a ≤ b) : ¬a < b ↔ b = a := h.gt_iff_ne.not_left theorem le_iff_eq (h : a ≤ b) : b ≤ a ↔ b = a := ⟨fun h' ↦ h'.antisymm h, Eq.le⟩ theorem ge_iff_eq (h : a ≤ b) : b ≤ a ↔ a = b := ⟨h.antisymm, Eq.ge⟩ end LE.le -- See Note [decidable namespace] protected theorem Decidable.le_iff_eq_or_lt [DecidableLE α] : a ≤ b ↔ a = b ∨ a < b := Decidable.le_iff_lt_or_eq.trans or_comm theorem le_iff_eq_or_lt : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or_comm theorem lt_iff_le_and_ne : a < b ↔ a ≤ b ∧ a ≠ b := ⟨fun h ↦ ⟨le_of_lt h, ne_of_lt h⟩, fun ⟨h1, h2⟩ ↦ h1.lt_of_ne h2⟩ lemma eq_iff_not_lt_of_le (hab : a ≤ b) : a = b ↔ ¬ a < b := by simp [hab, lt_iff_le_and_ne] alias LE.le.eq_iff_not_lt := eq_iff_not_lt_of_le -- See Note [decidable namespace] protected theorem Decidable.eq_iff_le_not_lt [DecidableLE α] : a = b ↔ a ≤ b ∧ ¬a < b := ⟨fun h ↦ ⟨h.le, h ▸ lt_irrefl _⟩, fun ⟨h₁, h₂⟩ ↦ h₁.antisymm <| Decidable.byContradiction fun h₃ ↦ h₂ (h₁.lt_of_not_le h₃)⟩ theorem eq_iff_le_not_lt : a = b ↔ a ≤ b ∧ ¬a < b := haveI := Classical.dec Decidable.eq_iff_le_not_lt theorem eq_or_lt_of_le (h : a ≤ b) : a = b ∨ a < b := h.lt_or_eq.symm theorem eq_or_gt_of_le (h : a ≤ b) : b = a ∨ a < b := h.lt_or_eq.symm.imp Eq.symm id theorem gt_or_eq_of_le (h : a ≤ b) : a < b ∨ b = a := (eq_or_gt_of_le h).symm alias LE.le.eq_or_lt_dec := Decidable.eq_or_lt_of_le alias LE.le.eq_or_lt := eq_or_lt_of_le alias LE.le.eq_or_gt := eq_or_gt_of_le alias LE.le.gt_or_eq := gt_or_eq_of_le theorem eq_of_le_of_not_lt (hab : a ≤ b) (hba : ¬a < b) : a = b := hab.eq_or_lt.resolve_right hba theorem eq_of_ge_of_not_gt (hab : a ≤ b) (hba : ¬a < b) : b = a := (eq_of_le_of_not_lt hab hba).symm alias LE.le.eq_of_not_lt := eq_of_le_of_not_lt alias LE.le.eq_of_not_gt := eq_of_ge_of_not_gt theorem Ne.le_iff_lt (h : a ≠ b) : a ≤ b ↔ a < b := ⟨fun h' ↦ lt_of_le_of_ne h' h, fun h ↦ h.le⟩ theorem Ne.not_le_or_not_le (h : a ≠ b) : ¬a ≤ b ∨ ¬b ≤ a := not_and_or.1 <| le_antisymm_iff.not.1 h -- See Note [decidable namespace] protected theorem Decidable.ne_iff_lt_iff_le [DecidableEq α] : (a ≠ b ↔ a < b) ↔ a ≤ b := ⟨fun h ↦ Decidable.byCases le_of_eq (le_of_lt ∘ h.mp), fun h ↦ ⟨lt_of_le_of_ne h, ne_of_lt⟩⟩ @[simp] theorem ne_iff_lt_iff_le : (a ≠ b ↔ a < b) ↔ a ≤ b := haveI := Classical.dec Decidable.ne_iff_lt_iff_le lemma eq_of_forall_le_iff (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := ((H _).1 le_rfl).antisymm ((H _).2 le_rfl) lemma eq_of_forall_ge_iff (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := ((H _).2 le_rfl).antisymm ((H _).1 le_rfl) /-- To prove commutativity of a binary operation `○`, we only to check `a ○ b ≤ b ○ a` for all `a`, `b`. -/ lemma commutative_of_le {f : β → β → α} (comm : ∀ a b, f a b ≤ f b a) : ∀ a b, f a b = f b a := fun _ _ ↦ (comm _ _).antisymm <| comm _ _ /-- To prove associativity of a commutative binary operation `○`, we only to check `(a ○ b) ○ c ≤ a ○ (b ○ c)` for all `a`, `b`, `c`. -/ lemma associative_of_commutative_of_le {f : α → α → α} (comm : Std.Commutative f) (assoc : ∀ a b c, f (f a b) c ≤ f a (f b c)) : Std.Associative f where assoc a b c := le_antisymm (assoc _ _ _) <| by rw [comm.comm, comm.comm b, comm.comm _ c, comm.comm a] exact assoc .. end PartialOrder section LinearOrder variable [LinearOrder α] {a b : α} namespace LE.le lemma lt_or_le (h : a ≤ b) (c : α) : a < c ∨ c ≤ b := (lt_or_ge a c).imp id h.trans' lemma le_or_lt (h : a ≤ b) (c : α) : a ≤ c ∨ c < b := (le_or_gt a c).imp id h.trans_lt' lemma le_or_le (h : a ≤ b) (c : α) : a ≤ c ∨ c ≤ b := (h.lt_or_le c).imp le_of_lt id end LE.le namespace LT.lt lemma lt_or_lt (h : a < b) (c : α) : a < c ∨ c < b := (le_or_gt b c).imp h.trans_le id end LT.lt -- Variant of `min_def` with the branches reversed. theorem min_def' (a b : α) : min a b = if b ≤ a then b else a := by rw [min_def] rcases lt_trichotomy a b with (lt | eq | gt) · rw [if_pos lt.le, if_neg (not_le.mpr lt)] · rw [if_pos eq.le, if_pos eq.ge, eq] · rw [if_neg (not_le.mpr gt.gt), if_pos gt.le] -- Variant of `min_def` with the branches reversed. -- This is sometimes useful as it used to be the default. theorem max_def' (a b : α) : max a b = if b ≤ a then a else b := by rw [max_def] rcases lt_trichotomy a b with (lt | eq | gt) · rw [if_pos lt.le, if_neg (not_le.mpr lt)] · rw [if_pos eq.le, if_pos eq.ge, eq] · rw [if_neg (not_le.mpr gt.gt), if_pos gt.le] theorem lt_of_not_le (h : ¬b ≤ a) : a < b := ((le_total _ _).resolve_right h).lt_of_not_le h theorem lt_iff_not_le : a < b ↔ ¬b ≤ a := ⟨not_le_of_lt, lt_of_not_le⟩ theorem Ne.lt_or_lt (h : a ≠ b) : a < b ∨ b < a := lt_or_gt_of_ne h /-- A version of `ne_iff_lt_or_gt` with LHS and RHS reversed. -/ @[simp] theorem lt_or_lt_iff_ne : a < b ∨ b < a ↔ a ≠ b := ne_iff_lt_or_gt.symm theorem not_lt_iff_eq_or_lt : ¬a < b ↔ a = b ∨ b < a := not_lt.trans <| Decidable.le_iff_eq_or_lt.trans <| or_congr eq_comm Iff.rfl theorem exists_ge_of_linear (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | Or.inl h => ⟨_, h, le_rfl⟩ | Or.inr h => ⟨_, le_rfl, h⟩ lemma exists_forall_ge_and {p q : α → Prop} : (∃ i, ∀ j ≥ i, p j) → (∃ i, ∀ j ≥ i, q j) → ∃ i, ∀ j ≥ i, p j ∧ q j | ⟨a, ha⟩, ⟨b, hb⟩ => let ⟨c, hac, hbc⟩ := exists_ge_of_linear a b ⟨c, fun _d hcd ↦ ⟨ha _ <| hac.trans hcd, hb _ <| hbc.trans hcd⟩⟩ theorem le_of_forall_lt (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt fun h ↦ lt_irrefl _ (H _ h) theorem forall_lt_iff_le : (∀ ⦃c⦄, c < a → c < b) ↔ a ≤ b := ⟨le_of_forall_lt, fun h _ hca ↦ lt_of_lt_of_le hca h⟩ theorem le_of_forall_lt' (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt fun h ↦ lt_irrefl _ (H _ h) theorem forall_lt_iff_le' : (∀ ⦃c⦄, a < c → b < c) ↔ b ≤ a := ⟨le_of_forall_lt', fun h _ hac ↦ lt_of_le_of_lt h hac⟩ theorem eq_of_forall_lt_iff (h : ∀ c, c < a ↔ c < b) : a = b := (le_of_forall_lt fun _ ↦ (h _).1).antisymm <| le_of_forall_lt fun _ ↦ (h _).2 theorem eq_of_forall_gt_iff (h : ∀ c, a < c ↔ b < c) : a = b := (le_of_forall_lt' fun _ ↦ (h _).2).antisymm <| le_of_forall_lt' fun _ ↦ (h _).1 section ltByCases variable {P : Sort*} {x y : α} @[simp] lemma ltByCases_lt (h : x < y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₁ h := dif_pos h @[simp] lemma ltByCases_gt (h : y < x) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₃ h := (dif_neg h.not_lt).trans (dif_pos h) @[simp] lemma ltByCases_eq (h : x = y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} : ltByCases x y h₁ h₂ h₃ = h₂ h := (dif_neg h.not_lt).trans (dif_neg h.not_gt) lemma ltByCases_not_lt (h : ¬ x < y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ y < x → x = y := fun h' => (le_antisymm (le_of_not_gt h') (le_of_not_gt h))) : ltByCases x y h₁ h₂ h₃ = if h' : y < x then h₃ h' else h₂ (p h') := dif_neg h lemma ltByCases_not_gt (h : ¬ y < x) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ x < y → x = y := fun h' => (le_antisymm (le_of_not_gt h) (le_of_not_gt h'))) : ltByCases x y h₁ h₂ h₃ = if h' : x < y then h₁ h' else h₂ (p h') := dite_congr rfl (fun _ => rfl) (fun _ => dif_neg h) lemma ltByCases_ne (h : x ≠ y) {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : ¬ x < y → y < x := fun h' => h.lt_or_lt.resolve_left h') : ltByCases x y h₁ h₂ h₃ = if h' : x < y then h₁ h' else h₃ (p h') := dite_congr rfl (fun _ => rfl) (fun _ => dif_pos _) lemma ltByCases_comm {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : y = x → x = y := fun h' => h'.symm) : ltByCases x y h₁ h₂ h₃ = ltByCases y x h₃ (h₂ ∘ p) h₁ := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · rw [ltByCases_lt h, ltByCases_gt h] · rw [ltByCases_eq h, ltByCases_eq h.symm, comp_apply] · rw [ltByCases_lt h, ltByCases_gt h] lemma eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt {x' y' : α} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) : x = y ↔ x' = y' := by simp_rw [eq_iff_le_not_lt, ← not_lt, ltc, gtc] lemma ltByCases_rec {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} (p : P) (hlt : (h : x < y) → h₁ h = p) (heq : (h : x = y) → h₂ h = p) (hgt : (h : y < x) → h₃ h = p) : ltByCases x y h₁ h₂ h₃ = p := ltByCases x y (fun h => ltByCases_lt h ▸ hlt h) (fun h => ltByCases_eq h ▸ heq h) (fun h => ltByCases_gt h ▸ hgt h) lemma ltByCases_eq_iff {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} {p : P} : ltByCases x y h₁ h₂ h₃ = p ↔ (∃ h, h₁ h = p) ∨ (∃ h, h₂ h = p) ∨ (∃ h, h₃ h = p) := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · simp only [ltByCases_lt, exists_prop_of_true, h, h.not_lt, not_false_eq_true, exists_prop_of_false, or_false, h.ne] · simp only [h, lt_self_iff_false, ltByCases_eq, not_false_eq_true, exists_prop_of_false, exists_prop_of_true, or_false, false_or] · simp only [ltByCases_gt, exists_prop_of_true, h, h.not_lt, not_false_eq_true, exists_prop_of_false, false_or, h.ne'] lemma ltByCases_congr {x' y' : α} {h₁ : x < y → P} {h₂ : x = y → P} {h₃ : y < x → P} {h₁' : x' < y' → P} {h₂' : x' = y' → P} {h₃' : y' < x' → P} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) (hh'₁ : ∀ (h : x' < y'), h₁ (ltc.mpr h) = h₁' h) (hh'₂ : ∀ (h : x' = y'), h₂ ((eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt ltc gtc).mpr h) = h₂' h) (hh'₃ : ∀ (h : y' < x'), h₃ (gtc.mpr h) = h₃' h) : ltByCases x y h₁ h₂ h₃ = ltByCases x' y' h₁' h₂' h₃' := by refine ltByCases_rec _ (fun h => ?_) (fun h => ?_) (fun h => ?_) · rw [ltByCases_lt (ltc.mp h), hh'₁] · rw [eq_iff_eq_of_lt_iff_lt_of_gt_iff_gt ltc gtc] at h rw [ltByCases_eq h, hh'₂] · rw [ltByCases_gt (gtc.mp h), hh'₃] /-- Perform a case-split on the ordering of `x` and `y` in a decidable linear order, non-dependently. -/ abbrev ltTrichotomy (x y : α) (p q r : P) := ltByCases x y (fun _ => p) (fun _ => q) (fun _ => r) variable {p q r s : P} @[simp] lemma ltTrichotomy_lt (h : x < y) : ltTrichotomy x y p q r = p := ltByCases_lt h @[simp] lemma ltTrichotomy_gt (h : y < x) : ltTrichotomy x y p q r = r := ltByCases_gt h @[simp] lemma ltTrichotomy_eq (h : x = y) : ltTrichotomy x y p q r = q := ltByCases_eq h lemma ltTrichotomy_not_lt (h : ¬ x < y) : ltTrichotomy x y p q r = if y < x then r else q := ltByCases_not_lt h lemma ltTrichotomy_not_gt (h : ¬ y < x) : ltTrichotomy x y p q r = if x < y then p else q := ltByCases_not_gt h lemma ltTrichotomy_ne (h : x ≠ y) : ltTrichotomy x y p q r = if x < y then p else r := ltByCases_ne h lemma ltTrichotomy_comm : ltTrichotomy x y p q r = ltTrichotomy y x r q p := ltByCases_comm lemma ltTrichotomy_self {p : P} : ltTrichotomy x y p p p = p := ltByCases_rec p (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) lemma ltTrichotomy_eq_iff : ltTrichotomy x y p q r = s ↔ (x < y ∧ p = s) ∨ (x = y ∧ q = s) ∨ (y < x ∧ r = s) := by refine ltByCases x y (fun h => ?_) (fun h => ?_) (fun h => ?_) · simp only [ltTrichotomy_lt, false_and, true_and, or_false, h, h.not_lt, h.ne] · simp only [ltTrichotomy_eq, false_and, true_and, or_false, false_or, h, lt_irrefl] · simp only [ltTrichotomy_gt, false_and, true_and, false_or, h, h.not_lt, h.ne'] lemma ltTrichotomy_congr {x' y' : α} {p' q' r' : P} (ltc : (x < y) ↔ (x' < y')) (gtc : (y < x) ↔ (y' < x')) (hh'₁ : x' < y' → p = p') (hh'₂ : x' = y' → q = q') (hh'₃ : y' < x' → r = r') : ltTrichotomy x y p q r = ltTrichotomy x' y' p' q' r' := ltByCases_congr ltc gtc hh'₁ hh'₂ hh'₃ end ltByCases /-! #### `min`/`max` recursors -/ section MinMaxRec variable {p : α → Prop} lemma min_rec (ha : a ≤ b → p a) (hb : b ≤ a → p b) : p (min a b) := by obtain hab | hba := le_total a b <;> simp [min_eq_left, min_eq_right, *] lemma max_rec (ha : b ≤ a → p a) (hb : a ≤ b → p b) : p (max a b) := by obtain hab | hba := le_total a b <;> simp [max_eq_left, max_eq_right, *] lemma min_rec' (p : α → Prop) (ha : p a) (hb : p b) : p (min a b) := min_rec (fun _ ↦ ha) fun _ ↦ hb lemma max_rec' (p : α → Prop) (ha : p a) (hb : p b) : p (max a b) := max_rec (fun _ ↦ ha) fun _ ↦ hb lemma min_def_lt (a b : α) : min a b = if a < b then a else b := by rw [min_comm, min_def, ← ite_not]; simp only [not_le] lemma max_def_lt (a b : α) : max a b = if a < b then b else a := by rw [max_comm, max_def, ← ite_not]; simp only [not_le] end MinMaxRec end LinearOrder /-! ### Implications -/ lemma lt_imp_lt_of_le_imp_le {β} [LinearOrder α] [Preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_le fun h' ↦ (H h').not_lt h lemma le_imp_le_iff_lt_imp_lt {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} : a ≤ b → c ≤ d ↔ d < c → b < a := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [Preorder α] [Preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans <| (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans <| (not_congr H).trans <| not_le lemma le_iff_le_iff_lt_iff_lt {β} [LinearOrder α] [LinearOrder β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, fun H ↦ not_lt.symm.trans <| (not_congr H).trans <| not_lt⟩ /-- A symmetric relation implies two values are equal, when it implies they're less-equal. -/ lemma rel_imp_eq_of_rel_imp_le [PartialOrder β] (r : α → α → Prop) [IsSymm α r] {f : α → β} (h : ∀ a b, r a b → f a ≤ f b) {a b : α} : r a b → f a = f b := fun hab ↦ le_antisymm (h a b hab) (h b a <| symm hab) /-! ### Extensionality lemmas -/ @[ext] lemma Preorder.toLE_injective : Function.Injective (@Preorder.toLE α) := fun | { lt := A_lt, lt_iff_le_not_le := A_iff, .. }, { lt := B_lt, lt_iff_le_not_le := B_iff, .. } => by rintro ⟨⟩ have : A_lt = B_lt := by funext a b rw [A_iff, B_iff] cases this congr @[ext] lemma PartialOrder.toPreorder_injective : Function.Injective (@PartialOrder.toPreorder α) := by rintro ⟨⟩ ⟨⟩ ⟨⟩; congr @[ext] lemma LinearOrder.toPartialOrder_injective : Function.Injective (@LinearOrder.toPartialOrder α) := fun | { le := A_le, lt := A_lt, toDecidableLE := A_decidableLE, toDecidableEq := A_decidableEq, toDecidableLT := A_decidableLT min := A_min, max := A_max, min_def := A_min_def, max_def := A_max_def, compare := A_compare, compare_eq_compareOfLessAndEq := A_compare_canonical, .. }, { le := B_le, lt := B_lt, toDecidableLE := B_decidableLE, toDecidableEq := B_decidableEq, toDecidableLT := B_decidableLT min := B_min, max := B_max, min_def := B_min_def, max_def := B_max_def, compare := B_compare, compare_eq_compareOfLessAndEq := B_compare_canonical, .. } => by rintro ⟨⟩ obtain rfl : A_decidableLE = B_decidableLE := Subsingleton.elim _ _ obtain rfl : A_decidableEq = B_decidableEq := Subsingleton.elim _ _ obtain rfl : A_decidableLT = B_decidableLT := Subsingleton.elim _ _ have : A_min = B_min := by funext a b exact (A_min_def _ _).trans (B_min_def _ _).symm cases this have : A_max = B_max := by funext a b exact (A_max_def _ _).trans (B_max_def _ _).symm cases this have : A_compare = B_compare := by funext a b exact (A_compare_canonical _ _).trans (B_compare_canonical _ _).symm congr lemma Preorder.ext {A B : Preorder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma PartialOrder.ext {A B : PartialOrder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma PartialOrder.ext_lt {A B : PartialOrder α} (H : ∀ x y : α, (haveI := A; x < y) ↔ x < y) : A = B := by ext x y; rw [le_iff_lt_or_eq, @le_iff_lt_or_eq _ A, H] lemma LinearOrder.ext {A B : LinearOrder α} (H : ∀ x y : α, (haveI := A; x ≤ y) ↔ x ≤ y) : A = B := by ext x y; exact H x y lemma LinearOrder.ext_lt {A B : LinearOrder α} (H : ∀ x y : α, (haveI := A; x < y) ↔ x < y) : A = B := LinearOrder.toPartialOrder_injective (PartialOrder.ext_lt H) /-! ### Order dual -/ /-- Type synonym to equip a type with the dual order: `≤` means `≥` and `<` means `>`. `αᵒᵈ` is notation for `OrderDual α`. -/ def OrderDual (α : Type*) : Type _ := α @[inherit_doc] notation:max α "ᵒᵈ" => OrderDual α namespace OrderDual instance (α : Type*) [h : Nonempty α] : Nonempty αᵒᵈ := h instance (α : Type*) [h : Subsingleton α] : Subsingleton αᵒᵈ := h instance (α : Type*) [LE α] : LE αᵒᵈ := ⟨fun x y : α ↦ y ≤ x⟩ instance (α : Type*) [LT α] : LT αᵒᵈ := ⟨fun x y : α ↦ y < x⟩ instance instOrd (α : Type*) [Ord α] : Ord αᵒᵈ where compare := fun (a b : α) ↦ compare b a instance instSup (α : Type*) [Min α] : Max αᵒᵈ := ⟨((· ⊓ ·) : α → α → α)⟩ instance instInf (α : Type*) [Max α] : Min αᵒᵈ := ⟨((· ⊔ ·) : α → α → α)⟩ instance instPreorder (α : Type*) [Preorder α] : Preorder αᵒᵈ where le_refl := fun _ ↦ le_refl _ le_trans := fun _ _ _ hab hbc ↦ hbc.trans hab lt_iff_le_not_le := fun _ _ ↦ lt_iff_le_not_le instance instPartialOrder (α : Type*) [PartialOrder α] : PartialOrder αᵒᵈ where __ := inferInstanceAs (Preorder αᵒᵈ) le_antisymm := fun a b hab hba ↦ @le_antisymm α _ a b hba hab instance instLinearOrder (α : Type*) [LinearOrder α] : LinearOrder αᵒᵈ where __ := inferInstanceAs (PartialOrder αᵒᵈ) __ := inferInstanceAs (Ord αᵒᵈ) le_total := fun a b : α ↦ le_total b a max := fun a b ↦ (min a b : α) min := fun a b ↦ (max a b : α) min_def := fun a b ↦ show (max .. : α) = _ by rw [max_comm, max_def]; rfl max_def := fun a b ↦ show (min .. : α) = _ by rw [min_comm, min_def]; rfl toDecidableLE := (inferInstance : DecidableRel (fun a b : α ↦ b ≤ a)) toDecidableLT := (inferInstance : DecidableRel (fun a b : α ↦ b < a)) toDecidableEq := (inferInstance : DecidableEq α) compare_eq_compareOfLessAndEq a b := by simp only [compare, LinearOrder.compare_eq_compareOfLessAndEq, compareOfLessAndEq, eq_comm] rfl /-- The opposite linear order to a given linear order -/ def _root_.LinearOrder.swap (α : Type*) (_ : LinearOrder α) : LinearOrder α := inferInstanceAs <| LinearOrder (OrderDual α) instance : ∀ [Inhabited α], Inhabited αᵒᵈ := fun [x : Inhabited α] => x theorem Ord.dual_dual (α : Type*) [H : Ord α] : OrderDual.instOrd αᵒᵈ = H := rfl theorem Preorder.dual_dual (α : Type*) [H : Preorder α] : OrderDual.instPreorder αᵒᵈ = H := rfl theorem instPartialOrder.dual_dual (α : Type*) [H : PartialOrder α] : OrderDual.instPartialOrder αᵒᵈ = H := rfl theorem instLinearOrder.dual_dual (α : Type*) [H : LinearOrder α] : OrderDual.instLinearOrder αᵒᵈ = H := rfl end OrderDual /-! ### `HasCompl` -/ instance Prop.hasCompl : HasCompl Prop := ⟨Not⟩ instance Pi.hasCompl [∀ i, HasCompl (π i)] : HasCompl (∀ i, π i) := ⟨fun x i ↦ (x i)ᶜ⟩ theorem Pi.compl_def [∀ i, HasCompl (π i)] (x : ∀ i, π i) : xᶜ = fun i ↦ (x i)ᶜ := rfl @[simp] theorem Pi.compl_apply [∀ i, HasCompl (π i)] (x : ∀ i, π i) (i : ι) : xᶜ i = (x i)ᶜ := rfl instance IsIrrefl.compl (r) [IsIrrefl α r] : IsRefl α rᶜ := ⟨@irrefl α r _⟩ instance IsRefl.compl (r) [IsRefl α r] : IsIrrefl α rᶜ := ⟨fun a ↦ not_not_intro (refl a)⟩ theorem compl_lt [LinearOrder α] : (· < · : α → α → _)ᶜ = (· ≥ ·) := by ext; simp [compl] theorem compl_le [LinearOrder α] : (· ≤ · : α → α → _)ᶜ = (· > ·) := by ext; simp [compl] theorem compl_gt [LinearOrder α] : (· > · : α → α → _)ᶜ = (· ≤ ·) := by ext; simp [compl] theorem compl_ge [LinearOrder α] : (· ≥ · : α → α → _)ᶜ = (· < ·) := by ext; simp [compl] instance Ne.instIsEquiv_compl : IsEquiv α (· ≠ ·)ᶜ := by convert eq_isEquiv α simp [compl] /-! ### Order instances on the function space -/ instance Pi.hasLe [∀ i, LE (π i)] : LE (∀ i, π i) where le x y := ∀ i, x i ≤ y i theorem Pi.le_def [∀ i, LE (π i)] {x y : ∀ i, π i} : x ≤ y ↔ ∀ i, x i ≤ y i := Iff.rfl instance Pi.preorder [∀ i, Preorder (π i)] : Preorder (∀ i, π i) where __ := inferInstanceAs (LE (∀ i, π i)) le_refl := fun a i ↦ le_refl (a i) le_trans := fun _ _ _ h₁ h₂ i ↦ le_trans (h₁ i) (h₂ i) theorem Pi.lt_def [∀ i, Preorder (π i)] {x y : ∀ i, π i} : x < y ↔ x ≤ y ∧ ∃ i, x i < y i := by simp +contextual [lt_iff_le_not_le, Pi.le_def] instance Pi.partialOrder [∀ i, PartialOrder (π i)] : PartialOrder (∀ i, π i) where __ := Pi.preorder le_antisymm := fun _ _ h1 h2 ↦ funext fun b ↦ (h1 b).antisymm (h2 b) namespace Sum variable {α₁ α₂ : Type*} [LE β] @[simp] lemma elim_le_elim_iff {u₁ v₁ : α₁ → β} {u₂ v₂ : α₂ → β} : Sum.elim u₁ u₂ ≤ Sum.elim v₁ v₂ ↔ u₁ ≤ v₁ ∧ u₂ ≤ v₂ := Sum.forall lemma const_le_elim_iff {b : β} {v₁ : α₁ → β} {v₂ : α₂ → β} : Function.const _ b ≤ Sum.elim v₁ v₂ ↔ Function.const _ b ≤ v₁ ∧ Function.const _ b ≤ v₂ := elim_const_const b ▸ elim_le_elim_iff .. lemma elim_le_const_iff {b : β} {u₁ : α₁ → β} {u₂ : α₂ → β} : Sum.elim u₁ u₂ ≤ Function.const _ b ↔ u₁ ≤ Function.const _ b ∧ u₂ ≤ Function.const _ b := elim_const_const b ▸ elim_le_elim_iff .. end Sum section Pi /-- A function `a` is strongly less than a function `b` if `a i < b i` for all `i`. -/ def StrongLT [∀ i, LT (π i)] (a b : ∀ i, π i) : Prop := ∀ i, a i < b i @[inherit_doc] local infixl:50 " ≺ " => StrongLT variable [∀ i, Preorder (π i)] {a b c : ∀ i, π i} theorem le_of_strongLT (h : a ≺ b) : a ≤ b := fun _ ↦ (h _).le theorem lt_of_strongLT [Nonempty ι] (h : a ≺ b) : a < b := by inhabit ι exact Pi.lt_def.2 ⟨le_of_strongLT h, default, h _⟩ theorem strongLT_of_strongLT_of_le (hab : a ≺ b) (hbc : b ≤ c) : a ≺ c := fun _ ↦ (hab _).trans_le <| hbc _ theorem strongLT_of_le_of_strongLT (hab : a ≤ b) (hbc : b ≺ c) : a ≺ c := fun _ ↦ (hab _).trans_lt <| hbc _ alias StrongLT.le := le_of_strongLT alias StrongLT.lt := lt_of_strongLT alias StrongLT.trans_le := strongLT_of_strongLT_of_le alias LE.le.trans_strongLT := strongLT_of_le_of_strongLT end Pi section Function variable [DecidableEq ι] [∀ i, Preorder (π i)] {x y : ∀ i, π i} {i : ι} {a b : π i} theorem le_update_iff : x ≤ Function.update y i a ↔ x i ≤ a ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := Function.forall_update_iff _ fun j z ↦ x j ≤ z theorem update_le_iff : Function.update x i a ≤ y ↔ a ≤ y i ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := Function.forall_update_iff _ fun j z ↦ z ≤ y j theorem update_le_update_iff : Function.update x i a ≤ Function.update y i b ↔ a ≤ b ∧ ∀ (j) (_ : j ≠ i), x j ≤ y j := by simp +contextual [update_le_iff] @[simp] theorem update_le_update_iff' : update x i a ≤ update x i b ↔ a ≤ b := by simp [update_le_update_iff] @[simp] theorem update_lt_update_iff : update x i a < update x i b ↔ a < b := lt_iff_lt_of_le_iff_le' update_le_update_iff' update_le_update_iff' @[simp] theorem le_update_self_iff : x ≤ update x i a ↔ x i ≤ a := by simp [le_update_iff] @[simp] theorem update_le_self_iff : update x i a ≤ x ↔ a ≤ x i := by simp [update_le_iff] @[simp] theorem lt_update_self_iff : x < update x i a ↔ x i < a := by simp [lt_iff_le_not_le] @[simp] theorem update_lt_self_iff : update x i a < x ↔ a < x i := by simp [lt_iff_le_not_le] end Function instance Pi.sdiff [∀ i, SDiff (π i)] : SDiff (∀ i, π i) := ⟨fun x y i ↦ x i \ y i⟩ theorem Pi.sdiff_def [∀ i, SDiff (π i)] (x y : ∀ i, π i) : x \ y = fun i ↦ x i \ y i := rfl @[simp] theorem Pi.sdiff_apply [∀ i, SDiff (π i)] (x y : ∀ i, π i) (i : ι) : (x \ y) i = x i \ y i := rfl namespace Function variable [Preorder α] [Nonempty β] {a b : α} @[simp] theorem const_le_const : const β a ≤ const β b ↔ a ≤ b := by simp [Pi.le_def] @[simp] theorem const_lt_const : const β a < const β b ↔ a < b := by simpa [Pi.lt_def] using le_of_lt end Function /-! ### Lifts of order instances -/ /-- Transfer a `Preorder` on `β` to a `Preorder` on `α` using a function `f : α → β`. See note [reducible non-instances]. -/ abbrev Preorder.lift [Preorder β] (f : α → β) : Preorder α where le x y := f x ≤ f y le_refl _ := le_rfl le_trans _ _ _ := _root_.le_trans lt x y := f x < f y lt_iff_le_not_le _ _ := _root_.lt_iff_le_not_le /-- Transfer a `PartialOrder` on `β` to a `PartialOrder` on `α` using an injective function `f : α → β`. See note [reducible non-instances]. -/ abbrev PartialOrder.lift [PartialOrder β] (f : α → β) (inj : Injective f) : PartialOrder α := { Preorder.lift f with le_antisymm := fun _ _ h₁ h₂ ↦ inj (h₁.antisymm h₂) } theorem compare_of_injective_eq_compareOfLessAndEq (a b : α) [LinearOrder β] [DecidableEq α] (f : α → β) (inj : Injective f) [Decidable (LT.lt (self := PartialOrder.lift f inj |>.toLT) a b)] : compare (f a) (f b) = @compareOfLessAndEq _ a b (PartialOrder.lift f inj |>.toLT) _ _ := by have h := LinearOrder.compare_eq_compareOfLessAndEq (f a) (f b) simp only [h, compareOfLessAndEq] split_ifs <;> try (first | rfl | contradiction) · have : ¬ f a = f b := by rename_i h; exact inj.ne h contradiction · have : f a = f b := by rename_i h; exact congrArg f h contradiction /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version takes `[Max α]` and `[Min α]` as arguments, then uses them for `max` and `min` fields. See `LinearOrder.lift'` for a version that autogenerates `min` and `max` fields, and `LinearOrder.liftWithOrd` for one that does not auto-generate `compare` fields. See note [reducible non-instances]. -/ abbrev LinearOrder.lift [LinearOrder β] [Max α] [Min α] (f : α → β) (inj : Injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrder α := letI instOrdα : Ord α := ⟨fun a b ↦ compare (f a) (f b)⟩ letI decidableLE := fun x y ↦ (inferInstance : Decidable (f x ≤ f y)) letI decidableLT := fun x y ↦ (inferInstance : Decidable (f x < f y)) letI decidableEq := fun x y ↦ decidable_of_iff (f x = f y) inj.eq_iff { PartialOrder.lift f inj, instOrdα with le_total := fun x y ↦ le_total (f x) (f y) toDecidableLE := decidableLE toDecidableLT := decidableLT toDecidableEq := decidableEq min := (· ⊓ ·) max := (· ⊔ ·) min_def := by intros x y apply inj rw [apply_ite f] exact (hinf _ _).trans (min_def _ _) max_def := by intros x y apply inj rw [apply_ite f] exact (hsup _ _).trans (max_def _ _) compare_eq_compareOfLessAndEq := fun a b ↦ compare_of_injective_eq_compareOfLessAndEq a b f inj } /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version autogenerates `min` and `max` fields. See `LinearOrder.lift` for a version that takes `[Max α]` and `[Min α]`, then uses them as `max` and `min`. See `LinearOrder.liftWithOrd'` for a version which does not auto-generate `compare` fields. See note [reducible non-instances]. -/ abbrev LinearOrder.lift' [LinearOrder β] (f : α → β) (inj : Injective f) : LinearOrder α := @LinearOrder.lift α β _ ⟨fun x y ↦ if f x ≤ f y then y else x⟩ ⟨fun x y ↦ if f x ≤ f y then x else y⟩ f inj (fun _ _ ↦ (apply_ite f _ _ _).trans (max_def _ _).symm) fun _ _ ↦ (apply_ite f _ _ _).trans (min_def _ _).symm /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version takes `[Max α]` and `[Min α]` as arguments, then uses them for `max` and `min` fields. It also takes `[Ord α]` as an argument and uses them for `compare` fields. See `LinearOrder.lift` for a version that autogenerates `compare` fields, and `LinearOrder.liftWithOrd'` for one that auto-generates `min` and `max` fields. fields. See note [reducible non-instances]. -/ abbrev LinearOrder.liftWithOrd [LinearOrder β] [Max α] [Min α] [Ord α] (f : α → β) (inj : Injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) (compare_f : ∀ a b : α, compare a b = compare (f a) (f b)) : LinearOrder α := letI decidableLE := fun x y ↦ (inferInstance : Decidable (f x ≤ f y)) letI decidableLT := fun x y ↦ (inferInstance : Decidable (f x < f y)) letI decidableEq := fun x y ↦ decidable_of_iff (f x = f y) inj.eq_iff { PartialOrder.lift f inj with le_total := fun x y ↦ le_total (f x) (f y) toDecidableLE := decidableLE toDecidableLT := decidableLT toDecidableEq := decidableEq min := (· ⊓ ·) max := (· ⊔ ·) min_def := by intros x y apply inj rw [apply_ite f] exact (hinf _ _).trans (min_def _ _) max_def := by intros x y apply inj rw [apply_ite f] exact (hsup _ _).trans (max_def _ _) compare_eq_compareOfLessAndEq := fun a b ↦ (compare_f a b).trans <| compare_of_injective_eq_compareOfLessAndEq a b f inj } /-- Transfer a `LinearOrder` on `β` to a `LinearOrder` on `α` using an injective function `f : α → β`. This version auto-generates `min` and `max` fields. It also takes `[Ord α]` as an argument and uses them for `compare` fields. See `LinearOrder.lift` for a version that autogenerates `compare` fields, and `LinearOrder.liftWithOrd` for one that doesn't auto-generate `min` and `max` fields. fields. See note [reducible non-instances]. -/ abbrev LinearOrder.liftWithOrd' [LinearOrder β] [Ord α] (f : α → β) (inj : Injective f) (compare_f : ∀ a b : α, compare a b = compare (f a) (f b)) : LinearOrder α := @LinearOrder.liftWithOrd α β _ ⟨fun x y ↦ if f x ≤ f y then y else x⟩ ⟨fun x y ↦ if f x ≤ f y then x else y⟩ _ f inj (fun _ _ ↦ (apply_ite f _ _ _).trans (max_def _ _).symm) (fun _ _ ↦ (apply_ite f _ _ _).trans (min_def _ _).symm) compare_f /-! ### Subtype of an order -/ namespace Subtype instance le [LE α] {p : α → Prop} : LE (Subtype p) := ⟨fun x y ↦ (x : α) ≤ y⟩ instance lt [LT α] {p : α → Prop} : LT (Subtype p) := ⟨fun x y ↦ (x : α) < y⟩ @[simp] theorem mk_le_mk [LE α] {p : α → Prop} {x y : α} {hx : p x} {hy : p y} : (⟨x, hx⟩ : Subtype p) ≤ ⟨y, hy⟩ ↔ x ≤ y := Iff.rfl @[simp] theorem mk_lt_mk [LT α] {p : α → Prop} {x y : α} {hx : p x} {hy : p y} : (⟨x, hx⟩ : Subtype p) < ⟨y, hy⟩ ↔ x < y := Iff.rfl @[simp, norm_cast] theorem coe_le_coe [LE α] {p : α → Prop} {x y : Subtype p} : (x : α) ≤ y ↔ x ≤ y := Iff.rfl @[gcongr] alias ⟨_, GCongr.coe_le_coe⟩ := coe_le_coe @[simp, norm_cast] theorem coe_lt_coe [LT α] {p : α → Prop} {x y : Subtype p} : (x : α) < y ↔ x < y := Iff.rfl @[gcongr] alias ⟨_, GCongr.coe_lt_coe⟩ := coe_lt_coe instance preorder [Preorder α] (p : α → Prop) : Preorder (Subtype p) := Preorder.lift (fun (a : Subtype p) ↦ (a : α)) instance partialOrder [PartialOrder α] (p : α → Prop) : PartialOrder (Subtype p) := PartialOrder.lift (fun (a : Subtype p) ↦ (a : α)) Subtype.coe_injective instance decidableLE [Preorder α] [h : DecidableLE α] {p : α → Prop} : DecidableLE (Subtype p) := fun a b ↦ h a b instance decidableLT [Preorder α] [h : DecidableLT α] {p : α → Prop} : DecidableLT (Subtype p) := fun a b ↦ h a b /-- A subtype of a linear order is a linear order. We explicitly give the proofs of decidable equality and decidable order in order to ensure the decidability instances are all definitionally equal. -/ instance instLinearOrder [LinearOrder α] (p : α → Prop) : LinearOrder (Subtype p) := @LinearOrder.lift (Subtype p) _ _ ⟨fun x y ↦ ⟨max x y, max_rec' _ x.2 y.2⟩⟩ ⟨fun x y ↦ ⟨min x y, min_rec' _ x.2 y.2⟩⟩ (fun (a : Subtype p) ↦ (a : α)) Subtype.coe_injective (fun _ _ ↦ rfl) fun _ _ ↦ rfl end Subtype /-! ### Pointwise order on `α × β` The lexicographic order is defined in `Data.Prod.Lex`, and the instances are available via the type synonym `α ×ₗ β = α × β`. -/ namespace Prod section LE variable [LE α] [LE β] {x y : α × β} {a a₁ a₂ : α} {b b₁ b₂ : β} instance : LE (α × β) where le p q := p.1 ≤ q.1 ∧ p.2 ≤ q.2 instance instDecidableLE [Decidable (x.1 ≤ y.1)] [Decidable (x.2 ≤ y.2)] : Decidable (x ≤ y) := inferInstanceAs (Decidable (x.1 ≤ y.1 ∧ x.2 ≤ y.2)) lemma le_def : x ≤ y ↔ x.1 ≤ y.1 ∧ x.2 ≤ y.2 := .rfl @[simp] lemma mk_le_mk : (a₁, b₁) ≤ (a₂, b₂) ↔ a₁ ≤ a₂ ∧ b₁ ≤ b₂ := .rfl @[simp] lemma swap_le_swap : x.swap ≤ y.swap ↔ x ≤ y := and_comm @[simp] lemma swap_le_mk : x.swap ≤ (b, a) ↔ x ≤ (a, b) := and_comm @[simp] lemma mk_le_swap : (b, a) ≤ x.swap ↔ (a, b) ≤ x := and_comm end LE section Preorder variable [Preorder α] [Preorder β] {a a₁ a₂ : α} {b b₁ b₂ : β} {x y : α × β} instance : Preorder (α × β) where __ := inferInstanceAs (LE (α × β)) le_refl := fun ⟨a, b⟩ ↦ ⟨le_refl a, le_refl b⟩ le_trans := fun ⟨_, _⟩ ⟨_, _⟩ ⟨_, _⟩ ⟨hac, hbd⟩ ⟨hce, hdf⟩ ↦ ⟨le_trans hac hce, le_trans hbd hdf⟩ @[simp] theorem swap_lt_swap : x.swap < y.swap ↔ x < y := and_congr swap_le_swap (not_congr swap_le_swap) @[simp] lemma swap_lt_mk : x.swap < (b, a) ↔ x < (a, b) := by rw [← swap_lt_swap]; simp @[simp] lemma mk_lt_swap : (b, a) < x.swap ↔ (a, b) < x := by rw [← swap_lt_swap]; simp theorem mk_le_mk_iff_left : (a₁, b) ≤ (a₂, b) ↔ a₁ ≤ a₂ := and_iff_left le_rfl theorem mk_le_mk_iff_right : (a, b₁) ≤ (a, b₂) ↔ b₁ ≤ b₂ := and_iff_right le_rfl theorem mk_lt_mk_iff_left : (a₁, b) < (a₂, b) ↔ a₁ < a₂ := lt_iff_lt_of_le_iff_le' mk_le_mk_iff_left mk_le_mk_iff_left theorem mk_lt_mk_iff_right : (a, b₁) < (a, b₂) ↔ b₁ < b₂ := lt_iff_lt_of_le_iff_le' mk_le_mk_iff_right mk_le_mk_iff_right theorem lt_iff : x < y ↔ x.1 < y.1 ∧ x.2 ≤ y.2 ∨ x.1 ≤ y.1 ∧ x.2 < y.2 := by refine ⟨fun h ↦ ?_, ?_⟩ · by_cases h₁ : y.1 ≤ x.1 · exact Or.inr ⟨h.1.1, LE.le.lt_of_not_le h.1.2 fun h₂ ↦ h.2 ⟨h₁, h₂⟩⟩ · exact Or.inl ⟨LE.le.lt_of_not_le h.1.1 h₁, h.1.2⟩ · rintro (⟨h₁, h₂⟩ | ⟨h₁, h₂⟩) · exact ⟨⟨h₁.le, h₂⟩, fun h ↦ h₁.not_le h.1⟩ · exact ⟨⟨h₁, h₂.le⟩, fun h ↦ h₂.not_le h.2⟩ @[simp] theorem mk_lt_mk : (a₁, b₁) < (a₂, b₂) ↔ a₁ < a₂ ∧ b₁ ≤ b₂ ∨ a₁ ≤ a₂ ∧ b₁ < b₂ := lt_iff protected lemma lt_of_lt_of_le (h₁ : x.1 < y.1) (h₂ : x.2 ≤ y.2) : x < y := by simp [lt_iff, *] protected lemma lt_of_le_of_lt (h₁ : x.1 ≤ y.1) (h₂ : x.2 < y.2) : x < y := by simp [lt_iff, *] lemma mk_lt_mk_of_lt_of_le (h₁ : a₁ < a₂) (h₂ : b₁ ≤ b₂) : (a₁, b₁) < (a₂, b₂) := by simp [lt_iff, *] lemma mk_lt_mk_of_le_of_lt (h₁ : a₁ ≤ a₂) (h₂ : b₁ < b₂) : (a₁, b₁) < (a₂, b₂) := by simp [lt_iff, *] end Preorder /-- The pointwise partial order on a product. (The lexicographic ordering is defined in `Order.Lexicographic`, and the instances are available via the type synonym `α ×ₗ β = α × β`.) -/ instance instPartialOrder (α β : Type*) [PartialOrder α] [PartialOrder β] : PartialOrder (α × β) where __ := inferInstanceAs (Preorder (α × β)) le_antisymm := fun _ _ ⟨hac, hbd⟩ ⟨hca, hdb⟩ ↦ Prod.ext (hac.antisymm hca) (hbd.antisymm hdb) end Prod /-! ### Additional order classes -/ /-- An order is dense if there is an element between any pair of distinct comparable elements. -/ class DenselyOrdered (α : Type*) [LT α] : Prop where /-- An order is dense if there is an element between any pair of distinct elements. -/ dense : ∀ a₁ a₂ : α, a₁ < a₂ → ∃ a, a₁ < a ∧ a < a₂ theorem exists_between [LT α] [DenselyOrdered α] : ∀ {a₁ a₂ : α}, a₁ < a₂ → ∃ a, a₁ < a ∧ a < a₂ := DenselyOrdered.dense _ _ instance OrderDual.denselyOrdered (α : Type*) [LT α] [h : DenselyOrdered α] : DenselyOrdered αᵒᵈ := ⟨fun _ _ ha ↦ (@exists_between α _ h _ _ ha).imp fun _ ↦ And.symm⟩ @[simp] theorem denselyOrdered_orderDual [LT α] : DenselyOrdered αᵒᵈ ↔ DenselyOrdered α := ⟨by convert @OrderDual.denselyOrdered αᵒᵈ _, @OrderDual.denselyOrdered α _⟩ /-- Any ordered subsingleton is densely ordered. Not an instance to avoid a heavy subsingleton typeclass search. -/ lemma Subsingleton.instDenselyOrdered {X : Type*} [Subsingleton X] [Preorder X] : DenselyOrdered X := ⟨fun _ _ h ↦ (not_lt_of_subsingleton h).elim⟩ instance [Preorder α] [Preorder β] [DenselyOrdered α] [DenselyOrdered β] : DenselyOrdered (α × β) := ⟨fun a b ↦ by simp_rw [Prod.lt_iff] rintro (⟨h₁, h₂⟩ | ⟨h₁, h₂⟩) · obtain ⟨c, ha, hb⟩ := exists_between h₁ exact ⟨(c, _), Or.inl ⟨ha, h₂⟩, Or.inl ⟨hb, le_rfl⟩⟩ · obtain ⟨c, ha, hb⟩ := exists_between h₂ exact ⟨(_, c), Or.inr ⟨h₁, ha⟩, Or.inr ⟨le_rfl, hb⟩⟩⟩ instance [∀ i, Preorder (π i)] [∀ i, DenselyOrdered (π i)] : DenselyOrdered (∀ i, π i) := ⟨fun a b ↦ by classical simp_rw [Pi.lt_def] rintro ⟨hab, i, hi⟩ obtain ⟨c, ha, hb⟩ := exists_between hi exact ⟨Function.update a i c, ⟨le_update_iff.2 ⟨ha.le, fun _ _ ↦ le_rfl⟩, i, by rwa [update_self]⟩, update_le_iff.2 ⟨hb.le, fun _ _ ↦ hab _⟩, i, by rwa [update_self]⟩⟩ section LinearOrder variable [LinearOrder α] [DenselyOrdered α] {a₁ a₂ : α} theorem le_of_forall_gt_imp_ge_of_dense (h : ∀ a, a₂ < a → a₁ ≤ a) : a₁ ≤ a₂ := le_of_not_gt fun ha ↦ let ⟨a, ha₁, ha₂⟩ := exists_between ha lt_irrefl a <| lt_of_lt_of_le ‹a < a₁› (h _ ‹a₂ < a›) lemma forall_gt_imp_ge_iff_le_of_dense : (∀ a, a₂ < a → a₁ ≤ a) ↔ a₁ ≤ a₂ := ⟨le_of_forall_gt_imp_ge_of_dense, fun ha _a ha₂ ↦ ha.trans ha₂.le⟩ lemma eq_of_le_of_forall_lt_imp_le_of_dense (h₁ : a₂ ≤ a₁) (h₂ : ∀ a, a₂ < a → a₁ ≤ a) : a₁ = a₂ := le_antisymm (le_of_forall_gt_imp_ge_of_dense h₂) h₁ theorem le_of_forall_lt_imp_le_of_dense (h : ∀ a < a₁, a ≤ a₂) : a₁ ≤ a₂ := le_of_not_gt fun ha ↦ let ⟨a, ha₁, ha₂⟩ := exists_between ha lt_irrefl a <| lt_of_le_of_lt (h _ ‹a < a₁›) ‹a₂ < a› lemma forall_lt_imp_le_iff_le_of_dense : (∀ a < a₁, a ≤ a₂) ↔ a₁ ≤ a₂ := ⟨le_of_forall_lt_imp_le_of_dense, fun ha _a ha₁ ↦ ha₁.le.trans ha⟩ theorem eq_of_le_of_forall_gt_imp_ge_of_dense (h₁ : a₂ ≤ a₁) (h₂ : ∀ a < a₁, a ≤ a₂) : a₁ = a₂ := (le_of_forall_lt_imp_le_of_dense h₂).antisymm h₁ @[deprecated (since := "2025-01-21")] alias le_of_forall_le_of_dense := le_of_forall_gt_imp_ge_of_dense @[deprecated (since := "2025-01-21")] alias le_of_forall_ge_of_dense := le_of_forall_lt_imp_le_of_dense @[deprecated (since := "2025-01-21")] alias forall_lt_le_iff := forall_lt_imp_le_iff_le_of_dense @[deprecated (since := "2025-01-21")] alias forall_gt_ge_iff := forall_gt_imp_ge_iff_le_of_dense @[deprecated (since := "2025-01-21")] alias eq_of_le_of_forall_le_of_dense := eq_of_le_of_forall_lt_imp_le_of_dense @[deprecated (since := "2025-01-21")] alias eq_of_le_of_forall_ge_of_dense := eq_of_le_of_forall_gt_imp_ge_of_dense end LinearOrder theorem dense_or_discrete [LinearOrder α] (a₁ a₂ : α) : (∃ a, a₁ < a ∧ a < a₂) ∨ (∀ a, a₁ < a → a₂ ≤ a) ∧ ∀ a < a₂, a ≤ a₁ := or_iff_not_imp_left.2 fun h ↦ ⟨fun a ha₁ ↦ le_of_not_gt fun ha₂ ↦ h ⟨a, ha₁, ha₂⟩, fun a ha₂ ↦ le_of_not_gt fun ha₁ ↦ h ⟨a, ha₁, ha₂⟩⟩ /-- If a linear order has no elements `x < y < z`, then it has at most two elements. -/ lemma eq_or_eq_or_eq_of_forall_not_lt_lt [LinearOrder α] (h : ∀ ⦃x y z : α⦄, x < y → y < z → False) (x y z : α) : x = y ∨ y = z ∨ x = z := by by_contra hne simp only [not_or, ← Ne.eq_def] at hne rcases hne.1.lt_or_lt with h₁ | h₁ <;> rcases hne.2.1.lt_or_lt with h₂ | h₂ <;> rcases hne.2.2.lt_or_lt with h₃ | h₃ exacts [h h₁ h₂, h h₂ h₃, h h₃ h₂, h h₃ h₁, h h₁ h₃, h h₂ h₃, h h₁ h₃, h h₂ h₁] namespace PUnit variable (a b : PUnit) instance instLinearOrder : LinearOrder PUnit where le := fun _ _ ↦ True lt := fun _ _ ↦ False max := fun _ _ ↦ unit min := fun _ _ ↦ unit toDecidableEq := inferInstance toDecidableLE := fun _ _ ↦ Decidable.isTrue trivial toDecidableLT := fun _ _ ↦ Decidable.isFalse id le_refl := by intros; trivial le_trans := by intros; trivial le_total := by intros; exact Or.inl trivial le_antisymm := by intros; rfl lt_iff_le_not_le := by simp only [not_true, and_false, forall_const] theorem max_eq : max a b = unit := rfl theorem min_eq : min a b = unit := rfl protected theorem le : a ≤ b := trivial theorem not_lt : ¬a < b := not_false instance : DenselyOrdered PUnit := ⟨fun _ _ ↦ False.elim⟩ end PUnit section «Prop» /-- Propositions form a complete boolean algebra, where the `≤` relation is given by implication. -/ instance Prop.le : LE Prop := ⟨(· → ·)⟩ @[simp] theorem le_Prop_eq : ((· ≤ ·) : Prop → Prop → Prop) = (· → ·) := rfl theorem subrelation_iff_le {r s : α → α → Prop} : Subrelation r s ↔ r ≤ s := Iff.rfl instance Prop.partialOrder : PartialOrder Prop where __ := Prop.le le_refl _ := id le_trans _ _ _ f g := g ∘ f le_antisymm _ _ Hab Hba := propext ⟨Hab, Hba⟩ end «Prop» /-! ### Linear order from a total partial order -/ /-- Type synonym to create an instance of `LinearOrder` from a `PartialOrder` and `IsTotal α (≤)` -/ def AsLinearOrder (α : Type*) := α instance [Inhabited α] : Inhabited (AsLinearOrder α) := ⟨(default : α)⟩ noncomputable instance AsLinearOrder.linearOrder [PartialOrder α] [IsTotal α (· ≤ ·)] : LinearOrder (AsLinearOrder α) where __ := inferInstanceAs (PartialOrder α) le_total := @total_of α (· ≤ ·) _ toDecidableLE := Classical.decRel _
Mathlib/Order/Basic.lean
1,544
1,546
/- 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, Yury Kudryashov -/ import Mathlib.Order.Filter.Tendsto import Mathlib.Data.Set.Accumulate import Mathlib.Topology.Bornology.Basic import Mathlib.Topology.ContinuousOn import Mathlib.Topology.Ultrafilter import Mathlib.Topology.Defs.Ultrafilter /-! # Compact sets and compact spaces ## Main results * `isCompact_univ_pi`: **Tychonov's theorem** - an arbitrary product of compact sets is compact. -/ open Set Filter Topology TopologicalSpace Function universe u v variable {X : Type u} {Y : Type v} {ι : Type*} variable [TopologicalSpace X] [TopologicalSpace Y] {s t : Set X} {f : X → Y} -- compact sets section Compact lemma IsCompact.exists_clusterPt (hs : IsCompact s) {f : Filter X} [NeBot f] (hf : f ≤ 𝓟 s) : ∃ x ∈ s, ClusterPt x f := hs hf lemma IsCompact.exists_mapClusterPt {ι : Type*} (hs : IsCompact s) {f : Filter ι} [NeBot f] {u : ι → X} (hf : Filter.map u f ≤ 𝓟 s) : ∃ x ∈ s, MapClusterPt x f u := hs hf lemma IsCompact.exists_clusterPt_of_frequently {l : Filter X} (hs : IsCompact s) (hl : ∃ᶠ x in l, x ∈ s) : ∃ a ∈ s, ClusterPt a l := let ⟨a, has, ha⟩ := @hs _ (frequently_mem_iff_neBot.mp hl) inf_le_right ⟨a, has, ha.mono inf_le_left⟩ lemma IsCompact.exists_mapClusterPt_of_frequently {l : Filter ι} {f : ι → X} (hs : IsCompact s) (hf : ∃ᶠ x in l, f x ∈ s) : ∃ a ∈ s, MapClusterPt a l f := hs.exists_clusterPt_of_frequently hf /-- The complement to a compact set belongs to a filter `f` if it belongs to each filter `𝓝 x ⊓ f`, `x ∈ s`. -/ theorem IsCompact.compl_mem_sets (hs : IsCompact s) {f : Filter X} (hf : ∀ x ∈ s, sᶜ ∈ 𝓝 x ⊓ f) : sᶜ ∈ f := by contrapose! hf simp only [not_mem_iff_inf_principal_compl, compl_compl, inf_assoc] at hf ⊢ exact @hs _ hf inf_le_right /-- The complement to a compact set belongs to a filter `f` if each `x ∈ s` has a neighborhood `t` within `s` such that `tᶜ` belongs to `f`. -/ theorem IsCompact.compl_mem_sets_of_nhdsWithin (hs : IsCompact s) {f : Filter X} (hf : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, tᶜ ∈ f) : sᶜ ∈ f := by refine hs.compl_mem_sets fun x hx => ?_ rcases hf x hx with ⟨t, ht, hst⟩ replace ht := mem_inf_principal.1 ht apply mem_inf_of_inter ht hst rintro x ⟨h₁, h₂⟩ hs exact h₂ (h₁ hs) /-- If `p : Set X → Prop` is stable under restriction and union, and each point `x` of a compact set `s` has a neighborhood `t` within `s` such that `p t`, then `p s` holds. -/ @[elab_as_elim] theorem IsCompact.induction_on (hs : IsCompact s) {p : Set X → Prop} (he : p ∅) (hmono : ∀ ⦃s t⦄, s ⊆ t → p t → p s) (hunion : ∀ ⦃s t⦄, p s → p t → p (s ∪ t)) (hnhds : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, p t) : p s := by let f : Filter X := comk p he (fun _t ht _s hsub ↦ hmono hsub ht) (fun _s hs _t ht ↦ hunion hs ht) have : sᶜ ∈ f := hs.compl_mem_sets_of_nhdsWithin (by simpa [f] using hnhds) rwa [← compl_compl s] /-- The intersection of a compact set and a closed set is a compact set. -/ theorem IsCompact.inter_right (hs : IsCompact s) (ht : IsClosed t) : IsCompact (s ∩ t) := by intro f hnf hstf obtain ⟨x, hsx, hx⟩ : ∃ x ∈ s, ClusterPt x f := hs (le_trans hstf (le_principal_iff.2 inter_subset_left)) have : x ∈ t := ht.mem_of_nhdsWithin_neBot <| hx.mono <| le_trans hstf (le_principal_iff.2 inter_subset_right) exact ⟨x, ⟨hsx, this⟩, hx⟩ /-- The intersection of a closed set and a compact set is a compact set. -/ theorem IsCompact.inter_left (ht : IsCompact t) (hs : IsClosed s) : IsCompact (s ∩ t) := inter_comm t s ▸ ht.inter_right hs /-- The set difference of a compact set and an open set is a compact set. -/ theorem IsCompact.diff (hs : IsCompact s) (ht : IsOpen t) : IsCompact (s \ t) := hs.inter_right (isClosed_compl_iff.mpr ht) /-- A closed subset of a compact set is a compact set. -/ theorem IsCompact.of_isClosed_subset (hs : IsCompact s) (ht : IsClosed t) (h : t ⊆ s) : IsCompact t := inter_eq_self_of_subset_right h ▸ hs.inter_right ht theorem IsCompact.image_of_continuousOn {f : X → Y} (hs : IsCompact s) (hf : ContinuousOn f s) : IsCompact (f '' s) := by intro l lne ls have : NeBot (l.comap f ⊓ 𝓟 s) := comap_inf_principal_neBot_of_image_mem lne (le_principal_iff.1 ls) obtain ⟨x, hxs, hx⟩ : ∃ x ∈ s, ClusterPt x (l.comap f ⊓ 𝓟 s) := @hs _ this inf_le_right haveI := hx.neBot use f x, mem_image_of_mem f hxs have : Tendsto f (𝓝 x ⊓ (comap f l ⊓ 𝓟 s)) (𝓝 (f x) ⊓ l) := by convert (hf x hxs).inf (@tendsto_comap _ _ f l) using 1 rw [nhdsWithin] ac_rfl exact this.neBot theorem IsCompact.image {f : X → Y} (hs : IsCompact s) (hf : Continuous f) : IsCompact (f '' s) := hs.image_of_continuousOn hf.continuousOn theorem IsCompact.adherence_nhdset {f : Filter X} (hs : IsCompact s) (hf₂ : f ≤ 𝓟 s) (ht₁ : IsOpen t) (ht₂ : ∀ x ∈ s, ClusterPt x f → x ∈ t) : t ∈ f := Classical.by_cases mem_of_eq_bot fun (this : f ⊓ 𝓟 tᶜ ≠ ⊥) => let ⟨x, hx, (hfx : ClusterPt x <| f ⊓ 𝓟 tᶜ)⟩ := @hs _ ⟨this⟩ <| inf_le_of_left_le hf₂ have : x ∈ t := ht₂ x hx hfx.of_inf_left have : tᶜ ∩ t ∈ 𝓝[tᶜ] x := inter_mem_nhdsWithin _ (IsOpen.mem_nhds ht₁ this) have A : 𝓝[tᶜ] x = ⊥ := empty_mem_iff_bot.1 <| compl_inter_self t ▸ this have : 𝓝[tᶜ] x ≠ ⊥ := hfx.of_inf_right.ne absurd A this theorem isCompact_iff_ultrafilter_le_nhds : IsCompact s ↔ ∀ f : Ultrafilter X, ↑f ≤ 𝓟 s → ∃ x ∈ s, ↑f ≤ 𝓝 x := by refine (forall_neBot_le_iff ?_).trans ?_ · rintro f g hle ⟨x, hxs, hxf⟩ exact ⟨x, hxs, hxf.mono hle⟩ · simp only [Ultrafilter.clusterPt_iff] alias ⟨IsCompact.ultrafilter_le_nhds, _⟩ := isCompact_iff_ultrafilter_le_nhds theorem isCompact_iff_ultrafilter_le_nhds' : IsCompact s ↔ ∀ f : Ultrafilter X, s ∈ f → ∃ x ∈ s, ↑f ≤ 𝓝 x := by simp only [isCompact_iff_ultrafilter_le_nhds, le_principal_iff, Ultrafilter.mem_coe] alias ⟨IsCompact.ultrafilter_le_nhds', _⟩ := isCompact_iff_ultrafilter_le_nhds' /-- If a compact set belongs to a filter and this filter has a unique cluster point `y` in this set, then the filter is less than or equal to `𝓝 y`. -/ lemma IsCompact.le_nhds_of_unique_clusterPt (hs : IsCompact s) {l : Filter X} {y : X} (hmem : s ∈ l) (h : ∀ x ∈ s, ClusterPt x l → x = y) : l ≤ 𝓝 y := by refine le_iff_ultrafilter.2 fun f hf ↦ ?_ rcases hs.ultrafilter_le_nhds' f (hf hmem) with ⟨x, hxs, hx⟩ convert ← hx exact h x hxs (.mono (.of_le_nhds hx) hf) /-- If values of `f : Y → X` belong to a compact set `s` eventually along a filter `l` and `y` is a unique `MapClusterPt` for `f` along `l` in `s`, then `f` tends to `𝓝 y` along `l`. -/ lemma IsCompact.tendsto_nhds_of_unique_mapClusterPt {Y} {l : Filter Y} {y : X} {f : Y → X} (hs : IsCompact s) (hmem : ∀ᶠ x in l, f x ∈ s) (h : ∀ x ∈ s, MapClusterPt x l f → x = y) : Tendsto f l (𝓝 y) := hs.le_nhds_of_unique_clusterPt (mem_map.2 hmem) h /-- For every open directed cover of a compact set, there exists a single element of the cover which itself includes the set. -/ theorem IsCompact.elim_directed_cover {ι : Type v} [hι : Nonempty ι] (hs : IsCompact s) (U : ι → Set X) (hUo : ∀ i, IsOpen (U i)) (hsU : s ⊆ ⋃ i, U i) (hdU : Directed (· ⊆ ·) U) : ∃ i, s ⊆ U i := hι.elim fun i₀ => IsCompact.induction_on hs ⟨i₀, empty_subset _⟩ (fun _ _ hs ⟨i, hi⟩ => ⟨i, hs.trans hi⟩) (fun _ _ ⟨i, hi⟩ ⟨j, hj⟩ => let ⟨k, hki, hkj⟩ := hdU i j ⟨k, union_subset (Subset.trans hi hki) (Subset.trans hj hkj)⟩) fun _x hx => let ⟨i, hi⟩ := mem_iUnion.1 (hsU hx) ⟨U i, mem_nhdsWithin_of_mem_nhds (IsOpen.mem_nhds (hUo i) hi), i, Subset.refl _⟩ /-- For every open cover of a compact set, there exists a finite subcover. -/ theorem IsCompact.elim_finite_subcover {ι : Type v} (hs : IsCompact s) (U : ι → Set X) (hUo : ∀ i, IsOpen (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : Finset ι, s ⊆ ⋃ i ∈ t, U i := hs.elim_directed_cover _ (fun _ => isOpen_biUnion fun i _ => hUo i) (iUnion_eq_iUnion_finset U ▸ hsU) (directed_of_isDirected_le fun _ _ h => biUnion_subset_biUnion_left h) lemma IsCompact.elim_nhds_subcover_nhdsSet' (hs : IsCompact s) (U : ∀ x ∈ s, Set X) (hU : ∀ x hx, U x hx ∈ 𝓝 x) : ∃ t : Finset s, (⋃ x ∈ t, U x.1 x.2) ∈ 𝓝ˢ s := by rcases hs.elim_finite_subcover (fun x : s ↦ interior (U x x.2)) (fun _ ↦ isOpen_interior) fun x hx ↦ mem_iUnion.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 <| hU _ _⟩ with ⟨t, hst⟩ refine ⟨t, mem_nhdsSet_iff_forall.2 fun x hx ↦ ?_⟩ rcases mem_iUnion₂.1 (hst hx) with ⟨y, hyt, hy⟩ refine mem_of_superset ?_ (subset_biUnion_of_mem hyt) exact mem_interior_iff_mem_nhds.1 hy lemma IsCompact.elim_nhds_subcover_nhdsSet (hs : IsCompact s) {U : X → Set X} (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ t : Finset X, (∀ x ∈ t, x ∈ s) ∧ (⋃ x ∈ t, U x) ∈ 𝓝ˢ s := by let ⟨t, ht⟩ := hs.elim_nhds_subcover_nhdsSet' (fun x _ => U x) hU classical exact ⟨t.image (↑), fun x hx => let ⟨y, _, hyx⟩ := Finset.mem_image.1 hx hyx ▸ y.2, by rwa [Finset.set_biUnion_finset_image]⟩ theorem IsCompact.elim_nhds_subcover' (hs : IsCompact s) (U : ∀ x ∈ s, Set X) (hU : ∀ x (hx : x ∈ s), U x ‹x ∈ s› ∈ 𝓝 x) : ∃ t : Finset s, s ⊆ ⋃ x ∈ t, U (x : s) x.2 := (hs.elim_nhds_subcover_nhdsSet' U hU).imp fun _ ↦ subset_of_mem_nhdsSet theorem IsCompact.elim_nhds_subcover (hs : IsCompact s) (U : X → Set X) (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ t : Finset X, (∀ x ∈ t, x ∈ s) ∧ s ⊆ ⋃ x ∈ t, U x := (hs.elim_nhds_subcover_nhdsSet hU).imp fun _ h ↦ h.imp_right subset_of_mem_nhdsSet theorem IsCompact.elim_nhdsWithin_subcover' (hs : IsCompact s) (U : ∀ x ∈ s, Set X) (hU : ∀ x (hx : x ∈ s), U x hx ∈ 𝓝[s] x) : ∃ t : Finset s, s ⊆ ⋃ x ∈ t, U x x.2 := by choose V V_nhds hV using fun x hx => mem_nhdsWithin_iff_exists_mem_nhds_inter.1 (hU x hx) refine (hs.elim_nhds_subcover' V V_nhds).imp fun t ht => subset_trans ?_ (iUnion₂_mono fun x _ => hV x x.2) simpa [← iUnion_inter, ← iUnion_coe_set] theorem IsCompact.elim_nhdsWithin_subcover (hs : IsCompact s) (U : X → Set X) (hU : ∀ x ∈ s, U x ∈ 𝓝[s] x) : ∃ t : Finset X, (∀ x ∈ t, x ∈ s) ∧ s ⊆ ⋃ x ∈ t, U x := by choose! V V_nhds hV using fun x hx => mem_nhdsWithin_iff_exists_mem_nhds_inter.1 (hU x hx) refine (hs.elim_nhds_subcover V V_nhds).imp fun t ⟨t_sub_s, ht⟩ => ⟨t_sub_s, subset_trans ?_ (iUnion₂_mono fun x hx => hV x (t_sub_s x hx))⟩ simpa [← iUnion_inter] /-- The neighborhood filter of a compact set is disjoint with a filter `l` if and only if the neighborhood filter of each point of this set is disjoint with `l`. -/ theorem IsCompact.disjoint_nhdsSet_left {l : Filter X} (hs : IsCompact s) : Disjoint (𝓝ˢ s) l ↔ ∀ x ∈ s, Disjoint (𝓝 x) l := by refine ⟨fun h x hx => h.mono_left <| nhds_le_nhdsSet hx, fun H => ?_⟩ choose! U hxU hUl using fun x hx => (nhds_basis_opens x).disjoint_iff_left.1 (H x hx) choose hxU hUo using hxU rcases hs.elim_nhds_subcover U fun x hx => (hUo x hx).mem_nhds (hxU x hx) with ⟨t, hts, hst⟩ refine (hasBasis_nhdsSet _).disjoint_iff_left.2 ⟨⋃ x ∈ t, U x, ⟨isOpen_biUnion fun x hx => hUo x (hts x hx), hst⟩, ?_⟩ rw [compl_iUnion₂, biInter_finset_mem] exact fun x hx => hUl x (hts x hx) /-- A filter `l` is disjoint with the neighborhood filter of a compact set if and only if it is disjoint with the neighborhood filter of each point of this set. -/ theorem IsCompact.disjoint_nhdsSet_right {l : Filter X} (hs : IsCompact s) : Disjoint l (𝓝ˢ s) ↔ ∀ x ∈ s, Disjoint l (𝓝 x) := by simpa only [disjoint_comm] using hs.disjoint_nhdsSet_left -- TODO: reformulate using `Disjoint` /-- For every directed family of closed sets whose intersection avoids a compact set, there exists a single element of the family which itself avoids this compact set. -/ theorem IsCompact.elim_directed_family_closed {ι : Type v} [Nonempty ι] (hs : IsCompact s) (t : ι → Set X) (htc : ∀ i, IsClosed (t i)) (hst : (s ∩ ⋂ i, t i) = ∅) (hdt : Directed (· ⊇ ·) t) : ∃ i : ι, s ∩ t i = ∅ := let ⟨t, ht⟩ := hs.elim_directed_cover (compl ∘ t) (fun i => (htc i).isOpen_compl) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_iUnion, exists_prop, mem_inter_iff, not_and, mem_iInter, mem_compl_iff] using hst) (hdt.mono_comp _ fun _ _ => compl_subset_compl.mpr) ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_iUnion, exists_prop, mem_inter_iff, not_and, mem_iInter, mem_compl_iff] using ht⟩ -- TODO: reformulate using `Disjoint` /-- For every family of closed sets whose intersection avoids a compact set, there exists a finite subfamily whose intersection avoids this compact set. -/ theorem IsCompact.elim_finite_subfamily_closed {ι : Type v} (hs : IsCompact s) (t : ι → Set X) (htc : ∀ i, IsClosed (t i)) (hst : (s ∩ ⋂ i, t i) = ∅) :
∃ u : Finset ι, (s ∩ ⋂ i ∈ u, t i) = ∅ := hs.elim_directed_family_closed _ (fun _ ↦ isClosed_biInter fun _ _ ↦ htc _) (by rwa [← iInter_eq_iInter_finset]) (directed_of_isDirected_le fun _ _ h ↦ biInter_subset_biInter_left h) /-- To show that a compact set intersects the intersection of a family of closed sets,
Mathlib/Topology/Compactness/Compact.lean
259
264
/- Copyright (c) 2023 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Analysis.InnerProductSpace.Projection import Mathlib.Dynamics.BirkhoffSum.NormedSpace /-! # Von Neumann Mean Ergodic Theorem in a Hilbert Space In this file we prove the von Neumann Mean Ergodic Theorem for an operator in a Hilbert space. It says that for a contracting linear self-map `f : E →ₗ[𝕜] E` of a Hilbert space, the Birkhoff averages ``` birkhoffAverage 𝕜 f id N x = (N : 𝕜)⁻¹ • ∑ n ∈ Finset.range N, f^[n] x ``` converge to the orthogonal projection of `x` to the subspace of fixed points of `f`, see `ContinuousLinearMap.tendsto_birkhoffAverage_orthogonalProjection`. -/ open Filter Finset Function Bornology open scoped Topology variable {𝕜 E : Type*} [RCLike 𝕜] [NormedAddCommGroup E] /-- **Von Neumann Mean Ergodic Theorem**, a version for a normed space. Let `f : E → E` be a contracting linear self-map of a normed space. Let `S` be the subspace of fixed points of `f`. Let `g : E → S` be a continuous linear projection, `g|_S=id`. If the range of `f - id` is dense in the kernel of `g`, then for each `x`, the Birkhoff averages ``` birkhoffAverage 𝕜 f id N x = (N : 𝕜)⁻¹ • ∑ n ∈ Finset.range N, f^[n] x ``` converge to `g x` as `N → ∞`. Usually, this fact is not formulated as a separate lemma. I chose to do it in order to isolate parts of the proof that do not rely on the inner product space structure. -/
theorem LinearMap.tendsto_birkhoffAverage_of_ker_subset_closure [NormedSpace 𝕜 E] (f : E →ₗ[𝕜] E) (hf : LipschitzWith 1 f) (g : E →L[𝕜] LinearMap.eqLocus f 1) (hg_proj : ∀ x : LinearMap.eqLocus f 1, g x = x) (hg_ker : (LinearMap.ker g : Set E) ⊆ closure (LinearMap.range (f - 1))) (x : E) : Tendsto (birkhoffAverage 𝕜 f _root_.id · x) atTop (𝓝 (g x)) := by /- Any point can be represented as a sum of `y ∈ LinearMap.ker g` and a fixed point `z`. -/ obtain ⟨y, hy, z, hz, rfl⟩ : ∃ y, g y = 0 ∧ ∃ z, IsFixedPt f z ∧ x = y + z := ⟨x - g x, by simp [hg_proj], g x, (g x).2, by simp⟩ /- For a fixed point, the theorem is trivial, so it suffices to prove it for `y ∈ LinearMap.ker g`. -/ suffices Tendsto (birkhoffAverage 𝕜 f _root_.id · y) atTop (𝓝 0) by have hgz : g z = z := congr_arg Subtype.val (hg_proj ⟨z, hz⟩) simpa [hy, hgz, birkhoffAverage, birkhoffSum, Finset.sum_add_distrib, smul_add] using this.add (hz.tendsto_birkhoffAverage 𝕜 _root_.id) /- By continuity, it suffices to prove the theorem on a dense subset of `LinearMap.ker g`. By assumption, `LinearMap.range (f - 1)` is dense in the kernel of `g`, so it suffices to prove the theorem for `y = f x - x`. -/ have : IsClosed {x | Tendsto (birkhoffAverage 𝕜 f _root_.id · x) atTop (𝓝 0)} := isClosed_setOf_tendsto_birkhoffAverage 𝕜 hf uniformContinuous_id continuous_const refine closure_minimal (Set.forall_mem_range.2 fun x ↦ ?_) this (hg_ker hy) /- Finally, for `y = f x - x` the average is equal to the difference between averages along the orbits of `f x` and `x`, and most of the terms cancel. -/ have : IsBounded (Set.range (_root_.id <| f^[·] x)) := isBounded_iff_forall_norm_le.2 ⟨‖x‖, Set.forall_mem_range.2 fun n ↦ by have H : f^[n] 0 = 0 := iterate_map_zero (f : E →+ E) n simpa [H] using (hf.iterate n).dist_le_mul x 0⟩ have H : ∀ n x y, f^[n] (x - y) = f^[n] x - f^[n] y := iterate_map_sub (f : E →+ E) simpa [birkhoffAverage, birkhoffSum, Finset.sum_sub_distrib, smul_sub, H] using tendsto_birkhoffAverage_apply_sub_birkhoffAverage 𝕜 this
Mathlib/Analysis/InnerProductSpace/MeanErgodic.lean
43
71
/- 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.Algebra.CharP.Basic import Mathlib.Algebra.CharP.Lemmas import Mathlib.Data.Fintype.Units import Mathlib.GroupTheory.OrderOfElement /-! # Multiplicative characters of finite rings and fields Let `R` and `R'` be a commutative rings. A *multiplicative character* of `R` with values in `R'` is a morphism of monoids from the multiplicative monoid of `R` into that of `R'` that sends non-units to zero. We use the namespace `MulChar` for the definitions and results. ## Main results We show that the multiplicative characters form a group (if `R'` is commutative); see `MulChar.commGroup`. We also provide an equivalence with the homomorphisms `Rˣ →* R'ˣ`; see `MulChar.equivToUnitHom`. We define a multiplicative character to be *quadratic* if its values are among `0`, `1` and `-1`, and we prove some properties of quadratic characters. Finally, we show that the sum of all values of a nontrivial multiplicative character vanishes; see `MulChar.IsNontrivial.sum_eq_zero`. ## Tags multiplicative character -/ /-! ### Definitions related to multiplicative characters Even though the intended use is when domain and target of the characters are commutative rings, we define them in the more general setting when the domain is a commutative monoid and the target is a commutative monoid with zero. (We need a zero in the target, since non-units are supposed to map to zero.) In this setting, there is an equivalence between multiplicative characters `R → R'` and group homomorphisms `Rˣ → R'ˣ`, and the multiplicative characters have a natural structure as a commutative group. -/ section Defi -- The domain of our multiplicative characters variable (R : Type*) [CommMonoid R] -- The target variable (R' : Type*) [CommMonoidWithZero R'] /-- Define a structure for multiplicative characters. A multiplicative character from a commutative monoid `R` to a commutative monoid with zero `R'` is a homomorphism of (multiplicative) monoids that sends non-units to zero. -/ structure MulChar extends MonoidHom R R' where map_nonunit' : ∀ a : R, ¬IsUnit a → toFun a = 0 instance MulChar.instFunLike : FunLike (MulChar R R') R R' := ⟨fun χ => χ.toFun, fun χ₀ χ₁ h => by cases χ₀; cases χ₁; congr; apply MonoidHom.ext (fun _ => congr_fun h _)⟩ /-- This is the corresponding extension of `MonoidHomClass`. -/ class MulCharClass (F : Type*) (R R' : outParam Type*) [CommMonoid R] [CommMonoidWithZero R'] [FunLike F R R'] : Prop extends MonoidHomClass F R R' where map_nonunit : ∀ (χ : F) {a : R} (_ : ¬IsUnit a), χ a = 0 initialize_simps_projections MulChar (toFun → apply, -toMonoidHom) end Defi namespace MulChar attribute [scoped simp] MulCharClass.map_nonunit section Group -- The domain of our multiplicative characters variable {R : Type*} [CommMonoid R] -- The target variable {R' : Type*} [CommMonoidWithZero R'] variable (R R') in /-- The trivial multiplicative character. It takes the value `0` on non-units and the value `1` on units. -/ @[simps] noncomputable def trivial : MulChar R R' where toFun := by classical exact fun x => if IsUnit x then 1 else 0 map_nonunit' := by intro a ha simp only [ha, if_false] map_one' := by simp only [isUnit_one, if_true] map_mul' := by intro x y classical simp only [IsUnit.mul_iff, boole_mul] split_ifs <;> tauto @[simp] theorem coe_mk (f : R →* R') (hf) : (MulChar.mk f hf : R → R') = f := rfl /-- Extensionality. See `ext` below for the version that will actually be used. -/ theorem ext' {χ χ' : MulChar R R'} (h : ∀ a, χ a = χ' a) : χ = χ' := by cases χ cases χ' congr exact MonoidHom.ext h instance : MulCharClass (MulChar R R') R R' where map_mul χ := χ.map_mul' map_one χ := χ.map_one' map_nonunit χ := χ.map_nonunit' _ theorem map_nonunit (χ : MulChar R R') {a : R} (ha : ¬IsUnit a) : χ a = 0 := χ.map_nonunit' a ha /-- Extensionality. Since `MulChar`s always take the value zero on non-units, it is sufficient to compare the values on units. -/ @[ext] theorem ext {χ χ' : MulChar R R'} (h : ∀ a : Rˣ, χ a = χ' a) : χ = χ' := by apply ext' intro a by_cases ha : IsUnit a · exact h ha.unit · rw [map_nonunit χ ha, map_nonunit χ' ha] /-! ### Equivalence of multiplicative characters with homomorphisms on units We show that restriction / extension by zero gives an equivalence between `MulChar R R'` and `Rˣ →* R'ˣ`. -/ /-- Turn a `MulChar` into a homomorphism between the unit groups. -/ def toUnitHom (χ : MulChar R R') : Rˣ →* R'ˣ := Units.map χ theorem coe_toUnitHom (χ : MulChar R R') (a : Rˣ) : ↑(χ.toUnitHom a) = χ a := rfl /-- Turn a homomorphism between unit groups into a `MulChar`. -/ noncomputable def ofUnitHom (f : Rˣ →* R'ˣ) : MulChar R R' where toFun := by classical exact fun x => if hx : IsUnit x then f hx.unit else 0 map_one' := by have h1 : (isUnit_one.unit : Rˣ) = 1 := Units.eq_iff.mp rfl simp only [h1, dif_pos, Units.val_eq_one, map_one, isUnit_one] map_mul' := by classical intro x y by_cases hx : IsUnit x · simp only [hx, IsUnit.mul_iff, true_and, dif_pos] by_cases hy : IsUnit y · simp only [hy, dif_pos] have hm : (IsUnit.mul_iff.mpr ⟨hx, hy⟩).unit = hx.unit * hy.unit := Units.eq_iff.mp rfl rw [hm, map_mul] norm_cast · simp only [hy, not_false_iff, dif_neg, mul_zero] · simp only [hx, IsUnit.mul_iff, false_and, not_false_iff, dif_neg, zero_mul] map_nonunit' := by intro a ha simp only [ha, not_false_iff, dif_neg] theorem ofUnitHom_coe (f : Rˣ →* R'ˣ) (a : Rˣ) : ofUnitHom f ↑a = f a := by simp [ofUnitHom] /-- The equivalence between multiplicative characters and homomorphisms of unit groups. -/ noncomputable def equivToUnitHom : MulChar R R' ≃ (Rˣ →* R'ˣ) where toFun := toUnitHom invFun := ofUnitHom left_inv := by intro χ ext x rw [ofUnitHom_coe, coe_toUnitHom] right_inv := by intro f ext x simp only [coe_toUnitHom, ofUnitHom_coe] @[simp] theorem toUnitHom_eq (χ : MulChar R R') : toUnitHom χ = equivToUnitHom χ := rfl @[simp] theorem ofUnitHom_eq (χ : Rˣ →* R'ˣ) : ofUnitHom χ = equivToUnitHom.symm χ := rfl @[simp] theorem coe_equivToUnitHom (χ : MulChar R R') (a : Rˣ) : ↑(equivToUnitHom χ a) = χ a := coe_toUnitHom χ a @[simp] theorem equivToUnitHom_symm_coe (f : Rˣ →* R'ˣ) (a : Rˣ) : equivToUnitHom.symm f ↑a = f a := ofUnitHom_coe f a @[simp] lemma coe_toMonoidHom (χ : MulChar R R') (x : R) : χ.toMonoidHom x = χ x := rfl /-! ### Commutative group structure on multiplicative characters The multiplicative characters `R → R'` form a commutative group. -/ protected theorem map_one (χ : MulChar R R') : χ (1 : R) = 1 := χ.map_one' /-- If the domain has a zero (and is nontrivial), then `χ 0 = 0`. -/ protected theorem map_zero {R : Type*} [CommMonoidWithZero R] [Nontrivial R] (χ : MulChar R R') : χ (0 : R) = 0 := by rw [map_nonunit χ not_isUnit_zero] /-- We can convert a multiplicative character into a homomorphism of monoids with zero when the source has a zero and another element. -/ @[coe, simps] def toMonoidWithZeroHom {R : Type*} [CommMonoidWithZero R] [Nontrivial R] (χ : MulChar R R') : R →*₀ R' where toFun := χ.toFun map_zero' := χ.map_zero map_one' := χ.map_one' map_mul' := χ.map_mul' /-- If the domain is a ring `R`, then `χ (ringChar R) = 0`. -/ theorem map_ringChar {R : Type*} [CommSemiring R] [Nontrivial R] (χ : MulChar R R') : χ (ringChar R) = 0 := by rw [ringChar.Nat.cast_ringChar, χ.map_zero] noncomputable instance hasOne : One (MulChar R R') := ⟨trivial R R'⟩ noncomputable instance inhabited : Inhabited (MulChar R R') := ⟨1⟩ /-- Evaluation of the trivial character -/ @[simp] theorem one_apply_coe (a : Rˣ) : (1 : MulChar R R') a = 1 := by classical exact dif_pos a.isUnit /-- Evaluation of the trivial character -/ lemma one_apply {x : R} (hx : IsUnit x) : (1 : MulChar R R') x = 1 := one_apply_coe hx.unit /-- Multiplication of multiplicative characters. (This needs the target to be commutative.) -/ def mul (χ χ' : MulChar R R') : MulChar R R' := { χ.toMonoidHom * χ'.toMonoidHom with toFun := χ * χ' map_nonunit' := fun a ha => by simp only [map_nonunit χ ha, zero_mul, Pi.mul_apply] } instance hasMul : Mul (MulChar R R') := ⟨mul⟩ theorem mul_apply (χ χ' : MulChar R R') (a : R) : (χ * χ') a = χ a * χ' a := rfl @[simp] theorem coeToFun_mul (χ χ' : MulChar R R') : ⇑(χ * χ') = χ * χ' := rfl protected theorem one_mul (χ : MulChar R R') : (1 : MulChar R R') * χ = χ := by ext simp only [one_mul, Pi.mul_apply, MulChar.coeToFun_mul, MulChar.one_apply_coe] protected theorem mul_one (χ : MulChar R R') : χ * 1 = χ := by ext simp only [mul_one, Pi.mul_apply, MulChar.coeToFun_mul, MulChar.one_apply_coe] /-- The inverse of a multiplicative character. We define it as `inverse ∘ χ`. -/ noncomputable def inv (χ : MulChar R R') : MulChar R R' := { MonoidWithZero.inverse.toMonoidHom.comp χ.toMonoidHom with toFun := fun a => MonoidWithZero.inverse (χ a) map_nonunit' := fun a ha => by simp [map_nonunit _ ha] } noncomputable instance hasInv : Inv (MulChar R R') := ⟨inv⟩ /-- The inverse of a multiplicative character `χ`, applied to `a`, is the inverse of `χ a`. -/ theorem inv_apply_eq_inv (χ : MulChar R R') (a : R) : χ⁻¹ a = Ring.inverse (χ a) := Eq.refl <| inv χ a /-- The inverse of a multiplicative character `χ`, applied to `a`, is the inverse of `χ a`. Variant when the target is a field -/ theorem inv_apply_eq_inv' {R' : Type*} [CommGroupWithZero R'] (χ : MulChar R R') (a : R) : χ⁻¹ a = (χ a)⁻¹ := (inv_apply_eq_inv χ a).trans <| Ring.inverse_eq_inv (χ a) /-- When the domain has a zero, then the inverse of a multiplicative character `χ`, applied to `a`, is `χ` applied to the inverse of `a`. -/ theorem inv_apply {R : Type*} [CommMonoidWithZero R] (χ : MulChar R R') (a : R) : χ⁻¹ a = χ (Ring.inverse a) := by by_cases ha : IsUnit a · rw [inv_apply_eq_inv] have h := IsUnit.map χ ha apply_fun (χ a * ·) using IsUnit.mul_right_injective h dsimp only rw [Ring.mul_inverse_cancel _ h, ← map_mul, Ring.mul_inverse_cancel _ ha, map_one] · revert ha nontriviality R intro ha -- `nontriviality R` by itself doesn't do it rw [map_nonunit _ ha, Ring.inverse_non_unit a ha, MulChar.map_zero χ] /-- When the domain has a zero, then the inverse of a multiplicative character `χ`, applied to `a`, is `χ` applied to the inverse of `a`. -/ theorem inv_apply' {R : Type*} [CommGroupWithZero R] (χ : MulChar R R') (a : R) : χ⁻¹ a = χ a⁻¹ := (inv_apply χ a).trans <| congr_arg _ (Ring.inverse_eq_inv a) /-- The product of a character with its inverse is the trivial character. -/ theorem inv_mul (χ : MulChar R R') : χ⁻¹ * χ = 1 := by ext x rw [coeToFun_mul, Pi.mul_apply, inv_apply_eq_inv] simp only [Ring.inverse_mul_cancel _ (IsUnit.map χ x.isUnit)] rw [one_apply_coe] /-- The commutative group structure on `MulChar R R'`. -/ noncomputable instance commGroup : CommGroup (MulChar R R') := { one := 1 mul := (· * ·) inv := Inv.inv inv_mul_cancel := inv_mul mul_assoc := by intro χ₁ χ₂ χ₃ ext a simp only [mul_assoc, Pi.mul_apply, MulChar.coeToFun_mul] mul_comm := by intro χ₁ χ₂ ext a simp only [mul_comm, Pi.mul_apply, MulChar.coeToFun_mul] one_mul := MulChar.one_mul mul_one := MulChar.mul_one } /-- If `a` is a unit and `n : ℕ`, then `(χ ^ n) a = (χ a) ^ n`. -/ theorem pow_apply_coe (χ : MulChar R R') (n : ℕ) (a : Rˣ) : (χ ^ n) a = χ a ^ n := by induction n with | zero => rw [pow_zero, pow_zero, one_apply_coe] | succ n ih => rw [pow_succ, pow_succ, mul_apply, ih] /-- If `n` is positive, then `(χ ^ n) a = (χ a) ^ n`. -/ theorem pow_apply' (χ : MulChar R R') {n : ℕ} (hn : n ≠ 0) (a : R) : (χ ^ n) a = χ a ^ n := by by_cases ha : IsUnit a · exact pow_apply_coe χ n ha.unit · rw [map_nonunit (χ ^ n) ha, map_nonunit χ ha, zero_pow hn] lemma equivToUnitHom_mul_apply (χ₁ χ₂ : MulChar R R') (a : Rˣ) : equivToUnitHom (χ₁ * χ₂) a = equivToUnitHom χ₁ a * equivToUnitHom χ₂ a := by apply_fun ((↑) : R'ˣ → R') using Units.ext push_cast simp_rw [coe_equivToUnitHom, coeToFun_mul, Pi.mul_apply] /-- The equivalence between multiplicative characters and homomorphisms of unit groups as a multiplicative equivalence. -/ noncomputable def mulEquivToUnitHom : MulChar R R' ≃* (Rˣ →* R'ˣ) := { equivToUnitHom with map_mul' := by intro χ ψ ext simp only [Equiv.toFun_as_coe, coe_equivToUnitHom, coeToFun_mul, Pi.mul_apply, MonoidHom.mul_apply, Units.val_mul] } end Group /-! ### Properties of multiplicative characters We introduce the properties of being nontrivial or quadratic and prove some basic facts about them. We now (mostly) assume that the target is a commutative ring. -/ section Properties section nontrivial variable {R : Type*} [CommMonoid R] {R' : Type*} [CommMonoidWithZero R'] lemma eq_one_iff {χ : MulChar R R'} : χ = 1 ↔ ∀ a : Rˣ, χ a = 1 := by simp only [MulChar.ext_iff, one_apply_coe] lemma ne_one_iff {χ : MulChar R R'} : χ ≠ 1 ↔ ∃ a : Rˣ, χ a ≠ 1 := by simp only [Ne, eq_one_iff, not_forall] end nontrivial section quadratic_and_comp variable {R : Type*} [CommMonoid R] {R' : Type*} [CommRing R'] {R'' : Type*} [CommRing R''] /-- A multiplicative character is *quadratic* if it takes only the values `0`, `1`, `-1`. -/ def IsQuadratic (χ : MulChar R R') : Prop := ∀ a, χ a = 0 ∨ χ a = 1 ∨ χ a = -1 /-- If two values of quadratic characters with target `ℤ` agree after coercion into a ring of characteristic not `2`, then they agree in `ℤ`. -/ theorem IsQuadratic.eq_of_eq_coe {χ : MulChar R ℤ} (hχ : IsQuadratic χ) {χ' : MulChar R' ℤ} (hχ' : IsQuadratic χ') [Nontrivial R''] (hR'' : ringChar R'' ≠ 2) {a : R} {a' : R'} (h : (χ a : R'') = χ' a') : χ a = χ' a' := Int.cast_injOn_of_ringChar_ne_two hR'' (hχ a) (hχ' a') h /-- We can post-compose a multiplicative character with a ring homomorphism. -/ @[simps] def ringHomComp (χ : MulChar R R') (f : R' →+* R'') : MulChar R R'' := { f.toMonoidHom.comp χ.toMonoidHom with toFun := fun a => f (χ a) map_nonunit' := fun a ha => by simp only [map_nonunit χ ha, map_zero] } @[simp] lemma ringHomComp_one (f : R' →+* R'') : (1 : MulChar R R').ringHomComp f = 1 := by ext1 simp only [MulChar.ringHomComp_apply, MulChar.one_apply_coe, map_one] lemma ringHomComp_inv {R : Type*} [CommMonoidWithZero R] (χ : MulChar R R') (f : R' →+* R'') : (χ.ringHomComp f)⁻¹ = χ⁻¹.ringHomComp f := by ext1 simp only [inv_apply, Ring.inverse_unit, ringHomComp_apply] lemma ringHomComp_mul (χ φ : MulChar R R') (f : R' →+* R'') : (χ * φ).ringHomComp f = χ.ringHomComp f * φ.ringHomComp f := by ext1 simp only [ringHomComp_apply, coeToFun_mul, Pi.mul_apply, map_mul] lemma ringHomComp_pow (χ : MulChar R R') (f : R' →+* R'') (n : ℕ) : χ.ringHomComp f ^ n = (χ ^ n).ringHomComp f := by induction n with | zero => simp only [pow_zero, ringHomComp_one] | succ n ih => simp only [pow_succ, ih, ringHomComp_mul] lemma injective_ringHomComp {f : R' →+* R''} (hf : Function.Injective f) : Function.Injective (ringHomComp (R := R) · f) := by simpa only [Function.Injective, MulChar.ext_iff, ringHomComp, coe_mk, MonoidHom.coe_mk, OneHom.coe_mk] using fun χ χ' h a ↦ hf (h a) lemma ringHomComp_eq_one_iff {f : R' →+* R''} (hf : Function.Injective f) {χ : MulChar R R'} : χ.ringHomComp f = 1 ↔ χ = 1 := by conv_lhs => rw [← (show (1 : MulChar R R').ringHomComp f = 1 by ext; simp)] exact (injective_ringHomComp hf).eq_iff lemma ringHomComp_ne_one_iff {f : R' →+* R''} (hf : Function.Injective f) {χ : MulChar R R'} : χ.ringHomComp f ≠ 1 ↔ χ ≠ 1 := (ringHomComp_eq_one_iff hf).not /-- Composition with a ring homomorphism preserves the property of being a quadratic character. -/ theorem IsQuadratic.comp {χ : MulChar R R'} (hχ : χ.IsQuadratic) (f : R' →+* R'') : (χ.ringHomComp f).IsQuadratic := by intro a rcases hχ a with (ha | ha | ha) <;> simp [ha] /-- The inverse of a quadratic character is itself. → -/ theorem IsQuadratic.inv {χ : MulChar R R'} (hχ : χ.IsQuadratic) : χ⁻¹ = χ := by ext x rw [inv_apply_eq_inv] rcases hχ x with (h₀ | h₁ | h₂) · rw [h₀, Ring.inverse_zero] · rw [h₁, Ring.inverse_one] · -- Porting note: was `by norm_cast` have : (-1 : R') = (-1 : R'ˣ) := by rw [Units.val_neg, Units.val_one] rw [h₂, this, Ring.inverse_unit (-1 : R'ˣ)] rfl /-- The square of a quadratic character is the trivial character. -/ theorem IsQuadratic.sq_eq_one {χ : MulChar R R'} (hχ : χ.IsQuadratic) : χ ^ 2 = 1 := by rw [← inv_mul_cancel χ, pow_two, hχ.inv] /-- The `p`th power of a quadratic character is itself, when `p` is the (prime) characteristic of the target ring. -/ theorem IsQuadratic.pow_char {χ : MulChar R R'} (hχ : χ.IsQuadratic) (p : ℕ) [hp : Fact p.Prime] [CharP R' p] : χ ^ p = χ := by ext x rw [pow_apply_coe] rcases hχ x with (hx | hx | hx) <;> rw [hx] · rw [zero_pow (@Fact.out p.Prime).ne_zero] · rw [one_pow] · exact neg_one_pow_char R' p /-- The `n`th power of a quadratic character is the trivial character, when `n` is even. -/ theorem IsQuadratic.pow_even {χ : MulChar R R'} (hχ : χ.IsQuadratic) {n : ℕ} (hn : Even n) : χ ^ n = 1 := by obtain ⟨n, rfl⟩ := even_iff_two_dvd.mp hn rw [pow_mul, hχ.sq_eq_one, one_pow] /-- The `n`th power of a quadratic character is itself, when `n` is odd. -/ theorem IsQuadratic.pow_odd {χ : MulChar R R'} (hχ : χ.IsQuadratic) {n : ℕ} (hn : Odd n) : χ ^ n = χ := by obtain ⟨n, rfl⟩ := hn rw [pow_add, pow_one, hχ.pow_even (even_two_mul _), one_mul] /-- A multiplicative character `χ` into an integral domain is quadratic if and only if `χ^2 = 1`. -/ lemma isQuadratic_iff_sq_eq_one {M R : Type*} [CommMonoid M] [CommRing R] [NoZeroDivisors R] [Nontrivial R] {χ : MulChar M R} : IsQuadratic χ ↔ χ ^ 2 = 1:= by refine ⟨fun h ↦ ext (fun x ↦ ?_), fun h x ↦ ?_⟩ · rw [one_apply_coe, χ.pow_apply_coe] rcases h x with H | H | H · exact (not_isUnit_zero <| H ▸ IsUnit.map χ <| x.isUnit).elim · simp only [H, one_pow] · simp only [H, even_two, Even.neg_pow, one_pow] · by_cases hx : IsUnit x · refine .inr <| sq_eq_one_iff.mp ?_ rw [← χ.pow_apply' two_ne_zero, h, MulChar.one_apply hx] · exact .inl <| map_nonunit χ hx end quadratic_and_comp end Properties /-! ### Multiplicative characters with finite domain -/ section Finite variable {M : Type*} [CommMonoid M] variable {R : Type*} [CommMonoidWithZero R] /-- If `χ` is a multiplicative character on a commutative monoid `M` with finitely many units, then `χ ^ #Mˣ = 1`. -/ protected lemma pow_card_eq_one [Fintype Mˣ] (χ : MulChar M R) : χ ^ (Fintype.card Mˣ) = 1 := by ext1 rw [pow_apply_coe, ← map_pow, one_apply_coe, ← Units.val_pow_eq_pow_val, pow_card_eq_one, Units.val_eq_one.mpr rfl, map_one] /-- A multiplicative character on a commutative monoid with finitely many units has finite (= positive) order. -/ lemma orderOf_pos [Finite Mˣ] (χ : MulChar M R) : 0 < orderOf χ := by cases nonempty_fintype Mˣ apply IsOfFinOrder.orderOf_pos exact isOfFinOrder_iff_pow_eq_one.2 ⟨_, Fintype.card_pos, χ.pow_card_eq_one⟩ end Finite section sum variable {R : Type*} [CommMonoid R] [Fintype R] {R' : Type*} [CommRing R'] /-- The sum over all values of a nontrivial multiplicative character on a finite ring is zero (when the target is a domain). -/ theorem sum_eq_zero_of_ne_one [IsDomain R'] {χ : MulChar R R'} (hχ : χ ≠ 1) : ∑ a, χ a = 0 := by rcases ne_one_iff.mp hχ with ⟨b, hb⟩ refine eq_zero_of_mul_eq_self_left hb ?_ simpa only [Finset.mul_sum, ← map_mul] using b.mulLeft_bijective.sum_comp _ /-- The sum over all values of the trivial multiplicative character on a finite ring is the cardinality of its unit group. -/ theorem sum_one_eq_card_units [DecidableEq R] : (∑ a, (1 : MulChar R R') a) = Fintype.card Rˣ := by calc (∑ a, (1 : MulChar R R') a) = ∑ a : R, if IsUnit a then 1 else 0 := Finset.sum_congr rfl fun a _ => ?_ _ = ((Finset.univ : Finset R).filter IsUnit).card := Finset.sum_boole _ _ _ = (Finset.univ.map ⟨((↑) : Rˣ → R), Units.ext⟩).card := ?_ _ = Fintype.card Rˣ := congr_arg _ (Finset.card_map _) · split_ifs with h · exact one_apply_coe h.unit · exact map_nonunit _ h · congr ext a simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_map, Function.Embedding.coeFn_mk, exists_true_left, IsUnit] end sum /-! ### Multiplicative characters on rings -/ section Ring variable {R R' : Type*} [CommRing R] [CommMonoidWithZero R'] /-- If `χ` is of odd order, then `χ(-1) = 1` -/ lemma val_neg_one_eq_one_of_odd_order {χ : MulChar R R'} {n : ℕ} (hn : Odd n) (hχ : χ ^ n = 1) : χ (-1) = 1 := by rw [← hn.neg_one_pow, map_pow, ← χ.pow_apply' (Nat.ne_of_odd_add hn), hχ] exact MulChar.one_apply_coe (-1) end Ring end MulChar
Mathlib/NumberTheory/MulChar/Basic.lean
604
618
/- 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.LinearAlgebra.Matrix.NonsingularInverse import Mathlib.LinearAlgebra.Matrix.Symmetric /-! # Integer powers of square matrices In this file, we define integer power of matrices, relying on the nonsingular inverse definition for negative powers. ## Implementation details The main definition is a direct recursive call on the integer inductive type, as provided by the `DivInvMonoid.Pow` default implementation. The lemma names are taken from `Algebra.GroupWithZero.Power`. ## Tags matrix inverse, matrix powers -/ open Matrix namespace Matrix variable {n' : Type*} [DecidableEq n'] [Fintype n'] {R : Type*} [CommRing R] local notation "M" => Matrix n' n' R noncomputable instance : DivInvMonoid M := { show Monoid M by infer_instance, show Inv M by infer_instance with } section NatPow @[simp] theorem inv_pow' (A : M) (n : ℕ) : A⁻¹ ^ n = (A ^ n)⁻¹ := by induction n with | zero => simp | succ n ih => rw [pow_succ A, mul_inv_rev, ← ih, ← pow_succ'] theorem pow_sub' (A : M) {m n : ℕ} (ha : IsUnit A.det) (h : n ≤ m) : A ^ (m - n) = A ^ m * (A ^ n)⁻¹ := by rw [← tsub_add_cancel_of_le h, pow_add, Matrix.mul_assoc, mul_nonsing_inv, tsub_add_cancel_of_le h, Matrix.mul_one] simpa using ha.pow n theorem pow_inv_comm' (A : M) (m n : ℕ) : A⁻¹ ^ m * A ^ n = A ^ n * A⁻¹ ^ m := by induction n generalizing m with | zero => simp | succ n IH => rcases m with m | m · simp rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h · calc A⁻¹ ^ (m + 1) * A ^ (n + 1) = A⁻¹ ^ m * (A⁻¹ * A) * A ^ n := by simp only [pow_succ A⁻¹, pow_succ' A, Matrix.mul_assoc] _ = A ^ n * A⁻¹ ^ m := by simp only [h, Matrix.mul_one, Matrix.one_mul, IH m] _ = A ^ n * (A * A⁻¹) * A⁻¹ ^ m := by simp only [h', Matrix.mul_one, Matrix.one_mul] _ = A ^ (n + 1) * A⁻¹ ^ (m + 1) := by simp only [pow_succ A, pow_succ' A⁻¹, Matrix.mul_assoc] · simp [h] end NatPow section ZPow open Int @[simp] theorem one_zpow : ∀ n : ℤ, (1 : M) ^ n = 1 | (n : ℕ) => by rw [zpow_natCast, one_pow] | -[n+1] => by rw [zpow_negSucc, one_pow, inv_one] theorem zero_zpow : ∀ z : ℤ, z ≠ 0 → (0 : M) ^ z = 0 | (n : ℕ), h => by rw [zpow_natCast, zero_pow] exact mod_cast h | -[n+1], _ => by simp [zero_pow n.succ_ne_zero] theorem zero_zpow_eq (n : ℤ) : (0 : M) ^ n = if n = 0 then 1 else 0 := by split_ifs with h · rw [h, zpow_zero] · rw [zero_zpow _ h] theorem inv_zpow (A : M) : ∀ n : ℤ, A⁻¹ ^ n = (A ^ n)⁻¹ | (n : ℕ) => by rw [zpow_natCast, zpow_natCast, inv_pow'] | -[n+1] => by rw [zpow_negSucc, zpow_negSucc, inv_pow'] @[simp] theorem zpow_neg_one (A : M) : A ^ (-1 : ℤ) = A⁻¹ := by convert DivInvMonoid.zpow_neg' 0 A simp only [zpow_one, Int.ofNat_zero, Int.natCast_succ, zpow_eq_pow, zero_add] @[simp] theorem zpow_neg_natCast (A : M) (n : ℕ) : A ^ (-n : ℤ) = (A ^ n)⁻¹ := by cases n · simp · exact DivInvMonoid.zpow_neg' _ _ theorem _root_.IsUnit.det_zpow {A : M} (h : IsUnit A.det) (n : ℤ) : IsUnit (A ^ n).det := by rcases n with n | n · simpa using h.pow n · simpa using h.pow n.succ theorem isUnit_det_zpow_iff {A : M} {z : ℤ} : IsUnit (A ^ z).det ↔ IsUnit A.det ∨ z = 0 := by induction z with | hz => simp | hp z => rw [← Int.natCast_succ, zpow_natCast, det_pow, isUnit_pow_succ_iff, ← Int.ofNat_zero, Int.ofNat_inj] simp | hn z => rw [← neg_add', ← Int.natCast_succ, zpow_neg_natCast, isUnit_nonsing_inv_det_iff, det_pow, isUnit_pow_succ_iff, neg_eq_zero, ← Int.ofNat_zero, Int.ofNat_inj]
simp theorem zpow_neg {A : M} (h : IsUnit A.det) : ∀ n : ℤ, A ^ (-n) = (A ^ n)⁻¹ | (n : ℕ) => zpow_neg_natCast _ _
Mathlib/LinearAlgebra/Matrix/ZPow.lean
120
123
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Group.Action.Pointwise.Finset import Mathlib.GroupTheory.QuotientGroup.Defs import Mathlib.Order.ConditionallyCompleteLattice.Basic /-! # Stabilizer of a set under a pointwise action This file characterises the stabilizer of a set/finset under the pointwise action of a group. -/ open Function MulOpposite Set open scoped Pointwise namespace MulAction variable {G H α : Type*} /-! ### Stabilizer of a set -/ section Set section Group variable [Group G] [Group H] [MulAction G α] {a : G} {s t : Set α} @[to_additive (attr := simp)] lemma stabilizer_empty : stabilizer G (∅ : Set α) = ⊤ := Subgroup.coe_eq_univ.1 <| eq_univ_of_forall fun _a ↦ smul_set_empty @[to_additive (attr := simp)] lemma stabilizer_univ : stabilizer G (Set.univ : Set α) = ⊤ := by ext simp @[to_additive (attr := simp)] lemma stabilizer_singleton (b : α) : stabilizer G ({b} : Set α) = stabilizer G b := by ext; simp @[to_additive] lemma mem_stabilizer_set {s : Set α} : a ∈ stabilizer G s ↔ ∀ b, a • b ∈ s ↔ b ∈ s := by refine mem_stabilizer_iff.trans ⟨fun h b ↦ ?_, fun h ↦ ?_⟩ · rw [← (smul_mem_smul_set_iff : a • b ∈ _ ↔ _), h] simp_rw [Set.ext_iff, mem_smul_set_iff_inv_smul_mem] exact ((MulAction.toPerm a).forall_congr' <| by simp [Iff.comm]).1 h @[to_additive] lemma map_stabilizer_le (f : G →* H) (s : Set G) : (stabilizer G s).map f ≤ stabilizer H (f '' s) := by rintro a simp only [Subgroup.mem_map, mem_stabilizer_iff, exists_prop, forall_exists_index, and_imp] rintro a ha rfl rw [← image_smul_distrib, ha] @[to_additive (attr := simp)] lemma stabilizer_mul_self (s : Set G) : (stabilizer G s : Set G) * s = s := by ext refine ⟨?_, fun h ↦ ⟨_, (stabilizer G s).one_mem, _, h, one_mul _⟩⟩ rintro ⟨a, ha, b, hb, rfl⟩ rw [← mem_stabilizer_iff.1 ha] exact smul_mem_smul_set hb @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_apply₂ {f : Set α → Set α → Set α} (hf : ∀ a : G, a • f s t = f (a • s) (a • t)) : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (f s t) := by aesop (add simp [SetLike.le_def]) @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_union : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (s ∪ t) := stabilizer_inf_stabilizer_le_stabilizer_apply₂ fun _ ↦ smul_set_union @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_inter : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (s ∩ t) := stabilizer_inf_stabilizer_le_stabilizer_apply₂ fun _ ↦ smul_set_inter @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_sdiff : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (s \ t) := stabilizer_inf_stabilizer_le_stabilizer_apply₂ fun _ ↦ smul_set_sdiff @[to_additive] lemma stabilizer_union_eq_left (hdisj : Disjoint s t) (hstab : stabilizer G s ≤ stabilizer G t) (hstab_union : stabilizer G (s ∪ t) ≤ stabilizer G t) : stabilizer G (s ∪ t) = stabilizer G s := by refine le_antisymm ?_ ?_ · calc stabilizer G (s ∪ t) ≤ stabilizer G (s ∪ t) ⊓ stabilizer G t := by simpa _ ≤ stabilizer G ((s ∪ t) \ t) := stabilizer_inf_stabilizer_le_stabilizer_sdiff _ = stabilizer G s := by rw [union_diff_cancel_right]; simpa [← disjoint_iff_inter_eq_empty] · calc stabilizer G s ≤ stabilizer G s ⊓ stabilizer G t := by simpa _ ≤ stabilizer G (s ∪ t) := stabilizer_inf_stabilizer_le_stabilizer_union @[to_additive] lemma stabilizer_union_eq_right (hdisj : Disjoint s t) (hstab : stabilizer G t ≤ stabilizer G s) (hstab_union : stabilizer G (s ∪ t) ≤ stabilizer G s) : stabilizer G (s ∪ t) = stabilizer G t := by rw [union_comm, stabilizer_union_eq_left hdisj.symm hstab (union_comm .. ▸ hstab_union)] variable {s : Set G} open scoped RightActions in @[to_additive] lemma op_smul_set_stabilizer_subset (ha : a ∈ s) : (stabilizer G s : Set G) <• a ⊆ s := smul_set_subset_iff.2 fun b hb ↦ by rw [← hb]; exact smul_mem_smul_set ha @[to_additive]
lemma stabilizer_subset_div_right (ha : a ∈ s) : ↑(stabilizer G s) ⊆ s / {a} := fun b hb ↦ ⟨_, by rwa [← smul_eq_mul, mem_stabilizer_set.1 hb], _, mem_singleton _, mul_div_cancel_right _ _⟩
Mathlib/Algebra/Pointwise/Stabilizer.lean
112
114
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.RingTheory.Finiteness.Basic import Mathlib.RingTheory.Finiteness.Nakayama import Mathlib.RingTheory.Jacobson.Ideal /-! # Nakayama's lemma This file contains some alternative statements of Nakayama's Lemma as found in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). ## Main statements * `Submodule.eq_smul_of_le_smul_of_le_jacobson` - A version of (2) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV), generalising to the Jacobson of any ideal. * `Submodule.eq_bot_of_le_smul_of_le_jacobson_bot` - Statement (2) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). * `Submodule.sup_smul_eq_sup_smul_of_le_smul_of_le_jacobson` - A version of (4) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV), generalising to the Jacobson of any ideal. * `Submodule.smul_le_of_le_smul_of_le_jacobson_bot` - Statement (4) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). Note that a version of Statement (1) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV) can be found in `RingTheory.Finiteness` under the name `Submodule.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul` ## References * [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV) ## Tags Nakayama, Jacobson -/ variable {R M : Type*} [CommRing R] [AddCommGroup M] [Module R M] open Ideal namespace Submodule /-- **Nakayama's Lemma** - A slightly more general version of (2) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `eq_bot_of_le_smul_of_le_jacobson_bot` for the special case when `J = ⊥`. -/ @[stacks 00DV "(2)"] theorem eq_smul_of_le_smul_of_le_jacobson {I J : Ideal R} {N : Submodule R M} (hN : N.FG) (hIN : N ≤ I • N) (hIjac : I ≤ jacobson J) : N = J • N := by refine le_antisymm ?_ (Submodule.smul_le.2 fun _ _ _ => Submodule.smul_mem _ _) intro n hn obtain ⟨r, hr⟩ := Submodule.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul I N hN hIN obtain ⟨s, hs⟩ := exists_mul_sub_mem_of_sub_one_mem_jacobson r (hIjac hr.1) have : n = -(s * r - 1) • n := by rw [neg_sub, sub_smul, mul_smul, hr.2 n hn, one_smul, smul_zero, sub_zero] rw [this] exact Submodule.smul_mem_smul (Submodule.neg_mem _ hs) hn lemma eq_bot_of_eq_ideal_smul_of_le_jacobson_annihilator {I : Ideal R} {N : Submodule R M} (hN : FG N) (hIN : N = I • N) (hIjac : I ≤ N.annihilator.jacobson) : N = ⊥ := (eq_smul_of_le_smul_of_le_jacobson hN hIN.le hIjac).trans N.annihilator_smul open Pointwise in lemma eq_bot_of_eq_pointwise_smul_of_mem_jacobson_annihilator {r : R} {N : Submodule R M} (hN : FG N) (hrN : N = r • N) (hrJac : r ∈ N.annihilator.jacobson) : N = ⊥ := eq_bot_of_eq_ideal_smul_of_le_jacobson_annihilator hN (Eq.trans hrN (ideal_span_singleton_smul r N).symm) ((span_singleton_le_iff_mem r _).mpr hrJac) open Pointwise in lemma eq_bot_of_set_smul_eq_of_subset_jacobson_annihilator {s : Set R} {N : Submodule R M} (hN : FG N) (hsN : N = s • N) (hsJac : s ⊆ N.annihilator.jacobson) : N = ⊥ := eq_bot_of_eq_ideal_smul_of_le_jacobson_annihilator hN (Eq.trans hsN (span_smul_eq s N).symm) (span_le.mpr hsJac) lemma top_ne_ideal_smul_of_le_jacobson_annihilator [Nontrivial M] [Module.Finite R M] {I} (h : I ≤ (Module.annihilator R M).jacobson) : (⊤ : Submodule R M) ≠ I • ⊤ := fun H => top_ne_bot <| eq_bot_of_eq_ideal_smul_of_le_jacobson_annihilator Module.Finite.fg_top H <| (congrArg (I ≤ Ideal.jacobson ·) annihilator_top).mpr h open Pointwise in lemma top_ne_set_smul_of_subset_jacobson_annihilator [Nontrivial M] [Module.Finite R M] {s : Set R} (h : s ⊆ (Module.annihilator R M).jacobson) : (⊤ : Submodule R M) ≠ s • ⊤ := ne_of_ne_of_eq (top_ne_ideal_smul_of_le_jacobson_annihilator (span_le.mpr h)) (span_smul_eq _ _) open Pointwise in lemma top_ne_pointwise_smul_of_mem_jacobson_annihilator [Nontrivial M] [Module.Finite R M] {r} (h : r ∈ (Module.annihilator R M).jacobson) : (⊤ : Submodule R M) ≠ r • ⊤ := ne_of_ne_of_eq (top_ne_set_smul_of_subset_jacobson_annihilator <| Set.singleton_subset_iff.mpr h) (singleton_set_smul ⊤ r) /-- **Nakayama's Lemma** - Statement (2) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `eq_smul_of_le_smul_of_le_jacobson` for a generalisation to the `jacobson` of any ideal -/
@[stacks 00DV "(2)"] theorem eq_bot_of_le_smul_of_le_jacobson_bot (I : Ideal R) (N : Submodule R M) (hN : N.FG) (hIN : N ≤ I • N) (hIjac : I ≤ jacobson ⊥) : N = ⊥ := by
Mathlib/RingTheory/Nakayama.lean
109
111
/- 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, Yury Kudryashov -/ import Mathlib.Topology.Order.LeftRight import Mathlib.Topology.Separation.Hausdorff /-! # Order-closed topologies In this file we introduce 3 typeclass mixins that relate topology and order structures: - `ClosedIicTopology` says that all the intervals $(-∞, a]$ (formally, `Set.Iic a`) are closed sets; - `ClosedIciTopology` says that all the intervals $[a, +∞)$ (formally, `Set.Ici a`) are closed sets; - `OrderClosedTopology` says that the set of points `(x, y)` such that `x ≤ y` is closed in the product topology. The last predicate implies the first two. We prove many basic properties of such topologies. ## Main statements This file contains the proofs of the following facts. For exact requirements (`OrderClosedTopology` vs `ClosedIciTopology` vs `ClosedIicTopology, `Preorder` vs `PartialOrder` vs `LinearOrder` etc) see their statements. ### Open / closed sets * `isOpen_lt` : if `f` and `g` are continuous functions, then `{x | f x < g x}` is open; * `isOpen_Iio`, `isOpen_Ioi`, `isOpen_Ioo` : open intervals are open; * `isClosed_le` : if `f` and `g` are continuous functions, then `{x | f x ≤ g x}` is closed; * `isClosed_Iic`, `isClosed_Ici`, `isClosed_Icc` : closed intervals are closed; * `frontier_le_subset_eq`, `frontier_lt_subset_eq` : frontiers of both `{x | f x ≤ g x}` and `{x | f x < g x}` are included by `{x | f x = g x}`; ### Convergence and inequalities * `le_of_tendsto_of_tendsto` : if `f` converges to `a`, `g` converges to `b`, and eventually `f x ≤ g x`, then `a ≤ b` * `le_of_tendsto`, `ge_of_tendsto` : if `f` converges to `a` and eventually `f x ≤ b` (resp., `b ≤ f x`), then `a ≤ b` (resp., `b ≤ a`); we also provide primed versions that assume the inequalities to hold for all `x`. ### Min, max, `sSup` and `sInf` * `Continuous.min`, `Continuous.max`: pointwise `min`/`max` of two continuous functions is continuous. * `Tendsto.min`, `Tendsto.max` : if `f` tends to `a` and `g` tends to `b`, then their pointwise `min`/`max` tend to `min a b` and `max a b`, respectively. -/ open Set Filter open OrderDual (toDual) open scoped Topology universe u v w variable {α : Type u} {β : Type v} {γ : Type w} /-- If `α` is a topological space and a preorder, `ClosedIicTopology α` means that `Iic a` is closed for all `a : α`. -/ class ClosedIicTopology (α : Type*) [TopologicalSpace α] [Preorder α] : Prop where /-- For any `a`, the set `(-∞, a]` is closed. -/ isClosed_Iic (a : α) : IsClosed (Iic a) /-- If `α` is a topological space and a preorder, `ClosedIciTopology α` means that `Ici a` is closed for all `a : α`. -/ class ClosedIciTopology (α : Type*) [TopologicalSpace α] [Preorder α] : Prop where /-- For any `a`, the set `[a, +∞)` is closed. -/ isClosed_Ici (a : α) : IsClosed (Ici a) /-- A topology on a set which is both a topological space and a preorder is _order-closed_ if the set of points `(x, y)` with `x ≤ y` is closed in the product space. We introduce this as a mixin. This property is satisfied for the order topology on a linear order, but it can be satisfied more generally, and suffices to derive many interesting properties relating order and topology. -/ class OrderClosedTopology (α : Type*) [TopologicalSpace α] [Preorder α] : Prop where /-- The set `{ (x, y) | x ≤ y }` is a closed set. -/ isClosed_le' : IsClosed { p : α × α | p.1 ≤ p.2 } instance [TopologicalSpace α] [h : FirstCountableTopology α] : FirstCountableTopology αᵒᵈ := h instance [TopologicalSpace α] [h : SecondCountableTopology α] : SecondCountableTopology αᵒᵈ := h theorem Dense.orderDual [TopologicalSpace α] {s : Set α} (hs : Dense s) : Dense (OrderDual.ofDual ⁻¹' s) := hs section General variable [TopologicalSpace α] [Preorder α] {s : Set α} protected lemma BddAbove.of_closure : BddAbove (closure s) → BddAbove s := BddAbove.mono subset_closure protected lemma BddBelow.of_closure : BddBelow (closure s) → BddBelow s := BddBelow.mono subset_closure end General section ClosedIicTopology section Preorder variable [TopologicalSpace α] [Preorder α] [ClosedIicTopology α] {f : β → α} {a b : α} {s : Set α} theorem isClosed_Iic : IsClosed (Iic a) := ClosedIicTopology.isClosed_Iic a instance : ClosedIciTopology αᵒᵈ where isClosed_Ici _ := isClosed_Iic (α := α) @[simp] theorem closure_Iic (a : α) : closure (Iic a) = Iic a := isClosed_Iic.closure_eq theorem le_of_tendsto_of_frequently {x : Filter β} (lim : Tendsto f x (𝓝 a)) (h : ∃ᶠ c in x, f c ≤ b) : a ≤ b := isClosed_Iic.mem_of_frequently_of_tendsto h lim theorem le_of_tendsto {x : Filter β} [NeBot x] (lim : Tendsto f x (𝓝 a)) (h : ∀ᶠ c in x, f c ≤ b) : a ≤ b := isClosed_Iic.mem_of_tendsto lim h theorem le_of_tendsto' {x : Filter β} [NeBot x] (lim : Tendsto f x (𝓝 a)) (h : ∀ c, f c ≤ b) : a ≤ b := le_of_tendsto lim (Eventually.of_forall h) @[simp] lemma upperBounds_closure (s : Set α) : upperBounds (closure s : Set α) = upperBounds s := ext fun a ↦ by simp_rw [mem_upperBounds_iff_subset_Iic, isClosed_Iic.closure_subset_iff] @[simp] lemma bddAbove_closure : BddAbove (closure s) ↔ BddAbove s := by simp_rw [BddAbove, upperBounds_closure] protected alias ⟨_, BddAbove.closure⟩ := bddAbove_closure @[simp] theorem disjoint_nhds_atBot_iff : Disjoint (𝓝 a) atBot ↔ ¬IsBot a := by constructor · intro hd hbot rw [hbot.atBot_eq, disjoint_principal_right] at hd exact mem_of_mem_nhds hd le_rfl · simp only [IsBot, not_forall] rintro ⟨b, hb⟩ refine disjoint_of_disjoint_of_mem disjoint_compl_left ?_ (Iic_mem_atBot b) exact isClosed_Iic.isOpen_compl.mem_nhds hb theorem IsLUB.range_of_tendsto {F : Filter β} [F.NeBot] (hle : ∀ i, f i ≤ a) (hlim : Tendsto f F (𝓝 a)) : IsLUB (range f) a := ⟨forall_mem_range.mpr hle, fun _c hc ↦ le_of_tendsto' hlim fun i ↦ hc <| mem_range_self i⟩ end Preorder section NoBotOrder variable [Preorder α] [NoBotOrder α] [TopologicalSpace α] [ClosedIicTopology α] {a : α} {l : Filter β} [NeBot l] {f : β → α} theorem disjoint_nhds_atBot (a : α) : Disjoint (𝓝 a) atBot := by simp @[simp] theorem inf_nhds_atBot (a : α) : 𝓝 a ⊓ atBot = ⊥ := (disjoint_nhds_atBot a).eq_bot theorem not_tendsto_nhds_of_tendsto_atBot (hf : Tendsto f l atBot) (a : α) : ¬Tendsto f l (𝓝 a) := hf.not_tendsto (disjoint_nhds_atBot a).symm theorem not_tendsto_atBot_of_tendsto_nhds (hf : Tendsto f l (𝓝 a)) : ¬Tendsto f l atBot := hf.not_tendsto (disjoint_nhds_atBot a) end NoBotOrder theorem iSup_eq_of_forall_le_of_tendsto {ι : Type*} {F : Filter ι} [Filter.NeBot F] [ConditionallyCompleteLattice α] [TopologicalSpace α] [ClosedIicTopology α] {a : α} {f : ι → α} (hle : ∀ i, f i ≤ a) (hlim : Filter.Tendsto f F (𝓝 a)) : ⨆ i, f i = a := have := F.nonempty_of_neBot (IsLUB.range_of_tendsto hle hlim).ciSup_eq theorem iUnion_Iic_eq_Iio_of_lt_of_tendsto {ι : Type*} {F : Filter ι} [F.NeBot] [ConditionallyCompleteLinearOrder α] [TopologicalSpace α] [ClosedIicTopology α] {a : α} {f : ι → α} (hlt : ∀ i, f i < a) (hlim : Tendsto f F (𝓝 a)) : ⋃ i : ι, Iic (f i) = Iio a := by have obs : a ∉ range f := by rw [mem_range] rintro ⟨i, rfl⟩ exact (hlt i).false rw [← biUnion_range, (IsLUB.range_of_tendsto (le_of_lt <| hlt ·) hlim).biUnion_Iic_eq_Iio obs] section LinearOrder variable [TopologicalSpace α] [LinearOrder α] [ClosedIicTopology α] [TopologicalSpace β] {a b c : α} {f : α → β} theorem isOpen_Ioi : IsOpen (Ioi a) := by rw [← compl_Iic] exact isClosed_Iic.isOpen_compl @[simp] theorem interior_Ioi : interior (Ioi a) = Ioi a := isOpen_Ioi.interior_eq theorem Ioi_mem_nhds (h : a < b) : Ioi a ∈ 𝓝 b := IsOpen.mem_nhds isOpen_Ioi h theorem eventually_gt_nhds (hab : b < a) : ∀ᶠ x in 𝓝 a, b < x := Ioi_mem_nhds hab theorem Ici_mem_nhds (h : a < b) : Ici a ∈ 𝓝 b := mem_of_superset (Ioi_mem_nhds h) Ioi_subset_Ici_self theorem eventually_ge_nhds (hab : b < a) : ∀ᶠ x in 𝓝 a, b ≤ x := Ici_mem_nhds hab theorem Filter.Tendsto.eventually_const_lt {l : Filter γ} {f : γ → α} {u v : α} (hv : u < v) (h : Filter.Tendsto f l (𝓝 v)) : ∀ᶠ a in l, u < f a := h.eventually <| eventually_gt_nhds hv @[deprecated (since := "2024-11-17")] alias eventually_gt_of_tendsto_gt := Filter.Tendsto.eventually_const_lt theorem Filter.Tendsto.eventually_const_le {l : Filter γ} {f : γ → α} {u v : α} (hv : u < v) (h : Tendsto f l (𝓝 v)) : ∀ᶠ a in l, u ≤ f a := h.eventually <| eventually_ge_nhds hv @[deprecated (since := "2024-11-17")] alias eventually_ge_of_tendsto_gt := Filter.Tendsto.eventually_const_le protected theorem Dense.exists_gt [NoMaxOrder α] {s : Set α} (hs : Dense s) (x : α) : ∃ y ∈ s, x < y := hs.exists_mem_open isOpen_Ioi (exists_gt x) protected theorem Dense.exists_ge [NoMaxOrder α] {s : Set α} (hs : Dense s) (x : α) : ∃ y ∈ s, x ≤ y := (hs.exists_gt x).imp fun _ h ↦ ⟨h.1, h.2.le⟩ theorem Dense.exists_ge' {s : Set α} (hs : Dense s) (htop : ∀ x, IsTop x → x ∈ s) (x : α) : ∃ y ∈ s, x ≤ y := by by_cases hx : IsTop x · exact ⟨x, htop x hx, le_rfl⟩ · simp only [IsTop, not_forall, not_le] at hx rcases hs.exists_mem_open isOpen_Ioi hx with ⟨y, hys, hy : x < y⟩ exact ⟨y, hys, hy.le⟩ /-! ### Left neighborhoods on a `ClosedIicTopology` Limits to the left of real functions are defined in terms of neighborhoods to the left, either open or closed, i.e., members of `𝓝[<] a` and `𝓝[≤] a`. Here we prove that all left-neighborhoods of a point are equal, and we prove other useful characterizations which require the stronger hypothesis `OrderTopology α` in another file. -/ /-! #### Point excluded -/ theorem Ioo_mem_nhdsLT (H : a < b) : Ioo a b ∈ 𝓝[<] b := by simpa only [← Iio_inter_Ioi] using inter_mem_nhdsWithin _ (Ioi_mem_nhds H) @[deprecated (since := "2024-12-21")] alias Ioo_mem_nhdsWithin_Iio' := Ioo_mem_nhdsLT theorem Ioo_mem_nhdsLT_of_mem (H : b ∈ Ioc a c) : Ioo a c ∈ 𝓝[<] b := mem_of_superset (Ioo_mem_nhdsLT H.1) <| Ioo_subset_Ioo_right H.2 @[deprecated (since := "2024-12-21")] alias Ioo_mem_nhdsWithin_Iio := Ioo_mem_nhdsLT_of_mem protected theorem CovBy.nhdsLT (h : a ⋖ b) : 𝓝[<] b = ⊥ := empty_mem_iff_bot.mp <| h.Ioo_eq ▸ Ioo_mem_nhdsLT h.1 @[deprecated (since := "2024-12-21")] protected alias CovBy.nhdsWithin_Iio := CovBy.nhdsLT protected theorem PredOrder.nhdsLT [PredOrder α] : 𝓝[<] a = ⊥ := by if h : IsMin a then simp [h.Iio_eq] else exact (Order.pred_covBy_of_not_isMin h).nhdsLT @[deprecated (since := "2024-12-21")] protected alias PredOrder.nhdsWithin_Iio := PredOrder.nhdsLT theorem PredOrder.nhdsGT_eq_nhdsNE [PredOrder α] (a : α) : 𝓝[>] a = 𝓝[≠] a := by rw [← nhdsLT_sup_nhdsGT, PredOrder.nhdsLT, bot_sup_eq] theorem PredOrder.nhdsGE_eq_nhds [PredOrder α] (a : α) : 𝓝[≥] a = 𝓝 a := by rw [← nhdsLT_sup_nhdsGE, PredOrder.nhdsLT, bot_sup_eq] theorem Ico_mem_nhdsLT_of_mem (H : b ∈ Ioc a c) : Ico a c ∈ 𝓝[<] b := mem_of_superset (Ioo_mem_nhdsLT_of_mem H) Ioo_subset_Ico_self @[deprecated (since := "2024-12-21")] alias Ico_mem_nhdsWithin_Iio := Ico_mem_nhdsLT_of_mem theorem Ico_mem_nhdsLT (H : a < b) : Ico a b ∈ 𝓝[<] b := Ico_mem_nhdsLT_of_mem ⟨H, le_rfl⟩ @[deprecated (since := "2024-12-21")] alias Ico_mem_nhdsWithin_Iio' := Ico_mem_nhdsLT theorem Ioc_mem_nhdsLT_of_mem (H : b ∈ Ioc a c) : Ioc a c ∈ 𝓝[<] b := mem_of_superset (Ioo_mem_nhdsLT_of_mem H) Ioo_subset_Ioc_self @[deprecated (since := "2024-12-21")] alias Ioc_mem_nhdsWithin_Iio := Ioc_mem_nhdsLT_of_mem theorem Ioc_mem_nhdsLT (H : a < b) : Ioc a b ∈ 𝓝[<] b := Ioc_mem_nhdsLT_of_mem ⟨H, le_rfl⟩ @[deprecated (since := "2024-12-21")] alias Ioc_mem_nhdsWithin_Iio' := Ioc_mem_nhdsLT theorem Icc_mem_nhdsLT_of_mem (H : b ∈ Ioc a c) : Icc a c ∈ 𝓝[<] b := mem_of_superset (Ioo_mem_nhdsLT_of_mem H) Ioo_subset_Icc_self @[deprecated (since := "2024-12-21")] alias Icc_mem_nhdsWithin_Iio := Icc_mem_nhdsLT_of_mem theorem Icc_mem_nhdsLT (H : a < b) : Icc a b ∈ 𝓝[<] b := Icc_mem_nhdsLT_of_mem ⟨H, le_rfl⟩ @[deprecated (since := "2024-12-21")] alias Icc_mem_nhdsWithin_Iio' := Icc_mem_nhdsLT @[simp] theorem nhdsWithin_Ico_eq_nhdsLT (h : a < b) : 𝓝[Ico a b] b = 𝓝[<] b := nhdsWithin_inter_of_mem <| nhdsWithin_le_nhds <| Ici_mem_nhds h @[deprecated (since := "2024-12-21")] alias nhdsWithin_Ico_eq_nhdsWithin_Iio := nhdsWithin_Ico_eq_nhdsLT @[simp] theorem nhdsWithin_Ioo_eq_nhdsLT (h : a < b) : 𝓝[Ioo a b] b = 𝓝[<] b := nhdsWithin_inter_of_mem <| nhdsWithin_le_nhds <| Ioi_mem_nhds h @[deprecated (since := "2024-12-21")] alias nhdsWithin_Ioo_eq_nhdsWithin_Iio := nhdsWithin_Ioo_eq_nhdsLT @[simp] theorem continuousWithinAt_Ico_iff_Iio (h : a < b) : ContinuousWithinAt f (Ico a b) b ↔ ContinuousWithinAt f (Iio b) b := by simp only [ContinuousWithinAt, nhdsWithin_Ico_eq_nhdsLT h] @[simp] theorem continuousWithinAt_Ioo_iff_Iio (h : a < b) : ContinuousWithinAt f (Ioo a b) b ↔ ContinuousWithinAt f (Iio b) b := by simp only [ContinuousWithinAt, nhdsWithin_Ioo_eq_nhdsLT h] /-! #### Point included -/ protected theorem CovBy.nhdsLE (H : a ⋖ b) : 𝓝[≤] b = pure b := by rw [← Iio_insert, nhdsWithin_insert, H.nhdsLT, sup_bot_eq] @[deprecated (since := "2024-12-21")] protected alias CovBy.nhdsWithin_Iic := CovBy.nhdsLE protected theorem PredOrder.nhdsLE [PredOrder α] : 𝓝[≤] b = pure b := by rw [← Iio_insert, nhdsWithin_insert, PredOrder.nhdsLT, sup_bot_eq] @[deprecated (since := "2024-12-21")] protected alias PredOrder.nhdsWithin_Iic := PredOrder.nhdsLE theorem Ioc_mem_nhdsLE (H : a < b) : Ioc a b ∈ 𝓝[≤] b := inter_mem (nhdsWithin_le_nhds <| Ioi_mem_nhds H) self_mem_nhdsWithin @[deprecated (since := "2024-12-21")] alias Ioc_mem_nhdsWithin_Iic' := Ioc_mem_nhdsLE theorem Ioo_mem_nhdsLE_of_mem (H : b ∈ Ioo a c) : Ioo a c ∈ 𝓝[≤] b := mem_of_superset (Ioc_mem_nhdsLE H.1) <| Ioc_subset_Ioo_right H.2 @[deprecated (since := "2024-12-21")] alias Ioo_mem_nhdsWithin_Iic := Ioo_mem_nhdsLE_of_mem theorem Ico_mem_nhdsLE_of_mem (H : b ∈ Ioo a c) : Ico a c ∈ 𝓝[≤] b := mem_of_superset (Ioo_mem_nhdsLE_of_mem H) Ioo_subset_Ico_self @[deprecated (since := "2024-12-22")] alias Ico_mem_nhdsWithin_Iic := Ico_mem_nhdsLE_of_mem theorem Ioc_mem_nhdsLE_of_mem (H : b ∈ Ioc a c) : Ioc a c ∈ 𝓝[≤] b := mem_of_superset (Ioc_mem_nhdsLE H.1) <| Ioc_subset_Ioc_right H.2 @[deprecated (since := "2024-12-22")] alias Ioc_mem_nhdsWithin_Iic := Ioc_mem_nhdsLE_of_mem theorem Icc_mem_nhdsLE_of_mem (H : b ∈ Ioc a c) : Icc a c ∈ 𝓝[≤] b := mem_of_superset (Ioc_mem_nhdsLE_of_mem H) Ioc_subset_Icc_self @[deprecated (since := "2024-12-22")] alias Icc_mem_nhdsWithin_Iic := Icc_mem_nhdsLE_of_mem theorem Icc_mem_nhdsLE (H : a < b) : Icc a b ∈ 𝓝[≤] b := Icc_mem_nhdsLE_of_mem ⟨H, le_rfl⟩ @[deprecated (since := "2024-12-22")] alias Icc_mem_nhdsWithin_Iic' := Icc_mem_nhdsLE @[simp] theorem nhdsWithin_Icc_eq_nhdsLE (h : a < b) : 𝓝[Icc a b] b = 𝓝[≤] b := nhdsWithin_inter_of_mem <| nhdsWithin_le_nhds <| Ici_mem_nhds h @[deprecated (since := "2024-12-22")] alias nhdsWithin_Icc_eq_nhdsWithin_Iic := nhdsWithin_Icc_eq_nhdsLE @[simp] theorem nhdsWithin_Ioc_eq_nhdsLE (h : a < b) : 𝓝[Ioc a b] b = 𝓝[≤] b := nhdsWithin_inter_of_mem <| nhdsWithin_le_nhds <| Ioi_mem_nhds h @[deprecated (since := "2024-12-22")] alias nhdsWithin_Ioc_eq_nhdsWithin_Iic := nhdsWithin_Ioc_eq_nhdsLE @[simp] theorem continuousWithinAt_Icc_iff_Iic (h : a < b) : ContinuousWithinAt f (Icc a b) b ↔ ContinuousWithinAt f (Iic b) b := by simp only [ContinuousWithinAt, nhdsWithin_Icc_eq_nhdsLE h] @[simp]
theorem continuousWithinAt_Ioc_iff_Iic (h : a < b) : ContinuousWithinAt f (Ioc a b) b ↔ ContinuousWithinAt f (Iic b) b := by
Mathlib/Topology/Order/OrderClosed.lean
404
405
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import Mathlib.Data.PFunctor.Multivariate.Basic /-! # The W construction as a multivariate polynomial functor. W types are well-founded tree-like structures. They are defined as the least fixpoint of a polynomial functor. ## Main definitions * `W_mk` - constructor * `W_dest - destructor * `W_rec` - recursor: basis for defining functions by structural recursion on `P.W α` * `W_rec_eq` - defining equation for `W_rec` * `W_ind` - induction principle for `P.W α` ## Implementation notes Three views of M-types: * `wp`: polynomial functor * `W`: data type inductively defined by a triple: shape of the root, data in the root and children of the root * `W`: least fixed point of a polynomial functor Specifically, we define the polynomial functor `wp` as: * A := a tree-like structure without information in the nodes * B := given the tree-like structure `t`, `B t` is a valid path (specified inductively by `W_path`) from the root of `t` to any given node. As a result `wp α` is made of a dataless tree and a function from its valid paths to values of `α` ## Reference * Jeremy Avigad, Mario M. Carneiro and Simon Hudon. [*Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universe u v namespace MvPFunctor open TypeVec open MvFunctor variable {n : ℕ} (P : MvPFunctor.{u} (n + 1)) /-- A path from the root of a tree to one of its node -/ inductive WPath : P.last.W → Fin2 n → Type u | root (a : P.A) (f : P.last.B a → P.last.W) (i : Fin2 n) (c : P.drop.B a i) : WPath ⟨a, f⟩ i | child (a : P.A) (f : P.last.B a → P.last.W) (i : Fin2 n) (j : P.last.B a) (c : WPath (f j) i) : WPath ⟨a, f⟩ i instance WPath.inhabited (x : P.last.W) {i} [I : Inhabited (P.drop.B x.head i)] : Inhabited (WPath P x i) := ⟨match x, I with | ⟨a, f⟩, I => WPath.root a f i (@default _ I)⟩ /-- Specialized destructor on `WPath` -/ def wPathCasesOn {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : P.WPath ⟨a, f⟩ ⟹ α := by intro i x match x with | WPath.root _ _ i c => exact g' i c | WPath.child _ _ i j c => exact g j i c /-- Specialized destructor on `WPath` -/ def wPathDestLeft {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.WPath ⟨a, f⟩ ⟹ α) : P.drop.B a ⟹ α := fun i c => h i (WPath.root a f i c) /-- Specialized destructor on `WPath` -/ def wPathDestRight {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.WPath ⟨a, f⟩ ⟹ α) : ∀ j : P.last.B a, P.WPath (f j) ⟹ α := fun j i c => h i (WPath.child a f i j c) theorem wPathDestLeft_wPathCasesOn {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : P.wPathDestLeft (P.wPathCasesOn g' g) = g' := rfl theorem wPathDestRight_wPathCasesOn {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : P.wPathDestRight (P.wPathCasesOn g' g) = g := rfl theorem wPathCasesOn_eta {α : TypeVec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.WPath ⟨a, f⟩ ⟹ α) : P.wPathCasesOn (P.wPathDestLeft h) (P.wPathDestRight h) = h := by ext i x; cases x <;> rfl theorem comp_wPathCasesOn {α β : TypeVec n} (h : α ⟹ β) {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : h ⊚ P.wPathCasesOn g' g = P.wPathCasesOn (h ⊚ g') fun i => h ⊚ g i := by ext i x; cases x <;> rfl /-- Polynomial functor for the W-type of `P`. `A` is a data-less well-founded tree whereas, for a given `a : A`, `B a` is a valid path in tree `a` so that `Wp.obj α` is made of a tree and a function from its valid paths to the values it contains -/ def wp : MvPFunctor n where A := P.last.W B := P.WPath /-- W-type of `P` -/ def W (α : TypeVec n) : Type _ := P.wp α instance mvfunctorW : MvFunctor P.W := by delta MvPFunctor.W; infer_instance /-! First, describe operations on `W` as a polynomial functor. -/ /-- Constructor for `wp` -/ def wpMk {α : TypeVec n} (a : P.A) (f : P.last.B a → P.last.W) (f' : P.WPath ⟨a, f⟩ ⟹ α) : P.W α := ⟨⟨a, f⟩, f'⟩ def wpRec {α : TypeVec n} {C : Type*} (g : ∀ (a : P.A) (f : P.last.B a → P.last.W), P.WPath ⟨a, f⟩ ⟹ α → (P.last.B a → C) → C) : ∀ (x : P.last.W) (_ : P.WPath x ⟹ α), C | ⟨a, f⟩, f' => g a f f' fun i => wpRec g (f i) (P.wPathDestRight f' i) theorem wpRec_eq {α : TypeVec n} {C : Type*} (g : ∀ (a : P.A) (f : P.last.B a → P.last.W), P.WPath ⟨a, f⟩ ⟹ α → (P.last.B a → C) → C) (a : P.A) (f : P.last.B a → P.last.W) (f' : P.WPath ⟨a, f⟩ ⟹ α) : P.wpRec g ⟨a, f⟩ f' = g a f f' fun i => P.wpRec g (f i) (P.wPathDestRight f' i) := rfl -- Note: we could replace Prop by Type* and obtain a dependent recursor theorem wp_ind {α : TypeVec n} {C : ∀ x : P.last.W, P.WPath x ⟹ α → Prop} (ih : ∀ (a : P.A) (f : P.last.B a → P.last.W) (f' : P.WPath ⟨a, f⟩ ⟹ α), (∀ i : P.last.B a, C (f i) (P.wPathDestRight f' i)) → C ⟨a, f⟩ f') : ∀ (x : P.last.W) (f' : P.WPath x ⟹ α), C x f' | ⟨a, f⟩, f' => ih a f f' fun _i => wp_ind ih _ _ /-! Now think of W as defined inductively by the data ⟨a, f', f⟩ where - `a : P.A` is the shape of the top node - `f' : P.drop.B a ⟹ α` is the contents of the top node - `f : P.last.B a → P.last.W` are the subtrees -/ /-- Constructor for `W` -/ def wMk {α : TypeVec n} (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : P.W α := let g : P.last.B a → P.last.W := fun i => (f i).fst let g' : P.WPath ⟨a, g⟩ ⟹ α := P.wPathCasesOn f' fun i => (f i).snd ⟨⟨a, g⟩, g'⟩ /-- Recursor for `W` -/ def wRec {α : TypeVec n} {C : Type*} (g : ∀ a : P.A, P.drop.B a ⟹ α → (P.last.B a → P.W α) → (P.last.B a → C) → C) : P.W α → C | ⟨a, f'⟩ => let g' (a : P.A) (f : P.last.B a → P.last.W) (h : P.WPath ⟨a, f⟩ ⟹ α) (h' : P.last.B a → C) : C := g a (P.wPathDestLeft h) (fun i => ⟨f i, P.wPathDestRight h i⟩) h' P.wpRec g' a f' /-- Defining equation for the recursor of `W` -/ theorem wRec_eq {α : TypeVec n} {C : Type*} (g : ∀ a : P.A, P.drop.B a ⟹ α → (P.last.B a → P.W α) → (P.last.B a → C) → C) (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : P.wRec g (P.wMk a f' f) = g a f' f fun i => P.wRec g (f i) := by rw [wMk, wRec]; rw [wpRec_eq] dsimp only [wPathDestLeft_wPathCasesOn, wPathDestRight_wPathCasesOn] congr /-- Induction principle for `W` -/ theorem w_ind {α : TypeVec n} {C : P.W α → Prop} (ih : ∀ (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α), (∀ i, C (f i)) → C (P.wMk a f' f)) : ∀ x, C x := by intro x; obtain ⟨a, f⟩ := x apply @wp_ind n P α fun a f => C ⟨a, f⟩ intro a f f' ih' dsimp [wMk] at ih let ih'' := ih a (P.wPathDestLeft f') fun i => ⟨f i, P.wPathDestRight f' i⟩ dsimp at ih''; rw [wPathCasesOn_eta] at ih'' apply ih'' apply ih' theorem w_cases {α : TypeVec n} {C : P.W α → Prop} (ih : ∀ (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α), C (P.wMk a f' f)) : ∀ x, C x := P.w_ind fun a f' f _ih' => ih a f' f /-- W-types are functorial -/ def wMap {α β : TypeVec n} (g : α ⟹ β) : P.W α → P.W β := fun x => g <$$> x theorem wMk_eq {α : TypeVec n} (a : P.A) (f : P.last.B a → P.last.W) (g' : P.drop.B a ⟹ α) (g : ∀ j : P.last.B a, P.WPath (f j) ⟹ α) : (P.wMk a g' fun i => ⟨f i, g i⟩) = ⟨⟨a, f⟩, P.wPathCasesOn g' g⟩ := rfl theorem w_map_wMk {α β : TypeVec n} (g : α ⟹ β) (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : g <$$> P.wMk a f' f = P.wMk a (g ⊚ f') fun i => g <$$> f i := by show _ = P.wMk a (g ⊚ f') (MvFunctor.map g ∘ f) have : MvFunctor.map g ∘ f = fun i => ⟨(f i).fst, g ⊚ (f i).snd⟩ := by ext i : 1 dsimp [Function.comp_def]
cases f i rfl rw [this] have : f = fun i => ⟨(f i).fst, (f i).snd⟩ := by ext1 x cases f x rfl
Mathlib/Data/PFunctor/Multivariate/W.lean
206
212
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Order.PropInstances import Mathlib.Order.GaloisConnection.Defs /-! # Heyting algebras This file defines Heyting, co-Heyting and bi-Heyting algebras. A Heyting algebra is a bounded distributive lattice with an implication operation `⇨` such that `a ≤ b ⇨ c ↔ a ⊓ b ≤ c`. It also comes with a pseudo-complement `ᶜ`, such that `aᶜ = a ⇨ ⊥`. Co-Heyting algebras are dual to Heyting algebras. They have a difference `\` and a negation `¬` such that `a \ b ≤ c ↔ a ≤ b ⊔ c` and `¬a = ⊤ \ a`. Bi-Heyting algebras are Heyting algebras that are also co-Heyting algebras. From a logic standpoint, Heyting algebras precisely model intuitionistic logic, whereas boolean algebras model classical logic. Heyting algebras are the order theoretic equivalent of cartesian-closed categories. ## Main declarations * `GeneralizedHeytingAlgebra`: Heyting algebra without a top element (nor negation). * `GeneralizedCoheytingAlgebra`: Co-Heyting algebra without a bottom element (nor complement). * `HeytingAlgebra`: Heyting algebra. * `CoheytingAlgebra`: Co-Heyting algebra. * `BiheytingAlgebra`: bi-Heyting algebra. ## References * [Francis Borceux, *Handbook of Categorical Algebra III*][borceux-vol3] ## Tags Heyting, Brouwer, algebra, implication, negation, intuitionistic -/ assert_not_exists RelIso open Function OrderDual universe u variable {ι α β : Type*} /-! ### Notation -/ section variable (α β) instance Prod.instHImp [HImp α] [HImp β] : HImp (α × β) := ⟨fun a b => (a.1 ⇨ b.1, a.2 ⇨ b.2)⟩ instance Prod.instHNot [HNot α] [HNot β] : HNot (α × β) := ⟨fun a => (¬a.1, ¬a.2)⟩ instance Prod.instSDiff [SDiff α] [SDiff β] : SDiff (α × β) := ⟨fun a b => (a.1 \ b.1, a.2 \ b.2)⟩ instance Prod.instHasCompl [HasCompl α] [HasCompl β] : HasCompl (α × β) := ⟨fun a => (a.1ᶜ, a.2ᶜ)⟩ end @[simp] theorem fst_himp [HImp α] [HImp β] (a b : α × β) : (a ⇨ b).1 = a.1 ⇨ b.1 := rfl @[simp] theorem snd_himp [HImp α] [HImp β] (a b : α × β) : (a ⇨ b).2 = a.2 ⇨ b.2 := rfl @[simp] theorem fst_hnot [HNot α] [HNot β] (a : α × β) : (¬a).1 = ¬a.1 := rfl @[simp] theorem snd_hnot [HNot α] [HNot β] (a : α × β) : (¬a).2 = ¬a.2 := rfl @[simp] theorem fst_sdiff [SDiff α] [SDiff β] (a b : α × β) : (a \ b).1 = a.1 \ b.1 := rfl @[simp] theorem snd_sdiff [SDiff α] [SDiff β] (a b : α × β) : (a \ b).2 = a.2 \ b.2 := rfl @[simp] theorem fst_compl [HasCompl α] [HasCompl β] (a : α × β) : aᶜ.1 = a.1ᶜ := rfl @[simp] theorem snd_compl [HasCompl α] [HasCompl β] (a : α × β) : aᶜ.2 = a.2ᶜ := rfl namespace Pi variable {π : ι → Type*} instance [∀ i, HImp (π i)] : HImp (∀ i, π i) := ⟨fun a b i => a i ⇨ b i⟩ instance [∀ i, HNot (π i)] : HNot (∀ i, π i) := ⟨fun a i => ¬a i⟩ theorem himp_def [∀ i, HImp (π i)] (a b : ∀ i, π i) : a ⇨ b = fun i => a i ⇨ b i := rfl theorem hnot_def [∀ i, HNot (π i)] (a : ∀ i, π i) : ¬a = fun i => ¬a i := rfl @[simp] theorem himp_apply [∀ i, HImp (π i)] (a b : ∀ i, π i) (i : ι) : (a ⇨ b) i = a i ⇨ b i := rfl @[simp] theorem hnot_apply [∀ i, HNot (π i)] (a : ∀ i, π i) (i : ι) : (¬a) i = ¬a i := rfl end Pi /-- A generalized Heyting algebra is a lattice with an additional binary operation `⇨` called Heyting implication such that `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)`. This generalizes `HeytingAlgebra` by not requiring a bottom element. -/ class GeneralizedHeytingAlgebra (α : Type*) extends Lattice α, OrderTop α, HImp α where /-- `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)` -/ le_himp_iff (a b c : α) : a ≤ b ⇨ c ↔ a ⊓ b ≤ c /-- A generalized co-Heyting algebra is a lattice with an additional binary difference operation `\` such that `(· \ a)` is left adjoint to `(· ⊔ a)`. This generalizes `CoheytingAlgebra` by not requiring a top element. -/ class GeneralizedCoheytingAlgebra (α : Type*) extends Lattice α, OrderBot α, SDiff α where /-- `(· \ a)` is left adjoint to `(· ⊔ a)` -/ sdiff_le_iff (a b c : α) : a \ b ≤ c ↔ a ≤ b ⊔ c /-- A Heyting algebra is a bounded lattice with an additional binary operation `⇨` called Heyting implication such that `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)`. -/ class HeytingAlgebra (α : Type*) extends GeneralizedHeytingAlgebra α, OrderBot α, HasCompl α where /-- `aᶜ` is defined as `a ⇨ ⊥` -/ himp_bot (a : α) : a ⇨ ⊥ = aᶜ /-- A co-Heyting algebra is a bounded lattice with an additional binary difference operation `\` such that `(· \ a)` is left adjoint to `(· ⊔ a)`. -/ class CoheytingAlgebra (α : Type*) extends GeneralizedCoheytingAlgebra α, OrderTop α, HNot α where /-- `⊤ \ a` is `¬a` -/ top_sdiff (a : α) : ⊤ \ a = ¬a /-- A bi-Heyting algebra is a Heyting algebra that is also a co-Heyting algebra. -/ class BiheytingAlgebra (α : Type*) extends HeytingAlgebra α, SDiff α, HNot α where /-- `(· \ a)` is left adjoint to `(· ⊔ a)` -/ sdiff_le_iff (a b c : α) : a \ b ≤ c ↔ a ≤ b ⊔ c /-- `⊤ \ a` is `¬a` -/ top_sdiff (a : α) : ⊤ \ a = ¬a -- See note [lower instance priority] attribute [instance 100] GeneralizedHeytingAlgebra.toOrderTop attribute [instance 100] GeneralizedCoheytingAlgebra.toOrderBot -- See note [lower instance priority] instance (priority := 100) HeytingAlgebra.toBoundedOrder [HeytingAlgebra α] : BoundedOrder α := { bot_le := ‹HeytingAlgebra α›.bot_le } -- See note [lower instance priority] instance (priority := 100) CoheytingAlgebra.toBoundedOrder [CoheytingAlgebra α] : BoundedOrder α := { ‹CoheytingAlgebra α› with } -- See note [lower instance priority] instance (priority := 100) BiheytingAlgebra.toCoheytingAlgebra [BiheytingAlgebra α] : CoheytingAlgebra α := { ‹BiheytingAlgebra α› with } -- See note [reducible non-instances] /-- Construct a Heyting algebra from the lattice structure and Heyting implication alone. -/ abbrev HeytingAlgebra.ofHImp [DistribLattice α] [BoundedOrder α] (himp : α → α → α) (le_himp_iff : ∀ a b c, a ≤ himp b c ↔ a ⊓ b ≤ c) : HeytingAlgebra α := { ‹DistribLattice α›, ‹BoundedOrder α› with himp, compl := fun a => himp a ⊥, le_himp_iff, himp_bot := fun _ => rfl } -- See note [reducible non-instances] /-- Construct a Heyting algebra from the lattice structure and complement operator alone. -/ abbrev HeytingAlgebra.ofCompl [DistribLattice α] [BoundedOrder α] (compl : α → α) (le_himp_iff : ∀ a b c, a ≤ compl b ⊔ c ↔ a ⊓ b ≤ c) : HeytingAlgebra α where himp := (compl · ⊔ ·) compl := compl le_himp_iff := le_himp_iff himp_bot _ := sup_bot_eq _ -- See note [reducible non-instances] /-- Construct a co-Heyting algebra from the lattice structure and the difference alone. -/ abbrev CoheytingAlgebra.ofSDiff [DistribLattice α] [BoundedOrder α] (sdiff : α → α → α) (sdiff_le_iff : ∀ a b c, sdiff a b ≤ c ↔ a ≤ b ⊔ c) : CoheytingAlgebra α := { ‹DistribLattice α›, ‹BoundedOrder α› with sdiff, hnot := fun a => sdiff ⊤ a, sdiff_le_iff, top_sdiff := fun _ => rfl } -- See note [reducible non-instances] /-- Construct a co-Heyting algebra from the difference and Heyting negation alone. -/ abbrev CoheytingAlgebra.ofHNot [DistribLattice α] [BoundedOrder α] (hnot : α → α) (sdiff_le_iff : ∀ a b c, a ⊓ hnot b ≤ c ↔ a ≤ b ⊔ c) : CoheytingAlgebra α where sdiff a b := a ⊓ hnot b hnot := hnot sdiff_le_iff := sdiff_le_iff top_sdiff _ := top_inf_eq _ /-! In this section, we'll give interpretations of these results in the Heyting algebra model of intuitionistic logic,- where `≤` can be interpreted as "validates", `⇨` as "implies", `⊓` as "and", `⊔` as "or", `⊥` as "false" and `⊤` as "true". Note that we confuse `→` and `⊢` because those are the same in this logic. See also `Prop.heytingAlgebra`. -/ section GeneralizedHeytingAlgebra variable [GeneralizedHeytingAlgebra α] {a b c d : α} /-- `p → q → r ↔ p ∧ q → r` -/ @[simp] theorem le_himp_iff : a ≤ b ⇨ c ↔ a ⊓ b ≤ c := GeneralizedHeytingAlgebra.le_himp_iff _ _ _ /-- `p → q → r ↔ q ∧ p → r` -/ theorem le_himp_iff' : a ≤ b ⇨ c ↔ b ⊓ a ≤ c := by rw [le_himp_iff, inf_comm] /-- `p → q → r ↔ q → p → r` -/ theorem le_himp_comm : a ≤ b ⇨ c ↔ b ≤ a ⇨ c := by rw [le_himp_iff, le_himp_iff'] /-- `p → q → p` -/ theorem le_himp : a ≤ b ⇨ a := le_himp_iff.2 inf_le_left /-- `p → p → q ↔ p → q` -/ theorem le_himp_iff_left : a ≤ a ⇨ b ↔ a ≤ b := by rw [le_himp_iff, inf_idem] /-- `p → p` -/ @[simp] theorem himp_self : a ⇨ a = ⊤ := top_le_iff.1 <| le_himp_iff.2 inf_le_right /-- `(p → q) ∧ p → q` -/ theorem himp_inf_le : (a ⇨ b) ⊓ a ≤ b := le_himp_iff.1 le_rfl /-- `p ∧ (p → q) → q` -/ theorem inf_himp_le : a ⊓ (a ⇨ b) ≤ b := by rw [inf_comm, ← le_himp_iff] /-- `p ∧ (p → q) ↔ p ∧ q` -/ @[simp] theorem inf_himp (a b : α) : a ⊓ (a ⇨ b) = a ⊓ b := le_antisymm (le_inf inf_le_left <| by rw [inf_comm, ← le_himp_iff]) <| inf_le_inf_left _ le_himp /-- `(p → q) ∧ p ↔ q ∧ p` -/ @[simp] theorem himp_inf_self (a b : α) : (a ⇨ b) ⊓ a = b ⊓ a := by rw [inf_comm, inf_himp, inf_comm] /-- The **deduction theorem** in the Heyting algebra model of intuitionistic logic: an implication holds iff the conclusion follows from the hypothesis. -/ @[simp] theorem himp_eq_top_iff : a ⇨ b = ⊤ ↔ a ≤ b := by rw [← top_le_iff, le_himp_iff, top_inf_eq] /-- `p → true`, `true → p ↔ p` -/ @[simp] theorem himp_top : a ⇨ ⊤ = ⊤ := himp_eq_top_iff.2 le_top @[simp] theorem top_himp : ⊤ ⇨ a = a := eq_of_forall_le_iff fun b => by rw [le_himp_iff, inf_top_eq] /-- `p → q → r ↔ p ∧ q → r` -/ theorem himp_himp (a b c : α) : a ⇨ b ⇨ c = a ⊓ b ⇨ c := eq_of_forall_le_iff fun d => by simp_rw [le_himp_iff, inf_assoc] /-- `(q → r) → (p → q) → q → r` -/ theorem himp_le_himp_himp_himp : b ⇨ c ≤ (a ⇨ b) ⇨ a ⇨ c := by rw [le_himp_iff, le_himp_iff, inf_assoc, himp_inf_self, ← inf_assoc, himp_inf_self, inf_assoc] exact inf_le_left @[simp] theorem himp_inf_himp_inf_le : (b ⇨ c) ⊓ (a ⇨ b) ⊓ a ≤ c := by simpa using @himp_le_himp_himp_himp /-- `p → q → r ↔ q → p → r` -/ theorem himp_left_comm (a b c : α) : a ⇨ b ⇨ c = b ⇨ a ⇨ c := by simp_rw [himp_himp, inf_comm] @[simp] theorem himp_idem : b ⇨ b ⇨ a = b ⇨ a := by rw [himp_himp, inf_idem] theorem himp_inf_distrib (a b c : α) : a ⇨ b ⊓ c = (a ⇨ b) ⊓ (a ⇨ c) := eq_of_forall_le_iff fun d => by simp_rw [le_himp_iff, le_inf_iff, le_himp_iff] theorem sup_himp_distrib (a b c : α) : a ⊔ b ⇨ c = (a ⇨ c) ⊓ (b ⇨ c) := eq_of_forall_le_iff fun d => by rw [le_inf_iff, le_himp_comm, sup_le_iff] simp_rw [le_himp_comm] theorem himp_le_himp_left (h : a ≤ b) : c ⇨ a ≤ c ⇨ b := le_himp_iff.2 <| himp_inf_le.trans h theorem himp_le_himp_right (h : a ≤ b) : b ⇨ c ≤ a ⇨ c := le_himp_iff.2 <| (inf_le_inf_left _ h).trans himp_inf_le theorem himp_le_himp (hab : a ≤ b) (hcd : c ≤ d) : b ⇨ c ≤ a ⇨ d := (himp_le_himp_right hab).trans <| himp_le_himp_left hcd @[simp] theorem sup_himp_self_left (a b : α) : a ⊔ b ⇨ a = b ⇨ a := by rw [sup_himp_distrib, himp_self, top_inf_eq] @[simp] theorem sup_himp_self_right (a b : α) : a ⊔ b ⇨ b = a ⇨ b := by rw [sup_himp_distrib, himp_self, inf_top_eq] theorem Codisjoint.himp_eq_right (h : Codisjoint a b) : b ⇨ a = a := by conv_rhs => rw [← @top_himp _ _ a] rw [← h.eq_top, sup_himp_self_left] theorem Codisjoint.himp_eq_left (h : Codisjoint a b) : a ⇨ b = b := h.symm.himp_eq_right theorem Codisjoint.himp_inf_cancel_right (h : Codisjoint a b) : a ⇨ a ⊓ b = b := by rw [himp_inf_distrib, himp_self, top_inf_eq, h.himp_eq_left] theorem Codisjoint.himp_inf_cancel_left (h : Codisjoint a b) : b ⇨ a ⊓ b = a := by rw [himp_inf_distrib, himp_self, inf_top_eq, h.himp_eq_right] /-- See `himp_le` for a stronger version in Boolean algebras. -/ theorem Codisjoint.himp_le_of_right_le (hac : Codisjoint a c) (hba : b ≤ a) : c ⇨ b ≤ a := (himp_le_himp_left hba).trans_eq hac.himp_eq_right theorem le_himp_himp : a ≤ (a ⇨ b) ⇨ b := le_himp_iff.2 inf_himp_le @[simp] lemma himp_eq_himp_iff : b ⇨ a = a ⇨ b ↔ a = b := by simp [le_antisymm_iff] lemma himp_ne_himp_iff : b ⇨ a ≠ a ⇨ b ↔ a ≠ b := himp_eq_himp_iff.not theorem himp_triangle (a b c : α) : (a ⇨ b) ⊓ (b ⇨ c) ≤ a ⇨ c := by rw [le_himp_iff, inf_right_comm, ← le_himp_iff] exact himp_inf_le.trans le_himp_himp theorem himp_inf_himp_cancel (hba : b ≤ a) (hcb : c ≤ b) : (a ⇨ b) ⊓ (b ⇨ c) = a ⇨ c := (himp_triangle _ _ _).antisymm <| le_inf (himp_le_himp_left hcb) (himp_le_himp_right hba) theorem gc_inf_himp : GaloisConnection (a ⊓ ·) (a ⇨ ·) := fun _ _ ↦ Iff.symm le_himp_iff' -- See note [lower instance priority] instance (priority := 100) GeneralizedHeytingAlgebra.toDistribLattice : DistribLattice α := DistribLattice.ofInfSupLe fun a b c => by simp_rw [inf_comm a, ← le_himp_iff, sup_le_iff, le_himp_iff, ← sup_le_iff]; rfl instance OrderDual.instGeneralizedCoheytingAlgebra : GeneralizedCoheytingAlgebra αᵒᵈ where sdiff a b := toDual (ofDual b ⇨ ofDual a) sdiff_le_iff a b c := by rw [sup_comm]; exact le_himp_iff instance Prod.instGeneralizedHeytingAlgebra [GeneralizedHeytingAlgebra β] : GeneralizedHeytingAlgebra (α × β) where le_himp_iff _ _ _ := and_congr le_himp_iff le_himp_iff instance Pi.instGeneralizedHeytingAlgebra {α : ι → Type*} [∀ i, GeneralizedHeytingAlgebra (α i)] : GeneralizedHeytingAlgebra (∀ i, α i) where le_himp_iff i := by simp [le_def] end GeneralizedHeytingAlgebra section GeneralizedCoheytingAlgebra variable [GeneralizedCoheytingAlgebra α] {a b c d : α} @[simp] theorem sdiff_le_iff : a \ b ≤ c ↔ a ≤ b ⊔ c := GeneralizedCoheytingAlgebra.sdiff_le_iff _ _ _ theorem sdiff_le_iff' : a \ b ≤ c ↔ a ≤ c ⊔ b := by rw [sdiff_le_iff, sup_comm] theorem sdiff_le_comm : a \ b ≤ c ↔ a \ c ≤ b := by rw [sdiff_le_iff, sdiff_le_iff'] theorem sdiff_le : a \ b ≤ a := sdiff_le_iff.2 le_sup_right theorem Disjoint.disjoint_sdiff_left (h : Disjoint a b) : Disjoint (a \ c) b := h.mono_left sdiff_le theorem Disjoint.disjoint_sdiff_right (h : Disjoint a b) : Disjoint a (b \ c) := h.mono_right sdiff_le theorem sdiff_le_iff_left : a \ b ≤ b ↔ a ≤ b := by rw [sdiff_le_iff, sup_idem] @[simp] theorem sdiff_self : a \ a = ⊥ := le_bot_iff.1 <| sdiff_le_iff.2 le_sup_left theorem le_sup_sdiff : a ≤ b ⊔ a \ b := sdiff_le_iff.1 le_rfl theorem le_sdiff_sup : a ≤ a \ b ⊔ b := by rw [sup_comm, ← sdiff_le_iff] theorem sup_sdiff_left : a ⊔ a \ b = a := sup_of_le_left sdiff_le theorem sup_sdiff_right : a \ b ⊔ a = a := sup_of_le_right sdiff_le theorem inf_sdiff_left : a \ b ⊓ a = a \ b := inf_of_le_left sdiff_le theorem inf_sdiff_right : a ⊓ a \ b = a \ b := inf_of_le_right sdiff_le @[simp] theorem sup_sdiff_self (a b : α) : a ⊔ b \ a = a ⊔ b := le_antisymm (sup_le_sup_left sdiff_le _) (sup_le le_sup_left le_sup_sdiff) @[simp] theorem sdiff_sup_self (a b : α) : b \ a ⊔ a = b ⊔ a := by rw [sup_comm, sup_sdiff_self, sup_comm] alias sup_sdiff_self_left := sdiff_sup_self alias sup_sdiff_self_right := sup_sdiff_self theorem sup_sdiff_eq_sup (h : c ≤ a) : a ⊔ b \ c = a ⊔ b := sup_congr_left (sdiff_le.trans le_sup_right) <| le_sup_sdiff.trans <| sup_le_sup_right h _ -- cf. `Set.union_diff_cancel'` theorem sup_sdiff_cancel' (hab : a ≤ b) (hbc : b ≤ c) : b ⊔ c \ a = c := by rw [sup_sdiff_eq_sup hab, sup_of_le_right hbc] theorem sup_sdiff_cancel_right (h : a ≤ b) : a ⊔ b \ a = b := sup_sdiff_cancel' le_rfl h theorem sdiff_sup_cancel (h : b ≤ a) : a \ b ⊔ b = a := by rw [sup_comm, sup_sdiff_cancel_right h] theorem sup_le_of_le_sdiff_left (h : b ≤ c \ a) (hac : a ≤ c) : a ⊔ b ≤ c := sup_le hac <| h.trans sdiff_le theorem sup_le_of_le_sdiff_right (h : a ≤ c \ b) (hbc : b ≤ c) : a ⊔ b ≤ c := sup_le (h.trans sdiff_le) hbc @[simp] theorem sdiff_eq_bot_iff : a \ b = ⊥ ↔ a ≤ b := by rw [← le_bot_iff, sdiff_le_iff, sup_bot_eq] @[simp] theorem sdiff_bot : a \ ⊥ = a := eq_of_forall_ge_iff fun b => by rw [sdiff_le_iff, bot_sup_eq] @[simp] theorem bot_sdiff : ⊥ \ a = ⊥ := sdiff_eq_bot_iff.2 bot_le theorem sdiff_sdiff_sdiff_le_sdiff : (a \ b) \ (a \ c) ≤ c \ b := by rw [sdiff_le_iff, sdiff_le_iff, sup_left_comm, sup_sdiff_self, sup_left_comm, sdiff_sup_self, sup_left_comm] exact le_sup_left @[simp] theorem le_sup_sdiff_sup_sdiff : a ≤ b ⊔ (a \ c ⊔ c \ b) := by simpa using @sdiff_sdiff_sdiff_le_sdiff theorem sdiff_sdiff (a b c : α) : (a \ b) \ c = a \ (b ⊔ c) := eq_of_forall_ge_iff fun d => by simp_rw [sdiff_le_iff, sup_assoc] theorem sdiff_sdiff_left : (a \ b) \ c = a \ (b ⊔ c) := sdiff_sdiff _ _ _ theorem sdiff_right_comm (a b c : α) : (a \ b) \ c = (a \ c) \ b := by simp_rw [sdiff_sdiff, sup_comm] theorem sdiff_sdiff_comm : (a \ b) \ c = (a \ c) \ b := sdiff_right_comm _ _ _ @[simp] theorem sdiff_idem : (a \ b) \ b = a \ b := by rw [sdiff_sdiff_left, sup_idem] @[simp] theorem sdiff_sdiff_self : (a \ b) \ a = ⊥ := by rw [sdiff_sdiff_comm, sdiff_self, bot_sdiff] theorem sup_sdiff_distrib (a b c : α) : (a ⊔ b) \ c = a \ c ⊔ b \ c := eq_of_forall_ge_iff fun d => by simp_rw [sdiff_le_iff, sup_le_iff, sdiff_le_iff] theorem sdiff_inf_distrib (a b c : α) : a \ (b ⊓ c) = a \ b ⊔ a \ c := eq_of_forall_ge_iff fun d => by rw [sup_le_iff, sdiff_le_comm, le_inf_iff] simp_rw [sdiff_le_comm] theorem sup_sdiff : (a ⊔ b) \ c = a \ c ⊔ b \ c := sup_sdiff_distrib _ _ _ @[simp] theorem sup_sdiff_right_self : (a ⊔ b) \ b = a \ b := by rw [sup_sdiff, sdiff_self, sup_bot_eq] @[simp] theorem sup_sdiff_left_self : (a ⊔ b) \ a = b \ a := by rw [sup_comm, sup_sdiff_right_self] @[gcongr] theorem sdiff_le_sdiff_right (h : a ≤ b) : a \ c ≤ b \ c := sdiff_le_iff.2 <| h.trans <| le_sup_sdiff @[gcongr] theorem sdiff_le_sdiff_left (h : a ≤ b) : c \ b ≤ c \ a := sdiff_le_iff.2 <| le_sup_sdiff.trans <| sup_le_sup_right h _ @[gcongr] theorem sdiff_le_sdiff (hab : a ≤ b) (hcd : c ≤ d) : a \ d ≤ b \ c := (sdiff_le_sdiff_right hab).trans <| sdiff_le_sdiff_left hcd -- cf. `IsCompl.inf_sup` theorem sdiff_inf : a \ (b ⊓ c) = a \ b ⊔ a \ c := sdiff_inf_distrib _ _ _ @[simp] theorem sdiff_inf_self_left (a b : α) : a \ (a ⊓ b) = a \ b := by rw [sdiff_inf, sdiff_self, bot_sup_eq] @[simp] theorem sdiff_inf_self_right (a b : α) : b \ (a ⊓ b) = b \ a := by rw [sdiff_inf, sdiff_self, sup_bot_eq] theorem Disjoint.sdiff_eq_left (h : Disjoint a b) : a \ b = a := by conv_rhs => rw [← @sdiff_bot _ _ a] rw [← h.eq_bot, sdiff_inf_self_left] theorem Disjoint.sdiff_eq_right (h : Disjoint a b) : b \ a = b := h.symm.sdiff_eq_left theorem Disjoint.sup_sdiff_cancel_left (h : Disjoint a b) : (a ⊔ b) \ a = b := by rw [sup_sdiff, sdiff_self, bot_sup_eq, h.sdiff_eq_right] theorem Disjoint.sup_sdiff_cancel_right (h : Disjoint a b) : (a ⊔ b) \ b = a := by rw [sup_sdiff, sdiff_self, sup_bot_eq, h.sdiff_eq_left] /-- See `le_sdiff` for a stronger version in generalised Boolean algebras. -/ theorem Disjoint.le_sdiff_of_le_left (hac : Disjoint a c) (hab : a ≤ b) : a ≤ b \ c := hac.sdiff_eq_left.ge.trans <| sdiff_le_sdiff_right hab theorem sdiff_sdiff_le : a \ (a \ b) ≤ b := sdiff_le_iff.2 le_sdiff_sup @[simp] lemma sdiff_eq_sdiff_iff : a \ b = b \ a ↔ a = b := by simp [le_antisymm_iff] lemma sdiff_ne_sdiff_iff : a \ b ≠ b \ a ↔ a ≠ b := sdiff_eq_sdiff_iff.not theorem sdiff_triangle (a b c : α) : a \ c ≤ a \ b ⊔ b \ c := by rw [sdiff_le_iff, sup_left_comm, ← sdiff_le_iff] exact sdiff_sdiff_le.trans le_sup_sdiff theorem sdiff_sup_sdiff_cancel (hba : b ≤ a) (hcb : c ≤ b) : a \ b ⊔ b \ c = a \ c := (sdiff_triangle _ _ _).antisymm' <| sup_le (sdiff_le_sdiff_left hcb) (sdiff_le_sdiff_right hba) /-- a version of `sdiff_sup_sdiff_cancel` with more general hypotheses. -/ theorem sdiff_sup_sdiff_cancel' (hinf : a ⊓ c ≤ b) (hsup : b ≤ a ⊔ c) : a \ b ⊔ b \ c = a \ c := by refine (sdiff_triangle ..).antisymm' <| sup_le ?_ <| by simpa [sup_comm] rw [← sdiff_inf_self_left (b := c)] exact sdiff_le_sdiff_left hinf theorem sdiff_le_sdiff_of_sup_le_sup_left (h : c ⊔ a ≤ c ⊔ b) : a \ c ≤ b \ c := by rw [← sup_sdiff_left_self, ← @sup_sdiff_left_self _ _ _ b] exact sdiff_le_sdiff_right h theorem sdiff_le_sdiff_of_sup_le_sup_right (h : a ⊔ c ≤ b ⊔ c) : a \ c ≤ b \ c := by rw [← sup_sdiff_right_self, ← @sup_sdiff_right_self _ _ b] exact sdiff_le_sdiff_right h @[simp] theorem inf_sdiff_sup_left : a \ c ⊓ (a ⊔ b) = a \ c := inf_of_le_left <| sdiff_le.trans le_sup_left @[simp] theorem inf_sdiff_sup_right : a \ c ⊓ (b ⊔ a) = a \ c := inf_of_le_left <| sdiff_le.trans le_sup_right theorem gc_sdiff_sup : GaloisConnection (· \ a) (a ⊔ ·) := fun _ _ ↦ sdiff_le_iff -- See note [lower instance priority] instance (priority := 100) GeneralizedCoheytingAlgebra.toDistribLattice : DistribLattice α := { ‹GeneralizedCoheytingAlgebra α› with le_sup_inf := fun a b c => by simp_rw [← sdiff_le_iff, le_inf_iff, sdiff_le_iff, ← le_inf_iff]; rfl } instance OrderDual.instGeneralizedHeytingAlgebra : GeneralizedHeytingAlgebra αᵒᵈ where himp := fun a b => toDual (ofDual b \ ofDual a) le_himp_iff := fun a b c => by rw [inf_comm]; exact sdiff_le_iff instance Prod.instGeneralizedCoheytingAlgebra [GeneralizedCoheytingAlgebra β] : GeneralizedCoheytingAlgebra (α × β) where sdiff_le_iff _ _ _ := and_congr sdiff_le_iff sdiff_le_iff instance Pi.instGeneralizedCoheytingAlgebra {α : ι → Type*} [∀ i, GeneralizedCoheytingAlgebra (α i)] : GeneralizedCoheytingAlgebra (∀ i, α i) where sdiff_le_iff i := by simp [le_def] end GeneralizedCoheytingAlgebra section HeytingAlgebra variable [HeytingAlgebra α] {a b : α} @[simp] theorem himp_bot (a : α) : a ⇨ ⊥ = aᶜ := HeytingAlgebra.himp_bot _ @[simp] theorem bot_himp (a : α) : ⊥ ⇨ a = ⊤ := himp_eq_top_iff.2 bot_le theorem compl_sup_distrib (a b : α) : (a ⊔ b)ᶜ = aᶜ ⊓ bᶜ := by simp_rw [← himp_bot, sup_himp_distrib] @[simp] theorem compl_sup : (a ⊔ b)ᶜ = aᶜ ⊓ bᶜ := compl_sup_distrib _ _ theorem compl_le_himp : aᶜ ≤ a ⇨ b := (himp_bot _).ge.trans <| himp_le_himp_left bot_le theorem compl_sup_le_himp : aᶜ ⊔ b ≤ a ⇨ b := sup_le compl_le_himp le_himp theorem sup_compl_le_himp : b ⊔ aᶜ ≤ a ⇨ b := sup_le le_himp compl_le_himp -- `p → ¬ p ↔ ¬ p` @[simp] theorem himp_compl (a : α) : a ⇨ aᶜ = aᶜ := by rw [← himp_bot, himp_himp, inf_idem] -- `p → ¬ q ↔ q → ¬ p` theorem himp_compl_comm (a b : α) : a ⇨ bᶜ = b ⇨ aᶜ := by simp_rw [← himp_bot, himp_left_comm] theorem le_compl_iff_disjoint_right : a ≤ bᶜ ↔ Disjoint a b := by rw [← himp_bot, le_himp_iff, disjoint_iff_inf_le] theorem le_compl_iff_disjoint_left : a ≤ bᶜ ↔ Disjoint b a := le_compl_iff_disjoint_right.trans disjoint_comm theorem le_compl_comm : a ≤ bᶜ ↔ b ≤ aᶜ := by rw [le_compl_iff_disjoint_right, le_compl_iff_disjoint_left] alias ⟨_, Disjoint.le_compl_right⟩ := le_compl_iff_disjoint_right alias ⟨_, Disjoint.le_compl_left⟩ := le_compl_iff_disjoint_left alias le_compl_iff_le_compl := le_compl_comm alias ⟨le_compl_of_le_compl, _⟩ := le_compl_comm theorem disjoint_compl_left : Disjoint aᶜ a := disjoint_iff_inf_le.mpr <| le_himp_iff.1 (himp_bot _).ge theorem disjoint_compl_right : Disjoint a aᶜ := disjoint_compl_left.symm theorem LE.le.disjoint_compl_left (h : b ≤ a) : Disjoint aᶜ b := _root_.disjoint_compl_left.mono_right h theorem LE.le.disjoint_compl_right (h : a ≤ b) : Disjoint a bᶜ := _root_.disjoint_compl_right.mono_left h theorem IsCompl.compl_eq (h : IsCompl a b) : aᶜ = b := h.1.le_compl_left.antisymm' <| Disjoint.le_of_codisjoint disjoint_compl_left h.2 theorem IsCompl.eq_compl (h : IsCompl a b) : a = bᶜ := h.1.le_compl_right.antisymm <| Disjoint.le_of_codisjoint disjoint_compl_left h.2.symm theorem compl_unique (h₀ : a ⊓ b = ⊥) (h₁ : a ⊔ b = ⊤) : aᶜ = b := (IsCompl.of_eq h₀ h₁).compl_eq @[simp] theorem inf_compl_self (a : α) : a ⊓ aᶜ = ⊥ := disjoint_compl_right.eq_bot @[simp] theorem compl_inf_self (a : α) : aᶜ ⊓ a = ⊥ := disjoint_compl_left.eq_bot theorem inf_compl_eq_bot : a ⊓ aᶜ = ⊥ := inf_compl_self _ theorem compl_inf_eq_bot : aᶜ ⊓ a = ⊥ := compl_inf_self _ @[simp] theorem compl_top : (⊤ : α)ᶜ = ⊥ := eq_of_forall_le_iff fun a => by rw [le_compl_iff_disjoint_right, disjoint_top, le_bot_iff] @[simp] theorem compl_bot : (⊥ : α)ᶜ = ⊤ := by rw [← himp_bot, himp_self] @[simp] theorem le_compl_self : a ≤ aᶜ ↔ a = ⊥ := by rw [le_compl_iff_disjoint_left, disjoint_self] @[simp] theorem ne_compl_self [Nontrivial α] : a ≠ aᶜ := by intro h cases le_compl_self.1 (le_of_eq h) simp at h @[simp] theorem compl_ne_self [Nontrivial α] : aᶜ ≠ a := ne_comm.1 ne_compl_self @[simp] theorem lt_compl_self [Nontrivial α] : a < aᶜ ↔ a = ⊥ := by rw [lt_iff_le_and_ne]; simp theorem le_compl_compl : a ≤ aᶜᶜ := disjoint_compl_right.le_compl_right theorem compl_anti : Antitone (compl : α → α) := fun _ _ h => le_compl_comm.1 <| h.trans le_compl_compl @[gcongr] theorem compl_le_compl (h : a ≤ b) : bᶜ ≤ aᶜ := compl_anti h @[simp] theorem compl_compl_compl (a : α) : aᶜᶜᶜ = aᶜ := (compl_anti le_compl_compl).antisymm le_compl_compl @[simp] theorem disjoint_compl_compl_left_iff : Disjoint aᶜᶜ b ↔ Disjoint a b := by simp_rw [← le_compl_iff_disjoint_left, compl_compl_compl] @[simp] theorem disjoint_compl_compl_right_iff : Disjoint a bᶜᶜ ↔ Disjoint a b := by simp_rw [← le_compl_iff_disjoint_right, compl_compl_compl] theorem compl_sup_compl_le : aᶜ ⊔ bᶜ ≤ (a ⊓ b)ᶜ := sup_le (compl_anti inf_le_left) <| compl_anti inf_le_right theorem compl_compl_inf_distrib (a b : α) : (a ⊓ b)ᶜᶜ = aᶜᶜ ⊓ bᶜᶜ := by refine ((compl_anti compl_sup_compl_le).trans (compl_sup_distrib _ _).le).antisymm ?_ rw [le_compl_iff_disjoint_right, disjoint_assoc, disjoint_compl_compl_left_iff, disjoint_left_comm, disjoint_compl_compl_left_iff, ← disjoint_assoc, inf_comm] exact disjoint_compl_right theorem compl_compl_himp_distrib (a b : α) : (a ⇨ b)ᶜᶜ = aᶜᶜ ⇨ bᶜᶜ := by apply le_antisymm · rw [le_himp_iff, ← compl_compl_inf_distrib] exact compl_anti (compl_anti himp_inf_le) · refine le_compl_comm.1 ((compl_anti compl_sup_le_himp).trans ?_) rw [compl_sup_distrib, le_compl_iff_disjoint_right, disjoint_right_comm, ← le_compl_iff_disjoint_right] exact inf_himp_le instance OrderDual.instCoheytingAlgebra : CoheytingAlgebra αᵒᵈ where hnot := toDual ∘ compl ∘ ofDual sdiff a b := toDual (ofDual b ⇨ ofDual a) sdiff_le_iff a b c := by rw [sup_comm]; exact le_himp_iff top_sdiff := @himp_bot α _ @[simp] theorem ofDual_hnot (a : αᵒᵈ) : ofDual (¬a) = (ofDual a)ᶜ := rfl @[simp] theorem toDual_compl (a : α) : toDual aᶜ = ¬toDual a := rfl instance Prod.instHeytingAlgebra [HeytingAlgebra β] : HeytingAlgebra (α × β) where himp_bot a := Prod.ext_iff.2 ⟨himp_bot a.1, himp_bot a.2⟩ instance Pi.instHeytingAlgebra {α : ι → Type*} [∀ i, HeytingAlgebra (α i)] : HeytingAlgebra (∀ i, α i) where himp_bot f := funext fun i ↦ himp_bot (f i) end HeytingAlgebra section CoheytingAlgebra variable [CoheytingAlgebra α] {a b : α} @[simp] theorem top_sdiff' (a : α) : ⊤ \ a = ¬a := CoheytingAlgebra.top_sdiff _ @[simp] theorem sdiff_top (a : α) : a \ ⊤ = ⊥ := sdiff_eq_bot_iff.2 le_top theorem hnot_inf_distrib (a b : α) : ¬(a ⊓ b) = ¬a ⊔ ¬b := by simp_rw [← top_sdiff', sdiff_inf_distrib] theorem sdiff_le_hnot : a \ b ≤ ¬b := (sdiff_le_sdiff_right le_top).trans_eq <| top_sdiff' _ theorem sdiff_le_inf_hnot : a \ b ≤ a ⊓ ¬b := le_inf sdiff_le sdiff_le_hnot -- See note [lower instance priority] instance (priority := 100) CoheytingAlgebra.toDistribLattice : DistribLattice α := { ‹CoheytingAlgebra α› with le_sup_inf := fun a b c => by simp_rw [← sdiff_le_iff, le_inf_iff, sdiff_le_iff, ← le_inf_iff]; rfl } @[simp] theorem hnot_sdiff (a : α) : ¬a \ a = ¬a := by rw [← top_sdiff', sdiff_sdiff, sup_idem] theorem hnot_sdiff_comm (a b : α) : ¬a \ b = ¬b \ a := by simp_rw [← top_sdiff', sdiff_right_comm] theorem hnot_le_iff_codisjoint_right : ¬a ≤ b ↔ Codisjoint a b := by rw [← top_sdiff', sdiff_le_iff, codisjoint_iff_le_sup] theorem hnot_le_iff_codisjoint_left : ¬a ≤ b ↔ Codisjoint b a := hnot_le_iff_codisjoint_right.trans codisjoint_comm theorem hnot_le_comm : ¬a ≤ b ↔ ¬b ≤ a := by rw [hnot_le_iff_codisjoint_right, hnot_le_iff_codisjoint_left] alias ⟨_, Codisjoint.hnot_le_right⟩ := hnot_le_iff_codisjoint_right alias ⟨_, Codisjoint.hnot_le_left⟩ := hnot_le_iff_codisjoint_left theorem codisjoint_hnot_right : Codisjoint a (¬a) := codisjoint_iff_le_sup.2 <| sdiff_le_iff.1 (top_sdiff' _).le theorem codisjoint_hnot_left : Codisjoint (¬a) a := codisjoint_hnot_right.symm theorem LE.le.codisjoint_hnot_left (h : a ≤ b) : Codisjoint (¬a) b := _root_.codisjoint_hnot_left.mono_right h theorem LE.le.codisjoint_hnot_right (h : b ≤ a) : Codisjoint a (¬b) := _root_.codisjoint_hnot_right.mono_left h theorem IsCompl.hnot_eq (h : IsCompl a b) : ¬a = b := h.2.hnot_le_right.antisymm <| Disjoint.le_of_codisjoint h.1.symm codisjoint_hnot_right theorem IsCompl.eq_hnot (h : IsCompl a b) : a = ¬b := h.2.hnot_le_left.antisymm' <| Disjoint.le_of_codisjoint h.1 codisjoint_hnot_right @[simp] theorem sup_hnot_self (a : α) : a ⊔ ¬a = ⊤ := Codisjoint.eq_top codisjoint_hnot_right @[simp] theorem hnot_sup_self (a : α) : ¬a ⊔ a = ⊤ := Codisjoint.eq_top codisjoint_hnot_left @[simp] theorem hnot_bot : ¬(⊥ : α) = ⊤ := eq_of_forall_ge_iff fun a => by rw [hnot_le_iff_codisjoint_left, codisjoint_bot, top_le_iff] @[simp] theorem hnot_top : ¬(⊤ : α) = ⊥ := by rw [← top_sdiff', sdiff_self] theorem hnot_hnot_le : ¬¬a ≤ a := codisjoint_hnot_right.hnot_le_left theorem hnot_anti : Antitone (hnot : α → α) := fun _ _ h => hnot_le_comm.1 <| hnot_hnot_le.trans h theorem hnot_le_hnot (h : a ≤ b) : ¬b ≤ ¬a := hnot_anti h @[simp] theorem hnot_hnot_hnot (a : α) : ¬¬¬a = ¬a := hnot_hnot_le.antisymm <| hnot_anti hnot_hnot_le @[simp] theorem codisjoint_hnot_hnot_left_iff : Codisjoint (¬¬a) b ↔ Codisjoint a b := by simp_rw [← hnot_le_iff_codisjoint_right, hnot_hnot_hnot] @[simp] theorem codisjoint_hnot_hnot_right_iff : Codisjoint a (¬¬b) ↔ Codisjoint a b := by simp_rw [← hnot_le_iff_codisjoint_left, hnot_hnot_hnot] theorem le_hnot_inf_hnot : ¬(a ⊔ b) ≤ ¬a ⊓ ¬b := le_inf (hnot_anti le_sup_left) <| hnot_anti le_sup_right theorem hnot_hnot_sup_distrib (a b : α) : ¬¬(a ⊔ b) = ¬¬a ⊔ ¬¬b := by refine ((hnot_inf_distrib _ _).ge.trans <| hnot_anti le_hnot_inf_hnot).antisymm' ?_ rw [hnot_le_iff_codisjoint_left, codisjoint_assoc, codisjoint_hnot_hnot_left_iff, codisjoint_left_comm, codisjoint_hnot_hnot_left_iff, ← codisjoint_assoc, sup_comm] exact codisjoint_hnot_right theorem hnot_hnot_sdiff_distrib (a b : α) : ¬¬(a \ b) = ¬¬a \ ¬¬b := by apply le_antisymm · refine hnot_le_comm.1 ((hnot_anti sdiff_le_inf_hnot).trans' ?_) rw [hnot_inf_distrib, hnot_le_iff_codisjoint_right, codisjoint_left_comm, ← hnot_le_iff_codisjoint_right] exact le_sdiff_sup · rw [sdiff_le_iff, ← hnot_hnot_sup_distrib] exact hnot_anti (hnot_anti le_sup_sdiff) instance OrderDual.instHeytingAlgebra : HeytingAlgebra αᵒᵈ where compl := toDual ∘ hnot ∘ ofDual himp a b := toDual (ofDual b \ ofDual a) le_himp_iff a b c := by rw [inf_comm]; exact sdiff_le_iff himp_bot := @top_sdiff' α _ @[simp] theorem ofDual_compl (a : αᵒᵈ) : ofDual aᶜ = ¬ofDual a := rfl @[simp] theorem ofDual_himp (a b : αᵒᵈ) : ofDual (a ⇨ b) = ofDual b \ ofDual a := rfl @[simp] theorem toDual_hnot (a : α) : toDual (¬a) = (toDual a)ᶜ := rfl @[simp] theorem toDual_sdiff (a b : α) : toDual (a \ b) = toDual b ⇨ toDual a := rfl instance Prod.instCoheytingAlgebra [CoheytingAlgebra β] : CoheytingAlgebra (α × β) where sdiff_le_iff _ _ _ := and_congr sdiff_le_iff sdiff_le_iff top_sdiff a := Prod.ext_iff.2 ⟨top_sdiff' a.1, top_sdiff' a.2⟩ instance Pi.instCoheytingAlgebra {α : ι → Type*} [∀ i, CoheytingAlgebra (α i)] : CoheytingAlgebra (∀ i, α i) where top_sdiff f := funext fun i ↦ top_sdiff' (f i) end CoheytingAlgebra section BiheytingAlgebra variable [BiheytingAlgebra α] {a : α} theorem compl_le_hnot : aᶜ ≤ ¬a := (disjoint_compl_left : Disjoint _ a).le_of_codisjoint codisjoint_hnot_right end BiheytingAlgebra /-- Propositions form a Heyting algebra with implication as Heyting implication and negation as complement. -/ instance Prop.instHeytingAlgebra : HeytingAlgebra Prop := { Prop.instDistribLattice, Prop.instBoundedOrder with himp := (· → ·), le_himp_iff := fun _ _ _ => and_imp.symm, himp_bot := fun _ => rfl } @[simp] theorem himp_iff_imp (p q : Prop) : p ⇨ q ↔ p → q := Iff.rfl @[simp] theorem compl_iff_not (p : Prop) : pᶜ ↔ ¬p := Iff.rfl -- See note [reducible non-instances] /-- A bounded linear order is a bi-Heyting algebra by setting * `a ⇨ b = ⊤` if `a ≤ b` and `a ⇨ b = b` otherwise. * `a \ b = ⊥` if `a ≤ b` and `a \ b = a` otherwise. -/ abbrev LinearOrder.toBiheytingAlgebra [LinearOrder α] [BoundedOrder α] : BiheytingAlgebra α := { LinearOrder.toLattice, ‹BoundedOrder α› with himp := fun a b => if a ≤ b then ⊤ else b, compl := fun a => if a = ⊥ then ⊤ else ⊥, le_himp_iff := fun a b c => by split_ifs with h · exact iff_of_true le_top (inf_le_of_right_le h) · rw [inf_le_iff, or_iff_left h], himp_bot := fun _ => if_congr le_bot_iff rfl rfl, sdiff := fun a b => if a ≤ b then ⊥ else a, hnot := fun a => if a = ⊤ then ⊥ else ⊤, sdiff_le_iff := fun a b c => by split_ifs with h · exact iff_of_true bot_le (le_sup_of_le_left h) · rw [le_sup_iff, or_iff_right h], top_sdiff := fun _ => if_congr top_le_iff rfl rfl } instance OrderDual.instBiheytingAlgebra [BiheytingAlgebra α] : BiheytingAlgebra αᵒᵈ where __ := instHeytingAlgebra __ := instCoheytingAlgebra instance Prod.instBiheytingAlgebra [BiheytingAlgebra α] [BiheytingAlgebra β] : BiheytingAlgebra (α × β) where __ := instHeytingAlgebra __ := instCoheytingAlgebra instance Pi.instBiheytingAlgebra {α : ι → Type*} [∀ i, BiheytingAlgebra (α i)] : BiheytingAlgebra (∀ i, α i) where __ := instHeytingAlgebra __ := instCoheytingAlgebra section lift -- See note [reducible non-instances] /-- Pullback a `GeneralizedHeytingAlgebra` along an injection. -/ protected abbrev Function.Injective.generalizedHeytingAlgebra [Max α] [Min α] [Top α] [HImp α] [GeneralizedHeytingAlgebra β] (f : α → β) (hf : Injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_top : f ⊤ = ⊤) (map_himp : ∀ a b, f (a ⇨ b) = f a ⇨ f b) : GeneralizedHeytingAlgebra α := { __ := hf.lattice f map_sup map_inf __ := ‹Top α› __ := ‹HImp α› le_top := fun a => by change f _ ≤ _ rw [map_top] exact le_top, le_himp_iff := fun a b c => by change f _ ≤ _ ↔ f _ ≤ _ rw [map_himp, map_inf, le_himp_iff] } -- See note [reducible non-instances] /-- Pullback a `GeneralizedCoheytingAlgebra` along an injection. -/ protected abbrev Function.Injective.generalizedCoheytingAlgebra [Max α] [Min α] [Bot α] [SDiff α] [GeneralizedCoheytingAlgebra β] (f : α → β) (hf : Injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_bot : f ⊥ = ⊥) (map_sdiff : ∀ a b, f (a \ b) = f a \ f b) : GeneralizedCoheytingAlgebra α := { __ := hf.lattice f map_sup map_inf __ := ‹Bot α› __ := ‹SDiff α› bot_le := fun a => by change f _ ≤ _ rw [map_bot] exact bot_le, sdiff_le_iff := fun a b c => by change f _ ≤ _ ↔ f _ ≤ _ rw [map_sdiff, map_sup, sdiff_le_iff] } -- See note [reducible non-instances] /-- Pullback a `HeytingAlgebra` along an injection. -/ protected abbrev Function.Injective.heytingAlgebra [Max α] [Min α] [Top α] [Bot α] [HasCompl α] [HImp α] [HeytingAlgebra β] (f : α → β) (hf : Injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_top : f ⊤ = ⊤) (map_bot : f ⊥ = ⊥) (map_compl : ∀ a, f aᶜ = (f a)ᶜ) (map_himp : ∀ a b, f (a ⇨ b) = f a ⇨ f b) : HeytingAlgebra α := { __ := hf.generalizedHeytingAlgebra f map_sup map_inf map_top map_himp __ := ‹Bot α› __ := ‹HasCompl α› bot_le := fun a => by change f _ ≤ _ rw [map_bot] exact bot_le, himp_bot := fun a => hf <| by rw [map_himp, map_compl, map_bot, himp_bot] } -- See note [reducible non-instances] /-- Pullback a `CoheytingAlgebra` along an injection. -/ protected abbrev Function.Injective.coheytingAlgebra [Max α] [Min α] [Top α] [Bot α] [HNot α] [SDiff α] [CoheytingAlgebra β] (f : α → β) (hf : Injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_top : f ⊤ = ⊤) (map_bot : f ⊥ = ⊥) (map_hnot : ∀ a, f (¬a) = ¬f a) (map_sdiff : ∀ a b, f (a \ b) = f a \ f b) : CoheytingAlgebra α := { __ := hf.generalizedCoheytingAlgebra f map_sup map_inf map_bot map_sdiff
__ := ‹Top α› __ := ‹HNot α›
Mathlib/Order/Heyting/Basic.lean
1,044
1,045
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Floris van Doorn, Gabriel Ebner, Yury Kudryashov -/ import Mathlib.Data.Set.Accumulate import Mathlib.Order.ConditionallyCompleteLattice.Finset import Mathlib.Order.Interval.Finset.Nat /-! # Conditionally complete linear order structure on `ℕ` In this file we * define a `ConditionallyCompleteLinearOrderBot` structure on `ℕ`; * prove a few lemmas about `iSup`/`iInf`/`Set.iUnion`/`Set.iInter` and natural numbers. -/ assert_not_exists MonoidWithZero open Set namespace Nat open scoped Classical in noncomputable instance : InfSet ℕ := ⟨fun s ↦ if h : ∃ n, n ∈ s then @Nat.find (fun n ↦ n ∈ s) _ h else 0⟩ open scoped Classical in noncomputable instance : SupSet ℕ := ⟨fun s ↦ if h : ∃ n, ∀ a ∈ s, a ≤ n then @Nat.find (fun n ↦ ∀ a ∈ s, a ≤ n) _ h else 0⟩ open scoped Classical in theorem sInf_def {s : Set ℕ} (h : s.Nonempty) : sInf s = @Nat.find (fun n ↦ n ∈ s) _ h := dif_pos _ open scoped Classical in theorem sSup_def {s : Set ℕ} (h : ∃ n, ∀ a ∈ s, a ≤ n) : sSup s = @Nat.find (fun n ↦ ∀ a ∈ s, a ≤ n) _ h := dif_pos _ theorem _root_.Set.Infinite.Nat.sSup_eq_zero {s : Set ℕ} (h : s.Infinite) : sSup s = 0 := dif_neg fun ⟨n, hn⟩ ↦ let ⟨k, hks, hk⟩ := h.exists_gt n (hn k hks).not_lt hk @[simp] theorem sInf_eq_zero {s : Set ℕ} : sInf s = 0 ↔ 0 ∈ s ∨ s = ∅ := by cases eq_empty_or_nonempty s with | inl h => subst h simp only [or_true, eq_self_iff_true, iInf, InfSet.sInf, mem_empty_iff_false, exists_false, dif_neg, not_false_iff] | inr h => simp only [h.ne_empty, or_false, Nat.sInf_def, h, Nat.find_eq_zero] @[simp] theorem sInf_empty : sInf ∅ = 0 := by rw [sInf_eq_zero] right rfl @[simp] theorem iInf_of_empty {ι : Sort*} [IsEmpty ι] (f : ι → ℕ) : iInf f = 0 := by rw [iInf_of_isEmpty, sInf_empty] /-- This combines `Nat.iInf_of_empty` with `ciInf_const`. -/ @[simp] lemma iInf_const_zero {ι : Sort*} : ⨅ _ : ι, 0 = 0 := (isEmpty_or_nonempty ι).elim (fun h ↦ by simp) fun h ↦ sInf_eq_zero.2 <| by simp theorem sInf_mem {s : Set ℕ} (h : s.Nonempty) : sInf s ∈ s := by classical rw [Nat.sInf_def h] exact Nat.find_spec h theorem not_mem_of_lt_sInf {s : Set ℕ} {m : ℕ} (hm : m < sInf s) : m ∉ s := by classical cases eq_empty_or_nonempty s with | inl h => subst h; apply not_mem_empty | inr h => rw [Nat.sInf_def h] at hm; exact Nat.find_min h hm
protected theorem sInf_le {s : Set ℕ} {m : ℕ} (hm : m ∈ s) : sInf s ≤ m := by classical rw [Nat.sInf_def ⟨m, hm⟩]
Mathlib/Data/Nat/Lattice.lean
80
83
/- Copyright (c) 2023 Andrew Yang, Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import Mathlib.RingTheory.RootsOfUnity.PrimitiveRoots import Mathlib.FieldTheory.Galois.Basic import Mathlib.FieldTheory.KummerPolynomial import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.RingTheory.Norm.Basic /-! # Kummer Extensions ## Main result - `isCyclic_tfae`: Suppose `L/K` is a finite extension of dimension `n`, and `K` contains all `n`-th roots of unity. Then `L/K` is cyclic iff `L` is a splitting field of some irreducible polynomial of the form `Xⁿ - a : K[X]` iff `L = K[α]` for some `αⁿ ∈ K`. - `autEquivRootsOfUnity`: Given an instance `IsSplittingField K L (X ^ n - C a)` (perhaps via `isSplittingField_X_pow_sub_C_of_root_adjoin_eq_top`), then the galois group is isomorphic to `rootsOfUnity n K`, by sending `σ ↦ σ α / α` for `α ^ n = a`, and the inverse is given by `μ ↦ (α ↦ μ • α)`. - `autEquivZmod`: Furthermore, given an explicit choice `ζ` of a primitive `n`-th root of unity, the galois group is then isomorphic to `Multiplicative (ZMod n)` whose inverse is given by `i ↦ (α ↦ ζⁱ • α)`. ## Other results Criteria for `X ^ n - C a` to be irreducible is given: - `X_pow_sub_C_irreducible_iff_of_prime_pow`: For `n = p ^ k` an odd prime power, `X ^ n - C a` is irreducible iff `a` is not a `p`-power. - `X_pow_sub_C_irreducible_iff_forall_prime_of_odd`: For `n` odd, `X ^ n - C a` is irreducible iff `a` is not a `p`-power for all prime `p ∣ n`. - `X_pow_sub_C_irreducible_iff_of_odd`: For `n` odd, `X ^ n - C a` is irreducible iff `a` is not a `d`-power for `d ∣ n` and `d ≠ 1`. TODO: criteria for even `n`. See [serge_lang_algebra] VI,§9. TODO: relate Kummer extensions of degree 2 with the class `Algebra.IsQuadraticExtension`. -/ universe u variable {K : Type u} [Field K] open Polynomial IntermediateField AdjoinRoot section Splits theorem X_pow_sub_C_splits_of_isPrimitiveRoot {n : ℕ} {ζ : K} (hζ : IsPrimitiveRoot ζ n) {α a : K} (e : α ^ n = a) : (X ^ n - C a).Splits (RingHom.id _) := by cases n.eq_zero_or_pos with | inl hn => rw [hn, pow_zero, ← C.map_one, ← map_sub] exact splits_C _ _ | inr hn => rw [splits_iff_card_roots, ← nthRoots, hζ.card_nthRoots, natDegree_X_pow_sub_C, if_pos ⟨α, e⟩] -- make this private, as we only use it to prove a strictly more general version private theorem X_pow_sub_C_eq_prod' {n : ℕ} {ζ : K} (hζ : IsPrimitiveRoot ζ n) {α a : K} (hn : 0 < n) (e : α ^ n = a) : (X ^ n - C a) = ∏ i ∈ Finset.range n, (X - C (ζ ^ i * α)) := by rw [eq_prod_roots_of_monic_of_splits_id (monic_X_pow_sub_C _ (Nat.pos_iff_ne_zero.mp hn)) (X_pow_sub_C_splits_of_isPrimitiveRoot hζ e), ← nthRoots, hζ.nthRoots_eq e, Multiset.map_map] rfl lemma X_pow_sub_C_eq_prod {R : Type*} [CommRing R] [IsDomain R] {n : ℕ} {ζ : R} (hζ : IsPrimitiveRoot ζ n) {α a : R} (hn : 0 < n) (e : α ^ n = a) : (X ^ n - C a) = ∏ i ∈ Finset.range n, (X - C (ζ ^ i * α)) := by let K := FractionRing R let i := algebraMap R K have h := FaithfulSMul.algebraMap_injective R K apply_fun Polynomial.map i using map_injective i h simpa only [Polynomial.map_sub, Polynomial.map_pow, map_X, map_C, map_mul, map_pow, Polynomial.map_prod, Polynomial.map_mul] using X_pow_sub_C_eq_prod' (hζ.map_of_injective h) hn <| map_pow i α n ▸ congrArg i e end Splits section Irreducible theorem X_pow_mul_sub_C_irreducible {n m : ℕ} {a : K} (hm : Irreducible (X ^ m - C a)) (hn : ∀ (E : Type u) [Field E] [Algebra K E] (x : E) (_ : minpoly K x = X ^ m - C a), Irreducible (X ^ n - C (AdjoinSimple.gen K x))) : Irreducible (X ^ (n * m) - C a) := by have hm' : m ≠ 0 := by rintro rfl rw [pow_zero, ← C.map_one, ← map_sub] at hm exact not_irreducible_C _ hm simpa [pow_mul] using irreducible_comp (monic_X_pow_sub_C a hm') (monic_X_pow n) hm (by simpa only [Polynomial.map_pow, map_X] using hn) -- TODO: generalize to even `n` theorem X_pow_sub_C_irreducible_of_odd {n : ℕ} (hn : Odd n) {a : K} (ha : ∀ p : ℕ, p.Prime → p ∣ n → ∀ b : K, b ^ p ≠ a) : Irreducible (X ^ n - C a) := by induction n using induction_on_primes generalizing K a with | h₀ => simp [← Nat.not_even_iff_odd] at hn | h₁ => simpa using irreducible_X_sub_C a | h p n hp IH => rw [mul_comm] apply X_pow_mul_sub_C_irreducible (X_pow_sub_C_irreducible_of_prime hp (ha p hp (dvd_mul_right _ _))) intro E _ _ x hx have : IsIntegral K x := not_not.mp fun h ↦ by simpa only [degree_zero, degree_X_pow_sub_C hp.pos, WithBot.natCast_ne_bot] using congr_arg degree (hx.symm.trans (dif_neg h)) apply IH (Nat.odd_mul.mp hn).2 intros q hq hqn b hb apply ha q hq (dvd_mul_of_dvd_right hqn p) (Algebra.norm _ b) rw [← map_pow, hb, ← adjoin.powerBasis_gen this, Algebra.PowerBasis.norm_gen_eq_coeff_zero_minpoly] simp [minpoly_gen, hx, hp.ne_zero.symm, (Nat.odd_mul.mp hn).1.neg_pow] theorem X_pow_sub_C_irreducible_iff_forall_prime_of_odd {n : ℕ} (hn : Odd n) {a : K} : Irreducible (X ^ n - C a) ↔ (∀ p : ℕ, p.Prime → p ∣ n → ∀ b : K, b ^ p ≠ a) := ⟨fun e _ hp hpn ↦ pow_ne_of_irreducible_X_pow_sub_C e hpn hp.ne_one, X_pow_sub_C_irreducible_of_odd hn⟩ theorem X_pow_sub_C_irreducible_iff_of_odd {n : ℕ} (hn : Odd n) {a : K} : Irreducible (X ^ n - C a) ↔ (∀ d, d ∣ n → d ≠ 1 → ∀ b : K, b ^ d ≠ a) := ⟨fun e _ ↦ pow_ne_of_irreducible_X_pow_sub_C e, fun H ↦ X_pow_sub_C_irreducible_of_odd hn fun p hp hpn ↦ (H p hpn hp.ne_one)⟩ -- TODO: generalize to `p = 2` theorem X_pow_sub_C_irreducible_of_prime_pow {p : ℕ} (hp : p.Prime) (hp' : p ≠ 2) (n : ℕ) {a : K} (ha : ∀ b : K, b ^ p ≠ a) : Irreducible (X ^ (p ^ n) - C a) := by apply X_pow_sub_C_irreducible_of_odd (hp.odd_of_ne_two hp').pow intros q hq hq' simpa [(Nat.prime_dvd_prime_iff_eq hq hp).mp (hq.dvd_of_dvd_pow hq')] using ha theorem X_pow_sub_C_irreducible_iff_of_prime_pow {p : ℕ} (hp : p.Prime) (hp' : p ≠ 2) {n} (hn : n ≠ 0) {a : K} : Irreducible (X ^ p ^ n - C a) ↔ ∀ b, b ^ p ≠ a := ⟨(pow_ne_of_irreducible_X_pow_sub_C · (dvd_pow dvd_rfl hn) hp.ne_one), X_pow_sub_C_irreducible_of_prime_pow hp hp' n⟩ end Irreducible /-! ### Galois Group of `K[n√a]` We first develop the theory for a specific `K[n√a] := AdjoinRoot (X ^ n - C a)`. The main result is the description of the galois group: `autAdjoinRootXPowSubCEquiv`. -/ variable {n : ℕ} (hζ : (primitiveRoots n K).Nonempty) variable (a : K) (H : Irreducible (X ^ n - C a)) set_option quotPrecheck false in scoped[KummerExtension] notation3 "K[" n "√" a "]" => AdjoinRoot (Polynomial.X ^ n - Polynomial.C a) attribute [nolint docBlame] KummerExtension.«termK[_√_]» open scoped KummerExtension section AdjoinRoot include hζ H in /-- Also see `Polynomial.separable_X_pow_sub_C_unit` -/ theorem Polynomial.separable_X_pow_sub_C_of_irreducible : (X ^ n - C a).Separable := by letI := Fact.mk H letI : Algebra K K[n√a] := inferInstance have hn := Nat.pos_iff_ne_zero.mpr (ne_zero_of_irreducible_X_pow_sub_C H) by_cases hn' : n = 1 · rw [hn', pow_one]; exact separable_X_sub_C have ⟨ζ, hζ⟩ := hζ rw [mem_primitiveRoots (Nat.pos_of_ne_zero <| ne_zero_of_irreducible_X_pow_sub_C H)] at hζ rw [← separable_map (algebraMap K K[n√a]), Polynomial.map_sub, Polynomial.map_pow, map_C, map_X, AdjoinRoot.algebraMap_eq, X_pow_sub_C_eq_prod (hζ.map_of_injective (algebraMap K _).injective) hn (root_X_pow_sub_C_pow n a), separable_prod_X_sub_C_iff'] #adaptation_note /-- https://github.com/leanprover/lean4/pull/5376 we need to provide this helper instance. -/ have : MonoidHomClass (K →+* K[n√a]) K K[n√a] := inferInstance exact (hζ.map_of_injective (algebraMap K K[n√a]).injective).injOn_pow_mul (root_X_pow_sub_C_ne_zero (lt_of_le_of_ne (show 1 ≤ n from hn) (Ne.symm hn')) _) variable (n) /-- The natural embedding of the roots of unity of `K` into `Gal(K[ⁿ√a]/K)`, by sending `η ↦ (ⁿ√a ↦ η • ⁿ√a)`. Also see `autAdjoinRootXPowSubC` for the `AlgEquiv` version. -/ noncomputable def autAdjoinRootXPowSubCHom : rootsOfUnity n K →* (K[n√a] →ₐ[K] K[n√a]) where toFun := fun η ↦ liftHom (X ^ n - C a) (((η : Kˣ) : K) • (root _) : K[n√a]) <| by have := (mem_rootsOfUnity' _ _).mp η.prop rw [map_sub, map_pow, aeval_C, aeval_X, Algebra.smul_def, mul_pow, root_X_pow_sub_C_pow, AdjoinRoot.algebraMap_eq, ← map_pow, this, map_one, one_mul, sub_self] map_one' := algHom_ext <| by simp map_mul' := fun ε η ↦ algHom_ext <| by simp [mul_smul, smul_comm ((ε : Kˣ) : K)] /-- The natural embedding of the roots of unity of `K` into `Gal(K[ⁿ√a]/K)`, by sending `η ↦ (ⁿ√a ↦ η • ⁿ√a)`. This is an isomorphism when `K` contains a primitive root of unity. See `autAdjoinRootXPowSubCEquiv`. -/ noncomputable def autAdjoinRootXPowSubC : rootsOfUnity n K →* (K[n√a] ≃ₐ[K] K[n√a]) := (AlgEquiv.algHomUnitsEquiv _ _).toMonoidHom.comp (autAdjoinRootXPowSubCHom n a).toHomUnits variable {n} lemma autAdjoinRootXPowSubC_root (η) : autAdjoinRootXPowSubC n a η (root _) = ((η : Kˣ) : K) • root _ := by dsimp [autAdjoinRootXPowSubC, autAdjoinRootXPowSubCHom, AlgEquiv.algHomUnitsEquiv] apply liftHom_root variable {a} /-- The inverse function of `autAdjoinRootXPowSubC` if `K` has all roots of unity. See `autAdjoinRootXPowSubCEquiv`. -/ noncomputable def AdjoinRootXPowSubCEquivToRootsOfUnity [NeZero n] (σ : K[n√a] ≃ₐ[K] K[n√a]) : rootsOfUnity n K := letI := Fact.mk H letI : IsDomain K[n√a] := inferInstance letI := Classical.decEq K (rootsOfUnityEquivOfPrimitiveRoots (n := n) (algebraMap K K[n√a]).injective hζ).symm (rootsOfUnity.mkOfPowEq (if a = 0 then 1 else σ (root _) / root _) (by -- The if is needed in case `n = 1` and `a = 0` and `K[n√a] = K`. split · exact one_pow _ rw [div_pow, ← map_pow] simp only [root_X_pow_sub_C_pow, ← AdjoinRoot.algebraMap_eq, AlgEquiv.commutes] rw [div_self] rwa [Ne, map_eq_zero_iff _ (algebraMap K _).injective])) /-- The equivalence between the roots of unity of `K` and `Gal(K[ⁿ√a]/K)`. -/ noncomputable def autAdjoinRootXPowSubCEquiv [NeZero n] : rootsOfUnity n K ≃* (K[n√a] ≃ₐ[K] K[n√a]) where __ := autAdjoinRootXPowSubC n a invFun := AdjoinRootXPowSubCEquivToRootsOfUnity hζ H left_inv := by intro η have := Fact.mk H have : IsDomain K[n√a] := inferInstance letI : Algebra K K[n√a] := inferInstance apply (rootsOfUnityEquivOfPrimitiveRoots (algebraMap K K[n√a]).injective hζ).injective ext simp only [AdjoinRoot.algebraMap_eq, OneHom.toFun_eq_coe, MonoidHom.toOneHom_coe, autAdjoinRootXPowSubC_root, Algebra.smul_def, ne_eq, MulEquiv.apply_symm_apply, rootsOfUnity.val_mkOfPowEq_coe, val_rootsOfUnityEquivOfPrimitiveRoots_apply_coe, AdjoinRootXPowSubCEquivToRootsOfUnity] split_ifs with h · obtain rfl := not_imp_not.mp (fun hn ↦ ne_zero_of_irreducible_X_pow_sub_C' hn H) h have : (η : Kˣ) = 1 := (pow_one _).symm.trans η.prop simp only [this, Units.val_one, map_one] · exact mul_div_cancel_right₀ _ (root_X_pow_sub_C_ne_zero' (NeZero.pos n) h) right_inv := by intro e have := Fact.mk H letI : Algebra K K[n√a] := inferInstance apply AlgEquiv.coe_algHom_injective apply AdjoinRoot.algHom_ext simp only [AdjoinRootXPowSubCEquivToRootsOfUnity, AdjoinRoot.algebraMap_eq, OneHom.toFun_eq_coe, MonoidHom.toOneHom_coe, AlgHom.coe_coe, autAdjoinRootXPowSubC_root, Algebra.smul_def] rw [rootsOfUnityEquivOfPrimitiveRoots_symm_apply, rootsOfUnity.val_mkOfPowEq_coe] split_ifs with h · obtain rfl := not_imp_not.mp (fun hn ↦ ne_zero_of_irreducible_X_pow_sub_C' hn H) h rw [(pow_one _).symm.trans (root_X_pow_sub_C_pow 1 a), one_mul, ← AdjoinRoot.algebraMap_eq, AlgEquiv.commutes] · refine div_mul_cancel₀ _ (root_X_pow_sub_C_ne_zero' (NeZero.pos n) h) lemma autAdjoinRootXPowSubCEquiv_root [NeZero n] (η) : autAdjoinRootXPowSubCEquiv hζ H η (root _) = ((η : Kˣ) : K) • root _ := autAdjoinRootXPowSubC_root a η lemma autAdjoinRootXPowSubCEquiv_symm_smul [NeZero n] (σ) : ((autAdjoinRootXPowSubCEquiv hζ H).symm σ : Kˣ) • (root _ : K[n√a]) = σ (root _) := by have := Fact.mk H simp only [autAdjoinRootXPowSubCEquiv, OneHom.toFun_eq_coe, MonoidHom.toOneHom_coe, MulEquiv.symm_mk, MulEquiv.coe_mk, Equiv.coe_fn_symm_mk, AdjoinRootXPowSubCEquivToRootsOfUnity, AdjoinRoot.algebraMap_eq, rootsOfUnity.mkOfPowEq, Units.smul_def, Algebra.smul_def, rootsOfUnityEquivOfPrimitiveRoots_symm_apply, Units.val_ofPowEqOne, ite_mul, one_mul] simp_rw [← root_X_pow_sub_C_eq_zero_iff H] split_ifs with h · rw [h, map_zero] · rw [div_mul_cancel₀ _ h] end AdjoinRoot /-! ### Galois Group of `IsSplittingField K L (X ^ n - C a)` -/ section IsSplittingField variable {a} variable {L : Type*} [Field L] [Algebra K L] [IsSplittingField K L (X ^ n - C a)] include hζ in lemma isSplittingField_AdjoinRoot_X_pow_sub_C : haveI := Fact.mk H letI : Algebra K K[n√a] := inferInstance
IsSplittingField K K[n√a] (X ^ n - C a) := by have := Fact.mk H letI : Algebra K K[n√a] := inferInstance constructor
Mathlib/FieldTheory/KummerExtension.lean
300
303
/- Copyright (c) 2022 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import Mathlib.Order.Interval.Set.Monotone import Mathlib.Probability.Process.HittingTime import Mathlib.Probability.Martingale.Basic import Mathlib.Tactic.AdaptationNote /-! # Doob's upcrossing estimate Given a discrete real-valued submartingale $(f_n)_{n \in \mathbb{N}}$, denoting by $U_N(a, b)$ the number of times $f_n$ crossed from below $a$ to above $b$ before time $N$, Doob's upcrossing estimate (also known as Doob's inequality) states that $$(b - a) \mathbb{E}[U_N(a, b)] \le \mathbb{E}[(f_N - a)^+].$$ Doob's upcrossing estimate is an important inequality and is central in proving the martingale convergence theorems. ## Main definitions * `MeasureTheory.upperCrossingTime a b f N n`: is the stopping time corresponding to `f` crossing above `b` the `n`-th time before time `N` (if this does not occur then the value is taken to be `N`). * `MeasureTheory.lowerCrossingTime a b f N n`: is the stopping time corresponding to `f` crossing below `a` the `n`-th time before time `N` (if this does not occur then the value is taken to be `N`). * `MeasureTheory.upcrossingStrat a b f N`: is the predictable process which is 1 if `n` is between a consecutive pair of lower and upper crossings and is 0 otherwise. Intuitively one might think of the `upcrossingStrat` as the strategy of buying 1 share whenever the process crosses below `a` for the first time after selling and selling 1 share whenever the process crosses above `b` for the first time after buying. * `MeasureTheory.upcrossingsBefore a b f N`: is the number of times `f` crosses from below `a` to above `b` before time `N`. * `MeasureTheory.upcrossings a b f`: is the number of times `f` crosses from below `a` to above `b`. This takes value in `ℝ≥0∞` and so is allowed to be `∞`. ## Main results * `MeasureTheory.Adapted.isStoppingTime_upperCrossingTime`: `upperCrossingTime` is a stopping time whenever the process it is associated to is adapted. * `MeasureTheory.Adapted.isStoppingTime_lowerCrossingTime`: `lowerCrossingTime` is a stopping time whenever the process it is associated to is adapted. * `MeasureTheory.Submartingale.mul_integral_upcrossingsBefore_le_integral_pos_part`: Doob's upcrossing estimate. * `MeasureTheory.Submartingale.mul_lintegral_upcrossings_le_lintegral_pos_part`: the inequality obtained by taking the supremum on both sides of Doob's upcrossing estimate. ### References We mostly follow the proof from [Kallenberg, *Foundations of modern probability*][kallenberg2021] -/ open TopologicalSpace Filter open scoped NNReal ENNReal MeasureTheory ProbabilityTheory Topology namespace MeasureTheory variable {Ω ι : Type*} {m0 : MeasurableSpace Ω} {μ : Measure Ω} /-! ## Proof outline In this section, we will denote by $U_N(a, b)$ the number of upcrossings of $(f_n)$ from below $a$ to above $b$ before time $N$. To define $U_N(a, b)$, we will construct two stopping times corresponding to when $(f_n)$ crosses below $a$ and above $b$. Namely, we define $$ \sigma_n := \inf \{n \ge \tau_n \mid f_n \le a\} \wedge N; $$ $$ \tau_{n + 1} := \inf \{n \ge \sigma_n \mid f_n \ge b\} \wedge N. $$ These are `lowerCrossingTime` and `upperCrossingTime` in our formalization which are defined using `MeasureTheory.hitting` allowing us to specify a starting and ending time. Then, we may simply define $U_N(a, b) := \sup \{n \mid \tau_n < N\}$. Fixing $a < b \in \mathbb{R}$, we will first prove the theorem in the special case that $0 \le f_0$ and $a \le f_N$. In particular, we will show $$ (b - a) \mathbb{E}[U_N(a, b)] \le \mathbb{E}[f_N]. $$ This is `MeasureTheory.integral_mul_upcrossingsBefore_le_integral` in our formalization. To prove this, we use the fact that given a non-negative, bounded, predictable process $(C_n)$ (i.e. $(C_{n + 1})$ is adapted), $(C \bullet f)_n := \sum_{k \le n} C_{k + 1}(f_{k + 1} - f_k)$ is a submartingale if $(f_n)$ is. Define $C_n := \sum_{k \le n} \mathbf{1}_{[\sigma_k, \tau_{k + 1})}(n)$. It is easy to see that $(1 - C_n)$ is non-negative, bounded and predictable, and hence, given a submartingale $(f_n)$, $(1 - C) \bullet f$ is also a submartingale. Thus, by the submartingale property, $0 \le \mathbb{E}[((1 - C) \bullet f)_0] \le \mathbb{E}[((1 - C) \bullet f)_N]$ implying $$ \mathbb{E}[(C \bullet f)_N] \le \mathbb{E}[(1 \bullet f)_N] = \mathbb{E}[f_N] - \mathbb{E}[f_0]. $$ Furthermore, \begin{align} (C \bullet f)_N & = \sum_{n \le N} \sum_{k \le N} \mathbf{1}_{[\sigma_k, \tau_{k + 1})}(n)(f_{n + 1} - f_n)\\ & = \sum_{k \le N} \sum_{n \le N} \mathbf{1}_{[\sigma_k, \tau_{k + 1})}(n)(f_{n + 1} - f_n)\\ & = \sum_{k \le N} (f_{\sigma_k + 1} - f_{\sigma_k} + f_{\sigma_k + 2} - f_{\sigma_k + 1} + \cdots + f_{\tau_{k + 1}} - f_{\tau_{k + 1} - 1})\\ & = \sum_{k \le N} (f_{\tau_{k + 1}} - f_{\sigma_k}) \ge \sum_{k < U_N(a, b)} (b - a) = (b - a) U_N(a, b) \end{align} where the inequality follows since for all $k < U_N(a, b)$, $f_{\tau_{k + 1}} - f_{\sigma_k} \ge b - a$ while for all $k > U_N(a, b)$, $f_{\tau_{k + 1}} = f_{\sigma_k} = f_N$ and $f_{\tau_{U_N(a, b) + 1}} - f_{\sigma_{U_N(a, b)}} = f_N - a \ge 0$. Hence, we have $$ (b - a) \mathbb{E}[U_N(a, b)] \le \mathbb{E}[(C \bullet f)_N] \le \mathbb{E}[f_N] - \mathbb{E}[f_0] \le \mathbb{E}[f_N], $$ as required. To obtain the general case, we simply apply the above to $((f_n - a)^+)_n$. -/ /-- `lowerCrossingTimeAux a f c N` is the first time `f` reached below `a` after time `c` before time `N`. -/ noncomputable def lowerCrossingTimeAux [Preorder ι] [InfSet ι] (a : ℝ) (f : ι → Ω → ℝ) (c N : ι) : Ω → ι := hitting f (Set.Iic a) c N /-- `upperCrossingTime a b f N n` is the first time before time `N`, `f` reaches above `b` after `f` reached below `a` for the `n - 1`-th time. -/ noncomputable def upperCrossingTime [Preorder ι] [OrderBot ι] [InfSet ι] (a b : ℝ) (f : ι → Ω → ℝ) (N : ι) : ℕ → Ω → ι | 0 => ⊥ | n + 1 => fun ω => hitting f (Set.Ici b) (lowerCrossingTimeAux a f (upperCrossingTime a b f N n ω) N ω) N ω /-- `lowerCrossingTime a b f N n` is the first time before time `N`, `f` reaches below `a` after `f` reached above `b` for the `n`-th time. -/ noncomputable def lowerCrossingTime [Preorder ι] [OrderBot ι] [InfSet ι] (a b : ℝ) (f : ι → Ω → ℝ) (N : ι) (n : ℕ) : Ω → ι := fun ω => hitting f (Set.Iic a) (upperCrossingTime a b f N n ω) N ω section variable [Preorder ι] [OrderBot ι] [InfSet ι] variable {a b : ℝ} {f : ι → Ω → ℝ} {N : ι} {n : ℕ} {ω : Ω} @[simp] theorem upperCrossingTime_zero : upperCrossingTime a b f N 0 = ⊥ := rfl @[simp] theorem lowerCrossingTime_zero : lowerCrossingTime a b f N 0 = hitting f (Set.Iic a) ⊥ N := rfl theorem upperCrossingTime_succ : upperCrossingTime a b f N (n + 1) ω = hitting f (Set.Ici b) (lowerCrossingTimeAux a f (upperCrossingTime a b f N n ω) N ω) N ω := by rw [upperCrossingTime] theorem upperCrossingTime_succ_eq (ω : Ω) : upperCrossingTime a b f N (n + 1) ω = hitting f (Set.Ici b) (lowerCrossingTime a b f N n ω) N ω := by simp only [upperCrossingTime_succ] rfl end section ConditionallyCompleteLinearOrderBot variable [ConditionallyCompleteLinearOrderBot ι] variable {a b : ℝ} {f : ι → Ω → ℝ} {N : ι} {n m : ℕ} {ω : Ω} theorem upperCrossingTime_le : upperCrossingTime a b f N n ω ≤ N := by cases n · simp only [upperCrossingTime_zero, Pi.bot_apply, bot_le] · simp only [upperCrossingTime_succ, hitting_le] @[simp] theorem upperCrossingTime_zero' : upperCrossingTime a b f ⊥ n ω = ⊥ := eq_bot_iff.2 upperCrossingTime_le theorem lowerCrossingTime_le : lowerCrossingTime a b f N n ω ≤ N := by simp only [lowerCrossingTime, hitting_le ω] theorem upperCrossingTime_le_lowerCrossingTime : upperCrossingTime a b f N n ω ≤ lowerCrossingTime a b f N n ω := by simp only [lowerCrossingTime, le_hitting upperCrossingTime_le ω] theorem lowerCrossingTime_le_upperCrossingTime_succ : lowerCrossingTime a b f N n ω ≤ upperCrossingTime a b f N (n + 1) ω := by rw [upperCrossingTime_succ] exact le_hitting lowerCrossingTime_le ω theorem lowerCrossingTime_mono (hnm : n ≤ m) : lowerCrossingTime a b f N n ω ≤ lowerCrossingTime a b f N m ω := by suffices Monotone fun n => lowerCrossingTime a b f N n ω by exact this hnm exact monotone_nat_of_le_succ fun n => le_trans lowerCrossingTime_le_upperCrossingTime_succ upperCrossingTime_le_lowerCrossingTime theorem upperCrossingTime_mono (hnm : n ≤ m) : upperCrossingTime a b f N n ω ≤ upperCrossingTime a b f N m ω := by suffices Monotone fun n => upperCrossingTime a b f N n ω by exact this hnm exact monotone_nat_of_le_succ fun n => le_trans upperCrossingTime_le_lowerCrossingTime lowerCrossingTime_le_upperCrossingTime_succ end ConditionallyCompleteLinearOrderBot variable {a b : ℝ} {f : ℕ → Ω → ℝ} {N : ℕ} {n m : ℕ} {ω : Ω} theorem stoppedValue_lowerCrossingTime (h : lowerCrossingTime a b f N n ω ≠ N) : stoppedValue f (lowerCrossingTime a b f N n) ω ≤ a := by obtain ⟨j, hj₁, hj₂⟩ := (hitting_le_iff_of_lt _ (lt_of_le_of_ne lowerCrossingTime_le h)).1 le_rfl exact stoppedValue_hitting_mem ⟨j, ⟨hj₁.1, le_trans hj₁.2 lowerCrossingTime_le⟩, hj₂⟩ theorem stoppedValue_upperCrossingTime (h : upperCrossingTime a b f N (n + 1) ω ≠ N) : b ≤ stoppedValue f (upperCrossingTime a b f N (n + 1)) ω := by obtain ⟨j, hj₁, hj₂⟩ := (hitting_le_iff_of_lt _ (lt_of_le_of_ne upperCrossingTime_le h)).1 le_rfl exact stoppedValue_hitting_mem ⟨j, ⟨hj₁.1, le_trans hj₁.2 (hitting_le _)⟩, hj₂⟩ theorem upperCrossingTime_lt_lowerCrossingTime (hab : a < b) (hn : lowerCrossingTime a b f N (n + 1) ω ≠ N) : upperCrossingTime a b f N (n + 1) ω < lowerCrossingTime a b f N (n + 1) ω := by refine lt_of_le_of_ne upperCrossingTime_le_lowerCrossingTime fun h => not_le.2 hab <| le_trans ?_ (stoppedValue_lowerCrossingTime hn) simp only [stoppedValue] rw [← h] exact stoppedValue_upperCrossingTime (h.symm ▸ hn) theorem lowerCrossingTime_lt_upperCrossingTime (hab : a < b) (hn : upperCrossingTime a b f N (n + 1) ω ≠ N) : lowerCrossingTime a b f N n ω < upperCrossingTime a b f N (n + 1) ω := by refine lt_of_le_of_ne lowerCrossingTime_le_upperCrossingTime_succ fun h => not_le.2 hab <| le_trans (stoppedValue_upperCrossingTime hn) ?_ simp only [stoppedValue] rw [← h] exact stoppedValue_lowerCrossingTime (h.symm ▸ hn) theorem upperCrossingTime_lt_succ (hab : a < b) (hn : upperCrossingTime a b f N (n + 1) ω ≠ N) : upperCrossingTime a b f N n ω < upperCrossingTime a b f N (n + 1) ω := lt_of_le_of_lt upperCrossingTime_le_lowerCrossingTime (lowerCrossingTime_lt_upperCrossingTime hab hn) theorem lowerCrossingTime_stabilize (hnm : n ≤ m) (hn : lowerCrossingTime a b f N n ω = N) : lowerCrossingTime a b f N m ω = N := le_antisymm lowerCrossingTime_le (le_trans (le_of_eq hn.symm) (lowerCrossingTime_mono hnm)) theorem upperCrossingTime_stabilize (hnm : n ≤ m) (hn : upperCrossingTime a b f N n ω = N) : upperCrossingTime a b f N m ω = N := le_antisymm upperCrossingTime_le (le_trans (le_of_eq hn.symm) (upperCrossingTime_mono hnm)) theorem lowerCrossingTime_stabilize' (hnm : n ≤ m) (hn : N ≤ lowerCrossingTime a b f N n ω) : lowerCrossingTime a b f N m ω = N := lowerCrossingTime_stabilize hnm (le_antisymm lowerCrossingTime_le hn) theorem upperCrossingTime_stabilize' (hnm : n ≤ m) (hn : N ≤ upperCrossingTime a b f N n ω) : upperCrossingTime a b f N m ω = N := upperCrossingTime_stabilize hnm (le_antisymm upperCrossingTime_le hn) -- `upperCrossingTime_bound_eq` provides an explicit bound theorem exists_upperCrossingTime_eq (f : ℕ → Ω → ℝ) (N : ℕ) (ω : Ω) (hab : a < b) : ∃ n, upperCrossingTime a b f N n ω = N := by by_contra h; push_neg at h have : StrictMono fun n => upperCrossingTime a b f N n ω := strictMono_nat_of_lt_succ fun n => upperCrossingTime_lt_succ hab (h _) obtain ⟨_, ⟨k, rfl⟩, hk⟩ : ∃ (m : _) (_ : m ∈ Set.range fun n => upperCrossingTime a b f N n ω), N < m := ⟨upperCrossingTime a b f N (N + 1) ω, ⟨N + 1, rfl⟩, lt_of_lt_of_le N.lt_succ_self (StrictMono.id_le this (N + 1))⟩ exact not_le.2 hk upperCrossingTime_le theorem upperCrossingTime_lt_bddAbove (hab : a < b) : BddAbove {n | upperCrossingTime a b f N n ω < N} := by obtain ⟨k, hk⟩ := exists_upperCrossingTime_eq f N ω hab refine ⟨k, fun n (hn : upperCrossingTime a b f N n ω < N) => ?_⟩ by_contra hn' exact hn.ne (upperCrossingTime_stabilize (not_le.1 hn').le hk) theorem upperCrossingTime_lt_nonempty (hN : 0 < N) : {n | upperCrossingTime a b f N n ω < N}.Nonempty := ⟨0, hN⟩ theorem upperCrossingTime_bound_eq (f : ℕ → Ω → ℝ) (N : ℕ) (ω : Ω) (hab : a < b) : upperCrossingTime a b f N N ω = N := by by_cases hN' : N < Nat.find (exists_upperCrossingTime_eq f N ω hab) · refine le_antisymm upperCrossingTime_le ?_ have hmono : StrictMonoOn (fun n => upperCrossingTime a b f N n ω) (Set.Iic (Nat.find (exists_upperCrossingTime_eq f N ω hab)).pred) := by refine strictMonoOn_Iic_of_lt_succ fun m hm => upperCrossingTime_lt_succ hab ?_ rw [Nat.lt_pred_iff] at hm convert Nat.find_min _ hm convert StrictMonoOn.Iic_id_le hmono N (Nat.le_sub_one_of_lt hN') · rw [not_lt] at hN' exact upperCrossingTime_stabilize hN' (Nat.find_spec (exists_upperCrossingTime_eq f N ω hab)) theorem upperCrossingTime_eq_of_bound_le (hab : a < b) (hn : N ≤ n) : upperCrossingTime a b f N n ω = N := le_antisymm upperCrossingTime_le (le_trans (upperCrossingTime_bound_eq f N ω hab).symm.le (upperCrossingTime_mono hn)) variable {ℱ : Filtration ℕ m0} theorem Adapted.isStoppingTime_crossing (hf : Adapted ℱ f) : IsStoppingTime ℱ (upperCrossingTime a b f N n) ∧ IsStoppingTime ℱ (lowerCrossingTime a b f N n) := by induction' n with k ih · refine ⟨isStoppingTime_const _ 0, ?_⟩ simp [hitting_isStoppingTime hf measurableSet_Iic] · obtain ⟨_, ih₂⟩ := ih have : IsStoppingTime ℱ (upperCrossingTime a b f N (k + 1)) := by intro n simp_rw [upperCrossingTime_succ_eq] exact isStoppingTime_hitting_isStoppingTime ih₂ (fun _ => lowerCrossingTime_le) measurableSet_Ici hf _ refine ⟨this, ?_⟩ intro n exact isStoppingTime_hitting_isStoppingTime this (fun _ => upperCrossingTime_le) measurableSet_Iic hf _ theorem Adapted.isStoppingTime_upperCrossingTime (hf : Adapted ℱ f) : IsStoppingTime ℱ (upperCrossingTime a b f N n) := hf.isStoppingTime_crossing.1 theorem Adapted.isStoppingTime_lowerCrossingTime (hf : Adapted ℱ f) : IsStoppingTime ℱ (lowerCrossingTime a b f N n) := hf.isStoppingTime_crossing.2 /-- `upcrossingStrat a b f N n` is 1 if `n` is between a consecutive pair of lower and upper crossings and is 0 otherwise. `upcrossingStrat` is shifted by one index so that it is adapted rather than predictable. -/ noncomputable def upcrossingStrat (a b : ℝ) (f : ℕ → Ω → ℝ) (N n : ℕ) (ω : Ω) : ℝ := ∑ k ∈ Finset.range N, (Set.Ico (lowerCrossingTime a b f N k ω) (upperCrossingTime a b f N (k + 1) ω)).indicator 1 n theorem upcrossingStrat_nonneg : 0 ≤ upcrossingStrat a b f N n ω := Finset.sum_nonneg fun _ _ => Set.indicator_nonneg (fun _ _ => zero_le_one) _ theorem upcrossingStrat_le_one : upcrossingStrat a b f N n ω ≤ 1 := by rw [upcrossingStrat, ← Finset.indicator_biUnion_apply] · exact Set.indicator_le_self' (fun _ _ => zero_le_one) _ intro i _ j _ hij simp only [Set.Ico_disjoint_Ico] obtain hij' | hij' := lt_or_gt_of_ne hij · rw [min_eq_left (upperCrossingTime_mono (Nat.succ_le_succ hij'.le) : upperCrossingTime a b f N _ ω ≤ upperCrossingTime a b f N _ ω), max_eq_right (lowerCrossingTime_mono hij'.le : lowerCrossingTime a b f N _ _ ≤ lowerCrossingTime _ _ _ _ _ _)] refine le_trans upperCrossingTime_le_lowerCrossingTime (lowerCrossingTime_mono (Nat.succ_le_of_lt hij')) · rw [gt_iff_lt] at hij' rw [min_eq_right (upperCrossingTime_mono (Nat.succ_le_succ hij'.le) : upperCrossingTime a b f N _ ω ≤ upperCrossingTime a b f N _ ω), max_eq_left (lowerCrossingTime_mono hij'.le : lowerCrossingTime a b f N _ _ ≤ lowerCrossingTime _ _ _ _ _ _)] refine le_trans upperCrossingTime_le_lowerCrossingTime (lowerCrossingTime_mono (Nat.succ_le_of_lt hij')) theorem Adapted.upcrossingStrat_adapted (hf : Adapted ℱ f) : Adapted ℱ (upcrossingStrat a b f N) := by intro n change StronglyMeasurable[ℱ n] fun ω => ∑ k ∈ Finset.range N, ({n | lowerCrossingTime a b f N k ω ≤ n} ∩ {n | n < upperCrossingTime a b f N (k + 1) ω}).indicator 1 n refine Finset.stronglyMeasurable_sum _ fun i _ => stronglyMeasurable_const.indicator ((hf.isStoppingTime_lowerCrossingTime n).inter ?_) simp_rw [← not_le] exact (hf.isStoppingTime_upperCrossingTime n).compl theorem Submartingale.sum_upcrossingStrat_mul [IsFiniteMeasure μ] (hf : Submartingale f ℱ μ) (a b : ℝ) (N : ℕ) : Submartingale (fun n : ℕ => ∑ k ∈ Finset.range n, upcrossingStrat a b f N k * (f (k + 1) - f k)) ℱ μ := hf.sum_mul_sub hf.adapted.upcrossingStrat_adapted (fun _ _ => upcrossingStrat_le_one) fun _ _ => upcrossingStrat_nonneg theorem Submartingale.sum_sub_upcrossingStrat_mul [IsFiniteMeasure μ] (hf : Submartingale f ℱ μ) (a b : ℝ) (N : ℕ) : Submartingale (fun n : ℕ => ∑ k ∈ Finset.range n, (1 - upcrossingStrat a b f N k) * (f (k + 1) - f k)) ℱ μ := by refine hf.sum_mul_sub (fun n => (adapted_const ℱ 1 n).sub (hf.adapted.upcrossingStrat_adapted n)) (?_ : ∀ n ω, (1 - upcrossingStrat a b f N n) ω ≤ 1) ?_ · exact fun n ω => sub_le_self _ upcrossingStrat_nonneg · intro n ω simp [upcrossingStrat_le_one] theorem Submartingale.sum_mul_upcrossingStrat_le [IsFiniteMeasure μ] (hf : Submartingale f ℱ μ) : μ[∑ k ∈ Finset.range n, upcrossingStrat a b f N k * (f (k + 1) - f k)] ≤ μ[f n] - μ[f 0] := by have h₁ : (0 : ℝ) ≤ μ[∑ k ∈ Finset.range n, (1 - upcrossingStrat a b f N k) * (f (k + 1) - f k)] := by have := (hf.sum_sub_upcrossingStrat_mul a b N).setIntegral_le (zero_le n) MeasurableSet.univ rw [setIntegral_univ, setIntegral_univ] at this refine le_trans ?_ this simp only [Finset.range_zero, Finset.sum_empty, integral_zero', le_refl] have h₂ : μ[∑ k ∈ Finset.range n, (1 - upcrossingStrat a b f N k) * (f (k + 1) - f k)] = μ[∑ k ∈ Finset.range n, (f (k + 1) - f k)] - μ[∑ k ∈ Finset.range n, upcrossingStrat a b f N k * (f (k + 1) - f k)] := by simp only [sub_mul, one_mul, Finset.sum_sub_distrib, Pi.sub_apply, Finset.sum_apply, Pi.mul_apply] refine integral_sub (Integrable.sub (integrable_finset_sum _ fun i _ => hf.integrable _) (integrable_finset_sum _ fun i _ => hf.integrable _)) ?_ convert (hf.sum_upcrossingStrat_mul a b N).integrable n using 1 ext; simp rw [h₂, sub_nonneg] at h₁ refine le_trans h₁ ?_ simp_rw [Finset.sum_range_sub, integral_sub' (hf.integrable _) (hf.integrable _), le_refl] /-- The number of upcrossings (strictly) before time `N`. -/ noncomputable def upcrossingsBefore [Preorder ι] [OrderBot ι] [InfSet ι] (a b : ℝ) (f : ι → Ω → ℝ) (N : ι) (ω : Ω) : ℕ := sSup {n | upperCrossingTime a b f N n ω < N} @[simp] theorem upcrossingsBefore_bot [Preorder ι] [OrderBot ι] [InfSet ι] {a b : ℝ} {f : ι → Ω → ℝ} {ω : Ω} : upcrossingsBefore a b f ⊥ ω = ⊥ := by simp [upcrossingsBefore] theorem upcrossingsBefore_zero : upcrossingsBefore a b f 0 ω = 0 := by simp [upcrossingsBefore] @[simp] theorem upcrossingsBefore_zero' : upcrossingsBefore a b f 0 = 0 := by ext ω; exact upcrossingsBefore_zero theorem upperCrossingTime_lt_of_le_upcrossingsBefore (hN : 0 < N) (hab : a < b) (hn : n ≤ upcrossingsBefore a b f N ω) : upperCrossingTime a b f N n ω < N := haveI : upperCrossingTime a b f N (upcrossingsBefore a b f N ω) ω < N := (upperCrossingTime_lt_nonempty hN).csSup_mem ((OrderBot.bddBelow _).finite_of_bddAbove (upperCrossingTime_lt_bddAbove hab)) lt_of_le_of_lt (upperCrossingTime_mono hn) this theorem upperCrossingTime_eq_of_upcrossingsBefore_lt (hab : a < b) (hn : upcrossingsBefore a b f N ω < n) : upperCrossingTime a b f N n ω = N := by refine le_antisymm upperCrossingTime_le (not_lt.1 ?_) convert not_mem_of_csSup_lt hn (upperCrossingTime_lt_bddAbove hab) using 1 theorem upcrossingsBefore_le (f : ℕ → Ω → ℝ) (ω : Ω) (hab : a < b) : upcrossingsBefore a b f N ω ≤ N := by by_cases hN : N = 0 · subst hN rw [upcrossingsBefore_zero] · refine csSup_le ⟨0, zero_lt_iff.2 hN⟩ fun n (hn : _ < N) => ?_ by_contra hnN exact hn.ne (upperCrossingTime_eq_of_bound_le hab (not_le.1 hnN).le) theorem crossing_eq_crossing_of_lowerCrossingTime_lt {M : ℕ} (hNM : N ≤ M) (h : lowerCrossingTime a b f N n ω < N) : upperCrossingTime a b f M n ω = upperCrossingTime a b f N n ω ∧ lowerCrossingTime a b f M n ω = lowerCrossingTime a b f N n ω := by have h' : upperCrossingTime a b f N n ω < N := lt_of_le_of_lt upperCrossingTime_le_lowerCrossingTime h induction' n with k ih · simp only [upperCrossingTime_zero, bot_eq_zero', eq_self_iff_true, lowerCrossingTime_zero, true_and, eq_comm] refine hitting_eq_hitting_of_exists hNM ?_ rw [lowerCrossingTime, hitting_lt_iff] at h · obtain ⟨j, hj₁, hj₂⟩ := h exact ⟨j, ⟨hj₁.1, hj₁.2.le⟩, hj₂⟩ · exact le_rfl · specialize ih (lt_of_le_of_lt (lowerCrossingTime_mono (Nat.le_succ _)) h) (lt_of_le_of_lt (upperCrossingTime_mono (Nat.le_succ _)) h') have : upperCrossingTime a b f M k.succ ω = upperCrossingTime a b f N k.succ ω := by rw [upperCrossingTime_succ_eq, hitting_lt_iff] at h' · simp only [upperCrossingTime_succ_eq] obtain ⟨j, hj₁, hj₂⟩ := h' rw [eq_comm, ih.2] exact hitting_eq_hitting_of_exists hNM ⟨j, ⟨hj₁.1, hj₁.2.le⟩, hj₂⟩ · exact le_rfl refine ⟨this, ?_⟩ simp only [lowerCrossingTime, eq_comm, this, Nat.succ_eq_add_one] refine hitting_eq_hitting_of_exists hNM ?_ rw [lowerCrossingTime, hitting_lt_iff _ le_rfl] at h obtain ⟨j, hj₁, hj₂⟩ := h exact ⟨j, ⟨hj₁.1, hj₁.2.le⟩, hj₂⟩ theorem crossing_eq_crossing_of_upperCrossingTime_lt {M : ℕ} (hNM : N ≤ M) (h : upperCrossingTime a b f N (n + 1) ω < N) : upperCrossingTime a b f M (n + 1) ω = upperCrossingTime a b f N (n + 1) ω ∧ lowerCrossingTime a b f M n ω = lowerCrossingTime a b f N n ω := by have := (crossing_eq_crossing_of_lowerCrossingTime_lt hNM (lt_of_le_of_lt lowerCrossingTime_le_upperCrossingTime_succ h)).2 refine ⟨?_, this⟩ rw [upperCrossingTime_succ_eq, upperCrossingTime_succ_eq, eq_comm, this] refine hitting_eq_hitting_of_exists hNM ?_ rw [upperCrossingTime_succ_eq, hitting_lt_iff] at h · obtain ⟨j, hj₁, hj₂⟩ := h exact ⟨j, ⟨hj₁.1, hj₁.2.le⟩, hj₂⟩ · exact le_rfl theorem upperCrossingTime_eq_upperCrossingTime_of_lt {M : ℕ} (hNM : N ≤ M) (h : upperCrossingTime a b f N n ω < N) : upperCrossingTime a b f M n ω = upperCrossingTime a b f N n ω := by cases n · simp · exact (crossing_eq_crossing_of_upperCrossingTime_lt hNM h).1 theorem upcrossingsBefore_mono (hab : a < b) : Monotone fun N ω => upcrossingsBefore a b f N ω := by intro N M hNM ω simp only [upcrossingsBefore] by_cases hemp : {n : ℕ | upperCrossingTime a b f N n ω < N}.Nonempty · refine csSup_le_csSup (upperCrossingTime_lt_bddAbove hab) hemp fun n hn => ?_ rw [Set.mem_setOf_eq, upperCrossingTime_eq_upperCrossingTime_of_lt hNM hn] exact lt_of_lt_of_le hn hNM · rw [Set.not_nonempty_iff_eq_empty] at hemp simp [hemp, csSup_empty, bot_eq_zero', zero_le'] theorem upcrossingsBefore_lt_of_exists_upcrossing (hab : a < b) {N₁ N₂ : ℕ} (hN₁ : N ≤ N₁) (hN₁' : f N₁ ω < a) (hN₂ : N₁ ≤ N₂) (hN₂' : b < f N₂ ω) : upcrossingsBefore a b f N ω < upcrossingsBefore a b f (N₂ + 1) ω := by refine lt_of_lt_of_le (Nat.lt_succ_self _) (le_csSup (upperCrossingTime_lt_bddAbove hab) ?_) rw [Set.mem_setOf_eq, upperCrossingTime_succ_eq, hitting_lt_iff _ le_rfl] refine ⟨N₂, ⟨?_, Nat.lt_succ_self _⟩, hN₂'.le⟩ rw [lowerCrossingTime, hitting_le_iff_of_lt _ (Nat.lt_succ_self _)] refine ⟨N₁, ⟨le_trans ?_ hN₁, hN₂⟩, hN₁'.le⟩ by_cases hN : 0 < N · have : upperCrossingTime a b f N (upcrossingsBefore a b f N ω) ω < N := Nat.sSup_mem (upperCrossingTime_lt_nonempty hN) (upperCrossingTime_lt_bddAbove hab) rw [upperCrossingTime_eq_upperCrossingTime_of_lt (hN₁.trans (hN₂.trans <| Nat.le_succ _)) this] exact this.le · rw [not_lt, Nat.le_zero] at hN rw [hN, upcrossingsBefore_zero, upperCrossingTime_zero, Pi.bot_apply, bot_eq_zero'] theorem lowerCrossingTime_lt_of_lt_upcrossingsBefore (hN : 0 < N) (hab : a < b) (hn : n < upcrossingsBefore a b f N ω) : lowerCrossingTime a b f N n ω < N := lt_of_le_of_lt lowerCrossingTime_le_upperCrossingTime_succ (upperCrossingTime_lt_of_le_upcrossingsBefore hN hab hn) theorem le_sub_of_le_upcrossingsBefore (hN : 0 < N) (hab : a < b) (hn : n < upcrossingsBefore a b f N ω) : b - a ≤ stoppedValue f (upperCrossingTime a b f N (n + 1)) ω - stoppedValue f (lowerCrossingTime a b f N n) ω := sub_le_sub (stoppedValue_upperCrossingTime (upperCrossingTime_lt_of_le_upcrossingsBefore hN hab hn).ne) (stoppedValue_lowerCrossingTime (lowerCrossingTime_lt_of_lt_upcrossingsBefore hN hab hn).ne) theorem sub_eq_zero_of_upcrossingsBefore_lt (hab : a < b) (hn : upcrossingsBefore a b f N ω < n) : stoppedValue f (upperCrossingTime a b f N (n + 1)) ω - stoppedValue f (lowerCrossingTime a b f N n) ω = 0 := by have : N ≤ upperCrossingTime a b f N n ω := by rw [upcrossingsBefore] at hn rw [← not_lt] exact fun h => not_le.2 hn (le_csSup (upperCrossingTime_lt_bddAbove hab) h) simp [stoppedValue, upperCrossingTime_stabilize' (Nat.le_succ n) this, lowerCrossingTime_stabilize' le_rfl (le_trans this upperCrossingTime_le_lowerCrossingTime)]
theorem mul_upcrossingsBefore_le (hf : a ≤ f N ω) (hab : a < b) : (b - a) * upcrossingsBefore a b f N ω ≤ ∑ k ∈ Finset.range N, upcrossingStrat a b f N k ω * (f (k + 1) - f k) ω := by classical by_cases hN : N = 0 · simp [hN] simp_rw [upcrossingStrat, Finset.sum_mul, ← Set.indicator_mul_left _ _ (fun x ↦ (f (x + 1) - f x) ω), Pi.one_apply, Pi.sub_apply, one_mul] rw [Finset.sum_comm]
Mathlib/Probability/Martingale/Upcrossing.lean
545
553
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro -/ import Mathlib.Data.List.Sigma /-! # Association Lists This file defines association lists. An association list is a list where every element consists of a key and a value, and no two entries have the same key. The type of the value is allowed to be dependent on the type of the key. This type dependence is implemented using `Sigma`: The elements of the list are of type `Sigma β`, for some type index `β`. ## Main definitions Association lists are represented by the `AList` structure. This file defines this structure and provides ways to access, modify, and combine `AList`s. * `AList.keys` returns a list of keys of the alist. * `AList.membership` returns membership in the set of keys. * `AList.erase` removes a certain key. * `AList.insert` adds a key-value mapping to the list. * `AList.union` combines two association lists. ## References * <https://en.wikipedia.org/wiki/Association_list> -/ universe u v w open List variable {α : Type u} {β : α → Type v} /-- `AList β` is a key-value map stored as a `List` (i.e. a linked list). It is a wrapper around certain `List` functions with the added constraint that the list have unique keys. -/ structure AList (β : α → Type v) : Type max u v where /-- The underlying `List` of an `AList` -/ entries : List (Sigma β) /-- There are no duplicate keys in `entries` -/ nodupKeys : entries.NodupKeys /-- Given `l : List (Sigma β)`, create a term of type `AList β` by removing entries with duplicate keys. -/ def List.toAList [DecidableEq α] {β : α → Type v} (l : List (Sigma β)) : AList β where entries := _ nodupKeys := nodupKeys_dedupKeys l namespace AList @[ext] theorem ext : ∀ {s t : AList β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩, ⟨l₂, _⟩, H => by congr instance [DecidableEq α] [∀ a, DecidableEq (β a)] : DecidableEq (AList β) := fun xs ys => by rw [AList.ext_iff]; infer_instance /-! ### keys -/ /-- The list of keys of an association list. -/ def keys (s : AList β) : List α := s.entries.keys theorem keys_nodup (s : AList β) : s.keys.Nodup := s.nodupKeys /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : Membership α (AList β) := ⟨fun s a => a ∈ s.keys⟩ theorem mem_keys {a : α} {s : AList β} : a ∈ s ↔ a ∈ s.keys := Iff.rfl theorem mem_of_perm {a : α} {s₁ s₂ : AList β} (p : s₁.entries ~ s₂.entries) : a ∈ s₁ ↔ a ∈ s₂ := (p.map Sigma.fst).mem_iff /-! ### empty -/ /-- The empty association list. -/ instance : EmptyCollection (AList β) := ⟨⟨[], nodupKeys_nil⟩⟩ instance : Inhabited (AList β) := ⟨∅⟩ @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : AList β) := not_mem_nil @[simp] theorem empty_entries : (∅ : AList β).entries = [] := rfl @[simp] theorem keys_empty : (∅ : AList β).keys = [] := rfl /-! ### singleton -/ /-- The singleton association list. -/ def singleton (a : α) (b : β a) : AList β := ⟨[⟨a, b⟩], nodupKeys_singleton _⟩ @[simp] theorem singleton_entries (a : α) (b : β a) : (singleton a b).entries = [Sigma.mk a b] := rfl @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = [a] := rfl /-! ### lookup -/ section variable [DecidableEq α] /-- Look up the value associated to a key in an association list. -/ def lookup (a : α) (s : AList β) : Option (β a) := s.entries.dlookup a @[simp] theorem lookup_empty (a) : lookup a (∅ : AList β) = none := rfl theorem lookup_isSome {a : α} {s : AList β} : (s.lookup a).isSome ↔ a ∈ s := dlookup_isSome theorem lookup_eq_none {a : α} {s : AList β} : lookup a s = none ↔ a ∉ s := dlookup_eq_none theorem mem_lookup_iff {a : α} {b : β a} {s : AList β} : b ∈ lookup a s ↔ Sigma.mk a b ∈ s.entries := mem_dlookup_iff s.nodupKeys theorem perm_lookup {a : α} {s₁ s₂ : AList β} (p : s₁.entries ~ s₂.entries) : s₁.lookup a = s₂.lookup a := perm_dlookup _ s₁.nodupKeys s₂.nodupKeys p instance (a : α) (s : AList β) : Decidable (a ∈ s) := decidable_of_iff _ lookup_isSome end theorem keys_subset_keys_of_entries_subset_entries {s₁ s₂ : AList β} (h : s₁.entries ⊆ s₂.entries) : s₁.keys ⊆ s₂.keys := by intro k hk letI : DecidableEq α := Classical.decEq α have := h (mem_lookup_iff.1 (Option.get_mem (lookup_isSome.2 hk))) rw [← mem_lookup_iff, Option.mem_def] at this rw [← mem_keys, ← lookup_isSome, this] exact Option.isSome_some /-! ### replace -/ section variable [DecidableEq α] /-- Replace a key with a given value in an association list. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : AList β) : AList β := ⟨kreplace a b s.entries, (kreplace_nodupKeys a b).2 s.nodupKeys⟩ @[simp] theorem keys_replace (a : α) (b : β a) (s : AList β) : (replace a b s).keys = s.keys := keys_kreplace _ _ _ @[simp] theorem mem_replace {a a' : α} {b : β a} {s : AList β} : a' ∈ replace a b s ↔ a' ∈ s := by rw [mem_keys, keys_replace, ← mem_keys] theorem perm_replace {a : α} {b : β a} {s₁ s₂ : AList β} : s₁.entries ~ s₂.entries → (replace a b s₁).entries ~ (replace a b s₂).entries := Perm.kreplace s₁.nodupKeys end /-- Fold a function over the key-value pairs in the map. -/ def foldl {δ : Type w} (f : δ → ∀ a, β a → δ) (d : δ) (m : AList β) : δ := m.entries.foldl (fun r a => f r a.1 a.2) d /-! ### erase -/ section variable [DecidableEq α] /-- Erase a key from the map. If the key is not present, do nothing. -/ def erase (a : α) (s : AList β) : AList β := ⟨s.entries.kerase a, s.nodupKeys.kerase a⟩ @[simp] theorem keys_erase (a : α) (s : AList β) : (erase a s).keys = s.keys.erase a := keys_kerase @[simp] theorem mem_erase {a a' : α} {s : AList β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := by rw [mem_keys, keys_erase, s.keys_nodup.mem_erase_iff, ← mem_keys] theorem perm_erase {a : α} {s₁ s₂ : AList β} : s₁.entries ~ s₂.entries → (erase a s₁).entries ~ (erase a s₂).entries := Perm.kerase s₁.nodupKeys @[simp] theorem lookup_erase (a) (s : AList β) : lookup a (erase a s) = none := dlookup_kerase a s.nodupKeys @[simp] theorem lookup_erase_ne {a a'} {s : AList β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := dlookup_kerase_ne h theorem erase_erase (a a' : α) (s : AList β) : (s.erase a).erase a' = (s.erase a').erase a := ext <| kerase_kerase /-! ### insert -/ /-- Insert a key-value pair into an association list and erase any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : AList β) : AList β := ⟨kinsert a b s.entries, kinsert_nodupKeys a b s.nodupKeys⟩ @[simp] theorem entries_insert {a} {b : β a} {s : AList β} : (insert a b s).entries = Sigma.mk a b :: kerase a s.entries := rfl @[deprecated (since := "2024-12-17")] alias insert_entries := entries_insert theorem entries_insert_of_not_mem {a} {b : β a} {s : AList β} (h : a ∉ s) : (insert a b s).entries = ⟨a, b⟩ :: s.entries := by rw [entries_insert, kerase_of_not_mem_keys h] theorem insert_of_not_mem {a} {b : β a} {s : AList β} (h : a ∉ s) : insert a b s = ⟨⟨a, b⟩ :: s.entries, nodupKeys_cons.2 ⟨h, s.2⟩⟩ := ext <| entries_insert_of_not_mem h @[deprecated (since := "2024-12-14")] alias insert_entries_of_neg := entries_insert_of_not_mem @[deprecated (since := "2024-12-14")] alias insert_of_neg := insert_of_not_mem @[simp] theorem insert_empty (a) (b : β a) : insert a b ∅ = singleton a b := rfl @[simp] theorem mem_insert {a a'} {b' : β a'} (s : AList β) : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := mem_keys_kinsert @[simp] theorem keys_insert {a} {b : β a} (s : AList β) : (insert a b s).keys = a :: s.keys.erase a := by simp [insert, keys, keys_kerase] theorem perm_insert {a} {b : β a} {s₁ s₂ : AList β} (p : s₁.entries ~ s₂.entries) : (insert a b s₁).entries ~ (insert a b s₂).entries := by simp only [entries_insert]; exact p.kinsert s₁.nodupKeys @[simp] theorem lookup_insert {a} {b : β a} (s : AList β) : lookup a (insert a b s) = some b := by simp only [lookup, insert, dlookup_kinsert] @[simp] theorem lookup_insert_ne {a a'} {b' : β a'} {s : AList β} (h : a ≠ a') : lookup a (insert a' b' s) = lookup a s := dlookup_kinsert_ne h @[simp] theorem lookup_insert_eq_none {l : AList β} {k k' : α} {v : β k} : (l.insert k v).lookup k' = none ↔ (k' ≠ k) ∧ l.lookup k' = none := by by_cases h : k' = k · subst h; simp · simp_all [lookup_insert_ne h] @[simp] theorem lookup_to_alist {a} (s : List (Sigma β)) : lookup a s.toAList = s.dlookup a := by rw [List.toAList, lookup, dlookup_dedupKeys] @[simp] theorem insert_insert {a} {b b' : β a} (s : AList β) : (s.insert a b).insert a b' = s.insert a b' := by ext : 1; simp only [AList.entries_insert, List.kerase_cons_eq] theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : AList β) (h : a ≠ a') : ((s.insert a b).insert a' b').entries ~ ((s.insert a' b').insert a b).entries := by simp only [entries_insert]; rw [kerase_cons_ne, kerase_cons_ne, kerase_comm] <;> [apply Perm.swap; exact h; exact h.symm] @[simp] theorem insert_singleton_eq {a : α} {b b' : β a} : insert a b (singleton a b') = singleton a b := ext <| by simp only [AList.entries_insert, List.kerase_cons_eq, and_self_iff, AList.singleton_entries, heq_iff_eq, eq_self_iff_true] @[simp] theorem entries_toAList (xs : List (Sigma β)) : (List.toAList xs).entries = dedupKeys xs := rfl theorem toAList_cons (a : α) (b : β a) (xs : List (Sigma β)) : List.toAList (⟨a, b⟩ :: xs) = insert a b xs.toAList := rfl theorem mk_cons_eq_insert (c : Sigma β) (l : List (Sigma β)) (h : (c :: l).NodupKeys) : (⟨c :: l, h⟩ : AList β) = insert c.1 c.2 ⟨l, nodupKeys_of_nodupKeys_cons h⟩ := by simpa [insert] using (kerase_of_not_mem_keys <| not_mem_keys_of_nodupKeys_cons h).symm /-- Recursion on an `AList`, using `insert`. Use as `induction l`. -/ @[elab_as_elim, induction_eliminator] def insertRec {C : AList β → Sort*} (H0 : C ∅) (IH : ∀ (a : α) (b : β a) (l : AList β), a ∉ l → C l → C (l.insert a b)) : ∀ l : AList β, C l | ⟨[], _⟩ => H0 | ⟨c :: l, h⟩ => by rw [mk_cons_eq_insert] refine IH _ _ _ ?_ (insertRec H0 IH _) exact not_mem_keys_of_nodupKeys_cons h -- Test that the `induction` tactic works on `insertRec`. example (l : AList β) : True := by induction l <;> trivial @[simp] theorem insertRec_empty {C : AList β → Sort*} (H0 : C ∅) (IH : ∀ (a : α) (b : β a) (l : AList β), a ∉ l → C l → C (l.insert a b)) : @insertRec α β _ C H0 IH ∅ = H0 := by change @insertRec α β _ C H0 IH ⟨[], _⟩ = H0 rw [insertRec] theorem insertRec_insert {C : AList β → Sort*} (H0 : C ∅) (IH : ∀ (a : α) (b : β a) (l : AList β), a ∉ l → C l → C (l.insert a b)) {c : Sigma β} {l : AList β} (h : c.1 ∉ l) : @insertRec α β _ C H0 IH (l.insert c.1 c.2) = IH c.1 c.2 l h (@insertRec α β _ C H0 IH l) := by obtain ⟨l, hl⟩ := l suffices HEq (@insertRec α β _ C H0 IH ⟨c :: l, nodupKeys_cons.2 ⟨h, hl⟩⟩) (IH c.1 c.2 ⟨l, hl⟩ h (@insertRec α β _ C H0 IH ⟨l, hl⟩)) by cases c apply eq_of_heq convert this <;> rw [insert_of_not_mem h] rw [insertRec] apply cast_heq theorem insertRec_insert_mk {C : AList β → Sort*} (H0 : C ∅) (IH : ∀ (a : α) (b : β a) (l : AList β), a ∉ l → C l → C (l.insert a b)) {a : α} (b : β a) {l : AList β} (h : a ∉ l) : @insertRec α β _ C H0 IH (l.insert a b) = IH a b l h (@insertRec α β _ C H0 IH l) := @insertRec_insert α β _ C H0 IH ⟨a, b⟩ l h /-! ### extract -/ /-- Erase a key from the map, and return the corresponding value, if found. -/ def extract (a : α) (s : AList β) : Option (β a) × AList β := have : (kextract a s.entries).2.NodupKeys := by rw [kextract_eq_dlookup_kerase]; exact s.nodupKeys.kerase _ match kextract a s.entries, this with | (b, l), h => (b, ⟨l, h⟩) @[simp] theorem extract_eq_lookup_erase (a : α) (s : AList β) : extract a s = (lookup a s, erase a s) := by simp [extract]; constructor <;> rfl /-! ### union -/ /-- `s₁ ∪ s₂` is the key-based union of two association lists. It is left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/ def union (s₁ s₂ : AList β) : AList β := ⟨s₁.entries.kunion s₂.entries, s₁.nodupKeys.kunion s₂.nodupKeys⟩ instance : Union (AList β) := ⟨union⟩ @[simp] theorem union_entries {s₁ s₂ : AList β} : (s₁ ∪ s₂).entries = kunion s₁.entries s₂.entries := rfl @[simp] theorem empty_union {s : AList β} : (∅ : AList β) ∪ s = s := ext rfl @[simp] theorem union_empty {s : AList β} : s ∪ (∅ : AList β) = s := ext <| by simp @[simp] theorem mem_union {a} {s₁ s₂ : AList β} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_keys_kunion theorem perm_union {s₁ s₂ s₃ s₄ : AList β} (p₁₂ : s₁.entries ~ s₂.entries) (p₃₄ : s₃.entries ~ s₄.entries) : (s₁ ∪ s₃).entries ~ (s₂ ∪ s₄).entries := by simp [p₁₂.kunion s₃.nodupKeys p₃₄] theorem union_erase (a : α) (s₁ s₂ : AList β) : erase a (s₁ ∪ s₂) = erase a s₁ ∪ erase a s₂ := ext kunion_kerase.symm @[simp] theorem lookup_union_left {a} {s₁ s₂ : AList β} : a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ := dlookup_kunion_left @[simp] theorem lookup_union_right {a} {s₁ s₂ : AList β} : a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ := dlookup_kunion_right -- The corresponding lemma in `simp`-normal form is `lookup_union_eq_some`. theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : AList β} : b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ := mem_dlookup_kunion @[simp] theorem lookup_union_eq_some {a} {b : β a} {s₁ s₂ : AList β} : lookup a (s₁ ∪ s₂) = some b ↔ lookup a s₁ = some b ∨ a ∉ s₁ ∧ lookup a s₂ = some b := mem_dlookup_kunion theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : AList β} : b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) := mem_dlookup_kunion_middle theorem insert_union {a} {b : β a} {s₁ s₂ : AList β} : insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ := by ext; simp theorem union_assoc {s₁ s₂ s₃ : AList β} : (s₁ ∪ s₂ ∪ s₃).entries ~ (s₁ ∪ (s₂ ∪ s₃)).entries := lookup_ext (AList.nodupKeys _) (AList.nodupKeys _) (by simp [not_or, or_assoc, and_or_left, and_assoc]) end /-! ### disjoint -/ /-- Two associative lists are disjoint if they have no common keys. -/ def Disjoint (s₁ s₂ : AList β) : Prop := ∀ k ∈ s₁.keys, ¬k ∈ s₂.keys variable [DecidableEq α] theorem union_comm_of_disjoint {s₁ s₂ : AList β} (h : Disjoint s₁ s₂) :
(s₁ ∪ s₂).entries ~ (s₂ ∪ s₁).entries := lookup_ext (AList.nodupKeys _) (AList.nodupKeys _)
Mathlib/Data/List/AList.lean
449
450
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import Mathlib.CategoryTheory.Abelian.Basic /-! # Idempotent complete categories In this file, we define the notion of idempotent complete categories (also known as Karoubian categories, or pseudoabelian in the case of preadditive categories). ## Main definitions - `IsIdempotentComplete C` expresses that `C` is idempotent complete, i.e. all idempotents in `C` split. Other characterisations of idempotent completeness are given by `isIdempotentComplete_iff_hasEqualizer_of_id_and_idempotent` and `isIdempotentComplete_iff_idempotents_have_kernels`. - `isIdempotentComplete_of_abelian` expresses that abelian categories are idempotent complete. - `isIdempotentComplete_iff_ofEquivalence` expresses that if two categories `C` and `D` are equivalent, then `C` is idempotent complete iff `D` is. - `isIdempotentComplete_iff_opposite` expresses that `Cᵒᵖ` is idempotent complete iff `C` is. ## References * [Stacks: Karoubian categories] https://stacks.math.columbia.edu/tag/09SF -/ open CategoryTheory open CategoryTheory.Category open CategoryTheory.Limits open CategoryTheory.Preadditive open Opposite namespace CategoryTheory variable (C : Type*) [Category C] /-- A category is idempotent complete iff all idempotent endomorphisms `p` split as a composition `p = e ≫ i` with `i ≫ e = 𝟙 _` -/ class IsIdempotentComplete : Prop where /-- A category is idempotent complete iff all idempotent endomorphisms `p` split as a composition `p = e ≫ i` with `i ≫ e = 𝟙 _` -/ idempotents_split : ∀ (X : C) (p : X ⟶ X), p ≫ p = p → ∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p namespace Idempotents /-- A category is idempotent complete iff for all idempotent endomorphisms, the equalizer of the identity and this idempotent exists. -/ theorem isIdempotentComplete_iff_hasEqualizer_of_id_and_idempotent : IsIdempotentComplete C ↔ ∀ (X : C) (p : X ⟶ X), p ≫ p = p → HasEqualizer (𝟙 X) p := by constructor · intro intro X p hp rcases IsIdempotentComplete.idempotents_split X p hp with ⟨Y, i, e, ⟨h₁, h₂⟩⟩ exact ⟨Nonempty.intro { cone := Fork.ofι i (show i ≫ 𝟙 X = i ≫ p by rw [comp_id, ← h₂, ← assoc, h₁, id_comp]) isLimit := by apply Fork.IsLimit.mk' intro s refine ⟨s.ι ≫ e, ?_⟩ constructor · erw [assoc, h₂, ← Limits.Fork.condition s, comp_id] · intro m hm rw [Fork.ι_ofι] at hm rw [← hm] simp only [← hm, assoc, h₁] exact (comp_id m).symm }⟩ · intro h refine ⟨?_⟩ intro X p hp haveI : HasEqualizer (𝟙 X) p := h X p hp refine ⟨equalizer (𝟙 X) p, equalizer.ι (𝟙 X) p, equalizer.lift p (show p ≫ 𝟙 X = p ≫ p by rw [hp, comp_id]), ?_, equalizer.lift_ι _ _⟩ ext simp only [assoc, limit.lift_π, Eq.ndrec, id_eq, eq_mpr_eq_cast, Fork.ofι_pt, Fork.ofι_π_app, id_comp] rw [← equalizer.condition, comp_id] variable {C} in /-- In a preadditive category, when `p : X ⟶ X` is idempotent, then `𝟙 X - p` is also idempotent. -/ theorem idem_of_id_sub_idem [Preadditive C] {X : C} (p : X ⟶ X) (hp : p ≫ p = p) : (𝟙 _ - p) ≫ (𝟙 _ - p) = 𝟙 _ - p := by simp only [comp_sub, sub_comp, id_comp, comp_id, hp, sub_self, sub_zero] /-- A preadditive category is pseudoabelian iff all idempotent endomorphisms have a kernel. -/ theorem isIdempotentComplete_iff_idempotents_have_kernels [Preadditive C] : IsIdempotentComplete C ↔ ∀ (X : C) (p : X ⟶ X), p ≫ p = p → HasKernel p := by rw [isIdempotentComplete_iff_hasEqualizer_of_id_and_idempotent] constructor · intro h X p hp haveI : HasEqualizer (𝟙 X) (𝟙 X - p) := h X (𝟙 _ - p) (idem_of_id_sub_idem p hp) convert hasKernel_of_hasEqualizer (𝟙 X) (𝟙 X - p) rw [sub_sub_cancel] · intro h X p hp haveI : HasKernel (𝟙 _ - p) := h X (𝟙 _ - p) (idem_of_id_sub_idem p hp) apply Preadditive.hasEqualizer_of_hasKernel /-- An abelian category is idempotent complete. -/ instance (priority := 100) isIdempotentComplete_of_abelian (D : Type*) [Category D] [Abelian D] : IsIdempotentComplete D := by rw [isIdempotentComplete_iff_idempotents_have_kernels] intros infer_instance variable {C} theorem split_imp_of_iso {X X' : C} (φ : X ≅ X') (p : X ⟶ X) (p' : X' ⟶ X') (hpp' : p ≫ φ.hom = φ.hom ≫ p') (h : ∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) : ∃ (Y' : C) (i' : Y' ⟶ X') (e' : X' ⟶ Y'), i' ≫ e' = 𝟙 Y' ∧ e' ≫ i' = p' := by rcases h with ⟨Y, i, e, ⟨h₁, h₂⟩⟩ use Y, i ≫ φ.hom, φ.inv ≫ e constructor · slice_lhs 2 3 => rw [φ.hom_inv_id] rw [id_comp, h₁] · slice_lhs 2 3 => rw [h₂] rw [hpp', ← assoc, φ.inv_hom_id, id_comp] theorem split_iff_of_iso {X X' : C} (φ : X ≅ X') (p : X ⟶ X) (p' : X' ⟶ X') (hpp' : p ≫ φ.hom = φ.hom ≫ p') : (∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) ↔ ∃ (Y' : C) (i' : Y' ⟶ X') (e' : X' ⟶ Y'), i' ≫ e' = 𝟙 Y' ∧ e' ≫ i' = p' := by constructor · exact split_imp_of_iso φ p p' hpp' · apply split_imp_of_iso φ.symm p' p rw [← comp_id p, ← φ.hom_inv_id] slice_rhs 2 3 => rw [hpp'] slice_rhs 1 2 => erw [φ.inv_hom_id] simp only [id_comp]
rfl theorem Equivalence.isIdempotentComplete {D : Type*} [Category D] (ε : C ≌ D) (h : IsIdempotentComplete C) : IsIdempotentComplete D := by refine ⟨?_⟩ intro X' p hp let φ := ε.counitIso.symm.app X' erw [split_iff_of_iso φ p (φ.inv ≫ p ≫ φ.hom) (by slice_rhs 1 2 => rw [φ.hom_inv_id] rw [id_comp])] rcases IsIdempotentComplete.idempotents_split (ε.inverse.obj X') (ε.inverse.map p)
Mathlib/CategoryTheory/Idempotents/Basic.lean
143
154
/- Copyright (c) 2023 Antoine Chambert-Loir and María Inés de Frutos-Fernández. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Antoine Chambert-Loir, María Inés de Frutos-Fernández, Eric Wieser, Bhavik Mehta, Yaël Dillies -/ import Mathlib.Algebra.Order.Antidiag.Pi import Mathlib.Data.Finsupp.Basic /-! # Antidiagonal of finitely supported functions as finsets This file defines the finset of finitely functions summing to a specific value on a finset. Such finsets should be thought of as the "antidiagonals" in the space of finitely supported functions. Precisely, for a commutative monoid `μ` with antidiagonals (see `Finset.HasAntidiagonal`), `Finset.finsuppAntidiag s n` is the finset of all finitely supported functions `f : ι →₀ μ` with support contained in `s` and such that the sum of its values equals `n : μ`. We define it using `Finset.piAntidiag s n`, the corresponding antidiagonal in `ι → μ`. ## Main declarations * `Finset.finsuppAntidiag s n`: Finset of all finitely supported functions `f : ι →₀ μ` with support contained in `s` and such that the sum of its values equals `n : μ`. -/ open Finsupp Function variable {ι μ μ' : Type*} namespace Finset section AddCommMonoid variable [DecidableEq ι] [AddCommMonoid μ] [HasAntidiagonal μ] [DecidableEq μ] {s : Finset ι} {n : μ} {f : ι →₀ μ} /-- The finset of functions `ι →₀ μ` with support contained in `s` and sum equal to `n`. -/ def finsuppAntidiag (s : Finset ι) (n : μ) : Finset (ι →₀ μ) := (piAntidiag s n).attach.map ⟨fun f ↦ ⟨s.filter (f.1 · ≠ 0), f.1, by simpa using (mem_piAntidiag.1 f.2).2⟩, fun _ _ hfg ↦ Subtype.ext (congr_arg (⇑) hfg)⟩ @[simp] lemma mem_finsuppAntidiag : f ∈ finsuppAntidiag s n ↔ s.sum f = n ∧ f.support ⊆ s := by simp [finsuppAntidiag, ← DFunLike.coe_fn_eq, subset_iff] lemma mem_finsuppAntidiag' : f ∈ finsuppAntidiag s n ↔ f.sum (fun _ x ↦ x) = n ∧ f.support ⊆ s := by simp only [mem_finsuppAntidiag, and_congr_left_iff] rintro hf rw [sum_of_support_subset (N := μ) f hf (fun _ x ↦ x) fun _ _ ↦ rfl] @[simp] lemma finsuppAntidiag_empty_zero : finsuppAntidiag (∅ : Finset ι) (0 : μ) = {0} := by ext f; simp [finsuppAntidiag, ← DFunLike.coe_fn_eq (g := f), eq_comm]
@[simp] lemma finsuppAntidiag_empty_of_ne_zero (hn : n ≠ 0) : finsuppAntidiag (∅ : Finset ι) n = ∅ := eq_empty_of_forall_not_mem (by simp [@eq_comm _ 0, hn.symm])
Mathlib/Algebra/Order/Antidiag/Finsupp.lean
55
57
/- Copyright (c) 2023 Dagur Asgeirsson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dagur Asgeirsson -/ import Mathlib.Topology.Category.Profinite.Nobeling.Basic import Mathlib.Topology.Category.Profinite.Nobeling.Induction import Mathlib.Topology.Category.Profinite.Nobeling.Span import Mathlib.Topology.Category.Profinite.Nobeling.Successor import Mathlib.Topology.Category.Profinite.Nobeling.ZeroLimit deprecated_module (since := "2025-04-13")
Mathlib/Topology/Category/Profinite/Nobeling.lean
430
448
/- Copyright (c) 2021 Praneeth Kolichala. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Praneeth Kolichala -/ import Mathlib.Topology.Constructions import Mathlib.Topology.Homotopy.Path /-! # Product of homotopies In this file, we introduce definitions for the product of homotopies. We show that the products of relative homotopies are still relative homotopies. Finally, we specialize to the case of path homotopies, and provide the definition for the product of path classes. We show various lemmas associated with these products, such as the fact that path products commute with path composition, and that projection is the inverse of products. ## Definitions ### General homotopies - `ContinuousMap.Homotopy.pi homotopies`: Let f and g be a family of functions indexed on I, such that for each i ∈ I, fᵢ and gᵢ are maps from A to Xᵢ. Let `homotopies` be a family of homotopies from fᵢ to gᵢ for each i. Then `Homotopy.pi homotopies` is the canonical homotopy from ∏ f to ∏ g, where ∏ f is the product map from A to Πi, Xᵢ, and similarly for ∏ g. - `ContinuousMap.HomotopyRel.pi homotopies`: Same as `ContinuousMap.Homotopy.pi`, but all homotopies are done relative to some set S ⊆ A. - `ContinuousMap.Homotopy.prod F G` is the product of homotopies F and G, where F is a homotopy between f₀ and f₁, G is a homotopy between g₀ and g₁. The result F × G is a homotopy between (f₀ × g₀) and (f₁ × g₁). Again, all homotopies are done relative to S. - `ContinuousMap.HomotopyRel.prod F G`: Same as `ContinuousMap.Homotopy.prod`, but all homotopies are done relative to some set S ⊆ A. ### Path products - `Path.Homotopic.pi` The product of a family of path classes, where a path class is an equivalence class of paths up to path homotopy. - `Path.Homotopic.prod` The product of two path classes. -/ noncomputable section namespace ContinuousMap open ContinuousMap section Pi variable {I A : Type*} {X : I → Type*} [∀ i, TopologicalSpace (X i)] [TopologicalSpace A] {f g : ∀ i, C(A, X i)} {S : Set A} /-- The relative product homotopy of `homotopies` between functions `f` and `g` -/ @[simps!] def HomotopyRel.pi (homotopies : ∀ i : I, HomotopyRel (f i) (g i) S) : HomotopyRel (pi f) (pi g) S := { Homotopy.pi fun i => (homotopies i).toHomotopy with prop' := by intro t x hx dsimp only [coe_mk, pi_eval, toFun_eq_coe, HomotopyWith.coe_toContinuousMap] simp only [funext_iff, ← forall_and] intro i exact (homotopies i).prop' t x hx } end Pi section Prod variable {α β : Type*} [TopologicalSpace α] [TopologicalSpace β] {A : Type*} [TopologicalSpace A] {f₀ f₁ : C(A, α)} {g₀ g₁ : C(A, β)} {S : Set A} /-- The product of homotopies `F` and `G`, where `F` takes `f₀` to `f₁` and `G` takes `g₀` to `g₁` -/ @[simps] def Homotopy.prod (F : Homotopy f₀ f₁) (G : Homotopy g₀ g₁) : Homotopy (ContinuousMap.prodMk f₀ g₀) (ContinuousMap.prodMk f₁ g₁) where toFun t := (F t, G t) map_zero_left x := by simp only [prod_eval, Homotopy.apply_zero] map_one_left x := by simp only [prod_eval, Homotopy.apply_one] /-- The relative product of homotopies `F` and `G`, where `F` takes `f₀` to `f₁` and `G` takes `g₀` to `g₁` -/ @[simps!] def HomotopyRel.prod (F : HomotopyRel f₀ f₁ S) (G : HomotopyRel g₀ g₁ S) : HomotopyRel (prodMk f₀ g₀) (prodMk f₁ g₁) S where toHomotopy := Homotopy.prod F.toHomotopy G.toHomotopy prop' t x hx := Prod.ext (F.prop' t x hx) (G.prop' t x hx) end Prod end ContinuousMap namespace Path.Homotopic attribute [local instance] Path.Homotopic.setoid local infixl:70 " ⬝ " => Quotient.comp section Pi variable {ι : Type*} {X : ι → Type*} [∀ i, TopologicalSpace (X i)] {as bs cs : ∀ i, X i} /-- The product of a family of path homotopies. This is just a specialization of `HomotopyRel`. -/ def piHomotopy (γ₀ γ₁ : ∀ i, Path (as i) (bs i)) (H : ∀ i, Path.Homotopy (γ₀ i) (γ₁ i)) : Path.Homotopy (Path.pi γ₀) (Path.pi γ₁) := ContinuousMap.HomotopyRel.pi H /-- The product of a family of path homotopy classes. -/ def pi (γ : ∀ i, Path.Homotopic.Quotient (as i) (bs i)) : Path.Homotopic.Quotient as bs := (Quotient.map Path.pi fun x y hxy => Nonempty.map (piHomotopy x y) (Classical.nonempty_pi.mpr hxy)) (Quotient.choice γ) theorem pi_lift (γ : ∀ i, Path (as i) (bs i)) : (Path.Homotopic.pi fun i => ⟦γ i⟧) = ⟦Path.pi γ⟧ := by unfold pi; simp /-- Composition and products commute. This is `Path.trans_pi_eq_pi_trans` descended to path homotopy classes. -/ theorem comp_pi_eq_pi_comp (γ₀ : ∀ i, Path.Homotopic.Quotient (as i) (bs i)) (γ₁ : ∀ i, Path.Homotopic.Quotient (bs i) (cs i)) : pi γ₀ ⬝ pi γ₁ = pi fun i ↦ γ₀ i ⬝ γ₁ i := by induction γ₁ using Quotient.induction_on_pi with | _ a => induction γ₀ using Quotient.induction_on_pi simp only [pi_lift] rw [← Path.Homotopic.comp_lift, Path.trans_pi_eq_pi_trans, ← pi_lift] rfl /-- Abbreviation for projection onto the ith coordinate. -/ abbrev proj (i : ι) (p : Path.Homotopic.Quotient as bs) : Path.Homotopic.Quotient (as i) (bs i) := p.mapFn ⟨_, continuous_apply i⟩
/-- Lemmas showing projection is the inverse of pi. -/
Mathlib/Topology/Homotopy/Product.lean
135
136
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.Algebra.Polynomial.Eval.Defs import Mathlib.LinearAlgebra.Dimension.Constructions /-! # Linear recurrence Informally, a "linear recurrence" is an assertion of the form `∀ n : ℕ, u (n + d) = a 0 * u n + a 1 * u (n+1) + ... + a (d-1) * u (n+d-1)`, where `u` is a sequence, `d` is the *order* of the recurrence and the `a i` are its *coefficients*. In this file, we define the structure `LinearRecurrence` so that `LinearRecurrence.mk d a` represents the above relation, and we call a sequence `u` which verifies it a *solution* of the linear recurrence. We prove a few basic lemmas about this concept, such as : * the space of solutions is a submodule of `(ℕ → α)` (i.e a vector space if `α` is a field) * the function that maps a solution `u` to its first `d` terms builds a `LinearEquiv` between the solution space and `Fin d → α`, aka `α ^ d`. As a consequence, two solutions are equal if and only if their first `d` terms are equals. * a geometric sequence `q ^ n` is solution iff `q` is a root of a particular polynomial, which we call the *characteristic polynomial* of the recurrence Of course, although we can inductively generate solutions (cf `mkSol`), the interesting part would be to determinate closed-forms for the solutions. This is currently *not implemented*, as we are waiting for definition and properties of eigenvalues and eigenvectors. -/ noncomputable section open Finset open Polynomial /-- A "linear recurrence relation" over a commutative semiring is given by its order `n` and `n` coefficients. -/ structure LinearRecurrence (R : Type*) [CommSemiring R] where /-- Order of the linear recurrence -/ order : ℕ /-- Coefficients of the linear recurrence -/ coeffs : Fin order → R instance (R : Type*) [CommSemiring R] : Inhabited (LinearRecurrence R) := ⟨⟨0, default⟩⟩ namespace LinearRecurrence section CommSemiring variable {R : Type*} [CommSemiring R] (E : LinearRecurrence R) /-- We say that a sequence `u` is solution of `LinearRecurrence order coeffs` when we have `u (n + order) = ∑ i : Fin order, coeffs i * u (n + i)` for any `n`. -/ def IsSolution (u : ℕ → R) := ∀ n, u (n + E.order) = ∑ i, E.coeffs i * u (n + i) /-- A solution of a `LinearRecurrence` which satisfies certain initial conditions. We will prove this is the only such solution. -/ def mkSol (init : Fin E.order → R) : ℕ → R | n => if h : n < E.order then init ⟨n, h⟩ else ∑ k : Fin E.order, have _ : n - E.order + k < n := by omega E.coeffs k * mkSol init (n - E.order + k) /-- `E.mkSol` indeed gives solutions to `E`. -/ theorem is_sol_mkSol (init : Fin E.order → R) : E.IsSolution (E.mkSol init) := by intro n rw [mkSol] simp /-- `E.mkSol init`'s first `E.order` terms are `init`. -/ theorem mkSol_eq_init (init : Fin E.order → R) : ∀ n : Fin E.order, E.mkSol init n = init n := by intro n rw [mkSol] simp only [n.is_lt, dif_pos, Fin.mk_val, Fin.eta] /-- If `u` is a solution to `E` and `init` designates its first `E.order` values, then `∀ n, u n = E.mkSol init n`. -/ theorem eq_mk_of_is_sol_of_eq_init {u : ℕ → R} {init : Fin E.order → R} (h : E.IsSolution u) (heq : ∀ n : Fin E.order, u n = init n) : ∀ n, u n = E.mkSol init n := by intro n rw [mkSol] split_ifs with h' · exact mod_cast heq ⟨n, h'⟩ · dsimp only rw [← tsub_add_cancel_of_le (le_of_not_lt h'), h (n - E.order)] congr with k have : n - E.order + k < n := by omega rw [eq_mk_of_is_sol_of_eq_init h heq (n - E.order + k)] simp /-- If `u` is a solution to `E` and `init` designates its first `E.order` values, then `u = E.mkSol init`. This proves that `E.mkSol init` is the only solution of `E` whose first `E.order` values are given by `init`. -/ theorem eq_mk_of_is_sol_of_eq_init' {u : ℕ → R} {init : Fin E.order → R} (h : E.IsSolution u) (heq : ∀ n : Fin E.order, u n = init n) : u = E.mkSol init := funext (E.eq_mk_of_is_sol_of_eq_init h heq) /-- The space of solutions of `E`, as a `Submodule` over `R` of the module `ℕ → R`. -/ def solSpace : Submodule R (ℕ → R) where carrier := { u | E.IsSolution u } zero_mem' n := by simp add_mem' {u v} hu hv n := by simp [mul_add, sum_add_distrib, hu n, hv n] smul_mem' a u hu n := by simp [hu n, mul_sum]; congr; ext; ac_rfl /-- Defining property of the solution space : `u` is a solution iff it belongs to the solution space. -/ theorem is_sol_iff_mem_solSpace (u : ℕ → R) : E.IsSolution u ↔ u ∈ E.solSpace := Iff.rfl /-- The function that maps a solution `u` of `E` to its first `E.order` terms as a `LinearEquiv`. -/ def toInit : E.solSpace ≃ₗ[R] Fin E.order → R where toFun u x := (u : ℕ → R) x map_add' u v := by ext simp map_smul' a u := by ext simp invFun u := ⟨E.mkSol u, E.is_sol_mkSol u⟩ left_inv u := by ext n; symm; apply E.eq_mk_of_is_sol_of_eq_init u.2; intro k; rfl right_inv u := funext_iff.mpr fun n ↦ E.mkSol_eq_init u n /-- Two solutions are equal iff they are equal on `range E.order`. -/ theorem sol_eq_of_eq_init (u v : ℕ → R) (hu : E.IsSolution u) (hv : E.IsSolution v) : u = v ↔ Set.EqOn u v ↑(range E.order) := by refine Iff.intro (fun h x _ ↦ h ▸ rfl) ?_ intro h set u' : ↥E.solSpace := ⟨u, hu⟩ set v' : ↥E.solSpace := ⟨v, hv⟩ change u'.val = v'.val suffices h' : u' = v' from h' ▸ rfl rw [← E.toInit.toEquiv.apply_eq_iff_eq, LinearEquiv.coe_toEquiv] ext x exact mod_cast h (mem_range.mpr x.2) /-! `E.tupleSucc` maps `![s₀, s₁, ..., sₙ]` to `![s₁, ..., sₙ, ∑ (E.coeffs i) * sᵢ]`, where `n := E.order`. This operation is quite useful for determining closed-form solutions of `E`. -/ /-- `E.tupleSucc` maps `![s₀, s₁, ..., sₙ]` to `![s₁, ..., sₙ, ∑ (E.coeffs i) * sᵢ]`, where `n := E.order`. -/
def tupleSucc : (Fin E.order → R) →ₗ[R] Fin E.order → R where toFun X i := if h : (i : ℕ) + 1 < E.order then X ⟨i + 1, h⟩ else ∑ i, E.coeffs i * X i map_add' x y := by ext i split_ifs with h <;> simp [h, mul_add, sum_add_distrib] map_smul' x y := by ext i split_ifs with h <;> simp [h, mul_sum] exact sum_congr rfl fun x _ ↦ by ac_rfl end CommSemiring
Mathlib/Algebra/LinearRecurrence.lean
156
166
/- 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.Algebra.Group.Action.Pi import Mathlib.Algebra.Group.End import Mathlib.Algebra.Module.NatInt import Mathlib.Algebra.Order.Archimedean.Basic /-! # Maps (semi)conjugating a shift to a shift Denote by $S^1$ the unit circle `UnitAddCircle`. A common way to study a self-map $f\colon S^1\to S^1$ of degree `1` is to lift it to a map $\tilde f\colon \mathbb R\to \mathbb R$ such that $\tilde f(x + 1) = \tilde f(x)+1$ for all `x`. In this file we define a structure and a typeclass for bundled maps satisfying `f (x + a) = f x + b`. We use parameters `a` and `b` instead of `1` to accommodate for two use cases: - maps between circles of different lengths; - self-maps $f\colon S^1\to S^1$ of degree other than one, including orientation-reversing maps. -/ assert_not_exists Finset open Function Set /-- A bundled map `f : G → H` such that `f (x + a) = f x + b` for all `x`, denoted as `f: G →+c[a, b] H`. One can think about `f` as a lift to `G` of a map between two `AddCircle`s. -/ structure AddConstMap (G H : Type*) [Add G] [Add H] (a : G) (b : H) where /-- The underlying function of an `AddConstMap`. Use automatic coercion to function instead. -/ protected toFun : G → H /-- An `AddConstMap` satisfies `f (x + a) = f x + b`. Use `map_add_const` instead. -/ map_add_const' (x : G) : toFun (x + a) = toFun x + b @[inherit_doc] scoped [AddConstMap] notation:25 G " →+c[" a ", " b "] " H => AddConstMap G H a b /-- Typeclass for maps satisfying `f (x + a) = f x + b`. Note that `a` and `b` are `outParam`s, so one should not add instances like `[AddConstMapClass F G H a b] : AddConstMapClass F G H (-a) (-b)`. -/ class AddConstMapClass (F : Type*) (G H : outParam Type*) [Add G] [Add H] (a : outParam G) (b : outParam H) [FunLike F G H] : Prop where /-- A map of `AddConstMapClass` class semiconjugates shift by `a` to the shift by `b`: `∀ x, f (x + a) = f x + b`. -/ map_add_const (f : F) (x : G) : f (x + a) = f x + b namespace AddConstMapClass /-! ### Properties of `AddConstMapClass` maps In this section we prove properties like `f (x + n • a) = f x + n • b`. -/ scoped [AddConstMapClass] attribute [simp] map_add_const variable {F G H : Type*} [FunLike F G H] {a : G} {b : H} protected theorem semiconj [Add G] [Add H] [AddConstMapClass F G H a b] (f : F) : Semiconj f (· + a) (· + b) := map_add_const f @[scoped simp] theorem map_add_nsmul [AddMonoid G] [AddMonoid H] [AddConstMapClass F G H a b] (f : F) (x : G) (n : ℕ) : f (x + n • a) = f x + n • b := by simpa using (AddConstMapClass.semiconj f).iterate_right n x @[scoped simp] theorem map_add_nat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) : f (x + n) = f x + n • b := by simp [← map_add_nsmul] theorem map_add_one [AddMonoidWithOne G] [Add H] [AddConstMapClass F G H 1 b] (f : F) (x : G) : f (x + 1) = f x + b := map_add_const f x @[scoped simp] theorem map_add_ofNat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) [n.AtLeastTwo] : f (x + ofNat(n)) = f x + (ofNat(n) : ℕ) • b := map_add_nat' f x n theorem map_add_nat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℕ) : f (x + n) = f x + n := by simp theorem map_add_ofNat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℕ) [n.AtLeastTwo] : f (x + ofNat(n)) = f x + ofNat(n) := map_add_nat f x n @[scoped simp] theorem map_const [AddZeroClass G] [Add H] [AddConstMapClass F G H a b] (f : F) : f a = f 0 + b := by simpa using map_add_const f 0 theorem map_one [AddZeroClass G] [One G] [Add H] [AddConstMapClass F G H 1 b] (f : F) : f 1 = f 0 + b := map_const f @[scoped simp] theorem map_nsmul_const [AddMonoid G] [AddMonoid H] [AddConstMapClass F G H a b] (f : F) (n : ℕ) : f (n • a) = f 0 + n • b := by simpa using map_add_nsmul f 0 n @[scoped simp] theorem map_nat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) : f n = f 0 + n • b := by simpa using map_add_nat' f 0 n theorem map_ofNat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) [n.AtLeastTwo] : f (ofNat(n)) = f 0 + (ofNat(n) : ℕ) • b := map_nat' f n theorem map_nat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) : f n = f 0 + n := by simp theorem map_ofNat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) [n.AtLeastTwo] : f ofNat(n) = f 0 + ofNat(n) := map_nat f n @[scoped simp] theorem map_const_add [AddCommMagma G] [Add H] [AddConstMapClass F G H a b] (f : F) (x : G) : f (a + x) = f x + b := by rw [add_comm, map_add_const] theorem map_one_add [AddCommMonoidWithOne G] [Add H] [AddConstMapClass F G H 1 b] (f : F) (x : G) : f (1 + x) = f x + b := map_const_add f x @[scoped simp] theorem map_nsmul_add [AddCommMonoid G] [AddMonoid H] [AddConstMapClass F G H a b] (f : F) (n : ℕ) (x : G) : f (n • a + x) = f x + n • b := by rw [add_comm, map_add_nsmul] @[scoped simp] theorem map_nat_add' [AddCommMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) (x : G) : f (↑n + x) = f x + n • b := by simpa using map_nsmul_add f n x theorem map_ofNat_add' [AddCommMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) [n.AtLeastTwo] (x : G) : f (ofNat(n) + x) = f x + ofNat(n) • b := map_nat_add' f n x theorem map_nat_add [AddCommMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) (x : G) : f (↑n + x) = f x + n := by simp theorem map_ofNat_add [AddCommMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) [n.AtLeastTwo] (x : G) : f (ofNat(n) + x) = f x + ofNat(n) := map_nat_add f n x @[scoped simp] theorem map_sub_nsmul [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) (n : ℕ) : f (x - n • a) = f x - n • b := by conv_rhs => rw [← sub_add_cancel x (n • a), map_add_nsmul, add_sub_cancel_right] @[scoped simp] theorem map_sub_const [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) : f (x - a) = f x - b := by simpa using map_sub_nsmul f x 1 theorem map_sub_one [AddGroup G] [One G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) : f (x - 1) = f x - b := map_sub_const f x @[scoped simp] theorem map_sub_nat' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) : f (x - n) = f x - n • b := by simpa using map_sub_nsmul f x n @[scoped simp] theorem map_sub_ofNat' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) [n.AtLeastTwo] : f (x - ofNat(n)) = f x - ofNat(n) • b := map_sub_nat' f x n @[scoped simp] theorem map_add_zsmul [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) : ∀ n : ℤ, f (x + n • a) = f x + n • b | (n : ℕ) => by simp | .negSucc n => by simp [← sub_eq_add_neg] @[scoped simp] theorem map_zsmul_const [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (n : ℤ) : f (n • a) = f 0 + n • b := by simpa using map_add_zsmul f 0 n @[scoped simp] theorem map_add_int' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℤ) : f (x + n) = f x + n • b := by rw [← map_add_zsmul f x n, zsmul_one] theorem map_add_int [AddGroupWithOne G] [AddGroupWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℤ) : f (x + n) = f x + n := by simp @[scoped simp] theorem map_sub_zsmul [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) (n : ℤ) : f (x - n • a) = f x - n • b := by simpa [sub_eq_add_neg] using map_add_zsmul f x (-n)
@[scoped simp] theorem map_sub_int' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℤ) : f (x - n) = f x - n • b := by
Mathlib/Algebra/AddConstMap/Basic.lean
210
212