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) 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, Kevin Buzzard, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import Mathlib.Algebra.Module.Submodule.Ker import Mathlib.Algebra.Module.Submodule.RestrictScalars import Mathlib.Data.Set.Finite.Range /-! # Range of linear maps The range `LinearMap.range` of a (semi)linear map `f : M → M₂` is a submodule of `M₂`. More specifically, `LinearMap.range` applies to any `SemilinearMapClass` over a `RingHomSurjective` ring homomorphism. Note that this also means that dot notation (i.e. `f.range` for a linear map `f`) does not work. ## Notations * We continue to use the notations `M →ₛₗ[σ] M₂` and `M →ₗ[R] M₂` for the type of semilinear (resp. linear) maps from `M` to `M₂` over the ring homomorphism `σ` (resp. over the ring `R`). ## Tags linear algebra, vector space, module, range -/ open Function variable {R : Type*} {R₂ : Type*} {R₃ : Type*} variable {K : Type*} variable {M : Type*} {M₂ : Type*} {M₃ : Type*} variable {V : Type*} {V₂ : Type*} namespace LinearMap section AddCommMonoid variable [Semiring R] [Semiring R₂] [Semiring R₃] variable [AddCommMonoid M] [AddCommMonoid M₂] [AddCommMonoid M₃] variable [Module R M] [Module R₂ M₂] [Module R₃ M₃] open Submodule variable {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variable [RingHomCompTriple τ₁₂ τ₂₃ τ₁₃] section variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. See Note [range copy pattern]. -/ def range [RingHomSurjective τ₁₂] (f : F) : Submodule R₂ M₂ := (map f ⊤).copy (Set.range f) Set.image_univ.symm theorem range_coe [RingHomSurjective τ₁₂] (f : F) : (range f : Set M₂) = Set.range f := rfl theorem range_toAddSubmonoid [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : (range f).toAddSubmonoid = AddMonoidHom.mrange f := rfl @[simp] theorem mem_range [RingHomSurjective τ₁₂] {f : F} {x} : x ∈ range f ↔ ∃ y, f y = x := Iff.rfl theorem range_eq_map [RingHomSurjective τ₁₂] (f : F) : range f = map f ⊤ := by ext simp theorem mem_range_self [RingHomSurjective τ₁₂] (f : F) (x : M) : f x ∈ range f := ⟨x, rfl⟩ @[simp] theorem range_id : range (LinearMap.id : M →ₗ[R] M) = ⊤ := SetLike.coe_injective Set.range_id theorem range_comp [RingHomSurjective τ₁₂] [RingHomSurjective τ₂₃] [RingHomSurjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = map g (range f) := SetLike.coe_injective (Set.range_comp g f) theorem range_comp_le_range [RingHomSurjective τ₂₃] [RingHomSurjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) ≤ range g := SetLike.coe_mono (Set.range_comp_subset_range f g) theorem range_eq_top [RingHomSurjective τ₁₂] {f : F} : range f = ⊤ ↔ Surjective f := by rw [SetLike.ext'_iff, range_coe, top_coe, Set.range_eq_univ] theorem range_eq_top_of_surjective [RingHomSurjective τ₁₂] (f : F) (hf : Surjective f) : range f = ⊤ := range_eq_top.2 hf theorem range_le_iff_comap [RingHomSurjective τ₁₂] {f : F} {p : Submodule R₂ M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range_eq_map, map_le_iff_le_comap, eq_top_iff] theorem map_le_range [RingHomSurjective τ₁₂] {f : F} {p : Submodule R M} : map f p ≤ range f := SetLike.coe_mono (Set.image_subset_range f p) @[simp] theorem range_neg {R : Type*} {R₂ : Type*} {M : Type*} {M₂ : Type*} [Semiring R] [Ring R₂] [AddCommMonoid M] [AddCommGroup M₂] [Module R M] [Module R₂ M₂] {τ₁₂ : R →+* R₂} [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : LinearMap.range (-f) = LinearMap.range f := by change range ((-LinearMap.id : M₂ →ₗ[R₂] M₂).comp f) = _ rw [range_comp, Submodule.map_neg, Submodule.map_id] @[simp] lemma range_domRestrict [Module R M₂] (K : Submodule R M) (f : M →ₗ[R] M₂) : range (domRestrict f K) = K.map f := by ext; simp lemma range_domRestrict_le_range [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) (S : Submodule R M) : LinearMap.range (f.domRestrict S) ≤ LinearMap.range f := by rintro x ⟨⟨y, hy⟩, rfl⟩ exact LinearMap.mem_range_self f y @[simp] theorem _root_.AddMonoidHom.coe_toIntLinearMap_range {M M₂ : Type*} [AddCommGroup M] [AddCommGroup M₂] (f : M →+ M₂) : LinearMap.range f.toIntLinearMap = AddSubgroup.toIntSubmodule f.range := rfl lemma _root_.Submodule.map_comap_eq_of_le [RingHomSurjective τ₁₂] {f : F} {p : Submodule R₂ M₂} (h : p ≤ LinearMap.range f) : (p.comap f).map f = p := SetLike.coe_injective <| Set.image_preimage_eq_of_subset h lemma range_restrictScalars [SMul R R₂] [Module R₂ M] [Module R M₂] [CompatibleSMul M M₂ R R₂] [IsScalarTower R R₂ M₂] (f : M →ₗ[R₂] M₂) : LinearMap.range (f.restrictScalars R) = (LinearMap.range f).restrictScalars R := rfl end /-- The decreasing sequence of submodules consisting of the ranges of the iterates of a linear map. -/ @[simps] def iterateRange (f : M →ₗ[R] M) : ℕ →o (Submodule R M)ᵒᵈ where toFun n := LinearMap.range (f ^ n) monotone' n m w x h := by obtain ⟨c, rfl⟩ := Nat.exists_eq_add_of_le w rw [LinearMap.mem_range] at h obtain ⟨m, rfl⟩ := h rw [LinearMap.mem_range] use (f ^ c) m rw [pow_add, Module.End.mul_apply] /-- Restrict the codomain of a linear map `f` to `f.range`. This is the bundled version of `Set.rangeFactorization`. -/ abbrev rangeRestrict [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : M →ₛₗ[τ₁₂] LinearMap.range f := f.codRestrict (LinearMap.range f) (LinearMap.mem_range_self f) /-- The range of a linear map is finite if the domain is finite. Note: this instance can form a diamond with `Subtype.fintype` in the presence of `Fintype M₂`. -/ instance fintypeRange [Fintype M] [DecidableEq M₂] [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : Fintype (range f) := Set.fintypeRange f variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] theorem range_codRestrict {τ₂₁ : R₂ →+* R} [RingHomSurjective τ₂₁] (p : Submodule R M) (f : M₂ →ₛₗ[τ₂₁] M) (hf) : range (codRestrict p f hf) = comap p.subtype (LinearMap.range f) := by simpa only [range_eq_map] using map_codRestrict _ _ _ _ theorem _root_.Submodule.map_comap_eq [RingHomSurjective τ₁₂] (f : F) (q : Submodule R₂ M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf map_le_range (map_comap_le _ _)) <| by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ theorem _root_.Submodule.map_comap_eq_self [RingHomSurjective τ₁₂] {f : F} {q : Submodule R₂ M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [Submodule.map_comap_eq, inf_eq_right] @[simp] theorem range_zero [RingHomSurjective τ₁₂] : range (0 : M →ₛₗ[τ₁₂] M₂) = ⊥ := by simpa only [range_eq_map] using Submodule.map_zero _ section variable [RingHomSurjective τ₁₂] theorem range_le_bot_iff (f : M →ₛₗ[τ₁₂] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem range_eq_bot {f : M →ₛₗ[τ₁₂] M₂} : range f = ⊥ ↔ f = 0 := by rw [← range_le_bot_iff, le_bot_iff] theorem range_le_ker_iff {f : M →ₛₗ[τ₁₂] M₂} {g : M₂ →ₛₗ[τ₂₃] M₃} : range f ≤ ker g ↔ (g.comp f : M →ₛₗ[τ₁₃] M₃) = 0 := ⟨fun h => ker_eq_top.1 <| eq_top_iff'.2 fun _ => h <| ⟨_, rfl⟩, fun h x hx => mem_ker.2 <| Exists.elim hx fun y hy => by rw [← hy, ← comp_apply, h, zero_apply]⟩ theorem comap_le_comap_iff {f : F} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨fun H ↦ by rwa [SetLike.le_def, (range_eq_top.1 hf).forall], comap_mono⟩ theorem comap_injective {f : F} (hf : range f = ⊤) : Injective (comap f) := fun _ _ h => le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) -- TODO (?): generalize to semilinear maps with `f ∘ₗ g` bijective. theorem ker_eq_range_of_comp_eq_id {M P} [AddCommGroup M] [Module R M] [AddCommGroup P] [Module R P] {f : M →ₗ[R] P} {g : P →ₗ[R] M} (h : f ∘ₗ g = .id) : ker f = range (LinearMap.id - g ∘ₗ f) := le_antisymm (fun x hx ↦ ⟨x, show x - g (f x) = x by rw [hx, map_zero, sub_zero]⟩) <| range_le_ker_iff.mpr <| by rw [comp_sub, comp_id, ← comp_assoc, h, id_comp, sub_self] end end AddCommMonoid section Ring variable [Ring R] [Ring R₂] variable [AddCommGroup M] [AddCommGroup M₂] variable [Module R M] [Module R₂ M₂] variable {τ₁₂ : R →+* R₂} variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] variable {f : F} open Submodule theorem range_toAddSubgroup [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : (range f).toAddSubgroup = f.toAddMonoidHom.range := rfl theorem ker_le_iff [RingHomSurjective τ₁₂] {p : Submodule R M} : ker f ≤ p ↔ ∃ y ∈ range f, f ⁻¹' {y} ⊆ p := by constructor · intro h use 0 rw [← SetLike.mem_coe, range_coe] exact ⟨⟨0, map_zero f⟩, h⟩ · rintro ⟨y, h₁, h₂⟩ rw [SetLike.le_def] intro z hz simp only [mem_ker, SetLike.mem_coe] at hz
rw [← SetLike.mem_coe, range_coe, Set.mem_range] at h₁ obtain ⟨x, hx⟩ := h₁ have hx' : x ∈ p := h₂ hx have hxz : z + x ∈ p := by apply h₂ simp [hx, hz] suffices z + x - x ∈ p by simpa only [this, add_sub_cancel_right] exact p.sub_mem hxz hx' end Ring section Semifield variable [Semifield K] variable [AddCommMonoid V] [Module K V] variable [AddCommMonoid V₂] [Module K V₂] theorem range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := by simpa only [range_eq_map] using Submodule.map_smul f _ a h
Mathlib/Algebra/Module/Submodule/Range.lean
235
253
/- 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 -/ import Mathlib.Topology.UniformSpace.Cauchy /-! # Uniform convergence A sequence of functions `Fₙ` (with values in a metric space) converges uniformly on a set `s` to a function `f` if, for all `ε > 0`, for all large enough `n`, one has for all `y ∈ s` the inequality `dist (f y, Fₙ y) < ε`. Under uniform convergence, many properties of the `Fₙ` pass to the limit, most notably continuity. We prove this in the file, defining the notion of uniform convergence in the more general setting of uniform spaces, and with respect to an arbitrary indexing set endowed with a filter (instead of just `ℕ` with `atTop`). ## Main results Let `α` be a topological space, `β` a uniform space, `Fₙ` and `f` be functions from `α` to `β` (where the index `n` belongs to an indexing type `ι` endowed with a filter `p`). * `TendstoUniformlyOn F f p s`: the fact that `Fₙ` converges uniformly to `f` on `s`. This means that, for any entourage `u` of the diagonal, for large enough `n` (with respect to `p`), one has `(f y, Fₙ y) ∈ u` for all `y ∈ s`. * `TendstoUniformly F f p`: same notion with `s = univ`. * `TendstoUniformlyOn.continuousOn`: a uniform limit on a set of functions which are continuous on this set is itself continuous on this set. * `TendstoUniformly.continuous`: a uniform limit of continuous functions is continuous. * `TendstoUniformlyOn.tendsto_comp`: If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then `Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s`. * `TendstoUniformly.tendsto_comp`: If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. Finally, we introduce the notion of a uniform Cauchy sequence, which is to uniform convergence what a Cauchy sequence is to the usual notion of convergence. ## Implementation notes We derive most of our initial results from an auxiliary definition `TendstoUniformlyOnFilter`. This definition in and of itself can sometimes be useful, e.g., when studying the local behavior of the `Fₙ` near a point, which would typically look like `TendstoUniformlyOnFilter F f p (𝓝 x)`. Still, while this may be the "correct" definition (see `tendstoUniformlyOn_iff_tendstoUniformlyOnFilter`), it is somewhat unwieldy to work with in practice. Thus, we provide the more traditional definition in `TendstoUniformlyOn`. ## Tags Uniform limit, uniform convergence, tends uniformly to -/ noncomputable section open Topology Uniformity Filter Set Uniform variable {α β γ ι : Type*} [UniformSpace β] variable {F : ι → α → β} {f : α → β} {s s' : Set α} {x : α} {p : Filter ι} {p' : Filter α} /-! ### Different notions of uniform convergence We define uniform convergence, on a set or in the whole space. -/ /-- A sequence of functions `Fₙ` converges uniformly on a filter `p'` to a limiting function `f` with respect to the filter `p` if, for any entourage of the diagonal `u`, one has `p ×ˢ p'`-eventually `(f x, Fₙ x) ∈ u`. -/ def TendstoUniformlyOnFilter (F : ι → α → β) (f : α → β) (p : Filter ι) (p' : Filter α) := ∀ u ∈ 𝓤 β, ∀ᶠ n : ι × α in p ×ˢ p', (f n.snd, F n.fst n.snd) ∈ u /-- A sequence of functions `Fₙ` converges uniformly on a filter `p'` to a limiting function `f` w.r.t. filter `p` iff the function `(n, x) ↦ (f x, Fₙ x)` converges along `p ×ˢ p'` to the uniformity. In other words: one knows nothing about the behavior of `x` in this limit besides it being in `p'`. -/ theorem tendstoUniformlyOnFilter_iff_tendsto : TendstoUniformlyOnFilter F f p p' ↔ Tendsto (fun q : ι × α => (f q.2, F q.1 q.2)) (p ×ˢ p') (𝓤 β) := Iff.rfl /-- A sequence of functions `Fₙ` converges uniformly on a set `s` to a limiting function `f` with respect to the filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually `(f x, Fₙ x) ∈ u` for all `x ∈ s`. -/ def TendstoUniformlyOn (F : ι → α → β) (f : α → β) (p : Filter ι) (s : Set α) := ∀ u ∈ 𝓤 β, ∀ᶠ n in p, ∀ x : α, x ∈ s → (f x, F n x) ∈ u theorem tendstoUniformlyOn_iff_tendstoUniformlyOnFilter : TendstoUniformlyOn F f p s ↔ TendstoUniformlyOnFilter F f p (𝓟 s) := by simp only [TendstoUniformlyOn, TendstoUniformlyOnFilter] apply forall₂_congr simp_rw [eventually_prod_principal_iff] simp alias ⟨TendstoUniformlyOn.tendstoUniformlyOnFilter, TendstoUniformlyOnFilter.tendstoUniformlyOn⟩ := tendstoUniformlyOn_iff_tendstoUniformlyOnFilter /-- A sequence of functions `Fₙ` converges uniformly on a set `s` to a limiting function `f` w.r.t. filter `p` iff the function `(n, x) ↦ (f x, Fₙ x)` converges along `p ×ˢ 𝓟 s` to the uniformity. In other words: one knows nothing about the behavior of `x` in this limit besides it being in `s`. -/ theorem tendstoUniformlyOn_iff_tendsto : TendstoUniformlyOn F f p s ↔ Tendsto (fun q : ι × α => (f q.2, F q.1 q.2)) (p ×ˢ 𝓟 s) (𝓤 β) := by simp [tendstoUniformlyOn_iff_tendstoUniformlyOnFilter, tendstoUniformlyOnFilter_iff_tendsto] /-- A sequence of functions `Fₙ` converges uniformly to a limiting function `f` with respect to a filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually `(f x, Fₙ x) ∈ u` for all `x`. -/ def TendstoUniformly (F : ι → α → β) (f : α → β) (p : Filter ι) := ∀ u ∈ 𝓤 β, ∀ᶠ n in p, ∀ x : α, (f x, F n x) ∈ u theorem tendstoUniformlyOn_univ : TendstoUniformlyOn F f p univ ↔ TendstoUniformly F f p := by simp [TendstoUniformlyOn, TendstoUniformly] theorem tendstoUniformly_iff_tendstoUniformlyOnFilter : TendstoUniformly F f p ↔ TendstoUniformlyOnFilter F f p ⊤ := by rw [← tendstoUniformlyOn_univ, tendstoUniformlyOn_iff_tendstoUniformlyOnFilter, principal_univ] theorem TendstoUniformly.tendstoUniformlyOnFilter (h : TendstoUniformly F f p) : TendstoUniformlyOnFilter F f p ⊤ := by rwa [← tendstoUniformly_iff_tendstoUniformlyOnFilter] theorem tendstoUniformlyOn_iff_tendstoUniformly_comp_coe : TendstoUniformlyOn F f p s ↔ TendstoUniformly (fun i (x : s) => F i x) (f ∘ (↑)) p := forall₂_congr fun u _ => by simp /-- A sequence of functions `Fₙ` converges uniformly to a limiting function `f` w.r.t. filter `p` iff the function `(n, x) ↦ (f x, Fₙ x)` converges along `p ×ˢ ⊤` to the uniformity. In other words: one knows nothing about the behavior of `x` in this limit. -/ theorem tendstoUniformly_iff_tendsto : TendstoUniformly F f p ↔ Tendsto (fun q : ι × α => (f q.2, F q.1 q.2)) (p ×ˢ ⊤) (𝓤 β) := by simp [tendstoUniformly_iff_tendstoUniformlyOnFilter, tendstoUniformlyOnFilter_iff_tendsto] /-- Uniform convergence implies pointwise convergence. -/ theorem TendstoUniformlyOnFilter.tendsto_at (h : TendstoUniformlyOnFilter F f p p') (hx : 𝓟 {x} ≤ p') : Tendsto (fun n => F n x) p <| 𝓝 (f x) := by refine Uniform.tendsto_nhds_right.mpr fun u hu => mem_map.mpr ?_ filter_upwards [(h u hu).curry] intro i h simpa using h.filter_mono hx /-- Uniform convergence implies pointwise convergence. -/ theorem TendstoUniformlyOn.tendsto_at (h : TendstoUniformlyOn F f p s) (hx : x ∈ s) : Tendsto (fun n => F n x) p <| 𝓝 (f x) := h.tendstoUniformlyOnFilter.tendsto_at (le_principal_iff.mpr <| mem_principal.mpr <| singleton_subset_iff.mpr <| hx) /-- Uniform convergence implies pointwise convergence. -/ theorem TendstoUniformly.tendsto_at (h : TendstoUniformly F f p) (x : α) : Tendsto (fun n => F n x) p <| 𝓝 (f x) := h.tendstoUniformlyOnFilter.tendsto_at le_top theorem TendstoUniformlyOnFilter.mono_left {p'' : Filter ι} (h : TendstoUniformlyOnFilter F f p p') (hp : p'' ≤ p) : TendstoUniformlyOnFilter F f p'' p' := fun u hu => (h u hu).filter_mono (p'.prod_mono_left hp) theorem TendstoUniformlyOnFilter.mono_right {p'' : Filter α} (h : TendstoUniformlyOnFilter F f p p') (hp : p'' ≤ p') : TendstoUniformlyOnFilter F f p p'' := fun u hu => (h u hu).filter_mono (p.prod_mono_right hp) theorem TendstoUniformlyOn.mono (h : TendstoUniformlyOn F f p s) (h' : s' ⊆ s) : TendstoUniformlyOn F f p s' := tendstoUniformlyOn_iff_tendstoUniformlyOnFilter.mpr (h.tendstoUniformlyOnFilter.mono_right (le_principal_iff.mpr <| mem_principal.mpr h')) theorem TendstoUniformlyOnFilter.congr {F' : ι → α → β} (hf : TendstoUniformlyOnFilter F f p p') (hff' : ∀ᶠ n : ι × α in p ×ˢ p', F n.fst n.snd = F' n.fst n.snd) : TendstoUniformlyOnFilter F' f p p' := by refine fun u hu => ((hf u hu).and hff').mono fun n h => ?_ rw [← h.right] exact h.left theorem TendstoUniformlyOn.congr {F' : ι → α → β} (hf : TendstoUniformlyOn F f p s) (hff' : ∀ᶠ n in p, Set.EqOn (F n) (F' n) s) : TendstoUniformlyOn F' f p s := by rw [tendstoUniformlyOn_iff_tendstoUniformlyOnFilter] at hf ⊢ refine hf.congr ?_ rw [eventually_iff] at hff' ⊢ simp only [Set.EqOn] at hff' simp only [mem_prod_principal, hff', mem_setOf_eq] lemma tendstoUniformly_congr {F' : ι → α → β} (hF : F =ᶠ[p] F') : TendstoUniformly F f p ↔ TendstoUniformly F' f p := by simp_rw [← tendstoUniformlyOn_univ] at * have HF := EventuallyEq.exists_mem hF exact ⟨fun h => h.congr (by aesop), fun h => h.congr (by simp_rw [eqOn_comm]; aesop)⟩ theorem TendstoUniformlyOn.congr_right {g : α → β} (hf : TendstoUniformlyOn F f p s) (hfg : EqOn f g s) : TendstoUniformlyOn F g p s := fun u hu => by filter_upwards [hf u hu] with i hi a ha using hfg ha ▸ hi a ha protected theorem TendstoUniformly.tendstoUniformlyOn (h : TendstoUniformly F f p) : TendstoUniformlyOn F f p s := (tendstoUniformlyOn_univ.2 h).mono (subset_univ s) /-- Composing on the right by a function preserves uniform convergence on a filter -/ theorem TendstoUniformlyOnFilter.comp (h : TendstoUniformlyOnFilter F f p p') (g : γ → α) : TendstoUniformlyOnFilter (fun n => F n ∘ g) (f ∘ g) p (p'.comap g) := by rw [tendstoUniformlyOnFilter_iff_tendsto] at h ⊢ exact h.comp (tendsto_id.prodMap tendsto_comap) /-- Composing on the right by a function preserves uniform convergence on a set -/ theorem TendstoUniformlyOn.comp (h : TendstoUniformlyOn F f p s) (g : γ → α) : TendstoUniformlyOn (fun n => F n ∘ g) (f ∘ g) p (g ⁻¹' s) := by rw [tendstoUniformlyOn_iff_tendstoUniformlyOnFilter] at h ⊢ simpa [TendstoUniformlyOn, comap_principal] using TendstoUniformlyOnFilter.comp h g /-- Composing on the right by a function preserves uniform convergence -/ theorem TendstoUniformly.comp (h : TendstoUniformly F f p) (g : γ → α) : TendstoUniformly (fun n => F n ∘ g) (f ∘ g) p := by rw [tendstoUniformly_iff_tendstoUniformlyOnFilter] at h ⊢ simpa [principal_univ, comap_principal] using h.comp g /-- Composing on the left by a uniformly continuous function preserves uniform convergence on a filter -/ theorem UniformContinuous.comp_tendstoUniformlyOnFilter [UniformSpace γ] {g : β → γ} (hg : UniformContinuous g) (h : TendstoUniformlyOnFilter F f p p') : TendstoUniformlyOnFilter (fun i => g ∘ F i) (g ∘ f) p p' := fun _u hu => h _ (hg hu) /-- Composing on the left by a uniformly continuous function preserves uniform convergence on a set -/ theorem UniformContinuous.comp_tendstoUniformlyOn [UniformSpace γ] {g : β → γ} (hg : UniformContinuous g) (h : TendstoUniformlyOn F f p s) : TendstoUniformlyOn (fun i => g ∘ F i) (g ∘ f) p s := fun _u hu => h _ (hg hu) /-- Composing on the left by a uniformly continuous function preserves uniform convergence -/ theorem UniformContinuous.comp_tendstoUniformly [UniformSpace γ] {g : β → γ} (hg : UniformContinuous g) (h : TendstoUniformly F f p) : TendstoUniformly (fun i => g ∘ F i) (g ∘ f) p := fun _u hu => h _ (hg hu) theorem TendstoUniformlyOnFilter.prodMap {ι' α' β' : Type*} [UniformSpace β'] {F' : ι' → α' → β'} {f' : α' → β'} {q : Filter ι'} {q' : Filter α'} (h : TendstoUniformlyOnFilter F f p p') (h' : TendstoUniformlyOnFilter F' f' q q') : TendstoUniformlyOnFilter (fun i : ι × ι' => Prod.map (F i.1) (F' i.2)) (Prod.map f f') (p ×ˢ q) (p' ×ˢ q') := by rw [tendstoUniformlyOnFilter_iff_tendsto] at h h' ⊢ rw [uniformity_prod_eq_comap_prod, tendsto_comap_iff, ← map_swap4_prod, tendsto_map'_iff] simpa using h.prodMap h' @[deprecated (since := "2025-03-10")] alias TendstoUniformlyOnFilter.prod_map := TendstoUniformlyOnFilter.prodMap theorem TendstoUniformlyOn.prodMap {ι' α' β' : Type*} [UniformSpace β'] {F' : ι' → α' → β'} {f' : α' → β'} {p' : Filter ι'} {s' : Set α'} (h : TendstoUniformlyOn F f p s) (h' : TendstoUniformlyOn F' f' p' s') : TendstoUniformlyOn (fun i : ι × ι' => Prod.map (F i.1) (F' i.2)) (Prod.map f f') (p ×ˢ p') (s ×ˢ s') := by rw [tendstoUniformlyOn_iff_tendstoUniformlyOnFilter] at h h' ⊢ simpa only [prod_principal_principal] using h.prodMap h' @[deprecated (since := "2025-03-10")] alias TendstoUniformlyOn.prod_map := TendstoUniformlyOn.prodMap theorem TendstoUniformly.prodMap {ι' α' β' : Type*} [UniformSpace β'] {F' : ι' → α' → β'} {f' : α' → β'} {p' : Filter ι'} (h : TendstoUniformly F f p) (h' : TendstoUniformly F' f' p') : TendstoUniformly (fun i : ι × ι' => Prod.map (F i.1) (F' i.2)) (Prod.map f f') (p ×ˢ p') := by rw [← tendstoUniformlyOn_univ, ← univ_prod_univ] at * exact h.prodMap h' @[deprecated (since := "2025-03-10")] alias TendstoUniformly.prod_map := TendstoUniformly.prodMap theorem TendstoUniformlyOnFilter.prodMk {ι' β' : Type*} [UniformSpace β'] {F' : ι' → α → β'} {f' : α → β'} {q : Filter ι'} (h : TendstoUniformlyOnFilter F f p p') (h' : TendstoUniformlyOnFilter F' f' q p') : TendstoUniformlyOnFilter (fun (i : ι × ι') a => (F i.1 a, F' i.2 a)) (fun a => (f a, f' a)) (p ×ˢ q) p' := fun u hu => ((h.prodMap h') u hu).diag_of_prod_right @[deprecated (since := "2025-03-10")] alias TendstoUniformlyOnFilter.prod := TendstoUniformlyOnFilter.prodMk protected theorem TendstoUniformlyOn.prodMk {ι' β' : Type*} [UniformSpace β'] {F' : ι' → α → β'} {f' : α → β'} {p' : Filter ι'} (h : TendstoUniformlyOn F f p s) (h' : TendstoUniformlyOn F' f' p' s) : TendstoUniformlyOn (fun (i : ι × ι') a => (F i.1 a, F' i.2 a)) (fun a => (f a, f' a)) (p ×ˢ p') s := (congr_arg _ s.inter_self).mp ((h.prodMap h').comp fun a => (a, a)) @[deprecated (since := "2025-03-10")] alias TendstoUniformlyOn.prod := TendstoUniformlyOn.prodMk theorem TendstoUniformly.prodMk {ι' β' : Type*} [UniformSpace β'] {F' : ι' → α → β'} {f' : α → β'} {p' : Filter ι'} (h : TendstoUniformly F f p) (h' : TendstoUniformly F' f' p') : TendstoUniformly (fun (i : ι × ι') a => (F i.1 a, F' i.2 a)) (fun a => (f a, f' a)) (p ×ˢ p') := (h.prodMap h').comp fun a => (a, a) @[deprecated (since := "2025-03-10")] alias TendstoUniformly.prod := TendstoUniformly.prodMk /-- Uniform convergence on a filter `p'` to a constant function is equivalent to convergence in `p ×ˢ p'`. -/ theorem tendsto_prod_filter_iff {c : β} : Tendsto (↿F) (p ×ˢ p') (𝓝 c) ↔ TendstoUniformlyOnFilter F (fun _ => c) p p' := by simp_rw [nhds_eq_comap_uniformity, tendsto_comap_iff] rfl /-- Uniform convergence on a set `s` to a constant function is equivalent to convergence in `p ×ˢ 𝓟 s`. -/ theorem tendsto_prod_principal_iff {c : β} : Tendsto (↿F) (p ×ˢ 𝓟 s) (𝓝 c) ↔ TendstoUniformlyOn F (fun _ => c) p s := by rw [tendstoUniformlyOn_iff_tendstoUniformlyOnFilter] exact tendsto_prod_filter_iff /-- Uniform convergence to a constant function is equivalent to convergence in `p ×ˢ ⊤`. -/ theorem tendsto_prod_top_iff {c : β} : Tendsto (↿F) (p ×ˢ ⊤) (𝓝 c) ↔ TendstoUniformly F (fun _ => c) p := by rw [tendstoUniformly_iff_tendstoUniformlyOnFilter] exact tendsto_prod_filter_iff /-- Uniform convergence on the empty set is vacuously true -/ theorem tendstoUniformlyOn_empty : TendstoUniformlyOn F f p ∅ := fun u _ => by simp /-- Uniform convergence on a singleton is equivalent to regular convergence -/ theorem tendstoUniformlyOn_singleton_iff_tendsto : TendstoUniformlyOn F f p {x} ↔ Tendsto (fun n : ι => F n x) p (𝓝 (f x)) := by simp_rw [tendstoUniformlyOn_iff_tendsto, Uniform.tendsto_nhds_right, tendsto_def] exact forall₂_congr fun u _ => by simp [mem_prod_principal, preimage] /-- If a sequence `g` converges to some `b`, then the sequence of constant functions `fun n ↦ fun a ↦ g n` converges to the constant function `fun a ↦ b` on any set `s` -/ theorem Filter.Tendsto.tendstoUniformlyOnFilter_const {g : ι → β} {b : β} (hg : Tendsto g p (𝓝 b)) (p' : Filter α) : TendstoUniformlyOnFilter (fun n : ι => fun _ : α => g n) (fun _ : α => b) p p' := by simpa only [nhds_eq_comap_uniformity, tendsto_comap_iff] using hg.comp (tendsto_fst (g := p')) /-- If a sequence `g` converges to some `b`, then the sequence of constant functions `fun n ↦ fun a ↦ g n` converges to the constant function `fun a ↦ b` on any set `s` -/ theorem Filter.Tendsto.tendstoUniformlyOn_const {g : ι → β} {b : β} (hg : Tendsto g p (𝓝 b)) (s : Set α) : TendstoUniformlyOn (fun n : ι => fun _ : α => g n) (fun _ : α => b) p s := tendstoUniformlyOn_iff_tendstoUniformlyOnFilter.mpr (hg.tendstoUniformlyOnFilter_const (𝓟 s)) theorem UniformContinuousOn.tendstoUniformlyOn [UniformSpace α] [UniformSpace γ] {U : Set α} {V : Set β} {F : α → β → γ} (hF : UniformContinuousOn (↿F) (U ×ˢ V)) (hU : x ∈ U) : TendstoUniformlyOn F (F x) (𝓝[U] x) V := by set φ := fun q : α × β => ((x, q.2), q) rw [tendstoUniformlyOn_iff_tendsto] change Tendsto (Prod.map (↿F) ↿F ∘ φ) (𝓝[U] x ×ˢ 𝓟 V) (𝓤 γ) simp only [nhdsWithin, Filter.prod_eq_inf, comap_inf, inf_assoc, comap_principal, inf_principal] refine hF.comp (Tendsto.inf ?_ <| tendsto_principal_principal.2 fun x hx => ⟨⟨hU, hx.2⟩, hx⟩) simp only [uniformity_prod_eq_comap_prod, tendsto_comap_iff, (· ∘ ·), nhds_eq_comap_uniformity, comap_comap] exact tendsto_comap.prodMk (tendsto_diag_uniformity _ _) theorem UniformContinuousOn.tendstoUniformly [UniformSpace α] [UniformSpace γ] {U : Set α} (hU : U ∈ 𝓝 x) {F : α → β → γ} (hF : UniformContinuousOn (↿F) (U ×ˢ (univ : Set β))) : TendstoUniformly F (F x) (𝓝 x) := by simpa only [tendstoUniformlyOn_univ, nhdsWithin_eq_nhds.2 hU] using hF.tendstoUniformlyOn (mem_of_mem_nhds hU) theorem UniformContinuous₂.tendstoUniformly [UniformSpace α] [UniformSpace γ] {f : α → β → γ} (h : UniformContinuous₂ f) : TendstoUniformly f (f x) (𝓝 x) := UniformContinuousOn.tendstoUniformly univ_mem <| by rwa [univ_prod_univ, uniformContinuousOn_univ] /-- A sequence is uniformly Cauchy if eventually all of its pairwise differences are uniformly bounded -/ def UniformCauchySeqOnFilter (F : ι → α → β) (p : Filter ι) (p' : Filter α) : Prop := ∀ u ∈ 𝓤 β, ∀ᶠ m : (ι × ι) × α in (p ×ˢ p) ×ˢ p', (F m.fst.fst m.snd, F m.fst.snd m.snd) ∈ u /-- A sequence is uniformly Cauchy if eventually all of its pairwise differences are uniformly bounded -/ def UniformCauchySeqOn (F : ι → α → β) (p : Filter ι) (s : Set α) : Prop := ∀ u ∈ 𝓤 β, ∀ᶠ m : ι × ι in p ×ˢ p, ∀ x : α, x ∈ s → (F m.fst x, F m.snd x) ∈ u theorem uniformCauchySeqOn_iff_uniformCauchySeqOnFilter : UniformCauchySeqOn F p s ↔ UniformCauchySeqOnFilter F p (𝓟 s) := by simp only [UniformCauchySeqOn, UniformCauchySeqOnFilter] refine forall₂_congr fun u hu => ?_ rw [eventually_prod_principal_iff] theorem UniformCauchySeqOn.uniformCauchySeqOnFilter (hF : UniformCauchySeqOn F p s) : UniformCauchySeqOnFilter F p (𝓟 s) := by rwa [← uniformCauchySeqOn_iff_uniformCauchySeqOnFilter] /-- A sequence that converges uniformly is also uniformly Cauchy -/ theorem TendstoUniformlyOnFilter.uniformCauchySeqOnFilter (hF : TendstoUniformlyOnFilter F f p p') : UniformCauchySeqOnFilter F p p' := by intro u hu rcases comp_symm_of_uniformity hu with ⟨t, ht, htsymm, htmem⟩ have := tendsto_swap4_prod.eventually ((hF t ht).prod_mk (hF t ht)) apply this.diag_of_prod_right.mono simp only [and_imp, Prod.forall] intro n1 n2 x hl hr exact Set.mem_of_mem_of_subset (prodMk_mem_compRel (htsymm hl) hr) htmem /-- A sequence that converges uniformly is also uniformly Cauchy -/ theorem TendstoUniformlyOn.uniformCauchySeqOn (hF : TendstoUniformlyOn F f p s) : UniformCauchySeqOn F p s := uniformCauchySeqOn_iff_uniformCauchySeqOnFilter.mpr hF.tendstoUniformlyOnFilter.uniformCauchySeqOnFilter /-- A uniformly Cauchy sequence converges uniformly to its limit -/ theorem UniformCauchySeqOnFilter.tendstoUniformlyOnFilter_of_tendsto (hF : UniformCauchySeqOnFilter F p p') (hF' : ∀ᶠ x : α in p', Tendsto (fun n => F n x) p (𝓝 (f x))) : TendstoUniformlyOnFilter F f p p' := by rcases p.eq_or_neBot with rfl | _ · simp only [TendstoUniformlyOnFilter, bot_prod, eventually_bot, implies_true] -- Proof idea: |f_n(x) - f(x)| ≤ |f_n(x) - f_m(x)| + |f_m(x) - f(x)|. We choose `n` -- so that |f_n(x) - f_m(x)| is uniformly small across `s` whenever `m ≥ n`. Then for -- a fixed `x`, we choose `m` sufficiently large such that |f_m(x) - f(x)| is small. intro u hu rcases comp_symm_of_uniformity hu with ⟨t, ht, htsymm, htmem⟩ -- We will choose n, x, and m simultaneously. n and x come from hF. m comes from hF' -- But we need to promote hF' to the full product filter to use it have hmc : ∀ᶠ x in (p ×ˢ p) ×ˢ p', Tendsto (fun n : ι => F n x.snd) p (𝓝 (f x.snd)) := by rw [eventually_prod_iff] exact ⟨fun _ => True, by simp, _, hF', by simp⟩ -- To apply filter operations we'll need to do some order manipulation rw [Filter.eventually_swap_iff] have := tendsto_prodAssoc.eventually (tendsto_prod_swap.eventually ((hF t ht).and hmc)) apply this.curry.mono simp only [Equiv.prodAssoc_apply, eventually_and, eventually_const, Prod.snd_swap, Prod.fst_swap, and_imp, Prod.forall] -- Complete the proof intro x n hx hm' refine Set.mem_of_mem_of_subset (mem_compRel.mpr ?_) htmem rw [Uniform.tendsto_nhds_right] at hm' have := hx.and (hm' ht) obtain ⟨m, hm⟩ := this.exists exact ⟨F m x, ⟨hm.2, htsymm hm.1⟩⟩ /-- A uniformly Cauchy sequence converges uniformly to its limit -/ theorem UniformCauchySeqOn.tendstoUniformlyOn_of_tendsto (hF : UniformCauchySeqOn F p s) (hF' : ∀ x : α, x ∈ s → Tendsto (fun n => F n x) p (𝓝 (f x))) : TendstoUniformlyOn F f p s := tendstoUniformlyOn_iff_tendstoUniformlyOnFilter.mpr (hF.uniformCauchySeqOnFilter.tendstoUniformlyOnFilter_of_tendsto hF') theorem UniformCauchySeqOnFilter.mono_left {p'' : Filter ι} (hf : UniformCauchySeqOnFilter F p p') (hp : p'' ≤ p) : UniformCauchySeqOnFilter F p'' p' := by intro u hu have := (hf u hu).filter_mono (p'.prod_mono_left (Filter.prod_mono hp hp)) exact this.mono (by simp) theorem UniformCauchySeqOnFilter.mono_right {p'' : Filter α} (hf : UniformCauchySeqOnFilter F p p') (hp : p'' ≤ p') : UniformCauchySeqOnFilter F p p'' := fun u hu => have := (hf u hu).filter_mono ((p ×ˢ p).prod_mono_right hp) this.mono (by simp) theorem UniformCauchySeqOn.mono (hf : UniformCauchySeqOn F p s) (hss' : s' ⊆ s) : UniformCauchySeqOn F p s' := by rw [uniformCauchySeqOn_iff_uniformCauchySeqOnFilter] at hf ⊢ exact hf.mono_right (le_principal_iff.mpr <| mem_principal.mpr hss') /-- Composing on the right by a function preserves uniform Cauchy sequences -/ theorem UniformCauchySeqOnFilter.comp {γ : Type*} (hf : UniformCauchySeqOnFilter F p p') (g : γ → α) : UniformCauchySeqOnFilter (fun n => F n ∘ g) p (p'.comap g) := fun u hu => by obtain ⟨pa, hpa, pb, hpb, hpapb⟩ := eventually_prod_iff.mp (hf u hu) rw [eventually_prod_iff] refine ⟨pa, hpa, pb ∘ g, ?_, fun hx _ hy => hpapb hx hy⟩ exact eventually_comap.mpr (hpb.mono fun x hx y hy => by simp only [hx, hy, Function.comp_apply]) /-- Composing on the right by a function preserves uniform Cauchy sequences -/ theorem UniformCauchySeqOn.comp {γ : Type*} (hf : UniformCauchySeqOn F p s) (g : γ → α) : UniformCauchySeqOn (fun n => F n ∘ g) p (g ⁻¹' s) := by rw [uniformCauchySeqOn_iff_uniformCauchySeqOnFilter] at hf ⊢ simpa only [UniformCauchySeqOn, comap_principal] using hf.comp g /-- Composing on the left by a uniformly continuous function preserves uniform Cauchy sequences -/ theorem UniformContinuous.comp_uniformCauchySeqOn [UniformSpace γ] {g : β → γ} (hg : UniformContinuous g) (hf : UniformCauchySeqOn F p s) : UniformCauchySeqOn (fun n => g ∘ F n) p s := fun _u hu => hf _ (hg hu) theorem UniformCauchySeqOn.prodMap {ι' α' β' : Type*} [UniformSpace β'] {F' : ι' → α' → β'} {p' : Filter ι'} {s' : Set α'} (h : UniformCauchySeqOn F p s) (h' : UniformCauchySeqOn F' p' s') : UniformCauchySeqOn (fun i : ι × ι' => Prod.map (F i.1) (F' i.2)) (p ×ˢ p') (s ×ˢ s') := by intro u hu rw [uniformity_prod_eq_prod, mem_map, mem_prod_iff] at hu obtain ⟨v, hv, w, hw, hvw⟩ := hu simp_rw [mem_prod, and_imp, Prod.forall, Prod.map_apply] rw [← Set.image_subset_iff] at hvw apply (tendsto_swap4_prod.eventually ((h v hv).prod_mk (h' w hw))).mono intro x hx a b ha hb exact hvw ⟨_, mk_mem_prod (hx.1 a ha) (hx.2 b hb), rfl⟩ @[deprecated (since := "2025-03-10")] alias UniformCauchySeqOn.prod_map := UniformCauchySeqOn.prodMap theorem UniformCauchySeqOn.prod {ι' β' : Type*} [UniformSpace β'] {F' : ι' → α → β'} {p' : Filter ι'} (h : UniformCauchySeqOn F p s) (h' : UniformCauchySeqOn F' p' s) : UniformCauchySeqOn (fun (i : ι × ι') a => (F i.fst a, F' i.snd a)) (p ×ˢ p') s := (congr_arg _ s.inter_self).mp ((h.prodMap h').comp fun a => (a, a)) theorem UniformCauchySeqOn.prod' {β' : Type*} [UniformSpace β'] {F' : ι → α → β'} (h : UniformCauchySeqOn F p s) (h' : UniformCauchySeqOn F' p s) : UniformCauchySeqOn (fun (i : ι) a => (F i a, F' i a)) p s := fun u hu => have hh : Tendsto (fun x : ι => (x, x)) p (p ×ˢ p) := tendsto_diag (hh.prodMap hh).eventually ((h.prod h') u hu) /-- If a sequence of functions is uniformly Cauchy on a set, then the values at each point form a Cauchy sequence. -/ theorem UniformCauchySeqOn.cauchy_map [hp : NeBot p] (hf : UniformCauchySeqOn F p s) (hx : x ∈ s) : Cauchy (map (fun i => F i x) p) := by simp only [cauchy_map_iff, hp, true_and] intro u hu rw [mem_map] filter_upwards [hf u hu] with p hp using hp x hx /-- If a sequence of functions is uniformly Cauchy on a set, then the values at each point form a Cauchy sequence. See `UniformCauchSeqOn.cauchy_map` for the non-`atTop` case. -/ theorem UniformCauchySeqOn.cauchySeq [Nonempty ι] [SemilatticeSup ι] (hf : UniformCauchySeqOn F atTop s) (hx : x ∈ s) : CauchySeq fun i ↦ F i x := hf.cauchy_map (hp := atTop_neBot) hx section SeqTendsto theorem tendstoUniformlyOn_of_seq_tendstoUniformlyOn {l : Filter ι} [l.IsCountablyGenerated] (h : ∀ u : ℕ → ι, Tendsto u atTop l → TendstoUniformlyOn (fun n => F (u n)) f atTop s) : TendstoUniformlyOn F f l s := by rw [tendstoUniformlyOn_iff_tendsto, tendsto_iff_seq_tendsto] intro u hu rw [tendsto_prod_iff'] at hu specialize h (fun n => (u n).fst) hu.1 rw [tendstoUniformlyOn_iff_tendsto] at h exact h.comp (tendsto_id.prodMk hu.2) theorem TendstoUniformlyOn.seq_tendstoUniformlyOn {l : Filter ι} (h : TendstoUniformlyOn F f l s) (u : ℕ → ι) (hu : Tendsto u atTop l) : TendstoUniformlyOn (fun n => F (u n)) f atTop s := by rw [tendstoUniformlyOn_iff_tendsto] at h ⊢ exact h.comp ((hu.comp tendsto_fst).prodMk tendsto_snd) theorem tendstoUniformlyOn_iff_seq_tendstoUniformlyOn {l : Filter ι} [l.IsCountablyGenerated] : TendstoUniformlyOn F f l s ↔ ∀ u : ℕ → ι, Tendsto u atTop l → TendstoUniformlyOn (fun n => F (u n)) f atTop s := ⟨TendstoUniformlyOn.seq_tendstoUniformlyOn, tendstoUniformlyOn_of_seq_tendstoUniformlyOn⟩ theorem tendstoUniformly_iff_seq_tendstoUniformly {l : Filter ι} [l.IsCountablyGenerated] : TendstoUniformly F f l ↔ ∀ u : ℕ → ι, Tendsto u atTop l → TendstoUniformly (fun n => F (u n)) f atTop := by simp_rw [← tendstoUniformlyOn_univ] exact tendstoUniformlyOn_iff_seq_tendstoUniformlyOn end SeqTendsto section variable [NeBot p] {L : ι → β} {ℓ : β} theorem TendstoUniformlyOnFilter.tendsto_of_eventually_tendsto (h1 : TendstoUniformlyOnFilter F f p p') (h2 : ∀ᶠ i in p, Tendsto (F i) p' (𝓝 (L i))) (h3 : Tendsto L p (𝓝 ℓ)) : Tendsto f p' (𝓝 ℓ) := by rw [tendsto_nhds_left] intro s hs rw [mem_map, Set.preimage, ← eventually_iff] obtain ⟨t, ht, hts⟩ := comp3_mem_uniformity hs have p1 : ∀ᶠ i in p, (L i, ℓ) ∈ t := tendsto_nhds_left.mp h3 ht have p2 : ∀ᶠ i in p, ∀ᶠ x in p', (F i x, L i) ∈ t := by filter_upwards [h2] with i h2 using tendsto_nhds_left.mp h2 ht have p3 : ∀ᶠ i in p, ∀ᶠ x in p', (f x, F i x) ∈ t := (h1 t ht).curry obtain ⟨i, p4, p5, p6⟩ := (p1.and (p2.and p3)).exists filter_upwards [p5, p6] with x p5 p6 using hts ⟨F i x, p6, L i, p5, p4⟩ theorem TendstoUniformly.tendsto_of_eventually_tendsto (h1 : TendstoUniformly F f p) (h2 : ∀ᶠ i in p, Tendsto (F i) p' (𝓝 (L i))) (h3 : Tendsto L p (𝓝 ℓ)) : Tendsto f p' (𝓝 ℓ) := (h1.tendstoUniformlyOnFilter.mono_right le_top).tendsto_of_eventually_tendsto h2 h3 end
Mathlib/Topology/UniformSpace/UniformConvergence.lean
759
762
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import Batteries.Data.List.Perm import Mathlib.Data.List.OfFn import Mathlib.Data.List.Nodup import Mathlib.Data.List.TakeWhile import Mathlib.Order.Fin.Basic /-! # Sorting algorithms on lists In this file we define `List.Sorted r l` to be an alias for `List.Pairwise r l`. This alias is preferred in the case that `r` is a `<` or `≤`-like relation. Then we define the sorting algorithm `List.insertionSort` and prove its correctness. -/ open List.Perm universe u v namespace List /-! ### The predicate `List.Sorted` -/ section Sorted variable {α : Type u} {r : α → α → Prop} {a : α} {l : List α} /-- `Sorted r l` is the same as `List.Pairwise r l`, preferred in the case that `r` is a `<` or `≤`-like relation (transitive and antisymmetric or asymmetric) -/ def Sorted := @Pairwise instance decidableSorted [DecidableRel r] (l : List α) : Decidable (Sorted r l) := List.instDecidablePairwise _ protected theorem Sorted.le_of_lt [Preorder α] {l : List α} (h : l.Sorted (· < ·)) : l.Sorted (· ≤ ·) := h.imp le_of_lt protected theorem Sorted.lt_of_le [PartialOrder α] {l : List α} (h₁ : l.Sorted (· ≤ ·)) (h₂ : l.Nodup) : l.Sorted (· < ·) := h₁.imp₂ (fun _ _ => lt_of_le_of_ne) h₂ protected theorem Sorted.ge_of_gt [Preorder α] {l : List α} (h : l.Sorted (· > ·)) : l.Sorted (· ≥ ·) := h.imp le_of_lt protected theorem Sorted.gt_of_ge [PartialOrder α] {l : List α} (h₁ : l.Sorted (· ≥ ·)) (h₂ : l.Nodup) : l.Sorted (· > ·) := h₁.imp₂ (fun _ _ => lt_of_le_of_ne) <| by simp_rw [ne_comm]; exact h₂ @[simp] theorem sorted_nil : Sorted r [] := Pairwise.nil theorem Sorted.of_cons : Sorted r (a :: l) → Sorted r l := Pairwise.of_cons theorem Sorted.tail {r : α → α → Prop} {l : List α} (h : Sorted r l) : Sorted r l.tail := Pairwise.tail h theorem rel_of_sorted_cons {a : α} {l : List α} : Sorted r (a :: l) → ∀ b ∈ l, r a b := rel_of_pairwise_cons nonrec theorem Sorted.cons {r : α → α → Prop} [IsTrans α r] {l : List α} {a b : α} (hab : r a b) (h : Sorted r (b :: l)) : Sorted r (a :: b :: l) := h.cons <| forall_mem_cons.2 ⟨hab, fun _ hx => _root_.trans hab <| rel_of_sorted_cons h _ hx⟩ theorem sorted_cons_cons {r : α → α → Prop} [IsTrans α r] {l : List α} {a b : α} : Sorted r (b :: a :: l) ↔ r b a ∧ Sorted r (a :: l) := by constructor · intro h exact ⟨rel_of_sorted_cons h _ mem_cons_self, h.of_cons⟩ · rintro ⟨h, ha⟩ exact ha.cons h theorem Sorted.head!_le [Inhabited α] [Preorder α] {a : α} {l : List α} (h : Sorted (· < ·) l) (ha : a ∈ l) : l.head! ≤ a := by rw [← List.cons_head!_tail (List.ne_nil_of_mem ha)] at h ha cases ha · exact le_rfl · exact le_of_lt (rel_of_sorted_cons h a (by assumption)) theorem Sorted.le_head! [Inhabited α] [Preorder α] {a : α} {l : List α} (h : Sorted (· > ·) l) (ha : a ∈ l) : a ≤ l.head! := by rw [← List.cons_head!_tail (List.ne_nil_of_mem ha)] at h ha cases ha · exact le_rfl · exact le_of_lt (rel_of_sorted_cons h a (by assumption)) @[simp] theorem sorted_cons {a : α} {l : List α} : Sorted r (a :: l) ↔ (∀ b ∈ l, r a b) ∧ Sorted r l := pairwise_cons protected theorem Sorted.nodup {r : α → α → Prop} [IsIrrefl α r] {l : List α} (h : Sorted r l) : Nodup l := Pairwise.nodup h protected theorem Sorted.filter {l : List α} (f : α → Bool) (h : Sorted r l) : Sorted r (filter f l) := h.sublist filter_sublist theorem eq_of_perm_of_sorted [IsAntisymm α r] {l₁ l₂ : List α} (hp : l₁ ~ l₂) (hs₁ : Sorted r l₁) (hs₂ : Sorted r l₂) : l₁ = l₂ := by induction hs₁ generalizing l₂ with | nil => exact hp.nil_eq | @cons a l₁ h₁ hs₁ IH => have : a ∈ l₂ := hp.subset mem_cons_self rcases append_of_mem this with ⟨u₂, v₂, rfl⟩ have hp' := (perm_cons a).1 (hp.trans perm_middle) obtain rfl := IH hp' (hs₂.sublist <| by simp) change a :: u₂ ++ v₂ = u₂ ++ ([a] ++ v₂) rw [← append_assoc] congr have : ∀ x ∈ u₂, x = a := fun x m => antisymm ((pairwise_append.1 hs₂).2.2 _ m a mem_cons_self) (h₁ _ (by simp [m])) rw [(@eq_replicate_iff _ a (length u₂ + 1) (a :: u₂)).2, (@eq_replicate_iff _ a (length u₂ + 1) (u₂ ++ [a])).2] <;> constructor <;> simp [iff_true_intro this, or_comm] theorem Sorted.eq_of_mem_iff [IsAntisymm α r] [IsIrrefl α r] {l₁ l₂ : List α} (h₁ : Sorted r l₁) (h₂ : Sorted r l₂) (h : ∀ a : α, a ∈ l₁ ↔ a ∈ l₂) : l₁ = l₂ := eq_of_perm_of_sorted ((perm_ext_iff_of_nodup h₁.nodup h₂.nodup).2 h) h₁ h₂ theorem sublist_of_subperm_of_sorted [IsAntisymm α r] {l₁ l₂ : List α} (hp : l₁ <+~ l₂) (hs₁ : l₁.Sorted r) (hs₂ : l₂.Sorted r) : l₁ <+ l₂ := by let ⟨_, h, h'⟩ := hp rwa [← eq_of_perm_of_sorted h (hs₂.sublist h') hs₁] @[simp 1100] -- Higher priority shortcut lemma. theorem sorted_singleton (a : α) : Sorted r [a] := by simp theorem sorted_lt_range (n : ℕ) : Sorted (· < ·) (range n) := by rw [Sorted, pairwise_iff_get] simp theorem sorted_replicate (n : ℕ) (a : α) : Sorted r (replicate n a) ↔ n ≤ 1 ∨ r a a := pairwise_replicate theorem sorted_le_replicate (n : ℕ) (a : α) [Preorder α] : Sorted (· ≤ ·) (replicate n a) := by simp [sorted_replicate] theorem sorted_le_range (n : ℕ) : Sorted (· ≤ ·) (range n) := (sorted_lt_range n).le_of_lt lemma sorted_lt_range' (a b) {s} (hs : s ≠ 0) : Sorted (· < ·) (range' a b s) := by induction b generalizing a with | zero => simp | succ n ih => rw [List.range'_succ] refine List.sorted_cons.mpr ⟨fun b hb ↦ ?_, @ih (a + s)⟩ exact lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.zero_lt_of_ne_zero hs)) (List.left_le_of_mem_range' hb) lemma sorted_le_range' (a b s) : Sorted (· ≤ ·) (range' a b s) := by by_cases hs : s ≠ 0 · exact (sorted_lt_range' a b hs).le_of_lt · rw [ne_eq, Decidable.not_not] at hs simpa [hs] using sorted_le_replicate b a theorem Sorted.rel_get_of_lt {l : List α} (h : l.Sorted r) {a b : Fin l.length} (hab : a < b) : r (l.get a) (l.get b) := List.pairwise_iff_get.1 h _ _ hab theorem Sorted.rel_get_of_le [IsRefl α r] {l : List α} (h : l.Sorted r) {a b : Fin l.length} (hab : a ≤ b) : r (l.get a) (l.get b) := by obtain rfl | hlt := Fin.eq_or_lt_of_le hab; exacts [refl _, h.rel_get_of_lt hlt] theorem Sorted.rel_of_mem_take_of_mem_drop {l : List α} (h : List.Sorted r l) {k : ℕ} {x y : α} (hx : x ∈ List.take k l) (hy : y ∈ List.drop k l) : r x y := by obtain ⟨iy, hiy, rfl⟩ := getElem_of_mem hy obtain ⟨ix, hix, rfl⟩ := getElem_of_mem hx rw [getElem_take, getElem_drop] rw [length_take] at hix exact h.rel_get_of_lt (Nat.lt_add_right _ (Nat.lt_min.mp hix).left) /-- If a list is sorted with respect to a decidable relation, then it is sorted with respect to the corresponding Bool-valued relation. -/ theorem Sorted.decide [DecidableRel r] (l : List α) (h : Sorted r l) : Sorted (fun a b => decide (r a b) = true) l := by refine h.imp fun {a b} h => by simpa using h end Sorted section Monotone variable {n : ℕ} {α : Type u} {f : Fin n → α} open scoped Relator in theorem sorted_ofFn_iff {r : α → α → Prop} : (ofFn f).Sorted r ↔ ((· < ·) ⇒ r) f f := by simp_rw [Sorted, pairwise_iff_get, get_ofFn, Relator.LiftFun] exact Iff.symm (Fin.rightInverse_cast _).surjective.forall₂ variable [Preorder α] /-- The list `List.ofFn f` is strictly sorted with respect to `(· ≤ ·)` if and only if `f` is strictly monotone. -/ @[simp] theorem sorted_lt_ofFn_iff : (ofFn f).Sorted (· < ·) ↔ StrictMono f := sorted_ofFn_iff /-- The list `List.ofFn f` is strictly sorted with respect to `(· ≥ ·)` if and only if `f` is strictly antitone. -/ @[simp] theorem sorted_gt_ofFn_iff : (ofFn f).Sorted (· > ·) ↔ StrictAnti f := sorted_ofFn_iff /-- The list `List.ofFn f` is sorted with respect to `(· ≤ ·)` if and only if `f` is monotone. -/ @[simp] theorem sorted_le_ofFn_iff : (ofFn f).Sorted (· ≤ ·) ↔ Monotone f := sorted_ofFn_iff.trans monotone_iff_forall_lt.symm /-- The list obtained from a monotone tuple is sorted. -/ alias ⟨_, _root_.Monotone.ofFn_sorted⟩ := sorted_le_ofFn_iff /-- The list `List.ofFn f` is sorted with respect to `(· ≥ ·)` if and only if `f` is antitone. -/ @[simp] theorem sorted_ge_ofFn_iff : (ofFn f).Sorted (· ≥ ·) ↔ Antitone f := sorted_ofFn_iff.trans antitone_iff_forall_lt.symm /-- The list obtained from an antitone tuple is sorted. -/ alias ⟨_, _root_.Antitone.ofFn_sorted⟩ := sorted_ge_ofFn_iff end Monotone lemma Sorted.filterMap {α β : Type*} {p : α → Option β} {l : List α} {r : α → α → Prop} {r' : β → β → Prop} (hl : l.Sorted r) (hp : ∀ (a b : α) (c d : β), p a = some c → p b = some d → r a b → r' c d) : (l.filterMap p).Sorted r' := by induction l with | nil => simp | cons a l ih => rw [List.filterMap_cons] cases ha : p a with | none => exact ih (List.sorted_cons.mp hl).right | some b => rw [List.sorted_cons] refine ⟨fun x hx ↦ ?_, ih (List.sorted_cons.mp hl).right⟩ obtain ⟨u, hu, hu'⟩ := List.mem_filterMap.mp hx exact hp a u b x ha hu' <| (List.sorted_cons.mp hl).left u hu end List open List namespace RelEmbedding variable {α β : Type*} {ra : α → α → Prop} {rb : β → β → Prop} @[simp] theorem sorted_listMap (e : ra ↪r rb) {l : List α} : (l.map e).Sorted rb ↔ l.Sorted ra := by simp [Sorted, pairwise_map, e.map_rel_iff] @[simp] theorem sorted_swap_listMap (e : ra ↪r rb) {l : List α} : (l.map e).Sorted (Function.swap rb) ↔ l.Sorted (Function.swap ra) := by simp [Sorted, pairwise_map, e.map_rel_iff] end RelEmbedding namespace OrderEmbedding variable {α β : Type*} [Preorder α] [Preorder β] @[simp] theorem sorted_lt_listMap (e : α ↪o β) {l : List α} : (l.map e).Sorted (· < ·) ↔ l.Sorted (· < ·) := e.ltEmbedding.sorted_listMap @[simp] theorem sorted_gt_listMap (e : α ↪o β) {l : List α} : (l.map e).Sorted (· > ·) ↔ l.Sorted (· > ·) := e.ltEmbedding.sorted_swap_listMap end OrderEmbedding namespace RelIso variable {α β : Type*} {ra : α → α → Prop} {rb : β → β → Prop} @[simp] theorem sorted_listMap (e : ra ≃r rb) {l : List α} : (l.map e).Sorted rb ↔ l.Sorted ra := e.toRelEmbedding.sorted_listMap @[simp] theorem sorted_swap_listMap (e : ra ≃r rb) {l : List α} : (l.map e).Sorted (Function.swap rb) ↔ l.Sorted (Function.swap ra) := e.toRelEmbedding.sorted_swap_listMap end RelIso namespace OrderIso variable {α β : Type*} [Preorder α] [Preorder β] @[simp] theorem sorted_lt_listMap (e : α ≃o β) {l : List α} : (l.map e).Sorted (· < ·) ↔ l.Sorted (· < ·) := e.toOrderEmbedding.sorted_lt_listMap @[simp] theorem sorted_gt_listMap (e : α ≃o β) {l : List α} : (l.map e).Sorted (· > ·) ↔ l.Sorted (· > ·) := e.toOrderEmbedding.sorted_gt_listMap end OrderIso namespace StrictMono variable {α β : Type*} [LinearOrder α] [Preorder β] {f : α → β} {l : List α} theorem sorted_le_listMap (hf : StrictMono f) : (l.map f).Sorted (· ≤ ·) ↔ l.Sorted (· ≤ ·) := (OrderEmbedding.ofStrictMono f hf).sorted_listMap theorem sorted_ge_listMap (hf : StrictMono f) : (l.map f).Sorted (· ≥ ·) ↔ l.Sorted (· ≥ ·) := (OrderEmbedding.ofStrictMono f hf).sorted_swap_listMap theorem sorted_lt_listMap (hf : StrictMono f) : (l.map f).Sorted (· < ·) ↔ l.Sorted (· < ·) := (OrderEmbedding.ofStrictMono f hf).sorted_lt_listMap theorem sorted_gt_listMap (hf : StrictMono f) : (l.map f).Sorted (· > ·) ↔ l.Sorted (· > ·) := (OrderEmbedding.ofStrictMono f hf).sorted_gt_listMap end StrictMono namespace StrictAnti variable {α β : Type*} [LinearOrder α] [Preorder β] {f : α → β} {l : List α} theorem sorted_le_listMap (hf : StrictAnti f) : (l.map f).Sorted (· ≤ ·) ↔ l.Sorted (· ≥ ·) := hf.dual_right.sorted_ge_listMap theorem sorted_ge_listMap (hf : StrictAnti f) : (l.map f).Sorted (· ≥ ·) ↔ l.Sorted (· ≤ ·) := hf.dual_right.sorted_le_listMap theorem sorted_lt_listMap (hf : StrictAnti f) : (l.map f).Sorted (· < ·) ↔ l.Sorted (· > ·) := hf.dual_right.sorted_gt_listMap theorem sorted_gt_listMap (hf : StrictAnti f) : (l.map f).Sorted (· > ·) ↔ l.Sorted (· < ·) := hf.dual_right.sorted_lt_listMap end StrictAnti namespace List section sort variable {α : Type u} {β : Type v} (r : α → α → Prop) (s : β → β → Prop) variable [DecidableRel r] [DecidableRel s] local infixl:50 " ≼ " => r local infixl:50 " ≼ " => s /-! ### Insertion sort -/ section InsertionSort /-- `orderedInsert a l` inserts `a` into `l` at such that `orderedInsert a l` is sorted if `l` is. -/ @[simp] def orderedInsert (a : α) : List α → List α | [] => [a] | b :: l => if a ≼ b then a :: b :: l else b :: orderedInsert a l theorem orderedInsert_of_le {a b : α} (l : List α) (h : a ≼ b) : orderedInsert r a (b :: l) = a :: b :: l := dif_pos h /-- `insertionSort l` returns `l` sorted using the insertion sort algorithm. -/ @[simp] def insertionSort : List α → List α | [] => [] | b :: l => orderedInsert r b (insertionSort l)
-- A quick check that insertionSort is stable:
Mathlib/Data/List/Sort.lean
391
392
/- 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`, i.e., there exist an open `U ⊇ K` and `T ∈ l` such that `U ∩ T ⊆ s`. -/ theorem IsCompact.mem_nhdsSet_inf_of_forall {K : Set X} {l : Filter X} {s : Set X} (hK : IsCompact K) (hs : ∀ x ∈ K, s ∈ 𝓝 x ⊓ l) : s ∈ (𝓝ˢ K) ⊓ l := (hK.nhdsSet_inf_eq_biSup l).symm ▸ by simpa using hs /-- If `s : Set S` belongs to `l ⊓ 𝓝 x` for all `x` 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_inf_nhdsSet_of_forall {K : Set X} {l : Filter X} {s : Set X} (hK : IsCompact K) (hs : ∀ y ∈ K, s ∈ l ⊓ 𝓝 y) : s ∈ l ⊓ 𝓝ˢ K := (hK.inf_nhdsSet_eq_biSup l).symm ▸ by simpa using hs /-- To show that `∀ y ∈ K, P x y` holds for `x` close enough to `x₀` when `K` is compact, it is sufficient to show that for all `y₀ ∈ K` there `P x y` holds for `(x, y)` close enough to `(x₀, y₀)`. Provided for backwards compatibility, see `IsCompact.mem_prod_nhdsSet_of_forall` for a stronger statement. -/ theorem IsCompact.eventually_forall_of_forall_eventually {x₀ : X} {K : Set Y} (hK : IsCompact K) {P : X → Y → Prop} (hP : ∀ y ∈ K, ∀ᶠ z : X × Y in 𝓝 (x₀, y), P z.1 z.2) : ∀ᶠ x in 𝓝 x₀, ∀ y ∈ K, P x y := by simp only [nhds_prod_eq, ← eventually_iSup, ← hK.prod_nhdsSet_eq_biSup] at hP exact hP.curry.mono fun _ h ↦ h.self_of_nhdsSet theorem isCompact_empty : IsCompact (∅ : Set X) := fun _f hnf hsf => Not.elim hnf.ne <| empty_mem_iff_bot.1 <| le_principal_iff.1 hsf theorem isCompact_singleton {x : X} : IsCompact ({x} : Set X) := fun _ hf hfa => ⟨x, rfl, ClusterPt.of_le_nhds' (hfa.trans <| by simpa only [principal_singleton] using pure_le_nhds x) hf⟩ theorem Set.Subsingleton.isCompact (hs : s.Subsingleton) : IsCompact s := Subsingleton.induction_on hs isCompact_empty fun _ => isCompact_singleton theorem Set.Finite.isCompact_biUnion {s : Set ι} {f : ι → Set X} (hs : s.Finite) (hf : ∀ i ∈ s, IsCompact (f i)) : IsCompact (⋃ i ∈ s, f i) := isCompact_iff_ultrafilter_le_nhds'.2 fun l hl => by rw [Ultrafilter.finite_biUnion_mem_iff hs] at hl rcases hl with ⟨i, his, hi⟩ rcases (hf i his).ultrafilter_le_nhds _ (le_principal_iff.2 hi) with ⟨x, hxi, hlx⟩ exact ⟨x, mem_iUnion₂.2 ⟨i, his, hxi⟩, hlx⟩ theorem Finset.isCompact_biUnion (s : Finset ι) {f : ι → Set X} (hf : ∀ i ∈ s, IsCompact (f i)) : IsCompact (⋃ i ∈ s, f i) := s.finite_toSet.isCompact_biUnion hf theorem isCompact_accumulate {K : ℕ → Set X} (hK : ∀ n, IsCompact (K n)) (n : ℕ) : IsCompact (Accumulate K n) := (finite_le_nat n).isCompact_biUnion fun k _ => hK k theorem Set.Finite.isCompact_sUnion {S : Set (Set X)} (hf : S.Finite) (hc : ∀ s ∈ S, IsCompact s) : IsCompact (⋃₀ S) := by rw [sUnion_eq_biUnion]; exact hf.isCompact_biUnion hc theorem isCompact_iUnion {ι : Sort*} {f : ι → Set X} [Finite ι] (h : ∀ i, IsCompact (f i)) : IsCompact (⋃ i, f i) := (finite_range f).isCompact_sUnion <| forall_mem_range.2 h @[simp] theorem Set.Finite.isCompact (hs : s.Finite) : IsCompact s := biUnion_of_singleton s ▸ hs.isCompact_biUnion fun _ _ => isCompact_singleton theorem IsCompact.finite_of_discrete [DiscreteTopology X] (hs : IsCompact s) : s.Finite := by have : ∀ x : X, ({x} : Set X) ∈ 𝓝 x := by simp [nhds_discrete] rcases hs.elim_nhds_subcover (fun x => {x}) fun x _ => this x with ⟨t, _, hst⟩ simp only [← t.set_biUnion_coe, biUnion_of_singleton] at hst exact t.finite_toSet.subset hst theorem isCompact_iff_finite [DiscreteTopology X] : IsCompact s ↔ s.Finite := ⟨fun h => h.finite_of_discrete, fun h => h.isCompact⟩ theorem IsCompact.union (hs : IsCompact s) (ht : IsCompact t) : IsCompact (s ∪ t) := by rw [union_eq_iUnion]; exact isCompact_iUnion fun b => by cases b <;> assumption protected theorem IsCompact.insert (hs : IsCompact s) (a) : IsCompact (insert a s) := isCompact_singleton.union hs -- TODO: reformulate using `𝓝ˢ` /-- If `V : ι → Set X` is a decreasing family of closed compact sets then any neighborhood of `⋂ i, V i` contains some `V i`. We assume each `V i` is compact *and* closed because `X` is not assumed to be Hausdorff. See `exists_subset_nhd_of_compact` for version assuming this. -/ theorem exists_subset_nhds_of_isCompact' [Nonempty ι] {V : ι → Set X} (hV : Directed (· ⊇ ·) V) (hV_cpct : ∀ i, IsCompact (V i)) (hV_closed : ∀ i, IsClosed (V i)) {U : Set X} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := by obtain ⟨W, hsubW, W_op, hWU⟩ := exists_open_set_nhds hU suffices ∃ i, V i ⊆ W from this.imp fun i hi => hi.trans hWU by_contra! H replace H : ∀ i, (V i ∩ Wᶜ).Nonempty := fun i => Set.inter_compl_nonempty_iff.mpr (H i) have : (⋂ i, V i ∩ Wᶜ).Nonempty := by refine IsCompact.nonempty_iInter_of_directed_nonempty_isCompact_isClosed _ (fun i j => ?_) H (fun i => (hV_cpct i).inter_right W_op.isClosed_compl) fun i => (hV_closed i).inter W_op.isClosed_compl rcases hV i j with ⟨k, hki, hkj⟩ refine ⟨k, ⟨fun x => ?_, fun x => ?_⟩⟩ <;> simp only [and_imp, mem_inter_iff, mem_compl_iff] <;> tauto have : ¬⋂ i : ι, V i ⊆ W := by simpa [← iInter_inter, inter_compl_nonempty_iff] contradiction namespace Filter theorem hasBasis_cocompact : (cocompact X).HasBasis IsCompact compl := hasBasis_biInf_principal' (fun s hs t ht => ⟨s ∪ t, hs.union ht, compl_subset_compl.2 subset_union_left, compl_subset_compl.2 subset_union_right⟩) ⟨∅, isCompact_empty⟩ theorem mem_cocompact : s ∈ cocompact X ↔ ∃ t, IsCompact t ∧ tᶜ ⊆ s := hasBasis_cocompact.mem_iff theorem mem_cocompact' : s ∈ cocompact X ↔ ∃ t, IsCompact t ∧ sᶜ ⊆ t := mem_cocompact.trans <| exists_congr fun _ => and_congr_right fun _ => compl_subset_comm theorem _root_.IsCompact.compl_mem_cocompact (hs : IsCompact s) : sᶜ ∈ Filter.cocompact X := hasBasis_cocompact.mem_of_mem hs theorem cocompact_le_cofinite : cocompact X ≤ cofinite := fun s hs => compl_compl s ▸ hs.isCompact.compl_mem_cocompact theorem cocompact_eq_cofinite (X : Type*) [TopologicalSpace X] [DiscreteTopology X] : cocompact X = cofinite := by simp only [cocompact, hasBasis_cofinite.eq_biInf, isCompact_iff_finite] /-- A filter is disjoint from the cocompact filter if and only if it contains a compact set. -/ theorem disjoint_cocompact_left (f : Filter X) : Disjoint (Filter.cocompact X) f ↔ ∃ K ∈ f, IsCompact K := by simp_rw [hasBasis_cocompact.disjoint_iff_left, compl_compl] tauto /-- A filter is disjoint from the cocompact filter if and only if it contains a compact set. -/ theorem disjoint_cocompact_right (f : Filter X) : Disjoint f (Filter.cocompact X) ↔ ∃ K ∈ f, IsCompact K := by simp_rw [hasBasis_cocompact.disjoint_iff_right, compl_compl] tauto theorem Tendsto.isCompact_insert_range_of_cocompact {f : X → Y} {y} (hf : Tendsto f (cocompact X) (𝓝 y)) (hfc : Continuous f) : IsCompact (insert y (range f)) := by intro l hne hle by_cases hy : ClusterPt y l · exact ⟨y, Or.inl rfl, hy⟩ simp only [clusterPt_iff_nonempty, not_forall, ← not_disjoint_iff_nonempty_inter, not_not] at hy rcases hy with ⟨s, hsy, t, htl, hd⟩ rcases mem_cocompact.1 (hf hsy) with ⟨K, hKc, hKs⟩ have : f '' K ∈ l := by filter_upwards [htl, le_principal_iff.1 hle] with y hyt hyf rcases hyf with (rfl | ⟨x, rfl⟩) exacts [(hd.le_bot ⟨mem_of_mem_nhds hsy, hyt⟩).elim, mem_image_of_mem _ (not_not.1 fun hxK => hd.le_bot ⟨hKs hxK, hyt⟩)] rcases hKc.image hfc (le_principal_iff.2 this) with ⟨y, hy, hyl⟩ exact ⟨y, Or.inr <| image_subset_range _ _ hy, hyl⟩ theorem Tendsto.isCompact_insert_range_of_cofinite {f : ι → X} {x} (hf : Tendsto f cofinite (𝓝 x)) : IsCompact (insert x (range f)) := by letI : TopologicalSpace ι := ⊥; haveI h : DiscreteTopology ι := ⟨rfl⟩ rw [← cocompact_eq_cofinite ι] at hf exact hf.isCompact_insert_range_of_cocompact continuous_of_discreteTopology theorem Tendsto.isCompact_insert_range {f : ℕ → X} {x} (hf : Tendsto f atTop (𝓝 x)) : IsCompact (insert x (range f)) := Filter.Tendsto.isCompact_insert_range_of_cofinite <| Nat.cofinite_eq_atTop.symm ▸ hf theorem hasBasis_coclosedCompact : (Filter.coclosedCompact X).HasBasis (fun s => IsClosed s ∧ IsCompact s) compl := by simp only [Filter.coclosedCompact, iInf_and'] refine hasBasis_biInf_principal' ?_ ⟨∅, isClosed_empty, isCompact_empty⟩ rintro s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩ exact ⟨s ∪ t, ⟨⟨hs₁.union ht₁, hs₂.union ht₂⟩, compl_subset_compl.2 subset_union_left, compl_subset_compl.2 subset_union_right⟩⟩ /-- A set belongs to `coclosedCompact` if and only if the closure of its complement is compact. -/ theorem mem_coclosedCompact_iff : s ∈ coclosedCompact X ↔ IsCompact (closure sᶜ) := by refine hasBasis_coclosedCompact.mem_iff.trans ⟨?_, fun h ↦ ?_⟩ · rintro ⟨t, ⟨htcl, htco⟩, hst⟩ exact htco.of_isClosed_subset isClosed_closure <| closure_minimal (compl_subset_comm.2 hst) htcl · exact ⟨closure sᶜ, ⟨isClosed_closure, h⟩, compl_subset_comm.2 subset_closure⟩ /-- Complement of a set belongs to `coclosedCompact` if and only if its closure is compact. -/ theorem compl_mem_coclosedCompact : sᶜ ∈ coclosedCompact X ↔ IsCompact (closure s) := by rw [mem_coclosedCompact_iff, compl_compl] theorem cocompact_le_coclosedCompact : cocompact X ≤ coclosedCompact X := iInf_mono fun _ => le_iInf fun _ => le_rfl end Filter theorem IsCompact.compl_mem_coclosedCompact_of_isClosed (hs : IsCompact s) (hs' : IsClosed s) : sᶜ ∈ Filter.coclosedCompact X := hasBasis_coclosedCompact.mem_of_mem ⟨hs', hs⟩ namespace Bornology variable (X) in /-- Sets that are contained in a compact set form a bornology. Its `cobounded` filter is `Filter.cocompact`. See also `Bornology.relativelyCompact` the bornology of sets with compact closure. -/ def inCompact : Bornology X where cobounded' := Filter.cocompact X le_cofinite' := Filter.cocompact_le_cofinite theorem inCompact.isBounded_iff : @IsBounded _ (inCompact X) s ↔ ∃ t, IsCompact t ∧ s ⊆ t := by change sᶜ ∈ Filter.cocompact X ↔ _ rw [Filter.mem_cocompact] simp end Bornology /-- If `s` and `t` are compact sets, then the set neighborhoods filter of `s ×ˢ t` is the product of set neighborhoods filters for `s` and `t`. For general sets, only the `≤` inequality holds, see `nhdsSet_prod_le`. -/ theorem IsCompact.nhdsSet_prod_eq {t : Set Y} (hs : IsCompact s) (ht : IsCompact t) : 𝓝ˢ (s ×ˢ t) = 𝓝ˢ s ×ˢ 𝓝ˢ t := by simp_rw [hs.nhdsSet_prod_eq_biSup, ht.prod_nhdsSet_eq_biSup, nhdsSet, sSup_image, biSup_prod, nhds_prod_eq] theorem nhdsSet_prod_le_of_disjoint_cocompact {f : Filter Y} (hs : IsCompact s) (hf : Disjoint f (Filter.cocompact Y)) : 𝓝ˢ s ×ˢ f ≤ 𝓝ˢ (s ×ˢ Set.univ) := by obtain ⟨K, hKf, hK⟩ := (disjoint_cocompact_right f).mp hf calc 𝓝ˢ s ×ˢ f _ ≤ 𝓝ˢ s ×ˢ 𝓟 K := Filter.prod_mono_right _ (Filter.le_principal_iff.mpr hKf) _ ≤ 𝓝ˢ s ×ˢ 𝓝ˢ K := Filter.prod_mono_right _ principal_le_nhdsSet _ = 𝓝ˢ (s ×ˢ K) := (hs.nhdsSet_prod_eq hK).symm _ ≤ 𝓝ˢ (s ×ˢ Set.univ) := nhdsSet_mono (prod_mono_right le_top) theorem prod_nhdsSet_le_of_disjoint_cocompact {t : Set Y} {f : Filter X} (ht : IsCompact t) (hf : Disjoint f (Filter.cocompact X)) : f ×ˢ 𝓝ˢ t ≤ 𝓝ˢ (Set.univ ×ˢ t) := by obtain ⟨K, hKf, hK⟩ := (disjoint_cocompact_right f).mp hf calc f ×ˢ 𝓝ˢ t _ ≤ (𝓟 K) ×ˢ 𝓝ˢ t := Filter.prod_mono_left _ (Filter.le_principal_iff.mpr hKf) _ ≤ 𝓝ˢ K ×ˢ 𝓝ˢ t := Filter.prod_mono_left _ principal_le_nhdsSet _ = 𝓝ˢ (K ×ˢ t) := (hK.nhdsSet_prod_eq ht).symm _ ≤ 𝓝ˢ (Set.univ ×ˢ t) := nhdsSet_mono (prod_mono_left le_top) theorem nhds_prod_le_of_disjoint_cocompact {f : Filter Y} (x : X) (hf : Disjoint f (Filter.cocompact Y)) : 𝓝 x ×ˢ f ≤ 𝓝ˢ ({x} ×ˢ Set.univ) := by simpa using nhdsSet_prod_le_of_disjoint_cocompact isCompact_singleton hf theorem prod_nhds_le_of_disjoint_cocompact {f : Filter X} (y : Y) (hf : Disjoint f (Filter.cocompact X)) : f ×ˢ 𝓝 y ≤ 𝓝ˢ (Set.univ ×ˢ {y}) := by simpa using prod_nhdsSet_le_of_disjoint_cocompact isCompact_singleton hf /-- If `s` and `t` are compact sets and `n` is an open neighborhood of `s × t`, then there exist open neighborhoods `u ⊇ s` and `v ⊇ t` such that `u × v ⊆ n`. See also `IsCompact.nhdsSet_prod_eq`. -/ theorem generalized_tube_lemma (hs : IsCompact s) {t : Set Y} (ht : IsCompact t) {n : Set (X × Y)} (hn : IsOpen n) (hp : s ×ˢ t ⊆ n) : ∃ (u : Set X) (v : Set Y), IsOpen u ∧ IsOpen v ∧ s ⊆ u ∧ t ⊆ v ∧ u ×ˢ v ⊆ n := by rw [← hn.mem_nhdsSet, hs.nhdsSet_prod_eq ht, ((hasBasis_nhdsSet _).prod (hasBasis_nhdsSet _)).mem_iff] at hp rcases hp with ⟨⟨u, v⟩, ⟨⟨huo, hsu⟩, hvo, htv⟩, hn⟩ exact ⟨u, v, huo, hvo, hsu, htv, hn⟩ -- see Note [lower instance priority] instance (priority := 10) Subsingleton.compactSpace [Subsingleton X] : CompactSpace X := ⟨subsingleton_univ.isCompact⟩ theorem isCompact_univ_iff : IsCompact (univ : Set X) ↔ CompactSpace X := ⟨fun h => ⟨h⟩, fun h => h.1⟩ theorem isCompact_univ [h : CompactSpace X] : IsCompact (univ : Set X) := h.isCompact_univ theorem exists_clusterPt_of_compactSpace [CompactSpace X] (f : Filter X) [NeBot f] : ∃ x, ClusterPt x f := by simpa using isCompact_univ (show f ≤ 𝓟 univ by simp) nonrec theorem Ultrafilter.le_nhds_lim [CompactSpace X] (F : Ultrafilter X) : ↑F ≤ 𝓝 F.lim := by rcases isCompact_univ.ultrafilter_le_nhds F (by simp) with ⟨x, -, h⟩ exact le_nhds_lim ⟨x, h⟩ theorem CompactSpace.elim_nhds_subcover [CompactSpace X] (U : X → Set X) (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : Finset X, ⋃ x ∈ t, U x = ⊤ := by obtain ⟨t, -, s⟩ := IsCompact.elim_nhds_subcover isCompact_univ U fun x _ => hU x exact ⟨t, top_unique s⟩ theorem compactSpace_of_finite_subfamily_closed (h : ∀ {ι : Type u} (t : ι → Set X), (∀ i, IsClosed (t i)) → ⋂ i, t i = ∅ → ∃ u : Finset ι, ⋂ i ∈ u, t i = ∅) : CompactSpace X where isCompact_univ := isCompact_of_finite_subfamily_closed fun t => by simpa using h t theorem IsClosed.isCompact [CompactSpace X] (h : IsClosed s) : IsCompact s := isCompact_univ.of_isClosed_subset h (subset_univ _) /-- If a filter has a unique cluster point `y` in a compact topological space, then the filter is less than or equal to `𝓝 y`. -/ lemma le_nhds_of_unique_clusterPt [CompactSpace X] {l : Filter X} {y : X} (h : ∀ x, ClusterPt x l → x = y) : l ≤ 𝓝 y := isCompact_univ.le_nhds_of_unique_clusterPt univ_mem fun x _ ↦ h x /-- If `y` is a unique `MapClusterPt` for `f` along `l` and the codomain of `f` is a compact space, then `f` tends to `𝓝 y` along `l`. -/ lemma tendsto_nhds_of_unique_mapClusterPt [CompactSpace X] {Y} {l : Filter Y} {y : X} {f : Y → X} (h : ∀ x, MapClusterPt x l f → x = y) : Tendsto f l (𝓝 y) := le_nhds_of_unique_clusterPt h lemma noncompact_univ (X : Type*) [TopologicalSpace X] [NoncompactSpace X] : ¬IsCompact (univ : Set X) := NoncompactSpace.noncompact_univ theorem IsCompact.ne_univ [NoncompactSpace X] (hs : IsCompact s) : s ≠ univ := fun h => noncompact_univ X (h ▸ hs) instance [NoncompactSpace X] : NeBot (Filter.cocompact X) := by refine Filter.hasBasis_cocompact.neBot_iff.2 fun hs => ?_ contrapose hs; rw [not_nonempty_iff_eq_empty, compl_empty_iff] at hs rw [hs]; exact noncompact_univ X @[simp] theorem Filter.cocompact_eq_bot [CompactSpace X] : Filter.cocompact X = ⊥ := Filter.hasBasis_cocompact.eq_bot_iff.mpr ⟨Set.univ, isCompact_univ, Set.compl_univ⟩ instance [NoncompactSpace X] : NeBot (Filter.coclosedCompact X) := neBot_of_le Filter.cocompact_le_coclosedCompact theorem noncompactSpace_of_neBot (_ : NeBot (Filter.cocompact X)) : NoncompactSpace X := ⟨fun h' => (Filter.nonempty_of_mem h'.compl_mem_cocompact).ne_empty compl_univ⟩ theorem Filter.cocompact_neBot_iff : NeBot (Filter.cocompact X) ↔ NoncompactSpace X := ⟨noncompactSpace_of_neBot, fun _ => inferInstance⟩ theorem not_compactSpace_iff : ¬CompactSpace X ↔ NoncompactSpace X := ⟨fun h₁ => ⟨fun h₂ => h₁ ⟨h₂⟩⟩, fun ⟨h₁⟩ ⟨h₂⟩ => h₁ h₂⟩ instance : NoncompactSpace ℤ := noncompactSpace_of_neBot <| by simp only [Filter.cocompact_eq_cofinite, Filter.cofinite_neBot] -- Note: We can't make this into an instance because it loops with `Finite.compactSpace`. /-- A compact discrete space is finite. -/ theorem finite_of_compact_of_discrete [CompactSpace X] [DiscreteTopology X] : Finite X := Finite.of_finite_univ <| isCompact_univ.finite_of_discrete lemma Set.Infinite.exists_accPt_cofinite_inf_principal_of_subset_isCompact {K : Set X} (hs : s.Infinite) (hK : IsCompact K) (hsub : s ⊆ K) : ∃ x ∈ K, AccPt x (cofinite ⊓ 𝓟 s) := (@hK _ hs.cofinite_inf_principal_neBot (inf_le_right.trans <| principal_mono.2 hsub)).imp fun x hx ↦ by rwa [accPt_iff_clusterPt, inf_comm, inf_right_comm, (finite_singleton _).cofinite_inf_principal_compl] lemma Set.Infinite.exists_accPt_of_subset_isCompact {K : Set X} (hs : s.Infinite) (hK : IsCompact K) (hsub : s ⊆ K) : ∃ x ∈ K, AccPt x (𝓟 s) := let ⟨x, hxK, hx⟩ := hs.exists_accPt_cofinite_inf_principal_of_subset_isCompact hK hsub ⟨x, hxK, hx.mono inf_le_right⟩ lemma Set.Infinite.exists_accPt_cofinite_inf_principal [CompactSpace X] (hs : s.Infinite) : ∃ x, AccPt x (cofinite ⊓ 𝓟 s) := by simpa only [mem_univ, true_and] using hs.exists_accPt_cofinite_inf_principal_of_subset_isCompact isCompact_univ s.subset_univ lemma Set.Infinite.exists_accPt_principal [CompactSpace X] (hs : s.Infinite) : ∃ x, AccPt x (𝓟 s) := hs.exists_accPt_cofinite_inf_principal.imp fun _x hx ↦ hx.mono inf_le_right theorem exists_nhds_ne_neBot (X : Type*) [TopologicalSpace X] [CompactSpace X] [Infinite X] : ∃ z : X, (𝓝[≠] z).NeBot := by simpa [AccPt] using (@infinite_univ X _).exists_accPt_principal theorem finite_cover_nhds_interior [CompactSpace X] {U : X → Set X} (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : Finset X, ⋃ x ∈ t, interior (U x) = univ := let ⟨t, ht⟩ := isCompact_univ.elim_finite_subcover (fun x => interior (U x)) (fun _ => isOpen_interior) fun x _ => mem_iUnion.2 ⟨x, mem_interior_iff_mem_nhds.2 (hU x)⟩ ⟨t, univ_subset_iff.1 ht⟩ theorem finite_cover_nhds [CompactSpace X] {U : X → Set X} (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : Finset X, ⋃ x ∈ t, U x = univ := let ⟨t, ht⟩ := finite_cover_nhds_interior hU ⟨t, univ_subset_iff.1 <| ht.symm.subset.trans <| iUnion₂_mono fun _ _ => interior_subset⟩ /-- The comap of the cocompact filter on `Y` by a continuous function `f : X → Y` is less than or equal to the cocompact filter on `X`. This is a reformulation of the fact that images of compact sets are compact. -/ theorem Filter.comap_cocompact_le {f : X → Y} (hf : Continuous f) : (Filter.cocompact Y).comap f ≤ Filter.cocompact X := by rw [(Filter.hasBasis_cocompact.comap f).le_basis_iff Filter.hasBasis_cocompact] intro t ht refine ⟨f '' t, ht.image hf, ?_⟩ simpa using t.subset_preimage_image f /-- If a filter is disjoint from the cocompact filter, so is its image under any continuous function. -/ theorem disjoint_map_cocompact {g : X → Y} {f : Filter X} (hg : Continuous g) (hf : Disjoint f (Filter.cocompact X)) : Disjoint (map g f) (Filter.cocompact Y) := by rw [← Filter.disjoint_comap_iff_map, disjoint_iff_inf_le] calc f ⊓ (comap g (cocompact Y)) _ ≤ f ⊓ Filter.cocompact X := inf_le_inf_left f (Filter.comap_cocompact_le hg) _ = ⊥ := disjoint_iff.mp hf theorem isCompact_range [CompactSpace X] {f : X → Y} (hf : Continuous f) : IsCompact (range f) := by rw [← image_univ]; exact isCompact_univ.image hf theorem isCompact_diagonal [CompactSpace X] : IsCompact (diagonal X) := @range_diag X ▸ isCompact_range (continuous_id.prodMk continuous_id) /-- If `X` is a compact topological space, then `Prod.snd : X × Y → Y` is a closed map. -/ theorem isClosedMap_snd_of_compactSpace [CompactSpace X] : IsClosedMap (Prod.snd : X × Y → Y) := fun s hs => by rw [← isOpen_compl_iff, isOpen_iff_mem_nhds] intro y hy have : univ ×ˢ {y} ⊆ sᶜ := by exact fun (x, y') ⟨_, rfl⟩ hs => hy ⟨(x, y'), hs, rfl⟩ rcases generalized_tube_lemma isCompact_univ isCompact_singleton hs.isOpen_compl this with ⟨U, V, -, hVo, hU, hV, hs⟩ refine mem_nhds_iff.2 ⟨V, ?_, hVo, hV rfl⟩ rintro _ hzV ⟨z, hzs, rfl⟩ exact hs ⟨hU trivial, hzV⟩ hzs /-- If `Y` is a compact topological space, then `Prod.fst : X × Y → X` is a closed map. -/ theorem isClosedMap_fst_of_compactSpace [CompactSpace Y] : IsClosedMap (Prod.fst : X × Y → X) := isClosedMap_snd_of_compactSpace.comp isClosedMap_swap theorem exists_subset_nhds_of_compactSpace [CompactSpace X] [Nonempty ι] {V : ι → Set X} (hV : Directed (· ⊇ ·) V) (hV_closed : ∀ i, IsClosed (V i)) {U : Set X} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := exists_subset_nhds_of_isCompact' hV (fun i => (hV_closed i).isCompact) hV_closed hU /-- If `f : X → Y` is an inducing map, the image `f '' s` of a set `s` is compact if and only if `s` is compact. -/ theorem Topology.IsInducing.isCompact_iff {f : X → Y} (hf : IsInducing f) : IsCompact s ↔ IsCompact (f '' s) := by refine ⟨fun hs => hs.image hf.continuous, fun hs F F_ne_bot F_le => ?_⟩ obtain ⟨_, ⟨x, x_in : x ∈ s, rfl⟩, hx : ClusterPt (f x) (map f F)⟩ := hs ((map_mono F_le).trans_eq map_principal) exact ⟨x, x_in, hf.mapClusterPt_iff.1 hx⟩ @[deprecated (since := "2024-10-28")] alias Inducing.isCompact_iff := IsInducing.isCompact_iff /-- If `f : X → Y` is an embedding, the image `f '' s` of a set `s` is compact if and only if `s` is compact. -/ theorem Topology.IsEmbedding.isCompact_iff {f : X → Y} (hf : IsEmbedding f) : IsCompact s ↔ IsCompact (f '' s) := hf.isInducing.isCompact_iff @[deprecated (since := "2024-10-26")] alias Embedding.isCompact_iff := IsEmbedding.isCompact_iff /-- The preimage of a compact set under an inducing map is a compact set. -/ theorem Topology.IsInducing.isCompact_preimage (hf : IsInducing f) (hf' : IsClosed (range f)) {K : Set Y} (hK : IsCompact K) : IsCompact (f ⁻¹' K) := by replace hK := hK.inter_right hf' rwa [hf.isCompact_iff, image_preimage_eq_inter_range] @[deprecated (since := "2024-10-28")] alias Inducing.isCompact_preimage := IsInducing.isCompact_preimage lemma Topology.IsInducing.isCompact_preimage_iff {f : X → Y} (hf : IsInducing f) {K : Set Y} (Kf : K ⊆ range f) : IsCompact (f ⁻¹' K) ↔ IsCompact K := by rw [hf.isCompact_iff, image_preimage_eq_of_subset Kf] @[deprecated (since := "2024-10-28")] alias Inducing.isCompact_preimage_iff := IsInducing.isCompact_preimage_iff /-- The preimage of a compact set in the image of an inducing map is compact. -/ lemma Topology.IsInducing.isCompact_preimage' (hf : IsInducing f) {K : Set Y} (hK : IsCompact K) (Kf : K ⊆ range f) : IsCompact (f ⁻¹' K) := (hf.isCompact_preimage_iff Kf).2 hK @[deprecated (since := "2024-10-28")] alias Inducing.isCompact_preimage' := IsInducing.isCompact_preimage' /-- The preimage of a compact set under a closed embedding is a compact set. -/ theorem Topology.IsClosedEmbedding.isCompact_preimage (hf : IsClosedEmbedding f) {K : Set Y} (hK : IsCompact K) : IsCompact (f ⁻¹' K) := hf.isInducing.isCompact_preimage (hf.isClosed_range) hK /-- A closed embedding is proper, ie, inverse images of compact sets are contained in compacts. Moreover, the preimage of a compact set is compact, see `IsClosedEmbedding.isCompact_preimage`. -/ theorem Topology.IsClosedEmbedding.tendsto_cocompact (hf : IsClosedEmbedding f) : Tendsto f (Filter.cocompact X) (Filter.cocompact Y) := Filter.hasBasis_cocompact.tendsto_right_iff.mpr fun _K hK => (hf.isCompact_preimage hK).compl_mem_cocompact /-- Sets of subtype are compact iff the image under a coercion is. -/ theorem Subtype.isCompact_iff {p : X → Prop} {s : Set { x // p x }} : IsCompact s ↔ IsCompact ((↑) '' s : Set X) := IsEmbedding.subtypeVal.isCompact_iff theorem isCompact_iff_isCompact_univ : IsCompact s ↔ IsCompact (univ : Set s) := by rw [Subtype.isCompact_iff, image_univ, Subtype.range_coe] theorem isCompact_iff_compactSpace : IsCompact s ↔ CompactSpace s := isCompact_iff_isCompact_univ.trans isCompact_univ_iff theorem IsCompact.finite (hs : IsCompact s) (hs' : DiscreteTopology s) : s.Finite := finite_coe_iff.mp (@finite_of_compact_of_discrete _ _ (isCompact_iff_compactSpace.mp hs) hs') theorem exists_nhds_ne_inf_principal_neBot (hs : IsCompact s) (hs' : s.Infinite) : ∃ z ∈ s, (𝓝[≠] z ⊓ 𝓟 s).NeBot := hs'.exists_accPt_of_subset_isCompact hs Subset.rfl protected theorem Topology.IsClosedEmbedding.noncompactSpace [NoncompactSpace X] {f : X → Y} (hf : IsClosedEmbedding f) : NoncompactSpace Y := noncompactSpace_of_neBot hf.tendsto_cocompact.neBot protected theorem Topology.IsClosedEmbedding.compactSpace [h : CompactSpace Y] {f : X → Y} (hf : IsClosedEmbedding f) : CompactSpace X := ⟨by rw [hf.isInducing.isCompact_iff, image_univ]; exact hf.isClosed_range.isCompact⟩ theorem IsCompact.prod {t : Set Y} (hs : IsCompact s) (ht : IsCompact t) : IsCompact (s ×ˢ t) := by rw [isCompact_iff_ultrafilter_le_nhds'] at hs ht ⊢ intro f hfs obtain ⟨x : X, sx : x ∈ s, hx : map Prod.fst f.1 ≤ 𝓝 x⟩ := hs (f.map Prod.fst) (mem_map.2 <| mem_of_superset hfs fun x => And.left) obtain ⟨y : Y, ty : y ∈ t, hy : map Prod.snd f.1 ≤ 𝓝 y⟩ := ht (f.map Prod.snd) (mem_map.2 <| mem_of_superset hfs fun x => And.right) rw [map_le_iff_le_comap] at hx hy refine ⟨⟨x, y⟩, ⟨sx, ty⟩, ?_⟩ rw [nhds_prod_eq]; exact le_inf hx hy /-- Finite topological spaces are compact. -/ instance (priority := 100) Finite.compactSpace [Finite X] : CompactSpace X where isCompact_univ := finite_univ.isCompact instance ULift.compactSpace [CompactSpace X] : CompactSpace (ULift.{v} X) := IsClosedEmbedding.uliftDown.compactSpace /-- The product of two compact spaces is compact. -/ instance [CompactSpace X] [CompactSpace Y] : CompactSpace (X × Y) := ⟨by rw [← univ_prod_univ]; exact isCompact_univ.prod isCompact_univ⟩ /-- The disjoint union of two compact spaces is compact. -/ instance [CompactSpace X] [CompactSpace Y] : CompactSpace (X ⊕ Y) := ⟨by rw [← range_inl_union_range_inr] exact (isCompact_range continuous_inl).union (isCompact_range continuous_inr)⟩ instance {X : ι → Type*} [Finite ι] [∀ i, TopologicalSpace (X i)] [∀ i, CompactSpace (X i)] : CompactSpace (Σi, X i) := by refine ⟨?_⟩ rw [Sigma.univ] exact isCompact_iUnion fun i => isCompact_range continuous_sigmaMk /-- The coproduct of the cocompact filters on two topological spaces is the cocompact filter on their product. -/ theorem Filter.coprod_cocompact : (Filter.cocompact X).coprod (Filter.cocompact Y) = Filter.cocompact (X × Y) := by apply le_antisymm · exact sup_le (comap_cocompact_le continuous_fst) (comap_cocompact_le continuous_snd) · refine (hasBasis_cocompact.coprod hasBasis_cocompact).ge_iff.2 fun K hK ↦ ?_ rw [← univ_prod, ← prod_univ, ← compl_prod_eq_union] exact (hK.1.prod hK.2).compl_mem_cocompact theorem Prod.noncompactSpace_iff : NoncompactSpace (X × Y) ↔ NoncompactSpace X ∧ Nonempty Y ∨ Nonempty X ∧ NoncompactSpace Y := by simp [← Filter.cocompact_neBot_iff, ← Filter.coprod_cocompact, Filter.coprod_neBot_iff] -- See Note [lower instance priority] instance (priority := 100) Prod.noncompactSpace_left [NoncompactSpace X] [Nonempty Y] : NoncompactSpace (X × Y) := Prod.noncompactSpace_iff.2 (Or.inl ⟨‹_›, ‹_›⟩) -- See Note [lower instance priority] instance (priority := 100) Prod.noncompactSpace_right [Nonempty X] [NoncompactSpace Y] : NoncompactSpace (X × Y) := Prod.noncompactSpace_iff.2 (Or.inr ⟨‹_›, ‹_›⟩) section Tychonoff variable {X : ι → Type*} [∀ i, TopologicalSpace (X i)] /-- **Tychonoff's theorem**: product of compact sets is compact. -/ theorem isCompact_pi_infinite {s : ∀ i, Set (X i)} : (∀ i, IsCompact (s i)) → IsCompact { x : ∀ i, X i | ∀ i, x i ∈ s i } := by simp only [isCompact_iff_ultrafilter_le_nhds, nhds_pi, le_pi, le_principal_iff] intro h f hfs have : ∀ i : ι, ∃ x, x ∈ s i ∧ Tendsto (Function.eval i) f (𝓝 x) := by refine fun i => h i (f.map _) (mem_map.2 ?_) exact mem_of_superset hfs fun x hx => hx i choose x hx using this exact ⟨x, fun i => (hx i).left, fun i => (hx i).right⟩ /-- **Tychonoff's theorem** formulated using `Set.pi`: product of compact sets is compact. -/ theorem isCompact_univ_pi {s : ∀ i, Set (X i)} (h : ∀ i, IsCompact (s i)) : IsCompact (pi univ s) := by convert isCompact_pi_infinite h simp only [← mem_univ_pi, setOf_mem_eq] instance Pi.compactSpace [∀ i, CompactSpace (X i)] : CompactSpace (∀ i, X i) := ⟨by rw [← pi_univ univ]; exact isCompact_univ_pi fun i => isCompact_univ⟩ instance Function.compactSpace [CompactSpace Y] : CompactSpace (ι → Y) := Pi.compactSpace lemma Pi.isCompact_iff_of_isClosed {s : Set (Π i, X i)} (hs : IsClosed s) : IsCompact s ↔ ∀ i, IsCompact (eval i '' s) := by constructor <;> intro H · exact fun i ↦ H.image <| continuous_apply i · exact IsCompact.of_isClosed_subset (isCompact_univ_pi H) hs (subset_pi_eval_image univ s) protected lemma Pi.exists_compact_superset_iff {s : Set (Π i, X i)} : (∃ K, IsCompact K ∧ s ⊆ K) ↔ ∀ i, ∃ Ki, IsCompact Ki ∧ s ⊆ eval i ⁻¹' Ki := by constructor · intro ⟨K, hK, hsK⟩ i exact ⟨eval i '' K, hK.image <| continuous_apply i, hsK.trans <| K.subset_preimage_image _⟩ · intro H choose K hK hsK using H exact ⟨pi univ K, isCompact_univ_pi hK, fun _ hx i _ ↦ hsK i hx⟩ /-- **Tychonoff's theorem** formulated in terms of filters: `Filter.cocompact` on an indexed product type `Π d, X d` the `Filter.coprodᵢ` of filters `Filter.cocompact` on `X d`. -/ theorem Filter.coprodᵢ_cocompact {X : ι → Type*} [∀ d, TopologicalSpace (X d)] : (Filter.coprodᵢ fun d => Filter.cocompact (X d)) = Filter.cocompact (∀ d, X d) := by refine le_antisymm (iSup_le fun i => Filter.comap_cocompact_le (continuous_apply i)) ?_ refine compl_surjective.forall.2 fun s H => ?_ simp only [compl_mem_coprodᵢ, Filter.mem_cocompact, compl_subset_compl, image_subset_iff] at H ⊢ choose K hKc htK using H exact ⟨Set.pi univ K, isCompact_univ_pi hKc, fun f hf i _ => htK i hf⟩ end Tychonoff instance Quot.compactSpace {r : X → X → Prop} [CompactSpace X] : CompactSpace (Quot r) := ⟨by rw [← range_quot_mk] exact isCompact_range continuous_quot_mk⟩ instance Quotient.compactSpace {s : Setoid X} [CompactSpace X] : CompactSpace (Quotient s) := Quot.compactSpace theorem IsClosed.exists_minimal_nonempty_closed_subset [CompactSpace X] {S : Set X} (hS : IsClosed S) (hne : S.Nonempty) : ∃ V : Set X, V ⊆ S ∧ V.Nonempty ∧ IsClosed V ∧ ∀ V' : Set X, V' ⊆ V → V'.Nonempty → IsClosed V' → V' = V := by let opens := { U : Set X | Sᶜ ⊆ U ∧ IsOpen U ∧ Uᶜ.Nonempty } obtain ⟨U, h⟩ := zorn_subset opens fun c hc hz => by by_cases hcne : c.Nonempty · obtain ⟨U₀, hU₀⟩ := hcne
haveI : Nonempty { U // U ∈ c } := ⟨⟨U₀, hU₀⟩⟩ obtain ⟨U₀compl, -, -⟩ := hc hU₀
Mathlib/Topology/Compactness/Compact.lean
1,043
1,044
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Alex Meiburg -/ import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.Algebra.Polynomial.Degree.Monomial /-! # Erase the leading term of a univariate polynomial ## Definition * `eraseLead f`: the polynomial `f - leading term of f` `eraseLead` serves as reduction step in an induction, shaving off one monomial from a polynomial. The definition is set up so that it does not mention subtraction in the definition, and thus works for polynomials over semirings as well as rings. -/ noncomputable section open Polynomial open Polynomial Finset namespace Polynomial variable {R : Type*} [Semiring R] {f : R[X]} /-- `eraseLead f` for a polynomial `f` is the polynomial obtained by subtracting from `f` the leading term of `f`. -/ def eraseLead (f : R[X]) : R[X] := Polynomial.erase f.natDegree f section EraseLead theorem eraseLead_support (f : R[X]) : f.eraseLead.support = f.support.erase f.natDegree := by simp only [eraseLead, support_erase] theorem eraseLead_coeff (i : ℕ) : f.eraseLead.coeff i = if i = f.natDegree then 0 else f.coeff i := by simp only [eraseLead, coeff_erase] @[simp] theorem eraseLead_coeff_natDegree : f.eraseLead.coeff f.natDegree = 0 := by simp [eraseLead_coeff] theorem eraseLead_coeff_of_ne (i : ℕ) (hi : i ≠ f.natDegree) : f.eraseLead.coeff i = f.coeff i := by simp [eraseLead_coeff, hi]
Mathlib/Algebra/Polynomial/EraseLead.lean
52
52
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Order.Filter.AtTopBot.Finset import Mathlib.Topology.Algebra.InfiniteSum.Group import Mathlib.Topology.Algebra.Star /-! # Topological sums and functorial constructions Lemmas on the interaction of `tprod`, `tsum`, `HasProd`, `HasSum` etc with products, Sigma and Pi types, `MulOpposite`, etc. -/ noncomputable section open Filter Finset Function open scoped Topology variable {α β γ : Type*} /-! ## Product, Sigma and Pi types -/ section ProdDomain variable [CommMonoid α] [TopologicalSpace α] @[to_additive] theorem hasProd_pi_single [DecidableEq β] (b : β) (a : α) : HasProd (Pi.mulSingle b a) a := by convert hasProd_ite_eq b a simp [Pi.mulSingle_apply] @[to_additive (attr := simp)] theorem tprod_pi_single [DecidableEq β] (b : β) (a : α) : ∏' b', Pi.mulSingle b a b' = a := by rw [tprod_eq_mulSingle b] · simp · intro b' hb'; simp [hb'] @[to_additive tsum_setProd_singleton_left] lemma tprod_setProd_singleton_left (b : β) (t : Set γ) (f : β × γ → α) : (∏' x : {b} ×ˢ t, f x) = ∏' c : t, f (b, c) := by rw [tprod_congr_set_coe _ Set.singleton_prod, tprod_image _ (Prod.mk_right_injective b).injOn] @[to_additive tsum_setProd_singleton_right]
lemma tprod_setProd_singleton_right (s : Set β) (c : γ) (f : β × γ → α) : (∏' x : s ×ˢ {c}, f x) = ∏' b : s, f (b, c) := by rw [tprod_congr_set_coe _ Set.prod_singleton, tprod_image _ (Prod.mk_left_injective c).injOn]
Mathlib/Topology/Algebra/InfiniteSum/Constructions.lean
50
53
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Yaël Dillies -/ import Mathlib.Analysis.Convex.Basic import Mathlib.Order.Closure /-! # Convex hull This file defines the convex hull of a set `s` in a module. `convexHull 𝕜 s` is the smallest convex set containing `s`. In order theory speak, this is a closure operator. ## Implementation notes `convexHull` is defined as a closure operator. This gives access to the `ClosureOperator` API while the impact on writing code is minimal as `convexHull 𝕜 s` is automatically elaborated as `(convexHull 𝕜) s`. -/ open Set open Pointwise variable {𝕜 E F : Type*} section convexHull section OrderedSemiring variable [Semiring 𝕜] [PartialOrder 𝕜] section AddCommMonoid variable (𝕜) variable [AddCommMonoid E] [AddCommMonoid F] [Module 𝕜 E] [Module 𝕜 F] /-- The convex hull of a set `s` is the minimal convex set that includes `s`. -/ @[simps! isClosed] def convexHull : ClosureOperator (Set E) := .ofCompletePred (Convex 𝕜) fun _ ↦ convex_sInter variable (s : Set E) theorem subset_convexHull : s ⊆ convexHull 𝕜 s := (convexHull 𝕜).le_closure s theorem convex_convexHull : Convex 𝕜 (convexHull 𝕜 s) := (convexHull 𝕜).isClosed_closure s theorem convexHull_eq_iInter : convexHull 𝕜 s = ⋂ (t : Set E) (_ : s ⊆ t) (_ : Convex 𝕜 t), t := by simp [convexHull, iInter_subtype, iInter_and] variable {𝕜 s} {t : Set E} {x y : E} theorem mem_convexHull_iff : x ∈ convexHull 𝕜 s ↔ ∀ t, s ⊆ t → Convex 𝕜 t → x ∈ t := by simp_rw [convexHull_eq_iInter, mem_iInter] theorem convexHull_min : s ⊆ t → Convex 𝕜 t → convexHull 𝕜 s ⊆ t := (convexHull 𝕜).closure_min theorem Convex.convexHull_subset_iff (ht : Convex 𝕜 t) : convexHull 𝕜 s ⊆ t ↔ s ⊆ t := (show (convexHull 𝕜).IsClosed t from ht).closure_le_iff @[mono, gcongr] theorem convexHull_mono (hst : s ⊆ t) : convexHull 𝕜 s ⊆ convexHull 𝕜 t := (convexHull 𝕜).monotone hst lemma convexHull_eq_self : convexHull 𝕜 s = s ↔ Convex 𝕜 s := (convexHull 𝕜).isClosed_iff.symm alias ⟨_, Convex.convexHull_eq⟩ := convexHull_eq_self @[simp] theorem convexHull_univ : convexHull 𝕜 (univ : Set E) = univ := ClosureOperator.closure_top (convexHull 𝕜) @[simp] theorem convexHull_empty : convexHull 𝕜 (∅ : Set E) = ∅ := convex_empty.convexHull_eq @[simp] theorem convexHull_empty_iff : convexHull 𝕜 s = ∅ ↔ s = ∅ := by constructor · intro h rw [← Set.subset_empty_iff, ← h] exact subset_convexHull 𝕜 _ · rintro rfl exact convexHull_empty @[simp] theorem convexHull_nonempty_iff : (convexHull 𝕜 s).Nonempty ↔ s.Nonempty := by rw [nonempty_iff_ne_empty, nonempty_iff_ne_empty, Ne, Ne] exact not_congr convexHull_empty_iff protected alias ⟨_, Set.Nonempty.convexHull⟩ := convexHull_nonempty_iff theorem segment_subset_convexHull (hx : x ∈ s) (hy : y ∈ s) : segment 𝕜 x y ⊆ convexHull 𝕜 s := (convex_convexHull _ _).segment_subset (subset_convexHull _ _ hx) (subset_convexHull _ _ hy) @[simp] theorem convexHull_singleton (x : E) : convexHull 𝕜 ({x} : Set E) = {x} := (convex_singleton x).convexHull_eq @[simp] theorem convexHull_zero : convexHull 𝕜 (0 : Set E) = 0 := convexHull_singleton 0 @[simp] theorem convexHull_pair [IsOrderedRing 𝕜] (x y : E) : convexHull 𝕜 {x, y} = segment 𝕜 x y := by refine (convexHull_min ?_ <| convex_segment _ _).antisymm (segment_subset_convexHull (mem_insert _ _) <| subset_insert _ _ <| mem_singleton _) rw [insert_subset_iff, singleton_subset_iff] exact ⟨left_mem_segment _ _ _, right_mem_segment _ _ _⟩ theorem convexHull_convexHull_union_left (s t : Set E) : convexHull 𝕜 (convexHull 𝕜 s ∪ t) = convexHull 𝕜 (s ∪ t) := ClosureOperator.closure_sup_closure_left _ _ _ theorem convexHull_convexHull_union_right (s t : Set E) : convexHull 𝕜 (s ∪ convexHull 𝕜 t) = convexHull 𝕜 (s ∪ t) := ClosureOperator.closure_sup_closure_right _ _ _ theorem Convex.convex_remove_iff_not_mem_convexHull_remove {s : Set E} (hs : Convex 𝕜 s) (x : E) : Convex 𝕜 (s \ {x}) ↔ x ∉ convexHull 𝕜 (s \ {x}) := by constructor · rintro hsx hx rw [hsx.convexHull_eq] at hx
exact hx.2 (mem_singleton _) rintro hx suffices h : s \ {x} = convexHull 𝕜 (s \ {x}) by rw [h] exact convex_convexHull 𝕜 _
Mathlib/Analysis/Convex/Hull.lean
127
131
/- Copyright (c) 2021 Martin Zinkevich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Martin Zinkevich, Rémy Degenne -/ import Mathlib.Logic.Encodable.Lattice import Mathlib.MeasureTheory.MeasurableSpace.Defs import Mathlib.Order.Disjointed /-! # Induction principles for measurable sets, related to π-systems and λ-systems. ## Main statements * The main theorem of this file is Dynkin's π-λ theorem, which appears here as an induction principle `induction_on_inter`. Suppose `s` is a collection of subsets of `α` such that the intersection of two members of `s` belongs to `s` whenever it is nonempty. Let `m` be the σ-algebra generated by `s`. In order to check that a predicate `C` holds on every member of `m`, it suffices to check that `C` holds on the members of `s` and that `C` is preserved by complementation and *disjoint* countable unions. * The proof of this theorem relies on the notion of `IsPiSystem`, i.e., a collection of sets which is closed under binary non-empty intersections. Note that this is a small variation around the usual notion in the literature, which often requires that a π-system is non-empty, and closed also under disjoint intersections. This variation turns out to be convenient for the formalization. * The proof of Dynkin's π-λ theorem also requires the notion of `DynkinSystem`, i.e., a collection of sets which contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `σ`-algebras. * `generatePiSystem g` gives the minimal π-system containing `g`. This can be considered a Galois insertion into both measurable spaces and sets. * `generateFrom_generatePiSystem_eq` proves that if you start from a collection of sets `g`, take the generated π-system, and then the generated σ-algebra, you get the same result as the σ-algebra generated from `g`. This is useful because there are connections between independent sets that are π-systems and the generated independent spaces. * `mem_generatePiSystem_iUnion_elim` and `mem_generatePiSystem_iUnion_elim'` show that any element of the π-system generated from the union of a set of π-systems can be represented as the intersection of a finite number of elements from these sets. * `piiUnionInter` defines a new π-system from a family of π-systems `π : ι → Set (Set α)` and a set of indices `S : Set ι`. `piiUnionInter π S` is the set of sets that can be written as `⋂ x ∈ t, f x` for some finset `t ∈ S` and sets `f x ∈ π x`. ## Implementation details * `IsPiSystem` is a predicate, not a type. Thus, we don't explicitly define the galois insertion, nor do we define a complete lattice. In theory, we could define a complete lattice and galois insertion on the subtype corresponding to `IsPiSystem`. -/ open MeasurableSpace Set open MeasureTheory variable {α β : Type*} /-- A π-system is a collection of subsets of `α` that is closed under binary intersection of non-disjoint sets. Usually it is also required that the collection is nonempty, but we don't do that here. -/ def IsPiSystem (C : Set (Set α)) : Prop := ∀ᵉ (s ∈ C) (t ∈ C), (s ∩ t : Set α).Nonempty → s ∩ t ∈ C namespace MeasurableSpace theorem isPiSystem_measurableSet {α : Type*} [MeasurableSpace α] : IsPiSystem { s : Set α | MeasurableSet s } := fun _ hs _ ht _ => hs.inter ht end MeasurableSpace theorem IsPiSystem.singleton (S : Set α) : IsPiSystem ({S} : Set (Set α)) := by intro s h_s t h_t _ rw [Set.mem_singleton_iff.1 h_s, Set.mem_singleton_iff.1 h_t, Set.inter_self, Set.mem_singleton_iff] theorem IsPiSystem.insert_empty {S : Set (Set α)} (h_pi : IsPiSystem S) : IsPiSystem (insert ∅ S) := by intro s hs t ht hst rcases hs with hs | hs · simp [hs] · rcases ht with ht | ht · simp [ht] · exact Set.mem_insert_of_mem _ (h_pi s hs t ht hst) theorem IsPiSystem.insert_univ {S : Set (Set α)} (h_pi : IsPiSystem S) : IsPiSystem (insert Set.univ S) := by intro s hs t ht hst rcases hs with hs | hs · rcases ht with ht | ht <;> simp [hs, ht] · rcases ht with ht | ht · simp [hs, ht] · exact Set.mem_insert_of_mem _ (h_pi s hs t ht hst) theorem IsPiSystem.comap {α β} {S : Set (Set β)} (h_pi : IsPiSystem S) (f : α → β) : IsPiSystem { s : Set α | ∃ t ∈ S, f ⁻¹' t = s } := by rintro _ ⟨s, hs_mem, rfl⟩ _ ⟨t, ht_mem, rfl⟩ hst rw [← Set.preimage_inter] at hst ⊢ exact ⟨s ∩ t, h_pi s hs_mem t ht_mem (nonempty_of_nonempty_preimage hst), rfl⟩ theorem isPiSystem_iUnion_of_directed_le {α ι} (p : ι → Set (Set α)) (hp_pi : ∀ n, IsPiSystem (p n)) (hp_directed : Directed (· ≤ ·) p) : IsPiSystem (⋃ n, p n) := by intro t1 ht1 t2 ht2 h rw [Set.mem_iUnion] at ht1 ht2 ⊢ obtain ⟨n, ht1⟩ := ht1 obtain ⟨m, ht2⟩ := ht2 obtain ⟨k, hpnk, hpmk⟩ : ∃ k, p n ≤ p k ∧ p m ≤ p k := hp_directed n m exact ⟨k, hp_pi k t1 (hpnk ht1) t2 (hpmk ht2) h⟩ theorem isPiSystem_iUnion_of_monotone {α ι} [SemilatticeSup ι] (p : ι → Set (Set α)) (hp_pi : ∀ n, IsPiSystem (p n)) (hp_mono : Monotone p) : IsPiSystem (⋃ n, p n) := isPiSystem_iUnion_of_directed_le p hp_pi (Monotone.directed_le hp_mono) /-- Rectangles formed by π-systems form a π-system. -/ lemma IsPiSystem.prod {C : Set (Set α)} {D : Set (Set β)} (hC : IsPiSystem C) (hD : IsPiSystem D) : IsPiSystem (image2 (· ×ˢ ·) C D) := by rintro _ ⟨s₁, hs₁, t₁, ht₁, rfl⟩ _ ⟨s₂, hs₂, t₂, ht₂, rfl⟩ hst rw [prod_inter_prod] at hst ⊢; rw [prod_nonempty_iff] at hst exact mem_image2_of_mem (hC _ hs₁ _ hs₂ hst.1) (hD _ ht₁ _ ht₂ hst.2) section Order variable {ι ι' : Sort*} [LinearOrder α] theorem isPiSystem_image_Iio (s : Set α) : IsPiSystem (Iio '' s) := by rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ - exact ⟨a ⊓ b, inf_ind a b ha hb, Iio_inter_Iio.symm⟩ theorem isPiSystem_Iio : IsPiSystem (range Iio : Set (Set α)) := @image_univ α _ Iio ▸ isPiSystem_image_Iio univ theorem isPiSystem_image_Ioi (s : Set α) : IsPiSystem (Ioi '' s) := @isPiSystem_image_Iio αᵒᵈ _ s theorem isPiSystem_Ioi : IsPiSystem (range Ioi : Set (Set α)) := @image_univ α _ Ioi ▸ isPiSystem_image_Ioi univ theorem isPiSystem_image_Iic (s : Set α) : IsPiSystem (Iic '' s) := by rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ - exact ⟨a ⊓ b, inf_ind a b ha hb, Iic_inter_Iic.symm⟩ theorem isPiSystem_Iic : IsPiSystem (range Iic : Set (Set α)) := @image_univ α _ Iic ▸ isPiSystem_image_Iic univ theorem isPiSystem_image_Ici (s : Set α) : IsPiSystem (Ici '' s) := @isPiSystem_image_Iic αᵒᵈ _ s theorem isPiSystem_Ici : IsPiSystem (range Ici : Set (Set α)) := @image_univ α _ Ici ▸ isPiSystem_image_Ici univ theorem isPiSystem_Ixx_mem {Ixx : α → α → Set α} {p : α → α → Prop} (Hne : ∀ {a b}, (Ixx a b).Nonempty → p a b) (Hi : ∀ {a₁ b₁ a₂ b₂}, Ixx a₁ b₁ ∩ Ixx a₂ b₂ = Ixx (max a₁ a₂) (min b₁ b₂)) (s t : Set α) : IsPiSystem { S | ∃ᵉ (l ∈ s) (u ∈ t), p l u ∧ Ixx l u = S } := by rintro _ ⟨l₁, hls₁, u₁, hut₁, _, rfl⟩ _ ⟨l₂, hls₂, u₂, hut₂, _, rfl⟩ simp only [Hi] exact fun H => ⟨l₁ ⊔ l₂, sup_ind l₁ l₂ hls₁ hls₂, u₁ ⊓ u₂, inf_ind u₁ u₂ hut₁ hut₂, Hne H, rfl⟩
theorem isPiSystem_Ixx {Ixx : α → α → Set α} {p : α → α → Prop} (Hne : ∀ {a b}, (Ixx a b).Nonempty → p a b) (Hi : ∀ {a₁ b₁ a₂ b₂}, Ixx a₁ b₁ ∩ Ixx a₂ b₂ = Ixx (max a₁ a₂) (min b₁ b₂)) (f : ι → α) (g : ι' → α) : @IsPiSystem α { S | ∃ i j, p (f i) (g j) ∧ Ixx (f i) (g j) = S } := by simpa only [exists_range_iff] using isPiSystem_Ixx_mem (@Hne) (@Hi) (range f) (range g)
Mathlib/MeasureTheory/PiSystem.lean
164
170
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import Mathlib.Tactic.Attr.Register import Mathlib.Tactic.Basic import Batteries.Logic import Batteries.Tactic.Trans import Batteries.Util.LibraryNote import Mathlib.Data.Nat.Notation import Mathlib.Data.Int.Notation /-! # Basic logic properties This file is one of the earliest imports in mathlib. ## Implementation notes Theorems that require decidability hypotheses are in the namespace `Decidable`. Classical versions are in the namespace `Classical`. -/ open Function section Miscellany -- attribute [refl] HEq.refl -- FIXME This is still rejected after https://github.com/leanprover-community/mathlib4/pull/857 attribute [trans] Iff.trans HEq.trans heq_of_eq_of_heq attribute [simp] cast_heq /-- An identity function with its main argument implicit. This will be printed as `hidden` even if it is applied to a large term, so it can be used for elision, as done in the `elide` and `unelide` tactics. -/ abbrev hidden {α : Sort*} {a : α} := a variable {α : Sort*} instance (priority := 10) decidableEq_of_subsingleton [Subsingleton α] : DecidableEq α := fun a b ↦ isTrue (Subsingleton.elim a b) instance [Subsingleton α] (p : α → Prop) : Subsingleton (Subtype p) := ⟨fun ⟨x, _⟩ ⟨y, _⟩ ↦ by cases Subsingleton.elim x y; rfl⟩ theorem congr_heq {α β γ : Sort _} {f : α → γ} {g : β → γ} {x : α} {y : β} (h₁ : HEq f g) (h₂ : HEq x y) : f x = g y := by cases h₂; cases h₁; rfl theorem congr_arg_heq {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → HEq (f a₁) (f a₂) | _, _, rfl => HEq.rfl @[simp] theorem eq_iff_eq_cancel_left {b c : α} : (∀ {a}, a = b ↔ a = c) ↔ b = c := ⟨fun h ↦ by rw [← h], fun h a ↦ by rw [h]⟩ @[simp] theorem eq_iff_eq_cancel_right {a b : α} : (∀ {c}, a = c ↔ b = c) ↔ a = b := ⟨fun h ↦ by rw [h], fun h a ↦ by rw [h]⟩ lemma ne_and_eq_iff_right {a b c : α} (h : b ≠ c) : a ≠ b ∧ a = c ↔ a = c := and_iff_right_of_imp (fun h2 => h2.symm ▸ h.symm) /-- Wrapper for adding elementary propositions to the type class systems. Warning: this can easily be abused. See the rest of this docstring for details. Certain propositions should not be treated as a class globally, but sometimes it is very convenient to be able to use the type class system in specific circumstances. For example, `ZMod p` is a field if and only if `p` is a prime number. In order to be able to find this field instance automatically by type class search, we have to turn `p.prime` into an instance implicit assumption. On the other hand, making `Nat.prime` a class would require a major refactoring of the library, and it is questionable whether making `Nat.prime` a class is desirable at all. The compromise is to add the assumption `[Fact p.prime]` to `ZMod.field`. In particular, this class is not intended for turning the type class system into an automated theorem prover for first order logic. -/ class Fact (p : Prop) : Prop where /-- `Fact.out` contains the unwrapped witness for the fact represented by the instance of `Fact p`. -/ out : p library_note "fact non-instances"/-- In most cases, we should not have global instances of `Fact`; typeclass search only reads the head symbol and then tries any instances, which means that adding any such instance will cause slowdowns everywhere. We instead make them as lemmata and make them local instances as required. -/ theorem Fact.elim {p : Prop} (h : Fact p) : p := h.1 theorem fact_iff {p : Prop} : Fact p ↔ p := ⟨fun h ↦ h.1, fun h ↦ ⟨h⟩⟩ instance {p : Prop} [Decidable p] : Decidable (Fact p) := decidable_of_iff _ fact_iff.symm /-- Swaps two pairs of arguments to a function. -/ abbrev Function.swap₂ {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*} {φ : ∀ i₁, κ₁ i₁ → ∀ i₂, κ₂ i₂ → Sort*} (f : ∀ i₁ j₁ i₂ j₂, φ i₁ j₁ i₂ j₂) (i₂ j₂ i₁ j₁) : φ i₁ j₁ i₂ j₂ := f i₁ j₁ i₂ j₂ end Miscellany open Function /-! ### Declarations about propositional connectives -/ section Propositional /-! ### Declarations about `implies` -/ alias Iff.imp := imp_congr -- This is a duplicate of `Classical.imp_iff_right_iff`. Deprecate? theorem imp_iff_right_iff {a b : Prop} : (a → b ↔ b) ↔ a ∨ b := open scoped Classical in Decidable.imp_iff_right_iff -- This is a duplicate of `Classical.and_or_imp`. Deprecate? theorem and_or_imp {a b c : Prop} : a ∧ b ∨ (a → c) ↔ a → b ∨ c := open scoped Classical in Decidable.and_or_imp /-- Provide modus tollens (`mt`) as dot notation for implications. -/ protected theorem Function.mt {a b : Prop} : (a → b) → ¬b → ¬a := mt /-! ### Declarations about `not` -/ alias dec_em := Decidable.em theorem dec_em' (p : Prop) [Decidable p] : ¬p ∨ p := (dec_em p).symm alias em := Classical.em theorem em' (p : Prop) : ¬p ∨ p := (em p).symm theorem or_not {p : Prop} : p ∨ ¬p := em _ theorem Decidable.eq_or_ne {α : Sort*} (x y : α) [Decidable (x = y)] : x = y ∨ x ≠ y := dec_em <| x = y theorem Decidable.ne_or_eq {α : Sort*} (x y : α) [Decidable (x = y)] : x ≠ y ∨ x = y := dec_em' <| x = y theorem eq_or_ne {α : Sort*} (x y : α) : x = y ∨ x ≠ y := em <| x = y theorem ne_or_eq {α : Sort*} (x y : α) : x ≠ y ∨ x = y := em' <| x = y theorem by_contradiction {p : Prop} : (¬p → False) → p := open scoped Classical in Decidable.byContradiction theorem by_cases {p q : Prop} (hpq : p → q) (hnpq : ¬p → q) : q := open scoped Classical in if hp : p then hpq hp else hnpq hp alias by_contra := by_contradiction library_note "decidable namespace"/-- In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely. The `Decidable` namespace contains versions of lemmas from the root namespace that explicitly attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs. You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if `Classical.choice` appears in the list. -/ library_note "decidable arguments"/-- As mathlib is primarily classical, if the type signature of a `def` or `lemma` does not require any `Decidable` instances to state, it is preferable not to introduce any `Decidable` instances that are needed in the proof as arguments, but rather to use the `classical` tactic as needed. In the other direction, when `Decidable` instances do appear in the type signature, it is better to use explicitly introduced ones rather than allowing Lean to automatically infer classical ones, as these may cause instance mismatch errors later. -/ export Classical (not_not) attribute [simp] not_not variable {a b : Prop} theorem of_not_not {a : Prop} : ¬¬a → a := by_contra theorem not_ne_iff {α : Sort*} {a b : α} : ¬a ≠ b ↔ a = b := not_not theorem of_not_imp : ¬(a → b) → a := open scoped Classical in Decidable.of_not_imp alias Not.decidable_imp_symm := Decidable.not_imp_symm theorem Not.imp_symm : (¬a → b) → ¬b → a := open scoped Classical in Not.decidable_imp_symm theorem not_imp_comm : ¬a → b ↔ ¬b → a := open scoped Classical in Decidable.not_imp_comm @[simp] theorem not_imp_self : ¬a → a ↔ a := open scoped Classical in Decidable.not_imp_self theorem Imp.swap {a b : Sort*} {c : Prop} : a → b → c ↔ b → a → c := ⟨fun h x y ↦ h y x, fun h x y ↦ h y x⟩ alias Iff.not := not_congr theorem Iff.not_left (h : a ↔ ¬b) : ¬a ↔ b := h.not.trans not_not theorem Iff.not_right (h : ¬a ↔ b) : a ↔ ¬b := not_not.symm.trans h.not protected lemma Iff.ne {α β : Sort*} {a b : α} {c d : β} : (a = b ↔ c = d) → (a ≠ b ↔ c ≠ d) := Iff.not lemma Iff.ne_left {α β : Sort*} {a b : α} {c d : β} : (a = b ↔ c ≠ d) → (a ≠ b ↔ c = d) := Iff.not_left lemma Iff.ne_right {α β : Sort*} {a b : α} {c d : β} : (a ≠ b ↔ c = d) → (a = b ↔ c ≠ d) := Iff.not_right /-! ### Declarations about `Xor'` -/ /-- `Xor' a b` is the exclusive-or of propositions. -/ def Xor' (a b : Prop) := (a ∧ ¬b) ∨ (b ∧ ¬a) instance [Decidable a] [Decidable b] : Decidable (Xor' a b) := inferInstanceAs (Decidable (Or ..)) @[simp] theorem xor_true : Xor' True = Not := by simp +unfoldPartialApp [Xor'] @[simp] theorem xor_false : Xor' False = id := by ext; simp [Xor'] theorem xor_comm (a b : Prop) : Xor' a b = Xor' b a := by simp [Xor', and_comm, or_comm] instance : Std.Commutative Xor' := ⟨xor_comm⟩ @[simp] theorem xor_self (a : Prop) : Xor' a a = False := by simp [Xor'] @[simp] theorem xor_not_left : Xor' (¬a) b ↔ (a ↔ b) := by by_cases a <;> simp [*] @[simp] theorem xor_not_right : Xor' a (¬b) ↔ (a ↔ b) := by by_cases a <;> simp [*] theorem xor_not_not : Xor' (¬a) (¬b) ↔ Xor' a b := by simp [Xor', or_comm, and_comm] protected theorem Xor'.or (h : Xor' a b) : a ∨ b := h.imp And.left And.left /-! ### Declarations about `and` -/ alias Iff.and := and_congr alias ⟨And.rotate, _⟩ := and_rotate theorem and_symm_right {α : Sort*} (a b : α) (p : Prop) : p ∧ a = b ↔ p ∧ b = a := by simp [eq_comm] theorem and_symm_left {α : Sort*} (a b : α) (p : Prop) : a = b ∧ p ↔ b = a ∧ p := by simp [eq_comm] /-! ### Declarations about `or` -/ alias Iff.or := or_congr alias ⟨Or.rotate, _⟩ := or_rotate theorem Or.elim3 {c d : Prop} (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := Or.elim h ha fun h₂ ↦ Or.elim h₂ hb hc theorem Or.imp3 {d e c f : Prop} (had : a → d) (hbe : b → e) (hcf : c → f) : a ∨ b ∨ c → d ∨ e ∨ f := Or.imp had <| Or.imp hbe hcf export Classical (or_iff_not_imp_left or_iff_not_imp_right) theorem not_or_of_imp : (a → b) → ¬a ∨ b := open scoped Classical in Decidable.not_or_of_imp -- See Note [decidable namespace] protected theorem Decidable.or_not_of_imp [Decidable a] (h : a → b) : b ∨ ¬a := dite _ (Or.inl ∘ h) Or.inr theorem or_not_of_imp : (a → b) → b ∨ ¬a := open scoped Classical in Decidable.or_not_of_imp theorem imp_iff_not_or : a → b ↔ ¬a ∨ b := open scoped Classical in Decidable.imp_iff_not_or theorem imp_iff_or_not {b a : Prop} : b → a ↔ a ∨ ¬b := open scoped Classical in Decidable.imp_iff_or_not theorem not_imp_not : ¬a → ¬b ↔ b → a := open scoped Classical in Decidable.not_imp_not theorem imp_and_neg_imp_iff (p q : Prop) : (p → q) ∧ (¬p → q) ↔ q := by simp /-- Provide the reverse of modus tollens (`mt`) as dot notation for implications. -/ protected theorem Function.mtr : (¬a → ¬b) → b → a := not_imp_not.mp theorem or_congr_left' {c a b : Prop} (h : ¬c → (a ↔ b)) : a ∨ c ↔ b ∨ c := open scoped Classical in Decidable.or_congr_left' h theorem or_congr_right' {c : Prop} (h : ¬a → (b ↔ c)) : a ∨ b ↔ a ∨ c := open scoped Classical in Decidable.or_congr_right' h /-! ### Declarations about distributivity -/ /-! Declarations about `iff` -/ alias Iff.iff := iff_congr -- @[simp] -- FIXME simp ignores proof rewrites theorem iff_mpr_iff_true_intro {P : Prop} (h : P) : Iff.mpr (iff_true_intro h) True.intro = h := rfl theorem imp_or {a b c : Prop} : a → b ∨ c ↔ (a → b) ∨ (a → c) := open scoped Classical in Decidable.imp_or theorem imp_or' {a : Sort*} {b c : Prop} : a → b ∨ c ↔ (a → b) ∨ (a → c) := open scoped Classical in Decidable.imp_or'
theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := open scoped Classical in Decidable.not_imp_iff_and_not
Mathlib/Logic/Basic.lean
302
303
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Jakob von Raumer -/ import Mathlib.CategoryTheory.Limits.Shapes.FiniteProducts import Mathlib.CategoryTheory.Limits.Shapes.Kernels /-! # Biproducts and binary biproducts We introduce the notion of (finite) biproducts. Binary biproducts are defined in `CategoryTheory.Limits.Shapes.BinaryBiproducts`. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) For results about biproducts in preadditive categories see `CategoryTheory.Preadditive.Biproducts`. For biproducts indexed by a `Fintype J`, a `bicone` consists of a cone point `X` and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. ## Implementation notes Prior to https://github.com/leanprover-community/mathlib3/pull/14046, `HasFiniteBiproducts` required a `DecidableEq` instance on the indexing type. As this had no pay-off (everything about limits is non-constructive in mathlib), and occasional cost (constructing decidability instances appropriate for constructions involving the indexing type), we made everything classical. -/ noncomputable section universe w w' v u open CategoryTheory Functor namespace CategoryTheory.Limits variable {J : Type w} universe uC' uC uD' uD variable {C : Type uC} [Category.{uC'} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] open scoped Classical in /-- A `c : Bicone F` is: * an object `c.pt` and * morphisms `π j : pt ⟶ F j` and `ι j : F j ⟶ pt` for each `j`, * such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. -/ structure Bicone (F : J → C) where pt : C π : ∀ j, pt ⟶ F j ι : ∀ j, F j ⟶ pt ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eqToHom (congrArg F h) else 0 := by aesop attribute [inherit_doc Bicone] Bicone.pt Bicone.π Bicone.ι Bicone.ι_π @[reassoc (attr := simp)] theorem bicone_ι_π_self {F : J → C} (B : Bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) := by simpa using B.ι_π j j @[reassoc (attr := simp)] theorem bicone_ι_π_ne {F : J → C} (B : Bicone F) {j j' : J} (h : j ≠ j') : B.ι j ≫ B.π j' = 0 := by simpa [h] using B.ι_π j j' variable {F : J → C} /-- A bicone morphism between two bicones for the same diagram is a morphism of the bicone points which commutes with the cone and cocone legs. -/ structure BiconeMorphism {F : J → C} (A B : Bicone F) where /-- A morphism between the two vertex objects of the bicones -/ hom : A.pt ⟶ B.pt /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wπ : ∀ j : J, hom ≫ B.π j = A.π j := by aesop_cat /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wι : ∀ j : J, A.ι j ≫ hom = B.ι j := by aesop_cat attribute [reassoc (attr := simp)] BiconeMorphism.wι BiconeMorphism.wπ /-- The category of bicones on a given diagram. -/ @[simps] instance Bicone.category : Category (Bicone F) where Hom A B := BiconeMorphism A B comp f g := { hom := f.hom ≫ g.hom } id B := { hom := 𝟙 B.pt } -- Porting note: if we do not have `simps` automatically generate the lemma for simplifying -- the `hom` field of a category, we need to write the `ext` lemma in terms of the categorical -- morphism, rather than the underlying structure. @[ext] theorem BiconeMorphism.ext {c c' : Bicone F} (f g : c ⟶ c') (w : f.hom = g.hom) : f = g := by cases f cases g congr namespace Bicones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[aesop apply safe (rule_sets := [CategoryTheory]), simps] def ext {c c' : Bicone F} (φ : c.pt ≅ c'.pt) (wι : ∀ j, c.ι j ≫ φ.hom = c'.ι j := by aesop_cat) (wπ : ∀ j, φ.hom ≫ c'.π j = c.π j := by aesop_cat) : c ≅ c' where hom := { hom := φ.hom } inv := { hom := φ.inv wι := fun j => φ.comp_inv_eq.mpr (wι j).symm wπ := fun j => φ.inv_comp_eq.mpr (wπ j).symm } variable (F) in /-- A functor `G : C ⥤ D` sends bicones over `F` to bicones over `G.obj ∘ F` functorially. -/ @[simps] def functoriality (G : C ⥤ D) [Functor.PreservesZeroMorphisms G] : Bicone F ⥤ Bicone (G.obj ∘ F) where obj A := { pt := G.obj A.pt π := fun j => G.map (A.π j) ι := fun j => G.map (A.ι j) ι_π := fun i j => (Functor.map_comp _ _ _).symm.trans <| by rw [A.ι_π] aesop_cat } map f := { hom := G.map f.hom wπ := fun j => by simp [-BiconeMorphism.wπ, ← f.wπ j] wι := fun j => by simp [-BiconeMorphism.wι, ← f.wι j] } variable (G : C ⥤ D) instance functoriality_full [G.PreservesZeroMorphisms] [G.Full] [G.Faithful] : (functoriality F G).Full where map_surjective t := ⟨{ hom := G.preimage t.hom wι := fun j => G.map_injective (by simpa using t.wι j) wπ := fun j => G.map_injective (by simpa using t.wπ j) }, by aesop_cat⟩ instance functoriality_faithful [G.PreservesZeroMorphisms] [G.Faithful] : (functoriality F G).Faithful where map_injective {_X} {_Y} f g h := BiconeMorphism.ext f g <| G.map_injective <| congr_arg BiconeMorphism.hom h end Bicones namespace Bicone attribute [local aesop safe tactic (rule_sets := [CategoryTheory])] CategoryTheory.Discrete.discreteCases -- Porting note: would it be okay to use this more generally? attribute [local aesop safe cases (rule_sets := [CategoryTheory])] Eq /-- Extract the cone from a bicone. -/ def toConeFunctor : Bicone F ⥤ Cone (Discrete.functor F) where obj B := { pt := B.pt, π := { app := fun j => B.π j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wπ _ } /-- A shorthand for `toConeFunctor.obj` -/ abbrev toCone (B : Bicone F) : Cone (Discrete.functor F) := toConeFunctor.obj B -- TODO Consider changing this API to `toFan (B : Bicone F) : Fan F`. @[simp] theorem toCone_pt (B : Bicone F) : B.toCone.pt = B.pt := rfl @[simp] theorem toCone_π_app (B : Bicone F) (j : Discrete J) : B.toCone.π.app j = B.π j.as := rfl theorem toCone_π_app_mk (B : Bicone F) (j : J) : B.toCone.π.app ⟨j⟩ = B.π j := rfl @[simp] theorem toCone_proj (B : Bicone F) (j : J) : Fan.proj B.toCone j = B.π j := rfl /-- Extract the cocone from a bicone. -/ def toCoconeFunctor : Bicone F ⥤ Cocone (Discrete.functor F) where obj B := { pt := B.pt, ι := { app := fun j => B.ι j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wι _ } /-- A shorthand for `toCoconeFunctor.obj` -/ abbrev toCocone (B : Bicone F) : Cocone (Discrete.functor F) := toCoconeFunctor.obj B @[simp] theorem toCocone_pt (B : Bicone F) : B.toCocone.pt = B.pt := rfl @[simp] theorem toCocone_ι_app (B : Bicone F) (j : Discrete J) : B.toCocone.ι.app j = B.ι j.as := rfl @[simp] theorem toCocone_inj (B : Bicone F) (j : J) : Cofan.inj B.toCocone j = B.ι j := rfl theorem toCocone_ι_app_mk (B : Bicone F) (j : J) : B.toCocone.ι.app ⟨j⟩ = B.ι j := rfl open scoped Classical in /-- We can turn any limit cone over a discrete collection of objects into a bicone. -/ @[simps] def ofLimitCone {f : J → C} {t : Cone (Discrete.functor f)} (ht : IsLimit t) : Bicone f where pt := t.pt π j := t.π.app ⟨j⟩ ι j := ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) ι_π j j' := by simp open scoped Classical in theorem ι_of_isLimit {f : J → C} {t : Bicone f} (ht : IsLimit t.toCone) (j : J) : t.ι j = ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] open scoped Classical in /-- We can turn any colimit cocone over a discrete collection of objects into a bicone. -/ @[simps] def ofColimitCocone {f : J → C} {t : Cocone (Discrete.functor f)} (ht : IsColimit t) : Bicone f where pt := t.pt π j := ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) ι j := t.ι.app ⟨j⟩ ι_π j j' := by simp open scoped Classical in theorem π_of_isColimit {f : J → C} {t : Bicone f} (ht : IsColimit t.toCocone) (j : J) : t.π j = ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] /-- Structure witnessing that a bicone is both a limit cone and a colimit cocone. -/ structure IsBilimit {F : J → C} (B : Bicone F) where isLimit : IsLimit B.toCone isColimit : IsColimit B.toCocone attribute [inherit_doc IsBilimit] IsBilimit.isLimit IsBilimit.isColimit attribute [simp] IsBilimit.mk.injEq attribute [local ext] Bicone.IsBilimit instance subsingleton_isBilimit {f : J → C} {c : Bicone f} : Subsingleton c.IsBilimit := ⟨fun _ _ => Bicone.IsBilimit.ext (Subsingleton.elim _ _) (Subsingleton.elim _ _)⟩ section Whisker variable {K : Type w'} /-- Whisker a bicone with an equivalence between the indexing types. -/ @[simps] def whisker {f : J → C} (c : Bicone f) (g : K ≃ J) : Bicone (f ∘ g) where pt := c.pt π k := c.π (g k) ι k := c.ι (g k) ι_π k k' := by simp only [c.ι_π] split_ifs with h h' h' <;> simp [Equiv.apply_eq_iff_eq g] at h h' <;> tauto /-- Taking the cone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cone and postcomposing with a suitable isomorphism. -/ def whiskerToCone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCone ≅ (Cones.postcompose (Discrete.functorComp f g).inv).obj (c.toCone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cones.ext (Iso.refl _) (by simp) /-- Taking the cocone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cocone and precomposing with a suitable isomorphism. -/ def whiskerToCocone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCocone ≅ (Cocones.precompose (Discrete.functorComp f g).hom).obj (c.toCocone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cocones.ext (Iso.refl _) (by simp) /-- Whiskering a bicone with an equivalence between types preserves being a bilimit bicone. -/ noncomputable def whiskerIsBilimitIff {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).IsBilimit ≃ c.IsBilimit := by refine equivOfSubsingletonOfSubsingleton (fun hc => ⟨?_, ?_⟩) fun hc => ⟨?_, ?_⟩ · let this := IsLimit.ofIsoLimit hc.isLimit (Bicone.whiskerToCone c g) let this := (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _) this exact IsLimit.ofWhiskerEquivalence (Discrete.equivalence g) this · let this := IsColimit.ofIsoColimit hc.isColimit (Bicone.whiskerToCocone c g) let this := (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _) this exact IsColimit.ofWhiskerEquivalence (Discrete.equivalence g) this · apply IsLimit.ofIsoLimit _ (Bicone.whiskerToCone c g).symm apply (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _).symm _ exact IsLimit.whiskerEquivalence hc.isLimit (Discrete.equivalence g) · apply IsColimit.ofIsoColimit _ (Bicone.whiskerToCocone c g).symm apply (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _).symm _ exact IsColimit.whiskerEquivalence hc.isColimit (Discrete.equivalence g) end Whisker end Bicone /-- A bicone over `F : J → C`, which is both a limit cone and a colimit cocone. -/ structure LimitBicone (F : J → C) where bicone : Bicone F isBilimit : bicone.IsBilimit attribute [inherit_doc LimitBicone] LimitBicone.bicone LimitBicone.isBilimit /-- `HasBiproduct F` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `F`. -/ class HasBiproduct (F : J → C) : Prop where mk' :: exists_biproduct : Nonempty (LimitBicone F) attribute [inherit_doc HasBiproduct] HasBiproduct.exists_biproduct theorem HasBiproduct.mk {F : J → C} (d : LimitBicone F) : HasBiproduct F := ⟨Nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `BiproductData F` from `HasBiproduct F`. -/ def getBiproductData (F : J → C) [HasBiproduct F] : LimitBicone F := Classical.choice HasBiproduct.exists_biproduct /-- A bicone for `F` which is both a limit cone and a colimit cocone. -/ def biproduct.bicone (F : J → C) [HasBiproduct F] : Bicone F := (getBiproductData F).bicone /-- `biproduct.bicone F` is a bilimit bicone. -/ def biproduct.isBilimit (F : J → C) [HasBiproduct F] : (biproduct.bicone F).IsBilimit := (getBiproductData F).isBilimit /-- `biproduct.bicone F` is a limit cone. -/ def biproduct.isLimit (F : J → C) [HasBiproduct F] : IsLimit (biproduct.bicone F).toCone := (getBiproductData F).isBilimit.isLimit /-- `biproduct.bicone F` is a colimit cocone. -/ def biproduct.isColimit (F : J → C) [HasBiproduct F] : IsColimit (biproduct.bicone F).toCocone := (getBiproductData F).isBilimit.isColimit instance (priority := 100) hasProduct_of_hasBiproduct [HasBiproduct F] : HasProduct F := HasLimit.mk { cone := (biproduct.bicone F).toCone isLimit := biproduct.isLimit F } instance (priority := 100) hasCoproduct_of_hasBiproduct [HasBiproduct F] : HasCoproduct F := HasColimit.mk { cocone := (biproduct.bicone F).toCocone isColimit := biproduct.isColimit F } variable (J C) /-- `C` has biproducts of shape `J` if we have a limit and a colimit, with the same cone points, of every function `F : J → C`. -/ class HasBiproductsOfShape : Prop where has_biproduct : ∀ F : J → C, HasBiproduct F attribute [instance 100] HasBiproductsOfShape.has_biproduct /-- `HasFiniteBiproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type. -/ class HasFiniteBiproducts : Prop where out : ∀ n, HasBiproductsOfShape (Fin n) C attribute [inherit_doc HasFiniteBiproducts] HasFiniteBiproducts.out variable {J} theorem hasBiproductsOfShape_of_equiv {K : Type w'} [HasBiproductsOfShape K C] (e : J ≃ K) : HasBiproductsOfShape J C := ⟨fun F => let ⟨⟨h⟩⟩ := HasBiproductsOfShape.has_biproduct (F ∘ e.symm) let ⟨c, hc⟩ := h HasBiproduct.mk <| by simpa only [Function.comp_def, e.symm_apply_apply] using LimitBicone.mk (c.whisker e) ((c.whiskerIsBilimitIff _).2 hc)⟩ instance (priority := 100) hasBiproductsOfShape_finite [HasFiniteBiproducts C] [Finite J] : HasBiproductsOfShape J C := by rcases Finite.exists_equiv_fin J with ⟨n, ⟨e⟩⟩ haveI : HasBiproductsOfShape (Fin n) C := HasFiniteBiproducts.out n exact hasBiproductsOfShape_of_equiv C e instance (priority := 100) hasFiniteProducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteProducts C where out _ := ⟨fun _ => hasLimit_of_iso Discrete.natIsoFunctor.symm⟩ instance (priority := 100) hasFiniteCoproducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteCoproducts C where out _ := ⟨fun _ => hasColimit_of_iso Discrete.natIsoFunctor⟩ instance (priority := 100) hasProductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasProductsOfShape J C where has_limit _ := hasLimit_of_iso Discrete.natIsoFunctor.symm instance (priority := 100) hasCoproductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasCoproductsOfShape J C where has_colimit _ := hasColimit_of_iso Discrete.natIsoFunctor variable {C} /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproductIso (F : J → C) [HasBiproduct F] : Limits.piObj F ≅ Limits.sigmaObj F := (IsLimit.conePointUniqueUpToIso (limit.isLimit _) (biproduct.isLimit F)).trans <| IsColimit.coconePointUniqueUpToIso (biproduct.isColimit F) (colimit.isColimit _) variable {J : Type w} {K : Type*} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (Discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbrev biproduct (f : J → C) [HasBiproduct f] : C := (biproduct.bicone f).pt @[inherit_doc biproduct] notation "⨁ " f:20 => biproduct f /-- The projection onto a summand of a biproduct. -/ abbrev biproduct.π (f : J → C) [HasBiproduct f] (b : J) : ⨁ f ⟶ f b := (biproduct.bicone f).π b @[simp] theorem biproduct.bicone_π (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).π b = biproduct.π f b := rfl /-- The inclusion into a summand of a biproduct. -/ abbrev biproduct.ι (f : J → C) [HasBiproduct f] (b : J) : f b ⟶ ⨁ f := (biproduct.bicone f).ι b @[simp] theorem biproduct.bicone_ι (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).ι b = biproduct.ι f b := rfl /-- Note that as this lemma has an `if` in the statement, we include a `DecidableEq` argument. This means you may not be able to `simp` using this lemma unless you `open scoped Classical`. -/ @[reassoc] theorem biproduct.ι_π [DecidableEq J] (f : J → C) [HasBiproduct f] (j j' : J) : biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eqToHom (congr_arg f h) else 0 := by convert (biproduct.bicone f).ι_π j j' @[reassoc] -- Porting note: both versions proven by simp theorem biproduct.ι_π_self (f : J → C) [HasBiproduct f] (j : J) : biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ := by simp [biproduct.ι_π] @[reassoc (attr := simp)] theorem biproduct.ι_π_ne (f : J → C) [HasBiproduct f] {j j' : J} (h : j ≠ j') : biproduct.ι f j ≫ biproduct.π f j' = 0 := by simp [biproduct.ι_π, h] -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.eqToHom_comp_ι (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : eqToHom (by simp [w]) ≫ biproduct.ι f j' = biproduct.ι f j := by cases w simp -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.π_comp_eqToHom (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : biproduct.π f j ≫ eqToHom (by simp [w]) = biproduct.π f j' := by cases w simp /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbrev biproduct.lift {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) : P ⟶ ⨁ f := (biproduct.isLimit f).lift (Fan.mk P p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbrev biproduct.desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) : ⨁ f ⟶ P := (biproduct.isColimit f).desc (Cofan.mk P p) @[reassoc (attr := simp)] theorem biproduct.lift_π {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) (j : J) : biproduct.lift p ≫ biproduct.π f j = p j := (biproduct.isLimit f).fac _ ⟨j⟩ @[reassoc (attr := simp)] theorem biproduct.ι_desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) (j : J) : biproduct.ι f j ≫ biproduct.desc p = p j := (biproduct.isColimit f).fac _ ⟨j⟩ /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map between the biproducts. -/ abbrev biproduct.map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsLimit.map (biproduct.bicone f).toCone (biproduct.isLimit g) (Discrete.natTrans (fun j => p j.as)) /-- An alternative to `biproduct.map` constructed via colimits. This construction only exists in order to show it is equal to `biproduct.map`. -/ abbrev biproduct.map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsColimit.map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) -- We put this at slightly higher priority than `biproduct.hom_ext'`, -- to get the matrix indices in the "right" order. @[ext 1001] theorem biproduct.hom_ext {f : J → C} [HasBiproduct f] {Z : C} (g h : Z ⟶ ⨁ f) (w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h := (biproduct.isLimit f).hom_ext fun j => w j.as @[ext] theorem biproduct.hom_ext' {f : J → C} [HasBiproduct f] {Z : C} (g h : ⨁ f ⟶ Z) (w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h := (biproduct.isColimit f).hom_ext fun j => w j.as /-- The canonical isomorphism between the chosen biproduct and the chosen product. -/ def biproduct.isoProduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∏ᶜ f := IsLimit.conePointUniqueUpToIso (biproduct.isLimit f) (limit.isLimit _) @[simp] theorem biproduct.isoProduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).hom = Pi.lift (biproduct.π f) := limit.hom_ext fun j => by simp [biproduct.isoProduct] @[simp] theorem biproduct.isoProduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).inv = biproduct.lift (Pi.π f) := biproduct.hom_ext _ _ fun j => by simp [Iso.inv_comp_eq] /-- The canonical isomorphism between the chosen biproduct and the chosen coproduct. -/ def biproduct.isoCoproduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∐ f := IsColimit.coconePointUniqueUpToIso (biproduct.isColimit f) (colimit.isColimit _) @[simp] theorem biproduct.isoCoproduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).inv = Sigma.desc (biproduct.ι f) := colimit.hom_ext fun j => by simp [biproduct.isoCoproduct] @[simp] theorem biproduct.isoCoproduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).hom = biproduct.desc (Sigma.ι f) := biproduct.hom_ext' _ _ fun j => by simp [← Iso.eq_comp_inv] /-- If a category has biproducts of a shape `J`, its `colim` and `lim` functor on diagrams over `J` are isomorphic. -/ @[simps!] def HasBiproductsOfShape.colimIsoLim [HasBiproductsOfShape J C] : colim (J := Discrete J) (C := C) ≅ lim := NatIso.ofComponents (fun F => (Sigma.isoColimit F).symm ≪≫ (biproduct.isoCoproduct _).symm ≪≫ biproduct.isoProduct _ ≪≫ Pi.isoLimit F) fun η => colimit.hom_ext fun ⟨i⟩ => limit.hom_ext fun ⟨j⟩ => by classical by_cases h : i = j <;> simp_all [h, Sigma.isoColimit, Pi.isoLimit, biproduct.ι_π, biproduct.ι_π_assoc] theorem biproduct.map_eq_map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : biproduct.map p = biproduct.map' p := by classical ext dsimp simp only [Discrete.natTrans_app, Limits.IsColimit.ι_map_assoc, Limits.IsLimit.map_π, Category.assoc, ← Bicone.toCone_π_app_mk, ← biproduct.bicone_π, ← Bicone.toCocone_ι_app_mk, ← biproduct.bicone_ι] dsimp rw [biproduct.ι_π_assoc, biproduct.ι_π] split_ifs with h · subst h; simp · simp @[reassoc (attr := simp)] theorem biproduct.map_π {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j := Limits.IsLimit.map_π _ _ _ (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.ι_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j := by rw [biproduct.map_eq_map'] apply Limits.IsColimit.ι_map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.map_desc {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) {P : C} (k : ∀ j, g j ⟶ P) : biproduct.map p ≫ biproduct.desc k = biproduct.desc fun j => p j ≫ k j := by ext; simp @[reassoc (attr := simp)] theorem biproduct.lift_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] {P : C} (k : ∀ j, P ⟶ f j) (p : ∀ j, f j ⟶ g j) : biproduct.lift k ≫ biproduct.map p = biproduct.lift fun j => k j ≫ p j := by ext; simp /-- Given a collection of isomorphisms between corresponding summands of a pair of biproducts indexed by the same type, we obtain an isomorphism between the biproducts. -/ @[simps] def biproduct.mapIso {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ≅ g b) : ⨁ f ≅ ⨁ g where hom := biproduct.map fun b => (p b).hom inv := biproduct.map fun b => (p b).inv instance biproduct.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (biproduct.map p) := by classical have : biproduct.map p = (biproduct.isoCoproduct _).hom ≫ Sigma.map p ≫ (biproduct.isoCoproduct _).inv := by ext simp only [map_π, isoCoproduct_hom, isoCoproduct_inv, Category.assoc, ι_desc_assoc, ι_colimMap_assoc, Discrete.functor_obj_eq_as, Discrete.natTrans_app, colimit.ι_desc_assoc, Cofan.mk_pt, Cofan.mk_ι_app, ι_π, ι_π_assoc] split all_goals simp_all rw [this] infer_instance instance Pi.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (Pi.map p) := by rw [show Pi.map p = (biproduct.isoProduct _).inv ≫ biproduct.map p ≫ (biproduct.isoProduct _).hom by aesop] infer_instance instance biproduct.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (biproduct.map p) := by rw [show biproduct.map p = (biproduct.isoProduct _).hom ≫ Pi.map p ≫ (biproduct.isoProduct _).inv by aesop] infer_instance instance Sigma.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (Sigma.map p) := by rw [show Sigma.map p = (biproduct.isoCoproduct _).inv ≫ biproduct.map p ≫ (biproduct.isoCoproduct _).hom by aesop] infer_instance /-- Two biproducts which differ by an equivalence in the indexing type, and up to isomorphism in the factors, are isomorphic. Unfortunately there are two natural ways to define each direction of this isomorphism (because it is true for both products and coproducts separately). We give the alternative definitions as lemmas below. -/ @[simps] def biproduct.whiskerEquiv {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : ⨁ f ≅ ⨁ g where hom := biproduct.desc fun j => (w j).inv ≫ biproduct.ι g (e j) inv := biproduct.desc fun k => eqToHom (by simp) ≫ (w (e.symm k)).hom ≫ biproduct.ι f _ lemma biproduct.whiskerEquiv_hom_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).hom = biproduct.lift fun k => biproduct.π f (e.symm k) ≫ (w _).inv ≫ eqToHom (by simp) := by simp only [whiskerEquiv_hom] ext k j by_cases h : k = e j · subst h simp · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · rintro rfl simp at h · exact Ne.symm h lemma biproduct.whiskerEquiv_inv_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).inv = biproduct.lift fun j => biproduct.π g (e j) ≫ (w j).hom := by simp only [whiskerEquiv_inv] ext j k by_cases h : k = e j · subst h simp only [ι_desc_assoc, ← eqToHom_iso_hom_naturality_assoc w (e.symm_apply_apply j).symm, Equiv.symm_apply_apply, eqToHom_comp_ι, Category.assoc, bicone_ι_π_self, Category.comp_id, lift_π, bicone_ι_π_self_assoc] · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · exact h · rintro rfl simp at h attribute [local simp] Sigma.forall in instance {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : HasBiproduct fun p : Σ i, f i => g p.1 p.2 where exists_biproduct := Nonempty.intro { bicone := { pt := ⨁ fun i => ⨁ g i ι := fun X => biproduct.ι (g X.1) X.2 ≫ biproduct.ι (fun i => ⨁ g i) X.1 π := fun X => biproduct.π (fun i => ⨁ g i) X.1 ≫ biproduct.π (g X.1) X.2 ι_π := fun ⟨j, x⟩ ⟨j', y⟩ => by split_ifs with h · obtain ⟨rfl, rfl⟩ := h simp · simp only [Sigma.mk.inj_iff, not_and] at h by_cases w : j = j' · cases w simp only [heq_eq_eq, forall_true_left] at h simp [biproduct.ι_π_ne _ h] · simp [biproduct.ι_π_ne_assoc _ w] } isBilimit := { isLimit := mkFanLimit _ (fun s => biproduct.lift fun b => biproduct.lift fun c => s.proj ⟨b, c⟩) isColimit := mkCofanColimit _ (fun s => biproduct.desc fun b => biproduct.desc fun c => s.inj ⟨b, c⟩) } } /-- An iterated biproduct is a biproduct over a sigma type. -/ @[simps] def biproductBiproductIso {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : (⨁ fun i => ⨁ g i) ≅ (⨁ fun p : Σ i, f i => g p.1 p.2) where hom := biproduct.lift fun ⟨i, x⟩ => biproduct.π _ i ≫ biproduct.π _ x inv := biproduct.lift fun i => biproduct.lift fun x => biproduct.π _ (⟨i, x⟩ : Σ i, f i) section πKernel section variable (f : J → C) [HasBiproduct f] variable (p : J → Prop) [HasBiproduct (Subtype.restrict p f)] /-- The canonical morphism from the biproduct over a restricted index type to the biproduct of the full index type. -/ def biproduct.fromSubtype : ⨁ Subtype.restrict p f ⟶ ⨁ f := biproduct.desc fun j => biproduct.ι _ j.val /-- The canonical morphism from a biproduct to the biproduct over a restriction of its index type. -/ def biproduct.toSubtype : ⨁ f ⟶ ⨁ Subtype.restrict p f := biproduct.lift fun _ => biproduct.π _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_π [DecidablePred p] (j : J) : biproduct.fromSubtype f p ≫ biproduct.π f j = if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i; dsimp rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show (i : J) ≠ j from fun h₂ => h (h₂ ▸ i.2)), comp_zero] theorem biproduct.fromSubtype_eq_lift [DecidablePred p] : biproduct.fromSubtype f p = biproduct.lift fun j => if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext _ _ (by simp) @[reassoc] -- Porting note: both version solved using simp theorem biproduct.fromSubtype_π_subtype (j : Subtype p) : biproduct.fromSubtype f p ≫ biproduct.π f j = biproduct.π (Subtype.restrict p f) j := by classical ext rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.toSubtype_π (j : Subtype p) : biproduct.toSubtype f p ≫ biproduct.π (Subtype.restrict p f) j = biproduct.π f j := biproduct.lift_π _ _ @[reassoc (attr := simp)] theorem biproduct.ι_toSubtype [DecidablePred p] (j : J) : biproduct.ι f j ≫ biproduct.toSubtype f p = if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show j ≠ i from fun h₂ => h (h₂.symm ▸ i.2)), zero_comp] theorem biproduct.toSubtype_eq_desc [DecidablePred p] : biproduct.toSubtype f p = biproduct.desc fun j => if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext' _ _ (by simp) @[reassoc] theorem biproduct.ι_toSubtype_subtype (j : Subtype p) : biproduct.ι f j ≫ biproduct.toSubtype f p = biproduct.ι (Subtype.restrict p f) j := by classical ext rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.ι_fromSubtype (j : Subtype p) : biproduct.ι (Subtype.restrict p f) j ≫ biproduct.fromSubtype f p = biproduct.ι f j := biproduct.ι_desc _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_toSubtype : biproduct.fromSubtype f p ≫ biproduct.toSubtype f p = 𝟙 (⨁ Subtype.restrict p f) := by refine biproduct.hom_ext _ _ fun j => ?_ rw [Category.assoc, biproduct.toSubtype_π, biproduct.fromSubtype_π_subtype, Category.id_comp] @[reassoc (attr := simp)] theorem biproduct.toSubtype_fromSubtype [DecidablePred p] : biproduct.toSubtype f p ≫ biproduct.fromSubtype f p = biproduct.map fun j => if p j then 𝟙 (f j) else 0 := by ext1 i by_cases h : p i · simp [h] · simp [h] end section variable (f : J → C) (i : J) [HasBiproduct f] [HasBiproduct (Subtype.restrict (fun j => j ≠ i) f)] open scoped Classical in /-- The kernel of `biproduct.π f i` is the inclusion from the biproduct which omits `i` from the index set `J` into the biproduct over `J`. -/ def biproduct.isLimitFromSubtype : IsLimit (KernelFork.ofι (biproduct.fromSubtype f fun j => j ≠ i) (by simp) : KernelFork (biproduct.π f i)) := Fork.IsLimit.mk' _ fun s => ⟨s.ι ≫ biproduct.toSubtype _ _, by apply biproduct.hom_ext; intro j rw [KernelFork.ι_ofι, Category.assoc, Category.assoc, biproduct.toSubtype_fromSubtype_assoc, biproduct.map_π] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), comp_zero, comp_zero, KernelFork.condition] · rw [if_pos (Ne.symm h), Category.comp_id], by intro m hm rw [← hm, KernelFork.ι_ofι, Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.comp_id _).symm⟩ instance : HasKernel (biproduct.π f i) := HasLimit.mk ⟨_, biproduct.isLimitFromSubtype f i⟩ /-- The kernel of `biproduct.π f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def kernelBiproductπIso : kernel (biproduct.π f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := limit.isoLimitCone ⟨_, biproduct.isLimitFromSubtype f i⟩ open scoped Classical in /-- The cokernel of `biproduct.ι f i` is the projection from the biproduct over the index set `J` onto the biproduct omitting `i`. -/ def biproduct.isColimitToSubtype : IsColimit (CokernelCofork.ofπ (biproduct.toSubtype f fun j => j ≠ i) (by simp) : CokernelCofork (biproduct.ι f i)) := Cofork.IsColimit.mk' _ fun s => ⟨biproduct.fromSubtype _ _ ≫ s.π, by apply biproduct.hom_ext'; intro j rw [CokernelCofork.π_ofπ, biproduct.toSubtype_fromSubtype_assoc, biproduct.ι_map_assoc] rcases Classical.em (i = j) with (rfl | h) · rw [if_neg (Classical.not_not.2 rfl), zero_comp, CokernelCofork.condition] · rw [if_pos (Ne.symm h), Category.id_comp], by intro m hm rw [← hm, CokernelCofork.π_ofπ, ← Category.assoc, biproduct.fromSubtype_toSubtype] exact (Category.id_comp _).symm⟩ instance : HasCokernel (biproduct.ι f i) := HasColimit.mk ⟨_, biproduct.isColimitToSubtype f i⟩ /-- The cokernel of `biproduct.ι f i` is `⨁ Subtype.restrict {i}ᶜ f`. -/ @[simps!] def cokernelBiproductιIso : cokernel (biproduct.ι f i) ≅ ⨁ Subtype.restrict (fun j => j ≠ i) f := colimit.isoColimitCocone ⟨_, biproduct.isColimitToSubtype f i⟩ end section -- Per https://github.com/leanprover-community/mathlib3/pull/15067, we only allow indexing in `Type 0` here. variable {K : Type} [Finite K] [HasFiniteBiproducts C] (f : K → C) /-- The limit cone exhibiting `⨁ Subtype.restrict pᶜ f` as the kernel of `biproduct.toSubtype f p` -/ @[simps] def kernelForkBiproductToSubtype (p : Set K) : LimitCone (parallelPair (biproduct.toSubtype f p) 0) where cone := KernelFork.ofι (biproduct.fromSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg k.2] simp only [zero_comp]) isLimit := KernelFork.IsLimit.ofι _ _ (fun {_} g _ => g ≫ biproduct.toSubtype f pᶜ) (by classical intro W' g' w ext j simp only [Category.assoc, biproduct.toSubtype_fromSubtype, Pi.compl_apply, biproduct.map_π] split_ifs with h · simp · replace w := w =≫ biproduct.π _ ⟨j, not_not.mp h⟩ simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasKernel (biproduct.toSubtype f p) := HasLimit.mk (kernelForkBiproductToSubtype f p) /-- The kernel of `biproduct.toSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def kernelBiproductToSubtypeIso (p : Set K) : kernel (biproduct.toSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := limit.isoLimitCone (kernelForkBiproductToSubtype f p) /-- The colimit cocone exhibiting `⨁ Subtype.restrict pᶜ f` as the cokernel of `biproduct.fromSubtype f p` -/ @[simps] def cokernelCoforkBiproductFromSubtype (p : Set K) : ColimitCocone (parallelPair (biproduct.fromSubtype f p) 0) where cocone := CokernelCofork.ofπ (biproduct.toSubtype f pᶜ) (by classical ext j k simp only [Category.assoc, Pi.compl_apply, biproduct.ι_fromSubtype_assoc, biproduct.ι_toSubtype_assoc, comp_zero, zero_comp] rw [dif_neg] · simp only [zero_comp] · exact not_not.mpr k.2) isColimit := CokernelCofork.IsColimit.ofπ _ _ (fun {_} g _ => biproduct.fromSubtype f pᶜ ≫ g) (by classical intro W g' w ext j simp only [biproduct.toSubtype_fromSubtype_assoc, Pi.compl_apply, biproduct.ι_map_assoc] split_ifs with h · simp · replace w := biproduct.ι _ (⟨j, not_not.mp h⟩ : p) ≫= w simpa using w.symm) (by aesop_cat) instance (p : Set K) : HasCokernel (biproduct.fromSubtype f p) := HasColimit.mk (cokernelCoforkBiproductFromSubtype f p) /-- The cokernel of `biproduct.fromSubtype f p` is `⨁ Subtype.restrict pᶜ f`. -/ @[simps!] def cokernelBiproductFromSubtypeIso (p : Set K) : cokernel (biproduct.fromSubtype f p) ≅ ⨁ Subtype.restrict pᶜ f := colimit.isoColimitCocone (cokernelCoforkBiproductFromSubtype f p) end end πKernel section FiniteBiproducts variable {J : Type} [Finite J] {K : Type} [Finite K] {C : Type u} [Category.{v} C] [HasZeroMorphisms C] [HasFiniteBiproducts C] {f : J → C} {g : K → C} /-- Convert a (dependently typed) matrix to a morphism of biproducts. -/ def biproduct.matrix (m : ∀ j k, f j ⟶ g k) : ⨁ f ⟶ ⨁ g := biproduct.desc fun j => biproduct.lift fun k => m j k @[reassoc (attr := simp)] theorem biproduct.matrix_π (m : ∀ j k, f j ⟶ g k) (k : K) : biproduct.matrix m ≫ biproduct.π g k = biproduct.desc fun j => m j k := by ext simp [biproduct.matrix] @[reassoc (attr := simp)] theorem biproduct.ι_matrix (m : ∀ j k, f j ⟶ g k) (j : J) : biproduct.ι f j ≫ biproduct.matrix m = biproduct.lift fun k => m j k := by ext simp [biproduct.matrix] /-- Extract the matrix components from a morphism of biproducts. -/ def biproduct.components (m : ⨁ f ⟶ ⨁ g) (j : J) (k : K) : f j ⟶ g k := biproduct.ι f j ≫ m ≫ biproduct.π g k @[simp] theorem biproduct.matrix_components (m : ∀ j k, f j ⟶ g k) (j : J) (k : K) : biproduct.components (biproduct.matrix m) j k = m j k := by simp [biproduct.components] @[simp] theorem biproduct.components_matrix (m : ⨁ f ⟶ ⨁ g) : (biproduct.matrix fun j k => biproduct.components m j k) = m := by ext simp [biproduct.components] /-- Morphisms between direct sums are matrices. -/ @[simps] def biproduct.matrixEquiv : (⨁ f ⟶ ⨁ g) ≃ ∀ j k, f j ⟶ g k where toFun := biproduct.components invFun := biproduct.matrix left_inv := biproduct.components_matrix right_inv m := by ext apply biproduct.matrix_components end FiniteBiproducts variable {J : Type w} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] instance biproduct.ι_mono (f : J → C) [HasBiproduct f] (b : J) : IsSplitMono (biproduct.ι f b) := by classical exact IsSplitMono.mk' { retraction := biproduct.desc <| Pi.single b (𝟙 (f b)) } instance biproduct.π_epi (f : J → C) [HasBiproduct f] (b : J) : IsSplitEpi (biproduct.π f b) := by classical exact IsSplitEpi.mk' { section_ := biproduct.lift <| Pi.single b (𝟙 (f b)) } /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_hom (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).hom = biproduct.lift b.π := rfl /-- Auxiliary lemma for `biproduct.uniqueUpToIso`. -/ theorem biproduct.conePointUniqueUpToIso_inv (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : (hb.isLimit.conePointUniqueUpToIso (biproduct.isLimit _)).inv = biproduct.desc b.ι := by classical refine biproduct.hom_ext' _ _ fun j => hb.isLimit.hom_ext fun j' => ?_ rw [Category.assoc, IsLimit.conePointUniqueUpToIso_inv_comp, Bicone.toCone_π_app, biproduct.bicone_π, biproduct.ι_desc, biproduct.ι_π, b.toCone_π_app, b.ι_π] /-- Biproducts are unique up to isomorphism. This already follows because bilimits are limits, but in the case of biproducts we can give an isomorphism with particularly nice definitional properties, namely that `biproduct.lift b.π` and `biproduct.desc b.ι` are inverses of each other. -/ @[simps] def biproduct.uniqueUpToIso (f : J → C) [HasBiproduct f] {b : Bicone f} (hb : b.IsBilimit) : b.pt ≅ ⨁ f where hom := biproduct.lift b.π inv := biproduct.desc b.ι hom_inv_id := by rw [← biproduct.conePointUniqueUpToIso_hom f hb, ← biproduct.conePointUniqueUpToIso_inv f hb, Iso.hom_inv_id] inv_hom_id := by rw [← biproduct.conePointUniqueUpToIso_hom f hb, ← biproduct.conePointUniqueUpToIso_inv f hb, Iso.inv_hom_id] variable (C) -- see Note [lower instance priority] /-- A category with finite biproducts has a zero object. -/ instance (priority := 100) hasZeroObject_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasZeroObject C := by refine ⟨⟨biproduct Empty.elim, fun X => ⟨⟨⟨0⟩, ?_⟩⟩, fun X => ⟨⟨⟨0⟩, ?_⟩⟩⟩⟩ · intro a; apply biproduct.hom_ext'; simp · intro a; apply biproduct.hom_ext; simp section variable {C} attribute [local simp] eq_iff_true_of_subsingleton in /-- The limit bicone for the biproduct over an index type with exactly one term. -/ @[simps] def limitBiconeOfUnique [Unique J] (f : J → C) : LimitBicone f where bicone := { pt := f default π := fun j => eqToHom (by congr; rw [← Unique.uniq] ) ι := fun j => eqToHom (by congr; rw [← Unique.uniq] ) } isBilimit := { isLimit := (limitConeOfUnique f).isLimit isColimit := (colimitCoconeOfUnique f).isColimit } instance (priority := 100) hasBiproduct_unique [Subsingleton J] [Nonempty J] (f : J → C) : HasBiproduct f := let ⟨_⟩ := nonempty_unique J; .mk (limitBiconeOfUnique f) /-- A biproduct over an index type with exactly one term is just the object over that term. -/ @[simps!] def biproductUniqueIso [Unique J] (f : J → C) : ⨁ f ≅ f default := (biproduct.uniqueUpToIso _ (limitBiconeOfUnique f).isBilimit).symm end end CategoryTheory.Limits
Mathlib/CategoryTheory/Limits/Shapes/Biproducts.lean
1,753
1,755
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Analysis.Convex.Basic import Mathlib.Topology.Algebra.Group.Pointwise import Mathlib.Topology.Order.Basic /-! # Strictly convex sets This file defines strictly convex sets. A set is strictly convex if the open segment between any two distinct points lies in its interior. -/ open Set open Convex Pointwise variable {𝕜 𝕝 E F β : Type*} open Function Set open Convex section OrderedSemiring /-- A set is strictly convex if the open segment between any two distinct points lies is in its interior. This basically means "convex and not flat on the boundary". -/ def StrictConvex (𝕜 : Type*) {E : Type*} [Semiring 𝕜] [PartialOrder 𝕜] [TopologicalSpace E] [AddCommMonoid E] [SMul 𝕜 E] (s : Set E) : Prop := s.Pairwise fun x y => ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ interior s variable [Semiring 𝕜] [PartialOrder 𝕜] [TopologicalSpace E] [TopologicalSpace F] section AddCommMonoid variable [AddCommMonoid E] [AddCommMonoid F] section SMul variable [SMul 𝕜 E] [SMul 𝕜 F] (s : Set E) variable {s} variable {x y : E} {a b : 𝕜} theorem strictConvex_iff_openSegment_subset : StrictConvex 𝕜 s ↔ s.Pairwise fun x y => openSegment 𝕜 x y ⊆ interior s := forall₅_congr fun _ _ _ _ _ => (openSegment_subset_iff 𝕜).symm theorem StrictConvex.openSegment_subset (hs : StrictConvex 𝕜 s) (hx : x ∈ s) (hy : y ∈ s) (h : x ≠ y) : openSegment 𝕜 x y ⊆ interior s := strictConvex_iff_openSegment_subset.1 hs hx hy h theorem strictConvex_empty : StrictConvex 𝕜 (∅ : Set E) := pairwise_empty _ theorem strictConvex_univ : StrictConvex 𝕜 (univ : Set E) := by intro x _ y _ _ a b _ _ _ rw [interior_univ] exact mem_univ _ protected nonrec theorem StrictConvex.eq (hs : StrictConvex 𝕜 s) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (h : a • x + b • y ∉ interior s) : x = y := hs.eq hx hy fun H => h <| H ha hb hab protected theorem StrictConvex.inter {t : Set E} (hs : StrictConvex 𝕜 s) (ht : StrictConvex 𝕜 t) : StrictConvex 𝕜 (s ∩ t) := by intro x hx y hy hxy a b ha hb hab rw [interior_inter] exact ⟨hs hx.1 hy.1 hxy ha hb hab, ht hx.2 hy.2 hxy ha hb hab⟩ theorem Directed.strictConvex_iUnion {ι : Sort*} {s : ι → Set E} (hdir : Directed (· ⊆ ·) s) (hs : ∀ ⦃i : ι⦄, StrictConvex 𝕜 (s i)) : StrictConvex 𝕜 (⋃ i, s i) := by rintro x hx y hy hxy a b ha hb hab rw [mem_iUnion] at hx hy obtain ⟨i, hx⟩ := hx obtain ⟨j, hy⟩ := hy obtain ⟨k, hik, hjk⟩ := hdir i j exact interior_mono (subset_iUnion s k) (hs (hik hx) (hjk hy) hxy ha hb hab) theorem DirectedOn.strictConvex_sUnion {S : Set (Set E)} (hdir : DirectedOn (· ⊆ ·) S) (hS : ∀ s ∈ S, StrictConvex 𝕜 s) : StrictConvex 𝕜 (⋃₀ S) := by rw [sUnion_eq_iUnion] exact (directedOn_iff_directed.1 hdir).strictConvex_iUnion fun s => hS _ s.2 end SMul section Module variable [Module 𝕜 E] [Module 𝕜 F] {s : Set E} protected theorem StrictConvex.convex (hs : StrictConvex 𝕜 s) : Convex 𝕜 s := convex_iff_pairwise_pos.2 fun _ hx _ hy hxy _ _ ha hb hab => interior_subset <| hs hx hy hxy ha hb hab /-- An open convex set is strictly convex. -/ protected theorem Convex.strictConvex_of_isOpen (h : IsOpen s) (hs : Convex 𝕜 s) : StrictConvex 𝕜 s := fun _ hx _ hy _ _ _ ha hb hab => h.interior_eq.symm ▸ hs hx hy ha.le hb.le hab theorem IsOpen.strictConvex_iff (h : IsOpen s) : StrictConvex 𝕜 s ↔ Convex 𝕜 s := ⟨StrictConvex.convex, Convex.strictConvex_of_isOpen h⟩ theorem strictConvex_singleton (c : E) : StrictConvex 𝕜 ({c} : Set E) := pairwise_singleton _ _ theorem Set.Subsingleton.strictConvex (hs : s.Subsingleton) : StrictConvex 𝕜 s := hs.pairwise _ theorem StrictConvex.linear_image [Semiring 𝕝] [Module 𝕝 E] [Module 𝕝 F] [LinearMap.CompatibleSMul E F 𝕜 𝕝] (hs : StrictConvex 𝕜 s) (f : E →ₗ[𝕝] F) (hf : IsOpenMap f) : StrictConvex 𝕜 (f '' s) := by rintro _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩ hxy a b ha hb hab refine hf.image_interior_subset _ ⟨a • x + b • y, hs hx hy (ne_of_apply_ne _ hxy) ha hb hab, ?_⟩ rw [map_add, f.map_smul_of_tower a, f.map_smul_of_tower b] theorem StrictConvex.is_linear_image (hs : StrictConvex 𝕜 s) {f : E → F} (h : IsLinearMap 𝕜 f) (hf : IsOpenMap f) : StrictConvex 𝕜 (f '' s) := hs.linear_image (h.mk' f) hf theorem StrictConvex.linear_preimage {s : Set F} (hs : StrictConvex 𝕜 s) (f : E →ₗ[𝕜] F) (hf : Continuous f) (hfinj : Injective f) : StrictConvex 𝕜 (s.preimage f) := by intro x hx y hy hxy a b ha hb hab refine preimage_interior_subset_interior_preimage hf ?_ rw [mem_preimage, f.map_add, f.map_smul, f.map_smul] exact hs hx hy (hfinj.ne hxy) ha hb hab theorem StrictConvex.is_linear_preimage {s : Set F} (hs : StrictConvex 𝕜 s) {f : E → F} (h : IsLinearMap 𝕜 f) (hf : Continuous f) (hfinj : Injective f) : StrictConvex 𝕜 (s.preimage f) := hs.linear_preimage (h.mk' f) hf hfinj section LinearOrderedCancelAddCommMonoid variable [TopologicalSpace β] [AddCommMonoid β] [LinearOrder β] [IsOrderedCancelAddMonoid β] [OrderTopology β] [Module 𝕜 β] [OrderedSMul 𝕜 β] protected theorem Set.OrdConnected.strictConvex {s : Set β} (hs : OrdConnected s) : StrictConvex 𝕜 s := by refine strictConvex_iff_openSegment_subset.2 fun x hx y hy hxy => ?_ rcases hxy.lt_or_lt with hlt | hlt <;> [skip; rw [openSegment_symm]] <;> exact (openSegment_subset_Ioo hlt).trans (isOpen_Ioo.subset_interior_iff.2 <| Ioo_subset_Icc_self.trans <| hs.out ‹_› ‹_›) theorem strictConvex_Iic (r : β) : StrictConvex 𝕜 (Iic r) := ordConnected_Iic.strictConvex theorem strictConvex_Ici (r : β) : StrictConvex 𝕜 (Ici r) := ordConnected_Ici.strictConvex theorem strictConvex_Iio (r : β) : StrictConvex 𝕜 (Iio r) := ordConnected_Iio.strictConvex theorem strictConvex_Ioi (r : β) : StrictConvex 𝕜 (Ioi r) := ordConnected_Ioi.strictConvex theorem strictConvex_Icc (r s : β) : StrictConvex 𝕜 (Icc r s) := ordConnected_Icc.strictConvex theorem strictConvex_Ioo (r s : β) : StrictConvex 𝕜 (Ioo r s) := ordConnected_Ioo.strictConvex theorem strictConvex_Ico (r s : β) : StrictConvex 𝕜 (Ico r s) := ordConnected_Ico.strictConvex theorem strictConvex_Ioc (r s : β) : StrictConvex 𝕜 (Ioc r s) := ordConnected_Ioc.strictConvex theorem strictConvex_uIcc (r s : β) : StrictConvex 𝕜 (uIcc r s) := strictConvex_Icc _ _ theorem strictConvex_uIoc (r s : β) : StrictConvex 𝕜 (uIoc r s) := strictConvex_Ioc _ _ end LinearOrderedCancelAddCommMonoid end Module end AddCommMonoid section AddCancelCommMonoid variable [AddCancelCommMonoid E] [ContinuousAdd E] [Module 𝕜 E] {s : Set E} /-- The translation of a strictly convex set is also strictly convex. -/ theorem StrictConvex.preimage_add_right (hs : StrictConvex 𝕜 s) (z : E) : StrictConvex 𝕜 ((fun x => z + x) ⁻¹' s) := by intro x hx y hy hxy a b ha hb hab refine preimage_interior_subset_interior_preimage (continuous_add_left _) ?_ have h := hs hx hy ((add_right_injective _).ne hxy) ha hb hab rwa [smul_add, smul_add, add_add_add_comm, ← _root_.add_smul, hab, one_smul] at h /-- The translation of a strictly convex set is also strictly convex. -/ theorem StrictConvex.preimage_add_left (hs : StrictConvex 𝕜 s) (z : E) : StrictConvex 𝕜 ((fun x => x + z) ⁻¹' s) := by simpa only [add_comm] using hs.preimage_add_right z end AddCancelCommMonoid section AddCommGroup variable [AddCommGroup E] [AddCommGroup F] [Module 𝕜 E] [Module 𝕜 F] section continuous_add variable [ContinuousAdd E] {s t : Set E} theorem StrictConvex.add (hs : StrictConvex 𝕜 s) (ht : StrictConvex 𝕜 t) : StrictConvex 𝕜 (s + t) := by rintro _ ⟨v, hv, w, hw, rfl⟩ _ ⟨x, hx, y, hy, rfl⟩ h a b ha hb hab rw [smul_add, smul_add, add_add_add_comm] obtain rfl | hvx := eq_or_ne v x · refine interior_mono (add_subset_add (singleton_subset_iff.2 hv) Subset.rfl) ?_ rw [Convex.combo_self hab, singleton_add] exact (isOpenMap_add_left _).image_interior_subset _ (mem_image_of_mem _ <| ht hw hy (ne_of_apply_ne _ h) ha hb hab) exact subset_interior_add_left (add_mem_add (hs hv hx hvx ha hb hab) <| ht.convex hw hy ha.le hb.le hab) theorem StrictConvex.add_left (hs : StrictConvex 𝕜 s) (z : E) : StrictConvex 𝕜 ((fun x => z + x) '' s) := by simpa only [singleton_add] using (strictConvex_singleton z).add hs theorem StrictConvex.add_right (hs : StrictConvex 𝕜 s) (z : E) : StrictConvex 𝕜 ((fun x => x + z) '' s) := by simpa only [add_comm] using hs.add_left z /-- The translation of a strictly convex set is also strictly convex. -/ theorem StrictConvex.vadd (hs : StrictConvex 𝕜 s) (x : E) : StrictConvex 𝕜 (x +ᵥ s) := hs.add_left x end continuous_add section ContinuousSMul variable [Field 𝕝] [Module 𝕝 E] [ContinuousConstSMul 𝕝 E] [LinearMap.CompatibleSMul E E 𝕜 𝕝] {s : Set E} {x : E} theorem StrictConvex.smul (hs : StrictConvex 𝕜 s) (c : 𝕝) : StrictConvex 𝕜 (c • s) := by obtain rfl | hc := eq_or_ne c 0 · exact (subsingleton_zero_smul_set _).strictConvex · exact hs.linear_image (LinearMap.lsmul _ _ c) (isOpenMap_smul₀ hc) theorem StrictConvex.affinity [ContinuousAdd E] (hs : StrictConvex 𝕜 s) (z : E) (c : 𝕝) : StrictConvex 𝕜 (z +ᵥ c • s) := (hs.smul c).vadd z end ContinuousSMul end AddCommGroup end OrderedSemiring section CommSemiring variable [CommSemiring 𝕜] [PartialOrder 𝕜] [TopologicalSpace E] section AddCommGroup variable [AddCommGroup E] [Module 𝕜 E] [NoZeroSMulDivisors 𝕜 E] [ContinuousConstSMul 𝕜 E] {s : Set E} theorem StrictConvex.preimage_smul (hs : StrictConvex 𝕜 s) (c : 𝕜) : StrictConvex 𝕜 ((fun z => c • z) ⁻¹' s) := by classical obtain rfl | hc := eq_or_ne c 0 · simp_rw [zero_smul, preimage_const] split_ifs · exact strictConvex_univ · exact strictConvex_empty refine hs.linear_preimage (LinearMap.lsmul _ _ c) ?_ (smul_right_injective E hc) unfold LinearMap.lsmul LinearMap.mk₂ LinearMap.mk₂' LinearMap.mk₂'ₛₗ exact continuous_const_smul _ end AddCommGroup end CommSemiring section OrderedRing variable [Ring 𝕜] [PartialOrder 𝕜] [TopologicalSpace E] [TopologicalSpace F] section AddCommGroup variable [AddCommGroup E] [AddCommGroup F] [Module 𝕜 E] [Module 𝕜 F] {s t : Set E} {x y : E} theorem StrictConvex.eq_of_openSegment_subset_frontier [IsOrderedRing 𝕜] [Nontrivial 𝕜] [DenselyOrdered 𝕜] (hs : StrictConvex 𝕜 s) (hx : x ∈ s) (hy : y ∈ s) (h : openSegment 𝕜 x y ⊆ frontier s) : x = y := by obtain ⟨a, ha₀, ha₁⟩ := DenselyOrdered.dense (0 : 𝕜) 1 zero_lt_one classical by_contra hxy exact (h ⟨a, 1 - a, ha₀, sub_pos_of_lt ha₁, add_sub_cancel _ _, rfl⟩).2 (hs hx hy hxy ha₀ (sub_pos_of_lt ha₁) <| add_sub_cancel _ _) theorem StrictConvex.add_smul_mem [AddRightStrictMono 𝕜] (hs : StrictConvex 𝕜 s) (hx : x ∈ s) (hxy : x + y ∈ s) (hy : y ≠ 0) {t : 𝕜} (ht₀ : 0 < t) (ht₁ : t < 1) : x + t • y ∈ interior s := by have h : x + t • y = (1 - t) • x + t • (x + y) := by match_scalars <;> field_simp rw [h] exact hs hx hxy (fun h => hy <| add_left_cancel (a := x) (by rw [← h, add_zero])) (sub_pos_of_lt ht₁) ht₀ (sub_add_cancel 1 t) theorem StrictConvex.smul_mem_of_zero_mem [AddRightStrictMono 𝕜] (hs : StrictConvex 𝕜 s) (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) (hx₀ : x ≠ 0) {t : 𝕜} (ht₀ : 0 < t) (ht₁ : t < 1) : t • x ∈ interior s := by simpa using hs.add_smul_mem zero_mem (by simpa using hx) hx₀ ht₀ ht₁ theorem StrictConvex.add_smul_sub_mem [AddRightMono 𝕜] (h : StrictConvex 𝕜 s) (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) {t : 𝕜} (ht₀ : 0 < t) (ht₁ : t < 1) : x + t • (y - x) ∈ interior s := by apply h.openSegment_subset hx hy hxy rw [openSegment_eq_image'] exact mem_image_of_mem _ ⟨ht₀, ht₁⟩ /-- The preimage of a strictly convex set under an affine map is strictly convex. -/ theorem StrictConvex.affine_preimage {s : Set F} (hs : StrictConvex 𝕜 s) {f : E →ᵃ[𝕜] F} (hf : Continuous f) (hfinj : Injective f) : StrictConvex 𝕜 (f ⁻¹' s) := by intro x hx y hy hxy a b ha hb hab refine preimage_interior_subset_interior_preimage hf ?_ rw [mem_preimage, Convex.combo_affine_apply hab] exact hs hx hy (hfinj.ne hxy) ha hb hab /-- The image of a strictly convex set under an affine map is strictly convex. -/ theorem StrictConvex.affine_image (hs : StrictConvex 𝕜 s) {f : E →ᵃ[𝕜] F} (hf : IsOpenMap f) : StrictConvex 𝕜 (f '' s) := by rintro _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩ hxy a b ha hb hab exact hf.image_interior_subset _ ⟨a • x + b • y, ⟨hs hx hy (ne_of_apply_ne _ hxy) ha hb hab, Convex.combo_affine_apply hab⟩⟩ variable [IsTopologicalAddGroup E] theorem StrictConvex.neg (hs : StrictConvex 𝕜 s) : StrictConvex 𝕜 (-s) := hs.is_linear_preimage IsLinearMap.isLinearMap_neg continuous_id.neg neg_injective
theorem StrictConvex.sub (hs : StrictConvex 𝕜 s) (ht : StrictConvex 𝕜 t) : StrictConvex 𝕜 (s - t) := (sub_eq_add_neg s t).symm ▸ hs.add ht.neg end AddCommGroup end OrderedRing
Mathlib/Analysis/Convex/Strict.lean
344
350
/- 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.Finite.Defs import Mathlib.Data.Finset.BooleanAlgebra import Mathlib.Data.Finset.Image import Mathlib.Data.Fintype.Defs import Mathlib.Data.Fintype.OfMap import Mathlib.Data.Fintype.Sets import Mathlib.Data.List.FinRange /-! # Instances for finite types This file is a collection of basic `Fintype` instances for types such as `Fin`, `Prod` and pi types. -/ assert_not_exists Monoid open Function open Nat universe u v variable {α β γ : Type*} open Finset instance Fin.fintype (n : ℕ) : Fintype (Fin n) := ⟨⟨List.finRange n, List.nodup_finRange n⟩, List.mem_finRange⟩ theorem Fin.univ_def (n : ℕ) : (univ : Finset (Fin n)) = ⟨List.finRange n, List.nodup_finRange n⟩ := rfl theorem Finset.val_univ_fin (n : ℕ) : (Finset.univ : Finset (Fin n)).val = List.finRange n := rfl /-- See also `nonempty_encodable`, `nonempty_denumerable`. -/ theorem nonempty_fintype (α : Type*) [Finite α] : Nonempty (Fintype α) := by rcases Finite.exists_equiv_fin α with ⟨n, ⟨e⟩⟩ exact ⟨.ofEquiv _ e.symm⟩ @[simp] theorem List.toFinset_finRange (n : ℕ) : (List.finRange n).toFinset = Finset.univ := by ext; simp @[simp] theorem Fin.univ_val_map {n : ℕ} (f : Fin n → α) : Finset.univ.val.map f = List.ofFn f := by simp [List.ofFn_eq_map, univ_def] theorem Fin.univ_image_def {n : ℕ} [DecidableEq α] (f : Fin n → α) : Finset.univ.image f = (List.ofFn f).toFinset := by simp [Finset.image] theorem Fin.univ_map_def {n : ℕ} (f : Fin n ↪ α) : Finset.univ.map f = ⟨List.ofFn f, List.nodup_ofFn.mpr f.injective⟩ := by simp [Finset.map] @[simp] theorem Fin.image_succAbove_univ {n : ℕ} (i : Fin (n + 1)) : univ.image i.succAbove = {i}ᶜ := by ext m simp @[simp] theorem Fin.image_succ_univ (n : ℕ) : (univ : Finset (Fin n)).image Fin.succ = {0}ᶜ := by rw [← Fin.succAbove_zero, Fin.image_succAbove_univ] @[simp] theorem Fin.image_castSucc (n : ℕ) : (univ : Finset (Fin n)).image Fin.castSucc = {Fin.last n}ᶜ := by rw [← Fin.succAbove_last, Fin.image_succAbove_univ] /- The following three lemmas use `Finset.cons` instead of `insert` and `Finset.map` instead of `Finset.image` to reduce proof obligations downstream. -/ /-- Embed `Fin n` into `Fin (n + 1)` by prepending zero to the `univ` -/ theorem Fin.univ_succ (n : ℕ) : (univ : Finset (Fin (n + 1))) = Finset.cons 0 (univ.map ⟨Fin.succ, Fin.succ_injective _⟩) (by simp [map_eq_image]) := by simp [map_eq_image] /-- Embed `Fin n` into `Fin (n + 1)` by appending a new `Fin.last n` to the `univ` -/ theorem Fin.univ_castSuccEmb (n : ℕ) : (univ : Finset (Fin (n + 1))) = Finset.cons (Fin.last n) (univ.map Fin.castSuccEmb) (by simp [map_eq_image]) := by simp [map_eq_image] /-- Embed `Fin n` into `Fin (n + 1)` by inserting around a specified pivot `p : Fin (n + 1)` into the `univ` -/ theorem Fin.univ_succAbove (n : ℕ) (p : Fin (n + 1)) : (univ : Finset (Fin (n + 1))) = Finset.cons p (univ.map <| Fin.succAboveEmb p) (by simp) := by simp [map_eq_image] @[simp] theorem Fin.univ_image_get [DecidableEq α] (l : List α) : Finset.univ.image l.get = l.toFinset := by simp [univ_image_def] @[simp] theorem Fin.univ_image_getElem' [DecidableEq β] (l : List α) (f : α → β) : Finset.univ.image (fun i : Fin l.length => f <| l[(i : Nat)]) = (l.map f).toFinset := by simp only [univ_image_def, List.ofFn_getElem_eq_map] theorem Fin.univ_image_get' [DecidableEq β] (l : List α) (f : α → β) : Finset.univ.image (f <| l.get ·) = (l.map f).toFinset := by simp @[instance] def Unique.fintype {α : Type*} [Unique α] : Fintype α := Fintype.ofSubsingleton default /-- Short-circuit instance to decrease search for `Unique.fintype`, since that relies on a subsingleton elimination for `Unique`. -/ instance Fintype.subtypeEq (y : α) : Fintype { x // x = y } := Fintype.subtype {y} (by simp) /-- Short-circuit instance to decrease search for `Unique.fintype`, since that relies on a subsingleton elimination for `Unique`. -/ instance Fintype.subtypeEq' (y : α) : Fintype { x // y = x } := Fintype.subtype {y} (by simp [eq_comm]) theorem Fintype.univ_empty : @univ Empty _ = ∅ := rfl theorem Fintype.univ_pempty : @univ PEmpty _ = ∅ := rfl instance Unit.fintype : Fintype Unit := Fintype.ofSubsingleton () theorem Fintype.univ_unit : @univ Unit _ = {()} := rfl instance PUnit.fintype : Fintype PUnit := Fintype.ofSubsingleton PUnit.unit theorem Fintype.univ_punit : @univ PUnit _ = {PUnit.unit} := rfl @[simp] theorem Fintype.univ_bool : @univ Bool _ = {true, false} := rfl /-- Given that `α × β` is a fintype, `α` is also a fintype. -/ def Fintype.prodLeft {α β} [DecidableEq α] [Fintype (α × β)] [Nonempty β] : Fintype α := ⟨(@univ (α × β) _).image Prod.fst, fun a => by simp⟩ /-- Given that `α × β` is a fintype, `β` is also a fintype. -/ def Fintype.prodRight {α β} [DecidableEq β] [Fintype (α × β)] [Nonempty α] : Fintype β := ⟨(@univ (α × β) _).image Prod.snd, fun b => by simp⟩ instance ULift.fintype (α : Type*) [Fintype α] : Fintype (ULift α) := Fintype.ofEquiv _ Equiv.ulift.symm instance PLift.fintype (α : Type*) [Fintype α] : Fintype (PLift α) := Fintype.ofEquiv _ Equiv.plift.symm instance PLift.fintypeProp (p : Prop) [Decidable p] : Fintype (PLift p) := ⟨if h : p then {⟨h⟩} else ∅, fun ⟨h⟩ => by simp [h]⟩ instance Quotient.fintype [Fintype α] (s : Setoid α) [DecidableRel ((· ≈ ·) : α → α → Prop)] : Fintype (Quotient s) := Fintype.ofSurjective Quotient.mk'' Quotient.mk''_surjective instance PSigma.fintypePropLeft {α : Prop} {β : α → Type*} [Decidable α] [∀ a, Fintype (β a)] : Fintype (Σ'a, β a) := if h : α then Fintype.ofEquiv (β h) ⟨fun x => ⟨h, x⟩, PSigma.snd, fun _ => rfl, fun ⟨_, _⟩ => rfl⟩ else ⟨∅, fun x => (h x.1).elim⟩ instance PSigma.fintypePropRight {α : Type*} {β : α → Prop} [∀ a, Decidable (β a)] [Fintype α] : Fintype (Σ'a, β a) := Fintype.ofEquiv { a // β a } ⟨fun ⟨x, y⟩ => ⟨x, y⟩, fun ⟨x, y⟩ => ⟨x, y⟩, fun ⟨_, _⟩ => rfl, fun ⟨_, _⟩ => rfl⟩ instance PSigma.fintypePropProp {α : Prop} {β : α → Prop} [Decidable α] [∀ a, Decidable (β a)] : Fintype (Σ'a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, fun ⟨_, _⟩ => by simp⟩ else ⟨∅, fun ⟨x, y⟩ => (h ⟨x, y⟩).elim⟩ instance pfunFintype (p : Prop) [Decidable p] (α : p → Type*) [∀ hp, Fintype (α hp)] : Fintype (∀ hp : p, α hp) := if hp : p then Fintype.ofEquiv (α hp) ⟨fun a _ => a, fun f => f hp, fun _ => rfl, fun _ => rfl⟩ else ⟨singleton fun h => (hp h).elim, fun h => mem_singleton.2 (funext fun x => by contradiction)⟩ section Trunc /-- For `s : Multiset α`, we can lift the existential statement that `∃ x, x ∈ s` to a `Trunc α`. -/ def truncOfMultisetExistsMem {α} (s : Multiset α) : (∃ x, x ∈ s) → Trunc α := Quotient.recOnSubsingleton s fun l h => match l, h with | [], _ => False.elim (by tauto) | a :: _, _ => Trunc.mk a /-- A `Nonempty` `Fintype` constructively contains an element. -/ def truncOfNonemptyFintype (α) [Nonempty α] [Fintype α] : Trunc α := truncOfMultisetExistsMem Finset.univ.val (by simp) /-- By iterating over the elements of a fintype, we can lift an existential statement `∃ a, P a` to `Trunc (Σ' a, P a)`, containing data. -/ def truncSigmaOfExists {α} [Fintype α] {P : α → Prop} [DecidablePred P] (h : ∃ a, P a) : Trunc (Σ'a, P a) := @truncOfNonemptyFintype (Σ'a, P a) ((Exists.elim h) fun a ha => ⟨⟨a, ha⟩⟩) _ end Trunc namespace Multiset variable [Fintype α] [Fintype β] @[simp] theorem count_univ [DecidableEq α] (a : α) : count a Finset.univ.val = 1 := count_eq_one_of_mem Finset.univ.nodup (Finset.mem_univ _) @[simp] theorem map_univ_val_equiv (e : α ≃ β) : map e univ.val = univ.val := by rw [← congr_arg Finset.val (Finset.map_univ_equiv e), Finset.map_val, Equiv.coe_toEmbedding] /-- For functions on finite sets, they are bijections iff they map universes into universes. -/ @[simp] theorem bijective_iff_map_univ_eq_univ (f : α → β) : f.Bijective ↔ map f (Finset.univ : Finset α).val = univ.val := ⟨fun bij ↦ congr_arg (·.val) (map_univ_equiv <| Equiv.ofBijective f bij), fun eq ↦ ⟨ fun a₁ a₂ ↦ inj_on_of_nodup_map (eq.symm ▸ univ.nodup) _ (mem_univ a₁) _ (mem_univ a₂), fun b ↦ have ⟨a, _, h⟩ := mem_map.mp (eq.symm ▸ mem_univ_val b); ⟨a, h⟩⟩⟩ end Multiset /-- Auxiliary definition to show `exists_seq_of_forall_finset_exists`. -/ noncomputable def seqOfForallFinsetExistsAux {α : Type*} [DecidableEq α] (P : α → Prop) (r : α → α → Prop) (h : ∀ s : Finset α, ∃ y, (∀ x ∈ s, P x) → P y ∧ ∀ x ∈ s, r x y) : ℕ → α | n => Classical.choose (h (Finset.image (fun i : Fin n => seqOfForallFinsetExistsAux P r h i) (Finset.univ : Finset (Fin n)))) /-- Induction principle to build a sequence, by adding one point at a time satisfying a given relation with respect to all the previously chosen points. More precisely, Assume that, for any finite set `s`, one can find another point satisfying some relation `r` with respect to all the points in `s`. Then one may construct a function `f : ℕ → α` such that `r (f m) (f n)` holds whenever `m < n`. We also ensure that all constructed points satisfy a given predicate `P`. -/ theorem exists_seq_of_forall_finset_exists {α : Type*} (P : α → Prop) (r : α → α → Prop) (h : ∀ s : Finset α, (∀ x ∈ s, P x) → ∃ y, P y ∧ ∀ x ∈ s, r x y) : ∃ f : ℕ → α, (∀ n, P (f n)) ∧ ∀ m n, m < n → r (f m) (f n) := by classical have : Nonempty α := by rcases h ∅ (by simp) with ⟨y, _⟩ exact ⟨y⟩ choose! F hF using h have h' : ∀ s : Finset α, ∃ y, (∀ x ∈ s, P x) → P y ∧ ∀ x ∈ s, r x y := fun s => ⟨F s, hF s⟩ set f := seqOfForallFinsetExistsAux P r h' with hf have A : ∀ n : ℕ, P (f n) := by intro n induction' n using Nat.strong_induction_on with n IH have IH' : ∀ x : Fin n, P (f x) := fun n => IH n.1 n.2 rw [hf, seqOfForallFinsetExistsAux] exact (Classical.choose_spec (h' (Finset.image (fun i : Fin n => f i) (Finset.univ : Finset (Fin n)))) (by simp [IH'])).1 refine ⟨f, A, fun m n hmn => ?_⟩ conv_rhs => rw [hf] rw [seqOfForallFinsetExistsAux] apply (Classical.choose_spec (h' (Finset.image (fun i : Fin n => f i) (Finset.univ : Finset (Fin n)))) (by simp [A])).2 exact Finset.mem_image.2 ⟨⟨m, hmn⟩, Finset.mem_univ _, rfl⟩ /-- Induction principle to build a sequence, by adding one point at a time satisfying a given symmetric relation with respect to all the previously chosen points. More precisely, Assume that, for any finite set `s`, one can find another point satisfying some relation `r` with respect to all the points in `s`. Then one may construct a function `f : ℕ → α` such that `r (f m) (f n)` holds whenever `m ≠ n`. We also ensure that all constructed points satisfy a given predicate `P`. -/ theorem exists_seq_of_forall_finset_exists' {α : Type*} (P : α → Prop) (r : α → α → Prop) [IsSymm α r] (h : ∀ s : Finset α, (∀ x ∈ s, P x) → ∃ y, P y ∧ ∀ x ∈ s, r x y) : ∃ f : ℕ → α, (∀ n, P (f n)) ∧ Pairwise (r on f) := by rcases exists_seq_of_forall_finset_exists P r h with ⟨f, hf, hf'⟩ refine ⟨f, hf, fun m n hmn => ?_⟩ rcases lt_trichotomy m n with (h | rfl | h) · exact hf' m n h · exact (hmn rfl).elim · unfold Function.onFun apply symm exact hf' n m h
Mathlib/Data/Fintype/Basic.lean
837
839
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Order.Disjoint import Mathlib.Order.RelIso.Basic import Mathlib.Tactic.Monotonicity.Attr /-! # Order homomorphisms This file defines order homomorphisms, which are bundled monotone functions. A preorder homomorphism `f : α →o β` is a function `α → β` along with a proof that `∀ x y, x ≤ y → f x ≤ f y`. ## Main definitions In this file we define the following bundled monotone maps: * `OrderHom α β` a.k.a. `α →o β`: Preorder homomorphism. An `OrderHom α β` is a function `f : α → β` such that `a₁ ≤ a₂ → f a₁ ≤ f a₂` * `OrderEmbedding α β` a.k.a. `α ↪o β`: Relation embedding. An `OrderEmbedding α β` is an embedding `f : α ↪ β` such that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@RelEmbedding α β (≤) (≤)`. * `OrderIso`: Relation isomorphism. An `OrderIso α β` is an equivalence `f : α ≃ β` such that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@RelIso α β (≤) (≤)`. We also define many `OrderHom`s. In some cases we define two versions, one with `ₘ` suffix and one without it (e.g., `OrderHom.compₘ` and `OrderHom.comp`). This means that the former function is a "more bundled" version of the latter. We can't just drop the "less bundled" version because the more bundled version usually does not work with dot notation. * `OrderHom.id`: identity map as `α →o α`; * `OrderHom.curry`: an order isomorphism between `α × β →o γ` and `α →o β →o γ`; * `OrderHom.comp`: composition of two bundled monotone maps; * `OrderHom.compₘ`: composition of bundled monotone maps as a bundled monotone map; * `OrderHom.const`: constant function as a bundled monotone map; * `OrderHom.prod`: combine `α →o β` and `α →o γ` into `α →o β × γ`; * `OrderHom.prodₘ`: a more bundled version of `OrderHom.prod`; * `OrderHom.prodIso`: order isomorphism between `α →o β × γ` and `(α →o β) × (α →o γ)`; * `OrderHom.diag`: diagonal embedding of `α` into `α × α` as a bundled monotone map; * `OrderHom.onDiag`: restrict a monotone map `α →o α →o β` to the diagonal; * `OrderHom.fst`: projection `Prod.fst : α × β → α` as a bundled monotone map; * `OrderHom.snd`: projection `Prod.snd : α × β → β` as a bundled monotone map; * `OrderHom.prodMap`: `Prod.map f g` as a bundled monotone map; * `Pi.evalOrderHom`: evaluation of a function at a point `Function.eval i` as a bundled monotone map; * `OrderHom.coeFnHom`: coercion to function as a bundled monotone map; * `OrderHom.apply`: application of an `OrderHom` at a point as a bundled monotone map; * `OrderHom.pi`: combine a family of monotone maps `f i : α →o π i` into a monotone map `α →o Π i, π i`; * `OrderHom.piIso`: order isomorphism between `α →o Π i, π i` and `Π i, α →o π i`; * `OrderHom.subtype.val`: embedding `Subtype.val : Subtype p → α` as a bundled monotone map; * `OrderHom.dual`: reinterpret a monotone map `α →o β` as a monotone map `αᵒᵈ →o βᵒᵈ`; * `OrderHom.dualIso`: order isomorphism between `α →o β` and `(αᵒᵈ →o βᵒᵈ)ᵒᵈ`; * `OrderHom.compl`: order isomorphism `α ≃o αᵒᵈ` given by taking complements in a boolean algebra; We also define two functions to convert other bundled maps to `α →o β`: * `OrderEmbedding.toOrderHom`: convert `α ↪o β` to `α →o β`; * `RelHom.toOrderHom`: convert a `RelHom` between strict orders to an `OrderHom`. ## Tags monotone map, bundled morphism -/ -- Developments relating order homs and sets belong in `Order.Hom.Set` or later. assert_not_exists Set.range open OrderDual variable {F α β γ δ : Type*} /-- Bundled monotone (aka, increasing) function -/ structure OrderHom (α β : Type*) [Preorder α] [Preorder β] where /-- The underlying function of an `OrderHom`. -/ toFun : α → β /-- The underlying function of an `OrderHom` is monotone. -/ monotone' : Monotone toFun /-- Notation for an `OrderHom`. -/ infixr:25 " →o " => OrderHom /-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `RelEmbedding (≤) (≤)`. -/ abbrev OrderEmbedding (α β : Type*) [LE α] [LE β] := @RelEmbedding α β (· ≤ ·) (· ≤ ·) /-- Notation for an `OrderEmbedding`. -/ infixl:25 " ↪o " => OrderEmbedding /-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `RelIso (≤) (≤)`. -/ abbrev OrderIso (α β : Type*) [LE α] [LE β] := @RelIso α β (· ≤ ·) (· ≤ ·) /-- Notation for an `OrderIso`. -/ infixl:25 " ≃o " => OrderIso section /-- `OrderHomClass F α b` asserts that `F` is a type of `≤`-preserving morphisms. -/ abbrev OrderHomClass (F : Type*) (α β : outParam Type*) [LE α] [LE β] [FunLike F α β] := RelHomClass F ((· ≤ ·) : α → α → Prop) ((· ≤ ·) : β → β → Prop) /-- `OrderIsoClass F α β` states that `F` is a type of order isomorphisms. You should extend this class when you extend `OrderIso`. -/ class OrderIsoClass (F : Type*) (α β : outParam Type*) [LE α] [LE β] [EquivLike F α β] : Prop where /-- An order isomorphism respects `≤`. -/ map_le_map_iff (f : F) {a b : α} : f a ≤ f b ↔ a ≤ b end export OrderIsoClass (map_le_map_iff) attribute [simp] map_le_map_iff /-- Turn an element of a type `F` satisfying `OrderIsoClass F α β` into an actual `OrderIso`. This is declared as the default coercion from `F` to `α ≃o β`. -/ @[coe] def OrderIsoClass.toOrderIso [LE α] [LE β] [EquivLike F α β] [OrderIsoClass F α β] (f : F) : α ≃o β := { EquivLike.toEquiv f with map_rel_iff' := map_le_map_iff f } /-- Any type satisfying `OrderIsoClass` can be cast into `OrderIso` via `OrderIsoClass.toOrderIso`. -/ instance [LE α] [LE β] [EquivLike F α β] [OrderIsoClass F α β] : CoeTC F (α ≃o β) := ⟨OrderIsoClass.toOrderIso⟩ -- See note [lower instance priority] instance (priority := 100) OrderIsoClass.toOrderHomClass [LE α] [LE β] [EquivLike F α β] [OrderIsoClass F α β] : OrderHomClass F α β := { EquivLike.toEmbeddingLike (E := F) with map_rel := fun f _ _ => (map_le_map_iff f).2 } namespace OrderHomClass variable [Preorder α] [Preorder β] [FunLike F α β] [OrderHomClass F α β] protected theorem monotone (f : F) : Monotone f := fun _ _ => map_rel f protected theorem mono (f : F) : Monotone f := fun _ _ => map_rel f @[gcongr] protected lemma GCongr.mono (f : F) {a b : α} (hab : a ≤ b) : f a ≤ f b := OrderHomClass.mono f hab /-- Turn an element of a type `F` satisfying `OrderHomClass F α β` into an actual `OrderHom`. This is declared as the default coercion from `F` to `α →o β`. -/ @[coe] def toOrderHom (f : F) : α →o β where toFun := f monotone' := OrderHomClass.monotone f /-- Any type satisfying `OrderHomClass` can be cast into `OrderHom` via `OrderHomClass.toOrderHom`. -/ instance : CoeTC F (α →o β) := ⟨toOrderHom⟩ end OrderHomClass section OrderIsoClass section LE variable [LE α] [LE β] [EquivLike F α β] [OrderIsoClass F α β] @[simp] theorem map_inv_le_iff (f : F) {a : α} {b : β} : EquivLike.inv f b ≤ a ↔ b ≤ f a := by convert (map_le_map_iff f).symm exact (EquivLike.right_inv f _).symm theorem map_inv_le_map_inv_iff (f : F) {a b : β} : EquivLike.inv f b ≤ EquivLike.inv f a ↔ b ≤ a := by simp @[simp] theorem le_map_inv_iff (f : F) {a : α} {b : β} : a ≤ EquivLike.inv f b ↔ f a ≤ b := by convert (map_le_map_iff f).symm exact (EquivLike.right_inv _ _).symm end LE variable [Preorder α] [Preorder β] [EquivLike F α β] [OrderIsoClass F α β] theorem map_lt_map_iff (f : F) {a b : α} : f a < f b ↔ a < b := lt_iff_lt_of_le_iff_le' (map_le_map_iff f) (map_le_map_iff f) @[simp] theorem map_inv_lt_iff (f : F) {a : α} {b : β} : EquivLike.inv f b < a ↔ b < f a := by rw [← map_lt_map_iff f] simp only [EquivLike.apply_inv_apply] theorem map_inv_lt_map_inv_iff (f : F) {a b : β} : EquivLike.inv f b < EquivLike.inv f a ↔ b < a := by simp @[simp] theorem lt_map_inv_iff (f : F) {a : α} {b : β} : a < EquivLike.inv f b ↔ f a < b := by rw [← map_lt_map_iff f] simp only [EquivLike.apply_inv_apply] end OrderIsoClass namespace OrderHom variable [Preorder α] [Preorder β] [Preorder γ] [Preorder δ] instance : FunLike (α →o β) α β where coe := toFun coe_injective' f g h := by cases f; cases g; congr instance : OrderHomClass (α →o β) α β where map_rel f _ _ h := f.monotone' h @[simp] theorem coe_mk (f : α → β) (hf : Monotone f) : ⇑(mk f hf) = f := rfl protected theorem monotone (f : α →o β) : Monotone f := f.monotone' protected theorem mono (f : α →o β) : Monotone f := f.monotone /-- See Note [custom simps projection]. We give this manually so that we use `toFun` as the projection directly instead. -/ def Simps.coe (f : α →o β) : α → β := f /- TODO: all other DFunLike classes use `apply` instead of `coe` for the projection names. Maybe we should change this. -/ initialize_simps_projections OrderHom (toFun → coe) @[simp] theorem toFun_eq_coe (f : α →o β) : f.toFun = f := rfl -- See library note [partially-applied ext lemmas] @[ext] theorem ext (f g : α →o β) (h : (f : α → β) = g) : f = g := DFunLike.coe_injective h @[simp] theorem coe_eq (f : α →o β) : OrderHomClass.toOrderHom f = f := rfl @[simp] theorem _root_.OrderHomClass.coe_coe {F} [FunLike F α β] [OrderHomClass F α β] (f : F) : ⇑(f : α →o β) = f := rfl /-- One can lift an unbundled monotone function to a bundled one. -/ protected instance canLift : CanLift (α → β) (α →o β) (↑) Monotone where prf f h := ⟨⟨f, h⟩, rfl⟩ /-- Copy of an `OrderHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →o β) (f' : α → β) (h : f' = f) : α →o β := ⟨f', h.symm.subst f.monotone'⟩ @[simp] theorem coe_copy (f : α →o β) (f' : α → β) (h : f' = f) : (f.copy f' h) = f' := rfl theorem copy_eq (f : α →o β) (f' : α → β) (h : f' = f) : f.copy f' h = f := DFunLike.ext' h /-- The identity function as bundled monotone function. -/ @[simps -fullyApplied] def id : α →o α := ⟨_root_.id, monotone_id⟩ instance : Inhabited (α →o α) := ⟨id⟩ /-- The preorder structure of `α →o β` is pointwise inequality: `f ≤ g ↔ ∀ a, f a ≤ g a`. -/ instance : Preorder (α →o β) := @Preorder.lift (α →o β) (α → β) _ toFun instance {β : Type*} [PartialOrder β] : PartialOrder (α →o β) := @PartialOrder.lift (α →o β) (α → β) _ toFun ext theorem le_def {f g : α →o β} : f ≤ g ↔ ∀ x, f x ≤ g x := Iff.rfl @[simp, norm_cast] theorem coe_le_coe {f g : α →o β} : (f : α → β) ≤ g ↔ f ≤ g := Iff.rfl @[simp] theorem mk_le_mk {f g : α → β} {hf hg} : mk f hf ≤ mk g hg ↔ f ≤ g := Iff.rfl @[mono] theorem apply_mono {f g : α →o β} {x y : α} (h₁ : f ≤ g) (h₂ : x ≤ y) : f x ≤ g y := (h₁ x).trans <| g.mono h₂ /-- Curry/uncurry as an order isomorphism between `α × β →o γ` and `α →o β →o γ`. -/ def curry : (α × β →o γ) ≃o (α →o β →o γ) where toFun f := ⟨fun x ↦ ⟨Function.curry f x, fun _ _ h ↦ f.mono ⟨le_rfl, h⟩⟩, fun _ _ h _ => f.mono ⟨h, le_rfl⟩⟩ invFun f := ⟨Function.uncurry fun x ↦ f x, fun x y h ↦ (f.mono h.1 x.2).trans ((f y.1).mono h.2)⟩ left_inv _ := rfl right_inv _ := rfl map_rel_iff' := by simp [le_def] @[simp] theorem curry_apply (f : α × β →o γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl @[simp] theorem curry_symm_apply (f : α →o β →o γ) (x : α × β) : curry.symm f x = f x.1 x.2 := rfl /-- The composition of two bundled monotone functions. -/ @[simps -fullyApplied] def comp (g : β →o γ) (f : α →o β) : α →o γ := ⟨g ∘ f, g.mono.comp f.mono⟩ @[mono] theorem comp_mono ⦃g₁ g₂ : β →o γ⦄ (hg : g₁ ≤ g₂) ⦃f₁ f₂ : α →o β⦄ (hf : f₁ ≤ f₂) : g₁.comp f₁ ≤ g₂.comp f₂ := fun _ => (hg _).trans (g₂.mono <| hf _) @[simp] lemma mk_comp_mk (g : β → γ) (f : α → β) (hg hf) : comp ⟨g, hg⟩ ⟨f, hf⟩ = ⟨g ∘ f, hg.comp hf⟩ := rfl /-- The composition of two bundled monotone functions, a fully bundled version. -/ @[simps! -fullyApplied] def compₘ : (β →o γ) →o (α →o β) →o α →o γ := curry ⟨fun f : (β →o γ) × (α →o β) => f.1.comp f.2, fun _ _ h => comp_mono h.1 h.2⟩ @[simp] theorem comp_id (f : α →o β) : comp f id = f := by ext rfl @[simp] theorem id_comp (f : α →o β) : comp id f = f := by ext rfl /-- Constant function bundled as an `OrderHom`. -/ @[simps -fullyApplied] def const (α : Type*) [Preorder α] {β : Type*} [Preorder β] : β →o α →o β where toFun b := ⟨Function.const α b, fun _ _ _ => le_rfl⟩ monotone' _ _ h _ := h @[simp] theorem const_comp (f : α →o β) (c : γ) : (const β c).comp f = const α c := rfl @[simp] theorem comp_const (γ : Type*) [Preorder γ] (f : α →o β) (c : α) : f.comp (const γ c) = const γ (f c) := rfl /-- Given two bundled monotone maps `f`, `g`, `f.prod g` is the map `x ↦ (f x, g x)` bundled as a `OrderHom`. -/ @[simps] protected def prod (f : α →o β) (g : α →o γ) : α →o β × γ := ⟨fun x => (f x, g x), fun _ _ h => ⟨f.mono h, g.mono h⟩⟩ @[mono] theorem prod_mono {f₁ f₂ : α →o β} (hf : f₁ ≤ f₂) {g₁ g₂ : α →o γ} (hg : g₁ ≤ g₂) : f₁.prod g₁ ≤ f₂.prod g₂ := fun _ => Prod.le_def.2 ⟨hf _, hg _⟩ theorem comp_prod_comp_same (f₁ f₂ : β →o γ) (g : α →o β) : (f₁.comp g).prod (f₂.comp g) = (f₁.prod f₂).comp g := rfl /-- Given two bundled monotone maps `f`, `g`, `f.prod g` is the map `x ↦ (f x, g x)` bundled as a `OrderHom`. This is a fully bundled version. -/ @[simps!] def prodₘ : (α →o β) →o (α →o γ) →o α →o β × γ := curry ⟨fun f : (α →o β) × (α →o γ) => f.1.prod f.2, fun _ _ h => prod_mono h.1 h.2⟩ /-- Diagonal embedding of `α` into `α × α` as an `OrderHom`. -/ @[simps!] def diag : α →o α × α := id.prod id /-- Restriction of `f : α →o α →o β` to the diagonal. -/ @[simps! +simpRhs] def onDiag (f : α →o α →o β) : α →o β := (curry.symm f).comp diag /-- `Prod.fst` as an `OrderHom`. -/ @[simps] def fst : α × β →o α := ⟨Prod.fst, fun _ _ h => h.1⟩ /-- `Prod.snd` as an `OrderHom`. -/ @[simps] def snd : α × β →o β := ⟨Prod.snd, fun _ _ h => h.2⟩ @[simp] theorem fst_prod_snd : (fst : α × β →o α).prod snd = id := by ext ⟨x, y⟩ : 2 rfl @[simp] theorem fst_comp_prod (f : α →o β) (g : α →o γ) : fst.comp (f.prod g) = f := ext _ _ rfl @[simp] theorem snd_comp_prod (f : α →o β) (g : α →o γ) : snd.comp (f.prod g) = g := ext _ _ rfl /-- Order isomorphism between the space of monotone maps to `β × γ` and the product of the spaces of monotone maps to `β` and `γ`. -/ @[simps] def prodIso : (α →o β × γ) ≃o (α →o β) × (α →o γ) where toFun f := (fst.comp f, snd.comp f) invFun f := f.1.prod f.2 left_inv _ := rfl right_inv _ := rfl map_rel_iff' := forall_and.symm /-- `Prod.map` of two `OrderHom`s as an `OrderHom` -/ @[simps] def prodMap (f : α →o β) (g : γ →o δ) : α × γ →o β × δ := ⟨Prod.map f g, fun _ _ h => ⟨f.mono h.1, g.mono h.2⟩⟩ variable {ι : Type*} {π : ι → Type*} [∀ i, Preorder (π i)] /-- Evaluation of an unbundled function at a point (`Function.eval`) as an `OrderHom`. -/ @[simps -fullyApplied] def _root_.Pi.evalOrderHom (i : ι) : (∀ j, π j) →o π i := ⟨Function.eval i, Function.monotone_eval i⟩ /-- The "forgetful functor" from `α →o β` to `α → β` that takes the underlying function, is monotone. -/ @[simps -fullyApplied] def coeFnHom : (α →o β) →o α → β where toFun f := f monotone' _ _ h := h /-- Function application `fun f => f a` (for fixed `a`) is a monotone function from the monotone function space `α →o β` to `β`. See also `Pi.evalOrderHom`. -/ @[simps! -fullyApplied] def apply (x : α) : (α →o β) →o β := (Pi.evalOrderHom x).comp coeFnHom /-- Construct a bundled monotone map `α →o Π i, π i` from a family of monotone maps `f i : α →o π i`. -/ @[simps] def pi (f : ∀ i, α →o π i) : α →o ∀ i, π i := ⟨fun x i => f i x, fun _ _ h i => (f i).mono h⟩ /-- Order isomorphism between bundled monotone maps `α →o Π i, π i` and families of bundled monotone maps `Π i, α →o π i`. -/ @[simps] def piIso : (α →o ∀ i, π i) ≃o ∀ i, α →o π i where toFun f i := (Pi.evalOrderHom i).comp f invFun := pi left_inv _ := rfl right_inv _ := rfl map_rel_iff' := forall_swap /-- `Subtype.val` as a bundled monotone function. -/ @[simps -fullyApplied] def Subtype.val (p : α → Prop) : Subtype p →o α := ⟨_root_.Subtype.val, fun _ _ h => h⟩ /-- `Subtype.impEmbedding` as an order embedding. -/ @[simps!] def _root_.Subtype.orderEmbedding {p q : α → Prop} (h : ∀ a, p a → q a) : {x // p x} ↪o {x // q x} := { Subtype.impEmbedding _ _ h with map_rel_iff' := by aesop } /-- There is a unique monotone map from a subsingleton to itself. -/ instance unique [Subsingleton α] : Unique (α →o α) where default := OrderHom.id uniq _ := ext _ _ (Subsingleton.elim _ _) theorem orderHom_eq_id [Subsingleton α] (g : α →o α) : g = OrderHom.id := Subsingleton.elim _ _ /-- Reinterpret a bundled monotone function as a monotone function between dual orders. -/ @[simps] protected def dual : (α →o β) ≃ (αᵒᵈ →o βᵒᵈ) where toFun f := ⟨(OrderDual.toDual : β → βᵒᵈ) ∘ (f : α → β) ∘ (OrderDual.ofDual : αᵒᵈ → α), f.mono.dual⟩ invFun f := ⟨OrderDual.ofDual ∘ f ∘ OrderDual.toDual, f.mono.dual⟩ left_inv _ := rfl right_inv _ := rfl @[simp] theorem dual_id : (OrderHom.id : α →o α).dual = OrderHom.id := rfl @[simp] theorem dual_comp (g : β →o γ) (f : α →o β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] theorem symm_dual_id : OrderHom.dual.symm OrderHom.id = (OrderHom.id : α →o α) := rfl @[simp] theorem symm_dual_comp (g : βᵒᵈ →o γᵒᵈ) (f : αᵒᵈ →o βᵒᵈ) : OrderHom.dual.symm (g.comp f) = (OrderHom.dual.symm g).comp (OrderHom.dual.symm f) := rfl /-- `OrderHom.dual` as an order isomorphism. -/ def dualIso (α β : Type*) [Preorder α] [Preorder β] : (α →o β) ≃o (αᵒᵈ →o βᵒᵈ)ᵒᵈ where toEquiv := OrderHom.dual.trans OrderDual.toDual map_rel_iff' := Iff.rfl /-- Lift an order homomorphism `f : α →o β` to an order homomorphism `ULift α →o ULift β` in a higher universe. -/ @[simps!] def uliftMap (f : α →o β) : ULift α →o ULift β := ⟨fun i => ⟨f i.down⟩, fun _ _ h ↦ f.monotone h⟩ end OrderHom -- See note [lower instance priority] instance (priority := 90) OrderHomClass.toOrderHomClassOrderDual [LE α] [LE β] [FunLike F α β] [OrderHomClass F α β] : OrderHomClass F αᵒᵈ βᵒᵈ where map_rel f := map_rel f /-- Embeddings of partial orders that preserve `<` also preserve `≤`. -/ def RelEmbedding.orderEmbeddingOfLTEmbedding [PartialOrder α] [PartialOrder β] (f : ((· < ·) : α → α → Prop) ↪r ((· < ·) : β → β → Prop)) : α ↪o β := { f with map_rel_iff' := by intros simp [le_iff_lt_or_eq, f.map_rel_iff, f.injective.eq_iff] } @[simp] theorem RelEmbedding.orderEmbeddingOfLTEmbedding_apply [PartialOrder α] [PartialOrder β] {f : ((· < ·) : α → α → Prop) ↪r ((· < ·) : β → β → Prop)} {x : α} : RelEmbedding.orderEmbeddingOfLTEmbedding f x = f x := rfl namespace OrderEmbedding variable [Preorder α] [Preorder β] (f : α ↪o β) /-- `<` is preserved by order embeddings of preorders. -/ def ltEmbedding : ((· < ·) : α → α → Prop) ↪r ((· < ·) : β → β → Prop) := { f with map_rel_iff' := by intros; simp [lt_iff_le_not_le, f.map_rel_iff] } @[simp] theorem ltEmbedding_apply (x : α) : f.ltEmbedding x = f x := rfl @[simp] theorem le_iff_le {a b} : f a ≤ f b ↔ a ≤ b := f.map_rel_iff @[simp] theorem lt_iff_lt {a b} : f a < f b ↔ a < b := f.ltEmbedding.map_rel_iff theorem eq_iff_eq {a b} : f a = f b ↔ a = b := f.injective.eq_iff protected theorem monotone : Monotone f := OrderHomClass.monotone f protected theorem strictMono : StrictMono f := fun _ _ => f.lt_iff_lt.2 protected theorem acc (a : α) : Acc (· < ·) (f a) → Acc (· < ·) a := f.ltEmbedding.acc a protected theorem wellFounded (f : α ↪o β) : WellFounded ((· < ·) : β → β → Prop) → WellFounded ((· < ·) : α → α → Prop) := f.ltEmbedding.wellFounded protected theorem isWellOrder [IsWellOrder β (· < ·)] (f : α ↪o β) : IsWellOrder α (· < ·) := f.ltEmbedding.isWellOrder /-- An order embedding is also an order embedding between dual orders. -/ protected def dual : αᵒᵈ ↪o βᵒᵈ := ⟨f.toEmbedding, f.map_rel_iff⟩ /-- A preorder which embeds into a well-founded preorder is itself well-founded. -/ protected theorem wellFoundedLT [WellFoundedLT β] (f : α ↪o β) : WellFoundedLT α where wf := f.wellFounded IsWellFounded.wf /-- A preorder which embeds into a preorder in which `(· > ·)` is well-founded also has `(· > ·)` well-founded. -/ protected theorem wellFoundedGT [WellFoundedGT β] (f : α ↪o β) : WellFoundedGT α := @OrderEmbedding.wellFoundedLT αᵒᵈ _ _ _ _ f.dual /-- To define an order embedding from a partial order to a preorder it suffices to give a function together with a proof that it satisfies `f a ≤ f b ↔ a ≤ b`. -/ def ofMapLEIff {α β} [PartialOrder α] [Preorder β] (f : α → β) (hf : ∀ a b, f a ≤ f b ↔ a ≤ b) : α ↪o β := RelEmbedding.ofMapRelIff f hf @[simp] theorem coe_ofMapLEIff {α β} [PartialOrder α] [Preorder β] {f : α → β} (h) : ⇑(ofMapLEIff f h) = f := rfl /-- A strictly monotone map from a linear order is an order embedding. -/ def ofStrictMono {α β} [LinearOrder α] [Preorder β] (f : α → β) (h : StrictMono f) : α ↪o β := ofMapLEIff f fun _ _ => h.le_iff_le @[simp] theorem coe_ofStrictMono {α β} [LinearOrder α] [Preorder β] {f : α → β} (h : StrictMono f) : ⇑(ofStrictMono f h) = f := rfl /-- Embedding of a subtype into the ambient type as an `OrderEmbedding`. -/ def subtype (p : α → Prop) : Subtype p ↪o α := ⟨Function.Embedding.subtype p, Iff.rfl⟩ @[simp] theorem subtype_apply {p : α → Prop} (x : Subtype p) : subtype p x = x := rfl theorem subtype_injective (p : α → Prop) : Function.Injective (subtype p) := Subtype.coe_injective @[simp] theorem coe_subtype (p : α → Prop) : ⇑(subtype p) = Subtype.val := rfl /-- Convert an `OrderEmbedding` to an `OrderHom`. -/ @[simps -fullyApplied] def toOrderHom {X Y : Type*} [Preorder X] [Preorder Y] (f : X ↪o Y) : X →o Y where toFun := f monotone' := f.monotone /-- The trivial embedding from an empty preorder to another preorder -/ @[simps] def ofIsEmpty [IsEmpty α] : α ↪o β where toFun := isEmptyElim inj' := isEmptyElim map_rel_iff' {a} := isEmptyElim a @[simp, norm_cast] lemma coe_ofIsEmpty [IsEmpty α] : (ofIsEmpty : α ↪o β) = (isEmptyElim : α → β) := rfl end OrderEmbedding section Disjoint variable [PartialOrder α] [PartialOrder β] (f : OrderEmbedding α β) /-- If the images by an order embedding of two elements are disjoint, then they are themselves disjoint. -/ lemma Disjoint.of_orderEmbedding [OrderBot α] [OrderBot β] {a₁ a₂ : α} : Disjoint (f a₁) (f a₂) → Disjoint a₁ a₂ := by intro h x h₁ h₂ rw [← f.le_iff_le] at h₁ h₂ ⊢ calc f x ≤ ⊥ := h h₁ h₂ _ ≤ f ⊥ := bot_le /-- If the images by an order embedding of two elements are codisjoint, then they are themselves codisjoint. -/ lemma Codisjoint.of_orderEmbedding [OrderTop α] [OrderTop β] {a₁ a₂ : α} : Codisjoint (f a₁) (f a₂) → Codisjoint a₁ a₂ := Disjoint.of_orderEmbedding (α := αᵒᵈ) (β := βᵒᵈ) f.dual /-- If the images by an order embedding of two elements are complements, then they are themselves complements. -/ lemma IsCompl.of_orderEmbedding [BoundedOrder α] [BoundedOrder β] {a₁ a₂ : α} : IsCompl (f a₁) (f a₂) → IsCompl a₁ a₂ := fun ⟨hd, hcd⟩ ↦ ⟨Disjoint.of_orderEmbedding f hd, Codisjoint.of_orderEmbedding f hcd⟩ end Disjoint section RelHom variable [PartialOrder α] [Preorder β] namespace RelHom variable (f : ((· < ·) : α → α → Prop) →r ((· < ·) : β → β → Prop)) /-- A bundled expression of the fact that a map between partial orders that is strictly monotone is weakly monotone. -/ @[simps -fullyApplied] def toOrderHom : α →o β where toFun := f monotone' := StrictMono.monotone fun _ _ => f.map_rel end RelHom theorem RelEmbedding.toOrderHom_injective (f : ((· < ·) : α → α → Prop) ↪r ((· < ·) : β → β → Prop)) : Function.Injective (f : ((· < ·) : α → α → Prop) →r ((· < ·) : β → β → Prop)).toOrderHom := fun _ _ h => f.injective h end RelHom namespace OrderIso section LE variable [LE α] [LE β] [LE γ] instance : EquivLike (α ≃o β) α β where coe f := f.toFun inv f := f.invFun left_inv f := f.left_inv right_inv f := f.right_inv coe_injective' f g h₁ h₂ := by obtain ⟨⟨_, _⟩, _⟩ := f obtain ⟨⟨_, _⟩, _⟩ := g congr instance : OrderIsoClass (α ≃o β) α β where map_le_map_iff f _ _ := f.map_rel_iff' @[simp] theorem toFun_eq_coe {f : α ≃o β} : f.toFun = f := rfl -- See note [partially-applied ext lemmas] @[ext] theorem ext {f g : α ≃o β} (h : (f : α → β) = g) : f = g := DFunLike.coe_injective h /-- Reinterpret an order isomorphism as an order embedding. -/ def toOrderEmbedding (e : α ≃o β) : α ↪o β := e.toRelEmbedding @[simp] theorem coe_toOrderEmbedding (e : α ≃o β) : ⇑e.toOrderEmbedding = e := rfl protected theorem bijective (e : α ≃o β) : Function.Bijective e := e.toEquiv.bijective protected theorem injective (e : α ≃o β) : Function.Injective e := e.toEquiv.injective protected theorem surjective (e : α ≃o β) : Function.Surjective e := e.toEquiv.surjective theorem apply_eq_iff_eq (e : α ≃o β) {x y : α} : e x = e y ↔ x = y := e.toEquiv.apply_eq_iff_eq /-- Identity order isomorphism. -/ def refl (α : Type*) [LE α] : α ≃o α := RelIso.refl (· ≤ ·) @[simp] theorem coe_refl : ⇑(refl α) = id := rfl @[simp] theorem refl_apply (x : α) : refl α x = x := rfl @[simp] theorem refl_toEquiv : (refl α).toEquiv = Equiv.refl α := rfl /-- Inverse of an order isomorphism. -/ def symm (e : α ≃o β) : β ≃o α := RelIso.symm e @[simp] theorem apply_symm_apply (e : α ≃o β) (x : β) : e (e.symm x) = x := e.toEquiv.apply_symm_apply x @[simp] theorem symm_apply_apply (e : α ≃o β) (x : α) : e.symm (e x) = x := e.toEquiv.symm_apply_apply x @[simp] theorem symm_refl (α : Type*) [LE α] : (refl α).symm = refl α := rfl theorem apply_eq_iff_eq_symm_apply (e : α ≃o β) (x : α) (y : β) : e x = y ↔ x = e.symm y := e.toEquiv.apply_eq_iff_eq_symm_apply theorem symm_apply_eq (e : α ≃o β) {x : α} {y : β} : e.symm y = x ↔ y = e x := e.toEquiv.symm_apply_eq @[simp] theorem symm_symm (e : α ≃o β) : e.symm.symm = e := rfl theorem symm_bijective : Function.Bijective (OrderIso.symm : (α ≃o β) → β ≃o α) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ theorem symm_injective : Function.Injective (symm : α ≃o β → β ≃o α) := symm_bijective.injective @[simp] theorem toEquiv_symm (e : α ≃o β) : e.toEquiv.symm = e.symm.toEquiv := rfl /-- Composition of two order isomorphisms is an order isomorphism. -/ @[trans] def trans (e : α ≃o β) (e' : β ≃o γ) : α ≃o γ := RelIso.trans e e' @[simp] theorem coe_trans (e : α ≃o β) (e' : β ≃o γ) : ⇑(e.trans e') = e' ∘ e := rfl @[simp] theorem trans_apply (e : α ≃o β) (e' : β ≃o γ) (x : α) : e.trans e' x = e' (e x) := rfl @[simp] theorem refl_trans (e : α ≃o β) : (refl α).trans e = e := by ext x rfl @[simp] theorem trans_refl (e : α ≃o β) : e.trans (refl β) = e := by ext x rfl @[simp] theorem symm_trans_apply (e₁ : α ≃o β) (e₂ : β ≃o γ) (c : γ) : (e₁.trans e₂).symm c = e₁.symm (e₂.symm c) := rfl theorem symm_trans (e₁ : α ≃o β) (e₂ : β ≃o γ) : (e₁.trans e₂).symm = e₂.symm.trans e₁.symm := rfl @[simp] theorem self_trans_symm (e : α ≃o β) : e.trans e.symm = OrderIso.refl α := RelIso.self_trans_symm e @[simp] theorem symm_trans_self (e : α ≃o β) : e.symm.trans e = OrderIso.refl β := RelIso.symm_trans_self e /-- An order isomorphism between the domains and codomains of two prosets of order homomorphisms gives an order isomorphism between the two function prosets. -/ @[simps apply symm_apply] def arrowCongr {α β γ δ} [Preorder α] [Preorder β] [Preorder γ] [Preorder δ] (f : α ≃o γ) (g : β ≃o δ) : (α →o β) ≃o (γ →o δ) where toFun p := .comp g <| .comp p f.symm invFun p := .comp g.symm <| .comp p f left_inv p := DFunLike.coe_injective <| by change (g.symm ∘ g) ∘ p ∘ (f.symm ∘ f) = p simp only [← DFunLike.coe_eq_coe_fn, ← OrderIso.coe_trans, Function.id_comp, OrderIso.self_trans_symm, OrderIso.coe_refl, Function.comp_id] right_inv p := DFunLike.coe_injective <| by change (g ∘ g.symm) ∘ p ∘ (f ∘ f.symm) = p simp only [← DFunLike.coe_eq_coe_fn, ← OrderIso.coe_trans, Function.id_comp, OrderIso.symm_trans_self, OrderIso.coe_refl, Function.comp_id] map_rel_iff' {p q} := by simp only [Equiv.coe_fn_mk, OrderHom.le_def, OrderHom.comp_coe, OrderHomClass.coe_coe, Function.comp_apply, map_le_map_iff] exact Iff.symm f.forall_congr_left /-- If `α` and `β` are order-isomorphic then the two orders of order-homomorphisms from `α` and `β` to themselves are order-isomorphic. -/ @[simps! apply symm_apply] def conj {α β} [Preorder α] [Preorder β] (f : α ≃o β) : (α →o α) ≃ (β →o β) := arrowCongr f f /-- `Prod.swap` as an `OrderIso`. -/ def prodComm : α × β ≃o β × α where toEquiv := Equiv.prodComm α β map_rel_iff' := Prod.swap_le_swap @[simp] theorem coe_prodComm : ⇑(prodComm : α × β ≃o β × α) = Prod.swap := rfl @[simp] theorem prodComm_symm : (prodComm : α × β ≃o β × α).symm = prodComm := rfl variable (α) /-- The order isomorphism between a type and its double dual. -/ def dualDual : α ≃o αᵒᵈᵒᵈ := refl α @[simp] theorem coe_dualDual : ⇑(dualDual α) = toDual ∘ toDual := rfl @[simp] theorem coe_dualDual_symm : ⇑(dualDual α).symm = ofDual ∘ ofDual := rfl variable {α} @[simp] theorem dualDual_apply (a : α) : dualDual α a = toDual (toDual a) := rfl @[simp] theorem dualDual_symm_apply (a : αᵒᵈᵒᵈ) : (dualDual α).symm a = ofDual (ofDual a) := rfl end LE open Set section LE variable [LE α] [LE β] theorem le_iff_le (e : α ≃o β) {x y : α} : e x ≤ e y ↔ x ≤ y := e.map_rel_iff @[gcongr] protected alias ⟨_, GCongr.orderIso_apply_le_apply⟩ := le_iff_le theorem le_symm_apply (e : α ≃o β) {x : α} {y : β} : x ≤ e.symm y ↔ e x ≤ y := e.rel_symm_apply theorem symm_apply_le (e : α ≃o β) {x : α} {y : β} : e.symm y ≤ x ↔ y ≤ e x := e.symm_apply_rel end LE variable [Preorder α] [Preorder β] protected theorem monotone (e : α ≃o β) : Monotone e := e.toOrderEmbedding.monotone protected theorem strictMono (e : α ≃o β) : StrictMono e := e.toOrderEmbedding.strictMono @[simp] theorem lt_iff_lt (e : α ≃o β) {x y : α} : e x < e y ↔ x < y := e.toOrderEmbedding.lt_iff_lt @[gcongr] protected alias ⟨_, GCongr.orderIso_apply_lt_apply⟩ := lt_iff_lt theorem lt_symm_apply (e : α ≃o β) {x : α} {y : β} : x < e.symm y ↔ e x < y := by rw [← e.lt_iff_lt, e.apply_symm_apply] theorem symm_apply_lt (e : α ≃o β) {x : α} {y : β} : e.symm y < x ↔ y < e x := by rw [← e.lt_iff_lt, e.apply_symm_apply] /-- Converts an `OrderIso` into a `RelIso (<) (<)`. -/ def toRelIsoLT (e : α ≃o β) : ((· < ·) : α → α → Prop) ≃r ((· < ·) : β → β → Prop) := ⟨e.toEquiv, lt_iff_lt e⟩ @[simp] theorem toRelIsoLT_apply (e : α ≃o β) (x : α) : e.toRelIsoLT x = e x := rfl @[simp] theorem toRelIsoLT_symm (e : α ≃o β) : e.toRelIsoLT.symm = e.symm.toRelIsoLT := rfl /-- Converts a `RelIso (<) (<)` into an `OrderIso`. -/ def ofRelIsoLT {α β} [PartialOrder α] [PartialOrder β] (e : ((· < ·) : α → α → Prop) ≃r ((· < ·) : β → β → Prop)) : α ≃o β := ⟨e.toEquiv, by simp [le_iff_eq_or_lt, e.map_rel_iff, e.injective.eq_iff]⟩ @[simp] theorem ofRelIsoLT_apply {α β} [PartialOrder α] [PartialOrder β] (e : ((· < ·) : α → α → Prop) ≃r ((· < ·) : β → β → Prop)) (x : α) : ofRelIsoLT e x = e x := rfl @[simp] theorem ofRelIsoLT_symm {α β} [PartialOrder α] [PartialOrder β] (e : ((· < ·) : α → α → Prop) ≃r ((· < ·) : β → β → Prop)) : (ofRelIsoLT e).symm = ofRelIsoLT e.symm := rfl @[simp] theorem ofRelIsoLT_toRelIsoLT {α β} [PartialOrder α] [PartialOrder β] (e : α ≃o β) : ofRelIsoLT (toRelIsoLT e) = e := by ext simp @[simp] theorem toRelIsoLT_ofRelIsoLT {α β} [PartialOrder α] [PartialOrder β] (e : ((· < ·) : α → α → Prop) ≃r ((· < ·) : β → β → Prop)) : toRelIsoLT (ofRelIsoLT e) = e := by ext simp /-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders, it suffices to prove `cmp a (g b) = cmp (f a) b`. -/ def ofCmpEqCmp {α β} [LinearOrder α] [LinearOrder β] (f : α → β) (g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β := have gf : ∀ a : α, a = g (f a) := by intro rw [← cmp_eq_eq_iff, h, cmp_self_eq_eq] { toFun := f, invFun := g, left_inv := fun a => (gf a).symm, right_inv := by intro rw [← cmp_eq_eq_iff, ← h, cmp_self_eq_eq], map_rel_iff' := by intros a b apply le_iff_le_of_cmp_eq_cmp convert (h a (f b)).symm apply gf } /-- To show that `f : α →o β` and `g : β →o α` make up an order isomorphism it is enough to show that `g` is the inverse of `f`. -/ def ofHomInv {F G : Type*} [FunLike F α β] [OrderHomClass F α β] [FunLike G β α] [OrderHomClass G β α] (f : F) (g : G) (h₁ : (f : α →o β).comp (g : β →o α) = OrderHom.id) (h₂ : (g : β →o α).comp (f : α →o β) = OrderHom.id) : α ≃o β where toFun := f invFun := g left_inv := DFunLike.congr_fun h₂ right_inv := DFunLike.congr_fun h₁ map_rel_iff' := @fun a b => ⟨fun h => by replace h := map_rel g h rwa [Equiv.coe_fn_mk, show g (f a) = (g : β →o α).comp (f : α →o β) a from rfl, show g (f b) = (g : β →o α).comp (f : α →o β) b from rfl, h₂] at h, fun h => (f : α →o β).monotone h⟩ /-- Order isomorphism between `α → β` and `β`, where `α` has a unique element. -/ @[simps! toEquiv apply] def funUnique (α β : Type*) [Unique α] [Preorder β] : (α → β) ≃o β where toEquiv := Equiv.funUnique α β map_rel_iff' := by simp [Pi.le_def, Unique.forall_iff] @[simp] theorem funUnique_symm_apply {α β : Type*} [Unique α] [Preorder β] : ((funUnique α β).symm : β → α → β) = Function.const α := rfl /-- The order isomorphism `α ≃o β` when `α` and `β` are preordered types containing unique elements. -/ @[simps!] noncomputable def ofUnique (α β : Type*) [Unique α] [Unique β] [Preorder α] [Preorder β] : α ≃o β where toEquiv := Equiv.ofUnique α β map_rel_iff' := by simp end OrderIso namespace Equiv variable [Preorder α] [Preorder β] /-- If `e` is an equivalence with monotone forward and inverse maps, then `e` is an order isomorphism. -/ def toOrderIso (e : α ≃ β) (h₁ : Monotone e) (h₂ : Monotone e.symm) : α ≃o β := ⟨e, ⟨fun h => by simpa only [e.symm_apply_apply] using h₂ h, fun h => h₁ h⟩⟩ @[simp] theorem coe_toOrderIso (e : α ≃ β) (h₁ : Monotone e) (h₂ : Monotone e.symm) : ⇑(e.toOrderIso h₁ h₂) = e := rfl @[simp] theorem toOrderIso_toEquiv (e : α ≃ β) (h₁ : Monotone e) (h₂ : Monotone e.symm) : (e.toOrderIso h₁ h₂).toEquiv = e := rfl end Equiv namespace StrictMono variable [LinearOrder α] [Preorder β] variable (f : α → β) (h_mono : StrictMono f) /-- A strictly monotone function with a right inverse is an order isomorphism. -/ @[simps -fullyApplied] def orderIsoOfRightInverse (g : β → α) (hg : Function.RightInverse g f) : α ≃o β := { OrderEmbedding.ofStrictMono f h_mono with toFun := f, invFun := g, left_inv := fun _ => h_mono.injective <| hg _, right_inv := hg } end StrictMono /-- An order isomorphism is also an order isomorphism between dual orders. -/ protected def OrderIso.dual [LE α] [LE β] (f : α ≃o β) : αᵒᵈ ≃o βᵒᵈ := ⟨f.toEquiv, f.map_rel_iff⟩ section LatticeIsos theorem OrderIso.map_bot' [LE α] [PartialOrder β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x ≤ x') (hy : ∀ y', y ≤ y') : f x = y := by refine le_antisymm ?_ (hy _) rw [← f.apply_symm_apply y, f.map_rel_iff] apply hx theorem OrderIso.map_bot [LE α] [PartialOrder β] [OrderBot α] [OrderBot β] (f : α ≃o β) : f ⊥ = ⊥ := f.map_bot' (fun _ => bot_le) fun _ => bot_le theorem OrderIso.map_top' [LE α] [PartialOrder β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x' ≤ x) (hy : ∀ y', y' ≤ y) : f x = y := f.dual.map_bot' hx hy theorem OrderIso.map_top [LE α] [PartialOrder β] [OrderTop α] [OrderTop β] (f : α ≃o β) : f ⊤ = ⊤ := f.dual.map_bot theorem OrderEmbedding.map_inf_le [SemilatticeInf α] [SemilatticeInf β] (f : α ↪o β) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := f.monotone.map_inf_le x y theorem OrderEmbedding.le_map_sup [SemilatticeSup α] [SemilatticeSup β] (f : α ↪o β) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := f.monotone.le_map_sup x y theorem OrderIso.map_inf [SemilatticeInf α] [SemilatticeInf β] (f : α ≃o β) (x y : α) : f (x ⊓ y) = f x ⊓ f y := by refine (f.toOrderEmbedding.map_inf_le x y).antisymm ?_ apply f.symm.le_iff_le.1 simpa using f.symm.toOrderEmbedding.map_inf_le (f x) (f y) theorem OrderIso.map_sup [SemilatticeSup α] [SemilatticeSup β] (f : α ≃o β) (x y : α) : f (x ⊔ y) = f x ⊔ f y := f.dual.map_inf x y theorem OrderIso.isMax_apply {α β : Type*} [Preorder α] [Preorder β] (f : α ≃o β) {x : α} : IsMax (f x) ↔ IsMax x := by refine ⟨f.strictMono.isMax_of_apply, ?_⟩ conv_lhs => rw [← f.symm_apply_apply x] exact f.symm.strictMono.isMax_of_apply theorem OrderIso.isMin_apply {α β : Type*} [Preorder α] [Preorder β] (f : α ≃o β) {x : α} : IsMin (f x) ↔ IsMin x := by refine ⟨f.strictMono.isMin_of_apply, ?_⟩ conv_lhs => rw [← f.symm_apply_apply x] exact f.symm.strictMono.isMin_of_apply /-- Note that this goal could also be stated `(Disjoint on f) a b` -/ theorem Disjoint.map_orderIso [SemilatticeInf α] [OrderBot α] [SemilatticeInf β] [OrderBot β] {a b : α} (f : α ≃o β) (ha : Disjoint a b) : Disjoint (f a) (f b) := by rw [disjoint_iff_inf_le, ← f.map_inf, ← f.map_bot] exact f.monotone ha.le_bot
/-- Note that this goal could also be stated `(Codisjoint on f) a b` -/ theorem Codisjoint.map_orderIso [SemilatticeSup α] [OrderTop α] [SemilatticeSup β] [OrderTop β] {a b : α} (f : α ≃o β) (ha : Codisjoint a b) : Codisjoint (f a) (f b) := by rw [codisjoint_iff_le_sup, ← f.map_sup, ← f.map_top]
Mathlib/Order/Hom/Basic.lean
1,125
1,128
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Yury Kudryashov, Neil Strickland -/ import Mathlib.Algebra.Group.Defs import Mathlib.Algebra.GroupWithZero.Defs import Mathlib.Data.Int.Cast.Defs import Mathlib.Tactic.Spread import Mathlib.Util.AssertExists import Mathlib.Tactic.StacksAttribute /-! # Semirings and rings This file defines semirings, rings and domains. This is analogous to `Algebra.Group.Defs` and `Algebra.Group.Basic`, the difference being that the former is about `+` and `*` separately, while the present file is about their interaction. ## Main definitions * `Distrib`: Typeclass for distributivity of multiplication over addition. * `HasDistribNeg`: Typeclass for commutativity of negation and multiplication. This is useful when dealing with multiplicative submonoids which are closed under negation without being closed under addition, for example `Units`. * `(NonUnital)(NonAssoc)(Semi)Ring`: Typeclasses for possibly non-unital or non-associative rings and semirings. Some combinations are not defined yet because they haven't found use. For Lie Rings, there is a type synonym `CommutatorRing` defined in `Mathlib/Algebra/Algebra/NonUnitalHom.lean` turning the bracket into a multiplication so that the instance `instNonUnitalNonAssocSemiringCommutatorRing` can be defined. ## Tags `Semiring`, `CommSemiring`, `Ring`, `CommRing`, domain, `IsDomain`, nonzero, units -/ /-! Previously an import dependency on `Mathlib.Algebra.Group.Basic` had crept in. In general, the `.Defs` files in the basic algebraic hierarchy should only depend on earlier `.Defs` files, without importing `.Basic` theory development. These `assert_not_exists` statements guard against this returning. -/ assert_not_exists DivisionMonoid.toDivInvOneMonoid mul_rotate universe u v variable {α : Type u} {R : Type v} open Function /-! ### `Distrib` class -/ /-- A typeclass stating that multiplication is left and right distributive over addition. -/ class Distrib (R : Type*) extends Mul R, Add R where /-- Multiplication is left distributive over addition -/ protected left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c /-- Multiplication is right distributive over addition -/ protected right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c /-- A typeclass stating that multiplication is left distributive over addition. -/ class LeftDistribClass (R : Type*) [Mul R] [Add R] : Prop where /-- Multiplication is left distributive over addition -/ protected left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c /-- A typeclass stating that multiplication is right distributive over addition. -/ class RightDistribClass (R : Type*) [Mul R] [Add R] : Prop where /-- Multiplication is right distributive over addition -/ protected right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c -- see Note [lower instance priority] instance (priority := 100) Distrib.leftDistribClass (R : Type*) [Distrib R] : LeftDistribClass R := ⟨Distrib.left_distrib⟩ -- see Note [lower instance priority] instance (priority := 100) Distrib.rightDistribClass (R : Type*) [Distrib R] : RightDistribClass R := ⟨Distrib.right_distrib⟩ theorem left_distrib [Mul R] [Add R] [LeftDistribClass R] (a b c : R) : a * (b + c) = a * b + a * c := LeftDistribClass.left_distrib a b c alias mul_add := left_distrib theorem right_distrib [Mul R] [Add R] [RightDistribClass R] (a b c : R) : (a + b) * c = a * c + b * c := RightDistribClass.right_distrib a b c alias add_mul := right_distrib theorem distrib_three_right [Mul R] [Add R] [RightDistribClass R] (a b c d : R) : (a + b + c) * d = a * d + b * d + c * d := by simp [right_distrib] /-! ### Classes of semirings and rings We make sure that the canonical path from `NonAssocSemiring` to `Ring` passes through `Semiring`, as this is a path which is followed all the time in linear algebra where the defining semilinear map `σ : R →+* S` depends on the `NonAssocSemiring` structure of `R` and `S` while the module definition depends on the `Semiring` structure. It is not currently possible to adjust priorities by hand (see https://github.com/leanprover/lean4/issues/2115). Instead, the last declared instance is used, so we make sure that `Semiring` is declared after `NonAssocRing`, so that `Semiring -> NonAssocSemiring` is tried before `NonAssocRing -> NonAssocSemiring`. TODO: clean this once https://github.com/leanprover/lean4/issues/2115 is fixed -/ /-- A not-necessarily-unital, not-necessarily-associative semiring. See `CommutatorRing` and the documentation thereof in case you need a `NonUnitalNonAssocSemiring` instance on a Lie ring or a Lie algebra. -/ class NonUnitalNonAssocSemiring (α : Type u) extends AddCommMonoid α, Distrib α, MulZeroClass α /-- An associative but not-necessarily unital semiring. -/ class NonUnitalSemiring (α : Type u) extends NonUnitalNonAssocSemiring α, SemigroupWithZero α /-- A unital but not-necessarily-associative semiring. -/ class NonAssocSemiring (α : Type u) extends NonUnitalNonAssocSemiring α, MulZeroOneClass α, AddCommMonoidWithOne α /-- A not-necessarily-unital, not-necessarily-associative ring. -/ class NonUnitalNonAssocRing (α : Type u) extends AddCommGroup α, NonUnitalNonAssocSemiring α /-- An associative but not-necessarily unital ring. -/ class NonUnitalRing (α : Type*) extends NonUnitalNonAssocRing α, NonUnitalSemiring α /-- A unital but not-necessarily-associative ring. -/ class NonAssocRing (α : Type*) extends NonUnitalNonAssocRing α, NonAssocSemiring α, AddCommGroupWithOne α /-- A `Semiring` is a type with addition, multiplication, a `0` and a `1` where addition is commutative and associative, multiplication is associative and left and right distributive over addition, and `0` and `1` are additive and multiplicative identities. -/ class Semiring (α : Type u) extends NonUnitalSemiring α, NonAssocSemiring α, MonoidWithZero α /-- A `Ring` is a `Semiring` with negation making it an additive group. -/ class Ring (R : Type u) extends Semiring R, AddCommGroup R, AddGroupWithOne R /-! ### Semirings -/ section DistribMulOneClass variable [Add α] [MulOneClass α] theorem add_one_mul [RightDistribClass α] (a b : α) : (a + 1) * b = a * b + b := by rw [add_mul, one_mul] theorem mul_add_one [LeftDistribClass α] (a b : α) : a * (b + 1) = a * b + a := by rw [mul_add, mul_one] theorem one_add_mul [RightDistribClass α] (a b : α) : (1 + a) * b = b + a * b := by rw [add_mul, one_mul] theorem mul_one_add [LeftDistribClass α] (a b : α) : a * (1 + b) = a + a * b := by rw [mul_add, mul_one] end DistribMulOneClass section NonAssocSemiring variable [NonAssocSemiring α] -- Porting note: was [has_add α] [mul_one_class α] [right_distrib_class α] theorem two_mul (n : α) : 2 * n = n + n := (congrArg₂ _ one_add_one_eq_two.symm rfl).trans <| (right_distrib 1 1 n).trans (by rw [one_mul]) -- Porting note: was [has_add α] [mul_one_class α] [left_distrib_class α] theorem mul_two (n : α) : n * 2 = n + n := (congrArg₂ _ rfl one_add_one_eq_two.symm).trans <| (left_distrib n 1 1).trans (by rw [mul_one]) end NonAssocSemiring section MulZeroClass variable [MulZeroClass α] (P Q : Prop) [Decidable P] [Decidable Q] (a b : α) lemma ite_zero_mul : ite P a 0 * b = ite P (a * b) 0 := by simp lemma mul_ite_zero : a * ite P b 0 = ite P (a * b) 0 := by simp lemma ite_zero_mul_ite_zero : ite P a 0 * ite Q b 0 = ite (P ∧ Q) (a * b) 0 := by simp only [← ite_and, ite_mul, mul_ite, mul_zero, zero_mul, and_comm] end MulZeroClass theorem mul_boole {α} [MulZeroOneClass α] (P : Prop) [Decidable P] (a : α) : (a * if P then 1 else 0) = if P then a else 0 := by simp theorem boole_mul {α} [MulZeroOneClass α] (P : Prop) [Decidable P] (a : α) : (if P then 1 else 0) * a = if P then a else 0 := by simp /-- A not-necessarily-unital, not-necessarily-associative, but commutative semiring. -/ class NonUnitalNonAssocCommSemiring (α : Type u) extends NonUnitalNonAssocSemiring α, CommMagma α /-- A non-unital commutative semiring is a `NonUnitalSemiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`AddCommMonoid`), commutative semigroup (`CommSemigroup`), distributive laws (`Distrib`), and multiplication by zero law (`MulZeroClass`). -/ class NonUnitalCommSemiring (α : Type u) extends NonUnitalSemiring α, CommSemigroup α /-- A commutative semiring is a semiring with commutative multiplication. -/ class CommSemiring (R : Type u) extends Semiring R, CommMonoid R -- see Note [lower instance priority] instance (priority := 100) CommSemiring.toNonUnitalCommSemiring [CommSemiring α] : NonUnitalCommSemiring α := { inferInstanceAs (CommMonoid α), inferInstanceAs (CommSemiring α) with } -- see Note [lower instance priority] instance (priority := 100) CommSemiring.toCommMonoidWithZero [CommSemiring α] : CommMonoidWithZero α := { inferInstanceAs (CommMonoid α), inferInstanceAs (CommSemiring α) with } section CommSemiring variable [CommSemiring α] theorem add_mul_self_eq (a b : α) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b] lemma add_sq (a b : α) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by simp only [sq, add_mul_self_eq] lemma add_sq' (a b : α) : (a + b) ^ 2 = a ^ 2 + b ^ 2 + 2 * a * b := by rw [add_sq, add_assoc, add_comm _ (b ^ 2), add_assoc] alias add_pow_two := add_sq end CommSemiring section HasDistribNeg /-- Typeclass for a negation operator that distributes across multiplication. This is useful for dealing with submonoids of a ring that contain `-1` without having to duplicate lemmas. -/ class HasDistribNeg (α : Type*) [Mul α] extends InvolutiveNeg α where /-- Negation is left distributive over multiplication -/ neg_mul : ∀ x y : α, -x * y = -(x * y) /-- Negation is right distributive over multiplication -/ mul_neg : ∀ x y : α, x * -y = -(x * y) section Mul variable [Mul α] [HasDistribNeg α] @[simp] theorem neg_mul (a b : α) : -a * b = -(a * b) := HasDistribNeg.neg_mul _ _ @[simp] theorem mul_neg (a b : α) : a * -b = -(a * b) := HasDistribNeg.mul_neg _ _ theorem neg_mul_neg (a b : α) : -a * -b = a * b := by simp theorem neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b := (neg_mul _ _).symm theorem neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b := (mul_neg _ _).symm theorem neg_mul_comm (a b : α) : -a * b = a * -b := by simp end Mul section MulOneClass variable [MulOneClass α] [HasDistribNeg α] theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a := by simp /-- An element of a ring multiplied by the additive inverse of one is the element's additive inverse. -/ theorem mul_neg_one (a : α) : a * -1 = -a := by simp /-- The additive inverse of one multiplied by an element of a ring is the element's additive inverse. -/ theorem neg_one_mul (a : α) : -1 * a = -a := by simp end MulOneClass section MulZeroClass variable [MulZeroClass α] [HasDistribNeg α] instance (priority := 100) MulZeroClass.negZeroClass : NegZeroClass α where __ := inferInstanceAs (Zero α); __ := inferInstanceAs (InvolutiveNeg α) neg_zero := by rw [← zero_mul (0 : α), ← neg_mul, mul_zero, mul_zero] end MulZeroClass end HasDistribNeg /-! ### Rings -/ section NonUnitalNonAssocRing variable [NonUnitalNonAssocRing α] instance (priority := 100) NonUnitalNonAssocRing.toHasDistribNeg : HasDistribNeg α where neg := Neg.neg neg_neg := neg_neg neg_mul a b := eq_neg_of_add_eq_zero_left <| by rw [← right_distrib, neg_add_cancel, zero_mul] mul_neg a b := eq_neg_of_add_eq_zero_left <| by rw [← left_distrib, neg_add_cancel, mul_zero] theorem mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c := by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c) alias mul_sub := mul_sub_left_distrib theorem mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c := by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c alias sub_mul := mul_sub_right_distrib end NonUnitalNonAssocRing section NonAssocRing variable [NonAssocRing α] theorem sub_one_mul (a b : α) : (a - 1) * b = a * b - b := by rw [sub_mul, one_mul] theorem mul_sub_one (a b : α) : a * (b - 1) = a * b - a := by rw [mul_sub, mul_one] theorem one_sub_mul (a b : α) : (1 - a) * b = b - a * b := by rw [sub_mul, one_mul] theorem mul_one_sub (a b : α) : a * (1 - b) = a - a * b := by rw [mul_sub, mul_one] end NonAssocRing section Ring variable [Ring α] -- A (unital, associative) ring is a not-necessarily-unital ring -- see Note [lower instance priority] instance (priority := 100) Ring.toNonUnitalRing : NonUnitalRing α := { ‹Ring α› with } -- A (unital, associative) ring is a not-necessarily-associative ring -- see Note [lower instance priority] instance (priority := 100) Ring.toNonAssocRing : NonAssocRing α := { ‹Ring α› with } end Ring /-- A non-unital non-associative commutative ring is a `NonUnitalNonAssocRing` with commutative multiplication. -/ class NonUnitalNonAssocCommRing (α : Type u) extends NonUnitalNonAssocRing α, NonUnitalNonAssocCommSemiring α /-- A non-unital commutative ring is a `NonUnitalRing` with commutative multiplication. -/ class NonUnitalCommRing (α : Type u) extends NonUnitalRing α, NonUnitalNonAssocCommRing α -- see Note [lower instance priority] instance (priority := 100) NonUnitalCommRing.toNonUnitalCommSemiring [s : NonUnitalCommRing α] : NonUnitalCommSemiring α := { s with } /-- A commutative ring is a ring with commutative multiplication. -/ class CommRing (α : Type u) extends Ring α, CommMonoid α instance (priority := 100) CommRing.toCommSemiring [s : CommRing α] : CommSemiring α := { s with } -- see Note [lower instance priority] instance (priority := 100) CommRing.toNonUnitalCommRing [s : CommRing α] : NonUnitalCommRing α := { s with } -- see Note [lower instance priority] instance (priority := 100) CommRing.toAddCommGroupWithOne [s : CommRing α] : AddCommGroupWithOne α := { s with } /-- A domain is a nontrivial semiring such that multiplication by a non zero element is cancellative on both sides. In other words, a nontrivial semiring `R` satisfying `∀ {a b c : R}, a ≠ 0 → a * b = a * c → b = c` and `∀ {a b c : R}, b ≠ 0 → a * b = c * b → a = c`.
This is implemented as a mixin for `Semiring α`.
Mathlib/Algebra/Ring/Defs.lean
390
391
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Filippo A. E. Nuccio -/ import Mathlib.RingTheory.Localization.Integer import Mathlib.RingTheory.Localization.Submodule /-! # Fractional ideals This file defines fractional ideals of an integral domain and proves basic facts about them. ## Main definitions Let `S` be a submonoid of an integral domain `R` and `P` the localization of `R` at `S`. * `IsFractional` defines which `R`-submodules of `P` are fractional ideals * `FractionalIdeal S P` is the type of fractional ideals in `P` * a coercion `coeIdeal : Ideal R → FractionalIdeal S P` * `CommSemiring (FractionalIdeal S P)` instance: the typical ideal operations generalized to fractional ideals * `Lattice (FractionalIdeal S P)` instance ## Main statements * `mul_left_mono` and `mul_right_mono` state that ideal multiplication is monotone * `mul_div_self_cancel_iff` states that `1 / I` is the inverse of `I` if one exists ## Implementation notes Fractional ideals are considered equal when they contain the same elements, independent of the denominator `a : R` such that `a I ⊆ R`. Thus, we define `FractionalIdeal` to be the subtype of the predicate `IsFractional`, instead of having `FractionalIdeal` be a structure of which `a` is a field. Most definitions in this file specialize operations from submodules to fractional ideals, proving that the result of this operation is fractional if the input is fractional. Exceptions to this rule are defining `(+) := (⊔)` and `⊥ := 0`, in order to re-use their respective proof terms. We can still use `simp` to show `↑I + ↑J = ↑(I + J)` and `↑⊥ = ↑0`. Many results in fact do not need that `P` is a localization, only that `P` is an `R`-algebra. We omit the `IsLocalization` parameter whenever this is practical. Similarly, we don't assume that the localization is a field until we need it to define ideal quotients. When this assumption is needed, we replace `S` with `R⁰`, making the localization a field. ## References * https://en.wikipedia.org/wiki/Fractional_ideal ## Tags fractional ideal, fractional ideals, invertible ideal -/ open IsLocalization Pointwise nonZeroDivisors section Defs variable {R : Type*} [CommRing R] {S : Submonoid R} {P : Type*} [CommRing P] variable [Algebra R P] variable (S) /-- A submodule `I` is a fractional ideal if `a I ⊆ R` for some `a ≠ 0`. -/ def IsFractional (I : Submodule R P) := ∃ a ∈ S, ∀ b ∈ I, IsInteger R (a • b) variable (P) /-- The fractional ideals of a domain `R` are ideals of `R` divided by some `a ∈ R`. More precisely, let `P` be a localization of `R` at some submonoid `S`, then a fractional ideal `I ⊆ P` is an `R`-submodule of `P`, such that there is a nonzero `a : R` with `a I ⊆ R`. -/ def FractionalIdeal := { I : Submodule R P // IsFractional S I } end Defs namespace FractionalIdeal open Set Submodule variable {R : Type*} [CommRing R] {S : Submonoid R} {P : Type*} [CommRing P] variable [Algebra R P] /-- Map a fractional ideal `I` to a submodule by forgetting that `∃ a, a I ⊆ R`. This implements the coercion `FractionalIdeal S P → Submodule R P`. -/ @[coe] def coeToSubmodule (I : FractionalIdeal S P) : Submodule R P := I.val /-- Map a fractional ideal `I` to a submodule by forgetting that `∃ a, a I ⊆ R`. This coercion is typically called `coeToSubmodule` in lemma names (or `coe` when the coercion is clear from the context), not to be confused with `IsLocalization.coeSubmodule : Ideal R → Submodule R P` (which we use to define `coe : Ideal R → FractionalIdeal S P`). -/ instance : CoeOut (FractionalIdeal S P) (Submodule R P) := ⟨coeToSubmodule⟩ protected theorem isFractional (I : FractionalIdeal S P) : IsFractional S (I : Submodule R P) := I.prop /-- An element of `S` such that `I.den • I = I.num`, see `FractionalIdeal.num` and `FractionalIdeal.den_mul_self_eq_num`. -/ noncomputable def den (I : FractionalIdeal S P) : S := ⟨I.2.choose, I.2.choose_spec.1⟩ /-- An ideal of `R` such that `I.den • I = I.num`, see `FractionalIdeal.den` and `FractionalIdeal.den_mul_self_eq_num`. -/ noncomputable def num (I : FractionalIdeal S P) : Ideal R := (I.den • (I : Submodule R P)).comap (Algebra.linearMap R P) theorem den_mul_self_eq_num (I : FractionalIdeal S P) : I.den • (I : Submodule R P) = Submodule.map (Algebra.linearMap R P) I.num := by rw [den, num, Submodule.map_comap_eq] refine (inf_of_le_right ?_).symm rintro _ ⟨a, ha, rfl⟩ exact I.2.choose_spec.2 a ha /-- The linear equivalence between the fractional ideal `I` and the integral ideal `I.num` defined by mapping `x` to `den I • x`. -/ noncomputable def equivNum [Nontrivial P] [NoZeroSMulDivisors R P] {I : FractionalIdeal S P} (h_nz : (I.den : R) ≠ 0) : I ≃ₗ[R] I.num := by refine LinearEquiv.trans (LinearEquiv.ofBijective ((DistribMulAction.toLinearMap R P I.den).restrict fun _ hx ↦ ?_) ⟨fun _ _ hxy ↦ ?_, fun ⟨y, hy⟩ ↦ ?_⟩) (Submodule.equivMapOfInjective (Algebra.linearMap R P) (FaithfulSMul.algebraMap_injective R P) (num I)).symm · rw [← den_mul_self_eq_num] exact Submodule.smul_mem_pointwise_smul _ _ _ hx · simp_rw [LinearMap.restrict_apply, DistribMulAction.toLinearMap_apply, Subtype.mk.injEq] at hxy rwa [Submonoid.smul_def, Submonoid.smul_def, smul_right_inj h_nz, SetCoe.ext_iff] at hxy · rw [← den_mul_self_eq_num] at hy obtain ⟨x, hx, hxy⟩ := hy exact ⟨⟨x, hx⟩, by simp_rw [LinearMap.restrict_apply, Subtype.ext_iff, ← hxy]; rfl⟩ section SetLike instance : SetLike (FractionalIdeal S P) P where coe I := ↑(I : Submodule R P) coe_injective' := SetLike.coe_injective.comp Subtype.coe_injective @[simp] theorem mem_coe {I : FractionalIdeal S P} {x : P} : x ∈ (I : Submodule R P) ↔ x ∈ I := Iff.rfl @[ext] theorem ext {I J : FractionalIdeal S P} : (∀ x, x ∈ I ↔ x ∈ J) → I = J := SetLike.ext @[simp] theorem equivNum_apply [Nontrivial P] [NoZeroSMulDivisors R P] {I : FractionalIdeal S P} (h_nz : (I.den : R) ≠ 0) (x : I) : algebraMap R P (equivNum h_nz x) = I.den • x := by change Algebra.linearMap R P _ = _ rw [equivNum, LinearEquiv.trans_apply, LinearEquiv.ofBijective_apply, LinearMap.restrict_apply, Submodule.map_equivMapOfInjective_symm_apply, Subtype.coe_mk, DistribMulAction.toLinearMap_apply] /-- Copy of a `FractionalIdeal` with a new underlying set equal to the old one. Useful to fix definitional equalities. -/ protected def copy (p : FractionalIdeal S P) (s : Set P) (hs : s = ↑p) : FractionalIdeal S P := ⟨Submodule.copy p s hs, by convert p.isFractional ext simp only [hs] rfl⟩ @[simp] theorem coe_copy (p : FractionalIdeal S P) (s : Set P) (hs : s = ↑p) : ↑(p.copy s hs) = s := rfl theorem coe_eq (p : FractionalIdeal S P) (s : Set P) (hs : s = ↑p) : p.copy s hs = p := SetLike.coe_injective hs end SetLike lemma zero_mem (I : FractionalIdeal S P) : 0 ∈ I := I.coeToSubmodule.zero_mem -- Porting note: this seems to be needed a lot more than in Lean 3 @[simp] theorem val_eq_coe (I : FractionalIdeal S P) : I.val = I := rfl -- Porting note: had to rephrase this to make it clear to `simp` what was going on. @[simp, norm_cast] theorem coe_mk (I : Submodule R P) (hI : IsFractional S I) : coeToSubmodule ⟨I, hI⟩ = I := rfl theorem coeToSet_coeToSubmodule (I : FractionalIdeal S P) : ((I : Submodule R P) : Set P) = I := rfl /-! Transfer instances from `Submodule R P` to `FractionalIdeal S P`. -/ instance (I : FractionalIdeal S P) : Module R I := Submodule.module (I : Submodule R P) theorem coeToSubmodule_injective : Function.Injective (fun (I : FractionalIdeal S P) ↦ (I : Submodule R P)) := Subtype.coe_injective theorem coeToSubmodule_inj {I J : FractionalIdeal S P} : (I : Submodule R P) = J ↔ I = J := coeToSubmodule_injective.eq_iff theorem isFractional_of_le_one (I : Submodule R P) (h : I ≤ 1) : IsFractional S I := by use 1, S.one_mem intro b hb rw [one_smul] obtain ⟨b', b'_mem, rfl⟩ := mem_one.mp (h hb) exact Set.mem_range_self b' theorem isFractional_of_le {I : Submodule R P} {J : FractionalIdeal S P} (hIJ : I ≤ J) : IsFractional S I := by obtain ⟨a, a_mem, ha⟩ := J.isFractional use a, a_mem intro b b_mem exact ha b (hIJ b_mem) /-- Map an ideal `I` to a fractional ideal by forgetting `I` is integral. This is the function that implements the coercion `Ideal R → FractionalIdeal S P`. -/ @[coe] def coeIdeal (I : Ideal R) : FractionalIdeal S P := ⟨coeSubmodule P I, isFractional_of_le_one _ <| by simpa using coeSubmodule_mono P (le_top : I ≤ ⊤)⟩ -- Is a `CoeTC` rather than `Coe` to speed up failing inference, see library note [use has_coe_t] /-- Map an ideal `I` to a fractional ideal by forgetting `I` is integral. This is a bundled version of `IsLocalization.coeSubmodule : Ideal R → Submodule R P`, which is not to be confused with the `coe : FractionalIdeal S P → Submodule R P`, also called `coeToSubmodule` in theorem names. This map is available as a ring hom, called `FractionalIdeal.coeIdealHom`. -/ instance : CoeTC (Ideal R) (FractionalIdeal S P) := ⟨fun I => coeIdeal I⟩ @[simp, norm_cast] theorem coe_coeIdeal (I : Ideal R) : ((I : FractionalIdeal S P) : Submodule R P) = coeSubmodule P I := rfl variable (S) @[simp] theorem mem_coeIdeal {x : P} {I : Ideal R} : x ∈ (I : FractionalIdeal S P) ↔ ∃ x', x' ∈ I ∧ algebraMap R P x' = x := mem_coeSubmodule _ _ theorem mem_coeIdeal_of_mem {x : R} {I : Ideal R} (hx : x ∈ I) : algebraMap R P x ∈ (I : FractionalIdeal S P) := (mem_coeIdeal S).mpr ⟨x, hx, rfl⟩ theorem coeIdeal_le_coeIdeal' [IsLocalization S P] (h : S ≤ nonZeroDivisors R) {I J : Ideal R} : (I : FractionalIdeal S P) ≤ J ↔ I ≤ J := coeSubmodule_le_coeSubmodule h @[simp] theorem coeIdeal_le_coeIdeal (K : Type*) [CommRing K] [Algebra R K] [IsFractionRing R K] {I J : Ideal R} : (I : FractionalIdeal R⁰ K) ≤ J ↔ I ≤ J := IsFractionRing.coeSubmodule_le_coeSubmodule instance : Zero (FractionalIdeal S P) := ⟨(0 : Ideal R)⟩ @[simp] theorem mem_zero_iff {x : P} : x ∈ (0 : FractionalIdeal S P) ↔ x = 0 := ⟨fun ⟨x', x'_mem_zero, x'_eq_x⟩ => by have x'_eq_zero : x' = 0 := x'_mem_zero simp [x'_eq_x.symm, x'_eq_zero], fun hx => ⟨0, rfl, by simp [hx]⟩⟩ variable {S} @[simp, norm_cast] theorem coe_zero : ↑(0 : FractionalIdeal S P) = (⊥ : Submodule R P) := Submodule.ext fun _ => mem_zero_iff S @[simp, norm_cast] theorem coeIdeal_bot : ((⊥ : Ideal R) : FractionalIdeal S P) = 0 := rfl section variable [loc : IsLocalization S P] variable (P) in @[simp] theorem exists_mem_algebraMap_eq {x : R} {I : Ideal R} (h : S ≤ nonZeroDivisors R) : (∃ x', x' ∈ I ∧ algebraMap R P x' = algebraMap R P x) ↔ x ∈ I := ⟨fun ⟨_, hx', Eq⟩ => IsLocalization.injective _ h Eq ▸ hx', fun h => ⟨x, h, rfl⟩⟩ theorem coeIdeal_injective' (h : S ≤ nonZeroDivisors R) : Function.Injective (fun (I : Ideal R) ↦ (I : FractionalIdeal S P)) := fun _ _ h' => ((coeIdeal_le_coeIdeal' S h).mp h'.le).antisymm ((coeIdeal_le_coeIdeal' S h).mp h'.ge) theorem coeIdeal_inj' (h : S ≤ nonZeroDivisors R) {I J : Ideal R} : (I : FractionalIdeal S P) = J ↔ I = J := (coeIdeal_injective' h).eq_iff -- Porting note: doesn't need to be @[simp] because it can be proved by coeIdeal_eq_zero theorem coeIdeal_eq_zero' {I : Ideal R} (h : S ≤ nonZeroDivisors R) : (I : FractionalIdeal S P) = 0 ↔ I = (⊥ : Ideal R) := coeIdeal_inj' h theorem coeIdeal_ne_zero' {I : Ideal R} (h : S ≤ nonZeroDivisors R) : (I : FractionalIdeal S P) ≠ 0 ↔ I ≠ (⊥ : Ideal R) := not_iff_not.mpr <| coeIdeal_eq_zero' h end theorem coeToSubmodule_eq_bot {I : FractionalIdeal S P} : (I : Submodule R P) = ⊥ ↔ I = 0 := ⟨fun h => coeToSubmodule_injective (by simp [h]), fun h => by simp [h]⟩ theorem coeToSubmodule_ne_bot {I : FractionalIdeal S P} : ↑I ≠ (⊥ : Submodule R P) ↔ I ≠ 0 := not_iff_not.mpr coeToSubmodule_eq_bot instance : Inhabited (FractionalIdeal S P) := ⟨0⟩ instance : One (FractionalIdeal S P) := ⟨(⊤ : Ideal R)⟩ theorem zero_of_num_eq_bot [NoZeroSMulDivisors R P] (hS : 0 ∉ S) {I : FractionalIdeal S P} (hI : I.num = ⊥) : I = 0 := by rw [← coeToSubmodule_eq_bot, eq_bot_iff] intro x hx suffices (den I : R) • x = 0 from (smul_eq_zero.mp this).resolve_left (ne_of_mem_of_not_mem (SetLike.coe_mem _) hS) have h_eq : I.den • (I : Submodule R P) = ⊥ := by rw [den_mul_self_eq_num, hI, Submodule.map_bot] exact (Submodule.eq_bot_iff _).mp h_eq (den I • x) ⟨x, hx, rfl⟩ theorem num_zero_eq (h_inj : Function.Injective (algebraMap R P)) : num (0 : FractionalIdeal S P) = 0 := by simpa [num, LinearMap.ker_eq_bot] using h_inj variable (S) @[simp, norm_cast] theorem coeIdeal_top : ((⊤ : Ideal R) : FractionalIdeal S P) = 1 := rfl theorem mem_one_iff {x : P} : x ∈ (1 : FractionalIdeal S P) ↔ ∃ x' : R, algebraMap R P x' = x := Iff.intro (fun ⟨x', _, h⟩ => ⟨x', h⟩) fun ⟨x', h⟩ => ⟨x', ⟨⟩, h⟩ theorem coe_mem_one (x : R) : algebraMap R P x ∈ (1 : FractionalIdeal S P) := (mem_one_iff S).mpr ⟨x, rfl⟩ theorem one_mem_one : (1 : P) ∈ (1 : FractionalIdeal S P) := (mem_one_iff S).mpr ⟨1, RingHom.map_one _⟩ variable {S} /-- `(1 : FractionalIdeal S P)` is defined as the R-submodule `f(R) ≤ P`. However, this is not definitionally equal to `1 : Submodule R P`, which is proved in the actual `simp` lemma `coe_one`. -/ theorem coe_one_eq_coeSubmodule_top : ↑(1 : FractionalIdeal S P) = coeSubmodule P (⊤ : Ideal R) := rfl @[simp, norm_cast] theorem coe_one : (↑(1 : FractionalIdeal S P) : Submodule R P) = 1 := by rw [coe_one_eq_coeSubmodule_top, coeSubmodule_top] section Lattice /-! ### `Lattice` section Defines the order on fractional ideals as inclusion of their underlying sets, and ports the lattice structure on submodules to fractional ideals. -/ @[simp] theorem coe_le_coe {I J : FractionalIdeal S P} : (I : Submodule R P) ≤ (J : Submodule R P) ↔ I ≤ J := Iff.rfl theorem zero_le (I : FractionalIdeal S P) : 0 ≤ I := by intro x hx -- Porting note: changed the proof from convert; simp into rw; exact rw [(mem_zero_iff _).mp hx] exact zero_mem I instance orderBot : OrderBot (FractionalIdeal S P) where bot := 0 bot_le := zero_le @[simp] theorem bot_eq_zero : (⊥ : FractionalIdeal S P) = 0 := rfl @[simp] theorem le_zero_iff {I : FractionalIdeal S P} : I ≤ 0 ↔ I = 0 := le_bot_iff theorem eq_zero_iff {I : FractionalIdeal S P} : I = 0 ↔ ∀ x ∈ I, x = (0 : P) := ⟨fun h x hx => by simpa [h, mem_zero_iff] using hx, fun h => le_bot_iff.mp fun x hx => (mem_zero_iff S).mpr (h x hx)⟩ theorem _root_.IsFractional.sup {I J : Submodule R P} : IsFractional S I → IsFractional S J → IsFractional S (I ⊔ J) | ⟨aI, haI, hI⟩, ⟨aJ, haJ, hJ⟩ => ⟨aI * aJ, S.mul_mem haI haJ, fun b hb => by rcases mem_sup.mp hb with ⟨bI, hbI, bJ, hbJ, rfl⟩ rw [smul_add] apply isInteger_add · rw [mul_smul, smul_comm] exact isInteger_smul (hI bI hbI) · rw [mul_smul] exact isInteger_smul (hJ bJ hbJ)⟩ theorem _root_.IsFractional.inf_right {I : Submodule R P} : IsFractional S I → ∀ J, IsFractional S (I ⊓ J) | ⟨aI, haI, hI⟩, J => ⟨aI, haI, fun b hb => by rcases mem_inf.mp hb with ⟨hbI, _⟩ exact hI b hbI⟩ instance : Min (FractionalIdeal S P) := ⟨fun I J => ⟨I ⊓ J, I.isFractional.inf_right J⟩⟩ @[simp, norm_cast] theorem coe_inf (I J : FractionalIdeal S P) : ↑(I ⊓ J) = (I ⊓ J : Submodule R P) := rfl instance : Max (FractionalIdeal S P) := ⟨fun I J => ⟨I ⊔ J, I.isFractional.sup J.isFractional⟩⟩ @[norm_cast] theorem coe_sup (I J : FractionalIdeal S P) : ↑(I ⊔ J) = (I ⊔ J : Submodule R P) := rfl instance lattice : Lattice (FractionalIdeal S P) := Function.Injective.lattice _ Subtype.coe_injective coe_sup coe_inf
instance : SemilatticeSup (FractionalIdeal S P) := { FractionalIdeal.lattice with }
Mathlib/RingTheory/FractionalIdeal/Basic.lean
447
449
/- 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]
Mathlib/Order/Heyting/Basic.lean
306
306
/- 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.Group.Nat.Defs import Mathlib.CategoryTheory.Category.Preorder import Mathlib.CategoryTheory.EqToHom import Mathlib.CategoryTheory.Functor.Const import Mathlib.Order.Fin.Basic import Mathlib.Tactic.FinCases import Mathlib.Tactic.SuppressCompilation /-! # Composable arrows If `C` is a category, the type of `n`-simplices in the nerve of `C` identifies to the type of functors `Fin (n + 1) ⥤ C`, which can be thought as families of `n` composable arrows in `C`. In this file, we introduce and study this category `ComposableArrows C n` of `n` composable arrows in `C`. If `F : ComposableArrows C n`, we define `F.left` as the leftmost object, `F.right` as the rightmost object, and `F.hom : F.left ⟶ F.right` is the canonical map. The most significant definition in this file is the constructor `F.precomp f : ComposableArrows C (n + 1)` for `F : ComposableArrows C n` and `f : X ⟶ F.left`: "it shifts `F` towards the right and inserts `f` on the left". This `precomp` has good definitional properties. In the namespace `CategoryTheory.ComposableArrows`, we provide constructors like `mk₁ f`, `mk₂ f g`, `mk₃ f g h` for `ComposableArrows C n` for small `n`. TODO (@joelriou): * redefine `Arrow C` as `ComposableArrow C 1`? * construct some elements in `ComposableArrows m (Fin (n + 1))` for small `n` the precomposition with which shall induce functors `ComposableArrows C n ⥤ ComposableArrows C m` which correspond to simplicial operations (specifically faces) with good definitional properties (this might be necessary for up to `n = 7` in order to formalize spectral sequences following Verdier) -/ /-! New `simprocs` that run even in `dsimp` have caused breakages in this file. (e.g. `dsimp` can now simplify `2 + 3` to `5`) For now, we just turn off simprocs in this file. We'll soon provide finer grained options here, e.g. to turn off simprocs only in `dsimp`, etc. *However*, hopefully it is possible to refactor the material here so that no backwards compatibility `set_option`s are required at all -/ set_option simprocs false namespace CategoryTheory open Category variable (C : Type*) [Category C] /-- `ComposableArrows C n` is the type of functors `Fin (n + 1) ⥤ C`. -/ abbrev ComposableArrows (n : ℕ) := Fin (n + 1) ⥤ C namespace ComposableArrows variable {C} {n m : ℕ} variable (F G : ComposableArrows C n) /-- A wrapper for `omega` which prefaces it with some quick and useful attempts -/ macro "valid" : tactic => `(tactic| first | assumption | apply zero_le | apply le_rfl | transitivity <;> assumption | omega) /-- The `i`th object (with `i : ℕ` such that `i ≤ n`) of `F : ComposableArrows C n`. -/ @[simp] abbrev obj' (i : ℕ) (hi : i ≤ n := by valid) : C := F.obj ⟨i, by omega⟩ /-- The map `F.obj' i ⟶ F.obj' j` when `F : ComposableArrows C n`, and `i` and `j` are natural numbers such that `i ≤ j ≤ n`. -/ @[simp] abbrev map' (i j : ℕ) (hij : i ≤ j := by valid) (hjn : j ≤ n := by valid) : F.obj ⟨i, by omega⟩ ⟶ F.obj ⟨j, by omega⟩ := F.map (homOfLE (by simp only [Fin.mk_le_mk] valid)) lemma map'_self (i : ℕ) (hi : i ≤ n := by valid) : F.map' i i = 𝟙 _ := F.map_id _ lemma map'_comp (i j k : ℕ) (hij : i ≤ j := by valid) (hjk : j ≤ k := by valid) (hk : k ≤ n := by valid) : F.map' i k = F.map' i j ≫ F.map' j k := F.map_comp _ _ /-- The leftmost object of `F : ComposableArrows C n`. -/ abbrev left := obj' F 0 /-- The rightmost object of `F : ComposableArrows C n`. -/ abbrev right := obj' F n /-- The canonical map `F.left ⟶ F.right` for `F : ComposableArrows C n`. -/ abbrev hom : F.left ⟶ F.right := map' F 0 n variable {F G} /-- The map `F.obj' i ⟶ G.obj' i` induced on `i`th objects by a morphism `F ⟶ G` in `ComposableArrows C n` when `i` is a natural number such that `i ≤ n`. -/ @[simp] abbrev app' (φ : F ⟶ G) (i : ℕ) (hi : i ≤ n := by valid) : F.obj' i ⟶ G.obj' i := φ.app _ @[reassoc] lemma naturality' (φ : F ⟶ G) (i j : ℕ) (hij : i ≤ j := by valid) (hj : j ≤ n := by valid) : F.map' i j ≫ app' φ j = app' φ i ≫ G.map' i j := φ.naturality _ /-- Constructor for `ComposableArrows C 0`. -/ @[simps!] def mk₀ (X : C) : ComposableArrows C 0 := (Functor.const (Fin 1)).obj X namespace Mk₁ variable (X₀ X₁ : C) /-- The map which sends `0 : Fin 2` to `X₀` and `1` to `X₁`. -/ @[simp] def obj : Fin 2 → C | ⟨0, _⟩ => X₀ | ⟨1, _⟩ => X₁ variable {X₀ X₁} variable (f : X₀ ⟶ X₁) /-- The obvious map `obj X₀ X₁ i ⟶ obj X₀ X₁ j` whenever `i j : Fin 2` satisfy `i ≤ j`. -/ @[simp] def map : ∀ (i j : Fin 2) (_ : i ≤ j), obj X₀ X₁ i ⟶ obj X₀ X₁ j | ⟨0, _⟩, ⟨0, _⟩, _ => 𝟙 _ | ⟨0, _⟩, ⟨1, _⟩, _ => f | ⟨1, _⟩, ⟨1, _⟩, _ => 𝟙 _ lemma map_id (i : Fin 2) : map f i i (by simp) = 𝟙 _ := match i with | 0 => rfl | 1 => rfl lemma map_comp {i j k : Fin 2} (hij : i ≤ j) (hjk : j ≤ k) : map f i k (hij.trans hjk) = map f i j hij ≫ map f j k hjk := by obtain rfl | rfl : i = j ∨ j = k := by omega · rw [map_id, id_comp] · rw [map_id, comp_id] end Mk₁ /-- Constructor for `ComposableArrows C 1`. -/ @[simps] def mk₁ {X₀ X₁ : C} (f : X₀ ⟶ X₁) : ComposableArrows C 1 where obj := Mk₁.obj X₀ X₁ map g := Mk₁.map f _ _ (leOfHom g) map_id := Mk₁.map_id f map_comp g g' := Mk₁.map_comp f (leOfHom g) (leOfHom g') /-- Constructor for morphisms `F ⟶ G` in `ComposableArrows C n` which takes as inputs a family of morphisms `F.obj i ⟶ G.obj i` and the naturality condition only for the maps in `Fin (n + 1)` given by inequalities of the form `i ≤ i + 1`. -/ @[simps] def homMk {F G : ComposableArrows C n} (app : ∀ i, F.obj i ⟶ G.obj i) (w : ∀ (i : ℕ) (hi : i < n), F.map' i (i + 1) ≫ app _ = app _ ≫ G.map' i (i + 1)) : F ⟶ G where app := app naturality := by suffices ∀ (k i j : ℕ) (hj : i + k = j) (hj' : j ≤ n), F.map' i j ≫ app _ = app _ ≫ G.map' i j by rintro ⟨i, hi⟩ ⟨j, hj⟩ hij have hij' := leOfHom hij simp only [Fin.mk_le_mk] at hij' obtain ⟨k, hk⟩ := Nat.le.dest hij' exact this k i j hk (by valid) intro k induction' k with k hk · intro i j hj hj' simp only [add_zero] at hj obtain rfl := hj rw [F.map'_self i, G.map'_self i, id_comp, comp_id] · intro i j hj hj' rw [← add_assoc] at hj subst hj rw [F.map'_comp i (i + k) (i + k + 1), G.map'_comp i (i + k) (i + k + 1), assoc, w (i + k) (by valid), reassoc_of% (hk i (i + k) rfl (by valid))] /-- Constructor for isomorphisms `F ≅ G` in `ComposableArrows C n` which takes as inputs a family of isomorphisms `F.obj i ≅ G.obj i` and the naturality condition only for the maps in `Fin (n + 1)` given by inequalities of the form `i ≤ i + 1`. -/ @[simps] def isoMk {F G : ComposableArrows C n} (app : ∀ i, F.obj i ≅ G.obj i) (w : ∀ (i : ℕ) (hi : i < n), F.map' i (i + 1) ≫ (app _).hom = (app _).hom ≫ G.map' i (i + 1)) : F ≅ G where hom := homMk (fun i => (app i).hom) w inv := homMk (fun i => (app i).inv) (fun i hi => by dsimp only rw [← cancel_epi ((app _).hom), ← reassoc_of% (w i hi), Iso.hom_inv_id, comp_id, Iso.hom_inv_id_assoc]) lemma ext {F G : ComposableArrows C n} (h : ∀ i, F.obj i = G.obj i) (w : ∀ (i : ℕ) (hi : i < n), F.map' i (i + 1) = eqToHom (h _) ≫ G.map' i (i + 1) ≫ eqToHom (h _).symm) : F = G := Functor.ext_of_iso (isoMk (fun i => eqToIso (h i)) (fun i hi => by simp [w i hi])) h (fun _ => rfl) /-- Constructor for morphisms in `ComposableArrows C 0`. -/ @[simps!] def homMk₀ {F G : ComposableArrows C 0} (f : F.obj' 0 ⟶ G.obj' 0) : F ⟶ G := homMk (fun i => match i with | ⟨0, _⟩ => f) (fun i hi => by simp at hi) @[ext] lemma hom_ext₀ {F G : ComposableArrows C 0} {φ φ' : F ⟶ G} (h : app' φ 0 = app' φ' 0) : φ = φ' := by ext i fin_cases i exact h /-- Constructor for isomorphisms in `ComposableArrows C 0`. -/ @[simps!] def isoMk₀ {F G : ComposableArrows C 0} (e : F.obj' 0 ≅ G.obj' 0) : F ≅ G where hom := homMk₀ e.hom inv := homMk₀ e.inv lemma ext₀ {F G : ComposableArrows C 0} (h : F.obj' 0 = G.obj 0) : F = G := ext (fun i => match i with | ⟨0, _⟩ => h) (fun i hi => by simp at hi) lemma mk₀_surjective (F : ComposableArrows C 0) : ∃ (X : C), F = mk₀ X := ⟨F.obj' 0, ext₀ rfl⟩ /-- Constructor for morphisms in `ComposableArrows C 1`. -/ @[simps!] def homMk₁ {F G : ComposableArrows C 1} (left : F.obj' 0 ⟶ G.obj' 0) (right : F.obj' 1 ⟶ G.obj' 1) (w : F.map' 0 1 ≫ right = left ≫ G.map' 0 1 := by aesop_cat) : F ⟶ G := homMk (fun i => match i with | ⟨0, _⟩ => left | ⟨1, _⟩ => right) (by intro i hi obtain rfl : i = 0 := by simpa using hi exact w) @[ext] lemma hom_ext₁ {F G : ComposableArrows C 1} {φ φ' : F ⟶ G} (h₀ : app' φ 0 = app' φ' 0) (h₁ : app' φ 1 = app' φ' 1) : φ = φ' := by ext i match i with | 0 => exact h₀ | 1 => exact h₁ /-- Constructor for isomorphisms in `ComposableArrows C 1`. -/ @[simps!] def isoMk₁ {F G : ComposableArrows C 1} (left : F.obj' 0 ≅ G.obj' 0) (right : F.obj' 1 ≅ G.obj' 1) (w : F.map' 0 1 ≫ right.hom = left.hom ≫ G.map' 0 1 := by aesop_cat) : F ≅ G where hom := homMk₁ left.hom right.hom w inv := homMk₁ left.inv right.inv (by rw [← cancel_mono right.hom, assoc, assoc, w, right.inv_hom_id, left.inv_hom_id_assoc] apply comp_id) lemma map'_eq_hom₁ (F : ComposableArrows C 1) : F.map' 0 1 = F.hom := rfl lemma ext₁ {F G : ComposableArrows C 1} (left : F.left = G.left) (right : F.right = G.right) (w : F.hom = eqToHom left ≫ G.hom ≫ eqToHom right.symm) : F = G := Functor.ext_of_iso (isoMk₁ (eqToIso left) (eqToIso right) (by simp [map'_eq_hom₁, w])) (fun i => by fin_cases i <;> assumption)
(fun i => by fin_cases i <;> rfl) lemma mk₁_surjective (X : ComposableArrows C 1) : ∃ (X₀ X₁ : C) (f : X₀ ⟶ X₁), X = mk₁ f := ⟨_, _, X.map' 0 1, ext₁ rfl rfl (by simp)⟩ variable (F)
Mathlib/CategoryTheory/ComposableArrows.lean
277
282
/- Copyright (c) 2021 Apurva Nakade. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Apurva Nakade -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Ring.Regular import Mathlib.GroupTheory.MonoidLocalization.Away import Mathlib.RingTheory.Localization.Defs import Mathlib.SetTheory.Game.Birthday import Mathlib.SetTheory.Surreal.Multiplication import Mathlib.Tactic.Linarith /-! # Dyadic numbers Dyadic numbers are obtained by localizing ℤ away from 2. They are the initial object in the category of rings with no 2-torsion. ## Dyadic surreal numbers We construct dyadic surreal numbers using the canonical map from ℤ[2 ^ {-1}] to surreals. As we currently do not have a ring structure on `Surreal` we construct this map explicitly. Once we have the ring structure, this map can be constructed directly by sending `2 ^ {-1}` to `half`. ## Embeddings The above construction gives us an abelian group embedding of ℤ into `Surreal`. The goal is to extend this to an embedding of dyadic rationals into `Surreal` and use Cauchy sequences of dyadic rational numbers to construct an ordered field embedding of ℝ into `Surreal`. -/ universe u namespace SetTheory namespace PGame /-- For a natural number `n`, the pre-game `powHalf (n + 1)` is recursively defined as `{0 | powHalf n}`. These are the explicit expressions of powers of `1 / 2`. By definition, we have `powHalf 0 = 1` and `powHalf 1 ≈ 1 / 2` and we prove later on that `powHalf (n + 1) + powHalf (n + 1) ≈ powHalf n`. -/ def powHalf : ℕ → PGame | 0 => 1 | n + 1 => ⟨PUnit, PUnit, 0, fun _ => powHalf n⟩ @[simp] theorem powHalf_zero : powHalf 0 = 1 := rfl theorem powHalf_leftMoves (n) : (powHalf n).LeftMoves = PUnit := by cases n <;> rfl theorem powHalf_zero_rightMoves : (powHalf 0).RightMoves = PEmpty := rfl theorem powHalf_succ_rightMoves (n) : (powHalf (n + 1)).RightMoves = PUnit := rfl @[simp] theorem powHalf_moveLeft (n i) : (powHalf n).moveLeft i = 0 := by cases n <;> cases i <;> rfl @[simp] theorem powHalf_succ_moveRight (n i) : (powHalf (n + 1)).moveRight i = powHalf n := rfl instance uniquePowHalfLeftMoves (n) : Unique (powHalf n).LeftMoves := by cases n <;> exact PUnit.instUnique instance isEmpty_powHalf_zero_rightMoves : IsEmpty (powHalf 0).RightMoves := inferInstanceAs (IsEmpty PEmpty) instance uniquePowHalfSuccRightMoves (n) : Unique (powHalf (n + 1)).RightMoves := PUnit.instUnique @[simp] theorem birthday_half : birthday (powHalf 1) = 2 := by rw [birthday_def]; simp /-- For all natural numbers `n`, the pre-games `powHalf n` are numeric. -/ theorem numeric_powHalf (n) : (powHalf n).Numeric := by induction n with | zero => exact numeric_one | succ n hn => constructor · simpa using hn.moveLeft_lt default · exact ⟨fun _ => numeric_zero, fun _ => hn⟩ theorem powHalf_succ_lt_powHalf (n : ℕ) : powHalf (n + 1) < powHalf n := (numeric_powHalf (n + 1)).lt_moveRight default theorem powHalf_succ_le_powHalf (n : ℕ) : powHalf (n + 1) ≤ powHalf n := (powHalf_succ_lt_powHalf n).le theorem powHalf_le_one (n : ℕ) : powHalf n ≤ 1 := by induction n with | zero => exact le_rfl | succ n hn => exact (powHalf_succ_le_powHalf n).trans hn theorem powHalf_succ_lt_one (n : ℕ) : powHalf (n + 1) < 1 := (powHalf_succ_lt_powHalf n).trans_le <| powHalf_le_one n theorem powHalf_pos (n : ℕ) : 0 < powHalf n := by rw [← lf_iff_lt numeric_zero (numeric_powHalf n), zero_lf_le]; simp theorem zero_le_powHalf (n : ℕ) : 0 ≤ powHalf n := (powHalf_pos n).le theorem add_powHalf_succ_self_eq_powHalf (n) : powHalf (n + 1) + powHalf (n + 1) ≈ powHalf n := by induction' n using Nat.strong_induction_on with n hn constructor <;> rw [le_iff_forall_lf] <;> constructor · rintro (⟨⟨⟩⟩ | ⟨⟨⟩⟩) <;> apply lf_of_lt · calc 0 + powHalf n.succ ≈ powHalf n.succ := zero_add_equiv _ _ < powHalf n := powHalf_succ_lt_powHalf n · calc powHalf n.succ + 0 ≈ powHalf n.succ := add_zero_equiv _ _ < powHalf n := powHalf_succ_lt_powHalf n · rcases n with - | n · rintro ⟨⟩ rintro ⟨⟩ apply lf_of_moveRight_le swap · exact Sum.inl default calc powHalf n.succ + powHalf (n.succ + 1) ≤ powHalf n.succ + powHalf n.succ := add_le_add_left (powHalf_succ_le_powHalf _) _ _ ≈ powHalf n := hn _ (Nat.lt_succ_self n) · simp only [powHalf_moveLeft, forall_const] apply lf_of_lt calc 0 ≈ 0 + 0 := Equiv.symm (add_zero_equiv 0) _ ≤ powHalf n.succ + 0 := add_le_add_right (zero_le_powHalf _) _ _ < powHalf n.succ + powHalf n.succ := add_lt_add_left (powHalf_pos _) _ · rintro (⟨⟨⟩⟩ | ⟨⟨⟩⟩) <;> apply lf_of_lt · calc powHalf n ≈ powHalf n + 0 := Equiv.symm (add_zero_equiv _) _ < powHalf n + powHalf n.succ := add_lt_add_left (powHalf_pos _) _ · calc powHalf n ≈ 0 + powHalf n := Equiv.symm (zero_add_equiv _) _ < powHalf n.succ + powHalf n := add_lt_add_right (powHalf_pos _) _ theorem half_add_half_equiv_one : powHalf 1 + powHalf 1 ≈ 1 := add_powHalf_succ_self_eq_powHalf 0 end PGame end SetTheory namespace Surreal open SetTheory PGame /-- Powers of the surreal number `half`. -/ def powHalf (n : ℕ) : Surreal := ⟦⟨PGame.powHalf n, PGame.numeric_powHalf n⟩⟧ @[simp] theorem powHalf_zero : powHalf 0 = 1 := rfl @[simp] theorem double_powHalf_succ_eq_powHalf (n : ℕ) : 2 * powHalf (n + 1) = powHalf n := by rw [two_mul]; exact Quotient.sound (PGame.add_powHalf_succ_self_eq_powHalf n) @[simp] theorem nsmul_pow_two_powHalf (n : ℕ) : 2 ^ n * powHalf n = 1 := by induction' n with n hn · simp only [pow_zero, powHalf_zero, mul_one] · rw [← hn, ← double_powHalf_succ_eq_powHalf n, ← mul_assoc (2 ^ n) 2 (powHalf (n + 1)), pow_succ', mul_comm 2 (2 ^ n)] @[simp] theorem nsmul_pow_two_powHalf' (n k : ℕ) : 2 ^ n * powHalf (n + k) = powHalf k := by induction' k with k hk · simp only [add_zero, Surreal.nsmul_pow_two_powHalf, eq_self_iff_true, Surreal.powHalf_zero] · rw [← double_powHalf_succ_eq_powHalf (n + k), ← double_powHalf_succ_eq_powHalf k, ← mul_assoc, mul_comm (2 ^ n) 2, mul_assoc] at hk rw [← zsmul_eq_zsmul_iff' two_ne_zero] simpa only [zsmul_eq_mul, Int.cast_ofNat] theorem zsmul_pow_two_powHalf (m : ℤ) (n k : ℕ) : (m * 2 ^ n) * powHalf (n + k) = m * powHalf k := by rw [mul_assoc] congr exact nsmul_pow_two_powHalf' n k theorem dyadic_aux {m₁ m₂ : ℤ} {y₁ y₂ : ℕ} (h₂ : m₁ * 2 ^ y₁ = m₂ * 2 ^ y₂) : m₁ * powHalf y₂ = m₂ * powHalf y₁ := by revert m₁ m₂ wlog h : y₁ ≤ y₂ · intro m₁ m₂ aux; exact (this (le_of_not_le h) aux.symm).symm intro m₁ m₂ h₂ obtain ⟨c, rfl⟩ := le_iff_exists_add.mp h rw [add_comm, pow_add, ← mul_assoc, mul_eq_mul_right_iff] at h₂ rcases h₂ with h₂ | h₂ · rw [h₂, add_comm] simp_rw [Int.cast_mul, Int.cast_pow, Int.cast_ofNat, zsmul_pow_two_powHalf m₂ c y₁] · have := Nat.one_le_pow y₁ 2 Nat.succ_pos' norm_cast at h₂; omega /-- The additive monoid morphism `dyadicMap` sends ⟦⟨m, 2^n⟩⟧ to m • half ^ n. -/ noncomputable def dyadicMap : Localization.Away (2 : ℤ) →+ Surreal where toFun x := (Localization.liftOn x fun x y => x * powHalf (Submonoid.log y)) <| by
intro m₁ m₂ n₁ n₂ h₁ obtain ⟨⟨n₃, y₃, hn₃⟩, h₂⟩ := Localization.r_iff_exists.mp h₁ simp only [Subtype.coe_mk, mul_eq_mul_left_iff] at h₂ cases h₂ · obtain ⟨a₁, ha₁⟩ := n₁.prop
Mathlib/SetTheory/Surreal/Dyadic.lean
205
209
/- Copyright (c) 2023 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.Measure.Decomposition.RadonNikodym /-! # Exponentially tilted measures The exponential tilting of a measure `μ` on `α` by a function `f : α → ℝ` is the measure with density `x ↦ exp (f x) / ∫ y, exp (f y) ∂μ` with respect to `μ`. This is sometimes also called the Esscher transform. The definition is mostly used for `f` linear, in which case the exponentially tilted measure belongs to the natural exponential family of the base measure. Exponentially tilted measures for general `f` can be used for example to establish variational expressions for the Kullback-Leibler divergence. ## Main definitions * `Measure.tilted μ f`: exponential tilting of `μ` by `f`, equal to `μ.withDensity (fun x ↦ ENNReal.ofReal (exp (f x) / ∫ x, exp (f x) ∂μ))`. -/ open Real open scoped ENNReal NNReal namespace MeasureTheory variable {α : Type*} {mα : MeasurableSpace α} {μ : Measure α} {f : α → ℝ} /-- Exponentially tilted measure. When `x ↦ exp (f x)` is integrable, `μ.tilted f` is the probability measure with density with respect to `μ` proportional to `exp (f x)`. Otherwise it is 0. -/ noncomputable def Measure.tilted (μ : Measure α) (f : α → ℝ) : Measure α := μ.withDensity (fun x ↦ ENNReal.ofReal (exp (f x) / ∫ x, exp (f x) ∂μ)) @[simp] lemma tilted_of_not_integrable (hf : ¬ Integrable (fun x ↦ exp (f x)) μ) : μ.tilted f = 0 := by rw [Measure.tilted, integral_undef hf] simp @[simp] lemma tilted_of_not_aemeasurable (hf : ¬ AEMeasurable f μ) : μ.tilted f = 0 := by refine tilted_of_not_integrable ?_ suffices ¬ AEMeasurable (fun x ↦ exp (f x)) μ by exact fun h ↦ this h.1.aemeasurable exact fun h ↦ hf (aemeasurable_of_aemeasurable_exp h) @[simp] lemma tilted_zero_measure (f : α → ℝ) : (0 : Measure α).tilted f = 0 := by simp [Measure.tilted] @[simp] lemma tilted_const' (μ : Measure α) (c : ℝ) : μ.tilted (fun _ ↦ c) = (μ Set.univ)⁻¹ • μ := by cases eq_zero_or_neZero μ with | inl h => rw [h]; simp | inr h0 => simp only [Measure.tilted, withDensity_const, integral_const, smul_eq_mul] by_cases h_univ : μ Set.univ = ∞ · simp only [measureReal_def, h_univ, ENNReal.toReal_top, zero_mul, div_zero, ENNReal.ofReal_zero, zero_smul, ENNReal.inv_top] congr rw [div_eq_mul_inv, mul_inv, mul_comm, mul_assoc, inv_mul_cancel₀ (exp_pos _).ne', mul_one, measureReal_def, ← ENNReal.toReal_inv, ENNReal.ofReal_toReal] simp [h0.out] lemma tilted_const (μ : Measure α) [IsProbabilityMeasure μ] (c : ℝ) : μ.tilted (fun _ ↦ c) = μ := by simp @[simp] lemma tilted_zero' (μ : Measure α) : μ.tilted 0 = (μ Set.univ)⁻¹ • μ := by change μ.tilted (fun _ ↦ 0) = (μ Set.univ)⁻¹ • μ simp
lemma tilted_zero (μ : Measure α) [IsProbabilityMeasure μ] : μ.tilted 0 = μ := by simp
Mathlib/MeasureTheory/Measure/Tilted.lean
78
78
/- 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 _)
Mathlib/NumberTheory/Cyclotomic/PrimitiveRoots.lean
128
131
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import Mathlib.Algebra.Order.GroupWithZero.Synonym import Mathlib.Algebra.Order.Ring.Canonical import Mathlib.Algebra.Ring.Hom.Defs import Mathlib.Algebra.Order.Monoid.WithTop /-! # Structures involving `*` and `0` on `WithTop` and `WithBot` The main results of this section are `WithTop.instOrderedCommSemiring` and `WithBot.instOrderedCommSemiring`. -/ variable {α : Type*} namespace WithTop variable [DecidableEq α] section MulZeroClass variable [MulZeroClass α] {a b : WithTop α} instance instMulZeroClass : MulZeroClass (WithTop α) where zero := 0 mul | (a : α), (b : α) => ↑(a * b) | (a : α), ⊤ => if a = 0 then 0 else ⊤ | ⊤, (b : α) => if b = 0 then 0 else ⊤ | ⊤, ⊤ => ⊤ mul_zero | (a : α) => congr_arg some <| mul_zero _ | ⊤ => if_pos rfl zero_mul | (b : α) => congr_arg some <| zero_mul _ | ⊤ => if_pos rfl @[simp, norm_cast] lemma coe_mul (a b : α) : (↑(a * b) : WithTop α) = a * b := rfl lemma mul_top' : ∀ (a : WithTop α), a * ⊤ = if a = 0 then 0 else ⊤ | (a : α) => if_congr coe_eq_zero.symm rfl rfl | ⊤ => (if_neg top_ne_zero).symm @[simp] lemma mul_top (h : a ≠ 0) : a * ⊤ = ⊤ := by rw [mul_top', if_neg h] lemma top_mul' : ∀ (b : WithTop α), ⊤ * b = if b = 0 then 0 else ⊤ | (b : α) => if_congr coe_eq_zero.symm rfl rfl | ⊤ => (if_neg top_ne_zero).symm @[simp] lemma top_mul (hb : b ≠ 0) : ⊤ * b = ⊤ := by rw [top_mul', if_neg hb] @[simp] lemma top_mul_top : (⊤ * ⊤ : WithTop α) = ⊤ := rfl lemma mul_def (a b : WithTop α) : a * b = if a = 0 ∨ b = 0 then 0 else WithTop.map₂ (· * ·) a b := by cases a <;> cases b <;> aesop lemma mul_eq_top_iff : a * b = ⊤ ↔ a ≠ 0 ∧ b = ⊤ ∨ a = ⊤ ∧ b ≠ 0 := by rw [mul_def]; aesop lemma mul_coe_eq_bind {b : α} (hb : b ≠ 0) : ∀ a, (a * b : WithTop α) = a.bind fun a ↦ ↑(a * b) | ⊤ => by simp [top_mul, hb]; rfl | (a : α) => rfl lemma coe_mul_eq_bind {a : α} (ha : a ≠ 0) : ∀ b, (a * b : WithTop α) = b.bind fun b ↦ ↑(a * b) | ⊤ => by simp [top_mul, ha]; rfl | (b : α) => rfl @[simp] lemma untopD_zero_mul (a b : WithTop α) : (a * b).untopD 0 = a.untopD 0 * b.untopD 0 := by by_cases ha : a = 0; · rw [ha, zero_mul, ← coe_zero, untopD_coe, zero_mul] by_cases hb : b = 0; · rw [hb, mul_zero, ← coe_zero, untopD_coe, mul_zero] induction a; · rw [top_mul hb, untopD_top, zero_mul] induction b; · rw [mul_top ha, untopD_top, mul_zero] rw [← coe_mul, untopD_coe, untopD_coe, untopD_coe] @[deprecated (since := "2025-02-06")] alias untop'_zero_mul := untopD_zero_mul theorem mul_ne_top {a b : WithTop α} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a * b ≠ ⊤ := by simp [mul_eq_top_iff, *] theorem mul_lt_top [LT α] {a b : WithTop α} (ha : a < ⊤) (hb : b < ⊤) : a * b < ⊤ := by rw [WithTop.lt_top_iff_ne_top] at * exact mul_ne_top ha hb instance instNoZeroDivisors [NoZeroDivisors α] : NoZeroDivisors (WithTop α) := by refine ⟨fun h₁ => Decidable.byContradiction fun h₂ => ?_⟩ rw [mul_def, if_neg h₂] at h₁ rcases Option.mem_map₂_iff.1 h₁ with ⟨a, b, (rfl : _ = _), (rfl : _ = _), hab⟩ exact h₂ ((eq_zero_or_eq_zero_of_mul_eq_zero hab).imp (congr_arg some) (congr_arg some)) end MulZeroClass /-- `Nontrivial α` is needed here as otherwise we have `1 * ⊤ = ⊤` but also `0 * ⊤ = 0`. -/ instance instMulZeroOneClass [MulZeroOneClass α] [Nontrivial α] : MulZeroOneClass (WithTop α) where __ := instMulZeroClass one_mul | ⊤ => mul_top (mt coe_eq_coe.1 one_ne_zero) | (a : α) => by rw [← coe_one, ← coe_mul, one_mul] mul_one | ⊤ => top_mul (mt coe_eq_coe.1 one_ne_zero) | (a : α) => by rw [← coe_one, ← coe_mul, mul_one] /-- A version of `WithTop.map` for `MonoidWithZeroHom`s. -/ @[simps -fullyApplied] protected def _root_.MonoidWithZeroHom.withTopMap {R S : Type*} [MulZeroOneClass R] [DecidableEq R] [Nontrivial R] [MulZeroOneClass S] [DecidableEq S] [Nontrivial S] (f : R →*₀ S) (hf : Function.Injective f) : WithTop R →*₀ WithTop S := { f.toZeroHom.withTopMap, f.toMonoidHom.toOneHom.withTopMap with toFun := WithTop.map f map_mul' := fun x y => by have : ∀ z, map f z = 0 ↔ z = 0 := fun z => (Option.map_injective hf).eq_iff' f.toZeroHom.withTopMap.map_zero rcases Decidable.eq_or_ne x 0 with (rfl | hx) · simp rcases Decidable.eq_or_ne y 0 with (rfl | hy) · simp induction' x with x · simp [hy, this] induction' y with y · have : (f x : WithTop S) ≠ 0 := by simpa [hf.eq_iff' (map_zero f)] using hx simp [mul_top hx, mul_top this] · simp [← coe_mul] } instance instSemigroupWithZero [SemigroupWithZero α] [NoZeroDivisors α] : SemigroupWithZero (WithTop α) where __ := instMulZeroClass mul_assoc a b c := by rcases eq_or_ne a 0 with (rfl | ha); · simp only [zero_mul] rcases eq_or_ne b 0 with (rfl | hb); · simp only [zero_mul, mul_zero] rcases eq_or_ne c 0 with (rfl | hc); · simp only [mul_zero] induction' a with a; · simp [hb, hc] induction' b with b; · simp [mul_top ha, top_mul hc] induction' c with c · rw [mul_top hb, mul_top ha] rw [← coe_zero, ne_eq, coe_eq_coe] at ha hb simp [ha, hb] simp only [← coe_mul, mul_assoc] section MonoidWithZero variable [MonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] {x : WithTop α} {n : ℕ} instance instMonoidWithZero : MonoidWithZero (WithTop α) where __ := instMulZeroOneClass __ := instSemigroupWithZero npow n a := match a, n with | (a : α), n => ↑(a ^ n) | ⊤, 0 => 1 | ⊤, _n + 1 => ⊤ npow_zero a := by cases a <;> simp npow_succ n a := by cases n <;> cases a <;> simp [pow_succ] @[simp, norm_cast] lemma coe_pow (a : α) (n : ℕ) : (↑(a ^ n) : WithTop α) = a ^ n := rfl @[simp] lemma top_pow : ∀ {n : ℕ}, n ≠ 0 → (⊤ : WithTop α) ^ n = ⊤ | _ + 1, _ => rfl @[simp] lemma pow_eq_top_iff : x ^ n = ⊤ ↔ x = ⊤ ∧ n ≠ 0 := by induction x <;> cases n <;> simp [← coe_pow] lemma pow_ne_top_iff : x ^ n ≠ ⊤ ↔ x ≠ ⊤ ∨ n = 0 := by simp [pow_eq_top_iff, or_iff_not_imp_left] @[simp] lemma pow_lt_top_iff [Preorder α] : x ^ n < ⊤ ↔ x < ⊤ ∨ n = 0 := by simp_rw [WithTop.lt_top_iff_ne_top, pow_ne_top_iff] lemma eq_top_of_pow (n : ℕ) (hx : x ^ n = ⊤) : x = ⊤ := (pow_eq_top_iff.1 hx).1 lemma pow_ne_top (hx : x ≠ ⊤) : x ^ n ≠ ⊤ := pow_ne_top_iff.2 <| .inl hx lemma pow_lt_top [Preorder α] (hx : x < ⊤) : x ^ n < ⊤ := pow_lt_top_iff.2 <| .inl hx end MonoidWithZero instance instCommMonoidWithZero [CommMonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] : CommMonoidWithZero (WithTop α) where __ := instMonoidWithZero mul_comm a b := by simp_rw [mul_def]; exact if_congr or_comm rfl (Option.map₂_comm mul_comm) instance instNonUnitalNonAssocSemiring [NonUnitalNonAssocSemiring α] [PartialOrder α] [CanonicallyOrderedAdd α] : NonUnitalNonAssocSemiring (WithTop α) where toAddCommMonoid := WithTop.addCommMonoid __ := WithTop.instMulZeroClass right_distrib a b c := by induction' c with c · by_cases ha : a = 0 <;> simp [ha] · by_cases hc : c = 0; · simp [hc] simp only [mul_coe_eq_bind hc] cases a <;> cases b <;> try rfl exact congr_arg some (add_mul _ _ _) left_distrib c a b := by induction' c with c · by_cases ha : a = 0 <;> simp [ha] · by_cases hc : c = 0; · simp [hc] simp only [coe_mul_eq_bind hc] cases a <;> cases b <;> try rfl exact congr_arg some (mul_add _ _ _) instance instNonAssocSemiring [NonAssocSemiring α] [PartialOrder α] [CanonicallyOrderedAdd α] [Nontrivial α] : NonAssocSemiring (WithTop α) where toNonUnitalNonAssocSemiring := instNonUnitalNonAssocSemiring __ := WithTop.instMulZeroOneClass __ := WithTop.addCommMonoidWithOne instance instNonUnitalSemiring [NonUnitalSemiring α] [PartialOrder α] [CanonicallyOrderedAdd α] [NoZeroDivisors α] : NonUnitalSemiring (WithTop α) where toNonUnitalNonAssocSemiring := WithTop.instNonUnitalNonAssocSemiring __ := WithTop.instSemigroupWithZero instance instSemiring [Semiring α] [PartialOrder α] [CanonicallyOrderedAdd α] [NoZeroDivisors α] [Nontrivial α] : Semiring (WithTop α) where toNonUnitalSemiring := WithTop.instNonUnitalSemiring __ := WithTop.instMonoidWithZero __ := WithTop.addCommMonoidWithOne instance instCommSemiring [CommSemiring α] [PartialOrder α] [CanonicallyOrderedAdd α] [NoZeroDivisors α] [Nontrivial α] : CommSemiring (WithTop α) where toSemiring := WithTop.instSemiring __ := WithTop.instCommMonoidWithZero instance instIsOrderedRing [CommSemiring α] [PartialOrder α] [CanonicallyOrderedAdd α] [NoZeroDivisors α] [Nontrivial α] : IsOrderedRing (WithTop α) := CanonicallyOrderedAdd.toIsOrderedRing /-- A version of `WithTop.map` for `RingHom`s. -/ @[simps -fullyApplied] protected def _root_.RingHom.withTopMap {R S : Type*} [NonAssocSemiring R] [PartialOrder R] [CanonicallyOrderedAdd R] [DecidableEq R] [Nontrivial R] [NonAssocSemiring S] [PartialOrder S] [CanonicallyOrderedAdd S] [DecidableEq S] [Nontrivial S] (f : R →+* S) (hf : Function.Injective f) : WithTop R →+* WithTop S := {MonoidWithZeroHom.withTopMap f.toMonoidWithZeroHom hf, f.toAddMonoidHom.withTopMap with} variable [CommSemiring α] [PartialOrder α] [CanonicallyOrderedAdd α] [PosMulStrictMono α] {a a₁ a₂ b₁ b₂ : WithTop α} @[gcongr] protected lemma mul_lt_mul (ha : a₁ < a₂) (hb : b₁ < b₂) : a₁ * b₁ < a₂ * b₂ := by have := posMulStrictMono_iff_mulPosStrictMono.1 ‹_› lift a₁ to α using ha.lt_top.ne lift b₁ to α using hb.lt_top.ne obtain rfl | ha₂ := eq_or_ne a₂ ⊤ · rw [top_mul (by simpa [bot_eq_zero] using hb.bot_lt.ne')] exact coe_lt_top _ obtain rfl | hb₂ := eq_or_ne b₂ ⊤ · rw [mul_top (by simpa [bot_eq_zero] using ha.bot_lt.ne')] exact coe_lt_top _ lift a₂ to α using ha₂ lift b₂ to α using hb₂ norm_cast at * obtain rfl | hb₁ := eq_zero_or_pos b₁ · rw [mul_zero] exact mul_pos (by simpa [bot_eq_zero] using ha.bot_lt) hb · exact mul_lt_mul ha hb.le hb₁ (zero_le _) variable [NoZeroDivisors α] [Nontrivial α] {a b : WithTop α} protected lemma pow_right_strictMono : ∀ {n : ℕ}, n ≠ 0 → StrictMono fun a : WithTop α ↦ a ^ n | 0, h => absurd rfl h | 1, _ => by simpa only [pow_one] using strictMono_id | n + 2, _ => fun x y h ↦ by
simp_rw [pow_succ _ (n + 1)] exact WithTop.mul_lt_mul (WithTop.pow_right_strictMono n.succ_ne_zero h) h
Mathlib/Algebra/Order/Ring/WithTop.lean
260
262
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Group.Pi.Lemmas import Mathlib.Algebra.GroupWithZero.Units.Equiv import Mathlib.Topology.Algebra.Monoid /-! # Topological group with zero In this file we define `HasContinuousInv₀` to be a mixin typeclass a type with `Inv` and `Zero` (e.g., a `GroupWithZero`) such that `fun x ↦ x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. Currently the only example of `HasContinuousInv₀` in `mathlib` which is not a normed field is the type `NNReal` (a.k.a. `ℝ≥0`) of nonnegative real numbers. Then we prove lemmas about continuity of `x ↦ x⁻¹` and `f / g` providing dot-style `*.inv₀` and `*.div` operations on `Filter.Tendsto`, `ContinuousAt`, `ContinuousWithinAt`, `ContinuousOn`, and `Continuous`. As a special case, we provide `*.div_const` operations that require only `DivInvMonoid` and `ContinuousMul` instances. All lemmas about `(⁻¹)` use `inv₀` in their names because lemmas without `₀` are used for `IsTopologicalGroup`s. We also use `'` in the typeclass name `HasContinuousInv₀` for the sake of consistency of notation. On a `GroupWithZero` with continuous multiplication, we also define left and right multiplication as homeomorphisms. -/ open Topology Filter Function /-! ### A `DivInvMonoid` with continuous multiplication If `G₀` is a `DivInvMonoid` with continuous `(*)`, then `(/y)` is continuous for any `y`. In this section we prove lemmas that immediately follow from this fact providing `*.div_const` dot-style operations on `Filter.Tendsto`, `ContinuousAt`, `ContinuousWithinAt`, `ContinuousOn`, and `Continuous`. -/ variable {α β G₀ : Type*} section DivConst variable [DivInvMonoid G₀] [TopologicalSpace G₀] [ContinuousMul G₀] {f : α → G₀} {s : Set α} {l : Filter α} theorem Filter.Tendsto.div_const {x : G₀} (hf : Tendsto f l (𝓝 x)) (y : G₀) : Tendsto (fun a => f a / y) l (𝓝 (x / y)) := by simpa only [div_eq_mul_inv] using hf.mul tendsto_const_nhds variable [TopologicalSpace α] nonrec theorem ContinuousAt.div_const {a : α} (hf : ContinuousAt f a) (y : G₀) : ContinuousAt (fun x => f x / y) a := hf.div_const y nonrec theorem ContinuousWithinAt.div_const {a} (hf : ContinuousWithinAt f s a) (y : G₀) : ContinuousWithinAt (fun x => f x / y) s a := hf.div_const _ theorem ContinuousOn.div_const (hf : ContinuousOn f s) (y : G₀) : ContinuousOn (fun x => f x / y) s := by simpa only [div_eq_mul_inv] using hf.mul continuousOn_const @[continuity, fun_prop] theorem Continuous.div_const (hf : Continuous f) (y : G₀) : Continuous fun x => f x / y := by simpa only [div_eq_mul_inv] using hf.mul continuous_const end DivConst /-- A type with `0` and `Inv` such that `fun x ↦ x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. -/ class HasContinuousInv₀ (G₀ : Type*) [Zero G₀] [Inv G₀] [TopologicalSpace G₀] : Prop where /-- The map `fun x ↦ x⁻¹` is continuous at all nonzero points. -/ continuousAt_inv₀ : ∀ ⦃x : G₀⦄, x ≠ 0 → ContinuousAt Inv.inv x export HasContinuousInv₀ (continuousAt_inv₀) section Inv₀ variable [Zero G₀] [Inv G₀] [TopologicalSpace G₀] [HasContinuousInv₀ G₀] {l : Filter α} {f : α → G₀} {s : Set α} {a : α} /-! ### Continuity of `fun x ↦ x⁻¹` at a non-zero point We define `HasContinuousInv₀` to be a `GroupWithZero` such that the operation `x ↦ x⁻¹` is continuous at all nonzero points. In this section we prove dot-style `*.inv₀` lemmas for `Filter.Tendsto`, `ContinuousAt`, `ContinuousWithinAt`, `ContinuousOn`, and `Continuous`. -/ theorem tendsto_inv₀ {x : G₀} (hx : x ≠ 0) : Tendsto Inv.inv (𝓝 x) (𝓝 x⁻¹) := continuousAt_inv₀ hx theorem continuousOn_inv₀ : ContinuousOn (Inv.inv : G₀ → G₀) {0}ᶜ := fun _x hx => (continuousAt_inv₀ hx).continuousWithinAt /-- If a function converges to a nonzero value, its inverse converges to the inverse of this value. We use the name `Filter.Tendsto.inv₀` as `Filter.Tendsto.inv` is already used in multiplicative topological groups. -/ theorem Filter.Tendsto.inv₀ {a : G₀} (hf : Tendsto f l (𝓝 a)) (ha : a ≠ 0) : Tendsto (fun x => (f x)⁻¹) l (𝓝 a⁻¹) := (tendsto_inv₀ ha).comp hf variable [TopologicalSpace α] nonrec theorem ContinuousWithinAt.inv₀ (hf : ContinuousWithinAt f s a) (ha : f a ≠ 0) : ContinuousWithinAt (fun x => (f x)⁻¹) s a := hf.inv₀ ha @[fun_prop] nonrec theorem ContinuousAt.inv₀ (hf : ContinuousAt f a) (ha : f a ≠ 0) : ContinuousAt (fun x => (f x)⁻¹) a := hf.inv₀ ha @[continuity, fun_prop] theorem Continuous.inv₀ (hf : Continuous f) (h0 : ∀ x, f x ≠ 0) : Continuous fun x => (f x)⁻¹ := continuous_iff_continuousAt.2 fun x => (hf.tendsto x).inv₀ (h0 x) @[fun_prop] theorem ContinuousOn.inv₀ (hf : ContinuousOn f s) (h0 : ∀ x ∈ s, f x ≠ 0) : ContinuousOn (fun x => (f x)⁻¹) s := fun x hx => (hf x hx).inv₀ (h0 x hx) end Inv₀ /-- If `G₀` is a group with zero with topology such that `x ↦ x⁻¹` is continuous at all nonzero points. Then the coercion `G₀ˣ → G₀` is a topological embedding. -/ theorem Units.isEmbedding_val₀ [GroupWithZero G₀] [TopologicalSpace G₀] [HasContinuousInv₀ G₀] : IsEmbedding (val : G₀ˣ → G₀) := embedding_val_mk <| (continuousOn_inv₀ (G₀ := G₀)).mono fun _ ↦ IsUnit.ne_zero @[deprecated (since := "2024-10-26")] alias Units.embedding_val₀ := Units.isEmbedding_val₀ section NhdsInv variable [GroupWithZero G₀] [TopologicalSpace G₀] [HasContinuousInv₀ G₀] {x : G₀} lemma nhds_inv₀ (hx : x ≠ 0) : 𝓝 x⁻¹ = (𝓝 x)⁻¹ := by refine le_antisymm (inv_le_iff_le_inv.1 ?_) (tendsto_inv₀ hx) simpa only [inv_inv] using tendsto_inv₀ (inv_ne_zero hx) lemma tendsto_inv_iff₀ {l : Filter α} {f : α → G₀} (hx : x ≠ 0) : Tendsto (fun x ↦ (f x)⁻¹) l (𝓝 x⁻¹) ↔ Tendsto f l (𝓝 x) := by simp only [nhds_inv₀ hx, ← Filter.comap_inv, tendsto_comap_iff, Function.comp_def, inv_inv] end NhdsInv /-! ### Continuity of division If `G₀` is a `GroupWithZero` with `x ↦ x⁻¹` continuous at all nonzero points and `(*)`, then division `(/)` is continuous at any point where the denominator is continuous. -/ section Div variable [GroupWithZero G₀] [TopologicalSpace G₀] [HasContinuousInv₀ G₀] [ContinuousMul G₀] {f g : α → G₀} theorem Filter.Tendsto.div {l : Filter α} {a b : G₀} (hf : Tendsto f l (𝓝 a)) (hg : Tendsto g l (𝓝 b)) (hy : b ≠ 0) : Tendsto (f / g) l (𝓝 (a / b)) := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv₀ hy) theorem Filter.tendsto_mul_iff_of_ne_zero [T1Space G₀] {f g : α → G₀} {l : Filter α} {x y : G₀} (hg : Tendsto g l (𝓝 y)) (hy : y ≠ 0) : Tendsto (fun n => f n * g n) l (𝓝 <| x * y) ↔ Tendsto f l (𝓝 x) := by refine ⟨fun hfg => ?_, fun hf => hf.mul hg⟩ rw [← mul_div_cancel_right₀ x hy] refine Tendsto.congr' ?_ (hfg.div hg hy) exact (hg.eventually_ne hy).mono fun n hn => mul_div_cancel_right₀ _ hn variable [TopologicalSpace α] [TopologicalSpace β] {s : Set α} {a : α} nonrec theorem ContinuousWithinAt.div (hf : ContinuousWithinAt f s a) (hg : ContinuousWithinAt g s a) (h₀ : g a ≠ 0) : ContinuousWithinAt (f / g) s a := hf.div hg h₀ theorem ContinuousOn.div (hf : ContinuousOn f s) (hg : ContinuousOn g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : ContinuousOn (f / g) s := fun x hx => (hf x hx).div (hg x hx) (h₀ x hx) /-- Continuity at a point of the result of dividing two functions continuous at that point, where the denominator is nonzero. -/ nonrec theorem ContinuousAt.div (hf : ContinuousAt f a) (hg : ContinuousAt g a) (h₀ : g a ≠ 0) : ContinuousAt (f / g) a := hf.div hg h₀ @[continuity] theorem Continuous.div (hf : Continuous f) (hg : Continuous g) (h₀ : ∀ x, g x ≠ 0) : Continuous (f / g) := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀) theorem continuousOn_div : ContinuousOn (fun p : G₀ × G₀ => p.1 / p.2) { p | p.2 ≠ 0 } := continuousOn_fst.div continuousOn_snd fun _ => id @[fun_prop] theorem Continuous.div₀ (hf : Continuous f) (hg : Continuous g) (h₀ : ∀ x, g x ≠ 0) : Continuous (fun x => f x / g x) := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀) @[fun_prop] theorem ContinuousAt.div₀ (hf : ContinuousAt f a) (hg : ContinuousAt g a) (h₀ : g a ≠ 0) : ContinuousAt (fun x => f x / g x) a := ContinuousAt.div hf hg h₀ @[fun_prop] theorem ContinuousOn.div₀ (hf : ContinuousOn f s) (hg : ContinuousOn g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : ContinuousOn (fun x => f x / g x) s := ContinuousOn.div hf hg h₀
/-- The function `f x / g x` is discontinuous when `g x = 0`. However, under appropriate
Mathlib/Topology/Algebra/GroupWithZero.lean
210
211
/- 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.QuasiIso import Mathlib.CategoryTheory.Limits.Preserves.Finite import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Kernels /-! # Functors which preserves homology If `F : C ⥤ D` is a functor between categories with zero morphisms, we shall say that `F` preserves homology when `F` preserves both kernels and cokernels. This typeclass is named `[F.PreservesHomology]`, and is automatically satisfied when `F` preserves both finite limits and finite colimits. If `S : ShortComplex C` and `[F.PreservesHomology]`, then there is an isomorphism `S.mapHomologyIso F : (S.map F).homology ≅ F.obj S.homology`, which is part of the natural isomorphism `homologyFunctorIso F` between the functors `F.mapShortComplex ⋙ homologyFunctor D` and `homologyFunctor C ⋙ F`. -/ namespace CategoryTheory open Category Limits variable {C D : Type*} [Category C] [Category D] [HasZeroMorphisms C] [HasZeroMorphisms D] namespace Functor variable (F : C ⥤ D) /-- A functor preserves homology when it preserves both kernels and cokernels. -/ class PreservesHomology (F : C ⥤ D) [PreservesZeroMorphisms F] : Prop where /-- the functor preserves kernels -/ preservesKernels ⦃X Y : C⦄ (f : X ⟶ Y) : PreservesLimit (parallelPair f 0) F := by infer_instance /-- the functor preserves cokernels -/ preservesCokernels ⦃X Y : C⦄ (f : X ⟶ Y) : PreservesColimit (parallelPair f 0) F := by infer_instance variable [PreservesZeroMorphisms F] /-- A functor which preserves homology preserves kernels. -/ lemma PreservesHomology.preservesKernel [F.PreservesHomology] {X Y : C} (f : X ⟶ Y) : PreservesLimit (parallelPair f 0) F := PreservesHomology.preservesKernels _ /-- A functor which preserves homology preserves cokernels. -/ lemma PreservesHomology.preservesCokernel [F.PreservesHomology] {X Y : C} (f : X ⟶ Y) : PreservesColimit (parallelPair f 0) F := PreservesHomology.preservesCokernels _ noncomputable instance preservesHomologyOfExact [PreservesFiniteLimits F] [PreservesFiniteColimits F] : F.PreservesHomology where end Functor namespace ShortComplex variable {S S₁ S₂ : ShortComplex C} namespace LeftHomologyData variable (h : S.LeftHomologyData) (F : C ⥤ D) /-- A left homology data `h` of a short complex `S` is preserved by a functor `F` is `F` preserves the kernel of `S.g : S.X₂ ⟶ S.X₃` and the cokernel of `h.f' : S.X₁ ⟶ h.K`. -/ class IsPreservedBy [F.PreservesZeroMorphisms] : Prop where /-- the functor preserves the kernel of `S.g : S.X₂ ⟶ S.X₃`. -/ g : PreservesLimit (parallelPair S.g 0) F /-- the functor preserves the cokernel of `h.f' : S.X₁ ⟶ h.K`. -/ f' : PreservesColimit (parallelPair h.f' 0) F variable [F.PreservesZeroMorphisms] noncomputable instance isPreservedBy_of_preservesHomology [F.PreservesHomology] : h.IsPreservedBy F where g := Functor.PreservesHomology.preservesKernel _ _ f' := Functor.PreservesHomology.preservesCokernel _ _ variable [h.IsPreservedBy F] include h in /-- When a left homology data is preserved by a functor `F`, this functor preserves the kernel of `S.g : S.X₂ ⟶ S.X₃`. -/ lemma IsPreservedBy.hg : PreservesLimit (parallelPair S.g 0) F := @IsPreservedBy.g _ _ _ _ _ _ _ h F _ _ /-- When a left homology data `h` is preserved by a functor `F`, this functor preserves the cokernel of `h.f' : S.X₁ ⟶ h.K`. -/ lemma IsPreservedBy.hf' : PreservesColimit (parallelPair h.f' 0) F := IsPreservedBy.f' /-- When a left homology data `h` of a short complex `S` is preserved by a functor `F`, this is the induced left homology data `h.map F` for the short complex `S.map F`. -/ @[simps] noncomputable def map : (S.map F).LeftHomologyData := by have := IsPreservedBy.hg h F have := IsPreservedBy.hf' h F have wi : F.map h.i ≫ F.map S.g = 0 := by rw [← F.map_comp, h.wi, F.map_zero] have hi := KernelFork.mapIsLimit _ h.hi F let f' : F.obj S.X₁ ⟶ F.obj h.K := hi.lift (KernelFork.ofι (S.map F).f (S.map F).zero) have hf' : f' = F.map h.f' := Fork.IsLimit.hom_ext hi (by rw [Fork.IsLimit.lift_ι hi] simp only [KernelFork.map_ι, Fork.ι_ofι, map_f, ← F.map_comp, f'_i]) have wπ : f' ≫ F.map h.π = 0 := by rw [hf', ← F.map_comp, f'_π, F.map_zero] have hπ : IsColimit (CokernelCofork.ofπ (F.map h.π) wπ) := by let e : parallelPair f' 0 ≅ parallelPair (F.map h.f') 0 := parallelPair.ext (Iso.refl _) (Iso.refl _) (by simpa using hf') (by simp) refine IsColimit.precomposeInvEquiv e _ (IsColimit.ofIsoColimit (CokernelCofork.mapIsColimit _ h.hπ' F) ?_) exact Cofork.ext (Iso.refl _) (by simp [e]) exact { K := F.obj h.K H := F.obj h.H i := F.map h.i π := F.map h.π wi := wi hi := hi wπ := wπ hπ := hπ } @[simp] lemma map_f' : (h.map F).f' = F.map h.f' := by rw [← cancel_mono (h.map F).i, f'_i, map_f, map_i, ← F.map_comp, f'_i] end LeftHomologyData /-- Given a left homology map data `ψ : LeftHomologyMapData φ h₁ h₂` such that both left homology data `h₁` and `h₂` are preserved by a functor `F`, this is the induced left homology map data for the morphism `F.mapShortComplex.map φ`. -/ @[simps] def LeftHomologyMapData.map {φ : S₁ ⟶ S₂} {h₁ : S₁.LeftHomologyData} {h₂ : S₂.LeftHomologyData} (ψ : LeftHomologyMapData φ h₁ h₂) (F : C ⥤ D) [F.PreservesZeroMorphisms] [h₁.IsPreservedBy F] [h₂.IsPreservedBy F] : LeftHomologyMapData (F.mapShortComplex.map φ) (h₁.map F) (h₂.map F) where φK := F.map ψ.φK φH := F.map ψ.φH commi := by simpa only [F.map_comp] using F.congr_map ψ.commi commf' := by simpa only [LeftHomologyData.map_f', F.map_comp] using F.congr_map ψ.commf' commπ := by simpa only [F.map_comp] using F.congr_map ψ.commπ namespace RightHomologyData variable (h : S.RightHomologyData) (F : C ⥤ D) /-- A right homology data `h` of a short complex `S` is preserved by a functor `F` is `F` preserves the cokernel of `S.f : S.X₁ ⟶ S.X₂` and the kernel of `h.g' : h.Q ⟶ S.X₃`. -/ class IsPreservedBy [F.PreservesZeroMorphisms] : Prop where /-- the functor preserves the cokernel of `S.f : S.X₁ ⟶ S.X₂`. -/ f : PreservesColimit (parallelPair S.f 0) F /-- the functor preserves the kernel of `h.g' : h.Q ⟶ S.X₃`. -/ g' : PreservesLimit (parallelPair h.g' 0) F variable [F.PreservesZeroMorphisms] noncomputable instance isPreservedBy_of_preservesHomology [F.PreservesHomology] : h.IsPreservedBy F where f := Functor.PreservesHomology.preservesCokernel F _ g' := Functor.PreservesHomology.preservesKernel F _ variable [h.IsPreservedBy F] include h in /-- When a right homology data is preserved by a functor `F`, this functor preserves the cokernel of `S.f : S.X₁ ⟶ S.X₂`. -/ lemma IsPreservedBy.hf : PreservesColimit (parallelPair S.f 0) F := @IsPreservedBy.f _ _ _ _ _ _ _ h F _ _ /-- When a right homology data `h` is preserved by a functor `F`, this functor preserves the kernel of `h.g' : h.Q ⟶ S.X₃`. -/ lemma IsPreservedBy.hg' : PreservesLimit (parallelPair h.g' 0) F := @IsPreservedBy.g' _ _ _ _ _ _ _ h F _ _ /-- When a right homology data `h` of a short complex `S` is preserved by a functor `F`, this is the induced right homology data `h.map F` for the short complex `S.map F`. -/ @[simps] noncomputable def map : (S.map F).RightHomologyData := by have := IsPreservedBy.hf h F have := IsPreservedBy.hg' h F have wp : F.map S.f ≫ F.map h.p = 0 := by rw [← F.map_comp, h.wp, F.map_zero] have hp := CokernelCofork.mapIsColimit _ h.hp F let g' : F.obj h.Q ⟶ F.obj S.X₃ := hp.desc (CokernelCofork.ofπ (S.map F).g (S.map F).zero) have hg' : g' = F.map h.g' := by apply Cofork.IsColimit.hom_ext hp rw [Cofork.IsColimit.π_desc hp] simp only [Cofork.π_ofπ, CokernelCofork.map_π, map_g, ← F.map_comp, p_g'] have wι : F.map h.ι ≫ g' = 0 := by rw [hg', ← F.map_comp, ι_g', F.map_zero] have hι : IsLimit (KernelFork.ofι (F.map h.ι) wι) := by let e : parallelPair g' 0 ≅ parallelPair (F.map h.g') 0 := parallelPair.ext (Iso.refl _) (Iso.refl _) (by simpa using hg') (by simp) refine IsLimit.postcomposeHomEquiv e _ (IsLimit.ofIsoLimit (KernelFork.mapIsLimit _ h.hι' F) ?_) exact Fork.ext (Iso.refl _) (by simp [e]) exact { Q := F.obj h.Q H := F.obj h.H p := F.map h.p ι := F.map h.ι wp := wp hp := hp wι := wι
hι := hι } @[simp]
Mathlib/Algebra/Homology/ShortComplex/PreservesHomology.lean
207
209
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import Mathlib.Topology.Category.TopCat.Limits.Pullbacks import Mathlib.Geometry.RingedSpace.LocallyRingedSpace /-! # Open immersions of structured spaces We say that a morphism of presheafed spaces `f : X ⟶ Y` is an open immersion if the underlying map of spaces is an open embedding `f : X ⟶ U ⊆ Y`, and the sheaf map `Y(V) ⟶ f _* X(V)` is an iso for each `V ⊆ U`. Abbreviations are also provided for `SheafedSpace`, `LocallyRingedSpace` and `Scheme`. ## Main definitions * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion`: the `Prop`-valued typeclass asserting that a PresheafedSpace hom `f` is an open_immersion. * `AlgebraicGeometry.IsOpenImmersion`: the `Prop`-valued typeclass asserting that a Scheme morphism `f` is an open_immersion. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.isoRestrict`: The source of an open immersion is isomorphic to the restriction of the target onto the image. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.lift`: Any morphism whose range is contained in an open immersion factors though the open immersion. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.toSheafedSpace`: If `f : X ⟶ Y` is an open immersion of presheafed spaces, and `Y` is a sheafed space, then `X` is also a sheafed space. The morphism as morphisms of sheafed spaces is given by `toSheafedSpaceHom`. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.toLocallyRingedSpace`: If `f : X ⟶ Y` is an open immersion of presheafed spaces, and `Y` is a locally ringed space, then `X` is also a locally ringed space. The morphism as morphisms of locally ringed spaces is given by `toLocallyRingedSpaceHom`. ## Main results * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.comp`: The composition of two open immersions is an open immersion. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.ofIso`: An iso is an open immersion. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.to_iso`: A surjective open immersion is an isomorphism. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.stalk_iso`: An open immersion induces an isomorphism on stalks. * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.hasPullback_of_left`: If `f` is an open immersion, then the pullback `(f, g)` exists (and the forgetful functor to `TopCat` preserves it). * `AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.pullbackSndOfLeft`: Open immersions are stable under pullbacks. * `AlgebraicGeometry.SheafedSpace.IsOpenImmersion.of_stalk_iso` An (topological) open embedding between two sheafed spaces is an open immersion if all the stalk maps are isomorphisms. -/ open TopologicalSpace CategoryTheory Opposite Topology open CategoryTheory.Limits namespace AlgebraicGeometry universe w v v₁ v₂ u variable {C : Type u} [Category.{v} C] /-- An open immersion of PresheafedSpaces is an open embedding `f : X ⟶ U ⊆ Y` of the underlying spaces, such that the sheaf map `Y(V) ⟶ f _* X(V)` is an iso for each `V ⊆ U`. -/ class PresheafedSpace.IsOpenImmersion {X Y : PresheafedSpace C} (f : X ⟶ Y) : Prop where /-- the underlying continuous map of underlying spaces from the source to an open subset of the target. -/ base_open : IsOpenEmbedding f.base /-- the underlying sheaf morphism is an isomorphism on each open subset -/ c_iso : ∀ U : Opens X, IsIso (f.c.app (op (base_open.isOpenMap.functor.obj U))) /-- A morphism of SheafedSpaces is an open immersion if it is an open immersion as a morphism of PresheafedSpaces -/ abbrev SheafedSpace.IsOpenImmersion {X Y : SheafedSpace C} (f : X ⟶ Y) : Prop := PresheafedSpace.IsOpenImmersion f /-- A morphism of LocallyRingedSpaces is an open immersion if it is an open immersion as a morphism of SheafedSpaces -/ abbrev LocallyRingedSpace.IsOpenImmersion {X Y : LocallyRingedSpace} (f : X ⟶ Y) : Prop := SheafedSpace.IsOpenImmersion f.1 namespace PresheafedSpace.IsOpenImmersion open PresheafedSpace local notation "IsOpenImmersion" => PresheafedSpace.IsOpenImmersion attribute [instance] IsOpenImmersion.c_iso section variable {X Y : PresheafedSpace C} (f : X ⟶ Y) [H : IsOpenImmersion f] /-- The functor `Opens X ⥤ Opens Y` associated with an open immersion `f : X ⟶ Y`. -/ abbrev opensFunctor := H.base_open.isOpenMap.functor /-- An open immersion `f : X ⟶ Y` induces an isomorphism `X ≅ Y|_{f(X)}`. -/ @[simps! hom_c_app] noncomputable def isoRestrict : X ≅ Y.restrict H.base_open := PresheafedSpace.isoOfComponents (Iso.refl _) <| by symm fapply NatIso.ofComponents · intro U refine asIso (f.c.app (op (opensFunctor f |>.obj (unop U)))) ≪≫ X.presheaf.mapIso (eqToIso ?_) induction U with | op U => ?_ cases U dsimp only [IsOpenMap.functor, Functor.op, Opens.map] congr 2 erw [Set.preimage_image_eq _ H.base_open.injective] rfl · intro U V i dsimp simp only [NatTrans.naturality_assoc, TopCat.Presheaf.pushforward_obj_obj, TopCat.Presheaf.pushforward_obj_map, Quiver.Hom.unop_op, Category.assoc] rw [← X.presheaf.map_comp, ← X.presheaf.map_comp] congr 1 @[reassoc (attr := simp)] theorem isoRestrict_hom_ofRestrict : (isoRestrict f).hom ≫ Y.ofRestrict _ = f := by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `ext` did not pick up `NatTrans.ext` refine PresheafedSpace.Hom.ext _ _ rfl <| NatTrans.ext <| funext fun x => ?_ simp only [isoRestrict_hom_c_app, NatTrans.comp_app, eqToHom_refl, ofRestrict_c_app, Category.assoc, whiskerRight_id'] erw [Category.comp_id, comp_c_app, f.c.naturality_assoc, ← X.presheaf.map_comp] trans f.c.app x ≫ X.presheaf.map (𝟙 _) · congr 1 · simp @[reassoc (attr := simp)] theorem isoRestrict_inv_ofRestrict : (isoRestrict f).inv ≫ f = Y.ofRestrict _ := by rw [Iso.inv_comp_eq, isoRestrict_hom_ofRestrict] instance mono : Mono f := by rw [← H.isoRestrict_hom_ofRestrict]; apply mono_comp lemma c_iso' {V : Opens Y} (U : Opens X) (h : V = (opensFunctor f).obj U) : IsIso (f.c.app (Opposite.op V)) := by subst h infer_instance /-- The composition of two open immersions is an open immersion. -/ instance comp {Z : PresheafedSpace C} (g : Y ⟶ Z) [hg : IsOpenImmersion g] : IsOpenImmersion (f ≫ g) where base_open := hg.base_open.comp H.base_open c_iso U := by generalize_proofs h dsimp only [AlgebraicGeometry.PresheafedSpace.comp_c_app, unop_op, Functor.op, comp_base, Opens.map_comp_obj] apply IsIso.comp_isIso' · exact c_iso' g ((opensFunctor f).obj U) (by ext; simp) · apply c_iso' f U ext1 dsimp only [Opens.map_coe, IsOpenMap.coe_functor_obj, comp_base, TopCat.coe_comp] rw [Set.image_comp, Set.preimage_image_eq _ hg.base_open.injective] /-- For an open immersion `f : X ⟶ Y` and an open set `U ⊆ X`, we have the map `X(U) ⟶ Y(U)`. -/ noncomputable def invApp (U : Opens X) : X.presheaf.obj (op U) ⟶ Y.presheaf.obj (op (opensFunctor f |>.obj U)) := X.presheaf.map (eqToHom (by simp [Opens.map, Set.preimage_image_eq _ H.base_open.injective])) ≫ inv (f.c.app (op (opensFunctor f |>.obj U))) @[simp, reassoc] theorem inv_naturality {U V : (Opens X)ᵒᵖ} (i : U ⟶ V) : X.presheaf.map i ≫ H.invApp _ (unop V) = invApp f (unop U) ≫ Y.presheaf.map (opensFunctor f |>.op.map i) := by simp only [invApp, ← Category.assoc] rw [IsIso.comp_inv_eq] simp only [Functor.op_obj, op_unop, ← X.presheaf.map_comp, Functor.op_map, Category.assoc, NatTrans.naturality, Quiver.Hom.unop_op, IsIso.inv_hom_id_assoc, TopCat.Presheaf.pushforward_obj_map] congr 1 instance (U : Opens X) : IsIso (invApp f U) := by delta invApp; infer_instance theorem inv_invApp (U : Opens X) : inv (H.invApp _ U) = f.c.app (op (opensFunctor f |>.obj U)) ≫ X.presheaf.map (eqToHom (by simp [Opens.map, Set.preimage_image_eq _ H.base_open.injective])) := by rw [← cancel_epi (H.invApp _ U), IsIso.hom_inv_id] delta invApp simp [← Functor.map_comp] @[simp, reassoc, elementwise] theorem invApp_app (U : Opens X) : invApp f U ≫ f.c.app (op (opensFunctor f |>.obj U)) = X.presheaf.map (eqToHom (by simp [Opens.map, Set.preimage_image_eq _ H.base_open.injective])) := by rw [invApp, Category.assoc, IsIso.inv_hom_id, Category.comp_id] @[simp, reassoc] theorem app_invApp (U : Opens Y) : f.c.app (op U) ≫ H.invApp _ ((Opens.map f.base).obj U) = Y.presheaf.map ((homOfLE (Set.image_preimage_subset f.base U.1)).op : op U ⟶ op (opensFunctor f |>.obj ((Opens.map f.base).obj U))) := by erw [← Category.assoc]; rw [IsIso.comp_inv_eq, f.c.naturality]; congr /-- A variant of `app_inv_app` that gives an `eqToHom` instead of `homOfLe`. -/ @[reassoc] theorem app_inv_app' (U : Opens Y) (hU : (U : Set Y) ⊆ Set.range f.base) : f.c.app (op U) ≫ invApp f ((Opens.map f.base).obj U) = Y.presheaf.map (eqToHom (le_antisymm (Set.image_preimage_subset f.base U.1) <| (Set.image_preimage_eq_inter_range (f := f.base) (t := U.1)).symm ▸ Set.subset_inter_iff.mpr ⟨fun _ h => h, hU⟩)).op := by erw [← Category.assoc]; rw [IsIso.comp_inv_eq, f.c.naturality]; congr /-- An isomorphism is an open immersion. -/ instance ofIso {X Y : PresheafedSpace C} (H : X ≅ Y) : IsOpenImmersion H.hom where base_open := (TopCat.homeoOfIso ((forget C).mapIso H)).isOpenEmbedding -- Porting note: `inferInstance` will fail if Lean is not told that `H.hom.c` is iso c_iso _ := letI : IsIso H.hom.c := c_isIso_of_iso H.hom; inferInstance instance (priority := 100) ofIsIso {X Y : PresheafedSpace C} (f : X ⟶ Y) [IsIso f] : IsOpenImmersion f := AlgebraicGeometry.PresheafedSpace.IsOpenImmersion.ofIso (asIso f) instance ofRestrict {X : TopCat} (Y : PresheafedSpace C) {f : X ⟶ Y.carrier} (hf : IsOpenEmbedding f) : IsOpenImmersion (Y.ofRestrict hf) where base_open := hf c_iso U := by dsimp have : (Opens.map f).obj (hf.isOpenMap.functor.obj U) = U := by ext1 exact Set.preimage_image_eq _ hf.injective convert_to IsIso (Y.presheaf.map (𝟙 _)) · congr · -- Porting note: was `apply Subsingleton.helim; rw [this]` -- See https://github.com/leanprover/lean4/issues/2273 congr · simp only [unop_op] congr apply Subsingleton.helim rw [this] · infer_instance @[elementwise, simp] theorem ofRestrict_invApp {C : Type*} [Category C] (X : PresheafedSpace C) {Y : TopCat} {f : Y ⟶ TopCat.of X.carrier} (h : IsOpenEmbedding f) (U : Opens (X.restrict h).carrier) : (PresheafedSpace.IsOpenImmersion.ofRestrict X h).invApp _ U = 𝟙 _ := by delta invApp rw [IsIso.comp_inv_eq, Category.id_comp] change X.presheaf.map _ = X.presheaf.map _ congr 1 /-- An open immersion is an iso if the underlying continuous map is epi. -/ theorem to_iso [h' : Epi f.base] : IsIso f := by have : ∀ (U : (Opens Y)ᵒᵖ), IsIso (f.c.app U) := by intro U have : U = op (opensFunctor f |>.obj ((Opens.map f.base).obj (unop U))) := by induction U with | op U => ?_ cases U dsimp only [Functor.op, Opens.map] congr exact (Set.image_preimage_eq _ ((TopCat.epi_iff_surjective _).mp h')).symm convert H.c_iso (Opens.map f.base |>.obj <| unop U) have : IsIso f.c := NatIso.isIso_of_isIso_app _ apply (config := { allowSynthFailures := true }) isIso_of_components let t : X ≃ₜ Y := H.base_open.isEmbedding.toHomeomorph.trans { toFun := Subtype.val invFun := fun x => ⟨x, by rw [Set.range_eq_univ.mpr ((TopCat.epi_iff_surjective _).mp h')]; trivial⟩ left_inv := fun ⟨_, _⟩ => rfl right_inv := fun _ => rfl } exact (TopCat.isoOfHomeo t).isIso_hom instance stalk_iso [HasColimits C] (x : X) : IsIso (f.stalkMap x) := by rw [← H.isoRestrict_hom_ofRestrict, PresheafedSpace.stalkMap.comp] infer_instance end noncomputable section Pullback variable {X Y Z : PresheafedSpace C} (f : X ⟶ Z) [hf : IsOpenImmersion f] (g : Y ⟶ Z) /-- (Implementation.) The projection map when constructing the pullback along an open immersion. -/ def pullbackConeOfLeftFst : Y.restrict (TopCat.snd_isOpenEmbedding_of_left hf.base_open g.base) ⟶ X where base := pullback.fst _ _ c := { app := fun U => hf.invApp _ (unop U) ≫ g.c.app (op (hf.base_open.isOpenMap.functor.obj (unop U))) ≫ Y.presheaf.map (eqToHom (by simp only [IsOpenMap.functor, Subtype.mk_eq_mk, unop_op, op_inj_iff, Opens.map, Subtype.coe_mk, Functor.op_obj] apply LE.le.antisymm · rintro _ ⟨_, h₁, h₂⟩ use (TopCat.pullbackIsoProdSubtype _ _).inv ⟨⟨_, _⟩, h₂⟩ -- Porting note: need a slight hand holding -- used to be `simpa using h₁` before https://github.com/leanprover-community/mathlib4/pull/13170 change _ ∈ _ ⁻¹' _ ∧ _ simp only [TopCat.coe_of, restrict_carrier, Set.preimage_id', Set.mem_preimage, SetLike.mem_coe] constructor · change _ ∈ U.unop at h₁ convert h₁ rw [TopCat.pullbackIsoProdSubtype_inv_fst_apply] · rw [TopCat.pullbackIsoProdSubtype_inv_snd_apply] · rintro _ ⟨x, h₁, rfl⟩ exact ⟨_, h₁, CategoryTheory.congr_fun pullback.condition x⟩)) naturality := by intro U V i induction U induction V -- Note: this doesn't fire in `simp` because of reduction of the term via structure eta -- before discrimination tree key generation rw [inv_naturality_assoc] dsimp simp only [NatTrans.naturality_assoc, TopCat.Presheaf.pushforward_obj_map, Quiver.Hom.unop_op, ← Functor.map_comp, Category.assoc] rfl } theorem pullback_cone_of_left_condition : pullbackConeOfLeftFst f g ≫ f = Y.ofRestrict _ ≫ g := by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `ext` did not pick up `NatTrans.ext` refine PresheafedSpace.Hom.ext _ _ ?_ <| NatTrans.ext <| funext fun U => ?_ · simpa using pullback.condition · induction U -- Porting note: `NatTrans.comp_app` is not picked up by `dsimp` -- Perhaps see : https://github.com/leanprover-community/mathlib4/issues/5026 rw [NatTrans.comp_app] dsimp only [comp_c_app, unop_op, whiskerRight_app, pullbackConeOfLeftFst] -- simp only [ofRestrict_c_app, NatTrans.comp_app] simp only [app_invApp_assoc, eqToHom_app, Category.assoc, NatTrans.naturality_assoc] erw [← Y.presheaf.map_comp, ← Y.presheaf.map_comp] congr 1 /-- We construct the pullback along an open immersion via restricting along the pullback of the maps of underlying spaces (which is also an open embedding). -/ def pullbackConeOfLeft : PullbackCone f g := PullbackCone.mk (pullbackConeOfLeftFst f g) (Y.ofRestrict _) (pullback_cone_of_left_condition f g) variable (s : PullbackCone f g) /-- (Implementation.) Any cone over `cospan f g` indeed factors through the constructed cone. -/ def pullbackConeOfLeftLift : s.pt ⟶ (pullbackConeOfLeft f g).pt where base := pullback.lift s.fst.base s.snd.base (congr_arg (fun x => PresheafedSpace.Hom.base x) s.condition) c := { app := fun U => s.snd.c.app _ ≫ s.pt.presheaf.map (eqToHom (by dsimp only [Opens.map, IsOpenMap.functor, Functor.op] congr 2 let s' : PullbackCone f.base g.base := PullbackCone.mk s.fst.base s.snd.base -- Porting note: in mathlib3, this is just an underscore (congr_arg Hom.base s.condition) have : _ = s.snd.base := limit.lift_π s' WalkingCospan.right conv_lhs => rw [← this] dsimp [s'] rw [Function.comp_def, ← Set.preimage_preimage] rw [Set.preimage_image_eq _ (TopCat.snd_isOpenEmbedding_of_left hf.base_open g.base).injective] rfl)) naturality := fun U V i => by erw [s.snd.c.naturality_assoc] rw [Category.assoc] erw [← s.pt.presheaf.map_comp, ← s.pt.presheaf.map_comp] congr 1 } -- this lemma is not a `simp` lemma, because it is an implementation detail theorem pullbackConeOfLeftLift_fst : pullbackConeOfLeftLift f g s ≫ (pullbackConeOfLeft f g).fst = s.fst := by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `ext` did not pick up `NatTrans.ext` refine PresheafedSpace.Hom.ext _ _ ?_ <| NatTrans.ext <| funext fun x => ?_ · change pullback.lift _ _ _ ≫ pullback.fst _ _ = _ simp · induction x with | op x => ?_ change ((_ ≫ _) ≫ _ ≫ _) ≫ _ = _ simp_rw [Category.assoc] erw [← s.pt.presheaf.map_comp] erw [s.snd.c.naturality_assoc] have := congr_app s.condition (op (opensFunctor f |>.obj x)) dsimp only [comp_c_app, unop_op] at this rw [← IsIso.comp_inv_eq] at this replace this := reassoc_of% this erw [← this, hf.invApp_app_assoc, s.fst.c.naturality_assoc] simp [eqToHom_map] -- this lemma is not a `simp` lemma, because it is an implementation detail theorem pullbackConeOfLeftLift_snd : pullbackConeOfLeftLift f g s ≫ (pullbackConeOfLeft f g).snd = s.snd := by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `ext` did not pick up `NatTrans.ext` refine PresheafedSpace.Hom.ext _ _ ?_ <| NatTrans.ext <| funext fun x => ?_ · change pullback.lift _ _ _ ≫ pullback.snd _ _ = _ simp · change (_ ≫ _ ≫ _) ≫ _ = _ simp_rw [Category.assoc] erw [s.snd.c.naturality_assoc] erw [← s.pt.presheaf.map_comp, ← s.pt.presheaf.map_comp] trans s.snd.c.app x ≫ s.pt.presheaf.map (𝟙 _) · congr 1 · simp instance pullbackConeSndIsOpenImmersion : IsOpenImmersion (pullbackConeOfLeft f g).snd := by erw [CategoryTheory.Limits.PullbackCone.mk_snd] infer_instance /-- The constructed pullback cone is indeed the pullback. -/ def pullbackConeOfLeftIsLimit : IsLimit (pullbackConeOfLeft f g) := by apply PullbackCone.isLimitAux' intro s use pullbackConeOfLeftLift f g s use pullbackConeOfLeftLift_fst f g s use pullbackConeOfLeftLift_snd f g s intro m _ h₂ rw [← cancel_mono (pullbackConeOfLeft f g).snd] exact h₂.trans (pullbackConeOfLeftLift_snd f g s).symm instance hasPullback_of_left : HasPullback f g := ⟨⟨⟨_, pullbackConeOfLeftIsLimit f g⟩⟩⟩ instance hasPullback_of_right : HasPullback g f := hasPullback_symmetry f g /-- Open immersions are stable under base-change. -/ instance pullbackSndOfLeft : IsOpenImmersion (pullback.snd f g) := by delta pullback.snd rw [← limit.isoLimitCone_hom_π ⟨_, pullbackConeOfLeftIsLimit f g⟩ WalkingCospan.right] infer_instance /-- Open immersions are stable under base-change. -/ instance pullbackFstOfRight : IsOpenImmersion (pullback.fst g f) := by rw [← pullbackSymmetry_hom_comp_snd] infer_instance instance pullbackToBaseIsOpenImmersion [IsOpenImmersion g] : IsOpenImmersion (limit.π (cospan f g) WalkingCospan.one) := by rw [← limit.w (cospan f g) WalkingCospan.Hom.inl, cospan_map_inl] infer_instance instance forget_preservesLimitsOfLeft : PreservesLimit (cospan f g) (forget C) := preservesLimit_of_preserves_limit_cone (pullbackConeOfLeftIsLimit f g) (by apply (IsLimit.postcomposeHomEquiv (diagramIsoCospan _) _).toFun refine (IsLimit.equivIsoLimit ?_).toFun (limit.isLimit (cospan f.base g.base)) fapply Cones.ext · exact Iso.refl _ change ∀ j, _ = 𝟙 _ ≫ _ ≫ _ simp_rw [Category.id_comp] rintro (_ | _ | _) <;> symm · erw [Category.comp_id] exact limit.w (cospan f.base g.base) WalkingCospan.Hom.inl · exact Category.comp_id _ · exact Category.comp_id _) instance forget_preservesLimitsOfRight : PreservesLimit (cospan g f) (forget C) := preservesPullback_symmetry (forget C) f g theorem pullback_snd_isIso_of_range_subset (H : Set.range g.base ⊆ Set.range f.base) : IsIso (pullback.snd f g) := by haveI := TopCat.snd_iso_of_left_embedding_range_subset hf.base_open.isEmbedding g.base H have : IsIso (pullback.snd f g).base := by delta pullback.snd rw [← limit.isoLimitCone_hom_π ⟨_, pullbackConeOfLeftIsLimit f g⟩ WalkingCospan.right] change IsIso (_ ≫ pullback.snd _ _) infer_instance apply to_iso /-- The universal property of open immersions: For an open immersion `f : X ⟶ Z`, given any morphism of schemes `g : Y ⟶ Z` whose topological image is contained in the image of `f`, we can lift this morphism to a unique `Y ⟶ X` that commutes with these maps. -/ def lift (H : Set.range g.base ⊆ Set.range f.base) : Y ⟶ X := haveI := pullback_snd_isIso_of_range_subset f g H inv (pullback.snd f g) ≫ pullback.fst _ _ @[simp, reassoc] theorem lift_fac (H : Set.range g.base ⊆ Set.range f.base) : lift f g H ≫ f = g := by erw [Category.assoc]; rw [IsIso.inv_comp_eq]; exact pullback.condition theorem lift_uniq (H : Set.range g.base ⊆ Set.range f.base) (l : Y ⟶ X) (hl : l ≫ f = g) : l = lift f g H := by rw [← cancel_mono f, hl, lift_fac] /-- Two open immersions with equal range is isomorphic. -/ @[simps] def isoOfRangeEq [IsOpenImmersion g] (e : Set.range f.base = Set.range g.base) : X ≅ Y where hom := lift g f (le_of_eq e) inv := lift f g (le_of_eq e.symm) hom_inv_id := by rw [← cancel_mono f]; simp inv_hom_id := by rw [← cancel_mono g]; simp end Pullback open CategoryTheory.Limits.WalkingCospan section ToSheafedSpace variable {X : PresheafedSpace C} (Y : SheafedSpace C) /-- If `X ⟶ Y` is an open immersion, and `Y` is a SheafedSpace, then so is `X`. -/ def toSheafedSpace (f : X ⟶ Y.toPresheafedSpace) [H : IsOpenImmersion f] : SheafedSpace C where IsSheaf := by apply TopCat.Presheaf.isSheaf_of_iso (sheafIsoOfIso (isoRestrict f).symm).symm apply TopCat.Sheaf.pushforward_sheaf_of_sheaf exact (Y.restrict H.base_open).IsSheaf toPresheafedSpace := X variable (f : X ⟶ Y.toPresheafedSpace) [H : IsOpenImmersion f] @[simp] theorem toSheafedSpace_toPresheafedSpace : (toSheafedSpace Y f).toPresheafedSpace = X := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a SheafedSpace, we can upgrade it into a morphism of SheafedSpaces. -/ def toSheafedSpaceHom : toSheafedSpace Y f ⟶ Y := f @[simp] theorem toSheafedSpaceHom_base : (toSheafedSpaceHom Y f).base = f.base := rfl @[simp] theorem toSheafedSpaceHom_c : (toSheafedSpaceHom Y f).c = f.c := rfl instance toSheafedSpace_isOpenImmersion : SheafedSpace.IsOpenImmersion (toSheafedSpaceHom Y f) := H @[simp] theorem sheafedSpace_toSheafedSpace {X Y : SheafedSpace C} (f : X ⟶ Y) [IsOpenImmersion f] : toSheafedSpace Y f = X := by cases X; rfl end ToSheafedSpace section ToLocallyRingedSpace variable {X : PresheafedSpace CommRingCat} (Y : LocallyRingedSpace) variable (f : X ⟶ Y.toPresheafedSpace) [H : IsOpenImmersion f] /-- If `X ⟶ Y` is an open immersion, and `Y` is a LocallyRingedSpace, then so is `X`. -/ def toLocallyRingedSpace : LocallyRingedSpace where toSheafedSpace := toSheafedSpace Y.toSheafedSpace f isLocalRing x := haveI : IsLocalRing (Y.presheaf.stalk (f.base x)) := Y.isLocalRing _ (asIso (f.stalkMap x)).commRingCatIsoToRingEquiv.isLocalRing @[simp] theorem toLocallyRingedSpace_toSheafedSpace : (toLocallyRingedSpace Y f).toSheafedSpace = toSheafedSpace Y.1 f := rfl /-- If `X ⟶ Y` is an open immersion of PresheafedSpaces, and `Y` is a LocallyRingedSpace, we can upgrade it into a morphism of LocallyRingedSpace. -/ def toLocallyRingedSpaceHom : toLocallyRingedSpace Y f ⟶ Y := ⟨f, fun _ => inferInstance⟩ @[simp] theorem toLocallyRingedSpaceHom_val : (toLocallyRingedSpaceHom Y f).toShHom = f := rfl instance toLocallyRingedSpace_isOpenImmersion : LocallyRingedSpace.IsOpenImmersion (toLocallyRingedSpaceHom Y f) := H @[simp] theorem locallyRingedSpace_toLocallyRingedSpace {X Y : LocallyRingedSpace} (f : X ⟶ Y) [LocallyRingedSpace.IsOpenImmersion f] : toLocallyRingedSpace Y f.1 = X := by cases X; delta toLocallyRingedSpace; simp end ToLocallyRingedSpace theorem isIso_of_subset {X Y : PresheafedSpace C} (f : X ⟶ Y) [H : PresheafedSpace.IsOpenImmersion f] (U : Opens Y.carrier) (hU : (U : Set Y.carrier) ⊆ Set.range f.base) : IsIso (f.c.app <| op U) := by have : U = H.base_open.isOpenMap.functor.obj ((Opens.map f.base).obj U) := by ext1 exact (Set.inter_eq_left.mpr hU).symm.trans Set.image_preimage_eq_inter_range.symm convert H.c_iso ((Opens.map f.base).obj U) end PresheafedSpace.IsOpenImmersion namespace SheafedSpace.IsOpenImmersion instance (priority := 100) of_isIso {X Y : SheafedSpace C} (f : X ⟶ Y) [IsIso f] : SheafedSpace.IsOpenImmersion f := @PresheafedSpace.IsOpenImmersion.ofIsIso _ _ _ _ f (SheafedSpace.forgetToPresheafedSpace.map_isIso _) instance comp {X Y Z : SheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) [SheafedSpace.IsOpenImmersion f] [SheafedSpace.IsOpenImmersion g] : SheafedSpace.IsOpenImmersion (f ≫ g) := PresheafedSpace.IsOpenImmersion.comp f g noncomputable section Pullback variable {X Y Z : SheafedSpace C} (f : X ⟶ Z) (g : Y ⟶ Z) variable [H : SheafedSpace.IsOpenImmersion f] -- Porting note: in mathlib3, this local notation is often followed by a space to avoid confusion -- with the forgetful functor, now it is often wrapped in a parenthesis local notation "forget" => SheafedSpace.forgetToPresheafedSpace open CategoryTheory.Limits.WalkingCospan instance : Mono f := (forget).mono_of_mono_map (show @Mono (PresheafedSpace C) _ _ _ f by infer_instance) instance forgetMapIsOpenImmersion : PresheafedSpace.IsOpenImmersion ((forget).map f) := ⟨H.base_open, H.c_iso⟩ instance hasLimit_cospan_forget_of_left : HasLimit (cospan f g ⋙ forget) := by have : HasLimit (cospan ((cospan f g ⋙ forget).map Hom.inl) ((cospan f g ⋙ forget).map Hom.inr)) := by change HasLimit (cospan ((forget).map f) ((forget).map g)) infer_instance apply hasLimit_of_iso (diagramIsoCospan _).symm instance hasLimit_cospan_forget_of_left' : HasLimit (cospan ((cospan f g ⋙ forget).map Hom.inl) ((cospan f g ⋙ forget).map Hom.inr)) := show HasLimit (cospan ((forget).map f) ((forget).map g)) from inferInstance instance hasLimit_cospan_forget_of_right : HasLimit (cospan g f ⋙ forget) := by have : HasLimit (cospan ((cospan g f ⋙ forget).map Hom.inl) ((cospan g f ⋙ forget).map Hom.inr)) := by change HasLimit (cospan ((forget).map g) ((forget).map f)) infer_instance apply hasLimit_of_iso (diagramIsoCospan _).symm instance hasLimit_cospan_forget_of_right' : HasLimit (cospan ((cospan g f ⋙ forget).map Hom.inl) ((cospan g f ⋙ forget).map Hom.inr)) := show HasLimit (cospan ((forget).map g) ((forget).map f)) from inferInstance instance forgetCreatesPullbackOfLeft : CreatesLimit (cospan f g) forget := createsLimitOfFullyFaithfulOfIso (PresheafedSpace.IsOpenImmersion.toSheafedSpace Y (@pullback.snd (PresheafedSpace C) _ _ _ _ f g _)) (eqToIso (show pullback _ _ = pullback _ _ by congr) ≪≫ HasLimit.isoOfNatIso (diagramIsoCospan _).symm) instance forgetCreatesPullbackOfRight : CreatesLimit (cospan g f) forget := createsLimitOfFullyFaithfulOfIso (PresheafedSpace.IsOpenImmersion.toSheafedSpace Y (@pullback.fst (PresheafedSpace C) _ _ _ _ g f _)) (eqToIso (show pullback _ _ = pullback _ _ by congr) ≪≫ HasLimit.isoOfNatIso (diagramIsoCospan _).symm) instance sheafedSpace_forgetPreserves_of_left : PreservesLimit (cospan f g) (SheafedSpace.forget C) := @Limits.comp_preservesLimit _ _ _ _ _ _ (cospan f g) _ _ forget (PresheafedSpace.forget C) inferInstance <| by have : PreservesLimit (cospan ((cospan f g ⋙ forget).map Hom.inl) ((cospan f g ⋙ forget).map Hom.inr)) (PresheafedSpace.forget C) := by dsimp infer_instance apply preservesLimit_of_iso_diagram _ (diagramIsoCospan _).symm instance sheafedSpace_forgetPreserves_of_right : PreservesLimit (cospan g f) (SheafedSpace.forget C) := preservesPullback_symmetry _ _ _ instance sheafedSpace_hasPullback_of_left : HasPullback f g := hasLimit_of_created (cospan f g) forget instance sheafedSpace_hasPullback_of_right : HasPullback g f := hasLimit_of_created (cospan g f) forget /-- Open immersions are stable under base-change. -/ instance sheafedSpace_pullback_snd_of_left : SheafedSpace.IsOpenImmersion (pullback.snd f g) := by delta pullback.snd have : _ = limit.π (cospan f g) right := preservesLimitIso_hom_π forget (cospan f g) right rw [← this] have := HasLimit.isoOfNatIso_hom_π (diagramIsoCospan (cospan f g ⋙ forget)) right erw [Category.comp_id] at this rw [← this] dsimp infer_instance instance sheafedSpace_pullback_fst_of_right : SheafedSpace.IsOpenImmersion (pullback.fst g f) := by delta pullback.fst have : _ = limit.π (cospan g f) left := preservesLimitIso_hom_π forget (cospan g f) left rw [← this] have := HasLimit.isoOfNatIso_hom_π (diagramIsoCospan (cospan g f ⋙ forget)) left erw [Category.comp_id] at this rw [← this] dsimp infer_instance instance sheafedSpace_pullback_to_base_isOpenImmersion [SheafedSpace.IsOpenImmersion g] : SheafedSpace.IsOpenImmersion (limit.π (cospan f g) one : pullback f g ⟶ Z) := by rw [← limit.w (cospan f g) Hom.inl, cospan_map_inl] infer_instance end Pullback section OfStalkIso variable [HasLimits C] [HasColimits C] {FC : C → C → Type*} {CC : C → Type v} variable [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [instCC : ConcreteCategory.{v} C FC] variable [(CategoryTheory.forget C).ReflectsIsomorphisms] [PreservesLimits (CategoryTheory.forget C)] variable [PreservesFilteredColimits (CategoryTheory.forget C)] include instCC in /-- Suppose `X Y : SheafedSpace C`, where `C` is a concrete category, whose forgetful functor reflects isomorphisms, preserves limits and filtered colimits. Then a morphism `X ⟶ Y` that is a topological open embedding is an open immersion iff every stalk map is an iso. -/ theorem of_stalk_iso {X Y : SheafedSpace C} (f : X ⟶ Y) (hf : IsOpenEmbedding f.base) [H : ∀ x : X.1, IsIso (f.stalkMap x)] : SheafedSpace.IsOpenImmersion f := { base_open := hf c_iso := fun U => by apply (config := {allowSynthFailures := true}) TopCat.Presheaf.app_isIso_of_stalkFunctor_map_iso (show Y.sheaf ⟶ (TopCat.Sheaf.pushforward _ f.base).obj X.sheaf from ⟨f.c⟩) rintro ⟨_, y, hy, rfl⟩ specialize H y delta PresheafedSpace.Hom.stalkMap at H haveI H' := TopCat.Presheaf.stalkPushforward.stalkPushforward_iso_of_isInducing C hf.toIsInducing X.presheaf y have := IsIso.comp_isIso' H (@IsIso.inv_isIso _ _ _ _ _ H') rwa [Category.assoc, IsIso.hom_inv_id, Category.comp_id] at this } end OfStalkIso section variable {X Y : SheafedSpace C} (f : X ⟶ Y) [H : IsOpenImmersion f] /-- The functor `Opens X ⥤ Opens Y` associated with an open immersion `f : X ⟶ Y`. -/ abbrev opensFunctor : Opens X ⥤ Opens Y := H.base_open.isOpenMap.functor /-- An open immersion `f : X ⟶ Y` induces an isomorphism `X ≅ Y|_{f(X)}`. -/ @[simps! hom_c_app] noncomputable def isoRestrict : X ≅ Y.restrict H.base_open := SheafedSpace.isoMk <| PresheafedSpace.IsOpenImmersion.isoRestrict f @[reassoc (attr := simp)] theorem isoRestrict_hom_ofRestrict : (isoRestrict f).hom ≫ Y.ofRestrict _ = f := PresheafedSpace.IsOpenImmersion.isoRestrict_hom_ofRestrict f @[reassoc (attr := simp)] theorem isoRestrict_inv_ofRestrict : (isoRestrict f).inv ≫ f = Y.ofRestrict _ := PresheafedSpace.IsOpenImmersion.isoRestrict_inv_ofRestrict f /-- For an open immersion `f : X ⟶ Y` and an open set `U ⊆ X`, we have the map `X(U) ⟶ Y(U)`. -/ noncomputable def invApp (U : Opens X) : X.presheaf.obj (op U) ⟶ Y.presheaf.obj (op (opensFunctor f |>.obj U)) := PresheafedSpace.IsOpenImmersion.invApp f U @[reassoc (attr := simp)] theorem inv_naturality {U V : (Opens X)ᵒᵖ} (i : U ⟶ V) : X.presheaf.map i ≫ H.invApp _ (unop V) = H.invApp _ (unop U) ≫ Y.presheaf.map (opensFunctor f |>.op.map i) := PresheafedSpace.IsOpenImmersion.inv_naturality f i instance (U : Opens X) : IsIso (H.invApp _ U) := by delta invApp; infer_instance theorem inv_invApp (U : Opens X) : inv (H.invApp _ U) = f.c.app (op (opensFunctor f |>.obj U)) ≫ X.presheaf.map (eqToHom (by simp [Opens.map, Set.preimage_image_eq _ H.base_open.injective])) := PresheafedSpace.IsOpenImmersion.inv_invApp f U @[reassoc (attr := simp)] theorem invApp_app (U : Opens X) : H.invApp _ U ≫ f.c.app (op (opensFunctor f |>.obj U)) = X.presheaf.map (eqToHom (by simp [Opens.map, Set.preimage_image_eq _ H.base_open.injective])) := PresheafedSpace.IsOpenImmersion.invApp_app f U attribute [elementwise] invApp_app @[reassoc (attr := simp)] theorem app_invApp (U : Opens Y) : f.c.app (op U) ≫ H.invApp _ ((Opens.map f.base).obj U) = Y.presheaf.map ((homOfLE (Set.image_preimage_subset f.base U.1)).op : op U ⟶ op (opensFunctor f |>.obj ((Opens.map f.base).obj U))) := PresheafedSpace.IsOpenImmersion.app_invApp f U /-- A variant of `app_inv_app` that gives an `eqToHom` instead of `homOfLe`. -/ @[reassoc] theorem app_inv_app' (U : Opens Y) (hU : (U : Set Y) ⊆ Set.range f.base) : f.c.app (op U) ≫ invApp f ((Opens.map f.base).obj U) = Y.presheaf.map (eqToHom <| le_antisymm (Set.image_preimage_subset f.base U.1) <| (Set.image_preimage_eq_inter_range (f := f.base) (t := U.1)).symm ▸ Set.subset_inter_iff.mpr ⟨fun _ h => h, hU⟩).op := PresheafedSpace.IsOpenImmersion.app_invApp f U instance ofRestrict {X : TopCat} (Y : SheafedSpace C) {f : X ⟶ Y.carrier} (hf : IsOpenEmbedding f) : IsOpenImmersion (Y.ofRestrict hf) := PresheafedSpace.IsOpenImmersion.ofRestrict _ hf @[elementwise, simp] theorem ofRestrict_invApp {C : Type*} [Category C] (X : SheafedSpace C) {Y : TopCat} {f : Y ⟶ TopCat.of X.carrier} (h : IsOpenEmbedding f) (U : Opens (X.restrict h).carrier) : (SheafedSpace.IsOpenImmersion.ofRestrict X h).invApp _ U = 𝟙 _ := PresheafedSpace.IsOpenImmersion.ofRestrict_invApp _ h U /-- An open immersion is an iso if the underlying continuous map is epi. -/ theorem to_iso [h' : Epi f.base] : IsIso f := by haveI : IsIso (forgetToPresheafedSpace.map f) := PresheafedSpace.IsOpenImmersion.to_iso f apply isIso_of_reflects_iso _ (SheafedSpace.forgetToPresheafedSpace) instance stalk_iso [HasColimits C] (x : X) : IsIso (f.stalkMap x) := PresheafedSpace.IsOpenImmersion.stalk_iso f x end section Prod -- Porting note: here `ι` should have same universe level as morphism of `C`, so needs explicit -- universe level now variable [HasLimits C] {ι : Type v} (F : Discrete ι ⥤ SheafedSpace.{_, v, v} C) [HasColimit F] (i : Discrete ι) theorem sigma_ι_isOpenEmbedding : IsOpenEmbedding (colimit.ι F i).base := by rw [← show _ = (colimit.ι F i).base from ι_preservesColimitIso_inv (SheafedSpace.forget C) F i] have : _ = _ ≫ colimit.ι (Discrete.functor ((F ⋙ SheafedSpace.forget C).obj ∘ Discrete.mk)) i := HasColimit.isoOfNatIso_ι_hom Discrete.natIsoFunctor i rw [← Iso.eq_comp_inv] at this rw [this] have : colimit.ι _ _ ≫ _ = _ := TopCat.sigmaIsoSigma_hom_ι.{v, v} ((F ⋙ SheafedSpace.forget C).obj ∘ Discrete.mk) i.as rw [← Iso.eq_comp_inv] at this cases i rw [this, ← Category.assoc] -- Porting note: `simp_rw` can't use `TopCat.isOpenEmbedding_iff_comp_isIso` and -- `TopCat.isOpenEmbedding_iff_isIso_comp`. -- See https://github.com/leanprover-community/mathlib4/issues/5026 rw [TopCat.isOpenEmbedding_iff_comp_isIso, TopCat.isOpenEmbedding_iff_comp_isIso, TopCat.isOpenEmbedding_iff_comp_isIso, TopCat.isOpenEmbedding_iff_isIso_comp] exact .sigmaMk theorem image_preimage_is_empty (j : Discrete ι) (h : i ≠ j) (U : Opens (F.obj i)) : (Opens.map (colimit.ι (F ⋙ SheafedSpace.forgetToPresheafedSpace) j).base).obj ((Opens.map (preservesColimitIso SheafedSpace.forgetToPresheafedSpace F).inv.base).obj ((sigma_ι_isOpenEmbedding F i).isOpenMap.functor.obj U)) = ⊥ := by ext x apply iff_false_intro rintro ⟨y, hy, eq⟩ replace eq := ConcreteCategory.congr_arg (preservesColimitIso (SheafedSpace.forget C) F ≪≫ HasColimit.isoOfNatIso Discrete.natIsoFunctor ≪≫ TopCat.sigmaIsoSigma.{v, v} _).hom eq simp_rw [CategoryTheory.Iso.trans_hom, ← TopCat.comp_app, ← PresheafedSpace.comp_base] at eq rw [ι_preservesColimitIso_inv] at eq change ((SheafedSpace.forget C).map (colimit.ι F i) ≫ _) y = ((SheafedSpace.forget C).map (colimit.ι F j) ≫ _) x at eq cases i; cases j rw [ι_preservesColimitIso_hom_assoc, ι_preservesColimitIso_hom_assoc, HasColimit.isoOfNatIso_ι_hom_assoc, HasColimit.isoOfNatIso_ι_hom_assoc, TopCat.sigmaIsoSigma_hom_ι, TopCat.sigmaIsoSigma_hom_ι] at eq exact h (congr_arg Discrete.mk (congr_arg Sigma.fst eq)) instance sigma_ι_isOpenImmersion_aux [HasStrictTerminalObjects C] : SheafedSpace.IsOpenImmersion (colimit.ι F i) where base_open := sigma_ι_isOpenEmbedding F i c_iso U := by have e : colimit.ι F i = _ := (ι_preservesColimitIso_inv SheafedSpace.forgetToPresheafedSpace F i).symm have H : IsOpenEmbedding (colimit.ι (F ⋙ SheafedSpace.forgetToPresheafedSpace) i ≫ (preservesColimitIso SheafedSpace.forgetToPresheafedSpace F).inv).base := e ▸ sigma_ι_isOpenEmbedding F i suffices IsIso <| (colimit.ι (F ⋙ SheafedSpace.forgetToPresheafedSpace) i ≫ (preservesColimitIso SheafedSpace.forgetToPresheafedSpace F).inv).c.app <| op (H.isOpenMap.functor.obj U) by -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11083): just `convert` is very slow, so helps it a bit convert this using 2 <;> congr rw [PresheafedSpace.comp_c_app, ← PresheafedSpace.colimitPresheafObjIsoComponentwiseLimit_hom_π] -- Porting note: this instance created manually to make the `inferInstance` below work have inst1 : IsIso (preservesColimitIso forgetToPresheafedSpace F).inv.c := PresheafedSpace.c_isIso_of_iso _ rsuffices : IsIso (limit.π (PresheafedSpace.componentwiseDiagram (F ⋙ SheafedSpace.forgetToPresheafedSpace) ((Opens.map (preservesColimitIso SheafedSpace.forgetToPresheafedSpace F).inv.base).obj (unop <| op <| H.isOpenMap.functor.obj U))) (op i)) · infer_instance apply limit_π_isIso_of_is_strict_terminal intro j hj induction j with | op j => ?_ dsimp convert (F.obj j).sheaf.isTerminalOfEmpty using 3 convert image_preimage_is_empty F i j (fun h => hj (congr_arg op h.symm)) U using 6 exact (congr_arg PresheafedSpace.Hom.base e).symm instance sigma_ι_isOpenImmersion {ι : Type w} [Small.{v} ι] (F : Discrete ι ⥤ SheafedSpace.{_, v, v} C) [HasColimit F] (i : Discrete ι) [HasStrictTerminalObjects C] : SheafedSpace.IsOpenImmersion (colimit.ι F i) := by obtain ⟨ι', ⟨e⟩⟩ := Small.equiv_small (α := ι)
let f : Discrete ι' ≌ Discrete ι := Discrete.equivalence e.symm have : colimit.ι F i = (colimit.ι F i ≫ (HasColimit.isoOfEquivalence f (Iso.refl _)).inv) ≫ (HasColimit.isoOfEquivalence f (Iso.refl _)).hom := by simp rw [this, HasColimit.isoOfEquivalence_inv_π] infer_instance end Prod end SheafedSpace.IsOpenImmersion namespace LocallyRingedSpace.IsOpenImmersion instance (X : LocallyRingedSpace) {U : TopCat} (f : U ⟶ X.toTopCat) (hf : IsOpenEmbedding f) : LocallyRingedSpace.IsOpenImmersion (X.ofRestrict hf) := PresheafedSpace.IsOpenImmersion.ofRestrict X.toPresheafedSpace hf
Mathlib/Geometry/RingedSpace/OpenImmersion.lean
922
938
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.Algebra.Operations import Mathlib.Algebra.Module.BigOperators import Mathlib.Data.Fintype.Lattice import Mathlib.RingTheory.Coprime.Lemmas import Mathlib.RingTheory.Ideal.Basic import Mathlib.RingTheory.NonUnitalSubsemiring.Basic /-! # More operations on modules and ideals -/ assert_not_exists Basis -- See `RingTheory.Ideal.Basis` Submodule.hasQuotient -- See `RingTheory.Ideal.Quotient.Operations` universe u v w x open Pointwise namespace Submodule lemma coe_span_smul {R' M' : Type*} [CommSemiring R'] [AddCommMonoid M'] [Module R' M'] (s : Set R') (N : Submodule R' M') : (Ideal.span s : Set R') • N = s • N := set_smul_eq_of_le _ _ _ (by rintro r n hr hn induction hr using Submodule.span_induction with | mem _ h => exact mem_set_smul_of_mem_mem h hn | zero => rw [zero_smul]; exact Submodule.zero_mem _ | add _ _ _ _ ihr ihs => rw [add_smul]; exact Submodule.add_mem _ ihr ihs | smul _ _ hr => rw [mem_span_set] at hr obtain ⟨c, hc, rfl⟩ := hr rw [Finsupp.sum, Finset.smul_sum, Finset.sum_smul] refine Submodule.sum_mem _ fun i hi => ?_ rw [← mul_smul, smul_eq_mul, mul_comm, mul_smul] exact mem_set_smul_of_mem_mem (hc hi) <| Submodule.smul_mem _ _ hn) <| set_smul_mono_left _ Submodule.subset_span lemma span_singleton_toAddSubgroup_eq_zmultiples (a : ℤ) : (span ℤ {a}).toAddSubgroup = AddSubgroup.zmultiples a := by ext i simp [Ideal.mem_span_singleton', AddSubgroup.mem_zmultiples_iff] @[simp] lemma _root_.Ideal.span_singleton_toAddSubgroup_eq_zmultiples (a : ℤ) : (Ideal.span {a}).toAddSubgroup = AddSubgroup.zmultiples a := Submodule.span_singleton_toAddSubgroup_eq_zmultiples _ variable {R : Type u} {M : Type v} {M' F G : Type*} section Semiring variable [Semiring R] [AddCommMonoid M] [Module R M] /-- This duplicates the global `smul_eq_mul`, but doesn't have to unfold anywhere near as much to apply. -/ protected theorem _root_.Ideal.smul_eq_mul (I J : Ideal R) : I • J = I * J := rfl variable {I J : Ideal R} {N : Submodule R M} theorem smul_le_right : I • N ≤ N := smul_le.2 fun r _ _ ↦ N.smul_mem r theorem map_le_smul_top (I : Ideal R) (f : R →ₗ[R] M) : Submodule.map f I ≤ I • (⊤ : Submodule R M) := by rintro _ ⟨y, hy, rfl⟩ rw [← mul_one y, ← smul_eq_mul, f.map_smul] exact smul_mem_smul hy mem_top variable (I J N) @[simp] theorem top_smul : (⊤ : Ideal R) • N = N := le_antisymm smul_le_right fun r hri => one_smul R r ▸ smul_mem_smul mem_top hri protected theorem mul_smul : (I * J) • N = I • J • N := Submodule.smul_assoc _ _ _ theorem mem_of_span_top_of_smul_mem (M' : Submodule R M) (s : Set R) (hs : Ideal.span s = ⊤) (x : M) (H : ∀ r : s, (r : R) • x ∈ M') : x ∈ M' := by suffices LinearMap.range (LinearMap.toSpanSingleton R M x) ≤ M' by rw [← LinearMap.toSpanSingleton_one R M x] exact this (LinearMap.mem_range_self _ 1) rw [LinearMap.range_eq_map, ← hs, map_le_iff_le_comap, Ideal.span, span_le] exact fun r hr ↦ H ⟨r, hr⟩ variable {M' : Type w} [AddCommMonoid M'] [Module R M'] @[simp] theorem map_smul'' (f : M →ₗ[R] M') : (I • N).map f = I • N.map f := le_antisymm (map_le_iff_le_comap.2 <| smul_le.2 fun r hr n hn => show f (r • n) ∈ I • N.map f from (f.map_smul r n).symm ▸ smul_mem_smul hr (mem_map_of_mem hn)) <| smul_le.2 fun r hr _ hn => let ⟨p, hp, hfp⟩ := mem_map.1 hn hfp ▸ f.map_smul r p ▸ mem_map_of_mem (smul_mem_smul hr hp) theorem mem_smul_top_iff (N : Submodule R M) (x : N) : x ∈ I • (⊤ : Submodule R N) ↔ (x : M) ∈ I • N := by have : Submodule.map N.subtype (I • ⊤) = I • N := by rw [Submodule.map_smul'', Submodule.map_top, Submodule.range_subtype] simp [← this, -map_smul''] @[simp] theorem smul_comap_le_comap_smul (f : M →ₗ[R] M') (S : Submodule R M') (I : Ideal R) : I • S.comap f ≤ (I • S).comap f := by refine Submodule.smul_le.mpr fun r hr x hx => ?_ rw [Submodule.mem_comap] at hx ⊢ rw [f.map_smul] exact Submodule.smul_mem_smul hr hx end Semiring section CommSemiring variable [CommSemiring R] [AddCommMonoid M] [Module R M] [AddCommMonoid M'] [Module R M'] open Pointwise theorem mem_smul_span_singleton {I : Ideal R} {m : M} {x : M} : x ∈ I • span R ({m} : Set M) ↔ ∃ y ∈ I, y • m = x := ⟨fun hx => smul_induction_on hx (fun r hri _ hnm => let ⟨s, hs⟩ := mem_span_singleton.1 hnm ⟨r * s, I.mul_mem_right _ hri, hs ▸ mul_smul r s m⟩) fun m1 m2 ⟨y1, hyi1, hy1⟩ ⟨y2, hyi2, hy2⟩ => ⟨y1 + y2, I.add_mem hyi1 hyi2, by rw [add_smul, hy1, hy2]⟩, fun ⟨_, hyi, hy⟩ => hy ▸ smul_mem_smul hyi (subset_span <| Set.mem_singleton m)⟩ variable {I J : Ideal R} {N P : Submodule R M} variable (S : Set R) (T : Set M) theorem smul_eq_map₂ : I • N = Submodule.map₂ (LinearMap.lsmul R M) I N := le_antisymm (smul_le.mpr fun _m hm _n ↦ Submodule.apply_mem_map₂ _ hm) (map₂_le.mpr fun _m hm _n ↦ smul_mem_smul hm) theorem span_smul_span : Ideal.span S • span R T = span R (⋃ (s ∈ S) (t ∈ T), {s • t}) := by rw [smul_eq_map₂] exact (map₂_span_span _ _ _ _).trans <| congr_arg _ <| Set.image2_eq_iUnion _ _ _ theorem ideal_span_singleton_smul (r : R) (N : Submodule R M) : (Ideal.span {r} : Ideal R) • N = r • N := by have : span R (⋃ (t : M) (_ : t ∈ N), {r • t}) = r • N := by convert span_eq (r • N) exact (Set.image_eq_iUnion _ (N : Set M)).symm conv_lhs => rw [← span_eq N, span_smul_span] simpa /-- Given `s`, a generating set of `R`, to check that an `x : M` falls in a submodule `M'` of `x`, we only need to show that `r ^ n • x ∈ M'` for some `n` for each `r : s`. -/ theorem mem_of_span_eq_top_of_smul_pow_mem (M' : Submodule R M) (s : Set R) (hs : Ideal.span s = ⊤) (x : M) (H : ∀ r : s, ∃ n : ℕ, ((r : R) ^ n : R) • x ∈ M') : x ∈ M' := by choose f hf using H apply M'.mem_of_span_top_of_smul_mem _ (Ideal.span_range_pow_eq_top s hs f) rintro ⟨_, r, hr, rfl⟩ exact hf r open Pointwise in @[simp] theorem map_pointwise_smul (r : R) (N : Submodule R M) (f : M →ₗ[R] M') : (r • N).map f = r • N.map f := by simp_rw [← ideal_span_singleton_smul, map_smul''] theorem mem_smul_span {s : Set M} {x : M} : x ∈ I • Submodule.span R s ↔ x ∈ Submodule.span R (⋃ (a ∈ I) (b ∈ s), ({a • b} : Set M)) := by rw [← I.span_eq, Submodule.span_smul_span, I.span_eq] simp variable (I) /-- If `x` is an `I`-multiple of the submodule spanned by `f '' s`, then we can write `x` as an `I`-linear combination of the elements of `f '' s`. -/ theorem mem_ideal_smul_span_iff_exists_sum {ι : Type*} (f : ι → M) (x : M) : x ∈ I • span R (Set.range f) ↔ ∃ (a : ι →₀ R) (_ : ∀ i, a i ∈ I), (a.sum fun i c => c • f i) = x := by constructor; swap · rintro ⟨a, ha, rfl⟩ exact Submodule.sum_mem _ fun c _ => smul_mem_smul (ha c) <| subset_span <| Set.mem_range_self _ refine fun hx => span_induction ?_ ?_ ?_ ?_ (mem_smul_span.mp hx) · simp only [Set.mem_iUnion, Set.mem_range, Set.mem_singleton_iff] rintro x ⟨y, hy, x, ⟨i, rfl⟩, rfl⟩ refine ⟨Finsupp.single i y, fun j => ?_, ?_⟩ · letI := Classical.decEq ι rw [Finsupp.single_apply] split_ifs · assumption · exact I.zero_mem refine @Finsupp.sum_single_index ι R M _ _ i _ (fun i y => y • f i) ?_ simp · exact ⟨0, fun _ => I.zero_mem, Finsupp.sum_zero_index⟩ · rintro x y - - ⟨ax, hax, rfl⟩ ⟨ay, hay, rfl⟩ refine ⟨ax + ay, fun i => I.add_mem (hax i) (hay i), Finsupp.sum_add_index' ?_ ?_⟩ <;> intros <;> simp only [zero_smul, add_smul] · rintro c x - ⟨a, ha, rfl⟩ refine ⟨c • a, fun i => I.mul_mem_left c (ha i), ?_⟩ rw [Finsupp.sum_smul_index, Finsupp.smul_sum] <;> intros <;> simp only [zero_smul, mul_smul] theorem mem_ideal_smul_span_iff_exists_sum' {ι : Type*} (s : Set ι) (f : ι → M) (x : M) : x ∈ I • span R (f '' s) ↔ ∃ (a : s →₀ R) (_ : ∀ i, a i ∈ I), (a.sum fun i c => c • f i) = x := by rw [← Submodule.mem_ideal_smul_span_iff_exists_sum, ← Set.image_eq_range] end CommSemiring end Submodule namespace Ideal section Add variable {R : Type u} [Semiring R] @[simp] theorem add_eq_sup {I J : Ideal R} : I + J = I ⊔ J := rfl @[simp] theorem zero_eq_bot : (0 : Ideal R) = ⊥ := rfl @[simp] theorem sum_eq_sup {ι : Type*} (s : Finset ι) (f : ι → Ideal R) : s.sum f = s.sup f := rfl end Add section Semiring variable {R : Type u} [Semiring R] {I J K L : Ideal R} @[simp] theorem one_eq_top : (1 : Ideal R) = ⊤ := by rw [Submodule.one_eq_span, ← Ideal.span, Ideal.span_singleton_one] theorem add_eq_one_iff : I + J = 1 ↔ ∃ i ∈ I, ∃ j ∈ J, i + j = 1 := by rw [one_eq_top, eq_top_iff_one, add_eq_sup, Submodule.mem_sup] theorem mul_mem_mul {r s} (hr : r ∈ I) (hs : s ∈ J) : r * s ∈ I * J := Submodule.smul_mem_smul hr hs theorem pow_mem_pow {x : R} (hx : x ∈ I) (n : ℕ) : x ^ n ∈ I ^ n := Submodule.pow_mem_pow _ hx _ theorem mul_le : I * J ≤ K ↔ ∀ r ∈ I, ∀ s ∈ J, r * s ∈ K := Submodule.smul_le theorem mul_le_left : I * J ≤ J := mul_le.2 fun _ _ _ => J.mul_mem_left _ @[simp] theorem sup_mul_left_self : I ⊔ J * I = I := sup_eq_left.2 mul_le_left @[simp] theorem mul_left_self_sup : J * I ⊔ I = I := sup_eq_right.2 mul_le_left theorem mul_le_right [I.IsTwoSided] : I * J ≤ I := mul_le.2 fun _ hr _ _ ↦ I.mul_mem_right _ hr @[simp] theorem sup_mul_right_self [I.IsTwoSided] : I ⊔ I * J = I := sup_eq_left.2 mul_le_right @[simp] theorem mul_right_self_sup [I.IsTwoSided] : I * J ⊔ I = I := sup_eq_right.2 mul_le_right protected theorem mul_assoc : I * J * K = I * (J * K) := Submodule.smul_assoc I J K variable (I) theorem mul_bot : I * ⊥ = ⊥ := by simp theorem bot_mul : ⊥ * I = ⊥ := by simp @[simp] theorem top_mul : ⊤ * I = I := Submodule.top_smul I variable {I} theorem mul_mono (hik : I ≤ K) (hjl : J ≤ L) : I * J ≤ K * L := Submodule.smul_mono hik hjl theorem mul_mono_left (h : I ≤ J) : I * K ≤ J * K := Submodule.smul_mono_left h theorem mul_mono_right (h : J ≤ K) : I * J ≤ I * K := smul_mono_right I h variable (I J K) theorem mul_sup : I * (J ⊔ K) = I * J ⊔ I * K := Submodule.smul_sup I J K theorem sup_mul : (I ⊔ J) * K = I * K ⊔ J * K := Submodule.sup_smul I J K variable {I J K} theorem pow_le_pow_right {m n : ℕ} (h : m ≤ n) : I ^ n ≤ I ^ m := by obtain _ | m := m · rw [Submodule.pow_zero, one_eq_top]; exact le_top obtain ⟨n, rfl⟩ := Nat.exists_eq_add_of_le h rw [add_comm, Submodule.pow_add _ m.add_one_ne_zero] exact mul_le_left theorem pow_le_self {n : ℕ} (hn : n ≠ 0) : I ^ n ≤ I := calc I ^ n ≤ I ^ 1 := pow_le_pow_right (Nat.pos_of_ne_zero hn) _ = I := Submodule.pow_one _ theorem pow_right_mono (e : I ≤ J) (n : ℕ) : I ^ n ≤ J ^ n := by
induction' n with _ hn · rw [Submodule.pow_zero, Submodule.pow_zero] · rw [Submodule.pow_succ, Submodule.pow_succ]
Mathlib/RingTheory/Ideal/Operations.lean
324
326
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Moritz Doll -/ import Mathlib.LinearAlgebra.Prod /-! # Partially defined linear maps A `LinearPMap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. We define a `SemilatticeInf` with `OrderBot` instance on this, and define three operations: * `mkSpanSingleton` defines a partial linear map defined on the span of a singleton. * `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that extends both `f` and `g`. * `sSup` takes a `DirectedOn (· ≤ ·)` set of partial linear maps, and returns the unique partial linear map on the `sSup` of their domains that extends all these maps. Moreover, we define * `LinearPMap.graph` is the graph of the partial linear map viewed as a submodule of `E × F`. Partially defined maps are currently used in `Mathlib` to prove Hahn-Banach theorem and its variations. Namely, `LinearPMap.sSup` implies that every chain of `LinearPMap`s is bounded above. They are also the basis for the theory of unbounded operators. -/ universe u v w /-- A `LinearPMap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. -/ structure LinearPMap (R : Type u) [Ring R] (E : Type v) [AddCommGroup E] [Module R E] (F : Type w) [AddCommGroup F] [Module R F] where domain : Submodule R E toFun : domain →ₗ[R] F @[inherit_doc] notation:25 E " →ₗ.[" R:25 "] " F:0 => LinearPMap R E F variable {R : Type*} [Ring R] {E : Type*} [AddCommGroup E] [Module R E] {F : Type*} [AddCommGroup F] [Module R F] {G : Type*} [AddCommGroup G] [Module R G] namespace LinearPMap open Submodule @[coe] def toFun' (f : E →ₗ.[R] F) : f.domain → F := f.toFun instance : CoeFun (E →ₗ.[R] F) fun f : E →ₗ.[R] F => f.domain → F := ⟨toFun'⟩ @[simp] theorem toFun_eq_coe (f : E →ₗ.[R] F) (x : f.domain) : f.toFun x = f x := rfl @[ext (iff := false)] theorem ext {f g : E →ₗ.[R] F} (h : f.domain = g.domain) (h' : ∀ ⦃x : E⦄ ⦃hf : x ∈ f.domain⦄ ⦃hg : x ∈ g.domain⦄, f ⟨x, hf⟩ = g ⟨x, hg⟩) : f = g := by rcases f with ⟨f_dom, f⟩ rcases g with ⟨g_dom, g⟩ obtain rfl : f_dom = g_dom := h congr
apply LinearMap.ext intro x apply h' /-- A dependent version of `ext`. -/ theorem dExt {f g : E →ₗ.[R] F} (h : f.domain = g.domain) (h' : ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (_h : (x : E) = y), f x = g y) : f = g :=
Mathlib/LinearAlgebra/LinearPMap.lean
65
71
/- Copyright (c) 2023 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import Mathlib.Computability.AkraBazzi.GrowsPolynomially import Mathlib.Analysis.Calculus.Deriv.Inv import Mathlib.Analysis.SpecialFunctions.Pow.Deriv /-! # Divide-and-conquer recurrences and the Akra-Bazzi theorem A divide-and-conquer recurrence is a function `T : ℕ → ℝ` that satisfies a recurrence relation of the form `T(n) = ∑_{i=0}^{k-1} a_i T(r_i(n)) + g(n)` for large enough `n`, where `r_i(n)` is some function where `‖r_i(n) - b_i n‖ ∈ o(n / (log n)^2)` for every `i`, the `a_i`'s are some positive coefficients, and the `b_i`'s are reals `∈ (0,1)`. (Note that this can be improved to `O(n / (log n)^(1+ε))`, this is left as future work.) These recurrences arise mainly in the analysis of divide-and-conquer algorithms such as mergesort or Strassen's algorithm for matrix multiplication. This class of algorithms works by dividing an instance of the problem of size `n`, into `k` smaller instances, where the `i`'th instance is of size roughly `b_i n`, and calling itself recursively on those smaller instances. `T(n)` then represents the running time of the algorithm, and `g(n)` represents the running time required to actually divide up the instance and process the answers that come out of the recursive calls. Since virtually all such algorithms produce instances that are only approximately of size `b_i n` (they have to round up or down at the very least), we allow the instance sizes to be given by some function `r_i(n)` that approximates `b_i n`. The Akra-Bazzi theorem gives the asymptotic order of such a recurrence: it states that `T(n) ∈ Θ(n^p (1 + ∑_{u=0}^{n-1} g(n) / u^{p+1}))`, where `p` is the unique real number such that `∑ a_i b_i^p = 1`. ## Main definitions and results * `AkraBazziRecurrence T g a b r`: the predicate stating that `T : ℕ → ℝ` satisfies an Akra-Bazzi recurrence with parameters `g`, `a`, `b` and `r` as above. * `GrowsPolynomially`: The growth condition that `g` must satisfy for the theorem to apply. It roughly states that `c₁ g(n) ≤ g(u) ≤ c₂ g(n)`, for u between b*n and n for any constant `b ∈ (0,1)`. * `sumTransform`: The transformation which turns a function `g` into `n^p * ∑ u ∈ Finset.Ico n₀ n, g u / u^(p+1)`. * `asympBound`: The asymptotic bound satisfied by an Akra-Bazzi recurrence, namely `n^p (1 + ∑ g(u) / u^(p+1))` * `isTheta_asympBound`: The main result stating that `T(n) ∈ Θ(n^p (1 + ∑_{u=0}^{n-1} g(n) / u^{p+1}))` ## Implementation Note that the original version of the theorem has an integral rather than a sum in the above expression, and first considers the `T : ℝ → ℝ` case before moving on to `ℕ → ℝ`. We prove the above version with a sum, as it is simpler and more relevant for algorithms. ## TODO * Specialize this theorem to the very common case where the recurrence is of the form `T(n) = ℓT(r_i(n)) + g(n)` where `g(n) ∈ Θ(n^t)` for some `t`. (This is often called the "master theorem" in the literature.) * Add the original version of the theorem with an integral instead of a sum. ## References * Mohamad Akra and Louay Bazzi, On the solution of linear recurrence equations * Tom Leighton, Notes on better master theorems for divide-and-conquer recurrences * Manuel Eberl, Asymptotic reasoning in a proof assistant -/ open Finset Real Filter Asymptotics open scoped Topology /-! #### Definition of Akra-Bazzi recurrences This section defines the predicate `AkraBazziRecurrence T g a b r` which states that `T` satisfies the recurrence `T(n) = ∑_{i=0}^{k-1} a_i T(r_i(n)) + g(n)` with appropriate conditions on the various parameters. -/ /-- An Akra-Bazzi recurrence is a function that satisfies the recurrence `T n = (∑ i, a i * T (r i n)) + g n`. -/ structure AkraBazziRecurrence {α : Type*} [Fintype α] [Nonempty α] (T : ℕ → ℝ) (g : ℝ → ℝ) (a : α → ℝ) (b : α → ℝ) (r : α → ℕ → ℕ) where /-- Point below which the recurrence is in the base case -/ n₀ : ℕ /-- `n₀` is always `> 0` -/ n₀_gt_zero : 0 < n₀ /-- The `a`'s are nonzero -/ a_pos : ∀ i, 0 < a i /-- The `b`'s are nonzero -/ b_pos : ∀ i, 0 < b i /-- The b's are less than 1 -/ b_lt_one : ∀ i, b i < 1 /-- `g` is nonnegative -/ g_nonneg : ∀ x ≥ 0, 0 ≤ g x /-- `g` grows polynomially -/ g_grows_poly : AkraBazziRecurrence.GrowsPolynomially g /-- The actual recurrence -/ h_rec (n : ℕ) (hn₀ : n₀ ≤ n) : T n = (∑ i, a i * T (r i n)) + g n /-- Base case: `T(n) > 0` whenever `n < n₀` -/ T_gt_zero' (n : ℕ) (hn : n < n₀) : 0 < T n /-- The `r`'s always reduce `n` -/ r_lt_n : ∀ i n, n₀ ≤ n → r i n < n /-- The `r`'s approximate the `b`'s -/ dist_r_b : ∀ i, (fun n => (r i n : ℝ) - b i * n) =o[atTop] fun n => n / (log n) ^ 2 namespace AkraBazziRecurrence section min_max variable {α : Type*} [Finite α] [Nonempty α] /-- Smallest `b i` -/ noncomputable def min_bi (b : α → ℝ) : α := Classical.choose <| Finite.exists_min b /-- Largest `b i` -/ noncomputable def max_bi (b : α → ℝ) : α := Classical.choose <| Finite.exists_max b @[aesop safe apply] lemma min_bi_le {b : α → ℝ} (i : α) : b (min_bi b) ≤ b i := Classical.choose_spec (Finite.exists_min b) i @[aesop safe apply] lemma max_bi_le {b : α → ℝ} (i : α) : b i ≤ b (max_bi b) := Classical.choose_spec (Finite.exists_max b) i end min_max lemma isLittleO_self_div_log_id : (fun (n : ℕ) => n / log n ^ 2) =o[atTop] (fun (n : ℕ) => (n : ℝ)) := by calc (fun (n : ℕ) => (n : ℝ) / log n ^ 2) = fun (n : ℕ) => (n : ℝ) * ((log n) ^ 2)⁻¹ := by simp_rw [div_eq_mul_inv] _ =o[atTop] fun (n : ℕ) => (n : ℝ) * 1⁻¹ := by refine IsBigO.mul_isLittleO (isBigO_refl _ _) ?_ refine IsLittleO.inv_rev ?main ?zero case zero => simp case main => calc _ = (fun (_ : ℕ) => ((1 : ℝ) ^ 2)) := by simp _ =o[atTop] (fun (n : ℕ) => (log n)^2) := IsLittleO.pow (IsLittleO.natCast_atTop <| isLittleO_const_log_atTop) (by norm_num) _ = (fun (n : ℕ) => (n : ℝ)) := by ext; simp variable {α : Type*} [Fintype α] {T : ℕ → ℝ} {g : ℝ → ℝ} {a b : α → ℝ} {r : α → ℕ → ℕ} variable [Nonempty α] (R : AkraBazziRecurrence T g a b r) section include R lemma dist_r_b' : ∀ᶠ n in atTop, ∀ i, ‖(r i n : ℝ) - b i * n‖ ≤ n / log n ^ 2 := by rw [Filter.eventually_all] intro i simpa using IsLittleO.eventuallyLE (R.dist_r_b i) lemma eventually_b_le_r : ∀ᶠ (n : ℕ) in atTop, ∀ i, (b i : ℝ) * n - (n / log n ^ 2) ≤ r i n := by filter_upwards [R.dist_r_b'] with n hn intro i have h₁ : 0 ≤ b i := le_of_lt <| R.b_pos _ rw [sub_le_iff_le_add, add_comm, ← sub_le_iff_le_add] calc (b i : ℝ) * n - r i n = ‖b i * n‖ - ‖(r i n : ℝ)‖ := by simp only [norm_mul, RCLike.norm_natCast, sub_left_inj, Nat.cast_eq_zero, Real.norm_of_nonneg h₁] _ ≤ ‖(b i * n : ℝ) - r i n‖ := norm_sub_norm_le _ _ _ = ‖(r i n : ℝ) - b i * n‖ := norm_sub_rev _ _ _ ≤ n / log n ^ 2 := hn i lemma eventually_r_le_b : ∀ᶠ (n : ℕ) in atTop, ∀ i, r i n ≤ (b i : ℝ) * n + (n / log n ^ 2) := by filter_upwards [R.dist_r_b'] with n hn intro i calc r i n = b i * n + (r i n - b i * n) := by ring _ ≤ b i * n + ‖r i n - b i * n‖ := by gcongr; exact Real.le_norm_self _ _ ≤ b i * n + n / log n ^ 2 := by gcongr; exact hn i lemma eventually_r_lt_n : ∀ᶠ (n : ℕ) in atTop, ∀ i, r i n < n := by filter_upwards [eventually_ge_atTop R.n₀] with n hn exact fun i => R.r_lt_n i n hn lemma eventually_bi_mul_le_r : ∀ᶠ (n : ℕ) in atTop, ∀ i, (b (min_bi b) / 2) * n ≤ r i n := by have gt_zero : 0 < b (min_bi b) := R.b_pos (min_bi b) have hlo := isLittleO_self_div_log_id rw [Asymptotics.isLittleO_iff] at hlo have hlo' := hlo (by positivity : 0 < b (min_bi b) / 2) filter_upwards [hlo', R.eventually_b_le_r] with n hn hn' intro i simp only [Real.norm_of_nonneg (by positivity : 0 ≤ (n : ℝ))] at hn calc b (min_bi b) / 2 * n = b (min_bi b) * n - b (min_bi b) / 2 * n := by ring _ ≤ b (min_bi b) * n - ‖n / log n ^ 2‖ := by gcongr _ ≤ b i * n - ‖n / log n ^ 2‖ := by gcongr; aesop _ = b i * n - n / log n ^ 2 := by congr exact Real.norm_of_nonneg <| by positivity _ ≤ r i n := hn' i lemma bi_min_div_two_lt_one : b (min_bi b) / 2 < 1 := by have gt_zero : 0 < b (min_bi b) := R.b_pos (min_bi b) calc b (min_bi b) / 2 < b (min_bi b) := by aesop (add safe apply div_two_lt_of_pos) _ < 1 := R.b_lt_one _ lemma bi_min_div_two_pos : 0 < b (min_bi b) / 2 := div_pos (R.b_pos _) (by norm_num) lemma exists_eventually_const_mul_le_r : ∃ c ∈ Set.Ioo (0 : ℝ) 1, ∀ᶠ (n : ℕ) in atTop, ∀ i, c * n ≤ r i n := by have gt_zero : 0 < b (min_bi b) := R.b_pos (min_bi b) exact ⟨b (min_bi b) / 2, ⟨⟨by positivity, R.bi_min_div_two_lt_one⟩, R.eventually_bi_mul_le_r⟩⟩ lemma eventually_r_ge (C : ℝ) : ∀ᶠ (n : ℕ) in atTop, ∀ i, C ≤ r i n := by obtain ⟨c, hc_mem, hc⟩ := R.exists_eventually_const_mul_le_r filter_upwards [eventually_ge_atTop ⌈C / c⌉₊, hc] with n hn₁ hn₂ have h₁ := hc_mem.1 intro i calc C = c * (C / c) := by rw [← mul_div_assoc] exact (mul_div_cancel_left₀ _ (by positivity)).symm _ ≤ c * ⌈C / c⌉₊ := by gcongr; simp [Nat.le_ceil] _ ≤ c * n := by gcongr _ ≤ r i n := hn₂ i lemma tendsto_atTop_r (i : α) : Tendsto (r i) atTop atTop := by rw [tendsto_atTop] intro b have := R.eventually_r_ge b rw [Filter.eventually_all] at this exact_mod_cast this i lemma tendsto_atTop_r_real (i : α) : Tendsto (fun n => (r i n : ℝ)) atTop atTop := Tendsto.comp tendsto_natCast_atTop_atTop (R.tendsto_atTop_r i) lemma exists_eventually_r_le_const_mul : ∃ c ∈ Set.Ioo (0 : ℝ) 1, ∀ᶠ (n : ℕ) in atTop, ∀ i, r i n ≤ c * n := by let c := b (max_bi b) + (1 - b (max_bi b)) / 2 have h_max_bi_pos : 0 < b (max_bi b) := R.b_pos _ have h_max_bi_lt_one : 0 < 1 - b (max_bi b) := by have : b (max_bi b) < 1 := R.b_lt_one _ linarith have hc_pos : 0 < c := by positivity have h₁ : 0 < (1 - b (max_bi b)) / 2 := by positivity have hc_lt_one : c < 1 := calc b (max_bi b) + (1 - b (max_bi b)) / 2 = b (max_bi b) * (1 / 2) + 1 / 2 := by ring _ < 1 * (1 / 2) + 1 / 2 := by gcongr exact R.b_lt_one _ _ = 1 := by norm_num refine ⟨c, ⟨hc_pos, hc_lt_one⟩, ?_⟩ have hlo := isLittleO_self_div_log_id rw [Asymptotics.isLittleO_iff] at hlo have hlo' := hlo h₁ filter_upwards [hlo', R.eventually_r_le_b] with n hn hn' intro i rw [Real.norm_of_nonneg (by positivity)] at hn simp only [Real.norm_of_nonneg (by positivity : 0 ≤ (n : ℝ))] at hn calc r i n ≤ b i * n + n / log n ^ 2 := by exact hn' i _ ≤ b i * n + (1 - b (max_bi b)) / 2 * n := by gcongr _ = (b i + (1 - b (max_bi b)) / 2) * n := by ring _ ≤ (b (max_bi b) + (1 - b (max_bi b)) / 2) * n := by gcongr; exact max_bi_le _ lemma eventually_r_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < r i n := by rw [Filter.eventually_all] exact fun i => (R.tendsto_atTop_r i).eventually_gt_atTop 0 lemma eventually_log_b_mul_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < log (b i * n) := by rw [Filter.eventually_all] intro i have h : Tendsto (fun (n : ℕ) => log (b i * n)) atTop atTop := Tendsto.comp tendsto_log_atTop <| Tendsto.const_mul_atTop (b_pos R i) tendsto_natCast_atTop_atTop exact h.eventually_gt_atTop 0 @[aesop safe apply] lemma T_pos (n : ℕ) : 0 < T n := by induction n using Nat.strongRecOn with | ind n h_ind => cases lt_or_le n R.n₀ with | inl hn => exact R.T_gt_zero' n hn -- n < R.n₀ | inr hn => -- R.n₀ ≤ n rw [R.h_rec n hn] have := R.g_nonneg refine add_pos_of_pos_of_nonneg (Finset.sum_pos ?sum_elems univ_nonempty) (by aesop) exact fun i _ => mul_pos (R.a_pos i) <| h_ind _ (R.r_lt_n i _ hn) @[aesop safe apply] lemma T_nonneg (n : ℕ) : 0 ≤ T n := le_of_lt <| R.T_pos n end /-! #### Smoothing function We define `ε` as the "smoothing function" `fun n => 1 / log n`, which will be used in the form of a factor of `1 ± ε n` needed to make the induction step go through. This is its own definition to make it easier to switch to a different smoothing function. For example, choosing `1 / log n ^ δ` for a suitable choice of `δ` leads to a slightly tighter theorem at the price of a more complicated proof. This part of the file then proves several properties of this function that will be needed later in the proof. -/ /-- The "smoothing function" is defined as `1 / log n`. This is defined as an `ℝ → ℝ` function as opposed to `ℕ → ℝ` since this is more convenient for the proof, where we need to e.g. take derivatives. -/ noncomputable def smoothingFn (n : ℝ) : ℝ := 1 / log n local notation "ε" => smoothingFn lemma one_add_smoothingFn_le_two {x : ℝ} (hx : exp 1 ≤ x) : 1 + ε x ≤ 2 := by simp only [smoothingFn, ← one_add_one_eq_two] gcongr have : 1 < x := by calc 1 = exp 0 := by simp _ < exp 1 := by simp _ ≤ x := hx rw [div_le_one (log_pos this)] calc 1 = log (exp 1) := by simp _ ≤ log x := log_le_log (exp_pos _) hx lemma isLittleO_smoothingFn_one : ε =o[atTop] (fun _ => (1 : ℝ)) := by unfold smoothingFn refine isLittleO_of_tendsto (fun _ h => False.elim <| one_ne_zero h) ?_ simp only [one_div, div_one] exact Tendsto.inv_tendsto_atTop Real.tendsto_log_atTop lemma isEquivalent_one_add_smoothingFn_one : (fun x => 1 + ε x) ~[atTop] (fun _ => (1 : ℝ)) := IsEquivalent.add_isLittleO IsEquivalent.refl isLittleO_smoothingFn_one lemma isEquivalent_one_sub_smoothingFn_one : (fun x => 1 - ε x) ~[atTop] (fun _ => (1 : ℝ)) := IsEquivalent.sub_isLittleO IsEquivalent.refl isLittleO_smoothingFn_one lemma growsPolynomially_one_sub_smoothingFn : GrowsPolynomially fun x => 1 - ε x := GrowsPolynomially.of_isEquivalent_const isEquivalent_one_sub_smoothingFn_one lemma growsPolynomially_one_add_smoothingFn : GrowsPolynomially fun x => 1 + ε x := GrowsPolynomially.of_isEquivalent_const isEquivalent_one_add_smoothingFn_one lemma eventually_one_sub_smoothingFn_gt_const_real (c : ℝ) (hc : c < 1) : ∀ᶠ (x : ℝ) in atTop, c < 1 - ε x := by have h₁ : Tendsto (fun x => 1 - ε x) atTop (𝓝 1) := by rw [← isEquivalent_const_iff_tendsto one_ne_zero] exact isEquivalent_one_sub_smoothingFn_one rw [tendsto_order] at h₁ exact h₁.1 c hc lemma eventually_one_sub_smoothingFn_gt_const (c : ℝ) (hc : c < 1) : ∀ᶠ (n : ℕ) in atTop, c < 1 - ε n := Eventually.natCast_atTop (p := fun n => c < 1 - ε n) <| eventually_one_sub_smoothingFn_gt_const_real c hc lemma eventually_one_sub_smoothingFn_pos_real : ∀ᶠ (x : ℝ) in atTop, 0 < 1 - ε x := eventually_one_sub_smoothingFn_gt_const_real 0 zero_lt_one lemma eventually_one_sub_smoothingFn_pos : ∀ᶠ (n : ℕ) in atTop, 0 < 1 - ε n := (eventually_one_sub_smoothingFn_pos_real).natCast_atTop lemma eventually_one_sub_smoothingFn_nonneg : ∀ᶠ (n : ℕ) in atTop, 0 ≤ 1 - ε n := by filter_upwards [eventually_one_sub_smoothingFn_pos] with n hn; exact le_of_lt hn include R in lemma eventually_one_sub_smoothingFn_r_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < 1 - ε (r i n) := by rw [Filter.eventually_all] exact fun i => (R.tendsto_atTop_r_real i).eventually eventually_one_sub_smoothingFn_pos_real @[aesop safe apply] lemma differentiableAt_smoothingFn {x : ℝ} (hx : 1 < x) : DifferentiableAt ℝ ε x := by have : log x ≠ 0 := Real.log_ne_zero_of_pos_of_ne_one (by positivity) (ne_of_gt hx) show DifferentiableAt ℝ (fun z => 1 / log z) x simp_rw [one_div] exact DifferentiableAt.inv (differentiableAt_log (by positivity)) this @[aesop safe apply] lemma differentiableAt_one_sub_smoothingFn {x : ℝ} (hx : 1 < x) : DifferentiableAt ℝ (fun z => 1 - ε z) x := DifferentiableAt.sub (differentiableAt_const _) <| differentiableAt_smoothingFn hx lemma differentiableOn_one_sub_smoothingFn : DifferentiableOn ℝ (fun z => 1 - ε z) (Set.Ioi 1) := fun _ hx => (differentiableAt_one_sub_smoothingFn hx).differentiableWithinAt @[aesop safe apply] lemma differentiableAt_one_add_smoothingFn {x : ℝ} (hx : 1 < x) : DifferentiableAt ℝ (fun z => 1 + ε z) x := DifferentiableAt.add (differentiableAt_const _) <| differentiableAt_smoothingFn hx lemma differentiableOn_one_add_smoothingFn : DifferentiableOn ℝ (fun z => 1 + ε z) (Set.Ioi 1) := fun _ hx => (differentiableAt_one_add_smoothingFn hx).differentiableWithinAt lemma deriv_smoothingFn {x : ℝ} (hx : 1 < x) : deriv ε x = -x⁻¹ / (log x ^ 2) := by have : log x ≠ 0 := Real.log_ne_zero_of_pos_of_ne_one (by positivity) (ne_of_gt hx) show deriv (fun z => 1 / log z) x = -x⁻¹ / (log x ^ 2) rw [deriv_div] <;> aesop lemma isLittleO_deriv_smoothingFn : deriv ε =o[atTop] fun x => x⁻¹ := calc deriv ε =ᶠ[atTop] fun x => -x⁻¹ / (log x ^ 2) := by filter_upwards [eventually_gt_atTop 1] with x hx rw [deriv_smoothingFn hx] _ = fun x => (-x * log x ^ 2)⁻¹ := by simp_rw [neg_div, div_eq_mul_inv, ← mul_inv, neg_inv, neg_mul] _ =o[atTop] fun x => (x * 1)⁻¹ := by refine IsLittleO.inv_rev ?_ ?_ · refine IsBigO.mul_isLittleO (by rw [isBigO_neg_right]; aesop (add safe isBigO_refl)) ?_ rw [isLittleO_one_left_iff] exact Tendsto.comp tendsto_norm_atTop_atTop <| Tendsto.comp (tendsto_pow_atTop (by norm_num)) tendsto_log_atTop · exact Filter.Eventually.of_forall (fun x hx => by rw [mul_one] at hx; simp [hx]) _ = fun x => x⁻¹ := by simp lemma eventually_deriv_one_sub_smoothingFn : deriv (fun x => 1 - ε x) =ᶠ[atTop] fun x => x⁻¹ / (log x ^ 2) := calc deriv (fun x => 1 - ε x) =ᶠ[atTop] -(deriv ε) := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_sub] <;> aesop _ =ᶠ[atTop] fun x => x⁻¹ / (log x ^ 2) := by filter_upwards [eventually_gt_atTop 1] with x hx simp [deriv_smoothingFn hx, neg_div] lemma eventually_deriv_one_add_smoothingFn : deriv (fun x => 1 + ε x) =ᶠ[atTop] fun x => -x⁻¹ / (log x ^ 2) := calc deriv (fun x => 1 + ε x) =ᶠ[atTop] deriv ε := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_add] <;> aesop _ =ᶠ[atTop] fun x => -x⁻¹ / (log x ^ 2) := by filter_upwards [eventually_gt_atTop 1] with x hx simp [deriv_smoothingFn hx] lemma isLittleO_deriv_one_sub_smoothingFn : deriv (fun x => 1 - ε x) =o[atTop] fun (x : ℝ) => x⁻¹ := calc deriv (fun x => 1 - ε x) =ᶠ[atTop] fun z => -(deriv ε z) := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_sub] <;> aesop _ =o[atTop] fun x => x⁻¹ := by rw [isLittleO_neg_left]; exact isLittleO_deriv_smoothingFn lemma isLittleO_deriv_one_add_smoothingFn : deriv (fun x => 1 + ε x) =o[atTop] fun (x : ℝ) => x⁻¹ := calc deriv (fun x => 1 + ε x) =ᶠ[atTop] fun z => deriv ε z := by filter_upwards [eventually_gt_atTop 1] with x hx; rw [deriv_add] <;> aesop _ =o[atTop] fun x => x⁻¹ := isLittleO_deriv_smoothingFn lemma eventually_one_add_smoothingFn_pos : ∀ᶠ (n : ℕ) in atTop, 0 < 1 + ε n := by have h₁ := isLittleO_smoothingFn_one rw [isLittleO_iff] at h₁ refine Eventually.natCast_atTop (p := fun n => 0 < 1 + ε n) ?_ filter_upwards [h₁ (by norm_num : (0 : ℝ) < 1/2), eventually_gt_atTop 1] with x _ hx' have : 0 < log x := Real.log_pos hx' show 0 < 1 + 1 / log x positivity include R in lemma eventually_one_add_smoothingFn_r_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < 1 + ε (r i n) := by rw [Filter.eventually_all] exact fun i => (R.tendsto_atTop_r i).eventually (f := r i) eventually_one_add_smoothingFn_pos lemma eventually_one_add_smoothingFn_nonneg : ∀ᶠ (n : ℕ) in atTop, 0 ≤ 1 + ε n := by filter_upwards [eventually_one_add_smoothingFn_pos] with n hn; exact le_of_lt hn lemma strictAntiOn_smoothingFn : StrictAntiOn ε (Set.Ioi 1) := by show StrictAntiOn (fun x => 1 / log x) (Set.Ioi 1) simp_rw [one_div] refine StrictAntiOn.comp_strictMonoOn inv_strictAntiOn ?log fun _ hx => log_pos hx refine StrictMonoOn.mono strictMonoOn_log (fun x hx => ?_) exact Set.Ioi_subset_Ioi zero_le_one hx lemma strictMonoOn_one_sub_smoothingFn : StrictMonoOn (fun (x : ℝ) => (1 : ℝ) - ε x) (Set.Ioi 1) := by simp_rw [sub_eq_add_neg] exact StrictMonoOn.const_add (StrictAntiOn.neg <| strictAntiOn_smoothingFn) 1 lemma strictAntiOn_one_add_smoothingFn : StrictAntiOn (fun (x : ℝ) => (1 : ℝ) + ε x) (Set.Ioi 1) := StrictAntiOn.const_add strictAntiOn_smoothingFn 1 section include R lemma isEquivalent_smoothingFn_sub_self (i : α) : (fun (n : ℕ) => ε (b i * n) - ε n) ~[atTop] fun n => -log (b i) / (log n)^2 := by calc (fun (n : ℕ) => 1 / log (b i * n) - 1 / log n) =ᶠ[atTop] fun (n : ℕ) => (log n - log (b i * n)) / (log (b i * n) * log n) := by filter_upwards [eventually_gt_atTop 1, R.eventually_log_b_mul_pos] with n hn hn' have h_log_pos : 0 < log n := Real.log_pos <| by aesop simp only [one_div] rw [inv_sub_inv (by have := hn' i; positivity) (by aesop)] _ =ᶠ[atTop] (fun (n : ℕ) ↦ (log n - log (b i) - log n) / ((log (b i) + log n) * log n)) := by filter_upwards [eventually_ne_atTop 0] with n hn have : 0 < b i := R.b_pos i rw [log_mul (by positivity) (by aesop), sub_add_eq_sub_sub] _ = (fun (n : ℕ) => -log (b i) / ((log (b i) + log n) * log n)) := by ext; congr; ring _ ~[atTop] (fun (n : ℕ) => -log (b i) / (log n * log n)) := by refine IsEquivalent.div (IsEquivalent.refl) <| IsEquivalent.mul ?_ (IsEquivalent.refl) have : (fun (n : ℕ) => log (b i) + log n) = fun (n : ℕ) => log n + log (b i) := by ext; simp [add_comm] rw [this] exact IsEquivalent.add_isLittleO IsEquivalent.refl <| IsLittleO.natCast_atTop (f := fun (_ : ℝ) => log (b i)) isLittleO_const_log_atTop _ = (fun (n : ℕ) => -log (b i) / (log n)^2) := by ext; congr 1; rw [← pow_two] lemma isTheta_smoothingFn_sub_self (i : α) : (fun (n : ℕ) => ε (b i * n) - ε n) =Θ[atTop] fun n => 1 / (log n)^2 := by calc (fun (n : ℕ) => ε (b i * n) - ε n) =Θ[atTop] fun n => (-log (b i)) / (log n)^2 := by exact (R.isEquivalent_smoothingFn_sub_self i).isTheta _ = fun (n : ℕ) => (-log (b i)) * 1 / (log n)^2 := by simp only [mul_one] _ = fun (n : ℕ) => -log (b i) * (1 / (log n)^2) := by simp_rw [← mul_div_assoc] _ =Θ[atTop] fun (n : ℕ) => 1 / (log n)^2 := by have : -log (b i) ≠ 0 := by rw [neg_ne_zero] exact Real.log_ne_zero_of_pos_of_ne_one (R.b_pos i) (ne_of_lt <| R.b_lt_one i) rw [← isTheta_const_mul_right this] /-! #### Akra-Bazzi exponent `p` Every Akra-Bazzi recurrence has an associated exponent, denoted by `p : ℝ`, such that `∑ a_i b_i^p = 1`. This section shows the existence and uniqueness of this exponent `p` for any `R : AkraBazziRecurrence`, and defines `R.asympBound` to be the asymptotic bound satisfied by `R`, namely `n^p (1 + ∑_{u < n} g(u) / u^(p+1))`. -/ @[continuity] lemma continuous_sumCoeffsExp : Continuous (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) := by refine continuous_finset_sum Finset.univ fun i _ => Continuous.mul (by fun_prop) ?_ exact Continuous.rpow continuous_const continuous_id (fun x => Or.inl (ne_of_gt (R.b_pos i))) lemma strictAnti_sumCoeffsExp : StrictAnti (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) := by rw [← Finset.sum_fn] refine Finset.sum_induction_nonempty _ _ (fun _ _ => StrictAnti.add) univ_nonempty ?terms refine fun i _ => StrictAnti.const_mul ?_ (R.a_pos i) exact Real.strictAnti_rpow_of_base_lt_one (R.b_pos i) (R.b_lt_one i) lemma tendsto_zero_sumCoeffsExp : Tendsto (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) atTop (𝓝 0) := by have h₁ : Finset.univ.sum (fun _ : α => (0 : ℝ)) = 0 := by simp rw [← h₁] refine tendsto_finset_sum (univ : Finset α) (fun i _ => ?_) rw [← mul_zero (a i)] refine Tendsto.mul (by simp) <| tendsto_rpow_atTop_of_base_lt_one _ ?_ (R.b_lt_one i) have := R.b_pos i linarith lemma tendsto_atTop_sumCoeffsExp : Tendsto (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) atBot atTop := by have h₁ : Tendsto (fun p : ℝ => (a (max_bi b) : ℝ) * b (max_bi b) ^ p) atBot atTop := Tendsto.const_mul_atTop (R.a_pos (max_bi b)) <| tendsto_rpow_atBot_of_base_lt_one _ (by have := R.b_pos (max_bi b); linarith) (R.b_lt_one _) refine tendsto_atTop_mono (fun p => ?_) h₁ refine Finset.single_le_sum (f := fun i => (a i : ℝ) * b i ^ p) (fun i _ => ?_) (mem_univ _) have h₁ : 0 < a i := R.a_pos i have h₂ : 0 < b i := R.b_pos i positivity lemma one_mem_range_sumCoeffsExp : 1 ∈ Set.range (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) := by refine mem_range_of_exists_le_of_exists_ge R.continuous_sumCoeffsExp ?le_one ?ge_one case le_one => exact R.tendsto_zero_sumCoeffsExp.eventually_le_const zero_lt_one |>.exists case ge_one => exact R.tendsto_atTop_sumCoeffsExp.eventually_ge_atTop _ |>.exists /-- The function x ↦ ∑ a_i b_i^x is injective. This implies the uniqueness of `p`. -/ lemma injective_sumCoeffsExp : Function.Injective (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) := R.strictAnti_sumCoeffsExp.injective end variable (a b) in /-- The exponent `p` associated with a particular Akra-Bazzi recurrence. -/ noncomputable irreducible_def p : ℝ := Function.invFun (fun (p : ℝ) => ∑ i, a i * (b i) ^ p) 1 include R in @[simp] lemma sumCoeffsExp_p_eq_one : ∑ i, a i * (b i) ^ p a b = 1 := by simp only [p] exact Function.invFun_eq (by rw [← Set.mem_range]; exact R.one_mem_range_sumCoeffsExp) /-! #### The sum transform This section defines the "sum transform" of a function `g` as `∑ u ∈ Finset.Ico n₀ n, g u / u^(p+1)`, and uses it to define `asympBound` as the bound satisfied by an Akra-Bazzi recurrence. Several properties of the sum transform are then proven. -/ /-- The transformation which turns a function `g` into `n^p * ∑ u ∈ Finset.Ico n₀ n, g u / u^(p+1)`. -/ noncomputable def sumTransform (p : ℝ) (g : ℝ → ℝ) (n₀ n : ℕ) := n^p * ∑ u ∈ Finset.Ico n₀ n, g u / u^(p + 1) lemma sumTransform_def {p : ℝ} {g : ℝ → ℝ} {n₀ n : ℕ} : sumTransform p g n₀ n = n^p * ∑ u ∈ Finset.Ico n₀ n, g u / u^(p + 1) := rfl variable (g) (a) (b) /-- The asymptotic bound satisfied by an Akra-Bazzi recurrence, namely `n^p (1 + ∑_{u < n} g(u) / u^(p+1))`. -/ noncomputable def asympBound (n : ℕ) : ℝ := n ^ p a b + sumTransform (p a b) g 0 n lemma asympBound_def {α} [Fintype α] (a b : α → ℝ) {n : ℕ} : asympBound g a b n = n ^ p a b + sumTransform (p a b) g 0 n := rfl variable {g} {a} {b} lemma asympBound_def' {α} [Fintype α] (a b : α → ℝ) {n : ℕ} : asympBound g a b n = n ^ p a b * (1 + (∑ u ∈ range n, g u / u ^ (p a b + 1))) := by simp [asympBound_def, sumTransform, mul_add, mul_one, Finset.sum_Ico_eq_sum_range] section include R lemma asympBound_pos (n : ℕ) (hn : 0 < n) : 0 < asympBound g a b n := by calc 0 < (n : ℝ) ^ p a b * (1 + 0) := by aesop (add safe Real.rpow_pos_of_pos) _ ≤ asympBound g a b n := by simp only [asympBound_def'] gcongr n^p a b * (1 + ?_) have := R.g_nonneg aesop (add safe Real.rpow_nonneg, safe div_nonneg, safe Finset.sum_nonneg) lemma eventually_asympBound_pos : ∀ᶠ (n : ℕ) in atTop, 0 < asympBound g a b n := by filter_upwards [eventually_gt_atTop 0] with n hn exact R.asympBound_pos n hn lemma eventually_asympBound_r_pos : ∀ᶠ (n : ℕ) in atTop, ∀ i, 0 < asympBound g a b (r i n) := by rw [Filter.eventually_all] exact fun i => (R.tendsto_atTop_r i).eventually R.eventually_asympBound_pos lemma eventually_atTop_sumTransform_le : ∃ c > 0, ∀ᶠ (n : ℕ) in atTop, ∀ i, sumTransform (p a b) g (r i n) n ≤ c * g n := by obtain ⟨c₁, hc₁_mem, hc₁⟩ := R.exists_eventually_const_mul_le_r obtain ⟨c₂, hc₂_mem, hc₂⟩ := R.g_grows_poly.eventually_atTop_le_nat hc₁_mem have hc₁_pos : 0 < c₁ := hc₁_mem.1 refine ⟨max c₂ (c₂ / c₁ ^ (p a b + 1)), by positivity, ?_⟩ filter_upwards [hc₁, hc₂, R.eventually_r_pos, R.eventually_r_lt_n, eventually_gt_atTop 0] with n hn₁ hn₂ hrpos hr_lt_n hn_pos intro i have hrpos_i := hrpos i have g_nonneg : 0 ≤ g n := R.g_nonneg n (by positivity) cases le_or_lt 0 (p a b + 1) with | inl hp => -- 0 ≤ p a b + 1 calc sumTransform (p a b) g (r i n) n = n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, g u / u ^ ((p a b) + 1)) := by rfl _ ≤ n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, c₂ * g n / u ^ ((p a b) + 1)) := by gcongr with u hu rw [Finset.mem_Ico] at hu have hu' : u ∈ Set.Icc (r i n) n := ⟨hu.1, by omega⟩ refine hn₂ u ?_ rw [Set.mem_Icc] refine ⟨?_, by norm_cast; omega⟩ calc c₁ * n ≤ r i n := by exact hn₁ i _ ≤ u := by exact_mod_cast hu'.1 _ ≤ n ^ (p a b) * (∑ _u ∈ Finset.Ico (r i n) n, c₂ * g n / (r i n) ^ ((p a b) + 1)) := by gcongr with u hu; rw [Finset.mem_Ico] at hu; exact hu.1 _ ≤ n ^ p a b * #(Ico (r i n) n) • (c₂ * g n / r i n ^ (p a b + 1)) := by gcongr; exact Finset.sum_le_card_nsmul _ _ _ (fun x _ => by rfl) _ = n ^ p a b * #(Ico (r i n) n) * (c₂ * g n / r i n ^ (p a b + 1)) := by rw [nsmul_eq_mul, mul_assoc] _ = n ^ (p a b) * (n - r i n) * (c₂ * g n / (r i n) ^ ((p a b) + 1)) := by congr; rw [Nat.card_Ico, Nat.cast_sub (le_of_lt <| hr_lt_n i)] _ ≤ n ^ (p a b) * n * (c₂ * g n / (r i n) ^ ((p a b) + 1)) := by gcongr; simp only [tsub_le_iff_right, le_add_iff_nonneg_right, Nat.cast_nonneg] _ ≤ n ^ (p a b) * n * (c₂ * g n / (c₁ * n) ^ ((p a b) + 1)) := by gcongr; exact hn₁ i _ = c₂ * g n * n ^ ((p a b) + 1) / (c₁ * n) ^ ((p a b) + 1) := by rw [← Real.rpow_add_one (by positivity) (p a b)]; ring _ = c₂ * g n * n ^ ((p a b) + 1) / (n ^ ((p a b) + 1) * c₁ ^ ((p a b) + 1)) := by rw [mul_comm c₁, Real.mul_rpow (by positivity) (by positivity)] _ = c₂ * g n * (n ^ ((p a b) + 1) / (n ^ ((p a b) + 1))) / c₁ ^ ((p a b) + 1) := by ring _ = c₂ * g n / c₁ ^ ((p a b) + 1) := by rw [div_self (by positivity), mul_one] _ = (c₂ / c₁ ^ ((p a b) + 1)) * g n := by ring _ ≤ max c₂ (c₂ / c₁ ^ ((p a b) + 1)) * g n := by gcongr; exact le_max_right _ _ | inr hp => -- p a b + 1 < 0 calc sumTransform (p a b) g (r i n) n = n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, g u / u ^ ((p a b) + 1)) := by rfl _ ≤ n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, c₂ * g n / u ^ ((p a b) + 1)) := by gcongr with u hu rw [Finset.mem_Ico] at hu have hu' : u ∈ Set.Icc (r i n) n := ⟨hu.1, by omega⟩ refine hn₂ u ?_ rw [Set.mem_Icc] refine ⟨?_, by norm_cast; omega⟩ calc c₁ * n ≤ r i n := by exact hn₁ i _ ≤ u := by exact_mod_cast hu'.1 _ ≤ n ^ (p a b) * (∑ _u ∈ Finset.Ico (r i n) n, c₂ * g n / n ^ ((p a b) + 1)) := by gcongr n ^ (p a b) * (Finset.Ico (r i n) n).sum (fun _ => c₂ * g n / ?_) with u hu rw [Finset.mem_Ico] at hu have : 0 < u := calc
0 < r i n := by exact hrpos_i _ ≤ u := by exact hu.1 exact rpow_le_rpow_of_exponent_nonpos (by positivity) (by exact_mod_cast (le_of_lt hu.2)) (le_of_lt hp) _ ≤ n ^ p a b * #(Ico (r i n) n) • (c₂ * g n / n ^ (p a b + 1)) := by gcongr; exact Finset.sum_le_card_nsmul _ _ _ (fun x _ => by rfl) _ = n ^ p a b * #(Ico (r i n) n) * (c₂ * g n / n ^ (p a b + 1)) := by rw [nsmul_eq_mul, mul_assoc] _ = n ^ (p a b) * (n - r i n) * (c₂ * g n / n ^ ((p a b) + 1)) := by congr; rw [Nat.card_Ico, Nat.cast_sub (le_of_lt <| hr_lt_n i)] _ ≤ n ^ (p a b) * n * (c₂ * g n / n ^ ((p a b) + 1)) := by gcongr; simp only [tsub_le_iff_right, le_add_iff_nonneg_right, Nat.cast_nonneg] _ = c₂ * (n^((p a b) + 1) / n ^ ((p a b) + 1)) * g n := by rw [← Real.rpow_add_one (by positivity) (p a b)]; ring _ = c₂ * g n := by rw [div_self (by positivity), mul_one] _ ≤ max c₂ (c₂ / c₁ ^ ((p a b) + 1)) * g n := by gcongr; exact le_max_left _ _ lemma eventually_atTop_sumTransform_ge : ∃ c > 0, ∀ᶠ (n : ℕ) in atTop, ∀ i, c * g n ≤ sumTransform (p a b) g (r i n) n := by obtain ⟨c₁, hc₁_mem, hc₁⟩ := R.exists_eventually_const_mul_le_r obtain ⟨c₂, hc₂_mem, hc₂⟩ := R.g_grows_poly.eventually_atTop_ge_nat hc₁_mem obtain ⟨c₃, hc₃_mem, hc₃⟩ := R.exists_eventually_r_le_const_mul have hc₁_pos : 0 < c₁ := hc₁_mem.1 have hc₃' : 0 < (1 - c₃) := by have := hc₃_mem.2; linarith refine ⟨min (c₂ * (1 - c₃)) ((1 - c₃) * c₂ / c₁^((p a b) + 1)), by positivity, ?_⟩ filter_upwards [hc₁, hc₂, hc₃, R.eventually_r_pos, R.eventually_r_lt_n, eventually_gt_atTop 0] with n hn₁ hn₂ hn₃ hrpos hr_lt_n hn_pos intro i have hrpos_i := hrpos i have g_nonneg : 0 ≤ g n := R.g_nonneg n (by positivity) cases le_or_gt 0 (p a b + 1) with | inl hp => -- 0 ≤ (p a b) + 1 calc sumTransform (p a b) g (r i n) n = n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, g u / u ^ ((p a b) + 1)) := rfl _ ≥ n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, c₂ * g n / u^((p a b) + 1)) := by gcongr with u hu rw [Finset.mem_Ico] at hu have hu' : u ∈ Set.Icc (r i n) n := ⟨hu.1, by omega⟩ refine hn₂ u ?_ rw [Set.mem_Icc] refine ⟨?_, by norm_cast; omega⟩ calc c₁ * n ≤ r i n := by exact hn₁ i _ ≤ u := by exact_mod_cast hu'.1 _ ≥ n ^ (p a b) * (∑ _u ∈ Finset.Ico (r i n) n, c₂ * g n / n ^ ((p a b) + 1)) := by gcongr with u hu · rw [Finset.mem_Ico] at hu have := calc 0 < r i n := hrpos_i _ ≤ u := hu.1 positivity · rw [Finset.mem_Ico] at hu exact le_of_lt hu.2 _ ≥ n ^ p a b * #(Ico (r i n) n) • (c₂ * g n / n ^ (p a b + 1)) := by gcongr; exact Finset.card_nsmul_le_sum _ _ _ (fun x _ => by rfl) _ = n ^ p a b * #(Ico (r i n) n) * (c₂ * g n / n ^ (p a b + 1)) := by rw [nsmul_eq_mul, mul_assoc] _ = n ^ (p a b) * (n - r i n) * (c₂ * g n / n ^ ((p a b) + 1)) := by congr; rw [Nat.card_Ico, Nat.cast_sub (le_of_lt <| hr_lt_n i)] _ ≥ n ^ (p a b) * (n - c₃ * n) * (c₂ * g n / n ^ ((p a b) + 1)) := by gcongr; exact hn₃ i _ = n ^ (p a b) * n * (1 - c₃) * (c₂ * g n / n ^ ((p a b) + 1)) := by ring _ = c₂ * (1 - c₃) * g n * (n ^ ((p a b) + 1) / n ^ ((p a b) + 1)) := by rw [← Real.rpow_add_one (by positivity) (p a b)]; ring _ = c₂ * (1 - c₃) * g n := by rw [div_self (by positivity), mul_one] _ ≥ min (c₂ * (1 - c₃)) ((1 - c₃) * c₂ / c₁ ^ ((p a b) + 1)) * g n := by gcongr; exact min_le_left _ _ | inr hp => -- (p a b) + 1 < 0 calc sumTransform (p a b) g (r i n) n = n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, g u / u^((p a b) + 1)) := by rfl _ ≥ n ^ (p a b) * (∑ u ∈ Finset.Ico (r i n) n, c₂ * g n / u ^ ((p a b) + 1)) := by gcongr with u hu rw [Finset.mem_Ico] at hu have hu' : u ∈ Set.Icc (r i n) n := ⟨hu.1, by omega⟩ refine hn₂ u ?_ rw [Set.mem_Icc] refine ⟨?_, by norm_cast; omega⟩ calc c₁ * n ≤ r i n := by exact hn₁ i _ ≤ u := by exact_mod_cast hu'.1 _ ≥ n ^ (p a b) * (∑ _u ∈ Finset.Ico (r i n) n, c₂ * g n / (r i n) ^ ((p a b) + 1)) := by gcongr n^(p a b) * (Finset.Ico (r i n) n).sum (fun _ => c₂ * g n / ?_) with u hu · rw [Finset.mem_Ico] at hu have := calc 0 < r i n := hrpos_i _ ≤ u := hu.1 positivity · rw [Finset.mem_Ico] at hu exact rpow_le_rpow_of_exponent_nonpos (by positivity) (by exact_mod_cast hu.1) (le_of_lt hp) _ ≥ n ^ p a b * #(Ico (r i n) n) • (c₂ * g n / r i n ^ (p a b + 1)) := by gcongr; exact Finset.card_nsmul_le_sum _ _ _ (fun x _ => by rfl) _ = n ^ p a b * #(Ico (r i n) n) * (c₂ * g n / r i n ^ (p a b + 1)) := by
Mathlib/Computability/AkraBazzi/AkraBazzi.lean
680
768
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Analysis.SpecialFunctions.Pow.Asymptotics import Mathlib.Analysis.Asymptotics.AsymptoticEquivalent import Mathlib.Analysis.Asymptotics.SpecificAsymptotics /-! # Growth estimates on `x ^ y` for complex `x`, `y` Let `l` be a filter on `ℂ` such that `Complex.re` tends to infinity along `l` and `Complex.im z` grows at a subexponential rate compared to `Complex.re z`. Then - `Complex.isLittleO_log_abs_re`: `Real.log ∘ Complex.abs` is `o`-small of `Complex.re` along `l`; - `Complex.isLittleO_cpow_mul_exp`: $z^{a_1}e^{b_1 * z} = o\left(z^{a_1}e^{b_1 * z}\right)$ along `l` for any complex `a₁`, `a₂` and real `b₁ < b₂`. We use these assumptions on `l` for two reasons. First, these are the assumptions that naturally appear in the proof. Second, in some applications (e.g., in Ilyashenko's proof of the individual finiteness theorem for limit cycles of polynomial ODEs with hyperbolic singularities only) natural stronger assumptions (e.g., `im z` is bounded from below and from above) are not available. -/ open Asymptotics Filter Function open scoped Topology namespace Complex /-- We say that `l : Filter ℂ` is an *exponential comparison filter* if the real part tends to infinity along `l` and the imaginary part grows subexponentially compared to the real part. These properties guarantee that `(fun z ↦ z ^ a₁ * exp (b₁ * z)) =o[l] (fun z ↦ z ^ a₂ * exp (b₂ * z))` for any complex `a₁`, `a₂` and real `b₁ < b₂`. In particular, the second property is automatically satisfied if the imaginary part is bounded along `l`. -/ structure IsExpCmpFilter (l : Filter ℂ) : Prop where tendsto_re : Tendsto re l atTop isBigO_im_pow_re : ∀ n : ℕ, (fun z : ℂ => z.im ^ n) =O[l] fun z => Real.exp z.re namespace IsExpCmpFilter variable {l : Filter ℂ} /-! ### Alternative constructors -/ theorem of_isBigO_im_re_rpow (hre : Tendsto re l atTop) (r : ℝ) (hr : im =O[l] fun z => z.re ^ r) : IsExpCmpFilter l := ⟨hre, fun n => IsLittleO.isBigO <| calc (fun z : ℂ => z.im ^ n) =O[l] fun z => (z.re ^ r) ^ n := hr.pow n _ =ᶠ[l] fun z => z.re ^ (r * n) := ((hre.eventually_ge_atTop 0).mono fun z hz => by simp only [Real.rpow_mul hz r n, Real.rpow_natCast]) _ =o[l] fun z => Real.exp z.re := (isLittleO_rpow_exp_atTop _).comp_tendsto hre ⟩ theorem of_isBigO_im_re_pow (hre : Tendsto re l atTop) (n : ℕ) (hr : im =O[l] fun z => z.re ^ n) : IsExpCmpFilter l := of_isBigO_im_re_rpow hre n <| mod_cast hr theorem of_boundedUnder_abs_im (hre : Tendsto re l atTop) (him : IsBoundedUnder (· ≤ ·) l fun z => |z.im|) : IsExpCmpFilter l := of_isBigO_im_re_pow hre 0 <| by simpa only [pow_zero] using him.isBigO_const (f := im) one_ne_zero theorem of_boundedUnder_im (hre : Tendsto re l atTop) (him_le : IsBoundedUnder (· ≤ ·) l im) (him_ge : IsBoundedUnder (· ≥ ·) l im) : IsExpCmpFilter l := of_boundedUnder_abs_im hre <| isBoundedUnder_le_abs.2 ⟨him_le, him_ge⟩ /-! ### Preliminary lemmas -/ theorem eventually_ne (hl : IsExpCmpFilter l) : ∀ᶠ w : ℂ in l, w ≠ 0 := hl.tendsto_re.eventually_ne_atTop' _ theorem tendsto_abs_re (hl : IsExpCmpFilter l) : Tendsto (fun z : ℂ => |z.re|) l atTop := tendsto_abs_atTop_atTop.comp hl.tendsto_re theorem tendsto_norm (hl : IsExpCmpFilter l) : Tendsto norm l atTop := tendsto_atTop_mono abs_re_le_norm hl.tendsto_abs_re @[deprecated (since := "2025-02-17")] alias tendsto_abs := tendsto_norm theorem isLittleO_log_re_re (hl : IsExpCmpFilter l) : (fun z => Real.log z.re) =o[l] re := Real.isLittleO_log_id_atTop.comp_tendsto hl.tendsto_re theorem isLittleO_im_pow_exp_re (hl : IsExpCmpFilter l) (n : ℕ) : (fun z : ℂ => z.im ^ n) =o[l] fun z => Real.exp z.re := flip IsLittleO.of_pow two_ne_zero <| calc (fun z : ℂ ↦ (z.im ^ n) ^ 2) = (fun z ↦ z.im ^ (2 * n)) := by simp only [pow_mul'] _ =O[l] fun z ↦ Real.exp z.re := hl.isBigO_im_pow_re _ _ = fun z ↦ (Real.exp z.re) ^ 1 := by simp only [pow_one] _ =o[l] fun z ↦ (Real.exp z.re) ^ 2 := (isLittleO_pow_pow_atTop_of_lt one_lt_two).comp_tendsto <| Real.tendsto_exp_atTop.comp hl.tendsto_re theorem abs_im_pow_eventuallyLE_exp_re (hl : IsExpCmpFilter l) (n : ℕ) : (fun z : ℂ => |z.im| ^ n) ≤ᶠ[l] fun z => Real.exp z.re := by simpa using (hl.isLittleO_im_pow_exp_re n).bound zero_lt_one /-- If `l : Filter ℂ` is an "exponential comparison filter", then $\log |z| =o(ℜ z)$ along `l`. This is the main lemma in the proof of `Complex.IsExpCmpFilter.isLittleO_cpow_exp` below. -/ theorem isLittleO_log_norm_re (hl : IsExpCmpFilter l) : (fun z => Real.log ‖z‖) =o[l] re := calc (fun z => Real.log ‖z‖) =O[l] fun z => Real.log (√2) + Real.log (max z.re |z.im|) := .of_norm_eventuallyLE <| (hl.tendsto_re.eventually_ge_atTop 1).mono fun z hz => by have h2 : 0 < √2 := by simp have hz' : 1 ≤ ‖z‖ := hz.trans (re_le_norm z) have hm₀ : 0 < max z.re |z.im| := lt_max_iff.2 (Or.inl <| one_pos.trans_le hz) simp only [Real.norm_of_nonneg (Real.log_nonneg hz')] rw [← Real.log_mul, Real.log_le_log_iff, ← abs_of_nonneg (le_trans zero_le_one hz)] exacts [norm_le_sqrt_two_mul_max z, one_pos.trans_le hz', mul_pos h2 hm₀, h2.ne', hm₀.ne'] _ =o[l] re := IsLittleO.add (isLittleO_const_left.2 <| Or.inr <| hl.tendsto_abs_re) <| isLittleO_iff_nat_mul_le.2 fun n => by filter_upwards [isLittleO_iff_nat_mul_le'.1 hl.isLittleO_log_re_re n, hl.abs_im_pow_eventuallyLE_exp_re n, hl.tendsto_re.eventually_gt_atTop 1] with z hre him h₁ rcases le_total |z.im| z.re with hle | hle · rwa [max_eq_left hle] · have H : 1 < |z.im| := h₁.trans_le hle norm_cast at * rwa [max_eq_right hle, Real.norm_eq_abs, Real.norm_eq_abs, abs_of_pos (Real.log_pos H), ← Real.log_pow, Real.log_le_iff_le_exp (pow_pos (one_pos.trans H) _), abs_of_pos (one_pos.trans h₁)] @[deprecated (since := "2025-02-17")] alias isLittleO_log_abs_re := isLittleO_log_norm_re /-! ### Main results -/ lemma isTheta_cpow_exp_re_mul_log (hl : IsExpCmpFilter l) (a : ℂ) : (· ^ a) =Θ[l] fun z ↦ Real.exp (re a * Real.log ‖z‖) := calc (fun z => z ^ a) =Θ[l] (fun z : ℂ => ‖z‖ ^ re a) := isTheta_cpow_const_rpow fun _ _ => hl.eventually_ne _ =ᶠ[l] fun z => Real.exp (re a * Real.log ‖z‖) := (hl.eventually_ne.mono fun z hz => by simp [Real.rpow_def_of_pos, norm_pos_iff.mpr hz, mul_comm]) /-- If `l : Filter ℂ` is an "exponential comparison filter", then for any complex `a` and any positive real `b`, we have `(fun z ↦ z ^ a) =o[l] (fun z ↦ exp (b * z))`. -/ theorem isLittleO_cpow_exp (hl : IsExpCmpFilter l) (a : ℂ) {b : ℝ} (hb : 0 < b) : (fun z => z ^ a) =o[l] fun z => exp (b * z) := calc (fun z => z ^ a) =Θ[l] fun z => Real.exp (re a * Real.log ‖z‖) := hl.isTheta_cpow_exp_re_mul_log a _ =o[l] fun z => exp (b * z) := IsLittleO.of_norm_right <| by simp only [norm_exp, re_ofReal_mul, Real.isLittleO_exp_comp_exp_comp] refine (IsEquivalent.refl.sub_isLittleO ?_).symm.tendsto_atTop (hl.tendsto_re.const_mul_atTop hb) exact (hl.isLittleO_log_norm_re.const_mul_left _).const_mul_right hb.ne' /-- If `l : Filter ℂ` is an "exponential comparison filter", then for any complex `a₁`, `a₂` and any real `b₁ < b₂`, we have `(fun z ↦ z ^ a₁ * exp (b₁ * z)) =o[l] (fun z ↦ z ^ a₂ * exp (b₂ * z))`. -/ theorem isLittleO_cpow_mul_exp {b₁ b₂ : ℝ} (hl : IsExpCmpFilter l) (hb : b₁ < b₂) (a₁ a₂ : ℂ) : (fun z => z ^ a₁ * exp (b₁ * z)) =o[l] fun z => z ^ a₂ * exp (b₂ * z) := calc (fun z => z ^ a₁ * exp (b₁ * z)) =ᶠ[l] fun z => z ^ a₂ * exp (b₁ * z) * z ^ (a₁ - a₂) := hl.eventually_ne.mono fun z hz => by simp only rw [mul_right_comm, ← cpow_add _ _ hz, add_sub_cancel] _ =o[l] fun z => z ^ a₂ * exp (b₁ * z) * exp (↑(b₂ - b₁) * z) := ((isBigO_refl (fun z => z ^ a₂ * exp (b₁ * z)) l).mul_isLittleO <| hl.isLittleO_cpow_exp _ (sub_pos.2 hb)) _ =ᶠ[l] fun z => z ^ a₂ * exp (b₂ * z) := by simp only [ofReal_sub, sub_mul, mul_assoc, ← exp_add, add_sub_cancel] norm_cast /-- If `l : Filter ℂ` is an "exponential comparison filter", then for any complex `a` and any negative real `b`, we have `(fun z ↦ exp (b * z)) =o[l] (fun z ↦ z ^ a)`. -/ theorem isLittleO_exp_cpow (hl : IsExpCmpFilter l) (a : ℂ) {b : ℝ} (hb : b < 0) : (fun z => exp (b * z)) =o[l] fun z => z ^ a := by simpa using hl.isLittleO_cpow_mul_exp hb 0 a /-- If `l : Filter ℂ` is an "exponential comparison filter", then for any complex `a₁`, `a₂` and any natural `b₁ < b₂`, we have `(fun z ↦ z ^ a₁ * exp (b₁ * z)) =o[l] (fun z ↦ z ^ a₂ * exp (b₂ * z))`. -/ theorem isLittleO_pow_mul_exp {b₁ b₂ : ℝ} (hl : IsExpCmpFilter l) (hb : b₁ < b₂) (m n : ℕ) : (fun z => z ^ m * exp (b₁ * z)) =o[l] fun z => z ^ n * exp (b₂ * z) := by simpa only [cpow_natCast] using hl.isLittleO_cpow_mul_exp hb m n /-- If `l : Filter ℂ` is an "exponential comparison filter", then for any complex `a₁`, `a₂` and any integer `b₁ < b₂`, we have `(fun z ↦ z ^ a₁ * exp (b₁ * z)) =o[l] (fun z ↦ z ^ a₂ * exp (b₂ * z))`. -/ theorem isLittleO_zpow_mul_exp {b₁ b₂ : ℝ} (hl : IsExpCmpFilter l) (hb : b₁ < b₂) (m n : ℤ) :
(fun z => z ^ m * exp (b₁ * z)) =o[l] fun z => z ^ n * exp (b₂ * z) := by simpa only [cpow_intCast] using hl.isLittleO_cpow_mul_exp hb m n
Mathlib/Analysis/SpecialFunctions/CompareExp.lean
200
201
/- Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import Mathlib.Combinatorics.SimpleGraph.Clique import Mathlib.Combinatorics.SimpleGraph.Regularity.Uniform import Mathlib.Data.Real.Basic import Mathlib.Tactic.Linarith /-! # Triangle counting lemma In this file, we prove the triangle counting lemma. ## References [Yaël Dillies, Bhavik Mehta, *Formalising Szemerédi’s Regularity Lemma in Lean*][srl_itp] -/ -- TODO: This instance is bad because it creates data out of a Prop attribute [-instance] decidableEq_of_subsingleton open Finset Fintype variable {α : Type*} (G : SimpleGraph α) [DecidableRel G.Adj] {ε : ℝ} {s t u : Finset α} namespace SimpleGraph /-- The vertices of `s` whose density in `t` is `ε` less than expected. -/ private noncomputable def badVertices (ε : ℝ) (s t : Finset α) : Finset α := {x ∈ s | #{y ∈ t | G.Adj x y} < (G.edgeDensity s t - ε) * #t} private lemma card_interedges_badVertices_le : #(Rel.interedges G.Adj (badVertices G ε s t) t) ≤ #(badVertices G ε s t) * #t * (G.edgeDensity s t - ε) := by classical refine (Nat.cast_le.2 <| (card_le_card <| subset_of_eq (Rel.interedges_eq_biUnion _)).trans card_biUnion_le).trans ?_ simp_rw [Nat.cast_sum, card_map, ← nsmul_eq_mul, smul_mul_assoc, mul_comm (#t : ℝ)] exact sum_le_card_nsmul _ _ _ fun x hx ↦ (mem_filter.1 hx).2.le private lemma edgeDensity_badVertices_le (hε : 0 ≤ ε) (dst : 2 * ε ≤ G.edgeDensity s t) : G.edgeDensity (badVertices G ε s t) t ≤ G.edgeDensity s t - ε := by rw [edgeDensity_def] push_cast refine div_le_of_le_mul₀ (by positivity) (sub_nonneg_of_le <| by linarith) ?_ rw [mul_comm] exact G.card_interedges_badVertices_le private lemma card_badVertices_le (dst : 2 * ε ≤ G.edgeDensity s t) (hst : G.IsUniform ε s t) : #(badVertices G ε s t) ≤ #s * ε := by have hε : ε ≤ 1 := (le_rfl.trans <| le_mul_of_one_le_left hst.pos.le (by norm_num)).trans (dst.trans <| by exact_mod_cast edgeDensity_le_one _ _ _) by_contra! h have : |(G.edgeDensity (badVertices G ε s t) t - G.edgeDensity s t : ℝ)| < ε := hst (filter_subset _ _) Subset.rfl h.le (mul_le_of_le_one_right (Nat.cast_nonneg _) hε) rw [abs_sub_lt_iff] at this linarith [G.edgeDensity_badVertices_le hst.pos.le dst] /-- A subset of the triangles constructed in a weird way to make them easy to count. -/ private lemma triangle_split_helper [DecidableEq α] : (s \ (badVertices G ε s t ∪ badVertices G ε s u)).biUnion (fun x ↦ (G.interedges {y ∈ t | G.Adj x y} {y ∈ u | G.Adj x y}).image (x, ·)) ⊆ (s ×ˢ t ×ˢ u).filter (fun (x, y, z) ↦ G.Adj x y ∧ G.Adj x z ∧ G.Adj y z) := by rintro ⟨x, y, z⟩ simp only [mem_filter, mem_product, mem_biUnion, mem_sdiff, exists_prop, mem_union, mem_image, Prod.exists, and_assoc, exists_imp, and_imp, Prod.mk_inj, mem_interedges_iff] rintro x hx - y z hy xy hz xz yz rfl rfl rfl exact ⟨hx, hy, hz, xy, xz, yz⟩
private lemma good_vertices_triangle_card [DecidableEq α] (dst : 2 * ε ≤ G.edgeDensity s t) (dsu : 2 * ε ≤ G.edgeDensity s u) (dtu : 2 * ε ≤ G.edgeDensity t u) (utu : G.IsUniform ε t u) (x : α) (hx : x ∈ s \ (badVertices G ε s t ∪ badVertices G ε s u)) : ε ^ 3 * #t * #u ≤ #((({y ∈ t | G.Adj x y} ×ˢ {y ∈ u | G.Adj x y}).filter fun (y, z) ↦ G.Adj y z).image (x, ·)) := by simp only [mem_sdiff, badVertices, mem_union, not_or, mem_filter, not_and_or, not_lt] at hx rw [← or_and_left, and_or_left] at hx simp only [false_or, and_not_self, mul_comm (_ - _)] at hx obtain ⟨-, hxY, hsu⟩ := hx have hY : #t * ε ≤ #{y ∈ t | G.Adj x y} := (mul_le_mul_of_nonneg_left (by linarith) (Nat.cast_nonneg _)).trans hxY have hZ : #u * ε ≤ #{y ∈ u | G.Adj x y} := (mul_le_mul_of_nonneg_left (by linarith) (Nat.cast_nonneg _)).trans hsu rw [card_image_of_injective _ (Prod.mk_right_injective _)] have := utu (filter_subset (G.Adj x) _) (filter_subset (G.Adj x) _) hY hZ have : ε ≤ G.edgeDensity {y ∈ t | G.Adj x y} {y ∈ u | G.Adj x y} := by rw [abs_sub_lt_iff] at this; linarith rw [edgeDensity_def] at this push_cast at this have hε := utu.pos.le refine le_trans ?_ (mul_le_of_le_div₀ (Nat.cast_nonneg _) (by positivity) this) refine Eq.trans_le ?_ (mul_le_mul_of_nonneg_left (mul_le_mul hY hZ (by positivity) (by positivity)) hε) ring
Mathlib/Combinatorics/SimpleGraph/Triangle/Counting.lean
72
95
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import Mathlib.Analysis.SpecialFunctions.Pow.Complex import Qq /-! # Power function on `ℝ` We construct the power functions `x ^ y`, where `x` and `y` are real numbers. -/ noncomputable section open Real ComplexConjugate Finset Set /- ## Definitions -/ namespace Real variable {x y z : ℝ} /-- The real power function `x ^ y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0=1` and `0 ^ y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitrary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (π y)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : Pow ℝ ℝ := ⟨rpow⟩ @[simp] theorem rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl theorem rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl theorem rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, Complex.cpow_def]; split_ifs <;> simp_all [(Complex.ofReal_log hx).symm, -Complex.ofReal_mul, (Complex.ofReal_mul _ _).symm, Complex.exp_ofReal_re, Complex.ofReal_eq_zero] theorem rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] theorem exp_mul (x y : ℝ) : exp (x * y) = exp x ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] @[simp, norm_cast] theorem rpow_intCast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, ← Complex.ofReal_zpow, Complex.cpow_intCast, Complex.ofReal_intCast, Complex.ofReal_re] @[simp, norm_cast] theorem rpow_natCast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simpa using rpow_intCast x n @[simp] theorem exp_one_rpow (x : ℝ) : exp 1 ^ x = exp x := by rw [← exp_mul, one_mul] @[simp] lemma exp_one_pow (n : ℕ) : exp 1 ^ n = exp n := by rw [← rpow_natCast, exp_one_rpow] theorem rpow_eq_zero_iff_of_nonneg (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by simp only [rpow_def_of_nonneg hx] split_ifs <;> simp [*, exp_ne_zero] @[simp] lemma rpow_eq_zero (hx : 0 ≤ x) (hy : y ≠ 0) : x ^ y = 0 ↔ x = 0 := by simp [rpow_eq_zero_iff_of_nonneg, *] @[simp] lemma rpow_ne_zero (hx : 0 ≤ x) (hy : y ≠ 0) : x ^ y ≠ 0 ↔ x ≠ 0 := Real.rpow_eq_zero hx hy |>.not open Real theorem rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := by rw [rpow_def, Complex.cpow_def, if_neg] · have : Complex.log x * y = ↑(log (-x) * y) + ↑(y * π) * Complex.I := by simp only [Complex.log, Complex.norm_real, norm_eq_abs, abs_of_neg hx, log_neg_eq_log, Complex.arg_ofReal_of_neg hx, Complex.ofReal_mul] ring rw [this, Complex.exp_add_mul_I, ← Complex.ofReal_exp, ← Complex.ofReal_cos, ← Complex.ofReal_sin, mul_add, ← Complex.ofReal_mul, ← mul_assoc, ← Complex.ofReal_mul, Complex.add_re, Complex.ofReal_re, Complex.mul_re, Complex.I_re, Complex.ofReal_im, Real.log_neg_eq_log] ring · rw [Complex.ofReal_eq_zero] exact ne_of_lt hx theorem rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs with h <;> simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ @[bound] theorem rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw [rpow_def_of_pos hx]; apply exp_pos @[simp] theorem rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] theorem rpow_zero_pos (x : ℝ) : 0 < x ^ (0 : ℝ) := by simp @[simp] theorem zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] theorem zero_rpow_eq_iff {x : ℝ} {a : ℝ} : 0 ^ x = a ↔ x ≠ 0 ∧ a = 0 ∨ x = 0 ∧ a = 1 := by constructor · intro hyp simp only [rpow_def, Complex.ofReal_zero] at hyp by_cases h : x = 0 · subst h simp only [Complex.one_re, Complex.ofReal_zero, Complex.cpow_zero] at hyp exact Or.inr ⟨rfl, hyp.symm⟩ · rw [Complex.zero_cpow (Complex.ofReal_ne_zero.mpr h)] at hyp exact Or.inl ⟨h, hyp.symm⟩ · rintro (⟨h, rfl⟩ | ⟨rfl, rfl⟩) · exact zero_rpow h · exact rpow_zero _ theorem eq_zero_rpow_iff {x : ℝ} {a : ℝ} : a = 0 ^ x ↔ x ≠ 0 ∧ a = 0 ∨ x = 0 ∧ a = 1 := by rw [← zero_rpow_eq_iff, eq_comm] @[simp] theorem rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] theorem one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] theorem zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by by_cases h : x = 0 <;> simp [h, zero_le_one] theorem zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by by_cases h : x = 0 <;> simp [h, zero_le_one] @[bound] theorem rpow_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs <;> simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] theorem abs_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : |x ^ y| = |x| ^ y := by have h_rpow_nonneg : 0 ≤ x ^ y := Real.rpow_nonneg hx_nonneg _ rw [abs_eq_self.mpr hx_nonneg, abs_eq_self.mpr h_rpow_nonneg] @[bound] theorem abs_rpow_le_abs_rpow (x y : ℝ) : |x ^ y| ≤ |x| ^ y := by rcases le_or_lt 0 x with hx | hx · rw [abs_rpow_of_nonneg hx] · rw [abs_of_neg hx, rpow_def_of_neg hx, rpow_def_of_pos (neg_pos.2 hx), log_neg_eq_log, abs_mul, abs_of_pos (exp_pos _)] exact mul_le_of_le_one_right (exp_pos _).le (abs_cos_le_one _) theorem abs_rpow_le_exp_log_mul (x y : ℝ) : |x ^ y| ≤ exp (log x * y) := by refine (abs_rpow_le_abs_rpow x y).trans ?_ by_cases hx : x = 0 · by_cases hy : y = 0 <;> simp [hx, hy, zero_le_one] · rw [rpow_def_of_pos (abs_pos.2 hx), log_abs] lemma rpow_inv_log (hx₀ : 0 < x) (hx₁ : x ≠ 1) : x ^ (log x)⁻¹ = exp 1 := by rw [rpow_def_of_pos hx₀, mul_inv_cancel₀] exact log_ne_zero.2 ⟨hx₀.ne', hx₁, (hx₀.trans' <| by norm_num).ne'⟩ /-- See `Real.rpow_inv_log` for the equality when `x ≠ 1` is strictly positive. -/ lemma rpow_inv_log_le_exp_one : x ^ (log x)⁻¹ ≤ exp 1 := by calc _ ≤ |x ^ (log x)⁻¹| := le_abs_self _ _ ≤ |x| ^ (log x)⁻¹ := abs_rpow_le_abs_rpow .. rw [← log_abs] obtain hx | hx := (abs_nonneg x).eq_or_gt · simp [hx] · rw [rpow_def_of_pos hx] gcongr exact mul_inv_le_one theorem norm_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : ‖x ^ y‖ = ‖x‖ ^ y := by simp_rw [Real.norm_eq_abs] exact abs_rpow_of_nonneg hx_nonneg variable {w x y z : ℝ} theorem rpow_add (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] theorem rpow_add' (hx : 0 ≤ x) (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by rcases hx.eq_or_lt with (rfl | pos) · rw [zero_rpow h, zero_eq_mul] have : y ≠ 0 ∨ z ≠ 0 := not_and_or.1 fun ⟨hy, hz⟩ => h <| hy.symm ▸ hz.symm ▸ zero_add 0 exact this.imp zero_rpow zero_rpow · exact rpow_add pos _ _ /-- Variant of `Real.rpow_add'` that avoids having to prove `y + z = w` twice. -/ lemma rpow_of_add_eq (hx : 0 ≤ x) (hw : w ≠ 0) (h : y + z = w) : x ^ w = x ^ y * x ^ z := by rw [← h, rpow_add' hx]; rwa [h] theorem rpow_add_of_nonneg (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := by rcases hy.eq_or_lt with (rfl | hy) · rw [zero_add, rpow_zero, one_mul] exact rpow_add' hx (ne_of_gt <| add_pos_of_pos_of_nonneg hy hz) /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ theorem le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := by rcases le_iff_eq_or_lt.1 hx with (H | pos) · by_cases h : y + z = 0 · simp only [H.symm, h, rpow_zero] calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 := mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one _ = 1 := by simp · simp [rpow_add', ← H, h] · simp [rpow_add pos] theorem rpow_sum_of_pos {ι : Type*} {a : ℝ} (ha : 0 < a) (f : ι → ℝ) (s : Finset ι) : (a ^ ∑ x ∈ s, f x) = ∏ x ∈ s, a ^ f x := map_sum (⟨⟨fun (x : ℝ) => (a ^ x : ℝ), rpow_zero a⟩, rpow_add ha⟩ : ℝ →+ (Additive ℝ)) f s theorem rpow_sum_of_nonneg {ι : Type*} {a : ℝ} (ha : 0 ≤ a) {s : Finset ι} {f : ι → ℝ} (h : ∀ x ∈ s, 0 ≤ f x) : (a ^ ∑ x ∈ s, f x) = ∏ x ∈ s, a ^ f x := by induction' s using Finset.cons_induction with i s hi ihs · rw [sum_empty, Finset.prod_empty, rpow_zero] · rw [forall_mem_cons] at h rw [sum_cons, prod_cons, ← ihs h.2, rpow_add_of_nonneg ha h.1 (sum_nonneg h.2)] theorem rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ (-y) = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs <;> simp_all [exp_neg] theorem rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] theorem rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg] at h ⊢ simp only [rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] protected theorem _root_.HasCompactSupport.rpow_const {α : Type*} [TopologicalSpace α] {f : α → ℝ} (hf : HasCompactSupport f) {r : ℝ} (hr : r ≠ 0) : HasCompactSupport (fun x ↦ f x ^ r) := hf.comp_left (g := (· ^ r)) (Real.zero_rpow hr) end Real /-! ## Comparing real and complex powers -/ namespace Complex theorem ofReal_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp only [Real.rpow_def_of_nonneg hx, Complex.cpow_def, ofReal_eq_zero]; split_ifs <;> simp [Complex.ofReal_log hx] theorem ofReal_cpow_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℂ) : (x : ℂ) ^ y = (-x : ℂ) ^ y * exp (π * I * y) := by rcases hx.eq_or_lt with (rfl | hlt) · rcases eq_or_ne y 0 with (rfl | hy) <;> simp [*] have hne : (x : ℂ) ≠ 0 := ofReal_ne_zero.mpr hlt.ne rw [cpow_def_of_ne_zero hne, cpow_def_of_ne_zero (neg_ne_zero.2 hne), ← exp_add, ← add_mul, log, log, norm_neg, arg_ofReal_of_neg hlt, ← ofReal_neg, arg_ofReal_of_nonneg (neg_nonneg.2 hx), ofReal_zero, zero_mul, add_zero] lemma cpow_ofReal (x : ℂ) (y : ℝ) : x ^ (y : ℂ) = ↑(‖x‖ ^ y) * (Real.cos (arg x * y) + Real.sin (arg x * y) * I) := by rcases eq_or_ne x 0 with rfl | hx · simp [ofReal_cpow le_rfl] · rw [cpow_def_of_ne_zero hx, exp_eq_exp_re_mul_sin_add_cos, mul_comm (log x)] norm_cast rw [re_ofReal_mul, im_ofReal_mul, log_re, log_im, mul_comm y, mul_comm y, Real.exp_mul, Real.exp_log] rwa [norm_pos_iff] lemma cpow_ofReal_re (x : ℂ) (y : ℝ) : (x ^ (y : ℂ)).re = ‖x‖ ^ y * Real.cos (arg x * y) := by rw [cpow_ofReal]; generalize arg x * y = z; simp [Real.cos] lemma cpow_ofReal_im (x : ℂ) (y : ℝ) : (x ^ (y : ℂ)).im = ‖x‖ ^ y * Real.sin (arg x * y) := by rw [cpow_ofReal]; generalize arg x * y = z; simp [Real.sin] theorem norm_cpow_of_ne_zero {z : ℂ} (hz : z ≠ 0) (w : ℂ) : ‖z ^ w‖ = ‖z‖ ^ w.re / Real.exp (arg z * im w) := by rw [cpow_def_of_ne_zero hz, norm_exp, mul_re, log_re, log_im, Real.exp_sub, Real.rpow_def_of_pos (norm_pos_iff.mpr hz)] theorem norm_cpow_of_imp {z w : ℂ} (h : z = 0 → w.re = 0 → w = 0) : ‖z ^ w‖ = ‖z‖ ^ w.re / Real.exp (arg z * im w) := by rcases ne_or_eq z 0 with (hz | rfl) <;> [exact norm_cpow_of_ne_zero hz w; rw [norm_zero]] rcases eq_or_ne w.re 0 with hw | hw · simp [hw, h rfl hw] · rw [Real.zero_rpow hw, zero_div, zero_cpow, norm_zero] exact ne_of_apply_ne re hw theorem norm_cpow_le (z w : ℂ) : ‖z ^ w‖ ≤ ‖z‖ ^ w.re / Real.exp (arg z * im w) := by by_cases h : z = 0 → w.re = 0 → w = 0 · exact (norm_cpow_of_imp h).le · push_neg at h simp [h] @[simp] theorem norm_cpow_real (x : ℂ) (y : ℝ) : ‖x ^ (y : ℂ)‖ = ‖x‖ ^ y := by rw [norm_cpow_of_imp] <;> simp @[simp] theorem norm_cpow_inv_nat (x : ℂ) (n : ℕ) : ‖x ^ (n⁻¹ : ℂ)‖ = ‖x‖ ^ (n⁻¹ : ℝ) := by rw [← norm_cpow_real]; simp theorem norm_cpow_eq_rpow_re_of_pos {x : ℝ} (hx : 0 < x) (y : ℂ) : ‖(x : ℂ) ^ y‖ = x ^ y.re := by rw [norm_cpow_of_ne_zero (ofReal_ne_zero.mpr hx.ne'), arg_ofReal_of_nonneg hx.le, zero_mul, Real.exp_zero, div_one, Complex.norm_of_nonneg hx.le] theorem norm_cpow_eq_rpow_re_of_nonneg {x : ℝ} (hx : 0 ≤ x) {y : ℂ} (hy : re y ≠ 0) : ‖(x : ℂ) ^ y‖ = x ^ re y := by rw [norm_cpow_of_imp] <;> simp [*, arg_ofReal_of_nonneg, abs_of_nonneg] @[deprecated (since := "2025-02-17")] alias abs_cpow_of_ne_zero := norm_cpow_of_ne_zero @[deprecated (since := "2025-02-17")] alias abs_cpow_of_imp := norm_cpow_of_imp @[deprecated (since := "2025-02-17")] alias abs_cpow_le := norm_cpow_le @[deprecated (since := "2025-02-17")] alias abs_cpow_real := norm_cpow_real @[deprecated (since := "2025-02-17")] alias abs_cpow_inv_nat := norm_cpow_inv_nat @[deprecated (since := "2025-02-17")] alias abs_cpow_eq_rpow_re_of_pos := norm_cpow_eq_rpow_re_of_pos @[deprecated (since := "2025-02-17")] alias abs_cpow_eq_rpow_re_of_nonneg := norm_cpow_eq_rpow_re_of_nonneg open Filter in lemma norm_ofReal_cpow_eventually_eq_atTop (c : ℂ) : (fun t : ℝ ↦ ‖(t : ℂ) ^ c‖) =ᶠ[atTop] fun t ↦ t ^ c.re := by filter_upwards [eventually_gt_atTop 0] with t ht rw [norm_cpow_eq_rpow_re_of_pos ht] lemma norm_natCast_cpow_of_re_ne_zero (n : ℕ) {s : ℂ} (hs : s.re ≠ 0) : ‖(n : ℂ) ^ s‖ = (n : ℝ) ^ (s.re) := by rw [← ofReal_natCast, norm_cpow_eq_rpow_re_of_nonneg n.cast_nonneg hs] lemma norm_natCast_cpow_of_pos {n : ℕ} (hn : 0 < n) (s : ℂ) : ‖(n : ℂ) ^ s‖ = (n : ℝ) ^ (s.re) := by rw [← ofReal_natCast, norm_cpow_eq_rpow_re_of_pos (Nat.cast_pos.mpr hn) _] lemma norm_natCast_cpow_pos_of_pos {n : ℕ} (hn : 0 < n) (s : ℂ) : 0 < ‖(n : ℂ) ^ s‖ := (norm_natCast_cpow_of_pos hn _).symm ▸ Real.rpow_pos_of_pos (Nat.cast_pos.mpr hn) _ theorem cpow_mul_ofReal_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) (z : ℂ) : (x : ℂ) ^ (↑y * z) = (↑(x ^ y) : ℂ) ^ z := by rw [cpow_mul, ofReal_cpow hx] · rw [← ofReal_log hx, ← ofReal_mul, ofReal_im, neg_lt_zero]; exact Real.pi_pos · rw [← ofReal_log hx, ← ofReal_mul, ofReal_im]; exact Real.pi_pos.le end Complex /-! ### Positivity extension -/ namespace Mathlib.Meta.Positivity open Lean Meta Qq /-- Extension for the `positivity` tactic: exponentiation by a real number is positive (namely 1) when the exponent is zero. The other cases are done in `evalRpow`. -/ @[positivity (_ : ℝ) ^ (0 : ℝ)] def evalRpowZero : PositivityExt where eval {u α} _ _ e := do match u, α, e with | 0, ~q(ℝ), ~q($a ^ (0 : ℝ)) => assertInstancesCommute pure (.positive q(Real.rpow_zero_pos $a)) | _, _, _ => throwError "not Real.rpow" /-- Extension for the `positivity` tactic: exponentiation by a real number is nonnegative when the base is nonnegative and positive when the base is positive. -/ @[positivity (_ : ℝ) ^ (_ : ℝ)] def evalRpow : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q($a ^ ($b : ℝ)) => let ra ← core q(inferInstance) q(inferInstance) a assertInstancesCommute match ra with | .positive pa => pure (.positive q(Real.rpow_pos_of_pos $pa $b)) | .nonnegative pa => pure (.nonnegative q(Real.rpow_nonneg $pa $b)) | _ => pure .none | _, _, _ => throwError "not Real.rpow" end Mathlib.Meta.Positivity /-! ## Further algebraic properties of `rpow` -/ namespace Real variable {x y z : ℝ} {n : ℕ} theorem rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← Complex.ofReal_inj, Complex.ofReal_cpow (rpow_nonneg hx _), Complex.ofReal_cpow hx, Complex.ofReal_mul, Complex.cpow_mul, Complex.ofReal_cpow hx] <;> simp only [(Complex.ofReal_mul _ _).symm, (Complex.ofReal_log hx).symm, Complex.ofReal_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_pow_comm {x : ℝ} (hx : 0 ≤ x) (y : ℝ) (n : ℕ) : (x ^ y) ^ n = (x ^ n) ^ y := by simp_rw [← rpow_natCast, ← rpow_mul hx, mul_comm y] lemma rpow_zpow_comm {x : ℝ} (hx : 0 ≤ x) (y : ℝ) (n : ℤ) : (x ^ y) ^ n = (x ^ n) ^ y := by simp_rw [← rpow_intCast, ← rpow_mul hx, mul_comm y] lemma rpow_add_intCast {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_def, rpow_def, Complex.ofReal_add, Complex.cpow_add _ _ (Complex.ofReal_ne_zero.mpr hx), Complex.ofReal_intCast, Complex.cpow_intCast, ← Complex.ofReal_zpow, mul_comm, Complex.re_ofReal_mul, mul_comm] lemma rpow_add_natCast {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := by simpa using rpow_add_intCast hx y n lemma rpow_sub_intCast {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_add_intCast hx y (-n) lemma rpow_sub_natCast {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_sub_intCast hx y n lemma rpow_add_intCast' (hx : 0 ≤ x) {n : ℤ} (h : y + n ≠ 0) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_add' hx h, rpow_intCast] lemma rpow_add_natCast' (hx : 0 ≤ x) (h : y + n ≠ 0) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_add' hx h, rpow_natCast] lemma rpow_sub_intCast' (hx : 0 ≤ x) {n : ℤ} (h : y - n ≠ 0) : x ^ (y - n) = x ^ y / x ^ n := by rw [rpow_sub' hx h, rpow_intCast] lemma rpow_sub_natCast' (hx : 0 ≤ x) (h : y - n ≠ 0) : x ^ (y - n) = x ^ y / x ^ n := by rw [rpow_sub' hx h, rpow_natCast] theorem rpow_add_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_natCast hx y 1 theorem rpow_sub_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_natCast hx y 1 lemma rpow_add_one' (hx : 0 ≤ x) (h : y + 1 ≠ 0) : x ^ (y + 1) = x ^ y * x := by rw [rpow_add' hx h, rpow_one] lemma rpow_one_add' (hx : 0 ≤ x) (h : 1 + y ≠ 0) : x ^ (1 + y) = x * x ^ y := by rw [rpow_add' hx h, rpow_one] lemma rpow_sub_one' (hx : 0 ≤ x) (h : y - 1 ≠ 0) : x ^ (y - 1) = x ^ y / x := by rw [rpow_sub' hx h, rpow_one] lemma rpow_one_sub' (hx : 0 ≤ x) (h : 1 - y ≠ 0) : x ^ (1 - y) = x / x ^ y := by rw [rpow_sub' hx h, rpow_one] @[simp] theorem rpow_two (x : ℝ) : x ^ (2 : ℝ) = x ^ 2 := by rw [← rpow_natCast] simp only [Nat.cast_ofNat] theorem rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := by suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹ by rwa [Int.cast_neg, Int.cast_one] at H simp only [rpow_intCast, zpow_one, zpow_neg] theorem mul_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) : (x * y) ^ z = x ^ z * y ^ z := by iterate 2 rw [Real.rpow_def_of_nonneg]; split_ifs with h_ifs <;> simp_all · rw [log_mul ‹_› ‹_›, add_mul, exp_add, rpow_def_of_pos (hy.lt_of_ne' ‹_›)] all_goals positivity theorem inv_rpow (hx : 0 ≤ x) (y : ℝ) : x⁻¹ ^ y = (x ^ y)⁻¹ := by simp only [← rpow_neg_one, ← rpow_mul hx, mul_comm] theorem div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] theorem log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x ^ y) = y * log x := by apply exp_injective rw [exp_log (rpow_pos_of_pos hx y), ← exp_log hx, mul_comm, rpow_def_of_pos (exp_pos (log x)) y] theorem mul_log_eq_log_iff {x y z : ℝ} (hx : 0 < x) (hz : 0 < z) : y * log x = log z ↔ x ^ y = z := ⟨fun h ↦ log_injOn_pos (rpow_pos_of_pos hx _) hz <| log_rpow hx _ |>.trans h, by rintro rfl; rw [log_rpow hx]⟩ @[simp] lemma rpow_rpow_inv (hx : 0 ≤ x) (hy : y ≠ 0) : (x ^ y) ^ y⁻¹ = x := by rw [← rpow_mul hx, mul_inv_cancel₀ hy, rpow_one] @[simp] lemma rpow_inv_rpow (hx : 0 ≤ x) (hy : y ≠ 0) : (x ^ y⁻¹) ^ y = x := by rw [← rpow_mul hx, inv_mul_cancel₀ hy, rpow_one] theorem pow_rpow_inv_natCast (hx : 0 ≤ x) (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by have hn0 : (n : ℝ) ≠ 0 := Nat.cast_ne_zero.2 hn rw [← rpow_natCast, ← rpow_mul hx, mul_inv_cancel₀ hn0, rpow_one] theorem rpow_inv_natCast_pow (hx : 0 ≤ x) (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by have hn0 : (n : ℝ) ≠ 0 := Nat.cast_ne_zero.2 hn rw [← rpow_natCast, ← rpow_mul hx, inv_mul_cancel₀ hn0, rpow_one] lemma rpow_natCast_mul (hx : 0 ≤ x) (n : ℕ) (z : ℝ) : x ^ (n * z) = (x ^ n) ^ z := by rw [rpow_mul hx, rpow_natCast] lemma rpow_mul_natCast (hx : 0 ≤ x) (y : ℝ) (n : ℕ) : x ^ (y * n) = (x ^ y) ^ n := by rw [rpow_mul hx, rpow_natCast] lemma rpow_intCast_mul (hx : 0 ≤ x) (n : ℤ) (z : ℝ) : x ^ (n * z) = (x ^ n) ^ z := by rw [rpow_mul hx, rpow_intCast] lemma rpow_mul_intCast (hx : 0 ≤ x) (y : ℝ) (n : ℤ) : x ^ (y * n) = (x ^ y) ^ n := by rw [rpow_mul hx, rpow_intCast] /-! Note: lemmas about `(∏ i ∈ s, f i ^ r)` such as `Real.finset_prod_rpow` are proved in `Mathlib/Analysis/SpecialFunctions/Pow/NNReal.lean` instead. -/ /-! ## Order and monotonicity -/ @[gcongr, bound] theorem rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x ^ z < y ^ z := by rw [le_iff_eq_or_lt] at hx; rcases hx with hx | hx · rw [← hx, zero_rpow (ne_of_gt hz)] exact rpow_pos_of_pos (by rwa [← hx] at hxy) _ · rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp] exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz theorem strictMonoOn_rpow_Ici_of_exponent_pos {r : ℝ} (hr : 0 < r) : StrictMonoOn (fun (x : ℝ) => x ^ r) (Set.Ici 0) := fun _ ha _ _ hab => rpow_lt_rpow ha hab hr @[gcongr, bound] theorem rpow_le_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := by rcases eq_or_lt_of_le h₁ with (rfl | h₁'); · rfl rcases eq_or_lt_of_le h₂ with (rfl | h₂'); · simp exact le_of_lt (rpow_lt_rpow h h₁' h₂') theorem monotoneOn_rpow_Ici_of_exponent_nonneg {r : ℝ} (hr : 0 ≤ r) : MonotoneOn (fun (x : ℝ) => x ^ r) (Set.Ici 0) := fun _ ha _ _ hab => rpow_le_rpow ha hab hr lemma rpow_lt_rpow_of_neg (hx : 0 < x) (hxy : x < y) (hz : z < 0) : y ^ z < x ^ z := by have := hx.trans hxy rw [← inv_lt_inv₀, ← rpow_neg, ← rpow_neg] on_goal 1 => refine rpow_lt_rpow ?_ hxy (neg_pos.2 hz) all_goals positivity lemma rpow_le_rpow_of_nonpos (hx : 0 < x) (hxy : x ≤ y) (hz : z ≤ 0) : y ^ z ≤ x ^ z := by have := hx.trans_le hxy rw [← inv_le_inv₀, ← rpow_neg, ← rpow_neg] on_goal 1 => refine rpow_le_rpow ?_ hxy (neg_nonneg.2 hz) all_goals positivity theorem rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le fun h => rpow_le_rpow hy h (le_of_lt hz), fun h => rpow_lt_rpow hx h hz⟩ theorem rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 <| rpow_lt_rpow_iff hy hx hz lemma rpow_lt_rpow_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z < y ^ z ↔ y < x := ⟨lt_imp_lt_of_le_imp_le fun h ↦ rpow_le_rpow_of_nonpos hx h hz.le, fun h ↦ rpow_lt_rpow_of_neg hy h hz⟩ lemma rpow_le_rpow_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z ≤ y ^ z ↔ y ≤ x := le_iff_le_iff_lt_iff_lt.2 <| rpow_lt_rpow_iff_of_neg hy hx hz lemma le_rpow_inv_iff_of_pos (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ≤ y ^ z⁻¹ ↔ x ^ z ≤ y := by rw [← rpow_le_rpow_iff hx _ hz, rpow_inv_rpow] <;> positivity lemma rpow_inv_le_iff_of_pos (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z⁻¹ ≤ y ↔ x ≤ y ^ z := by rw [← rpow_le_rpow_iff _ hy hz, rpow_inv_rpow] <;> positivity lemma lt_rpow_inv_iff_of_pos (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x < y ^ z⁻¹ ↔ x ^ z < y := lt_iff_lt_of_le_iff_le <| rpow_inv_le_iff_of_pos hy hx hz lemma rpow_inv_lt_iff_of_pos (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z⁻¹ < y ↔ x < y ^ z := lt_iff_lt_of_le_iff_le <| le_rpow_inv_iff_of_pos hy hx hz theorem le_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ≤ y ^ z⁻¹ ↔ y ≤ x ^ z := by rw [← rpow_le_rpow_iff_of_neg _ hx hz, rpow_inv_rpow _ hz.ne] <;> positivity theorem lt_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x < y ^ z⁻¹ ↔ y < x ^ z := by rw [← rpow_lt_rpow_iff_of_neg _ hx hz, rpow_inv_rpow _ hz.ne] <;> positivity theorem rpow_inv_lt_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ < y ↔ y ^ z < x := by rw [← rpow_lt_rpow_iff_of_neg hy _ hz, rpow_inv_rpow _ hz.ne] <;> positivity theorem rpow_inv_le_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ ≤ y ↔ y ^ z ≤ x := by rw [← rpow_le_rpow_iff_of_neg hy _ hz, rpow_inv_rpow _ hz.ne] <;> positivity theorem rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x ^ y < x ^ z := by repeat' rw [rpow_def_of_pos (lt_trans zero_lt_one hx)] rw [exp_lt_exp]; exact mul_lt_mul_of_pos_left hyz (log_pos hx) @[gcongr] theorem rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := by repeat' rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)] rw [exp_le_exp]; exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx) theorem rpow_lt_rpow_of_exponent_neg {x y z : ℝ} (hy : 0 < y) (hxy : y < x) (hz : z < 0) : x ^ z < y ^ z := by have hx : 0 < x := hy.trans hxy rw [← neg_neg z, Real.rpow_neg (le_of_lt hx) (-z), Real.rpow_neg (le_of_lt hy) (-z), inv_lt_inv₀ (rpow_pos_of_pos hx _) (rpow_pos_of_pos hy _)] exact Real.rpow_lt_rpow (by positivity) hxy <| neg_pos_of_neg hz theorem strictAntiOn_rpow_Ioi_of_exponent_neg {r : ℝ} (hr : r < 0) : StrictAntiOn (fun (x : ℝ) => x ^ r) (Set.Ioi 0) := fun _ ha _ _ hab => rpow_lt_rpow_of_exponent_neg ha hab hr theorem rpow_le_rpow_of_exponent_nonpos {x y : ℝ} (hy : 0 < y) (hxy : y ≤ x) (hz : z ≤ 0) : x ^ z ≤ y ^ z := by rcases ne_or_eq z 0 with hz_zero | rfl case inl => rcases ne_or_eq x y with hxy' | rfl case inl => exact le_of_lt <| rpow_lt_rpow_of_exponent_neg hy (Ne.lt_of_le (id (Ne.symm hxy')) hxy) (Ne.lt_of_le hz_zero hz) case inr => simp case inr => simp theorem antitoneOn_rpow_Ioi_of_exponent_nonpos {r : ℝ} (hr : r ≤ 0) : AntitoneOn (fun (x : ℝ) => x ^ r) (Set.Ioi 0) := fun _ ha _ _ hab => rpow_le_rpow_of_exponent_nonpos ha hab hr @[simp] theorem rpow_le_rpow_left_iff (hx : 1 < x) : x ^ y ≤ x ^ z ↔ y ≤ z := by have x_pos : 0 < x := lt_trans zero_lt_one hx rw [← log_le_log_iff (rpow_pos_of_pos x_pos y) (rpow_pos_of_pos x_pos z), log_rpow x_pos, log_rpow x_pos, mul_le_mul_right (log_pos hx)] @[simp] theorem rpow_lt_rpow_left_iff (hx : 1 < x) : x ^ y < x ^ z ↔ y < z := by rw [lt_iff_not_le, rpow_le_rpow_left_iff hx, lt_iff_not_le] theorem rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := by repeat' rw [rpow_def_of_pos hx0] rw [exp_lt_exp]; exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1) theorem rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := by repeat' rw [rpow_def_of_pos hx0] rw [exp_le_exp]; exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1) @[simp] theorem rpow_le_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y ≤ x ^ z ↔ z ≤ y := by rw [← log_le_log_iff (rpow_pos_of_pos hx0 y) (rpow_pos_of_pos hx0 z), log_rpow hx0, log_rpow hx0, mul_le_mul_right_of_neg (log_neg hx0 hx1)] @[simp] theorem rpow_lt_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y < x ^ z ↔ z < y := by rw [lt_iff_not_le, rpow_le_rpow_left_iff_of_base_lt_one hx0 hx1, lt_iff_not_le] theorem rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x ^ z < 1 := by rw [← one_rpow z] exact rpow_lt_rpow hx1 hx2 hz theorem rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := by rw [← one_rpow z] exact rpow_le_rpow hx1 hx2 hz theorem rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := by convert rpow_lt_rpow_of_exponent_lt hx hz exact (rpow_zero x).symm theorem rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x ^ z ≤ 1 := by convert rpow_le_rpow_of_exponent_le hx hz exact (rpow_zero x).symm theorem one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := by rw [← one_rpow z] exact rpow_lt_rpow zero_le_one hx hz theorem one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x ^ z := by rw [← one_rpow z] exact rpow_le_rpow zero_le_one hx hz theorem one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := by convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz exact (rpow_zero x).symm theorem one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x ^ z := by convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz exact (rpow_zero x).symm theorem rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx.le, log_neg_iff hx] theorem rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rcases hx.eq_or_lt with (rfl | hx) · rcases _root_.em (y = 0) with (rfl | hy) <;> simp [*, lt_irrefl, zero_lt_one] · simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] theorem rpow_lt_one_iff' {x y : ℝ} (hx : 0 ≤ x) (hy : 0 < y) : x ^ y < 1 ↔ x < 1 := by rw [← Real.rpow_lt_rpow_iff hx zero_le_one hy, Real.one_rpow] theorem one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx.le, log_neg_iff hx] theorem one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := by rcases hx.eq_or_lt with (rfl | hx) · rcases _root_.em (y = 0) with (rfl | hy) <;> simp [*, lt_irrefl, (zero_lt_one' ℝ).not_lt] · simp [one_lt_rpow_iff_of_pos hx, hx] /-- This is a more general but less convenient version of `rpow_le_rpow_of_exponent_ge`. This version allows `x = 0`, so it explicitly forbids `x = y = 0`, `z ≠ 0`. -/ theorem rpow_le_rpow_of_exponent_ge_of_imp (hx0 : 0 ≤ x) (hx1 : x ≤ 1) (hyz : z ≤ y) (h : x = 0 → y = 0 → z = 0) : x ^ y ≤ x ^ z := by rcases eq_or_lt_of_le hx0 with (rfl | hx0') · rcases eq_or_ne y 0 with rfl | hy0 · rw [h rfl rfl] · rw [zero_rpow hy0] apply zero_rpow_nonneg · exact rpow_le_rpow_of_exponent_ge hx0' hx1 hyz /-- This version of `rpow_le_rpow_of_exponent_ge` allows `x = 0` but requires `0 ≤ z`. See also `rpow_le_rpow_of_exponent_ge_of_imp` for the most general version. -/ theorem rpow_le_rpow_of_exponent_ge' (hx0 : 0 ≤ x) (hx1 : x ≤ 1) (hz : 0 ≤ z) (hyz : z ≤ y) : x ^ y ≤ x ^ z := rpow_le_rpow_of_exponent_ge_of_imp hx0 hx1 hyz fun _ hy ↦ le_antisymm (hyz.trans_eq hy) hz lemma rpow_max {x y p : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (hp : 0 ≤ p) : (max x y) ^ p = max (x ^ p) (y ^ p) := by rcases le_total x y with hxy | hxy · rw [max_eq_right hxy, max_eq_right (rpow_le_rpow hx hxy hp)] · rw [max_eq_left hxy, max_eq_left (rpow_le_rpow hy hxy hp)] theorem self_le_rpow_of_le_one (h₁ : 0 ≤ x) (h₂ : x ≤ 1) (h₃ : y ≤ 1) : x ≤ x ^ y := by simpa only [rpow_one] using rpow_le_rpow_of_exponent_ge_of_imp h₁ h₂ h₃ fun _ ↦ (absurd · one_ne_zero) theorem self_le_rpow_of_one_le (h₁ : 1 ≤ x) (h₂ : 1 ≤ y) : x ≤ x ^ y := by simpa only [rpow_one] using rpow_le_rpow_of_exponent_le h₁ h₂ theorem rpow_le_self_of_le_one (h₁ : 0 ≤ x) (h₂ : x ≤ 1) (h₃ : 1 ≤ y) : x ^ y ≤ x := by simpa only [rpow_one] using rpow_le_rpow_of_exponent_ge_of_imp h₁ h₂ h₃ fun _ ↦ (absurd · (one_pos.trans_le h₃).ne') theorem rpow_le_self_of_one_le (h₁ : 1 ≤ x) (h₂ : y ≤ 1) : x ^ y ≤ x := by simpa only [rpow_one] using rpow_le_rpow_of_exponent_le h₁ h₂ theorem self_lt_rpow_of_lt_one (h₁ : 0 < x) (h₂ : x < 1) (h₃ : y < 1) : x < x ^ y := by simpa only [rpow_one] using rpow_lt_rpow_of_exponent_gt h₁ h₂ h₃ theorem self_lt_rpow_of_one_lt (h₁ : 1 < x) (h₂ : 1 < y) : x < x ^ y := by simpa only [rpow_one] using rpow_lt_rpow_of_exponent_lt h₁ h₂ theorem rpow_lt_self_of_lt_one (h₁ : 0 < x) (h₂ : x < 1) (h₃ : 1 < y) : x ^ y < x := by simpa only [rpow_one] using rpow_lt_rpow_of_exponent_gt h₁ h₂ h₃ theorem rpow_lt_self_of_one_lt (h₁ : 1 < x) (h₂ : y < 1) : x ^ y < x := by simpa only [rpow_one] using rpow_lt_rpow_of_exponent_lt h₁ h₂ theorem rpow_left_injOn {x : ℝ} (hx : x ≠ 0) : InjOn (fun y : ℝ => y ^ x) { y : ℝ | 0 ≤ y } := by rintro y hy z hz (hyz : y ^ x = z ^ x) rw [← rpow_one y, ← rpow_one z, ← mul_inv_cancel₀ hx, rpow_mul hy, rpow_mul hz, hyz] lemma rpow_left_inj (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y := (rpow_left_injOn hz).eq_iff hx hy lemma rpow_inv_eq (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : z ≠ 0) : x ^ z⁻¹ = y ↔ x = y ^ z := by rw [← rpow_left_inj _ hy hz, rpow_inv_rpow hx hz]; positivity lemma eq_rpow_inv (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : z ≠ 0) : x = y ^ z⁻¹ ↔ x ^ z = y := by rw [← rpow_left_inj hx _ hz, rpow_inv_rpow hy hz]; positivity theorem le_rpow_iff_log_le (hx : 0 < x) (hy : 0 < y) : x ≤ y ^ z ↔ log x ≤ z * log y := by rw [← log_le_log_iff hx (rpow_pos_of_pos hy z), log_rpow hy] lemma le_pow_iff_log_le (hx : 0 < x) (hy : 0 < y) : x ≤ y ^ n ↔ log x ≤ n * log y := rpow_natCast _ _ ▸ le_rpow_iff_log_le hx hy lemma le_zpow_iff_log_le {n : ℤ} (hx : 0 < x) (hy : 0 < y) : x ≤ y ^ n ↔ log x ≤ n * log y := rpow_intCast _ _ ▸ le_rpow_iff_log_le hx hy lemma le_rpow_of_log_le (hy : 0 < y) (h : log x ≤ z * log y) : x ≤ y ^ z := by obtain hx | hx := le_or_lt x 0 · exact hx.trans (rpow_pos_of_pos hy _).le · exact (le_rpow_iff_log_le hx hy).2 h lemma le_pow_of_log_le (hy : 0 < y) (h : log x ≤ n * log y) : x ≤ y ^ n := rpow_natCast _ _ ▸ le_rpow_of_log_le hy h lemma le_zpow_of_log_le {n : ℤ} (hy : 0 < y) (h : log x ≤ n * log y) : x ≤ y ^ n := rpow_intCast _ _ ▸ le_rpow_of_log_le hy h theorem lt_rpow_iff_log_lt (hx : 0 < x) (hy : 0 < y) : x < y ^ z ↔ log x < z * log y := by rw [← log_lt_log_iff hx (rpow_pos_of_pos hy z), log_rpow hy] lemma lt_pow_iff_log_lt (hx : 0 < x) (hy : 0 < y) : x < y ^ n ↔ log x < n * log y := rpow_natCast _ _ ▸ lt_rpow_iff_log_lt hx hy lemma lt_zpow_iff_log_lt {n : ℤ} (hx : 0 < x) (hy : 0 < y) : x < y ^ n ↔ log x < n * log y := rpow_intCast _ _ ▸ lt_rpow_iff_log_lt hx hy lemma lt_rpow_of_log_lt (hy : 0 < y) (h : log x < z * log y) : x < y ^ z := by obtain hx | hx := le_or_lt x 0 · exact hx.trans_lt (rpow_pos_of_pos hy _) · exact (lt_rpow_iff_log_lt hx hy).2 h lemma lt_pow_of_log_lt (hy : 0 < y) (h : log x < n * log y) : x < y ^ n := rpow_natCast _ _ ▸ lt_rpow_of_log_lt hy h lemma lt_zpow_of_log_lt {n : ℤ} (hy : 0 < y) (h : log x < n * log y) : x < y ^ n := rpow_intCast _ _ ▸ lt_rpow_of_log_lt hy h lemma rpow_le_iff_le_log (hx : 0 < x) (hy : 0 < y) : x ^ z ≤ y ↔ z * log x ≤ log y := by rw [← log_le_log_iff (rpow_pos_of_pos hx _) hy, log_rpow hx] lemma pow_le_iff_le_log (hx : 0 < x) (hy : 0 < y) : x ^ n ≤ y ↔ n * log x ≤ log y := by rw [← rpow_le_iff_le_log hx hy, rpow_natCast] lemma zpow_le_iff_le_log {n : ℤ} (hx : 0 < x) (hy : 0 < y) : x ^ n ≤ y ↔ n * log x ≤ log y := by rw [← rpow_le_iff_le_log hx hy, rpow_intCast] lemma le_log_of_rpow_le (hx : 0 < x) (h : x ^ z ≤ y) : z * log x ≤ log y := log_rpow hx _ ▸ log_le_log (by positivity) h lemma le_log_of_pow_le (hx : 0 < x) (h : x ^ n ≤ y) : n * log x ≤ log y := le_log_of_rpow_le hx (rpow_natCast _ _ ▸ h) lemma le_log_of_zpow_le {n : ℤ} (hx : 0 < x) (h : x ^ n ≤ y) : n * log x ≤ log y := le_log_of_rpow_le hx (rpow_intCast _ _ ▸ h) lemma rpow_le_of_le_log (hy : 0 < y) (h : log x ≤ z * log y) : x ≤ y ^ z := by obtain hx | hx := le_or_lt x 0 · exact hx.trans (rpow_pos_of_pos hy _).le · exact (le_rpow_iff_log_le hx hy).2 h lemma pow_le_of_le_log (hy : 0 < y) (h : log x ≤ n * log y) : x ≤ y ^ n := rpow_natCast _ _ ▸ rpow_le_of_le_log hy h lemma zpow_le_of_le_log {n : ℤ} (hy : 0 < y) (h : log x ≤ n * log y) : x ≤ y ^ n := rpow_intCast _ _ ▸ rpow_le_of_le_log hy h lemma rpow_lt_iff_lt_log (hx : 0 < x) (hy : 0 < y) : x ^ z < y ↔ z * log x < log y := by rw [← log_lt_log_iff (rpow_pos_of_pos hx _) hy, log_rpow hx] lemma pow_lt_iff_lt_log (hx : 0 < x) (hy : 0 < y) : x ^ n < y ↔ n * log x < log y := by rw [← rpow_lt_iff_lt_log hx hy, rpow_natCast] lemma zpow_lt_iff_lt_log {n : ℤ} (hx : 0 < x) (hy : 0 < y) : x ^ n < y ↔ n * log x < log y := by rw [← rpow_lt_iff_lt_log hx hy, rpow_intCast] lemma lt_log_of_rpow_lt (hx : 0 < x) (h : x ^ z < y) : z * log x < log y := log_rpow hx _ ▸ log_lt_log (by positivity) h lemma lt_log_of_pow_lt (hx : 0 < x) (h : x ^ n < y) : n * log x < log y := lt_log_of_rpow_lt hx (rpow_natCast _ _ ▸ h) lemma lt_log_of_zpow_lt {n : ℤ} (hx : 0 < x) (h : x ^ n < y) : n * log x < log y := lt_log_of_rpow_lt hx (rpow_intCast _ _ ▸ h) lemma rpow_lt_of_lt_log (hy : 0 < y) (h : log x < z * log y) : x < y ^ z := by obtain hx | hx := le_or_lt x 0 · exact hx.trans_lt (rpow_pos_of_pos hy _) · exact (lt_rpow_iff_log_lt hx hy).2 h lemma pow_lt_of_lt_log (hy : 0 < y) (h : log x < n * log y) : x < y ^ n := rpow_natCast _ _ ▸ rpow_lt_of_lt_log hy h lemma zpow_lt_of_lt_log {n : ℤ} (hy : 0 < y) (h : log x < n * log y) : x < y ^ n := rpow_intCast _ _ ▸ rpow_lt_of_lt_log hy h theorem rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx.le]
/-- Bound for `|log x * x ^ t|` in the interval `(0, 1]`, for positive real `t`. -/ theorem abs_log_mul_self_rpow_lt (x t : ℝ) (h1 : 0 < x) (h2 : x ≤ 1) (ht : 0 < t) : |log x * x ^ t| < 1 / t := by rw [lt_div_iff₀ ht] have := abs_log_mul_self_lt (x ^ t) (rpow_pos_of_pos h1 t) (rpow_le_one h1.le h2 ht.le) rwa [log_rpow h1, mul_assoc, abs_mul, abs_of_pos ht, mul_comm] at this
Mathlib/Analysis/SpecialFunctions/Pow/Real.lean
869
875
/- Copyright (c) 2022 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Yury Kudryashov, Kevin H. Wilson, Heather Macbeth -/ import Mathlib.Order.Filter.Tendsto /-! # Product and coproduct filters In this file we define `Filter.prod f g` (notation: `f ×ˢ g`) and `Filter.coprod f g`. The product of two filters is the largest filter `l` such that `Filter.Tendsto Prod.fst l f` and `Filter.Tendsto Prod.snd l g`. ## Implementation details The product filter cannot be defined using the monad structure on filters. For example: ```lean F := do {x ← seq, y ← top, return (x, y)} G := do {y ← top, x ← seq, return (x, y)} ``` hence: ```lean s ∈ F ↔ ∃ n, [n..∞] × univ ⊆ s s ∈ G ↔ ∀ i:ℕ, ∃ n, [n..∞] × {i} ⊆ s ``` Now `⋃ i, [i..∞] × {i}` is in `G` but not in `F`. As product filter we want to have `F` as result. ## Notations * `f ×ˢ g` : `Filter.prod f g`, localized in `Filter`. -/ open Set open Filter namespace Filter variable {α β γ δ : Type*} {ι : Sort*} section Prod variable {s : Set α} {t : Set β} {f : Filter α} {g : Filter β} theorem prod_mem_prod (hs : s ∈ f) (ht : t ∈ g) : s ×ˢ t ∈ f ×ˢ g := inter_mem_inf (preimage_mem_comap hs) (preimage_mem_comap ht) theorem mem_prod_iff {s : Set (α × β)} {f : Filter α} {g : Filter β} : s ∈ f ×ˢ g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ×ˢ t₂ ⊆ s := by constructor · rintro ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, rfl⟩ exact ⟨s₁, hs₁, s₂, hs₂, fun p ⟨h, h'⟩ => ⟨hts₁ h, hts₂ h'⟩⟩ · rintro ⟨t₁, ht₁, t₂, ht₂, h⟩ exact mem_inf_of_inter (preimage_mem_comap ht₁) (preimage_mem_comap ht₂) h @[simp] theorem compl_diagonal_mem_prod {l₁ l₂ : Filter α} : (diagonal α)ᶜ ∈ l₁ ×ˢ l₂ ↔ Disjoint l₁ l₂ := by simp only [mem_prod_iff, Filter.disjoint_iff, prod_subset_compl_diagonal_iff_disjoint] @[simp] theorem prod_mem_prod_iff [f.NeBot] [g.NeBot] : s ×ˢ t ∈ f ×ˢ g ↔ s ∈ f ∧ t ∈ g := ⟨fun h => let ⟨_s', hs', _t', ht', H⟩ := mem_prod_iff.1 h (prod_subset_prod_iff.1 H).elim (fun ⟨hs's, ht't⟩ => ⟨mem_of_superset hs' hs's, mem_of_superset ht' ht't⟩) fun h => h.elim (fun hs'e => absurd hs'e (nonempty_of_mem hs').ne_empty) fun ht'e => absurd ht'e (nonempty_of_mem ht').ne_empty, fun h => prod_mem_prod h.1 h.2⟩ theorem mem_prod_principal {s : Set (α × β)} : s ∈ f ×ˢ 𝓟 t ↔ { a | ∀ b ∈ t, (a, b) ∈ s } ∈ f := by rw [← @exists_mem_subset_iff _ f, mem_prod_iff] refine exists_congr fun u => Iff.rfl.and ⟨?_, fun h => ⟨t, mem_principal_self t, ?_⟩⟩ · rintro ⟨v, v_in, hv⟩ a a_in b b_in exact hv (mk_mem_prod a_in <| v_in b_in) · rintro ⟨x, y⟩ ⟨hx, hy⟩ exact h hx y hy theorem mem_prod_top {s : Set (α × β)} : s ∈ f ×ˢ (⊤ : Filter β) ↔ { a | ∀ b, (a, b) ∈ s } ∈ f := by rw [← principal_univ, mem_prod_principal] simp only [mem_univ, forall_true_left] theorem eventually_prod_principal_iff {p : α × β → Prop} {s : Set β} : (∀ᶠ x : α × β in f ×ˢ 𝓟 s, p x) ↔ ∀ᶠ x : α in f, ∀ y : β, y ∈ s → p (x, y) := by rw [eventually_iff, eventually_iff, mem_prod_principal] simp only [mem_setOf_eq] theorem comap_prod (f : α → β × γ) (b : Filter β) (c : Filter γ) : comap f (b ×ˢ c) = comap (Prod.fst ∘ f) b ⊓ comap (Prod.snd ∘ f) c := by rw [prod_eq_inf, comap_inf, Filter.comap_comap, Filter.comap_comap] theorem comap_prodMap_prod (f : α → β) (g : γ → δ) (lb : Filter β) (ld : Filter δ) : comap (Prod.map f g) (lb ×ˢ ld) = comap f lb ×ˢ comap g ld := by simp [prod_eq_inf, comap_comap, Function.comp_def] theorem prod_top : f ×ˢ (⊤ : Filter β) = f.comap Prod.fst := by rw [prod_eq_inf, comap_top, inf_top_eq] theorem top_prod : (⊤ : Filter α) ×ˢ g = g.comap Prod.snd := by rw [prod_eq_inf, comap_top, top_inf_eq] theorem sup_prod (f₁ f₂ : Filter α) (g : Filter β) : (f₁ ⊔ f₂) ×ˢ g = (f₁ ×ˢ g) ⊔ (f₂ ×ˢ g) := by simp only [prod_eq_inf, comap_sup, inf_sup_right] theorem prod_sup (f : Filter α) (g₁ g₂ : Filter β) : f ×ˢ (g₁ ⊔ g₂) = (f ×ˢ g₁) ⊔ (f ×ˢ g₂) := by simp only [prod_eq_inf, comap_sup, inf_sup_left] theorem eventually_prod_iff {p : α × β → Prop} : (∀ᶠ x in f ×ˢ g, p x) ↔ ∃ pa : α → Prop, (∀ᶠ x in f, pa x) ∧ ∃ pb : β → Prop, (∀ᶠ y in g, pb y) ∧ ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by
simpa only [Set.prod_subset_iff] using @mem_prod_iff α β p f g theorem tendsto_fst : Tendsto Prod.fst (f ×ˢ g) f :=
Mathlib/Order/Filter/Prod.lean
117
119
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import Batteries.Tactic.Congr import Mathlib.Data.Option.Basic import Mathlib.Data.Prod.Basic import Mathlib.Data.Set.Subsingleton import Mathlib.Data.Set.SymmDiff import Mathlib.Data.Set.Inclusion /-! # Images and preimages of sets ## Main definitions * `preimage f t : Set α` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of β. * `range f : Set β` : the image of `univ` under `f`. Also works for `{p : Prop} (f : p → α)` (unlike `image`) ## Notation * `f ⁻¹' t` for `Set.preimage f t` * `f '' s` for `Set.image f s` ## Tags set, sets, image, preimage, pre-image, range -/ assert_not_exists WithTop OrderIso universe u v open Function Set namespace Set variable {α β γ : Type*} {ι : Sort*} /-! ### Inverse image -/ section Preimage variable {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl theorem preimage_congr {f g : α → β} {s : Set β} (h : ∀ x : α, f x = g x) : f ⁻¹' s = g ⁻¹' s := by congr with x simp [h] @[gcongr] theorem preimage_mono {s t : Set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := fun _ hx => h hx @[simp, mfld_simps] theorem preimage_univ : f ⁻¹' univ = univ := rfl theorem subset_preimage_univ {s : Set α} : s ⊆ f ⁻¹' univ := subset_univ _ @[simp, mfld_simps] theorem preimage_inter {s t : Set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : Set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : Set β} : f ⁻¹' sᶜ = (f ⁻¹' s)ᶜ := rfl @[simp] theorem preimage_diff (f : α → β) (s t : Set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl open scoped symmDiff in @[simp] lemma preimage_symmDiff {f : α → β} (s t : Set β) : f ⁻¹' (s ∆ t) = (f ⁻¹' s) ∆ (f ⁻¹' t) := rfl @[simp] theorem preimage_ite (f : α → β) (s t₁ t₂ : Set β) : f ⁻¹' s.ite t₁ t₂ = (f ⁻¹' s).ite (f ⁻¹' t₁) (f ⁻¹' t₂) := rfl @[simp] theorem preimage_setOf_eq {p : α → Prop} {f : β → α} : f ⁻¹' { a | p a } = { a | p (f a) } := rfl @[simp] theorem preimage_id_eq : preimage (id : α → α) = id := rfl @[mfld_simps] theorem preimage_id {s : Set α} : id ⁻¹' s = s := rfl @[simp, mfld_simps] theorem preimage_id' {s : Set α} : (fun x => x) ⁻¹' s = s := rfl @[simp] theorem preimage_const_of_mem {b : β} {s : Set β} (h : b ∈ s) : (fun _ : α => b) ⁻¹' s = univ := eq_univ_of_forall fun _ => h @[simp] theorem preimage_const_of_not_mem {b : β} {s : Set β} (h : b ∉ s) : (fun _ : α => b) ⁻¹' s = ∅ := eq_empty_of_subset_empty fun _ hx => h hx theorem preimage_const (b : β) (s : Set β) [Decidable (b ∈ s)] : (fun _ : α => b) ⁻¹' s = if b ∈ s then univ else ∅ := by split_ifs with hb exacts [preimage_const_of_mem hb, preimage_const_of_not_mem hb] /-- If preimage of each singleton under `f : α → β` is either empty or the whole type, then `f` is a constant. -/ lemma exists_eq_const_of_preimage_singleton [Nonempty β] {f : α → β} (hf : ∀ b : β, f ⁻¹' {b} = ∅ ∨ f ⁻¹' {b} = univ) : ∃ b, f = const α b := by rcases em (∃ b, f ⁻¹' {b} = univ) with ⟨b, hb⟩ | hf' · exact ⟨b, funext fun x ↦ eq_univ_iff_forall.1 hb x⟩ · have : ∀ x b, f x ≠ b := fun x b ↦ eq_empty_iff_forall_not_mem.1 ((hf b).resolve_right fun h ↦ hf' ⟨b, h⟩) x exact ⟨Classical.arbitrary β, funext fun x ↦ absurd rfl (this x _)⟩ theorem preimage_comp {s : Set γ} : g ∘ f ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl theorem preimage_comp_eq : preimage (g ∘ f) = preimage f ∘ preimage g := rfl theorem preimage_iterate_eq {f : α → α} {n : ℕ} : Set.preimage f^[n] = (Set.preimage f)^[n] := by induction n with | zero => simp | succ n ih => rw [iterate_succ, iterate_succ', preimage_comp_eq, ih] theorem preimage_preimage {g : β → γ} {f : α → β} {s : Set γ} : f ⁻¹' (g ⁻¹' s) = (fun x => g (f x)) ⁻¹' s := preimage_comp.symm theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : Set (Subtype p)} {t : Set α} : s = Subtype.val ⁻¹' t ↔ ∀ (x) (h : p x), (⟨x, h⟩ : Subtype p) ∈ s ↔ x ∈ t := ⟨fun s_eq x h => by rw [s_eq] simp, fun h => ext fun ⟨x, hx⟩ => by simp [h]⟩ theorem nonempty_of_nonempty_preimage {s : Set β} {f : α → β} (hf : (f ⁻¹' s).Nonempty) : s.Nonempty := let ⟨x, hx⟩ := hf ⟨f x, hx⟩ @[simp] theorem preimage_singleton_true (p : α → Prop) : p ⁻¹' {True} = {a | p a} := by ext; simp @[simp] theorem preimage_singleton_false (p : α → Prop) : p ⁻¹' {False} = {a | ¬p a} := by ext; simp theorem preimage_subtype_coe_eq_compl {s u v : Set α} (hsuv : s ⊆ u ∪ v) (H : s ∩ (u ∩ v) = ∅) : ((↑) : s → α) ⁻¹' u = ((↑) ⁻¹' v)ᶜ := by ext ⟨x, x_in_s⟩ constructor · intro x_in_u x_in_v exact eq_empty_iff_forall_not_mem.mp H x ⟨x_in_s, ⟨x_in_u, x_in_v⟩⟩ · intro hx exact Or.elim (hsuv x_in_s) id fun hx' => hx.elim hx' lemma preimage_subset {s t} (hs : s ⊆ f '' t) (hf : Set.InjOn f (f ⁻¹' s)) : f ⁻¹' s ⊆ t := by rintro a ha obtain ⟨b, hb, hba⟩ := hs ha rwa [hf ha _ hba.symm] simpa [hba] end Preimage /-! ### Image of a set under a function -/ section Image variable {f : α → β} {s t : Set α} theorem image_eta (f : α → β) : f '' s = (fun x => f x) '' s := rfl theorem _root_.Function.Injective.mem_set_image {f : α → β} (hf : Injective f) {s : Set α} {a : α} : f a ∈ f '' s ↔ a ∈ s := ⟨fun ⟨_, hb, Eq⟩ => hf Eq ▸ hb, mem_image_of_mem f⟩ lemma preimage_subset_of_surjOn {t : Set β} (hf : Injective f) (h : SurjOn f s t) : f ⁻¹' t ⊆ s := fun _ hx ↦ hf.mem_set_image.1 <| h hx theorem forall_mem_image {f : α → β} {s : Set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ ∀ ⦃x⦄, x ∈ s → p (f x) := by simp theorem exists_mem_image {f : α → β} {s : Set α} {p : β → Prop} : (∃ y ∈ f '' s, p y) ↔ ∃ x ∈ s, p (f x) := by simp @[congr] theorem image_congr {f g : α → β} {s : Set α} (h : ∀ a ∈ s, f a = g a) : f '' s = g '' s := by aesop /-- A common special case of `image_congr` -/ theorem image_congr' {f g : α → β} {s : Set α} (h : ∀ x : α, f x = g x) : f '' s = g '' s := image_congr fun x _ => h x @[gcongr] lemma image_mono (h : s ⊆ t) : f '' s ⊆ f '' t := by rintro - ⟨a, ha, rfl⟩; exact mem_image_of_mem f (h ha) theorem image_comp (f : β → γ) (g : α → β) (a : Set α) : f ∘ g '' a = f '' (g '' a) := by aesop theorem image_comp_eq {g : β → γ} : image (g ∘ f) = image g ∘ image f := by ext; simp /-- A variant of `image_comp`, useful for rewriting -/ theorem image_image (g : β → γ) (f : α → β) (s : Set α) : g '' (f '' s) = (fun x => g (f x)) '' s := (image_comp g f s).symm theorem image_comm {β'} {f : β → γ} {g : α → β} {f' : α → β'} {g' : β' → γ} (h_comm : ∀ a, f (g a) = g' (f' a)) : (s.image g).image f = (s.image f').image g' := by simp_rw [image_image, h_comm] theorem _root_.Function.Semiconj.set_image {f : α → β} {ga : α → α} {gb : β → β} (h : Function.Semiconj f ga gb) : Function.Semiconj (image f) (image ga) (image gb) := fun _ => image_comm h theorem _root_.Function.Commute.set_image {f g : α → α} (h : Function.Commute f g) : Function.Commute (image f) (image g) := Function.Semiconj.set_image h /-- Image is monotone with respect to `⊆`. See `Set.monotone_image` for the statement in terms of `≤`. -/ @[gcongr] theorem image_subset {a b : Set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by simp only [subset_def, mem_image] exact fun x => fun ⟨w, h1, h2⟩ => ⟨w, h h1, h2⟩ /-- `Set.image` is monotone. See `Set.image_subset` for the statement in terms of `⊆`. -/ lemma monotone_image {f : α → β} : Monotone (image f) := fun _ _ => image_subset _ theorem image_union (f : α → β) (s t : Set α) : f '' (s ∪ t) = f '' s ∪ f '' t := ext fun x => ⟨by rintro ⟨a, h | h, rfl⟩ <;> [left; right] <;> exact ⟨_, h, rfl⟩, by rintro (⟨a, h, rfl⟩ | ⟨a, h, rfl⟩) <;> refine ⟨_, ?_, rfl⟩ · exact mem_union_left t h · exact mem_union_right s h⟩ @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := by ext simp theorem image_inter_subset (f : α → β) (s t : Set α) : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := subset_inter (image_subset _ inter_subset_left) (image_subset _ inter_subset_right) theorem image_inter_on {f : α → β} {s t : Set α} (h : ∀ x ∈ t, ∀ y ∈ s, f x = f y → x = y) : f '' (s ∩ t) = f '' s ∩ f '' t := (image_inter_subset _ _ _).antisymm fun b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩ ↦ have : a₂ = a₁ := h _ ha₂ _ ha₁ (by simp [*]) ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩ theorem image_inter {f : α → β} {s t : Set α} (H : Injective f) : f '' (s ∩ t) = f '' s ∩ f '' t := image_inter_on fun _ _ _ _ h => H h theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : Surjective f) : f '' univ = univ := eq_univ_of_forall <| by simpa [image] @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := by ext simp [image, eq_comm] @[simp] theorem Nonempty.image_const {s : Set α} (hs : s.Nonempty) (a : β) : (fun _ => a) '' s = {a} := ext fun _ => ⟨fun ⟨_, _, h⟩ => h ▸ mem_singleton _, fun h => (eq_of_mem_singleton h).symm ▸ hs.imp fun _ hy => ⟨hy, rfl⟩⟩ @[simp, mfld_simps] theorem image_eq_empty {α β} {f : α → β} {s : Set α} : f '' s = ∅ ↔ s = ∅ := by simp only [eq_empty_iff_forall_not_mem] exact ⟨fun H a ha => H _ ⟨_, ha, rfl⟩, fun H b ⟨_, ha, _⟩ => H _ ha⟩ theorem preimage_compl_eq_image_compl [BooleanAlgebra α] (S : Set α) : HasCompl.compl ⁻¹' S = HasCompl.compl '' S := Set.ext fun x => ⟨fun h => ⟨xᶜ, h, compl_compl x⟩, fun h => Exists.elim h fun _ hy => (compl_eq_comm.mp hy.2).symm.subst hy.1⟩ theorem mem_compl_image [BooleanAlgebra α] (t : α) (S : Set α) : t ∈ HasCompl.compl '' S ↔ tᶜ ∈ S := by simp [← preimage_compl_eq_image_compl] @[simp] theorem image_id_eq : image (id : α → α) = id := by ext; simp /-- A variant of `image_id` -/ @[simp] theorem image_id' (s : Set α) : (fun x => x) '' s = s := by ext simp theorem image_id (s : Set α) : id '' s = s := by simp lemma image_iterate_eq {f : α → α} {n : ℕ} : image (f^[n]) = (image f)^[n] := by induction n with | zero => simp | succ n ih => rw [iterate_succ', iterate_succ', ← ih, image_comp_eq] theorem compl_compl_image [BooleanAlgebra α] (S : Set α) : HasCompl.compl '' (HasCompl.compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : Set α} : f '' insert a s = insert (f a) (f '' s) := by ext simp [and_or_left, exists_or, eq_comm, or_comm, and_comm] theorem image_pair (f : α → β) (a b : α) : f '' {a, b} = {f a, f b} := by simp only [image_insert_eq, image_singleton] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : LeftInverse g f) (s : Set α) : f '' s ⊆ g ⁻¹' s := fun _ ⟨a, h, e⟩ => e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : LeftInverse g f) (s : Set β) : f ⁻¹' s ⊆ g '' s := fun b h => ⟨f b, h, I b⟩ theorem range_inter_ssubset_iff_preimage_ssubset {f : α → β} {S S' : Set β} : range f ∩ S ⊂ range f ∩ S' ↔ f ⁻¹' S ⊂ f ⁻¹' S' := by simp only [Set.ssubset_iff_exists] apply and_congr ?_ (by aesop) constructor all_goals intro r x hx simp_all only [subset_inter_iff, inter_subset_left, true_and, mem_preimage, mem_inter_iff, mem_range, true_and] aesop theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : LeftInverse g f) (h₂ : RightInverse g f) : image f = preimage g := funext fun s => Subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : Set α} (h₁ : LeftInverse g f) (h₂ : RightInverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw [image_eq_preimage_of_inverse h₁ h₂]; rfl theorem image_compl_subset {f : α → β} {s : Set α} (H : Injective f) : f '' sᶜ ⊆ (f '' s)ᶜ := Disjoint.subset_compl_left <| by simp [disjoint_iff_inf_le, ← image_inter H] theorem subset_image_compl {f : α → β} {s : Set α} (H : Surjective f) : (f '' s)ᶜ ⊆ f '' sᶜ := compl_subset_iff_union.2 <| by rw [← image_union] simp [image_univ_of_surjective H] theorem image_compl_eq {f : α → β} {s : Set α} (H : Bijective f) : f '' sᶜ = (f '' s)ᶜ := Subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) theorem subset_image_diff (f : α → β) (s t : Set α) : f '' s \ f '' t ⊆ f '' (s \ t) := by rw [diff_subset_iff, ← image_union, union_diff_self] exact image_subset f subset_union_right open scoped symmDiff in theorem subset_image_symmDiff : (f '' s) ∆ (f '' t) ⊆ f '' s ∆ t := (union_subset_union (subset_image_diff _ _ _) <| subset_image_diff _ _ _).trans (superset_of_eq (image_union _ _ _)) theorem image_diff {f : α → β} (hf : Injective f) (s t : Set α) : f '' (s \ t) = f '' s \ f '' t := Subset.antisymm (Subset.trans (image_inter_subset _ _ _) <| inter_subset_inter_right _ <| image_compl_subset hf) (subset_image_diff f s t) open scoped symmDiff in theorem image_symmDiff (hf : Injective f) (s t : Set α) : f '' s ∆ t = (f '' s) ∆ (f '' t) := by simp_rw [Set.symmDiff_def, image_union, image_diff hf] theorem Nonempty.image (f : α → β) {s : Set α} : s.Nonempty → (f '' s).Nonempty | ⟨x, hx⟩ => ⟨f x, mem_image_of_mem f hx⟩ theorem Nonempty.of_image {f : α → β} {s : Set α} : (f '' s).Nonempty → s.Nonempty | ⟨_, x, hx, _⟩ => ⟨x, hx⟩ @[simp] theorem image_nonempty {f : α → β} {s : Set α} : (f '' s).Nonempty ↔ s.Nonempty := ⟨Nonempty.of_image, fun h => h.image f⟩ theorem Nonempty.preimage {s : Set β} (hs : s.Nonempty) {f : α → β} (hf : Surjective f) : (f ⁻¹' s).Nonempty := let ⟨y, hy⟩ := hs let ⟨x, hx⟩ := hf y ⟨x, mem_preimage.2 <| hx.symm ▸ hy⟩ instance (f : α → β) (s : Set α) [Nonempty s] : Nonempty (f '' s) := (Set.Nonempty.image f .of_subtype).to_subtype /-- image and preimage are a Galois connection -/ @[simp] theorem image_subset_iff {s : Set α} {t : Set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := forall_mem_image theorem image_preimage_subset (f : α → β) (s : Set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 Subset.rfl theorem subset_preimage_image (f : α → β) (s : Set α) : s ⊆ f ⁻¹' (f '' s) := fun _ => mem_image_of_mem f theorem preimage_image_univ {f : α → β} : f ⁻¹' (f '' univ) = univ := Subset.antisymm (fun _ _ => trivial) (subset_preimage_image f univ) @[simp] theorem preimage_image_eq {f : α → β} (s : Set α) (h : Injective f) : f ⁻¹' (f '' s) = s := Subset.antisymm (fun _ ⟨_, hy, e⟩ => h e ▸ hy) (subset_preimage_image f s) @[simp] theorem image_preimage_eq {f : α → β} (s : Set β) (h : Surjective f) : f '' (f ⁻¹' s) = s := Subset.antisymm (image_preimage_subset f s) fun x hx => let ⟨y, e⟩ := h x ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩ @[simp] theorem Nonempty.subset_preimage_const {s : Set α} (hs : Set.Nonempty s) (t : Set β) (a : β) : s ⊆ (fun _ => a) ⁻¹' t ↔ a ∈ t := by rw [← image_subset_iff, hs.image_const, singleton_subset_iff] -- Note defeq abuse identifying `preimage` with function composition in the following two proofs. @[simp] theorem preimage_injective : Injective (preimage f) ↔ Surjective f := injective_comp_right_iff_surjective @[simp] theorem preimage_surjective : Surjective (preimage f) ↔ Injective f := surjective_comp_right_iff_injective @[simp] theorem preimage_eq_preimage {f : β → α} (hf : Surjective f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := (preimage_injective.mpr hf).eq_iff theorem image_inter_preimage (f : α → β) (s : Set α) (t : Set β) : f '' (s ∩ f ⁻¹' t) = f '' s ∩ t := by apply Subset.antisymm · calc f '' (s ∩ f ⁻¹' t) ⊆ f '' s ∩ f '' (f ⁻¹' t) := image_inter_subset _ _ _ _ ⊆ f '' s ∩ t := inter_subset_inter_right _ (image_preimage_subset f t) · rintro _ ⟨⟨x, h', rfl⟩, h⟩ exact ⟨x, ⟨h', h⟩, rfl⟩ theorem image_preimage_inter (f : α → β) (s : Set α) (t : Set β) : f '' (f ⁻¹' t ∩ s) = t ∩ f '' s := by simp only [inter_comm, image_inter_preimage] @[simp] theorem image_inter_nonempty_iff {f : α → β} {s : Set α} {t : Set β} : (f '' s ∩ t).Nonempty ↔ (s ∩ f ⁻¹' t).Nonempty := by rw [← image_inter_preimage, image_nonempty] theorem image_diff_preimage {f : α → β} {s : Set α} {t : Set β} : f '' (s \ f ⁻¹' t) = f '' s \ t := by simp_rw [diff_eq, ← preimage_compl, image_inter_preimage] theorem compl_image : image (compl : Set α → Set α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {p : Set α → Prop} : compl '' { s | p s } = { s | p sᶜ } := congr_fun compl_image p theorem inter_preimage_subset (s : Set α) (t : Set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := fun _ h => ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : Set α) (t : Set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := fun _ h => Or.elim h (fun l => Or.inl <| mem_image_of_mem _ l) fun r => Or.inr r theorem subset_image_union (f : α → β) (s : Set α) (t : Set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) theorem preimage_subset_iff {A : Set α} {B : Set β} {f : α → β} : f ⁻¹' B ⊆ A ↔ ∀ a : α, f a ∈ B → a ∈ A := Iff.rfl theorem image_eq_image {f : α → β} (hf : Injective f) : f '' s = f '' t ↔ s = t := Iff.symm <| (Iff.intro fun eq => eq ▸ rfl) fun eq => by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] theorem subset_image_iff {t : Set β} : t ⊆ f '' s ↔ ∃ u, u ⊆ s ∧ f '' u = t := by refine ⟨fun h ↦ ⟨f ⁻¹' t ∩ s, inter_subset_right, ?_⟩, fun ⟨u, hu, hu'⟩ ↦ hu'.symm ▸ image_mono hu⟩ rwa [image_preimage_inter, inter_eq_left] @[simp] lemma exists_subset_image_iff {p : Set β → Prop} : (∃ t ⊆ f '' s, p t) ↔ ∃ t ⊆ s, p (f '' t) := by simp [subset_image_iff] @[simp] lemma forall_subset_image_iff {p : Set β → Prop} : (∀ t ⊆ f '' s, p t) ↔ ∀ t ⊆ s, p (f '' t) := by simp [subset_image_iff] theorem image_subset_image_iff {f : α → β} (hf : Injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := by refine Iff.symm <| (Iff.intro (image_subset f)) fun h => ?_ rw [← preimage_image_eq s hf, ← preimage_image_eq t hf] exact preimage_mono h theorem prod_quotient_preimage_eq_image [s : Setoid α] (g : Quotient s → β) {h : α → β} (Hh : h = g ∘ Quotient.mk'') (r : Set (β × β)) : { x : Quotient s × Quotient s | (g x.1, g x.2) ∈ r } = (fun a : α × α => (⟦a.1⟧, ⟦a.2⟧)) '' ((fun a : α × α => (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ Set.ext fun ⟨a₁, a₂⟩ => ⟨Quot.induction_on₂ a₁ a₂ fun a₁ a₂ h => ⟨(a₁, a₂), h, rfl⟩, fun ⟨⟨b₁, b₂⟩, h₁, h₂⟩ => show (g a₁, g a₂) ∈ r from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := Prod.ext_iff.1 h₂ h₃.1 ▸ h₃.2 ▸ h₁⟩ theorem exists_image_iff (f : α → β) (x : Set α) (P : β → Prop) : (∃ a : f '' x, P a) ↔ ∃ a : x, P (f a) := ⟨fun ⟨a, h⟩ => ⟨⟨_, a.prop.choose_spec.1⟩, a.prop.choose_spec.2.symm ▸ h⟩, fun ⟨a, h⟩ => ⟨⟨_, _, a.prop, rfl⟩, h⟩⟩ theorem imageFactorization_eq {f : α → β} {s : Set α} : Subtype.val ∘ imageFactorization f s = f ∘ Subtype.val := funext fun _ => rfl theorem surjective_onto_image {f : α → β} {s : Set α} : Surjective (imageFactorization f s) := fun ⟨_, ⟨a, ha, rfl⟩⟩ => ⟨⟨a, ha⟩, rfl⟩ /-- If the only elements outside `s` are those left fixed by `σ`, then mapping by `σ` has no effect. -/ theorem image_perm {s : Set α} {σ : Equiv.Perm α} (hs : { a : α | σ a ≠ a } ⊆ s) : σ '' s = s := by ext i obtain hi | hi := eq_or_ne (σ i) i · refine ⟨?_, fun h => ⟨i, h, hi⟩⟩ rintro ⟨j, hj, h⟩ rwa [σ.injective (hi.trans h.symm)] · refine iff_of_true ⟨σ.symm i, hs fun h => hi ?_, σ.apply_symm_apply _⟩ (hs hi) convert congr_arg σ h <;> exact (σ.apply_symm_apply _).symm end Image /-! ### Lemmas about the powerset and image. -/ /-- The powerset of `{a} ∪ s` is `𝒫 s` together with `{a} ∪ t` for each `t ∈ 𝒫 s`. -/ theorem powerset_insert (s : Set α) (a : α) : 𝒫 insert a s = 𝒫 s ∪ insert a '' 𝒫 s := by ext t simp_rw [mem_union, mem_image, mem_powerset_iff] constructor · intro h by_cases hs : a ∈ t · right refine ⟨t \ {a}, ?_, ?_⟩ · rw [diff_singleton_subset_iff] assumption · rw [insert_diff_singleton, insert_eq_of_mem hs] · left exact (subset_insert_iff_of_not_mem hs).mp h · rintro (h | ⟨s', h₁, rfl⟩) · exact subset_trans h (subset_insert a s) · exact insert_subset_insert h₁ /-! ### Lemmas about range of a function. -/ section Range variable {f : ι → α} {s t : Set α} theorem forall_mem_range {p : α → Prop} : (∀ a ∈ range f, p a) ↔ ∀ i, p (f i) := by simp theorem forall_subtype_range_iff {p : range f → Prop} : (∀ a : range f, p a) ↔ ∀ i, p ⟨f i, mem_range_self _⟩ := ⟨fun H _ => H _, fun H ⟨y, i, hi⟩ => by subst hi apply H⟩ theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ ∃ i, p (f i) := by simp theorem exists_subtype_range_iff {p : range f → Prop} : (∃ a : range f, p a) ↔ ∃ i, p ⟨f i, mem_range_self _⟩ := ⟨fun ⟨⟨a, i, hi⟩, ha⟩ => by subst a exact ⟨i, ha⟩, fun ⟨_, hi⟩ => ⟨_, hi⟩⟩ theorem range_eq_univ : range f = univ ↔ Surjective f := eq_univ_iff_forall @[deprecated (since := "2024-11-11")] alias range_iff_surjective := range_eq_univ alias ⟨_, _root_.Function.Surjective.range_eq⟩ := range_eq_univ @[simp] theorem subset_range_of_surjective {f : α → β} (h : Surjective f) (s : Set β) : s ⊆ range f := Surjective.range_eq h ▸ subset_univ s @[simp] theorem image_univ {f : α → β} : f '' univ = range f := by ext simp [image, range] lemma image_compl_eq_range_diff_image {f : α → β} (hf : Injective f) (s : Set α) : f '' sᶜ = range f \ f '' s := by rw [← image_univ, ← image_diff hf, compl_eq_univ_diff] /-- Alias of `Set.image_compl_eq_range_sdiff_image`. -/ lemma range_diff_image {f : α → β} (hf : Injective f) (s : Set α) : range f \ f '' s = f '' sᶜ := by rw [image_compl_eq_range_diff_image hf] @[simp] theorem preimage_eq_univ_iff {f : α → β} {s} : f ⁻¹' s = univ ↔ range f ⊆ s := by rw [← univ_subset_iff, ← image_subset_iff, image_univ] theorem image_subset_range (f : α → β) (s) : f '' s ⊆ range f := by rw [← image_univ]; exact image_subset _ (subset_univ _) theorem mem_range_of_mem_image (f : α → β) (s) {x : β} (h : x ∈ f '' s) : x ∈ range f := image_subset_range f s h theorem _root_.Nat.mem_range_succ (i : ℕ) : i ∈ range Nat.succ ↔ 0 < i := ⟨by rintro ⟨n, rfl⟩ exact Nat.succ_pos n, fun h => ⟨_, Nat.succ_pred_eq_of_pos h⟩⟩ theorem Nonempty.preimage' {s : Set β} (hs : s.Nonempty) {f : α → β} (hf : s ⊆ range f) : (f ⁻¹' s).Nonempty := let ⟨_, hy⟩ := hs let ⟨x, hx⟩ := hf hy ⟨x, Set.mem_preimage.2 <| hx.symm ▸ hy⟩ theorem range_comp (g : α → β) (f : ι → α) : range (g ∘ f) = g '' range f := by aesop /-- Variant of `range_comp` using a lambda instead of function composition. -/ theorem range_comp' (g : α → β) (f : ι → α) : range (fun x => g (f x)) = g '' range f := range_comp g f theorem range_subset_iff : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_mem_range theorem range_subset_range_iff_exists_comp {f : α → γ} {g : β → γ} : range f ⊆ range g ↔ ∃ h : α → β, f = g ∘ h := by simp only [range_subset_iff, mem_range, Classical.skolem, funext_iff, (· ∘ ·), eq_comm] theorem range_eq_iff (f : α → β) (s : Set β) : range f = s ↔ (∀ a, f a ∈ s) ∧ ∀ b ∈ s, ∃ a, f a = b := by rw [← range_subset_iff] exact le_antisymm_iff theorem range_comp_subset_range (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g := by rw [range_comp]; apply image_subset_range theorem range_nonempty_iff_nonempty : (range f).Nonempty ↔ Nonempty ι := ⟨fun ⟨_, x, _⟩ => ⟨x⟩, fun ⟨x⟩ => ⟨f x, mem_range_self x⟩⟩ theorem range_nonempty [h : Nonempty ι] (f : ι → α) : (range f).Nonempty := range_nonempty_iff_nonempty.2 h @[simp] theorem range_eq_empty_iff {f : ι → α} : range f = ∅ ↔ IsEmpty ι := by rw [← not_nonempty_iff, ← range_nonempty_iff_nonempty, not_nonempty_iff_eq_empty] theorem range_eq_empty [IsEmpty ι] (f : ι → α) : range f = ∅ := range_eq_empty_iff.2 ‹_› instance instNonemptyRange [Nonempty ι] (f : ι → α) : Nonempty (range f) := (range_nonempty f).to_subtype @[simp] theorem image_union_image_compl_eq_range (f : α → β) : f '' s ∪ f '' sᶜ = range f := by rw [← image_union, ← image_univ, ← union_compl_self] theorem insert_image_compl_eq_range (f : α → β) (x : α) : insert (f x) (f '' {x}ᶜ) = range f := by rw [← image_insert_eq, insert_eq, union_compl_self, image_univ] theorem image_preimage_eq_range_inter {f : α → β} {t : Set β} : f '' (f ⁻¹' t) = range f ∩ t := ext fun x => ⟨fun ⟨_, hx, HEq⟩ => HEq ▸ ⟨mem_range_self _, hx⟩, fun ⟨⟨y, h_eq⟩, hx⟩ => h_eq ▸ mem_image_of_mem f <| show y ∈ f ⁻¹' t by rw [preimage, mem_setOf, h_eq]; exact hx⟩ theorem image_preimage_eq_inter_range {f : α → β} {t : Set β} : f '' (f ⁻¹' t) = t ∩ range f := by rw [image_preimage_eq_range_inter, inter_comm] theorem image_preimage_eq_of_subset {f : α → β} {s : Set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_range_inter, inter_eq_self_of_subset_right hs] theorem image_preimage_eq_iff {f : α → β} {s : Set β} : f '' (f ⁻¹' s) = s ↔ s ⊆ range f := ⟨by intro h rw [← h] apply image_subset_range, image_preimage_eq_of_subset⟩ theorem subset_range_iff_exists_image_eq {f : α → β} {s : Set β} : s ⊆ range f ↔ ∃ t, f '' t = s := ⟨fun h => ⟨_, image_preimage_eq_iff.2 h⟩, fun ⟨_, ht⟩ => ht ▸ image_subset_range _ _⟩ theorem range_image (f : α → β) : range (image f) = 𝒫 range f := ext fun _ => subset_range_iff_exists_image_eq.symm @[simp] theorem exists_subset_range_and_iff {f : α → β} {p : Set β → Prop} : (∃ s, s ⊆ range f ∧ p s) ↔ ∃ s, p (f '' s) := by rw [← exists_range_iff, range_image]; rfl @[simp] theorem forall_subset_range_iff {f : α → β} {p : Set β → Prop} : (∀ s, s ⊆ range f → p s) ↔ ∀ s, p (f '' s) := by rw [← forall_mem_range, range_image]; simp only [mem_powerset_iff] @[simp] theorem preimage_subset_preimage_iff {s t : Set α} {f : β → α} (hs : s ⊆ range f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := by constructor · intro h x hx rcases hs hx with ⟨y, rfl⟩ exact h hx intro h x; apply h theorem preimage_eq_preimage' {s t : Set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := by constructor · intro h apply Subset.antisymm · rw [← preimage_subset_preimage_iff hs, h] · rw [← preimage_subset_preimage_iff ht, h] rintro rfl; rfl -- Not `@[simp]` since `simp` can prove this. theorem preimage_inter_range {f : α → β} {s : Set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := Set.ext fun x => and_iff_left ⟨x, rfl⟩ -- Not `@[simp]` since `simp` can prove this. theorem preimage_range_inter {f : α → β} {s : Set β} : f ⁻¹' (range f ∩ s) = f ⁻¹' s := by rw [inter_comm, preimage_inter_range] theorem preimage_image_preimage {f : α → β} {s : Set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_range_inter, preimage_range_inter] @[simp, mfld_simps] theorem range_id : range (@id α) = univ := range_eq_univ.2 surjective_id @[simp, mfld_simps] theorem range_id' : (range fun x : α => x) = univ := range_id @[simp] theorem _root_.Prod.range_fst [Nonempty β] : range (Prod.fst : α × β → α) = univ := Prod.fst_surjective.range_eq @[simp] theorem _root_.Prod.range_snd [Nonempty α] : range (Prod.snd : α × β → β) = univ := Prod.snd_surjective.range_eq @[simp] theorem range_eval {α : ι → Sort _} [∀ i, Nonempty (α i)] (i : ι) : range (eval i : (∀ i, α i) → α i) = univ := (surjective_eval i).range_eq theorem range_inl : range (@Sum.inl α β) = {x | Sum.isLeft x} := by ext (_|_) <;> simp theorem range_inr : range (@Sum.inr α β) = {x | Sum.isRight x} := by ext (_|_) <;> simp theorem isCompl_range_inl_range_inr : IsCompl (range <| @Sum.inl α β) (range Sum.inr) := IsCompl.of_le (by rintro y ⟨⟨x₁, rfl⟩, ⟨x₂, h⟩⟩ exact Sum.noConfusion h) (by rintro (x | y) - <;> [left; right] <;> exact mem_range_self _) @[simp] theorem range_inl_union_range_inr : range (Sum.inl : α → α ⊕ β) ∪ range Sum.inr = univ := isCompl_range_inl_range_inr.sup_eq_top @[simp] theorem range_inl_inter_range_inr : range (Sum.inl : α → α ⊕ β) ∩ range Sum.inr = ∅ := isCompl_range_inl_range_inr.inf_eq_bot @[simp] theorem range_inr_union_range_inl : range (Sum.inr : β → α ⊕ β) ∪ range Sum.inl = univ := isCompl_range_inl_range_inr.symm.sup_eq_top @[simp] theorem range_inr_inter_range_inl : range (Sum.inr : β → α ⊕ β) ∩ range Sum.inl = ∅ := isCompl_range_inl_range_inr.symm.inf_eq_bot @[simp] theorem preimage_inl_image_inr (s : Set β) : Sum.inl ⁻¹' (@Sum.inr α β '' s) = ∅ := by ext simp @[simp] theorem preimage_inr_image_inl (s : Set α) : Sum.inr ⁻¹' (@Sum.inl α β '' s) = ∅ := by ext simp @[simp] theorem preimage_inl_range_inr : Sum.inl ⁻¹' range (Sum.inr : β → α ⊕ β) = ∅ := by rw [← image_univ, preimage_inl_image_inr] @[simp] theorem preimage_inr_range_inl : Sum.inr ⁻¹' range (Sum.inl : α → α ⊕ β) = ∅ := by rw [← image_univ, preimage_inr_image_inl] @[simp] theorem compl_range_inl : (range (Sum.inl : α → α ⊕ β))ᶜ = range (Sum.inr : β → α ⊕ β) := IsCompl.compl_eq isCompl_range_inl_range_inr @[simp] theorem compl_range_inr : (range (Sum.inr : β → α ⊕ β))ᶜ = range (Sum.inl : α → α ⊕ β) := IsCompl.compl_eq isCompl_range_inl_range_inr.symm theorem image_preimage_inl_union_image_preimage_inr (s : Set (α ⊕ β)) : Sum.inl '' (Sum.inl ⁻¹' s) ∪ Sum.inr '' (Sum.inr ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, ← inter_union_distrib_left, range_inl_union_range_inr, inter_univ] @[simp] theorem range_quot_mk (r : α → α → Prop) : range (Quot.mk r) = univ := Quot.mk_surjective.range_eq @[simp] theorem range_quot_lift {r : ι → ι → Prop} (hf : ∀ x y, r x y → f x = f y) : range (Quot.lift f hf) = range f := ext fun _ => Quot.mk_surjective.exists @[simp] theorem range_quotient_mk {s : Setoid α} : range (Quotient.mk s) = univ := range_quot_mk _ @[simp] theorem range_quotient_lift [s : Setoid ι] (hf) : range (Quotient.lift f hf : Quotient s → α) = range f := range_quot_lift _ @[simp] theorem range_quotient_mk' {s : Setoid α} : range (Quotient.mk' : α → Quotient s) = univ := range_quot_mk _ lemma Quotient.range_mk'' {sa : Setoid α} : range (Quotient.mk'' (s₁ := sa)) = univ := range_quotient_mk @[simp] theorem range_quotient_lift_on' {s : Setoid ι} (hf) : (range fun x : Quotient s => Quotient.liftOn' x f hf) = range f := range_quot_lift _ instance canLift (c) (p) [CanLift α β c p] : CanLift (Set α) (Set β) (c '' ·) fun s => ∀ x ∈ s, p x where prf _ hs := subset_range_iff_exists_image_eq.mp fun x hx => CanLift.prf _ (hs x hx) theorem range_const_subset {c : α} : (range fun _ : ι => c) ⊆ {c} := range_subset_iff.2 fun _ => rfl @[simp] theorem range_const : ∀ [Nonempty ι] {c : α}, (range fun _ : ι => c) = {c} | ⟨x⟩, _ => (Subset.antisymm range_const_subset) fun _ hy => (mem_singleton_iff.1 hy).symm ▸ mem_range_self x theorem range_subtype_map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀ x, p x → q (f x)) : range (Subtype.map f h) = (↑) ⁻¹' (f '' { x | p x }) := by ext ⟨x, hx⟩ simp_rw [mem_preimage, mem_range, mem_image, Subtype.exists, Subtype.map] simp only [Subtype.mk.injEq, exists_prop, mem_setOf_eq] theorem image_swap_eq_preimage_swap : image (@Prod.swap α β) = preimage Prod.swap := image_eq_preimage_of_inverse Prod.swap_leftInverse Prod.swap_rightInverse theorem preimage_singleton_nonempty {f : α → β} {y : β} : (f ⁻¹' {y}).Nonempty ↔ y ∈ range f := Iff.rfl theorem preimage_singleton_eq_empty {f : α → β} {y : β} : f ⁻¹' {y} = ∅ ↔ y ∉ range f := not_nonempty_iff_eq_empty.symm.trans preimage_singleton_nonempty.not theorem range_subset_singleton {f : ι → α} {x : α} : range f ⊆ {x} ↔ f = const ι x := by simp [range_subset_iff, funext_iff, mem_singleton] theorem image_compl_preimage {f : α → β} {s : Set β} : f '' (f ⁻¹' s)ᶜ = range f \ s := by rw [compl_eq_univ_diff, image_diff_preimage, image_univ] theorem rangeFactorization_eq {f : ι → β} : Subtype.val ∘ rangeFactorization f = f := funext fun _ => rfl @[simp] theorem rangeFactorization_coe (f : ι → β) (a : ι) : (rangeFactorization f a : β) = f a := rfl @[simp] theorem coe_comp_rangeFactorization (f : ι → β) : (↑) ∘ rangeFactorization f = f := rfl theorem surjective_onto_range : Surjective (rangeFactorization f) := fun ⟨_, ⟨i, rfl⟩⟩ => ⟨i, rfl⟩ theorem image_eq_range (f : α → β) (s : Set α) : f '' s = range fun x : s => f x := by ext constructor · rintro ⟨x, h1, h2⟩ exact ⟨⟨x, h1⟩, h2⟩ · rintro ⟨⟨x, h1⟩, h2⟩ exact ⟨x, h1, h2⟩ theorem _root_.Sum.range_eq (f : α ⊕ β → γ) : range f = range (f ∘ Sum.inl) ∪ range (f ∘ Sum.inr) := ext fun _ => Sum.exists @[simp] theorem Sum.elim_range (f : α → γ) (g : β → γ) : range (Sum.elim f g) = range f ∪ range g := Sum.range_eq _ theorem range_ite_subset' {p : Prop} [Decidable p] {f g : α → β} : range (if p then f else g) ⊆ range f ∪ range g := by by_cases h : p · rw [if_pos h] exact subset_union_left · rw [if_neg h] exact subset_union_right theorem range_ite_subset {p : α → Prop} [DecidablePred p] {f g : α → β} : (range fun x => if p x then f x else g x) ⊆ range f ∪ range g := by rw [range_subset_iff]; intro x; by_cases h : p x · simp only [if_pos h, mem_union, mem_range, exists_apply_eq_apply, true_or] · simp [if_neg h, mem_union, mem_range_self] @[simp] theorem preimage_range (f : α → β) : f ⁻¹' range f = univ := eq_univ_of_forall mem_range_self /-- The range of a function from a `Unique` type contains just the function applied to its single value. -/ theorem range_unique [h : Unique ι] : range f = {f default} := by ext x rw [mem_range] constructor · rintro ⟨i, hi⟩ rw [h.uniq i] at hi exact hi ▸ mem_singleton _ · exact fun h => ⟨default, h.symm⟩ theorem range_diff_image_subset (f : α → β) (s : Set α) : range f \ f '' s ⊆ f '' sᶜ := fun _ ⟨⟨x, h₁⟩, h₂⟩ => ⟨x, fun h => h₂ ⟨x, h, h₁⟩, h₁⟩ @[simp] theorem range_inclusion (h : s ⊆ t) : range (inclusion h) = { x : t | (x : α) ∈ s } := by ext ⟨x, hx⟩ simp -- When `f` is injective, see also `Equiv.ofInjective`. theorem leftInverse_rangeSplitting (f : α → β) : LeftInverse (rangeFactorization f) (rangeSplitting f) := fun x => by ext simp only [rangeFactorization_coe] apply apply_rangeSplitting theorem rangeSplitting_injective (f : α → β) : Injective (rangeSplitting f) := (leftInverse_rangeSplitting f).injective theorem rightInverse_rangeSplitting {f : α → β} (h : Injective f) : RightInverse (rangeFactorization f) (rangeSplitting f) := (leftInverse_rangeSplitting f).rightInverse_of_injective fun _ _ hxy => h <| Subtype.ext_iff.1 hxy theorem preimage_rangeSplitting {f : α → β} (hf : Injective f) : preimage (rangeSplitting f) = image (rangeFactorization f) := (image_eq_preimage_of_inverse (rightInverse_rangeSplitting hf) (leftInverse_rangeSplitting f)).symm theorem isCompl_range_some_none (α : Type*) : IsCompl (range (some : α → Option α)) {none} := IsCompl.of_le (fun _ ⟨⟨_, ha⟩, (hn : _ = none)⟩ => Option.some_ne_none _ (ha.trans hn)) fun x _ => Option.casesOn x (Or.inr rfl) fun _ => Or.inl <| mem_range_self _ @[simp] theorem compl_range_some (α : Type*) : (range (some : α → Option α))ᶜ = {none} := (isCompl_range_some_none α).compl_eq @[simp] theorem range_some_inter_none (α : Type*) : range (some : α → Option α) ∩ {none} = ∅ := (isCompl_range_some_none α).inf_eq_bot -- Not `@[simp]` since `simp` can prove this. theorem range_some_union_none (α : Type*) : range (some : α → Option α) ∪ {none} = univ := (isCompl_range_some_none α).sup_eq_top @[simp] theorem insert_none_range_some (α : Type*) : insert none (range (some : α → Option α)) = univ := (isCompl_range_some_none α).symm.sup_eq_top lemma image_of_range_union_range_eq_univ {α β γ γ' δ δ' : Type*} {h : β → α} {f : γ → β} {f₁ : γ' → α} {f₂ : γ → γ'} {g : δ → β} {g₁ : δ' → α} {g₂ : δ → δ'} (hf : h ∘ f = f₁ ∘ f₂) (hg : h ∘ g = g₁ ∘ g₂) (hfg : range f ∪ range g = univ) (s : Set β) : h '' s = f₁ '' (f₂ '' (f ⁻¹' s)) ∪ g₁ '' (g₂ '' (g ⁻¹' s)) := by rw [← image_comp, ← image_comp, ← hf, ← hg, image_comp, image_comp, image_preimage_eq_inter_range, image_preimage_eq_inter_range, ← image_union, ← inter_union_distrib_left, hfg, inter_univ] end Range section Subsingleton variable {s : Set α} {f : α → β} /-- The image of a subsingleton is a subsingleton. -/ theorem Subsingleton.image (hs : s.Subsingleton) (f : α → β) : (f '' s).Subsingleton := fun _ ⟨_, hx, Hx⟩ _ ⟨_, hy, Hy⟩ => Hx ▸ Hy ▸ congr_arg f (hs hx hy) /-- The preimage of a subsingleton under an injective map is a subsingleton. -/ theorem Subsingleton.preimage {s : Set β} (hs : s.Subsingleton) (hf : Function.Injective f) : (f ⁻¹' s).Subsingleton := fun _ ha _ hb => hf <| hs ha hb /-- If the image of a set under an injective map is a subsingleton, the set is a subsingleton. -/ theorem subsingleton_of_image (hf : Function.Injective f) (s : Set α) (hs : (f '' s).Subsingleton) : s.Subsingleton := (hs.preimage hf).anti <| subset_preimage_image _ _ /-- If the preimage of a set under a surjective map is a subsingleton, the set is a subsingleton. -/ theorem subsingleton_of_preimage (hf : Function.Surjective f) (s : Set β) (hs : (f ⁻¹' s).Subsingleton) : s.Subsingleton := fun fx hx fy hy => by rcases hf fx, hf fy with ⟨⟨x, rfl⟩, ⟨y, rfl⟩⟩ exact congr_arg f (hs hx hy) theorem subsingleton_range {α : Sort*} [Subsingleton α] (f : α → β) : (range f).Subsingleton := forall_mem_range.2 fun x => forall_mem_range.2 fun y => congr_arg f (Subsingleton.elim x y) /-- The preimage of a nontrivial set under a surjective map is nontrivial. -/ theorem Nontrivial.preimage {s : Set β} (hs : s.Nontrivial) (hf : Function.Surjective f) : (f ⁻¹' s).Nontrivial := by rcases hs with ⟨fx, hx, fy, hy, hxy⟩ rcases hf fx, hf fy with ⟨⟨x, rfl⟩, ⟨y, rfl⟩⟩ exact ⟨x, hx, y, hy, mt (congr_arg f) hxy⟩ /-- The image of a nontrivial set under an injective map is nontrivial. -/ theorem Nontrivial.image (hs : s.Nontrivial) (hf : Function.Injective f) : (f '' s).Nontrivial := let ⟨x, hx, y, hy, hxy⟩ := hs ⟨f x, mem_image_of_mem f hx, f y, mem_image_of_mem f hy, hf.ne hxy⟩ theorem Nontrivial.image_of_injOn (hs : s.Nontrivial) (hf : s.InjOn f) : (f '' s).Nontrivial := by obtain ⟨x, hx, y, hy, hxy⟩ := hs exact ⟨f x, mem_image_of_mem _ hx, f y, mem_image_of_mem _ hy, (hxy <| hf hx hy ·)⟩ /-- If the image of a set is nontrivial, the set is nontrivial. -/ theorem nontrivial_of_image (f : α → β) (s : Set α) (hs : (f '' s).Nontrivial) : s.Nontrivial := let ⟨_, ⟨x, hx, rfl⟩, _, ⟨y, hy, rfl⟩, hxy⟩ := hs ⟨x, hx, y, hy, mt (congr_arg f) hxy⟩ @[simp] theorem image_nontrivial (hf : f.Injective) : (f '' s).Nontrivial ↔ s.Nontrivial := ⟨nontrivial_of_image f s, fun h ↦ h.image hf⟩ @[simp] theorem InjOn.image_nontrivial_iff (hf : s.InjOn f) : (f '' s).Nontrivial ↔ s.Nontrivial := ⟨nontrivial_of_image f s, fun h ↦ h.image_of_injOn hf⟩ /-- If the preimage of a set under an injective map is nontrivial, the set is nontrivial. -/ theorem nontrivial_of_preimage (hf : Function.Injective f) (s : Set β) (hs : (f ⁻¹' s).Nontrivial) : s.Nontrivial := (hs.image hf).mono <| image_preimage_subset _ _ end Subsingleton end Set namespace Function variable {α β : Type*} {ι : Sort*} {f : α → β} open Set theorem Surjective.preimage_injective (hf : Surjective f) : Injective (preimage f) := fun _ _ => (preimage_eq_preimage hf).1 theorem Injective.preimage_image (hf : Injective f) (s : Set α) : f ⁻¹' (f '' s) = s := preimage_image_eq s hf theorem Injective.preimage_surjective (hf : Injective f) : Surjective (preimage f) := Set.preimage_surjective.mpr hf theorem Injective.subsingleton_image_iff (hf : Injective f) {s : Set α} : (f '' s).Subsingleton ↔ s.Subsingleton := ⟨subsingleton_of_image hf s, fun h => h.image f⟩ theorem Surjective.image_preimage (hf : Surjective f) (s : Set β) : f '' (f ⁻¹' s) = s := image_preimage_eq s hf theorem Surjective.image_surjective (hf : Surjective f) : Surjective (image f) := by intro s use f ⁻¹' s rw [hf.image_preimage] @[simp] theorem Surjective.nonempty_preimage (hf : Surjective f) {s : Set β} : (f ⁻¹' s).Nonempty ↔ s.Nonempty := by rw [← image_nonempty, hf.image_preimage] theorem Injective.image_injective (hf : Injective f) : Injective (image f) := by intro s t h rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, h] lemma Injective.image_strictMono (inj : Function.Injective f) : StrictMono (image f) := monotone_image.strictMono_of_injective inj.image_injective theorem Surjective.preimage_subset_preimage_iff {s t : Set β} (hf : Surjective f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := by apply Set.preimage_subset_preimage_iff rw [hf.range_eq] apply subset_univ theorem Surjective.range_comp {ι' : Sort*} {f : ι → ι'} (hf : Surjective f) (g : ι' → α) : range (g ∘ f) = range g := ext fun y => (@Surjective.exists _ _ _ hf fun x => g x = y).symm theorem Injective.mem_range_iff_existsUnique (hf : Injective f) {b : β} : b ∈ range f ↔ ∃! a, f a = b := ⟨fun ⟨a, h⟩ => ⟨a, h, fun _ ha => hf (ha.trans h.symm)⟩, ExistsUnique.exists⟩ alias ⟨Injective.existsUnique_of_mem_range, _⟩ := Injective.mem_range_iff_existsUnique theorem Injective.compl_image_eq (hf : Injective f) (s : Set α) : (f '' s)ᶜ = f '' sᶜ ∪ (range f)ᶜ := by ext y rcases em (y ∈ range f) with (⟨x, rfl⟩ | hx) · simp [hf.eq_iff] · rw [mem_range, not_exists] at hx simp [hx] theorem LeftInverse.image_image {g : β → α} (h : LeftInverse g f) (s : Set α) : g '' (f '' s) = s := by rw [← image_comp, h.comp_eq_id, image_id] theorem LeftInverse.preimage_preimage {g : β → α} (h : LeftInverse g f) (s : Set α) : f ⁻¹' (g ⁻¹' s) = s := by rw [← preimage_comp, h.comp_eq_id, preimage_id] protected theorem Involutive.preimage {f : α → α} (hf : Involutive f) : Involutive (preimage f) := hf.rightInverse.preimage_preimage end Function namespace EquivLike variable {ι ι' : Sort*} {E : Type*} [EquivLike E ι ι'] @[simp] lemma range_comp {α : Type*} (f : ι' → α) (e : E) : range (f ∘ e) = range f := (EquivLike.surjective _).range_comp _ end EquivLike /-! ### Image and preimage on subtypes -/ namespace Subtype variable {α : Type*} theorem coe_image {p : α → Prop} {s : Set (Subtype p)} : (↑) '' s = { x | ∃ h : p x, (⟨x, h⟩ : Subtype p) ∈ s } := Set.ext fun a => ⟨fun ⟨⟨_, ha'⟩, in_s, h_eq⟩ => h_eq ▸ ⟨ha', in_s⟩, fun ⟨ha, in_s⟩ => ⟨⟨a, ha⟩, in_s, rfl⟩⟩ @[simp] theorem coe_image_of_subset {s t : Set α} (h : t ⊆ s) : (↑) '' { x : ↥s | ↑x ∈ t } = t := by ext x rw [mem_image] exact ⟨fun ⟨_, hx', hx⟩ => hx ▸ hx', fun hx => ⟨⟨x, h hx⟩, hx, rfl⟩⟩ theorem range_coe {s : Set α} : range ((↑) : s → α) = s := by rw [← image_univ] simp [-image_univ, coe_image] /-- A variant of `range_coe`. Try to use `range_coe` if possible. This version is useful when defining a new type that is defined as the subtype of something. In that case, the coercion doesn't fire anymore. -/ theorem range_val {s : Set α} : range (Subtype.val : s → α) = s := range_coe /-- We make this the simp lemma instead of `range_coe`. The reason is that if we write for `s : Set α` the function `(↑) : s → α`, then the inferred implicit arguments of `(↑)` are `↑α (fun x ↦ x ∈ s)`. -/ @[simp] theorem range_coe_subtype {p : α → Prop} : range ((↑) : Subtype p → α) = { x | p x } := range_coe @[simp] theorem coe_preimage_self (s : Set α) : ((↑) : s → α) ⁻¹' s = univ := by rw [← preimage_range, range_coe] theorem range_val_subtype {p : α → Prop} : range (Subtype.val : Subtype p → α) = { x | p x } := range_coe theorem coe_image_subset (s : Set α) (t : Set s) : ((↑) : s → α) '' t ⊆ s := fun x ⟨y, _, yvaleq⟩ => by rw [← yvaleq]; exact y.property theorem coe_image_univ (s : Set α) : ((↑) : s → α) '' Set.univ = s := image_univ.trans range_coe @[simp] theorem image_preimage_coe (s t : Set α) : ((↑) : s → α) '' (((↑) : s → α) ⁻¹' t) = s ∩ t := image_preimage_eq_range_inter.trans <| congr_arg (· ∩ t) range_coe theorem image_preimage_val (s t : Set α) : (Subtype.val : s → α) '' (Subtype.val ⁻¹' t) = s ∩ t := image_preimage_coe s t theorem preimage_coe_eq_preimage_coe_iff {s t u : Set α} : ((↑) : s → α) ⁻¹' t = ((↑) : s → α) ⁻¹' u ↔ s ∩ t = s ∩ u := by rw [← image_preimage_coe, ← image_preimage_coe, coe_injective.image_injective.eq_iff] theorem preimage_coe_self_inter (s t : Set α) : ((↑) : s → α) ⁻¹' (s ∩ t) = ((↑) : s → α) ⁻¹' t := by rw [preimage_coe_eq_preimage_coe_iff, ← inter_assoc, inter_self] -- Not `@[simp]` since `simp` can prove this. theorem preimage_coe_inter_self (s t : Set α) : ((↑) : s → α) ⁻¹' (t ∩ s) = ((↑) : s → α) ⁻¹' t := by rw [inter_comm, preimage_coe_self_inter] theorem preimage_val_eq_preimage_val_iff (s t u : Set α) : (Subtype.val : s → α) ⁻¹' t = Subtype.val ⁻¹' u ↔ s ∩ t = s ∩ u := preimage_coe_eq_preimage_coe_iff lemma preimage_val_subset_preimage_val_iff (s t u : Set α) : (Subtype.val ⁻¹' t : Set s) ⊆ Subtype.val ⁻¹' u ↔ s ∩ t ⊆ s ∩ u := by constructor · rw [← image_preimage_coe, ← image_preimage_coe] exact image_subset _ · intro h x a exact (h ⟨x.2, a⟩).2 theorem exists_set_subtype {t : Set α} (p : Set α → Prop) : (∃ s : Set t, p (((↑) : t → α) '' s)) ↔ ∃ s : Set α, s ⊆ t ∧ p s := by rw [← exists_subset_range_and_iff, range_coe] theorem forall_set_subtype {t : Set α} (p : Set α → Prop) : (∀ s : Set t, p (((↑) : t → α) '' s)) ↔ ∀ s : Set α, s ⊆ t → p s := by rw [← forall_subset_range_iff, range_coe] theorem preimage_coe_nonempty {s t : Set α} : (((↑) : s → α) ⁻¹' t).Nonempty ↔ (s ∩ t).Nonempty := by rw [← image_preimage_coe, image_nonempty] theorem preimage_coe_eq_empty {s t : Set α} : ((↑) : s → α) ⁻¹' t = ∅ ↔ s ∩ t = ∅ := by simp [← not_nonempty_iff_eq_empty, preimage_coe_nonempty] -- Not `@[simp]` since `simp` can prove this. theorem preimage_coe_compl (s : Set α) : ((↑) : s → α) ⁻¹' sᶜ = ∅ := preimage_coe_eq_empty.2 (inter_compl_self s) @[simp] theorem preimage_coe_compl' (s : Set α) : (fun x : (sᶜ : Set α) => (x : α)) ⁻¹' s = ∅ := preimage_coe_eq_empty.2 (compl_inter_self s) end Subtype /-! ### Images and preimages on `Option` -/ open Set namespace Option theorem injective_iff {α β} {f : Option α → β} : Injective f ↔ Injective (f ∘ some) ∧ f none ∉ range (f ∘ some) := by simp only [mem_range, not_exists, (· ∘ ·)] refine ⟨fun hf => ⟨hf.comp (Option.some_injective _), fun x => hf.ne <| Option.some_ne_none _⟩, ?_⟩ rintro ⟨h_some, h_none⟩ (_ | a) (_ | b) hab exacts [rfl, (h_none _ hab.symm).elim, (h_none _ hab).elim, congr_arg some (h_some hab)] theorem range_eq {α β} (f : Option α → β) : range f = insert (f none) (range (f ∘ some)) := Set.ext fun _ => Option.exists.trans <| eq_comm.or Iff.rfl end Option namespace Set open Function /-! ### Injectivity and surjectivity lemmas for image and preimage -/ section ImagePreimage variable {α : Type u} {β : Type v} {f : α → β} @[simp] theorem image_surjective : Surjective (image f) ↔ Surjective f := by refine ⟨fun h y => ?_, Surjective.image_surjective⟩ rcases h {y} with ⟨s, hs⟩ have := mem_singleton y; rw [← hs] at this; rcases this with ⟨x, _, hx⟩ exact ⟨x, hx⟩ @[simp] theorem image_injective : Injective (image f) ↔ Injective f := by refine ⟨fun h x x' hx => ?_, Injective.image_injective⟩ rw [← singleton_eq_singleton_iff]; apply h rw [image_singleton, image_singleton, hx] theorem preimage_eq_iff_eq_image {f : α → β} (hf : Bijective f) {s t} : f ⁻¹' s = t ↔ s = f '' t := by rw [← image_eq_image hf.1, hf.2.image_preimage] theorem eq_preimage_iff_image_eq {f : α → β} (hf : Bijective f) {s t} : s = f ⁻¹' t ↔ f '' s = t := by rw [← image_eq_image hf.1, hf.2.image_preimage] end ImagePreimage end Set /-! ### Disjoint lemmas for image and preimage -/ section Disjoint variable {α β γ : Type*} {f : α → β} {s t : Set α} theorem Disjoint.preimage (f : α → β) {s t : Set β} (h : Disjoint s t) : Disjoint (f ⁻¹' s) (f ⁻¹' t) := disjoint_iff_inf_le.mpr fun _ hx => h.le_bot hx lemma Codisjoint.preimage (f : α → β) {s t : Set β} (h : Codisjoint s t) : Codisjoint (f ⁻¹' s) (f ⁻¹' t) := by simp only [codisjoint_iff_le_sup, Set.sup_eq_union, top_le_iff, ← Set.preimage_union] at h ⊢ rw [h]; rfl lemma IsCompl.preimage (f : α → β) {s t : Set β} (h : IsCompl s t) : IsCompl (f ⁻¹' s) (f ⁻¹' t) := ⟨h.1.preimage f, h.2.preimage f⟩ namespace Set theorem disjoint_image_image {f : β → α} {g : γ → α} {s : Set β} {t : Set γ} (h : ∀ b ∈ s, ∀ c ∈ t, f b ≠ g c) : Disjoint (f '' s) (g '' t) := disjoint_iff_inf_le.mpr <| by rintro a ⟨⟨b, hb, eq⟩, c, hc, rfl⟩; exact h b hb c hc eq theorem disjoint_image_of_injective (hf : Injective f) {s t : Set α} (hd : Disjoint s t) : Disjoint (f '' s) (f '' t) := disjoint_image_image fun _ hx _ hy => hf.ne fun H => Set.disjoint_iff.1 hd ⟨hx, H.symm ▸ hy⟩ theorem _root_.Disjoint.of_image (h : Disjoint (f '' s) (f '' t)) : Disjoint s t := disjoint_iff_inf_le.mpr fun _ hx => disjoint_left.1 h (mem_image_of_mem _ hx.1) (mem_image_of_mem _ hx.2) @[simp] theorem disjoint_image_iff (hf : Injective f) : Disjoint (f '' s) (f '' t) ↔ Disjoint s t := ⟨Disjoint.of_image, disjoint_image_of_injective hf⟩ theorem _root_.Disjoint.of_preimage (hf : Surjective f) {s t : Set β} (h : Disjoint (f ⁻¹' s) (f ⁻¹' t)) : Disjoint s t := by rw [disjoint_iff_inter_eq_empty, ← image_preimage_eq (_ ∩ _) hf, preimage_inter, h.inter_eq, image_empty] @[simp] theorem disjoint_preimage_iff (hf : Surjective f) {s t : Set β} : Disjoint (f ⁻¹' s) (f ⁻¹' t) ↔ Disjoint s t := ⟨Disjoint.of_preimage hf, Disjoint.preimage _⟩ theorem preimage_eq_empty {s : Set β} (h : Disjoint s (range f)) : f ⁻¹' s = ∅ := by simpa using h.preimage f theorem preimage_eq_empty_iff {s : Set β} : f ⁻¹' s = ∅ ↔ Disjoint s (range f) := ⟨fun h => by simp only [eq_empty_iff_forall_not_mem, disjoint_iff_inter_eq_empty, not_exists, mem_inter_iff, not_and, mem_range, mem_preimage] at h ⊢ intro y hy x hx rw [← hx] at hy exact h x hy, preimage_eq_empty⟩ end Set
end Disjoint section Sigma
Mathlib/Data/Set/Image.lean
1,373
1,375
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Algebra.CharP.Algebra import Mathlib.FieldTheory.SplittingField.IsSplittingField import Mathlib.LinearAlgebra.Dual.Lemmas import Mathlib.RingTheory.Algebraic.Basic /-! # Splitting fields In this file we prove the existence and uniqueness of splitting fields. ## Main definitions * `Polynomial.SplittingField f`: A fixed splitting field of the polynomial `f`. ## Main statements * `Polynomial.IsSplittingField.algEquiv`: Every splitting field of a polynomial `f` is isomorphic to `SplittingField f` and thus, being a splitting field is unique up to isomorphism. ## Implementation details We construct a `SplittingFieldAux` without worrying about whether the instances satisfy nice definitional equalities. Then the actual `SplittingField` is defined to be a quotient of a `MvPolynomial` ring by the kernel of the obvious map into `SplittingFieldAux`. Because the actual `SplittingField` will be a quotient of a `MvPolynomial`, it has nice instances on it. -/ noncomputable section universe u v w variable {F : Type u} {K : Type v} {L : Type w} namespace Polynomial variable [Field K] [Field L] [Field F] open Polynomial section SplittingField open Classical in /-- Non-computably choose an irreducible factor from a polynomial. -/ def factor (f : K[X]) : K[X] := if H : ∃ g, Irreducible g ∧ g ∣ f then Classical.choose H else X theorem irreducible_factor (f : K[X]) : Irreducible (factor f) := by rw [factor] split_ifs with H · exact (Classical.choose_spec H).1 · exact irreducible_X /-- See note [fact non-instances]. -/ theorem fact_irreducible_factor (f : K[X]) : Fact (Irreducible (factor f)) := ⟨irreducible_factor f⟩ attribute [local instance] fact_irreducible_factor theorem factor_dvd_of_not_isUnit {f : K[X]} (hf1 : ¬IsUnit f) : factor f ∣ f := by by_cases hf2 : f = 0; · rw [hf2]; exact dvd_zero _ rw [factor, dif_pos (WfDvdMonoid.exists_irreducible_factor hf1 hf2)] exact (Classical.choose_spec <| WfDvdMonoid.exists_irreducible_factor hf1 hf2).2 theorem factor_dvd_of_degree_ne_zero {f : K[X]} (hf : f.degree ≠ 0) : factor f ∣ f := factor_dvd_of_not_isUnit (mt degree_eq_zero_of_isUnit hf) theorem factor_dvd_of_natDegree_ne_zero {f : K[X]} (hf : f.natDegree ≠ 0) : factor f ∣ f := factor_dvd_of_degree_ne_zero (mt natDegree_eq_of_degree_eq_some hf) lemma isCoprime_iff_aeval_ne_zero (f g : K[X]) : IsCoprime f g ↔ ∀ {A : Type v} [CommRing A] [IsDomain A] [Algebra K A] (a : A), aeval a f ≠ 0 ∨ aeval a g ≠ 0 := by refine ⟨fun h => aeval_ne_zero_of_isCoprime h, fun h => isCoprime_of_dvd _ _ ?_ fun x hx _ => ?_⟩ · replace h := @h K _ _ _ 0 contrapose! h rw [h.left, h.right, map_zero, and_self] · rintro ⟨_, rfl⟩ ⟨_, rfl⟩ replace h := not_and_or.mpr <| h <| AdjoinRoot.root x.factor simp only [AdjoinRoot.aeval_eq, AdjoinRoot.mk_eq_zero, dvd_mul_of_dvd_left <| factor_dvd_of_not_isUnit hx, true_and, not_true] at h /-- Divide a polynomial f by `X - C r` where `r` is a root of `f` in a bigger field extension. -/ def removeFactor (f : K[X]) : Polynomial (AdjoinRoot <| factor f) :=
map (AdjoinRoot.of f.factor) f /ₘ (X - C (AdjoinRoot.root f.factor)) theorem X_sub_C_mul_removeFactor (f : K[X]) (hf : f.natDegree ≠ 0) : (X - C (AdjoinRoot.root f.factor)) * f.removeFactor = map (AdjoinRoot.of f.factor) f := by let ⟨g, hg⟩ := factor_dvd_of_natDegree_ne_zero hf apply (mul_divByMonic_eq_iff_isRoot
Mathlib/FieldTheory/SplittingField/Construction.lean
88
93
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Order.Group.Unbundled.Int import Mathlib.Algebra.Ring.Nat import Mathlib.Data.Int.GCD /-! # Congruences modulo a natural number This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers, and proves basic properties about it such as the Chinese Remainder Theorem `modEq_and_modEq_iff_modEq_mul`. ## Notations `a ≡ b [MOD n]` is notation for `nat.ModEq n a b`, which is defined to mean `a % n = b % n`. ## Tags ModEq, congruence, mod, MOD, modulo -/ assert_not_exists OrderedAddCommMonoid Function.support namespace Nat /-- Modular equality. `n.ModEq a b`, or `a ≡ b [MOD n]`, means that `a - b` is a multiple of `n`. -/ def ModEq (n a b : ℕ) := a % n = b % n @[inherit_doc] notation:50 a " ≡ " b " [MOD " n "]" => ModEq n a b variable {m n a b c d : ℕ} -- Since `ModEq` is semi-reducible, we need to provide the decidable instance manually instance : Decidable (ModEq n a b) := inferInstanceAs <| Decidable (a % n = b % n) namespace ModEq @[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := rfl protected theorem rfl : a ≡ a [MOD n] := ModEq.refl _ instance : IsRefl _ (ModEq n) := ⟨ModEq.refl⟩ @[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := Eq.symm @[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := Eq.trans instance : Trans (ModEq n) (ModEq n) (ModEq n) where trans := Nat.ModEq.trans protected theorem comm : a ≡ b [MOD n] ↔ b ≡ a [MOD n] := ⟨ModEq.symm, ModEq.symm⟩ end ModEq theorem modEq_zero_iff_dvd : a ≡ 0 [MOD n] ↔ n ∣ a := by rw [ModEq, zero_mod, dvd_iff_mod_eq_zero] theorem _root_.Dvd.dvd.modEq_zero_nat (h : n ∣ a) : a ≡ 0 [MOD n] := modEq_zero_iff_dvd.2 h theorem _root_.Dvd.dvd.zero_modEq_nat (h : n ∣ a) : 0 ≡ a [MOD n] := h.modEq_zero_nat.symm theorem modEq_iff_dvd : a ≡ b [MOD n] ↔ (n : ℤ) ∣ b - a := by rw [ModEq, eq_comm, ← Int.natCast_inj, Int.natCast_mod, Int.natCast_mod, Int.emod_eq_emod_iff_emod_sub_eq_zero, Int.dvd_iff_emod_eq_zero] alias ⟨ModEq.dvd, modEq_of_dvd⟩ := modEq_iff_dvd /-- A variant of `modEq_iff_dvd` with `Nat` divisibility -/ theorem modEq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a := by rw [modEq_iff_dvd, ← Int.natCast_dvd_natCast, Int.ofNat_sub h] theorem mod_modEq (a n) : a % n ≡ a [MOD n] := mod_mod _ _ namespace ModEq lemma of_dvd (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] := modEq_of_dvd <| Int.ofNat_dvd.mpr d |>.trans h.dvd protected theorem mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD c * n] := by unfold ModEq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h] @[gcongr] protected theorem mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] := (h.mul_left' _).of_dvd (dvd_mul_left _ _) protected theorem mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n * c] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact h.mul_left' c @[gcongr] protected theorem mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] := by rw [mul_comm a, mul_comm b]; exact h.mul_left c @[gcongr] protected theorem mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] := (h₂.mul_left _).trans (h₁.mul_right _) @[gcongr] protected theorem pow (m : ℕ) (h : a ≡ b [MOD n]) : a ^ m ≡ b ^ m [MOD n] := by induction m with | zero => rfl | succ d hd => rw [Nat.pow_succ, Nat.pow_succ] exact hd.mul h @[gcongr] protected theorem add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] := by rw [modEq_iff_dvd, Int.natCast_add, Int.natCast_add, add_sub_add_comm] exact Int.dvd_add h₁.dvd h₂.dvd @[gcongr] protected theorem add_left (c : ℕ) (h : a ≡ b [MOD n]) : c + a ≡ c + b [MOD n] := ModEq.rfl.add h @[gcongr] protected theorem add_right (c : ℕ) (h : a ≡ b [MOD n]) : a + c ≡ b + c [MOD n] := h.add ModEq.rfl protected theorem add_left_cancel (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] := by simp only [modEq_iff_dvd, Int.natCast_add] at * rw [add_sub_add_comm] at h₂ convert Int.dvd_sub h₂ h₁ using 1 rw [add_sub_cancel_left] protected theorem add_left_cancel' (c : ℕ) (h : c + a ≡ c + b [MOD n]) : a ≡ b [MOD n] := ModEq.rfl.add_left_cancel h protected theorem add_right_cancel (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] := by rw [add_comm a, add_comm b] at h₂ exact h₁.add_left_cancel h₂ protected theorem add_right_cancel' (c : ℕ) (h : a + c ≡ b + c [MOD n]) : a ≡ b [MOD n] := ModEq.rfl.add_right_cancel h /-- Cancel left multiplication on both sides of the `≡` and in the modulus. For cancelling left multiplication in the modulus, see `Nat.ModEq.of_mul_left`. -/ protected theorem mul_left_cancel' {a b c m : ℕ} (hc : c ≠ 0) : c * a ≡ c * b [MOD c * m] → a ≡ b [MOD m] := by simp only [modEq_iff_dvd, Int.natCast_mul, ← Int.mul_sub] exact fun h => (Int.dvd_of_mul_dvd_mul_left (Int.ofNat_ne_zero.mpr hc) h) protected theorem mul_left_cancel_iff' {a b c m : ℕ} (hc : c ≠ 0) : c * a ≡ c * b [MOD c * m] ↔ a ≡ b [MOD m] := ⟨ModEq.mul_left_cancel' hc, ModEq.mul_left' _⟩ /-- Cancel right multiplication on both sides of the `≡` and in the modulus. For cancelling right multiplication in the modulus, see `Nat.ModEq.of_mul_right`. -/ protected theorem mul_right_cancel' {a b c m : ℕ} (hc : c ≠ 0) : a * c ≡ b * c [MOD m * c] → a ≡ b [MOD m] := by simp only [modEq_iff_dvd, Int.natCast_mul, ← Int.sub_mul] exact fun h => (Int.dvd_of_mul_dvd_mul_right (Int.ofNat_ne_zero.mpr hc) h) protected theorem mul_right_cancel_iff' {a b c m : ℕ} (hc : c ≠ 0) : a * c ≡ b * c [MOD m * c] ↔ a ≡ b [MOD m] := ⟨ModEq.mul_right_cancel' hc, ModEq.mul_right' _⟩ /-- Cancel left multiplication in the modulus. For cancelling left multiplication on both sides of the `≡`, see `nat.modeq.mul_left_cancel'`. -/ lemma of_mul_left (m : ℕ) (h : a ≡ b [MOD m * n]) : a ≡ b [MOD n] := by rw [modEq_iff_dvd] at * exact (dvd_mul_left (n : ℤ) (m : ℤ)).trans h /-- Cancel right multiplication in the modulus. For cancelling right multiplication on both sides of the `≡`, see `nat.modeq.mul_right_cancel'`. -/ lemma of_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] := mul_comm m n ▸ of_mul_left _ theorem of_div (h : a / c ≡ b / c [MOD m / c]) (ha : c ∣ a) (ha : c ∣ b) (ha : c ∣ m) : a ≡ b [MOD m] := by convert h.mul_left' c <;> rwa [Nat.mul_div_cancel'] end ModEq lemma modEq_sub (h : b ≤ a) : a ≡ b [MOD a - b] := (modEq_of_dvd <| by rw [Int.ofNat_sub h]).symm lemma modEq_one : a ≡ b [MOD 1] := modEq_of_dvd <| one_dvd _ @[simp] lemma modEq_zero_iff : a ≡ b [MOD 0] ↔ a = b := by rw [ModEq, mod_zero, mod_zero] @[simp] lemma add_modEq_left : n + a ≡ a [MOD n] := by rw [ModEq, add_mod_left] @[simp] lemma add_modEq_right : a + n ≡ a [MOD n] := by rw [ModEq, add_mod_right] namespace ModEq theorem le_of_lt_add (h1 : a ≡ b [MOD m]) (h2 : a < b + m) : a ≤ b := (le_total a b).elim id fun h3 => Nat.le_of_sub_eq_zero (eq_zero_of_dvd_of_lt ((modEq_iff_dvd' h3).mp h1.symm) (by omega)) theorem add_le_of_lt (h1 : a ≡ b [MOD m]) (h2 : a < b) : a + m ≤ b := le_of_lt_add (add_modEq_right.trans h1) (by omega) theorem dvd_iff (h : a ≡ b [MOD m]) (hdm : d ∣ m) : d ∣ a ↔ d ∣ b := by simp only [← modEq_zero_iff_dvd] replace h := h.of_dvd hdm exact ⟨h.symm.trans, h.trans⟩ theorem gcd_eq (h : a ≡ b [MOD m]) : gcd a m = gcd b m := by have h1 := gcd_dvd_right a m have h2 := gcd_dvd_right b m exact dvd_antisymm (dvd_gcd ((h.dvd_iff h1).mp (gcd_dvd_left a m)) h1) (dvd_gcd ((h.dvd_iff h2).mpr (gcd_dvd_left b m)) h2) lemma eq_of_abs_lt (h : a ≡ b [MOD m]) (h2 : |(b : ℤ) - a| < m) : a = b := by apply Int.ofNat.inj rw [eq_comm, ← sub_eq_zero] exact Int.eq_zero_of_abs_lt_dvd h.dvd h2 lemma eq_of_lt_of_lt (h : a ≡ b [MOD m]) (ha : a < m) (hb : b < m) : a = b := h.eq_of_abs_lt <| Int.abs_sub_lt_of_lt_lt ha hb /-- To cancel a common factor `c` from a `ModEq` we must divide the modulus `m` by `gcd m c` -/ lemma cancel_left_div_gcd (hm : 0 < m) (h : c * a ≡ c * b [MOD m]) : a ≡ b [MOD m / gcd m c] := by let d := gcd m c have hmd := gcd_dvd_left m c have hcd := gcd_dvd_right m c rw [modEq_iff_dvd] refine @Int.dvd_of_dvd_mul_right_of_gcd_one (m / d) (c / d) (b - a) ?_ ?_ · show (m / d : ℤ) ∣ c / d * (b - a) rw [mul_comm, ← Int.mul_ediv_assoc (b - a) (Int.natCast_dvd_natCast.mpr hcd), mul_comm] apply Int.ediv_dvd_ediv (Int.natCast_dvd_natCast.mpr hmd) rw [Int.mul_sub] exact modEq_iff_dvd.mp h · show Int.gcd (m / d) (c / d) = 1 simp only [d, ← Int.natCast_div, Int.gcd_natCast_natCast (m / d) (c / d), gcd_div hmd hcd, Nat.div_self (gcd_pos_of_pos_left c hm)] /-- To cancel a common factor `c` from a `ModEq` we must divide the modulus `m` by `gcd m c` -/ lemma cancel_right_div_gcd (hm : 0 < m) (h : a * c ≡ b * c [MOD m]) : a ≡ b [MOD m / gcd m c] := by apply cancel_left_div_gcd hm simpa [mul_comm] using h lemma cancel_left_div_gcd' (hm : 0 < m) (hcd : c ≡ d [MOD m]) (h : c * a ≡ d * b [MOD m]) : a ≡ b [MOD m / gcd m c] := (h.trans <| hcd.symm.mul_right b).cancel_left_div_gcd hm lemma cancel_right_div_gcd' (hm : 0 < m) (hcd : c ≡ d [MOD m]) (h : a * c ≡ b * d [MOD m]) : a ≡ b [MOD m / gcd m c] := (h.trans <| hcd.symm.mul_left b).cancel_right_div_gcd hm /-- A common factor that's coprime with the modulus can be cancelled from a `ModEq` -/ lemma cancel_left_of_coprime (hmc : gcd m c = 1) (h : c * a ≡ c * b [MOD m]) : a ≡ b [MOD m] := by rcases m.eq_zero_or_pos with (rfl | hm) · simp only [gcd_zero_left] at hmc simp only [gcd_zero_left, hmc, one_mul, modEq_zero_iff] at h subst h rfl simpa [hmc] using h.cancel_left_div_gcd hm /-- A common factor that's coprime with the modulus can be cancelled from a `ModEq` -/ lemma cancel_right_of_coprime (hmc : gcd m c = 1) (h : a * c ≡ b * c [MOD m]) : a ≡ b [MOD m] := cancel_left_of_coprime hmc <| by simpa [mul_comm] using h end ModEq /-- The natural number less than `lcm n m` congruent to `a` mod `n` and `b` mod `m` -/ def chineseRemainder' (h : a ≡ b [MOD gcd n m]) : { k // k ≡ a [MOD n] ∧ k ≡ b [MOD m] } := if hn : n = 0 then ⟨a, by rw [hn, gcd_zero_left] at h; constructor · rfl · exact h⟩ else if hm : m = 0 then ⟨b, by rw [hm, gcd_zero_right] at h; constructor · exact h.symm · rfl⟩ else ⟨let (c, d) := xgcd n m; Int.toNat ((n * c * b + m * d * a) / gcd n m % lcm n m), by rw [xgcd_val] dsimp rw [modEq_iff_dvd, modEq_iff_dvd, Int.toNat_of_nonneg (Int.emod_nonneg _ (Int.natCast_ne_zero.2 (lcm_ne_zero hn hm)))] have hnonzero : (gcd n m : ℤ) ≠ 0 := by norm_cast rw [Nat.gcd_eq_zero_iff, not_and] exact fun _ => hm have hcoedvd : ∀ t, (gcd n m : ℤ) ∣ t * (b - a) := fun t => h.dvd.mul_left _ have := gcd_eq_gcd_ab n m constructor <;> rw [Int.emod_def, ← sub_add] <;> refine Int.dvd_add ?_ (dvd_mul_of_dvd_left ?_ _) <;> try norm_cast · rw [← sub_eq_iff_eq_add'] at this rw [← this, Int.sub_mul, ← add_sub_assoc, add_comm, add_sub_assoc, ← Int.mul_sub, Int.add_ediv_of_dvd_left, Int.mul_ediv_cancel_left _ hnonzero, Int.mul_ediv_assoc _ h.dvd, ← sub_sub, sub_self, zero_sub, Int.dvd_neg, mul_assoc] · exact dvd_mul_right _ _ norm_cast exact dvd_mul_right _ _ · exact dvd_lcm_left n m · rw [← sub_eq_iff_eq_add] at this rw [← this, Int.sub_mul, sub_add, ← Int.mul_sub, Int.sub_ediv_of_dvd, Int.mul_ediv_cancel_left _ hnonzero, Int.mul_ediv_assoc _ h.dvd, ← sub_add, sub_self, zero_add, mul_assoc] · exact dvd_mul_right _ _ · exact hcoedvd _ · exact dvd_lcm_right n m⟩ /-- The natural number less than `n*m` congruent to `a` mod `n` and `b` mod `m` -/ def chineseRemainder (co : n.Coprime m) (a b : ℕ) : { k // k ≡ a [MOD n] ∧ k ≡ b [MOD m] } := chineseRemainder' (by convert @modEq_one a b) theorem chineseRemainder'_lt_lcm (h : a ≡ b [MOD gcd n m]) (hn : n ≠ 0) (hm : m ≠ 0) : ↑(chineseRemainder' h) < lcm n m := by dsimp only [chineseRemainder'] rw [dif_neg hn, dif_neg hm, Subtype.coe_mk, xgcd_val, ← Int.toNat_natCast (lcm n m)] have lcm_pos := Int.natCast_pos.mpr (Nat.pos_of_ne_zero (lcm_ne_zero hn hm)) exact (Int.toNat_lt_toNat lcm_pos).mpr (Int.emod_lt_of_pos _ lcm_pos) theorem chineseRemainder_lt_mul (co : n.Coprime m) (a b : ℕ) (hn : n ≠ 0) (hm : m ≠ 0) : ↑(chineseRemainder co a b) < n * m := lt_of_lt_of_le (chineseRemainder'_lt_lcm _ hn hm) (le_of_eq co.lcm_eq_mul) theorem mod_lcm (hn : a ≡ b [MOD n]) (hm : a ≡ b [MOD m]) : a ≡ b [MOD lcm n m] := Nat.modEq_iff_dvd.mpr <| Int.coe_lcm_dvd (Nat.modEq_iff_dvd.mp hn) (Nat.modEq_iff_dvd.mp hm) theorem chineseRemainder_modEq_unique (co : n.Coprime m) {a b z} (hzan : z ≡ a [MOD n]) (hzbm : z ≡ b [MOD m]) : z ≡ chineseRemainder co a b [MOD n*m] := by simpa [Nat.Coprime.lcm_eq_mul co] using mod_lcm (hzan.trans ((chineseRemainder co a b).prop.1).symm) (hzbm.trans ((chineseRemainder co a b).prop.2).symm) theorem modEq_and_modEq_iff_modEq_mul {a b m n : ℕ} (hmn : m.Coprime n) : a ≡ b [MOD m] ∧ a ≡ b [MOD n] ↔ a ≡ b [MOD m * n] := ⟨fun h => by rw [Nat.modEq_iff_dvd, Nat.modEq_iff_dvd, ← Int.dvd_natAbs, Int.natCast_dvd_natCast, ← Int.dvd_natAbs, Int.natCast_dvd_natCast] at h rw [Nat.modEq_iff_dvd, ← Int.dvd_natAbs, Int.natCast_dvd_natCast] exact hmn.mul_dvd_of_dvd_of_dvd h.1 h.2, fun h => ⟨h.of_mul_right _, h.of_mul_left _⟩⟩ theorem coprime_of_mul_modEq_one (b : ℕ) {a n : ℕ} (h : a * b ≡ 1 [MOD n]) : a.Coprime n := by obtain ⟨g, hh⟩ := Nat.gcd_dvd_right a n rw [Nat.coprime_iff_gcd_eq_one, ← Nat.dvd_one, ← Nat.modEq_zero_iff_dvd] calc 1 ≡ a * b [MOD a.gcd n] := (hh ▸ h).symm.of_mul_right g _ ≡ 0 * b [MOD a.gcd n] := (Nat.modEq_zero_iff_dvd.mpr (Nat.gcd_dvd_left _ _)).mul_right b _ = 0 := by rw [zero_mul] theorem add_mod_add_ite (a b c : ℕ) : ((a + b) % c + if c ≤ a % c + b % c then c else 0) = a % c + b % c := have : (a + b) % c = (a % c + b % c) % c := ((mod_modEq _ _).add <| mod_modEq _ _).symm if hc0 : c = 0 then by simp [hc0, Nat.mod_zero] else by rw [this] split_ifs with h · have h2 : (a % c + b % c) / c < 2 := Nat.div_lt_of_lt_mul (by rw [mul_two] exact add_lt_add (Nat.mod_lt _ (Nat.pos_of_ne_zero hc0)) (Nat.mod_lt _ (Nat.pos_of_ne_zero hc0))) have h0 : 0 < (a % c + b % c) / c := Nat.div_pos h (Nat.pos_of_ne_zero hc0) rw [← @add_right_cancel_iff _ _ _ (c * ((a % c + b % c) / c)), add_comm _ c, add_assoc, mod_add_div, le_antisymm (le_of_lt_succ h2) h0, mul_one, add_comm] · rw [Nat.mod_eq_of_lt (lt_of_not_ge h), add_zero] theorem add_mod_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) % c = a % c + b % c := by rw [← add_mod_add_ite, if_neg (not_le_of_lt hc), add_zero] theorem add_mod_add_of_le_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) : (a + b) % c + c = a % c + b % c := by rw [← add_mod_add_ite, if_pos hc] theorem add_div_eq_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) / c = a / c + b / c := if hc0 : c = 0 then by simp [hc0] else by rw [Nat.add_div (Nat.pos_of_ne_zero hc0), if_neg (not_le_of_lt hc), add_zero] protected theorem add_div_of_dvd_right {a b c : ℕ} (hca : c ∣ a) : (a + b) / c = a / c + b / c := if h : c = 0 then by simp [h] else add_div_eq_of_add_mod_lt (by rw [Nat.mod_eq_zero_of_dvd hca, zero_add] exact Nat.mod_lt _ (zero_lt_of_ne_zero h)) protected theorem add_div_of_dvd_left {a b c : ℕ} (hca : c ∣ b) : (a + b) / c = a / c + b / c := by rwa [add_comm, Nat.add_div_of_dvd_right, add_comm] theorem add_div_eq_of_le_mod_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) (hc0 : 0 < c) : (a + b) / c = a / c + b / c + 1 := by rw [Nat.add_div hc0, if_pos hc] theorem add_div_le_add_div (a b c : ℕ) : a / c + b / c ≤ (a + b) / c := if hc0 : c = 0 then by simp [hc0] else by rw [Nat.add_div (Nat.pos_of_ne_zero hc0)]; exact Nat.le_add_right _ _ theorem le_mod_add_mod_of_dvd_add_of_not_dvd {a b c : ℕ} (h : c ∣ a + b) (ha : ¬c ∣ a) : c ≤ a % c + b % c := by_contradiction fun hc => by have : (a + b) % c = a % c + b % c := add_mod_of_add_mod_lt (lt_of_not_ge hc) simp_all [dvd_iff_mod_eq_zero] theorem odd_mul_odd {n m : ℕ} : n % 2 = 1 → m % 2 = 1 → n * m % 2 = 1 := by simpa [Nat.ModEq] using @ModEq.mul 2 n 1 m 1 theorem odd_mul_odd_div_two {m n : ℕ} (hm1 : m % 2 = 1) (hn1 : n % 2 = 1) : m * n / 2 = m * (n / 2) + m / 2 := have hn0 : 0 < n := Nat.pos_of_ne_zero fun h => by simp_all mul_right_injective₀ two_ne_zero <| by dsimp rw [mul_add, two_mul_odd_div_two hm1, mul_left_comm, two_mul_odd_div_two hn1, two_mul_odd_div_two (Nat.odd_mul_odd hm1 hn1), Nat.mul_sub, mul_one, ← Nat.add_sub_assoc (by omega), Nat.sub_add_cancel (Nat.le_mul_of_pos_right m hn0)] theorem odd_of_mod_four_eq_one {n : ℕ} : n % 4 = 1 → n % 2 = 1 := by simpa [ModEq] using @ModEq.of_mul_left 2 n 1 2 theorem odd_of_mod_four_eq_three {n : ℕ} : n % 4 = 3 → n % 2 = 1 := by simpa [ModEq] using @ModEq.of_mul_left 2 n 3 2 /-- A natural number is odd iff it has residue `1` or `3` mod `4`. -/ theorem odd_mod_four_iff {n : ℕ} : n % 2 = 1 ↔ n % 4 = 1 ∨ n % 4 = 3 := have help : ∀ m : ℕ, m < 4 → m % 2 = 1 → m = 1 ∨ m = 3 := by decide ⟨fun hn => help (n % 4) (mod_lt n (by omega)) <| (mod_mod_of_dvd n (by decide : 2 ∣ 4)).trans hn, fun h => Or.elim h odd_of_mod_four_eq_one odd_of_mod_four_eq_three⟩ lemma mod_eq_of_modEq {a b n} (h : a ≡ b [MOD n]) (hb : b < n) : a % n = b := Eq.trans h (mod_eq_of_lt hb) end Nat
Mathlib/Data/Nat/ModEq.lean
527
531
/- Copyright (c) 2021 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer -/ import Mathlib.LinearAlgebra.Contraction /-! # The coevaluation map on finite dimensional vector spaces Given a finite dimensional vector space `V` over a field `K` this describes the canonical linear map from `K` to `V ⊗ Dual K V` which corresponds to the identity function on `V`. ## Tags coevaluation, dual module, tensor product ## Future work * Prove that this is independent of the choice of basis on `V`. -/ noncomputable section section coevaluation open TensorProduct Module open TensorProduct universe u v variable (K : Type u) [Field K] variable (V : Type v) [AddCommGroup V] [Module K V] [FiniteDimensional K V] /-- The coevaluation map is a linear map from a field `K` to a finite dimensional vector space `V`. -/ def coevaluation : K →ₗ[K] V ⊗[K] Module.Dual K V := let bV := Basis.ofVectorSpace K V (Basis.singleton Unit K).constr K fun _ => ∑ i : Basis.ofVectorSpaceIndex K V, bV i ⊗ₜ[K] bV.coord i theorem coevaluation_apply_one : (coevaluation K V) (1 : K) = let bV := Basis.ofVectorSpace K V ∑ i : Basis.ofVectorSpaceIndex K V, bV i ⊗ₜ[K] bV.coord i := by simp only [coevaluation, id] rw [(Basis.singleton Unit K).constr_apply_fintype K] simp only [Fintype.univ_punit, Finset.sum_const, one_smul, Basis.singleton_repr, Basis.equivFun_apply, Basis.coe_ofVectorSpace, one_nsmul, Finset.card_singleton] open TensorProduct /-- This lemma corresponds to one of the coherence laws for duals in rigid categories, see `CategoryTheory.Monoidal.Rigid`. -/ theorem contractLeft_assoc_coevaluation : (contractLeft K V).rTensor _ ∘ₗ (TensorProduct.assoc K _ _ _).symm.toLinearMap ∘ₗ (coevaluation K V).lTensor (Module.Dual K V) = (TensorProduct.lid K _).symm.toLinearMap ∘ₗ (TensorProduct.rid K _).toLinearMap := by letI := Classical.decEq (Basis.ofVectorSpaceIndex K V) apply TensorProduct.ext apply (Basis.ofVectorSpace K V).dualBasis.ext; intro j; apply LinearMap.ext_ring rw [LinearMap.compr₂_apply, LinearMap.compr₂_apply, TensorProduct.mk_apply] simp only [LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_toLinearMap] rw [rid_tmul, one_smul, lid_symm_apply] simp only [LinearEquiv.coe_toLinearMap, LinearMap.lTensor_tmul, coevaluation_apply_one] rw [TensorProduct.tmul_sum, map_sum]; simp only [assoc_symm_tmul] rw [map_sum]; simp only [LinearMap.rTensor_tmul, contractLeft_apply] simp only [Basis.coe_dualBasis, Basis.coord_apply, Basis.repr_self_apply, TensorProduct.ite_tmul] rw [Finset.sum_ite_eq']; simp only [Finset.mem_univ, if_true] /-- This lemma corresponds to one of the coherence laws for duals in rigid categories, see `CategoryTheory.Monoidal.Rigid`. -/ theorem contractLeft_assoc_coevaluation' : (contractLeft K V).lTensor _ ∘ₗ (TensorProduct.assoc K _ _ _).toLinearMap ∘ₗ (coevaluation K V).rTensor V = (TensorProduct.rid K _).symm.toLinearMap ∘ₗ (TensorProduct.lid K _).toLinearMap := by letI := Classical.decEq (Basis.ofVectorSpaceIndex K V)
apply TensorProduct.ext apply LinearMap.ext_ring; apply (Basis.ofVectorSpace K V).ext; intro j rw [LinearMap.compr₂_apply, LinearMap.compr₂_apply, TensorProduct.mk_apply] simp only [LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_toLinearMap] rw [lid_tmul, one_smul, rid_symm_apply] simp only [LinearEquiv.coe_toLinearMap, LinearMap.rTensor_tmul, coevaluation_apply_one] rw [TensorProduct.sum_tmul, map_sum]; simp only [assoc_tmul] rw [map_sum]; simp only [LinearMap.lTensor_tmul, contractLeft_apply] simp only [Basis.coord_apply, Basis.repr_self_apply, TensorProduct.tmul_ite] rw [Finset.sum_ite_eq]; simp only [Finset.mem_univ, if_true] end coevaluation
Mathlib/LinearAlgebra/Coevaluation.lean
81
95
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Joey van Langen, Casper Putz -/ import Mathlib.Algebra.CharP.Algebra import Mathlib.Algebra.CharP.Reduced import Mathlib.Algebra.Field.ZMod import Mathlib.Data.Nat.Prime.Int import Mathlib.Data.ZMod.ValMinAbs import Mathlib.LinearAlgebra.FreeModule.Finite.Matrix import Mathlib.FieldTheory.Finiteness import Mathlib.FieldTheory.Perfect import Mathlib.FieldTheory.Separable import Mathlib.RingTheory.IntegralDomain /-! # Finite fields This file contains basic results about finite fields. Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. See `RingTheory.IntegralDomain` for the fact that the unit group of a finite field is a cyclic group, as well as the fact that every finite integral domain is a field (`Fintype.fieldOfDomain`). ## Main results 1. `Fintype.card_units`: The unit group of a finite field has cardinality `q - 1`. 2. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is - `q-1` if `q-1 ∣ i` - `0` otherwise 3. `FiniteField.card`: The cardinality `q` is a power of the characteristic of `K`. See `FiniteField.card'` for a variant. ## Notation Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. ## Implementation notes While `Fintype Kˣ` can be inferred from `Fintype K` in the presence of `DecidableEq K`, in this file we take the `Fintype Kˣ` argument directly to reduce the chance of typeclass diamonds, as `Fintype` carries data. -/ variable {K : Type*} {R : Type*} local notation "q" => Fintype.card K open Finset open scoped Polynomial namespace FiniteField section Polynomial variable [CommRing R] [IsDomain R] open Polynomial /-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n` polynomial -/ theorem card_image_polynomial_eval [DecidableEq R] [Fintype R] {p : R[X]} (hp : 0 < p.degree) : Fintype.card R ≤ natDegree p * #(univ.image fun x => eval x p) := Finset.card_le_mul_card_image _ _ (fun a _ => calc _ = #(p - C a).roots.toFinset := congr_arg card (by simp [Finset.ext_iff, ← mem_roots_sub_C hp]) _ ≤ Multiset.card (p - C a).roots := Multiset.toFinset_card_le _ _ ≤ _ := card_roots_sub_C' hp) /-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/ theorem exists_root_sum_quadratic [Fintype R] {f g : R[X]} (hf2 : degree f = 2) (hg2 : degree g = 2) (hR : Fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 := letI := Classical.decEq R suffices ¬Disjoint (univ.image fun x : R => eval x f) (univ.image fun x : R => eval x (-g)) by simp only [disjoint_left, mem_image] at this push_neg at this rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩ exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_cancel]⟩ fun hd : Disjoint _ _ => lt_irrefl (2 * #((univ.image fun x : R => eval x f) ∪ univ.image fun x : R => eval x (-g))) <| calc 2 * #((univ.image fun x : R => eval x f) ∪ univ.image fun x : R => eval x (-g)) ≤ 2 * Fintype.card R := Nat.mul_le_mul_left _ (Finset.card_le_univ _) _ = Fintype.card R + Fintype.card R := two_mul _ _ < natDegree f * #(univ.image fun x : R => eval x f) + natDegree (-g) * #(univ.image fun x : R => eval x (-g)) := (add_lt_add_of_lt_of_le (lt_of_le_of_ne (card_image_polynomial_eval (by rw [hf2]; decide)) (mt (congr_arg (· % 2)) (by simp [natDegree_eq_of_degree_eq_some hf2, hR]))) (card_image_polynomial_eval (by rw [degree_neg, hg2]; decide))) _ = 2 * #((univ.image fun x : R => eval x f) ∪ univ.image fun x : R => eval x (-g)) := by rw [card_union_of_disjoint hd] simp [natDegree_eq_of_degree_eq_some hf2, natDegree_eq_of_degree_eq_some hg2, mul_add] end Polynomial theorem prod_univ_units_id_eq_neg_one [CommRing K] [IsDomain K] [Fintype Kˣ] : ∏ x : Kˣ, x = (-1 : Kˣ) := by classical have : (∏ x ∈ (@univ Kˣ _).erase (-1), x) = 1 := prod_involution (fun x _ => x⁻¹) (by simp) (fun a => by simp +contextual [Units.inv_eq_self_iff]) (fun a => by simp [@inv_eq_iff_eq_inv _ _ a]) (by simp) rw [← insert_erase (mem_univ (-1 : Kˣ)), prod_insert (not_mem_erase _ _), this, mul_one] theorem card_cast_subgroup_card_ne_zero [Ring K] [NoZeroDivisors K] [Nontrivial K] (G : Subgroup Kˣ) [Fintype G] : (Fintype.card G : K) ≠ 0 := by let n := Fintype.card G intro nzero have ⟨p, char_p⟩ := CharP.exists K have hd : p ∣ n := (CharP.cast_eq_zero_iff K p n).mp nzero cases CharP.char_is_prime_or_zero K p with | inr pzero => exact (Fintype.card_pos).ne' <| Nat.eq_zero_of_zero_dvd <| pzero ▸ hd | inl pprime => have fact_pprime := Fact.mk pprime -- G has an element x of order p by Cauchy's theorem have ⟨x, hx⟩ := exists_prime_orderOf_dvd_card p hd -- F has an element u (= ↑↑x) of order p let u := ((x : Kˣ) : K) have hu : orderOf u = p := by rwa [orderOf_units, Subgroup.orderOf_coe] -- u ^ p = 1 implies (u - 1) ^ p = 0 and hence u = 1 ... have h : u = 1 := by rw [← sub_left_inj, sub_self 1] apply pow_eq_zero (n := p) rw [sub_pow_char_of_commute, one_pow, ← hu, pow_orderOf_eq_one, sub_self] exact Commute.one_right u -- ... meaning x didn't have order p after all, contradiction apply pprime.one_lt.ne rw [← hu, h, orderOf_one] /-- The sum of a nontrivial subgroup of the units of a field is zero. -/ theorem sum_subgroup_units_eq_zero [Ring K] [NoZeroDivisors K] {G : Subgroup Kˣ} [Fintype G] (hg : G ≠ ⊥) : ∑ x : G, (x.val : K) = 0 := by rw [Subgroup.ne_bot_iff_exists_ne_one] at hg rcases hg with ⟨a, ha⟩ -- The action of a on G as an embedding let a_mul_emb : G ↪ G := mulLeftEmbedding a -- ... and leaves G unchanged have h_unchanged : Finset.univ.map a_mul_emb = Finset.univ := by simp -- Therefore the sum of x over a G is the sum of a x over G have h_sum_map := Finset.univ.sum_map a_mul_emb fun x => ((x : Kˣ) : K) -- ... and the former is the sum of x over G. -- By algebraic manipulation, we have Σ G, x = ∑ G, a x = a ∑ G, x simp only [h_unchanged, mulLeftEmbedding_apply, Subgroup.coe_mul, Units.val_mul, ← mul_sum, a_mul_emb] at h_sum_map -- thus one of (a - 1) or ∑ G, x is zero have hzero : (((a : Kˣ) : K) - 1) = 0 ∨ ∑ x : ↥G, ((x : Kˣ) : K) = 0 := by rw [← mul_eq_zero, sub_mul, ← h_sum_map, one_mul, sub_self] apply Or.resolve_left hzero contrapose! ha ext rwa [← sub_eq_zero] /-- The sum of a subgroup of the units of a field is 1 if the subgroup is trivial and 1 otherwise -/ @[simp] theorem sum_subgroup_units [Ring K] [NoZeroDivisors K] {G : Subgroup Kˣ} [Fintype G] [Decidable (G = ⊥)] : ∑ x : G, (x.val : K) = if G = ⊥ then 1 else 0 := by by_cases G_bot : G = ⊥ · subst G_bot simp only [univ_unique, sum_singleton, ↓reduceIte, Units.val_eq_one, OneMemClass.coe_eq_one] rw [Set.default_coe_singleton] rfl · simp only [G_bot, ite_false] exact sum_subgroup_units_eq_zero G_bot @[simp] theorem sum_subgroup_pow_eq_zero [CommRing K] [NoZeroDivisors K] {G : Subgroup Kˣ} [Fintype G] {k : ℕ} (k_pos : k ≠ 0) (k_lt_card_G : k < Fintype.card G) : ∑ x : G, ((x : Kˣ) : K) ^ k = 0 := by rw [← Nat.card_eq_fintype_card] at k_lt_card_G nontriviality K have := NoZeroDivisors.to_isDomain K rcases (exists_pow_ne_one_of_isCyclic k_pos k_lt_card_G) with ⟨a, ha⟩ rw [Finset.sum_eq_multiset_sum] have h_multiset_map : Finset.univ.val.map (fun x : G => ((x : Kˣ) : K) ^ k) = Finset.univ.val.map (fun x : G => ((x : Kˣ) : K) ^ k * ((a : Kˣ) : K) ^ k) := by simp_rw [← mul_pow] have as_comp : (fun x : ↥G => (((x : Kˣ) : K) * ((a : Kˣ) : K)) ^ k) = (fun x : ↥G => ((x : Kˣ) : K) ^ k) ∘ fun x : ↥G => x * a := by funext x simp only [Function.comp_apply, Subgroup.coe_mul, Units.val_mul] rw [as_comp, ← Multiset.map_map] congr rw [eq_comm] exact Multiset.map_univ_val_equiv (Equiv.mulRight a) have h_multiset_map_sum : (Multiset.map (fun x : G => ((x : Kˣ) : K) ^ k) Finset.univ.val).sum = (Multiset.map (fun x : G => ((x : Kˣ) : K) ^ k * ((a : Kˣ) : K) ^ k) Finset.univ.val).sum := by rw [h_multiset_map] rw [Multiset.sum_map_mul_right] at h_multiset_map_sum have hzero : (((a : Kˣ) : K) ^ k - 1 : K) * (Multiset.map (fun i : G => (i.val : K) ^ k) Finset.univ.val).sum = 0 := by rw [sub_mul, mul_comm, ← h_multiset_map_sum, one_mul, sub_self] rw [mul_eq_zero] at hzero refine hzero.resolve_left fun h => ha ?_ ext rw [← sub_eq_zero] simp_rw [SubmonoidClass.coe_pow, Units.val_pow_eq_pow_val, OneMemClass.coe_one, Units.val_one, h] section variable [GroupWithZero K] [Fintype K] theorem pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) : a ^ (q - 1) = 1 := by calc a ^ (Fintype.card K - 1) = (Units.mk0 a ha ^ (Fintype.card K - 1) : Kˣ).1 := by rw [Units.val_pow_eq_pow_val, Units.val_mk0] _ = 1 := by classical rw [← Fintype.card_units, pow_card_eq_one] rfl theorem pow_card (a : K) : a ^ q = a := by by_cases h : a = 0; · rw [h]; apply zero_pow Fintype.card_ne_zero rw [← Nat.succ_pred_eq_of_pos Fintype.card_pos, pow_succ, Nat.pred_eq_sub_one, pow_card_sub_one_eq_one a h, one_mul] theorem pow_card_pow (n : ℕ) (a : K) : a ^ q ^ n = a := by induction n with | zero => simp | succ n ih => simp [pow_succ, pow_mul, ih, pow_card] end variable (K) [Field K] [Fintype K] /-- The cardinality `q` is a power of the characteristic of `K`. -/ @[stacks 09HY "first part"] theorem card (p : ℕ) [CharP K p] : ∃ n : ℕ+, Nat.Prime p ∧ q = p ^ (n : ℕ) := by haveI hp : Fact p.Prime := ⟨CharP.char_is_prime K p⟩ letI : Module (ZMod p) K := { (ZMod.castHom dvd_rfl K : ZMod p →+* _).toModule with } obtain ⟨n, h⟩ := VectorSpace.card_fintype (ZMod p) K rw [ZMod.card] at h refine ⟨⟨n, ?_⟩, hp.1, h⟩ apply Or.resolve_left (Nat.eq_zero_or_pos n) rintro rfl rw [pow_zero] at h have : (0 : K) = 1 := by apply Fintype.card_le_one_iff.mp (le_of_eq h) exact absurd this zero_ne_one -- this statement doesn't use `q` because we want `K` to be an explicit parameter theorem card' : ∃ (p : ℕ), CharP K p ∧ ∃ (n : ℕ+), Nat.Prime p ∧ Fintype.card K = p ^ (n : ℕ) := let ⟨p, hc⟩ := CharP.exists K ⟨p, hc, @FiniteField.card K _ _ p hc⟩ lemma isPrimePow_card : IsPrimePow (Fintype.card K) := by obtain ⟨p, _, n, hp, hn⟩ := card' K exact ⟨p, n, Nat.prime_iff.mp hp, n.prop, hn.symm⟩ theorem cast_card_eq_zero : (q : K) = 0 := by simp theorem forall_pow_eq_one_iff (i : ℕ) : (∀ x : Kˣ, x ^ i = 1) ↔ q - 1 ∣ i := by classical obtain ⟨x, hx⟩ := IsCyclic.exists_generator (α := Kˣ) rw [← Nat.card_eq_fintype_card, ← Nat.card_units, ← orderOf_eq_card_of_forall_mem_zpowers hx, orderOf_dvd_iff_pow_eq_one] constructor · intro h; apply h · intro h y simp_rw [← mem_powers_iff_mem_zpowers] at hx rcases hx y with ⟨j, rfl⟩ rw [← pow_mul, mul_comm, pow_mul, h, one_pow] /-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q` is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/ theorem sum_pow_units [DecidableEq K] (i : ℕ) : (∑ x : Kˣ, (x ^ i : K)) = if q - 1 ∣ i then -1 else 0 := by let φ : Kˣ →* K := { toFun := fun x => x ^ i map_one' := by simp map_mul' := by intros; simp [mul_pow] } have : Decidable (φ = 1) := by classical infer_instance calc (∑ x : Kˣ, φ x) = if φ = 1 then Fintype.card Kˣ else 0 := sum_hom_units φ _ = if q - 1 ∣ i then -1 else 0 := by suffices q - 1 ∣ i ↔ φ = 1 by simp only [this] split_ifs; swap · exact Nat.cast_zero · rw [Fintype.card_units, Nat.cast_sub, cast_card_eq_zero, Nat.cast_one, zero_sub] show 1 ≤ q; exact Fintype.card_pos_iff.mpr ⟨0⟩ rw [← forall_pow_eq_one_iff, DFunLike.ext_iff] apply forall_congr'; intro x; simp [φ, Units.ext_iff] /-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q` is equal to `0` if `i < q - 1`. -/ theorem sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) : ∑ x : K, x ^ i = 0 := by by_cases hi : i = 0 · simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero] classical have hiq : ¬q - 1 ∣ i := by contrapose! h; exact Nat.le_of_dvd (Nat.pos_of_ne_zero hi) h let φ : Kˣ ↪ K := ⟨fun x ↦ x, Units.ext⟩ have : univ.map φ = univ \ {0} := by ext x simpa only [mem_map, mem_univ, Function.Embedding.coeFn_mk, true_and, mem_sdiff, mem_singleton, φ] using isUnit_iff_ne_zero calc ∑ x : K, x ^ i = ∑ x ∈ univ \ {(0 : K)}, x ^ i := by rw [← sum_sdiff ({0} : Finset K).subset_univ, sum_singleton, zero_pow hi, add_zero] _ = ∑ x : Kˣ, (x ^ i : K) := by simp [φ, ← this, univ.sum_map φ] _ = 0 := by rw [sum_pow_units K i, if_neg]; exact hiq section frobenius variable (R) [CommRing R] [Algebra K R] /-- If `R` is an algebra over a finite field `K`, the Frobenius `K`-algebra endomorphism of `R` is given by raising every element of `R` to its `#K`-th power. -/ @[simps!] def frobeniusAlgHom : R →ₐ[K] R where __ := powMonoidHom q map_zero' := zero_pow Fintype.card_pos.ne' map_add' _ _ := by obtain ⟨p, _, _, hp, card_eq⟩ := card' K nontriviality R have : CharP R p := charP_of_injective_algebraMap' K R p have : ExpChar R p := .prime hp simp only [OneHom.toFun_eq_coe, MonoidHom.toOneHom_coe, powMonoidHom_apply, card_eq] exact add_pow_expChar_pow .. commutes' _ := by simp [← RingHom.map_pow, pow_card] theorem coe_frobeniusAlgHom : ⇑(frobeniusAlgHom K R) = (· ^ q) := rfl /-- If `R` is a perfect ring and an algebra over a finite field `K`, the Frobenius `K`-algebra endomorphism of `R` is an automorphism. -/ @[simps!] noncomputable def frobeniusAlgEquiv (p : ℕ) [ExpChar R p] [PerfectRing R p] : R ≃ₐ[K] R := .ofBijective (frobeniusAlgHom K R) <| by obtain ⟨p', _, n, hp, card_eq⟩ := card' K rw [coe_frobeniusAlgHom, card_eq] have : ExpChar K p' := ExpChar.prime hp nontriviality R have := ExpChar.eq ‹_› (expChar_of_injective_algebraMap (algebraMap K R).injective p') subst this apply bijective_iterateFrobenius variable (L : Type*) [Field L] [Algebra K L] /-- If `L/K` is an algebraic extension of a finite field, the Frobenius `K`-algebra endomorphism of `L` is an automorphism. -/ @[simps!] noncomputable def frobeniusAlgEquivOfAlgebraic [Algebra.IsAlgebraic K L] : L ≃ₐ[K] L := (Algebra.IsAlgebraic.algEquivEquivAlgHom K L).symm (frobeniusAlgHom K L) theorem coe_frobeniusAlgEquivOfAlgebraic [Algebra.IsAlgebraic K L] : ⇑(frobeniusAlgEquivOfAlgebraic K L) = (· ^ q) := rfl variable [Finite L] open Polynomial in theorem orderOf_frobeniusAlgHom : orderOf (frobeniusAlgHom K L) = Module.finrank K L := (orderOf_eq_iff Module.finrank_pos).mpr <| by have := Fintype.ofFinite L refine ⟨DFunLike.ext _ _ fun x ↦ ?_, fun m lt pos eq ↦ ?_⟩ · simp_rw [AlgHom.coe_pow, coe_frobeniusAlgHom, pow_iterate, AlgHom.one_apply, ← Module.card_eq_pow_finrank, pow_card] have := card_le_degree_of_subset_roots (R := L) (p := X ^ q ^ m - X) (Z := univ) fun x _ ↦ by simp_rw [mem_roots', IsRoot, eval_sub, eval_pow, eval_X] have := DFunLike.congr_fun eq x rw [AlgHom.coe_pow, coe_frobeniusAlgHom, pow_iterate, AlgHom.one_apply, ← sub_eq_zero] at this refine ⟨fun h ↦ ?_, this⟩ simpa [if_neg (Nat.one_lt_pow pos.ne' Fintype.one_lt_card).ne] using congr_arg (coeff · 1) h refine this.not_lt (((natDegree_sub_le ..).trans_eq ?_).trans_lt <| (Nat.pow_lt_pow_right Fintype.one_lt_card lt).trans_eq Module.card_eq_pow_finrank.symm) simp [Nat.one_le_pow _ _ Fintype.card_pos] theorem orderOf_frobeniusAlgEquivOfAlgebraic : orderOf (frobeniusAlgEquivOfAlgebraic K L) = Module.finrank K L := by simpa [orderOf_eq_iff Module.finrank_pos, DFunLike.ext_iff] using orderOf_frobeniusAlgHom K L theorem bijective_frobeniusAlgHom_pow : Function.Bijective fun n : Fin (Module.finrank K L) ↦ frobeniusAlgHom K L ^ n.1 := let e := (finCongr <| orderOf_frobeniusAlgHom K L).symm.trans <| finEquivPowers (orderOf_pos_iff.mp <| orderOf_frobeniusAlgHom K L ▸ Module.finrank_pos) (Subtype.val_injective.comp e.injective).bijective_of_nat_card_le ((card_algHom_le_finrank K L L).trans_eq <| by simp) theorem bijective_frobeniusAlgEquivOfAlgebraic_pow : Function.Bijective fun n : Fin (Module.finrank K L) ↦ frobeniusAlgEquivOfAlgebraic K L ^ n.1 := ((Algebra.IsAlgebraic.algEquivEquivAlgHom K L).bijective.of_comp_iff' _).mp <| by simpa only [Function.comp_def, map_pow] using bijective_frobeniusAlgHom_pow K L instance (K L) [Finite L] [Field K] [Field L] [Algebra K L] : IsCyclic (L ≃ₐ[K] L) where exists_zpow_surjective := have := Finite.of_injective _ (algebraMap K L).injective have := Fintype.ofFinite K ⟨frobeniusAlgEquivOfAlgebraic K L, fun f ↦ have ⟨n, hn⟩ := (bijective_frobeniusAlgEquivOfAlgebraic_pow K L).2 f; ⟨n, hn⟩⟩ end frobenius open Polynomial section variable [Fintype K] (K' : Type*) [Field K'] {p n : ℕ} theorem X_pow_card_sub_X_natDegree_eq (hp : 1 < p) : (X ^ p - X : K'[X]).natDegree = p := by have h1 : (X : K'[X]).degree < (X ^ p : K'[X]).degree := by rw [degree_X_pow, degree_X] exact mod_cast hp rw [natDegree_eq_of_degree_eq (degree_sub_eq_left_of_degree_lt h1), natDegree_X_pow] theorem X_pow_card_pow_sub_X_natDegree_eq (hn : n ≠ 0) (hp : 1 < p) : (X ^ p ^ n - X : K'[X]).natDegree = p ^ n := X_pow_card_sub_X_natDegree_eq K' <| Nat.one_lt_pow hn hp theorem X_pow_card_sub_X_ne_zero (hp : 1 < p) : (X ^ p - X : K'[X]) ≠ 0 := ne_zero_of_natDegree_gt <| calc 1 < _ := hp _ = _ := (X_pow_card_sub_X_natDegree_eq K' hp).symm theorem X_pow_card_pow_sub_X_ne_zero (hn : n ≠ 0) (hp : 1 < p) : (X ^ p ^ n - X : K'[X]) ≠ 0 := X_pow_card_sub_X_ne_zero K' <| Nat.one_lt_pow hn hp end theorem roots_X_pow_card_sub_X : roots (X ^ q - X : K[X]) = Finset.univ.val := by classical have aux : (X ^ q - X : K[X]) ≠ 0 := X_pow_card_sub_X_ne_zero K Fintype.one_lt_card have : (roots (X ^ q - X : K[X])).toFinset = Finset.univ := by rw [eq_univ_iff_forall] intro x rw [Multiset.mem_toFinset, mem_roots aux, IsRoot.def, eval_sub, eval_pow, eval_X, sub_eq_zero, pow_card] rw [← this, Multiset.toFinset_val, eq_comm, Multiset.dedup_eq_self] apply nodup_roots rw [separable_def] convert isCoprime_one_right.neg_right (R := K[X]) using 1 rw [derivative_sub, derivative_X, derivative_X_pow, Nat.cast_card_eq_zero K, C_0, zero_mul, zero_sub] variable {K} theorem frobenius_pow {p : ℕ} [Fact p.Prime] [CharP K p] {n : ℕ} (hcard : q = p ^ n) : frobenius K p ^ n = 1 := by ext x; conv_rhs => rw [RingHom.one_def, RingHom.id_apply, ← pow_card x, hcard] clear hcard induction n with | zero => simp | succ n hn => rw [pow_succ', pow_succ, pow_mul, RingHom.mul_def, RingHom.comp_apply, frobenius_def, hn] open Polynomial theorem expand_card (f : K[X]) : expand K q f = f ^ q := by obtain ⟨p, hp⟩ := CharP.exists K letI := hp rcases FiniteField.card K p with ⟨⟨n, npos⟩, ⟨hp, hn⟩⟩ haveI : Fact p.Prime := ⟨hp⟩ dsimp at hn rw [hn, ← map_expand_pow_char, frobenius_pow hn, RingHom.one_def, map_id] end FiniteField namespace ZMod open FiniteField Polynomial theorem sq_add_sq (p : ℕ) [hp : Fact p.Prime] (x : ZMod p) : ∃ a b : ZMod p, a ^ 2 + b ^ 2 = x := by rcases hp.1.eq_two_or_odd with hp2 | hp_odd · subst p change Fin 2 at x fin_cases x · use 0; simp · use 0, 1; simp let f : (ZMod p)[X] := X ^ 2 let g : (ZMod p)[X] := X ^ 2 - C x obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 := @exists_root_sum_quadratic _ _ _ _ f g (degree_X_pow 2) (degree_X_pow_sub_C (by decide) _) (by rw [ZMod.card, hp_odd]) refine ⟨a, b, ?_⟩ rw [← sub_eq_zero] simpa only [f, g, eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab end ZMod /-- If `p` is a prime natural number and `x` is an integer number, then there exist natural numbers `a ≤ p / 2` and `b ≤ p / 2` such that `a ^ 2 + b ^ 2 ≡ x [ZMOD p]`. This is a version of `ZMod.sq_add_sq` with estimates on `a` and `b`. -/ theorem Nat.sq_add_sq_zmodEq (p : ℕ) [Fact p.Prime] (x : ℤ) : ∃ a b : ℕ, a ≤ p / 2 ∧ b ≤ p / 2 ∧ (a : ℤ) ^ 2 + (b : ℤ) ^ 2 ≡ x [ZMOD p] := by rcases ZMod.sq_add_sq p x with ⟨a, b, hx⟩ refine ⟨a.valMinAbs.natAbs, b.valMinAbs.natAbs, ZMod.natAbs_valMinAbs_le _, ZMod.natAbs_valMinAbs_le _, ?_⟩ rw [← a.coe_valMinAbs, ← b.coe_valMinAbs] at hx push_cast rw [sq_abs, sq_abs, ← ZMod.intCast_eq_intCast_iff] exact mod_cast hx /-- If `p` is a prime natural number and `x` is a natural number, then there exist natural numbers `a ≤ p / 2` and `b ≤ p / 2` such that `a ^ 2 + b ^ 2 ≡ x [MOD p]`. This is a version of `ZMod.sq_add_sq` with estimates on `a` and `b`. -/ theorem Nat.sq_add_sq_modEq (p : ℕ) [Fact p.Prime] (x : ℕ) : ∃ a b : ℕ, a ≤ p / 2 ∧ b ≤ p / 2 ∧ a ^ 2 + b ^ 2 ≡ x [MOD p] := by simpa only [← Int.natCast_modEq_iff] using Nat.sq_add_sq_zmodEq p x namespace CharP theorem sq_add_sq (R : Type*) [Ring R] [IsDomain R] (p : ℕ) [NeZero p] [CharP R p] (x : ℤ) : ∃ a b : ℕ, ((a : R) ^ 2 + (b : R) ^ 2) = x := by haveI := char_is_prime_of_pos R p obtain ⟨a, b, hab⟩ := ZMod.sq_add_sq p x refine ⟨a.val, b.val, ?_⟩ simpa using congr_arg (ZMod.castHom dvd_rfl R) hab end CharP open scoped Nat open ZMod /-- The **Fermat-Euler totient theorem**. `Nat.ModEq.pow_totient` is an alternative statement of the same theorem. -/ @[simp] theorem ZMod.pow_totient {n : ℕ} (x : (ZMod n)ˣ) : x ^ φ n = 1 := by cases n · rw [Nat.totient_zero, pow_zero] · rw [← card_units_eq_totient, pow_card_eq_one] /-- The **Fermat-Euler totient theorem**. `ZMod.pow_totient` is an alternative statement of the same theorem. -/ theorem Nat.ModEq.pow_totient {x n : ℕ} (h : Nat.Coprime x n) : x ^ φ n ≡ 1 [MOD n] := by rw [← ZMod.eq_iff_modEq_nat] let x' : Units (ZMod n) := ZMod.unitOfCoprime _ h have := ZMod.pow_totient x' apply_fun ((fun (x : Units (ZMod n)) => (x : ZMod n)) : Units (ZMod n) → ZMod n) at this simpa only [Nat.succ_eq_add_one, Nat.cast_pow, Units.val_one, Nat.cast_one, coe_unitOfCoprime, Units.val_pow_eq_pow_val] /-- For each `n ≥ 0`, the unit group of `ZMod n` is finite. -/ instance instFiniteZModUnits : (n : ℕ) → Finite (ZMod n)ˣ | 0 => Finite.of_fintype ℤˣ | _ + 1 => inferInstance open FiniteField namespace ZMod variable {p : ℕ} [Fact p.Prime] instance : Subsingleton (Subfield (ZMod p)) := subsingleton_of_bot_eq_top <| top_unique (a := ⊥) fun n _ ↦ have := zsmul_mem (one_mem (⊥ : Subfield (ZMod p))) n.val by rwa [natCast_zsmul, Nat.smul_one_eq_cast, ZMod.natCast_zmod_val] at this theorem fieldRange_castHom_eq_bot (p : ℕ) [Fact p.Prime] [DivisionRing K] [CharP K p] : (ZMod.castHom (m := p) dvd_rfl K).fieldRange = (⊥ : Subfield K) := by rw [RingHom.fieldRange_eq_map, ← Subfield.map_bot (K := ZMod p), Subsingleton.elim ⊥] /-- A variation on Fermat's little theorem. See `ZMod.pow_card_sub_one_eq_one` -/ @[simp] theorem pow_card (x : ZMod p) : x ^ p = x := by have h := FiniteField.pow_card x; rwa [ZMod.card p] at h @[simp] theorem pow_card_pow {n : ℕ} (x : ZMod p) : x ^ p ^ n = x := by induction n with | zero => simp | succ n ih => simp [pow_succ, pow_mul, ih, pow_card] @[simp] theorem frobenius_zmod (p : ℕ) [Fact p.Prime] : frobenius (ZMod p) p = RingHom.id _ := by ext a rw [frobenius_def, ZMod.pow_card, RingHom.id_apply] -- This was a `simp` lemma, but now the LHS simplifies to `φ p`. theorem card_units (p : ℕ) [Fact p.Prime] : Fintype.card (ZMod p)ˣ = p - 1 := by rw [Fintype.card_units, card] /-- **Fermat's Little Theorem**: for every unit `a` of `ZMod p`, we have `a ^ (p - 1) = 1`. -/ theorem units_pow_card_sub_one_eq_one (p : ℕ) [Fact p.Prime] (a : (ZMod p)ˣ) : a ^ (p - 1) = 1 := by rw [← card_units p, pow_card_eq_one] /-- **Fermat's Little Theorem**: for all nonzero `a : ZMod p`, we have `a ^ (p - 1) = 1`. -/ theorem pow_card_sub_one_eq_one {a : ZMod p} (ha : a ≠ 0) : a ^ (p - 1) = 1 := by have h := FiniteField.pow_card_sub_one_eq_one a ha rwa [ZMod.card p] at h lemma pow_card_sub_one (a : ZMod p) : a ^ (p - 1) = if a ≠ 0 then 1 else 0 := by split_ifs with ha · exact pow_card_sub_one_eq_one ha · simp [of_not_not ha, (Fact.out : p.Prime).one_lt, tsub_eq_zero_iff_le] theorem orderOf_units_dvd_card_sub_one (u : (ZMod p)ˣ) : orderOf u ∣ p - 1 := orderOf_dvd_of_pow_eq_one <| units_pow_card_sub_one_eq_one _ _ theorem orderOf_dvd_card_sub_one {a : ZMod p} (ha : a ≠ 0) : orderOf a ∣ p - 1 := orderOf_dvd_of_pow_eq_one <| pow_card_sub_one_eq_one ha open Polynomial theorem expand_card (f : Polynomial (ZMod p)) : expand (ZMod p) p f = f ^ p := by have h := FiniteField.expand_card f; rwa [ZMod.card p] at h end ZMod /-- **Fermat's Little Theorem**: for all `a : ℤ` coprime to `p`, we have `a ^ (p - 1) ≡ 1 [ZMOD p]`. -/ theorem Int.ModEq.pow_card_sub_one_eq_one {p : ℕ} (hp : Nat.Prime p) {n : ℤ} (hpn : IsCoprime n p) : n ^ (p - 1) ≡ 1 [ZMOD p] := by haveI : Fact p.Prime := ⟨hp⟩ have : ¬(n : ZMod p) = 0 := by rw [CharP.intCast_eq_zero_iff _ p, ← (Nat.prime_iff_prime_int.mp hp).coprime_iff_not_dvd] · exact hpn.symm simpa [← ZMod.intCast_eq_intCast_iff] using ZMod.pow_card_sub_one_eq_one this theorem pow_pow_modEq_one (p m a : ℕ) : (1 + p * a) ^ (p ^ m) ≡ 1 [MOD p ^ m] := by
induction' m with m hm · exact Nat.modEq_one · rw [Nat.ModEq.comm, add_comm, Nat.modEq_iff_dvd' (Nat.one_le_pow' _ _)] at hm obtain ⟨d, hd⟩ := hm rw [tsub_eq_iff_eq_add_of_le (Nat.one_le_pow' _ _), add_comm] at hd rw [pow_succ, pow_mul, hd, add_pow, Finset.sum_range_succ', pow_zero, one_mul, one_pow, one_mul, Nat.choose_zero_right, Nat.cast_one] refine Nat.ModEq.add_right 1 (Nat.modEq_zero_iff_dvd.mpr ?_) simp_rw [one_pow, mul_one, pow_succ', mul_assoc, ← Finset.mul_sum] refine mul_dvd_mul_left (p ^ m) (dvd_mul_of_dvd_right (Finset.dvd_sum fun k hk ↦ ?_) d) cases m · rw [pow_zero, pow_one, one_mul, add_comm, add_left_inj] at hd cases k <;> simp [← hd, mul_assoc, pow_succ'] · cases k <;> simp [mul_assoc, pow_succ'] theorem ZMod.eq_one_or_isUnit_sub_one {n p k : ℕ} [Fact p.Prime] (hn : n = p ^ k) (a : ZMod n) (ha : (orderOf a).Coprime n) : a = 1 ∨ IsUnit (a - 1) := by rcases eq_or_ne n 0 with rfl | hn0 · exact Or.inl (orderOf_eq_one_iff.mp ((orderOf a).coprime_zero_right.mp ha)) rcases eq_or_ne a 0 with rfl | ha0 · exact Or.inr (zero_sub (1 : ZMod n) ▸ isUnit_neg_one) have : NeZero n := ⟨hn0⟩
Mathlib/FieldTheory/Finite/Basic.lean
623
644
/- Copyright (c) 2023 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.MeasureTheory.Integral.IntegralEqImproper /-! # Integrals against peak functions A sequence of peak functions is a sequence of functions with average one concentrating around a point `x₀`. Given such a sequence `φₙ`, then `∫ φₙ g` tends to `g x₀` in many situations, with a whole zoo of possible assumptions on `φₙ` and `g`. This file is devoted to such results. Such functions are also called approximations of unity, or approximations of identity. ## Main results * `tendsto_setIntegral_peak_smul_of_integrableOn_of_tendsto`: If a sequence of peak functions `φᵢ` converges uniformly to zero away from a point `x₀`, and `g` is integrable and continuous at `x₀`, then `∫ φᵢ • g` converges to `g x₀`. * `tendsto_setIntegral_pow_smul_of_unique_maximum_of_isCompact_of_continuousOn`: If a continuous function `c` realizes its maximum at a unique point `x₀` in a compact set `s`, then the sequence of functions `(c x) ^ n / ∫ (c x) ^ n` is a sequence of peak functions concentrating around `x₀`. Therefore, `∫ (c x) ^ n * g / ∫ (c x) ^ n` converges to `g x₀` if `g` is continuous on `s`. * `tendsto_integral_comp_smul_smul_of_integrable`: If a nonnegative function `φ` has integral one and decays quickly enough at infinity, then its renormalizations `x ↦ c ^ d * φ (c • x)` form a sequence of peak functions as `c → ∞`. Therefore, `∫ (c ^ d * φ (c • x)) • g x` converges to `g 0` as `c → ∞` if `g` is continuous at `0` and integrable. Note that there are related results about convolution with respect to peak functions in the file `Mathlib.Analysis.Convolution`, such as `MeasureTheory.convolution_tendsto_right` there. -/ open Set Filter MeasureTheory MeasureTheory.Measure TopologicalSpace Metric open scoped Topology ENNReal /-! ### General convergent result for integrals against a sequence of peak functions -/ open Set variable {α E ι : Type*} {hm : MeasurableSpace α} {μ : Measure α} [TopologicalSpace α] [BorelSpace α] [NormedAddCommGroup E] [NormedSpace ℝ E] {g : α → E} {l : Filter ι} {x₀ : α} {s t : Set α} {φ : ι → α → ℝ} {a : E} /-- If a sequence of peak functions `φᵢ` converges uniformly to zero away from a point `x₀`, and `g` is integrable and has a limit at `x₀`, then `φᵢ • g` is eventually integrable. -/ theorem integrableOn_peak_smul_of_integrableOn_of_tendsto (hs : MeasurableSet s) (h'st : t ∈ 𝓝[s] x₀) (hlφ : ∀ u : Set α, IsOpen u → x₀ ∈ u → TendstoUniformlyOn φ 0 l (s \ u)) (hiφ : Tendsto (fun i ↦ ∫ x in t, φ i x ∂μ) l (𝓝 1)) (h'iφ : ∀ᶠ i in l, AEStronglyMeasurable (φ i) (μ.restrict s)) (hmg : IntegrableOn g s μ) (hcg : Tendsto g (𝓝[s] x₀) (𝓝 a)) : ∀ᶠ i in l, IntegrableOn (fun x => φ i x • g x) s μ := by obtain ⟨u, u_open, x₀u, ut, hu⟩ : ∃ u, IsOpen u ∧ x₀ ∈ u ∧ s ∩ u ⊆ t ∧ ∀ x ∈ u ∩ s, g x ∈ ball a 1 := by rcases mem_nhdsWithin.1 (Filter.inter_mem h'st (hcg (ball_mem_nhds _ zero_lt_one))) with ⟨u, u_open, x₀u, hu⟩ refine ⟨u, u_open, x₀u, ?_, hu.trans inter_subset_right⟩ rw [inter_comm] exact hu.trans inter_subset_left rw [tendsto_iff_norm_sub_tendsto_zero] at hiφ filter_upwards [tendstoUniformlyOn_iff.1 (hlφ u u_open x₀u) 1 zero_lt_one, (tendsto_order.1 hiφ).2 1 zero_lt_one, h'iφ] with i hi h'i h''i have I : IntegrableOn (φ i) t μ := .of_integral_ne_zero (fun h ↦ by simp [h] at h'i) have A : IntegrableOn (fun x => φ i x • g x) (s \ u) μ := by refine Integrable.smul_of_top_right (hmg.mono diff_subset le_rfl) ?_ apply memLp_top_of_bound (h''i.mono_set diff_subset) 1 filter_upwards [self_mem_ae_restrict (hs.diff u_open.measurableSet)] with x hx simpa only [Pi.zero_apply, dist_zero_left] using (hi x hx).le have B : IntegrableOn (fun x => φ i x • g x) (s ∩ u) μ := by apply Integrable.smul_of_top_left · exact IntegrableOn.mono_set I ut · apply memLp_top_of_bound (hmg.mono_set inter_subset_left).aestronglyMeasurable (‖a‖ + 1) filter_upwards [self_mem_ae_restrict (hs.inter u_open.measurableSet)] with x hx rw [inter_comm] at hx exact (norm_lt_of_mem_ball (hu x hx)).le convert A.union B simp only [diff_union_inter] /-- If a sequence of peak functions `φᵢ` converges uniformly to zero away from a point `x₀` and its integral on some finite-measure neighborhood of `x₀` converges to `1`, and `g` is integrable and has a limit `a` at `x₀`, then `∫ φᵢ • g` converges to `a`. Auxiliary lemma where one assumes additionally `a = 0`. -/ theorem tendsto_setIntegral_peak_smul_of_integrableOn_of_tendsto_aux (hs : MeasurableSet s) (ht : MeasurableSet t) (hts : t ⊆ s) (h'ts : t ∈ 𝓝[s] x₀) (hnφ : ∀ᶠ i in l, ∀ x ∈ s, 0 ≤ φ i x) (hlφ : ∀ u : Set α, IsOpen u → x₀ ∈ u → TendstoUniformlyOn φ 0 l (s \ u)) (hiφ : Tendsto (fun i ↦ ∫ x in t, φ i x ∂μ) l (𝓝 1)) (h'iφ : ∀ᶠ i in l, AEStronglyMeasurable (φ i) (μ.restrict s)) (hmg : IntegrableOn g s μ) (hcg : Tendsto g (𝓝[s] x₀) (𝓝 0)) : Tendsto (fun i : ι => ∫ x in s, φ i x • g x ∂μ) l (𝓝 0) := by refine Metric.tendsto_nhds.2 fun ε εpos => ?_ obtain ⟨δ, hδ, δpos, δone⟩ : ∃ δ, (δ * ∫ x in s, ‖g x‖ ∂μ) + 2 * δ < ε ∧ 0 < δ ∧ δ < 1 := by have A : Tendsto (fun δ => (δ * ∫ x in s, ‖g x‖ ∂μ) + 2 * δ) (𝓝[>] 0) (𝓝 ((0 * ∫ x in s, ‖g x‖ ∂μ) + 2 * 0)) := by apply Tendsto.mono_left _ nhdsWithin_le_nhds exact (tendsto_id.mul tendsto_const_nhds).add (tendsto_id.const_mul _) rw [zero_mul, zero_add, mul_zero] at A have : Ioo (0 : ℝ) 1 ∈ 𝓝[>] 0 := Ioo_mem_nhdsGT zero_lt_one rcases (((tendsto_order.1 A).2 ε εpos).and this).exists with ⟨δ, hδ, h'δ⟩ exact ⟨δ, hδ, h'δ.1, h'δ.2⟩ suffices ∀ᶠ i in l, ‖∫ x in s, φ i x • g x ∂μ‖ ≤ (δ * ∫ x in s, ‖g x‖ ∂μ) + 2 * δ by filter_upwards [this] with i hi simp only [dist_zero_right] exact hi.trans_lt hδ obtain ⟨u, u_open, x₀u, ut, hu⟩ : ∃ u, IsOpen u ∧ x₀ ∈ u ∧ s ∩ u ⊆ t ∧ ∀ x ∈ u ∩ s, g x ∈ ball 0 δ := by rcases mem_nhdsWithin.1 (Filter.inter_mem h'ts (hcg (ball_mem_nhds _ δpos))) with ⟨u, u_open, x₀u, hu⟩ refine ⟨u, u_open, x₀u, ?_, hu.trans inter_subset_right⟩ rw [inter_comm] exact hu.trans inter_subset_left filter_upwards [tendstoUniformlyOn_iff.1 (hlφ u u_open x₀u) δ δpos, (tendsto_order.1 (tendsto_iff_norm_sub_tendsto_zero.1 hiφ)).2 δ δpos, hnφ, integrableOn_peak_smul_of_integrableOn_of_tendsto hs h'ts hlφ hiφ h'iφ hmg hcg] with i hi h'i hφpos h''i have I : IntegrableOn (φ i) t μ := by apply Integrable.of_integral_ne_zero (fun h ↦ ?_) simp [h] at h'i linarith have B : ‖∫ x in s ∩ u, φ i x • g x ∂μ‖ ≤ 2 * δ := calc ‖∫ x in s ∩ u, φ i x • g x ∂μ‖ ≤ ∫ x in s ∩ u, ‖φ i x • g x‖ ∂μ := norm_integral_le_integral_norm _ _ ≤ ∫ x in s ∩ u, ‖φ i x‖ * δ ∂μ := by refine setIntegral_mono_on ?_ ?_ (hs.inter u_open.measurableSet) fun x hx => ?_ · exact IntegrableOn.mono_set h''i.norm inter_subset_left · exact IntegrableOn.mono_set (I.norm.mul_const _) ut rw [norm_smul] apply mul_le_mul_of_nonneg_left _ (norm_nonneg _) rw [inter_comm] at hu exact (mem_ball_zero_iff.1 (hu x hx)).le _ ≤ ∫ x in t, ‖φ i x‖ * δ ∂μ := by apply setIntegral_mono_set · exact I.norm.mul_const _ · exact Eventually.of_forall fun x => mul_nonneg (norm_nonneg _) δpos.le · exact Eventually.of_forall ut _ = ∫ x in t, φ i x * δ ∂μ := by apply setIntegral_congr_fun ht fun x hx => ?_ rw [Real.norm_of_nonneg (hφpos _ (hts hx))] _ = (∫ x in t, φ i x ∂μ) * δ := by rw [integral_mul_const] _ ≤ 2 * δ := by gcongr; linarith [(le_abs_self _).trans h'i.le] have C : ‖∫ x in s \ u, φ i x • g x ∂μ‖ ≤ δ * ∫ x in s, ‖g x‖ ∂μ := calc ‖∫ x in s \ u, φ i x • g x ∂μ‖ ≤ ∫ x in s \ u, ‖φ i x • g x‖ ∂μ := norm_integral_le_integral_norm _ _ ≤ ∫ x in s \ u, δ * ‖g x‖ ∂μ := by refine setIntegral_mono_on ?_ ?_ (hs.diff u_open.measurableSet) fun x hx => ?_ · exact IntegrableOn.mono_set h''i.norm diff_subset · exact IntegrableOn.mono_set (hmg.norm.const_mul _) diff_subset rw [norm_smul] apply mul_le_mul_of_nonneg_right _ (norm_nonneg _) simpa only [Pi.zero_apply, dist_zero_left] using (hi x hx).le _ ≤ δ * ∫ x in s, ‖g x‖ ∂μ := by rw [integral_const_mul] apply mul_le_mul_of_nonneg_left (setIntegral_mono_set hmg.norm _ _) δpos.le · filter_upwards with x using norm_nonneg _ · filter_upwards using diff_subset (s := s) (t := u) calc ‖∫ x in s, φ i x • g x ∂μ‖ = ‖(∫ x in s \ u, φ i x • g x ∂μ) + ∫ x in s ∩ u, φ i x • g x ∂μ‖ := by conv_lhs => rw [← diff_union_inter s u] rw [setIntegral_union disjoint_sdiff_inter (hs.inter u_open.measurableSet) (h''i.mono_set diff_subset) (h''i.mono_set inter_subset_left)] _ ≤ ‖∫ x in s \ u, φ i x • g x ∂μ‖ + ‖∫ x in s ∩ u, φ i x • g x ∂μ‖ := norm_add_le _ _ _ ≤ (δ * ∫ x in s, ‖g x‖ ∂μ) + 2 * δ := add_le_add C B variable [CompleteSpace E] /-- If a sequence of peak functions `φᵢ` converges uniformly to zero away from a point `x₀` and its integral on some finite-measure neighborhood of `x₀` converges to `1`, and `g` is integrable and has a limit `a` at `x₀`, then `∫ φᵢ • g` converges to `a`. Version localized to a subset. -/ theorem tendsto_setIntegral_peak_smul_of_integrableOn_of_tendsto (hs : MeasurableSet s) {t : Set α} (ht : MeasurableSet t) (hts : t ⊆ s) (h'ts : t ∈ 𝓝[s] x₀) (h't : μ t ≠ ∞) (hnφ : ∀ᶠ i in l, ∀ x ∈ s, 0 ≤ φ i x) (hlφ : ∀ u : Set α, IsOpen u → x₀ ∈ u → TendstoUniformlyOn φ 0 l (s \ u)) (hiφ : Tendsto (fun i ↦ ∫ x in t, φ i x ∂μ) l (𝓝 1)) (h'iφ : ∀ᶠ i in l, AEStronglyMeasurable (φ i) (μ.restrict s)) (hmg : IntegrableOn g s μ) (hcg : Tendsto g (𝓝[s] x₀) (𝓝 a)) : Tendsto (fun i : ι ↦ ∫ x in s, φ i x • g x ∂μ) l (𝓝 a) := by let h := g - t.indicator (fun _ ↦ a) have A : Tendsto (fun i : ι => (∫ x in s, φ i x • h x ∂μ) + (∫ x in t, φ i x ∂μ) • a) l (𝓝 (0 + (1 : ℝ) • a)) := by refine Tendsto.add ?_ (Tendsto.smul hiφ tendsto_const_nhds) apply tendsto_setIntegral_peak_smul_of_integrableOn_of_tendsto_aux hs ht hts h'ts hnφ hlφ hiφ h'iφ · apply hmg.sub simp only [integrable_indicator_iff ht, integrableOn_const, ht, Measure.restrict_apply] right exact lt_of_le_of_lt (measure_mono inter_subset_left) (h't.lt_top) · rw [← sub_self a] apply Tendsto.sub hcg apply tendsto_const_nhds.congr' filter_upwards [h'ts] with x hx using by simp [hx] simp only [one_smul, zero_add] at A refine Tendsto.congr' ?_ A filter_upwards [integrableOn_peak_smul_of_integrableOn_of_tendsto hs h'ts hlφ hiφ h'iφ hmg hcg, (tendsto_order.1 (tendsto_iff_norm_sub_tendsto_zero.1 hiφ)).2 1 zero_lt_one] with i hi h'i simp only [h, Pi.sub_apply, smul_sub, ← indicator_smul_apply] rw [integral_sub hi, setIntegral_indicator ht, inter_eq_right.mpr hts, integral_smul_const, sub_add_cancel] rw [integrable_indicator_iff ht] apply Integrable.smul_const rw [restrict_restrict ht, inter_eq_left.mpr hts] exact .of_integral_ne_zero (fun h ↦ by simp [h] at h'i) /-- If a sequence of peak functions `φᵢ` converges uniformly to zero away from a point `x₀` and its integral on some finite-measure neighborhood of `x₀` converges to `1`, and `g` is integrable and has a limit `a` at `x₀`, then `∫ φᵢ • g` converges to `a`. -/ theorem tendsto_integral_peak_smul_of_integrable_of_tendsto {t : Set α} (ht : MeasurableSet t) (h'ts : t ∈ 𝓝 x₀) (h't : μ t ≠ ∞) (hnφ : ∀ᶠ i in l, ∀ x, 0 ≤ φ i x) (hlφ : ∀ u : Set α, IsOpen u → x₀ ∈ u → TendstoUniformlyOn φ 0 l uᶜ) (hiφ : Tendsto (fun i ↦ ∫ x in t, φ i x ∂μ) l (𝓝 1)) (h'iφ : ∀ᶠ i in l, AEStronglyMeasurable (φ i) μ) (hmg : Integrable g μ) (hcg : Tendsto g (𝓝 x₀) (𝓝 a)) : Tendsto (fun i : ι ↦ ∫ x, φ i x • g x ∂μ) l (𝓝 a) := by suffices Tendsto (fun i : ι ↦ ∫ x in univ, φ i x • g x ∂μ) l (𝓝 a) by simpa exact tendsto_setIntegral_peak_smul_of_integrableOn_of_tendsto MeasurableSet.univ ht (x₀ := x₀) (subset_univ _) (by simpa [nhdsWithin_univ]) h't (by simpa) (by simpa [← compl_eq_univ_diff] using hlφ) hiφ (by simpa) (by simpa) (by simpa [nhdsWithin_univ]) /-! ### Peak functions of the form `x ↦ (c x) ^ n / ∫ (c y) ^ n` -/
/-- If a continuous function `c` realizes its maximum at a unique point `x₀` in a compact set `s`, then the sequence of functions `(c x) ^ n / ∫ (c x) ^ n` is a sequence of peak functions concentrating around `x₀`. Therefore, `∫ (c x) ^ n * g / ∫ (c x) ^ n` converges to `g x₀` if `g` is integrable on `s` and continuous at `x₀`. Version assuming that `μ` gives positive mass to all neighborhoods of `x₀` within `s`. For a less precise but more usable version, see `tendsto_setIntegral_pow_smul_of_unique_maximum_of_isCompact_of_continuousOn`. -/ theorem tendsto_setIntegral_pow_smul_of_unique_maximum_of_isCompact_of_measure_nhdsWithin_pos [MetrizableSpace α] [IsLocallyFiniteMeasure μ] (hs : IsCompact s) (hμ : ∀ u, IsOpen u → x₀ ∈ u → 0 < μ (u ∩ s)) {c : α → ℝ} (hc : ContinuousOn c s)
Mathlib/MeasureTheory/Integral/PeakFunction.lean
235
247
/- 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.AlgebraicTopology.DoldKan.FunctorN /-! # Comparison with the normalized Moore complex functor In this file, we show that when the category `A` is abelian, there is an isomorphism `N₁_iso_normalizedMooreComplex_comp_toKaroubi` between the functor `N₁ : SimplicialObject A ⥤ Karoubi (ChainComplex A ℕ)` defined in `FunctorN.lean` and the composition of `normalizedMooreComplex A` with the inclusion `ChainComplex A ℕ ⥤ Karoubi (ChainComplex A ℕ)`. This isomorphism shall be used in `Equivalence.lean` in order to obtain the Dold-Kan equivalence `CategoryTheory.Abelian.DoldKan.equivalence : SimplicialObject A ≌ ChainComplex A ℕ` with a functor (definitionally) equal to `normalizedMooreComplex A`. (See `Equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.) -/ open CategoryTheory CategoryTheory.Category CategoryTheory.Limits CategoryTheory.Subobject CategoryTheory.Idempotents DoldKan noncomputable section namespace AlgebraicTopology namespace DoldKan universe v variable {A : Type*} [Category A] [Abelian A] {X : SimplicialObject A} theorem HigherFacesVanish.inclusionOfMooreComplexMap (n : ℕ) : HigherFacesVanish (n + 1) ((inclusionOfMooreComplexMap X).f (n + 1)) := fun j _ => by dsimp [AlgebraicTopology.inclusionOfMooreComplexMap, NormalizedMooreComplex.objX] rw [← factorThru_arrow _ _ (finset_inf_arrow_factors Finset.univ _ j (by simp only [Finset.mem_univ])), assoc, kernelSubobject_arrow_comp, comp_zero] theorem factors_normalizedMooreComplex_PInfty (n : ℕ) : Subobject.Factors (NormalizedMooreComplex.objX X n) (PInfty.f n) := by rcases n with _|n · apply top_factors · rw [PInfty_f, NormalizedMooreComplex.objX, finset_inf_factors] intro i _ apply kernelSubobject_factors exact (HigherFacesVanish.of_P (n + 1) n) i le_add_self /-- `PInfty` factors through the normalized Moore complex -/ @[simps!] def PInftyToNormalizedMooreComplex (X : SimplicialObject A) : K[X] ⟶ N[X] := ChainComplex.ofHom _ _ _ _ _ _ (fun n => factorThru _ _ (factors_normalizedMooreComplex_PInfty n)) fun n => by rw [← cancel_mono (NormalizedMooreComplex.objX X n).arrow, assoc, assoc, factorThru_arrow, ← inclusionOfMooreComplexMap_f, ← normalizedMooreComplex_objD, ← (inclusionOfMooreComplexMap X).comm (n + 1) n, inclusionOfMooreComplexMap_f, factorThru_arrow_assoc, ← alternatingFaceMapComplex_obj_d] exact PInfty.comm (n + 1) n @[reassoc (attr := simp)] theorem PInftyToNormalizedMooreComplex_comp_inclusionOfMooreComplexMap (X : SimplicialObject A) : PInftyToNormalizedMooreComplex X ≫ inclusionOfMooreComplexMap X = PInfty := by aesop_cat @[reassoc (attr := simp)] theorem PInftyToNormalizedMooreComplex_naturality {X Y : SimplicialObject A} (f : X ⟶ Y) : AlternatingFaceMapComplex.map f ≫ PInftyToNormalizedMooreComplex Y = PInftyToNormalizedMooreComplex X ≫ NormalizedMooreComplex.map f := by aesop_cat @[reassoc (attr := simp)] theorem PInfty_comp_PInftyToNormalizedMooreComplex (X : SimplicialObject A) : PInfty ≫ PInftyToNormalizedMooreComplex X = PInftyToNormalizedMooreComplex X := by aesop_cat @[reassoc (attr := simp)] theorem inclusionOfMooreComplexMap_comp_PInfty (X : SimplicialObject A) : inclusionOfMooreComplexMap X ≫ PInfty = inclusionOfMooreComplexMap X := by ext (_|n) · dsimp simp only [comp_id] · exact (HigherFacesVanish.inclusionOfMooreComplexMap n).comp_P_eq_self instance : Mono (inclusionOfMooreComplexMap X) := ⟨fun _ _ hf => by ext n dsimp ext exact HomologicalComplex.congr_hom hf n⟩
/-- `inclusionOfMooreComplexMap X` is a split mono. -/ def splitMonoInclusionOfMooreComplexMap (X : SimplicialObject A) : SplitMono (inclusionOfMooreComplexMap X) where retraction := PInftyToNormalizedMooreComplex X id := by simp only [← cancel_mono (inclusionOfMooreComplexMap X), assoc, id_comp,
Mathlib/AlgebraicTopology/DoldKan/Normalized.lean
97
102
/- Copyright (c) 2020 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import Mathlib.Algebra.GCDMonoid.Multiset import Mathlib.Algebra.GCDMonoid.Nat import Mathlib.Algebra.Group.TypeTags.Finite import Mathlib.Combinatorics.Enumerative.Partition import Mathlib.Data.List.Rotate import Mathlib.GroupTheory.Perm.Closure import Mathlib.GroupTheory.Perm.Cycle.Factors import Mathlib.Tactic.NormNum.GCD /-! # Cycle Types In this file we define the cycle type of a permutation. ## Main definitions - `Equiv.Perm.cycleType σ` where `σ` is a permutation of a `Fintype` - `Equiv.Perm.partition σ` where `σ` is a permutation of a `Fintype` ## Main results - `sum_cycleType` : The sum of `σ.cycleType` equals `σ.support.card` - `lcm_cycleType` : The lcm of `σ.cycleType` equals `orderOf σ` - `isConj_iff_cycleType_eq` : Two permutations are conjugate if and only if they have the same cycle type. - `exists_prime_orderOf_dvd_card`: For every prime `p` dividing the order of a finite group `G` there exists an element of order `p` in `G`. This is known as Cauchy's theorem. -/ open scoped Finset namespace Equiv.Perm open List (Vector) open Equiv List Multiset variable {α : Type*} [Fintype α] section CycleType variable [DecidableEq α] /-- The cycle type of a permutation -/ def cycleType (σ : Perm α) : Multiset ℕ := σ.cycleFactorsFinset.1.map (Finset.card ∘ support) theorem cycleType_def (σ : Perm α) : σ.cycleType = σ.cycleFactorsFinset.1.map (Finset.card ∘ support) := rfl theorem cycleType_eq' {σ : Perm α} (s : Finset (Perm α)) (h1 : ∀ f : Perm α, f ∈ s → f.IsCycle) (h2 : (s : Set (Perm α)).Pairwise Disjoint) (h0 : s.noncommProd id (h2.imp fun _ _ => Disjoint.commute) = σ) : σ.cycleType = s.1.map (Finset.card ∘ support) := by rw [cycleType_def] congr rw [cycleFactorsFinset_eq_finset] exact ⟨h1, h2, h0⟩ theorem cycleType_eq {σ : Perm α} (l : List (Perm α)) (h0 : l.prod = σ) (h1 : ∀ σ : Perm α, σ ∈ l → σ.IsCycle) (h2 : l.Pairwise Disjoint) : σ.cycleType = l.map (Finset.card ∘ support) := by have hl : l.Nodup := nodup_of_pairwise_disjoint_cycles h1 h2 rw [cycleType_eq' l.toFinset] · simp [List.dedup_eq_self.mpr hl, Function.comp_def] · simpa using h1 · simpa [hl] using h2 · simp [hl, h0] theorem CycleType.count_def {σ : Perm α} (n : ℕ) : σ.cycleType.count n = Fintype.card {c : σ.cycleFactorsFinset // #(c : Perm α).support = n } := by -- work on the LHS rw [cycleType, Multiset.count_eq_card_filter_eq] -- rewrite the `Fintype.card` as a `Finset.card` rw [Fintype.subtype_card, Finset.univ_eq_attach, Finset.filter_attach', Finset.card_map, Finset.card_attach] simp only [Function.comp_apply, Finset.card, Finset.filter_val, Multiset.filter_map, Multiset.card_map] congr 1 apply Multiset.filter_congr intro d h simp only [Function.comp_apply, eq_comm, Finset.mem_val.mp h, exists_const] @[simp] theorem cycleType_eq_zero {σ : Perm α} : σ.cycleType = 0 ↔ σ = 1 := by simp [cycleType_def, cycleFactorsFinset_eq_empty_iff] @[simp] theorem cycleType_one : (1 : Perm α).cycleType = 0 := cycleType_eq_zero.2 rfl theorem card_cycleType_eq_zero {σ : Perm α} : Multiset.card σ.cycleType = 0 ↔ σ = 1 := by rw [card_eq_zero, cycleType_eq_zero] theorem card_cycleType_pos {σ : Perm α} : 0 < Multiset.card σ.cycleType ↔ σ ≠ 1 := pos_iff_ne_zero.trans card_cycleType_eq_zero.not theorem two_le_of_mem_cycleType {σ : Perm α} {n : ℕ} (h : n ∈ σ.cycleType) : 2 ≤ n := by simp only [cycleType_def, ← Finset.mem_def, Function.comp_apply, Multiset.mem_map, mem_cycleFactorsFinset_iff] at h obtain ⟨_, ⟨hc, -⟩, rfl⟩ := h exact hc.two_le_card_support theorem one_lt_of_mem_cycleType {σ : Perm α} {n : ℕ} (h : n ∈ σ.cycleType) : 1 < n := two_le_of_mem_cycleType h theorem IsCycle.cycleType {σ : Perm α} (hσ : IsCycle σ) : σ.cycleType = {#σ.support} := cycleType_eq [σ] (mul_one σ) (fun _τ hτ => (congr_arg IsCycle (List.mem_singleton.mp hτ)).mpr hσ) (List.pairwise_singleton Disjoint σ) theorem card_cycleType_eq_one {σ : Perm α} : Multiset.card σ.cycleType = 1 ↔ σ.IsCycle := by rw [card_eq_one] simp_rw [cycleType_def, Multiset.map_eq_singleton, ← Finset.singleton_val, Finset.val_inj, cycleFactorsFinset_eq_singleton_iff] constructor · rintro ⟨_, _, ⟨h, -⟩, -⟩ exact h · intro h use #σ.support, σ simp [h] theorem Disjoint.cycleType {σ τ : Perm α} (h : Disjoint σ τ) : (σ * τ).cycleType = σ.cycleType + τ.cycleType := by rw [cycleType_def, cycleType_def, cycleType_def, h.cycleFactorsFinset_mul_eq_union, ← Multiset.map_add, Finset.union_val, Multiset.add_eq_union_iff_disjoint.mpr _] exact Finset.disjoint_val.2 h.disjoint_cycleFactorsFinset @[simp] theorem cycleType_inv (σ : Perm α) : σ⁻¹.cycleType = σ.cycleType := cycle_induction_on (P := fun τ : Perm α => τ⁻¹.cycleType = τ.cycleType) σ rfl (fun σ hσ => by simp only [hσ.cycleType, hσ.inv.cycleType, support_inv]) fun σ τ hστ _ hσ hτ => by simp only [mul_inv_rev, hστ.cycleType, hστ.symm.inv_left.inv_right.cycleType, hσ, hτ, add_comm] @[simp] theorem cycleType_conj {σ τ : Perm α} : (τ * σ * τ⁻¹).cycleType = σ.cycleType := by induction σ using cycle_induction_on with | base_one => simp | base_cycles σ hσ => rw [hσ.cycleType, hσ.conj.cycleType, card_support_conj] | induction_disjoint σ π hd _ hσ hπ => rw [← conj_mul, hd.cycleType, (hd.conj _).cycleType, hσ, hπ] theorem sum_cycleType (σ : Perm α) : σ.cycleType.sum = #σ.support := by induction σ using cycle_induction_on with | base_one => simp | base_cycles σ hσ => rw [hσ.cycleType, Multiset.sum_singleton] | induction_disjoint σ τ hd _ hσ hτ => rw [hd.cycleType, sum_add, hσ, hτ, hd.card_support_mul] theorem card_fixedPoints (σ : Equiv.Perm α) : Fintype.card (Function.fixedPoints σ) = Fintype.card α - σ.cycleType.sum := by rw [Equiv.Perm.sum_cycleType, ← Finset.card_compl, Fintype.card_ofFinset] congr; aesop theorem sign_of_cycleType' (σ : Perm α) : sign σ = (σ.cycleType.map fun n => -(-1 : ℤˣ) ^ n).prod := by induction σ using cycle_induction_on with | base_one => simp | base_cycles σ hσ => simp [hσ.cycleType, hσ.sign] | induction_disjoint σ τ hd _ hσ hτ => simp [hσ, hτ, hd.cycleType] theorem sign_of_cycleType (f : Perm α) : sign f = (-1 : ℤˣ) ^ (f.cycleType.sum + Multiset.card f.cycleType) := by rw [sign_of_cycleType'] induction' f.cycleType using Multiset.induction_on with a s ihs · rfl · rw [Multiset.map_cons, Multiset.prod_cons, Multiset.sum_cons, Multiset.card_cons, ihs] simp only [pow_add, pow_one, mul_neg_one, neg_mul, mul_neg, mul_assoc, mul_one] @[simp] theorem lcm_cycleType (σ : Perm α) : σ.cycleType.lcm = orderOf σ := by induction σ using cycle_induction_on with | base_one => simp | base_cycles σ hσ => simp [hσ.cycleType, hσ.orderOf] | induction_disjoint σ τ hd _ hσ hτ => simp [hd.cycleType, hd.orderOf, lcm_eq_nat_lcm, hσ, hτ] theorem dvd_of_mem_cycleType {σ : Perm α} {n : ℕ} (h : n ∈ σ.cycleType) : n ∣ orderOf σ := by rw [← lcm_cycleType] exact dvd_lcm h theorem orderOf_cycleOf_dvd_orderOf (f : Perm α) (x : α) : orderOf (cycleOf f x) ∣ orderOf f := by by_cases hx : f x = x · rw [← cycleOf_eq_one_iff] at hx simp [hx] · refine dvd_of_mem_cycleType ?_ rw [cycleType, Multiset.mem_map] refine ⟨f.cycleOf x, ?_, ?_⟩ · rwa [← Finset.mem_def, cycleOf_mem_cycleFactorsFinset_iff, mem_support] · simp [(isCycle_cycleOf _ hx).orderOf] theorem two_dvd_card_support {σ : Perm α} (hσ : σ ^ 2 = 1) : 2 ∣ #σ.support := (congr_arg (Dvd.dvd 2) σ.sum_cycleType).mp (Multiset.dvd_sum fun n hn => by rw [_root_.le_antisymm (Nat.le_of_dvd zero_lt_two <| (dvd_of_mem_cycleType hn).trans <| orderOf_dvd_of_pow_eq_one hσ) (two_le_of_mem_cycleType hn)]) theorem cycleType_prime_order {σ : Perm α} (hσ : (orderOf σ).Prime) : ∃ n : ℕ, σ.cycleType = Multiset.replicate (n + 1) (orderOf σ) := by refine ⟨Multiset.card σ.cycleType - 1, eq_replicate.2 ⟨?_, fun n hn ↦ ?_⟩⟩ · rw [tsub_add_cancel_of_le] rw [Nat.succ_le_iff, card_cycleType_pos, Ne, ← orderOf_eq_one_iff] exact hσ.ne_one · exact (hσ.eq_one_or_self_of_dvd n (dvd_of_mem_cycleType hn)).resolve_left (one_lt_of_mem_cycleType hn).ne' theorem pow_prime_eq_one_iff {σ : Perm α} {p : ℕ} [hp : Fact (Nat.Prime p)] : σ ^ p = 1 ↔ ∀ c ∈ σ.cycleType, c = p := by rw [← orderOf_dvd_iff_pow_eq_one, ← lcm_cycleType, Multiset.lcm_dvd] apply forall_congr' exact fun c ↦ ⟨fun hc h ↦ Or.resolve_left (hp.elim.eq_one_or_self_of_dvd c (hc h)) (Nat.ne_of_lt' (one_lt_of_mem_cycleType h)), fun hc h ↦ by rw [hc h]⟩ theorem isCycle_of_prime_order {σ : Perm α} (h1 : (orderOf σ).Prime) (h2 : #σ.support < 2 * orderOf σ) : σ.IsCycle := by obtain ⟨n, hn⟩ := cycleType_prime_order h1 rw [← σ.sum_cycleType, hn, Multiset.sum_replicate, nsmul_eq_mul, Nat.cast_id, mul_lt_mul_right (orderOf_pos σ), Nat.succ_lt_succ_iff, Nat.lt_succ_iff, Nat.le_zero] at h2 rw [← card_cycleType_eq_one, hn, card_replicate, h2] theorem cycleType_le_of_mem_cycleFactorsFinset {f g : Perm α} (hf : f ∈ g.cycleFactorsFinset) : f.cycleType ≤ g.cycleType := by have hf' := mem_cycleFactorsFinset_iff.1 hf rw [cycleType_def, cycleType_def, hf'.left.cycleFactorsFinset_eq_singleton] refine map_le_map ?_ simpa only [Finset.singleton_val, singleton_le, Finset.mem_val] using hf theorem Disjoint.cycleType_mul {f g : Perm α} (h : f.Disjoint g) : (f * g).cycleType = f.cycleType + g.cycleType := by simp only [Perm.cycleType] rw [h.cycleFactorsFinset_mul_eq_union] simp only [Finset.union_val, Function.comp_apply] rw [← Multiset.add_eq_union_iff_disjoint.mpr _, Multiset.map_add] simp only [Finset.disjoint_val, Disjoint.disjoint_cycleFactorsFinset h] theorem Disjoint.cycleType_noncommProd {ι : Type*} {k : ι → Perm α} {s : Finset ι} (hs : Set.Pairwise s fun i j ↦ Disjoint (k i) (k j)) (hs' : Set.Pairwise s fun i j ↦ Commute (k i) (k j) := hs.imp (fun _ _ ↦ Perm.Disjoint.commute)) : (s.noncommProd k hs').cycleType = s.sum fun i ↦ (k i).cycleType := by classical induction s using Finset.induction_on with | empty => simp | insert i s hi hrec => have hs' : (s : Set ι).Pairwise fun i j ↦ Disjoint (k i) (k j) := hs.mono (by simp only [Finset.coe_insert, Set.subset_insert]) rw [Finset.noncommProd_insert_of_not_mem _ _ _ _ hi, Finset.sum_insert hi] rw [Equiv.Perm.Disjoint.cycleType_mul, hrec hs'] apply disjoint_noncommProd_right intro j hj apply hs _ _ (ne_of_mem_of_not_mem hj hi).symm <;> simp only [Finset.coe_insert, Set.mem_insert_iff, Finset.mem_coe, hj, or_true, true_or] theorem cycleType_mul_inv_mem_cycleFactorsFinset_eq_sub {f g : Perm α} (hf : f ∈ g.cycleFactorsFinset) : (g * f⁻¹).cycleType = g.cycleType - f.cycleType := add_right_cancel (b := f.cycleType) <| by rw [← (disjoint_mul_inv_of_mem_cycleFactorsFinset hf).cycleType, inv_mul_cancel_right, tsub_add_cancel_of_le (cycleType_le_of_mem_cycleFactorsFinset hf)] theorem isConj_of_cycleType_eq {σ τ : Perm α} (h : cycleType σ = cycleType τ) : IsConj σ τ := by induction σ using cycle_induction_on generalizing τ with | base_one => rw [cycleType_one, eq_comm, cycleType_eq_zero] at h rw [h] | base_cycles σ hσ => have hτ := card_cycleType_eq_one.2 hσ rw [h, card_cycleType_eq_one] at hτ apply hσ.isConj hτ rwa [hσ.cycleType, hτ.cycleType, Multiset.singleton_inj] at h | induction_disjoint σ π hd hc hσ hπ => rw [hd.cycleType] at h have h' : #σ.support ∈ τ.cycleType := by simp [← h, hc.cycleType] obtain ⟨σ', hσ'l, hσ'⟩ := Multiset.mem_map.mp h' have key : IsConj (σ' * τ * σ'⁻¹) τ := (isConj_iff.2 ⟨σ', rfl⟩).symm refine IsConj.trans ?_ key rw [mul_assoc] have hs : σ.cycleType = σ'.cycleType := by rw [← Finset.mem_def, mem_cycleFactorsFinset_iff] at hσ'l rw [hc.cycleType, ← hσ', hσ'l.left.cycleType]; rfl refine hd.isConj_mul (hσ hs) (hπ ?_) ?_ · rw [cycleType_mul_inv_mem_cycleFactorsFinset_eq_sub, ← h, add_comm, hs, add_tsub_cancel_right] rwa [Finset.mem_def] · exact (disjoint_mul_inv_of_mem_cycleFactorsFinset hσ'l).symm theorem isConj_iff_cycleType_eq {σ τ : Perm α} : IsConj σ τ ↔ σ.cycleType = τ.cycleType := ⟨fun h => by obtain ⟨π, rfl⟩ := isConj_iff.1 h rw [cycleType_conj], isConj_of_cycleType_eq⟩ @[simp] theorem cycleType_extendDomain {β : Type*} [Fintype β] [DecidableEq β] {p : β → Prop} [DecidablePred p] (f : α ≃ Subtype p) {g : Perm α} : cycleType (g.extendDomain f) = cycleType g := by induction g using cycle_induction_on with | base_one => rw [extendDomain_one, cycleType_one, cycleType_one] | base_cycles σ hσ => rw [(hσ.extendDomain f).cycleType, hσ.cycleType, card_support_extend_domain] | induction_disjoint σ τ hd _ hσ hτ => rw [hd.cycleType, ← extendDomain_mul, (hd.extendDomain f).cycleType, hσ, hτ] theorem cycleType_ofSubtype {p : α → Prop} [DecidablePred p] {g : Perm (Subtype p)} : cycleType (ofSubtype g) = cycleType g := cycleType_extendDomain (Equiv.refl (Subtype p)) theorem mem_cycleType_iff {n : ℕ} {σ : Perm α} : n ∈ cycleType σ ↔ ∃ c τ, σ = c * τ ∧ Disjoint c τ ∧ IsCycle c ∧ c.support.card = n := by constructor · intro h obtain ⟨l, rfl, hlc, hld⟩ := truncCycleFactors σ rw [cycleType_eq _ rfl hlc hld, Multiset.mem_coe, List.mem_map] at h obtain ⟨c, cl, rfl⟩ := h rw [(List.perm_cons_erase cl).pairwise_iff @(Disjoint.symmetric)] at hld refine ⟨c, (l.erase c).prod, ?_, ?_, hlc _ cl, rfl⟩ · rw [← List.prod_cons, (List.perm_cons_erase cl).symm.prod_eq' (hld.imp Disjoint.commute)] · exact disjoint_prod_right _ fun g => List.rel_of_pairwise_cons hld · rintro ⟨c, t, rfl, hd, hc, rfl⟩ simp [hd.cycleType, hc.cycleType] theorem le_card_support_of_mem_cycleType {n : ℕ} {σ : Perm α} (h : n ∈ cycleType σ) : n ≤ #σ.support := (le_sum_of_mem h).trans (le_of_eq σ.sum_cycleType) theorem cycleType_of_card_le_mem_cycleType_add_two {n : ℕ} {g : Perm α} (hn2 : Fintype.card α < n + 2) (hng : n ∈ g.cycleType) : g.cycleType = {n} := by obtain ⟨c, g', rfl, hd, hc, rfl⟩ := mem_cycleType_iff.1 hng suffices g'1 : g' = 1 by rw [hd.cycleType, hc.cycleType, g'1, cycleType_one, add_zero] contrapose! hn2 with g'1 apply le_trans _ (c * g').support.card_le_univ rw [hd.card_support_mul] exact add_le_add_left (two_le_card_support_of_ne_one g'1) _ end CycleType theorem card_compl_support_modEq [DecidableEq α] {p n : ℕ} [hp : Fact p.Prime] {σ : Perm α} (hσ : σ ^ p ^ n = 1) : σ.supportᶜ.card ≡ Fintype.card α [MOD p] := by rw [Nat.modEq_iff_dvd', ← Finset.card_compl, compl_compl, ← sum_cycleType] · refine Multiset.dvd_sum fun k hk => ?_ obtain ⟨m, -, hm⟩ := (Nat.dvd_prime_pow hp.out).mp (orderOf_dvd_of_pow_eq_one hσ) obtain ⟨l, -, rfl⟩ := (Nat.dvd_prime_pow hp.out).mp ((congr_arg _ hm).mp (dvd_of_mem_cycleType hk)) exact dvd_pow_self _ fun h => (one_lt_of_mem_cycleType hk).ne <| by rw [h, pow_zero] · exact Finset.card_le_univ _ open Function in /-- The number of fixed points of a `p ^ n`-th root of the identity function over a finite set and the set's cardinality have the same residue modulo `p`, where `p` is a prime. -/ theorem card_fixedPoints_modEq [DecidableEq α] {f : Function.End α} {p n : ℕ} [hp : Fact p.Prime] (hf : f ^ p ^ n = 1) : Fintype.card α ≡ Fintype.card f.fixedPoints [MOD p] := by let σ : α ≃ α := ⟨f, f ^ (p ^ n - 1), leftInverse_iff_comp.mpr ((pow_sub_mul_pow f (Nat.one_le_pow n p hp.out.pos)).trans hf), leftInverse_iff_comp.mpr ((pow_mul_pow_sub f (Nat.one_le_pow n p hp.out.pos)).trans hf)⟩ have hσ : σ ^ p ^ n = 1 := by rw [DFunLike.ext'_iff, coe_pow] exact (hom_coe_pow (fun g : Function.End α ↦ g) rfl (fun g h ↦ rfl) f (p ^ n)).symm.trans hf suffices Fintype.card f.fixedPoints = (support σ)ᶜ.card from this ▸ (card_compl_support_modEq hσ).symm suffices f.fixedPoints = (support σ)ᶜ by simp only [this]; apply Fintype.card_coe simp [σ, Set.ext_iff, IsFixedPt] theorem exists_fixed_point_of_prime {p n : ℕ} [hp : Fact p.Prime] (hα : ¬p ∣ Fintype.card α) {σ : Perm α} (hσ : σ ^ p ^ n = 1) : ∃ a : α, σ a = a := by classical contrapose! hα simp_rw [← mem_support, ← Finset.eq_univ_iff_forall] at hα exact Nat.modEq_zero_iff_dvd.1 ((congr_arg _ (Finset.card_eq_zero.2 (compl_eq_bot.2 hα))).mp (card_compl_support_modEq hσ).symm) theorem exists_fixed_point_of_prime' {p n : ℕ} [hp : Fact p.Prime] (hα : p ∣ Fintype.card α) {σ : Perm α} (hσ : σ ^ p ^ n = 1) {a : α} (ha : σ a = a) : ∃ b : α, σ b = b ∧ b ≠ a := by classical have h : ∀ b : α, b ∈ σ.supportᶜ ↔ σ b = b := fun b => by rw [Finset.mem_compl, mem_support, Classical.not_not] obtain ⟨b, hb1, hb2⟩ := Finset.exists_ne_of_one_lt_card (hp.out.one_lt.trans_le (Nat.le_of_dvd (Finset.card_pos.mpr ⟨a, (h a).mpr ha⟩) (Nat.modEq_zero_iff_dvd.mp ((card_compl_support_modEq hσ).trans (Nat.modEq_zero_iff_dvd.mpr hα))))) a exact ⟨b, (h b).mp hb1, hb2⟩ theorem isCycle_of_prime_order' {σ : Perm α} (h1 : (orderOf σ).Prime) (h2 : Fintype.card α < 2 * orderOf σ) : σ.IsCycle := by classical exact isCycle_of_prime_order h1 (lt_of_le_of_lt σ.support.card_le_univ h2) theorem isCycle_of_prime_order'' {σ : Perm α} (h1 : (Fintype.card α).Prime) (h2 : orderOf σ = Fintype.card α) : σ.IsCycle := isCycle_of_prime_order' ((congr_arg Nat.Prime h2).mpr h1) <| by rw [← one_mul (Fintype.card α), ← h2, mul_lt_mul_right (orderOf_pos σ)] exact one_lt_two section Cauchy variable (G : Type*) [Group G] (n : ℕ) /-- The type of vectors with terms from `G`, length `n`, and product equal to `1:G`. -/ def vectorsProdEqOne : Set (List.Vector G n) := { v | v.toList.prod = 1 } namespace VectorsProdEqOne theorem mem_iff {n : ℕ} (v : List.Vector G n) : v ∈ vectorsProdEqOne G n ↔ v.toList.prod = 1 := Iff.rfl theorem zero_eq : vectorsProdEqOne G 0 = {Vector.nil} := Set.eq_singleton_iff_unique_mem.mpr ⟨Eq.refl (1 : G), fun v _ => v.eq_nil⟩ theorem one_eq : vectorsProdEqOne G 1 = {Vector.nil.cons 1} := by simp_rw [Set.eq_singleton_iff_unique_mem, mem_iff, List.Vector.toList_singleton, List.prod_singleton, List.Vector.head_cons, true_and] exact fun v hv => v.cons_head_tail.symm.trans (congr_arg₂ Vector.cons hv v.tail.eq_nil) instance zeroUnique : Unique (vectorsProdEqOne G 0) := by rw [zero_eq] exact Set.uniqueSingleton Vector.nil instance oneUnique : Unique (vectorsProdEqOne G 1) := by rw [one_eq] exact Set.uniqueSingleton (Vector.nil.cons 1) /-- Given a vector `v` of length `n`, make a vector of length `n + 1` whose product is `1`, by appending the inverse of the product of `v`. -/ @[simps] def vectorEquiv : List.Vector G n ≃ vectorsProdEqOne G (n + 1) where toFun v := ⟨v.toList.prod⁻¹ ::ᵥ v, by rw [mem_iff, Vector.toList_cons, List.prod_cons, inv_mul_cancel]⟩ invFun v := v.1.tail left_inv v := v.tail_cons v.toList.prod⁻¹ right_inv v := Subtype.ext <| calc v.1.tail.toList.prod⁻¹ ::ᵥ v.1.tail = v.1.head ::ᵥ v.1.tail := congr_arg (· ::ᵥ v.1.tail) <| Eq.symm <| eq_inv_of_mul_eq_one_left <| by rw [← List.prod_cons, ← Vector.toList_cons, v.1.cons_head_tail] exact v.2 _ = v.1 := v.1.cons_head_tail /-- Given a vector `v` of length `n` whose product is 1, make a vector of length `n - 1`, by deleting the last entry of `v`. -/ def equivVector : ∀ n, vectorsProdEqOne G n ≃ List.Vector G (n - 1) | 0 => (ofUnique (vectorsProdEqOne G 0) (vectorsProdEqOne G 1)).trans (vectorEquiv G 0).symm | (n + 1) => (vectorEquiv G n).symm instance [Fintype G] : Fintype (vectorsProdEqOne G n) := Fintype.ofEquiv (List.Vector G (n - 1)) (equivVector G n).symm theorem card [Fintype G] : Fintype.card (vectorsProdEqOne G n) = Fintype.card G ^ (n - 1) := (Fintype.card_congr (equivVector G n)).trans (card_vector (n - 1)) variable {G n} {g : G} variable (v : vectorsProdEqOne G n) (j k : ℕ) /-- Rotate a vector whose product is 1. -/ def rotate : vectorsProdEqOne G n := ⟨⟨_, (v.1.1.length_rotate k).trans v.1.2⟩, List.prod_rotate_eq_one_of_prod_eq_one v.2 k⟩ theorem rotate_zero : rotate v 0 = v := Subtype.ext (Subtype.ext v.1.1.rotate_zero) theorem rotate_rotate : rotate (rotate v j) k = rotate v (j + k) := Subtype.ext (Subtype.ext (v.1.1.rotate_rotate j k)) theorem rotate_length : rotate v n = v := Subtype.ext (Subtype.ext ((congr_arg _ v.1.2.symm).trans v.1.1.rotate_length)) end VectorsProdEqOne -- TODO: Make the `Finite` version of this theorem the default /-- For every prime `p` dividing the order of a finite group `G` there exists an element of order `p` in `G`. This is known as Cauchy's theorem. -/ theorem _root_.exists_prime_orderOf_dvd_card {G : Type*} [Group G] [Fintype G] (p : ℕ) [hp : Fact p.Prime] (hdvd : p ∣ Fintype.card G) : ∃ x : G, orderOf x = p := by have hp' : p - 1 ≠ 0 := mt tsub_eq_zero_iff_le.mp (not_le_of_lt hp.out.one_lt) have Scard := calc p ∣ Fintype.card G ^ (p - 1) := hdvd.trans (dvd_pow (dvd_refl _) hp') _ = Fintype.card (vectorsProdEqOne G p) := (VectorsProdEqOne.card G p).symm let f : ℕ → vectorsProdEqOne G p → vectorsProdEqOne G p := fun k v => VectorsProdEqOne.rotate v k have hf1 : ∀ v, f 0 v = v := VectorsProdEqOne.rotate_zero have hf2 : ∀ j k v, f k (f j v) = f (j + k) v := fun j k v => VectorsProdEqOne.rotate_rotate v j k have hf3 : ∀ v, f p v = v := VectorsProdEqOne.rotate_length let σ := Equiv.mk (f 1) (f (p - 1)) (fun s => by rw [hf2, add_tsub_cancel_of_le hp.out.one_lt.le, hf3]) fun s => by rw [hf2, tsub_add_cancel_of_le hp.out.one_lt.le, hf3] have hσ : ∀ k v, (σ ^ k) v = f k v := fun k => Nat.rec (fun v => (hf1 v).symm) (fun k hk v => by rw [pow_succ, Perm.mul_apply, hk (σ v), Nat.succ_eq_one_add, ← hf2 1 k] simp only [σ, coe_fn_mk]) k replace hσ : σ ^ p ^ 1 = 1 := Perm.ext fun v => by rw [pow_one, hσ, hf3, one_apply] let v₀ : vectorsProdEqOne G p := ⟨List.Vector.replicate p 1, (List.prod_replicate p 1).trans (one_pow p)⟩ have hv₀ : σ v₀ = v₀ := Subtype.ext (Subtype.ext (List.rotate_replicate (1 : G) p 1)) obtain ⟨v, hv1, hv2⟩ := exists_fixed_point_of_prime' Scard hσ hv₀ refine Exists.imp (fun g hg => orderOf_eq_prime ?_ fun hg' => hv2 ?_) (List.rotate_one_eq_self_iff_eq_replicate.mp (Subtype.ext_iff.mp (Subtype.ext_iff.mp hv1))) · rw [← List.prod_replicate, ← v.1.2, ← hg, show v.val.val.prod = 1 from v.2] · rw [Subtype.ext_iff_val, Subtype.ext_iff_val, hg, hg', v.1.2] simp only [v₀, List.Vector.replicate] -- TODO: Make the `Finite` version of this theorem the default /-- For every prime `p` dividing the order of a finite additive group `G` there exists an element of order `p` in `G`. This is the additive version of Cauchy's theorem. -/ theorem _root_.exists_prime_addOrderOf_dvd_card {G : Type*} [AddGroup G] [Fintype G] (p : ℕ) [Fact p.Prime] (hdvd : p ∣ Fintype.card G) : ∃ x : G, addOrderOf x = p := @exists_prime_orderOf_dvd_card (Multiplicative G) _ _ _ _ (by convert hdvd) attribute [to_additive existing] exists_prime_orderOf_dvd_card -- TODO: Make the `Finite` version of this theorem the default /-- For every prime `p` dividing the order of a finite group `G` there exists an element of order `p` in `G`. This is known as Cauchy's theorem. -/ @[to_additive] theorem _root_.exists_prime_orderOf_dvd_card' {G : Type*} [Group G] [Finite G] (p : ℕ) [hp : Fact p.Prime] (hdvd : p ∣ Nat.card G) : ∃ x : G, orderOf x = p := by have := Fintype.ofFinite G rw [Nat.card_eq_fintype_card] at hdvd exact exists_prime_orderOf_dvd_card p hdvd end Cauchy theorem subgroup_eq_top_of_swap_mem [DecidableEq α] {H : Subgroup (Perm α)} [d : DecidablePred (· ∈ H)] {τ : Perm α} (h0 : (Fintype.card α).Prime) (h1 : Fintype.card α ∣ Fintype.card H) (h2 : τ ∈ H) (h3 : IsSwap τ) : H = ⊤ := by haveI : Fact (Fintype.card α).Prime := ⟨h0⟩ obtain ⟨σ, hσ⟩ := exists_prime_orderOf_dvd_card (Fintype.card α) h1 have hσ1 : orderOf (σ : Perm α) = Fintype.card α := (Subgroup.orderOf_coe σ).trans hσ have hσ2 : IsCycle ↑σ := isCycle_of_prime_order'' h0 hσ1 have hσ3 : (σ : Perm α).support = ⊤ := Finset.eq_univ_of_card (σ : Perm α).support (hσ2.orderOf.symm.trans hσ1) have hσ4 : Subgroup.closure {↑σ, τ} = ⊤ := closure_prime_cycle_swap h0 hσ2 hσ3 h3 rw [eq_top_iff, ← hσ4, Subgroup.closure_le, Set.insert_subset_iff, Set.singleton_subset_iff] exact ⟨Subtype.mem σ, h2⟩ section Partition variable [DecidableEq α] /-- The partition corresponding to a permutation -/ def partition (σ : Perm α) : (Fintype.card α).Partition where parts := σ.cycleType + Multiset.replicate (Fintype.card α - #σ.support) 1 parts_pos {n hn} := by rcases mem_add.mp hn with hn | hn · exact zero_lt_one.trans (one_lt_of_mem_cycleType hn) · exact lt_of_lt_of_le zero_lt_one (ge_of_eq (Multiset.eq_of_mem_replicate hn)) parts_sum := by rw [sum_add, sum_cycleType, Multiset.sum_replicate, nsmul_eq_mul, Nat.cast_id, mul_one, add_tsub_cancel_of_le σ.support.card_le_univ] theorem parts_partition {σ : Perm α} : σ.partition.parts = σ.cycleType + Multiset.replicate (Fintype.card α - #σ.support) 1 := rfl theorem filter_parts_partition_eq_cycleType {σ : Perm α} : ((partition σ).parts.filter fun n => 2 ≤ n) = σ.cycleType := by rw [parts_partition, filter_add, Multiset.filter_eq_self.2 fun _ => two_le_of_mem_cycleType, Multiset.filter_eq_nil.2 fun a h => ?_, add_zero] rw [Multiset.eq_of_mem_replicate h] decide theorem partition_eq_of_isConj {σ τ : Perm α} : IsConj σ τ ↔ σ.partition = τ.partition := by rw [isConj_iff_cycleType_eq] refine ⟨fun h => ?_, fun h => ?_⟩ · rw [Nat.Partition.ext_iff, parts_partition, parts_partition, ← sum_cycleType, ← sum_cycleType, h] · rw [← filter_parts_partition_eq_cycleType, ← filter_parts_partition_eq_cycleType, h] end Partition /-! ### 3-cycles -/ /-- A three-cycle is a cycle of length 3. -/ def IsThreeCycle [DecidableEq α] (σ : Perm α) : Prop := σ.cycleType = {3} namespace IsThreeCycle variable [DecidableEq α] {σ : Perm α} theorem cycleType (h : IsThreeCycle σ) : σ.cycleType = {3} := h theorem card_support (h : IsThreeCycle σ) : #σ.support = 3 := by rw [← sum_cycleType, h.cycleType, Multiset.sum_singleton] theorem _root_.card_support_eq_three_iff : #σ.support = 3 ↔ σ.IsThreeCycle := by refine ⟨fun h => ?_, IsThreeCycle.card_support⟩ by_cases h0 : σ.cycleType = 0 · rw [← sum_cycleType, h0, sum_zero] at h exact (ne_of_lt zero_lt_three h).elim obtain ⟨n, hn⟩ := exists_mem_of_ne_zero h0 by_cases h1 : σ.cycleType.erase n = 0 · rw [← sum_cycleType, ← cons_erase hn, h1, cons_zero, Multiset.sum_singleton] at h rw [IsThreeCycle, ← cons_erase hn, h1, h, ← cons_zero] obtain ⟨m, hm⟩ := exists_mem_of_ne_zero h1 rw [← sum_cycleType, ← cons_erase hn, ← cons_erase hm, Multiset.sum_cons, Multiset.sum_cons] at h have : ∀ {k}, 2 ≤ m → 2 ≤ n → n + (m + k) = 3 → False := by omega
cases this (two_le_of_mem_cycleType (mem_of_mem_erase hm)) (two_le_of_mem_cycleType hn) h theorem isCycle (h : IsThreeCycle σ) : IsCycle σ := by
Mathlib/GroupTheory/Perm/Cycle/Type.lean
612
614
/- Copyright (c) 2021 Aaron Anderson, Jesse Michael Han, Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jesse Michael Han, Floris van Doorn -/ import Mathlib.ModelTheory.Basic /-! # Language Maps Maps between first-order languages in the style of the [Flypitch project](https://flypitch.github.io/), as well as several important maps between structures. ## Main Definitions - A `FirstOrder.Language.LHom`, denoted `L →ᴸ L'`, is a map between languages, sending the symbols of one to symbols of the same kind and arity in the other. - A `FirstOrder.Language.LEquiv`, denoted `L ≃ᴸ L'`, is an invertible language homomorphism. - `FirstOrder.Language.withConstants` is defined so that if `M` is an `L.Structure` and `A : Set M`, `L.withConstants A`, denoted `L[[A]]`, is a language which adds constant symbols for elements of `A` to `L`. ## References For the Flypitch project: - [J. Han, F. van Doorn, *A formal proof of the independence of the continuum hypothesis*] [flypitch_cpp] - [J. Han, F. van Doorn, *A formalization of forcing and the unprovability of the continuum hypothesis*][flypitch_itp] -/ universe u v u' v' w w' namespace FirstOrder namespace Language open Structure Cardinal open Cardinal variable (L : Language.{u, v}) (L' : Language.{u', v'}) {M : Type w} [L.Structure M] /-- A language homomorphism maps the symbols of one language to symbols of another. -/ structure LHom where /-- The mapping of functions -/ onFunction : ∀ ⦃n⦄, L.Functions n → L'.Functions n := by exact fun {n} => isEmptyElim /-- The mapping of relations -/ onRelation : ∀ ⦃n⦄, L.Relations n → L'.Relations n :=by exact fun {n} => isEmptyElim @[inherit_doc FirstOrder.Language.LHom] infixl:10 " →ᴸ " => LHom -- \^L variable {L L'} namespace LHom variable (ϕ : L →ᴸ L') /-- Pulls a structure back along a language map. -/ def reduct (M : Type*) [L'.Structure M] : L.Structure M where funMap f xs := funMap (ϕ.onFunction f) xs RelMap r xs := RelMap (ϕ.onRelation r) xs /-- The identity language homomorphism. -/ @[simps] protected def id (L : Language) : L →ᴸ L := ⟨fun _n => id, fun _n => id⟩ instance : Inhabited (L →ᴸ L) := ⟨LHom.id L⟩ /-- The inclusion of the left factor into the sum of two languages. -/ @[simps] protected def sumInl : L →ᴸ L.sum L' := ⟨fun _n => Sum.inl, fun _n => Sum.inl⟩ /-- The inclusion of the right factor into the sum of two languages. -/ @[simps] protected def sumInr : L' →ᴸ L.sum L' := ⟨fun _n => Sum.inr, fun _n => Sum.inr⟩ variable (L L') /-- The inclusion of an empty language into any other language. -/ @[simps] protected def ofIsEmpty [L.IsAlgebraic] [L.IsRelational] : L →ᴸ L' where variable {L L'} {L'' : Language} @[ext] protected theorem funext {F G : L →ᴸ L'} (h_fun : F.onFunction = G.onFunction) (h_rel : F.onRelation = G.onRelation) : F = G := by obtain ⟨Ff, Fr⟩ := F obtain ⟨Gf, Gr⟩ := G simp only [mk.injEq] exact And.intro h_fun h_rel instance [L.IsAlgebraic] [L.IsRelational] : Unique (L →ᴸ L') := ⟨⟨LHom.ofIsEmpty L L'⟩, fun _ => LHom.funext (Subsingleton.elim _ _) (Subsingleton.elim _ _)⟩ /-- The composition of two language homomorphisms. -/ @[simps] def comp (g : L' →ᴸ L'') (f : L →ᴸ L') : L →ᴸ L'' := ⟨fun _n F => g.1 (f.1 F), fun _ R => g.2 (f.2 R)⟩ -- added ᴸ to avoid clash with function composition @[inherit_doc] local infixl:60 " ∘ᴸ " => LHom.comp @[simp] theorem id_comp (F : L →ᴸ L') : LHom.id L' ∘ᴸ F = F := by cases F rfl @[simp] theorem comp_id (F : L →ᴸ L') : F ∘ᴸ LHom.id L = F := by cases F rfl theorem comp_assoc {L3 : Language} (F : L'' →ᴸ L3) (G : L' →ᴸ L'') (H : L →ᴸ L') : F ∘ᴸ G ∘ᴸ H = F ∘ᴸ (G ∘ᴸ H) := rfl section SumElim variable (ψ : L'' →ᴸ L') /-- A language map defined on two factors of a sum. -/ @[simps] protected def sumElim : L.sum L'' →ᴸ L' where onFunction _n := Sum.elim (fun f => ϕ.onFunction f) fun f => ψ.onFunction f onRelation _n := Sum.elim (fun f => ϕ.onRelation f) fun f => ψ.onRelation f theorem sumElim_comp_inl (ψ : L'' →ᴸ L') : ϕ.sumElim ψ ∘ᴸ LHom.sumInl = ϕ := LHom.funext (funext fun _ => rfl) (funext fun _ => rfl) theorem sumElim_comp_inr (ψ : L'' →ᴸ L') : ϕ.sumElim ψ ∘ᴸ LHom.sumInr = ψ := LHom.funext (funext fun _ => rfl) (funext fun _ => rfl) theorem sumElim_inl_inr : LHom.sumInl.sumElim LHom.sumInr = LHom.id (L.sum L') := LHom.funext (funext fun _ => Sum.elim_inl_inr) (funext fun _ => Sum.elim_inl_inr) theorem comp_sumElim {L3 : Language} (θ : L' →ᴸ L3) : θ ∘ᴸ ϕ.sumElim ψ = (θ ∘ᴸ ϕ).sumElim (θ ∘ᴸ ψ) := LHom.funext (funext fun _n => Sum.comp_elim _ _ _) (funext fun _n => Sum.comp_elim _ _ _) end SumElim section SumMap variable {L₁ L₂ : Language} (ψ : L₁ →ᴸ L₂) /-- The map between two sum-languages induced by maps on the two factors. -/ @[simps] def sumMap : L.sum L₁ →ᴸ L'.sum L₂ where onFunction _n := Sum.map (fun f => ϕ.onFunction f) fun f => ψ.onFunction f onRelation _n := Sum.map (fun f => ϕ.onRelation f) fun f => ψ.onRelation f @[simp] theorem sumMap_comp_inl : ϕ.sumMap ψ ∘ᴸ LHom.sumInl = LHom.sumInl ∘ᴸ ϕ := LHom.funext (funext fun _ => rfl) (funext fun _ => rfl) @[simp] theorem sumMap_comp_inr : ϕ.sumMap ψ ∘ᴸ LHom.sumInr = LHom.sumInr ∘ᴸ ψ := LHom.funext (funext fun _ => rfl) (funext fun _ => rfl) end SumMap /-- A language homomorphism is injective when all the maps between symbol types are. -/ protected structure Injective : Prop where onFunction {n} : Function.Injective fun f : L.Functions n => onFunction ϕ f onRelation {n} : Function.Injective fun R : L.Relations n => onRelation ϕ R /-- Pulls an `L`-structure along a language map `ϕ : L →ᴸ L'`, and then expands it to an `L'`-structure arbitrarily. -/ noncomputable def defaultExpansion (ϕ : L →ᴸ L') [∀ (n) (f : L'.Functions n), Decidable (f ∈ Set.range fun f : L.Functions n => onFunction ϕ f)] [∀ (n) (r : L'.Relations n), Decidable (r ∈ Set.range fun r : L.Relations n => onRelation ϕ r)] (M : Type*) [Inhabited M] [L.Structure M] : L'.Structure M where funMap {n} f xs := if h' : f ∈ Set.range fun f : L.Functions n => onFunction ϕ f then funMap h'.choose xs else default RelMap {n} r xs := if h' : r ∈ Set.range fun r : L.Relations n => onRelation ϕ r then RelMap h'.choose xs else default /-- A language homomorphism is an expansion on a structure if it commutes with the interpretation of all symbols on that structure. -/ class IsExpansionOn (M : Type*) [L.Structure M] [L'.Structure M] : Prop where map_onFunction : ∀ {n} (f : L.Functions n) (x : Fin n → M), funMap (ϕ.onFunction f) x = funMap f x := by exact fun {n} => isEmptyElim map_onRelation : ∀ {n} (R : L.Relations n) (x : Fin n → M), RelMap (ϕ.onRelation R) x = RelMap R x := by exact fun {n} => isEmptyElim @[simp] theorem map_onFunction {M : Type*} [L.Structure M] [L'.Structure M] [ϕ.IsExpansionOn M] {n} (f : L.Functions n) (x : Fin n → M) : funMap (ϕ.onFunction f) x = funMap f x := IsExpansionOn.map_onFunction f x @[simp] theorem map_onRelation {M : Type*} [L.Structure M] [L'.Structure M] [ϕ.IsExpansionOn M] {n} (R : L.Relations n) (x : Fin n → M) : RelMap (ϕ.onRelation R) x = RelMap R x := IsExpansionOn.map_onRelation R x instance id_isExpansionOn (M : Type*) [L.Structure M] : IsExpansionOn (LHom.id L) M := ⟨fun _ _ => rfl, fun _ _ => rfl⟩ instance ofIsEmpty_isExpansionOn (M : Type*) [L.Structure M] [L'.Structure M] [L.IsAlgebraic] [L.IsRelational] : IsExpansionOn (LHom.ofIsEmpty L L') M where instance sumElim_isExpansionOn {L'' : Language} (ψ : L'' →ᴸ L') (M : Type*) [L.Structure M] [L'.Structure M] [L''.Structure M] [ϕ.IsExpansionOn M] [ψ.IsExpansionOn M] : (ϕ.sumElim ψ).IsExpansionOn M := ⟨fun f _ => Sum.casesOn f (by simp) (by simp), fun R _ => Sum.casesOn R (by simp) (by simp)⟩ instance sumMap_isExpansionOn {L₁ L₂ : Language} (ψ : L₁ →ᴸ L₂) (M : Type*) [L.Structure M] [L'.Structure M] [L₁.Structure M] [L₂.Structure M] [ϕ.IsExpansionOn M] [ψ.IsExpansionOn M] : (ϕ.sumMap ψ).IsExpansionOn M := ⟨fun f _ => Sum.casesOn f (by simp) (by simp), fun R _ => Sum.casesOn R (by simp) (by simp)⟩ instance sumInl_isExpansionOn (M : Type*) [L.Structure M] [L'.Structure M] : (LHom.sumInl : L →ᴸ L.sum L').IsExpansionOn M := ⟨fun _f _ => rfl, fun _R _ => rfl⟩ instance sumInr_isExpansionOn (M : Type*) [L.Structure M] [L'.Structure M] : (LHom.sumInr : L' →ᴸ L.sum L').IsExpansionOn M := ⟨fun _f _ => rfl, fun _R _ => rfl⟩ @[simp] theorem funMap_sumInl [(L.sum L').Structure M] [(LHom.sumInl : L →ᴸ L.sum L').IsExpansionOn M] {n} {f : L.Functions n} {x : Fin n → M} : @funMap (L.sum L') M _ n (Sum.inl f) x = funMap f x := (LHom.sumInl : L →ᴸ L.sum L').map_onFunction f x @[simp] theorem funMap_sumInr [(L'.sum L).Structure M] [(LHom.sumInr : L →ᴸ L'.sum L).IsExpansionOn M] {n} {f : L.Functions n} {x : Fin n → M} : @funMap (L'.sum L) M _ n (Sum.inr f) x = funMap f x := (LHom.sumInr : L →ᴸ L'.sum L).map_onFunction f x theorem sumInl_injective : (LHom.sumInl : L →ᴸ L.sum L').Injective := ⟨fun h => Sum.inl_injective h, fun h => Sum.inl_injective h⟩ theorem sumInr_injective : (LHom.sumInr : L' →ᴸ L.sum L').Injective := ⟨fun h => Sum.inr_injective h, fun h => Sum.inr_injective h⟩ instance (priority := 100) isExpansionOn_reduct (ϕ : L →ᴸ L') (M : Type*) [L'.Structure M] : @IsExpansionOn L L' ϕ M (ϕ.reduct M) _ := letI := ϕ.reduct M ⟨fun _f _ => rfl, fun _R _ => rfl⟩ theorem Injective.isExpansionOn_default {ϕ : L →ᴸ L'} [∀ (n) (f : L'.Functions n), Decidable (f ∈ Set.range fun f : L.Functions n => ϕ.onFunction f)] [∀ (n) (r : L'.Relations n), Decidable (r ∈ Set.range fun r : L.Relations n => ϕ.onRelation r)] (h : ϕ.Injective) (M : Type*) [Inhabited M] [L.Structure M] : @IsExpansionOn L L' ϕ M _ (ϕ.defaultExpansion M) := by letI := ϕ.defaultExpansion M refine ⟨fun {n} f xs => ?_, fun {n} r xs => ?_⟩ · have hf : ϕ.onFunction f ∈ Set.range fun f : L.Functions n => ϕ.onFunction f := ⟨f, rfl⟩ refine (dif_pos hf).trans ?_ rw [h.onFunction hf.choose_spec] · have hr : ϕ.onRelation r ∈ Set.range fun r : L.Relations n => ϕ.onRelation r := ⟨r, rfl⟩ refine (dif_pos hr).trans ?_ rw [h.onRelation hr.choose_spec] end LHom /-- A language equivalence maps the symbols of one language to symbols of another bijectively. -/ structure LEquiv (L L' : Language) where /-- The forward language homomorphism -/ toLHom : L →ᴸ L' /-- The inverse language homomorphism -/ invLHom : L' →ᴸ L left_inv : invLHom.comp toLHom = LHom.id L right_inv : toLHom.comp invLHom = LHom.id L' @[inherit_doc] infixl:10 " ≃ᴸ " => LEquiv -- \^L namespace LEquiv variable (L) in /-- The identity equivalence from a first-order language to itself. -/ @[simps] protected def refl : L ≃ᴸ L := ⟨LHom.id L, LHom.id L, LHom.comp_id _, LHom.comp_id _⟩ instance : Inhabited (L ≃ᴸ L) := ⟨LEquiv.refl L⟩ variable {L'' : Language} (e' : L' ≃ᴸ L'') (e : L ≃ᴸ L') /-- The inverse of an equivalence of first-order languages. -/ @[simps] protected def symm : L' ≃ᴸ L := ⟨e.invLHom, e.toLHom, e.right_inv, e.left_inv⟩ /-- The composition of equivalences of first-order languages. -/ @[simps, trans] protected def trans (e : L ≃ᴸ L') (e' : L' ≃ᴸ L'') : L ≃ᴸ L'' := ⟨e'.toLHom.comp e.toLHom, e.invLHom.comp e'.invLHom, by rw [LHom.comp_assoc, ← LHom.comp_assoc e'.invLHom, e'.left_inv, LHom.id_comp, e.left_inv], by rw [LHom.comp_assoc, ← LHom.comp_assoc e.toLHom, e.right_inv, LHom.id_comp, e'.right_inv]⟩ end LEquiv section ConstantsOn variable (α : Type u') /-- The type of functions for a language consisting only of constant symbols. -/ @[simp] def constantsOnFunc : ℕ → Type u' | 0 => α | (_ + 1) => PEmpty /-- A language with constants indexed by a type. -/ @[simps] def constantsOn : Language.{u', 0} := ⟨constantsOnFunc α, fun _ => Empty⟩ variable {α} theorem constantsOn_constants : (constantsOn α).Constants = α := rfl instance isAlgebraic_constantsOn : IsAlgebraic (constantsOn α) := by unfold constantsOn infer_instance instance isEmpty_functions_constantsOn_succ {n : ℕ} : IsEmpty ((constantsOn α).Functions (n + 1)) := inferInstanceAs (IsEmpty PEmpty) instance isRelational_constantsOn [_ie : IsEmpty α] : IsRelational (constantsOn α) := fun n => Nat.casesOn n _ie inferInstance theorem card_constantsOn : (constantsOn α).card = #α := by simp [card_eq_card_functions_add_card_relations, sum_nat_eq_add_sum_succ] /-- Gives a `constantsOn α` structure to a type by assigning each constant a value. -/ def constantsOn.structure (f : α → M) : (constantsOn α).Structure M where funMap := fun {n} c _ => match n, c with | 0, c => f c variable {β : Type v'} /-- A map between index types induces a map between constant languages. -/ def LHom.constantsOnMap (f : α → β) : constantsOn α →ᴸ constantsOn β where onFunction := fun {n} c => match n, c with | 0, c => f c theorem constantsOnMap_isExpansionOn {f : α → β} {fα : α → M} {fβ : β → M} (h : fβ ∘ f = fα) : @LHom.IsExpansionOn _ _ (LHom.constantsOnMap f) M (constantsOn.structure fα) (constantsOn.structure fβ) := by letI := constantsOn.structure fα letI := constantsOn.structure fβ exact ⟨fun {n} => Nat.casesOn n (fun F _x => (congr_fun h F :)) fun n F => isEmptyElim F, fun R => isEmptyElim R⟩ end ConstantsOn section WithConstants variable (L) section variable (α : Type w') /-- Extends a language with a constant for each element of a parameter set in `M`. -/ def withConstants : Language.{max u w', v} := L.sum (constantsOn α) @[inherit_doc FirstOrder.Language.withConstants] scoped[FirstOrder] notation:95 L "[[" α "]]" => Language.withConstants L α @[simp] theorem card_withConstants : L[[α]].card = Cardinal.lift.{w'} L.card + Cardinal.lift.{max u v} #α := by rw [withConstants, card_sum, card_constantsOn] /-- The language map adding constants. -/ @[simps!] def lhomWithConstants : L →ᴸ L[[α]] := LHom.sumInl theorem lhomWithConstants_injective : (L.lhomWithConstants α).Injective := LHom.sumInl_injective variable {α} /-- The constant symbol indexed by a particular element. -/ protected def con (a : α) : L[[α]].Constants := Sum.inr a variable {L} (α) /-- Adds constants to a language map. -/ def LHom.addConstants {L' : Language} (φ : L →ᴸ L') : L[[α]] →ᴸ L'[[α]] := φ.sumMap (LHom.id _)
Mathlib/ModelTheory/LanguageMap.lean
411
411
/- Copyright (c) 2014 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro -/ import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.Ring.Commute import Mathlib.Algebra.Ring.Invertible import Mathlib.Order.Synonym /-! # Lemmas about division (semi)rings and (semi)fields -/ open Function OrderDual Set universe u variable {K L : Type*} section DivisionSemiring variable [DivisionSemiring K] {a b c d : K} theorem add_div (a b c : K) : (a + b) / c = a / c + b / c := by simp_rw [div_eq_mul_inv, add_mul] @[field_simps] theorem div_add_div_same (a b c : K) : a / c + b / c = (a + b) / c := (add_div _ _ _).symm theorem same_add_div (h : b ≠ 0) : (b + a) / b = 1 + a / b := by rw [← div_self h, add_div] theorem div_add_same (h : b ≠ 0) : (a + b) / b = a / b + 1 := by rw [← div_self h, add_div] theorem one_add_div (h : b ≠ 0) : 1 + a / b = (b + a) / b :=
(same_add_div h).symm
Mathlib/Algebra/Field/Basic.lean
37
37
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import Mathlib.Algebra.CharP.Invertible import Mathlib.Algebra.Order.Interval.Set.Group import Mathlib.Analysis.Convex.Basic import Mathlib.Analysis.Convex.Segment import Mathlib.LinearAlgebra.AffineSpace.FiniteDimensional import Mathlib.Tactic.FieldSimp /-! # Betweenness in affine spaces This file defines notions of a point in an affine space being between two given points. ## Main definitions * `affineSegment R x y`: The segment of points weakly between `x` and `y`. * `Wbtw R x y z`: The point `y` is weakly between `x` and `z`. * `Sbtw R x y z`: The point `y` is strictly between `x` and `z`. -/ variable (R : Type*) {V V' P P' : Type*} open AffineEquiv AffineMap section OrderedRing /-- The segment of points weakly between `x` and `y`. When convexity is refactored to support abstract affine combination spaces, this will no longer need to be a separate definition from `segment`. However, lemmas involving `+ᵥ` or `-ᵥ` will still be relevant after such a refactoring, as distinct from versions involving `+` or `-` in a module. -/ def affineSegment [Ring R] [PartialOrder R] [AddCommGroup V] [Module R V] [AddTorsor V P] (x y : P) := lineMap x y '' Set.Icc (0 : R) 1 variable [Ring R] [PartialOrder R] [AddCommGroup V] [Module R V] [AddTorsor V P] variable [AddCommGroup V'] [Module R V'] [AddTorsor V' P'] variable {R} in @[simp] theorem affineSegment_image (f : P →ᵃ[R] P') (x y : P) : f '' affineSegment R x y = affineSegment R (f x) (f y) := by rw [affineSegment, affineSegment, Set.image_image, ← comp_lineMap] rfl @[simp] theorem affineSegment_const_vadd_image (x y : P) (v : V) : (v +ᵥ ·) '' affineSegment R x y = affineSegment R (v +ᵥ x) (v +ᵥ y) := affineSegment_image (AffineEquiv.constVAdd R P v : P →ᵃ[R] P) x y @[simp] theorem affineSegment_vadd_const_image (x y : V) (p : P) : (· +ᵥ p) '' affineSegment R x y = affineSegment R (x +ᵥ p) (y +ᵥ p) := affineSegment_image (AffineEquiv.vaddConst R p : V →ᵃ[R] P) x y @[simp] theorem affineSegment_const_vsub_image (x y p : P) : (p -ᵥ ·) '' affineSegment R x y = affineSegment R (p -ᵥ x) (p -ᵥ y) := affineSegment_image (AffineEquiv.constVSub R p : P →ᵃ[R] V) x y @[simp] theorem affineSegment_vsub_const_image (x y p : P) : (· -ᵥ p) '' affineSegment R x y = affineSegment R (x -ᵥ p) (y -ᵥ p) := affineSegment_image ((AffineEquiv.vaddConst R p).symm : P →ᵃ[R] V) x y variable {R} @[simp] theorem mem_const_vadd_affineSegment {x y z : P} (v : V) : v +ᵥ z ∈ affineSegment R (v +ᵥ x) (v +ᵥ y) ↔ z ∈ affineSegment R x y := by rw [← affineSegment_const_vadd_image, (AddAction.injective v).mem_set_image] @[simp] theorem mem_vadd_const_affineSegment {x y z : V} (p : P) : z +ᵥ p ∈ affineSegment R (x +ᵥ p) (y +ᵥ p) ↔ z ∈ affineSegment R x y := by rw [← affineSegment_vadd_const_image, (vadd_right_injective p).mem_set_image] @[simp] theorem mem_const_vsub_affineSegment {x y z : P} (p : P) : p -ᵥ z ∈ affineSegment R (p -ᵥ x) (p -ᵥ y) ↔ z ∈ affineSegment R x y := by rw [← affineSegment_const_vsub_image, (vsub_right_injective p).mem_set_image] @[simp] theorem mem_vsub_const_affineSegment {x y z : P} (p : P) : z -ᵥ p ∈ affineSegment R (x -ᵥ p) (y -ᵥ p) ↔ z ∈ affineSegment R x y := by rw [← affineSegment_vsub_const_image, (vsub_left_injective p).mem_set_image] variable (R) section OrderedRing variable [IsOrderedRing R] theorem affineSegment_eq_segment (x y : V) : affineSegment R x y = segment R x y := by rw [segment_eq_image_lineMap, affineSegment] theorem affineSegment_comm (x y : P) : affineSegment R x y = affineSegment R y x := by refine Set.ext fun z => ?_ constructor <;> · rintro ⟨t, ht, hxy⟩ refine ⟨1 - t, ?_, ?_⟩ · rwa [Set.sub_mem_Icc_iff_right, sub_self, sub_zero] · rwa [lineMap_apply_one_sub] theorem left_mem_affineSegment (x y : P) : x ∈ affineSegment R x y := ⟨0, Set.left_mem_Icc.2 zero_le_one, lineMap_apply_zero _ _⟩ theorem right_mem_affineSegment (x y : P) : y ∈ affineSegment R x y := ⟨1, Set.right_mem_Icc.2 zero_le_one, lineMap_apply_one _ _⟩ @[simp] theorem affineSegment_same (x : P) : affineSegment R x x = {x} := by simp_rw [affineSegment, lineMap_same, AffineMap.coe_const, Function.const, (Set.nonempty_Icc.mpr zero_le_one).image_const] end OrderedRing /-- The point `y` is weakly between `x` and `z`. -/ def Wbtw (x y z : P) : Prop := y ∈ affineSegment R x z /-- The point `y` is strictly between `x` and `z`. -/ def Sbtw (x y z : P) : Prop := Wbtw R x y z ∧ y ≠ x ∧ y ≠ z variable {R} section OrderedRing variable [IsOrderedRing R] lemma mem_segment_iff_wbtw {x y z : V} : y ∈ segment R x z ↔ Wbtw R x y z := by rw [Wbtw, affineSegment_eq_segment] alias ⟨_, Wbtw.mem_segment⟩ := mem_segment_iff_wbtw lemma Convex.mem_of_wbtw {p₀ p₁ p₂ : V} {s : Set V} (hs : Convex R s) (h₀₁₂ : Wbtw R p₀ p₁ p₂) (h₀ : p₀ ∈ s) (h₂ : p₂ ∈ s) : p₁ ∈ s := hs.segment_subset h₀ h₂ h₀₁₂.mem_segment theorem wbtw_comm {x y z : P} : Wbtw R x y z ↔ Wbtw R z y x := by rw [Wbtw, Wbtw, affineSegment_comm] alias ⟨Wbtw.symm, _⟩ := wbtw_comm theorem sbtw_comm {x y z : P} : Sbtw R x y z ↔ Sbtw R z y x := by
rw [Sbtw, Sbtw, wbtw_comm, ← and_assoc, ← and_assoc, and_right_comm] alias ⟨Sbtw.symm, _⟩ := sbtw_comm
Mathlib/Analysis/Convex/Between.lean
150
152
/- 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, Kevin Buzzard, Yury Kudryashov, Eric Wieser -/ import Mathlib.Algebra.Algebra.Prod import Mathlib.Algebra.Group.Graph import Mathlib.LinearAlgebra.Span.Basic /-! ### Products of modules This file defines constructors for linear maps whose domains or codomains are products. It contains theorems relating these to each other, as well as to `Submodule.prod`, `Submodule.map`, `Submodule.comap`, `LinearMap.range`, and `LinearMap.ker`. ## Main definitions - products in the domain: - `LinearMap.fst` - `LinearMap.snd` - `LinearMap.coprod` - `LinearMap.prod_ext` - products in the codomain: - `LinearMap.inl` - `LinearMap.inr` - `LinearMap.prod` - products in both domain and codomain: - `LinearMap.prodMap` - `LinearEquiv.prodMap` - `LinearEquiv.skewProd` -/ universe u v w x y z u' v' w' y' variable {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variable {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} variable {M₅ M₆ : Type*} section Prod namespace LinearMap variable (S : Type*) [Semiring R] [Semiring S] variable [AddCommMonoid M] [AddCommMonoid M₂] [AddCommMonoid M₃] [AddCommMonoid M₄] variable [AddCommMonoid M₅] [AddCommMonoid M₆] variable [Module R M] [Module R M₂] [Module R M₃] [Module R M₄] variable [Module R M₅] [Module R M₆] variable (f : M →ₗ[R] M₂) section variable (R M M₂) /-- The first projection of a product is a linear map. -/ def fst : M × M₂ →ₗ[R] M where toFun := Prod.fst map_add' _x _y := rfl map_smul' _x _y := rfl /-- The second projection of a product is a linear map. -/ def snd : M × M₂ →ₗ[R] M₂ where toFun := Prod.snd map_add' _x _y := rfl map_smul' _x _y := rfl end @[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl @[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl @[simp, norm_cast] lemma coe_fst : ⇑(fst R M M₂) = Prod.fst := rfl @[simp, norm_cast] lemma coe_snd : ⇑(snd R M M₂) = Prod.snd := rfl theorem fst_surjective : Function.Surjective (fst R M M₂) := fun x => ⟨(x, 0), rfl⟩ theorem snd_surjective : Function.Surjective (snd R M M₂) := fun x => ⟨(0, x), rfl⟩ /-- The prod of two linear maps is a linear map. -/ @[simps] def prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : M →ₗ[R] M₂ × M₃ where toFun := Pi.prod f g map_add' x y := by simp only [Pi.prod, Prod.mk_add_mk, map_add] map_smul' c x := by simp only [Pi.prod, Prod.smul_mk, map_smul, RingHom.id_apply] theorem coe_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ⇑(f.prod g) = Pi.prod f g := rfl @[simp] theorem fst_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (fst R M₂ M₃).comp (prod f g) = f := rfl @[simp] theorem snd_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (snd R M₂ M₃).comp (prod f g) = g := rfl @[simp] theorem pair_fst_snd : prod (fst R M M₂) (snd R M M₂) = LinearMap.id := rfl theorem prod_comp (f : M₂ →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (h : M →ₗ[R] M₂) : (f.prod g).comp h = (f.comp h).prod (g.comp h) := rfl /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def prodEquiv [Module S M₂] [Module S M₃] [SMulCommClass R S M₂] [SMulCommClass R S M₃] : ((M →ₗ[R] M₂) × (M →ₗ[R] M₃)) ≃ₗ[S] M →ₗ[R] M₂ × M₃ where toFun f := f.1.prod f.2 invFun f := ((fst _ _ _).comp f, (snd _ _ _).comp f) left_inv f := by ext <;> rfl right_inv f := by ext <;> rfl map_add' _ _ := rfl map_smul' _ _ := rfl section variable (R M M₂) /-- The left injection into a product is a linear map. -/ def inl : M →ₗ[R] M × M₂ := prod LinearMap.id 0 /-- The right injection into a product is a linear map. -/ def inr : M₂ →ₗ[R] M × M₂ := prod 0 LinearMap.id theorem range_inl : range (inl R M M₂) = ker (snd R M M₂) := by ext x simp only [mem_ker, mem_range] constructor · rintro ⟨y, rfl⟩ rfl · intro h exact ⟨x.fst, Prod.ext rfl h.symm⟩ theorem ker_snd : ker (snd R M M₂) = range (inl R M M₂) := Eq.symm <| range_inl R M M₂ theorem range_inr : range (inr R M M₂) = ker (fst R M M₂) := by ext x simp only [mem_ker, mem_range] constructor · rintro ⟨y, rfl⟩ rfl · intro h exact ⟨x.snd, Prod.ext h.symm rfl⟩ theorem ker_fst : ker (fst R M M₂) = range (inr R M M₂) := Eq.symm <| range_inr R M M₂ @[simp] theorem fst_comp_inl : fst R M M₂ ∘ₗ inl R M M₂ = id := rfl @[simp] theorem snd_comp_inl : snd R M M₂ ∘ₗ inl R M M₂ = 0 := rfl @[simp] theorem fst_comp_inr : fst R M M₂ ∘ₗ inr R M M₂ = 0 := rfl @[simp] theorem snd_comp_inr : snd R M M₂ ∘ₗ inr R M M₂ = id := rfl end @[simp] theorem coe_inl : (inl R M M₂ : M → M × M₂) = fun x => (x, 0) := rfl theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl @[simp] theorem coe_inr : (inr R M M₂ : M₂ → M × M₂) = Prod.mk 0 := rfl theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl theorem inl_eq_prod : inl R M M₂ = prod LinearMap.id 0 := rfl theorem inr_eq_prod : inr R M M₂ = prod 0 LinearMap.id := rfl theorem inl_injective : Function.Injective (inl R M M₂) := fun _ => by simp theorem inr_injective : Function.Injective (inr R M M₂) := fun _ => by simp /-- The coprod function `x : M × M₂ ↦ f x.1 + g x.2` is a linear map. -/ def coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ := f.comp (fst _ _ _) + g.comp (snd _ _ _) @[simp] theorem coprod_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M × M₂) : coprod f g x = f x.1 + g x.2 := rfl @[simp] theorem coprod_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inl R M M₂) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inr R M M₂) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M M₂) (inr R M M₂) = LinearMap.id := by ext <;> simp only [Prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem coprod_zero_left (g : M₂ →ₗ[R] M₃) : (0 : M →ₗ[R] M₃).coprod g = g.comp (snd R M M₂) := zero_add _ theorem coprod_zero_right (f : M →ₗ[R] M₃) : f.coprod (0 : M₂ →ₗ[R] M₃) = f.comp (fst R M M₂) := add_zero _ theorem comp_coprod (f : M₃ →ₗ[R] M₄) (g₁ : M →ₗ[R] M₃) (g₂ : M₂ →ₗ[R] M₃) : f.comp (g₁.coprod g₂) = (f.comp g₁).coprod (f.comp g₂) := ext fun x => f.map_add (g₁ x.1) (g₂ x.2) theorem fst_eq_coprod : fst R M M₂ = coprod LinearMap.id 0 := by ext; simp theorem snd_eq_coprod : snd R M M₂ = coprod 0 LinearMap.id := by ext; simp @[simp] theorem coprod_comp_prod (f : M₂ →ₗ[R] M₄) (g : M₃ →ₗ[R] M₄) (f' : M →ₗ[R] M₂) (g' : M →ₗ[R] M₃) : (f.coprod g).comp (f'.prod g') = f.comp f' + g.comp g' := rfl @[simp] theorem coprod_map_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (S : Submodule R M) (S' : Submodule R M₂) : (Submodule.prod S S').map (LinearMap.coprod f g) = S.map f ⊔ S'.map g := SetLike.coe_injective <| by simp only [LinearMap.coprod_apply, Submodule.coe_sup, Submodule.map_coe] rw [← Set.image2_add, Set.image2_image_left, Set.image2_image_right] exact Set.image_prod fun m m₂ => f m + g m₂ /-- Taking the product of two maps with the same codomain is equivalent to taking the product of their domains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def coprodEquiv [Module S M₃] [SMulCommClass R S M₃] : ((M →ₗ[R] M₃) × (M₂ →ₗ[R] M₃)) ≃ₗ[S] M × M₂ →ₗ[R] M₃ where toFun f := f.1.coprod f.2 invFun f := (f.comp (inl _ _ _), f.comp (inr _ _ _)) left_inv f := by simp only [coprod_inl, coprod_inr] right_inv f := by simp only [← comp_coprod, comp_id, coprod_inl_inr] map_add' a b := by ext simp only [Prod.snd_add, add_apply, coprod_apply, Prod.fst_add, add_add_add_comm] map_smul' r a := by dsimp ext simp only [smul_add, smul_apply, Prod.smul_snd, Prod.smul_fst, coprod_apply] theorem prod_ext_iff {f g : M × M₂ →ₗ[R] M₃} : f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) := (coprodEquiv ℕ).symm.injective.eq_iff.symm.trans Prod.ext_iff /-- Split equality of linear maps from a product into linear maps over each component, to allow `ext` to apply lemmas specific to `M →ₗ M₃` and `M₂ →ₗ M₃`. See note [partially-applied ext lemmas]. -/ @[ext 1100] theorem prod_ext {f g : M × M₂ →ₗ[R] M₃} (hl : f.comp (inl _ _ _) = g.comp (inl _ _ _)) (hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) : f = g := prod_ext_iff.2 ⟨hl, hr⟩ /-- `Prod.map` of two linear maps. -/ def prodMap (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : M × M₂ →ₗ[R] M₃ × M₄ := (f.comp (fst R M M₂)).prod (g.comp (snd R M M₂)) theorem coe_prodMap (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : ⇑(f.prodMap g) = Prod.map f g := rfl @[simp] theorem prodMap_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (x) : f.prodMap g x = (f x.1, g x.2) := rfl theorem prodMap_comap_prod (f : M →ₗ[R] M₂) (g : M₃ →ₗ[R] M₄) (S : Submodule R M₂) (S' : Submodule R M₄) : (Submodule.prod S S').comap (LinearMap.prodMap f g) = (S.comap f).prod (S'.comap g) := SetLike.coe_injective <| Set.preimage_prod_map_prod f g _ _ theorem ker_prodMap (f : M →ₗ[R] M₂) (g : M₃ →ₗ[R] M₄) : ker (LinearMap.prodMap f g) = Submodule.prod (ker f) (ker g) := by dsimp only [ker] rw [← prodMap_comap_prod, Submodule.prod_bot] @[simp] theorem prodMap_id : (id : M →ₗ[R] M).prodMap (id : M₂ →ₗ[R] M₂) = id := rfl @[simp] theorem prodMap_one : (1 : M →ₗ[R] M).prodMap (1 : M₂ →ₗ[R] M₂) = 1 := rfl theorem prodMap_comp (f₁₂ : M →ₗ[R] M₂) (f₂₃ : M₂ →ₗ[R] M₃) (g₁₂ : M₄ →ₗ[R] M₅) (g₂₃ : M₅ →ₗ[R] M₆) : f₂₃.prodMap g₂₃ ∘ₗ f₁₂.prodMap g₁₂ = (f₂₃ ∘ₗ f₁₂).prodMap (g₂₃ ∘ₗ g₁₂) := rfl theorem prodMap_mul (f₁₂ : M →ₗ[R] M) (f₂₃ : M →ₗ[R] M) (g₁₂ : M₂ →ₗ[R] M₂) (g₂₃ : M₂ →ₗ[R] M₂) : f₂₃.prodMap g₂₃ * f₁₂.prodMap g₁₂ = (f₂₃ * f₁₂).prodMap (g₂₃ * g₁₂) := rfl theorem prodMap_add (f₁ : M →ₗ[R] M₃) (f₂ : M →ₗ[R] M₃) (g₁ : M₂ →ₗ[R] M₄) (g₂ : M₂ →ₗ[R] M₄) : (f₁ + f₂).prodMap (g₁ + g₂) = f₁.prodMap g₁ + f₂.prodMap g₂ := rfl @[simp] theorem prodMap_zero : (0 : M →ₗ[R] M₂).prodMap (0 : M₃ →ₗ[R] M₄) = 0 := rfl @[simp] theorem prodMap_smul [DistribMulAction S M₃] [DistribMulAction S M₄] [SMulCommClass R S M₃] [SMulCommClass R S M₄] (s : S) (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : prodMap (s • f) (s • g) = s • prodMap f g := rfl variable (R M M₂ M₃ M₄) /-- `LinearMap.prodMap` as a `LinearMap` -/ @[simps] def prodMapLinear [Module S M₃] [Module S M₄] [SMulCommClass R S M₃] [SMulCommClass R S M₄] : (M →ₗ[R] M₃) × (M₂ →ₗ[R] M₄) →ₗ[S] M × M₂ →ₗ[R] M₃ × M₄ where toFun f := prodMap f.1 f.2 map_add' _ _ := rfl map_smul' _ _ := rfl /-- `LinearMap.prodMap` as a `RingHom` -/ @[simps] def prodMapRingHom : (M →ₗ[R] M) × (M₂ →ₗ[R] M₂) →+* M × M₂ →ₗ[R] M × M₂ where toFun f := prodMap f.1 f.2 map_one' := prodMap_one map_zero' := rfl map_add' _ _ := rfl map_mul' _ _ := rfl variable {R M M₂ M₃ M₄} section map_mul variable {A : Type*} [NonUnitalNonAssocSemiring A] [Module R A] variable {B : Type*} [NonUnitalNonAssocSemiring B] [Module R B] theorem inl_map_mul (a₁ a₂ : A) : LinearMap.inl R A B (a₁ * a₂) = LinearMap.inl R A B a₁ * LinearMap.inl R A B a₂ := Prod.ext rfl (by simp) theorem inr_map_mul (b₁ b₂ : B) : LinearMap.inr R A B (b₁ * b₂) = LinearMap.inr R A B b₁ * LinearMap.inr R A B b₂ := Prod.ext (by simp) rfl end map_mul end LinearMap end Prod namespace LinearMap variable (R M M₂) variable [CommSemiring R] variable [AddCommMonoid M] [AddCommMonoid M₂] variable [Module R M] [Module R M₂] /-- `LinearMap.prodMap` as an `AlgHom` -/ @[simps!] def prodMapAlgHom : Module.End R M × Module.End R M₂ →ₐ[R] Module.End R (M × M₂) := { prodMapRingHom R M M₂ with commutes' := fun _ => rfl } end LinearMap namespace LinearMap open Submodule variable [Semiring R] [AddCommMonoid M] [AddCommMonoid M₂] [AddCommMonoid M₃] [AddCommMonoid M₄] [Module R M] [Module R M₂] [Module R M₃] [Module R M₄] theorem range_coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : range (f.coprod g) = range f ⊔ range g := Submodule.ext fun x => by simp [mem_sup] theorem isCompl_range_inl_inr : IsCompl (range <| inl R M M₂) (range <| inr R M M₂) := by constructor · rw [disjoint_def] rintro ⟨_, _⟩ ⟨x, hx⟩ ⟨y, hy⟩ simp only [Prod.ext_iff, inl_apply, inr_apply, mem_bot] at hx hy ⊢ exact ⟨hy.1.symm, hx.2.symm⟩ · rw [codisjoint_iff_le_sup] rintro ⟨x, y⟩ - simp only [mem_sup, mem_range, exists_prop] refine ⟨(x, 0), ⟨x, rfl⟩, (0, y), ⟨y, rfl⟩, ?_⟩ simp theorem sup_range_inl_inr : (range <| inl R M M₂) ⊔ (range <| inr R M M₂) = ⊤ := IsCompl.sup_eq_top isCompl_range_inl_inr theorem disjoint_inl_inr : Disjoint (range <| inl R M M₂) (range <| inr R M M₂) := by simp +contextual [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] theorem map_coprod_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (p : Submodule R M) (q : Submodule R M₂) : map (coprod f g) (p.prod q) = map f p ⊔ map g q := by refine le_antisymm ?_ (sup_le (map_le_iff_le_comap.2 ?_) (map_le_iff_le_comap.2 ?_)) · rw [SetLike.le_def] rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩ exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ · exact fun x hx => ⟨(x, 0), by simp [hx]⟩ · exact fun x hx => ⟨(0, x), by simp [hx]⟩ theorem comap_prod_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (p : Submodule R M₂) (q : Submodule R M₃) : comap (prod f g) (p.prod q) = comap f p ⊓ comap g q := Submodule.ext fun _x => Iff.rfl theorem prod_eq_inf_comap (p : Submodule R M) (q : Submodule R M₂) : p.prod q = p.comap (LinearMap.fst R M M₂) ⊓ q.comap (LinearMap.snd R M M₂) := Submodule.ext fun _x => Iff.rfl theorem prod_eq_sup_map (p : Submodule R M) (q : Submodule R M₂) : p.prod q = p.map (LinearMap.inl R M M₂) ⊔ q.map (LinearMap.inr R M M₂) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] theorem span_inl_union_inr {s : Set M} {t : Set M₂} : span R (inl R M M₂ '' s ∪ inr R M M₂ '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image] @[simp] theorem ker_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ker (prod f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; rfl theorem range_prod_le (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : range (prod f g) ≤ (range f).prod (range g) := by simp only [SetLike.le_def, prod_apply, mem_range, SetLike.mem_coe, mem_prod, exists_imp] rintro _ x rfl exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ theorem ker_prod_ker_le_ker_coprod {M₂ : Type*} [AddCommMonoid M₂] [Module R M₂] {M₃ : Type*} [AddCommMonoid M₃] [Module R M₃] (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (ker f).prod (ker g) ≤ ker (f.coprod g) := by rintro ⟨y, z⟩ simp +contextual theorem ker_coprod_of_disjoint_range {M₂ : Type*} [AddCommGroup M₂] [Module R M₂] {M₃ : Type*} [AddCommGroup M₃] [Module R M₃] (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (hd : Disjoint (range f) (range g)) : ker (f.coprod g) = (ker f).prod (ker g) := by apply le_antisymm _ (ker_prod_ker_le_ker_coprod f g) rintro ⟨y, z⟩ h simp only [mem_ker, mem_prod, coprod_apply] at h ⊢ have : f y ∈ (range f) ⊓ (range g) := by simp only [true_and, mem_range, mem_inf, exists_apply_eq_apply] use -z rwa [eq_comm, map_neg, ← sub_eq_zero, sub_neg_eq_add] rw [hd.eq_bot, mem_bot] at this rw [this] at h simpa [this] using h end LinearMap namespace Submodule open LinearMap variable [Semiring R] variable [AddCommMonoid M] [AddCommMonoid M₂] variable [Module R M] [Module R M₂] theorem sup_eq_range (p q : Submodule R M) : p ⊔ q = range (p.subtype.coprod q.subtype) := Submodule.ext fun x => by simp [Submodule.mem_sup, SetLike.exists] variable (p : Submodule R M) (q : Submodule R M₂) @[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ := by ext ⟨x, y⟩ simp only [and_left_comm, eq_comm, mem_map, Prod.mk_inj, inl_apply, mem_bot, exists_eq_left', mem_prod] @[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q := by ext ⟨x, y⟩; simp [and_left_comm, eq_comm, and_comm] @[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : ker (inl R M M₂) = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : ker (inr R M M₂) = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : range (fst R M M₂) = ⊤ := by rw [range_eq_map, ← prod_top, prod_map_fst] @[simp] theorem range_snd : range (snd R M M₂) = ⊤ := by rw [range_eq_map, ← prod_top, prod_map_snd] variable (R M M₂) /-- `M` as a submodule of `M × N`. -/ def fst : Submodule R (M × M₂) := (⊥ : Submodule R M₂).comap (LinearMap.snd R M M₂) /-- `M` as a submodule of `M × N` is isomorphic to `M`. -/ @[simps] def fstEquiv : Submodule.fst R M M₂ ≃ₗ[R] M where -- Porting note: proofs were `tidy` or `simp` toFun x := x.1.1 invFun m := ⟨⟨m, 0⟩, by simp [fst]⟩ map_add' := by simp map_smul' := by simp left_inv := by rintro ⟨⟨x, y⟩, hy⟩ simp only [fst, comap_bot, mem_ker, snd_apply] at hy simpa only [Subtype.mk.injEq, Prod.mk.injEq, true_and] using hy.symm right_inv := by rintro x; rfl theorem fst_map_fst : (Submodule.fst R M M₂).map (LinearMap.fst R M M₂) = ⊤ := by aesop theorem fst_map_snd : (Submodule.fst R M M₂).map (LinearMap.snd R M M₂) = ⊥ := by aesop (add simp fst) /-- `N` as a submodule of `M × N`. -/ def snd : Submodule R (M × M₂) := (⊥ : Submodule R M).comap (LinearMap.fst R M M₂) /-- `N` as a submodule of `M × N` is isomorphic to `N`. -/ @[simps] def sndEquiv : Submodule.snd R M M₂ ≃ₗ[R] M₂ where -- Porting note: proofs were `tidy` or `simp` toFun x := x.1.2 invFun n := ⟨⟨0, n⟩, by simp [snd]⟩ map_add' := by simp map_smul' := by simp left_inv := by rintro ⟨⟨x, y⟩, hx⟩ simp only [snd, comap_bot, mem_ker, fst_apply] at hx simpa only [Subtype.mk.injEq, Prod.mk.injEq, and_true] using hx.symm right_inv := by rintro x; rfl theorem snd_map_fst : (Submodule.snd R M M₂).map (LinearMap.fst R M M₂) = ⊥ := by aesop (add simp snd) theorem snd_map_snd : (Submodule.snd R M M₂).map (LinearMap.snd R M M₂) = ⊤ := by aesop theorem fst_sup_snd : Submodule.fst R M M₂ ⊔ Submodule.snd R M M₂ = ⊤ := by rw [eq_top_iff] rintro ⟨m, n⟩ - rw [show (m, n) = (m, 0) + (0, n) by simp] apply Submodule.add_mem (Submodule.fst R M M₂ ⊔ Submodule.snd R M M₂) · exact Submodule.mem_sup_left (Submodule.mem_comap.mpr (by simp)) · exact Submodule.mem_sup_right (Submodule.mem_comap.mpr (by simp)) theorem fst_inf_snd : Submodule.fst R M M₂ ⊓ Submodule.snd R M M₂ = ⊥ := by aesop theorem le_prod_iff {p₁ : Submodule R M} {p₂ : Submodule R M₂} {q : Submodule R (M × M₂)} : q ≤ p₁.prod p₂ ↔ map (LinearMap.fst R M M₂) q ≤ p₁ ∧ map (LinearMap.snd R M M₂) q ≤ p₂ := by constructor · intro h constructor · rintro x ⟨⟨y1, y2⟩, ⟨hy1, rfl⟩⟩ exact (h hy1).1 · rintro x ⟨⟨y1, y2⟩, ⟨hy1, rfl⟩⟩ exact (h hy1).2 · rintro ⟨hH, hK⟩ ⟨x1, x2⟩ h exact ⟨hH ⟨_, h, rfl⟩, hK ⟨_, h, rfl⟩⟩ theorem prod_le_iff {p₁ : Submodule R M} {p₂ : Submodule R M₂} {q : Submodule R (M × M₂)} : p₁.prod p₂ ≤ q ↔ map (LinearMap.inl R M M₂) p₁ ≤ q ∧ map (LinearMap.inr R M M₂) p₂ ≤ q := by constructor
· intro h
Mathlib/LinearAlgebra/Prod.lean
597
597
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Analysis.Complex.UpperHalfPlane.Topology import Mathlib.Analysis.SpecialFunctions.Arsinh import Mathlib.Geometry.Euclidean.Inversion.Basic /-! # Metric on the upper half-plane In this file we define a `MetricSpace` structure on the `UpperHalfPlane`. We use hyperbolic (Poincaré) distance given by `dist z w = 2 * arsinh (dist (z : ℂ) w / (2 * √(z.im * w.im)))` instead of the induced Euclidean distance because the hyperbolic distance is invariant under holomorphic automorphisms of the upper half-plane. However, we ensure that the projection to `TopologicalSpace` is definitionally equal to the induced topological space structure. We also prove that a metric ball/closed ball/sphere in Poincaré metric is a Euclidean ball/closed ball/sphere with another center and radius. -/ noncomputable section open Filter Metric Real Set Topology open scoped UpperHalfPlane ComplexConjugate NNReal Topology MatrixGroups variable {z w : ℍ} {r : ℝ} namespace UpperHalfPlane instance : Dist ℍ := ⟨fun z w => 2 * arsinh (dist (z : ℂ) w / (2 * √(z.im * w.im)))⟩ theorem dist_eq (z w : ℍ) : dist z w = 2 * arsinh (dist (z : ℂ) w / (2 * √(z.im * w.im))) := rfl theorem sinh_half_dist (z w : ℍ) : sinh (dist z w / 2) = dist (z : ℂ) w / (2 * √(z.im * w.im)) := by rw [dist_eq, mul_div_cancel_left₀ (arsinh _) two_ne_zero, sinh_arsinh] theorem cosh_half_dist (z w : ℍ) : cosh (dist z w / 2) = dist (z : ℂ) (conj (w : ℂ)) / (2 * √(z.im * w.im)) := by rw [← sq_eq_sq₀, cosh_sq', sinh_half_dist, div_pow, div_pow, one_add_div, mul_pow, sq_sqrt] · congr 1 simp only [Complex.dist_eq, Complex.sq_norm, Complex.normSq_sub, Complex.normSq_conj, Complex.conj_conj, Complex.mul_re, Complex.conj_re, Complex.conj_im, coe_im] ring all_goals positivity theorem tanh_half_dist (z w : ℍ) : tanh (dist z w / 2) = dist (z : ℂ) w / dist (z : ℂ) (conj ↑w) := by rw [tanh_eq_sinh_div_cosh, sinh_half_dist, cosh_half_dist, div_div_div_comm, div_self, div_one] positivity theorem exp_half_dist (z w : ℍ) : exp (dist z w / 2) = (dist (z : ℂ) w + dist (z : ℂ) (conj ↑w)) / (2 * √(z.im * w.im)) := by rw [← sinh_add_cosh, sinh_half_dist, cosh_half_dist, add_div] theorem cosh_dist (z w : ℍ) : cosh (dist z w) = 1 + dist (z : ℂ) w ^ 2 / (2 * z.im * w.im) := by rw [dist_eq, cosh_two_mul, cosh_sq', add_assoc, ← two_mul, sinh_arsinh, div_pow, mul_pow, sq_sqrt, sq (2 : ℝ), mul_assoc, ← mul_div_assoc, mul_assoc, mul_div_mul_left] <;> positivity
theorem sinh_half_dist_add_dist (a b c : ℍ) : sinh ((dist a b + dist b c) / 2) = (dist (a : ℂ) b * dist (c : ℂ) (conj ↑b) + dist (b : ℂ) c * dist (a : ℂ) (conj ↑b)) /
Mathlib/Analysis/Complex/UpperHalfPlane/Metric.lean
66
68
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Algebra.Group.Action.End import Mathlib.Algebra.Group.Action.Pointwise.Set.Basic import Mathlib.Algebra.Group.Action.Prod import Mathlib.Algebra.Group.Subgroup.Map import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.NoZeroSMulDivisors.Defs import Mathlib.Data.Finite.Sigma import Mathlib.Data.Set.Finite.Range import Mathlib.Data.Setoid.Basic import Mathlib.GroupTheory.GroupAction.Defs /-! # Basic properties of group actions This file primarily concerns itself with orbits, stabilizers, and other objects defined in terms of actions. Despite this file being called `basic`, low-level helper lemmas for algebraic manipulation of `•` belong elsewhere. ## Main definitions * `MulAction.orbit` * `MulAction.fixedPoints` * `MulAction.fixedBy` * `MulAction.stabilizer` -/ universe u v open Pointwise open Function namespace MulAction variable (M : Type u) [Monoid M] (α : Type v) [MulAction M α] {β : Type*} [MulAction M β] section Orbit variable {α M} @[to_additive] lemma fst_mem_orbit_of_mem_orbit {x y : α × β} (h : x ∈ MulAction.orbit M y) : x.1 ∈ MulAction.orbit M y.1 := by rcases h with ⟨g, rfl⟩ exact mem_orbit _ _ @[to_additive] lemma snd_mem_orbit_of_mem_orbit {x y : α × β} (h : x ∈ MulAction.orbit M y) : x.2 ∈ MulAction.orbit M y.2 := by rcases h with ⟨g, rfl⟩ exact mem_orbit _ _ @[to_additive] lemma _root_.Finite.finite_mulAction_orbit [Finite M] (a : α) : Set.Finite (orbit M a) := Set.finite_range _ variable (M) @[to_additive] theorem orbit_eq_univ [IsPretransitive M α] (a : α) : orbit M a = Set.univ := (surjective_smul M a).range_eq end Orbit section FixedPoints variable {M α} @[to_additive (attr := simp)] theorem subsingleton_orbit_iff_mem_fixedPoints {a : α} : (orbit M a).Subsingleton ↔ a ∈ fixedPoints M α := by rw [mem_fixedPoints] constructor · exact fun h m ↦ h (mem_orbit a m) (mem_orbit_self a) · rintro h _ ⟨m, rfl⟩ y ⟨p, rfl⟩ simp only [h] @[to_additive mem_fixedPoints_iff_card_orbit_eq_one] theorem mem_fixedPoints_iff_card_orbit_eq_one {a : α} [Fintype (orbit M a)] : a ∈ fixedPoints M α ↔ Fintype.card (orbit M a) = 1 := by simp only [← subsingleton_orbit_iff_mem_fixedPoints, le_antisymm_iff, Fintype.card_le_one_iff_subsingleton, Nat.add_one_le_iff, Fintype.card_pos_iff, Set.subsingleton_coe, iff_self_and, Set.nonempty_coe_sort, orbit_nonempty, implies_true] @[to_additive instDecidablePredMemSetFixedByAddOfDecidableEq] instance (m : M) [DecidableEq β] : DecidablePred fun b : β => b ∈ MulAction.fixedBy β m := fun b ↦ by simp only [MulAction.mem_fixedBy, Equiv.Perm.smul_def] infer_instance end FixedPoints end MulAction /-- `smul` by a `k : M` over a group is injective, if `k` is not a zero divisor. The general theory of such `k` is elaborated by `IsSMulRegular`. The typeclass that restricts all terms of `M` to have this property is `NoZeroSMulDivisors`. -/ theorem smul_cancel_of_non_zero_divisor {M G : Type*} [Monoid M] [AddGroup G] [DistribMulAction M G] (k : M) (h : ∀ x : G, k • x = 0 → x = 0) {a b : G} (h' : k • a = k • b) : a = b := by rw [← sub_eq_zero] refine h _ ?_ rw [smul_sub, h', sub_self] namespace MulAction variable {G α β : Type*} [Group G] [MulAction G α] [MulAction G β] @[to_additive] theorem fixedPoints_of_subsingleton [Subsingleton α] : fixedPoints G α = .univ := by apply Set.eq_univ_of_forall simp only [mem_fixedPoints] intro x hx apply Subsingleton.elim .. /-- If a group acts nontrivially, then the type is nontrivial -/ @[to_additive "If a subgroup acts nontrivially, then the type is nontrivial."] theorem nontrivial_of_fixedPoints_ne_univ (h : fixedPoints G α ≠ .univ) : Nontrivial α := (subsingleton_or_nontrivial α).resolve_left fun _ ↦ h fixedPoints_of_subsingleton section Orbit -- TODO: This proof is redoing a special case of `MulAction.IsInvariantBlock.isBlock`. Can we move -- this lemma earlier to golf? @[to_additive (attr := simp)] theorem smul_orbit (g : G) (a : α) : g • orbit G a = orbit G a := (smul_orbit_subset g a).antisymm <| calc orbit G a = g • g⁻¹ • orbit G a := (smul_inv_smul _ _).symm _ ⊆ g • orbit G a := Set.image_subset _ (smul_orbit_subset _ _) /-- The action of a group on an orbit is transitive. -/ @[to_additive "The action of an additive group on an orbit is transitive."] instance (a : α) : IsPretransitive G (orbit G a) := ⟨by rintro ⟨_, g, rfl⟩ ⟨_, h, rfl⟩ use h * g⁻¹ ext1 simp [mul_smul]⟩ @[to_additive] lemma orbitRel_subgroup_le (H : Subgroup G) : orbitRel H α ≤ orbitRel G α := Setoid.le_def.2 mem_orbit_of_mem_orbit_subgroup @[to_additive] lemma orbitRel_subgroupOf (H K : Subgroup G) : orbitRel (H.subgroupOf K) α = orbitRel (H ⊓ K : Subgroup G) α := by rw [← Subgroup.subgroupOf_map_subtype] ext x simp_rw [orbitRel_apply] refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · rcases h with ⟨⟨gv, gp⟩, rfl⟩ simp only [Submonoid.mk_smul] refine mem_orbit _ (⟨gv, ?_⟩ : Subgroup.map K.subtype (H.subgroupOf K)) simpa using gp · rcases h with ⟨⟨gv, gp⟩, rfl⟩ simp only [Submonoid.mk_smul] simp only [Subgroup.subgroupOf_map_subtype, Subgroup.mem_inf] at gp refine mem_orbit _ (⟨⟨gv, ?_⟩, ?_⟩ : H.subgroupOf K) · exact gp.2 · simp only [Subgroup.mem_subgroupOf] exact gp.1 variable (G α) /-- An action is pretransitive if and only if the quotient by `MulAction.orbitRel` is a subsingleton. -/ @[to_additive "An additive action is pretransitive if and only if the quotient by `AddAction.orbitRel` is a subsingleton."] theorem pretransitive_iff_subsingleton_quotient : IsPretransitive G α ↔ Subsingleton (orbitRel.Quotient G α) := by refine ⟨fun _ ↦ ⟨fun a b ↦ ?_⟩, fun _ ↦ ⟨fun a b ↦ ?_⟩⟩ · refine Quot.inductionOn a (fun x ↦ ?_) exact Quot.inductionOn b (fun y ↦ Quot.sound <| exists_smul_eq G y x) · have h : Quotient.mk (orbitRel G α) b = ⟦a⟧ := Subsingleton.elim _ _ exact Quotient.eq''.mp h /-- If `α` is non-empty, an action is pretransitive if and only if the quotient has exactly one element. -/ @[to_additive "If `α` is non-empty, an additive action is pretransitive if and only if the quotient has exactly one element."] theorem pretransitive_iff_unique_quotient_of_nonempty [Nonempty α] : IsPretransitive G α ↔ Nonempty (Unique <| orbitRel.Quotient G α) := by rw [unique_iff_subsingleton_and_nonempty, pretransitive_iff_subsingleton_quotient, iff_self_and] exact fun _ ↦ (nonempty_quotient_iff _).mpr inferInstance variable {G α} @[to_additive] instance (x : orbitRel.Quotient G α) : IsPretransitive G x.orbit where exists_smul_eq := by induction x using Quotient.inductionOn' rintro ⟨y, yh⟩ ⟨z, zh⟩ rw [orbitRel.Quotient.mem_orbit, Quotient.eq''] at yh zh rcases yh with ⟨g, rfl⟩ rcases zh with ⟨h, rfl⟩ refine ⟨h * g⁻¹, ?_⟩ ext simp [mul_smul] variable (G) (α) local notation "Ω" => orbitRel.Quotient G α @[to_additive] lemma _root_.Finite.of_finite_mulAction_orbitRel_quotient [Finite G] [Finite Ω] : Finite α := by rw [(selfEquivSigmaOrbits' G _).finite_iff] have : ∀ g : Ω, Finite g.orbit := by intro g induction g using Quotient.inductionOn' simpa [Set.finite_coe_iff] using Finite.finite_mulAction_orbit _ exact Finite.instSigma variable (β) @[to_additive] lemma orbitRel_le_fst : orbitRel G (α × β) ≤ (orbitRel G α).comap Prod.fst := Setoid.le_def.2 fst_mem_orbit_of_mem_orbit @[to_additive] lemma orbitRel_le_snd : orbitRel G (α × β) ≤ (orbitRel G β).comap Prod.snd := Setoid.le_def.2 snd_mem_orbit_of_mem_orbit end Orbit section Stabilizer variable (G) variable {G} /-- If the stabilizer of `a` is `S`, then the stabilizer of `g • a` is `gSg⁻¹`. -/ theorem stabilizer_smul_eq_stabilizer_map_conj (g : G) (a : α) : stabilizer G (g • a) = (stabilizer G a).map (MulAut.conj g).toMonoidHom := by ext h rw [mem_stabilizer_iff, ← smul_left_cancel_iff g⁻¹, smul_smul, smul_smul, smul_smul, inv_mul_cancel, one_smul, ← mem_stabilizer_iff, Subgroup.mem_map_equiv, MulAut.conj_symm_apply] /-- A bijection between the stabilizers of two elements in the same orbit. -/ noncomputable def stabilizerEquivStabilizerOfOrbitRel {a b : α} (h : orbitRel G α a b) : stabilizer G a ≃* stabilizer G b := let g : G := Classical.choose h have hg : g • b = a := Classical.choose_spec h have this : stabilizer G a = (stabilizer G b).map (MulAut.conj g).toMonoidHom := by rw [← hg, stabilizer_smul_eq_stabilizer_map_conj] (MulEquiv.subgroupCongr this).trans ((MulAut.conj g).subgroupMap <| stabilizer G b).symm end Stabilizer end MulAction namespace AddAction variable {G α : Type*} [AddGroup G] [AddAction G α] /-- If the stabilizer of `x` is `S`, then the stabilizer of `g +ᵥ x` is `g + S + (-g)`. -/ theorem stabilizer_vadd_eq_stabilizer_map_conj (g : G) (a : α) : stabilizer G (g +ᵥ a) = (stabilizer G a).map (AddAut.conj g).toMul.toAddMonoidHom := by ext h rw [mem_stabilizer_iff, ← vadd_left_cancel_iff (-g), vadd_vadd, vadd_vadd, vadd_vadd, neg_add_cancel, zero_vadd, ← mem_stabilizer_iff, AddSubgroup.mem_map_equiv, AddAut.conj_symm_apply] /-- A bijection between the stabilizers of two elements in the same orbit. -/ noncomputable def stabilizerEquivStabilizerOfOrbitRel {a b : α} (h : orbitRel G α a b) : stabilizer G a ≃+ stabilizer G b := let g : G := Classical.choose h have hg : g +ᵥ b = a := Classical.choose_spec h have this : stabilizer G a = (stabilizer G b).map (AddAut.conj g).toMul.toAddMonoidHom := by rw [← hg, stabilizer_vadd_eq_stabilizer_map_conj] (AddEquiv.addSubgroupCongr this).trans ((AddAut.conj g).addSubgroupMap <| stabilizer G b).symm end AddAction attribute [to_additive existing] MulAction.stabilizer_smul_eq_stabilizer_map_conj attribute [to_additive existing] MulAction.stabilizerEquivStabilizerOfOrbitRel theorem Equiv.swap_mem_stabilizer {α : Type*} [DecidableEq α] {S : Set α} {a b : α} : Equiv.swap a b ∈ MulAction.stabilizer (Equiv.Perm α) S ↔ (a ∈ S ↔ b ∈ S) := by rw [MulAction.mem_stabilizer_iff, Set.ext_iff, ← swap_inv] simp_rw [Set.mem_inv_smul_set_iff, Perm.smul_def, swap_apply_def] exact ⟨fun h ↦ by simpa [Iff.comm] using h a, by intros; split_ifs <;> simp [*]⟩ namespace MulAction variable {G : Type*} [Group G] {α : Type*} [MulAction G α] /-- To prove inclusion of a *subgroup* in a stabilizer, it is enough to prove inclusions. -/ @[to_additive "To prove inclusion of a *subgroup* in a stabilizer, it is enough to prove inclusions."] theorem le_stabilizer_iff_smul_le (s : Set α) (H : Subgroup G) : H ≤ stabilizer G s ↔ ∀ g ∈ H, g • s ⊆ s := by constructor · intro hyp g hg apply Eq.subset rw [← mem_stabilizer_iff] exact hyp hg · intro hyp g hg rw [mem_stabilizer_iff] apply subset_antisymm (hyp g hg) intro x hx use g⁻¹ • x constructor · apply hyp g⁻¹ (inv_mem hg) simp only [Set.smul_mem_smul_set_iff, hx] · simp only [smul_inv_smul] end MulAction section variable (R M : Type*) [Ring R] [AddCommGroup M] [Module R M] [NoZeroSMulDivisors R M] variable {M} in lemma Module.stabilizer_units_eq_bot_of_ne_zero {x : M} (hx : x ≠ 0) : MulAction.stabilizer Rˣ x = ⊥ := by rw [eq_bot_iff] intro g (hg : g.val • x = x) ext rw [← sub_eq_zero, ← smul_eq_zero_iff_left hx, Units.val_one, sub_smul, hg, one_smul, sub_self] end
Mathlib/GroupTheory/GroupAction/Basic.lean
739
743
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.DFinsupp.BigOperators import Mathlib.Data.DFinsupp.Order import Mathlib.Order.Interval.Finset.Basic import Mathlib.Algebra.Group.Pointwise.Finset.Basic /-! # Finite intervals of finitely supported functions This file provides the `LocallyFiniteOrder` instance for `Π₀ i, α i` when `α` itself is locally finite and calculates the cardinality of its finite intervals. -/ open DFinsupp Finset open Pointwise variable {ι : Type*} {α : ι → Type*} namespace Finset variable [DecidableEq ι] [∀ i, Zero (α i)] {s : Finset ι} {f : Π₀ i, α i} {t : ∀ i, Finset (α i)} /-- Finitely supported product of finsets. -/ def dfinsupp (s : Finset ι) (t : ∀ i, Finset (α i)) : Finset (Π₀ i, α i) := (s.pi t).map ⟨fun f => DFinsupp.mk s fun i => f i i.2, by refine (mk_injective _).comp fun f g h => ?_ ext i hi convert congr_fun h ⟨i, hi⟩⟩ @[simp] theorem card_dfinsupp (s : Finset ι) (t : ∀ i, Finset (α i)) : #(s.dfinsupp t) = ∏ i ∈ s, #(t i) := (card_map _).trans <| card_pi _ _ variable [∀ i, DecidableEq (α i)] theorem mem_dfinsupp_iff : f ∈ s.dfinsupp t ↔ f.support ⊆ s ∧ ∀ i ∈ s, f i ∈ t i := by refine mem_map.trans ⟨?_, ?_⟩ · rintro ⟨f, hf, rfl⟩ rw [Function.Embedding.coeFn_mk] refine ⟨support_mk_subset, fun i hi => ?_⟩ convert mem_pi.1 hf i hi exact mk_of_mem hi · refine fun h => ⟨fun i _ => f i, mem_pi.2 h.2, ?_⟩ ext i dsimp exact ite_eq_left_iff.2 fun hi => (not_mem_support_iff.1 fun H => hi <| h.1 H).symm /-- When `t` is supported on `s`, `f ∈ s.dfinsupp t` precisely means that `f` is pointwise in `t`. -/ @[simp] theorem mem_dfinsupp_iff_of_support_subset {t : Π₀ i, Finset (α i)} (ht : t.support ⊆ s) : f ∈ s.dfinsupp t ↔ ∀ i, f i ∈ t i := by refine mem_dfinsupp_iff.trans (forall_and.symm.trans <| forall_congr' fun i => ⟨ fun h => ?_, fun h => ⟨fun hi => ht <| mem_support_iff.2 fun H => mem_support_iff.1 hi ?_, fun _ => h⟩⟩) · by_cases hi : i ∈ s · exact h.2 hi · rw [not_mem_support_iff.1 (mt h.1 hi), not_mem_support_iff.1 (not_mem_mono ht hi)] exact zero_mem_zero · rwa [H, mem_zero] at h end Finset open Finset namespace DFinsupp section BundledSingleton variable [∀ i, Zero (α i)] {f : Π₀ i, α i} {i : ι} {a : α i} /-- Pointwise `Finset.singleton` bundled as a `DFinsupp`. -/ def singleton (f : Π₀ i, α i) : Π₀ i, Finset (α i) where toFun i := {f i} support' := f.support'.map fun s => ⟨s.1, fun i => (s.prop i).imp id (congr_arg _)⟩ theorem mem_singleton_apply_iff : a ∈ f.singleton i ↔ a = f i := mem_singleton end BundledSingleton section BundledIcc variable [∀ i, Zero (α i)] [∀ i, PartialOrder (α i)] [∀ i, LocallyFiniteOrder (α i)] {f g : Π₀ i, α i} {i : ι} {a : α i} /-- Pointwise `Finset.Icc` bundled as a `DFinsupp`. -/ def rangeIcc (f g : Π₀ i, α i) : Π₀ i, Finset (α i) where toFun i := Icc (f i) (g i) support' := f.support'.bind fun fs => g.support'.map fun gs => ⟨ fs.1 + gs.1, fun i => or_iff_not_imp_left.2 fun h => by have hf : f i = 0 := (fs.prop i).resolve_left (Multiset.not_mem_mono (Multiset.Le.subset <| Multiset.le_add_right _ _) h) have hg : g i = 0 := (gs.prop i).resolve_left (Multiset.not_mem_mono (Multiset.Le.subset <| Multiset.le_add_left _ _) h) simp_rw [hf, hg] exact Icc_self _⟩ @[simp] theorem rangeIcc_apply (f g : Π₀ i, α i) (i : ι) : f.rangeIcc g i = Icc (f i) (g i) := rfl theorem mem_rangeIcc_apply_iff : a ∈ f.rangeIcc g i ↔ f i ≤ a ∧ a ≤ g i := mem_Icc theorem support_rangeIcc_subset [DecidableEq ι] [∀ i, DecidableEq (α i)] : (f.rangeIcc g).support ⊆ f.support ∪ g.support := by refine fun x hx => ?_ by_contra h refine not_mem_support_iff.2 ?_ hx rw [rangeIcc_apply, not_mem_support_iff.1 (not_mem_mono subset_union_left h), not_mem_support_iff.1 (not_mem_mono subset_union_right h)] exact Icc_self _ end BundledIcc section Pi variable [∀ i, Zero (α i)] [DecidableEq ι] [∀ i, DecidableEq (α i)] /-- Given a finitely supported function `f : Π₀ i, Finset (α i)`, one can define the finset `f.pi` of all finitely supported functions whose value at `i` is in `f i` for all `i`. -/ def pi (f : Π₀ i, Finset (α i)) : Finset (Π₀ i, α i) := f.support.dfinsupp f @[simp] theorem mem_pi {f : Π₀ i, Finset (α i)} {g : Π₀ i, α i} : g ∈ f.pi ↔ ∀ i, g i ∈ f i := mem_dfinsupp_iff_of_support_subset <| Subset.refl _ @[simp] theorem card_pi (f : Π₀ i, Finset (α i)) : #f.pi = f.prod fun i ↦ #(f i) := by rw [pi, card_dfinsupp] exact Finset.prod_congr rfl fun i _ => by simp only [Pi.natCast_apply, Nat.cast_id] end Pi section PartialOrder variable [DecidableEq ι] [∀ i, DecidableEq (α i)] variable [∀ i, PartialOrder (α i)] [∀ i, Zero (α i)] [∀ i, LocallyFiniteOrder (α i)] instance instLocallyFiniteOrder : LocallyFiniteOrder (Π₀ i, α i) := LocallyFiniteOrder.ofIcc (Π₀ i, α i) (fun f g => (f.support ∪ g.support).dfinsupp <| f.rangeIcc g) (fun f g x => by refine (mem_dfinsupp_iff_of_support_subset <| support_rangeIcc_subset).trans ?_ simp_rw [mem_rangeIcc_apply_iff, forall_and] rfl) variable (f g : Π₀ i, α i) theorem Icc_eq : Icc f g = (f.support ∪ g.support).dfinsupp (f.rangeIcc g) := rfl lemma card_Icc : #(Icc f g) = ∏ i ∈ f.support ∪ g.support, #(Icc (f i) (g i)) := card_dfinsupp _ _ lemma card_Ico : #(Ico f g) = (∏ i ∈ f.support ∪ g.support, #(Icc (f i) (g i))) - 1 := by rw [card_Ico_eq_card_Icc_sub_one, card_Icc] lemma card_Ioc : #(Ioc f g) = (∏ i ∈ f.support ∪ g.support, #(Icc (f i) (g i))) - 1 := by rw [card_Ioc_eq_card_Icc_sub_one, card_Icc] lemma card_Ioo : #(Ioo f g) = (∏ i ∈ f.support ∪ g.support, #(Icc (f i) (g i))) - 2 := by rw [card_Ioo_eq_card_Icc_sub_two, card_Icc] end PartialOrder section Lattice variable [DecidableEq ι] [∀ i, DecidableEq (α i)] [∀ i, Lattice (α i)] [∀ i, Zero (α i)] [∀ i, LocallyFiniteOrder (α i)] (f g : Π₀ i, α i) lemma card_uIcc : #(uIcc f g) = ∏ i ∈ f.support ∪ g.support, #(uIcc (f i) (g i)) := by rw [← support_inf_union_support_sup]; exact card_Icc _ _ end Lattice section CanonicallyOrdered variable [DecidableEq ι] [∀ i, DecidableEq (α i)] variable [∀ i, AddCommMonoid (α i)] [∀ i, PartialOrder (α i)] [∀ i, CanonicallyOrderedAdd (α i)] [∀ i, LocallyFiniteOrder (α i)] variable (f : Π₀ i, α i)
lemma card_Iic : #(Iic f) = ∏ i ∈ f.support, #(Iic (f i)) := by
Mathlib/Data/DFinsupp/Interval.lean
189
190
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Aaron Anderson, Yakov Pechersky -/ import Mathlib.Data.Fintype.Card import Mathlib.Algebra.Group.Commute.Basic import Mathlib.Algebra.Group.End import Mathlib.Data.Finset.NoncommProd /-! # support of a permutation ## Main definitions In the following, `f g : Equiv.Perm α`. * `Equiv.Perm.Disjoint`: two permutations `f` and `g` are `Disjoint` if every element is fixed either by `f`, or by `g`. Equivalently, `f` and `g` are `Disjoint` iff their `support` are disjoint. * `Equiv.Perm.IsSwap`: `f = swap x y` for `x ≠ y`. * `Equiv.Perm.support`: the elements `x : α` that are not fixed by `f`. Assume `α` is a Fintype: * `Equiv.Perm.fixed_point_card_lt_of_ne_one f` says that `f` has strictly less than `Fintype.card α - 1` fixed points, unless `f = 1`. (Equivalently, `f.support` has at least 2 elements.) -/ open Equiv Finset Function namespace Equiv.Perm variable {α : Type*} section Disjoint /-- Two permutations `f` and `g` are `Disjoint` if their supports are disjoint, i.e., every element is fixed either by `f`, or by `g`. -/ def Disjoint (f g : Perm α) := ∀ x, f x = x ∨ g x = x variable {f g h : Perm α} @[symm] theorem Disjoint.symm : Disjoint f g → Disjoint g f := by simp only [Disjoint, or_comm, imp_self] theorem Disjoint.symmetric : Symmetric (@Disjoint α) := fun _ _ => Disjoint.symm instance : IsSymm (Perm α) Disjoint := ⟨Disjoint.symmetric⟩ theorem disjoint_comm : Disjoint f g ↔ Disjoint g f := ⟨Disjoint.symm, Disjoint.symm⟩ theorem Disjoint.commute (h : Disjoint f g) : Commute f g := Equiv.ext fun x => (h x).elim (fun hf => (h (g x)).elim (fun hg => by simp [mul_apply, hf, hg]) fun hg => by simp [mul_apply, hf, g.injective hg]) fun hg => (h (f x)).elim (fun hf => by simp [mul_apply, f.injective hf, hg]) fun hf => by simp [mul_apply, hf, hg] @[simp] theorem disjoint_one_left (f : Perm α) : Disjoint 1 f := fun _ => Or.inl rfl @[simp] theorem disjoint_one_right (f : Perm α) : Disjoint f 1 := fun _ => Or.inr rfl theorem disjoint_iff_eq_or_eq : Disjoint f g ↔ ∀ x : α, f x = x ∨ g x = x := Iff.rfl @[simp] theorem disjoint_refl_iff : Disjoint f f ↔ f = 1 := by refine ⟨fun h => ?_, fun h => h.symm ▸ disjoint_one_left 1⟩ ext x rcases h x with hx | hx <;> simp [hx] theorem Disjoint.inv_left (h : Disjoint f g) : Disjoint f⁻¹ g := by intro x rw [inv_eq_iff_eq, eq_comm] exact h x
theorem Disjoint.inv_right (h : Disjoint f g) : Disjoint f g⁻¹ := h.symm.inv_left.symm
Mathlib/GroupTheory/Perm/Support.lean
87
90
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Topology.Constructions /-! # Neighborhoods and continuity relative to a subset This file develops API on the relative versions * `nhdsWithin` of `nhds` * `ContinuousOn` of `Continuous` * `ContinuousWithinAt` of `ContinuousAt` related to continuity, which are defined in previous definition files. Their basic properties studied in this file include the relationships between these restricted notions and the corresponding notions for the subtype equipped with the subspace topology. ## Notation * `𝓝 x`: the filter of neighborhoods of a point `x`; * `𝓟 s`: the principal filter of a set `s`; * `𝓝[s] x`: the filter `nhdsWithin x s` of neighborhoods of a point `x` within a set `s`. -/ open Set Filter Function Topology Filter variable {α β γ δ : Type*} variable [TopologicalSpace α] /-! ## Properties of the neighborhood-within filter -/ @[simp] theorem nhds_bind_nhdsWithin {a : α} {s : Set α} : ((𝓝 a).bind fun x => 𝓝[s] x) = 𝓝[s] a := bind_inf_principal.trans <| congr_arg₂ _ nhds_bind_nhds rfl @[simp] theorem eventually_nhds_nhdsWithin {a : α} {s : Set α} {p : α → Prop} : (∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝[s] y, p x) ↔ ∀ᶠ x in 𝓝[s] a, p x := Filter.ext_iff.1 nhds_bind_nhdsWithin { x | p x } theorem eventually_nhdsWithin_iff {a : α} {s : Set α} {p : α → Prop} : (∀ᶠ x in 𝓝[s] a, p x) ↔ ∀ᶠ x in 𝓝 a, x ∈ s → p x := eventually_inf_principal theorem frequently_nhdsWithin_iff {z : α} {s : Set α} {p : α → Prop} : (∃ᶠ x in 𝓝[s] z, p x) ↔ ∃ᶠ x in 𝓝 z, p x ∧ x ∈ s := frequently_inf_principal.trans <| by simp only [and_comm] theorem mem_closure_ne_iff_frequently_within {z : α} {s : Set α} : z ∈ closure (s \ {z}) ↔ ∃ᶠ x in 𝓝[≠] z, x ∈ s := by simp [mem_closure_iff_frequently, frequently_nhdsWithin_iff] @[simp] theorem eventually_eventually_nhdsWithin {a : α} {s : Set α} {p : α → Prop} : (∀ᶠ y in 𝓝[s] a, ∀ᶠ x in 𝓝[s] y, p x) ↔ ∀ᶠ x in 𝓝[s] a, p x := by refine ⟨fun h => ?_, fun h => (eventually_nhds_nhdsWithin.2 h).filter_mono inf_le_left⟩ simp only [eventually_nhdsWithin_iff] at h ⊢ exact h.mono fun x hx hxs => (hx hxs).self_of_nhds hxs @[simp] theorem eventually_mem_nhdsWithin_iff {x : α} {s t : Set α} : (∀ᶠ x' in 𝓝[s] x, t ∈ 𝓝[s] x') ↔ t ∈ 𝓝[s] x := eventually_eventually_nhdsWithin theorem nhdsWithin_eq (a : α) (s : Set α) : 𝓝[s] a = ⨅ t ∈ { t : Set α | a ∈ t ∧ IsOpen t }, 𝓟 (t ∩ s) := ((nhds_basis_opens a).inf_principal s).eq_biInf @[simp] lemma nhdsWithin_univ (a : α) : 𝓝[Set.univ] a = 𝓝 a := by rw [nhdsWithin, principal_univ, inf_top_eq] theorem nhdsWithin_hasBasis {ι : Sort*} {p : ι → Prop} {s : ι → Set α} {a : α} (h : (𝓝 a).HasBasis p s) (t : Set α) : (𝓝[t] a).HasBasis p fun i => s i ∩ t := h.inf_principal t theorem nhdsWithin_basis_open (a : α) (t : Set α) : (𝓝[t] a).HasBasis (fun u => a ∈ u ∧ IsOpen u) fun u => u ∩ t := nhdsWithin_hasBasis (nhds_basis_opens a) t theorem mem_nhdsWithin {t : Set α} {a : α} {s : Set α} : t ∈ 𝓝[s] a ↔ ∃ u, IsOpen u ∧ a ∈ u ∧ u ∩ s ⊆ t := by
simpa only [and_assoc, and_left_comm] using (nhdsWithin_basis_open a s).mem_iff theorem mem_nhdsWithin_iff_exists_mem_nhds_inter {t : Set α} {a : α} {s : Set α} :
Mathlib/Topology/ContinuousOn.lean
89
91
/- Copyright (c) 2023 Peter Nelson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Peter Nelson -/ import Mathlib.Data.Matroid.IndepAxioms /-! # Matroid Duality For a matroid `M` on ground set `E`, the collection of complements of the bases of `M` is the collection of bases of another matroid on `E` called the 'dual' of `M`. The map from `M` to its dual is an involution, interacts nicely with minors, and preserves many important matroid properties such as representability and connectivity. This file defines the dual matroid `M✶` of `M`, and gives associated API. The definition is in terms of its independent sets, using `IndepMatroid.matroid`. We also define 'Co-independence' (independence in the dual) of a set as a predicate `M.Coindep X`. This is an abbreviation for `M✶.Indep X`, but has its own name for the sake of dot notation. ## Main Definitions * `M.Dual`, written `M✶`, is the matroid on `M.E` which a set `B ⊆ M.E` is a base if and only if `M.E \ B` is a base for `M`. * `M.Coindep X` means `M✶.Indep X`, or equivalently that `X` is contained in `M.E \ B` for some base `B` of `M`. -/ assert_not_exists Field open Set namespace Matroid variable {α : Type*} {M : Matroid α} {I B X : Set α} section dual /-- Given `M : Matroid α`, the `IndepMatroid α` whose independent sets are the subsets of `M.E` that are disjoint from some base of `M` -/ @[simps] def dualIndepMatroid (M : Matroid α) : IndepMatroid α where E := M.E Indep I := I ⊆ M.E ∧ ∃ B, M.IsBase B ∧ Disjoint I B indep_empty := ⟨empty_subset M.E, M.exists_isBase.imp (fun _ hB ↦ ⟨hB, empty_disjoint _⟩)⟩ indep_subset := by rintro I J ⟨hJE, B, hB, hJB⟩ hIJ exact ⟨hIJ.trans hJE, ⟨B, hB, disjoint_of_subset_left hIJ hJB⟩⟩ indep_aug := by rintro I X ⟨hIE, B, hB, hIB⟩ hI_not_max hX_max have hXE := hX_max.1.1 have hB' := (isBase_compl_iff_maximal_disjoint_isBase hXE).mpr hX_max set B' := M.E \ X with hX have hI := (not_iff_not.mpr (isBase_compl_iff_maximal_disjoint_isBase)).mpr hI_not_max obtain ⟨B'', hB'', hB''₁, hB''₂⟩ := (hB'.indep.diff I).exists_isBase_subset_union_isBase hB rw [← compl_subset_compl, ← hIB.sdiff_eq_right, ← union_diff_distrib, diff_eq, compl_inter, compl_compl, union_subset_iff, compl_subset_compl] at hB''₂ have hssu := (subset_inter (hB''₂.2) hIE).ssubset_of_ne (by { rintro rfl; apply hI; convert hB''; simp [hB''.subset_ground] }) obtain ⟨e, ⟨(heB'' : e ∉ _), heE⟩, heI⟩ := exists_of_ssubset hssu use e simp_rw [mem_diff, insert_subset_iff, and_iff_left heI, and_iff_right heE, and_iff_right hIE] refine ⟨by_contra (fun heX ↦ heB'' (hB''₁ ⟨?_, heI⟩)), ⟨B'', hB'', ?_⟩⟩ · rw [hX]; exact ⟨heE, heX⟩ rw [← union_singleton, disjoint_union_left, disjoint_singleton_left, and_iff_left heB''] exact disjoint_of_subset_left hB''₂.2 disjoint_compl_left indep_maximal := by rintro X - I' ⟨hI'E, B, hB, hI'B⟩ hI'X obtain ⟨I, hI⟩ := M.exists_isBasis (M.E \ X) obtain ⟨B', hB', hIB', hB'IB⟩ := hI.indep.exists_isBase_subset_union_isBase hB obtain rfl : I = B' \ X := hI.eq_of_subset_indep (hB'.indep.diff _) (subset_diff.2 ⟨hIB', (subset_diff.1 hI.subset).2⟩) (diff_subset_diff_left hB'.subset_ground) simp_rw [maximal_subset_iff'] refine ⟨(X \ B') ∩ M.E, ?_, ⟨⟨inter_subset_right, ?_⟩, ?_⟩, ?_⟩ · rw [subset_inter_iff, and_iff_left hI'E, subset_diff, and_iff_right hI'X] exact Disjoint.mono_right hB'IB <| disjoint_union_right.2 ⟨disjoint_sdiff_right.mono_left hI'X , hI'B⟩ · exact ⟨B', hB', (disjoint_sdiff_left (t := X)).mono_left inter_subset_left⟩ · exact inter_subset_left.trans diff_subset simp only [subset_inter_iff, subset_diff, and_imp, forall_exists_index] refine fun J hJE B'' hB'' hdj hJX hXJ ↦ ⟨⟨hJX, ?_⟩, hJE⟩ have hI' : (B'' ∩ X) ∪ (B' \ X) ⊆ B' := by rw [union_subset_iff, and_iff_left diff_subset, ← union_diff_cancel hJX, inter_union_distrib_left, hdj.symm.inter_eq, empty_union, diff_eq, ← inter_assoc, ← diff_eq, diff_subset_comm, diff_eq, inter_assoc, ← diff_eq, inter_comm] exact subset_trans (inter_subset_inter_right _ hB''.subset_ground) hXJ obtain ⟨B₁,hB₁,hI'B₁,hB₁I⟩ := (hB'.indep.subset hI').exists_isBase_subset_union_isBase hB'' rw [union_comm, ← union_assoc, union_eq_self_of_subset_right inter_subset_left] at hB₁I obtain rfl : B₁ = B' := by refine hB₁.eq_of_subset_indep hB'.indep (fun e he ↦ ?_) refine (hB₁I he).elim (fun heB'' ↦ ?_) (fun h ↦ h.1) refine (em (e ∈ X)).elim (fun heX ↦ hI' (Or.inl ⟨heB'', heX⟩)) (fun heX ↦ hIB' ?_) refine hI.mem_of_insert_indep ⟨hB₁.subset_ground he, heX⟩ ?_ exact hB₁.indep.subset (insert_subset he (subset_union_right.trans hI'B₁)) by_contra hdj' obtain ⟨e, heJ, heB'⟩ := not_disjoint_iff.mp hdj' obtain (heB'' | ⟨-,heX⟩ ) := hB₁I heB' · exact hdj.ne_of_mem heJ heB'' rfl exact heX (hJX heJ) subset_ground := by tauto /-- The dual of a matroid; the bases are the complements (w.r.t `M.E`) of the bases of `M`. -/ def dual (M : Matroid α) : Matroid α := M.dualIndepMatroid.matroid /-- The `✶` symbol, which denotes matroid duality. (This is distinct from the usual `*` symbol for multiplication, due to precedence issues.) -/ postfix:max "✶" => Matroid.dual theorem dual_indep_iff_exists' : (M✶.Indep I) ↔ I ⊆ M.E ∧ (∃ B, M.IsBase B ∧ Disjoint I B) := Iff.rfl @[simp] theorem dual_ground : M✶.E = M.E := rfl theorem dual_indep_iff_exists (hI : I ⊆ M.E := by aesop_mat) : M✶.Indep I ↔ (∃ B, M.IsBase B ∧ Disjoint I B) := by rw [dual_indep_iff_exists', and_iff_right hI] theorem dual_dep_iff_forall : (M✶.Dep I) ↔ (∀ B, M.IsBase B → (I ∩ B).Nonempty) ∧ I ⊆ M.E := by simp_rw [dep_iff, dual_indep_iff_exists', dual_ground, and_congr_left_iff, not_and, not_exists, not_and, not_disjoint_iff_nonempty_inter, Classical.imp_iff_right_iff, iff_true_intro Or.inl] instance dual_finite [M.Finite] : M✶.Finite := ⟨M.ground_finite⟩ instance dual_nonempty [M.Nonempty] : M✶.Nonempty := ⟨M.ground_nonempty⟩ @[simp] theorem dual_isBase_iff (hB : B ⊆ M.E := by aesop_mat) : M✶.IsBase B ↔ M.IsBase (M.E \ B) := by rw [isBase_compl_iff_maximal_disjoint_isBase, isBase_iff_maximal_indep, maximal_subset_iff, maximal_subset_iff] simp [dual_indep_iff_exists', hB] theorem dual_isBase_iff' : M✶.IsBase B ↔ M.IsBase (M.E \ B) ∧ B ⊆ M.E := (em (B ⊆ M.E)).elim (fun h ↦ by rw [dual_isBase_iff, and_iff_left h]) (fun h ↦ iff_of_false (h ∘ (fun h' ↦ h'.subset_ground)) (h ∘ And.right)) theorem setOf_dual_isBase_eq : {B | M✶.IsBase B} = (fun X ↦ M.E \ X) '' {B | M.IsBase B} := by ext B simp only [mem_setOf_eq, mem_image, dual_isBase_iff'] refine ⟨fun h ↦ ⟨_, h.1, diff_diff_cancel_left h.2⟩, fun ⟨B', hB', h⟩ ↦ ⟨?_,h.symm.trans_subset diff_subset⟩⟩ rwa [← h, diff_diff_cancel_left hB'.subset_ground] @[simp] theorem dual_dual (M : Matroid α) : M✶✶ = M := ext_isBase rfl (fun B (h : B ⊆ M.E) ↦ by rw [dual_isBase_iff, dual_isBase_iff, dual_ground, diff_diff_cancel_left h]) theorem dual_involutive : Function.Involutive (dual : Matroid α → Matroid α) := dual_dual theorem dual_injective : Function.Injective (dual : Matroid α → Matroid α) := dual_involutive.injective @[simp] theorem dual_inj {M₁ M₂ : Matroid α} : M₁✶ = M₂✶ ↔ M₁ = M₂ := dual_injective.eq_iff theorem eq_dual_comm {M₁ M₂ : Matroid α} : M₁ = M₂✶ ↔ M₂ = M₁✶ := by rw [← dual_inj, dual_dual, eq_comm] theorem eq_dual_iff_dual_eq {M₁ M₂ : Matroid α} : M₁ = M₂✶ ↔ M₁✶ = M₂ := dual_involutive.eq_iff.symm theorem IsBase.compl_isBase_of_dual (h : M✶.IsBase B) : M.IsBase (M.E \ B) := (dual_isBase_iff'.1 h).1 theorem IsBase.compl_isBase_dual (h : M.IsBase B) : M✶.IsBase (M.E \ B) := by rwa [dual_isBase_iff, diff_diff_cancel_left h.subset_ground] theorem IsBase.compl_inter_isBasis_of_inter_isBasis (hB : M.IsBase B) (hBX : M.IsBasis (B ∩ X) X) : M✶.IsBasis ((M.E \ B) ∩ (M.E \ X)) (M.E \ X) := by refine Indep.isBasis_of_forall_insert ?_ inter_subset_right (fun e he ↦ ?_) · rw [dual_indep_iff_exists] exact ⟨B, hB, disjoint_of_subset_left inter_subset_left disjoint_sdiff_left⟩ simp only [diff_inter_self_eq_diff, mem_diff, not_and, not_not, imp_iff_right he.1.1] at he simp_rw [dual_dep_iff_forall, insert_subset_iff, and_iff_right he.1.1, and_iff_left (inter_subset_left.trans diff_subset)] refine fun B' hB' ↦ by_contra (fun hem ↦ ?_) rw [nonempty_iff_ne_empty, not_ne_iff, ← union_singleton, diff_inter_diff, union_inter_distrib_right, union_empty_iff, singleton_inter_eq_empty, diff_eq, inter_right_comm, inter_eq_self_of_subset_right hB'.subset_ground, ← diff_eq, diff_eq_empty] at hem obtain ⟨f, hfb, hBf⟩ := hB.exchange hB' ⟨he.2, hem.2⟩ have hi : M.Indep (insert f (B ∩ X)) := by refine hBf.indep.subset (insert_subset_insert ?_) simp_rw [subset_diff, and_iff_right inter_subset_left, disjoint_singleton_right, mem_inter_iff, iff_false_intro he.1.2, and_false, not_false_iff] exact hfb.2 (hBX.mem_of_insert_indep (Or.elim (hem.1 hfb.1) (False.elim ∘ hfb.2) id) hi).1 theorem IsBase.inter_isBasis_iff_compl_inter_isBasis_dual (hB : M.IsBase B) (hX : X ⊆ M.E := by aesop_mat) : M.IsBasis (B ∩ X) X ↔ M✶.IsBasis ((M.E \ B) ∩ (M.E \ X)) (M.E \ X) := by refine ⟨hB.compl_inter_isBasis_of_inter_isBasis, fun h ↦ ?_⟩ simpa [inter_eq_self_of_subset_right hX, inter_eq_self_of_subset_right hB.subset_ground] using hB.compl_isBase_dual.compl_inter_isBasis_of_inter_isBasis h theorem base_iff_dual_isBase_compl (hB : B ⊆ M.E := by aesop_mat) : M.IsBase B ↔ M✶.IsBase (M.E \ B) := by
rw [dual_isBase_iff, diff_diff_cancel_left hB] theorem ground_not_isBase (M : Matroid α) [h : RankPos M✶] : ¬M.IsBase M.E := by
Mathlib/Data/Matroid/Dual.lean
209
211
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Eric Wieser -/ import Mathlib.Algebra.GroupWithZero.Action.Pointwise.Set import Mathlib.Algebra.Order.Module.OrderedSMul import Mathlib.Algebra.Order.Module.Pointwise import Mathlib.Data.Real.Archimedean /-! # Pointwise operations on sets of reals This file relates `sInf (a • s)`/`sSup (a • s)` with `a • sInf s`/`a • sSup s` for `s : Set ℝ`. From these, it relates `⨅ i, a • f i` / `⨆ i, a • f i` with `a • (⨅ i, f i)` / `a • (⨆ i, f i)`, and provides lemmas about distributing `*` over `⨅` and `⨆`. ## TODO This is true more generally for conditionally complete linear order whose default value is `0`. We don't have those yet. -/ assert_not_exists Finset open Set open Pointwise variable {ι : Sort*} {α : Type*} [Field α] [LinearOrder α] [IsStrictOrderedRing α] section MulActionWithZero variable [MulActionWithZero α ℝ] [OrderedSMul α ℝ] {a : α} theorem Real.sInf_smul_of_nonneg (ha : 0 ≤ a) (s : Set ℝ) : sInf (a • s) = a • sInf s := by obtain rfl | hs := s.eq_empty_or_nonempty · rw [smul_set_empty, Real.sInf_empty, smul_zero] obtain rfl | ha' := ha.eq_or_lt · rw [zero_smul_set hs, zero_smul] exact csInf_singleton 0 by_cases h : BddBelow s · exact ((OrderIso.smulRight ha').map_csInf' hs h).symm · rw [Real.sInf_of_not_bddBelow (mt (bddBelow_smul_iff_of_pos ha').1 h), Real.sInf_of_not_bddBelow h, smul_zero] theorem Real.smul_iInf_of_nonneg (ha : 0 ≤ a) (f : ι → ℝ) : (a • ⨅ i, f i) = ⨅ i, a • f i := (Real.sInf_smul_of_nonneg ha _).symm.trans <| congr_arg sInf <| (range_comp _ _).symm theorem Real.sSup_smul_of_nonneg (ha : 0 ≤ a) (s : Set ℝ) : sSup (a • s) = a • sSup s := by obtain rfl | hs := s.eq_empty_or_nonempty · rw [smul_set_empty, Real.sSup_empty, smul_zero] obtain rfl | ha' := ha.eq_or_lt · rw [zero_smul_set hs, zero_smul] exact csSup_singleton 0 by_cases h : BddAbove s · exact ((OrderIso.smulRight ha').map_csSup' hs h).symm · rw [Real.sSup_of_not_bddAbove (mt (bddAbove_smul_iff_of_pos ha').1 h), Real.sSup_of_not_bddAbove h, smul_zero] theorem Real.smul_iSup_of_nonneg (ha : 0 ≤ a) (f : ι → ℝ) : (a • ⨆ i, f i) = ⨆ i, a • f i := (Real.sSup_smul_of_nonneg ha _).symm.trans <| congr_arg sSup <| (range_comp _ _).symm end MulActionWithZero section Module variable [Module α ℝ] [OrderedSMul α ℝ] {a : α} theorem Real.sInf_smul_of_nonpos (ha : a ≤ 0) (s : Set ℝ) : sInf (a • s) = a • sSup s := by obtain rfl | hs := s.eq_empty_or_nonempty · rw [smul_set_empty, Real.sInf_empty, Real.sSup_empty, smul_zero] obtain rfl | ha' := ha.eq_or_lt · rw [zero_smul_set hs, zero_smul] exact csInf_singleton 0 by_cases h : BddAbove s · exact ((OrderIso.smulRightDual ℝ ha').map_csSup' hs h).symm · rw [Real.sInf_of_not_bddBelow (mt (bddBelow_smul_iff_of_neg ha').1 h), Real.sSup_of_not_bddAbove h, smul_zero] theorem Real.smul_iSup_of_nonpos (ha : a ≤ 0) (f : ι → ℝ) : (a • ⨆ i, f i) = ⨅ i, a • f i := (Real.sInf_smul_of_nonpos ha _).symm.trans <| congr_arg sInf <| (range_comp _ _).symm theorem Real.sSup_smul_of_nonpos (ha : a ≤ 0) (s : Set ℝ) : sSup (a • s) = a • sInf s := by obtain rfl | hs := s.eq_empty_or_nonempty · rw [smul_set_empty, Real.sSup_empty, Real.sInf_empty, smul_zero] obtain rfl | ha' := ha.eq_or_lt · rw [zero_smul_set hs, zero_smul] exact csSup_singleton 0 by_cases h : BddBelow s · exact ((OrderIso.smulRightDual ℝ ha').map_csInf' hs h).symm · rw [Real.sSup_of_not_bddAbove (mt (bddAbove_smul_iff_of_neg ha').1 h), Real.sInf_of_not_bddBelow h, smul_zero] theorem Real.smul_iInf_of_nonpos (ha : a ≤ 0) (f : ι → ℝ) : (a • ⨅ i, f i) = ⨆ i, a • f i := (Real.sSup_smul_of_nonpos ha _).symm.trans <| congr_arg sSup <| (range_comp _ _).symm end Module /-! ## Special cases for real multiplication -/ section Mul variable {r : ℝ} theorem Real.mul_iInf_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : (r * ⨅ i, f i) = ⨅ i, r * f i := Real.smul_iInf_of_nonneg ha f theorem Real.mul_iSup_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : (r * ⨆ i, f i) = ⨆ i, r * f i := Real.smul_iSup_of_nonneg ha f theorem Real.mul_iInf_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : (r * ⨅ i, f i) = ⨆ i, r * f i := Real.smul_iInf_of_nonpos ha f theorem Real.mul_iSup_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : (r * ⨆ i, f i) = ⨅ i, r * f i := Real.smul_iSup_of_nonpos ha f theorem Real.iInf_mul_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : (⨅ i, f i) * r = ⨅ i, f i * r := by simp only [Real.mul_iInf_of_nonneg ha, mul_comm] theorem Real.iSup_mul_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : (⨆ i, f i) * r = ⨆ i, f i * r := by simp only [Real.mul_iSup_of_nonneg ha, mul_comm] theorem Real.iInf_mul_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : (⨅ i, f i) * r = ⨆ i, f i * r := by simp only [Real.mul_iInf_of_nonpos ha, mul_comm] theorem Real.iSup_mul_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : (⨆ i, f i) * r = ⨅ i, f i * r := by simp only [Real.mul_iSup_of_nonpos ha, mul_comm] end Mul
Mathlib/Data/Real/Pointwise.lean
136
137
/- 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₃⟩
Mathlib/SetTheory/Ordinal/Arithmetic.lean
598
599
/- 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 : α} theorem encard_insert_le (s : Set α) (x : α) : (insert x s).encard ≤ s.encard + 1 := by rw [← union_singleton, ← encard_singleton x]; apply encard_union_le theorem encard_singleton_inter (s : Set α) (x : α) : ({x} ∩ s).encard ≤ 1 := by rw [← encard_singleton x]; exact encard_le_encard inter_subset_left theorem encard_diff_singleton_add_one (h : a ∈ s) : (s \ {a}).encard + 1 = s.encard := by rw [← encard_insert_of_not_mem (fun h ↦ h.2 rfl), insert_diff_singleton, insert_eq_of_mem h] theorem encard_diff_singleton_of_mem (h : a ∈ s) : (s \ {a}).encard = s.encard - 1 := by rw [← encard_diff_singleton_add_one h, ← WithTop.add_right_inj WithTop.one_ne_top, tsub_add_cancel_of_le (self_le_add_left _ _)] theorem encard_tsub_one_le_encard_diff_singleton (s : Set α) (x : α) : s.encard - 1 ≤ (s \ {x}).encard := by rw [← encard_singleton x]; apply tsub_encard_le_encard_diff theorem encard_exchange (ha : a ∉ s) (hb : b ∈ s) : (insert a (s \ {b})).encard = s.encard := by rw [encard_insert_of_not_mem, encard_diff_singleton_add_one hb] simp_all only [not_true, mem_diff, mem_singleton_iff, false_and, not_false_eq_true] theorem encard_exchange' (ha : a ∉ s) (hb : b ∈ s) : (insert a s \ {b}).encard = s.encard := by rw [← insert_diff_singleton_comm (by rintro rfl; exact ha hb), encard_exchange ha hb] theorem encard_eq_add_one_iff {k : ℕ∞} : s.encard = k + 1 ↔ (∃ a t, ¬a ∈ t ∧ insert a t = s ∧ t.encard = k) := by refine ⟨fun h ↦ ?_, ?_⟩ · obtain ⟨a, ha⟩ := nonempty_of_encard_ne_zero (s := s) (by simp [h]) refine ⟨a, s \ {a}, fun h ↦ h.2 rfl, by rwa [insert_diff_singleton, insert_eq_of_mem], ?_⟩ rw [← WithTop.add_right_inj WithTop.one_ne_top, ← h, encard_diff_singleton_add_one ha] rintro ⟨a, t, h, rfl, rfl⟩ rw [encard_insert_of_not_mem h] /-- Every set is either empty, infinite, or can have its `encard` reduced by a removal. Intended for well-founded induction on the value of `encard`. -/ theorem eq_empty_or_encard_eq_top_or_encard_diff_singleton_lt (s : Set α) : s = ∅ ∨ s.encard = ⊤ ∨ ∃ a ∈ s, (s \ {a}).encard < s.encard := by refine s.eq_empty_or_nonempty.elim Or.inl (Or.inr ∘ fun ⟨a,ha⟩ ↦ (s.finite_or_infinite.elim (fun hfin ↦ Or.inr ⟨a, ha, ?_⟩) (Or.inl ∘ Infinite.encard_eq))) rw [← encard_diff_singleton_add_one ha]; nth_rw 1 [← add_zero (encard _)] exact WithTop.add_lt_add_left hfin.diff.encard_lt_top.ne zero_lt_one end InsertErase section SmallSets theorem encard_pair {x y : α} (hne : x ≠ y) : ({x, y} : Set α).encard = 2 := by rw [encard_insert_of_not_mem (by simpa), ← one_add_one_eq_two, WithTop.add_right_inj WithTop.one_ne_top, encard_singleton] theorem encard_eq_one : s.encard = 1 ↔ ∃ x, s = {x} := by refine ⟨fun h ↦ ?_, fun ⟨x, hx⟩ ↦ by rw [hx, encard_singleton]⟩ obtain ⟨x, hx⟩ := nonempty_of_encard_ne_zero (s := s) (by rw [h]; simp) exact ⟨x, ((finite_singleton x).eq_of_subset_of_encard_le (by simpa) (by simp [h])).symm⟩ theorem encard_le_one_iff_eq : s.encard ≤ 1 ↔ s = ∅ ∨ ∃ x, s = {x} := by rw [le_iff_lt_or_eq, lt_iff_not_le, ENat.one_le_iff_ne_zero, not_not, encard_eq_zero, encard_eq_one] theorem encard_le_one_iff : s.encard ≤ 1 ↔ ∀ a b, a ∈ s → b ∈ s → a = b := by rw [encard_le_one_iff_eq, or_iff_not_imp_left, ← Ne, ← nonempty_iff_ne_empty] refine ⟨fun h a b has hbs ↦ ?_, fun h ⟨x, hx⟩ ↦ ⟨x, ((singleton_subset_iff.2 hx).antisymm' (fun y hy ↦ h _ _ hy hx))⟩⟩ obtain ⟨x, rfl⟩ := h ⟨_, has⟩ rw [(has : a = x), (hbs : b = x)] theorem encard_le_one_iff_subsingleton : s.encard ≤ 1 ↔ s.Subsingleton := by rw [encard_le_one_iff, Set.Subsingleton] tauto theorem one_lt_encard_iff_nontrivial : 1 < s.encard ↔ s.Nontrivial := by rw [← not_iff_not, not_lt, Set.not_nontrivial_iff, ← encard_le_one_iff_subsingleton] theorem one_lt_encard_iff : 1 < s.encard ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b := by rw [← not_iff_not, not_exists, not_lt, encard_le_one_iff]; aesop theorem exists_ne_of_one_lt_encard (h : 1 < s.encard) (a : α) : ∃ b ∈ s, b ≠ a := by by_contra! h' obtain ⟨b, b', hb, hb', hne⟩ := one_lt_encard_iff.1 h apply hne rw [h' b hb, h' b' hb'] theorem encard_eq_two : s.encard = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} := by refine ⟨fun h ↦ ?_, fun ⟨x, y, hne, hs⟩ ↦ by rw [hs, encard_pair hne]⟩ obtain ⟨x, hx⟩ := nonempty_of_encard_ne_zero (s := s) (by rw [h]; simp) rw [← insert_eq_of_mem hx, ← insert_diff_singleton, encard_insert_of_not_mem (fun h ↦ h.2 rfl), ← one_add_one_eq_two, WithTop.add_right_inj (WithTop.one_ne_top), encard_eq_one] at h obtain ⟨y, h⟩ := h refine ⟨x, y, by rintro rfl; exact (h.symm.subset rfl).2 rfl, ?_⟩ rw [← h, insert_diff_singleton, insert_eq_of_mem hx] theorem encard_eq_three {α : Type u_1} {s : Set α} : encard s = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} := by refine ⟨fun h ↦ ?_, fun ⟨x, y, z, hxy, hyz, hxz, hs⟩ ↦ ?_⟩ · obtain ⟨x, hx⟩ := nonempty_of_encard_ne_zero (s := s) (by rw [h]; simp) rw [← insert_eq_of_mem hx, ← insert_diff_singleton, encard_insert_of_not_mem (fun h ↦ h.2 rfl), (by exact rfl : (3 : ℕ∞) = 2 + 1), WithTop.add_right_inj WithTop.one_ne_top, encard_eq_two] at h obtain ⟨y, z, hne, hs⟩ := h refine ⟨x, y, z, ?_, ?_, hne, ?_⟩ · rintro rfl; exact (hs.symm.subset (Or.inl rfl)).2 rfl · rintro rfl; exact (hs.symm.subset (Or.inr rfl)).2 rfl rw [← hs, insert_diff_singleton, insert_eq_of_mem hx] rw [hs, encard_insert_of_not_mem, encard_insert_of_not_mem, encard_singleton] <;> aesop theorem Nat.encard_range (k : ℕ) : {i | i < k}.encard = k := by convert encard_coe_eq_coe_finsetCard (Finset.range k) using 1 · rw [Finset.coe_range, Iio_def] rw [Finset.card_range] end SmallSets theorem Finite.eq_insert_of_subset_of_encard_eq_succ (hs : s.Finite) (h : s ⊆ t) (hst : t.encard = s.encard + 1) : ∃ a, t = insert a s := by rw [← encard_diff_add_encard_of_subset h, add_comm, WithTop.add_left_inj hs.encard_lt_top.ne, encard_eq_one] at hst obtain ⟨x, hx⟩ := hst; use x; rw [← diff_union_of_subset h, hx, singleton_union] theorem exists_subset_encard_eq {k : ℕ∞} (hk : k ≤ s.encard) : ∃ t, t ⊆ s ∧ t.encard = k := by revert hk refine ENat.nat_induction k (fun _ ↦ ⟨∅, empty_subset _, by simp⟩) (fun n IH hle ↦ ?_) ?_ · obtain ⟨t₀, ht₀s, ht₀⟩ := IH (le_trans (by simp) hle) simp only [Nat.cast_succ] at * have hne : t₀ ≠ s := by rintro rfl; rw [ht₀, ← Nat.cast_one, ← Nat.cast_add, Nat.cast_le] at hle; simp at hle obtain ⟨x, hx⟩ := exists_of_ssubset (ht₀s.ssubset_of_ne hne) exact ⟨insert x t₀, insert_subset hx.1 ht₀s, by rw [encard_insert_of_not_mem hx.2, ht₀]⟩ simp only [top_le_iff, encard_eq_top_iff] exact fun _ hi ↦ ⟨s, Subset.rfl, hi⟩ theorem exists_superset_subset_encard_eq {k : ℕ∞} (hst : s ⊆ t) (hsk : s.encard ≤ k) (hkt : k ≤ t.encard) : ∃ r, s ⊆ r ∧ r ⊆ t ∧ r.encard = k := by obtain (hs | hs) := eq_or_ne s.encard ⊤ · rw [hs, top_le_iff] at hsk; subst hsk; exact ⟨s, Subset.rfl, hst, hs⟩ obtain ⟨k, rfl⟩ := exists_add_of_le hsk obtain ⟨k', hk'⟩ := exists_add_of_le hkt have hk : k ≤ encard (t \ s) := by rw [← encard_diff_add_encard_of_subset hst, add_comm] at hkt exact WithTop.le_of_add_le_add_right hs hkt obtain ⟨r', hr', rfl⟩ := exists_subset_encard_eq hk refine ⟨s ∪ r', subset_union_left, union_subset hst (hr'.trans diff_subset), ?_⟩ rw [encard_union_eq (disjoint_of_subset_right hr' disjoint_sdiff_right)] section Function variable {s : Set α} {t : Set β} {f : α → β} theorem InjOn.encard_image (h : InjOn f s) : (f '' s).encard = s.encard := by rw [encard, ENat.card_image_of_injOn h, encard] theorem encard_congr (e : s ≃ t) : s.encard = t.encard := by rw [← encard_univ_coe, ← encard_univ_coe t, encard_univ, encard_univ, ENat.card_congr e] theorem _root_.Function.Injective.encard_image (hf : f.Injective) (s : Set α) : (f '' s).encard = s.encard := hf.injOn.encard_image theorem _root_.Function.Embedding.encard_le (e : s ↪ t) : s.encard ≤ t.encard := by rw [← encard_univ_coe, ← e.injective.encard_image, ← Subtype.coe_injective.encard_image] exact encard_mono (by simp) theorem encard_image_le (f : α → β) (s : Set α) : (f '' s).encard ≤ s.encard := by obtain (h | h) := isEmpty_or_nonempty α · rw [s.eq_empty_of_isEmpty]; simp rw [← (f.invFunOn_injOn_image s).encard_image] apply encard_le_encard exact f.invFunOn_image_image_subset s theorem Finite.injOn_of_encard_image_eq (hs : s.Finite) (h : (f '' s).encard = s.encard) : InjOn f s := by obtain (h' | hne) := isEmpty_or_nonempty α · rw [s.eq_empty_of_isEmpty]; simp rw [← (f.invFunOn_injOn_image s).encard_image] at h rw [injOn_iff_invFunOn_image_image_eq_self] exact hs.eq_of_subset_of_encard_le' (f.invFunOn_image_image_subset s) h.symm.le theorem encard_preimage_of_injective_subset_range (hf : f.Injective) (ht : t ⊆ range f) : (f ⁻¹' t).encard = t.encard := by rw [← hf.encard_image, image_preimage_eq_inter_range, inter_eq_self_of_subset_left ht] lemma encard_preimage_of_bijective (hf : f.Bijective) (t : Set β) : (f ⁻¹' t).encard = t.encard := encard_preimage_of_injective_subset_range hf.injective (by simp [hf.surjective.range_eq]) theorem encard_le_encard_of_injOn (hf : MapsTo f s t) (f_inj : InjOn f s) : s.encard ≤ t.encard := by rw [← f_inj.encard_image]; apply encard_le_encard; rintro _ ⟨x, hx, rfl⟩; exact hf hx theorem Finite.exists_injOn_of_encard_le [Nonempty β] {s : Set α} {t : Set β} (hs : s.Finite) (hle : s.encard ≤ t.encard) : ∃ (f : α → β), s ⊆ f ⁻¹' t ∧ InjOn f s := by classical obtain (rfl | h | ⟨a, has, -⟩) := s.eq_empty_or_encard_eq_top_or_encard_diff_singleton_lt · simp · exact (encard_ne_top_iff.mpr hs h).elim obtain ⟨b, hbt⟩ := encard_pos.1 ((encard_pos.2 ⟨_, has⟩).trans_le hle) have hle' : (s \ {a}).encard ≤ (t \ {b}).encard := by rwa [← WithTop.add_le_add_iff_right WithTop.one_ne_top, encard_diff_singleton_add_one has, encard_diff_singleton_add_one hbt] obtain ⟨f₀, hf₀s, hinj⟩ := exists_injOn_of_encard_le hs.diff hle' simp only [preimage_diff, subset_def, mem_diff, mem_singleton_iff, mem_preimage, and_imp] at hf₀s use Function.update f₀ a b rw [← insert_eq_of_mem has, ← insert_diff_singleton, injOn_insert (fun h ↦ h.2 rfl)] simp only [mem_diff, mem_singleton_iff, not_true, and_false, insert_diff_singleton, subset_def, mem_insert_iff, mem_preimage, ne_eq, Function.update_apply, forall_eq_or_imp, ite_true, and_imp, mem_image, ite_eq_left_iff, not_exists, not_and, not_forall, exists_prop, and_iff_right hbt] refine ⟨?_, ?_, fun x hxs hxa ↦ ⟨hxa, (hf₀s x hxs hxa).2⟩⟩ · rintro x hx; split_ifs with h · assumption · exact (hf₀s x hx h).1 exact InjOn.congr hinj (fun x ⟨_, hxa⟩ ↦ by rwa [Function.update_of_ne]) termination_by encard s theorem Finite.exists_bijOn_of_encard_eq [Nonempty β] (hs : s.Finite) (h : s.encard = t.encard) : ∃ (f : α → β), BijOn f s t := by obtain ⟨f, hf, hinj⟩ := hs.exists_injOn_of_encard_le h.le; use f convert hinj.bijOn_image rw [(hs.image f).eq_of_subset_of_encard_le (image_subset_iff.mpr hf) (h.symm.trans hinj.encard_image.symm).le] end Function section ncard open Nat /-- A tactic (for use in default params) that applies `Set.toFinite` to synthesize a `Set.Finite` term. -/ syntax "toFinite_tac" : tactic macro_rules | `(tactic| toFinite_tac) => `(tactic| apply Set.toFinite) /-- A tactic useful for transferring proofs for `encard` to their corresponding `card` statements -/ syntax "to_encard_tac" : tactic macro_rules | `(tactic| to_encard_tac) => `(tactic| simp only [← Nat.cast_le (α := ℕ∞), ← Nat.cast_inj (R := ℕ∞), Nat.cast_add, Nat.cast_one]) /-- The cardinality of `s : Set α` . Has the junk value `0` if `s` is infinite -/ noncomputable def ncard (s : Set α) : ℕ := ENat.toNat s.encard theorem ncard_def (s : Set α) : s.ncard = ENat.toNat s.encard := rfl theorem Finite.cast_ncard_eq (hs : s.Finite) : s.ncard = s.encard := by rwa [ncard, ENat.coe_toNat_eq_self, ne_eq, encard_eq_top_iff, Set.Infinite, not_not] lemma ncard_le_encard (s : Set α) : s.ncard ≤ s.encard := ENat.coe_toNat_le_self _ theorem Nat.card_coe_set_eq (s : Set α) : Nat.card s = s.ncard := by obtain (h | h) := s.finite_or_infinite · have := h.fintype rw [ncard, h.encard_eq_coe_toFinset_card, Nat.card_eq_fintype_card, toFinite_toFinset, toFinset_card, ENat.toNat_coe] have := infinite_coe_iff.2 h rw [ncard, h.encard_eq, Nat.card_eq_zero_of_infinite, ENat.toNat_top] theorem ncard_eq_toFinset_card (s : Set α) (hs : s.Finite := by toFinite_tac) : s.ncard = hs.toFinset.card := by rw [← Nat.card_coe_set_eq, @Nat.card_eq_fintype_card _ hs.fintype, @Finite.card_toFinset _ _ hs.fintype hs] theorem ncard_eq_toFinset_card' (s : Set α) [Fintype s] : s.ncard = s.toFinset.card := by simp [← Nat.card_coe_set_eq, Nat.card_eq_fintype_card] lemma cast_ncard {s : Set α} (hs : s.Finite) : (s.ncard : Cardinal) = Cardinal.mk s := @Nat.cast_card _ hs theorem encard_le_coe_iff_finite_ncard_le {k : ℕ} : s.encard ≤ k ↔ s.Finite ∧ s.ncard ≤ k := by rw [encard_le_coe_iff, and_congr_right_iff] exact fun hfin ↦ ⟨fun ⟨n₀, hn₀, hle⟩ ↦ by rwa [ncard_def, hn₀, ENat.toNat_coe], fun h ↦ ⟨s.ncard, by rw [hfin.cast_ncard_eq], h⟩⟩ theorem Infinite.ncard (hs : s.Infinite) : s.ncard = 0 := by rw [← Nat.card_coe_set_eq, @Nat.card_eq_zero_of_infinite _ hs.to_subtype] @[gcongr] theorem ncard_le_ncard (hst : s ⊆ t) (ht : t.Finite := by toFinite_tac) : s.ncard ≤ t.ncard := by rw [← Nat.cast_le (α := ℕ∞), ht.cast_ncard_eq, (ht.subset hst).cast_ncard_eq] exact encard_mono hst theorem ncard_mono [Finite α] : @Monotone (Set α) _ _ _ ncard := fun _ _ ↦ ncard_le_ncard @[simp] theorem ncard_eq_zero (hs : s.Finite := by toFinite_tac) : s.ncard = 0 ↔ s = ∅ := by rw [← Nat.cast_inj (R := ℕ∞), hs.cast_ncard_eq, Nat.cast_zero, encard_eq_zero] @[simp, norm_cast] theorem ncard_coe_Finset (s : Finset α) : (s : Set α).ncard = s.card := by rw [ncard_eq_toFinset_card _, Finset.finite_toSet_toFinset] theorem ncard_univ (α : Type*) : (univ : Set α).ncard = Nat.card α := by rcases finite_or_infinite α with h | h · have hft := Fintype.ofFinite α rw [ncard_eq_toFinset_card, Finite.toFinset_univ, Finset.card_univ, Nat.card_eq_fintype_card] rw [Nat.card_eq_zero_of_infinite, Infinite.ncard] exact infinite_univ @[simp] theorem ncard_empty (α : Type*) : (∅ : Set α).ncard = 0 := by rw [ncard_eq_zero] theorem ncard_pos (hs : s.Finite := by toFinite_tac) : 0 < s.ncard ↔ s.Nonempty := by rw [pos_iff_ne_zero, Ne, ncard_eq_zero hs, nonempty_iff_ne_empty] protected alias ⟨_, Nonempty.ncard_pos⟩ := ncard_pos theorem ncard_ne_zero_of_mem {a : α} (h : a ∈ s) (hs : s.Finite := by toFinite_tac) : s.ncard ≠ 0 := ((ncard_pos hs).mpr ⟨a, h⟩).ne.symm theorem finite_of_ncard_ne_zero (hs : s.ncard ≠ 0) : s.Finite := s.finite_or_infinite.elim id fun h ↦ (hs h.ncard).elim theorem finite_of_ncard_pos (hs : 0 < s.ncard) : s.Finite := finite_of_ncard_ne_zero hs.ne.symm theorem nonempty_of_ncard_ne_zero (hs : s.ncard ≠ 0) : s.Nonempty := by rw [nonempty_iff_ne_empty]; rintro rfl; simp at hs @[simp] theorem ncard_singleton (a : α) : ({a} : Set α).ncard = 1 := by simp [ncard] theorem ncard_singleton_inter (a : α) (s : Set α) : ({a} ∩ s).ncard ≤ 1 := by rw [← Nat.cast_le (α := ℕ∞), (toFinite _).cast_ncard_eq, Nat.cast_one] apply encard_singleton_inter @[simp] theorem ncard_prod : (s ×ˢ t).ncard = s.ncard * t.ncard := by simp [ncard, ENat.toNat_mul] @[simp] theorem ncard_powerset (s : Set α) (hs : s.Finite := by toFinite_tac) : (𝒫 s).ncard = 2 ^ s.ncard := by have h := Cardinal.mk_powerset s rw [← cast_ncard hs.powerset, ← cast_ncard hs] at h norm_cast at h section InsertErase @[simp] theorem ncard_insert_of_not_mem {a : α} (h : a ∉ s) (hs : s.Finite := by toFinite_tac) : (insert a s).ncard = s.ncard + 1 := by rw [← Nat.cast_inj (R := ℕ∞), (hs.insert a).cast_ncard_eq, Nat.cast_add, Nat.cast_one, hs.cast_ncard_eq, encard_insert_of_not_mem h]
theorem ncard_insert_of_mem {a : α} (h : a ∈ s) : ncard (insert a s) = s.ncard := by
Mathlib/Data/Set/Card.lean
601
602
/- 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 := WithBot.giUnbotDBot.gc.monotone_l hpq @[simp] theorem degree_C (ha : a ≠ 0) : degree (C a) = (0 : WithBot ℕ) := by rw [degree, ← monomial_zero_left, support_monomial 0 ha, max_eq_sup_coe, sup_singleton, WithBot.coe_zero] theorem degree_C_le : degree (C a) ≤ 0 := by by_cases h : a = 0 · rw [h, C_0] exact bot_le · rw [degree_C h] theorem degree_C_lt : degree (C a) < 1 := degree_C_le.trans_lt <| WithBot.coe_lt_coe.mpr zero_lt_one theorem degree_one_le : degree (1 : R[X]) ≤ (0 : WithBot ℕ) := by rw [← C_1]; exact degree_C_le @[simp] theorem natDegree_C (a : R) : natDegree (C a) = 0 := by by_cases ha : a = 0 · have : C a = 0 := by rw [ha, C_0] rw [natDegree, degree_eq_bot.2 this, WithBot.unbotD_bot] · rw [natDegree, degree_C ha, WithBot.unbotD_zero] @[simp] theorem natDegree_one : natDegree (1 : R[X]) = 0 := natDegree_C 1 @[simp] theorem natDegree_natCast (n : ℕ) : natDegree (n : R[X]) = 0 := by simp only [← C_eq_natCast, natDegree_C] @[simp] theorem natDegree_ofNat (n : ℕ) [Nat.AtLeastTwo n] : natDegree (ofNat(n) : R[X]) = 0 := natDegree_natCast _ theorem degree_natCast_le (n : ℕ) : degree (n : R[X]) ≤ 0 := degree_le_of_natDegree_le (by simp) @[simp] theorem degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (monomial n a) = n := by rw [degree, support_monomial n ha, max_singleton, Nat.cast_withBot] @[simp] theorem degree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [C_mul_X_pow_eq_monomial, degree_monomial n ha] theorem degree_C_mul_X (ha : a ≠ 0) : degree (C a * X) = 1 := by simpa only [pow_one] using degree_C_mul_X_pow 1 ha theorem degree_monomial_le (n : ℕ) (a : R) : degree (monomial n a) ≤ n := letI := Classical.decEq R if h : a = 0 then by rw [h, (monomial n).map_zero, degree_zero]; exact bot_le else le_of_eq (degree_monomial n h) theorem degree_C_mul_X_pow_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n := by rw [C_mul_X_pow_eq_monomial] apply degree_monomial_le theorem degree_C_mul_X_le (a : R) : degree (C a * X) ≤ 1 := by simpa only [pow_one] using degree_C_mul_X_pow_le 1 a @[simp] theorem natDegree_C_mul_X_pow (n : ℕ) (a : R) (ha : a ≠ 0) : natDegree (C a * X ^ n) = n := natDegree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha) @[simp] theorem natDegree_C_mul_X (a : R) (ha : a ≠ 0) : natDegree (C a * X) = 1 := by simpa only [pow_one] using natDegree_C_mul_X_pow 1 a ha @[simp] theorem natDegree_monomial [DecidableEq R] (i : ℕ) (r : R) : natDegree (monomial i r) = if r = 0 then 0 else i := by split_ifs with hr · simp [hr] · rw [← C_mul_X_pow_eq_monomial, natDegree_C_mul_X_pow i r hr] theorem natDegree_monomial_le (a : R) {m : ℕ} : (monomial m a).natDegree ≤ m := by classical rw [Polynomial.natDegree_monomial] split_ifs exacts [Nat.zero_le _, le_rfl] theorem natDegree_monomial_eq (i : ℕ) {r : R} (r0 : r ≠ 0) : (monomial i r).natDegree = i := letI := Classical.decEq R Eq.trans (natDegree_monomial _ _) (if_neg r0) theorem coeff_ne_zero_of_eq_degree (hn : degree p = n) : coeff p n ≠ 0 := fun h => mem_support_iff.mp (mem_of_max hn) h theorem degree_X_pow_le (n : ℕ) : degree (X ^ n : R[X]) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1 : R) theorem degree_X_le : degree (X : R[X]) ≤ 1 := degree_monomial_le _ _ theorem natDegree_X_le : (X : R[X]).natDegree ≤ 1 := natDegree_le_of_degree_le degree_X_le theorem withBotSucc_degree_eq_natDegree_add_one (h : p ≠ 0) : p.degree.succ = p.natDegree + 1 := by rw [degree_eq_natDegree h] exact WithBot.succ_coe p.natDegree end Semiring section NonzeroSemiring variable [Semiring R] [Nontrivial R] {p q : R[X]} @[simp] theorem degree_one : degree (1 : R[X]) = (0 : WithBot ℕ) := degree_C one_ne_zero @[simp] theorem degree_X : degree (X : R[X]) = 1 := degree_monomial _ one_ne_zero @[simp] theorem natDegree_X : (X : R[X]).natDegree = 1 := natDegree_eq_of_degree_eq_some degree_X end NonzeroSemiring section Ring variable [Ring R] @[simp] theorem degree_neg (p : R[X]) : degree (-p) = degree p := by unfold degree; rw [support_neg] theorem degree_neg_le_of_le {a : WithBot ℕ} {p : R[X]} (hp : degree p ≤ a) : degree (-p) ≤ a := p.degree_neg.le.trans hp @[simp] theorem natDegree_neg (p : R[X]) : natDegree (-p) = natDegree p := by simp [natDegree] theorem natDegree_neg_le_of_le {p : R[X]} (hp : natDegree p ≤ m) : natDegree (-p) ≤ m := (natDegree_neg p).le.trans hp @[simp] theorem natDegree_intCast (n : ℤ) : natDegree (n : R[X]) = 0 := by rw [← C_eq_intCast, natDegree_C] theorem degree_intCast_le (n : ℤ) : degree (n : R[X]) ≤ 0 := degree_le_of_natDegree_le (by simp) @[simp] theorem leadingCoeff_neg (p : R[X]) : (-p).leadingCoeff = -p.leadingCoeff := by rw [leadingCoeff, leadingCoeff, natDegree_neg, coeff_neg] end Ring section Semiring variable [Semiring R] {p : R[X]} /-- The second-highest coefficient, or 0 for constants -/ def nextCoeff (p : R[X]) : R := if p.natDegree = 0 then 0 else p.coeff (p.natDegree - 1) lemma nextCoeff_eq_zero : p.nextCoeff = 0 ↔ p.natDegree = 0 ∨ 0 < p.natDegree ∧ p.coeff (p.natDegree - 1) = 0 := by simp [nextCoeff, or_iff_not_imp_left, pos_iff_ne_zero]; aesop lemma nextCoeff_ne_zero : p.nextCoeff ≠ 0 ↔ p.natDegree ≠ 0 ∧ p.coeff (p.natDegree - 1) ≠ 0 := by simp [nextCoeff] @[simp] theorem nextCoeff_C_eq_zero (c : R) : nextCoeff (C c) = 0 := by rw [nextCoeff] simp theorem nextCoeff_of_natDegree_pos (hp : 0 < p.natDegree) : nextCoeff p = p.coeff (p.natDegree - 1) := by rw [nextCoeff, if_neg] contrapose! hp simpa variable {p q : R[X]} {ι : Type*} theorem degree_add_le (p q : R[X]) : degree (p + q) ≤ max (degree p) (degree q) := by simpa only [degree, ← support_toFinsupp, toFinsupp_add] using AddMonoidAlgebra.sup_support_add_le _ _ _ theorem degree_add_le_of_degree_le {p q : R[X]} {n : ℕ} (hp : degree p ≤ n) (hq : degree q ≤ n) : degree (p + q) ≤ n := (degree_add_le p q).trans <| max_le hp hq theorem degree_add_le_of_le {a b : WithBot ℕ} (hp : degree p ≤ a) (hq : degree q ≤ b) : degree (p + q) ≤ max a b := (p.degree_add_le q).trans <| max_le_max ‹_› ‹_› theorem natDegree_add_le (p q : R[X]) : natDegree (p + q) ≤ max (natDegree p) (natDegree q) := by rcases le_max_iff.1 (degree_add_le p q) with h | h <;> simp [natDegree_le_natDegree h] theorem natDegree_add_le_of_degree_le {p q : R[X]} {n : ℕ} (hp : natDegree p ≤ n) (hq : natDegree q ≤ n) : natDegree (p + q) ≤ n := (natDegree_add_le p q).trans <| max_le hp hq theorem natDegree_add_le_of_le (hp : natDegree p ≤ m) (hq : natDegree q ≤ n) : natDegree (p + q) ≤ max m n := (p.natDegree_add_le q).trans <| max_le_max ‹_› ‹_› @[simp] theorem leadingCoeff_zero : leadingCoeff (0 : R[X]) = 0 := rfl @[simp] theorem leadingCoeff_eq_zero : leadingCoeff p = 0 ↔ p = 0 := ⟨fun h => Classical.by_contradiction fun hp => mt mem_support_iff.1 (Classical.not_not.2 h) (mem_of_max (degree_eq_natDegree hp)), fun h => h.symm ▸ leadingCoeff_zero⟩ theorem leadingCoeff_ne_zero : leadingCoeff p ≠ 0 ↔ p ≠ 0 := by rw [Ne, leadingCoeff_eq_zero] theorem leadingCoeff_eq_zero_iff_deg_eq_bot : leadingCoeff p = 0 ↔ degree p = ⊥ := by rw [leadingCoeff_eq_zero, degree_eq_bot] theorem natDegree_C_mul_X_pow_le (a : R) (n : ℕ) : natDegree (C a * X ^ n) ≤ n := natDegree_le_iff_degree_le.2 <| degree_C_mul_X_pow_le _ _ theorem degree_erase_le (p : R[X]) (n : ℕ) : degree (p.erase n) ≤ degree p := by rcases p with ⟨p⟩ simp only [erase_def, degree, coeff, support] apply sup_mono rw [Finsupp.support_erase] apply Finset.erase_subset theorem degree_erase_lt (hp : p ≠ 0) : degree (p.erase (natDegree p)) < degree p := by apply lt_of_le_of_ne (degree_erase_le _ _) rw [degree_eq_natDegree hp, degree, support_erase] exact fun h => not_mem_erase _ _ (mem_of_max h) theorem degree_update_le (p : R[X]) (n : ℕ) (a : R) : degree (p.update n a) ≤ max (degree p) n := by classical rw [degree, support_update] split_ifs · exact (Finset.max_mono (erase_subset _ _)).trans (le_max_left _ _) · rw [max_insert, max_comm] exact le_rfl theorem degree_sum_le (s : Finset ι) (f : ι → R[X]) : degree (∑ i ∈ s, f i) ≤ s.sup fun b => degree (f b) := Finset.cons_induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) fun a s has ih => calc degree (∑ i ∈ cons a s has, f i) ≤ max (degree (f a)) (degree (∑ i ∈ s, f i)) := by rw [Finset.sum_cons]; exact degree_add_le _ _ _ ≤ _ := by rw [sup_cons]; exact max_le_max le_rfl ih theorem degree_mul_le (p q : R[X]) : degree (p * q) ≤ degree p + degree q := by simpa only [degree, ← support_toFinsupp, toFinsupp_mul] using AddMonoidAlgebra.sup_support_mul_le (WithBot.coe_add _ _).le _ _ theorem degree_mul_le_of_le {a b : WithBot ℕ} (hp : degree p ≤ a) (hq : degree q ≤ b) : degree (p * q) ≤ a + b := (p.degree_mul_le _).trans <| add_le_add ‹_› ‹_› theorem degree_pow_le (p : R[X]) : ∀ n : ℕ, degree (p ^ n) ≤ n • degree p | 0 => by rw [pow_zero, zero_nsmul]; exact degree_one_le | n + 1 => calc degree (p ^ (n + 1)) ≤ degree (p ^ n) + degree p := by rw [pow_succ]; exact degree_mul_le _ _ _ ≤ _ := by rw [succ_nsmul]; exact add_le_add_right (degree_pow_le _ _) _ theorem degree_pow_le_of_le {a : WithBot ℕ} (b : ℕ) (hp : degree p ≤ a) : degree (p ^ b) ≤ b * a := by induction b with | zero => simp [degree_one_le] | succ n hn => rw [Nat.cast_succ, add_mul, one_mul, pow_succ] exact degree_mul_le_of_le hn hp @[simp] theorem leadingCoeff_monomial (a : R) (n : ℕ) : leadingCoeff (monomial n a) = a := by classical by_cases ha : a = 0 · simp only [ha, (monomial n).map_zero, leadingCoeff_zero] · rw [leadingCoeff, natDegree_monomial, if_neg ha, coeff_monomial] simp theorem leadingCoeff_C_mul_X_pow (a : R) (n : ℕ) : leadingCoeff (C a * X ^ n) = a := by rw [C_mul_X_pow_eq_monomial, leadingCoeff_monomial] theorem leadingCoeff_C_mul_X (a : R) : leadingCoeff (C a * X) = a := by simpa only [pow_one] using leadingCoeff_C_mul_X_pow a 1 @[simp] theorem leadingCoeff_C (a : R) : leadingCoeff (C a) = a := leadingCoeff_monomial a 0 theorem leadingCoeff_X_pow (n : ℕ) : leadingCoeff ((X : R[X]) ^ n) = 1 := by simpa only [C_1, one_mul] using leadingCoeff_C_mul_X_pow (1 : R) n theorem leadingCoeff_X : leadingCoeff (X : R[X]) = 1 := by simpa only [pow_one] using @leadingCoeff_X_pow R _ 1 @[simp] theorem monic_X_pow (n : ℕ) : Monic (X ^ n : R[X]) := leadingCoeff_X_pow n @[simp] theorem monic_X : Monic (X : R[X]) := leadingCoeff_X theorem leadingCoeff_one : leadingCoeff (1 : R[X]) = 1 := leadingCoeff_C 1 @[simp] theorem monic_one : Monic (1 : R[X]) := leadingCoeff_C _ theorem Monic.ne_zero {R : Type*} [Semiring R] [Nontrivial R] {p : R[X]} (hp : p.Monic) : p ≠ 0 := by rintro rfl simp [Monic] at hp theorem Monic.ne_zero_of_ne (h : (0 : R) ≠ 1) {p : R[X]} (hp : p.Monic) : p ≠ 0 := by nontriviality R exact hp.ne_zero theorem Monic.ne_zero_of_polynomial_ne {r} (hp : Monic p) (hne : q ≠ r) : p ≠ 0 := haveI := Nontrivial.of_polynomial_ne hne hp.ne_zero theorem natDegree_mul_le {p q : R[X]} : natDegree (p * q) ≤ natDegree p + natDegree q := by apply natDegree_le_of_degree_le apply le_trans (degree_mul_le p q) rw [Nat.cast_add] apply add_le_add <;> apply degree_le_natDegree theorem natDegree_mul_le_of_le (hp : natDegree p ≤ m) (hg : natDegree q ≤ n) : natDegree (p * q) ≤ m + n := natDegree_mul_le.trans <| add_le_add ‹_› ‹_› theorem natDegree_pow_le {p : R[X]} {n : ℕ} : (p ^ n).natDegree ≤ n * p.natDegree := by induction n with | zero => simp | succ i hi => rw [pow_succ, Nat.succ_mul] apply le_trans natDegree_mul_le (add_le_add_right hi _) theorem natDegree_pow_le_of_le (n : ℕ) (hp : natDegree p ≤ m) : natDegree (p ^ n) ≤ n * m := natDegree_pow_le.trans (Nat.mul_le_mul le_rfl ‹_›) theorem natDegree_eq_zero_iff_degree_le_zero : p.natDegree = 0 ↔ p.degree ≤ 0 := by rw [← nonpos_iff_eq_zero, natDegree_le_iff_degree_le, Nat.cast_zero] theorem degree_zero_le : degree (0 : R[X]) ≤ 0 := natDegree_eq_zero_iff_degree_le_zero.mp rfl theorem degree_le_iff_coeff_zero (f : R[X]) (n : WithBot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := by simp only [degree, Finset.max, Finset.sup_le_iff, mem_support_iff, Ne, ← not_le, not_imp_comm, Nat.cast_withBot] theorem degree_lt_iff_coeff_zero (f : R[X]) (n : ℕ) : degree f < n ↔ ∀ m : ℕ, n ≤ m → coeff f m = 0 := by simp only [degree, Finset.sup_lt_iff (WithBot.bot_lt_coe n), mem_support_iff, WithBot.coe_lt_coe, ← @not_le ℕ, max_eq_sup_coe, Nat.cast_withBot, Ne, not_imp_not] theorem natDegree_pos_iff_degree_pos : 0 < natDegree p ↔ 0 < degree p := lt_iff_lt_of_le_iff_le natDegree_le_iff_degree_le end Semiring section NontrivialSemiring variable [Semiring R] [Nontrivial R] {p q : R[X]} (n : ℕ) @[simp] theorem degree_X_pow : degree ((X : R[X]) ^ n) = n := by rw [X_pow_eq_monomial, degree_monomial _ (one_ne_zero' R)] @[simp] theorem natDegree_X_pow : natDegree ((X : R[X]) ^ n) = n := natDegree_eq_of_degree_eq_some (degree_X_pow n) end NontrivialSemiring section Ring variable [Ring R] {p q : R[X]} theorem degree_sub_le (p q : R[X]) : degree (p - q) ≤ max (degree p) (degree q) := by simpa only [degree_neg q] using degree_add_le p (-q) theorem degree_sub_le_of_le {a b : WithBot ℕ} (hp : degree p ≤ a) (hq : degree q ≤ b) : degree (p - q) ≤ max a b := (p.degree_sub_le q).trans <| max_le_max ‹_› ‹_› theorem natDegree_sub_le (p q : R[X]) : natDegree (p - q) ≤ max (natDegree p) (natDegree q) := by simpa only [← natDegree_neg q] using natDegree_add_le p (-q) theorem natDegree_sub_le_of_le (hp : natDegree p ≤ m) (hq : natDegree q ≤ n) : natDegree (p - q) ≤ max m n := (p.natDegree_sub_le q).trans <| max_le_max ‹_› ‹_› theorem degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leadingCoeff p = leadingCoeff q) : degree (p - q) < degree p := have hp : monomial (natDegree p) (leadingCoeff p) + p.erase (natDegree p) = p := monomial_add_erase _ _ have hq : monomial (natDegree q) (leadingCoeff q) + q.erase (natDegree q) = q := monomial_add_erase _ _ have hd' : natDegree p = natDegree q := by unfold natDegree; rw [hd] have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0) calc degree (p - q) = degree (erase (natDegree q) p + -erase (natDegree q) q) := by conv => lhs rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg] _ ≤ max (degree (erase (natDegree q) p)) (degree (erase (natDegree q) q)) := (degree_neg (erase (natDegree q) q) ▸ degree_add_le _ _) _ < degree p := max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ theorem degree_X_sub_C_le (r : R) : (X - C r).degree ≤ 1 := (degree_sub_le _ _).trans (max_le degree_X_le (degree_C_le.trans zero_le_one)) theorem natDegree_X_sub_C_le (r : R) : (X - C r).natDegree ≤ 1 := natDegree_le_iff_degree_le.2 <| degree_X_sub_C_le r end Ring end Polynomial
Mathlib/Algebra/Polynomial/Degree/Definitions.lean
812
818
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl, Yaël Dillies -/ import Mathlib.Analysis.Normed.Group.Seminorm import Mathlib.Data.NNReal.Basic import Mathlib.Topology.Algebra.Support import Mathlib.Topology.MetricSpace.Basic import Mathlib.Topology.Order.Real /-! # Normed (semi)groups In this file we define 10 classes: * `Norm`, `NNNorm`: auxiliary classes endowing a type `α` with a function `norm : α → ℝ` (notation: `‖x‖`) and `nnnorm : α → ℝ≥0` (notation: `‖x‖₊`), respectively; * `Seminormed...Group`: A seminormed (additive) (commutative) group is an (additive) (commutative) group with a norm and a compatible pseudometric space structure: `∀ x y, dist x y = ‖x / y‖` or `∀ x y, dist x y = ‖x - y‖`, depending on the group operation. * `Normed...Group`: A normed (additive) (commutative) group is an (additive) (commutative) group with a norm and a compatible metric space structure. We also prove basic properties of (semi)normed groups and provide some instances. ## Notes The current convention `dist x y = ‖x - y‖` means that the distance is invariant under right addition, but actions in mathlib are usually from the left. This means we might want to change it to `dist x y = ‖-x + y‖`. The normed group hierarchy would lend itself well to a mixin design (that is, having `SeminormedGroup` and `SeminormedAddGroup` not extend `Group` and `AddGroup`), but we choose not to for performance concerns. ## Tags normed group -/ variable {𝓕 α ι κ E F G : Type*} open Filter Function Metric Bornology open ENNReal Filter NNReal Uniformity Pointwise Topology /-- Auxiliary class, endowing a type `E` with a function `norm : E → ℝ` with notation `‖x‖`. This class is designed to be extended in more interesting classes specifying the properties of the norm. -/ @[notation_class] class Norm (E : Type*) where /-- the `ℝ`-valued norm function. -/ norm : E → ℝ /-- Auxiliary class, endowing a type `α` with a function `nnnorm : α → ℝ≥0` with notation `‖x‖₊`. -/ @[notation_class] class NNNorm (E : Type*) where /-- the `ℝ≥0`-valued norm function. -/ nnnorm : E → ℝ≥0 /-- Auxiliary class, endowing a type `α` with a function `enorm : α → ℝ≥0∞` with notation `‖x‖ₑ`. -/ @[notation_class] class ENorm (E : Type*) where /-- the `ℝ≥0∞`-valued norm function. -/ enorm : E → ℝ≥0∞ export Norm (norm) export NNNorm (nnnorm) export ENorm (enorm) @[inherit_doc] notation "‖" e "‖" => norm e @[inherit_doc] notation "‖" e "‖₊" => nnnorm e @[inherit_doc] notation "‖" e "‖ₑ" => enorm e section ENorm variable {E : Type*} [NNNorm E] {x : E} {r : ℝ≥0} instance NNNorm.toENorm : ENorm E where enorm := (‖·‖₊ : E → ℝ≥0∞) lemma enorm_eq_nnnorm (x : E) : ‖x‖ₑ = ‖x‖₊ := rfl @[simp] lemma toNNReal_enorm (x : E) : ‖x‖ₑ.toNNReal = ‖x‖₊ := rfl @[simp, norm_cast] lemma coe_le_enorm : r ≤ ‖x‖ₑ ↔ r ≤ ‖x‖₊ := by simp [enorm] @[simp, norm_cast] lemma enorm_le_coe : ‖x‖ₑ ≤ r ↔ ‖x‖₊ ≤ r := by simp [enorm] @[simp, norm_cast] lemma coe_lt_enorm : r < ‖x‖ₑ ↔ r < ‖x‖₊ := by simp [enorm] @[simp, norm_cast] lemma enorm_lt_coe : ‖x‖ₑ < r ↔ ‖x‖₊ < r := by simp [enorm] @[simp] lemma enorm_ne_top : ‖x‖ₑ ≠ ∞ := by simp [enorm] @[simp] lemma enorm_lt_top : ‖x‖ₑ < ∞ := by simp [enorm] end ENorm /-- A type `E` equipped with a continuous map `‖·‖ₑ : E → ℝ≥0∞` NB. We do not demand that the topology is somehow defined by the enorm: for ℝ≥0∞ (the motivating example behind this definition), this is not true. -/ class ContinuousENorm (E : Type*) [TopologicalSpace E] extends ENorm E where continuous_enorm : Continuous enorm /-- An enormed monoid is an additive monoid endowed with a continuous enorm. -/ class ENormedAddMonoid (E : Type*) [TopologicalSpace E] extends ContinuousENorm E, AddMonoid E where enorm_eq_zero : ∀ x : E, ‖x‖ₑ = 0 ↔ x = 0 protected enorm_add_le : ∀ x y : E, ‖x + y‖ₑ ≤ ‖x‖ₑ + ‖y‖ₑ /-- An enormed monoid is a monoid endowed with a continuous enorm. -/ @[to_additive] class ENormedMonoid (E : Type*) [TopologicalSpace E] extends ContinuousENorm E, Monoid E where enorm_eq_zero : ∀ x : E, ‖x‖ₑ = 0 ↔ x = 1 enorm_mul_le : ∀ x y : E, ‖x * y‖ₑ ≤ ‖x‖ₑ + ‖y‖ₑ /-- An enormed commutative monoid is an additive commutative monoid endowed with a continuous enorm. We don't have `ENormedAddCommMonoid` extend `EMetricSpace`, since the canonical instance `ℝ≥0∞` is not an `EMetricSpace`. This is because `ℝ≥0∞` carries the order topology, which is distinct from the topology coming from `edist`. -/ class ENormedAddCommMonoid (E : Type*) [TopologicalSpace E] extends ENormedAddMonoid E, AddCommMonoid E where /-- An enormed commutative monoid is a commutative monoid endowed with a continuous enorm. -/ @[to_additive] class ENormedCommMonoid (E : Type*) [TopologicalSpace E] extends ENormedMonoid E, CommMonoid E where /-- A seminormed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a pseudometric space structure. -/ class SeminormedAddGroup (E : Type*) extends Norm E, AddGroup E, PseudoMetricSpace E where dist := fun x y => ‖x - y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x - y‖ := by aesop /-- A seminormed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a pseudometric space structure. -/ @[to_additive] class SeminormedGroup (E : Type*) extends Norm E, Group E, PseudoMetricSpace E where dist := fun x y => ‖x / y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x / y‖ := by aesop /-- A normed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a metric space structure. -/ class NormedAddGroup (E : Type*) extends Norm E, AddGroup E, MetricSpace E where dist := fun x y => ‖x - y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x - y‖ := by aesop /-- A normed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a metric space structure. -/ @[to_additive] class NormedGroup (E : Type*) extends Norm E, Group E, MetricSpace E where dist := fun x y => ‖x / y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x / y‖ := by aesop /-- A seminormed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a pseudometric space structure. -/ class SeminormedAddCommGroup (E : Type*) extends Norm E, AddCommGroup E, PseudoMetricSpace E where dist := fun x y => ‖x - y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x - y‖ := by aesop /-- A seminormed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a pseudometric space structure. -/ @[to_additive] class SeminormedCommGroup (E : Type*) extends Norm E, CommGroup E, PseudoMetricSpace E where dist := fun x y => ‖x / y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x / y‖ := by aesop /-- A normed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a metric space structure. -/ class NormedAddCommGroup (E : Type*) extends Norm E, AddCommGroup E, MetricSpace E where dist := fun x y => ‖x - y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x - y‖ := by aesop /-- A normed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a metric space structure. -/ @[to_additive] class NormedCommGroup (E : Type*) extends Norm E, CommGroup E, MetricSpace E where dist := fun x y => ‖x / y‖ /-- The distance function is induced by the norm. -/ dist_eq : ∀ x y, dist x y = ‖x / y‖ := by aesop -- See note [lower instance priority] @[to_additive] instance (priority := 100) NormedGroup.toSeminormedGroup [NormedGroup E] : SeminormedGroup E := { ‹NormedGroup E› with } -- See note [lower instance priority] @[to_additive] instance (priority := 100) NormedCommGroup.toSeminormedCommGroup [NormedCommGroup E] : SeminormedCommGroup E := { ‹NormedCommGroup E› with } -- See note [lower instance priority] @[to_additive] instance (priority := 100) SeminormedCommGroup.toSeminormedGroup [SeminormedCommGroup E] : SeminormedGroup E := { ‹SeminormedCommGroup E› with } -- See note [lower instance priority] @[to_additive] instance (priority := 100) NormedCommGroup.toNormedGroup [NormedCommGroup E] : NormedGroup E := { ‹NormedCommGroup E› with } -- See note [reducible non-instances] /-- Construct a `NormedGroup` from a `SeminormedGroup` satisfying `∀ x, ‖x‖ = 0 → x = 1`. This avoids having to go back to the `(Pseudo)MetricSpace` level when declaring a `NormedGroup` instance as a special case of a more general `SeminormedGroup` instance. -/ @[to_additive "Construct a `NormedAddGroup` from a `SeminormedAddGroup` satisfying `∀ x, ‖x‖ = 0 → x = 0`. This avoids having to go back to the `(Pseudo)MetricSpace` level when declaring a `NormedAddGroup` instance as a special case of a more general `SeminormedAddGroup` instance."] abbrev NormedGroup.ofSeparation [SeminormedGroup E] (h : ∀ x : E, ‖x‖ = 0 → x = 1) : NormedGroup E where dist_eq := ‹SeminormedGroup E›.dist_eq toMetricSpace := { eq_of_dist_eq_zero := fun hxy => div_eq_one.1 <| h _ <| (‹SeminormedGroup E›.dist_eq _ _).symm.trans hxy } -- See note [reducible non-instances] /-- Construct a `NormedCommGroup` from a `SeminormedCommGroup` satisfying `∀ x, ‖x‖ = 0 → x = 1`. This avoids having to go back to the `(Pseudo)MetricSpace` level when declaring a `NormedCommGroup` instance as a special case of a more general `SeminormedCommGroup` instance. -/ @[to_additive "Construct a `NormedAddCommGroup` from a `SeminormedAddCommGroup` satisfying `∀ x, ‖x‖ = 0 → x = 0`. This avoids having to go back to the `(Pseudo)MetricSpace` level when declaring a `NormedAddCommGroup` instance as a special case of a more general `SeminormedAddCommGroup` instance."] abbrev NormedCommGroup.ofSeparation [SeminormedCommGroup E] (h : ∀ x : E, ‖x‖ = 0 → x = 1) : NormedCommGroup E := { ‹SeminormedCommGroup E›, NormedGroup.ofSeparation h with } -- See note [reducible non-instances] /-- Construct a seminormed group from a multiplication-invariant distance. -/ @[to_additive "Construct a seminormed group from a translation-invariant distance."] abbrev SeminormedGroup.ofMulDist [Norm E] [Group E] [PseudoMetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : SeminormedGroup E where dist_eq x y := by rw [h₁]; apply le_antisymm · simpa only [div_eq_mul_inv, ← mul_inv_cancel y] using h₂ _ _ _ · simpa only [div_mul_cancel, one_mul] using h₂ (x / y) 1 y -- See note [reducible non-instances] /-- Construct a seminormed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a seminormed group from a translation-invariant pseudodistance."] abbrev SeminormedGroup.ofMulDist' [Norm E] [Group E] [PseudoMetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : SeminormedGroup E where dist_eq x y := by rw [h₁]; apply le_antisymm · simpa only [div_mul_cancel, one_mul] using h₂ (x / y) 1 y · simpa only [div_eq_mul_inv, ← mul_inv_cancel y] using h₂ _ _ _ -- See note [reducible non-instances] /-- Construct a seminormed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a seminormed group from a translation-invariant pseudodistance."] abbrev SeminormedCommGroup.ofMulDist [Norm E] [CommGroup E] [PseudoMetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : SeminormedCommGroup E := { SeminormedGroup.ofMulDist h₁ h₂ with mul_comm := mul_comm } -- See note [reducible non-instances] /-- Construct a seminormed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a seminormed group from a translation-invariant pseudodistance."] abbrev SeminormedCommGroup.ofMulDist' [Norm E] [CommGroup E] [PseudoMetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : SeminormedCommGroup E := { SeminormedGroup.ofMulDist' h₁ h₂ with mul_comm := mul_comm } -- See note [reducible non-instances] /-- Construct a normed group from a multiplication-invariant distance. -/ @[to_additive "Construct a normed group from a translation-invariant distance."] abbrev NormedGroup.ofMulDist [Norm E] [Group E] [MetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : NormedGroup E := { SeminormedGroup.ofMulDist h₁ h₂ with eq_of_dist_eq_zero := eq_of_dist_eq_zero } -- See note [reducible non-instances] /-- Construct a normed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a normed group from a translation-invariant pseudodistance."] abbrev NormedGroup.ofMulDist' [Norm E] [Group E] [MetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : NormedGroup E := { SeminormedGroup.ofMulDist' h₁ h₂ with eq_of_dist_eq_zero := eq_of_dist_eq_zero } -- See note [reducible non-instances] /-- Construct a normed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a normed group from a translation-invariant pseudodistance."] abbrev NormedCommGroup.ofMulDist [Norm E] [CommGroup E] [MetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : NormedCommGroup E := { NormedGroup.ofMulDist h₁ h₂ with mul_comm := mul_comm } -- See note [reducible non-instances] /-- Construct a normed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a normed group from a translation-invariant pseudodistance."] abbrev NormedCommGroup.ofMulDist' [Norm E] [CommGroup E] [MetricSpace E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : NormedCommGroup E := { NormedGroup.ofMulDist' h₁ h₂ with mul_comm := mul_comm } -- See note [reducible non-instances] /-- Construct a seminormed group from a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`). -/ @[to_additive "Construct a seminormed group from a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`)."] abbrev GroupSeminorm.toSeminormedGroup [Group E] (f : GroupSeminorm E) : SeminormedGroup E where dist x y := f (x / y) norm := f dist_eq _ _ := rfl dist_self x := by simp only [div_self', map_one_eq_zero] dist_triangle := le_map_div_add_map_div f dist_comm := map_div_rev f -- See note [reducible non-instances] /-- Construct a seminormed group from a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`). -/ @[to_additive "Construct a seminormed group from a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`)."] abbrev GroupSeminorm.toSeminormedCommGroup [CommGroup E] (f : GroupSeminorm E) : SeminormedCommGroup E := { f.toSeminormedGroup with mul_comm := mul_comm } -- See note [reducible non-instances] /-- Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`). -/ @[to_additive "Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`)."] abbrev GroupNorm.toNormedGroup [Group E] (f : GroupNorm E) : NormedGroup E := { f.toGroupSeminorm.toSeminormedGroup with eq_of_dist_eq_zero := fun h => div_eq_one.1 <| eq_one_of_map_eq_zero f h } -- See note [reducible non-instances] /-- Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`). -/ @[to_additive "Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `UniformSpace` instance on `E`)."] abbrev GroupNorm.toNormedCommGroup [CommGroup E] (f : GroupNorm E) : NormedCommGroup E := { f.toNormedGroup with mul_comm := mul_comm } section SeminormedGroup variable [SeminormedGroup E] [SeminormedGroup F] [SeminormedGroup G] {s : Set E} {a a₁ a₂ b c : E} {r r₁ r₂ : ℝ} @[to_additive] theorem dist_eq_norm_div (a b : E) : dist a b = ‖a / b‖ := SeminormedGroup.dist_eq _ _ @[to_additive] theorem dist_eq_norm_div' (a b : E) : dist a b = ‖b / a‖ := by rw [dist_comm, dist_eq_norm_div] alias dist_eq_norm := dist_eq_norm_sub alias dist_eq_norm' := dist_eq_norm_sub' @[to_additive of_forall_le_norm] lemma DiscreteTopology.of_forall_le_norm' (hpos : 0 < r) (hr : ∀ x : E, x ≠ 1 → r ≤ ‖x‖) : DiscreteTopology E := .of_forall_le_dist hpos fun x y hne ↦ by simp only [dist_eq_norm_div] exact hr _ (div_ne_one.2 hne) @[to_additive (attr := simp)] theorem dist_one_right (a : E) : dist a 1 = ‖a‖ := by rw [dist_eq_norm_div, div_one] @[to_additive] theorem inseparable_one_iff_norm {a : E} : Inseparable a 1 ↔ ‖a‖ = 0 := by rw [Metric.inseparable_iff, dist_one_right] @[to_additive] lemma dist_one_left (a : E) : dist 1 a = ‖a‖ := by rw [dist_comm, dist_one_right] @[to_additive (attr := simp)] lemma dist_one : dist (1 : E) = norm := funext dist_one_left @[to_additive] theorem norm_div_rev (a b : E) : ‖a / b‖ = ‖b / a‖ := by simpa only [dist_eq_norm_div] using dist_comm a b @[to_additive (attr := simp) norm_neg] theorem norm_inv' (a : E) : ‖a⁻¹‖ = ‖a‖ := by simpa using norm_div_rev 1 a @[to_additive (attr := simp) norm_abs_zsmul] theorem norm_zpow_abs (a : E) (n : ℤ) : ‖a ^ |n|‖ = ‖a ^ n‖ := by rcases le_total 0 n with hn | hn <;> simp [hn, abs_of_nonneg, abs_of_nonpos] @[to_additive (attr := simp) norm_natAbs_smul] theorem norm_pow_natAbs (a : E) (n : ℤ) : ‖a ^ n.natAbs‖ = ‖a ^ n‖ := by rw [← zpow_natCast, ← Int.abs_eq_natAbs, norm_zpow_abs] @[to_additive norm_isUnit_zsmul] theorem norm_zpow_isUnit (a : E) {n : ℤ} (hn : IsUnit n) : ‖a ^ n‖ = ‖a‖ := by rw [← norm_pow_natAbs, Int.isUnit_iff_natAbs_eq.mp hn, pow_one] @[simp] theorem norm_units_zsmul {E : Type*} [SeminormedAddGroup E] (n : ℤˣ) (a : E) : ‖n • a‖ = ‖a‖ := norm_isUnit_zsmul a n.isUnit open scoped symmDiff in @[to_additive] theorem dist_mulIndicator (s t : Set α) (f : α → E) (x : α) : dist (s.mulIndicator f x) (t.mulIndicator f x) = ‖(s ∆ t).mulIndicator f x‖ := by rw [dist_eq_norm_div, Set.apply_mulIndicator_symmDiff norm_inv'] /-- **Triangle inequality** for the norm. -/ @[to_additive norm_add_le "**Triangle inequality** for the norm."] theorem norm_mul_le' (a b : E) : ‖a * b‖ ≤ ‖a‖ + ‖b‖ := by simpa [dist_eq_norm_div] using dist_triangle a 1 b⁻¹ /-- **Triangle inequality** for the norm. -/ @[to_additive norm_add_le_of_le "**Triangle inequality** for the norm."] theorem norm_mul_le_of_le' (h₁ : ‖a₁‖ ≤ r₁) (h₂ : ‖a₂‖ ≤ r₂) : ‖a₁ * a₂‖ ≤ r₁ + r₂ := (norm_mul_le' a₁ a₂).trans <| add_le_add h₁ h₂ /-- **Triangle inequality** for the norm. -/ @[to_additive norm_add₃_le "**Triangle inequality** for the norm."] lemma norm_mul₃_le' : ‖a * b * c‖ ≤ ‖a‖ + ‖b‖ + ‖c‖ := norm_mul_le_of_le' (norm_mul_le' _ _) le_rfl @[to_additive] lemma norm_div_le_norm_div_add_norm_div (a b c : E) : ‖a / c‖ ≤ ‖a / b‖ + ‖b / c‖ := by simpa only [dist_eq_norm_div] using dist_triangle a b c @[to_additive (attr := simp) norm_nonneg] theorem norm_nonneg' (a : E) : 0 ≤ ‖a‖ := by rw [← dist_one_right] exact dist_nonneg attribute [bound] norm_nonneg @[to_additive (attr := simp) abs_norm] theorem abs_norm' (z : E) : |‖z‖| = ‖z‖ := abs_of_nonneg <| norm_nonneg' _ @[to_additive (attr := simp) norm_zero] theorem norm_one' : ‖(1 : E)‖ = 0 := by rw [← dist_one_right, dist_self] @[to_additive] theorem ne_one_of_norm_ne_zero : ‖a‖ ≠ 0 → a ≠ 1 := mt <| by rintro rfl exact norm_one' @[to_additive (attr := nontriviality) norm_of_subsingleton] theorem norm_of_subsingleton' [Subsingleton E] (a : E) : ‖a‖ = 0 := by rw [Subsingleton.elim a 1, norm_one'] @[to_additive zero_lt_one_add_norm_sq] theorem zero_lt_one_add_norm_sq' (x : E) : 0 < 1 + ‖x‖ ^ 2 := by positivity @[to_additive] theorem norm_div_le (a b : E) : ‖a / b‖ ≤ ‖a‖ + ‖b‖ := by simpa [dist_eq_norm_div] using dist_triangle a 1 b attribute [bound] norm_sub_le @[to_additive] theorem norm_div_le_of_le {r₁ r₂ : ℝ} (H₁ : ‖a₁‖ ≤ r₁) (H₂ : ‖a₂‖ ≤ r₂) : ‖a₁ / a₂‖ ≤ r₁ + r₂ := (norm_div_le a₁ a₂).trans <| add_le_add H₁ H₂ @[to_additive dist_le_norm_add_norm] theorem dist_le_norm_add_norm' (a b : E) : dist a b ≤ ‖a‖ + ‖b‖ := by rw [dist_eq_norm_div] apply norm_div_le @[to_additive abs_norm_sub_norm_le] theorem abs_norm_sub_norm_le' (a b : E) : |‖a‖ - ‖b‖| ≤ ‖a / b‖ := by simpa [dist_eq_norm_div] using abs_dist_sub_le a b 1 @[to_additive norm_sub_norm_le] theorem norm_sub_norm_le' (a b : E) : ‖a‖ - ‖b‖ ≤ ‖a / b‖ := (le_abs_self _).trans (abs_norm_sub_norm_le' a b) @[to_additive (attr := bound)] theorem norm_sub_le_norm_mul (a b : E) : ‖a‖ - ‖b‖ ≤ ‖a * b‖ := by simpa using norm_mul_le' (a * b) (b⁻¹) @[to_additive dist_norm_norm_le] theorem dist_norm_norm_le' (a b : E) : dist ‖a‖ ‖b‖ ≤ ‖a / b‖ := abs_norm_sub_norm_le' a b @[to_additive] theorem norm_le_norm_add_norm_div' (u v : E) : ‖u‖ ≤ ‖v‖ + ‖u / v‖ := by rw [add_comm] refine (norm_mul_le' _ _).trans_eq' ?_ rw [div_mul_cancel]
@[to_additive] theorem norm_le_norm_add_norm_div (u v : E) : ‖v‖ ≤ ‖u‖ + ‖u / v‖ := by
Mathlib/Analysis/Normed/Group/Basic.lean
527
528
/- 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]
Mathlib/LinearAlgebra/Matrix/SpecialLinearGroup.lean
470
474
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Data.Set.Piecewise import Mathlib.Logic.Equiv.Defs import Mathlib.Tactic.Core import Mathlib.Tactic.Attr.Core /-! # Partial equivalences This files defines equivalences between subsets of given types. An element `e` of `PartialEquiv α β` is made of two maps `e.toFun` and `e.invFun` respectively from α to β and from β to α (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively α and β. They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two partial equivalences by restricting the source and target to the maximal set where the composition makes sense. As for equivs, we register a coercion to functions and use it in our simp normal form: we write `e x` and `e.symm y` instead of `e.toFun x` and `e.invFun y`. ## Main definitions * `Equiv.toPartialEquiv`: associating a partial equiv to an equiv, with source = target = univ * `PartialEquiv.symm`: the inverse of a partial equivalence * `PartialEquiv.trans`: the composition of two partial equivalences * `PartialEquiv.refl`: the identity partial equivalence * `PartialEquiv.ofSet`: the identity on a set `s` * `EqOnSource`: equivalence relation describing the "right" notion of equality for partial equivalences (see below in implementation notes) ## Implementation notes There are at least three possible implementations of partial equivalences: * equivs on subtypes * pairs of functions taking values in `Option α` and `Option β`, equal to none where the partial equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `PEquiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The `PartialEquiv` version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `toFun` and `invFun` outside of `source` and `target`). In particular, the equality notion between partial equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no partial equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal partial equivs, this is not an issue in practice. Still, we introduce an equivalence relation `EqOnSource` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. ### Local coding conventions If a lemma deals with the intersection of a set with either source or target of a `PartialEquiv`, then it should use `e.source ∩ s` or `e.target ∩ t`, not `s ∩ e.source` or `t ∩ e.target`. -/ open Lean Meta Elab Tactic /-! Implementation of the `mfld_set_tac` tactic for working with the domains of partially-defined functions (`PartialEquiv`, `PartialHomeomorph`, etc). This is in a separate file from `Mathlib.Logic.Equiv.MfldSimpsAttr` because attributes need a new file to become functional. -/ /-- Common `@[simps]` configuration options used for manifold-related declarations. -/ def mfld_cfg : Simps.Config where attrs := [`mfld_simps] fullyApplied := false namespace Tactic.MfldSetTac /-- A very basic tactic to show that sets showing up in manifolds coincide or are included in one another. -/ elab (name := mfldSetTac) "mfld_set_tac" : tactic => withMainContext do let g ← getMainGoal let goalTy := (← instantiateMVars (← g.getDecl).type).getAppFnArgs match goalTy with | (``Eq, #[_ty, _e₁, _e₂]) => evalTactic (← `(tactic| ( apply Set.ext; intro my_y constructor <;> · intro h_my_y try simp only [*, mfld_simps] at h_my_y try simp only [*, mfld_simps]))) | (``Subset, #[_ty, _inst, _e₁, _e₂]) => evalTactic (← `(tactic| ( intro my_y h_my_y try simp only [*, mfld_simps] at h_my_y try simp only [*, mfld_simps]))) | _ => throwError "goal should be an equality or an inclusion" attribute [mfld_simps] and_true eq_self_iff_true Function.comp_apply end Tactic.MfldSetTac open Function Set variable {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Local equivalence between subsets `source` and `target` of `α` and `β` respectively. The (global) maps `toFun : α → β` and `invFun : β → α` map `source` to `target` and conversely, and are inverse to each other there. The values of `toFun` outside of `source` and of `invFun` outside of `target` are irrelevant. -/ structure PartialEquiv (α : Type*) (β : Type*) where /-- The global function which has a partial inverse. Its value outside of the `source` subset is irrelevant. -/ toFun : α → β /-- The partial inverse to `toFun`. Its value outside of the `target` subset is irrelevant. -/ invFun : β → α /-- The domain of the partial equivalence. -/ source : Set α /-- The codomain of the partial equivalence. -/ target : Set β /-- The proposition that elements of `source` are mapped to elements of `target`. -/ map_source' : ∀ ⦃x⦄, x ∈ source → toFun x ∈ target /-- The proposition that elements of `target` are mapped to elements of `source`. -/ map_target' : ∀ ⦃x⦄, x ∈ target → invFun x ∈ source /-- The proposition that `invFun` is a left-inverse of `toFun` on `source`. -/ left_inv' : ∀ ⦃x⦄, x ∈ source → invFun (toFun x) = x /-- The proposition that `invFun` is a right-inverse of `toFun` on `target`. -/ right_inv' : ∀ ⦃x⦄, x ∈ target → toFun (invFun x) = x attribute [coe] PartialEquiv.toFun namespace PartialEquiv variable (e : PartialEquiv α β) (e' : PartialEquiv β γ) instance [Inhabited α] [Inhabited β] : Inhabited (PartialEquiv α β) := ⟨⟨const α default, const β default, ∅, ∅, mapsTo_empty _ _, mapsTo_empty _ _, eqOn_empty _ _, eqOn_empty _ _⟩⟩ /-- The inverse of a partial equivalence -/ @[symm] protected def symm : PartialEquiv β α where toFun := e.invFun invFun := e.toFun source := e.target target := e.source map_source' := e.map_target' map_target' := e.map_source' left_inv' := e.right_inv' right_inv' := e.left_inv' instance : CoeFun (PartialEquiv α β) fun _ => α → β := ⟨PartialEquiv.toFun⟩ /-- See Note [custom simps projection] -/ def Simps.symm_apply (e : PartialEquiv α β) : β → α := e.symm initialize_simps_projections PartialEquiv (toFun → apply, invFun → symm_apply) theorem coe_mk (f : α → β) (g s t ml mr il ir) : (PartialEquiv.mk f g s t ml mr il ir : α → β) = f := rfl @[simp, mfld_simps] theorem coe_symm_mk (f : α → β) (g s t ml mr il ir) : ((PartialEquiv.mk f g s t ml mr il ir).symm : β → α) = g := rfl @[simp, mfld_simps] theorem invFun_as_coe : e.invFun = e.symm := rfl @[simp, mfld_simps] theorem map_source {x : α} (h : x ∈ e.source) : e x ∈ e.target := e.map_source' h /-- Variant of `e.map_source` and `map_source'`, stated for images of subsets of `source`. -/ lemma map_source'' : e '' e.source ⊆ e.target := fun _ ⟨_, hx, hex⟩ ↦ mem_of_eq_of_mem (id hex.symm) (e.map_source' hx) @[simp, mfld_simps] theorem map_target {x : β} (h : x ∈ e.target) : e.symm x ∈ e.source := e.map_target' h @[simp, mfld_simps] theorem left_inv {x : α} (h : x ∈ e.source) : e.symm (e x) = x := e.left_inv' h @[simp, mfld_simps] theorem right_inv {x : β} (h : x ∈ e.target) : e (e.symm x) = x := e.right_inv' h theorem eq_symm_apply {x : α} {y : β} (hx : x ∈ e.source) (hy : y ∈ e.target) : x = e.symm y ↔ e x = y := ⟨fun h => by rw [← e.right_inv hy, h], fun h => by rw [← e.left_inv hx, h]⟩ protected theorem mapsTo : MapsTo e e.source e.target := fun _ => e.map_source theorem symm_mapsTo : MapsTo e.symm e.target e.source := e.symm.mapsTo protected theorem leftInvOn : LeftInvOn e.symm e e.source := fun _ => e.left_inv protected theorem rightInvOn : RightInvOn e.symm e e.target := fun _ => e.right_inv protected theorem invOn : InvOn e.symm e e.source e.target := ⟨e.leftInvOn, e.rightInvOn⟩ protected theorem injOn : InjOn e e.source := e.leftInvOn.injOn protected theorem bijOn : BijOn e e.source e.target := e.invOn.bijOn e.mapsTo e.symm_mapsTo protected theorem surjOn : SurjOn e e.source e.target := e.bijOn.surjOn /-- Interpret an `Equiv` as a `PartialEquiv` by restricting it to `s` in the domain and to `t` in the codomain. -/ @[simps -fullyApplied] def _root_.Equiv.toPartialEquivOfImageEq (e : α ≃ β) (s : Set α) (t : Set β) (h : e '' s = t) : PartialEquiv α β where toFun := e invFun := e.symm source := s target := t map_source' _ hx := h ▸ mem_image_of_mem _ hx map_target' x hx := by subst t rcases hx with ⟨x, hx, rfl⟩ rwa [e.symm_apply_apply] left_inv' x _ := e.symm_apply_apply x right_inv' x _ := e.apply_symm_apply x /-- Associate a `PartialEquiv` to an `Equiv`. -/ @[simps! (config := mfld_cfg)] def _root_.Equiv.toPartialEquiv (e : α ≃ β) : PartialEquiv α β := e.toPartialEquivOfImageEq univ univ <| by rw [image_univ, e.surjective.range_eq] instance inhabitedOfEmpty [IsEmpty α] [IsEmpty β] : Inhabited (PartialEquiv α β) := ⟨((Equiv.equivEmpty α).trans (Equiv.equivEmpty β).symm).toPartialEquiv⟩ /-- Create a copy of a `PartialEquiv` providing better definitional equalities. -/ @[simps -fullyApplied] def copy (e : PartialEquiv α β) (f : α → β) (hf : ⇑e = f) (g : β → α) (hg : ⇑e.symm = g) (s : Set α) (hs : e.source = s) (t : Set β) (ht : e.target = t) : PartialEquiv α β where toFun := f invFun := g source := s target := t map_source' _ := ht ▸ hs ▸ hf ▸ e.map_source map_target' _ := hs ▸ ht ▸ hg ▸ e.map_target left_inv' _ := hs ▸ hf ▸ hg ▸ e.left_inv right_inv' _ := ht ▸ hf ▸ hg ▸ e.right_inv theorem copy_eq (e : PartialEquiv α β) (f : α → β) (hf : ⇑e = f) (g : β → α) (hg : ⇑e.symm = g) (s : Set α) (hs : e.source = s) (t : Set β) (ht : e.target = t) : e.copy f hf g hg s hs t ht = e := by substs f g s t cases e rfl /-- Associate to a `PartialEquiv` an `Equiv` between the source and the target. -/ protected def toEquiv : e.source ≃ e.target where toFun x := ⟨e x, e.map_source x.mem⟩ invFun y := ⟨e.symm y, e.map_target y.mem⟩ left_inv := fun ⟨_, hx⟩ => Subtype.eq <| e.left_inv hx right_inv := fun ⟨_, hy⟩ => Subtype.eq <| e.right_inv hy @[simp, mfld_simps] theorem symm_source : e.symm.source = e.target := rfl @[simp, mfld_simps] theorem symm_target : e.symm.target = e.source := rfl @[simp, mfld_simps] theorem symm_symm : e.symm.symm = e := rfl theorem symm_bijective : Function.Bijective (PartialEquiv.symm : PartialEquiv α β → PartialEquiv β α) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ theorem image_source_eq_target : e '' e.source = e.target := e.bijOn.image_eq theorem forall_mem_target {p : β → Prop} : (∀ y ∈ e.target, p y) ↔ ∀ x ∈ e.source, p (e x) := by rw [← image_source_eq_target, forall_mem_image] theorem exists_mem_target {p : β → Prop} : (∃ y ∈ e.target, p y) ↔ ∃ x ∈ e.source, p (e x) := by rw [← image_source_eq_target, exists_mem_image] /-- We say that `t : Set β` is an image of `s : Set α` under a partial equivalence if any of the following equivalent conditions hold: * `e '' (e.source ∩ s) = e.target ∩ t`; * `e.source ∩ e ⁻¹ t = e.source ∩ s`; * `∀ x ∈ e.source, e x ∈ t ↔ x ∈ s` (this one is used in the definition). -/ def IsImage (s : Set α) (t : Set β) : Prop := ∀ ⦃x⦄, x ∈ e.source → (e x ∈ t ↔ x ∈ s) namespace IsImage variable {e} {s : Set α} {t : Set β} {x : α} theorem apply_mem_iff (h : e.IsImage s t) (hx : x ∈ e.source) : e x ∈ t ↔ x ∈ s := h hx theorem symm_apply_mem_iff (h : e.IsImage s t) : ∀ ⦃y⦄, y ∈ e.target → (e.symm y ∈ s ↔ y ∈ t) := e.forall_mem_target.mpr fun x hx => by rw [e.left_inv hx, h hx] protected theorem symm (h : e.IsImage s t) : e.symm.IsImage t s := h.symm_apply_mem_iff @[simp] theorem symm_iff : e.symm.IsImage t s ↔ e.IsImage s t := ⟨fun h => h.symm, fun h => h.symm⟩ protected theorem mapsTo (h : e.IsImage s t) : MapsTo e (e.source ∩ s) (e.target ∩ t) := fun _ hx => ⟨e.mapsTo hx.1, (h hx.1).2 hx.2⟩ theorem symm_mapsTo (h : e.IsImage s t) : MapsTo e.symm (e.target ∩ t) (e.source ∩ s) := h.symm.mapsTo /-- Restrict a `PartialEquiv` to a pair of corresponding sets. -/ @[simps -fullyApplied] def restr (h : e.IsImage s t) : PartialEquiv α β where toFun := e invFun := e.symm source := e.source ∩ s target := e.target ∩ t map_source' := h.mapsTo map_target' := h.symm_mapsTo left_inv' := e.leftInvOn.mono inter_subset_left right_inv' := e.rightInvOn.mono inter_subset_left theorem image_eq (h : e.IsImage s t) : e '' (e.source ∩ s) = e.target ∩ t := h.restr.image_source_eq_target theorem symm_image_eq (h : e.IsImage s t) : e.symm '' (e.target ∩ t) = e.source ∩ s := h.symm.image_eq theorem iff_preimage_eq : e.IsImage s t ↔ e.source ∩ e ⁻¹' t = e.source ∩ s := by simp only [IsImage, Set.ext_iff, mem_inter_iff, mem_preimage, and_congr_right_iff] alias ⟨preimage_eq, of_preimage_eq⟩ := iff_preimage_eq theorem iff_symm_preimage_eq : e.IsImage s t ↔ e.target ∩ e.symm ⁻¹' s = e.target ∩ t := symm_iff.symm.trans iff_preimage_eq alias ⟨symm_preimage_eq, of_symm_preimage_eq⟩ := iff_symm_preimage_eq theorem of_image_eq (h : e '' (e.source ∩ s) = e.target ∩ t) : e.IsImage s t := of_symm_preimage_eq <| Eq.trans (of_symm_preimage_eq rfl).image_eq.symm h theorem of_symm_image_eq (h : e.symm '' (e.target ∩ t) = e.source ∩ s) : e.IsImage s t := of_preimage_eq <| Eq.trans (iff_preimage_eq.2 rfl).symm_image_eq.symm h protected theorem compl (h : e.IsImage s t) : e.IsImage sᶜ tᶜ := fun _ hx => not_congr (h hx) protected theorem inter {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s ∩ s') (t ∩ t') := fun _ hx => and_congr (h hx) (h' hx) protected theorem union {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s ∪ s') (t ∪ t') := fun _ hx => or_congr (h hx) (h' hx) protected theorem diff {s' t'} (h : e.IsImage s t) (h' : e.IsImage s' t') : e.IsImage (s \ s') (t \ t') := h.inter h'.compl theorem leftInvOn_piecewise {e' : PartialEquiv α β} [∀ i, Decidable (i ∈ s)] [∀ i, Decidable (i ∈ t)] (h : e.IsImage s t) (h' : e'.IsImage s t) : LeftInvOn (t.piecewise e.symm e'.symm) (s.piecewise e e') (s.ite e.source e'.source) := by rintro x (⟨he, hs⟩ | ⟨he, hs : x ∉ s⟩) · rw [piecewise_eq_of_mem _ _ _ hs, piecewise_eq_of_mem _ _ _ ((h he).2 hs), e.left_inv he] · rw [piecewise_eq_of_not_mem _ _ _ hs, piecewise_eq_of_not_mem _ _ _ ((h'.compl he).2 hs), e'.left_inv he] theorem inter_eq_of_inter_eq_of_eqOn {e' : PartialEquiv α β} (h : e.IsImage s t) (h' : e'.IsImage s t) (hs : e.source ∩ s = e'.source ∩ s) (heq : EqOn e e' (e.source ∩ s)) : e.target ∩ t = e'.target ∩ t := by rw [← h.image_eq, ← h'.image_eq, ← hs, heq.image_eq] theorem symm_eq_on_of_inter_eq_of_eqOn {e' : PartialEquiv α β} (h : e.IsImage s t) (hs : e.source ∩ s = e'.source ∩ s) (heq : EqOn e e' (e.source ∩ s)) : EqOn e.symm e'.symm (e.target ∩ t) := by rw [← h.image_eq] rintro y ⟨x, hx, rfl⟩ have hx' := hx; rw [hs] at hx' rw [e.left_inv hx.1, heq hx, e'.left_inv hx'.1] end IsImage theorem isImage_source_target : e.IsImage e.source e.target := fun x hx => by simp [hx] theorem isImage_source_target_of_disjoint (e' : PartialEquiv α β) (hs : Disjoint e.source e'.source) (ht : Disjoint e.target e'.target) : e.IsImage e'.source e'.target := IsImage.of_image_eq <| by rw [hs.inter_eq, ht.inter_eq, image_empty] theorem image_source_inter_eq' (s : Set α) : e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' s := by rw [inter_comm, e.leftInvOn.image_inter', image_source_eq_target, inter_comm] theorem image_source_inter_eq (s : Set α) : e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' (e.source ∩ s) := by rw [inter_comm, e.leftInvOn.image_inter, image_source_eq_target, inter_comm] theorem image_eq_target_inter_inv_preimage {s : Set α} (h : s ⊆ e.source) : e '' s = e.target ∩ e.symm ⁻¹' s := by rw [← e.image_source_inter_eq', inter_eq_self_of_subset_right h] theorem symm_image_eq_source_inter_preimage {s : Set β} (h : s ⊆ e.target) : e.symm '' s = e.source ∩ e ⁻¹' s := e.symm.image_eq_target_inter_inv_preimage h theorem symm_image_target_inter_eq (s : Set β) : e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' (e.target ∩ s) := e.symm.image_source_inter_eq _ theorem symm_image_target_inter_eq' (s : Set β) : e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' s := e.symm.image_source_inter_eq' _ theorem source_inter_preimage_inv_preimage (s : Set α) : e.source ∩ e ⁻¹' (e.symm ⁻¹' s) = e.source ∩ s := Set.ext fun x => and_congr_right_iff.2 fun hx => by simp only [mem_preimage, e.left_inv hx] theorem source_inter_preimage_target_inter (s : Set β) : e.source ∩ e ⁻¹' (e.target ∩ s) = e.source ∩ e ⁻¹' s := ext fun _ => ⟨fun hx => ⟨hx.1, hx.2.2⟩, fun hx => ⟨hx.1, e.map_source hx.1, hx.2⟩⟩ theorem target_inter_inv_preimage_preimage (s : Set β) : e.target ∩ e.symm ⁻¹' (e ⁻¹' s) = e.target ∩ s := e.symm.source_inter_preimage_inv_preimage _ theorem symm_image_image_of_subset_source {s : Set α} (h : s ⊆ e.source) : e.symm '' (e '' s) = s := (e.leftInvOn.mono h).image_image theorem image_symm_image_of_subset_target {s : Set β} (h : s ⊆ e.target) : e '' (e.symm '' s) = s := e.symm.symm_image_image_of_subset_source h theorem source_subset_preimage_target : e.source ⊆ e ⁻¹' e.target := e.mapsTo theorem symm_image_target_eq_source : e.symm '' e.target = e.source := e.symm.image_source_eq_target theorem target_subset_preimage_source : e.target ⊆ e.symm ⁻¹' e.source := e.symm_mapsTo /-- Two partial equivs that have the same `source`, same `toFun` and same `invFun`, coincide. -/ @[ext] protected theorem ext {e e' : PartialEquiv α β} (h : ∀ x, e x = e' x) (hsymm : ∀ x, e.symm x = e'.symm x) (hs : e.source = e'.source) : e = e' := by have A : (e : α → β) = e' := by ext x exact h x have B : (e.symm : β → α) = e'.symm := by ext x exact hsymm x have I : e '' e.source = e.target := e.image_source_eq_target have I' : e' '' e'.source = e'.target := e'.image_source_eq_target rw [A, hs, I'] at I cases e; cases e' simp_all /-- Restricting a partial equivalence to `e.source ∩ s` -/ protected def restr (s : Set α) : PartialEquiv α β := (@IsImage.of_symm_preimage_eq α β e s (e.symm ⁻¹' s) rfl).restr @[simp, mfld_simps] theorem restr_coe (s : Set α) : (e.restr s : α → β) = e := rfl @[simp, mfld_simps] theorem restr_coe_symm (s : Set α) : ((e.restr s).symm : β → α) = e.symm := rfl @[simp, mfld_simps] theorem restr_source (s : Set α) : (e.restr s).source = e.source ∩ s := rfl theorem source_restr_subset_source (s : Set α) : (e.restr s).source ⊆ e.source := inter_subset_left @[simp, mfld_simps] theorem restr_target (s : Set α) : (e.restr s).target = e.target ∩ e.symm ⁻¹' s := rfl theorem restr_eq_of_source_subset {e : PartialEquiv α β} {s : Set α} (h : e.source ⊆ s) : e.restr s = e := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [inter_eq_self_of_subset_left h]) @[simp, mfld_simps] theorem restr_univ {e : PartialEquiv α β} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) /-- The identity partial equiv -/ protected def refl (α : Type*) : PartialEquiv α α := (Equiv.refl α).toPartialEquiv @[simp, mfld_simps] theorem refl_source : (PartialEquiv.refl α).source = univ := rfl @[simp, mfld_simps] theorem refl_target : (PartialEquiv.refl α).target = univ := rfl @[simp, mfld_simps] theorem refl_coe : (PartialEquiv.refl α : α → α) = id := rfl @[simp, mfld_simps] theorem refl_symm : (PartialEquiv.refl α).symm = PartialEquiv.refl α := rfl @[mfld_simps] theorem refl_restr_source (s : Set α) : ((PartialEquiv.refl α).restr s).source = s := by simp @[mfld_simps] theorem refl_restr_target (s : Set α) : ((PartialEquiv.refl α).restr s).target = s := by simp /-- The identity partial equivalence on a set `s` -/ def ofSet (s : Set α) : PartialEquiv α α where toFun := id invFun := id source := s target := s map_source' _ hx := hx map_target' _ hx := hx left_inv' _ _ := rfl right_inv' _ _ := rfl @[simp, mfld_simps] theorem ofSet_source (s : Set α) : (PartialEquiv.ofSet s).source = s := rfl @[simp, mfld_simps] theorem ofSet_target (s : Set α) : (PartialEquiv.ofSet s).target = s := rfl @[simp, mfld_simps] theorem ofSet_coe (s : Set α) : (PartialEquiv.ofSet s : α → α) = id := rfl @[simp, mfld_simps] theorem ofSet_symm (s : Set α) : (PartialEquiv.ofSet s).symm = PartialEquiv.ofSet s := rfl /-- `Function.const` as a `PartialEquiv`. It consists of two constant maps in opposite directions. -/ @[simps] def single (a : α) (b : β) : PartialEquiv α β where toFun := Function.const α b invFun := Function.const β a source := {a} target := {b} map_source' _ _ := rfl map_target' _ _ := rfl left_inv' a' ha' := by rw [eq_of_mem_singleton ha', const_apply] right_inv' b' hb' := by rw [eq_of_mem_singleton hb', const_apply] /-- Composing two partial equivs if the target of the first coincides with the source of the second. -/ @[simps] protected def trans' (e' : PartialEquiv β γ) (h : e.target = e'.source) : PartialEquiv α γ where toFun := e' ∘ e invFun := e.symm ∘ e'.symm source := e.source target := e'.target map_source' x hx := by simp [← h, hx] map_target' y hy := by simp [h, hy] left_inv' x hx := by simp [hx, ← h] right_inv' y hy := by simp [hy, h] /-- Composing two partial equivs, by restricting to the maximal domain where their composition is well defined. Within the `Manifold` namespace, there is the notation `e ≫ f` for this. -/ @[trans] protected def trans : PartialEquiv α γ := PartialEquiv.trans' (e.symm.restr e'.source).symm (e'.restr e.target) (inter_comm _ _) @[simp, mfld_simps] theorem coe_trans : (e.trans e' : α → γ) = e' ∘ e := rfl @[simp, mfld_simps] theorem coe_trans_symm : ((e.trans e').symm : γ → α) = e.symm ∘ e'.symm := rfl theorem trans_apply {x : α} : (e.trans e') x = e' (e x) := rfl theorem trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; rfl @[simp, mfld_simps] theorem trans_source : (e.trans e').source = e.source ∩ e ⁻¹' e'.source := rfl theorem trans_source' : (e.trans e').source = e.source ∩ e ⁻¹' (e.target ∩ e'.source) := by mfld_set_tac theorem trans_source'' : (e.trans e').source = e.symm '' (e.target ∩ e'.source) := by rw [e.trans_source', e.symm_image_target_inter_eq] theorem image_trans_source : e '' (e.trans e').source = e.target ∩ e'.source := (e.symm.restr e'.source).symm.image_source_eq_target @[simp, mfld_simps] theorem trans_target : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' e.target := rfl theorem trans_target' : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm theorem trans_target'' : (e.trans e').target = e' '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm theorem inv_image_trans_target : e'.symm '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm theorem trans_assoc (e'' : PartialEquiv γ δ) : (e.trans e').trans e'' = e.trans (e'.trans e'') := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source, @preimage_comp α β γ, inter_assoc]) @[simp, mfld_simps] theorem trans_refl : e.trans (PartialEquiv.refl β) = e := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source])
@[simp, mfld_simps] theorem refl_trans : (PartialEquiv.refl α).trans e = e := PartialEquiv.ext (fun _ => rfl) (fun _ => rfl) (by simp [trans_source, preimage_id])
Mathlib/Logic/Equiv/PartialEquiv.lean
639
641
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Violeta Hernández Palacios, Grayson Burton, Floris van Doorn -/ import Mathlib.Order.Antisymmetrization import Mathlib.Order.Hom.WithTopBot import Mathlib.Order.Interval.Set.OrdConnected import Mathlib.Order.Interval.Set.WithBotTop /-! # The covering relation This file proves properties of the covering relation in an order. We say that `b` *covers* `a` if `a < b` and there is no element in between. We say that `b` *weakly covers* `a` if `a ≤ b` and there is no element between `a` and `b`. In a partial order this is equivalent to `a ⋖ b ∨ a = b`, in a preorder this is equivalent to `a ⋖ b ∨ (a ≤ b ∧ b ≤ a)` ## Notation * `a ⋖ b` means that `b` covers `a`. * `a ⩿ b` means that `b` weakly covers `a`. -/ open Set OrderDual variable {α β : Type*} section WeaklyCovers section Preorder variable [Preorder α] [Preorder β] {a b c : α} theorem WCovBy.le (h : a ⩿ b) : a ≤ b := h.1 theorem WCovBy.refl (a : α) : a ⩿ a := ⟨le_rfl, fun _ hc => hc.not_lt⟩ @[simp] lemma WCovBy.rfl : a ⩿ a := WCovBy.refl a protected theorem Eq.wcovBy (h : a = b) : a ⩿ b := h ▸ WCovBy.rfl theorem wcovBy_of_le_of_le (h1 : a ≤ b) (h2 : b ≤ a) : a ⩿ b := ⟨h1, fun _ hac hcb => (hac.trans hcb).not_le h2⟩ alias LE.le.wcovBy_of_le := wcovBy_of_le_of_le theorem AntisymmRel.wcovBy (h : AntisymmRel (· ≤ ·) a b) : a ⩿ b := wcovBy_of_le_of_le h.1 h.2 theorem WCovBy.wcovBy_iff_le (hab : a ⩿ b) : b ⩿ a ↔ b ≤ a := ⟨fun h => h.le, fun h => h.wcovBy_of_le hab.le⟩ theorem wcovBy_of_eq_or_eq (hab : a ≤ b) (h : ∀ c, a ≤ c → c ≤ b → c = a ∨ c = b) : a ⩿ b := ⟨hab, fun c ha hb => (h c ha.le hb.le).elim ha.ne' hb.ne⟩ theorem AntisymmRel.trans_wcovBy (hab : AntisymmRel (· ≤ ·) a b) (hbc : b ⩿ c) : a ⩿ c := ⟨hab.1.trans hbc.le, fun _ had hdc => hbc.2 (hab.2.trans_lt had) hdc⟩ theorem wcovBy_congr_left (hab : AntisymmRel (· ≤ ·) a b) : a ⩿ c ↔ b ⩿ c := ⟨hab.symm.trans_wcovBy, hab.trans_wcovBy⟩ theorem WCovBy.trans_antisymm_rel (hab : a ⩿ b) (hbc : AntisymmRel (· ≤ ·) b c) : a ⩿ c := ⟨hab.le.trans hbc.1, fun _ had hdc => hab.2 had <| hdc.trans_le hbc.2⟩ theorem wcovBy_congr_right (hab : AntisymmRel (· ≤ ·) a b) : c ⩿ a ↔ c ⩿ b := ⟨fun h => h.trans_antisymm_rel hab, fun h => h.trans_antisymm_rel hab.symm⟩ /-- If `a ≤ b`, then `b` does not cover `a` iff there's an element in between. -/ theorem not_wcovBy_iff (h : a ≤ b) : ¬a ⩿ b ↔ ∃ c, a < c ∧ c < b := by simp_rw [WCovBy, h, true_and, not_forall, exists_prop, not_not] instance WCovBy.isRefl : IsRefl α (· ⩿ ·) := ⟨WCovBy.refl⟩ theorem WCovBy.Ioo_eq (h : a ⩿ b) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 fun _ hx => h.2 hx.1 hx.2 theorem wcovBy_iff_Ioo_eq : a ⩿ b ↔ a ≤ b ∧ Ioo a b = ∅ := and_congr_right' <| by simp [eq_empty_iff_forall_not_mem] lemma WCovBy.of_le_of_le (hac : a ⩿ c) (hab : a ≤ b) (hbc : b ≤ c) : b ⩿ c := ⟨hbc, fun _x hbx hxc ↦ hac.2 (hab.trans_lt hbx) hxc⟩ lemma WCovBy.of_le_of_le' (hac : a ⩿ c) (hab : a ≤ b) (hbc : b ≤ c) : a ⩿ b := ⟨hab, fun _x hax hxb ↦ hac.2 hax <| hxb.trans_le hbc⟩ theorem WCovBy.of_image (f : α ↪o β) (h : f a ⩿ f b) : a ⩿ b := ⟨f.le_iff_le.mp h.le, fun _ hac hcb => h.2 (f.lt_iff_lt.mpr hac) (f.lt_iff_lt.mpr hcb)⟩ theorem WCovBy.image (f : α ↪o β) (hab : a ⩿ b) (h : (range f).OrdConnected) : f a ⩿ f b := by refine ⟨f.monotone hab.le, fun c ha hb => ?_⟩ obtain ⟨c, rfl⟩ := h.out (mem_range_self _) (mem_range_self _) ⟨ha.le, hb.le⟩ rw [f.lt_iff_lt] at ha hb exact hab.2 ha hb theorem Set.OrdConnected.apply_wcovBy_apply_iff (f : α ↪o β) (h : (range f).OrdConnected) : f a ⩿ f b ↔ a ⩿ b := ⟨fun h2 => h2.of_image f, fun hab => hab.image f h⟩ @[simp] theorem apply_wcovBy_apply_iff {E : Type*} [EquivLike E α β] [OrderIsoClass E α β] (e : E) : e a ⩿ e b ↔ a ⩿ b := (ordConnected_range (e : α ≃o β)).apply_wcovBy_apply_iff ((e : α ≃o β) : α ↪o β) @[simp] theorem toDual_wcovBy_toDual_iff : toDual b ⩿ toDual a ↔ a ⩿ b := and_congr_right' <| forall_congr' fun _ => forall_swap @[simp] theorem ofDual_wcovBy_ofDual_iff {a b : αᵒᵈ} : ofDual a ⩿ ofDual b ↔ b ⩿ a := and_congr_right' <| forall_congr' fun _ => forall_swap alias ⟨_, WCovBy.toDual⟩ := toDual_wcovBy_toDual_iff alias ⟨_, WCovBy.ofDual⟩ := ofDual_wcovBy_ofDual_iff theorem OrderEmbedding.wcovBy_of_apply {α β : Type*} [Preorder α] [Preorder β] (f : α ↪o β) {x y : α} (h : f x ⩿ f y) : x ⩿ y := by use f.le_iff_le.1 h.1 intro a rw [← f.lt_iff_lt, ← f.lt_iff_lt] apply h.2 theorem OrderIso.map_wcovBy {α β : Type*} [Preorder α] [Preorder β] (f : α ≃o β) {x y : α} : f x ⩿ f y ↔ x ⩿ y := by use f.toOrderEmbedding.wcovBy_of_apply conv_lhs => rw [← f.symm_apply_apply x, ← f.symm_apply_apply y] exact f.symm.toOrderEmbedding.wcovBy_of_apply end Preorder section PartialOrder variable [PartialOrder α] {a b c : α} theorem WCovBy.eq_or_eq (h : a ⩿ b) (h2 : a ≤ c) (h3 : c ≤ b) : c = a ∨ c = b := by rcases h2.eq_or_lt with (h2 | h2); · exact Or.inl h2.symm rcases h3.eq_or_lt with (h3 | h3); · exact Or.inr h3 exact (h.2 h2 h3).elim /-- An `iff` version of `WCovBy.eq_or_eq` and `wcovBy_of_eq_or_eq`. -/ theorem wcovBy_iff_le_and_eq_or_eq : a ⩿ b ↔ a ≤ b ∧ ∀ c, a ≤ c → c ≤ b → c = a ∨ c = b := ⟨fun h => ⟨h.le, fun _ => h.eq_or_eq⟩, And.rec wcovBy_of_eq_or_eq⟩ theorem WCovBy.le_and_le_iff (h : a ⩿ b) : a ≤ c ∧ c ≤ b ↔ c = a ∨ c = b := by refine ⟨fun h2 => h.eq_or_eq h2.1 h2.2, ?_⟩; rintro (rfl | rfl) exacts [⟨le_rfl, h.le⟩, ⟨h.le, le_rfl⟩] theorem WCovBy.Icc_eq (h : a ⩿ b) : Icc a b = {a, b} := by ext c exact h.le_and_le_iff theorem WCovBy.Ico_subset (h : a ⩿ b) : Ico a b ⊆ {a} := by rw [← Icc_diff_right, h.Icc_eq, diff_singleton_subset_iff, pair_comm] theorem WCovBy.Ioc_subset (h : a ⩿ b) : Ioc a b ⊆ {b} := by rw [← Icc_diff_left, h.Icc_eq, diff_singleton_subset_iff] end PartialOrder section SemilatticeSup variable [SemilatticeSup α] {a b c : α} theorem WCovBy.sup_eq (hac : a ⩿ c) (hbc : b ⩿ c) (hab : a ≠ b) : a ⊔ b = c := (sup_le hac.le hbc.le).eq_of_not_lt fun h => hab.lt_sup_or_lt_sup.elim (fun h' => hac.2 h' h) fun h' => hbc.2 h' h end SemilatticeSup section SemilatticeInf variable [SemilatticeInf α] {a b c : α} theorem WCovBy.inf_eq (hca : c ⩿ a) (hcb : c ⩿ b) (hab : a ≠ b) : a ⊓ b = c := (le_inf hca.le hcb.le).eq_of_not_gt fun h => hab.inf_lt_or_inf_lt.elim (hca.2 h) (hcb.2 h) end SemilatticeInf end WeaklyCovers section LT variable [LT α] {a b : α} theorem CovBy.lt (h : a ⋖ b) : a < b := h.1 /-- If `a < b`, then `b` does not cover `a` iff there's an element in between. -/ theorem not_covBy_iff (h : a < b) : ¬a ⋖ b ↔ ∃ c, a < c ∧ c < b := by simp_rw [CovBy, h, true_and, not_forall, exists_prop, not_not] alias ⟨exists_lt_lt_of_not_covBy, _⟩ := not_covBy_iff alias LT.lt.exists_lt_lt := exists_lt_lt_of_not_covBy /-- In a dense order, nothing covers anything. -/ theorem not_covBy [DenselyOrdered α] : ¬a ⋖ b := fun h => let ⟨_, hc⟩ := exists_between h.1 h.2 hc.1 hc.2 theorem denselyOrdered_iff_forall_not_covBy : DenselyOrdered α ↔ ∀ a b : α, ¬a ⋖ b := ⟨fun h _ _ => @not_covBy _ _ _ _ h, fun h => ⟨fun _ _ hab => exists_lt_lt_of_not_covBy hab <| h _ _⟩⟩ @[simp] theorem toDual_covBy_toDual_iff : toDual b ⋖ toDual a ↔ a ⋖ b := and_congr_right' <| forall_congr' fun _ => forall_swap @[simp] theorem ofDual_covBy_ofDual_iff {a b : αᵒᵈ} : ofDual a ⋖ ofDual b ↔ b ⋖ a := and_congr_right' <| forall_congr' fun _ => forall_swap alias ⟨_, CovBy.toDual⟩ := toDual_covBy_toDual_iff alias ⟨_, CovBy.ofDual⟩ := ofDual_covBy_ofDual_iff end LT section Preorder variable [Preorder α] [Preorder β] {a b c : α} theorem CovBy.le (h : a ⋖ b) : a ≤ b := h.1.le protected theorem CovBy.ne (h : a ⋖ b) : a ≠ b := h.lt.ne theorem CovBy.ne' (h : a ⋖ b) : b ≠ a := h.lt.ne' protected theorem CovBy.wcovBy (h : a ⋖ b) : a ⩿ b := ⟨h.le, h.2⟩ theorem WCovBy.covBy_of_not_le (h : a ⩿ b) (h2 : ¬b ≤ a) : a ⋖ b := ⟨h.le.lt_of_not_le h2, h.2⟩ theorem WCovBy.covBy_of_lt (h : a ⩿ b) (h2 : a < b) : a ⋖ b := ⟨h2, h.2⟩ lemma CovBy.of_le_of_lt (hac : a ⋖ c) (hab : a ≤ b) (hbc : b < c) : b ⋖ c := ⟨hbc, fun _x hbx hxc ↦ hac.2 (hab.trans_lt hbx) hxc⟩ lemma CovBy.of_lt_of_le (hac : a ⋖ c) (hab : a < b) (hbc : b ≤ c) : a ⋖ b := ⟨hab, fun _x hax hxb ↦ hac.2 hax <| hxb.trans_le hbc⟩ theorem not_covBy_of_lt_of_lt (h₁ : a < b) (h₂ : b < c) : ¬a ⋖ c := (not_covBy_iff (h₁.trans h₂)).2 ⟨b, h₁, h₂⟩ theorem covBy_iff_wcovBy_and_lt : a ⋖ b ↔ a ⩿ b ∧ a < b := ⟨fun h => ⟨h.wcovBy, h.lt⟩, fun h => h.1.covBy_of_lt h.2⟩ theorem covBy_iff_wcovBy_and_not_le : a ⋖ b ↔ a ⩿ b ∧ ¬b ≤ a := ⟨fun h => ⟨h.wcovBy, h.lt.not_le⟩, fun h => h.1.covBy_of_not_le h.2⟩ theorem wcovBy_iff_covBy_or_le_and_le : a ⩿ b ↔ a ⋖ b ∨ a ≤ b ∧ b ≤ a := ⟨fun h => or_iff_not_imp_right.mpr fun h' => h.covBy_of_not_le fun hba => h' ⟨h.le, hba⟩, fun h' => h'.elim (fun h => h.wcovBy) fun h => h.1.wcovBy_of_le h.2⟩ alias ⟨WCovBy.covBy_or_le_and_le, _⟩ := wcovBy_iff_covBy_or_le_and_le theorem AntisymmRel.trans_covBy (hab : AntisymmRel (· ≤ ·) a b) (hbc : b ⋖ c) : a ⋖ c := ⟨hab.1.trans_lt hbc.lt, fun _ had hdc => hbc.2 (hab.2.trans_lt had) hdc⟩ theorem covBy_congr_left (hab : AntisymmRel (· ≤ ·) a b) : a ⋖ c ↔ b ⋖ c := ⟨hab.symm.trans_covBy, hab.trans_covBy⟩ theorem CovBy.trans_antisymmRel (hab : a ⋖ b) (hbc : AntisymmRel (· ≤ ·) b c) : a ⋖ c := ⟨hab.lt.trans_le hbc.1, fun _ had hdb => hab.2 had <| hdb.trans_le hbc.2⟩ theorem covBy_congr_right (hab : AntisymmRel (· ≤ ·) a b) : c ⋖ a ↔ c ⋖ b := ⟨fun h => h.trans_antisymmRel hab, fun h => h.trans_antisymmRel hab.symm⟩ instance : IsNonstrictStrictOrder α (· ⩿ ·) (· ⋖ ·) := ⟨fun _ _ => covBy_iff_wcovBy_and_not_le.trans <| and_congr_right fun h => h.wcovBy_iff_le.not.symm⟩ instance CovBy.isIrrefl : IsIrrefl α (· ⋖ ·) := ⟨fun _ ha => ha.ne rfl⟩ theorem CovBy.Ioo_eq (h : a ⋖ b) : Ioo a b = ∅ := h.wcovBy.Ioo_eq theorem covBy_iff_Ioo_eq : a ⋖ b ↔ a < b ∧ Ioo a b = ∅ := and_congr_right' <| by simp [eq_empty_iff_forall_not_mem] theorem CovBy.of_image (f : α ↪o β) (h : f a ⋖ f b) : a ⋖ b := ⟨f.lt_iff_lt.mp h.lt, fun _ hac hcb => h.2 (f.lt_iff_lt.mpr hac) (f.lt_iff_lt.mpr hcb)⟩ theorem CovBy.image (f : α ↪o β) (hab : a ⋖ b) (h : (range f).OrdConnected) : f a ⋖ f b := (hab.wcovBy.image f h).covBy_of_lt <| f.strictMono hab.lt theorem Set.OrdConnected.apply_covBy_apply_iff (f : α ↪o β) (h : (range f).OrdConnected) : f a ⋖ f b ↔ a ⋖ b := ⟨CovBy.of_image f, fun hab => hab.image f h⟩ @[simp] theorem apply_covBy_apply_iff {E : Type*} [EquivLike E α β] [OrderIsoClass E α β] (e : E) : e a ⋖ e b ↔ a ⋖ b := (ordConnected_range (e : α ≃o β)).apply_covBy_apply_iff ((e : α ≃o β) : α ↪o β) theorem covBy_of_eq_or_eq (hab : a < b) (h : ∀ c, a ≤ c → c ≤ b → c = a ∨ c = b) : a ⋖ b := ⟨hab, fun c ha hb => (h c ha.le hb.le).elim ha.ne' hb.ne⟩ theorem OrderEmbedding.covBy_of_apply {α β : Type*} [Preorder α] [Preorder β] (f : α ↪o β) {x y : α} (h : f x ⋖ f y) : x ⋖ y := by use f.lt_iff_lt.1 h.1 intro a rw [← f.lt_iff_lt, ← f.lt_iff_lt] apply h.2 theorem OrderIso.map_covBy {α β : Type*} [Preorder α] [Preorder β] (f : α ≃o β) {x y : α} : f x ⋖ f y ↔ x ⋖ y := by use f.toOrderEmbedding.covBy_of_apply conv_lhs => rw [← f.symm_apply_apply x, ← f.symm_apply_apply y] exact f.symm.toOrderEmbedding.covBy_of_apply end Preorder section PartialOrder variable [PartialOrder α] {a b c : α} theorem WCovBy.covBy_of_ne (h : a ⩿ b) (h2 : a ≠ b) : a ⋖ b := ⟨h.le.lt_of_ne h2, h.2⟩ theorem covBy_iff_wcovBy_and_ne : a ⋖ b ↔ a ⩿ b ∧ a ≠ b := ⟨fun h => ⟨h.wcovBy, h.ne⟩, fun h => h.1.covBy_of_ne h.2⟩ theorem wcovBy_iff_covBy_or_eq : a ⩿ b ↔ a ⋖ b ∨ a = b := by rw [le_antisymm_iff, wcovBy_iff_covBy_or_le_and_le] theorem wcovBy_iff_eq_or_covBy : a ⩿ b ↔ a = b ∨ a ⋖ b := wcovBy_iff_covBy_or_eq.trans or_comm alias ⟨WCovBy.covBy_or_eq, _⟩ := wcovBy_iff_covBy_or_eq alias ⟨WCovBy.eq_or_covBy, _⟩ := wcovBy_iff_eq_or_covBy theorem CovBy.eq_or_eq (h : a ⋖ b) (h2 : a ≤ c) (h3 : c ≤ b) : c = a ∨ c = b := h.wcovBy.eq_or_eq h2 h3 /-- An `iff` version of `CovBy.eq_or_eq` and `covBy_of_eq_or_eq`. -/ theorem covBy_iff_lt_and_eq_or_eq : a ⋖ b ↔ a < b ∧ ∀ c, a ≤ c → c ≤ b → c = a ∨ c = b := ⟨fun h => ⟨h.lt, fun _ => h.eq_or_eq⟩, And.rec covBy_of_eq_or_eq⟩ theorem CovBy.Ico_eq (h : a ⋖ b) : Ico a b = {a} := by rw [← Ioo_union_left h.lt, h.Ioo_eq, empty_union] theorem CovBy.Ioc_eq (h : a ⋖ b) : Ioc a b = {b} := by rw [← Ioo_union_right h.lt, h.Ioo_eq, empty_union] theorem CovBy.Icc_eq (h : a ⋖ b) : Icc a b = {a, b} := h.wcovBy.Icc_eq end PartialOrder section LinearOrder variable [LinearOrder α] {a b c : α} theorem CovBy.Ioi_eq (h : a ⋖ b) : Ioi a = Ici b := by rw [← Ioo_union_Ici_eq_Ioi h.lt, h.Ioo_eq, empty_union] theorem CovBy.Iio_eq (h : a ⋖ b) : Iio b = Iic a := by rw [← Iic_union_Ioo_eq_Iio h.lt, h.Ioo_eq, union_empty] theorem WCovBy.le_of_lt (hab : a ⩿ b) (hcb : c < b) : c ≤ a := not_lt.1 fun hac => hab.2 hac hcb theorem WCovBy.ge_of_gt (hab : a ⩿ b) (hac : a < c) : b ≤ c := not_lt.1 <| hab.2 hac theorem CovBy.le_of_lt (hab : a ⋖ b) : c < b → c ≤ a := hab.wcovBy.le_of_lt theorem CovBy.ge_of_gt (hab : a ⋖ b) : a < c → b ≤ c := hab.wcovBy.ge_of_gt theorem CovBy.unique_left (ha : a ⋖ c) (hb : b ⋖ c) : a = b := (hb.le_of_lt ha.lt).antisymm <| ha.le_of_lt hb.lt theorem CovBy.unique_right (hb : a ⋖ b) (hc : a ⋖ c) : b = c := (hb.ge_of_gt hc.lt).antisymm <| hc.ge_of_gt hb.lt /-- If `a`, `b`, `c` are consecutive and `a < x < c` then `x = b`. -/ theorem CovBy.eq_of_between {x : α} (hab : a ⋖ b) (hbc : b ⋖ c) (hax : a < x) (hxc : x < c) : x = b := le_antisymm (le_of_not_lt fun h => hbc.2 h hxc) (le_of_not_lt <| hab.2 hax) theorem covBy_iff_lt_iff_le_left {x y : α} : x ⋖ y ↔ ∀ {z}, z < y ↔ z ≤ x where mp := fun hx _z ↦ ⟨hx.le_of_lt, fun hz ↦ hz.trans_lt hx.lt⟩ mpr := fun H ↦ ⟨H.2 le_rfl, fun _z hx hz ↦ (H.1 hz).not_lt hx⟩ theorem covBy_iff_le_iff_lt_left {x y : α} : x ⋖ y ↔ ∀ {z}, z ≤ x ↔ z < y := by simp_rw [covBy_iff_lt_iff_le_left, iff_comm] theorem covBy_iff_lt_iff_le_right {x y : α} : x ⋖ y ↔ ∀ {z}, x < z ↔ y ≤ z := by trans ∀ {z}, ¬ z ≤ x ↔ ¬ z < y · simp_rw [covBy_iff_le_iff_lt_left, not_iff_not] · simp theorem covBy_iff_le_iff_lt_right {x y : α} : x ⋖ y ↔ ∀ {z}, y ≤ z ↔ x < z := by simp_rw [covBy_iff_lt_iff_le_right, iff_comm] alias ⟨CovBy.lt_iff_le_left, _⟩ := covBy_iff_lt_iff_le_left alias ⟨CovBy.le_iff_lt_left, _⟩ := covBy_iff_le_iff_lt_left alias ⟨CovBy.lt_iff_le_right, _⟩ := covBy_iff_lt_iff_le_right alias ⟨CovBy.le_iff_lt_right, _⟩ := covBy_iff_le_iff_lt_right /-- If `a < b` then there exist `a' > a` and `b' < b` such that `Set.Iio a'` is strictly to the left of `Set.Ioi b'`. -/ lemma LT.lt.exists_disjoint_Iio_Ioi (h : a < b) : ∃ a' > a, ∃ b' < b, ∀ x < a', ∀ y > b', x < y := by by_cases h' : a ⋖ b · exact ⟨b, h, a, h, fun x hx y hy => hx.trans_le <| h'.ge_of_gt hy⟩ · rcases h.exists_lt_lt h' with ⟨c, ha, hb⟩ exact ⟨c, ha, c, hb, fun _ h₁ _ => lt_trans h₁⟩ end LinearOrder namespace Bool @[simp] theorem wcovBy_iff : ∀ {a b : Bool}, a ⩿ b ↔ a ≤ b := by unfold WCovBy; decide @[simp] theorem covBy_iff : ∀ {a b : Bool}, a ⋖ b ↔ a < b := by unfold CovBy; decide instance instDecidableRelWCovBy : DecidableRel (· ⩿ · : Bool → Bool → Prop) := fun _ _ ↦ decidable_of_iff _ wcovBy_iff.symm instance instDecidableRelCovBy : DecidableRel (· ⋖ · : Bool → Bool → Prop) := fun _ _ ↦ decidable_of_iff _ covBy_iff.symm end Bool namespace Set variable {s t : Set α} {a : α} @[simp] lemma wcovBy_insert (x : α) (s : Set α) : s ⩿ insert x s := by refine wcovBy_of_eq_or_eq (subset_insert x s) fun t hst h2t => ?_ by_cases h : x ∈ t · exact Or.inr (subset_antisymm h2t <| insert_subset_iff.mpr ⟨h, hst⟩) · refine Or.inl (subset_antisymm ?_ hst) rwa [← diff_singleton_eq_self h, diff_singleton_subset_iff] @[simp] lemma sdiff_singleton_wcovBy (s : Set α) (a : α) : s \ {a} ⩿ s := by by_cases ha : a ∈ s · convert wcovBy_insert a _ ext simp [ha] · simp [ha] @[simp] lemma covBy_insert (ha : a ∉ s) : s ⋖ insert a s := (wcovBy_insert _ _).covBy_of_lt <| ssubset_insert ha @[simp] lemma sdiff_singleton_covBy (ha : a ∈ s) : s \ {a} ⋖ s := ⟨sdiff_lt (singleton_subset_iff.2 ha) <| singleton_ne_empty _, (sdiff_singleton_wcovBy _ _).2⟩ lemma _root_.CovBy.exists_set_insert (h : s ⋖ t) : ∃ a ∉ s, insert a s = t := let ⟨a, ha, hst⟩ := ssubset_iff_insert.1 h.lt ⟨a, ha, (hst.eq_of_not_ssuperset <| h.2 <| ssubset_insert ha).symm⟩ lemma _root_.CovBy.exists_set_sdiff_singleton (h : s ⋖ t) : ∃ a ∈ t, t \ {a} = s := let ⟨a, ha, hst⟩ := ssubset_iff_sdiff_singleton.1 h.lt ⟨a, ha, (hst.eq_of_not_ssubset fun h' ↦ h.2 h' <| sdiff_lt (singleton_subset_iff.2 ha) <| singleton_ne_empty _).symm⟩ lemma covBy_iff_exists_insert : s ⋖ t ↔ ∃ a ∉ s, insert a s = t := ⟨CovBy.exists_set_insert, by rintro ⟨a, ha, rfl⟩; exact covBy_insert ha⟩ lemma covBy_iff_exists_sdiff_singleton : s ⋖ t ↔ ∃ a ∈ t, t \ {a} = s := ⟨CovBy.exists_set_sdiff_singleton, by rintro ⟨a, ha, rfl⟩; exact sdiff_singleton_covBy ha⟩ end Set section Relation open Relation lemma wcovBy_eq_reflGen_covBy [PartialOrder α] : ((· : α) ⩿ ·) = ReflGen (· ⋖ ·) := by ext x y; simp_rw [wcovBy_iff_eq_or_covBy, @eq_comm _ x, reflGen_iff] lemma transGen_wcovBy_eq_reflTransGen_covBy [PartialOrder α] : TransGen ((· : α) ⩿ ·) = ReflTransGen (· ⋖ ·) := by rw [wcovBy_eq_reflGen_covBy, transGen_reflGen] lemma reflTransGen_wcovBy_eq_reflTransGen_covBy [PartialOrder α] : ReflTransGen ((· : α) ⩿ ·) = ReflTransGen (· ⋖ ·) := by rw [wcovBy_eq_reflGen_covBy, reflTransGen_reflGen] end Relation namespace Prod variable [PartialOrder α] [PartialOrder β] {a a₁ a₂ : α} {b b₁ b₂ : β} {x y : α × β} @[simp] theorem swap_wcovBy_swap : x.swap ⩿ y.swap ↔ x ⩿ y := apply_wcovBy_apply_iff (OrderIso.prodComm : α × β ≃o β × α) @[simp] theorem swap_covBy_swap : x.swap ⋖ y.swap ↔ x ⋖ y := apply_covBy_apply_iff (OrderIso.prodComm : α × β ≃o β × α) theorem fst_eq_or_snd_eq_of_wcovBy : x ⩿ y → x.1 = y.1 ∨ x.2 = y.2 := by refine fun h => of_not_not fun hab => ?_ push_neg at hab exact h.2 (mk_lt_mk.2 <| Or.inl ⟨hab.1.lt_of_le h.1.1, le_rfl⟩) (mk_lt_mk.2 <| Or.inr ⟨le_rfl, hab.2.lt_of_le h.1.2⟩) theorem _root_.WCovBy.fst (h : x ⩿ y) : x.1 ⩿ y.1 := ⟨h.1.1, fun _ h₁ h₂ => h.2 (mk_lt_mk_iff_left.2 h₁) ⟨⟨h₂.le, h.1.2⟩, fun hc => h₂.not_le hc.1⟩⟩ theorem _root_.WCovBy.snd (h : x ⩿ y) : x.2 ⩿ y.2 := ⟨h.1.2, fun _ h₁ h₂ => h.2 (mk_lt_mk_iff_right.2 h₁) ⟨⟨h.1.1, h₂.le⟩, fun hc => h₂.not_le hc.2⟩⟩ theorem mk_wcovBy_mk_iff_left : (a₁, b) ⩿ (a₂, b) ↔ a₁ ⩿ a₂ := by refine ⟨WCovBy.fst, (And.imp mk_le_mk_iff_left.2) fun h c h₁ h₂ => ?_⟩ have : c.2 = b := h₂.le.2.antisymm h₁.le.2 rw [← @Prod.mk.eta _ _ c, this, mk_lt_mk_iff_left] at h₁ h₂ exact h h₁ h₂
theorem mk_wcovBy_mk_iff_right : (a, b₁) ⩿ (a, b₂) ↔ b₁ ⩿ b₂ := swap_wcovBy_swap.trans mk_wcovBy_mk_iff_left
Mathlib/Order/Cover.lean
530
531
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen -/ import Mathlib.Algebra.Algebra.Subalgebra.Tower import Mathlib.Data.Finite.Sum import Mathlib.Data.Matrix.Block import Mathlib.Data.Matrix.Notation import Mathlib.LinearAlgebra.Basis.Basic import Mathlib.LinearAlgebra.Basis.Fin import Mathlib.LinearAlgebra.Basis.Prod import Mathlib.LinearAlgebra.Basis.SMul import Mathlib.LinearAlgebra.Matrix.StdBasis import Mathlib.RingTheory.AlgebraTower import Mathlib.RingTheory.Ideal.Span /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. ## Main definitions In the list below, and in all this file, `R` is a commutative ring (semiring is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite types used for indexing. * `LinearMap.toMatrix`: given bases `v₁ : ι → M₁` and `v₂ : κ → M₂`, the `R`-linear equivalence from `M₁ →ₗ[R] M₂` to `Matrix κ ι R` * `Matrix.toLin`: the inverse of `LinearMap.toMatrix` * `LinearMap.toMatrix'`: the `R`-linear equivalence from `(m → R) →ₗ[R] (n → R)` to `Matrix m n R` (with the standard basis on `m → R` and `n → R`) * `Matrix.toLin'`: the inverse of `LinearMap.toMatrix'` * `algEquivMatrix`: given a basis indexed by `n`, the `R`-algebra equivalence between `R`-endomorphisms of `M` and `Matrix n n R` ## Issues This file was originally written without attention to non-commutative rings, and so mostly only works in the commutative setting. This should be fixed. In particular, `Matrix.mulVec` gives us a linear equivalence `Matrix m n R ≃ₗ[R] (n → R) →ₗ[Rᵐᵒᵖ] (m → R)` while `Matrix.vecMul` gives us a linear equivalence `Matrix m n R ≃ₗ[Rᵐᵒᵖ] (m → R) →ₗ[R] (n → R)`. At present, the first equivalence is developed in detail but only for commutative rings (and we omit the distinction between `Rᵐᵒᵖ` and `R`), while the second equivalence is developed only in brief, but for not-necessarily-commutative rings. Naming is slightly inconsistent between the two developments. In the original (commutative) development `linear` is abbreviated to `lin`, although this is not consistent with the rest of mathlib. In the new (non-commutative) development `linear` is not abbreviated, and declarations use `_right` to indicate they use the right action of matrices on vectors (via `Matrix.vecMul`). When the two developments are made uniform, the names should be made uniform, too, by choosing between `linear` and `lin` consistently, and (presumably) adding `_left` where necessary. ## Tags linear_map, matrix, linear_equiv, diagonal, det, trace -/ noncomputable section open LinearMap Matrix Set Submodule section ToMatrixRight variable {R : Type*} [Semiring R] variable {l m n : Type*} /-- `Matrix.vecMul M` is a linear map. -/ def Matrix.vecMulLinear [Fintype m] (M : Matrix m n R) : (m → R) →ₗ[R] n → R where toFun x := x ᵥ* M map_add' _ _ := funext fun _ ↦ add_dotProduct _ _ _ map_smul' _ _ := funext fun _ ↦ smul_dotProduct _ _ _ @[simp] theorem Matrix.vecMulLinear_apply [Fintype m] (M : Matrix m n R) (x : m → R) : M.vecMulLinear x = x ᵥ* M := rfl theorem Matrix.coe_vecMulLinear [Fintype m] (M : Matrix m n R) : (M.vecMulLinear : _ → _) = M.vecMul := rfl variable [Fintype m] theorem range_vecMulLinear (M : Matrix m n R) : LinearMap.range M.vecMulLinear = span R (range M.row) := by letI := Classical.decEq m simp_rw [range_eq_map, ← iSup_range_single, Submodule.map_iSup, range_eq_map, ← Ideal.span_singleton_one, Ideal.span, Submodule.map_span, image_image, image_singleton, Matrix.vecMulLinear_apply, iSup_span, range_eq_iUnion, iUnion_singleton_eq_range, LinearMap.single, LinearMap.coe_mk, AddHom.coe_mk, row_def] unfold vecMul simp_rw [single_dotProduct, one_mul] theorem Matrix.vecMul_injective_iff {R : Type*} [Ring R] {M : Matrix m n R} : Function.Injective M.vecMul ↔ LinearIndependent R M.row := by rw [← coe_vecMulLinear] simp only [← LinearMap.ker_eq_bot, Fintype.linearIndependent_iff, Submodule.eq_bot_iff, LinearMap.mem_ker, vecMulLinear_apply, row_def] refine ⟨fun h c h0 ↦ congr_fun <| h c ?_, fun h c h0 ↦ funext <| h c ?_⟩ · rw [← h0] ext i simp [vecMul, dotProduct] · rw [← h0] ext j simp [vecMul, dotProduct] lemma Matrix.linearIndependent_rows_of_isUnit {R : Type*} [Ring R] {A : Matrix m m R} [DecidableEq m] (ha : IsUnit A) : LinearIndependent R A.row := by rw [← Matrix.vecMul_injective_iff] exact Matrix.vecMul_injective_of_isUnit ha section variable [DecidableEq m] /-- Linear maps `(m → R) →ₗ[R] (n → R)` are linearly equivalent over `Rᵐᵒᵖ` to `Matrix m n R`, by having matrices act by right multiplication. -/ def LinearMap.toMatrixRight' : ((m → R) →ₗ[R] n → R) ≃ₗ[Rᵐᵒᵖ] Matrix m n R where toFun f i j := f (single R (fun _ ↦ R) i 1) j invFun := Matrix.vecMulLinear right_inv M := by ext i j simp left_inv f := by apply (Pi.basisFun R m).ext intro j; ext i simp map_add' f g := by ext i j simp only [Pi.add_apply, LinearMap.add_apply, Matrix.add_apply] map_smul' c f := by ext i j simp only [Pi.smul_apply, LinearMap.smul_apply, RingHom.id_apply, Matrix.smul_apply] /-- A `Matrix m n R` is linearly equivalent over `Rᵐᵒᵖ` to a linear map `(m → R) →ₗ[R] (n → R)`, by having matrices act by right multiplication. -/ abbrev Matrix.toLinearMapRight' [DecidableEq m] : Matrix m n R ≃ₗ[Rᵐᵒᵖ] (m → R) →ₗ[R] n → R := LinearEquiv.symm LinearMap.toMatrixRight' @[simp] theorem Matrix.toLinearMapRight'_apply (M : Matrix m n R) (v : m → R) : (Matrix.toLinearMapRight') M v = v ᵥ* M := rfl @[simp] theorem Matrix.toLinearMapRight'_mul [Fintype l] [DecidableEq l] (M : Matrix l m R) (N : Matrix m n R) : Matrix.toLinearMapRight' (M * N) = (Matrix.toLinearMapRight' N).comp (Matrix.toLinearMapRight' M) := LinearMap.ext fun _x ↦ (vecMul_vecMul _ M N).symm theorem Matrix.toLinearMapRight'_mul_apply [Fintype l] [DecidableEq l] (M : Matrix l m R) (N : Matrix m n R) (x) : Matrix.toLinearMapRight' (M * N) x = Matrix.toLinearMapRight' N (Matrix.toLinearMapRight' M x) := (vecMul_vecMul _ M N).symm @[simp] theorem Matrix.toLinearMapRight'_one : Matrix.toLinearMapRight' (1 : Matrix m m R) = LinearMap.id := by ext simp [Module.End.one_apply] /-- If `M` and `M'` are each other's inverse matrices, they provide an equivalence between `n → A` and `m → A` corresponding to `M.vecMul` and `M'.vecMul`. -/ @[simps] def Matrix.toLinearEquivRight'OfInv [Fintype n] [DecidableEq n] {M : Matrix m n R} {M' : Matrix n m R} (hMM' : M * M' = 1) (hM'M : M' * M = 1) : (n → R) ≃ₗ[R] m → R := { LinearMap.toMatrixRight'.symm M' with toFun := Matrix.toLinearMapRight' M' invFun := Matrix.toLinearMapRight' M left_inv := fun x ↦ by rw [← Matrix.toLinearMapRight'_mul_apply, hM'M, Matrix.toLinearMapRight'_one, id_apply] right_inv := fun x ↦ by rw [← Matrix.toLinearMapRight'_mul_apply, hMM', Matrix.toLinearMapRight'_one, id_apply] } end end ToMatrixRight /-! From this point on, we only work with commutative rings, and fail to distinguish between `Rᵐᵒᵖ` and `R`. This should eventually be remedied. -/ section mulVec variable {R : Type*} [CommSemiring R] variable {k l m n : Type*} /-- `Matrix.mulVec M` is a linear map. -/ def Matrix.mulVecLin [Fintype n] (M : Matrix m n R) : (n → R) →ₗ[R] m → R where toFun := M.mulVec map_add' _ _ := funext fun _ ↦ dotProduct_add _ _ _ map_smul' _ _ := funext fun _ ↦ dotProduct_smul _ _ _ theorem Matrix.coe_mulVecLin [Fintype n] (M : Matrix m n R) : (M.mulVecLin : _ → _) = M.mulVec := rfl @[simp] theorem Matrix.mulVecLin_apply [Fintype n] (M : Matrix m n R) (v : n → R) : M.mulVecLin v = M *ᵥ v := rfl @[simp] theorem Matrix.mulVecLin_zero [Fintype n] : Matrix.mulVecLin (0 : Matrix m n R) = 0 := LinearMap.ext zero_mulVec @[simp] theorem Matrix.mulVecLin_add [Fintype n] (M N : Matrix m n R) : (M + N).mulVecLin = M.mulVecLin + N.mulVecLin := LinearMap.ext fun _ ↦ add_mulVec _ _ _ @[simp] theorem Matrix.mulVecLin_transpose [Fintype m] (M : Matrix m n R) : Mᵀ.mulVecLin = M.vecMulLinear := by ext; simp [mulVec_transpose] @[simp] theorem Matrix.vecMulLinear_transpose [Fintype n] (M : Matrix m n R) : Mᵀ.vecMulLinear = M.mulVecLin := by ext; simp [vecMul_transpose] theorem Matrix.mulVecLin_submatrix [Fintype n] [Fintype l] (f₁ : m → k) (e₂ : n ≃ l) (M : Matrix k l R) : (M.submatrix f₁ e₂).mulVecLin = funLeft R R f₁ ∘ₗ M.mulVecLin ∘ₗ funLeft _ _ e₂.symm := LinearMap.ext fun _ ↦ submatrix_mulVec_equiv _ _ _ _ /-- A variant of `Matrix.mulVecLin_submatrix` that keeps around `LinearEquiv`s. -/ theorem Matrix.mulVecLin_reindex [Fintype n] [Fintype l] (e₁ : k ≃ m) (e₂ : l ≃ n) (M : Matrix k l R) : (reindex e₁ e₂ M).mulVecLin = ↑(LinearEquiv.funCongrLeft R R e₁.symm) ∘ₗ M.mulVecLin ∘ₗ ↑(LinearEquiv.funCongrLeft R R e₂) := Matrix.mulVecLin_submatrix _ _ _ variable [Fintype n] @[simp] theorem Matrix.mulVecLin_one [DecidableEq n] : Matrix.mulVecLin (1 : Matrix n n R) = LinearMap.id := by ext; simp [Matrix.one_apply, Pi.single_apply, eq_comm] @[simp] theorem Matrix.mulVecLin_mul [Fintype m] (M : Matrix l m R) (N : Matrix m n R) : Matrix.mulVecLin (M * N) = (Matrix.mulVecLin M).comp (Matrix.mulVecLin N) := LinearMap.ext fun _ ↦ (mulVec_mulVec _ _ _).symm theorem Matrix.ker_mulVecLin_eq_bot_iff {M : Matrix m n R} : (LinearMap.ker M.mulVecLin) = ⊥ ↔ ∀ v, M *ᵥ v = 0 → v = 0 := by simp only [Submodule.eq_bot_iff, LinearMap.mem_ker, Matrix.mulVecLin_apply] theorem Matrix.range_mulVecLin (M : Matrix m n R) : LinearMap.range M.mulVecLin = span R (range M.col) := by rw [← vecMulLinear_transpose, range_vecMulLinear, row_transpose] theorem Matrix.mulVec_injective_iff {R : Type*} [CommRing R] {M : Matrix m n R} : Function.Injective M.mulVec ↔ LinearIndependent R M.col := by change Function.Injective (fun x ↦ _) ↔ _ simp_rw [← M.vecMul_transpose, vecMul_injective_iff, row_transpose] lemma Matrix.linearIndependent_cols_of_isUnit {R : Type*} [CommRing R] [Fintype m] {A : Matrix m m R} [DecidableEq m] (ha : IsUnit A) : LinearIndependent R A.col := by rw [← Matrix.mulVec_injective_iff] exact Matrix.mulVec_injective_of_isUnit ha end mulVec section ToMatrix' variable {R : Type*} [CommSemiring R] variable {k l m n : Type*} [DecidableEq n] [Fintype n] /-- Linear maps `(n → R) →ₗ[R] (m → R)` are linearly equivalent to `Matrix m n R`. -/ def LinearMap.toMatrix' : ((n → R) →ₗ[R] m → R) ≃ₗ[R] Matrix m n R where toFun f := of fun i j ↦ f (Pi.single j 1) i invFun := Matrix.mulVecLin right_inv M := by ext i j simp only [Matrix.mulVec_single_one, Matrix.mulVecLin_apply, of_apply, transpose_apply] left_inv f := by apply (Pi.basisFun R n).ext intro j; ext i simp only [Pi.basisFun_apply, Matrix.mulVec_single_one, Matrix.mulVecLin_apply, of_apply, transpose_apply] map_add' f g := by ext i j simp only [Pi.add_apply, LinearMap.add_apply, of_apply, Matrix.add_apply] map_smul' c f := by ext i j simp only [Pi.smul_apply, LinearMap.smul_apply, RingHom.id_apply, of_apply, Matrix.smul_apply] /-- A `Matrix m n R` is linearly equivalent to a linear map `(n → R) →ₗ[R] (m → R)`. Note that the forward-direction does not require `DecidableEq` and is `Matrix.vecMulLin`. -/ def Matrix.toLin' : Matrix m n R ≃ₗ[R] (n → R) →ₗ[R] m → R := LinearMap.toMatrix'.symm theorem Matrix.toLin'_apply' (M : Matrix m n R) : Matrix.toLin' M = M.mulVecLin := rfl @[simp] theorem LinearMap.toMatrix'_symm : (LinearMap.toMatrix'.symm : Matrix m n R ≃ₗ[R] _) = Matrix.toLin' := rfl @[simp] theorem Matrix.toLin'_symm : (Matrix.toLin'.symm : ((n → R) →ₗ[R] m → R) ≃ₗ[R] _) = LinearMap.toMatrix' := rfl @[simp] theorem LinearMap.toMatrix'_toLin' (M : Matrix m n R) : LinearMap.toMatrix' (Matrix.toLin' M) = M := LinearMap.toMatrix'.apply_symm_apply M @[simp] theorem Matrix.toLin'_toMatrix' (f : (n → R) →ₗ[R] m → R) : Matrix.toLin' (LinearMap.toMatrix' f) = f := Matrix.toLin'.apply_symm_apply f @[simp] theorem LinearMap.toMatrix'_apply (f : (n → R) →ₗ[R] m → R) (i j) : LinearMap.toMatrix' f i j = f (fun j' ↦ if j' = j then 1 else 0) i := by simp only [LinearMap.toMatrix', LinearEquiv.coe_mk, of_apply] congr! with i split_ifs with h · rw [h, Pi.single_eq_same] apply Pi.single_eq_of_ne h @[simp] theorem Matrix.toLin'_apply (M : Matrix m n R) (v : n → R) : Matrix.toLin' M v = M *ᵥ v := rfl @[simp] theorem Matrix.toLin'_one : Matrix.toLin' (1 : Matrix n n R) = LinearMap.id := Matrix.mulVecLin_one @[simp] theorem LinearMap.toMatrix'_id : LinearMap.toMatrix' (LinearMap.id : (n → R) →ₗ[R] n → R) = 1 := by ext rw [Matrix.one_apply, LinearMap.toMatrix'_apply, id_apply] @[simp] theorem LinearMap.toMatrix'_one : LinearMap.toMatrix' (1 : (n → R) →ₗ[R] n → R) = 1 := LinearMap.toMatrix'_id @[simp] theorem Matrix.toLin'_mul [Fintype m] [DecidableEq m] (M : Matrix l m R) (N : Matrix m n R) : Matrix.toLin' (M * N) = (Matrix.toLin' M).comp (Matrix.toLin' N) := Matrix.mulVecLin_mul _ _ @[simp] theorem Matrix.toLin'_submatrix [Fintype l] [DecidableEq l] (f₁ : m → k) (e₂ : n ≃ l) (M : Matrix k l R) : Matrix.toLin' (M.submatrix f₁ e₂) = funLeft R R f₁ ∘ₗ (Matrix.toLin' M) ∘ₗ funLeft _ _ e₂.symm := Matrix.mulVecLin_submatrix _ _ _ /-- A variant of `Matrix.toLin'_submatrix` that keeps around `LinearEquiv`s. -/ theorem Matrix.toLin'_reindex [Fintype l] [DecidableEq l] (e₁ : k ≃ m) (e₂ : l ≃ n) (M : Matrix k l R) : Matrix.toLin' (reindex e₁ e₂ M) = ↑(LinearEquiv.funCongrLeft R R e₁.symm) ∘ₗ (Matrix.toLin' M) ∘ₗ ↑(LinearEquiv.funCongrLeft R R e₂) := Matrix.mulVecLin_reindex _ _ _ /-- Shortcut lemma for `Matrix.toLin'_mul` and `LinearMap.comp_apply` -/ theorem Matrix.toLin'_mul_apply [Fintype m] [DecidableEq m] (M : Matrix l m R) (N : Matrix m n R) (x) : Matrix.toLin' (M * N) x = Matrix.toLin' M (Matrix.toLin' N x) := by rw [Matrix.toLin'_mul, LinearMap.comp_apply] theorem LinearMap.toMatrix'_comp [Fintype l] [DecidableEq l] (f : (n → R) →ₗ[R] m → R) (g : (l → R) →ₗ[R] n → R) : LinearMap.toMatrix' (f.comp g) = LinearMap.toMatrix' f * LinearMap.toMatrix' g := by suffices f.comp g = Matrix.toLin' (LinearMap.toMatrix' f * LinearMap.toMatrix' g) by rw [this, LinearMap.toMatrix'_toLin'] rw [Matrix.toLin'_mul, Matrix.toLin'_toMatrix', Matrix.toLin'_toMatrix'] theorem LinearMap.toMatrix'_mul [Fintype m] [DecidableEq m] (f g : (m → R) →ₗ[R] m → R) : LinearMap.toMatrix' (f * g) = LinearMap.toMatrix' f * LinearMap.toMatrix' g := LinearMap.toMatrix'_comp f g @[simp] theorem LinearMap.toMatrix'_algebraMap (x : R) : LinearMap.toMatrix' (algebraMap R (Module.End R (n → R)) x) = scalar n x := by simp [Module.algebraMap_end_eq_smul_id, smul_eq_diagonal_mul] theorem Matrix.ker_toLin'_eq_bot_iff {M : Matrix n n R} : LinearMap.ker (Matrix.toLin' M) = ⊥ ↔ ∀ v, M *ᵥ v = 0 → v = 0 := Matrix.ker_mulVecLin_eq_bot_iff theorem Matrix.range_toLin' (M : Matrix m n R) : LinearMap.range (Matrix.toLin' M) = span R (range M.col) := Matrix.range_mulVecLin _ /-- If `M` and `M'` are each other's inverse matrices, they provide an equivalence between `m → A` and `n → A` corresponding to `M.mulVec` and `M'.mulVec`. -/ @[simps] def Matrix.toLin'OfInv [Fintype m] [DecidableEq m] {M : Matrix m n R} {M' : Matrix n m R} (hMM' : M * M' = 1) (hM'M : M' * M = 1) : (m → R) ≃ₗ[R] n → R := { Matrix.toLin' M' with toFun := Matrix.toLin' M' invFun := Matrix.toLin' M left_inv := fun x ↦ by rw [← Matrix.toLin'_mul_apply, hMM', Matrix.toLin'_one, id_apply] right_inv := fun x ↦ by rw [← Matrix.toLin'_mul_apply, hM'M, Matrix.toLin'_one, id_apply] } /-- Linear maps `(n → R) →ₗ[R] (n → R)` are algebra equivalent to `Matrix n n R`. -/ def LinearMap.toMatrixAlgEquiv' : ((n → R) →ₗ[R] n → R) ≃ₐ[R] Matrix n n R := AlgEquiv.ofLinearEquiv LinearMap.toMatrix' LinearMap.toMatrix'_one LinearMap.toMatrix'_mul /-- A `Matrix n n R` is algebra equivalent to a linear map `(n → R) →ₗ[R] (n → R)`. -/ def Matrix.toLinAlgEquiv' : Matrix n n R ≃ₐ[R] (n → R) →ₗ[R] n → R := LinearMap.toMatrixAlgEquiv'.symm @[simp] theorem LinearMap.toMatrixAlgEquiv'_symm : (LinearMap.toMatrixAlgEquiv'.symm : Matrix n n R ≃ₐ[R] _) = Matrix.toLinAlgEquiv' := rfl @[simp] theorem Matrix.toLinAlgEquiv'_symm : (Matrix.toLinAlgEquiv'.symm : ((n → R) →ₗ[R] n → R) ≃ₐ[R] _) = LinearMap.toMatrixAlgEquiv' := rfl @[simp] theorem LinearMap.toMatrixAlgEquiv'_toLinAlgEquiv' (M : Matrix n n R) : LinearMap.toMatrixAlgEquiv' (Matrix.toLinAlgEquiv' M) = M := LinearMap.toMatrixAlgEquiv'.apply_symm_apply M @[simp] theorem Matrix.toLinAlgEquiv'_toMatrixAlgEquiv' (f : (n → R) →ₗ[R] n → R) : Matrix.toLinAlgEquiv' (LinearMap.toMatrixAlgEquiv' f) = f := Matrix.toLinAlgEquiv'.apply_symm_apply f @[simp] theorem LinearMap.toMatrixAlgEquiv'_apply (f : (n → R) →ₗ[R] n → R) (i j) : LinearMap.toMatrixAlgEquiv' f i j = f (fun j' ↦ if j' = j then 1 else 0) i := by simp [LinearMap.toMatrixAlgEquiv'] @[simp] theorem Matrix.toLinAlgEquiv'_apply (M : Matrix n n R) (v : n → R) : Matrix.toLinAlgEquiv' M v = M *ᵥ v := rfl theorem Matrix.toLinAlgEquiv'_one : Matrix.toLinAlgEquiv' (1 : Matrix n n R) = LinearMap.id := Matrix.toLin'_one @[simp] theorem LinearMap.toMatrixAlgEquiv'_id : LinearMap.toMatrixAlgEquiv' (LinearMap.id : (n → R) →ₗ[R] n → R) = 1 := LinearMap.toMatrix'_id theorem LinearMap.toMatrixAlgEquiv'_comp (f g : (n → R) →ₗ[R] n → R) : LinearMap.toMatrixAlgEquiv' (f.comp g) = LinearMap.toMatrixAlgEquiv' f * LinearMap.toMatrixAlgEquiv' g := LinearMap.toMatrix'_comp _ _ theorem LinearMap.toMatrixAlgEquiv'_mul (f g : (n → R) →ₗ[R] n → R) : LinearMap.toMatrixAlgEquiv' (f * g) = LinearMap.toMatrixAlgEquiv' f * LinearMap.toMatrixAlgEquiv' g := LinearMap.toMatrixAlgEquiv'_comp f g end ToMatrix' section ToMatrix section Finite variable {R : Type*} [CommSemiring R] variable {l m n : Type*} [Fintype n] [Finite m] [DecidableEq n] variable {M₁ M₂ : Type*} [AddCommMonoid M₁] [AddCommMonoid M₂] [Module R M₁] [Module R M₂] variable (v₁ : Basis n R M₁) (v₂ : Basis m R M₂) /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between linear maps `M₁ →ₗ M₂` and matrices over `R` indexed by the bases. -/ def LinearMap.toMatrix : (M₁ →ₗ[R] M₂) ≃ₗ[R] Matrix m n R := LinearEquiv.trans (LinearEquiv.arrowCongr v₁.equivFun v₂.equivFun) LinearMap.toMatrix' /-- `LinearMap.toMatrix'` is a particular case of `LinearMap.toMatrix`, for the standard basis `Pi.basisFun R n`. -/ theorem LinearMap.toMatrix_eq_toMatrix' : LinearMap.toMatrix (Pi.basisFun R n) (Pi.basisFun R n) = LinearMap.toMatrix' := rfl /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between matrices over `R` indexed by the bases and linear maps `M₁ →ₗ M₂`. -/ def Matrix.toLin : Matrix m n R ≃ₗ[R] M₁ →ₗ[R] M₂ := (LinearMap.toMatrix v₁ v₂).symm /-- `Matrix.toLin'` is a particular case of `Matrix.toLin`, for the standard basis `Pi.basisFun R n`. -/ theorem Matrix.toLin_eq_toLin' : Matrix.toLin (Pi.basisFun R n) (Pi.basisFun R m) = Matrix.toLin' := rfl @[simp] theorem LinearMap.toMatrix_symm : (LinearMap.toMatrix v₁ v₂).symm = Matrix.toLin v₁ v₂ := rfl @[simp] theorem Matrix.toLin_symm : (Matrix.toLin v₁ v₂).symm = LinearMap.toMatrix v₁ v₂ := rfl @[simp] theorem Matrix.toLin_toMatrix (f : M₁ →ₗ[R] M₂) : Matrix.toLin v₁ v₂ (LinearMap.toMatrix v₁ v₂ f) = f := by rw [← Matrix.toLin_symm, LinearEquiv.apply_symm_apply] @[simp] theorem LinearMap.toMatrix_toLin (M : Matrix m n R) : LinearMap.toMatrix v₁ v₂ (Matrix.toLin v₁ v₂ M) = M := by rw [← Matrix.toLin_symm, LinearEquiv.symm_apply_apply] theorem LinearMap.toMatrix_apply (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : LinearMap.toMatrix v₁ v₂ f i j = v₂.repr (f (v₁ j)) i := by rw [LinearMap.toMatrix, LinearEquiv.trans_apply, LinearMap.toMatrix'_apply, LinearEquiv.arrowCongr_apply, Basis.equivFun_symm_apply, Finset.sum_eq_single j, if_pos rfl, one_smul, Basis.equivFun_apply] · intro j' _ hj' rw [if_neg hj', zero_smul] · intro hj have := Finset.mem_univ j contradiction theorem LinearMap.toMatrix_transpose_apply (f : M₁ →ₗ[R] M₂) (j : n) : (LinearMap.toMatrix v₁ v₂ f)ᵀ j = v₂.repr (f (v₁ j)) := funext fun i ↦ f.toMatrix_apply _ _ i j theorem LinearMap.toMatrix_apply' (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : LinearMap.toMatrix v₁ v₂ f i j = v₂.repr (f (v₁ j)) i := LinearMap.toMatrix_apply v₁ v₂ f i j theorem LinearMap.toMatrix_transpose_apply' (f : M₁ →ₗ[R] M₂) (j : n) : (LinearMap.toMatrix v₁ v₂ f)ᵀ j = v₂.repr (f (v₁ j)) := LinearMap.toMatrix_transpose_apply v₁ v₂ f j /-- This will be a special case of `LinearMap.toMatrix_id_eq_basis_toMatrix`. -/ theorem LinearMap.toMatrix_id : LinearMap.toMatrix v₁ v₁ id = 1 := by ext i j simp [LinearMap.toMatrix_apply, Matrix.one_apply, Finsupp.single_apply, eq_comm] @[simp] theorem LinearMap.toMatrix_one : LinearMap.toMatrix v₁ v₁ 1 = 1 := LinearMap.toMatrix_id v₁ @[simp] lemma LinearMap.toMatrix_singleton {ι : Type*} [Unique ι] (f : R →ₗ[R] R) (i j : ι) : f.toMatrix (.singleton ι R) (.singleton ι R) i j = f 1 := by simp [toMatrix, Subsingleton.elim j default] @[simp] theorem Matrix.toLin_one : Matrix.toLin v₁ v₁ 1 = LinearMap.id := by rw [← LinearMap.toMatrix_id v₁, Matrix.toLin_toMatrix] theorem LinearMap.toMatrix_reindexRange [DecidableEq M₁] (f : M₁ →ₗ[R] M₂) (k : m) (i : n) : LinearMap.toMatrix v₁.reindexRange v₂.reindexRange f ⟨v₂ k, Set.mem_range_self k⟩ ⟨v₁ i, Set.mem_range_self i⟩ = LinearMap.toMatrix v₁ v₂ f k i := by simp_rw [LinearMap.toMatrix_apply, Basis.reindexRange_self, Basis.reindexRange_repr] @[simp] theorem LinearMap.toMatrix_algebraMap (x : R) : LinearMap.toMatrix v₁ v₁ (algebraMap R (Module.End R M₁) x) = scalar n x := by simp [Module.algebraMap_end_eq_smul_id, LinearMap.toMatrix_id, smul_eq_diagonal_mul] theorem LinearMap.toMatrix_mulVec_repr (f : M₁ →ₗ[R] M₂) (x : M₁) : LinearMap.toMatrix v₁ v₂ f *ᵥ v₁.repr x = v₂.repr (f x) := by ext i rw [← Matrix.toLin'_apply, LinearMap.toMatrix, LinearEquiv.trans_apply, Matrix.toLin'_toMatrix', LinearEquiv.arrowCongr_apply, v₂.equivFun_apply] congr exact v₁.equivFun.symm_apply_apply x @[simp] theorem LinearMap.toMatrix_basis_equiv [Fintype l] [DecidableEq l] (b : Basis l R M₁) (b' : Basis l R M₂) : LinearMap.toMatrix b' b (b'.equiv b (Equiv.refl l) : M₂ →ₗ[R] M₁) = 1 := by ext i j simp [LinearMap.toMatrix_apply, Matrix.one_apply, Finsupp.single_apply, eq_comm] theorem LinearMap.toMatrix_smulBasis_left {G} [Group G] [DistribMulAction G M₁] [SMulCommClass G R M₁] (g : G) (f : M₁ →ₗ[R] M₂) : LinearMap.toMatrix (g • v₁) v₂ f = LinearMap.toMatrix v₁ v₂ (f ∘ₗ DistribMulAction.toLinearMap _ _ g) := by ext rw [LinearMap.toMatrix_apply, LinearMap.toMatrix_apply] dsimp theorem LinearMap.toMatrix_smulBasis_right {G} [Group G] [DistribMulAction G M₂] [SMulCommClass G R M₂] (g : G) (f : M₁ →ₗ[R] M₂) : LinearMap.toMatrix v₁ (g • v₂) f = LinearMap.toMatrix v₁ v₂ (DistribMulAction.toLinearMap _ _ g⁻¹ ∘ₗ f) := by ext rw [LinearMap.toMatrix_apply, LinearMap.toMatrix_apply] dsimp end Finite variable {R : Type*} [CommSemiring R] variable {l m n : Type*} [Fintype n] [Fintype m] [DecidableEq n] variable {M₁ M₂ : Type*} [AddCommMonoid M₁] [AddCommMonoid M₂] [Module R M₁] [Module R M₂] variable (v₁ : Basis n R M₁) (v₂ : Basis m R M₂) theorem Matrix.toLin_apply (M : Matrix m n R) (v : M₁) : Matrix.toLin v₁ v₂ M v = ∑ j, (M *ᵥ v₁.repr v) j • v₂ j := show v₂.equivFun.symm (Matrix.toLin' M (v₁.repr v)) = _ by rw [Matrix.toLin'_apply, v₂.equivFun_symm_apply] @[simp] theorem Matrix.toLin_self (M : Matrix m n R) (i : n) : Matrix.toLin v₁ v₂ M (v₁ i) = ∑ j, M j i • v₂ j := by rw [Matrix.toLin_apply, Finset.sum_congr rfl fun j _hj ↦ ?_] rw [Basis.repr_self, Matrix.mulVec, dotProduct, Finset.sum_eq_single i, Finsupp.single_eq_same, mul_one] · intro i' _ i'_ne rw [Finsupp.single_eq_of_ne i'_ne.symm, mul_zero] · intros have := Finset.mem_univ i contradiction variable {M₃ : Type*} [AddCommMonoid M₃] [Module R M₃] (v₃ : Basis l R M₃) theorem LinearMap.toMatrix_comp [Finite l] [DecidableEq m] (f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) : LinearMap.toMatrix v₁ v₃ (f.comp g) = LinearMap.toMatrix v₂ v₃ f * LinearMap.toMatrix v₁ v₂ g := by simp_rw [LinearMap.toMatrix, LinearEquiv.trans_apply, LinearEquiv.arrowCongr_comp _ v₂.equivFun, LinearMap.toMatrix'_comp] theorem LinearMap.toMatrix_mul (f g : M₁ →ₗ[R] M₁) : LinearMap.toMatrix v₁ v₁ (f * g) = LinearMap.toMatrix v₁ v₁ f * LinearMap.toMatrix v₁ v₁ g := by rw [Module.End.mul_eq_comp, LinearMap.toMatrix_comp v₁ v₁ v₁ f g] lemma LinearMap.toMatrix_pow (f : M₁ →ₗ[R] M₁) (k : ℕ) : (toMatrix v₁ v₁ f) ^ k = toMatrix v₁ v₁ (f ^ k) := by induction k with | zero => simp | succ k ih => rw [pow_succ, pow_succ, ih, ← toMatrix_mul] theorem Matrix.toLin_mul [Finite l] [DecidableEq m] (A : Matrix l m R) (B : Matrix m n R) : Matrix.toLin v₁ v₃ (A * B) = (Matrix.toLin v₂ v₃ A).comp (Matrix.toLin v₁ v₂ B) := by apply (LinearMap.toMatrix v₁ v₃).injective haveI : DecidableEq l := fun _ _ ↦ Classical.propDecidable _ rw [LinearMap.toMatrix_comp v₁ v₂ v₃] repeat' rw [LinearMap.toMatrix_toLin] /-- Shortcut lemma for `Matrix.toLin_mul` and `LinearMap.comp_apply`. -/ theorem Matrix.toLin_mul_apply [Finite l] [DecidableEq m] (A : Matrix l m R) (B : Matrix m n R) (x) : Matrix.toLin v₁ v₃ (A * B) x = (Matrix.toLin v₂ v₃ A) (Matrix.toLin v₁ v₂ B x) := by rw [Matrix.toLin_mul v₁ v₂, LinearMap.comp_apply] /-- If `M` and `M` are each other's inverse matrices, `Matrix.toLin M` and `Matrix.toLin M'` form a linear equivalence. -/ @[simps] def Matrix.toLinOfInv [DecidableEq m] {M : Matrix m n R} {M' : Matrix n m R} (hMM' : M * M' = 1) (hM'M : M' * M = 1) : M₁ ≃ₗ[R] M₂ := { Matrix.toLin v₁ v₂ M with toFun := Matrix.toLin v₁ v₂ M invFun := Matrix.toLin v₂ v₁ M' left_inv := fun x ↦ by rw [← Matrix.toLin_mul_apply, hM'M, Matrix.toLin_one, id_apply] right_inv := fun x ↦ by rw [← Matrix.toLin_mul_apply, hMM', Matrix.toLin_one, id_apply] } /-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra equivalence between linear maps `M₁ →ₗ M₁` and square matrices over `R` indexed by the basis. -/ def LinearMap.toMatrixAlgEquiv : (M₁ →ₗ[R] M₁) ≃ₐ[R] Matrix n n R := AlgEquiv.ofLinearEquiv (LinearMap.toMatrix v₁ v₁) (LinearMap.toMatrix_one v₁) (LinearMap.toMatrix_mul v₁) /-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra equivalence between square matrices over `R` indexed by the basis and linear maps `M₁ →ₗ M₁`. -/ def Matrix.toLinAlgEquiv : Matrix n n R ≃ₐ[R] M₁ →ₗ[R] M₁ := (LinearMap.toMatrixAlgEquiv v₁).symm @[simp] theorem LinearMap.toMatrixAlgEquiv_symm : (LinearMap.toMatrixAlgEquiv v₁).symm = Matrix.toLinAlgEquiv v₁ := rfl @[simp] theorem Matrix.toLinAlgEquiv_symm : (Matrix.toLinAlgEquiv v₁).symm = LinearMap.toMatrixAlgEquiv v₁ := rfl @[simp] theorem Matrix.toLinAlgEquiv_toMatrixAlgEquiv (f : M₁ →ₗ[R] M₁) : Matrix.toLinAlgEquiv v₁ (LinearMap.toMatrixAlgEquiv v₁ f) = f := by rw [← Matrix.toLinAlgEquiv_symm, AlgEquiv.apply_symm_apply] @[simp] theorem LinearMap.toMatrixAlgEquiv_toLinAlgEquiv (M : Matrix n n R) : LinearMap.toMatrixAlgEquiv v₁ (Matrix.toLinAlgEquiv v₁ M) = M := by rw [← Matrix.toLinAlgEquiv_symm, AlgEquiv.symm_apply_apply] theorem LinearMap.toMatrixAlgEquiv_apply (f : M₁ →ₗ[R] M₁) (i j : n) : LinearMap.toMatrixAlgEquiv v₁ f i j = v₁.repr (f (v₁ j)) i := by simp [LinearMap.toMatrixAlgEquiv, LinearMap.toMatrix_apply] theorem LinearMap.toMatrixAlgEquiv_transpose_apply (f : M₁ →ₗ[R] M₁) (j : n) : (LinearMap.toMatrixAlgEquiv v₁ f)ᵀ j = v₁.repr (f (v₁ j)) := funext fun i ↦ f.toMatrix_apply _ _ i j theorem LinearMap.toMatrixAlgEquiv_apply' (f : M₁ →ₗ[R] M₁) (i j : n) : LinearMap.toMatrixAlgEquiv v₁ f i j = v₁.repr (f (v₁ j)) i := LinearMap.toMatrixAlgEquiv_apply v₁ f i j theorem LinearMap.toMatrixAlgEquiv_transpose_apply' (f : M₁ →ₗ[R] M₁) (j : n) : (LinearMap.toMatrixAlgEquiv v₁ f)ᵀ j = v₁.repr (f (v₁ j)) := LinearMap.toMatrixAlgEquiv_transpose_apply v₁ f j theorem Matrix.toLinAlgEquiv_apply (M : Matrix n n R) (v : M₁) : Matrix.toLinAlgEquiv v₁ M v = ∑ j, (M *ᵥ v₁.repr v) j • v₁ j := show v₁.equivFun.symm (Matrix.toLinAlgEquiv' M (v₁.repr v)) = _ by rw [Matrix.toLinAlgEquiv'_apply, v₁.equivFun_symm_apply] @[simp] theorem Matrix.toLinAlgEquiv_self (M : Matrix n n R) (i : n) : Matrix.toLinAlgEquiv v₁ M (v₁ i) = ∑ j, M j i • v₁ j := Matrix.toLin_self _ _ _ _ theorem LinearMap.toMatrixAlgEquiv_id : LinearMap.toMatrixAlgEquiv v₁ id = 1 := by simp_rw [LinearMap.toMatrixAlgEquiv, AlgEquiv.ofLinearEquiv_apply, LinearMap.toMatrix_id] theorem Matrix.toLinAlgEquiv_one : Matrix.toLinAlgEquiv v₁ 1 = LinearMap.id := by rw [← LinearMap.toMatrixAlgEquiv_id v₁, Matrix.toLinAlgEquiv_toMatrixAlgEquiv] theorem LinearMap.toMatrixAlgEquiv_reindexRange [DecidableEq M₁] (f : M₁ →ₗ[R] M₁) (k i : n) : LinearMap.toMatrixAlgEquiv v₁.reindexRange f ⟨v₁ k, Set.mem_range_self k⟩ ⟨v₁ i, Set.mem_range_self i⟩ = LinearMap.toMatrixAlgEquiv v₁ f k i := by simp_rw [LinearMap.toMatrixAlgEquiv_apply, Basis.reindexRange_self, Basis.reindexRange_repr] theorem LinearMap.toMatrixAlgEquiv_comp (f g : M₁ →ₗ[R] M₁) : LinearMap.toMatrixAlgEquiv v₁ (f.comp g) = LinearMap.toMatrixAlgEquiv v₁ f * LinearMap.toMatrixAlgEquiv v₁ g := by simp [LinearMap.toMatrixAlgEquiv, LinearMap.toMatrix_comp v₁ v₁ v₁ f g] theorem LinearMap.toMatrixAlgEquiv_mul (f g : M₁ →ₗ[R] M₁) : LinearMap.toMatrixAlgEquiv v₁ (f * g) = LinearMap.toMatrixAlgEquiv v₁ f * LinearMap.toMatrixAlgEquiv v₁ g := by rw [Module.End.mul_eq_comp, LinearMap.toMatrixAlgEquiv_comp v₁ f g] theorem Matrix.toLinAlgEquiv_mul (A B : Matrix n n R) : Matrix.toLinAlgEquiv v₁ (A * B) = (Matrix.toLinAlgEquiv v₁ A).comp (Matrix.toLinAlgEquiv v₁ B) := by convert Matrix.toLin_mul v₁ v₁ v₁ A B @[simp] theorem Matrix.toLin_finTwoProd_apply (a b c d : R) (x : R × R) : Matrix.toLin (Basis.finTwoProd R) (Basis.finTwoProd R) !![a, b; c, d] x = (a * x.fst + b * x.snd, c * x.fst + d * x.snd) := by simp [Matrix.toLin_apply, Matrix.mulVec, dotProduct] theorem Matrix.toLin_finTwoProd (a b c d : R) : Matrix.toLin (Basis.finTwoProd R) (Basis.finTwoProd R) !![a, b; c, d] = (a • LinearMap.fst R R R + b • LinearMap.snd R R R).prod (c • LinearMap.fst R R R + d • LinearMap.snd R R R) := LinearMap.ext <| Matrix.toLin_finTwoProd_apply _ _ _ _ @[simp] theorem toMatrix_distrib_mul_action_toLinearMap (x : R) : LinearMap.toMatrix v₁ v₁ (DistribMulAction.toLinearMap R M₁ x) = Matrix.diagonal fun _ ↦ x := by ext rw [LinearMap.toMatrix_apply, DistribMulAction.toLinearMap_apply, LinearEquiv.map_smul, Basis.repr_self, Finsupp.smul_single_one, Finsupp.single_eq_pi_single, Matrix.diagonal_apply, Pi.single_apply] lemma LinearMap.toMatrix_prodMap [DecidableEq m] [DecidableEq (n ⊕ m)] (φ₁ : Module.End R M₁) (φ₂ : Module.End R M₂) : toMatrix (v₁.prod v₂) (v₁.prod v₂) (φ₁.prodMap φ₂) = Matrix.fromBlocks (toMatrix v₁ v₁ φ₁) 0 0 (toMatrix v₂ v₂ φ₂) := by ext (i|i) (j|j) <;> simp [toMatrix] end ToMatrix namespace Algebra section Lmul variable {R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S] variable {m : Type*} [Fintype m] [DecidableEq m] (b : Basis m R S) theorem toMatrix_lmul' (x : S) (i j) : LinearMap.toMatrix b b (lmul R S x) i j = b.repr (x * b j) i := by simp only [LinearMap.toMatrix_apply', coe_lmul_eq_mul, LinearMap.mul_apply'] @[simp] theorem toMatrix_lsmul (x : R) : LinearMap.toMatrix b b (Algebra.lsmul R R S x) = Matrix.diagonal fun _ ↦ x := toMatrix_distrib_mul_action_toLinearMap b x /-- `leftMulMatrix b x` is the matrix corresponding to the linear map `fun y ↦ x * y`. `leftMulMatrix_eq_repr_mul` gives a formula for the entries of `leftMulMatrix`. This definition is useful for doing (more) explicit computations with `LinearMap.mulLeft`, such as the trace form or norm map for algebras. -/ noncomputable def leftMulMatrix : S →ₐ[R] Matrix m m R where toFun x := LinearMap.toMatrix b b (Algebra.lmul R S x) map_zero' := by rw [map_zero, LinearEquiv.map_zero] map_one' := by rw [map_one, LinearMap.toMatrix_one] map_add' x y := by rw [map_add, LinearEquiv.map_add] map_mul' x y := by rw [map_mul, LinearMap.toMatrix_mul] commutes' r := by ext rw [lmul_algebraMap, toMatrix_lsmul, algebraMap_eq_diagonal, Pi.algebraMap_def, Algebra.id.map_eq_self] theorem leftMulMatrix_apply (x : S) : leftMulMatrix b x = LinearMap.toMatrix b b (lmul R S x) := rfl theorem leftMulMatrix_eq_repr_mul (x : S) (i j) : leftMulMatrix b x i j = b.repr (x * b j) i := by -- This is defeq to just `toMatrix_lmul' b x i j`, -- but the unfolding goes a lot faster with this explicit `rw`. rw [leftMulMatrix_apply, toMatrix_lmul' b x i j] theorem leftMulMatrix_mulVec_repr (x y : S) : leftMulMatrix b x *ᵥ b.repr y = b.repr (x * y) := (LinearMap.mulLeft R x).toMatrix_mulVec_repr b b y
@[simp] theorem toMatrix_lmul_eq (x : S) : LinearMap.toMatrix b b (LinearMap.mulLeft R x) = leftMulMatrix b x := rfl
Mathlib/LinearAlgebra/Matrix/ToLin.lean
833
836
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import Mathlib.Algebra.Module.Torsion import Mathlib.Algebra.Ring.Idempotent import Mathlib.LinearAlgebra.Dimension.FreeAndStrongRankCondition import Mathlib.LinearAlgebra.FiniteDimensional.Defs import Mathlib.RingTheory.Filtration import Mathlib.RingTheory.Ideal.Operations import Mathlib.RingTheory.LocalRing.ResidueField.Basic import Mathlib.RingTheory.Nakayama /-! # The module `I ⧸ I ^ 2` In this file, we provide special API support for the module `I ⧸ I ^ 2`. The official definition is a quotient module of `I`, but the alternative definition as an ideal of `R ⧸ I ^ 2` is also given, and the two are `R`-equivalent as in `Ideal.cotangentEquivIdeal`. Additional support is also given to the cotangent space `m ⧸ m ^ 2` of a local ring. -/ namespace Ideal -- Porting note: universes need to be explicit to avoid bad universe levels in `quotCotangent` universe u v w variable {R : Type u} {S : Type v} {S' : Type w} [CommRing R] [CommSemiring S] [Algebra S R] variable [CommSemiring S'] [Algebra S' R] [Algebra S S'] [IsScalarTower S S' R] (I : Ideal R) /-- `I ⧸ I ^ 2` as a quotient of `I`. -/ def Cotangent : Type _ := I ⧸ (I • ⊤ : Submodule R I) -- The `AddCommGroup, Module (R ⧸ I), Inhabited, Module S, IsScalarTower, IsNoetherian` instances -- should be constructed by a deriving handler. -- https://github.com/leanprover-community/mathlib4/issues/380 instance : AddCommGroup I.Cotangent := by delta Cotangent; infer_instance instance cotangentModule : Module (R ⧸ I) I.Cotangent := by delta Cotangent; infer_instance instance : Inhabited I.Cotangent := ⟨0⟩ instance Cotangent.moduleOfTower : Module S I.Cotangent := Submodule.Quotient.module' _ instance Cotangent.isScalarTower : IsScalarTower S S' I.Cotangent := Submodule.Quotient.isScalarTower _ _ instance [IsNoetherian R I] : IsNoetherian R I.Cotangent := inferInstanceAs (IsNoetherian R (I ⧸ (I • ⊤ : Submodule R I))) /-- The quotient map from `I` to `I ⧸ I ^ 2`. -/ @[simps! -isSimp apply] def toCotangent : I →ₗ[R] I.Cotangent := Submodule.mkQ _ theorem map_toCotangent_ker : (LinearMap.ker I.toCotangent).map I.subtype = I ^ 2 := by rw [Ideal.toCotangent, Submodule.ker_mkQ, pow_two, Submodule.map_smul'' I ⊤ (Submodule.subtype I), Algebra.id.smul_eq_mul, Submodule.map_subtype_top] theorem mem_toCotangent_ker {x : I} : x ∈ LinearMap.ker I.toCotangent ↔ (x : R) ∈ I ^ 2 := by rw [← I.map_toCotangent_ker] simp theorem toCotangent_eq {x y : I} : I.toCotangent x = I.toCotangent y ↔ (x - y : R) ∈ I ^ 2 := by rw [← sub_eq_zero] exact I.mem_toCotangent_ker theorem toCotangent_eq_zero (x : I) : I.toCotangent x = 0 ↔ (x : R) ∈ I ^ 2 := I.mem_toCotangent_ker theorem toCotangent_surjective : Function.Surjective I.toCotangent := Submodule.mkQ_surjective _ theorem toCotangent_range : LinearMap.range I.toCotangent = ⊤ := Submodule.range_mkQ _ theorem cotangent_subsingleton_iff : Subsingleton I.Cotangent ↔ IsIdempotentElem I := by constructor · intro H refine (pow_two I).symm.trans (le_antisymm (Ideal.pow_le_self two_ne_zero) ?_) exact fun x hx => (I.toCotangent_eq_zero ⟨x, hx⟩).mp (Subsingleton.elim _ _) · exact fun e => ⟨fun x y => Quotient.inductionOn₂' x y fun x y => I.toCotangent_eq.mpr <| ((pow_two I).trans e).symm ▸ I.sub_mem x.prop y.prop⟩
/-- The inclusion map `I ⧸ I ^ 2` to `R ⧸ I ^ 2`. -/ def cotangentToQuotientSquare : I.Cotangent →ₗ[R] R ⧸ I ^ 2 := Submodule.mapQ (I • ⊤) (I ^ 2) I.subtype (by rw [← Submodule.map_le_iff_le_comap, Submodule.map_smul'', Submodule.map_top, Submodule.range_subtype, smul_eq_mul, pow_two] ) theorem to_quotient_square_comp_toCotangent : I.cotangentToQuotientSquare.comp I.toCotangent = (I ^ 2).mkQ.comp (Submodule.subtype I) :=
Mathlib/RingTheory/Ideal/Cotangent.lean
88
96
/- Copyright (c) 2016 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import Mathlib.Data.Set.Defs import Mathlib.Logic.Basic import Mathlib.Logic.ExistsUnique import Mathlib.Logic.Nonempty import Mathlib.Logic.Nontrivial.Defs import Batteries.Tactic.Init import Mathlib.Order.Defs.Unbundled /-! # Miscellaneous function constructions and lemmas -/ open Function universe u v w namespace Function section variable {α β γ : Sort*} {f : α → β} /-- Evaluate a function at an argument. Useful if you want to talk about the partially applied `Function.eval x : (∀ x, β x) → β x`. -/ @[reducible, simp] def eval {β : α → Sort*} (x : α) (f : ∀ x, β x) : β x := f x theorem eval_apply {β : α → Sort*} (x : α) (f : ∀ x, β x) : eval x f = f x := rfl theorem const_def {y : β} : (fun _ : α ↦ y) = const α y := rfl theorem const_injective [Nonempty α] : Injective (const α : β → α → β) := fun _ _ h ↦ let ⟨x⟩ := ‹Nonempty α› congr_fun h x @[simp] theorem const_inj [Nonempty α] {y₁ y₂ : β} : const α y₁ = const α y₂ ↔ y₁ = y₂ := ⟨fun h ↦ const_injective h, fun h ↦ h ▸ rfl⟩ theorem onFun_apply (f : β → β → γ) (g : α → β) (a b : α) : onFun f g a b = f (g a) (g b) := rfl lemma hfunext {α α' : Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : ∀a, β a} {f' : ∀a, β' a} (hα : α = α') (h : ∀a a', HEq a a' → HEq (f a) (f' a')) : HEq f f' := by subst hα have : ∀a, HEq (f a) (f' a) := fun a ↦ h a a (HEq.refl a) have : β = β' := by funext a; exact type_eq_of_heq (this a) subst this apply heq_of_eq funext a exact eq_of_heq (this a) theorem ne_iff {β : α → Sort*} {f₁ f₂ : ∀ a, β a} : f₁ ≠ f₂ ↔ ∃ a, f₁ a ≠ f₂ a := funext_iff.not.trans not_forall lemma funext_iff_of_subsingleton [Subsingleton α] {g : α → β} (x y : α) : f x = g y ↔ f = g := by refine ⟨fun h ↦ funext fun z ↦ ?_, fun h ↦ ?_⟩ · rwa [Subsingleton.elim x z, Subsingleton.elim y z] at h · rw [h, Subsingleton.elim x y] theorem swap_lt {α} [LT α] : swap (· < · : α → α → _) = (· > ·) := rfl theorem swap_le {α} [LE α] : swap (· ≤ · : α → α → _) = (· ≥ ·) := rfl theorem swap_gt {α} [LT α] : swap (· > · : α → α → _) = (· < ·) := rfl theorem swap_ge {α} [LE α] : swap (· ≥ · : α → α → _) = (· ≤ ·) := rfl protected theorem Bijective.injective {f : α → β} (hf : Bijective f) : Injective f := hf.1 protected theorem Bijective.surjective {f : α → β} (hf : Bijective f) : Surjective f := hf.2 theorem not_injective_iff : ¬ Injective f ↔ ∃ a b, f a = f b ∧ a ≠ b := by simp only [Injective, not_forall, exists_prop] /-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then the domain `α` also has decidable equality. -/ protected def Injective.decidableEq [DecidableEq β] (I : Injective f) : DecidableEq α := fun _ _ ↦ decidable_of_iff _ I.eq_iff theorem Injective.of_comp {g : γ → α} (I : Injective (f ∘ g)) : Injective g := fun _ _ h ↦ I <| congr_arg f h @[simp] theorem Injective.of_comp_iff (hf : Injective f) (g : γ → α) : Injective (f ∘ g) ↔ Injective g := ⟨Injective.of_comp, hf.comp⟩ theorem Injective.of_comp_right {g : γ → α} (I : Injective (f ∘ g)) (hg : Surjective g) : Injective f := fun x y h ↦ by obtain ⟨x, rfl⟩ := hg x obtain ⟨y, rfl⟩ := hg y exact congr_arg g (I h) theorem Surjective.bijective₂_of_injective {g : γ → α} (hf : Surjective f) (hg : Surjective g) (I : Injective (f ∘ g)) : Bijective f ∧ Bijective g := ⟨⟨I.of_comp_right hg, hf⟩, I.of_comp, hg⟩ @[simp] theorem Injective.of_comp_iff' (f : α → β) {g : γ → α} (hg : Bijective g) : Injective (f ∘ g) ↔ Injective f := ⟨fun I ↦ I.of_comp_right hg.2, fun h ↦ h.comp hg.injective⟩ theorem Injective.piMap {ι : Sort*} {α β : ι → Sort*} {f : ∀ i, α i → β i} (hf : ∀ i, Injective (f i)) : Injective (Pi.map f) := fun _ _ h ↦ funext fun i ↦ hf i <| congrFun h _ /-- Composition by an injective function on the left is itself injective. -/ theorem Injective.comp_left {g : β → γ} (hg : Injective g) : Injective (g ∘ · : (α → β) → α → γ) := .piMap fun _ ↦ hg theorem injective_comp_left_iff [Nonempty α] {g : β → γ} : Injective (g ∘ · : (α → β) → α → γ) ↔ Injective g := ⟨fun h b₁ b₂ eq ↦ Nonempty.elim ‹_› (congr_fun <| h (a₁ := fun _ ↦ b₁) (a₂ := fun _ ↦ b₂) <| funext fun _ ↦ eq), (·.comp_left)⟩ @[nontriviality] theorem injective_of_subsingleton [Subsingleton α] (f : α → β) : Injective f := fun _ _ _ ↦ Subsingleton.elim _ _ @[nontriviality] theorem bijective_of_subsingleton [Subsingleton α] (f : α → α) : Bijective f := ⟨injective_of_subsingleton f, fun a ↦ ⟨a, Subsingleton.elim ..⟩⟩ lemma Injective.dite (p : α → Prop) [DecidablePred p] {f : {a : α // p a} → β} {f' : {a : α // ¬ p a} → β} (hf : Injective f) (hf' : Injective f') (im_disj : ∀ {x x' : α} {hx : p x} {hx' : ¬ p x'}, f ⟨x, hx⟩ ≠ f' ⟨x', hx'⟩) : Function.Injective (fun x ↦ if h : p x then f ⟨x, h⟩ else f' ⟨x, h⟩) := fun x₁ x₂ h => by dsimp only at h by_cases h₁ : p x₁ <;> by_cases h₂ : p x₂ · rw [dif_pos h₁, dif_pos h₂] at h; injection (hf h) · rw [dif_pos h₁, dif_neg h₂] at h; exact (im_disj h).elim · rw [dif_neg h₁, dif_pos h₂] at h; exact (im_disj h.symm).elim · rw [dif_neg h₁, dif_neg h₂] at h; injection (hf' h) theorem Surjective.of_comp {g : γ → α} (S : Surjective (f ∘ g)) : Surjective f := fun y ↦ let ⟨x, h⟩ := S y ⟨g x, h⟩ @[simp] theorem Surjective.of_comp_iff (f : α → β) {g : γ → α} (hg : Surjective g) : Surjective (f ∘ g) ↔ Surjective f := ⟨Surjective.of_comp, fun h ↦ h.comp hg⟩ theorem Surjective.of_comp_left {g : γ → α} (S : Surjective (f ∘ g)) (hf : Injective f) : Surjective g := fun a ↦ let ⟨c, hc⟩ := S (f a); ⟨c, hf hc⟩ theorem Injective.bijective₂_of_surjective {g : γ → α} (hf : Injective f) (hg : Injective g) (S : Surjective (f ∘ g)) : Bijective f ∧ Bijective g := ⟨⟨hf, S.of_comp⟩, hg, S.of_comp_left hf⟩ @[simp] theorem Surjective.of_comp_iff' (hf : Bijective f) (g : γ → α) : Surjective (f ∘ g) ↔ Surjective g := ⟨fun S ↦ S.of_comp_left hf.1, hf.surjective.comp⟩ instance decidableEqPFun (p : Prop) [Decidable p] (α : p → Type*) [∀ hp, DecidableEq (α hp)] : DecidableEq (∀ hp, α hp) | f, g => decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm protected theorem Surjective.forall (hf : Surjective f) {p : β → Prop} : (∀ y, p y) ↔ ∀ x, p (f x) := ⟨fun h x ↦ h (f x), fun h y ↦ let ⟨x, hx⟩ := hf y hx ▸ h x⟩ protected theorem Surjective.forall₂ (hf : Surjective f) {p : β → β → Prop} : (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂) := hf.forall.trans <| forall_congr' fun _ ↦ hf.forall protected theorem Surjective.forall₃ (hf : Surjective f) {p : β → β → β → Prop} : (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.forall.trans <| forall_congr' fun _ ↦ hf.forall₂ protected theorem Surjective.exists (hf : Surjective f) {p : β → Prop} : (∃ y, p y) ↔ ∃ x, p (f x) := ⟨fun ⟨y, hy⟩ ↦ let ⟨x, hx⟩ := hf y ⟨x, hx.symm ▸ hy⟩, fun ⟨x, hx⟩ ↦ ⟨f x, hx⟩⟩ protected theorem Surjective.exists₂ (hf : Surjective f) {p : β → β → Prop} : (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂) := hf.exists.trans <| exists_congr fun _ ↦ hf.exists protected theorem Surjective.exists₃ (hf : Surjective f) {p : β → β → β → Prop} : (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.exists.trans <| exists_congr fun _ ↦ hf.exists₂ theorem Surjective.injective_comp_right (hf : Surjective f) : Injective fun g : β → γ ↦ g ∘ f := fun _ _ h ↦ funext <| hf.forall.2 <| congr_fun h theorem injective_comp_right_iff_surjective {γ : Type*} [Nontrivial γ] : Injective (fun g : β → γ ↦ g ∘ f) ↔ Surjective f := by refine ⟨not_imp_not.mp fun not_surj inj ↦ not_subsingleton γ ⟨fun c c' ↦ ?_⟩, (·.injective_comp_right)⟩ have ⟨b₀, hb⟩ := not_forall.mp not_surj classical have := inj (a₁ := fun _ ↦ c) (a₂ := (if · = b₀ then c' else c)) ?_ · simpa using congr_fun this b₀ ext a; simp only [comp_apply, if_neg fun h ↦ hb ⟨a, h⟩] protected theorem Surjective.right_cancellable (hf : Surjective f) {g₁ g₂ : β → γ} : g₁ ∘ f = g₂ ∘ f ↔ g₁ = g₂ := hf.injective_comp_right.eq_iff theorem surjective_of_right_cancellable_Prop (h : ∀ g₁ g₂ : β → Prop, g₁ ∘ f = g₂ ∘ f → g₁ = g₂) : Surjective f := injective_comp_right_iff_surjective.mp h theorem bijective_iff_existsUnique (f : α → β) : Bijective f ↔ ∀ b : β, ∃! a : α, f a = b := ⟨fun hf b ↦ let ⟨a, ha⟩ := hf.surjective b ⟨a, ha, fun _ ha' ↦ hf.injective (ha'.trans ha.symm)⟩, fun he ↦ ⟨fun {_a a'} h ↦ (he (f a')).unique h rfl, fun b ↦ (he b).exists⟩⟩ /-- Shorthand for using projection notation with `Function.bijective_iff_existsUnique`. -/ protected theorem Bijective.existsUnique {f : α → β} (hf : Bijective f) (b : β) : ∃! a : α, f a = b := (bijective_iff_existsUnique f).mp hf b theorem Bijective.existsUnique_iff {f : α → β} (hf : Bijective f) {p : β → Prop} : (∃! y, p y) ↔ ∃! x, p (f x) := ⟨fun ⟨y, hpy, hy⟩ ↦ let ⟨x, hx⟩ := hf.surjective y ⟨x, by simpa [hx], fun z (hz : p (f z)) ↦ hf.injective <| hx.symm ▸ hy _ hz⟩, fun ⟨x, hpx, hx⟩ ↦ ⟨f x, hpx, fun y hy ↦ let ⟨z, hz⟩ := hf.surjective y hz ▸ congr_arg f (hx _ (by simpa [hz]))⟩⟩ theorem Bijective.of_comp_iff (f : α → β) {g : γ → α} (hg : Bijective g) : Bijective (f ∘ g) ↔ Bijective f := and_congr (Injective.of_comp_iff' _ hg) (Surjective.of_comp_iff _ hg.surjective) theorem Bijective.of_comp_iff' {f : α → β} (hf : Bijective f) (g : γ → α) : Function.Bijective (f ∘ g) ↔ Function.Bijective g := and_congr (Injective.of_comp_iff hf.injective _) (Surjective.of_comp_iff' hf _) /-- **Cantor's diagonal argument** implies that there are no surjective functions from `α` to `Set α`. -/ theorem cantor_surjective {α} (f : α → Set α) : ¬Surjective f | h => let ⟨D, e⟩ := h {a | ¬ f a a} @iff_not_self (D ∈ f D) <| iff_of_eq <| congr_arg (D ∈ ·) e /-- **Cantor's diagonal argument** implies that there are no injective functions from `Set α` to `α`. -/ theorem cantor_injective {α : Type*} (f : Set α → α) : ¬Injective f | i => cantor_surjective (fun a ↦ {b | ∀ U, a = f U → U b}) <| RightInverse.surjective (fun U ↦ Set.ext fun _ ↦ ⟨fun h ↦ h U rfl, fun h _ e ↦ i e ▸ h⟩) /-- There is no surjection from `α : Type u` into `Type (max u v)`. This theorem demonstrates why `Type : Type` would be inconsistent in Lean. -/ theorem not_surjective_Type {α : Type u} (f : α → Type max u v) : ¬Surjective f := by intro hf let T : Type max u v := Sigma f cases hf (Set T) with | intro U hU => let g : Set T → T := fun s ↦ ⟨U, cast hU.symm s⟩ have hg : Injective g := by intro s t h suffices cast hU (g s).2 = cast hU (g t).2 by simp only [g, cast_cast, cast_eq] at this assumption · congr exact cantor_injective g hg /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def IsPartialInv {α β} (f : α → β) (g : β → Option α) : Prop := ∀ x y, g y = some x ↔ f x = y theorem isPartialInv_left {α β} {f : α → β} {g} (H : IsPartialInv f g) (x) : g (f x) = some x := (H _ _).2 rfl theorem injective_of_isPartialInv {α β} {f : α → β} {g} (H : IsPartialInv f g) : Injective f := fun _ _ h ↦ Option.some.inj <| ((H _ _).2 h).symm.trans ((H _ _).2 rfl) theorem injective_of_isPartialInv_right {α β} {f : α → β} {g} (H : IsPartialInv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂) theorem LeftInverse.comp_eq_id {f : α → β} {g : β → α} (h : LeftInverse f g) : f ∘ g = id := funext h theorem leftInverse_iff_comp {f : α → β} {g : β → α} : LeftInverse f g ↔ f ∘ g = id := ⟨LeftInverse.comp_eq_id, congr_fun⟩ theorem RightInverse.comp_eq_id {f : α → β} {g : β → α} (h : RightInverse f g) : g ∘ f = id := funext h theorem rightInverse_iff_comp {f : α → β} {g : β → α} : RightInverse f g ↔ g ∘ f = id := ⟨RightInverse.comp_eq_id, congr_fun⟩ theorem LeftInverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : LeftInverse f g) (hh : LeftInverse h i) : LeftInverse (h ∘ f) (g ∘ i) := fun a ↦ show h (f (g (i a))) = a by rw [hf (i a), hh a] theorem RightInverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : RightInverse f g) (hh : RightInverse h i) : RightInverse (h ∘ f) (g ∘ i) := LeftInverse.comp hh hf theorem LeftInverse.rightInverse {f : α → β} {g : β → α} (h : LeftInverse g f) : RightInverse f g := h theorem RightInverse.leftInverse {f : α → β} {g : β → α} (h : RightInverse g f) : LeftInverse f g := h theorem LeftInverse.surjective {f : α → β} {g : β → α} (h : LeftInverse f g) : Surjective f := h.rightInverse.surjective theorem RightInverse.injective {f : α → β} {g : β → α} (h : RightInverse f g) : Injective f := h.leftInverse.injective theorem LeftInverse.rightInverse_of_injective {f : α → β} {g : β → α} (h : LeftInverse f g) (hf : Injective f) : RightInverse f g := fun x ↦ hf <| h (f x) theorem LeftInverse.rightInverse_of_surjective {f : α → β} {g : β → α} (h : LeftInverse f g) (hg : Surjective g) : RightInverse f g := fun x ↦ let ⟨y, hy⟩ := hg x; hy ▸ congr_arg g (h y) theorem RightInverse.leftInverse_of_surjective {f : α → β} {g : β → α} : RightInverse f g → Surjective f → LeftInverse f g := LeftInverse.rightInverse_of_surjective theorem RightInverse.leftInverse_of_injective {f : α → β} {g : β → α} : RightInverse f g → Injective g → LeftInverse f g := LeftInverse.rightInverse_of_injective theorem LeftInverse.eq_rightInverse {f : α → β} {g₁ g₂ : β → α} (h₁ : LeftInverse g₁ f) (h₂ : RightInverse g₂ f) : g₁ = g₂ := calc g₁ = g₁ ∘ f ∘ g₂ := by rw [h₂.comp_eq_id, comp_id] _ = g₂ := by rw [← comp_assoc, h₁.comp_eq_id, id_comp] /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ noncomputable def partialInv {α β} (f : α → β) (b : β) : Option α := open scoped Classical in if h : ∃ a, f a = b then some (Classical.choose h) else none theorem partialInv_of_injective {α β} {f : α → β} (I : Injective f) : IsPartialInv f (partialInv f) | a, b => ⟨fun h => open scoped Classical in have hpi : partialInv f b = if h : ∃ a, f a = b then some (Classical.choose h) else none := rfl if h' : ∃ a, f a = b then by rw [hpi, dif_pos h'] at h injection h with h subst h apply Classical.choose_spec h' else by rw [hpi, dif_neg h'] at h; contradiction, fun e => e ▸ have h : ∃ a', f a' = f a := ⟨_, rfl⟩ (dif_pos h).trans (congr_arg _ (I <| Classical.choose_spec h))⟩ theorem partialInv_left {α β} {f : α → β} (I : Injective f) : ∀ x, partialInv f (f x) = some x := isPartialInv_left (partialInv_of_injective I) end section InvFun variable {α β : Sort*} [Nonempty α] {f : α → β} {b : β} /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ -- Explicit Sort so that `α` isn't inferred to be Prop via `exists_prop_decidable` noncomputable def invFun {α : Sort u} {β} [Nonempty α] (f : α → β) : β → α := open scoped Classical in fun y ↦ if h : (∃ x, f x = y) then h.choose else Classical.arbitrary α theorem invFun_eq (h : ∃ a, f a = b) : f (invFun f b) = b := by simp only [invFun, dif_pos h, h.choose_spec] theorem apply_invFun_apply {α β : Type*} {f : α → β} {a : α} : f (@invFun _ _ ⟨a⟩ f (f a)) = f a := @invFun_eq _ _ ⟨a⟩ _ _ ⟨_, rfl⟩ theorem invFun_neg (h : ¬∃ a, f a = b) : invFun f b = Classical.choice ‹_› := dif_neg h theorem invFun_eq_of_injective_of_rightInverse {g : β → α} (hf : Injective f) (hg : RightInverse g f) : invFun f = g := funext fun b ↦ hf (by rw [hg b] exact invFun_eq ⟨g b, hg b⟩) theorem rightInverse_invFun (hf : Surjective f) : RightInverse (invFun f) f := fun b ↦ invFun_eq <| hf b theorem leftInverse_invFun (hf : Injective f) : LeftInverse (invFun f) f := fun b ↦ hf <| invFun_eq ⟨b, rfl⟩ theorem invFun_surjective (hf : Injective f) : Surjective (invFun f) := (leftInverse_invFun hf).surjective theorem invFun_comp (hf : Injective f) : invFun f ∘ f = id := funext <| leftInverse_invFun hf theorem Injective.hasLeftInverse (hf : Injective f) : HasLeftInverse f := ⟨invFun f, leftInverse_invFun hf⟩ theorem injective_iff_hasLeftInverse : Injective f ↔ HasLeftInverse f := ⟨Injective.hasLeftInverse, HasLeftInverse.injective⟩ end InvFun section SurjInv variable {α : Sort u} {β : Sort v} {γ : Sort w} {f : α → β} /-- The inverse of a surjective function. (Unlike `invFun`, this does not require `α` to be inhabited.) -/ noncomputable def surjInv {f : α → β} (h : Surjective f) (b : β) : α := Classical.choose (h b) theorem surjInv_eq (h : Surjective f) (b) : f (surjInv h b) = b := Classical.choose_spec (h b) theorem rightInverse_surjInv (hf : Surjective f) : RightInverse (surjInv hf) f := surjInv_eq hf theorem leftInverse_surjInv (hf : Bijective f) : LeftInverse (surjInv hf.2) f := rightInverse_of_injective_of_leftInverse hf.1 (rightInverse_surjInv hf.2) theorem Surjective.hasRightInverse (hf : Surjective f) : HasRightInverse f := ⟨_, rightInverse_surjInv hf⟩ theorem surjective_iff_hasRightInverse : Surjective f ↔ HasRightInverse f := ⟨Surjective.hasRightInverse, HasRightInverse.surjective⟩ theorem bijective_iff_has_inverse : Bijective f ↔ ∃ g, LeftInverse g f ∧ RightInverse g f := ⟨fun hf ↦ ⟨_, leftInverse_surjInv hf, rightInverse_surjInv hf.2⟩, fun ⟨_, gl, gr⟩ ↦ ⟨gl.injective, gr.surjective⟩⟩
theorem injective_surjInv (h : Surjective f) : Injective (surjInv h) :=
Mathlib/Logic/Function/Basic.lean
441
442
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Kim Morrison -/ import Mathlib.CategoryTheory.Preadditive.Yoneda.Basic import Mathlib.CategoryTheory.Preadditive.Projective.Basic import Mathlib.Algebra.Category.Grp.EpiMono import Mathlib.Algebra.Category.ModuleCat.EpiMono /-! An object is projective iff the preadditive coyoneda functor on it preserves epimorphisms. -/ universe v u open Opposite namespace CategoryTheory variable {C : Type u} [Category.{v} C] section Preadditive variable [Preadditive C] namespace Projective theorem projective_iff_preservesEpimorphisms_preadditiveCoyoneda_obj (P : C) : Projective P ↔ (preadditiveCoyoneda.obj (op P)).PreservesEpimorphisms := by rw [projective_iff_preservesEpimorphisms_coyoneda_obj] refine ⟨fun h : (preadditiveCoyoneda.obj (op P) ⋙ forget AddCommGrp).PreservesEpimorphisms => ?_, ?_⟩ · exact Functor.preservesEpimorphisms_of_preserves_of_reflects (preadditiveCoyoneda.obj (op P)) (forget _) · intro exact (inferInstance : (preadditiveCoyoneda.obj (op P) ⋙ forget _).PreservesEpimorphisms) theorem projective_iff_preservesEpimorphisms_preadditiveCoyonedaObj (P : C) : Projective P ↔ (preadditiveCoyonedaObj P).PreservesEpimorphisms := by
rw [projective_iff_preservesEpimorphisms_coyoneda_obj] refine ⟨fun h : (preadditiveCoyonedaObj P ⋙ forget _).PreservesEpimorphisms => ?_, ?_⟩ · exact Functor.preservesEpimorphisms_of_preserves_of_reflects (preadditiveCoyonedaObj P) (forget _) · intro exact (inferInstance : (preadditiveCoyonedaObj P ⋙ forget _).PreservesEpimorphisms) end Projective
Mathlib/CategoryTheory/Preadditive/Yoneda/Projective.lean
42
50
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro -/ import Mathlib.Algebra.Ring.Defs import Mathlib.Order.RelClasses /-! # Euclidean domains This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise, a slightly more general version is provided which is sometimes called a transfinite Euclidean domain and differs in the fact that the degree function need not take values in `ℕ` but can take values in any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which don't satisfy the classical notion were provided independently by Hiblot and Nagata. ## Main definitions * `EuclideanDomain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances of `Div` and `Mod` are provided, so that one can write `a = b * (a / b) + a % b`. * `gcd`: defines the greatest common divisors of two elements of a Euclidean domain. * `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that `x * a + y * b = gcd a b`. * `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as `a * b / (gcd a b)` ## Main statements See `Algebra.EuclideanDomain.Basic` for most of the theorems about Euclidean domains, including Bézout's lemma. See `Algebra.EuclideanDomain.Instances` for the fact that `ℤ` is a Euclidean domain, as is any field. ## Notation `≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than the degree of `q`. ## Implementation details Instead of working with a valuation, `EuclideanDomain` is implemented with the existence of a well founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute value of `j`. ## References * [Th. Motzkin, *The Euclidean algorithm*][MR32592] * [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*] [MR399081] * [M. Nagata, *On Euclid algorithm*][MR541021] ## Tags Euclidean domain, transfinite Euclidean domain, Bézout's lemma -/ universe u /-- A `EuclideanDomain` is a non-trivial commutative ring with a division and a remainder, satisfying `b * (a / b) + a % b = a`. The definition of a Euclidean domain usually includes a valuation function `R → ℕ`. This definition is slightly generalised to include a well founded relation `r` with the property that `r (a % b) b`, instead of a valuation. -/ class EuclideanDomain (R : Type u) extends CommRing R, Nontrivial R where /-- A division function (denoted `/`) on `R`. This satisfies the property `b * (a / b) + a % b = a`, where `%` denotes `remainder`. -/ protected quotient : R → R → R /-- Division by zero should always give zero by convention. -/ protected quotient_zero : ∀ a, quotient a 0 = 0 /-- A remainder function (denoted `%`) on `R`. This satisfies the property `b * (a / b) + a % b = a`, where `/` denotes `quotient`. -/ protected remainder : R → R → R /-- The property that links the quotient and remainder functions. This allows us to compute GCDs and LCMs. -/ protected quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a /-- A well-founded relation on `R`, satisfying `r (a % b) b`. This ensures that the GCD algorithm always terminates. -/ protected r : R → R → Prop /-- The relation `r` must be well-founded. This ensures that the GCD algorithm always terminates. -/ r_wellFounded : WellFounded r /-- The relation `r` satisfies `r (a % b) b`. -/ protected remainder_lt : ∀ (a) {b}, b ≠ 0 → r (remainder a b) b /-- An additional constraint on `r`. -/ mul_left_not_lt : ∀ (a) {b}, b ≠ 0 → ¬r (a * b) a namespace EuclideanDomain variable {R : Type u} [EuclideanDomain R] /-- Abbreviated notation for the well-founded relation `r` in a Euclidean domain. -/ local infixl:50 " ≺ " => EuclideanDomain.r local instance wellFoundedRelation : WellFoundedRelation R where rel := EuclideanDomain.r wf := r_wellFounded instance isWellFounded : IsWellFounded R (· ≺ ·) where wf := r_wellFounded -- see Note [lower instance priority] instance (priority := 70) : Div R := ⟨EuclideanDomain.quotient⟩ -- see Note [lower instance priority] instance (priority := 70) : Mod R := ⟨EuclideanDomain.remainder⟩ theorem div_add_mod (a b : R) : b * (a / b) + a % b = a := EuclideanDomain.quotient_mul_add_remainder_eq _ _ theorem mod_add_div (a b : R) : a % b + b * (a / b) = a := (add_comm _ _).trans (div_add_mod _ _) theorem mod_add_div' (m k : R) : m % k + m / k * k = m := by rw [mul_comm] exact mod_add_div _ _ theorem div_add_mod' (m k : R) : m / k * k + m % k = m := by rw [mul_comm] exact div_add_mod _ _ theorem mod_lt : ∀ (a) {b : R}, b ≠ 0 → a % b ≺ b := EuclideanDomain.remainder_lt theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬a * b ≺ b := by rw [mul_comm] exact mul_left_not_lt b h @[simp] theorem mod_zero (a : R) : a % 0 = a := by simpa only [zero_mul, zero_add] using div_add_mod a 0 theorem lt_one (a : R) : a ≺ (1 : R) → a = 0 := haveI := Classical.dec not_imp_not.1 fun h => by simpa only [one_mul] using mul_left_not_lt 1 h
@[simp] theorem div_zero (a : R) : a / 0 = 0 := EuclideanDomain.quotient_zero a
Mathlib/Algebra/EuclideanDomain/Defs.lean
141
144
/- Copyright (c) 2021 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Order.Module.Algebra import Mathlib.Algebra.Ring.Subring.Units import Mathlib.LinearAlgebra.LinearIndependent.Defs import Mathlib.Tactic.LinearCombination import Mathlib.Tactic.Module import Mathlib.Tactic.Positivity.Basic /-! # Rays in modules This file defines rays in modules. ## Main definitions * `SameRay`: two vectors belong to the same ray if they are proportional with a nonnegative coefficient. * `Module.Ray` is a type for the equivalence class of nonzero vectors in a module with some common positive multiple. -/ noncomputable section section StrictOrderedCommSemiring -- TODO: remove `[IsStrictOrderedRing R]` and `@[nolint unusedArguments]`. /-- Two vectors are in the same ray if either one of them is zero or some positive multiples of them are equal (in the typical case over a field, this means one of them is a nonnegative multiple of the other). -/ @[nolint unusedArguments] def SameRay (R : Type*) [CommSemiring R] [PartialOrder R] [IsStrictOrderedRing R] {M : Type*} [AddCommMonoid M] [Module R M] (v₁ v₂ : M) : Prop := v₁ = 0 ∨ v₂ = 0 ∨ ∃ r₁ r₂ : R, 0 < r₁ ∧ 0 < r₂ ∧ r₁ • v₁ = r₂ • v₂ variable {R : Type*} [CommSemiring R] [PartialOrder R] [IsStrictOrderedRing R] variable {M : Type*} [AddCommMonoid M] [Module R M] variable {N : Type*} [AddCommMonoid N] [Module R N] variable (ι : Type*) [DecidableEq ι] namespace SameRay variable {x y z : M} @[simp] theorem zero_left (y : M) : SameRay R 0 y := Or.inl rfl @[simp] theorem zero_right (x : M) : SameRay R x 0 := Or.inr <| Or.inl rfl @[nontriviality] theorem of_subsingleton [Subsingleton M] (x y : M) : SameRay R x y := by rw [Subsingleton.elim x 0] exact zero_left _ @[nontriviality] theorem of_subsingleton' [Subsingleton R] (x y : M) : SameRay R x y := haveI := Module.subsingleton R M of_subsingleton x y /-- `SameRay` is reflexive. -/ @[refl] theorem refl (x : M) : SameRay R x x := by nontriviality R exact Or.inr (Or.inr <| ⟨1, 1, zero_lt_one, zero_lt_one, rfl⟩) protected theorem rfl : SameRay R x x := refl _ /-- `SameRay` is symmetric. -/ @[symm] theorem symm (h : SameRay R x y) : SameRay R y x := (or_left_comm.1 h).imp_right <| Or.imp_right fun ⟨r₁, r₂, h₁, h₂, h⟩ => ⟨r₂, r₁, h₂, h₁, h.symm⟩ /-- If `x` and `y` are nonzero vectors on the same ray, then there exist positive numbers `r₁ r₂` such that `r₁ • x = r₂ • y`. -/ theorem exists_pos (h : SameRay R x y) (hx : x ≠ 0) (hy : y ≠ 0) : ∃ r₁ r₂ : R, 0 < r₁ ∧ 0 < r₂ ∧ r₁ • x = r₂ • y := (h.resolve_left hx).resolve_left hy theorem sameRay_comm : SameRay R x y ↔ SameRay R y x := ⟨SameRay.symm, SameRay.symm⟩ /-- `SameRay` is transitive unless the vector in the middle is zero and both other vectors are nonzero. -/ theorem trans (hxy : SameRay R x y) (hyz : SameRay R y z) (hy : y = 0 → x = 0 ∨ z = 0) : SameRay R x z := by rcases eq_or_ne x 0 with (rfl | hx); · exact zero_left z rcases eq_or_ne z 0 with (rfl | hz); · exact zero_right x rcases eq_or_ne y 0 with (rfl | hy) · exact (hy rfl).elim (fun h => (hx h).elim) fun h => (hz h).elim rcases hxy.exists_pos hx hy with ⟨r₁, r₂, hr₁, hr₂, h₁⟩ rcases hyz.exists_pos hy hz with ⟨r₃, r₄, hr₃, hr₄, h₂⟩ refine Or.inr (Or.inr <| ⟨r₃ * r₁, r₂ * r₄, mul_pos hr₃ hr₁, mul_pos hr₂ hr₄, ?_⟩) rw [mul_smul, mul_smul, h₁, ← h₂, smul_comm] variable {S : Type*} [CommSemiring S] [PartialOrder S] [Algebra S R] [Module S M] [SMulPosMono S R] [IsScalarTower S R M] {a : S} /-- A vector is in the same ray as a nonnegative multiple of itself. -/ lemma sameRay_nonneg_smul_right (v : M) (h : 0 ≤ a) : SameRay R v (a • v) := by obtain h | h := (algebraMap_nonneg R h).eq_or_gt · rw [← algebraMap_smul R a v, h, zero_smul] exact zero_right _ · refine Or.inr <| Or.inr ⟨algebraMap S R a, 1, h, by nontriviality R; exact zero_lt_one, ?_⟩ module /-- A nonnegative multiple of a vector is in the same ray as that vector. -/ lemma sameRay_nonneg_smul_left (v : M) (ha : 0 ≤ a) : SameRay R (a • v) v := (sameRay_nonneg_smul_right v ha).symm /-- A vector is in the same ray as a positive multiple of itself. -/ lemma sameRay_pos_smul_right (v : M) (ha : 0 < a) : SameRay R v (a • v) := sameRay_nonneg_smul_right v ha.le /-- A positive multiple of a vector is in the same ray as that vector. -/ lemma sameRay_pos_smul_left (v : M) (ha : 0 < a) : SameRay R (a • v) v := sameRay_nonneg_smul_left v ha.le /-- A vector is in the same ray as a nonnegative multiple of one it is in the same ray as. -/ lemma nonneg_smul_right (h : SameRay R x y) (ha : 0 ≤ a) : SameRay R x (a • y) := h.trans (sameRay_nonneg_smul_right y ha) fun hy => Or.inr <| by rw [hy, smul_zero] /-- A nonnegative multiple of a vector is in the same ray as one it is in the same ray as. -/ lemma nonneg_smul_left (h : SameRay R x y) (ha : 0 ≤ a) : SameRay R (a • x) y := (h.symm.nonneg_smul_right ha).symm /-- A vector is in the same ray as a positive multiple of one it is in the same ray as. -/ theorem pos_smul_right (h : SameRay R x y) (ha : 0 < a) : SameRay R x (a • y) := h.nonneg_smul_right ha.le /-- A positive multiple of a vector is in the same ray as one it is in the same ray as. -/ theorem pos_smul_left (h : SameRay R x y) (hr : 0 < a) : SameRay R (a • x) y := h.nonneg_smul_left hr.le /-- If two vectors are on the same ray then they remain so after applying a linear map. -/ theorem map (f : M →ₗ[R] N) (h : SameRay R x y) : SameRay R (f x) (f y) := (h.imp fun hx => by rw [hx, map_zero]) <| Or.imp (fun hy => by rw [hy, map_zero]) fun ⟨r₁, r₂, hr₁, hr₂, h⟩ => ⟨r₁, r₂, hr₁, hr₂, by rw [← f.map_smul, ← f.map_smul, h]⟩ /-- The images of two vectors under an injective linear map are on the same ray if and only if the original vectors are on the same ray. -/ theorem _root_.Function.Injective.sameRay_map_iff {F : Type*} [FunLike F M N] [LinearMapClass F R M N] {f : F} (hf : Function.Injective f) : SameRay R (f x) (f y) ↔ SameRay R x y := by simp only [SameRay, map_zero, ← hf.eq_iff, map_smul] /-- The images of two vectors under a linear equivalence are on the same ray if and only if the original vectors are on the same ray. -/ @[simp] theorem sameRay_map_iff (e : M ≃ₗ[R] N) : SameRay R (e x) (e y) ↔ SameRay R x y := Function.Injective.sameRay_map_iff (EquivLike.injective e) /-- If two vectors are on the same ray then both scaled by the same action are also on the same ray. -/ theorem smul {S : Type*} [Monoid S] [DistribMulAction S M] [SMulCommClass R S M] (h : SameRay R x y) (s : S) : SameRay R (s • x) (s • y) := h.map (s • (LinearMap.id : M →ₗ[R] M)) /-- If `x` and `y` are on the same ray as `z`, then so is `x + y`. -/ theorem add_left (hx : SameRay R x z) (hy : SameRay R y z) : SameRay R (x + y) z := by rcases eq_or_ne x 0 with (rfl | hx₀); · rwa [zero_add] rcases eq_or_ne y 0 with (rfl | hy₀); · rwa [add_zero] rcases eq_or_ne z 0 with (rfl | hz₀); · apply zero_right rcases hx.exists_pos hx₀ hz₀ with ⟨rx, rz₁, hrx, hrz₁, Hx⟩ rcases hy.exists_pos hy₀ hz₀ with ⟨ry, rz₂, hry, hrz₂, Hy⟩ refine Or.inr (Or.inr ⟨rx * ry, ry * rz₁ + rx * rz₂, mul_pos hrx hry, ?_, ?_⟩) · positivity · convert congr(ry • $Hx + rx • $Hy) using 1 <;> module /-- If `y` and `z` are on the same ray as `x`, then so is `y + z`. -/ theorem add_right (hy : SameRay R x y) (hz : SameRay R x z) : SameRay R x (y + z) := (hy.symm.add_left hz.symm).symm end SameRay set_option linter.unusedVariables false in /-- Nonzero vectors, as used to define rays. This type depends on an unused argument `R` so that `RayVector.Setoid` can be an instance. -/ @[nolint unusedArguments] def RayVector (R M : Type*) [Zero M] := { v : M // v ≠ 0 } instance RayVector.coe [Zero M] : CoeOut (RayVector R M) M where coe := Subtype.val instance {R M : Type*} [Zero M] [Nontrivial M] : Nonempty (RayVector R M) := let ⟨x, hx⟩ := exists_ne (0 : M) ⟨⟨x, hx⟩⟩ variable (R M) /-- The setoid of the `SameRay` relation for the subtype of nonzero vectors. -/ instance RayVector.Setoid : Setoid (RayVector R M) where r x y := SameRay R (x : M) y iseqv := ⟨fun _ => SameRay.refl _, fun h => h.symm, by intros x y z hxy hyz exact hxy.trans hyz fun hy => (y.2 hy).elim⟩ /-- A ray (equivalence class of nonzero vectors with common positive multiples) in a module. -/ def Module.Ray := Quotient (RayVector.Setoid R M) variable {R M} /-- Equivalence of nonzero vectors, in terms of `SameRay`. -/ theorem equiv_iff_sameRay {v₁ v₂ : RayVector R M} : v₁ ≈ v₂ ↔ SameRay R (v₁ : M) v₂ := Iff.rfl variable (R) /-- The ray given by a nonzero vector. -/ def rayOfNeZero (v : M) (h : v ≠ 0) : Module.Ray R M := ⟦⟨v, h⟩⟧ /-- An induction principle for `Module.Ray`, used as `induction x using Module.Ray.ind`. -/ theorem Module.Ray.ind {C : Module.Ray R M → Prop} (h : ∀ (v) (hv : v ≠ 0), C (rayOfNeZero R v hv)) (x : Module.Ray R M) : C x := Quotient.ind (Subtype.rec <| h) x variable {R} instance [Nontrivial M] : Nonempty (Module.Ray R M) := Nonempty.map Quotient.mk' inferInstance /-- The rays given by two nonzero vectors are equal if and only if those vectors satisfy `SameRay`. -/ theorem ray_eq_iff {v₁ v₂ : M} (hv₁ : v₁ ≠ 0) (hv₂ : v₂ ≠ 0) : rayOfNeZero R _ hv₁ = rayOfNeZero R _ hv₂ ↔ SameRay R v₁ v₂ := Quotient.eq' /-- The ray given by a positive multiple of a nonzero vector. -/ @[simp] theorem ray_pos_smul {v : M} (h : v ≠ 0) {r : R} (hr : 0 < r) (hrv : r • v ≠ 0) : rayOfNeZero R (r • v) hrv = rayOfNeZero R v h := (ray_eq_iff _ _).2 <| SameRay.sameRay_pos_smul_left v hr /-- An equivalence between modules implies an equivalence between ray vectors. -/ def RayVector.mapLinearEquiv (e : M ≃ₗ[R] N) : RayVector R M ≃ RayVector R N := Equiv.subtypeEquiv e.toEquiv fun _ => e.map_ne_zero_iff.symm /-- An equivalence between modules implies an equivalence between rays. -/ def Module.Ray.map (e : M ≃ₗ[R] N) : Module.Ray R M ≃ Module.Ray R N := Quotient.congr (RayVector.mapLinearEquiv e) fun _ _=> (SameRay.sameRay_map_iff _).symm @[simp] theorem Module.Ray.map_apply (e : M ≃ₗ[R] N) (v : M) (hv : v ≠ 0) : Module.Ray.map e (rayOfNeZero _ v hv) = rayOfNeZero _ (e v) (e.map_ne_zero_iff.2 hv) := rfl @[simp] theorem Module.Ray.map_refl : (Module.Ray.map <| LinearEquiv.refl R M) = Equiv.refl _ := Equiv.ext <| Module.Ray.ind R fun _ _ => rfl @[simp] theorem Module.Ray.map_symm (e : M ≃ₗ[R] N) : (Module.Ray.map e).symm = Module.Ray.map e.symm := rfl section Action variable {G : Type*} [Group G] [DistribMulAction G M] /-- Any invertible action preserves the non-zeroness of ray vectors. This is primarily of interest when `G = Rˣ` -/ instance {R : Type*} : MulAction G (RayVector R M) where smul r := Subtype.map (r • ·) fun _ => (smul_ne_zero_iff_ne _).2 mul_smul a b _ := Subtype.ext <| mul_smul a b _ one_smul _ := Subtype.ext <| one_smul _ _ variable [SMulCommClass R G M] /-- Any invertible action preserves the non-zeroness of rays. This is primarily of interest when `G = Rˣ` -/ instance : MulAction G (Module.Ray R M) where smul r := Quotient.map (r • ·) fun _ _ h => h.smul _ mul_smul a b := Quotient.ind fun _ => congr_arg Quotient.mk' <| mul_smul a b _ one_smul := Quotient.ind fun _ => congr_arg Quotient.mk' <| one_smul _ _ /-- The action via `LinearEquiv.apply_distribMulAction` corresponds to `Module.Ray.map`. -/ @[simp] theorem Module.Ray.linearEquiv_smul_eq_map (e : M ≃ₗ[R] M) (v : Module.Ray R M) : e • v = Module.Ray.map e v := rfl @[simp] theorem smul_rayOfNeZero (g : G) (v : M) (hv) : g • rayOfNeZero R v hv = rayOfNeZero R (g • v) ((smul_ne_zero_iff_ne _).2 hv) := rfl end Action namespace Module.Ray /-- Scaling by a positive unit is a no-op. -/ theorem units_smul_of_pos (u : Rˣ) (hu : 0 < (u : R)) (v : Module.Ray R M) : u • v = v := by induction v using Module.Ray.ind rw [smul_rayOfNeZero, ray_eq_iff] exact SameRay.sameRay_pos_smul_left _ hu /-- An arbitrary `RayVector` giving a ray. -/ def someRayVector (x : Module.Ray R M) : RayVector R M := Quotient.out x /-- The ray of `someRayVector`. -/ @[simp] theorem someRayVector_ray (x : Module.Ray R M) : (⟦x.someRayVector⟧ : Module.Ray R M) = x := Quotient.out_eq _ /-- An arbitrary nonzero vector giving a ray. -/ def someVector (x : Module.Ray R M) : M := x.someRayVector /-- `someVector` is nonzero. -/ @[simp] theorem someVector_ne_zero (x : Module.Ray R M) : x.someVector ≠ 0 := x.someRayVector.property /-- The ray of `someVector`. -/ @[simp] theorem someVector_ray (x : Module.Ray R M) : rayOfNeZero R _ x.someVector_ne_zero = x := (congr_arg _ (Subtype.coe_eta _ _) :).trans x.out_eq end Module.Ray end StrictOrderedCommSemiring section StrictOrderedCommRing variable {R : Type*} [CommRing R] [PartialOrder R] [IsStrictOrderedRing R] variable {M N : Type*} [AddCommGroup M] [AddCommGroup N] [Module R M] [Module R N] {x y : M} /-- `SameRay.neg` as an `iff`. -/ @[simp] theorem sameRay_neg_iff : SameRay R (-x) (-y) ↔ SameRay R x y := by simp only [SameRay, neg_eq_zero, smul_neg, neg_inj] alias ⟨SameRay.of_neg, SameRay.neg⟩ := sameRay_neg_iff theorem sameRay_neg_swap : SameRay R (-x) y ↔ SameRay R x (-y) := by rw [← sameRay_neg_iff, neg_neg] theorem eq_zero_of_sameRay_neg_smul_right [NoZeroSMulDivisors R M] {r : R} (hr : r < 0) (h : SameRay R x (r • x)) : x = 0 := by rcases h with (rfl | h₀ | ⟨r₁, r₂, hr₁, hr₂, h⟩) · rfl · simpa [hr.ne] using h₀ · rw [← sub_eq_zero, smul_smul, ← sub_smul, smul_eq_zero] at h refine h.resolve_left (ne_of_gt <| sub_pos.2 ?_) exact (mul_neg_of_pos_of_neg hr₂ hr).trans hr₁ /-- If a vector is in the same ray as its negation, that vector is zero. -/ theorem eq_zero_of_sameRay_self_neg [NoZeroSMulDivisors R M] (h : SameRay R x (-x)) : x = 0 := by nontriviality M; haveI : Nontrivial R := Module.nontrivial R M refine eq_zero_of_sameRay_neg_smul_right (neg_lt_zero.2 (zero_lt_one' R)) ?_ rwa [neg_one_smul] namespace RayVector /-- Negating a nonzero vector. -/ instance {R : Type*} : Neg (RayVector R M) := ⟨fun v => ⟨-v, neg_ne_zero.2 v.prop⟩⟩ /-- Negating a nonzero vector commutes with coercion to the underlying module. -/ @[simp, norm_cast] theorem coe_neg {R : Type*} (v : RayVector R M) : ↑(-v) = -(v : M) := rfl /-- Negating a nonzero vector twice produces the original vector. -/ instance {R : Type*} : InvolutiveNeg (RayVector R M) where neg := Neg.neg neg_neg v := by rw [Subtype.ext_iff, coe_neg, coe_neg, neg_neg] /-- If two nonzero vectors are equivalent, so are their negations. -/ @[simp] theorem equiv_neg_iff {v₁ v₂ : RayVector R M} : -v₁ ≈ -v₂ ↔ v₁ ≈ v₂ := sameRay_neg_iff end RayVector variable (R) /-- Negating a ray. -/ instance : Neg (Module.Ray R M) := ⟨Quotient.map (fun v => -v) fun _ _ => RayVector.equiv_neg_iff.2⟩ /-- The ray given by the negation of a nonzero vector. -/ @[simp] theorem neg_rayOfNeZero (v : M) (h : v ≠ 0) : -rayOfNeZero R _ h = rayOfNeZero R (-v) (neg_ne_zero.2 h) := rfl namespace Module.Ray variable {R} /-- Negating a ray twice produces the original ray. -/ instance : InvolutiveNeg (Module.Ray R M) where neg := Neg.neg neg_neg x := by apply ind R (by simp) x -- Quotient.ind (fun a => congr_arg Quotient.mk' <| neg_neg _) x /-- A ray does not equal its own negation. -/ theorem ne_neg_self [NoZeroSMulDivisors R M] (x : Module.Ray R M) : x ≠ -x := by induction x using Module.Ray.ind with | h x hx => rw [neg_rayOfNeZero, Ne, ray_eq_iff] exact mt eq_zero_of_sameRay_self_neg hx theorem neg_units_smul (u : Rˣ) (v : Module.Ray R M) : -u • v = -(u • v) := by induction v using Module.Ray.ind simp only [smul_rayOfNeZero, Units.smul_def, Units.val_neg, neg_smul, neg_rayOfNeZero] /-- Scaling by a negative unit is negation. -/ theorem units_smul_of_neg (u : Rˣ) (hu : (u : R) < 0) (v : Module.Ray R M) : u • v = -v := by rw [← neg_inj, neg_neg, ← neg_units_smul, units_smul_of_pos] rwa [Units.val_neg, Right.neg_pos_iff] @[simp] protected theorem map_neg (f : M ≃ₗ[R] N) (v : Module.Ray R M) : map f (-v) = -map f v := by induction v using Module.Ray.ind with | h g hg => simp end Module.Ray end StrictOrderedCommRing section LinearOrderedCommRing variable {R : Type*} [CommRing R] [LinearOrder R] [IsStrictOrderedRing R] variable {M : Type*} [AddCommGroup M] [Module R M] /-- `SameRay` follows from membership of `MulAction.orbit` for the `Units.posSubgroup`. -/ theorem sameRay_of_mem_orbit {v₁ v₂ : M} (h : v₁ ∈ MulAction.orbit (Units.posSubgroup R) v₂) : SameRay R v₁ v₂ := by rcases h with ⟨⟨r, hr : 0 < r.1⟩, rfl : r • v₂ = v₁⟩ exact SameRay.sameRay_pos_smul_left _ hr /-- Scaling by an inverse unit is the same as scaling by itself. -/ @[simp] theorem units_inv_smul (u : Rˣ) (v : Module.Ray R M) : u⁻¹ • v = u • v := have := mul_self_pos.2 u.ne_zero calc u⁻¹ • v = (u * u) • u⁻¹ • v := Eq.symm <| (u⁻¹ • v).units_smul_of_pos _ (by exact this) _ = u • v := by rw [mul_smul, smul_inv_smul] section variable [NoZeroSMulDivisors R M] @[simp] theorem sameRay_smul_right_iff {v : M} {r : R} : SameRay R v (r • v) ↔ 0 ≤ r ∨ v = 0 := ⟨fun hrv => or_iff_not_imp_left.2 fun hr => eq_zero_of_sameRay_neg_smul_right (not_le.1 hr) hrv, or_imp.2 ⟨SameRay.sameRay_nonneg_smul_right v, fun h => h.symm ▸ SameRay.zero_left _⟩⟩ /-- A nonzero vector is in the same ray as a multiple of itself if and only if that multiple is positive. -/ theorem sameRay_smul_right_iff_of_ne {v : M} (hv : v ≠ 0) {r : R} (hr : r ≠ 0) : SameRay R v (r • v) ↔ 0 < r := by simp only [sameRay_smul_right_iff, hv, or_false, hr.symm.le_iff_lt] @[simp] theorem sameRay_smul_left_iff {v : M} {r : R} : SameRay R (r • v) v ↔ 0 ≤ r ∨ v = 0 := SameRay.sameRay_comm.trans sameRay_smul_right_iff /-- A multiple of a nonzero vector is in the same ray as that vector if and only if that multiple is positive. -/ theorem sameRay_smul_left_iff_of_ne {v : M} (hv : v ≠ 0) {r : R} (hr : r ≠ 0) : SameRay R (r • v) v ↔ 0 < r := SameRay.sameRay_comm.trans (sameRay_smul_right_iff_of_ne hv hr) @[simp] theorem sameRay_neg_smul_right_iff {v : M} {r : R} : SameRay R (-v) (r • v) ↔ r ≤ 0 ∨ v = 0 := by rw [← sameRay_neg_iff, neg_neg, ← neg_smul, sameRay_smul_right_iff, neg_nonneg] theorem sameRay_neg_smul_right_iff_of_ne {v : M} {r : R} (hv : v ≠ 0) (hr : r ≠ 0) : SameRay R (-v) (r • v) ↔ r < 0 := by simp only [sameRay_neg_smul_right_iff, hv, or_false, hr.le_iff_lt] @[simp] theorem sameRay_neg_smul_left_iff {v : M} {r : R} : SameRay R (r • v) (-v) ↔ r ≤ 0 ∨ v = 0 := SameRay.sameRay_comm.trans sameRay_neg_smul_right_iff theorem sameRay_neg_smul_left_iff_of_ne {v : M} {r : R} (hv : v ≠ 0) (hr : r ≠ 0) : SameRay R (r • v) (-v) ↔ r < 0 := SameRay.sameRay_comm.trans <| sameRay_neg_smul_right_iff_of_ne hv hr @[simp] theorem units_smul_eq_self_iff {u : Rˣ} {v : Module.Ray R M} : u • v = v ↔ 0 < (u : R) := by induction v using Module.Ray.ind with | h v hv => simp only [smul_rayOfNeZero, ray_eq_iff, Units.smul_def, sameRay_smul_left_iff_of_ne hv u.ne_zero] @[simp] theorem units_smul_eq_neg_iff {u : Rˣ} {v : Module.Ray R M} : u • v = -v ↔ u.1 < 0 := by rw [← neg_inj, neg_neg, ← Module.Ray.neg_units_smul, units_smul_eq_self_iff, Units.val_neg, neg_pos] /-- Two vectors are in the same ray, or the first is in the same ray as the negation of the second, if and only if they are not linearly independent. -/ theorem sameRay_or_sameRay_neg_iff_not_linearIndependent {x y : M} : SameRay R x y ∨ SameRay R x (-y) ↔ ¬LinearIndependent R ![x, y] := by by_cases hx : x = 0; · simpa [hx] using fun h : LinearIndependent R ![0, y] => h.ne_zero 0 rfl by_cases hy : y = 0; · simpa [hy] using fun h : LinearIndependent R ![x, 0] => h.ne_zero 1 rfl simp_rw [Fintype.not_linearIndependent_iff] refine ⟨fun h => ?_, fun h => ?_⟩ · rcases h with ((hx0 | hy0 | ⟨r₁, r₂, hr₁, _, h⟩) | (hx0 | hy0 | ⟨r₁, r₂, hr₁, _, h⟩)) · exact False.elim (hx hx0) · exact False.elim (hy hy0) · refine ⟨![r₁, -r₂], ?_⟩ rw [Fin.sum_univ_two, Fin.exists_fin_two] simp [h, hr₁.ne.symm] · exact False.elim (hx hx0) · exact False.elim (hy (neg_eq_zero.1 hy0)) · refine ⟨![r₁, r₂], ?_⟩ rw [Fin.sum_univ_two, Fin.exists_fin_two] simp [h, hr₁.ne.symm] · rcases h with ⟨m, hm, hmne⟩ rw [Fin.sum_univ_two, add_eq_zero_iff_eq_neg] at hm dsimp only [Matrix.cons_val] at hm rcases lt_trichotomy (m 0) 0 with (hm0 | hm0 | hm0) <;> rcases lt_trichotomy (m 1) 0 with (hm1 | hm1 | hm1) · refine Or.inr (Or.inr (Or.inr ⟨-m 0, -m 1, Left.neg_pos_iff.2 hm0, Left.neg_pos_iff.2 hm1, ?_⟩)) linear_combination (norm := module) -hm · exfalso simp [hm1, hx, hm0.ne] at hm · refine Or.inl (Or.inr (Or.inr ⟨-m 0, m 1, Left.neg_pos_iff.2 hm0, hm1, ?_⟩)) linear_combination (norm := module) -hm · exfalso simp [hm0, hy, hm1.ne] at hm · rw [Fin.exists_fin_two] at hmne exact False.elim (not_and_or.2 hmne ⟨hm0, hm1⟩) · exfalso simp [hm0, hy, hm1.ne.symm] at hm · refine Or.inl (Or.inr (Or.inr ⟨m 0, -m 1, hm0, Left.neg_pos_iff.2 hm1, ?_⟩)) rwa [neg_smul] · exfalso simp [hm1, hx, hm0.ne.symm] at hm · refine Or.inr (Or.inr (Or.inr ⟨m 0, m 1, hm0, hm1, ?_⟩)) rwa [smul_neg] /-- Two vectors are in the same ray, or they are nonzero and the first is in the same ray as the negation of the second, if and only if they are not linearly independent. -/ theorem sameRay_or_ne_zero_and_sameRay_neg_iff_not_linearIndependent {x y : M} : SameRay R x y ∨ x ≠ 0 ∧ y ≠ 0 ∧ SameRay R x (-y) ↔ ¬LinearIndependent R ![x, y] := by rw [← sameRay_or_sameRay_neg_iff_not_linearIndependent] by_cases hx : x = 0; · simp [hx] by_cases hy : y = 0 <;> simp [hx, hy] end end LinearOrderedCommRing namespace SameRay variable {R : Type*} [Field R] [LinearOrder R] [IsStrictOrderedRing R] variable {M : Type*} [AddCommGroup M] [Module R M] {x y v₁ v₂ : M} theorem exists_pos_left (h : SameRay R x y) (hx : x ≠ 0) (hy : y ≠ 0) : ∃ r : R, 0 < r ∧ r • x = y := let ⟨r₁, r₂, hr₁, hr₂, h⟩ := h.exists_pos hx hy ⟨r₂⁻¹ * r₁, mul_pos (inv_pos.2 hr₂) hr₁, by rw [mul_smul, h, inv_smul_smul₀ hr₂.ne']⟩ theorem exists_pos_right (h : SameRay R x y) (hx : x ≠ 0) (hy : y ≠ 0) : ∃ r : R, 0 < r ∧ x = r • y := (h.symm.exists_pos_left hy hx).imp fun _ => And.imp_right Eq.symm /-- If a vector `v₂` is on the same ray as a nonzero vector `v₁`, then it is equal to `c • v₁` for some nonnegative `c`. -/ theorem exists_nonneg_left (h : SameRay R x y) (hx : x ≠ 0) : ∃ r : R, 0 ≤ r ∧ r • x = y := by obtain rfl | hy := eq_or_ne y 0 · exact ⟨0, le_rfl, zero_smul _ _⟩ · exact (h.exists_pos_left hx hy).imp fun _ => And.imp_left le_of_lt /-- If a vector `v₁` is on the same ray as a nonzero vector `v₂`, then it is equal to `c • v₂` for some nonnegative `c`. -/ theorem exists_nonneg_right (h : SameRay R x y) (hy : y ≠ 0) : ∃ r : R, 0 ≤ r ∧ x = r • y := (h.symm.exists_nonneg_left hy).imp fun _ => And.imp_right Eq.symm /-- If vectors `v₁` and `v₂` are on the same ray, then for some nonnegative `a b`, `a + b = 1`, we have `v₁ = a • (v₁ + v₂)` and `v₂ = b • (v₁ + v₂)`. -/ theorem exists_eq_smul_add (h : SameRay R v₁ v₂) : ∃ a b : R, 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ v₁ = a • (v₁ + v₂) ∧ v₂ = b • (v₁ + v₂) := by rcases h with (rfl | rfl | ⟨r₁, r₂, h₁, h₂, H⟩) · use 0, 1 simp · use 1, 0 simp · have h₁₂ : 0 < r₁ + r₂ := add_pos h₁ h₂ refine ⟨r₂ / (r₁ + r₂), r₁ / (r₁ + r₂), div_nonneg h₂.le h₁₂.le, div_nonneg h₁.le h₁₂.le, ?_, ?_, ?_⟩ · rw [← add_div, add_comm, div_self h₁₂.ne'] · rw [div_eq_inv_mul, mul_smul, smul_add, ← H, ← add_smul, add_comm r₂, inv_smul_smul₀ h₁₂.ne'] · rw [div_eq_inv_mul, mul_smul, smul_add, H, ← add_smul, add_comm r₂, inv_smul_smul₀ h₁₂.ne'] /-- If vectors `v₁` and `v₂` are on the same ray, then they are nonnegative multiples of the same vector. Actually, this vector can be assumed to be `v₁ + v₂`, see `SameRay.exists_eq_smul_add`. -/ theorem exists_eq_smul (h : SameRay R v₁ v₂) : ∃ (u : M) (a b : R), 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ v₁ = a • u ∧ v₂ = b • u := ⟨v₁ + v₂, h.exists_eq_smul_add⟩ end SameRay section LinearOrderedField variable {R : Type*} [Field R] [LinearOrder R] [IsStrictOrderedRing R] variable {M : Type*} [AddCommGroup M] [Module R M] {x y : M} theorem exists_pos_left_iff_sameRay (hx : x ≠ 0) (hy : y ≠ 0) : (∃ r : R, 0 < r ∧ r • x = y) ↔ SameRay R x y := by refine ⟨fun h => ?_, fun h => h.exists_pos_left hx hy⟩ rcases h with ⟨r, hr, rfl⟩ exact SameRay.sameRay_pos_smul_right x hr theorem exists_pos_left_iff_sameRay_and_ne_zero (hx : x ≠ 0) : (∃ r : R, 0 < r ∧ r • x = y) ↔ SameRay R x y ∧ y ≠ 0 := by constructor · rintro ⟨r, hr, rfl⟩ simp [hx, hr.le, hr.ne'] · rintro ⟨hxy, hy⟩ exact (exists_pos_left_iff_sameRay hx hy).2 hxy theorem exists_nonneg_left_iff_sameRay (hx : x ≠ 0) : (∃ r : R, 0 ≤ r ∧ r • x = y) ↔ SameRay R x y := by refine ⟨fun h => ?_, fun h => h.exists_nonneg_left hx⟩ rcases h with ⟨r, hr, rfl⟩ exact SameRay.sameRay_nonneg_smul_right x hr theorem exists_pos_right_iff_sameRay (hx : x ≠ 0) (hy : y ≠ 0) : (∃ r : R, 0 < r ∧ x = r • y) ↔ SameRay R x y := by rw [SameRay.sameRay_comm] simp_rw [eq_comm (a := x)] exact exists_pos_left_iff_sameRay hy hx theorem exists_pos_right_iff_sameRay_and_ne_zero (hy : y ≠ 0) : (∃ r : R, 0 < r ∧ x = r • y) ↔ SameRay R x y ∧ x ≠ 0 := by rw [SameRay.sameRay_comm] simp_rw [eq_comm (a := x)] exact exists_pos_left_iff_sameRay_and_ne_zero hy theorem exists_nonneg_right_iff_sameRay (hy : y ≠ 0) : (∃ r : R, 0 ≤ r ∧ x = r • y) ↔ SameRay R x y := by rw [SameRay.sameRay_comm] simp_rw [eq_comm (a := x)] exact exists_nonneg_left_iff_sameRay (R := R) hy end LinearOrderedField
Mathlib/LinearAlgebra/Ray.lean
731
735
/- Copyright (c) 2022 Mantas Bakšys. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mantas Bakšys -/ import Mathlib.Algebra.Order.Module.OrderedSMul import Mathlib.Algebra.Order.Module.Synonym import Mathlib.Data.Prod.Lex import Mathlib.Data.Set.Image import Mathlib.Data.Finset.Max import Mathlib.GroupTheory.Perm.Support import Mathlib.Order.Monotone.Monovary import Mathlib.Tactic.Abel /-! # Rearrangement inequality This file proves the rearrangement inequality and deduces the conditions for equality and strict inequality. The rearrangement inequality tells you that for two functions `f g : ι → α`, the sum `∑ i, f i * g (σ i)` is maximized over all `σ : Perm ι` when `g ∘ σ` monovaries with `f` and minimized when `g ∘ σ` antivaries with `f`. The inequality also tells you that `∑ i, f i * g (σ i) = ∑ i, f i * g i` if and only if `g ∘ σ` monovaries with `f` when `g` monovaries with `f`. The above equality also holds if and only if `g ∘ σ` antivaries with `f` when `g` antivaries with `f`. From the above two statements, we deduce that the inequality is strict if and only if `g ∘ σ` does not monovary with `f` when `g` monovaries with `f`. Analogously, the inequality is strict if and only if `g ∘ σ` does not antivary with `f` when `g` antivaries with `f`. ## Implementation notes In fact, we don't need much compatibility between the addition and multiplication of `α`, so we can actually decouple them by replacing multiplication with scalar multiplication and making `f` and `g` land in different types. As a bonus, this makes the dual statement trivial. The multiplication versions are provided for convenience. The case for `Monotone`/`Antitone` pairs of functions over a `LinearOrder` is not deduced in this file because it is easily deducible from the `Monovary` API. ## TODO Add equality cases for when the permute function is injective. This comes from the following fact: If `Monovary f g`, `Injective g` and `σ` is a permutation, then `Monovary f (g ∘ σ) ↔ σ = 1`. -/ open Equiv Equiv.Perm Finset Function OrderDual variable {ι α β : Type*} [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] [ExistsAddOfLE α] [AddCommMonoid β] [LinearOrder β] [IsOrderedCancelAddMonoid β] [Module α β] /-! ### Scalar multiplication versions -/ section SMul /-! #### Weak rearrangement inequality -/ section weak_inequality variable [PosSMulMono α β] {s : Finset ι} {σ : Perm ι} {f : ι → α} {g : ι → β} /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when `f` and `g` monovary together on `s`. Stated by permuting the entries of `g`. -/ theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g (σ i) ≤ ∑ i ∈ s, f i • g i := by classical revert hσ σ hfg apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i)) (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → {x | σ x ≠ x} ⊆ t → ∑ i ∈ t, f i • g (σ i) ≤ ∑ i ∈ t, f i • g i) s · simp only [le_rfl, Finset.sum_empty, imp_true_iff] intro a s has hamax hind σ hfg hσ set τ : Perm ι := σ.trans (swap a (σ a)) with hτ have hτs : {x | τ x ≠ x} ⊆ s := by intro x hx simp only [τ, Ne, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx split_ifs at hx with h₁ h₂ · obtain rfl | hax := eq_or_ne x a · contradiction · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax · exact (hx <| σ.injective h₂.symm).elim · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂) specialize hind (hfg.subset <| subset_insert _ _) hτs simp_rw [sum_insert has] refine le_trans ?_ (add_le_add_left hind _) obtain hσa | hσa := eq_or_ne a (σ a) · rw [hτ, ← hσa, swap_self, trans_refl] have h1s : σ⁻¹ a ∈ s := by rw [Ne, ← inv_eq_iff_eq] at hσa refine mem_of_mem_insert_of_ne (hσ fun h ↦ hσa ?_) hσa rwa [apply_inv_self, eq_comm] at h simp only [← s.sum_erase_add _ h1s, add_comm] rw [← add_assoc, ← add_assoc] simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self] refine add_le_add (smul_add_smul_le_smul_add_smul' ?_ ?_) (sum_congr rfl fun x hx ↦ ?_).le · specialize hamax (σ⁻¹ a) h1s rw [Prod.Lex.toLex_le_toLex] at hamax rcases hamax with hamax | hamax · exact hfg (mem_insert_of_mem h1s) (mem_insert_self _ _) hamax · exact hamax.2 · specialize hamax (σ a) (mem_of_mem_insert_of_ne (hσ <| σ.injective.ne hσa.symm) hσa.symm) rw [Prod.Lex.toLex_le_toLex] at hamax rcases hamax with hamax | hamax · exact hamax.le · exact hamax.1.le · rw [mem_erase, Ne, eq_inv_iff_eq] at hx rw [swap_apply_of_ne_of_ne hx.1 (σ.injective.ne _)] rintro rfl exact has hx.2 /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is minimized when `f` and `g` antivary together on `s`. Stated by permuting the entries of `g`. -/ theorem AntivaryOn.sum_smul_le_sum_smul_comp_perm (hfg : AntivaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g i ≤ ∑ i ∈ s, f i • g (σ i) := hfg.dual_right.sum_smul_comp_perm_le_sum_smul hσ /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when `f` and `g` monovary together on `s`. Stated by permuting the entries of `f`. -/ theorem MonovaryOn.sum_comp_perm_smul_le_sum_smul (hfg : MonovaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f (σ i) • g i ≤ ∑ i ∈ s, f i • g i := by convert hfg.sum_smul_comp_perm_le_sum_smul (show { x | σ⁻¹ x ≠ x } ⊆ s by simp only [set_support_inv_eq, hσ]) using 1 exact σ.sum_comp' s (fun i j ↦ f i • g j) hσ /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is minimized when `f` and `g` antivary together on `s`. Stated by permuting the entries of `f`. -/ theorem AntivaryOn.sum_smul_le_sum_comp_perm_smul (hfg : AntivaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g i ≤ ∑ i ∈ s, f (σ i) • g i := hfg.dual_right.sum_comp_perm_smul_le_sum_smul hσ variable [Fintype ι] /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when `f` and `g` monovary together. Stated by permuting the entries of `g`. -/ theorem Monovary.sum_smul_comp_perm_le_sum_smul (hfg : Monovary f g) : ∑ i, f i • g (σ i) ≤ ∑ i, f i • g i := (hfg.monovaryOn _).sum_smul_comp_perm_le_sum_smul fun _ _ ↦ mem_univ _ /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is minimized when `f` and `g` antivary together. Stated by permuting the entries of `g`. -/ theorem Antivary.sum_smul_le_sum_smul_comp_perm (hfg : Antivary f g) : ∑ i, f i • g i ≤ ∑ i, f i • g (σ i) := (hfg.antivaryOn _).sum_smul_le_sum_smul_comp_perm fun _ _ ↦ mem_univ _ /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when `f` and `g` monovary together. Stated by permuting the entries of `f`. -/ theorem Monovary.sum_comp_perm_smul_le_sum_smul (hfg : Monovary f g) : ∑ i, f (σ i) • g i ≤ ∑ i, f i • g i := (hfg.monovaryOn _).sum_comp_perm_smul_le_sum_smul fun _ _ ↦ mem_univ _ /-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is minimized when `f` and `g` antivary together. Stated by permuting the entries of `f`. -/ theorem Antivary.sum_smul_le_sum_comp_perm_smul (hfg : Antivary f g) : ∑ i, f i • g i ≤ ∑ i, f (σ i) • g i := (hfg.antivaryOn _).sum_smul_le_sum_comp_perm_smul fun _ _ ↦ mem_univ _ end weak_inequality /-! #### Equality case of the rearrangement inequality -/ section equality_case variable [PosSMulStrictMono α β] {s : Finset ι} {σ : Perm ι} {f : ι → α} {g : ι → β} /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which monovary together on `s`, is unchanged by a permutation if and only if `f` and `g ∘ σ` monovary together on `s`. Stated by permuting the entries of `g`. -/ theorem MonovaryOn.sum_smul_comp_perm_eq_sum_smul_iff (hfg : MonovaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g (σ i) = ∑ i ∈ s, f i • g i ↔ MonovaryOn f (g ∘ σ) s := by classical refine ⟨not_imp_not.1 fun h ↦ ?_, fun h ↦ (hfg.sum_smul_comp_perm_le_sum_smul hσ).antisymm ?_⟩ · rw [MonovaryOn] at h push_neg at h obtain ⟨x, hx, y, hy, hgxy, hfxy⟩ := h set τ : Perm ι := (Equiv.swap x y).trans σ have hτs : {x | τ x ≠ x} ⊆ s := by refine (set_support_mul_subset σ <| swap x y).trans (Set.union_subset hσ fun z hz ↦ ?_) obtain ⟨_, rfl | rfl⟩ := swap_apply_ne_self_iff.1 hz <;> assumption refine ((hfg.sum_smul_comp_perm_le_sum_smul hτs).trans_lt' ?_).ne obtain rfl | hxy := eq_or_ne x y · cases lt_irrefl _ hfxy simp only [τ, ← s.sum_erase_add _ hx, ← (s.erase x).sum_erase_add _ (mem_erase.2 ⟨hxy.symm, hy⟩), add_assoc, Equiv.coe_trans, Function.comp_apply, swap_apply_right, swap_apply_left] refine add_lt_add_of_le_of_lt (Finset.sum_congr rfl fun z hz ↦ ?_).le (smul_add_smul_lt_smul_add_smul hfxy hgxy) simp_rw [mem_erase] at hz rw [swap_apply_of_ne_of_ne hz.2.1 hz.1] · convert h.sum_smul_comp_perm_le_sum_smul ((set_support_inv_eq _).subset.trans hσ) using 1 simp_rw [Function.comp_apply, apply_inv_self] /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which antivary together on `s`, is unchanged by a permutation if and only if `f` and `g ∘ σ` antivary together on `s`. Stated by permuting the entries of `g`. -/ theorem AntivaryOn.sum_smul_comp_perm_eq_sum_smul_iff (hfg : AntivaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g (σ i) = ∑ i ∈ s, f i • g i ↔ AntivaryOn f (g ∘ σ) s := (hfg.dual_right.sum_smul_comp_perm_eq_sum_smul_iff hσ).trans monovaryOn_toDual_right /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which monovary together on `s`, is unchanged by a permutation if and only if `f ∘ σ` and `g` monovary together on `s`. Stated by permuting the entries of `f`. -/ theorem MonovaryOn.sum_comp_perm_smul_eq_sum_smul_iff (hfg : MonovaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f (σ i) • g i = ∑ i ∈ s, f i • g i ↔ MonovaryOn (f ∘ σ) g s := by have hσinv : { x | σ⁻¹ x ≠ x } ⊆ s := (set_support_inv_eq _).subset.trans hσ refine (Iff.trans ?_ <| hfg.sum_smul_comp_perm_eq_sum_smul_iff hσinv).trans ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · apply eq_iff_eq_cancel_right.2 rw [σ.sum_comp' s (fun i j ↦ f i • g j) hσ] congr · convert h.comp_right σ · rw [comp_assoc, inv_def, symm_comp_self, comp_id] · rw [σ.eq_preimage_iff_image_eq, Set.image_perm hσ] · convert h.comp_right σ.symm · rw [comp_assoc, self_comp_symm, comp_id] · rw [σ.symm.eq_preimage_iff_image_eq] exact Set.image_perm hσinv /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which antivary together on `s`, is unchanged by a permutation if and only if `f ∘ σ` and `g` antivary together on `s`. Stated by permuting the entries of `f`. -/ theorem AntivaryOn.sum_comp_perm_smul_eq_sum_smul_iff (hfg : AntivaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f (σ i) • g i = ∑ i ∈ s, f i • g i ↔ AntivaryOn (f ∘ σ) g s := (hfg.dual_right.sum_comp_perm_smul_eq_sum_smul_iff hσ).trans monovaryOn_toDual_right variable [Fintype ι] /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which monovary together, is unchanged by a permutation if and only if `f` and `g ∘ σ` monovary together. Stated by permuting the entries of `g`. -/ theorem Monovary.sum_smul_comp_perm_eq_sum_smul_iff (hfg : Monovary f g) : ∑ i, f i • g (σ i) = ∑ i, f i • g i ↔ Monovary f (g ∘ σ) := by simp [(hfg.monovaryOn _).sum_smul_comp_perm_eq_sum_smul_iff fun _ _ ↦ mem_univ _] /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which monovary together, is unchanged by a permutation if and only if `f ∘ σ` and `g` monovary together. Stated by permuting the entries of `g`. -/ theorem Monovary.sum_comp_perm_smul_eq_sum_smul_iff (hfg : Monovary f g) : ∑ i, f (σ i) • g i = ∑ i, f i • g i ↔ Monovary (f ∘ σ) g := by simp [(hfg.monovaryOn _).sum_comp_perm_smul_eq_sum_smul_iff fun _ _ ↦ mem_univ _] /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which antivary together, is unchanged by a permutation if and only if `f` and `g ∘ σ` antivary together. Stated by permuting the entries of `g`. -/ theorem Antivary.sum_smul_comp_perm_eq_sum_smul_iff (hfg : Antivary f g) : ∑ i, f i • g (σ i) = ∑ i, f i • g i ↔ Antivary f (g ∘ σ) := by simp [(hfg.antivaryOn _).sum_smul_comp_perm_eq_sum_smul_iff fun _ _ ↦ mem_univ _] /-- **Equality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which antivary together, is unchanged by a permutation if and only if `f ∘ σ` and `g` antivary together. Stated by permuting the entries of `f`. -/ theorem Antivary.sum_comp_perm_smul_eq_sum_smul_iff (hfg : Antivary f g) : ∑ i, f (σ i) • g i = ∑ i, f i • g i ↔ Antivary (f ∘ σ) g := by simp [(hfg.antivaryOn _).sum_comp_perm_smul_eq_sum_smul_iff fun _ _ ↦ mem_univ _] end equality_case /-! #### Strict rearrangement inequality -/ section strict_inequality variable [PosSMulStrictMono α β] {s : Finset ι} {σ : Perm ι} {f : ι → α} {g : ι → β} /-- **Strict inequality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which monovary together on `s`, is strictly decreased by a permutation if and only if `f` and `g ∘ σ` do not monovary together on `s`. Stated by permuting the entries of `g`. -/ theorem MonovaryOn.sum_smul_comp_perm_lt_sum_smul_iff (hfg : MonovaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g (σ i) < ∑ i ∈ s, f i • g i ↔ ¬MonovaryOn f (g ∘ σ) s := by simp [← hfg.sum_smul_comp_perm_eq_sum_smul_iff hσ, lt_iff_le_and_ne, hfg.sum_smul_comp_perm_le_sum_smul hσ] /-- **Strict inequality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which antivary together on `s`, is strictly decreased by a permutation if and only if `f` and `g ∘ σ` do not antivary together on `s`. Stated by permuting the entries of `g`. -/ theorem AntivaryOn.sum_smul_lt_sum_smul_comp_perm_iff (hfg : AntivaryOn f g s) (hσ : {x | σ x ≠ x} ⊆ s) : ∑ i ∈ s, f i • g i < ∑ i ∈ s, f i • g (σ i) ↔ ¬AntivaryOn f (g ∘ σ) s := by simp [← hfg.sum_smul_comp_perm_eq_sum_smul_iff hσ, lt_iff_le_and_ne, eq_comm, hfg.sum_smul_le_sum_smul_comp_perm hσ]
/-- **Strict inequality case of the Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g`, which monovary together on `s`, is strictly decreased by a permutation if and only if
Mathlib/Algebra/Order/Rearrangement.lean
285
287
/- Copyright (c) 2023 David Kurniadi Angdinata. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Kurniadi Angdinata -/ import Mathlib.Algebra.Polynomial.Bivariate import Mathlib.AlgebraicGeometry.EllipticCurve.Weierstrass import Mathlib.AlgebraicGeometry.EllipticCurve.VariableChange /-! # Affine coordinates for Weierstrass curves This file defines the type of points on a Weierstrass curve as an inductive, consisting of the point at infinity and affine points satisfying a Weierstrass equation with a nonsingular condition. This file also defines the negation and addition operations of the group law for this type, and proves that they respect the Weierstrass equation and the nonsingular condition. The fact that they form an abelian group is proven in `Mathlib/AlgebraicGeometry/EllipticCurve/Group.lean`. ## Mathematical background Let `W` be a Weierstrass curve over a field `F` with coefficients `aᵢ`. An *affine point* on `W` is a tuple `(x, y)` of elements in `R` satisfying the *Weierstrass equation* `W(X, Y) = 0` in *affine coordinates*, where `W(X, Y) := Y² + a₁XY + a₃Y - (X³ + a₂X² + a₄X + a₆)`. It is *nonsingular* if its partial derivatives `W_X(x, y)` and `W_Y(x, y)` do not vanish simultaneously. The nonsingular affine points on `W` can be given negation and addition operations defined by a secant-and-tangent process. * Given a nonsingular affine point `P`, its *negation* `-P` is defined to be the unique third nonsingular point of intersection between `W` and the vertical line through `P`. Explicitly, if `P` is `(x, y)`, then `-P` is `(x, -y - a₁x - a₃)`. * Given two nonsingular affine points `P` and `Q`, their *addition* `P + Q` is defined to be the negation of the unique third nonsingular point of intersection between `W` and the line `L` through `P` and `Q`. Explicitly, let `P` be `(x₁, y₁)` and let `Q` be `(x₂, y₂)`. * If `x₁ = x₂` and `y₁ = -y₂ - a₁x₂ - a₃`, then `L` is vertical. * If `x₁ = x₂` and `y₁ ≠ -y₂ - a₁x₂ - a₃`, then `L` is the tangent of `W` at `P = Q`, and has slope `ℓ := (3x₁² + 2a₂x₁ + a₄ - a₁y₁) / (2y₁ + a₁x₁ + a₃)`. * Otherwise `x₁ ≠ x₂`, then `L` is the secant of `W` through `P` and `Q`, and has slope `ℓ := (y₁ - y₂) / (x₁ - x₂)`. In the last two cases, the `X`-coordinate of `P + Q` is then the unique third solution of the equation obtained by substituting the line `Y = ℓ(X - x₁) + y₁` into the Weierstrass equation, and can be written down explicitly as `x := ℓ² + a₁ℓ - a₂ - x₁ - x₂` by inspecting the coefficients of `X²`. The `Y`-coordinate of `P + Q`, after applying the final negation that maps `Y` to `-Y - a₁X - a₃`, is precisely `y := -(ℓ(x - x₁) + y₁) - a₁x - a₃`. The type of nonsingular points `W⟮F⟯` in affine coordinates is an inductive, consisting of the unique point at infinity `𝓞` and nonsingular affine points `(x, y)`. Then `W⟮F⟯` can be endowed with a group law, with `𝓞` as the identity nonsingular point, which is uniquely determined by these formulae. ## Main definitions * `WeierstrassCurve.Affine.Equation`: the Weierstrass equation of an affine Weierstrass curve. * `WeierstrassCurve.Affine.Nonsingular`: the nonsingular condition on an affine Weierstrass curve. * `WeierstrassCurve.Affine.Point`: a nonsingular rational point on an affine Weierstrass curve. * `WeierstrassCurve.Affine.Point.neg`: the negation operation on an affine Weierstrass curve. * `WeierstrassCurve.Affine.Point.add`: the addition operation on an affine Weierstrass curve. ## Main statements * `WeierstrassCurve.Affine.equation_neg`: negation preserves the Weierstrass equation. * `WeierstrassCurve.Affine.equation_add`: addition preserves the Weierstrass equation. * `WeierstrassCurve.Affine.nonsingular_neg`: negation preserves the nonsingular condition. * `WeierstrassCurve.Affine.nonsingular_add`: addition preserves the nonsingular condition. * `WeierstrassCurve.Affine.nonsingular_of_Δ_ne_zero`: an affine Weierstrass curve is nonsingular at every point if its discriminant is non-zero. * `WeierstrassCurve.Affine.nonsingular`: an affine elliptic curve is nonsingular at every point. ## Notations * `W⟮K⟯`: the group of nonsingular rational points on `W` base changed to `K`. ## References [J Silverman, *The Arithmetic of Elliptic Curves*][silverman2009] ## Tags elliptic curve, rational point, affine coordinates -/ open Polynomial open scoped Polynomial.Bivariate local macro "C_simp" : tactic => `(tactic| simp only [map_ofNat, C_0, C_1, C_neg, C_add, C_sub, C_mul, C_pow]) local macro "derivative_simp" : tactic => `(tactic| simp only [derivative_C, derivative_X, derivative_X_pow, derivative_neg, derivative_add, derivative_sub, derivative_mul, derivative_sq]) local macro "eval_simp" : tactic => `(tactic| simp only [eval_C, eval_X, eval_neg, eval_add, eval_sub, eval_mul, eval_pow, evalEval]) local macro "map_simp" : tactic => `(tactic| simp only [map_ofNat, map_neg, map_add, map_sub, map_mul, map_pow, map_div₀, Polynomial.map_ofNat, map_C, map_X, Polynomial.map_neg, Polynomial.map_add, Polynomial.map_sub, Polynomial.map_mul, Polynomial.map_pow, Polynomial.map_div, coe_mapRingHom, WeierstrassCurve.map]) universe r s u v w /-! ## Weierstrass curves -/ namespace WeierstrassCurve variable {R : Type r} {S : Type s} {A F : Type u} {B K : Type v} {L : Type w} variable (R) in /-- An abbreviation for a Weierstrass curve in affine coordinates. -/ abbrev Affine : Type r := WeierstrassCurve R /-- The conversion from a Weierstrass curve to affine coordinates. -/ abbrev toAffine (W : WeierstrassCurve R) : Affine R := W namespace Affine variable [CommRing R] [CommRing S] [CommRing A] [CommRing B] [Field F] [Field K] [Field L] {W' : Affine R} {W : Affine F} section Equation /-! ### Weierstrass equations -/ variable (W') in /-- The polynomial `W(X, Y) := Y² + a₁XY + a₃Y - (X³ + a₂X² + a₄X + a₆)` associated to a Weierstrass curve `W` over a ring `R` in affine coordinates. For ease of polynomial manipulation, this is represented as a term of type `R[X][X]`, where the inner variable represents `X` and the outer variable represents `Y`. For clarity, the alternative notations `Y` and `R[X][Y]` are provided in the `Polynomial.Bivariate` scope to represent the outer variable and the bivariate polynomial ring `R[X][X]` respectively. -/ noncomputable def polynomial : R[X][Y] := Y ^ 2 + C (C W'.a₁ * X + C W'.a₃) * Y - C (X ^ 3 + C W'.a₂ * X ^ 2 + C W'.a₄ * X + C W'.a₆) lemma polynomial_eq : W'.polynomial = Cubic.toPoly ⟨0, 1, Cubic.toPoly ⟨0, 0, W'.a₁, W'.a₃⟩, Cubic.toPoly ⟨-1, -W'.a₂, -W'.a₄, -W'.a₆⟩⟩ := by simp only [polynomial, Cubic.toPoly] C_simp ring1 lemma polynomial_ne_zero [Nontrivial R] : W'.polynomial ≠ 0 := by rw [polynomial_eq] exact Cubic.ne_zero_of_b_ne_zero one_ne_zero @[simp] lemma degree_polynomial [Nontrivial R] : W'.polynomial.degree = 2 := by rw [polynomial_eq] exact Cubic.degree_of_b_ne_zero' one_ne_zero @[simp] lemma natDegree_polynomial [Nontrivial R] : W'.polynomial.natDegree = 2 := by rw [polynomial_eq] exact Cubic.natDegree_of_b_ne_zero' one_ne_zero lemma monic_polynomial : W'.polynomial.Monic := by nontriviality R simpa only [polynomial_eq] using Cubic.monic_of_b_eq_one' lemma irreducible_polynomial [IsDomain R] : Irreducible W'.polynomial := by by_contra h rcases (monic_polynomial.not_irreducible_iff_exists_add_mul_eq_coeff natDegree_polynomial).mp h with ⟨f, g, h0, h1⟩ simp only [polynomial_eq, Cubic.coeff_eq_c, Cubic.coeff_eq_d] at h0 h1 apply_fun degree at h0 h1 rw [Cubic.degree_of_a_ne_zero' <| neg_ne_zero.mpr <| one_ne_zero' R, degree_mul] at h0 apply (h1.symm.le.trans Cubic.degree_of_b_eq_zero').not_lt rcases Nat.WithBot.add_eq_three_iff.mp h0.symm with h | h | h | h iterate 2 rw [degree_add_eq_right_of_degree_lt] <;> simp only [h] <;> decide iterate 2 rw [degree_add_eq_left_of_degree_lt] <;> simp only [h] <;> decide lemma evalEval_polynomial (x y : R) : W'.polynomial.evalEval x y = y ^ 2 + W'.a₁ * x * y + W'.a₃ * y - (x ^ 3 + W'.a₂ * x ^ 2 + W'.a₄ * x + W'.a₆) := by simp only [polynomial] eval_simp rw [add_mul, ← add_assoc] @[simp] lemma evalEval_polynomial_zero : W'.polynomial.evalEval 0 0 = -W'.a₆ := by simp only [evalEval_polynomial, zero_add, zero_sub, mul_zero, zero_pow <| Nat.succ_ne_zero _] variable (W') in /-- The proposition that an affine point `(x, y)` lies in a Weierstrass curve `W`. In other words, it satisfies the Weierstrass equation `W(X, Y) = 0`. -/ def Equation (x y : R) : Prop := W'.polynomial.evalEval x y = 0 lemma equation_iff' (x y : R) : W'.Equation x y ↔ y ^ 2 + W'.a₁ * x * y + W'.a₃ * y - (x ^ 3 + W'.a₂ * x ^ 2 + W'.a₄ * x + W'.a₆) = 0 := by rw [Equation, evalEval_polynomial] lemma equation_iff (x y : R) : W'.Equation x y ↔ y ^ 2 + W'.a₁ * x * y + W'.a₃ * y = x ^ 3 + W'.a₂ * x ^ 2 + W'.a₄ * x + W'.a₆ := by rw [equation_iff', sub_eq_zero] @[simp] lemma equation_zero : W'.Equation 0 0 ↔ W'.a₆ = 0 := by rw [Equation, evalEval_polynomial_zero, neg_eq_zero] lemma equation_iff_variableChange (x y : R) : W'.Equation x y ↔ (VariableChange.mk 1 x 0 y • W').toAffine.Equation 0 0 := by rw [equation_iff', ← neg_eq_zero, equation_zero, variableChange_a₆, inv_one, Units.val_one] congr! 1 ring1 end Equation section Nonsingular /-! ### Nonsingular Weierstrass equations -/ variable (W') in /-- The partial derivative `W_X(X, Y)` with respect to `X` of the polynomial `W(X, Y)` associated to a Weierstrass curve `W` in affine coordinates. -/ -- TODO: define this in terms of `Polynomial.derivative`. noncomputable def polynomialX : R[X][Y] := C (C W'.a₁) * Y - C (C 3 * X ^ 2 + C (2 * W'.a₂) * X + C W'.a₄) lemma evalEval_polynomialX (x y : R) : W'.polynomialX.evalEval x y = W'.a₁ * y - (3 * x ^ 2 + 2 * W'.a₂ * x + W'.a₄) := by simp only [polynomialX] eval_simp @[simp] lemma evalEval_polynomialX_zero : W'.polynomialX.evalEval 0 0 = -W'.a₄ := by simp only [evalEval_polynomialX, zero_add, zero_sub, mul_zero, zero_pow <| Nat.succ_ne_zero _] variable (W') in /-- The partial derivative `W_Y(X, Y)` with respect to `Y` of the polynomial `W(X, Y)` associated to a Weierstrass curve `W` in affine coordinates. -/ -- TODO: define this in terms of `Polynomial.derivative`. noncomputable def polynomialY : R[X][Y] := C (C 2) * Y + C (C W'.a₁ * X + C W'.a₃) lemma evalEval_polynomialY (x y : R) : W'.polynomialY.evalEval x y = 2 * y + W'.a₁ * x + W'.a₃ := by simp only [polynomialY] eval_simp rw [← add_assoc] @[simp] lemma evalEval_polynomialY_zero : W'.polynomialY.evalEval 0 0 = W'.a₃ := by simp only [evalEval_polynomialY, zero_add, mul_zero] variable (W') in /-- The proposition that an affine point `(x, y)` on a Weierstrass curve `W` is nonsingular. In other words, either `W_X(x, y) ≠ 0` or `W_Y(x, y) ≠ 0`. Note that this definition is only mathematically accurate for fields. -/ -- TODO: generalise this definition to be mathematically accurate for a larger class of rings. def Nonsingular (x y : R) : Prop := W'.Equation x y ∧ (W'.polynomialX.evalEval x y ≠ 0 ∨ W'.polynomialY.evalEval x y ≠ 0) lemma nonsingular_iff' (x y : R) : W'.Nonsingular x y ↔ W'.Equation x y ∧ (W'.a₁ * y - (3 * x ^ 2 + 2 * W'.a₂ * x + W'.a₄) ≠ 0 ∨ 2 * y + W'.a₁ * x + W'.a₃ ≠ 0) := by rw [Nonsingular, equation_iff', evalEval_polynomialX, evalEval_polynomialY] lemma nonsingular_iff (x y : R) : W'.Nonsingular x y ↔ W'.Equation x y ∧ (W'.a₁ * y ≠ 3 * x ^ 2 + 2 * W'.a₂ * x + W'.a₄ ∨ y ≠ -y - W'.a₁ * x - W'.a₃) := by rw [nonsingular_iff', sub_ne_zero, ← sub_ne_zero (a := y)] congr! 3 ring1 @[simp] lemma nonsingular_zero : W'.Nonsingular 0 0 ↔ W'.a₆ = 0 ∧ (W'.a₃ ≠ 0 ∨ W'.a₄ ≠ 0) := by rw [Nonsingular, equation_zero, evalEval_polynomialX_zero, neg_ne_zero, evalEval_polynomialY_zero, or_comm] lemma nonsingular_iff_variableChange (x y : R) : W'.Nonsingular x y ↔ (VariableChange.mk 1 x 0 y • W').toAffine.Nonsingular 0 0 := by rw [nonsingular_iff', equation_iff_variableChange, equation_zero, ← neg_ne_zero, or_comm, nonsingular_zero, variableChange_a₃, variableChange_a₄, inv_one, Units.val_one] simp only [variableChange_def] congr! 3 <;> ring1 private lemma equation_zero_iff_nonsingular_zero_of_Δ_ne_zero (hΔ : W'.Δ ≠ 0) : W'.Equation 0 0 ↔ W'.Nonsingular 0 0 := by simp only [equation_zero, nonsingular_zero, iff_self_and] contrapose! hΔ simp only [b₂, b₄, b₆, b₈, Δ, hΔ] ring1 /-- A Weierstrass curve is nonsingular at every point if its discriminant is non-zero. -/ lemma equation_iff_nonsingular_of_Δ_ne_zero {x y : R} (hΔ : W'.Δ ≠ 0) : W'.Equation x y ↔ W'.Nonsingular x y := by rw [equation_iff_variableChange, nonsingular_iff_variableChange, equation_zero_iff_nonsingular_zero_of_Δ_ne_zero <| by rwa [variableChange_Δ, inv_one, Units.val_one, one_pow, one_mul]] /-- An elliptic curve is nonsingular at every point. -/ lemma equation_iff_nonsingular [Nontrivial R] [W'.IsElliptic] {x y : R} : W'.toAffine.Equation x y ↔ W'.toAffine.Nonsingular x y := W'.toAffine.equation_iff_nonsingular_of_Δ_ne_zero <| W'.coe_Δ' ▸ W'.Δ'.ne_zero @[deprecated (since := "2025-03-01")] alias nonsingular_zero_of_Δ_ne_zero := equation_iff_nonsingular_of_Δ_ne_zero @[deprecated (since := "2025-03-01")] alias nonsingular_of_Δ_ne_zero := equation_iff_nonsingular_of_Δ_ne_zero @[deprecated (since := "2025-03-01")] alias nonsingular := equation_iff_nonsingular end Nonsingular section Ring /-! ### Group operation polynomials over a ring -/ variable (W') in /-- The negation polynomial `-Y - a₁X - a₃` associated to the negation of a nonsingular affine point on a Weierstrass curve. -/ noncomputable def negPolynomial : R[X][Y] := -(Y : R[X][Y]) - C (C W'.a₁ * X + C W'.a₃) lemma Y_sub_polynomialY : Y - W'.polynomialY = W'.negPolynomial := by rw [polynomialY, negPolynomial] C_simp ring1 lemma Y_sub_negPolynomial : Y - W'.negPolynomial = W'.polynomialY := by rw [← Y_sub_polynomialY, sub_sub_cancel] variable (W') in /-- The `Y`-coordinate of `-(x, y)` for a nonsingular affine point `(x, y)` on a Weierstrass curve `W`. This depends on `W`, and has argument order: `x`, `y`. -/ @[simp] def negY (x y : R) : R := -y - W'.a₁ * x - W'.a₃ lemma negY_negY (x y : R) : W'.negY x (W'.negY x y) = y := by simp only [negY] ring1 lemma evalEval_negPolynomial (x y : R) : W'.negPolynomial.evalEval x y = W'.negY x y := by rw [negY, sub_sub, negPolynomial] eval_simp @[deprecated (since := "2025-03-05")] alias eval_negPolynomial := evalEval_negPolynomial /-- The line polynomial `ℓ(X - x) + y` associated to the line `Y = ℓ(X - x) + y` that passes through a nonsingular affine point `(x, y)` on a Weierstrass curve `W` with a slope of `ℓ`. This does not depend on `W`, and has argument order: `x`, `y`, `ℓ`. -/ noncomputable def linePolynomial (x y ℓ : R) : R[X] := C ℓ * (X - C x) + C y variable (W') in /-- The addition polynomial obtained by substituting the line `Y = ℓ(X - x) + y` into the polynomial `W(X, Y)` associated to a Weierstrass curve `W`. If such a line intersects `W` at another nonsingular affine point `(x', y')` on `W`, then the roots of this polynomial are precisely `x`, `x'`, and the `X`-coordinate of the addition of `(x, y)` and `(x', y')`. This depends on `W`, and has argument order: `x`, `y`, `ℓ`. -/ noncomputable def addPolynomial (x y ℓ : R) : R[X] := W'.polynomial.eval <| linePolynomial x y ℓ lemma C_addPolynomial (x y ℓ : R) : C (W'.addPolynomial x y ℓ) = (Y - C (linePolynomial x y ℓ)) * (W'.negPolynomial - C (linePolynomial x y ℓ)) + W'.polynomial := by rw [addPolynomial, linePolynomial, polynomial, negPolynomial] eval_simp C_simp ring1 lemma addPolynomial_eq (x y ℓ : R) : W'.addPolynomial x y ℓ = -Cubic.toPoly ⟨1, -ℓ ^ 2 - W'.a₁ * ℓ + W'.a₂, 2 * x * ℓ ^ 2 + (W'.a₁ * x - 2 * y - W'.a₃) * ℓ + (-W'.a₁ * y + W'.a₄), -x ^ 2 * ℓ ^ 2 + (2 * x * y + W'.a₃ * x) * ℓ - (y ^ 2 + W'.a₃ * y - W'.a₆)⟩ := by rw [addPolynomial, linePolynomial, polynomial, Cubic.toPoly] eval_simp C_simp ring1 variable (W') in /-- The `X`-coordinate of `(x₁, y₁) + (x₂, y₂)` for two nonsingular affine points `(x₁, y₁)` and `(x₂, y₂)` on a Weierstrass curve `W`, where the line through them has a slope of `ℓ`. This depends on `W`, and has argument order: `x₁`, `x₂`, `ℓ`. -/ @[simp] def addX (x₁ x₂ ℓ : R) : R := ℓ ^ 2 + W'.a₁ * ℓ - W'.a₂ - x₁ - x₂ variable (W') in /-- The `Y`-coordinate of `-((x₁, y₁) + (x₂, y₂))` for two nonsingular affine points `(x₁, y₁)` and `(x₂, y₂)` on a Weierstrass curve `W`, where the line through them has a slope of `ℓ`. This depends on `W`, and has argument order: `x₁`, `x₂`, `y₁`, `ℓ`. -/ @[simp] def negAddY (x₁ x₂ y₁ ℓ : R) : R := ℓ * (W'.addX x₁ x₂ ℓ - x₁) + y₁ variable (W') in /-- The `Y`-coordinate of `(x₁, y₁) + (x₂, y₂)` for two nonsingular affine points `(x₁, y₁)` and `(x₂, y₂)` on a Weierstrass curve `W`, where the line through them has a slope of `ℓ`. This depends on `W`, and has argument order: `x₁`, `x₂`, `y₁`, `ℓ`. -/ @[simp] def addY (x₁ x₂ y₁ ℓ : R) : R := W'.negY (W'.addX x₁ x₂ ℓ) (W'.negAddY x₁ x₂ y₁ ℓ) lemma equation_neg (x y : R) : W'.Equation x (W'.negY x y) ↔ W'.Equation x y := by rw [equation_iff, equation_iff, negY] congr! 1 ring1 @[deprecated (since := "2025-02-01")] alias equation_neg_of := equation_neg @[deprecated (since := "2025-02-01")] alias equation_neg_iff := equation_neg lemma nonsingular_neg (x y : R) : W'.Nonsingular x (W'.negY x y) ↔ W'.Nonsingular x y := by rw [nonsingular_iff, equation_neg, ← negY, negY_negY, ← @ne_comm _ y, nonsingular_iff] exact and_congr_right' <| (iff_congr not_and_or.symm not_and_or.symm).mpr <| not_congr <| and_congr_left fun h => by rw [← h] @[deprecated (since := "2025-02-01")] alias nonsingular_neg_of := nonsingular_neg @[deprecated (since := "2025-02-01")] alias nonsingular_neg_iff := nonsingular_neg lemma equation_add_iff (x₁ x₂ y₁ ℓ : R) : W'.Equation (W'.addX x₁ x₂ ℓ) (W'.negAddY x₁ x₂ y₁ ℓ) ↔ (W'.addPolynomial x₁ y₁ ℓ).eval (W'.addX x₁ x₂ ℓ) = 0 := by rw [Equation, negAddY, addPolynomial, linePolynomial, polynomial] eval_simp lemma nonsingular_negAdd_of_eval_derivative_ne_zero {x₁ x₂ y₁ ℓ : R} (hx' : W'.Equation (W'.addX x₁ x₂ ℓ) (W'.negAddY x₁ x₂ y₁ ℓ)) (hx : (W'.addPolynomial x₁ y₁ ℓ).derivative.eval (W'.addX x₁ x₂ ℓ) ≠ 0) : W'.Nonsingular (W'.addX x₁ x₂ ℓ) (W'.negAddY x₁ x₂ y₁ ℓ) := by rw [Nonsingular, and_iff_right hx', negAddY, polynomialX, polynomialY] eval_simp contrapose! hx rw [addPolynomial, linePolynomial, polynomial] eval_simp derivative_simp simp only [zero_add, add_zero, sub_zero, zero_mul, mul_one] eval_simp linear_combination (norm := (norm_num1; ring1)) hx.left + ℓ * hx.right end Ring section Field /-! ### Group operation polynomials over a field -/ open Classical in variable (W) in /-- The slope of the line through two nonsingular affine points `(x₁, y₁)` and `(x₂, y₂)` on a Weierstrass curve `W`. If `x₁ ≠ x₂`, then this line is the secant of `W` through `(x₁, y₁)` and `(x₂, y₂)`, and has slope `(y₁ - y₂) / (x₁ - x₂)`. Otherwise, if `y₁ ≠ -y₁ - a₁x₁ - a₃`, then this line is the tangent of `W` at `(x₁, y₁) = (x₂, y₂)`, and has slope `(3x₁² + 2a₂x₁ + a₄ - a₁y₁) / (2y₁ + a₁x₁ + a₃)`. Otherwise, this line is vertical, in which case this returns the value `0`. This depends on `W`, and has argument order: `x₁`, `x₂`, `y₁`, `y₂`. -/ noncomputable def slope (x₁ x₂ y₁ y₂ : F) : F := if x₁ = x₂ then if y₁ = W.negY x₂ y₂ then 0 else (3 * x₁ ^ 2 + 2 * W.a₂ * x₁ + W.a₄ - W.a₁ * y₁) / (y₁ - W.negY x₁ y₁) else (y₁ - y₂) / (x₁ - x₂) @[simp] lemma slope_of_Y_eq {x₁ x₂ y₁ y₂ : F} (hx : x₁ = x₂) (hy : y₁ = W.negY x₂ y₂) : W.slope x₁ x₂ y₁ y₂ = 0 := by rw [slope, if_pos hx, if_pos hy] @[simp] lemma slope_of_Y_ne {x₁ x₂ y₁ y₂ : F} (hx : x₁ = x₂) (hy : y₁ ≠ W.negY x₂ y₂) : W.slope x₁ x₂ y₁ y₂ = (3 * x₁ ^ 2 + 2 * W.a₂ * x₁ + W.a₄ - W.a₁ * y₁) / (y₁ - W.negY x₁ y₁) := by rw [slope, if_pos hx, if_neg hy] @[simp] lemma slope_of_X_ne {x₁ x₂ y₁ y₂ : F} (hx : x₁ ≠ x₂) : W.slope x₁ x₂ y₁ y₂ = (y₁ - y₂) / (x₁ - x₂) := by rw [slope, if_neg hx] lemma slope_of_Y_ne_eq_evalEval {x₁ x₂ y₁ y₂ : F} (hx : x₁ = x₂) (hy : y₁ ≠ W.negY x₂ y₂) : W.slope x₁ x₂ y₁ y₂ = -W.polynomialX.evalEval x₁ y₁ / W.polynomialY.evalEval x₁ y₁ := by rw [slope_of_Y_ne hx hy, evalEval_polynomialX, neg_sub] congr 1 rw [negY, evalEval_polynomialY] ring1 @[deprecated (since := "2025-03-05")] alias slope_of_Y_ne_eq_eval := slope_of_Y_ne_eq_evalEval lemma Y_eq_of_X_eq {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hx : x₁ = x₂) : y₁ = y₂ ∨ y₁ = W.negY x₂ y₂ := by rw [equation_iff] at h₁ h₂ rw [← sub_eq_zero, ← sub_eq_zero (a := y₁), ← mul_eq_zero, negY] linear_combination (norm := (rw [hx]; ring1)) h₁ - h₂ lemma Y_eq_of_Y_ne {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hx : x₁ = x₂) (hy : y₁ ≠ W.negY x₂ y₂) : y₁ = y₂ := (Y_eq_of_X_eq h₁ h₂ hx).resolve_right hy lemma addPolynomial_slope {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : W.addPolynomial x₁ y₁ (W.slope x₁ x₂ y₁ y₂) = -((X - C x₁) * (X - C x₂) * (X - C (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂))) := by rw [addPolynomial_eq, neg_inj, Cubic.prod_X_sub_C_eq, Cubic.toPoly_injective] by_cases hx : x₁ = x₂ · have hy : y₁ ≠ W.negY x₂ y₂ := fun h => hxy ⟨hx, h⟩ rcases hx, Y_eq_of_Y_ne h₁ h₂ hx hy with ⟨rfl, rfl⟩ rw [equation_iff] at h₁ h₂ rw [slope_of_Y_ne rfl hy] rw [negY, ← sub_ne_zero] at hy ext · rfl · simp only [addX] ring1 · field_simp [hy] ring1 · linear_combination (norm := (field_simp [hy]; ring1)) -h₁ · rw [equation_iff] at h₁ h₂ rw [slope_of_X_ne hx] rw [← sub_eq_zero] at hx ext · rfl · simp only [addX] ring1 · apply mul_right_injective₀ hx linear_combination (norm := (field_simp [hx]; ring1)) h₂ - h₁ · apply mul_right_injective₀ hx linear_combination (norm := (field_simp [hx]; ring1)) x₂ * h₁ - x₁ * h₂ /-- The negated addition of two affine points in `W` on a sloped line lies in `W`. -/ lemma equation_negAdd {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : W.Equation (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂) (W.negAddY x₁ x₂ y₁ <| W.slope x₁ x₂ y₁ y₂) := by rw [equation_add_iff, addPolynomial_slope h₁ h₂ hxy] eval_simp rw [neg_eq_zero, sub_self, mul_zero] /-- The addition of two affine points in `W` on a sloped line lies in `W`. -/ lemma equation_add {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : W.Equation (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂) (W.addY x₁ x₂ y₁ <| W.slope x₁ x₂ y₁ y₂) := (equation_neg ..).mpr <| equation_negAdd h₁ h₂ hxy lemma C_addPolynomial_slope {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : C (W.addPolynomial x₁ y₁ <| W.slope x₁ x₂ y₁ y₂) = -(C (X - C x₁) * C (X - C x₂) * C (X - C (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂))) := by rw [addPolynomial_slope h₁ h₂ hxy] map_simp lemma derivative_addPolynomial_slope {x₁ x₂ y₁ y₂ : F} (h₁ : W.Equation x₁ y₁) (h₂ : W.Equation x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : derivative (W.addPolynomial x₁ y₁ <| W.slope x₁ x₂ y₁ y₂) = -((X - C x₁) * (X - C x₂) + (X - C x₁) * (X - C (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂)) + (X - C x₂) * (X - C (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂))) := by rw [addPolynomial_slope h₁ h₂ hxy] derivative_simp ring1 /-- The negated addition of two nonsingular affine points in `W` on a sloped line is nonsingular. -/ lemma nonsingular_negAdd {x₁ x₂ y₁ y₂ : F} (h₁ : W.Nonsingular x₁ y₁) (h₂ : W.Nonsingular x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : W.Nonsingular (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂) (W.negAddY x₁ x₂ y₁ <| W.slope x₁ x₂ y₁ y₂) := by by_cases hx₁ : W.addX x₁ x₂ (W.slope x₁ x₂ y₁ y₂) = x₁ · rwa [negAddY, hx₁, sub_self, mul_zero, zero_add] · by_cases hx₂ : W.addX x₁ x₂ (W.slope x₁ x₂ y₁ y₂) = x₂ · by_cases hx : x₁ = x₂ · subst hx contradiction · rwa [negAddY, ← neg_sub, mul_neg, hx₂, slope_of_X_ne hx, div_mul_cancel₀ _ <| sub_ne_zero_of_ne hx, neg_sub, sub_add_cancel] · apply nonsingular_negAdd_of_eval_derivative_ne_zero <| equation_negAdd h₁.left h₂.left hxy rw [derivative_addPolynomial_slope h₁.left h₂.left hxy] eval_simp simp only [neg_ne_zero, sub_self, mul_zero, add_zero] exact mul_ne_zero (sub_ne_zero_of_ne hx₁) (sub_ne_zero_of_ne hx₂) /-- The addition of two nonsingular affine points in `W` on a sloped line is nonsingular. -/ lemma nonsingular_add {x₁ x₂ y₁ y₂ : F} (h₁ : W.Nonsingular x₁ y₁) (h₂ : W.Nonsingular x₂ y₂) (hxy : ¬(x₁ = x₂ ∧ y₁ = W.negY x₂ y₂)) : W.Nonsingular (W.addX x₁ x₂ <| W.slope x₁ x₂ y₁ y₂) (W.addY x₁ x₂ y₁ <| W.slope x₁ x₂ y₁ y₂) := (nonsingular_neg ..).mpr <| nonsingular_negAdd h₁ h₂ hxy /-- The formula `x(P₁ + P₂) = x(P₁ - P₂) - ψ(P₁)ψ(P₂) / (x(P₂) - x(P₁))²`, where `ψ(x,y) = 2y + a₁x + a₃`. -/ lemma addX_eq_addX_negY_sub {x₁ x₂ : F} (y₁ y₂ : F) (hx : x₁ ≠ x₂) :
W.addX x₁ x₂ (W.slope x₁ x₂ y₁ y₂) = W.addX x₁ x₂ (W.slope x₁ x₂ y₁ <| W.negY x₂ y₂) - (y₁ - W.negY x₁ y₁) * (y₂ - W.negY x₂ y₂) / (x₂ - x₁) ^ 2 := by simp_rw [slope_of_X_ne hx, addX, negY, ← neg_sub x₁, neg_sq] field_simp [sub_ne_zero.mpr hx] ring1 /-- The formula `y(P₁)(x(P₂) - x(P₃)) + y(P₂)(x(P₃) - x(P₁)) + y(P₃)(x(P₁) - x(P₂)) = 0`,
Mathlib/AlgebraicGeometry/EllipticCurve/Affine.lean
582
588
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import Mathlib.Order.Bounds.Defs import Mathlib.Order.Directed import Mathlib.Order.BoundedOrder.Monotone import Mathlib.Order.Interval.Set.Basic /-! # Upper / lower bounds In this file we prove various lemmas about upper/lower bounds of a set: monotonicity, behaviour under `∪`, `∩`, `insert`, and provide formulas for `∅`, `univ`, and intervals. -/ open Function Set open OrderDual (toDual ofDual) universe u v variable {α : Type u} {γ : Type v} section variable [Preorder α] {s t : Set α} {a b : α} theorem mem_upperBounds : a ∈ upperBounds s ↔ ∀ x ∈ s, x ≤ a := Iff.rfl theorem mem_lowerBounds : a ∈ lowerBounds s ↔ ∀ x ∈ s, a ≤ x := Iff.rfl lemma mem_upperBounds_iff_subset_Iic : a ∈ upperBounds s ↔ s ⊆ Iic a := Iff.rfl lemma mem_lowerBounds_iff_subset_Ici : a ∈ lowerBounds s ↔ s ⊆ Ici a := Iff.rfl theorem bddAbove_def : BddAbove s ↔ ∃ x, ∀ y ∈ s, y ≤ x := Iff.rfl theorem bddBelow_def : BddBelow s ↔ ∃ x, ∀ y ∈ s, x ≤ y := Iff.rfl theorem bot_mem_lowerBounds [OrderBot α] (s : Set α) : ⊥ ∈ lowerBounds s := fun _ _ => bot_le theorem top_mem_upperBounds [OrderTop α] (s : Set α) : ⊤ ∈ upperBounds s := fun _ _ => le_top @[simp] theorem isLeast_bot_iff [OrderBot α] : IsLeast s ⊥ ↔ ⊥ ∈ s := and_iff_left <| bot_mem_lowerBounds _ @[simp] theorem isGreatest_top_iff [OrderTop α] : IsGreatest s ⊤ ↔ ⊤ ∈ s := and_iff_left <| top_mem_upperBounds _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x` is not greater than or equal to `y`. This version only assumes `Preorder` structure and uses `¬(y ≤ x)`. A version for linear orders is called `not_bddAbove_iff`. -/ theorem not_bddAbove_iff' : ¬BddAbove s ↔ ∀ x, ∃ y ∈ s, ¬y ≤ x := by simp [BddAbove, upperBounds, Set.Nonempty] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x` is not less than or equal to `y`. This version only assumes `Preorder` structure and uses `¬(x ≤ y)`. A version for linear orders is called `not_bddBelow_iff`. -/ theorem not_bddBelow_iff' : ¬BddBelow s ↔ ∀ x, ∃ y ∈ s, ¬x ≤ y := @not_bddAbove_iff' αᵒᵈ _ _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater than `x`. A version for preorders is called `not_bddAbove_iff'`. -/ theorem not_bddAbove_iff {α : Type*} [LinearOrder α] {s : Set α} : ¬BddAbove s ↔ ∀ x, ∃ y ∈ s, x < y := by simp only [not_bddAbove_iff', not_le] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less than `x`. A version for preorders is called `not_bddBelow_iff'`. -/ theorem not_bddBelow_iff {α : Type*} [LinearOrder α] {s : Set α} : ¬BddBelow s ↔ ∀ x, ∃ y ∈ s, y < x := @not_bddAbove_iff αᵒᵈ _ _ @[simp] lemma bddBelow_preimage_ofDual {s : Set α} : BddBelow (ofDual ⁻¹' s) ↔ BddAbove s := Iff.rfl @[simp] lemma bddAbove_preimage_ofDual {s : Set α} : BddAbove (ofDual ⁻¹' s) ↔ BddBelow s := Iff.rfl @[simp] lemma bddBelow_preimage_toDual {s : Set αᵒᵈ} : BddBelow (toDual ⁻¹' s) ↔ BddAbove s := Iff.rfl @[simp] lemma bddAbove_preimage_toDual {s : Set αᵒᵈ} : BddAbove (toDual ⁻¹' s) ↔ BddBelow s := Iff.rfl theorem BddAbove.dual (h : BddAbove s) : BddBelow (ofDual ⁻¹' s) := h theorem BddBelow.dual (h : BddBelow s) : BddAbove (ofDual ⁻¹' s) := h theorem IsLeast.dual (h : IsLeast s a) : IsGreatest (ofDual ⁻¹' s) (toDual a) := h theorem IsGreatest.dual (h : IsGreatest s a) : IsLeast (ofDual ⁻¹' s) (toDual a) := h theorem IsLUB.dual (h : IsLUB s a) : IsGLB (ofDual ⁻¹' s) (toDual a) := h theorem IsGLB.dual (h : IsGLB s a) : IsLUB (ofDual ⁻¹' s) (toDual a) := h /-- If `a` is the least element of a set `s`, then subtype `s` is an order with bottom element. -/ abbrev IsLeast.orderBot (h : IsLeast s a) : OrderBot s where bot := ⟨a, h.1⟩ bot_le := Subtype.forall.2 h.2 /-- If `a` is the greatest element of a set `s`, then subtype `s` is an order with top element. -/ abbrev IsGreatest.orderTop (h : IsGreatest s a) : OrderTop s where top := ⟨a, h.1⟩ le_top := Subtype.forall.2 h.2 theorem isLUB_congr (h : upperBounds s = upperBounds t) : IsLUB s a ↔ IsLUB t a := by rw [IsLUB, IsLUB, h] theorem isGLB_congr (h : lowerBounds s = lowerBounds t) : IsGLB s a ↔ IsGLB t a := by rw [IsGLB, IsGLB, h] /-! ### Monotonicity -/ theorem upperBounds_mono_set ⦃s t : Set α⦄ (hst : s ⊆ t) : upperBounds t ⊆ upperBounds s := fun _ hb _ h => hb <| hst h theorem lowerBounds_mono_set ⦃s t : Set α⦄ (hst : s ⊆ t) : lowerBounds t ⊆ lowerBounds s := fun _ hb _ h => hb <| hst h theorem upperBounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upperBounds s → b ∈ upperBounds s := fun ha _ h => le_trans (ha h) hab theorem lowerBounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lowerBounds s → a ∈ lowerBounds s := fun hb _ h => le_trans hab (hb h) theorem upperBounds_mono ⦃s t : Set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : a ∈ upperBounds t → b ∈ upperBounds s := fun ha => upperBounds_mono_set hst <| upperBounds_mono_mem hab ha theorem lowerBounds_mono ⦃s t : Set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : b ∈ lowerBounds t → a ∈ lowerBounds s := fun hb => lowerBounds_mono_set hst <| lowerBounds_mono_mem hab hb /-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/ theorem BddAbove.mono ⦃s t : Set α⦄ (h : s ⊆ t) : BddAbove t → BddAbove s := Nonempty.mono <| upperBounds_mono_set h /-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/ theorem BddBelow.mono ⦃s t : Set α⦄ (h : s ⊆ t) : BddBelow t → BddBelow s := Nonempty.mono <| lowerBounds_mono_set h /-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any set `t`, `s ⊆ t ⊆ p`. -/ theorem IsLUB.of_subset_of_superset {s t p : Set α} (hs : IsLUB s a) (hp : IsLUB p a) (hst : s ⊆ t) (htp : t ⊆ p) : IsLUB t a := ⟨upperBounds_mono_set htp hp.1, lowerBounds_mono_set (upperBounds_mono_set hst) hs.2⟩ /-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any set `t`, `s ⊆ t ⊆ p`. -/ theorem IsGLB.of_subset_of_superset {s t p : Set α} (hs : IsGLB s a) (hp : IsGLB p a) (hst : s ⊆ t) (htp : t ⊆ p) : IsGLB t a := hs.dual.of_subset_of_superset hp hst htp theorem IsLeast.mono (ha : IsLeast s a) (hb : IsLeast t b) (hst : s ⊆ t) : b ≤ a := hb.2 (hst ha.1) theorem IsGreatest.mono (ha : IsGreatest s a) (hb : IsGreatest t b) (hst : s ⊆ t) : a ≤ b := hb.2 (hst ha.1) theorem IsLUB.mono (ha : IsLUB s a) (hb : IsLUB t b) (hst : s ⊆ t) : a ≤ b := IsLeast.mono hb ha <| upperBounds_mono_set hst theorem IsGLB.mono (ha : IsGLB s a) (hb : IsGLB t b) (hst : s ⊆ t) : b ≤ a := IsGreatest.mono hb ha <| lowerBounds_mono_set hst theorem subset_lowerBounds_upperBounds (s : Set α) : s ⊆ lowerBounds (upperBounds s) := fun _ hx _ hy => hy hx theorem subset_upperBounds_lowerBounds (s : Set α) : s ⊆ upperBounds (lowerBounds s) := fun _ hx _ hy => hy hx theorem Set.Nonempty.bddAbove_lowerBounds (hs : s.Nonempty) : BddAbove (lowerBounds s) := hs.mono (subset_upperBounds_lowerBounds s) theorem Set.Nonempty.bddBelow_upperBounds (hs : s.Nonempty) : BddBelow (upperBounds s) := hs.mono (subset_lowerBounds_upperBounds s) /-! ### Conversions -/ theorem IsLeast.isGLB (h : IsLeast s a) : IsGLB s a := ⟨h.2, fun _ hb => hb h.1⟩ theorem IsGreatest.isLUB (h : IsGreatest s a) : IsLUB s a := ⟨h.2, fun _ hb => hb h.1⟩ theorem IsLUB.upperBounds_eq (h : IsLUB s a) : upperBounds s = Ici a := Set.ext fun _ => ⟨fun hb => h.2 hb, fun hb => upperBounds_mono_mem hb h.1⟩ theorem IsGLB.lowerBounds_eq (h : IsGLB s a) : lowerBounds s = Iic a := h.dual.upperBounds_eq theorem IsLeast.lowerBounds_eq (h : IsLeast s a) : lowerBounds s = Iic a := h.isGLB.lowerBounds_eq theorem IsGreatest.upperBounds_eq (h : IsGreatest s a) : upperBounds s = Ici a := h.isLUB.upperBounds_eq theorem IsGreatest.lt_iff (h : IsGreatest s a) : a < b ↔ ∀ x ∈ s, x < b := ⟨fun hlt _x hx => (h.2 hx).trans_lt hlt, fun h' => h' _ h.1⟩ theorem IsLeast.lt_iff (h : IsLeast s a) : b < a ↔ ∀ x ∈ s, b < x := h.dual.lt_iff theorem isLUB_le_iff (h : IsLUB s a) : a ≤ b ↔ b ∈ upperBounds s := by rw [h.upperBounds_eq] rfl theorem le_isGLB_iff (h : IsGLB s a) : b ≤ a ↔ b ∈ lowerBounds s := by rw [h.lowerBounds_eq] rfl theorem isLUB_iff_le_iff : IsLUB s a ↔ ∀ b, a ≤ b ↔ b ∈ upperBounds s := ⟨fun h _ => isLUB_le_iff h, fun H => ⟨(H _).1 le_rfl, fun b hb => (H b).2 hb⟩⟩ theorem isGLB_iff_le_iff : IsGLB s a ↔ ∀ b, b ≤ a ↔ b ∈ lowerBounds s := @isLUB_iff_le_iff αᵒᵈ _ _ _ /-- If `s` has a least upper bound, then it is bounded above. -/ theorem IsLUB.bddAbove (h : IsLUB s a) : BddAbove s := ⟨a, h.1⟩ /-- If `s` has a greatest lower bound, then it is bounded below. -/ theorem IsGLB.bddBelow (h : IsGLB s a) : BddBelow s := ⟨a, h.1⟩ /-- If `s` has a greatest element, then it is bounded above. -/ theorem IsGreatest.bddAbove (h : IsGreatest s a) : BddAbove s := ⟨a, h.2⟩ /-- If `s` has a least element, then it is bounded below. -/ theorem IsLeast.bddBelow (h : IsLeast s a) : BddBelow s := ⟨a, h.2⟩ theorem IsLeast.nonempty (h : IsLeast s a) : s.Nonempty := ⟨a, h.1⟩ theorem IsGreatest.nonempty (h : IsGreatest s a) : s.Nonempty := ⟨a, h.1⟩ /-! ### Union and intersection -/ @[simp] theorem upperBounds_union : upperBounds (s ∪ t) = upperBounds s ∩ upperBounds t := Subset.antisymm (fun _ hb => ⟨fun _ hx => hb (Or.inl hx), fun _ hx => hb (Or.inr hx)⟩) fun _ hb _ hx => hx.elim (fun hs => hb.1 hs) fun ht => hb.2 ht @[simp] theorem lowerBounds_union : lowerBounds (s ∪ t) = lowerBounds s ∩ lowerBounds t := @upperBounds_union αᵒᵈ _ s t theorem union_upperBounds_subset_upperBounds_inter : upperBounds s ∪ upperBounds t ⊆ upperBounds (s ∩ t) := union_subset (upperBounds_mono_set inter_subset_left) (upperBounds_mono_set inter_subset_right) theorem union_lowerBounds_subset_lowerBounds_inter : lowerBounds s ∪ lowerBounds t ⊆ lowerBounds (s ∩ t) := @union_upperBounds_subset_upperBounds_inter αᵒᵈ _ s t theorem isLeast_union_iff {a : α} {s t : Set α} : IsLeast (s ∪ t) a ↔ IsLeast s a ∧ a ∈ lowerBounds t ∨ a ∈ lowerBounds s ∧ IsLeast t a := by simp [IsLeast, lowerBounds_union, or_and_right, and_comm (a := a ∈ t), and_assoc] theorem isGreatest_union_iff : IsGreatest (s ∪ t) a ↔ IsGreatest s a ∧ a ∈ upperBounds t ∨ a ∈ upperBounds s ∧ IsGreatest t a := @isLeast_union_iff αᵒᵈ _ a s t /-- If `s` is bounded, then so is `s ∩ t` -/ theorem BddAbove.inter_of_left (h : BddAbove s) : BddAbove (s ∩ t) := h.mono inter_subset_left /-- If `t` is bounded, then so is `s ∩ t` -/ theorem BddAbove.inter_of_right (h : BddAbove t) : BddAbove (s ∩ t) := h.mono inter_subset_right /-- If `s` is bounded, then so is `s ∩ t` -/ theorem BddBelow.inter_of_left (h : BddBelow s) : BddBelow (s ∩ t) := h.mono inter_subset_left /-- If `t` is bounded, then so is `s ∩ t` -/ theorem BddBelow.inter_of_right (h : BddBelow t) : BddBelow (s ∩ t) := h.mono inter_subset_right /-- In a directed order, the union of bounded above sets is bounded above. -/ theorem BddAbove.union [IsDirected α (· ≤ ·)] {s t : Set α} : BddAbove s → BddAbove t → BddAbove (s ∪ t) := by rintro ⟨a, ha⟩ ⟨b, hb⟩ obtain ⟨c, hca, hcb⟩ := exists_ge_ge a b rw [BddAbove, upperBounds_union] exact ⟨c, upperBounds_mono_mem hca ha, upperBounds_mono_mem hcb hb⟩ /-- In a directed order, the union of two sets is bounded above if and only if both sets are. -/ theorem bddAbove_union [IsDirected α (· ≤ ·)] {s t : Set α} : BddAbove (s ∪ t) ↔ BddAbove s ∧ BddAbove t := ⟨fun h => ⟨h.mono subset_union_left, h.mono subset_union_right⟩, fun h => h.1.union h.2⟩ /-- In a codirected order, the union of bounded below sets is bounded below. -/ theorem BddBelow.union [IsDirected α (· ≥ ·)] {s t : Set α} : BddBelow s → BddBelow t → BddBelow (s ∪ t) := @BddAbove.union αᵒᵈ _ _ _ _ /-- In a codirected order, the union of two sets is bounded below if and only if both sets are. -/ theorem bddBelow_union [IsDirected α (· ≥ ·)] {s t : Set α} : BddBelow (s ∪ t) ↔ BddBelow s ∧ BddBelow t := @bddAbove_union αᵒᵈ _ _ _ _ /-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`, then `a ⊔ b` is the least upper bound of `s ∪ t`. -/ theorem IsLUB.union [SemilatticeSup γ] {a b : γ} {s t : Set γ} (hs : IsLUB s a) (ht : IsLUB t b) : IsLUB (s ∪ t) (a ⊔ b) := ⟨fun _ h => h.casesOn (fun h => le_sup_of_le_left <| hs.left h) fun h => le_sup_of_le_right <| ht.left h, fun _ hc => sup_le (hs.right fun _ hd => hc <| Or.inl hd) (ht.right fun _ hd => hc <| Or.inr hd)⟩ /-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`, then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/ theorem IsGLB.union [SemilatticeInf γ] {a₁ a₂ : γ} {s t : Set γ} (hs : IsGLB s a₁) (ht : IsGLB t a₂) : IsGLB (s ∪ t) (a₁ ⊓ a₂) := hs.dual.union ht /-- If `a` is the least element of `s` and `b` is the least element of `t`, then `min a b` is the least element of `s ∪ t`. -/ theorem IsLeast.union [LinearOrder γ] {a b : γ} {s t : Set γ} (ha : IsLeast s a) (hb : IsLeast t b) : IsLeast (s ∪ t) (min a b) := ⟨by rcases le_total a b with h | h <;> simp [h, ha.1, hb.1], (ha.isGLB.union hb.isGLB).1⟩ /-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`, then `max a b` is the greatest element of `s ∪ t`. -/ theorem IsGreatest.union [LinearOrder γ] {a b : γ} {s t : Set γ} (ha : IsGreatest s a) (hb : IsGreatest t b) : IsGreatest (s ∪ t) (max a b) := ⟨by rcases le_total a b with h | h <;> simp [h, ha.1, hb.1], (ha.isLUB.union hb.isLUB).1⟩ theorem IsLUB.inter_Ici_of_mem [LinearOrder γ] {s : Set γ} {a b : γ} (ha : IsLUB s a) (hb : b ∈ s) : IsLUB (s ∩ Ici b) a := ⟨fun _ hx => ha.1 hx.1, fun c hc => have hbc : b ≤ c := hc ⟨hb, le_rfl⟩ ha.2 fun x hx => ((le_total x b).elim fun hxb => hxb.trans hbc) fun hbx => hc ⟨hx, hbx⟩⟩ theorem IsGLB.inter_Iic_of_mem [LinearOrder γ] {s : Set γ} {a b : γ} (ha : IsGLB s a) (hb : b ∈ s) : IsGLB (s ∩ Iic b) a := ha.dual.inter_Ici_of_mem hb theorem bddAbove_iff_exists_ge [SemilatticeSup γ] {s : Set γ} (x₀ : γ) : BddAbove s ↔ ∃ x, x₀ ≤ x ∧ ∀ y ∈ s, y ≤ x := by rw [bddAbove_def, exists_ge_and_iff_exists] exact Monotone.ball fun x _ => monotone_le theorem bddBelow_iff_exists_le [SemilatticeInf γ] {s : Set γ} (x₀ : γ) : BddBelow s ↔ ∃ x, x ≤ x₀ ∧ ∀ y ∈ s, x ≤ y := bddAbove_iff_exists_ge (toDual x₀) theorem BddAbove.exists_ge [SemilatticeSup γ] {s : Set γ} (hs : BddAbove s) (x₀ : γ) : ∃ x, x₀ ≤ x ∧ ∀ y ∈ s, y ≤ x := (bddAbove_iff_exists_ge x₀).mp hs theorem BddBelow.exists_le [SemilatticeInf γ] {s : Set γ} (hs : BddBelow s) (x₀ : γ) : ∃ x, x ≤ x₀ ∧ ∀ y ∈ s, x ≤ y := (bddBelow_iff_exists_le x₀).mp hs /-! ### Specific sets #### Unbounded intervals -/ theorem isLeast_Ici : IsLeast (Ici a) a := ⟨left_mem_Ici, fun _ => id⟩ theorem isGreatest_Iic : IsGreatest (Iic a) a := ⟨right_mem_Iic, fun _ => id⟩ theorem isLUB_Iic : IsLUB (Iic a) a := isGreatest_Iic.isLUB theorem isGLB_Ici : IsGLB (Ici a) a := isLeast_Ici.isGLB theorem upperBounds_Iic : upperBounds (Iic a) = Ici a := isLUB_Iic.upperBounds_eq theorem lowerBounds_Ici : lowerBounds (Ici a) = Iic a := isGLB_Ici.lowerBounds_eq theorem bddAbove_Iic : BddAbove (Iic a) := isLUB_Iic.bddAbove theorem bddBelow_Ici : BddBelow (Ici a) := isGLB_Ici.bddBelow theorem bddAbove_Iio : BddAbove (Iio a) := ⟨a, fun _ hx => le_of_lt hx⟩ theorem bddBelow_Ioi : BddBelow (Ioi a) := ⟨a, fun _ hx => le_of_lt hx⟩ theorem lub_Iio_le (a : α) (hb : IsLUB (Iio a) b) : b ≤ a := (isLUB_le_iff hb).mpr fun _ hk => le_of_lt hk theorem le_glb_Ioi (a : α) (hb : IsGLB (Ioi a) b) : a ≤ b := @lub_Iio_le αᵒᵈ _ _ a hb theorem lub_Iio_eq_self_or_Iio_eq_Iic [PartialOrder γ] {j : γ} (i : γ) (hj : IsLUB (Iio i) j) : j = i ∨ Iio i = Iic j := by rcases eq_or_lt_of_le (lub_Iio_le i hj) with hj_eq_i | hj_lt_i · exact Or.inl hj_eq_i · right exact Set.ext fun k => ⟨fun hk_lt => hj.1 hk_lt, fun hk_le_j => lt_of_le_of_lt hk_le_j hj_lt_i⟩ theorem glb_Ioi_eq_self_or_Ioi_eq_Ici [PartialOrder γ] {j : γ} (i : γ) (hj : IsGLB (Ioi i) j) : j = i ∨ Ioi i = Ici j := @lub_Iio_eq_self_or_Iio_eq_Iic γᵒᵈ _ j i hj section variable [LinearOrder γ] theorem exists_lub_Iio (i : γ) : ∃ j, IsLUB (Iio i) j := by by_cases h_exists_lt : ∃ j, j ∈ upperBounds (Iio i) ∧ j < i · obtain ⟨j, hj_ub, hj_lt_i⟩ := h_exists_lt exact ⟨j, hj_ub, fun k hk_ub => hk_ub hj_lt_i⟩ · refine ⟨i, fun j hj => le_of_lt hj, ?_⟩ rw [mem_lowerBounds] by_contra h refine h_exists_lt ?_ push_neg at h exact h theorem exists_glb_Ioi (i : γ) : ∃ j, IsGLB (Ioi i) j := @exists_lub_Iio γᵒᵈ _ i variable [DenselyOrdered γ] theorem isLUB_Iio {a : γ} : IsLUB (Iio a) a := ⟨fun _ hx => le_of_lt hx, fun _ hy => le_of_forall_lt_imp_le_of_dense hy⟩ theorem isGLB_Ioi {a : γ} : IsGLB (Ioi a) a := @isLUB_Iio γᵒᵈ _ _ a theorem upperBounds_Iio {a : γ} : upperBounds (Iio a) = Ici a := isLUB_Iio.upperBounds_eq theorem lowerBounds_Ioi {a : γ} : lowerBounds (Ioi a) = Iic a := isGLB_Ioi.lowerBounds_eq end /-! #### Singleton -/ @[simp] theorem isGreatest_singleton : IsGreatest {a} a := ⟨mem_singleton a, fun _ hx => le_of_eq <| eq_of_mem_singleton hx⟩ @[simp] theorem isLeast_singleton : IsLeast {a} a := @isGreatest_singleton αᵒᵈ _ a @[simp] theorem isLUB_singleton : IsLUB {a} a := isGreatest_singleton.isLUB @[simp] theorem isGLB_singleton : IsGLB {a} a := isLeast_singleton.isGLB @[simp] lemma bddAbove_singleton : BddAbove ({a} : Set α) := isLUB_singleton.bddAbove @[simp] lemma bddBelow_singleton : BddBelow ({a} : Set α) := isGLB_singleton.bddBelow @[simp] theorem upperBounds_singleton : upperBounds {a} = Ici a := isLUB_singleton.upperBounds_eq @[simp] theorem lowerBounds_singleton : lowerBounds {a} = Iic a := isGLB_singleton.lowerBounds_eq /-! #### Bounded intervals -/ theorem bddAbove_Icc : BddAbove (Icc a b) := ⟨b, fun _ => And.right⟩ theorem bddBelow_Icc : BddBelow (Icc a b) := ⟨a, fun _ => And.left⟩ theorem bddAbove_Ico : BddAbove (Ico a b) := bddAbove_Icc.mono Ico_subset_Icc_self theorem bddBelow_Ico : BddBelow (Ico a b) := bddBelow_Icc.mono Ico_subset_Icc_self theorem bddAbove_Ioc : BddAbove (Ioc a b) := bddAbove_Icc.mono Ioc_subset_Icc_self theorem bddBelow_Ioc : BddBelow (Ioc a b) := bddBelow_Icc.mono Ioc_subset_Icc_self theorem bddAbove_Ioo : BddAbove (Ioo a b) := bddAbove_Icc.mono Ioo_subset_Icc_self theorem bddBelow_Ioo : BddBelow (Ioo a b) := bddBelow_Icc.mono Ioo_subset_Icc_self theorem isGreatest_Icc (h : a ≤ b) : IsGreatest (Icc a b) b := ⟨right_mem_Icc.2 h, fun _ => And.right⟩ theorem isLUB_Icc (h : a ≤ b) : IsLUB (Icc a b) b := (isGreatest_Icc h).isLUB theorem upperBounds_Icc (h : a ≤ b) : upperBounds (Icc a b) = Ici b := (isLUB_Icc h).upperBounds_eq theorem isLeast_Icc (h : a ≤ b) : IsLeast (Icc a b) a := ⟨left_mem_Icc.2 h, fun _ => And.left⟩ theorem isGLB_Icc (h : a ≤ b) : IsGLB (Icc a b) a := (isLeast_Icc h).isGLB theorem lowerBounds_Icc (h : a ≤ b) : lowerBounds (Icc a b) = Iic a := (isGLB_Icc h).lowerBounds_eq theorem isGreatest_Ioc (h : a < b) : IsGreatest (Ioc a b) b := ⟨right_mem_Ioc.2 h, fun _ => And.right⟩ theorem isLUB_Ioc (h : a < b) : IsLUB (Ioc a b) b := (isGreatest_Ioc h).isLUB theorem upperBounds_Ioc (h : a < b) : upperBounds (Ioc a b) = Ici b := (isLUB_Ioc h).upperBounds_eq theorem isLeast_Ico (h : a < b) : IsLeast (Ico a b) a := ⟨left_mem_Ico.2 h, fun _ => And.left⟩ theorem isGLB_Ico (h : a < b) : IsGLB (Ico a b) a := (isLeast_Ico h).isGLB theorem lowerBounds_Ico (h : a < b) : lowerBounds (Ico a b) = Iic a := (isGLB_Ico h).lowerBounds_eq section variable [SemilatticeSup γ] [DenselyOrdered γ] theorem isGLB_Ioo {a b : γ} (h : a < b) : IsGLB (Ioo a b) a := ⟨fun _ hx => hx.1.le, fun x hx => by rcases eq_or_lt_of_le (le_sup_right : a ≤ x ⊔ a) with h₁ | h₂ · exact h₁.symm ▸ le_sup_left obtain ⟨y, lty, ylt⟩ := exists_between h₂ apply (not_lt_of_le (sup_le (hx ⟨lty, ylt.trans_le (sup_le _ h.le)⟩) lty.le) ylt).elim obtain ⟨u, au, ub⟩ := exists_between h apply (hx ⟨au, ub⟩).trans ub.le⟩ theorem lowerBounds_Ioo {a b : γ} (hab : a < b) : lowerBounds (Ioo a b) = Iic a := (isGLB_Ioo hab).lowerBounds_eq theorem isGLB_Ioc {a b : γ} (hab : a < b) : IsGLB (Ioc a b) a := (isGLB_Ioo hab).of_subset_of_superset (isGLB_Icc hab.le) Ioo_subset_Ioc_self Ioc_subset_Icc_self theorem lowerBounds_Ioc {a b : γ} (hab : a < b) : lowerBounds (Ioc a b) = Iic a := (isGLB_Ioc hab).lowerBounds_eq end section variable [SemilatticeInf γ] [DenselyOrdered γ] theorem isLUB_Ioo {a b : γ} (hab : a < b) : IsLUB (Ioo a b) b := by simpa only [Ioo_toDual] using isGLB_Ioo hab.dual theorem upperBounds_Ioo {a b : γ} (hab : a < b) : upperBounds (Ioo a b) = Ici b := (isLUB_Ioo hab).upperBounds_eq theorem isLUB_Ico {a b : γ} (hab : a < b) : IsLUB (Ico a b) b := by simpa only [Ioc_toDual] using isGLB_Ioc hab.dual theorem upperBounds_Ico {a b : γ} (hab : a < b) : upperBounds (Ico a b) = Ici b := (isLUB_Ico hab).upperBounds_eq end theorem bddBelow_iff_subset_Ici : BddBelow s ↔ ∃ a, s ⊆ Ici a := Iff.rfl theorem bddAbove_iff_subset_Iic : BddAbove s ↔ ∃ a, s ⊆ Iic a := Iff.rfl theorem bddBelow_bddAbove_iff_subset_Icc : BddBelow s ∧ BddAbove s ↔ ∃ a b, s ⊆ Icc a b := by simp [Ici_inter_Iic.symm, subset_inter_iff, bddBelow_iff_subset_Ici, bddAbove_iff_subset_Iic, exists_and_left, exists_and_right] /-! #### Univ -/ @[simp] theorem isGreatest_univ_iff : IsGreatest univ a ↔ IsTop a := by simp [IsGreatest, mem_upperBounds, IsTop] theorem isGreatest_univ [OrderTop α] : IsGreatest (univ : Set α) ⊤ := isGreatest_univ_iff.2 isTop_top @[simp] theorem OrderTop.upperBounds_univ [PartialOrder γ] [OrderTop γ] : upperBounds (univ : Set γ) = {⊤} := by rw [isGreatest_univ.upperBounds_eq, Ici_top] theorem isLUB_univ [OrderTop α] : IsLUB (univ : Set α) ⊤ := isGreatest_univ.isLUB @[simp] theorem OrderBot.lowerBounds_univ [PartialOrder γ] [OrderBot γ] : lowerBounds (univ : Set γ) = {⊥} := @OrderTop.upperBounds_univ γᵒᵈ _ _ @[simp] theorem isLeast_univ_iff : IsLeast univ a ↔ IsBot a := @isGreatest_univ_iff αᵒᵈ _ _ theorem isLeast_univ [OrderBot α] : IsLeast (univ : Set α) ⊥ := @isGreatest_univ αᵒᵈ _ _ theorem isGLB_univ [OrderBot α] : IsGLB (univ : Set α) ⊥ := isLeast_univ.isGLB @[simp] theorem NoTopOrder.upperBounds_univ [NoTopOrder α] : upperBounds (univ : Set α) = ∅ := eq_empty_of_subset_empty fun b hb => not_isTop b fun x => hb (mem_univ x) @[deprecated (since := "2025-04-18")] alias NoMaxOrder.upperBounds_univ := NoTopOrder.upperBounds_univ @[simp] theorem NoBotOrder.lowerBounds_univ [NoBotOrder α] : lowerBounds (univ : Set α) = ∅ := @NoTopOrder.upperBounds_univ αᵒᵈ _ _ @[deprecated (since := "2025-04-18")] alias NoMinOrder.lowerBounds_univ := NoBotOrder.lowerBounds_univ @[simp] theorem not_bddAbove_univ [NoTopOrder α] : ¬BddAbove (univ : Set α) := by simp [BddAbove] @[simp] theorem not_bddBelow_univ [NoBotOrder α] : ¬BddBelow (univ : Set α) := @not_bddAbove_univ αᵒᵈ _ _ /-! #### Empty set -/ @[simp] theorem upperBounds_empty : upperBounds (∅ : Set α) = univ := by simp only [upperBounds, eq_univ_iff_forall, mem_setOf_eq, forall_mem_empty, forall_true_iff] @[simp] theorem lowerBounds_empty : lowerBounds (∅ : Set α) = univ := @upperBounds_empty αᵒᵈ _ @[simp] theorem bddAbove_empty [Nonempty α] : BddAbove (∅ : Set α) := by simp only [BddAbove, upperBounds_empty, univ_nonempty] @[simp] theorem bddBelow_empty [Nonempty α] : BddBelow (∅ : Set α) := by simp only [BddBelow, lowerBounds_empty, univ_nonempty] @[simp] theorem isGLB_empty_iff : IsGLB ∅ a ↔ IsTop a := by simp [IsGLB] @[simp] theorem isLUB_empty_iff : IsLUB ∅ a ↔ IsBot a := @isGLB_empty_iff αᵒᵈ _ _ theorem isGLB_empty [OrderTop α] : IsGLB ∅ (⊤ : α) := isGLB_empty_iff.2 isTop_top theorem isLUB_empty [OrderBot α] : IsLUB ∅ (⊥ : α) := @isGLB_empty αᵒᵈ _ _ theorem IsLUB.nonempty [NoBotOrder α] (hs : IsLUB s a) : s.Nonempty := nonempty_iff_ne_empty.2 fun h => not_isBot a fun _ => hs.right <| by rw [h, upperBounds_empty]; exact mem_univ _ theorem IsGLB.nonempty [NoTopOrder α] (hs : IsGLB s a) : s.Nonempty := hs.dual.nonempty theorem nonempty_of_not_bddAbove [ha : Nonempty α] (h : ¬BddAbove s) : s.Nonempty := (Nonempty.elim ha) fun x => (not_bddAbove_iff'.1 h x).imp fun _ ha => ha.1 theorem nonempty_of_not_bddBelow [Nonempty α] (h : ¬BddBelow s) : s.Nonempty := @nonempty_of_not_bddAbove αᵒᵈ _ _ _ h /-! #### insert -/ /-- Adding a point to a set preserves its boundedness above. -/ @[simp] theorem bddAbove_insert [IsDirected α (· ≤ ·)] {s : Set α} {a : α} : BddAbove (insert a s) ↔ BddAbove s := by simp only [insert_eq, bddAbove_union, bddAbove_singleton, true_and] protected theorem BddAbove.insert [IsDirected α (· ≤ ·)] {s : Set α} (a : α) : BddAbove s → BddAbove (insert a s) := bddAbove_insert.2 /-- Adding a point to a set preserves its boundedness below. -/ @[simp] theorem bddBelow_insert [IsDirected α (· ≥ ·)] {s : Set α} {a : α} : BddBelow (insert a s) ↔ BddBelow s := by simp only [insert_eq, bddBelow_union, bddBelow_singleton, true_and] protected theorem BddBelow.insert [IsDirected α (· ≥ ·)] {s : Set α} (a : α) : BddBelow s → BddBelow (insert a s) := bddBelow_insert.2 protected theorem IsLUB.insert [SemilatticeSup γ] (a) {b} {s : Set γ} (hs : IsLUB s b) : IsLUB (insert a s) (a ⊔ b) := by rw [insert_eq] exact isLUB_singleton.union hs protected theorem IsGLB.insert [SemilatticeInf γ] (a) {b} {s : Set γ} (hs : IsGLB s b) : IsGLB (insert a s) (a ⊓ b) := by rw [insert_eq] exact isGLB_singleton.union hs protected theorem IsGreatest.insert [LinearOrder γ] (a) {b} {s : Set γ} (hs : IsGreatest s b) : IsGreatest (insert a s) (max a b) := by rw [insert_eq] exact isGreatest_singleton.union hs protected theorem IsLeast.insert [LinearOrder γ] (a) {b} {s : Set γ} (hs : IsLeast s b) : IsLeast (insert a s) (min a b) := by rw [insert_eq] exact isLeast_singleton.union hs @[simp] theorem upperBounds_insert (a : α) (s : Set α) : upperBounds (insert a s) = Ici a ∩ upperBounds s := by rw [insert_eq, upperBounds_union, upperBounds_singleton] @[simp] theorem lowerBounds_insert (a : α) (s : Set α) : lowerBounds (insert a s) = Iic a ∩ lowerBounds s := by rw [insert_eq, lowerBounds_union, lowerBounds_singleton] /-- When there is a global maximum, every set is bounded above. -/ @[simp] protected theorem OrderTop.bddAbove [OrderTop α] (s : Set α) : BddAbove s := ⟨⊤, fun a _ => OrderTop.le_top a⟩ /-- When there is a global minimum, every set is bounded below. -/ @[simp] protected theorem OrderBot.bddBelow [OrderBot α] (s : Set α) : BddBelow s := ⟨⊥, fun a _ => OrderBot.bot_le a⟩ /-- Sets are automatically bounded or cobounded in complete lattices. To use the same statements in complete and conditionally complete lattices but let automation fill automatically the boundedness proofs in complete lattices, we use the tactic `bddDefault` in the statements, in the form `(hA : BddAbove A := by bddDefault)`. -/ macro "bddDefault" : tactic => `(tactic| first | apply OrderTop.bddAbove | apply OrderBot.bddBelow) /-! #### Pair -/ theorem isLUB_pair [SemilatticeSup γ] {a b : γ} : IsLUB {a, b} (a ⊔ b) := isLUB_singleton.insert _ theorem isGLB_pair [SemilatticeInf γ] {a b : γ} : IsGLB {a, b} (a ⊓ b) := isGLB_singleton.insert _ theorem isLeast_pair [LinearOrder γ] {a b : γ} : IsLeast {a, b} (min a b) := isLeast_singleton.insert _ theorem isGreatest_pair [LinearOrder γ] {a b : γ} : IsGreatest {a, b} (max a b) := isGreatest_singleton.insert _ /-! #### Lower/upper bounds -/ @[simp] theorem isLUB_lowerBounds : IsLUB (lowerBounds s) a ↔ IsGLB s a := ⟨fun H => ⟨fun _ hx => H.2 <| subset_upperBounds_lowerBounds s hx, H.1⟩, IsGreatest.isLUB⟩ @[simp] theorem isGLB_upperBounds : IsGLB (upperBounds s) a ↔ IsLUB s a := @isLUB_lowerBounds αᵒᵈ _ _ _ end /-! ### (In)equalities with the least upper bound and the greatest lower bound -/ section Preorder variable [Preorder α] {s : Set α} {a b : α} theorem lowerBounds_le_upperBounds (ha : a ∈ lowerBounds s) (hb : b ∈ upperBounds s) : s.Nonempty → a ≤ b | ⟨_, hc⟩ => le_trans (ha hc) (hb hc) theorem isGLB_le_isLUB (ha : IsGLB s a) (hb : IsLUB s b) (hs : s.Nonempty) : a ≤ b := lowerBounds_le_upperBounds ha.1 hb.1 hs theorem isLUB_lt_iff (ha : IsLUB s a) : a < b ↔ ∃ c ∈ upperBounds s, c < b := ⟨fun hb => ⟨a, ha.1, hb⟩, fun ⟨_, hcs, hcb⟩ => lt_of_le_of_lt (ha.2 hcs) hcb⟩ theorem lt_isGLB_iff (ha : IsGLB s a) : b < a ↔ ∃ c ∈ lowerBounds s, b < c := isLUB_lt_iff ha.dual theorem le_of_isLUB_le_isGLB {x y} (ha : IsGLB s a) (hb : IsLUB s b) (hab : b ≤ a) (hx : x ∈ s) (hy : y ∈ s) : x ≤ y := calc x ≤ b := hb.1 hx _ ≤ a := hab _ ≤ y := ha.1 hy end Preorder section PartialOrder variable [PartialOrder α] {s : Set α} {a b : α} theorem IsLeast.unique (Ha : IsLeast s a) (Hb : IsLeast s b) : a = b := le_antisymm (Ha.right Hb.left) (Hb.right Ha.left) theorem IsLeast.isLeast_iff_eq (Ha : IsLeast s a) : IsLeast s b ↔ a = b := Iff.intro Ha.unique fun h => h ▸ Ha theorem IsGreatest.unique (Ha : IsGreatest s a) (Hb : IsGreatest s b) : a = b := le_antisymm (Hb.right Ha.left) (Ha.right Hb.left) theorem IsGreatest.isGreatest_iff_eq (Ha : IsGreatest s a) : IsGreatest s b ↔ a = b := Iff.intro Ha.unique fun h => h ▸ Ha theorem IsLUB.unique (Ha : IsLUB s a) (Hb : IsLUB s b) : a = b := IsLeast.unique Ha Hb theorem IsGLB.unique (Ha : IsGLB s a) (Hb : IsGLB s b) : a = b := IsGreatest.unique Ha Hb theorem Set.subsingleton_of_isLUB_le_isGLB (Ha : IsGLB s a) (Hb : IsLUB s b) (hab : b ≤ a) : s.Subsingleton := fun _ hx _ hy => le_antisymm (le_of_isLUB_le_isGLB Ha Hb hab hx hy) (le_of_isLUB_le_isGLB Ha Hb hab hy hx) theorem isGLB_lt_isLUB_of_ne (Ha : IsGLB s a) (Hb : IsLUB s b) {x y} (Hx : x ∈ s) (Hy : y ∈ s) (Hxy : x ≠ y) : a < b := lt_iff_le_not_le.2 ⟨lowerBounds_le_upperBounds Ha.1 Hb.1 ⟨x, Hx⟩, fun hab => Hxy <| Set.subsingleton_of_isLUB_le_isGLB Ha Hb hab Hx Hy⟩ end PartialOrder section LinearOrder variable [LinearOrder α] {s : Set α} {a b : α} theorem lt_isLUB_iff (h : IsLUB s a) : b < a ↔ ∃ c ∈ s, b < c := by simp_rw [← not_le, isLUB_le_iff h, mem_upperBounds, not_forall, not_le, exists_prop] theorem isGLB_lt_iff (h : IsGLB s a) : a < b ↔ ∃ c ∈ s, c < b := lt_isLUB_iff h.dual theorem IsLUB.exists_between (h : IsLUB s a) (hb : b < a) : ∃ c ∈ s, b < c ∧ c ≤ a := let ⟨c, hcs, hbc⟩ := (lt_isLUB_iff h).1 hb ⟨c, hcs, hbc, h.1 hcs⟩ theorem IsLUB.exists_between' (h : IsLUB s a) (h' : a ∉ s) (hb : b < a) : ∃ c ∈ s, b < c ∧ c < a := let ⟨c, hcs, hbc, hca⟩ := h.exists_between hb ⟨c, hcs, hbc, hca.lt_of_ne fun hac => h' <| hac ▸ hcs⟩ theorem IsGLB.exists_between (h : IsGLB s a) (hb : a < b) : ∃ c ∈ s, a ≤ c ∧ c < b := let ⟨c, hcs, hbc⟩ := (isGLB_lt_iff h).1 hb ⟨c, hcs, h.1 hcs, hbc⟩ theorem IsGLB.exists_between' (h : IsGLB s a) (h' : a ∉ s) (hb : a < b) : ∃ c ∈ s, a < c ∧ c < b := let ⟨c, hcs, hac, hcb⟩ := h.exists_between hb ⟨c, hcs, hac.lt_of_ne fun hac => h' <| hac.symm ▸ hcs, hcb⟩ end LinearOrder theorem isGreatest_himp [GeneralizedHeytingAlgebra α] (a b : α) : IsGreatest {w | w ⊓ a ≤ b} (a ⇨ b) := by simp [IsGreatest, mem_upperBounds] theorem isLeast_sdiff [GeneralizedCoheytingAlgebra α] (a b : α) : IsLeast {w | a ≤ b ⊔ w} (a \ b) := by simp [IsLeast, mem_lowerBounds] theorem isGreatest_compl [HeytingAlgebra α] (a : α) : IsGreatest {w | Disjoint w a} (aᶜ) := by simpa only [himp_bot, disjoint_iff_inf_le] using isGreatest_himp a ⊥ theorem isLeast_hnot [CoheytingAlgebra α] (a : α) : IsLeast {w | Codisjoint a w} (¬a) := by simpa only [CoheytingAlgebra.top_sdiff, codisjoint_iff_le_sup] using isLeast_sdiff ⊤ a
Mathlib/Order/Bounds/Basic.lean
1,714
1,720
/- 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) :=
Mathlib/Topology/Connected/Basic.lean
503
508
/- Copyright (c) 2022 Yaël Dillies, Sara Rousta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Sara Rousta -/ import Mathlib.Logic.Equiv.Set import Mathlib.Order.Interval.Set.OrderEmbedding import Mathlib.Order.SetNotation /-! # Properties of unbundled upper/lower sets This file proves results on `IsUpperSet` and `IsLowerSet`, including their interactions with set operations, images, preimages and order duals, and properties that reflect stronger assumptions on the underlying order (such as `PartialOrder` and `LinearOrder`). ## TODO * Lattice structure on antichains. * Order equivalence between upper/lower sets and antichains. -/ open OrderDual Set variable {α β : Type*} {ι : Sort*} {κ : ι → Sort*} attribute [aesop norm unfold] IsUpperSet IsLowerSet section LE variable [LE α] {s t : Set α} {a : α} theorem isUpperSet_empty : IsUpperSet (∅ : Set α) := fun _ _ _ => id theorem isLowerSet_empty : IsLowerSet (∅ : Set α) := fun _ _ _ => id theorem isUpperSet_univ : IsUpperSet (univ : Set α) := fun _ _ _ => id theorem isLowerSet_univ : IsLowerSet (univ : Set α) := fun _ _ _ => id theorem IsUpperSet.compl (hs : IsUpperSet s) : IsLowerSet sᶜ := fun _a _b h hb ha => hb <| hs h ha theorem IsLowerSet.compl (hs : IsLowerSet s) : IsUpperSet sᶜ := fun _a _b h hb ha => hb <| hs h ha @[simp] theorem isUpperSet_compl : IsUpperSet sᶜ ↔ IsLowerSet s := ⟨fun h => by convert h.compl rw [compl_compl], IsLowerSet.compl⟩ @[simp] theorem isLowerSet_compl : IsLowerSet sᶜ ↔ IsUpperSet s := ⟨fun h => by convert h.compl rw [compl_compl], IsUpperSet.compl⟩ theorem IsUpperSet.union (hs : IsUpperSet s) (ht : IsUpperSet t) : IsUpperSet (s ∪ t) := fun _ _ h => Or.imp (hs h) (ht h) theorem IsLowerSet.union (hs : IsLowerSet s) (ht : IsLowerSet t) : IsLowerSet (s ∪ t) := fun _ _ h => Or.imp (hs h) (ht h) theorem IsUpperSet.inter (hs : IsUpperSet s) (ht : IsUpperSet t) : IsUpperSet (s ∩ t) := fun _ _ h => And.imp (hs h) (ht h) theorem IsLowerSet.inter (hs : IsLowerSet s) (ht : IsLowerSet t) : IsLowerSet (s ∩ t) := fun _ _ h => And.imp (hs h) (ht h) theorem isUpperSet_sUnion {S : Set (Set α)} (hf : ∀ s ∈ S, IsUpperSet s) : IsUpperSet (⋃₀ S) := fun _ _ h => Exists.imp fun _ hs => ⟨hs.1, hf _ hs.1 h hs.2⟩ theorem isLowerSet_sUnion {S : Set (Set α)} (hf : ∀ s ∈ S, IsLowerSet s) : IsLowerSet (⋃₀ S) := fun _ _ h => Exists.imp fun _ hs => ⟨hs.1, hf _ hs.1 h hs.2⟩ theorem isUpperSet_iUnion {f : ι → Set α} (hf : ∀ i, IsUpperSet (f i)) : IsUpperSet (⋃ i, f i) := isUpperSet_sUnion <| forall_mem_range.2 hf theorem isLowerSet_iUnion {f : ι → Set α} (hf : ∀ i, IsLowerSet (f i)) : IsLowerSet (⋃ i, f i) := isLowerSet_sUnion <| forall_mem_range.2 hf theorem isUpperSet_iUnion₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsUpperSet (f i j)) : IsUpperSet (⋃ (i) (j), f i j) := isUpperSet_iUnion fun i => isUpperSet_iUnion <| hf i theorem isLowerSet_iUnion₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsLowerSet (f i j)) : IsLowerSet (⋃ (i) (j), f i j) := isLowerSet_iUnion fun i => isLowerSet_iUnion <| hf i theorem isUpperSet_sInter {S : Set (Set α)} (hf : ∀ s ∈ S, IsUpperSet s) : IsUpperSet (⋂₀ S) := fun _ _ h => forall₂_imp fun s hs => hf s hs h theorem isLowerSet_sInter {S : Set (Set α)} (hf : ∀ s ∈ S, IsLowerSet s) : IsLowerSet (⋂₀ S) := fun _ _ h => forall₂_imp fun s hs => hf s hs h theorem isUpperSet_iInter {f : ι → Set α} (hf : ∀ i, IsUpperSet (f i)) : IsUpperSet (⋂ i, f i) := isUpperSet_sInter <| forall_mem_range.2 hf theorem isLowerSet_iInter {f : ι → Set α} (hf : ∀ i, IsLowerSet (f i)) : IsLowerSet (⋂ i, f i) := isLowerSet_sInter <| forall_mem_range.2 hf theorem isUpperSet_iInter₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsUpperSet (f i j)) : IsUpperSet (⋂ (i) (j), f i j) := isUpperSet_iInter fun i => isUpperSet_iInter <| hf i theorem isLowerSet_iInter₂ {f : ∀ i, κ i → Set α} (hf : ∀ i j, IsLowerSet (f i j)) : IsLowerSet (⋂ (i) (j), f i j) := isLowerSet_iInter fun i => isLowerSet_iInter <| hf i @[simp] theorem isLowerSet_preimage_ofDual_iff : IsLowerSet (ofDual ⁻¹' s) ↔ IsUpperSet s := Iff.rfl @[simp] theorem isUpperSet_preimage_ofDual_iff : IsUpperSet (ofDual ⁻¹' s) ↔ IsLowerSet s := Iff.rfl @[simp] theorem isLowerSet_preimage_toDual_iff {s : Set αᵒᵈ} : IsLowerSet (toDual ⁻¹' s) ↔ IsUpperSet s := Iff.rfl @[simp] theorem isUpperSet_preimage_toDual_iff {s : Set αᵒᵈ} : IsUpperSet (toDual ⁻¹' s) ↔ IsLowerSet s := Iff.rfl alias ⟨_, IsUpperSet.toDual⟩ := isLowerSet_preimage_ofDual_iff alias ⟨_, IsLowerSet.toDual⟩ := isUpperSet_preimage_ofDual_iff alias ⟨_, IsUpperSet.ofDual⟩ := isLowerSet_preimage_toDual_iff alias ⟨_, IsLowerSet.ofDual⟩ := isUpperSet_preimage_toDual_iff lemma IsUpperSet.isLowerSet_preimage_coe (hs : IsUpperSet s) : IsLowerSet ((↑) ⁻¹' t : Set s) ↔ ∀ b ∈ s, ∀ c ∈ t, b ≤ c → b ∈ t := by aesop lemma IsLowerSet.isUpperSet_preimage_coe (hs : IsLowerSet s) : IsUpperSet ((↑) ⁻¹' t : Set s) ↔ ∀ b ∈ s, ∀ c ∈ t, c ≤ b → b ∈ t := by aesop lemma IsUpperSet.sdiff (hs : IsUpperSet s) (ht : ∀ b ∈ s, ∀ c ∈ t, b ≤ c → b ∈ t) : IsUpperSet (s \ t) := fun _b _c hbc hb ↦ ⟨hs hbc hb.1, fun hc ↦ hb.2 <| ht _ hb.1 _ hc hbc⟩ lemma IsLowerSet.sdiff (hs : IsLowerSet s) (ht : ∀ b ∈ s, ∀ c ∈ t, c ≤ b → b ∈ t) : IsLowerSet (s \ t) := fun _b _c hcb hb ↦ ⟨hs hcb hb.1, fun hc ↦ hb.2 <| ht _ hb.1 _ hc hcb⟩ lemma IsUpperSet.sdiff_of_isLowerSet (hs : IsUpperSet s) (ht : IsLowerSet t) : IsUpperSet (s \ t) := hs.sdiff <| by aesop lemma IsLowerSet.sdiff_of_isUpperSet (hs : IsLowerSet s) (ht : IsUpperSet t) : IsLowerSet (s \ t) := hs.sdiff <| by aesop lemma IsUpperSet.erase (hs : IsUpperSet s) (has : ∀ b ∈ s, b ≤ a → b = a) : IsUpperSet (s \ {a}) := hs.sdiff <| by simpa using has lemma IsLowerSet.erase (hs : IsLowerSet s) (has : ∀ b ∈ s, a ≤ b → b = a) : IsLowerSet (s \ {a}) := hs.sdiff <| by simpa using has end LE section Preorder variable [Preorder α] [Preorder β] {s : Set α} {p : α → Prop} (a : α) theorem isUpperSet_Ici : IsUpperSet (Ici a) := fun _ _ => ge_trans theorem isLowerSet_Iic : IsLowerSet (Iic a) := fun _ _ => le_trans theorem isUpperSet_Ioi : IsUpperSet (Ioi a) := fun _ _ => flip lt_of_lt_of_le theorem isLowerSet_Iio : IsLowerSet (Iio a) := fun _ _ => lt_of_le_of_lt theorem isUpperSet_iff_Ici_subset : IsUpperSet s ↔ ∀ ⦃a⦄, a ∈ s → Ici a ⊆ s := by simp [IsUpperSet, subset_def, @forall_swap (_ ∈ s)] theorem isLowerSet_iff_Iic_subset : IsLowerSet s ↔ ∀ ⦃a⦄, a ∈ s → Iic a ⊆ s := by simp [IsLowerSet, subset_def, @forall_swap (_ ∈ s)] alias ⟨IsUpperSet.Ici_subset, _⟩ := isUpperSet_iff_Ici_subset alias ⟨IsLowerSet.Iic_subset, _⟩ := isLowerSet_iff_Iic_subset theorem IsUpperSet.Ioi_subset (h : IsUpperSet s) ⦃a⦄ (ha : a ∈ s) : Ioi a ⊆ s := Ioi_subset_Ici_self.trans <| h.Ici_subset ha theorem IsLowerSet.Iio_subset (h : IsLowerSet s) ⦃a⦄ (ha : a ∈ s) : Iio a ⊆ s := h.toDual.Ioi_subset ha theorem IsUpperSet.ordConnected (h : IsUpperSet s) : s.OrdConnected := ⟨fun _ ha _ _ => Icc_subset_Ici_self.trans <| h.Ici_subset ha⟩ theorem IsLowerSet.ordConnected (h : IsLowerSet s) : s.OrdConnected := ⟨fun _ _ _ hb => Icc_subset_Iic_self.trans <| h.Iic_subset hb⟩ theorem IsUpperSet.preimage (hs : IsUpperSet s) {f : β → α} (hf : Monotone f) : IsUpperSet (f ⁻¹' s : Set β) := fun _ _ h => hs <| hf h theorem IsLowerSet.preimage (hs : IsLowerSet s) {f : β → α} (hf : Monotone f) : IsLowerSet (f ⁻¹' s : Set β) := fun _ _ h => hs <| hf h theorem IsUpperSet.image (hs : IsUpperSet s) (f : α ≃o β) : IsUpperSet (f '' s : Set β) := by change IsUpperSet ((f : α ≃ β) '' s) rw [Set.image_equiv_eq_preimage_symm] exact hs.preimage f.symm.monotone theorem IsLowerSet.image (hs : IsLowerSet s) (f : α ≃o β) : IsLowerSet (f '' s : Set β) := by change IsLowerSet ((f : α ≃ β) '' s) rw [Set.image_equiv_eq_preimage_symm] exact hs.preimage f.symm.monotone theorem OrderEmbedding.image_Ici (e : α ↪o β) (he : IsUpperSet (range e)) (a : α) : e '' Ici a = Ici (e a) := by rw [← e.preimage_Ici, image_preimage_eq_inter_range, inter_eq_left.2 <| he.Ici_subset (mem_range_self _)] theorem OrderEmbedding.image_Iic (e : α ↪o β) (he : IsLowerSet (range e)) (a : α) : e '' Iic a = Iic (e a) := e.dual.image_Ici he a theorem OrderEmbedding.image_Ioi (e : α ↪o β) (he : IsUpperSet (range e)) (a : α) : e '' Ioi a = Ioi (e a) := by rw [← e.preimage_Ioi, image_preimage_eq_inter_range, inter_eq_left.2 <| he.Ioi_subset (mem_range_self _)] theorem OrderEmbedding.image_Iio (e : α ↪o β) (he : IsLowerSet (range e)) (a : α) : e '' Iio a = Iio (e a) := e.dual.image_Ioi he a @[simp] theorem Set.monotone_mem : Monotone (· ∈ s) ↔ IsUpperSet s := Iff.rfl @[simp] theorem Set.antitone_mem : Antitone (· ∈ s) ↔ IsLowerSet s := forall_swap @[simp] theorem isUpperSet_setOf : IsUpperSet { a | p a } ↔ Monotone p := Iff.rfl @[simp] theorem isLowerSet_setOf : IsLowerSet { a | p a } ↔ Antitone p := forall_swap lemma IsUpperSet.upperBounds_subset (hs : IsUpperSet s) : s.Nonempty → upperBounds s ⊆ s := fun ⟨_a, ha⟩ _b hb ↦ hs (hb ha) ha lemma IsLowerSet.lowerBounds_subset (hs : IsLowerSet s) : s.Nonempty → lowerBounds s ⊆ s := fun ⟨_a, ha⟩ _b hb ↦ hs (hb ha) ha section OrderTop variable [OrderTop α] theorem IsLowerSet.top_mem (hs : IsLowerSet s) : ⊤ ∈ s ↔ s = univ := ⟨fun h => eq_univ_of_forall fun _ => hs le_top h, fun h => h.symm ▸ mem_univ _⟩ theorem IsUpperSet.top_mem (hs : IsUpperSet s) : ⊤ ∈ s ↔ s.Nonempty := ⟨fun h => ⟨_, h⟩, fun ⟨_a, ha⟩ => hs le_top ha⟩ theorem IsUpperSet.not_top_mem (hs : IsUpperSet s) : ⊤ ∉ s ↔ s = ∅ := hs.top_mem.not.trans not_nonempty_iff_eq_empty end OrderTop section OrderBot variable [OrderBot α] theorem IsUpperSet.bot_mem (hs : IsUpperSet s) : ⊥ ∈ s ↔ s = univ := ⟨fun h => eq_univ_of_forall fun _ => hs bot_le h, fun h => h.symm ▸ mem_univ _⟩ theorem IsLowerSet.bot_mem (hs : IsLowerSet s) : ⊥ ∈ s ↔ s.Nonempty := ⟨fun h => ⟨_, h⟩, fun ⟨_a, ha⟩ => hs bot_le ha⟩ theorem IsLowerSet.not_bot_mem (hs : IsLowerSet s) : ⊥ ∉ s ↔ s = ∅ := hs.bot_mem.not.trans not_nonempty_iff_eq_empty end OrderBot section NoMaxOrder variable [NoMaxOrder α] theorem IsUpperSet.not_bddAbove (hs : IsUpperSet s) : s.Nonempty → ¬BddAbove s := by rintro ⟨a, ha⟩ ⟨b, hb⟩ obtain ⟨c, hc⟩ := exists_gt b exact hc.not_le (hb <| hs ((hb ha).trans hc.le) ha) theorem not_bddAbove_Ici : ¬BddAbove (Ici a) := (isUpperSet_Ici _).not_bddAbove nonempty_Ici theorem not_bddAbove_Ioi : ¬BddAbove (Ioi a) := (isUpperSet_Ioi _).not_bddAbove nonempty_Ioi end NoMaxOrder section NoMinOrder variable [NoMinOrder α] theorem IsLowerSet.not_bddBelow (hs : IsLowerSet s) : s.Nonempty → ¬BddBelow s := by rintro ⟨a, ha⟩ ⟨b, hb⟩ obtain ⟨c, hc⟩ := exists_lt b exact hc.not_le (hb <| hs (hc.le.trans <| hb ha) ha) theorem not_bddBelow_Iic : ¬BddBelow (Iic a) := (isLowerSet_Iic _).not_bddBelow nonempty_Iic theorem not_bddBelow_Iio : ¬BddBelow (Iio a) := (isLowerSet_Iio _).not_bddBelow nonempty_Iio end NoMinOrder end Preorder section PartialOrder variable [PartialOrder α] {s : Set α} theorem isUpperSet_iff_forall_lt : IsUpperSet s ↔ ∀ ⦃a b : α⦄, a < b → a ∈ s → b ∈ s := forall_congr' fun a => by simp [le_iff_eq_or_lt, or_imp, forall_and] theorem isLowerSet_iff_forall_lt : IsLowerSet s ↔ ∀ ⦃a b : α⦄, b < a → a ∈ s → b ∈ s := forall_congr' fun a => by simp [le_iff_eq_or_lt, or_imp, forall_and] theorem isUpperSet_iff_Ioi_subset : IsUpperSet s ↔ ∀ ⦃a⦄, a ∈ s → Ioi a ⊆ s := by simp [isUpperSet_iff_forall_lt, subset_def, @forall_swap (_ ∈ s)] theorem isLowerSet_iff_Iio_subset : IsLowerSet s ↔ ∀ ⦃a⦄, a ∈ s → Iio a ⊆ s := by simp [isLowerSet_iff_forall_lt, subset_def, @forall_swap (_ ∈ s)] end PartialOrder section LinearOrder variable [LinearOrder α] {s t : Set α} theorem IsUpperSet.total (hs : IsUpperSet s) (ht : IsUpperSet t) : s ⊆ t ∨ t ⊆ s := by by_contra! h simp_rw [Set.not_subset] at h obtain ⟨⟨a, has, hat⟩, b, hbt, hbs⟩ := h obtain hab | hba := le_total a b · exact hbs (hs hab has) · exact hat (ht hba hbt) theorem IsLowerSet.total (hs : IsLowerSet s) (ht : IsLowerSet t) : s ⊆ t ∨ t ⊆ s := hs.toDual.total ht.toDual end LinearOrder
Mathlib/Order/UpperLower/Basic.lean
1,906
1,908
/- 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.Ordmap.Invariants /-! # Verification of `Ordnode` This file uses the invariants defined in `Mathlib.Data.Ordmap.Invariants` to construct `Ordset α`, a wrapper around `Ordnode α` which includes the correctness invariant of the type. It exposes parallel operations like `insert` as functions on `Ordset` that do the same thing but bundle the correctness proofs. The advantage is that it is possible to, for example, prove that the result of `find` on `insert` will actually find the element, while `Ordnode` cannot guarantee this if the input tree did not satisfy the type invariants. ## Main definitions * `Ordnode.Valid`: The validity predicate for an `Ordnode` subtree. * `Ordset α`: A well formed set of values of type `α`. ## Implementation notes Because the `Ordnode` file was ported from Haskell, the correctness invariants of some of the functions have not been spelled out, and some theorems like `Ordnode.Valid'.balanceL_aux` show very intricate assumptions on the sizes, which may need to be revised if it turns out some operations violate these assumptions, because there is a decent amount of slop in the actual data structure invariants, so the theorem will go through with multiple choices of assumption. -/ variable {α : Type*} namespace Ordnode section Valid variable [Preorder α] /-- The validity predicate for an `Ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. This version of `Valid` also puts all elements in the tree in the interval `(lo, hi)`. -/ structure Valid' (lo : WithBot α) (t : Ordnode α) (hi : WithTop α) : Prop where ord : t.Bounded lo hi sz : t.Sized bal : t.Balanced /-- The validity predicate for an `Ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. -/ def Valid (t : Ordnode α) : Prop := Valid' ⊥ t ⊤ theorem Valid'.mono_left {x y : α} (xy : x ≤ y) {t : Ordnode α} {o} (h : Valid' y t o) : Valid' x t o := ⟨h.1.mono_left xy, h.2, h.3⟩ theorem Valid'.mono_right {x y : α} (xy : x ≤ y) {t : Ordnode α} {o} (h : Valid' o t x) : Valid' o t y := ⟨h.1.mono_right xy, h.2, h.3⟩ theorem Valid'.trans_left {t₁ t₂ : Ordnode α} {x : α} {o₁ o₂} (h : Bounded t₁ o₁ x) (H : Valid' x t₂ o₂) : Valid' o₁ t₂ o₂ := ⟨h.trans_left H.1, H.2, H.3⟩ theorem Valid'.trans_right {t₁ t₂ : Ordnode α} {x : α} {o₁ o₂} (H : Valid' o₁ t₁ x) (h : Bounded t₂ x o₂) : Valid' o₁ t₁ o₂ := ⟨H.1.trans_right h, H.2, H.3⟩ theorem Valid'.of_lt {t : Ordnode α} {x : α} {o₁ o₂} (H : Valid' o₁ t o₂) (h₁ : Bounded nil o₁ x) (h₂ : All (· < x) t) : Valid' o₁ t x := ⟨H.1.of_lt h₁ h₂, H.2, H.3⟩ theorem Valid'.of_gt {t : Ordnode α} {x : α} {o₁ o₂} (H : Valid' o₁ t o₂) (h₁ : Bounded nil x o₂) (h₂ : All (· > x) t) : Valid' x t o₂ := ⟨H.1.of_gt h₁ h₂, H.2, H.3⟩ theorem Valid'.valid {t o₁ o₂} (h : @Valid' α _ o₁ t o₂) : Valid t := ⟨h.1.weak, h.2, h.3⟩ theorem valid'_nil {o₁ o₂} (h : Bounded nil o₁ o₂) : Valid' o₁ (@nil α) o₂ := ⟨h, ⟨⟩, ⟨⟩⟩ theorem valid_nil : Valid (@nil α) := valid'_nil ⟨⟩ theorem Valid'.node {s l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : BalancedSz (size l) (size r)) (hs : s = size l + size r + 1) : Valid' o₁ (@node α s l x r) o₂ := ⟨⟨hl.1, hr.1⟩, ⟨hs, hl.2, hr.2⟩, ⟨H, hl.3, hr.3⟩⟩ theorem Valid'.dual : ∀ {t : Ordnode α} {o₁ o₂}, Valid' o₁ t o₂ → @Valid' αᵒᵈ _ o₂ (dual t) o₁ | .nil, _, _, h => valid'_nil h.1.dual | .node _ l _ r, _, _, ⟨⟨ol, Or⟩, ⟨rfl, sl, sr⟩, ⟨b, bl, br⟩⟩ => let ⟨ol', sl', bl'⟩ := Valid'.dual ⟨ol, sl, bl⟩ let ⟨or', sr', br'⟩ := Valid'.dual ⟨Or, sr, br⟩ ⟨⟨or', ol'⟩, ⟨by simp [size_dual, add_comm], sr', sl'⟩, ⟨by rw [size_dual, size_dual]; exact b.symm, br', bl'⟩⟩ theorem Valid'.dual_iff {t : Ordnode α} {o₁ o₂} : Valid' o₁ t o₂ ↔ @Valid' αᵒᵈ _ o₂ (.dual t) o₁ := ⟨Valid'.dual, fun h => by have := Valid'.dual h; rwa [dual_dual, OrderDual.Preorder.dual_dual] at this⟩ theorem Valid.dual {t : Ordnode α} : Valid t → @Valid αᵒᵈ _ (.dual t) := Valid'.dual theorem Valid.dual_iff {t : Ordnode α} : Valid t ↔ @Valid αᵒᵈ _ (.dual t) := Valid'.dual_iff theorem Valid'.left {s l x r o₁ o₂} (H : Valid' o₁ (@Ordnode.node α s l x r) o₂) : Valid' o₁ l x := ⟨H.1.1, H.2.2.1, H.3.2.1⟩ theorem Valid'.right {s l x r o₁ o₂} (H : Valid' o₁ (@Ordnode.node α s l x r) o₂) : Valid' x r o₂ := ⟨H.1.2, H.2.2.2, H.3.2.2⟩ nonrec theorem Valid.left {s l x r} (H : Valid (@node α s l x r)) : Valid l := H.left.valid nonrec theorem Valid.right {s l x r} (H : Valid (@node α s l x r)) : Valid r := H.right.valid theorem Valid.size_eq {s l x r} (H : Valid (@node α s l x r)) : size (@node α s l x r) = size l + size r + 1 := H.2.1 theorem Valid'.node' {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : BalancedSz (size l) (size r)) : Valid' o₁ (@node' α l x r) o₂ := hl.node hr H rfl theorem valid'_singleton {x : α} {o₁ o₂} (h₁ : Bounded nil o₁ x) (h₂ : Bounded nil x o₂) : Valid' o₁ (singleton x : Ordnode α) o₂ := (valid'_nil h₁).node (valid'_nil h₂) (Or.inl zero_le_one) rfl theorem valid_singleton {x : α} : Valid (singleton x : Ordnode α) := valid'_singleton ⟨⟩ ⟨⟩ theorem Valid'.node3L {l} {x : α} {m} {y : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hm : Valid' x m y) (hr : Valid' y r o₂) (H1 : BalancedSz (size l) (size m)) (H2 : BalancedSz (size l + size m + 1) (size r)) : Valid' o₁ (@node3L α l x m y r) o₂ := (hl.node' hm H1).node' hr H2 theorem Valid'.node3R {l} {x : α} {m} {y : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hm : Valid' x m y) (hr : Valid' y r o₂) (H1 : BalancedSz (size l) (size m + size r + 1)) (H2 : BalancedSz (size m) (size r)) : Valid' o₁ (@node3R α l x m y r) o₂ := hl.node' (hm.node' hr H2) H1 theorem Valid'.node4L_lemma₁ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : b < 3 * a + 1 := by omega theorem Valid'.node4L_lemma₂ {b c d : ℕ} (mr₂ : b + c + 1 ≤ 3 * d) : c ≤ 3 * d := by omega theorem Valid'.node4L_lemma₃ {b c d : ℕ} (mr₁ : 2 * d ≤ b + c + 1) (mm₁ : b ≤ 3 * c) : d ≤ 3 * c := by omega theorem Valid'.node4L_lemma₄ {a b c d : ℕ} (lr₁ : 3 * a ≤ b + c + 1 + d) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : a + b + 1 ≤ 3 * (c + d + 1) := by omega theorem Valid'.node4L_lemma₅ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₁ : 2 * d ≤ b + c + 1) (mm₂ : c ≤ 3 * b) : c + d + 1 ≤ 3 * (a + b + 1) := by omega theorem Valid'.node4L {l} {x : α} {m} {y : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hm : Valid' x m y) (hr : Valid' (↑y) r o₂) (Hm : 0 < size m) (H : size l = 0 ∧ size m = 1 ∧ size r ≤ 1 ∨ 0 < size l ∧ ratio * size r ≤ size m ∧ delta * size l ≤ size m + size r ∧ 3 * (size m + size r) ≤ 16 * size l + 9 ∧ size m ≤ delta * size r) : Valid' o₁ (@node4L α l x m y r) o₂ := by obtain - | ⟨s, ml, z, mr⟩ := m; · cases Hm suffices BalancedSz (size l) (size ml) ∧ BalancedSz (size mr) (size r) ∧ BalancedSz (size l + size ml + 1) (size mr + size r + 1) from Valid'.node' (hl.node' hm.left this.1) (hm.right.node' hr this.2.1) this.2.2 rcases H with (⟨l0, m1, r0⟩ | ⟨l0, mr₁, lr₁, lr₂, mr₂⟩) · rw [hm.2.size_eq, Nat.succ_inj, add_eq_zero] at m1 rw [l0, m1.1, m1.2]; revert r0; rcases size r with (_ | _ | _) <;> [decide; decide; (intro r0; unfold BalancedSz delta; omega)] · rcases Nat.eq_zero_or_pos (size r) with r0 | r0 · rw [r0] at mr₂; cases not_le_of_lt Hm mr₂ rw [hm.2.size_eq] at lr₁ lr₂ mr₁ mr₂ by_cases mm : size ml + size mr ≤ 1 · have r1 := le_antisymm ((mul_le_mul_left (by decide)).1 (le_trans mr₁ (Nat.succ_le_succ mm) : _ ≤ ratio * 1)) r0 rw [r1, add_assoc] at lr₁ have l1 := le_antisymm ((mul_le_mul_left (by decide)).1 (le_trans lr₁ (add_le_add_right mm 2) : _ ≤ delta * 1)) l0 rw [l1, r1] revert mm; cases size ml <;> cases size mr <;> intro mm · decide · rw [zero_add] at mm; rcases mm with (_ | ⟨⟨⟩⟩) decide · rcases mm with (_ | ⟨⟨⟩⟩); decide · rw [Nat.succ_add] at mm; rcases mm with (_ | ⟨⟨⟩⟩) rcases hm.3.1.resolve_left mm with ⟨mm₁, mm₂⟩ rcases Nat.eq_zero_or_pos (size ml) with ml0 | ml0 · rw [ml0, mul_zero, Nat.le_zero] at mm₂ rw [ml0, mm₂] at mm; cases mm (by decide) have : 2 * size l ≤ size ml + size mr + 1 := by have := Nat.mul_le_mul_left ratio lr₁ rw [mul_left_comm, mul_add] at this have := le_trans this (add_le_add_left mr₁ _) rw [← Nat.succ_mul] at this exact (mul_le_mul_left (by decide)).1 this refine ⟨Or.inr ⟨?_, ?_⟩, Or.inr ⟨?_, ?_⟩, Or.inr ⟨?_, ?_⟩⟩ · refine (mul_le_mul_left (by decide)).1 (le_trans this ?_) rw [two_mul, Nat.succ_le_iff] refine add_lt_add_of_lt_of_le ?_ mm₂ simpa using (mul_lt_mul_right ml0).2 (by decide : 1 < 3) · exact Nat.le_of_lt_succ (Valid'.node4L_lemma₁ lr₂ mr₂ mm₁) · exact Valid'.node4L_lemma₂ mr₂ · exact Valid'.node4L_lemma₃ mr₁ mm₁ · exact Valid'.node4L_lemma₄ lr₁ mr₂ mm₁ · exact Valid'.node4L_lemma₅ lr₂ mr₁ mm₂ theorem Valid'.rotateL_lemma₁ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (hb₂ : c ≤ 3 * b) : a ≤ 3 * b := by omega theorem Valid'.rotateL_lemma₂ {a b c : ℕ} (H3 : 2 * (b + c) ≤ 9 * a + 3) (h : b < 2 * c) : b < 3 * a + 1 := by omega theorem Valid'.rotateL_lemma₃ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (h : b < 2 * c) : a + b < 3 * c := by omega theorem Valid'.rotateL_lemma₄ {a b : ℕ} (H3 : 2 * b ≤ 9 * a + 3) : 3 * b ≤ 16 * a + 9 := by omega theorem Valid'.rotateL {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H1 : ¬size l + size r ≤ 1) (H2 : delta * size l < size r) (H3 : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@rotateL α l x r) o₂ := by obtain - | ⟨rs, rl, rx, rr⟩ := r; · cases H2 rw [hr.2.size_eq, Nat.lt_succ_iff] at H2 rw [hr.2.size_eq] at H3 replace H3 : 2 * (size rl + size rr) ≤ 9 * size l + 3 ∨ size rl + size rr ≤ 2 := H3.imp (@Nat.le_of_add_le_add_right _ 2 _) Nat.le_of_succ_le_succ have H3_0 : size l = 0 → size rl + size rr ≤ 2 := by intro l0; rw [l0] at H3 exact (or_iff_right_of_imp fun h => (mul_le_mul_left (by decide)).1 (le_trans h (by decide))).1 H3 have H3p : size l > 0 → 2 * (size rl + size rr) ≤ 9 * size l + 3 := fun l0 : 1 ≤ size l => (or_iff_left_of_imp <| by omega).1 H3 have ablem : ∀ {a b : ℕ}, 1 ≤ a → a + b ≤ 2 → b ≤ 1 := by omega have hlp : size l > 0 → ¬size rl + size rr ≤ 1 := fun l0 hb => absurd (le_trans (le_trans (Nat.mul_le_mul_left _ l0) H2) hb) (by decide) rw [Ordnode.rotateL_node]; split_ifs with h · have rr0 : size rr > 0 := (mul_lt_mul_left (by decide)).1 (lt_of_le_of_lt (Nat.zero_le _) h : ratio * 0 < _) suffices BalancedSz (size l) (size rl) ∧ BalancedSz (size l + size rl + 1) (size rr) by exact hl.node3L hr.left hr.right this.1 this.2 rcases Nat.eq_zero_or_pos (size l) with l0 | l0 · rw [l0]; replace H3 := H3_0 l0 have := hr.3.1 rcases Nat.eq_zero_or_pos (size rl) with rl0 | rl0 · rw [rl0] at this ⊢ rw [le_antisymm (balancedSz_zero.1 this.symm) rr0] decide have rr1 : size rr = 1 := le_antisymm (ablem rl0 H3) rr0 rw [add_comm] at H3 rw [rr1, show size rl = 1 from le_antisymm (ablem rr0 H3) rl0] decide replace H3 := H3p l0 rcases hr.3.1.resolve_left (hlp l0) with ⟨_, hb₂⟩ refine ⟨Or.inr ⟨?_, ?_⟩, Or.inr ⟨?_, ?_⟩⟩ · exact Valid'.rotateL_lemma₁ H2 hb₂ · exact Nat.le_of_lt_succ (Valid'.rotateL_lemma₂ H3 h) · exact Valid'.rotateL_lemma₃ H2 h · exact le_trans hb₂ (Nat.mul_le_mul_left _ <| le_trans (Nat.le_add_left _ _) (Nat.le_add_right _ _)) · rcases Nat.eq_zero_or_pos (size rl) with rl0 | rl0 · rw [rl0, not_lt, Nat.le_zero, Nat.mul_eq_zero] at h replace h := h.resolve_left (by decide) rw [rl0, h, Nat.le_zero, Nat.mul_eq_zero] at H2 rw [hr.2.size_eq, rl0, h, H2.resolve_left (by decide)] at H1 cases H1 (by decide) refine hl.node4L hr.left hr.right rl0 ?_ rcases Nat.eq_zero_or_pos (size l) with l0 | l0 · replace H3 := H3_0 l0 rcases Nat.eq_zero_or_pos (size rr) with rr0 | rr0 · have := hr.3.1 rw [rr0] at this exact Or.inl ⟨l0, le_antisymm (balancedSz_zero.1 this) rl0, rr0.symm ▸ zero_le_one⟩ exact Or.inl ⟨l0, le_antisymm (ablem rr0 <| by rwa [add_comm]) rl0, ablem rl0 H3⟩ exact Or.inr ⟨l0, not_lt.1 h, H2, Valid'.rotateL_lemma₄ (H3p l0), (hr.3.1.resolve_left (hlp l0)).1⟩ theorem Valid'.rotateR {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H1 : ¬size l + size r ≤ 1) (H2 : delta * size r < size l) (H3 : 2 * size l ≤ 9 * size r + 5 ∨ size l ≤ 3) : Valid' o₁ (@rotateR α l x r) o₂ := by refine Valid'.dual_iff.2 ?_ rw [dual_rotateR] refine hr.dual.rotateL hl.dual ?_ ?_ ?_ · rwa [size_dual, size_dual, add_comm] · rwa [size_dual, size_dual] · rwa [size_dual, size_dual] theorem Valid'.balance'_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H₁ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) (H₂ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : Valid' o₁ (@balance' α l x r) o₂ := by rw [balance']; split_ifs with h h_1 h_2 · exact hl.node' hr (Or.inl h) · exact hl.rotateL hr h h_1 H₁ · exact hl.rotateR hr h h_2 H₂ · exact hl.node' hr (Or.inr ⟨not_lt.1 h_2, not_lt.1 h_1⟩) theorem Valid'.balance'_lemma {α l l' r r'} (H1 : BalancedSz l' r') (H2 : Nat.dist (@size α l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l') : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3 := by suffices @size α r ≤ 3 * (size l + 1) by omega rcases H2 with (⟨hl, rfl⟩ | ⟨hr, rfl⟩) <;> rcases H1 with (h | ⟨_, h₂⟩) · exact le_trans (Nat.le_add_left _ _) (le_trans h (Nat.le_add_left _ _)) · exact le_trans h₂ (Nat.mul_le_mul_left _ <| le_trans (Nat.dist_tri_right _ _) (Nat.add_le_add_left hl _)) · exact le_trans (Nat.dist_tri_left' _ _) (le_trans (add_le_add hr (le_trans (Nat.le_add_left _ _) h)) (by omega)) · rw [Nat.mul_succ] exact le_trans (Nat.dist_tri_right' _ _) (add_le_add h₂ (le_trans hr (by decide))) theorem Valid'.balance' {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : ∃ l' r', BalancedSz l' r' ∧ (Nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l')) : Valid' o₁ (@balance' α l x r) o₂ := let ⟨_, _, H1, H2⟩ := H Valid'.balance'_aux hl hr (Valid'.balance'_lemma H1 H2) (Valid'.balance'_lemma H1.symm H2.symm) theorem Valid'.balance {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : ∃ l' r', BalancedSz l' r' ∧ (Nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l')) : Valid' o₁ (@balance α l x r) o₂ := by rw [balance_eq_balance' hl.3 hr.3 hl.2 hr.2]; exact hl.balance' hr H theorem Valid'.balanceL_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H₁ : size l = 0 → size r ≤ 1) (H₂ : 1 ≤ size l → 1 ≤ size r → size r ≤ delta * size l) (H₃ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : Valid' o₁ (@balanceL α l x r) o₂ := by rw [balanceL_eq_balance hl.2 hr.2 H₁ H₂, balance_eq_balance' hl.3 hr.3 hl.2 hr.2] refine hl.balance'_aux hr (Or.inl ?_) H₃ rcases Nat.eq_zero_or_pos (size r) with r0 | r0 · rw [r0]; exact Nat.zero_le _ rcases Nat.eq_zero_or_pos (size l) with l0 | l0 · rw [l0]; exact le_trans (Nat.mul_le_mul_left _ (H₁ l0)) (by decide) replace H₂ : _ ≤ 3 * _ := H₂ l0 r0; omega theorem Valid'.balanceL {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : (∃ l', Raised l' (size l) ∧ BalancedSz l' (size r)) ∨ ∃ r', Raised (size r) r' ∧ BalancedSz (size l) r') : Valid' o₁ (@balanceL α l x r) o₂ := by rw [balanceL_eq_balance' hl.3 hr.3 hl.2 hr.2 H] refine hl.balance' hr ?_ rcases H with (⟨l', e, H⟩ | ⟨r', e, H⟩) · exact ⟨_, _, H, Or.inl ⟨e.dist_le', rfl⟩⟩ · exact ⟨_, _, H, Or.inr ⟨e.dist_le, rfl⟩⟩ theorem Valid'.balanceR_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r) (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@balanceR α l x r) o₂ := by rw [Valid'.dual_iff, dual_balanceR] have := hr.dual.balanceL_aux hl.dual rw [size_dual, size_dual] at this exact this H₁ H₂ H₃ theorem Valid'.balanceR {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : (∃ l', Raised (size l) l' ∧ BalancedSz l' (size r)) ∨ ∃ r', Raised r' (size r) ∧ BalancedSz (size l) r') : Valid' o₁ (@balanceR α l x r) o₂ := by rw [Valid'.dual_iff, dual_balanceR]; exact hr.dual.balanceL hl.dual (balance_sz_dual H) theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) : Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧ size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by have := H.2.eq_node'; rw [this] at H; clear this induction r generalizing l x o₁ with | nil => exact ⟨H.left, rfl⟩ | node rs rl rx rr _ IHrr => have := H.2.2.2.eq_node'; rw [this] at H ⊢ rcases IHrr H.right with ⟨h, e⟩ refine ⟨Valid'.balanceL H.left h (Or.inr ⟨_, Or.inr e, H.3.1⟩), ?_⟩ rw [eraseMax, size_balanceL H.3.2.1 h.3 H.2.2.1 h.2 (Or.inr ⟨_, Or.inr e, H.3.1⟩)] rw [size_node, e]; rfl theorem Valid'.eraseMin_aux {s l} {x : α} {r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) : Valid' ↑(findMin' l x) (@eraseMin α (.node' l x r)) o₂ ∧
size (.node' l x r) = size (eraseMin (.node' l x r)) + 1 := by have := H.dual.eraseMax_aux
Mathlib/Data/Ordmap/Ordset.lean
390
391
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import Mathlib.Data.Set.Function import Mathlib.Logic.Equiv.Defs import Mathlib.Tactic.Says /-! # Equivalences and sets In this file we provide lemmas linking equivalences to sets. Some notable definitions are: * `Equiv.ofInjective`: an injective function is (noncomputably) equivalent to its range. * `Equiv.setCongr`: two equal sets are equivalent as types. * `Equiv.Set.union`: a disjoint union of sets is equivalent to their `Sum`. This file is separate from `Equiv/Basic` such that we do not require the full lattice structure on sets before defining what an equivalence is. -/ open Function Set universe u v w z variable {α : Sort u} {β : Sort v} {γ : Sort w} namespace EquivLike @[simp] theorem range_eq_univ {α : Type*} {β : Type*} {E : Type*} [EquivLike E α β] (e : E) : range e = univ := eq_univ_of_forall (EquivLike.toEquiv e).surjective end EquivLike namespace Equiv theorem range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : range e = univ := EquivLike.range_eq_univ e protected theorem image_eq_preimage {α β} (e : α ≃ β) (s : Set α) : e '' s = e.symm ⁻¹' s := Set.ext fun _ => mem_image_iff_of_inverse e.left_inv e.right_inv @[simp 1001] theorem _root_.Set.mem_image_equiv {α β} {S : Set α} {f : α ≃ β} {x : β} : x ∈ f '' S ↔ f.symm x ∈ S := Set.ext_iff.mp (f.image_eq_preimage S) x /-- Alias for `Equiv.image_eq_preimage` -/ theorem _root_.Set.image_equiv_eq_preimage_symm {α β} (S : Set α) (f : α ≃ β) : f '' S = f.symm ⁻¹' S := f.image_eq_preimage S /-- Alias for `Equiv.image_eq_preimage` -/ theorem _root_.Set.preimage_equiv_eq_image_symm {α β} (S : Set α) (f : β ≃ α) : f ⁻¹' S = f.symm '' S := (f.symm.image_eq_preimage S).symm -- Increased priority so this fires before `image_subset_iff` @[simp high] protected theorem symm_image_subset {α β} (e : α ≃ β) (s : Set α) (t : Set β) : e.symm '' t ⊆ s ↔ t ⊆ e '' s := by rw [image_subset_iff, e.image_eq_preimage] -- Increased priority so this fires before `image_subset_iff` @[simp high] protected theorem subset_symm_image {α β} (e : α ≃ β) (s : Set α) (t : Set β) : s ⊆ e.symm '' t ↔ e '' s ⊆ t := calc s ⊆ e.symm '' t ↔ e.symm.symm '' s ⊆ t := by rw [e.symm.symm_image_subset] _ ↔ e '' s ⊆ t := by rw [e.symm_symm] @[simp] theorem symm_image_image {α β} (e : α ≃ β) (s : Set α) : e.symm '' (e '' s) = s := e.leftInverse_symm.image_image s theorem eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : Set α) (t : Set β) : t = e '' s ↔ e.symm '' t = s := (e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm @[simp] theorem image_symm_image {α β} (e : α ≃ β) (s : Set β) : e '' (e.symm '' s) = s := e.symm.symm_image_image s @[simp] theorem image_preimage {α β} (e : α ≃ β) (s : Set β) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] theorem preimage_image {α β} (e : α ≃ β) (s : Set α) : e ⁻¹' (e '' s) = s := e.injective.preimage_image s protected theorem image_compl {α β} (f : Equiv α β) (s : Set α) : f '' sᶜ = (f '' s)ᶜ := image_compl_eq f.bijective @[simp] theorem symm_preimage_preimage {α β} (e : α ≃ β) (s : Set β) : e.symm ⁻¹' (e ⁻¹' s) = s := e.rightInverse_symm.preimage_preimage s @[simp] theorem preimage_symm_preimage {α β} (e : α ≃ β) (s : Set α) : e ⁻¹' (e.symm ⁻¹' s) = s := e.leftInverse_symm.preimage_preimage s theorem preimage_subset {α β} (e : α ≃ β) (s t : Set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t := e.surjective.preimage_subset_preimage_iff theorem image_subset {α β} (e : α ≃ β) (s t : Set α) : e '' s ⊆ e '' t ↔ s ⊆ t := image_subset_image_iff e.injective @[simp] theorem image_eq_iff_eq {α β} (e : α ≃ β) (s t : Set α) : e '' s = e '' t ↔ s = t := image_eq_image e.injective theorem preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t := Set.preimage_eq_iff_eq_image e.bijective theorem eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t := Set.eq_preimage_iff_image_eq e.bijective lemma setOf_apply_symm_eq_image_setOf {α β} (e : α ≃ β) (p : α → Prop) : {b | p (e.symm b)} = e '' {a | p a} := by rw [Equiv.image_eq_preimage, preimage_setOf_eq] @[simp] theorem prod_assoc_preimage {α β γ} {s : Set α} {t : Set β} {u : Set γ} : Equiv.prodAssoc α β γ ⁻¹' s ×ˢ t ×ˢ u = (s ×ˢ t) ×ˢ u := by ext simp [and_assoc] @[simp] theorem prod_assoc_symm_preimage {α β γ} {s : Set α} {t : Set β} {u : Set γ} : (Equiv.prodAssoc α β γ).symm ⁻¹' (s ×ˢ t) ×ˢ u = s ×ˢ t ×ˢ u := by ext simp [and_assoc] -- `@[simp]` doesn't like these lemmas, as it uses `Set.image_congr'` to turn `Equiv.prodAssoc` -- into a lambda expression and then unfold it. theorem prod_assoc_image {α β γ} {s : Set α} {t : Set β} {u : Set γ} : Equiv.prodAssoc α β γ '' (s ×ˢ t) ×ˢ u = s ×ˢ t ×ˢ u := by simpa only [Equiv.image_eq_preimage] using prod_assoc_symm_preimage theorem prod_assoc_symm_image {α β γ} {s : Set α} {t : Set β} {u : Set γ} : (Equiv.prodAssoc α β γ).symm '' s ×ˢ t ×ˢ u = (s ×ˢ t) ×ˢ u := by simpa only [Equiv.image_eq_preimage] using prod_assoc_preimage /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def setProdEquivSigma {α β : Type*} (s : Set (α × β)) : s ≃ Σx : α, { y : β | (x, y) ∈ s } where toFun x := ⟨x.1.1, x.1.2, by simp⟩ invFun x := ⟨(x.1, x.2.1), x.2.2⟩ left_inv := fun ⟨⟨_, _⟩, _⟩ => rfl right_inv := fun ⟨_, _, _⟩ => rfl /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps! apply symm_apply] def setCongr {α : Type*} {s t : Set α} (h : s = t) : s ≃ t := subtypeEquivProp h -- We could construct this using `Equiv.Set.image e s e.injective`, -- but this definition provides an explicit inverse. /-- A set is equivalent to its image under an equivalence. -/ @[simps] def image {α β : Type*} (e : α ≃ β) (s : Set α) : s ≃ e '' s where toFun x := ⟨e x.1, by simp⟩ invFun y := ⟨e.symm y.1, by rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩ simpa using m⟩ left_inv x := by simp right_inv y := by simp namespace Set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨Subtype.val, fun a => ⟨a, trivial⟩, fun ⟨_, _⟩ => rfl, fun _ => rfl⟩ /-- An empty set is equivalent to the `Empty` type. -/ protected def empty (α) : (∅ : Set α) ≃ Empty := equivEmpty _ /-- An empty set is equivalent to a `PEmpty` type. -/ protected def pempty (α) : (∅ : Set α) ≃ PEmpty := equivPEmpty _ /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : Set α} (p : α → Prop) [DecidablePred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬p x) : (s ∪ t : Set α) ≃ s ⊕ t where toFun x := if hp : p x then Sum.inl ⟨_, x.2.resolve_right fun xt => ht _ xt hp⟩ else Sum.inr ⟨_, x.2.resolve_left fun xs => hp (hs _ xs)⟩ invFun o := match o with | Sum.inl x => ⟨x, Or.inl x.2⟩ | Sum.inr x => ⟨x, Or.inr x.2⟩ left_inv := fun ⟨x, h'⟩ => by by_cases h : p x <;> simp [h] right_inv o := by rcases o with (⟨x, h⟩ | ⟨x, h⟩) <;> [simp [hs _ h]; simp [ht _ h]] /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : Set α} [DecidablePred fun x => x ∈ s] (H : Disjoint s t) : (s ∪ t : Set α) ≃ s ⊕ t := Set.union' (fun x => x ∈ s) (fun _ => id) fun _ xt xs => Set.disjoint_left.mp H xs xt theorem union_apply_left {α} {s t : Set α} [DecidablePred fun x => x ∈ s] (H : Disjoint s t) {a : (s ∪ t : Set α)} (ha : ↑a ∈ s) : Equiv.Set.union H a = Sum.inl ⟨a, ha⟩ := dif_pos ha theorem union_apply_right {α} {s t : Set α} [DecidablePred fun x => x ∈ s] (H : Disjoint s t) {a : (s ∪ t : Set α)} (ha : ↑a ∈ t) : Equiv.Set.union H a = Sum.inr ⟨a, ha⟩ := dif_neg fun h => Set.disjoint_left.mp H h ha @[simp] theorem union_symm_apply_left {α} {s t : Set α} [DecidablePred fun x => x ∈ s] (H : Disjoint s t) (a : s) : (Equiv.Set.union H).symm (Sum.inl a) = ⟨a, by simp⟩ := rfl @[simp] theorem union_symm_apply_right {α} {s t : Set α} [DecidablePred fun x => x ∈ s] (H : Disjoint s t) (a : t) : (Equiv.Set.union H).symm (Sum.inr a) = ⟨a, by simp⟩ := rfl /-- A singleton set is equivalent to a `PUnit` type. -/ protected def singleton {α} (a : α) : ({a} : Set α) ≃ PUnit.{u} := ⟨fun _ => PUnit.unit, fun _ => ⟨a, mem_singleton _⟩, fun ⟨x, h⟩ => by simp? at h says simp only [mem_singleton_iff] at h subst x rfl, fun ⟨⟩ => rfl⟩ @[deprecated (since := "2025-03-19"), simps! apply symm_apply] protected alias ofEq := Equiv.setCongr attribute [deprecated Equiv.setCongr_apply (since := "2025-03-19")] Set.ofEq_apply attribute [deprecated Equiv.setCongr_symm_apply (since := "2025-03-19")] Set.ofEq_symm_apply lemma Equiv.strictMono_setCongr {α : Type*} [Preorder α] {S T : Set α} (h : S = T) : StrictMono (setCongr h) := fun _ _ ↦ id /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ PUnit`. -/ protected def insert {α} {s : Set.{u} α} [DecidablePred (· ∈ s)] {a : α} (H : a ∉ s) : (insert a s : Set α) ≃ s ⊕ PUnit.{u + 1} := calc (insert a s : Set α) ≃ ↥(s ∪ {a}) := Equiv.setCongr (by simp) _ ≃ s ⊕ ({a} : Set α) := Equiv.Set.union <| by simpa _ ≃ s ⊕ PUnit.{u + 1} := sumCongr (Equiv.refl _) (Equiv.Set.singleton _) @[simp] theorem insert_symm_apply_inl {α} {s : Set.{u} α} [DecidablePred (· ∈ s)] {a : α} (H : a ∉ s) (b : s) : (Equiv.Set.insert H).symm (Sum.inl b) = ⟨b, Or.inr b.2⟩ := rfl @[simp] theorem insert_symm_apply_inr {α} {s : Set.{u} α} [DecidablePred (· ∈ s)] {a : α} (H : a ∉ s) (b : PUnit.{u + 1}) : (Equiv.Set.insert H).symm (Sum.inr b) = ⟨a, Or.inl rfl⟩ := rfl @[simp] theorem insert_apply_left {α} {s : Set.{u} α} [DecidablePred (· ∈ s)] {a : α} (H : a ∉ s) : Equiv.Set.insert H ⟨a, Or.inl rfl⟩ = Sum.inr PUnit.unit := (Equiv.Set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] theorem insert_apply_right {α} {s : Set.{u} α} [DecidablePred (· ∈ s)] {a : α} (H : a ∉ s) (b : s) : Equiv.Set.insert H ⟨b, Or.inr b.2⟩ = Sum.inl b := (Equiv.Set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : Set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sumCompl {α} (s : Set α) [DecidablePred (· ∈ s)] : s ⊕ (sᶜ : Set α) ≃ α := calc s ⊕ (sᶜ : Set α) ≃ ↥(s ∪ sᶜ) := (Equiv.Set.union disjoint_compl_right).symm _ ≃ @univ α := Equiv.setCongr (by simp) _ ≃ α := Equiv.Set.univ _ @[simp] theorem sumCompl_apply_inl {α : Type u} (s : Set α) [DecidablePred (· ∈ s)] (x : s) : Equiv.Set.sumCompl s (Sum.inl x) = x := rfl @[simp] theorem sumCompl_apply_inr {α : Type u} (s : Set α) [DecidablePred (· ∈ s)] (x : (sᶜ : Set α)) : Equiv.Set.sumCompl s (Sum.inr x) = x := rfl theorem sumCompl_symm_apply_of_mem {α : Type u} {s : Set α} [DecidablePred (· ∈ s)] {x : α} (hx : x ∈ s) : (Equiv.Set.sumCompl s).symm x = Sum.inl ⟨x, hx⟩ := by simp [Equiv.Set.sumCompl, Equiv.Set.univ, union_apply_left, hx] theorem sumCompl_symm_apply_of_not_mem {α : Type u} {s : Set α} [DecidablePred (· ∈ s)] {x : α} (hx : x ∉ s) : (Equiv.Set.sumCompl s).symm x = Sum.inr ⟨x, hx⟩ := by simp [Equiv.Set.sumCompl, Equiv.Set.univ, union_apply_right, hx] @[simp] theorem sumCompl_symm_apply {α : Type*} {s : Set α} [DecidablePred (· ∈ s)] {x : s} : (Equiv.Set.sumCompl s).symm x = Sum.inl x := Set.sumCompl_symm_apply_of_mem x.2 @[simp] theorem sumCompl_symm_apply_compl {α : Type*} {s : Set α} [DecidablePred (· ∈ s)] {x : (sᶜ : Set α)} : (Equiv.Set.sumCompl s).symm x = Sum.inr x := Set.sumCompl_symm_apply_of_not_mem x.2 /-- `sumDiffSubset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sumDiffSubset {α} {s t : Set α} (h : s ⊆ t) [DecidablePred (· ∈ s)] : s ⊕ (t \ s : Set α) ≃ t := calc s ⊕ (t \ s : Set α) ≃ (s ∪ t \ s : Set α) := (Equiv.Set.union disjoint_sdiff_self_right).symm _ ≃ t := Equiv.setCongr (by simp [union_diff_self, union_eq_self_of_subset_left h]) @[simp] theorem sumDiffSubset_apply_inl {α} {s t : Set α} (h : s ⊆ t) [DecidablePred (· ∈ s)] (x : s) : Equiv.Set.sumDiffSubset h (Sum.inl x) = inclusion h x := rfl @[simp] theorem sumDiffSubset_apply_inr {α} {s t : Set α} (h : s ⊆ t) [DecidablePred (· ∈ s)] (x : (t \ s : Set α)) : Equiv.Set.sumDiffSubset h (Sum.inr x) = inclusion diff_subset x := rfl theorem sumDiffSubset_symm_apply_of_mem {α} {s t : Set α} (h : s ⊆ t) [DecidablePred (· ∈ s)] {x : t} (hx : x.1 ∈ s) : (Equiv.Set.sumDiffSubset h).symm x = Sum.inl ⟨x, hx⟩ := by apply (Equiv.Set.sumDiffSubset h).injective simp only [apply_symm_apply, sumDiffSubset_apply_inl, Set.inclusion_mk] theorem sumDiffSubset_symm_apply_of_not_mem {α} {s t : Set α} (h : s ⊆ t) [DecidablePred (· ∈ s)] {x : t} (hx : x.1 ∉ s) : (Equiv.Set.sumDiffSubset h).symm x = Sum.inr ⟨x, ⟨x.2, hx⟩⟩ := by apply (Equiv.Set.sumDiffSubset h).injective simp only [apply_symm_apply, sumDiffSubset_apply_inr, Set.inclusion_mk] /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def unionSumInter {α : Type u} (s t : Set α) [DecidablePred (· ∈ s)] : (s ∪ t : Set α) ⊕ (s ∩ t : Set α) ≃ s ⊕ t := calc (s ∪ t : Set α) ⊕ (s ∩ t : Set α) ≃ (s ∪ t \ s : Set α) ⊕ (s ∩ t : Set α) := by rw [union_diff_self] _ ≃ (s ⊕ (t \ s : Set α)) ⊕ (s ∩ t : Set α) := sumCongr (Set.union disjoint_sdiff_self_right) (Equiv.refl _) _ ≃ s ⊕ ((t \ s : Set α) ⊕ (s ∩ t : Set α)) := sumAssoc _ _ _ _ ≃ s ⊕ (t \ s ∪ s ∩ t : Set α) := sumCongr (Equiv.refl _) (by refine (Set.union' (· ∉ s) ?_ ?_).symm exacts [fun x hx => hx.2, fun x hx => not_not_intro hx.1]) _ ≃ s ⊕ t := by { rw [(_ : t \ s ∪ s ∩ t = t)] rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : Set α` and `t : Set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : Set α} {t : Set β} [DecidablePred (· ∈ s)] [DecidablePred (· ∈ t)] (e₀ : s ≃ t) : { e : α ≃ β // ∀ x : s, e x = e₀ x } ≃ ((sᶜ : Set α) ≃ (tᶜ : Set β)) where toFun e := subtypeEquiv e fun _ => not_congr <| Iff.symm <| MapsTo.mem_iff (mapsTo_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (SurjOn.mapsTo_compl (surjOn_iff_exists_map_subtype.2 ⟨t, e₀, Subset.refl t, e₀.surjective, e.2⟩) e.1.injective) invFun e₁ := Subtype.mk (calc α ≃ s ⊕ (sᶜ : Set α) := (Set.sumCompl s).symm _ ≃ t ⊕ (tᶜ : Set β) := e₀.sumCongr e₁ _ ≃ β := Set.sumCompl t ) fun x => by simp only [Sum.map_inl, trans_apply, sumCongr_apply, Set.sumCompl_apply_inl, Set.sumCompl_symm_apply, Trans.trans] left_inv e := by ext x by_cases hx : x ∈ s · simp only [Set.sumCompl_symm_apply_of_mem hx, ← e.prop ⟨x, hx⟩, Sum.map_inl, sumCongr_apply, trans_apply, Subtype.coe_mk, Set.sumCompl_apply_inl, Trans.trans] · simp only [Set.sumCompl_symm_apply_of_not_mem hx, Sum.map_inr, subtypeEquiv_apply, Set.sumCompl_apply_inr, trans_apply, sumCongr_apply, Subtype.coe_mk, Trans.trans] right_inv e := Equiv.ext fun x => by simp only [Sum.map_inr, subtypeEquiv_apply, Set.sumCompl_apply_inr, Function.comp_apply, sumCongr_apply, Equiv.coe_trans, Subtype.coe_eta, Subtype.coe_mk, Trans.trans, Set.sumCompl_symm_apply_compl] /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : Set α) (t : Set β) : ↥(s ×ˢ t) ≃ s × t := @subtypeProdEquivProd α β s t /-- The set `Set.pi Set.univ s` is equivalent to `Π a, s a`. -/ @[simps] protected def univPi {α : Type*} {β : α → Type*} (s : ∀ a, Set (β a)) : pi univ s ≃ ∀ a, s a where toFun f a := ⟨(f : ∀ a, β a) a, f.2 a (mem_univ a)⟩ invFun f := ⟨fun a => f a, fun a _ => (f a).2⟩ left_inv := fun ⟨f, hf⟩ => by ext a rfl right_inv f := by ext a rfl /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def imageOfInjOn {α β} (f : α → β) (s : Set α) (H : InjOn f s) : s ≃ f '' s := ⟨fun p => ⟨f p, mem_image_of_mem f p.2⟩, fun p => ⟨Classical.choose p.2, (Classical.choose_spec p.2).1⟩, fun ⟨_, h⟩ => Subtype.eq (H (Classical.choose_spec (mem_image_of_mem f h)).1 h (Classical.choose_spec (mem_image_of_mem f h)).2), fun ⟨_, h⟩ => Subtype.eq (Classical.choose_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps! apply] protected noncomputable def image {α β} (f : α → β) (s : Set α) (H : Injective f) : s ≃ f '' s := Equiv.Set.imageOfInjOn f s H.injOn @[simp] protected theorem image_symm_apply {α β} (f : α → β) (s : Set α) (H : Injective f) (x : α) (h : f x ∈ f '' s) : (Set.image f s H).symm ⟨f x, h⟩ = ⟨x, H.mem_set_image.1 h⟩ := (Equiv.symm_apply_eq _).2 rfl theorem image_symm_preimage {α β} {f : α → β} (hf : Injective f) (u s : Set α) : (fun x => (Set.image f s hf).symm x : f '' s → α) ⁻¹' u = Subtype.val ⁻¹' (f '' u) := by ext ⟨b, a, has, rfl⟩ simp [hf.eq_iff] /-- If `α` is equivalent to `β`, then `Set α` is equivalent to `Set β`. -/ @[simps] protected def congr {α β : Type*} (e : α ≃ β) : Set α ≃ Set β := ⟨fun s => e '' s, fun t => e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : Set α) (t : α → Prop) : ({ x ∈ s | t x } : Set α) ≃ { x : s | t x } := (Equiv.subtypeSubtypeEquivSubtypeInter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `Set S`. -/ protected def powerset {α} (S : Set α) : 𝒫 S ≃ Set S where toFun := fun x : 𝒫 S => Subtype.val ⁻¹' (x : Set α) invFun := fun x : Set S => ⟨Subtype.val '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩ left_inv x := by ext y;exact ⟨fun ⟨⟨_, _⟩, h, rfl⟩ => h, fun h => ⟨⟨_, x.2 h⟩, h, rfl⟩⟩ right_inv x := by ext; simp /-- If `s` is a set in `range f`, then its image under `rangeSplitting f` is in bijection (via `f`) with `s`. -/ @[simps] noncomputable def rangeSplittingImageEquiv {α β : Type*} (f : α → β) (s : Set (range f)) : rangeSplitting f '' s ≃ s where toFun x := ⟨⟨f x, by simp⟩, by rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩ simpa [apply_rangeSplitting f] using m⟩ invFun x := ⟨rangeSplitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩ left_inv x := by rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩ simp [apply_rangeSplitting f] right_inv x := by simp [apply_rangeSplitting f] /-- Equivalence between the range of `Sum.inl : α → α ⊕ β` and `α`. -/ @[simps symm_apply_coe] def rangeInl (α β : Type*) : Set.range (Sum.inl : α → α ⊕ β) ≃ α where toFun | ⟨.inl x, _⟩ => x | ⟨.inr _, h⟩ => False.elim <| by rcases h with ⟨x, h'⟩; cases h' invFun x := ⟨.inl x, mem_range_self _⟩ left_inv := fun ⟨_, _, rfl⟩ => rfl right_inv _ := rfl @[simp] lemma rangeInl_apply_inl {α : Type*} (β : Type*) (x : α) : (rangeInl α β) ⟨.inl x, mem_range_self _⟩ = x := rfl /-- Equivalence between the range of `Sum.inr : β → α ⊕ β` and `β`. -/ @[simps symm_apply_coe] def rangeInr (α β : Type*) : Set.range (Sum.inr : β → α ⊕ β) ≃ β where toFun | ⟨.inl _, h⟩ => False.elim <| by rcases h with ⟨x, h'⟩; cases h' | ⟨.inr x, _⟩ => x invFun x := ⟨.inr x, mem_range_self _⟩ left_inv := fun ⟨_, _, rfl⟩ => rfl right_inv _ := rfl @[simp] lemma rangeInr_apply_inr (α : Type*) {β : Type*} (x : β) : (rangeInr α β) ⟨.inr x, mem_range_self _⟩ = x := rfl end Set /-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the range of `f`. While awkward, the `Nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is empty too. This hypothesis is absent on analogous definitions on stronger `Equiv`s like `LinearEquiv.ofLeftInverse` and `RingEquiv.ofLeftInverse` as their typeclass assumptions are already sufficient to ensure non-emptiness. -/ @[simps] def ofLeftInverse {α β : Sort _} (f : α → β) (f_inv : Nonempty α → β → α) (hf : ∀ h : Nonempty α, LeftInverse (f_inv h) f) : α ≃ range f where toFun a := ⟨f a, a, rfl⟩ invFun b := f_inv (nonempty_of_exists b.2) b left_inv a := hf ⟨a⟩ a right_inv := fun ⟨b, a, ha⟩ => Subtype.eq <| show f (f_inv ⟨a⟩ b) = b from Eq.trans (congr_arg f <| ha ▸ hf _ a) ha /-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`. Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike the stronger but less convenient `ofLeftInverse`. -/ abbrev ofLeftInverse' {α β : Sort _} (f : α → β) (f_inv : β → α) (hf : LeftInverse f_inv f) : α ≃ range f := ofLeftInverse f (fun _ => f_inv) fun _ => hf /-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/ @[simps! apply] noncomputable def ofInjective {α β} (f : α → β) (hf : Injective f) : α ≃ range f := Equiv.ofLeftInverse f (fun _ => Function.invFun f) fun _ => Function.leftInverse_invFun hf theorem apply_ofInjective_symm {α β} {f : α → β} (hf : Injective f) (b : range f) : f ((ofInjective f hf).symm b) = b := Subtype.ext_iff.1 <| (ofInjective f hf).apply_symm_apply b @[simp] theorem ofInjective_symm_apply {α β} {f : α → β} (hf : Injective f) (a : α) : (ofInjective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a := by apply (ofInjective f hf).injective simp [apply_ofInjective_symm hf] theorem coe_ofInjective_symm {α β} {f : α → β} (hf : Injective f) : ((ofInjective f hf).symm : range f → α) = rangeSplitting f := by ext ⟨y, x, rfl⟩ apply hf simp [apply_rangeSplitting f] @[simp] theorem self_comp_ofInjective_symm {α β} {f : α → β} (hf : Injective f) : f ∘ (ofInjective f hf).symm = Subtype.val := funext fun x => apply_ofInjective_symm hf x theorem ofLeftInverse_eq_ofInjective {α β : Type*} (f : α → β) (f_inv : Nonempty α → β → α) (hf : ∀ h : Nonempty α, LeftInverse (f_inv h) f) : ofLeftInverse f f_inv hf = ofInjective f ((isEmpty_or_nonempty α).elim (fun _ _ _ _ => Subsingleton.elim _ _) (fun h => (hf h).injective)) := by ext simp theorem ofLeftInverse'_eq_ofInjective {α β : Type*} (f : α → β) (f_inv : β → α) (hf : LeftInverse f_inv f) : ofLeftInverse' f f_inv hf = ofInjective f hf.injective := by ext simp protected theorem set_forall_iff {α β} (e : α ≃ β) {p : Set α → Prop} : (∀ a, p a) ↔ ∀ a, p (e ⁻¹' a) := e.injective.preimage_surjective.forall theorem preimage_piEquivPiSubtypeProd_symm_pi {α : Type*} {β : α → Type*} (p : α → Prop) [DecidablePred p] (s : ∀ i, Set (β i)) : (piEquivPiSubtypeProd p β).symm ⁻¹' pi univ s = (pi univ fun i : { i // p i } => s i) ×ˢ pi univ fun i : { i // ¬p i } => s i := by ext ⟨f, g⟩ simp only [mem_preimage, mem_univ_pi, prodMk_mem_set_prod_eq, Subtype.forall, ← forall_and] refine forall_congr' fun i => ?_ dsimp only [Subtype.coe_mk] by_cases hi : p i <;> simp [hi] -- See also `Equiv.sigmaFiberEquiv`. /-- `sigmaPreimageEquiv f` for `f : α → β` is the natural equivalence between the type of all preimages of points under `f` and the total space `α`. -/ @[simps!] def sigmaPreimageEquiv {α β} (f : α → β) : (Σb, f ⁻¹' {b}) ≃ α := sigmaFiberEquiv f -- See also `Equiv.ofFiberEquiv`. /-- A family of equivalences between preimages of points gives an equivalence between domains. -/ @[simps!] def ofPreimageEquiv {α β γ} {f : α → γ} {g : β → γ} (e : ∀ c, f ⁻¹' {c} ≃ g ⁻¹' {c}) : α ≃ β := Equiv.ofFiberEquiv e theorem ofPreimageEquiv_map {α β γ} {f : α → γ} {g : β → γ} (e : ∀ c, f ⁻¹' {c} ≃ g ⁻¹' {c}) (a : α) : g (ofPreimageEquiv e a) = f a := Equiv.ofFiberEquiv_map e a end Equiv /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the types `↥s` and `↥t`. -/ noncomputable def Set.BijOn.equiv {α : Type*} {β : Type*} {s : Set α} {t : Set β} (f : α → β) (h : BijOn f s t) : s ≃ t := Equiv.ofBijective _ h.bijective /-- The composition of an updated function with an equiv on a subtype can be expressed as an updated function. -/ theorem dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {p : α → Prop} (e : β ≃ {x // p x}) (v : β → γ) (w : α → γ) (j : β) (x : γ) [DecidableEq β] [DecidableEq α] [∀ j, Decidable (p j)] : (fun i : α => if h : p i then (Function.update v j x) (e.symm ⟨i, h⟩) else w i) = Function.update (fun i : α => if h : p i then v (e.symm ⟨i, h⟩) else w i) (e j) x := by ext i by_cases h : p i · rw [dif_pos h, Function.update_apply_equiv_apply, Equiv.symm_symm, Function.update_apply, Function.update_apply, dif_pos h] have h_coe : (⟨i, h⟩ : Subtype p) = e j ↔ i = e j := Subtype.ext_iff.trans (by rw [Subtype.coe_mk]) simp [h_coe] · have : i ≠ e j := by contrapose! h have : p (e j : α) := (e j).2 rwa [← h] at this simp [h, this] section Swap variable {α : Type*} [DecidableEq α] {a b : α} {s : Set α} theorem Equiv.swap_bijOn_self (hs : a ∈ s ↔ b ∈ s) : BijOn (Equiv.swap a b) s s := by refine ⟨fun x hx ↦ ?_, (Equiv.injective _).injOn, fun x hx ↦ ?_⟩ · obtain (rfl | hxa) := eq_or_ne x a · rwa [swap_apply_left, ← hs] obtain (rfl | hxb) := eq_or_ne x b · rwa [swap_apply_right, hs] rwa [swap_apply_of_ne_of_ne hxa hxb] obtain (rfl | hxa) := eq_or_ne x a · simp [hs.1 hx] obtain (rfl | hxb) := eq_or_ne x b · simp [hs.2 hx] exact ⟨x, hx, swap_apply_of_ne_of_ne hxa hxb⟩
theorem Equiv.swap_bijOn_exchange (ha : a ∈ s) (hb : b ∉ s) : BijOn (Equiv.swap a b) s (insert b (s \ {a})) := by refine ⟨fun x hx ↦ ?_, (Equiv.injective _).injOn, fun x hx ↦ ?_⟩ · obtain (rfl | hxa) := eq_or_ne x a · simp [swap_apply_left] rw [swap_apply_of_ne_of_ne hxa (by rintro rfl; contradiction)]
Mathlib/Logic/Equiv/Set.lean
642
648
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Floris van Doorn -/ import Mathlib.Analysis.Calculus.ContDiff.Defs import Mathlib.Analysis.Calculus.ContDiff.FaaDiBruno import Mathlib.Analysis.Calculus.FDeriv.Add import Mathlib.Analysis.Calculus.FDeriv.Mul /-! # Higher differentiability of composition We prove that the composition of `C^n` functions is `C^n`. We also expand the API around `C^n` functions. ## Main results * `ContDiff.comp` states that the composition of two `C^n` functions is `C^n`. Similar results are given for `C^n` functions on domains. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. In this file, we denote `(⊤ : ℕ∞) : WithTop ℕ∞` with `∞` and `⊤ : WithTop ℕ∞` with `ω`. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ noncomputable section open scoped NNReal Nat ContDiff universe u uE uF uG attribute [local instance 1001] NormedAddCommGroup.toAddCommGroup AddCommGroup.toAddCommMonoid open Set Fin Filter Function open scoped Topology variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type uE} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {F : Type uF} [NormedAddCommGroup F] [NormedSpace 𝕜 F] {G : Type uG} [NormedAddCommGroup G] [NormedSpace 𝕜 G] {X : Type*} [NormedAddCommGroup X] [NormedSpace 𝕜 X] {s t : Set E} {f : E → F} {g : F → G} {x x₀ : E} {b : E × F → G} {m n : WithTop ℕ∞} {p : E → FormalMultilinearSeries 𝕜 E F} /-! ### Constants -/ section constants theorem iteratedFDerivWithin_succ_const (n : ℕ) (c : F) : iteratedFDerivWithin 𝕜 (n + 1) (fun _ : E ↦ c) s = 0 := by induction n with | zero => ext1 simp [iteratedFDerivWithin_succ_eq_comp_left, iteratedFDerivWithin_zero_eq_comp, comp_def] | succ n IH => rw [iteratedFDerivWithin_succ_eq_comp_left, IH] simp only [Pi.zero_def, comp_def, fderivWithin_const, map_zero] @[simp] theorem iteratedFDerivWithin_zero_fun {i : ℕ} : iteratedFDerivWithin 𝕜 i (fun _ : E ↦ (0 : F)) s = 0 := by cases i with | zero => ext; simp | succ i => apply iteratedFDerivWithin_succ_const @[simp] theorem iteratedFDeriv_zero_fun {n : ℕ} : (iteratedFDeriv 𝕜 n fun _ : E ↦ (0 : F)) = 0 := funext fun x ↦ by simp only [← iteratedFDerivWithin_univ, iteratedFDerivWithin_zero_fun] theorem contDiff_zero_fun : ContDiff 𝕜 n fun _ : E => (0 : F) := analyticOnNhd_const.contDiff /-- Constants are `C^∞`. -/ theorem contDiff_const {c : F} : ContDiff 𝕜 n fun _ : E => c := analyticOnNhd_const.contDiff theorem contDiffOn_const {c : F} {s : Set E} : ContDiffOn 𝕜 n (fun _ : E => c) s := contDiff_const.contDiffOn theorem contDiffAt_const {c : F} : ContDiffAt 𝕜 n (fun _ : E => c) x := contDiff_const.contDiffAt theorem contDiffWithinAt_const {c : F} : ContDiffWithinAt 𝕜 n (fun _ : E => c) s x := contDiffAt_const.contDiffWithinAt @[nontriviality] theorem contDiff_of_subsingleton [Subsingleton F] : ContDiff 𝕜 n f := by rw [Subsingleton.elim f fun _ => 0]; exact contDiff_const @[nontriviality] theorem contDiffAt_of_subsingleton [Subsingleton F] : ContDiffAt 𝕜 n f x := by rw [Subsingleton.elim f fun _ => 0]; exact contDiffAt_const @[nontriviality] theorem contDiffWithinAt_of_subsingleton [Subsingleton F] : ContDiffWithinAt 𝕜 n f s x := by rw [Subsingleton.elim f fun _ => 0]; exact contDiffWithinAt_const @[nontriviality] theorem contDiffOn_of_subsingleton [Subsingleton F] : ContDiffOn 𝕜 n f s := by rw [Subsingleton.elim f fun _ => 0]; exact contDiffOn_const theorem iteratedFDerivWithin_const_of_ne {n : ℕ} (hn : n ≠ 0) (c : F) (s : Set E) : iteratedFDerivWithin 𝕜 n (fun _ : E ↦ c) s = 0 := by cases n with | zero => contradiction | succ n => exact iteratedFDerivWithin_succ_const n c theorem iteratedFDeriv_const_of_ne {n : ℕ} (hn : n ≠ 0) (c : F) : (iteratedFDeriv 𝕜 n fun _ : E ↦ c) = 0 := by simp only [← iteratedFDerivWithin_univ, iteratedFDerivWithin_const_of_ne hn] theorem iteratedFDeriv_succ_const (n : ℕ) (c : F) : (iteratedFDeriv 𝕜 (n + 1) fun _ : E ↦ c) = 0 := iteratedFDeriv_const_of_ne (by simp) _ theorem contDiffWithinAt_singleton : ContDiffWithinAt 𝕜 n f {x} x := (contDiffWithinAt_const (c := f x)).congr (by simp) rfl end constants /-! ### Smoothness of linear functions -/ section linear /-- Unbundled bounded linear functions are `C^n`. -/ theorem IsBoundedLinearMap.contDiff (hf : IsBoundedLinearMap 𝕜 f) : ContDiff 𝕜 n f := (ContinuousLinearMap.analyticOnNhd hf.toContinuousLinearMap univ).contDiff theorem ContinuousLinearMap.contDiff (f : E →L[𝕜] F) : ContDiff 𝕜 n f := f.isBoundedLinearMap.contDiff theorem ContinuousLinearEquiv.contDiff (f : E ≃L[𝕜] F) : ContDiff 𝕜 n f :=
(f : E →L[𝕜] F).contDiff theorem LinearIsometry.contDiff (f : E →ₗᵢ[𝕜] F) : ContDiff 𝕜 n f := f.toContinuousLinearMap.contDiff theorem LinearIsometryEquiv.contDiff (f : E ≃ₗᵢ[𝕜] F) : ContDiff 𝕜 n f :=
Mathlib/Analysis/Calculus/ContDiff/Basic.lean
140
145
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Data.NNReal.Basic import Mathlib.Order.Fin.Tuple import Mathlib.Order.Interval.Set.Monotone import Mathlib.Topology.MetricSpace.Basic import Mathlib.Topology.MetricSpace.Bounded import Mathlib.Topology.MetricSpace.Pseudo.Real import Mathlib.Topology.Order.MonotoneConvergence /-! # Rectangular boxes in `ℝⁿ` In this file we define rectangular boxes in `ℝⁿ`. As usual, we represent `ℝⁿ` as the type of functions `ι → ℝ` (usually `ι = Fin n` for some `n`). When we need to interpret a box `[l, u]` as a set, we use the product `{x | ∀ i, l i < x i ∧ x i ≤ u i}` of half-open intervals `(l i, u i]`. We exclude `l i` because this way boxes of a partition are disjoint as sets in `ℝⁿ`. Currently, the only use cases for these constructions are the definitions of Riemann-style integrals (Riemann, Henstock-Kurzweil, McShane). ## Main definitions We use the same structure `BoxIntegral.Box` both for ambient boxes and for elements of a partition. Each box is stored as two points `lower upper : ι → ℝ` and a proof of `∀ i, lower i < upper i`. We define instances `Membership (ι → ℝ) (Box ι)` and `CoeTC (Box ι) (Set <| ι → ℝ)` so that each box is interpreted as the set `{x | ∀ i, x i ∈ Set.Ioc (I.lower i) (I.upper i)}`. This way boxes of a partition are pairwise disjoint and their union is exactly the original box. We require boxes to be nonempty, because this way coercion to sets is injective. The empty box can be represented as `⊥ : WithBot (BoxIntegral.Box ι)`. We define the following operations on boxes: * coercion to `Set (ι → ℝ)` and `Membership (ι → ℝ) (BoxIntegral.Box ι)` as described above; * `PartialOrder` and `SemilatticeSup` instances such that `I ≤ J` is equivalent to `(I : Set (ι → ℝ)) ⊆ J`; * `Lattice` instances on `WithBot (BoxIntegral.Box ι)`; * `BoxIntegral.Box.Icc`: the closed box `Set.Icc I.lower I.upper`; defined as a bundled monotone map from `Box ι` to `Set (ι → ℝ)`; * `BoxIntegral.Box.face I i : Box (Fin n)`: a hyperface of `I : BoxIntegral.Box (Fin (n + 1))`; * `BoxIntegral.Box.distortion`: the maximal ratio of two lengths of edges of a box; defined as the supremum of `nndist I.lower I.upper / nndist (I.lower i) (I.upper i)`. We also provide a convenience constructor `BoxIntegral.Box.mk' (l u : ι → ℝ) : WithBot (Box ι)` that returns the box `⟨l, u, _⟩` if it is nonempty and `⊥` otherwise. ## Tags rectangular box -/ open Set Function Metric Filter noncomputable section open scoped NNReal Topology namespace BoxIntegral variable {ι : Type*} /-! ### Rectangular box: definition and partial order -/ /-- A nontrivial rectangular box in `ι → ℝ` with corners `lower` and `upper`. Represents the product of half-open intervals `(lower i, upper i]`. -/ structure Box (ι : Type*) where /-- coordinates of the lower and upper corners of the box -/ (lower upper : ι → ℝ) /-- Each lower coordinate is less than its upper coordinate: i.e., the box is non-empty -/ lower_lt_upper : ∀ i, lower i < upper i attribute [simp] Box.lower_lt_upper namespace Box variable (I J : Box ι) {x y : ι → ℝ} instance : Inhabited (Box ι) := ⟨⟨0, 1, fun _ ↦ zero_lt_one⟩⟩ theorem lower_le_upper : I.lower ≤ I.upper := fun i ↦ (I.lower_lt_upper i).le theorem lower_ne_upper (i) : I.lower i ≠ I.upper i := (I.lower_lt_upper i).ne instance : Membership (ι → ℝ) (Box ι) := ⟨fun I x ↦ ∀ i, x i ∈ Ioc (I.lower i) (I.upper i)⟩ /-- The set of points in this box: this is the product of half-open intervals `(lower i, upper i]`, where `lower` and `upper` are this box' corners. -/ @[coe] def toSet (I : Box ι) : Set (ι → ℝ) := { x | x ∈ I } instance : CoeTC (Box ι) (Set <| ι → ℝ) := ⟨toSet⟩ @[simp] theorem mem_mk {l u x : ι → ℝ} {H} : x ∈ mk l u H ↔ ∀ i, x i ∈ Ioc (l i) (u i) := Iff.rfl @[simp, norm_cast] theorem mem_coe : x ∈ (I : Set (ι → ℝ)) ↔ x ∈ I := Iff.rfl theorem mem_def : x ∈ I ↔ ∀ i, x i ∈ Ioc (I.lower i) (I.upper i) := Iff.rfl theorem mem_univ_Ioc {I : Box ι} : (x ∈ pi univ fun i ↦ Ioc (I.lower i) (I.upper i)) ↔ x ∈ I := mem_univ_pi theorem coe_eq_pi : (I : Set (ι → ℝ)) = pi univ fun i ↦ Ioc (I.lower i) (I.upper i) := Set.ext fun _ ↦ mem_univ_Ioc.symm @[simp] theorem upper_mem : I.upper ∈ I := fun i ↦ right_mem_Ioc.2 <| I.lower_lt_upper i theorem exists_mem : ∃ x, x ∈ I := ⟨_, I.upper_mem⟩ theorem nonempty_coe : Set.Nonempty (I : Set (ι → ℝ)) := I.exists_mem @[simp] theorem coe_ne_empty : (I : Set (ι → ℝ)) ≠ ∅ := I.nonempty_coe.ne_empty @[simp] theorem empty_ne_coe : ∅ ≠ (I : Set (ι → ℝ)) := I.coe_ne_empty.symm instance : LE (Box ι) := ⟨fun I J ↦ ∀ ⦃x⦄, x ∈ I → x ∈ J⟩ theorem le_def : I ≤ J ↔ ∀ x ∈ I, x ∈ J := Iff.rfl theorem le_TFAE : List.TFAE [I ≤ J, (I : Set (ι → ℝ)) ⊆ J, Icc I.lower I.upper ⊆ Icc J.lower J.upper, J.lower ≤ I.lower ∧ I.upper ≤ J.upper] := by tfae_have 1 ↔ 2 := Iff.rfl tfae_have 2 → 3 | h => by simpa [coe_eq_pi, closure_pi_set, lower_ne_upper] using closure_mono h tfae_have 3 ↔ 4 := Icc_subset_Icc_iff I.lower_le_upper tfae_have 4 → 2 | h, x, hx, i => Ioc_subset_Ioc (h.1 i) (h.2 i) (hx i) tfae_finish variable {I J} @[simp, norm_cast] theorem coe_subset_coe : (I : Set (ι → ℝ)) ⊆ J ↔ I ≤ J := Iff.rfl theorem le_iff_bounds : I ≤ J ↔ J.lower ≤ I.lower ∧ I.upper ≤ J.upper := (le_TFAE I J).out 0 3 theorem injective_coe : Injective ((↑) : Box ι → Set (ι → ℝ)) := by rintro ⟨l₁, u₁, h₁⟩ ⟨l₂, u₂, h₂⟩ h simp only [Subset.antisymm_iff, coe_subset_coe, le_iff_bounds] at h congr exacts [le_antisymm h.2.1 h.1.1, le_antisymm h.1.2 h.2.2] @[simp, norm_cast] theorem coe_inj : (I : Set (ι → ℝ)) = J ↔ I = J := injective_coe.eq_iff @[ext] theorem ext (H : ∀ x, x ∈ I ↔ x ∈ J) : I = J := injective_coe <| Set.ext H theorem ne_of_disjoint_coe (h : Disjoint (I : Set (ι → ℝ)) J) : I ≠ J := mt coe_inj.2 <| h.ne I.coe_ne_empty instance : PartialOrder (Box ι) := { PartialOrder.lift ((↑) : Box ι → Set (ι → ℝ)) injective_coe with le := (· ≤ ·) } /-- Closed box corresponding to `I : BoxIntegral.Box ι`. -/ protected def Icc : Box ι ↪o Set (ι → ℝ) := OrderEmbedding.ofMapLEIff (fun I : Box ι ↦ Icc I.lower I.upper) fun I J ↦ (le_TFAE I J).out 2 0 theorem Icc_def : Box.Icc I = Icc I.lower I.upper := rfl @[simp] theorem upper_mem_Icc (I : Box ι) : I.upper ∈ Box.Icc I := right_mem_Icc.2 I.lower_le_upper @[simp] theorem lower_mem_Icc (I : Box ι) : I.lower ∈ Box.Icc I := left_mem_Icc.2 I.lower_le_upper protected theorem isCompact_Icc (I : Box ι) : IsCompact (Box.Icc I) := isCompact_Icc theorem Icc_eq_pi : Box.Icc I = pi univ fun i ↦ Icc (I.lower i) (I.upper i) := (pi_univ_Icc _ _).symm theorem le_iff_Icc : I ≤ J ↔ Box.Icc I ⊆ Box.Icc J := (le_TFAE I J).out 0 2 theorem antitone_lower : Antitone fun I : Box ι ↦ I.lower := fun _ _ H ↦ (le_iff_bounds.1 H).1 theorem monotone_upper : Monotone fun I : Box ι ↦ I.upper := fun _ _ H ↦ (le_iff_bounds.1 H).2 theorem coe_subset_Icc : ↑I ⊆ Box.Icc I := fun _ hx ↦ ⟨fun i ↦ (hx i).1.le, fun i ↦ (hx i).2⟩ theorem isBounded_Icc [Finite ι] (I : Box ι) : Bornology.IsBounded (Box.Icc I) := by cases nonempty_fintype ι exact Metric.isBounded_Icc _ _ theorem isBounded [Finite ι] (I : Box ι) : Bornology.IsBounded I.toSet := Bornology.IsBounded.subset I.isBounded_Icc coe_subset_Icc /-! ### Supremum of two boxes -/ /-- `I ⊔ J` is the least box that includes both `I` and `J`. Since `↑I ∪ ↑J` is usually not a box, `↑(I ⊔ J)` is larger than `↑I ∪ ↑J`. -/ instance : SemilatticeSup (Box ι) := { sup := fun I J ↦ ⟨I.lower ⊓ J.lower, I.upper ⊔ J.upper, fun i ↦ (min_le_left _ _).trans_lt <| (I.lower_lt_upper i).trans_le (le_max_left _ _)⟩ le_sup_left := fun _ _ ↦ le_iff_bounds.2 ⟨inf_le_left, le_sup_left⟩ le_sup_right := fun _ _ ↦ le_iff_bounds.2 ⟨inf_le_right, le_sup_right⟩ sup_le := fun _ _ _ h₁ h₂ ↦ le_iff_bounds.2 ⟨le_inf (antitone_lower h₁) (antitone_lower h₂), sup_le (monotone_upper h₁) (monotone_upper h₂)⟩ } /-! ### `WithBot (Box ι)` In this section we define coercion from `WithBot (Box ι)` to `Set (ι → ℝ)` by sending `⊥` to `∅`. -/ /-- The set underlying this box: `⊥` is mapped to `∅`. -/ @[coe] def withBotToSet (o : WithBot (Box ι)) : Set (ι → ℝ) := o.elim ∅ (↑) instance withBotCoe : CoeTC (WithBot (Box ι)) (Set (ι → ℝ)) := ⟨withBotToSet⟩ @[simp, norm_cast] theorem coe_bot : ((⊥ : WithBot (Box ι)) : Set (ι → ℝ)) = ∅ := rfl @[simp, norm_cast] theorem coe_coe : ((I : WithBot (Box ι)) : Set (ι → ℝ)) = I := rfl theorem isSome_iff : ∀ {I : WithBot (Box ι)}, I.isSome ↔ (I : Set (ι → ℝ)).Nonempty | ⊥ => by unfold Option.isSome simp | (I : Box ι) => by unfold Option.isSome simp [I.nonempty_coe] theorem biUnion_coe_eq_coe (I : WithBot (Box ι)) : ⋃ (J : Box ι) (_ : ↑J = I), (J : Set (ι → ℝ)) = I := by induction I <;> simp [WithBot.coe_eq_coe] @[simp, norm_cast] theorem withBotCoe_subset_iff {I J : WithBot (Box ι)} : (I : Set (ι → ℝ)) ⊆ J ↔ I ≤ J := by induction I; · simp induction J; · simp [subset_empty_iff] simp [le_def] @[simp, norm_cast] theorem withBotCoe_inj {I J : WithBot (Box ι)} : (I : Set (ι → ℝ)) = J ↔ I = J := by simp only [Subset.antisymm_iff, ← le_antisymm_iff, withBotCoe_subset_iff] open scoped Classical in /-- Make a `WithBot (Box ι)` from a pair of corners `l u : ι → ℝ`. If `l i < u i` for all `i`, then the result is `⟨l, u, _⟩ : Box ι`, otherwise it is `⊥`. In any case, the result interpreted as a set in `ι → ℝ` is the set `{x : ι → ℝ | ∀ i, x i ∈ Ioc (l i) (u i)}`. -/ def mk' (l u : ι → ℝ) : WithBot (Box ι) := if h : ∀ i, l i < u i then ↑(⟨l, u, h⟩ : Box ι) else ⊥ @[simp] theorem mk'_eq_bot {l u : ι → ℝ} : mk' l u = ⊥ ↔ ∃ i, u i ≤ l i := by rw [mk'] split_ifs with h <;> simpa using h @[simp] theorem mk'_eq_coe {l u : ι → ℝ} : mk' l u = I ↔ l = I.lower ∧ u = I.upper := by obtain ⟨lI, uI, hI⟩ := I; rw [mk']; split_ifs with h · simp [WithBot.coe_eq_coe] · suffices l = lI → u ≠ uI by simpa rintro rfl rfl exact h hI @[simp] theorem coe_mk' (l u : ι → ℝ) : (mk' l u : Set (ι → ℝ)) = pi univ fun i ↦ Ioc (l i) (u i) := by rw [mk']; split_ifs with h · exact coe_eq_pi _ · rcases not_forall.mp h with ⟨i, hi⟩
rw [coe_bot, univ_pi_eq_empty] exact Ioc_eq_empty hi
Mathlib/Analysis/BoxIntegral/Box/Basic.lean
300
302
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Computability.Halting import Mathlib.Computability.TuringMachine import Mathlib.Data.Num.Lemmas import Mathlib.Tactic.DeriveFintype import Mathlib.Computability.TMConfig /-! # Modelling partial recursive functions using Turing machines The files `TMConfig` and `TMToPartrec` define a simplified basis for partial recursive functions, and a `Turing.TM2` model Turing machine for evaluating these functions. This amounts to a constructive proof that every `Partrec` function can be evaluated by a Turing machine. ## Main definitions * `PartrecToTM2.tr`: A TM2 turing machine which can evaluate `code` programs -/ open List (Vector) open Function (update) open Relation namespace Turing /-! ## Simulating sequentialized partial recursive functions in TM2 At this point we have a sequential model of partial recursive functions: the `Cfg` type and `step : Cfg → Option Cfg` function from `TMConfig.lean`. The key feature of this model is that it does a finite amount of computation (in fact, an amount which is statically bounded by the size of the program) between each step, and no individual step can diverge (unlike the compositional semantics, where every sub-part of the computation is potentially divergent). So we can utilize the same techniques as in the other TM simulations in `Computability.TuringMachine` to prove that each step corresponds to a finite number of steps in a lower level model. (We don't prove it here, but in anticipation of the complexity class P, the simulation is actually polynomial-time as well.) The target model is `Turing.TM2`, which has a fixed finite set of stacks, a bit of local storage, with programs selected from a potentially infinite (but finitely accessible) set of program positions, or labels `Λ`, each of which executes a finite sequence of basic stack commands. For this program we will need four stacks, each on an alphabet `Γ'` like so: inductive Γ' | consₗ | cons | bit0 | bit1 We represent a number as a bit sequence, lists of numbers by putting `cons` after each element, and lists of lists of natural numbers by putting `consₗ` after each list. For example: 0 ~> [] 1 ~> [bit1] 6 ~> [bit0, bit1, bit1] [1, 2] ~> [bit1, cons, bit0, bit1, cons] [[], [1, 2]] ~> [consₗ, bit1, cons, bit0, bit1, cons, consₗ] The four stacks are `main`, `rev`, `aux`, `stack`. In normal mode, `main` contains the input to the current program (a `List ℕ`) and `stack` contains data (a `List (List ℕ)`) associated to the current continuation, and in `ret` mode `main` contains the value that is being passed to the continuation and `stack` contains the data for the continuation. The `rev` and `aux` stacks are usually empty; `rev` is used to store reversed data when e.g. moving a value from one stack to another, while `aux` is used as a temporary for a `main`/`stack` swap that happens during `cons₁` evaluation. The only local store we need is `Option Γ'`, which stores the result of the last pop operation. (Most of our working data are natural numbers, which are too large to fit in the local store.) The continuations from the previous section are data-carrying, containing all the values that have been computed and are awaiting other arguments. In order to have only a finite number of continuations appear in the program so that they can be used in machine states, we separate the data part (anything with type `List ℕ`) from the `Cont` type, producing a `Cont'` type that lacks this information. The data is kept on the `stack` stack. Because we want to have subroutines for e.g. moving an entire stack to another place, we use an infinite inductive type `Λ'` so that we can execute a program and then return to do something else without having to define too many different kinds of intermediate states. (We must nevertheless prove that only finitely many labels are accessible.) The labels are: * `move p k₁ k₂ q`: move elements from stack `k₁` to `k₂` while `p` holds of the value being moved. The last element, that fails `p`, is placed in neither stack but left in the local store. At the end of the operation, `k₂` will have the elements of `k₁` in reverse order. Then do `q`. * `clear p k q`: delete elements from stack `k` until `p` is true. Like `move`, the last element is left in the local storage. Then do `q`. * `copy q`: Move all elements from `rev` to both `main` and `stack` (in reverse order), then do `q`. That is, it takes `(a, b, c, d)` to `(b.reverse ++ a, [], c, b.reverse ++ d)`. * `push k f q`: push `f s`, where `s` is the local store, to stack `k`, then do `q`. This is a duplicate of the `push` instruction that is part of the TM2 model, but by having a subroutine just for this purpose we can build up programs to execute inside a `goto` statement, where we have the flexibility to be general recursive. * `read (f : Option Γ' → Λ')`: go to state `f s` where `s` is the local store. Again this is only here for convenience. * `succ q`: perform a successor operation. Assuming `[n]` is encoded on `main` before, `[n+1]` will be on main after. This implements successor for binary natural numbers. * `pred q₁ q₂`: perform a predecessor operation or `case` statement. If `[]` is encoded on `main` before, then we transition to `q₁` with `[]` on main; if `(0 :: v)` is on `main` before then `v` will be on `main` after and we transition to `q₁`; and if `(n+1 :: v)` is on `main` before then `n :: v` will be on `main` after and we transition to `q₂`. * `ret k`: call continuation `k`. Each continuation has its own interpretation of the data in `stack` and sets up the data for the next continuation. * `ret (cons₁ fs k)`: `v :: KData` on `stack` and `ns` on `main`, and the next step expects `v` on `main` and `ns :: KData` on `stack`. So we have to do a little dance here with six reverse-moves using the `aux` stack to perform a three-point swap, each of which involves two reversals. * `ret (cons₂ k)`: `ns :: KData` is on `stack` and `v` is on `main`, and we have to put `ns.headI :: v` on `main` and `KData` on `stack`. This is done using the `head` subroutine. * `ret (fix f k)`: This stores no data, so we just check if `main` starts with `0` and if so, remove it and call `k`, otherwise `clear` the first value and call `f`. * `ret halt`: the stack is empty, and `main` has the output. Do nothing and halt. In addition to these basic states, we define some additional subroutines that are used in the above: * `push'`, `peek'`, `pop'` are special versions of the builtins that use the local store to supply inputs and outputs. * `unrev`: special case `move false rev main` to move everything from `rev` back to `main`. Used as a cleanup operation in several functions. * `moveExcl p k₁ k₂ q`: same as `move` but pushes the last value read back onto the source stack. * `move₂ p k₁ k₂ q`: double `move`, so that the result comes out in the right order at the target stack. Implemented as `moveExcl p k rev; move false rev k₂`. Assumes that neither `k₁` nor `k₂` is `rev` and `rev` is initially empty. * `head k q`: get the first natural number from stack `k` and reverse-move it to `rev`, then clear the rest of the list at `k` and then `unrev` to reverse-move the head value to `main`. This is used with `k = main` to implement regular `head`, i.e. if `v` is on `main` before then `[v.headI]` will be on `main` after; and also with `k = stack` for the `cons` operation, which has `v` on `main` and `ns :: KData` on `stack`, and results in `KData` on `stack` and `ns.headI :: v` on `main`. * `trNormal` is the main entry point, defining states that perform a given `code` computation. It mostly just dispatches to functions written above. The main theorem of this section is `tr_eval`, which asserts that for each that for each code `c`, the state `init c v` steps to `halt v'` in finitely many steps if and only if `Code.eval c v = some v'`. -/ namespace PartrecToTM2 section open ToPartrec /-- The alphabet for the stacks in the program. `bit0` and `bit1` are used to represent `ℕ` values as lists of binary digits, `cons` is used to separate `List ℕ` values, and `consₗ` is used to separate `List (List ℕ)` values. See the section documentation. -/ inductive Γ' | consₗ | cons | bit0
| bit1 deriving DecidableEq, Inhabited, Fintype
Mathlib/Computability/TMToPartrec.lean
158
160
/- Copyright (c) 2022 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import Mathlib.Algebra.Field.ULift import Mathlib.Algebra.MvPolynomial.Cardinal import Mathlib.Data.Nat.Factorization.PrimePow import Mathlib.Data.Rat.Encodable import Mathlib.FieldTheory.Finite.GaloisField import Mathlib.RingTheory.Localization.Cardinality import Mathlib.SetTheory.Cardinal.Divisibility /-! # Cardinality of Fields In this file we show all the possible cardinalities of fields. All infinite cardinals can harbour a field structure, and so can all types with prime power cardinalities, and this is sharp. ## Main statements * `Fintype.nonempty_field_iff`: A `Fintype` can be given a field structure iff its cardinality is a prime power. * `Infinite.nonempty_field` : Any infinite type can be endowed a field structure. * `Field.nonempty_iff` : There is a field structure on type iff its cardinality is a prime power. -/ local notation "‖" x "‖" => Fintype.card x open scoped Cardinal nonZeroDivisors universe u /-- A finite field has prime power cardinality. -/ theorem Fintype.isPrimePow_card_of_field {α} [Fintype α] [Field α] : IsPrimePow ‖α‖ := by -- TODO: `Algebra` version of `CharP.exists`, of type `∀ p, Algebra (ZMod p) α` obtain ⟨p, _⟩ := CharP.exists α haveI hp := Fact.mk (CharP.char_is_prime α p) letI : Algebra (ZMod p) α := ZMod.algebra _ _ let b := IsNoetherian.finsetBasis (ZMod p) α rw [Module.card_fintype b, ZMod.card, isPrimePow_pow_iff] · exact hp.1.isPrimePow rw [← Module.finrank_eq_card_basis b] exact Module.finrank_pos.ne' /-- A `Fintype` can be given a field structure iff its cardinality is a prime power. -/ theorem Fintype.nonempty_field_iff {α} [Fintype α] : Nonempty (Field α) ↔ IsPrimePow ‖α‖ := by refine ⟨fun ⟨h⟩ => Fintype.isPrimePow_card_of_field, ?_⟩ rintro ⟨p, n, hp, hn, hα⟩ haveI := Fact.mk hp.nat_prime haveI : Fintype (GaloisField p n) := Fintype.ofFinite (GaloisField p n) exact ⟨(Fintype.equivOfCardEq (((Fintype.card_eq_nat_card).trans (GaloisField.card p n hn.ne')).trans hα)).symm.field⟩ theorem Fintype.not_isField_of_card_not_prime_pow {α} [Fintype α] [Ring α] : ¬IsPrimePow ‖α‖ → ¬IsField α := mt fun h => Fintype.nonempty_field_iff.mp ⟨h.toField⟩ /-- Any infinite type can be endowed a field structure. -/ theorem Infinite.nonempty_field {α : Type u} [Infinite α] : Nonempty (Field α) := by suffices #α = #(FractionRing (MvPolynomial α <| ULift.{u} ℚ)) from (Cardinal.eq.1 this).map (·.field) simp /-- There is a field structure on type if and only if its cardinality is a prime power. -/ theorem Field.nonempty_iff {α : Type u} : Nonempty (Field α) ↔ IsPrimePow #α := by rw [Cardinal.isPrimePow_iff] obtain h | h := fintypeOrInfinite α · simpa only [Cardinal.mk_fintype, Nat.cast_inj, exists_eq_left', (Cardinal.nat_lt_aleph0 _).not_le, false_or] using Fintype.nonempty_field_iff · simpa only [← Cardinal.infinite_iff, h, true_or, iff_true] using Infinite.nonempty_field
Mathlib/FieldTheory/Cardinality.lean
80
85
/- 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.MeasurableSpace.Basic import Mathlib.MeasureTheory.Measure.MeasureSpaceDef /-! # Sequence of measurable functions associated to a sequence of a.e.-measurable functions We define here tools to prove statements about limits (infi, supr...) of sequences of `AEMeasurable` functions. Given a sequence of a.e.-measurable functions `f : ι → α → β` with hypothesis `hf : ∀ i, AEMeasurable (f i) μ`, and a pointwise property `p : α → (ι → β) → Prop` such that we have `hp : ∀ᵐ x ∂μ, p x (fun n ↦ f n x)`, we define a sequence of measurable functions `aeSeq hf p` and a measurable set `aeSeqSet hf p`, such that * `μ (aeSeqSet hf p)ᶜ = 0` * `x ∈ aeSeqSet hf p → ∀ i : ι, aeSeq hf hp i x = f i x` * `x ∈ aeSeqSet hf p → p x (fun n ↦ f n x)` -/ open MeasureTheory variable {ι : Sort*} {α β γ : Type*} [MeasurableSpace α] [MeasurableSpace β] {f : ι → α → β} {μ : Measure α} {p : α → (ι → β) → Prop} /-- If we have the additional hypothesis `∀ᵐ x ∂μ, p x (fun n ↦ f n x)`, this is a measurable set whose complement has measure 0 such that for all `x ∈ aeSeqSet`, `f i x` is equal to `(hf i).mk (f i) x` for all `i` and we have the pointwise property `p x (fun n ↦ f n x)`. -/ def aeSeqSet (hf : ∀ i, AEMeasurable (f i) μ) (p : α → (ι → β) → Prop) : Set α := (toMeasurable μ { x | (∀ i, f i x = (hf i).mk (f i) x) ∧ p x fun n => f n x }ᶜ)ᶜ open Classical in /-- A sequence of measurable functions that are equal to `f` and verify property `p` on the measurable set `aeSeqSet hf p`. -/ noncomputable def aeSeq (hf : ∀ i, AEMeasurable (f i) μ) (p : α → (ι → β) → Prop) : ι → α → β := fun i x => ite (x ∈ aeSeqSet hf p) ((hf i).mk (f i) x) (⟨f i x⟩ : Nonempty β).some namespace aeSeq section MemAESeqSet theorem mk_eq_fun_of_mem_aeSeqSet (hf : ∀ i, AEMeasurable (f i) μ) {x : α} (hx : x ∈ aeSeqSet hf p) (i : ι) : (hf i).mk (f i) x = f i x := haveI h_ss : aeSeqSet hf p ⊆ { x | ∀ i, f i x = (hf i).mk (f i) x } := by rw [aeSeqSet, ← compl_compl { x | ∀ i, f i x = (hf i).mk (f i) x }, Set.compl_subset_compl] refine Set.Subset.trans (Set.compl_subset_compl.mpr fun x h => ?_) (subset_toMeasurable _ _) exact h.1 (h_ss hx i).symm theorem aeSeq_eq_mk_of_mem_aeSeqSet (hf : ∀ i, AEMeasurable (f i) μ) {x : α} (hx : x ∈ aeSeqSet hf p) (i : ι) : aeSeq hf p i x = (hf i).mk (f i) x := by simp only [aeSeq, hx, if_true] theorem aeSeq_eq_fun_of_mem_aeSeqSet (hf : ∀ i, AEMeasurable (f i) μ) {x : α} (hx : x ∈ aeSeqSet hf p) (i : ι) : aeSeq hf p i x = f i x := by simp only [aeSeq_eq_mk_of_mem_aeSeqSet hf hx i, mk_eq_fun_of_mem_aeSeqSet hf hx i] theorem prop_of_mem_aeSeqSet (hf : ∀ i, AEMeasurable (f i) μ) {x : α} (hx : x ∈ aeSeqSet hf p) : p x fun n => aeSeq hf p n x := by simp only [aeSeq, hx, if_true] rw [funext fun n => mk_eq_fun_of_mem_aeSeqSet hf hx n] have h_ss : aeSeqSet hf p ⊆ { x | p x fun n => f n x } := by rw [← compl_compl { x | p x fun n => f n x }, aeSeqSet, Set.compl_subset_compl] refine Set.Subset.trans (Set.compl_subset_compl.mpr ?_) (subset_toMeasurable _ _) exact fun x hx => hx.2 have hx' := Set.mem_of_subset_of_mem h_ss hx exact hx' theorem fun_prop_of_mem_aeSeqSet (hf : ∀ i, AEMeasurable (f i) μ) {x : α} (hx : x ∈ aeSeqSet hf p) : p x fun n => f n x := by have h_eq : (fun n => f n x) = fun n => aeSeq hf p n x := funext fun n => (aeSeq_eq_fun_of_mem_aeSeqSet hf hx n).symm rw [h_eq] exact prop_of_mem_aeSeqSet hf hx end MemAESeqSet theorem aeSeqSet_measurableSet {hf : ∀ i, AEMeasurable (f i) μ} : MeasurableSet (aeSeqSet hf p) := (measurableSet_toMeasurable _ _).compl theorem measurable (hf : ∀ i, AEMeasurable (f i) μ) (p : α → (ι → β) → Prop) (i : ι) : Measurable (aeSeq hf p i) := Measurable.ite aeSeqSet_measurableSet (hf i).measurable_mk <| measurable_const' fun _ _ => rfl theorem measure_compl_aeSeqSet_eq_zero [Countable ι] (hf : ∀ i, AEMeasurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x fun n => f n x) : μ (aeSeqSet hf p)ᶜ = 0 := by rw [aeSeqSet, compl_compl, measure_toMeasurable] have hf_eq := fun i => (hf i).ae_eq_mk simp_rw [Filter.EventuallyEq, ← ae_all_iff] at hf_eq exact Filter.Eventually.and hf_eq hp theorem aeSeq_eq_mk_ae [Countable ι] (hf : ∀ i, AEMeasurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x fun n => f n x) : ∀ᵐ a : α ∂μ, ∀ i : ι, aeSeq hf p i a = (hf i).mk (f i) a := have h_ss : aeSeqSet hf p ⊆ { a : α | ∀ i, aeSeq hf p i a = (hf i).mk (f i) a } := fun x hx i => by simp only [aeSeq, hx, if_true] (ae_iff.2 (measure_compl_aeSeqSet_eq_zero hf hp)).mono h_ss theorem aeSeq_eq_fun_ae [Countable ι] (hf : ∀ i, AEMeasurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x fun n => f n x) : ∀ᵐ a : α ∂μ, ∀ i : ι, aeSeq hf p i a = f i a := haveI h_ss : { a : α | ¬∀ i : ι, aeSeq hf p i a = f i a } ⊆ (aeSeqSet hf p)ᶜ := fun _ => mt fun hx i => aeSeq_eq_fun_of_mem_aeSeqSet hf hx i measure_mono_null h_ss (measure_compl_aeSeqSet_eq_zero hf hp) theorem aeSeq_n_eq_fun_n_ae [Countable ι] (hf : ∀ i, AEMeasurable (f i) μ)
(hp : ∀ᵐ x ∂μ, p x fun n => f n x) (n : ι) : aeSeq hf p n =ᵐ[μ] f n := ae_all_iff.mp (aeSeq_eq_fun_ae hf hp) n theorem iSup [SupSet β] [Countable ι] (hf : ∀ i, AEMeasurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x fun n => f n x) : ⨆ n, aeSeq hf p n =ᵐ[μ] ⨆ n, f n := by
Mathlib/MeasureTheory/Function/AEMeasurableSequence.lean
108
112
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.Algebra.BigOperators.Ring.Finset import Mathlib.Algebra.Module.BigOperators import Mathlib.NumberTheory.Divisors import Mathlib.Data.Nat.Squarefree import Mathlib.Data.Nat.GCD.BigOperators import Mathlib.Data.Nat.Factorization.Induction import Mathlib.Tactic.ArithMult /-! # Arithmetic Functions and Dirichlet Convolution This file defines arithmetic functions, which are functions from `ℕ` to a specified type that map 0 to 0. In the literature, they are often instead defined as functions from `ℕ+`. These arithmetic functions are endowed with a multiplication, given by Dirichlet convolution, and pointwise addition, to form the Dirichlet ring. ## Main Definitions * `ArithmeticFunction R` consists of functions `f : ℕ → R` such that `f 0 = 0`. * An arithmetic function `f` `IsMultiplicative` when `x.Coprime y → f (x * y) = f x * f y`. * The pointwise operations `pmul` and `ppow` differ from the multiplication and power instances on `ArithmeticFunction R`, which use Dirichlet multiplication. * `ζ` is the arithmetic function such that `ζ x = 1` for `0 < x`. * `σ k` is the arithmetic function such that `σ k x = ∑ y ∈ divisors x, y ^ k` for `0 < x`. * `pow k` is the arithmetic function such that `pow k x = x ^ k` for `0 < x`. * `id` is the identity arithmetic function on `ℕ`. * `ω n` is the number of distinct prime factors of `n`. * `Ω n` is the number of prime factors of `n` counted with multiplicity. * `μ` is the Möbius function (spelled `moebius` in code). ## Main Results * Several forms of Möbius inversion: * `sum_eq_iff_sum_mul_moebius_eq` for functions to a `CommRing` * `sum_eq_iff_sum_smul_moebius_eq` for functions to an `AddCommGroup` * `prod_eq_iff_prod_pow_moebius_eq` for functions to a `CommGroup` * `prod_eq_iff_prod_pow_moebius_eq_of_nonzero` for functions to a `CommGroupWithZero` * And variants that apply when the equalities only hold on a set `S : Set ℕ` such that `m ∣ n → n ∈ S → m ∈ S`: * `sum_eq_iff_sum_mul_moebius_eq_on` for functions to a `CommRing` * `sum_eq_iff_sum_smul_moebius_eq_on` for functions to an `AddCommGroup` * `prod_eq_iff_prod_pow_moebius_eq_on` for functions to a `CommGroup` * `prod_eq_iff_prod_pow_moebius_eq_on_of_nonzero` for functions to a `CommGroupWithZero` ## Notation All notation is localized in the namespace `ArithmeticFunction`. The arithmetic functions `ζ`, `σ`, `ω`, `Ω` and `μ` have Greek letter names. In addition, there are separate locales `ArithmeticFunction.zeta` for `ζ`, `ArithmeticFunction.sigma` for `σ`, `ArithmeticFunction.omega` for `ω`, `ArithmeticFunction.Omega` for `Ω`, and `ArithmeticFunction.Moebius` for `μ`, to allow for selective access to these notations. The arithmetic function $$n \mapsto \prod_{p \mid n} f(p)$$ is given custom notation `∏ᵖ p ∣ n, f p` when applied to `n`. ## Tags arithmetic functions, dirichlet convolution, divisors -/ open Finset open Nat variable (R : Type*) /-- An arithmetic function is a function from `ℕ` that maps 0 to 0. In the literature, they are often instead defined as functions from `ℕ+`. Multiplication on `ArithmeticFunctions` is by Dirichlet convolution. -/ def ArithmeticFunction [Zero R] := ZeroHom ℕ R instance ArithmeticFunction.zero [Zero R] : Zero (ArithmeticFunction R) := inferInstanceAs (Zero (ZeroHom ℕ R)) instance [Zero R] : Inhabited (ArithmeticFunction R) := inferInstanceAs (Inhabited (ZeroHom ℕ R)) variable {R} namespace ArithmeticFunction section Zero variable [Zero R] instance : FunLike (ArithmeticFunction R) ℕ R := inferInstanceAs (FunLike (ZeroHom ℕ R) ℕ R) @[simp] theorem toFun_eq (f : ArithmeticFunction R) : f.toFun = f := rfl @[simp] theorem coe_mk (f : ℕ → R) (hf) : @DFunLike.coe (ArithmeticFunction R) _ _ _ (ZeroHom.mk f hf) = f := rfl @[simp] theorem map_zero {f : ArithmeticFunction R} : f 0 = 0 := ZeroHom.map_zero' f theorem coe_inj {f g : ArithmeticFunction R} : (f : ℕ → R) = g ↔ f = g := DFunLike.coe_fn_eq @[simp] theorem zero_apply {x : ℕ} : (0 : ArithmeticFunction R) x = 0 := ZeroHom.zero_apply x @[ext] theorem ext ⦃f g : ArithmeticFunction R⦄ (h : ∀ x, f x = g x) : f = g := ZeroHom.ext h section One variable [One R] instance one : One (ArithmeticFunction R) := ⟨⟨fun x => ite (x = 1) 1 0, rfl⟩⟩ theorem one_apply {x : ℕ} : (1 : ArithmeticFunction R) x = ite (x = 1) 1 0 := rfl @[simp] theorem one_one : (1 : ArithmeticFunction R) 1 = 1 := rfl @[simp] theorem one_apply_ne {x : ℕ} (h : x ≠ 1) : (1 : ArithmeticFunction R) x = 0 := if_neg h end One end Zero /-- Coerce an arithmetic function with values in `ℕ` to one with values in `R`. We cannot inline this in `natCoe` because it gets unfolded too much. -/ @[coe] def natToArithmeticFunction [AddMonoidWithOne R] : (ArithmeticFunction ℕ) → (ArithmeticFunction R) := fun f => ⟨fun n => ↑(f n), by simp⟩ instance natCoe [AddMonoidWithOne R] : Coe (ArithmeticFunction ℕ) (ArithmeticFunction R) := ⟨natToArithmeticFunction⟩ @[simp] theorem natCoe_nat (f : ArithmeticFunction ℕ) : natToArithmeticFunction f = f := ext fun _ => cast_id _ @[simp] theorem natCoe_apply [AddMonoidWithOne R] {f : ArithmeticFunction ℕ} {x : ℕ} : (f : ArithmeticFunction R) x = f x := rfl /-- Coerce an arithmetic function with values in `ℤ` to one with values in `R`. We cannot inline this in `intCoe` because it gets unfolded too much. -/ @[coe] def ofInt [AddGroupWithOne R] : (ArithmeticFunction ℤ) → (ArithmeticFunction R) := fun f => ⟨fun n => ↑(f n), by simp⟩ instance intCoe [AddGroupWithOne R] : Coe (ArithmeticFunction ℤ) (ArithmeticFunction R) := ⟨ofInt⟩ @[simp] theorem intCoe_int (f : ArithmeticFunction ℤ) : ofInt f = f := ext fun _ => Int.cast_id @[simp] theorem intCoe_apply [AddGroupWithOne R] {f : ArithmeticFunction ℤ} {x : ℕ} : (f : ArithmeticFunction R) x = f x := rfl @[simp] theorem coe_coe [AddGroupWithOne R] {f : ArithmeticFunction ℕ} : ((f : ArithmeticFunction ℤ) : ArithmeticFunction R) = (f : ArithmeticFunction R) := by ext simp @[simp] theorem natCoe_one [AddMonoidWithOne R] : ((1 : ArithmeticFunction ℕ) : ArithmeticFunction R) = 1 := by ext n simp [one_apply] @[simp] theorem intCoe_one [AddGroupWithOne R] : ((1 : ArithmeticFunction ℤ) : ArithmeticFunction R) = 1 := by ext n simp [one_apply] section AddMonoid variable [AddMonoid R] instance add : Add (ArithmeticFunction R) := ⟨fun f g => ⟨fun n => f n + g n, by simp⟩⟩ @[simp] theorem add_apply {f g : ArithmeticFunction R} {n : ℕ} : (f + g) n = f n + g n := rfl instance instAddMonoid : AddMonoid (ArithmeticFunction R) := { ArithmeticFunction.zero R, ArithmeticFunction.add with add_assoc := fun _ _ _ => ext fun _ => add_assoc _ _ _ zero_add := fun _ => ext fun _ => zero_add _ add_zero := fun _ => ext fun _ => add_zero _ nsmul := nsmulRec } end AddMonoid instance instAddMonoidWithOne [AddMonoidWithOne R] : AddMonoidWithOne (ArithmeticFunction R) := { ArithmeticFunction.instAddMonoid, ArithmeticFunction.one with natCast := fun n => ⟨fun x => if x = 1 then (n : R) else 0, by simp⟩ natCast_zero := by ext; simp natCast_succ := fun n => by ext x; by_cases h : x = 1 <;> simp [h] } instance instAddCommMonoid [AddCommMonoid R] : AddCommMonoid (ArithmeticFunction R) := { ArithmeticFunction.instAddMonoid with add_comm := fun _ _ => ext fun _ => add_comm _ _ } instance [NegZeroClass R] : Neg (ArithmeticFunction R) where neg f := ⟨fun n => -f n, by simp⟩ instance [AddGroup R] : AddGroup (ArithmeticFunction R) := { ArithmeticFunction.instAddMonoid with neg_add_cancel := fun _ => ext fun _ => neg_add_cancel _ zsmul := zsmulRec } instance [AddCommGroup R] : AddCommGroup (ArithmeticFunction R) := { show AddGroup (ArithmeticFunction R) by infer_instance with add_comm := fun _ _ ↦ add_comm _ _ } section SMul variable {M : Type*} [Zero R] [AddCommMonoid M] [SMul R M] /-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/ instance : SMul (ArithmeticFunction R) (ArithmeticFunction M) := ⟨fun f g => ⟨fun n => ∑ x ∈ divisorsAntidiagonal n, f x.fst • g x.snd, by simp⟩⟩ @[simp] theorem smul_apply {f : ArithmeticFunction R} {g : ArithmeticFunction M} {n : ℕ} : (f • g) n = ∑ x ∈ divisorsAntidiagonal n, f x.fst • g x.snd := rfl end SMul /-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/ instance [Semiring R] : Mul (ArithmeticFunction R) := ⟨(· • ·)⟩ @[simp] theorem mul_apply [Semiring R] {f g : ArithmeticFunction R} {n : ℕ} : (f * g) n = ∑ x ∈ divisorsAntidiagonal n, f x.fst * g x.snd := rfl theorem mul_apply_one [Semiring R] {f g : ArithmeticFunction R} : (f * g) 1 = f 1 * g 1 := by simp @[simp, norm_cast] theorem natCoe_mul [Semiring R] {f g : ArithmeticFunction ℕ} : (↑(f * g) : ArithmeticFunction R) = f * g := by ext n simp @[simp, norm_cast] theorem intCoe_mul [Ring R] {f g : ArithmeticFunction ℤ} : (↑(f * g) : ArithmeticFunction R) = ↑f * g := by ext n simp section Module variable {M : Type*} [Semiring R] [AddCommMonoid M] [Module R M] theorem mul_smul' (f g : ArithmeticFunction R) (h : ArithmeticFunction M) : (f * g) • h = f • g • h := by ext n simp only [mul_apply, smul_apply, sum_smul, mul_smul, smul_sum, Finset.sum_sigma'] apply Finset.sum_nbij' (fun ⟨⟨_i, j⟩, ⟨k, l⟩⟩ ↦ ⟨(k, l * j), (l, j)⟩) (fun ⟨⟨i, _j⟩, ⟨k, l⟩⟩ ↦ ⟨(i * k, l), (i, k)⟩) <;> aesop (add simp mul_assoc) theorem one_smul' (b : ArithmeticFunction M) : (1 : ArithmeticFunction R) • b = b := by ext x rw [smul_apply] by_cases x0 : x = 0 · simp [x0] have h : {(1, x)} ⊆ divisorsAntidiagonal x := by simp [x0] rw [← sum_subset h] · simp intro y ymem ynmem have y1ne : y.fst ≠ 1 := fun con => by simp_all [Prod.ext_iff] simp [y1ne] end Module section Semiring variable [Semiring R] instance instMonoid : Monoid (ArithmeticFunction R) := { one := One.one mul := Mul.mul one_mul := one_smul' mul_one := fun f => by ext x rw [mul_apply] by_cases x0 : x = 0 · simp [x0] have h : {(x, 1)} ⊆ divisorsAntidiagonal x := by simp [x0] rw [← sum_subset h] · simp intro ⟨y₁, y₂⟩ ymem ynmem have y2ne : y₂ ≠ 1 := by intro con simp_all simp [y2ne] mul_assoc := mul_smul' } instance instSemiring : Semiring (ArithmeticFunction R) := { ArithmeticFunction.instAddMonoidWithOne, ArithmeticFunction.instMonoid, ArithmeticFunction.instAddCommMonoid with zero_mul := fun f => by ext simp mul_zero := fun f => by ext simp left_distrib := fun a b c => by ext simp [← sum_add_distrib, mul_add] right_distrib := fun a b c => by ext simp [← sum_add_distrib, add_mul] } end Semiring instance [CommSemiring R] : CommSemiring (ArithmeticFunction R) := { ArithmeticFunction.instSemiring with mul_comm := fun f g => by ext rw [mul_apply, ← map_swap_divisorsAntidiagonal, sum_map] simp [mul_comm] } instance [CommRing R] : CommRing (ArithmeticFunction R) := { ArithmeticFunction.instSemiring with neg_add_cancel := neg_add_cancel mul_comm := mul_comm zsmul := (· • ·) } instance {M : Type*} [Semiring R] [AddCommMonoid M] [Module R M] : Module (ArithmeticFunction R) (ArithmeticFunction M) where one_smul := one_smul' mul_smul := mul_smul' smul_add r x y := by ext simp only [sum_add_distrib, smul_add, smul_apply, add_apply] smul_zero r := by ext simp only [smul_apply, sum_const_zero, smul_zero, zero_apply] add_smul r s x := by ext simp only [add_smul, sum_add_distrib, smul_apply, add_apply] zero_smul r := by ext simp only [smul_apply, sum_const_zero, zero_smul, zero_apply] section Zeta /-- `ζ 0 = 0`, otherwise `ζ x = 1`. The Dirichlet Series is the Riemann `ζ`. -/ def zeta : ArithmeticFunction ℕ := ⟨fun x => ite (x = 0) 0 1, rfl⟩ @[inherit_doc] scoped[ArithmeticFunction] notation "ζ" => ArithmeticFunction.zeta @[inherit_doc] scoped[ArithmeticFunction.zeta] notation "ζ" => ArithmeticFunction.zeta @[simp] theorem zeta_apply {x : ℕ} : ζ x = if x = 0 then 0 else 1 := rfl theorem zeta_apply_ne {x : ℕ} (h : x ≠ 0) : ζ x = 1 := if_neg h -- Porting note: removed `@[simp]`, LHS not in normal form theorem coe_zeta_smul_apply {M} [Semiring R] [AddCommMonoid M] [MulAction R M] {f : ArithmeticFunction M} {x : ℕ} : ((↑ζ : ArithmeticFunction R) • f) x = ∑ i ∈ divisors x, f i := by rw [smul_apply] trans ∑ i ∈ divisorsAntidiagonal x, f i.snd · refine sum_congr rfl fun i hi => ?_ rcases mem_divisorsAntidiagonal.1 hi with ⟨rfl, h⟩ rw [natCoe_apply, zeta_apply_ne (left_ne_zero_of_mul h), cast_one, one_smul] · rw [← map_div_left_divisors, sum_map, Function.Embedding.coeFn_mk] theorem coe_zeta_mul_apply [Semiring R] {f : ArithmeticFunction R} {x : ℕ} : (↑ζ * f) x = ∑ i ∈ divisors x, f i := coe_zeta_smul_apply theorem coe_mul_zeta_apply [Semiring R] {f : ArithmeticFunction R} {x : ℕ} : (f * ζ) x = ∑ i ∈ divisors x, f i := by rw [mul_apply] trans ∑ i ∈ divisorsAntidiagonal x, f i.1 · refine sum_congr rfl fun i hi => ?_ rcases mem_divisorsAntidiagonal.1 hi with ⟨rfl, h⟩ rw [natCoe_apply, zeta_apply_ne (right_ne_zero_of_mul h), cast_one, mul_one] · rw [← map_div_right_divisors, sum_map, Function.Embedding.coeFn_mk] theorem zeta_mul_apply {f : ArithmeticFunction ℕ} {x : ℕ} : (ζ * f) x = ∑ i ∈ divisors x, f i := by rw [← natCoe_nat ζ, coe_zeta_mul_apply] theorem mul_zeta_apply {f : ArithmeticFunction ℕ} {x : ℕ} : (f * ζ) x = ∑ i ∈ divisors x, f i := by rw [← natCoe_nat ζ, coe_mul_zeta_apply] end Zeta open ArithmeticFunction section Pmul /-- This is the pointwise product of `ArithmeticFunction`s. -/ def pmul [MulZeroClass R] (f g : ArithmeticFunction R) : ArithmeticFunction R := ⟨fun x => f x * g x, by simp⟩ @[simp] theorem pmul_apply [MulZeroClass R] {f g : ArithmeticFunction R} {x : ℕ} : f.pmul g x = f x * g x := rfl theorem pmul_comm [CommMonoidWithZero R] (f g : ArithmeticFunction R) : f.pmul g = g.pmul f := by ext simp [mul_comm] lemma pmul_assoc [SemigroupWithZero R] (f₁ f₂ f₃ : ArithmeticFunction R) : pmul (pmul f₁ f₂) f₃ = pmul f₁ (pmul f₂ f₃) := by ext simp only [pmul_apply, mul_assoc] section NonAssocSemiring variable [NonAssocSemiring R] @[simp] theorem pmul_zeta (f : ArithmeticFunction R) : f.pmul ↑ζ = f := by ext x cases x <;> simp [Nat.succ_ne_zero] @[simp] theorem zeta_pmul (f : ArithmeticFunction R) : (ζ : ArithmeticFunction R).pmul f = f := by ext x cases x <;> simp [Nat.succ_ne_zero] end NonAssocSemiring variable [Semiring R] /-- This is the pointwise power of `ArithmeticFunction`s. -/ def ppow (f : ArithmeticFunction R) (k : ℕ) : ArithmeticFunction R := if h0 : k = 0 then ζ else ⟨fun x ↦ f x ^ k, by simp_rw [map_zero, zero_pow h0]⟩ @[simp] theorem ppow_zero {f : ArithmeticFunction R} : f.ppow 0 = ζ := by rw [ppow, dif_pos rfl] @[simp] theorem ppow_apply {f : ArithmeticFunction R} {k x : ℕ} (kpos : 0 < k) : f.ppow k x = f x ^ k := by rw [ppow, dif_neg (Nat.ne_of_gt kpos), coe_mk] theorem ppow_succ' {f : ArithmeticFunction R} {k : ℕ} : f.ppow (k + 1) = f.pmul (f.ppow k) := by ext x rw [ppow_apply (Nat.succ_pos k), _root_.pow_succ'] induction k <;> simp theorem ppow_succ {f : ArithmeticFunction R} {k : ℕ} {kpos : 0 < k} : f.ppow (k + 1) = (f.ppow k).pmul f := by ext x rw [ppow_apply (Nat.succ_pos k), _root_.pow_succ] induction k <;> simp end Pmul section Pdiv /-- This is the pointwise division of `ArithmeticFunction`s. -/ def pdiv [GroupWithZero R] (f g : ArithmeticFunction R) : ArithmeticFunction R := ⟨fun n => f n / g n, by simp only [map_zero, ne_eq, not_true, div_zero]⟩ @[simp] theorem pdiv_apply [GroupWithZero R] (f g : ArithmeticFunction R) (n : ℕ) : pdiv f g n = f n / g n := rfl /-- This result only holds for `DivisionSemiring`s instead of `GroupWithZero`s because zeta takes values in ℕ, and hence the coercion requires an `AddMonoidWithOne`. TODO: Generalise zeta -/ @[simp] theorem pdiv_zeta [DivisionSemiring R] (f : ArithmeticFunction R) : pdiv f zeta = f := by ext n cases n <;> simp [succ_ne_zero] end Pdiv section ProdPrimeFactors /-- The map $n \mapsto \prod_{p \mid n} f(p)$ as an arithmetic function -/ def prodPrimeFactors [CommMonoidWithZero R] (f : ℕ → R) : ArithmeticFunction R where toFun d := if d = 0 then 0 else ∏ p ∈ d.primeFactors, f p map_zero' := if_pos rfl open Batteries.ExtendedBinder /-- `∏ᵖ p ∣ n, f p` is custom notation for `prodPrimeFactors f n` -/ scoped syntax (name := bigproddvd) "∏ᵖ " extBinder " ∣ " term ", " term:67 : term scoped macro_rules (kind := bigproddvd) | `(∏ᵖ $x:ident ∣ $n, $r) => `(prodPrimeFactors (fun $x ↦ $r) $n) @[simp] theorem prodPrimeFactors_apply [CommMonoidWithZero R] {f : ℕ → R} {n : ℕ} (hn : n ≠ 0) : ∏ᵖ p ∣ n, f p = ∏ p ∈ n.primeFactors, f p := if_neg hn end ProdPrimeFactors /-- Multiplicative functions -/ def IsMultiplicative [MonoidWithZero R] (f : ArithmeticFunction R) : Prop := f 1 = 1 ∧ ∀ {m n : ℕ}, m.Coprime n → f (m * n) = f m * f n namespace IsMultiplicative section MonoidWithZero variable [MonoidWithZero R] @[simp, arith_mult] theorem map_one {f : ArithmeticFunction R} (h : f.IsMultiplicative) : f 1 = 1 := h.1 @[simp] theorem map_mul_of_coprime {f : ArithmeticFunction R} (hf : f.IsMultiplicative) {m n : ℕ} (h : m.Coprime n) : f (m * n) = f m * f n := hf.2 h end MonoidWithZero open scoped Function in -- required for scoped `on` notation theorem map_prod {ι : Type*} [CommMonoidWithZero R] (g : ι → ℕ) {f : ArithmeticFunction R} (hf : f.IsMultiplicative) (s : Finset ι) (hs : (s : Set ι).Pairwise (Coprime on g)) : f (∏ i ∈ s, g i) = ∏ i ∈ s, f (g i) := by classical induction s using Finset.induction_on with | empty => simp [hf] | insert _ _ has ih => rw [coe_insert, Set.pairwise_insert_of_symmetric (Coprime.symmetric.comap g)] at hs rw [prod_insert has, prod_insert has, hf.map_mul_of_coprime, ih hs.1] exact .prod_right fun i hi => hs.2 _ hi (hi.ne_of_not_mem has).symm theorem map_prod_of_prime [CommMonoidWithZero R] {f : ArithmeticFunction R} (h_mult : ArithmeticFunction.IsMultiplicative f) (t : Finset ℕ) (ht : ∀ p ∈ t, p.Prime) : f (∏ a ∈ t, a) = ∏ a ∈ t, f a := map_prod _ h_mult t fun x hx y hy hxy => (coprime_primes (ht x hx) (ht y hy)).mpr hxy theorem map_prod_of_subset_primeFactors [CommMonoidWithZero R] {f : ArithmeticFunction R} (h_mult : ArithmeticFunction.IsMultiplicative f) (l : ℕ) (t : Finset ℕ) (ht : t ⊆ l.primeFactors) : f (∏ a ∈ t, a) = ∏ a ∈ t, f a := map_prod_of_prime h_mult t fun _ a => prime_of_mem_primeFactors (ht a) theorem map_div_of_coprime [GroupWithZero R] {f : ArithmeticFunction R} (hf : IsMultiplicative f) {l d : ℕ} (hdl : d ∣ l) (hl : (l / d).Coprime d) (hd : f d ≠ 0) : f (l / d) = f l / f d := by apply (div_eq_of_eq_mul hd ..).symm rw [← hf.right hl, Nat.div_mul_cancel hdl] @[arith_mult] theorem natCast {f : ArithmeticFunction ℕ} [Semiring R] (h : f.IsMultiplicative) : IsMultiplicative (f : ArithmeticFunction R) := ⟨by simp [h], fun {m n} cop => by simp [h.2 cop]⟩ @[arith_mult] theorem intCast {f : ArithmeticFunction ℤ} [Ring R] (h : f.IsMultiplicative) : IsMultiplicative (f : ArithmeticFunction R) := ⟨by simp [h], fun {m n} cop => by simp [h.2 cop]⟩ @[arith_mult] theorem mul [CommSemiring R] {f g : ArithmeticFunction R} (hf : f.IsMultiplicative) (hg : g.IsMultiplicative) : IsMultiplicative (f * g) := by refine ⟨by simp [hf.1, hg.1], ?_⟩ simp only [mul_apply] intro m n cop rw [sum_mul_sum, ← sum_product'] symm apply sum_nbij fun ((i, j), k, l) ↦ (i * k, j * l) · rintro ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ h simp only [mem_divisorsAntidiagonal, Ne, mem_product] at h rcases h with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩ simp only [mem_divisorsAntidiagonal, Nat.mul_eq_zero, Ne] constructor · ring rw [Nat.mul_eq_zero] at * apply not_or_intro ha hb · simp only [Set.InjOn, mem_coe, mem_divisorsAntidiagonal, Ne, mem_product, Prod.mk_inj] rintro ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩ ⟨⟨c1, c2⟩, ⟨d1, d2⟩⟩ hcd h simp only [Prod.mk_inj] at h ext <;> dsimp only · trans Nat.gcd (a1 * a2) (a1 * b1) · rw [Nat.gcd_mul_left, cop.coprime_mul_left.coprime_mul_right_right.gcd_eq_one, mul_one] · rw [← hcd.1.1, ← hcd.2.1] at cop rw [← hcd.1.1, h.1, Nat.gcd_mul_left, cop.coprime_mul_left.coprime_mul_right_right.gcd_eq_one, mul_one] · trans Nat.gcd (a1 * a2) (a2 * b2) · rw [mul_comm, Nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.gcd_eq_one, mul_one] · rw [← hcd.1.1, ← hcd.2.1] at cop rw [← hcd.1.1, h.2, mul_comm, Nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.gcd_eq_one, mul_one] · trans Nat.gcd (b1 * b2) (a1 * b1) · rw [mul_comm, Nat.gcd_mul_right, cop.coprime_mul_right.coprime_mul_left_right.symm.gcd_eq_one, one_mul] · rw [← hcd.1.1, ← hcd.2.1] at cop rw [← hcd.2.1, h.1, mul_comm c1 d1, Nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.symm.gcd_eq_one, mul_one] · trans Nat.gcd (b1 * b2) (a2 * b2) · rw [Nat.gcd_mul_right, cop.coprime_mul_left.coprime_mul_right_right.symm.gcd_eq_one, one_mul] · rw [← hcd.1.1, ← hcd.2.1] at cop rw [← hcd.2.1, h.2, Nat.gcd_mul_right, cop.coprime_mul_left.coprime_mul_right_right.symm.gcd_eq_one, one_mul] · simp only [Set.SurjOn, Set.subset_def, mem_coe, mem_divisorsAntidiagonal, Ne, mem_product, Set.mem_image, exists_prop, Prod.mk_inj] rintro ⟨b1, b2⟩ h dsimp at h use ((b1.gcd m, b2.gcd m), (b1.gcd n, b2.gcd n)) rw [← cop.gcd_mul _, ← cop.gcd_mul _, ← h.1, Nat.gcd_mul_gcd_of_coprime_of_mul_eq_mul cop h.1, Nat.gcd_mul_gcd_of_coprime_of_mul_eq_mul cop.symm _] · rw [Nat.mul_eq_zero, not_or] at h simp [h.2.1, h.2.2] rw [mul_comm n m, h.1] · simp only [mem_divisorsAntidiagonal, Ne, mem_product] rintro ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩ dsimp only rw [hf.map_mul_of_coprime cop.coprime_mul_right.coprime_mul_right_right, hg.map_mul_of_coprime cop.coprime_mul_left.coprime_mul_left_right] ring @[arith_mult] theorem pmul [CommSemiring R] {f g : ArithmeticFunction R} (hf : f.IsMultiplicative) (hg : g.IsMultiplicative) : IsMultiplicative (f.pmul g) := ⟨by simp [hf, hg], fun {m n} cop => by simp only [pmul_apply, hf.map_mul_of_coprime cop, hg.map_mul_of_coprime cop] ring⟩ @[arith_mult] theorem pdiv [CommGroupWithZero R] {f g : ArithmeticFunction R} (hf : IsMultiplicative f)
(hg : IsMultiplicative g) : IsMultiplicative (pdiv f g) := ⟨by simp [hf, hg], fun {m n} cop => by simp only [pdiv_apply, map_mul_of_coprime hf cop, map_mul_of_coprime hg cop, div_eq_mul_inv, mul_inv]
Mathlib/NumberTheory/ArithmeticFunction.lean
663
666
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import Mathlib.GroupTheory.OrderOfElement import Mathlib.Algebra.GCDMonoid.Finset import Mathlib.Algebra.GCDMonoid.Nat import Mathlib.Data.Nat.Factorization.Basic import Mathlib.Tactic.Peel import Mathlib.Algebra.Order.BigOperators.Ring.Finset /-! # Exponent of a group This file defines the exponent of a group, or more generally a monoid. For a group `G` it is defined to be the minimal `n≥1` such that `g ^ n = 1` for all `g ∈ G`. For a finite group `G`, it is equal to the lowest common multiple of the order of all elements of the group `G`. ## Main definitions * `Monoid.ExponentExists` is a predicate on a monoid `G` saying that there is some positive `n` such that `g ^ n = 1` for all `g ∈ G`. * `Monoid.exponent` defines the exponent of a monoid `G` as the minimal positive `n` such that `g ^ n = 1` for all `g ∈ G`, by convention it is `0` if no such `n` exists. * `AddMonoid.ExponentExists` the additive version of `Monoid.ExponentExists`. * `AddMonoid.exponent` the additive version of `Monoid.exponent`. ## Main results * `Monoid.lcm_order_eq_exponent`: For a finite left cancel monoid `G`, the exponent is equal to the `Finset.lcm` of the order of its elements. * `Monoid.exponent_eq_iSup_orderOf(')`: For a commutative cancel monoid, the exponent is equal to `⨆ g : G, orderOf g` (or zero if it has any order-zero elements). * `Monoid.exponent_pi` and `Monoid.exponent_prod`: The exponent of a finite product of monoids is the least common multiple (`Finset.lcm` and `lcm`, respectively) of the exponents of the constituent monoids. * `MonoidHom.exponent_dvd`: If `f : M₁ →⋆ M₂` is surjective, then the exponent of `M₂` divides the exponent of `M₁`. ## TODO * Refactor the characteristic of a ring to be the exponent of its underlying additive group. -/ universe u variable {G : Type u} namespace Monoid section Monoid variable (G) [Monoid G] /-- A predicate on a monoid saying that there is a positive integer `n` such that `g ^ n = 1` for all `g`. -/ @[to_additive "A predicate on an additive monoid saying that there is a positive integer `n` such\n that `n • g = 0` for all `g`."] def ExponentExists := ∃ n, 0 < n ∧ ∀ g : G, g ^ n = 1 open scoped Classical in /-- The exponent of a group is the smallest positive integer `n` such that `g ^ n = 1` for all `g ∈ G` if it exists, otherwise it is zero by convention. -/ @[to_additive "The exponent of an additive group is the smallest positive integer `n` such that\n `n • g = 0` for all `g ∈ G` if it exists, otherwise it is zero by convention."] noncomputable def exponent := if h : ExponentExists G then Nat.find h else 0 variable {G} @[simp] theorem _root_.AddMonoid.exponent_additive : AddMonoid.exponent (Additive G) = exponent G := rfl @[simp] theorem exponent_multiplicative {G : Type*} [AddMonoid G] : exponent (Multiplicative G) = AddMonoid.exponent G := rfl open MulOpposite in @[to_additive (attr := simp)] theorem _root_.MulOpposite.exponent : exponent (MulOpposite G) = exponent G := by simp only [Monoid.exponent, ExponentExists] congr! all_goals exact ⟨(op_injective <| · <| op ·), (unop_injective <| · <| unop ·)⟩ @[to_additive] theorem ExponentExists.isOfFinOrder (h : ExponentExists G) {g : G} : IsOfFinOrder g := isOfFinOrder_iff_pow_eq_one.mpr <| by peel 2 h; exact this g @[to_additive] theorem ExponentExists.orderOf_pos (h : ExponentExists G) (g : G) : 0 < orderOf g := h.isOfFinOrder.orderOf_pos @[to_additive] theorem exponent_ne_zero : exponent G ≠ 0 ↔ ExponentExists G := by rw [exponent] split_ifs with h · simp [h, @not_lt_zero' ℕ] --if this isn't done this way, `to_additive` freaks · tauto @[to_additive] protected alias ⟨_, ExponentExists.exponent_ne_zero⟩ := exponent_ne_zero @[to_additive] theorem exponent_pos : 0 < exponent G ↔ ExponentExists G := pos_iff_ne_zero.trans exponent_ne_zero @[to_additive] protected alias ⟨_, ExponentExists.exponent_pos⟩ := exponent_pos @[to_additive] theorem exponent_eq_zero_iff : exponent G = 0 ↔ ¬ExponentExists G := exponent_ne_zero.not_right @[to_additive exponent_eq_zero_addOrder_zero] theorem exponent_eq_zero_of_order_zero {g : G} (hg : orderOf g = 0) : exponent G = 0 := exponent_eq_zero_iff.mpr fun h ↦ h.orderOf_pos g |>.ne' hg /-- The exponent is zero iff for all nonzero `n`, one can find a `g` such that `g ^ n ≠ 1`. -/ @[to_additive "The exponent is zero iff for all nonzero `n`, one can find a `g` such that `n • g ≠ 0`."] theorem exponent_eq_zero_iff_forall : exponent G = 0 ↔ ∀ n > 0, ∃ g : G, g ^ n ≠ 1 := by rw [exponent_eq_zero_iff, ExponentExists] push_neg rfl @[to_additive exponent_nsmul_eq_zero] theorem pow_exponent_eq_one (g : G) : g ^ exponent G = 1 := by classical by_cases h : ExponentExists G · simp_rw [exponent, dif_pos h] exact (Nat.find_spec h).2 g · simp_rw [exponent, dif_neg h, pow_zero] @[to_additive] theorem pow_eq_mod_exponent {n : ℕ} (g : G) : g ^ n = g ^ (n % exponent G) := calc g ^ n = g ^ (n % exponent G + exponent G * (n / exponent G)) := by rw [Nat.mod_add_div] _ = g ^ (n % exponent G) := by simp [pow_add, pow_mul, pow_exponent_eq_one] @[to_additive] theorem exponent_pos_of_exists (n : ℕ) (hpos : 0 < n) (hG : ∀ g : G, g ^ n = 1) : 0 < exponent G := ExponentExists.exponent_pos ⟨n, hpos, hG⟩ @[to_additive] theorem exponent_min' (n : ℕ) (hpos : 0 < n) (hG : ∀ g : G, g ^ n = 1) : exponent G ≤ n := by classical rw [exponent, dif_pos] · apply Nat.find_min' exact ⟨hpos, hG⟩ · exact ⟨n, hpos, hG⟩ @[to_additive] theorem exponent_min (m : ℕ) (hpos : 0 < m) (hm : m < exponent G) : ∃ g : G, g ^ m ≠ 1 := by by_contra! h have hcon : exponent G ≤ m := exponent_min' m hpos h omega @[to_additive AddMonoid.exp_eq_one_iff] theorem exp_eq_one_iff : exponent G = 1 ↔ Subsingleton G := by refine ⟨fun eq_one => ⟨fun a b => ?a_eq_b⟩, fun h => le_antisymm ?le ?ge⟩ · rw [← pow_one a, ← pow_one b, ← eq_one, Monoid.pow_exponent_eq_one, Monoid.pow_exponent_eq_one] · apply exponent_min' _ Nat.one_pos simp [eq_iff_true_of_subsingleton] · apply Nat.succ_le_of_lt apply exponent_pos_of_exists 1 Nat.one_pos simp [eq_iff_true_of_subsingleton] @[to_additive (attr := simp) AddMonoid.exp_eq_one_of_subsingleton] theorem exp_eq_one_of_subsingleton [hs : Subsingleton G] : exponent G = 1 := exp_eq_one_iff.mpr hs @[to_additive addOrder_dvd_exponent] theorem order_dvd_exponent (g : G) : orderOf g ∣ exponent G := orderOf_dvd_of_pow_eq_one <| pow_exponent_eq_one g @[to_additive] theorem orderOf_le_exponent (h : ExponentExists G) (g : G) : orderOf g ≤ exponent G := Nat.le_of_dvd h.exponent_pos (order_dvd_exponent g) @[to_additive] theorem exponent_dvd_iff_forall_pow_eq_one {n : ℕ} : exponent G ∣ n ↔ ∀ g : G, g ^ n = 1 := by rcases n.eq_zero_or_pos with (rfl | hpos) · simp constructor · intro h g rw [Nat.dvd_iff_mod_eq_zero] at h rw [pow_eq_mod_exponent, h, pow_zero] · intro hG by_contra h rw [Nat.dvd_iff_mod_eq_zero, ← Ne, ← pos_iff_ne_zero] at h have h₂ : n % exponent G < exponent G := Nat.mod_lt _ (exponent_pos_of_exists n hpos hG) have h₃ : exponent G ≤ n % exponent G := by apply exponent_min' _ h simp_rw [← pow_eq_mod_exponent] exact hG exact h₂.not_le h₃ @[to_additive] alias ⟨_, exponent_dvd_of_forall_pow_eq_one⟩ := exponent_dvd_iff_forall_pow_eq_one @[to_additive] theorem exponent_dvd {n : ℕ} : exponent G ∣ n ↔ ∀ g : G, orderOf g ∣ n := by simp_rw [exponent_dvd_iff_forall_pow_eq_one, orderOf_dvd_iff_pow_eq_one] variable (G) @[to_additive] theorem lcm_orderOf_dvd_exponent [Fintype G] : (Finset.univ : Finset G).lcm orderOf ∣ exponent G := by apply Finset.lcm_dvd intro g _ exact order_dvd_exponent g @[to_additive exists_addOrderOf_eq_pow_padic_val_nat_add_exponent] theorem _root_.Nat.Prime.exists_orderOf_eq_pow_factorization_exponent {p : ℕ} (hp : p.Prime) : ∃ g : G, orderOf g = p ^ (exponent G).factorization p := by haveI := Fact.mk hp rcases eq_or_ne ((exponent G).factorization p) 0 with (h | h) · refine ⟨1, by rw [h, pow_zero, orderOf_one]⟩ have he : 0 < exponent G := Ne.bot_lt fun ht => by rw [ht] at h apply h rw [bot_eq_zero, Nat.factorization_zero, Finsupp.zero_apply] rw [← Finsupp.mem_support_iff] at h obtain ⟨g, hg⟩ : ∃ g : G, g ^ (exponent G / p) ≠ 1 := by suffices key : ¬exponent G ∣ exponent G / p by rwa [exponent_dvd_iff_forall_pow_eq_one, not_forall] at key exact fun hd => hp.one_lt.not_le ((mul_le_iff_le_one_left he).mp <| Nat.le_of_dvd he <| Nat.mul_dvd_of_dvd_div (Nat.dvd_of_mem_primeFactors h) hd) obtain ⟨k, hk : exponent G = p ^ _ * k⟩ := Nat.ordProj_dvd _ _ obtain ⟨t, ht⟩ := Nat.exists_eq_succ_of_ne_zero (Finsupp.mem_support_iff.mp h) refine ⟨g ^ k, ?_⟩ rw [ht] apply orderOf_eq_prime_pow · rwa [hk, mul_comm, ht, pow_succ, ← mul_assoc, Nat.mul_div_cancel _ hp.pos, pow_mul] at hg · rw [← Nat.succ_eq_add_one, ← ht, ← pow_mul, mul_comm, ← hk] exact pow_exponent_eq_one g variable {G} in open Nat in /-- If two commuting elements `x` and `y` of a monoid have order `n` and `m`, there is an element of order `lcm n m`. The result actually gives an explicit (computable) element, written as the product of a power of `x` and a power of `y`. See also the result below if you don't need the explicit formula. -/ @[to_additive "If two commuting elements `x` and `y` of an additive monoid have order `n` and `m`, there is an element of order `lcm n m`. The result actually gives an explicit (computable) element, written as the sum of a multiple of `x` and a multiple of `y`. See also the result below if you don't need the explicit formula."] lemma _root_.Commute.orderOf_mul_pow_eq_lcm {x y : G} (h : Commute x y) (hx : orderOf x ≠ 0) (hy : orderOf y ≠ 0) : orderOf (x ^ (orderOf x / (factorizationLCMLeft (orderOf x) (orderOf y))) * y ^ (orderOf y / factorizationLCMRight (orderOf x) (orderOf y))) = Nat.lcm (orderOf x) (orderOf y) := by rw [(h.pow_pow _ _).orderOf_mul_eq_mul_orderOf_of_coprime] all_goals iterate 2 rw [orderOf_pow_orderOf_div]; try rw [Coprime] all_goals simp [factorizationLCMLeft_mul_factorizationLCMRight, factorizationLCMLeft_dvd_left, factorizationLCMRight_dvd_right, coprime_factorizationLCMLeft_factorizationLCMRight, hx, hy] open Submonoid in /-- If two commuting elements `x` and `y` of a monoid have order `n` and `m`, then there is an element of order `lcm n m` that lies in the subgroup generated by `x` and `y`. -/ @[to_additive "If two commuting elements `x` and `y` of an additive monoid have order `n` and `m`, then there is an element of order `lcm n m` that lies in the additive subgroup generated by `x` and `y`."] theorem _root_.Commute.exists_orderOf_eq_lcm {x y : G} (h : Commute x y) : ∃ z ∈ closure {x, y}, orderOf z = Nat.lcm (orderOf x) (orderOf y) := by by_cases hx : orderOf x = 0 <;> by_cases hy : orderOf y = 0 · exact ⟨x, subset_closure (by simp), by simp [hx]⟩ · exact ⟨x, subset_closure (by simp), by simp [hx]⟩ · exact ⟨y, subset_closure (by simp), by simp [hy]⟩ · exact ⟨_, mul_mem (pow_mem (subset_closure (by simp)) _) (pow_mem (subset_closure (by simp)) _), h.orderOf_mul_pow_eq_lcm hx hy⟩ /-- A nontrivial monoid has prime exponent `p` if and only if every non-identity element has order `p`. -/ @[to_additive] lemma exponent_eq_prime_iff {G : Type*} [Monoid G] [Nontrivial G] {p : ℕ} (hp : p.Prime) : Monoid.exponent G = p ↔ ∀ g : G, g ≠ 1 → orderOf g = p := by refine ⟨fun hG g hg ↦ ?_, fun h ↦ dvd_antisymm ?_ ?_⟩ · rw [Ne, ← orderOf_eq_one_iff] at hg exact Eq.symm <| (hp.dvd_iff_eq hg).mp <| hG ▸ Monoid.order_dvd_exponent g · rw [exponent_dvd] intro g by_cases hg : g = 1 · simp [hg] · rw [h g hg] · obtain ⟨g, hg⟩ := exists_ne (1 : G) simpa [h g hg] using Monoid.order_dvd_exponent g variable {G} @[to_additive] theorem exponent_ne_zero_iff_range_orderOf_finite (h : ∀ g : G, 0 < orderOf g) : exponent G ≠ 0 ↔ (Set.range (orderOf : G → ℕ)).Finite := by refine ⟨fun he => ?_, fun he => ?_⟩ · by_contra h obtain ⟨m, ⟨t, rfl⟩, het⟩ := Set.Infinite.exists_gt h (exponent G) exact pow_ne_one_of_lt_orderOf he het (pow_exponent_eq_one t) · lift Set.range (orderOf (G := G)) to Finset ℕ using he with t ht have htpos : 0 < t.prod id := by refine Finset.prod_pos fun a ha => ?_ rw [← Finset.mem_coe, ht] at ha obtain ⟨k, rfl⟩ := ha exact h k suffices exponent G ∣ t.prod id by intro h rw [h, zero_dvd_iff] at this exact htpos.ne' this rw [exponent_dvd] intro g apply Finset.dvd_prod_of_mem id (?_ : orderOf g ∈ _) rw [← Finset.mem_coe, ht] exact Set.mem_range_self g @[to_additive] theorem exponent_eq_zero_iff_range_orderOf_infinite (h : ∀ g : G, 0 < orderOf g) : exponent G = 0 ↔ (Set.range (orderOf : G → ℕ)).Infinite := by have := exponent_ne_zero_iff_range_orderOf_finite h rwa [Ne, not_iff_comm, Iff.comm] at this @[to_additive] theorem lcm_orderOf_eq_exponent [Fintype G] : (Finset.univ : Finset G).lcm orderOf = exponent G := Nat.dvd_antisymm (lcm_orderOf_dvd_exponent G) (exponent_dvd.mpr fun g => Finset.dvd_lcm (Finset.mem_univ g)) variable {H : Type*} [Monoid H] /-- If there exists an injective, multiplication-preserving map from `G` to `H`, then the exponent of `G` divides the exponent of `H`. -/ @[to_additive "If there exists an injective, addition-preserving map from `G` to `H`, then the exponent of `G` divides the exponent of `H`."] theorem exponent_dvd_of_monoidHom (e : G →* H) (e_inj : Function.Injective e) : Monoid.exponent G ∣ Monoid.exponent H := exponent_dvd_of_forall_pow_eq_one fun g => e_inj (by rw [map_pow, pow_exponent_eq_one, map_one]) /-- If there exists a multiplication-preserving equivalence between `G` and `H`, then the exponent of `G` is equal to the exponent of `H`. -/ @[to_additive "If there exists a addition-preserving equivalence between `G` and `H`, then the exponent of `G` is equal to the exponent of `H`."] theorem exponent_eq_of_mulEquiv (e : G ≃* H) : Monoid.exponent G = Monoid.exponent H := Nat.dvd_antisymm (exponent_dvd_of_monoidHom e e.injective) (exponent_dvd_of_monoidHom e.symm e.symm.injective) end Monoid section Submonoid variable [Monoid G] variable (G) in @[to_additive (attr := simp)] theorem _root_.Submonoid.exponent_top : Monoid.exponent (⊤ : Submonoid G) = Monoid.exponent G := exponent_eq_of_mulEquiv Submonoid.topEquiv @[to_additive] theorem _root_.Submonoid.pow_exponent_eq_one {S : Submonoid G} {g : G} (g_in_s : g ∈ S) : g ^ (Monoid.exponent S) = 1 := by have := Monoid.pow_exponent_eq_one (⟨g, g_in_s⟩ : S) rwa [SubmonoidClass.mk_pow, ← OneMemClass.coe_eq_one] at this end Submonoid section LeftCancelMonoid variable [LeftCancelMonoid G] [Finite G] @[to_additive] theorem ExponentExists.of_finite : ExponentExists G := by let _inst := Fintype.ofFinite G simp only [Monoid.ExponentExists] refine ⟨(Finset.univ : Finset G).lcm orderOf, ?_, fun g => ?_⟩ · simpa [pos_iff_ne_zero, Finset.lcm_eq_zero_iff] using fun x => (_root_.orderOf_pos x).ne' · rw [← orderOf_dvd_iff_pow_eq_one, lcm_orderOf_eq_exponent] exact order_dvd_exponent g
@[to_additive] theorem exponent_ne_zero_of_finite : exponent G ≠ 0 := ExponentExists.of_finite.exponent_ne_zero
Mathlib/GroupTheory/Exponent.lean
393
396
/- Copyright (c) 2021 Aaron Anderson, Jesse Michael Han, Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jesse Michael Han, Floris van Doorn -/ import Mathlib.Data.Fin.VecNotation import Mathlib.SetTheory.Cardinal.Basic /-! # Basics on First-Order Structures This file defines first-order languages and structures in the style of the [Flypitch project](https://flypitch.github.io/), as well as several important maps between structures. ## Main Definitions - A `FirstOrder.Language` defines a language as a pair of functions from the natural numbers to `Type l`. One sends `n` to the type of `n`-ary functions, and the other sends `n` to the type of `n`-ary relations. - A `FirstOrder.Language.Structure` interprets the symbols of a given `FirstOrder.Language` in the context of a given type. - A `FirstOrder.Language.Hom`, denoted `M →[L] N`, is a map from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations (although only in the forward direction). - A `FirstOrder.Language.Embedding`, denoted `M ↪[L] N`, is an embedding from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations in both directions. - A `FirstOrder.Language.Equiv`, denoted `M ≃[L] N`, is an equivalence from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations in both directions. ## References For the Flypitch project: - [J. Han, F. van Doorn, *A formal proof of the independence of the continuum hypothesis*] [flypitch_cpp] - [J. Han, F. van Doorn, *A formalization of forcing and the unprovability of the continuum hypothesis*][flypitch_itp] -/ universe u v u' v' w w' open Cardinal namespace FirstOrder /-! ### Languages and Structures -/ -- intended to be used with explicit universe parameters /-- A first-order language consists of a type of functions of every natural-number arity and a type of relations of every natural-number arity. -/ @[nolint checkUnivs] structure Language where /-- For every arity, a `Type*` of functions of that arity -/ Functions : ℕ → Type u /-- For every arity, a `Type*` of relations of that arity -/ Relations : ℕ → Type v namespace Language variable (L : Language.{u, v}) /-- A language is relational when it has no function symbols. -/ abbrev IsRelational : Prop := ∀ n, IsEmpty (L.Functions n) /-- A language is algebraic when it has no relation symbols. -/ abbrev IsAlgebraic : Prop := ∀ n, IsEmpty (L.Relations n) /-- The empty language has no symbols. -/ protected def empty : Language := ⟨fun _ => Empty, fun _ => Empty⟩ deriving IsAlgebraic, IsRelational instance : Inhabited Language := ⟨Language.empty⟩ /-- The sum of two languages consists of the disjoint union of their symbols. -/ protected def sum (L' : Language.{u', v'}) : Language := ⟨fun n => L.Functions n ⊕ L'.Functions n, fun n => L.Relations n ⊕ L'.Relations n⟩ /-- The type of constants in a given language. -/ protected abbrev Constants := L.Functions 0 /-- The type of symbols in a given language. -/ abbrev Symbols := (Σ l, L.Functions l) ⊕ (Σ l, L.Relations l) /-- The cardinality of a language is the cardinality of its type of symbols. -/ def card : Cardinal := #L.Symbols variable {L} {L' : Language.{u', v'}} theorem card_eq_card_functions_add_card_relations : L.card = (Cardinal.sum fun l => Cardinal.lift.{v} #(L.Functions l)) + Cardinal.sum fun l => Cardinal.lift.{u} #(L.Relations l) := by simp only [card, mk_sum, mk_sigma, lift_sum] instance isRelational_sum [L.IsRelational] [L'.IsRelational] : IsRelational (L.sum L') := fun _ => instIsEmptySum instance isAlgebraic_sum [L.IsAlgebraic] [L'.IsAlgebraic] : IsAlgebraic (L.sum L') := fun _ => instIsEmptySum @[simp] theorem card_empty : Language.empty.card = 0 := by simp only [card, mk_sum, mk_sigma, mk_eq_zero, sum_const, mk_eq_aleph0, lift_id', mul_zero, add_zero] @[deprecated (since := "2025-02-05")] alias empty_card := card_empty instance isEmpty_empty : IsEmpty Language.empty.Symbols := by simp only [Language.Symbols, isEmpty_sum, isEmpty_sigma] exact ⟨fun _ => inferInstance, fun _ => inferInstance⟩ instance Countable.countable_functions [h : Countable L.Symbols] : Countable (Σl, L.Functions l) := @Function.Injective.countable _ _ h _ Sum.inl_injective @[simp] theorem card_functions_sum (i : ℕ) : #((L.sum L').Functions i) = (Cardinal.lift.{u'} #(L.Functions i) + Cardinal.lift.{u} #(L'.Functions i) : Cardinal) := by simp [Language.sum] @[simp] theorem card_relations_sum (i : ℕ) : #((L.sum L').Relations i) = Cardinal.lift.{v'} #(L.Relations i) + Cardinal.lift.{v} #(L'.Relations i) := by simp [Language.sum] theorem card_sum : (L.sum L').card = Cardinal.lift.{max u' v'} L.card + Cardinal.lift.{max u v} L'.card := by simp only [card, mk_sum, mk_sigma, card_functions_sum, sum_add_distrib', lift_add, lift_sum, lift_lift, card_relations_sum, add_assoc, add_comm (Cardinal.sum fun i => (#(L'.Functions i)).lift)] /-- Passes a `DecidableEq` instance on a type of function symbols through the `Language` constructor. Despite the fact that this is proven by `inferInstance`, it is still needed - see the `example`s in `ModelTheory/Ring/Basic`. -/ instance instDecidableEqFunctions {f : ℕ → Type*} {R : ℕ → Type*} (n : ℕ) [DecidableEq (f n)] : DecidableEq ((⟨f, R⟩ : Language).Functions n) := inferInstance /-- Passes a `DecidableEq` instance on a type of relation symbols through the `Language` constructor. Despite the fact that this is proven by `inferInstance`, it is still needed - see the `example`s in `ModelTheory/Ring/Basic`. -/ instance instDecidableEqRelations {f : ℕ → Type*} {R : ℕ → Type*} (n : ℕ) [DecidableEq (R n)] : DecidableEq ((⟨f, R⟩ : Language).Relations n) := inferInstance variable (L) (M : Type w) /-- A first-order structure on a type `M` consists of interpretations of all the symbols in a given language. Each function of arity `n` is interpreted as a function sending tuples of length `n` (modeled as `(Fin n → M)`) to `M`, and a relation of arity `n` is a function from tuples of length `n` to `Prop`. -/ @[ext] class Structure where /-- Interpretation of the function symbols -/ funMap : ∀ {n}, L.Functions n → (Fin n → M) → M := by exact fun {n} => isEmptyElim /-- Interpretation of the relation symbols -/ RelMap : ∀ {n}, L.Relations n → (Fin n → M) → Prop := by exact fun {n} => isEmptyElim variable (N : Type w') [L.Structure M] [L.Structure N] open Structure /-- Used for defining `FirstOrder.Language.Theory.ModelType.instInhabited`. -/ def Inhabited.trivialStructure {α : Type*} [Inhabited α] : L.Structure α := ⟨default, default⟩ /-! ### Maps -/ /-- A homomorphism between first-order structures is a function that commutes with the interpretations of functions and maps tuples in one structure where a given relation is true to tuples in the second structure where that relation is still true. -/ structure Hom where /-- The underlying function of a homomorphism of structures -/ toFun : M → N /-- The homomorphism commutes with the interpretations of the function symbols -/ -- Porting note: -- The autoparam here used to be `obviously`. We would like to replace it with `aesop` -- but that isn't currently sufficient. -- See https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Aesop.20and.20cases -- If that can be improved, we should change this to `by aesop` and remove the proofs below. map_fun' : ∀ {n} (f : L.Functions n) (x), toFun (funMap f x) = funMap f (toFun ∘ x) := by intros; trivial /-- The homomorphism sends related elements to related elements -/ map_rel' : ∀ {n} (r : L.Relations n) (x), RelMap r x → RelMap r (toFun ∘ x) := by -- Porting note: see porting note on `Hom.map_fun'` intros; trivial @[inherit_doc] scoped[FirstOrder] notation:25 A " →[" L "] " B => FirstOrder.Language.Hom L A B /-- An embedding of first-order structures is an embedding that commutes with the interpretations of functions and relations. -/ structure Embedding extends M ↪ N where map_fun' : ∀ {n} (f : L.Functions n) (x), toFun (funMap f x) = funMap f (toFun ∘ x) := by -- Porting note: see porting note on `Hom.map_fun'` intros; trivial map_rel' : ∀ {n} (r : L.Relations n) (x), RelMap r (toFun ∘ x) ↔ RelMap r x := by -- Porting note: see porting note on `Hom.map_fun'` intros; trivial @[inherit_doc] scoped[FirstOrder] notation:25 A " ↪[" L "] " B => FirstOrder.Language.Embedding L A B /-- An equivalence of first-order structures is an equivalence that commutes with the interpretations of functions and relations. -/ structure Equiv extends M ≃ N where map_fun' : ∀ {n} (f : L.Functions n) (x), toFun (funMap f x) = funMap f (toFun ∘ x) := by -- Porting note: see porting note on `Hom.map_fun'` intros; trivial map_rel' : ∀ {n} (r : L.Relations n) (x), RelMap r (toFun ∘ x) ↔ RelMap r x := by -- Porting note: see porting note on `Hom.map_fun'` intros; trivial @[inherit_doc] scoped[FirstOrder] notation:25 A " ≃[" L "] " B => FirstOrder.Language.Equiv L A B variable {L M N} {P : Type*} [L.Structure P] {Q : Type*} [L.Structure Q] /-- Interpretation of a constant symbol -/ @[coe] def constantMap (c : L.Constants) : M := funMap c default instance : CoeTC L.Constants M := ⟨constantMap⟩ theorem funMap_eq_coe_constants {c : L.Constants} {x : Fin 0 → M} : funMap c x = c := congr rfl (funext finZeroElim) /-- Given a language with a nonempty type of constants, any structure will be nonempty. This cannot be a global instance, because `L` becomes a metavariable. -/ theorem nonempty_of_nonempty_constants [h : Nonempty L.Constants] : Nonempty M := h.map (↑) /-- `HomClass L F M N` states that `F` is a type of `L`-homomorphisms. You should extend this typeclass when you extend `FirstOrder.Language.Hom`. -/ class HomClass (L : outParam Language) (F : Type*) (M N : outParam Type*) [FunLike F M N] [L.Structure M] [L.Structure N] : Prop where map_fun : ∀ (φ : F) {n} (f : L.Functions n) (x), φ (funMap f x) = funMap f (φ ∘ x) map_rel : ∀ (φ : F) {n} (r : L.Relations n) (x), RelMap r x → RelMap r (φ ∘ x) /-- `StrongHomClass L F M N` states that `F` is a type of `L`-homomorphisms which preserve relations in both directions. -/ class StrongHomClass (L : outParam Language) (F : Type*) (M N : outParam Type*) [FunLike F M N] [L.Structure M] [L.Structure N] : Prop where map_fun : ∀ (φ : F) {n} (f : L.Functions n) (x), φ (funMap f x) = funMap f (φ ∘ x) map_rel : ∀ (φ : F) {n} (r : L.Relations n) (x), RelMap r (φ ∘ x) ↔ RelMap r x instance (priority := 100) StrongHomClass.homClass {F : Type*} [L.Structure M] [L.Structure N] [FunLike F M N] [StrongHomClass L F M N] : HomClass L F M N where map_fun := StrongHomClass.map_fun map_rel φ _ R x := (StrongHomClass.map_rel φ R x).2 /-- Not an instance to avoid a loop. -/ theorem HomClass.strongHomClassOfIsAlgebraic [L.IsAlgebraic] {F M N} [L.Structure M] [L.Structure N] [FunLike F M N] [HomClass L F M N] : StrongHomClass L F M N where map_fun := HomClass.map_fun map_rel _ _ := isEmptyElim theorem HomClass.map_constants {F M N} [L.Structure M] [L.Structure N] [FunLike F M N] [HomClass L F M N] (φ : F) (c : L.Constants) : φ c = c := (HomClass.map_fun φ c default).trans (congr rfl (funext default)) attribute [inherit_doc FirstOrder.Language.Hom.map_fun'] FirstOrder.Language.Embedding.map_fun' FirstOrder.Language.HomClass.map_fun FirstOrder.Language.StrongHomClass.map_fun FirstOrder.Language.Equiv.map_fun' attribute [inherit_doc FirstOrder.Language.Hom.map_rel'] FirstOrder.Language.Embedding.map_rel' FirstOrder.Language.HomClass.map_rel FirstOrder.Language.StrongHomClass.map_rel FirstOrder.Language.Equiv.map_rel' namespace Hom instance instFunLike : FunLike (M →[L] N) M N where coe := Hom.toFun coe_injective' f g h := by cases f; cases g; cases h; rfl instance homClass : HomClass L (M →[L] N) M N where map_fun := map_fun' map_rel := map_rel' instance [L.IsAlgebraic] : StrongHomClass L (M →[L] N) M N := HomClass.strongHomClassOfIsAlgebraic @[simp] theorem toFun_eq_coe {f : M →[L] N} : f.toFun = (f : M → N) := rfl @[ext] theorem ext ⦃f g : M →[L] N⦄ (h : ∀ x, f x = g x) : f = g := DFunLike.ext f g h @[simp] theorem map_fun (φ : M →[L] N) {n : ℕ} (f : L.Functions n) (x : Fin n → M) : φ (funMap f x) = funMap f (φ ∘ x) := HomClass.map_fun φ f x @[simp] theorem map_constants (φ : M →[L] N) (c : L.Constants) : φ c = c := HomClass.map_constants φ c @[simp] theorem map_rel (φ : M →[L] N) {n : ℕ} (r : L.Relations n) (x : Fin n → M) : RelMap r x → RelMap r (φ ∘ x) := HomClass.map_rel φ r x variable (L) (M) /-- The identity map from a structure to itself. -/ @[refl] def id : M →[L] M where toFun m := m variable {L} {M} instance : Inhabited (M →[L] M) := ⟨id L M⟩ @[simp] theorem id_apply (x : M) : id L M x = x := rfl /-- Composition of first-order homomorphisms. -/ @[trans] def comp (hnp : N →[L] P) (hmn : M →[L] N) : M →[L] P where toFun := hnp ∘ hmn -- Porting note: should be done by autoparam? map_fun' _ _ := by simp; rfl -- Porting note: should be done by autoparam? map_rel' _ _ h := map_rel _ _ _ (map_rel _ _ _ h) @[simp] theorem comp_apply (g : N →[L] P) (f : M →[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order homomorphisms is associative. -/ theorem comp_assoc (f : M →[L] N) (g : N →[L] P) (h : P →[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] theorem comp_id (f : M →[L] N) : f.comp (id L M) = f := rfl @[simp] theorem id_comp (f : M →[L] N) : (id L N).comp f = f := rfl end Hom /-- Any element of a `HomClass` can be realized as a first_order homomorphism. -/ @[simps] def HomClass.toHom {F M N} [L.Structure M] [L.Structure N] [FunLike F M N] [HomClass L F M N] : F → M →[L] N := fun φ => ⟨φ, HomClass.map_fun φ, HomClass.map_rel φ⟩ namespace Embedding instance funLike : FunLike (M ↪[L] N) M N where coe f := f.toFun coe_injective' f g h := by cases f cases g congr ext x exact funext_iff.1 h x instance embeddingLike : EmbeddingLike (M ↪[L] N) M N where injective' f := f.toEmbedding.injective instance strongHomClass : StrongHomClass L (M ↪[L] N) M N where map_fun := map_fun' map_rel := map_rel' @[simp] theorem map_fun (φ : M ↪[L] N) {n : ℕ} (f : L.Functions n) (x : Fin n → M) : φ (funMap f x) = funMap f (φ ∘ x) := HomClass.map_fun φ f x @[simp] theorem map_constants (φ : M ↪[L] N) (c : L.Constants) : φ c = c := HomClass.map_constants φ c @[simp] theorem map_rel (φ : M ↪[L] N) {n : ℕ} (r : L.Relations n) (x : Fin n → M) : RelMap r (φ ∘ x) ↔ RelMap r x := StrongHomClass.map_rel φ r x /-- A first-order embedding is also a first-order homomorphism. -/ def toHom : (M ↪[L] N) → M →[L] N := HomClass.toHom @[simp] theorem coe_toHom {f : M ↪[L] N} : (f.toHom : M → N) = f := rfl theorem coe_injective : @Function.Injective (M ↪[L] N) (M → N) (↑) | f, g, h => by cases f cases g congr ext x exact funext_iff.1 h x @[ext] theorem ext ⦃f g : M ↪[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) theorem toHom_injective : @Function.Injective (M ↪[L] N) (M →[L] N) (·.toHom) := by intro f f' h ext exact congr_fun (congr_arg (↑) h) _ @[simp] theorem toHom_inj {f g : M ↪[L] N} : f.toHom = g.toHom ↔ f = g := ⟨fun h ↦ toHom_injective h, fun h ↦ congr_arg (·.toHom) h⟩ theorem injective (f : M ↪[L] N) : Function.Injective f := f.toEmbedding.injective /-- In an algebraic language, any injective homomorphism is an embedding. -/ @[simps!] def ofInjective [L.IsAlgebraic] {f : M →[L] N} (hf : Function.Injective f) : M ↪[L] N := { f with inj' := hf map_rel' := fun {_} r x => StrongHomClass.map_rel f r x } @[simp] theorem coeFn_ofInjective [L.IsAlgebraic] {f : M →[L] N} (hf : Function.Injective f) : (ofInjective hf : M → N) = f := rfl @[simp] theorem ofInjective_toHom [L.IsAlgebraic] {f : M →[L] N} (hf : Function.Injective f) : (ofInjective hf).toHom = f := by ext; simp variable (L) (M) /-- The identity embedding from a structure to itself. -/ @[refl] def refl : M ↪[L] M where toEmbedding := Function.Embedding.refl M variable {L} {M} instance : Inhabited (M ↪[L] M) := ⟨refl L M⟩ @[simp] theorem refl_apply (x : M) : refl L M x = x := rfl /-- Composition of first-order embeddings. -/ @[trans] def comp (hnp : N ↪[L] P) (hmn : M ↪[L] N) : M ↪[L] P where toFun := hnp ∘ hmn inj' := hnp.injective.comp hmn.injective -- Porting note: should be done by autoparam? map_fun' := by intros; simp only [Function.comp_apply, map_fun]; trivial -- Porting note: should be done by autoparam? map_rel' := by intros; rw [Function.comp_assoc, map_rel, map_rel] @[simp] theorem comp_apply (g : N ↪[L] P) (f : M ↪[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order embeddings is associative. -/ theorem comp_assoc (f : M ↪[L] N) (g : N ↪[L] P) (h : P ↪[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl theorem comp_injective (h : N ↪[L] P) : Function.Injective (h.comp : (M ↪[L] N) → (M ↪[L] P)) := by intro f g hfg ext x; exact h.injective (DFunLike.congr_fun hfg x) @[simp] theorem comp_inj (h : N ↪[L] P) (f g : M ↪[L] N) : h.comp f = h.comp g ↔ f = g := ⟨fun eq ↦ h.comp_injective eq, congr_arg h.comp⟩ theorem toHom_comp_injective (h : N ↪[L] P) : Function.Injective (h.toHom.comp : (M →[L] N) → (M →[L] P)) := by intro f g hfg ext x; exact h.injective (DFunLike.congr_fun hfg x) @[simp] theorem toHom_comp_inj (h : N ↪[L] P) (f g : M →[L] N) : h.toHom.comp f = h.toHom.comp g ↔ f = g := ⟨fun eq ↦ h.toHom_comp_injective eq, congr_arg h.toHom.comp⟩ @[simp] theorem comp_toHom (hnp : N ↪[L] P) (hmn : M ↪[L] N) : (hnp.comp hmn).toHom = hnp.toHom.comp hmn.toHom := rfl @[simp] theorem comp_refl (f : M ↪[L] N) : f.comp (refl L M) = f := DFunLike.coe_injective rfl @[simp] theorem refl_comp (f : M ↪[L] N) : (refl L N).comp f = f := DFunLike.coe_injective rfl @[simp] theorem refl_toHom : (refl L M).toHom = Hom.id L M := rfl end Embedding /-- Any element of an injective `StrongHomClass` can be realized as a first_order embedding. -/ @[simps] def StrongHomClass.toEmbedding {F M N} [L.Structure M] [L.Structure N] [FunLike F M N] [EmbeddingLike F M N] [StrongHomClass L F M N] : F → M ↪[L] N := fun φ => ⟨⟨φ, EmbeddingLike.injective φ⟩, StrongHomClass.map_fun φ, StrongHomClass.map_rel φ⟩ namespace Equiv instance : EquivLike (M ≃[L] N) M N where coe f := f.toFun inv f := f.invFun left_inv f := f.left_inv right_inv f := f.right_inv coe_injective' f g h₁ h₂ := by cases f cases g simp only [mk.injEq] ext x exact funext_iff.1 h₁ x instance : StrongHomClass L (M ≃[L] N) M N where map_fun := map_fun' map_rel := map_rel' /-- The inverse of a first-order equivalence is a first-order equivalence. -/ @[symm] def symm (f : M ≃[L] N) : N ≃[L] M := { f.toEquiv.symm with map_fun' := fun n f' {x} => by simp only [Equiv.toFun_as_coe] rw [Equiv.symm_apply_eq] refine Eq.trans ?_ (f.map_fun' f' (f.toEquiv.symm ∘ x)).symm rw [← Function.comp_assoc, Equiv.toFun_as_coe, Equiv.self_comp_symm, Function.id_comp] map_rel' := fun n r {x} => by simp only [Equiv.toFun_as_coe] refine (f.map_rel' r (f.toEquiv.symm ∘ x)).symm.trans ?_ rw [← Function.comp_assoc, Equiv.toFun_as_coe, Equiv.self_comp_symm, Function.id_comp] } @[simp] theorem symm_symm (f : M ≃[L] N) : f.symm.symm = f := rfl theorem symm_bijective : Function.Bijective (symm : (M ≃[L] N) → _) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ @[simp] theorem apply_symm_apply (f : M ≃[L] N) (a : N) : f (f.symm a) = a := f.toEquiv.apply_symm_apply a @[simp] theorem symm_apply_apply (f : M ≃[L] N) (a : M) : f.symm (f a) = a := f.toEquiv.symm_apply_apply a @[simp] theorem map_fun (φ : M ≃[L] N) {n : ℕ} (f : L.Functions n) (x : Fin n → M) : φ (funMap f x) = funMap f (φ ∘ x) := HomClass.map_fun φ f x @[simp] theorem map_constants (φ : M ≃[L] N) (c : L.Constants) : φ c = c := HomClass.map_constants φ c @[simp] theorem map_rel (φ : M ≃[L] N) {n : ℕ} (r : L.Relations n) (x : Fin n → M) : RelMap r (φ ∘ x) ↔ RelMap r x := StrongHomClass.map_rel φ r x /-- A first-order equivalence is also a first-order embedding. -/ def toEmbedding : (M ≃[L] N) → M ↪[L] N := StrongHomClass.toEmbedding /-- A first-order equivalence is also a first-order homomorphism. -/ def toHom : (M ≃[L] N) → M →[L] N := HomClass.toHom @[simp] theorem toEmbedding_toHom (f : M ≃[L] N) : f.toEmbedding.toHom = f.toHom := rfl @[simp] theorem coe_toHom {f : M ≃[L] N} : (f.toHom : M → N) = (f : M → N) := rfl @[simp] theorem coe_toEmbedding (f : M ≃[L] N) : (f.toEmbedding : M → N) = (f : M → N) := rfl theorem injective_toEmbedding : Function.Injective (toEmbedding : (M ≃[L] N) → M ↪[L] N) := by intro _ _ h; apply DFunLike.coe_injective; exact congr_arg (DFunLike.coe ∘ Embedding.toHom) h theorem coe_injective : @Function.Injective (M ≃[L] N) (M → N) (↑) := DFunLike.coe_injective @[ext] theorem ext ⦃f g : M ≃[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) theorem bijective (f : M ≃[L] N) : Function.Bijective f := EquivLike.bijective f theorem injective (f : M ≃[L] N) : Function.Injective f := EquivLike.injective f theorem surjective (f : M ≃[L] N) : Function.Surjective f := EquivLike.surjective f variable (L) (M) /-- The identity equivalence from a structure to itself. -/ @[refl] def refl : M ≃[L] M where toEquiv := _root_.Equiv.refl M variable {L} {M} instance : Inhabited (M ≃[L] M) := ⟨refl L M⟩ @[simp] theorem refl_apply (x : M) : refl L M x = x := by simp [refl]; rfl /-- Composition of first-order equivalences. -/ @[trans] def comp (hnp : N ≃[L] P) (hmn : M ≃[L] N) : M ≃[L] P := { hmn.toEquiv.trans hnp.toEquiv with toFun := hnp ∘ hmn -- Porting note: should be done by autoparam? map_fun' := by intros; simp only [Function.comp_apply, map_fun]; trivial -- Porting note: should be done by autoparam? map_rel' := by intros; rw [Function.comp_assoc, map_rel, map_rel] } @[simp] theorem comp_apply (g : N ≃[L] P) (f : M ≃[L] N) (x : M) : g.comp f x = g (f x) := rfl @[simp] theorem comp_refl (g : M ≃[L] N) : g.comp (refl L M) = g := rfl @[simp] theorem refl_comp (g : M ≃[L] N) : (refl L N).comp g = g := rfl @[simp] theorem refl_toEmbedding : (refl L M).toEmbedding = Embedding.refl L M := rfl @[simp] theorem refl_toHom : (refl L M).toHom = Hom.id L M := rfl /-- Composition of first-order homomorphisms is associative. -/ theorem comp_assoc (f : M ≃[L] N) (g : N ≃[L] P) (h : P ≃[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl theorem injective_comp (h : N ≃[L] P) : Function.Injective (h.comp : (M ≃[L] N) → (M ≃[L] P)) := by intro f g hfg ext x; exact h.injective (congr_fun (congr_arg DFunLike.coe hfg) x) @[simp] theorem comp_toHom (hnp : N ≃[L] P) (hmn : M ≃[L] N) : (hnp.comp hmn).toHom = hnp.toHom.comp hmn.toHom := rfl @[simp] theorem comp_toEmbedding (hnp : N ≃[L] P) (hmn : M ≃[L] N) : (hnp.comp hmn).toEmbedding = hnp.toEmbedding.comp hmn.toEmbedding := rfl @[simp] theorem self_comp_symm (f : M ≃[L] N) : f.comp f.symm = refl L N := by ext; rw [comp_apply, apply_symm_apply, refl_apply] @[simp] theorem symm_comp_self (f : M ≃[L] N) : f.symm.comp f = refl L M := by ext; rw [comp_apply, symm_apply_apply, refl_apply] @[simp] theorem symm_comp_self_toEmbedding (f : M ≃[L] N) : f.symm.toEmbedding.comp f.toEmbedding = Embedding.refl L M := by rw [← comp_toEmbedding, symm_comp_self, refl_toEmbedding] @[simp] theorem self_comp_symm_toEmbedding (f : M ≃[L] N) : f.toEmbedding.comp f.symm.toEmbedding = Embedding.refl L N := by rw [← comp_toEmbedding, self_comp_symm, refl_toEmbedding] @[simp] theorem symm_comp_self_toHom (f : M ≃[L] N) : f.symm.toHom.comp f.toHom = Hom.id L M := by rw [← comp_toHom, symm_comp_self, refl_toHom] @[simp] theorem self_comp_symm_toHom (f : M ≃[L] N) : f.toHom.comp f.symm.toHom = Hom.id L N := by rw [← comp_toHom, self_comp_symm, refl_toHom] @[simp] theorem comp_symm (f : M ≃[L] N) (g : N ≃[L] P) : (g.comp f).symm = f.symm.comp g.symm := rfl theorem comp_right_injective (h : M ≃[L] N) : Function.Injective (fun f ↦ f.comp h : (N ≃[L] P) → (M ≃[L] P)) := by intro f g hfg convert (congr_arg (fun r : (M ≃[L] P) ↦ r.comp h.symm) hfg) <;> rw [comp_assoc, self_comp_symm, comp_refl] @[simp] theorem comp_right_inj (h : M ≃[L] N) (f g : N ≃[L] P) : f.comp h = g.comp h ↔ f = g := ⟨fun eq ↦ h.comp_right_injective eq, congr_arg (fun (r : N ≃[L] P) ↦ r.comp h)⟩ end Equiv /-- Any element of a bijective `StrongHomClass` can be realized as a first_order isomorphism. -/ @[simps] def StrongHomClass.toEquiv {F M N} [L.Structure M] [L.Structure N] [EquivLike F M N] [StrongHomClass L F M N] : F → M ≃[L] N := fun φ => ⟨⟨φ, EquivLike.inv φ, EquivLike.left_inv φ, EquivLike.right_inv φ⟩, StrongHomClass.map_fun φ, StrongHomClass.map_rel φ⟩ section SumStructure variable (L₁ L₂ : Language) (S : Type*) [L₁.Structure S] [L₂.Structure S] instance sumStructure : (L₁.sum L₂).Structure S where funMap := Sum.elim funMap funMap RelMap := Sum.elim RelMap RelMap variable {L₁ L₂ S} @[simp] theorem funMap_sumInl {n : ℕ} (f : L₁.Functions n) : @funMap (L₁.sum L₂) S _ n (Sum.inl f) = funMap f := rfl @[simp] theorem funMap_sumInr {n : ℕ} (f : L₂.Functions n) : @funMap (L₁.sum L₂) S _ n (Sum.inr f) = funMap f := rfl @[simp] theorem relMap_sumInl {n : ℕ} (R : L₁.Relations n) : @RelMap (L₁.sum L₂) S _ n (Sum.inl R) = RelMap R := rfl @[simp] theorem relMap_sumInr {n : ℕ} (R : L₂.Relations n) : @RelMap (L₁.sum L₂) S _ n (Sum.inr R) = RelMap R := rfl @[deprecated (since := "2025-02-21")] alias funMap_sum_inl := funMap_sumInl @[deprecated (since := "2025-02-21")] alias funMap_sum_inr := funMap_sumInr @[deprecated (since := "2025-02-21")] alias relMap_sum_inl := relMap_sumInl @[deprecated (since := "2025-02-21")] alias relMap_sum_inr := relMap_sumInr end SumStructure section Empty /-- Any type can be made uniquely into a structure over the empty language. -/ def emptyStructure : Language.empty.Structure M where instance : Unique (Language.empty.Structure M) := ⟨⟨Language.emptyStructure⟩, fun a => by ext _ f <;> exact Empty.elim f⟩ variable [Language.empty.Structure M] [Language.empty.Structure N] instance (priority := 100) strongHomClassEmpty {F} [FunLike F M N] : StrongHomClass Language.empty F M N := ⟨fun _ _ f => Empty.elim f, fun _ _ r => Empty.elim r⟩ @[simp] theorem empty.nonempty_embedding_iff : Nonempty (M ↪[Language.empty] N) ↔ Cardinal.lift.{w'} #M ≤ Cardinal.lift.{w} #N := _root_.trans ⟨Nonempty.map fun f => f.toEmbedding, Nonempty.map StrongHomClass.toEmbedding⟩ Cardinal.lift_mk_le'.symm @[simp] theorem empty.nonempty_equiv_iff : Nonempty (M ≃[Language.empty] N) ↔ Cardinal.lift.{w'} #M = Cardinal.lift.{w} #N := _root_.trans ⟨Nonempty.map fun f => f.toEquiv, Nonempty.map fun f => { toEquiv := f }⟩ Cardinal.lift_mk_eq'.symm /-- Makes a `Language.empty.Hom` out of any function. This is only needed because there is no instance of `FunLike (M → N) M N`, and thus no instance of `Language.empty.HomClass M N`. -/ @[simps] def _root_.Function.emptyHom (f : M → N) : M →[Language.empty] N where toFun := f end Empty end Language end FirstOrder namespace Equiv open FirstOrder FirstOrder.Language FirstOrder.Language.Structure open FirstOrder variable {L : Language} {M : Type*} {N : Type*} [L.Structure M] /-- A structure induced by a bijection. -/ @[simps!] def inducedStructure (e : M ≃ N) : L.Structure N := ⟨fun f x => e (funMap f (e.symm ∘ x)), fun r x => RelMap r (e.symm ∘ x)⟩ /-- A bijection as a first-order isomorphism with the induced structure on the codomain. -/ def inducedStructureEquiv (e : M ≃ N) : @Language.Equiv L M N _ (inducedStructure e) := by letI : L.Structure N := inducedStructure e exact { e with map_fun' := @fun n f x => by simp [← Function.comp_assoc e.symm e x] map_rel' := @fun n r x => by simp [← Function.comp_assoc e.symm e x] } @[simp] theorem toEquiv_inducedStructureEquiv (e : M ≃ N) : @Language.Equiv.toEquiv L M N _ (inducedStructure e) (inducedStructureEquiv e) = e := rfl @[simp] theorem toFun_inducedStructureEquiv (e : M ≃ N) : DFunLike.coe (@inducedStructureEquiv L M N _ e) = e := rfl @[simp] theorem toFun_inducedStructureEquiv_Symm (e : M ≃ N) : (by letI : L.Structure N := inducedStructure e exact DFunLike.coe (@inducedStructureEquiv L M N _ e).symm) = (e.symm : N → M) := rfl end Equiv
Mathlib/ModelTheory/Basic.lean
989
991
/- 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
Mathlib/Algebra/Polynomial/Degree/TrailingDegree.lean
128
131
/- Copyright (c) 2024 Peter Nelson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Peter Nelson -/ import Mathlib.Data.Matroid.Minor.Restrict /-! # Some constructions of matroids This file defines some very elementary examples of matroids, namely those with at most one base. ## Main definitions * `emptyOn α` is the matroid on `α` with empty ground set. For `E : Set α`, ... * `loopyOn E` is the matroid on `E` whose elements are all loops, or equivalently in which `∅` is the only base. * `freeOn E` is the 'free matroid' whose ground set `E` is the only base. * For `I ⊆ E`, `uniqueBaseOn I E` is the matroid with ground set `E` in which `I` is the only base. ## Implementation details To avoid the tedious process of certifying the matroid axioms for each of these easy examples, we bootstrap the definitions starting with `emptyOn α` (which `simp` can prove is a matroid) and then construct the other examples using duality and restriction. -/ assert_not_exists Field variable {α : Type*} {M : Matroid α} {E B I X R J : Set α} namespace Matroid open Set section EmptyOn /-- The `Matroid α` with empty ground set. -/ def emptyOn (α : Type*) : Matroid α where E := ∅ IsBase := (· = ∅) Indep := (· = ∅) indep_iff' := by simp [subset_empty_iff] exists_isBase := ⟨∅, rfl⟩ isBase_exchange := by rintro _ _ rfl; simp maximality := by rintro _ _ _ rfl -; exact ⟨∅, by simp [Maximal]⟩ subset_ground := by simp @[simp] theorem emptyOn_ground : (emptyOn α).E = ∅ := rfl @[simp] theorem emptyOn_isBase_iff : (emptyOn α).IsBase B ↔ B = ∅ := Iff.rfl @[simp] theorem emptyOn_indep_iff : (emptyOn α).Indep I ↔ I = ∅ := Iff.rfl theorem ground_eq_empty_iff : (M.E = ∅) ↔ M = emptyOn α := by simp only [emptyOn, ext_iff_indep, iff_self_and] exact fun h ↦ by simp [h, subset_empty_iff] @[simp] theorem emptyOn_dual_eq : (emptyOn α)✶ = emptyOn α := by rw [← ground_eq_empty_iff]; rfl @[simp] theorem restrict_empty (M : Matroid α) : M ↾ (∅ : Set α) = emptyOn α := by simp [← ground_eq_empty_iff] theorem eq_emptyOn_or_nonempty (M : Matroid α) : M = emptyOn α ∨ Matroid.Nonempty M := by rw [← ground_eq_empty_iff] exact M.E.eq_empty_or_nonempty.elim Or.inl (fun h ↦ Or.inr ⟨h⟩) theorem eq_emptyOn [IsEmpty α] (M : Matroid α) : M = emptyOn α := by rw [← ground_eq_empty_iff] exact M.E.eq_empty_of_isEmpty instance finite_emptyOn (α : Type*) : (emptyOn α).Finite := ⟨finite_empty⟩ end EmptyOn section LoopyOn /-- The `Matroid α` with ground set `E` whose only base is `∅`. The elements are all 'loops' - see `Matroid.IsLoop` and `Matroid.loopyOn_isLoop_iff`. -/ def loopyOn (E : Set α) : Matroid α := emptyOn α ↾ E @[simp] theorem loopyOn_ground (E : Set α) : (loopyOn E).E = E := rfl @[simp] theorem loopyOn_empty (α : Type*) : loopyOn (∅ : Set α) = emptyOn α := by rw [← ground_eq_empty_iff, loopyOn_ground] @[simp] theorem loopyOn_indep_iff : (loopyOn E).Indep I ↔ I = ∅ := by simp only [loopyOn, restrict_indep_iff, emptyOn_indep_iff, and_iff_left_iff_imp] rintro rfl; apply empty_subset theorem eq_loopyOn_iff : M = loopyOn E ↔ M.E = E ∧ ∀ X ⊆ M.E, M.Indep X → X = ∅ := by simp only [ext_iff_indep, loopyOn_ground, loopyOn_indep_iff, and_congr_right_iff] rintro rfl refine ⟨fun h I hI ↦ (h hI).1, fun h I hIE ↦ ⟨h I hIE, by rintro rfl; simp⟩⟩ @[simp] theorem loopyOn_isBase_iff : (loopyOn E).IsBase B ↔ B = ∅ := by simp [Maximal, isBase_iff_maximal_indep] @[simp] theorem loopyOn_isBasis_iff : (loopyOn E).IsBasis I X ↔ I = ∅ ∧ X ⊆ E := ⟨fun h ↦ ⟨loopyOn_indep_iff.mp h.indep, h.subset_ground⟩, by rintro ⟨rfl, hX⟩; rw [isBasis_iff]; simp⟩ instance : RankFinite (loopyOn E) := ⟨⟨∅, loopyOn_isBase_iff.2 rfl, finite_empty⟩⟩ theorem Finite.loopyOn_finite (hE : E.Finite) : Matroid.Finite (loopyOn E) := ⟨hE⟩ @[simp] theorem loopyOn_restrict (E R : Set α) : (loopyOn E) ↾ R = loopyOn R := by refine ext_indep rfl ?_ simp only [restrict_ground_eq, restrict_indep_iff, loopyOn_indep_iff, and_iff_left_iff_imp] exact fun _ h _ ↦ h theorem empty_isBase_iff : M.IsBase ∅ ↔ M = loopyOn M.E := by simp only [isBase_iff_maximal_indep, Maximal, empty_indep, le_eq_subset, empty_subset, subset_empty_iff, true_implies, true_and, ext_iff_indep, loopyOn_ground, loopyOn_indep_iff] exact ⟨fun h I _ ↦ ⟨@h _, fun hI ↦ by simp [hI]⟩, fun h I hI ↦ (h hI.subset_ground).1 hI⟩ theorem eq_loopyOn_or_rankPos (M : Matroid α) : M = loopyOn M.E ∨ RankPos M := by rw [← empty_isBase_iff, rankPos_iff]; apply em theorem not_rankPos_iff : ¬RankPos M ↔ M = loopyOn M.E := by rw [rankPos_iff, not_iff_comm, empty_isBase_iff] instance loopyOn_rankFinite : RankFinite (loopyOn E) := ⟨∅, by simp⟩ end LoopyOn section FreeOn /-- The `Matroid α` with ground set `E` whose only base is `E`. -/ def freeOn (E : Set α) : Matroid α := (loopyOn E)✶ @[simp] theorem freeOn_ground : (freeOn E).E = E := rfl @[simp] theorem freeOn_dual_eq : (freeOn E)✶ = loopyOn E := by rw [freeOn, dual_dual] @[simp] theorem loopyOn_dual_eq : (loopyOn E)✶ = freeOn E := rfl @[simp] theorem freeOn_empty (α : Type*) : freeOn (∅ : Set α) = emptyOn α := by simp [freeOn] @[simp] theorem freeOn_isBase_iff : (freeOn E).IsBase B ↔ B = E := by simp only [freeOn, loopyOn_ground, dual_isBase_iff', loopyOn_isBase_iff, diff_eq_empty, ← subset_antisymm_iff, eq_comm (a := E)] @[simp] theorem freeOn_indep_iff : (freeOn E).Indep I ↔ I ⊆ E := by simp [indep_iff] theorem freeOn_indep (hIE : I ⊆ E) : (freeOn E).Indep I := freeOn_indep_iff.2 hIE @[simp] theorem freeOn_isBasis_iff : (freeOn E).IsBasis I X ↔ I = X ∧ X ⊆ E := by use fun h ↦ ⟨(freeOn_indep h.subset_ground).eq_of_isBasis h ,h.subset_ground⟩ rintro ⟨rfl, hIE⟩ exact (freeOn_indep hIE).isBasis_self @[simp] theorem freeOn_isBasis'_iff : (freeOn E).IsBasis' I X ↔ I = X ∩ E := by rw [isBasis'_iff_isBasis_inter_ground, freeOn_isBasis_iff, freeOn_ground, and_iff_left inter_subset_right] theorem eq_freeOn_iff : M = freeOn E ↔ M.E = E ∧ M.Indep E := by refine ⟨?_, fun h ↦ ?_⟩ · rintro rfl; simp [Subset.rfl] simp only [ext_iff_indep, freeOn_ground, freeOn_indep_iff, h.1, true_and] exact fun I hIX ↦ iff_of_true (h.2.subset hIX) hIX theorem ground_indep_iff_eq_freeOn : M.Indep M.E ↔ M = freeOn M.E := by simp [eq_freeOn_iff] theorem freeOn_restrict (h : R ⊆ E) : (freeOn E) ↾ R = freeOn R := by simp [h, eq_freeOn_iff, Subset.rfl] theorem restrict_eq_freeOn_iff : M ↾ I = freeOn I ↔ M.Indep I := by rw [eq_freeOn_iff, and_iff_right M.restrict_ground_eq, restrict_indep_iff, and_iff_left Subset.rfl] theorem Indep.restrict_eq_freeOn (hI : M.Indep I) : M ↾ I = freeOn I := by rwa [restrict_eq_freeOn_iff] instance freeOn_finitary : Finitary (freeOn E) := by simp only [finitary_iff, freeOn_indep_iff] exact fun I h e heI ↦ by simpa using h {e} (by simpa) lemma freeOn_rankPos (hE : E.Nonempty) : RankPos (freeOn E) := by
simp [rankPos_iff, hE.ne_empty.symm]
Mathlib/Data/Matroid/Constructions.lean
195
196
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import Mathlib.Control.Basic import Mathlib.Data.Nat.Basic import Mathlib.Data.Option.Basic import Mathlib.Data.List.Defs import Mathlib.Data.List.Monad import Mathlib.Logic.OpClass import Mathlib.Logic.Unique import Mathlib.Order.Basic import Mathlib.Tactic.Common /-! # Basic properties of lists -/ assert_not_exists GroupWithZero assert_not_exists Lattice assert_not_exists Prod.swap_eq_iff_eq_swap assert_not_exists Ring assert_not_exists Set.range open Function open Nat hiding one_pos namespace List universe u v w variable {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {l₁ l₂ : List α} /-- There is only one list of an empty type -/ instance uniqueOfIsEmpty [IsEmpty α] : Unique (List α) := { instInhabitedList with uniq := fun l => match l with | [] => rfl | a :: _ => isEmptyElim a } instance : Std.LawfulIdentity (α := List α) Append.append [] where left_id := nil_append right_id := append_nil instance : Std.Associative (α := List α) Append.append where assoc := append_assoc @[simp] theorem cons_injective {a : α} : Injective (cons a) := fun _ _ => tail_eq_of_cons_eq theorem singleton_injective : Injective fun a : α => [a] := fun _ _ h => (cons_eq_cons.1 h).1 theorem set_of_mem_cons (l : List α) (a : α) : { x | x ∈ a :: l } = insert a { x | x ∈ l } := Set.ext fun _ => mem_cons /-! ### mem -/ theorem _root_.Decidable.List.eq_or_ne_mem_of_mem [DecidableEq α] {a b : α} {l : List α} (h : a ∈ b :: l) : a = b ∨ a ≠ b ∧ a ∈ l := by by_cases hab : a = b · exact Or.inl hab · exact ((List.mem_cons.1 h).elim Or.inl (fun h => Or.inr ⟨hab, h⟩)) lemma mem_pair {a b c : α} : a ∈ [b, c] ↔ a = b ∨ a = c := by rw [mem_cons, mem_singleton] -- The simpNF linter says that the LHS can be simplified via `List.mem_map`. -- However this is a higher priority lemma. -- It seems the side condition `hf` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Injective f) {a : α} {l : List α} : f a ∈ map f l ↔ a ∈ l := ⟨fun m => let ⟨_, m', e⟩ := exists_of_mem_map m; H e ▸ m', mem_map_of_mem⟩ @[simp] theorem _root_.Function.Involutive.exists_mem_and_apply_eq_iff {f : α → α} (hf : Function.Involutive f) (x : α) (l : List α) : (∃ y : α, y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by rintro ⟨y, h, rfl⟩; rwa [hf y], fun h => ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : α → α} (hf : Involutive f) {a : α} {l : List α} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] /-! ### length -/ alias ⟨_, length_pos_of_ne_nil⟩ := length_pos_iff theorem length_pos_iff_ne_nil {l : List α} : 0 < length l ↔ l ≠ [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ theorem exists_of_length_succ {n} : ∀ l : List α, l.length = n + 1 → ∃ h t, l = h :: t | [], H => absurd H.symm <| succ_ne_zero n | h :: t, _ => ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : Injective (List.length : List α → ℕ) ↔ Subsingleton α := by constructor · intro h; refine ⟨fun x y => ?_⟩; (suffices [x] = [y] by simpa using this); apply h; rfl · intros hα l1 l2 hl induction l1 generalizing l2 <;> cases l2 · rfl · cases hl · cases hl · next ih _ _ => congr · subsingleton · apply ih; simpa using hl @[simp default+1] -- Raise priority above `length_injective_iff`. lemma length_injective [Subsingleton α] : Injective (length : List α → ℕ) := length_injective_iff.mpr inferInstance theorem length_eq_two {l : List α} : l.length = 2 ↔ ∃ a b, l = [a, b] := ⟨fun _ => let [a, b] := l; ⟨a, b, rfl⟩, fun ⟨_, _, e⟩ => e ▸ rfl⟩ theorem length_eq_three {l : List α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] := ⟨fun _ => let [a, b, c] := l; ⟨a, b, c, rfl⟩, fun ⟨_, _, _, e⟩ => e ▸ rfl⟩ /-! ### set-theoretic notation of lists -/ instance instSingletonList : Singleton α (List α) := ⟨fun x => [x]⟩ instance [DecidableEq α] : Insert α (List α) := ⟨List.insert⟩ instance [DecidableEq α] : LawfulSingleton α (List α) := { insert_empty_eq := fun x => show (if x ∈ ([] : List α) then [] else [x]) = [x] from if_neg not_mem_nil } theorem singleton_eq (x : α) : ({x} : List α) = [x] := rfl theorem insert_neg [DecidableEq α] {x : α} {l : List α} (h : x ∉ l) : Insert.insert x l = x :: l := insert_of_not_mem h theorem insert_pos [DecidableEq α] {x : α} {l : List α} (h : x ∈ l) : Insert.insert x l = l := insert_of_mem h theorem doubleton_eq [DecidableEq α] {x y : α} (h : x ≠ y) : ({x, y} : List α) = [x, y] := by rw [insert_neg, singleton_eq] rwa [singleton_eq, mem_singleton] /-! ### bounded quantifiers over lists -/ theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : List α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : List α) (h : p a) : ∃ x ∈ a :: l, p x := ⟨a, mem_cons_self, h⟩ theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ l, p x) → ∃ x ∈ a :: l, p x := fun ⟨x, xl, px⟩ => ⟨x, mem_cons_of_mem _ xl, px⟩ theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ a :: l, p x) → p a ∨ ∃ x ∈ l, p x := fun ⟨x, xal, px⟩ => Or.elim (eq_or_mem_of_mem_cons xal) (fun h : x = a => by rw [← h]; left; exact px) fun h : x ∈ l => Or.inr ⟨x, h, px⟩ theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : List α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := Iff.intro or_exists_of_exists_mem_cons fun h => Or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists /-! ### list subset -/ theorem cons_subset_of_subset_of_mem {a : α} {l m : List α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ l₂ l : List α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := fun _ h ↦ (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem map_subset_iff {l₁ l₂ : List α} (f : α → β) (h : Injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := by refine ⟨?_, map_subset f⟩; intro h2 x hx rcases mem_map.1 (h2 (mem_map_of_mem hx)) with ⟨x', hx', hxx'⟩ cases h hxx'; exact hx' /-! ### append -/ theorem append_eq_has_append {L₁ L₂ : List α} : List.append L₁ L₂ = L₁ ++ L₂ := rfl theorem append_right_injective (s : List α) : Injective fun t ↦ s ++ t := fun _ _ ↦ append_cancel_left theorem append_left_injective (t : List α) : Injective fun s ↦ s ++ t := fun _ _ ↦ append_cancel_right /-! ### replicate -/ theorem eq_replicate_length {a : α} : ∀ {l : List α}, l = replicate l.length a ↔ ∀ b ∈ l, b = a | [] => by simp | (b :: l) => by simp [eq_replicate_length, replicate_succ] theorem replicate_add (m n) (a : α) : replicate (m + n) a = replicate m a ++ replicate n a := by rw [replicate_append_replicate] theorem replicate_subset_singleton (n) (a : α) : replicate n a ⊆ [a] := fun _ h => mem_singleton.2 (eq_of_mem_replicate h) theorem subset_singleton_iff {a : α} {L : List α} : L ⊆ [a] ↔ ∃ n, L = replicate n a := by simp only [eq_replicate_iff, subset_def, mem_singleton, exists_eq_left'] theorem replicate_right_injective {n : ℕ} (hn : n ≠ 0) : Injective (@replicate α n) := fun _ _ h => (eq_replicate_iff.1 h).2 _ <| mem_replicate.2 ⟨hn, rfl⟩ theorem replicate_right_inj {a b : α} {n : ℕ} (hn : n ≠ 0) : replicate n a = replicate n b ↔ a = b := (replicate_right_injective hn).eq_iff theorem replicate_right_inj' {a b : α} : ∀ {n}, replicate n a = replicate n b ↔ n = 0 ∨ a = b | 0 => by simp | n + 1 => (replicate_right_inj n.succ_ne_zero).trans <| by simp only [n.succ_ne_zero, false_or] theorem replicate_left_injective (a : α) : Injective (replicate · a) := LeftInverse.injective (length_replicate (n := ·)) theorem replicate_left_inj {a : α} {n m : ℕ} : replicate n a = replicate m a ↔ n = m := (replicate_left_injective a).eq_iff @[simp] theorem head?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.head? = l.head? := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h induction l <;> simp [replicate] @[simp] theorem getLast?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.getLast? = l.getLast? := by rw [← List.head?_reverse, ← List.head?_reverse, List.reverse_flatten, List.map_replicate, List.reverse_replicate, head?_flatten_replicate h] /-! ### pure -/ theorem mem_pure (x y : α) : x ∈ (pure y : List α) ↔ x = y := by simp /-! ### bind -/ @[simp] theorem bind_eq_flatMap {α β} (f : α → List β) (l : List α) : l >>= f = l.flatMap f := rfl /-! ### concat -/ /-! ### reverse -/ theorem reverse_cons' (a : α) (l : List α) : reverse (a :: l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] theorem reverse_concat' (l : List α) (a : α) : (l ++ [a]).reverse = a :: l.reverse := by rw [reverse_append]; rfl @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_involutive : Involutive (@reverse α) := reverse_reverse @[simp] theorem reverse_injective : Injective (@reverse α) := reverse_involutive.injective theorem reverse_surjective : Surjective (@reverse α) := reverse_involutive.surjective theorem reverse_bijective : Bijective (@reverse α) := reverse_involutive.bijective theorem concat_eq_reverse_cons (a : α) (l : List α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] theorem map_reverseAux (f : α → β) (l₁ l₂ : List α) : map f (reverseAux l₁ l₂) = reverseAux (map f l₁) (map f l₂) := by simp only [reverseAux_eq, map_append, map_reverse] -- TODO: Rename `List.reverse_perm` to `List.reverse_perm_self` @[simp] lemma reverse_perm' : l₁.reverse ~ l₂ ↔ l₁ ~ l₂ where mp := l₁.reverse_perm.symm.trans mpr := l₁.reverse_perm.trans @[simp] lemma perm_reverse : l₁ ~ l₂.reverse ↔ l₁ ~ l₂ where mp hl := hl.trans l₂.reverse_perm mpr hl := hl.trans l₂.reverse_perm.symm /-! ### getLast -/ attribute [simp] getLast_cons theorem getLast_append_singleton {a : α} (l : List α) : getLast (l ++ [a]) (append_ne_nil_of_right_ne_nil l (cons_ne_nil a _)) = a := by simp [getLast_append] theorem getLast_append_of_right_ne_nil (l₁ l₂ : List α) (h : l₂ ≠ []) : getLast (l₁ ++ l₂) (append_ne_nil_of_right_ne_nil l₁ h) = getLast l₂ h := by induction l₁ with | nil => simp | cons _ _ ih => simp only [cons_append]; rw [List.getLast_cons]; exact ih @[deprecated (since := "2025-02-06")] alias getLast_append' := getLast_append_of_right_ne_nil theorem getLast_concat' {a : α} (l : List α) : getLast (concat l a) (by simp) = a := by simp @[simp] theorem getLast_singleton' (a : α) : getLast [a] (cons_ne_nil a []) = a := rfl @[simp] theorem getLast_cons_cons (a₁ a₂ : α) (l : List α) : getLast (a₁ :: a₂ :: l) (cons_ne_nil _ _) = getLast (a₂ :: l) (cons_ne_nil a₂ l) := rfl theorem dropLast_append_getLast : ∀ {l : List α} (h : l ≠ []), dropLast l ++ [getLast l h] = l | [], h => absurd rfl h | [_], _ => rfl | a :: b :: l, h => by rw [dropLast_cons₂, cons_append, getLast_cons (cons_ne_nil _ _)] congr exact dropLast_append_getLast (cons_ne_nil b l) theorem getLast_congr {l₁ l₂ : List α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : getLast l₁ h₁ = getLast l₂ h₂ := by subst l₁; rfl theorem getLast_replicate_succ (m : ℕ) (a : α) : (replicate (m + 1) a).getLast (ne_nil_of_length_eq_add_one length_replicate) = a := by simp only [replicate_succ'] exact getLast_append_singleton _ @[deprecated (since := "2025-02-07")] alias getLast_filter' := getLast_filter_of_pos /-! ### getLast? -/ theorem mem_getLast?_eq_getLast : ∀ {l : List α} {x : α}, x ∈ l.getLast? → ∃ h, x = getLast l h | [], x, hx => False.elim <| by simp at hx | [a], x, hx => have : a = x := by simpa using hx this ▸ ⟨cons_ne_nil a [], rfl⟩ | a :: b :: l, x, hx => by rw [getLast?_cons_cons] at hx rcases mem_getLast?_eq_getLast hx with ⟨_, h₂⟩ use cons_ne_nil _ _ assumption theorem getLast?_eq_getLast_of_ne_nil : ∀ {l : List α} (h : l ≠ []), l.getLast? = some (l.getLast h) | [], h => (h rfl).elim | [_], _ => rfl | _ :: b :: l, _ => @getLast?_eq_getLast_of_ne_nil (b :: l) (cons_ne_nil _ _) theorem mem_getLast?_cons {x y : α} : ∀ {l : List α}, x ∈ l.getLast? → x ∈ (y :: l).getLast? | [], _ => by contradiction | _ :: _, h => h theorem dropLast_append_getLast? : ∀ {l : List α}, ∀ a ∈ l.getLast?, dropLast l ++ [a] = l | [], a, ha => (Option.not_mem_none a ha).elim | [a], _, rfl => rfl | a :: b :: l, c, hc => by rw [getLast?_cons_cons] at hc rw [dropLast_cons₂, cons_append, dropLast_append_getLast? _ hc] theorem getLastI_eq_getLast? [Inhabited α] : ∀ l : List α, l.getLastI = l.getLast?.iget | [] => by simp [getLastI, Inhabited.default] | [_] => rfl | [_, _] => rfl | [_, _, _] => rfl | _ :: _ :: c :: l => by simp [getLastI, getLastI_eq_getLast? (c :: l)] theorem getLast?_append_cons : ∀ (l₁ : List α) (a : α) (l₂ : List α), getLast? (l₁ ++ a :: l₂) = getLast? (a :: l₂) | [], _, _ => rfl | [_], _, _ => rfl | b :: c :: l₁, a, l₂ => by rw [cons_append, cons_append, getLast?_cons_cons, ← cons_append, getLast?_append_cons (c :: l₁)] theorem getLast?_append_of_ne_nil (l₁ : List α) : ∀ {l₂ : List α} (_ : l₂ ≠ []), getLast? (l₁ ++ l₂) = getLast? l₂ | [], hl₂ => by contradiction | b :: l₂, _ => getLast?_append_cons l₁ b l₂ theorem mem_getLast?_append_of_mem_getLast? {l₁ l₂ : List α} {x : α} (h : x ∈ l₂.getLast?) : x ∈ (l₁ ++ l₂).getLast? := by cases l₂ · contradiction · rw [List.getLast?_append_cons] exact h /-! ### head(!?) and tail -/ @[simp] theorem head!_nil [Inhabited α] : ([] : List α).head! = default := rfl @[simp] theorem head_cons_tail (x : List α) (h : x ≠ []) : x.head h :: x.tail = x := by cases x <;> simp at h ⊢ theorem head_eq_getElem_zero {l : List α} (hl : l ≠ []) : l.head hl = l[0]'(length_pos_iff.2 hl) := (getElem_zero _).symm theorem head!_eq_head? [Inhabited α] (l : List α) : head! l = (head? l).iget := by cases l <;> rfl theorem surjective_head! [Inhabited α] : Surjective (@head! α _) := fun x => ⟨[x], rfl⟩ theorem surjective_head? : Surjective (@head? α) := Option.forall.2 ⟨⟨[], rfl⟩, fun x => ⟨[x], rfl⟩⟩ theorem surjective_tail : Surjective (@tail α) | [] => ⟨[], rfl⟩ | a :: l => ⟨a :: a :: l, rfl⟩ theorem eq_cons_of_mem_head? {x : α} : ∀ {l : List α}, x ∈ l.head? → l = x :: tail l | [], h => (Option.not_mem_none _ h).elim | a :: l, h => by simp only [head?, Option.mem_def, Option.some_inj] at h exact h ▸ rfl @[simp] theorem head!_cons [Inhabited α] (a : α) (l : List α) : head! (a :: l) = a := rfl @[simp] theorem head!_append [Inhabited α] (t : List α) {s : List α} (h : s ≠ []) : head! (s ++ t) = head! s := by induction s · contradiction · rfl theorem mem_head?_append_of_mem_head? {s t : List α} {x : α} (h : x ∈ s.head?) : x ∈ (s ++ t).head? := by cases s · contradiction · exact h theorem head?_append_of_ne_nil : ∀ (l₁ : List α) {l₂ : List α} (_ : l₁ ≠ []), head? (l₁ ++ l₂) = head? l₁ | _ :: _, _, _ => rfl theorem tail_append_singleton_of_ne_nil {a : α} {l : List α} (h : l ≠ nil) : tail (l ++ [a]) = tail l ++ [a] := by induction l · contradiction · rw [tail, cons_append, tail] theorem cons_head?_tail : ∀ {l : List α} {a : α}, a ∈ head? l → a :: tail l = l | [], a, h => by contradiction | b :: l, a, h => by simp? at h says simp only [head?_cons, Option.mem_def, Option.some.injEq] at h simp [h] theorem head!_mem_head? [Inhabited α] : ∀ {l : List α}, l ≠ [] → head! l ∈ head? l | [], h => by contradiction | _ :: _, _ => rfl theorem cons_head!_tail [Inhabited α] {l : List α} (h : l ≠ []) : head! l :: tail l = l := cons_head?_tail (head!_mem_head? h) theorem head!_mem_self [Inhabited α] {l : List α} (h : l ≠ nil) : l.head! ∈ l := by have h' : l.head! ∈ l.head! :: l.tail := mem_cons_self rwa [cons_head!_tail h] at h' theorem get_eq_getElem? (l : List α) (i : Fin l.length) : l.get i = l[i]?.get (by simp [getElem?_eq_getElem]) := by simp @[deprecated (since := "2025-02-15")] alias get_eq_get? := get_eq_getElem? theorem exists_mem_iff_getElem {l : List α} {p : α → Prop} : (∃ x ∈ l, p x) ↔ ∃ (i : ℕ) (_ : i < l.length), p l[i] := by simp only [mem_iff_getElem] exact ⟨fun ⟨_x, ⟨i, hi, hix⟩, hxp⟩ ↦ ⟨i, hi, hix ▸ hxp⟩, fun ⟨i, hi, hp⟩ ↦ ⟨_, ⟨i, hi, rfl⟩, hp⟩⟩ theorem forall_mem_iff_getElem {l : List α} {p : α → Prop} : (∀ x ∈ l, p x) ↔ ∀ (i : ℕ) (_ : i < l.length), p l[i] := by simp [mem_iff_getElem, @forall_swap α] theorem get_tail (l : List α) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := (by simp only [length_tail] at h; omega)) : l.tail.get ⟨i, h⟩ = l.get ⟨i + 1, h'⟩ := by cases l <;> [cases h; rfl] /-! ### sublists -/ attribute [refl] List.Sublist.refl theorem Sublist.cons_cons {l₁ l₂ : List α} (a : α) (s : l₁ <+ l₂) : a :: l₁ <+ a :: l₂ := Sublist.cons₂ _ s lemma cons_sublist_cons' {a b : α} : a :: l₁ <+ b :: l₂ ↔ a :: l₁ <+ l₂ ∨ a = b ∧ l₁ <+ l₂ := by constructor · rintro (_ | _) · exact Or.inl ‹_› · exact Or.inr ⟨rfl, ‹_›⟩ · rintro (h | ⟨rfl, h⟩) · exact h.cons _ · rwa [cons_sublist_cons] theorem sublist_cons_of_sublist (a : α) (h : l₁ <+ l₂) : l₁ <+ a :: l₂ := h.cons _ @[deprecated (since := "2025-02-07")] alias sublist_nil_iff_eq_nil := sublist_nil @[simp] lemma sublist_singleton {l : List α} {a : α} : l <+ [a] ↔ l = [] ∨ l = [a] := by constructor <;> rintro (_ | _) <;> aesop theorem Sublist.antisymm (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := s₁.eq_of_length_le s₂.length_le /-- If the first element of two lists are different, then a sublist relation can be reduced. -/ theorem Sublist.of_cons_of_ne {a b} (h₁ : a ≠ b) (h₂ : a :: l₁ <+ b :: l₂) : a :: l₁ <+ l₂ := match h₁, h₂ with | _, .cons _ h => h /-! ### indexOf -/ section IndexOf variable [DecidableEq α] theorem idxOf_cons_eq {a b : α} (l : List α) : b = a → idxOf a (b :: l) = 0 | e => by rw [← e]; exact idxOf_cons_self @[deprecated (since := "2025-01-30")] alias indexOf_cons_eq := idxOf_cons_eq @[simp] theorem idxOf_cons_ne {a b : α} (l : List α) : b ≠ a → idxOf a (b :: l) = succ (idxOf a l) | h => by simp only [idxOf_cons, Bool.cond_eq_ite, beq_iff_eq, if_neg h] @[deprecated (since := "2025-01-30")] alias indexOf_cons_ne := idxOf_cons_ne theorem idxOf_eq_length_iff {a : α} {l : List α} : idxOf a l = length l ↔ a ∉ l := by induction l with | nil => exact iff_of_true rfl not_mem_nil | cons b l ih => simp only [length, mem_cons, idxOf_cons, eq_comm] rw [cond_eq_if] split_ifs with h <;> simp at h · exact iff_of_false (by rintro ⟨⟩) fun H => H <| Or.inl h.symm · simp only [Ne.symm h, false_or] rw [← ih] exact succ_inj @[simp] theorem idxOf_of_not_mem {l : List α} {a : α} : a ∉ l → idxOf a l = length l := idxOf_eq_length_iff.2 @[deprecated (since := "2025-01-30")] alias indexOf_of_not_mem := idxOf_of_not_mem theorem idxOf_le_length {a : α} {l : List α} : idxOf a l ≤ length l := by induction l with | nil => rfl | cons b l ih => ?_ simp only [length, idxOf_cons, cond_eq_if, beq_iff_eq] by_cases h : b = a · rw [if_pos h]; exact Nat.zero_le _ · rw [if_neg h]; exact succ_le_succ ih @[deprecated (since := "2025-01-30")] alias indexOf_le_length := idxOf_le_length theorem idxOf_lt_length_iff {a} {l : List α} : idxOf a l < length l ↔ a ∈ l := ⟨fun h => Decidable.byContradiction fun al => Nat.ne_of_lt h <| idxOf_eq_length_iff.2 al, fun al => (lt_of_le_of_ne idxOf_le_length) fun h => idxOf_eq_length_iff.1 h al⟩ @[deprecated (since := "2025-01-30")] alias indexOf_lt_length_iff := idxOf_lt_length_iff theorem idxOf_append_of_mem {a : α} (h : a ∈ l₁) : idxOf a (l₁ ++ l₂) = idxOf a l₁ := by induction l₁ with | nil => exfalso exact not_mem_nil h | cons d₁ t₁ ih => rw [List.cons_append] by_cases hh : d₁ = a · iterate 2 rw [idxOf_cons_eq _ hh] rw [idxOf_cons_ne _ hh, idxOf_cons_ne _ hh, ih (mem_of_ne_of_mem (Ne.symm hh) h)] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_mem := idxOf_append_of_mem theorem idxOf_append_of_not_mem {a : α} (h : a ∉ l₁) : idxOf a (l₁ ++ l₂) = l₁.length + idxOf a l₂ := by induction l₁ with | nil => rw [List.nil_append, List.length, Nat.zero_add] | cons d₁ t₁ ih => rw [List.cons_append, idxOf_cons_ne _ (ne_of_not_mem_cons h).symm, List.length, ih (not_mem_of_not_mem_cons h), Nat.succ_add] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_not_mem := idxOf_append_of_not_mem end IndexOf /-! ### nth element -/ section deprecated @[simp] theorem getElem?_length (l : List α) : l[l.length]? = none := getElem?_eq_none le_rfl /-- A version of `getElem_map` that can be used for rewriting. -/ theorem getElem_map_rev (f : α → β) {l} {n : Nat} {h : n < l.length} : f l[n] = (map f l)[n]'((l.length_map f).symm ▸ h) := Eq.symm (getElem_map _) theorem get_length_sub_one {l : List α} (h : l.length - 1 < l.length) : l.get ⟨l.length - 1, h⟩ = l.getLast (by rintro rfl; exact Nat.lt_irrefl 0 h) := (getLast_eq_getElem _).symm theorem take_one_drop_eq_of_lt_length {l : List α} {n : ℕ} (h : n < l.length) : (l.drop n).take 1 = [l.get ⟨n, h⟩] := by rw [drop_eq_getElem_cons h, take, take] simp theorem ext_getElem?' {l₁ l₂ : List α} (h' : ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]?) : l₁ = l₂ := by apply ext_getElem? intro n rcases Nat.lt_or_ge n <| max l₁.length l₂.length with hn | hn · exact h' n hn · simp_all [Nat.max_le, getElem?_eq_none] @[deprecated (since := "2025-02-15")] alias ext_get?' := ext_getElem?' @[deprecated (since := "2025-02-15")] alias ext_get?_iff := List.ext_getElem?_iff theorem ext_get_iff {l₁ l₂ : List α} : l₁ = l₂ ↔ l₁.length = l₂.length ∧ ∀ n h₁ h₂, get l₁ ⟨n, h₁⟩ = get l₂ ⟨n, h₂⟩ := by constructor · rintro rfl exact ⟨rfl, fun _ _ _ ↦ rfl⟩ · intro ⟨h₁, h₂⟩ exact ext_get h₁ h₂ theorem ext_getElem?_iff' {l₁ l₂ : List α} : l₁ = l₂ ↔ ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]? := ⟨by rintro rfl _ _; rfl, ext_getElem?'⟩ @[deprecated (since := "2025-02-15")] alias ext_get?_iff' := ext_getElem?_iff' /-- If two lists `l₁` and `l₂` are the same length and `l₁[n]! = l₂[n]!` for all `n`, then the lists are equal. -/ theorem ext_getElem! [Inhabited α] (hl : length l₁ = length l₂) (h : ∀ n : ℕ, l₁[n]! = l₂[n]!) : l₁ = l₂ := ext_getElem hl fun n h₁ h₂ ↦ by simpa only [← getElem!_pos] using h n @[simp] theorem getElem_idxOf [DecidableEq α] {a : α} : ∀ {l : List α} (h : idxOf a l < l.length), l[idxOf a l] = a | b :: l, h => by by_cases h' : b = a <;> simp [h', if_pos, if_false, getElem_idxOf] @[deprecated (since := "2025-01-30")] alias getElem_indexOf := getElem_idxOf -- This is incorrectly named and should be `get_idxOf`; -- this already exists, so will require a deprecation dance. theorem idxOf_get [DecidableEq α] {a : α} {l : List α} (h) : get l ⟨idxOf a l, h⟩ = a := by simp @[deprecated (since := "2025-01-30")] alias indexOf_get := idxOf_get @[simp] theorem getElem?_idxOf [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : l[idxOf a l]? = some a := by rw [getElem?_eq_getElem, getElem_idxOf (idxOf_lt_length_iff.2 h)] @[deprecated (since := "2025-01-30")] alias getElem?_indexOf := getElem?_idxOf @[deprecated (since := "2025-02-15")] alias idxOf_get? := getElem?_idxOf @[deprecated (since := "2025-01-30")] alias indexOf_get? := getElem?_idxOf theorem idxOf_inj [DecidableEq α] {l : List α} {x y : α} (hx : x ∈ l) (hy : y ∈ l) : idxOf x l = idxOf y l ↔ x = y := ⟨fun h => by have x_eq_y : get l ⟨idxOf x l, idxOf_lt_length_iff.2 hx⟩ = get l ⟨idxOf y l, idxOf_lt_length_iff.2 hy⟩ := by simp only [h] simp only [idxOf_get] at x_eq_y; exact x_eq_y, fun h => by subst h; rfl⟩ @[deprecated (since := "2025-01-30")] alias indexOf_inj := idxOf_inj theorem get_reverse' (l : List α) (n) (hn') : l.reverse.get n = l.get ⟨l.length - 1 - n, hn'⟩ := by simp theorem eq_cons_of_length_one {l : List α} (h : l.length = 1) : l = [l.get ⟨0, by omega⟩] := by refine ext_get (by convert h) fun n h₁ h₂ => ?_ simp congr omega end deprecated @[simp] theorem getElem_set_of_ne {l : List α} {i j : ℕ} (h : i ≠ j) (a : α) (hj : j < (l.set i a).length) : (l.set i a)[j] = l[j]'(by simpa using hj) := by rw [← Option.some_inj, ← List.getElem?_eq_getElem, List.getElem?_set_ne h, List.getElem?_eq_getElem] /-! ### map -/ -- `List.map_const` (the version with `Function.const` instead of a lambda) is already tagged -- `simp` in Core -- TODO: Upstream the tagging to Core? attribute [simp] map_const' theorem flatMap_pure_eq_map (f : α → β) (l : List α) : l.flatMap (pure ∘ f) = map f l := .symm <| map_eq_flatMap .. theorem flatMap_congr {l : List α} {f g : α → List β} (h : ∀ x ∈ l, f x = g x) : l.flatMap f = l.flatMap g := (congr_arg List.flatten <| map_congr_left h :) theorem infix_flatMap_of_mem {a : α} {as : List α} (h : a ∈ as) (f : α → List α) : f a <:+: as.flatMap f := infix_of_mem_flatten (mem_map_of_mem h) @[simp] theorem map_eq_map {α β} (f : α → β) (l : List α) : f <$> l = map f l := rfl /-- A single `List.map` of a composition of functions is equal to composing a `List.map` with another `List.map`, fully applied. This is the reverse direction of `List.map_map`. -/ theorem comp_map (h : β → γ) (g : α → β) (l : List α) : map (h ∘ g) l = map h (map g l) := map_map.symm /-- Composing a `List.map` with another `List.map` is equal to a single `List.map` of composed functions. -/ @[simp] theorem map_comp_map (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := by ext l; rw [comp_map, Function.comp_apply] section map_bijectivity theorem _root_.Function.LeftInverse.list_map {f : α → β} {g : β → α} (h : LeftInverse f g) : LeftInverse (map f) (map g) | [] => by simp_rw [map_nil] | x :: xs => by simp_rw [map_cons, h x, h.list_map xs] nonrec theorem _root_.Function.RightInverse.list_map {f : α → β} {g : β → α} (h : RightInverse f g) : RightInverse (map f) (map g) := h.list_map nonrec theorem _root_.Function.Involutive.list_map {f : α → α} (h : Involutive f) : Involutive (map f) := Function.LeftInverse.list_map h @[simp] theorem map_leftInverse_iff {f : α → β} {g : β → α} : LeftInverse (map f) (map g) ↔ LeftInverse f g := ⟨fun h x => by injection h [x], (·.list_map)⟩ @[simp] theorem map_rightInverse_iff {f : α → β} {g : β → α} : RightInverse (map f) (map g) ↔ RightInverse f g := map_leftInverse_iff @[simp] theorem map_involutive_iff {f : α → α} : Involutive (map f) ↔ Involutive f := map_leftInverse_iff theorem _root_.Function.Injective.list_map {f : α → β} (h : Injective f) : Injective (map f) | [], [], _ => rfl | x :: xs, y :: ys, hxy => by injection hxy with hxy hxys rw [h hxy, h.list_map hxys] @[simp] theorem map_injective_iff {f : α → β} : Injective (map f) ↔ Injective f := by refine ⟨fun h x y hxy => ?_, (·.list_map)⟩ suffices [x] = [y] by simpa using this apply h simp [hxy] theorem _root_.Function.Surjective.list_map {f : α → β} (h : Surjective f) : Surjective (map f) := let ⟨_, h⟩ := h.hasRightInverse; h.list_map.surjective @[simp] theorem map_surjective_iff {f : α → β} : Surjective (map f) ↔ Surjective f := by refine ⟨fun h x => ?_, (·.list_map)⟩ let ⟨[y], hxy⟩ := h [x] exact ⟨_, List.singleton_injective hxy⟩ theorem _root_.Function.Bijective.list_map {f : α → β} (h : Bijective f) : Bijective (map f) := ⟨h.1.list_map, h.2.list_map⟩ @[simp] theorem map_bijective_iff {f : α → β} : Bijective (map f) ↔ Bijective f := by simp_rw [Function.Bijective, map_injective_iff, map_surjective_iff] end map_bijectivity theorem eq_of_mem_map_const {b₁ b₂ : β} {l : List α} (h : b₁ ∈ map (const α b₂) l) : b₁ = b₂ := by rw [map_const] at h; exact eq_of_mem_replicate h /-- `eq_nil_or_concat` in simp normal form -/ lemma eq_nil_or_concat' (l : List α) : l = [] ∨ ∃ L b, l = L ++ [b] := by simpa using l.eq_nil_or_concat /-! ### foldl, foldr -/ theorem foldl_ext (f g : α → β → α) (a : α) {l : List β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := by induction l generalizing a with | nil => rfl | cons hd tl ih => unfold foldl rw [ih _ fun a b bin => H a b <| mem_cons_of_mem _ bin, H a hd mem_cons_self] theorem foldr_ext (f g : α → β → β) (b : β) {l : List α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := by induction l with | nil => rfl | cons hd tl ih => ?_ simp only [mem_cons, or_imp, forall_and, forall_eq] at H simp only [foldr, ih H.2, H.1] theorem foldl_concat (f : β → α → β) (b : β) (x : α) (xs : List α) : List.foldl f b (xs ++ [x]) = f (List.foldl f b xs) x := by simp only [List.foldl_append, List.foldl] theorem foldr_concat (f : α → β → β) (b : β) (x : α) (xs : List α) : List.foldr f b (xs ++ [x]) = (List.foldr f (f x b) xs) := by simp only [List.foldr_append, List.foldr] theorem foldl_fixed' {f : α → β → α} {a : α} (hf : ∀ b, f a b = a) : ∀ l : List β, foldl f a l = a | [] => rfl | b :: l => by rw [foldl_cons, hf b, foldl_fixed' hf l] theorem foldr_fixed' {f : α → β → β} {b : β} (hf : ∀ a, f a b = b) : ∀ l : List α, foldr f b l = b | [] => rfl | a :: l => by rw [foldr_cons, foldr_fixed' hf l, hf a] @[simp] theorem foldl_fixed {a : α} : ∀ l : List β, foldl (fun a _ => a) a l = a := foldl_fixed' fun _ => rfl @[simp] theorem foldr_fixed {b : β} : ∀ l : List α, foldr (fun _ b => b) b l = b := foldr_fixed' fun _ => rfl @[deprecated foldr_cons_nil (since := "2025-02-10")] theorem foldr_eta (l : List α) : foldr cons [] l = l := foldr_cons_nil theorem reverse_foldl {l : List α} : reverse (foldl (fun t h => h :: t) [] l) = l := by simp theorem foldl_hom₂ (l : List ι) (f : α → β → γ) (op₁ : α → ι → α) (op₂ : β → ι → β) (op₃ : γ → ι → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ a i) (op₂ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl op₂ b l) := Eq.symm <| by revert a b induction l <;> intros <;> [rfl; simp only [*, foldl]] theorem foldr_hom₂ (l : List ι) (f : α → β → γ) (op₁ : ι → α → α) (op₂ : ι → β → β) (op₃ : ι → γ → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ i a) (op₂ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr op₂ b l) := by revert a induction l <;> intros <;> [rfl; simp only [*, foldr]] theorem injective_foldl_comp {l : List (α → α)} {f : α → α} (hl : ∀ f ∈ l, Function.Injective f) (hf : Function.Injective f) : Function.Injective (@List.foldl (α → α) (α → α) Function.comp f l) := by induction l generalizing f with | nil => exact hf | cons lh lt l_ih => apply l_ih fun _ h => hl _ (List.mem_cons_of_mem _ h) apply Function.Injective.comp hf apply hl _ mem_cons_self /-- Consider two lists `l₁` and `l₂` with designated elements `a₁` and `a₂` somewhere in them: `l₁ = x₁ ++ [a₁] ++ z₁` and `l₂ = x₂ ++ [a₂] ++ z₂`. Assume the designated element `a₂` is present in neither `x₁` nor `z₁`. We conclude that the lists are equal (`l₁ = l₂`) if and only if their respective parts are equal (`x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂`). -/ lemma append_cons_inj_of_not_mem {x₁ x₂ z₁ z₂ : List α} {a₁ a₂ : α} (notin_x : a₂ ∉ x₁) (notin_z : a₂ ∉ z₁) : x₁ ++ a₁ :: z₁ = x₂ ++ a₂ :: z₂ ↔ x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂ := by constructor · simp only [append_eq_append_iff, cons_eq_append_iff, cons_eq_cons] rintro (⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩ | ⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩) <;> simp_all · rintro ⟨rfl, rfl, rfl⟩ rfl section FoldlEqFoldr -- foldl and foldr coincide when f is commutative and associative variable {f : α → α → α} theorem foldl1_eq_foldr1 [hassoc : Std.Associative f] : ∀ a b l, foldl f a (l ++ [b]) = foldr f b (a :: l) | _, _, nil => rfl | a, b, c :: l => by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l] rw [hassoc.assoc] theorem foldl_eq_of_comm_of_assoc [hcomm : Std.Commutative f] [hassoc : Std.Associative f] : ∀ a b l, foldl f a (b :: l) = f b (foldl f a l) | a, b, nil => hcomm.comm a b | a, b, c :: l => by simp only [foldl_cons] have : RightCommutative f := inferInstance rw [← foldl_eq_of_comm_of_assoc .., this.right_comm, foldl_cons] theorem foldl_eq_foldr [Std.Commutative f] [Std.Associative f] : ∀ a l, foldl f a l = foldr f a l | _, nil => rfl | a, b :: l => by simp only [foldr_cons, foldl_eq_of_comm_of_assoc] rw [foldl_eq_foldr a l] end FoldlEqFoldr section FoldlEqFoldlr' variable {f : α → β → α} variable (hf : ∀ a b c, f (f a b) c = f (f a c) b) include hf theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b :: l) = f (foldl f a l) b | _, _, [] => rfl | a, b, c :: l => by rw [foldl, foldl, foldl, ← foldl_eq_of_comm' .., foldl, hf] theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l | _, [] => rfl | a, b :: l => by rw [foldl_eq_of_comm' hf, foldr, foldl_eq_foldr' ..]; rfl end FoldlEqFoldlr' section FoldlEqFoldlr' variable {f : α → β → β} theorem foldr_eq_of_comm' (hf : ∀ a b c, f a (f b c) = f b (f a c)) : ∀ a b l, foldr f a (b :: l) = foldr f (f b a) l | _, _, [] => rfl | a, b, c :: l => by rw [foldr, foldr, foldr, hf, ← foldr_eq_of_comm' hf ..]; rfl end FoldlEqFoldlr' section variable {op : α → α → α} [ha : Std.Associative op] /-- Notation for `op a b`. -/ local notation a " ⋆ " b => op a b /-- Notation for `foldl op a l`. -/ local notation l " <*> " a => foldl op a l theorem foldl_op_eq_op_foldr_assoc : ∀ {l : List α} {a₁ a₂}, ((l <*> a₁) ⋆ a₂) = a₁ ⋆ l.foldr (· ⋆ ·) a₂ | [], _, _ => rfl | a :: l, a₁, a₂ => by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] variable [hc : Std.Commutative op] theorem foldl_assoc_comm_cons {l : List α} {a₁ a₂} : ((a₁ :: l) <*> a₂) = a₁ ⋆ l <*> a₂ := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### foldlM, foldrM, mapM -/ section FoldlMFoldrM variable {m : Type v → Type w} [Monad m] variable [LawfulMonad m] theorem foldrM_eq_foldr (f : α → β → m β) (b l) : foldrM f b l = foldr (fun a mb => mb >>= f a) (pure b) l := by induction l <;> simp [*] theorem foldlM_eq_foldl (f : β → α → m β) (b l) : List.foldlM f b l = foldl (fun mb a => mb >>= fun b => f b a) (pure b) l := by suffices h : ∀ mb : m β, (mb >>= fun b => List.foldlM f b l) = foldl (fun mb a => mb >>= fun b => f b a) mb l by simp [← h (pure b)] induction l with | nil => intro; simp | cons _ _ l_ih => intro; simp only [List.foldlM, foldl, ← l_ih, functor_norm] end FoldlMFoldrM /-! ### intersperse -/ @[deprecated (since := "2025-02-07")] alias intersperse_singleton := intersperse_single @[deprecated (since := "2025-02-07")] alias intersperse_cons_cons := intersperse_cons₂ /-! ### map for partial functions -/ @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {l : List α} (hx : x ∈ l) : SizeOf.sizeOf x < SizeOf.sizeOf l := by induction l with | nil => ?_ | cons h t ih => ?_ <;> cases hx <;> rw [cons.sizeOf_spec] · omega · specialize ih ‹_› omega /-! ### filter -/ theorem length_eq_length_filter_add {l : List (α)} (f : α → Bool) : l.length = (l.filter f).length + (l.filter (! f ·)).length := by simp_rw [← List.countP_eq_length_filter, l.length_eq_countP_add_countP f, Bool.not_eq_true, Bool.decide_eq_false] /-! ### filterMap -/ theorem filterMap_eq_flatMap_toList (f : α → Option β) (l : List α) : l.filterMap f = l.flatMap fun a ↦ (f a).toList := by induction l with | nil => ?_ | cons a l ih => ?_ <;> simp [filterMap_cons] rcases f a <;> simp [ih] theorem filterMap_congr {f g : α → Option β} {l : List α} (h : ∀ x ∈ l, f x = g x) : l.filterMap f = l.filterMap g := by induction l <;> simp_all [filterMap_cons] theorem filterMap_eq_map_iff_forall_eq_some {f : α → Option β} {g : α → β} {l : List α} : l.filterMap f = l.map g ↔ ∀ x ∈ l, f x = some (g x) where mp := by induction l with | nil => simp | cons a l ih => ?_ rcases ha : f a with - | b <;> simp [ha, filterMap_cons] · intro h simpa [show (filterMap f l).length = l.length + 1 from by simp[h], Nat.add_one_le_iff] using List.length_filterMap_le f l · rintro rfl h exact ⟨rfl, ih h⟩ mpr h := Eq.trans (filterMap_congr <| by simpa) (congr_fun filterMap_eq_map _) /-! ### filter -/ section Filter variable {p : α → Bool} theorem filter_singleton {a : α} : [a].filter p = bif p a then [a] else [] := rfl theorem filter_eq_foldr (p : α → Bool) (l : List α) : filter p l = foldr (fun a out => bif p a then a :: out else out) [] l := by induction l <;> simp [*, filter]; rfl #adaptation_note /-- nightly-2024-07-27 This has to be temporarily renamed to avoid an unintentional collision. The prime should be removed at nightly-2024-07-27. -/ @[simp] theorem filter_subset' (l : List α) : filter p l ⊆ l := filter_sublist.subset theorem of_mem_filter {a : α} {l} (h : a ∈ filter p l) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset' l h theorem mem_filter_of_mem {a : α} {l} (h₁ : a ∈ l) (h₂ : p a) : a ∈ filter p l := mem_filter.2 ⟨h₁, h₂⟩ @[deprecated (since := "2025-02-07")] alias monotone_filter_left := filter_subset variable (p) theorem monotone_filter_right (l : List α) ⦃p q : α → Bool⦄ (h : ∀ a, p a → q a) : l.filter p <+ l.filter q := by induction l with | nil => rfl | cons hd tl IH => by_cases hp : p hd · rw [filter_cons_of_pos hp, filter_cons_of_pos (h _ hp)] exact IH.cons_cons hd · rw [filter_cons_of_neg hp] by_cases hq : q hd · rw [filter_cons_of_pos hq] exact sublist_cons_of_sublist hd IH · rw [filter_cons_of_neg hq] exact IH lemma map_filter {f : α → β} (hf : Injective f) (l : List α) [DecidablePred fun b => ∃ a, p a ∧ f a = b] : (l.filter p).map f = (l.map f).filter fun b => ∃ a, p a ∧ f a = b := by simp [comp_def, filter_map, hf.eq_iff] @[deprecated (since := "2025-02-07")] alias map_filter' := map_filter lemma filter_attach' (l : List α) (p : {a // a ∈ l} → Bool) [DecidableEq α] : l.attach.filter p = (l.filter fun x => ∃ h, p ⟨x, h⟩).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := by classical refine map_injective_iff.2 Subtype.coe_injective ?_ simp [comp_def, map_filter _ Subtype.coe_injective] lemma filter_attach (l : List α) (p : α → Bool) : (l.attach.filter fun x => p x : List {x // x ∈ l}) = (l.filter p).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := map_injective_iff.2 Subtype.coe_injective <| by simp_rw [map_map, comp_def, Subtype.map, id, ← Function.comp_apply (g := Subtype.val), ← filter_map, attach_map_subtype_val] lemma filter_comm (q) (l : List α) : filter p (filter q l) = filter q (filter p l) := by simp [Bool.and_comm] @[simp] theorem filter_true (l : List α) : filter (fun _ => true) l = l := by induction l <;> simp [*, filter] @[simp] theorem filter_false (l : List α) : filter (fun _ => false) l = [] := by induction l <;> simp [*, filter] end Filter /-! ### eraseP -/ section eraseP variable {p : α → Bool} @[simp] theorem length_eraseP_add_one {l : List α} {a} (al : a ∈ l) (pa : p a) : (l.eraseP p).length + 1 = l.length := by let ⟨_, l₁, l₂, _, _, h₁, h₂⟩ := exists_of_eraseP al pa rw [h₂, h₁, length_append, length_append] rfl end eraseP /-! ### erase -/ section Erase variable [DecidableEq α] @[simp] theorem length_erase_add_one {a : α} {l : List α} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_eraseP, length_eraseP_add_one h (decide_eq_true rfl)] theorem map_erase [DecidableEq β] {f : α → β} (finj : Injective f) {a : α} (l : List α) : map f (l.erase a) = (map f l).erase (f a) := by have this : (a == ·) = (f a == f ·) := by ext b; simp [beq_eq_decide, finj.eq_iff] rw [erase_eq_eraseP, erase_eq_eraseP, eraseP_map, this]; rfl theorem map_foldl_erase [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (foldl List.erase l₁ l₂) = foldl (fun l a => l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁ <;> [rfl; simp only [foldl_cons, map_erase finj, *]] theorem erase_getElem [DecidableEq ι] {l : List ι} {i : ℕ} (hi : i < l.length) : Perm (l.erase l[i]) (l.eraseIdx i) := by induction l generalizing i with | nil => simp | cons a l IH => cases i with | zero => simp | succ i => have hi' : i < l.length := by simpa using hi if ha : a = l[i] then simpa [ha] using .trans (perm_cons_erase (getElem_mem _)) (.cons _ (IH hi')) else simpa [ha] using IH hi' theorem length_eraseIdx_add_one {l : List ι} {i : ℕ} (h : i < l.length) : (l.eraseIdx i).length + 1 = l.length := by rw [length_eraseIdx] split <;> omega end Erase /-! ### diff -/ section Diff variable [DecidableEq α] @[simp] theorem map_diff [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] @[deprecated (since := "2025-04-10")] alias erase_diff_erase_sublist_of_sublist := Sublist.erase_diff_erase_sublist end Diff section Choose variable (p : α → Prop) [DecidablePred p] (l : List α) theorem choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose /-! ### Forall -/ section Forall variable {p q : α → Prop} {l : List α} @[simp] theorem forall_cons (p : α → Prop) (x : α) : ∀ l : List α, Forall p (x :: l) ↔ p x ∧ Forall p l | [] => (and_iff_left_of_imp fun _ ↦ trivial).symm | _ :: _ => Iff.rfl @[simp] theorem forall_append {p : α → Prop} : ∀ {xs ys : List α}, Forall p (xs ++ ys) ↔ Forall p xs ∧ Forall p ys | [] => by simp | _ :: _ => by simp [forall_append, and_assoc] theorem forall_iff_forall_mem : ∀ {l : List α}, Forall p l ↔ ∀ x ∈ l, p x | [] => (iff_true_intro <| forall_mem_nil _).symm | x :: l => by rw [forall_mem_cons, forall_cons, forall_iff_forall_mem] theorem Forall.imp (h : ∀ x, p x → q x) : ∀ {l : List α}, Forall p l → Forall q l | [] => id | x :: l => by simp only [forall_cons, and_imp] rw [← and_imp] exact And.imp (h x) (Forall.imp h) @[simp] theorem forall_map_iff {p : β → Prop} (f : α → β) : Forall p (l.map f) ↔ Forall (p ∘ f) l := by induction l <;> simp [*] instance (p : α → Prop) [DecidablePred p] : DecidablePred (Forall p) := fun _ => decidable_of_iff' _ forall_iff_forall_mem end Forall /-! ### Miscellaneous lemmas -/ theorem get_attach (l : List α) (i) : (l.attach.get i).1 = l.get ⟨i, length_attach (l := l) ▸ i.2⟩ := by simp section Disjoint /-- The images of disjoint lists under a partially defined map are disjoint -/ theorem disjoint_pmap {p : α → Prop} {f : ∀ a : α, p a → β} {s t : List α} (hs : ∀ a ∈ s, p a) (ht : ∀ a ∈ t, p a) (hf : ∀ (a a' : α) (ha : p a) (ha' : p a'), f a ha = f a' ha' → a = a') (h : Disjoint s t) : Disjoint (s.pmap f hs) (t.pmap f ht) := by simp only [Disjoint, mem_pmap] rintro b ⟨a, ha, rfl⟩ ⟨a', ha', ha''⟩ apply h ha rwa [hf a a' (hs a ha) (ht a' ha') ha''.symm] /-- The images of disjoint lists under an injective map are disjoint -/ theorem disjoint_map {f : α → β} {s t : List α} (hf : Function.Injective f) (h : Disjoint s t) : Disjoint (s.map f) (t.map f) := by rw [← pmap_eq_map (fun _ _ ↦ trivial), ← pmap_eq_map (fun _ _ ↦ trivial)] exact disjoint_pmap _ _ (fun _ _ _ _ h' ↦ hf h') h alias Disjoint.map := disjoint_map theorem Disjoint.of_map {f : α → β} {s t : List α} (h : Disjoint (s.map f) (t.map f)) : Disjoint s t := fun _a has hat ↦ h (mem_map_of_mem has) (mem_map_of_mem hat) theorem Disjoint.map_iff {f : α → β} {s t : List α} (hf : Function.Injective f) : Disjoint (s.map f) (t.map f) ↔ Disjoint s t := ⟨fun h ↦ h.of_map, fun h ↦ h.map hf⟩ theorem Perm.disjoint_left {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l₁ l ↔ Disjoint l₂ l := by simp_rw [List.disjoint_left, p.mem_iff] theorem Perm.disjoint_right {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l l₁ ↔ Disjoint l l₂ := by simp_rw [List.disjoint_right, p.mem_iff] @[simp] theorem disjoint_reverse_left {l₁ l₂ : List α} : Disjoint l₁.reverse l₂ ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_left @[simp] theorem disjoint_reverse_right {l₁ l₂ : List α} : Disjoint l₁ l₂.reverse ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_right end Disjoint section lookup variable [BEq α] [LawfulBEq α] lemma lookup_graph (f : α → β) {a : α} {as : List α} (h : a ∈ as) : lookup a (as.map fun x => (x, f x)) = some (f a) := by induction as with | nil => exact (not_mem_nil h).elim | cons a' as ih => by_cases ha : a = a' · simp [ha, lookup_cons] · simpa [lookup_cons, beq_false_of_ne ha] using ih (List.mem_of_ne_of_mem ha h) end lookup section range' @[simp] lemma range'_0 (a b : ℕ) : range' a b 0 = replicate b a := by induction b with | zero => simp | succ b ih => simp [range'_succ, ih, replicate_succ] lemma left_le_of_mem_range' {a b s x : ℕ} (hx : x ∈ List.range' a b s) : a ≤ x := by obtain ⟨i, _, rfl⟩ := List.mem_range'.mp hx exact le_add_right a (s * i) end range' end List
Mathlib/Data/List/Basic.lean
1,932
1,939
/- Copyright (c) 2023 Alex Keizer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex Keizer -/ import Mathlib.Data.Vector.Basic /-! This file establishes a `snoc : Vector α n → α → Vector α (n+1)` operation, that appends a single element to the back of a vector. It provides a collection of lemmas that show how different `Vector` operations reduce when their argument is `snoc xs x`. Also, an alternative, reverse, induction principle is added, that breaks down a vector into `snoc xs x` for its inductive case. Effectively doing induction from right-to-left -/ namespace List namespace Vector variable {α β σ φ : Type*} {n : ℕ} {x : α} {s : σ} (xs : Vector α n) /-- Append a single element to the end of a vector -/ def snoc : Vector α n → α → Vector α (n+1) := fun xs x => append xs (x ::ᵥ Vector.nil) /-! ## Simplification lemmas -/ section Simp variable {y : α} @[simp] theorem snoc_cons : (x ::ᵥ xs).snoc y = x ::ᵥ (xs.snoc y) := rfl @[simp] theorem snoc_nil : (nil.snoc x) = x ::ᵥ nil := rfl
@[simp] theorem reverse_cons : reverse (x ::ᵥ xs) = (reverse xs).snoc x := by cases xs
Mathlib/Data/Vector/Snoc.lean
42
45
/- Copyright (c) 2020 Johan Commelin, Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import Mathlib.Algebra.Field.ZMod import Mathlib.NumberTheory.Padics.PadicIntegers import Mathlib.RingTheory.LocalRing.ResidueField.Defs import Mathlib.RingTheory.ZMod /-! # Relating `ℤ_[p]` to `ZMod (p ^ n)`, aka `ℤ/p^nℤ`. In this file we establish connections between the `p`-adic integers `ℤ_[p]` and the integers modulo powers of `p`, `ℤ/p^nℤ`, implemented as `ZMod (p^n)`. ## Main declarations We show that `ℤ_[p]` has a ring homomorphism to `ℤ/p^nℤ` for each `n`. The case for `n = 1` is handled separately, since it is used in the general construction and we may want to use it without the `^1` getting in the way. * `PadicInt.toZMod`: ring homomorphism to `ℤ/pℤ`, implemented as `ZMod p`. * `PadicInt.toZModPow`: ring homomorphism to `ℤ/p^nℤ`, implemented as `ZMod (p^n)`. * `PadicInt.ker_toZMod` / `PadicInt.ker_toZModPow`: the kernels of these maps are the ideals generated by `p^n` * `PadicInt.residueField` shows that the residue field of `ℤ_[p]` is isomorhic to ``ℤ/pℤ`. We also establish the universal property of `ℤ_[p]` as a projective limit. Given a family of compatible ring homomorphisms `f_k : R → ℤ/p^nℤ`, there is a unique limit `R → ℤ_[p]` * `PadicInt.lift`: the limit function * `PadicInt.lift_spec` / `PadicInt.lift_unique`: the universal property ## Implementation notes The constructions of the ring homomorphisms go through an auxiliary constructor `PadicInt.toZModHom`, which removes some boilerplate code. -/ noncomputable section open Nat IsLocalRing Padic namespace PadicInt variable {p : ℕ} [hp_prime : Fact p.Prime] section RingHoms /-! ### Ring homomorphisms to `ZMod p` and `ZMod (p ^ n)` -/ variable (p) (r : ℚ) /-- `modPart p r` is an integer that satisfies `‖(r - modPart p r : ℚ_[p])‖ < 1` when `‖(r : ℚ_[p])‖ ≤ 1`, see `PadicInt.norm_sub_modPart`. It is the unique non-negative integer that is `< p` with this property. (Note that this definition assumes `r : ℚ`. See `PadicInt.zmodRepr` for a version that takes values in `ℕ` and works for arbitrary `x : ℤ_[p]`.) -/ def modPart : ℤ := r.num * gcdA r.den p % p variable {p} theorem modPart_lt_p : modPart p r < p := by convert Int.emod_lt_abs _ _ · simp · exact mod_cast hp_prime.1.ne_zero theorem modPart_nonneg : 0 ≤ modPart p r := Int.emod_nonneg _ <| mod_cast hp_prime.1.ne_zero theorem isUnit_den (r : ℚ) (h : ‖(r : ℚ_[p])‖ ≤ 1) : IsUnit (r.den : ℤ_[p]) := by rw [isUnit_iff] apply le_antisymm (r.den : ℤ_[p]).2 rw [← not_lt, coe_natCast] intro norm_denom_lt have hr : ‖(r * r.den : ℚ_[p])‖ = ‖(r.num : ℚ_[p])‖ := by congr rw_mod_cast [@Rat.mul_den_eq_num r] rw [padicNormE.mul] at hr have key : ‖(r.num : ℚ_[p])‖ < 1 := by calc _ = _ := hr.symm _ < 1 * 1 := mul_lt_mul' h norm_denom_lt (norm_nonneg _) zero_lt_one _ = 1 := mul_one 1 have : ↑p ∣ r.num ∧ (p : ℤ) ∣ r.den := by simp only [← norm_int_lt_one_iff_dvd, ← padic_norm_e_of_padicInt] exact ⟨key, norm_denom_lt⟩ apply hp_prime.1.not_dvd_one rwa [← r.reduced.gcd_eq_one, Nat.dvd_gcd_iff, ← Int.natCast_dvd, ← Int.natCast_dvd_natCast] theorem norm_sub_modPart_aux (r : ℚ) (h : ‖(r : ℚ_[p])‖ ≤ 1) : ↑p ∣ r.num - r.num * r.den.gcdA p % p * ↑r.den := by rw [← ZMod.intCast_zmod_eq_zero_iff_dvd] simp only [Int.cast_natCast, ZMod.natCast_mod, Int.cast_mul, Int.cast_sub] have := congr_arg (fun x => x % p : ℤ → ZMod p) (gcd_eq_gcd_ab r.den p) simp only [Int.cast_natCast, CharP.cast_eq_zero, EuclideanDomain.mod_zero, Int.cast_add, Int.cast_mul, zero_mul, add_zero] at this push_cast rw [mul_right_comm, mul_assoc, ← this] suffices rdcp : r.den.Coprime p by rw [rdcp.gcd_eq_one] simp only [mul_one, cast_one, sub_self] apply Coprime.symm apply (coprime_or_dvd_of_prime hp_prime.1 _).resolve_right rw [← Int.natCast_dvd_natCast, ← norm_int_lt_one_iff_dvd, not_lt] apply ge_of_eq rw [← isUnit_iff] exact isUnit_den r h theorem norm_sub_modPart (h : ‖(r : ℚ_[p])‖ ≤ 1) : ‖(⟨r, h⟩ - modPart p r : ℤ_[p])‖ < 1 := by let n := modPart p r rw [norm_lt_one_iff_dvd, ← (isUnit_den r h).dvd_mul_right] suffices ↑p ∣ r.num - n * r.den by convert (Int.castRingHom ℤ_[p]).map_dvd this simp only [n, sub_mul, Int.cast_natCast, eq_intCast, Int.cast_mul, sub_left_inj, Int.cast_sub] apply Subtype.coe_injective simp only [coe_mul, Subtype.coe_mk, coe_natCast] rw_mod_cast [@Rat.mul_den_eq_num r] rfl exact norm_sub_modPart_aux r h theorem exists_mem_range_of_norm_rat_le_one (h : ‖(r : ℚ_[p])‖ ≤ 1) : ∃ n : ℤ, 0 ≤ n ∧ n < p ∧ ‖(⟨r, h⟩ - n : ℤ_[p])‖ < 1 := ⟨modPart p r, modPart_nonneg _, modPart_lt_p _, norm_sub_modPart _ h⟩ theorem zmod_congr_of_sub_mem_span_aux (n : ℕ) (x : ℤ_[p]) (a b : ℤ) (ha : x - a ∈ (Ideal.span {(p : ℤ_[p]) ^ n})) (hb : x - b ∈ (Ideal.span {(p : ℤ_[p]) ^ n})) : (a : ZMod (p ^ n)) = b := by rw [Ideal.mem_span_singleton] at ha hb rw [← sub_eq_zero, ← Int.cast_sub, ZMod.intCast_zmod_eq_zero_iff_dvd, Int.natCast_pow] rw [← dvd_neg, neg_sub] at ha have := dvd_add ha hb rwa [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, ← sub_eq_add_neg, ← Int.cast_sub, pow_p_dvd_int_iff] at this theorem zmod_congr_of_sub_mem_span (n : ℕ) (x : ℤ_[p]) (a b : ℕ) (ha : x - a ∈ (Ideal.span {(p : ℤ_[p]) ^ n})) (hb : x - b ∈ (Ideal.span {(p : ℤ_[p]) ^ n})) : (a : ZMod (p ^ n)) = b := by simpa using zmod_congr_of_sub_mem_span_aux n x a b ha hb theorem zmod_congr_of_sub_mem_max_ideal (x : ℤ_[p]) (m n : ℕ) (hm : x - m ∈ maximalIdeal ℤ_[p]) (hn : x - n ∈ maximalIdeal ℤ_[p]) : (m : ZMod p) = n := by rw [maximalIdeal_eq_span_p] at hm hn have := zmod_congr_of_sub_mem_span_aux 1 x m n simp only [pow_one] at this specialize this hm hn apply_fun ZMod.castHom (show p ∣ p ^ 1 by rw [pow_one]) (ZMod p) at this simp only [map_intCast] at this simpa only [Int.cast_natCast] using this variable (x : ℤ_[p]) theorem exists_mem_range : ∃ n : ℕ, n < p ∧ x - n ∈ maximalIdeal ℤ_[p] := by simp only [maximalIdeal_eq_span_p, Ideal.mem_span_singleton, ← norm_lt_one_iff_dvd] obtain ⟨r, hr⟩ := rat_dense p (x : ℚ_[p]) zero_lt_one have H : ‖(r : ℚ_[p])‖ ≤ 1 := by rw [norm_sub_rev] at hr calc _ = ‖(r : ℚ_[p]) - x + x‖ := by ring_nf _ ≤ _ := padicNormE.nonarchimedean _ _ _ ≤ _ := max_le (le_of_lt hr) x.2 obtain ⟨n, hzn, hnp, hn⟩ := exists_mem_range_of_norm_rat_le_one r H lift n to ℕ using hzn use n constructor · exact mod_cast hnp simp only [norm_def, coe_sub, Subtype.coe_mk, coe_natCast] at hn ⊢ rw [show (x - n : ℚ_[p]) = x - r + (r - n) by ring] apply lt_of_le_of_lt (padicNormE.nonarchimedean _ _) apply max_lt hr simpa using hn theorem existsUnique_mem_range : ∃! n : ℕ, n < p ∧ x - n ∈ maximalIdeal ℤ_[p] := by obtain ⟨n, hn₁, hn₂⟩ := exists_mem_range x use n, ⟨hn₁, hn₂⟩, fun m ⟨hm₁, hm₂⟩ ↦ ?_ have := (zmod_congr_of_sub_mem_max_ideal x n m hn₂ hm₂).symm rwa [ZMod.natCast_eq_natCast_iff, ModEq, mod_eq_of_lt hn₁, mod_eq_of_lt hm₁] at this @[deprecated (since := "2024-12-17")] alias exists_unique_mem_range := existsUnique_mem_range /-- `zmodRepr x` is the unique natural number smaller than `p` satisfying `‖(x - zmodRepr x : ℤ_[p])‖ < 1`. -/ def zmodRepr : ℕ := Classical.choose (existsUnique_mem_range x).exists theorem zmodRepr_spec : zmodRepr x < p ∧ x - zmodRepr x ∈ maximalIdeal ℤ_[p] := Classical.choose_spec (existsUnique_mem_range x).exists theorem zmodRepr_unique (y : ℕ) (hy₁ : y < p) (hy₂ : x - y ∈ maximalIdeal ℤ_[p]) : y = zmodRepr x := have h := (Classical.choose_spec (existsUnique_mem_range x)).right (h y ⟨hy₁, hy₂⟩).trans (h (zmodRepr x) (zmodRepr_spec x)).symm theorem zmodRepr_lt_p : zmodRepr x < p := (zmodRepr_spec _).1 theorem sub_zmodRepr_mem : x - zmodRepr x ∈ maximalIdeal ℤ_[p] := (zmodRepr_spec _).2 /-- `toZModHom` is an auxiliary constructor for creating ring homs from `ℤ_[p]` to `ZMod v`. -/ def toZModHom (v : ℕ) (f : ℤ_[p] → ℕ) (f_spec : ∀ x, x - f x ∈ (Ideal.span {↑v} : Ideal ℤ_[p])) (f_congr : ∀ (x : ℤ_[p]) (a b : ℕ), x - a ∈ (Ideal.span {↑v} : Ideal ℤ_[p]) → x - b ∈ (Ideal.span {↑v} : Ideal ℤ_[p]) → (a : ZMod v) = b) : ℤ_[p] →+* ZMod v where toFun x := f x map_zero' := by rw [f_congr (0 : ℤ_[p]) _ 0, cast_zero] · exact f_spec _ · simp only [sub_zero, cast_zero, Submodule.zero_mem] map_one' := by rw [f_congr (1 : ℤ_[p]) _ 1, cast_one] · exact f_spec _ · simp only [sub_self, cast_one, Submodule.zero_mem] map_add' := by intro x y rw [f_congr (x + y) _ (f x + f y), cast_add] · exact f_spec _ · convert Ideal.add_mem _ (f_spec x) (f_spec y) using 1 rw [cast_add] ring map_mul' := by intro x y rw [f_congr (x * y) _ (f x * f y), cast_mul] · exact f_spec _ · let I : Ideal ℤ_[p] := Ideal.span {↑v} convert I.add_mem (I.mul_mem_left x (f_spec y)) (I.mul_mem_right ↑(f y) (f_spec x)) using 1 rw [cast_mul] ring /-- `toZMod` is a ring hom from `ℤ_[p]` to `ZMod p`, with the equality `toZMod x = (zmodRepr x : ZMod p)`. -/ def toZMod : ℤ_[p] →+* ZMod p := toZModHom p zmodRepr (by rw [← maximalIdeal_eq_span_p] exact sub_zmodRepr_mem) (by rw [← maximalIdeal_eq_span_p] exact zmod_congr_of_sub_mem_max_ideal) /-- `z - (toZMod z : ℤ_[p])` is contained in the maximal ideal of `ℤ_[p]`, for every `z : ℤ_[p]`. The coercion from `ZMod p` to `ℤ_[p]` is `ZMod.cast`, which coerces `ZMod p` into arbitrary rings. This is unfortunate, but a consequence of the fact that we allow `ZMod p` to coerce to rings of arbitrary characteristic, instead of only rings of characteristic `p`. This coercion is only a ring homomorphism if it coerces into a ring whose characteristic divides `p`. While this is not the case here we can still make use of the coercion. -/ theorem toZMod_spec : x - (ZMod.cast (toZMod x) : ℤ_[p]) ∈ maximalIdeal ℤ_[p] := by convert sub_zmodRepr_mem x using 2 dsimp [toZMod, toZModHom] rcases Nat.exists_eq_add_of_lt hp_prime.1.pos with ⟨p', rfl⟩ change ↑((_ : ZMod (0 + p' + 1)).val) = (_ : ℤ_[0 + p' + 1]) rw [Nat.cast_inj] apply mod_eq_of_lt simpa only [zero_add] using zmodRepr_lt_p x theorem ker_toZMod : RingHom.ker (toZMod : ℤ_[p] →+* ZMod p) = maximalIdeal ℤ_[p] := by ext x rw [RingHom.mem_ker] constructor · intro h simpa only [h, ZMod.cast_zero, sub_zero] using toZMod_spec x · intro h rw [← sub_zero x] at h dsimp [toZMod, toZModHom] convert zmod_congr_of_sub_mem_max_ideal x _ 0 _ h · norm_cast · apply sub_zmodRepr_mem /-- The equivalence between the residue field of the `p`-adic integers and `ℤ/pℤ` -/ def residueField : IsLocalRing.ResidueField ℤ_[p] ≃+* ZMod p := (Ideal.quotEquivOfEq PadicInt.ker_toZMod.symm).trans <| RingHom.quotientKerEquivOfSurjective (ZMod.ringHom_surjective PadicInt.toZMod) open scoped Classical in /-- `appr n x` gives a value `v : ℕ` such that `x` and `↑v : ℤ_p` are congruent mod `p^n`. See `appr_spec`. -/ noncomputable def appr : ℤ_[p] → ℕ → ℕ | _x, 0 => 0 | x, n + 1 => let y := x - appr x n if hy : y = 0 then appr x n else let u := (unitCoeff hy : ℤ_[p]) appr x n + p ^ n * (toZMod ((u * (p : ℤ_[p]) ^ (y.valuation - n : ℤ).natAbs) : ℤ_[p])).val theorem appr_lt (x : ℤ_[p]) (n : ℕ) : x.appr n < p ^ n := by induction n generalizing x with | zero => simp only [appr, zero_eq, _root_.pow_zero, zero_lt_one] | succ n ih => simp only [appr, map_natCast, ZMod.natCast_self, RingHom.map_pow, Int.natAbs, RingHom.map_mul] have hp : p ^ n < p ^ (n + 1) := by apply Nat.pow_lt_pow_right hp_prime.1.one_lt n.lt_add_one split_ifs with h · apply lt_trans (ih _) hp · calc _ < p ^ n + p ^ n * (p - 1) := ?_ _ = p ^ (n + 1) := ?_ · apply add_lt_add_of_lt_of_le (ih _) apply Nat.mul_le_mul_left apply le_pred_of_lt apply ZMod.val_lt · rw [mul_tsub, mul_one, ← _root_.pow_succ] apply add_tsub_cancel_of_le (le_of_lt hp) theorem appr_mono (x : ℤ_[p]) : Monotone x.appr := by apply monotone_nat_of_le_succ intro n dsimp [appr] split_ifs; · rfl apply Nat.le_add_right theorem dvd_appr_sub_appr (x : ℤ_[p]) (m n : ℕ) (h : m ≤ n) : p ^ m ∣ x.appr n - x.appr m := by obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le h; clear h induction k with | zero => simp only [zero_eq, add_zero, le_refl, tsub_eq_zero_of_le, ne_eq, Nat.isUnit_iff, dvd_zero] | succ k ih => rw [← add_assoc] dsimp [appr] split_ifs with h · exact ih rw [add_comm, add_tsub_assoc_of_le (appr_mono _ (Nat.le_add_right m k))] apply dvd_add _ ih apply dvd_mul_of_dvd_left apply pow_dvd_pow _ (Nat.le_add_right m k) theorem appr_spec (n : ℕ) : ∀ x : ℤ_[p], x - appr x n ∈ Ideal.span {(p : ℤ_[p]) ^ n} := by simp only [Ideal.mem_span_singleton] induction n with | zero => simp only [zero_eq, _root_.pow_zero, isUnit_one, IsUnit.dvd, forall_const] | succ n ih => intro x dsimp only [appr] split_ifs with h · rw [h] apply dvd_zero push_cast rw [sub_add_eq_sub_sub] obtain ⟨c, hc⟩ := ih x simp only [map_natCast, ZMod.natCast_self, RingHom.map_pow, RingHom.map_mul, ZMod.natCast_val] have hc' : c ≠ 0 := by rintro rfl simp only [mul_zero] at hc contradiction conv_rhs => congr simp only [hc] rw [show (x - (appr x n : ℤ_[p])).valuation = ((p : ℤ_[p]) ^ n * c).valuation by rw [hc]] rw [valuation_p_pow_mul _ _ hc', Nat.cast_add, add_sub_cancel_left, _root_.pow_succ, ← mul_sub] apply mul_dvd_mul_left obtain hc0 | hc0 := eq_or_ne c.valuation 0 · simp only [hc0, mul_one, _root_.pow_zero, Nat.cast_zero, Int.natAbs_zero] rw [mul_comm, unitCoeff_spec h] at hc suffices c = unitCoeff h by rw [← this, ← Ideal.mem_span_singleton, ← maximalIdeal_eq_span_p] apply toZMod_spec lift c to ℤ_[p]ˣ using by simp [isUnit_iff, norm_eq_zpow_neg_valuation hc', hc0] rw [IsDiscreteValuationRing.unit_mul_pow_congr_unit _ _ _ _ _ hc] exact irreducible_p · simp only [Int.natAbs_natCast, zero_pow hc0, sub_zero, ZMod.cast_zero, mul_zero] rw [unitCoeff_spec hc'] exact (dvd_pow_self (p : ℤ_[p]) hc0).mul_left _ /-- A ring hom from `ℤ_[p]` to `ZMod (p^n)`, with underlying function `PadicInt.appr n`. -/ def toZModPow (n : ℕ) : ℤ_[p] →+* ZMod (p ^ n) := toZModHom (p ^ n) (fun x => appr x n) (by intros rw [Nat.cast_pow] exact appr_spec n _) (by intro x a b ha hb apply zmod_congr_of_sub_mem_span n x a b · simpa using ha · simpa using hb) theorem ker_toZModPow (n : ℕ) : RingHom.ker (toZModPow n : ℤ_[p] →+* ZMod (p ^ n)) = Ideal.span {(p : ℤ_[p]) ^ n} := by ext x rw [RingHom.mem_ker] constructor · intro h suffices x.appr n = 0 by convert appr_spec n x simp only [this, sub_zero, cast_zero] dsimp [toZModPow, toZModHom] at h rw [ZMod.natCast_zmod_eq_zero_iff_dvd] at h apply eq_zero_of_dvd_of_lt h (appr_lt _ _) · intro h rw [← sub_zero x] at h dsimp [toZModPow, toZModHom] rw [zmod_congr_of_sub_mem_span n x _ 0 _ h, cast_zero] apply appr_spec -- This is not a simp lemma; simp can't match the LHS. theorem zmod_cast_comp_toZModPow (m n : ℕ) (h : m ≤ n) : (ZMod.castHom (pow_dvd_pow p h) (ZMod (p ^ m))).comp (@toZModPow p _ n) = @toZModPow p _ m := by apply ZMod.ringHom_eq_of_ker_eq ext x rw [RingHom.mem_ker, RingHom.mem_ker] simp only [Function.comp_apply, ZMod.castHom_apply, RingHom.coe_comp] simp only [toZModPow, toZModHom, RingHom.coe_mk] dsimp rw [ZMod.cast_natCast (pow_dvd_pow p h), zmod_congr_of_sub_mem_span m (x.appr n) (x.appr n) (x.appr m)] · rw [sub_self] apply Ideal.zero_mem _ · rw [Ideal.mem_span_singleton] rcases dvd_appr_sub_appr x m n h with ⟨c, hc⟩ use c rw [← Nat.cast_sub (appr_mono _ h), hc, Nat.cast_mul, Nat.cast_pow] @[simp] theorem cast_toZModPow (m n : ℕ) (h : m ≤ n) (x : ℤ_[p]) : ZMod.cast (toZModPow n x) = toZModPow m x := by rw [← zmod_cast_comp_toZModPow _ _ h] rfl theorem denseRange_natCast : DenseRange (Nat.cast : ℕ → ℤ_[p]) := by intro x rw [Metric.mem_closure_range_iff] intro ε hε obtain ⟨n, hn⟩ := exists_pow_neg_lt p hε use x.appr n rw [dist_eq_norm] apply lt_of_le_of_lt _ hn rw [norm_le_pow_iff_mem_span_pow] apply appr_spec theorem denseRange_intCast : DenseRange (Int.cast : ℤ → ℤ_[p]) := by intro x refine DenseRange.induction_on denseRange_natCast x ?_ ?_ · exact isClosed_closure · intro a apply subset_closure exact Set.mem_range_self _ end RingHoms section lift /-! ### Universal property as projective limit -/ open CauSeq PadicSeq variable {R : Type*} [NonAssocSemiring R] {p : Nat} (f : ∀ k : ℕ, R →+* ZMod (p ^ k)) /-- Given a family of ring homs `f : Π n : ℕ, R →+* ZMod (p ^ n)`, `nthHom f r` is an integer-valued sequence whose `n`th value is the unique integer `k` such that `0 ≤ k < p ^ n` and `f n r = (k : ZMod (p ^ n))`. -/ def nthHom (r : R) : ℕ → ℤ := fun n => (f n r : ZMod (p ^ n)).val @[simp] theorem nthHom_zero : nthHom f 0 = 0 := by simp +unfoldPartialApp [nthHom] rfl variable {f} variable [hp_prime : Fact p.Prime] section variable (f_compat : ∀ (k1 k2) (hk : k1 ≤ k2), (ZMod.castHom (pow_dvd_pow p hk) _).comp (f k2) = f k1) include f_compat theorem pow_dvd_nthHom_sub (r : R) (i j : ℕ) (h : i ≤ j) : (p : ℤ) ^ i ∣ nthHom f r j - nthHom f r i := by specialize f_compat i j h rw [← Int.natCast_pow, ← ZMod.intCast_zmod_eq_zero_iff_dvd, Int.cast_sub] dsimp [nthHom] rw [← f_compat, RingHom.comp_apply] simp only [ZMod.cast_id, ZMod.castHom_apply, sub_self, ZMod.natCast_val, ZMod.intCast_cast] theorem isCauSeq_nthHom (r : R) : IsCauSeq (padicNorm p) fun n => nthHom f r n := by intro ε hε
obtain ⟨k, hk⟩ : ∃ k : ℕ, (p : ℚ) ^ (-((k : ℕ) : ℤ)) < ε := exists_pow_neg_lt_rat p hε use k intro j hj
Mathlib/NumberTheory/Padics/RingHoms.lean
498
500
/- 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, Floris van Doorn -/ import Mathlib.Data.Countable.Small import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.Fintype.Powerset import Mathlib.Data.Nat.Cast.Order.Basic import Mathlib.Data.Set.Countable import Mathlib.Logic.Equiv.Fin.Basic import Mathlib.Logic.Small.Set import Mathlib.Logic.UnivLE import Mathlib.SetTheory.Cardinal.Order /-! # Basic results on cardinal numbers We provide a collection of basic results on cardinal numbers, in particular focussing on finite/countable/small types and sets. ## Main definitions * `Cardinal.powerlt a b` or `a ^< b` is defined as the supremum of `a ^ c` for `c < b`. ## References * <https://en.wikipedia.org/wiki/Cardinal_number> ## Tags cardinal number, cardinal arithmetic, cardinal exponentiation, aleph, Cantor's theorem, König's theorem, Konig's theorem -/ assert_not_exists Field open List (Vector) open Function Order Set noncomputable section universe u v w v' w' variable {α β : Type u} namespace Cardinal /-! ### Lifting cardinals to a higher universe -/ @[simp] lemma mk_preimage_down {s : Set α} : #(ULift.down.{v} ⁻¹' s) = lift.{v} (#s) := by rw [← mk_uLift, Cardinal.eq] constructor let f : ULift.down ⁻¹' s → ULift s := fun x ↦ ULift.up (restrictPreimage s ULift.down x) have : Function.Bijective f := ULift.up_bijective.comp (restrictPreimage_bijective _ (ULift.down_bijective)) exact Equiv.ofBijective f this -- `simp` can't figure out universe levels: normal form is `lift_mk_shrink'`. theorem lift_mk_shrink (α : Type u) [Small.{v} α] : Cardinal.lift.{max u w} #(Shrink.{v} α) = Cardinal.lift.{max v w} #α := lift_mk_eq.2 ⟨(equivShrink α).symm⟩ @[simp] theorem lift_mk_shrink' (α : Type u) [Small.{v} α] : Cardinal.lift.{u} #(Shrink.{v} α) = Cardinal.lift.{v} #α := lift_mk_shrink.{u, v, 0} α @[simp] theorem lift_mk_shrink'' (α : Type max u v) [Small.{v} α] : Cardinal.lift.{u} #(Shrink.{v} α) = #α := by rw [← lift_umax, lift_mk_shrink.{max u v, v, 0} α, ← lift_umax, lift_id] theorem prod_eq_of_fintype {α : Type u} [h : Fintype α] (f : α → Cardinal.{v}) : prod f = Cardinal.lift.{u} (∏ i, f i) := by revert f refine Fintype.induction_empty_option ?_ ?_ ?_ α (h_fintype := h) · intro α β hβ e h f letI := Fintype.ofEquiv β e.symm rw [← e.prod_comp f, ← h] exact mk_congr (e.piCongrLeft _).symm · intro f rw [Fintype.univ_pempty, Finset.prod_empty, lift_one, Cardinal.prod, mk_eq_one] · intro α hα h f rw [Cardinal.prod, mk_congr Equiv.piOptionEquivProd, mk_prod, lift_umax.{v, u}, mk_out, ← Cardinal.prod, lift_prod, Fintype.prod_option, lift_mul, ← h fun a => f (some a)] simp only [lift_id] /-! ### Basic cardinals -/ theorem le_one_iff_subsingleton {α : Type u} : #α ≤ 1 ↔ Subsingleton α := ⟨fun ⟨f⟩ => ⟨fun _ _ => f.injective (Subsingleton.elim _ _)⟩, fun ⟨h⟩ => ⟨fun _ => ULift.up 0, fun _ _ _ => h _ _⟩⟩ @[simp] theorem mk_le_one_iff_set_subsingleton {s : Set α} : #s ≤ 1 ↔ s.Subsingleton := le_one_iff_subsingleton.trans s.subsingleton_coe alias ⟨_, _root_.Set.Subsingleton.cardinalMk_le_one⟩ := mk_le_one_iff_set_subsingleton @[deprecated (since := "2024-11-10")] alias _root_.Set.Subsingleton.cardinal_mk_le_one := Set.Subsingleton.cardinalMk_le_one private theorem cast_succ (n : ℕ) : ((n + 1 : ℕ) : Cardinal.{u}) = n + 1 := by change #(ULift.{u} _) = #(ULift.{u} _) + 1 rw [← mk_option] simp /-! ### Order properties -/ theorem one_lt_iff_nontrivial {α : Type u} : 1 < #α ↔ Nontrivial α := by rw [← not_le, le_one_iff_subsingleton, ← not_nontrivial_iff_subsingleton, Classical.not_not] lemma sInf_eq_zero_iff {s : Set Cardinal} : sInf s = 0 ↔ s = ∅ ∨ ∃ a ∈ s, a = 0 := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · rcases s.eq_empty_or_nonempty with rfl | hne · exact Or.inl rfl · exact Or.inr ⟨sInf s, csInf_mem hne, h⟩ · rcases h with rfl | ⟨a, ha, rfl⟩ · exact Cardinal.sInf_empty · exact eq_bot_iff.2 (csInf_le' ha) lemma iInf_eq_zero_iff {ι : Sort*} {f : ι → Cardinal} : (⨅ i, f i) = 0 ↔ IsEmpty ι ∨ ∃ i, f i = 0 := by simp [iInf, sInf_eq_zero_iff] /-- A variant of `ciSup_of_empty` but with `0` on the RHS for convenience -/ protected theorem iSup_of_empty {ι} (f : ι → Cardinal) [IsEmpty ι] : iSup f = 0 := ciSup_of_empty f @[simp] theorem lift_sInf (s : Set Cardinal) : lift.{u, v} (sInf s) = sInf (lift.{u, v} '' s) := by rcases eq_empty_or_nonempty s with (rfl | hs) · simp · exact lift_monotone.map_csInf hs @[simp] theorem lift_iInf {ι} (f : ι → Cardinal) : lift.{u, v} (iInf f) = ⨅ i, lift.{u, v} (f i) := by unfold iInf convert lift_sInf (range f) simp_rw [← comp_apply (f := lift), range_comp] end Cardinal /-! ### Small sets of cardinals -/ namespace Cardinal instance small_Iic (a : Cardinal.{u}) : Small.{u} (Iic a) := by rw [← mk_out a] apply @small_of_surjective (Set a.out) (Iic #a.out) _ fun x => ⟨#x, mk_set_le x⟩ rintro ⟨x, hx⟩ simpa using le_mk_iff_exists_set.1 hx instance small_Iio (a : Cardinal.{u}) : Small.{u} (Iio a) := small_subset Iio_subset_Iic_self instance small_Icc (a b : Cardinal.{u}) : Small.{u} (Icc a b) := small_subset Icc_subset_Iic_self instance small_Ico (a b : Cardinal.{u}) : Small.{u} (Ico a b) := small_subset Ico_subset_Iio_self instance small_Ioc (a b : Cardinal.{u}) : Small.{u} (Ioc a b) := small_subset Ioc_subset_Iic_self instance small_Ioo (a b : Cardinal.{u}) : Small.{u} (Ioo a b) := small_subset Ioo_subset_Iio_self /-- A set of cardinals is bounded above iff it's small, i.e. it corresponds to a usual ZFC set. -/ theorem bddAbove_iff_small {s : Set Cardinal.{u}} : BddAbove s ↔ Small.{u} s := ⟨fun ⟨a, ha⟩ => @small_subset _ (Iic a) s (fun _ h => ha h) _, by rintro ⟨ι, ⟨e⟩⟩ use sum.{u, u} fun x ↦ e.symm x intro a ha simpa using le_sum (fun x ↦ e.symm x) (e ⟨a, ha⟩)⟩ theorem bddAbove_of_small (s : Set Cardinal.{u}) [h : Small.{u} s] : BddAbove s := bddAbove_iff_small.2 h theorem bddAbove_range {ι : Type*} [Small.{u} ι] (f : ι → Cardinal.{u}) : BddAbove (Set.range f) := bddAbove_of_small _ theorem bddAbove_image (f : Cardinal.{u} → Cardinal.{max u v}) {s : Set Cardinal.{u}} (hs : BddAbove s) : BddAbove (f '' s) := by rw [bddAbove_iff_small] at hs ⊢ exact small_lift _ theorem bddAbove_range_comp {ι : Type u} {f : ι → Cardinal.{v}} (hf : BddAbove (range f)) (g : Cardinal.{v} → Cardinal.{max v w}) : BddAbove (range (g ∘ f)) := by rw [range_comp] exact bddAbove_image g hf /-- The type of cardinals in universe `u` is not `Small.{u}`. This is a version of the Burali-Forti paradox. -/ theorem _root_.not_small_cardinal : ¬ Small.{u} Cardinal.{max u v} := by intro h have := small_lift.{_, v} Cardinal.{max u v} rw [← small_univ_iff, ← bddAbove_iff_small] at this exact not_bddAbove_univ this instance uncountable : Uncountable Cardinal.{u} := Uncountable.of_not_small not_small_cardinal.{u} /-! ### Bounds on suprema -/ theorem sum_le_iSup_lift {ι : Type u} (f : ι → Cardinal.{max u v}) : sum f ≤ Cardinal.lift #ι * iSup f := by rw [← (iSup f).lift_id, ← lift_umax, lift_umax.{max u v, u}, ← sum_const] exact sum_le_sum _ _ (le_ciSup <| bddAbove_of_small _) theorem sum_le_iSup {ι : Type u} (f : ι → Cardinal.{u}) : sum f ≤ #ι * iSup f := by rw [← lift_id #ι] exact sum_le_iSup_lift f /-- The lift of a supremum is the supremum of the lifts. -/ theorem lift_sSup {s : Set Cardinal} (hs : BddAbove s) : lift.{u} (sSup s) = sSup (lift.{u} '' s) := by apply ((le_csSup_iff' (bddAbove_image.{_,u} _ hs)).2 fun c hc => _).antisymm (csSup_le' _) · intro c hc by_contra h obtain ⟨d, rfl⟩ := Cardinal.mem_range_lift_of_le (not_le.1 h).le simp_rw [lift_le] at h hc rw [csSup_le_iff' hs] at h exact h fun a ha => lift_le.1 <| hc (mem_image_of_mem _ ha) · rintro i ⟨j, hj, rfl⟩ exact lift_le.2 (le_csSup hs hj) /-- The lift of a supremum is the supremum of the lifts. -/ theorem lift_iSup {ι : Type v} {f : ι → Cardinal.{w}} (hf : BddAbove (range f)) : lift.{u} (iSup f) = ⨆ i, lift.{u} (f i) := by rw [iSup, iSup, lift_sSup hf, ← range_comp] simp [Function.comp_def] /-- To prove that the lift of a supremum is bounded by some cardinal `t`, it suffices to show that the lift of each cardinal is bounded by `t`. -/ theorem lift_iSup_le {ι : Type v} {f : ι → Cardinal.{w}} {t : Cardinal} (hf : BddAbove (range f)) (w : ∀ i, lift.{u} (f i) ≤ t) : lift.{u} (iSup f) ≤ t := by rw [lift_iSup hf] exact ciSup_le' w @[simp] theorem lift_iSup_le_iff {ι : Type v} {f : ι → Cardinal.{w}} (hf : BddAbove (range f)) {t : Cardinal} : lift.{u} (iSup f) ≤ t ↔ ∀ i, lift.{u} (f i) ≤ t := by rw [lift_iSup hf] exact ciSup_le_iff' (bddAbove_range_comp.{_,_,u} hf _) /-- To prove an inequality between the lifts to a common universe of two different supremums, it suffices to show that the lift of each cardinal from the smaller supremum if bounded by the lift of some cardinal from the larger supremum. -/ theorem lift_iSup_le_lift_iSup {ι : Type v} {ι' : Type v'} {f : ι → Cardinal.{w}} {f' : ι' → Cardinal.{w'}} (hf : BddAbove (range f)) (hf' : BddAbove (range f')) {g : ι → ι'} (h : ∀ i, lift.{w'} (f i) ≤ lift.{w} (f' (g i))) : lift.{w'} (iSup f) ≤ lift.{w} (iSup f') := by rw [lift_iSup hf, lift_iSup hf'] exact ciSup_mono' (bddAbove_range_comp.{_,_,w} hf' _) fun i => ⟨_, h i⟩ /-- A variant of `lift_iSup_le_lift_iSup` with universes specialized via `w = v` and `w' = v'`. This is sometimes necessary to avoid universe unification issues. -/ theorem lift_iSup_le_lift_iSup' {ι : Type v} {ι' : Type v'} {f : ι → Cardinal.{v}} {f' : ι' → Cardinal.{v'}} (hf : BddAbove (range f)) (hf' : BddAbove (range f')) (g : ι → ι') (h : ∀ i, lift.{v'} (f i) ≤ lift.{v} (f' (g i))) : lift.{v'} (iSup f) ≤ lift.{v} (iSup f') := lift_iSup_le_lift_iSup hf hf' h /-! ### Properties about the cast from `ℕ` -/ theorem mk_finset_of_fintype [Fintype α] : #(Finset α) = 2 ^ Fintype.card α := by simp [Pow.pow] @[norm_cast] theorem nat_succ (n : ℕ) : (n.succ : Cardinal) = succ ↑n := by rw [Nat.cast_succ] refine (add_one_le_succ _).antisymm (succ_le_of_lt ?_) rw [← Nat.cast_succ] exact Nat.cast_lt.2 (Nat.lt_succ_self _) lemma succ_natCast (n : ℕ) : Order.succ (n : Cardinal) = n + 1 := by rw [← Cardinal.nat_succ] norm_cast lemma natCast_add_one_le_iff {n : ℕ} {c : Cardinal} : n + 1 ≤ c ↔ n < c := by rw [← Order.succ_le_iff, Cardinal.succ_natCast] lemma two_le_iff_one_lt {c : Cardinal} : 2 ≤ c ↔ 1 < c := by convert natCast_add_one_le_iff norm_cast @[simp] theorem succ_zero : succ (0 : Cardinal) = 1 := by norm_cast -- This works generally to prove inequalities between numeric cardinals. theorem one_lt_two : (1 : Cardinal) < 2 := by norm_cast theorem exists_finset_le_card (α : Type*) (n : ℕ) (h : n ≤ #α) : ∃ s : Finset α, n ≤ s.card := by obtain hα|hα := finite_or_infinite α · let hα := Fintype.ofFinite α use Finset.univ simpa only [mk_fintype, Nat.cast_le] using h · obtain ⟨s, hs⟩ := Infinite.exists_subset_card_eq α n exact ⟨s, hs.ge⟩ theorem card_le_of {α : Type u} {n : ℕ} (H : ∀ s : Finset α, s.card ≤ n) : #α ≤ n := by contrapose! H apply exists_finset_le_card α (n+1) simpa only [nat_succ, succ_le_iff] using H theorem cantor' (a) {b : Cardinal} (hb : 1 < b) : a < b ^ a := by rw [← succ_le_iff, (by norm_cast : succ (1 : Cardinal) = 2)] at hb exact (cantor a).trans_le (power_le_power_right hb) theorem one_le_iff_pos {c : Cardinal} : 1 ≤ c ↔ 0 < c := by rw [← succ_zero, succ_le_iff] theorem one_le_iff_ne_zero {c : Cardinal} : 1 ≤ c ↔ c ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] @[simp] theorem lt_one_iff_zero {c : Cardinal} : c < 1 ↔ c = 0 := by simpa using lt_succ_bot_iff (a := c) /-! ### Properties about `aleph0` -/ theorem nat_lt_aleph0 (n : ℕ) : (n : Cardinal.{u}) < ℵ₀ := succ_le_iff.1 (by rw [← nat_succ, ← lift_mk_fin, aleph0, lift_mk_le.{u}] exact ⟨⟨(↑), fun a b => Fin.ext⟩⟩) @[simp] theorem one_lt_aleph0 : 1 < ℵ₀ := by simpa using nat_lt_aleph0 1 @[simp] theorem one_le_aleph0 : 1 ≤ ℵ₀ := one_lt_aleph0.le theorem lt_aleph0 {c : Cardinal} : c < ℵ₀ ↔ ∃ n : ℕ, c = n := ⟨fun h => by rcases lt_lift_iff.1 h with ⟨c, h', rfl⟩ rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩ suffices S.Finite by lift S to Finset ℕ using this simp contrapose! h' haveI := Infinite.to_subtype h' exact ⟨Infinite.natEmbedding S⟩, fun ⟨_, e⟩ => e.symm ▸ nat_lt_aleph0 _⟩ lemma succ_eq_of_lt_aleph0 {c : Cardinal} (h : c < ℵ₀) : Order.succ c = c + 1 := by obtain ⟨n, hn⟩ := Cardinal.lt_aleph0.mp h rw [hn, succ_natCast] theorem aleph0_le {c : Cardinal} : ℵ₀ ≤ c ↔ ∀ n : ℕ, ↑n ≤ c := ⟨fun h _ => (nat_lt_aleph0 _).le.trans h, fun h => le_of_not_lt fun hn => by rcases lt_aleph0.1 hn with ⟨n, rfl⟩ exact (Nat.lt_succ_self _).not_le (Nat.cast_le.1 (h (n + 1)))⟩ theorem isSuccPrelimit_aleph0 : IsSuccPrelimit ℵ₀ := isSuccPrelimit_of_succ_lt fun a ha => by rcases lt_aleph0.1 ha with ⟨n, rfl⟩ rw [← nat_succ] apply nat_lt_aleph0 theorem isSuccLimit_aleph0 : IsSuccLimit ℵ₀ := by rw [Cardinal.isSuccLimit_iff] exact ⟨aleph0_ne_zero, isSuccPrelimit_aleph0⟩ lemma not_isSuccLimit_natCast : (n : ℕ) → ¬ IsSuccLimit (n : Cardinal.{u}) | 0, e => e.1 isMin_bot | Nat.succ n, e => Order.not_isSuccPrelimit_succ _ (nat_succ n ▸ e.2) theorem not_isSuccLimit_of_lt_aleph0 {c : Cardinal} (h : c < ℵ₀) : ¬ IsSuccLimit c := by obtain ⟨n, rfl⟩ := lt_aleph0.1 h exact not_isSuccLimit_natCast n theorem aleph0_le_of_isSuccLimit {c : Cardinal} (h : IsSuccLimit c) : ℵ₀ ≤ c := by contrapose! h exact not_isSuccLimit_of_lt_aleph0 h theorem isStrongLimit_aleph0 : IsStrongLimit ℵ₀ := by refine ⟨aleph0_ne_zero, fun x hx ↦ ?_⟩ obtain ⟨n, rfl⟩ := lt_aleph0.1 hx exact_mod_cast nat_lt_aleph0 _ theorem IsStrongLimit.aleph0_le {c} (H : IsStrongLimit c) : ℵ₀ ≤ c := aleph0_le_of_isSuccLimit H.isSuccLimit lemma exists_eq_natCast_of_iSup_eq {ι : Type u} [Nonempty ι] (f : ι → Cardinal.{v}) (hf : BddAbove (range f)) (n : ℕ) (h : ⨆ i, f i = n) : ∃ i, f i = n := exists_eq_of_iSup_eq_of_not_isSuccLimit.{u, v} f hf (not_isSuccLimit_natCast n) h @[simp] theorem range_natCast : range ((↑) : ℕ → Cardinal) = Iio ℵ₀ := ext fun x => by simp only [mem_Iio, mem_range, eq_comm, lt_aleph0] theorem mk_eq_nat_iff {α : Type u} {n : ℕ} : #α = n ↔ Nonempty (α ≃ Fin n) := by rw [← lift_mk_fin, ← lift_uzero #α, lift_mk_eq'] theorem lt_aleph0_iff_finite {α : Type u} : #α < ℵ₀ ↔ Finite α := by simp only [lt_aleph0, mk_eq_nat_iff, finite_iff_exists_equiv_fin] theorem lt_aleph0_iff_fintype {α : Type u} : #α < ℵ₀ ↔ Nonempty (Fintype α) := lt_aleph0_iff_finite.trans (finite_iff_nonempty_fintype _) theorem lt_aleph0_of_finite (α : Type u) [Finite α] : #α < ℵ₀ := lt_aleph0_iff_finite.2 ‹_› theorem lt_aleph0_iff_set_finite {S : Set α} : #S < ℵ₀ ↔ S.Finite := lt_aleph0_iff_finite.trans finite_coe_iff alias ⟨_, _root_.Set.Finite.lt_aleph0⟩ := lt_aleph0_iff_set_finite @[simp] theorem lt_aleph0_iff_subtype_finite {p : α → Prop} : #{ x // p x } < ℵ₀ ↔ { x | p x }.Finite := lt_aleph0_iff_set_finite theorem mk_le_aleph0_iff : #α ≤ ℵ₀ ↔ Countable α := by rw [countable_iff_nonempty_embedding, aleph0, ← lift_uzero #α, lift_mk_le'] @[simp] theorem mk_le_aleph0 [Countable α] : #α ≤ ℵ₀ := mk_le_aleph0_iff.mpr ‹_› theorem le_aleph0_iff_set_countable {s : Set α} : #s ≤ ℵ₀ ↔ s.Countable := mk_le_aleph0_iff alias ⟨_, _root_.Set.Countable.le_aleph0⟩ := le_aleph0_iff_set_countable @[simp] theorem le_aleph0_iff_subtype_countable {p : α → Prop} : #{ x // p x } ≤ ℵ₀ ↔ { x | p x }.Countable := le_aleph0_iff_set_countable theorem aleph0_lt_mk_iff : ℵ₀ < #α ↔ Uncountable α := by rw [← not_le, ← not_countable_iff, not_iff_not, mk_le_aleph0_iff] @[simp] theorem aleph0_lt_mk [Uncountable α] : ℵ₀ < #α := aleph0_lt_mk_iff.mpr ‹_› instance canLiftCardinalNat : CanLift Cardinal ℕ (↑) fun x => x < ℵ₀ := ⟨fun _ hx => let ⟨n, hn⟩ := lt_aleph0.mp hx ⟨n, hn.symm⟩⟩ theorem add_lt_aleph0 {a b : Cardinal} (ha : a < ℵ₀) (hb : b < ℵ₀) : a + b < ℵ₀ := match a, b, lt_aleph0.1 ha, lt_aleph0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by rw [← Nat.cast_add]; apply nat_lt_aleph0 theorem add_lt_aleph0_iff {a b : Cardinal} : a + b < ℵ₀ ↔ a < ℵ₀ ∧ b < ℵ₀ := ⟨fun h => ⟨(self_le_add_right _ _).trans_lt h, (self_le_add_left _ _).trans_lt h⟩, fun ⟨h1, h2⟩ => add_lt_aleph0 h1 h2⟩ theorem aleph0_le_add_iff {a b : Cardinal} : ℵ₀ ≤ a + b ↔ ℵ₀ ≤ a ∨ ℵ₀ ≤ b := by simp only [← not_lt, add_lt_aleph0_iff, not_and_or] /-- See also `Cardinal.nsmul_lt_aleph0_iff_of_ne_zero` if you already have `n ≠ 0`. -/ theorem nsmul_lt_aleph0_iff {n : ℕ} {a : Cardinal} : n • a < ℵ₀ ↔ n = 0 ∨ a < ℵ₀ := by cases n with | zero => simpa using nat_lt_aleph0 0 | succ n => simp only [Nat.succ_ne_zero, false_or] induction' n with n ih · simp rw [succ_nsmul, add_lt_aleph0_iff, ih, and_self_iff] /-- See also `Cardinal.nsmul_lt_aleph0_iff` for a hypothesis-free version. -/ theorem nsmul_lt_aleph0_iff_of_ne_zero {n : ℕ} {a : Cardinal} (h : n ≠ 0) : n • a < ℵ₀ ↔ a < ℵ₀ := nsmul_lt_aleph0_iff.trans <| or_iff_right h theorem mul_lt_aleph0 {a b : Cardinal} (ha : a < ℵ₀) (hb : b < ℵ₀) : a * b < ℵ₀ := match a, b, lt_aleph0.1 ha, lt_aleph0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by rw [← Nat.cast_mul]; apply nat_lt_aleph0 theorem mul_lt_aleph0_iff {a b : Cardinal} : a * b < ℵ₀ ↔ a = 0 ∨ b = 0 ∨ a < ℵ₀ ∧ b < ℵ₀ := by refine ⟨fun h => ?_, ?_⟩ · by_cases ha : a = 0 · exact Or.inl ha right by_cases hb : b = 0 · exact Or.inl hb right rw [← Ne, ← one_le_iff_ne_zero] at ha hb constructor · rw [← mul_one a] exact (mul_le_mul' le_rfl hb).trans_lt h · rw [← one_mul b] exact (mul_le_mul' ha le_rfl).trans_lt h rintro (rfl | rfl | ⟨ha, hb⟩) <;> simp only [*, mul_lt_aleph0, aleph0_pos, zero_mul, mul_zero] /-- See also `Cardinal.aleph0_le_mul_iff`. -/ theorem aleph0_le_mul_iff {a b : Cardinal} : ℵ₀ ≤ a * b ↔ a ≠ 0 ∧ b ≠ 0 ∧ (ℵ₀ ≤ a ∨ ℵ₀ ≤ b) := by let h := (@mul_lt_aleph0_iff a b).not rwa [not_lt, not_or, not_or, not_and_or, not_lt, not_lt] at h /-- See also `Cardinal.aleph0_le_mul_iff'`. -/ theorem aleph0_le_mul_iff' {a b : Cardinal.{u}} : ℵ₀ ≤ a * b ↔ a ≠ 0 ∧ ℵ₀ ≤ b ∨ ℵ₀ ≤ a ∧ b ≠ 0 := by have : ∀ {a : Cardinal.{u}}, ℵ₀ ≤ a → a ≠ 0 := fun a => ne_bot_of_le_ne_bot aleph0_ne_zero a simp only [aleph0_le_mul_iff, and_or_left, and_iff_right_of_imp this, @and_left_comm (a ≠ 0)] simp only [and_comm, or_comm] theorem mul_lt_aleph0_iff_of_ne_zero {a b : Cardinal} (ha : a ≠ 0) (hb : b ≠ 0) : a * b < ℵ₀ ↔ a < ℵ₀ ∧ b < ℵ₀ := by simp [mul_lt_aleph0_iff, ha, hb] theorem power_lt_aleph0 {a b : Cardinal} (ha : a < ℵ₀) (hb : b < ℵ₀) : a ^ b < ℵ₀ := match a, b, lt_aleph0.1 ha, lt_aleph0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by rw [power_natCast, ← Nat.cast_pow]; apply nat_lt_aleph0 theorem eq_one_iff_unique {α : Type*} : #α = 1 ↔ Subsingleton α ∧ Nonempty α := calc #α = 1 ↔ #α ≤ 1 ∧ 1 ≤ #α := le_antisymm_iff _ ↔ Subsingleton α ∧ Nonempty α := le_one_iff_subsingleton.and (one_le_iff_ne_zero.trans mk_ne_zero_iff) theorem infinite_iff {α : Type u} : Infinite α ↔ ℵ₀ ≤ #α := by rw [← not_lt, lt_aleph0_iff_finite, not_finite_iff_infinite] lemma aleph0_le_mk_iff : ℵ₀ ≤ #α ↔ Infinite α := infinite_iff.symm lemma mk_lt_aleph0_iff : #α < ℵ₀ ↔ Finite α := by simp [← not_le, aleph0_le_mk_iff] @[simp] lemma mk_lt_aleph0 [Finite α] : #α < ℵ₀ := mk_lt_aleph0_iff.2 ‹_› @[simp] theorem aleph0_le_mk (α : Type u) [Infinite α] : ℵ₀ ≤ #α := infinite_iff.1 ‹_› @[simp] theorem mk_eq_aleph0 (α : Type*) [Countable α] [Infinite α] : #α = ℵ₀ := mk_le_aleph0.antisymm <| aleph0_le_mk _ theorem denumerable_iff {α : Type u} : Nonempty (Denumerable α) ↔ #α = ℵ₀ := ⟨fun ⟨h⟩ => mk_congr ((@Denumerable.eqv α h).trans Equiv.ulift.symm), fun h => by obtain ⟨f⟩ := Quotient.exact h exact ⟨Denumerable.mk' <| f.trans Equiv.ulift⟩⟩ theorem mk_denumerable (α : Type u) [Denumerable α] : #α = ℵ₀ := denumerable_iff.1 ⟨‹_›⟩ theorem _root_.Set.countable_infinite_iff_nonempty_denumerable {α : Type*} {s : Set α} : s.Countable ∧ s.Infinite ↔ Nonempty (Denumerable s) := by rw [nonempty_denumerable_iff, ← Set.infinite_coe_iff, countable_coe_iff] @[simp] theorem aleph0_add_aleph0 : ℵ₀ + ℵ₀ = ℵ₀ := mk_denumerable _ theorem aleph0_mul_aleph0 : ℵ₀ * ℵ₀ = ℵ₀ := mk_denumerable _ @[simp] theorem nat_mul_aleph0 {n : ℕ} (hn : n ≠ 0) : ↑n * ℵ₀ = ℵ₀ := le_antisymm (lift_mk_fin n ▸ mk_le_aleph0) <| le_mul_of_one_le_left (zero_le _) <| by rwa [← Nat.cast_one, Nat.cast_le, Nat.one_le_iff_ne_zero] @[simp] theorem aleph0_mul_nat {n : ℕ} (hn : n ≠ 0) : ℵ₀ * n = ℵ₀ := by rw [mul_comm, nat_mul_aleph0 hn] @[simp] theorem ofNat_mul_aleph0 {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) * ℵ₀ = ℵ₀ := nat_mul_aleph0 (NeZero.ne n) @[simp] theorem aleph0_mul_ofNat {n : ℕ} [Nat.AtLeastTwo n] : ℵ₀ * ofNat(n) = ℵ₀ := aleph0_mul_nat (NeZero.ne n) @[simp] theorem add_le_aleph0 {c₁ c₂ : Cardinal} : c₁ + c₂ ≤ ℵ₀ ↔ c₁ ≤ ℵ₀ ∧ c₂ ≤ ℵ₀ := ⟨fun h => ⟨le_self_add.trans h, le_add_self.trans h⟩, fun h => aleph0_add_aleph0 ▸ add_le_add h.1 h.2⟩ @[simp] theorem aleph0_add_nat (n : ℕ) : ℵ₀ + n = ℵ₀ := (add_le_aleph0.2 ⟨le_rfl, (nat_lt_aleph0 n).le⟩).antisymm le_self_add @[simp] theorem nat_add_aleph0 (n : ℕ) : ↑n + ℵ₀ = ℵ₀ := by rw [add_comm, aleph0_add_nat] @[simp] theorem ofNat_add_aleph0 {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) + ℵ₀ = ℵ₀ := nat_add_aleph0 n @[simp] theorem aleph0_add_ofNat {n : ℕ} [Nat.AtLeastTwo n] : ℵ₀ + ofNat(n) = ℵ₀ := aleph0_add_nat n theorem exists_nat_eq_of_le_nat {c : Cardinal} {n : ℕ} (h : c ≤ n) : ∃ m, m ≤ n ∧ c = m := by lift c to ℕ using h.trans_lt (nat_lt_aleph0 _) exact ⟨c, mod_cast h, rfl⟩ theorem mk_int : #ℤ = ℵ₀ := mk_denumerable ℤ theorem mk_pnat : #ℕ+ = ℵ₀ := mk_denumerable ℕ+ @[deprecated (since := "2025-04-27")] alias mk_pNat := mk_pnat /-! ### Cardinalities of basic sets and types -/ @[simp] theorem mk_additive : #(Additive α) = #α := rfl @[simp] theorem mk_multiplicative : #(Multiplicative α) = #α := rfl @[to_additive (attr := simp)] theorem mk_mulOpposite : #(MulOpposite α) = #α := mk_congr MulOpposite.opEquiv.symm theorem mk_singleton {α : Type u} (x : α) : #({x} : Set α) = 1 := mk_eq_one _ @[simp] theorem mk_vector (α : Type u) (n : ℕ) : #(List.Vector α n) = #α ^ n := (mk_congr (Equiv.vectorEquivFin α n)).trans <| by simp theorem mk_list_eq_sum_pow (α : Type u) : #(List α) = sum fun n : ℕ => #α ^ n := calc #(List α) = #(Σn, List.Vector α n) := mk_congr (Equiv.sigmaFiberEquiv List.length).symm _ = sum fun n : ℕ => #α ^ n := by simp theorem mk_quot_le {α : Type u} {r : α → α → Prop} : #(Quot r) ≤ #α := mk_le_of_surjective Quot.exists_rep theorem mk_quotient_le {α : Type u} {s : Setoid α} : #(Quotient s) ≤ #α := mk_quot_le theorem mk_subtype_le_of_subset {α : Type u} {p q : α → Prop} (h : ∀ ⦃x⦄, p x → q x) : #(Subtype p) ≤ #(Subtype q) := ⟨Embedding.subtypeMap (Embedding.refl α) h⟩ theorem mk_emptyCollection (α : Type u) : #(∅ : Set α) = 0 := mk_eq_zero _ theorem mk_emptyCollection_iff {α : Type u} {s : Set α} : #s = 0 ↔ s = ∅ := by constructor · intro h rw [mk_eq_zero_iff] at h exact eq_empty_iff_forall_not_mem.2 fun x hx => h.elim' ⟨x, hx⟩ · rintro rfl exact mk_emptyCollection _ @[simp] theorem mk_univ {α : Type u} : #(@univ α) = #α := mk_congr (Equiv.Set.univ α) @[simp] lemma mk_setProd {α β : Type u} (s : Set α) (t : Set β) : #(s ×ˢ t) = #s * #t := by rw [mul_def, mk_congr (Equiv.Set.prod ..)] theorem mk_image_le {α β : Type u} {f : α → β} {s : Set α} : #(f '' s) ≤ #s := mk_le_of_surjective surjective_onto_image lemma mk_image2_le {α β γ : Type u} {f : α → β → γ} {s : Set α} {t : Set β} : #(image2 f s t) ≤ #s * #t := by rw [← image_uncurry_prod, ← mk_setProd] exact mk_image_le theorem mk_image_le_lift {α : Type u} {β : Type v} {f : α → β} {s : Set α} : lift.{u} #(f '' s) ≤ lift.{v} #s := lift_mk_le.{0}.mpr ⟨Embedding.ofSurjective _ surjective_onto_image⟩ theorem mk_range_le {α β : Type u} {f : α → β} : #(range f) ≤ #α := mk_le_of_surjective surjective_onto_range theorem mk_range_le_lift {α : Type u} {β : Type v} {f : α → β} : lift.{u} #(range f) ≤ lift.{v} #α := lift_mk_le.{0}.mpr ⟨Embedding.ofSurjective _ surjective_onto_range⟩ theorem mk_range_eq (f : α → β) (h : Injective f) : #(range f) = #α := mk_congr (Equiv.ofInjective f h).symm theorem mk_range_eq_lift {α : Type u} {β : Type v} {f : α → β} (hf : Injective f) : lift.{max u w} #(range f) = lift.{max v w} #α := lift_mk_eq.{v,u,w}.mpr ⟨(Equiv.ofInjective f hf).symm⟩ theorem mk_range_eq_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : Injective f) : lift.{u} #(range f) = lift.{v} #α := lift_mk_eq'.mpr ⟨(Equiv.ofInjective f hf).symm⟩ lemma lift_mk_le_lift_mk_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : Injective f) : Cardinal.lift.{v} (#α) ≤ Cardinal.lift.{u} (#β) := by rw [← Cardinal.mk_range_eq_of_injective hf] exact Cardinal.lift_le.2 (Cardinal.mk_set_le _) lemma lift_mk_le_lift_mk_of_surjective {α : Type u} {β : Type v} {f : α → β} (hf : Surjective f) : Cardinal.lift.{u} (#β) ≤ Cardinal.lift.{v} (#α) := lift_mk_le_lift_mk_of_injective (injective_surjInv hf) theorem mk_image_eq_of_injOn {α β : Type u} (f : α → β) (s : Set α) (h : InjOn f s) : #(f '' s) = #s := mk_congr (Equiv.Set.imageOfInjOn f s h).symm theorem mk_image_eq_of_injOn_lift {α : Type u} {β : Type v} (f : α → β) (s : Set α) (h : InjOn f s) : lift.{u} #(f '' s) = lift.{v} #s := lift_mk_eq.{v, u, 0}.mpr ⟨(Equiv.Set.imageOfInjOn f s h).symm⟩ theorem mk_image_eq {α β : Type u} {f : α → β} {s : Set α} (hf : Injective f) : #(f '' s) = #s := mk_image_eq_of_injOn _ _ hf.injOn theorem mk_image_eq_lift {α : Type u} {β : Type v} (f : α → β) (s : Set α) (h : Injective f) : lift.{u} #(f '' s) = lift.{v} #s := mk_image_eq_of_injOn_lift _ _ h.injOn @[simp] theorem mk_image_embedding_lift {β : Type v} (f : α ↪ β) (s : Set α) : lift.{u} #(f '' s) = lift.{v} #s := mk_image_eq_lift _ _ f.injective @[simp] theorem mk_image_embedding (f : α ↪ β) (s : Set α) : #(f '' s) = #s := by simpa using mk_image_embedding_lift f s theorem mk_iUnion_le_sum_mk {α ι : Type u} {f : ι → Set α} : #(⋃ i, f i) ≤ sum fun i => #(f i) := calc #(⋃ i, f i) ≤ #(Σi, f i) := mk_le_of_surjective (Set.sigmaToiUnion_surjective f) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_le_sum_mk_lift {α : Type u} {ι : Type v} {f : ι → Set α} : lift.{v} #(⋃ i, f i) ≤ sum fun i => #(f i) := calc lift.{v} #(⋃ i, f i) ≤ #(Σi, f i) := mk_le_of_surjective <| ULift.up_surjective.comp (Set.sigmaToiUnion_surjective f) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_eq_sum_mk {α ι : Type u} {f : ι → Set α} (h : Pairwise (Disjoint on f)) : #(⋃ i, f i) = sum fun i => #(f i) := calc #(⋃ i, f i) = #(Σi, f i) := mk_congr (Set.unionEqSigmaOfDisjoint h) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_eq_sum_mk_lift {α : Type u} {ι : Type v} {f : ι → Set α} (h : Pairwise (Disjoint on f)) : lift.{v} #(⋃ i, f i) = sum fun i => #(f i) := calc lift.{v} #(⋃ i, f i) = #(Σi, f i) := mk_congr <| .trans Equiv.ulift (Set.unionEqSigmaOfDisjoint h) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_le {α ι : Type u} (f : ι → Set α) : #(⋃ i, f i) ≤ #ι * ⨆ i, #(f i) := mk_iUnion_le_sum_mk.trans (sum_le_iSup _) theorem mk_iUnion_le_lift {α : Type u} {ι : Type v} (f : ι → Set α) : lift.{v} #(⋃ i, f i) ≤ lift.{u} #ι * ⨆ i, lift.{v} #(f i) := by refine mk_iUnion_le_sum_mk_lift.trans <| Eq.trans_le ?_ (sum_le_iSup_lift _) rw [← lift_sum, lift_id'.{_,u}] theorem mk_sUnion_le {α : Type u} (A : Set (Set α)) : #(⋃₀ A) ≤ #A * ⨆ s : A, #s := by rw [sUnion_eq_iUnion] apply mk_iUnion_le theorem mk_biUnion_le {ι α : Type u} (A : ι → Set α) (s : Set ι) : #(⋃ x ∈ s, A x) ≤ #s * ⨆ x : s, #(A x.1) := by rw [biUnion_eq_iUnion] apply mk_iUnion_le theorem mk_biUnion_le_lift {α : Type u} {ι : Type v} (A : ι → Set α) (s : Set ι) : lift.{v} #(⋃ x ∈ s, A x) ≤ lift.{u} #s * ⨆ x : s, lift.{v} #(A x.1) := by rw [biUnion_eq_iUnion] apply mk_iUnion_le_lift theorem finset_card_lt_aleph0 (s : Finset α) : #(↑s : Set α) < ℵ₀ := lt_aleph0_of_finite _ theorem mk_set_eq_nat_iff_finset {α} {s : Set α} {n : ℕ} : #s = n ↔ ∃ t : Finset α, (t : Set α) = s ∧ t.card = n := by constructor · intro h lift s to Finset α using lt_aleph0_iff_set_finite.1 (h.symm ▸ nat_lt_aleph0 n) simpa using h · rintro ⟨t, rfl, rfl⟩ exact mk_coe_finset theorem mk_eq_nat_iff_finset {n : ℕ} : #α = n ↔ ∃ t : Finset α, (t : Set α) = univ ∧ t.card = n := by rw [← mk_univ, mk_set_eq_nat_iff_finset] theorem mk_eq_nat_iff_fintype {n : ℕ} : #α = n ↔ ∃ h : Fintype α, @Fintype.card α h = n := by rw [mk_eq_nat_iff_finset] constructor · rintro ⟨t, ht, hn⟩ exact ⟨⟨t, eq_univ_iff_forall.1 ht⟩, hn⟩ · rintro ⟨⟨t, ht⟩, hn⟩ exact ⟨t, eq_univ_iff_forall.2 ht, hn⟩ theorem mk_union_add_mk_inter {α : Type u} {S T : Set α} : #(S ∪ T : Set α) + #(S ∩ T : Set α) = #S + #T := by classical exact Quot.sound ⟨Equiv.Set.unionSumInter S T⟩ /-- The cardinality of a union is at most the sum of the cardinalities of the two sets. -/ theorem mk_union_le {α : Type u} (S T : Set α) : #(S ∪ T : Set α) ≤ #S + #T := @mk_union_add_mk_inter α S T ▸ self_le_add_right #(S ∪ T : Set α) #(S ∩ T : Set α) theorem mk_union_of_disjoint {α : Type u} {S T : Set α} (H : Disjoint S T) : #(S ∪ T : Set α) = #S + #T := by classical exact Quot.sound ⟨Equiv.Set.union H⟩ theorem mk_insert {α : Type u} {s : Set α} {a : α} (h : a ∉ s) : #(insert a s : Set α) = #s + 1 := by rw [← union_singleton, mk_union_of_disjoint, mk_singleton] simpa theorem mk_insert_le {α : Type u} {s : Set α} {a : α} : #(insert a s : Set α) ≤ #s + 1 := by by_cases h : a ∈ s · simp only [insert_eq_of_mem h, self_le_add_right] · rw [mk_insert h] theorem mk_sum_compl {α} (s : Set α) : #s + #(sᶜ : Set α) = #α := by classical exact mk_congr (Equiv.Set.sumCompl s) theorem mk_le_mk_of_subset {α} {s t : Set α} (h : s ⊆ t) : #s ≤ #t := ⟨Set.embeddingOfSubset s t h⟩ theorem mk_le_iff_forall_finset_subset_card_le {α : Type u} {n : ℕ} {t : Set α} : #t ≤ n ↔ ∀ s : Finset α, (s : Set α) ⊆ t → s.card ≤ n := by refine ⟨fun H s hs ↦ by simpa using (mk_le_mk_of_subset hs).trans H, fun H ↦ ?_⟩ apply card_le_of (fun s ↦ ?_) classical let u : Finset α := s.image Subtype.val have : u.card = s.card := Finset.card_image_of_injOn Subtype.coe_injective.injOn rw [← this] apply H simp only [u, Finset.coe_image, image_subset_iff, Subtype.coe_preimage_self, subset_univ] theorem mk_subtype_mono {p q : α → Prop} (h : ∀ x, p x → q x) : #{ x // p x } ≤ #{ x // q x } := ⟨embeddingOfSubset _ _ h⟩ theorem le_mk_diff_add_mk (S T : Set α) : #S ≤ #(S \ T : Set α) + #T := (mk_le_mk_of_subset <| subset_diff_union _ _).trans <| mk_union_le _ _ theorem mk_diff_add_mk {S T : Set α} (h : T ⊆ S) : #(S \ T : Set α) + #T = #S := by refine (mk_union_of_disjoint <| ?_).symm.trans <| by rw [diff_union_of_subset h] exact disjoint_sdiff_self_left theorem mk_union_le_aleph0 {α} {P Q : Set α} : #(P ∪ Q : Set α) ≤ ℵ₀ ↔ #P ≤ ℵ₀ ∧ #Q ≤ ℵ₀ := by simp only [le_aleph0_iff_subtype_countable, mem_union, setOf_mem_eq, Set.union_def, ← countable_union] theorem mk_sep (s : Set α) (t : α → Prop) : #({ x ∈ s | t x } : Set α) = #{ x : s | t x.1 } := mk_congr (Equiv.Set.sep s t) theorem mk_preimage_of_injective_lift {α : Type u} {β : Type v} (f : α → β) (s : Set β) (h : Injective f) : lift.{v} #(f ⁻¹' s) ≤ lift.{u} #s := by rw [lift_mk_le.{0}] -- Porting note: Needed to insert `mem_preimage.mp` below use Subtype.coind (fun x => f x.1) fun x => mem_preimage.mp x.2 apply Subtype.coind_injective; exact h.comp Subtype.val_injective theorem mk_preimage_of_subset_range_lift {α : Type u} {β : Type v} (f : α → β) (s : Set β) (h : s ⊆ range f) : lift.{u} #s ≤ lift.{v} #(f ⁻¹' s) := by rw [← image_preimage_eq_iff] at h nth_rewrite 1 [← h] apply mk_image_le_lift theorem mk_preimage_of_injective_of_subset_range_lift {β : Type v} (f : α → β) (s : Set β) (h : Injective f) (h2 : s ⊆ range f) : lift.{v} #(f ⁻¹' s) = lift.{u} #s := le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2) theorem mk_preimage_of_injective_of_subset_range (f : α → β) (s : Set β) (h : Injective f) (h2 : s ⊆ range f) : #(f ⁻¹' s) = #s := by convert mk_preimage_of_injective_of_subset_range_lift.{u, u} f s h h2 using 1 <;> rw [lift_id] @[simp] theorem mk_preimage_equiv_lift {β : Type v} (f : α ≃ β) (s : Set β) : lift.{v} #(f ⁻¹' s) = lift.{u} #s := by apply mk_preimage_of_injective_of_subset_range_lift _ _ f.injective rw [f.range_eq_univ] exact fun _ _ ↦ ⟨⟩ @[simp] theorem mk_preimage_equiv (f : α ≃ β) (s : Set β) : #(f ⁻¹' s) = #s := by simpa using mk_preimage_equiv_lift f s theorem mk_preimage_of_injective (f : α → β) (s : Set β) (h : Injective f) : #(f ⁻¹' s) ≤ #s := by rw [← lift_id #(↑(f ⁻¹' s)), ← lift_id #(↑s)] exact mk_preimage_of_injective_lift f s h theorem mk_preimage_of_subset_range (f : α → β) (s : Set β) (h : s ⊆ range f) : #s ≤ #(f ⁻¹' s) := by rw [← lift_id #(↑(f ⁻¹' s)), ← lift_id #(↑s)] exact mk_preimage_of_subset_range_lift f s h theorem mk_subset_ge_of_subset_image_lift {α : Type u} {β : Type v} (f : α → β) {s : Set α} {t : Set β} (h : t ⊆ f '' s) : lift.{u} #t ≤ lift.{v} #({ x ∈ s | f x ∈ t } : Set α) := by rw [image_eq_range] at h convert mk_preimage_of_subset_range_lift _ _ h using 1 rw [mk_sep] rfl theorem mk_subset_ge_of_subset_image (f : α → β) {s : Set α} {t : Set β} (h : t ⊆ f '' s) : #t ≤ #({ x ∈ s | f x ∈ t } : Set α) := by rw [image_eq_range] at h convert mk_preimage_of_subset_range _ _ h using 1 rw [mk_sep] rfl theorem le_mk_iff_exists_subset {c : Cardinal} {α : Type u} {s : Set α} : c ≤ #s ↔ ∃ p : Set α, p ⊆ s ∧ #p = c := by rw [le_mk_iff_exists_set, ← Subtype.exists_set_subtype] apply exists_congr; intro t; rw [mk_image_eq]; apply Subtype.val_injective @[simp] theorem mk_range_inl {α : Type u} {β : Type v} : #(range (@Sum.inl α β)) = lift.{v} #α := by rw [← lift_id'.{u, v} #_, (Equiv.Set.rangeInl α β).lift_cardinal_eq, lift_umax.{u, v}] @[simp] theorem mk_range_inr {α : Type u} {β : Type v} : #(range (@Sum.inr α β)) = lift.{u} #β := by rw [← lift_id'.{v, u} #_, (Equiv.Set.rangeInr α β).lift_cardinal_eq, lift_umax.{v, u}] theorem two_le_iff : (2 : Cardinal) ≤ #α ↔ ∃ x y : α, x ≠ y := by rw [← Nat.cast_two, nat_succ, succ_le_iff, Nat.cast_one, one_lt_iff_nontrivial, nontrivial_iff] theorem two_le_iff' (x : α) : (2 : Cardinal) ≤ #α ↔ ∃ y : α, y ≠ x := by rw [two_le_iff, ← nontrivial_iff, nontrivial_iff_exists_ne x] theorem mk_eq_two_iff : #α = 2 ↔ ∃ x y : α, x ≠ y ∧ ({x, y} : Set α) = univ := by classical simp only [← @Nat.cast_two Cardinal, mk_eq_nat_iff_finset, Finset.card_eq_two] constructor · rintro ⟨t, ht, x, y, hne, rfl⟩ exact ⟨x, y, hne, by simpa using ht⟩ · rintro ⟨x, y, hne, h⟩ exact ⟨{x, y}, by simpa using h, x, y, hne, rfl⟩ theorem mk_eq_two_iff' (x : α) : #α = 2 ↔ ∃! y, y ≠ x := by rw [mk_eq_two_iff]; constructor · rintro ⟨a, b, hne, h⟩ simp only [eq_univ_iff_forall, mem_insert_iff, mem_singleton_iff] at h rcases h x with (rfl | rfl) exacts [⟨b, hne.symm, fun z => (h z).resolve_left⟩, ⟨a, hne, fun z => (h z).resolve_right⟩] · rintro ⟨y, hne, hy⟩ exact ⟨x, y, hne.symm, eq_univ_of_forall fun z => or_iff_not_imp_left.2 (hy z)⟩ theorem exists_not_mem_of_length_lt {α : Type*} (l : List α) (h : ↑l.length < #α) : ∃ z : α, z ∉ l := by classical contrapose! h calc #α = #(Set.univ : Set α) := mk_univ.symm _ ≤ #l.toFinset := mk_le_mk_of_subset fun x _ => List.mem_toFinset.mpr (h x) _ = l.toFinset.card := Cardinal.mk_coe_finset _ ≤ l.length := Nat.cast_le.mpr (List.toFinset_card_le l) theorem three_le {α : Type*} (h : 3 ≤ #α) (x : α) (y : α) : ∃ z : α, z ≠ x ∧ z ≠ y := by have : ↑(3 : ℕ) ≤ #α := by simpa using h have : ↑(2 : ℕ) < #α := by rwa [← succ_le_iff, ← Cardinal.nat_succ] have := exists_not_mem_of_length_lt [x, y] this simpa [not_or] using this /-! ### `powerlt` operation -/ /-- The function `a ^< b`, defined as the supremum of `a ^ c` for `c < b`. -/ def powerlt (a b : Cardinal.{u}) : Cardinal.{u} := ⨆ c : Iio b, a ^ (c : Cardinal) @[inherit_doc] infixl:80 " ^< " => powerlt theorem le_powerlt {b c : Cardinal.{u}} (a) (h : c < b) : (a^c) ≤ a ^< b := by refine le_ciSup (f := fun y : Iio b => a ^ (y : Cardinal)) ?_ ⟨c, h⟩ rw [← image_eq_range] exact bddAbove_image.{u, u} _ bddAbove_Iio theorem powerlt_le {a b c : Cardinal.{u}} : a ^< b ≤ c ↔ ∀ x < b, a ^ x ≤ c := by rw [powerlt, ciSup_le_iff'] · simp · rw [← image_eq_range] exact bddAbove_image.{u, u} _ bddAbove_Iio theorem powerlt_le_powerlt_left {a b c : Cardinal} (h : b ≤ c) : a ^< b ≤ a ^< c := powerlt_le.2 fun _ hx => le_powerlt a <| hx.trans_le h theorem powerlt_mono_left (a) : Monotone fun c => a ^< c := fun _ _ => powerlt_le_powerlt_left theorem powerlt_succ {a b : Cardinal} (h : a ≠ 0) : a ^< succ b = a ^ b := (powerlt_le.2 fun _ h' => power_le_power_left h <| le_of_lt_succ h').antisymm <| le_powerlt a (lt_succ b) theorem powerlt_min {a b c : Cardinal} : a ^< min b c = min (a ^< b) (a ^< c) := (powerlt_mono_left a).map_min theorem powerlt_max {a b c : Cardinal} : a ^< max b c = max (a ^< b) (a ^< c) := (powerlt_mono_left a).map_max theorem zero_powerlt {a : Cardinal} (h : a ≠ 0) : 0 ^< a = 1 := by apply (powerlt_le.2 fun c _ => zero_power_le _).antisymm rw [← power_zero] exact le_powerlt 0 (pos_iff_ne_zero.2 h) @[simp] theorem powerlt_zero {a : Cardinal} : a ^< 0 = 0 := by convert Cardinal.iSup_of_empty _ exact Subtype.isEmpty_of_false fun x => mem_Iio.not.mpr (Cardinal.zero_le x).not_lt end Cardinal
Mathlib/SetTheory/Cardinal/Basic.lean
1,711
1,714
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Analysis.Analytic.IsolatedZeros import Mathlib.Analysis.SpecialFunctions.Complex.CircleMap import Mathlib.Analysis.SpecialFunctions.NonIntegrable /-! # Integral over a circle in `ℂ` In this file we define `∮ z in C(c, R), f z` to be the integral $\oint_{|z-c|=|R|} f(z)\,dz$ and prove some properties of this integral. We give definition and prove most lemmas for a function `f : ℂ → E`, where `E` is a complex Banach space. For this reason, some lemmas use, e.g., `(z - c)⁻¹ • f z` instead of `f z / (z - c)`. ## Main definitions * `CircleIntegrable f c R`: a function `f : ℂ → E` is integrable on the circle with center `c` and radius `R` if `f ∘ circleMap c R` is integrable on `[0, 2π]`; * `circleIntegral f c R`: the integral $\oint_{|z-c|=|R|} f(z)\,dz$, defined as $\int_{0}^{2π}(c + Re^{θ i})' f(c+Re^{θ i})\,dθ$; * `cauchyPowerSeries f c R`: the power series that is equal to $\sum_{n=0}^{\infty} \oint_{|z-c|=R} \left(\frac{w-c}{z - c}\right)^n \frac{1}{z-c}f(z)\,dz$ at `w - c`. The coefficients of this power series depend only on `f ∘ circleMap c R`, and the power series converges to `f w` if `f` is differentiable on the closed ball `Metric.closedBall c R` and `w` belongs to the corresponding open ball. ## Main statements * `hasFPowerSeriesOn_cauchy_integral`: for any circle integrable function `f`, the power series `cauchyPowerSeries f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `Metric.ball c R`; * `circleIntegral.integral_sub_zpow_of_undef`, `circleIntegral.integral_sub_zpow_of_ne`, and `circleIntegral.integral_sub_inv_of_mem_ball`: formulas for `∮ z in C(c, R), (z - w) ^ n`, `n : ℤ`. These lemmas cover the following cases: - `circleIntegral.integral_sub_zpow_of_undef`, `n < 0` and `|w - c| = |R|`: in this case the function is not integrable, so the integral is equal to its default value (zero); - `circleIntegral.integral_sub_zpow_of_ne`, `n ≠ -1`: in the cases not covered by the previous lemma, we have `(z - w) ^ n = ((z - w) ^ (n + 1) / (n + 1))'`, thus the integral equals zero; - `circleIntegral.integral_sub_inv_of_mem_ball`, `n = -1`, `|w - c| < R`: in this case the integral is equal to `2πi`. The case `n = -1`, `|w -c| > R` is not covered by these lemmas. While it is possible to construct an explicit primitive, it is easier to apply Cauchy theorem, so we postpone the proof till we have this theorem (see https://github.com/leanprover-community/mathlib4/pull/10000). ## Notation - `∮ z in C(c, R), f z`: notation for the integral $\oint_{|z-c|=|R|} f(z)\,dz$, defined as $\int_{0}^{2π}(c + Re^{θ i})' f(c+Re^{θ i})\,dθ$. ## Tags integral, circle, Cauchy integral -/ variable {E : Type*} [NormedAddCommGroup E] noncomputable section open scoped Real NNReal Interval Pointwise Topology open Complex MeasureTheory TopologicalSpace Metric Function Set Filter Asymptotics /-! ### Facts about `circleMap` -/ /-- The range of `circleMap c R` is the circle with center `c` and radius `|R|`. -/ @[simp] theorem range_circleMap (c : ℂ) (R : ℝ) : range (circleMap c R) = sphere c |R| := calc range (circleMap c R) = c +ᵥ R • range fun θ : ℝ => exp (θ * I) := by simp +unfoldPartialApp only [← image_vadd, ← image_smul, ← range_comp, vadd_eq_add, circleMap, comp_def, real_smul] _ = sphere c |R| := by rw [range_exp_mul_I, smul_sphere R 0 zero_le_one] simp /-- The image of `(0, 2π]` under `circleMap c R` is the circle with center `c` and radius `|R|`. -/ @[simp] theorem image_circleMap_Ioc (c : ℂ) (R : ℝ) : circleMap c R '' Ioc 0 (2 * π) = sphere c |R| := by rw [← range_circleMap, ← (periodic_circleMap c R).image_Ioc Real.two_pi_pos 0, zero_add] theorem hasDerivAt_circleMap (c : ℂ) (R : ℝ) (θ : ℝ) : HasDerivAt (circleMap c R) (circleMap 0 R θ * I) θ := by simpa only [mul_assoc, one_mul, ofRealCLM_apply, circleMap, ofReal_one, zero_add] using (((ofRealCLM.hasDerivAt (x := θ)).mul_const I).cexp.const_mul (R : ℂ)).const_add c theorem differentiable_circleMap (c : ℂ) (R : ℝ) : Differentiable ℝ (circleMap c R) := fun θ => (hasDerivAt_circleMap c R θ).differentiableAt /-- The circleMap is real analytic. -/ theorem analyticOnNhd_circleMap (c : ℂ) (R : ℝ) : AnalyticOnNhd ℝ (circleMap c R) Set.univ := by intro z hz apply analyticAt_const.add apply analyticAt_const.mul rw [← Function.comp_def] apply analyticAt_cexp.restrictScalars.comp ((ofRealCLM.analyticAt z).mul (by fun_prop)) /-- The circleMap is continuously differentiable. -/ theorem contDiff_circleMap (c : ℂ) (R : ℝ) {n : WithTop ℕ∞} : ContDiff ℝ n (circleMap c R) := (analyticOnNhd_circleMap c R).contDiff @[continuity, fun_prop] theorem continuous_circleMap (c : ℂ) (R : ℝ) : Continuous (circleMap c R) := (differentiable_circleMap c R).continuous @[fun_prop, measurability] theorem measurable_circleMap (c : ℂ) (R : ℝ) : Measurable (circleMap c R) := (continuous_circleMap c R).measurable @[simp] theorem deriv_circleMap (c : ℂ) (R : ℝ) (θ : ℝ) : deriv (circleMap c R) θ = circleMap 0 R θ * I := (hasDerivAt_circleMap _ _ _).deriv theorem deriv_circleMap_eq_zero_iff {c : ℂ} {R : ℝ} {θ : ℝ} : deriv (circleMap c R) θ = 0 ↔ R = 0 := by simp [I_ne_zero] theorem deriv_circleMap_ne_zero {c : ℂ} {R : ℝ} {θ : ℝ} (hR : R ≠ 0) : deriv (circleMap c R) θ ≠ 0 := mt deriv_circleMap_eq_zero_iff.1 hR theorem lipschitzWith_circleMap (c : ℂ) (R : ℝ) : LipschitzWith (Real.nnabs R) (circleMap c R) := lipschitzWith_of_nnnorm_deriv_le (differentiable_circleMap _ _) fun θ => NNReal.coe_le_coe.1 <| by simp theorem continuous_circleMap_inv {R : ℝ} {z w : ℂ} (hw : w ∈ ball z R) : Continuous fun θ => (circleMap z R θ - w)⁻¹ := by have : ∀ θ, circleMap z R θ - w ≠ 0 := by simp_rw [sub_ne_zero] exact fun θ => circleMap_ne_mem_ball hw θ -- Porting note: was `continuity` exact Continuous.inv₀ (by fun_prop) this theorem circleMap_preimage_codiscrete {c : ℂ} {R : ℝ} (hR : R ≠ 0) : map (circleMap c R) (codiscrete ℝ) ≤ codiscreteWithin (Metric.sphere c |R|) := by intro s hs apply (analyticOnNhd_circleMap c R).preimage_mem_codiscreteWithin · intro x hx by_contra hCon obtain ⟨a, ha⟩ := eventuallyConst_iff_exists_eventuallyEq.1 hCon have := ha.deriv.eq_of_nhds simp [hR] at this · rwa [Set.image_univ, range_circleMap] /-! ### Integrability of a function on a circle -/ /-- We say that a function `f : ℂ → E` is integrable on the circle with center `c` and radius `R` if the function `f ∘ circleMap c R` is integrable on `[0, 2π]`. Note that the actual function used in the definition of `circleIntegral` is `(deriv (circleMap c R) θ) • f (circleMap c R θ)`. Integrability of this function is equivalent to integrability of `f ∘ circleMap c R` whenever `R ≠ 0`. -/ def CircleIntegrable (f : ℂ → E) (c : ℂ) (R : ℝ) : Prop := IntervalIntegrable (fun θ : ℝ => f (circleMap c R θ)) volume 0 (2 * π) @[simp] theorem circleIntegrable_const (a : E) (c : ℂ) (R : ℝ) : CircleIntegrable (fun _ => a) c R := intervalIntegrable_const namespace CircleIntegrable variable {f g : ℂ → E} {c : ℂ} {R : ℝ} nonrec theorem add (hf : CircleIntegrable f c R) (hg : CircleIntegrable g c R) : CircleIntegrable (f + g) c R := hf.add hg nonrec theorem neg (hf : CircleIntegrable f c R) : CircleIntegrable (-f) c R := hf.neg /-- The function we actually integrate over `[0, 2π]` in the definition of `circleIntegral` is integrable. -/ theorem out [NormedSpace ℂ E] (hf : CircleIntegrable f c R) : IntervalIntegrable (fun θ : ℝ => deriv (circleMap c R) θ • f (circleMap c R θ)) volume 0 (2 * π) := by simp only [CircleIntegrable, deriv_circleMap, intervalIntegrable_iff] at * refine (hf.norm.const_mul |R|).mono' ?_ ?_ · exact ((continuous_circleMap _ _).aestronglyMeasurable.mul_const I).smul hf.aestronglyMeasurable · simp [norm_smul] end CircleIntegrable @[simp] theorem circleIntegrable_zero_radius {f : ℂ → E} {c : ℂ} : CircleIntegrable f c 0 := by simp [CircleIntegrable] /-- Circle integrability is invariant when functions change along discrete sets. -/ theorem CircleIntegrable.congr_codiscreteWithin {c : ℂ} {R : ℝ} {f₁ f₂ : ℂ → ℂ} (hf : f₁ =ᶠ[codiscreteWithin (Metric.sphere c |R|)] f₂) (hf₁ : CircleIntegrable f₁ c R) : CircleIntegrable f₂ c R := by by_cases hR : R = 0 · simp [hR] apply (intervalIntegrable_congr_codiscreteWithin _).1 hf₁ rw [eventuallyEq_iff_exists_mem] exact ⟨(circleMap c R)⁻¹' {z | f₁ z = f₂ z}, codiscreteWithin.mono (by simp only [Set.subset_univ]) (circleMap_preimage_codiscrete hR hf), by tauto⟩ /-- Circle integrability is invariant when functions change along discrete sets. -/ theorem circleIntegrable_congr_codiscreteWithin {c : ℂ} {R : ℝ} {f₁ f₂ : ℂ → ℂ} (hf : f₁ =ᶠ[codiscreteWithin (Metric.sphere c |R|)] f₂) : CircleIntegrable f₁ c R ↔ CircleIntegrable f₂ c R := ⟨(CircleIntegrable.congr_codiscreteWithin hf ·), (CircleIntegrable.congr_codiscreteWithin hf.symm ·)⟩ theorem circleIntegrable_iff [NormedSpace ℂ E] {f : ℂ → E} {c : ℂ} (R : ℝ) : CircleIntegrable f c R ↔ IntervalIntegrable (fun θ : ℝ => deriv (circleMap c R) θ • f (circleMap c R θ)) volume 0 (2 * π) := by by_cases h₀ : R = 0 · simp +unfoldPartialApp [h₀, const] refine ⟨fun h => h.out, fun h => ?_⟩ simp only [CircleIntegrable, intervalIntegrable_iff, deriv_circleMap] at h ⊢ refine (h.norm.const_mul |R|⁻¹).mono' ?_ ?_ · have H : ∀ {θ}, circleMap 0 R θ * I ≠ 0 := fun {θ} => by simp [h₀, I_ne_zero] simpa only [inv_smul_smul₀ H] using ((continuous_circleMap 0 R).aestronglyMeasurable.mul_const I).aemeasurable.inv.aestronglyMeasurable.smul h.aestronglyMeasurable · simp [norm_smul, h₀] theorem ContinuousOn.circleIntegrable' {f : ℂ → E} {c : ℂ} {R : ℝ} (hf : ContinuousOn f (sphere c |R|)) : CircleIntegrable f c R := (hf.comp_continuous (continuous_circleMap _ _) (circleMap_mem_sphere' _ _)).intervalIntegrable _ _ theorem ContinuousOn.circleIntegrable {f : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (hf : ContinuousOn f (sphere c R)) : CircleIntegrable f c R := ContinuousOn.circleIntegrable' <| (abs_of_nonneg hR).symm ▸ hf /-- The function `fun z ↦ (z - w) ^ n`, `n : ℤ`, is circle integrable on the circle with center `c` and radius `|R|` if and only if `R = 0` or `0 ≤ n`, or `w` does not belong to this circle. -/ @[simp] theorem circleIntegrable_sub_zpow_iff {c w : ℂ} {R : ℝ} {n : ℤ} : CircleIntegrable (fun z => (z - w) ^ n) c R ↔ R = 0 ∨ 0 ≤ n ∨ w ∉ sphere c |R| := by constructor · intro h; contrapose! h; rcases h with ⟨hR, hn, hw⟩ simp only [circleIntegrable_iff R, deriv_circleMap] rw [← image_circleMap_Ioc] at hw; rcases hw with ⟨θ, hθ, rfl⟩ replace hθ : θ ∈ [[0, 2 * π]] := Icc_subset_uIcc (Ioc_subset_Icc_self hθ) refine not_intervalIntegrable_of_sub_inv_isBigO_punctured ?_ Real.two_pi_pos.ne hθ set f : ℝ → ℂ := fun θ' => circleMap c R θ' - circleMap c R θ have : ∀ᶠ θ' in 𝓝[≠] θ, f θ' ∈ ball (0 : ℂ) 1 \ {0} := by suffices ∀ᶠ z in 𝓝[≠] circleMap c R θ, z - circleMap c R θ ∈ ball (0 : ℂ) 1 \ {0} from ((differentiable_circleMap c R θ).hasDerivAt.tendsto_nhdsNE (deriv_circleMap_ne_zero hR)).eventually this filter_upwards [self_mem_nhdsWithin, mem_nhdsWithin_of_mem_nhds (ball_mem_nhds _ zero_lt_one)] simp_all [dist_eq, sub_eq_zero] refine (((hasDerivAt_circleMap c R θ).isBigO_sub.mono inf_le_left).inv_rev (this.mono fun θ' h₁ h₂ => absurd h₂ h₁.2)).trans ?_ refine IsBigO.of_bound |R|⁻¹ (this.mono fun θ' hθ' => ?_) set x := ‖f θ'‖ suffices x⁻¹ ≤ x ^ n by simp only [inv_mul_cancel_left₀, abs_eq_zero.not.2 hR, Algebra.id.smul_eq_mul, norm_mul, norm_inv, norm_I, mul_one] simpa only [norm_circleMap_zero, norm_zpow, Ne, abs_eq_zero.not.2 hR, not_false_iff, inv_mul_cancel_left₀] using this have : x ∈ Ioo (0 : ℝ) 1 := by simpa [x, and_comm] using hθ' rw [← zpow_neg_one] refine (zpow_right_strictAnti₀ this.1 this.2).le_iff_le.2 (Int.lt_add_one_iff.1 ?_); exact hn · rintro (rfl | H) exacts [circleIntegrable_zero_radius, ((continuousOn_id.sub continuousOn_const).zpow₀ _ fun z hz => H.symm.imp_left fun (hw : w ∉ sphere c |R|) => sub_ne_zero.2 <| ne_of_mem_of_not_mem hz hw).circleIntegrable'] @[simp] theorem circleIntegrable_sub_inv_iff {c w : ℂ} {R : ℝ} : CircleIntegrable (fun z => (z - w)⁻¹) c R ↔ R = 0 ∨ w ∉ sphere c |R| := by simp only [← zpow_neg_one, circleIntegrable_sub_zpow_iff]; norm_num variable [NormedSpace ℂ E] /-- Definition for $\oint_{|z-c|=R} f(z)\,dz$ -/ def circleIntegral (f : ℂ → E) (c : ℂ) (R : ℝ) : E := ∫ θ : ℝ in (0)..2 * π, deriv (circleMap c R) θ • f (circleMap c R θ) /-- `∮ z in C(c, R), f z` is the circle integral $\oint_{|z-c|=R} f(z)\,dz$. -/ notation3 "∮ "(...)" in ""C("c", "R")"", "r:(scoped f => circleIntegral f c R) => r theorem circleIntegral_def_Icc (f : ℂ → E) (c : ℂ) (R : ℝ) : (∮ z in C(c, R), f z) = ∫ θ in Icc 0 (2 * π), deriv (circleMap c R) θ • f (circleMap c R θ) := by rw [circleIntegral, intervalIntegral.integral_of_le Real.two_pi_pos.le, Measure.restrict_congr_set Ioc_ae_eq_Icc] namespace circleIntegral @[simp] theorem integral_radius_zero (f : ℂ → E) (c : ℂ) : (∮ z in C(c, 0), f z) = 0 := by simp +unfoldPartialApp [circleIntegral, const] theorem integral_congr {f g : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (h : EqOn f g (sphere c R)) : (∮ z in C(c, R), f z) = ∮ z in C(c, R), g z := intervalIntegral.integral_congr fun θ _ => by simp only [h (circleMap_mem_sphere _ hR _)] /-- Circle integrals are invariant when functions change along discrete sets. -/ theorem circleIntegral_congr_codiscreteWithin {c : ℂ} {R : ℝ} {f₁ f₂ : ℂ → ℂ} (hf : f₁ =ᶠ[codiscreteWithin (Metric.sphere c |R|)] f₂) (hR : R ≠ 0) : (∮ z in C(c, R), f₁ z) = (∮ z in C(c, R), f₂ z) := by apply intervalIntegral.integral_congr_ae_restrict apply ae_restrict_le_codiscreteWithin measurableSet_uIoc simp only [deriv_circleMap, smul_eq_mul, mul_eq_mul_left_iff, mul_eq_zero, circleMap_eq_center_iff, hR, Complex.I_ne_zero, or_self, or_false] exact codiscreteWithin.mono (by tauto) (circleMap_preimage_codiscrete hR hf) theorem integral_sub_inv_smul_sub_smul (f : ℂ → E) (c w : ℂ) (R : ℝ) : (∮ z in C(c, R), (z - w)⁻¹ • (z - w) • f z) = ∮ z in C(c, R), f z := by rcases eq_or_ne R 0 with (rfl | hR); · simp only [integral_radius_zero] have : (circleMap c R ⁻¹' {w}).Countable := (countable_singleton _).preimage_circleMap c hR refine intervalIntegral.integral_congr_ae ((this.ae_not_mem _).mono fun θ hθ _' => ?_) change circleMap c R θ ≠ w at hθ simp only [inv_smul_smul₀ (sub_ne_zero.2 <| hθ)] theorem integral_undef {f : ℂ → E} {c : ℂ} {R : ℝ} (hf : ¬CircleIntegrable f c R) : (∮ z in C(c, R), f z) = 0 := intervalIntegral.integral_undef (mt (circleIntegrable_iff R).mpr hf) theorem integral_add {f g : ℂ → E} {c : ℂ} {R : ℝ} (hf : CircleIntegrable f c R) (hg : CircleIntegrable g c R) : (∮ z in C(c, R), f z + g z) = (∮ z in C(c, R), f z) + (∮ z in C(c, R), g z) := by simp only [circleIntegral, smul_add, intervalIntegral.integral_add hf.out hg.out] theorem integral_sub {f g : ℂ → E} {c : ℂ} {R : ℝ} (hf : CircleIntegrable f c R) (hg : CircleIntegrable g c R) : (∮ z in C(c, R), f z - g z) = (∮ z in C(c, R), f z) - ∮ z in C(c, R), g z := by simp only [circleIntegral, smul_sub, intervalIntegral.integral_sub hf.out hg.out] theorem norm_integral_le_of_norm_le_const' {f : ℂ → E} {c : ℂ} {R C : ℝ} (hf : ∀ z ∈ sphere c |R|, ‖f z‖ ≤ C) : ‖∮ z in C(c, R), f z‖ ≤ 2 * π * |R| * C := calc ‖∮ z in C(c, R), f z‖ ≤ |R| * C * |2 * π - 0| := intervalIntegral.norm_integral_le_of_norm_le_const fun θ _ => calc ‖deriv (circleMap c R) θ • f (circleMap c R θ)‖ = |R| * ‖f (circleMap c R θ)‖ := by simp [norm_smul] _ ≤ |R| * C := mul_le_mul_of_nonneg_left (hf _ <| circleMap_mem_sphere' _ _ _) (abs_nonneg _) _ = 2 * π * |R| * C := by rw [sub_zero, _root_.abs_of_pos Real.two_pi_pos]; ac_rfl theorem norm_integral_le_of_norm_le_const {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 ≤ R) (hf : ∀ z ∈ sphere c R, ‖f z‖ ≤ C) : ‖∮ z in C(c, R), f z‖ ≤ 2 * π * R * C := have : |R| = R := abs_of_nonneg hR calc ‖∮ z in C(c, R), f z‖ ≤ 2 * π * |R| * C := norm_integral_le_of_norm_le_const' <| by rwa [this] _ = 2 * π * R * C := by rw [this] theorem norm_two_pi_i_inv_smul_integral_le_of_norm_le_const {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 ≤ R) (hf : ∀ z ∈ sphere c R, ‖f z‖ ≤ C) : ‖(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), f z‖ ≤ R * C := by have : ‖(2 * π * I : ℂ)⁻¹‖ = (2 * π)⁻¹ := by simp [Real.pi_pos.le] rw [norm_smul, this, ← div_eq_inv_mul, div_le_iff₀ Real.two_pi_pos, mul_comm (R * C), ← mul_assoc] exact norm_integral_le_of_norm_le_const hR hf /-- If `f` is continuous on the circle `|z - c| = R`, `R > 0`, the `‖f z‖` is less than or equal to `C : ℝ` on this circle, and this norm is strictly less than `C` at some point `z` of the circle, then `‖∮ z in C(c, R), f z‖ < 2 * π * R * C`. -/ theorem norm_integral_lt_of_norm_le_const_of_lt {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 < R) (hc : ContinuousOn f (sphere c R)) (hf : ∀ z ∈ sphere c R, ‖f z‖ ≤ C) (hlt : ∃ z ∈ sphere c R, ‖f z‖ < C) : ‖∮ z in C(c, R), f z‖ < 2 * π * R * C := by rw [← _root_.abs_of_pos hR, ← image_circleMap_Ioc] at hlt rcases hlt with ⟨_, ⟨θ₀, hmem, rfl⟩, hlt⟩ calc ‖∮ z in C(c, R), f z‖ ≤ ∫ θ in (0)..2 * π, ‖deriv (circleMap c R) θ • f (circleMap c R θ)‖ := intervalIntegral.norm_integral_le_integral_norm Real.two_pi_pos.le _ < ∫ _ in (0)..2 * π, R * C := by simp only [deriv_circleMap, norm_smul, norm_mul, norm_circleMap_zero, abs_of_pos hR, norm_I, mul_one] refine intervalIntegral.integral_lt_integral_of_continuousOn_of_le_of_exists_lt Real.two_pi_pos ?_ continuousOn_const (fun θ _ => ?_) ⟨θ₀, Ioc_subset_Icc_self hmem, ?_⟩ · exact continuousOn_const.mul (hc.comp (continuous_circleMap _ _).continuousOn fun θ _ => circleMap_mem_sphere _ hR.le _).norm · exact mul_le_mul_of_nonneg_left (hf _ <| circleMap_mem_sphere _ hR.le _) hR.le · exact (mul_lt_mul_left hR).2 hlt _ = 2 * π * R * C := by simp [mul_assoc]; ring @[simp] theorem integral_smul {𝕜 : Type*} [RCLike 𝕜] [NormedSpace 𝕜 E] [SMulCommClass 𝕜 ℂ E] (a : 𝕜) (f : ℂ → E) (c : ℂ) (R : ℝ) : (∮ z in C(c, R), a • f z) = a • ∮ z in C(c, R), f z := by simp only [circleIntegral, ← smul_comm a (_ : ℂ) (_ : E), intervalIntegral.integral_smul] @[simp] theorem integral_smul_const [CompleteSpace E] (f : ℂ → ℂ) (a : E) (c : ℂ) (R : ℝ) : (∮ z in C(c, R), f z • a) = (∮ z in C(c, R), f z) • a := by simp only [circleIntegral, intervalIntegral.integral_smul_const, ← smul_assoc] @[simp] theorem integral_const_mul (a : ℂ) (f : ℂ → ℂ) (c : ℂ) (R : ℝ) : (∮ z in C(c, R), a * f z) = a * ∮ z in C(c, R), f z := integral_smul a f c R @[simp] theorem integral_sub_center_inv (c : ℂ) {R : ℝ} (hR : R ≠ 0) : (∮ z in C(c, R), (z - c)⁻¹) = 2 * π * I := by simp [circleIntegral, ← div_eq_mul_inv, mul_div_cancel_left₀ _ (circleMap_ne_center hR)] /-- If `f' : ℂ → E` is a derivative of a complex differentiable function on the circle `Metric.sphere c |R|`, then `∮ z in C(c, R), f' z = 0`. -/ theorem integral_eq_zero_of_hasDerivWithinAt' [CompleteSpace E] {f f' : ℂ → E} {c : ℂ} {R : ℝ} (h : ∀ z ∈ sphere c |R|, HasDerivWithinAt f (f' z) (sphere c |R|) z) : (∮ z in C(c, R), f' z) = 0 := by by_cases hi : CircleIntegrable f' c R · rw [← sub_eq_zero.2 ((periodic_circleMap c R).comp f).eq] refine intervalIntegral.integral_eq_sub_of_hasDerivAt (fun θ _ => ?_) hi.out exact (h _ (circleMap_mem_sphere' _ _ _)).scomp_hasDerivAt θ (differentiable_circleMap _ _ _).hasDerivAt (circleMap_mem_sphere' _ _) · exact integral_undef hi /-- If `f' : ℂ → E` is a derivative of a complex differentiable function on the circle `Metric.sphere c R`, then `∮ z in C(c, R), f' z = 0`. -/ theorem integral_eq_zero_of_hasDerivWithinAt [CompleteSpace E] {f f' : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (h : ∀ z ∈ sphere c R, HasDerivWithinAt f (f' z) (sphere c R) z) : (∮ z in C(c, R), f' z) = 0 := integral_eq_zero_of_hasDerivWithinAt' <| (abs_of_nonneg hR).symm ▸ h /-- If `n < 0` and `|w - c| = |R|`, then `(z - w) ^ n` is not circle integrable on the circle with center `c` and radius `|R|`, so the integral `∮ z in C(c, R), (z - w) ^ n` is equal to zero. -/ theorem integral_sub_zpow_of_undef {n : ℤ} {c w : ℂ} {R : ℝ} (hn : n < 0) (hw : w ∈ sphere c |R|) : (∮ z in C(c, R), (z - w) ^ n) = 0 := by rcases eq_or_ne R 0 with (rfl | h0) · apply integral_radius_zero · apply integral_undef simpa [circleIntegrable_sub_zpow_iff, *, not_or] /-- If `n ≠ -1` is an integer number, then the integral of `(z - w) ^ n` over the circle equals zero. -/ theorem integral_sub_zpow_of_ne {n : ℤ} (hn : n ≠ -1) (c w : ℂ) (R : ℝ) : (∮ z in C(c, R), (z - w) ^ n) = 0 := by rcases em (w ∈ sphere c |R| ∧ n < -1) with (⟨hw, hn⟩ | H) · exact integral_sub_zpow_of_undef (hn.trans (by decide)) hw push_neg at H have hd : ∀ z, z ≠ w ∨ -1 ≤ n → HasDerivAt (fun z => (z - w) ^ (n + 1) / (n + 1)) ((z - w) ^ n) z := by intro z hne convert ((hasDerivAt_zpow (n + 1) _ (hne.imp _ _)).comp z ((hasDerivAt_id z).sub_const w)).div_const _ using 1 · have hn' : (n + 1 : ℂ) ≠ 0 := by rwa [Ne, ← eq_neg_iff_add_eq_zero, ← Int.cast_one, ← Int.cast_neg, Int.cast_inj] simp [mul_assoc, mul_div_cancel_left₀ _ hn'] exacts [sub_ne_zero.2, neg_le_iff_add_nonneg.1] refine integral_eq_zero_of_hasDerivWithinAt' fun z hz => (hd z ?_).hasDerivWithinAt exact (ne_or_eq z w).imp_right fun (h : z = w) => H <| h ▸ hz end circleIntegral /-- The power series that is equal to $\frac{1}{2πi}\sum_{n=0}^{\infty} \oint_{|z-c|=R} \left(\frac{w-c}{z - c}\right)^n \frac{1}{z-c}f(z)\,dz$ at `w - c`. The coefficients of this power series depend only on `f ∘ circleMap c R`, and the power series converges to `f w` if `f` is differentiable on the closed ball `Metric.closedBall c R` and `w` belongs to the corresponding open ball. For any circle integrable function `f`, this power series converges to the Cauchy integral for `f`. -/ def cauchyPowerSeries (f : ℂ → E) (c : ℂ) (R : ℝ) : FormalMultilinearSeries ℂ ℂ E := fun n => ContinuousMultilinearMap.mkPiRing ℂ _ <| (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - c)⁻¹ ^ n • (z - c)⁻¹ • f z theorem cauchyPowerSeries_apply (f : ℂ → E) (c : ℂ) (R : ℝ) (n : ℕ) (w : ℂ) : (cauchyPowerSeries f c R n fun _ => w) = (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (w / (z - c)) ^ n • (z - c)⁻¹ • f z := by simp only [cauchyPowerSeries, ContinuousMultilinearMap.mkPiRing_apply, Fin.prod_const, div_eq_mul_inv, mul_pow, mul_smul, circleIntegral.integral_smul] rw [← smul_comm (w ^ n)] theorem norm_cauchyPowerSeries_le (f : ℂ → E) (c : ℂ) (R : ℝ) (n : ℕ) : ‖cauchyPowerSeries f c R n‖ ≤ ((2 * π)⁻¹ * ∫ θ : ℝ in (0)..2 * π, ‖f (circleMap c R θ)‖) * |R|⁻¹ ^ n := calc ‖cauchyPowerSeries f c R n‖ _ = (2 * π)⁻¹ * ‖∮ z in C(c, R), (z - c)⁻¹ ^ n • (z - c)⁻¹ • f z‖ := by simp [cauchyPowerSeries, norm_smul, Real.pi_pos.le] _ ≤ (2 * π)⁻¹ * ∫ θ in (0)..2 * π, ‖deriv (circleMap c R) θ • (circleMap c R θ - c)⁻¹ ^ n • (circleMap c R θ - c)⁻¹ • f (circleMap c R θ)‖ := (mul_le_mul_of_nonneg_left (intervalIntegral.norm_integral_le_integral_norm Real.two_pi_pos.le) (by simp [Real.pi_pos.le])) _ = (2 * π)⁻¹ * (|R|⁻¹ ^ n * (|R| * (|R|⁻¹ * ∫ x : ℝ in (0)..2 * π, ‖f (circleMap c R x)‖))) := by simp [norm_smul, mul_left_comm |R|] _ ≤ ((2 * π)⁻¹ * ∫ θ : ℝ in (0)..2 * π, ‖f (circleMap c R θ)‖) * |R|⁻¹ ^ n := by rcases eq_or_ne R 0 with (rfl | hR) · cases n <;> simp [-mul_inv_rev] rw [← mul_assoc, inv_mul_cancel₀ (Real.two_pi_pos.ne.symm), one_mul] apply norm_nonneg · rw [mul_inv_cancel_left₀, mul_assoc, mul_comm (|R|⁻¹ ^ n)] rwa [Ne, _root_.abs_eq_zero] theorem le_radius_cauchyPowerSeries (f : ℂ → E) (c : ℂ) (R : ℝ≥0) : ↑R ≤ (cauchyPowerSeries f c R).radius := by refine (cauchyPowerSeries f c R).le_radius_of_bound ((2 * π)⁻¹ * ∫ θ : ℝ in (0)..2 * π, ‖f (circleMap c R θ)‖) fun n => ?_ refine (mul_le_mul_of_nonneg_right (norm_cauchyPowerSeries_le _ _ _ _) (pow_nonneg R.coe_nonneg _)).trans ?_ rw [abs_of_nonneg R.coe_nonneg] rcases eq_or_ne (R ^ n : ℝ) 0 with hR | hR · rw_mod_cast [hR, mul_zero] exact mul_nonneg (inv_nonneg.2 Real.two_pi_pos.le) (intervalIntegral.integral_nonneg Real.two_pi_pos.le fun _ _ => norm_nonneg _) · rw [inv_pow] have : (R : ℝ) ^ n ≠ 0 := by norm_cast at hR ⊢ rw [inv_mul_cancel_right₀ this] /-- For any circle integrable function `f`, the power series `cauchyPowerSeries f c R` multiplied by `2πI` converges to the integral `∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `Metric.ball c R`. -/ theorem hasSum_two_pi_I_cauchyPowerSeries_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ} (hf : CircleIntegrable f c R) (hw : ‖w‖ < R) : HasSum (fun n : ℕ => ∮ z in C(c, R), (w / (z - c)) ^ n • (z - c)⁻¹ • f z) (∮ z in C(c, R), (z - (c + w))⁻¹ • f z) := by have hR : 0 < R := (norm_nonneg w).trans_lt hw have hwR : ‖w‖ / R ∈ Ico (0 : ℝ) 1 := ⟨div_nonneg (norm_nonneg w) hR.le, (div_lt_one hR).2 hw⟩ refine intervalIntegral.hasSum_integral_of_dominated_convergence (fun n θ => ‖f (circleMap c R θ)‖ * (‖w‖ / R) ^ n) (fun n => ?_) (fun n => ?_) ?_ ?_ ?_ · simp only [deriv_circleMap] apply_rules [AEStronglyMeasurable.smul, hf.def'.1] <;> apply Measurable.aestronglyMeasurable · fun_prop · fun_prop · fun_prop · simp [norm_smul, abs_of_pos hR, mul_left_comm R, inv_mul_cancel_left₀ hR.ne', mul_comm ‖_‖] · exact Eventually.of_forall fun _ _ => (summable_geometric_of_lt_one hwR.1 hwR.2).mul_left _ · simpa only [tsum_mul_left, tsum_geometric_of_lt_one hwR.1 hwR.2] using hf.norm.mul_continuousOn continuousOn_const · refine Eventually.of_forall fun θ _ => HasSum.const_smul _ ?_ simp only [smul_smul] refine HasSum.smul_const ?_ _ have : ‖w / (circleMap c R θ - c)‖ < 1 := by simpa [abs_of_pos hR] using hwR.2 convert (hasSum_geometric_of_norm_lt_one this).mul_right _ using 1 simp [← sub_sub, ← mul_inv, sub_mul, div_mul_cancel₀ _ (circleMap_ne_center hR.ne')] /-- For any circle integrable function `f`, the power series `cauchyPowerSeries f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `Metric.ball c R`. -/ theorem hasSum_cauchyPowerSeries_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ} (hf : CircleIntegrable f c R) (hw : ‖w‖ < R) : HasSum (fun n => cauchyPowerSeries f c R n fun _ => w) ((2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - (c + w))⁻¹ • f z) := by simp only [cauchyPowerSeries_apply] exact (hasSum_two_pi_I_cauchyPowerSeries_integral hf hw).const_smul _ /-- For any circle integrable function `f`, the power series `cauchyPowerSeries f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `Metric.ball c R`. -/ theorem sum_cauchyPowerSeries_eq_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ} (hf : CircleIntegrable f c R) (hw : ‖w‖ < R) : (cauchyPowerSeries f c R).sum w = (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - (c + w))⁻¹ • f z := (hasSum_cauchyPowerSeries_integral hf hw).tsum_eq /-- For any circle integrable function `f`, the power series `cauchyPowerSeries f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `Metric.ball c R`. -/ theorem hasFPowerSeriesOn_cauchy_integral {f : ℂ → E} {c : ℂ} {R : ℝ≥0} (hf : CircleIntegrable f c R) (hR : 0 < R) :
HasFPowerSeriesOnBall (fun w => (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z) (cauchyPowerSeries f c R) c R := { r_le := le_radius_cauchyPowerSeries _ _ _ r_pos := ENNReal.coe_pos.2 hR hasSum := fun hy ↦ hasSum_cauchyPowerSeries_integral hf <| by simpa using hy } namespace circleIntegral /-- Integral $\oint_{|z-c|=R} \frac{dz}{z-w} = 2πi$ whenever $|w-c| < R$. -/ theorem integral_sub_inv_of_mem_ball {c w : ℂ} {R : ℝ} (hw : w ∈ ball c R) : (∮ z in C(c, R), (z - w)⁻¹) = 2 * π * I := by have hR : 0 < R := dist_nonneg.trans_lt hw suffices H : HasSum (fun n : ℕ => ∮ z in C(c, R), ((w - c) / (z - c)) ^ n * (z - c)⁻¹) (2 * π * I) by have A : CircleIntegrable (fun _ => (1 : ℂ)) c R := continuousOn_const.circleIntegrable'
Mathlib/MeasureTheory/Integral/CircleIntegral.lean
564
578
/- 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⟩ @[simp] theorem prod_eq_empty_iff : s ×ˢ t = ∅ ↔ s = ∅ ∨ t = ∅ := by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_or] theorem prod_sub_preimage_iff {W : Set γ} {f : α × β → γ} : s ×ˢ t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] theorem image_prodMk_subset_prod {f : α → β} {g : α → γ} {s : Set α} : (fun x => (f x, g x)) '' s ⊆ (f '' s) ×ˢ (g '' s) := by rintro _ ⟨x, hx, rfl⟩ exact mk_mem_prod (mem_image_of_mem f hx) (mem_image_of_mem g hx) @[deprecated (since := "2025-02-22")] alias image_prod_mk_subset_prod := image_prodMk_subset_prod theorem image_prodMk_subset_prod_left (hb : b ∈ t) : (fun a => (a, b)) '' s ⊆ s ×ˢ t := by rintro _ ⟨a, ha, rfl⟩ exact ⟨ha, hb⟩ @[deprecated (since := "2025-02-22")] alias image_prod_mk_subset_prod_left := image_prodMk_subset_prod_left theorem image_prodMk_subset_prod_right (ha : a ∈ s) : Prod.mk a '' t ⊆ s ×ˢ t := by rintro _ ⟨b, hb, rfl⟩ exact ⟨ha, hb⟩ @[deprecated (since := "2025-02-22")] alias image_prod_mk_subset_prod_right := image_prodMk_subset_prod_right theorem prod_subset_preimage_fst (s : Set α) (t : Set β) : s ×ˢ t ⊆ Prod.fst ⁻¹' s := inter_subset_left theorem fst_image_prod_subset (s : Set α) (t : Set β) : Prod.fst '' s ×ˢ t ⊆ s := image_subset_iff.2 <| prod_subset_preimage_fst s t theorem fst_image_prod (s : Set β) {t : Set α} (ht : t.Nonempty) : Prod.fst '' s ×ˢ t = s := (fst_image_prod_subset _ _).antisymm fun y hy => let ⟨x, hx⟩ := ht ⟨(y, x), ⟨hy, hx⟩, rfl⟩ lemma mapsTo_fst_prod {s : Set α} {t : Set β} : MapsTo Prod.fst (s ×ˢ t) s := fun _ hx ↦ (mem_prod.1 hx).1 theorem prod_subset_preimage_snd (s : Set α) (t : Set β) : s ×ˢ t ⊆ Prod.snd ⁻¹' t := inter_subset_right theorem snd_image_prod_subset (s : Set α) (t : Set β) : Prod.snd '' s ×ˢ t ⊆ t := image_subset_iff.2 <| prod_subset_preimage_snd s t theorem snd_image_prod {s : Set α} (hs : s.Nonempty) (t : Set β) : Prod.snd '' s ×ˢ t = t := (snd_image_prod_subset _ _).antisymm fun y y_in => let ⟨x, x_in⟩ := hs ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ lemma mapsTo_snd_prod {s : Set α} {t : Set β} : MapsTo Prod.snd (s ×ˢ t) t := fun _ hx ↦ (mem_prod.1 hx).2 theorem prod_diff_prod : s ×ˢ t \ s₁ ×ˢ t₁ = s ×ˢ (t \ t₁) ∪ (s \ s₁) ×ˢ t := by ext x by_cases h₁ : x.1 ∈ s₁ <;> by_cases h₂ : x.2 ∈ t₁ <;> simp [*] /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ theorem prod_subset_prod_iff : s ×ˢ t ⊆ s₁ ×ˢ t₁ ↔ s ⊆ s₁ ∧ t ⊆ t₁ ∨ s = ∅ ∨ t = ∅ := by rcases (s ×ˢ t).eq_empty_or_nonempty with h | h · simp [h, prod_eq_empty_iff.1 h] have st : s.Nonempty ∧ t.Nonempty := by rwa [prod_nonempty_iff] at h refine ⟨fun H => Or.inl ⟨?_, ?_⟩, ?_⟩ · have := image_subset (Prod.fst : α × β → α) H rwa [fst_image_prod _ st.2, fst_image_prod _ (h.mono H).snd] at this · have := image_subset (Prod.snd : α × β → β) H rwa [snd_image_prod st.1, snd_image_prod (h.mono H).fst] at this · intro H simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H exact prod_mono H.1 H.2 theorem prod_eq_prod_iff_of_nonempty (h : (s ×ˢ t).Nonempty) : s ×ˢ t = s₁ ×ˢ t₁ ↔ s = s₁ ∧ t = t₁ := by constructor · intro heq have h₁ : (s₁ ×ˢ t₁ : Set _).Nonempty := by rwa [← heq] rw [prod_nonempty_iff] at h h₁ rw [← fst_image_prod s h.2, ← fst_image_prod s₁ h₁.2, heq, eq_self_iff_true, true_and, ← snd_image_prod h.1 t, ← snd_image_prod h₁.1 t₁, heq] · rintro ⟨rfl, rfl⟩ rfl theorem prod_eq_prod_iff : s ×ˢ t = s₁ ×ˢ t₁ ↔ s = s₁ ∧ t = t₁ ∨ (s = ∅ ∨ t = ∅) ∧ (s₁ = ∅ ∨ t₁ = ∅) := by symm rcases eq_empty_or_nonempty (s ×ˢ t) with h | h · simp_rw [h, @eq_comm _ ∅, prod_eq_empty_iff, prod_eq_empty_iff.mp h, true_and, or_iff_right_iff_imp] rintro ⟨rfl, rfl⟩ exact prod_eq_empty_iff.mp h rw [prod_eq_prod_iff_of_nonempty h] rw [nonempty_iff_ne_empty, Ne, prod_eq_empty_iff] at h simp_rw [h, false_and, or_false] @[simp] theorem prod_eq_iff_eq (ht : t.Nonempty) : s ×ˢ t = s₁ ×ˢ t ↔ s = s₁ := by simp_rw [prod_eq_prod_iff, ht.ne_empty, and_true, or_iff_left_iff_imp, or_false] rintro ⟨rfl, rfl⟩ rfl theorem subset_prod {s : Set (α × β)} : s ⊆ (Prod.fst '' s) ×ˢ (Prod.snd '' s) := fun _ hp ↦ mem_prod.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩ section Mono variable [Preorder α] {f : α → Set β} {g : α → Set γ} theorem _root_.Monotone.set_prod (hf : Monotone f) (hg : Monotone g) : Monotone fun x => f x ×ˢ g x := fun _ _ h => prod_mono (hf h) (hg h) theorem _root_.Antitone.set_prod (hf : Antitone f) (hg : Antitone g) : Antitone fun x => f x ×ˢ g x := fun _ _ h => prod_mono (hf h) (hg h) theorem _root_.MonotoneOn.set_prod (hf : MonotoneOn f s) (hg : MonotoneOn g s) : MonotoneOn (fun x => f x ×ˢ g x) s := fun _ ha _ hb h => prod_mono (hf ha hb h) (hg ha hb h) theorem _root_.AntitoneOn.set_prod (hf : AntitoneOn f s) (hg : AntitoneOn g s) : AntitoneOn (fun x => f x ×ˢ g x) s := fun _ ha _ hb h => prod_mono (hf ha hb h) (hg ha hb h) end Mono end Prod /-! ### Diagonal In this section we prove some lemmas about the diagonal set `{p | p.1 = p.2}` and the diagonal map `fun x ↦ (x, x)`. -/ section Diagonal variable {α : Type*} {s t : Set α} lemma diagonal_nonempty [Nonempty α] : (diagonal α).Nonempty := Nonempty.elim ‹_› fun x => ⟨_, mem_diagonal x⟩ instance decidableMemDiagonal [h : DecidableEq α] (x : α × α) : Decidable (x ∈ diagonal α) := h x.1 x.2 theorem preimage_coe_coe_diagonal (s : Set α) : Prod.map (fun x : s => (x : α)) (fun x : s => (x : α)) ⁻¹' diagonal α = diagonal s := by ext ⟨⟨x, hx⟩, ⟨y, hy⟩⟩ simp [Set.diagonal] @[simp] theorem range_diag : (range fun x => (x, x)) = diagonal α := by ext ⟨x, y⟩ simp [diagonal, eq_comm] theorem diagonal_subset_iff {s} : diagonal α ⊆ s ↔ ∀ x, (x, x) ∈ s := by rw [← range_diag, range_subset_iff] @[simp] theorem prod_subset_compl_diagonal_iff_disjoint : s ×ˢ t ⊆ (diagonal α)ᶜ ↔ Disjoint s t := prod_subset_iff.trans disjoint_iff_forall_ne.symm @[simp] theorem diag_preimage_prod (s t : Set α) : (fun x => (x, x)) ⁻¹' s ×ˢ t = s ∩ t := rfl theorem diag_preimage_prod_self (s : Set α) : (fun x => (x, x)) ⁻¹' s ×ˢ s = s := inter_self s theorem diag_image (s : Set α) : (fun x => (x, x)) '' s = diagonal α ∩ s ×ˢ s := by rw [← range_diag, ← image_preimage_eq_range_inter, diag_preimage_prod_self] theorem diagonal_eq_univ_iff : diagonal α = univ ↔ Subsingleton α := by simp only [subsingleton_iff, eq_univ_iff_forall, Prod.forall, mem_diagonal_iff] theorem diagonal_eq_univ [Subsingleton α] : diagonal α = univ := diagonal_eq_univ_iff.2 ‹_› end Diagonal /-- A function is `Function.const α a` for some `a` if and only if `∀ x y, f x = f y`. -/ theorem range_const_eq_diagonal {α β : Type*} [hβ : Nonempty β] : range (const α) = {f : α → β | ∀ x y, f x = f y} := by refine (range_eq_iff _ _).mpr ⟨fun _ _ _ ↦ rfl, fun f hf ↦ ?_⟩ rcases isEmpty_or_nonempty α with h|⟨⟨a⟩⟩ · exact hβ.elim fun b ↦ ⟨b, Subsingleton.elim _ _⟩ · exact ⟨f a, funext fun x ↦ hf _ _⟩ end Set section Pullback open Set variable {X Y Z} /-- The fiber product $X \times_Y Z$. -/ abbrev Function.Pullback (f : X → Y) (g : Z → Y) := {p : X × Z // f p.1 = g p.2} /-- The fiber product $X \times_Y X$. -/ abbrev Function.PullbackSelf (f : X → Y) := f.Pullback f /-- The projection from the fiber product to the first factor. -/ def Function.Pullback.fst {f : X → Y} {g : Z → Y} (p : f.Pullback g) : X := p.val.1 /-- The projection from the fiber product to the second factor. -/ def Function.Pullback.snd {f : X → Y} {g : Z → Y} (p : f.Pullback g) : Z := p.val.2 open Function.Pullback in lemma Function.pullback_comm_sq (f : X → Y) (g : Z → Y) : f ∘ @fst X Y Z f g = g ∘ @snd X Y Z f g := funext fun p ↦ p.2 /-- The diagonal map $\Delta: X \to X \times_Y X$. -/ @[simps] def toPullbackDiag (f : X → Y) (x : X) : f.Pullback f := ⟨(x, x), rfl⟩ /-- The diagonal $\Delta(X) \subseteq X \times_Y X$. -/ def Function.pullbackDiagonal (f : X → Y) : Set (f.Pullback f) := {p | p.fst = p.snd} /-- Three functions between the three pairs of spaces $X_i, Y_i, Z_i$ that are compatible induce a function $X_1 \times_{Y_1} Z_1 \to X_2 \times_{Y_2} Z_2$. -/ def Function.mapPullback {X₁ X₂ Y₁ Y₂ Z₁ Z₂} {f₁ : X₁ → Y₁} {g₁ : Z₁ → Y₁} {f₂ : X₂ → Y₂} {g₂ : Z₂ → Y₂} (mapX : X₁ → X₂) (mapY : Y₁ → Y₂) (mapZ : Z₁ → Z₂) (commX : f₂ ∘ mapX = mapY ∘ f₁) (commZ : g₂ ∘ mapZ = mapY ∘ g₁) (p : f₁.Pullback g₁) : f₂.Pullback g₂ := ⟨(mapX p.fst, mapZ p.snd), (congr_fun commX _).trans <| (congr_arg mapY p.2).trans <| congr_fun commZ.symm _⟩ open Function.Pullback in /-- The projection $(X \times_Y Z) \times_Z (X \times_Y Z) \to X \times_Y X$. -/ def Function.PullbackSelf.map_fst {f : X → Y} {g : Z → Y} : (@snd X Y Z f g).PullbackSelf → f.PullbackSelf := mapPullback fst g fst (pullback_comm_sq f g) (pullback_comm_sq f g) open Function.Pullback in /-- The projection $(X \times_Y Z) \times_X (X \times_Y Z) \to Z \times_Y Z$. -/ def Function.PullbackSelf.map_snd {f : X → Y} {g : Z → Y} : (@fst X Y Z f g).PullbackSelf → g.PullbackSelf := mapPullback snd f snd (pullback_comm_sq f g).symm (pullback_comm_sq f g).symm open Function.PullbackSelf Function.Pullback theorem preimage_map_fst_pullbackDiagonal {f : X → Y} {g : Z → Y} : @map_fst X Y Z f g ⁻¹' pullbackDiagonal f = pullbackDiagonal (@snd X Y Z f g) := by ext ⟨⟨p₁, p₂⟩, he⟩ simp_rw [pullbackDiagonal, mem_setOf, Subtype.ext_iff, Prod.ext_iff] exact (and_iff_left he).symm theorem Function.Injective.preimage_pullbackDiagonal {f : X → Y} {g : Z → X} (inj : g.Injective) : mapPullback g id g (by rfl) (by rfl) ⁻¹' pullbackDiagonal f = pullbackDiagonal (f ∘ g) := ext fun _ ↦ inj.eq_iff theorem image_toPullbackDiag (f : X → Y) (s : Set X) : toPullbackDiag f '' s = pullbackDiagonal f ∩ Subtype.val ⁻¹' s ×ˢ s := by ext x constructor · rintro ⟨x, hx, rfl⟩ exact ⟨rfl, hx, hx⟩ · obtain ⟨⟨x, y⟩, h⟩ := x rintro ⟨rfl : x = y, h2x⟩ exact mem_image_of_mem _ h2x.1 theorem range_toPullbackDiag (f : X → Y) : range (toPullbackDiag f) = pullbackDiagonal f := by rw [← image_univ, image_toPullbackDiag, univ_prod_univ, preimage_univ, inter_univ] theorem injective_toPullbackDiag (f : X → Y) : (toPullbackDiag f).Injective := fun _ _ h ↦ congr_arg Prod.fst (congr_arg Subtype.val h) end Pullback namespace Set section OffDiag variable {α : Type*} {s t : Set α} {a : α} theorem offDiag_mono : Monotone (offDiag : Set α → Set (α × α)) := fun _ _ h _ => And.imp (@h _) <| And.imp_left <| @h _ @[simp] theorem offDiag_nonempty : s.offDiag.Nonempty ↔ s.Nontrivial := by simp [offDiag, Set.Nonempty, Set.Nontrivial] @[simp] theorem offDiag_eq_empty : s.offDiag = ∅ ↔ s.Subsingleton := by rw [← not_nonempty_iff_eq_empty, ← not_nontrivial_iff, offDiag_nonempty.not] alias ⟨_, Nontrivial.offDiag_nonempty⟩ := offDiag_nonempty alias ⟨_, Subsingleton.offDiag_eq_empty⟩ := offDiag_nonempty variable (s t) theorem offDiag_subset_prod : s.offDiag ⊆ s ×ˢ s := fun _ hx => ⟨hx.1, hx.2.1⟩ theorem offDiag_eq_sep_prod : s.offDiag = { x ∈ s ×ˢ s | x.1 ≠ x.2 } := ext fun _ => and_assoc.symm @[simp] theorem offDiag_empty : (∅ : Set α).offDiag = ∅ := by simp @[simp] theorem offDiag_singleton (a : α) : ({a} : Set α).offDiag = ∅ := by simp @[simp] theorem offDiag_univ : (univ : Set α).offDiag = (diagonal α)ᶜ := ext <| by simp @[simp] theorem prod_sdiff_diagonal : s ×ˢ s \ diagonal α = s.offDiag := ext fun _ => and_assoc @[simp] theorem disjoint_diagonal_offDiag : Disjoint (diagonal α) s.offDiag := disjoint_left.mpr fun _ hd ho => ho.2.2 hd theorem offDiag_inter : (s ∩ t).offDiag = s.offDiag ∩ t.offDiag := ext fun x => by simp only [mem_offDiag, mem_inter_iff] tauto variable {s t} theorem offDiag_union (h : Disjoint s t) : (s ∪ t).offDiag = s.offDiag ∪ t.offDiag ∪ s ×ˢ t ∪ t ×ˢ s := by ext x simp only [mem_offDiag, mem_union, ne_eq, mem_prod] constructor · rintro ⟨h0|h0, h1|h1, h2⟩ <;> simp [h0, h1, h2] · rintro (((⟨h0, h1, h2⟩|⟨h0, h1, h2⟩)|⟨h0, h1⟩)|⟨h0, h1⟩) <;> simp [*] · rintro h3 rw [h3] at h0 exact Set.disjoint_left.mp h h0 h1 · rintro h3 rw [h3] at h0 exact (Set.disjoint_right.mp h h0 h1).elim theorem offDiag_insert (ha : a ∉ s) : (insert a s).offDiag = s.offDiag ∪ {a} ×ˢ s ∪ s ×ˢ {a} := by rw [insert_eq, union_comm, offDiag_union, offDiag_singleton, union_empty, union_right_comm] rw [disjoint_left] rintro b hb (rfl : b = a) exact ha hb end OffDiag /-! ### Cartesian set-indexed product of sets -/ section Pi variable {ι : Type*} {α β : ι → Type*} {s s₁ s₂ : Set ι} {t t₁ t₂ : ∀ i, Set (α i)} {i : ι} @[simp] theorem empty_pi (s : ∀ i, Set (α i)) : pi ∅ s = univ := by ext simp [pi] theorem subsingleton_univ_pi (ht : ∀ i, (t i).Subsingleton) : (univ.pi t).Subsingleton := fun _f hf _g hg ↦ funext fun i ↦ (ht i) (hf _ <| mem_univ _) (hg _ <| mem_univ _) @[simp] theorem pi_univ (s : Set ι) : (pi s fun i => (univ : Set (α i))) = univ := eq_univ_of_forall fun _ _ _ => mem_univ _ @[simp] theorem pi_univ_ite (s : Set ι) [DecidablePred (· ∈ s)] (t : ∀ i, Set (α i)) : (pi univ fun i => if i ∈ s then t i else univ) = s.pi t := by ext; simp_rw [Set.mem_pi]; apply forall_congr'; intro i; split_ifs with h <;> simp [h] theorem pi_mono (h : ∀ i ∈ s, t₁ i ⊆ t₂ i) : pi s t₁ ⊆ pi s t₂ := fun _ hx i hi => h i hi <| hx i hi theorem pi_inter_distrib : (s.pi fun i => t i ∩ t₁ i) = s.pi t ∩ s.pi t₁ := ext fun x => by simp only [forall_and, mem_pi, mem_inter_iff] theorem pi_congr (h : s₁ = s₂) (h' : ∀ i ∈ s₁, t₁ i = t₂ i) : s₁.pi t₁ = s₂.pi t₂ := h ▸ ext fun _ => forall₂_congr fun i hi => h' i hi ▸ Iff.rfl theorem pi_eq_empty (hs : i ∈ s) (ht : t i = ∅) : s.pi t = ∅ := by ext f simp only [mem_empty_iff_false, not_forall, iff_false, mem_pi, Classical.not_imp] exact ⟨i, hs, by simp [ht]⟩ theorem univ_pi_eq_empty (ht : t i = ∅) : pi univ t = ∅ := pi_eq_empty (mem_univ i) ht theorem pi_nonempty_iff : (s.pi t).Nonempty ↔ ∀ i, ∃ x, i ∈ s → x ∈ t i := by simp [Classical.skolem, Set.Nonempty] theorem univ_pi_nonempty_iff : (pi univ t).Nonempty ↔ ∀ i, (t i).Nonempty := by simp [Classical.skolem, Set.Nonempty] theorem pi_eq_empty_iff : s.pi t = ∅ ↔ ∃ i, IsEmpty (α i) ∨ i ∈ s ∧ t i = ∅ := by
rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff] push_neg refine exists_congr fun i => ?_ cases isEmpty_or_nonempty (α i) <;> simp [*, forall_and, eq_empty_iff_forall_not_mem]
Mathlib/Data/Set/Prod.lean
665
668
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Yongle Hu -/ import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.Group.Subgroup.Actions import Mathlib.RingTheory.Ideal.Pointwise import Mathlib.RingTheory.Ideal.Quotient.Operations /-! # Ideals over/under ideals This file concerns ideals lying over other ideals. Let `f : R →+* S` be a ring homomorphism (typically a ring extension), `I` an ideal of `R` and `J` an ideal of `S`. We say `J` lies over `I` (and `I` under `J`) if `I` is the `f`-preimage of `J`. This is expressed here by writing `I = J.comap f`. -/ -- for going-up results about integral extensions, see `Mathlib.RingTheory.Ideal.GoingUp` assert_not_exists Algebra.IsIntegral -- for results about finiteness, see `Mathlib.RingTheory.Finiteness.Quotient` assert_not_exists Module.Finite variable {R : Type*} [CommRing R] namespace Ideal open Submodule open scoped Pointwise section CommRing variable {S : Type*} [CommRing S] {f : R →+* S} {I J : Ideal S} variable {p : Ideal R} {P : Ideal S} /-- If there is an injective map `R/p → S/P` such that following diagram commutes: ``` R → S ↓ ↓ R/p → S/P ``` then `P` lies over `p`. -/ theorem comap_eq_of_scalar_tower_quotient [Algebra R S] [Algebra (R ⧸ p) (S ⧸ P)] [IsScalarTower R (R ⧸ p) (S ⧸ P)] (h : Function.Injective (algebraMap (R ⧸ p) (S ⧸ P))) : comap (algebraMap R S) P = p := by ext x rw [mem_comap, ← Quotient.eq_zero_iff_mem, ← Quotient.eq_zero_iff_mem, Quotient.mk_algebraMap, IsScalarTower.algebraMap_apply R (R ⧸ p) (S ⧸ P), Quotient.algebraMap_eq] constructor · intro hx
exact (injective_iff_map_eq_zero (algebraMap (R ⧸ p) (S ⧸ P))).mp h _ hx · intro hx rw [hx, RingHom.map_zero] variable [Algebra R S] /-- `R / p` has a canonical map to `S / pS`. -/ instance Quotient.algebraQuotientMapQuotient : Algebra (R ⧸ p) (S ⧸ map (algebraMap R S) p) := Ideal.Quotient.algebraQuotientOfLEComap le_comap_map @[simp] theorem Quotient.algebraMap_quotient_map_quotient (x : R) : letI f := algebraMap R S algebraMap (R ⧸ p) (S ⧸ map f p) (Ideal.Quotient.mk p x) = Ideal.Quotient.mk (map f p) (f x) :=
Mathlib/RingTheory/Ideal/Over.lean
56
70
/- 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, Kevin Buzzard, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import Mathlib.Algebra.Module.Submodule.Ker import Mathlib.Algebra.Module.Submodule.RestrictScalars import Mathlib.Data.Set.Finite.Range /-! # Range of linear maps The range `LinearMap.range` of a (semi)linear map `f : M → M₂` is a submodule of `M₂`. More specifically, `LinearMap.range` applies to any `SemilinearMapClass` over a `RingHomSurjective` ring homomorphism. Note that this also means that dot notation (i.e. `f.range` for a linear map `f`) does not work. ## Notations * We continue to use the notations `M →ₛₗ[σ] M₂` and `M →ₗ[R] M₂` for the type of semilinear (resp. linear) maps from `M` to `M₂` over the ring homomorphism `σ` (resp. over the ring `R`). ## Tags linear algebra, vector space, module, range -/ open Function variable {R : Type*} {R₂ : Type*} {R₃ : Type*} variable {K : Type*} variable {M : Type*} {M₂ : Type*} {M₃ : Type*} variable {V : Type*} {V₂ : Type*} namespace LinearMap section AddCommMonoid variable [Semiring R] [Semiring R₂] [Semiring R₃] variable [AddCommMonoid M] [AddCommMonoid M₂] [AddCommMonoid M₃] variable [Module R M] [Module R₂ M₂] [Module R₃ M₃] open Submodule variable {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variable [RingHomCompTriple τ₁₂ τ₂₃ τ₁₃] section variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. See Note [range copy pattern]. -/ def range [RingHomSurjective τ₁₂] (f : F) : Submodule R₂ M₂ := (map f ⊤).copy (Set.range f) Set.image_univ.symm theorem range_coe [RingHomSurjective τ₁₂] (f : F) : (range f : Set M₂) = Set.range f := rfl theorem range_toAddSubmonoid [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : (range f).toAddSubmonoid = AddMonoidHom.mrange f := rfl @[simp] theorem mem_range [RingHomSurjective τ₁₂] {f : F} {x} : x ∈ range f ↔ ∃ y, f y = x := Iff.rfl theorem range_eq_map [RingHomSurjective τ₁₂] (f : F) : range f = map f ⊤ := by ext simp theorem mem_range_self [RingHomSurjective τ₁₂] (f : F) (x : M) : f x ∈ range f := ⟨x, rfl⟩ @[simp] theorem range_id : range (LinearMap.id : M →ₗ[R] M) = ⊤ := SetLike.coe_injective Set.range_id theorem range_comp [RingHomSurjective τ₁₂] [RingHomSurjective τ₂₃] [RingHomSurjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = map g (range f) := SetLike.coe_injective (Set.range_comp g f) theorem range_comp_le_range [RingHomSurjective τ₂₃] [RingHomSurjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) ≤ range g := SetLike.coe_mono (Set.range_comp_subset_range f g) theorem range_eq_top [RingHomSurjective τ₁₂] {f : F} : range f = ⊤ ↔ Surjective f := by rw [SetLike.ext'_iff, range_coe, top_coe, Set.range_eq_univ] theorem range_eq_top_of_surjective [RingHomSurjective τ₁₂] (f : F) (hf : Surjective f) : range f = ⊤ := range_eq_top.2 hf theorem range_le_iff_comap [RingHomSurjective τ₁₂] {f : F} {p : Submodule R₂ M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range_eq_map, map_le_iff_le_comap, eq_top_iff] theorem map_le_range [RingHomSurjective τ₁₂] {f : F} {p : Submodule R M} : map f p ≤ range f := SetLike.coe_mono (Set.image_subset_range f p) @[simp] theorem range_neg {R : Type*} {R₂ : Type*} {M : Type*} {M₂ : Type*} [Semiring R] [Ring R₂] [AddCommMonoid M] [AddCommGroup M₂] [Module R M] [Module R₂ M₂] {τ₁₂ : R →+* R₂} [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : LinearMap.range (-f) = LinearMap.range f := by change range ((-LinearMap.id : M₂ →ₗ[R₂] M₂).comp f) = _ rw [range_comp, Submodule.map_neg, Submodule.map_id] @[simp] lemma range_domRestrict [Module R M₂] (K : Submodule R M) (f : M →ₗ[R] M₂) : range (domRestrict f K) = K.map f := by ext; simp lemma range_domRestrict_le_range [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) (S : Submodule R M) : LinearMap.range (f.domRestrict S) ≤ LinearMap.range f := by rintro x ⟨⟨y, hy⟩, rfl⟩ exact LinearMap.mem_range_self f y @[simp] theorem _root_.AddMonoidHom.coe_toIntLinearMap_range {M M₂ : Type*} [AddCommGroup M] [AddCommGroup M₂] (f : M →+ M₂) : LinearMap.range f.toIntLinearMap = AddSubgroup.toIntSubmodule f.range := rfl lemma _root_.Submodule.map_comap_eq_of_le [RingHomSurjective τ₁₂] {f : F} {p : Submodule R₂ M₂} (h : p ≤ LinearMap.range f) : (p.comap f).map f = p := SetLike.coe_injective <| Set.image_preimage_eq_of_subset h lemma range_restrictScalars [SMul R R₂] [Module R₂ M] [Module R M₂] [CompatibleSMul M M₂ R R₂] [IsScalarTower R R₂ M₂] (f : M →ₗ[R₂] M₂) : LinearMap.range (f.restrictScalars R) = (LinearMap.range f).restrictScalars R := rfl end /-- The decreasing sequence of submodules consisting of the ranges of the iterates of a linear map. -/ @[simps] def iterateRange (f : M →ₗ[R] M) : ℕ →o (Submodule R M)ᵒᵈ where toFun n := LinearMap.range (f ^ n) monotone' n m w x h := by obtain ⟨c, rfl⟩ := Nat.exists_eq_add_of_le w rw [LinearMap.mem_range] at h obtain ⟨m, rfl⟩ := h rw [LinearMap.mem_range] use (f ^ c) m rw [pow_add, Module.End.mul_apply] /-- Restrict the codomain of a linear map `f` to `f.range`. This is the bundled version of `Set.rangeFactorization`. -/ abbrev rangeRestrict [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : M →ₛₗ[τ₁₂] LinearMap.range f := f.codRestrict (LinearMap.range f) (LinearMap.mem_range_self f) /-- The range of a linear map is finite if the domain is finite. Note: this instance can form a diamond with `Subtype.fintype` in the presence of `Fintype M₂`. -/ instance fintypeRange [Fintype M] [DecidableEq M₂] [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : Fintype (range f) := Set.fintypeRange f variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] theorem range_codRestrict {τ₂₁ : R₂ →+* R} [RingHomSurjective τ₂₁] (p : Submodule R M) (f : M₂ →ₛₗ[τ₂₁] M) (hf) : range (codRestrict p f hf) = comap p.subtype (LinearMap.range f) := by simpa only [range_eq_map] using map_codRestrict _ _ _ _ theorem _root_.Submodule.map_comap_eq [RingHomSurjective τ₁₂] (f : F) (q : Submodule R₂ M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf map_le_range (map_comap_le _ _)) <| by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ theorem _root_.Submodule.map_comap_eq_self [RingHomSurjective τ₁₂] {f : F} {q : Submodule R₂ M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [Submodule.map_comap_eq, inf_eq_right] @[simp] theorem range_zero [RingHomSurjective τ₁₂] : range (0 : M →ₛₗ[τ₁₂] M₂) = ⊥ := by simpa only [range_eq_map] using Submodule.map_zero _ section variable [RingHomSurjective τ₁₂] theorem range_le_bot_iff (f : M →ₛₗ[τ₁₂] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem range_eq_bot {f : M →ₛₗ[τ₁₂] M₂} : range f = ⊥ ↔ f = 0 := by rw [← range_le_bot_iff, le_bot_iff] theorem range_le_ker_iff {f : M →ₛₗ[τ₁₂] M₂} {g : M₂ →ₛₗ[τ₂₃] M₃} : range f ≤ ker g ↔ (g.comp f : M →ₛₗ[τ₁₃] M₃) = 0 := ⟨fun h => ker_eq_top.1 <| eq_top_iff'.2 fun _ => h <| ⟨_, rfl⟩, fun h x hx => mem_ker.2 <| Exists.elim hx fun y hy => by rw [← hy, ← comp_apply, h, zero_apply]⟩ theorem comap_le_comap_iff {f : F} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨fun H ↦ by rwa [SetLike.le_def, (range_eq_top.1 hf).forall], comap_mono⟩ theorem comap_injective {f : F} (hf : range f = ⊤) : Injective (comap f) := fun _ _ h => le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) -- TODO (?): generalize to semilinear maps with `f ∘ₗ g` bijective. theorem ker_eq_range_of_comp_eq_id {M P} [AddCommGroup M] [Module R M] [AddCommGroup P] [Module R P] {f : M →ₗ[R] P} {g : P →ₗ[R] M} (h : f ∘ₗ g = .id) : ker f = range (LinearMap.id - g ∘ₗ f) := le_antisymm (fun x hx ↦ ⟨x, show x - g (f x) = x by rw [hx, map_zero, sub_zero]⟩) <| range_le_ker_iff.mpr <| by rw [comp_sub, comp_id, ← comp_assoc, h, id_comp, sub_self] end end AddCommMonoid section Ring variable [Ring R] [Ring R₂] variable [AddCommGroup M] [AddCommGroup M₂] variable [Module R M] [Module R₂ M₂] variable {τ₁₂ : R →+* R₂} variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] variable {f : F} open Submodule theorem range_toAddSubgroup [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : (range f).toAddSubgroup = f.toAddMonoidHom.range := rfl theorem ker_le_iff [RingHomSurjective τ₁₂] {p : Submodule R M} : ker f ≤ p ↔ ∃ y ∈ range f, f ⁻¹' {y} ⊆ p := by constructor · intro h use 0 rw [← SetLike.mem_coe, range_coe] exact ⟨⟨0, map_zero f⟩, h⟩ · rintro ⟨y, h₁, h₂⟩ rw [SetLike.le_def] intro z hz simp only [mem_ker, SetLike.mem_coe] at hz rw [← SetLike.mem_coe, range_coe, Set.mem_range] at h₁ obtain ⟨x, hx⟩ := h₁ have hx' : x ∈ p := h₂ hx have hxz : z + x ∈ p := by apply h₂ simp [hx, hz] suffices z + x - x ∈ p by simpa only [this, add_sub_cancel_right] exact p.sub_mem hxz hx' end Ring section Semifield variable [Semifield K] variable [AddCommMonoid V] [Module K V] variable [AddCommMonoid V₂] [Module K V₂] theorem range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := by simpa only [range_eq_map] using Submodule.map_smul f _ a h theorem range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆ _ : a ≠ 0, range f := by simpa only [range_eq_map] using Submodule.map_smul' f _ a end Semifield end LinearMap namespace Submodule section AddCommMonoid variable [Semiring R] [Semiring R₂] [AddCommMonoid M] [AddCommMonoid M₂] variable [Module R M] [Module R₂ M₂] variable (p : Submodule R M) variable {τ₁₂ : R →+* R₂} variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F τ₁₂ M M₂] open LinearMap @[simp] theorem map_top [RingHomSurjective τ₁₂] (f : F) : map f ⊤ = range f := (range_eq_map f).symm @[simp] theorem range_subtype : range p.subtype = p := by simpa using map_comap_subtype p ⊤ theorem map_subtype_le (p' : Submodule R p) : map p.subtype p' ≤ p := by simpa using (map_le_range : map p.subtype p' ≤ range p.subtype) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p`. -/ theorem map_subtype_top : map p.subtype (⊤ : Submodule R p) = p := by simp @[simp] theorem comap_subtype_eq_top {p p' : Submodule R M} : comap p.subtype p' = ⊤ ↔ p ≤ p' := eq_top_iff.trans <| map_le_iff_le_comap.symm.trans <| by rw [map_subtype_top] @[simp] theorem comap_subtype_self : comap p.subtype p = ⊤ := comap_subtype_eq_top.2 le_rfl @[simp] lemma comap_subtype_le_iff {p q r : Submodule R M} : q.comap p.subtype ≤ r.comap p.subtype ↔ p ⊓ q ≤ p ⊓ r := ⟨fun h ↦ by simpa using map_mono (f := p.subtype) h, fun h ↦ by simpa using comap_mono (f := p.subtype) h⟩ theorem range_inclusion (p q : Submodule R M) (h : p ≤ q) : range (inclusion h) = comap q.subtype p := by rw [← map_top, inclusion, LinearMap.map_codRestrict, map_top, range_subtype] @[simp] theorem map_subtype_range_inclusion {p p' : Submodule R M} (h : p ≤ p') : map p'.subtype (range <| inclusion h) = p := by simp [range_inclusion, map_comap_eq, h] lemma restrictScalars_map [SMul R R₂] [Module R₂ M] [Module R M₂] [IsScalarTower R R₂ M] [IsScalarTower R R₂ M₂] (f : M →ₗ[R₂] M₂) (M' : Submodule R₂ M) : (M'.map f).restrictScalars R = (M'.restrictScalars R).map (f.restrictScalars R) := rfl /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N`. See also `Submodule.mapIic`. -/ def MapSubtype.relIso : Submodule R p ≃o { p' : Submodule R M // p' ≤ p } where toFun p' := ⟨map p.subtype p', map_subtype_le p _⟩ invFun q := comap p.subtype q left_inv p' := comap_map_eq_of_injective (by exact Subtype.val_injective) p' right_inv := fun ⟨q, hq⟩ => Subtype.ext_val <| by simp [map_comap_subtype p, inf_of_le_right hq] map_rel_iff' {p₁ p₂} := Subtype.coe_le_coe.symm.trans <| by dsimp rw [map_le_iff_le_comap, comap_map_eq_of_injective (show Injective p.subtype from Subtype.coe_injective) p₂] /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def MapSubtype.orderEmbedding : Submodule R p ↪o Submodule R M := (RelIso.toRelEmbedding <| MapSubtype.relIso p).trans <| Subtype.relEmbedding (X := Submodule R M) (fun p p' ↦ p ≤ p') _ @[simp] theorem map_subtype_embedding_eq (p' : Submodule R p) : MapSubtype.orderEmbedding p p' = map p.subtype p' := rfl /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N`. -/ def mapIic (p : Submodule R M) : Submodule R p ≃o Set.Iic p := Submodule.MapSubtype.relIso p @[simp] lemma coe_mapIic_apply (p : Submodule R M) (q : Submodule R p) : (p.mapIic q : Submodule R M) = q.map p.subtype := rfl end AddCommMonoid end Submodule namespace LinearMap section Semiring variable [Semiring R] [Semiring R₂] [Semiring R₃] variable [AddCommMonoid M] [AddCommMonoid M₂] [AddCommMonoid M₃] variable [Module R M] [Module R₂ M₂] [Module R₃ M₃] variable {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variable [RingHomCompTriple τ₁₂ τ₂₃ τ₁₃] /-- A monomorphism is injective. -/ theorem ker_eq_bot_of_cancel {f : M →ₛₗ[τ₁₂] M₂} (h : ∀ u v : ker f →ₗ[R] M, f.comp u = f.comp v → u = v) : ker f = ⊥ := by have h₁ : f.comp (0 : ker f →ₗ[R] M) = 0 := comp_zero _ rw [← Submodule.range_subtype (ker f), ← h 0 (ker f).subtype (Eq.trans h₁ (comp_ker_subtype f).symm)] exact range_zero theorem range_comp_of_range_eq_top [RingHomSurjective τ₁₂] [RingHomSurjective τ₂₃] [RingHomSurjective τ₁₃] {f : M →ₛₗ[τ₁₂] M₂} (g : M₂ →ₛₗ[τ₂₃] M₃) (hf : range f = ⊤) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = range g := by rw [range_comp, hf, Submodule.map_top] section Image /-- If `O` is a submodule of `M`, and `Φ : O →ₗ M'` is a linear map, then `(ϕ : O →ₗ M').submoduleImage N` is `ϕ(N)` as a submodule of `M'` -/ def submoduleImage {M' : Type*} [AddCommMonoid M'] [Module R M'] {O : Submodule R M} (ϕ : O →ₗ[R] M') (N : Submodule R M) : Submodule R M' := (N.comap O.subtype).map ϕ @[simp] theorem mem_submoduleImage {M' : Type*} [AddCommMonoid M'] [Module R M'] {O : Submodule R M} {ϕ : O →ₗ[R] M'} {N : Submodule R M} {x : M'} : x ∈ ϕ.submoduleImage N ↔ ∃ (y : _) (yO : y ∈ O), y ∈ N ∧ ϕ ⟨y, yO⟩ = x := by refine Submodule.mem_map.trans ⟨?_, ?_⟩ <;> simp_rw [Submodule.mem_comap] · rintro ⟨⟨y, yO⟩, yN : y ∈ N, h⟩ exact ⟨y, yO, yN, h⟩ · rintro ⟨y, yO, yN, h⟩ exact ⟨⟨y, yO⟩, yN, h⟩ theorem mem_submoduleImage_of_le {M' : Type*} [AddCommMonoid M'] [Module R M'] {O : Submodule R M} {ϕ : O →ₗ[R] M'} {N : Submodule R M} (hNO : N ≤ O) {x : M'} : x ∈ ϕ.submoduleImage N ↔ ∃ (y : _) (yN : y ∈ N), ϕ ⟨y, hNO yN⟩ = x := by refine mem_submoduleImage.trans ⟨?_, ?_⟩ · rintro ⟨y, yO, yN, h⟩ exact ⟨y, yN, h⟩ · rintro ⟨y, yN, h⟩ exact ⟨y, hNO yN, yN, h⟩ theorem submoduleImage_apply_of_le {M' : Type*} [AddCommMonoid M'] [Module R M'] {O : Submodule R M} (ϕ : O →ₗ[R] M') (N : Submodule R M) (hNO : N ≤ O) : ϕ.submoduleImage N = range (ϕ.comp (Submodule.inclusion hNO)) := by rw [submoduleImage, range_comp, Submodule.range_inclusion] end Image section rangeRestrict variable [RingHomSurjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) @[simp] theorem range_rangeRestrict : range f.rangeRestrict = ⊤ := by simp [f.range_codRestrict _] theorem surjective_rangeRestrict : Surjective f.rangeRestrict := by rw [← range_eq_top, range_rangeRestrict] @[simp] theorem ker_rangeRestrict : ker f.rangeRestrict = ker f := LinearMap.ker_codRestrict _ _ _ @[simp] theorem injective_rangeRestrict_iff : Injective f.rangeRestrict ↔ Injective f := Set.injective_codRestrict _
end rangeRestrict end Semiring
Mathlib/Algebra/Module/Submodule/Range.lean
422
425
/- Copyright (c) 2022 Moritz Doll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Moritz Doll -/ import Mathlib.Analysis.LocallyConvex.BalancedCoreHull import Mathlib.Analysis.Convex.TotallyBounded /-! # Absolutely convex sets A set `s` in an commutative monoid `E` is called absolutely convex or disked if it is convex and balanced. The importance of absolutely convex sets comes from the fact that every locally convex topological vector space has a basis consisting of absolutely convex sets. ## Main definitions * `absConvexHull`: the absolutely convex hull of a set `s` is the smallest absolutely convex set containing `s`; * `closedAbsConvexHull`: the closed absolutely convex hull of a set `s` is the smallest absolutely convex set containing `s`; ## Main statements * `absConvexHull_eq_convexHull_balancedHull`: when the locally convex space is a module, the absolutely convex hull of a set `s` equals the convex hull of the balanced hull of `s`; * `convexHull_union_neg_eq_absConvexHull`: the convex hull of `s ∪ -s` is the absolutely convex hull of `s`; * `closedAbsConvexHull_closure_eq_closedAbsConvexHull` : the closed absolutely convex hull of the closure of `s` equals the closed absolutely convex hull of `s`; ## Implementation notes Mathlib's definition of `Convex` requires the scalars to be an `OrderedSemiring` whereas the definition of `Balanced` requires the scalars to be a `SeminormedRing`. Mathlib doesn't currently have a concept of a semi-normed ordered ring, so we define a set as `AbsConvex` if it is balanced over a `SeminormedRing` `𝕜` and convex over `ℝ`, assuming `IsScalarTower ℝ 𝕜 E` and `SMulCommClass ℝ 𝕜 E` where required. ## Tags disks, convex, balanced -/ open NormedField Set open NNReal Pointwise Topology variable {𝕜 E : Type*} section AbsolutelyConvex
variable (𝕜) [SeminormedRing 𝕜] [SMul 𝕜 E] [SMul ℝ E] [AddCommMonoid E] /-- A set is absolutely convex if it is balanced and convex. Mathlib's definition of `Convex` requires the scalars to be an `OrderedSemiring` whereas the definition of `Balanced` requires the scalars to be a `SeminormedRing`. Mathlib doesn't currently have a concept of a semi-normed ordered ring, so we define a set as `AbsConvex` if it is balanced over a `SeminormedRing` `𝕜` and convex over `ℝ`. -/ def AbsConvex (s : Set E) : Prop := Balanced 𝕜 s ∧ Convex ℝ s
Mathlib/Analysis/LocallyConvex/AbsConvex.lean
52
60
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Yury Kudryashov, Sébastien Gouëzel, Chris Hughes -/ import Mathlib.Data.Fin.Rev import Mathlib.Data.Nat.Find /-! # Operation on tuples We interpret maps `∀ i : Fin n, α i` as `n`-tuples of elements of possibly varying type `α i`, `(α 0, …, α (n-1))`. A particular case is `Fin n → α` of elements with all the same type. In this case when `α i` is a constant map, then tuples are isomorphic (but not definitionally equal) to `Vector`s. ## Main declarations There are three (main) ways to consider `Fin n` as a subtype of `Fin (n + 1)`, hence three (main) ways to move between tuples of length `n` and of length `n + 1` by adding/removing an entry. ### Adding at the start * `Fin.succ`: Send `i : Fin n` to `i + 1 : Fin (n + 1)`. This is defined in Core. * `Fin.cases`: Induction/recursion principle for `Fin`: To prove a property/define a function for all `Fin (n + 1)`, it is enough to prove/define it for `0` and for `i.succ` for all `i : Fin n`. This is defined in Core. * `Fin.cons`: Turn a tuple `f : Fin n → α` and an entry `a : α` into a tuple `Fin.cons a f : Fin (n + 1) → α` by adding `a` at the start. In general, tuples can be dependent functions, in which case `f : ∀ i : Fin n, α i.succ` and `a : α 0`. This is a special case of `Fin.cases`. * `Fin.tail`: Turn a tuple `f : Fin (n + 1) → α` into a tuple `Fin.tail f : Fin n → α` by forgetting the start. In general, tuples can be dependent functions, in which case `Fin.tail f : ∀ i : Fin n, α i.succ`. ### Adding at the end * `Fin.castSucc`: Send `i : Fin n` to `i : Fin (n + 1)`. This is defined in Core. * `Fin.lastCases`: Induction/recursion principle for `Fin`: To prove a property/define a function for all `Fin (n + 1)`, it is enough to prove/define it for `last n` and for `i.castSucc` for all `i : Fin n`. This is defined in Core. * `Fin.snoc`: Turn a tuple `f : Fin n → α` and an entry `a : α` into a tuple `Fin.snoc f a : Fin (n + 1) → α` by adding `a` at the end. In general, tuples can be dependent functions, in which case `f : ∀ i : Fin n, α i.castSucc` and `a : α (last n)`. This is a special case of `Fin.lastCases`. * `Fin.init`: Turn a tuple `f : Fin (n + 1) → α` into a tuple `Fin.init f : Fin n → α` by forgetting the start. In general, tuples can be dependent functions, in which case `Fin.init f : ∀ i : Fin n, α i.castSucc`. ### Adding in the middle For a **pivot** `p : Fin (n + 1)`, * `Fin.succAbove`: Send `i : Fin n` to * `i : Fin (n + 1)` if `i < p`, * `i + 1 : Fin (n + 1)` if `p ≤ i`. * `Fin.succAboveCases`: Induction/recursion principle for `Fin`: To prove a property/define a function for all `Fin (n + 1)`, it is enough to prove/define it for `p` and for `p.succAbove i` for all `i : Fin n`. * `Fin.insertNth`: Turn a tuple `f : Fin n → α` and an entry `a : α` into a tuple `Fin.insertNth f a : Fin (n + 1) → α` by adding `a` in position `p`. In general, tuples can be dependent functions, in which case `f : ∀ i : Fin n, α (p.succAbove i)` and `a : α p`. This is a special case of `Fin.succAboveCases`. * `Fin.removeNth`: Turn a tuple `f : Fin (n + 1) → α` into a tuple `Fin.removeNth p f : Fin n → α` by forgetting the `p`-th value. In general, tuples can be dependent functions, in which case `Fin.removeNth f : ∀ i : Fin n, α (succAbove p i)`. `p = 0` means we add at the start. `p = last n` means we add at the end. ### Miscellaneous * `Fin.find p` : returns the first index `n` where `p n` is satisfied, and `none` if it is never satisfied. * `Fin.append a b` : append two tuples. * `Fin.repeat n a` : repeat a tuple `n` times. -/ assert_not_exists Monoid universe u v namespace Fin variable {m n : ℕ} open Function section Tuple /-- There is exactly one tuple of size zero. -/ example (α : Fin 0 → Sort u) : Unique (∀ i : Fin 0, α i) := by infer_instance theorem tuple0_le {α : Fin 0 → Type*} [∀ i, Preorder (α i)] (f g : ∀ i, α i) : f ≤ g := finZeroElim variable {α : Fin (n + 1) → Sort u} (x : α 0) (q : ∀ i, α i) (p : ∀ i : Fin n, α i.succ) (i : Fin n) (y : α i.succ) (z : α 0) /-- The tail of an `n+1` tuple, i.e., its last `n` entries. -/ def tail (q : ∀ i, α i) : ∀ i : Fin n, α i.succ := fun i ↦ q i.succ theorem tail_def {n : ℕ} {α : Fin (n + 1) → Sort*} {q : ∀ i, α i} : (tail fun k : Fin (n + 1) ↦ q k) = fun k : Fin n ↦ q k.succ := rfl /-- Adding an element at the beginning of an `n`-tuple, to get an `n+1`-tuple. -/ def cons (x : α 0) (p : ∀ i : Fin n, α i.succ) : ∀ i, α i := fun j ↦ Fin.cases x p j @[simp] theorem tail_cons : tail (cons x p) = p := by simp +unfoldPartialApp [tail, cons] @[simp] theorem cons_succ : cons x p i.succ = p i := by simp [cons] @[simp] theorem cons_zero : cons x p 0 = x := by simp [cons] @[simp] theorem cons_one {α : Fin (n + 2) → Sort*} (x : α 0) (p : ∀ i : Fin n.succ, α i.succ) : cons x p 1 = p 0 := by rw [← cons_succ x p]; rfl /-- Updating a tuple and adding an element at the beginning commute. -/ @[simp] theorem cons_update : cons x (update p i y) = update (cons x p) i.succ y := by ext j by_cases h : j = 0 · rw [h] simp [Ne.symm (succ_ne_zero i)] · let j' := pred j h have : j'.succ = j := succ_pred j h rw [← this, cons_succ] by_cases h' : j' = i · rw [h'] simp · have : j'.succ ≠ i.succ := by rwa [Ne, succ_inj] rw [update_of_ne h', update_of_ne this, cons_succ] /-- As a binary function, `Fin.cons` is injective. -/ theorem cons_injective2 : Function.Injective2 (@cons n α) := fun x₀ y₀ x y h ↦ ⟨congr_fun h 0, funext fun i ↦ by simpa using congr_fun h (Fin.succ i)⟩ @[simp] theorem cons_inj {x₀ y₀ : α 0} {x y : ∀ i : Fin n, α i.succ} : cons x₀ x = cons y₀ y ↔ x₀ = y₀ ∧ x = y := cons_injective2.eq_iff theorem cons_left_injective (x : ∀ i : Fin n, α i.succ) : Function.Injective fun x₀ ↦ cons x₀ x := cons_injective2.left _ theorem cons_right_injective (x₀ : α 0) : Function.Injective (cons x₀) := cons_injective2.right _ /-- Adding an element at the beginning of a tuple and then updating it amounts to adding it directly. -/ theorem update_cons_zero : update (cons x p) 0 z = cons z p := by ext j by_cases h : j = 0 · rw [h] simp · simp only [h, update_of_ne, Ne, not_false_iff] let j' := pred j h have : j'.succ = j := succ_pred j h rw [← this, cons_succ, cons_succ] /-- Concatenating the first element of a tuple with its tail gives back the original tuple -/ @[simp] theorem cons_self_tail : cons (q 0) (tail q) = q := by ext j by_cases h : j = 0 · rw [h] simp · let j' := pred j h have : j'.succ = j := succ_pred j h rw [← this] unfold tail rw [cons_succ] /-- Equivalence between tuples of length `n + 1` and pairs of an element and a tuple of length `n` given by separating out the first element of the tuple. This is `Fin.cons` as an `Equiv`. -/ @[simps] def consEquiv (α : Fin (n + 1) → Type*) : α 0 × (∀ i, α (succ i)) ≃ ∀ i, α i where toFun f := cons f.1 f.2 invFun f := (f 0, tail f) left_inv f := by simp right_inv f := by simp /-- Recurse on an `n+1`-tuple by splitting it into a single element and an `n`-tuple. -/ @[elab_as_elim] def consCases {P : (∀ i : Fin n.succ, α i) → Sort v} (h : ∀ x₀ x, P (Fin.cons x₀ x)) (x : ∀ i : Fin n.succ, α i) : P x := _root_.cast (by rw [cons_self_tail]) <| h (x 0) (tail x) @[simp] theorem consCases_cons {P : (∀ i : Fin n.succ, α i) → Sort v} (h : ∀ x₀ x, P (Fin.cons x₀ x)) (x₀ : α 0) (x : ∀ i : Fin n, α i.succ) : @consCases _ _ _ h (cons x₀ x) = h x₀ x := by rw [consCases, cast_eq] congr /-- Recurse on a tuple by splitting into `Fin.elim0` and `Fin.cons`. -/ @[elab_as_elim] def consInduction {α : Sort*} {P : ∀ {n : ℕ}, (Fin n → α) → Sort v} (h0 : P Fin.elim0) (h : ∀ {n} (x₀) (x : Fin n → α), P x → P (Fin.cons x₀ x)) : ∀ {n : ℕ} (x : Fin n → α), P x | 0, x => by convert h0 | _ + 1, x => consCases (fun _ _ ↦ h _ _ <| consInduction h0 h _) x theorem cons_injective_of_injective {α} {x₀ : α} {x : Fin n → α} (hx₀ : x₀ ∉ Set.range x) (hx : Function.Injective x) : Function.Injective (cons x₀ x : Fin n.succ → α) := by refine Fin.cases ?_ ?_ · refine Fin.cases ?_ ?_ · intro rfl · intro j h rw [cons_zero, cons_succ] at h exact hx₀.elim ⟨_, h.symm⟩ · intro i refine Fin.cases ?_ ?_ · intro h rw [cons_zero, cons_succ] at h exact hx₀.elim ⟨_, h⟩ · intro j h rw [cons_succ, cons_succ] at h exact congr_arg _ (hx h) theorem cons_injective_iff {α} {x₀ : α} {x : Fin n → α} : Function.Injective (cons x₀ x : Fin n.succ → α) ↔ x₀ ∉ Set.range x ∧ Function.Injective x := by refine ⟨fun h ↦ ⟨?_, ?_⟩, fun h ↦ cons_injective_of_injective h.1 h.2⟩ · rintro ⟨i, hi⟩ replace h := @h i.succ 0 simp [hi] at h · simpa [Function.comp] using h.comp (Fin.succ_injective _) @[simp] theorem forall_fin_zero_pi {α : Fin 0 → Sort*} {P : (∀ i, α i) → Prop} : (∀ x, P x) ↔ P finZeroElim := ⟨fun h ↦ h _, fun h x ↦ Subsingleton.elim finZeroElim x ▸ h⟩ @[simp] theorem exists_fin_zero_pi {α : Fin 0 → Sort*} {P : (∀ i, α i) → Prop} : (∃ x, P x) ↔ P finZeroElim := ⟨fun ⟨x, h⟩ ↦ Subsingleton.elim x finZeroElim ▸ h, fun h ↦ ⟨_, h⟩⟩ theorem forall_fin_succ_pi {P : (∀ i, α i) → Prop} : (∀ x, P x) ↔ ∀ a v, P (Fin.cons a v) := ⟨fun h a v ↦ h (Fin.cons a v), consCases⟩ theorem exists_fin_succ_pi {P : (∀ i, α i) → Prop} : (∃ x, P x) ↔ ∃ a v, P (Fin.cons a v) := ⟨fun ⟨x, h⟩ ↦ ⟨x 0, tail x, (cons_self_tail x).symm ▸ h⟩, fun ⟨_, _, h⟩ ↦ ⟨_, h⟩⟩ /-- Updating the first element of a tuple does not change the tail. -/ @[simp] theorem tail_update_zero : tail (update q 0 z) = tail q := by ext j simp [tail] /-- Updating a nonzero element and taking the tail commute. -/ @[simp] theorem tail_update_succ : tail (update q i.succ y) = update (tail q) i y := by ext j by_cases h : j = i · rw [h] simp [tail] · simp [tail, (Fin.succ_injective n).ne h, h] theorem comp_cons {α : Sort*} {β : Sort*} (g : α → β) (y : α) (q : Fin n → α) : g ∘ cons y q = cons (g y) (g ∘ q) := by ext j by_cases h : j = 0 · rw [h] rfl · let j' := pred j h have : j'.succ = j := succ_pred j h rw [← this, cons_succ, comp_apply, comp_apply, cons_succ] theorem comp_tail {α : Sort*} {β : Sort*} (g : α → β) (q : Fin n.succ → α) : g ∘ tail q = tail (g ∘ q) := by ext j simp [tail] section Preorder variable {α : Fin (n + 1) → Type*} theorem le_cons [∀ i, Preorder (α i)] {x : α 0} {q : ∀ i, α i} {p : ∀ i : Fin n, α i.succ} : q ≤ cons x p ↔ q 0 ≤ x ∧ tail q ≤ p := forall_fin_succ.trans <| and_congr Iff.rfl <| forall_congr' fun j ↦ by simp [tail] theorem cons_le [∀ i, Preorder (α i)] {x : α 0} {q : ∀ i, α i} {p : ∀ i : Fin n, α i.succ} : cons x p ≤ q ↔ x ≤ q 0 ∧ p ≤ tail q := @le_cons _ (fun i ↦ (α i)ᵒᵈ) _ x q p theorem cons_le_cons [∀ i, Preorder (α i)] {x₀ y₀ : α 0} {x y : ∀ i : Fin n, α i.succ} : cons x₀ x ≤ cons y₀ y ↔ x₀ ≤ y₀ ∧ x ≤ y := forall_fin_succ.trans <| and_congr_right' <| by simp only [cons_succ, Pi.le_def] end Preorder theorem range_fin_succ {α} (f : Fin (n + 1) → α) : Set.range f = insert (f 0) (Set.range (Fin.tail f)) := Set.ext fun _ ↦ exists_fin_succ.trans <| eq_comm.or Iff.rfl @[simp] theorem range_cons {α} {n : ℕ} (x : α) (b : Fin n → α) : Set.range (Fin.cons x b : Fin n.succ → α) = insert x (Set.range b) := by rw [range_fin_succ, cons_zero, tail_cons] section Append variable {α : Sort*} /-- Append a tuple of length `m` to a tuple of length `n` to get a tuple of length `m + n`. This is a non-dependent version of `Fin.add_cases`. -/ def append (a : Fin m → α) (b : Fin n → α) : Fin (m + n) → α := @Fin.addCases _ _ (fun _ => α) a b @[simp] theorem append_left (u : Fin m → α) (v : Fin n → α) (i : Fin m) : append u v (Fin.castAdd n i) = u i := addCases_left _ @[simp] theorem append_right (u : Fin m → α) (v : Fin n → α) (i : Fin n) : append u v (natAdd m i) = v i := addCases_right _ theorem append_right_nil (u : Fin m → α) (v : Fin n → α) (hv : n = 0) : append u v = u ∘ Fin.cast (by rw [hv, Nat.add_zero]) := by refine funext (Fin.addCases (fun l => ?_) fun r => ?_) · rw [append_left, Function.comp_apply] refine congr_arg u (Fin.ext ?_) simp · exact (Fin.cast hv r).elim0 @[simp] theorem append_elim0 (u : Fin m → α) : append u Fin.elim0 = u ∘ Fin.cast (Nat.add_zero _) := append_right_nil _ _ rfl theorem append_left_nil (u : Fin m → α) (v : Fin n → α) (hu : m = 0) : append u v = v ∘ Fin.cast (by rw [hu, Nat.zero_add]) := by refine funext (Fin.addCases (fun l => ?_) fun r => ?_) · exact (Fin.cast hu l).elim0 · rw [append_right, Function.comp_apply] refine congr_arg v (Fin.ext ?_) simp [hu] @[simp] theorem elim0_append (v : Fin n → α) : append Fin.elim0 v = v ∘ Fin.cast (Nat.zero_add _) := append_left_nil _ _ rfl theorem append_assoc {p : ℕ} (a : Fin m → α) (b : Fin n → α) (c : Fin p → α) : append (append a b) c = append a (append b c) ∘ Fin.cast (Nat.add_assoc ..) := by ext i rw [Function.comp_apply] refine Fin.addCases (fun l => ?_) (fun r => ?_) i · rw [append_left] refine Fin.addCases (fun ll => ?_) (fun lr => ?_) l · rw [append_left] simp [castAdd_castAdd] · rw [append_right] simp [castAdd_natAdd] · rw [append_right] simp [← natAdd_natAdd] /-- Appending a one-tuple to the left is the same as `Fin.cons`. -/ theorem append_left_eq_cons {n : ℕ} (x₀ : Fin 1 → α) (x : Fin n → α) : Fin.append x₀ x = Fin.cons (x₀ 0) x ∘ Fin.cast (Nat.add_comm ..) := by ext i refine Fin.addCases ?_ ?_ i <;> clear i · intro i rw [Subsingleton.elim i 0, Fin.append_left, Function.comp_apply, eq_comm] exact Fin.cons_zero _ _ · intro i rw [Fin.append_right, Function.comp_apply, Fin.cast_natAdd, eq_comm, Fin.addNat_one] exact Fin.cons_succ _ _ _ /-- `Fin.cons` is the same as appending a one-tuple to the left. -/ theorem cons_eq_append (x : α) (xs : Fin n → α) : cons x xs = append (cons x Fin.elim0) xs ∘ Fin.cast (Nat.add_comm ..) := by funext i; simp [append_left_eq_cons] @[simp] lemma append_cast_left {n m} (xs : Fin n → α) (ys : Fin m → α) (n' : ℕ) (h : n' = n) : Fin.append (xs ∘ Fin.cast h) ys = Fin.append xs ys ∘ (Fin.cast <| by rw [h]) := by subst h; simp @[simp] lemma append_cast_right {n m} (xs : Fin n → α) (ys : Fin m → α) (m' : ℕ) (h : m' = m) : Fin.append xs (ys ∘ Fin.cast h) = Fin.append xs ys ∘ (Fin.cast <| by rw [h]) := by subst h; simp lemma append_rev {m n} (xs : Fin m → α) (ys : Fin n → α) (i : Fin (m + n)) : append xs ys (rev i) = append (ys ∘ rev) (xs ∘ rev) (i.cast (Nat.add_comm ..)) := by rcases rev_surjective i with ⟨i, rfl⟩ rw [rev_rev] induction i using Fin.addCases · simp [rev_castAdd] · simp [cast_rev, rev_addNat] lemma append_comp_rev {m n} (xs : Fin m → α) (ys : Fin n → α) : append xs ys ∘ rev = append (ys ∘ rev) (xs ∘ rev) ∘ Fin.cast (Nat.add_comm ..) := funext <| append_rev xs ys theorem append_castAdd_natAdd {f : Fin (m + n) → α} : append (fun i ↦ f (castAdd n i)) (fun i ↦ f (natAdd m i)) = f := by unfold append addCases simp end Append section Repeat variable {α : Sort*} /-- Repeat `a` `m` times. For example `Fin.repeat 2 ![0, 3, 7] = ![0, 3, 7, 0, 3, 7]`. -/ def «repeat» (m : ℕ) (a : Fin n → α) : Fin (m * n) → α | i => a i.modNat @[simp] theorem repeat_apply (a : Fin n → α) (i : Fin (m * n)) : Fin.repeat m a i = a i.modNat := rfl @[simp] theorem repeat_zero (a : Fin n → α) : Fin.repeat 0 a = Fin.elim0 ∘ Fin.cast (Nat.zero_mul _) := funext fun x => (x.cast (Nat.zero_mul _)).elim0 @[simp] theorem repeat_one (a : Fin n → α) : Fin.repeat 1 a = a ∘ Fin.cast (Nat.one_mul _) := by generalize_proofs h apply funext rw [(Fin.rightInverse_cast h.symm).surjective.forall] intro i simp [modNat, Nat.mod_eq_of_lt i.is_lt] theorem repeat_succ (a : Fin n → α) (m : ℕ) : Fin.repeat m.succ a = append a (Fin.repeat m a) ∘ Fin.cast ((Nat.succ_mul _ _).trans (Nat.add_comm ..)) := by generalize_proofs h apply funext rw [(Fin.rightInverse_cast h.symm).surjective.forall] refine Fin.addCases (fun l => ?_) fun r => ?_ · simp [modNat, Nat.mod_eq_of_lt l.is_lt] · simp [modNat] @[simp] theorem repeat_add (a : Fin n → α) (m₁ m₂ : ℕ) : Fin.repeat (m₁ + m₂) a = append (Fin.repeat m₁ a) (Fin.repeat m₂ a) ∘ Fin.cast (Nat.add_mul ..) := by generalize_proofs h apply funext rw [(Fin.rightInverse_cast h.symm).surjective.forall] refine Fin.addCases (fun l => ?_) fun r => ?_ · simp [modNat, Nat.mod_eq_of_lt l.is_lt] · simp [modNat, Nat.add_mod] theorem repeat_rev (a : Fin n → α) (k : Fin (m * n)) : Fin.repeat m a k.rev = Fin.repeat m (a ∘ Fin.rev) k := congr_arg a k.modNat_rev theorem repeat_comp_rev (a : Fin n → α) : Fin.repeat m a ∘ Fin.rev = Fin.repeat m (a ∘ Fin.rev) := funext <| repeat_rev a end Repeat end Tuple section TupleRight /-! In the previous section, we have discussed inserting or removing elements on the left of a tuple. In this section, we do the same on the right. A difference is that `Fin (n+1)` is constructed inductively from `Fin n` starting from the left, not from the right. This implies that Lean needs more help to realize that elements belong to the right types, i.e., we need to insert casts at several places. -/ variable {α : Fin (n + 1) → Sort*} (x : α (last n)) (q : ∀ i, α i) (p : ∀ i : Fin n, α i.castSucc) (i : Fin n) (y : α i.castSucc) (z : α (last n)) /-- The beginning of an `n+1` tuple, i.e., its first `n` entries -/ def init (q : ∀ i, α i) (i : Fin n) : α i.castSucc := q i.castSucc theorem init_def {q : ∀ i, α i} : (init fun k : Fin (n + 1) ↦ q k) = fun k : Fin n ↦ q k.castSucc := rfl /-- Adding an element at the end of an `n`-tuple, to get an `n+1`-tuple. The name `snoc` comes from `cons` (i.e., adding an element to the left of a tuple) read in reverse order. -/ def snoc (p : ∀ i : Fin n, α i.castSucc) (x : α (last n)) (i : Fin (n + 1)) : α i := if h : i.val < n then _root_.cast (by rw [Fin.castSucc_castLT i h]) (p (castLT i h)) else _root_.cast (by rw [eq_last_of_not_lt h]) x @[simp] theorem init_snoc : init (snoc p x) = p := by ext i simp only [init, snoc, coe_castSucc, is_lt, cast_eq, dite_true] convert cast_eq rfl (p i) @[simp] theorem snoc_castSucc : snoc p x i.castSucc = p i := by simp only [snoc, coe_castSucc, is_lt, cast_eq, dite_true] convert cast_eq rfl (p i) @[simp] theorem snoc_comp_castSucc {α : Sort*} {a : α} {f : Fin n → α} : (snoc f a : Fin (n + 1) → α) ∘ castSucc = f := funext fun i ↦ by rw [Function.comp_apply, snoc_castSucc] @[simp] theorem snoc_last : snoc p x (last n) = x := by simp [snoc] lemma snoc_zero {α : Sort*} (p : Fin 0 → α) (x : α) : Fin.snoc p x = fun _ ↦ x := by ext y have : Subsingleton (Fin (0 + 1)) := Fin.subsingleton_one simp only [Subsingleton.elim y (Fin.last 0), snoc_last] @[simp] theorem snoc_comp_nat_add {n m : ℕ} {α : Sort*} (f : Fin (m + n) → α) (a : α) : (snoc f a : Fin _ → α) ∘ (natAdd m : Fin (n + 1) → Fin (m + n + 1)) = snoc (f ∘ natAdd m) a := by ext i refine Fin.lastCases ?_ (fun i ↦ ?_) i · simp only [Function.comp_apply] rw [snoc_last, natAdd_last, snoc_last] · simp only [comp_apply, snoc_castSucc] rw [natAdd_castSucc, snoc_castSucc] @[simp] theorem snoc_cast_add {α : Fin (n + m + 1) → Sort*} (f : ∀ i : Fin (n + m), α i.castSucc) (a : α (last (n + m))) (i : Fin n) : (snoc f a) (castAdd (m + 1) i) = f (castAdd m i) := dif_pos _ @[simp] theorem snoc_comp_cast_add {n m : ℕ} {α : Sort*} (f : Fin (n + m) → α) (a : α) : (snoc f a : Fin _ → α) ∘ castAdd (m + 1) = f ∘ castAdd m := funext (snoc_cast_add _ _) /-- Updating a tuple and adding an element at the end commute. -/ @[simp] theorem snoc_update : snoc (update p i y) x = update (snoc p x) i.castSucc y := by ext j cases j using lastCases with | cast j => rcases eq_or_ne j i with rfl | hne <;> simp [*] | last => simp [Ne.symm] /-- Adding an element at the beginning of a tuple and then updating it amounts to adding it directly. -/ theorem update_snoc_last : update (snoc p x) (last n) z = snoc p z := by ext j cases j using lastCases <;> simp /-- As a binary function, `Fin.snoc` is injective. -/ theorem snoc_injective2 : Function.Injective2 (@snoc n α) := fun x y xₙ yₙ h ↦ ⟨funext fun i ↦ by simpa using congr_fun h (castSucc i), by simpa using congr_fun h (last n)⟩ @[simp] theorem snoc_inj {x y : ∀ i : Fin n, α i.castSucc} {xₙ yₙ : α (last n)} : snoc x xₙ = snoc y yₙ ↔ x = y ∧ xₙ = yₙ := snoc_injective2.eq_iff theorem snoc_right_injective (x : ∀ i : Fin n, α i.castSucc) : Function.Injective (snoc x) := snoc_injective2.right _ theorem snoc_left_injective (xₙ : α (last n)) : Function.Injective (snoc · xₙ) := snoc_injective2.left _ /-- Concatenating the first element of a tuple with its tail gives back the original tuple -/ @[simp] theorem snoc_init_self : snoc (init q) (q (last n)) = q := by ext j by_cases h : j.val < n · simp only [init, snoc, h, cast_eq, dite_true, castSucc_castLT] · rw [eq_last_of_not_lt h] simp /-- Updating the last element of a tuple does not change the beginning. -/ @[simp] theorem init_update_last : init (update q (last n) z) = init q := by ext j simp [init, Fin.ne_of_lt] /-- Updating an element and taking the beginning commute. -/ @[simp] theorem init_update_castSucc : init (update q i.castSucc y) = update (init q) i y := by ext j by_cases h : j = i · rw [h] simp [init] · simp [init, h, castSucc_inj] /-- `tail` and `init` commute. We state this lemma in a non-dependent setting, as otherwise it would involve a cast to convince Lean that the two types are equal, making it harder to use. -/ theorem tail_init_eq_init_tail {β : Sort*} (q : Fin (n + 2) → β) : tail (init q) = init (tail q) := by ext i simp [tail, init, castSucc_fin_succ] /-- `cons` and `snoc` commute. We state this lemma in a non-dependent setting, as otherwise it would involve a cast to convince Lean that the two types are equal, making it harder to use. -/ theorem cons_snoc_eq_snoc_cons {β : Sort*} (a : β) (q : Fin n → β) (b : β) : @cons n.succ (fun _ ↦ β) a (snoc q b) = snoc (cons a q) b := by ext i by_cases h : i = 0 · simp [h, snoc, castLT] set j := pred i h with ji have : i = j.succ := by rw [ji, succ_pred] rw [this, cons_succ] by_cases h' : j.val < n · set k := castLT j h' with jk have : j = castSucc k := by rw [jk, castSucc_castLT] rw [this, ← castSucc_fin_succ, snoc] simp [pred, snoc, cons] rw [eq_last_of_not_lt h', succ_last] simp theorem comp_snoc {α : Sort*} {β : Sort*} (g : α → β) (q : Fin n → α) (y : α) : g ∘ snoc q y = snoc (g ∘ q) (g y) := by ext j by_cases h : j.val < n · simp [h, snoc, castSucc_castLT] · rw [eq_last_of_not_lt h] simp /-- Appending a one-tuple to the right is the same as `Fin.snoc`. -/ theorem append_right_eq_snoc {α : Sort*} {n : ℕ} (x : Fin n → α) (x₀ : Fin 1 → α) : Fin.append x x₀ = Fin.snoc x (x₀ 0) := by ext i refine Fin.addCases ?_ ?_ i <;> clear i · intro i rw [Fin.append_left] exact (@snoc_castSucc _ (fun _ => α) _ _ i).symm · intro i rw [Subsingleton.elim i 0, Fin.append_right] exact (@snoc_last _ (fun _ => α) _ _).symm /-- `Fin.snoc` is the same as appending a one-tuple -/ theorem snoc_eq_append {α : Sort*} (xs : Fin n → α) (x : α) : snoc xs x = append xs (cons x Fin.elim0) := (append_right_eq_snoc xs (cons x Fin.elim0)).symm theorem append_left_snoc {n m} {α : Sort*} (xs : Fin n → α) (x : α) (ys : Fin m → α) : Fin.append (Fin.snoc xs x) ys = Fin.append xs (Fin.cons x ys) ∘ Fin.cast (Nat.succ_add_eq_add_succ ..) := by rw [snoc_eq_append, append_assoc, append_left_eq_cons, append_cast_right]; rfl theorem append_right_cons {n m} {α : Sort*} (xs : Fin n → α) (y : α) (ys : Fin m → α) : Fin.append xs (Fin.cons y ys) = Fin.append (Fin.snoc xs y) ys ∘ Fin.cast (Nat.succ_add_eq_add_succ ..).symm := by rw [append_left_snoc]; rfl theorem append_cons {α : Sort*} (a : α) (as : Fin n → α) (bs : Fin m → α) : Fin.append (cons a as) bs = cons a (Fin.append as bs) ∘ (Fin.cast <| Nat.add_right_comm n 1 m) := by funext i rcases i with ⟨i, -⟩ simp only [append, addCases, cons, castLT, cast, comp_apply] rcases i with - | i · simp · split_ifs with h · have : i < n := Nat.lt_of_succ_lt_succ h simp [addCases, this] · have : ¬i < n := Nat.not_le.mpr <| Nat.lt_succ.mp <| Nat.not_le.mp h simp [addCases, this] theorem append_snoc {α : Sort*} (as : Fin n → α) (bs : Fin m → α) (b : α) : Fin.append as (snoc bs b) = snoc (Fin.append as bs) b := by funext i rcases i with ⟨i, isLt⟩ simp only [append, addCases, castLT, cast_mk, subNat_mk, natAdd_mk, cast, snoc.eq_1, cast_eq, eq_rec_constant, Nat.add_eq, Nat.add_zero, castLT_mk] split_ifs with lt_n lt_add sub_lt nlt_add lt_add <;> (try rfl) · have := Nat.lt_add_right m lt_n contradiction · obtain rfl := Nat.eq_of_le_of_lt_succ (Nat.not_lt.mp nlt_add) isLt simp [Nat.add_comm n m] at sub_lt · have := Nat.sub_lt_left_of_lt_add (Nat.not_lt.mp lt_n) lt_add contradiction theorem comp_init {α : Sort*} {β : Sort*} (g : α → β) (q : Fin n.succ → α) : g ∘ init q = init (g ∘ q) := by ext j simp [init] /-- Equivalence between tuples of length `n + 1` and pairs of an element and a tuple of length `n` given by separating out the last element of the tuple. This is `Fin.snoc` as an `Equiv`. -/ @[simps] def snocEquiv (α : Fin (n + 1) → Type*) : α (last n) × (∀ i, α (castSucc i)) ≃ ∀ i, α i where toFun f _ := Fin.snoc f.2 f.1 _ invFun f := ⟨f _, Fin.init f⟩ left_inv f := by simp right_inv f := by simp /-- Recurse on an `n+1`-tuple by splitting it its initial `n`-tuple and its last element. -/ @[elab_as_elim, inline] def snocCases {P : (∀ i : Fin n.succ, α i) → Sort*} (h : ∀ xs x, P (Fin.snoc xs x)) (x : ∀ i : Fin n.succ, α i) : P x := _root_.cast (by rw [Fin.snoc_init_self]) <| h (Fin.init x) (x <| Fin.last _) @[simp] lemma snocCases_snoc {P : (∀ i : Fin (n+1), α i) → Sort*} (h : ∀ x x₀, P (Fin.snoc x x₀)) (x : ∀ i : Fin n, (Fin.init α) i) (x₀ : α (Fin.last _)) : snocCases h (Fin.snoc x x₀) = h x x₀ := by rw [snocCases, cast_eq_iff_heq, Fin.init_snoc, Fin.snoc_last] /-- Recurse on a tuple by splitting into `Fin.elim0` and `Fin.snoc`. -/ @[elab_as_elim] def snocInduction {α : Sort*} {P : ∀ {n : ℕ}, (Fin n → α) → Sort*} (h0 : P Fin.elim0) (h : ∀ {n} (x : Fin n → α) (x₀), P x → P (Fin.snoc x x₀)) : ∀ {n : ℕ} (x : Fin n → α), P x | 0, x => by convert h0 | _ + 1, x => snocCases (fun _ _ ↦ h _ _ <| snocInduction h0 h _) x end TupleRight section InsertNth variable {α : Fin (n + 1) → Sort*} {β : Sort*} /- Porting note: Lean told me `(fun x x_1 ↦ α x)` was an invalid motive, but disabling automatic insertion and specifying that motive seems to work. -/ /-- Define a function on `Fin (n + 1)` from a value on `i : Fin (n + 1)` and values on each `Fin.succAbove i j`, `j : Fin n`. This version is elaborated as eliminator and works for propositions, see also `Fin.insertNth` for a version without an `@[elab_as_elim]` attribute. -/ @[elab_as_elim] def succAboveCases {α : Fin (n + 1) → Sort u} (i : Fin (n + 1)) (x : α i) (p : ∀ j : Fin n, α (i.succAbove j)) (j : Fin (n + 1)) : α j := if hj : j = i then Eq.rec x hj.symm else if hlt : j < i then @Eq.recOn _ _ (fun x _ ↦ α x) _ (succAbove_castPred_of_lt _ _ hlt) (p _) else @Eq.recOn _ _ (fun x _ ↦ α x) _ (succAbove_pred_of_lt _ _ <| (Fin.lt_or_lt_of_ne hj).resolve_left hlt) (p _) -- This is a duplicate of `Fin.exists_fin_succ` in Core. We should upstream the name change. alias forall_iff_succ := forall_fin_succ -- This is a duplicate of `Fin.exists_fin_succ` in Core. We should upstream the name change. alias exists_iff_succ := exists_fin_succ lemma forall_iff_castSucc {P : Fin (n + 1) → Prop} : (∀ i, P i) ↔ P (last n) ∧ ∀ i : Fin n, P i.castSucc := ⟨fun h ↦ ⟨h _, fun _ ↦ h _⟩, fun h ↦ lastCases h.1 h.2⟩ lemma exists_iff_castSucc {P : Fin (n + 1) → Prop} : (∃ i, P i) ↔ P (last n) ∨ ∃ i : Fin n, P i.castSucc where mp := by rintro ⟨i, hi⟩ induction' i using lastCases · exact .inl hi · exact .inr ⟨_, hi⟩ mpr := by rintro (h | ⟨i, hi⟩) <;> exact ⟨_, ‹_›⟩ theorem forall_iff_succAbove {P : Fin (n + 1) → Prop} (p : Fin (n + 1)) : (∀ i, P i) ↔ P p ∧ ∀ i, P (p.succAbove i) := ⟨fun h ↦ ⟨h _, fun _ ↦ h _⟩, fun h ↦ succAboveCases p h.1 h.2⟩ lemma exists_iff_succAbove {P : Fin (n + 1) → Prop} (p : Fin (n + 1)) : (∃ i, P i) ↔ P p ∨ ∃ i, P (p.succAbove i) where mp := by rintro ⟨i, hi⟩ induction' i using p.succAboveCases · exact .inl hi · exact .inr ⟨_, hi⟩ mpr := by rintro (h | ⟨i, hi⟩) <;> exact ⟨_, ‹_›⟩ /-- Analogue of `Fin.eq_zero_or_eq_succ` for `succAbove`. -/ theorem eq_self_or_eq_succAbove (p i : Fin (n + 1)) : i = p ∨ ∃ j, i = p.succAbove j := succAboveCases p (.inl rfl) (fun j => .inr ⟨j, rfl⟩) i /-- Remove the `p`-th entry of a tuple. -/ def removeNth (p : Fin (n + 1)) (f : ∀ i, α i) : ∀ i, α (p.succAbove i) := fun i ↦ f (p.succAbove i) /-- Insert an element into a tuple at a given position. For `i = 0` see `Fin.cons`, for `i = Fin.last n` see `Fin.snoc`. See also `Fin.succAboveCases` for a version elaborated as an eliminator. -/ def insertNth (i : Fin (n + 1)) (x : α i) (p : ∀ j : Fin n, α (i.succAbove j)) (j : Fin (n + 1)) : α j := succAboveCases i x p j @[simp] theorem insertNth_apply_same (i : Fin (n + 1)) (x : α i) (p : ∀ j, α (i.succAbove j)) : insertNth i x p i = x := by simp [insertNth, succAboveCases] @[simp] theorem insertNth_apply_succAbove (i : Fin (n + 1)) (x : α i) (p : ∀ j, α (i.succAbove j)) (j : Fin n) : insertNth i x p (i.succAbove j) = p j := by simp only [insertNth, succAboveCases, dif_neg (succAbove_ne _ _), succAbove_lt_iff_castSucc_lt] split_ifs with hlt · generalize_proofs H₁ H₂; revert H₂ generalize hk : castPred ((succAbove i) j) H₁ = k rw [castPred_succAbove _ _ hlt] at hk; cases hk intro; rfl · generalize_proofs H₀ H₁ H₂; revert H₂ generalize hk : pred (succAbove i j) H₁ = k rw [pred_succAbove _ _ (Fin.not_lt.1 hlt)] at hk; cases hk intro; rfl @[simp] theorem succAbove_cases_eq_insertNth : @succAboveCases = @insertNth := rfl @[simp] lemma removeNth_insertNth (p : Fin (n + 1)) (a : α p) (f : ∀ i, α (succAbove p i)) : removeNth p (insertNth p a f) = f := by ext; unfold removeNth; simp @[simp] lemma removeNth_zero (f : ∀ i, α i) : removeNth 0 f = tail f := by ext; simp [tail, removeNth] @[simp] lemma removeNth_last {α : Type*} (f : Fin (n + 1) → α) : removeNth (last n) f = init f := by ext; simp [init, removeNth] @[simp] theorem insertNth_comp_succAbove (i : Fin (n + 1)) (x : β) (p : Fin n → β) : insertNth i x p ∘ i.succAbove = p := funext (insertNth_apply_succAbove i _ _) theorem insertNth_eq_iff {p : Fin (n + 1)} {a : α p} {f : ∀ i, α (p.succAbove i)} {g : ∀ j, α j} : insertNth p a f = g ↔ a = g p ∧ f = removeNth p g := by simp [funext_iff, forall_iff_succAbove p, removeNth] theorem eq_insertNth_iff {p : Fin (n + 1)} {a : α p} {f : ∀ i, α (p.succAbove i)} {g : ∀ j, α j} : g = insertNth p a f ↔ g p = a ∧ removeNth p g = f := by simpa [eq_comm] using insertNth_eq_iff /-- As a binary function, `Fin.insertNth` is injective. -/ theorem insertNth_injective2 {p : Fin (n + 1)} : Function.Injective2 (@insertNth n α p) := fun xₚ yₚ x y h ↦ ⟨by simpa using congr_fun h p, funext fun i ↦ by simpa using congr_fun h (succAbove p i)⟩ @[simp] theorem insertNth_inj {p : Fin (n + 1)} {x y : ∀ i, α (succAbove p i)} {xₚ yₚ : α p} : insertNth p xₚ x = insertNth p yₚ y ↔ xₚ = yₚ ∧ x = y := insertNth_injective2.eq_iff theorem insertNth_left_injective {p : Fin (n + 1)} (x : ∀ i, α (succAbove p i)) : Function.Injective (insertNth p · x) := insertNth_injective2.left _ theorem insertNth_right_injective {p : Fin (n + 1)} (x : α p) : Function.Injective (insertNth p x) := insertNth_injective2.right _ /- Porting note: Once again, Lean told me `(fun x x_1 ↦ α x)` was an invalid motive, but disabling automatic insertion and specifying that motive seems to work. -/ theorem insertNth_apply_below {i j : Fin (n + 1)} (h : j < i) (x : α i) (p : ∀ k, α (i.succAbove k)) : i.insertNth x p j = @Eq.recOn _ _ (fun x _ ↦ α x) _ (succAbove_castPred_of_lt _ _ h) (p <| j.castPred _) := by rw [insertNth, succAboveCases, dif_neg (Fin.ne_of_lt h), dif_pos h] /- Porting note: Once again, Lean told me `(fun x x_1 ↦ α x)` was an invalid motive, but disabling automatic insertion and specifying that motive seems to work. -/ theorem insertNth_apply_above {i j : Fin (n + 1)} (h : i < j) (x : α i) (p : ∀ k, α (i.succAbove k)) : i.insertNth x p j = @Eq.recOn _ _ (fun x _ ↦ α x) _ (succAbove_pred_of_lt _ _ h) (p <| j.pred _) := by rw [insertNth, succAboveCases, dif_neg (Fin.ne_of_gt h), dif_neg (Fin.lt_asymm h)] theorem insertNth_zero (x : α 0) (p : ∀ j : Fin n, α (succAbove 0 j)) : insertNth 0 x p = cons x fun j ↦ _root_.cast (congr_arg α (congr_fun succAbove_zero j)) (p j) := by refine insertNth_eq_iff.2 ⟨by simp, ?_⟩ ext j convert (cons_succ x p j).symm @[simp] theorem insertNth_zero' (x : β) (p : Fin n → β) : @insertNth _ (fun _ ↦ β) 0 x p = cons x p := by simp [insertNth_zero] theorem insertNth_last (x : α (last n)) (p : ∀ j : Fin n, α ((last n).succAbove j)) : insertNth (last n) x p = snoc (fun j ↦ _root_.cast (congr_arg α (succAbove_last_apply j)) (p j)) x := by refine insertNth_eq_iff.2 ⟨by simp, ?_⟩ ext j apply eq_of_heq trans snoc (fun j ↦ _root_.cast (congr_arg α (succAbove_last_apply j)) (p j)) x j.castSucc · rw [snoc_castSucc] exact (cast_heq _ _).symm · apply congr_arg_heq rw [succAbove_last] @[simp] theorem insertNth_last' (x : β) (p : Fin n → β) : @insertNth _ (fun _ ↦ β) (last n) x p = snoc p x := by simp [insertNth_last] lemma insertNth_rev {α : Sort*} (i : Fin (n + 1)) (a : α) (f : Fin n → α) (j : Fin (n + 1)) : insertNth (α := fun _ ↦ α) i a f (rev j) = insertNth (α := fun _ ↦ α) i.rev a (f ∘ rev) j := by induction j using Fin.succAboveCases · exact rev i · simp · simp [rev_succAbove] theorem insertNth_comp_rev {α} (i : Fin (n + 1)) (x : α) (p : Fin n → α) : (Fin.insertNth i x p) ∘ Fin.rev = Fin.insertNth (Fin.rev i) x (p ∘ Fin.rev) := by funext x apply insertNth_rev theorem cons_rev {α n} (a : α) (f : Fin n → α) (i : Fin <| n + 1) : cons (α := fun _ => α) a f i.rev = snoc (α := fun _ => α) (f ∘ Fin.rev : Fin _ → α) a i := by simpa using insertNth_rev 0 a f i theorem cons_comp_rev {α n} (a : α) (f : Fin n → α) : Fin.cons a f ∘ Fin.rev = Fin.snoc (f ∘ Fin.rev) a := by funext i; exact cons_rev ..
theorem snoc_rev {α n} (a : α) (f : Fin n → α) (i : Fin <| n + 1) : snoc (α := fun _ => α) f a i.rev = cons (α := fun _ => α) a (f ∘ Fin.rev : Fin _ → α) i := by simpa using insertNth_rev (last n) a f i theorem snoc_comp_rev {α n} (a : α) (f : Fin n → α) :
Mathlib/Data/Fin/Tuple/Basic.lean
915
920
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Yury Kudryashov -/ import Mathlib.Tactic.ApplyFun import Mathlib.Topology.Separation.Regular import Mathlib.Topology.UniformSpace.Basic /-! # Hausdorff properties of uniform spaces. Separation quotient. Two points of a topological space are called `Inseparable`, if their neighborhoods filter are equal. Equivalently, `Inseparable x y` means that any open set that contains `x` must contain `y` and vice versa. In a uniform space, points `x` and `y` are inseparable if and only if `(x, y)` belongs to all entourages, see `inseparable_iff_ker_uniformity`. A uniform space is a regular topological space, hence separation axioms `T0Space`, `T1Space`, `T2Space`, and `T3Space` are equivalent for uniform spaces, and Lean typeclass search can automatically convert from one assumption to another. We say that a uniform space is *separated*, if it satisfies these axioms. If you need an `Iff` statement (e.g., to rewrite), then see `R1Space.t0Space_iff_t2Space` and `RegularSpace.t0Space_iff_t3Space`. In this file we prove several facts that relate `Inseparable` and `Specializes` to the uniformity filter. Most of them are simple corollaries of `Filter.HasBasis.inseparable_iff_uniformity` for different filter bases of `𝓤 α`. Then we study the Kolmogorov quotient `SeparationQuotient X` of a uniform space. For a general topological space, this quotient is defined as the quotient by `Inseparable` equivalence relation. It is the maximal T₀ quotient of a topological space. In case of a uniform space, we equip this quotient with a `UniformSpace` structure that agrees with the quotient topology. We also prove that the quotient map induces uniformity on the original space. Finally, we turn `SeparationQuotient` into a functor (not in terms of `CategoryTheory.Functor` to avoid extra imports) by defining `SeparationQuotient.lift'` and `SeparationQuotient.map` operations. ## Main definitions * `SeparationQuotient.instUniformSpace`: uniform space structure on `SeparationQuotient α`, where `α` is a uniform space; * `SeparationQuotient.lift'`: given a map `f : α → β` from a uniform space to a separated uniform space, lift it to a map `SeparationQuotient α → β`; if the original map is not uniformly continuous, then returns a constant map. * `SeparationQuotient.map`: given a map `f : α → β` between uniform spaces, returns a map `SeparationQuotient α → SeparationQuotient β`. If the original map is not uniformly continuous, then returns a constant map. Otherwise, `SeparationQuotient.map f (SeparationQuotient.mk x) = SeparationQuotient.mk (f x)`. ## Main results * `SeparationQuotient.uniformity_eq`: the uniformity filter on `SeparationQuotient α` is the push forward of the uniformity filter on `α`. * `SeparationQuotient.comap_mk_uniformity`: the quotient map `α → SeparationQuotient α` induces uniform space structure on the original space. * `SeparationQuotient.uniformContinuous_lift'`: factoring a uniformly continuous map through the separation quotient gives a uniformly continuous map. * `SeparationQuotient.uniformContinuous_map`: maps induced between separation quotients are uniformly continuous. ## Implementation notes This files used to contain definitions of `separationRel α` and `UniformSpace.SeparationQuotient α`. These definitions were equal (but not definitionally equal) to `{x : α × α | Inseparable x.1 x.2}` and `SeparationQuotient α`, respectively, and were added to the library before their geneeralizations to topological spaces. In https://github.com/leanprover-community/mathlib4/pull/10644, we migrated from these definitions to more general `Inseparable` and `SeparationQuotient`. ## TODO Definitions `SeparationQuotient.lift'` and `SeparationQuotient.map` rely on `UniformSpace` structures in the domain and in the codomain. We should generalize them to topological spaces. This generalization will drop `UniformContinuous` assumptions in some lemmas, and add these assumptions in other lemmas, so it was not done in https://github.com/leanprover-community/mathlib4/pull/10644 to keep it reasonably sized. ## Keywords uniform space, separated space, Hausdorff space, separation quotient -/ open Filter Set Function Topology Uniformity UniformSpace noncomputable section universe u v w variable {α : Type u} {β : Type v} {γ : Type w} variable [UniformSpace α] [UniformSpace β] [UniformSpace γ] /-! ### Separated uniform spaces -/ instance (priority := 100) UniformSpace.to_regularSpace : RegularSpace α := .of_hasBasis (fun _ ↦ nhds_basis_uniformity' uniformity_hasBasis_closed) fun a _V hV ↦ isClosed_ball a hV.2 theorem Filter.HasBasis.specializes_iff_uniformity {ι : Sort*} {p : ι → Prop} {s : ι → Set (α × α)} (h : (𝓤 α).HasBasis p s) {x y : α} : x ⤳ y ↔ ∀ i, p i → (x, y) ∈ s i := (nhds_basis_uniformity h).specializes_iff theorem Filter.HasBasis.inseparable_iff_uniformity {ι : Sort*} {p : ι → Prop} {s : ι → Set (α × α)} (h : (𝓤 α).HasBasis p s) {x y : α} : Inseparable x y ↔ ∀ i, p i → (x, y) ∈ s i := specializes_iff_inseparable.symm.trans h.specializes_iff_uniformity theorem inseparable_iff_ker_uniformity {x y : α} : Inseparable x y ↔ (x, y) ∈ (𝓤 α).ker := (𝓤 α).basis_sets.inseparable_iff_uniformity protected theorem Inseparable.nhds_le_uniformity {x y : α} (h : Inseparable x y) : 𝓝 (x, y) ≤ 𝓤 α := by rw [h.prod rfl] apply nhds_le_uniformity theorem inseparable_iff_clusterPt_uniformity {x y : α} : Inseparable x y ↔ ClusterPt (x, y) (𝓤 α) := by refine ⟨fun h ↦ .of_nhds_le h.nhds_le_uniformity, fun h ↦ ?_⟩ simp_rw [uniformity_hasBasis_closed.inseparable_iff_uniformity, isClosed_iff_clusterPt] exact fun U ⟨hU, hUc⟩ ↦ hUc _ <| h.mono <| le_principal_iff.2 hU theorem t0Space_iff_uniformity : T0Space α ↔ ∀ x y, (∀ r ∈ 𝓤 α, (x, y) ∈ r) → x = y := by simp only [t0Space_iff_inseparable, inseparable_iff_ker_uniformity, mem_ker, id] theorem t0Space_iff_uniformity' : T0Space α ↔ Pairwise fun x y ↦ ∃ r ∈ 𝓤 α, (x, y) ∉ r := by simp [t0Space_iff_not_inseparable, inseparable_iff_ker_uniformity] theorem t0Space_iff_ker_uniformity : T0Space α ↔ (𝓤 α).ker = diagonal α := by simp_rw [t0Space_iff_uniformity, subset_antisymm_iff, diagonal_subset_iff, subset_def, Prod.forall, Filter.mem_ker, mem_diagonal_iff, iff_self_and] exact fun _ x s hs ↦ refl_mem_uniformity hs theorem eq_of_uniformity {α : Type*} [UniformSpace α] [T0Space α] {x y : α} (h : ∀ {V}, V ∈ 𝓤 α → (x, y) ∈ V) : x = y := t0Space_iff_uniformity.mp ‹T0Space α› x y @h theorem eq_of_uniformity_basis {α : Type*} [UniformSpace α] [T0Space α] {ι : Sort*} {p : ι → Prop} {s : ι → Set (α × α)} (hs : (𝓤 α).HasBasis p s) {x y : α} (h : ∀ {i}, p i → (x, y) ∈ s i) : x = y := (hs.inseparable_iff_uniformity.2 @h).eq theorem eq_of_forall_symmetric {α : Type*} [UniformSpace α] [T0Space α] {x y : α} (h : ∀ {V}, V ∈ 𝓤 α → IsSymmetricRel V → (x, y) ∈ V) : x = y := eq_of_uniformity_basis hasBasis_symmetric (by simpa) theorem eq_of_clusterPt_uniformity [T0Space α] {x y : α} (h : ClusterPt (x, y) (𝓤 α)) : x = y := (inseparable_iff_clusterPt_uniformity.2 h).eq theorem Filter.Tendsto.inseparable_iff_uniformity {β} {l : Filter β} [NeBot l] {f g : β → α} {a b : α} (ha : Tendsto f l (𝓝 a)) (hb : Tendsto g l (𝓝 b)) : Inseparable a b ↔ Tendsto (fun x ↦ (f x, g x)) l (𝓤 α) := by refine ⟨fun h ↦ (ha.prodMk_nhds hb).mono_right h.nhds_le_uniformity, fun h ↦ ?_⟩ rw [inseparable_iff_clusterPt_uniformity] exact (ClusterPt.of_le_nhds (ha.prodMk_nhds hb)).mono h theorem isClosed_of_spaced_out [T0Space α] {V₀ : Set (α × α)} (V₀_in : V₀ ∈ 𝓤 α) {s : Set α} (hs : s.Pairwise fun x y => (x, y) ∉ V₀) : IsClosed s := by rcases comp_symm_mem_uniformity_sets V₀_in with ⟨V₁, V₁_in, V₁_symm, h_comp⟩ apply isClosed_of_closure_subset intro x hx rw [mem_closure_iff_ball] at hx rcases hx V₁_in with ⟨y, hy, hy'⟩ suffices x = y by rwa [this] apply eq_of_forall_symmetric intro V V_in _ rcases hx (inter_mem V₁_in V_in) with ⟨z, hz, hz'⟩ obtain rfl : z = y := by by_contra hzy exact hs hz' hy' hzy (h_comp <| mem_comp_of_mem_ball V₁_symm (ball_inter_left x _ _ hz) hy) exact ball_inter_right x _ _ hz theorem isClosed_range_of_spaced_out {ι} [T0Space α] {V₀ : Set (α × α)} (V₀_in : V₀ ∈ 𝓤 α) {f : ι → α} (hf : Pairwise fun x y => (f x, f y) ∉ V₀) : IsClosed (range f) := isClosed_of_spaced_out V₀_in <| by rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ h exact hf (ne_of_apply_ne f h) /-! ### Separation quotient -/ namespace SeparationQuotient theorem comap_map_mk_uniformity : comap (Prod.map mk mk) (map (Prod.map mk mk) (𝓤 α)) = 𝓤 α := by refine le_antisymm ?_ le_comap_map refine ((((𝓤 α).basis_sets.map _).comap _).le_basis_iff uniformity_hasBasis_open).2 fun U hU ↦ ?_ refine ⟨U, hU.1, fun (x₁, x₂) ⟨(y₁, y₂), hyU, hxy⟩ ↦ ?_⟩ simp only [Prod.map, Prod.ext_iff, mk_eq_mk] at hxy exact ((hxy.1.prod hxy.2).mem_open_iff hU.2).1 hyU instance instUniformSpace : UniformSpace (SeparationQuotient α) where uniformity := map (Prod.map mk mk) (𝓤 α) symm := tendsto_map' <| tendsto_map.comp tendsto_swap_uniformity comp := fun t ht ↦ by rcases comp_open_symm_mem_uniformity_sets ht with ⟨U, hU, hUo, -, hUt⟩ refine mem_of_superset (mem_lift' <| image_mem_map hU) ?_ simp only [subset_def, Prod.forall, mem_compRel, mem_image, Prod.ext_iff] rintro _ _ ⟨_, ⟨⟨x, y⟩, hxyU, rfl, rfl⟩, ⟨⟨y', z⟩, hyzU, hy, rfl⟩⟩ have : y' ⤳ y := (mk_eq_mk.1 hy).specializes exact @hUt (x, z) ⟨y', this.mem_open (UniformSpace.isOpen_ball _ hUo) hxyU, hyzU⟩ nhds_eq_comap_uniformity := surjective_mk.forall.2 fun x ↦ comap_injective surjective_mk <| by conv_lhs => rw [comap_mk_nhds_mk, nhds_eq_comap_uniformity, ← comap_map_mk_uniformity] simp only [Filter.comap_comap, Function.comp_def, Prod.map_apply] theorem uniformity_eq : 𝓤 (SeparationQuotient α) = (𝓤 α).map (Prod.map mk mk) := rfl theorem uniformContinuous_mk : UniformContinuous (mk : α → SeparationQuotient α) := le_rfl theorem uniformContinuous_dom {f : SeparationQuotient α → β} : UniformContinuous f ↔ UniformContinuous (f ∘ mk) := .rfl theorem uniformContinuous_dom₂ {f : SeparationQuotient α × SeparationQuotient β → γ} : UniformContinuous f ↔ UniformContinuous fun p : α × β ↦ f (mk p.1, mk p.2) := by simp only [UniformContinuous, uniformity_prod_eq_prod, uniformity_eq, prod_map_map_eq, tendsto_map'_iff] rfl theorem uniformContinuous_lift {f : α → β} (h : ∀ a b, Inseparable a b → f a = f b) : UniformContinuous (lift f h) ↔ UniformContinuous f := .rfl theorem uniformContinuous_uncurry_lift₂ {f : α → β → γ} (h : ∀ a c b d, Inseparable a b → Inseparable c d → f a c = f b d) : UniformContinuous (uncurry <| lift₂ f h) ↔ UniformContinuous (uncurry f) := uniformContinuous_dom₂ theorem comap_mk_uniformity : (𝓤 (SeparationQuotient α)).comap (Prod.map mk mk) = 𝓤 α := comap_map_mk_uniformity open Classical in /-- Factoring functions to a separated space through the separation quotient. TODO: unify with `SeparationQuotient.lift`. -/ def lift' [T0Space β] (f : α → β) : SeparationQuotient α → β := if hc : UniformContinuous f then lift f fun _ _ h => (h.map hc.continuous).eq else fun x => f (Nonempty.some ⟨x.out⟩) theorem lift'_mk [T0Space β] {f : α → β} (h : UniformContinuous f) (a : α) : lift' f (mk a) = f a := by rw [lift', dif_pos h, lift_mk] theorem uniformContinuous_lift' [T0Space β] (f : α → β) : UniformContinuous (lift' f) := by by_cases hf : UniformContinuous f · rwa [lift', dif_pos hf, uniformContinuous_lift] · rw [lift', dif_neg hf] exact uniformContinuous_of_const fun a _ => rfl /-- The separation quotient functor acting on functions. -/ def map (f : α → β) : SeparationQuotient α → SeparationQuotient β := lift' (mk ∘ f) theorem map_mk {f : α → β} (h : UniformContinuous f) (a : α) : map f (mk a) = mk (f a) := by rw [map, lift'_mk (uniformContinuous_mk.comp h)]; rfl theorem uniformContinuous_map (f : α → β) : UniformContinuous (map f) := uniformContinuous_lift' _ theorem map_unique {f : α → β} (hf : UniformContinuous f) {g : SeparationQuotient α → SeparationQuotient β} (comm : mk ∘ f = g ∘ mk) : map f = g := by ext ⟨a⟩ calc map f ⟦a⟧ = ⟦f a⟧ := map_mk hf a _ = g ⟦a⟧ := congr_fun comm a @[simp] theorem map_id : map (@id α) = id := map_unique uniformContinuous_id rfl theorem map_comp {f : α → β} {g : β → γ} (hf : UniformContinuous f) (hg : UniformContinuous g) : map g ∘ map f = map (g ∘ f) := (map_unique (hg.comp hf) <| by simp only [Function.comp_def, map_mk, hf, hg]).symm end SeparationQuotient
Mathlib/Topology/UniformSpace/Separation.lean
306
307
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Order.Filter.Prod import Mathlib.Order.ConditionallyCompleteLattice.Basic import Mathlib.Order.Filter.Finite import Mathlib.Order.Filter.Bases.Basic /-! # Lift filters along filter and set functions -/ open Set Filter Function namespace Filter variable {α β γ : Type*} {ι : Sort*} section lift variable {f f₁ f₂ : Filter α} {g g₁ g₂ : Set α → Filter β} @[simp] theorem lift_top (g : Set α → Filter β) : (⊤ : Filter α).lift g = g univ := by simp [Filter.lift] /-- If `(p : ι → Prop, s : ι → Set α)` is a basis of a filter `f`, `g` is a monotone function `Set α → Filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → Set α)` is a basis of the filter `g (s i)`, then `(fun (i : ι) (x : β i) ↦ p i ∧ pg i x, fun (i : ι) (x : β i) ↦ sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `Filter.HasBasis` one has to use `Σ i, β i` as the index type, see `Filter.HasBasis.lift`. This lemma states the corresponding `mem_iff` statement without using a sigma type. -/ theorem HasBasis.mem_lift_iff {ι} {p : ι → Prop} {s : ι → Set α} {f : Filter α} (hf : f.HasBasis p s) {β : ι → Type*} {pg : ∀ i, β i → Prop} {sg : ∀ i, β i → Set γ} {g : Set α → Filter γ} (hg : ∀ i, (g <| s i).HasBasis (pg i) (sg i)) (gm : Monotone g) {s : Set γ} : s ∈ f.lift g ↔ ∃ i, p i ∧ ∃ x, pg i x ∧ sg i x ⊆ s := by refine (mem_biInf_of_directed ?_ ⟨univ, univ_sets _⟩).trans ?_ · intro t₁ ht₁ t₂ ht₂ exact ⟨t₁ ∩ t₂, inter_mem ht₁ ht₂, gm inter_subset_left, gm inter_subset_right⟩ · simp only [← (hg _).mem_iff] exact hf.exists_iff fun t₁ t₂ ht H => gm ht H /-- If `(p : ι → Prop, s : ι → Set α)` is a basis of a filter `f`, `g` is a monotone function `Set α → Filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → Set α)` is a basis of the filter `g (s i)`, then `(fun (i : ι) (x : β i) ↦ p i ∧ pg i x, fun (i : ι) (x : β i) ↦ sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `has_basis` one has to use `Σ i, β i` as the index type. See also `Filter.HasBasis.mem_lift_iff` for the corresponding `mem_iff` statement formulated without using a sigma type. -/ theorem HasBasis.lift {ι} {p : ι → Prop} {s : ι → Set α} {f : Filter α} (hf : f.HasBasis p s) {β : ι → Type*} {pg : ∀ i, β i → Prop} {sg : ∀ i, β i → Set γ} {g : Set α → Filter γ} (hg : ∀ i, (g (s i)).HasBasis (pg i) (sg i)) (gm : Monotone g) : (f.lift g).HasBasis (fun i : Σi, β i => p i.1 ∧ pg i.1 i.2) fun i : Σi, β i => sg i.1 i.2 := by refine ⟨fun t => (hf.mem_lift_iff hg gm).trans ?_⟩ simp [Sigma.exists, and_assoc, exists_and_left] theorem mem_lift_sets (hg : Monotone g) {s : Set β} : s ∈ f.lift g ↔ ∃ t ∈ f, s ∈ g t := (f.basis_sets.mem_lift_iff (fun s => (g s).basis_sets) hg).trans <| by simp only [id, exists_mem_subset_iff] theorem sInter_lift_sets (hg : Monotone g) : ⋂₀ { s | s ∈ f.lift g } = ⋂ s ∈ f, ⋂₀ { t | t ∈ g s } := by simp only [sInter_eq_biInter, mem_setOf_eq, Filter.mem_sets, mem_lift_sets hg, iInter_exists, iInter_and, @iInter_comm _ (Set β)] theorem mem_lift {s : Set β} {t : Set α} (ht : t ∈ f) (hs : s ∈ g t) : s ∈ f.lift g := le_principal_iff.mp <| show f.lift g ≤ 𝓟 s from iInf_le_of_le t <| iInf_le_of_le ht <| le_principal_iff.mpr hs theorem lift_le {f : Filter α} {g : Set α → Filter β} {h : Filter β} {s : Set α} (hs : s ∈ f) (hg : g s ≤ h) : f.lift g ≤ h := iInf₂_le_of_le s hs hg theorem le_lift {f : Filter α} {g : Set α → Filter β} {h : Filter β} : h ≤ f.lift g ↔ ∀ s ∈ f, h ≤ g s := le_iInf₂_iff theorem lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := iInf_mono fun s => iInf_mono' fun hs => ⟨hf hs, hg s⟩ theorem lift_mono' (hg : ∀ s ∈ f, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := iInf₂_mono hg theorem tendsto_lift {m : γ → β} {l : Filter γ} : Tendsto m l (f.lift g) ↔ ∀ s ∈ f, Tendsto m l (g s) := by simp only [Filter.lift, tendsto_iInf] theorem map_lift_eq {m : β → γ} (hg : Monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have : Monotone (map m ∘ g) := map_mono.comp hg Filter.ext fun s => by simp only [mem_lift_sets hg, mem_lift_sets this, exists_prop, mem_map, Function.comp_apply] theorem comap_lift_eq {m : γ → β} : comap m (f.lift g) = f.lift (comap m ∘ g) := by simp only [Filter.lift, comap_iInf]; rfl theorem comap_lift_eq2 {m : β → α} {g : Set β → Filter γ} (hg : Monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_iInf₂ fun s hs => iInf₂_le (m ⁻¹' s) ⟨s, hs, Subset.rfl⟩) (le_iInf₂ fun _s ⟨s', hs', h_sub⟩ => iInf₂_le_of_le s' hs' <| hg h_sub)
theorem lift_map_le {g : Set β → Filter γ} {m : α → β} : (map m f).lift g ≤ f.lift (g ∘ image m) := le_lift.2 fun _s hs => lift_le (image_mem_map hs) le_rfl
Mathlib/Order/Filter/Lift.lean
106
108
/- 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.LinearAlgebra.AffineSpace.Independent import Mathlib.LinearAlgebra.AffineSpace.Pointwise import Mathlib.LinearAlgebra.Basis.SMul /-! # Affine bases and barycentric coordinates Suppose `P` is an affine space modelled on the module `V` over the ring `k`, and `p : ι → P` is an affine-independent family of points spanning `P`. Given this data, each point `q : P` may be written uniquely as an affine combination: `q = w₀ p₀ + w₁ p₁ + ⋯` for some (finitely-supported) weights `wᵢ`. For each `i : ι`, we thus have an affine map `P →ᵃ[k] k`, namely `q ↦ wᵢ`. This family of maps is known as the family of barycentric coordinates. It is defined in this file. ## The construction Fixing `i : ι`, and allowing `j : ι` to range over the values `j ≠ i`, we obtain a basis `bᵢ` of `V` defined by `bᵢ j = p j -ᵥ p i`. Let `fᵢ j : V →ₗ[k] k` be the corresponding dual basis and let `fᵢ = ∑ j, fᵢ j : V →ₗ[k] k` be the corresponding "sum of all coordinates" form. Then the `i`th barycentric coordinate of `q : P` is `1 - fᵢ (q -ᵥ p i)`. ## Main definitions * `AffineBasis`: a structure representing an affine basis of an affine space. * `AffineBasis.coord`: the map `P →ᵃ[k] k` corresponding to `i : ι`. * `AffineBasis.coord_apply_eq`: the behaviour of `AffineBasis.coord i` on `p i`. * `AffineBasis.coord_apply_ne`: the behaviour of `AffineBasis.coord i` on `p j` when `j ≠ i`. * `AffineBasis.coord_apply`: the behaviour of `AffineBasis.coord i` on `p j` for general `j`. * `AffineBasis.coord_apply_combination`: the characterisation of `AffineBasis.coord i` in terms of affine combinations, i.e., `AffineBasis.coord i (w₀ p₀ + w₁ p₁ + ⋯) = wᵢ`. ## TODO * Construct the affine equivalence between `P` and `{ f : ι →₀ k | f.sum = 1 }`. -/ open Affine Set open scoped Pointwise universe u₁ u₂ u₃ u₄ /-- An affine basis is a family of affine-independent points whose span is the top subspace. -/ structure AffineBasis (ι : Type u₁) (k : Type u₂) {V : Type u₃} (P : Type u₄) [AddCommGroup V] [AffineSpace V P] [Ring k] [Module k V] where protected toFun : ι → P protected ind' : AffineIndependent k toFun protected tot' : affineSpan k (range toFun) = ⊤ variable {ι ι' G G' k V P : Type*} [AddCommGroup V] [AffineSpace V P] namespace AffineBasis section Ring variable [Ring k] [Module k V] (b : AffineBasis ι k P) {s : Finset ι} {i j : ι} (e : ι ≃ ι') /-- The unique point in a single-point space is the simplest example of an affine basis. -/ instance : Inhabited (AffineBasis PUnit k PUnit) := ⟨⟨id, affineIndependent_of_subsingleton k id, by simp⟩⟩ instance instFunLike : FunLike (AffineBasis ι k P) ι P where coe := AffineBasis.toFun coe_injective' f g h := by cases f; cases g; congr @[ext] theorem ext {b₁ b₂ : AffineBasis ι k P} (h : (b₁ : ι → P) = b₂) : b₁ = b₂ := DFunLike.coe_injective h theorem ind : AffineIndependent k b := b.ind' theorem tot : affineSpan k (range b) = ⊤ := b.tot' include b in protected theorem nonempty : Nonempty ι := not_isEmpty_iff.mp fun hι => by simpa only [@range_eq_empty _ _ hι, AffineSubspace.span_empty, bot_ne_top] using b.tot /-- Composition of an affine basis and an equivalence of index types. -/ def reindex (e : ι ≃ ι') : AffineBasis ι' k P := ⟨b ∘ e.symm, b.ind.comp_embedding e.symm.toEmbedding, by rw [e.symm.surjective.range_comp] exact b.3⟩ @[simp, norm_cast] theorem coe_reindex : ⇑(b.reindex e) = b ∘ e.symm := rfl @[simp] theorem reindex_apply (i' : ι') : b.reindex e i' = b (e.symm i') := rfl @[simp] theorem reindex_refl : b.reindex (Equiv.refl _) = b := ext rfl /-- Given an affine basis for an affine space `P`, if we single out one member of the family, we obtain a linear basis for the model space `V`. The linear basis corresponding to the singled-out member `i : ι` is indexed by `{j : ι // j ≠ i}` and its `j`th element is `b j -ᵥ b i`. (See `basisOf_apply`.) -/ noncomputable def basisOf (i : ι) : Basis { j : ι // j ≠ i } k V := Basis.mk ((affineIndependent_iff_linearIndependent_vsub k b i).mp b.ind) (by suffices Submodule.span k (range fun j : { x // x ≠ i } => b ↑j -ᵥ b i) = vectorSpan k (range b) by rw [this, ← direction_affineSpan, b.tot, AffineSubspace.direction_top] conv_rhs => rw [← image_univ] rw [vectorSpan_image_eq_span_vsub_set_right_ne k b (mem_univ i)] congr ext v simp) @[simp] theorem basisOf_apply (i : ι) (j : { j : ι // j ≠ i }) : b.basisOf i j = b ↑j -ᵥ b i := by simp [basisOf] @[simp] theorem basisOf_reindex (i : ι') : (b.reindex e).basisOf i = (b.basisOf <| e.symm i).reindex (e.subtypeEquiv fun _ => e.eq_symm_apply.not) := by ext j simp /-- The `i`th barycentric coordinate of a point. -/ noncomputable def coord (i : ι) : P →ᵃ[k] k where toFun q := 1 - (b.basisOf i).sumCoords (q -ᵥ b i) linear := -(b.basisOf i).sumCoords map_vadd' q v := by rw [vadd_vsub_assoc, LinearMap.map_add, vadd_eq_add, LinearMap.neg_apply, sub_add_eq_sub_sub_swap, add_comm, sub_eq_add_neg]
@[simp] theorem linear_eq_sumCoords (i : ι) : (b.coord i).linear = -(b.basisOf i).sumCoords := rfl @[simp]
Mathlib/LinearAlgebra/AffineSpace/Basis.lean
139
143
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Andrew Yang -/ import Mathlib.AlgebraicGeometry.ProjectiveSpectrum.StructureSheaf import Mathlib.AlgebraicGeometry.GammaSpecAdjunction import Mathlib.RingTheory.GradedAlgebra.Radical /-! # Proj as a scheme This file is to prove that `Proj` is a scheme. ## Notation * `Proj` : `Proj` as a locally ringed space * `Proj.T` : the underlying topological space of `Proj` * `Proj| U` : `Proj` restricted to some open set `U` * `Proj.T| U` : the underlying topological space of `Proj` restricted to open set `U` * `pbo f` : basic open set at `f` in `Proj` * `Spec` : `Spec` as a locally ringed space * `Spec.T` : the underlying topological space of `Spec` * `sbo g` : basic open set at `g` in `Spec` * `A⁰_x` : the degree zero part of localized ring `Aₓ` ## Implementation In `AlgebraicGeometry/ProjectiveSpectrum/StructureSheaf.lean`, we have given `Proj` a structure sheaf so that `Proj` is a locally ringed space. In this file we will prove that `Proj` equipped with this structure sheaf is a scheme. We achieve this by using an affine cover by basic open sets in `Proj`, more specifically: 1. We prove that `Proj` can be covered by basic open sets at homogeneous element of positive degree. 2. We prove that for any homogeneous element `f : A` of positive degree `m`, `Proj.T | (pbo f)` is homeomorphic to `Spec.T A⁰_f`: - forward direction `toSpec`: for any `x : pbo f`, i.e. a relevant homogeneous prime ideal `x`, send it to `A⁰_f ∩ span {g / 1 | g ∈ x}` (see `ProjIsoSpecTopComponent.IoSpec.carrier`). This ideal is prime, the proof is in `ProjIsoSpecTopComponent.ToSpec.toFun`. The fact that this function is continuous is found in `ProjIsoSpecTopComponent.toSpec` - backward direction `fromSpec`: for any `q : Spec A⁰_f`, we send it to `{a | ∀ i, aᵢᵐ/fⁱ ∈ q}`; we need this to be a homogeneous prime ideal that is relevant. * This is in fact an ideal, the proof can be found in `ProjIsoSpecTopComponent.FromSpec.carrier.asIdeal`; * This ideal is also homogeneous, the proof can be found in `ProjIsoSpecTopComponent.FromSpec.carrier.asIdeal.homogeneous`; * This ideal is relevant, the proof can be found in `ProjIsoSpecTopComponent.FromSpec.carrier.relevant`; * This ideal is prime, the proof can be found in `ProjIsoSpecTopComponent.FromSpec.carrier.asIdeal.prime`. Hence we have a well defined function `Spec.T A⁰_f → Proj.T | (pbo f)`, this function is called `ProjIsoSpecTopComponent.FromSpec.toFun`. But to prove the continuity of this function, we need to prove `fromSpec ∘ toSpec` and `toSpec ∘ fromSpec` are both identities; these are achieved in `ProjIsoSpecTopComponent.fromSpec_toSpec` and `ProjIsoSpecTopComponent.toSpec_fromSpec`. 3. Then we construct a morphism of locally ringed spaces `α : Proj| (pbo f) ⟶ Spec.T A⁰_f` as the following: by the Gamma-Spec adjunction, it is sufficient to construct a ring map `A⁰_f → Γ(Proj, pbo f)` from the ring of homogeneous localization of `A` away from `f` to the local sections of structure sheaf of projective spectrum on the basic open set around `f`. The map `A⁰_f → Γ(Proj, pbo f)` is constructed in `awayToΓ` and is defined by sending `s ∈ A⁰_f` to the section `x ↦ s` on `pbo f`. ## Main Definitions and Statements For a homogeneous element `f` of degree `m` * `ProjIsoSpecTopComponent.toSpec`: the continuous map between `Proj.T| pbo f` and `Spec.T A⁰_f` defined by sending `x : Proj| (pbo f)` to `A⁰_f ∩ span {g / 1 | g ∈ x}`. We also denote this map as `ψ`. * `ProjIsoSpecTopComponent.ToSpec.preimage_eq`: for any `a: A`, if `a/f^m` has degree zero, then the preimage of `sbo a/f^m` under `toSpec f` is `pbo f ∩ pbo a`. If we further assume `m` is positive * `ProjIsoSpecTopComponent.fromSpec`: the continuous map between `Spec.T A⁰_f` and `Proj.T| pbo f` defined by sending `q` to `{a | aᵢᵐ/fⁱ ∈ q}` where `aᵢ` is the `i`-th coordinate of `a`. We also denote this map as `φ` * `projIsoSpecTopComponent`: the homeomorphism `Proj.T| pbo f ≅ Spec.T A⁰_f` obtained by `φ` and `ψ`. * `ProjectiveSpectrum.Proj.toSpec`: the morphism of locally ringed spaces between `Proj| pbo f` and `Spec A⁰_f` corresponding to the ring map `A⁰_f → Γ(Proj, pbo f)` under the Gamma-Spec adjunction defined by sending `s` to the section `x ↦ s` on `pbo f`. Finally, * `AlgebraicGeometry.Proj`: for any `ℕ`-graded ring `A`, `Proj A` is locally affine, hence is a scheme. ## Reference * [Robin Hartshorne, *Algebraic Geometry*][Har77]: Chapter II.2 Proposition 2.5 -/ noncomputable section namespace AlgebraicGeometry open scoped DirectSum Pointwise open DirectSum SetLike.GradedMonoid Localization open Finset hiding mk_zero variable {R A : Type*} variable [CommRing R] [CommRing A] [Algebra R A] variable (𝒜 : ℕ → Submodule R A) variable [GradedAlgebra 𝒜] open TopCat TopologicalSpace open CategoryTheory Opposite open ProjectiveSpectrum.StructureSheaf -- Porting note: currently require lack of hygiene to use in variable declarations -- maybe all make into notation3? set_option hygiene false /-- `Proj` as a locally ringed space -/ local notation3 "Proj" => Proj.toLocallyRingedSpace 𝒜 /-- The underlying topological space of `Proj` -/ local notation3 "Proj.T" => PresheafedSpace.carrier <| SheafedSpace.toPresheafedSpace <| LocallyRingedSpace.toSheafedSpace <| Proj.toLocallyRingedSpace 𝒜 /-- `Proj` restrict to some open set -/ macro "Proj| " U:term : term => `((Proj.toLocallyRingedSpace 𝒜).restrict (Opens.isOpenEmbedding (X := Proj.T) ($U : Opens Proj.T))) /-- the underlying topological space of `Proj` restricted to some open set -/ local notation "Proj.T| " U => PresheafedSpace.carrier <| SheafedSpace.toPresheafedSpace <| LocallyRingedSpace.toSheafedSpace <| (LocallyRingedSpace.restrict Proj (Opens.isOpenEmbedding (X := Proj.T) (U : Opens Proj.T))) /-- basic open sets in `Proj` -/ local notation "pbo " x => ProjectiveSpectrum.basicOpen 𝒜 x /-- basic open sets in `Spec` -/ local notation "sbo " f => PrimeSpectrum.basicOpen f /-- `Spec` as a locally ringed space -/ local notation3 "Spec " ring => Spec.locallyRingedSpaceObj (CommRingCat.of ring) /-- the underlying topological space of `Spec` -/ local notation "Spec.T " ring => (Spec.locallyRingedSpaceObj (CommRingCat.of ring)).toSheafedSpace.toPresheafedSpace.1 local notation3 "A⁰_ " f => HomogeneousLocalization.Away 𝒜 f namespace ProjIsoSpecTopComponent /- This section is to construct the homeomorphism between `Proj` restricted at basic open set at a homogeneous element `x` and `Spec A⁰ₓ` where `A⁰ₓ` is the degree zero part of the localized ring `Aₓ`. -/ namespace ToSpec open Ideal -- This section is to construct the forward direction : -- So for any `x` in `Proj| (pbo f)`, we need some point in `Spec A⁰_f`, i.e. a prime ideal, -- and we need this correspondence to be continuous in their Zariski topology. variable {𝒜} variable {f : A} {m : ℕ} (x : Proj| (pbo f)) /-- For any `x` in `Proj| (pbo f)`, the corresponding ideal in `Spec A⁰_f`. This fact that this ideal is prime is proven in `TopComponent.Forward.toFun`. -/ def carrier : Ideal (A⁰_ f) := Ideal.comap (algebraMap (A⁰_ f) (Away f)) (x.val.asHomogeneousIdeal.toIdeal.map (algebraMap A (Away f))) @[simp] theorem mk_mem_carrier (z : HomogeneousLocalization.NumDenSameDeg 𝒜 (.powers f)) : HomogeneousLocalization.mk z ∈ carrier x ↔ z.num.1 ∈ x.1.asHomogeneousIdeal := by rw [carrier, Ideal.mem_comap, HomogeneousLocalization.algebraMap_apply, HomogeneousLocalization.val_mk, Localization.mk_eq_mk', IsLocalization.mk'_eq_mul_mk'_one, mul_comm, Ideal.unit_mul_mem_iff_mem, ← Ideal.mem_comap, IsLocalization.comap_map_of_isPrime_disjoint (.powers f)] · rfl · infer_instance · exact (disjoint_powers_iff_not_mem _ (Ideal.IsPrime.isRadical inferInstance)).mpr x.2 · exact isUnit_of_invertible _ theorem isPrime_carrier : Ideal.IsPrime (carrier x) := by refine Ideal.IsPrime.comap _ (hK := ?_) exact IsLocalization.isPrime_of_isPrime_disjoint (Submonoid.powers f) _ _ inferInstance ((disjoint_powers_iff_not_mem _ (Ideal.IsPrime.isRadical inferInstance)).mpr x.2) variable (f) /-- The function between the basic open set `D(f)` in `Proj` to the corresponding basic open set in `Spec A⁰_f`. This is bundled into a continuous map in `TopComponent.forward`. -/ @[simps -isSimp] def toFun (x : Proj.T| pbo f) : Spec.T A⁰_ f := ⟨carrier x, isPrime_carrier x⟩ /- The preimage of basic open set `D(a/f^n)` in `Spec A⁰_f` under the forward map from `Proj A` to `Spec A⁰_f` is the basic open set `D(a) ∩ D(f)` in `Proj A`. This lemma is used to prove that the forward map is continuous. -/ theorem preimage_basicOpen (z : HomogeneousLocalization.NumDenSameDeg 𝒜 (.powers f)) : toFun f ⁻¹' (sbo (HomogeneousLocalization.mk z) : Set (PrimeSpectrum (A⁰_ f))) = Subtype.val ⁻¹' (pbo z.num.1 : Set (ProjectiveSpectrum 𝒜)) := Set.ext fun y ↦ (mk_mem_carrier y z).not end ToSpec section /-- The continuous function from the basic open set `D(f)` in `Proj` to the corresponding basic open set in `Spec A⁰_f`. -/ @[simps! -isSimp hom_apply_asIdeal] def toSpec (f : A) : (Proj.T| pbo f) ⟶ Spec.T A⁰_ f := TopCat.ofHom { toFun := ToSpec.toFun f continuous_toFun := by rw [PrimeSpectrum.isTopologicalBasis_basic_opens.continuous_iff] rintro _ ⟨x, rfl⟩ obtain ⟨x, rfl⟩ := Quotient.mk''_surjective x rw [ToSpec.preimage_basicOpen] exact (pbo x.num).2.preimage continuous_subtype_val } variable {𝒜} in lemma toSpec_preimage_basicOpen {f} (z : HomogeneousLocalization.NumDenSameDeg 𝒜 (.powers f)) : toSpec 𝒜 f ⁻¹' (sbo (HomogeneousLocalization.mk z) : Set (PrimeSpectrum (A⁰_ f))) = Subtype.val ⁻¹' (pbo z.num.1 : Set (ProjectiveSpectrum 𝒜)) := ToSpec.preimage_basicOpen f z end namespace FromSpec open GradedAlgebra SetLike open Finset hiding mk_zero open HomogeneousLocalization variable {𝒜} variable {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) open Lean Meta Elab Tactic macro "mem_tac_aux" : tactic => `(tactic| first | exact pow_mem_graded _ (Submodule.coe_mem _) | exact natCast_mem_graded _ _ | exact pow_mem_graded _ f_deg) macro "mem_tac" : tactic => `(tactic| first | mem_tac_aux | repeat (all_goals (apply SetLike.GradedMonoid.toGradedMul.mul_mem)); mem_tac_aux) /-- The function from `Spec A⁰_f` to `Proj|D(f)` is defined by `q ↦ {a | aᵢᵐ/fⁱ ∈ q}`, i.e. sending `q` a prime ideal in `A⁰_f` to the homogeneous prime relevant ideal containing only and all the elements `a : A` such that for every `i`, the degree 0 element formed by dividing the `m`-th power of the `i`-th projection of `a` by the `i`-th power of the degree-`m` homogeneous element `f`, lies in `q`. The set `{a | aᵢᵐ/fⁱ ∈ q}` * is an ideal, as proved in `carrier.asIdeal`; * is homogeneous, as proved in `carrier.asHomogeneousIdeal`; * is prime, as proved in `carrier.asIdeal.prime`; * is relevant, as proved in `carrier.relevant`. -/ def carrier (f_deg : f ∈ 𝒜 m) (q : Spec.T A⁰_ f) : Set A := {a | ∀ i, (HomogeneousLocalization.mk ⟨m * i, ⟨proj 𝒜 i a ^ m, by rw [← smul_eq_mul]; mem_tac⟩, ⟨f ^ i, by rw [mul_comm]; mem_tac⟩, ⟨_, rfl⟩⟩ : A⁰_ f) ∈ q.1} theorem mem_carrier_iff (q : Spec.T A⁰_ f) (a : A) : a ∈ carrier f_deg q ↔ ∀ i, (HomogeneousLocalization.mk ⟨m * i, ⟨proj 𝒜 i a ^ m, by rw [← smul_eq_mul]; mem_tac⟩, ⟨f ^ i, by rw [mul_comm]; mem_tac⟩, ⟨_, rfl⟩⟩ : A⁰_ f) ∈ q.1 := Iff.rfl theorem mem_carrier_iff' (q : Spec.T A⁰_ f) (a : A) : a ∈ carrier f_deg q ↔ ∀ i, (Localization.mk (proj 𝒜 i a ^ m) ⟨f ^ i, ⟨i, rfl⟩⟩ : Localization.Away f) ∈ algebraMap (HomogeneousLocalization.Away 𝒜 f) (Localization.Away f) '' { s | s ∈ q.1 } := (mem_carrier_iff f_deg q a).trans (by constructor <;> intro h i <;> specialize h i · rw [Set.mem_image]; refine ⟨_, h, rfl⟩ · rw [Set.mem_image] at h; rcases h with ⟨x, h, hx⟩ change x ∈ q.asIdeal at h convert h rw [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_mk] dsimp only [Subtype.coe_mk]; rw [← hx]; rfl) theorem mem_carrier_iff_of_mem (hm : 0 < m) (q : Spec.T A⁰_ f) (a : A) {n} (hn : a ∈ 𝒜 n) : a ∈ carrier f_deg q ↔ (HomogeneousLocalization.mk ⟨m * n, ⟨a ^ m, pow_mem_graded m hn⟩, ⟨f ^ n, by rw [mul_comm]; mem_tac⟩, ⟨_, rfl⟩⟩ : A⁰_ f) ∈ q.asIdeal := by trans (HomogeneousLocalization.mk ⟨m * n, ⟨proj 𝒜 n a ^ m, by rw [← smul_eq_mul]; mem_tac⟩, ⟨f ^ n, by rw [mul_comm]; mem_tac⟩, ⟨_, rfl⟩⟩ : A⁰_ f) ∈ q.asIdeal · refine ⟨fun h ↦ h n, fun h i ↦ if hi : i = n then hi ▸ h else ?_⟩ convert zero_mem q.asIdeal apply HomogeneousLocalization.val_injective simp only [proj_apply, decompose_of_mem_ne _ hn (Ne.symm hi), zero_pow hm.ne', HomogeneousLocalization.val_mk, Localization.mk_zero, HomogeneousLocalization.val_zero] · simp only [proj_apply, decompose_of_mem_same _ hn] theorem mem_carrier_iff_of_mem_mul (hm : 0 < m) (q : Spec.T A⁰_ f) (a : A) {n} (hn : a ∈ 𝒜 (n * m)) : a ∈ carrier f_deg q ↔ (HomogeneousLocalization.mk ⟨m * n, ⟨a, mul_comm n m ▸ hn⟩, ⟨f ^ n, by rw [mul_comm]; mem_tac⟩, ⟨_, rfl⟩⟩ : A⁰_ f) ∈ q.asIdeal := by rw [mem_carrier_iff_of_mem f_deg hm q a hn, iff_iff_eq, eq_comm, ← Ideal.IsPrime.pow_mem_iff_mem (α := A⁰_ f) inferInstance m hm] congr 1 apply HomogeneousLocalization.val_injective simp only [HomogeneousLocalization.val_mk, HomogeneousLocalization.val_pow, Localization.mk_pow, pow_mul] rfl theorem num_mem_carrier_iff (hm : 0 < m) (q : Spec.T A⁰_ f) (z : HomogeneousLocalization.NumDenSameDeg 𝒜 (.powers f)) : z.num.1 ∈ carrier f_deg q ↔ HomogeneousLocalization.mk z ∈ q.asIdeal := by obtain ⟨n, hn : f ^ n = _⟩ := z.den_mem have : f ^ n ≠ 0 := fun e ↦ by have := HomogeneousLocalization.subsingleton 𝒜 (x := .powers f) ⟨n, e⟩ exact IsEmpty.elim (inferInstanceAs (IsEmpty (PrimeSpectrum (A⁰_ f)))) q convert mem_carrier_iff_of_mem_mul f_deg hm q z.num.1 (n := n) ?_ using 2 · apply HomogeneousLocalization.val_injective; simp only [hn, HomogeneousLocalization.val_mk] · have := degree_eq_of_mem_mem 𝒜 (SetLike.pow_mem_graded n f_deg) (hn.symm ▸ z.den.2) this rw [← smul_eq_mul, this]; exact z.num.2 theorem carrier.add_mem (q : Spec.T A⁰_ f) {a b : A} (ha : a ∈ carrier f_deg q) (hb : b ∈ carrier f_deg q) : a + b ∈ carrier f_deg q := by refine fun i => (q.2.mem_or_mem ?_).elim id id change (HomogeneousLocalization.mk ⟨_, _, _, _⟩ : A⁰_ f) ∈ q.1; dsimp only [Subtype.coe_mk] simp_rw [← pow_add, map_add, add_pow, mul_comm, ← nsmul_eq_mul] let g : ℕ → A⁰_ f := fun j => (m + m).choose j • if h2 : m + m < j then (0 : A⁰_ f) else -- Porting note: inlining `l`, `r` causes a "can't synth HMul A⁰_ f A⁰_ f ?" error if h1 : j ≤ m then letI l : A⁰_ f := HomogeneousLocalization.mk ⟨m * i, ⟨proj 𝒜 i a ^ j * proj 𝒜 i b ^ (m - j), ?_⟩, ⟨_, by rw [mul_comm]; mem_tac⟩, ⟨i, rfl⟩⟩ letI r : A⁰_ f := (HomogeneousLocalization.mk ⟨m * i, ⟨proj 𝒜 i b ^ m, by rw [← smul_eq_mul]; mem_tac⟩, ⟨_, by rw [mul_comm]; mem_tac⟩, ⟨i, rfl⟩⟩) l * r else letI l : A⁰_ f := HomogeneousLocalization.mk ⟨m * i, ⟨proj 𝒜 i a ^ m, by rw [← smul_eq_mul]; mem_tac⟩, ⟨_, by rw [mul_comm]; mem_tac⟩, ⟨i, rfl⟩⟩ letI r : A⁰_ f := HomogeneousLocalization.mk ⟨m * i, ⟨proj 𝒜 i a ^ (j - m) * proj 𝒜 i b ^ (m + m - j), ?_⟩, ⟨_, by rw [mul_comm]; mem_tac⟩, ⟨i, rfl⟩⟩ l * r rotate_left · rw [(_ : m * i = _)] apply GradedMonoid.toGradedMul.mul_mem <;> mem_tac_aux rw [← add_smul, Nat.add_sub_of_le h1]; rfl · rw [(_ : m * i = _)] apply GradedMonoid.toGradedMul.mul_mem (i := (j-m) • i) (j := (m + m - j) • i) <;> mem_tac_aux rw [← add_smul]; congr; zify [le_of_not_lt h2, le_of_not_le h1]; abel convert_to ∑ i ∈ range (m + m + 1), g i ∈ q.1; swap · refine q.1.sum_mem fun j _ => nsmul_mem ?_ _; split_ifs exacts [q.1.zero_mem, q.1.mul_mem_left _ (hb i), q.1.mul_mem_right _ (ha i)] rw [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_mk] change _ = (algebraMap (HomogeneousLocalization.Away 𝒜 f) (Localization.Away f)) _ dsimp only [Subtype.coe_mk]; rw [map_sum, mk_sum] apply Finset.sum_congr rfl fun j hj => _ intro j hj change _ = HomogeneousLocalization.val _ rw [HomogeneousLocalization.val_smul] split_ifs with h2 h1 · exact ((Finset.mem_range.1 hj).not_le h2).elim all_goals simp only [HomogeneousLocalization.val_mul, HomogeneousLocalization.val_zero, HomogeneousLocalization.val_mk, Subtype.coe_mk, Localization.mk_mul, ← smul_mk]; congr 2 · dsimp; rw [mul_assoc, ← pow_add, add_comm (m - j), Nat.add_sub_assoc h1] · simp_rw [pow_add]; rfl · dsimp; rw [← mul_assoc, ← pow_add, Nat.add_sub_of_le (le_of_not_le h1)] · simp_rw [pow_add]; rfl variable (hm : 0 < m) (q : Spec.T A⁰_ f) include hm theorem carrier.zero_mem : (0 : A) ∈ carrier f_deg q := fun i => by convert Submodule.zero_mem q.1 using 1 rw [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_mk, HomogeneousLocalization.val_zero]; simp_rw [map_zero, zero_pow hm.ne'] convert Localization.mk_zero (S := Submonoid.powers f) _ using 1 theorem carrier.smul_mem (c x : A) (hx : x ∈ carrier f_deg q) : c • x ∈ carrier f_deg q := by revert c refine DirectSum.Decomposition.inductionOn 𝒜 ?_ ?_ ?_ · rw [zero_smul]; exact carrier.zero_mem f_deg hm _ · rintro n ⟨a, ha⟩ i simp_rw [proj_apply, smul_eq_mul, coe_decompose_mul_of_left_mem 𝒜 i ha] let product : A⁰_ f := (HomogeneousLocalization.mk ⟨_, ⟨a ^ m, pow_mem_graded m ha⟩, ⟨_, ?_⟩, ⟨n, rfl⟩⟩ : A⁰_ f) * (HomogeneousLocalization.mk ⟨_, ⟨proj 𝒜 (i - n) x ^ m, by mem_tac⟩, ⟨_, ?_⟩, ⟨i - n, rfl⟩⟩ : A⁰_ f) · split_ifs with h · convert_to product ∈ q.1 · dsimp [product] rw [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_mk, HomogeneousLocalization.val_mul, HomogeneousLocalization.val_mk, HomogeneousLocalization.val_mk] · simp_rw [mul_pow]; rw [Localization.mk_mul] · congr; rw [← pow_add, Nat.add_sub_of_le h] · apply Ideal.mul_mem_left (α := A⁰_ f) _ _ (hx _) rw [(_ : m • n = _)] · mem_tac · simp only [smul_eq_mul, mul_comm] · simpa only [map_zero, zero_pow hm.ne'] using zero_mem f_deg hm q i rw [(_ : m • (i - n) = _)] · mem_tac · simp only [smul_eq_mul, mul_comm] · simp_rw [add_smul]; exact fun _ _ => carrier.add_mem f_deg q /-- For a prime ideal `q` in `A⁰_f`, the set `{a | aᵢᵐ/fⁱ ∈ q}` as an ideal. -/ def carrier.asIdeal : Ideal A where carrier := carrier f_deg q zero_mem' := carrier.zero_mem f_deg hm q add_mem' := carrier.add_mem f_deg q smul_mem' := carrier.smul_mem f_deg hm q theorem carrier.asIdeal.homogeneous : (carrier.asIdeal f_deg hm q).IsHomogeneous 𝒜 := fun i a ha j => (em (i = j)).elim (fun h => h ▸ by simpa only [proj_apply, decompose_coe, of_eq_same] using ha _) fun h => by simpa only [proj_apply, decompose_of_mem_ne 𝒜 (Submodule.coe_mem (decompose 𝒜 a i)) h, zero_pow hm.ne', map_zero] using carrier.zero_mem f_deg hm q j /-- For a prime ideal `q` in `A⁰_f`, the set `{a | aᵢᵐ/fⁱ ∈ q}` as a homogeneous ideal. -/ def carrier.asHomogeneousIdeal : HomogeneousIdeal 𝒜 := ⟨carrier.asIdeal f_deg hm q, carrier.asIdeal.homogeneous f_deg hm q⟩ theorem carrier.denom_not_mem : f ∉ carrier.asIdeal f_deg hm q := fun rid => q.isPrime.ne_top <| (Ideal.eq_top_iff_one _).mpr (by convert rid m rw [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_one, HomogeneousLocalization.val_mk] dsimp simp_rw [decompose_of_mem_same _ f_deg] simp only [mk_eq_monoidOf_mk', Submonoid.LocalizationMap.mk'_self]) theorem carrier.relevant : ¬HomogeneousIdeal.irrelevant 𝒜 ≤ carrier.asHomogeneousIdeal f_deg hm q := fun rid => carrier.denom_not_mem f_deg hm q <| rid <| DirectSum.decompose_of_mem_ne 𝒜 f_deg hm.ne' theorem carrier.asIdeal.ne_top : carrier.asIdeal f_deg hm q ≠ ⊤ := fun rid => carrier.denom_not_mem f_deg hm q (rid.symm ▸ Submodule.mem_top) theorem carrier.asIdeal.prime : (carrier.asIdeal f_deg hm q).IsPrime := (carrier.asIdeal.homogeneous f_deg hm q).isPrime_of_homogeneous_mem_or_mem (carrier.asIdeal.ne_top f_deg hm q) fun {x y} ⟨nx, hnx⟩ ⟨ny, hny⟩ hxy => show (∀ _, _ ∈ _) ∨ ∀ _, _ ∈ _ by rw [← and_forall_ne nx, and_iff_left, ← and_forall_ne ny, and_iff_left] · apply q.2.mem_or_mem; convert hxy (nx + ny) using 1 dsimp simp_rw [decompose_of_mem_same 𝒜 hnx, decompose_of_mem_same 𝒜 hny, decompose_of_mem_same 𝒜 (SetLike.GradedMonoid.toGradedMul.mul_mem hnx hny), mul_pow, pow_add] simp only [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_mk, HomogeneousLocalization.val_mul, Localization.mk_mul] simp only [Submonoid.mk_mul_mk, mk_eq_monoidOf_mk'] all_goals intro n hn; convert q.1.zero_mem using 1 rw [HomogeneousLocalization.ext_iff_val, HomogeneousLocalization.val_mk, HomogeneousLocalization.val_zero]; simp_rw [proj_apply] convert mk_zero (S := Submonoid.powers f) _ rw [decompose_of_mem_ne 𝒜 _ hn.symm, zero_pow hm.ne'] · first | exact hnx | exact hny /-- The function `Spec A⁰_f → Proj|D(f)` sending `q` to `{a | aᵢᵐ/fⁱ ∈ q}`. -/ def toFun : (Spec.T A⁰_ f) → Proj.T| pbo f := fun q => ⟨⟨carrier.asHomogeneousIdeal f_deg hm q, carrier.asIdeal.prime f_deg hm q, carrier.relevant f_deg hm q⟩, (ProjectiveSpectrum.mem_basicOpen _ f _).mp <| carrier.denom_not_mem f_deg hm q⟩ end FromSpec section toSpecFromSpec lemma toSpec_fromSpec {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) (x : Spec.T (A⁰_ f)) : toSpec 𝒜 f (FromSpec.toFun f_deg hm x) = x := by apply PrimeSpectrum.ext ext z obtain ⟨z, rfl⟩ := HomogeneousLocalization.mk_surjective z rw [← FromSpec.num_mem_carrier_iff f_deg hm x] exact ToSpec.mk_mem_carrier _ z end toSpecFromSpec section fromSpecToSpec lemma fromSpec_toSpec {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) (x : Proj.T| pbo f) : FromSpec.toFun f_deg hm (toSpec 𝒜 f x) = x := by refine Subtype.ext <| ProjectiveSpectrum.ext <| HomogeneousIdeal.ext' ?_ intros i z hzi refine (FromSpec.mem_carrier_iff_of_mem f_deg hm _ _ hzi).trans ?_ exact (ToSpec.mk_mem_carrier _ _).trans (x.1.2.pow_mem_iff_mem m hm) lemma toSpec_injective {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : Function.Injective (toSpec 𝒜 f) := by intro x₁ x₂ h have := congr_arg (FromSpec.toFun f_deg hm) h rwa [fromSpec_toSpec, fromSpec_toSpec] at this lemma toSpec_surjective {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : Function.Surjective (toSpec 𝒜 f) := Function.surjective_iff_hasRightInverse |>.mpr ⟨FromSpec.toFun f_deg hm, toSpec_fromSpec 𝒜 f_deg hm⟩ lemma toSpec_bijective {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : Function.Bijective (toSpec (𝒜 := 𝒜) (f := f)) := ⟨toSpec_injective 𝒜 f_deg hm, toSpec_surjective 𝒜 f_deg hm⟩ end fromSpecToSpec namespace toSpec variable {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) include hm f_deg variable {𝒜} in lemma image_basicOpen_eq_basicOpen (a : A) (i : ℕ) : toSpec 𝒜 f '' (Subtype.val ⁻¹' (pbo (decompose 𝒜 a i) : Set (ProjectiveSpectrum 𝒜))) = (PrimeSpectrum.basicOpen (R := A⁰_ f) <| HomogeneousLocalization.mk ⟨m * i, ⟨decompose 𝒜 a i ^ m, smul_eq_mul m i ▸ SetLike.pow_mem_graded _ (Submodule.coe_mem _)⟩, ⟨f^i, by rw [mul_comm]; exact SetLike.pow_mem_graded _ f_deg⟩, ⟨i, rfl⟩⟩).1 := Set.preimage_injective.mpr (toSpec_surjective 𝒜 f_deg hm) <| Set.preimage_image_eq _ (toSpec_injective 𝒜 f_deg hm) ▸ by rw [Opens.carrier_eq_coe, toSpec_preimage_basicOpen, ProjectiveSpectrum.basicOpen_pow 𝒜 _ m hm] end toSpec variable {𝒜} in /-- The continuous function `Spec A⁰_f → Proj|D(f)` sending `q` to `{a | aᵢᵐ/fⁱ ∈ q}` where `m` is the degree of `f` -/ def fromSpec {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : (Spec.T (A⁰_ f)) ⟶ (Proj.T| (pbo f)) := TopCat.ofHom { toFun := FromSpec.toFun f_deg hm continuous_toFun := by rw [isTopologicalBasis_subtype (ProjectiveSpectrum.isTopologicalBasis_basic_opens 𝒜) (pbo f).1 |>.continuous_iff] rintro s ⟨_, ⟨a, rfl⟩, rfl⟩ have h₁ : Subtype.val (p := (pbo f).1) ⁻¹' (pbo a) = ⋃ i : ℕ, Subtype.val (p := (pbo f).1) ⁻¹' (pbo (decompose 𝒜 a i)) := by simp [ProjectiveSpectrum.basicOpen_eq_union_of_projection 𝒜 a] let e : _ ≃ _ := ⟨FromSpec.toFun f_deg hm, ToSpec.toFun f, toSpec_fromSpec _ _ _, fromSpec_toSpec _ _ _⟩ change IsOpen <| e ⁻¹' _ rw [Set.preimage_equiv_eq_image_symm, h₁, Set.image_iUnion] exact isOpen_iUnion fun i ↦ toSpec.image_basicOpen_eq_basicOpen f_deg hm a i ▸ PrimeSpectrum.isOpen_basicOpen } end ProjIsoSpecTopComponent variable {𝒜} in /-- The homeomorphism `Proj|D(f) ≅ Spec A⁰_f` defined by - `φ : Proj|D(f) ⟶ Spec A⁰_f` by sending `x` to `A⁰_f ∩ span {g / 1 | g ∈ x}` - `ψ : Spec A⁰_f ⟶ Proj|D(f)` by sending `q` to `{a | aᵢᵐ/fⁱ ∈ q}`. -/ def projIsoSpecTopComponent {f : A} {m : ℕ} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : (Proj.T| (pbo f)) ≅ (Spec.T (A⁰_ f)) where hom := ProjIsoSpecTopComponent.toSpec 𝒜 f inv := ProjIsoSpecTopComponent.fromSpec f_deg hm hom_inv_id := ConcreteCategory.hom_ext _ _ (ProjIsoSpecTopComponent.fromSpec_toSpec 𝒜 f_deg hm) inv_hom_id := ConcreteCategory.hom_ext _ _ (ProjIsoSpecTopComponent.toSpec_fromSpec 𝒜 f_deg hm) namespace ProjectiveSpectrum.Proj /-- The ring map from `A⁰_ f` to the local sections of the structure sheaf of the projective spectrum of `A` on the basic open set `D(f)` defined by sending `s ∈ A⁰_f` to the section `x ↦ s` on `D(f)`. -/ def awayToSection (f) : CommRingCat.of (A⁰_ f) ⟶ (structureSheaf 𝒜).1.obj (op (pbo f)) := CommRingCat.ofHom -- Have to hint `S`, otherwise it gets unfolded to `structureSheafInType` -- causing `ext` to fail (S := (structureSheaf 𝒜).1.obj (op (pbo f))) { toFun s := ⟨fun x ↦ HomogeneousLocalization.mapId 𝒜 (Submonoid.powers_le.mpr x.2) s, fun x ↦ by obtain ⟨s, rfl⟩ := HomogeneousLocalization.mk_surjective s obtain ⟨n, hn : f ^ n = s.den.1⟩ := s.den_mem exact ⟨_, x.2, 𝟙 _, s.1, s.2, s.3, fun x hsx ↦ x.2 (Ideal.IsPrime.mem_of_pow_mem inferInstance n (hn ▸ hsx)), fun _ ↦ rfl⟩⟩ map_add' _ _ := by ext; simp only [map_add, HomogeneousLocalization.val_add, Proj.add_apply] map_mul' _ _ := by ext; simp only [map_mul, HomogeneousLocalization.val_mul, Proj.mul_apply] map_zero' := by ext; simp only [map_zero, HomogeneousLocalization.val_zero, Proj.zero_apply] map_one' := by ext; simp only [map_one, HomogeneousLocalization.val_one, Proj.one_apply] } lemma awayToSection_germ (f x hx) : awayToSection 𝒜 f ≫ (structureSheaf 𝒜).presheaf.germ _ x hx = CommRingCat.ofHom (HomogeneousLocalization.mapId 𝒜 (Submonoid.powers_le.mpr hx)) ≫ (Proj.stalkIso' 𝒜 x).toCommRingCatIso.inv := by ext z apply (Proj.stalkIso' 𝒜 x).eq_symm_apply.mpr apply Proj.stalkIso'_germ lemma awayToSection_apply (f : A) (x p) : (((ProjectiveSpectrum.Proj.awayToSection 𝒜 f).1 x).val p).val = IsLocalization.map (M := Submonoid.powers f) (T := p.1.1.toIdeal.primeCompl) _ (RingHom.id _) (Submonoid.powers_le.mpr p.2) x.val := by obtain ⟨x, rfl⟩ := HomogeneousLocalization.mk_surjective x show (HomogeneousLocalization.mapId 𝒜 _ _).val = _ dsimp [HomogeneousLocalization.mapId, HomogeneousLocalization.map] rw [Localization.mk_eq_mk', Localization.mk_eq_mk', IsLocalization.map_mk'] rfl /-- The ring map from `A⁰_ f` to the global sections of the structure sheaf of the projective spectrum of `A` restricted to the basic open set `D(f)`. Mathematically, the map is the same as `awayToSection`. -/ def awayToΓ (f) : CommRingCat.of (A⁰_ f) ⟶ LocallyRingedSpace.Γ.obj (op <| Proj| pbo f) := awayToSection 𝒜 f ≫ (ProjectiveSpectrum.Proj.structureSheaf 𝒜).1.map (homOfLE (Opens.isOpenEmbedding_obj_top _).le).op lemma awayToΓ_ΓToStalk (f) (x) : awayToΓ 𝒜 f ≫ (Proj| pbo f).presheaf.Γgerm x = CommRingCat.ofHom (HomogeneousLocalization.mapId 𝒜 (Submonoid.powers_le.mpr x.2)) ≫ (Proj.stalkIso' 𝒜 x.1).toCommRingCatIso.inv ≫ ((Proj.toLocallyRingedSpace 𝒜).restrictStalkIso (Opens.isOpenEmbedding _) x).inv := by rw [awayToΓ, Category.assoc, ← Category.assoc _ (Iso.inv _), Iso.eq_comp_inv, Category.assoc, Category.assoc, Presheaf.Γgerm] rw [LocallyRingedSpace.restrictStalkIso_hom_eq_germ] simp only [Proj.toLocallyRingedSpace, Proj.toSheafedSpace] rw [Presheaf.germ_res, awayToSection_germ] rfl /-- The morphism of locally ringed space from `Proj|D(f)` to `Spec A⁰_f` induced by the ring map `A⁰_ f → Γ(Proj, D(f))` under the gamma spec adjunction. -/ def toSpec (f) : (Proj| pbo f) ⟶ Spec (A⁰_ f) := ΓSpec.locallyRingedSpaceAdjunction.homEquiv (Proj| pbo f) (op (CommRingCat.of <| A⁰_ f)) (awayToΓ 𝒜 f).op open HomogeneousLocalization IsLocalRing lemma toSpec_base_apply_eq_comap {f} (x : Proj| pbo f) : (toSpec 𝒜 f).base x = PrimeSpectrum.comap (mapId 𝒜 (Submonoid.powers_le.mpr x.2)) (closedPoint (AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal)) := by show PrimeSpectrum.comap (awayToΓ 𝒜 f ≫ (Proj| pbo f).presheaf.Γgerm x).hom (IsLocalRing.closedPoint ((Proj| pbo f).presheaf.stalk x)) = _ rw [awayToΓ_ΓToStalk, CommRingCat.hom_comp, PrimeSpectrum.comap_comp] exact congr(PrimeSpectrum.comap _ $(@IsLocalRing.comap_closedPoint (HomogeneousLocalization.AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal) _ _ ((Proj| pbo f).presheaf.stalk x) _ _ _ (isLocalHom_of_isIso _))) lemma toSpec_base_apply_eq {f} (x : Proj| pbo f) : (toSpec 𝒜 f).base x = ProjIsoSpecTopComponent.toSpec 𝒜 f x := toSpec_base_apply_eq_comap 𝒜 x |>.trans <| PrimeSpectrum.ext <| Ideal.ext fun z => show ¬ IsUnit _ ↔ z ∈ ProjIsoSpecTopComponent.ToSpec.carrier _ by obtain ⟨z, rfl⟩ := z.mk_surjective rw [← HomogeneousLocalization.isUnit_iff_isUnit_val, ProjIsoSpecTopComponent.ToSpec.mk_mem_carrier, HomogeneousLocalization.map_mk, HomogeneousLocalization.val_mk, Localization.mk_eq_mk', IsLocalization.AtPrime.isUnit_mk'_iff] exact not_not lemma toSpec_base_isIso {f} {m} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : IsIso (toSpec 𝒜 f).base := by convert (projIsoSpecTopComponent f_deg hm).isIso_hom exact ConcreteCategory.hom_ext _ _ <| toSpec_base_apply_eq 𝒜 lemma mk_mem_toSpec_base_apply {f} (x : Proj| pbo f) (z : NumDenSameDeg 𝒜 (.powers f)) : HomogeneousLocalization.mk z ∈ ((toSpec 𝒜 f).base x).asIdeal ↔ z.num.1 ∈ x.1.asHomogeneousIdeal := (toSpec_base_apply_eq 𝒜 x).symm ▸ ProjIsoSpecTopComponent.ToSpec.mk_mem_carrier _ _ lemma toSpec_preimage_basicOpen {f} (t : NumDenSameDeg 𝒜 (.powers f)) : (Opens.map (toSpec 𝒜 f).base).obj (sbo (HomogeneousLocalization.mk t)) = Opens.comap ⟨_, continuous_subtype_val⟩ (pbo t.num.1) := Opens.ext <| Opens.map_coe _ _ ▸ by convert (ProjIsoSpecTopComponent.ToSpec.preimage_basicOpen f t) exact funext fun _ => toSpec_base_apply_eq _ _ @[reassoc] lemma toOpen_toSpec_val_c_app (f) (U) : StructureSheaf.toOpen (A⁰_ f) U.unop ≫ (toSpec 𝒜 f).c.app U = awayToΓ 𝒜 f ≫ (Proj| pbo f).presheaf.map (homOfLE le_top).op := Eq.trans (by congr) <| ΓSpec.toOpen_comp_locallyRingedSpaceAdjunction_homEquiv_app _ U @[reassoc] lemma toStalk_stalkMap_toSpec (f) (x) : StructureSheaf.toStalk _ _ ≫ (toSpec 𝒜 f).stalkMap x = awayToΓ 𝒜 f ≫ (Proj| pbo f).presheaf.Γgerm x := by rw [StructureSheaf.toStalk, Category.assoc] simp_rw [← Spec.locallyRingedSpaceObj_presheaf'] rw [LocallyRingedSpace.stalkMap_germ (toSpec 𝒜 f), toOpen_toSpec_val_c_app_assoc, Presheaf.germ_res] rfl /-- If `x` is a point in the basic open set `D(f)` where `f` is a homogeneous element of positive degree, then the homogeneously localized ring `A⁰ₓ` has the universal property of the localization of `A⁰_f` at `φ(x)` where `φ : Proj|D(f) ⟶ Spec A⁰_f` is the morphism of locally ringed space constructed as above. -/ lemma isLocalization_atPrime (f) (x : pbo f) {m} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : @IsLocalization (Away 𝒜 f) _ ((toSpec 𝒜 f).base x).asIdeal.primeCompl (AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal) _ (mapId 𝒜 (Submonoid.powers_le.mpr x.2)).toAlgebra := by letI : Algebra (Away 𝒜 f) (AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal) := (mapId 𝒜 (Submonoid.powers_le.mpr x.2)).toAlgebra constructor · rintro ⟨y, hy⟩ obtain ⟨y, rfl⟩ := HomogeneousLocalization.mk_surjective y refine isUnit_of_mul_eq_one _ (.mk ⟨y.deg, y.den, y.num, (mk_mem_toSpec_base_apply _ _ _).not.mp hy⟩) <| val_injective _ ?_ simp only [RingHom.algebraMap_toAlgebra, map_mk, RingHom.id_apply, val_mul, val_mk, mk_eq_mk', val_one, IsLocalization.mk'_mul_mk'_eq_one'] · intro z obtain ⟨⟨i, a, ⟨b, hb⟩, (hb' : b ∉ x.1.1)⟩, rfl⟩ := z.mk_surjective refine ⟨⟨HomogeneousLocalization.mk ⟨i * m, ⟨a * b ^ (m - 1), ?_⟩, ⟨f ^ i, SetLike.pow_mem_graded _ f_deg⟩, ⟨_, rfl⟩⟩, ⟨HomogeneousLocalization.mk ⟨i * m, ⟨b ^ m, mul_comm m i ▸ SetLike.pow_mem_graded _ hb⟩, ⟨f ^ i, SetLike.pow_mem_graded _ f_deg⟩, ⟨_, rfl⟩⟩, (mk_mem_toSpec_base_apply _ _ _).not.mpr <| x.1.1.toIdeal.primeCompl.pow_mem hb' m⟩⟩, val_injective _ ?_⟩ · convert SetLike.mul_mem_graded a.2 (SetLike.pow_mem_graded (m - 1) hb) using 2 rw [← succ_nsmul', tsub_add_cancel_of_le (by omega), mul_comm, smul_eq_mul] · simp only [RingHom.algebraMap_toAlgebra, map_mk, RingHom.id_apply, val_mul, val_mk, mk_eq_mk', ← IsLocalization.mk'_mul, Submonoid.mk_mul_mk, IsLocalization.mk'_eq_iff_eq] rw [mul_comm b, mul_mul_mul_comm, ← pow_succ', mul_assoc, tsub_add_cancel_of_le (by omega)] · intros y z e obtain ⟨y, rfl⟩ := HomogeneousLocalization.mk_surjective y obtain ⟨z, rfl⟩ := HomogeneousLocalization.mk_surjective z obtain ⟨i, c, hc, hc', e⟩ : ∃ i, ∃ c ∈ 𝒜 i, c ∉ x.1.asHomogeneousIdeal ∧ c * (z.den.1 * y.num.1) = c * (y.den.1 * z.num.1) := by apply_fun HomogeneousLocalization.val at e simp only [RingHom.algebraMap_toAlgebra, map_mk, RingHom.id_apply, val_mk, mk_eq_mk', IsLocalization.mk'_eq_iff_eq] at e obtain ⟨⟨c, hcx⟩, hc⟩ := IsLocalization.exists_of_eq (M := x.1.1.toIdeal.primeCompl) e obtain ⟨i, hi⟩ := not_forall.mp ((x.1.1.isHomogeneous.mem_iff _).not.mp hcx) refine ⟨i, _, (decompose 𝒜 c i).2, hi, ?_⟩ apply_fun fun x ↦ (decompose 𝒜 x (i + z.deg + y.deg)).1 at hc conv_rhs at hc => rw [add_right_comm] rwa [← mul_assoc, coe_decompose_mul_add_of_right_mem, coe_decompose_mul_add_of_right_mem, ← mul_assoc, coe_decompose_mul_add_of_right_mem, coe_decompose_mul_add_of_right_mem, mul_assoc, mul_assoc] at hc exacts [y.den.2, z.num.2, z.den.2, y.num.2] refine ⟨⟨HomogeneousLocalization.mk ⟨m * i, ⟨c ^ m, SetLike.pow_mem_graded _ hc⟩, ⟨f ^ i, mul_comm m i ▸ SetLike.pow_mem_graded _ f_deg⟩, ⟨_, rfl⟩⟩, (mk_mem_toSpec_base_apply _ _ _).not.mpr <| x.1.1.toIdeal.primeCompl.pow_mem hc' _⟩, val_injective _ ?_⟩ simp only [val_mul, val_mk, mk_eq_mk', ← IsLocalization.mk'_mul, Submonoid.mk_mul_mk, IsLocalization.mk'_eq_iff_eq, mul_assoc] congr 2 rw [mul_left_comm, mul_left_comm y.den.1, ← tsub_add_cancel_of_le (show 1 ≤ m from hm), pow_succ, mul_assoc, mul_assoc, e] /-- For an element `f ∈ A` with positive degree and a homogeneous ideal in `D(f)`, we have that the stalk of `Spec A⁰_ f` at `y` is isomorphic to `A⁰ₓ` where `y` is the point in `Proj` corresponding to `x`. -/ def specStalkEquiv (f) (x : pbo f) {m} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : (Spec.structureSheaf (A⁰_ f)).presheaf.stalk ((toSpec 𝒜 f).base x) ≅ CommRingCat.of (AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal) := letI : Algebra (Away 𝒜 f) (AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal) := (mapId 𝒜 (Submonoid.powers_le.mpr x.2)).toAlgebra haveI := isLocalization_atPrime 𝒜 f x f_deg hm (IsLocalization.algEquiv (R := A⁰_ f) (M := ((toSpec 𝒜 f).base x).asIdeal.primeCompl) (S := (Spec.structureSheaf (A⁰_ f)).presheaf.stalk ((toSpec 𝒜 f).base x)) (Q := AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal)).toRingEquiv.toCommRingCatIso lemma toStalk_specStalkEquiv (f) (x : pbo f) {m} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) : StructureSheaf.toStalk (A⁰_ f) ((toSpec 𝒜 f).base x) ≫ (specStalkEquiv 𝒜 f x f_deg hm).hom = CommRingCat.ofHom (mapId _ <| Submonoid.powers_le.mpr x.2) := letI : Algebra (Away 𝒜 f) (AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal) := (mapId 𝒜 (Submonoid.powers_le.mpr x.2)).toAlgebra letI := isLocalization_atPrime 𝒜 f x f_deg hm CommRingCat.hom_ext (IsLocalization.algEquiv (R := A⁰_ f) (M := ((toSpec 𝒜 f).base x).asIdeal.primeCompl) (S := (Spec.structureSheaf (A⁰_ f)).presheaf.stalk ((toSpec 𝒜 f).base x)) (Q := AtPrime 𝒜 x.1.asHomogeneousIdeal.toIdeal)).toAlgHom.comp_algebraMap lemma stalkMap_toSpec (f) (x : pbo f) {m} (f_deg : f ∈ 𝒜 m) (hm : 0 < m) :
(toSpec 𝒜 f).stalkMap x = (specStalkEquiv 𝒜 f x f_deg hm).hom ≫ (Proj.stalkIso' 𝒜 x.1).toCommRingCatIso.inv ≫ ((Proj.toLocallyRingedSpace 𝒜).restrictStalkIso (Opens.isOpenEmbedding _) x).inv := CommRingCat.hom_ext <| IsLocalization.ringHom_ext (R := A⁰_ f) ((toSpec 𝒜 f).base x).asIdeal.primeCompl (S := (Spec.structureSheaf (A⁰_ f)).presheaf.stalk ((toSpec 𝒜 f).base x)) <| CommRingCat.hom_ext_iff.mp <| (toStalk_stalkMap_toSpec _ _ _).trans <| by
Mathlib/AlgebraicGeometry/ProjectiveSpectrum/Scheme.lean
798
805
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import Mathlib.Algebra.BigOperators.Group.Finset.Indicator import Mathlib.Algebra.Module.BigOperators import Mathlib.LinearAlgebra.AffineSpace.AffineSubspace.Basic import Mathlib.LinearAlgebra.Finsupp.LinearCombination import Mathlib.Tactic.FinCases /-! # Affine combinations of points This file defines affine combinations of points. ## Main definitions * `weightedVSubOfPoint` is a general weighted combination of subtractions with an explicit base point, yielding a vector. * `weightedVSub` uses an arbitrary choice of base point and is intended to be used when the sum of weights is 0, in which case the result is independent of the choice of base point. * `affineCombination` adds the weighted combination to the arbitrary base point, yielding a point rather than a vector, and is intended to be used when the sum of weights is 1, in which case the result is independent of the choice of base point. These definitions are for sums over a `Finset`; versions for a `Fintype` may be obtained using `Finset.univ`, while versions for a `Finsupp` may be obtained using `Finsupp.support`. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable section open Affine namespace Finset theorem univ_fin2 : (univ : Finset (Fin 2)) = {0, 1} := by ext x fin_cases x <;> simp variable {k : Type*} {V : Type*} {P : Type*} [Ring k] [AddCommGroup V] [Module k V] variable [S : AffineSpace V P] variable {ι : Type*} (s : Finset ι) variable {ι₂ : Type*} (s₂ : Finset ι₂) /-- A weighted sum of the results of subtracting a base point from the given points, as a linear map on the weights. The main cases of interest are where the sum of the weights is 0, in which case the sum is independent of the choice of base point, and where the sum of the weights is 1, in which case the sum added to the base point is independent of the choice of base point. -/ def weightedVSubOfPoint (p : ι → P) (b : P) : (ι → k) →ₗ[k] V := ∑ i ∈ s, (LinearMap.proj i : (ι → k) →ₗ[k] k).smulRight (p i -ᵥ b) @[simp] theorem weightedVSubOfPoint_apply (w : ι → k) (p : ι → P) (b : P) : s.weightedVSubOfPoint p b w = ∑ i ∈ s, w i • (p i -ᵥ b) := by simp [weightedVSubOfPoint, LinearMap.sum_apply] /-- The value of `weightedVSubOfPoint`, where the given points are equal. -/ @[simp (high)] theorem weightedVSubOfPoint_apply_const (w : ι → k) (p : P) (b : P) : s.weightedVSubOfPoint (fun _ => p) b w = (∑ i ∈ s, w i) • (p -ᵥ b) := by rw [weightedVSubOfPoint_apply, sum_smul] lemma weightedVSubOfPoint_vadd (s : Finset ι) (w : ι → k) (p : ι → P) (b : P) (v : V) : s.weightedVSubOfPoint (v +ᵥ p) b w = s.weightedVSubOfPoint p (-v +ᵥ b) w := by simp [vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, add_comm] lemma weightedVSubOfPoint_smul {G : Type*} [Group G] [DistribMulAction G V] [SMulCommClass G k V] (s : Finset ι) (w : ι → k) (p : ι → V) (b : V) (a : G) : s.weightedVSubOfPoint (a • p) b w = a • s.weightedVSubOfPoint p (a⁻¹ • b) w := by simp [smul_sum, smul_sub, smul_comm a (w _)] /-- `weightedVSubOfPoint` gives equal results for two families of weights and two families of points that are equal on `s`. -/ theorem weightedVSubOfPoint_congr {w₁ w₂ : ι → k} (hw : ∀ i ∈ s, w₁ i = w₂ i) {p₁ p₂ : ι → P} (hp : ∀ i ∈ s, p₁ i = p₂ i) (b : P) : s.weightedVSubOfPoint p₁ b w₁ = s.weightedVSubOfPoint p₂ b w₂ := by simp_rw [weightedVSubOfPoint_apply] refine sum_congr rfl fun i hi => ?_ rw [hw i hi, hp i hi] /-- Given a family of points, if we use a member of the family as a base point, the `weightedVSubOfPoint` does not depend on the value of the weights at this point. -/ theorem weightedVSubOfPoint_eq_of_weights_eq (p : ι → P) (j : ι) (w₁ w₂ : ι → k) (hw : ∀ i, i ≠ j → w₁ i = w₂ i) : s.weightedVSubOfPoint p (p j) w₁ = s.weightedVSubOfPoint p (p j) w₂ := by simp only [Finset.weightedVSubOfPoint_apply] congr ext i rcases eq_or_ne i j with h | h · simp [h] · simp [hw i h] /-- The weighted sum is independent of the base point when the sum of the weights is 0. -/ theorem weightedVSubOfPoint_eq_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i ∈ s, w i = 0) (b₁ b₂ : P) : s.weightedVSubOfPoint p b₁ w = s.weightedVSubOfPoint p b₂ w := by apply eq_of_sub_eq_zero rw [weightedVSubOfPoint_apply, weightedVSubOfPoint_apply, ← sum_sub_distrib] conv_lhs => congr · skip · ext rw [← smul_sub, vsub_sub_vsub_cancel_left] rw [← sum_smul, h, zero_smul] /-- The weighted sum, added to the base point, is independent of the base point when the sum of the weights is 1. -/ theorem weightedVSubOfPoint_vadd_eq_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i ∈ s, w i = 1) (b₁ b₂ : P) : s.weightedVSubOfPoint p b₁ w +ᵥ b₁ = s.weightedVSubOfPoint p b₂ w +ᵥ b₂ := by rw [weightedVSubOfPoint_apply, weightedVSubOfPoint_apply, ← @vsub_eq_zero_iff_eq V, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, ← add_sub_assoc, add_comm, add_sub_assoc, ← sum_sub_distrib] conv_lhs => congr · skip · congr · skip · ext rw [← smul_sub, vsub_sub_vsub_cancel_left] rw [← sum_smul, h, one_smul, vsub_add_vsub_cancel, vsub_self] /-- The weighted sum is unaffected by removing the base point, if present, from the set of points. -/ @[simp (high)] theorem weightedVSubOfPoint_erase [DecidableEq ι] (w : ι → k) (p : ι → P) (i : ι) : (s.erase i).weightedVSubOfPoint p (p i) w = s.weightedVSubOfPoint p (p i) w := by rw [weightedVSubOfPoint_apply, weightedVSubOfPoint_apply] apply sum_erase rw [vsub_self, smul_zero] /-- The weighted sum is unaffected by adding the base point, whether or not present, to the set of points. -/ @[simp (high)] theorem weightedVSubOfPoint_insert [DecidableEq ι] (w : ι → k) (p : ι → P) (i : ι) : (insert i s).weightedVSubOfPoint p (p i) w = s.weightedVSubOfPoint p (p i) w := by rw [weightedVSubOfPoint_apply, weightedVSubOfPoint_apply] apply sum_insert_zero rw [vsub_self, smul_zero] /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ theorem weightedVSubOfPoint_indicator_subset (w : ι → k) (p : ι → P) (b : P) {s₁ s₂ : Finset ι} (h : s₁ ⊆ s₂) : s₁.weightedVSubOfPoint p b w = s₂.weightedVSubOfPoint p b (Set.indicator (↑s₁) w) := by rw [weightedVSubOfPoint_apply, weightedVSubOfPoint_apply] exact Eq.symm <| sum_indicator_subset_of_eq_zero w (fun i wi => wi • (p i -ᵥ b : V)) h fun i => zero_smul k _ /-- A weighted sum, over the image of an embedding, equals a weighted sum with the same points and weights over the original `Finset`. -/ theorem weightedVSubOfPoint_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) (b : P) : (s₂.map e).weightedVSubOfPoint p b w = s₂.weightedVSubOfPoint (p ∘ e) b (w ∘ e) := by simp_rw [weightedVSubOfPoint_apply] exact Finset.sum_map _ _ _ /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `weightedVSubOfPoint` expressions. -/ theorem sum_smul_vsub_eq_weightedVSubOfPoint_sub (w : ι → k) (p₁ p₂ : ι → P) (b : P) : (∑ i ∈ s, w i • (p₁ i -ᵥ p₂ i)) = s.weightedVSubOfPoint p₁ b w - s.weightedVSubOfPoint p₂ b w := by simp_rw [weightedVSubOfPoint_apply, ← sum_sub_distrib, ← smul_sub, vsub_sub_vsub_cancel_right] /-- A weighted sum of pairwise subtractions, where the point on the right is constant, expressed as a subtraction involving a `weightedVSubOfPoint` expression. -/ theorem sum_smul_vsub_const_eq_weightedVSubOfPoint_sub (w : ι → k) (p₁ : ι → P) (p₂ b : P) : (∑ i ∈ s, w i • (p₁ i -ᵥ p₂)) = s.weightedVSubOfPoint p₁ b w - (∑ i ∈ s, w i) • (p₂ -ᵥ b) := by rw [sum_smul_vsub_eq_weightedVSubOfPoint_sub, weightedVSubOfPoint_apply_const] /-- A weighted sum of pairwise subtractions, where the point on the left is constant, expressed as a subtraction involving a `weightedVSubOfPoint` expression. -/ theorem sum_smul_const_vsub_eq_sub_weightedVSubOfPoint (w : ι → k) (p₂ : ι → P) (p₁ b : P) : (∑ i ∈ s, w i • (p₁ -ᵥ p₂ i)) = (∑ i ∈ s, w i) • (p₁ -ᵥ b) - s.weightedVSubOfPoint p₂ b w := by rw [sum_smul_vsub_eq_weightedVSubOfPoint_sub, weightedVSubOfPoint_apply_const] /-- A weighted sum may be split into such sums over two subsets. -/ theorem weightedVSubOfPoint_sdiff [DecidableEq ι] {s₂ : Finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) (b : P) : (s \ s₂).weightedVSubOfPoint p b w + s₂.weightedVSubOfPoint p b w = s.weightedVSubOfPoint p b w := by simp_rw [weightedVSubOfPoint_apply, sum_sdiff h] /-- A weighted sum may be split into a subtraction of such sums over two subsets. -/ theorem weightedVSubOfPoint_sdiff_sub [DecidableEq ι] {s₂ : Finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) (b : P) : (s \ s₂).weightedVSubOfPoint p b w - s₂.weightedVSubOfPoint p b (-w) = s.weightedVSubOfPoint p b w := by rw [map_neg, sub_neg_eq_add, s.weightedVSubOfPoint_sdiff h] /-- A weighted sum over `s.subtype pred` equals one over `{x ∈ s | pred x}`. -/ theorem weightedVSubOfPoint_subtype_eq_filter (w : ι → k) (p : ι → P) (b : P) (pred : ι → Prop) [DecidablePred pred] : ((s.subtype pred).weightedVSubOfPoint (fun i => p i) b fun i => w i) =
{x ∈ s | pred x}.weightedVSubOfPoint p b w := by rw [weightedVSubOfPoint_apply, weightedVSubOfPoint_apply, ← sum_subtype_eq_sum_filter] /-- A weighted sum over `{x ∈ s | pred x}` equals one over `s` if all the weights at indices in `s` not satisfying `pred` are zero. -/
Mathlib/LinearAlgebra/AffineSpace/Combination.lean
208
212
/- Copyright (c) 2014 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Leonardo de Moura, Mario Carneiro, Floris van Doorn -/ import Mathlib.Algebra.Field.Basic import Mathlib.Algebra.GroupWithZero.Units.Lemmas import Mathlib.Algebra.Order.Ring.Abs import Mathlib.Order.Bounds.Basic import Mathlib.Order.Bounds.OrderIso import Mathlib.Tactic.Positivity.Core /-! # Lemmas about linear ordered (semi)fields -/ open Function OrderDual variable {ι α β : Type*} section LinearOrderedSemifield variable [Semifield α] [LinearOrder α] [IsStrictOrderedRing α] {a b c d e : α} {m n : ℤ} /-! ### Relating two divisions. -/ @[deprecated div_le_div_iff_of_pos_right (since := "2024-11-12")] theorem div_le_div_right (hc : 0 < c) : a / c ≤ b / c ↔ a ≤ b := div_le_div_iff_of_pos_right hc @[deprecated div_lt_div_iff_of_pos_right (since := "2024-11-12")] theorem div_lt_div_right (hc : 0 < c) : a / c < b / c ↔ a < b := div_lt_div_iff_of_pos_right hc @[deprecated div_lt_div_iff_of_pos_left (since := "2024-11-13")] theorem div_lt_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b < a / c ↔ c < b := div_lt_div_iff_of_pos_left ha hb hc @[deprecated div_le_div_iff_of_pos_left (since := "2024-11-12")] theorem div_le_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b ≤ a / c ↔ c ≤ b := div_le_div_iff_of_pos_left ha hb hc @[deprecated div_lt_div_iff₀ (since := "2024-11-12")] theorem div_lt_div_iff (b0 : 0 < b) (d0 : 0 < d) : a / b < c / d ↔ a * d < c * b := div_lt_div_iff₀ b0 d0 @[deprecated div_le_div_iff₀ (since := "2024-11-12")] theorem div_le_div_iff (b0 : 0 < b) (d0 : 0 < d) : a / b ≤ c / d ↔ a * d ≤ c * b := div_le_div_iff₀ b0 d0 @[deprecated div_le_div₀ (since := "2024-11-12")] theorem div_le_div (hc : 0 ≤ c) (hac : a ≤ c) (hd : 0 < d) (hbd : d ≤ b) : a / b ≤ c / d := div_le_div₀ hc hac hd hbd @[deprecated div_lt_div₀ (since := "2024-11-12")] theorem div_lt_div (hac : a < c) (hbd : d ≤ b) (c0 : 0 ≤ c) (d0 : 0 < d) : a / b < c / d := div_lt_div₀ hac hbd c0 d0 @[deprecated div_lt_div₀' (since := "2024-11-12")] theorem div_lt_div' (hac : a ≤ c) (hbd : d < b) (c0 : 0 < c) (d0 : 0 < d) : a / b < c / d := div_lt_div₀' hac hbd c0 d0 /-! ### Relating one division and involving `1` -/ @[bound] theorem div_le_self (ha : 0 ≤ a) (hb : 1 ≤ b) : a / b ≤ a := by simpa only [div_one] using div_le_div_of_nonneg_left ha zero_lt_one hb @[bound] theorem div_lt_self (ha : 0 < a) (hb : 1 < b) : a / b < a := by simpa only [div_one] using div_lt_div_of_pos_left ha zero_lt_one hb @[bound] theorem le_div_self (ha : 0 ≤ a) (hb₀ : 0 < b) (hb₁ : b ≤ 1) : a ≤ a / b := by simpa only [div_one] using div_le_div_of_nonneg_left ha hb₀ hb₁ theorem one_le_div (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a := by rw [le_div_iff₀ hb, one_mul] theorem div_le_one (hb : 0 < b) : a / b ≤ 1 ↔ a ≤ b := by rw [div_le_iff₀ hb, one_mul] theorem one_lt_div (hb : 0 < b) : 1 < a / b ↔ b < a := by rw [lt_div_iff₀ hb, one_mul] theorem div_lt_one (hb : 0 < b) : a / b < 1 ↔ a < b := by rw [div_lt_iff₀ hb, one_mul] theorem one_div_le (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ b ↔ 1 / b ≤ a := by simpa using inv_le_comm₀ ha hb theorem one_div_lt (ha : 0 < a) (hb : 0 < b) : 1 / a < b ↔ 1 / b < a := by simpa using inv_lt_comm₀ ha hb theorem le_one_div (ha : 0 < a) (hb : 0 < b) : a ≤ 1 / b ↔ b ≤ 1 / a := by simpa using le_inv_comm₀ ha hb theorem lt_one_div (ha : 0 < a) (hb : 0 < b) : a < 1 / b ↔ b < 1 / a := by simpa using lt_inv_comm₀ ha hb @[bound] lemma Bound.one_lt_div_of_pos_of_lt (b0 : 0 < b) : b < a → 1 < a / b := (one_lt_div b0).mpr @[bound] lemma Bound.div_lt_one_of_pos_of_lt (b0 : 0 < b) : a < b → a / b < 1 := (div_lt_one b0).mpr /-! ### Relating two divisions, involving `1` -/ theorem one_div_le_one_div_of_le (ha : 0 < a) (h : a ≤ b) : 1 / b ≤ 1 / a := by simpa using inv_anti₀ ha h theorem one_div_lt_one_div_of_lt (ha : 0 < a) (h : a < b) : 1 / b < 1 / a := by rwa [lt_div_iff₀' ha, ← div_eq_mul_one_div, div_lt_one (ha.trans h)] theorem le_of_one_div_le_one_div (ha : 0 < a) (h : 1 / a ≤ 1 / b) : b ≤ a := le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_lt ha) h theorem lt_of_one_div_lt_one_div (ha : 0 < a) (h : 1 / a < 1 / b) : b < a := lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_le ha) h /-- For the single implications with fewer assumptions, see `one_div_le_one_div_of_le` and `le_of_one_div_le_one_div` -/ theorem one_div_le_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ 1 / b ↔ b ≤ a := div_le_div_iff_of_pos_left zero_lt_one ha hb /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and `lt_of_one_div_lt_one_div` -/ theorem one_div_lt_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a < 1 / b ↔ b < a := div_lt_div_iff_of_pos_left zero_lt_one ha hb
theorem one_lt_one_div (h1 : 0 < a) (h2 : a < 1) : 1 < 1 / a := by rwa [lt_one_div (@zero_lt_one α _ _ _ _ _) h1, one_div_one]
Mathlib/Algebra/Order/Field/Basic.lean
131
133
/- Copyright (c) 2024 Jeremy Tan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Tan -/ import Mathlib.Combinatorics.SimpleGraph.Clique import Mathlib.Order.Partition.Equipartition /-! # Turán's theorem In this file we prove Turán's theorem, the first important result of extremal graph theory, which states that the `r + 1`-cliquefree graph on `n` vertices with the most edges is the complete `r`-partite graph with part sizes as equal as possible (`turanGraph n r`). The forward direction of the proof performs "Zykov symmetrisation", which first shows constructively that non-adjacency is an equivalence relation in a maximal graph, so it must be complete multipartite with the parts being the equivalence classes. Then basic manipulations show that the graph is isomorphic to the Turán graph for the given parameters. For the reverse direction we first show that a Turán-maximal graph exists, then transfer the property through `turanGraph n r` using the isomorphism provided by the forward direction. ## Main declarations * `SimpleGraph.IsTuranMaximal`: `G.IsTuranMaximal r` means that `G` has the most number of edges for its number of vertices while still being `r + 1`-cliquefree. * `SimpleGraph.turanGraph n r`: The canonical `r + 1`-cliquefree Turán graph on `n` vertices. * `SimpleGraph.IsTuranMaximal.finpartition`: The result of Zykov symmetrisation, a finpartition of the vertices such that two vertices are in the same part iff they are non-adjacent. * `SimpleGraph.IsTuranMaximal.nonempty_iso_turanGraph`: The forward direction, an isomorphism between `G` satisfying `G.IsTuranMaximal r` and `turanGraph n r`. * `isTuranMaximal_of_iso`: the reverse direction, `G.IsTuranMaximal r` given the isomorphism. * `isTuranMaximal_iff_nonempty_iso_turanGraph`: Turán's theorem in full. ## References * https://en.wikipedia.org/wiki/Turán%27s_theorem -/ open Finset namespace SimpleGraph variable {V : Type*} [Fintype V] {G : SimpleGraph V} [DecidableRel G.Adj] {n r : ℕ} variable (G) in /-- An `r + 1`-cliquefree graph is `r`-Turán-maximal if any other `r + 1`-cliquefree graph on the same vertex set has the same or fewer number of edges. -/ def IsTuranMaximal (r : ℕ) : Prop := G.CliqueFree (r + 1) ∧ ∀ (H : SimpleGraph V) [DecidableRel H.Adj], H.CliqueFree (r + 1) → #H.edgeFinset ≤ #G.edgeFinset section Defs variable {H : SimpleGraph V}
lemma IsTuranMaximal.le_iff_eq (hG : G.IsTuranMaximal r) (hH : H.CliqueFree (r + 1)) : G ≤ H ↔ G = H := by classical exact ⟨fun hGH ↦ edgeFinset_inj.1 <| eq_of_subset_of_card_le (edgeFinset_subset_edgeFinset.2 hGH) (hG.2 _ hH), le_of_eq⟩
Mathlib/Combinatorics/SimpleGraph/Turan.lean
58
61
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Jeremy Avigad, Simon Hudon -/ import Mathlib.Algebra.Notation.Defs import Mathlib.Data.Set.Subsingleton import Mathlib.Logic.Equiv.Defs /-! # Partial values of a type This file defines `Part α`, the partial values of a type. `o : Part α` carries a proposition `o.Dom`, its domain, along with a function `get : o.Dom → α`, its value. The rule is then that every partial value has a value but, to access it, you need to provide a proof of the domain. `Part α` behaves the same as `Option α` except that `o : Option α` is decidably `none` or `some a` for some `a : α`, while the domain of `o : Part α` doesn't have to be decidable. That means you can translate back and forth between a partial value with a decidable domain and an option, and `Option α` and `Part α` are classically equivalent. In general, `Part α` is bigger than `Option α`. In current mathlib, `Part ℕ`, aka `PartENat`, is used to move decidability of the order to decidability of `PartENat.find` (which is the smallest natural satisfying a predicate, or `∞` if there's none). ## Main declarations `Option`-like declarations: * `Part.none`: The partial value whose domain is `False`. * `Part.some a`: The partial value whose domain is `True` and whose value is `a`. * `Part.ofOption`: Converts an `Option α` to a `Part α` by sending `none` to `none` and `some a` to `some a`. * `Part.toOption`: Converts a `Part α` with a decidable domain to an `Option α`. * `Part.equivOption`: Classical equivalence between `Part α` and `Option α`. Monadic structure: * `Part.bind`: `o.bind f` has value `(f (o.get _)).get _` (`f o` morally) and is defined when `o` and `f (o.get _)` are defined. * `Part.map`: Maps the value and keeps the same domain. Other: * `Part.restrict`: `Part.restrict p o` replaces the domain of `o : Part α` by `p : Prop` so long as `p → o.Dom`. * `Part.assert`: `assert p f` appends `p` to the domains of the values of a partial function. * `Part.unwrap`: Gets the value of a partial value regardless of its domain. Unsound. ## Notation For `a : α`, `o : Part α`, `a ∈ o` means that `o` is defined and equal to `a`. Formally, it means `o.Dom` and `o.get _ = a`. -/ assert_not_exists RelIso open Function /-- `Part α` is the type of "partial values" of type `α`. It is similar to `Option α` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure Part.{u} (α : Type u) : Type u where /-- The domain of a partial value -/ Dom : Prop /-- Extract a value from a partial value given a proof of `Dom` -/ get : Dom → α namespace Part variable {α : Type*} {β : Type*} {γ : Type*} /-- Convert a `Part α` with a decidable domain to an option -/ def toOption (o : Part α) [Decidable o.Dom] : Option α := if h : Dom o then some (o.get h) else none @[simp] lemma toOption_isSome (o : Part α) [Decidable o.Dom] : o.toOption.isSome ↔ o.Dom := by by_cases h : o.Dom <;> simp [h, toOption] @[simp] lemma toOption_eq_none (o : Part α) [Decidable o.Dom] : o.toOption = none ↔ ¬o.Dom := by by_cases h : o.Dom <;> simp [h, toOption] /-- `Part` extensionality -/ theorem ext' : ∀ {o p : Part α}, (o.Dom ↔ p.Dom) → (∀ h₁ h₂, o.get h₁ = p.get h₂) → o = p | ⟨od, o⟩, ⟨pd, p⟩, H1, H2 => by have t : od = pd := propext H1 cases t; rw [show o = p from funext fun p => H2 p p] /-- `Part` eta expansion -/ @[simp] theorem eta : ∀ o : Part α, (⟨o.Dom, fun h => o.get h⟩ : Part α) = o | ⟨_, _⟩ => rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def Mem (o : Part α) (a : α) : Prop := ∃ h, o.get h = a instance : Membership α (Part α) := ⟨Part.Mem⟩ theorem mem_eq (a : α) (o : Part α) : (a ∈ o) = ∃ h, o.get h = a := rfl theorem dom_iff_mem : ∀ {o : Part α}, o.Dom ↔ ∃ y, y ∈ o | ⟨_, f⟩ => ⟨fun h => ⟨f h, h, rfl⟩, fun ⟨_, h, rfl⟩ => h⟩ theorem get_mem {o : Part α} (h) : get o h ∈ o := ⟨_, rfl⟩ @[simp] theorem mem_mk_iff {p : Prop} {o : p → α} {a : α} : a ∈ Part.mk p o ↔ ∃ h, o h = a := Iff.rfl /-- `Part` extensionality -/ @[ext] theorem ext {o p : Part α} (H : ∀ a, a ∈ o ↔ a ∈ p) : o = p := (ext' ⟨fun h => ((H _).1 ⟨h, rfl⟩).fst, fun h => ((H _).2 ⟨h, rfl⟩).fst⟩) fun _ _ => ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `Part` has a `False` domain and an empty function. -/ def none : Part α := ⟨False, False.rec⟩ instance : Inhabited (Part α) := ⟨none⟩ @[simp] theorem not_mem_none (a : α) : a ∉ @none α := fun h => h.fst /-- The `some a` value in `Part` has a `True` domain and the function returns `a`. -/ def some (a : α) : Part α := ⟨True, fun _ => a⟩ @[simp] theorem some_dom (a : α) : (some a).Dom := trivial theorem mem_unique : ∀ {a b : α} {o : Part α}, a ∈ o → b ∈ o → a = b | _, _, ⟨_, _⟩, ⟨_, rfl⟩, ⟨_, rfl⟩ => rfl theorem mem_right_unique : ∀ {a : α} {o p : Part α}, a ∈ o → a ∈ p → o = p | _, _, _, ⟨ho, _⟩, ⟨hp, _⟩ => ext' (iff_of_true ho hp) (by simp [*]) theorem Mem.left_unique : Relator.LeftUnique ((· ∈ ·) : α → Part α → Prop) := fun _ _ _ => mem_unique theorem Mem.right_unique : Relator.RightUnique ((· ∈ ·) : α → Part α → Prop) := fun _ _ _ => mem_right_unique theorem get_eq_of_mem {o : Part α} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h protected theorem subsingleton (o : Part α) : Set.Subsingleton { a | a ∈ o } := fun _ ha _ hb => mem_unique ha hb @[simp] theorem get_some {a : α} (ha : (some a).Dom) : get (some a) ha = a := rfl theorem mem_some (a : α) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : Part α) ↔ b = a := ⟨fun ⟨_, e⟩ => e.symm, fun e => ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : α} {o : Part α} : o = some a ↔ a ∈ o := ⟨fun e => e.symm ▸ mem_some _, fun ⟨h, e⟩ => e ▸ ext' (iff_true_intro h) fun _ _ => rfl⟩ theorem eq_none_iff {o : Part α} : o = none ↔ ∀ a, a ∉ o := ⟨fun e => e.symm ▸ not_mem_none, fun h => ext (by simpa)⟩ theorem eq_none_iff' {o : Part α} : o = none ↔ ¬o.Dom := ⟨fun e => e.symm ▸ id, fun h => eq_none_iff.2 fun _ h' => h h'.fst⟩ @[simp] theorem not_none_dom : ¬(none : Part α).Dom := id @[simp] theorem some_ne_none (x : α) : some x ≠ none := by intro h exact true_ne_false (congr_arg Dom h) @[simp] theorem none_ne_some (x : α) : none ≠ some x := (some_ne_none x).symm theorem ne_none_iff {o : Part α} : o ≠ none ↔ ∃ x, o = some x := by constructor · rw [Ne, eq_none_iff', not_not] exact fun h => ⟨o.get h, eq_some_iff.2 (get_mem h)⟩ · rintro ⟨x, rfl⟩ apply some_ne_none theorem eq_none_or_eq_some (o : Part α) : o = none ∨ ∃ x, o = some x := or_iff_not_imp_left.2 ne_none_iff.1 theorem some_injective : Injective (@Part.some α) := fun _ _ h => congr_fun (eq_of_heq (Part.mk.inj h).2) trivial @[simp] theorem some_inj {a b : α} : Part.some a = some b ↔ a = b := some_injective.eq_iff @[simp] theorem some_get {a : Part α} (ha : a.Dom) : Part.some (Part.get a ha) = a := Eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) theorem get_eq_iff_eq_some {a : Part α} {ha : a.Dom} {b : α} : a.get ha = b ↔ a = some b := ⟨fun h => by simp [h.symm], fun h => by simp [h]⟩ theorem get_eq_get_of_eq (a : Part α) (ha : a.Dom) {b : Part α} (h : a = b) : a.get ha = b.get (h ▸ ha) := by congr theorem get_eq_iff_mem {o : Part α} {a : α} (h : o.Dom) : o.get h = a ↔ a ∈ o := ⟨fun H => ⟨h, H⟩, fun ⟨_, H⟩ => H⟩ theorem eq_get_iff_mem {o : Part α} {a : α} (h : o.Dom) : a = o.get h ↔ a ∈ o := eq_comm.trans (get_eq_iff_mem h) @[simp] theorem none_toOption [Decidable (@none α).Dom] : (none : Part α).toOption = Option.none := dif_neg id @[simp] theorem some_toOption (a : α) [Decidable (some a).Dom] : (some a).toOption = Option.some a := dif_pos trivial instance noneDecidable : Decidable (@none α).Dom := instDecidableFalse instance someDecidable (a : α) : Decidable (some a).Dom := instDecidableTrue /-- Retrieves the value of `a : Part α` if it exists, and return the provided default value otherwise. -/ def getOrElse (a : Part α) [Decidable a.Dom] (d : α) := if ha : a.Dom then a.get ha else d theorem getOrElse_of_dom (a : Part α) (h : a.Dom) [Decidable a.Dom] (d : α) : getOrElse a d = a.get h := dif_pos h theorem getOrElse_of_not_dom (a : Part α) (h : ¬a.Dom) [Decidable a.Dom] (d : α) : getOrElse a d = d := dif_neg h @[simp] theorem getOrElse_none (d : α) [Decidable (none : Part α).Dom] : getOrElse none d = d := none.getOrElse_of_not_dom not_none_dom d @[simp] theorem getOrElse_some (a : α) (d : α) [Decidable (some a).Dom] : getOrElse (some a) d = a := (some a).getOrElse_of_dom (some_dom a) d -- `simp`-normal form is `toOption_eq_some_iff`. theorem mem_toOption {o : Part α} [Decidable o.Dom] {a : α} : a ∈ toOption o ↔ a ∈ o := by unfold toOption by_cases h : o.Dom <;> simp [h] · exact ⟨fun h => ⟨_, h⟩, fun ⟨_, h⟩ => h⟩ · exact mt Exists.fst h @[simp] theorem toOption_eq_some_iff {o : Part α} [Decidable o.Dom] {a : α} : toOption o = Option.some a ↔ a ∈ o := by rw [← Option.mem_def, mem_toOption] protected theorem Dom.toOption {o : Part α} [Decidable o.Dom] (h : o.Dom) : o.toOption = o.get h := dif_pos h theorem toOption_eq_none_iff {a : Part α} [Decidable a.Dom] : a.toOption = Option.none ↔ ¬a.Dom := Ne.dite_eq_right_iff fun _ => Option.some_ne_none _ @[simp] theorem elim_toOption {α β : Type*} (a : Part α) [Decidable a.Dom] (b : β) (f : α → β) : a.toOption.elim b f = if h : a.Dom then f (a.get h) else b := by split_ifs with h · rw [h.toOption] rfl · rw [Part.toOption_eq_none_iff.2 h] rfl /-- Converts an `Option α` into a `Part α`. -/ @[coe] def ofOption : Option α → Part α | Option.none => none | Option.some a => some a @[simp] theorem mem_ofOption {a : α} : ∀ {o : Option α}, a ∈ ofOption o ↔ a ∈ o | Option.none => ⟨fun h => h.fst.elim, fun h => Option.noConfusion h⟩ | Option.some _ => ⟨fun h => congr_arg Option.some h.snd, fun h => ⟨trivial, Option.some.inj h⟩⟩ @[simp] theorem ofOption_dom {α} : ∀ o : Option α, (ofOption o).Dom ↔ o.isSome | Option.none => by simp [ofOption, none] | Option.some a => by simp [ofOption] theorem ofOption_eq_get {α} (o : Option α) : ofOption o = ⟨_, @Option.get _ o⟩ := Part.ext' (ofOption_dom o) fun h₁ h₂ => by cases o · simp at h₂ · rfl instance : Coe (Option α) (Part α) := ⟨ofOption⟩ theorem mem_coe {a : α} {o : Option α} : a ∈ (o : Part α) ↔ a ∈ o := mem_ofOption @[simp] theorem coe_none : (@Option.none α : Part α) = none := rfl @[simp] theorem coe_some (a : α) : (Option.some a : Part α) = some a := rfl @[elab_as_elim] protected theorem induction_on {P : Part α → Prop} (a : Part α) (hnone : P none) (hsome : ∀ a : α, P (some a)) : P a := (Classical.em a.Dom).elim (fun h => Part.some_get h ▸ hsome _) fun h => (eq_none_iff'.2 h).symm ▸ hnone instance ofOptionDecidable : ∀ o : Option α, Decidable (ofOption o).Dom | Option.none => Part.noneDecidable | Option.some a => Part.someDecidable a @[simp] theorem to_ofOption (o : Option α) : toOption (ofOption o) = o := by cases o <;> rfl @[simp] theorem of_toOption (o : Part α) [Decidable o.Dom] : ofOption (toOption o) = o := ext fun _ => mem_ofOption.trans mem_toOption /-- `Part α` is (classically) equivalent to `Option α`. -/ noncomputable def equivOption : Part α ≃ Option α := haveI := Classical.dec ⟨fun o => toOption o, ofOption, fun o => of_toOption o, fun o => Eq.trans (by dsimp; congr) (to_ofOption o)⟩ /-- We give `Part α` the order where everything is greater than `none`. -/ instance : PartialOrder (Part α) where le x y := ∀ i, i ∈ x → i ∈ y le_refl _ _ := id le_trans _ _ _ f g _ := g _ ∘ f _ le_antisymm _ _ f g := Part.ext fun _ => ⟨f _, g _⟩ instance : OrderBot (Part α) where bot := none bot_le := by rintro x _ ⟨⟨_⟩, _⟩ theorem le_total_of_le_of_le {x y : Part α} (z : Part α) (hx : x ≤ z) (hy : y ≤ z) : x ≤ y ∨ y ≤ x := by rcases Part.eq_none_or_eq_some x with (h | ⟨b, h₀⟩) · rw [h] left apply OrderBot.bot_le _ right; intro b' h₁ rw [Part.eq_some_iff] at h₀ have hx := hx _ h₀; have hy := hy _ h₁ have hx := Part.mem_unique hx hy; subst hx exact h₀ /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p → Part α) : Part α := ⟨∃ h : p, (f h).Dom, fun ha => (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : Part α) (g : α → Part β) : Part β := assert (Dom f) fun b => g (f.get b) /-- The map operation for `Part` just maps the value and maintains the same domain. -/ @[simps] def map (f : α → β) (o : Part α) : Part β := ⟨o.Dom, f ∘ o.get⟩ theorem mem_map (f : α → β) {o : Part α} : ∀ {a}, a ∈ o → f a ∈ map f o | _, ⟨_, rfl⟩ => ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : α → β) {o : Part α} {b} : b ∈ map f o ↔ ∃ a ∈ o, f a = b := ⟨fun hb => match b, hb with | _, ⟨_, rfl⟩ => ⟨_, ⟨_, rfl⟩, rfl⟩, fun ⟨_, h₁, h₂⟩ => h₂ ▸ mem_map f h₁⟩ @[simp] theorem map_none (f : α → β) : map f none = none := eq_none_iff.2 fun a => by simp @[simp] theorem map_some (f : α → β) (a : α) : map f (some a) = some (f a) := eq_some_iff.2 <| mem_map f <| mem_some _ theorem mem_assert {p : Prop} {f : p → Part α} : ∀ {a} (h : p), a ∈ f h → a ∈ assert p f | _, x, ⟨h, rfl⟩ => ⟨⟨x, h⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p → Part α} {a} : a ∈ assert p f ↔ ∃ h : p, a ∈ f h := ⟨fun ha => match a, ha with | _, ⟨_, rfl⟩ => ⟨_, ⟨_, rfl⟩⟩, fun ⟨_, h⟩ => mem_assert _ h⟩ theorem assert_pos {p : Prop} {f : p → Part α} (h : p) : assert p f = f h := by dsimp [assert] cases h' : f h simp only [h', mk.injEq, h, exists_prop_of_true, true_and] apply Function.hfunext · simp only [h, h', exists_prop_of_true] · simp theorem assert_neg {p : Prop} {f : p → Part α} (h : ¬p) : assert p f = none := by dsimp [assert, none]; congr · simp only [h, not_false_iff, exists_prop_of_false] · apply Function.hfunext · simp only [h, not_false_iff, exists_prop_of_false] simp at * theorem mem_bind {f : Part α} {g : α → Part β} : ∀ {a b}, a ∈ f → b ∈ g a → b ∈ f.bind g | _, _, ⟨h, rfl⟩, ⟨h₂, rfl⟩ => ⟨⟨h, h₂⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : Part α} {g : α → Part β} {b} : b ∈ f.bind g ↔ ∃ a ∈ f, b ∈ g a := ⟨fun hb => match b, hb with | _, ⟨⟨_, _⟩, rfl⟩ => ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩, fun ⟨_, h₁, h₂⟩ => mem_bind h₁ h₂⟩ protected theorem Dom.bind {o : Part α} (h : o.Dom) (f : α → Part β) : o.bind f = f (o.get h) := by ext b simp only [Part.mem_bind_iff, exists_prop] refine ⟨?_, fun hb => ⟨o.get h, Part.get_mem _, hb⟩⟩ rintro ⟨a, ha, hb⟩ rwa [Part.get_eq_of_mem ha] theorem Dom.of_bind {f : α → Part β} {a : Part α} (h : (a.bind f).Dom) : a.Dom := h.1 @[simp] theorem bind_none (f : α → Part β) : none.bind f = none := eq_none_iff.2 fun a => by simp @[simp] theorem bind_some (a : α) (f : α → Part β) : (some a).bind f = f a := ext <| by simp theorem bind_of_mem {o : Part α} {a : α} (h : a ∈ o) (f : α → Part β) : o.bind f = f a := by rw [eq_some_iff.2 h, bind_some] theorem bind_some_eq_map (f : α → β) (x : Part α) : x.bind (fun y => some (f y)) = map f x := ext <| by simp [eq_comm] theorem bind_toOption (f : α → Part β) (o : Part α) [Decidable o.Dom] [∀ a, Decidable (f a).Dom] [Decidable (o.bind f).Dom] : (o.bind f).toOption = o.toOption.elim Option.none fun a => (f a).toOption := by by_cases h : o.Dom · simp_rw [h.toOption, h.bind] rfl · rw [Part.toOption_eq_none_iff.2 h] exact Part.toOption_eq_none_iff.2 fun ho => h ho.of_bind theorem bind_assoc {γ} (f : Part α) (g : α → Part β) (k : β → Part γ) : (f.bind g).bind k = f.bind fun x => (g x).bind k := ext fun a => by simp only [mem_bind_iff] exact ⟨fun ⟨_, ⟨_, h₁, h₂⟩, h₃⟩ => ⟨_, h₁, _, h₂, h₃⟩, fun ⟨_, h₁, _, h₂, h₃⟩ => ⟨_, ⟨_, h₁, h₂⟩, h₃⟩⟩ @[simp] theorem bind_map {γ} (f : α → β) (x) (g : β → Part γ) : (map f x).bind g = x.bind fun y => g (f y) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {γ} (f : α → Part β) (x : Part α) (g : β → γ) : map g (x.bind f) = x.bind fun y => map g (f y) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : β → γ) (f : α → β) (o : Part α) : map g (map f o) = map (g ∘ f) o := by simp [map, Function.comp_assoc] instance : Monad Part where pure := @some map := @map bind := @Part.bind instance : LawfulMonad Part where bind_pure_comp := @bind_some_eq_map id_map f := by cases f; rfl pure_bind := @bind_some bind_assoc := @bind_assoc map_const := by simp [Functor.mapConst, Functor.map] --Porting TODO : In Lean3 these were automatic by a tactic seqLeft_eq x y := ext' (by simp [SeqLeft.seqLeft, Part.bind, assert, Seq.seq, const, (· <$> ·), and_comm]) (fun _ _ => rfl) seqRight_eq x y := ext' (by simp [SeqRight.seqRight, Part.bind, assert, Seq.seq, const, (· <$> ·), and_comm]) (fun _ _ => rfl) pure_seq x y := ext' (by simp [Seq.seq, Part.bind, assert, (· <$> ·), pure]) (fun _ _ => rfl) bind_map x y := ext' (by simp [(· >>= ·), Part.bind, assert, Seq.seq, get, (· <$> ·)] ) (fun _ _ => rfl) theorem map_id' {f : α → α} (H : ∀ x : α, f x = x) (o) : map f o = o := by rw [show f = id from funext H]; exact id_map o @[simp] theorem bind_some_right (x : Part α) : x.bind some = x := by rw [bind_some_eq_map] simp [map_id'] @[simp] theorem pure_eq_some (a : α) : pure a = some a := rfl @[simp] theorem ret_eq_some (a : α) : (return a : Part α) = some a := rfl @[simp] theorem map_eq_map {α β} (f : α → β) (o : Part α) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {α β} (f : Part α) (g : α → Part β) : f >>= g = f.bind g := rfl theorem bind_le {α} (x : Part α) (f : α → Part β) (y : Part β) : x >>= f ≤ y ↔ ∀ a, a ∈ x → f a ≤ y := by constructor <;> intro h · intro a h' b have h := h b simp only [and_imp, exists_prop, bind_eq_bind, mem_bind_iff, exists_imp] at h apply h _ h' · intro b h' simp only [exists_prop, bind_eq_bind, mem_bind_iff] at h' rcases h' with ⟨a, h₀, h₁⟩ apply h _ h₀ _ h₁ -- TODO: if `MonadFail` is defined, define the below instance. -- instance : MonadFail Part := -- { Part.monad with fail := fun _ _ => none } /-- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) (o : Part α) (H : p → o.Dom) : Part α := ⟨p, fun h => o.get (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : Part α) (h : p → o.Dom) (a : α) : a ∈ restrict p o h ↔ p ∧ a ∈ o := by dsimp [restrict, mem_eq]; constructor · rintro ⟨h₀, h₁⟩ exact ⟨h₀, ⟨_, h₁⟩⟩ rintro ⟨h₀, _, h₂⟩; exact ⟨h₀, h₂⟩ /-- `unwrap o` gets the value at `o`, ignoring the condition. This function is unsound. -/ unsafe def unwrap (o : Part α) : α := o.get lcProof theorem assert_defined {p : Prop} {f : p → Part α} : ∀ h : p, (f h).Dom → (assert p f).Dom := Exists.intro theorem bind_defined {f : Part α} {g : α → Part β} : ∀ h : f.Dom, (g (f.get h)).Dom → (f.bind g).Dom := assert_defined @[simp] theorem bind_dom {f : Part α} {g : α → Part β} : (f.bind g).Dom ↔ ∃ h : f.Dom, (g (f.get h)).Dom := Iff.rfl section Instances /-! We define several instances for constants and operations on `Part α` inherited from `α`. This section could be moved to a separate file to avoid the import of `Mathlib.Algebra.Group.Defs`. -/ @[to_additive] instance [One α] : One (Part α) where one := pure 1 @[to_additive] instance [Mul α] : Mul (Part α) where mul a b := (· * ·) <$> a <*> b @[to_additive] instance [Inv α] : Inv (Part α) where inv := map Inv.inv @[to_additive] instance [Div α] : Div (Part α) where div a b := (· / ·) <$> a <*> b instance [Mod α] : Mod (Part α) where mod a b := (· % ·) <$> a <*> b instance [Append α] : Append (Part α) where append a b := (· ++ ·) <$> a <*> b instance [Inter α] : Inter (Part α) where inter a b := (· ∩ ·) <$> a <*> b instance [Union α] : Union (Part α) where union a b := (· ∪ ·) <$> a <*> b instance [SDiff α] : SDiff (Part α) where sdiff a b := (· \ ·) <$> a <*> b section @[to_additive] theorem mul_def [Mul α] (a b : Part α) : a * b = bind a fun y ↦ map (y * ·) b := rfl @[to_additive] theorem one_def [One α] : (1 : Part α) = some 1 := rfl @[to_additive] theorem inv_def [Inv α] (a : Part α) : a⁻¹ = Part.map (· ⁻¹) a := rfl @[to_additive] theorem div_def [Div α] (a b : Part α) : a / b = bind a fun y => map (y / ·) b := rfl theorem mod_def [Mod α] (a b : Part α) : a % b = bind a fun y => map (y % ·) b := rfl theorem append_def [Append α] (a b : Part α) : a ++ b = bind a fun y => map (y ++ ·) b := rfl theorem inter_def [Inter α] (a b : Part α) : a ∩ b = bind a fun y => map (y ∩ ·) b := rfl theorem union_def [Union α] (a b : Part α) : a ∪ b = bind a fun y => map (y ∪ ·) b := rfl theorem sdiff_def [SDiff α] (a b : Part α) : a \ b = bind a fun y => map (y \ ·) b := rfl end @[to_additive] theorem one_mem_one [One α] : (1 : α) ∈ (1 : Part α) := ⟨trivial, rfl⟩ @[to_additive] theorem mul_mem_mul [Mul α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma * mb ∈ a * b := ⟨⟨ha.1, hb.1⟩, by simp only [← ha.2, ← hb.2]; rfl⟩ @[to_additive] theorem left_dom_of_mul_dom [Mul α] {a b : Part α} (hab : Dom (a * b)) : a.Dom := hab.1 @[to_additive] theorem right_dom_of_mul_dom [Mul α] {a b : Part α} (hab : Dom (a * b)) : b.Dom := hab.2 @[to_additive (attr := simp)] theorem mul_get_eq [Mul α] (a b : Part α) (hab : Dom (a * b)) : (a * b).get hab = a.get (left_dom_of_mul_dom hab) * b.get (right_dom_of_mul_dom hab) := rfl @[to_additive] theorem some_mul_some [Mul α] (a b : α) : some a * some b = some (a * b) := by simp [mul_def] @[to_additive] theorem inv_mem_inv [Inv α] (a : Part α) (ma : α) (ha : ma ∈ a) : ma⁻¹ ∈ a⁻¹ := by simp [inv_def]; aesop @[to_additive] theorem inv_some [Inv α] (a : α) : (some a)⁻¹ = some a⁻¹ := rfl @[to_additive] theorem div_mem_div [Div α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma / mb ∈ a / b := by simp [div_def]; aesop @[to_additive] theorem left_dom_of_div_dom [Div α] {a b : Part α} (hab : Dom (a / b)) : a.Dom := hab.1 @[to_additive] theorem right_dom_of_div_dom [Div α] {a b : Part α} (hab : Dom (a / b)) : b.Dom := hab.2 @[to_additive (attr := simp)] theorem div_get_eq [Div α] (a b : Part α) (hab : Dom (a / b)) : (a / b).get hab = a.get (left_dom_of_div_dom hab) / b.get (right_dom_of_div_dom hab) := by simp [div_def]; aesop @[to_additive] theorem some_div_some [Div α] (a b : α) : some a / some b = some (a / b) := by simp [div_def] theorem mod_mem_mod [Mod α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma % mb ∈ a % b := by simp [mod_def]; aesop theorem left_dom_of_mod_dom [Mod α] {a b : Part α} (hab : Dom (a % b)) : a.Dom := hab.1 theorem right_dom_of_mod_dom [Mod α] {a b : Part α} (hab : Dom (a % b)) : b.Dom := hab.2 @[simp] theorem mod_get_eq [Mod α] (a b : Part α) (hab : Dom (a % b)) : (a % b).get hab = a.get (left_dom_of_mod_dom hab) % b.get (right_dom_of_mod_dom hab) := by simp [mod_def]; aesop theorem some_mod_some [Mod α] (a b : α) : some a % some b = some (a % b) := by simp [mod_def] theorem append_mem_append [Append α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ++ mb ∈ a ++ b := by simp [append_def]; aesop theorem left_dom_of_append_dom [Append α] {a b : Part α} (hab : Dom (a ++ b)) : a.Dom := hab.1 theorem right_dom_of_append_dom [Append α] {a b : Part α} (hab : Dom (a ++ b)) : b.Dom := hab.2 @[simp] theorem append_get_eq [Append α] (a b : Part α) (hab : Dom (a ++ b)) : (a ++ b).get hab = a.get (left_dom_of_append_dom hab) ++ b.get (right_dom_of_append_dom hab) := by simp [append_def]; aesop theorem some_append_some [Append α] (a b : α) : some a ++ some b = some (a ++ b) := by simp [append_def] theorem inter_mem_inter [Inter α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ∩ mb ∈ a ∩ b := by simp [inter_def]; aesop theorem left_dom_of_inter_dom [Inter α] {a b : Part α} (hab : Dom (a ∩ b)) : a.Dom := hab.1 theorem right_dom_of_inter_dom [Inter α] {a b : Part α} (hab : Dom (a ∩ b)) : b.Dom := hab.2 @[simp] theorem inter_get_eq [Inter α] (a b : Part α) (hab : Dom (a ∩ b)) : (a ∩ b).get hab = a.get (left_dom_of_inter_dom hab) ∩ b.get (right_dom_of_inter_dom hab) := by simp [inter_def]; aesop theorem some_inter_some [Inter α] (a b : α) : some a ∩ some b = some (a ∩ b) := by simp [inter_def] theorem union_mem_union [Union α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ∪ mb ∈ a ∪ b := by simp [union_def]; aesop theorem left_dom_of_union_dom [Union α] {a b : Part α} (hab : Dom (a ∪ b)) : a.Dom := hab.1 theorem right_dom_of_union_dom [Union α] {a b : Part α} (hab : Dom (a ∪ b)) : b.Dom := hab.2 @[simp] theorem union_get_eq [Union α] (a b : Part α) (hab : Dom (a ∪ b)) : (a ∪ b).get hab = a.get (left_dom_of_union_dom hab) ∪ b.get (right_dom_of_union_dom hab) := by simp [union_def]; aesop theorem some_union_some [Union α] (a b : α) : some a ∪ some b = some (a ∪ b) := by simp [union_def] theorem sdiff_mem_sdiff [SDiff α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma \ mb ∈ a \ b := by simp [sdiff_def]; aesop theorem left_dom_of_sdiff_dom [SDiff α] {a b : Part α} (hab : Dom (a \ b)) : a.Dom := hab.1 theorem right_dom_of_sdiff_dom [SDiff α] {a b : Part α} (hab : Dom (a \ b)) : b.Dom := hab.2 @[simp] theorem sdiff_get_eq [SDiff α] (a b : Part α) (hab : Dom (a \ b)) : (a \ b).get hab = a.get (left_dom_of_sdiff_dom hab) \ b.get (right_dom_of_sdiff_dom hab) := by simp [sdiff_def]; aesop theorem some_sdiff_some [SDiff α] (a b : α) : some a \ some b = some (a \ b) := by simp [sdiff_def] end Instances end Part
Mathlib/Data/Part.lean
824
825
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Order.ConditionallyCompleteLattice.Group import Mathlib.Topology.MetricSpace.Isometry /-! # Metric space gluing Gluing two metric spaces along a common subset. Formally, we are given ``` Φ Z ---> X | |Ψ v Y ``` where `hΦ : Isometry Φ` and `hΨ : Isometry Ψ`. We want to complete the square by a space `GlueSpacescan hΦ hΨ` and two isometries `toGlueL hΦ hΨ` and `toGlueR hΦ hΨ` that make the square commute. We start by defining a predistance on the disjoint union `X ⊕ Y`, for which points `Φ p` and `Ψ p` are at distance 0. The (quotient) metric space associated to this predistance is the desired space. This is an instance of a more general construction, where `Φ` and `Ψ` do not have to be isometries, but the distances in the image almost coincide, up to `2ε` say. Then one can almost glue the two spaces so that the images of a point under `Φ` and `Ψ` are `ε`-close. If `ε > 0`, this yields a metric space structure on `X ⊕ Y`, without the need to take a quotient. In particular, this gives a natural metric space structure on `X ⊕ Y`, where the basepoints are at distance 1, say, and the distances between other points are obtained by going through the two basepoints. (We also register the same metric space structure on a general disjoint union `Σ i, E i`). We also define the inductive limit of metric spaces. Given ``` f 0 f 1 f 2 f 3 X 0 -----> X 1 -----> X 2 -----> X 3 -----> ... ``` where the `X n` are metric spaces and `f n` isometric embeddings, we define the inductive limit of the `X n`, also known as the increasing union of the `X n` in this context, if we identify `X n` and `X (n+1)` through `f n`. This is a metric space in which all `X n` embed isometrically and in a way compatible with `f n`. -/ noncomputable section universe u v w open Function Set Uniformity Topology namespace Metric section ApproxGluing variable {X : Type u} {Y : Type v} {Z : Type w} variable [MetricSpace X] [MetricSpace Y] {Φ : Z → X} {Ψ : Z → Y} {ε : ℝ} /-- Define a predistance on `X ⊕ Y`, for which `Φ p` and `Ψ p` are at distance `ε` -/ def glueDist (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) : X ⊕ Y → X ⊕ Y → ℝ | .inl x, .inl y => dist x y | .inr x, .inr y => dist x y | .inl x, .inr y => (⨅ p, dist x (Φ p) + dist y (Ψ p)) + ε | .inr x, .inl y => (⨅ p, dist y (Φ p) + dist x (Ψ p)) + ε private theorem glueDist_self (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) : ∀ x, glueDist Φ Ψ ε x x = 0 | .inl _ => dist_self _ | .inr _ => dist_self _ theorem glueDist_glued_points [Nonempty Z] (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (p : Z) : glueDist Φ Ψ ε (.inl (Φ p)) (.inr (Ψ p)) = ε := by have : ⨅ q, dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q) = 0 := by have A : ∀ q, 0 ≤ dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q) := fun _ => add_nonneg dist_nonneg dist_nonneg refine le_antisymm ?_ (le_ciInf A) have : 0 = dist (Φ p) (Φ p) + dist (Ψ p) (Ψ p) := by simp rw [this] exact ciInf_le ⟨0, forall_mem_range.2 A⟩ p simp only [glueDist, this, zero_add] private theorem glueDist_comm (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) : ∀ x y, glueDist Φ Ψ ε x y = glueDist Φ Ψ ε y x | .inl _, .inl _ => dist_comm _ _ | .inr _, .inr _ => dist_comm _ _ | .inl _, .inr _ => rfl | .inr _, .inl _ => rfl theorem glueDist_swap (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) : ∀ x y, glueDist Ψ Φ ε x.swap y.swap = glueDist Φ Ψ ε x y | .inl _, .inl _ => rfl | .inr _, .inr _ => rfl | .inl _, .inr _ => by simp only [glueDist, Sum.swap_inl, Sum.swap_inr, dist_comm, add_comm] | .inr _, .inl _ => by simp only [glueDist, Sum.swap_inl, Sum.swap_inr, dist_comm, add_comm] theorem le_glueDist_inl_inr (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (x y) : ε ≤ glueDist Φ Ψ ε (.inl x) (.inr y) := le_add_of_nonneg_left <| Real.iInf_nonneg fun _ => add_nonneg dist_nonneg dist_nonneg theorem le_glueDist_inr_inl (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (x y) : ε ≤ glueDist Φ Ψ ε (.inr x) (.inl y) := by rw [glueDist_comm]; apply le_glueDist_inl_inr section variable [Nonempty Z] private theorem glueDist_triangle_inl_inr_inr (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (x : X) (y z : Y) : glueDist Φ Ψ ε (.inl x) (.inr z) ≤ glueDist Φ Ψ ε (.inl x) (.inr y) + glueDist Φ Ψ ε (.inr y) (.inr z) := by simp only [glueDist] rw [add_right_comm, add_le_add_iff_right] refine le_ciInf_add fun p => ciInf_le_of_le ⟨0, ?_⟩ p ?_ · exact forall_mem_range.2 fun _ => add_nonneg dist_nonneg dist_nonneg · linarith [dist_triangle_left z (Ψ p) y] private theorem glueDist_triangle_inl_inr_inl (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (H : ∀ p q, |dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)| ≤ 2 * ε) (x : X) (y : Y) (z : X) : glueDist Φ Ψ ε (.inl x) (.inl z) ≤ glueDist Φ Ψ ε (.inl x) (.inr y) + glueDist Φ Ψ ε (.inr y) (.inl z) := by simp_rw [glueDist, add_add_add_comm _ ε, add_assoc] refine le_ciInf_add fun p => ?_ rw [add_left_comm, add_assoc, ← two_mul] refine le_ciInf_add fun q => ?_ rw [dist_comm z] linarith [dist_triangle4 x (Φ p) (Φ q) z, dist_triangle_left (Ψ p) (Ψ q) y, (abs_le.1 (H p q)).2] private theorem glueDist_triangle (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (H : ∀ p q, |dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)| ≤ 2 * ε) : ∀ x y z, glueDist Φ Ψ ε x z ≤ glueDist Φ Ψ ε x y + glueDist Φ Ψ ε y z | .inl _, .inl _, .inl _ => dist_triangle _ _ _ | .inr _, .inr _, .inr _ => dist_triangle _ _ _ | .inr x, .inl y, .inl z => by simp only [← glueDist_swap Φ] apply glueDist_triangle_inl_inr_inr | .inr x, .inr y, .inl z => by simpa only [glueDist_comm, add_comm] using glueDist_triangle_inl_inr_inr _ _ _ z y x | .inl x, .inl y, .inr z => by simpa only [← glueDist_swap Φ, glueDist_comm, add_comm, Sum.swap_inl, Sum.swap_inr] using glueDist_triangle_inl_inr_inr Ψ Φ ε z y x | .inl _, .inr _, .inr _ => glueDist_triangle_inl_inr_inr .. | .inl x, .inr y, .inl z => glueDist_triangle_inl_inr_inl Φ Ψ ε H x y z | .inr x, .inl y, .inr z => by simp only [← glueDist_swap Φ] apply glueDist_triangle_inl_inr_inl simpa only [abs_sub_comm] end private theorem eq_of_glueDist_eq_zero (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (ε0 : 0 < ε) : ∀ p q : X ⊕ Y, glueDist Φ Ψ ε p q = 0 → p = q | .inl x, .inl y, h => by rw [eq_of_dist_eq_zero h] | .inl x, .inr y, h => by exfalso; linarith [le_glueDist_inl_inr Φ Ψ ε x y] | .inr x, .inl y, h => by exfalso; linarith [le_glueDist_inr_inl Φ Ψ ε x y] | .inr x, .inr y, h => by rw [eq_of_dist_eq_zero h] theorem Sum.mem_uniformity_iff_glueDist (hε : 0 < ε) (s : Set ((X ⊕ Y) × (X ⊕ Y))) : s ∈ 𝓤 (X ⊕ Y) ↔ ∃ δ > 0, ∀ a b, glueDist Φ Ψ ε a b < δ → (a, b) ∈ s := by simp only [Sum.uniformity, Filter.mem_sup, Filter.mem_map, mem_uniformity_dist, mem_preimage] constructor · rintro ⟨⟨δX, δX0, hX⟩, δY, δY0, hY⟩ refine ⟨min (min δX δY) ε, lt_min (lt_min δX0 δY0) hε, ?_⟩ rintro (a | a) (b | b) h <;> simp only [lt_min_iff] at h · exact hX h.1.1 · exact absurd h.2 (le_glueDist_inl_inr _ _ _ _ _).not_lt · exact absurd h.2 (le_glueDist_inr_inl _ _ _ _ _).not_lt · exact hY h.1.2 · rintro ⟨ε, ε0, H⟩ constructor <;> exact ⟨ε, ε0, fun _ _ h => H _ _ h⟩ /-- Given two maps `Φ` and `Ψ` intro metric spaces `X` and `Y` such that the distances between `Φ p` and `Φ q`, and between `Ψ p` and `Ψ q`, coincide up to `2 ε` where `ε > 0`, one can almost glue the two spaces `X` and `Y` along the images of `Φ` and `Ψ`, so that `Φ p` and `Ψ p` are at distance `ε`. -/ def glueMetricApprox [Nonempty Z] (Φ : Z → X) (Ψ : Z → Y) (ε : ℝ) (ε0 : 0 < ε) (H : ∀ p q, |dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)| ≤ 2 * ε) : MetricSpace (X ⊕ Y) where dist := glueDist Φ Ψ ε dist_self := glueDist_self Φ Ψ ε dist_comm := glueDist_comm Φ Ψ ε dist_triangle := glueDist_triangle Φ Ψ ε H eq_of_dist_eq_zero := eq_of_glueDist_eq_zero Φ Ψ ε ε0 _ _ toUniformSpace := Sum.instUniformSpace uniformity_dist := uniformity_dist_of_mem_uniformity _ _ <| Sum.mem_uniformity_iff_glueDist ε0 end ApproxGluing section Sum /-! ### Metric on `X ⊕ Y` A particular case of the previous construction is when one uses basepoints in `X` and `Y` and one glues only along the basepoints, putting them at distance 1. We give a direct definition of the distance, without `iInf`, as it is easier to use in applications, and show that it is equal to the gluing distance defined above to take advantage of the lemmas we have already proved. -/ variable {X : Type u} {Y : Type v} {Z : Type w} variable [MetricSpace X] [MetricSpace Y] /-- Distance on a disjoint union. There are many (noncanonical) ways to put a distance compatible with each factor. If the two spaces are bounded, one can say for instance that each point in the first is at distance `diam X + diam Y + 1` of each point in the second. Instead, we choose a construction that works for unbounded spaces, but requires basepoints, chosen arbitrarily. We embed isometrically each factor, set the basepoints at distance 1, arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to their respective basepoints, plus the distance 1 between the basepoints. Since there is an arbitrary choice in this construction, it is not an instance by default. -/ protected def Sum.dist : X ⊕ Y → X ⊕ Y → ℝ | .inl a, .inl a' => dist a a' | .inr b, .inr b' => dist b b' | .inl a, .inr b => dist a (Nonempty.some ⟨a⟩) + 1 + dist (Nonempty.some ⟨b⟩) b | .inr b, .inl a => dist b (Nonempty.some ⟨b⟩) + 1 + dist (Nonempty.some ⟨a⟩) a theorem Sum.dist_eq_glueDist {p q : X ⊕ Y} (x : X) (y : Y) : Sum.dist p q = glueDist (fun _ : Unit => Nonempty.some ⟨x⟩) (fun _ : Unit => Nonempty.some ⟨y⟩) 1 p q := by cases p <;> cases q <;> first |rfl|simp [Sum.dist, glueDist, dist_comm, add_comm, add_left_comm, add_assoc] private theorem Sum.dist_comm (x y : X ⊕ Y) : Sum.dist x y = Sum.dist y x := by cases x <;> cases y <;> simp [Sum.dist, _root_.dist_comm, add_comm, add_left_comm, add_assoc] theorem Sum.one_le_dist_inl_inr {x : X} {y : Y} : 1 ≤ Sum.dist (.inl x) (.inr y) := le_trans (le_add_of_nonneg_right dist_nonneg) <| add_le_add_right (le_add_of_nonneg_left dist_nonneg) _ theorem Sum.one_le_dist_inr_inl {x : X} {y : Y} : 1 ≤ Sum.dist (.inr y) (.inl x) := by rw [Sum.dist_comm]; exact Sum.one_le_dist_inl_inr private theorem Sum.mem_uniformity (s : Set ((X ⊕ Y) × (X ⊕ Y))) : s ∈ 𝓤 (X ⊕ Y) ↔ ∃ ε > 0, ∀ a b, Sum.dist a b < ε → (a, b) ∈ s := by constructor · rintro ⟨hsX, hsY⟩ rcases mem_uniformity_dist.1 hsX with ⟨εX, εX0, hX⟩ rcases mem_uniformity_dist.1 hsY with ⟨εY, εY0, hY⟩ refine ⟨min (min εX εY) 1, lt_min (lt_min εX0 εY0) zero_lt_one, ?_⟩ rintro (a | a) (b | b) h · exact hX (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_left _ _))) · cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) Sum.one_le_dist_inl_inr · cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) Sum.one_le_dist_inr_inl · exact hY (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_right _ _))) · rintro ⟨ε, ε0, H⟩ constructor <;> rw [Filter.mem_map, mem_uniformity_dist] <;> exact ⟨ε, ε0, fun _ _ h => H _ _ h⟩ /-- The distance on the disjoint union indeed defines a metric space. All the distance properties follow from our choice of the distance. The harder work is to show that the uniform structure defined by the distance coincides with the disjoint union uniform structure. -/ def metricSpaceSum : MetricSpace (X ⊕ Y) where dist := Sum.dist dist_self x := by cases x <;> simp only [Sum.dist, dist_self] dist_comm := Sum.dist_comm dist_triangle | .inl p, .inl q, .inl r => dist_triangle p q r | .inl p, .inr q, _ => by simp only [Sum.dist_eq_glueDist p q] exact glueDist_triangle _ _ _ (by norm_num) _ _ _ | _, .inl q, .inr r => by simp only [Sum.dist_eq_glueDist q r] exact glueDist_triangle _ _ _ (by norm_num) _ _ _ | .inr p, _, .inl r => by simp only [Sum.dist_eq_glueDist r p] exact glueDist_triangle _ _ _ (by norm_num) _ _ _ | .inr p, .inr q, .inr r => dist_triangle p q r eq_of_dist_eq_zero {p q} h := by rcases p with p | p <;> rcases q with q | q · rw [eq_of_dist_eq_zero h] · exact eq_of_glueDist_eq_zero _ _ _ one_pos _ _ ((Sum.dist_eq_glueDist p q).symm.trans h) · exact eq_of_glueDist_eq_zero _ _ _ one_pos _ _ ((Sum.dist_eq_glueDist q p).symm.trans h) · rw [eq_of_dist_eq_zero h] toUniformSpace := Sum.instUniformSpace uniformity_dist := uniformity_dist_of_mem_uniformity _ _ Sum.mem_uniformity attribute [local instance] metricSpaceSum theorem Sum.dist_eq {x y : X ⊕ Y} : dist x y = Sum.dist x y := rfl /-- The left injection of a space in a disjoint union is an isometry -/ theorem isometry_inl : Isometry (Sum.inl : X → X ⊕ Y) := Isometry.of_dist_eq fun _ _ => rfl /-- The right injection of a space in a disjoint union is an isometry -/ theorem isometry_inr : Isometry (Sum.inr : Y → X ⊕ Y) := Isometry.of_dist_eq fun _ _ => rfl end Sum namespace Sigma /- Copy of the previous paragraph, but for arbitrary disjoint unions instead of the disjoint union of two spaces. I.e., work with sigma types instead of sum types. -/ variable {ι : Type*} {E : ι → Type*} [∀ i, MetricSpace (E i)] open Classical in /-- Distance on a disjoint union. There are many (noncanonical) ways to put a distance compatible with each factor. We choose a construction that works for unbounded spaces, but requires basepoints, chosen arbitrarily. We embed isometrically each factor, set the basepoints at distance 1, arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to their respective basepoints, plus the distance 1 between the basepoints. Since there is an arbitrary choice in this construction, it is not an instance by default. -/ protected def dist : (Σ i, E i) → (Σ i, E i) → ℝ | ⟨i, x⟩, ⟨j, y⟩ => if h : i = j then haveI : E j = E i := by rw [h] Dist.dist x (cast this y) else Dist.dist x (Nonempty.some ⟨x⟩) + 1 + Dist.dist (Nonempty.some ⟨y⟩) y /-- A `Dist` instance on the disjoint union `Σ i, E i`. We embed isometrically each factor, set the basepoints at distance 1, arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to their respective basepoints, plus the distance 1 between the basepoints. Since there is an arbitrary choice in this construction, it is not an instance by default. -/ def instDist : Dist (Σ i, E i) := ⟨Sigma.dist⟩ attribute [local instance] Sigma.instDist @[simp] theorem dist_same (i : ι) (x y : E i) : dist (Sigma.mk i x) ⟨i, y⟩ = dist x y := by simp [Dist.dist, Sigma.dist] @[simp] theorem dist_ne {i j : ι} (h : i ≠ j) (x : E i) (y : E j) : dist (⟨i, x⟩ : Σk, E k) ⟨j, y⟩ = dist x (Nonempty.some ⟨x⟩) + 1 + dist (Nonempty.some ⟨y⟩) y := dif_neg h theorem one_le_dist_of_ne {i j : ι} (h : i ≠ j) (x : E i) (y : E j) : 1 ≤ dist (⟨i, x⟩ : Σk, E k) ⟨j, y⟩ := by rw [Sigma.dist_ne h x y] linarith [@dist_nonneg _ _ x (Nonempty.some ⟨x⟩), @dist_nonneg _ _ (Nonempty.some ⟨y⟩) y] theorem fst_eq_of_dist_lt_one (x y : Σ i, E i) (h : dist x y < 1) : x.1 = y.1 := by cases x; cases y contrapose! h apply one_le_dist_of_ne h protected theorem dist_triangle (x y z : Σ i, E i) : dist x z ≤ dist x y + dist y z := by rcases x with ⟨i, x⟩; rcases y with ⟨j, y⟩; rcases z with ⟨k, z⟩ rcases eq_or_ne i k with (rfl | hik) · rcases eq_or_ne i j with (rfl | hij) · simpa using dist_triangle x y z · simp only [Sigma.dist_same, Sigma.dist_ne hij, Sigma.dist_ne hij.symm] calc dist x z ≤ dist x (Nonempty.some ⟨x⟩) + 0 + 0 + (0 + 0 + dist (Nonempty.some ⟨z⟩) z) := by simpa only [zero_add, add_zero] using dist_triangle _ _ _ _ ≤ _ := by apply_rules [add_le_add, le_rfl, dist_nonneg, zero_le_one] · rcases eq_or_ne i j with (rfl | hij) · simp only [Sigma.dist_ne hik, Sigma.dist_same] calc dist x (Nonempty.some ⟨x⟩) + 1 + dist (Nonempty.some ⟨z⟩) z ≤ dist x y + dist y (Nonempty.some ⟨y⟩) + 1 + dist (Nonempty.some ⟨z⟩) z := by apply_rules [add_le_add, le_rfl, dist_triangle] _ = _ := by abel · rcases eq_or_ne j k with (rfl | hjk) · simp only [Sigma.dist_ne hij, Sigma.dist_same] calc dist x (Nonempty.some ⟨x⟩) + 1 + dist (Nonempty.some ⟨z⟩) z ≤ dist x (Nonempty.some ⟨x⟩) + 1 + (dist (Nonempty.some ⟨z⟩) y + dist y z) := by apply_rules [add_le_add, le_rfl, dist_triangle] _ = _ := by abel · simp only [hik, hij, hjk, Sigma.dist_ne, Ne, not_false_iff] calc dist x (Nonempty.some ⟨x⟩) + 1 + dist (Nonempty.some ⟨z⟩) z = dist x (Nonempty.some ⟨x⟩) + 1 + 0 + (0 + 0 + dist (Nonempty.some ⟨z⟩) z) := by simp only [add_zero, zero_add] _ ≤ _ := by apply_rules [add_le_add, zero_le_one, dist_nonneg, le_rfl] protected theorem isOpen_iff (s : Set (Σ i, E i)) : IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s := by constructor · rintro hs ⟨i, x⟩ hx obtain ⟨ε, εpos, hε⟩ : ∃ ε > 0, ball x ε ⊆ Sigma.mk i ⁻¹' s := Metric.isOpen_iff.1 (isOpen_sigma_iff.1 hs i) x hx refine ⟨min ε 1, lt_min εpos zero_lt_one, ?_⟩ rintro ⟨j, y⟩ hy rcases eq_or_ne i j with (rfl | hij) · simp only [Sigma.dist_same, lt_min_iff] at hy exact hε (mem_ball'.2 hy.1) · apply (lt_irrefl (1 : ℝ) _).elim calc 1 ≤ Sigma.dist ⟨i, x⟩ ⟨j, y⟩ := Sigma.one_le_dist_of_ne hij _ _ _ < 1 := hy.trans_le (min_le_right _ _) · refine fun H => isOpen_sigma_iff.2 fun i => Metric.isOpen_iff.2 fun x hx => ?_ obtain ⟨ε, εpos, hε⟩ : ∃ ε > 0, ∀ y, dist (⟨i, x⟩ : Σj, E j) y < ε → y ∈ s := H ⟨i, x⟩ hx refine ⟨ε, εpos, fun y hy => ?_⟩ apply hε ⟨i, y⟩ rw [Sigma.dist_same] exact mem_ball'.1 hy /-- A metric space structure on the disjoint union `Σ i, E i`. We embed isometrically each factor, set the basepoints at distance 1, arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to their respective basepoints, plus the distance 1 between the basepoints. Since there is an arbitrary choice in this construction, it is not an instance by default. -/ protected def metricSpace : MetricSpace (Σ i, E i) := by refine MetricSpace.ofDistTopology Sigma.dist ?_ ?_ Sigma.dist_triangle Sigma.isOpen_iff ?_ · rintro ⟨i, x⟩ simp [Sigma.dist] · rintro ⟨i, x⟩ ⟨j, y⟩ rcases eq_or_ne i j with (rfl | h) · simp [Sigma.dist, dist_comm] · simp only [Sigma.dist, dist_comm, h, h.symm, not_false_iff, dif_neg] abel · rintro ⟨i, x⟩ ⟨j, y⟩ rcases eq_or_ne i j with (rfl | hij) · simp [Sigma.dist] · intro h apply (lt_irrefl (1 : ℝ) _).elim calc 1 ≤ Sigma.dist (⟨i, x⟩ : Σk, E k) ⟨j, y⟩ := Sigma.one_le_dist_of_ne hij _ _ _ < 1 := by rw [h]; exact zero_lt_one attribute [local instance] Sigma.metricSpace open Topology open Filter /-- The injection of a space in a disjoint union is an isometry -/ theorem isometry_mk (i : ι) : Isometry (Sigma.mk i : E i → Σk, E k) := Isometry.of_dist_eq fun x y => by simp /-- A disjoint union of complete metric spaces is complete. -/ protected theorem completeSpace [∀ i, CompleteSpace (E i)] : CompleteSpace (Σ i, E i) := by set s : ι → Set (Σ i, E i) := fun i => Sigma.fst ⁻¹' {i} set U := { p : (Σk, E k) × Σk, E k | dist p.1 p.2 < 1 } have hc : ∀ i, IsComplete (s i) := fun i => by simp only [s, ← range_sigmaMk] exact (isometry_mk i).isUniformInducing.isComplete_range have hd : ∀ (i j), ∀ x ∈ s i, ∀ y ∈ s j, (x, y) ∈ U → i = j := fun i j x hx y hy hxy => (Eq.symm hx).trans ((fst_eq_of_dist_lt_one _ _ hxy).trans hy) refine completeSpace_of_isComplete_univ ?_ convert isComplete_iUnion_separated hc (dist_mem_uniformity zero_lt_one) hd simp only [s, ← preimage_iUnion, iUnion_of_singleton, preimage_univ] end Sigma section Gluing -- Exact gluing of two metric spaces along isometric subsets. variable {X : Type u} {Y : Type v} {Z : Type w} variable [Nonempty Z] [MetricSpace Z] [MetricSpace X] [MetricSpace Y] {Φ : Z → X} {Ψ : Z → Y} {ε : ℝ} /-- Given two isometric embeddings `Φ : Z → X` and `Ψ : Z → Y`, we define a pseudo metric space structure on `X ⊕ Y` by declaring that `Φ x` and `Ψ x` are at distance `0`. -/ def gluePremetric (hΦ : Isometry Φ) (hΨ : Isometry Ψ) : PseudoMetricSpace (X ⊕ Y) where dist := glueDist Φ Ψ 0 dist_self := glueDist_self Φ Ψ 0 dist_comm := glueDist_comm Φ Ψ 0 dist_triangle := glueDist_triangle Φ Ψ 0 fun p q => by rw [hΦ.dist_eq, hΨ.dist_eq]; simp /-- Given two isometric embeddings `Φ : Z → X` and `Ψ : Z → Y`, we define a space `GlueSpace hΦ hΨ` by identifying in `X ⊕ Y` the points `Φ x` and `Ψ x`. -/ def GlueSpace (hΦ : Isometry Φ) (hΨ : Isometry Ψ) : Type _ := @SeparationQuotient _ (gluePremetric hΦ hΨ).toUniformSpace.toTopologicalSpace instance (hΦ : Isometry Φ) (hΨ : Isometry Ψ) : MetricSpace (GlueSpace hΦ hΨ) := inferInstanceAs <| MetricSpace <| @SeparationQuotient _ (gluePremetric hΦ hΨ).toUniformSpace.toTopologicalSpace /-- The canonical map from `X` to the space obtained by gluing isometric subsets in `X` and `Y`. -/ def toGlueL (hΦ : Isometry Φ) (hΨ : Isometry Ψ) (x : X) : GlueSpace hΦ hΨ := Quotient.mk'' (.inl x) /-- The canonical map from `Y` to the space obtained by gluing isometric subsets in `X` and `Y`. -/ def toGlueR (hΦ : Isometry Φ) (hΨ : Isometry Ψ) (y : Y) : GlueSpace hΦ hΨ := Quotient.mk'' (.inr y) instance inhabitedLeft (hΦ : Isometry Φ) (hΨ : Isometry Ψ) [Inhabited X] : Inhabited (GlueSpace hΦ hΨ) := ⟨toGlueL _ _ default⟩ instance inhabitedRight (hΦ : Isometry Φ) (hΨ : Isometry Ψ) [Inhabited Y] : Inhabited (GlueSpace hΦ hΨ) := ⟨toGlueR _ _ default⟩ theorem toGlue_commute (hΦ : Isometry Φ) (hΨ : Isometry Ψ) : toGlueL hΦ hΨ ∘ Φ = toGlueR hΦ hΨ ∘ Ψ := by let i : PseudoMetricSpace (X ⊕ Y) := gluePremetric hΦ hΨ let _ := i.toUniformSpace.toTopologicalSpace funext simp only [comp, toGlueL, toGlueR] refine SeparationQuotient.mk_eq_mk.2 (Metric.inseparable_iff.2 ?_) exact glueDist_glued_points Φ Ψ 0 _ theorem toGlueL_isometry (hΦ : Isometry Φ) (hΨ : Isometry Ψ) : Isometry (toGlueL hΦ hΨ) := Isometry.of_dist_eq fun _ _ => rfl theorem toGlueR_isometry (hΦ : Isometry Φ) (hΨ : Isometry Ψ) : Isometry (toGlueR hΦ hΨ) := Isometry.of_dist_eq fun _ _ => rfl end Gluing --section section InductiveLimit /-! ### Inductive limit of metric spaces In this section, we define the inductive limit of ``` f 0 f 1 f 2 f 3 X 0 -----> X 1 -----> X 2 -----> X 3 -----> ... ``` where the `X n` are metric spaces and f n isometric embeddings. We do it by defining a premetric
space structure on `Σ n, X n`, where the predistance `dist x y` is obtained by pushing `x` and `y` in a common `X k` using composition by the `f n`, and taking the distance there. This does not depend on the choice of `k` as the `f n` are isometries. The metric space associated to this premetric space is the desired inductive limit. -/ open Nat
Mathlib/Topology/MetricSpace/Gluing.lean
515
522
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Order.BigOperators.Ring.Finset import Mathlib.Analysis.Convex.Hull import Mathlib.LinearAlgebra.AffineSpace.Basis /-! # Convex combinations This file defines convex combinations of points in a vector space. ## Main declarations * `Finset.centerMass`: Center of mass of a finite family of points. ## Implementation notes We divide by the sum of the weights in the definition of `Finset.centerMass` because of the way mathematical arguments go: one doesn't change weights, but merely adds some. This also makes a few lemmas unconditional on the sum of the weights being `1`. -/ open Set Function Pointwise universe u u' section variable {R R' E F ι ι' α : Type*} [Field R] [Field R'] [AddCommGroup E] [AddCommGroup F] [AddCommGroup α] [LinearOrder α] [Module R E] [Module R F] [Module R α] {s : Set E} /-- Center of mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ def Finset.centerMass (t : Finset ι) (w : ι → R) (z : ι → E) : E := (∑ i ∈ t, w i)⁻¹ • ∑ i ∈ t, w i • z i variable (i j : ι) (c : R) (t : Finset ι) (w : ι → R) (z : ι → E) open Finset theorem Finset.centerMass_empty : (∅ : Finset ι).centerMass w z = 0 := by simp only [centerMass, sum_empty, smul_zero] theorem Finset.centerMass_pair [DecidableEq ι] (hne : i ≠ j) : ({i, j} : Finset ι).centerMass w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := by simp only [centerMass, sum_pair hne] module variable {w} theorem Finset.centerMass_insert [DecidableEq ι] (ha : i ∉ t) (hw : ∑ j ∈ t, w j ≠ 0) : (insert i t).centerMass w z = (w i / (w i + ∑ j ∈ t, w j)) • z i + ((∑ j ∈ t, w j) / (w i + ∑ j ∈ t, w j)) • t.centerMass w z := by simp only [centerMass, sum_insert ha, smul_add, (mul_smul _ _ _).symm, ← div_eq_inv_mul] congr 2 rw [div_mul_eq_mul_div, mul_inv_cancel₀ hw, one_div] theorem Finset.centerMass_singleton (hw : w i ≠ 0) : ({i} : Finset ι).centerMass w z = z i := by rw [centerMass, sum_singleton, sum_singleton] match_scalars field_simp @[simp] lemma Finset.centerMass_neg_left : t.centerMass (-w) z = t.centerMass w z := by simp [centerMass, inv_neg] lemma Finset.centerMass_smul_left {c : R'} [Module R' R] [Module R' E] [SMulCommClass R' R R] [IsScalarTower R' R R] [SMulCommClass R R' E] [IsScalarTower R' R E] (hc : c ≠ 0) : t.centerMass (c • w) z = t.centerMass w z := by simp [centerMass, -smul_assoc, smul_assoc c, ← smul_sum, smul_inv₀, smul_smul_smul_comm, hc] theorem Finset.centerMass_eq_of_sum_1 (hw : ∑ i ∈ t, w i = 1) : t.centerMass w z = ∑ i ∈ t, w i • z i := by simp only [Finset.centerMass, hw, inv_one, one_smul] theorem Finset.centerMass_smul : (t.centerMass w fun i => c • z i) = c • t.centerMass w z := by simp only [Finset.centerMass, Finset.smul_sum, (mul_smul _ _ _).symm, mul_comm c, mul_assoc] /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ theorem Finset.centerMass_segment' (s : Finset ι) (t : Finset ι') (ws : ι → R) (zs : ι → E) (wt : ι' → R) (zt : ι' → E) (hws : ∑ i ∈ s, ws i = 1) (hwt : ∑ i ∈ t, wt i = 1) (a b : R) (hab : a + b = 1) : a • s.centerMass ws zs + b • t.centerMass wt zt = (s.disjSum t).centerMass (Sum.elim (fun i => a * ws i) fun j => b * wt j) (Sum.elim zs zt) := by rw [s.centerMass_eq_of_sum_1 _ hws, t.centerMass_eq_of_sum_1 _ hwt, smul_sum, smul_sum, ← Finset.sum_sumElim, Finset.centerMass_eq_of_sum_1] · congr with ⟨⟩ <;> simp only [Sum.elim_inl, Sum.elim_inr, mul_smul] · rw [sum_sumElim, ← mul_sum, ← mul_sum, hws, hwt, mul_one, mul_one, hab] /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ theorem Finset.centerMass_segment (s : Finset ι) (w₁ w₂ : ι → R) (z : ι → E) (hw₁ : ∑ i ∈ s, w₁ i = 1) (hw₂ : ∑ i ∈ s, w₂ i = 1) (a b : R) (hab : a + b = 1) : a • s.centerMass w₁ z + b • s.centerMass w₂ z = s.centerMass (fun i => a * w₁ i + b * w₂ i) z := by have hw : (∑ i ∈ s, (a * w₁ i + b * w₂ i)) = 1 := by simp only [← mul_sum, sum_add_distrib, mul_one, *] simp only [Finset.centerMass_eq_of_sum_1, Finset.centerMass_eq_of_sum_1 _ _ hw, smul_sum, sum_add_distrib, add_smul, mul_smul, *] theorem Finset.centerMass_ite_eq [DecidableEq ι] (hi : i ∈ t) : t.centerMass (fun j => if i = j then (1 : R) else 0) z = z i := by rw [Finset.centerMass_eq_of_sum_1] · trans ∑ j ∈ t, if i = j then z i else 0 · congr with i split_ifs with h exacts [h ▸ one_smul _ _, zero_smul _ _] · rw [sum_ite_eq, if_pos hi] · rw [sum_ite_eq, if_pos hi] variable {t} theorem Finset.centerMass_subset {t' : Finset ι} (ht : t ⊆ t') (h : ∀ i ∈ t', i ∉ t → w i = 0) : t.centerMass w z = t'.centerMass w z := by rw [centerMass, sum_subset ht h, smul_sum, centerMass, smul_sum] apply sum_subset ht intro i hit' hit rw [h i hit' hit, zero_smul, smul_zero] theorem Finset.centerMass_filter_ne_zero [∀ i, Decidable (w i ≠ 0)] : {i ∈ t | w i ≠ 0}.centerMass w z = t.centerMass w z := Finset.centerMass_subset z (filter_subset _ _) fun i hit hit' => by simpa only [hit, mem_filter, true_and, Ne, Classical.not_not] using hit' namespace Finset variable [LinearOrder R] [IsStrictOrderedRing R] [IsOrderedAddMonoid α] [OrderedSMul R α] theorem centerMass_le_sup {s : Finset ι} {f : ι → α} {w : ι → R} (hw₀ : ∀ i ∈ s, 0 ≤ w i) (hw₁ : 0 < ∑ i ∈ s, w i) : s.centerMass w f ≤ s.sup' (nonempty_of_ne_empty <| by rintro rfl; simp at hw₁) f := by rw [centerMass, inv_smul_le_iff_of_pos hw₁, sum_smul] exact sum_le_sum fun i hi => smul_le_smul_of_nonneg_left (le_sup' _ hi) <| hw₀ i hi theorem inf_le_centerMass {s : Finset ι} {f : ι → α} {w : ι → R} (hw₀ : ∀ i ∈ s, 0 ≤ w i) (hw₁ : 0 < ∑ i ∈ s, w i) : s.inf' (nonempty_of_ne_empty <| by rintro rfl; simp at hw₁) f ≤ s.centerMass w f := centerMass_le_sup (α := αᵒᵈ) hw₀ hw₁ end Finset variable {z} lemma Finset.centerMass_of_sum_add_sum_eq_zero {s t : Finset ι} (hw : ∑ i ∈ s, w i + ∑ i ∈ t, w i = 0) (hz : ∑ i ∈ s, w i • z i + ∑ i ∈ t, w i • z i = 0) : s.centerMass w z = t.centerMass w z := by simp [centerMass, eq_neg_of_add_eq_zero_right hw, eq_neg_of_add_eq_zero_left hz, ← neg_inv] variable [LinearOrder R] [IsStrictOrderedRing R] [IsOrderedAddMonoid α] [OrderedSMul R α] /-- The center of mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ theorem Convex.centerMass_mem (hs : Convex R s) : (∀ i ∈ t, 0 ≤ w i) → (0 < ∑ i ∈ t, w i) → (∀ i ∈ t, z i ∈ s) → t.centerMass w z ∈ s := by classical induction' t using Finset.induction with i t hi ht · simp [lt_irrefl] intro h₀ hpos hmem have zi : z i ∈ s := hmem _ (mem_insert_self _ _) have hs₀ : ∀ j ∈ t, 0 ≤ w j := fun j hj => h₀ j <| mem_insert_of_mem hj rw [sum_insert hi] at hpos by_cases hsum_t : ∑ j ∈ t, w j = 0 · have ws : ∀ j ∈ t, w j = 0 := (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_t have wz : ∑ j ∈ t, w j • z j = 0 := sum_eq_zero fun i hi => by simp [ws i hi] simp only [centerMass, sum_insert hi, wz, hsum_t, add_zero] simp only [hsum_t, add_zero] at hpos rw [← mul_smul, inv_mul_cancel₀ (ne_of_gt hpos), one_smul] exact zi · rw [Finset.centerMass_insert _ _ _ hi hsum_t] refine convex_iff_div.1 hs zi (ht hs₀ ?_ ?_) ?_ (sum_nonneg hs₀) hpos · exact lt_of_le_of_ne (sum_nonneg hs₀) (Ne.symm hsum_t) · intro j hj exact hmem j (mem_insert_of_mem hj) · exact h₀ _ (mem_insert_self _ _) theorem Convex.sum_mem (hs : Convex R s) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i ∈ t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) : (∑ i ∈ t, w i • z i) ∈ s := by simpa only [h₁, centerMass, inv_one, one_smul] using hs.centerMass_mem h₀ (h₁.symm ▸ zero_lt_one) hz /-- A version of `Convex.sum_mem` for `finsum`s. If `s` is a convex set, `w : ι → R` is a family of nonnegative weights with sum one and `z : ι → E` is a family of elements of a module over `R` such that `z i ∈ s` whenever `w i ≠ 0`, then the sum `∑ᶠ i, w i • z i` belongs to `s`. See also `PartitionOfUnity.finsum_smul_mem_convex`. -/ theorem Convex.finsum_mem {ι : Sort*} {w : ι → R} {z : ι → E} {s : Set E} (hs : Convex R s) (h₀ : ∀ i, 0 ≤ w i) (h₁ : ∑ᶠ i, w i = 1) (hz : ∀ i, w i ≠ 0 → z i ∈ s) : (∑ᶠ i, w i • z i) ∈ s := by have hfin_w : (support (w ∘ PLift.down)).Finite := by by_contra H rw [finsum, dif_neg H] at h₁ exact zero_ne_one h₁ have hsub : support ((fun i => w i • z i) ∘ PLift.down) ⊆ hfin_w.toFinset := (support_smul_subset_left _ _).trans hfin_w.coe_toFinset.ge rw [finsum_eq_sum_plift_of_support_subset hsub] refine hs.sum_mem (fun _ _ => h₀ _) ?_ fun i hi => hz _ ?_ · rwa [finsum, dif_pos hfin_w] at h₁ · rwa [hfin_w.mem_toFinset] at hi theorem convex_iff_sum_mem : Convex R s ↔ ∀ (t : Finset E) (w : E → R), (∀ i ∈ t, 0 ≤ w i) → ∑ i ∈ t, w i = 1 → (∀ x ∈ t, x ∈ s) → (∑ x ∈ t, w x • x) ∈ s := by classical refine ⟨fun hs t w hw₀ hw₁ hts => hs.sum_mem hw₀ hw₁ hts, ?_⟩ intro h x hx y hy a b ha hb hab by_cases h_cases : x = y · rw [h_cases, ← add_smul, hab, one_smul] exact hy · convert h {x, y} (fun z => if z = y then b else a) _ _ _ · simp only [sum_pair h_cases, if_neg h_cases, if_pos trivial] · intro i _ simp only split_ifs <;> assumption · simp only [sum_pair h_cases, if_neg h_cases, if_pos trivial, hab] · intro i hi simp only [Finset.mem_singleton, Finset.mem_insert] at hi cases hi <;> subst i <;> assumption theorem Finset.centerMass_mem_convexHull (t : Finset ι) {w : ι → R} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i ∈ t, w i) {z : ι → E} (hz : ∀ i ∈ t, z i ∈ s) : t.centerMass w z ∈ convexHull R s := (convex_convexHull R s).centerMass_mem hw₀ hws fun i hi => subset_convexHull R s <| hz i hi /-- A version of `Finset.centerMass_mem_convexHull` for when the weights are nonpositive. -/ lemma Finset.centerMass_mem_convexHull_of_nonpos (t : Finset ι) (hw₀ : ∀ i ∈ t, w i ≤ 0) (hws : ∑ i ∈ t, w i < 0) (hz : ∀ i ∈ t, z i ∈ s) : t.centerMass w z ∈ convexHull R s := by rw [← centerMass_neg_left] exact Finset.centerMass_mem_convexHull _ (fun _i hi ↦ neg_nonneg.2 <| hw₀ _ hi) (by simpa) hz /-- A refinement of `Finset.centerMass_mem_convexHull` when the indexed family is a `Finset` of the space. -/ theorem Finset.centerMass_id_mem_convexHull (t : Finset E) {w : E → R} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i ∈ t, w i) : t.centerMass w id ∈ convexHull R (t : Set E) := t.centerMass_mem_convexHull hw₀ hws fun _ => mem_coe.2 /-- A version of `Finset.centerMass_mem_convexHull` for when the weights are nonpositive. -/ lemma Finset.centerMass_id_mem_convexHull_of_nonpos (t : Finset E) {w : E → R} (hw₀ : ∀ i ∈ t, w i ≤ 0) (hws : ∑ i ∈ t, w i < 0) : t.centerMass w id ∈ convexHull R (t : Set E) := t.centerMass_mem_convexHull_of_nonpos hw₀ hws fun _ ↦ mem_coe.2 omit [LinearOrder R] [IsStrictOrderedRing R] in theorem affineCombination_eq_centerMass {ι : Type*} {t : Finset ι} {p : ι → E} {w : ι → R} (hw₂ : ∑ i ∈ t, w i = 1) : t.affineCombination R p w = centerMass t w p := by rw [affineCombination_eq_weightedVSubOfPoint_vadd_of_sum_eq_one _ w _ hw₂ (0 : E), Finset.weightedVSubOfPoint_apply, vadd_eq_add, add_zero, t.centerMass_eq_of_sum_1 _ hw₂] simp_rw [vsub_eq_sub, sub_zero] theorem affineCombination_mem_convexHull {s : Finset ι} {v : ι → E} {w : ι → R} (hw₀ : ∀ i ∈ s, 0 ≤ w i) (hw₁ : s.sum w = 1) : s.affineCombination R v w ∈ convexHull R (range v) := by rw [affineCombination_eq_centerMass hw₁] apply s.centerMass_mem_convexHull hw₀ · simp [hw₁] · simp /-- The centroid can be regarded as a center of mass. -/ @[simp] theorem Finset.centroid_eq_centerMass (s : Finset ι) (hs : s.Nonempty) (p : ι → E) : s.centroid R p = s.centerMass (s.centroidWeights R) p := affineCombination_eq_centerMass (s.sum_centroidWeights_eq_one_of_nonempty R hs) theorem Finset.centroid_mem_convexHull (s : Finset E) (hs : s.Nonempty) : s.centroid R id ∈ convexHull R (s : Set E) := by rw [s.centroid_eq_centerMass hs] apply s.centerMass_id_mem_convexHull · simp only [inv_nonneg, imp_true_iff, Nat.cast_nonneg, Finset.centroidWeights_apply] · have hs_card : (#s : R) ≠ 0 := by simp [Finset.nonempty_iff_ne_empty.mp hs] simp only [hs_card, Finset.sum_const, nsmul_eq_mul, mul_inv_cancel₀, Ne, not_false_iff, Finset.centroidWeights_apply, zero_lt_one] theorem convexHull_range_eq_exists_affineCombination (v : ι → E) : convexHull R (range v) = { x | ∃ (s : Finset ι) (w : ι → R), (∀ i ∈ s, 0 ≤ w i) ∧ s.sum w = 1 ∧ s.affineCombination R v w = x } := by classical refine Subset.antisymm (convexHull_min ?_ ?_) ?_ · intro x hx obtain ⟨i, hi⟩ := Set.mem_range.mp hx exact ⟨{i}, Function.const ι (1 : R), by simp, by simp, by simp [hi]⟩ · rintro x ⟨s, w, hw₀, hw₁, rfl⟩ y ⟨s', w', hw₀', hw₁', rfl⟩ a b ha hb hab let W : ι → R := fun i => (if i ∈ s then a * w i else 0) + if i ∈ s' then b * w' i else 0 have hW₁ : (s ∪ s').sum W = 1 := by rw [sum_add_distrib, ← sum_subset subset_union_left, ← sum_subset subset_union_right, sum_ite_of_true, sum_ite_of_true, ← mul_sum, ← mul_sum, hw₁, hw₁', ← add_mul, hab, mul_one] <;> intros <;> simp_all refine ⟨s ∪ s', W, ?_, hW₁, ?_⟩ · rintro i - by_cases hi : i ∈ s <;> by_cases hi' : i ∈ s' <;> simp [W, hi, hi', add_nonneg, mul_nonneg ha (hw₀ i _), mul_nonneg hb (hw₀' i _)] · simp_rw [W, affineCombination_eq_linear_combination (s ∪ s') v _ hW₁, affineCombination_eq_linear_combination s v w hw₁, affineCombination_eq_linear_combination s' v w' hw₁', add_smul, sum_add_distrib] rw [← sum_subset subset_union_left, ← sum_subset subset_union_right] · simp only [ite_smul, sum_ite_of_true fun _ hi => hi, mul_smul, ← smul_sum] · intro i _ hi' simp [hi'] · intro i _ hi' simp [hi'] · rintro x ⟨s, w, hw₀, hw₁, rfl⟩ exact affineCombination_mem_convexHull hw₀ hw₁ /-- Convex hull of `s` is equal to the set of all centers of masses of `Finset`s `t`, `z '' t ⊆ s`. For universe reasons, you shouldn't use this lemma to prove that a given center of mass belongs to the convex hull. Use convexity of the convex hull instead. -/ theorem convexHull_eq (s : Set E) : convexHull R s = { x : E | ∃ (ι : Type) (t : Finset ι) (w : ι → R) (z : ι → E), (∀ i ∈ t, 0 ≤ w i) ∧ ∑ i ∈ t, w i = 1 ∧ (∀ i ∈ t, z i ∈ s) ∧ t.centerMass w z = x } := by refine Subset.antisymm (convexHull_min ?_ ?_) ?_ · intro x hx use PUnit, {PUnit.unit}, fun _ => 1, fun _ => x, fun _ _ => zero_le_one, sum_singleton _ _, fun _ _ => hx simp only [Finset.centerMass, Finset.sum_singleton, inv_one, one_smul] · rintro x ⟨ι, sx, wx, zx, hwx₀, hwx₁, hzx, rfl⟩ y ⟨ι', sy, wy, zy, hwy₀, hwy₁, hzy, rfl⟩ a b ha hb hab rw [Finset.centerMass_segment' _ _ _ _ _ _ hwx₁ hwy₁ _ _ hab] refine ⟨_, _, _, _, ?_, ?_, ?_, rfl⟩ · rintro i hi rw [Finset.mem_disjSum] at hi rcases hi with (⟨j, hj, rfl⟩ | ⟨j, hj, rfl⟩) <;> simp only [Sum.elim_inl, Sum.elim_inr] <;> apply_rules [mul_nonneg, hwx₀, hwy₀] · simp [Finset.sum_sumElim, ← mul_sum, *] · intro i hi rw [Finset.mem_disjSum] at hi rcases hi with (⟨j, hj, rfl⟩ | ⟨j, hj, rfl⟩) <;> apply_rules [hzx, hzy]
· rintro _ ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩ exact t.centerMass_mem_convexHull hw₀ (hw₁.symm ▸ zero_lt_one) hz /-- Universe polymorphic version of the reverse implication of `mem_convexHull_iff_exists_fintype`. -/ lemma mem_convexHull_of_exists_fintype {s : Set E} {x : E} [Fintype ι] (w : ι → R) (z : ι → E) (hw₀ : ∀ i, 0 ≤ w i) (hw₁ : ∑ i, w i = 1) (hz : ∀ i, z i ∈ s) (hx : ∑ i, w i • z i = x) : x ∈ convexHull R s := by rw [← hx, ← centerMass_eq_of_sum_1 _ _ hw₁] exact centerMass_mem_convexHull _ (by simpa using hw₀) (by simp [hw₁]) (by simpa using hz) /-- The convex hull of `s` is equal to the set of centers of masses of finite families of points in `s`. For universe reasons, you shouldn't use this lemma to prove that a given center of mass belongs to the convex hull. Use `mem_convexHull_of_exists_fintype` of the convex hull instead. -/ lemma mem_convexHull_iff_exists_fintype {s : Set E} {x : E} : x ∈ convexHull R s ↔ ∃ (ι : Type) (_ : Fintype ι) (w : ι → R) (z : ι → E), (∀ i, 0 ≤ w i) ∧ ∑ i, w i = 1 ∧ (∀ i, z i ∈ s) ∧ ∑ i, w i • z i = x := by constructor · simp only [convexHull_eq, mem_setOf_eq] rintro ⟨ι, t, w, z, h⟩
Mathlib/Analysis/Convex/Combination.lean
329
350