Context
stringlengths
227
76.5k
target
stringlengths
0
11.6k
file_name
stringlengths
21
79
start
int64
14
3.67k
end
int64
16
3.69k
/- Copyright (c) 2023 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
Mathlib/MeasureTheory/Integral/PeakFunction.lean
54
86
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Control.Combinators import Mathlib.Data.Option.Defs import Mathlib.Logic.IsEmpty import Mathlib.Logic.Relator import Mathlib.Util.CompileInductive import Aesop /-! # Option of a type This file develops the basic theory of option types. If `α` is a type, then `Option α` can be understood as the type with one more element than `α`. `Option α` has terms `some a`, where `a : α`, and `none`, which is the added element. This is useful in multiple ways: * It is the prototype of addition of terms to a type. See for example `WithBot α` which uses `none` as an element smaller than all others. * It can be used to define failsafe partial functions, which return `some the_result_we_expect` if we can find `the_result_we_expect`, and `none` if there is no meaningful result. This forces any subsequent use of the partial function to explicitly deal with the exceptions that make it return `none`. * `Option` is a monad. We love monads. `Part` is an alternative to `Option` that can be seen as the type of `True`/`False` values along with a term `a : α` if the value is `True`. -/ universe u namespace Option variable {α β γ δ : Type*} theorem coe_def : (fun a ↦ ↑a : α → Option α) = some := rfl theorem mem_map {f : α → β} {y : β} {o : Option α} : y ∈ o.map f ↔ ∃ x ∈ o, f x = y := by simp -- The simpNF linter says that the LHS can be simplified via `Option.mem_def`. -- However this is a higher priority lemma. -- It seems the side condition `H` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Function.Injective f) {a : α} {o : Option α} : f a ∈ o.map f ↔ a ∈ o := by aesop theorem forall_mem_map {f : α → β} {o : Option α} {p : β → Prop} : (∀ y ∈ o.map f, p y) ↔ ∀ x ∈ o, p (f x) := by simp theorem exists_mem_map {f : α → β} {o : Option α} {p : β → Prop} : (∃ y ∈ o.map f, p y) ↔ ∃ x ∈ o, p (f x) := by simp theorem coe_get {o : Option α} (h : o.isSome) : ((Option.get _ h : α) : Option α) = o := Option.some_get h theorem eq_of_mem_of_mem {a : α} {o1 o2 : Option α} (h1 : a ∈ o1) (h2 : a ∈ o2) : o1 = o2 := h1.trans h2.symm theorem Mem.leftUnique : Relator.LeftUnique ((· ∈ ·) : α → Option α → Prop) := fun _ _ _=> mem_unique theorem some_injective (α : Type*) : Function.Injective (@some α) := fun _ _ ↦ some_inj.mp /-- `Option.map f` is injective if `f` is injective. -/ theorem map_injective {f : α → β} (Hf : Function.Injective f) : Function.Injective (Option.map f) | none, none, _ => rfl | some a₁, some a₂, H => by rw [Hf (Option.some.inj H)] @[simp] theorem map_comp_some (f : α → β) : Option.map f ∘ some = some ∘ f := rfl @[simp] theorem none_bind' (f : α → Option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → Option β) : (some a).bind f = f a := rfl theorem bind_eq_some' {x : Option α} {f : α → Option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x <;> simp @[congr] theorem bind_congr' {f g : α → Option β} {x y : Option α} (hx : x = y) (hf : ∀ a ∈ y, f a = g a) : x.bind f = y.bind g := hx.symm ▸ bind_congr hf @[deprecated bind_congr (since := "2025-03-20")] -- This was renamed from `bind_congr` after https://github.com/leanprover/lean4/pull/7529 -- upstreamed it with a slightly different statement. theorem bind_congr'' {f g : α → Option β} {x : Option α} (h : ∀ a ∈ x, f a = g a) : x.bind f = x.bind g := by cases x <;> simp only [some_bind, none_bind, mem_def, h] theorem joinM_eq_join : joinM = @join α := funext fun _ ↦ rfl theorem bind_eq_bind' {α β : Type u} {f : α → Option β} {x : Option α} : x >>= f = x.bind f := rfl theorem map_coe {α β} {a : α} {f : α → β} : f <$> (a : Option α) = ↑(f a) := rfl @[simp] theorem map_coe' {a : α} {f : α → β} : Option.map f (a : Option α) = ↑(f a) := rfl /-- `Option.map` as a function between functions is injective. -/ theorem map_injective' : Function.Injective (@Option.map α β) := fun f g h ↦ funext fun x ↦ some_injective _ <| by simp only [← map_some', h] @[simp] theorem map_inj {f g : α → β} : Option.map f = Option.map g ↔ f = g := map_injective'.eq_iff attribute [simp] map_id @[simp] theorem map_eq_id {f : α → α} : Option.map f = id ↔ f = id := map_injective'.eq_iff' map_id theorem map_comm {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ} (h : g₁ ∘ f₁ = g₂ ∘ f₂) (a : α) : (Option.map f₁ a).map g₁ = (Option.map f₂ a).map g₂ := by rw [map_map, h, ← map_map] section pmap variable {p : α → Prop} (f : ∀ a : α, p a → β) (x : Option α) @[simp] theorem pbind_eq_bind (f : α → Option β) (x : Option α) : (x.pbind fun a _ ↦ f a) = x.bind f := by cases x <;> simp only [pbind, none_bind', some_bind'] theorem map_bind' (f : β → γ) (x : Option α) (g : α → Option β) : Option.map f (x.bind g) = x.bind fun a ↦ Option.map f (g a) := by cases x <;> simp theorem pbind_map (f : α → β) (x : Option α) (g : ∀ b : β, b ∈ x.map f → Option γ) : pbind (Option.map f x) g = x.pbind fun a h ↦ g (f a) (mem_map_of_mem _ h) := by cases x <;> rfl theorem mem_pmem {a : α} (h : ∀ a ∈ x, p a) (ha : a ∈ x) : f a (h a ha) ∈ pmap f x h := by rw [mem_def] at ha ⊢ subst ha rfl theorem pmap_bind {α β γ} {x : Option α} {g : α → Option β} {p : β → Prop} {f : ∀ b, p b → γ} (H) (H' : ∀ (a : α), ∀ b ∈ g a, b ∈ x >>= g) : pmap f (x >>= g) H = x >>= fun a ↦ pmap f (g a) fun _ h ↦ H _ (H' a _ h) := by cases x <;> simp only [pmap, bind_eq_bind, none_bind, some_bind] theorem bind_pmap {α β γ} {p : α → Prop} (f : ∀ a, p a → β) (x : Option α) (g : β → Option γ) (H) : pmap f x H >>= g = x.pbind fun a h ↦ g (f a (H _ h)) := by cases x <;> simp only [pmap, bind_eq_bind, none_bind, some_bind, pbind] variable {f x} theorem pbind_eq_none {f : ∀ a : α, a ∈ x → Option β} (h' : ∀ a (H : a ∈ x), f a H = none → x = none) : x.pbind f = none ↔ x = none := by cases x · simp · simp only [pbind, iff_false, reduceCtorEq] intro h cases h' _ rfl h theorem pbind_eq_some {f : ∀ a : α, a ∈ x → Option β} {y : β} : x.pbind f = some y ↔ ∃ (z : α) (H : z ∈ x), f z H = some y := by rcases x with (_|x) · simp · simp only [pbind] refine ⟨fun h ↦ ⟨x, rfl, h⟩, ?_⟩ rintro ⟨z, H, hz⟩ simp only [mem_def, Option.some_inj] at H simpa [H] using hz theorem join_pmap_eq_pmap_join {f : ∀ a, p a → β} {x : Option (Option α)} (H) : (pmap (pmap f) x H).join = pmap f x.join fun a h ↦ H (some a) (mem_of_mem_join h) _ rfl := by rcases x with (_ | _ | x) <;> simp /-- `simp`-normal form of `join_pmap_eq_pmap_join` -/ @[simp] theorem pmap_bind_id_eq_pmap_join {f : ∀ a, p a → β} {x : Option (Option α)} (H) : ((pmap (pmap f) x H).bind fun a ↦ a) = pmap f x.join fun a h ↦ H (some a) (mem_of_mem_join h) _ rfl := by rcases x with (_ | _ | x) <;> simp end pmap @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orElse' (a : α) (x : Option α) : (some a).orElse (fun _ ↦ x) = some a := rfl @[simp] theorem none_orElse' (x : Option α) : none.orElse (fun _ ↦ x) = x := by cases x <;> rfl @[simp] theorem orElse_none' (x : Option α) : x.orElse (fun _ ↦ none) = x := by cases x <;> rfl theorem exists_ne_none {p : Option α → Prop} : (∃ x ≠ none, p x) ↔ (∃ x : α, p x) := by simp only [← exists_prop, bex_ne_none] theorem iget_mem [Inhabited α] : ∀ {o : Option α}, isSome o → o.iget ∈ o | some _, _ => rfl theorem iget_of_mem [Inhabited α] {a : α} : ∀ {o : Option α}, a ∈ o → o.iget = a | _, rfl => rfl theorem getD_default_eq_iget [Inhabited α] (o : Option α) : o.getD default = o.iget := by cases o <;> rfl @[simp] theorem guard_eq_some' {p : Prop} [Decidable p] (u) : _root_.guard p = some u ↔ p := by cases u by_cases h : p <;> simp [_root_.guard, h] theorem liftOrGet_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, liftOrGet f o₁ o₂ = o₁ ∨ liftOrGet f o₁ o₂ = o₂ | none, none => Or.inl rfl | some _, none => Or.inl rfl | none, some _ => Or.inr rfl | some a, some b => by simpa [liftOrGet] using h a b /-- Given an element of `a : Option α`, a default element `b : β` and a function `α → β`, apply this function to `a` if it comes from `α`, and return `b` otherwise. -/ def casesOn' : Option α → β → (α → β) → β | none, n, _ => n | some a, _, s => s a @[simp] theorem casesOn'_none (x : β) (f : α → β) : casesOn' none x f = x := rfl @[simp] theorem casesOn'_some (x : β) (f : α → β) (a : α) : casesOn' (some a) x f = f a := rfl @[simp] theorem casesOn'_coe (x : β) (f : α → β) (a : α) : casesOn' (a : Option α) x f = f a := rfl @[simp] theorem casesOn'_none_coe (f : Option α → β) (o : Option α) : casesOn' o (f none) (f ∘ (fun a ↦ ↑a)) = f o := by cases o <;> rfl lemma casesOn'_eq_elim (b : β) (f : α → β) (a : Option α) : Option.casesOn' a b f = Option.elim a b f := by cases a <;> rfl theorem orElse_eq_some (o o' : Option α) (x : α) : (o <|> o') = some x ↔ o = some x ∨ o = none ∧ o' = some x := by cases o · simp only [true_and, false_or, eq_self_iff_true, none_orElse, reduceCtorEq] · simp only [some_orElse, or_false, false_and, reduceCtorEq] theorem orElse_eq_some' (o o' : Option α) (x : α) : o.orElse (fun _ ↦ o') = some x ↔ o = some x ∨ o = none ∧ o' = some x := Option.orElse_eq_some o o' x @[simp] theorem orElse_eq_none (o o' : Option α) : (o <|> o') = none ↔ o = none ∧ o' = none := by cases o · simp only [true_and, none_orElse, eq_self_iff_true] · simp only [some_orElse, reduceCtorEq, false_and] @[simp] theorem orElse_eq_none' (o o' : Option α) : o.orElse (fun _ ↦ o') = none ↔ o = none ∧ o' = none := Option.orElse_eq_none o o' section theorem choice_eq_none (α : Type*) [IsEmpty α] : choice α = none := dif_neg (not_nonempty_iff_imp_false.mpr isEmptyElim) end @[simp] theorem elim_none_some (f : Option α → β) (i : Option α) : i.elim (f none) (f ∘ some) = f i := by cases i <;> rfl theorem elim_comp (h : α → β) {f : γ → α} {x : α} {i : Option γ} : (i.elim (h x) fun j => h (f j)) = h (i.elim x f) := by cases i <;> rfl theorem elim_comp₂ (h : α → β → γ) {f : γ → α} {x : α} {g : γ → β} {y : β} {i : Option γ} : (i.elim (h x y) fun j => h (f j) (g j)) = h (i.elim x f) (i.elim y g) := by cases i <;> rfl theorem elim_apply {f : γ → α → β} {x : α → β} {i : Option γ} {y : α} : i.elim x f y = i.elim (x y) fun j => f j y := by rw [elim_comp fun f : α → β => f y] @[simp] lemma bnot_isSome (a : Option α) : (! a.isSome) = a.isNone := by cases a <;> simp @[simp] lemma bnot_comp_isSome : (! ·) ∘ @Option.isSome α = Option.isNone := by funext simp @[simp] lemma bnot_isNone (a : Option α) : (! a.isNone) = a.isSome := by cases a <;> simp @[simp] lemma bnot_comp_isNone : (! ·) ∘ @Option.isNone α = Option.isSome := by funext x simp @[simp] lemma isNone_eq_false_iff (a : Option α) : Option.isNone a = false ↔ Option.isSome a := by cases a <;> simp lemma eq_none_or_eq_some (a : Option α) : a = none ∨ ∃ x, a = some x := Option.exists.mp exists_eq' lemma eq_none_iff_forall_some_ne {o : Option α} : o = none ↔ ∀ a : α, some a ≠ o := by
apply not_iff_not.1 simpa only [not_forall, not_not] using Option.ne_none_iff_exists
Mathlib/Data/Option/Basic.lean
326
327
/- 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.Finset.Card import Mathlib.Data.Fintype.Basic /-! # Cardinalities of finite types This file defines the cardinality `Fintype.card α` as the number of elements in `(univ : Finset α)`. We also include some elementary results on the values of `Fintype.card` on specific types. ## Main declarations * `Fintype.card α`: Cardinality of a fintype. Equal to `Finset.univ.card`. * `Finite.surjective_of_injective`: an injective function from a finite type to itself is also surjective. -/ assert_not_exists Monoid open Function universe u v variable {α β γ : Type*} open Finset Function namespace Fintype /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [Fintype α] : ℕ := (@univ α _).card theorem subtype_card {p : α → Prop} (s : Finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card { x // p x } (Fintype.subtype s H) = #s := Multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : Finset α) (H : ∀ x : α, x ∈ s ↔ p x) [Fintype { x // p x }] : card { x // p x } = #s := by rw [← subtype_card s H] congr! @[simp] theorem card_ofFinset {p : Set α} (s : Finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @Fintype.card p (ofFinset s H) = #s := Fintype.subtype_card s H theorem card_of_finset' {p : Set α} (s : Finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [Fintype p] : Fintype.card p = #s := by rw [← card_ofFinset s H]; congr! end Fintype namespace Fintype theorem ofEquiv_card [Fintype α] (f : α ≃ β) : @card β (ofEquiv α f) = card α := Multiset.card_map _ _ theorem card_congr {α β} [Fintype α] [Fintype β] (f : α ≃ β) : card α = card β := by rw [← ofEquiv_card f]; congr! @[congr] theorem card_congr' {α β} [Fintype α] [Fintype β] (h : α = β) : card α = card β := card_congr (by rw [h]) /-- Note: this lemma is specifically about `Fintype.ofSubsingleton`. For a statement about arbitrary `Fintype` instances, use either `Fintype.card_le_one_iff_subsingleton` or `Fintype.card_unique`. -/ theorem card_ofSubsingleton (a : α) [Subsingleton α] : @Fintype.card _ (ofSubsingleton a) = 1 := rfl @[simp] theorem card_unique [Unique α] [h : Fintype α] : Fintype.card α = 1 := Subsingleton.elim (ofSubsingleton default) h ▸ card_ofSubsingleton _ /-- Note: this lemma is specifically about `Fintype.ofIsEmpty`. For a statement about arbitrary `Fintype` instances, use `Fintype.card_eq_zero`. -/ theorem card_ofIsEmpty [IsEmpty α] : @Fintype.card α Fintype.ofIsEmpty = 0 := rfl end Fintype namespace Set variable {s t : Set α} -- We use an arbitrary `[Fintype s]` instance here, -- not necessarily coming from a `[Fintype α]`. @[simp] theorem toFinset_card {α : Type*} (s : Set α) [Fintype s] : s.toFinset.card = Fintype.card s := Multiset.card_map Subtype.val Finset.univ.val end Set @[simp] theorem Finset.card_univ [Fintype α] : #(univ : Finset α) = Fintype.card α := rfl theorem Finset.eq_univ_of_card [Fintype α] (s : Finset α) (hs : #s = Fintype.card α) : s = univ := eq_of_subset_of_card_le (subset_univ _) <| by rw [hs, Finset.card_univ] theorem Finset.card_eq_iff_eq_univ [Fintype α] (s : Finset α) : #s = Fintype.card α ↔ s = univ := ⟨s.eq_univ_of_card, by rintro rfl exact Finset.card_univ⟩ theorem Finset.card_le_univ [Fintype α] (s : Finset α) : #s ≤ Fintype.card α := card_le_card (subset_univ s) theorem Finset.card_lt_univ_of_not_mem [Fintype α] {s : Finset α} {x : α} (hx : x ∉ s) : #s < Fintype.card α := card_lt_card ⟨subset_univ s, not_forall.2 ⟨x, fun hx' => hx (hx' <| mem_univ x)⟩⟩ theorem Finset.card_lt_iff_ne_univ [Fintype α] (s : Finset α) : #s < Fintype.card α ↔ s ≠ Finset.univ := s.card_le_univ.lt_iff_ne.trans (not_congr s.card_eq_iff_eq_univ) theorem Finset.card_compl_lt_iff_nonempty [Fintype α] [DecidableEq α] (s : Finset α) : #sᶜ < Fintype.card α ↔ s.Nonempty := sᶜ.card_lt_iff_ne_univ.trans s.compl_ne_univ_iff_nonempty theorem Finset.card_univ_diff [DecidableEq α] [Fintype α] (s : Finset α) : #(univ \ s) = Fintype.card α - #s := Finset.card_sdiff (subset_univ s) theorem Finset.card_compl [DecidableEq α] [Fintype α] (s : Finset α) : #sᶜ = Fintype.card α - #s := Finset.card_univ_diff s @[simp] theorem Finset.card_add_card_compl [DecidableEq α] [Fintype α] (s : Finset α) : #s + #sᶜ = Fintype.card α := by rw [Finset.card_compl, ← Nat.add_sub_assoc (card_le_univ s), Nat.add_sub_cancel_left] @[simp] theorem Finset.card_compl_add_card [DecidableEq α] [Fintype α] (s : Finset α) : #sᶜ + #s = Fintype.card α := by rw [Nat.add_comm, card_add_card_compl] theorem Fintype.card_compl_set [Fintype α] (s : Set α) [Fintype s] [Fintype (↥sᶜ : Sort _)] : Fintype.card (↥sᶜ : Sort _) = Fintype.card α - Fintype.card s := by classical rw [← Set.toFinset_card, ← Set.toFinset_card, ← Finset.card_compl, Set.toFinset_compl] theorem Fintype.card_subtype_eq (y : α) [Fintype { x // x = y }] : Fintype.card { x // x = y } = 1 := Fintype.card_unique theorem Fintype.card_subtype_eq' (y : α) [Fintype { x // y = x }] : Fintype.card { x // y = x } = 1 := Fintype.card_unique theorem Fintype.card_empty : Fintype.card Empty = 0 := rfl theorem Fintype.card_pempty : Fintype.card PEmpty = 0 := rfl theorem Fintype.card_unit : Fintype.card Unit = 1 := rfl @[simp] theorem Fintype.card_punit : Fintype.card PUnit = 1 := rfl @[simp] theorem Fintype.card_bool : Fintype.card Bool = 2 := rfl @[simp] theorem Fintype.card_ulift (α : Type*) [Fintype α] : Fintype.card (ULift α) = Fintype.card α := Fintype.ofEquiv_card _ @[simp] theorem Fintype.card_plift (α : Type*) [Fintype α] : Fintype.card (PLift α) = Fintype.card α := Fintype.ofEquiv_card _ @[simp] theorem Fintype.card_orderDual (α : Type*) [Fintype α] : Fintype.card αᵒᵈ = Fintype.card α := rfl @[simp] theorem Fintype.card_lex (α : Type*) [Fintype α] : Fintype.card (Lex α) = Fintype.card α := rfl -- Note: The extra hypothesis `h` is there so that the rewrite lemma applies, -- no matter what instance of `Fintype (Set.univ : Set α)` is used. @[simp] theorem Fintype.card_setUniv [Fintype α] {h : Fintype (Set.univ : Set α)} : Fintype.card (Set.univ : Set α) = Fintype.card α := by apply Fintype.card_of_finset' simp @[simp] theorem Fintype.card_subtype_true [Fintype α] {h : Fintype {_a : α // True}} : @Fintype.card {_a // True} h = Fintype.card α := by apply Fintype.card_of_subtype simp /-- Given that `α ⊕ β` is a fintype, `α` is also a fintype. This is non-computable as it uses that `Sum.inl` is an injection, but there's no clear inverse if `α` is empty. -/ noncomputable def Fintype.sumLeft {α β} [Fintype (α ⊕ β)] : Fintype α := Fintype.ofInjective (Sum.inl : α → α ⊕ β) Sum.inl_injective /-- Given that `α ⊕ β` is a fintype, `β` is also a fintype. This is non-computable as it uses that `Sum.inr` is an injection, but there's no clear inverse if `β` is empty. -/ noncomputable def Fintype.sumRight {α β} [Fintype (α ⊕ β)] : Fintype β := Fintype.ofInjective (Sum.inr : β → α ⊕ β) Sum.inr_injective theorem Finite.exists_univ_list (α) [Finite α] : ∃ l : List α, l.Nodup ∧ ∀ x : α, x ∈ l := by cases nonempty_fintype α obtain ⟨l, e⟩ := Quotient.exists_rep (@univ α _).1 have := And.intro (@univ α _).2 (@mem_univ_val α _) exact ⟨_, by rwa [← e] at this⟩ theorem List.Nodup.length_le_card {α : Type*} [Fintype α] {l : List α} (h : l.Nodup) : l.length ≤ Fintype.card α := by classical exact List.toFinset_card_of_nodup h ▸ l.toFinset.card_le_univ namespace Fintype variable [Fintype α] [Fintype β] theorem card_le_of_injective (f : α → β) (hf : Function.Injective f) : card α ≤ card β := Finset.card_le_card_of_injOn f (fun _ _ => Finset.mem_univ _) fun _ _ _ _ h => hf h theorem card_le_of_embedding (f : α ↪ β) : card α ≤ card β := card_le_of_injective f f.2 theorem card_lt_of_injective_of_not_mem (f : α → β) (h : Function.Injective f) {b : β} (w : b ∉ Set.range f) : card α < card β := calc card α = (univ.map ⟨f, h⟩).card := (card_map _).symm _ < card β := Finset.card_lt_univ_of_not_mem (x := b) <| by rwa [← mem_coe, coe_map, coe_univ, Set.image_univ] theorem card_lt_of_injective_not_surjective (f : α → β) (h : Function.Injective f) (h' : ¬Function.Surjective f) : card α < card β := let ⟨_y, hy⟩ := not_forall.1 h' card_lt_of_injective_of_not_mem f h hy theorem card_le_of_surjective (f : α → β) (h : Function.Surjective f) : card β ≤ card α := card_le_of_injective _ (Function.injective_surjInv h) theorem card_range_le {α β : Type*} (f : α → β) [Fintype α] [Fintype (Set.range f)] : Fintype.card (Set.range f) ≤ Fintype.card α := Fintype.card_le_of_surjective (fun a => ⟨f a, by simp⟩) fun ⟨_, a, ha⟩ => ⟨a, by simpa using ha⟩ theorem card_range {α β F : Type*} [FunLike F α β] [EmbeddingLike F α β] (f : F) [Fintype α] [Fintype (Set.range f)] : Fintype.card (Set.range f) = Fintype.card α := Eq.symm <| Fintype.card_congr <| Equiv.ofInjective _ <| EmbeddingLike.injective f theorem card_eq_zero_iff : card α = 0 ↔ IsEmpty α := by rw [card, Finset.card_eq_zero, univ_eq_empty_iff] @[simp] theorem card_eq_zero [IsEmpty α] : card α = 0 := card_eq_zero_iff.2 ‹_› alias card_of_isEmpty := card_eq_zero /-- A `Fintype` with cardinality zero is equivalent to `Empty`. -/ def cardEqZeroEquivEquivEmpty : card α = 0 ≃ (α ≃ Empty) := (Equiv.ofIff card_eq_zero_iff).trans (Equiv.equivEmptyEquiv α).symm theorem card_pos_iff : 0 < card α ↔ Nonempty α := Nat.pos_iff_ne_zero.trans <| not_iff_comm.mp <| not_nonempty_iff.trans card_eq_zero_iff.symm theorem card_pos [h : Nonempty α] : 0 < card α := card_pos_iff.mpr h @[simp] theorem card_ne_zero [Nonempty α] : card α ≠ 0 := _root_.ne_of_gt card_pos instance [Nonempty α] : NeZero (card α) := ⟨card_ne_zero⟩ theorem existsUnique_iff_card_one {α} [Fintype α] (p : α → Prop) [DecidablePred p] : (∃! a : α, p a) ↔ #{x | p x} = 1 := by rw [Finset.card_eq_one] refine exists_congr fun x => ?_ simp only [forall_true_left, Subset.antisymm_iff, subset_singleton_iff', singleton_subset_iff, true_and, and_comm, mem_univ, mem_filter] @[deprecated (since := "2024-12-17")] alias exists_unique_iff_card_one := existsUnique_iff_card_one nonrec theorem two_lt_card_iff : 2 < card α ↔ ∃ a b c : α, a ≠ b ∧ a ≠ c ∧ b ≠ c := by simp_rw [← Finset.card_univ, two_lt_card_iff, mem_univ, true_and] theorem card_of_bijective {f : α → β} (hf : Bijective f) : card α = card β := card_congr (Equiv.ofBijective f hf) end Fintype namespace Finite variable [Finite α] theorem surjective_of_injective {f : α → α} (hinj : Injective f) : Surjective f := by intro x have := Classical.propDecidable cases nonempty_fintype α have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_rfl) have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ x obtain ⟨y, h⟩ := mem_image.1 h₂ exact ⟨y, h.2⟩ theorem injective_iff_surjective {f : α → α} : Injective f ↔ Surjective f := ⟨surjective_of_injective, fun hsurj => HasLeftInverse.injective ⟨surjInv hsurj, leftInverse_of_surjective_of_rightInverse (surjective_of_injective (injective_surjInv _)) (rightInverse_surjInv _)⟩⟩ theorem injective_iff_bijective {f : α → α} : Injective f ↔ Bijective f := by simp [Bijective, injective_iff_surjective] theorem surjective_iff_bijective {f : α → α} : Surjective f ↔ Bijective f := by simp [Bijective, injective_iff_surjective] theorem injective_iff_surjective_of_equiv {f : α → β} (e : α ≃ β) : Injective f ↔ Surjective f := have : Injective (e.symm ∘ f) ↔ Surjective (e.symm ∘ f) := injective_iff_surjective ⟨fun hinj => by simpa [Function.comp] using e.surjective.comp (this.1 (e.symm.injective.comp hinj)), fun hsurj => by simpa [Function.comp] using e.injective.comp (this.2 (e.symm.surjective.comp hsurj))⟩ alias ⟨_root_.Function.Injective.bijective_of_finite, _⟩ := injective_iff_bijective alias ⟨_root_.Function.Surjective.bijective_of_finite, _⟩ := surjective_iff_bijective alias ⟨_root_.Function.Injective.surjective_of_fintype, _root_.Function.Surjective.injective_of_fintype⟩ := injective_iff_surjective_of_equiv end Finite @[simp] theorem Fintype.card_coe (s : Finset α) [Fintype s] : Fintype.card s = #s := @Fintype.card_of_finset' _ _ _ (fun _ => Iff.rfl) (id _) /-- We can inflate a set `s` to any bigger size. -/ lemma Finset.exists_superset_card_eq [Fintype α] {n : ℕ} {s : Finset α} (hsn : #s ≤ n) (hnα : n ≤ Fintype.card α) : ∃ t, s ⊆ t ∧ #t = n := by simpa using exists_subsuperset_card_eq s.subset_univ hsn hnα @[simp] theorem Fintype.card_prop : Fintype.card Prop = 2 := rfl theorem set_fintype_card_le_univ [Fintype α] (s : Set α) [Fintype s] : Fintype.card s ≤ Fintype.card α := Fintype.card_le_of_embedding (Function.Embedding.subtype s) theorem set_fintype_card_eq_univ_iff [Fintype α] (s : Set α) [Fintype s] : Fintype.card s = Fintype.card α ↔ s = Set.univ := by rw [← Set.toFinset_card, Finset.card_eq_iff_eq_univ, ← Set.toFinset_univ, Set.toFinset_inj] theorem Fintype.card_subtype_le [Fintype α] (p : α → Prop) [Fintype {a // p a}] : Fintype.card { x // p x } ≤ Fintype.card α := Fintype.card_le_of_embedding (Function.Embedding.subtype _) lemma Fintype.card_subtype_lt [Fintype α] {p : α → Prop} [Fintype {a // p a}] {x : α} (hx : ¬p x) : Fintype.card { x // p x } < Fintype.card α := Fintype.card_lt_of_injective_of_not_mem (b := x) (↑) Subtype.coe_injective <| by rwa [Subtype.range_coe_subtype] theorem Fintype.card_subtype [Fintype α] (p : α → Prop) [Fintype {a // p a}] [DecidablePred p] : Fintype.card { x // p x } = #{x | p x} := by refine Fintype.card_of_subtype _ ?_ simp @[simp] theorem Fintype.card_subtype_compl [Fintype α] (p : α → Prop) [Fintype { x // p x }] [Fintype { x // ¬p x }] : Fintype.card { x // ¬p x } = Fintype.card α - Fintype.card { x // p x } := by classical rw [Fintype.card_of_subtype (Set.toFinset { x | p x }ᶜ), Set.toFinset_compl, Finset.card_compl, Fintype.card_of_subtype] <;> · intro simp only [Set.mem_toFinset, Set.mem_compl_iff, Set.mem_setOf] theorem Fintype.card_subtype_mono (p q : α → Prop) (h : p ≤ q) [Fintype { x // p x }] [Fintype { x // q x }] : Fintype.card { x // p x } ≤ Fintype.card { x // q x } := Fintype.card_le_of_embedding (Subtype.impEmbedding _ _ h) /-- If two subtypes of a fintype have equal cardinality, so do their complements. -/ theorem Fintype.card_compl_eq_card_compl [Finite α] (p q : α → Prop) [Fintype { x // p x }] [Fintype { x // ¬p x }] [Fintype { x // q x }] [Fintype { x // ¬q x }] (h : Fintype.card { x // p x } = Fintype.card { x // q x }) : Fintype.card { x // ¬p x } = Fintype.card { x // ¬q x } := by cases nonempty_fintype α simp only [Fintype.card_subtype_compl, h] theorem Fintype.card_quotient_le [Fintype α] (s : Setoid α) [DecidableRel ((· ≈ ·) : α → α → Prop)] : Fintype.card (Quotient s) ≤ Fintype.card α := Fintype.card_le_of_surjective _ Quotient.mk'_surjective theorem univ_eq_singleton_of_card_one {α} [Fintype α] (x : α) (h : Fintype.card α = 1) : (univ : Finset α) = {x} := by symm apply eq_of_subset_of_card_le (subset_univ {x}) apply le_of_eq simp [h, Finset.card_univ] namespace Finite variable [Finite α] theorem wellFounded_of_trans_of_irrefl (r : α → α → Prop) [IsTrans α r] [IsIrrefl α r] : WellFounded r := by classical cases nonempty_fintype α have (x y) (hxy : r x y) : #{z | r z x} < #{z | r z y} := Finset.card_lt_card <| by simp only [Finset.lt_iff_ssubset.symm, lt_iff_le_not_le, Finset.le_iff_subset, Finset.subset_iff, mem_filter, true_and, mem_univ, hxy] exact ⟨fun z hzx => _root_.trans hzx hxy, not_forall_of_exists_not ⟨x, Classical.not_imp.2 ⟨hxy, irrefl x⟩⟩⟩ exact Subrelation.wf (this _ _) (measure _).wf -- See note [lower instance priority] instance (priority := 100) to_wellFoundedLT [Preorder α] : WellFoundedLT α := ⟨wellFounded_of_trans_of_irrefl _⟩ -- See note [lower instance priority] instance (priority := 100) to_wellFoundedGT [Preorder α] : WellFoundedGT α := ⟨wellFounded_of_trans_of_irrefl _⟩ end Finite -- Shortcut instances to make sure those are found even in the presence of other instances -- See https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/WellFoundedLT.20Prop.20is.20not.20found.20when.20importing.20too.20much instance Bool.instWellFoundedLT : WellFoundedLT Bool := inferInstance instance Bool.instWellFoundedGT : WellFoundedGT Bool := inferInstance instance Prop.instWellFoundedLT : WellFoundedLT Prop := inferInstance instance Prop.instWellFoundedGT : WellFoundedGT Prop := inferInstance section Trunc /-- A `Fintype` with positive cardinality constructively contains an element. -/ def truncOfCardPos {α} [Fintype α] (h : 0 < Fintype.card α) : Trunc α := letI := Fintype.card_pos_iff.mp h truncOfNonemptyFintype α end Trunc /-- A custom induction principle for fintypes. The base case is a subsingleton type, and the induction step is for non-trivial types, and one can assume the hypothesis for smaller types (via `Fintype.card`). The major premise is `Fintype α`, so to use this with the `induction` tactic you have to give a name to that instance and use that name. -/ @[elab_as_elim] theorem Fintype.induction_subsingleton_or_nontrivial {P : ∀ (α) [Fintype α], Prop} (α : Type*) [Fintype α] (hbase : ∀ (α) [Fintype α] [Subsingleton α], P α) (hstep : ∀ (α) [Fintype α] [Nontrivial α], (∀ (β) [Fintype β], Fintype.card β < Fintype.card α → P β) → P α) : P α := by obtain ⟨n, hn⟩ : ∃ n, Fintype.card α = n := ⟨Fintype.card α, rfl⟩ induction' n using Nat.strong_induction_on with n ih generalizing α rcases subsingleton_or_nontrivial α with hsing | hnontriv · apply hbase · apply hstep intro β _ hlt rw [hn] at hlt exact ih (Fintype.card β) hlt _ rfl section Fin @[simp] theorem Fintype.card_fin (n : ℕ) : Fintype.card (Fin n) = n := List.length_finRange theorem Fintype.card_fin_lt_of_le {m n : ℕ} (h : m ≤ n) : Fintype.card {i : Fin n // i < m} = m := by conv_rhs => rw [← Fintype.card_fin m] apply Fintype.card_congr exact { toFun := fun ⟨⟨i, _⟩, hi⟩ ↦ ⟨i, hi⟩ invFun := fun ⟨i, hi⟩ ↦ ⟨⟨i, lt_of_lt_of_le hi h⟩, hi⟩ left_inv := fun i ↦ rfl right_inv := fun i ↦ rfl } theorem Finset.card_fin (n : ℕ) : #(univ : Finset (Fin n)) = n := by simp /-- `Fin` as a map from `ℕ` to `Type` is injective. Note that since this is a statement about equality of types, using it should be avoided if possible. -/ theorem fin_injective : Function.Injective Fin := fun m n h => (Fintype.card_fin m).symm.trans <| (Fintype.card_congr <| Equiv.cast h).trans (Fintype.card_fin n) theorem Fin.val_eq_val_of_heq {k l : ℕ} {i : Fin k} {j : Fin l} (h : HEq i j) : (i : ℕ) = (j : ℕ) := (Fin.heq_ext_iff (fin_injective (type_eq_of_heq h))).1 h /-- A reversed version of `Fin.cast_eq_cast` that is easier to rewrite with. -/ theorem Fin.cast_eq_cast' {n m : ℕ} (h : Fin n = Fin m) : _root_.cast h = Fin.cast (fin_injective h) := by cases fin_injective h rfl theorem card_finset_fin_le {n : ℕ} (s : Finset (Fin n)) : #s ≤ n := by simpa only [Fintype.card_fin] using s.card_le_univ end Fin
Mathlib/Data/Fintype/Card.lean
528
534
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Polynomial.Degree.Support import Mathlib.Data.ENat.Basic /-! # Trailing degree of univariate polynomials ## Main definitions * `trailingDegree p`: the multiplicity of `X` in the polynomial `p` * `natTrailingDegree`: a variant of `trailingDegree` that takes values in the natural numbers * `trailingCoeff`: the coefficient at index `natTrailingDegree p` Converts most results about `degree`, `natDegree` and `leadingCoeff` to results about the bottom end of a polynomial -/ noncomputable section open Function Polynomial Finsupp Finset open scoped Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b : R} {n m : ℕ} section Semiring variable [Semiring R] {p q r : R[X]} /-- `trailingDegree p` is the multiplicity of `x` in the polynomial `p`, i.e. the smallest `X`-exponent in `p`. `trailingDegree p = some n` when `p ≠ 0` and `n` is the smallest power of `X` that appears in `p`, otherwise `trailingDegree 0 = ⊤`. -/ def trailingDegree (p : R[X]) : ℕ∞ := p.support.min theorem trailingDegree_lt_wf : WellFounded fun p q : R[X] => trailingDegree p < trailingDegree q := InvImage.wf trailingDegree wellFounded_lt /-- `natTrailingDegree p` forces `trailingDegree p` to `ℕ`, by defining `natTrailingDegree ⊤ = 0`. -/ def natTrailingDegree (p : R[X]) : ℕ := ENat.toNat (trailingDegree p) /-- `trailingCoeff p` gives the coefficient of the smallest power of `X` in `p`. -/ def trailingCoeff (p : R[X]) : R := coeff p (natTrailingDegree p) /-- a polynomial is `monic_at` if its trailing coefficient is 1 -/ def TrailingMonic (p : R[X]) := trailingCoeff p = (1 : R) theorem TrailingMonic.def : TrailingMonic p ↔ trailingCoeff p = 1 := Iff.rfl instance TrailingMonic.decidable [DecidableEq R] : Decidable (TrailingMonic p) := inferInstanceAs <| Decidable (trailingCoeff p = (1 : R)) @[simp] theorem TrailingMonic.trailingCoeff {p : R[X]} (hp : p.TrailingMonic) : trailingCoeff p = 1 := hp @[simp] theorem trailingDegree_zero : trailingDegree (0 : R[X]) = ⊤ := rfl @[simp] theorem trailingCoeff_zero : trailingCoeff (0 : R[X]) = 0 := rfl @[simp] theorem natTrailingDegree_zero : natTrailingDegree (0 : R[X]) = 0 := rfl @[simp] theorem trailingDegree_eq_top : trailingDegree p = ⊤ ↔ p = 0 := ⟨fun h => support_eq_empty.1 (Finset.min_eq_top.1 h), fun h => by simp [h]⟩ theorem trailingDegree_eq_natTrailingDegree (hp : p ≠ 0) : trailingDegree p = (natTrailingDegree p : ℕ∞) := .symm <| ENat.coe_toNat <| mt trailingDegree_eq_top.1 hp theorem trailingDegree_eq_iff_natTrailingDegree_eq {p : R[X]} {n : ℕ} (hp : p ≠ 0) : p.trailingDegree = n ↔ p.natTrailingDegree = n := by rw [trailingDegree_eq_natTrailingDegree hp, Nat.cast_inj] theorem trailingDegree_eq_iff_natTrailingDegree_eq_of_pos {p : R[X]} {n : ℕ} (hn : n ≠ 0) : p.trailingDegree = n ↔ p.natTrailingDegree = n := by rw [natTrailingDegree, ENat.toNat_eq_iff hn] theorem natTrailingDegree_eq_of_trailingDegree_eq_some {p : R[X]} {n : ℕ} (h : trailingDegree p = n) : natTrailingDegree p = n := by simp [natTrailingDegree, h] @[simp] theorem natTrailingDegree_le_trailingDegree : ↑(natTrailingDegree p) ≤ trailingDegree p := ENat.coe_toNat_le_self _ theorem natTrailingDegree_eq_of_trailingDegree_eq [Semiring S] {q : S[X]} (h : trailingDegree p = trailingDegree q) : natTrailingDegree p = natTrailingDegree q := by unfold natTrailingDegree rw [h] theorem trailingDegree_le_of_ne_zero (h : coeff p n ≠ 0) : trailingDegree p ≤ n := min_le (mem_support_iff.2 h) theorem natTrailingDegree_le_of_ne_zero (h : coeff p n ≠ 0) : natTrailingDegree p ≤ n := ENat.toNat_le_of_le_coe <| trailingDegree_le_of_ne_zero h @[simp] lemma coeff_natTrailingDegree_eq_zero : coeff p p.natTrailingDegree = 0 ↔ p = 0 := by constructor · rintro h by_contra hp obtain ⟨n, hpn, hn⟩ := by simpa using min_mem_image_coe <| support_nonempty.2 hp obtain rfl := (trailingDegree_eq_iff_natTrailingDegree_eq hp).1 hn.symm exact hpn h · rintro rfl simp lemma coeff_natTrailingDegree_ne_zero : coeff p p.natTrailingDegree ≠ 0 ↔ p ≠ 0 := coeff_natTrailingDegree_eq_zero.not @[simp] lemma trailingDegree_eq_zero : trailingDegree p = 0 ↔ coeff p 0 ≠ 0 := Finset.min_eq_bot.trans mem_support_iff @[simp] lemma natTrailingDegree_eq_zero : natTrailingDegree p = 0 ↔ p = 0 ∨ coeff p 0 ≠ 0 := by simp [natTrailingDegree, or_comm] lemma natTrailingDegree_ne_zero : natTrailingDegree p ≠ 0 ↔ p ≠ 0 ∧ coeff p 0 = 0 := natTrailingDegree_eq_zero.not.trans <| by rw [not_or, not_ne_iff]
lemma trailingDegree_ne_zero : trailingDegree p ≠ 0 ↔ coeff p 0 = 0 := trailingDegree_eq_zero.not_left @[simp] theorem trailingDegree_le_trailingDegree (h : coeff q (natTrailingDegree p) ≠ 0) : trailingDegree q ≤ trailingDegree p := (trailingDegree_le_of_ne_zero h).trans natTrailingDegree_le_trailingDegree theorem trailingDegree_ne_of_natTrailingDegree_ne {n : ℕ} :
Mathlib/Algebra/Polynomial/Degree/TrailingDegree.lean
142
150
/- Copyright (c) 2022 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Heather Macbeth -/ import Mathlib.Data.Nat.Cast.WithTop import Mathlib.FieldTheory.IsAlgClosed.Basic import Mathlib.RingTheory.WittVector.DiscreteValuationRing /-! # Solving equations about the Frobenius map on the field of fractions of `𝕎 k` The goal of this file is to prove `WittVector.exists_frobenius_solution_fractionRing`, which says that for an algebraically closed field `k` of characteristic `p` and `a, b` in the field of fractions of Witt vectors over `k`, there is a solution `b` to the equation `φ b * a = p ^ m * b`, where `φ` is the Frobenius map. Most of this file builds up the equivalent theorem over `𝕎 k` directly, moving to the field of fractions at the end. See `WittVector.frobeniusRotation` and its specification. The construction proceeds by recursively defining a sequence of coefficients as solutions to a polynomial equation in `k`. We must define these as generic polynomials using Witt vector API (`WittVector.wittMul`, `wittPolynomial`) to show that they satisfy the desired equation. Preliminary work is done in the dependency `RingTheory.WittVector.MulCoeff` to isolate the `n+1`st coefficients of `x` and `y` in the `n+1`st coefficient of `x*y`. This construction is described in Dupuis, Lewis, and Macbeth, [Formalized functional analysis via semilinear maps][dupuis-lewis-macbeth2022]. We approximately follow an approach sketched on MathOverflow: <https://mathoverflow.net/questions/62468/about-frobenius-of-witt-vectors> The result is a dependency for the proof of `WittVector.isocrystal_classification`, the classification of one-dimensional isocrystals over an algebraically closed field. -/ noncomputable section namespace WittVector variable (p : ℕ) [hp : Fact p.Prime] local notation "𝕎" => WittVector p namespace RecursionMain /-! ## The recursive case of the vector coefficients The first coefficient of our solution vector is easy to define below. In this section we focus on the recursive case. The goal is to turn `WittVector.wittPolyProd n` into a univariate polynomial whose variable represents the `n`th coefficient of `x` in `x * a`. -/ section CommRing variable {k : Type*} [CommRing k] [CharP k p] open Polynomial /-- The root of this polynomial determines the `n+1`st coefficient of our solution. -/ def succNthDefiningPoly (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : Fin (n + 1) → k) : Polynomial k := X ^ p * C (a₁.coeff 0 ^ p ^ (n + 1)) - X * C (a₂.coeff 0 ^ p ^ (n + 1)) + C (a₁.coeff (n + 1) * (bs 0 ^ p) ^ p ^ (n + 1) + nthRemainder p n (fun v => bs v ^ p) (truncateFun (n + 1) a₁) - a₂.coeff (n + 1) * bs 0 ^ p ^ (n + 1) - nthRemainder p n bs (truncateFun (n + 1) a₂)) theorem succNthDefiningPoly_degree [IsDomain k] (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : Fin (n + 1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : (succNthDefiningPoly p n a₁ a₂ bs).degree = p := by have : (X ^ p * C (a₁.coeff 0 ^ p ^ (n + 1))).degree = (p : WithBot ℕ) := by rw [degree_mul, degree_C] · simp only [Nat.cast_withBot, add_zero, degree_X, degree_pow, Nat.smul_one_eq_cast] · exact pow_ne_zero _ ha₁ have : (X ^ p * C (a₁.coeff 0 ^ p ^ (n + 1)) - X * C (a₂.coeff 0 ^ p ^ (n + 1))).degree = (p : WithBot ℕ) := by rw [degree_sub_eq_left_of_degree_lt, this] rw [this, degree_mul, degree_C, degree_X, add_zero] · exact mod_cast hp.out.one_lt · exact pow_ne_zero _ ha₂ rw [succNthDefiningPoly, degree_add_eq_left_of_degree_lt, this] apply lt_of_le_of_lt degree_C_le rw [this] exact mod_cast hp.out.pos end CommRing section IsAlgClosed variable {k : Type*} [Field k] [CharP k p] [IsAlgClosed k] theorem root_exists (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : Fin (n + 1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : ∃ b : k, (succNthDefiningPoly p n a₁ a₂ bs).IsRoot b := IsAlgClosed.exists_root _ <| by simp only [succNthDefiningPoly_degree p n a₁ a₂ bs ha₁ ha₂, ne_eq, Nat.cast_eq_zero, hp.out.ne_zero, not_false_eq_true] /-- This is the `n+1`st coefficient of our solution, projected from `root_exists`. -/ def succNthVal (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : Fin (n + 1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : k := Classical.choose (root_exists p n a₁ a₂ bs ha₁ ha₂) theorem succNthVal_spec (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : Fin (n + 1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : (succNthDefiningPoly p n a₁ a₂ bs).IsRoot (succNthVal p n a₁ a₂ bs ha₁ ha₂) := Classical.choose_spec (root_exists p n a₁ a₂ bs ha₁ ha₂) theorem succNthVal_spec' (n : ℕ) (a₁ a₂ : 𝕎 k) (bs : Fin (n + 1) → k) (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : succNthVal p n a₁ a₂ bs ha₁ ha₂ ^ p * a₁.coeff 0 ^ p ^ (n + 1) + a₁.coeff (n + 1) * (bs 0 ^ p) ^ p ^ (n + 1) + nthRemainder p n (fun v => bs v ^ p) (truncateFun (n + 1) a₁) = succNthVal p n a₁ a₂ bs ha₁ ha₂ * a₂.coeff 0 ^ p ^ (n + 1) + a₂.coeff (n + 1) * bs 0 ^ p ^ (n + 1) + nthRemainder p n bs (truncateFun (n + 1) a₂) := by rw [← sub_eq_zero] have := succNthVal_spec p n a₁ a₂ bs ha₁ ha₂ simp only [Polynomial.map_add, Polynomial.eval_X, Polynomial.map_pow, Polynomial.eval_C, Polynomial.eval_pow, succNthDefiningPoly, Polynomial.eval_mul, Polynomial.eval_add, Polynomial.eval_sub, Polynomial.map_mul, Polynomial.map_sub, Polynomial.IsRoot.def] at this convert this using 1 ring end IsAlgClosed end RecursionMain namespace RecursionBase variable {k : Type*} [Field k] [IsAlgClosed k] theorem solution_pow (a₁ a₂ : 𝕎 k) : ∃ x : k, x ^ (p - 1) = a₂.coeff 0 / a₁.coeff 0 := IsAlgClosed.exists_pow_nat_eq _ <| tsub_pos_of_lt hp.out.one_lt /-- The base case (0th coefficient) of our solution vector. -/ def solution (a₁ a₂ : 𝕎 k) : k := Classical.choose <| solution_pow p a₁ a₂ theorem solution_spec (a₁ a₂ : 𝕎 k) : solution p a₁ a₂ ^ (p - 1) = a₂.coeff 0 / a₁.coeff 0 := Classical.choose_spec <| solution_pow p a₁ a₂ theorem solution_nonzero {a₁ a₂ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (ha₂ : a₂.coeff 0 ≠ 0) : solution p a₁ a₂ ≠ 0 := by intro h have := solution_spec p a₁ a₂ rw [h, zero_pow] at this · simpa [ha₁, ha₂] using _root_.div_eq_zero_iff.mp this.symm · exact Nat.sub_ne_zero_of_lt hp.out.one_lt theorem solution_spec' {a₁ : 𝕎 k} (ha₁ : a₁.coeff 0 ≠ 0) (a₂ : 𝕎 k) : solution p a₁ a₂ ^ p * a₁.coeff 0 = solution p a₁ a₂ * a₂.coeff 0 := by have := solution_spec p a₁ a₂ obtain ⟨q, hq⟩ := Nat.exists_eq_succ_of_ne_zero hp.out.ne_zero have hq' : q = p - 1 := by simp only [hq, tsub_zero, Nat.succ_sub_succ_eq_sub] conv_lhs => congr congr · skip · rw [hq] rw [pow_succ', hq', this] field_simp [ha₁, mul_comm]
end RecursionBase open RecursionMain RecursionBase section FrobeniusRotation section IsAlgClosed variable {k : Type*} [Field k] [CharP k p] [IsAlgClosed k] /-- Recursively defines the sequence of coefficients for `WittVector.frobeniusRotation`.
Mathlib/RingTheory/WittVector/FrobeniusFractionField.lean
171
182
/- 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.Data.Set.Lattice import Mathlib.Data.SetLike.Basic import Mathlib.Order.ModularLattice import Mathlib.Order.SuccPred.Basic import Mathlib.Order.WellFounded import Mathlib.Tactic.Nontriviality import Mathlib.Order.ConditionallyCompleteLattice.Indexed /-! # Atoms, Coatoms, and Simple Lattices This module defines atoms, which are minimal non-`⊥` elements in bounded lattices, simple lattices, which are lattices with only two elements, and related ideas. ## Main definitions ### Atoms and Coatoms * `IsAtom a` indicates that the only element below `a` is `⊥`. * `IsCoatom a` indicates that the only element above `a` is `⊤`. ### Atomic and Atomistic Lattices * `IsAtomic` indicates that every element other than `⊥` is above an atom. * `IsCoatomic` indicates that every element other than `⊤` is below a coatom. * `IsAtomistic` indicates that every element is the `sSup` of a set of atoms. * `IsCoatomistic` indicates that every element is the `sInf` of a set of coatoms. * `IsStronglyAtomic` indicates that for all `a < b`, there is some `x` with `a ⋖ x ≤ b`. * `IsStronglyCoatomic` indicates that for all `a < b`, there is some `x` with `a ≤ x ⋖ b`. ### Simple Lattices * `IsSimpleOrder` indicates that an order has only two unique elements, `⊥` and `⊤`. * `IsSimpleOrder.boundedOrder` * `IsSimpleOrder.distribLattice` * Given an instance of `IsSimpleOrder`, we provide the following definitions. These are not made global instances as they contain data : * `IsSimpleOrder.booleanAlgebra` * `IsSimpleOrder.completeLattice` * `IsSimpleOrder.completeBooleanAlgebra` ## Main results * `isAtom_dual_iff_isCoatom` and `isCoatom_dual_iff_isAtom` express the (definitional) duality of `IsAtom` and `IsCoatom`. * `isSimpleOrder_iff_isAtom_top` and `isSimpleOrder_iff_isCoatom_bot` express the connection between atoms, coatoms, and simple lattices * `IsCompl.isAtom_iff_isCoatom` and `IsCompl.isCoatom_if_isAtom`: In a modular bounded lattice, a complement of an atom is a coatom and vice versa. * `isAtomic_iff_isCoatomic`: A modular complemented lattice is atomic iff it is coatomic. -/ variable {ι : Sort*} {α β : Type*} section Atoms section IsAtom section Preorder variable [Preorder α] [OrderBot α] {a b x : α} /-- An atom of an `OrderBot` is an element with no other element between it and `⊥`, which is not `⊥`. -/ def IsAtom (a : α) : Prop := a ≠ ⊥ ∧ ∀ b, b < a → b = ⊥ theorem IsAtom.Iic (ha : IsAtom a) (hax : a ≤ x) : IsAtom (⟨a, hax⟩ : Set.Iic x) := ⟨fun con => ha.1 (Subtype.mk_eq_mk.1 con), fun ⟨b, _⟩ hba => Subtype.mk_eq_mk.2 (ha.2 b hba)⟩ theorem IsAtom.of_isAtom_coe_Iic {a : Set.Iic x} (ha : IsAtom a) : IsAtom (a : α) := ⟨fun con => ha.1 (Subtype.ext con), fun b hba => Subtype.mk_eq_mk.1 (ha.2 ⟨b, hba.le.trans a.prop⟩ hba)⟩ theorem isAtom_iff_le_of_ge : IsAtom a ↔ a ≠ ⊥ ∧ ∀ b ≠ ⊥, b ≤ a → a ≤ b := and_congr Iff.rfl <| forall_congr' fun b => by simp only [Ne, @not_imp_comm (b = ⊥), Classical.not_imp, lt_iff_le_not_le] end Preorder section PartialOrder variable [PartialOrder α] [OrderBot α] {a b x : α} theorem IsAtom.lt_iff (h : IsAtom a) : x < a ↔ x = ⊥ := ⟨h.2 x, fun hx => hx.symm ▸ h.1.bot_lt⟩ theorem IsAtom.le_iff (h : IsAtom a) : x ≤ a ↔ x = ⊥ ∨ x = a := by rw [le_iff_lt_or_eq, h.lt_iff] lemma IsAtom.bot_lt (h : IsAtom a) : ⊥ < a := h.lt_iff.mpr rfl lemma IsAtom.le_iff_eq (ha : IsAtom a) (hb : b ≠ ⊥) : b ≤ a ↔ b = a := ha.le_iff.trans <| or_iff_right hb theorem IsAtom.Iic_eq (h : IsAtom a) : Set.Iic a = {⊥, a} := Set.ext fun _ => h.le_iff @[simp] theorem bot_covBy_iff : ⊥ ⋖ a ↔ IsAtom a := by simp only [CovBy, bot_lt_iff_ne_bot, IsAtom, not_imp_not] alias ⟨CovBy.is_atom, IsAtom.bot_covBy⟩ := bot_covBy_iff end PartialOrder theorem atom_le_iSup [Order.Frame α] {a : α} (ha : IsAtom a) {f : ι → α} : a ≤ iSup f ↔ ∃ i, a ≤ f i := by refine ⟨?_, fun ⟨i, hi⟩ => le_trans hi (le_iSup _ _)⟩ show (a ≤ ⨆ i, f i) → _ refine fun h => of_not_not fun ha' => ?_ push_neg at ha' have ha'' : Disjoint a (⨆ i, f i) := disjoint_iSup_iff.2 fun i => fun x hxa hxf => le_bot_iff.2 <| of_not_not fun hx => have hxa : x < a := (le_iff_eq_or_lt.1 hxa).resolve_left (by rintro rfl; exact ha' _ hxf) hx (ha.2 _ hxa) obtain rfl := le_bot_iff.1 (ha'' le_rfl h) exact ha.1 rfl end IsAtom section IsCoatom section Preorder variable [Preorder α] /-- A coatom of an `OrderTop` is an element with no other element between it and `⊤`, which is not `⊤`. -/ def IsCoatom [OrderTop α] (a : α) : Prop := a ≠ ⊤ ∧ ∀ b, a < b → b = ⊤ @[simp] theorem isCoatom_dual_iff_isAtom [OrderBot α] {a : α} : IsCoatom (OrderDual.toDual a) ↔ IsAtom a := Iff.rfl @[simp] theorem isAtom_dual_iff_isCoatom [OrderTop α] {a : α} : IsAtom (OrderDual.toDual a) ↔ IsCoatom a := Iff.rfl alias ⟨_, IsAtom.dual⟩ := isCoatom_dual_iff_isAtom alias ⟨_, IsCoatom.dual⟩ := isAtom_dual_iff_isCoatom variable [OrderTop α] {a x : α} theorem IsCoatom.Ici (ha : IsCoatom a) (hax : x ≤ a) : IsCoatom (⟨a, hax⟩ : Set.Ici x) := ha.dual.Iic hax theorem IsCoatom.of_isCoatom_coe_Ici {a : Set.Ici x} (ha : IsCoatom a) : IsCoatom (a : α) := @IsAtom.of_isAtom_coe_Iic αᵒᵈ _ _ x a ha theorem isCoatom_iff_ge_of_le : IsCoatom a ↔ a ≠ ⊤ ∧ ∀ b ≠ ⊤, a ≤ b → b ≤ a := isAtom_iff_le_of_ge (α := αᵒᵈ) end Preorder section PartialOrder variable [PartialOrder α] [OrderTop α] {a b x : α} theorem IsCoatom.lt_iff (h : IsCoatom a) : a < x ↔ x = ⊤ := h.dual.lt_iff theorem IsCoatom.le_iff (h : IsCoatom a) : a ≤ x ↔ x = ⊤ ∨ x = a := h.dual.le_iff lemma IsCoatom.lt_top (h : IsCoatom a) : a < ⊤ := h.lt_iff.mpr rfl lemma IsCoatom.le_iff_eq (ha : IsCoatom a) (hb : b ≠ ⊤) : a ≤ b ↔ b = a := ha.dual.le_iff_eq hb theorem IsCoatom.Ici_eq (h : IsCoatom a) : Set.Ici a = {⊤, a} := h.dual.Iic_eq @[simp] theorem covBy_top_iff : a ⋖ ⊤ ↔ IsCoatom a := toDual_covBy_toDual_iff.symm.trans bot_covBy_iff alias ⟨CovBy.isCoatom, IsCoatom.covBy_top⟩ := covBy_top_iff namespace SetLike variable {A B : Type*} [SetLike A B] theorem isAtom_iff [OrderBot A] {K : A} : IsAtom K ↔ K ≠ ⊥ ∧ ∀ H g, H ≤ K → g ∉ H → g ∈ K → H = ⊥ := by simp_rw [IsAtom, lt_iff_le_not_le, SetLike.not_le_iff_exists, and_comm (a := _ ≤ _), and_imp, exists_imp, ← and_imp, and_comm] theorem isCoatom_iff [OrderTop A] {K : A} : IsCoatom K ↔ K ≠ ⊤ ∧ ∀ H g, K ≤ H → g ∉ K → g ∈ H → H = ⊤ := by simp_rw [IsCoatom, lt_iff_le_not_le, SetLike.not_le_iff_exists, and_comm (a := _ ≤ _), and_imp, exists_imp, ← and_imp, and_comm] theorem covBy_iff {K L : A} : K ⋖ L ↔ K < L ∧ ∀ H g, K ≤ H → H ≤ L → g ∉ K → g ∈ H → H = L := by refine and_congr_right fun _ ↦ forall_congr' fun H ↦ not_iff_not.mp ?_ push_neg rw [lt_iff_le_not_le, lt_iff_le_and_ne, and_and_and_comm] simp_rw [exists_and_left, and_assoc, and_congr_right_iff, ← and_assoc, and_comm, exists_and_left, SetLike.not_le_iff_exists, and_comm, implies_true] /-- Dual variant of `SetLike.covBy_iff` -/ theorem covBy_iff' {K L : A} : K ⋖ L ↔ K < L ∧ ∀ H g, K ≤ H → H ≤ L → g ∉ H → g ∈ L → H = K := by refine and_congr_right fun _ ↦ forall_congr' fun H ↦ not_iff_not.mp ?_ push_neg rw [lt_iff_le_and_ne, lt_iff_le_not_le, and_and_and_comm] simp_rw [exists_and_left, and_assoc, and_congr_right_iff, ← and_assoc, and_comm, exists_and_left, SetLike.not_le_iff_exists, ne_comm, implies_true] end SetLike end PartialOrder theorem iInf_le_coatom [Order.Coframe α] {a : α} (ha : IsCoatom a) {f : ι → α} : iInf f ≤ a ↔ ∃ i, f i ≤ a := atom_le_iSup (α := αᵒᵈ) ha end IsCoatom section PartialOrder variable [PartialOrder α] {a b : α} @[simp] theorem Set.Ici.isAtom_iff {b : Set.Ici a} : IsAtom b ↔ a ⋖ b := by rw [← bot_covBy_iff] refine (Set.OrdConnected.apply_covBy_apply_iff (OrderEmbedding.subtype fun c => a ≤ c) ?_).symm simpa only [OrderEmbedding.coe_subtype, Subtype.range_coe_subtype] using Set.ordConnected_Ici @[simp] theorem Set.Iic.isCoatom_iff {a : Set.Iic b} : IsCoatom a ↔ ↑a ⋖ b := by rw [← covBy_top_iff] refine (Set.OrdConnected.apply_covBy_apply_iff (OrderEmbedding.subtype fun c => c ≤ b) ?_).symm simpa only [OrderEmbedding.coe_subtype, Subtype.range_coe_subtype] using Set.ordConnected_Iic theorem covBy_iff_atom_Ici (h : a ≤ b) : a ⋖ b ↔ IsAtom (⟨b, h⟩ : Set.Ici a) := by simp theorem covBy_iff_coatom_Iic (h : a ≤ b) : a ⋖ b ↔ IsCoatom (⟨a, h⟩ : Set.Iic b) := by simp end PartialOrder section Pairwise theorem IsAtom.inf_eq_bot_of_ne [SemilatticeInf α] [OrderBot α] {a b : α} (ha : IsAtom a) (hb : IsAtom b) (hab : a ≠ b) : a ⊓ b = ⊥ := hab.not_le_or_not_le.elim (ha.lt_iff.1 ∘ inf_lt_left.2) (hb.lt_iff.1 ∘ inf_lt_right.2) theorem IsAtom.disjoint_of_ne [SemilatticeInf α] [OrderBot α] {a b : α} (ha : IsAtom a) (hb : IsAtom b) (hab : a ≠ b) : Disjoint a b := disjoint_iff.mpr (ha.inf_eq_bot_of_ne hb hab) theorem IsCoatom.sup_eq_top_of_ne [SemilatticeSup α] [OrderTop α] {a b : α} (ha : IsCoatom a) (hb : IsCoatom b) (hab : a ≠ b) : a ⊔ b = ⊤ := ha.dual.inf_eq_bot_of_ne hb.dual hab theorem IsCoatom.codisjoint_of_ne [SemilatticeSup α] [OrderTop α] {a b : α} (ha : IsCoatom a) (hb : IsCoatom b) (hab : a ≠ b) : Codisjoint a b := codisjoint_iff.mpr (ha.sup_eq_top_of_ne hb hab) end Pairwise end Atoms section Atomic variable [PartialOrder α] (α) /-- A lattice is atomic iff every element other than `⊥` has an atom below it. -/ @[mk_iff] class IsAtomic [OrderBot α] : Prop where /-- Every element other than `⊥` has an atom below it. -/ eq_bot_or_exists_atom_le : ∀ b : α, b = ⊥ ∨ ∃ a : α, IsAtom a ∧ a ≤ b /-- A lattice is coatomic iff every element other than `⊤` has a coatom above it. -/ @[mk_iff] class IsCoatomic [OrderTop α] : Prop where /-- Every element other than `⊤` has an atom above it. -/ eq_top_or_exists_le_coatom : ∀ b : α, b = ⊤ ∨ ∃ a : α, IsCoatom a ∧ b ≤ a export IsAtomic (eq_bot_or_exists_atom_le) export IsCoatomic (eq_top_or_exists_le_coatom) lemma IsAtomic.exists_atom [OrderBot α] [Nontrivial α] [IsAtomic α] : ∃ a : α, IsAtom a := have ⟨b, hb⟩ := exists_ne (⊥ : α) have ⟨a, ha⟩ := (eq_bot_or_exists_atom_le b).resolve_left hb ⟨a, ha.1⟩ lemma IsCoatomic.exists_coatom [OrderTop α] [Nontrivial α] [IsCoatomic α] : ∃ a : α, IsCoatom a := have ⟨b, hb⟩ := exists_ne (⊤ : α) have ⟨a, ha⟩ := (eq_top_or_exists_le_coatom b).resolve_left hb ⟨a, ha.1⟩ variable {α} @[simp] theorem isCoatomic_dual_iff_isAtomic [OrderBot α] : IsCoatomic αᵒᵈ ↔ IsAtomic α := ⟨fun h => ⟨fun b => by apply h.eq_top_or_exists_le_coatom⟩, fun h => ⟨fun b => by apply h.eq_bot_or_exists_atom_le⟩⟩ @[simp] theorem isAtomic_dual_iff_isCoatomic [OrderTop α] : IsAtomic αᵒᵈ ↔ IsCoatomic α := ⟨fun h => ⟨fun b => by apply h.eq_bot_or_exists_atom_le⟩, fun h => ⟨fun b => by apply h.eq_top_or_exists_le_coatom⟩⟩ namespace IsAtomic variable [OrderBot α] [IsAtomic α] instance _root_.OrderDual.instIsCoatomic : IsCoatomic αᵒᵈ := isCoatomic_dual_iff_isAtomic.2 ‹IsAtomic α› instance Set.Iic.isAtomic {x : α} : IsAtomic (Set.Iic x) := ⟨fun ⟨y, hy⟩ => (eq_bot_or_exists_atom_le y).imp Subtype.mk_eq_mk.2 fun ⟨a, ha, hay⟩ => ⟨⟨a, hay.trans hy⟩, ha.Iic (hay.trans hy), hay⟩⟩ end IsAtomic namespace IsCoatomic variable [OrderTop α] [IsCoatomic α] instance _root_.OrderDual.instIsAtomic : IsAtomic αᵒᵈ := isAtomic_dual_iff_isCoatomic.2 ‹IsCoatomic α› instance Set.Ici.isCoatomic {x : α} : IsCoatomic (Set.Ici x) := ⟨fun ⟨y, hy⟩ => (eq_top_or_exists_le_coatom y).imp Subtype.mk_eq_mk.2 fun ⟨a, ha, hay⟩ => ⟨⟨a, le_trans hy hay⟩, ha.Ici (le_trans hy hay), hay⟩⟩ end IsCoatomic theorem isAtomic_iff_forall_isAtomic_Iic [OrderBot α] : IsAtomic α ↔ ∀ x : α, IsAtomic (Set.Iic x) := ⟨@IsAtomic.Set.Iic.isAtomic _ _ _, fun h => ⟨fun x => ((@eq_bot_or_exists_atom_le _ _ _ (h x)) (⊤ : Set.Iic x)).imp Subtype.mk_eq_mk.1 (Exists.imp' (↑) fun ⟨_, _⟩ => And.imp_left IsAtom.of_isAtom_coe_Iic)⟩⟩ theorem isCoatomic_iff_forall_isCoatomic_Ici [OrderTop α] : IsCoatomic α ↔ ∀ x : α, IsCoatomic (Set.Ici x) := isAtomic_dual_iff_isCoatomic.symm.trans <| isAtomic_iff_forall_isAtomic_Iic.trans <| forall_congr' fun _ => isCoatomic_dual_iff_isAtomic.symm.trans Iff.rfl section StronglyAtomic variable {α : Type*} {a b : α} [Preorder α] /-- An order is strongly atomic if every nontrivial interval `[a, b]` contains an element covering `a`. -/ @[mk_iff] class IsStronglyAtomic (α : Type*) [Preorder α] : Prop where exists_covBy_le_of_lt : ∀ (a b : α), a < b → ∃ x, a ⋖ x ∧ x ≤ b theorem exists_covBy_le_of_lt [IsStronglyAtomic α] (h : a < b) : ∃ x, a ⋖ x ∧ x ≤ b := IsStronglyAtomic.exists_covBy_le_of_lt a b h alias LT.lt.exists_covby_le := exists_covBy_le_of_lt /-- An order is strongly coatomic if every nontrivial interval `[a, b]` contains an element covered by `b`. -/ @[mk_iff] class IsStronglyCoatomic (α : Type*) [Preorder α] : Prop where (exists_le_covBy_of_lt : ∀ (a b : α), a < b → ∃ x, a ≤ x ∧ x ⋖ b) theorem exists_le_covBy_of_lt [IsStronglyCoatomic α] (h : a < b) : ∃ x, a ≤ x ∧ x ⋖ b := IsStronglyCoatomic.exists_le_covBy_of_lt a b h alias LT.lt.exists_le_covby := exists_le_covBy_of_lt theorem isStronglyAtomic_dual_iff_is_stronglyCoatomic : IsStronglyAtomic αᵒᵈ ↔ IsStronglyCoatomic α := by simpa [isStronglyAtomic_iff, OrderDual.exists, OrderDual.forall, OrderDual.toDual_le_toDual, and_comm, isStronglyCoatomic_iff] using forall_comm @[simp] theorem isStronglyCoatomic_dual_iff_is_stronglyAtomic : IsStronglyCoatomic αᵒᵈ ↔ IsStronglyAtomic α := by rw [← isStronglyAtomic_dual_iff_is_stronglyCoatomic]; rfl instance OrderDual.instIsStronglyCoatomic [IsStronglyAtomic α] : IsStronglyCoatomic αᵒᵈ := by rwa [isStronglyCoatomic_dual_iff_is_stronglyAtomic] instance [IsStronglyCoatomic α] : IsStronglyAtomic αᵒᵈ := by rwa [isStronglyAtomic_dual_iff_is_stronglyCoatomic] instance IsStronglyAtomic.isAtomic (α : Type*) [PartialOrder α] [OrderBot α] [IsStronglyAtomic α] : IsAtomic α where eq_bot_or_exists_atom_le a := by rw [or_iff_not_imp_left, ← Ne, ← bot_lt_iff_ne_bot] refine fun hlt ↦ ?_ obtain ⟨x, hx, hxa⟩ := hlt.exists_covby_le exact ⟨x, bot_covBy_iff.1 hx, hxa⟩ instance IsStronglyCoatomic.toIsCoatomic (α : Type*) [PartialOrder α] [OrderTop α] [IsStronglyCoatomic α] : IsCoatomic α := isAtomic_dual_iff_isCoatomic.1 <| IsStronglyAtomic.isAtomic (α := αᵒᵈ) theorem Set.OrdConnected.isStronglyAtomic [IsStronglyAtomic α] {s : Set α} (h : Set.OrdConnected s) : IsStronglyAtomic s where exists_covBy_le_of_lt := by rintro ⟨c, hc⟩ ⟨d, hd⟩ hcd obtain ⟨x, hcx, hxd⟩ := (Subtype.mk_lt_mk.1 hcd).exists_covby_le exact ⟨⟨x, h.out' hc hd ⟨hcx.le, hxd⟩⟩, ⟨by simpa using hcx.lt, fun y hy hy' ↦ hcx.2 (by simpa using hy) (by simpa using hy')⟩, hxd⟩ theorem Set.OrdConnected.isStronglyCoatomic [IsStronglyCoatomic α] {s : Set α} (h : Set.OrdConnected s) : IsStronglyCoatomic s := isStronglyAtomic_dual_iff_is_stronglyCoatomic.1 h.dual.isStronglyAtomic instance [IsStronglyAtomic α] {s : Set α} [Set.OrdConnected s] : IsStronglyAtomic s := Set.OrdConnected.isStronglyAtomic <| by assumption instance [IsStronglyCoatomic α] {s : Set α} [h : Set.OrdConnected s] : IsStronglyCoatomic s := Set.OrdConnected.isStronglyCoatomic <| by assumption instance SuccOrder.toIsStronglyAtomic [SuccOrder α] : IsStronglyAtomic α where exists_covBy_le_of_lt a _ hab := ⟨SuccOrder.succ a, Order.covBy_succ_of_not_isMax fun ha ↦ ha.not_lt hab, SuccOrder.succ_le_of_lt hab⟩ instance [PredOrder α] : IsStronglyCoatomic α := by rw [← isStronglyAtomic_dual_iff_is_stronglyCoatomic]; infer_instance end StronglyAtomic section WellFounded theorem IsStronglyAtomic.of_wellFounded_lt (h : WellFounded ((· < ·) : α → α → Prop)) : IsStronglyAtomic α where exists_covBy_le_of_lt a b hab := by refine ⟨WellFounded.min h (Set.Ioc a b) ⟨b, hab,rfl.le⟩, ?_⟩ have hmem := (WellFounded.min_mem h (Set.Ioc a b) ⟨b, hab,rfl.le⟩) exact ⟨⟨hmem.1,fun c hac hlt ↦ WellFounded.not_lt_min h (Set.Ioc a b) ⟨b, hab,rfl.le⟩ ⟨hac, hlt.le.trans hmem.2⟩ hlt ⟩, hmem.2⟩ theorem IsStronglyCoatomic.of_wellFounded_gt (h : WellFounded ((· > ·) : α → α → Prop)) : IsStronglyCoatomic α := isStronglyAtomic_dual_iff_is_stronglyCoatomic.1 <| IsStronglyAtomic.of_wellFounded_lt (α := αᵒᵈ) h instance [WellFoundedLT α] : IsStronglyAtomic α := IsStronglyAtomic.of_wellFounded_lt wellFounded_lt instance [WellFoundedGT α] : IsStronglyCoatomic α := IsStronglyCoatomic.of_wellFounded_gt wellFounded_gt theorem isAtomic_of_orderBot_wellFounded_lt [OrderBot α] (h : WellFounded ((· < ·) : α → α → Prop)) : IsAtomic α := (IsStronglyAtomic.of_wellFounded_lt h).isAtomic theorem isCoatomic_of_orderTop_gt_wellFounded [OrderTop α] (h : WellFounded ((· > ·) : α → α → Prop)) : IsCoatomic α := isAtomic_dual_iff_isCoatomic.1 (@isAtomic_of_orderBot_wellFounded_lt αᵒᵈ _ _ h) end WellFounded namespace BooleanAlgebra theorem le_iff_atom_le_imp {α} [BooleanAlgebra α] [IsAtomic α] {x y : α} : x ≤ y ↔ ∀ a, IsAtom a → a ≤ x → a ≤ y := by refine ⟨fun h a _ => (le_trans · h), fun h => ?_⟩ have : x ⊓ yᶜ = ⊥ := of_not_not fun hbot => have ⟨a, ha, hle⟩ := (eq_bot_or_exists_atom_le _).resolve_left hbot have ⟨hx, hy'⟩ := le_inf_iff.1 hle have hy := h a ha hx have : a ≤ y ⊓ yᶜ := le_inf_iff.2 ⟨hy, hy'⟩ ha.1 (by simpa using this) exact (eq_compl_iff_isCompl.1 (by simp)).inf_right_eq_bot_iff.1 this theorem eq_iff_atom_le_iff {α} [BooleanAlgebra α] [IsAtomic α] {x y : α} : x = y ↔ ∀ a, IsAtom a → (a ≤ x ↔ a ≤ y) := by refine ⟨fun h => h ▸ by simp, fun h => ?_⟩ exact le_antisymm (le_iff_atom_le_imp.2 fun a ha hx => (h a ha).1 hx) (le_iff_atom_le_imp.2 fun a ha hy => (h a ha).2 hy) end BooleanAlgebra namespace CompleteBooleanAlgebra -- See note [reducible non-instances] abbrev toCompleteAtomicBooleanAlgebra {α} [CompleteBooleanAlgebra α] [IsAtomic α] : CompleteAtomicBooleanAlgebra α where __ := ‹CompleteBooleanAlgebra α› iInf_iSup_eq f := BooleanAlgebra.eq_iff_atom_le_iff.2 fun a ha => by simp only [le_iInf_iff, atom_le_iSup ha] rw [Classical.skolem] end CompleteBooleanAlgebra end Atomic section Atomistic variable (α) [PartialOrder α] /-- A lattice is atomistic iff every element is a `sSup` of a set of atoms. -/ @[mk_iff] class IsAtomistic [OrderBot α] : Prop where /-- Every element is a `sSup` of a set of atoms. -/ isLUB_atoms : ∀ b : α, ∃ s : Set α, IsLUB s b ∧ ∀ a, a ∈ s → IsAtom a /-- A lattice is coatomistic iff every element is an `sInf` of a set of coatoms. -/ @[mk_iff] class IsCoatomistic [OrderTop α] : Prop where /-- Every element is a `sInf` of a set of coatoms. -/ isGLB_coatoms : ∀ b : α, ∃ s : Set α, IsGLB s b ∧ ∀ a, a ∈ s → IsCoatom a export IsAtomistic (isLUB_atoms) export IsCoatomistic (isGLB_coatoms) variable {α} @[simp] theorem isCoatomistic_dual_iff_isAtomistic [OrderBot α] : IsCoatomistic αᵒᵈ ↔ IsAtomistic α := ⟨fun h => ⟨fun b => by apply h.isGLB_coatoms⟩, fun h => ⟨fun b => by apply h.isLUB_atoms⟩⟩ @[simp] theorem isAtomistic_dual_iff_isCoatomistic [OrderTop α] : IsAtomistic αᵒᵈ ↔ IsCoatomistic α := ⟨fun h => ⟨fun b => by apply h.isLUB_atoms⟩, fun h => ⟨fun b => by apply h.isGLB_coatoms⟩⟩ namespace IsAtomistic instance _root_.OrderDual.instIsCoatomistic [OrderBot α] [h : IsAtomistic α] : IsCoatomistic αᵒᵈ := isCoatomistic_dual_iff_isAtomistic.2 h variable [OrderBot α] [IsAtomistic α] instance (priority := 100) : IsAtomic α := ⟨fun b => by rcases isLUB_atoms b with ⟨s, hsb, hs⟩ rcases s.eq_empty_or_nonempty with rfl | ⟨a, ha⟩ · simp_all · exact Or.inr ⟨a, hs _ ha, hsb.1 ha⟩⟩ end IsAtomistic section IsAtomistic variable [OrderBot α] [IsAtomistic α] theorem isLUB_atoms_le (b : α) : IsLUB { a : α | IsAtom a ∧ a ≤ b } b := by rcases isLUB_atoms b with ⟨s, hsb, hs⟩ exact ⟨fun c hc ↦ hc.2, fun c hc ↦ hsb.2 fun i hi ↦ hc ⟨hs _ hi, hsb.1 hi⟩⟩ theorem isLUB_atoms_top [OrderTop α] : IsLUB { a : α | IsAtom a } ⊤ := by simpa using isLUB_atoms_le (⊤ : α) theorem le_iff_atom_le_imp {a b : α} : a ≤ b ↔ ∀ c : α, IsAtom c → c ≤ a → c ≤ b := ⟨fun hab _ _ hca ↦ hca.trans hab, fun h ↦ (isLUB_atoms_le a).mono (isLUB_atoms_le b) fun _ ⟨h₁, h₂⟩ ↦ ⟨h₁, h _ h₁ h₂⟩⟩ theorem eq_iff_atom_le_iff {a b : α} : a = b ↔ ∀ c, IsAtom c → (c ≤ a ↔ c ≤ b) := by refine ⟨fun h => by simp [h], fun h => ?_⟩ rw [le_antisymm_iff, le_iff_atom_le_imp, le_iff_atom_le_imp] aesop end IsAtomistic namespace IsCoatomistic variable [OrderTop α] instance _root_.OrderDual.instIsAtomistic [h : IsCoatomistic α] : IsAtomistic αᵒᵈ := isAtomistic_dual_iff_isCoatomistic.2 h variable [IsCoatomistic α] instance (priority := 100) : IsCoatomic α := ⟨fun b => by rcases isGLB_coatoms b with ⟨s, hsb, hs⟩ rcases s.eq_empty_or_nonempty with rfl | ⟨a, ha⟩ · simp_all · exact Or.inr ⟨a, hs _ ha, hsb.1 ha⟩⟩ end IsCoatomistic section CompleteLattice @[simp] theorem sSup_atoms_le_eq {α} [CompleteLattice α] [IsAtomistic α] (b : α) : sSup { a : α | IsAtom a ∧ a ≤ b } = b := (isLUB_atoms_le b).sSup_eq @[simp] theorem sSup_atoms_eq_top {α} [CompleteLattice α] [IsAtomistic α] : sSup { a : α | IsAtom a } = ⊤ := isLUB_atoms_top.sSup_eq nonrec lemma CompleteLattice.isAtomistic_iff {α} [CompleteLattice α] : IsAtomistic α ↔ ∀ b : α, ∃ s : Set α, b = sSup s ∧ ∀ a ∈ s, IsAtom a := by simp_rw [isAtomistic_iff, isLUB_iff_sSup_eq, eq_comm] lemma eq_sSup_atoms {α} [CompleteLattice α] [IsAtomistic α] (b : α) : ∃ s : Set α, b = sSup s ∧ ∀ a ∈ s, IsAtom a := CompleteLattice.isAtomistic_iff.1 ‹_› b nonrec lemma CompleteLattice.isCoatomistic_iff {α} [CompleteLattice α] : IsCoatomistic α ↔ ∀ b : α, ∃ s : Set α, b = sInf s ∧ ∀ a ∈ s, IsCoatom a := by simp_rw [isCoatomistic_iff, isGLB_iff_sInf_eq, eq_comm] lemma eq_sInf_coatoms {α} [CompleteLattice α] [IsCoatomistic α] (b : α) : ∃ s : Set α, b = sInf s ∧ ∀ a ∈ s, IsCoatom a := CompleteLattice.isCoatomistic_iff.1 ‹_› b end CompleteLattice namespace CompleteAtomicBooleanAlgebra instance {α} [CompleteAtomicBooleanAlgebra α] : IsAtomistic α := CompleteLattice.isAtomistic_iff.2 fun b ↦ by inhabit α refine ⟨{ a | IsAtom a ∧ a ≤ b }, ?_, fun a ha => ha.1⟩ refine le_antisymm ?_ (sSup_le fun c hc => hc.2) have : (⨅ c : α, ⨆ x, b ⊓ cond x c (cᶜ)) = b := by simp [iSup_bool_eq, iInf_const] rw [← this]; clear this simp_rw [iInf_iSup_eq, iSup_le_iff]; intro g if h : (⨅ a, b ⊓ cond (g a) a (aᶜ)) = ⊥ then simp [h] else refine le_sSup ⟨⟨h, fun c hc => ?_⟩, le_trans (by rfl) (le_iSup _ g)⟩; clear h have := lt_of_lt_of_le hc (le_trans (iInf_le _ c) inf_le_right) revert this nontriviality α cases g c <;> simp instance {α} [CompleteAtomicBooleanAlgebra α] : IsCoatomistic α := isAtomistic_dual_iff_isCoatomistic.1 inferInstance end CompleteAtomicBooleanAlgebra end Atomistic /-- An order is simple iff it has exactly two elements, `⊥` and `⊤`. -/ @[mk_iff] class IsSimpleOrder (α : Type*) [LE α] [BoundedOrder α] : Prop extends Nontrivial α where /-- Every element is either `⊥` or `⊤` -/ eq_bot_or_eq_top : ∀ a : α, a = ⊥ ∨ a = ⊤ export IsSimpleOrder (eq_bot_or_eq_top) theorem isSimpleOrder_iff_isSimpleOrder_orderDual [LE α] [BoundedOrder α] : IsSimpleOrder α ↔ IsSimpleOrder αᵒᵈ := by constructor <;> intro i <;> haveI := i · exact { exists_pair_ne := @exists_pair_ne α _ eq_bot_or_eq_top := fun a => Or.symm (eq_bot_or_eq_top (OrderDual.ofDual a) : _ ∨ _) } · exact { exists_pair_ne := @exists_pair_ne αᵒᵈ _ eq_bot_or_eq_top := fun a => Or.symm (eq_bot_or_eq_top (OrderDual.toDual a)) } theorem IsSimpleOrder.bot_ne_top [LE α] [BoundedOrder α] [IsSimpleOrder α] : (⊥ : α) ≠ (⊤ : α) := by obtain ⟨a, b, h⟩ := exists_pair_ne α rcases eq_bot_or_eq_top a with (rfl | rfl) <;> rcases eq_bot_or_eq_top b with (rfl | rfl) <;> first |simpa|simpa using h.symm section IsSimpleOrder variable [PartialOrder α] [BoundedOrder α] [IsSimpleOrder α] instance OrderDual.instIsSimpleOrder {α} [LE α] [BoundedOrder α] [IsSimpleOrder α] : IsSimpleOrder αᵒᵈ := isSimpleOrder_iff_isSimpleOrder_orderDual.1 (by infer_instance) /-- A simple `BoundedOrder` induces a preorder. This is not an instance to prevent loops. -/ protected def IsSimpleOrder.preorder {α} [LE α] [BoundedOrder α] [IsSimpleOrder α] : Preorder α where le := (· ≤ ·) le_refl a := by rcases eq_bot_or_eq_top a with (rfl | rfl) <;> simp le_trans a b c := by rcases eq_bot_or_eq_top a with (rfl | rfl) · simp · rcases eq_bot_or_eq_top b with (rfl | rfl) · rcases eq_bot_or_eq_top c with (rfl | rfl) <;> simp · simp /-- A simple partial ordered `BoundedOrder` induces a linear order. This is not an instance to prevent loops. -/ protected def IsSimpleOrder.linearOrder [DecidableEq α] : LinearOrder α := { (inferInstance : PartialOrder α) with le_total := fun a b => by rcases eq_bot_or_eq_top a with (rfl | rfl) <;> simp -- Note from #23976: do we want this inlined or should this be a separate definition? toDecidableLE := fun a b => if ha : a = ⊥ then isTrue (ha.le.trans bot_le) else if hb : b = ⊤ then isTrue (le_top.trans hb.ge) else isFalse fun H => hb (top_unique (le_trans (top_le_iff.mpr (Or.resolve_left (eq_bot_or_eq_top a) ha)) H)) toDecidableEq := ‹_› } theorem isAtom_top : IsAtom (⊤ : α) := ⟨top_ne_bot, fun a ha => Or.resolve_right (eq_bot_or_eq_top a) (ne_of_lt ha)⟩ @[simp] theorem isAtom_iff_eq_top {a : α} : IsAtom a ↔ a = ⊤ := ⟨fun h ↦ (eq_bot_or_eq_top a).resolve_left h.1, (· ▸ isAtom_top)⟩ theorem isCoatom_bot : IsCoatom (⊥ : α) := isAtom_dual_iff_isCoatom.1 isAtom_top @[simp] theorem isCoatom_iff_eq_bot {a : α} : IsCoatom a ↔ a = ⊥ := ⟨fun h ↦ (eq_bot_or_eq_top a).resolve_right h.1, (· ▸ isCoatom_bot)⟩ theorem bot_covBy_top : (⊥ : α) ⋖ ⊤ := isAtom_top.bot_covBy end IsSimpleOrder namespace IsSimpleOrder section Preorder variable [Preorder α] [BoundedOrder α] [IsSimpleOrder α] {a b : α} (h : a < b) include h theorem eq_bot_of_lt : a = ⊥ := (IsSimpleOrder.eq_bot_or_eq_top _).resolve_right h.ne_top theorem eq_top_of_lt : b = ⊤ := (IsSimpleOrder.eq_bot_or_eq_top _).resolve_left h.ne_bot alias _root_.LT.lt.eq_bot := eq_bot_of_lt alias _root_.LT.lt.eq_top := eq_top_of_lt end Preorder section BoundedOrder variable [Lattice α] [BoundedOrder α] [IsSimpleOrder α] /-- A simple partial ordered `BoundedOrder` induces a lattice. This is not an instance to prevent loops -/ protected def lattice {α} [DecidableEq α] [PartialOrder α] [BoundedOrder α] [IsSimpleOrder α] : Lattice α := @LinearOrder.toLattice α IsSimpleOrder.linearOrder /-- A lattice that is a `BoundedOrder` is a distributive lattice. This is not an instance to prevent loops -/ protected def distribLattice : DistribLattice α := { (inferInstance : Lattice α) with le_sup_inf := fun x y z => by rcases eq_bot_or_eq_top x with (rfl | rfl) <;> simp } -- see Note [lower instance priority] instance (priority := 100) : IsAtomic α := ⟨fun b => (eq_bot_or_eq_top b).imp_right fun h => ⟨⊤, ⟨isAtom_top, ge_of_eq h⟩⟩⟩ -- see Note [lower instance priority] instance (priority := 100) : IsCoatomic α := isAtomic_dual_iff_isCoatomic.1 (by infer_instance) end BoundedOrder -- It is important that in this section `IsSimpleOrder` is the last type-class argument. section DecidableEq variable [DecidableEq α] [PartialOrder α] [BoundedOrder α] [IsSimpleOrder α] /-- Every simple lattice is isomorphic to `Bool`, regardless of order. -/ @[simps] def equivBool {α} [DecidableEq α] [LE α] [BoundedOrder α] [IsSimpleOrder α] : α ≃ Bool where toFun x := x = ⊤ invFun x := x.casesOn ⊥ ⊤ left_inv x := by rcases eq_bot_or_eq_top x with (rfl | rfl) <;> simp [bot_ne_top] right_inv x := by cases x <;> simp [bot_ne_top] /-- Every simple lattice over a partial order is order-isomorphic to `Bool`. -/ def orderIsoBool : α ≃o Bool := { equivBool with map_rel_iff' := @fun a b => by rcases eq_bot_or_eq_top a with (rfl | rfl) · simp [bot_ne_top] · rcases eq_bot_or_eq_top b with (rfl | rfl) · simp [bot_ne_top.symm, bot_ne_top, Bool.false_lt_true] · simp [bot_ne_top] } /-- A simple `BoundedOrder` is also a `BooleanAlgebra`. -/ protected def booleanAlgebra {α} [DecidableEq α] [Lattice α] [BoundedOrder α] [IsSimpleOrder α] : BooleanAlgebra α := { inferInstanceAs (BoundedOrder α), IsSimpleOrder.distribLattice with compl := fun x => if x = ⊥ then ⊤ else ⊥ sdiff := fun x y => if x = ⊤ ∧ y = ⊥ then ⊤ else ⊥ sdiff_eq := fun x y => by rcases eq_bot_or_eq_top x with (rfl | rfl) <;> simp [bot_ne_top, SDiff.sdiff, compl] inf_compl_le_bot := fun x => by rcases eq_bot_or_eq_top x with (rfl | rfl) · simp · simp top_le_sup_compl := fun x => by rcases eq_bot_or_eq_top x with (rfl | rfl) <;> simp } end DecidableEq variable [Lattice α] [BoundedOrder α] [IsSimpleOrder α] open Classical in /-- A simple `BoundedOrder` is also complete. -/ protected noncomputable def completeLattice : CompleteLattice α := { (inferInstance : Lattice α), (inferInstance : BoundedOrder α) with sSup := fun s => if ⊤ ∈ s then ⊤ else ⊥ sInf := fun s => if ⊥ ∈ s then ⊥ else ⊤ le_sSup := fun s x h => by rcases eq_bot_or_eq_top x with (rfl | rfl) · exact bot_le · rw [if_pos h] sSup_le := fun s x h => by rcases eq_bot_or_eq_top x with (rfl | rfl) · rw [if_neg] intro con exact bot_ne_top (eq_top_iff.2 (h ⊤ con)) · exact le_top sInf_le := fun s x h => by rcases eq_bot_or_eq_top x with (rfl | rfl) · rw [if_pos h] · exact le_top le_sInf := fun s x h => by rcases eq_bot_or_eq_top x with (rfl | rfl) · exact bot_le · rw [if_neg] intro con exact top_ne_bot (eq_bot_iff.2 (h ⊥ con)) } open Classical in /-- A simple `BoundedOrder` is also a `CompleteBooleanAlgebra`. -/ protected noncomputable def completeBooleanAlgebra : CompleteBooleanAlgebra α := { __ := IsSimpleOrder.completeLattice __ := IsSimpleOrder.booleanAlgebra iInf_sup_le_sup_sInf := fun x s => by rcases eq_bot_or_eq_top x with (rfl | rfl) · simp [bot_sup_eq, ← sInf_eq_iInf] · simp only [top_le_iff, top_sup_eq, iInf_top, le_sInf_iff, le_refl] inf_sSup_le_iSup_inf := fun x s => by rcases eq_bot_or_eq_top x with (rfl | rfl) · simp only [le_bot_iff, sSup_eq_bot, bot_inf_eq, iSup_bot, le_refl] · simp only [top_inf_eq, ← sSup_eq_iSup] exact le_rfl } instance : ComplementedLattice α := letI := IsSimpleOrder.completeBooleanAlgebra (α := α); inferInstance end IsSimpleOrder namespace IsSimpleOrder variable [PartialOrder α] [BoundedOrder α] [IsSimpleOrder α] instance (priority := 100) : IsAtomistic α where isLUB_atoms b := (eq_bot_or_eq_top b).elim (fun h ↦ ⟨∅, by simp [h]⟩) (fun h ↦ ⟨{⊤}, by simp [h]⟩) instance (priority := 100) : IsCoatomistic α := isAtomistic_dual_iff_isCoatomistic.1 (by infer_instance) end IsSimpleOrder theorem isSimpleOrder_iff_isAtom_top [PartialOrder α] [BoundedOrder α] : IsSimpleOrder α ↔ IsAtom (⊤ : α) := ⟨fun h => @isAtom_top _ _ _ h, fun h => { exists_pair_ne := ⟨⊤, ⊥, h.1⟩ eq_bot_or_eq_top := fun a => ((eq_or_lt_of_le le_top).imp_right (h.2 a)).symm }⟩ theorem isSimpleOrder_iff_isCoatom_bot [PartialOrder α] [BoundedOrder α] : IsSimpleOrder α ↔ IsCoatom (⊥ : α) := isSimpleOrder_iff_isSimpleOrder_orderDual.trans isSimpleOrder_iff_isAtom_top namespace Set theorem isSimpleOrder_Iic_iff_isAtom [PartialOrder α] [OrderBot α] {a : α} : IsSimpleOrder (Iic a) ↔ IsAtom a := isSimpleOrder_iff_isAtom_top.trans <| and_congr (not_congr Subtype.mk_eq_mk) ⟨fun h b ab => Subtype.mk_eq_mk.1 (h ⟨b, le_of_lt ab⟩ ab), fun h ⟨b, _⟩ hbotb => Subtype.mk_eq_mk.2 (h b (Subtype.mk_lt_mk.1 hbotb))⟩ theorem isSimpleOrder_Ici_iff_isCoatom [PartialOrder α] [OrderTop α] {a : α} : IsSimpleOrder (Ici a) ↔ IsCoatom a := isSimpleOrder_iff_isCoatom_bot.trans <| and_congr (not_congr Subtype.mk_eq_mk) ⟨fun h b ab => Subtype.mk_eq_mk.1 (h ⟨b, le_of_lt ab⟩ ab), fun h ⟨b, _⟩ hbotb => Subtype.mk_eq_mk.2 (h b (Subtype.mk_lt_mk.1 hbotb))⟩ end Set namespace OrderEmbedding variable [PartialOrder α] [PartialOrder β] theorem isAtom_of_map_bot_of_image [OrderBot α] [OrderBot β] (f : β ↪o α) (hbot : f ⊥ = ⊥) {b : β} (hb : IsAtom (f b)) : IsAtom b := by simp only [← bot_covBy_iff] at hb ⊢ exact CovBy.of_image f (hbot.symm ▸ hb) theorem isCoatom_of_map_top_of_image [OrderTop α] [OrderTop β] (f : β ↪o α) (htop : f ⊤ = ⊤) {b : β} (hb : IsCoatom (f b)) : IsCoatom b := f.dual.isAtom_of_map_bot_of_image htop hb end OrderEmbedding namespace GaloisInsertion variable [PartialOrder α] [PartialOrder β] theorem isAtom_of_u_bot [OrderBot α] [OrderBot β] {l : α → β} {u : β → α} (gi : GaloisInsertion l u) (hbot : u ⊥ = ⊥) {b : β} (hb : IsAtom (u b)) : IsAtom b := OrderEmbedding.isAtom_of_map_bot_of_image ⟨⟨u, gi.u_injective⟩, @GaloisInsertion.u_le_u_iff _ _ _ _ _ _ gi⟩ hbot hb theorem isAtom_iff [OrderBot α] [IsAtomic α] [OrderBot β] {l : α → β} {u : β → α} (gi : GaloisInsertion l u) (hbot : u ⊥ = ⊥) (h_atom : ∀ a, IsAtom a → u (l a) = a) (a : α) : IsAtom (l a) ↔ IsAtom a := by
refine ⟨fun hla => ?_, fun ha => gi.isAtom_of_u_bot hbot ((h_atom a ha).symm ▸ ha)⟩ obtain ⟨a', ha', hab'⟩ := (eq_bot_or_exists_atom_le (u (l a))).resolve_left (hbot ▸ fun h => hla.1 (gi.u_injective h)) have :=
Mathlib/Order/Atoms.lean
917
920
/- Copyright (c) 2020 Aaron Anderson, Jalex Stark, Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark, Kyle Miller, Alena Gusakov, Hunter Monroe -/ import Mathlib.Combinatorics.SimpleGraph.Init import Mathlib.Data.Finite.Prod import Mathlib.Data.Rel import Mathlib.Data.Set.Finite.Basic import Mathlib.Data.Sym.Sym2 /-! # Simple graphs This module defines simple graphs on a vertex type `V` as an irreflexive symmetric relation. ## Main definitions * `SimpleGraph` is a structure for symmetric, irreflexive relations. * `SimpleGraph.neighborSet` is the `Set` of vertices adjacent to a given vertex. * `SimpleGraph.commonNeighbors` is the intersection of the neighbor sets of two given vertices. * `SimpleGraph.incidenceSet` is the `Set` of edges containing a given vertex. * `CompleteAtomicBooleanAlgebra` instance: Under the subgraph relation, `SimpleGraph` forms a `CompleteAtomicBooleanAlgebra`. In other words, this is the complete lattice of spanning subgraphs of the complete graph. ## TODO * This is the simplest notion of an unoriented graph. This should eventually fit into a more complete combinatorics hierarchy which includes multigraphs and directed graphs. We begin with simple graphs in order to start learning what the combinatorics hierarchy should look like. -/ attribute [aesop norm unfold (rule_sets := [SimpleGraph])] Symmetric attribute [aesop norm unfold (rule_sets := [SimpleGraph])] Irreflexive /-- A variant of the `aesop` tactic for use in the graph library. Changes relative to standard `aesop`: - We use the `SimpleGraph` rule set in addition to the default rule sets. - We instruct Aesop's `intro` rule to unfold with `default` transparency. - We instruct Aesop to fail if it can't fully solve the goal. This allows us to use `aesop_graph` for auto-params. -/ macro (name := aesop_graph) "aesop_graph" c:Aesop.tactic_clause* : tactic => `(tactic| aesop $c* (config := { introsTransparency? := some .default, terminal := true }) (rule_sets := [$(Lean.mkIdent `SimpleGraph):ident])) /-- Use `aesop_graph?` to pass along a `Try this` suggestion when using `aesop_graph` -/ macro (name := aesop_graph?) "aesop_graph?" c:Aesop.tactic_clause* : tactic => `(tactic| aesop? $c* (config := { introsTransparency? := some .default, terminal := true }) (rule_sets := [$(Lean.mkIdent `SimpleGraph):ident])) /-- A variant of `aesop_graph` which does not fail if it is unable to solve the goal. Use this only for exploration! Nonterminal Aesop is even worse than nonterminal `simp`. -/ macro (name := aesop_graph_nonterminal) "aesop_graph_nonterminal" c:Aesop.tactic_clause* : tactic => `(tactic| aesop $c* (config := { introsTransparency? := some .default, warnOnNonterminal := false }) (rule_sets := [$(Lean.mkIdent `SimpleGraph):ident])) open Finset Function universe u v w /-- A simple graph is an irreflexive symmetric relation `Adj` on a vertex type `V`. The relation describes which pairs of vertices are adjacent. There is exactly one edge for every pair of adjacent vertices; see `SimpleGraph.edgeSet` for the corresponding edge set. -/ @[ext, aesop safe constructors (rule_sets := [SimpleGraph])] structure SimpleGraph (V : Type u) where /-- The adjacency relation of a simple graph. -/ Adj : V → V → Prop symm : Symmetric Adj := by aesop_graph loopless : Irreflexive Adj := by aesop_graph initialize_simps_projections SimpleGraph (Adj → adj) /-- Constructor for simple graphs using a symmetric irreflexive boolean function. -/ @[simps] def SimpleGraph.mk' {V : Type u} : {adj : V → V → Bool // (∀ x y, adj x y = adj y x) ∧ (∀ x, ¬ adj x x)} ↪ SimpleGraph V where toFun x := ⟨fun v w ↦ x.1 v w, fun v w ↦ by simp [x.2.1], fun v ↦ by simp [x.2.2]⟩ inj' := by rintro ⟨adj, _⟩ ⟨adj', _⟩ simp only [mk.injEq, Subtype.mk.injEq] intro h funext v w simpa [Bool.coe_iff_coe] using congr_fun₂ h v w /-- We can enumerate simple graphs by enumerating all functions `V → V → Bool` and filtering on whether they are symmetric and irreflexive. -/ instance {V : Type u} [Fintype V] [DecidableEq V] : Fintype (SimpleGraph V) where elems := Finset.univ.map SimpleGraph.mk' complete := by classical rintro ⟨Adj, hs, hi⟩ simp only [mem_map, mem_univ, true_and, Subtype.exists, Bool.not_eq_true] refine ⟨fun v w ↦ Adj v w, ⟨?_, ?_⟩, ?_⟩ · simp [hs.iff] · intro v; simp [hi v] · ext simp /-- There are finitely many simple graphs on a given finite type. -/ instance SimpleGraph.instFinite {V : Type u} [Finite V] : Finite (SimpleGraph V) := .of_injective SimpleGraph.Adj fun _ _ ↦ SimpleGraph.ext /-- Construct the simple graph induced by the given relation. It symmetrizes the relation and makes it irreflexive. -/ def SimpleGraph.fromRel {V : Type u} (r : V → V → Prop) : SimpleGraph V where Adj a b := a ≠ b ∧ (r a b ∨ r b a) symm := fun _ _ ⟨hn, hr⟩ => ⟨hn.symm, hr.symm⟩ loopless := fun _ ⟨hn, _⟩ => hn rfl @[simp] theorem SimpleGraph.fromRel_adj {V : Type u} (r : V → V → Prop) (v w : V) : (SimpleGraph.fromRel r).Adj v w ↔ v ≠ w ∧ (r v w ∨ r w v) := Iff.rfl attribute [aesop safe (rule_sets := [SimpleGraph])] Ne.symm attribute [aesop safe (rule_sets := [SimpleGraph])] Ne.irrefl /-- The complete graph on a type `V` is the simple graph with all pairs of distinct vertices adjacent. In `Mathlib`, this is usually referred to as `⊤`. -/ def completeGraph (V : Type u) : SimpleGraph V where Adj := Ne /-- The graph with no edges on a given vertex type `V`. `Mathlib` prefers the notation `⊥`. -/ def emptyGraph (V : Type u) : SimpleGraph V where Adj _ _ := False /-- Two vertices are adjacent in the complete bipartite graph on two vertex types if and only if they are not from the same side. Any bipartite graph may be regarded as a subgraph of one of these. -/ @[simps] def completeBipartiteGraph (V W : Type*) : SimpleGraph (V ⊕ W) where Adj v w := v.isLeft ∧ w.isRight ∨ v.isRight ∧ w.isLeft symm v w := by cases v <;> cases w <;> simp loopless v := by cases v <;> simp namespace SimpleGraph variable {ι : Sort*} {V : Type u} (G : SimpleGraph V) {a b c u v w : V} {e : Sym2 V} @[simp] protected theorem irrefl {v : V} : ¬G.Adj v v := G.loopless v theorem adj_comm (u v : V) : G.Adj u v ↔ G.Adj v u := ⟨fun x => G.symm x, fun x => G.symm x⟩ @[symm] theorem adj_symm (h : G.Adj u v) : G.Adj v u := G.symm h theorem Adj.symm {G : SimpleGraph V} {u v : V} (h : G.Adj u v) : G.Adj v u := G.symm h theorem ne_of_adj (h : G.Adj a b) : a ≠ b := by rintro rfl exact G.irrefl h protected theorem Adj.ne {G : SimpleGraph V} {a b : V} (h : G.Adj a b) : a ≠ b := G.ne_of_adj h protected theorem Adj.ne' {G : SimpleGraph V} {a b : V} (h : G.Adj a b) : b ≠ a := h.ne.symm theorem ne_of_adj_of_not_adj {v w x : V} (h : G.Adj v x) (hn : ¬G.Adj w x) : v ≠ w := fun h' => hn (h' ▸ h) theorem adj_injective : Injective (Adj : SimpleGraph V → V → V → Prop) := fun _ _ => SimpleGraph.ext @[simp] theorem adj_inj {G H : SimpleGraph V} : G.Adj = H.Adj ↔ G = H := adj_injective.eq_iff theorem adj_congr_of_sym2 {u v w x : V} (h : s(u, v) = s(w, x)) : G.Adj u v ↔ G.Adj w x := by simp only [Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk] at h rcases h with hl | hr · rw [hl.1, hl.2] · rw [hr.1, hr.2, adj_comm] section Order /-- The relation that one `SimpleGraph` is a subgraph of another. Note that this should be spelled `≤`. -/ def IsSubgraph (x y : SimpleGraph V) : Prop := ∀ ⦃v w : V⦄, x.Adj v w → y.Adj v w instance : LE (SimpleGraph V) := ⟨IsSubgraph⟩ @[simp] theorem isSubgraph_eq_le : (IsSubgraph : SimpleGraph V → SimpleGraph V → Prop) = (· ≤ ·) := rfl /-- The supremum of two graphs `x ⊔ y` has edges where either `x` or `y` have edges. -/ instance : Max (SimpleGraph V) where max x y := { Adj := x.Adj ⊔ y.Adj symm := fun v w h => by rwa [Pi.sup_apply, Pi.sup_apply, x.adj_comm, y.adj_comm] } @[simp] theorem sup_adj (x y : SimpleGraph V) (v w : V) : (x ⊔ y).Adj v w ↔ x.Adj v w ∨ y.Adj v w := Iff.rfl /-- The infimum of two graphs `x ⊓ y` has edges where both `x` and `y` have edges. -/ instance : Min (SimpleGraph V) where min x y := { Adj := x.Adj ⊓ y.Adj symm := fun v w h => by rwa [Pi.inf_apply, Pi.inf_apply, x.adj_comm, y.adj_comm] } @[simp] theorem inf_adj (x y : SimpleGraph V) (v w : V) : (x ⊓ y).Adj v w ↔ x.Adj v w ∧ y.Adj v w := Iff.rfl /-- We define `Gᶜ` to be the `SimpleGraph V` such that no two adjacent vertices in `G` are adjacent in the complement, and every nonadjacent pair of vertices is adjacent (still ensuring that vertices are not adjacent to themselves). -/ instance hasCompl : HasCompl (SimpleGraph V) where compl G := { Adj := fun v w => v ≠ w ∧ ¬G.Adj v w symm := fun v w ⟨hne, _⟩ => ⟨hne.symm, by rwa [adj_comm]⟩ loopless := fun _ ⟨hne, _⟩ => (hne rfl).elim } @[simp] theorem compl_adj (G : SimpleGraph V) (v w : V) : Gᶜ.Adj v w ↔ v ≠ w ∧ ¬G.Adj v w := Iff.rfl /-- The difference of two graphs `x \ y` has the edges of `x` with the edges of `y` removed. -/ instance sdiff : SDiff (SimpleGraph V) where sdiff x y := { Adj := x.Adj \ y.Adj symm := fun v w h => by change x.Adj w v ∧ ¬y.Adj w v; rwa [x.adj_comm, y.adj_comm] } @[simp] theorem sdiff_adj (x y : SimpleGraph V) (v w : V) : (x \ y).Adj v w ↔ x.Adj v w ∧ ¬y.Adj v w := Iff.rfl instance supSet : SupSet (SimpleGraph V) where sSup s := { Adj := fun a b => ∃ G ∈ s, Adj G a b symm := fun _ _ => Exists.imp fun _ => And.imp_right Adj.symm loopless := by rintro a ⟨G, _, ha⟩ exact ha.ne rfl } instance infSet : InfSet (SimpleGraph V) where sInf s := { Adj := fun a b => (∀ ⦃G⦄, G ∈ s → Adj G a b) ∧ a ≠ b symm := fun _ _ => And.imp (forall₂_imp fun _ _ => Adj.symm) Ne.symm loopless := fun _ h => h.2 rfl } @[simp] theorem sSup_adj {s : Set (SimpleGraph V)} {a b : V} : (sSup s).Adj a b ↔ ∃ G ∈ s, Adj G a b := Iff.rfl @[simp] theorem sInf_adj {s : Set (SimpleGraph V)} : (sInf s).Adj a b ↔ (∀ G ∈ s, Adj G a b) ∧ a ≠ b := Iff.rfl @[simp] theorem iSup_adj {f : ι → SimpleGraph V} : (⨆ i, f i).Adj a b ↔ ∃ i, (f i).Adj a b := by simp [iSup] @[simp] theorem iInf_adj {f : ι → SimpleGraph V} : (⨅ i, f i).Adj a b ↔ (∀ i, (f i).Adj a b) ∧ a ≠ b := by simp [iInf] theorem sInf_adj_of_nonempty {s : Set (SimpleGraph V)} (hs : s.Nonempty) : (sInf s).Adj a b ↔ ∀ G ∈ s, Adj G a b := sInf_adj.trans <| and_iff_left_of_imp <| by obtain ⟨G, hG⟩ := hs exact fun h => (h _ hG).ne theorem iInf_adj_of_nonempty [Nonempty ι] {f : ι → SimpleGraph V} : (⨅ i, f i).Adj a b ↔ ∀ i, (f i).Adj a b := by rw [iInf, sInf_adj_of_nonempty (Set.range_nonempty _), Set.forall_mem_range] /-- For graphs `G`, `H`, `G ≤ H` iff `∀ a b, G.Adj a b → H.Adj a b`. -/ instance distribLattice : DistribLattice (SimpleGraph V) := { show DistribLattice (SimpleGraph V) from adj_injective.distribLattice _ (fun _ _ => rfl) fun _ _ => rfl with le := fun G H => ∀ ⦃a b⦄, G.Adj a b → H.Adj a b } instance completeAtomicBooleanAlgebra : CompleteAtomicBooleanAlgebra (SimpleGraph V) := { SimpleGraph.distribLattice with le := (· ≤ ·) sup := (· ⊔ ·) inf := (· ⊓ ·) compl := HasCompl.compl sdiff := (· \ ·) top := completeGraph V bot := emptyGraph V le_top := fun x _ _ h => x.ne_of_adj h bot_le := fun _ _ _ h => h.elim sdiff_eq := fun x y => by ext v w refine ⟨fun h => ⟨h.1, ⟨?_, h.2⟩⟩, fun h => ⟨h.1, h.2.2⟩⟩ rintro rfl exact x.irrefl h.1 inf_compl_le_bot := fun _ _ _ h => False.elim <| h.2.2 h.1 top_le_sup_compl := fun G v w hvw => by by_cases h : G.Adj v w · exact Or.inl h · exact Or.inr ⟨hvw, h⟩ sSup := sSup le_sSup := fun _ G hG _ _ hab => ⟨G, hG, hab⟩ sSup_le := fun s G hG a b => by rintro ⟨H, hH, hab⟩ exact hG _ hH hab sInf := sInf sInf_le := fun _ _ hG _ _ hab => hab.1 hG le_sInf := fun _ _ hG _ _ hab => ⟨fun _ hH => hG _ hH hab, hab.ne⟩ iInf_iSup_eq := fun f => by ext; simp [Classical.skolem] } @[simp] theorem top_adj (v w : V) : (⊤ : SimpleGraph V).Adj v w ↔ v ≠ w := Iff.rfl @[simp] theorem bot_adj (v w : V) : (⊥ : SimpleGraph V).Adj v w ↔ False := Iff.rfl @[simp] theorem completeGraph_eq_top (V : Type u) : completeGraph V = ⊤ := rfl @[simp] theorem emptyGraph_eq_bot (V : Type u) : emptyGraph V = ⊥ := rfl @[simps] instance (V : Type u) : Inhabited (SimpleGraph V) := ⟨⊥⟩ instance [Subsingleton V] : Unique (SimpleGraph V) where default := ⊥ uniq G := by ext a b; have := Subsingleton.elim a b; simp [this] instance [Nontrivial V] : Nontrivial (SimpleGraph V) := ⟨⟨⊥, ⊤, fun h ↦ not_subsingleton V ⟨by simpa only [← adj_inj, funext_iff, bot_adj, top_adj, ne_eq, eq_iff_iff, false_iff, not_not] using h⟩⟩⟩ section Decidable variable (V) (H : SimpleGraph V) [DecidableRel G.Adj] [DecidableRel H.Adj] instance Bot.adjDecidable : DecidableRel (⊥ : SimpleGraph V).Adj := inferInstanceAs <| DecidableRel fun _ _ => False instance Sup.adjDecidable : DecidableRel (G ⊔ H).Adj := inferInstanceAs <| DecidableRel fun v w => G.Adj v w ∨ H.Adj v w instance Inf.adjDecidable : DecidableRel (G ⊓ H).Adj := inferInstanceAs <| DecidableRel fun v w => G.Adj v w ∧ H.Adj v w instance Sdiff.adjDecidable : DecidableRel (G \ H).Adj := inferInstanceAs <| DecidableRel fun v w => G.Adj v w ∧ ¬H.Adj v w variable [DecidableEq V] instance Top.adjDecidable : DecidableRel (⊤ : SimpleGraph V).Adj := inferInstanceAs <| DecidableRel fun v w => v ≠ w instance Compl.adjDecidable : DecidableRel (Gᶜ.Adj) := inferInstanceAs <| DecidableRel fun v w => v ≠ w ∧ ¬G.Adj v w end Decidable end Order /-- `G.support` is the set of vertices that form edges in `G`. -/ def support : Set V := Rel.dom G.Adj theorem mem_support {v : V} : v ∈ G.support ↔ ∃ w, G.Adj v w := Iff.rfl theorem support_mono {G G' : SimpleGraph V} (h : G ≤ G') : G.support ⊆ G'.support := Rel.dom_mono h /-- `G.neighborSet v` is the set of vertices adjacent to `v` in `G`. -/ def neighborSet (v : V) : Set V := {w | G.Adj v w} instance neighborSet.memDecidable (v : V) [DecidableRel G.Adj] : DecidablePred (· ∈ G.neighborSet v) := inferInstanceAs <| DecidablePred (Adj G v) lemma neighborSet_subset_support (v : V) : G.neighborSet v ⊆ G.support := fun _ hadj ↦ ⟨v, hadj.symm⟩ section EdgeSet variable {G₁ G₂ : SimpleGraph V} /-- The edges of G consist of the unordered pairs of vertices related by `G.Adj`. This is the order embedding; for the edge set of a particular graph, see `SimpleGraph.edgeSet`. The way `edgeSet` is defined is such that `mem_edgeSet` is proved by `Iff.rfl`. (That is, `s(v, w) ∈ G.edgeSet` is definitionally equal to `G.Adj v w`.) -/ -- Porting note: We need a separate definition so that dot notation works. def edgeSetEmbedding (V : Type*) : SimpleGraph V ↪o Set (Sym2 V) := OrderEmbedding.ofMapLEIff (fun G => Sym2.fromRel G.symm) fun _ _ => ⟨fun h a b => @h s(a, b), fun h e => Sym2.ind @h e⟩ /-- `G.edgeSet` is the edge set for `G`. This is an abbreviation for `edgeSetEmbedding G` that permits dot notation. -/ abbrev edgeSet (G : SimpleGraph V) : Set (Sym2 V) := edgeSetEmbedding V G @[simp] theorem mem_edgeSet : s(v, w) ∈ G.edgeSet ↔ G.Adj v w := Iff.rfl theorem not_isDiag_of_mem_edgeSet : e ∈ edgeSet G → ¬e.IsDiag := Sym2.ind (fun _ _ => Adj.ne) e theorem edgeSet_inj : G₁.edgeSet = G₂.edgeSet ↔ G₁ = G₂ := (edgeSetEmbedding V).eq_iff_eq @[simp] theorem edgeSet_subset_edgeSet : edgeSet G₁ ⊆ edgeSet G₂ ↔ G₁ ≤ G₂ := (edgeSetEmbedding V).le_iff_le @[simp] theorem edgeSet_ssubset_edgeSet : edgeSet G₁ ⊂ edgeSet G₂ ↔ G₁ < G₂ := (edgeSetEmbedding V).lt_iff_lt theorem edgeSet_injective : Injective (edgeSet : SimpleGraph V → Set (Sym2 V)) := (edgeSetEmbedding V).injective alias ⟨_, edgeSet_mono⟩ := edgeSet_subset_edgeSet alias ⟨_, edgeSet_strict_mono⟩ := edgeSet_ssubset_edgeSet attribute [mono] edgeSet_mono edgeSet_strict_mono variable (G₁ G₂) @[simp] theorem edgeSet_bot : (⊥ : SimpleGraph V).edgeSet = ∅ := Sym2.fromRel_bot @[simp] theorem edgeSet_top : (⊤ : SimpleGraph V).edgeSet = {e | ¬e.IsDiag} := Sym2.fromRel_ne @[simp] theorem edgeSet_subset_setOf_not_isDiag : G.edgeSet ⊆ {e | ¬e.IsDiag} := fun _ h => (Sym2.fromRel_irreflexive (sym := G.symm)).mp G.loopless h @[simp] theorem edgeSet_sup : (G₁ ⊔ G₂).edgeSet = G₁.edgeSet ∪ G₂.edgeSet := by ext ⟨x, y⟩ rfl @[simp] theorem edgeSet_inf : (G₁ ⊓ G₂).edgeSet = G₁.edgeSet ∩ G₂.edgeSet := by ext ⟨x, y⟩ rfl @[simp] theorem edgeSet_sdiff : (G₁ \ G₂).edgeSet = G₁.edgeSet \ G₂.edgeSet := by ext ⟨x, y⟩ rfl variable {G G₁ G₂} @[simp] lemma disjoint_edgeSet : Disjoint G₁.edgeSet G₂.edgeSet ↔ Disjoint G₁ G₂ := by rw [Set.disjoint_iff, disjoint_iff_inf_le, ← edgeSet_inf, ← edgeSet_bot, ← Set.le_iff_subset, OrderEmbedding.le_iff_le] @[simp] lemma edgeSet_eq_empty : G.edgeSet = ∅ ↔ G = ⊥ := by rw [← edgeSet_bot, edgeSet_inj] @[simp] lemma edgeSet_nonempty : G.edgeSet.Nonempty ↔ G ≠ ⊥ := by rw [Set.nonempty_iff_ne_empty, edgeSet_eq_empty.ne] /-- This lemma, combined with `edgeSet_sdiff` and `edgeSet_from_edgeSet`, allows proving `(G \ from_edgeSet s).edge_set = G.edgeSet \ s` by `simp`. -/ @[simp] theorem edgeSet_sdiff_sdiff_isDiag (G : SimpleGraph V) (s : Set (Sym2 V)) : G.edgeSet \ (s \ { e | e.IsDiag }) = G.edgeSet \ s := by ext e simp only [Set.mem_diff, Set.mem_setOf_eq, not_and, not_not, and_congr_right_iff] intro h simp only [G.not_isDiag_of_mem_edgeSet h, imp_false] /-- Two vertices are adjacent iff there is an edge between them. The condition `v ≠ w` ensures they are different endpoints of the edge, which is necessary since when `v = w` the existential `∃ (e ∈ G.edgeSet), v ∈ e ∧ w ∈ e` is satisfied by every edge incident to `v`. -/ theorem adj_iff_exists_edge {v w : V} : G.Adj v w ↔ v ≠ w ∧ ∃ e ∈ G.edgeSet, v ∈ e ∧ w ∈ e := by refine ⟨fun _ => ⟨G.ne_of_adj ‹_›, s(v, w), by simpa⟩, ?_⟩ rintro ⟨hne, e, he, hv⟩ rw [Sym2.mem_and_mem_iff hne] at hv subst e rwa [mem_edgeSet] at he theorem adj_iff_exists_edge_coe : G.Adj a b ↔ ∃ e : G.edgeSet, e.val = s(a, b) := by simp only [mem_edgeSet, exists_prop, SetCoe.exists, exists_eq_right, Subtype.coe_mk] variable (G G₁ G₂) theorem edge_other_ne {e : Sym2 V} (he : e ∈ G.edgeSet) {v : V} (h : v ∈ e) : Sym2.Mem.other h ≠ v := by rw [← Sym2.other_spec h, Sym2.eq_swap] at he exact G.ne_of_adj he instance decidableMemEdgeSet [DecidableRel G.Adj] : DecidablePred (· ∈ G.edgeSet) := Sym2.fromRel.decidablePred G.symm instance fintypeEdgeSet [Fintype (Sym2 V)] [DecidableRel G.Adj] : Fintype G.edgeSet := Subtype.fintype _ instance fintypeEdgeSetBot : Fintype (⊥ : SimpleGraph V).edgeSet := by rw [edgeSet_bot] infer_instance instance fintypeEdgeSetSup [DecidableEq V] [Fintype G₁.edgeSet] [Fintype G₂.edgeSet] : Fintype (G₁ ⊔ G₂).edgeSet := by rw [edgeSet_sup] infer_instance instance fintypeEdgeSetInf [DecidableEq V] [Fintype G₁.edgeSet] [Fintype G₂.edgeSet] : Fintype (G₁ ⊓ G₂).edgeSet := by rw [edgeSet_inf] exact Set.fintypeInter _ _ instance fintypeEdgeSetSdiff [DecidableEq V] [Fintype G₁.edgeSet] [Fintype G₂.edgeSet] : Fintype (G₁ \ G₂).edgeSet := by rw [edgeSet_sdiff] exact Set.fintypeDiff _ _ end EdgeSet section FromEdgeSet variable (s : Set (Sym2 V)) /-- `fromEdgeSet` constructs a `SimpleGraph` from a set of edges, without loops. -/ def fromEdgeSet : SimpleGraph V where Adj := Sym2.ToRel s ⊓ Ne symm _ _ h := ⟨Sym2.toRel_symmetric s h.1, h.2.symm⟩ @[simp] theorem fromEdgeSet_adj : (fromEdgeSet s).Adj v w ↔ s(v, w) ∈ s ∧ v ≠ w := Iff.rfl -- Note: we need to make sure `fromEdgeSet_adj` and this lemma are confluent. -- In particular, both yield `s(u, v) ∈ (fromEdgeSet s).edgeSet` ==> `s(v, w) ∈ s ∧ v ≠ w`. @[simp] theorem edgeSet_fromEdgeSet : (fromEdgeSet s).edgeSet = s \ { e | e.IsDiag } := by ext e exact Sym2.ind (by simp) e @[simp] theorem fromEdgeSet_edgeSet : fromEdgeSet G.edgeSet = G := by ext v w exact ⟨fun h => h.1, fun h => ⟨h, G.ne_of_adj h⟩⟩ @[simp] theorem fromEdgeSet_empty : fromEdgeSet (∅ : Set (Sym2 V)) = ⊥ := by ext v w simp only [fromEdgeSet_adj, Set.mem_empty_iff_false, false_and, bot_adj] @[simp] theorem fromEdgeSet_univ : fromEdgeSet (Set.univ : Set (Sym2 V)) = ⊤ := by ext v w simp only [fromEdgeSet_adj, Set.mem_univ, true_and, top_adj] @[simp] theorem fromEdgeSet_inter (s t : Set (Sym2 V)) : fromEdgeSet (s ∩ t) = fromEdgeSet s ⊓ fromEdgeSet t := by ext v w simp only [fromEdgeSet_adj, Set.mem_inter_iff, Ne, inf_adj] tauto @[simp] theorem fromEdgeSet_union (s t : Set (Sym2 V)) : fromEdgeSet (s ∪ t) = fromEdgeSet s ⊔ fromEdgeSet t := by ext v w simp [Set.mem_union, or_and_right] @[simp] theorem fromEdgeSet_sdiff (s t : Set (Sym2 V)) : fromEdgeSet (s \ t) = fromEdgeSet s \ fromEdgeSet t := by ext v w constructor <;> simp +contextual @[gcongr, mono] theorem fromEdgeSet_mono {s t : Set (Sym2 V)} (h : s ⊆ t) : fromEdgeSet s ≤ fromEdgeSet t := by rintro v w simp +contextual only [fromEdgeSet_adj, Ne, not_false_iff, and_true, and_imp] exact fun vws _ => h vws @[simp] lemma disjoint_fromEdgeSet : Disjoint G (fromEdgeSet s) ↔ Disjoint G.edgeSet s := by conv_rhs => rw [← Set.diff_union_inter s {e : Sym2 V | e.IsDiag}] rw [← disjoint_edgeSet, edgeSet_fromEdgeSet, Set.disjoint_union_right, and_iff_left] exact Set.disjoint_left.2 fun e he he' ↦ not_isDiag_of_mem_edgeSet _ he he'.2 @[simp] lemma fromEdgeSet_disjoint : Disjoint (fromEdgeSet s) G ↔ Disjoint s G.edgeSet := by rw [disjoint_comm, disjoint_fromEdgeSet, disjoint_comm] instance [DecidableEq V] [Fintype s] : Fintype (fromEdgeSet s).edgeSet := by rw [edgeSet_fromEdgeSet s] infer_instance end FromEdgeSet /-! ### Incidence set -/ /-- Set of edges incident to a given vertex, aka incidence set. -/ def incidenceSet (v : V) : Set (Sym2 V) := { e ∈ G.edgeSet | v ∈ e } theorem incidenceSet_subset (v : V) : G.incidenceSet v ⊆ G.edgeSet := fun _ h => h.1
theorem mk'_mem_incidenceSet_iff : s(b, c) ∈ G.incidenceSet a ↔ G.Adj b c ∧ (a = b ∨ a = c) := and_congr_right' Sym2.mem_iff
Mathlib/Combinatorics/SimpleGraph/Basic.lean
638
640
/- 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.Algebra.BigOperators.Group.Finset.Powerset import Mathlib.Algebra.NoZeroSMulDivisors.Pi import Mathlib.Data.Finset.Sort import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.Fintype.Powerset import Mathlib.LinearAlgebra.Pi import Mathlib.Logic.Equiv.Fintype import Mathlib.Tactic.Abel /-! # Multilinear maps We define multilinear maps as maps from `∀ (i : ι), M₁ i` to `M₂` which are linear in each coordinate. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type (although some statements will require it to be a fintype). This space, denoted by `MultilinearMap R M₁ M₂`, inherits a module structure by pointwise addition and multiplication. ## Main definitions * `MultilinearMap R M₁ M₂` is the space of multilinear maps from `∀ (i : ι), M₁ i` to `M₂`. * `f.map_update_smul` is the multiplicativity of the multilinear map `f` along each coordinate. * `f.map_update_add` is the additivity of the multilinear map `f` along each coordinate. * `f.map_smul_univ` expresses the multiplicativity of `f` over all coordinates at the same time, writing `f (fun i => c i • m i)` as `(∏ i, c i) • f m`. * `f.map_add_univ` expresses the additivity of `f` over all coordinates at the same time, writing `f (m + m')` as the sum over all subsets `s` of `ι` of `f (s.piecewise m m')`. * `f.map_sum` expresses `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` as the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all possible functions. See `Mathlib.LinearAlgebra.Multilinear.Curry` for the currying of multilinear maps. ## Implementation notes Expressing that a map is linear along the `i`-th coordinate when all other coordinates are fixed can be done in two (equivalent) different ways: * fixing a vector `m : ∀ (j : ι - i), M₁ j.val`, and then choosing separately the `i`-th coordinate * fixing a vector `m : ∀j, M₁ j`, and then modifying its `i`-th coordinate The second way is more artificial as the value of `m` at `i` is not relevant, but it has the advantage of avoiding subtype inclusion issues. This is the definition we use, based on `Function.update` that allows to change the value of `m` at `i`. Note that the use of `Function.update` requires a `DecidableEq ι` term to appear somewhere in the statement of `MultilinearMap.map_update_add'` and `MultilinearMap.map_update_smul'`. Three possible choices are: 1. Requiring `DecidableEq ι` as an argument to `MultilinearMap` (as we did originally). 2. Using `Classical.decEq ι` in the statement of `map_add'` and `map_smul'`. 3. Quantifying over all possible `DecidableEq ι` instances in the statement of `map_add'` and `map_smul'`. Option 1 works fine, but puts unnecessary constraints on the user (the zero map certainly does not need decidability). Option 2 looks great at first, but in the common case when `ι = Fin n` it introduces non-defeq decidability instance diamonds within the context of proving `map_update_add'` and `map_update_smul'`, of the form `Fin.decidableEq n = Classical.decEq (Fin n)`. Option 3 of course does something similar, but of the form `Fin.decidableEq n = _inst`, which is much easier to clean up since `_inst` is a free variable and so the equality can just be substituted. -/ open Fin Function Finset Set universe uR uS uι v v' v₁ v₂ v₃ variable {R : Type uR} {S : Type uS} {ι : Type uι} {n : ℕ} {M : Fin n.succ → Type v} {M₁ : ι → Type v₁} {M₂ : Type v₂} {M₃ : Type v₃} {M' : Type v'} -- Don't generate injectivity lemmas, which the `simpNF` linter will time out on. set_option genInjectivity false in /-- Multilinear maps over the ring `R`, from `∀ i, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R`. -/ structure MultilinearMap (R : Type uR) {ι : Type uι} (M₁ : ι → Type v₁) (M₂ : Type v₂) [Semiring R] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [∀ i, Module R (M₁ i)] [Module R M₂] where /-- The underlying multivariate function of a multilinear map. -/ toFun : (∀ i, M₁ i) → M₂ /-- A multilinear map is additive in every argument. -/ map_update_add' : ∀ [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (x y : M₁ i), toFun (update m i (x + y)) = toFun (update m i x) + toFun (update m i y) /-- A multilinear map is compatible with scalar multiplication in every argument. -/ map_update_smul' : ∀ [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (c : R) (x : M₁ i), toFun (update m i (c • x)) = c • toFun (update m i x) namespace MultilinearMap section Semiring variable [Semiring R] [∀ i, AddCommMonoid (M i)] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [AddCommMonoid M₃] [AddCommMonoid M'] [∀ i, Module R (M i)] [∀ i, Module R (M₁ i)] [Module R M₂] [Module R M₃] [Module R M'] (f f' : MultilinearMap R M₁ M₂) instance : FunLike (MultilinearMap R M₁ M₂) (∀ i, M₁ i) M₂ where coe f := f.toFun coe_injective' f g h := by cases f; cases g; cases h; rfl initialize_simps_projections MultilinearMap (toFun → apply) /-- Constructor for `MultilinearMap R M₁ M₂` when the index type `ι` is already endowed with a `DecidableEq` instance. -/ @[simps] def mk' [DecidableEq ι] (f : (∀ i, M₁ i) → M₂) (h₁ : ∀ (m : ∀ i, M₁ i) (i : ι) (x y : M₁ i), f (update m i (x + y)) = f (update m i x) + f (update m i y) := by aesop) (h₂ : ∀ (m : ∀ i, M₁ i) (i : ι) (c : R) (x : M₁ i), f (update m i (c • x)) = c • f (update m i x) := by aesop) : MultilinearMap R M₁ M₂ where toFun := f map_update_add' m i x y := by convert h₁ m i x y map_update_smul' m i c x := by convert h₂ m i c x @[simp] theorem toFun_eq_coe : f.toFun = ⇑f := rfl @[simp] theorem coe_mk (f : (∀ i, M₁ i) → M₂) (h₁ h₂) : ⇑(⟨f, h₁, h₂⟩ : MultilinearMap R M₁ M₂) = f := rfl theorem congr_fun {f g : MultilinearMap R M₁ M₂} (h : f = g) (x : ∀ i, M₁ i) : f x = g x := DFunLike.congr_fun h x nonrec theorem congr_arg (f : MultilinearMap R M₁ M₂) {x y : ∀ i, M₁ i} (h : x = y) : f x = f y := DFunLike.congr_arg f h theorem coe_injective : Injective ((↑) : MultilinearMap R M₁ M₂ → (∀ i, M₁ i) → M₂) := DFunLike.coe_injective @[norm_cast] theorem coe_inj {f g : MultilinearMap R M₁ M₂} : (f : (∀ i, M₁ i) → M₂) = g ↔ f = g := DFunLike.coe_fn_eq @[ext] theorem ext {f f' : MultilinearMap R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := DFunLike.ext _ _ H @[simp] theorem mk_coe (f : MultilinearMap R M₁ M₂) (h₁ h₂) : (⟨f, h₁, h₂⟩ : MultilinearMap R M₁ M₂) = f := rfl @[simp] protected theorem map_update_add [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.map_update_add' m i x y @[deprecated (since := "2024-11-03")] protected alias map_add := MultilinearMap.map_update_add @[deprecated (since := "2024-11-03")] protected alias map_add' := MultilinearMap.map_update_add /-- Earlier, this name was used by what is now called `MultilinearMap.map_update_smul_left`. -/ @[simp] protected theorem map_update_smul [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.map_update_smul' m i c x @[deprecated (since := "2024-11-03")] protected alias map_smul := MultilinearMap.map_update_smul @[deprecated (since := "2024-11-03")] protected alias map_smul' := MultilinearMap.map_update_smul theorem map_coord_zero {m : ∀ i, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := by classical have : (0 : R) • (0 : M₁ i) = 0 := by simp rw [← update_eq_self i m, h, ← this, f.map_update_smul, zero_smul] @[simp] theorem map_update_zero [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) : f (update m i 0) = 0 := f.map_coord_zero i (update_self i 0 m) @[simp] theorem map_zero [Nonempty ι] : f 0 = 0 := by obtain ⟨i, _⟩ : ∃ i : ι, i ∈ Set.univ := Set.exists_mem_of_nonempty ι exact map_coord_zero f i rfl instance : Add (MultilinearMap R M₁ M₂) := ⟨fun f f' => ⟨fun x => f x + f' x, fun m i x y => by simp [add_left_comm, add_assoc], fun m i c x => by simp [smul_add]⟩⟩ @[simp] theorem add_apply (m : ∀ i, M₁ i) : (f + f') m = f m + f' m := rfl instance : Zero (MultilinearMap R M₁ M₂) := ⟨⟨fun _ => 0, fun _ _ _ _ => by simp, fun _ _ c _ => by simp⟩⟩ instance : Inhabited (MultilinearMap R M₁ M₂) := ⟨0⟩ @[simp] theorem zero_apply (m : ∀ i, M₁ i) : (0 : MultilinearMap R M₁ M₂) m = 0 := rfl section SMul variable [DistribSMul S M₂] [SMulCommClass R S M₂] instance : SMul S (MultilinearMap R M₁ M₂) := ⟨fun c f => ⟨fun m => c • f m, fun m i x y => by simp [smul_add], fun l i x d => by simp [← smul_comm x c (_ : M₂)]⟩⟩ @[simp] theorem smul_apply (f : MultilinearMap R M₁ M₂) (c : S) (m : ∀ i, M₁ i) : (c • f) m = c • f m := rfl theorem coe_smul (c : S) (f : MultilinearMap R M₁ M₂) : ⇑(c • f) = c • (⇑ f) := rfl end SMul instance addCommMonoid : AddCommMonoid (MultilinearMap R M₁ M₂) := coe_injective.addCommMonoid _ rfl (fun _ _ => rfl) fun _ _ => rfl /-- Coercion of a multilinear map to a function as an additive monoid homomorphism. -/ @[simps] def coeAddMonoidHom : MultilinearMap R M₁ M₂ →+ (((i : ι) → M₁ i) → M₂) where toFun := DFunLike.coe; map_zero' := rfl; map_add' _ _ := rfl @[simp] theorem coe_sum {α : Type*} (f : α → MultilinearMap R M₁ M₂) (s : Finset α) : ⇑(∑ a ∈ s, f a) = ∑ a ∈ s, ⇑(f a) := map_sum coeAddMonoidHom f s theorem sum_apply {α : Type*} (f : α → MultilinearMap R M₁ M₂) (m : ∀ i, M₁ i) {s : Finset α} : (∑ a ∈ s, f a) m = ∑ a ∈ s, f a m := by simp /-- If `f` is a multilinear map, then `f.toLinearMap m i` is the linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ @[simps] def toLinearMap [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ where toFun x := f (update m i x) map_add' x y := by simp map_smul' c x := by simp /-- The cartesian product of two multilinear maps, as a multilinear map. -/ @[simps] def prod (f : MultilinearMap R M₁ M₂) (g : MultilinearMap R M₁ M₃) : MultilinearMap R M₁ (M₂ × M₃) where toFun m := (f m, g m) map_update_add' m i x y := by simp map_update_smul' m i c x := by simp /-- Combine a family of multilinear maps with the same domain and codomains `M' i` into a multilinear map taking values in the space of functions `∀ i, M' i`. -/ @[simps] def pi {ι' : Type*} {M' : ι' → Type*} [∀ i, AddCommMonoid (M' i)] [∀ i, Module R (M' i)] (f : ∀ i, MultilinearMap R M₁ (M' i)) : MultilinearMap R M₁ (∀ i, M' i) where toFun m i := f i m map_update_add' _ _ _ _ := funext fun j => (f j).map_update_add _ _ _ _ map_update_smul' _ _ _ _ := funext fun j => (f j).map_update_smul _ _ _ _ section variable (R M₂ M₃) /-- Equivalence between linear maps `M₂ →ₗ[R] M₃` and one-multilinear maps. -/ @[simps] def ofSubsingleton [Subsingleton ι] (i : ι) : (M₂ →ₗ[R] M₃) ≃ MultilinearMap R (fun _ : ι ↦ M₂) M₃ where toFun f := { toFun := fun x ↦ f (x i) map_update_add' := by intros; simp [update_eq_const_of_subsingleton] map_update_smul' := by intros; simp [update_eq_const_of_subsingleton] } invFun f := { toFun := fun x ↦ f fun _ ↦ x map_add' := fun x y ↦ by simpa [update_eq_const_of_subsingleton] using f.map_update_add 0 i x y map_smul' := fun c x ↦ by simpa [update_eq_const_of_subsingleton] using f.map_update_smul 0 i c x } left_inv _ := rfl right_inv f := by ext x; refine congr_arg f ?_; exact (eq_const_of_subsingleton _ _).symm variable (M₁) {M₂} /-- The constant map is multilinear when `ι` is empty. -/ @[simps -fullyApplied] def constOfIsEmpty [IsEmpty ι] (m : M₂) : MultilinearMap R M₁ M₂ where toFun := Function.const _ m map_update_add' _ := isEmptyElim map_update_smul' _ := isEmptyElim end /-- Given a multilinear map `f` on `n` variables (parameterized by `Fin n`) and a subset `s` of `k` of these variables, one gets a new multilinear map on `Fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `Fin k` and `s` that we use is the canonical (increasing) bijection. -/ def restr {k n : ℕ} (f : MultilinearMap R (fun _ : Fin n => M') M₂) (s : Finset (Fin n)) (hk : #s = k) (z : M') : MultilinearMap R (fun _ : Fin k => M') M₂ where toFun v := f fun j => if h : j ∈ s then v ((s.orderIsoOfFin hk).symm ⟨j, h⟩) else z /- Porting note: The proofs of the following two lemmas used to only use `erw` followed by `simp`, but it seems `erw` no longer unfolds or unifies well enough to work without more help. -/ map_update_add' v i x y := by erw [dite_comp_equiv_update (s.orderIsoOfFin hk).toEquiv, dite_comp_equiv_update (s.orderIsoOfFin hk).toEquiv, dite_comp_equiv_update (s.orderIsoOfFin hk).toEquiv] simp map_update_smul' v i c x := by erw [dite_comp_equiv_update (s.orderIsoOfFin hk).toEquiv, dite_comp_equiv_update (s.orderIsoOfFin hk).toEquiv] simp /-- In the specific case of multilinear maps on spaces indexed by `Fin (n+1)`, where one can build an element of `∀ (i : Fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ theorem cons_add (f : MultilinearMap R M M₂) (m : ∀ i : Fin n, M i.succ) (x y : M 0) : f (cons (x + y) m) = f (cons x m) + f (cons y m) := by simp_rw [← update_cons_zero x m (x + y), f.map_update_add, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `Fin (n+1)`, where one can build an element of `∀ (i : Fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ theorem cons_smul (f : MultilinearMap R M M₂) (m : ∀ i : Fin n, M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := by simp_rw [← update_cons_zero x m (c • x), f.map_update_smul, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `Fin (n+1)`, where one can build an element of `∀ (i : Fin (n+1)), M i` using `snoc`, one can express directly the additivity of a multilinear map along the first variable. -/ theorem snoc_add (f : MultilinearMap R M M₂) (m : ∀ i : Fin n, M (castSucc i)) (x y : M (last n)) : f (snoc m (x + y)) = f (snoc m x) + f (snoc m y) := by simp_rw [← update_snoc_last x m (x + y), f.map_update_add, update_snoc_last] /-- In the specific case of multilinear maps on spaces indexed by `Fin (n+1)`, where one can build an element of `∀ (i : Fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ theorem snoc_smul (f : MultilinearMap R M M₂) (m : ∀ i : Fin n, M (castSucc i)) (c : R) (x : M (last n)) : f (snoc m (c • x)) = c • f (snoc m x) := by simp_rw [← update_snoc_last x m (c • x), f.map_update_smul, update_snoc_last] section variable {M₁' : ι → Type*} [∀ i, AddCommMonoid (M₁' i)] [∀ i, Module R (M₁' i)] variable {M₁'' : ι → Type*} [∀ i, AddCommMonoid (M₁'' i)] [∀ i, Module R (M₁'' i)] /-- If `g` is a multilinear map and `f` is a collection of linear maps, then `g (f₁ m₁, ..., fₙ mₙ)` is again a multilinear map, that we call `g.compLinearMap f`. -/ def compLinearMap (g : MultilinearMap R M₁' M₂) (f : ∀ i, M₁ i →ₗ[R] M₁' i) : MultilinearMap R M₁ M₂ where toFun m := g fun i => f i (m i) map_update_add' m i x y := by have : ∀ j z, f j (update m i z j) = update (fun k => f k (m k)) i (f i z) j := fun j z => Function.apply_update (fun k => f k) _ _ _ _ simp [this] map_update_smul' m i c x := by have : ∀ j z, f j (update m i z j) = update (fun k => f k (m k)) i (f i z) j := fun j z => Function.apply_update (fun k => f k) _ _ _ _ simp [this] @[simp] theorem compLinearMap_apply (g : MultilinearMap R M₁' M₂) (f : ∀ i, M₁ i →ₗ[R] M₁' i) (m : ∀ i, M₁ i) : g.compLinearMap f m = g fun i => f i (m i) := rfl /-- Composing a multilinear map twice with a linear map in each argument is the same as composing with their composition. -/ theorem compLinearMap_assoc (g : MultilinearMap R M₁'' M₂) (f₁ : ∀ i, M₁' i →ₗ[R] M₁'' i) (f₂ : ∀ i, M₁ i →ₗ[R] M₁' i) : (g.compLinearMap f₁).compLinearMap f₂ = g.compLinearMap fun i => f₁ i ∘ₗ f₂ i := rfl /-- Composing the zero multilinear map with a linear map in each argument. -/ @[simp] theorem zero_compLinearMap (f : ∀ i, M₁ i →ₗ[R] M₁' i) : (0 : MultilinearMap R M₁' M₂).compLinearMap f = 0 := ext fun _ => rfl /-- Composing a multilinear map with the identity linear map in each argument. -/ @[simp] theorem compLinearMap_id (g : MultilinearMap R M₁' M₂) : (g.compLinearMap fun _ => LinearMap.id) = g := ext fun _ => rfl /-- Composing with a family of surjective linear maps is injective. -/ theorem compLinearMap_injective (f : ∀ i, M₁ i →ₗ[R] M₁' i) (hf : ∀ i, Surjective (f i)) : Injective fun g : MultilinearMap R M₁' M₂ => g.compLinearMap f := fun g₁ g₂ h => ext fun x => by simpa [fun i => surjInv_eq (hf i)] using MultilinearMap.ext_iff.mp h fun i => surjInv (hf i) (x i) theorem compLinearMap_inj (f : ∀ i, M₁ i →ₗ[R] M₁' i) (hf : ∀ i, Surjective (f i)) (g₁ g₂ : MultilinearMap R M₁' M₂) : g₁.compLinearMap f = g₂.compLinearMap f ↔ g₁ = g₂ := (compLinearMap_injective _ hf).eq_iff /-- Composing a multilinear map with a linear equiv on each argument gives the zero map if and only if the multilinear map is the zero map. -/ @[simp] theorem comp_linearEquiv_eq_zero_iff (g : MultilinearMap R M₁' M₂) (f : ∀ i, M₁ i ≃ₗ[R] M₁' i) : (g.compLinearMap fun i => (f i : M₁ i →ₗ[R] M₁' i)) = 0 ↔ g = 0 := by set f' := fun i => (f i : M₁ i →ₗ[R] M₁' i) rw [← zero_compLinearMap f', compLinearMap_inj f' fun i => (f i).surjective] end /-- If one adds to a vector `m'` another vector `m`, but only for coordinates in a finset `t`, then the image under a multilinear map `f` is the sum of `f (s.piecewise m m')` along all subsets `s` of `t`. This is mainly an auxiliary statement to prove the result when `t = univ`, given in `map_add_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite. -/ theorem map_piecewise_add [DecidableEq ι] (m m' : ∀ i, M₁ i) (t : Finset ι) : f (t.piecewise (m + m') m') = ∑ s ∈ t.powerset, f (s.piecewise m m') := by revert m' refine Finset.induction_on t (by simp) ?_ intro i t hit Hrec m' have A : (insert i t).piecewise (m + m') m' = update (t.piecewise (m + m') m') i (m i + m' i) := t.piecewise_insert _ _ _ have B : update (t.piecewise (m + m') m') i (m' i) = t.piecewise (m + m') m' := by ext j by_cases h : j = i · rw [h] simp [hit] · simp [h] let m'' := update m' i (m i) have C : update (t.piecewise (m + m') m') i (m i) = t.piecewise (m + m'') m'' := by ext j by_cases h : j = i · rw [h] simp [m'', hit] · by_cases h' : j ∈ t <;> simp [m'', h, hit, h'] rw [A, f.map_update_add, B, C, Finset.sum_powerset_insert hit, Hrec, Hrec, add_comm (_ : M₂)] congr 1 refine Finset.sum_congr rfl fun s hs => ?_ have : (insert i s).piecewise m m' = s.piecewise m m'' := by ext j by_cases h : j = i · rw [h] simp [m'', Finset.not_mem_of_mem_powerset_of_not_mem hs hit] · by_cases h' : j ∈ s <;> simp [m'', h, h'] rw [this] /-- Additivity of a multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ theorem map_add_univ [DecidableEq ι] [Fintype ι] (m m' : ∀ i, M₁ i) : f (m + m') = ∑ s : Finset ι, f (s.piecewise m m') := by simpa using f.map_piecewise_add m m' Finset.univ section ApplySum variable {α : ι → Type*} (g : ∀ i, α i → M₁ i) (A : ∀ i, Finset (α i)) open Fintype Finset /-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. Here, we give an auxiliary statement tailored for an inductive proof. Use instead `map_sum_finset`. -/ theorem map_sum_finset_aux [DecidableEq ι] [Fintype ι] {n : ℕ} (h : (∑ i, #(A i)) = n) : (f fun i => ∑ j ∈ A i, g i j) = ∑ r ∈ piFinset A, f fun i => g i (r i) := by letI := fun i => Classical.decEq (α i) induction n using Nat.strong_induction_on generalizing A with | h n IH => -- If one of the sets is empty, then all the sums are zero by_cases Ai_empty : ∃ i, A i = ∅ · obtain ⟨i, hi⟩ : ∃ i, ∑ j ∈ A i, g i j = 0 := Ai_empty.imp fun i hi ↦ by simp [hi] have hpi : piFinset A = ∅ := by simpa rw [f.map_coord_zero i hi, hpi, Finset.sum_empty] push_neg at Ai_empty -- Otherwise, if all sets are at most singletons, then they are exactly singletons and the result -- is again straightforward by_cases Ai_singleton : ∀ i, #(A i) ≤ 1 · have Ai_card : ∀ i, #(A i) = 1 := by intro i have pos : #(A i) ≠ 0 := by simp [Finset.card_eq_zero, Ai_empty i] have : #(A i) ≤ 1 := Ai_singleton i exact le_antisymm this (Nat.succ_le_of_lt (_root_.pos_iff_ne_zero.mpr pos)) have : ∀ r : ∀ i, α i, r ∈ piFinset A → (f fun i => g i (r i)) = f fun i => ∑ j ∈ A i, g i j := by intro r hr congr with i have : ∀ j ∈ A i, g i j = g i (r i) := by intro j hj congr apply Finset.card_le_one_iff.1 (Ai_singleton i) hj exact mem_piFinset.mp hr i simp only [Finset.sum_congr rfl this, Finset.mem_univ, Finset.sum_const, Ai_card i, one_nsmul] simp only [Finset.sum_congr rfl this, Ai_card, card_piFinset, prod_const_one, one_nsmul, Finset.sum_const] -- Remains the interesting case where one of the `A i`, say `A i₀`, has cardinality at least 2. -- We will split into two parts `B i₀` and `C i₀` of smaller cardinality, let `B i = C i = A i` -- for `i ≠ i₀`, apply the inductive assumption to `B` and `C`, and add up the corresponding -- parts to get the sum for `A`. push_neg at Ai_singleton obtain ⟨i₀, hi₀⟩ : ∃ i, 1 < #(A i) := Ai_singleton obtain ⟨j₁, j₂, _, hj₂, _⟩ : ∃ j₁ j₂, j₁ ∈ A i₀ ∧ j₂ ∈ A i₀ ∧ j₁ ≠ j₂ := Finset.one_lt_card_iff.1 hi₀ let B := Function.update A i₀ (A i₀ \ {j₂}) let C := Function.update A i₀ {j₂} have B_subset_A : ∀ i, B i ⊆ A i := by intro i by_cases hi : i = i₀ · rw [hi] simp only [B, sdiff_subset, update_self] · simp only [B, hi, update_of_ne, Ne, not_false_iff, Finset.Subset.refl] have C_subset_A : ∀ i, C i ⊆ A i := by intro i by_cases hi : i = i₀ · rw [hi] simp only [C, hj₂, Finset.singleton_subset_iff, update_self] · simp only [C, hi, update_of_ne, Ne, not_false_iff, Finset.Subset.refl] -- split the sum at `i₀` as the sum over `B i₀` plus the sum over `C i₀`, to use additivity. have A_eq_BC : (fun i => ∑ j ∈ A i, g i j) = Function.update (fun i => ∑ j ∈ A i, g i j) i₀ ((∑ j ∈ B i₀, g i₀ j) + ∑ j ∈ C i₀, g i₀ j) := by ext i by_cases hi : i = i₀ · rw [hi, update_self] have : A i₀ = B i₀ ∪ C i₀ := by simp only [B, C, Function.update_self, Finset.sdiff_union_self_eq_union] symm simp only [hj₂, Finset.singleton_subset_iff, Finset.union_eq_left] rw [this] refine Finset.sum_union <| Finset.disjoint_right.2 fun j hj => ?_ have : j = j₂ := by simpa [C] using hj rw [this] simp only [B, mem_sdiff, eq_self_iff_true, not_true, not_false_iff, Finset.mem_singleton, update_self, and_false] · simp [hi] have Beq : Function.update (fun i => ∑ j ∈ A i, g i j) i₀ (∑ j ∈ B i₀, g i₀ j) = fun i => ∑ j ∈ B i, g i j := by ext i by_cases hi : i = i₀ · rw [hi] simp only [update_self] · simp only [B, hi, update_of_ne, Ne, not_false_iff] have Ceq : Function.update (fun i => ∑ j ∈ A i, g i j) i₀ (∑ j ∈ C i₀, g i₀ j) = fun i => ∑ j ∈ C i, g i j := by ext i by_cases hi : i = i₀ · rw [hi] simp only [update_self] · simp only [C, hi, update_of_ne, Ne, not_false_iff] -- Express the inductive assumption for `B` have Brec : (f fun i => ∑ j ∈ B i, g i j) = ∑ r ∈ piFinset B, f fun i => g i (r i) := by have : ∑ i, #(B i) < ∑ i, #(A i) := by refine sum_lt_sum (fun i _ => card_le_card (B_subset_A i)) ⟨i₀, mem_univ _, ?_⟩ have : {j₂} ⊆ A i₀ := by simp [hj₂] simp only [B, Finset.card_sdiff this, Function.update_self, Finset.card_singleton] exact Nat.pred_lt (ne_of_gt (lt_trans Nat.zero_lt_one hi₀)) rw [h] at this exact IH _ this B rfl -- Express the inductive assumption for `C` have Crec : (f fun i => ∑ j ∈ C i, g i j) = ∑ r ∈ piFinset C, f fun i => g i (r i) := by have : (∑ i, #(C i)) < ∑ i, #(A i) := Finset.sum_lt_sum (fun i _ => Finset.card_le_card (C_subset_A i)) ⟨i₀, Finset.mem_univ _, by simp [C, hi₀]⟩ rw [h] at this exact IH _ this C rfl have D : Disjoint (piFinset B) (piFinset C) := haveI : Disjoint (B i₀) (C i₀) := by simp [B, C] piFinset_disjoint_of_disjoint B C this have pi_BC : piFinset A = piFinset B ∪ piFinset C := by apply Finset.Subset.antisymm · intro r hr by_cases hri₀ : r i₀ = j₂ · apply Finset.mem_union_right refine mem_piFinset.2 fun i => ?_ by_cases hi : i = i₀ · have : r i₀ ∈ C i₀ := by simp [C, hri₀] rwa [hi] · simp [C, hi, mem_piFinset.1 hr i] · apply Finset.mem_union_left refine mem_piFinset.2 fun i => ?_ by_cases hi : i = i₀ · have : r i₀ ∈ B i₀ := by simp [B, hri₀, mem_piFinset.1 hr i₀] rwa [hi] · simp [B, hi, mem_piFinset.1 hr i] · exact Finset.union_subset (piFinset_subset _ _ fun i => B_subset_A i) (piFinset_subset _ _ fun i => C_subset_A i) rw [A_eq_BC] simp only [MultilinearMap.map_update_add, Beq, Ceq, Brec, Crec, pi_BC] rw [← Finset.sum_union D] /-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ theorem map_sum_finset [DecidableEq ι] [Fintype ι] : (f fun i => ∑ j ∈ A i, g i j) = ∑ r ∈ piFinset A, f fun i => g i (r i) := f.map_sum_finset_aux _ _ rfl /-- If `f` is multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ theorem map_sum [DecidableEq ι] [Fintype ι] [∀ i, Fintype (α i)] : (f fun i => ∑ j, g i j) = ∑ r : ∀ i, α i, f fun i => g i (r i) := f.map_sum_finset g fun _ => Finset.univ theorem map_update_sum {α : Type*} [DecidableEq ι] (t : Finset α) (i : ι) (g : α → M₁ i) (m : ∀ i, M₁ i) : f (update m i (∑ a ∈ t, g a)) = ∑ a ∈ t, f (update m i (g a)) := by classical induction t using Finset.induction with | empty => simp | insert _ _ has ih => simp [Finset.sum_insert has, ih] end ApplySum /-- Restrict the codomain of a multilinear map to a submodule. This is the multilinear version of `LinearMap.codRestrict`. -/ @[simps] def codRestrict (f : MultilinearMap R M₁ M₂) (p : Submodule R M₂) (h : ∀ v, f v ∈ p) : MultilinearMap R M₁ p where toFun v := ⟨f v, h v⟩ map_update_add' _ _ _ _ := Subtype.ext <| MultilinearMap.map_update_add _ _ _ _ _ map_update_smul' _ _ _ _ := Subtype.ext <| MultilinearMap.map_update_smul _ _ _ _ _ section RestrictScalar variable (R) variable {A : Type*} [Semiring A] [SMul R A] [∀ i : ι, Module A (M₁ i)] [Module A M₂] [∀ i, IsScalarTower R A (M₁ i)] [IsScalarTower R A M₂] /-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R` and their actions on all involved modules agree with the action of `R` on `A`. -/ def restrictScalars (f : MultilinearMap A M₁ M₂) : MultilinearMap R M₁ M₂ where toFun := f map_update_add' := f.map_update_add map_update_smul' m i := (f.toLinearMap m i).map_smul_of_tower @[simp] theorem coe_restrictScalars (f : MultilinearMap A M₁ M₂) : ⇑(f.restrictScalars R) = f := rfl end RestrictScalar section variable {ι₁ ι₂ ι₃ : Type*} /-- Transfer the arguments to a map along an equivalence between argument indices. The naming is derived from `Finsupp.domCongr`, noting that here the permutation applies to the domain of the domain. -/ @[simps apply] def domDomCongr (σ : ι₁ ≃ ι₂) (m : MultilinearMap R (fun _ : ι₁ => M₂) M₃) : MultilinearMap R (fun _ : ι₂ => M₂) M₃ where toFun v := m fun i => v (σ i) map_update_add' v i a b := by letI := σ.injective.decidableEq simp_rw [Function.update_apply_equiv_apply v] rw [m.map_update_add] map_update_smul' v i a b := by letI := σ.injective.decidableEq simp_rw [Function.update_apply_equiv_apply v] rw [m.map_update_smul] theorem domDomCongr_trans (σ₁ : ι₁ ≃ ι₂) (σ₂ : ι₂ ≃ ι₃) (m : MultilinearMap R (fun _ : ι₁ => M₂) M₃) : m.domDomCongr (σ₁.trans σ₂) = (m.domDomCongr σ₁).domDomCongr σ₂ := rfl theorem domDomCongr_mul (σ₁ : Equiv.Perm ι₁) (σ₂ : Equiv.Perm ι₁) (m : MultilinearMap R (fun _ : ι₁ => M₂) M₃) : m.domDomCongr (σ₂ * σ₁) = (m.domDomCongr σ₁).domDomCongr σ₂ := rfl /-- `MultilinearMap.domDomCongr` as an equivalence. This is declared separately because it does not work with dot notation. -/ @[simps apply symm_apply] def domDomCongrEquiv (σ : ι₁ ≃ ι₂) : MultilinearMap R (fun _ : ι₁ => M₂) M₃ ≃+ MultilinearMap R (fun _ : ι₂ => M₂) M₃ where toFun := domDomCongr σ invFun := domDomCongr σ.symm left_inv m := by ext simp [domDomCongr] right_inv m := by ext simp [domDomCongr] map_add' a b := by ext simp [domDomCongr] /-- The results of applying `domDomCongr` to two maps are equal if and only if those maps are. -/ @[simp] theorem domDomCongr_eq_iff (σ : ι₁ ≃ ι₂) (f g : MultilinearMap R (fun _ : ι₁ => M₂) M₃) : f.domDomCongr σ = g.domDomCongr σ ↔ f = g := (domDomCongrEquiv σ : _ ≃+ MultilinearMap R (fun _ => M₂) M₃).apply_eq_iff_eq end /-! If `{a // P a}` is a subtype of `ι` and if we fix an element `z` of `(i : {a // ¬ P a}) → M₁ i`, then a multilinear map on `M₁` defines a multilinear map on the restriction of `M₁` to `{a // P a}`, by fixing the arguments out of `{a // P a}` equal to the values of `z`. -/ lemma domDomRestrict_aux {ι} [DecidableEq ι] (P : ι → Prop) [DecidablePred P] {M₁ : ι → Type*} [DecidableEq {a // P a}] (x : (i : {a // P a}) → M₁ i) (z : (i : {a // ¬ P a}) → M₁ i) (i : {a : ι // P a}) (c : M₁ i) : (fun j ↦ if h : P j then Function.update x i c ⟨j, h⟩ else z ⟨j, h⟩) = Function.update (fun j => if h : P j then x ⟨j, h⟩ else z ⟨j, h⟩) i c := by ext j by_cases h : j = i · rw [h, Function.update_self] simp only [i.2, update_self, dite_true] · rw [Function.update_of_ne h] by_cases h' : P j · simp only [h', ne_eq, Subtype.mk.injEq, dite_true] have h'' : ¬ ⟨j, h'⟩ = i := fun he => by apply_fun (fun x => x.1) at he; exact h he rw [Function.update_of_ne h''] · simp only [h', ne_eq, Subtype.mk.injEq, dite_false] lemma domDomRestrict_aux_right {ι} [DecidableEq ι] (P : ι → Prop) [DecidablePred P] {M₁ : ι → Type*} [DecidableEq {a // ¬ P a}] (x : (i : {a // P a}) → M₁ i) (z : (i : {a // ¬ P a}) → M₁ i) (i : {a : ι // ¬ P a}) (c : M₁ i) : (fun j ↦ if h : P j then x ⟨j, h⟩ else Function.update z i c ⟨j, h⟩) = Function.update (fun j => if h : P j then x ⟨j, h⟩ else z ⟨j, h⟩) i c := by simpa only [dite_not] using domDomRestrict_aux _ z (fun j ↦ x ⟨j.1, not_not.mp j.2⟩) i c /-- Given a multilinear map `f` on `(i : ι) → M i`, a (decidable) predicate `P` on `ι` and an element `z` of `(i : {a // ¬ P a}) → M₁ i`, construct a multilinear map on `(i : {a // P a}) → M₁ i)` whose value at `x` is `f` evaluated at the vector with `i`th coordinate `x i` if `P i` and `z i` otherwise. The naming is similar to `MultilinearMap.domDomCongr`: here we are applying the restriction to the domain of the domain. For a linear map version, see `MultilinearMap.domDomRestrictₗ`. -/ def domDomRestrict (f : MultilinearMap R M₁ M₂) (P : ι → Prop) [DecidablePred P] (z : (i : {a : ι // ¬ P a}) → M₁ i) : MultilinearMap R (fun (i : {a : ι // P a}) => M₁ i) M₂ where toFun x := f (fun j ↦ if h : P j then x ⟨j, h⟩ else z ⟨j, h⟩) map_update_add' x i a b := by classical repeat (rw [domDomRestrict_aux]) simp only [MultilinearMap.map_update_add] map_update_smul' z i c a := by classical repeat (rw [domDomRestrict_aux]) simp only [MultilinearMap.map_update_smul] @[simp] lemma domDomRestrict_apply (f : MultilinearMap R M₁ M₂) (P : ι → Prop) [DecidablePred P] (x : (i : {a // P a}) → M₁ i) (z : (i : {a // ¬ P a}) → M₁ i) : f.domDomRestrict P z x = f (fun j => if h : P j then x ⟨j, h⟩ else z ⟨j, h⟩) := rfl -- TODO: Should add a ref here when available. /-- The "derivative" of a multilinear map, as a linear map from `(i : ι) → M₁ i` to `M₂`. For continuous multilinear maps, this will indeed be the derivative. -/ def linearDeriv [DecidableEq ι] [Fintype ι] (f : MultilinearMap R M₁ M₂) (x : (i : ι) → M₁ i) : ((i : ι) → M₁ i) →ₗ[R] M₂ := ∑ i : ι, (f.toLinearMap x i).comp (LinearMap.proj i) @[simp] lemma linearDeriv_apply [DecidableEq ι] [Fintype ι] (f : MultilinearMap R M₁ M₂) (x y : (i : ι) → M₁ i) : f.linearDeriv x y = ∑ i, f (update x i (y i)) := by unfold linearDeriv simp only [LinearMap.coeFn_sum, LinearMap.coe_comp, LinearMap.coe_proj, Finset.sum_apply, Function.comp_apply, Function.eval, toLinearMap_apply] end Semiring end MultilinearMap namespace LinearMap variable [Semiring R] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [AddCommMonoid M₃] [AddCommMonoid M'] [∀ i, Module R (M₁ i)] [Module R M₂] [Module R M₃] [Module R M'] /-- Composing a multilinear map with a linear map gives again a multilinear map. -/ def compMultilinearMap (g : M₂ →ₗ[R] M₃) (f : MultilinearMap R M₁ M₂) : MultilinearMap R M₁ M₃ where toFun := g ∘ f map_update_add' m i x y := by simp map_update_smul' m i c x := by simp @[simp] theorem coe_compMultilinearMap (g : M₂ →ₗ[R] M₃) (f : MultilinearMap R M₁ M₂) : ⇑(g.compMultilinearMap f) = g ∘ f := rfl @[simp] theorem compMultilinearMap_apply (g : M₂ →ₗ[R] M₃) (f : MultilinearMap R M₁ M₂) (m : ∀ i, M₁ i) : g.compMultilinearMap f m = g (f m) := rfl @[simp] theorem compMultilinearMap_zero (g : M₂ →ₗ[R] M₃) : g.compMultilinearMap (0 : MultilinearMap R M₁ M₂) = 0 := MultilinearMap.ext fun _ => map_zero g @[simp] theorem zero_compMultilinearMap (f : MultilinearMap R M₁ M₂) : (0 : M₂ →ₗ[R] M₃).compMultilinearMap f = 0 := rfl @[simp] theorem compMultilinearMap_add (g : M₂ →ₗ[R] M₃) (f₁ f₂ : MultilinearMap R M₁ M₂) : g.compMultilinearMap (f₁ + f₂) = g.compMultilinearMap f₁ + g.compMultilinearMap f₂ := MultilinearMap.ext fun _ => map_add g _ _ @[simp] theorem add_compMultilinearMap (g₁ g₂ : M₂ →ₗ[R] M₃) (f : MultilinearMap R M₁ M₂) : (g₁ + g₂).compMultilinearMap f = g₁.compMultilinearMap f + g₂.compMultilinearMap f := rfl @[simp] theorem compMultilinearMap_smul [DistribSMul S M₂] [DistribSMul S M₃] [SMulCommClass R S M₂] [SMulCommClass R S M₃] [CompatibleSMul M₂ M₃ S R] (g : M₂ →ₗ[R] M₃) (s : S) (f : MultilinearMap R M₁ M₂) : g.compMultilinearMap (s • f) = s • g.compMultilinearMap f := MultilinearMap.ext fun _ => g.map_smul_of_tower _ _ @[simp] theorem smul_compMultilinearMap [Monoid S] [DistribMulAction S M₃] [SMulCommClass R S M₃] (g : M₂ →ₗ[R] M₃) (s : S) (f : MultilinearMap R M₁ M₂) : (s • g).compMultilinearMap f = s • g.compMultilinearMap f := rfl /-- The multilinear version of `LinearMap.subtype_comp_codRestrict` -/ @[simp] theorem subtype_compMultilinearMap_codRestrict (f : MultilinearMap R M₁ M₂) (p : Submodule R M₂) (h) : p.subtype.compMultilinearMap (f.codRestrict p h) = f := rfl /-- The multilinear version of `LinearMap.comp_codRestrict` -/ @[simp] theorem compMultilinearMap_codRestrict (g : M₂ →ₗ[R] M₃) (f : MultilinearMap R M₁ M₂) (p : Submodule R M₃) (h) : (g.codRestrict p h).compMultilinearMap f = (g.compMultilinearMap f).codRestrict p fun v => h (f v) := rfl variable {ι₁ ι₂ : Type*} @[simp] theorem compMultilinearMap_domDomCongr (σ : ι₁ ≃ ι₂) (g : M₂ →ₗ[R] M₃) (f : MultilinearMap R (fun _ : ι₁ => M') M₂) : (g.compMultilinearMap f).domDomCongr σ = g.compMultilinearMap (f.domDomCongr σ) := by ext simp [MultilinearMap.domDomCongr] end LinearMap namespace MultilinearMap section Semiring variable [Semiring R] [(i : ι) → AddCommMonoid (M₁ i)] [(i : ι) → Module R (M₁ i)] [AddCommMonoid M₂] [Module R M₂] instance [Monoid S] [DistribMulAction S M₂] [Module R M₂] [SMulCommClass R S M₂] : DistribMulAction S (MultilinearMap R M₁ M₂) := coe_injective.distribMulAction coeAddMonoidHom fun _ _ ↦ rfl section Module variable [Semiring S] [Module S M₂] [SMulCommClass R S M₂] /-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : Module S (MultilinearMap R M₁ M₂) := coe_injective.module _ coeAddMonoidHom fun _ _ ↦ rfl instance [NoZeroSMulDivisors S M₂] : NoZeroSMulDivisors S (MultilinearMap R M₁ M₂) := coe_injective.noZeroSMulDivisors _ rfl coe_smul variable [AddCommMonoid M₃] [Module S M₃] [Module R M₃] [SMulCommClass R S M₃] variable (S) in /-- `LinearMap.compMultilinearMap` as an `S`-linear map. -/ @[simps] def _root_.LinearMap.compMultilinearMapₗ [Semiring S] [Module S M₂] [Module S M₃] [SMulCommClass R S M₂] [SMulCommClass R S M₃] [LinearMap.CompatibleSMul M₂ M₃ S R] (g : M₂ →ₗ[R] M₃) : MultilinearMap R M₁ M₂ →ₗ[S] MultilinearMap R M₁ M₃ where toFun := g.compMultilinearMap map_add' := g.compMultilinearMap_add map_smul' := g.compMultilinearMap_smul variable (R S M₁ M₂ M₃) section OfSubsingleton /-- Linear equivalence between linear maps `M₂ →ₗ[R] M₃` and one-multilinear maps `MultilinearMap R (fun _ : ι ↦ M₂) M₃`. -/ @[simps +simpRhs] def ofSubsingletonₗ [Subsingleton ι] (i : ι) : (M₂ →ₗ[R] M₃) ≃ₗ[S] MultilinearMap R (fun _ : ι ↦ M₂) M₃ := { ofSubsingleton R M₂ M₃ i with map_add' := fun _ _ ↦ rfl map_smul' := fun _ _ ↦ rfl } end OfSubsingleton /-- The dependent version of `MultilinearMap.domDomCongrLinearEquiv`. -/ @[simps apply symm_apply] def domDomCongrLinearEquiv' {ι' : Type*} (σ : ι ≃ ι') : MultilinearMap R M₁ M₂ ≃ₗ[S] MultilinearMap R (fun i => M₁ (σ.symm i)) M₂ where toFun f := { toFun := f ∘ (σ.piCongrLeft' M₁).symm map_update_add' := fun m i => by letI := σ.decidableEq rw [← σ.apply_symm_apply i] intro x y simp only [comp_apply, piCongrLeft'_symm_update, f.map_update_add] map_update_smul' := fun m i c => by letI := σ.decidableEq rw [← σ.apply_symm_apply i] intro x simp only [Function.comp, piCongrLeft'_symm_update, f.map_update_smul] } invFun f := { toFun := f ∘ σ.piCongrLeft' M₁ map_update_add' := fun m i => by letI := σ.symm.decidableEq rw [← σ.symm_apply_apply i] intro x y simp only [comp_apply, piCongrLeft'_update, f.map_update_add] map_update_smul' := fun m i c => by letI := σ.symm.decidableEq rw [← σ.symm_apply_apply i] intro x simp only [Function.comp, piCongrLeft'_update, f.map_update_smul] } map_add' f₁ f₂ := by ext simp only [Function.comp, coe_mk, add_apply] map_smul' c f := by ext simp only [Function.comp, coe_mk, smul_apply, RingHom.id_apply] left_inv f := by ext simp only [coe_mk, comp_apply, Equiv.symm_apply_apply] right_inv f := by ext simp only [coe_mk, comp_apply, Equiv.apply_symm_apply] /-- The space of constant maps is equivalent to the space of maps that are multilinear with respect to an empty family. -/ @[simps] def constLinearEquivOfIsEmpty [IsEmpty ι] : M₂ ≃ₗ[S] MultilinearMap R M₁ M₂ where toFun := MultilinearMap.constOfIsEmpty R _ map_add' _ _ := rfl map_smul' _ _ := rfl invFun f := f 0 left_inv _ := rfl right_inv f := ext fun _ => MultilinearMap.congr_arg f <| Subsingleton.elim _ _ /-- `MultilinearMap.domDomCongr` as a `LinearEquiv`. -/ @[simps apply symm_apply] def domDomCongrLinearEquiv {ι₁ ι₂} (σ : ι₁ ≃ ι₂) : MultilinearMap R (fun _ : ι₁ => M₂) M₃ ≃ₗ[S] MultilinearMap R (fun _ : ι₂ => M₂) M₃ := { (domDomCongrEquiv σ : MultilinearMap R (fun _ : ι₁ => M₂) M₃ ≃+ MultilinearMap R (fun _ : ι₂ => M₂) M₃) with map_smul' := fun c f => by ext simp [MultilinearMap.domDomCongr] } end Module end Semiring section CommSemiring variable [CommSemiring R] [∀ i, AddCommMonoid (M₁ i)] [∀ i, AddCommMonoid (M i)] [AddCommMonoid M₂] [∀ i, Module R (M i)] [∀ i, Module R (M₁ i)] [Module R M₂] (f f' : MultilinearMap R M₁ M₂) section variable {M₁' : ι → Type*} [Π i, AddCommMonoid (M₁' i)] [Π i, Module R (M₁' i)] /-- Given a predicate `P`, one may associate to a multilinear map `f` a multilinear map from the elements satisfying `P` to the multilinear maps on elements not satisfying `P`. In other words, splitting the variables into two subsets one gets a multilinear map into multilinear maps. This is a linear map version of the function `MultilinearMap.domDomRestrict`. -/ def domDomRestrictₗ (f : MultilinearMap R M₁ M₂) (P : ι → Prop) [DecidablePred P] : MultilinearMap R (fun (i : {a : ι // ¬ P a}) => M₁ i) (MultilinearMap R (fun (i : {a : ι // P a}) => M₁ i) M₂) where toFun := fun z ↦ domDomRestrict f P z map_update_add' := by intro h m i x y classical ext v simp [domDomRestrict_aux_right] map_update_smul' := by intro h m i c x classical ext v simp [domDomRestrict_aux_right] lemma iteratedFDeriv_aux {ι} {M₁ : ι → Type*} {α : Type*} [DecidableEq α] (s : Set ι) [DecidableEq { x // x ∈ s }] (e : α ≃ s) (m : α → ((i : ι) → M₁ i)) (a : α) (z : (i : ι) → M₁ i) : (fun i ↦ update m a z (e.symm i) i) = (fun i ↦ update (fun j ↦ m (e.symm j) j) (e a) (z (e a)) i) := by ext i rcases eq_or_ne a (e.symm i) with rfl | hne · rw [Equiv.apply_symm_apply e i, update_self, update_self] · rw [update_of_ne hne.symm, update_of_ne fun h ↦ (Equiv.symm_apply_apply .. ▸ h ▸ hne) rfl] /-- One of the components of the iterated derivative of a multilinear map. Given a bijection `e` between a type `α` (typically `Fin k`) and a subset `s` of `ι`, this component is a multilinear map of `k` vectors `v₁, ..., vₖ`, mapping them to `f (x₁, (v_{e.symm 2})₂, x₃, ...)`, where at indices `i` in `s` one uses the `i`-th coordinate of the vector `v_{e.symm i}` and otherwise one uses the `i`-th coordinate of a reference vector `x`. This is multilinear in the components of `x` outside of `s`, and in the `v_j`. -/ noncomputable def iteratedFDerivComponent {α : Type*} (f : MultilinearMap R M₁ M₂) {s : Set ι} (e : α ≃ s) [DecidablePred (· ∈ s)] : MultilinearMap R (fun (i : {a : ι // a ∉ s}) ↦ M₁ i) (MultilinearMap R (fun (_ : α) ↦ (∀ i, M₁ i)) M₂) where toFun := fun z ↦ { toFun := fun v ↦ domDomRestrictₗ f (fun i ↦ i ∈ s) z (fun i ↦ v (e.symm i) i) map_update_add' := by classical simp [iteratedFDeriv_aux] map_update_smul' := by classical simp [iteratedFDeriv_aux] } map_update_add' := by intros; ext; simp map_update_smul' := by intros; ext; simp open Classical in /-- The `k`-th iterated derivative of a multilinear map `f` at the point `x`. It is a multilinear map of `k` vectors `v₁, ..., vₖ` (with the same type as `x`), mapping them to `∑ f (x₁, (v_{i₁})₂, x₃, ...)`, where at each index `j` one uses either `xⱼ` or one of the `(vᵢ)ⱼ`, and each `vᵢ` has to be used exactly once. The sum is parameterized by the embeddings of `Fin k` in the index type `ι` (or, equivalently, by the subsets `s` of `ι` of cardinality `k` and then the bijections between `Fin k` and `s`). For the continuous version, see `ContinuousMultilinearMap.iteratedFDeriv`. -/ protected noncomputable def iteratedFDeriv [Fintype ι] (f : MultilinearMap R M₁ M₂) (k : ℕ) (x : (i : ι) → M₁ i) : MultilinearMap R (fun (_ : Fin k) ↦ (∀ i, M₁ i)) M₂ := ∑ e : Fin k ↪ ι, iteratedFDerivComponent f e.toEquivRange (fun i ↦ x i) /-- If `f` is a collection of linear maps, then the construction `MultilinearMap.compLinearMap` sending a multilinear map `g` to `g (f₁ ⬝ , ..., fₙ ⬝ )` is linear in `g`. -/ @[simps] def compLinearMapₗ (f : Π (i : ι), M₁ i →ₗ[R] M₁' i) : (MultilinearMap R M₁' M₂) →ₗ[R] MultilinearMap R M₁ M₂ where toFun := fun g ↦ g.compLinearMap f map_add' := fun _ _ ↦ rfl map_smul' := fun _ _ ↦ rfl /-- If `f` is a collection of linear maps, then the construction `MultilinearMap.compLinearMap` sending a multilinear map `g` to `g (f₁ ⬝ , ..., fₙ ⬝ )` is linear in `g` and multilinear in `f₁, ..., fₙ`. -/ @[simps] def compLinearMapMultilinear : @MultilinearMap R ι (fun i ↦ M₁ i →ₗ[R] M₁' i) ((MultilinearMap R M₁' M₂) →ₗ[R] MultilinearMap R M₁ M₂) _ _ _ (fun _ ↦ LinearMap.module) _ where toFun := MultilinearMap.compLinearMapₗ map_update_add' := by intro _ f i f₁ f₂ ext g x change (g fun j ↦ update f i (f₁ + f₂) j <| x j) = (g fun j ↦ update f i f₁ j <|x j) + g fun j ↦ update f i f₂ j (x j) let c : Π (i : ι), (M₁ i →ₗ[R] M₁' i) → M₁' i := fun i f ↦ f (x i) convert g.map_update_add (fun j ↦ f j (x j)) i (f₁ (x i)) (f₂ (x i)) with j j j · exact Function.apply_update c f i (f₁ + f₂) j · exact Function.apply_update c f i f₁ j · exact Function.apply_update c f i f₂ j map_update_smul' := by intro _ f i a f₀ ext g x change (g fun j ↦ update f i (a • f₀) j <| x j) = a • g fun j ↦ update f i f₀ j (x j) let c : Π (i : ι), (M₁ i →ₗ[R] M₁' i) → M₁' i := fun i f ↦ f (x i) convert g.map_update_smul (fun j ↦ f j (x j)) i a (f₀ (x i)) with j j j · exact Function.apply_update c f i (a • f₀) j · exact Function.apply_update c f i f₀ j /-- Let `M₁ᵢ` and `M₁ᵢ'` be two families of `R`-modules and `M₂` an `R`-module. Let us denote `Π i, M₁ᵢ` and `Π i, M₁ᵢ'` by `M` and `M'` respectively. If `g` is a multilinear map `M' → M₂`, then `g` can be reinterpreted as a multilinear map from `Π i, M₁ᵢ ⟶ M₁ᵢ'` to `M ⟶ M₂` via `(fᵢ) ↦ v ↦ g(fᵢ vᵢ)`. -/ @[simps!] def piLinearMap : MultilinearMap R M₁' M₂ →ₗ[R] MultilinearMap R (fun i ↦ M₁ i →ₗ[R] M₁' i) (MultilinearMap R M₁ M₂) where toFun g := (LinearMap.applyₗ g).compMultilinearMap compLinearMapMultilinear map_add' := by simp map_smul' := by simp end /-- If one multiplies by `c i` the coordinates in a finset `s`, then the image under a multilinear map is multiplied by `∏ i ∈ s, c i`. This is mainly an auxiliary statement to prove the result when `s = univ`, given in `map_smul_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite. -/ theorem map_piecewise_smul [DecidableEq ι] (c : ι → R) (m : ∀ i, M₁ i) (s : Finset ι) : f (s.piecewise (fun i => c i • m i) m) = (∏ i ∈ s, c i) • f m := by refine s.induction_on (by simp) ?_ intro j s j_not_mem_s Hrec have A : Function.update (s.piecewise (fun i => c i • m i) m) j (m j) = s.piecewise (fun i => c i • m i) m := by ext i by_cases h : i = j · rw [h] simp [j_not_mem_s] · simp [h] rw [s.piecewise_insert, f.map_update_smul, A, Hrec] simp [j_not_mem_s, mul_smul] /-- Multiplicativity of a multilinear map along all coordinates at the same time, writing `f (fun i => c i • m i)` as `(∏ i, c i) • f m`. -/ theorem map_smul_univ [Fintype ι] (c : ι → R) (m : ∀ i, M₁ i) : (f fun i => c i • m i) = (∏ i, c i) • f m := by classical simpa using map_piecewise_smul f c m Finset.univ @[simp] theorem map_update_smul_left [DecidableEq ι] [Fintype ι] (m : ∀ i, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update (c • m) i x) = c ^ (Fintype.card ι - 1) • f (update m i x) := by have : f ((Finset.univ.erase i).piecewise (c • update m i x) (update m i x)) = (∏ _i ∈ Finset.univ.erase i, c) • f (update m i x) := map_piecewise_smul f _ _ _ simpa [← Function.update_smul c m] using this section variable (R ι) variable (A : Type*) [CommSemiring A] [Algebra R A] [Fintype ι] /-- Given an `R`-algebra `A`, `mkPiAlgebra` is the multilinear map on `A^ι` associating to `m` the product of all the `m i`. See also `MultilinearMap.mkPiAlgebraFin` for a version that works with a non-commutative algebra `A` but requires `ι = Fin n`. -/ protected def mkPiAlgebra : MultilinearMap R (fun _ : ι => A) A where toFun m := ∏ i, m i map_update_add' m i x y := by simp [Finset.prod_update_of_mem, add_mul] map_update_smul' m i c x := by simp [Finset.prod_update_of_mem] variable {R A ι} @[simp] theorem mkPiAlgebra_apply (m : ι → A) : MultilinearMap.mkPiAlgebra R ι A m = ∏ i, m i := rfl end section variable (R n) variable (A : Type*) [Semiring A] [Algebra R A] /-- Given an `R`-algebra `A`, `mkPiAlgebraFin` is the multilinear map on `A^n` associating to `m` the product of all the `m i`. See also `MultilinearMap.mkPiAlgebra` for a version that assumes `[CommSemiring A]` but works
for `A^ι` with any finite type `ι`. -/ protected def mkPiAlgebraFin : MultilinearMap R (fun _ : Fin n => A) A := MultilinearMap.mk' (fun m ↦ (List.ofFn m).prod)
Mathlib/LinearAlgebra/Multilinear/Basic.lean
1,151
1,153
/- 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, Jeremy Avigad -/ import Mathlib.Algebra.Group.Basic import Mathlib.Algebra.Notation.Pi import Mathlib.Data.Set.Lattice import Mathlib.Order.Filter.Defs /-! # Theory of filters on sets A *filter* on a type `α` is a collection of sets of `α` which contains the whole `α`, is upwards-closed, and is stable under intersection. They are mostly used to abstract two related kinds of ideas: * *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions at a point or at infinity, etc... * *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough a point `x`, or for close enough pairs of points, or things happening almost everywhere in the sense of measure theory. Dually, filters can also express the idea of *things happening often*: for arbitrarily large `n`, or at a point in any neighborhood of given a point etc... ## Main definitions In this file, we endow `Filter α` it with a complete lattice structure. This structure is lifted from the lattice structure on `Set (Set X)` using the Galois insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to the smallest filter containing it in the other direction. We also prove `Filter` is a monadic functor, with a push-forward operation `Filter.map` and a pull-back operation `Filter.comap` that form a Galois connections for the order on filters. The examples of filters appearing in the description of the two motivating ideas are: * `(Filter.atTop : Filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N` * `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic) * `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces defined in `Mathlib/Topology/UniformSpace/Basic.lean`) * `MeasureTheory.ae` : made of sets whose complement has zero measure with respect to `μ` (defined in `Mathlib/MeasureTheory/OuterMeasure/AE`) The predicate "happening eventually" is `Filter.Eventually`, and "happening often" is `Filter.Frequently`, whose definitions are immediate after `Filter` is defined (but they come rather late in this file in order to immediately relate them to the lattice structure). ## Notations * `∀ᶠ x in f, p x` : `f.Eventually p`; * `∃ᶠ x in f, p x` : `f.Frequently p`; * `f =ᶠ[l] g` : `∀ᶠ x in l, f x = g x`; * `f ≤ᶠ[l] g` : `∀ᶠ x in l, f x ≤ g x`; * `𝓟 s` : `Filter.Principal s`, localized in `Filter`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which we do *not* require. This gives `Filter X` better formal properties, in particular a bottom element `⊥` for its lattice structure, at the cost of including the assumption `[NeBot f]` in a number of lemmas and definitions. -/ assert_not_exists OrderedSemiring Fintype open Function Set Order open scoped symmDiff universe u v w x y namespace Filter variable {α : Type u} {f g : Filter α} {s t : Set α} instance inhabitedMem : Inhabited { s : Set α // s ∈ f } := ⟨⟨univ, f.univ_sets⟩⟩ theorem filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ @[simp] theorem sets_subset_sets : f.sets ⊆ g.sets ↔ g ≤ f := .rfl @[simp] theorem sets_ssubset_sets : f.sets ⊂ g.sets ↔ g < f := .rfl /-- An extensionality lemma that is useful for filters with good lemmas about `sᶜ ∈ f` (e.g., `Filter.comap`, `Filter.coprod`, `Filter.Coprod`, `Filter.cofinite`). -/ protected theorem coext (h : ∀ s, sᶜ ∈ f ↔ sᶜ ∈ g) : f = g := Filter.ext <| compl_surjective.forall.2 h instance : Trans (· ⊇ ·) ((· ∈ ·) : Set α → Filter α → Prop) (· ∈ ·) where trans h₁ h₂ := mem_of_superset h₂ h₁ instance : Trans Membership.mem (· ⊆ ·) (Membership.mem : Filter α → Set α → Prop) where trans h₁ h₂ := mem_of_superset h₁ h₂ @[simp] theorem inter_mem_iff {s t : Set α} : s ∩ t ∈ f ↔ s ∈ f ∧ t ∈ f := ⟨fun h => ⟨mem_of_superset h inter_subset_left, mem_of_superset h inter_subset_right⟩, and_imp.2 inter_mem⟩ theorem diff_mem {s t : Set α} (hs : s ∈ f) (ht : tᶜ ∈ f) : s \ t ∈ f := inter_mem hs ht theorem congr_sets (h : { x | x ∈ s ↔ x ∈ t } ∈ f) : s ∈ f ↔ t ∈ f := ⟨fun hs => mp_mem hs (mem_of_superset h fun _ => Iff.mp), fun hs => mp_mem hs (mem_of_superset h fun _ => Iff.mpr)⟩ lemma copy_eq {S} (hmem : ∀ s, s ∈ S ↔ s ∈ f) : f.copy S hmem = f := Filter.ext hmem /-- Weaker version of `Filter.biInter_mem` that assumes `Subsingleton β` rather than `Finite β`. -/ theorem biInter_mem' {β : Type v} {s : β → Set α} {is : Set β} (hf : is.Subsingleton) : (⋂ i ∈ is, s i) ∈ f ↔ ∀ i ∈ is, s i ∈ f := by apply Subsingleton.induction_on hf <;> simp /-- Weaker version of `Filter.iInter_mem` that assumes `Subsingleton β` rather than `Finite β`. -/ theorem iInter_mem' {β : Sort v} {s : β → Set α} [Subsingleton β] : (⋂ i, s i) ∈ f ↔ ∀ i, s i ∈ f := by rw [← sInter_range, sInter_eq_biInter, biInter_mem' (subsingleton_range s), forall_mem_range] theorem exists_mem_subset_iff : (∃ t ∈ f, t ⊆ s) ↔ s ∈ f := ⟨fun ⟨_, ht, ts⟩ => mem_of_superset ht ts, fun hs => ⟨s, hs, Subset.rfl⟩⟩ theorem monotone_mem {f : Filter α} : Monotone fun s => s ∈ f := fun _ _ hst h => mem_of_superset h hst theorem exists_mem_and_iff {P : Set α → Prop} {Q : Set α → Prop} (hP : Antitone P) (hQ : Antitone Q) : ((∃ u ∈ f, P u) ∧ ∃ u ∈ f, Q u) ↔ ∃ u ∈ f, P u ∧ Q u := by constructor · rintro ⟨⟨u, huf, hPu⟩, v, hvf, hQv⟩ exact ⟨u ∩ v, inter_mem huf hvf, hP inter_subset_left hPu, hQ inter_subset_right hQv⟩ · rintro ⟨u, huf, hPu, hQu⟩ exact ⟨⟨u, huf, hPu⟩, u, huf, hQu⟩ theorem forall_in_swap {β : Type*} {p : Set α → β → Prop} : (∀ a ∈ f, ∀ (b), p a b) ↔ ∀ (b), ∀ a ∈ f, p a b := Set.forall_in_swap end Filter namespace Filter variable {α : Type u} {β : Type v} {γ : Type w} {δ : Type*} {ι : Sort x} theorem mem_principal_self (s : Set α) : s ∈ 𝓟 s := Subset.rfl section Lattice variable {f g : Filter α} {s t : Set α} protected theorem not_le : ¬f ≤ g ↔ ∃ s ∈ g, s ∉ f := by simp_rw [le_def, not_forall, exists_prop] /-- `GenerateSets g s`: `s` is in the filter closure of `g`. -/ inductive GenerateSets (g : Set (Set α)) : Set α → Prop | basic {s : Set α} : s ∈ g → GenerateSets g s | univ : GenerateSets g univ | superset {s t : Set α} : GenerateSets g s → s ⊆ t → GenerateSets g t | inter {s t : Set α} : GenerateSets g s → GenerateSets g t → GenerateSets g (s ∩ t) /-- `generate g` is the largest filter containing the sets `g`. -/ def generate (g : Set (Set α)) : Filter α where sets := {s | GenerateSets g s} univ_sets := GenerateSets.univ sets_of_superset := GenerateSets.superset inter_sets := GenerateSets.inter lemma mem_generate_of_mem {s : Set <| Set α} {U : Set α} (h : U ∈ s) : U ∈ generate s := GenerateSets.basic h theorem le_generate_iff {s : Set (Set α)} {f : Filter α} : f ≤ generate s ↔ s ⊆ f.sets := Iff.intro (fun h _ hu => h <| GenerateSets.basic <| hu) fun h _ hu => hu.recOn (fun h' => h h') univ_mem (fun _ hxy hx => mem_of_superset hx hxy) fun _ _ hx hy => inter_mem hx hy @[simp] lemma generate_singleton (s : Set α) : generate {s} = 𝓟 s := le_antisymm (fun _t ht ↦ mem_of_superset (mem_generate_of_mem <| mem_singleton _) ht) <| le_generate_iff.2 <| singleton_subset_iff.2 Subset.rfl /-- `mkOfClosure s hs` constructs a filter on `α` whose elements set is exactly `s : Set (Set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/ protected def mkOfClosure (s : Set (Set α)) (hs : (generate s).sets = s) : Filter α where sets := s univ_sets := hs ▸ univ_mem sets_of_superset := hs ▸ mem_of_superset inter_sets := hs ▸ inter_mem theorem mkOfClosure_sets {s : Set (Set α)} {hs : (generate s).sets = s} : Filter.mkOfClosure s hs = generate s := Filter.ext fun u => show u ∈ (Filter.mkOfClosure s hs).sets ↔ u ∈ (generate s).sets from hs.symm ▸ Iff.rfl /-- Galois insertion from sets of sets into filters. -/ def giGenerate (α : Type*) : @GaloisInsertion (Set (Set α)) (Filter α)ᵒᵈ _ _ Filter.generate Filter.sets where gc _ _ := le_generate_iff le_l_u _ _ h := GenerateSets.basic h choice s hs := Filter.mkOfClosure s (le_antisymm hs <| le_generate_iff.1 <| le_rfl) choice_eq _ _ := mkOfClosure_sets theorem mem_inf_iff {f g : Filter α} {s : Set α} : s ∈ f ⊓ g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, s = t₁ ∩ t₂ := Iff.rfl theorem mem_inf_of_left {f g : Filter α} {s : Set α} (h : s ∈ f) : s ∈ f ⊓ g := ⟨s, h, univ, univ_mem, (inter_univ s).symm⟩ theorem mem_inf_of_right {f g : Filter α} {s : Set α} (h : s ∈ g) : s ∈ f ⊓ g := ⟨univ, univ_mem, s, h, (univ_inter s).symm⟩ theorem inter_mem_inf {α : Type u} {f g : Filter α} {s t : Set α} (hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g := ⟨s, hs, t, ht, rfl⟩ theorem mem_inf_of_inter {f g : Filter α} {s t u : Set α} (hs : s ∈ f) (ht : t ∈ g) (h : s ∩ t ⊆ u) : u ∈ f ⊓ g := mem_of_superset (inter_mem_inf hs ht) h theorem mem_inf_iff_superset {f g : Filter α} {s : Set α} : s ∈ f ⊓ g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ∩ t₂ ⊆ s := ⟨fun ⟨t₁, h₁, t₂, h₂, Eq⟩ => ⟨t₁, h₁, t₂, h₂, Eq ▸ Subset.rfl⟩, fun ⟨_, h₁, _, h₂, sub⟩ => mem_inf_of_inter h₁ h₂ sub⟩ section CompleteLattice /-- Complete lattice structure on `Filter α`. -/ instance instCompleteLatticeFilter : CompleteLattice (Filter α) where inf a b := min a b sup a b := max a b le_sup_left _ _ _ h := h.1 le_sup_right _ _ _ h := h.2 sup_le _ _ _ h₁ h₂ _ h := ⟨h₁ h, h₂ h⟩ inf_le_left _ _ _ := mem_inf_of_left inf_le_right _ _ _ := mem_inf_of_right le_inf := fun _ _ _ h₁ h₂ _s ⟨_a, ha, _b, hb, hs⟩ => hs.symm ▸ inter_mem (h₁ ha) (h₂ hb) le_sSup _ _ h₁ _ h₂ := h₂ h₁ sSup_le _ _ h₁ _ h₂ _ h₃ := h₁ _ h₃ h₂ sInf_le _ _ h₁ _ h₂ := by rw [← Filter.sSup_lowerBounds]; exact fun _ h₃ ↦ h₃ h₁ h₂ le_sInf _ _ h₁ _ h₂ := by rw [← Filter.sSup_lowerBounds] at h₂; exact h₂ h₁ le_top _ _ := univ_mem' bot_le _ _ _ := trivial instance : Inhabited (Filter α) := ⟨⊥⟩ end CompleteLattice theorem NeBot.ne {f : Filter α} (hf : NeBot f) : f ≠ ⊥ := hf.ne' @[simp] theorem not_neBot {f : Filter α} : ¬f.NeBot ↔ f = ⊥ := neBot_iff.not_left theorem NeBot.mono {f g : Filter α} (hf : NeBot f) (hg : f ≤ g) : NeBot g := ⟨ne_bot_of_le_ne_bot hf.1 hg⟩ theorem neBot_of_le {f g : Filter α} [hf : NeBot f] (hg : f ≤ g) : NeBot g := hf.mono hg @[simp] theorem sup_neBot {f g : Filter α} : NeBot (f ⊔ g) ↔ NeBot f ∨ NeBot g := by simp only [neBot_iff, not_and_or, Ne, sup_eq_bot_iff] theorem not_disjoint_self_iff : ¬Disjoint f f ↔ f.NeBot := by rw [disjoint_self, neBot_iff] theorem bot_sets_eq : (⊥ : Filter α).sets = univ := rfl /-- Either `f = ⊥` or `Filter.NeBot f`. This is a version of `eq_or_ne` that uses `Filter.NeBot` as the second alternative, to be used as an instance. -/ theorem eq_or_neBot (f : Filter α) : f = ⊥ ∨ NeBot f := (eq_or_ne f ⊥).imp_right NeBot.mk theorem sup_sets_eq {f g : Filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (giGenerate α).gc.u_inf theorem sSup_sets_eq {s : Set (Filter α)} : (sSup s).sets = ⋂ f ∈ s, (f : Filter α).sets := (giGenerate α).gc.u_sInf theorem iSup_sets_eq {f : ι → Filter α} : (iSup f).sets = ⋂ i, (f i).sets := (giGenerate α).gc.u_iInf theorem generate_empty : Filter.generate ∅ = (⊤ : Filter α) := (giGenerate α).gc.l_bot theorem generate_univ : Filter.generate univ = (⊥ : Filter α) := bot_unique fun _ _ => GenerateSets.basic (mem_univ _) theorem generate_union {s t : Set (Set α)} : Filter.generate (s ∪ t) = Filter.generate s ⊓ Filter.generate t := (giGenerate α).gc.l_sup theorem generate_iUnion {s : ι → Set (Set α)} : Filter.generate (⋃ i, s i) = ⨅ i, Filter.generate (s i) := (giGenerate α).gc.l_iSup @[simp] theorem mem_sup {f g : Filter α} {s : Set α} : s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g := Iff.rfl theorem union_mem_sup {f g : Filter α} {s t : Set α} (hs : s ∈ f) (ht : t ∈ g) : s ∪ t ∈ f ⊔ g := ⟨mem_of_superset hs subset_union_left, mem_of_superset ht subset_union_right⟩ @[simp] theorem mem_iSup {x : Set α} {f : ι → Filter α} : x ∈ iSup f ↔ ∀ i, x ∈ f i := by simp only [← Filter.mem_sets, iSup_sets_eq, mem_iInter] @[simp] theorem iSup_neBot {f : ι → Filter α} : (⨆ i, f i).NeBot ↔ ∃ i, (f i).NeBot := by simp [neBot_iff] theorem iInf_eq_generate (s : ι → Filter α) : iInf s = generate (⋃ i, (s i).sets) := eq_of_forall_le_iff fun _ ↦ by simp [le_generate_iff] theorem mem_iInf_of_mem {f : ι → Filter α} (i : ι) {s} (hs : s ∈ f i) : s ∈ ⨅ i, f i := iInf_le f i hs @[simp] theorem le_principal_iff {s : Set α} {f : Filter α} : f ≤ 𝓟 s ↔ s ∈ f := ⟨fun h => h Subset.rfl, fun hs _ ht => mem_of_superset hs ht⟩ theorem Iic_principal (s : Set α) : Iic (𝓟 s) = { l | s ∈ l } := Set.ext fun _ => le_principal_iff theorem principal_mono {s t : Set α} : 𝓟 s ≤ 𝓟 t ↔ s ⊆ t := by simp only [le_principal_iff, mem_principal] @[gcongr] alias ⟨_, _root_.GCongr.filter_principal_mono⟩ := principal_mono @[mono] theorem monotone_principal : Monotone (𝓟 : Set α → Filter α) := fun _ _ => principal_mono.2 @[simp] theorem principal_eq_iff_eq {s t : Set α} : 𝓟 s = 𝓟 t ↔ s = t := by simp only [le_antisymm_iff, le_principal_iff, mem_principal]; rfl @[simp] theorem join_principal_eq_sSup {s : Set (Filter α)} : join (𝓟 s) = sSup s := rfl @[simp] theorem principal_univ : 𝓟 (univ : Set α) = ⊤ := top_unique <| by simp only [le_principal_iff, mem_top, eq_self_iff_true] @[simp] theorem principal_empty : 𝓟 (∅ : Set α) = ⊥ := bot_unique fun _ _ => empty_subset _ theorem generate_eq_biInf (S : Set (Set α)) : generate S = ⨅ s ∈ S, 𝓟 s := eq_of_forall_le_iff fun f => by simp [le_generate_iff, le_principal_iff, subset_def] /-! ### Lattice equations -/ theorem empty_mem_iff_bot {f : Filter α} : ∅ ∈ f ↔ f = ⊥ := ⟨fun h => bot_unique fun s _ => mem_of_superset h (empty_subset s), fun h => h.symm ▸ mem_bot⟩ theorem nonempty_of_mem {f : Filter α} [hf : NeBot f] {s : Set α} (hs : s ∈ f) : s.Nonempty := s.eq_empty_or_nonempty.elim (fun h => absurd hs (h.symm ▸ mt empty_mem_iff_bot.mp hf.1)) id theorem NeBot.nonempty_of_mem {f : Filter α} (hf : NeBot f) {s : Set α} (hs : s ∈ f) : s.Nonempty := @Filter.nonempty_of_mem α f hf s hs @[simp] theorem empty_not_mem (f : Filter α) [NeBot f] : ¬∅ ∈ f := fun h => (nonempty_of_mem h).ne_empty rfl theorem nonempty_of_neBot (f : Filter α) [NeBot f] : Nonempty α := nonempty_of_exists <| nonempty_of_mem (univ_mem : univ ∈ f) theorem compl_not_mem {f : Filter α} {s : Set α} [NeBot f] (h : s ∈ f) : sᶜ ∉ f := fun hsc => (nonempty_of_mem (inter_mem h hsc)).ne_empty <| inter_compl_self s theorem filter_eq_bot_of_isEmpty [IsEmpty α] (f : Filter α) : f = ⊥ := empty_mem_iff_bot.mp <| univ_mem' isEmptyElim protected lemma disjoint_iff {f g : Filter α} : Disjoint f g ↔ ∃ s ∈ f, ∃ t ∈ g, Disjoint s t := by simp only [disjoint_iff, ← empty_mem_iff_bot, mem_inf_iff, inf_eq_inter, bot_eq_empty, @eq_comm _ ∅] theorem disjoint_of_disjoint_of_mem {f g : Filter α} {s t : Set α} (h : Disjoint s t) (hs : s ∈ f) (ht : t ∈ g) : Disjoint f g := Filter.disjoint_iff.mpr ⟨s, hs, t, ht, h⟩ theorem NeBot.not_disjoint (hf : f.NeBot) (hs : s ∈ f) (ht : t ∈ f) : ¬Disjoint s t := fun h => not_disjoint_self_iff.2 hf <| Filter.disjoint_iff.2 ⟨s, hs, t, ht, h⟩ theorem inf_eq_bot_iff {f g : Filter α} : f ⊓ g = ⊥ ↔ ∃ U ∈ f, ∃ V ∈ g, U ∩ V = ∅ := by simp only [← disjoint_iff, Filter.disjoint_iff, Set.disjoint_iff_inter_eq_empty] /-- There is exactly one filter on an empty type. -/ instance unique [IsEmpty α] : Unique (Filter α) where default := ⊥ uniq := filter_eq_bot_of_isEmpty theorem NeBot.nonempty (f : Filter α) [hf : f.NeBot] : Nonempty α := not_isEmpty_iff.mp fun _ ↦ hf.ne (Subsingleton.elim _ _) /-- There are only two filters on a `Subsingleton`: `⊥` and `⊤`. If the type is empty, then they are equal. -/ theorem eq_top_of_neBot [Subsingleton α] (l : Filter α) [NeBot l] : l = ⊤ := by refine top_unique fun s hs => ?_ obtain rfl : s = univ := Subsingleton.eq_univ_of_nonempty (nonempty_of_mem hs) exact univ_mem theorem forall_mem_nonempty_iff_neBot {f : Filter α} : (∀ s : Set α, s ∈ f → s.Nonempty) ↔ NeBot f := ⟨fun h => ⟨fun hf => not_nonempty_empty (h ∅ <| hf.symm ▸ mem_bot)⟩, @nonempty_of_mem _ _⟩ instance instNeBotTop [Nonempty α] : NeBot (⊤ : Filter α) := forall_mem_nonempty_iff_neBot.1 fun s hs => by rwa [mem_top.1 hs, ← nonempty_iff_univ_nonempty] instance instNontrivialFilter [Nonempty α] : Nontrivial (Filter α) := ⟨⟨⊤, ⊥, instNeBotTop.ne⟩⟩ theorem nontrivial_iff_nonempty : Nontrivial (Filter α) ↔ Nonempty α := ⟨fun _ => by_contra fun h' => haveI := not_nonempty_iff.1 h' not_subsingleton (Filter α) inferInstance, @Filter.instNontrivialFilter α⟩ theorem eq_sInf_of_mem_iff_exists_mem {S : Set (Filter α)} {l : Filter α} (h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = sInf S := le_antisymm (le_sInf fun f hf _ hs => h.2 ⟨f, hf, hs⟩) fun _ hs => let ⟨_, hf, hs⟩ := h.1 hs; (sInf_le hf) hs theorem eq_iInf_of_mem_iff_exists_mem {f : ι → Filter α} {l : Filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) : l = iInf f := eq_sInf_of_mem_iff_exists_mem <| h.trans (exists_range_iff (p := (_ ∈ ·))).symm theorem eq_biInf_of_mem_iff_exists_mem {f : ι → Filter α} {p : ι → Prop} {l : Filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i, p i ∧ s ∈ f i) : l = ⨅ (i) (_ : p i), f i := by rw [iInf_subtype'] exact eq_iInf_of_mem_iff_exists_mem fun {_} => by simp only [Subtype.exists, h, exists_prop] theorem iInf_sets_eq {f : ι → Filter α} (h : Directed (· ≥ ·) f) [ne : Nonempty ι] : (iInf f).sets = ⋃ i, (f i).sets := let ⟨i⟩ := ne let u := { sets := ⋃ i, (f i).sets univ_sets := mem_iUnion.2 ⟨i, univ_mem⟩ sets_of_superset := by simp only [mem_iUnion, exists_imp] exact fun i hx hxy => ⟨i, mem_of_superset hx hxy⟩ inter_sets := by simp only [mem_iUnion, exists_imp] intro x y a hx b hy rcases h a b with ⟨c, ha, hb⟩ exact ⟨c, inter_mem (ha hx) (hb hy)⟩ } have : u = iInf f := eq_iInf_of_mem_iff_exists_mem mem_iUnion congr_arg Filter.sets this.symm theorem mem_iInf_of_directed {f : ι → Filter α} (h : Directed (· ≥ ·) f) [Nonempty ι] (s) : s ∈ iInf f ↔ ∃ i, s ∈ f i := by simp only [← Filter.mem_sets, iInf_sets_eq h, mem_iUnion] theorem mem_biInf_of_directed {f : β → Filter α} {s : Set β} (h : DirectedOn (f ⁻¹'o (· ≥ ·)) s) (ne : s.Nonempty) {t : Set α} : (t ∈ ⨅ i ∈ s, f i) ↔ ∃ i ∈ s, t ∈ f i := by haveI := ne.to_subtype simp_rw [iInf_subtype', mem_iInf_of_directed h.directed_val, Subtype.exists, exists_prop] theorem biInf_sets_eq {f : β → Filter α} {s : Set β} (h : DirectedOn (f ⁻¹'o (· ≥ ·)) s) (ne : s.Nonempty) : (⨅ i ∈ s, f i).sets = ⋃ i ∈ s, (f i).sets := ext fun t => by simp [mem_biInf_of_directed h ne] @[simp] theorem sup_join {f₁ f₂ : Filter (Filter α)} : join f₁ ⊔ join f₂ = join (f₁ ⊔ f₂) := Filter.ext fun x => by simp only [mem_sup, mem_join] @[simp] theorem iSup_join {ι : Sort w} {f : ι → Filter (Filter α)} : ⨆ x, join (f x) = join (⨆ x, f x) := Filter.ext fun x => by simp only [mem_iSup, mem_join] instance : DistribLattice (Filter α) := { Filter.instCompleteLatticeFilter with le_sup_inf := by intro x y z s simp only [and_assoc, mem_inf_iff, mem_sup, exists_prop, exists_imp, and_imp] rintro hs t₁ ht₁ t₂ ht₂ rfl exact ⟨t₁, x.sets_of_superset hs inter_subset_left, ht₁, t₂, x.sets_of_superset hs inter_subset_right, ht₂, rfl⟩ } /-- If `f : ι → Filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `iInf f ≠ ⊥`. See also `iInf_neBot_of_directed` for a version assuming `Nonempty α` instead of `Nonempty ι`. -/ theorem iInf_neBot_of_directed' {f : ι → Filter α} [Nonempty ι] (hd : Directed (· ≥ ·) f) : (∀ i, NeBot (f i)) → NeBot (iInf f) := not_imp_not.1 <| by simpa only [not_forall, not_neBot, ← empty_mem_iff_bot, mem_iInf_of_directed hd] using id /-- If `f : ι → Filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `iInf f ≠ ⊥`. See also `iInf_neBot_of_directed'` for a version assuming `Nonempty ι` instead of `Nonempty α`. -/ theorem iInf_neBot_of_directed {f : ι → Filter α} [hn : Nonempty α] (hd : Directed (· ≥ ·) f) (hb : ∀ i, NeBot (f i)) : NeBot (iInf f) := by cases isEmpty_or_nonempty ι · constructor simp [iInf_of_empty f, top_ne_bot] · exact iInf_neBot_of_directed' hd hb theorem sInf_neBot_of_directed' {s : Set (Filter α)} (hne : s.Nonempty) (hd : DirectedOn (· ≥ ·) s) (hbot : ⊥ ∉ s) : NeBot (sInf s) := (sInf_eq_iInf' s).symm ▸ @iInf_neBot_of_directed' _ _ _ hne.to_subtype hd.directed_val fun ⟨_, hf⟩ => ⟨ne_of_mem_of_not_mem hf hbot⟩ theorem sInf_neBot_of_directed [Nonempty α] {s : Set (Filter α)} (hd : DirectedOn (· ≥ ·) s) (hbot : ⊥ ∉ s) : NeBot (sInf s) := (sInf_eq_iInf' s).symm ▸ iInf_neBot_of_directed hd.directed_val fun ⟨_, hf⟩ => ⟨ne_of_mem_of_not_mem hf hbot⟩ theorem iInf_neBot_iff_of_directed' {f : ι → Filter α} [Nonempty ι] (hd : Directed (· ≥ ·) f) : NeBot (iInf f) ↔ ∀ i, NeBot (f i) := ⟨fun H i => H.mono (iInf_le _ i), iInf_neBot_of_directed' hd⟩ theorem iInf_neBot_iff_of_directed {f : ι → Filter α} [Nonempty α] (hd : Directed (· ≥ ·) f) : NeBot (iInf f) ↔ ∀ i, NeBot (f i) := ⟨fun H i => H.mono (iInf_le _ i), iInf_neBot_of_directed hd⟩ /-! #### `principal` equations -/ @[simp] theorem inf_principal {s t : Set α} : 𝓟 s ⊓ 𝓟 t = 𝓟 (s ∩ t) := le_antisymm (by simp only [le_principal_iff, mem_inf_iff]; exact ⟨s, Subset.rfl, t, Subset.rfl, rfl⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] theorem sup_principal {s t : Set α} : 𝓟 s ⊔ 𝓟 t = 𝓟 (s ∪ t) := Filter.ext fun u => by simp only [union_subset_iff, mem_sup, mem_principal] @[simp] theorem iSup_principal {ι : Sort w} {s : ι → Set α} : ⨆ x, 𝓟 (s x) = 𝓟 (⋃ i, s i) := Filter.ext fun x => by simp only [mem_iSup, mem_principal, iUnion_subset_iff] @[simp] theorem principal_eq_bot_iff {s : Set α} : 𝓟 s = ⊥ ↔ s = ∅ := empty_mem_iff_bot.symm.trans <| mem_principal.trans subset_empty_iff @[simp] theorem principal_neBot_iff {s : Set α} : NeBot (𝓟 s) ↔ s.Nonempty := neBot_iff.trans <| (not_congr principal_eq_bot_iff).trans nonempty_iff_ne_empty.symm alias ⟨_, _root_.Set.Nonempty.principal_neBot⟩ := principal_neBot_iff theorem isCompl_principal (s : Set α) : IsCompl (𝓟 s) (𝓟 sᶜ) := IsCompl.of_eq (by rw [inf_principal, inter_compl_self, principal_empty]) <| by rw [sup_principal, union_compl_self, principal_univ] theorem mem_inf_principal' {f : Filter α} {s t : Set α} : s ∈ f ⊓ 𝓟 t ↔ tᶜ ∪ s ∈ f := by simp only [← le_principal_iff, (isCompl_principal s).le_left_iff, disjoint_assoc, inf_principal, ← (isCompl_principal (t ∩ sᶜ)).le_right_iff, compl_inter, compl_compl] lemma mem_inf_principal {f : Filter α} {s t : Set α} : s ∈ f ⊓ 𝓟 t ↔ { x | x ∈ t → x ∈ s } ∈ f := by simp only [mem_inf_principal', imp_iff_not_or, setOf_or, compl_def, setOf_mem_eq] lemma iSup_inf_principal (f : ι → Filter α) (s : Set α) : ⨆ i, f i ⊓ 𝓟 s = (⨆ i, f i) ⊓ 𝓟 s := by ext simp only [mem_iSup, mem_inf_principal] theorem inf_principal_eq_bot {f : Filter α} {s : Set α} : f ⊓ 𝓟 s = ⊥ ↔ sᶜ ∈ f := by rw [← empty_mem_iff_bot, mem_inf_principal] simp only [mem_empty_iff_false, imp_false, compl_def] theorem mem_of_eq_bot {f : Filter α} {s : Set α} (h : f ⊓ 𝓟 sᶜ = ⊥) : s ∈ f := by rwa [inf_principal_eq_bot, compl_compl] at h theorem diff_mem_inf_principal_compl {f : Filter α} {s : Set α} (hs : s ∈ f) (t : Set α) : s \ t ∈ f ⊓ 𝓟 tᶜ := inter_mem_inf hs <| mem_principal_self tᶜ theorem principal_le_iff {s : Set α} {f : Filter α} : 𝓟 s ≤ f ↔ ∀ V ∈ f, s ⊆ V := by simp_rw [le_def, mem_principal] end Lattice @[mono, gcongr] theorem join_mono {f₁ f₂ : Filter (Filter α)} (h : f₁ ≤ f₂) : join f₁ ≤ join f₂ := fun _ hs => h hs /-! ### Eventually -/ theorem eventually_iff {f : Filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ { x | P x } ∈ f := Iff.rfl @[simp] theorem eventually_mem_set {s : Set α} {l : Filter α} : (∀ᶠ x in l, x ∈ s) ↔ s ∈ l := Iff.rfl protected theorem ext' {f₁ f₂ : Filter α} (h : ∀ p : α → Prop, (∀ᶠ x in f₁, p x) ↔ ∀ᶠ x in f₂, p x) : f₁ = f₂ := Filter.ext h theorem Eventually.filter_mono {f₁ f₂ : Filter α} (h : f₁ ≤ f₂) {p : α → Prop} (hp : ∀ᶠ x in f₂, p x) : ∀ᶠ x in f₁, p x := h hp theorem eventually_of_mem {f : Filter α} {P : α → Prop} {U : Set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) : ∀ᶠ x in f, P x := mem_of_superset hU h protected theorem Eventually.and {p q : α → Prop} {f : Filter α} : f.Eventually p → f.Eventually q → ∀ᶠ x in f, p x ∧ q x := inter_mem @[simp] theorem eventually_true (f : Filter α) : ∀ᶠ _ in f, True := univ_mem theorem Eventually.of_forall {p : α → Prop} {f : Filter α} (hp : ∀ x, p x) : ∀ᶠ x in f, p x := univ_mem' hp @[simp] theorem eventually_false_iff_eq_bot {f : Filter α} : (∀ᶠ _ in f, False) ↔ f = ⊥ := empty_mem_iff_bot @[simp] theorem eventually_const {f : Filter α} [t : NeBot f] {p : Prop} : (∀ᶠ _ in f, p) ↔ p := by by_cases h : p <;> simp [h, t.ne] theorem eventually_iff_exists_mem {p : α → Prop} {f : Filter α} : (∀ᶠ x in f, p x) ↔ ∃ v ∈ f, ∀ y ∈ v, p y := exists_mem_subset_iff.symm theorem Eventually.exists_mem {p : α → Prop} {f : Filter α} (hp : ∀ᶠ x in f, p x) : ∃ v ∈ f, ∀ y ∈ v, p y := eventually_iff_exists_mem.1 hp theorem Eventually.mp {p q : α → Prop} {f : Filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ᶠ x in f, p x → q x) : ∀ᶠ x in f, q x := mp_mem hp hq theorem Eventually.mono {p q : α → Prop} {f : Filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ x, p x → q x) : ∀ᶠ x in f, q x := hp.mp (Eventually.of_forall hq) theorem forall_eventually_of_eventually_forall {f : Filter α} {p : α → β → Prop} (h : ∀ᶠ x in f, ∀ y, p x y) : ∀ y, ∀ᶠ x in f, p x y := fun y => h.mono fun _ h => h y @[simp] theorem eventually_and {p q : α → Prop} {f : Filter α} : (∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ ∀ᶠ x in f, q x := inter_mem_iff theorem Eventually.congr {f : Filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x) (h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x := h'.mp (h.mono fun _ hx => hx.mp) theorem eventually_congr {f : Filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) : (∀ᶠ x in f, p x) ↔ ∀ᶠ x in f, q x := ⟨fun hp => hp.congr h, fun hq => hq.congr <| by simpa only [Iff.comm] using h⟩ @[simp] theorem eventually_or_distrib_left {f : Filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p ∨ q x) ↔ p ∨ ∀ᶠ x in f, q x := by_cases (fun h : p => by simp [h]) fun h => by simp [h] @[simp] theorem eventually_or_distrib_right {f : Filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x ∨ q) ↔ (∀ᶠ x in f, p x) ∨ q := by simp only [@or_comm _ q, eventually_or_distrib_left] theorem eventually_imp_distrib_left {f : Filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p → q x) ↔ p → ∀ᶠ x in f, q x := by simp only [imp_iff_not_or, eventually_or_distrib_left] @[simp] theorem eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩ @[simp] theorem eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ ∀ x, p x := Iff.rfl @[simp] theorem eventually_sup {p : α → Prop} {f g : Filter α} : (∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ ∀ᶠ x in g, p x := Iff.rfl @[simp] theorem eventually_sSup {p : α → Prop} {fs : Set (Filter α)} : (∀ᶠ x in sSup fs, p x) ↔ ∀ f ∈ fs, ∀ᶠ x in f, p x := Iff.rfl @[simp] theorem eventually_iSup {p : α → Prop} {fs : ι → Filter α} : (∀ᶠ x in ⨆ b, fs b, p x) ↔ ∀ b, ∀ᶠ x in fs b, p x := mem_iSup @[simp] theorem eventually_principal {a : Set α} {p : α → Prop} : (∀ᶠ x in 𝓟 a, p x) ↔ ∀ x ∈ a, p x := Iff.rfl theorem Eventually.forall_mem {α : Type*} {f : Filter α} {s : Set α} {P : α → Prop} (hP : ∀ᶠ x in f, P x) (hf : 𝓟 s ≤ f) : ∀ x ∈ s, P x := Filter.eventually_principal.mp (hP.filter_mono hf) theorem eventually_inf {f g : Filter α} {p : α → Prop} : (∀ᶠ x in f ⊓ g, p x) ↔ ∃ s ∈ f, ∃ t ∈ g, ∀ x ∈ s ∩ t, p x := mem_inf_iff_superset theorem eventually_inf_principal {f : Filter α} {p : α → Prop} {s : Set α} : (∀ᶠ x in f ⊓ 𝓟 s, p x) ↔ ∀ᶠ x in f, x ∈ s → p x := mem_inf_principal theorem eventually_iff_all_subsets {f : Filter α} {p : α → Prop} : (∀ᶠ x in f, p x) ↔ ∀ (s : Set α), ∀ᶠ x in f, x ∈ s → p x where mp h _ := by filter_upwards [h] with _ pa _ using pa mpr h := by filter_upwards [h univ] with _ pa using pa (by simp) /-! ### Frequently -/ theorem Eventually.frequently {f : Filter α} [NeBot f] {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ᶠ x in f, p x := compl_not_mem h theorem Frequently.of_forall {f : Filter α} [NeBot f] {p : α → Prop} (h : ∀ x, p x) : ∃ᶠ x in f, p x := Eventually.frequently (Eventually.of_forall h) theorem Frequently.mp {p q : α → Prop} {f : Filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ᶠ x in f, p x → q x) : ∃ᶠ x in f, q x := mt (fun hq => hq.mp <| hpq.mono fun _ => mt) h lemma frequently_congr {p q : α → Prop} {f : Filter α} (h : ∀ᶠ x in f, p x ↔ q x) : (∃ᶠ x in f, p x) ↔ ∃ᶠ x in f, q x := ⟨fun h' ↦ h'.mp (h.mono fun _ ↦ Iff.mp), fun h' ↦ h'.mp (h.mono fun _ ↦ Iff.mpr)⟩ theorem Frequently.filter_mono {p : α → Prop} {f g : Filter α} (h : ∃ᶠ x in f, p x) (hle : f ≤ g) : ∃ᶠ x in g, p x := mt (fun h' => h'.filter_mono hle) h theorem Frequently.mono {p q : α → Prop} {f : Filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ x, p x → q x) : ∃ᶠ x in f, q x := h.mp (Eventually.of_forall hpq) theorem Frequently.and_eventually {p q : α → Prop} {f : Filter α} (hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := by refine mt (fun h => hq.mp <| h.mono ?_) hp exact fun x hpq hq hp => hpq ⟨hp, hq⟩ theorem Eventually.and_frequently {p q : α → Prop} {f : Filter α} (hp : ∀ᶠ x in f, p x) (hq : ∃ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := by simpa only [and_comm] using hq.and_eventually hp theorem Frequently.exists {p : α → Prop} {f : Filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x := by by_contra H replace H : ∀ᶠ x in f, ¬p x := Eventually.of_forall (not_exists.1 H) exact hp H theorem Eventually.exists {p : α → Prop} {f : Filter α} [NeBot f] (hp : ∀ᶠ x in f, p x) : ∃ x, p x := hp.frequently.exists lemma frequently_iff_neBot {l : Filter α} {p : α → Prop} : (∃ᶠ x in l, p x) ↔ NeBot (l ⊓ 𝓟 {x | p x}) := by rw [neBot_iff, Ne, inf_principal_eq_bot]; rfl lemma frequently_mem_iff_neBot {l : Filter α} {s : Set α} : (∃ᶠ x in l, x ∈ s) ↔ NeBot (l ⊓ 𝓟 s) := frequently_iff_neBot theorem frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : Filter α} : (∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x := ⟨fun hp _ hq => (hp.and_eventually hq).exists, fun H hp => by simpa only [and_not_self_iff, exists_false] using H hp⟩ theorem frequently_iff {f : Filter α} {P : α → Prop} : (∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x := by simp only [frequently_iff_forall_eventually_exists_and, @and_comm (P _)] rfl @[simp] theorem not_eventually {p : α → Prop} {f : Filter α} : (¬∀ᶠ x in f, p x) ↔ ∃ᶠ x in f, ¬p x := by simp [Filter.Frequently] @[simp] theorem not_frequently {p : α → Prop} {f : Filter α} : (¬∃ᶠ x in f, p x) ↔ ∀ᶠ x in f, ¬p x := by simp only [Filter.Frequently, not_not] @[simp] theorem frequently_true_iff_neBot (f : Filter α) : (∃ᶠ _ in f, True) ↔ NeBot f := by simp [frequently_iff_neBot] @[simp] theorem frequently_false (f : Filter α) : ¬∃ᶠ _ in f, False := by simp @[simp] theorem frequently_const {f : Filter α} [NeBot f] {p : Prop} : (∃ᶠ _ in f, p) ↔ p := by by_cases p <;> simp [*] @[simp] theorem frequently_or_distrib {f : Filter α} {p q : α → Prop} : (∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ ∃ᶠ x in f, q x := by simp only [Filter.Frequently, ← not_and_or, not_or, eventually_and] theorem frequently_or_distrib_left {f : Filter α} [NeBot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∨ q x) ↔ p ∨ ∃ᶠ x in f, q x := by simp theorem frequently_or_distrib_right {f : Filter α} [NeBot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q := by simp theorem frequently_imp_distrib {f : Filter α} {p q : α → Prop} : (∃ᶠ x in f, p x → q x) ↔ (∀ᶠ x in f, p x) → ∃ᶠ x in f, q x := by simp [imp_iff_not_or] theorem frequently_imp_distrib_left {f : Filter α} [NeBot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p → q x) ↔ p → ∃ᶠ x in f, q x := by simp [frequently_imp_distrib] theorem frequently_imp_distrib_right {f : Filter α} [NeBot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x → q) ↔ (∀ᶠ x in f, p x) → q := by simp only [frequently_imp_distrib, frequently_const] theorem eventually_imp_distrib_right {f : Filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x → q) ↔ (∃ᶠ x in f, p x) → q := by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently] @[simp] theorem frequently_and_distrib_left {f : Filter α} {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∧ q x) ↔ p ∧ ∃ᶠ x in f, q x := by simp only [Filter.Frequently, not_and, eventually_imp_distrib_left, Classical.not_imp] @[simp] theorem frequently_and_distrib_right {f : Filter α} {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∧ q) ↔ (∃ᶠ x in f, p x) ∧ q := by simp only [@and_comm _ q, frequently_and_distrib_left] @[simp] theorem frequently_bot {p : α → Prop} : ¬∃ᶠ x in ⊥, p x := by simp @[simp] theorem frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ ∃ x, p x := by simp [Filter.Frequently] @[simp] theorem frequently_principal {a : Set α} {p : α → Prop} : (∃ᶠ x in 𝓟 a, p x) ↔ ∃ x ∈ a, p x := by simp [Filter.Frequently, not_forall] theorem frequently_inf_principal {f : Filter α} {s : Set α} {p : α → Prop} : (∃ᶠ x in f ⊓ 𝓟 s, p x) ↔ ∃ᶠ x in f, x ∈ s ∧ p x := by simp only [Filter.Frequently, eventually_inf_principal, not_and] alias ⟨Frequently.of_inf_principal, Frequently.inf_principal⟩ := frequently_inf_principal theorem frequently_sup {p : α → Prop} {f g : Filter α} : (∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ ∃ᶠ x in g, p x := by simp only [Filter.Frequently, eventually_sup, not_and_or] @[simp] theorem frequently_sSup {p : α → Prop} {fs : Set (Filter α)} : (∃ᶠ x in sSup fs, p x) ↔ ∃ f ∈ fs, ∃ᶠ x in f, p x := by simp only [Filter.Frequently, not_forall, eventually_sSup, exists_prop] @[simp] theorem frequently_iSup {p : α → Prop} {fs : β → Filter α} : (∃ᶠ x in ⨆ b, fs b, p x) ↔ ∃ b, ∃ᶠ x in fs b, p x := by simp only [Filter.Frequently, eventually_iSup, not_forall] theorem Eventually.choice {r : α → β → Prop} {l : Filter α} [l.NeBot] (h : ∀ᶠ x in l, ∃ y, r x y) : ∃ f : α → β, ∀ᶠ x in l, r x (f x) := by haveI : Nonempty β := let ⟨_, hx⟩ := h.exists; hx.nonempty choose! f hf using fun x (hx : ∃ y, r x y) => hx exact ⟨f, h.mono hf⟩ lemma skolem {ι : Type*} {α : ι → Type*} [∀ i, Nonempty (α i)] {P : ∀ i : ι, α i → Prop} {F : Filter ι} : (∀ᶠ i in F, ∃ b, P i b) ↔ ∃ b : (Π i, α i), ∀ᶠ i in F, P i (b i) := by classical refine ⟨fun H ↦ ?_, fun ⟨b, hb⟩ ↦ hb.mp (.of_forall fun x a ↦ ⟨_, a⟩)⟩ refine ⟨fun i ↦ if h : ∃ b, P i b then h.choose else Nonempty.some inferInstance, ?_⟩ filter_upwards [H] with i hi exact dif_pos hi ▸ hi.choose_spec /-! ### Relation “eventually equal” -/ section EventuallyEq variable {l : Filter α} {f g : α → β} theorem EventuallyEq.eventually (h : f =ᶠ[l] g) : ∀ᶠ x in l, f x = g x := h @[simp] lemma eventuallyEq_top : f =ᶠ[⊤] g ↔ f = g := by simp [EventuallyEq, funext_iff] theorem EventuallyEq.rw {l : Filter α} {f g : α → β} (h : f =ᶠ[l] g) (p : α → β → Prop) (hf : ∀ᶠ x in l, p x (f x)) : ∀ᶠ x in l, p x (g x) := hf.congr <| h.mono fun _ hx => hx ▸ Iff.rfl theorem eventuallyEq_set {s t : Set α} {l : Filter α} : s =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ s ↔ x ∈ t := eventually_congr <| Eventually.of_forall fun _ ↦ eq_iff_iff alias ⟨EventuallyEq.mem_iff, Eventually.set_eq⟩ := eventuallyEq_set @[simp] theorem eventuallyEq_univ {s : Set α} {l : Filter α} : s =ᶠ[l] univ ↔ s ∈ l := by simp [eventuallyEq_set] theorem EventuallyEq.exists_mem {l : Filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∃ s ∈ l, EqOn f g s := Eventually.exists_mem h theorem eventuallyEq_of_mem {l : Filter α} {f g : α → β} {s : Set α} (hs : s ∈ l) (h : EqOn f g s) : f =ᶠ[l] g := eventually_of_mem hs h theorem eventuallyEq_iff_exists_mem {l : Filter α} {f g : α → β} : f =ᶠ[l] g ↔ ∃ s ∈ l, EqOn f g s := eventually_iff_exists_mem theorem EventuallyEq.filter_mono {l l' : Filter α} {f g : α → β} (h₁ : f =ᶠ[l] g) (h₂ : l' ≤ l) : f =ᶠ[l'] g := h₂ h₁ @[refl, simp] theorem EventuallyEq.refl (l : Filter α) (f : α → β) : f =ᶠ[l] f := Eventually.of_forall fun _ => rfl protected theorem EventuallyEq.rfl {l : Filter α} {f : α → β} : f =ᶠ[l] f := EventuallyEq.refl l f theorem EventuallyEq.of_eq {l : Filter α} {f g : α → β} (h : f = g) : f =ᶠ[l] g := h ▸ .rfl alias _root_.Eq.eventuallyEq := EventuallyEq.of_eq @[symm] theorem EventuallyEq.symm {f g : α → β} {l : Filter α} (H : f =ᶠ[l] g) : g =ᶠ[l] f := H.mono fun _ => Eq.symm lemma eventuallyEq_comm {f g : α → β} {l : Filter α} : f =ᶠ[l] g ↔ g =ᶠ[l] f := ⟨.symm, .symm⟩ @[trans] theorem EventuallyEq.trans {l : Filter α} {f g h : α → β} (H₁ : f =ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f =ᶠ[l] h := H₂.rw (fun x y => f x = y) H₁ theorem EventuallyEq.congr_left {l : Filter α} {f g h : α → β} (H : f =ᶠ[l] g) : f =ᶠ[l] h ↔ g =ᶠ[l] h := ⟨H.symm.trans, H.trans⟩ theorem EventuallyEq.congr_right {l : Filter α} {f g h : α → β} (H : g =ᶠ[l] h) : f =ᶠ[l] g ↔ f =ᶠ[l] h := ⟨(·.trans H), (·.trans H.symm)⟩ instance {l : Filter α} : Trans ((· =ᶠ[l] ·) : (α → β) → (α → β) → Prop) (· =ᶠ[l] ·) (· =ᶠ[l] ·) where trans := EventuallyEq.trans theorem EventuallyEq.prodMk {l} {f f' : α → β} (hf : f =ᶠ[l] f') {g g' : α → γ} (hg : g =ᶠ[l] g') : (fun x => (f x, g x)) =ᶠ[l] fun x => (f' x, g' x) := hf.mp <| hg.mono <| by intros simp only [*] @[deprecated (since := "2025-03-10")] alias EventuallyEq.prod_mk := EventuallyEq.prodMk -- See `EventuallyEq.comp_tendsto` further below for a similar statement w.r.t. -- composition on the right. theorem EventuallyEq.fun_comp {f g : α → β} {l : Filter α} (H : f =ᶠ[l] g) (h : β → γ) : h ∘ f =ᶠ[l] h ∘ g := H.mono fun _ hx => congr_arg h hx theorem EventuallyEq.comp₂ {δ} {f f' : α → β} {g g' : α → γ} {l} (Hf : f =ᶠ[l] f') (h : β → γ → δ) (Hg : g =ᶠ[l] g') : (fun x => h (f x) (g x)) =ᶠ[l] fun x => h (f' x) (g' x) := (Hf.prodMk Hg).fun_comp (uncurry h) @[to_additive] theorem EventuallyEq.mul [Mul β] {f f' g g' : α → β} {l : Filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : (fun x => f x * f' x) =ᶠ[l] fun x => g x * g' x := h.comp₂ (· * ·) h' @[to_additive const_smul] theorem EventuallyEq.pow_const {γ} [Pow β γ] {f g : α → β} {l : Filter α} (h : f =ᶠ[l] g) (c : γ) : (fun x => f x ^ c) =ᶠ[l] fun x => g x ^ c := h.fun_comp (· ^ c) @[to_additive] theorem EventuallyEq.inv [Inv β] {f g : α → β} {l : Filter α} (h : f =ᶠ[l] g) : (fun x => (f x)⁻¹) =ᶠ[l] fun x => (g x)⁻¹ := h.fun_comp Inv.inv @[to_additive] theorem EventuallyEq.div [Div β] {f f' g g' : α → β} {l : Filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : (fun x => f x / f' x) =ᶠ[l] fun x => g x / g' x := h.comp₂ (· / ·) h' attribute [to_additive] EventuallyEq.const_smul @[to_additive] theorem EventuallyEq.smul {𝕜} [SMul 𝕜 β] {l : Filter α} {f f' : α → 𝕜} {g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : (fun x => f x • g x) =ᶠ[l] fun x => f' x • g' x := hf.comp₂ (· • ·) hg theorem EventuallyEq.sup [Max β] {l : Filter α} {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : (fun x => f x ⊔ g x) =ᶠ[l] fun x => f' x ⊔ g' x := hf.comp₂ (· ⊔ ·) hg theorem EventuallyEq.inf [Min β] {l : Filter α} {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : (fun x => f x ⊓ g x) =ᶠ[l] fun x => f' x ⊓ g' x := hf.comp₂ (· ⊓ ·) hg theorem EventuallyEq.preimage {l : Filter α} {f g : α → β} (h : f =ᶠ[l] g) (s : Set β) : f ⁻¹' s =ᶠ[l] g ⁻¹' s := h.fun_comp s theorem EventuallyEq.inter {s t s' t' : Set α} {l : Filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∩ s' : Set α) =ᶠ[l] (t ∩ t' : Set α) := h.comp₂ (· ∧ ·) h' theorem EventuallyEq.union {s t s' t' : Set α} {l : Filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∪ s' : Set α) =ᶠ[l] (t ∪ t' : Set α) := h.comp₂ (· ∨ ·) h' theorem EventuallyEq.compl {s t : Set α} {l : Filter α} (h : s =ᶠ[l] t) : (sᶜ : Set α) =ᶠ[l] (tᶜ : Set α) := h.fun_comp Not theorem EventuallyEq.diff {s t s' t' : Set α} {l : Filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s \ s' : Set α) =ᶠ[l] (t \ t' : Set α) := h.inter h'.compl protected theorem EventuallyEq.symmDiff {s t s' t' : Set α} {l : Filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∆ s' : Set α) =ᶠ[l] (t ∆ t' : Set α) := (h.diff h').union (h'.diff h) theorem eventuallyEq_empty {s : Set α} {l : Filter α} : s =ᶠ[l] (∅ : Set α) ↔ ∀ᶠ x in l, x ∉ s := eventuallyEq_set.trans <| by simp theorem inter_eventuallyEq_left {s t : Set α} {l : Filter α} : (s ∩ t : Set α) =ᶠ[l] s ↔ ∀ᶠ x in l, x ∈ s → x ∈ t := by simp only [eventuallyEq_set, mem_inter_iff, and_iff_left_iff_imp] theorem inter_eventuallyEq_right {s t : Set α} {l : Filter α} : (s ∩ t : Set α) =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ t → x ∈ s := by rw [inter_comm, inter_eventuallyEq_left] @[simp] theorem eventuallyEq_principal {s : Set α} {f g : α → β} : f =ᶠ[𝓟 s] g ↔ EqOn f g s := Iff.rfl theorem eventuallyEq_inf_principal_iff {F : Filter α} {s : Set α} {f g : α → β} : f =ᶠ[F ⊓ 𝓟 s] g ↔ ∀ᶠ x in F, x ∈ s → f x = g x := eventually_inf_principal theorem EventuallyEq.sub_eq [AddGroup β] {f g : α → β} {l : Filter α} (h : f =ᶠ[l] g) : f - g =ᶠ[l] 0 := by simpa using ((EventuallyEq.refl l f).sub h).symm theorem eventuallyEq_iff_sub [AddGroup β] {f g : α → β} {l : Filter α} : f =ᶠ[l] g ↔ f - g =ᶠ[l] 0 := ⟨fun h => h.sub_eq, fun h => by simpa using h.add (EventuallyEq.refl l g)⟩ theorem eventuallyEq_iff_all_subsets {f g : α → β} {l : Filter α} : f =ᶠ[l] g ↔ ∀ s : Set α, ∀ᶠ x in l, x ∈ s → f x = g x := eventually_iff_all_subsets section LE variable [LE β] {l : Filter α} theorem EventuallyLE.congr {f f' g g' : α → β} (H : f ≤ᶠ[l] g) (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f' ≤ᶠ[l] g' := H.mp <| hg.mp <| hf.mono fun x hf hg H => by rwa [hf, hg] at H theorem eventuallyLE_congr {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f ≤ᶠ[l] g ↔ f' ≤ᶠ[l] g' := ⟨fun H => H.congr hf hg, fun H => H.congr hf.symm hg.symm⟩ theorem eventuallyLE_iff_all_subsets {f g : α → β} {l : Filter α} : f ≤ᶠ[l] g ↔ ∀ s : Set α, ∀ᶠ x in l, x ∈ s → f x ≤ g x := eventually_iff_all_subsets end LE section Preorder variable [Preorder β] {l : Filter α} {f g h : α → β} theorem EventuallyEq.le (h : f =ᶠ[l] g) : f ≤ᶠ[l] g := h.mono fun _ => le_of_eq @[refl] theorem EventuallyLE.refl (l : Filter α) (f : α → β) : f ≤ᶠ[l] f := EventuallyEq.rfl.le theorem EventuallyLE.rfl : f ≤ᶠ[l] f := EventuallyLE.refl l f @[trans] theorem EventuallyLE.trans (H₁ : f ≤ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₂.mp <| H₁.mono fun _ => le_trans instance : Trans ((· ≤ᶠ[l] ·) : (α → β) → (α → β) → Prop) (· ≤ᶠ[l] ·) (· ≤ᶠ[l] ·) where trans := EventuallyLE.trans @[trans] theorem EventuallyEq.trans_le (H₁ : f =ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₁.le.trans H₂ instance : Trans ((· =ᶠ[l] ·) : (α → β) → (α → β) → Prop) (· ≤ᶠ[l] ·) (· ≤ᶠ[l] ·) where trans := EventuallyEq.trans_le @[trans] theorem EventuallyLE.trans_eq (H₁ : f ≤ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f ≤ᶠ[l] h := H₁.trans H₂.le instance : Trans ((· ≤ᶠ[l] ·) : (α → β) → (α → β) → Prop) (· =ᶠ[l] ·) (· ≤ᶠ[l] ·) where trans := EventuallyLE.trans_eq end Preorder variable {l : Filter α} theorem EventuallyLE.antisymm [PartialOrder β] {l : Filter α} {f g : α → β} (h₁ : f ≤ᶠ[l] g) (h₂ : g ≤ᶠ[l] f) : f =ᶠ[l] g := h₂.mp <| h₁.mono fun _ => le_antisymm theorem eventuallyLE_antisymm_iff [PartialOrder β] {l : Filter α} {f g : α → β} : f =ᶠ[l] g ↔ f ≤ᶠ[l] g ∧ g ≤ᶠ[l] f := by simp only [EventuallyEq, EventuallyLE, le_antisymm_iff, eventually_and] theorem EventuallyLE.le_iff_eq [PartialOrder β] {l : Filter α} {f g : α → β} (h : f ≤ᶠ[l] g) : g ≤ᶠ[l] f ↔ g =ᶠ[l] f := ⟨fun h' => h'.antisymm h, EventuallyEq.le⟩ theorem Eventually.ne_of_lt [Preorder β] {l : Filter α} {f g : α → β} (h : ∀ᶠ x in l, f x < g x) : ∀ᶠ x in l, f x ≠ g x := h.mono fun _ hx => hx.ne theorem Eventually.ne_top_of_lt [Preorder β] [OrderTop β] {l : Filter α} {f g : α → β} (h : ∀ᶠ x in l, f x < g x) : ∀ᶠ x in l, f x ≠ ⊤ := h.mono fun _ hx => hx.ne_top theorem Eventually.lt_top_of_ne [PartialOrder β] [OrderTop β] {l : Filter α} {f : α → β} (h : ∀ᶠ x in l, f x ≠ ⊤) : ∀ᶠ x in l, f x < ⊤ := h.mono fun _ hx => hx.lt_top theorem Eventually.lt_top_iff_ne_top [PartialOrder β] [OrderTop β] {l : Filter α} {f : α → β} : (∀ᶠ x in l, f x < ⊤) ↔ ∀ᶠ x in l, f x ≠ ⊤ := ⟨Eventually.ne_of_lt, Eventually.lt_top_of_ne⟩ @[mono] theorem EventuallyLE.inter {s t s' t' : Set α} {l : Filter α} (h : s ≤ᶠ[l] t) (h' : s' ≤ᶠ[l] t') : (s ∩ s' : Set α) ≤ᶠ[l] (t ∩ t' : Set α) := h'.mp <| h.mono fun _ => And.imp @[mono] theorem EventuallyLE.union {s t s' t' : Set α} {l : Filter α} (h : s ≤ᶠ[l] t) (h' : s' ≤ᶠ[l] t') : (s ∪ s' : Set α) ≤ᶠ[l] (t ∪ t' : Set α) := h'.mp <| h.mono fun _ => Or.imp @[mono] theorem EventuallyLE.compl {s t : Set α} {l : Filter α} (h : s ≤ᶠ[l] t) : (tᶜ : Set α) ≤ᶠ[l] (sᶜ : Set α) := h.mono fun _ => mt @[mono] theorem EventuallyLE.diff {s t s' t' : Set α} {l : Filter α} (h : s ≤ᶠ[l] t) (h' : t' ≤ᶠ[l] s') : (s \ s' : Set α) ≤ᶠ[l] (t \ t' : Set α) := h.inter h'.compl theorem set_eventuallyLE_iff_mem_inf_principal {s t : Set α} {l : Filter α} : s ≤ᶠ[l] t ↔ t ∈ l ⊓ 𝓟 s := eventually_inf_principal.symm theorem set_eventuallyLE_iff_inf_principal_le {s t : Set α} {l : Filter α} : s ≤ᶠ[l] t ↔ l ⊓ 𝓟 s ≤ l ⊓ 𝓟 t := set_eventuallyLE_iff_mem_inf_principal.trans <| by simp only [le_inf_iff, inf_le_left, true_and, le_principal_iff] theorem set_eventuallyEq_iff_inf_principal {s t : Set α} {l : Filter α} : s =ᶠ[l] t ↔ l ⊓ 𝓟 s = l ⊓ 𝓟 t := by simp only [eventuallyLE_antisymm_iff, le_antisymm_iff, set_eventuallyLE_iff_inf_principal_le] theorem EventuallyLE.sup [SemilatticeSup β] {l : Filter α} {f₁ f₂ g₁ g₂ : α → β} (hf : f₁ ≤ᶠ[l] f₂) (hg : g₁ ≤ᶠ[l] g₂) : f₁ ⊔ g₁ ≤ᶠ[l] f₂ ⊔ g₂ := by filter_upwards [hf, hg] with x hfx hgx using sup_le_sup hfx hgx theorem EventuallyLE.sup_le [SemilatticeSup β] {l : Filter α} {f g h : α → β} (hf : f ≤ᶠ[l] h) (hg : g ≤ᶠ[l] h) : f ⊔ g ≤ᶠ[l] h := by filter_upwards [hf, hg] with x hfx hgx using _root_.sup_le hfx hgx theorem EventuallyLE.le_sup_of_le_left [SemilatticeSup β] {l : Filter α} {f g h : α → β} (hf : h ≤ᶠ[l] f) : h ≤ᶠ[l] f ⊔ g := hf.mono fun _ => _root_.le_sup_of_le_left theorem EventuallyLE.le_sup_of_le_right [SemilatticeSup β] {l : Filter α} {f g h : α → β} (hg : h ≤ᶠ[l] g) : h ≤ᶠ[l] f ⊔ g := hg.mono fun _ => _root_.le_sup_of_le_right theorem join_le {f : Filter (Filter α)} {l : Filter α} (h : ∀ᶠ m in f, m ≤ l) : join f ≤ l := fun _ hs => h.mono fun _ hm => hm hs end EventuallyEq end Filter open Filter theorem Set.EqOn.eventuallyEq {α β} {s : Set α} {f g : α → β} (h : EqOn f g s) : f =ᶠ[𝓟 s] g := h theorem Set.EqOn.eventuallyEq_of_mem {α β} {s : Set α} {l : Filter α} {f g : α → β} (h : EqOn f g s) (hl : s ∈ l) : f =ᶠ[l] g := h.eventuallyEq.filter_mono <| Filter.le_principal_iff.2 hl theorem HasSubset.Subset.eventuallyLE {α} {l : Filter α} {s t : Set α} (h : s ⊆ t) : s ≤ᶠ[l] t := Filter.Eventually.of_forall h variable {α β : Type*} {F : Filter α} {G : Filter β} namespace Filter lemma compl_mem_comk {p : Set α → Prop} {he hmono hunion s} : sᶜ ∈ comk p he hmono hunion ↔ p s := by simp end Filter
Mathlib/Order/Filter/Basic.lean
3,171
3,173
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Bhavik Mehta -/ import Mathlib.CategoryTheory.Comma.Over.Basic import Mathlib.CategoryTheory.Discrete.Basic import Mathlib.CategoryTheory.EpiMono import Mathlib.CategoryTheory.Limits.Shapes.Terminal /-! # Binary (co)products We define a category `WalkingPair`, which is the index category for a binary (co)product diagram. A convenience method `pair X Y` constructs the functor from the walking pair, hitting the given objects. We define `prod X Y` and `coprod X Y` as limits and colimits of such functors. Typeclasses `HasBinaryProducts` and `HasBinaryCoproducts` assert the existence of (co)limits shaped as walking pairs. We include lemmas for simplifying equations involving projections and coprojections, and define braiding and associating isomorphisms, and the product comparison morphism. ## References * [Stacks: Products of pairs](https://stacks.math.columbia.edu/tag/001R) * [Stacks: coproducts of pairs](https://stacks.math.columbia.edu/tag/04AN) -/ universe v v₁ u u₁ u₂ open CategoryTheory namespace CategoryTheory.Limits /-- The type of objects for the diagram indexing a binary (co)product. -/ inductive WalkingPair : Type | left | right deriving DecidableEq, Inhabited open WalkingPair /-- The equivalence swapping left and right. -/ def WalkingPair.swap : WalkingPair ≃ WalkingPair where toFun | left => right | right => left invFun | left => right | right => left left_inv j := by cases j <;> rfl right_inv j := by cases j <;> rfl @[simp] theorem WalkingPair.swap_apply_left : WalkingPair.swap left = right := rfl @[simp] theorem WalkingPair.swap_apply_right : WalkingPair.swap right = left := rfl @[simp] theorem WalkingPair.swap_symm_apply_tt : WalkingPair.swap.symm left = right := rfl @[simp] theorem WalkingPair.swap_symm_apply_ff : WalkingPair.swap.symm right = left := rfl /-- An equivalence from `WalkingPair` to `Bool`, sometimes useful when reindexing limits. -/ def WalkingPair.equivBool : WalkingPair ≃ Bool where toFun | left => true | right => false -- to match equiv.sum_equiv_sigma_bool invFun b := Bool.recOn b right left left_inv j := by cases j <;> rfl right_inv b := by cases b <;> rfl @[simp] theorem WalkingPair.equivBool_apply_left : WalkingPair.equivBool left = true := rfl @[simp] theorem WalkingPair.equivBool_apply_right : WalkingPair.equivBool right = false := rfl @[simp] theorem WalkingPair.equivBool_symm_apply_true : WalkingPair.equivBool.symm true = left := rfl @[simp] theorem WalkingPair.equivBool_symm_apply_false : WalkingPair.equivBool.symm false = right := rfl variable {C : Type u} /-- The function on the walking pair, sending the two points to `X` and `Y`. -/ def pairFunction (X Y : C) : WalkingPair → C := fun j => WalkingPair.casesOn j X Y @[simp] theorem pairFunction_left (X Y : C) : pairFunction X Y left = X := rfl @[simp] theorem pairFunction_right (X Y : C) : pairFunction X Y right = Y := rfl variable [Category.{v} C] /-- The diagram on the walking pair, sending the two points to `X` and `Y`. -/ def pair (X Y : C) : Discrete WalkingPair ⥤ C := Discrete.functor fun j => WalkingPair.casesOn j X Y @[simp] theorem pair_obj_left (X Y : C) : (pair X Y).obj ⟨left⟩ = X := rfl @[simp] theorem pair_obj_right (X Y : C) : (pair X Y).obj ⟨right⟩ = Y := rfl section variable {F G : Discrete WalkingPair ⥤ C} (f : F.obj ⟨left⟩ ⟶ G.obj ⟨left⟩) (g : F.obj ⟨right⟩ ⟶ G.obj ⟨right⟩) attribute [local aesop safe tactic (rule_sets := [CategoryTheory])] CategoryTheory.Discrete.discreteCases /-- The natural transformation between two functors out of the walking pair, specified by its components. -/ def mapPair : F ⟶ G where app | ⟨left⟩ => f | ⟨right⟩ => g naturality := fun ⟨X⟩ ⟨Y⟩ ⟨⟨u⟩⟩ => by aesop_cat @[simp] theorem mapPair_left : (mapPair f g).app ⟨left⟩ = f := rfl @[simp] theorem mapPair_right : (mapPair f g).app ⟨right⟩ = g := rfl /-- The natural isomorphism between two functors out of the walking pair, specified by its components. -/ @[simps!] def mapPairIso (f : F.obj ⟨left⟩ ≅ G.obj ⟨left⟩) (g : F.obj ⟨right⟩ ≅ G.obj ⟨right⟩) : F ≅ G := NatIso.ofComponents (fun j ↦ match j with | ⟨left⟩ => f | ⟨right⟩ => g) (fun ⟨⟨u⟩⟩ => by aesop_cat) end /-- Every functor out of the walking pair is naturally isomorphic (actually, equal) to a `pair` -/ @[simps!] def diagramIsoPair (F : Discrete WalkingPair ⥤ C) : F ≅ pair (F.obj ⟨WalkingPair.left⟩) (F.obj ⟨WalkingPair.right⟩) := mapPairIso (Iso.refl _) (Iso.refl _) section variable {D : Type u₁} [Category.{v₁} D] /-- The natural isomorphism between `pair X Y ⋙ F` and `pair (F.obj X) (F.obj Y)`. -/ def pairComp (X Y : C) (F : C ⥤ D) : pair X Y ⋙ F ≅ pair (F.obj X) (F.obj Y) := diagramIsoPair _ end /-- A binary fan is just a cone on a diagram indexing a product. -/ abbrev BinaryFan (X Y : C) := Cone (pair X Y) /-- The first projection of a binary fan. -/ abbrev BinaryFan.fst {X Y : C} (s : BinaryFan X Y) := s.π.app ⟨WalkingPair.left⟩ /-- The second projection of a binary fan. -/ abbrev BinaryFan.snd {X Y : C} (s : BinaryFan X Y) := s.π.app ⟨WalkingPair.right⟩ @[simp] theorem BinaryFan.π_app_left {X Y : C} (s : BinaryFan X Y) : s.π.app ⟨WalkingPair.left⟩ = s.fst := rfl @[simp] theorem BinaryFan.π_app_right {X Y : C} (s : BinaryFan X Y) : s.π.app ⟨WalkingPair.right⟩ = s.snd := rfl /-- Constructs an isomorphism of `BinaryFan`s out of an isomorphism of the tips that commutes with the projections. -/ def BinaryFan.ext {A B : C} {c c' : BinaryFan A B} (e : c.pt ≅ c'.pt) (h₁ : c.fst = e.hom ≫ c'.fst) (h₂ : c.snd = e.hom ≫ c'.snd) : c ≅ c' := Cones.ext e (fun j => by rcases j with ⟨⟨⟩⟩ <;> assumption) @[simp] lemma BinaryFan.ext_hom_hom {A B : C} {c c' : BinaryFan A B} (e : c.pt ≅ c'.pt) (h₁ : c.fst = e.hom ≫ c'.fst) (h₂ : c.snd = e.hom ≫ c'.snd) : (ext e h₁ h₂).hom.hom = e.hom := rfl /-- A convenient way to show that a binary fan is a limit. -/ def BinaryFan.IsLimit.mk {X Y : C} (s : BinaryFan X Y) (lift : ∀ {T : C} (_ : T ⟶ X) (_ : T ⟶ Y), T ⟶ s.pt) (hl₁ : ∀ {T : C} (f : T ⟶ X) (g : T ⟶ Y), lift f g ≫ s.fst = f) (hl₂ : ∀ {T : C} (f : T ⟶ X) (g : T ⟶ Y), lift f g ≫ s.snd = g) (uniq : ∀ {T : C} (f : T ⟶ X) (g : T ⟶ Y) (m : T ⟶ s.pt) (_ : m ≫ s.fst = f) (_ : m ≫ s.snd = g), m = lift f g) : IsLimit s := Limits.IsLimit.mk (fun t => lift (BinaryFan.fst t) (BinaryFan.snd t)) (by rintro t (rfl | rfl) · exact hl₁ _ _ · exact hl₂ _ _) fun _ _ h => uniq _ _ _ (h ⟨WalkingPair.left⟩) (h ⟨WalkingPair.right⟩) theorem BinaryFan.IsLimit.hom_ext {W X Y : C} {s : BinaryFan X Y} (h : IsLimit s) {f g : W ⟶ s.pt} (h₁ : f ≫ s.fst = g ≫ s.fst) (h₂ : f ≫ s.snd = g ≫ s.snd) : f = g := h.hom_ext fun j => Discrete.recOn j fun j => WalkingPair.casesOn j h₁ h₂ /-- A binary cofan is just a cocone on a diagram indexing a coproduct. -/ abbrev BinaryCofan (X Y : C) := Cocone (pair X Y) /-- The first inclusion of a binary cofan. -/ abbrev BinaryCofan.inl {X Y : C} (s : BinaryCofan X Y) := s.ι.app ⟨WalkingPair.left⟩ /-- The second inclusion of a binary cofan. -/ abbrev BinaryCofan.inr {X Y : C} (s : BinaryCofan X Y) := s.ι.app ⟨WalkingPair.right⟩ /-- Constructs an isomorphism of `BinaryCofan`s out of an isomorphism of the tips that commutes with the injections. -/ def BinaryCofan.ext {A B : C} {c c' : BinaryCofan A B} (e : c.pt ≅ c'.pt) (h₁ : c.inl ≫ e.hom = c'.inl) (h₂ : c.inr ≫ e.hom = c'.inr) : c ≅ c' := Cocones.ext e (fun j => by rcases j with ⟨⟨⟩⟩ <;> assumption) @[simp] lemma BinaryCofan.ext_hom_hom {A B : C} {c c' : BinaryCofan A B} (e : c.pt ≅ c'.pt) (h₁ : c.inl ≫ e.hom = c'.inl) (h₂ : c.inr ≫ e.hom = c'.inr) : (ext e h₁ h₂).hom.hom = e.hom := rfl @[simp] theorem BinaryCofan.ι_app_left {X Y : C} (s : BinaryCofan X Y) : s.ι.app ⟨WalkingPair.left⟩ = s.inl := rfl @[simp] theorem BinaryCofan.ι_app_right {X Y : C} (s : BinaryCofan X Y) : s.ι.app ⟨WalkingPair.right⟩ = s.inr := rfl /-- A convenient way to show that a binary cofan is a colimit. -/ def BinaryCofan.IsColimit.mk {X Y : C} (s : BinaryCofan X Y) (desc : ∀ {T : C} (_ : X ⟶ T) (_ : Y ⟶ T), s.pt ⟶ T) (hd₁ : ∀ {T : C} (f : X ⟶ T) (g : Y ⟶ T), s.inl ≫ desc f g = f) (hd₂ : ∀ {T : C} (f : X ⟶ T) (g : Y ⟶ T), s.inr ≫ desc f g = g) (uniq : ∀ {T : C} (f : X ⟶ T) (g : Y ⟶ T) (m : s.pt ⟶ T) (_ : s.inl ≫ m = f) (_ : s.inr ≫ m = g), m = desc f g) : IsColimit s := Limits.IsColimit.mk (fun t => desc (BinaryCofan.inl t) (BinaryCofan.inr t)) (by rintro t (rfl | rfl) · exact hd₁ _ _ · exact hd₂ _ _) fun _ _ h => uniq _ _ _ (h ⟨WalkingPair.left⟩) (h ⟨WalkingPair.right⟩) theorem BinaryCofan.IsColimit.hom_ext {W X Y : C} {s : BinaryCofan X Y} (h : IsColimit s) {f g : s.pt ⟶ W} (h₁ : s.inl ≫ f = s.inl ≫ g) (h₂ : s.inr ≫ f = s.inr ≫ g) : f = g := h.hom_ext fun j => Discrete.recOn j fun j => WalkingPair.casesOn j h₁ h₂ variable {X Y : C} section 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 /-- A binary fan with vertex `P` consists of the two projections `π₁ : P ⟶ X` and `π₂ : P ⟶ Y`. -/ @[simps pt] def BinaryFan.mk {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : BinaryFan X Y where pt := P π := { app := fun | { as := j } => match j with | left => π₁ | right => π₂ } /-- A binary cofan with vertex `P` consists of the two inclusions `ι₁ : X ⟶ P` and `ι₂ : Y ⟶ P`. -/ @[simps pt] def BinaryCofan.mk {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : BinaryCofan X Y where pt := P ι := { app := fun | { as := j } => match j with | left => ι₁ | right => ι₂ } end @[simp] theorem BinaryFan.mk_fst {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (BinaryFan.mk π₁ π₂).fst = π₁ := rfl @[simp] theorem BinaryFan.mk_snd {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (BinaryFan.mk π₁ π₂).snd = π₂ := rfl @[simp] theorem BinaryCofan.mk_inl {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (BinaryCofan.mk ι₁ ι₂).inl = ι₁ := rfl @[simp] theorem BinaryCofan.mk_inr {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (BinaryCofan.mk ι₁ ι₂).inr = ι₂ := rfl /-- Every `BinaryFan` is isomorphic to an application of `BinaryFan.mk`. -/ def isoBinaryFanMk {X Y : C} (c : BinaryFan X Y) : c ≅ BinaryFan.mk c.fst c.snd := Cones.ext (Iso.refl _) fun ⟨l⟩ => by cases l; repeat simp /-- Every `BinaryFan` is isomorphic to an application of `BinaryFan.mk`. -/ def isoBinaryCofanMk {X Y : C} (c : BinaryCofan X Y) : c ≅ BinaryCofan.mk c.inl c.inr := Cocones.ext (Iso.refl _) fun ⟨l⟩ => by cases l; repeat simp /-- This is a more convenient formulation to show that a `BinaryFan` constructed using `BinaryFan.mk` is a limit cone. -/ def BinaryFan.isLimitMk {W : C} {fst : W ⟶ X} {snd : W ⟶ Y} (lift : ∀ s : BinaryFan X Y, s.pt ⟶ W) (fac_left : ∀ s : BinaryFan X Y, lift s ≫ fst = s.fst) (fac_right : ∀ s : BinaryFan X Y, lift s ≫ snd = s.snd) (uniq : ∀ (s : BinaryFan X Y) (m : s.pt ⟶ W) (_ : m ≫ fst = s.fst) (_ : m ≫ snd = s.snd), m = lift s) : IsLimit (BinaryFan.mk fst snd) := { lift := lift fac := fun s j => by rcases j with ⟨⟨⟩⟩ exacts [fac_left s, fac_right s] uniq := fun s m w => uniq s m (w ⟨WalkingPair.left⟩) (w ⟨WalkingPair.right⟩) } /-- This is a more convenient formulation to show that a `BinaryCofan` constructed using `BinaryCofan.mk` is a colimit cocone. -/ def BinaryCofan.isColimitMk {W : C} {inl : X ⟶ W} {inr : Y ⟶ W} (desc : ∀ s : BinaryCofan X Y, W ⟶ s.pt) (fac_left : ∀ s : BinaryCofan X Y, inl ≫ desc s = s.inl) (fac_right : ∀ s : BinaryCofan X Y, inr ≫ desc s = s.inr) (uniq : ∀ (s : BinaryCofan X Y) (m : W ⟶ s.pt) (_ : inl ≫ m = s.inl) (_ : inr ≫ m = s.inr), m = desc s) : IsColimit (BinaryCofan.mk inl inr) := { desc := desc fac := fun s j => by rcases j with ⟨⟨⟩⟩ exacts [fac_left s, fac_right s] uniq := fun s m w => uniq s m (w ⟨WalkingPair.left⟩) (w ⟨WalkingPair.right⟩) } /-- If `s` is a limit binary fan over `X` and `Y`, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `l : W ⟶ s.pt` satisfying `l ≫ s.fst = f` and `l ≫ s.snd = g`. -/ @[simps] def BinaryFan.IsLimit.lift' {W X Y : C} {s : BinaryFan X Y} (h : IsLimit s) (f : W ⟶ X) (g : W ⟶ Y) : { l : W ⟶ s.pt // l ≫ s.fst = f ∧ l ≫ s.snd = g } := ⟨h.lift <| BinaryFan.mk f g, h.fac _ _, h.fac _ _⟩ /-- If `s` is a colimit binary cofan over `X` and `Y`,, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `l : s.pt ⟶ W` satisfying `s.inl ≫ l = f` and `s.inr ≫ l = g`. -/ @[simps] def BinaryCofan.IsColimit.desc' {W X Y : C} {s : BinaryCofan X Y} (h : IsColimit s) (f : X ⟶ W) (g : Y ⟶ W) : { l : s.pt ⟶ W // s.inl ≫ l = f ∧ s.inr ≫ l = g } := ⟨h.desc <| BinaryCofan.mk f g, h.fac _ _, h.fac _ _⟩ /-- Binary products are symmetric. -/ def BinaryFan.isLimitFlip {X Y : C} {c : BinaryFan X Y} (hc : IsLimit c) : IsLimit (BinaryFan.mk c.snd c.fst) := BinaryFan.isLimitMk (fun s => hc.lift (BinaryFan.mk s.snd s.fst)) (fun _ => hc.fac _ _) (fun _ => hc.fac _ _) fun s _ e₁ e₂ => BinaryFan.IsLimit.hom_ext hc (e₂.trans (hc.fac (BinaryFan.mk s.snd s.fst) ⟨WalkingPair.left⟩).symm) (e₁.trans (hc.fac (BinaryFan.mk s.snd s.fst) ⟨WalkingPair.right⟩).symm) theorem BinaryFan.isLimit_iff_isIso_fst {X Y : C} (h : IsTerminal Y) (c : BinaryFan X Y) : Nonempty (IsLimit c) ↔ IsIso c.fst := by constructor · rintro ⟨H⟩ obtain ⟨l, hl, -⟩ := BinaryFan.IsLimit.lift' H (𝟙 X) (h.from X) exact ⟨⟨l, BinaryFan.IsLimit.hom_ext H (by simpa [hl, -Category.comp_id] using Category.comp_id _) (h.hom_ext _ _), hl⟩⟩ · intro exact ⟨BinaryFan.IsLimit.mk _ (fun f _ => f ≫ inv c.fst) (fun _ _ => by simp) (fun _ _ => h.hom_ext _ _) fun _ _ _ e _ => by simp [← e]⟩ theorem BinaryFan.isLimit_iff_isIso_snd {X Y : C} (h : IsTerminal X) (c : BinaryFan X Y) : Nonempty (IsLimit c) ↔ IsIso c.snd := by refine Iff.trans ?_ (BinaryFan.isLimit_iff_isIso_fst h (BinaryFan.mk c.snd c.fst)) exact ⟨fun h => ⟨BinaryFan.isLimitFlip h.some⟩, fun h => ⟨(BinaryFan.isLimitFlip h.some).ofIsoLimit (isoBinaryFanMk c).symm⟩⟩ /-- If `X' ≅ X`, then `X × Y` also is the product of `X'` and `Y`. -/ noncomputable def BinaryFan.isLimitCompLeftIso {X Y X' : C} (c : BinaryFan X Y) (f : X ⟶ X') [IsIso f] (h : IsLimit c) : IsLimit (BinaryFan.mk (c.fst ≫ f) c.snd) := by fapply BinaryFan.isLimitMk · exact fun s => h.lift (BinaryFan.mk (s.fst ≫ inv f) s.snd) · intro s -- Porting note: simp timed out here simp only [Category.comp_id,BinaryFan.π_app_left,IsIso.inv_hom_id, BinaryFan.mk_fst,IsLimit.fac_assoc,eq_self_iff_true,Category.assoc] · intro s -- Porting note: simp timed out here simp only [BinaryFan.π_app_right,BinaryFan.mk_snd,eq_self_iff_true,IsLimit.fac] · intro s m e₁ e₂ -- Porting note: simpa timed out here also apply BinaryFan.IsLimit.hom_ext h · simpa only [BinaryFan.π_app_left,BinaryFan.mk_fst,Category.assoc,IsLimit.fac,IsIso.eq_comp_inv] · simpa only [BinaryFan.π_app_right,BinaryFan.mk_snd,IsLimit.fac] /-- If `Y' ≅ Y`, then `X x Y` also is the product of `X` and `Y'`. -/ noncomputable def BinaryFan.isLimitCompRightIso {X Y Y' : C} (c : BinaryFan X Y) (f : Y ⟶ Y') [IsIso f] (h : IsLimit c) : IsLimit (BinaryFan.mk c.fst (c.snd ≫ f)) := BinaryFan.isLimitFlip <| BinaryFan.isLimitCompLeftIso _ f (BinaryFan.isLimitFlip h) /-- Binary coproducts are symmetric. -/ def BinaryCofan.isColimitFlip {X Y : C} {c : BinaryCofan X Y} (hc : IsColimit c) : IsColimit (BinaryCofan.mk c.inr c.inl) := BinaryCofan.isColimitMk (fun s => hc.desc (BinaryCofan.mk s.inr s.inl)) (fun _ => hc.fac _ _) (fun _ => hc.fac _ _) fun s _ e₁ e₂ => BinaryCofan.IsColimit.hom_ext hc (e₂.trans (hc.fac (BinaryCofan.mk s.inr s.inl) ⟨WalkingPair.left⟩).symm) (e₁.trans (hc.fac (BinaryCofan.mk s.inr s.inl) ⟨WalkingPair.right⟩).symm) theorem BinaryCofan.isColimit_iff_isIso_inl {X Y : C} (h : IsInitial Y) (c : BinaryCofan X Y) : Nonempty (IsColimit c) ↔ IsIso c.inl := by constructor · rintro ⟨H⟩ obtain ⟨l, hl, -⟩ := BinaryCofan.IsColimit.desc' H (𝟙 X) (h.to X) refine ⟨⟨l, hl, BinaryCofan.IsColimit.hom_ext H (?_) (h.hom_ext _ _)⟩⟩ rw [Category.comp_id] have e : (inl c ≫ l) ≫ inl c = 𝟙 X ≫ inl c := congrArg (·≫inl c) hl rwa [Category.assoc,Category.id_comp] at e · intro exact ⟨BinaryCofan.IsColimit.mk _ (fun f _ => inv c.inl ≫ f) (fun _ _ => IsIso.hom_inv_id_assoc _ _) (fun _ _ => h.hom_ext _ _) fun _ _ _ e _ => (IsIso.eq_inv_comp _).mpr e⟩ theorem BinaryCofan.isColimit_iff_isIso_inr {X Y : C} (h : IsInitial X) (c : BinaryCofan X Y) : Nonempty (IsColimit c) ↔ IsIso c.inr := by refine Iff.trans ?_ (BinaryCofan.isColimit_iff_isIso_inl h (BinaryCofan.mk c.inr c.inl)) exact ⟨fun h => ⟨BinaryCofan.isColimitFlip h.some⟩, fun h => ⟨(BinaryCofan.isColimitFlip h.some).ofIsoColimit (isoBinaryCofanMk c).symm⟩⟩ /-- If `X' ≅ X`, then `X ⨿ Y` also is the coproduct of `X'` and `Y`. -/ noncomputable def BinaryCofan.isColimitCompLeftIso {X Y X' : C} (c : BinaryCofan X Y) (f : X' ⟶ X) [IsIso f] (h : IsColimit c) : IsColimit (BinaryCofan.mk (f ≫ c.inl) c.inr) := by fapply BinaryCofan.isColimitMk · exact fun s => h.desc (BinaryCofan.mk (inv f ≫ s.inl) s.inr) · intro s -- Porting note: simp timed out here too simp only [IsColimit.fac,BinaryCofan.ι_app_left,eq_self_iff_true, Category.assoc,BinaryCofan.mk_inl,IsIso.hom_inv_id_assoc] · intro s -- Porting note: simp timed out here too simp only [IsColimit.fac,BinaryCofan.ι_app_right,eq_self_iff_true,BinaryCofan.mk_inr] · intro s m e₁ e₂ apply BinaryCofan.IsColimit.hom_ext h · rw [← cancel_epi f] -- Porting note: simp timed out here too simpa only [IsColimit.fac,BinaryCofan.ι_app_left,eq_self_iff_true, Category.assoc,BinaryCofan.mk_inl,IsIso.hom_inv_id_assoc] using e₁ -- Porting note: simp timed out here too · simpa only [IsColimit.fac,BinaryCofan.ι_app_right,eq_self_iff_true,BinaryCofan.mk_inr] /-- If `Y' ≅ Y`, then `X ⨿ Y` also is the coproduct of `X` and `Y'`. -/ noncomputable def BinaryCofan.isColimitCompRightIso {X Y Y' : C} (c : BinaryCofan X Y) (f : Y' ⟶ Y) [IsIso f] (h : IsColimit c) : IsColimit (BinaryCofan.mk c.inl (f ≫ c.inr)) := BinaryCofan.isColimitFlip <| BinaryCofan.isColimitCompLeftIso _ f (BinaryCofan.isColimitFlip h) /-- An abbreviation for `HasLimit (pair X Y)`. -/ abbrev HasBinaryProduct (X Y : C) := HasLimit (pair X Y) /-- An abbreviation for `HasColimit (pair X Y)`. -/ abbrev HasBinaryCoproduct (X Y : C) := HasColimit (pair X Y) /-- If we have a product of `X` and `Y`, we can access it using `prod X Y` or `X ⨯ Y`. -/ noncomputable abbrev prod (X Y : C) [HasBinaryProduct X Y] := limit (pair X Y) /-- If we have a coproduct of `X` and `Y`, we can access it using `coprod X Y` or `X ⨿ Y`. -/ noncomputable abbrev coprod (X Y : C) [HasBinaryCoproduct X Y] := colimit (pair X Y) /-- Notation for the product -/ notation:20 X " ⨯ " Y:20 => prod X Y /-- Notation for the coproduct -/ notation:20 X " ⨿ " Y:20 => coprod X Y /-- The projection map to the first component of the product. -/ noncomputable abbrev prod.fst {X Y : C} [HasBinaryProduct X Y] : X ⨯ Y ⟶ X := limit.π (pair X Y) ⟨WalkingPair.left⟩ /-- The projection map to the second component of the product. -/ noncomputable abbrev prod.snd {X Y : C} [HasBinaryProduct X Y] : X ⨯ Y ⟶ Y := limit.π (pair X Y) ⟨WalkingPair.right⟩ /-- The inclusion map from the first component of the coproduct. -/ noncomputable abbrev coprod.inl {X Y : C} [HasBinaryCoproduct X Y] : X ⟶ X ⨿ Y := colimit.ι (pair X Y) ⟨WalkingPair.left⟩ /-- The inclusion map from the second component of the coproduct. -/ noncomputable abbrev coprod.inr {X Y : C} [HasBinaryCoproduct X Y] : Y ⟶ X ⨿ Y := colimit.ι (pair X Y) ⟨WalkingPair.right⟩ /-- The binary fan constructed from the projection maps is a limit. -/ noncomputable def prodIsProd (X Y : C) [HasBinaryProduct X Y] : IsLimit (BinaryFan.mk (prod.fst : X ⨯ Y ⟶ X) prod.snd) := (limit.isLimit _).ofIsoLimit (Cones.ext (Iso.refl _) (fun ⟨u⟩ => by cases u · dsimp; simp only [Category.id_comp]; rfl · dsimp; simp only [Category.id_comp]; rfl )) /-- The binary cofan constructed from the coprojection maps is a colimit. -/ noncomputable def coprodIsCoprod (X Y : C) [HasBinaryCoproduct X Y] : IsColimit (BinaryCofan.mk (coprod.inl : X ⟶ X ⨿ Y) coprod.inr) := (colimit.isColimit _).ofIsoColimit (Cocones.ext (Iso.refl _) (fun ⟨u⟩ => by cases u · dsimp; simp only [Category.comp_id] · dsimp; simp only [Category.comp_id] )) @[ext 1100] theorem prod.hom_ext {W X Y : C} [HasBinaryProduct X Y] {f g : W ⟶ X ⨯ Y} (h₁ : f ≫ prod.fst = g ≫ prod.fst) (h₂ : f ≫ prod.snd = g ≫ prod.snd) : f = g := BinaryFan.IsLimit.hom_ext (limit.isLimit _) h₁ h₂ @[ext 1100] theorem coprod.hom_ext {W X Y : C} [HasBinaryCoproduct X Y] {f g : X ⨿ Y ⟶ W} (h₁ : coprod.inl ≫ f = coprod.inl ≫ g) (h₂ : coprod.inr ≫ f = coprod.inr ≫ g) : f = g := BinaryCofan.IsColimit.hom_ext (colimit.isColimit _) h₁ h₂ /-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `prod.lift f g : W ⟶ X ⨯ Y`. -/ noncomputable abbrev prod.lift {W X Y : C} [HasBinaryProduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⨯ Y := limit.lift _ (BinaryFan.mk f g) /-- diagonal arrow of the binary product in the category `fam I` -/ noncomputable abbrev diag (X : C) [HasBinaryProduct X X] : X ⟶ X ⨯ X := prod.lift (𝟙 _) (𝟙 _) /-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `coprod.desc f g : X ⨿ Y ⟶ W`. -/ noncomputable abbrev coprod.desc {W X Y : C} [HasBinaryCoproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : X ⨿ Y ⟶ W := colimit.desc _ (BinaryCofan.mk f g) /-- codiagonal arrow of the binary coproduct -/ noncomputable abbrev codiag (X : C) [HasBinaryCoproduct X X] : X ⨿ X ⟶ X := coprod.desc (𝟙 _) (𝟙 _) @[reassoc] theorem prod.lift_fst {W X Y : C} [HasBinaryProduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : prod.lift f g ≫ prod.fst = f := limit.lift_π _ _ @[reassoc] theorem prod.lift_snd {W X Y : C} [HasBinaryProduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : prod.lift f g ≫ prod.snd = g := limit.lift_π _ _ @[reassoc] theorem coprod.inl_desc {W X Y : C} [HasBinaryCoproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : coprod.inl ≫ coprod.desc f g = f := colimit.ι_desc _ _ @[reassoc] theorem coprod.inr_desc {W X Y : C} [HasBinaryCoproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : coprod.inr ≫ coprod.desc f g = g := colimit.ι_desc _ _ instance prod.mono_lift_of_mono_left {W X Y : C} [HasBinaryProduct X Y] (f : W ⟶ X) (g : W ⟶ Y) [Mono f] : Mono (prod.lift f g) := mono_of_mono_fac <| prod.lift_fst _ _ instance prod.mono_lift_of_mono_right {W X Y : C} [HasBinaryProduct X Y] (f : W ⟶ X) (g : W ⟶ Y) [Mono g] : Mono (prod.lift f g) := mono_of_mono_fac <| prod.lift_snd _ _ instance coprod.epi_desc_of_epi_left {W X Y : C} [HasBinaryCoproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) [Epi f] : Epi (coprod.desc f g) := epi_of_epi_fac <| coprod.inl_desc _ _ instance coprod.epi_desc_of_epi_right {W X Y : C} [HasBinaryCoproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) [Epi g] : Epi (coprod.desc f g) := epi_of_epi_fac <| coprod.inr_desc _ _ /-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `l : W ⟶ X ⨯ Y` satisfying `l ≫ Prod.fst = f` and `l ≫ Prod.snd = g`. -/ noncomputable def prod.lift' {W X Y : C} [HasBinaryProduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : { l : W ⟶ X ⨯ Y // l ≫ prod.fst = f ∧ l ≫ prod.snd = g } := ⟨prod.lift f g, prod.lift_fst _ _, prod.lift_snd _ _⟩ /-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `l : X ⨿ Y ⟶ W` satisfying `coprod.inl ≫ l = f` and `coprod.inr ≫ l = g`. -/ noncomputable def coprod.desc' {W X Y : C} [HasBinaryCoproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : { l : X ⨿ Y ⟶ W // coprod.inl ≫ l = f ∧ coprod.inr ≫ l = g } := ⟨coprod.desc f g, coprod.inl_desc _ _, coprod.inr_desc _ _⟩ /-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of morphisms `f : W ⟶ Y` and `g : X ⟶ Z` induces a morphism `prod.map f g : W ⨯ X ⟶ Y ⨯ Z`. -/ noncomputable def prod.map {W X Y Z : C} [HasBinaryProduct W X] [HasBinaryProduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨯ X ⟶ Y ⨯ Z := limMap (mapPair f g) /-- If the coproducts `W ⨿ X` and `Y ⨿ Z` exist, then every pair of morphisms `f : W ⟶ Y` and `g : W ⟶ Z` induces a morphism `coprod.map f g : W ⨿ X ⟶ Y ⨿ Z`. -/ noncomputable def coprod.map {W X Y Z : C} [HasBinaryCoproduct W X] [HasBinaryCoproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨿ X ⟶ Y ⨿ Z := colimMap (mapPair f g) noncomputable section ProdLemmas -- Making the reassoc version of this a simp lemma seems to be more harmful than helpful. @[reassoc, simp] theorem prod.comp_lift {V W X Y : C} [HasBinaryProduct X Y] (f : V ⟶ W) (g : W ⟶ X) (h : W ⟶ Y) : f ≫ prod.lift g h = prod.lift (f ≫ g) (f ≫ h) := by ext <;> simp theorem prod.comp_diag {X Y : C} [HasBinaryProduct Y Y] (f : X ⟶ Y) : f ≫ diag Y = prod.lift f f := by simp @[reassoc (attr := simp)] theorem prod.map_fst {W X Y Z : C} [HasBinaryProduct W X] [HasBinaryProduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.fst = prod.fst ≫ f := limMap_π _ _ @[reassoc (attr := simp)] theorem prod.map_snd {W X Y Z : C} [HasBinaryProduct W X] [HasBinaryProduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.snd = prod.snd ≫ g := limMap_π _ _ @[simp] theorem prod.map_id_id {X Y : C} [HasBinaryProduct X Y] : prod.map (𝟙 X) (𝟙 Y) = 𝟙 _ := by ext <;> simp @[simp] theorem prod.lift_fst_snd {X Y : C} [HasBinaryProduct X Y] : prod.lift prod.fst prod.snd = 𝟙 (X ⨯ Y) := by ext <;> simp @[reassoc (attr := simp)] theorem prod.lift_map {V W X Y Z : C} [HasBinaryProduct W X] [HasBinaryProduct Y Z] (f : V ⟶ W) (g : V ⟶ X) (h : W ⟶ Y) (k : X ⟶ Z) : prod.lift f g ≫ prod.map h k = prod.lift (f ≫ h) (g ≫ k) := by ext <;> simp @[simp] theorem prod.lift_fst_comp_snd_comp {W X Y Z : C} [HasBinaryProduct W Y] [HasBinaryProduct X Z] (g : W ⟶ X) (g' : Y ⟶ Z) : prod.lift (prod.fst ≫ g) (prod.snd ≫ g') = prod.map g g' := by rw [← prod.lift_map] simp -- We take the right hand side here to be simp normal form, as this way composition lemmas for -- `f ≫ h` and `g ≫ k` can fire (eg `id_comp`) , while `map_fst` and `map_snd` can still work just -- as well. @[reassoc (attr := simp)] theorem prod.map_map {A₁ A₂ A₃ B₁ B₂ B₃ : C} [HasBinaryProduct A₁ B₁] [HasBinaryProduct A₂ B₂] [HasBinaryProduct A₃ B₃] (f : A₁ ⟶ A₂) (g : B₁ ⟶ B₂) (h : A₂ ⟶ A₃) (k : B₂ ⟶ B₃) : prod.map f g ≫ prod.map h k = prod.map (f ≫ h) (g ≫ k) := by ext <;> simp -- TODO: is it necessary to weaken the assumption here? @[reassoc] theorem prod.map_swap {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) [HasLimitsOfShape (Discrete WalkingPair) C] : prod.map (𝟙 X) f ≫ prod.map g (𝟙 B) = prod.map g (𝟙 A) ≫ prod.map (𝟙 Y) f := by simp @[reassoc] theorem prod.map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) [HasBinaryProduct X W] [HasBinaryProduct Z W] [HasBinaryProduct Y W] : prod.map (f ≫ g) (𝟙 W) = prod.map f (𝟙 W) ≫ prod.map g (𝟙 W) := by simp @[reassoc] theorem prod.map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) [HasBinaryProduct W X] [HasBinaryProduct W Y] [HasBinaryProduct W Z] : prod.map (𝟙 W) (f ≫ g) = prod.map (𝟙 W) f ≫ prod.map (𝟙 W) g := by simp /-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of isomorphisms `f : W ≅ Y` and `g : X ≅ Z` induces an isomorphism `prod.mapIso f g : W ⨯ X ≅ Y ⨯ Z`. -/ @[simps] def prod.mapIso {W X Y Z : C} [HasBinaryProduct W X] [HasBinaryProduct Y Z] (f : W ≅ Y) (g : X ≅ Z) : W ⨯ X ≅ Y ⨯ Z where hom := prod.map f.hom g.hom inv := prod.map f.inv g.inv instance isIso_prod {W X Y Z : C} [HasBinaryProduct W X] [HasBinaryProduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) [IsIso f] [IsIso g] : IsIso (prod.map f g) := (prod.mapIso (asIso f) (asIso g)).isIso_hom instance prod.map_mono {C : Type*} [Category C] {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) [Mono f] [Mono g] [HasBinaryProduct W X] [HasBinaryProduct Y Z] : Mono (prod.map f g) := ⟨fun i₁ i₂ h => by ext · rw [← cancel_mono f] simpa using congr_arg (fun f => f ≫ prod.fst) h · rw [← cancel_mono g] simpa using congr_arg (fun f => f ≫ prod.snd) h⟩ @[reassoc] theorem prod.diag_map {X Y : C} (f : X ⟶ Y) [HasBinaryProduct X X] [HasBinaryProduct Y Y] : diag X ≫ prod.map f f = f ≫ diag Y := by simp @[reassoc] theorem prod.diag_map_fst_snd {X Y : C} [HasBinaryProduct X Y] [HasBinaryProduct (X ⨯ Y) (X ⨯ Y)] : diag (X ⨯ Y) ≫ prod.map prod.fst prod.snd = 𝟙 (X ⨯ Y) := by simp @[reassoc] theorem prod.diag_map_fst_snd_comp [HasLimitsOfShape (Discrete WalkingPair) C] {X X' Y Y' : C} (g : X ⟶ Y) (g' : X' ⟶ Y') : diag (X ⨯ X') ≫ prod.map (prod.fst ≫ g) (prod.snd ≫ g') = prod.map g g' := by simp instance {X : C} [HasBinaryProduct X X] : IsSplitMono (diag X) := IsSplitMono.mk' { retraction := prod.fst } end ProdLemmas noncomputable section CoprodLemmas @[reassoc, simp] theorem coprod.desc_comp {V W X Y : C} [HasBinaryCoproduct X Y] (f : V ⟶ W) (g : X ⟶ V) (h : Y ⟶ V) : coprod.desc g h ≫ f = coprod.desc (g ≫ f) (h ≫ f) := by
ext <;> simp theorem coprod.diag_comp {X Y : C} [HasBinaryCoproduct X X] (f : X ⟶ Y) :
Mathlib/CategoryTheory/Limits/Shapes/BinaryProducts.lean
740
742
/- 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.Support import Mathlib.Algebra.Polynomial.Basic import Mathlib.Data.Nat.Choose.Sum import Mathlib.Algebra.CharP.Defs /-! # Theory of univariate polynomials The theorems include formulas for computing coefficients, such as `coeff_add`, `coeff_sum`, `coeff_mul` -/ noncomputable section open Finsupp Finset AddMonoidAlgebra open Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b : R} {n m : ℕ} variable [Semiring R] {p q r : R[X]} section Coeff @[simp] theorem coeff_add (p q : R[X]) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := by rcases p with ⟨⟩ rcases q with ⟨⟩ simp_rw [← ofFinsupp_add, coeff] exact Finsupp.add_apply _ _ _ @[simp] theorem coeff_smul [SMulZeroClass S R] (r : S) (p : R[X]) (n : ℕ) : coeff (r • p) n = r • coeff p n := by rcases p with ⟨⟩ simp_rw [← ofFinsupp_smul, coeff] exact Finsupp.smul_apply _ _ _ theorem support_smul [SMulZeroClass S R] (r : S) (p : R[X]) : support (r • p) ⊆ support p := by intro i hi simp? [mem_support_iff] at hi ⊢ says simp only [mem_support_iff, coeff_smul, ne_eq] at hi ⊢ contrapose! hi simp [hi] open scoped Pointwise in theorem card_support_mul_le : #(p * q).support ≤ #p.support * #q.support := by calc #(p * q).support _ = #(p.toFinsupp * q.toFinsupp).support := by rw [← support_toFinsupp, toFinsupp_mul] _ ≤ #(p.toFinsupp.support + q.toFinsupp.support) := Finset.card_le_card (AddMonoidAlgebra.support_mul p.toFinsupp q.toFinsupp) _ ≤ #p.support * #q.support := Finset.card_image₂_le .. /-- `Polynomial.sum` as a linear map. -/ @[simps] def lsum {R A M : Type*} [Semiring R] [Semiring A] [AddCommMonoid M] [Module R A] [Module R M] (f : ℕ → A →ₗ[R] M) : A[X] →ₗ[R] M where toFun p := p.sum (f · ·) map_add' p q := sum_add_index p q _ (fun n => (f n).map_zero) fun n _ _ => (f n).map_add _ _ map_smul' c p := by rw [sum_eq_of_subset (f · ·) (fun n => (f n).map_zero) (support_smul c p)] simp only [sum_def, Finset.smul_sum, coeff_smul, LinearMap.map_smul, RingHom.id_apply] variable (R) in /-- The nth coefficient, as a linear map. -/ def lcoeff (n : ℕ) : R[X] →ₗ[R] R where toFun p := coeff p n map_add' p q := coeff_add p q n map_smul' r p := coeff_smul r p n @[simp] theorem lcoeff_apply (n : ℕ) (f : R[X]) : lcoeff R n f = coeff f n := rfl @[simp] theorem finset_sum_coeff {ι : Type*} (s : Finset ι) (f : ι → R[X]) (n : ℕ) : coeff (∑ b ∈ s, f b) n = ∑ b ∈ s, coeff (f b) n := map_sum (lcoeff R n) _ _ lemma coeff_list_sum (l : List R[X]) (n : ℕ) : l.sum.coeff n = (l.map (lcoeff R n)).sum := map_list_sum (lcoeff R n) _ lemma coeff_list_sum_map {ι : Type*} (l : List ι) (f : ι → R[X]) (n : ℕ) : (l.map f).sum.coeff n = (l.map (fun a => (f a).coeff n)).sum := by simp_rw [coeff_list_sum, List.map_map, Function.comp_def, lcoeff_apply] @[simp] theorem coeff_sum [Semiring S] (n : ℕ) (f : ℕ → R → S[X]) : coeff (p.sum f) n = p.sum fun a b => coeff (f a b) n := by rcases p with ⟨⟩ simp [Polynomial.sum, support_ofFinsupp, coeff_ofFinsupp] /-- Decomposes the coefficient of the product `p * q` as a sum over `antidiagonal`. A version which sums over `range (n + 1)` can be obtained by using `Finset.Nat.sum_antidiagonal_eq_sum_range_succ`. -/ theorem coeff_mul (p q : R[X]) (n : ℕ) : coeff (p * q) n = ∑ x ∈ antidiagonal n, coeff p x.1 * coeff q x.2 := by rcases p with ⟨p⟩; rcases q with ⟨q⟩ simp_rw [← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.mul_apply_antidiagonal p q n _ Finset.mem_antidiagonal @[simp] theorem mul_coeff_zero (p q : R[X]) : coeff (p * q) 0 = coeff p 0 * coeff q 0 := by simp [coeff_mul] theorem mul_coeff_one (p q : R[X]) : coeff (p * q) 1 = coeff p 0 * coeff q 1 + coeff p 1 * coeff q 0 := by rw [coeff_mul, Nat.antidiagonal_eq_map] simp [sum_range_succ] /-- `constantCoeff p` returns the constant term of the polynomial `p`, defined as `coeff p 0`. This is a ring homomorphism. -/ @[simps] def constantCoeff : R[X] →+* R where toFun p := coeff p 0 map_one' := coeff_one_zero map_mul' := mul_coeff_zero map_zero' := coeff_zero 0 map_add' p q := coeff_add p q 0 theorem isUnit_C {x : R} : IsUnit (C x) ↔ IsUnit x := ⟨fun h => (congr_arg IsUnit coeff_C_zero).mp (h.map <| @constantCoeff R _), fun h => h.map C⟩ theorem coeff_mul_X_zero (p : R[X]) : coeff (p * X) 0 = 0 := by simp theorem coeff_X_mul_zero (p : R[X]) : coeff (X * p) 0 = 0 := by simp theorem coeff_C_mul_X_pow (x : R) (k n : ℕ) : coeff (C x * X ^ k : R[X]) n = if n = k then x else 0 := by rw [C_mul_X_pow_eq_monomial, coeff_monomial] congr 1 simp [eq_comm] theorem coeff_C_mul_X (x : R) (n : ℕ) : coeff (C x * X : R[X]) n = if n = 1 then x else 0 := by rw [← pow_one X, coeff_C_mul_X_pow] @[simp] theorem coeff_C_mul (p : R[X]) : coeff (C a * p) n = a * coeff p n := by rcases p with ⟨p⟩ simp_rw [← monomial_zero_left, ← ofFinsupp_single, ← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.single_zero_mul_apply p a n theorem C_mul' (a : R) (f : R[X]) : C a * f = a • f := by ext rw [coeff_C_mul, coeff_smul, smul_eq_mul] @[simp] theorem coeff_mul_C (p : R[X]) (n : ℕ) (a : R) : coeff (p * C a) n = coeff p n * a := by rcases p with ⟨p⟩ simp_rw [← monomial_zero_left, ← ofFinsupp_single, ← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.mul_single_zero_apply p a n @[simp] lemma coeff_mul_natCast {a k : ℕ} : coeff (p * (a : R[X])) k = coeff p k * (↑a : R) := coeff_mul_C _ _ _ @[simp] lemma coeff_natCast_mul {a k : ℕ} : coeff ((a : R[X]) * p) k = a * coeff p k := coeff_C_mul _ @[simp] lemma coeff_mul_ofNat {a k : ℕ} [Nat.AtLeastTwo a] : coeff (p * (ofNat(a) : R[X])) k = coeff p k * ofNat(a) := coeff_mul_C _ _ _ @[simp] lemma coeff_ofNat_mul {a k : ℕ} [Nat.AtLeastTwo a] : coeff ((ofNat(a) : R[X]) * p) k = ofNat(a) * coeff p k := coeff_C_mul _ @[simp] lemma coeff_mul_intCast [Ring S] {p : S[X]} {a : ℤ} {k : ℕ} : coeff (p * (a : S[X])) k = coeff p k * (↑a : S) := coeff_mul_C _ _ _ @[simp] lemma coeff_intCast_mul [Ring S] {p : S[X]} {a : ℤ} {k : ℕ} : coeff ((a : S[X]) * p) k = a * coeff p k := coeff_C_mul _ @[simp] theorem coeff_X_pow (k n : ℕ) : coeff (X ^ k : R[X]) n = if n = k then 1 else 0 := by simp only [one_mul, RingHom.map_one, ← coeff_C_mul_X_pow] theorem coeff_X_pow_self (n : ℕ) : coeff (X ^ n : R[X]) n = 1 := by simp section Fewnomials open Finset theorem support_binomial {k m : ℕ} (hkm : k ≠ m) {x y : R} (hx : x ≠ 0) (hy : y ≠ 0) : support (C x * X ^ k + C y * X ^ m) = {k, m} := by apply subset_antisymm (support_binomial' k m x y) simp_rw [insert_subset_iff, singleton_subset_iff, mem_support_iff, coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow, if_neg hkm, if_neg hkm.symm, mul_zero, zero_add, add_zero, Ne, hx, hy, not_false_eq_true, and_true] theorem support_trinomial {k m n : ℕ} (hkm : k < m) (hmn : m < n) {x y z : R} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : support (C x * X ^ k + C y * X ^ m + C z * X ^ n) = {k, m, n} := by apply subset_antisymm (support_trinomial' k m n x y z) simp_rw [insert_subset_iff, singleton_subset_iff, mem_support_iff, coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow, if_neg hkm.ne, if_neg hkm.ne', if_neg hmn.ne, if_neg hmn.ne', if_neg (hkm.trans hmn).ne, if_neg (hkm.trans hmn).ne', mul_zero, add_zero, zero_add, Ne, hx, hy, hz, not_false_eq_true, and_true] theorem card_support_binomial {k m : ℕ} (h : k ≠ m) {x y : R} (hx : x ≠ 0) (hy : y ≠ 0) : #(support (C x * X ^ k + C y * X ^ m)) = 2 := by rw [support_binomial h hx hy, card_insert_of_not_mem (mt mem_singleton.mp h), card_singleton] theorem card_support_trinomial {k m n : ℕ} (hkm : k < m) (hmn : m < n) {x y z : R} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : #(support (C x * X ^ k + C y * X ^ m + C z * X ^ n)) = 3 := by rw [support_trinomial hkm hmn hx hy hz, card_insert_of_not_mem (mt mem_insert.mp (not_or_intro hkm.ne (mt mem_singleton.mp (hkm.trans hmn).ne))), card_insert_of_not_mem (mt mem_singleton.mp hmn.ne), card_singleton] end Fewnomials @[simp] theorem coeff_mul_X_pow (p : R[X]) (n d : ℕ) : coeff (p * Polynomial.X ^ n) (d + n) = coeff p d := by rw [coeff_mul, Finset.sum_eq_single (d, n), coeff_X_pow, if_pos rfl, mul_one] · rintro ⟨i, j⟩ h1 h2 rw [coeff_X_pow, if_neg, mul_zero] rintro rfl apply h2 rw [mem_antidiagonal, add_right_cancel_iff] at h1 subst h1 rfl · exact fun h1 => (h1 (mem_antidiagonal.2 rfl)).elim @[simp] theorem coeff_X_pow_mul (p : R[X]) (n d : ℕ) : coeff (Polynomial.X ^ n * p) (d + n) = coeff p d := by rw [(commute_X_pow p n).eq, coeff_mul_X_pow] theorem coeff_mul_X_pow' (p : R[X]) (n d : ℕ) : (p * X ^ n).coeff d = ite (n ≤ d) (p.coeff (d - n)) 0 := by split_ifs with h · rw [← tsub_add_cancel_of_le h, coeff_mul_X_pow, add_tsub_cancel_right] · refine (coeff_mul _ _ _).trans (Finset.sum_eq_zero fun x hx => ?_) rw [coeff_X_pow, if_neg, mul_zero] exact ((le_of_add_le_right (mem_antidiagonal.mp hx).le).trans_lt <| not_le.mp h).ne theorem coeff_X_pow_mul' (p : R[X]) (n d : ℕ) : (X ^ n * p).coeff d = ite (n ≤ d) (p.coeff (d - n)) 0 := by rw [(commute_X_pow p n).eq, coeff_mul_X_pow'] @[simp] theorem coeff_mul_X (p : R[X]) (n : ℕ) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n @[simp] theorem coeff_X_mul (p : R[X]) (n : ℕ) : coeff (X * p) (n + 1) = coeff p n := by rw [(commute_X p).eq, coeff_mul_X] theorem coeff_mul_monomial (p : R[X]) (n d : ℕ) (r : R) : coeff (p * monomial n r) (d + n) = coeff p d * r := by rw [← C_mul_X_pow_eq_monomial, ← X_pow_mul, ← mul_assoc, coeff_mul_C, coeff_mul_X_pow] theorem coeff_monomial_mul (p : R[X]) (n d : ℕ) (r : R) : coeff (monomial n r * p) (d + n) = r * coeff p d := by rw [← C_mul_X_pow_eq_monomial, mul_assoc, coeff_C_mul, X_pow_mul, coeff_mul_X_pow] -- This can already be proved by `simp`. theorem coeff_mul_monomial_zero (p : R[X]) (d : ℕ) (r : R) : coeff (p * monomial 0 r) d = coeff p d * r := coeff_mul_monomial p 0 d r -- This can already be proved by `simp`. theorem coeff_monomial_zero_mul (p : R[X]) (d : ℕ) (r : R) : coeff (monomial 0 r * p) d = r * coeff p d := coeff_monomial_mul p 0 d r theorem mul_X_pow_eq_zero {p : R[X]} {n : ℕ} (H : p * X ^ n = 0) : p = 0 := ext fun k => (coeff_mul_X_pow p n k).symm.trans <| ext_iff.1 H (k + n) theorem isRegular_X_pow (n : ℕ) : IsRegular (X ^ n : R[X]) := by suffices IsLeftRegular (X^n : R[X]) from ⟨this, this.right_of_commute (fun p => commute_X_pow p n)⟩ intro P Q (hPQ : X^n * P = X^n * Q) ext i rw [← coeff_X_pow_mul P n i, hPQ, coeff_X_pow_mul Q n i] @[simp] theorem isRegular_X : IsRegular (X : R[X]) := pow_one (X : R[X]) ▸ isRegular_X_pow 1 theorem coeff_X_add_C_pow (r : R) (n k : ℕ) : ((X + C r) ^ n).coeff k = r ^ (n - k) * (n.choose k : R) := by rw [(commute_X (C r : R[X])).add_pow, ← lcoeff_apply, map_sum] simp only [one_pow, mul_one, lcoeff_apply, ← C_eq_natCast, ← C_pow, coeff_mul_C, Nat.cast_id] rw [Finset.sum_eq_single k, coeff_X_pow_self, one_mul] · intro _ _ h simp [coeff_X_pow, h.symm] · simp only [coeff_X_pow_self, one_mul, not_lt, Finset.mem_range] intro h rw [Nat.choose_eq_zero_of_lt h, Nat.cast_zero, mul_zero] theorem coeff_X_add_one_pow (R : Type*) [Semiring R] (n k : ℕ) : ((X + 1) ^ n).coeff k = (n.choose k : R) := by rw [← C_1, coeff_X_add_C_pow, one_pow, one_mul] theorem coeff_one_add_X_pow (R : Type*) [Semiring R] (n k : ℕ) : ((1 + X) ^ n).coeff k = (n.choose k : R) := by rw [add_comm _ X, coeff_X_add_one_pow] theorem C_dvd_iff_dvd_coeff (r : R) (φ : R[X]) : C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i := by constructor · rintro ⟨φ, rfl⟩ c rw [coeff_C_mul] apply dvd_mul_right · intro h choose c hc using h classical let c' : ℕ → R := fun i => if i ∈ φ.support then c i else 0 let ψ : R[X] := ∑ i ∈ φ.support, monomial i (c' i) use ψ ext i simp only [c', ψ, coeff_C_mul, mem_support_iff, coeff_monomial, finset_sum_coeff, Finset.sum_ite_eq'] split_ifs with hi · rw [hc] · rw [Classical.not_not] at hi rwa [mul_zero] theorem smul_eq_C_mul (a : R) : a • p = C a * p := by simp [ext_iff] theorem update_eq_add_sub_coeff {R : Type*} [Ring R] (p : R[X]) (n : ℕ) (a : R) : p.update n a = p + Polynomial.C (a - p.coeff n) * Polynomial.X ^ n := by ext rw [coeff_update_apply, coeff_add, coeff_C_mul_X_pow] split_ifs with h <;> simp [h] end Coeff section cast theorem natCast_coeff_zero {n : ℕ} {R : Type*} [Semiring R] : (n : R[X]).coeff 0 = n := by simp only [coeff_natCast_ite, ite_true] @[norm_cast] theorem natCast_inj {m n : ℕ} {R : Type*} [Semiring R] [CharZero R] : (↑m : R[X]) = ↑n ↔ m = n := by constructor · intro h apply_fun fun p => p.coeff 0 at h
simpa using h · rintro rfl
Mathlib/Algebra/Polynomial/Coeff.lean
346
347
/- Copyright (c) 2022 Praneeth Kolichala. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Praneeth Kolichala -/ import Mathlib.Topology.Homotopy.Path import Mathlib.Topology.Homotopy.Equiv /-! # Contractible spaces In this file, we define `ContractibleSpace`, a space that is homotopy equivalent to `Unit`. -/ noncomputable section namespace ContinuousMap variable {X Y Z : Type*} [TopologicalSpace X] [TopologicalSpace Y] [TopologicalSpace Z] /-- A map is nullhomotopic if it is homotopic to a constant map. -/ def Nullhomotopic (f : C(X, Y)) : Prop := ∃ y : Y, Homotopic f (ContinuousMap.const _ y) theorem nullhomotopic_of_constant (y : Y) : Nullhomotopic (ContinuousMap.const X y) := ⟨y, by rfl⟩
theorem Nullhomotopic.comp_right {f : C(X, Y)} (hf : f.Nullhomotopic) (g : C(Y, Z)) : (g.comp f).Nullhomotopic := by
Mathlib/Topology/Homotopy/Contractible.lean
28
29
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Yuyang Zhao -/ import Mathlib.Algebra.Order.GroupWithZero.Unbundled.Basic import Mathlib.Algebra.Order.GroupWithZero.Unbundled.Defs import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module (since := "2025-04-13")
Mathlib/Algebra/Order/GroupWithZero/Unbundled.lean
1,022
1,025
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import Mathlib.RingTheory.Valuation.Basic import Mathlib.NumberTheory.Padics.PadicNorm import Mathlib.Analysis.Normed.Field.Lemmas import Mathlib.Tactic.Peel import Mathlib.Topology.MetricSpace.Ultra.Basic /-! # p-adic numbers This file defines the `p`-adic numbers (rationals) `ℚ_[p]` as the completion of `ℚ` with respect to the `p`-adic norm. We show that the `p`-adic norm on `ℚ` extends to `ℚ_[p]`, that `ℚ` is embedded in `ℚ_[p]`, and that `ℚ_[p]` is Cauchy complete. ## Important definitions * `Padic` : the type of `p`-adic numbers * `padicNormE` : the rational valued `p`-adic norm on `ℚ_[p]` * `Padic.addValuation` : the additive `p`-adic valuation on `ℚ_[p]`, with values in `WithTop ℤ` ## Notation We introduce the notation `ℚ_[p]` for the `p`-adic numbers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[Fact p.Prime]` as a type class argument. We use the same concrete Cauchy sequence construction that is used to construct `ℝ`. `ℚ_[p]` inherits a field structure from this construction. The extension of the norm on `ℚ` to `ℚ_[p]` is *not* analogous to extending the absolute value to `ℝ` and hence the proof that `ℚ_[p]` is complete is different from the proof that ℝ is complete. `padicNormE` is the rational-valued `p`-adic norm on `ℚ_[p]`. To instantiate `ℚ_[p]` as a normed field, we must cast this into an `ℝ`-valued norm. The `ℝ`-valued norm, using notation `‖ ‖` from normed spaces, is the canonical representation of this norm. `simp` prefers `padicNorm` to `padicNormE` when possible. Since `padicNormE` and `‖ ‖` have different types, `simp` does not rewrite one to the other. Coercions from `ℚ` to `ℚ_[p]` are set up to work with the `norm_cast` tactic. ## References * [F. Q. Gouvêa, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation, cauchy, completion, p-adic completion -/ noncomputable section open Nat padicNorm CauSeq CauSeq.Completion Metric /-- The type of Cauchy sequences of rationals with respect to the `p`-adic norm. -/ abbrev PadicSeq (p : ℕ) := CauSeq _ (padicNorm p) namespace PadicSeq section variable {p : ℕ} [Fact p.Prime] /-- The `p`-adic norm of the entries of a nonzero Cauchy sequence of rationals is eventually constant. -/ theorem stationary {f : CauSeq ℚ (padicNorm p)} (hf : ¬f ≈ 0) : ∃ N, ∀ m n, N ≤ m → N ≤ n → padicNorm p (f n) = padicNorm p (f m) := have : ∃ ε > 0, ∃ N1, ∀ j ≥ N1, ε ≤ padicNorm p (f j) := CauSeq.abv_pos_of_not_limZero <| not_limZero_of_not_congr_zero hf let ⟨ε, hε, N1, hN1⟩ := this let ⟨N2, hN2⟩ := CauSeq.cauchy₂ f hε ⟨max N1 N2, fun n m hn hm ↦ by have : padicNorm p (f n - f m) < ε := hN2 _ (max_le_iff.1 hn).2 _ (max_le_iff.1 hm).2 have : padicNorm p (f n - f m) < padicNorm p (f n) := lt_of_lt_of_le this <| hN1 _ (max_le_iff.1 hn).1 have : padicNorm p (f n - f m) < max (padicNorm p (f n)) (padicNorm p (f m)) := lt_max_iff.2 (Or.inl this) by_contra hne rw [← padicNorm.neg (f m)] at hne have hnam := add_eq_max_of_ne hne rw [padicNorm.neg, max_comm] at hnam rw [← hnam, sub_eq_add_neg, add_comm] at this apply _root_.lt_irrefl _ this⟩ /-- For all `n ≥ stationaryPoint f hf`, the `p`-adic norm of `f n` is the same. -/ def stationaryPoint {f : PadicSeq p} (hf : ¬f ≈ 0) : ℕ := Classical.choose <| stationary hf theorem stationaryPoint_spec {f : PadicSeq p} (hf : ¬f ≈ 0) : ∀ {m n}, stationaryPoint hf ≤ m → stationaryPoint hf ≤ n → padicNorm p (f n) = padicNorm p (f m) := @(Classical.choose_spec <| stationary hf) open Classical in /-- Since the norm of the entries of a Cauchy sequence is eventually stationary, we can lift the norm to sequences. -/ def norm (f : PadicSeq p) : ℚ := if hf : f ≈ 0 then 0 else padicNorm p (f (stationaryPoint hf)) theorem norm_zero_iff (f : PadicSeq p) : f.norm = 0 ↔ f ≈ 0 := by constructor · intro h by_contra hf unfold norm at h split_ifs at h apply hf intro ε hε exists stationaryPoint hf intro j hj have heq := stationaryPoint_spec hf le_rfl hj simpa [h, heq] · intro h simp [norm, h] end section Embedding open CauSeq variable {p : ℕ} [Fact p.Prime] theorem equiv_zero_of_val_eq_of_equiv_zero {f g : PadicSeq p} (h : ∀ k, padicNorm p (f k) = padicNorm p (g k)) (hf : f ≈ 0) : g ≈ 0 := fun ε hε ↦ let ⟨i, hi⟩ := hf _ hε ⟨i, fun j hj ↦ by simpa [h] using hi _ hj⟩ theorem norm_nonzero_of_not_equiv_zero {f : PadicSeq p} (hf : ¬f ≈ 0) : f.norm ≠ 0 := hf ∘ f.norm_zero_iff.1 theorem norm_eq_norm_app_of_nonzero {f : PadicSeq p} (hf : ¬f ≈ 0) : ∃ k, f.norm = padicNorm p k ∧ k ≠ 0 := have heq : f.norm = padicNorm p (f <| stationaryPoint hf) := by simp [norm, hf] ⟨f <| stationaryPoint hf, heq, fun h ↦ norm_nonzero_of_not_equiv_zero hf (by simpa [h] using heq)⟩ theorem not_limZero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬LimZero (const (padicNorm p) q) := fun h' ↦ hq <| const_limZero.1 h' theorem not_equiv_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬const (padicNorm p) q ≈ 0 := fun h : LimZero (const (padicNorm p) q - 0) ↦ not_limZero_const_of_nonzero (p := p) hq <| by simpa using h theorem norm_nonneg (f : PadicSeq p) : 0 ≤ f.norm := by classical exact if hf : f ≈ 0 then by simp [hf, norm] else by simp [norm, hf, padicNorm.nonneg] /-- An auxiliary lemma for manipulating sequence indices. -/ theorem lift_index_left_left {f : PadicSeq p} (hf : ¬f ≈ 0) (v2 v3 : ℕ) : padicNorm p (f (stationaryPoint hf)) = padicNorm p (f (max (stationaryPoint hf) (max v2 v3))) := by apply stationaryPoint_spec hf · apply le_max_left · exact le_rfl /-- An auxiliary lemma for manipulating sequence indices. -/ theorem lift_index_left {f : PadicSeq p} (hf : ¬f ≈ 0) (v1 v3 : ℕ) : padicNorm p (f (stationaryPoint hf)) = padicNorm p (f (max v1 (max (stationaryPoint hf) v3))) := by apply stationaryPoint_spec hf · apply le_trans · apply le_max_left _ v3 · apply le_max_right · exact le_rfl /-- An auxiliary lemma for manipulating sequence indices. -/ theorem lift_index_right {f : PadicSeq p} (hf : ¬f ≈ 0) (v1 v2 : ℕ) : padicNorm p (f (stationaryPoint hf)) = padicNorm p (f (max v1 (max v2 (stationaryPoint hf)))) := by apply stationaryPoint_spec hf · apply le_trans · apply le_max_right v2 · apply le_max_right · exact le_rfl end Embedding section Valuation open CauSeq variable {p : ℕ} [Fact p.Prime] /-! ### Valuation on `PadicSeq` -/ open Classical in /-- The `p`-adic valuation on `ℚ` lifts to `PadicSeq p`. `Valuation f` is defined to be the valuation of the (`ℚ`-valued) stationary point of `f`. -/ def valuation (f : PadicSeq p) : ℤ := if hf : f ≈ 0 then 0 else padicValRat p (f (stationaryPoint hf)) theorem norm_eq_zpow_neg_valuation {f : PadicSeq p} (hf : ¬f ≈ 0) : f.norm = (p : ℚ) ^ (-f.valuation : ℤ) := by rw [norm, valuation, dif_neg hf, dif_neg hf, padicNorm, if_neg] intro H apply CauSeq.not_limZero_of_not_congr_zero hf intro ε hε use stationaryPoint hf intro n hn rw [stationaryPoint_spec hf le_rfl hn] simpa [H] using hε @[deprecated (since := "2024-12-10")] alias norm_eq_pow_val := norm_eq_zpow_neg_valuation theorem val_eq_iff_norm_eq {f g : PadicSeq p} (hf : ¬f ≈ 0) (hg : ¬g ≈ 0) : f.valuation = g.valuation ↔ f.norm = g.norm := by rw [norm_eq_zpow_neg_valuation hf, norm_eq_zpow_neg_valuation hg, ← neg_inj, zpow_right_inj₀] · exact mod_cast (Fact.out : p.Prime).pos · exact mod_cast (Fact.out : p.Prime).ne_one end Valuation end PadicSeq section open PadicSeq -- Porting note: Commented out `padic_index_simp` tactic /- private unsafe def index_simp_core (hh hf hg : expr) (at_ : Interactive.Loc := Interactive.Loc.ns [none]) : tactic Unit := do let [v1, v2, v3] ← [hh, hf, hg].mapM fun n => tactic.mk_app `` stationary_point [n] <|> return n let e1 ← tactic.mk_app `` lift_index_left_left [hh, v2, v3] <|> return q(True) let e2 ← tactic.mk_app `` lift_index_left [hf, v1, v3] <|> return q(True) let e3 ← tactic.mk_app `` lift_index_right [hg, v1, v2] <|> return q(True) let sl ← [e1, e2, e3].foldlM (fun s e => simp_lemmas.add s e) simp_lemmas.mk when at_ (tactic.simp_target sl >> tactic.skip) let hs ← at_.get_locals hs (tactic.simp_hyp sl []) /-- This is a special-purpose tactic that lifts `padicNorm (f (stationary_point f))` to `padicNorm (f (max _ _ _))`. -/ unsafe def tactic.interactive.padic_index_simp (l : interactive.parse interactive.types.pexpr_list) (at_ : interactive.parse interactive.types.location) : tactic Unit := do let [h, f, g] ← l.mapM tactic.i_to_expr index_simp_core h f g at_ -/ end namespace PadicSeq section Embedding open CauSeq variable {p : ℕ} [hp : Fact p.Prime] theorem norm_mul (f g : PadicSeq p) : (f * g).norm = f.norm * g.norm := by classical exact if hf : f ≈ 0 then by have hg : f * g ≈ 0 := mul_equiv_zero' _ hf simp only [hf, hg, norm, dif_pos, zero_mul] else if hg : g ≈ 0 then by have hf : f * g ≈ 0 := mul_equiv_zero _ hg simp only [hf, hg, norm, dif_pos, mul_zero] else by unfold norm have hfg := mul_not_equiv_zero hf hg simp only [hfg, hf, hg, dite_false] -- Porting note: originally `padic_index_simp [hfg, hf, hg]` rw [lift_index_left_left hfg, lift_index_left hf, lift_index_right hg] apply padicNorm.mul theorem eq_zero_iff_equiv_zero (f : PadicSeq p) : mk f = 0 ↔ f ≈ 0 := mk_eq theorem ne_zero_iff_nequiv_zero (f : PadicSeq p) : mk f ≠ 0 ↔ ¬f ≈ 0 := eq_zero_iff_equiv_zero _ |>.not theorem norm_const (q : ℚ) : norm (const (padicNorm p) q) = padicNorm p q := by obtain rfl | hq := eq_or_ne q 0 · simp [norm] · simp [norm, not_equiv_zero_const_of_nonzero hq] theorem norm_values_discrete (a : PadicSeq p) (ha : ¬a ≈ 0) : ∃ z : ℤ, a.norm = (p : ℚ) ^ (-z) := by let ⟨k, hk, hk'⟩ := norm_eq_norm_app_of_nonzero ha simpa [hk] using padicNorm.values_discrete hk' theorem norm_one : norm (1 : PadicSeq p) = 1 := by have h1 : ¬(1 : PadicSeq p) ≈ 0 := one_not_equiv_zero _ simp [h1, norm, hp.1.one_lt] private theorem norm_eq_of_equiv_aux {f g : PadicSeq p} (hf : ¬f ≈ 0) (hg : ¬g ≈ 0) (hfg : f ≈ g) (h : padicNorm p (f (stationaryPoint hf)) ≠ padicNorm p (g (stationaryPoint hg))) (hlt : padicNorm p (g (stationaryPoint hg)) < padicNorm p (f (stationaryPoint hf))) : False := by have hpn : 0 < padicNorm p (f (stationaryPoint hf)) - padicNorm p (g (stationaryPoint hg)) := sub_pos_of_lt hlt obtain ⟨N, hN⟩ := hfg _ hpn let i := max N (max (stationaryPoint hf) (stationaryPoint hg)) have hi : N ≤ i := le_max_left _ _ have hN' := hN _ hi -- Porting note: originally `padic_index_simp [N, hf, hg] at hN' h hlt` rw [lift_index_left hf N (stationaryPoint hg), lift_index_right hg N (stationaryPoint hf)] at hN' h hlt have hpne : padicNorm p (f i) ≠ padicNorm p (-g i) := by rwa [← padicNorm.neg (g i)] at h rw [CauSeq.sub_apply, sub_eq_add_neg, add_eq_max_of_ne hpne, padicNorm.neg, max_eq_left_of_lt hlt] at hN' have : padicNorm p (f i) < padicNorm p (f i) := by apply lt_of_lt_of_le hN' apply sub_le_self apply padicNorm.nonneg exact lt_irrefl _ this private theorem norm_eq_of_equiv {f g : PadicSeq p} (hf : ¬f ≈ 0) (hg : ¬g ≈ 0) (hfg : f ≈ g) : padicNorm p (f (stationaryPoint hf)) = padicNorm p (g (stationaryPoint hg)) := by by_contra h cases lt_or_le (padicNorm p (g (stationaryPoint hg))) (padicNorm p (f (stationaryPoint hf))) with | inl hlt => exact norm_eq_of_equiv_aux hf hg hfg h hlt | inr hle => apply norm_eq_of_equiv_aux hg hf (Setoid.symm hfg) (Ne.symm h) exact lt_of_le_of_ne hle h theorem norm_equiv {f g : PadicSeq p} (hfg : f ≈ g) : f.norm = g.norm := by classical exact if hf : f ≈ 0 then by have hg : g ≈ 0 := Setoid.trans (Setoid.symm hfg) hf simp [norm, hf, hg] else by have hg : ¬g ≈ 0 := hf ∘ Setoid.trans hfg unfold norm; split_ifs; exact norm_eq_of_equiv hf hg hfg private theorem norm_nonarchimedean_aux {f g : PadicSeq p} (hfg : ¬f + g ≈ 0) (hf : ¬f ≈ 0) (hg : ¬g ≈ 0) : (f + g).norm ≤ max f.norm g.norm := by unfold norm; split_ifs -- Porting note: originally `padic_index_simp [hfg, hf, hg]` rw [lift_index_left_left hfg, lift_index_left hf, lift_index_right hg] apply padicNorm.nonarchimedean theorem norm_nonarchimedean (f g : PadicSeq p) : (f + g).norm ≤ max f.norm g.norm := by classical exact if hfg : f + g ≈ 0 then by have : 0 ≤ max f.norm g.norm := le_max_of_le_left (norm_nonneg _) simpa only [hfg, norm] else if hf : f ≈ 0 then by have hfg' : f + g ≈ g := by change LimZero (f - 0) at hf show LimZero (f + g - g); · simpa only [sub_zero, add_sub_cancel_right] using hf have hcfg : (f + g).norm = g.norm := norm_equiv hfg' have hcl : f.norm = 0 := (norm_zero_iff f).2 hf have : max f.norm g.norm = g.norm := by rw [hcl]; exact max_eq_right (norm_nonneg _) rw [this, hcfg] else if hg : g ≈ 0 then by have hfg' : f + g ≈ f := by change LimZero (g - 0) at hg show LimZero (f + g - f); · simpa only [add_sub_cancel_left, sub_zero] using hg have hcfg : (f + g).norm = f.norm := norm_equiv hfg' have hcl : g.norm = 0 := (norm_zero_iff g).2 hg have : max f.norm g.norm = f.norm := by rw [hcl]; exact max_eq_left (norm_nonneg _) rw [this, hcfg] else norm_nonarchimedean_aux hfg hf hg theorem norm_eq {f g : PadicSeq p} (h : ∀ k, padicNorm p (f k) = padicNorm p (g k)) : f.norm = g.norm := by classical exact if hf : f ≈ 0 then by have hg : g ≈ 0 := equiv_zero_of_val_eq_of_equiv_zero h hf simp only [hf, hg, norm, dif_pos] else by have hg : ¬g ≈ 0 := fun hg ↦ hf <| equiv_zero_of_val_eq_of_equiv_zero (by simp only [h, forall_const, eq_self_iff_true]) hg simp only [hg, hf, norm, dif_neg, not_false_iff] let i := max (stationaryPoint hf) (stationaryPoint hg) have hpf : padicNorm p (f (stationaryPoint hf)) = padicNorm p (f i) := by apply stationaryPoint_spec · apply le_max_left · exact le_rfl have hpg : padicNorm p (g (stationaryPoint hg)) = padicNorm p (g i) := by apply stationaryPoint_spec · apply le_max_right · exact le_rfl rw [hpf, hpg, h] theorem norm_neg (a : PadicSeq p) : (-a).norm = a.norm := norm_eq <| by simp theorem norm_eq_of_add_equiv_zero {f g : PadicSeq p} (h : f + g ≈ 0) : f.norm = g.norm := by have : LimZero (f + g - 0) := h have : f ≈ -g := show LimZero (f - -g) by simpa only [sub_zero, sub_neg_eq_add] have : f.norm = (-g).norm := norm_equiv this simpa only [norm_neg] using this theorem add_eq_max_of_ne {f g : PadicSeq p} (hfgne : f.norm ≠ g.norm) : (f + g).norm = max f.norm g.norm := by classical have hfg : ¬f + g ≈ 0 := mt norm_eq_of_add_equiv_zero hfgne exact if hf : f ≈ 0 then by have : LimZero (f - 0) := hf have : f + g ≈ g := show LimZero (f + g - g) by simpa only [sub_zero, add_sub_cancel_right] have h1 : (f + g).norm = g.norm := norm_equiv this have h2 : f.norm = 0 := (norm_zero_iff _).2 hf rw [h1, h2, max_eq_right (norm_nonneg _)] else if hg : g ≈ 0 then by have : LimZero (g - 0) := hg have : f + g ≈ f := show LimZero (f + g - f) by simpa only [add_sub_cancel_left, sub_zero] have h1 : (f + g).norm = f.norm := norm_equiv this have h2 : g.norm = 0 := (norm_zero_iff _).2 hg rw [h1, h2, max_eq_left (norm_nonneg _)] else by unfold norm at hfgne ⊢; split_ifs at hfgne ⊢ -- Porting note: originally `padic_index_simp [hfg, hf, hg] at hfgne ⊢` rw [lift_index_left hf, lift_index_right hg] at hfgne · rw [lift_index_left_left hfg, lift_index_left hf, lift_index_right hg] exact padicNorm.add_eq_max_of_ne hfgne end Embedding end PadicSeq /-- The `p`-adic numbers `ℚ_[p]` are the Cauchy completion of `ℚ` with respect to the `p`-adic norm. -/ def Padic (p : ℕ) [Fact p.Prime] := CauSeq.Completion.Cauchy (padicNorm p) /-- notation for p-padic rationals -/ notation "ℚ_[" p "]" => Padic p namespace Padic section Completion variable {p : ℕ} [Fact p.Prime] instance field : Field ℚ_[p] := Cauchy.field instance : Inhabited ℚ_[p] := ⟨0⟩ -- short circuits instance : CommRing ℚ_[p] := Cauchy.commRing instance : Ring ℚ_[p] := Cauchy.ring instance : Zero ℚ_[p] := by infer_instance instance : One ℚ_[p] := by infer_instance instance : Add ℚ_[p] := by infer_instance instance : Mul ℚ_[p] := by infer_instance instance : Sub ℚ_[p] := by infer_instance instance : Neg ℚ_[p] := by infer_instance instance : Div ℚ_[p] := by infer_instance instance : AddCommGroup ℚ_[p] := by infer_instance /-- Builds the equivalence class of a Cauchy sequence of rationals. -/ def mk : PadicSeq p → ℚ_[p] := Quotient.mk' variable (p) theorem zero_def : (0 : ℚ_[p]) = ⟦0⟧ := rfl theorem mk_eq {f g : PadicSeq p} : mk f = mk g ↔ f ≈ g := Quotient.eq' theorem const_equiv {q r : ℚ} : const (padicNorm p) q ≈ const (padicNorm p) r ↔ q = r := ⟨fun heq ↦ eq_of_sub_eq_zero <| const_limZero.1 heq, fun heq ↦ by rw [heq]⟩ @[norm_cast] theorem coe_inj {q r : ℚ} : (↑q : ℚ_[p]) = ↑r ↔ q = r := ⟨(const_equiv p).1 ∘ Quotient.eq'.1, fun h ↦ by rw [h]⟩ instance : CharZero ℚ_[p] := ⟨fun m n ↦ by rw [← Rat.cast_natCast] norm_cast exact id⟩ @[norm_cast] theorem coe_add : ∀ {x y : ℚ}, (↑(x + y) : ℚ_[p]) = ↑x + ↑y := Rat.cast_add _ _ @[norm_cast] theorem coe_neg : ∀ {x : ℚ}, (↑(-x) : ℚ_[p]) = -↑x := Rat.cast_neg _ @[norm_cast] theorem coe_mul : ∀ {x y : ℚ}, (↑(x * y) : ℚ_[p]) = ↑x * ↑y := Rat.cast_mul _ _ @[norm_cast] theorem coe_sub : ∀ {x y : ℚ}, (↑(x - y) : ℚ_[p]) = ↑x - ↑y := Rat.cast_sub _ _ @[norm_cast] theorem coe_div : ∀ {x y : ℚ}, (↑(x / y) : ℚ_[p]) = ↑x / ↑y := Rat.cast_div _ _ @[norm_cast] theorem coe_one : (↑(1 : ℚ) : ℚ_[p]) = 1 := rfl @[norm_cast] theorem coe_zero : (↑(0 : ℚ) : ℚ_[p]) = 0 := rfl end Completion end Padic /-- The rational-valued `p`-adic norm on `ℚ_[p]` is lifted from the norm on Cauchy sequences. The canonical form of this function is the normed space instance, with notation `‖ ‖`. -/ def padicNormE {p : ℕ} [hp : Fact p.Prime] : AbsoluteValue ℚ_[p] ℚ where toFun := Quotient.lift PadicSeq.norm <| @PadicSeq.norm_equiv _ _ map_mul' q r := Quotient.inductionOn₂ q r <| PadicSeq.norm_mul nonneg' q := Quotient.inductionOn q <| PadicSeq.norm_nonneg eq_zero' q := Quotient.inductionOn q fun r ↦ by rw [Padic.zero_def, Quotient.eq] exact PadicSeq.norm_zero_iff r add_le' q r := by trans max ((Quotient.lift PadicSeq.norm <| @PadicSeq.norm_equiv _ _) q) ((Quotient.lift PadicSeq.norm <| @PadicSeq.norm_equiv _ _) r) · exact Quotient.inductionOn₂ q r <| PadicSeq.norm_nonarchimedean refine max_le_add_of_nonneg (Quotient.inductionOn q <| PadicSeq.norm_nonneg) ?_ exact Quotient.inductionOn r <| PadicSeq.norm_nonneg namespace padicNormE section Embedding open PadicSeq variable {p : ℕ} [Fact p.Prime] theorem defn (f : PadicSeq p) {ε : ℚ} (hε : 0 < ε) : ∃ N, ∀ i ≥ N, padicNormE (Padic.mk f - f i : ℚ_[p]) < ε := by dsimp [padicNormE] -- `change ∃ N, ∀ i ≥ N, (f - const _ (f i)).norm < ε` also works, but is very slow suffices hyp : ∃ N, ∀ i ≥ N, (f - const _ (f i)).norm < ε by peel hyp with N; use N by_contra! h obtain ⟨N, hN⟩ := cauchy₂ f hε rcases h N with ⟨i, hi, hge⟩ have hne : ¬f - const (padicNorm p) (f i) ≈ 0 := fun h ↦ by rw [PadicSeq.norm, dif_pos h] at hge exact not_lt_of_ge hge hε unfold PadicSeq.norm at hge; split_ifs at hge apply not_le_of_gt _ hge cases _root_.le_total N (stationaryPoint hne) with | inl hgen => exact hN _ hgen _ hi | inr hngen => have := stationaryPoint_spec hne le_rfl hngen rw [← this] exact hN _ le_rfl _ hi /-- Theorems about `padicNormE` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`‖ ‖`). -/ theorem nonarchimedean' (q r : ℚ_[p]) : padicNormE (q + r : ℚ_[p]) ≤ max (padicNormE q) (padicNormE r) := Quotient.inductionOn₂ q r <| norm_nonarchimedean /-- Theorems about `padicNormE` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`‖ ‖`). -/ theorem add_eq_max_of_ne' {q r : ℚ_[p]} : padicNormE q ≠ padicNormE r → padicNormE (q + r : ℚ_[p]) = max (padicNormE q) (padicNormE r) := Quotient.inductionOn₂ q r fun _ _ ↦ PadicSeq.add_eq_max_of_ne @[simp] theorem eq_padic_norm' (q : ℚ) : padicNormE (q : ℚ_[p]) = padicNorm p q := norm_const _ protected theorem image' {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, padicNormE q = (p : ℚ) ^ (-n) := Quotient.inductionOn q fun f hf ↦ have : ¬f ≈ 0 := (ne_zero_iff_nequiv_zero f).1 hf norm_values_discrete f this end Embedding end padicNormE namespace Padic section Complete open PadicSeq Padic variable {p : ℕ} [Fact p.Prime] (f : CauSeq _ (@padicNormE p _)) theorem rat_dense' (q : ℚ_[p]) {ε : ℚ} (hε : 0 < ε) : ∃ r : ℚ, padicNormE (q - r : ℚ_[p]) < ε := Quotient.inductionOn q fun q' ↦ have : ∃ N, ∀ m ≥ N, ∀ n ≥ N, padicNorm p (q' m - q' n) < ε := cauchy₂ _ hε let ⟨N, hN⟩ := this ⟨q' N, by classical dsimp [padicNormE] -- Porting note: this used to be `change`, but that times out. convert_to PadicSeq.norm (q' - const _ (q' N)) < ε rcases Decidable.em (q' - const (padicNorm p) (q' N) ≈ 0) with heq | hne' · simpa only [heq, PadicSeq.norm, dif_pos] · simp only [PadicSeq.norm, dif_neg hne'] change padicNorm p (q' _ - q' _) < ε rcases Decidable.em (stationaryPoint hne' ≤ N) with hle | hle · have := (stationaryPoint_spec hne' le_rfl hle).symm simp only [const_apply, sub_apply, padicNorm.zero, sub_self] at this simpa only [this] · exact hN _ (lt_of_not_ge hle).le _ le_rfl⟩ private theorem div_nat_pos (n : ℕ) : 0 < 1 / (n + 1 : ℚ) := div_pos zero_lt_one (mod_cast succ_pos _) /-- `limSeq f`, for `f` a Cauchy sequence of `p`-adic numbers, is a sequence of rationals with the same limit point as `f`. -/ def limSeq : ℕ → ℚ := fun n ↦ Classical.choose (rat_dense' (f n) (div_nat_pos n)) theorem exi_rat_seq_conv {ε : ℚ} (hε : 0 < ε) : ∃ N, ∀ i ≥ N, padicNormE (f i - (limSeq f i : ℚ_[p]) : ℚ_[p]) < ε := by refine (exists_nat_gt (1 / ε)).imp fun N hN i hi ↦ ?_ have h := Classical.choose_spec (rat_dense' (f i) (div_nat_pos i)) refine lt_of_lt_of_le h ((div_le_iff₀' <| mod_cast succ_pos _).mpr ?_) rw [right_distrib] apply le_add_of_le_of_nonneg · exact (div_le_iff₀ hε).mp (le_trans (le_of_lt hN) (mod_cast hi)) · apply le_of_lt simpa theorem exi_rat_seq_conv_cauchy : IsCauSeq (padicNorm p) (limSeq f) := fun ε hε ↦ by have hε3 : 0 < ε / 3 := div_pos hε (by norm_num) let ⟨N, hN⟩ := exi_rat_seq_conv f hε3 let ⟨N2, hN2⟩ := f.cauchy₂ hε3 exists max N N2 intro j hj suffices padicNormE (limSeq f j - f (max N N2) + (f (max N N2) - limSeq f (max N N2)) : ℚ_[p]) < ε by ring_nf at this ⊢ rw [← padicNormE.eq_padic_norm'] exact mod_cast this apply lt_of_le_of_lt · apply padicNormE.add_le · rw [← add_thirds ε] apply _root_.add_lt_add · suffices padicNormE (limSeq f j - f j + (f j - f (max N N2)) : ℚ_[p]) < ε / 3 + ε / 3 by simpa only [sub_add_sub_cancel] apply lt_of_le_of_lt · apply padicNormE.add_le · apply _root_.add_lt_add · rw [padicNormE.map_sub] apply mod_cast hN j exact le_of_max_le_left hj · exact hN2 _ (le_of_max_le_right hj) _ (le_max_right _ _) · apply mod_cast hN (max N N2) apply le_max_left private def lim' : PadicSeq p := ⟨_, exi_rat_seq_conv_cauchy f⟩ private def lim : ℚ_[p] := ⟦lim' f⟧ theorem complete' : ∃ q : ℚ_[p], ∀ ε > 0, ∃ N, ∀ i ≥ N, padicNormE (q - f i : ℚ_[p]) < ε := ⟨lim f, fun ε hε ↦ by obtain ⟨N, hN⟩ := exi_rat_seq_conv f (half_pos hε) obtain ⟨N2, hN2⟩ := padicNormE.defn (lim' f) (half_pos hε) refine ⟨max N N2, fun i hi ↦ ?_⟩ rw [← sub_add_sub_cancel _ (lim' f i : ℚ_[p]) _] refine (padicNormE.add_le _ _).trans_lt ?_ rw [← add_halves ε] apply _root_.add_lt_add · apply hN2 _ (le_of_max_le_right hi) · rw [padicNormE.map_sub] exact hN _ (le_of_max_le_left hi)⟩ theorem complete'' : ∃ q : ℚ_[p], ∀ ε > 0, ∃ N, ∀ i ≥ N, padicNormE (f i - q : ℚ_[p]) < ε := by obtain ⟨x, hx⟩ := complete' f refine ⟨x, fun ε hε => ?_⟩ obtain ⟨N, hN⟩ := hx ε hε refine ⟨N, fun i hi => ?_⟩ rw [padicNormE.map_sub] exact hN i hi end Complete section NormedSpace variable (p : ℕ) [Fact p.Prime] instance : Dist ℚ_[p] := ⟨fun x y ↦ padicNormE (x - y : ℚ_[p])⟩ instance : IsUltrametricDist ℚ_[p] := ⟨fun x y z ↦ by simpa [dist] using padicNormE.nonarchimedean' (x - y) (y - z)⟩ instance metricSpace : MetricSpace ℚ_[p] where dist_self := by simp [dist] dist := dist dist_comm x y := by simp [dist, ← padicNormE.map_neg (x - y : ℚ_[p])] dist_triangle x y z := by dsimp [dist] exact mod_cast padicNormE.sub_le x y z eq_of_dist_eq_zero := by dsimp [dist]; intro _ _ h apply eq_of_sub_eq_zero apply padicNormE.eq_zero.1 exact mod_cast h instance : Norm ℚ_[p] := ⟨fun x ↦ padicNormE x⟩ instance normedField : NormedField ℚ_[p] := { Padic.field, Padic.metricSpace p with dist_eq := fun _ _ ↦ rfl norm_mul := by simp [Norm.norm, map_mul] norm := norm } instance isAbsoluteValue : IsAbsoluteValue fun a : ℚ_[p] ↦ ‖a‖ where abv_nonneg' := norm_nonneg abv_eq_zero' := norm_eq_zero abv_add' := norm_add_le abv_mul' := by simp [Norm.norm, map_mul] theorem rat_dense (q : ℚ_[p]) {ε : ℝ} (hε : 0 < ε) : ∃ r : ℚ, ‖q - r‖ < ε := let ⟨ε', hε'l, hε'r⟩ := exists_rat_btwn hε let ⟨r, hr⟩ := rat_dense' q (ε := ε') (by simpa using hε'l) ⟨r, lt_trans (by simpa [Norm.norm] using hr) hε'r⟩ end NormedSpace end Padic namespace padicNormE section NormedSpace variable {p : ℕ} [hp : Fact p.Prime] -- Porting note: Linter thinks this is a duplicate simp lemma, so `priority` is assigned @[simp (high)] protected theorem mul (q r : ℚ_[p]) : ‖q * r‖ = ‖q‖ * ‖r‖ := by simp [Norm.norm, map_mul] protected theorem is_norm (q : ℚ_[p]) : ↑(padicNormE q) = ‖q‖ := rfl theorem nonarchimedean (q r : ℚ_[p]) : ‖q + r‖ ≤ max ‖q‖ ‖r‖ := by dsimp [norm] exact mod_cast nonarchimedean' _ _ theorem add_eq_max_of_ne {q r : ℚ_[p]} (h : ‖q‖ ≠ ‖r‖) : ‖q + r‖ = max ‖q‖ ‖r‖ := by dsimp [norm] at h ⊢ have : padicNormE q ≠ padicNormE r := mod_cast h exact mod_cast add_eq_max_of_ne' this @[simp] theorem eq_padicNorm (q : ℚ) : ‖(q : ℚ_[p])‖ = padicNorm p q := by dsimp [norm] rw [← padicNormE.eq_padic_norm'] @[simp] theorem norm_p : ‖(p : ℚ_[p])‖ = (p : ℝ)⁻¹ := by rw [← @Rat.cast_natCast ℝ _ p] rw [← @Rat.cast_natCast ℚ_[p] _ p] simp [hp.1.ne_zero, hp.1.ne_one, norm, padicNorm, padicValRat, padicValInt, zpow_neg, -Rat.cast_natCast] theorem norm_p_lt_one : ‖(p : ℚ_[p])‖ < 1 := by rw [norm_p] exact inv_lt_one_of_one_lt₀ <| mod_cast hp.1.one_lt -- Porting note: Linter thinks this is a duplicate simp lemma, so `priority` is assigned @[simp (high)] theorem norm_p_zpow (n : ℤ) : ‖(p : ℚ_[p]) ^ n‖ = (p : ℝ) ^ (-n) := by rw [norm_zpow, norm_p, zpow_neg, inv_zpow] -- Porting note: Linter thinks this is a duplicate simp lemma, so `priority` is assigned @[simp (high)] theorem norm_p_pow (n : ℕ) : ‖(p : ℚ_[p]) ^ n‖ = (p : ℝ) ^ (-n : ℤ) := by rw [← norm_p_zpow, zpow_natCast] instance : NontriviallyNormedField ℚ_[p] := { Padic.normedField p with non_trivial := ⟨p⁻¹, by rw [norm_inv, norm_p, inv_inv] exact mod_cast hp.1.one_lt⟩ } protected theorem image {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, ‖q‖ = ↑((p : ℚ) ^ (-n)) := Quotient.inductionOn q fun f hf ↦ have : ¬f ≈ 0 := (PadicSeq.ne_zero_iff_nequiv_zero f).1 hf let ⟨n, hn⟩ := PadicSeq.norm_values_discrete f this ⟨n, by rw [← hn]; rfl⟩ protected theorem is_rat (q : ℚ_[p]) : ∃ q' : ℚ, ‖q‖ = q' := by classical exact if h : q = 0 then ⟨0, by simp [h]⟩ else let ⟨n, hn⟩ := padicNormE.image h ⟨_, hn⟩ /-- `ratNorm q`, for a `p`-adic number `q` is the `p`-adic norm of `q`, as rational number. The lemma `padicNormE.eq_ratNorm` asserts `‖q‖ = ratNorm q`. -/ def ratNorm (q : ℚ_[p]) : ℚ := Classical.choose (padicNormE.is_rat q) theorem eq_ratNorm (q : ℚ_[p]) : ‖q‖ = ratNorm q := Classical.choose_spec (padicNormE.is_rat q) theorem norm_rat_le_one : ∀ {q : ℚ} (_ : ¬p ∣ q.den), ‖(q : ℚ_[p])‖ ≤ 1 | ⟨n, d, hn, hd⟩ => fun hq : ¬p ∣ d ↦ if hnz : n = 0 then by have : (⟨n, d, hn, hd⟩ : ℚ) = 0 := Rat.zero_iff_num_zero.mpr hnz norm_num [this] else by have hnz' : (⟨n, d, hn, hd⟩ : ℚ) ≠ 0 := mt Rat.zero_iff_num_zero.1 hnz rw [padicNormE.eq_padicNorm] norm_cast -- Porting note: `Nat.cast_zero` instead of another `norm_cast` call rw [padicNorm.eq_zpow_of_nonzero hnz', padicValRat, neg_sub, padicValNat.eq_zero_of_not_dvd hq, Nat.cast_zero, zero_sub, zpow_neg, zpow_natCast] apply inv_le_one_of_one_le₀ norm_cast apply one_le_pow exact hp.1.pos theorem norm_int_le_one (z : ℤ) : ‖(z : ℚ_[p])‖ ≤ 1 := suffices ‖((z : ℚ) : ℚ_[p])‖ ≤ 1 by simpa norm_rat_le_one <| by simp [hp.1.ne_one] theorem norm_int_lt_one_iff_dvd (k : ℤ) : ‖(k : ℚ_[p])‖ < 1 ↔ ↑p ∣ k := by constructor · intro h contrapose! h apply le_of_eq rw [eq_comm] calc ‖(k : ℚ_[p])‖ = ‖((k : ℚ) : ℚ_[p])‖ := by norm_cast _ = padicNorm p k := padicNormE.eq_padicNorm _ _ = 1 := mod_cast (int_eq_one_iff k).mpr h · rintro ⟨x, rfl⟩
push_cast rw [padicNormE.mul]
Mathlib/NumberTheory/Padics/PadicNumbers.lean
852
853
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import Mathlib.Data.Finset.Attach import Mathlib.Data.Finset.Disjoint import Mathlib.Data.Finset.Erase import Mathlib.Data.Finset.Filter import Mathlib.Data.Finset.Range import Mathlib.Data.Finset.SDiff import Mathlib.Data.Multiset.Basic import Mathlib.Logic.Equiv.Set import Mathlib.Order.Directed import Mathlib.Order.Interval.Set.Defs import Mathlib.Data.Set.SymmDiff /-! # Basic lemmas on finite sets This file contains lemmas on the interaction of various definitions on the `Finset` type. For an explanation of `Finset` design decisions, please see `Mathlib/Data/Finset/Defs.lean`. ## Main declarations ### Main definitions * `Finset.choose`: Given a proof `h` of existence and uniqueness of a certain element satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate. ### Equivalences between finsets * The `Mathlib/Logic/Equiv/Defs.lean` file describes a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`. TODO: examples ## Tags finite sets, finset -/ -- Assert that we define `Finset` without the material on `List.sublists`. -- Note that we cannot use `List.sublists` itself as that is defined very early. assert_not_exists List.sublistsLen Multiset.powerset CompleteLattice Monoid open Multiset Subtype Function universe u variable {α : Type*} {β : Type*} {γ : Type*} namespace Finset -- TODO: these should be global attributes, but this will require fixing other files attribute [local trans] Subset.trans Superset.trans set_option linter.deprecated false in @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {s : Finset α} (hx : x ∈ s) : SizeOf.sizeOf x < SizeOf.sizeOf s := by cases s dsimp [SizeOf.sizeOf, SizeOf.sizeOf, Multiset.sizeOf] rw [Nat.add_comm] refine lt_trans ?_ (Nat.lt_succ_self _) exact Multiset.sizeOf_lt_sizeOf_of_mem hx /-! ### Lattice structure -/ section Lattice variable [DecidableEq α] {s s₁ s₂ t t₁ t₂ u v : Finset α} {a b : α} /-! #### union -/ @[simp] theorem disjUnion_eq_union (s t h) : @disjUnion α s t h = s ∪ t := ext fun a => by simp @[simp] theorem disjoint_union_left : Disjoint (s ∪ t) u ↔ Disjoint s u ∧ Disjoint t u := by simp only [disjoint_left, mem_union, or_imp, forall_and] @[simp] theorem disjoint_union_right : Disjoint s (t ∪ u) ↔ Disjoint s t ∧ Disjoint s u := by simp only [disjoint_right, mem_union, or_imp, forall_and] /-! #### inter -/ theorem not_disjoint_iff_nonempty_inter : ¬Disjoint s t ↔ (s ∩ t).Nonempty := not_disjoint_iff.trans <| by simp [Finset.Nonempty] alias ⟨_, Nonempty.not_disjoint⟩ := not_disjoint_iff_nonempty_inter theorem disjoint_or_nonempty_inter (s t : Finset α) : Disjoint s t ∨ (s ∩ t).Nonempty := by rw [← not_disjoint_iff_nonempty_inter] exact em _ omit [DecidableEq α] in theorem disjoint_of_subset_iff_left_eq_empty (h : s ⊆ t) : Disjoint s t ↔ s = ∅ := disjoint_of_le_iff_left_eq_bot h lemma pairwiseDisjoint_iff {ι : Type*} {s : Set ι} {f : ι → Finset α} : s.PairwiseDisjoint f ↔ ∀ ⦃i⦄, i ∈ s → ∀ ⦃j⦄, j ∈ s → (f i ∩ f j).Nonempty → i = j := by simp [Set.PairwiseDisjoint, Set.Pairwise, Function.onFun, not_imp_comm (a := _ = _), not_disjoint_iff_nonempty_inter] end Lattice instance isDirected_le : IsDirected (Finset α) (· ≤ ·) := by classical infer_instance instance isDirected_subset : IsDirected (Finset α) (· ⊆ ·) := isDirected_le /-! ### erase -/ section Erase variable [DecidableEq α] {s t u v : Finset α} {a b : α} @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl protected lemma Nontrivial.erase_nonempty (hs : s.Nontrivial) : (s.erase a).Nonempty := (hs.exists_ne a).imp <| by aesop @[simp] lemma erase_nonempty (ha : a ∈ s) : (s.erase a).Nonempty ↔ s.Nontrivial := by simp only [Finset.Nonempty, mem_erase, and_comm (b := _ ∈ _)] refine ⟨?_, fun hs ↦ hs.exists_ne a⟩ rintro ⟨b, hb, hba⟩ exact ⟨_, hb, _, ha, hba⟩ @[simp] theorem erase_singleton (a : α) : ({a} : Finset α).erase a = ∅ := by ext x simp @[simp] theorem erase_insert_eq_erase (s : Finset α) (a : α) : (insert a s).erase a = s.erase a := ext fun x => by simp +contextual only [mem_erase, mem_insert, and_congr_right_iff, false_or, iff_self, imp_true_iff] theorem erase_insert {a : α} {s : Finset α} (h : a ∉ s) : erase (insert a s) a = s := by rw [erase_insert_eq_erase, erase_eq_of_not_mem h] theorem erase_insert_of_ne {a b : α} {s : Finset α} (h : a ≠ b) : erase (insert a s) b = insert a (erase s b) := ext fun x => by have : x ≠ b ∧ x = a ↔ x = a := and_iff_right_of_imp fun hx => hx.symm ▸ h simp only [mem_erase, mem_insert, and_or_left, this] theorem erase_cons_of_ne {a b : α} {s : Finset α} (ha : a ∉ s) (hb : a ≠ b) : erase (cons a s ha) b = cons a (erase s b) fun h => ha <| erase_subset _ _ h := by simp only [cons_eq_insert, erase_insert_of_ne hb] @[simp] theorem insert_erase (h : a ∈ s) : insert a (erase s a) = s := ext fun x => by simp only [mem_insert, mem_erase, or_and_left, dec_em, true_and] apply or_iff_right_of_imp rintro rfl exact h lemma erase_eq_iff_eq_insert (hs : a ∈ s) (ht : a ∉ t) : erase s a = t ↔ s = insert a t := by aesop lemma insert_erase_invOn : Set.InvOn (insert a) (fun s ↦ erase s a) {s : Finset α | a ∈ s} {s : Finset α | a ∉ s} := ⟨fun _s ↦ insert_erase, fun _s ↦ erase_insert⟩ theorem erase_ssubset {a : α} {s : Finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) := ssubset_insert <| not_mem_erase _ _ _ = _ := insert_erase h theorem ssubset_iff_exists_subset_erase {s t : Finset α} : s ⊂ t ↔ ∃ a ∈ t, s ⊆ t.erase a := by refine ⟨fun h => ?_, fun ⟨a, ha, h⟩ => ssubset_of_subset_of_ssubset h <| erase_ssubset ha⟩ obtain ⟨a, ht, hs⟩ := not_subset.1 h.2 exact ⟨a, ht, subset_erase.2 ⟨h.1, hs⟩⟩ theorem erase_ssubset_insert (s : Finset α) (a : α) : s.erase a ⊂ insert a s := ssubset_iff_exists_subset_erase.2 ⟨a, mem_insert_self _ _, erase_subset_erase _ <| subset_insert _ _⟩ theorem erase_cons {s : Finset α} {a : α} (h : a ∉ s) : (s.cons a h).erase a = s := by rw [cons_eq_insert, erase_insert_eq_erase, erase_eq_of_not_mem h] theorem subset_insert_iff {a : α} {s t : Finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp] exact forall_congr' fun x => forall_swap theorem erase_insert_subset (a : α) (s : Finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 <| Subset.rfl theorem insert_erase_subset (a : α) (s : Finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 <| Subset.rfl theorem subset_insert_iff_of_not_mem (h : a ∉ s) : s ⊆ insert a t ↔ s ⊆ t := by rw [subset_insert_iff, erase_eq_of_not_mem h] theorem erase_subset_iff_of_mem (h : a ∈ t) : s.erase a ⊆ t ↔ s ⊆ t := by rw [← subset_insert_iff, insert_eq_of_mem h] theorem erase_injOn' (a : α) : { s : Finset α | a ∈ s }.InjOn fun s => erase s a := fun s hs t ht (h : s.erase a = _) => by rw [← insert_erase hs, ← insert_erase ht, h] end Erase lemma Nontrivial.exists_cons_eq {s : Finset α} (hs : s.Nontrivial) : ∃ t a ha b hb hab, (cons b t hb).cons a (mem_cons.not.2 <| not_or_intro hab ha) = s := by classical obtain ⟨a, ha, b, hb, hab⟩ := hs have : b ∈ s.erase a := mem_erase.2 ⟨hab.symm, hb⟩ refine ⟨(s.erase a).erase b, a, ?_, b, ?_, ?_, ?_⟩ <;> simp [insert_erase this, insert_erase ha, *] /-! ### sdiff -/ section Sdiff variable [DecidableEq α] {s t u v : Finset α} {a b : α} lemma erase_sdiff_erase (hab : a ≠ b) (hb : b ∈ s) : s.erase a \ s.erase b = {b} := by ext; aesop -- TODO: Do we want to delete this lemma and `Finset.disjUnion_singleton`, -- or instead add `Finset.union_singleton`/`Finset.singleton_union`? theorem sdiff_singleton_eq_erase (a : α) (s : Finset α) : s \ {a} = erase s a := by ext rw [mem_erase, mem_sdiff, mem_singleton, and_comm] -- This lemma matches `Finset.insert_eq` in functionality. theorem erase_eq (s : Finset α) (a : α) : s.erase a = s \ {a} := (sdiff_singleton_eq_erase _ _).symm theorem disjoint_erase_comm : Disjoint (s.erase a) t ↔ Disjoint s (t.erase a) := by simp_rw [erase_eq, disjoint_sdiff_comm] lemma disjoint_insert_erase (ha : a ∉ t) : Disjoint (s.erase a) (insert a t) ↔ Disjoint s t := by rw [disjoint_erase_comm, erase_insert ha] lemma disjoint_erase_insert (ha : a ∉ s) : Disjoint (insert a s) (t.erase a) ↔ Disjoint s t := by rw [← disjoint_erase_comm, erase_insert ha] theorem disjoint_of_erase_left (ha : a ∉ t) (hst : Disjoint (s.erase a) t) : Disjoint s t := by rw [← erase_insert ha, ← disjoint_erase_comm, disjoint_insert_right] exact ⟨not_mem_erase _ _, hst⟩ theorem disjoint_of_erase_right (ha : a ∉ s) (hst : Disjoint s (t.erase a)) : Disjoint s t := by rw [← erase_insert ha, disjoint_erase_comm, disjoint_insert_left] exact ⟨not_mem_erase _ _, hst⟩ theorem inter_erase (a : α) (s t : Finset α) : s ∩ t.erase a = (s ∩ t).erase a := by simp only [erase_eq, inter_sdiff_assoc] @[simp] theorem erase_inter (a : α) (s t : Finset α) : s.erase a ∩ t = (s ∩ t).erase a := by simpa only [inter_comm t] using inter_erase a t s theorem erase_sdiff_comm (s t : Finset α) (a : α) : s.erase a \ t = (s \ t).erase a := by simp_rw [erase_eq, sdiff_right_comm] theorem erase_inter_comm (s t : Finset α) (a : α) : s.erase a ∩ t = s ∩ t.erase a := by rw [erase_inter, inter_erase] theorem erase_union_distrib (s t : Finset α) (a : α) : (s ∪ t).erase a = s.erase a ∪ t.erase a := by simp_rw [erase_eq, union_sdiff_distrib] theorem insert_inter_distrib (s t : Finset α) (a : α) : insert a (s ∩ t) = insert a s ∩ insert a t := by simp_rw [insert_eq, union_inter_distrib_left] theorem erase_sdiff_distrib (s t : Finset α) (a : α) : (s \ t).erase a = s.erase a \ t.erase a := by simp_rw [erase_eq, sdiff_sdiff, sup_sdiff_eq_sup le_rfl, sup_comm] theorem erase_union_of_mem (ha : a ∈ t) (s : Finset α) : s.erase a ∪ t = s ∪ t := by rw [← insert_erase (mem_union_right s ha), erase_union_distrib, ← union_insert, insert_erase ha] theorem union_erase_of_mem (ha : a ∈ s) (t : Finset α) : s ∪ t.erase a = s ∪ t := by rw [← insert_erase (mem_union_left t ha), erase_union_distrib, ← insert_union, insert_erase ha] theorem sdiff_union_erase_cancel (hts : t ⊆ s) (ha : a ∈ t) : s \ t ∪ t.erase a = s.erase a := by simp_rw [erase_eq, sdiff_union_sdiff_cancel hts (singleton_subset_iff.2 ha)] theorem sdiff_insert (s t : Finset α) (x : α) : s \ insert x t = (s \ t).erase x := by simp_rw [← sdiff_singleton_eq_erase, insert_eq, sdiff_sdiff_left', sdiff_union_distrib, inter_comm] theorem sdiff_insert_insert_of_mem_of_not_mem {s t : Finset α} {x : α} (hxs : x ∈ s) (hxt : x ∉ t) : insert x (s \ insert x t) = s \ t := by rw [sdiff_insert, insert_erase (mem_sdiff.mpr ⟨hxs, hxt⟩)] theorem sdiff_erase (h : a ∈ s) : s \ t.erase a = insert a (s \ t) := by rw [← sdiff_singleton_eq_erase, sdiff_sdiff_eq_sdiff_union (singleton_subset_iff.2 h), insert_eq, union_comm] theorem sdiff_erase_self (ha : a ∈ s) : s \ s.erase a = {a} := by rw [sdiff_erase ha, Finset.sdiff_self, insert_empty_eq] theorem erase_eq_empty_iff (s : Finset α) (a : α) : s.erase a = ∅ ↔ s = ∅ ∨ s = {a} := by rw [← sdiff_singleton_eq_erase, sdiff_eq_empty_iff_subset, subset_singleton_iff] --TODO@Yaël: Kill lemmas duplicate with `BooleanAlgebra` theorem sdiff_disjoint : Disjoint (t \ s) s := disjoint_left.2 fun _a ha => (mem_sdiff.1 ha).2 theorem disjoint_sdiff : Disjoint s (t \ s) := sdiff_disjoint.symm theorem disjoint_sdiff_inter (s t : Finset α) : Disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right inter_subset_right sdiff_disjoint end Sdiff /-! ### attach -/ @[simp] theorem attach_empty : attach (∅ : Finset α) = ∅ := rfl @[simp] theorem attach_nonempty_iff {s : Finset α} : s.attach.Nonempty ↔ s.Nonempty := by simp [Finset.Nonempty] @[aesop safe apply (rule_sets := [finsetNonempty])] protected alias ⟨_, Nonempty.attach⟩ := attach_nonempty_iff @[simp] theorem attach_eq_empty_iff {s : Finset α} : s.attach = ∅ ↔ s = ∅ := by simp [eq_empty_iff_forall_not_mem] /-! ### filter -/ section Filter variable (p q : α → Prop) [DecidablePred p] [DecidablePred q] {s t : Finset α} theorem filter_singleton (a : α) : filter p {a} = if p a then {a} else ∅ := by classical ext x simp only [mem_singleton, forall_eq, mem_filter] split_ifs with h <;> by_cases h' : x = a <;> simp [h, h'] theorem filter_cons_of_pos (a : α) (s : Finset α) (ha : a ∉ s) (hp : p a) : filter p (cons a s ha) = cons a (filter p s) ((mem_of_mem_filter _).mt ha) := eq_of_veq <| Multiset.filter_cons_of_pos s.val hp theorem filter_cons_of_neg (a : α) (s : Finset α) (ha : a ∉ s) (hp : ¬p a) : filter p (cons a s ha) = filter p s := eq_of_veq <| Multiset.filter_cons_of_neg s.val hp theorem disjoint_filter {s : Finset α} {p q : α → Prop} [DecidablePred p] [DecidablePred q] : Disjoint (s.filter p) (s.filter q) ↔ ∀ x ∈ s, p x → ¬q x := by constructor <;> simp +contextual [disjoint_left] theorem disjoint_filter_filter' (s t : Finset α) {p q : α → Prop} [DecidablePred p] [DecidablePred q] (h : Disjoint p q) : Disjoint (s.filter p) (t.filter q) := by simp_rw [disjoint_left, mem_filter] rintro a ⟨_, hp⟩ ⟨_, hq⟩ rw [Pi.disjoint_iff] at h simpa [hp, hq] using h a theorem disjoint_filter_filter_neg (s t : Finset α) (p : α → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] : Disjoint (s.filter p) (t.filter fun a => ¬p a) := disjoint_filter_filter' s t disjoint_compl_right theorem filter_disj_union (s : Finset α) (t : Finset α) (h : Disjoint s t) : filter p (disjUnion s t h) = (filter p s).disjUnion (filter p t) (disjoint_filter_filter h) := eq_of_veq <| Multiset.filter_add _ _ _ theorem filter_cons {a : α} (s : Finset α) (ha : a ∉ s) : filter p (cons a s ha) = if p a then cons a (filter p s) ((mem_of_mem_filter _).mt ha) else filter p s := by split_ifs with h · rw [filter_cons_of_pos _ _ _ ha h] · rw [filter_cons_of_neg _ _ _ ha h] section variable [DecidableEq α] theorem filter_union (s₁ s₂ : Finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext fun _ => by simp only [mem_filter, mem_union, or_and_right] theorem filter_union_right (s : Finset α) : s.filter p ∪ s.filter q = s.filter fun x => p x ∨ q x := ext fun x => by simp [mem_filter, mem_union, ← and_or_left] theorem filter_mem_eq_inter {s t : Finset α} [∀ i, Decidable (i ∈ t)] : (s.filter fun i => i ∈ t) = s ∩ t := ext fun i => by simp [mem_filter, mem_inter] theorem filter_inter_distrib (s t : Finset α) : (s ∩ t).filter p = s.filter p ∩ t.filter p := by ext simp [mem_filter, mem_inter, and_assoc] theorem filter_inter (s t : Finset α) : filter p s ∩ t = filter p (s ∩ t) := by ext simp only [mem_inter, mem_filter, and_right_comm] theorem inter_filter (s t : Finset α) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : Finset α) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by ext x split_ifs with h <;> by_cases h' : x = a <;> simp [h, h'] theorem filter_erase (a : α) (s : Finset α) : filter p (erase s a) = erase (filter p s) a := by ext x simp only [and_assoc, mem_filter, iff_self, mem_erase] theorem filter_or (s : Finset α) : (s.filter fun a => p a ∨ q a) = s.filter p ∪ s.filter q := ext fun _ => by simp [mem_filter, mem_union, and_or_left] theorem filter_and (s : Finset α) : (s.filter fun a => p a ∧ q a) = s.filter p ∩ s.filter q := ext fun _ => by simp [mem_filter, mem_inter, and_comm, and_left_comm, and_self_iff, and_assoc] theorem filter_not (s : Finset α) : (s.filter fun a => ¬p a) = s \ s.filter p := ext fun a => by simp only [Bool.decide_coe, Bool.not_eq_true', mem_filter, and_comm, mem_sdiff, not_and_or, Bool.not_eq_true, and_or_left, and_not_self, or_false] lemma filter_and_not (s : Finset α) (p q : α → Prop) [DecidablePred p] [DecidablePred q] : s.filter (fun a ↦ p a ∧ ¬ q a) = s.filter p \ s.filter q := by rw [filter_and, filter_not, ← inter_sdiff_assoc, inter_eq_left.2 (filter_subset _ _)] theorem sdiff_eq_filter (s₁ s₂ : Finset α) : s₁ \ s₂ = filter (· ∉ s₂) s₁ := ext fun _ => by simp [mem_sdiff, mem_filter] theorem subset_union_elim {s : Finset α} {t₁ t₂ : Set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃ s₁ s₂ : Finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := by classical refine ⟨s.filter (· ∈ t₁), s.filter (· ∉ t₁), ?_, ?_, ?_⟩ · simp [filter_union_right, em] · intro x simp · intro x simp only [not_not, coe_filter, Set.mem_setOf_eq, Set.mem_diff, and_imp] intro hx hx₂ exact ⟨Or.resolve_left (h hx) hx₂, hx₂⟩ -- This is not a good simp lemma, as it would prevent `Finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter (Eq b)`. /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ theorem filter_eq [DecidableEq β] (s : Finset β) (b : β) : s.filter (Eq b) = ite (b ∈ s) {b} ∅ := by split_ifs with h · ext simp only [mem_filter, mem_singleton, decide_eq_true_eq] refine ⟨fun h => h.2.symm, ?_⟩ rintro rfl exact ⟨h, rfl⟩ · ext simp only [mem_filter, not_and, iff_false, not_mem_empty, decide_eq_true_eq] rintro m rfl exact h m /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ theorem filter_eq' [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => a = b) = ite (b ∈ s) {b} ∅ := _root_.trans (filter_congr fun _ _ => by simp_rw [@eq_comm _ b]) (filter_eq s b) theorem filter_ne [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => b ≠ a) = s.erase b := by ext simp only [mem_filter, mem_erase, Ne, decide_not, Bool.not_eq_true', decide_eq_false_iff_not] tauto theorem filter_ne' [DecidableEq β] (s : Finset β) (b : β) : (s.filter fun a => a ≠ b) = s.erase b := _root_.trans (filter_congr fun _ _ => by simp_rw [@ne_comm _ b]) (filter_ne s b) theorem filter_union_filter_of_codisjoint (s : Finset α) (h : Codisjoint p q) : s.filter p ∪ s.filter q = s := (filter_or _ _ _).symm.trans <| filter_true_of_mem fun x _ => h.top_le x trivial theorem filter_union_filter_neg_eq [∀ x, Decidable (¬p x)] (s : Finset α) : (s.filter p ∪ s.filter fun a => ¬p a) = s := filter_union_filter_of_codisjoint _ _ _ <| @codisjoint_hnot_right _ _ p end end Filter /-! ### range -/ section Range open Nat variable {n m l : ℕ} @[simp] theorem range_filter_eq {n m : ℕ} : (range n).filter (· = m) = if m < n then {m} else ∅ := by convert filter_eq (range n) m using 2 · ext rw [eq_comm] · simp end Range end Finset /-! ### dedup on list and multiset -/ namespace Multiset variable [DecidableEq α] {s t : Multiset α} @[simp] theorem toFinset_add (s t : Multiset α) : toFinset (s + t) = toFinset s ∪ toFinset t := Finset.ext <| by simp @[simp] theorem toFinset_inter (s t : Multiset α) : toFinset (s ∩ t) = toFinset s ∩ toFinset t := Finset.ext <| by simp @[simp] theorem toFinset_union (s t : Multiset α) : (s ∪ t).toFinset = s.toFinset ∪ t.toFinset := by ext; simp @[simp] theorem toFinset_eq_empty {m : Multiset α} : m.toFinset = ∅ ↔ m = 0 := Finset.val_inj.symm.trans Multiset.dedup_eq_zero @[simp] theorem toFinset_nonempty : s.toFinset.Nonempty ↔ s ≠ 0 := by simp only [toFinset_eq_empty, Ne, Finset.nonempty_iff_ne_empty] @[aesop safe apply (rule_sets := [finsetNonempty])] protected alias ⟨_, Aesop.toFinset_nonempty_of_ne⟩ := toFinset_nonempty @[simp] theorem toFinset_filter (s : Multiset α) (p : α → Prop) [DecidablePred p] : Multiset.toFinset (s.filter p) = s.toFinset.filter p := by ext; simp end Multiset namespace List variable [DecidableEq α] {l l' : List α} {a : α} {f : α → β} {s : Finset α} {t : Set β} {t' : Finset β} @[simp] theorem toFinset_union (l l' : List α) : (l ∪ l').toFinset = l.toFinset ∪ l'.toFinset := by ext simp @[simp] theorem toFinset_inter (l l' : List α) : (l ∩ l').toFinset = l.toFinset ∩ l'.toFinset := by ext simp @[aesop safe apply (rule_sets := [finsetNonempty])] alias ⟨_, Aesop.toFinset_nonempty_of_ne⟩ := toFinset_nonempty_iff @[simp] theorem toFinset_filter (s : List α) (p : α → Bool) : (s.filter p).toFinset = s.toFinset.filter (p ·) := by ext; simp [List.mem_filter] end List namespace Finset section ToList @[simp] theorem toList_eq_nil {s : Finset α} : s.toList = [] ↔ s = ∅ := Multiset.toList_eq_nil.trans val_eq_zero theorem empty_toList {s : Finset α} : s.toList.isEmpty ↔ s = ∅ := by simp @[simp] theorem toList_empty : (∅ : Finset α).toList = [] := toList_eq_nil.mpr rfl theorem Nonempty.toList_ne_nil {s : Finset α} (hs : s.Nonempty) : s.toList ≠ [] := mt toList_eq_nil.mp hs.ne_empty theorem Nonempty.not_empty_toList {s : Finset α} (hs : s.Nonempty) : ¬s.toList.isEmpty := mt empty_toList.mp hs.ne_empty end ToList /-! ### choose -/ section Choose variable (p : α → Prop) [DecidablePred p] (l : Finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def chooseX (hp : ∃! a, a ∈ l ∧ p a) : { a // a ∈ l ∧ p a } := Multiset.chooseX p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := chooseX p l hp theorem choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose end Finset namespace Equiv variable [DecidableEq α] {s t : Finset α} open Finset /-- The disjoint union of finsets is a sum -/ def Finset.union (s t : Finset α) (h : Disjoint s t) : s ⊕ t ≃ (s ∪ t : Finset α) := Equiv.setCongr (coe_union _ _) |>.trans (Equiv.Set.union (disjoint_coe.mpr h)) |>.symm @[simp] theorem Finset.union_symm_inl (h : Disjoint s t) (x : s) : Equiv.Finset.union s t h (Sum.inl x) = ⟨x, Finset.mem_union.mpr <| Or.inl x.2⟩ := rfl @[simp] theorem Finset.union_symm_inr (h : Disjoint s t) (y : t) : Equiv.Finset.union s t h (Sum.inr y) = ⟨y, Finset.mem_union.mpr <| Or.inr y.2⟩ := rfl /-- The type of dependent functions on the disjoint union of finsets `s ∪ t` is equivalent to the type of pairs of functions on `s` and on `t`. This is similar to `Equiv.sumPiEquivProdPi`. -/ def piFinsetUnion {ι} [DecidableEq ι] (α : ι → Type*) {s t : Finset ι} (h : Disjoint s t) : ((∀ i : s, α i) × ∀ i : t, α i) ≃ ∀ i : (s ∪ t : Finset ι), α i := let e := Equiv.Finset.union s t h sumPiEquivProdPi (fun b ↦ α (e b)) |>.symm.trans (.piCongrLeft (fun i : ↥(s ∪ t) ↦ α i) e) /-- A finset is equivalent to its coercion as a set. -/ def _root_.Finset.equivToSet (s : Finset α) : s ≃ s.toSet where toFun a := ⟨a.1, mem_coe.2 a.2⟩ invFun a := ⟨a.1, mem_coe.1 a.2⟩ left_inv := fun _ ↦ rfl right_inv := fun _ ↦ rfl end Equiv namespace Multiset variable [DecidableEq α] @[simp] lemma toFinset_replicate (n : ℕ) (a : α) : (replicate n a).toFinset = if n = 0 then ∅ else {a} := by ext x simp only [mem_toFinset, Finset.mem_singleton, mem_replicate] split_ifs with hn <;> simp [hn] end Multiset
Mathlib/Data/Finset/Basic.lean
917
918
/- 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.Finset.Basic import Mathlib.ModelTheory.Syntax import Mathlib.Data.List.ProdSigma /-! # Basics on First-Order Semantics This file defines the interpretations of first-order terms, formulas, sentences, and theories in a style inspired by the [Flypitch project](https://flypitch.github.io/). ## Main Definitions - `FirstOrder.Language.Term.realize` is defined so that `t.realize v` is the term `t` evaluated at variables `v`. - `FirstOrder.Language.BoundedFormula.Realize` is defined so that `φ.Realize v xs` is the bounded formula `φ` evaluated at tuples of variables `v` and `xs`. - `FirstOrder.Language.Formula.Realize` is defined so that `φ.Realize v` is the formula `φ` evaluated at variables `v`. - `FirstOrder.Language.Sentence.Realize` is defined so that `φ.Realize M` is the sentence `φ` evaluated in the structure `M`. Also denoted `M ⊨ φ`. - `FirstOrder.Language.Theory.Model` is defined so that `T.Model M` is true if and only if every sentence of `T` is realized in `M`. Also denoted `T ⊨ φ`. ## Main Results - Several results in this file show that syntactic constructions such as `relabel`, `castLE`, `liftAt`, `subst`, and the actions of language maps commute with realization of terms, formulas, sentences, and theories. ## Implementation Notes - Formulas use a modified version of de Bruijn variables. Specifically, a `L.BoundedFormula α n` is a formula with some variables indexed by a type `α`, which cannot be quantified over, and some indexed by `Fin n`, which can. For any `φ : L.BoundedFormula α (n + 1)`, we define the formula `∀' φ : L.BoundedFormula α n` by universally quantifying over the variable indexed by `n : Fin (n + 1)`. ## 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 w u' v' namespace FirstOrder namespace Language variable {L : Language.{u, v}} {L' : Language} variable {M : Type w} {N P : Type*} [L.Structure M] [L.Structure N] [L.Structure P] variable {α : Type u'} {β : Type v'} {γ : Type*} open FirstOrder Cardinal open Structure Cardinal Fin namespace Term /-- A term `t` with variables indexed by `α` can be evaluated by giving a value to each variable. -/ def realize (v : α → M) : ∀ _t : L.Term α, M | var k => v k | func f ts => funMap f fun i => (ts i).realize v @[simp] theorem realize_var (v : α → M) (k) : realize v (var k : L.Term α) = v k := rfl @[simp] theorem realize_func (v : α → M) {n} (f : L.Functions n) (ts) : realize v (func f ts : L.Term α) = funMap f fun i => (ts i).realize v := rfl @[simp] theorem realize_relabel {t : L.Term α} {g : α → β} {v : β → M} : (t.relabel g).realize v = t.realize (v ∘ g) := by induction t with | var => rfl | func f ts ih => simp [ih] @[simp] theorem realize_liftAt {n n' m : ℕ} {t : L.Term (α ⊕ (Fin n))} {v : α ⊕ (Fin (n + n')) → M} : (t.liftAt n' m).realize v = t.realize (v ∘ Sum.map id fun i : Fin _ => if ↑i < m then Fin.castAdd n' i else Fin.addNat i n') := realize_relabel @[simp] theorem realize_constants {c : L.Constants} {v : α → M} : c.term.realize v = c := funMap_eq_coe_constants @[simp] theorem realize_functions_apply₁ {f : L.Functions 1} {t : L.Term α} {v : α → M} : (f.apply₁ t).realize v = funMap f ![t.realize v] := by rw [Functions.apply₁, Term.realize] refine congr rfl (funext fun i => ?_) simp only [Matrix.cons_val_fin_one] @[simp] theorem realize_functions_apply₂ {f : L.Functions 2} {t₁ t₂ : L.Term α} {v : α → M} : (f.apply₂ t₁ t₂).realize v = funMap f ![t₁.realize v, t₂.realize v] := by rw [Functions.apply₂, Term.realize] refine congr rfl (funext (Fin.cases ?_ ?_)) · simp only [Matrix.cons_val_zero] · simp only [Matrix.cons_val_succ, Matrix.cons_val_fin_one, forall_const] theorem realize_con {A : Set M} {a : A} {v : α → M} : (L.con a).term.realize v = a := rfl @[simp] theorem realize_subst {t : L.Term α} {tf : α → L.Term β} {v : β → M} : (t.subst tf).realize v = t.realize fun a => (tf a).realize v := by induction t with | var => rfl | func _ _ ih => simp [ih] theorem realize_restrictVar [DecidableEq α] {t : L.Term α} {f : t.varFinset → β} {v : β → M} (v' : α → M) (hv' : ∀ a, v (f a) = v' a) : (t.restrictVar f).realize v = t.realize v' := by induction t with | var => simp [restrictVar, hv'] | func _ _ ih => exact congr rfl (funext fun i => ih i ((by simp [Function.comp_apply, hv']))) /-- A special case of `realize_restrictVar`, included because we can add the `simp` attribute to it -/ @[simp] theorem realize_restrictVar' [DecidableEq α] {t : L.Term α} {s : Set α} (h : ↑t.varFinset ⊆ s) {v : α → M} : (t.restrictVar (Set.inclusion h)).realize (v ∘ (↑)) = t.realize v := realize_restrictVar _ (by simp) theorem realize_restrictVarLeft [DecidableEq α] {γ : Type*} {t : L.Term (α ⊕ γ)} {f : t.varFinsetLeft → β} {xs : β ⊕ γ → M} (xs' : α → M) (hxs' : ∀ a, xs (Sum.inl (f a)) = xs' a) : (t.restrictVarLeft f).realize xs = t.realize (Sum.elim xs' (xs ∘ Sum.inr)) := by induction t with | var a => cases a <;> simp [restrictVarLeft, hxs'] | func _ _ ih => exact congr rfl (funext fun i => ih i (by simp [hxs'])) /-- A special case of `realize_restrictVarLeft`, included because we can add the `simp` attribute to it -/ @[simp] theorem realize_restrictVarLeft' [DecidableEq α] {γ : Type*} {t : L.Term (α ⊕ γ)} {s : Set α} (h : ↑t.varFinsetLeft ⊆ s) {v : α → M} {xs : γ → M} : (t.restrictVarLeft (Set.inclusion h)).realize (Sum.elim (v ∘ (↑)) xs) = t.realize (Sum.elim v xs) := realize_restrictVarLeft _ (by simp) @[simp] theorem realize_constantsToVars [L[[α]].Structure M] [(lhomWithConstants L α).IsExpansionOn M] {t : L[[α]].Term β} {v : β → M} : t.constantsToVars.realize (Sum.elim (fun a => ↑(L.con a)) v) = t.realize v := by induction t with | var => simp | @func n f ts ih => cases n · cases f · simp only [realize, ih, constantsOn, constantsOnFunc, constantsToVars] -- Porting note: below lemma does not work with simp for some reason rw [withConstants_funMap_sumInl] · simp only [realize, constantsToVars, Sum.elim_inl, funMap_eq_coe_constants] rfl · obtain - | f := f · simp only [realize, ih, constantsOn, constantsOnFunc, constantsToVars] -- Porting note: below lemma does not work with simp for some reason rw [withConstants_funMap_sumInl] · exact isEmptyElim f @[simp] theorem realize_varsToConstants [L[[α]].Structure M] [(lhomWithConstants L α).IsExpansionOn M] {t : L.Term (α ⊕ β)} {v : β → M} : t.varsToConstants.realize v = t.realize (Sum.elim (fun a => ↑(L.con a)) v) := by induction t with | var ab => rcases ab with a | b <;> simp [Language.con] | func f ts ih => simp only [realize, constantsOn, constantsOnFunc, ih, varsToConstants] -- Porting note: below lemma does not work with simp for some reason rw [withConstants_funMap_sumInl] theorem realize_constantsVarsEquivLeft [L[[α]].Structure M] [(lhomWithConstants L α).IsExpansionOn M] {n} {t : L[[α]].Term (β ⊕ (Fin n))} {v : β → M} {xs : Fin n → M} : (constantsVarsEquivLeft t).realize (Sum.elim (Sum.elim (fun a => ↑(L.con a)) v) xs) = t.realize (Sum.elim v xs) := by simp only [constantsVarsEquivLeft, realize_relabel, Equiv.coe_trans, Function.comp_apply, constantsVarsEquiv_apply, relabelEquiv_symm_apply] refine _root_.trans ?_ realize_constantsToVars rcongr x rcases x with (a | (b | i)) <;> simp end Term namespace LHom @[simp] theorem realize_onTerm [L'.Structure M] (φ : L →ᴸ L') [φ.IsExpansionOn M] (t : L.Term α) (v : α → M) : (φ.onTerm t).realize v = t.realize v := by induction t with | var => rfl | func f ts ih => simp only [Term.realize, LHom.onTerm, LHom.map_onFunction, ih] end LHom @[simp] theorem HomClass.realize_term {F : Type*} [FunLike F M N] [HomClass L F M N] (g : F) {t : L.Term α} {v : α → M} : t.realize (g ∘ v) = g (t.realize v) := by induction t · rfl · rw [Term.realize, Term.realize, HomClass.map_fun] refine congr rfl ?_ ext x simp [*] variable {n : ℕ} namespace BoundedFormula open Term /-- A bounded formula can be evaluated as true or false by giving values to each free variable. -/ def Realize : ∀ {l} (_f : L.BoundedFormula α l) (_v : α → M) (_xs : Fin l → M), Prop | _, falsum, _v, _xs => False | _, equal t₁ t₂, v, xs => t₁.realize (Sum.elim v xs) = t₂.realize (Sum.elim v xs) | _, rel R ts, v, xs => RelMap R fun i => (ts i).realize (Sum.elim v xs) | _, imp f₁ f₂, v, xs => Realize f₁ v xs → Realize f₂ v xs | _, all f, v, xs => ∀ x : M, Realize f v (snoc xs x) variable {l : ℕ} {φ ψ : L.BoundedFormula α l} {θ : L.BoundedFormula α l.succ} variable {v : α → M} {xs : Fin l → M} @[simp] theorem realize_bot : (⊥ : L.BoundedFormula α l).Realize v xs ↔ False := Iff.rfl @[simp] theorem realize_not : φ.not.Realize v xs ↔ ¬φ.Realize v xs := Iff.rfl @[simp] theorem realize_bdEqual (t₁ t₂ : L.Term (α ⊕ (Fin l))) : (t₁.bdEqual t₂).Realize v xs ↔ t₁.realize (Sum.elim v xs) = t₂.realize (Sum.elim v xs) := Iff.rfl @[simp] theorem realize_top : (⊤ : L.BoundedFormula α l).Realize v xs ↔ True := by simp [Top.top] @[simp] theorem realize_inf : (φ ⊓ ψ).Realize v xs ↔ φ.Realize v xs ∧ ψ.Realize v xs := by simp [Inf.inf, Realize] @[simp] theorem realize_foldr_inf (l : List (L.BoundedFormula α n)) (v : α → M) (xs : Fin n → M) : (l.foldr (· ⊓ ·) ⊤).Realize v xs ↔ ∀ φ ∈ l, BoundedFormula.Realize φ v xs := by induction' l with φ l ih · simp · simp [ih] @[simp] theorem realize_imp : (φ.imp ψ).Realize v xs ↔ φ.Realize v xs → ψ.Realize v xs := by simp only [Realize] @[simp] theorem realize_rel {k : ℕ} {R : L.Relations k} {ts : Fin k → L.Term _} : (R.boundedFormula ts).Realize v xs ↔ RelMap R fun i => (ts i).realize (Sum.elim v xs) := Iff.rfl @[simp] theorem realize_rel₁ {R : L.Relations 1} {t : L.Term _} : (R.boundedFormula₁ t).Realize v xs ↔ RelMap R ![t.realize (Sum.elim v xs)] := by rw [Relations.boundedFormula₁, realize_rel, iff_eq_eq] refine congr rfl (funext fun _ => ?_) simp only [Matrix.cons_val_fin_one] @[simp] theorem realize_rel₂ {R : L.Relations 2} {t₁ t₂ : L.Term _} : (R.boundedFormula₂ t₁ t₂).Realize v xs ↔ RelMap R ![t₁.realize (Sum.elim v xs), t₂.realize (Sum.elim v xs)] := by rw [Relations.boundedFormula₂, realize_rel, iff_eq_eq] refine congr rfl (funext (Fin.cases ?_ ?_)) · simp only [Matrix.cons_val_zero] · simp only [Matrix.cons_val_succ, Matrix.cons_val_fin_one, forall_const] @[simp] theorem realize_sup : (φ ⊔ ψ).Realize v xs ↔ φ.Realize v xs ∨ ψ.Realize v xs := by simp only [realize, max, realize_not, eq_iff_iff] tauto @[simp] theorem realize_foldr_sup (l : List (L.BoundedFormula α n)) (v : α → M) (xs : Fin n → M) : (l.foldr (· ⊔ ·) ⊥).Realize v xs ↔ ∃ φ ∈ l, BoundedFormula.Realize φ v xs := by induction' l with φ l ih · simp · simp_rw [List.foldr_cons, realize_sup, ih, List.mem_cons, or_and_right, exists_or, exists_eq_left] @[simp] theorem realize_all : (all θ).Realize v xs ↔ ∀ a : M, θ.Realize v (Fin.snoc xs a) := Iff.rfl @[simp] theorem realize_ex : θ.ex.Realize v xs ↔ ∃ a : M, θ.Realize v (Fin.snoc xs a) := by rw [BoundedFormula.ex, realize_not, realize_all, not_forall] simp_rw [realize_not, Classical.not_not] @[simp] theorem realize_iff : (φ.iff ψ).Realize v xs ↔ (φ.Realize v xs ↔ ψ.Realize v xs) := by simp only [BoundedFormula.iff, realize_inf, realize_imp, and_imp, ← iff_def] theorem realize_castLE_of_eq {m n : ℕ} (h : m = n) {h' : m ≤ n} {φ : L.BoundedFormula α m} {v : α → M} {xs : Fin n → M} : (φ.castLE h').Realize v xs ↔ φ.Realize v (xs ∘ Fin.cast h) := by subst h simp only [castLE_rfl, cast_refl, OrderIso.coe_refl, Function.comp_id] theorem realize_mapTermRel_id [L'.Structure M] {ft : ∀ n, L.Term (α ⊕ (Fin n)) → L'.Term (β ⊕ (Fin n))} {fr : ∀ n, L.Relations n → L'.Relations n} {n} {φ : L.BoundedFormula α n} {v : α → M} {v' : β → M} {xs : Fin n → M} (h1 : ∀ (n) (t : L.Term (α ⊕ (Fin n))) (xs : Fin n → M), (ft n t).realize (Sum.elim v' xs) = t.realize (Sum.elim v xs)) (h2 : ∀ (n) (R : L.Relations n) (x : Fin n → M), RelMap (fr n R) x = RelMap R x) : (φ.mapTermRel ft fr fun _ => id).Realize v' xs ↔ φ.Realize v xs := by induction φ with | falsum => rfl | equal => simp [mapTermRel, Realize, h1] | rel => simp [mapTermRel, Realize, h1, h2] | imp _ _ ih1 ih2 => simp [mapTermRel, Realize, ih1, ih2] | all _ ih => simp only [mapTermRel, Realize, ih, id] theorem realize_mapTermRel_add_castLe [L'.Structure M] {k : ℕ} {ft : ∀ n, L.Term (α ⊕ (Fin n)) → L'.Term (β ⊕ (Fin (k + n)))} {fr : ∀ n, L.Relations n → L'.Relations n} {n} {φ : L.BoundedFormula α n} (v : ∀ {n}, (Fin (k + n) → M) → α → M) {v' : β → M} (xs : Fin (k + n) → M) (h1 : ∀ (n) (t : L.Term (α ⊕ (Fin n))) (xs' : Fin (k + n) → M), (ft n t).realize (Sum.elim v' xs') = t.realize (Sum.elim (v xs') (xs' ∘ Fin.natAdd _))) (h2 : ∀ (n) (R : L.Relations n) (x : Fin n → M), RelMap (fr n R) x = RelMap R x) (hv : ∀ (n) (xs : Fin (k + n) → M) (x : M), @v (n + 1) (snoc xs x : Fin _ → M) = v xs) : (φ.mapTermRel ft fr fun _ => castLE (add_assoc _ _ _).symm.le).Realize v' xs ↔ φ.Realize (v xs) (xs ∘ Fin.natAdd _) := by induction φ with | falsum => rfl | equal => simp [mapTermRel, Realize, h1] | rel => simp [mapTermRel, Realize, h1, h2] | imp _ _ ih1 ih2 => simp [mapTermRel, Realize, ih1, ih2] | all _ ih => simp [mapTermRel, Realize, ih, hv] @[simp] theorem realize_relabel {m n : ℕ} {φ : L.BoundedFormula α n} {g : α → β ⊕ (Fin m)} {v : β → M} {xs : Fin (m + n) → M} : (φ.relabel g).Realize v xs ↔ φ.Realize (Sum.elim v (xs ∘ Fin.castAdd n) ∘ g) (xs ∘ Fin.natAdd m) := by apply realize_mapTermRel_add_castLe <;> simp theorem realize_liftAt {n n' m : ℕ} {φ : L.BoundedFormula α n} {v : α → M} {xs : Fin (n + n') → M} (hmn : m + n' ≤ n + 1) : (φ.liftAt n' m).Realize v xs ↔ φ.Realize v (xs ∘ fun i => if ↑i < m then Fin.castAdd n' i else Fin.addNat i n') := by rw [liftAt] induction φ with | falsum => simp [mapTermRel, Realize] | equal => simp [mapTermRel, Realize, realize_rel, realize_liftAt, Sum.elim_comp_map] | rel => simp [mapTermRel, Realize, realize_rel, realize_liftAt, Sum.elim_comp_map] | imp _ _ ih1 ih2 => simp only [mapTermRel, Realize, ih1 hmn, ih2 hmn] | @all k _ ih3 => have h : k + 1 + n' = k + n' + 1 := by rw [add_assoc, add_comm 1 n', ← add_assoc] simp only [mapTermRel, Realize, realize_castLE_of_eq h, ih3 (hmn.trans k.succ.le_succ)] refine forall_congr' fun x => iff_eq_eq.mpr (congr rfl (funext (Fin.lastCases ?_ fun i => ?_))) · simp only [Function.comp_apply, val_last, snoc_last] refine (congr rfl (Fin.ext ?_)).trans (snoc_last _ _) split_ifs <;> dsimp; omega · simp only [Function.comp_apply, Fin.snoc_castSucc] refine (congr rfl (Fin.ext ?_)).trans (snoc_castSucc _ _ _) simp only [coe_castSucc, coe_cast] split_ifs <;> simp theorem realize_liftAt_one {n m : ℕ} {φ : L.BoundedFormula α n} {v : α → M} {xs : Fin (n + 1) → M} (hmn : m ≤ n) : (φ.liftAt 1 m).Realize v xs ↔ φ.Realize v (xs ∘ fun i => if ↑i < m then castSucc i else i.succ) := by simp [realize_liftAt (add_le_add_right hmn 1), castSucc] @[simp] theorem realize_liftAt_one_self {n : ℕ} {φ : L.BoundedFormula α n} {v : α → M} {xs : Fin (n + 1) → M} : (φ.liftAt 1 n).Realize v xs ↔ φ.Realize v (xs ∘ castSucc) := by rw [realize_liftAt_one (refl n), iff_eq_eq] refine congr rfl (congr rfl (funext fun i => ?_)) rw [if_pos i.is_lt] @[simp] theorem realize_subst {φ : L.BoundedFormula α n} {tf : α → L.Term β} {v : β → M} {xs : Fin n → M} : (φ.subst tf).Realize v xs ↔ φ.Realize (fun a => (tf a).realize v) xs := realize_mapTermRel_id (fun n t x => by rw [Term.realize_subst] rcongr a cases a · simp only [Sum.elim_inl, Function.comp_apply, Term.realize_relabel, Sum.elim_comp_inl] · rfl) (by simp) theorem realize_restrictFreeVar [DecidableEq α] {n : ℕ} {φ : L.BoundedFormula α n} {f : φ.freeVarFinset → β} {v : β → M} {xs : Fin n → M} (v' : α → M) (hv' : ∀ a, v (f a) = v' a) : (φ.restrictFreeVar f).Realize v xs ↔ φ.Realize v' xs := by induction φ with | falsum => rfl | equal => simp only [Realize, restrictFreeVar, freeVarFinset.eq_2] rw [realize_restrictVarLeft v' (by simp [hv']), realize_restrictVarLeft v' (by simp [hv'])] simp [Function.comp_apply] | rel => simp only [Realize, freeVarFinset.eq_3, Finset.biUnion_val, restrictFreeVar] congr! rw [realize_restrictVarLeft v' (by simp [hv'])] simp [Function.comp_apply] | imp _ _ ih1 ih2 => simp only [Realize, restrictFreeVar, freeVarFinset.eq_4] rw [ih1, ih2] <;> simp [hv'] | all _ ih3 => simp only [restrictFreeVar, Realize] refine forall_congr' (fun _ => ?_) rw [ih3]; simp [hv'] /-- A special case of `realize_restrictFreeVar`, included because we can add the `simp` attribute to it -/ @[simp] theorem realize_restrictFreeVar' [DecidableEq α] {n : ℕ} {φ : L.BoundedFormula α n} {s : Set α} (h : ↑φ.freeVarFinset ⊆ s) {v : α → M} {xs : Fin n → M} : (φ.restrictFreeVar (Set.inclusion h)).Realize (v ∘ (↑)) xs ↔ φ.Realize v xs := realize_restrictFreeVar _ (by simp) theorem realize_constantsVarsEquiv [L[[α]].Structure M] [(lhomWithConstants L α).IsExpansionOn M] {n} {φ : L[[α]].BoundedFormula β n} {v : β → M} {xs : Fin n → M} : (constantsVarsEquiv φ).Realize (Sum.elim (fun a => ↑(L.con a)) v) xs ↔ φ.Realize v xs := by refine realize_mapTermRel_id (fun n t xs => realize_constantsVarsEquivLeft) fun n R xs => ?_ -- This used to be `rw`, but we need `erw` after https://github.com/leanprover/lean4/pull/2644 erw [← (lhomWithConstants L α).map_onRelation (Equiv.sumEmpty (L.Relations n) ((constantsOn α).Relations n) R) xs] rcongr obtain - | R := R · simp · exact isEmptyElim R @[simp] theorem realize_relabelEquiv {g : α ≃ β} {k} {φ : L.BoundedFormula α k} {v : β → M} {xs : Fin k → M} : (relabelEquiv g φ).Realize v xs ↔ φ.Realize (v ∘ g) xs := by simp only [relabelEquiv, mapTermRelEquiv_apply, Equiv.coe_refl] refine realize_mapTermRel_id (fun n t xs => ?_) fun _ _ _ => rfl simp only [relabelEquiv_apply, Term.realize_relabel] refine congr (congr rfl ?_) rfl ext (i | i) <;> rfl variable [Nonempty M] theorem realize_all_liftAt_one_self {n : ℕ} {φ : L.BoundedFormula α n} {v : α → M} {xs : Fin n → M} : (φ.liftAt 1 n).all.Realize v xs ↔ φ.Realize v xs := by inhabit M simp only [realize_all, realize_liftAt_one_self] refine ⟨fun h => ?_, fun h a => ?_⟩ · refine (congr rfl (funext fun i => ?_)).mp (h default) simp · refine (congr rfl (funext fun i => ?_)).mp h simp end BoundedFormula namespace LHom open BoundedFormula @[simp] theorem realize_onBoundedFormula [L'.Structure M] (φ : L →ᴸ L') [φ.IsExpansionOn M] {n : ℕ} (ψ : L.BoundedFormula α n) {v : α → M} {xs : Fin n → M} : (φ.onBoundedFormula ψ).Realize v xs ↔ ψ.Realize v xs := by induction ψ with | falsum => rfl | equal => simp only [onBoundedFormula, realize_bdEqual, realize_onTerm]; rfl | rel => simp only [onBoundedFormula, realize_rel, LHom.map_onRelation, Function.comp_apply, realize_onTerm] rfl | imp _ _ ih1 ih2 => simp only [onBoundedFormula, ih1, ih2, realize_imp] | all _ ih3 => simp only [onBoundedFormula, ih3, realize_all] end LHom namespace Formula /-- A formula can be evaluated as true or false by giving values to each free variable. -/ nonrec def Realize (φ : L.Formula α) (v : α → M) : Prop := φ.Realize v default variable {φ ψ : L.Formula α} {v : α → M} @[simp] theorem realize_not : φ.not.Realize v ↔ ¬φ.Realize v := Iff.rfl @[simp] theorem realize_bot : (⊥ : L.Formula α).Realize v ↔ False := Iff.rfl @[simp] theorem realize_top : (⊤ : L.Formula α).Realize v ↔ True := BoundedFormula.realize_top @[simp] theorem realize_inf : (φ ⊓ ψ).Realize v ↔ φ.Realize v ∧ ψ.Realize v := BoundedFormula.realize_inf @[simp] theorem realize_imp : (φ.imp ψ).Realize v ↔ φ.Realize v → ψ.Realize v := BoundedFormula.realize_imp @[simp] theorem realize_rel {k : ℕ} {R : L.Relations k} {ts : Fin k → L.Term α} : (R.formula ts).Realize v ↔ RelMap R fun i => (ts i).realize v := BoundedFormula.realize_rel.trans (by simp) @[simp] theorem realize_rel₁ {R : L.Relations 1} {t : L.Term _} : (R.formula₁ t).Realize v ↔ RelMap R ![t.realize v] := by rw [Relations.formula₁, realize_rel, iff_eq_eq] refine congr rfl (funext fun _ => ?_) simp only [Matrix.cons_val_fin_one] @[simp] theorem realize_rel₂ {R : L.Relations 2} {t₁ t₂ : L.Term _} : (R.formula₂ t₁ t₂).Realize v ↔ RelMap R ![t₁.realize v, t₂.realize v] := by rw [Relations.formula₂, realize_rel, iff_eq_eq] refine congr rfl (funext (Fin.cases ?_ ?_)) · simp only [Matrix.cons_val_zero] · simp only [Matrix.cons_val_succ, Matrix.cons_val_fin_one, forall_const] @[simp] theorem realize_sup : (φ ⊔ ψ).Realize v ↔ φ.Realize v ∨ ψ.Realize v := BoundedFormula.realize_sup @[simp] theorem realize_iff : (φ.iff ψ).Realize v ↔ (φ.Realize v ↔ ψ.Realize v) := BoundedFormula.realize_iff @[simp] theorem realize_relabel {φ : L.Formula α} {g : α → β} {v : β → M} : (φ.relabel g).Realize v ↔ φ.Realize (v ∘ g) := by rw [Realize, Realize, relabel, BoundedFormula.realize_relabel, iff_eq_eq, Fin.castAdd_zero] exact congr rfl (funext finZeroElim) theorem realize_relabel_sumInr (φ : L.Formula (Fin n)) {v : Empty → M} {x : Fin n → M} : (BoundedFormula.relabel Sum.inr φ).Realize v x ↔ φ.Realize x := by rw [BoundedFormula.realize_relabel, Formula.Realize, Sum.elim_comp_inr, Fin.castAdd_zero, cast_refl, Function.comp_id, Subsingleton.elim (x ∘ (natAdd n : Fin 0 → Fin n)) default] @[deprecated (since := "2025-02-21")] alias realize_relabel_sum_inr := realize_relabel_sumInr @[simp] theorem realize_equal {t₁ t₂ : L.Term α} {x : α → M} : (t₁.equal t₂).Realize x ↔ t₁.realize x = t₂.realize x := by simp [Term.equal, Realize] @[simp] theorem realize_graph {f : L.Functions n} {x : Fin n → M} {y : M} : (Formula.graph f).Realize (Fin.cons y x : _ → M) ↔ funMap f x = y := by simp only [Formula.graph, Term.realize, realize_equal, Fin.cons_zero, Fin.cons_succ] rw [eq_comm] theorem boundedFormula_realize_eq_realize (φ : L.Formula α) (x : α → M) (y : Fin 0 → M) : BoundedFormula.Realize φ x y ↔ φ.Realize x := by rw [Formula.Realize, iff_iff_eq] congr ext i; exact Fin.elim0 i end Formula @[simp] theorem LHom.realize_onFormula [L'.Structure M] (φ : L →ᴸ L') [φ.IsExpansionOn M] (ψ : L.Formula α) {v : α → M} : (φ.onFormula ψ).Realize v ↔ ψ.Realize v := φ.realize_onBoundedFormula ψ @[simp] theorem LHom.setOf_realize_onFormula [L'.Structure M] (φ : L →ᴸ L') [φ.IsExpansionOn M] (ψ : L.Formula α) : (setOf (φ.onFormula ψ).Realize : Set (α → M)) = setOf ψ.Realize := by ext simp variable (M) /-- A sentence can be evaluated as true or false in a structure. -/ nonrec def Sentence.Realize (φ : L.Sentence) : Prop := φ.Realize (default : _ → M) -- input using \|= or \vDash, but not using \models @[inherit_doc Sentence.Realize] infixl:51 " ⊨ " => Sentence.Realize @[simp] theorem Sentence.realize_not {φ : L.Sentence} : M ⊨ φ.not ↔ ¬M ⊨ φ := Iff.rfl namespace Formula @[simp] theorem realize_equivSentence_symm_con [L[[α]].Structure M] [(L.lhomWithConstants α).IsExpansionOn M] (φ : L[[α]].Sentence) : ((equivSentence.symm φ).Realize fun a => (L.con a : M)) ↔ φ.Realize M := by simp only [equivSentence, _root_.Equiv.symm_symm, Equiv.coe_trans, Realize, BoundedFormula.realize_relabelEquiv, Function.comp] refine _root_.trans ?_ BoundedFormula.realize_constantsVarsEquiv rw [iff_iff_eq] congr with (_ | a) · simp · cases a @[simp] theorem realize_equivSentence [L[[α]].Structure M] [(L.lhomWithConstants α).IsExpansionOn M] (φ : L.Formula α) : (equivSentence φ).Realize M ↔ φ.Realize fun a => (L.con a : M) := by rw [← realize_equivSentence_symm_con M (equivSentence φ), _root_.Equiv.symm_apply_apply] theorem realize_equivSentence_symm (φ : L[[α]].Sentence) (v : α → M) : (equivSentence.symm φ).Realize v ↔ @Sentence.Realize _ M (@Language.withConstantsStructure L M _ α (constantsOn.structure v)) φ := letI := constantsOn.structure v realize_equivSentence_symm_con M φ end Formula @[simp] theorem LHom.realize_onSentence [L'.Structure M] (φ : L →ᴸ L') [φ.IsExpansionOn M] (ψ : L.Sentence) : M ⊨ φ.onSentence ψ ↔ M ⊨ ψ := φ.realize_onFormula ψ variable (L) /-- The complete theory of a structure `M` is the set of all sentences `M` satisfies. -/ def completeTheory : L.Theory := { φ | M ⊨ φ } variable (N) /-- Two structures are elementarily equivalent when they satisfy the same sentences. -/ def ElementarilyEquivalent : Prop := L.completeTheory M = L.completeTheory N @[inherit_doc FirstOrder.Language.ElementarilyEquivalent] scoped[FirstOrder] notation:25 A " ≅[" L "] " B:50 => FirstOrder.Language.ElementarilyEquivalent L A B variable {L} {M} {N} @[simp] theorem mem_completeTheory {φ : Sentence L} : φ ∈ L.completeTheory M ↔ M ⊨ φ := Iff.rfl theorem elementarilyEquivalent_iff : M ≅[L] N ↔ ∀ φ : L.Sentence, M ⊨ φ ↔ N ⊨ φ := by simp only [ElementarilyEquivalent, Set.ext_iff, completeTheory, Set.mem_setOf_eq] variable (M) /-- A model of a theory is a structure in which every sentence is realized as true. -/ class Theory.Model (T : L.Theory) : Prop where realize_of_mem : ∀ φ ∈ T, M ⊨ φ -- input using \|= or \vDash, but not using \models @[inherit_doc Theory.Model] infixl:51 " ⊨ " => Theory.Model variable {M} (T : L.Theory) @[simp default - 10] theorem Theory.model_iff : M ⊨ T ↔ ∀ φ ∈ T, M ⊨ φ := ⟨fun h => h.realize_of_mem, fun h => ⟨h⟩⟩ theorem Theory.realize_sentence_of_mem [M ⊨ T] {φ : L.Sentence} (h : φ ∈ T) : M ⊨ φ := Theory.Model.realize_of_mem φ h @[simp] theorem LHom.onTheory_model [L'.Structure M] (φ : L →ᴸ L') [φ.IsExpansionOn M] (T : L.Theory) : M ⊨ φ.onTheory T ↔ M ⊨ T := by simp [Theory.model_iff, LHom.onTheory] variable {T} instance model_empty : M ⊨ (∅ : L.Theory) := ⟨fun φ hφ => (Set.not_mem_empty φ hφ).elim⟩ namespace Theory theorem Model.mono {T' : L.Theory} (_h : M ⊨ T') (hs : T ⊆ T') : M ⊨ T := ⟨fun _φ hφ => T'.realize_sentence_of_mem (hs hφ)⟩ theorem Model.union {T' : L.Theory} (h : M ⊨ T) (h' : M ⊨ T') : M ⊨ T ∪ T' := by simp only [model_iff, Set.mem_union] at * exact fun φ hφ => hφ.elim (h _) (h' _) @[simp] theorem model_union_iff {T' : L.Theory} : M ⊨ T ∪ T' ↔ M ⊨ T ∧ M ⊨ T' := ⟨fun h => ⟨h.mono Set.subset_union_left, h.mono Set.subset_union_right⟩, fun h => h.1.union h.2⟩ @[simp] theorem model_singleton_iff {φ : L.Sentence} : M ⊨ ({φ} : L.Theory) ↔ M ⊨ φ := by simp theorem model_insert_iff {φ : L.Sentence} : M ⊨ insert φ T ↔ M ⊨ φ ∧ M ⊨ T := by rw [Set.insert_eq, model_union_iff, model_singleton_iff] theorem model_iff_subset_completeTheory : M ⊨ T ↔ T ⊆ L.completeTheory M := T.model_iff theorem completeTheory.subset [MT : M ⊨ T] : T ⊆ L.completeTheory M := model_iff_subset_completeTheory.1 MT end Theory instance model_completeTheory : M ⊨ L.completeTheory M := Theory.model_iff_subset_completeTheory.2 (subset_refl _) variable (M N) theorem realize_iff_of_model_completeTheory [N ⊨ L.completeTheory M] (φ : L.Sentence) : N ⊨ φ ↔ M ⊨ φ := by refine ⟨fun h => ?_, (L.completeTheory M).realize_sentence_of_mem⟩ contrapose! h rw [← Sentence.realize_not] at * exact (L.completeTheory M).realize_sentence_of_mem (mem_completeTheory.2 h) variable {M N} namespace BoundedFormula @[simp] theorem realize_alls {φ : L.BoundedFormula α n} {v : α → M} : φ.alls.Realize v ↔ ∀ xs : Fin n → M, φ.Realize v xs := by induction' n with n ih · exact Unique.forall_iff.symm · simp only [alls, ih, Realize] exact ⟨fun h xs => Fin.snoc_init_self xs ▸ h _ _, fun h xs x => h (Fin.snoc xs x)⟩ @[simp] theorem realize_exs {φ : L.BoundedFormula α n} {v : α → M} : φ.exs.Realize v ↔ ∃ xs : Fin n → M, φ.Realize v xs := by induction' n with n ih · exact Unique.exists_iff.symm · simp only [BoundedFormula.exs, ih, realize_ex] constructor · rintro ⟨xs, x, h⟩ exact ⟨_, h⟩ · rintro ⟨xs, h⟩ rw [← Fin.snoc_init_self xs] at h exact ⟨_, _, h⟩ @[simp] theorem _root_.FirstOrder.Language.Formula.realize_iAlls [Finite β] {φ : L.Formula (α ⊕ β)} {v : α → M} : (φ.iAlls β).Realize v ↔ ∀ (i : β → M), φ.Realize (fun a => Sum.elim v i a) := by let e := Classical.choice (Classical.choose_spec (Finite.exists_equiv_fin β)) rw [Formula.iAlls] simp only [Nat.add_zero, realize_alls, realize_relabel, Function.comp_def, castAdd_zero, finCongr_refl, OrderIso.refl_apply, Sum.elim_map, id_eq] refine Equiv.forall_congr ?_ ?_ · exact ⟨fun v => v ∘ e, fun v => v ∘ e.symm, fun _ => by simp [Function.comp_def], fun _ => by simp [Function.comp_def]⟩ · intro x rw [Formula.Realize, iff_iff_eq] congr funext i exact i.elim0 @[simp] theorem realize_iAlls [Finite β] {φ : L.Formula (α ⊕ β)} {v : α → M} {v' : Fin 0 → M} : BoundedFormula.Realize (φ.iAlls β) v v' ↔ ∀ (i : β → M), φ.Realize (fun a => Sum.elim v i a) := by rw [← Formula.realize_iAlls, iff_iff_eq]; congr; simp [eq_iff_true_of_subsingleton] @[simp] theorem _root_.FirstOrder.Language.Formula.realize_iExs [Finite γ] {φ : L.Formula (α ⊕ γ)} {v : α → M} : (φ.iExs γ).Realize v ↔ ∃ (i : γ → M), φ.Realize (Sum.elim v i) := by let e := Classical.choice (Classical.choose_spec (Finite.exists_equiv_fin γ)) rw [Formula.iExs] simp only [Nat.add_zero, realize_exs, realize_relabel, Function.comp_def, castAdd_zero, finCongr_refl, OrderIso.refl_apply, Sum.elim_map, id_eq] refine Equiv.exists_congr ?_ ?_ · exact ⟨fun v => v ∘ e, fun v => v ∘ e.symm, fun _ => by simp [Function.comp_def], fun _ => by simp [Function.comp_def]⟩ · intro x rw [Formula.Realize, iff_iff_eq] congr funext i exact i.elim0 @[simp] theorem realize_iExs [Finite γ] {φ : L.Formula (α ⊕ γ)} {v : α → M} {v' : Fin 0 → M} : BoundedFormula.Realize (φ.iExs γ) v v' ↔ ∃ (i : γ → M), φ.Realize (Sum.elim v i) := by rw [← Formula.realize_iExs, iff_iff_eq]; congr; simp [eq_iff_true_of_subsingleton] @[simp] theorem realize_toFormula (φ : L.BoundedFormula α n) (v : α ⊕ (Fin n) → M) : φ.toFormula.Realize v ↔ φ.Realize (v ∘ Sum.inl) (v ∘ Sum.inr) := by induction φ with | falsum => rfl | equal => simp [BoundedFormula.Realize] | rel => simp [BoundedFormula.Realize] | imp _ _ ih1 ih2 => rw [toFormula, Formula.Realize, realize_imp, ← Formula.Realize, ih1, ← Formula.Realize, ih2, realize_imp] | all _ ih3 => rw [toFormula, Formula.Realize, realize_all, realize_all] refine forall_congr' fun a => ?_ have h := ih3 (Sum.elim (v ∘ Sum.inl) (snoc (v ∘ Sum.inr) a)) simp only [Sum.elim_comp_inl, Sum.elim_comp_inr] at h rw [← h, realize_relabel, Formula.Realize, iff_iff_eq] simp only [Function.comp_def] congr with x · rcases x with _ | x · simp · refine Fin.lastCases ?_ ?_ x · rw [Sum.elim_inr, Sum.elim_inr, finSumFinEquiv_symm_last, Sum.map_inr, Sum.elim_inr] simp [Fin.snoc] · simp only [castSucc, Function.comp_apply, Sum.elim_inr, finSumFinEquiv_symm_apply_castAdd, Sum.map_inl, Sum.elim_inl] rw [← castSucc] simp · exact Fin.elim0 x @[simp] theorem realize_iSup [Finite β] {f : β → L.BoundedFormula α n} {v : α → M} {v' : Fin n → M} : (iSup f).Realize v v' ↔ ∃ b, (f b).Realize v v' := by simp only [iSup, realize_foldr_sup, List.mem_map, Finset.mem_toList, Finset.mem_univ, true_and, exists_exists_eq_and] @[simp] theorem realize_iInf [Finite β] {f : β → L.BoundedFormula α n} {v : α → M} {v' : Fin n → M} : (iInf f).Realize v v' ↔ ∀ b, (f b).Realize v v' := by simp only [iInf, realize_foldr_inf, List.mem_map, Finset.mem_toList, Finset.mem_univ, true_and, forall_exists_index, forall_apply_eq_imp_iff]
@[simp]
Mathlib/ModelTheory/Semantics.lean
853
853
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudryashov, Yaël Dillies -/ import Mathlib.Algebra.Order.Invertible import Mathlib.Algebra.Order.Module.OrderedSMul import Mathlib.LinearAlgebra.AffineSpace.Midpoint import Mathlib.LinearAlgebra.LinearIndependent.Lemmas import Mathlib.LinearAlgebra.Ray import Mathlib.Tactic.GCongr /-! # Segments in vector spaces In a 𝕜-vector space, we define the following objects and properties. * `segment 𝕜 x y`: Closed segment joining `x` and `y`. * `openSegment 𝕜 x y`: Open segment joining `x` and `y`. ## Notations We provide the following notation: * `[x -[𝕜] y] = segment 𝕜 x y` in locale `Convex` ## TODO Generalize all this file to affine spaces. Should we rename `segment` and `openSegment` to `convex.Icc` and `convex.Ioo`? Should we also define `clopenSegment`/`convex.Ico`/`convex.Ioc`? -/ variable {𝕜 E F G ι : Type*} {M : ι → Type*} open Function Set open Pointwise Convex section OrderedSemiring variable [Semiring 𝕜] [PartialOrder 𝕜] [AddCommMonoid E] section SMul variable (𝕜) [SMul 𝕜 E] {s : Set E} {x y : E} /-- Segments in a vector space. -/ def segment (x y : E) : Set E := { z : E | ∃ a b : 𝕜, 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ a • x + b • y = z } /-- Open segment in a vector space. Note that `openSegment 𝕜 x x = {x}` instead of being `∅` when the base semiring has some element between `0` and `1`. Denoted as `[x -[𝕜] y]` within the `Convex` namespace. -/ def openSegment (x y : E) : Set E := { z : E | ∃ a b : 𝕜, 0 < a ∧ 0 < b ∧ a + b = 1 ∧ a • x + b • y = z } @[inherit_doc] scoped[Convex] notation (priority := high) "[" x " -[" 𝕜 "] " y "]" => segment 𝕜 x y theorem segment_eq_image₂ (x y : E) : [x -[𝕜] y] = (fun p : 𝕜 × 𝕜 => p.1 • x + p.2 • y) '' { p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1 } := by simp only [segment, image, Prod.exists, mem_setOf_eq, exists_prop, and_assoc] theorem openSegment_eq_image₂ (x y : E) : openSegment 𝕜 x y = (fun p : 𝕜 × 𝕜 => p.1 • x + p.2 • y) '' { p | 0 < p.1 ∧ 0 < p.2 ∧ p.1 + p.2 = 1 } := by simp only [openSegment, image, Prod.exists, mem_setOf_eq, exists_prop, and_assoc] theorem segment_symm (x y : E) : [x -[𝕜] y] = [y -[𝕜] x] := Set.ext fun _ => ⟨fun ⟨a, b, ha, hb, hab, H⟩ => ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, fun ⟨a, b, ha, hb, hab, H⟩ => ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ theorem openSegment_symm (x y : E) : openSegment 𝕜 x y = openSegment 𝕜 y x := Set.ext fun _ => ⟨fun ⟨a, b, ha, hb, hab, H⟩ => ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, fun ⟨a, b, ha, hb, hab, H⟩ => ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ theorem openSegment_subset_segment (x y : E) : openSegment 𝕜 x y ⊆ [x -[𝕜] y] := fun _ ⟨a, b, ha, hb, hab, hz⟩ => ⟨a, b, ha.le, hb.le, hab, hz⟩ theorem segment_subset_iff : [x -[𝕜] y] ⊆ s ↔ ∀ a b : 𝕜, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s := ⟨fun H a b ha hb hab => H ⟨a, b, ha, hb, hab, rfl⟩, fun H _ ⟨a, b, ha, hb, hab, hz⟩ => hz ▸ H a b ha hb hab⟩ theorem openSegment_subset_iff : openSegment 𝕜 x y ⊆ s ↔ ∀ a b : 𝕜, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := ⟨fun H a b ha hb hab => H ⟨a, b, ha, hb, hab, rfl⟩, fun H _ ⟨a, b, ha, hb, hab, hz⟩ => hz ▸ H a b ha hb hab⟩ end SMul open Convex section MulActionWithZero variable (𝕜) variable [ZeroLEOneClass 𝕜] [MulActionWithZero 𝕜 E] theorem left_mem_segment (x y : E) : x ∈ [x -[𝕜] y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ theorem right_mem_segment (x y : E) : y ∈ [x -[𝕜] y] := segment_symm 𝕜 y x ▸ left_mem_segment 𝕜 y x end MulActionWithZero section Module variable (𝕜) variable [ZeroLEOneClass 𝕜] [Module 𝕜 E] {s : Set E} {x y z : E} @[simp] theorem segment_same (x : E) : [x -[𝕜] x] = {x} := Set.ext fun z => ⟨fun ⟨a, b, _, _, hab, hz⟩ => by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, fun h => mem_singleton_iff.1 h ▸ left_mem_segment 𝕜 z z⟩ theorem insert_endpoints_openSegment (x y : E) : insert x (insert y (openSegment 𝕜 x y)) = [x -[𝕜] y] := by simp only [subset_antisymm_iff, insert_subset_iff, left_mem_segment, right_mem_segment, openSegment_subset_segment, true_and] rintro z ⟨a, b, ha, hb, hab, rfl⟩ refine hb.eq_or_gt.imp ?_ fun hb' => ha.eq_or_gt.imp ?_ fun ha' => ?_ · rintro rfl rw [← add_zero a, hab, one_smul, zero_smul, add_zero] · rintro rfl rw [← zero_add b, hab, one_smul, zero_smul, zero_add] · exact ⟨a, b, ha', hb', hab, rfl⟩ variable {𝕜} theorem mem_openSegment_of_ne_left_right (hx : x ≠ z) (hy : y ≠ z) (hz : z ∈ [x -[𝕜] y]) : z ∈ openSegment 𝕜 x y := by rw [← insert_endpoints_openSegment] at hz exact (hz.resolve_left hx.symm).resolve_left hy.symm theorem openSegment_subset_iff_segment_subset (hx : x ∈ s) (hy : y ∈ s) : openSegment 𝕜 x y ⊆ s ↔ [x -[𝕜] y] ⊆ s := by simp only [← insert_endpoints_openSegment, insert_subset_iff, *, true_and] end Module end OrderedSemiring open Convex section OrderedRing variable (𝕜) [Ring 𝕜] [PartialOrder 𝕜] [AddRightMono 𝕜] [AddCommGroup E] [AddCommGroup F] [AddCommGroup G] [Module 𝕜 E] [Module 𝕜 F]
section DenselyOrdered variable [ZeroLEOneClass 𝕜] [Nontrivial 𝕜] [DenselyOrdered 𝕜]
Mathlib/Analysis/Convex/Segment.lean
158
160
/- 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.Probability.ConditionalProbability import Mathlib.Probability.Kernel.Basic import Mathlib.Probability.Kernel.Composition.MeasureComp import Mathlib.Tactic.Peel import Mathlib.MeasureTheory.MeasurableSpace.Pi /-! # Independence with respect to a kernel and a measure A family of sets of sets `π : ι → Set (Set Ω)` is independent with respect to a kernel `κ : Kernel α Ω` and a measure `μ` on `α` if for any finite set of indices `s = {i_1, ..., i_n}`, for any sets `f i_1 ∈ π i_1, ..., f i_n ∈ π i_n`, then for `μ`-almost every `a : α`, `κ a (⋂ i in s, f i) = ∏ i ∈ s, κ a (f i)`. This notion of independence is a generalization of both independence and conditional independence. For conditional independence, `κ` is the conditional kernel `ProbabilityTheory.condExpKernel` and `μ` is the ambient measure. For (non-conditional) independence, `κ = Kernel.const Unit μ` and the measure is the Dirac measure on `Unit`. The main purpose of this file is to prove only once the properties that hold for both conditional and non-conditional independence. ## Main definitions * `ProbabilityTheory.Kernel.iIndepSets`: independence of a family of sets of sets. Variant for two sets of sets: `ProbabilityTheory.Kernel.IndepSets`. * `ProbabilityTheory.Kernel.iIndep`: independence of a family of σ-algebras. Variant for two σ-algebras: `Indep`. * `ProbabilityTheory.Kernel.iIndepSet`: independence of a family of sets. Variant for two sets: `ProbabilityTheory.Kernel.IndepSet`. * `ProbabilityTheory.Kernel.iIndepFun`: independence of a family of functions (random variables). Variant for two functions: `ProbabilityTheory.Kernel.IndepFun`. See the file `Mathlib/Probability/Kernel/Basic.lean` for a more detailed discussion of these definitions in the particular case of the usual independence notion. ## Main statements * `ProbabilityTheory.Kernel.iIndepSets.iIndep`: if π-systems are independent as sets of sets, then the measurable space structures they generate are independent. * `ProbabilityTheory.Kernel.IndepSets.Indep`: variant with two π-systems. -/ open Set MeasureTheory MeasurableSpace open scoped MeasureTheory ENNReal namespace ProbabilityTheory.Kernel variable {α Ω ι : Type*} section Definitions variable {_mα : MeasurableSpace α} /-- A family of sets of sets `π : ι → Set (Set Ω)` is independent with respect to a kernel `κ` and a measure `μ` if for any finite set of indices `s = {i_1, ..., i_n}`, for any sets `f i_1 ∈ π i_1, ..., f i_n ∈ π i_n`, then `∀ᵐ a ∂μ, κ a (⋂ i in s, f i) = ∏ i ∈ s, κ a (f i)`. It will be used for families of pi_systems. -/ def iIndepSets {_mΩ : MeasurableSpace Ω} (π : ι → Set (Set Ω)) (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := ∀ (s : Finset ι) {f : ι → Set Ω} (_H : ∀ i, i ∈ s → f i ∈ π i), ∀ᵐ a ∂μ, κ a (⋂ i ∈ s, f i) = ∏ i ∈ s, κ a (f i) /-- Two sets of sets `s₁, s₂` are independent with respect to a kernel `κ` and a measure `μ` if for any sets `t₁ ∈ s₁, t₂ ∈ s₂`, then `∀ᵐ a ∂μ, κ a (t₁ ∩ t₂) = κ a (t₁) * κ a (t₂)` -/ def IndepSets {_mΩ : MeasurableSpace Ω} (s1 s2 : Set (Set Ω)) (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := ∀ t1 t2 : Set Ω, t1 ∈ s1 → t2 ∈ s2 → (∀ᵐ a ∂μ, κ a (t1 ∩ t2) = κ a t1 * κ a t2) /-- A family of measurable space structures (i.e. of σ-algebras) is independent with respect to a kernel `κ` and a measure `μ` if the family of sets of measurable sets they define is independent. -/ def iIndep (m : ι → MeasurableSpace Ω) {_mΩ : MeasurableSpace Ω} (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := iIndepSets (fun x ↦ {s | MeasurableSet[m x] s}) κ μ /-- Two measurable space structures (or σ-algebras) `m₁, m₂` are independent with respect to a kernel `κ` and a measure `μ` if for any sets `t₁ ∈ m₁, t₂ ∈ m₂`, `∀ᵐ a ∂μ, κ a (t₁ ∩ t₂) = κ a (t₁) * κ a (t₂)` -/ def Indep (m₁ m₂ : MeasurableSpace Ω) {_mΩ : MeasurableSpace Ω} (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := IndepSets {s | MeasurableSet[m₁] s} {s | MeasurableSet[m₂] s} κ μ /-- A family of sets is independent if the family of measurable space structures they generate is independent. For a set `s`, the generated measurable space has measurable sets `∅, s, sᶜ, univ`. -/ def iIndepSet {_mΩ : MeasurableSpace Ω} (s : ι → Set Ω) (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := iIndep (m := fun i ↦ generateFrom {s i}) κ μ /-- Two sets are independent if the two measurable space structures they generate are independent. For a set `s`, the generated measurable space structure has measurable sets `∅, s, sᶜ, univ`. -/ def IndepSet {_mΩ : MeasurableSpace Ω} (s t : Set Ω) (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := Indep (generateFrom {s}) (generateFrom {t}) κ μ /-- A family of functions defined on the same space `Ω` and taking values in possibly different spaces, each with a measurable space structure, is independent if the family of measurable space structures they generate on `Ω` is independent. For a function `g` with codomain having measurable space structure `m`, the generated measurable space structure is `MeasurableSpace.comap g m`. -/ def iIndepFun {_mΩ : MeasurableSpace Ω} {β : ι → Type*} [m : ∀ x : ι, MeasurableSpace (β x)] (f : ∀ x : ι, Ω → β x) (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := iIndep (m := fun x ↦ MeasurableSpace.comap (f x) (m x)) κ μ /-- Two functions are independent if the two measurable space structures they generate are independent. For a function `f` with codomain having measurable space structure `m`, the generated measurable space structure is `MeasurableSpace.comap f m`. -/ def IndepFun {β γ} {_mΩ : MeasurableSpace Ω} [mβ : MeasurableSpace β] [mγ : MeasurableSpace γ] (f : Ω → β) (g : Ω → γ) (κ : Kernel α Ω) (μ : Measure α := by volume_tac) : Prop := Indep (MeasurableSpace.comap f mβ) (MeasurableSpace.comap g mγ) κ μ end Definitions section ByDefinition variable {β : ι → Type*} {mβ : ∀ i, MeasurableSpace (β i)} {_mα : MeasurableSpace α} {m : ι → MeasurableSpace Ω} {_mΩ : MeasurableSpace Ω} {κ η : Kernel α Ω} {μ : Measure α} {π : ι → Set (Set Ω)} {s : ι → Set Ω} {S : Finset ι} {f : ∀ x : ι, Ω → β x} {s1 s2 : Set (Set Ω)} @[simp] lemma iIndepSets_zero_right : iIndepSets π κ 0 := by simp [iIndepSets] @[simp] lemma indepSets_zero_right : IndepSets s1 s2 κ 0 := by simp [IndepSets] @[simp] lemma indepSets_zero_left : IndepSets s1 s2 (0 : Kernel α Ω) μ := by simp [IndepSets] @[simp] lemma iIndep_zero_right : iIndep m κ 0 := by simp [iIndep] @[simp] lemma indep_zero_right {m₁ m₂ : MeasurableSpace Ω} {_mΩ : MeasurableSpace Ω} {κ : Kernel α Ω} : Indep m₁ m₂ κ 0 := by simp [Indep] @[simp] lemma indep_zero_left {m₁ m₂ : MeasurableSpace Ω} {_mΩ : MeasurableSpace Ω} : Indep m₁ m₂ (0 : Kernel α Ω) μ := by simp [Indep] @[simp] lemma iIndepSet_zero_right : iIndepSet s κ 0 := by simp [iIndepSet] @[simp] lemma indepSet_zero_right {s t : Set Ω} : IndepSet s t κ 0 := by simp [IndepSet] @[simp] lemma indepSet_zero_left {s t : Set Ω} : IndepSet s t (0 : Kernel α Ω) μ := by simp [IndepSet] @[simp] lemma iIndepFun_zero_right {β : ι → Type*} {m : ∀ x : ι, MeasurableSpace (β x)} {f : ∀ x : ι, Ω → β x} : iIndepFun f κ 0 := by simp [iIndepFun] @[simp] lemma indepFun_zero_right {β γ} [MeasurableSpace β] [MeasurableSpace γ] {f : Ω → β} {g : Ω → γ} : IndepFun f g κ 0 := by simp [IndepFun] @[simp] lemma indepFun_zero_left {β γ} [MeasurableSpace β] [MeasurableSpace γ] {f : Ω → β} {g : Ω → γ} : IndepFun f g (0 : Kernel α Ω) μ := by simp [IndepFun] lemma iIndepSets_congr (h : κ =ᵐ[μ] η) : iIndepSets π κ μ ↔ iIndepSets π η μ := by peel 3 refine ⟨fun h' ↦ ?_, fun h' ↦ ?_⟩ <;> · filter_upwards [h, h'] with a ha h'a simpa [ha] using h'a alias ⟨iIndepSets.congr, _⟩ := iIndepSets_congr lemma indepSets_congr (h : κ =ᵐ[μ] η) : IndepSets s1 s2 κ μ ↔ IndepSets s1 s2 η μ := by peel 4 refine ⟨fun h' ↦ ?_, fun h' ↦ ?_⟩ <;> · filter_upwards [h, h'] with a ha h'a simpa [ha] using h'a alias ⟨IndepSets.congr, _⟩ := indepSets_congr lemma iIndep_congr (h : κ =ᵐ[μ] η) : iIndep m κ μ ↔ iIndep m η μ := iIndepSets_congr h alias ⟨iIndep.congr, _⟩ := iIndep_congr lemma indep_congr {m₁ m₂ : MeasurableSpace Ω} {_mΩ : MeasurableSpace Ω} {κ η : Kernel α Ω} (h : κ =ᵐ[μ] η) : Indep m₁ m₂ κ μ ↔ Indep m₁ m₂ η μ := indepSets_congr h alias ⟨Indep.congr, _⟩ := indep_congr lemma iIndepSet_congr (h : κ =ᵐ[μ] η) : iIndepSet s κ μ ↔ iIndepSet s η μ := iIndep_congr h alias ⟨iIndepSet.congr, _⟩ := iIndepSet_congr lemma indepSet_congr {s t : Set Ω} (h : κ =ᵐ[μ] η) : IndepSet s t κ μ ↔ IndepSet s t η μ := indep_congr h alias ⟨indepSet.congr, _⟩ := indepSet_congr lemma iIndepFun_congr {β : ι → Type*} {m : ∀ x : ι, MeasurableSpace (β x)} {f : ∀ x : ι, Ω → β x} (h : κ =ᵐ[μ] η) : iIndepFun f κ μ ↔ iIndepFun f η μ := iIndep_congr h alias ⟨iIndepFun.congr, _⟩ := iIndepFun_congr lemma indepFun_congr {β γ} [MeasurableSpace β] [MeasurableSpace γ] {f : Ω → β} {g : Ω → γ} (h : κ =ᵐ[μ] η) : IndepFun f g κ μ ↔ IndepFun f g η μ := indep_congr h alias ⟨IndepFun.congr, _⟩ := indepFun_congr lemma iIndepSets.meas_biInter (h : iIndepSets π κ μ) (s : Finset ι) {f : ι → Set Ω} (hf : ∀ i, i ∈ s → f i ∈ π i) : ∀ᵐ a ∂μ, κ a (⋂ i ∈ s, f i) = ∏ i ∈ s, κ a (f i) := h s hf lemma iIndepSets.ae_isProbabilityMeasure (h : iIndepSets π κ μ) : ∀ᵐ a ∂μ, IsProbabilityMeasure (κ a) := by filter_upwards [h.meas_biInter ∅ (f := fun _ ↦ Set.univ) (by simp)] with a ha exact ⟨by simpa using ha⟩ lemma iIndepSets.meas_iInter [Fintype ι] (h : iIndepSets π κ μ) (hs : ∀ i, s i ∈ π i) : ∀ᵐ a ∂μ, κ a (⋂ i, s i) = ∏ i, κ a (s i) := by filter_upwards [h.meas_biInter Finset.univ (fun _i _ ↦ hs _)] with a ha using by simp [← ha] lemma iIndep.iIndepSets' (hμ : iIndep m κ μ) : iIndepSets (fun x ↦ {s | MeasurableSet[m x] s}) κ μ := hμ lemma iIndep.ae_isProbabilityMeasure (h : iIndep m κ μ) : ∀ᵐ a ∂μ, IsProbabilityMeasure (κ a) := h.iIndepSets'.ae_isProbabilityMeasure lemma iIndep.meas_biInter (hμ : iIndep m κ μ) (hs : ∀ i, i ∈ S → MeasurableSet[m i] (s i)) : ∀ᵐ a ∂μ, κ a (⋂ i ∈ S, s i) = ∏ i ∈ S, κ a (s i) := hμ _ hs lemma iIndep.meas_iInter [Fintype ι] (h : iIndep m κ μ) (hs : ∀ i, MeasurableSet[m i] (s i)) : ∀ᵐ a ∂μ, κ a (⋂ i, s i) = ∏ i, κ a (s i) := by filter_upwards [h.meas_biInter (fun i (_ : i ∈ Finset.univ) ↦ hs _)] with a ha simp [← ha] @[nontriviality, simp] lemma iIndepSets.of_subsingleton [Subsingleton ι] {m : ι → Set (Set Ω)} {κ : Kernel α Ω} [IsMarkovKernel κ] : iIndepSets m κ μ := by rintro s f hf obtain rfl | ⟨i, rfl⟩ : s = ∅ ∨ ∃ i, s = {i} := by simpa using (subsingleton_of_subsingleton (s := s.toSet)).eq_empty_or_singleton all_goals simp @[nontriviality, simp] lemma iIndep.of_subsingleton [Subsingleton ι] {m : ι → MeasurableSpace Ω} {κ : Kernel α Ω} [IsMarkovKernel κ] : iIndep m κ μ := by simp [iIndep] @[nontriviality, simp] lemma iIndepFun.of_subsingleton [Subsingleton ι] {β : ι → Type*} {m : ∀ i, MeasurableSpace (β i)} {f : ∀ i, Ω → β i} [IsMarkovKernel κ] : iIndepFun f κ μ := by simp [iIndepFun] protected lemma iIndepFun.iIndep (hf : iIndepFun f κ μ) : iIndep (fun x ↦ (mβ x).comap (f x)) κ μ := hf lemma iIndepFun.ae_isProbabilityMeasure (h : iIndepFun f κ μ) : ∀ᵐ a ∂μ, IsProbabilityMeasure (κ a) := h.iIndep.ae_isProbabilityMeasure lemma iIndepFun.meas_biInter (hf : iIndepFun f κ μ) (hs : ∀ i, i ∈ S → MeasurableSet[(mβ i).comap (f i)] (s i)) : ∀ᵐ a ∂μ, κ a (⋂ i ∈ S, s i) = ∏ i ∈ S, κ a (s i) := hf.iIndep.meas_biInter hs lemma iIndepFun.meas_iInter [Fintype ι] (hf : iIndepFun f κ μ) (hs : ∀ i, MeasurableSet[(mβ i).comap (f i)] (s i)) : ∀ᵐ a ∂μ, κ a (⋂ i, s i) = ∏ i, κ a (s i) := hf.iIndep.meas_iInter hs lemma IndepFun.meas_inter {β γ : Type*} [mβ : MeasurableSpace β] [mγ : MeasurableSpace γ]
{f : Ω → β} {g : Ω → γ} (hfg : IndepFun f g κ μ) {s t : Set Ω} (hs : MeasurableSet[mβ.comap f] s) (ht : MeasurableSet[mγ.comap g] t) : ∀ᵐ a ∂μ, κ a (s ∩ t) = κ a s * κ a t := hfg _ _ hs ht end ByDefinition
Mathlib/Probability/Independence/Kernel.lean
268
273
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Mario Carneiro -/ import Mathlib.Algebra.Field.IsField import Mathlib.Data.Fin.VecNotation import Mathlib.Data.Nat.Choose.Sum import Mathlib.LinearAlgebra.Finsupp.LinearCombination import Mathlib.RingTheory.Ideal.Maximal import Mathlib.Tactic.FinCases /-! # Ideals over a ring This file contains an assortment of definitions and results for `Ideal R`, the type of (left) ideals over a ring `R`. Note that over commutative rings, left ideals and two-sided ideals are equivalent. ## Implementation notes `Ideal R` is implemented using `Submodule R R`, where `•` is interpreted as `*`. ## TODO Support right ideals, and two-sided ideals over non-commutative rings. -/ variable {ι α β F : Type*} open Set Function open Pointwise section Semiring namespace Ideal variable {α : ι → Type*} [Π i, Semiring (α i)] (I : Π i, Ideal (α i)) section Pi /-- `Πᵢ Iᵢ` as an ideal of `Πᵢ Rᵢ`. -/ def pi : Ideal (Π i, α i) where carrier := { x | ∀ i, x i ∈ I i } zero_mem' i := (I i).zero_mem add_mem' ha hb i := (I i).add_mem (ha i) (hb i) smul_mem' a _b hb i := (I i).mul_mem_left (a i) (hb i) theorem mem_pi (x : Π i, α i) : x ∈ pi I ↔ ∀ i, x i ∈ I i := Iff.rfl instance (priority := low) [∀ i, (I i).IsTwoSided] : (pi I).IsTwoSided := ⟨fun _b hb i ↦ mul_mem_right _ _ (hb i)⟩ end Pi section Commute variable {α : Type*} [Semiring α] (I : Ideal α) {a b : α} theorem add_pow_mem_of_pow_mem_of_le_of_commute {m n k : ℕ} (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hk : m + n ≤ k + 1) (hab : Commute a b) : (a + b) ^ k ∈ I := by simp_rw [hab.add_pow, ← Nat.cast_comm] apply I.sum_mem intro c _ apply mul_mem_left by_cases h : m ≤ c · rw [hab.pow_pow] exact I.mul_mem_left _ (I.pow_mem_of_pow_mem ha h) · refine I.mul_mem_left _ (I.pow_mem_of_pow_mem hb ?_) omega theorem add_pow_add_pred_mem_of_pow_mem_of_commute {m n : ℕ} (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hab : Commute a b) : (a + b) ^ (m + n - 1) ∈ I := I.add_pow_mem_of_pow_mem_of_le_of_commute ha hb (by rw [← Nat.sub_le_iff_le_add]) hab end Commute end Ideal end Semiring section CommSemiring variable {a b : α} -- A separate namespace definition is needed because the variables were historically in a different -- order. namespace Ideal variable [CommSemiring α] (I : Ideal α) theorem add_pow_mem_of_pow_mem_of_le {m n k : ℕ} (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hk : m + n ≤ k + 1) : (a + b) ^ k ∈ I := I.add_pow_mem_of_pow_mem_of_le_of_commute ha hb hk (Commute.all ..) theorem add_pow_add_pred_mem_of_pow_mem {m n : ℕ} (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) : (a + b) ^ (m + n - 1) ∈ I := I.add_pow_add_pred_mem_of_pow_mem_of_commute ha hb (Commute.all ..) theorem pow_multiset_sum_mem_span_pow [DecidableEq α] (s : Multiset α) (n : ℕ) : s.sum ^ (Multiset.card s * n + 1) ∈ span ((s.map fun (x : α) ↦ x ^ (n + 1)).toFinset : Set α) := by induction' s using Multiset.induction_on with a s hs · simp simp only [Finset.coe_insert, Multiset.map_cons, Multiset.toFinset_cons, Multiset.sum_cons, Multiset.card_cons, add_pow] refine Submodule.sum_mem _ ?_ intro c _hc rw [mem_span_insert] by_cases h : n + 1 ≤ c · refine ⟨a ^ (c - (n + 1)) * s.sum ^ ((Multiset.card s + 1) * n + 1 - c) * ((Multiset.card s + 1) * n + 1).choose c, 0, Submodule.zero_mem _, ?_⟩ rw [mul_comm _ (a ^ (n + 1))] simp_rw [← mul_assoc] rw [← pow_add, add_zero, add_tsub_cancel_of_le h] · use 0 simp_rw [zero_mul, zero_add] refine ⟨_, ?_, rfl⟩ replace h : c ≤ n := Nat.lt_succ_iff.mp (not_le.mp h) have : (Multiset.card s + 1) * n + 1 - c = Multiset.card s * n + 1 + (n - c) := by rw [add_mul, one_mul, add_assoc, add_comm n 1, ← add_assoc, add_tsub_assoc_of_le h] rw [this, pow_add] simp_rw [mul_assoc, mul_comm (s.sum ^ (Multiset.card s * n + 1)), ← mul_assoc] exact mul_mem_left _ _ hs theorem sum_pow_mem_span_pow {ι} (s : Finset ι) (f : ι → α) (n : ℕ) : (∑ i ∈ s, f i) ^ (s.card * n + 1) ∈ span ((fun i => f i ^ (n + 1)) '' s) := by classical simpa only [Multiset.card_map, Multiset.map_map, comp_apply, Multiset.toFinset_map, Finset.coe_image, Finset.val_toFinset] using pow_multiset_sum_mem_span_pow (s.1.map f) n theorem span_pow_eq_top (s : Set α) (hs : span s = ⊤) (n : ℕ) : span ((fun (x : α) => x ^ n) '' s) = ⊤ := by rw [eq_top_iff_one] rcases n with - | n · obtain rfl | ⟨x, hx⟩ := eq_empty_or_nonempty s · rw [Set.image_empty, hs] trivial · exact subset_span ⟨_, hx, pow_zero _⟩ rw [eq_top_iff_one, span, Finsupp.mem_span_iff_linearCombination] at hs rcases hs with ⟨f, hf⟩ have hf : (f.support.sum fun a => f a * a) = 1 := hf -- Porting note: was `change ... at hf` have := sum_pow_mem_span_pow f.support (fun a => f a * a) n rw [hf, one_pow] at this refine span_le.mpr ?_ this rintro _ hx simp_rw [Set.mem_image] at hx rcases hx with ⟨x, _, rfl⟩ have : span ({(x : α) ^ (n + 1)} : Set α) ≤ span ((fun x : α => x ^ (n + 1)) '' s) := by rw [span_le, Set.singleton_subset_iff] exact subset_span ⟨x, x.prop, rfl⟩ refine this ?_ rw [mul_pow, mem_span_singleton] exact ⟨f x ^ (n + 1), mul_comm _ _⟩ theorem span_range_pow_eq_top (s : Set α) (hs : span s = ⊤) (n : s → ℕ) : span (Set.range fun x ↦ x.1 ^ n x) = ⊤ := by have ⟨t, hts, mem⟩ := Submodule.mem_span_finite_of_mem_span ((eq_top_iff_one _).mp hs) refine top_unique ((span_pow_eq_top _ ((eq_top_iff_one _).mpr mem) <| t.attach.sup fun x ↦ n ⟨x, hts x.2⟩).ge.trans <| span_le.mpr ?_) rintro _ ⟨x, hxt, rfl⟩ rw [← Nat.sub_add_cancel (Finset.le_sup <| t.mem_attach ⟨x, hxt⟩)] simp_rw [pow_add] exact mul_mem_left _ _ (subset_span ⟨_, rfl⟩) theorem prod_mem {ι : Type*} {f : ι → α} {s : Finset ι} (I : Ideal α) {i : ι} (hi : i ∈ s) (hfi : f i ∈ I) : ∏ i ∈ s, f i ∈ I := by classical rw [Finset.prod_eq_prod_diff_singleton_mul hi] exact Ideal.mul_mem_left _ _ hfi end Ideal end CommSemiring section DivisionSemiring variable {K : Type*} [DivisionSemiring K] (I : Ideal K) namespace Ideal variable (K) in /-- A bijection between (left) ideals of a division ring and `{0, 1}`, sending `⊥` to `0` and `⊤` to `1`. -/ def equivFinTwo [DecidableEq (Ideal K)] : Ideal K ≃ Fin 2 where toFun := fun I ↦ if I = ⊥ then 0 else 1 invFun := ![⊥, ⊤] left_inv := fun I ↦ by rcases eq_bot_or_top I with rfl | rfl <;> simp right_inv := fun i ↦ by fin_cases i <;> simp instance : Finite (Ideal K) := let _i := Classical.decEq (Ideal K); ⟨equivFinTwo K⟩ /-- Ideals of a `DivisionSemiring` are a simple order. Thanks to the way abbreviations work, this automatically gives an `IsSimpleModule K` instance. -/ instance isSimpleOrder : IsSimpleOrder (Ideal K) := ⟨eq_bot_or_top⟩ end Ideal end DivisionSemiring -- TODO: consider moving the lemmas below out of the `Ring` namespace since they are -- about `CommSemiring`s. namespace Ring variable {R : Type*} [CommSemiring R] theorem exists_not_isUnit_of_not_isField [Nontrivial R] (hf : ¬IsField R) : ∃ (x : R) (_hx : x ≠ (0 : R)), ¬IsUnit x := by have : ¬_ := fun h => hf ⟨exists_pair_ne R, mul_comm, h⟩ simp_rw [isUnit_iff_exists_inv] push_neg at this ⊢ obtain ⟨x, hx, not_unit⟩ := this exact ⟨x, hx, not_unit⟩ theorem not_isField_iff_exists_ideal_bot_lt_and_lt_top [Nontrivial R] : ¬IsField R ↔ ∃ I : Ideal R, ⊥ < I ∧ I < ⊤ := by constructor · intro h obtain ⟨x, nz, nu⟩ := exists_not_isUnit_of_not_isField h use Ideal.span {x} rw [bot_lt_iff_ne_bot, lt_top_iff_ne_top] exact ⟨mt Ideal.span_singleton_eq_bot.mp nz, mt Ideal.span_singleton_eq_top.mp nu⟩ · rintro ⟨I, bot_lt, lt_top⟩ hf obtain ⟨x, mem, ne_zero⟩ := SetLike.exists_of_lt bot_lt rw [Submodule.mem_bot] at ne_zero obtain ⟨y, hy⟩ := hf.mul_inv_cancel ne_zero rw [lt_top_iff_ne_top, Ne, Ideal.eq_top_iff_one, ← hy] at lt_top exact lt_top (I.mul_mem_right _ mem) theorem not_isField_iff_exists_prime [Nontrivial R] : ¬IsField R ↔ ∃ p : Ideal R, p ≠ ⊥ ∧ p.IsPrime := not_isField_iff_exists_ideal_bot_lt_and_lt_top.trans ⟨fun ⟨I, bot_lt, lt_top⟩ => let ⟨p, hp, le_p⟩ := I.exists_le_maximal (lt_top_iff_ne_top.mp lt_top) ⟨p, bot_lt_iff_ne_bot.mp (lt_of_lt_of_le bot_lt le_p), hp.isPrime⟩, fun ⟨p, ne_bot, Prime⟩ => ⟨p, bot_lt_iff_ne_bot.mpr ne_bot, lt_top_iff_ne_top.mpr Prime.1⟩⟩ /-- Also see `Ideal.isSimpleOrder` for the forward direction as an instance when `R` is a division (semi)ring. This result actually holds for all division semirings, but we lack the predicate to state it. -/ theorem isField_iff_isSimpleOrder_ideal : IsField R ↔ IsSimpleOrder (Ideal R) := by cases subsingleton_or_nontrivial R · exact ⟨fun h => (not_isField_of_subsingleton _ h).elim, fun h => (false_of_nontrivial_of_subsingleton <| Ideal R).elim⟩ rw [← not_iff_not, Ring.not_isField_iff_exists_ideal_bot_lt_and_lt_top, ← not_iff_not] push_neg simp_rw [lt_top_iff_ne_top, bot_lt_iff_ne_bot, ← or_iff_not_imp_left, not_ne_iff] exact ⟨fun h => ⟨h⟩, fun h => h.2⟩ /-- When a ring is not a field, the maximal ideals are nontrivial. -/ theorem ne_bot_of_isMaximal_of_not_isField [Nontrivial R] {M : Ideal R} (max : M.IsMaximal) (not_field : ¬IsField R) : M ≠ ⊥ := by rintro h rw [h] at max rcases max with ⟨⟨_h1, h2⟩⟩ obtain ⟨I, hIbot, hItop⟩ := not_isField_iff_exists_ideal_bot_lt_and_lt_top.mp not_field exact ne_of_lt hItop (h2 I hIbot) end Ring namespace Ideal variable {R : Type*} [CommSemiring R] [Nontrivial R] theorem bot_lt_of_maximal (M : Ideal R) [hm : M.IsMaximal] (non_field : ¬IsField R) : ⊥ < M := by rcases Ring.not_isField_iff_exists_ideal_bot_lt_and_lt_top.1 non_field with ⟨I, Ibot, Itop⟩ constructor; · simp intro mle apply lt_irrefl (⊤ : Ideal R) have : M = ⊥ := eq_bot_iff.mpr mle rw [← this] at Ibot rwa [hm.1.2 I Ibot] at Itop end Ideal
Mathlib/RingTheory/Ideal/Basic.lean
366
367
/- 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.Support import Mathlib.Algebra.Polynomial.Basic import Mathlib.Data.Nat.Choose.Sum import Mathlib.Algebra.CharP.Defs /-! # Theory of univariate polynomials The theorems include formulas for computing coefficients, such as `coeff_add`, `coeff_sum`, `coeff_mul` -/ noncomputable section open Finsupp Finset AddMonoidAlgebra open Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b : R} {n m : ℕ} variable [Semiring R] {p q r : R[X]} section Coeff @[simp] theorem coeff_add (p q : R[X]) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := by rcases p with ⟨⟩ rcases q with ⟨⟩ simp_rw [← ofFinsupp_add, coeff] exact Finsupp.add_apply _ _ _ @[simp] theorem coeff_smul [SMulZeroClass S R] (r : S) (p : R[X]) (n : ℕ) : coeff (r • p) n = r • coeff p n := by rcases p with ⟨⟩ simp_rw [← ofFinsupp_smul, coeff] exact Finsupp.smul_apply _ _ _ theorem support_smul [SMulZeroClass S R] (r : S) (p : R[X]) : support (r • p) ⊆ support p := by intro i hi simp? [mem_support_iff] at hi ⊢ says simp only [mem_support_iff, coeff_smul, ne_eq] at hi ⊢ contrapose! hi simp [hi] open scoped Pointwise in theorem card_support_mul_le : #(p * q).support ≤ #p.support * #q.support := by calc #(p * q).support _ = #(p.toFinsupp * q.toFinsupp).support := by rw [← support_toFinsupp, toFinsupp_mul] _ ≤ #(p.toFinsupp.support + q.toFinsupp.support) := Finset.card_le_card (AddMonoidAlgebra.support_mul p.toFinsupp q.toFinsupp) _ ≤ #p.support * #q.support := Finset.card_image₂_le .. /-- `Polynomial.sum` as a linear map. -/ @[simps] def lsum {R A M : Type*} [Semiring R] [Semiring A] [AddCommMonoid M] [Module R A] [Module R M] (f : ℕ → A →ₗ[R] M) : A[X] →ₗ[R] M where toFun p := p.sum (f · ·) map_add' p q := sum_add_index p q _ (fun n => (f n).map_zero) fun n _ _ => (f n).map_add _ _ map_smul' c p := by rw [sum_eq_of_subset (f · ·) (fun n => (f n).map_zero) (support_smul c p)] simp only [sum_def, Finset.smul_sum, coeff_smul, LinearMap.map_smul, RingHom.id_apply] variable (R) in /-- The nth coefficient, as a linear map. -/ def lcoeff (n : ℕ) : R[X] →ₗ[R] R where toFun p := coeff p n map_add' p q := coeff_add p q n map_smul' r p := coeff_smul r p n @[simp] theorem lcoeff_apply (n : ℕ) (f : R[X]) : lcoeff R n f = coeff f n := rfl @[simp] theorem finset_sum_coeff {ι : Type*} (s : Finset ι) (f : ι → R[X]) (n : ℕ) : coeff (∑ b ∈ s, f b) n = ∑ b ∈ s, coeff (f b) n := map_sum (lcoeff R n) _ _ lemma coeff_list_sum (l : List R[X]) (n : ℕ) : l.sum.coeff n = (l.map (lcoeff R n)).sum := map_list_sum (lcoeff R n) _ lemma coeff_list_sum_map {ι : Type*} (l : List ι) (f : ι → R[X]) (n : ℕ) : (l.map f).sum.coeff n = (l.map (fun a => (f a).coeff n)).sum := by simp_rw [coeff_list_sum, List.map_map, Function.comp_def, lcoeff_apply] @[simp] theorem coeff_sum [Semiring S] (n : ℕ) (f : ℕ → R → S[X]) : coeff (p.sum f) n = p.sum fun a b => coeff (f a b) n := by rcases p with ⟨⟩ simp [Polynomial.sum, support_ofFinsupp, coeff_ofFinsupp] /-- Decomposes the coefficient of the product `p * q` as a sum over `antidiagonal`. A version which sums over `range (n + 1)` can be obtained by using `Finset.Nat.sum_antidiagonal_eq_sum_range_succ`. -/ theorem coeff_mul (p q : R[X]) (n : ℕ) : coeff (p * q) n = ∑ x ∈ antidiagonal n, coeff p x.1 * coeff q x.2 := by rcases p with ⟨p⟩; rcases q with ⟨q⟩ simp_rw [← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.mul_apply_antidiagonal p q n _ Finset.mem_antidiagonal @[simp] theorem mul_coeff_zero (p q : R[X]) : coeff (p * q) 0 = coeff p 0 * coeff q 0 := by simp [coeff_mul] theorem mul_coeff_one (p q : R[X]) : coeff (p * q) 1 = coeff p 0 * coeff q 1 + coeff p 1 * coeff q 0 := by rw [coeff_mul, Nat.antidiagonal_eq_map] simp [sum_range_succ] /-- `constantCoeff p` returns the constant term of the polynomial `p`, defined as `coeff p 0`. This is a ring homomorphism. -/ @[simps] def constantCoeff : R[X] →+* R where toFun p := coeff p 0 map_one' := coeff_one_zero map_mul' := mul_coeff_zero map_zero' := coeff_zero 0 map_add' p q := coeff_add p q 0 theorem isUnit_C {x : R} : IsUnit (C x) ↔ IsUnit x := ⟨fun h => (congr_arg IsUnit coeff_C_zero).mp (h.map <| @constantCoeff R _), fun h => h.map C⟩ theorem coeff_mul_X_zero (p : R[X]) : coeff (p * X) 0 = 0 := by simp theorem coeff_X_mul_zero (p : R[X]) : coeff (X * p) 0 = 0 := by simp theorem coeff_C_mul_X_pow (x : R) (k n : ℕ) : coeff (C x * X ^ k : R[X]) n = if n = k then x else 0 := by rw [C_mul_X_pow_eq_monomial, coeff_monomial] congr 1 simp [eq_comm] theorem coeff_C_mul_X (x : R) (n : ℕ) : coeff (C x * X : R[X]) n = if n = 1 then x else 0 := by rw [← pow_one X, coeff_C_mul_X_pow] @[simp] theorem coeff_C_mul (p : R[X]) : coeff (C a * p) n = a * coeff p n := by rcases p with ⟨p⟩ simp_rw [← monomial_zero_left, ← ofFinsupp_single, ← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.single_zero_mul_apply p a n theorem C_mul' (a : R) (f : R[X]) : C a * f = a • f := by ext rw [coeff_C_mul, coeff_smul, smul_eq_mul] @[simp] theorem coeff_mul_C (p : R[X]) (n : ℕ) (a : R) : coeff (p * C a) n = coeff p n * a := by rcases p with ⟨p⟩ simp_rw [← monomial_zero_left, ← ofFinsupp_single, ← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.mul_single_zero_apply p a n @[simp] lemma coeff_mul_natCast {a k : ℕ} : coeff (p * (a : R[X])) k = coeff p k * (↑a : R) := coeff_mul_C _ _ _ @[simp] lemma coeff_natCast_mul {a k : ℕ} : coeff ((a : R[X]) * p) k = a * coeff p k := coeff_C_mul _ @[simp] lemma coeff_mul_ofNat {a k : ℕ} [Nat.AtLeastTwo a] : coeff (p * (ofNat(a) : R[X])) k = coeff p k * ofNat(a) := coeff_mul_C _ _ _ @[simp] lemma coeff_ofNat_mul {a k : ℕ} [Nat.AtLeastTwo a] : coeff ((ofNat(a) : R[X]) * p) k = ofNat(a) * coeff p k := coeff_C_mul _ @[simp] lemma coeff_mul_intCast [Ring S] {p : S[X]} {a : ℤ} {k : ℕ} : coeff (p * (a : S[X])) k = coeff p k * (↑a : S) := coeff_mul_C _ _ _ @[simp] lemma coeff_intCast_mul [Ring S] {p : S[X]} {a : ℤ} {k : ℕ} : coeff ((a : S[X]) * p) k = a * coeff p k := coeff_C_mul _ @[simp] theorem coeff_X_pow (k n : ℕ) : coeff (X ^ k : R[X]) n = if n = k then 1 else 0 := by simp only [one_mul, RingHom.map_one, ← coeff_C_mul_X_pow] theorem coeff_X_pow_self (n : ℕ) : coeff (X ^ n : R[X]) n = 1 := by simp section Fewnomials open Finset theorem support_binomial {k m : ℕ} (hkm : k ≠ m) {x y : R} (hx : x ≠ 0) (hy : y ≠ 0) : support (C x * X ^ k + C y * X ^ m) = {k, m} := by apply subset_antisymm (support_binomial' k m x y) simp_rw [insert_subset_iff, singleton_subset_iff, mem_support_iff, coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow, if_neg hkm, if_neg hkm.symm, mul_zero, zero_add, add_zero, Ne, hx, hy, not_false_eq_true, and_true] theorem support_trinomial {k m n : ℕ} (hkm : k < m) (hmn : m < n) {x y z : R} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : support (C x * X ^ k + C y * X ^ m + C z * X ^ n) = {k, m, n} := by apply subset_antisymm (support_trinomial' k m n x y z) simp_rw [insert_subset_iff, singleton_subset_iff, mem_support_iff, coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow, if_neg hkm.ne, if_neg hkm.ne', if_neg hmn.ne, if_neg hmn.ne', if_neg (hkm.trans hmn).ne, if_neg (hkm.trans hmn).ne', mul_zero, add_zero, zero_add, Ne, hx, hy, hz, not_false_eq_true, and_true] theorem card_support_binomial {k m : ℕ} (h : k ≠ m) {x y : R} (hx : x ≠ 0) (hy : y ≠ 0) : #(support (C x * X ^ k + C y * X ^ m)) = 2 := by rw [support_binomial h hx hy, card_insert_of_not_mem (mt mem_singleton.mp h), card_singleton] theorem card_support_trinomial {k m n : ℕ} (hkm : k < m) (hmn : m < n) {x y z : R} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : #(support (C x * X ^ k + C y * X ^ m + C z * X ^ n)) = 3 := by rw [support_trinomial hkm hmn hx hy hz, card_insert_of_not_mem (mt mem_insert.mp (not_or_intro hkm.ne (mt mem_singleton.mp (hkm.trans hmn).ne))), card_insert_of_not_mem (mt mem_singleton.mp hmn.ne), card_singleton] end Fewnomials @[simp] theorem coeff_mul_X_pow (p : R[X]) (n d : ℕ) : coeff (p * Polynomial.X ^ n) (d + n) = coeff p d := by rw [coeff_mul, Finset.sum_eq_single (d, n), coeff_X_pow, if_pos rfl, mul_one] · rintro ⟨i, j⟩ h1 h2 rw [coeff_X_pow, if_neg, mul_zero] rintro rfl apply h2 rw [mem_antidiagonal, add_right_cancel_iff] at h1
subst h1 rfl · exact fun h1 => (h1 (mem_antidiagonal.2 rfl)).elim @[simp] theorem coeff_X_pow_mul (p : R[X]) (n d : ℕ) : coeff (Polynomial.X ^ n * p) (d + n) = coeff p d := by rw [(commute_X_pow p n).eq, coeff_mul_X_pow]
Mathlib/Algebra/Polynomial/Coeff.lean
230
237
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import Mathlib.Data.PFunctor.Univariate.M /-! # Quotients of Polynomial Functors We assume the following: * `P`: a polynomial functor * `W`: its W-type * `M`: its M-type * `F`: a functor We define: * `q`: `QPF` data, representing `F` as a quotient of `P` The main goal is to construct: * `Fix`: the initial algebra with structure map `F Fix → Fix`. * `Cofix`: the final coalgebra with structure map `Cofix → F Cofix` We also show that the composition of qpfs is a qpf, and that the quotient of a qpf is a qpf. The present theory focuses on the univariate case for qpfs ## References * [Jeremy Avigad, Mario M. Carneiro and Simon Hudon, *Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universe u /-- Quotients of polynomial functors. Roughly speaking, saying that `F` is a quotient of a polynomial functor means that for each `α`, elements of `F α` are represented by pairs `⟨a, f⟩`, where `a` is the shape of the object and `f` indexes the relevant elements of `α`, in a suitably natural manner. -/ class QPF (F : Type u → Type u) extends Functor F where P : PFunctor.{u} abs : ∀ {α}, P α → F α repr : ∀ {α}, F α → P α abs_repr : ∀ {α} (x : F α), abs (repr x) = x abs_map : ∀ {α β} (f : α → β) (p : P α), abs (P.map f p) = f <$> abs p namespace QPF variable {F : Type u → Type u} [q : QPF F] open Functor (Liftp Liftr) /- Show that every qpf is a lawful functor. Note: every functor has a field, `map_const`, and `lawfulFunctor` has the defining characterization. We can only propagate the assumption. -/ theorem id_map {α : Type _} (x : F α) : id <$> x = x := by rw [← abs_repr x] obtain ⟨a, f⟩ := repr x rw [← abs_map] rfl theorem comp_map {α β γ : Type _} (f : α → β) (g : β → γ) (x : F α) : (g ∘ f) <$> x = g <$> f <$> x := by rw [← abs_repr x] obtain ⟨a, f⟩ := repr x rw [← abs_map, ← abs_map, ← abs_map] rfl theorem lawfulFunctor (h : ∀ α β : Type u, @Functor.mapConst F _ α _ = Functor.map ∘ Function.const β) : LawfulFunctor F := { map_const := @h id_map := @id_map F _ comp_map := @comp_map F _ } /- Lifting predicates and relations -/ section open Functor theorem liftp_iff {α : Type u} (p : α → Prop) (x : F α) : Liftp p x ↔ ∃ a f, x = abs ⟨a, f⟩ ∧ ∀ i, p (f i) := by constructor · rintro ⟨y, hy⟩ rcases h : repr y with ⟨a, f⟩ use a, fun i => (f i).val constructor · rw [← hy, ← abs_repr y, h, ← abs_map] rfl intro i apply (f i).property rintro ⟨a, f, h₀, h₁⟩ use abs ⟨a, fun i => ⟨f i, h₁ i⟩⟩ rw [← abs_map, h₀]; rfl theorem liftp_iff' {α : Type u} (p : α → Prop) (x : F α) : Liftp p x ↔ ∃ u : q.P α, abs u = x ∧ ∀ i, p (u.snd i) := by constructor · rintro ⟨y, hy⟩ rcases h : repr y with ⟨a, f⟩ use ⟨a, fun i => (f i).val⟩ dsimp constructor · rw [← hy, ← abs_repr y, h, ← abs_map] rfl intro i apply (f i).property rintro ⟨⟨a, f⟩, h₀, h₁⟩; dsimp at * use abs ⟨a, fun i => ⟨f i, h₁ i⟩⟩ rw [← abs_map, ← h₀]; rfl theorem liftr_iff {α : Type u} (r : α → α → Prop) (x y : F α) : Liftr r x y ↔ ∃ a f₀ f₁, x = abs ⟨a, f₀⟩ ∧ y = abs ⟨a, f₁⟩ ∧ ∀ i, r (f₀ i) (f₁ i) := by constructor · rintro ⟨u, xeq, yeq⟩ rcases h : repr u with ⟨a, f⟩ use a, fun i => (f i).val.fst, fun i => (f i).val.snd constructor · rw [← xeq, ← abs_repr u, h, ← abs_map] rfl constructor · rw [← yeq, ← abs_repr u, h, ← abs_map] rfl intro i exact (f i).property rintro ⟨a, f₀, f₁, xeq, yeq, h⟩ use abs ⟨a, fun i => ⟨(f₀ i, f₁ i), h i⟩⟩ constructor · rw [xeq, ← abs_map] rfl rw [yeq, ← abs_map]; rfl end /- Think of trees in the `W` type corresponding to `P` as representatives of elements of the least fixed point of `F`, and assign a canonical representative to each equivalence class of trees. -/ /-- does recursion on `q.P.W` using `g : F α → α` rather than `g : P α → α` -/ def recF {α : Type _} (g : F α → α) : q.P.W → α | ⟨a, f⟩ => g (abs ⟨a, fun x => recF g (f x)⟩) theorem recF_eq {α : Type _} (g : F α → α) (x : q.P.W) : recF g x = g (abs (q.P.map (recF g) x.dest)) := by cases x rfl theorem recF_eq' {α : Type _} (g : F α → α) (a : q.P.A) (f : q.P.B a → q.P.W) : recF g ⟨a, f⟩ = g (abs (q.P.map (recF g) ⟨a, f⟩)) := rfl /-- two trees are equivalent if their F-abstractions are -/ inductive Wequiv : q.P.W → q.P.W → Prop | ind (a : q.P.A) (f f' : q.P.B a → q.P.W) : (∀ x, Wequiv (f x) (f' x)) → Wequiv ⟨a, f⟩ ⟨a, f'⟩ | abs (a : q.P.A) (f : q.P.B a → q.P.W) (a' : q.P.A) (f' : q.P.B a' → q.P.W) : abs ⟨a, f⟩ = abs ⟨a', f'⟩ → Wequiv ⟨a, f⟩ ⟨a', f'⟩ | trans (u v w : q.P.W) : Wequiv u v → Wequiv v w → Wequiv u w /-- `recF` is insensitive to the representation -/ theorem recF_eq_of_Wequiv {α : Type u} (u : F α → α) (x y : q.P.W) : Wequiv x y → recF u x = recF u y := by intro h induction h with | ind a f f' _ ih => simp only [recF_eq', PFunctor.map_eq, Function.comp_def, ih] | abs a f a' f' h => simp only [recF_eq', abs_map, h] | trans x y z _ _ ih₁ ih₂ => exact Eq.trans ih₁ ih₂ theorem Wequiv.abs' (x y : q.P.W) (h : QPF.abs x.dest = QPF.abs y.dest) : Wequiv x y := by cases x cases y apply Wequiv.abs apply h theorem Wequiv.refl (x : q.P.W) : Wequiv x x := by obtain ⟨a, f⟩ := x exact Wequiv.abs a f a f rfl theorem Wequiv.symm (x y : q.P.W) : Wequiv x y → Wequiv y x := by intro h induction h with | ind a f f' _ ih => exact Wequiv.ind _ _ _ ih | abs a f a' f' h => exact Wequiv.abs _ _ _ _ h.symm | trans x y z _ _ ih₁ ih₂ => exact QPF.Wequiv.trans _ _ _ ih₂ ih₁ /-- maps every element of the W type to a canonical representative -/ def Wrepr : q.P.W → q.P.W := recF (PFunctor.W.mk ∘ repr) theorem Wrepr_equiv (x : q.P.W) : Wequiv (Wrepr x) x := by induction' x with a f ih apply Wequiv.trans · change Wequiv (Wrepr ⟨a, f⟩) (PFunctor.W.mk (q.P.map Wrepr ⟨a, f⟩)) apply Wequiv.abs' have : Wrepr ⟨a, f⟩ = PFunctor.W.mk (repr (abs (q.P.map Wrepr ⟨a, f⟩))) := rfl rw [this, PFunctor.W.dest_mk, abs_repr] rfl apply Wequiv.ind; exact ih /-- Define the fixed point as the quotient of trees under the equivalence relation `Wequiv`. -/ def Wsetoid : Setoid q.P.W := ⟨Wequiv, @Wequiv.refl _ _, @Wequiv.symm _ _, @Wequiv.trans _ _⟩ attribute [local instance] Wsetoid /-- inductive type defined as initial algebra of a Quotient of Polynomial Functor -/ def Fix (F : Type u → Type u) [q : QPF F] := Quotient (Wsetoid : Setoid q.P.W) /-- recursor of a type defined by a qpf -/ def Fix.rec {α : Type _} (g : F α → α) : Fix F → α := Quot.lift (recF g) (recF_eq_of_Wequiv g) /-- access the underlying W-type of a fixpoint data type -/ def fixToW : Fix F → q.P.W := Quotient.lift Wrepr (recF_eq_of_Wequiv fun x => @PFunctor.W.mk q.P (repr x)) /-- constructor of a type defined by a qpf -/ def Fix.mk (x : F (Fix F)) : Fix F := Quot.mk _ (PFunctor.W.mk (q.P.map fixToW (repr x))) /-- destructor of a type defined by a qpf -/ def Fix.dest : Fix F → F (Fix F) := Fix.rec (Functor.map Fix.mk) theorem Fix.rec_eq {α : Type _} (g : F α → α) (x : F (Fix F)) : Fix.rec g (Fix.mk x) = g (Fix.rec g <$> x) := by have : recF g ∘ fixToW = Fix.rec g := by ext ⟨x⟩ apply recF_eq_of_Wequiv rw [fixToW] apply Wrepr_equiv conv => lhs rw [Fix.rec, Fix.mk] dsimp rcases h : repr x with ⟨a, f⟩ rw [PFunctor.map_eq, recF_eq, ← PFunctor.map_eq, PFunctor.W.dest_mk, PFunctor.map_map, abs_map, ← h, abs_repr, this] theorem Fix.ind_aux (a : q.P.A) (f : q.P.B a → q.P.W) : Fix.mk (abs ⟨a, fun x => ⟦f x⟧⟩) = ⟦⟨a, f⟩⟧ := by have : Fix.mk (abs ⟨a, fun x => ⟦f x⟧⟩) = ⟦Wrepr ⟨a, f⟩⟧ := by apply Quot.sound; apply Wequiv.abs' rw [PFunctor.W.dest_mk, abs_map, abs_repr, ← abs_map, PFunctor.map_eq] simp only [Wrepr, recF_eq, PFunctor.W.dest_mk, abs_repr, Function.comp] rfl rw [this] apply Quot.sound apply Wrepr_equiv theorem Fix.ind_rec {α : Type u} (g₁ g₂ : Fix F → α) (h : ∀ x : F (Fix F), g₁ <$> x = g₂ <$> x → g₁ (Fix.mk x) = g₂ (Fix.mk x)) : ∀ x, g₁ x = g₂ x := by rintro ⟨x⟩ induction' x with a f ih change g₁ ⟦⟨a, f⟩⟧ = g₂ ⟦⟨a, f⟩⟧ rw [← Fix.ind_aux a f]; apply h rw [← abs_map, ← abs_map, PFunctor.map_eq, PFunctor.map_eq] congr with x apply ih theorem Fix.rec_unique {α : Type u} (g : F α → α) (h : Fix F → α) (hyp : ∀ x, h (Fix.mk x) = g (h <$> x)) : Fix.rec g = h := by ext x apply Fix.ind_rec intro x hyp' rw [hyp, ← hyp', Fix.rec_eq] theorem Fix.mk_dest (x : Fix F) : Fix.mk (Fix.dest x) = x := by change (Fix.mk ∘ Fix.dest) x = id x apply Fix.ind_rec (mk ∘ dest) id intro x rw [Function.comp_apply, id_eq, Fix.dest, Fix.rec_eq, id_map, comp_map] intro h rw [h] theorem Fix.dest_mk (x : F (Fix F)) : Fix.dest (Fix.mk x) = x := by unfold Fix.dest; rw [Fix.rec_eq, ← Fix.dest, ← comp_map] conv => rhs rw [← id_map x] congr with x apply Fix.mk_dest theorem Fix.ind (p : Fix F → Prop) (h : ∀ x : F (Fix F), Liftp p x → p (Fix.mk x)) : ∀ x, p x := by rintro ⟨x⟩ induction' x with a f ih change p ⟦⟨a, f⟩⟧ rw [← Fix.ind_aux a f] apply h rw [liftp_iff] refine ⟨_, _, rfl, ?_⟩ convert ih end QPF /- Construct the final coalgebra to a qpf. -/ namespace QPF variable {F : Type u → Type u} [q : QPF F] open Functor (Liftp Liftr) /-- does recursion on `q.P.M` using `g : α → F α` rather than `g : α → P α` -/ def corecF {α : Type _} (g : α → F α) : α → q.P.M := PFunctor.M.corec fun x => repr (g x) theorem corecF_eq {α : Type _} (g : α → F α) (x : α) : PFunctor.M.dest (corecF g x) = q.P.map (corecF g) (repr (g x)) := by rw [corecF, PFunctor.M.dest_corec] -- Equivalence /-- A pre-congruence on `q.P.M` *viewed as an F-coalgebra*. Not necessarily symmetric. -/ def IsPrecongr (r : q.P.M → q.P.M → Prop) : Prop := ∀ ⦃x y⦄, r x y → abs (q.P.map (Quot.mk r) (PFunctor.M.dest x)) = abs (q.P.map (Quot.mk r) (PFunctor.M.dest y)) /-- The maximal congruence on `q.P.M`. -/ def Mcongr : q.P.M → q.P.M → Prop := fun x y => ∃ r, IsPrecongr r ∧ r x y /-- coinductive type defined as the final coalgebra of a qpf -/ def Cofix (F : Type u → Type u) [q : QPF F] := Quot (@Mcongr F q) instance [Inhabited q.P.A] : Inhabited (Cofix F) := ⟨Quot.mk _ default⟩ /-- corecursor for type defined by `Cofix` -/ def Cofix.corec {α : Type _} (g : α → F α) (x : α) : Cofix F := Quot.mk _ (corecF g x) /-- destructor for type defined by `Cofix` -/ def Cofix.dest : Cofix F → F (Cofix F) := Quot.lift (fun x => Quot.mk Mcongr <$> abs (PFunctor.M.dest x)) (by rintro x y ⟨r, pr, rxy⟩ dsimp have : ∀ x y, r x y → Mcongr x y := by intro x y h exact ⟨r, pr, h⟩ rw [← Quot.factor_mk_eq _ _ this] conv => lhs rw [comp_map, ← abs_map, pr rxy, abs_map, ← comp_map]) theorem Cofix.dest_corec {α : Type u} (g : α → F α) (x : α) : Cofix.dest (Cofix.corec g x) = Cofix.corec g <$> g x := by conv => lhs rw [Cofix.dest, Cofix.corec] dsimp rw [corecF_eq, abs_map, abs_repr, ← comp_map]; rfl private theorem Cofix.bisim_aux (r : Cofix F → Cofix F → Prop) (h' : ∀ x, r x x) (h : ∀ x y, r x y → Quot.mk r <$> Cofix.dest x = Quot.mk r <$> Cofix.dest y) : ∀ x y, r x y → x = y := by rintro ⟨x⟩ ⟨y⟩ rxy apply Quot.sound let r' x y := r (Quot.mk _ x) (Quot.mk _ y) have : IsPrecongr r' := by intro a b r'ab have h₀ : Quot.mk r <$> Quot.mk Mcongr <$> abs (PFunctor.M.dest a) = Quot.mk r <$> Quot.mk Mcongr <$> abs (PFunctor.M.dest b) := h _ _ r'ab have h₁ : ∀ u v : q.P.M, Mcongr u v → Quot.mk r' u = Quot.mk r' v := by intro u v cuv apply Quot.sound simp only [r'] rw [Quot.sound cuv] apply h' let f : Quot r → Quot r' := Quot.lift (Quot.lift (Quot.mk r') h₁) <| by rintro ⟨c⟩ ⟨d⟩ rcd exact Quot.sound rcd have : f ∘ Quot.mk r ∘ Quot.mk Mcongr = Quot.mk r' := rfl rw [← this, ← PFunctor.map_map _ _ f, ← PFunctor.map_map _ _ (Quot.mk r), abs_map, abs_map, abs_map, h₀] rw [← PFunctor.map_map _ _ f, ← PFunctor.map_map _ _ (Quot.mk r), abs_map, abs_map, abs_map] exact ⟨r', this, rxy⟩ theorem Cofix.bisim_rel (r : Cofix F → Cofix F → Prop) (h : ∀ x y, r x y → Quot.mk r <$> Cofix.dest x = Quot.mk r <$> Cofix.dest y) : ∀ x y, r x y → x = y := by let r' (x y) := x = y ∨ r x y intro x y rxy apply Cofix.bisim_aux r' · intro x left rfl · intro x y r'xy rcases r'xy with r'xy | r'xy · rw [r'xy] have : ∀ x y, r x y → r' x y := fun x y h => Or.inr h rw [← Quot.factor_mk_eq _ _ this] dsimp [r'] rw [@comp_map _ q _ _ _ (Quot.mk r), @comp_map _ q _ _ _ (Quot.mk r)] rw [h _ _ r'xy] right; exact rxy theorem Cofix.bisim (r : Cofix F → Cofix F → Prop) (h : ∀ x y, r x y → Liftr r (Cofix.dest x) (Cofix.dest y)) : ∀ x y, r x y → x = y := by apply Cofix.bisim_rel intro x y rxy rcases (liftr_iff r _ _).mp (h x y rxy) with ⟨a, f₀, f₁, dxeq, dyeq, h'⟩ rw [dxeq, dyeq, ← abs_map, ← abs_map, PFunctor.map_eq, PFunctor.map_eq] congr 2 with i apply Quot.sound apply h' theorem Cofix.bisim' {α : Type*} (Q : α → Prop) (u v : α → Cofix F) (h : ∀ x, Q x → ∃ a f f', Cofix.dest (u x) = abs ⟨a, f⟩ ∧ Cofix.dest (v x) = abs ⟨a, f'⟩ ∧ ∀ i, ∃ x', Q x' ∧ f i = u x' ∧ f' i = v x') : ∀ x, Q x → u x = v x := fun x Qx => let R := fun w z : Cofix F => ∃ x', Q x' ∧ w = u x' ∧ z = v x'
Cofix.bisim R (fun x y ⟨x', Qx', xeq, yeq⟩ => by rcases h x' Qx' with ⟨a, f, f', ux'eq, vx'eq, h'⟩ rw [liftr_iff] exact ⟨a, f, f', xeq.symm ▸ ux'eq, yeq.symm ▸ vx'eq, h'⟩) _ _ ⟨x, Qx, rfl, rfl⟩ end QPF /- Composition of qpfs. -/ namespace QPF variable {F₂ : Type u → Type u} [q₂ : QPF F₂] variable {F₁ : Type u → Type u} [q₁ : QPF F₁] /-- composition of qpfs gives another qpf -/ def comp : QPF (Functor.Comp F₂ F₁) where P := PFunctor.comp q₂.P q₁.P abs {α} := by dsimp [Functor.Comp] intro p exact abs ⟨p.1.1, fun x => abs ⟨p.1.2 x, fun y => p.2 ⟨x, y⟩⟩⟩ repr {α} := by dsimp [Functor.Comp] intro y refine ⟨⟨(repr y).1, fun u => (repr ((repr y).2 u)).1⟩, ?_⟩ dsimp [PFunctor.comp] intro x exact (repr ((repr y).2 x.1)).snd x.2 abs_repr {α} := by dsimp [Functor.Comp] intro x conv =>
Mathlib/Data/QPF/Univariate/Basic.lean
433
467
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import Mathlib.Algebra.Group.Commute.Units import Mathlib.Algebra.Group.Invertible.Defs import Mathlib.Algebra.Group.Hom.Defs import Mathlib.Logic.Equiv.Defs /-! # Theorems about invertible elements -/ assert_not_exists MonoidWithZero DenselyOrdered universe u variable {α : Type u} /-- An `Invertible` element is a unit. -/ @[simps] def unitOfInvertible [Monoid α] (a : α) [Invertible a] : αˣ where val := a inv := ⅟ a val_inv := by simp inv_val := by simp theorem isUnit_of_invertible [Monoid α] (a : α) [Invertible a] : IsUnit a := ⟨unitOfInvertible a, rfl⟩ /-- Units are invertible in their associated monoid. -/ def Units.invertible [Monoid α] (u : αˣ) : Invertible (u : α) where invOf := ↑u⁻¹ invOf_mul_self := u.inv_mul mul_invOf_self := u.mul_inv @[simp] theorem invOf_units [Monoid α] (u : αˣ) [Invertible (u : α)] : ⅟ (u : α) = ↑u⁻¹ := invOf_eq_right_inv u.mul_inv theorem IsUnit.nonempty_invertible [Monoid α] {a : α} (h : IsUnit a) : Nonempty (Invertible a) := let ⟨x, hx⟩ := h ⟨x.invertible.copy _ hx.symm⟩ /-- Convert `IsUnit` to `Invertible` using `Classical.choice`. Prefer `casesI h.nonempty_invertible` over `letI := h.invertible` if you want to avoid choice. -/ noncomputable def IsUnit.invertible [Monoid α] {a : α} (h : IsUnit a) : Invertible a := Classical.choice h.nonempty_invertible @[simp] theorem nonempty_invertible_iff_isUnit [Monoid α] (a : α) : Nonempty (Invertible a) ↔ IsUnit a := ⟨Nonempty.rec <| @isUnit_of_invertible _ _ _, IsUnit.nonempty_invertible⟩ theorem Commute.invOf_right [Monoid α] {a b : α} [Invertible b] (h : Commute a b) : Commute a (⅟ b) := calc a * ⅟ b = ⅟ b * (b * a * ⅟ b) := by simp [mul_assoc] _ = ⅟ b * (a * b * ⅟ b) := by rw [h.eq] _ = ⅟ b * a := by simp [mul_assoc] theorem Commute.invOf_left [Monoid α] {a b : α} [Invertible b] (h : Commute b a) : Commute (⅟ b) a := calc ⅟ b * a = ⅟ b * (a * b * ⅟ b) := by simp [mul_assoc] _ = ⅟ b * (b * a * ⅟ b) := by rw [h.eq] _ = a * ⅟ b := by simp [mul_assoc] theorem commute_invOf {M : Type*} [One M] [Mul M] (m : M) [Invertible m] : Commute m (⅟ m) := calc m * ⅟ m = 1 := mul_invOf_self m _ = ⅟ m * m := (invOf_mul_self m).symm section Monoid variable [Monoid α] /-- This is the `Invertible` version of `Units.isUnit_units_mul` -/ abbrev invertibleOfInvertibleMul (a b : α) [Invertible a] [Invertible (a * b)] : Invertible b where invOf := ⅟ (a * b) * a invOf_mul_self := by rw [mul_assoc, invOf_mul_self] mul_invOf_self := by rw [← (isUnit_of_invertible a).mul_right_inj, ← mul_assoc, ← mul_assoc, mul_invOf_self, mul_one, one_mul] /-- This is the `Invertible` version of `Units.isUnit_mul_units` -/ abbrev invertibleOfMulInvertible (a b : α) [Invertible (a * b)] [Invertible b] : Invertible a where invOf := b * ⅟ (a * b) invOf_mul_self := by rw [← (isUnit_of_invertible b).mul_left_inj, mul_assoc, mul_assoc, invOf_mul_self, mul_one, one_mul] mul_invOf_self := by rw [← mul_assoc, mul_invOf_self] /-- `invertibleOfInvertibleMul` and `invertibleMul` as an equivalence. -/ @[simps apply symm_apply] def Invertible.mulLeft {a : α} (_ : Invertible a) (b : α) : Invertible b ≃ Invertible (a * b) where toFun _ := invertibleMul a b invFun _ := invertibleOfInvertibleMul a _ left_inv _ := Subsingleton.elim _ _ right_inv _ := Subsingleton.elim _ _ /-- `invertibleOfMulInvertible` and `invertibleMul` as an equivalence. -/ @[simps apply symm_apply] def Invertible.mulRight (a : α) {b : α} (_ : Invertible b) : Invertible a ≃ Invertible (a * b) where toFun _ := invertibleMul a b invFun _ := invertibleOfMulInvertible _ b left_inv _ := Subsingleton.elim _ _ right_inv _ := Subsingleton.elim _ _ instance invertiblePow (m : α) [Invertible m] (n : ℕ) : Invertible (m ^ n) where invOf := ⅟ m ^ n invOf_mul_self := by rw [← (commute_invOf m).symm.mul_pow, invOf_mul_self, one_pow] mul_invOf_self := by rw [← (commute_invOf m).mul_pow, mul_invOf_self, one_pow] lemma invOf_pow (m : α) [Invertible m] (n : ℕ) [Invertible (m ^ n)] : ⅟ (m ^ n) = ⅟ m ^ n := @invertible_unique _ _ _ _ _ (invertiblePow m n) rfl /-- If `x ^ n = 1` then `x` has an inverse, `x^(n - 1)`. -/ def invertibleOfPowEqOne (x : α) (n : ℕ) (hx : x ^ n = 1) (hn : n ≠ 0) : Invertible x := (Units.ofPowEqOne x n hx hn).invertible end Monoid /-- Monoid homs preserve invertibility. -/ def Invertible.map {R : Type*} {S : Type*} {F : Type*} [MulOneClass R] [MulOneClass S] [FunLike F R S] [MonoidHomClass F R S] (f : F) (r : R) [Invertible r] : Invertible (f r) where invOf := f (⅟ r) invOf_mul_self := by rw [← map_mul, invOf_mul_self, map_one] mul_invOf_self := by rw [← map_mul, mul_invOf_self, map_one] /-- Note that the `Invertible (f r)` argument can be satisfied by using `letI := Invertible.map f r` before applying this lemma. -/ theorem map_invOf {R : Type*} {S : Type*} {F : Type*} [MulOneClass R] [Monoid S] [FunLike F R S] [MonoidHomClass F R S] (f : F) (r : R) [Invertible r] [ifr : Invertible (f r)] : f (⅟ r) = ⅟ (f r) := have h : ifr = Invertible.map f r := Subsingleton.elim _ _ by subst h; rfl /-- If a function `f : R → S` has a left-inverse that is a monoid hom, then `r : R` is invertible if `f r` is. The inverse is computed as `g (⅟(f r))` -/ @[simps! -isSimp] def Invertible.ofLeftInverse {R : Type*} {S : Type*} {G : Type*} [MulOneClass R] [MulOneClass S] [FunLike G S R] [MonoidHomClass G S R] (f : R → S) (g : G) (r : R) (h : Function.LeftInverse g f) [Invertible (f r)] : Invertible r := (Invertible.map g (f r)).copy _ (h r).symm /-- Invertibility on either side of a monoid hom with a left-inverse is equivalent. -/ @[simps] def invertibleEquivOfLeftInverse {R : Type*} {S : Type*} {F G : Type*} [Monoid R] [Monoid S] [FunLike F R S] [MonoidHomClass F R S] [FunLike G S R] [MonoidHomClass G S R] (f : F) (g : G) (r : R) (h : Function.LeftInverse g f) : Invertible (f r) ≃ Invertible r where toFun _ := Invertible.ofLeftInverse f _ _ h invFun _ := Invertible.map f _ left_inv _ := Subsingleton.elim _ _ right_inv _ := Subsingleton.elim _ _
Mathlib/Algebra/Group/Invertible/Basic.lean
164
169
/- 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.Logic.Nontrivial.Basic import Mathlib.Order.TypeTags import Mathlib.Data.Option.NAry import Mathlib.Tactic.Contrapose import Mathlib.Tactic.Lift import Mathlib.Data.Option.Basic import Mathlib.Order.Lattice import Mathlib.Order.BoundedOrder.Basic /-! # `WithBot`, `WithTop` Adding a `bot` or a `top` to an order. ## Main declarations * `With<Top/Bot> α`: Equips `Option α` with the order on `α` plus `none` as the top/bottom element. -/ variable {α β γ δ : Type*} namespace WithBot variable {a b : α} instance nontrivial [Nonempty α] : Nontrivial (WithBot α) := Option.nontrivial open Function theorem coe_injective : Injective ((↑) : α → WithBot α) := Option.some_injective _ @[simp, norm_cast] theorem coe_inj : (a : WithBot α) = b ↔ a = b := Option.some_inj protected theorem «forall» {p : WithBot α → Prop} : (∀ x, p x) ↔ p ⊥ ∧ ∀ x : α, p x := Option.forall protected theorem «exists» {p : WithBot α → Prop} : (∃ x, p x) ↔ p ⊥ ∨ ∃ x : α, p x := Option.exists theorem none_eq_bot : (none : WithBot α) = (⊥ : WithBot α) := rfl theorem some_eq_coe (a : α) : (Option.some a : WithBot α) = (↑a : WithBot α) := rfl @[simp] theorem bot_ne_coe : ⊥ ≠ (a : WithBot α) := nofun @[simp] theorem coe_ne_bot : (a : WithBot α) ≠ ⊥ := nofun /-- Specialization of `Option.getD` to values in `WithBot α` that respects API boundaries. -/ def unbotD (d : α) (x : WithBot α) : α := recBotCoe d id x @[deprecated (since := "2025-02-06")] alias unbot' := unbotD @[simp] theorem unbotD_bot {α} (d : α) : unbotD d ⊥ = d := rfl @[deprecated (since := "2025-02-06")] alias unbot'_bot := unbotD_bot @[simp] theorem unbotD_coe {α} (d x : α) : unbotD d x = x := rfl @[deprecated (since := "2025-02-06")] alias unbot'_coe := unbotD_coe theorem coe_eq_coe : (a : WithBot α) = b ↔ a = b := coe_inj theorem unbotD_eq_iff {d y : α} {x : WithBot α} : unbotD d x = y ↔ x = y ∨ x = ⊥ ∧ y = d := by induction x <;> simp [@eq_comm _ d] @[deprecated (since := "2025-02-06")] alias unbot'_eq_iff := unbotD_eq_iff @[simp] theorem unbotD_eq_self_iff {d : α} {x : WithBot α} : unbotD d x = d ↔ x = d ∨ x = ⊥ := by simp [unbotD_eq_iff] @[deprecated (since := "2025-02-06")] alias unbot'_eq_self_iff := unbotD_eq_self_iff theorem unbotD_eq_unbotD_iff {d : α} {x y : WithBot α} : unbotD d x = unbotD d y ↔ x = y ∨ x = d ∧ y = ⊥ ∨ x = ⊥ ∧ y = d := by induction y <;> simp [unbotD_eq_iff, or_comm] @[deprecated (since := "2025-02-06")] alias unbot'_eq_unbot'_iff := unbotD_eq_unbotD_iff /-- Lift a map `f : α → β` to `WithBot α → WithBot β`. Implemented using `Option.map`. -/ def map (f : α → β) : WithBot α → WithBot β := Option.map f @[simp] theorem map_bot (f : α → β) : map f ⊥ = ⊥ := rfl @[simp] theorem map_coe (f : α → β) (a : α) : map f a = f a := rfl @[simp] lemma map_eq_bot_iff {f : α → β} {a : WithBot α} : map f a = ⊥ ↔ a = ⊥ := Option.map_eq_none_iff theorem map_eq_some_iff {f : α → β} {y : β} {v : WithBot α} : WithBot.map f v = .some y ↔ ∃ x, v = .some x ∧ f x = y := Option.map_eq_some_iff theorem some_eq_map_iff {f : α → β} {y : β} {v : WithBot α} : .some y = WithBot.map f v ↔ ∃ x, v = .some x ∧ f x = y := by cases v <;> simp [eq_comm] theorem map_comm {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ} (h : g₁ ∘ f₁ = g₂ ∘ f₂) (a : α) : map g₁ (map f₁ a) = map g₂ (map f₂ a) := Option.map_comm h _ /-- The image of a binary function `f : α → β → γ` as a function `WithBot α → WithBot β → WithBot γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def map₂ : (α → β → γ) → WithBot α → WithBot β → WithBot γ := Option.map₂ lemma map₂_coe_coe (f : α → β → γ) (a : α) (b : β) : map₂ f a b = f a b := rfl @[simp] lemma map₂_bot_left (f : α → β → γ) (b) : map₂ f ⊥ b = ⊥ := rfl @[simp] lemma map₂_bot_right (f : α → β → γ) (a) : map₂ f a ⊥ = ⊥ := by cases a <;> rfl @[simp] lemma map₂_coe_left (f : α → β → γ) (a : α) (b) : map₂ f a b = b.map fun b ↦ f a b := rfl @[simp] lemma map₂_coe_right (f : α → β → γ) (a) (b : β) : map₂ f a b = a.map (f · b) := by cases a <;> rfl @[simp] lemma map₂_eq_bot_iff {f : α → β → γ} {a : WithBot α} {b : WithBot β} : map₂ f a b = ⊥ ↔ a = ⊥ ∨ b = ⊥ := Option.map₂_eq_none_iff lemma ne_bot_iff_exists {x : WithBot α} : x ≠ ⊥ ↔ ∃ a : α, ↑a = x := Option.ne_none_iff_exists lemma eq_bot_iff_forall_ne {x : WithBot α} : x = ⊥ ↔ ∀ a : α, ↑a ≠ x := Option.eq_none_iff_forall_some_ne @[deprecated (since := "2025-03-19")] alias forall_ne_iff_eq_bot := eq_bot_iff_forall_ne /-- Deconstruct a `x : WithBot α` to the underlying value in `α`, given a proof that `x ≠ ⊥`. -/ def unbot : ∀ x : WithBot α, x ≠ ⊥ → α | (x : α), _ => x @[simp] lemma coe_unbot : ∀ (x : WithBot α) hx, x.unbot hx = x | (x : α), _ => rfl @[simp] theorem unbot_coe (x : α) (h : (x : WithBot α) ≠ ⊥ := coe_ne_bot) : (x : WithBot α).unbot h = x := rfl instance canLift : CanLift (WithBot α) α (↑) fun r => r ≠ ⊥ where prf x h := ⟨x.unbot h, coe_unbot _ _⟩ instance instTop [Top α] : Top (WithBot α) where top := (⊤ : α) @[simp, norm_cast] lemma coe_top [Top α] : ((⊤ : α) : WithBot α) = ⊤ := rfl @[simp, norm_cast] lemma coe_eq_top [Top α] {a : α} : (a : WithBot α) = ⊤ ↔ a = ⊤ := coe_eq_coe @[simp, norm_cast] lemma top_eq_coe [Top α] {a : α} : ⊤ = (a : WithBot α) ↔ ⊤ = a := coe_eq_coe theorem unbot_eq_iff {a : WithBot α} {b : α} (h : a ≠ ⊥) : a.unbot h = b ↔ a = b := by induction a · simpa using h rfl · simp theorem eq_unbot_iff {a : α} {b : WithBot α} (h : b ≠ ⊥) : a = b.unbot h ↔ a = b := by induction b · simpa using h rfl · simp /-- The equivalence between the non-bottom elements of `WithBot α` and `α`. -/ @[simps] def _root_.Equiv.withBotSubtypeNe : {y : WithBot α // y ≠ ⊥} ≃ α where toFun := fun ⟨x,h⟩ => WithBot.unbot x h invFun x := ⟨x, WithBot.coe_ne_bot⟩ left_inv _ := by simp right_inv _ := by simp section LE variable [LE α] {x y : WithBot α} instance (priority := 10) le : LE (WithBot α) := ⟨fun o₁ o₂ => ∀ a : α, o₁ = ↑a → ∃ b : α, o₂ = ↑b ∧ a ≤ b⟩ lemma le_def : x ≤ y ↔ ∀ a : α, x = ↑a → ∃ b : α, y = ↑b ∧ a ≤ b := .rfl @[simp, norm_cast] lemma coe_le_coe : (a : WithBot α) ≤ b ↔ a ≤ b := by simp [le_def] lemma not_coe_le_bot (a : α) : ¬(a : WithBot α) ≤ ⊥ := by simp [le_def] instance orderBot : OrderBot (WithBot α) where bot_le := by simp [le_def] instance orderTop [OrderTop α] : OrderTop (WithBot α) where le_top x := by cases x <;> simp [le_def] instance instBoundedOrder [OrderTop α] : BoundedOrder (WithBot α) := { WithBot.orderBot, WithBot.orderTop with } /-- There is a general version `le_bot_iff`, but this lemma does not require a `PartialOrder`. -/ @[simp] protected theorem le_bot_iff : ∀ {a : WithBot α}, a ≤ ⊥ ↔ a = ⊥ | (a : α) => by simp [not_coe_le_bot _] | ⊥ => by simp theorem coe_le : ∀ {o : Option α}, b ∈ o → ((a : WithBot α) ≤ o ↔ a ≤ b) | _, rfl => coe_le_coe theorem coe_le_iff : a ≤ x ↔ ∃ b : α, x = b ∧ a ≤ b := by simp [le_def] theorem le_coe_iff : x ≤ b ↔ ∀ a : α, x = ↑a → a ≤ b := by simp [le_def] protected theorem _root_.IsMax.withBot (h : IsMax a) : IsMax (a : WithBot α) := fun x ↦ by cases x <;> simp; simpa using @h _ lemma le_unbot_iff (hy : y ≠ ⊥) : a ≤ unbot y hy ↔ a ≤ y := by lift y to α using id hy; simp lemma unbot_le_iff (hx : x ≠ ⊥) : unbot x hx ≤ b ↔ x ≤ b := by lift x to α using id hx; simp lemma unbotD_le_iff (hx : x = ⊥ → a ≤ b) : x.unbotD a ≤ b ↔ x ≤ b := by cases x <;> simp [hx] @[deprecated (since := "2025-02-06")] alias unbot'_le_iff := unbotD_le_iff end LE section LT variable [LT α] {x y : WithBot α} instance (priority := 10) lt : LT (WithBot α) := ⟨fun o₁ o₂ : WithBot α => ∃ b : α, o₂ = ↑b ∧ ∀ a : α, o₁ = ↑a → a < b⟩ lemma lt_def : x < y ↔ ∃ b : α, y = ↑b ∧ ∀ a : α, x = ↑a → a < b := .rfl @[simp, norm_cast] lemma coe_lt_coe : (a : WithBot α) < b ↔ a < b := by simp [lt_def] @[simp] lemma bot_lt_coe (a : α) : ⊥ < (a : WithBot α) := by simp [lt_def] @[simp] protected lemma not_lt_bot (a : WithBot α) : ¬a < ⊥ := by simp [lt_def] lemma lt_iff_exists_coe : x < y ↔ ∃ b : α, y = b ∧ x < b := by cases y <;> simp lemma lt_coe_iff : x < b ↔ ∀ a : α, x = a → a < b := by simp [lt_def] /-- A version of `bot_lt_iff_ne_bot` for `WithBot` that only requires `LT α`, not `PartialOrder α`. -/ protected lemma bot_lt_iff_ne_bot : ⊥ < x ↔ x ≠ ⊥ := by cases x <;> simp lemma lt_unbot_iff (hy : y ≠ ⊥) : a < unbot y hy ↔ a < y := by lift y to α using id hy; simp lemma unbot_lt_iff (hx : x ≠ ⊥) : unbot x hx < b ↔ x < b := by lift x to α using id hx; simp lemma unbotD_lt_iff (hx : x = ⊥ → a < b) : x.unbotD a < b ↔ x < b := by cases x <;> simp [hx] @[deprecated (since := "2025-02-06")] alias unbot'_lt_iff := unbotD_lt_iff end LT instance preorder [Preorder α] : Preorder (WithBot α) where lt_iff_le_not_le x y := by cases x <;> cases y <;> simp [lt_iff_le_not_le] le_refl x := by cases x <;> simp [le_def] le_trans x y z := by cases x <;> cases y <;> cases z <;> simp [le_def]; simpa using le_trans instance partialOrder [PartialOrder α] : PartialOrder (WithBot α) where le_antisymm x y := by cases x <;> cases y <;> simp [le_def]; simpa using le_antisymm section Preorder variable [Preorder α] [Preorder β] {x y : WithBot α} theorem coe_strictMono : StrictMono (fun (a : α) => (a : WithBot α)) := fun _ _ => coe_lt_coe.2 theorem coe_mono : Monotone (fun (a : α) => (a : WithBot α)) := fun _ _ => coe_le_coe.2 theorem monotone_iff {f : WithBot α → β} : Monotone f ↔ Monotone (fun a ↦ f a : α → β) ∧ ∀ x : α, f ⊥ ≤ f x := ⟨fun h ↦ ⟨h.comp WithBot.coe_mono, fun _ ↦ h bot_le⟩, fun h ↦ WithBot.forall.2 ⟨WithBot.forall.2 ⟨fun _ => le_rfl, fun x _ => h.2 x⟩, fun _ => WithBot.forall.2 ⟨fun h => (not_coe_le_bot _ h).elim, fun _ hle => h.1 (coe_le_coe.1 hle)⟩⟩⟩ @[simp] theorem monotone_map_iff {f : α → β} : Monotone (WithBot.map f) ↔ Monotone f := monotone_iff.trans <| by simp [Monotone] alias ⟨_, _root_.Monotone.withBot_map⟩ := monotone_map_iff theorem strictMono_iff {f : WithBot α → β} : StrictMono f ↔ StrictMono (fun a => f a : α → β) ∧ ∀ x : α, f ⊥ < f x := ⟨fun h => ⟨h.comp WithBot.coe_strictMono, fun _ => h (bot_lt_coe _)⟩, fun h => WithBot.forall.2 ⟨WithBot.forall.2 ⟨flip absurd (lt_irrefl _), fun x _ => h.2 x⟩, fun _ => WithBot.forall.2 ⟨fun h => (not_lt_bot h).elim, fun _ hle => h.1 (coe_lt_coe.1 hle)⟩⟩⟩ theorem strictAnti_iff {f : WithBot α → β} : StrictAnti f ↔ StrictAnti (fun a ↦ f a : α → β) ∧ ∀ x : α, f x < f ⊥ := strictMono_iff (β := βᵒᵈ) @[simp] theorem strictMono_map_iff {f : α → β} : StrictMono (WithBot.map f) ↔ StrictMono f := strictMono_iff.trans <| by simp [StrictMono, bot_lt_coe] alias ⟨_, _root_.StrictMono.withBot_map⟩ := strictMono_map_iff lemma map_le_iff (f : α → β) (mono_iff : ∀ {a b}, f a ≤ f b ↔ a ≤ b) : x.map f ≤ y.map f ↔ x ≤ y := by cases x <;> cases y <;> simp [mono_iff] theorem le_coe_unbotD (x : WithBot α) (b : α) : x ≤ x.unbotD b := by cases x <;> simp @[deprecated (since := "2025-02-06")] alias le_coe_unbot' := le_coe_unbotD @[simp] theorem lt_coe_bot [OrderBot α] : x < (⊥ : α) ↔ x = ⊥ := by cases x <;> simp lemma eq_bot_iff_forall_lt : x = ⊥ ↔ ∀ b : α, x < b := by cases x <;> simp; simpa using ⟨_, lt_irrefl _⟩ lemma eq_bot_iff_forall_le [NoBotOrder α] : x = ⊥ ↔ ∀ b : α, x ≤ b := by refine ⟨by simp +contextual, fun h ↦ (x.eq_bot_iff_forall_ne).2 fun y => ?_⟩ rintro rfl exact not_isBot y fun z => coe_le_coe.1 (h z) @[deprecated (since := "2025-03-19")] alias forall_lt_iff_eq_bot := eq_bot_iff_forall_lt @[deprecated (since := "2025-03-19")] alias forall_le_iff_eq_bot := eq_bot_iff_forall_le lemma forall_le_coe_iff_le [NoBotOrder α] : (∀ a : α, y ≤ a → x ≤ a) ↔ x ≤ y := by obtain _ | y := y · simp [WithBot.none_eq_bot, eq_bot_iff_forall_le] · exact ⟨fun h ↦ h _ le_rfl, fun hmn a ham ↦ hmn.trans ham⟩ end Preorder section PartialOrder variable [PartialOrder α] [NoBotOrder α] {x y : WithBot α} lemma eq_of_forall_le_coe_iff (h : ∀ a : α, x ≤ a ↔ y ≤ a) : x = y := le_antisymm (forall_le_coe_iff_le.mp fun a ↦ (h a).2) (forall_le_coe_iff_le.mp fun a ↦ (h a).1) end PartialOrder instance semilatticeSup [SemilatticeSup α] : SemilatticeSup (WithBot α) where sup -- note this is `Option.merge`, but with the right defeq when unfolding | ⊥, ⊥ => ⊥ | (a : α), ⊥ => a | ⊥, (b : α) => b | (a : α), (b : α) => ↑(a ⊔ b) le_sup_left x y := by cases x <;> cases y <;> simp le_sup_right x y := by cases x <;> cases y <;> simp sup_le x y z := by cases x <;> cases y <;> cases z <;> simp; simpa using sup_le theorem coe_sup [SemilatticeSup α] (a b : α) : ((a ⊔ b : α) : WithBot α) = (a : WithBot α) ⊔ b := rfl instance semilatticeInf [SemilatticeInf α] : SemilatticeInf (WithBot α) where inf := .map₂ (· ⊓ ·) inf_le_left x y := by cases x <;> cases y <;> simp inf_le_right x y := by cases x <;> cases y <;> simp le_inf x y z := by cases x <;> cases y <;> cases z <;> simp; simpa using le_inf theorem coe_inf [SemilatticeInf α] (a b : α) : ((a ⊓ b : α) : WithBot α) = (a : WithBot α) ⊓ b := rfl instance lattice [Lattice α] : Lattice (WithBot α) := { WithBot.semilatticeSup, WithBot.semilatticeInf with } instance distribLattice [DistribLattice α] : DistribLattice (WithBot α) where le_sup_inf x y z := by cases x <;> cases y <;> cases z <;> simp [← coe_inf, ← coe_sup] simpa [← coe_inf, ← coe_sup] using le_sup_inf instance decidableEq [DecidableEq α] : DecidableEq (WithBot α) := inferInstanceAs <| DecidableEq (Option α) instance decidableLE [LE α] [DecidableLE α] : DecidableLE (WithBot α) | ⊥, _ => isTrue <| by simp | (a : α), ⊥ => isFalse <| by simp | (a : α), (b : α) => decidable_of_iff' _ coe_le_coe instance decidableLT [LT α] [DecidableLT α] : DecidableLT (WithBot α) | _, ⊥ => isFalse <| by simp | ⊥, (a : α) => isTrue <| by simp | (a : α), (b : α) => decidable_of_iff' _ coe_lt_coe instance isTotal_le [LE α] [IsTotal α (· ≤ ·)] : IsTotal (WithBot α) (· ≤ ·) where total x y := by cases x <;> cases y <;> simp; simpa using IsTotal.total .. section LinearOrder variable [LinearOrder α] {x y : WithBot α} instance linearOrder : LinearOrder (WithBot α) := Lattice.toLinearOrder _ @[simp, norm_cast] lemma coe_min (a b : α) : ↑(min a b) = min (a : WithBot α) b := rfl @[simp, norm_cast] lemma coe_max (a b : α) : ↑(max a b) = max (a : WithBot α) b := rfl variable [DenselyOrdered α] [NoMinOrder α] lemma le_of_forall_lt_iff_le : (∀ z : α, x < z → y ≤ z) ↔ y ≤ x := by cases x <;> cases y <;> simp [exists_lt, forall_gt_imp_ge_iff_le_of_dense] lemma ge_of_forall_gt_iff_ge : (∀ z : α, z < x → z ≤ y) ↔ x ≤ y := by cases x <;> cases y <;> simp [exists_lt, forall_lt_imp_le_iff_le_of_dense] end LinearOrder instance instWellFoundedLT [LT α] [WellFoundedLT α] : WellFoundedLT (WithBot α) where wf := .intro fun | ⊥ => ⟨_, by simp⟩ | (a : α) => (wellFounded_lt.1 a).rec fun _ _ ih ↦ .intro _ fun | ⊥, _ => ⟨_, by simp⟩ | (b : α), hlt => ih _ (coe_lt_coe.1 hlt) instance _root_.WithBot.instWellFoundedGT [LT α] [WellFoundedGT α] : WellFoundedGT (WithBot α) where wf := have acc_some (a : α) : Acc ((· > ·) : WithBot α → WithBot α → Prop) a := (wellFounded_gt.1 a).rec fun _ _ ih => .intro _ fun | (b : α), hlt => ih _ (coe_lt_coe.1 hlt) .intro fun | (a : α) => acc_some a | ⊥ => .intro _ fun | (b : α), _ => acc_some b instance denselyOrdered [LT α] [DenselyOrdered α] [NoMinOrder α] : DenselyOrdered (WithBot α) where dense := fun | ⊥, (b : α), _ => let ⟨a, ha⟩ := exists_lt b ⟨a, by simpa⟩ | (a : α), (b : α), hab => let ⟨c, hac, hcb⟩ := exists_between (coe_lt_coe.1 hab) ⟨c, coe_lt_coe.2 hac, coe_lt_coe.2 hcb⟩ theorem lt_iff_exists_coe_btwn [Preorder α] [DenselyOrdered α] [NoMinOrder α] {a b : WithBot α} : a < b ↔ ∃ x : α, a < ↑x ∧ ↑x < b := ⟨fun h => let ⟨_, hy⟩ := exists_between h let ⟨x, hx⟩ := lt_iff_exists_coe.1 hy.1 ⟨x, hx.1 ▸ hy⟩, fun ⟨_, hx⟩ => lt_trans hx.1 hx.2⟩ instance noTopOrder [LE α] [NoTopOrder α] [Nonempty α] : NoTopOrder (WithBot α) where exists_not_le := fun | ⊥ => ‹Nonempty α›.elim fun a ↦ ⟨a, by simp⟩ | (a : α) => let ⟨b, hba⟩ := exists_not_le a; ⟨b, mod_cast hba⟩ instance noMaxOrder [LT α] [NoMaxOrder α] [Nonempty α] : NoMaxOrder (WithBot α) where exists_gt := fun | ⊥ => ‹Nonempty α›.elim fun a ↦ ⟨a, by simp⟩ | (a : α) => let ⟨b, hba⟩ := exists_gt a; ⟨b, mod_cast hba⟩ end WithBot namespace WithTop variable {a b : α} instance nontrivial [Nonempty α] : Nontrivial (WithTop α) := Option.nontrivial open Function theorem coe_injective : Injective ((↑) : α → WithTop α) := Option.some_injective _ @[norm_cast] theorem coe_inj : (a : WithTop α) = b ↔ a = b := Option.some_inj protected theorem «forall» {p : WithTop α → Prop} : (∀ x, p x) ↔ p ⊤ ∧ ∀ x : α, p x := Option.forall protected theorem «exists» {p : WithTop α → Prop} : (∃ x, p x) ↔ p ⊤ ∨ ∃ x : α, p x := Option.exists theorem none_eq_top : (none : WithTop α) = (⊤ : WithTop α) := rfl theorem some_eq_coe (a : α) : (Option.some a : WithTop α) = (↑a : WithTop α) := rfl @[simp] theorem top_ne_coe : ⊤ ≠ (a : WithTop α) := nofun @[simp] theorem coe_ne_top : (a : WithTop α) ≠ ⊤ := nofun /-- `WithTop.toDual` is the equivalence sending `⊤` to `⊥` and any `a : α` to `toDual a : αᵒᵈ`. See `WithTop.toDualBotEquiv` for the related order-iso. -/ protected def toDual : WithTop α ≃ WithBot αᵒᵈ := Equiv.refl _ /-- `WithTop.ofDual` is the equivalence sending `⊤` to `⊥` and any `a : αᵒᵈ` to `ofDual a : α`. See `WithTop.toDualBotEquiv` for the related order-iso. -/ protected def ofDual : WithTop αᵒᵈ ≃ WithBot α := Equiv.refl _ /-- `WithBot.toDual` is the equivalence sending `⊥` to `⊤` and any `a : α` to `toDual a : αᵒᵈ`. See `WithBot.toDual_top_equiv` for the related order-iso. -/ protected def _root_.WithBot.toDual : WithBot α ≃ WithTop αᵒᵈ := Equiv.refl _ /-- `WithBot.ofDual` is the equivalence sending `⊥` to `⊤` and any `a : αᵒᵈ` to `ofDual a : α`. See `WithBot.ofDual_top_equiv` for the related order-iso. -/ protected def _root_.WithBot.ofDual : WithBot αᵒᵈ ≃ WithTop α := Equiv.refl _ @[simp] theorem toDual_symm_apply (a : WithBot αᵒᵈ) : WithTop.toDual.symm a = WithBot.ofDual a := rfl @[simp] theorem ofDual_symm_apply (a : WithBot α) : WithTop.ofDual.symm a = WithBot.toDual a := rfl @[simp] theorem toDual_apply_top : WithTop.toDual (⊤ : WithTop α) = ⊥ := rfl @[simp] theorem ofDual_apply_top : WithTop.ofDual (⊤ : WithTop α) = ⊥ := rfl open OrderDual @[simp] theorem toDual_apply_coe (a : α) : WithTop.toDual (a : WithTop α) = toDual a := rfl @[simp] theorem ofDual_apply_coe (a : αᵒᵈ) : WithTop.ofDual (a : WithTop αᵒᵈ) = ofDual a := rfl /-- Specialization of `Option.getD` to values in `WithTop α` that respects API boundaries. -/ def untopD (d : α) (x : WithTop α) : α := recTopCoe d id x @[deprecated (since := "2025-02-06")] alias untop' := untopD @[simp] theorem untopD_top {α} (d : α) : untopD d ⊤ = d := rfl @[deprecated (since := "2025-02-06")] alias untop'_top := untopD_top @[simp] theorem untopD_coe {α} (d x : α) : untopD d x = x := rfl @[deprecated (since := "2025-02-06")] alias untop'_coe := untopD_coe @[simp, norm_cast] theorem coe_eq_coe : (a : WithTop α) = b ↔ a = b := Option.some_inj theorem untopD_eq_iff {d y : α} {x : WithTop α} : untopD d x = y ↔ x = y ∨ x = ⊤ ∧ y = d := WithBot.unbotD_eq_iff @[deprecated (since := "2025-02-06")] alias untop'_eq_iff := untopD_eq_iff @[simp] theorem untopD_eq_self_iff {d : α} {x : WithTop α} : untopD d x = d ↔ x = d ∨ x = ⊤ := WithBot.unbotD_eq_self_iff @[deprecated (since := "2025-02-06")] alias untop'_eq_self_iff := untopD_eq_self_iff theorem untopD_eq_untopD_iff {d : α} {x y : WithTop α} : untopD d x = untopD d y ↔ x = y ∨ x = d ∧ y = ⊤ ∨ x = ⊤ ∧ y = d := WithBot.unbotD_eq_unbotD_iff @[deprecated (since := "2025-02-06")] alias untop'_eq_untop'_iff := untopD_eq_untopD_iff /-- Lift a map `f : α → β` to `WithTop α → WithTop β`. Implemented using `Option.map`. -/ def map (f : α → β) : WithTop α → WithTop β := Option.map f @[simp] theorem map_top (f : α → β) : map f ⊤ = ⊤ := rfl @[simp] theorem map_coe (f : α → β) (a : α) : map f a = f a := rfl @[simp] lemma map_eq_top_iff {f : α → β} {a : WithTop α} : map f a = ⊤ ↔ a = ⊤ := Option.map_eq_none_iff theorem map_eq_some_iff {f : α → β} {y : β} {v : WithTop α} : WithTop.map f v = .some y ↔ ∃ x, v = .some x ∧ f x = y := Option.map_eq_some_iff theorem some_eq_map_iff {f : α → β} {y : β} {v : WithTop α} : .some y = WithTop.map f v ↔ ∃ x, v = .some x ∧ f x = y := by cases v <;> simp [eq_comm] theorem map_comm {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ} (h : g₁ ∘ f₁ = g₂ ∘ f₂) (a : α) : map g₁ (map f₁ a) = map g₂ (map f₂ a) := Option.map_comm h _ /-- The image of a binary function `f : α → β → γ` as a function `WithTop α → WithTop β → WithTop γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def map₂ : (α → β → γ) → WithTop α → WithTop β → WithTop γ := Option.map₂ lemma map₂_coe_coe (f : α → β → γ) (a : α) (b : β) : map₂ f a b = f a b := rfl @[simp] lemma map₂_top_left (f : α → β → γ) (b) : map₂ f ⊤ b = ⊤ := rfl @[simp] lemma map₂_top_right (f : α → β → γ) (a) : map₂ f a ⊤ = ⊤ := by cases a <;> rfl @[simp] lemma map₂_coe_left (f : α → β → γ) (a : α) (b) : map₂ f a b = b.map fun b ↦ f a b := rfl @[simp] lemma map₂_coe_right (f : α → β → γ) (a) (b : β) : map₂ f a b = a.map (f · b) := by cases a <;> rfl @[simp] lemma map₂_eq_top_iff {f : α → β → γ} {a : WithTop α} {b : WithTop β} : map₂ f a b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := Option.map₂_eq_none_iff theorem map_toDual (f : αᵒᵈ → βᵒᵈ) (a : WithBot α) : map f (WithBot.toDual a) = a.map (toDual ∘ f) := rfl theorem map_ofDual (f : α → β) (a : WithBot αᵒᵈ) : map f (WithBot.ofDual a) = a.map (ofDual ∘ f) := rfl theorem toDual_map (f : α → β) (a : WithTop α) : WithTop.toDual (map f a) = WithBot.map (toDual ∘ f ∘ ofDual) (WithTop.toDual a) := rfl theorem ofDual_map (f : αᵒᵈ → βᵒᵈ) (a : WithTop αᵒᵈ) : WithTop.ofDual (map f a) = WithBot.map (ofDual ∘ f ∘ toDual) (WithTop.ofDual a) := rfl lemma ne_top_iff_exists {x : WithTop α} : x ≠ ⊤ ↔ ∃ a : α, ↑a = x := Option.ne_none_iff_exists lemma eq_top_iff_forall_ne {x : WithTop α} : x = ⊤ ↔ ∀ a : α, ↑a ≠ x := Option.eq_none_iff_forall_some_ne @[deprecated (since := "2025-03-19")] alias forall_ne_iff_eq_top := eq_top_iff_forall_ne /-- Deconstruct a `x : WithTop α` to the underlying value in `α`, given a proof that `x ≠ ⊤`. -/ def untop : ∀ x : WithTop α, x ≠ ⊤ → α | (x : α), _ => x @[simp] lemma coe_untop : ∀ (x : WithTop α) hx, x.untop hx = x | (x : α), _ => rfl @[simp] theorem untop_coe (x : α) (h : (x : WithTop α) ≠ ⊤ := coe_ne_top) : (x : WithTop α).untop h = x := rfl instance canLift : CanLift (WithTop α) α (↑) fun r => r ≠ ⊤ where prf x h := ⟨x.untop h, coe_untop _ _⟩ instance instBot [Bot α] : Bot (WithTop α) where bot := (⊥ : α) @[simp, norm_cast] lemma coe_bot [Bot α] : ((⊥ : α) : WithTop α) = ⊥ := rfl @[simp, norm_cast] lemma coe_eq_bot [Bot α] {a : α} : (a : WithTop α) = ⊥ ↔ a = ⊥ := coe_eq_coe @[simp, norm_cast] lemma bot_eq_coe [Bot α] {a : α} : (⊥ : WithTop α) = a ↔ ⊥ = a := coe_eq_coe theorem untop_eq_iff {a : WithTop α} {b : α} (h : a ≠ ⊤) : a.untop h = b ↔ a = b := WithBot.unbot_eq_iff (α := αᵒᵈ) h theorem eq_untop_iff {a : α} {b : WithTop α} (h : b ≠ ⊤) : a = b.untop h ↔ a = b := WithBot.eq_unbot_iff (α := αᵒᵈ) h /-- The equivalence between the non-top elements of `WithTop α` and `α`. -/ @[simps] def _root_.Equiv.withTopSubtypeNe : {y : WithTop α // y ≠ ⊤} ≃ α where toFun := fun ⟨x,h⟩ => WithTop.untop x h invFun x := ⟨x, WithTop.coe_ne_top⟩ left_inv _ := by simp right_inv _:= by simp section LE variable [LE α] {x y : WithTop α} instance (priority := 10) le : LE (WithTop α) := ⟨fun o₁ o₂ => ∀ a : α, o₂ = ↑a → ∃ b : α, o₁ = ↑b ∧ b ≤ a⟩ lemma le_def : x ≤ y ↔ ∀ b : α, y = ↑b → ∃ a : α, x = ↑a ∧ a ≤ b := .rfl @[simp, norm_cast] lemma coe_le_coe : (a : WithTop α) ≤ b ↔ a ≤ b := by simp [le_def] lemma not_top_le_coe (a : α) : ¬ ⊤ ≤ (a : WithTop α) := by simp [le_def] instance orderTop : OrderTop (WithTop α) where le_top := by simp [le_def] instance orderBot [OrderBot α] : OrderBot (WithTop α) where bot_le x := by cases x <;> simp [le_def] instance boundedOrder [OrderBot α] : BoundedOrder (WithTop α) := { WithTop.orderTop, WithTop.orderBot with } /-- There is a general version `top_le_iff`, but this lemma does not require a `PartialOrder`. -/ @[simp] protected theorem top_le_iff : ∀ {a : WithTop α}, ⊤ ≤ a ↔ a = ⊤ | (a : α) => by simp [not_top_le_coe _] | ⊤ => by simp theorem le_coe : ∀ {o : Option α}, a ∈ o → (@LE.le (WithTop α) _ o b ↔ a ≤ b) | _, rfl => coe_le_coe theorem le_coe_iff : x ≤ b ↔ ∃ a : α, x = a ∧ a ≤ b := by simp [le_def] theorem coe_le_iff : ↑a ≤ x ↔ ∀ b : α, x = ↑b → a ≤ b := by simp [le_def] protected theorem _root_.IsMin.withTop (h : IsMin a) : IsMin (a : WithTop α) := fun x ↦ by cases x <;> simp; simpa using @h _ lemma untop_le_iff (hx : x ≠ ⊤) : untop x hx ≤ b ↔ x ≤ b := by lift x to α using id hx; simp lemma le_untop_iff (hy : y ≠ ⊤) : a ≤ untop y hy ↔ a ≤ y := by lift y to α using id hy; simp lemma le_untopD_iff (hy : y = ⊤ → a ≤ b) : a ≤ y.untopD b ↔ a ≤ y := by cases y <;> simp [hy] @[deprecated (since := "2025-02-11")] alias le_untop'_iff := le_untopD_iff end LE section LT variable [LT α] {x y : WithTop α} instance (priority := 10) lt : LT (WithTop α) := ⟨fun o₁ o₂ : Option α => ∃ b ∈ o₁, ∀ a ∈ o₂, b < a⟩ lemma lt_def : x < y ↔ ∃ a : α, x = ↑a ∧ ∀ b : α, y = ↑b → a < b := .rfl @[simp, norm_cast] lemma coe_lt_coe : (a : WithTop α) < b ↔ a < b := by simp [lt_def] @[simp] lemma coe_lt_top (a : α) : (a : WithTop α) < ⊤ := by simp [lt_def] @[simp] protected lemma not_top_lt (a : WithTop α) : ¬⊤ < a := by simp [lt_def] lemma lt_iff_exists_coe : x < y ↔ ∃ a : α, x = a ∧ a < y := by cases x <;> simp lemma coe_lt_iff : a < y ↔ ∀ b : α, y = b → a < b := by simp [lt_def] /-- A version of `lt_top_iff_ne_top` for `WithTop` that only requires `LT α`, not `PartialOrder α`. -/ protected lemma lt_top_iff_ne_top : x < ⊤ ↔ x ≠ ⊤ := by cases x <;> simp lemma lt_untop_iff (hy : y ≠ ⊤) : a < y.untop hy ↔ a < y := by lift y to α using id hy; simp lemma untop_lt_iff (hx : x ≠ ⊤) : x.untop hx < b ↔ x < b := by lift x to α using id hx; simp lemma lt_untopD_iff (hy : y = ⊤ → a < b) : a < y.untopD b ↔ a < y := by cases y <;> simp [hy] @[deprecated (since := "2025-02-11")] alias lt_untop'_iff := lt_untopD_iff end LT instance preorder [Preorder α] : Preorder (WithTop α) where lt_iff_le_not_le x y := by cases x <;> cases y <;> simp [lt_iff_le_not_le] le_refl x := by cases x <;> simp [le_def] le_trans x y z := by cases x <;> cases y <;> cases z <;> simp [le_def]; simpa using le_trans instance partialOrder [PartialOrder α] : PartialOrder (WithTop α) where le_antisymm x y := by cases x <;> cases y <;> simp [le_def]; simpa using le_antisymm section Preorder variable [Preorder α] [Preorder β] {x y : WithTop α} theorem coe_strictMono : StrictMono (fun a : α => (a : WithTop α)) := fun _ _ => coe_lt_coe.2 theorem coe_mono : Monotone (fun a : α => (a : WithTop α)) := fun _ _ => coe_le_coe.2 theorem monotone_iff {f : WithTop α → β} : Monotone f ↔ Monotone (fun (a : α) => f a) ∧ ∀ x : α, f x ≤ f ⊤ := ⟨fun h => ⟨h.comp WithTop.coe_mono, fun _ => h le_top⟩, fun h => WithTop.forall.2 ⟨WithTop.forall.2 ⟨fun _ => le_rfl, fun _ h => (not_top_le_coe _ h).elim⟩, fun x => WithTop.forall.2 ⟨fun _ => h.2 x, fun _ hle => h.1 (coe_le_coe.1 hle)⟩⟩⟩ @[simp] theorem monotone_map_iff {f : α → β} : Monotone (WithTop.map f) ↔ Monotone f := monotone_iff.trans <| by simp [Monotone] alias ⟨_, _root_.Monotone.withTop_map⟩ := monotone_map_iff theorem strictMono_iff {f : WithTop α → β} : StrictMono f ↔ StrictMono (fun (a : α) => f a) ∧ ∀ x : α, f x < f ⊤ := ⟨fun h => ⟨h.comp WithTop.coe_strictMono, fun _ => h (coe_lt_top _)⟩, fun h => WithTop.forall.2 ⟨WithTop.forall.2 ⟨flip absurd (lt_irrefl _), fun _ h => (not_top_lt h).elim⟩, fun x => WithTop.forall.2 ⟨fun _ => h.2 x, fun _ hle => h.1 (coe_lt_coe.1 hle)⟩⟩⟩ theorem strictAnti_iff {f : WithTop α → β} : StrictAnti f ↔ StrictAnti (fun a ↦ f a : α → β) ∧ ∀ x : α, f ⊤ < f x := strictMono_iff (β := βᵒᵈ) @[simp] theorem strictMono_map_iff {f : α → β} : StrictMono (WithTop.map f) ↔ StrictMono f := strictMono_iff.trans <| by simp [StrictMono, coe_lt_top] alias ⟨_, _root_.StrictMono.withTop_map⟩ := strictMono_map_iff theorem map_le_iff (f : α → β) (mono_iff : ∀ {a b}, f a ≤ f b ↔ a ≤ b) : x.map f ≤ y.map f ↔ x ≤ y := by cases x <;> cases y <;> simp [mono_iff] theorem coe_untopD_le (y : WithTop α) (a : α) : y.untopD a ≤ y := by cases y <;> simp @[deprecated (since := "2025-02-11")] alias coe_untop'_le := coe_untopD_le @[simp] theorem coe_top_lt [OrderTop α] : (⊤ : α) < x ↔ x = ⊤ := by cases x <;> simp lemma eq_top_iff_forall_gt : y = ⊤ ↔ ∀ a : α, a < y := by cases y <;> simp; simpa using ⟨_, lt_irrefl _⟩ lemma eq_top_iff_forall_ge [NoTopOrder α] : y = ⊤ ↔ ∀ a : α, a ≤ y := WithBot.eq_bot_iff_forall_le (α := αᵒᵈ) @[deprecated (since := "2025-03-19")] alias forall_gt_iff_eq_top := eq_top_iff_forall_gt @[deprecated (since := "2025-03-19")] alias forall_ge_iff_eq_top := eq_top_iff_forall_ge lemma forall_coe_le_iff_le [NoTopOrder α] : (∀ a : α, a ≤ x → a ≤ y) ↔ x ≤ y := WithBot.forall_le_coe_iff_le (α := αᵒᵈ) end Preorder section PartialOrder variable [PartialOrder α] [NoTopOrder α] {x y : WithTop α} lemma eq_of_forall_coe_le_iff (h : ∀ a : α, a ≤ x ↔ a ≤ y) : x = y := WithBot.eq_of_forall_le_coe_iff (α := αᵒᵈ) h end PartialOrder instance semilatticeInf [SemilatticeInf α] : SemilatticeInf (WithTop α) where inf -- note this is `Option.merge`, but with the right defeq when unfolding | ⊤, ⊤ => ⊤ | (a : α), ⊤ => a | ⊤, (b : α) => b | (a : α), (b : α) => ↑(a ⊓ b) inf_le_left x y := by cases x <;> cases y <;> simp inf_le_right x y := by cases x <;> cases y <;> simp le_inf x y z := by cases x <;> cases y <;> cases z <;> simp; simpa using le_inf theorem coe_inf [SemilatticeInf α] (a b : α) : ((a ⊓ b : α) : WithTop α) = (a : WithTop α) ⊓ b := rfl instance semilatticeSup [SemilatticeSup α] : SemilatticeSup (WithTop α) where sup := .map₂ (· ⊔ ·) le_sup_left x y := by cases x <;> cases y <;> simp le_sup_right x y := by cases x <;> cases y <;> simp sup_le x y z := by cases x <;> cases y <;> cases z <;> simp; simpa using sup_le theorem coe_sup [SemilatticeSup α] (a b : α) : ((a ⊔ b : α) : WithTop α) = (a : WithTop α) ⊔ b := rfl instance lattice [Lattice α] : Lattice (WithTop α) := { WithTop.semilatticeSup, WithTop.semilatticeInf with } instance distribLattice [DistribLattice α] : DistribLattice (WithTop α) where le_sup_inf x y z := by cases x <;> cases y <;> cases z <;> simp [← coe_inf, ← coe_sup] simpa [← coe_inf, ← coe_sup] using le_sup_inf instance decidableEq [DecidableEq α] : DecidableEq (WithTop α) := inferInstanceAs <| DecidableEq (Option α) instance decidableLE [LE α] [DecidableLE α] : DecidableLE (WithTop α) | _, ⊤ => isTrue <| by simp | ⊤, (a : α) => isFalse <| by simp | (a : α), (b : α) => decidable_of_iff' _ coe_le_coe instance decidableLT [LT α] [DecidableLT α] : DecidableLT (WithTop α) | ⊤, _ => isFalse <| by simp | (a : α), ⊤ => isTrue <| by simp | (a : α), (b : α) => decidable_of_iff' _ coe_lt_coe instance isTotal_le [LE α] [IsTotal α (· ≤ ·)] : IsTotal (WithTop α) (· ≤ ·) where total x y := by cases x <;> cases y <;> simp; simpa using IsTotal.total .. section LinearOrder variable [LinearOrder α] {x y : WithTop α} instance linearOrder [LinearOrder α] : LinearOrder (WithTop α) := Lattice.toLinearOrder _ @[simp, norm_cast] lemma coe_min (a b : α) : ↑(min a b) = min (a : WithTop α) b := rfl @[simp, norm_cast] lemma coe_max (a b : α) : ↑(max a b) = max (a : WithTop α) b := rfl variable [DenselyOrdered α] [NoMaxOrder α] lemma le_of_forall_lt_iff_le : (∀ b : α, x < b → y ≤ b) ↔ y ≤ x := by cases x <;> cases y <;> simp [exists_gt, forall_gt_imp_ge_iff_le_of_dense] lemma ge_of_forall_gt_iff_ge : (∀ a : α, a < x → a ≤ y) ↔ x ≤ y := by cases x <;> cases y <;> simp [exists_gt, forall_lt_imp_le_iff_le_of_dense] end LinearOrder instance instWellFoundedLT [LT α] [WellFoundedLT α] : WellFoundedLT (WithTop α) := inferInstanceAs <| WellFoundedLT (WithBot αᵒᵈ)ᵒᵈ open OrderDual instance instWellFoundedGT [LT α] [WellFoundedGT α] : WellFoundedGT (WithTop α) := inferInstanceAs <| WellFoundedGT (WithBot αᵒᵈ)ᵒᵈ instance trichotomous.lt [Preorder α] [IsTrichotomous α (· < ·)] : IsTrichotomous (WithTop α) (· < ·) where trichotomous x y := by cases x <;> cases y <;> simp [trichotomous] instance IsWellOrder.lt [Preorder α] [IsWellOrder α (· < ·)] : IsWellOrder (WithTop α) (· < ·) where instance trichotomous.gt [Preorder α] [IsTrichotomous α (· > ·)] : IsTrichotomous (WithTop α) (· > ·) := have : IsTrichotomous α (· < ·) := .swap _; .swap _ instance IsWellOrder.gt [Preorder α] [IsWellOrder α (· > ·)] : IsWellOrder (WithTop α) (· > ·) where instance _root_.WithBot.trichotomous.lt [Preorder α] [h : IsTrichotomous α (· < ·)] : IsTrichotomous (WithBot α) (· < ·) where trichotomous x y := by cases x <;> cases y <;> simp [trichotomous] instance _root_.WithBot.isWellOrder.lt [Preorder α] [IsWellOrder α (· < ·)] : IsWellOrder (WithBot α) (· < ·) where instance _root_.WithBot.trichotomous.gt [Preorder α] [h : IsTrichotomous α (· > ·)] : IsTrichotomous (WithBot α) (· > ·) where trichotomous x y := by cases x <;> cases y <;> simp; simpa using trichotomous_of (· > ·) .. instance _root_.WithBot.isWellOrder.gt [Preorder α] [h : IsWellOrder α (· > ·)] : IsWellOrder (WithBot α) (· > ·) where trichotomous x y := by cases x <;> cases y <;> simp; simpa using trichotomous_of (· > ·) .. instance [LT α] [DenselyOrdered α] [NoMaxOrder α] : DenselyOrdered (WithTop α) := OrderDual.denselyOrdered (WithBot αᵒᵈ) theorem lt_iff_exists_coe_btwn [Preorder α] [DenselyOrdered α] [NoMaxOrder α] {a b : WithTop α} : a < b ↔ ∃ x : α, a < ↑x ∧ ↑x < b := ⟨fun h => let ⟨_, hy⟩ := exists_between h let ⟨x, hx⟩ := lt_iff_exists_coe.1 hy.2 ⟨x, hx.1 ▸ hy⟩, fun ⟨_, hx⟩ => lt_trans hx.1 hx.2⟩ instance noBotOrder [LE α] [NoBotOrder α] [Nonempty α] : NoBotOrder (WithTop α) where exists_not_ge := fun | ⊤ => ‹Nonempty α›.elim fun a ↦ ⟨a, by simp⟩ | (a : α) => let ⟨b, hba⟩ := exists_not_ge a; ⟨b, mod_cast hba⟩ instance noMinOrder [LT α] [NoMinOrder α] [Nonempty α] : NoMinOrder (WithTop α) where exists_lt := fun | ⊤ => ‹Nonempty α›.elim fun a ↦ ⟨a, by simp⟩ | (a : α) => let ⟨b, hab⟩ := exists_lt a; ⟨b, mod_cast hab⟩ end WithTop section WithBotWithTop lemma WithBot.eq_top_iff_forall_ge [Preorder α] [Nonempty α] [NoTopOrder α] {x : WithBot (WithTop α)} : x = ⊤ ↔ ∀ a : α, a ≤ x := by refine ⟨by aesop, fun H ↦ ?_⟩ induction x · simp at H · simpa [WithTop.eq_top_iff_forall_ge] using H end WithBotWithTop /-! ### `(WithBot α)ᵒᵈ ≃ WithTop αᵒᵈ`, `(WithTop α)ᵒᵈ ≃ WithBot αᵒᵈ` -/ open OrderDual namespace WithBot @[simp] lemma toDual_symm_apply (a : WithTop αᵒᵈ) : WithBot.toDual.symm a = WithTop.ofDual a := rfl @[simp] lemma ofDual_symm_apply (a : WithTop α) : WithBot.ofDual.symm a = WithTop.toDual a := rfl @[simp] lemma toDual_apply_bot : WithBot.toDual (⊥ : WithBot α) = ⊤ := rfl @[simp] lemma ofDual_apply_bot : WithBot.ofDual (⊥ : WithBot α) = ⊤ := rfl @[simp] lemma toDual_apply_coe (a : α) : WithBot.toDual (a : WithBot α) = toDual a := rfl @[simp] lemma ofDual_apply_coe (a : αᵒᵈ) : WithBot.ofDual (a : WithBot αᵒᵈ) = ofDual a := rfl lemma map_toDual (f : αᵒᵈ → βᵒᵈ) (a : WithTop α) : WithBot.map f (WithTop.toDual a) = a.map (toDual ∘ f) := rfl lemma map_ofDual (f : α → β) (a : WithTop αᵒᵈ) : WithBot.map f (WithTop.ofDual a) = a.map (ofDual ∘ f) := rfl lemma toDual_map (f : α → β) (a : WithBot α) : WithBot.toDual (WithBot.map f a) = map (toDual ∘ f ∘ ofDual) (WithBot.toDual a) := rfl lemma ofDual_map (f : αᵒᵈ → βᵒᵈ) (a : WithBot αᵒᵈ) : WithBot.ofDual (WithBot.map f a) = map (ofDual ∘ f ∘ toDual) (WithBot.ofDual a) := rfl end WithBot section LE variable [LE α] lemma WithBot.toDual_le_iff {x : WithBot α} {y : WithTop αᵒᵈ} : x.toDual ≤ y ↔ WithTop.ofDual y ≤ x := by cases x <;> cases y <;> simp [toDual_le] lemma WithBot.le_toDual_iff {x : WithTop αᵒᵈ} {y : WithBot α} : x ≤ WithBot.toDual y ↔ y ≤ WithTop.ofDual x := by cases x <;> cases y <;> simp [le_toDual] @[simp] lemma WithBot.toDual_le_toDual_iff {x y : WithBot α} : x.toDual ≤ y.toDual ↔ y ≤ x := by cases x <;> cases y <;> simp lemma WithBot.ofDual_le_iff {x : WithBot αᵒᵈ} {y : WithTop α} : WithBot.ofDual x ≤ y ↔ y.toDual ≤ x := by cases x <;> cases y <;> simp [toDual_le] lemma WithBot.le_ofDual_iff {x : WithTop α} {y : WithBot αᵒᵈ} : x ≤ WithBot.ofDual y ↔ y ≤ x.toDual := by cases x <;> cases y <;> simp [le_toDual] @[simp] lemma WithBot.ofDual_le_ofDual_iff {x y : WithBot αᵒᵈ} : WithBot.ofDual x ≤ WithBot.ofDual y ↔ y ≤ x := by cases x <;> cases y <;> simp lemma WithTop.toDual_le_iff {x : WithTop α} {y : WithBot αᵒᵈ} : x.toDual ≤ y ↔ WithBot.ofDual y ≤ x := by cases x <;> cases y <;> simp [toDual_le] lemma WithTop.le_toDual_iff {x : WithBot αᵒᵈ} {y : WithTop α} : x ≤ WithTop.toDual y ↔ y ≤ WithBot.ofDual x := by cases x <;> cases y <;> simp [le_toDual] @[simp] lemma WithTop.toDual_le_toDual_iff {x y : WithTop α} : x.toDual ≤ y.toDual ↔ y ≤ x := by cases x <;> cases y <;> simp [le_toDual] lemma WithTop.ofDual_le_iff {x : WithTop αᵒᵈ} {y : WithBot α} : WithTop.ofDual x ≤ y ↔ y.toDual ≤ x := by cases x <;> cases y <;> simp [toDual_le] lemma WithTop.le_ofDual_iff {x : WithBot α} {y : WithTop αᵒᵈ} : x ≤ WithTop.ofDual y ↔ y ≤ x.toDual := by cases x <;> cases y <;> simp [le_toDual] @[simp] lemma WithTop.ofDual_le_ofDual_iff {x y : WithTop αᵒᵈ} : WithTop.ofDual x ≤ WithTop.ofDual y ↔ y ≤ x := by cases x <;> cases y <;> simp end LE section LT variable [LT α] lemma WithBot.toDual_lt_iff {x : WithBot α} {y : WithTop αᵒᵈ} : x.toDual < y ↔ WithTop.ofDual y < x := by cases x <;> cases y <;> simp [toDual_lt] lemma WithBot.lt_toDual_iff {x : WithTop αᵒᵈ} {y : WithBot α} : x < y.toDual ↔ y < WithTop.ofDual x := by cases x <;> cases y <;> simp [lt_toDual] @[simp] lemma WithBot.toDual_lt_toDual_iff {x y : WithBot α} : x.toDual < y.toDual ↔ y < x := by cases x <;> cases y <;> simp lemma WithBot.ofDual_lt_iff {x : WithBot αᵒᵈ} {y : WithTop α} : WithBot.ofDual x < y ↔ y.toDual < x := by cases x <;> cases y <;> simp [toDual_lt] lemma WithBot.lt_ofDual_iff {x : WithTop α} {y : WithBot αᵒᵈ} : x < WithBot.ofDual y ↔ y < x.toDual := by cases x <;> cases y <;> simp [lt_toDual] @[simp] lemma WithBot.ofDual_lt_ofDual_iff {x y : WithBot αᵒᵈ} : WithBot.ofDual x < WithBot.ofDual y ↔ y < x := by cases x <;> cases y <;> simp lemma WithTop.toDual_lt_iff {x : WithTop α} {y : WithBot αᵒᵈ} : WithTop.toDual x < y ↔ WithBot.ofDual y < x := by cases x <;> cases y <;> simp [toDual_lt] lemma WithTop.lt_toDual_iff {x : WithBot αᵒᵈ} {y : WithTop α} : x < WithTop.toDual y ↔ y < WithBot.ofDual x := by cases x <;> cases y <;> simp [lt_toDual] @[simp] lemma WithTop.toDual_lt_toDual_iff {x y : WithTop α} : WithTop.toDual x < WithTop.toDual y ↔ y < x := by cases x <;> cases y <;> simp lemma WithTop.ofDual_lt_iff {x : WithTop αᵒᵈ} {y : WithBot α} : WithTop.ofDual x < y ↔ WithBot.toDual y < x := by cases x <;> cases y <;> simp [toDual_lt] lemma WithTop.lt_ofDual_iff {x : WithBot α} {y : WithTop αᵒᵈ} : x < WithTop.ofDual y ↔ y < WithBot.toDual x := by cases x <;> cases y <;> simp [lt_toDual] @[simp] lemma WithTop.ofDual_lt_ofDual_iff {x y : WithTop αᵒᵈ} : WithTop.ofDual x < WithTop.ofDual y ↔ y < x := by cases x <;> cases y <;> simp end LT
Mathlib/Order/WithBot.lean
1,163
1,165
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.Data.Stream.Defs import Mathlib.Logic.Function.Basic import Mathlib.Data.List.Defs import Mathlib.Data.Nat.Basic import Mathlib.Tactic.Common /-! # Streams a.k.a. infinite lists a.k.a. infinite sequences -/ open Nat Function Option namespace Stream' universe u v w variable {α : Type u} {β : Type v} {δ : Type w} variable (m n : ℕ) (x y : List α) (a b : Stream' α) instance [Inhabited α] : Inhabited (Stream' α) := ⟨Stream'.const default⟩ @[simp] protected theorem eta (s : Stream' α) : head s :: tail s = s := funext fun i => by cases i <;> rfl /-- Alias for `Stream'.eta` to match `List` API. -/ alias cons_head_tail := Stream'.eta @[ext] protected theorem ext {s₁ s₂ : Stream' α} : (∀ n, get s₁ n = get s₂ n) → s₁ = s₂ := fun h => funext h @[simp] theorem get_zero_cons (a : α) (s : Stream' α) : get (a::s) 0 = a := rfl @[simp] theorem head_cons (a : α) (s : Stream' α) : head (a::s) = a := rfl @[simp] theorem tail_cons (a : α) (s : Stream' α) : tail (a::s) = s := rfl @[simp] theorem get_drop (n m : ℕ) (s : Stream' α) : get (drop m s) n = get s (m + n) := by rw [Nat.add_comm] rfl theorem tail_eq_drop (s : Stream' α) : tail s = drop 1 s := rfl @[simp] theorem drop_drop (n m : ℕ) (s : Stream' α) : drop n (drop m s) = drop (m + n) s := by ext; simp [Nat.add_assoc] @[simp] theorem get_tail {n : ℕ} {s : Stream' α} : s.tail.get n = s.get (n + 1) := rfl @[simp] theorem tail_drop' {i : ℕ} {s : Stream' α} : tail (drop i s) = s.drop (i + 1) := by ext; simp [Nat.add_comm, Nat.add_assoc, Nat.add_left_comm] @[simp] theorem drop_tail' {i : ℕ} {s : Stream' α} : drop i (tail s) = s.drop (i + 1) := rfl theorem tail_drop (n : ℕ) (s : Stream' α) : tail (drop n s) = drop n (tail s) := by simp theorem get_succ (n : ℕ) (s : Stream' α) : get s (succ n) = get (tail s) n := rfl @[simp] theorem get_succ_cons (n : ℕ) (s : Stream' α) (x : α) : get (x :: s) n.succ = get s n := rfl @[simp] lemma get_cons_append_zero {a : α} {x : List α} {s : Stream' α} : (a :: x ++ₛ s).get 0 = a := rfl @[simp] lemma append_eq_cons {a : α} {as : Stream' α} : [a] ++ₛ as = a :: as := by rfl @[simp] theorem drop_zero {s : Stream' α} : s.drop 0 = s := rfl theorem drop_succ (n : ℕ) (s : Stream' α) : drop (succ n) s = drop n (tail s) := rfl theorem head_drop (a : Stream' α) (n : ℕ) : (a.drop n).head = a.get n := by simp theorem cons_injective2 : Function.Injective2 (cons : α → Stream' α → Stream' α) := fun x y s t h => ⟨by rw [← get_zero_cons x s, h, get_zero_cons], Stream'.ext fun n => by rw [← get_succ_cons n _ x, h, get_succ_cons]⟩ theorem cons_injective_left (s : Stream' α) : Function.Injective fun x => cons x s := cons_injective2.left _ theorem cons_injective_right (x : α) : Function.Injective (cons x) := cons_injective2.right _ theorem all_def (p : α → Prop) (s : Stream' α) : All p s = ∀ n, p (get s n) := rfl theorem any_def (p : α → Prop) (s : Stream' α) : Any p s = ∃ n, p (get s n) := rfl @[simp] theorem mem_cons (a : α) (s : Stream' α) : a ∈ a::s := Exists.intro 0 rfl theorem mem_cons_of_mem {a : α} {s : Stream' α} (b : α) : a ∈ s → a ∈ b::s := fun ⟨n, h⟩ => Exists.intro (succ n) (by rw [get_succ, tail_cons, h]) theorem eq_or_mem_of_mem_cons {a b : α} {s : Stream' α} : (a ∈ b::s) → a = b ∨ a ∈ s := fun ⟨n, h⟩ => by rcases n with - | n' · left exact h · right rw [get_succ, tail_cons] at h exact ⟨n', h⟩ theorem mem_of_get_eq {n : ℕ} {s : Stream' α} {a : α} : a = get s n → a ∈ s := fun h => Exists.intro n h section Map variable (f : α → β) theorem drop_map (n : ℕ) (s : Stream' α) : drop n (map f s) = map f (drop n s) := Stream'.ext fun _ => rfl @[simp] theorem get_map (n : ℕ) (s : Stream' α) : get (map f s) n = f (get s n) := rfl theorem tail_map (s : Stream' α) : tail (map f s) = map f (tail s) := rfl @[simp] theorem head_map (s : Stream' α) : head (map f s) = f (head s) := rfl theorem map_eq (s : Stream' α) : map f s = f (head s)::map f (tail s) := by rw [← Stream'.eta (map f s), tail_map, head_map] theorem map_cons (a : α) (s : Stream' α) : map f (a::s) = f a::map f s := by rw [← Stream'.eta (map f (a::s)), map_eq]; rfl @[simp] theorem map_id (s : Stream' α) : map id s = s := rfl @[simp] theorem map_map (g : β → δ) (f : α → β) (s : Stream' α) : map g (map f s) = map (g ∘ f) s := rfl @[simp] theorem map_tail (s : Stream' α) : map f (tail s) = tail (map f s) := rfl theorem mem_map {a : α} {s : Stream' α} : a ∈ s → f a ∈ map f s := fun ⟨n, h⟩ => Exists.intro n (by rw [get_map, h]) theorem exists_of_mem_map {f} {b : β} {s : Stream' α} : b ∈ map f s → ∃ a, a ∈ s ∧ f a = b := fun ⟨n, h⟩ => ⟨get s n, ⟨n, rfl⟩, h.symm⟩ end Map section Zip variable (f : α → β → δ) theorem drop_zip (n : ℕ) (s₁ : Stream' α) (s₂ : Stream' β) : drop n (zip f s₁ s₂) = zip f (drop n s₁) (drop n s₂) := Stream'.ext fun _ => rfl @[simp] theorem get_zip (n : ℕ) (s₁ : Stream' α) (s₂ : Stream' β) : get (zip f s₁ s₂) n = f (get s₁ n) (get s₂ n) := rfl theorem head_zip (s₁ : Stream' α) (s₂ : Stream' β) : head (zip f s₁ s₂) = f (head s₁) (head s₂) := rfl theorem tail_zip (s₁ : Stream' α) (s₂ : Stream' β) : tail (zip f s₁ s₂) = zip f (tail s₁) (tail s₂) := rfl theorem zip_eq (s₁ : Stream' α) (s₂ : Stream' β) : zip f s₁ s₂ = f (head s₁) (head s₂)::zip f (tail s₁) (tail s₂) := by rw [← Stream'.eta (zip f s₁ s₂)]; rfl @[simp] theorem get_enum (s : Stream' α) (n : ℕ) : get (enum s) n = (n, s.get n) := rfl theorem enum_eq_zip (s : Stream' α) : enum s = zip Prod.mk nats s := rfl end Zip @[simp] theorem mem_const (a : α) : a ∈ const a := Exists.intro 0 rfl theorem const_eq (a : α) : const a = a::const a := by apply Stream'.ext; intro n cases n <;> rfl @[simp] theorem tail_const (a : α) : tail (const a) = const a := suffices tail (a::const a) = const a by rwa [← const_eq] at this rfl @[simp] theorem map_const (f : α → β) (a : α) : map f (const a) = const (f a) := rfl @[simp] theorem get_const (n : ℕ) (a : α) : get (const a) n = a := rfl @[simp] theorem drop_const (n : ℕ) (a : α) : drop n (const a) = const a := Stream'.ext fun _ => rfl @[simp] theorem head_iterate (f : α → α) (a : α) : head (iterate f a) = a := rfl theorem get_succ_iterate' (n : ℕ) (f : α → α) (a : α) : get (iterate f a) (succ n) = f (get (iterate f a) n) := rfl theorem tail_iterate (f : α → α) (a : α) : tail (iterate f a) = iterate f (f a) := by ext n rw [get_tail] induction' n with n' ih · rfl · rw [get_succ_iterate', ih, get_succ_iterate'] theorem iterate_eq (f : α → α) (a : α) : iterate f a = a::iterate f (f a) := by rw [← Stream'.eta (iterate f a)] rw [tail_iterate]; rfl @[simp] theorem get_zero_iterate (f : α → α) (a : α) : get (iterate f a) 0 = a := rfl theorem get_succ_iterate (n : ℕ) (f : α → α) (a : α) : get (iterate f a) (succ n) = get (iterate f (f a)) n := by rw [get_succ, tail_iterate] section Bisim variable (R : Stream' α → Stream' α → Prop) /-- equivalence relation -/ local infixl:50 " ~ " => R /-- Streams `s₁` and `s₂` are defined to be bisimulations if their heads are equal and tails are bisimulations. -/ def IsBisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → head s₁ = head s₂ ∧ tail s₁ ~ tail s₂ theorem get_of_bisim (bisim : IsBisimulation R) {s₁ s₂} : ∀ n, s₁ ~ s₂ → get s₁ n = get s₂ n ∧ drop (n + 1) s₁ ~ drop (n + 1) s₂ | 0, h => bisim h | n + 1, h => match bisim h with | ⟨_, trel⟩ => get_of_bisim bisim n trel -- If two streams are bisimilar, then they are equal theorem eq_of_bisim (bisim : IsBisimulation R) {s₁ s₂} : s₁ ~ s₂ → s₁ = s₂ := fun r => Stream'.ext fun n => And.left (get_of_bisim R bisim n r) end Bisim theorem bisim_simple (s₁ s₂ : Stream' α) : head s₁ = head s₂ → s₁ = tail s₁ → s₂ = tail s₂ → s₁ = s₂ := fun hh ht₁ ht₂ => eq_of_bisim (fun s₁ s₂ => head s₁ = head s₂ ∧ s₁ = tail s₁ ∧ s₂ = tail s₂) (fun s₁ s₂ ⟨h₁, h₂, h₃⟩ => by constructor · exact h₁ rw [← h₂, ← h₃] (repeat' constructor) <;> assumption) (And.intro hh (And.intro ht₁ ht₂)) theorem coinduction {s₁ s₂ : Stream' α} : head s₁ = head s₂ → (∀ (β : Type u) (fr : Stream' α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂ := fun hh ht => eq_of_bisim (fun s₁ s₂ => head s₁ = head s₂ ∧ ∀ (β : Type u) (fr : Stream' α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) (fun s₁ s₂ h => have h₁ : head s₁ = head s₂ := And.left h have h₂ : head (tail s₁) = head (tail s₂) := And.right h α (@head α) h₁ have h₃ : ∀ (β : Type u) (fr : Stream' α → β), fr (tail s₁) = fr (tail s₂) → fr (tail (tail s₁)) = fr (tail (tail s₂)) := fun β fr => And.right h β fun s => fr (tail s) And.intro h₁ (And.intro h₂ h₃)) (And.intro hh ht) @[simp] theorem iterate_id (a : α) : iterate id a = const a := coinduction rfl fun β fr ch => by rw [tail_iterate, tail_const]; exact ch theorem map_iterate (f : α → α) (a : α) : iterate f (f a) = map f (iterate f a) := by funext n induction' n with n' ih · rfl · unfold map iterate get rw [map, get] at ih rw [iterate] exact congrArg f ih section Corec theorem corec_def (f : α → β) (g : α → α) (a : α) : corec f g a = map f (iterate g a) := rfl theorem corec_eq (f : α → β) (g : α → α) (a : α) : corec f g a = f a :: corec f g (g a) := by rw [corec_def, map_eq, head_iterate, tail_iterate]; rfl theorem corec_id_id_eq_const (a : α) : corec id id a = const a := by rw [corec_def, map_id, iterate_id] theorem corec_id_f_eq_iterate (f : α → α) (a : α) : corec id f a = iterate f a := rfl end Corec section Corec' theorem corec'_eq (f : α → β × α) (a : α) : corec' f a = (f a).1 :: corec' f (f a).2 := corec_eq _ _ _ end Corec' theorem unfolds_eq (g : α → β) (f : α → α) (a : α) : unfolds g f a = g a :: unfolds g f (f a) := by unfold unfolds; rw [corec_eq] theorem get_unfolds_head_tail : ∀ (n : ℕ) (s : Stream' α), get (unfolds head tail s) n = get s n := by intro n; induction' n with n' ih · intro s rfl · intro s rw [get_succ, get_succ, unfolds_eq, tail_cons, ih] theorem unfolds_head_eq : ∀ s : Stream' α, unfolds head tail s = s := fun s => Stream'.ext fun n => get_unfolds_head_tail n s theorem interleave_eq (s₁ s₂ : Stream' α) : s₁ ⋈ s₂ = head s₁::head s₂::(tail s₁ ⋈ tail s₂) := by let t := tail s₁ ⋈ tail s₂ show s₁ ⋈ s₂ = head s₁::head s₂::t unfold interleave; unfold corecOn; rw [corec_eq]; dsimp; rw [corec_eq]; rfl theorem tail_interleave (s₁ s₂ : Stream' α) : tail (s₁ ⋈ s₂) = s₂ ⋈ tail s₁ := by unfold interleave corecOn; rw [corec_eq]; rfl theorem interleave_tail_tail (s₁ s₂ : Stream' α) : tail s₁ ⋈ tail s₂ = tail (tail (s₁ ⋈ s₂)) := by rw [interleave_eq s₁ s₂]; rfl theorem get_interleave_left : ∀ (n : ℕ) (s₁ s₂ : Stream' α), get (s₁ ⋈ s₂) (2 * n) = get s₁ n | 0, _, _ => rfl | n + 1, s₁, s₂ => by change get (s₁ ⋈ s₂) (succ (succ (2 * n))) = get s₁ (succ n) rw [get_succ, get_succ, interleave_eq, tail_cons, tail_cons] rw [get_interleave_left n (tail s₁) (tail s₂)] rfl theorem get_interleave_right : ∀ (n : ℕ) (s₁ s₂ : Stream' α), get (s₁ ⋈ s₂) (2 * n + 1) = get s₂ n | 0, _, _ => rfl | n + 1, s₁, s₂ => by change get (s₁ ⋈ s₂) (succ (succ (2 * n + 1))) = get s₂ (succ n) rw [get_succ, get_succ, interleave_eq, tail_cons, tail_cons, get_interleave_right n (tail s₁) (tail s₂)] rfl theorem mem_interleave_left {a : α} {s₁ : Stream' α} (s₂ : Stream' α) : a ∈ s₁ → a ∈ s₁ ⋈ s₂ := fun ⟨n, h⟩ => Exists.intro (2 * n) (by rw [h, get_interleave_left]) theorem mem_interleave_right {a : α} {s₁ : Stream' α} (s₂ : Stream' α) : a ∈ s₂ → a ∈ s₁ ⋈ s₂ := fun ⟨n, h⟩ => Exists.intro (2 * n + 1) (by rw [h, get_interleave_right]) theorem odd_eq (s : Stream' α) : odd s = even (tail s) := rfl @[simp] theorem head_even (s : Stream' α) : head (even s) = head s := rfl theorem tail_even (s : Stream' α) : tail (even s) = even (tail (tail s)) := by unfold even rw [corec_eq] rfl theorem even_cons_cons (a₁ a₂ : α) (s : Stream' α) : even (a₁::a₂::s) = a₁::even s := by unfold even rw [corec_eq]; rfl theorem even_tail (s : Stream' α) : even (tail s) = odd s := rfl theorem even_interleave (s₁ s₂ : Stream' α) : even (s₁ ⋈ s₂) = s₁ := eq_of_bisim (fun s₁' s₁ => ∃ s₂, s₁' = even (s₁ ⋈ s₂)) (fun s₁' s₁ ⟨s₂, h₁⟩ => by rw [h₁] constructor · rfl · exact ⟨tail s₂, by rw [interleave_eq, even_cons_cons, tail_cons]⟩) (Exists.intro s₂ rfl) theorem interleave_even_odd (s₁ : Stream' α) : even s₁ ⋈ odd s₁ = s₁ := eq_of_bisim (fun s' s => s' = even s ⋈ odd s) (fun s' s (h : s' = even s ⋈ odd s) => by rw [h]; constructor · rfl · simp [odd_eq, odd_eq, tail_interleave, tail_even]) rfl theorem get_even : ∀ (n : ℕ) (s : Stream' α), get (even s) n = get s (2 * n) | 0, _ => rfl | succ n, s => by change get (even s) (succ n) = get s (succ (succ (2 * n))) rw [get_succ, get_succ, tail_even, get_even n]; rfl theorem get_odd : ∀ (n : ℕ) (s : Stream' α), get (odd s) n = get s (2 * n + 1) := fun n s => by rw [odd_eq, get_even]; rfl theorem mem_of_mem_even (a : α) (s : Stream' α) : a ∈ even s → a ∈ s := fun ⟨n, h⟩ => Exists.intro (2 * n) (by rw [h, get_even]) theorem mem_of_mem_odd (a : α) (s : Stream' α) : a ∈ odd s → a ∈ s := fun ⟨n, h⟩ => Exists.intro (2 * n + 1) (by rw [h, get_odd]) @[simp] theorem nil_append_stream (s : Stream' α) : appendStream' [] s = s := rfl theorem cons_append_stream (a : α) (l : List α) (s : Stream' α) : appendStream' (a::l) s = a::appendStream' l s := rfl @[simp] theorem append_append_stream : ∀ (l₁ l₂ : List α) (s : Stream' α), l₁ ++ l₂ ++ₛ s = l₁ ++ₛ (l₂ ++ₛ s) | [], _, _ => rfl | List.cons a l₁, l₂, s => by rw [List.cons_append, cons_append_stream, cons_append_stream, append_append_stream l₁] lemma get_append_left (h : n < x.length) : (x ++ₛ a).get n = x[n] := by induction' x with b x ih generalizing n · simp at h · rcases n with (_ | n) · simp · simp [ih n (by simpa using h), cons_append_stream] @[simp] lemma get_append_right : (x ++ₛ a).get (x.length + n) = a.get n := by induction' x <;> simp [Nat.succ_add, *, cons_append_stream] @[simp] lemma get_append_length : (x ++ₛ a).get x.length = a.get 0 := get_append_right 0 x a lemma append_right_injective (h : x ++ₛ a = x ++ₛ b) : a = b := by ext n; replace h := congr_arg (fun a ↦ a.get (x.length + n)) h; simpa using h @[simp] lemma append_right_inj : x ++ₛ a = x ++ₛ b ↔ a = b := ⟨append_right_injective x a b, by simp +contextual⟩ lemma append_left_injective (h : x ++ₛ a = y ++ₛ b) (hl : x.length = y.length) : x = y := by apply List.ext_getElem hl intros rw [← get_append_left, ← get_append_left, h] theorem map_append_stream (f : α → β) : ∀ (l : List α) (s : Stream' α), map f (l ++ₛ s) = List.map f l ++ₛ map f s | [], _ => rfl | List.cons a l, s => by rw [cons_append_stream, List.map_cons, map_cons, cons_append_stream, map_append_stream f l] theorem drop_append_stream : ∀ (l : List α) (s : Stream' α), drop l.length (l ++ₛ s) = s | [], s => by rfl | List.cons a l, s => by rw [List.length_cons, drop_succ, cons_append_stream, tail_cons, drop_append_stream l s] theorem append_stream_head_tail (s : Stream' α) : [head s] ++ₛ tail s = s := by simp theorem mem_append_stream_right : ∀ {a : α} (l : List α) {s : Stream' α}, a ∈ s → a ∈ l ++ₛ s | _, [], _, h => h | a, List.cons _ l, s, h => have ih : a ∈ l ++ₛ s := mem_append_stream_right l h mem_cons_of_mem _ ih theorem mem_append_stream_left : ∀ {a : α} {l : List α} (s : Stream' α), a ∈ l → a ∈ l ++ₛ s | _, [], _, h => absurd h List.not_mem_nil | a, List.cons b l, s, h => Or.elim (List.eq_or_mem_of_mem_cons h) (fun aeqb : a = b => Exists.intro 0 aeqb) fun ainl : a ∈ l => mem_cons_of_mem b (mem_append_stream_left s ainl) @[simp] theorem take_zero (s : Stream' α) : take 0 s = [] := rfl -- This lemma used to be simp, but we removed it from the simp set because: -- 1) It duplicates the (often large) `s` term, resulting in large tactic states. -- 2) It conflicts with the very useful `dropLast_take` lemma below (causing nonconfluence). theorem take_succ (n : ℕ) (s : Stream' α) : take (succ n) s = head s::take n (tail s) := rfl @[simp] theorem take_succ_cons {a : α} (n : ℕ) (s : Stream' α) : take (n+1) (a::s) = a :: take n s := rfl theorem take_succ' {s : Stream' α} : ∀ n, s.take (n+1) = s.take n ++ [s.get n] | 0 => rfl | n+1 => by rw [take_succ, take_succ' n, ← List.cons_append, ← take_succ, get_tail] @[simp] theorem length_take (n : ℕ) (s : Stream' α) : (take n s).length = n := by induction n generalizing s <;> simp [*, take_succ] @[simp] theorem take_take {s : Stream' α} : ∀ {m n}, (s.take n).take m = s.take (min n m) | 0, n => by rw [Nat.min_zero, List.take_zero, take_zero] | m, 0 => by rw [Nat.zero_min, take_zero, List.take_nil] | m+1, n+1 => by rw [take_succ, List.take_succ_cons, Nat.succ_min_succ, take_succ, take_take] @[simp] theorem concat_take_get {n : ℕ} {s : Stream' α} : s.take n ++ [s.get n] = s.take (n + 1) := (take_succ' n).symm theorem getElem?_take {s : Stream' α} : ∀ {k n}, k < n → (s.take n)[k]? = s.get k | 0, _+1, _ => by simp only [length_take, zero_lt_succ, List.getElem?_eq_getElem]; rfl | k+1, n+1, h => by rw [take_succ, List.getElem?_cons_succ, getElem?_take (Nat.lt_of_succ_lt_succ h), get_succ] @[deprecated (since := "2025-02-14")] alias get?_take := getElem?_take theorem getElem?_take_succ (n : ℕ) (s : Stream' α) : (take (succ n) s)[n]? = some (get s n) := getElem?_take (Nat.lt_succ_self n) @[deprecated (since := "2025-02-14")] alias get?_take_succ := getElem?_take_succ @[simp] theorem dropLast_take {n : ℕ} {xs : Stream' α} : (Stream'.take n xs).dropLast = Stream'.take (n-1) xs := by cases n with | zero => simp | succ n => rw [take_succ', List.dropLast_concat, Nat.add_one_sub_one] @[simp] theorem append_take_drop : ∀ (n : ℕ) (s : Stream' α), appendStream' (take n s) (drop n s) = s := by intro n induction' n with n' ih · intro s rfl · intro s rw [take_succ, drop_succ, cons_append_stream, ih (tail s), Stream'.eta] lemma append_take : x ++ (a.take n) = (x ++ₛ a).take (x.length + n) := by induction' x <;> simp [take, Nat.add_comm, cons_append_stream, *] @[simp] lemma take_get (h : m < (a.take n).length) : (a.take n)[m] = a.get m := by nth_rw 2 [← append_take_drop n a]; rw [get_append_left] theorem take_append_of_le_length (h : n ≤ x.length) : (x ++ₛ a).take n = x.take n := by apply List.ext_getElem (by simp [h]) intro _ _ _; rw [List.getElem_take, take_get, get_append_left] lemma take_add : a.take (m + n) = a.take m ++ (a.drop m).take n := by apply append_left_injective _ _ (a.drop (m + n)) ((a.drop m).drop n) <;> simp [- drop_drop] @[gcongr] lemma take_prefix_take_left (h : m ≤ n) : a.take m <+: a.take n := by rw [(by simp [h] : a.take m = (a.take n).take m)] apply List.take_prefix @[simp] lemma take_prefix : a.take m <+: a.take n ↔ m ≤ n := ⟨fun h ↦ by simpa using h.length_le, take_prefix_take_left m n a⟩ lemma map_take (f : α → β) : (a.take n).map f = (a.map f).take n := by apply List.ext_getElem <;> simp lemma take_drop : (a.drop m).take n = (a.take (m + n)).drop m := by apply List.ext_getElem <;> simp lemma drop_append_of_le_length (h : n ≤ x.length) : (x ++ₛ a).drop n = x.drop n ++ₛ a := by obtain ⟨m, hm⟩ := Nat.exists_eq_add_of_le h ext k; rcases lt_or_ge k m with _ | hk · rw [get_drop, get_append_left, get_append_left, List.getElem_drop]; simpa [hm] · obtain ⟨p, rfl⟩ := Nat.exists_eq_add_of_le hk have hm' : m = (x.drop n).length := by simp [hm] simp_rw [get_drop, ← Nat.add_assoc, ← hm, get_append_right, hm', get_append_right] -- Take theorem reduces a proof of equality of infinite streams to an -- induction over all their finite approximations. theorem take_theorem (s₁ s₂ : Stream' α) : (∀ n : ℕ, take n s₁ = take n s₂) → s₁ = s₂ := by intro h; apply Stream'.ext; intro n induction' n with n _ · have aux := h 1 simp? [take] at aux says simp only [take, List.cons.injEq, and_true] at aux exact aux · have h₁ : some (get s₁ (succ n)) = some (get s₂ (succ n)) := by rw [← getElem?_take_succ, ← getElem?_take_succ, h (succ (succ n))] injection h₁ protected theorem cycle_g_cons (a : α) (a₁ : α) (l₁ : List α) (a₀ : α) (l₀ : List α) : Stream'.cycleG (a, a₁::l₁, a₀, l₀) = (a₁, l₁, a₀, l₀) := rfl theorem cycle_eq : ∀ (l : List α) (h : l ≠ []), cycle l h = l ++ₛ cycle l h | [], h => absurd rfl h | List.cons a l, _ => have gen : ∀ l' a', corec Stream'.cycleF Stream'.cycleG (a', l', a, l) = (a'::l') ++ₛ corec Stream'.cycleF Stream'.cycleG (a, l, a, l) := by intro l' induction' l' with a₁ l₁ ih · intros rw [corec_eq] rfl · intros rw [corec_eq, Stream'.cycle_g_cons, ih a₁] rfl gen l a theorem mem_cycle {a : α} {l : List α} : ∀ h : l ≠ [], a ∈ l → a ∈ cycle l h := fun h ainl => by rw [cycle_eq]; exact mem_append_stream_left _ ainl @[simp] theorem cycle_singleton (a : α) : cycle [a] (by simp) = const a := coinduction rfl fun β fr ch => by rwa [cycle_eq, const_eq] theorem tails_eq (s : Stream' α) : tails s = tail s::tails (tail s) := by unfold tails; rw [corec_eq]; rfl @[simp] theorem get_tails : ∀ (n : ℕ) (s : Stream' α), get (tails s) n = drop n (tail s) := by intro n; induction' n with n' ih · intros rfl · intro s rw [get_succ, drop_succ, tails_eq, tail_cons, ih] theorem tails_eq_iterate (s : Stream' α) : tails s = iterate tail (tail s) := rfl theorem inits_core_eq (l : List α) (s : Stream' α) : initsCore l s = l::initsCore (l ++ [head s]) (tail s) := by unfold initsCore corecOn rw [corec_eq] theorem tail_inits (s : Stream' α) : tail (inits s) = initsCore [head s, head (tail s)] (tail (tail s)) := by unfold inits rw [inits_core_eq]; rfl theorem inits_tail (s : Stream' α) : inits (tail s) = initsCore [head (tail s)] (tail (tail s)) := rfl theorem cons_get_inits_core : ∀ (a : α) (n : ℕ) (l : List α) (s : Stream' α), (a::get (initsCore l s) n) = get (initsCore (a::l) s) n := by intro a n induction' n with n' ih · intros rfl · intro l s rw [get_succ, inits_core_eq, tail_cons, ih, inits_core_eq (a::l) s] rfl @[simp] theorem get_inits : ∀ (n : ℕ) (s : Stream' α), get (inits s) n = take (succ n) s := by intro n; induction' n with n' ih · intros rfl · intros rw [get_succ, take_succ, ← ih, tail_inits, inits_tail, cons_get_inits_core] theorem inits_eq (s : Stream' α) : inits s = [head s]::map (List.cons (head s)) (inits (tail s)) := by apply Stream'.ext; intro n cases n · rfl · rw [get_inits, get_succ, tail_cons, get_map, get_inits] rfl theorem zip_inits_tails (s : Stream' α) : zip appendStream' (inits s) (tails s) = const s := by apply Stream'.ext; intro n rw [get_zip, get_inits, get_tails, get_const, take_succ, cons_append_stream, append_take_drop, Stream'.eta] theorem identity (s : Stream' α) : pure id ⊛ s = s := rfl theorem composition (g : Stream' (β → δ)) (f : Stream' (α → β)) (s : Stream' α) : pure comp ⊛ g ⊛ f ⊛ s = g ⊛ (f ⊛ s) :=
rfl theorem homomorphism (f : α → β) (a : α) : pure f ⊛ pure a = pure (f a) := rfl theorem interchange (fs : Stream' (α → β)) (a : α) : fs ⊛ pure a = (pure fun f : α → β => f a) ⊛ fs := rfl theorem map_eq_apply (f : α → β) (s : Stream' α) : map f s = pure f ⊛ s :=
Mathlib/Data/Stream/Init.lean
702
711
/- 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) :
Mathlib/Algebra/Order/Rearrangement.lean
152
156
/- 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 -/ import Mathlib.Analysis.Complex.Asymptotics import Mathlib.Analysis.SpecificLimits.Normed import Mathlib.Data.Complex.Trigonometric /-! # Complex and real exponential In this file we prove continuity of `Complex.exp` and `Real.exp`. We also prove a few facts about limits of `Real.exp` at infinity. ## Tags exp -/ noncomputable section open Asymptotics Bornology Finset Filter Function Metric Set Topology open scoped Nat namespace Complex variable {z y x : ℝ} theorem exp_bound_sq (x z : ℂ) (hz : ‖z‖ ≤ 1) : ‖exp (x + z) - exp x - z • exp x‖ ≤ ‖exp x‖ * ‖z‖ ^ 2 := calc ‖exp (x + z) - exp x - z * exp x‖ = ‖exp x * (exp z - 1 - z)‖ := by congr rw [exp_add] ring _ = ‖exp x‖ * ‖exp z - 1 - z‖ := norm_mul _ _ _ ≤ ‖exp x‖ * ‖z‖ ^ 2 := mul_le_mul_of_nonneg_left (norm_exp_sub_one_sub_id_le hz) (norm_nonneg _) theorem locally_lipschitz_exp {r : ℝ} (hr_nonneg : 0 ≤ r) (hr_le : r ≤ 1) (x y : ℂ) (hyx : ‖y - x‖ < r) : ‖exp y - exp x‖ ≤ (1 + r) * ‖exp x‖ * ‖y - x‖ := by have hy_eq : y = x + (y - x) := by abel have hyx_sq_le : ‖y - x‖ ^ 2 ≤ r * ‖y - x‖ := by rw [pow_two] exact mul_le_mul hyx.le le_rfl (norm_nonneg _) hr_nonneg have h_sq : ∀ z, ‖z‖ ≤ 1 → ‖exp (x + z) - exp x‖ ≤ ‖z‖ * ‖exp x‖ + ‖exp x‖ * ‖z‖ ^ 2 := by intro z hz have : ‖exp (x + z) - exp x - z • exp x‖ ≤ ‖exp x‖ * ‖z‖ ^ 2 := exp_bound_sq x z hz rw [← sub_le_iff_le_add', ← norm_smul z] exact (norm_sub_norm_le _ _).trans this calc ‖exp y - exp x‖ = ‖exp (x + (y - x)) - exp x‖ := by nth_rw 1 [hy_eq] _ ≤ ‖y - x‖ * ‖exp x‖ + ‖exp x‖ * ‖y - x‖ ^ 2 := h_sq (y - x) (hyx.le.trans hr_le) _ ≤ ‖y - x‖ * ‖exp x‖ + ‖exp x‖ * (r * ‖y - x‖) := (add_le_add_left (mul_le_mul le_rfl hyx_sq_le (sq_nonneg _) (norm_nonneg _)) _) _ = (1 + r) * ‖exp x‖ * ‖y - x‖ := by ring -- Porting note: proof by term mode `locally_lipschitz_exp zero_le_one le_rfl x` -- doesn't work because `‖y - x‖` and `dist y x` don't unify @[continuity] theorem continuous_exp : Continuous exp := continuous_iff_continuousAt.mpr fun x => continuousAt_of_locally_lipschitz zero_lt_one (2 * ‖exp x‖) (fun y ↦ by convert locally_lipschitz_exp zero_le_one le_rfl x y using 2 congr ring) theorem continuousOn_exp {s : Set ℂ} : ContinuousOn exp s := continuous_exp.continuousOn lemma exp_sub_sum_range_isBigO_pow (n : ℕ) : (fun x ↦ exp x - ∑ i ∈ Finset.range n, x ^ i / i !) =O[𝓝 0] (· ^ n) := by rcases (zero_le n).eq_or_lt with rfl | hn · simpa using continuous_exp.continuousAt.norm.isBoundedUnder_le · refine .of_bound (n.succ / (n ! * n)) ?_ rw [NormedAddCommGroup.nhds_zero_basis_norm_lt.eventually_iff] refine ⟨1, one_pos, fun x hx ↦ ?_⟩ convert exp_bound hx.out.le hn using 1 field_simp [mul_comm] lemma exp_sub_sum_range_succ_isLittleO_pow (n : ℕ) : (fun x ↦ exp x - ∑ i ∈ Finset.range (n + 1), x ^ i / i !) =o[𝓝 0] (· ^ n) := (exp_sub_sum_range_isBigO_pow (n + 1)).trans_isLittleO <| isLittleO_pow_pow n.lt_succ_self end Complex section ComplexContinuousExpComp variable {α : Type*} open Complex theorem Filter.Tendsto.cexp {l : Filter α} {f : α → ℂ} {z : ℂ} (hf : Tendsto f l (𝓝 z)) : Tendsto (fun x => exp (f x)) l (𝓝 (exp z)) := (continuous_exp.tendsto _).comp hf variable [TopologicalSpace α] {f : α → ℂ} {s : Set α} {x : α} nonrec theorem ContinuousWithinAt.cexp (h : ContinuousWithinAt f s x) : ContinuousWithinAt (fun y => exp (f y)) s x := h.cexp @[fun_prop] nonrec theorem ContinuousAt.cexp (h : ContinuousAt f x) : ContinuousAt (fun y => exp (f y)) x := h.cexp @[fun_prop] theorem ContinuousOn.cexp (h : ContinuousOn f s) : ContinuousOn (fun y => exp (f y)) s := fun x hx => (h x hx).cexp @[fun_prop] theorem Continuous.cexp (h : Continuous f) : Continuous fun y => exp (f y) := continuous_iff_continuousAt.2 fun _ => h.continuousAt.cexp /-- The complex exponential function is uniformly continuous on left half planes. -/ lemma UniformContinuousOn.cexp (a : ℝ) : UniformContinuousOn exp {x : ℂ | x.re ≤ a} := by have : Continuous (cexp - 1) := Continuous.sub (Continuous.cexp continuous_id') continuous_one rw [Metric.uniformContinuousOn_iff, Metric.continuous_iff'] at * intro ε hε simp only [gt_iff_lt, Pi.sub_apply, Pi.one_apply, dist_sub_eq_dist_add_right, sub_add_cancel] at this have ha : 0 < ε / (2 * Real.exp a) := by positivity have H := this 0 (ε / (2 * Real.exp a)) ha rw [Metric.eventually_nhds_iff] at H obtain ⟨δ, hδ⟩ := H refine ⟨δ, hδ.1, ?_⟩ intros x _ y hy hxy have h3 := hδ.2 (y := x - y) (by simpa only [dist_zero_right] using hxy) rw [dist_eq_norm, exp_zero] at * have : cexp x - cexp y = cexp y * (cexp (x - y) - 1) := by rw [mul_sub_one, ← exp_add] ring_nf rw [this, mul_comm] have hya : ‖cexp y‖ ≤ Real.exp a := by simp only [norm_exp, Real.exp_le_exp] exact hy simp only [gt_iff_lt, dist_zero_right, Set.mem_setOf_eq, norm_mul, Complex.norm_exp] at * apply lt_of_le_of_lt (mul_le_mul h3.le hya (Real.exp_nonneg y.re) (le_of_lt ha)) have hrr : ε / (2 * a.exp) * a.exp = ε / 2 := by nth_rw 2 [mul_comm] field_simp [mul_assoc] rw [hrr] exact div_two_lt_of_pos hε @[deprecated (since := "2025-02-11")] alias UniformlyContinuousOn.cexp := UniformContinuousOn.cexp end ComplexContinuousExpComp namespace Real @[continuity] theorem continuous_exp : Continuous exp := Complex.continuous_re.comp Complex.continuous_ofReal.cexp theorem continuousOn_exp {s : Set ℝ} : ContinuousOn exp s := continuous_exp.continuousOn lemma exp_sub_sum_range_isBigO_pow (n : ℕ) : (fun x ↦ exp x - ∑ i ∈ Finset.range n, x ^ i / i !) =O[𝓝 0] (· ^ n) := by have := (Complex.exp_sub_sum_range_isBigO_pow n).comp_tendsto (Complex.continuous_ofReal.tendsto' 0 0 rfl) simp only [Function.comp_def] at this norm_cast at this lemma exp_sub_sum_range_succ_isLittleO_pow (n : ℕ) : (fun x ↦ exp x - ∑ i ∈ Finset.range (n + 1), x ^ i / i !) =o[𝓝 0] (· ^ n) := (exp_sub_sum_range_isBigO_pow (n + 1)).trans_isLittleO <| isLittleO_pow_pow n.lt_succ_self end Real section RealContinuousExpComp variable {α : Type*} open Real theorem Filter.Tendsto.rexp {l : Filter α} {f : α → ℝ} {z : ℝ} (hf : Tendsto f l (𝓝 z)) : Tendsto (fun x => exp (f x)) l (𝓝 (exp z)) := (continuous_exp.tendsto _).comp hf variable [TopologicalSpace α] {f : α → ℝ} {s : Set α} {x : α} nonrec theorem ContinuousWithinAt.rexp (h : ContinuousWithinAt f s x) : ContinuousWithinAt (fun y ↦ exp (f y)) s x := h.rexp @[fun_prop] nonrec theorem ContinuousAt.rexp (h : ContinuousAt f x) : ContinuousAt (fun y ↦ exp (f y)) x := h.rexp @[fun_prop] theorem ContinuousOn.rexp (h : ContinuousOn f s) : ContinuousOn (fun y ↦ exp (f y)) s := fun x hx ↦ (h x hx).rexp @[fun_prop] theorem Continuous.rexp (h : Continuous f) : Continuous fun y ↦ exp (f y) := continuous_iff_continuousAt.2 fun _ ↦ h.continuousAt.rexp end RealContinuousExpComp namespace Real variable {α : Type*} {x y z : ℝ} {l : Filter α} theorem exp_half (x : ℝ) : exp (x / 2) = √(exp x) := by rw [eq_comm, sqrt_eq_iff_eq_sq, sq, ← exp_add, add_halves] <;> exact (exp_pos _).le /-- The real exponential function tends to `+∞` at `+∞`. -/ theorem tendsto_exp_atTop : Tendsto exp atTop atTop := by have A : Tendsto (fun x : ℝ => x + 1) atTop atTop := tendsto_atTop_add_const_right atTop 1 tendsto_id have B : ∀ᶠ x in atTop, x + 1 ≤ exp x := eventually_atTop.2 ⟨0, fun x _ => add_one_le_exp x⟩ exact tendsto_atTop_mono' atTop B A /-- The real exponential function tends to `0` at `-∞` or, equivalently, `exp(-x)` tends to `0` at `+∞` -/ theorem tendsto_exp_neg_atTop_nhds_zero : Tendsto (fun x => exp (-x)) atTop (𝓝 0) := (tendsto_inv_atTop_zero.comp tendsto_exp_atTop).congr fun x => (exp_neg x).symm /-- The real exponential function tends to `1` at `0`. -/ theorem tendsto_exp_nhds_zero_nhds_one : Tendsto exp (𝓝 0) (𝓝 1) := by convert continuous_exp.tendsto 0 simp theorem tendsto_exp_atBot : Tendsto exp atBot (𝓝 0) := (tendsto_exp_neg_atTop_nhds_zero.comp tendsto_neg_atBot_atTop).congr fun x => congr_arg exp <| neg_neg x theorem tendsto_exp_atBot_nhdsGT : Tendsto exp atBot (𝓝[>] 0) := tendsto_inf.2 ⟨tendsto_exp_atBot, tendsto_principal.2 <| Eventually.of_forall exp_pos⟩ @[deprecated (since := "2024-12-22")] alias tendsto_exp_atBot_nhdsWithin := tendsto_exp_atBot_nhdsGT @[simp] theorem isBoundedUnder_ge_exp_comp (l : Filter α) (f : α → ℝ) : IsBoundedUnder (· ≥ ·) l fun x => exp (f x) := isBoundedUnder_of ⟨0, fun _ => (exp_pos _).le⟩ @[simp] theorem isBoundedUnder_le_exp_comp {f : α → ℝ} : (IsBoundedUnder (· ≤ ·) l fun x => exp (f x)) ↔ IsBoundedUnder (· ≤ ·) l f := exp_monotone.isBoundedUnder_le_comp_iff tendsto_exp_atTop /-- The function `exp(x)/x^n` tends to `+∞` at `+∞`, for any natural number `n` -/ theorem tendsto_exp_div_pow_atTop (n : ℕ) : Tendsto (fun x => exp x / x ^ n) atTop atTop := by refine (atTop_basis_Ioi.tendsto_iff (atTop_basis' 1)).2 fun C hC₁ => ?_ have hC₀ : 0 < C := zero_lt_one.trans_le hC₁ have : 0 < (exp 1 * C)⁻¹ := inv_pos.2 (mul_pos (exp_pos _) hC₀) obtain ⟨N, hN⟩ : ∃ N : ℕ, ∀ k ≥ N, (↑k : ℝ) ^ n / exp 1 ^ k < (exp 1 * C)⁻¹ := eventually_atTop.1 ((tendsto_pow_const_div_const_pow_of_one_lt n (one_lt_exp_iff.2 zero_lt_one)).eventually (gt_mem_nhds this)) simp only [← exp_nat_mul, mul_one, div_lt_iff₀, exp_pos, ← div_eq_inv_mul] at hN refine ⟨N, trivial, fun x hx => ?_⟩ rw [Set.mem_Ioi] at hx have hx₀ : 0 < x := (Nat.cast_nonneg N).trans_lt hx rw [Set.mem_Ici, le_div_iff₀ (pow_pos hx₀ _), ← le_div_iff₀' hC₀] calc x ^ n ≤ ⌈x⌉₊ ^ n := by gcongr; exact Nat.le_ceil _ _ ≤ exp ⌈x⌉₊ / (exp 1 * C) := mod_cast (hN _ (Nat.lt_ceil.2 hx).le).le _ ≤ exp (x + 1) / (exp 1 * C) := by gcongr; exact (Nat.ceil_lt_add_one hx₀.le).le _ = exp x / C := by rw [add_comm, exp_add, mul_div_mul_left _ _ (exp_pos _).ne'] /-- The function `x^n * exp(-x)` tends to `0` at `+∞`, for any natural number `n`. -/ theorem tendsto_pow_mul_exp_neg_atTop_nhds_zero (n : ℕ) : Tendsto (fun x => x ^ n * exp (-x)) atTop (𝓝 0) := (tendsto_inv_atTop_zero.comp (tendsto_exp_div_pow_atTop n)).congr fun x => by rw [comp_apply, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg]
/-- The function `(b * exp x + c) / (x ^ n)` tends to `+∞` at `+∞`, for any natural number `n` and any real numbers `b` and `c` such that `b` is positive. -/ theorem tendsto_mul_exp_add_div_pow_atTop (b c : ℝ) (n : ℕ) (hb : 0 < b) : Tendsto (fun x => (b * exp x + c) / x ^ n) atTop atTop := by
Mathlib/Analysis/SpecialFunctions/Exp.lean
275
278
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn, Violeta Hernández Palacios -/ import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.SetTheory.Ordinal.FixedPoint /-! # Cofinality This file contains the definition of cofinality of an order and an ordinal number. ## Main Definitions * `Order.cof r` is the cofinality of a reflexive order. This is the smallest cardinality of a subset `s` that is *cofinal*, i.e. `∀ x, ∃ y ∈ s, r x y`. * `Ordinal.cof o` is the cofinality of the ordinal `o` when viewed as a linear order. ## Main Statements * `Cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for `c ≥ ℵ₀`. ## Implementation Notes * The cofinality is defined for ordinals. If `c` is a cardinal number, its cofinality is `c.ord.cof`. -/ noncomputable section open Function Cardinal Set Order open scoped Ordinal universe u v w variable {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} /-! ### Cofinality of orders -/ attribute [local instance] IsRefl.swap namespace Order /-- Cofinality of a reflexive order `≼`. This is the smallest cardinality of a subset `S : Set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/ def cof (r : α → α → Prop) : Cardinal := sInf { c | ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c } /-- The set in the definition of `Order.cof` is nonempty. -/ private theorem cof_nonempty (r : α → α → Prop) [IsRefl α r] : { c | ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c }.Nonempty := ⟨_, Set.univ, fun a => ⟨a, ⟨⟩, refl _⟩, rfl⟩ theorem cof_le (r : α → α → Prop) {S : Set α} (h : ∀ a, ∃ b ∈ S, r a b) : cof r ≤ #S := csInf_le' ⟨S, h, rfl⟩ theorem le_cof [IsRefl α r] (c : Cardinal) : c ≤ cof r ↔ ∀ {S : Set α}, (∀ a, ∃ b ∈ S, r a b) → c ≤ #S := by rw [cof, le_csInf_iff'' (cof_nonempty r)] use fun H S h => H _ ⟨S, h, rfl⟩ rintro H d ⟨S, h, rfl⟩ exact H h end Order namespace RelIso private theorem cof_le_lift [IsRefl β s] (f : r ≃r s) : Cardinal.lift.{v} (Order.cof r) ≤ Cardinal.lift.{u} (Order.cof s) := by rw [Order.cof, Order.cof, lift_sInf, lift_sInf, le_csInf_iff'' ((Order.cof_nonempty s).image _)] rintro - ⟨-, ⟨u, H, rfl⟩, rfl⟩ apply csInf_le' refine ⟨_, ⟨f.symm '' u, fun a => ?_, rfl⟩, lift_mk_eq'.2 ⟨(f.symm.toEquiv.image u).symm⟩⟩ rcases H (f a) with ⟨b, hb, hb'⟩ refine ⟨f.symm b, mem_image_of_mem _ hb, f.map_rel_iff.1 ?_⟩ rwa [RelIso.apply_symm_apply] theorem cof_eq_lift [IsRefl β s] (f : r ≃r s) : Cardinal.lift.{v} (Order.cof r) = Cardinal.lift.{u} (Order.cof s) := have := f.toRelEmbedding.isRefl (f.cof_le_lift).antisymm (f.symm.cof_le_lift) theorem cof_eq {α β : Type u} {r : α → α → Prop} {s} [IsRefl β s] (f : r ≃r s) : Order.cof r = Order.cof s := lift_inj.1 (f.cof_eq_lift) end RelIso /-! ### Cofinality of ordinals -/ namespace Ordinal /-- Cofinality of an ordinal. This is the smallest cardinal of a subset `S` of the ordinal which is unbounded, in the sense `∀ a, ∃ b ∈ S, a ≤ b`. In particular, `cof 0 = 0` and `cof (succ o) = 1`. -/ def cof (o : Ordinal.{u}) : Cardinal.{u} := o.liftOn (fun a ↦ Order.cof (swap a.rᶜ)) fun _ _ ⟨f⟩ ↦ f.compl.swap.cof_eq theorem cof_type (r : α → α → Prop) [IsWellOrder α r] : (type r).cof = Order.cof (swap rᶜ) := rfl theorem cof_type_lt [LinearOrder α] [IsWellOrder α (· < ·)] : (@type α (· < ·) _).cof = @Order.cof α (· ≤ ·) := by rw [cof_type, compl_lt, swap_ge] theorem cof_eq_cof_toType (o : Ordinal) : o.cof = @Order.cof o.toType (· ≤ ·) := by conv_lhs => rw [← type_toType o, cof_type_lt] theorem le_cof_type [IsWellOrder α r] {c} : c ≤ cof (type r) ↔ ∀ S, Unbounded r S → c ≤ #S := (le_csInf_iff'' (Order.cof_nonempty _)).trans ⟨fun H S h => H _ ⟨S, h, rfl⟩, by rintro H d ⟨S, h, rfl⟩ exact H _ h⟩ theorem cof_type_le [IsWellOrder α r] {S : Set α} (h : Unbounded r S) : cof (type r) ≤ #S := le_cof_type.1 le_rfl S h theorem lt_cof_type [IsWellOrder α r] {S : Set α} : #S < cof (type r) → Bounded r S := by simpa using not_imp_not.2 cof_type_le theorem cof_eq (r : α → α → Prop) [IsWellOrder α r] : ∃ S, Unbounded r S ∧ #S = cof (type r) := csInf_mem (Order.cof_nonempty (swap rᶜ)) theorem ord_cof_eq (r : α → α → Prop) [IsWellOrder α r] : ∃ S, Unbounded r S ∧ type (Subrel r (· ∈ S)) = (cof (type r)).ord := by let ⟨S, hS, e⟩ := cof_eq r let ⟨s, _, e'⟩ := Cardinal.ord_eq S let T : Set α := { a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a } suffices Unbounded r T by refine ⟨T, this, le_antisymm ?_ (Cardinal.ord_le.2 <| cof_type_le this)⟩ rw [← e, e'] refine (RelEmbedding.ofMonotone (fun a : T => (⟨a, let ⟨aS, _⟩ := a.2 aS⟩ : S)) fun a b h => ?_).ordinal_type_le rcases a with ⟨a, aS, ha⟩ rcases b with ⟨b, bS, hb⟩ change s ⟨a, _⟩ ⟨b, _⟩ refine ((trichotomous_of s _ _).resolve_left fun hn => ?_).resolve_left ?_ · exact asymm h (ha _ hn) · intro e injection e with e subst b exact irrefl _ h intro a have : { b : S | ¬r b a }.Nonempty := let ⟨b, bS, ba⟩ := hS a ⟨⟨b, bS⟩, ba⟩ let b := (IsWellFounded.wf : WellFounded s).min _ this have ba : ¬r b a := IsWellFounded.wf.min_mem _ this refine ⟨b, ⟨b.2, fun c => not_imp_not.1 fun h => ?_⟩, ba⟩ rw [show ∀ b : S, (⟨b, b.2⟩ : S) = b by intro b; cases b; rfl] exact IsWellFounded.wf.not_lt_min _ this (IsOrderConnected.neg_trans h ba) /-! ### Cofinality of suprema and least strict upper bounds -/ private theorem card_mem_cof {o} : ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = o.card := ⟨_, _, lsub_typein o, mk_toType o⟩ /-- The set in the `lsub` characterization of `cof` is nonempty. -/ theorem cof_lsub_def_nonempty (o) : { a : Cardinal | ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = a }.Nonempty := ⟨_, card_mem_cof⟩ theorem cof_eq_sInf_lsub (o : Ordinal.{u}) : cof o = sInf { a : Cardinal | ∃ (ι : Type u) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = a } := by refine le_antisymm (le_csInf (cof_lsub_def_nonempty o) ?_) (csInf_le' ?_) · rintro a ⟨ι, f, hf, rfl⟩ rw [← type_toType o] refine (cof_type_le fun a => ?_).trans (@mk_le_of_injective _ _ (fun s : typein ((· < ·) : o.toType → o.toType → Prop) ⁻¹' Set.range f => Classical.choose s.prop) fun s t hst => by let H := congr_arg f hst rwa [Classical.choose_spec s.prop, Classical.choose_spec t.prop, typein_inj, Subtype.coe_inj] at H) have := typein_lt_self a simp_rw [← hf, lt_lsub_iff] at this obtain ⟨i, hi⟩ := this refine ⟨enum (α := o.toType) (· < ·) ⟨f i, ?_⟩, ?_, ?_⟩ · rw [type_toType, ← hf] apply lt_lsub · rw [mem_preimage, typein_enum] exact mem_range_self i · rwa [← typein_le_typein, typein_enum] · rcases cof_eq (α := o.toType) (· < ·) with ⟨S, hS, hS'⟩ let f : S → Ordinal := fun s => typein LT.lt s.val refine ⟨S, f, le_antisymm (lsub_le fun i => typein_lt_self (o := o) i) (le_of_forall_lt fun a ha => ?_), by rwa [type_toType o] at hS'⟩ rw [← type_toType o] at ha rcases hS (enum (· < ·) ⟨a, ha⟩) with ⟨b, hb, hb'⟩ rw [← typein_le_typein, typein_enum] at hb' exact hb'.trans_lt (lt_lsub.{u, u} f ⟨b, hb⟩) @[simp] theorem lift_cof (o) : Cardinal.lift.{u, v} (cof o) = cof (Ordinal.lift.{u, v} o) := by refine inductionOn o fun α r _ ↦ ?_ rw [← type_uLift, cof_type, cof_type, ← Cardinal.lift_id'.{v, u} (Order.cof _), ← Cardinal.lift_umax] apply RelIso.cof_eq_lift ⟨Equiv.ulift.symm, _⟩ simp [swap] theorem cof_le_card (o) : cof o ≤ card o := by rw [cof_eq_sInf_lsub] exact csInf_le' card_mem_cof theorem cof_ord_le (c : Cardinal) : c.ord.cof ≤ c := by simpa using cof_le_card c.ord theorem ord_cof_le (o : Ordinal.{u}) : o.cof.ord ≤ o := (ord_le_ord.2 (cof_le_card o)).trans (ord_card_le o) theorem exists_lsub_cof (o : Ordinal) : ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = cof o := by rw [cof_eq_sInf_lsub] exact csInf_mem (cof_lsub_def_nonempty o) theorem cof_lsub_le {ι} (f : ι → Ordinal) : cof (lsub.{u, u} f) ≤ #ι := by rw [cof_eq_sInf_lsub] exact csInf_le' ⟨ι, f, rfl, rfl⟩ theorem cof_lsub_le_lift {ι} (f : ι → Ordinal) : cof (lsub.{u, v} f) ≤ Cardinal.lift.{v, u} #ι := by rw [← mk_uLift.{u, v}] convert cof_lsub_le.{max u v} fun i : ULift.{v, u} ι => f i.down exact lsub_eq_of_range_eq.{u, max u v, max u v} (Set.ext fun x => ⟨fun ⟨i, hi⟩ => ⟨ULift.up.{v, u} i, hi⟩, fun ⟨i, hi⟩ => ⟨_, hi⟩⟩) theorem le_cof_iff_lsub {o : Ordinal} {a : Cardinal} : a ≤ cof o ↔ ∀ {ι} (f : ι → Ordinal), lsub.{u, u} f = o → a ≤ #ι := by rw [cof_eq_sInf_lsub] exact (le_csInf_iff'' (cof_lsub_def_nonempty o)).trans ⟨fun H ι f hf => H _ ⟨ι, f, hf, rfl⟩, fun H b ⟨ι, f, hf, hb⟩ => by rw [← hb] exact H _ hf⟩ theorem lsub_lt_ord_lift {ι} {f : ι → Ordinal} {c : Ordinal} (hι : Cardinal.lift.{v, u} #ι < c.cof) (hf : ∀ i, f i < c) : lsub.{u, v} f < c := lt_of_le_of_ne (lsub_le hf) fun h => by subst h exact (cof_lsub_le_lift.{u, v} f).not_lt hι theorem lsub_lt_ord {ι} {f : ι → Ordinal} {c : Ordinal} (hι : #ι < c.cof) : (∀ i, f i < c) → lsub.{u, u} f < c := lsub_lt_ord_lift (by rwa [(#ι).lift_id]) theorem cof_iSup_le_lift {ι} {f : ι → Ordinal} (H : ∀ i, f i < iSup f) : cof (iSup f) ≤ Cardinal.lift.{v, u} #ι := by rw [← Ordinal.sup] at * rw [← sup_eq_lsub_iff_lt_sup.{u, v}] at H rw [H] exact cof_lsub_le_lift f theorem cof_iSup_le {ι} {f : ι → Ordinal} (H : ∀ i, f i < iSup f) : cof (iSup f) ≤ #ι := by rw [← (#ι).lift_id] exact cof_iSup_le_lift H theorem iSup_lt_ord_lift {ι} {f : ι → Ordinal} {c : Ordinal} (hι : Cardinal.lift.{v, u} #ι < c.cof) (hf : ∀ i, f i < c) : iSup f < c := (sup_le_lsub.{u, v} f).trans_lt (lsub_lt_ord_lift hι hf) theorem iSup_lt_ord {ι} {f : ι → Ordinal} {c : Ordinal} (hι : #ι < c.cof) : (∀ i, f i < c) → iSup f < c := iSup_lt_ord_lift (by rwa [(#ι).lift_id]) theorem iSup_lt_lift {ι} {f : ι → Cardinal} {c : Cardinal} (hι : Cardinal.lift.{v, u} #ι < c.ord.cof) (hf : ∀ i, f i < c) : iSup f < c := by rw [← ord_lt_ord, iSup_ord (Cardinal.bddAbove_range _)] refine iSup_lt_ord_lift hι fun i => ?_ rw [ord_lt_ord] apply hf theorem iSup_lt {ι} {f : ι → Cardinal} {c : Cardinal} (hι : #ι < c.ord.cof) : (∀ i, f i < c) → iSup f < c := iSup_lt_lift (by rwa [(#ι).lift_id]) theorem nfpFamily_lt_ord_lift {ι} {f : ι → Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hc' : Cardinal.lift.{v, u} #ι < cof c) (hf : ∀ (i), ∀ b < c, f i b < c) {a} (ha : a < c) : nfpFamily f a < c := by refine iSup_lt_ord_lift ((Cardinal.lift_le.2 (mk_list_le_max ι)).trans_lt ?_) fun l => ?_ · rw [lift_max] apply max_lt _ hc' rwa [Cardinal.lift_aleph0] · induction' l with i l H · exact ha · exact hf _ _ H theorem nfpFamily_lt_ord {ι} {f : ι → Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hc' : #ι < cof c) (hf : ∀ (i), ∀ b < c, f i b < c) {a} : a < c → nfpFamily.{u, u} f a < c := nfpFamily_lt_ord_lift hc (by rwa [(#ι).lift_id]) hf theorem nfp_lt_ord {f : Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hf : ∀ i < c, f i < c) {a} : a < c → nfp f a < c := nfpFamily_lt_ord_lift hc (by simpa using Cardinal.one_lt_aleph0.trans hc) fun _ => hf theorem exists_blsub_cof (o : Ordinal) : ∃ f : ∀ a < (cof o).ord, Ordinal, blsub.{u, u} _ f = o := by rcases exists_lsub_cof o with ⟨ι, f, hf, hι⟩ rcases Cardinal.ord_eq ι with ⟨r, hr, hι'⟩ rw [← @blsub_eq_lsub' ι r hr] at hf rw [← hι, hι'] exact ⟨_, hf⟩ theorem le_cof_iff_blsub {b : Ordinal} {a : Cardinal} : a ≤ cof b ↔ ∀ {o} (f : ∀ a < o, Ordinal), blsub.{u, u} o f = b → a ≤ o.card := le_cof_iff_lsub.trans ⟨fun H o f hf => by simpa using H _ hf, fun H ι f hf => by rcases Cardinal.ord_eq ι with ⟨r, hr, hι'⟩ rw [← @blsub_eq_lsub' ι r hr] at hf simpa using H _ hf⟩ theorem cof_blsub_le_lift {o} (f : ∀ a < o, Ordinal) : cof (blsub.{u, v} o f) ≤ Cardinal.lift.{v, u} o.card := by rw [← mk_toType o] exact cof_lsub_le_lift _ theorem cof_blsub_le {o} (f : ∀ a < o, Ordinal) : cof (blsub.{u, u} o f) ≤ o.card := by rw [← o.card.lift_id] exact cof_blsub_le_lift f theorem blsub_lt_ord_lift {o : Ordinal.{u}} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : Cardinal.lift.{v, u} o.card < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u, v} o f < c := lt_of_le_of_ne (blsub_le hf) fun h => ho.not_le (by simpa [← iSup_ord, hf, h] using cof_blsub_le_lift.{u, v} f) theorem blsub_lt_ord {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : o.card < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u, u} o f < c := blsub_lt_ord_lift (by rwa [o.card.lift_id]) hf theorem cof_bsup_le_lift {o : Ordinal} {f : ∀ a < o, Ordinal} (H : ∀ i h, f i h < bsup.{u, v} o f) : cof (bsup.{u, v} o f) ≤ Cardinal.lift.{v, u} o.card := by rw [← bsup_eq_blsub_iff_lt_bsup.{u, v}] at H rw [H] exact cof_blsub_le_lift.{u, v} f theorem cof_bsup_le {o : Ordinal} {f : ∀ a < o, Ordinal} : (∀ i h, f i h < bsup.{u, u} o f) → cof (bsup.{u, u} o f) ≤ o.card := by rw [← o.card.lift_id] exact cof_bsup_le_lift theorem bsup_lt_ord_lift {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : Cardinal.lift.{v, u} o.card < c.cof) (hf : ∀ i hi, f i hi < c) : bsup.{u, v} o f < c := (bsup_le_blsub f).trans_lt (blsub_lt_ord_lift ho hf) theorem bsup_lt_ord {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : o.card < c.cof) : (∀ i hi, f i hi < c) → bsup.{u, u} o f < c := bsup_lt_ord_lift (by rwa [o.card.lift_id]) /-! ### Basic results -/ @[simp] theorem cof_zero : cof 0 = 0 := by refine LE.le.antisymm ?_ (Cardinal.zero_le _) rw [← card_zero] exact cof_le_card 0 @[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 := ⟨inductionOn o fun _ r _ z => let ⟨_, hl, e⟩ := cof_eq r type_eq_zero_iff_isEmpty.2 <| ⟨fun a => let ⟨_, h, _⟩ := hl a (mk_eq_zero_iff.1 (e.trans z)).elim' ⟨_, h⟩⟩, fun e => by simp [e]⟩ theorem cof_ne_zero {o} : cof o ≠ 0 ↔ o ≠ 0 := cof_eq_zero.not @[simp] theorem cof_succ (o) : cof (succ o) = 1 := by apply le_antisymm · refine inductionOn o fun α r _ => ?_ change cof (type _) ≤ _ rw [← (_ : #_ = 1)] · apply cof_type_le refine fun a => ⟨Sum.inr PUnit.unit, Set.mem_singleton _, ?_⟩ rcases a with (a | ⟨⟨⟨⟩⟩⟩) <;> simp [EmptyRelation] · rw [Cardinal.mk_fintype, Set.card_singleton] simp · rw [← Cardinal.succ_zero, succ_le_iff] simpa [lt_iff_le_and_ne, Cardinal.zero_le] using fun h => succ_ne_zero o (cof_eq_zero.1 (Eq.symm h)) @[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a := ⟨inductionOn o fun α r _ z => by rcases cof_eq r with ⟨S, hl, e⟩; rw [z] at e obtain ⟨a⟩ := mk_ne_zero_iff.1 (by rw [e]; exact one_ne_zero) refine ⟨typein r a, Eq.symm <| Quotient.sound ⟨RelIso.ofSurjective (RelEmbedding.ofMonotone ?_ fun x y => ?_) fun x => ?_⟩⟩ · apply Sum.rec <;> [exact Subtype.val; exact fun _ => a] · rcases x with (x | ⟨⟨⟨⟩⟩⟩) <;> rcases y with (y | ⟨⟨⟨⟩⟩⟩) <;> simp [Subrel, Order.Preimage, EmptyRelation] exact x.2 · suffices r x a ∨ ∃ _ : PUnit.{u}, ↑a = x by convert this dsimp [RelEmbedding.ofMonotone]; simp rcases trichotomous_of r x a with (h | h | h) · exact Or.inl h · exact Or.inr ⟨PUnit.unit, h.symm⟩ · rcases hl x with ⟨a', aS, hn⟩ refine absurd h ?_ convert hn change (a : α) = ↑(⟨a', aS⟩ : S) have := le_one_iff_subsingleton.1 (le_of_eq e) congr!, fun ⟨a, e⟩ => by simp [e]⟩ /-! ### Fundamental sequences -/ -- TODO: move stuff about fundamental sequences to their own file. /-- A fundamental sequence for `a` is an increasing sequence of length `o = cof a` that converges at `a`. We provide `o` explicitly in order to avoid type rewrites. -/ def IsFundamentalSequence (a o : Ordinal.{u}) (f : ∀ b < o, Ordinal.{u}) : Prop := o ≤ a.cof.ord ∧ (∀ {i j} (hi hj), i < j → f i hi < f j hj) ∧ blsub.{u, u} o f = a namespace IsFundamentalSequence variable {a o : Ordinal.{u}} {f : ∀ b < o, Ordinal.{u}} protected theorem cof_eq (hf : IsFundamentalSequence a o f) : a.cof.ord = o := hf.1.antisymm' <| by rw [← hf.2.2] exact (ord_le_ord.2 (cof_blsub_le f)).trans (ord_card_le o) protected theorem strict_mono (hf : IsFundamentalSequence a o f) {i j} : ∀ hi hj, i < j → f i hi < f j hj := hf.2.1 theorem blsub_eq (hf : IsFundamentalSequence a o f) : blsub.{u, u} o f = a := hf.2.2 theorem ord_cof (hf : IsFundamentalSequence a o f) : IsFundamentalSequence a a.cof.ord fun i hi => f i (hi.trans_le (by rw [hf.cof_eq])) := by have H := hf.cof_eq subst H exact hf theorem id_of_le_cof (h : o ≤ o.cof.ord) : IsFundamentalSequence o o fun a _ => a := ⟨h, @fun _ _ _ _ => id, blsub_id o⟩ protected theorem zero {f : ∀ b < (0 : Ordinal), Ordinal} : IsFundamentalSequence 0 0 f := ⟨by rw [cof_zero, ord_zero], @fun i _ hi => (Ordinal.not_lt_zero i hi).elim, blsub_zero f⟩ protected theorem succ : IsFundamentalSequence (succ o) 1 fun _ _ => o := by refine ⟨?_, @fun i j hi hj h => ?_, blsub_const Ordinal.one_ne_zero o⟩ · rw [cof_succ, ord_one] · rw [lt_one_iff_zero] at hi hj rw [hi, hj] at h exact h.false.elim protected theorem monotone (hf : IsFundamentalSequence a o f) {i j : Ordinal} (hi : i < o) (hj : j < o) (hij : i ≤ j) : f i hi ≤ f j hj := by rcases lt_or_eq_of_le hij with (hij | rfl) · exact (hf.2.1 hi hj hij).le · rfl theorem trans {a o o' : Ordinal.{u}} {f : ∀ b < o, Ordinal.{u}} (hf : IsFundamentalSequence a o f) {g : ∀ b < o', Ordinal.{u}} (hg : IsFundamentalSequence o o' g) : IsFundamentalSequence a o' fun i hi => f (g i hi) (by rw [← hg.2.2]; apply lt_blsub) := by refine ⟨?_, @fun i j _ _ h => hf.2.1 _ _ (hg.2.1 _ _ h), ?_⟩ · rw [hf.cof_eq] exact hg.1.trans (ord_cof_le o) · rw [@blsub_comp.{u, u, u} o _ f (@IsFundamentalSequence.monotone _ _ f hf)] · exact hf.2.2 · exact hg.2.2 protected theorem lt {a o : Ordinal} {s : Π p < o, Ordinal} (h : IsFundamentalSequence a o s) {p : Ordinal} (hp : p < o) : s p hp < a := h.blsub_eq ▸ lt_blsub s p hp end IsFundamentalSequence /-- Every ordinal has a fundamental sequence. -/ theorem exists_fundamental_sequence (a : Ordinal.{u}) : ∃ f, IsFundamentalSequence a a.cof.ord f := by suffices h : ∃ o f, IsFundamentalSequence a o f by rcases h with ⟨o, f, hf⟩ exact ⟨_, hf.ord_cof⟩ rcases exists_lsub_cof a with ⟨ι, f, hf, hι⟩ rcases ord_eq ι with ⟨r, wo, hr⟩ haveI := wo let r' := Subrel r fun i ↦ ∀ j, r j i → f j < f i let hrr' : r' ↪r r := Subrel.relEmbedding _ _ haveI := hrr'.isWellOrder refine ⟨_, _, hrr'.ordinal_type_le.trans ?_, @fun i j _ h _ => (enum r' ⟨j, h⟩).prop _ ?_, le_antisymm (blsub_le fun i hi => lsub_le_iff.1 hf.le _) ?_⟩ · rw [← hι, hr] · change r (hrr'.1 _) (hrr'.1 _) rwa [hrr'.2, @enum_lt_enum _ r'] · rw [← hf, lsub_le_iff] intro i suffices h : ∃ i' hi', f i ≤ bfamilyOfFamily' r' (fun i => f i) i' hi' by rcases h with ⟨i', hi', hfg⟩ exact hfg.trans_lt (lt_blsub _ _ _) by_cases h : ∀ j, r j i → f j < f i · refine ⟨typein r' ⟨i, h⟩, typein_lt_type _ _, ?_⟩ rw [bfamilyOfFamily'_typein] · push_neg at h obtain ⟨hji, hij⟩ := wo.wf.min_mem _ h refine ⟨typein r' ⟨_, fun k hkj => lt_of_lt_of_le ?_ hij⟩, typein_lt_type _ _, ?_⟩ · by_contra! H exact (wo.wf.not_lt_min _ h ⟨IsTrans.trans _ _ _ hkj hji, H⟩) hkj · rwa [bfamilyOfFamily'_typein] @[simp] theorem cof_cof (a : Ordinal.{u}) : cof (cof a).ord = cof a := by obtain ⟨f, hf⟩ := exists_fundamental_sequence a obtain ⟨g, hg⟩ := exists_fundamental_sequence a.cof.ord exact ord_injective (hf.trans hg).cof_eq.symm protected theorem IsNormal.isFundamentalSequence {f : Ordinal.{u} → Ordinal.{u}} (hf : IsNormal f) {a o} (ha : IsLimit a) {g} (hg : IsFundamentalSequence a o g) : IsFundamentalSequence (f a) o fun b hb => f (g b hb) := by refine ⟨?_, @fun i j _ _ h => hf.strictMono (hg.2.1 _ _ h), ?_⟩ · rcases exists_lsub_cof (f a) with ⟨ι, f', hf', hι⟩ rw [← hg.cof_eq, ord_le_ord, ← hι] suffices (lsub.{u, u} fun i => sInf { b : Ordinal | f' i ≤ f b }) = a by rw [← this] apply cof_lsub_le have H : ∀ i, ∃ b < a, f' i ≤ f b := fun i => by have := lt_lsub.{u, u} f' i rw [hf', ← IsNormal.blsub_eq.{u, u} hf ha, lt_blsub_iff] at this simpa using this refine (lsub_le fun i => ?_).antisymm (le_of_forall_lt fun b hb => ?_) · rcases H i with ⟨b, hb, hb'⟩ exact lt_of_le_of_lt (csInf_le' hb') hb · have := hf.strictMono hb rw [← hf', lt_lsub_iff] at this obtain ⟨i, hi⟩ := this rcases H i with ⟨b, _, hb⟩ exact ((le_csInf_iff'' ⟨b, by exact hb⟩).2 fun c hc => hf.strictMono.le_iff_le.1 (hi.trans hc)).trans_lt (lt_lsub _ i) · rw [@blsub_comp.{u, u, u} a _ (fun b _ => f b) (@fun i j _ _ h => hf.strictMono.monotone h) g hg.2.2] exact IsNormal.blsub_eq.{u, u} hf ha theorem IsNormal.cof_eq {f} (hf : IsNormal f) {a} (ha : IsLimit a) : cof (f a) = cof a := let ⟨_, hg⟩ := exists_fundamental_sequence a ord_injective (hf.isFundamentalSequence ha hg).cof_eq theorem IsNormal.cof_le {f} (hf : IsNormal f) (a) : cof a ≤ cof (f a) := by rcases zero_or_succ_or_limit a with (rfl | ⟨b, rfl⟩ | ha) · rw [cof_zero] exact zero_le _ · rw [cof_succ, Cardinal.one_le_iff_ne_zero, cof_ne_zero, ← Ordinal.pos_iff_ne_zero] exact (Ordinal.zero_le (f b)).trans_lt (hf.1 b) · rw [hf.cof_eq ha] @[simp] theorem cof_add (a b : Ordinal) : b ≠ 0 → cof (a + b) = cof b := fun h => by rcases zero_or_succ_or_limit b with (rfl | ⟨c, rfl⟩ | hb) · contradiction · rw [add_succ, cof_succ, cof_succ] · exact (isNormal_add_right a).cof_eq hb theorem aleph0_le_cof {o} : ℵ₀ ≤ cof o ↔ IsLimit o := by rcases zero_or_succ_or_limit o with (rfl | ⟨o, rfl⟩ | l) · simp [not_zero_isLimit, Cardinal.aleph0_ne_zero] · simp [not_succ_isLimit, Cardinal.one_lt_aleph0] · simp only [l, iff_true] refine le_of_not_lt fun h => ?_ obtain ⟨n, e⟩ := Cardinal.lt_aleph0.1 h have := cof_cof o rw [e, ord_nat] at this cases n · simp at e simp [e, not_zero_isLimit] at l · rw [natCast_succ, cof_succ] at this rw [← this, cof_eq_one_iff_is_succ] at e rcases e with ⟨a, rfl⟩ exact not_succ_isLimit _ l @[simp] theorem cof_preOmega {o : Ordinal} (ho : IsSuccPrelimit o) : (preOmega o).cof = o.cof := by by_cases h : IsMin o · simp [h.eq_bot] · exact isNormal_preOmega.cof_eq ⟨h, ho⟩ @[simp] theorem cof_omega {o : Ordinal} (ho : o.IsLimit) : (ω_ o).cof = o.cof := isNormal_omega.cof_eq ho @[simp] theorem cof_omega0 : cof ω = ℵ₀ := (aleph0_le_cof.2 isLimit_omega0).antisymm' <| by rw [← card_omega0] apply cof_le_card theorem cof_eq' (r : α → α → Prop) [IsWellOrder α r] (h : IsLimit (type r)) : ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = cof (type r) := let ⟨S, H, e⟩ := cof_eq r ⟨S, fun a => let a' := enum r ⟨_, h.succ_lt (typein_lt_type r a)⟩ let ⟨b, h, ab⟩ := H a' ⟨b, h, (IsOrderConnected.conn a b a' <| (typein_lt_typein r).1 (by rw [typein_enum] exact lt_succ (typein _ _))).resolve_right ab⟩, e⟩ @[simp] theorem cof_univ : cof univ.{u, v} = Cardinal.univ.{u, v} := le_antisymm (cof_le_card _) (by refine le_of_forall_lt fun c h => ?_ rcases lt_univ'.1 h with ⟨c, rfl⟩ rcases @cof_eq Ordinal.{u} (· < ·) _ with ⟨S, H, Se⟩ rw [univ, ← lift_cof, ← Cardinal.lift_lift.{u+1, v, u}, Cardinal.lift_lt, ← Se] refine lt_of_not_ge fun h => ?_ obtain ⟨a, e⟩ := Cardinal.mem_range_lift_of_le h refine Quotient.inductionOn a (fun α e => ?_) e obtain ⟨f⟩ := Quotient.exact e have f := Equiv.ulift.symm.trans f let g a := (f a).1 let o := succ (iSup g) rcases H o with ⟨b, h, l⟩ refine l (lt_succ_iff.2 ?_) rw [← show g (f.symm ⟨b, h⟩) = b by simp [g]] apply Ordinal.le_iSup) end Ordinal namespace Cardinal open Ordinal /-! ### Results on sets -/ theorem mk_bounded_subset {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) {r : α → α → Prop} [IsWellOrder α r] (hr : (#α).ord = type r) : #{ s : Set α // Bounded r s } = #α := by rcases eq_or_ne #α 0 with (ha | ha) · rw [ha] haveI := mk_eq_zero_iff.1 ha rw [mk_eq_zero_iff] constructor rintro ⟨s, hs⟩ exact (not_unbounded_iff s).2 hs (unbounded_of_isEmpty s) have h' : IsStrongLimit #α := ⟨ha, @h⟩ have ha := h'.aleph0_le apply le_antisymm · have : { s : Set α | Bounded r s } = ⋃ i, 𝒫{ j | r j i } := setOf_exists _ rw [← coe_setOf, this] refine mk_iUnion_le_sum_mk.trans ((sum_le_iSup (fun i => #(𝒫{ j | r j i }))).trans ((mul_le_max_of_aleph0_le_left ha).trans ?_)) rw [max_eq_left] apply ciSup_le' _ intro i rw [mk_powerset] apply (h'.two_power_lt _).le rw [coe_setOf, card_typein, ← lt_ord, hr] apply typein_lt_type · refine @mk_le_of_injective α _ (fun x => Subtype.mk {x} ?_) ?_ · apply bounded_singleton rw [← hr] apply isLimit_ord ha · intro a b hab simpa [singleton_eq_singleton_iff] using hab theorem mk_subset_mk_lt_cof {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) : #{ s : Set α // #s < cof (#α).ord } = #α := by rcases eq_or_ne #α 0 with (ha | ha) · simp [ha] have h' : IsStrongLimit #α := ⟨ha, @h⟩ rcases ord_eq α with ⟨r, wo, hr⟩ haveI := wo apply le_antisymm · conv_rhs => rw [← mk_bounded_subset h hr] apply mk_le_mk_of_subset intro s hs rw [hr] at hs exact lt_cof_type hs · refine @mk_le_of_injective α _ (fun x => Subtype.mk {x} ?_) ?_ · rw [mk_singleton] exact one_lt_aleph0.trans_le (aleph0_le_cof.2 (isLimit_ord h'.aleph0_le)) · intro a b hab simpa [singleton_eq_singleton_iff] using hab /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : IsWellOrder α r] {s : Set (Set α)} (h₁ : Unbounded r <| ⋃₀ s) (h₂ : #s < Order.cof (swap rᶜ)) : ∃ x ∈ s, Unbounded r x := by by_contra! h simp_rw [not_unbounded_iff] at h let f : s → α := fun x : s => wo.wf.sup x (h x.1 x.2) refine h₂.not_le (le_trans (csInf_le' ⟨range f, fun x => ?_, rfl⟩) mk_range_le) rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩ exact ⟨f ⟨c, hc⟩, mem_range_self _, fun hxz => hxy (Trans.trans (wo.wf.lt_sup _ hy) hxz)⟩ /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_iUnion {α β : Type u} (r : α → α → Prop) [wo : IsWellOrder α r] (s : β → Set α) (h₁ : Unbounded r <| ⋃ x, s x) (h₂ : #β < Order.cof (swap rᶜ)) : ∃ x : β, Unbounded r (s x) := by rw [← sUnion_range] at h₁ rcases unbounded_of_unbounded_sUnion r h₁ (mk_range_le.trans_lt h₂) with ⟨_, ⟨x, rfl⟩, u⟩ exact ⟨x, u⟩ /-! ### Consequences of König's lemma -/ theorem lt_power_cof {c : Cardinal.{u}} : ℵ₀ ≤ c → c < c ^ c.ord.cof := Cardinal.inductionOn c fun α h => by rcases ord_eq α with ⟨r, wo, re⟩ have := isLimit_ord h rw [re] at this ⊢ rcases cof_eq' r this with ⟨S, H, Se⟩ have := sum_lt_prod (fun a : S => #{ x // r x a }) (fun _ => #α) fun i => ?_ · simp only [Cardinal.prod_const, Cardinal.lift_id, ← Se, ← mk_sigma, power_def] at this ⊢ refine lt_of_le_of_lt ?_ this refine ⟨Embedding.ofSurjective ?_ ?_⟩ · exact fun x => x.2.1 · exact fun a => let ⟨b, h, ab⟩ := H a ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ · have := typein_lt_type r i rwa [← re, lt_ord] at this theorem lt_cof_power {a b : Cardinal} (ha : ℵ₀ ≤ a) (b1 : 1 < b) : a < (b ^ a).ord.cof := by have b0 : b ≠ 0 := (zero_lt_one.trans b1).ne' apply lt_imp_lt_of_le_imp_le (power_le_power_left <| power_ne_zero a b0) rw [← power_mul, mul_eq_self ha] exact lt_power_cof (ha.trans <| (cantor' _ b1).le) end Cardinal
Mathlib/SetTheory/Cardinal/Cofinality.lean
1,104
1,106
/- 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 -/
Mathlib/Data/List/Basic.lean
692
701
/- 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,848
1,856
/- Copyright (c) 2023 Geoffrey Irving. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler, Geoffrey Irving, Stefan Kebekus -/ import Mathlib.Analysis.Analytic.Composition import Mathlib.Analysis.Analytic.Linear import Mathlib.Analysis.NormedSpace.OperatorNorm.Mul import Mathlib.Analysis.Normed.Ring.Units import Mathlib.Analysis.Analytic.OfScalars /-! # Various ways to combine analytic functions We show that the following are analytic: 1. Cartesian products of analytic functions 2. Arithmetic on analytic functions: `mul`, `smul`, `inv`, `div` 3. Finite sums and products: `Finset.sum`, `Finset.prod` -/ noncomputable section open scoped Topology open Filter Asymptotics ENNReal NNReal variable {α : Type*} variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] variable {E F G H : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] [NormedAddCommGroup F] [NormedSpace 𝕜 F] [NormedAddCommGroup G] [NormedSpace 𝕜 G] [NormedAddCommGroup H] [NormedSpace 𝕜 H] variable {𝕝 : Type*} [NontriviallyNormedField 𝕝] [NormedAlgebra 𝕜 𝕝] variable {A : Type*} [NormedRing A] [NormedAlgebra 𝕜 A] /-! ### Constants are analytic -/ theorem hasFPowerSeriesOnBall_const {c : F} {e : E} : HasFPowerSeriesOnBall (fun _ => c) (constFormalMultilinearSeries 𝕜 E c) e ⊤ := by refine ⟨by simp, WithTop.top_pos, fun _ => hasSum_single 0 fun n hn => ?_⟩ simp [constFormalMultilinearSeries_apply hn] theorem hasFPowerSeriesAt_const {c : F} {e : E} : HasFPowerSeriesAt (fun _ => c) (constFormalMultilinearSeries 𝕜 E c) e := ⟨⊤, hasFPowerSeriesOnBall_const⟩ @[fun_prop] theorem analyticAt_const {v : F} {x : E} : AnalyticAt 𝕜 (fun _ => v) x := ⟨constFormalMultilinearSeries 𝕜 E v, hasFPowerSeriesAt_const⟩ theorem analyticOnNhd_const {v : F} {s : Set E} : AnalyticOnNhd 𝕜 (fun _ => v) s := fun _ _ => analyticAt_const theorem analyticWithinAt_const {v : F} {s : Set E} {x : E} : AnalyticWithinAt 𝕜 (fun _ => v) s x := analyticAt_const.analyticWithinAt theorem analyticOn_const {v : F} {s : Set E} : AnalyticOn 𝕜 (fun _ => v) s := analyticOnNhd_const.analyticOn /-! ### Addition, negation, subtraction, scalar multiplication -/ section variable {f g : E → F} {pf pg : FormalMultilinearSeries 𝕜 E F} {s : Set E} {x : E} {r : ℝ≥0∞} {c : 𝕜} theorem HasFPowerSeriesWithinOnBall.add (hf : HasFPowerSeriesWithinOnBall f pf s x r) (hg : HasFPowerSeriesWithinOnBall g pg s x r) : HasFPowerSeriesWithinOnBall (f + g) (pf + pg) s x r := { r_le := le_trans (le_min_iff.2 ⟨hf.r_le, hg.r_le⟩) (pf.min_radius_le_radius_add pg) r_pos := hf.r_pos hasSum := fun hy h'y => (hf.hasSum hy h'y).add (hg.hasSum hy h'y) } theorem HasFPowerSeriesOnBall.add (hf : HasFPowerSeriesOnBall f pf x r) (hg : HasFPowerSeriesOnBall g pg x r) : HasFPowerSeriesOnBall (f + g) (pf + pg) x r := { r_le := le_trans (le_min_iff.2 ⟨hf.r_le, hg.r_le⟩) (pf.min_radius_le_radius_add pg) r_pos := hf.r_pos hasSum := fun hy => (hf.hasSum hy).add (hg.hasSum hy) } theorem HasFPowerSeriesWithinAt.add (hf : HasFPowerSeriesWithinAt f pf s x) (hg : HasFPowerSeriesWithinAt g pg s x) : HasFPowerSeriesWithinAt (f + g) (pf + pg) s x := by rcases (hf.eventually.and hg.eventually).exists with ⟨r, hr⟩ exact ⟨r, hr.1.add hr.2⟩ theorem HasFPowerSeriesAt.add (hf : HasFPowerSeriesAt f pf x) (hg : HasFPowerSeriesAt g pg x) : HasFPowerSeriesAt (f + g) (pf + pg) x := by rcases (hf.eventually.and hg.eventually).exists with ⟨r, hr⟩ exact ⟨r, hr.1.add hr.2⟩ theorem AnalyticWithinAt.add (hf : AnalyticWithinAt 𝕜 f s x) (hg : AnalyticWithinAt 𝕜 g s x) : AnalyticWithinAt 𝕜 (f + g) s x := let ⟨_, hpf⟩ := hf let ⟨_, hqf⟩ := hg (hpf.add hqf).analyticWithinAt @[fun_prop] theorem AnalyticAt.fun_add (hf : AnalyticAt 𝕜 f x) (hg : AnalyticAt 𝕜 g x) : AnalyticAt 𝕜 (fun z ↦ f z + g z) x := let ⟨_, hpf⟩ := hf let ⟨_, hqf⟩ := hg (hpf.add hqf).analyticAt @[deprecated (since := "2025-03-11")] alias AnalyticAt.add' := AnalyticAt.fun_add @[fun_prop] theorem AnalyticAt.add (hf : AnalyticAt 𝕜 f x) (hg : AnalyticAt 𝕜 g x) : AnalyticAt 𝕜 (f + g) x := hf.fun_add hg theorem HasFPowerSeriesWithinOnBall.neg (hf : HasFPowerSeriesWithinOnBall f pf s x r) : HasFPowerSeriesWithinOnBall (-f) (-pf) s x r := { r_le := by rw [pf.radius_neg] exact hf.r_le r_pos := hf.r_pos hasSum := fun hy h'y => (hf.hasSum hy h'y).neg } theorem HasFPowerSeriesOnBall.neg (hf : HasFPowerSeriesOnBall f pf x r) : HasFPowerSeriesOnBall (-f) (-pf) x r := { r_le := by rw [pf.radius_neg] exact hf.r_le r_pos := hf.r_pos hasSum := fun hy => (hf.hasSum hy).neg } theorem HasFPowerSeriesWithinAt.neg (hf : HasFPowerSeriesWithinAt f pf s x) : HasFPowerSeriesWithinAt (-f) (-pf) s x := let ⟨_, hrf⟩ := hf hrf.neg.hasFPowerSeriesWithinAt theorem HasFPowerSeriesAt.neg (hf : HasFPowerSeriesAt f pf x) : HasFPowerSeriesAt (-f) (-pf) x := let ⟨_, hrf⟩ := hf hrf.neg.hasFPowerSeriesAt theorem AnalyticWithinAt.neg (hf : AnalyticWithinAt 𝕜 f s x) : AnalyticWithinAt 𝕜 (-f) s x := let ⟨_, hpf⟩ := hf hpf.neg.analyticWithinAt @[fun_prop] theorem AnalyticAt.fun_neg (hf : AnalyticAt 𝕜 f x) : AnalyticAt 𝕜 (fun z ↦ -f z) x := let ⟨_, hpf⟩ := hf hpf.neg.analyticAt @[fun_prop] theorem AnalyticAt.neg (hf : AnalyticAt 𝕜 f x) : AnalyticAt 𝕜 (-f) x := hf.fun_neg @[deprecated (since := "2025-03-11")] alias AnalyticAt.neg' := AnalyticAt.fun_neg theorem HasFPowerSeriesWithinOnBall.sub (hf : HasFPowerSeriesWithinOnBall f pf s x r) (hg : HasFPowerSeriesWithinOnBall g pg s x r) : HasFPowerSeriesWithinOnBall (f - g) (pf - pg) s x r := by simpa only [sub_eq_add_neg] using hf.add hg.neg theorem HasFPowerSeriesOnBall.sub (hf : HasFPowerSeriesOnBall f pf x r) (hg : HasFPowerSeriesOnBall g pg x r) : HasFPowerSeriesOnBall (f - g) (pf - pg) x r := by simpa only [sub_eq_add_neg] using hf.add hg.neg theorem HasFPowerSeriesWithinAt.sub (hf : HasFPowerSeriesWithinAt f pf s x) (hg : HasFPowerSeriesWithinAt g pg s x) : HasFPowerSeriesWithinAt (f - g) (pf - pg) s x := by simpa only [sub_eq_add_neg] using hf.add hg.neg theorem HasFPowerSeriesAt.sub (hf : HasFPowerSeriesAt f pf x) (hg : HasFPowerSeriesAt g pg x) : HasFPowerSeriesAt (f - g) (pf - pg) x := by simpa only [sub_eq_add_neg] using hf.add hg.neg theorem AnalyticWithinAt.sub (hf : AnalyticWithinAt 𝕜 f s x) (hg : AnalyticWithinAt 𝕜 g s x) : AnalyticWithinAt 𝕜 (f - g) s x := by simpa only [sub_eq_add_neg] using hf.add hg.neg @[fun_prop] theorem AnalyticAt.fun_sub (hf : AnalyticAt 𝕜 f x) (hg : AnalyticAt 𝕜 g x) : AnalyticAt 𝕜 (fun z ↦ f z - g z) x := by simpa only [sub_eq_add_neg] using hf.add hg.neg
@[fun_prop] theorem AnalyticAt.sub (hf : AnalyticAt 𝕜 f x) (hg : AnalyticAt 𝕜 g x) : AnalyticAt 𝕜 (f - g) x := hf.fun_sub hg @[deprecated (since := "2025-03-11")] alias AnalyticAt.sub' := AnalyticAt.fun_sub theorem HasFPowerSeriesWithinOnBall.const_smul (hf : HasFPowerSeriesWithinOnBall f pf s x r) : HasFPowerSeriesWithinOnBall (c • f) (c • pf) s x r where r_le := le_trans hf.r_le pf.radius_le_smul
Mathlib/Analysis/Analytic/Constructions.lean
181
190
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import Mathlib.Data.ENNReal.Real import Mathlib.Tactic.Bound.Attribute import Mathlib.Topology.Bornology.Basic import Mathlib.Topology.EMetricSpace.Defs import Mathlib.Topology.UniformSpace.Basic /-! ## Pseudo-metric spaces This file defines pseudo-metric spaces: these differ from metric spaces by not imposing the condition `dist x y = 0 → x = y`. Many definitions and theorems expected on (pseudo-)metric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity. ## Main definitions * `Dist α`: Endows a space `α` with a function `dist a b`. * `PseudoMetricSpace α`: A space endowed with a distance function, which can be zero even if the two elements are non-equal. * `Metric.ball x ε`: The set of all points `y` with `dist y x < ε`. * `Metric.Bounded s`: Whether a subset of a `PseudoMetricSpace` is bounded. * `MetricSpace α`: A `PseudoMetricSpace` with the guarantee `dist x y = 0 → x = y`. Additional useful definitions: * `nndist a b`: `dist` as a function to the non-negative reals. * `Metric.closedBall x ε`: The set of all points `y` with `dist y x ≤ ε`. * `Metric.sphere x ε`: The set of all points `y` with `dist y x = ε`. TODO (anyone): Add "Main results" section. ## Tags pseudo_metric, dist -/ assert_not_exists compactSpace_uniformity open Set Filter TopologicalSpace Bornology open scoped ENNReal NNReal Uniformity Topology universe u v w variable {α : Type u} {β : Type v} {X ι : Type*} theorem UniformSpace.ofDist_aux (ε : ℝ) (hε : 0 < ε) : ∃ δ > (0 : ℝ), ∀ x < δ, ∀ y < δ, x + y < ε := ⟨ε / 2, half_pos hε, fun _x hx _y hy => add_halves ε ▸ add_lt_add hx hy⟩ /-- Construct a uniform structure from a distance function and metric space axioms -/ def UniformSpace.ofDist (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : UniformSpace α := .ofFun dist dist_self dist_comm dist_triangle ofDist_aux /-- Construct a bornology from a distance function and metric space axioms. -/ abbrev Bornology.ofDist {α : Type*} (dist : α → α → ℝ) (dist_comm : ∀ x y, dist x y = dist y x) (dist_triangle : ∀ x y z, dist x z ≤ dist x y + dist y z) : Bornology α := Bornology.ofBounded { s : Set α | ∃ C, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C } ⟨0, fun _ hx _ => hx.elim⟩ (fun _ ⟨c, hc⟩ _ h => ⟨c, fun _ hx _ hy => hc (h hx) (h hy)⟩) (fun s hs t ht => by rcases s.eq_empty_or_nonempty with rfl | ⟨x, hx⟩ · rwa [empty_union] rcases t.eq_empty_or_nonempty with rfl | ⟨y, hy⟩ · rwa [union_empty] rsuffices ⟨C, hC⟩ : ∃ C, ∀ z ∈ s ∪ t, dist x z ≤ C · refine ⟨C + C, fun a ha b hb => (dist_triangle a x b).trans ?_⟩ simpa only [dist_comm] using add_le_add (hC _ ha) (hC _ hb) rcases hs with ⟨Cs, hs⟩; rcases ht with ⟨Ct, ht⟩ refine ⟨max Cs (dist x y + Ct), fun z hz => hz.elim (fun hz => (hs hx hz).trans (le_max_left _ _)) (fun hz => (dist_triangle x y z).trans <| (add_le_add le_rfl (ht hy hz)).trans (le_max_right _ _))⟩) fun z => ⟨dist z z, forall_eq.2 <| forall_eq.2 le_rfl⟩ /-- The distance function (given an ambient metric space on `α`), which returns a nonnegative real number `dist x y` given `x y : α`. -/ @[ext] class Dist (α : Type*) where /-- Distance between two points -/ dist : α → α → ℝ export Dist (dist) -- the uniform structure and the emetric space structure are embedded in the metric space structure -- to avoid instance diamond issues. See Note [forgetful inheritance]. /-- This is an internal lemma used inside the default of `PseudoMetricSpace.edist`. -/ private theorem dist_nonneg' {α} {x y : α} (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : 0 ≤ dist x y := have : 0 ≤ 2 * dist x y := calc 0 = dist x x := (dist_self _).symm _ ≤ dist x y + dist y x := dist_triangle _ _ _ _ = 2 * dist x y := by rw [two_mul, dist_comm] nonneg_of_mul_nonneg_right this two_pos /-- A pseudometric space is a type endowed with a `ℝ`-valued distance `dist` satisfying reflexivity `dist x x = 0`, commutativity `dist x y = dist y x`, and the triangle inequality `dist x z ≤ dist x y + dist y z`. Note that we do not require `dist x y = 0 → x = y`. See metric spaces (`MetricSpace`) for the similar class with that stronger assumption. Any pseudometric space is a topological space and a uniform space (see `TopologicalSpace`, `UniformSpace`), where the topology and uniformity come from the metric. Note that a T1 pseudometric space is just a metric space. We make the uniformity/topology part of the data instead of deriving it from the metric. This eg ensures that we do not get a diamond when doing `[PseudoMetricSpace α] [PseudoMetricSpace β] : TopologicalSpace (α × β)`: The product metric and product topology agree, but not definitionally so. See Note [forgetful inheritance]. -/ class PseudoMetricSpace (α : Type u) : Type u extends Dist α where dist_self : ∀ x : α, dist x x = 0 dist_comm : ∀ x y : α, dist x y = dist y x dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z /-- Extended distance between two points -/ edist : α → α → ℝ≥0∞ := fun x y => ENNReal.ofNNReal ⟨dist x y, dist_nonneg' _ ‹_› ‹_› ‹_›⟩ edist_dist : ∀ x y : α, edist x y = ENNReal.ofReal (dist x y) := by intros x y; exact ENNReal.coe_nnreal_eq _ toUniformSpace : UniformSpace α := .ofDist dist dist_self dist_comm dist_triangle uniformity_dist : 𝓤 α = ⨅ ε > 0, 𝓟 { p : α × α | dist p.1 p.2 < ε } := by intros; rfl toBornology : Bornology α := Bornology.ofDist dist dist_comm dist_triangle cobounded_sets : (Bornology.cobounded α).sets = { s | ∃ C : ℝ, ∀ x ∈ sᶜ, ∀ y ∈ sᶜ, dist x y ≤ C } := by intros; rfl /-- Two pseudo metric space structures with the same distance function coincide. -/ @[ext] theorem PseudoMetricSpace.ext {α : Type*} {m m' : PseudoMetricSpace α} (h : m.toDist = m'.toDist) : m = m' := by let d := m.toDist obtain ⟨_, _, _, _, hed, _, hU, _, hB⟩ := m let d' := m'.toDist obtain ⟨_, _, _, _, hed', _, hU', _, hB'⟩ := m' obtain rfl : d = d' := h congr · ext x y : 2 rw [hed, hed'] · exact UniformSpace.ext (hU.trans hU'.symm) · ext : 2 rw [← Filter.mem_sets, ← Filter.mem_sets, hB, hB'] variable [PseudoMetricSpace α] attribute [instance] PseudoMetricSpace.toUniformSpace PseudoMetricSpace.toBornology -- see Note [lower instance priority] instance (priority := 200) PseudoMetricSpace.toEDist : EDist α := ⟨PseudoMetricSpace.edist⟩ /-- Construct a pseudo-metric space structure whose underlying topological space structure (definitionally) agrees which a pre-existing topology which is compatible with a given distance function. -/ def PseudoMetricSpace.ofDistTopology {α : Type u} [TopologicalSpace α] (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (H : ∀ s : Set α, IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s) : PseudoMetricSpace α := { dist := dist dist_self := dist_self dist_comm := dist_comm dist_triangle := dist_triangle toUniformSpace := (UniformSpace.ofDist dist dist_self dist_comm dist_triangle).replaceTopology <| TopologicalSpace.ext_iff.2 fun s ↦ (H s).trans <| forall₂_congr fun x _ ↦ ((UniformSpace.hasBasis_ofFun (exists_gt (0 : ℝ)) dist dist_self dist_comm dist_triangle UniformSpace.ofDist_aux).comap (Prod.mk x)).mem_iff.symm uniformity_dist := rfl toBornology := Bornology.ofDist dist dist_comm dist_triangle cobounded_sets := rfl } @[simp] theorem dist_self (x : α) : dist x x = 0 := PseudoMetricSpace.dist_self x theorem dist_comm (x y : α) : dist x y = dist y x := PseudoMetricSpace.dist_comm x y theorem edist_dist (x y : α) : edist x y = ENNReal.ofReal (dist x y) := PseudoMetricSpace.edist_dist x y @[bound] theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z := PseudoMetricSpace.dist_triangle x y z theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y := by rw [dist_comm z]; apply dist_triangle theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z := by rw [dist_comm y]; apply dist_triangle theorem dist_triangle4 (x y z w : α) : dist x w ≤ dist x y + dist y z + dist z w := calc dist x w ≤ dist x z + dist z w := dist_triangle x z w _ ≤ dist x y + dist y z + dist z w := add_le_add_right (dist_triangle x y z) _ theorem dist_triangle4_left (x₁ y₁ x₂ y₂ : α) : dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) := by rw [add_left_comm, dist_comm x₁, ← add_assoc] apply dist_triangle4 theorem dist_triangle4_right (x₁ y₁ x₂ y₂ : α) : dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ := by rw [add_right_comm, dist_comm y₁] apply dist_triangle4 theorem dist_triangle8 (a b c d e f g h : α) : dist a h ≤ dist a b + dist b c + dist c d + dist d e + dist e f + dist f g + dist g h := by apply le_trans (dist_triangle4 a f g h) apply add_le_add_right (add_le_add_right _ (dist f g)) (dist g h) apply le_trans (dist_triangle4 a d e f) apply add_le_add_right (add_le_add_right _ (dist d e)) (dist e f) exact dist_triangle4 a b c d theorem swap_dist : Function.swap (@dist α _) = dist := by funext x y; exact dist_comm _ _ theorem abs_dist_sub_le (x y z : α) : |dist x z - dist y z| ≤ dist x y := abs_sub_le_iff.2 ⟨sub_le_iff_le_add.2 (dist_triangle _ _ _), sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩ @[bound] theorem dist_nonneg {x y : α} : 0 ≤ dist x y := dist_nonneg' dist dist_self dist_comm dist_triangle namespace Mathlib.Meta.Positivity open Lean Meta Qq Function /-- Extension for the `positivity` tactic: distances are nonnegative. -/ @[positivity Dist.dist _ _] def evalDist : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q(@Dist.dist $β $inst $a $b) => let _inst ← synthInstanceQ q(PseudoMetricSpace $β) assertInstancesCommute pure (.nonnegative q(dist_nonneg)) | _, _, _ => throwError "not dist" end Mathlib.Meta.Positivity example {x y : α} : 0 ≤ dist x y := by positivity @[simp] theorem abs_dist {a b : α} : |dist a b| = dist a b := abs_of_nonneg dist_nonneg /-- A version of `Dist` that takes value in `ℝ≥0`. -/ class NNDist (α : Type*) where /-- Nonnegative distance between two points -/ nndist : α → α → ℝ≥0 export NNDist (nndist) -- see Note [lower instance priority] /-- Distance as a nonnegative real number. -/ instance (priority := 100) PseudoMetricSpace.toNNDist : NNDist α := ⟨fun a b => ⟨dist a b, dist_nonneg⟩⟩ /-- Express `dist` in terms of `nndist` -/ theorem dist_nndist (x y : α) : dist x y = nndist x y := rfl @[simp, norm_cast] theorem coe_nndist (x y : α) : ↑(nndist x y) = dist x y := rfl /-- Express `edist` in terms of `nndist` -/ theorem edist_nndist (x y : α) : edist x y = nndist x y := by rw [edist_dist, dist_nndist, ENNReal.ofReal_coe_nnreal] /-- Express `nndist` in terms of `edist` -/ theorem nndist_edist (x y : α) : nndist x y = (edist x y).toNNReal := by simp [edist_nndist] @[simp, norm_cast] theorem coe_nnreal_ennreal_nndist (x y : α) : ↑(nndist x y) = edist x y := (edist_nndist x y).symm @[simp, norm_cast] theorem edist_lt_coe {x y : α} {c : ℝ≥0} : edist x y < c ↔ nndist x y < c := by rw [edist_nndist, ENNReal.coe_lt_coe] @[simp, norm_cast] theorem edist_le_coe {x y : α} {c : ℝ≥0} : edist x y ≤ c ↔ nndist x y ≤ c := by rw [edist_nndist, ENNReal.coe_le_coe] /-- In a pseudometric space, the extended distance is always finite -/ theorem edist_lt_top {α : Type*} [PseudoMetricSpace α] (x y : α) : edist x y < ⊤ := (edist_dist x y).symm ▸ ENNReal.ofReal_lt_top /-- In a pseudometric space, the extended distance is always finite -/ theorem edist_ne_top (x y : α) : edist x y ≠ ⊤ := (edist_lt_top x y).ne /-- `nndist x x` vanishes -/ @[simp] theorem nndist_self (a : α) : nndist a a = 0 := NNReal.coe_eq_zero.1 (dist_self a) @[simp, norm_cast] theorem dist_lt_coe {x y : α} {c : ℝ≥0} : dist x y < c ↔ nndist x y < c := Iff.rfl @[simp, norm_cast] theorem dist_le_coe {x y : α} {c : ℝ≥0} : dist x y ≤ c ↔ nndist x y ≤ c := Iff.rfl @[simp] theorem edist_lt_ofReal {x y : α} {r : ℝ} : edist x y < ENNReal.ofReal r ↔ dist x y < r := by rw [edist_dist, ENNReal.ofReal_lt_ofReal_iff_of_nonneg dist_nonneg] @[simp] theorem edist_le_ofReal {x y : α} {r : ℝ} (hr : 0 ≤ r) : edist x y ≤ ENNReal.ofReal r ↔ dist x y ≤ r := by rw [edist_dist, ENNReal.ofReal_le_ofReal_iff hr] /-- Express `nndist` in terms of `dist` -/ theorem nndist_dist (x y : α) : nndist x y = Real.toNNReal (dist x y) := by rw [dist_nndist, Real.toNNReal_coe] theorem nndist_comm (x y : α) : nndist x y = nndist y x := NNReal.eq <| dist_comm x y /-- Triangle inequality for the nonnegative distance -/ theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z := dist_triangle _ _ _ theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y := dist_triangle_left _ _ _ theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z := dist_triangle_right _ _ _ /-- Express `dist` in terms of `edist` -/ theorem dist_edist (x y : α) : dist x y = (edist x y).toReal := by rw [edist_dist, ENNReal.toReal_ofReal dist_nonneg] namespace Metric -- instantiate pseudometric space as a topology variable {x y z : α} {δ ε ε₁ ε₂ : ℝ} {s : Set α} /-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/ def ball (x : α) (ε : ℝ) : Set α := { y | dist y x < ε } @[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := Iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw [dist_comm, mem_ball] theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε := dist_nonneg.trans_lt hy theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε := by rwa [mem_ball, dist_self] @[simp] theorem nonempty_ball : (ball x ε).Nonempty ↔ 0 < ε := ⟨fun ⟨_x, hx⟩ => pos_of_mem_ball hx, fun h => ⟨x, mem_ball_self h⟩⟩ @[simp] theorem ball_eq_empty : ball x ε = ∅ ↔ ε ≤ 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_ball, not_lt] @[simp] theorem ball_zero : ball x 0 = ∅ := by rw [ball_eq_empty] /-- If a point belongs to an open ball, then there is a strictly smaller radius whose ball also contains it. See also `exists_lt_subset_ball`. -/ theorem exists_lt_mem_ball_of_mem_ball (h : x ∈ ball y ε) : ∃ ε' < ε, x ∈ ball y ε' := by simp only [mem_ball] at h ⊢ exact ⟨(dist x y + ε) / 2, by linarith, by linarith⟩ theorem ball_eq_ball (ε : ℝ) (x : α) : UniformSpace.ball x { p | dist p.2 p.1 < ε } = Metric.ball x ε := rfl theorem ball_eq_ball' (ε : ℝ) (x : α) : UniformSpace.ball x { p | dist p.1 p.2 < ε } = Metric.ball x ε := by ext simp [dist_comm, UniformSpace.ball] @[simp] theorem iUnion_ball_nat (x : α) : ⋃ n : ℕ, ball x n = univ := iUnion_eq_univ_iff.2 fun y => exists_nat_gt (dist y x) @[simp] theorem iUnion_ball_nat_succ (x : α) : ⋃ n : ℕ, ball x (n + 1) = univ := iUnion_eq_univ_iff.2 fun y => (exists_nat_gt (dist y x)).imp fun _ h => h.trans (lt_add_one _) /-- `closedBall x ε` is the set of all points `y` with `dist y x ≤ ε` -/ def closedBall (x : α) (ε : ℝ) := { y | dist y x ≤ ε } @[simp] theorem mem_closedBall : y ∈ closedBall x ε ↔ dist y x ≤ ε := Iff.rfl theorem mem_closedBall' : y ∈ closedBall x ε ↔ dist x y ≤ ε := by rw [dist_comm, mem_closedBall] /-- `sphere x ε` is the set of all points `y` with `dist y x = ε` -/ def sphere (x : α) (ε : ℝ) := { y | dist y x = ε } @[simp] theorem mem_sphere : y ∈ sphere x ε ↔ dist y x = ε := Iff.rfl theorem mem_sphere' : y ∈ sphere x ε ↔ dist x y = ε := by rw [dist_comm, mem_sphere] theorem ne_of_mem_sphere (h : y ∈ sphere x ε) (hε : ε ≠ 0) : y ≠ x := ne_of_mem_of_not_mem h <| by simpa using hε.symm theorem nonneg_of_mem_sphere (hy : y ∈ sphere x ε) : 0 ≤ ε := dist_nonneg.trans_eq hy @[simp] theorem sphere_eq_empty_of_neg (hε : ε < 0) : sphere x ε = ∅ := Set.eq_empty_iff_forall_not_mem.mpr fun _y hy => (nonneg_of_mem_sphere hy).not_lt hε theorem sphere_eq_empty_of_subsingleton [Subsingleton α] (hε : ε ≠ 0) : sphere x ε = ∅ := Set.eq_empty_iff_forall_not_mem.mpr fun _ h => ne_of_mem_sphere h hε (Subsingleton.elim _ _) instance sphere_isEmpty_of_subsingleton [Subsingleton α] [NeZero ε] : IsEmpty (sphere x ε) := by rw [sphere_eq_empty_of_subsingleton (NeZero.ne ε)]; infer_instance theorem closedBall_eq_singleton_of_subsingleton [Subsingleton α] (h : 0 ≤ ε) : closedBall x ε = {x} := by ext x' simpa [Subsingleton.allEq x x'] theorem ball_eq_singleton_of_subsingleton [Subsingleton α] (h : 0 < ε) : ball x ε = {x} := by ext x' simpa [Subsingleton.allEq x x'] theorem mem_closedBall_self (h : 0 ≤ ε) : x ∈ closedBall x ε := by rwa [mem_closedBall, dist_self] @[simp] theorem nonempty_closedBall : (closedBall x ε).Nonempty ↔ 0 ≤ ε := ⟨fun ⟨_x, hx⟩ => dist_nonneg.trans hx, fun h => ⟨x, mem_closedBall_self h⟩⟩ @[simp] theorem closedBall_eq_empty : closedBall x ε = ∅ ↔ ε < 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_closedBall, not_le] /-- Closed balls and spheres coincide when the radius is non-positive -/ theorem closedBall_eq_sphere_of_nonpos (hε : ε ≤ 0) : closedBall x ε = sphere x ε := Set.ext fun _ => (hε.trans dist_nonneg).le_iff_eq theorem ball_subset_closedBall : ball x ε ⊆ closedBall x ε := fun _y hy => mem_closedBall.2 (le_of_lt hy) theorem sphere_subset_closedBall : sphere x ε ⊆ closedBall x ε := fun _ => le_of_eq lemma sphere_subset_ball {r R : ℝ} (h : r < R) : sphere x r ⊆ ball x R := fun _x hx ↦ (mem_sphere.1 hx).trans_lt h theorem closedBall_disjoint_ball (h : δ + ε ≤ dist x y) : Disjoint (closedBall x δ) (ball y ε) := Set.disjoint_left.mpr fun _a ha1 ha2 => (h.trans <| dist_triangle_left _ _ _).not_lt <| add_lt_add_of_le_of_lt ha1 ha2 theorem ball_disjoint_closedBall (h : δ + ε ≤ dist x y) : Disjoint (ball x δ) (closedBall y ε) := (closedBall_disjoint_ball <| by rwa [add_comm, dist_comm]).symm theorem ball_disjoint_ball (h : δ + ε ≤ dist x y) : Disjoint (ball x δ) (ball y ε) := (closedBall_disjoint_ball h).mono_left ball_subset_closedBall theorem closedBall_disjoint_closedBall (h : δ + ε < dist x y) : Disjoint (closedBall x δ) (closedBall y ε) := Set.disjoint_left.mpr fun _a ha1 ha2 => h.not_le <| (dist_triangle_left _ _ _).trans <| add_le_add ha1 ha2 theorem sphere_disjoint_ball : Disjoint (sphere x ε) (ball x ε) := Set.disjoint_left.mpr fun _y hy₁ hy₂ => absurd hy₁ <| ne_of_lt hy₂ @[simp] theorem ball_union_sphere : ball x ε ∪ sphere x ε = closedBall x ε := Set.ext fun _y => (@le_iff_lt_or_eq ℝ _ _ _).symm @[simp] theorem sphere_union_ball : sphere x ε ∪ ball x ε = closedBall x ε := by rw [union_comm, ball_union_sphere] @[simp] theorem closedBall_diff_sphere : closedBall x ε \ sphere x ε = ball x ε := by rw [← ball_union_sphere, Set.union_diff_cancel_right sphere_disjoint_ball.symm.le_bot] @[simp] theorem closedBall_diff_ball : closedBall x ε \ ball x ε = sphere x ε := by rw [← ball_union_sphere, Set.union_diff_cancel_left sphere_disjoint_ball.symm.le_bot] theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by rw [mem_ball', mem_ball] theorem mem_closedBall_comm : x ∈ closedBall y ε ↔ y ∈ closedBall x ε := by rw [mem_closedBall', mem_closedBall] theorem mem_sphere_comm : x ∈ sphere y ε ↔ y ∈ sphere x ε := by rw [mem_sphere', mem_sphere] @[gcongr] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := fun _y yx => lt_of_lt_of_le (mem_ball.1 yx) h theorem closedBall_eq_bInter_ball : closedBall x ε = ⋂ δ > ε, ball x δ := by ext y; rw [mem_closedBall, ← forall_lt_iff_le', mem_iInter₂]; rfl theorem ball_subset_ball' (h : ε₁ + dist x y ≤ ε₂) : ball x ε₁ ⊆ ball y ε₂ := fun z hz => calc dist z y ≤ dist z x + dist x y := dist_triangle _ _ _ _ < ε₁ + dist x y := add_lt_add_right (mem_ball.1 hz) _ _ ≤ ε₂ := h @[gcongr] theorem closedBall_subset_closedBall (h : ε₁ ≤ ε₂) : closedBall x ε₁ ⊆ closedBall x ε₂ := fun _y (yx : _ ≤ ε₁) => le_trans yx h theorem closedBall_subset_closedBall' (h : ε₁ + dist x y ≤ ε₂) : closedBall x ε₁ ⊆ closedBall y ε₂ := fun z hz => calc dist z y ≤ dist z x + dist x y := dist_triangle _ _ _ _ ≤ ε₁ + dist x y := add_le_add_right (mem_closedBall.1 hz) _ _ ≤ ε₂ := h theorem closedBall_subset_ball (h : ε₁ < ε₂) : closedBall x ε₁ ⊆ ball x ε₂ := fun y (yh : dist y x ≤ ε₁) => lt_of_le_of_lt yh h theorem closedBall_subset_ball' (h : ε₁ + dist x y < ε₂) : closedBall x ε₁ ⊆ ball y ε₂ := fun z hz => calc dist z y ≤ dist z x + dist x y := dist_triangle _ _ _ _ ≤ ε₁ + dist x y := add_le_add_right (mem_closedBall.1 hz) _ _ < ε₂ := h theorem dist_le_add_of_nonempty_closedBall_inter_closedBall (h : (closedBall x ε₁ ∩ closedBall y ε₂).Nonempty) : dist x y ≤ ε₁ + ε₂ := let ⟨z, hz⟩ := h calc dist x y ≤ dist z x + dist z y := dist_triangle_left _ _ _ _ ≤ ε₁ + ε₂ := add_le_add hz.1 hz.2 theorem dist_lt_add_of_nonempty_closedBall_inter_ball (h : (closedBall x ε₁ ∩ ball y ε₂).Nonempty) : dist x y < ε₁ + ε₂ := let ⟨z, hz⟩ := h calc dist x y ≤ dist z x + dist z y := dist_triangle_left _ _ _ _ < ε₁ + ε₂ := add_lt_add_of_le_of_lt hz.1 hz.2 theorem dist_lt_add_of_nonempty_ball_inter_closedBall (h : (ball x ε₁ ∩ closedBall y ε₂).Nonempty) : dist x y < ε₁ + ε₂ := by rw [inter_comm] at h rw [add_comm, dist_comm] exact dist_lt_add_of_nonempty_closedBall_inter_ball h theorem dist_lt_add_of_nonempty_ball_inter_ball (h : (ball x ε₁ ∩ ball y ε₂).Nonempty) : dist x y < ε₁ + ε₂ := dist_lt_add_of_nonempty_closedBall_inter_ball <| h.mono (inter_subset_inter ball_subset_closedBall Subset.rfl) @[simp] theorem iUnion_closedBall_nat (x : α) : ⋃ n : ℕ, closedBall x n = univ := iUnion_eq_univ_iff.2 fun y => exists_nat_ge (dist y x) theorem iUnion_inter_closedBall_nat (s : Set α) (x : α) : ⋃ n : ℕ, s ∩ closedBall x n = s := by rw [← inter_iUnion, iUnion_closedBall_nat, inter_univ] theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ := fun z zx => by rw [← add_sub_cancel ε₁ ε₂] exact lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h) theorem ball_half_subset (y) (h : y ∈ ball x (ε / 2)) : ball y (ε / 2) ⊆ ball x ε := ball_subset <| by rw [sub_self_div_two]; exact le_of_lt h theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε := ⟨_, sub_pos.2 h, ball_subset <| by rw [sub_sub_self]⟩ /-- If a property holds for all points in closed balls of arbitrarily large radii, then it holds for all points. -/ theorem forall_of_forall_mem_closedBall (p : α → Prop) (x : α) (H : ∃ᶠ R : ℝ in atTop, ∀ y ∈ closedBall x R, p y) (y : α) : p y := by obtain ⟨R, hR, h⟩ : ∃ R ≥ dist y x, ∀ z : α, z ∈ closedBall x R → p z := frequently_iff.1 H (Ici_mem_atTop (dist y x)) exact h _ hR /-- If a property holds for all points in balls of arbitrarily large radii, then it holds for all points. -/ theorem forall_of_forall_mem_ball (p : α → Prop) (x : α) (H : ∃ᶠ R : ℝ in atTop, ∀ y ∈ ball x R, p y) (y : α) : p y := by obtain ⟨R, hR, h⟩ : ∃ R > dist y x, ∀ z : α, z ∈ ball x R → p z := frequently_iff.1 H (Ioi_mem_atTop (dist y x)) exact h _ hR theorem isBounded_iff {s : Set α} : IsBounded s ↔ ∃ C : ℝ, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C := by rw [isBounded_def, ← Filter.mem_sets, @PseudoMetricSpace.cobounded_sets α, mem_setOf_eq, compl_compl] theorem isBounded_iff_eventually {s : Set α} : IsBounded s ↔ ∀ᶠ C in atTop, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C := isBounded_iff.trans ⟨fun ⟨C, h⟩ => eventually_atTop.2 ⟨C, fun _C' hC' _x hx _y hy => (h hx hy).trans hC'⟩, Eventually.exists⟩ theorem isBounded_iff_exists_ge {s : Set α} (c : ℝ) : IsBounded s ↔ ∃ C, c ≤ C ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C := ⟨fun h => ((eventually_ge_atTop c).and (isBounded_iff_eventually.1 h)).exists, fun h => isBounded_iff.2 <| h.imp fun _ => And.right⟩ theorem isBounded_iff_nndist {s : Set α} : IsBounded s ↔ ∃ C : ℝ≥0, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → nndist x y ≤ C := by simp only [isBounded_iff_exists_ge 0, NNReal.exists, ← NNReal.coe_le_coe, ← dist_nndist, NNReal.coe_mk, exists_prop] theorem toUniformSpace_eq : ‹PseudoMetricSpace α›.toUniformSpace = .ofDist dist dist_self dist_comm dist_triangle := UniformSpace.ext PseudoMetricSpace.uniformity_dist theorem uniformity_basis_dist : (𝓤 α).HasBasis (fun ε : ℝ => 0 < ε) fun ε => { p : α × α | dist p.1 p.2 < ε } := by rw [toUniformSpace_eq] exact UniformSpace.hasBasis_ofFun (exists_gt _) _ _ _ _ _ /-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`. For specific bases see `uniformity_basis_dist`, `uniformity_basis_dist_inv_nat_succ`, and `uniformity_basis_dist_inv_nat_pos`. -/ protected theorem mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ℝ} (hf₀ : ∀ i, p i → 0 < f i) (hf : ∀ ⦃ε⦄, 0 < ε → ∃ i, p i ∧ f i ≤ ε) : (𝓤 α).HasBasis p fun i => { p : α × α | dist p.1 p.2 < f i } := by refine ⟨fun s => uniformity_basis_dist.mem_iff.trans ?_⟩ constructor · rintro ⟨ε, ε₀, hε⟩ rcases hf ε₀ with ⟨i, hi, H⟩ exact ⟨i, hi, fun x (hx : _ < _) => hε <| lt_of_lt_of_le hx H⟩ · exact fun ⟨i, hi, H⟩ => ⟨f i, hf₀ i hi, H⟩ theorem uniformity_basis_dist_rat : (𝓤 α).HasBasis (fun r : ℚ => 0 < r) fun r => { p : α × α | dist p.1 p.2 < r } := Metric.mk_uniformity_basis (fun _ => Rat.cast_pos.2) fun _ε hε => let ⟨r, hr0, hrε⟩ := exists_rat_btwn hε ⟨r, Rat.cast_pos.1 hr0, hrε.le⟩ theorem uniformity_basis_dist_inv_nat_succ : (𝓤 α).HasBasis (fun _ => True) fun n : ℕ => { p : α × α | dist p.1 p.2 < 1 / (↑n + 1) } := Metric.mk_uniformity_basis (fun n _ => div_pos zero_lt_one <| Nat.cast_add_one_pos n) fun _ε ε0 => (exists_nat_one_div_lt ε0).imp fun _n hn => ⟨trivial, le_of_lt hn⟩ theorem uniformity_basis_dist_inv_nat_pos : (𝓤 α).HasBasis (fun n : ℕ => 0 < n) fun n : ℕ => { p : α × α | dist p.1 p.2 < 1 / ↑n } := Metric.mk_uniformity_basis (fun _ hn => div_pos zero_lt_one <| Nat.cast_pos.2 hn) fun _ ε0 => let ⟨n, hn⟩ := exists_nat_one_div_lt ε0 ⟨n + 1, Nat.succ_pos n, mod_cast hn.le⟩ theorem uniformity_basis_dist_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓤 α).HasBasis (fun _ : ℕ => True) fun n : ℕ => { p : α × α | dist p.1 p.2 < r ^ n } := Metric.mk_uniformity_basis (fun _ _ => pow_pos h0 _) fun _ε ε0 => let ⟨n, hn⟩ := exists_pow_lt_of_lt_one ε0 h1 ⟨n, trivial, hn.le⟩ theorem uniformity_basis_dist_lt {R : ℝ} (hR : 0 < R) : (𝓤 α).HasBasis (fun r : ℝ => 0 < r ∧ r < R) fun r => { p : α × α | dist p.1 p.2 < r } := Metric.mk_uniformity_basis (fun _ => And.left) fun r hr => ⟨min r (R / 2), ⟨lt_min hr (half_pos hR), min_lt_iff.2 <| Or.inr (half_lt_self hR)⟩, min_le_left _ _⟩ /-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then closed neighborhoods of the diagonal of sizes `{f i | p i}` form a basis of `𝓤 α`. Currently we have only one specific basis `uniformity_basis_dist_le` based on this constructor. More can be easily added if needed in the future. -/ protected theorem mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ℝ} (hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x, p x ∧ f x ≤ ε) : (𝓤 α).HasBasis p fun x => { p : α × α | dist p.1 p.2 ≤ f x } := by refine ⟨fun s => uniformity_basis_dist.mem_iff.trans ?_⟩ constructor · rintro ⟨ε, ε₀, hε⟩ rcases exists_between ε₀ with ⟨ε', hε'⟩ rcases hf ε' hε'.1 with ⟨i, hi, H⟩ exact ⟨i, hi, fun x (hx : _ ≤ _) => hε <| lt_of_le_of_lt (le_trans hx H) hε'.2⟩ · exact fun ⟨i, hi, H⟩ => ⟨f i, hf₀ i hi, fun x (hx : _ < _) => H (mem_setOf.2 hx.le)⟩ /-- Constant size closed neighborhoods of the diagonal form a basis of the uniformity filter. -/ theorem uniformity_basis_dist_le : (𝓤 α).HasBasis ((0 : ℝ) < ·) fun ε => { p : α × α | dist p.1 p.2 ≤ ε } := Metric.mk_uniformity_basis_le (fun _ => id) fun ε ε₀ => ⟨ε, ε₀, le_refl ε⟩ theorem uniformity_basis_dist_le_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓤 α).HasBasis (fun _ : ℕ => True) fun n : ℕ => { p : α × α | dist p.1 p.2 ≤ r ^ n } := Metric.mk_uniformity_basis_le (fun _ _ => pow_pos h0 _) fun _ε ε0 => let ⟨n, hn⟩ := exists_pow_lt_of_lt_one ε0 h1 ⟨n, trivial, hn.le⟩ theorem mem_uniformity_dist {s : Set (α × α)} : s ∈ 𝓤 α ↔ ∃ ε > 0, ∀ ⦃a b : α⦄, dist a b < ε → (a, b) ∈ s := uniformity_basis_dist.mem_uniformity_iff /-- A constant size neighborhood of the diagonal is an entourage. -/ theorem dist_mem_uniformity {ε : ℝ} (ε0 : 0 < ε) : { p : α × α | dist p.1 p.2 < ε } ∈ 𝓤 α := mem_uniformity_dist.2 ⟨ε, ε0, fun _ _ ↦ id⟩ theorem uniformContinuous_iff [PseudoMetricSpace β] {f : α → β} : UniformContinuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀ ⦃a b : α⦄, dist a b < δ → dist (f a) (f b) < ε := uniformity_basis_dist.uniformContinuous_iff uniformity_basis_dist theorem uniformContinuousOn_iff [PseudoMetricSpace β] {f : α → β} {s : Set α} : UniformContinuousOn f s ↔ ∀ ε > 0, ∃ δ > 0, ∀ x ∈ s, ∀ y ∈ s, dist x y < δ → dist (f x) (f y) < ε := Metric.uniformity_basis_dist.uniformContinuousOn_iff Metric.uniformity_basis_dist theorem uniformContinuousOn_iff_le [PseudoMetricSpace β] {f : α → β} {s : Set α} : UniformContinuousOn f s ↔ ∀ ε > 0, ∃ δ > 0, ∀ x ∈ s, ∀ y ∈ s, dist x y ≤ δ → dist (f x) (f y) ≤ ε := Metric.uniformity_basis_dist_le.uniformContinuousOn_iff Metric.uniformity_basis_dist_le theorem nhds_basis_ball : (𝓝 x).HasBasis (0 < ·) (ball x) := nhds_basis_uniformity uniformity_basis_dist theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ ε > 0, ball x ε ⊆ s := nhds_basis_ball.mem_iff theorem eventually_nhds_iff {p : α → Prop} : (∀ᶠ y in 𝓝 x, p y) ↔ ∃ ε > 0, ∀ ⦃y⦄, dist y x < ε → p y := mem_nhds_iff theorem eventually_nhds_iff_ball {p : α → Prop} : (∀ᶠ y in 𝓝 x, p y) ↔ ∃ ε > 0, ∀ y ∈ ball x ε, p y := mem_nhds_iff /-- A version of `Filter.eventually_prod_iff` where the first filter consists of neighborhoods in a pseudo-metric space. -/ theorem eventually_nhds_prod_iff {f : Filter ι} {x₀ : α} {p : α × ι → Prop} : (∀ᶠ x in 𝓝 x₀ ×ˢ f, p x) ↔ ∃ ε > (0 : ℝ), ∃ pa : ι → Prop, (∀ᶠ i in f, pa i) ∧ ∀ ⦃x⦄, dist x x₀ < ε → ∀ ⦃i⦄, pa i → p (x, i) := by refine (nhds_basis_ball.prod f.basis_sets).eventually_iff.trans ?_ simp only [Prod.exists, forall_prod_set, id, mem_ball, and_assoc, exists_and_left, and_imp] rfl /-- A version of `Filter.eventually_prod_iff` where the second filter consists of neighborhoods in a pseudo-metric space. -/ theorem eventually_prod_nhds_iff {f : Filter ι} {x₀ : α} {p : ι × α → Prop} : (∀ᶠ x in f ×ˢ 𝓝 x₀, p x) ↔ ∃ pa : ι → Prop, (∀ᶠ i in f, pa i) ∧ ∃ ε > 0, ∀ ⦃i⦄, pa i → ∀ ⦃x⦄, dist x x₀ < ε → p (i, x) := by rw [eventually_swap_iff, Metric.eventually_nhds_prod_iff] constructor <;> · rintro ⟨a1, a2, a3, a4, a5⟩ exact ⟨a3, a4, a1, a2, fun _ b1 b2 b3 => a5 b3 b1⟩ theorem nhds_basis_closedBall : (𝓝 x).HasBasis (fun ε : ℝ => 0 < ε) (closedBall x) := nhds_basis_uniformity uniformity_basis_dist_le theorem nhds_basis_ball_inv_nat_succ : (𝓝 x).HasBasis (fun _ => True) fun n : ℕ => ball x (1 / (↑n + 1)) := nhds_basis_uniformity uniformity_basis_dist_inv_nat_succ theorem nhds_basis_ball_inv_nat_pos : (𝓝 x).HasBasis (fun n => 0 < n) fun n : ℕ => ball x (1 / ↑n) := nhds_basis_uniformity uniformity_basis_dist_inv_nat_pos theorem nhds_basis_ball_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓝 x).HasBasis (fun _ => True) fun n : ℕ => ball x (r ^ n) := nhds_basis_uniformity (uniformity_basis_dist_pow h0 h1) theorem nhds_basis_closedBall_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓝 x).HasBasis (fun _ => True) fun n : ℕ => closedBall x (r ^ n) := nhds_basis_uniformity (uniformity_basis_dist_le_pow h0 h1) theorem isOpen_iff : IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, ball x ε ⊆ s := by simp only [isOpen_iff_mem_nhds, mem_nhds_iff] @[simp] theorem isOpen_ball : IsOpen (ball x ε) := isOpen_iff.2 fun _ => exists_ball_subset_ball theorem ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x := isOpen_ball.mem_nhds (mem_ball_self ε0) theorem closedBall_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : closedBall x ε ∈ 𝓝 x := mem_of_superset (ball_mem_nhds x ε0) ball_subset_closedBall theorem closedBall_mem_nhds_of_mem {x c : α} {ε : ℝ} (h : x ∈ ball c ε) : closedBall c ε ∈ 𝓝 x := mem_of_superset (isOpen_ball.mem_nhds h) ball_subset_closedBall theorem nhdsWithin_basis_ball {s : Set α} : (𝓝[s] x).HasBasis (fun ε : ℝ => 0 < ε) fun ε => ball x ε ∩ s := nhdsWithin_hasBasis nhds_basis_ball s theorem mem_nhdsWithin_iff {t : Set α} : s ∈ 𝓝[t] x ↔ ∃ ε > 0, ball x ε ∩ t ⊆ s := nhdsWithin_basis_ball.mem_iff theorem tendsto_nhdsWithin_nhdsWithin [PseudoMetricSpace β] {t : Set β} {f : α → β} {a b} : Tendsto f (𝓝[s] a) (𝓝[t] b) ↔ ∀ ε > 0, ∃ δ > 0, ∀ ⦃x : α⦄, x ∈ s → dist x a < δ → f x ∈ t ∧ dist (f x) b < ε := (nhdsWithin_basis_ball.tendsto_iff nhdsWithin_basis_ball).trans <| by simp only [inter_comm _ s, inter_comm _ t, mem_inter_iff, and_imp, gt_iff_lt, mem_ball] theorem tendsto_nhdsWithin_nhds [PseudoMetricSpace β] {f : α → β} {a b} : Tendsto f (𝓝[s] a) (𝓝 b) ↔ ∀ ε > 0, ∃ δ > 0, ∀ ⦃x : α⦄, x ∈ s → dist x a < δ → dist (f x) b < ε := by rw [← nhdsWithin_univ b, tendsto_nhdsWithin_nhdsWithin] simp only [mem_univ, true_and] theorem tendsto_nhds_nhds [PseudoMetricSpace β] {f : α → β} {a b} : Tendsto f (𝓝 a) (𝓝 b) ↔ ∀ ε > 0, ∃ δ > 0, ∀ ⦃x : α⦄, dist x a < δ → dist (f x) b < ε := nhds_basis_ball.tendsto_iff nhds_basis_ball theorem continuousAt_iff [PseudoMetricSpace β] {f : α → β} {a : α} : ContinuousAt f a ↔ ∀ ε > 0, ∃ δ > 0, ∀ ⦃x : α⦄, dist x a < δ → dist (f x) (f a) < ε := by rw [ContinuousAt, tendsto_nhds_nhds] theorem continuousWithinAt_iff [PseudoMetricSpace β] {f : α → β} {a : α} {s : Set α} : ContinuousWithinAt f s a ↔ ∀ ε > 0, ∃ δ > 0, ∀ ⦃x : α⦄, x ∈ s → dist x a < δ → dist (f x) (f a) < ε := by rw [ContinuousWithinAt, tendsto_nhdsWithin_nhds] theorem continuousOn_iff [PseudoMetricSpace β] {f : α → β} {s : Set α} : ContinuousOn f s ↔ ∀ b ∈ s, ∀ ε > 0, ∃ δ > 0, ∀ a ∈ s, dist a b < δ → dist (f a) (f b) < ε := by simp [ContinuousOn, continuousWithinAt_iff] theorem continuous_iff [PseudoMetricSpace β] {f : α → β} : Continuous f ↔ ∀ b, ∀ ε > 0, ∃ δ > 0, ∀ a, dist a b < δ → dist (f a) (f b) < ε := continuous_iff_continuousAt.trans <| forall_congr' fun _ => tendsto_nhds_nhds theorem tendsto_nhds {f : Filter β} {u : β → α} {a : α} : Tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, dist (u x) a < ε := nhds_basis_ball.tendsto_right_iff theorem continuousAt_iff' [TopologicalSpace β] {f : β → α} {b : β} : ContinuousAt f b ↔ ∀ ε > 0, ∀ᶠ x in 𝓝 b, dist (f x) (f b) < ε := by rw [ContinuousAt, tendsto_nhds] theorem continuousWithinAt_iff' [TopologicalSpace β] {f : β → α} {b : β} {s : Set β} : ContinuousWithinAt f s b ↔ ∀ ε > 0, ∀ᶠ x in 𝓝[s] b, dist (f x) (f b) < ε := by rw [ContinuousWithinAt, tendsto_nhds] theorem continuousOn_iff' [TopologicalSpace β] {f : β → α} {s : Set β} : ContinuousOn f s ↔ ∀ b ∈ s, ∀ ε > 0, ∀ᶠ x in 𝓝[s] b, dist (f x) (f b) < ε := by simp [ContinuousOn, continuousWithinAt_iff'] theorem continuous_iff' [TopologicalSpace β] {f : β → α} : Continuous f ↔ ∀ (a), ∀ ε > 0, ∀ᶠ x in 𝓝 a, dist (f x) (f a) < ε := continuous_iff_continuousAt.trans <| forall_congr' fun _ => tendsto_nhds theorem tendsto_atTop [Nonempty β] [SemilatticeSup β] {u : β → α} {a : α} : Tendsto u atTop (𝓝 a) ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, dist (u n) a < ε := (atTop_basis.tendsto_iff nhds_basis_ball).trans <| by simp only [true_and, mem_ball, mem_Ici] /-- A variant of `tendsto_atTop` that uses `∃ N, ∀ n > N, ...` rather than `∃ N, ∀ n ≥ N, ...` -/ theorem tendsto_atTop' [Nonempty β] [SemilatticeSup β] [NoMaxOrder β] {u : β → α} {a : α} : Tendsto u atTop (𝓝 a) ↔ ∀ ε > 0, ∃ N, ∀ n > N, dist (u n) a < ε := (atTop_basis_Ioi.tendsto_iff nhds_basis_ball).trans <| by simp only [true_and, gt_iff_lt, mem_Ioi, mem_ball] theorem isOpen_singleton_iff {α : Type*} [PseudoMetricSpace α] {x : α} : IsOpen ({x} : Set α) ↔ ∃ ε > 0, ∀ y, dist y x < ε → y = x := by simp [isOpen_iff, subset_singleton_iff, mem_ball] theorem _root_.Dense.exists_dist_lt {s : Set α} (hs : Dense s) (x : α) {ε : ℝ} (hε : 0 < ε) : ∃ y ∈ s, dist x y < ε := by have : (ball x ε).Nonempty := by simp [hε] simpa only [mem_ball'] using hs.exists_mem_open isOpen_ball this nonrec theorem _root_.DenseRange.exists_dist_lt {β : Type*} {f : β → α} (hf : DenseRange f) (x : α) {ε : ℝ} (hε : 0 < ε) : ∃ y, dist x (f y) < ε := exists_range_iff.1 (hf.exists_dist_lt x hε) /-- (Pseudo) metric space has discrete `UniformSpace` structure iff the distances between distinct points are uniformly bounded away from zero. -/ protected lemma uniformSpace_eq_bot : ‹PseudoMetricSpace α›.toUniformSpace = ⊥ ↔ ∃ r : ℝ, 0 < r ∧ Pairwise (r ≤ dist · · : α → α → Prop) := by simp only [uniformity_basis_dist.uniformSpace_eq_bot, mem_setOf_eq, not_lt] end Metric open Metric /-- If the distances between distinct points in a (pseudo) metric space are uniformly bounded away from zero, then the space has discrete topology. -/ lemma DiscreteTopology.of_forall_le_dist {α} [PseudoMetricSpace α] {r : ℝ} (hpos : 0 < r) (hr : Pairwise (r ≤ dist · · : α → α → Prop)) : DiscreteTopology α := ⟨by rw [Metric.uniformSpace_eq_bot.2 ⟨r, hpos, hr⟩, UniformSpace.toTopologicalSpace_bot]⟩ /- Instantiate a pseudometric space as a pseudoemetric space. Before we can state the instance, we need to show that the uniform structure coming from the edistance and the distance coincide. -/ theorem Metric.uniformity_edist_aux {α} (d : α → α → ℝ≥0) : ⨅ ε > (0 : ℝ), 𝓟 { p : α × α | ↑(d p.1 p.2) < ε } = ⨅ ε > (0 : ℝ≥0∞), 𝓟 { p : α × α | ↑(d p.1 p.2) < ε } := by simp only [le_antisymm_iff, le_iInf_iff, le_principal_iff] refine ⟨fun ε hε => ?_, fun ε hε => ?_⟩ · rcases ENNReal.lt_iff_exists_nnreal_btwn.1 hε with ⟨ε', ε'0, ε'ε⟩ refine mem_iInf_of_mem (ε' : ℝ) (mem_iInf_of_mem (ENNReal.coe_pos.1 ε'0) ?_) exact fun x hx => lt_trans (ENNReal.coe_lt_coe.2 hx) ε'ε · lift ε to ℝ≥0 using le_of_lt hε refine mem_iInf_of_mem (ε : ℝ≥0∞) (mem_iInf_of_mem (ENNReal.coe_pos.2 hε) ?_) exact fun _ => ENNReal.coe_lt_coe.1 theorem Metric.uniformity_edist : 𝓤 α = ⨅ ε > 0, 𝓟 { p : α × α | edist p.1 p.2 < ε } := by simp only [PseudoMetricSpace.uniformity_dist, dist_nndist, edist_nndist, Metric.uniformity_edist_aux] -- see Note [lower instance priority] /-- A pseudometric space induces a pseudoemetric space -/ instance (priority := 100) PseudoMetricSpace.toPseudoEMetricSpace : PseudoEMetricSpace α := { ‹PseudoMetricSpace α› with edist_self := by simp [edist_dist] edist_comm := fun _ _ => by simp only [edist_dist, dist_comm] edist_triangle := fun x y z => by simp only [edist_dist, ← ENNReal.ofReal_add, dist_nonneg] rw [ENNReal.ofReal_le_ofReal_iff _] · exact dist_triangle _ _ _ · simpa using add_le_add (dist_nonneg : 0 ≤ dist x y) dist_nonneg uniformity_edist := Metric.uniformity_edist } /-- In a pseudometric space, an open ball of infinite radius is the whole space -/ theorem Metric.eball_top_eq_univ (x : α) : EMetric.ball x ∞ = Set.univ := Set.eq_univ_iff_forall.mpr fun y => edist_lt_top y x /-- Balls defined using the distance or the edistance coincide -/ @[simp] theorem Metric.emetric_ball {x : α} {ε : ℝ} : EMetric.ball x (ENNReal.ofReal ε) = ball x ε := by ext y simp only [EMetric.mem_ball, mem_ball, edist_dist] exact ENNReal.ofReal_lt_ofReal_iff_of_nonneg dist_nonneg /-- Balls defined using the distance or the edistance coincide -/ @[simp] theorem Metric.emetric_ball_nnreal {x : α} {ε : ℝ≥0} : EMetric.ball x ε = ball x ε := by rw [← Metric.emetric_ball] simp /-- Closed balls defined using the distance or the edistance coincide -/ theorem Metric.emetric_closedBall {x : α} {ε : ℝ} (h : 0 ≤ ε) : EMetric.closedBall x (ENNReal.ofReal ε) = closedBall x ε := by ext y; simp [edist_le_ofReal h] /-- Closed balls defined using the distance or the edistance coincide -/ @[simp] theorem Metric.emetric_closedBall_nnreal {x : α} {ε : ℝ≥0} : EMetric.closedBall x ε = closedBall x ε := by rw [← Metric.emetric_closedBall ε.coe_nonneg, ENNReal.ofReal_coe_nnreal] @[simp] theorem Metric.emetric_ball_top (x : α) : EMetric.ball x ⊤ = univ := eq_univ_of_forall fun _ => edist_lt_top _ _ /-- Build a new pseudometric space from an old one where the bundled uniform structure is provably (but typically non-definitionaly) equal to some given uniform structure. See Note [forgetful inheritance]. See Note [reducible non-instances]. -/ abbrev PseudoMetricSpace.replaceUniformity {α} [U : UniformSpace α] (m : PseudoMetricSpace α) (H : 𝓤[U] = 𝓤[PseudoEMetricSpace.toUniformSpace]) : PseudoMetricSpace α := { m with toUniformSpace := U uniformity_dist := H.trans PseudoMetricSpace.uniformity_dist } theorem PseudoMetricSpace.replaceUniformity_eq {α} [U : UniformSpace α] (m : PseudoMetricSpace α) (H : 𝓤[U] = 𝓤[PseudoEMetricSpace.toUniformSpace]) : m.replaceUniformity H = m := by ext rfl
-- ensure that the bornology is unchanged when replacing the uniformity. example {α} [U : UniformSpace α] (m : PseudoMetricSpace α) (H : 𝓤[U] = 𝓤[PseudoEMetricSpace.toUniformSpace]) : (PseudoMetricSpace.replaceUniformity m H).toBornology = m.toBornology := by with_reducible_and_instances rfl
Mathlib/Topology/MetricSpace/Pseudo/Defs.lean
965
970
/- Copyright (c) 2024 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler -/ import Mathlib.NumberTheory.LSeries.RiemannZeta import Mathlib.NumberTheory.Harmonic.GammaDeriv /-! # Asymptotics of `ζ s` as `s → 1` The goal of this file is to evaluate the limit of `ζ s - 1 / (s - 1)` as `s → 1`. ### Main results * `tendsto_riemannZeta_sub_one_div`: the limit of `ζ s - 1 / (s - 1)`, at the filter of punctured neighbourhoods of 1 in `ℂ`, exists and is equal to the Euler-Mascheroni constant `γ`. * `riemannZeta_one_ne_zero`: with our definition of `ζ 1` (which is characterised as the limit of `ζ s - 1 / (s - 1) / Gammaℝ s` as `s → 1`), we have `ζ 1 ≠ 0`. ### Outline of arguments We consider the sum `F s = ∑' n : ℕ, f (n + 1) s`, where `s` is a real variable and `f n s = ∫ x in n..(n + 1), (x - n) / x ^ (s + 1)`. We show that `F s` is continuous on `[1, ∞)`, that `F 1 = 1 - γ`, and that `F s = 1 / (s - 1) - ζ s / s` for `1 < s`. By combining these formulae, one deduces that the limit of `ζ s - 1 / (s - 1)` at `𝓝[>] (1 : ℝ)` exists and is equal to `γ`. Finally, using this and the Riemann removable singularity criterion we obtain the limit along punctured neighbourhoods of 1 in `ℂ`. -/ open Real Set MeasureTheory Filter Topology @[inherit_doc] local notation "γ" => eulerMascheroniConstant namespace ZetaAsymptotics -- since the intermediate lemmas are of little interest in themselves we put them in a namespace /-! ## Definitions -/ /-- Auxiliary function used in studying zeta-function asymptotics. -/ noncomputable def term (n : ℕ) (s : ℝ) : ℝ := ∫ x : ℝ in n..(n + 1), (x - n) / x ^ (s + 1) /-- Sum of finitely many `term`s. -/ noncomputable def term_sum (s : ℝ) (N : ℕ) : ℝ := ∑ n ∈ Finset.range N, term (n + 1) s /-- Topological sum of `term`s. -/ noncomputable def term_tsum (s : ℝ) : ℝ := ∑' n, term (n + 1) s lemma term_nonneg (n : ℕ) (s : ℝ) : 0 ≤ term n s := by rw [term, intervalIntegral.integral_of_le (by simp)] refine setIntegral_nonneg measurableSet_Ioc (fun x hx ↦ ?_) refine div_nonneg ?_ (rpow_nonneg ?_ _) all_goals linarith [hx.1] lemma term_welldef {n : ℕ} (hn : 0 < n) {s : ℝ} (hs : 0 < s) : IntervalIntegrable (fun x : ℝ ↦ (x - n) / x ^ (s + 1)) volume n (n + 1) := by rw [intervalIntegrable_iff_integrableOn_Icc_of_le (by linarith)] refine (continuousOn_of_forall_continuousAt fun x hx ↦ ContinuousAt.div ?_ ?_ ?_).integrableOn_Icc · fun_prop · apply continuousAt_id.rpow_const (Or.inr <| by linarith) · exact (rpow_pos_of_pos ((Nat.cast_pos.mpr hn).trans_le hx.1) _).ne' section s_eq_one /-! ## Evaluation of the sum for `s = 1` -/ lemma term_one {n : ℕ} (hn : 0 < n) : term n 1 = (log (n + 1) - log n) - 1 / (n + 1) := by have hv : ∀ x ∈ uIcc (n : ℝ) (n + 1), 0 < x := by intro x hx rw [uIcc_of_le (by simp only [le_add_iff_nonneg_right, zero_le_one])] at hx exact (Nat.cast_pos.mpr hn).trans_le hx.1 calc term n 1 _ = ∫ x : ℝ in n..(n + 1), (x - n) / x ^ 2 := by simp_rw [term, one_add_one_eq_two, ← Nat.cast_two (R := ℝ), rpow_natCast] _ = ∫ x : ℝ in n..(n + 1), (1 / x - n / x ^ 2) := by refine intervalIntegral.integral_congr (fun x hx ↦ ?_) field_simp [(hv x hx).ne'] ring _ = (∫ x : ℝ in n..(n + 1), 1 / x) - n * ∫ x : ℝ in n..(n + 1), 1 / x ^ 2 := by simp_rw [← mul_one_div (n : ℝ)] rw [intervalIntegral.integral_sub] · simp_rw [intervalIntegral.integral_const_mul] · exact intervalIntegral.intervalIntegrable_one_div (fun x hx ↦ (hv x hx).ne') (by fun_prop) · exact (intervalIntegral.intervalIntegrable_one_div (fun x hx ↦ (sq_pos_of_pos (hv x hx)).ne') (by fun_prop)).const_mul _ _ = (log (↑n + 1) - log ↑n) - n * ∫ x : ℝ in n..(n + 1), 1 / x ^ 2 := by congr 1 rw [integral_one_div_of_pos, log_div] all_goals positivity _ = (log (↑n + 1) - log ↑n) - n * ∫ x : ℝ in n..(n + 1), x ^ (-2 : ℝ) := by congr 2 refine intervalIntegral.integral_congr (fun x hx ↦ ?_) rw [rpow_neg, one_div, ← Nat.cast_two (R := ℝ), rpow_natCast] exact (hv x hx).le _ = log (↑n + 1) - log ↑n - n * (1 / n - 1 / (n + 1)) := by rw [integral_rpow] · simp_rw [sub_div, (by norm_num : (-2 : ℝ) + 1 = -1), div_neg, div_one, neg_sub_neg, rpow_neg_one, ← one_div] · refine Or.inr ⟨by norm_num, not_mem_uIcc_of_lt ?_ ?_⟩ all_goals positivity _ = log (↑n + 1) - log ↑n - 1 / (↑n + 1) := by congr 1 field_simp lemma term_sum_one (N : ℕ) : term_sum 1 N = log (N + 1) - harmonic (N + 1) + 1 := by induction N with | zero => simp_rw [term_sum, Finset.sum_range_zero, harmonic_succ, harmonic_zero, Nat.cast_zero, zero_add, Nat.cast_one, inv_one, Rat.cast_one, log_one, sub_add_cancel] | succ N hN => unfold term_sum at hN ⊢ rw [Finset.sum_range_succ, hN, harmonic_succ (N + 1), term_one (by positivity : 0 < N + 1)] push_cast ring_nf /-- The topological sum of `ZetaAsymptotics.term (n + 1) 1` over all `n : ℕ` is `1 - γ`. This is proved by directly evaluating the sum of the first `N` terms and using the limit definition of `γ`. -/ lemma term_tsum_one : HasSum (fun n ↦ term (n + 1) 1) (1 - γ) := by rw [hasSum_iff_tendsto_nat_of_nonneg (fun n ↦ term_nonneg (n + 1) 1)] show Tendsto (fun N ↦ term_sum 1 N) atTop _ simp_rw [term_sum_one, sub_eq_neg_add] refine Tendsto.add ?_ tendsto_const_nhds have := (tendsto_eulerMascheroniSeq'.comp (tendsto_add_atTop_nat 1)).neg refine this.congr' (Eventually.of_forall (fun n ↦ ?_)) simp_rw [Function.comp_apply, eulerMascheroniSeq', reduceCtorEq, if_false] push_cast abel end s_eq_one section s_gt_one /-! ## Evaluation of the sum for `1 < s` -/ lemma term_of_lt {n : ℕ} (hn : 0 < n) {s : ℝ} (hs : 1 < s) : term n s = 1 / (s - 1) * (1 / n ^ (s - 1) - 1 / (n + 1) ^ (s - 1)) - n / s * (1 / n ^ s - 1 / (n + 1) ^ s) := by have hv : ∀ x ∈ uIcc (n : ℝ) (n + 1), 0 < x := by intro x hx rw [uIcc_of_le (by simp only [le_add_iff_nonneg_right, zero_le_one])] at hx exact (Nat.cast_pos.mpr hn).trans_le hx.1 calc term n s _ = ∫ x : ℝ in n..(n + 1), (x - n) / x ^ (s + 1) := by rfl _ = ∫ x : ℝ in n..(n + 1), (x ^ (-s) - n * x ^ (-(s + 1))) := by refine intervalIntegral.integral_congr (fun x hx ↦ ?_) rw [sub_div, rpow_add_one (hv x hx).ne', mul_comm, ← div_div, div_self (hv x hx).ne', rpow_neg (hv x hx).le, rpow_neg (hv x hx).le, one_div, rpow_add_one (hv x hx).ne', mul_comm, div_eq_mul_inv] _ = (∫ x : ℝ in n..(n + 1), x ^ (-s)) - n * (∫ x : ℝ in n..(n + 1), x ^ (-(s + 1))) := by rw [intervalIntegral.integral_sub, intervalIntegral.integral_const_mul] <;> [skip; apply IntervalIntegrable.const_mul] <;> · refine intervalIntegral.intervalIntegrable_rpow (Or.inr <| not_mem_uIcc_of_lt ?_ ?_) · exact_mod_cast hn · linarith _ = 1 / (s - 1) * (1 / n ^ (s - 1) - 1 / (n + 1) ^ (s - 1)) - n / s * (1 / n ^ s - 1 / (n + 1) ^ s) := by have : 0 ∉ uIcc (n : ℝ) (n + 1) := (lt_irrefl _ <| hv _ ·) rw [integral_rpow (Or.inr ⟨by linarith, this⟩), integral_rpow (Or.inr ⟨by linarith, this⟩)] congr 1 · rw [show -s + 1 = -(s - 1) by ring, div_neg, ← neg_div, mul_comm, mul_one_div, neg_sub, rpow_neg (Nat.cast_nonneg _), one_div, rpow_neg (by linarith), one_div] · rw [show -(s + 1) + 1 = -s by ring, div_neg, ← neg_div, neg_sub, div_mul_eq_mul_div, mul_div_assoc, rpow_neg (Nat.cast_nonneg _), one_div, rpow_neg (by linarith), one_div] lemma term_sum_of_lt (N : ℕ) {s : ℝ} (hs : 1 < s) : term_sum s N = 1 / (s - 1) * (1 - 1 / (N + 1) ^ (s - 1)) - 1 / s * ((∑ n ∈ Finset.range N, 1 / (n + 1 : ℝ) ^ s) - N / (N + 1) ^ s) := by simp only [term_sum] conv => enter [1, 2, n]; rw [term_of_lt (by simp) hs] rw [Finset.sum_sub_distrib] congr 1 · induction N with | zero => simp | succ N hN => rw [Finset.sum_range_succ, hN, Nat.cast_add_one] ring_nf · simp_rw [mul_comm (_ / _), ← mul_div_assoc, div_eq_mul_inv _ s, ← Finset.sum_mul, mul_one] congr 1 induction N with | zero => simp | succ N hN => simp_rw [Finset.sum_range_succ, hN, Nat.cast_add_one, sub_eq_add_neg, add_assoc] congr 1 ring_nf /-- For `1 < s`, the topological sum of `ZetaAsymptotics.term (n + 1) s` over all `n : ℕ` is `1 / (s - 1) - ζ s / s`. -/ lemma term_tsum_of_lt {s : ℝ} (hs : 1 < s) : term_tsum s = (1 / (s - 1) - 1 / s * ∑' n : ℕ, 1 / (n + 1 : ℝ) ^ s) := by apply HasSum.tsum_eq rw [hasSum_iff_tendsto_nat_of_nonneg (fun n ↦ term_nonneg (n + 1) s)] change Tendsto (fun N ↦ term_sum s N) atTop _ simp_rw [term_sum_of_lt _ hs] apply Tendsto.sub · rw [show 𝓝 (1 / (s - 1)) = 𝓝 (1 / (s - 1) - 1 / (s - 1) * 0) by simp] simp_rw [mul_sub, mul_one] refine tendsto_const_nhds.sub (Tendsto.const_mul _ ?_) refine tendsto_const_nhds.div_atTop <| (tendsto_rpow_atTop (by linarith)).comp ?_ exact tendsto_atTop_add_const_right _ _ tendsto_natCast_atTop_atTop · rw [← sub_zero (tsum _)] apply (((Summable.hasSum ?_).tendsto_sum_nat).sub ?_).const_mul · exact_mod_cast (summable_nat_add_iff 1).mpr (summable_one_div_nat_rpow.mpr hs) · apply tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds · change Tendsto (fun n : ℕ ↦ (1 / ↑(n + 1) : ℝ) ^ (s - 1)) .. rw [show 𝓝 (0 : ℝ) = 𝓝 (0 ^ (s - 1)) by rw [zero_rpow]; linarith] refine Tendsto.rpow_const ?_ (Or.inr <| by linarith) exact (tendsto_const_div_atTop_nhds_zero_nat _).comp (tendsto_add_atTop_nat _) · intro n positivity · intro n dsimp only transitivity (n + 1) / (n + 1) ^ s · gcongr linarith · apply le_of_eq rw [rpow_sub_one, ← div_mul, div_one, mul_comm, one_div, inv_rpow, ← div_eq_mul_inv] · norm_cast all_goals positivity /-- Reformulation of `ZetaAsymptotics.term_tsum_of_lt` which is useful for some computations below. -/ lemma zeta_limit_aux1 {s : ℝ} (hs : 1 < s) : (∑' n : ℕ, 1 / (n + 1 : ℝ) ^ s) - 1 / (s - 1) = 1 - s * term_tsum s := by rw [term_tsum_of_lt hs] generalize (∑' n : ℕ, 1 / (n + 1 : ℝ) ^ s) = Z field_simp [(show s - 1 ≠ 0 by linarith)] ring_nf end s_gt_one section continuity /-! ## Continuity of the sum -/ lemma continuousOn_term (n : ℕ) : ContinuousOn (fun x ↦ term (n + 1) x) (Ici 1) := by -- TODO: can this be shortened using the lemma -- `continuous_parametric_intervalIntegral_of_continuous'` from https://github.com/leanprover-community/mathlib4/pull/11185? simp only [term, intervalIntegral.integral_of_le (by linarith : (↑(n + 1) : ℝ) ≤ ↑(n + 1) + 1)] apply continuousOn_of_dominated (bound := fun x ↦ (x - ↑(n + 1)) / x ^ (2 : ℝ)) · exact fun s hs ↦ (term_welldef (by simp) (zero_lt_one.trans_le hs)).1.1 · intro s (hs : 1 ≤ s) rw [ae_restrict_iff' measurableSet_Ioc] filter_upwards with x hx have : 0 < x := lt_trans (by positivity) hx.1 rw [norm_of_nonneg (div_nonneg (sub_nonneg.mpr hx.1.le) (by positivity)), Nat.cast_add_one] apply div_le_div_of_nonneg_left · exact_mod_cast sub_nonneg.mpr hx.1.le · positivity · exact rpow_le_rpow_of_exponent_le (le_trans (by simp) hx.1.le) (by linarith) · rw [← IntegrableOn, ← intervalIntegrable_iff_integrableOn_Ioc_of_le (by linarith)] exact_mod_cast term_welldef (by omega : 0 < (n + 1)) zero_lt_one · rw [ae_restrict_iff' measurableSet_Ioc] filter_upwards with x hx refine continuousOn_of_forall_continuousAt (fun s (hs : 1 ≤ s) ↦ continuousAt_const.div ?_ ?_) · exact continuousAt_const.rpow (continuousAt_id.add continuousAt_const) (Or.inr (by linarith)) · exact (rpow_pos_of_pos ((Nat.cast_pos.mpr (by simp)).trans hx.1) _).ne' lemma continuousOn_term_tsum : ContinuousOn term_tsum (Ici 1) := by -- We use dominated convergence, using `fun n ↦ term n 1` as our uniform bound (since `term` is -- monotone decreasing in `s`.) refine continuousOn_tsum (fun i ↦ continuousOn_term _) term_tsum_one.summable (fun n s hs ↦ ?_) rw [term, term, norm_of_nonneg] · simp_rw [intervalIntegral.integral_of_le (by linarith : (↑(n + 1) : ℝ) ≤ ↑(n + 1) + 1)] refine setIntegral_mono_on ?_ ?_ measurableSet_Ioc (fun x hx ↦ ?_) · exact (term_welldef n.succ_pos (zero_lt_one.trans_le hs)).1 · exact (term_welldef n.succ_pos zero_lt_one).1 · rw [div_le_div_iff_of_pos_left] -- leave side-goals to end and kill them all together · apply rpow_le_rpow_of_exponent_le · exact (lt_of_le_of_lt (by simp) hx.1).le · linarith [mem_Ici.mp hs] · linarith [hx.1] all_goals apply rpow_pos_of_pos ((Nat.cast_nonneg _).trans_lt hx.1) · rw [intervalIntegral.integral_of_le (by linarith)] refine setIntegral_nonneg measurableSet_Ioc (fun x hx ↦ div_nonneg ?_ (rpow_nonneg ?_ _)) all_goals linarith [hx.1] /-- First version of the limit formula, with a limit over real numbers tending to 1 from above. -/ lemma tendsto_riemannZeta_sub_one_div_nhds_right : Tendsto (fun s : ℝ ↦ riemannZeta s - 1 / (s - 1)) (𝓝[>] 1) (𝓝 γ) := by suffices Tendsto (fun s : ℝ ↦ (∑' n : ℕ, 1 / (n + 1 : ℝ) ^ s) - 1 / (s - 1)) (𝓝[>] 1) (𝓝 γ) by apply ((Complex.continuous_ofReal.tendsto _).comp this).congr' filter_upwards [self_mem_nhdsWithin] with s hs simp only [Function.comp_apply, Complex.ofReal_sub, Complex.ofReal_div, Complex.ofReal_one, sub_left_inj, Complex.ofReal_tsum] rw [zeta_eq_tsum_one_div_nat_add_one_cpow (by simpa using hs)] congr 1 with n rw [Complex.ofReal_cpow (by positivity)] norm_cast suffices aux2 : Tendsto (fun s : ℝ ↦ (∑' n : ℕ, 1 / (n + 1 : ℝ) ^ s) - 1 / (s - 1)) (𝓝[>] 1) (𝓝 (1 - term_tsum 1)) by have := term_tsum_one.tsum_eq rw [← term_tsum, eq_sub_iff_add_eq, ← eq_sub_iff_add_eq'] at this simpa only [this] using aux2 apply Tendsto.congr' · filter_upwards [self_mem_nhdsWithin] with s hs using (zeta_limit_aux1 hs).symm · apply tendsto_const_nhds.sub rw [← one_mul (term_tsum 1)] apply (tendsto_id.mono_left nhdsWithin_le_nhds).mul have := continuousOn_term_tsum.continuousWithinAt left_mem_Ici exact Tendsto.mono_left this (nhdsWithin_mono _ Ioi_subset_Ici_self) /-- The function `ζ s - 1 / (s - 1)` tends to `γ` as `s → 1`. -/ theorem _root_.tendsto_riemannZeta_sub_one_div : Tendsto (fun s : ℂ ↦ riemannZeta s - 1 / (s - 1)) (𝓝[≠] 1) (𝓝 γ) := by -- We use the removable-singularity theorem to show that *some* limit over `𝓝[≠] (1 : ℂ)` exists, -- and then use the previous result to deduce that this limit must be `γ`. let f (s : ℂ) := riemannZeta s - 1 / (s - 1) suffices ∃ C, Tendsto f (𝓝[≠] 1) (𝓝 C) by obtain ⟨C, hC⟩ := this suffices Tendsto (fun s : ℝ ↦ f s) _ _ from (tendsto_nhds_unique this tendsto_riemannZeta_sub_one_div_nhds_right) ▸ hC refine hC.comp (tendsto_nhdsWithin_of_tendsto_nhds_of_eventually_within _ ?_ ?_) · exact (Complex.continuous_ofReal.tendsto 1).mono_left (nhdsWithin_le_nhds ..) · filter_upwards [self_mem_nhdsWithin] with a ha rw [mem_compl_singleton_iff, ← Complex.ofReal_one, Ne, Complex.ofReal_inj] exact ne_of_gt ha refine ⟨_, Complex.tendsto_limUnder_of_differentiable_on_punctured_nhds_of_isLittleO ?_ ?_⟩ · filter_upwards [self_mem_nhdsWithin] with s hs refine (differentiableAt_riemannZeta hs).sub ((differentiableAt_const _).div ?_ ?_) · fun_prop · rwa [mem_compl_singleton_iff, ← sub_ne_zero] at hs · refine Asymptotics.isLittleO_of_tendsto' ?_ ?_ · filter_upwards [self_mem_nhdsWithin] with t ht ht' rw [inv_eq_zero, sub_eq_zero] at ht' tauto · simp_rw [div_eq_mul_inv, inv_inv, sub_mul, (by ring_nf : 𝓝 (0 : ℂ) = 𝓝 ((1 - 1) - f 1 * (1 - 1)))] apply Tendsto.sub · simp_rw [mul_comm (f _), f, mul_sub] apply riemannZeta_residue_one.sub refine Tendsto.congr' ?_ (tendsto_const_nhds.mono_left nhdsWithin_le_nhds) filter_upwards [self_mem_nhdsWithin] with x hx field_simp [sub_ne_zero.mpr <| mem_compl_singleton_iff.mp hx] · exact ((tendsto_id.sub tendsto_const_nhds).mono_left nhdsWithin_le_nhds).const_mul _ lemma _root_.isBigO_riemannZeta_sub_one_div {F : Type*} [Norm F] [One F] [NormOneClass F] : (fun s : ℂ ↦ riemannZeta s - 1 / (s - 1)) =O[𝓝 1] (fun _ ↦ 1 : ℂ → F) := by simpa only [Asymptotics.isBigO_one_nhds_ne_iff] using tendsto_riemannZeta_sub_one_div.isBigO_one (F := F) end continuity section val_at_one open Complex lemma tendsto_Gamma_term_aux : Tendsto (fun s ↦ 1 / (s - 1) - 1 / Gammaℝ s / (s - 1)) (𝓝[≠] 1) (𝓝 (-(γ + Complex.log (4 * ↑π)) / 2)) := by have h := hasDerivAt_Gammaℝ_one rw [hasDerivAt_iff_tendsto_slope, slope_fun_def_field, Gammaℝ_one] at h have := h.div (hasDerivAt_Gammaℝ_one.continuousAt.tendsto.mono_left nhdsWithin_le_nhds) (Gammaℝ_one.trans_ne one_ne_zero) rw [Gammaℝ_one, div_one] at this refine this.congr' ?_ have : {z | 0 < re z} ∈ 𝓝 (1 : ℂ) := by apply (continuous_re.isOpen_preimage _ isOpen_Ioi).mem_nhds simp only [mem_preimage, one_re, mem_Ioi, zero_lt_one] rw [EventuallyEq, eventually_nhdsWithin_iff] filter_upwards [this] with a ha _
rw [Pi.div_apply, ← sub_div, div_right_comm, sub_div' (Gammaℝ_ne_zero_of_re_pos ha), one_mul] lemma tendsto_riemannZeta_sub_one_div_Gammaℝ : Tendsto (fun s ↦ riemannZeta s - 1 / Gammaℝ s / (s - 1)) (𝓝[≠] 1) (𝓝 ((γ - Complex.log (4 * ↑π)) / 2)) := by have := tendsto_riemannZeta_sub_one_div.add tendsto_Gamma_term_aux simp_rw [sub_add_sub_cancel] at this
Mathlib/NumberTheory/Harmonic/ZetaAsymp.lean
375
381
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.RingTheory.Ideal.Operations /-! # Maps on modules and ideals Main definitions include `Ideal.map`, `Ideal.comap`, `RingHom.ker`, `Module.annihilator` and `Submodule.annihilator`. -/ assert_not_exists Basis -- See `RingTheory.Ideal.Basis` Submodule.hasQuotient -- See `RingTheory.Ideal.Quotient.Operations` universe u v w x open Pointwise namespace Ideal section MapAndComap variable {R : Type u} {S : Type v} section Semiring variable {F : Type*} [Semiring R] [Semiring S] variable [FunLike F R S] variable (f : F) variable {I J : Ideal R} {K L : Ideal S} /-- `I.map f` is the span of the image of the ideal `I` under `f`, which may be bigger than the image itself. -/ def map (I : Ideal R) : Ideal S := span (f '' I) /-- `I.comap f` is the preimage of `I` under `f`. -/ def comap [RingHomClass F R S] (I : Ideal S) : Ideal R where carrier := f ⁻¹' I add_mem' {x y} hx hy := by simp only [Set.mem_preimage, SetLike.mem_coe, map_add f] at hx hy ⊢ exact add_mem hx hy zero_mem' := by simp only [Set.mem_preimage, map_zero, SetLike.mem_coe, Submodule.zero_mem] smul_mem' c x hx := by simp only [smul_eq_mul, Set.mem_preimage, map_mul, SetLike.mem_coe] at * exact mul_mem_left I _ hx @[simp] theorem coe_comap [RingHomClass F R S] (I : Ideal S) : (comap f I : Set R) = f ⁻¹' I := rfl lemma comap_coe [RingHomClass F R S] (I : Ideal S) : I.comap (f : R →+* S) = I.comap f := rfl lemma map_coe [RingHomClass F R S] (I : Ideal R) : I.map (f : R →+* S) = I.map f := rfl variable {f} theorem map_mono (h : I ≤ J) : map f I ≤ map f J := span_mono <| Set.image_subset _ h theorem mem_map_of_mem (f : F) {I : Ideal R} {x : R} (h : x ∈ I) : f x ∈ map f I := subset_span ⟨x, h, rfl⟩ theorem apply_coe_mem_map (f : F) (I : Ideal R) (x : I) : f x ∈ I.map f := mem_map_of_mem f x.2 theorem map_le_iff_le_comap [RingHomClass F R S] : map f I ≤ K ↔ I ≤ comap f K := span_le.trans Set.image_subset_iff @[simp] theorem mem_comap [RingHomClass F R S] {x} : x ∈ comap f K ↔ f x ∈ K := Iff.rfl theorem comap_mono [RingHomClass F R S] (h : K ≤ L) : comap f K ≤ comap f L := Set.preimage_mono fun _ hx => h hx variable (f) theorem comap_ne_top [RingHomClass F R S] (hK : K ≠ ⊤) : comap f K ≠ ⊤ := (ne_top_iff_one _).2 <| by rw [mem_comap, map_one]; exact (ne_top_iff_one _).1 hK lemma exists_ideal_comap_le_prime {S} [CommSemiring S] [FunLike F R S] [RingHomClass F R S] {f : F} (P : Ideal R) [P.IsPrime] (I : Ideal S) (le : I.comap f ≤ P) : ∃ Q ≥ I, Q.IsPrime ∧ Q.comap f ≤ P := have ⟨Q, hQ, hIQ, disj⟩ := I.exists_le_prime_disjoint (P.primeCompl.map f) <| Set.disjoint_left.mpr fun _ ↦ by rintro hI ⟨r, hp, rfl⟩; exact hp (le hI) ⟨Q, hIQ, hQ, fun r hp' ↦ of_not_not fun hp ↦ Set.disjoint_left.mp disj hp' ⟨_, hp, rfl⟩⟩ variable {G : Type*} [FunLike G S R] theorem map_le_comap_of_inv_on [RingHomClass G S R] (g : G) (I : Ideal R) (hf : Set.LeftInvOn g f I) : I.map f ≤ I.comap g := by refine Ideal.span_le.2 ?_ rintro x ⟨x, hx, rfl⟩ rw [SetLike.mem_coe, mem_comap, hf hx] exact hx theorem comap_le_map_of_inv_on [RingHomClass F R S] (g : G) (I : Ideal S) (hf : Set.LeftInvOn g f (f ⁻¹' I)) : I.comap f ≤ I.map g := fun x (hx : f x ∈ I) => hf hx ▸ Ideal.mem_map_of_mem g hx /-- The `Ideal` version of `Set.image_subset_preimage_of_inverse`. -/ theorem map_le_comap_of_inverse [RingHomClass G S R] (g : G) (I : Ideal R) (h : Function.LeftInverse g f) : I.map f ≤ I.comap g := map_le_comap_of_inv_on _ _ _ <| h.leftInvOn _ variable [RingHomClass F R S] instance (priority := low) [K.IsTwoSided] : (comap f K).IsTwoSided := ⟨fun b ha ↦ by rw [mem_comap, map_mul]; exact mul_mem_right _ _ ha⟩ /-- The `Ideal` version of `Set.preimage_subset_image_of_inverse`. -/ theorem comap_le_map_of_inverse (g : G) (I : Ideal S) (h : Function.LeftInverse g f) : I.comap f ≤ I.map g := comap_le_map_of_inv_on _ _ _ <| h.leftInvOn _ instance IsPrime.comap [hK : K.IsPrime] : (comap f K).IsPrime := ⟨comap_ne_top _ hK.1, fun {x y} => by simp only [mem_comap, map_mul]; apply hK.2⟩ variable (I J K L) theorem map_top : map f ⊤ = ⊤ := (eq_top_iff_one _).2 <| subset_span ⟨1, trivial, map_one f⟩ theorem gc_map_comap : GaloisConnection (Ideal.map f) (Ideal.comap f) := fun _ _ => Ideal.map_le_iff_le_comap @[simp] theorem comap_id : I.comap (RingHom.id R) = I := Ideal.ext fun _ => Iff.rfl @[simp] lemma comap_idₐ {R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S] (I : Ideal S) : Ideal.comap (AlgHom.id R S) I = I := I.comap_id @[simp] theorem map_id : I.map (RingHom.id R) = I := (gc_map_comap (RingHom.id R)).l_unique GaloisConnection.id comap_id @[simp] lemma map_idₐ {R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S] (I : Ideal S) : Ideal.map (AlgHom.id R S) I = I := I.map_id theorem comap_comap {T : Type*} [Semiring T] {I : Ideal T} (f : R →+* S) (g : S →+* T) : (I.comap g).comap f = I.comap (g.comp f) := rfl lemma comap_comapₐ {R A B C : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] {I : Ideal C} (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (I.comap g).comap f = I.comap (g.comp f) := I.comap_comap f.toRingHom g.toRingHom theorem map_map {T : Type*} [Semiring T] {I : Ideal R} (f : R →+* S) (g : S →+* T) : (I.map f).map g = I.map (g.comp f) := ((gc_map_comap f).compose (gc_map_comap g)).l_unique (gc_map_comap (g.comp f)) fun _ => comap_comap _ _ lemma map_mapₐ {R A B C : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] {I : Ideal A} (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (I.map f).map g = I.map (g.comp f) := I.map_map f.toRingHom g.toRingHom theorem map_span (f : F) (s : Set R) : map f (span s) = span (f '' s) := by refine (Submodule.span_eq_of_le _ ?_ ?_).symm · rintro _ ⟨x, hx, rfl⟩; exact mem_map_of_mem f (subset_span hx) · rw [map_le_iff_le_comap, span_le, coe_comap, ← Set.image_subset_iff] exact subset_span variable {f I J K L} theorem map_le_of_le_comap : I ≤ K.comap f → I.map f ≤ K := (gc_map_comap f).l_le theorem le_comap_of_map_le : I.map f ≤ K → I ≤ K.comap f := (gc_map_comap f).le_u theorem le_comap_map : I ≤ (I.map f).comap f := (gc_map_comap f).le_u_l _ theorem map_comap_le : (K.comap f).map f ≤ K := (gc_map_comap f).l_u_le _ @[simp] theorem comap_top : (⊤ : Ideal S).comap f = ⊤ := (gc_map_comap f).u_top @[simp] theorem comap_eq_top_iff {I : Ideal S} : I.comap f = ⊤ ↔ I = ⊤ := ⟨fun h => I.eq_top_iff_one.mpr (map_one f ▸ mem_comap.mp ((I.comap f).eq_top_iff_one.mp h)), fun h => by rw [h, comap_top]⟩ @[simp] theorem map_bot : (⊥ : Ideal R).map f = ⊥ := (gc_map_comap f).l_bot theorem ne_bot_of_map_ne_bot (hI : map f I ≠ ⊥) : I ≠ ⊥ := fun h => hI (Eq.mpr (congrArg (fun I ↦ map f I = ⊥) h) map_bot) variable (f I J K L) @[simp] theorem map_comap_map : ((I.map f).comap f).map f = I.map f := (gc_map_comap f).l_u_l_eq_l I @[simp] theorem comap_map_comap : ((K.comap f).map f).comap f = K.comap f := (gc_map_comap f).u_l_u_eq_u K theorem map_sup : (I ⊔ J).map f = I.map f ⊔ J.map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_sup theorem comap_inf : comap f (K ⊓ L) = comap f K ⊓ comap f L := rfl variable {ι : Sort*} theorem map_iSup (K : ι → Ideal R) : (iSup K).map f = ⨆ i, (K i).map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_iSup theorem comap_iInf (K : ι → Ideal S) : (iInf K).comap f = ⨅ i, (K i).comap f := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_iInf theorem map_sSup (s : Set (Ideal R)) : (sSup s).map f = ⨆ I ∈ s, (I : Ideal R).map f := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_sSup theorem comap_sInf (s : Set (Ideal S)) : (sInf s).comap f = ⨅ I ∈ s, (I : Ideal S).comap f := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_sInf theorem comap_sInf' (s : Set (Ideal S)) : (sInf s).comap f = ⨅ I ∈ comap f '' s, I := _root_.trans (comap_sInf f s) (by rw [iInf_image]) /-- Variant of `Ideal.IsPrime.comap` where ideal is explicit rather than implicit. -/ theorem comap_isPrime [H : IsPrime K] : IsPrime (comap f K) := H.comap f variable {I J K L} theorem map_inf_le : map f (I ⊓ J) ≤ map f I ⊓ map f J := (gc_map_comap f : GaloisConnection (map f) (comap f)).monotone_l.map_inf_le _ _ theorem le_comap_sup : comap f K ⊔ comap f L ≤ comap f (K ⊔ L) := (gc_map_comap f : GaloisConnection (map f) (comap f)).monotone_u.le_map_sup _ _ -- TODO: Should these be simp lemmas? theorem _root_.element_smul_restrictScalars {R S M} [CommSemiring R] [CommSemiring S] [Algebra R S] [AddCommMonoid M] [Module R M] [Module S M] [IsScalarTower R S M] (r : R) (N : Submodule S M) : (algebraMap R S r • N).restrictScalars R = r • N.restrictScalars R := SetLike.coe_injective (congrArg (· '' _) (funext (algebraMap_smul S r))) theorem smul_restrictScalars {R S M} [CommSemiring R] [CommSemiring S] [Algebra R S] [AddCommMonoid M] [Module R M] [Module S M] [IsScalarTower R S M] (I : Ideal R) (N : Submodule S M) : (I.map (algebraMap R S) • N).restrictScalars R = I • N.restrictScalars R := by simp_rw [map, Submodule.span_smul_eq, ← Submodule.coe_set_smul, Submodule.set_smul_eq_iSup, ← element_smul_restrictScalars, iSup_image] exact map_iSup₂ (Submodule.restrictScalarsLatticeHom R S M) _ @[simp] theorem smul_top_eq_map {R S : Type*} [CommSemiring R] [CommSemiring S] [Algebra R S] (I : Ideal R) : I • (⊤ : Submodule R S) = (I.map (algebraMap R S)).restrictScalars R := Eq.trans (smul_restrictScalars I (⊤ : Ideal S)).symm <| congrArg _ <| Eq.trans (Ideal.smul_eq_mul _ _) (Ideal.mul_top _) @[simp] theorem coe_restrictScalars {R S : Type*} [Semiring R] [Semiring S] [Module R S] [IsScalarTower R S S] (I : Ideal S) : (I.restrictScalars R : Set S) = ↑I := rfl /-- The smallest `S`-submodule that contains all `x ∈ I * y ∈ J` is also the smallest `R`-submodule that does so. -/ @[simp] theorem restrictScalars_mul {R S : Type*} [Semiring R] [Semiring S] [Module R S] [IsScalarTower R S S] (I J : Ideal S) : (I * J).restrictScalars R = I.restrictScalars R * J.restrictScalars R := rfl section Surjective section variable (hf : Function.Surjective f) include hf open Function theorem map_comap_of_surjective (I : Ideal S) : map f (comap f I) = I := le_antisymm (map_le_iff_le_comap.2 le_rfl) fun s hsi => let ⟨r, hfrs⟩ := hf s hfrs ▸ (mem_map_of_mem f <| show f r ∈ I from hfrs.symm ▸ hsi) /-- `map` and `comap` are adjoint, and the composition `map f ∘ comap f` is the identity -/ def giMapComap : GaloisInsertion (map f) (comap f) := GaloisInsertion.monotoneIntro (gc_map_comap f).monotone_u (gc_map_comap f).monotone_l (fun _ => le_comap_map) (map_comap_of_surjective _ hf) theorem map_surjective_of_surjective : Surjective (map f) := (giMapComap f hf).l_surjective theorem comap_injective_of_surjective : Injective (comap f) := (giMapComap f hf).u_injective theorem map_sup_comap_of_surjective (I J : Ideal S) : (I.comap f ⊔ J.comap f).map f = I ⊔ J := (giMapComap f hf).l_sup_u _ _ theorem map_iSup_comap_of_surjective (K : ι → Ideal S) : (⨆ i, (K i).comap f).map f = iSup K := (giMapComap f hf).l_iSup_u _ theorem map_inf_comap_of_surjective (I J : Ideal S) : (I.comap f ⊓ J.comap f).map f = I ⊓ J := (giMapComap f hf).l_inf_u _ _ theorem map_iInf_comap_of_surjective (K : ι → Ideal S) : (⨅ i, (K i).comap f).map f = iInf K := (giMapComap f hf).l_iInf_u _ theorem mem_image_of_mem_map_of_surjective {I : Ideal R} {y} (H : y ∈ map f I) : y ∈ f '' I := Submodule.span_induction (hx := H) (fun _ => id) ⟨0, I.zero_mem, map_zero f⟩ (fun _ _ _ _ ⟨x1, hx1i, hxy1⟩ ⟨x2, hx2i, hxy2⟩ => ⟨x1 + x2, I.add_mem hx1i hx2i, hxy1 ▸ hxy2 ▸ map_add f _ _⟩) fun c _ _ ⟨x, hxi, hxy⟩ => let ⟨d, hdc⟩ := hf c ⟨d * x, I.mul_mem_left _ hxi, hdc ▸ hxy ▸ map_mul f _ _⟩ theorem mem_map_iff_of_surjective {I : Ideal R} {y} : y ∈ map f I ↔ ∃ x, x ∈ I ∧ f x = y := ⟨fun h => (Set.mem_image _ _ _).2 (mem_image_of_mem_map_of_surjective f hf h), fun ⟨_, hx⟩ => hx.right ▸ mem_map_of_mem f hx.left⟩ theorem le_map_of_comap_le_of_surjective : comap f K ≤ I → K ≤ map f I := fun h => map_comap_of_surjective f hf K ▸ map_mono h end theorem map_comap_eq_self_of_equiv {E : Type*} [EquivLike E R S] [RingEquivClass E R S] (e : E) (I : Ideal S) : map e (comap e I) = I := I.map_comap_of_surjective e (EquivLike.surjective e) theorem map_eq_submodule_map (f : R →+* S) [h : RingHomSurjective f] (I : Ideal R) : I.map f = Submodule.map f.toSemilinearMap I := Submodule.ext fun _ => mem_map_iff_of_surjective f h.1 instance (priority := low) (f : R →+* S) [RingHomSurjective f] (I : Ideal R) [I.IsTwoSided] : (I.map f).IsTwoSided where mul_mem_of_left b ha := by rw [map_eq_submodule_map] at ha ⊢ obtain ⟨a, ha, rfl⟩ := ha obtain ⟨b, rfl⟩ := f.surjective b rw [RingHom.coe_toSemilinearMap, ← map_mul] exact ⟨_, I.mul_mem_right _ ha, rfl⟩ open Function in theorem IsMaximal.comap_piEvalRingHom {ι : Type*} {R : ι → Type*} [∀ i, Semiring (R i)] {i : ι} {I : Ideal (R i)} (h : I.IsMaximal) : (I.comap <| Pi.evalRingHom R i).IsMaximal := by refine isMaximal_iff.mpr ⟨I.ne_top_iff_one.mp h.ne_top, fun J x le hxI hxJ ↦ ?_⟩ have ⟨r, y, hy, eq⟩ := h.exists_inv hxI classical convert J.add_mem (J.mul_mem_left (update 0 i r) hxJ) (b := update 1 i y) (le <| by apply update_self i y 1 ▸ hy) ext j obtain rfl | ne := eq_or_ne j i · simpa [eq_comm] using eq · simp [update_of_ne ne] theorem comap_le_comap_iff_of_surjective (hf : Function.Surjective f) (I J : Ideal S) : comap f I ≤ comap f J ↔ I ≤ J := ⟨fun h => (map_comap_of_surjective f hf I).symm.le.trans (map_le_of_le_comap h), fun h => le_comap_of_map_le ((map_comap_of_surjective f hf I).le.trans h)⟩ /-- The map on ideals induced by a surjective map preserves inclusion. -/ def orderEmbeddingOfSurjective (hf : Function.Surjective f) : Ideal S ↪o Ideal R where toFun := comap f inj' _ _ eq := SetLike.ext' (Set.preimage_injective.mpr hf <| SetLike.ext'_iff.mp eq) map_rel_iff' := comap_le_comap_iff_of_surjective _ hf .. theorem map_eq_top_or_isMaximal_of_surjective (hf : Function.Surjective f) {I : Ideal R} (H : IsMaximal I) : map f I = ⊤ ∨ IsMaximal (map f I) := or_iff_not_imp_left.2 fun ne_top ↦ ⟨⟨ne_top, fun _J hJ ↦ comap_injective_of_surjective f hf <| H.1.2 _ (le_comap_map.trans_lt <| (orderEmbeddingOfSurjective f hf).strictMono hJ)⟩⟩ end Surjective section Injective theorem comap_bot_le_of_injective (hf : Function.Injective f) : comap f ⊥ ≤ I := by refine le_trans (fun x hx => ?_) bot_le rw [mem_comap, Submodule.mem_bot, ← map_zero f] at hx exact Eq.symm (hf hx) ▸ Submodule.zero_mem ⊥ theorem comap_bot_of_injective (hf : Function.Injective f) : Ideal.comap f ⊥ = ⊥ := le_bot_iff.mp (Ideal.comap_bot_le_of_injective f hf) end Injective /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `map f.symm (map f I) = I`. -/ @[simp] theorem map_of_equiv {I : Ideal R} (f : R ≃+* S) : (I.map (f : R →+* S)).map (f.symm : S →+* R) = I := by rw [← RingEquiv.toRingHom_eq_coe, ← RingEquiv.toRingHom_eq_coe, map_map, RingEquiv.toRingHom_eq_coe, RingEquiv.toRingHom_eq_coe, RingEquiv.symm_comp, map_id] /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `comap f (comap f.symm I) = I`. -/ @[simp] theorem comap_of_equiv {I : Ideal R} (f : R ≃+* S) : (I.comap (f.symm : S →+* R)).comap (f : R →+* S) = I := by rw [← RingEquiv.toRingHom_eq_coe, ← RingEquiv.toRingHom_eq_coe, comap_comap, RingEquiv.toRingHom_eq_coe, RingEquiv.toRingHom_eq_coe, RingEquiv.symm_comp, comap_id] /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `map f I = comap f.symm I`. -/ theorem map_comap_of_equiv {I : Ideal R} (f : R ≃+* S) : I.map (f : R →+* S) = I.comap f.symm := le_antisymm (Ideal.map_le_comap_of_inverse _ _ _ (Equiv.left_inv' _)) (Ideal.comap_le_map_of_inverse _ _ _ (Equiv.right_inv' _)) /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `comap f.symm I = map f I`. -/ @[simp] theorem comap_symm {I : Ideal R} (f : R ≃+* S) : I.comap f.symm = I.map f := (map_comap_of_equiv f).symm /-- If `f : R ≃+* S` is a ring isomorphism and `I : Ideal R`, then `map f.symm I = comap f I`. -/ @[simp] theorem map_symm {I : Ideal S} (f : R ≃+* S) : I.map f.symm = I.comap f := map_comap_of_equiv (RingEquiv.symm f) @[simp] theorem symm_apply_mem_of_equiv_iff {I : Ideal R} {f : R ≃+* S} {y : S} : f.symm y ∈ I ↔ y ∈ I.map f := by rw [← comap_symm, mem_comap] @[simp] theorem apply_mem_of_equiv_iff {I : Ideal R} {f : R ≃+* S} {x : R} : f x ∈ I.map f ↔ x ∈ I := by rw [← comap_symm, Ideal.mem_comap, f.symm_apply_apply] theorem mem_map_of_equiv {E : Type*} [EquivLike E R S] [RingEquivClass E R S] (e : E) {I : Ideal R} (y : S) : y ∈ map e I ↔ ∃ x ∈ I, e x = y := by constructor · intro h
simp_rw [show map e I = _ from map_comap_of_equiv (e : R ≃+* S)] at h exact ⟨(e : R ≃+* S).symm y, h, (e : R ≃+* S).apply_symm_apply y⟩ · rintro ⟨x, hx, rfl⟩ exact mem_map_of_mem e hx section Bijective variable (hf : Function.Bijective f) {I : Ideal R} {K : Ideal S}
Mathlib/RingTheory/Ideal/Maps.lean
445
452
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import Mathlib.Data.Set.Constructions import Mathlib.Order.Filter.AtTopBot.CountablyGenerated import Mathlib.Topology.Constructions import Mathlib.Topology.ContinuousOn /-! # Bases of topologies. Countability axioms. A topological basis on a topological space `t` is a collection of sets, such that all open sets can be generated as unions of these sets, without the need to take finite intersections of them. This file introduces a framework for dealing with these collections, and also what more we can say under certain countability conditions on bases, which are referred to as first- and second-countable. We also briefly cover the theory of separable spaces, which are those with a countable, dense subset. If a space is second-countable, and also has a countably generated uniformity filter (for example, if `t` is a metric space), it will automatically be separable (and indeed, these conditions are equivalent in this case). ## Main definitions * `TopologicalSpace.IsTopologicalBasis s`: The topological space `t` has basis `s`. * `TopologicalSpace.SeparableSpace α`: The topological space `t` has a countable, dense subset. * `TopologicalSpace.IsSeparable s`: The set `s` is contained in the closure of a countable set. * `FirstCountableTopology α`: A topology in which `𝓝 x` is countably generated for every `x`. * `SecondCountableTopology α`: A topology which has a topological basis which is countable. ## Main results * `TopologicalSpace.FirstCountableTopology.tendsto_subseq`: In a first-countable space, cluster points are limits of subsequences. * `TopologicalSpace.SecondCountableTopology.isOpen_iUnion_countable`: In a second-countable space, the union of arbitrarily-many open sets is equal to a sub-union of only countably many of these sets. * `TopologicalSpace.SecondCountableTopology.countable_cover_nhds`: Consider `f : α → Set α` with the property that `f x ∈ 𝓝 x` for all `x`. Then there is some countable set `s` whose image covers the space. ## Implementation Notes For our applications we are interested that there exists a countable basis, but we do not need the concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins. ## TODO More fine grained instances for `FirstCountableTopology`, `TopologicalSpace.SeparableSpace`, and more. -/ open Set Filter Function Topology noncomputable section namespace TopologicalSpace universe u variable {α : Type u} {β : Type*} [t : TopologicalSpace α] {B : Set (Set α)} {s : Set α} /-- A topological basis is one that satisfies the necessary conditions so that it suffices to take unions of the basis sets to get a topology (without taking finite intersections as well). -/ structure IsTopologicalBasis (s : Set (Set α)) : Prop where /-- For every point `x`, the set of `t ∈ s` such that `x ∈ t` is directed downwards. -/ exists_subset_inter : ∀ t₁ ∈ s, ∀ t₂ ∈ s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃ ∈ s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂ /-- The sets from `s` cover the whole space. -/ sUnion_eq : ⋃₀ s = univ /-- The topology is generated by sets from `s`. -/ eq_generateFrom : t = generateFrom s /-- If a family of sets `s` generates the topology, then intersections of finite subcollections of `s` form a topological basis. -/ theorem isTopologicalBasis_of_subbasis {s : Set (Set α)} (hs : t = generateFrom s) : IsTopologicalBasis ((fun f => ⋂₀ f) '' { f : Set (Set α) | f.Finite ∧ f ⊆ s }) := by subst t; letI := generateFrom s refine ⟨?_, ?_, le_antisymm (le_generateFrom ?_) <| generateFrom_anti fun t ht => ?_⟩ · rintro _ ⟨t₁, ⟨hft₁, ht₁b⟩, rfl⟩ _ ⟨t₂, ⟨hft₂, ht₂b⟩, rfl⟩ x h exact ⟨_, ⟨_, ⟨hft₁.union hft₂, union_subset ht₁b ht₂b⟩, sInter_union t₁ t₂⟩, h, Subset.rfl⟩ · rw [sUnion_image, iUnion₂_eq_univ_iff] exact fun x => ⟨∅, ⟨finite_empty, empty_subset _⟩, sInter_empty.substr <| mem_univ x⟩ · rintro _ ⟨t, ⟨hft, htb⟩, rfl⟩ exact hft.isOpen_sInter fun s hs ↦ GenerateOpen.basic _ <| htb hs · rw [← sInter_singleton t] exact ⟨{t}, ⟨finite_singleton t, singleton_subset_iff.2 ht⟩, rfl⟩ theorem isTopologicalBasis_of_subbasis_of_finiteInter {s : Set (Set α)} (hsg : t = generateFrom s) (hsi : FiniteInter s) : IsTopologicalBasis s := by convert isTopologicalBasis_of_subbasis hsg refine le_antisymm (fun t ht ↦ ⟨{t}, by simpa using ht⟩) ?_ rintro _ ⟨g, ⟨hg, hgs⟩, rfl⟩ lift g to Finset (Set α) using hg exact hsi.finiteInter_mem g hgs theorem isTopologicalBasis_of_subbasis_of_inter {r : Set (Set α)} (hsg : t = generateFrom r) (hsi : ∀ ⦃s⦄, s ∈ r → ∀ ⦃t⦄, t ∈ r → s ∩ t ∈ r) : IsTopologicalBasis (insert univ r) := isTopologicalBasis_of_subbasis_of_finiteInter (by simpa using hsg) (FiniteInter.mk₂ hsi) theorem IsTopologicalBasis.of_hasBasis_nhds {s : Set (Set α)} (h_nhds : ∀ a, (𝓝 a).HasBasis (fun t ↦ t ∈ s ∧ a ∈ t) id) : IsTopologicalBasis s where exists_subset_inter t₁ ht₁ t₂ ht₂ x hx := by simpa only [and_assoc, (h_nhds x).mem_iff] using (inter_mem ((h_nhds _).mem_of_mem ⟨ht₁, hx.1⟩) ((h_nhds _).mem_of_mem ⟨ht₂, hx.2⟩)) sUnion_eq := sUnion_eq_univ_iff.2 fun x ↦ (h_nhds x).ex_mem eq_generateFrom := ext_nhds fun x ↦ by simpa only [nhds_generateFrom, and_comm] using (h_nhds x).eq_biInf /-- If a family of open sets `s` is such that every open neighbourhood contains some member of `s`, then `s` is a topological basis. -/ theorem isTopologicalBasis_of_isOpen_of_nhds {s : Set (Set α)} (h_open : ∀ u ∈ s, IsOpen u) (h_nhds : ∀ (a : α) (u : Set α), a ∈ u → IsOpen u → ∃ v ∈ s, a ∈ v ∧ v ⊆ u) : IsTopologicalBasis s := .of_hasBasis_nhds <| fun a ↦ (nhds_basis_opens a).to_hasBasis' (by simpa [and_assoc] using h_nhds a) fun _ ⟨hts, hat⟩ ↦ (h_open _ hts).mem_nhds hat /-- A set `s` is in the neighbourhood of `a` iff there is some basis set `t`, which contains `a` and is itself contained in `s`. -/ theorem IsTopologicalBasis.mem_nhds_iff {a : α} {s : Set α} {b : Set (Set α)} (hb : IsTopologicalBasis b) : s ∈ 𝓝 a ↔ ∃ t ∈ b, a ∈ t ∧ t ⊆ s := by change s ∈ (𝓝 a).sets ↔ ∃ t ∈ b, a ∈ t ∧ t ⊆ s rw [hb.eq_generateFrom, nhds_generateFrom, biInf_sets_eq] · simp [and_assoc, and_left_comm] · rintro s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩ let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ ⟨hs₁, ht₁⟩ exact ⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (hu₃.trans inter_subset_left), le_principal_iff.2 (hu₃.trans inter_subset_right)⟩ · rcases eq_univ_iff_forall.1 hb.sUnion_eq a with ⟨i, h1, h2⟩ exact ⟨i, h2, h1⟩ theorem IsTopologicalBasis.isOpen_iff {s : Set α} {b : Set (Set α)} (hb : IsTopologicalBasis b) : IsOpen s ↔ ∀ a ∈ s, ∃ t ∈ b, a ∈ t ∧ t ⊆ s := by simp [isOpen_iff_mem_nhds, hb.mem_nhds_iff] theorem IsTopologicalBasis.of_isOpen_of_subset {s s' : Set (Set α)} (h_open : ∀ u ∈ s', IsOpen u) (hs : IsTopologicalBasis s) (hss' : s ⊆ s') : IsTopologicalBasis s' := isTopologicalBasis_of_isOpen_of_nhds h_open fun a _ ha u_open ↦ have ⟨t, hts, ht⟩ := hs.isOpen_iff.mp u_open a ha; ⟨t, hss' hts, ht⟩ theorem IsTopologicalBasis.nhds_hasBasis {b : Set (Set α)} (hb : IsTopologicalBasis b) {a : α} : (𝓝 a).HasBasis (fun t : Set α => t ∈ b ∧ a ∈ t) fun t => t := ⟨fun s => hb.mem_nhds_iff.trans <| by simp only [and_assoc]⟩ protected theorem IsTopologicalBasis.isOpen {s : Set α} {b : Set (Set α)} (hb : IsTopologicalBasis b) (hs : s ∈ b) : IsOpen s := by rw [hb.eq_generateFrom] exact .basic s hs theorem IsTopologicalBasis.insert_empty {s : Set (Set α)} (h : IsTopologicalBasis s) : IsTopologicalBasis (insert ∅ s) := h.of_isOpen_of_subset (by rintro _ (rfl | hu); exacts [isOpen_empty, h.isOpen hu]) (subset_insert ..) theorem IsTopologicalBasis.diff_empty {s : Set (Set α)} (h : IsTopologicalBasis s) : IsTopologicalBasis (s \ {∅}) := isTopologicalBasis_of_isOpen_of_nhds (fun _ hu ↦ h.isOpen hu.1) fun a _ ha hu ↦ have ⟨t, hts, ht⟩ := h.isOpen_iff.mp hu a ha ⟨t, ⟨hts, ne_of_mem_of_not_mem' ht.1 <| not_mem_empty _⟩, ht⟩ protected theorem IsTopologicalBasis.mem_nhds {a : α} {s : Set α} {b : Set (Set α)} (hb : IsTopologicalBasis b) (hs : s ∈ b) (ha : a ∈ s) : s ∈ 𝓝 a := (hb.isOpen hs).mem_nhds ha theorem IsTopologicalBasis.exists_subset_of_mem_open {b : Set (Set α)} (hb : IsTopologicalBasis b) {a : α} {u : Set α} (au : a ∈ u) (ou : IsOpen u) : ∃ v ∈ b, a ∈ v ∧ v ⊆ u := hb.mem_nhds_iff.1 <| IsOpen.mem_nhds ou au /-- Any open set is the union of the basis sets contained in it. -/ theorem IsTopologicalBasis.open_eq_sUnion' {B : Set (Set α)} (hB : IsTopologicalBasis B) {u : Set α} (ou : IsOpen u) : u = ⋃₀ { s ∈ B | s ⊆ u } := ext fun _a => ⟨fun ha => let ⟨b, hb, ab, bu⟩ := hB.exists_subset_of_mem_open ha ou ⟨b, ⟨hb, bu⟩, ab⟩, fun ⟨_b, ⟨_, bu⟩, ab⟩ => bu ab⟩ theorem IsTopologicalBasis.open_eq_sUnion {B : Set (Set α)} (hB : IsTopologicalBasis B) {u : Set α} (ou : IsOpen u) : ∃ S ⊆ B, u = ⋃₀ S := ⟨{ s ∈ B | s ⊆ u }, fun _ h => h.1, hB.open_eq_sUnion' ou⟩ theorem IsTopologicalBasis.open_iff_eq_sUnion {B : Set (Set α)} (hB : IsTopologicalBasis B) {u : Set α} : IsOpen u ↔ ∃ S ⊆ B, u = ⋃₀ S := ⟨hB.open_eq_sUnion, fun ⟨_S, hSB, hu⟩ => hu.symm ▸ isOpen_sUnion fun _s hs => hB.isOpen (hSB hs)⟩ theorem IsTopologicalBasis.open_eq_iUnion {B : Set (Set α)} (hB : IsTopologicalBasis B) {u : Set α} (ou : IsOpen u) : ∃ (β : Type u) (f : β → Set α), (u = ⋃ i, f i) ∧ ∀ i, f i ∈ B := ⟨↥({ s ∈ B | s ⊆ u }), (↑), by rw [← sUnion_eq_iUnion] apply hB.open_eq_sUnion' ou, fun s => And.left s.2⟩ lemma IsTopologicalBasis.subset_of_forall_subset {t : Set α} (hB : IsTopologicalBasis B) (hs : IsOpen s) (h : ∀ U ∈ B, U ⊆ s → U ⊆ t) : s ⊆ t := by rw [hB.open_eq_sUnion' hs]; simpa [sUnion_subset_iff] lemma IsTopologicalBasis.eq_of_forall_subset_iff {t : Set α} (hB : IsTopologicalBasis B) (hs : IsOpen s) (ht : IsOpen t) (h : ∀ U ∈ B, U ⊆ s ↔ U ⊆ t) : s = t := by rw [hB.open_eq_sUnion' hs, hB.open_eq_sUnion' ht] exact congr_arg _ (Set.ext fun U ↦ and_congr_right <| h _) /-- A point `a` is in the closure of `s` iff all basis sets containing `a` intersect `s`. -/ theorem IsTopologicalBasis.mem_closure_iff {b : Set (Set α)} (hb : IsTopologicalBasis b) {s : Set α} {a : α} : a ∈ closure s ↔ ∀ o ∈ b, a ∈ o → (o ∩ s).Nonempty := (mem_closure_iff_nhds_basis' hb.nhds_hasBasis).trans <| by simp only [and_imp] /-- A set is dense iff it has non-trivial intersection with all basis sets. -/ theorem IsTopologicalBasis.dense_iff {b : Set (Set α)} (hb : IsTopologicalBasis b) {s : Set α} : Dense s ↔ ∀ o ∈ b, Set.Nonempty o → (o ∩ s).Nonempty := by simp only [Dense, hb.mem_closure_iff] exact ⟨fun h o hb ⟨a, ha⟩ => h a o hb ha, fun h a o hb ha => h o hb ⟨a, ha⟩⟩ theorem IsTopologicalBasis.isOpenMap_iff {β} [TopologicalSpace β] {B : Set (Set α)} (hB : IsTopologicalBasis B) {f : α → β} : IsOpenMap f ↔ ∀ s ∈ B, IsOpen (f '' s) := by refine ⟨fun H o ho => H _ (hB.isOpen ho), fun hf o ho => ?_⟩ rw [hB.open_eq_sUnion' ho, sUnion_eq_iUnion, image_iUnion] exact isOpen_iUnion fun s => hf s s.2.1 theorem IsTopologicalBasis.exists_nonempty_subset {B : Set (Set α)} (hb : IsTopologicalBasis B) {u : Set α} (hu : u.Nonempty) (ou : IsOpen u) : ∃ v ∈ B, Set.Nonempty v ∧ v ⊆ u := let ⟨x, hx⟩ := hu let ⟨v, vB, xv, vu⟩ := hb.exists_subset_of_mem_open hx ou ⟨v, vB, ⟨x, xv⟩, vu⟩ theorem isTopologicalBasis_opens : IsTopologicalBasis { U : Set α | IsOpen U } := isTopologicalBasis_of_isOpen_of_nhds (by tauto) (by tauto) protected lemma IsTopologicalBasis.isInducing {β} [TopologicalSpace β] {f : α → β} {T : Set (Set β)} (hf : IsInducing f) (h : IsTopologicalBasis T) : IsTopologicalBasis ((preimage f) '' T) := .of_hasBasis_nhds fun a ↦ by convert (hf.basis_nhds (h.nhds_hasBasis (a := f a))).to_image_id with s aesop @[deprecated (since := "2024-10-28")] alias IsTopologicalBasis.inducing := IsTopologicalBasis.isInducing protected theorem IsTopologicalBasis.induced {α} [s : TopologicalSpace β] (f : α → β) {T : Set (Set β)} (h : IsTopologicalBasis T) : IsTopologicalBasis (t := induced f s) ((preimage f) '' T) := h.isInducing (t := induced f s) (.induced f) protected theorem IsTopologicalBasis.inf {t₁ t₂ : TopologicalSpace β} {B₁ B₂ : Set (Set β)} (h₁ : IsTopologicalBasis (t := t₁) B₁) (h₂ : IsTopologicalBasis (t := t₂) B₂) : IsTopologicalBasis (t := t₁ ⊓ t₂) (image2 (· ∩ ·) B₁ B₂) := by refine .of_hasBasis_nhds (t := ?_) fun a ↦ ?_ rw [nhds_inf (t₁ := t₁)] convert ((h₁.nhds_hasBasis (t := t₁)).inf (h₂.nhds_hasBasis (t := t₂))).to_image_id aesop theorem IsTopologicalBasis.inf_induced {γ} [s : TopologicalSpace β] {B₁ : Set (Set α)} {B₂ : Set (Set β)} (h₁ : IsTopologicalBasis B₁) (h₂ : IsTopologicalBasis B₂) (f₁ : γ → α) (f₂ : γ → β) : IsTopologicalBasis (t := induced f₁ t ⊓ induced f₂ s) (image2 (f₁ ⁻¹' · ∩ f₂ ⁻¹' ·) B₁ B₂) := by simpa only [image2_image_left, image2_image_right] using (h₁.induced f₁).inf (h₂.induced f₂) protected theorem IsTopologicalBasis.prod {β} [TopologicalSpace β] {B₁ : Set (Set α)} {B₂ : Set (Set β)} (h₁ : IsTopologicalBasis B₁) (h₂ : IsTopologicalBasis B₂) : IsTopologicalBasis (image2 (· ×ˢ ·) B₁ B₂) :=
h₁.inf_induced h₂ Prod.fst Prod.snd theorem isTopologicalBasis_of_cover {ι} {U : ι → Set α} (Uo : ∀ i, IsOpen (U i)) (Uc : ⋃ i, U i = univ) {b : ∀ i, Set (Set (U i))} (hb : ∀ i, IsTopologicalBasis (b i)) : IsTopologicalBasis (⋃ i : ι, image ((↑) : U i → α) '' b i) := by refine isTopologicalBasis_of_isOpen_of_nhds (fun u hu => ?_) ?_ · simp only [mem_iUnion, mem_image] at hu
Mathlib/Topology/Bases.lean
260
266
/- 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, Johan Commelin -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Div import Mathlib.RingTheory.Coprime.Basic /-! # Theory of univariate polynomials We prove basic results about univariate polynomials. -/ assert_not_exists Ideal.map noncomputable section open Polynomial open Finset namespace Polynomial universe u v w z variable {R : Type u} {S : Type v} {T : Type w} {a b : R} {n : ℕ} section CommRing variable [CommRing R] {p q : R[X]} section variable [Semiring S] theorem natDegree_pos_of_aeval_root [Algebra R S] {p : R[X]} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ x : R, algebraMap R S x = 0 → x = 0) : 0 < p.natDegree := natDegree_pos_of_eval₂_root hp (algebraMap R S) hz inj theorem degree_pos_of_aeval_root [Algebra R S] {p : R[X]} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ x : R, algebraMap R S x = 0 → x = 0) : 0 < p.degree := natDegree_pos_iff_degree_pos.mp (natDegree_pos_of_aeval_root hp hz inj) end theorem smul_modByMonic (c : R) (p : R[X]) : c • p %ₘ q = c • (p %ₘ q) := by by_cases hq : q.Monic · rcases subsingleton_or_nontrivial R with hR | hR · simp only [eq_iff_true_of_subsingleton] · exact (div_modByMonic_unique (c • (p /ₘ q)) (c • (p %ₘ q)) hq ⟨by rw [mul_smul_comm, ← smul_add, modByMonic_add_div p hq], (degree_smul_le _ _).trans_lt (degree_modByMonic_lt _ hq)⟩).2 · simp_rw [modByMonic_eq_of_not_monic _ hq] /-- `_ %ₘ q` as an `R`-linear map. -/ @[simps] def modByMonicHom (q : R[X]) : R[X] →ₗ[R] R[X] where toFun p := p %ₘ q map_add' := add_modByMonic map_smul' := smul_modByMonic theorem mem_ker_modByMonic (hq : q.Monic) {p : R[X]} : p ∈ LinearMap.ker (modByMonicHom q) ↔ q ∣ p := LinearMap.mem_ker.trans (modByMonic_eq_zero_iff_dvd hq) section variable [Ring S] theorem aeval_modByMonic_eq_self_of_root [Algebra R S] {p q : R[X]} (hq : q.Monic) {x : S} (hx : aeval x q = 0) : aeval x (p %ₘ q) = aeval x p := by --`eval₂_modByMonic_eq_self_of_root` doesn't work here as it needs commutativity rw [modByMonic_eq_sub_mul_div p hq, map_sub, map_mul, hx, zero_mul, sub_zero] end end CommRing section NoZeroDivisors variable [Semiring R] [NoZeroDivisors R] {p q : R[X]} theorem trailingDegree_mul : (p * q).trailingDegree = p.trailingDegree + q.trailingDegree := by by_cases hp : p = 0 · rw [hp, zero_mul, trailingDegree_zero, top_add] by_cases hq : q = 0 · rw [hq, mul_zero, trailingDegree_zero, add_top] · rw [trailingDegree_eq_natTrailingDegree hp, trailingDegree_eq_natTrailingDegree hq, trailingDegree_eq_natTrailingDegree (mul_ne_zero hp hq), natTrailingDegree_mul hp hq] apply WithTop.coe_add end NoZeroDivisors section CommRing variable [CommRing R] theorem rootMultiplicity_eq_rootMultiplicity {p : R[X]} {t : R} : p.rootMultiplicity t = (p.comp (X + C t)).rootMultiplicity 0 := by classical simp_rw [rootMultiplicity_eq_multiplicity, comp_X_add_C_eq_zero_iff] congr 1 rw [C_0, sub_zero] convert (multiplicity_map_eq <| algEquivAevalXAddC t).symm using 2 simp [C_eq_algebraMap] /-- See `Polynomial.rootMultiplicity_eq_natTrailingDegree'` for the special case of `t = 0`. -/ theorem rootMultiplicity_eq_natTrailingDegree {p : R[X]} {t : R} : p.rootMultiplicity t = (p.comp (X + C t)).natTrailingDegree := rootMultiplicity_eq_rootMultiplicity.trans rootMultiplicity_eq_natTrailingDegree' section nonZeroDivisors open scoped nonZeroDivisors theorem Monic.mem_nonZeroDivisors {p : R[X]} (h : p.Monic) : p ∈ R[X]⁰ := mem_nonzeroDivisors_of_coeff_mem _ (h.coeff_natDegree ▸ one_mem R⁰) theorem mem_nonZeroDivisors_of_leadingCoeff {p : R[X]} (h : p.leadingCoeff ∈ R⁰) : p ∈ R[X]⁰ := mem_nonzeroDivisors_of_coeff_mem _ h theorem mem_nonZeroDivisors_of_trailingCoeff {p : R[X]} (h : p.trailingCoeff ∈ R⁰) : p ∈ R[X]⁰ := mem_nonzeroDivisors_of_coeff_mem _ h end nonZeroDivisors theorem natDegree_pos_of_monic_of_aeval_eq_zero [Nontrivial R] [Semiring S] [Algebra R S] [FaithfulSMul R S] {p : R[X]} (hp : p.Monic) {x : S} (hx : aeval x p = 0) : 0 < p.natDegree := natDegree_pos_of_aeval_root (Monic.ne_zero hp) hx ((injective_iff_map_eq_zero (algebraMap R S)).mp (FaithfulSMul.algebraMap_injective R S)) theorem rootMultiplicity_mul_X_sub_C_pow {p : R[X]} {a : R} {n : ℕ} (h : p ≠ 0) : (p * (X - C a) ^ n).rootMultiplicity a = p.rootMultiplicity a + n := by have h2 := monic_X_sub_C a |>.pow n |>.mul_left_ne_zero h refine le_antisymm ?_ ?_ · rw [rootMultiplicity_le_iff h2, add_assoc, add_comm n, ← add_assoc, pow_add, dvd_cancel_right_mem_nonZeroDivisors (monic_X_sub_C a |>.pow n |>.mem_nonZeroDivisors)] exact pow_rootMultiplicity_not_dvd h a · rw [le_rootMultiplicity_iff h2, pow_add] exact mul_dvd_mul_right (pow_rootMultiplicity_dvd p a) _ /-- The multiplicity of `a` as root of `(X - a) ^ n` is `n`. -/ theorem rootMultiplicity_X_sub_C_pow [Nontrivial R] (a : R) (n : ℕ) : rootMultiplicity a ((X - C a) ^ n) = n := by have := rootMultiplicity_mul_X_sub_C_pow (a := a) (n := n) C.map_one_ne_zero rwa [rootMultiplicity_C, map_one, one_mul, zero_add] at this theorem rootMultiplicity_X_sub_C_self [Nontrivial R] {x : R} : rootMultiplicity x (X - C x) = 1 := pow_one (X - C x) ▸ rootMultiplicity_X_sub_C_pow x 1 -- Porting note: swapped instance argument order theorem rootMultiplicity_X_sub_C [Nontrivial R] [DecidableEq R] {x y : R} : rootMultiplicity x (X - C y) = if x = y then 1 else 0 := by split_ifs with hxy · rw [hxy] exact rootMultiplicity_X_sub_C_self exact rootMultiplicity_eq_zero (mt root_X_sub_C.mp (Ne.symm hxy)) theorem rootMultiplicity_mul' {p q : R[X]} {x : R} (hpq : (p /ₘ (X - C x) ^ p.rootMultiplicity x).eval x * (q /ₘ (X - C x) ^ q.rootMultiplicity x).eval x ≠ 0) : rootMultiplicity x (p * q) = rootMultiplicity x p + rootMultiplicity x q := by simp_rw [eval_divByMonic_eq_trailingCoeff_comp] at hpq
simp_rw [rootMultiplicity_eq_natTrailingDegree, mul_comp, natTrailingDegree_mul' hpq] theorem Monic.neg_one_pow_natDegree_mul_comp_neg_X {p : R[X]} (hp : p.Monic) : ((-1) ^ p.natDegree * p.comp (-X)).Monic := by
Mathlib/Algebra/Polynomial/RingDivision.lean
172
175
/- Copyright (c) 2021 Hunter Monroe. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hunter Monroe, Kyle Miller -/ import Mathlib.Combinatorics.SimpleGraph.Dart import Mathlib.Data.FunLike.Fintype import Mathlib.Logic.Embedding.Set /-! # Maps between graphs This file defines two functions and three structures relating graphs. The structures directly correspond to the classification of functions as injective, surjective and bijective, and have corresponding notation. ## Main definitions * `SimpleGraph.map`: the graph obtained by pushing the adjacency relation through an injective function between vertex types. * `SimpleGraph.comap`: the graph obtained by pulling the adjacency relation behind an arbitrary function between vertex types. * `SimpleGraph.induce`: the subgraph induced by the given vertex set, a wrapper around `comap`. * `SimpleGraph.spanningCoe`: the supergraph without any additional edges, a wrapper around `map`. * `SimpleGraph.Hom`, `G →g H`: a graph homomorphism from `G` to `H`. * `SimpleGraph.Embedding`, `G ↪g H`: a graph embedding of `G` in `H`. * `SimpleGraph.Iso`, `G ≃g H`: a graph isomorphism between `G` and `H`. Note that a graph embedding is a stronger notion than an injective graph homomorphism, since its image is an induced subgraph. ## Implementation notes Morphisms of graphs are abbreviations for `RelHom`, `RelEmbedding` and `RelIso`. To make use of pre-existing simp lemmas, definitions involving morphisms are abbreviations as well. -/ open Function namespace SimpleGraph variable {V W X : Type*} (G : SimpleGraph V) (G' : SimpleGraph W) {u v : V} /-! ## Map and comap -/ /-- Given an injective function, there is a covariant induced map on graphs by pushing forward the adjacency relation. This is injective (see `SimpleGraph.map_injective`). -/ protected def map (f : V ↪ W) (G : SimpleGraph V) : SimpleGraph W where Adj := Relation.Map G.Adj f f symm a b := by -- Porting note: `obviously` used to handle this rintro ⟨v, w, h, rfl, rfl⟩ use w, v, h.symm, rfl loopless a := by -- Porting note: `obviously` used to handle this rintro ⟨v, w, h, rfl, h'⟩ exact h.ne (f.injective h'.symm) instance instDecidableMapAdj {f : V ↪ W} {a b} [Decidable (Relation.Map G.Adj f f a b)] : Decidable ((G.map f).Adj a b) := ‹Decidable (Relation.Map G.Adj f f a b)› @[simp] theorem map_adj (f : V ↪ W) (G : SimpleGraph V) (u v : W) : (G.map f).Adj u v ↔ ∃ u' v' : V, G.Adj u' v' ∧ f u' = u ∧ f v' = v := Iff.rfl lemma map_adj_apply {G : SimpleGraph V} {f : V ↪ W} {a b : V} : (G.map f).Adj (f a) (f b) ↔ G.Adj a b := by simp theorem map_monotone (f : V ↪ W) : Monotone (SimpleGraph.map f) := by rintro G G' h _ _ ⟨u, v, ha, rfl, rfl⟩ exact ⟨_, _, h ha, rfl, rfl⟩ @[simp] lemma map_id : G.map (Function.Embedding.refl _) = G := SimpleGraph.ext <| Relation.map_id_id _ @[simp] lemma map_map (f : V ↪ W) (g : W ↪ X) : (G.map f).map g = G.map (f.trans g) := SimpleGraph.ext <| Relation.map_map _ _ _ _ _ /-- Given a function, there is a contravariant induced map on graphs by pulling back the adjacency relation. This is one of the ways of creating induced graphs. See `SimpleGraph.induce` for a wrapper. This is surjective when `f` is injective (see `SimpleGraph.comap_surjective`). -/ protected def comap (f : V → W) (G : SimpleGraph W) : SimpleGraph V where Adj u v := G.Adj (f u) (f v) symm _ _ h := h.symm loopless _ := G.loopless _ @[simp] lemma comap_adj {G : SimpleGraph W} {f : V → W} : (G.comap f).Adj u v ↔ G.Adj (f u) (f v) := Iff.rfl @[simp] lemma comap_id {G : SimpleGraph V} : G.comap id = G := SimpleGraph.ext rfl @[simp] lemma comap_comap {G : SimpleGraph X} (f : V → W) (g : W → X) : (G.comap g).comap f = G.comap (g ∘ f) := rfl instance instDecidableComapAdj (f : V → W) (G : SimpleGraph W) [DecidableRel G.Adj] : DecidableRel (G.comap f).Adj := fun _ _ ↦ ‹DecidableRel G.Adj› _ _ lemma comap_symm (G : SimpleGraph V) (e : V ≃ W) : G.comap e.symm.toEmbedding = G.map e.toEmbedding := by ext; simp only [Equiv.apply_eq_iff_eq_symm_apply, comap_adj, map_adj, Equiv.toEmbedding_apply, exists_eq_right_right, exists_eq_right] lemma map_symm (G : SimpleGraph W) (e : V ≃ W) : G.map e.symm.toEmbedding = G.comap e.toEmbedding := by rw [← comap_symm, e.symm_symm] theorem comap_monotone (f : V ↪ W) : Monotone (SimpleGraph.comap f) := by intro G G' h _ _ ha exact h ha @[simp] theorem comap_map_eq (f : V ↪ W) (G : SimpleGraph V) : (G.map f).comap f = G := by ext simp theorem leftInverse_comap_map (f : V ↪ W) : Function.LeftInverse (SimpleGraph.comap f) (SimpleGraph.map f) := comap_map_eq f theorem map_injective (f : V ↪ W) : Function.Injective (SimpleGraph.map f) := (leftInverse_comap_map f).injective theorem comap_surjective (f : V ↪ W) : Function.Surjective (SimpleGraph.comap f) := (leftInverse_comap_map f).surjective theorem map_le_iff_le_comap (f : V ↪ W) (G : SimpleGraph V) (G' : SimpleGraph W) : G.map f ≤ G' ↔ G ≤ G'.comap f := ⟨fun h _ _ ha => h ⟨_, _, ha, rfl, rfl⟩, by rintro h _ _ ⟨u, v, ha, rfl, rfl⟩ exact h ha⟩ theorem map_comap_le (f : V ↪ W) (G : SimpleGraph W) : (G.comap f).map f ≤ G := by rw [map_le_iff_le_comap] lemma le_comap_of_subsingleton (f : V → W) [Subsingleton V] : G ≤ G'.comap f := by intros v w; simp [Subsingleton.elim v w] lemma map_le_of_subsingleton (f : V ↪ W) [Subsingleton V] : G.map f ≤ G' := by rw [map_le_iff_le_comap]; apply le_comap_of_subsingleton /-- Given a family of vertex types indexed by `ι`, pulling back from `⊤ : SimpleGraph ι` yields the complete multipartite graph on the family. Two vertices are adjacent if and only if their indices are not equal. -/ abbrev completeMultipartiteGraph {ι : Type*} (V : ι → Type*) : SimpleGraph (Σ i, V i) := SimpleGraph.comap Sigma.fst ⊤ /-- Equivalent types have equivalent simple graphs. -/ @[simps apply] protected def _root_.Equiv.simpleGraph (e : V ≃ W) : SimpleGraph V ≃ SimpleGraph W where toFun := SimpleGraph.comap e.symm invFun := SimpleGraph.comap e left_inv _ := by simp right_inv _ := by simp @[simp] lemma _root_.Equiv.simpleGraph_refl : (Equiv.refl V).simpleGraph = Equiv.refl _ := by ext; rfl @[simp] lemma _root_.Equiv.simpleGraph_trans (e₁ : V ≃ W) (e₂ : W ≃ X) : (e₁.trans e₂).simpleGraph = e₁.simpleGraph.trans e₂.simpleGraph := rfl @[simp] lemma _root_.Equiv.symm_simpleGraph (e : V ≃ W) : e.simpleGraph.symm = e.symm.simpleGraph := rfl /-! ## Induced graphs -/ /- Given a set `s` of vertices, we can restrict a graph to those vertices by restricting its adjacency relation. This gives a map between `SimpleGraph V` and `SimpleGraph s`. There is also a notion of induced subgraphs (see `SimpleGraph.subgraph.induce`). -/ /-- Restrict a graph to the vertices in the set `s`, deleting all edges incident to vertices outside the set. This is a wrapper around `SimpleGraph.comap`. -/ abbrev induce (s : Set V) (G : SimpleGraph V) : SimpleGraph s := G.comap (Function.Embedding.subtype _) @[simp] lemma induce_singleton_eq_top (v : V) : G.induce {v} = ⊤ := by rw [eq_top_iff]; apply le_comap_of_subsingleton /-- Given a graph on a set of vertices, we can make it be a `SimpleGraph V` by adding in the remaining vertices without adding in any additional edges. This is a wrapper around `SimpleGraph.map`. -/ abbrev spanningCoe {s : Set V} (G : SimpleGraph s) : SimpleGraph V := G.map (Function.Embedding.subtype _) theorem induce_spanningCoe {s : Set V} {G : SimpleGraph s} : G.spanningCoe.induce s = G := comap_map_eq _ _ theorem spanningCoe_induce_le (s : Set V) : (G.induce s).spanningCoe ≤ G := map_comap_le _ _ /-! ## Homomorphisms, embeddings and isomorphisms -/ /-- A graph homomorphism is a map on vertex sets that respects adjacency relations. The notation `G →g G'` represents the type of graph homomorphisms. -/ abbrev Hom := RelHom G.Adj G'.Adj /-- A graph embedding is an embedding `f` such that for vertices `v w : V`, `G'.Adj (f v) (f w) ↔ G.Adj v w`. Its image is an induced subgraph of G'. The notation `G ↪g G'` represents the type of graph embeddings. -/ abbrev Embedding := RelEmbedding G.Adj G'.Adj /-- A graph isomorphism is a bijective map on vertex sets that respects adjacency relations. The notation `G ≃g G'` represents the type of graph isomorphisms. -/ abbrev Iso := RelIso G.Adj G'.Adj @[inherit_doc] infixl:50 " →g " => Hom @[inherit_doc] infixl:50 " ↪g " => Embedding @[inherit_doc] infixl:50 " ≃g " => Iso namespace Hom variable {G G'} {G₁ G₂ : SimpleGraph V} {H : SimpleGraph W} (f : G →g G') /-- The identity homomorphism from a graph to itself. -/ protected abbrev id : G →g G := RelHom.id _ @[simp, norm_cast] lemma coe_id : ⇑(Hom.id : G →g G) = id := rfl instance [Subsingleton (V → W)] : Subsingleton (G →g H) := DFunLike.coe_injective.subsingleton instance [IsEmpty V] : Unique (G →g H) where default := ⟨isEmptyElim, fun {a} ↦ isEmptyElim a⟩ uniq _ := Subsingleton.elim _ _ instance [Finite V] [Finite W] : Finite (G →g H) := DFunLike.finite _ theorem map_adj {v w : V} (h : G.Adj v w) : G'.Adj (f v) (f w) := f.map_rel' h theorem map_mem_edgeSet {e : Sym2 V} (h : e ∈ G.edgeSet) : e.map f ∈ G'.edgeSet := Sym2.ind (fun _ _ => f.map_rel') e h theorem apply_mem_neighborSet {v w : V} (h : w ∈ G.neighborSet v) : f w ∈ G'.neighborSet (f v) := map_adj f h /-- The map between edge sets induced by a homomorphism. The underlying map on edges is given by `Sym2.map`. -/ @[simps] def mapEdgeSet (e : G.edgeSet) : G'.edgeSet := ⟨Sym2.map f e, f.map_mem_edgeSet e.property⟩ /-- The map between neighbor sets induced by a homomorphism. -/ @[simps] def mapNeighborSet (v : V) (w : G.neighborSet v) : G'.neighborSet (f v) := ⟨f w, f.apply_mem_neighborSet w.property⟩ /-- The map between darts induced by a homomorphism. -/ def mapDart (d : G.Dart) : G'.Dart := ⟨d.1.map f f, f.map_adj d.2⟩ @[simp] theorem mapDart_apply (d : G.Dart) : f.mapDart d = ⟨d.1.map f f, f.map_adj d.2⟩ := rfl /-- The graph homomorphism from a smaller graph to a bigger one. -/ def ofLE (h : G₁ ≤ G₂) : G₁ →g G₂ := ⟨id, @h⟩ @[simp, norm_cast] lemma coe_ofLE (h : G₁ ≤ G₂) : ⇑(ofLE h) = id := rfl lemma ofLE_apply (h : G₁ ≤ G₂) (v : V) : ofLE h v = v := rfl /-- The induced map for spanning subgraphs, which is the identity on vertices. -/ @[deprecated ofLE (since := "2025-03-17")] def mapSpanningSubgraphs {G G' : SimpleGraph V} (h : G ≤ G') : G →g G' where toFun x := x map_rel' ha := h ha @[deprecated "This is true by simp" (since := "2025-03-17")] lemma mapSpanningSubgraphs_inj {G G' : SimpleGraph V} {v w : V} (h : G ≤ G') : ofLE h v = ofLE h w ↔ v = w := by simp @[deprecated "This is true by simp" (since := "2025-03-17")] lemma mapSpanningSubgraphs_injective {G G' : SimpleGraph V} (h : G ≤ G') : Injective (ofLE h) := fun v w hvw ↦ by simpa using hvw theorem mapEdgeSet.injective (hinj : Function.Injective f) : Function.Injective f.mapEdgeSet := by rintro ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ dsimp [Hom.mapEdgeSet] repeat rw [Subtype.mk_eq_mk] apply Sym2.map.injective hinj /-- Every graph homomorphism from a complete graph is injective. -/ theorem injective_of_top_hom (f : (⊤ : SimpleGraph V) →g G') : Function.Injective f := by intro v w h contrapose! h exact G'.ne_of_adj (map_adj _ ((top_adj _ _).mpr h)) /-- There is a homomorphism to a graph from a comapped graph. When the function is injective, this is an embedding (see `SimpleGraph.Embedding.comap`). -/ @[simps] protected def comap (f : V → W) (G : SimpleGraph W) : G.comap f →g G where toFun := f map_rel' := by simp variable {G'' : SimpleGraph X} /-- Composition of graph homomorphisms. -/ abbrev comp (f' : G' →g G'') (f : G →g G') : G →g G'' := RelHom.comp f' f @[simp] theorem coe_comp (f' : G' →g G'') (f : G →g G') : ⇑(f'.comp f) = f' ∘ f := rfl end Hom namespace Embedding variable {G G'} {H : SimpleGraph W} (f : G ↪g G') /-- The identity embedding from a graph to itself. -/ abbrev refl : G ↪g G := RelEmbedding.refl _ /-- An embedding of graphs gives rise to a homomorphism of graphs. -/ abbrev toHom : G →g G' := f.toRelHom @[simp] lemma coe_toHom (f : G ↪g H) : ⇑f.toHom = f := rfl @[simp] theorem map_adj_iff {v w : V} : G'.Adj (f v) (f w) ↔ G.Adj v w := f.map_rel_iff theorem map_mem_edgeSet_iff {e : Sym2 V} : e.map f ∈ G'.edgeSet ↔ e ∈ G.edgeSet := Sym2.ind (fun _ _ => f.map_adj_iff) e theorem apply_mem_neighborSet_iff {v w : V} : f w ∈ G'.neighborSet (f v) ↔ w ∈ G.neighborSet v := map_adj_iff f /-- A graph embedding induces an embedding of edge sets. -/ @[simps] def mapEdgeSet : G.edgeSet ↪ G'.edgeSet where toFun := Hom.mapEdgeSet f inj' := Hom.mapEdgeSet.injective f.toRelHom f.injective /-- A graph embedding induces an embedding of neighbor sets. -/ @[simps] def mapNeighborSet (v : V) : G.neighborSet v ↪ G'.neighborSet (f v) where toFun w := ⟨f w, f.apply_mem_neighborSet_iff.mpr w.2⟩ inj' := by rintro ⟨w₁, h₁⟩ ⟨w₂, h₂⟩ h rw [Subtype.mk_eq_mk] at h ⊢ exact f.inj' h /-- Given an injective function, there is an embedding from the comapped graph into the original graph. -/ -- Porting note: @[simps] does not work here since `f` is not a constructor application. -- `@[simps toEmbedding]` could work, but Floris suggested writing `comap_apply` for now. protected def comap (f : V ↪ W) (G : SimpleGraph W) : G.comap f ↪g G := { f with map_rel_iff' := by simp } @[simp] theorem comap_apply (f : V ↪ W) (G : SimpleGraph W) (v : V) : SimpleGraph.Embedding.comap f G v = f v := rfl /-- Given an injective function, there is an embedding from a graph into the mapped graph. -/ -- Porting note: @[simps] does not work here since `f` is not a constructor application. -- `@[simps toEmbedding]` could work, but Floris suggested writing `map_apply` for now. protected def map (f : V ↪ W) (G : SimpleGraph V) : G ↪g G.map f := { f with map_rel_iff' := by simp } @[simp] theorem map_apply (f : V ↪ W) (G : SimpleGraph V) (v : V) : SimpleGraph.Embedding.map f G v = f v := rfl /-- Induced graphs embed in the original graph. Note that if `G.induce s = ⊤` (i.e., if `s` is a clique) then this gives the embedding of a complete graph. -/ protected abbrev induce (s : Set V) : G.induce s ↪g G := SimpleGraph.Embedding.comap (Function.Embedding.subtype _) G /-- Graphs on a set of vertices embed in their `spanningCoe`. -/ protected abbrev spanningCoe {s : Set V} (G : SimpleGraph s) : G ↪g G.spanningCoe := SimpleGraph.Embedding.map (Function.Embedding.subtype _) G /-- Embeddings of types induce embeddings of complete graphs on those types. -/ protected def completeGraph {α β : Type*} (f : α ↪ β) : (⊤ : SimpleGraph α) ↪g (⊤ : SimpleGraph β) := { f with map_rel_iff' := by simp } @[simp] lemma coe_completeGraph {α β : Type*} (f : α ↪ β) : ⇑(Embedding.completeGraph f) = f := rfl variable {G'' : SimpleGraph X} /-- Composition of graph embeddings. -/ abbrev comp (f' : G' ↪g G'') (f : G ↪g G') : G ↪g G'' := f.trans f' @[simp] theorem coe_comp (f' : G' ↪g G'') (f : G ↪g G') : ⇑(f'.comp f) = f' ∘ f := rfl /-- Graph embeddings from `G` to `H` are the same thing as graph embeddings from `Gᶜ` to `Hᶜ`. -/ def complEquiv : G ↪g H ≃ Gᶜ ↪g Hᶜ where toFun f := ⟨f.toEmbedding, by simp⟩ invFun f := ⟨f.toEmbedding, fun {v w} ↦ by obtain rfl | hvw := eq_or_ne v w · simp · simpa [hvw, not_iff_not] using f.map_adj_iff (v := v) (w := w)⟩ left_inv f := rfl right_inv f := rfl end Embedding section induceHom variable {G G'} {G'' : SimpleGraph X} {s : Set V} {t : Set W} {r : Set X} (φ : G →g G') (φst : Set.MapsTo φ s t) (ψ : G' →g G'') (ψtr : Set.MapsTo ψ t r) /-- The restriction of a morphism of graphs to induced subgraphs. -/ def induceHom : G.induce s →g G'.induce t where toFun := Set.MapsTo.restrict φ s t φst map_rel' := φ.map_rel' @[simp, norm_cast] lemma coe_induceHom : ⇑(induceHom φ φst) = Set.MapsTo.restrict φ s t φst := rfl @[simp] lemma induceHom_id (G : SimpleGraph V) (s) : induceHom (Hom.id : G →g G) (Set.mapsTo_id s) = Hom.id := by ext x rfl @[simp] lemma induceHom_comp : (induceHom ψ ψtr).comp (induceHom φ φst) = induceHom (ψ.comp φ) (ψtr.comp φst) := by ext x rfl lemma induceHom_injective (hi : Set.InjOn φ s) : Function.Injective (induceHom φ φst) := by simpa [Set.MapsTo.restrict_inj] end induceHom section induceHomLE variable {s s' : Set V} (h : s ≤ s') /-- Given an inclusion of vertex subsets, the induced embedding on induced graphs. This is not an abbreviation for `induceHom` since we get an embedding in this case. -/ def induceHomOfLE (h : s ≤ s') : G.induce s ↪g G.induce s' where toEmbedding := Set.embeddingOfSubset s s' h map_rel_iff' := by simp @[simp] lemma induceHomOfLE_apply (v : s) : (G.induceHomOfLE h) v = Set.inclusion h v := rfl @[simp] lemma induceHomOfLE_toHom : (G.induceHomOfLE h).toHom = induceHom (.id : G →g G) ((Set.mapsTo_id s).mono_right h) := by ext; simp end induceHomLE namespace Iso variable {G G'} (f : G ≃g G') /-- The identity isomorphism of a graph with itself. -/ abbrev refl : G ≃g G := RelIso.refl _ /-- An isomorphism of graphs gives rise to an embedding of graphs. -/ abbrev toEmbedding : G ↪g G' := f.toRelEmbedding /-- An isomorphism of graphs gives rise to a homomorphism of graphs. -/ abbrev toHom : G →g G' := f.toEmbedding.toHom /-- The inverse of a graph isomorphism. -/ abbrev symm : G' ≃g G := RelIso.symm f theorem map_adj_iff {v w : V} : G'.Adj (f v) (f w) ↔ G.Adj v w := f.map_rel_iff
theorem map_mem_edgeSet_iff {e : Sym2 V} : e.map f ∈ G'.edgeSet ↔ e ∈ G.edgeSet := Sym2.ind (fun _ _ => f.map_adj_iff) e
Mathlib/Combinatorics/SimpleGraph/Maps.lean
489
492
/- 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.AlgebraicGeometry.Cover.Open import Mathlib.AlgebraicGeometry.Over /-! # Restriction of Schemes and Morphisms ## Main definition - `AlgebraicGeometry.Scheme.restrict`: The restriction of a scheme along an open embedding. The map `X.restrict f ⟶ X` is `AlgebraicGeometry.Scheme.ofRestrict`. `U : X.Opens` has a coercion to `Scheme` and `U.ι` is a shorthand for `X.restrict U.open_embedding : U ⟶ X`. - `AlgebraicGeometry.morphism_restrict`: The restriction of `X ⟶ Y` to `X ∣_ᵤ f ⁻¹ᵁ U ⟶ Y ∣_ᵤ U`. -/ -- Explicit universe annotations were used in this file to improve performance https://github.com/leanprover-community/mathlib4/issues/12737 noncomputable section open TopologicalSpace CategoryTheory Opposite open CategoryTheory.Limits namespace AlgebraicGeometry universe v v₁ v₂ u u₁ variable {C : Type u₁} [Category.{v} C] section variable {X : Scheme.{u}} (U : X.Opens) namespace Scheme.Opens /-- Open subset of a scheme as a scheme. -/ @[coe] def toScheme {X : Scheme.{u}} (U : X.Opens) : Scheme.{u} := X.restrict U.isOpenEmbedding instance : CoeOut X.Opens Scheme := ⟨toScheme⟩ /-- The restriction of a scheme to an open subset. -/ def ι : ↑U ⟶ X := X.ofRestrict _ @[simp] lemma ι_base_apply (x : U) : U.ι.base x = x.val := rfl instance : IsOpenImmersion U.ι := inferInstanceAs (IsOpenImmersion (X.ofRestrict _)) @[simps! over] instance : U.toScheme.CanonicallyOver X where hom := U.ι instance (U : X.Opens) : U.ι.IsOver X where lemma toScheme_carrier : (U : Type u) = (U : Set X) := rfl lemma toScheme_presheaf_obj (V) : Γ(U, V) = Γ(X, U.ι ''ᵁ V) := rfl @[simp] lemma toScheme_presheaf_map {V W} (i : V ⟶ W) : U.toScheme.presheaf.map i = X.presheaf.map (U.ι.opensFunctor.map i.unop).op := rfl @[simp] lemma ι_app (V) : U.ι.app V = X.presheaf.map (homOfLE (x := U.ι ''ᵁ U.ι ⁻¹ᵁ V) (Set.image_preimage_subset _ _)).op := rfl @[simp] lemma ι_appTop : U.ι.appTop = X.presheaf.map (homOfLE (x := U.ι ''ᵁ ⊤) le_top).op := rfl
@[simp] lemma ι_appLE (V W e) : U.ι.appLE V W e =
Mathlib/AlgebraicGeometry/Restrict.lean
80
82
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Ken Lee, Chris Hughes -/ import Mathlib.Algebra.Group.Action.Units import Mathlib.Algebra.Group.Nat.Units import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.Ring.Divisibility.Basic import Mathlib.Algebra.Ring.Hom.Defs import Mathlib.Logic.Basic import Mathlib.Tactic.Ring /-! # Coprime elements of a ring or monoid ## Main definition * `IsCoprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors (`IsRelPrime`) are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. The two notions are equivalent in Bézout rings, see `isRelPrime_iff_isCoprime`. This file also contains lemmas about `IsRelPrime` parallel to `IsCoprime`. See also `RingTheory.Coprime.Lemmas` for further development of coprime elements. -/ universe u v section CommSemiring variable {R : Type u} [CommSemiring R] (x y z : R) /-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/ def IsCoprime : Prop := ∃ a b, a * x + b * y = 1 variable {x y z} @[symm] theorem IsCoprime.symm (H : IsCoprime x y) : IsCoprime y x := let ⟨a, b, H⟩ := H ⟨b, a, by rw [add_comm, H]⟩ theorem isCoprime_comm : IsCoprime x y ↔ IsCoprime y x := ⟨IsCoprime.symm, IsCoprime.symm⟩ theorem isCoprime_self : IsCoprime x x ↔ IsUnit x := ⟨fun ⟨a, b, h⟩ => isUnit_of_mul_eq_one x (a + b) <| by rwa [mul_comm, add_mul], fun h => let ⟨b, hb⟩ := isUnit_iff_exists_inv'.1 h ⟨b, 0, by rwa [zero_mul, add_zero]⟩⟩ theorem isCoprime_zero_left : IsCoprime 0 x ↔ IsUnit x := ⟨fun ⟨a, b, H⟩ => isUnit_of_mul_eq_one x b <| by rwa [mul_zero, zero_add, mul_comm] at H, fun H => let ⟨b, hb⟩ := isUnit_iff_exists_inv'.1 H ⟨1, b, by rwa [one_mul, zero_add]⟩⟩ theorem isCoprime_zero_right : IsCoprime x 0 ↔ IsUnit x := isCoprime_comm.trans isCoprime_zero_left theorem not_isCoprime_zero_zero [Nontrivial R] : ¬IsCoprime (0 : R) 0 := mt isCoprime_zero_right.mp not_isUnit_zero lemma IsCoprime.intCast {R : Type*} [CommRing R] {a b : ℤ} (h : IsCoprime a b) : IsCoprime (a : R) (b : R) := by rcases h with ⟨u, v, H⟩ use u, v rw_mod_cast [H] exact Int.cast_one /-- If a 2-vector `p` satisfies `IsCoprime (p 0) (p 1)`, then `p ≠ 0`. -/ theorem IsCoprime.ne_zero [Nontrivial R] {p : Fin 2 → R} (h : IsCoprime (p 0) (p 1)) : p ≠ 0 := by rintro rfl exact not_isCoprime_zero_zero h theorem IsCoprime.ne_zero_or_ne_zero [Nontrivial R] (h : IsCoprime x y) : x ≠ 0 ∨ y ≠ 0 := by apply not_or_of_imp rintro rfl rfl exact not_isCoprime_zero_zero h theorem isCoprime_one_left : IsCoprime 1 x := ⟨1, 0, by rw [one_mul, zero_mul, add_zero]⟩ theorem isCoprime_one_right : IsCoprime x 1 := ⟨0, 1, by rw [one_mul, zero_mul, zero_add]⟩ theorem IsCoprime.dvd_of_dvd_mul_right (H1 : IsCoprime x z) (H2 : x ∣ y * z) : x ∣ y := by let ⟨a, b, H⟩ := H1 rw [← mul_one y, ← H, mul_add, ← mul_assoc, mul_left_comm] exact dvd_add (dvd_mul_left _ _) (H2.mul_left _) theorem IsCoprime.dvd_of_dvd_mul_left (H1 : IsCoprime x y) (H2 : x ∣ y * z) : x ∣ z := by let ⟨a, b, H⟩ := H1 rw [← one_mul z, ← H, add_mul, mul_right_comm, mul_assoc b] exact dvd_add (dvd_mul_left _ _) (H2.mul_left _) theorem IsCoprime.mul_left (H1 : IsCoprime x z) (H2 : IsCoprime y z) : IsCoprime (x * y) z := let ⟨a, b, h1⟩ := H1 let ⟨c, d, h2⟩ := H2 ⟨a * c, a * x * d + b * c * y + b * d * z, calc a * c * (x * y) + (a * x * d + b * c * y + b * d * z) * z _ = (a * x + b * z) * (c * y + d * z) := by ring _ = 1 := by rw [h1, h2, mul_one] ⟩ theorem IsCoprime.mul_right (H1 : IsCoprime x y) (H2 : IsCoprime x z) : IsCoprime x (y * z) := by rw [isCoprime_comm] at H1 H2 ⊢ exact H1.mul_left H2 theorem IsCoprime.mul_dvd (H : IsCoprime x y) (H1 : x ∣ z) (H2 : y ∣ z) : x * y ∣ z := by obtain ⟨a, b, h⟩ := H rw [← mul_one z, ← h, mul_add] apply dvd_add · rw [mul_comm z, mul_assoc] exact (mul_dvd_mul_left _ H2).mul_left _ · rw [mul_comm b, ← mul_assoc] exact (mul_dvd_mul_right H1 _).mul_right _ theorem IsCoprime.of_mul_left_left (H : IsCoprime (x * y) z) : IsCoprime x z := let ⟨a, b, h⟩ := H ⟨a * y, b, by rwa [mul_right_comm, mul_assoc]⟩ theorem IsCoprime.of_mul_left_right (H : IsCoprime (x * y) z) : IsCoprime y z := by rw [mul_comm] at H exact H.of_mul_left_left theorem IsCoprime.of_mul_right_left (H : IsCoprime x (y * z)) : IsCoprime x y := by rw [isCoprime_comm] at H ⊢ exact H.of_mul_left_left theorem IsCoprime.of_mul_right_right (H : IsCoprime x (y * z)) : IsCoprime x z := by rw [mul_comm] at H exact H.of_mul_right_left theorem IsCoprime.mul_left_iff : IsCoprime (x * y) z ↔ IsCoprime x z ∧ IsCoprime y z := ⟨fun H => ⟨H.of_mul_left_left, H.of_mul_left_right⟩, fun ⟨H1, H2⟩ => H1.mul_left H2⟩ theorem IsCoprime.mul_right_iff : IsCoprime x (y * z) ↔ IsCoprime x y ∧ IsCoprime x z := by rw [isCoprime_comm, IsCoprime.mul_left_iff, isCoprime_comm, @isCoprime_comm _ _ z] theorem IsCoprime.of_isCoprime_of_dvd_left (h : IsCoprime y z) (hdvd : x ∣ y) : IsCoprime x z := by obtain ⟨d, rfl⟩ := hdvd exact IsCoprime.of_mul_left_left h theorem IsCoprime.of_isCoprime_of_dvd_right (h : IsCoprime z y) (hdvd : x ∣ y) : IsCoprime z x := (h.symm.of_isCoprime_of_dvd_left hdvd).symm theorem IsCoprime.isUnit_of_dvd (H : IsCoprime x y) (d : x ∣ y) : IsUnit x := let ⟨k, hk⟩ := d isCoprime_self.1 <| IsCoprime.of_mul_right_left <| show IsCoprime x (x * k) from hk ▸ H theorem IsCoprime.isUnit_of_dvd' {a b x : R} (h : IsCoprime a b) (ha : x ∣ a) (hb : x ∣ b) : IsUnit x := (h.of_isCoprime_of_dvd_left ha).isUnit_of_dvd hb theorem IsCoprime.isRelPrime {a b : R} (h : IsCoprime a b) : IsRelPrime a b := fun _ ↦ h.isUnit_of_dvd' theorem IsCoprime.map (H : IsCoprime x y) {S : Type v} [CommSemiring S] (f : R →+* S) : IsCoprime (f x) (f y) := let ⟨a, b, h⟩ := H ⟨f a, f b, by rw [← f.map_mul, ← f.map_mul, ← f.map_add, h, f.map_one]⟩ theorem IsCoprime.of_add_mul_left_left (h : IsCoprime (x + y * z) y) : IsCoprime x y := let ⟨a, b, H⟩ := h ⟨a, a * z + b, by simpa only [add_mul, mul_add, add_assoc, add_comm, add_left_comm, mul_assoc, mul_comm, mul_left_comm] using H⟩ theorem IsCoprime.of_add_mul_right_left (h : IsCoprime (x + z * y) y) : IsCoprime x y := by rw [mul_comm] at h exact h.of_add_mul_left_left theorem IsCoprime.of_add_mul_left_right (h : IsCoprime x (y + x * z)) : IsCoprime x y := by rw [isCoprime_comm] at h ⊢ exact h.of_add_mul_left_left theorem IsCoprime.of_add_mul_right_right (h : IsCoprime x (y + z * x)) : IsCoprime x y := by rw [mul_comm] at h exact h.of_add_mul_left_right theorem IsCoprime.of_mul_add_left_left (h : IsCoprime (y * z + x) y) : IsCoprime x y := by rw [add_comm] at h exact h.of_add_mul_left_left theorem IsCoprime.of_mul_add_right_left (h : IsCoprime (z * y + x) y) : IsCoprime x y := by rw [add_comm] at h exact h.of_add_mul_right_left theorem IsCoprime.of_mul_add_left_right (h : IsCoprime x (x * z + y)) : IsCoprime x y := by rw [add_comm] at h exact h.of_add_mul_left_right theorem IsCoprime.of_mul_add_right_right (h : IsCoprime x (z * x + y)) : IsCoprime x y := by rw [add_comm] at h exact h.of_add_mul_right_right theorem IsRelPrime.of_add_mul_left_left (h : IsRelPrime (x + y * z) y) : IsRelPrime x y := fun _ hx hy ↦ h (dvd_add hx <| dvd_mul_of_dvd_left hy z) hy theorem IsRelPrime.of_add_mul_right_left (h : IsRelPrime (x + z * y) y) : IsRelPrime x y := (mul_comm z y ▸ h).of_add_mul_left_left theorem IsRelPrime.of_add_mul_left_right (h : IsRelPrime x (y + x * z)) : IsRelPrime x y := by rw [isRelPrime_comm] at h ⊢ exact h.of_add_mul_left_left theorem IsRelPrime.of_add_mul_right_right (h : IsRelPrime x (y + z * x)) : IsRelPrime x y := (mul_comm z x ▸ h).of_add_mul_left_right theorem IsRelPrime.of_mul_add_left_left (h : IsRelPrime (y * z + x) y) : IsRelPrime x y := (add_comm _ x ▸ h).of_add_mul_left_left theorem IsRelPrime.of_mul_add_right_left (h : IsRelPrime (z * y + x) y) : IsRelPrime x y := (add_comm _ x ▸ h).of_add_mul_right_left theorem IsRelPrime.of_mul_add_left_right (h : IsRelPrime x (x * z + y)) : IsRelPrime x y :=
(add_comm _ y ▸ h).of_add_mul_left_right theorem IsRelPrime.of_mul_add_right_right (h : IsRelPrime x (z * x + y)) : IsRelPrime x y :=
Mathlib/RingTheory/Coprime/Basic.lean
222
224
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import Mathlib.Topology.Algebra.Algebra import Mathlib.Analysis.InnerProductSpace.Convex import Mathlib.Algebra.Module.LinearMap.Rat import Mathlib.Tactic.Module /-! # Inner product space derived from a norm This file defines an `InnerProductSpace` instance from a norm that respects the parallellogram identity. The parallelogram identity is a way to express the inner product of `x` and `y` in terms of the norms of `x`, `y`, `x + y`, `x - y`. ## Main results - `InnerProductSpace.ofNorm`: a normed space whose norm respects the parallellogram identity, can be seen as an inner product space. ## Implementation notes We define `inner_` $$\langle x, y \rangle := \frac{1}{4} (‖x + y‖^2 - ‖x - y‖^2 + i ‖ix + y‖ ^ 2 - i ‖ix - y‖^2)$$ and use the parallelogram identity $$‖x + y‖^2 + ‖x - y‖^2 = 2 (‖x‖^2 + ‖y‖^2)$$ to prove it is an inner product, i.e., that it is conjugate-symmetric (`inner_.conj_symm`) and linear in the first argument. `add_left` is proved by judicious application of the parallelogram identity followed by tedious arithmetic. `smul_left` is proved step by step, first noting that $\langle λ x, y \rangle = λ \langle x, y \rangle$ for $λ ∈ ℕ$, $λ = -1$, hence $λ ∈ ℤ$ and $λ ∈ ℚ$ by arithmetic. Then by continuity and the fact that ℚ is dense in ℝ, the same is true for ℝ. The case of ℂ then follows by applying the result for ℝ and more arithmetic. ## TODO Move upstream to `Analysis.InnerProductSpace.Basic`. ## References - [Jordan, P. and von Neumann, J., *On inner products in linear, metric spaces*][Jordan1935] - https://math.stackexchange.com/questions/21792/norms-induced-by-inner-products-and-the-parallelogram-law - https://math.dartmouth.edu/archive/m113w10/public_html/jordan-vneumann-thm.pdf ## Tags inner product space, Hilbert space, norm -/ open RCLike open scoped ComplexConjugate variable {𝕜 : Type*} [RCLike 𝕜] (E : Type*) [NormedAddCommGroup E] /-- Predicate for the parallelogram identity to hold in a normed group. This is a scalar-less version of `InnerProductSpace`. If you have an `InnerProductSpaceable` assumption, you can locally upgrade that to `InnerProductSpace 𝕜 E` using `casesI nonempty_innerProductSpace 𝕜 E`. -/ class InnerProductSpaceable : Prop where parallelogram_identity : ∀ x y : E, ‖x + y‖ * ‖x + y‖ + ‖x - y‖ * ‖x - y‖ = 2 * (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) variable (𝕜) {E} theorem InnerProductSpace.toInnerProductSpaceable [InnerProductSpace 𝕜 E] : InnerProductSpaceable E := ⟨parallelogram_law_with_norm 𝕜⟩ -- See note [lower instance priority] instance (priority := 100) InnerProductSpace.toInnerProductSpaceable_ofReal [InnerProductSpace ℝ E] : InnerProductSpaceable E := ⟨parallelogram_law_with_norm ℝ⟩ variable [NormedSpace 𝕜 E] local notation "𝓚" => algebraMap ℝ 𝕜 /-- Auxiliary definition of the inner product derived from the norm. -/ private noncomputable def inner_ (x y : E) : 𝕜 := 4⁻¹ * (𝓚 ‖x + y‖ * 𝓚 ‖x + y‖ - 𝓚 ‖x - y‖ * 𝓚 ‖x - y‖ + (I : 𝕜) * 𝓚 ‖(I : 𝕜) • x + y‖ * 𝓚 ‖(I : 𝕜) • x + y‖ - (I : 𝕜) * 𝓚 ‖(I : 𝕜) • x - y‖ * 𝓚 ‖(I : 𝕜) • x - y‖) namespace InnerProductSpaceable variable {𝕜} (E) -- This has a prime added to avoid clashing with public `innerProp` /-- Auxiliary definition for the `add_left` property. -/ private def innerProp' (r : 𝕜) : Prop := ∀ x y : E, inner_ 𝕜 (r • x) y = conj r * inner_ 𝕜 x y variable {E} theorem _root_.Continuous.inner_ {f g : ℝ → E} (hf : Continuous f) (hg : Continuous g) : Continuous fun x => inner_ 𝕜 (f x) (g x) := by unfold _root_.inner_ fun_prop theorem inner_.norm_sq (x : E) : ‖x‖ ^ 2 = re (inner_ 𝕜 x x) := by simp only [inner_, normSq_apply, ofNat_re, ofNat_im, map_sub, map_add, map_zero, map_mul, ofReal_re, ofReal_im, mul_re, inv_re, mul_im, I_re, inv_im] have h₁ : ‖x - x‖ = 0 := by simp have h₂ : ‖x + x‖ = 2 • ‖x‖ := by convert norm_nsmul 𝕜 2 x using 2; module rw [h₁, h₂] ring theorem inner_.conj_symm (x y : E) : conj (inner_ 𝕜 y x) = inner_ 𝕜 x y := by simp only [inner_, map_sub, map_add, map_mul, map_inv₀, map_ofNat, conj_ofReal, conj_I] rw [add_comm y x, norm_sub_rev] by_cases hI : (I : 𝕜) = 0 · simp only [hI, neg_zero, zero_mul] have hI' := I_mul_I_of_nonzero hI have I_smul (v : E) : ‖(I : 𝕜) • v‖ = ‖v‖ := by rw [norm_smul, norm_I_of_ne_zero hI, one_mul] have h₁ : ‖(I : 𝕜) • y - x‖ = ‖(I : 𝕜) • x + y‖ := by convert I_smul ((I : 𝕜) • x + y) using 2 linear_combination (norm := module) -hI' • x have h₂ : ‖(I : 𝕜) • y + x‖ = ‖(I : 𝕜) • x - y‖ := by convert (I_smul ((I : 𝕜) • y + x)).symm using 2 linear_combination (norm := module) -hI' • y rw [h₁, h₂] ring variable [InnerProductSpaceable E] private theorem add_left_aux1 (x y z : E) : ‖2 • x + y‖ * ‖2 • x + y‖ + ‖2 • z + y‖ * ‖2 • z + y‖ = 2 * (‖x + y + z‖ * ‖x + y + z‖ + ‖x - z‖ * ‖x - z‖) := by convert parallelogram_identity (x + y + z) (x - z) using 4 <;> abel
private theorem add_left_aux2 (x y z : E) : ‖2 • x + y‖ * ‖2 • x + y‖ + ‖y - 2 • z‖ * ‖y - 2 • z‖ = 2 * (‖x + y - z‖ * ‖x + y - z‖ + ‖x + z‖ * ‖x + z‖) := by convert parallelogram_identity (x + y - z) (x + z) using 4 <;> abel private theorem add_left_aux3 (y z : E) : ‖2 • z + y‖ * ‖2 • z + y‖ + ‖y‖ * ‖y‖ = 2 * (‖y + z‖ * ‖y + z‖ + ‖z‖ * ‖z‖) := by convert parallelogram_identity (y + z) z using 4 <;> abel private theorem add_left_aux4 (y z : E) : ‖y‖ * ‖y‖ + ‖y - 2 • z‖ * ‖y - 2 • z‖ = 2 * (‖y - z‖ * ‖y - z‖ + ‖z‖ * ‖z‖) := by convert parallelogram_identity (y - z) z using 4 <;> abel variable (𝕜) private theorem add_left_aux5 (x y z : E) : ‖(I : 𝕜) • (2 • x + y)‖ * ‖(I : 𝕜) • (2 • x + y)‖ + ‖(I : 𝕜) • y + 2 • z‖ * ‖(I : 𝕜) • y + 2 • z‖ = 2 * (‖(I : 𝕜) • (x + y) + z‖ * ‖(I : 𝕜) • (x + y) + z‖ + ‖(I : 𝕜) • x - z‖ * ‖(I : 𝕜) • x - z‖) := by convert parallelogram_identity ((I : 𝕜) • (x + y) + z) ((I : 𝕜) • x - z) using 4 <;> module private theorem add_left_aux6 (x y z : E) : (‖(I : 𝕜) • (2 • x + y)‖ * ‖(I : 𝕜) • (2 • x + y)‖ +
Mathlib/Analysis/InnerProductSpace/OfNorm.lean
138
160
/- Copyright (c) 2022 Mario Carneiro, Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Heather Macbeth, Yaël Dillies -/ import Mathlib.Tactic.NormNum.Core import Mathlib.Tactic.HaveI import Mathlib.Algebra.Order.Invertible import Mathlib.Algebra.Order.Ring.Cast import Mathlib.Control.Basic import Mathlib.Data.Nat.Cast.Basic import Qq /-! ## `positivity` core functionality This file sets up the `positivity` tactic and the `@[positivity]` attribute, which allow for plugging in new positivity functionality around a positivity-based driver. The actual behavior is in `@[positivity]`-tagged definitions in `Tactic.Positivity.Basic` and elsewhere. -/ open Lean open Lean.Meta Qq Lean.Elab Term /-- Attribute for identifying `positivity` extensions. -/ syntax (name := positivity) "positivity " term,+ : attr lemma ne_of_ne_of_eq' {α : Sort*} {a c b : α} (hab : (a : α) ≠ c) (hbc : a = b) : b ≠ c := hbc ▸ hab namespace Mathlib.Meta.Positivity variable {u : Level} {α : Q(Type u)} (zα : Q(Zero $α)) (pα : Q(PartialOrder $α)) /-- The result of `positivity` running on an expression `e` of type `α`. -/ inductive Strictness (e : Q($α)) where | positive (pf : Q(0 < $e)) | nonnegative (pf : Q(0 ≤ $e)) | nonzero (pf : Q($e ≠ 0)) | none deriving Repr /-- Gives a generic description of the `positivity` result. -/ def Strictness.toString {e : Q($α)} : Strictness zα pα e → String | positive _ => "positive" | nonnegative _ => "nonnegative" | nonzero _ => "nonzero" | none => "none" /-- Extract a proof that `e` is nonnegative, if possible, from `Strictness` information about `e`. -/ def Strictness.toNonneg {e} : Strictness zα pα e → Option Q(0 ≤ $e) | .positive pf => some q(le_of_lt $pf) | .nonnegative pf => some pf | _ => .none /-- Extract a proof that `e` is nonzero, if possible, from `Strictness` information about `e`. -/ def Strictness.toNonzero {e} : Strictness zα pα e → Option Q($e ≠ 0) | .positive pf => some q(ne_of_gt $pf) | .nonzero pf => some pf | _ => .none /-- An extension for `positivity`. -/ structure PositivityExt where /-- Attempts to prove an expression `e : α` is `>0`, `≥0`, or `≠0`. -/ eval {u : Level} {α : Q(Type u)} (zα : Q(Zero $α)) (pα : Q(PartialOrder $α)) (e : Q($α)) : MetaM (Strictness zα pα e) /-- Read a `positivity` extension from a declaration of the right type. -/ def mkPositivityExt (n : Name) : ImportM PositivityExt := do let { env, opts, .. } ← read IO.ofExcept <| unsafe env.evalConstCheck PositivityExt opts ``PositivityExt n /-- Each `positivity` extension is labelled with a collection of patterns which determine the expressions to which it should be applied. -/ abbrev Entry := Array (Array DiscrTree.Key) × Name /-- Environment extensions for `positivity` declarations -/ initialize positivityExt : PersistentEnvExtension Entry (Entry × PositivityExt) (List Entry × DiscrTree PositivityExt) ← -- we only need this to deduplicate entries in the DiscrTree have : BEq PositivityExt := ⟨fun _ _ => false⟩ let insert kss v dt := kss.foldl (fun dt ks => dt.insertCore ks v) dt registerPersistentEnvExtension { mkInitial := pure ([], {}) addImportedFn := fun s => do let dt ← s.foldlM (init := {}) fun dt s => s.foldlM (init := dt) fun dt (kss, n) => do pure (insert kss (← mkPositivityExt n) dt) pure ([], dt) addEntryFn := fun (entries, s) ((kss, n), ext) => ((kss, n) :: entries, insert kss ext s) exportEntriesFn := fun s => s.1.reverse.toArray } initialize registerBuiltinAttribute { name := `positivity descr := "adds a positivity extension" applicationTime := .afterCompilation add := fun declName stx kind => match stx with | `(attr| positivity $es,*) => do unless kind == AttributeKind.global do throwError "invalid attribute 'positivity', must be global" let env ← getEnv unless (env.getModuleIdxFor? declName).isNone do throwError "invalid attribute 'positivity', declaration is in an imported module" if (IR.getSorryDep env declName).isSome then return -- ignore in progress definitions let ext ← mkPositivityExt declName let keys ← MetaM.run' <| es.getElems.mapM fun stx => do let e ← TermElabM.run' <| withSaveInfoContext <| withAutoBoundImplicit <| withReader ({ · with ignoreTCFailures := true }) do let e ← elabTerm stx none let (_, _, e) ← lambdaMetaTelescope (← mkLambdaFVars (← getLCtx).getFVars e) return e DiscrTree.mkPath e setEnv <| positivityExt.addEntry env ((keys, declName), ext) | _ => throwUnsupportedSyntax } variable {A : Type*} {e : A} lemma lt_of_le_of_ne' {a b : A} [PartialOrder A] : (a : A) ≤ b → b ≠ a → a < b := fun h₁ h₂ => lt_of_le_of_ne h₁ h₂.symm lemma pos_of_isNat {n : ℕ} [Semiring A] [PartialOrder A] [IsOrderedRing A] [Nontrivial A] (h : NormNum.IsNat e n) (w : Nat.ble 1 n = true) : 0 < (e : A) := by rw [NormNum.IsNat.to_eq h rfl] apply Nat.cast_pos.2 simpa using w lemma nonneg_of_isNat {n : ℕ} [Semiring A] [PartialOrder A] [IsOrderedRing A] (h : NormNum.IsNat e n) : 0 ≤ (e : A) := by rw [NormNum.IsNat.to_eq h rfl] exact Nat.cast_nonneg n lemma nz_of_isNegNat {n : ℕ} [Ring A] [PartialOrder A] [IsStrictOrderedRing A] (h : NormNum.IsInt e (.negOfNat n)) (w : Nat.ble 1 n = true) : (e : A) ≠ 0 := by rw [NormNum.IsInt.neg_to_eq h rfl] simp only [ne_eq, neg_eq_zero] apply ne_of_gt simpa using w lemma pos_of_isRat {n : ℤ} {d : ℕ} [Ring A] [LinearOrder A] [IsStrictOrderedRing A] : (NormNum.IsRat e n d) → (decide (0 < n)) → ((0 : A) < (e : A)) | ⟨inv, eq⟩, h => by have pos_invOf_d : (0 < ⅟ (d : A)) := pos_invOf_of_invertible_cast d have pos_n : (0 < (n : A)) := Int.cast_pos (n := n) |>.2 (of_decide_eq_true h) rw [eq] exact mul_pos pos_n pos_invOf_d lemma nonneg_of_isRat {n : ℤ} {d : ℕ} [Ring A] [LinearOrder A] : (NormNum.IsRat e n d) → (decide (n = 0)) → (0 ≤ (e : A))
| ⟨inv, eq⟩, h => by rw [eq, of_decide_eq_true h]; simp lemma nz_of_isRat {n : ℤ} {d : ℕ} [Ring A] [LinearOrder A] [IsStrictOrderedRing A] :
Mathlib/Tactic/Positivity/Core.lean
151
153
/- Copyright (c) 2022 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Oleksandr Manzyuk -/ import Mathlib.CategoryTheory.Bicategory.Basic import Mathlib.CategoryTheory.Monoidal.Mon_ import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Equalizers /-! # The category of bimodule objects over a pair of monoid objects. -/ universe v₁ v₂ u₁ u₂ open CategoryTheory open CategoryTheory.MonoidalCategory variable {C : Type u₁} [Category.{v₁} C] [MonoidalCategory.{v₁} C] section open CategoryTheory.Limits variable [HasCoequalizers C] section variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] theorem id_tensor_π_preserves_coequalizer_inv_desc {W X Y Z : C} (f g : X ⟶ Y) (h : Z ⊗ Y ⟶ W) (wh : (Z ◁ f) ≫ h = (Z ◁ g) ≫ h) : (Z ◁ coequalizer.π f g) ≫ (PreservesCoequalizer.iso (tensorLeft Z) f g).inv ≫ coequalizer.desc h wh = h := map_π_preserves_coequalizer_inv_desc (tensorLeft Z) f g h wh theorem id_tensor_π_preserves_coequalizer_inv_colimMap_desc {X Y Z X' Y' Z' : C} (f g : X ⟶ Y) (f' g' : X' ⟶ Y') (p : Z ⊗ X ⟶ X') (q : Z ⊗ Y ⟶ Y') (wf : (Z ◁ f) ≫ q = p ≫ f') (wg : (Z ◁ g) ≫ q = p ≫ g') (h : Y' ⟶ Z') (wh : f' ≫ h = g' ≫ h) : (Z ◁ coequalizer.π f g) ≫ (PreservesCoequalizer.iso (tensorLeft Z) f g).inv ≫ colimMap (parallelPairHom (Z ◁ f) (Z ◁ g) f' g' p q wf wg) ≫ coequalizer.desc h wh = q ≫ h := map_π_preserves_coequalizer_inv_colimMap_desc (tensorLeft Z) f g f' g' p q wf wg h wh end section variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] theorem π_tensor_id_preserves_coequalizer_inv_desc {W X Y Z : C} (f g : X ⟶ Y) (h : Y ⊗ Z ⟶ W) (wh : (f ▷ Z) ≫ h = (g ▷ Z) ≫ h) : (coequalizer.π f g ▷ Z) ≫ (PreservesCoequalizer.iso (tensorRight Z) f g).inv ≫ coequalizer.desc h wh = h := map_π_preserves_coequalizer_inv_desc (tensorRight Z) f g h wh theorem π_tensor_id_preserves_coequalizer_inv_colimMap_desc {X Y Z X' Y' Z' : C} (f g : X ⟶ Y) (f' g' : X' ⟶ Y') (p : X ⊗ Z ⟶ X') (q : Y ⊗ Z ⟶ Y') (wf : (f ▷ Z) ≫ q = p ≫ f') (wg : (g ▷ Z) ≫ q = p ≫ g') (h : Y' ⟶ Z') (wh : f' ≫ h = g' ≫ h) : (coequalizer.π f g ▷ Z) ≫ (PreservesCoequalizer.iso (tensorRight Z) f g).inv ≫ colimMap (parallelPairHom (f ▷ Z) (g ▷ Z) f' g' p q wf wg) ≫ coequalizer.desc h wh = q ≫ h := map_π_preserves_coequalizer_inv_colimMap_desc (tensorRight Z) f g f' g' p q wf wg h wh end end /-- A bimodule object for a pair of monoid objects, all internal to some monoidal category. -/ structure Bimod (A B : Mon_ C) where /-- The underlying monoidal category -/ X : C /-- The left action of this bimodule object -/ actLeft : A.X ⊗ X ⟶ X one_actLeft : (A.one ▷ X) ≫ actLeft = (λ_ X).hom := by aesop_cat left_assoc : (A.mul ▷ X) ≫ actLeft = (α_ A.X A.X X).hom ≫ (A.X ◁ actLeft) ≫ actLeft := by aesop_cat /-- The right action of this bimodule object -/ actRight : X ⊗ B.X ⟶ X actRight_one : (X ◁ B.one) ≫ actRight = (ρ_ X).hom := by aesop_cat right_assoc : (X ◁ B.mul) ≫ actRight = (α_ X B.X B.X).inv ≫ (actRight ▷ B.X) ≫ actRight := by aesop_cat middle_assoc : (actLeft ▷ B.X) ≫ actRight = (α_ A.X X B.X).hom ≫ (A.X ◁ actRight) ≫ actLeft := by aesop_cat attribute [reassoc (attr := simp)] Bimod.one_actLeft Bimod.actRight_one Bimod.left_assoc Bimod.right_assoc Bimod.middle_assoc namespace Bimod variable {A B : Mon_ C} (M : Bimod A B) /-- A morphism of bimodule objects. -/ @[ext] structure Hom (M N : Bimod A B) where /-- The morphism between `M`'s monoidal category and `N`'s monoidal category -/ hom : M.X ⟶ N.X left_act_hom : M.actLeft ≫ hom = (A.X ◁ hom) ≫ N.actLeft := by aesop_cat right_act_hom : M.actRight ≫ hom = (hom ▷ B.X) ≫ N.actRight := by aesop_cat attribute [reassoc (attr := simp)] Hom.left_act_hom Hom.right_act_hom /-- The identity morphism on a bimodule object. -/ @[simps] def id' (M : Bimod A B) : Hom M M where hom := 𝟙 M.X instance homInhabited (M : Bimod A B) : Inhabited (Hom M M) := ⟨id' M⟩ /-- Composition of bimodule object morphisms. -/ @[simps] def comp {M N O : Bimod A B} (f : Hom M N) (g : Hom N O) : Hom M O where hom := f.hom ≫ g.hom instance : Category (Bimod A B) where Hom M N := Hom M N id := id' comp f g := comp f g @[ext] lemma hom_ext {M N : Bimod A B} (f g : M ⟶ N) (h : f.hom = g.hom) : f = g := Hom.ext h @[simp] theorem id_hom' (M : Bimod A B) : (𝟙 M : Hom M M).hom = 𝟙 M.X := rfl @[simp] theorem comp_hom' {M N K : Bimod A B} (f : M ⟶ N) (g : N ⟶ K) : (f ≫ g : Hom M K).hom = f.hom ≫ g.hom := rfl /-- Construct an isomorphism of bimodules by giving an isomorphism between the underlying objects and checking compatibility with left and right actions only in the forward direction. -/ @[simps] def isoOfIso {X Y : Mon_ C} {P Q : Bimod X Y} (f : P.X ≅ Q.X) (f_left_act_hom : P.actLeft ≫ f.hom = (X.X ◁ f.hom) ≫ Q.actLeft) (f_right_act_hom : P.actRight ≫ f.hom = (f.hom ▷ Y.X) ≫ Q.actRight) : P ≅ Q where hom := { hom := f.hom } inv := { hom := f.inv left_act_hom := by rw [← cancel_mono f.hom, Category.assoc, Category.assoc, Iso.inv_hom_id, Category.comp_id, f_left_act_hom, ← Category.assoc, ← MonoidalCategory.whiskerLeft_comp, Iso.inv_hom_id, MonoidalCategory.whiskerLeft_id, Category.id_comp] right_act_hom := by rw [← cancel_mono f.hom, Category.assoc, Category.assoc, Iso.inv_hom_id, Category.comp_id, f_right_act_hom, ← Category.assoc, ← comp_whiskerRight, Iso.inv_hom_id, MonoidalCategory.id_whiskerRight, Category.id_comp] } hom_inv_id := by ext; dsimp; rw [Iso.hom_inv_id] inv_hom_id := by ext; dsimp; rw [Iso.inv_hom_id] variable (A) /-- A monoid object as a bimodule over itself. -/ @[simps] def regular : Bimod A A where X := A.X actLeft := A.mul actRight := A.mul instance : Inhabited (Bimod A A) := ⟨regular A⟩ /-- The forgetful functor from bimodule objects to the ambient category. -/ def forget : Bimod A B ⥤ C where obj A := A.X map f := f.hom open CategoryTheory.Limits variable [HasCoequalizers C] namespace TensorBimod variable {R S T : Mon_ C} (P : Bimod R S) (Q : Bimod S T) /-- The underlying object of the tensor product of two bimodules. -/ noncomputable def X : C := coequalizer (P.actRight ▷ Q.X) ((α_ _ _ _).hom ≫ (P.X ◁ Q.actLeft)) section variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] /-- Left action for the tensor product of two bimodules. -/ noncomputable def actLeft : R.X ⊗ X P Q ⟶ X P Q := (PreservesCoequalizer.iso (tensorLeft R.X) _ _).inv ≫ colimMap (parallelPairHom _ _ _ _ ((α_ _ _ _).inv ≫ ((α_ _ _ _).inv ▷ _) ≫ (P.actLeft ▷ S.X ▷ Q.X)) ((α_ _ _ _).inv ≫ (P.actLeft ▷ Q.X)) (by dsimp simp only [Category.assoc] slice_lhs 1 2 => rw [associator_inv_naturality_middle] slice_rhs 3 4 => rw [← comp_whiskerRight, middle_assoc, comp_whiskerRight] monoidal) (by dsimp slice_lhs 1 1 => rw [MonoidalCategory.whiskerLeft_comp] slice_lhs 2 3 => rw [associator_inv_naturality_right] slice_lhs 3 4 => rw [whisker_exchange] monoidal)) theorem whiskerLeft_π_actLeft : (R.X ◁ coequalizer.π _ _) ≫ actLeft P Q = (α_ _ _ _).inv ≫ (P.actLeft ▷ Q.X) ≫ coequalizer.π _ _ := by erw [map_π_preserves_coequalizer_inv_colimMap (tensorLeft _)] simp only [Category.assoc] theorem one_act_left' : (R.one ▷ _) ≫ actLeft P Q = (λ_ _).hom := by refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp [X] -- Porting note: had to replace `rw` by `erw` slice_lhs 1 2 => erw [whisker_exchange] slice_lhs 2 3 => rw [whiskerLeft_π_actLeft] slice_lhs 1 2 => rw [associator_inv_naturality_left] slice_lhs 2 3 => rw [← comp_whiskerRight, one_actLeft] slice_rhs 1 2 => rw [leftUnitor_naturality] monoidal theorem left_assoc' : (R.mul ▷ _) ≫ actLeft P Q = (α_ R.X R.X _).hom ≫ (R.X ◁ actLeft P Q) ≫ actLeft P Q := by refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp [X] slice_lhs 1 2 => rw [whisker_exchange] slice_lhs 2 3 => rw [whiskerLeft_π_actLeft] slice_lhs 1 2 => rw [associator_inv_naturality_left] slice_lhs 2 3 => rw [← comp_whiskerRight, left_assoc, comp_whiskerRight, comp_whiskerRight] slice_rhs 1 2 => rw [associator_naturality_right] slice_rhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, whiskerLeft_π_actLeft, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] slice_rhs 4 5 => rw [whiskerLeft_π_actLeft] slice_rhs 3 4 => rw [associator_inv_naturality_middle] monoidal end section variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] /-- Right action for the tensor product of two bimodules. -/ noncomputable def actRight : X P Q ⊗ T.X ⟶ X P Q := (PreservesCoequalizer.iso (tensorRight T.X) _ _).inv ≫ colimMap (parallelPairHom _ _ _ _ ((α_ _ _ _).hom ≫ (α_ _ _ _).hom ≫ (P.X ◁ S.X ◁ Q.actRight) ≫ (α_ _ _ _).inv) ((α_ _ _ _).hom ≫ (P.X ◁ Q.actRight)) (by dsimp slice_lhs 1 2 => rw [associator_naturality_left] slice_lhs 2 3 => rw [← whisker_exchange] simp) (by dsimp simp only [comp_whiskerRight, whisker_assoc, Category.assoc, Iso.inv_hom_id_assoc] slice_lhs 3 4 => rw [← MonoidalCategory.whiskerLeft_comp, middle_assoc, MonoidalCategory.whiskerLeft_comp] simp)) theorem π_tensor_id_actRight : (coequalizer.π _ _ ▷ T.X) ≫ actRight P Q = (α_ _ _ _).hom ≫ (P.X ◁ Q.actRight) ≫ coequalizer.π _ _ := by erw [map_π_preserves_coequalizer_inv_colimMap (tensorRight _)] simp only [Category.assoc] theorem actRight_one' : (_ ◁ T.one) ≫ actRight P Q = (ρ_ _).hom := by refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp [X] -- Porting note: had to replace `rw` by `erw` slice_lhs 1 2 =>erw [← whisker_exchange] slice_lhs 2 3 => rw [π_tensor_id_actRight] slice_lhs 1 2 => rw [associator_naturality_right] slice_lhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, actRight_one] simp theorem right_assoc' : (_ ◁ T.mul) ≫ actRight P Q = (α_ _ T.X T.X).inv ≫ (actRight P Q ▷ T.X) ≫ actRight P Q := by refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp [X] -- Porting note: had to replace some `rw` by `erw` slice_lhs 1 2 => rw [← whisker_exchange] slice_lhs 2 3 => rw [π_tensor_id_actRight] slice_lhs 1 2 => rw [associator_naturality_right] slice_lhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, right_assoc, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] slice_rhs 1 2 => rw [associator_inv_naturality_left] slice_rhs 2 3 => rw [← comp_whiskerRight, π_tensor_id_actRight, comp_whiskerRight, comp_whiskerRight] slice_rhs 4 5 => rw [π_tensor_id_actRight] simp end section variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] theorem middle_assoc' : (actLeft P Q ▷ T.X) ≫ actRight P Q = (α_ R.X _ T.X).hom ≫ (R.X ◁ actRight P Q) ≫ actLeft P Q := by refine (cancel_epi ((tensorLeft _ ⋙ tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp [X] slice_lhs 1 2 => rw [← comp_whiskerRight, whiskerLeft_π_actLeft, comp_whiskerRight, comp_whiskerRight] slice_lhs 3 4 => rw [π_tensor_id_actRight] slice_lhs 2 3 => rw [associator_naturality_left] -- Porting note: had to replace `rw` by `erw` slice_rhs 1 2 => rw [associator_naturality_middle] slice_rhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, π_tensor_id_actRight, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] slice_rhs 4 5 => rw [whiskerLeft_π_actLeft] slice_rhs 3 4 => rw [associator_inv_naturality_right] slice_rhs 4 5 => rw [whisker_exchange] simp end end TensorBimod section variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] /-- Tensor product of two bimodule objects as a bimodule object. -/ @[simps] noncomputable def tensorBimod {X Y Z : Mon_ C} (M : Bimod X Y) (N : Bimod Y Z) : Bimod X Z where X := TensorBimod.X M N actLeft := TensorBimod.actLeft M N actRight := TensorBimod.actRight M N one_actLeft := TensorBimod.one_act_left' M N actRight_one := TensorBimod.actRight_one' M N left_assoc := TensorBimod.left_assoc' M N right_assoc := TensorBimod.right_assoc' M N middle_assoc := TensorBimod.middle_assoc' M N /-- Left whiskering for morphisms of bimodule objects. -/ @[simps] noncomputable def whiskerLeft {X Y Z : Mon_ C} (M : Bimod X Y) {N₁ N₂ : Bimod Y Z} (f : N₁ ⟶ N₂) : M.tensorBimod N₁ ⟶ M.tensorBimod N₂ where hom := colimMap (parallelPairHom _ _ _ _ (_ ◁ f.hom) (_ ◁ f.hom) (by rw [whisker_exchange]) (by simp only [Category.assoc, tensor_whiskerLeft, Iso.inv_hom_id_assoc, Iso.cancel_iso_hom_left] slice_lhs 1 2 => rw [← MonoidalCategory.whiskerLeft_comp, Hom.left_act_hom] simp)) left_act_hom := by refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 2 => rw [TensorBimod.whiskerLeft_π_actLeft] slice_lhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_rhs 1 2 => rw [← MonoidalCategory.whiskerLeft_comp, ι_colimMap, parallelPairHom_app_one, MonoidalCategory.whiskerLeft_comp] slice_rhs 2 3 => rw [TensorBimod.whiskerLeft_π_actLeft] slice_rhs 1 2 => rw [associator_inv_naturality_right] slice_rhs 2 3 => rw [whisker_exchange] simp right_act_hom := by refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 2 => rw [TensorBimod.π_tensor_id_actRight] slice_lhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_lhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, Hom.right_act_hom] slice_rhs 1 2 => rw [← comp_whiskerRight, ι_colimMap, parallelPairHom_app_one, comp_whiskerRight] slice_rhs 2 3 => rw [TensorBimod.π_tensor_id_actRight] simp /-- Right whiskering for morphisms of bimodule objects. -/ @[simps] noncomputable def whiskerRight {X Y Z : Mon_ C} {M₁ M₂ : Bimod X Y} (f : M₁ ⟶ M₂) (N : Bimod Y Z) : M₁.tensorBimod N ⟶ M₂.tensorBimod N where hom := colimMap (parallelPairHom _ _ _ _ (f.hom ▷ _ ▷ _) (f.hom ▷ _) (by rw [← comp_whiskerRight, Hom.right_act_hom, comp_whiskerRight]) (by slice_lhs 2 3 => rw [whisker_exchange] simp)) left_act_hom := by refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 2 => rw [TensorBimod.whiskerLeft_π_actLeft] slice_lhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_lhs 2 3 => rw [← comp_whiskerRight, Hom.left_act_hom] slice_rhs 1 2 => rw [← MonoidalCategory.whiskerLeft_comp, ι_colimMap, parallelPairHom_app_one, MonoidalCategory.whiskerLeft_comp] slice_rhs 2 3 => rw [TensorBimod.whiskerLeft_π_actLeft] slice_rhs 1 2 => rw [associator_inv_naturality_middle] simp right_act_hom := by refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 2 => rw [TensorBimod.π_tensor_id_actRight] slice_lhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_lhs 2 3 => rw [whisker_exchange] slice_rhs 1 2 => rw [← comp_whiskerRight, ι_colimMap, parallelPairHom_app_one, comp_whiskerRight] slice_rhs 2 3 => rw [TensorBimod.π_tensor_id_actRight] simp end namespace AssociatorBimod variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] variable {R S T U : Mon_ C} (P : Bimod R S) (Q : Bimod S T) (L : Bimod T U) /-- An auxiliary morphism for the definition of the underlying morphism of the forward component of the associator isomorphism. -/ noncomputable def homAux : (P.tensorBimod Q).X ⊗ L.X ⟶ (P.tensorBimod (Q.tensorBimod L)).X := (PreservesCoequalizer.iso (tensorRight L.X) _ _).inv ≫ coequalizer.desc ((α_ _ _ _).hom ≫ (P.X ◁ coequalizer.π _ _) ≫ coequalizer.π _ _) (by dsimp; dsimp [TensorBimod.X] slice_lhs 1 2 => rw [associator_naturality_left] slice_lhs 2 3 => rw [← whisker_exchange] slice_lhs 3 4 => rw [coequalizer.condition] slice_lhs 2 3 => rw [associator_naturality_right] slice_lhs 3 4 => rw [← MonoidalCategory.whiskerLeft_comp, TensorBimod.whiskerLeft_π_actLeft, MonoidalCategory.whiskerLeft_comp] simp) /-- The underlying morphism of the forward component of the associator isomorphism. -/ noncomputable def hom : ((P.tensorBimod Q).tensorBimod L).X ⟶ (P.tensorBimod (Q.tensorBimod L)).X := coequalizer.desc (homAux P Q L) (by dsimp [homAux] refine (cancel_epi ((tensorRight _ ⋙ tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp [TensorBimod.X] slice_lhs 1 2 => rw [← comp_whiskerRight, TensorBimod.π_tensor_id_actRight, comp_whiskerRight, comp_whiskerRight] slice_lhs 3 5 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_lhs 2 3 => rw [associator_naturality_middle] slice_lhs 3 4 => rw [← MonoidalCategory.whiskerLeft_comp, coequalizer.condition, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] slice_rhs 1 2 => rw [associator_naturality_left] slice_rhs 2 3 => rw [← whisker_exchange] slice_rhs 3 5 => rw [π_tensor_id_preserves_coequalizer_inv_desc] simp) theorem hom_left_act_hom' : ((P.tensorBimod Q).tensorBimod L).actLeft ≫ hom P Q L = (R.X ◁ hom P Q L) ≫ (P.tensorBimod (Q.tensorBimod L)).actLeft := by dsimp; dsimp [hom, homAux] refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ rw [tensorLeft_map] slice_lhs 1 2 => rw [TensorBimod.whiskerLeft_π_actLeft] slice_lhs 3 4 => rw [coequalizer.π_desc] slice_rhs 1 2 => rw [← MonoidalCategory.whiskerLeft_comp, coequalizer.π_desc, MonoidalCategory.whiskerLeft_comp] refine (cancel_epi ((tensorRight _ ⋙ tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp; dsimp [TensorBimod.X] slice_lhs 1 2 => rw [associator_inv_naturality_middle] slice_lhs 2 3 => rw [← comp_whiskerRight, TensorBimod.whiskerLeft_π_actLeft, comp_whiskerRight, comp_whiskerRight] slice_lhs 4 6 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_lhs 3 4 => rw [associator_naturality_left] slice_rhs 1 3 => rw [← MonoidalCategory.whiskerLeft_comp, ← MonoidalCategory.whiskerLeft_comp, π_tensor_id_preserves_coequalizer_inv_desc, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] slice_rhs 3 4 => erw [TensorBimod.whiskerLeft_π_actLeft P (Q.tensorBimod L)] slice_rhs 2 3 => erw [associator_inv_naturality_right] slice_rhs 3 4 => erw [whisker_exchange] monoidal theorem hom_right_act_hom' : ((P.tensorBimod Q).tensorBimod L).actRight ≫ hom P Q L = (hom P Q L ▷ U.X) ≫ (P.tensorBimod (Q.tensorBimod L)).actRight := by dsimp; dsimp [hom, homAux] refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ rw [tensorRight_map] slice_lhs 1 2 => rw [TensorBimod.π_tensor_id_actRight] slice_lhs 3 4 => rw [coequalizer.π_desc] slice_rhs 1 2 => rw [← comp_whiskerRight, coequalizer.π_desc, comp_whiskerRight] refine (cancel_epi ((tensorRight _ ⋙ tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp; dsimp [TensorBimod.X] slice_lhs 1 2 => rw [associator_naturality_left] slice_lhs 2 3 => rw [← whisker_exchange] slice_lhs 3 5 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_lhs 2 3 => rw [associator_naturality_right] slice_rhs 1 3 => rw [← comp_whiskerRight, ← comp_whiskerRight, π_tensor_id_preserves_coequalizer_inv_desc, comp_whiskerRight, comp_whiskerRight] slice_rhs 3 4 => erw [TensorBimod.π_tensor_id_actRight P (Q.tensorBimod L)] slice_rhs 2 3 => erw [associator_naturality_middle] dsimp slice_rhs 3 4 => rw [← MonoidalCategory.whiskerLeft_comp, TensorBimod.π_tensor_id_actRight, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] monoidal /-- An auxiliary morphism for the definition of the underlying morphism of the inverse component of the associator isomorphism. -/ noncomputable def invAux : P.X ⊗ (Q.tensorBimod L).X ⟶ ((P.tensorBimod Q).tensorBimod L).X := (PreservesCoequalizer.iso (tensorLeft P.X) _ _).inv ≫ coequalizer.desc ((α_ _ _ _).inv ≫ (coequalizer.π _ _ ▷ L.X) ≫ coequalizer.π _ _) (by dsimp; dsimp [TensorBimod.X] slice_lhs 1 2 => rw [associator_inv_naturality_middle] rw [← Iso.inv_hom_id_assoc (α_ _ _ _) (P.X ◁ Q.actRight), comp_whiskerRight] slice_lhs 3 4 => rw [← comp_whiskerRight, Category.assoc, ← TensorBimod.π_tensor_id_actRight, comp_whiskerRight] slice_lhs 4 5 => rw [coequalizer.condition] slice_lhs 3 4 => rw [associator_naturality_left] slice_rhs 1 2 => rw [MonoidalCategory.whiskerLeft_comp] slice_rhs 2 3 => rw [associator_inv_naturality_right] slice_rhs 3 4 => rw [whisker_exchange] monoidal) /-- The underlying morphism of the inverse component of the associator isomorphism. -/ noncomputable def inv : (P.tensorBimod (Q.tensorBimod L)).X ⟶ ((P.tensorBimod Q).tensorBimod L).X := coequalizer.desc (invAux P Q L) (by dsimp [invAux] refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp [TensorBimod.X] slice_lhs 1 2 => rw [whisker_exchange] slice_lhs 2 4 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_lhs 1 2 => rw [associator_inv_naturality_left] slice_lhs 2 3 => rw [← comp_whiskerRight, coequalizer.condition, comp_whiskerRight, comp_whiskerRight] slice_rhs 1 2 => rw [associator_naturality_right] slice_rhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, TensorBimod.whiskerLeft_π_actLeft, MonoidalCategory.whiskerLeft_comp, MonoidalCategory.whiskerLeft_comp] slice_rhs 4 6 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_rhs 3 4 => rw [associator_inv_naturality_middle] monoidal) theorem hom_inv_id : hom P Q L ≫ inv P Q L = 𝟙 _ := by dsimp [hom, homAux, inv, invAux] apply coequalizer.hom_ext slice_lhs 1 2 => rw [coequalizer.π_desc] refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ rw [tensorRight_map] slice_lhs 1 3 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_lhs 3 4 => rw [coequalizer.π_desc] slice_lhs 2 4 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_lhs 1 3 => rw [Iso.hom_inv_id_assoc] dsimp only [TensorBimod.X] slice_rhs 2 3 => rw [Category.comp_id] rfl theorem inv_hom_id : inv P Q L ≫ hom P Q L = 𝟙 _ := by dsimp [hom, homAux, inv, invAux] apply coequalizer.hom_ext slice_lhs 1 2 => rw [coequalizer.π_desc] refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ rw [tensorLeft_map] slice_lhs 1 3 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_lhs 3 4 => rw [coequalizer.π_desc] slice_lhs 2 4 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_lhs 1 3 => rw [Iso.inv_hom_id_assoc] dsimp only [TensorBimod.X] slice_rhs 2 3 => rw [Category.comp_id] rfl end AssociatorBimod namespace LeftUnitorBimod variable {R S : Mon_ C} (P : Bimod R S) /-- The underlying morphism of the forward component of the left unitor isomorphism. -/ noncomputable def hom : TensorBimod.X (regular R) P ⟶ P.X := coequalizer.desc P.actLeft (by dsimp; rw [Category.assoc, left_assoc]) /-- The underlying morphism of the inverse component of the left unitor isomorphism. -/ noncomputable def inv : P.X ⟶ TensorBimod.X (regular R) P := (λ_ P.X).inv ≫ (R.one ▷ _) ≫ coequalizer.π _ _ theorem hom_inv_id : hom P ≫ inv P = 𝟙 _ := by dsimp only [hom, inv, TensorBimod.X] ext; dsimp slice_lhs 1 2 => rw [coequalizer.π_desc] slice_lhs 1 2 => rw [leftUnitor_inv_naturality] slice_lhs 2 3 => rw [whisker_exchange] slice_lhs 3 3 => rw [← Iso.inv_hom_id_assoc (α_ R.X R.X P.X) (R.X ◁ P.actLeft)] slice_lhs 4 6 => rw [← Category.assoc, ← coequalizer.condition] slice_lhs 2 3 => rw [associator_inv_naturality_left] slice_lhs 3 4 => rw [← comp_whiskerRight, Mon_.one_mul] slice_rhs 1 2 => rw [Category.comp_id] monoidal theorem inv_hom_id : inv P ≫ hom P = 𝟙 _ := by dsimp [hom, inv] slice_lhs 3 4 => rw [coequalizer.π_desc] rw [one_actLeft, Iso.inv_hom_id] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] theorem hom_left_act_hom' : ((regular R).tensorBimod P).actLeft ≫ hom P = (R.X ◁ hom P) ≫ P.actLeft := by dsimp; dsimp [hom, TensorBimod.actLeft, regular] refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 4 => rw [id_tensor_π_preserves_coequalizer_inv_colimMap_desc] slice_lhs 2 3 => rw [left_assoc] slice_rhs 1 2 => rw [← MonoidalCategory.whiskerLeft_comp, coequalizer.π_desc] rw [Iso.inv_hom_id_assoc] theorem hom_right_act_hom' : ((regular R).tensorBimod P).actRight ≫ hom P = (hom P ▷ S.X) ≫ P.actRight := by dsimp; dsimp [hom, TensorBimod.actRight, regular] refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 4 => rw [π_tensor_id_preserves_coequalizer_inv_colimMap_desc] slice_rhs 1 2 => rw [← comp_whiskerRight, coequalizer.π_desc] slice_rhs 1 2 => rw [middle_assoc] simp only [Category.assoc] end LeftUnitorBimod namespace RightUnitorBimod variable {R S : Mon_ C} (P : Bimod R S) /-- The underlying morphism of the forward component of the right unitor isomorphism. -/ noncomputable def hom : TensorBimod.X P (regular S) ⟶ P.X := coequalizer.desc P.actRight (by dsimp; rw [Category.assoc, right_assoc, Iso.hom_inv_id_assoc]) /-- The underlying morphism of the inverse component of the right unitor isomorphism. -/ noncomputable def inv : P.X ⟶ TensorBimod.X P (regular S) := (ρ_ P.X).inv ≫ (_ ◁ S.one) ≫ coequalizer.π _ _ theorem hom_inv_id : hom P ≫ inv P = 𝟙 _ := by dsimp only [hom, inv, TensorBimod.X] ext; dsimp slice_lhs 1 2 => rw [coequalizer.π_desc] slice_lhs 1 2 => rw [rightUnitor_inv_naturality] slice_lhs 2 3 => rw [← whisker_exchange] slice_lhs 3 4 => rw [coequalizer.condition] slice_lhs 2 3 => rw [associator_naturality_right] slice_lhs 3 4 => rw [← MonoidalCategory.whiskerLeft_comp, Mon_.mul_one] slice_rhs 1 2 => rw [Category.comp_id] monoidal theorem inv_hom_id : inv P ≫ hom P = 𝟙 _ := by dsimp [hom, inv] slice_lhs 3 4 => rw [coequalizer.π_desc] rw [actRight_one, Iso.inv_hom_id] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] theorem hom_left_act_hom' : (P.tensorBimod (regular S)).actLeft ≫ hom P = (R.X ◁ hom P) ≫ P.actLeft := by dsimp; dsimp [hom, TensorBimod.actLeft, regular] refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 4 => rw [id_tensor_π_preserves_coequalizer_inv_colimMap_desc] slice_lhs 2 3 => rw [middle_assoc] slice_rhs 1 2 => rw [← MonoidalCategory.whiskerLeft_comp, coequalizer.π_desc] rw [Iso.inv_hom_id_assoc] theorem hom_right_act_hom' : (P.tensorBimod (regular S)).actRight ≫ hom P = (hom P ▷ S.X) ≫ P.actRight := by dsimp; dsimp [hom, TensorBimod.actRight, regular] refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ dsimp slice_lhs 1 4 => rw [π_tensor_id_preserves_coequalizer_inv_colimMap_desc] slice_lhs 2 3 => rw [right_assoc] slice_rhs 1 2 => rw [← comp_whiskerRight, coequalizer.π_desc] rw [Iso.hom_inv_id_assoc] end RightUnitorBimod variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorLeft X)] variable [∀ X : C, PreservesColimitsOfSize.{0, 0} (tensorRight X)] /-- The associator as a bimodule isomorphism. -/ noncomputable def associatorBimod {W X Y Z : Mon_ C} (L : Bimod W X) (M : Bimod X Y) (N : Bimod Y Z) : (L.tensorBimod M).tensorBimod N ≅ L.tensorBimod (M.tensorBimod N) := isoOfIso { hom := AssociatorBimod.hom L M N inv := AssociatorBimod.inv L M N hom_inv_id := AssociatorBimod.hom_inv_id L M N inv_hom_id := AssociatorBimod.inv_hom_id L M N } (AssociatorBimod.hom_left_act_hom' L M N) (AssociatorBimod.hom_right_act_hom' L M N) /-- The left unitor as a bimodule isomorphism. -/ noncomputable def leftUnitorBimod {X Y : Mon_ C} (M : Bimod X Y) : (regular X).tensorBimod M ≅ M := isoOfIso { hom := LeftUnitorBimod.hom M inv := LeftUnitorBimod.inv M hom_inv_id := LeftUnitorBimod.hom_inv_id M inv_hom_id := LeftUnitorBimod.inv_hom_id M } (LeftUnitorBimod.hom_left_act_hom' M) (LeftUnitorBimod.hom_right_act_hom' M) /-- The right unitor as a bimodule isomorphism. -/ noncomputable def rightUnitorBimod {X Y : Mon_ C} (M : Bimod X Y) : M.tensorBimod (regular Y) ≅ M := isoOfIso { hom := RightUnitorBimod.hom M inv := RightUnitorBimod.inv M hom_inv_id := RightUnitorBimod.hom_inv_id M inv_hom_id := RightUnitorBimod.inv_hom_id M } (RightUnitorBimod.hom_left_act_hom' M) (RightUnitorBimod.hom_right_act_hom' M) theorem whiskerLeft_id_bimod {X Y Z : Mon_ C} {M : Bimod X Y} {N : Bimod Y Z} : whiskerLeft M (𝟙 N) = 𝟙 (M.tensorBimod N) := by ext apply Limits.coequalizer.hom_ext dsimp only [tensorBimod_X, whiskerLeft_hom, id_hom'] simp only [MonoidalCategory.whiskerLeft_id, ι_colimMap, parallelPair_obj_one, parallelPairHom_app_one, Category.id_comp] erw [Category.comp_id] theorem id_whiskerRight_bimod {X Y Z : Mon_ C} {M : Bimod X Y} {N : Bimod Y Z} : whiskerRight (𝟙 M) N = 𝟙 (M.tensorBimod N) := by ext apply Limits.coequalizer.hom_ext dsimp only [tensorBimod_X, whiskerRight_hom, id_hom'] simp only [MonoidalCategory.id_whiskerRight, ι_colimMap, parallelPair_obj_one, parallelPairHom_app_one, Category.id_comp] erw [Category.comp_id] theorem whiskerLeft_comp_bimod {X Y Z : Mon_ C} (M : Bimod X Y) {N P Q : Bimod Y Z} (f : N ⟶ P) (g : P ⟶ Q) : whiskerLeft M (f ≫ g) = whiskerLeft M f ≫ whiskerLeft M g := by ext apply Limits.coequalizer.hom_ext simp theorem id_whiskerLeft_bimod {X Y : Mon_ C} {M N : Bimod X Y} (f : M ⟶ N) : whiskerLeft (regular X) f = (leftUnitorBimod M).hom ≫ f ≫ (leftUnitorBimod N).inv := by dsimp [tensorHom, regular, leftUnitorBimod] ext apply coequalizer.hom_ext dsimp slice_lhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] dsimp [LeftUnitorBimod.hom] slice_rhs 1 2 => rw [coequalizer.π_desc] dsimp [LeftUnitorBimod.inv] slice_rhs 1 2 => rw [Hom.left_act_hom] slice_rhs 2 3 => rw [leftUnitor_inv_naturality] slice_rhs 3 4 => rw [whisker_exchange] slice_rhs 4 4 => rw [← Iso.inv_hom_id_assoc (α_ X.X X.X N.X) (X.X ◁ N.actLeft)] slice_rhs 5 7 => rw [← Category.assoc, ← coequalizer.condition] slice_rhs 3 4 => rw [associator_inv_naturality_left] slice_rhs 4 5 => rw [← comp_whiskerRight, Mon_.one_mul] have : (λ_ (X.X ⊗ N.X)).inv ≫ (α_ (𝟙_ C) X.X N.X).inv ≫ ((λ_ X.X).hom ▷ N.X) = 𝟙 _ := by monoidal slice_rhs 2 4 => rw [this] slice_rhs 1 2 => rw [Category.comp_id] theorem comp_whiskerLeft_bimod {W X Y Z : Mon_ C} (M : Bimod W X) (N : Bimod X Y) {P P' : Bimod Y Z} (f : P ⟶ P') : whiskerLeft (M.tensorBimod N) f = (associatorBimod M N P).hom ≫ whiskerLeft M (whiskerLeft N f) ≫ (associatorBimod M N P').inv := by dsimp [tensorHom, tensorBimod, associatorBimod] ext apply coequalizer.hom_ext dsimp slice_lhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] dsimp [TensorBimod.X, AssociatorBimod.hom] slice_rhs 1 2 => rw [coequalizer.π_desc] dsimp [AssociatorBimod.homAux, AssociatorBimod.inv] refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ rw [tensorRight_map] slice_rhs 1 3 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_rhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_rhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, ι_colimMap, parallelPairHom_app_one] slice_rhs 3 4 => rw [coequalizer.π_desc] dsimp [AssociatorBimod.invAux] slice_rhs 2 2 => rw [MonoidalCategory.whiskerLeft_comp] slice_rhs 3 5 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_rhs 2 3 => rw [associator_inv_naturality_right] slice_rhs 1 3 => rw [Iso.hom_inv_id_assoc] slice_lhs 1 2 => rw [← whisker_exchange] rfl theorem comp_whiskerRight_bimod {X Y Z : Mon_ C} {M N P : Bimod X Y} (f : M ⟶ N) (g : N ⟶ P) (Q : Bimod Y Z) : whiskerRight (f ≫ g) Q = whiskerRight f Q ≫ whiskerRight g Q := by ext apply Limits.coequalizer.hom_ext simp theorem whiskerRight_id_bimod {X Y : Mon_ C} {M N : Bimod X Y} (f : M ⟶ N) : whiskerRight f (regular Y) = (rightUnitorBimod M).hom ≫ f ≫ (rightUnitorBimod N).inv := by dsimp [tensorHom, regular, rightUnitorBimod] ext apply coequalizer.hom_ext dsimp slice_lhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] dsimp [RightUnitorBimod.hom] slice_rhs 1 2 => rw [coequalizer.π_desc] dsimp [RightUnitorBimod.inv] slice_rhs 1 2 => rw [Hom.right_act_hom] slice_rhs 2 3 => rw [rightUnitor_inv_naturality] slice_rhs 3 4 => rw [← whisker_exchange] slice_rhs 4 5 => rw [coequalizer.condition] slice_rhs 3 4 => rw [associator_naturality_right] slice_rhs 4 5 => rw [← MonoidalCategory.whiskerLeft_comp, Mon_.mul_one] simp theorem whiskerRight_comp_bimod {W X Y Z : Mon_ C} {M M' : Bimod W X} (f : M ⟶ M') (N : Bimod X Y) (P : Bimod Y Z) : whiskerRight f (N.tensorBimod P) = (associatorBimod M N P).inv ≫ whiskerRight (whiskerRight f N) P ≫ (associatorBimod M' N P).hom := by dsimp [tensorHom, tensorBimod, associatorBimod] ext apply coequalizer.hom_ext dsimp slice_lhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] dsimp [TensorBimod.X, AssociatorBimod.inv] slice_rhs 1 2 => rw [coequalizer.π_desc] dsimp [AssociatorBimod.invAux, AssociatorBimod.hom] refine (cancel_epi ((tensorLeft _).map (coequalizer.π _ _))).1 ?_ rw [tensorLeft_map] slice_rhs 1 3 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_rhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_rhs 2 3 => rw [← comp_whiskerRight, ι_colimMap, parallelPairHom_app_one] slice_rhs 3 4 => rw [coequalizer.π_desc] dsimp [AssociatorBimod.homAux] slice_rhs 2 2 => rw [comp_whiskerRight] slice_rhs 3 5 => rw [π_tensor_id_preserves_coequalizer_inv_desc] slice_rhs 2 3 => rw [associator_naturality_left] slice_rhs 1 3 => rw [Iso.inv_hom_id_assoc] slice_lhs 1 2 => rw [whisker_exchange] rfl theorem whisker_assoc_bimod {W X Y Z : Mon_ C} (M : Bimod W X) {N N' : Bimod X Y} (f : N ⟶ N') (P : Bimod Y Z) : whiskerRight (whiskerLeft M f) P = (associatorBimod M N P).hom ≫ whiskerLeft M (whiskerRight f P) ≫ (associatorBimod M N' P).inv := by dsimp [tensorHom, tensorBimod, associatorBimod] ext apply coequalizer.hom_ext dsimp slice_lhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] dsimp [AssociatorBimod.hom] slice_rhs 1 2 => rw [coequalizer.π_desc] dsimp [AssociatorBimod.homAux] refine (cancel_epi ((tensorRight _).map (coequalizer.π _ _))).1 ?_ rw [tensorRight_map] slice_lhs 1 2 => rw [← comp_whiskerRight, ι_colimMap, parallelPairHom_app_one] slice_rhs 1 3 => rw [π_tensor_id_preserves_coequalizer_inv_desc]
slice_rhs 3 4 => rw [ι_colimMap, parallelPairHom_app_one] slice_rhs 2 3 => rw [← MonoidalCategory.whiskerLeft_comp, ι_colimMap, parallelPairHom_app_one] dsimp [AssociatorBimod.inv] slice_rhs 3 4 => rw [coequalizer.π_desc] dsimp [AssociatorBimod.invAux] slice_rhs 2 2 => rw [MonoidalCategory.whiskerLeft_comp] slice_rhs 3 5 => rw [id_tensor_π_preserves_coequalizer_inv_desc] slice_rhs 2 3 => rw [associator_inv_naturality_middle] slice_rhs 1 3 => rw [Iso.hom_inv_id_assoc] slice_lhs 1 1 => rw [comp_whiskerRight] theorem whisker_exchange_bimod {X Y Z : Mon_ C} {M N : Bimod X Y} {P Q : Bimod Y Z} (f : M ⟶ N) (g : P ⟶ Q) : whiskerLeft M g ≫ whiskerRight f Q = whiskerRight f P ≫ whiskerLeft N g := by ext apply coequalizer.hom_ext dsimp slice_lhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] slice_lhs 2 3 => rw [ι_colimMap, parallelPairHom_app_one] slice_lhs 1 2 => rw [whisker_exchange] slice_rhs 1 2 => rw [ι_colimMap, parallelPairHom_app_one] slice_rhs 2 3 => rw [ι_colimMap, parallelPairHom_app_one] simp only [Category.assoc] theorem pentagon_bimod {V W X Y Z : Mon_ C} (M : Bimod V W) (N : Bimod W X) (P : Bimod X Y) (Q : Bimod Y Z) :
Mathlib/CategoryTheory/Monoidal/Bimod.lean
869
894
/- Copyright (c) 2022 Rémi Bottinelli. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémi Bottinelli, Junyan Xu -/ import Mathlib.Algebra.Group.Subgroup.Defs import Mathlib.CategoryTheory.Groupoid.VertexGroup import Mathlib.CategoryTheory.Groupoid.Basic import Mathlib.CategoryTheory.Groupoid import Mathlib.Data.Set.Lattice /-! # Subgroupoid This file defines subgroupoids as `structure`s containing the subsets of arrows and their stability under composition and inversion. Also defined are: * containment of subgroupoids is a complete lattice; * images and preimages of subgroupoids under a functor; * the notion of normality of subgroupoids and its stability under intersection and preimage; * compatibility of the above with `CategoryTheory.Groupoid.vertexGroup`. ## Main definitions Given a type `C` with associated `groupoid C` instance. * `CategoryTheory.Subgroupoid C` is the type of subgroupoids of `C` * `CategoryTheory.Subgroupoid.IsNormal` is the property that the subgroupoid is stable under conjugation by arbitrary arrows, _and_ that all identity arrows are contained in the subgroupoid. * `CategoryTheory.Subgroupoid.comap` is the "preimage" map of subgroupoids along a functor. * `CategoryTheory.Subgroupoid.map` is the "image" map of subgroupoids along a functor _injective on objects_. * `CategoryTheory.Subgroupoid.vertexSubgroup` is the subgroup of the *vertex group* at a given vertex `v`, assuming `v` is contained in the `CategoryTheory.Subgroupoid` (meaning, by definition, that the arrow `𝟙 v` is contained in the subgroupoid). ## Implementation details The structure of this file is copied from/inspired by `Mathlib/GroupTheory/Subgroup/Basic.lean` and `Mathlib/Combinatorics/SimpleGraph/Subgraph.lean`. ## TODO * Equivalent inductive characterization of generated (normal) subgroupoids. * Characterization of normal subgroupoids as kernels. * Prove that `CategoryTheory.Subgroupoid.full` and `CategoryTheory.Subgroupoid.disconnect` preserve intersections (and `CategoryTheory.Subgroupoid.disconnect` also unions) ## Tags category theory, groupoid, subgroupoid -/ namespace CategoryTheory open Set Groupoid universe u v variable {C : Type u} [Groupoid C] /-- A sugroupoid of `C` consists of a choice of arrows for each pair of vertices, closed under composition and inverses. -/ @[ext] structure Subgroupoid (C : Type u) [Groupoid C] where /-- The arrow choice for each pair of vertices -/ arrows : ∀ c d : C, Set (c ⟶ d) protected inv : ∀ {c d} {p : c ⟶ d}, p ∈ arrows c d → Groupoid.inv p ∈ arrows d c protected mul : ∀ {c d e} {p}, p ∈ arrows c d → ∀ {q}, q ∈ arrows d e → p ≫ q ∈ arrows c e namespace Subgroupoid variable (S : Subgroupoid C) theorem inv_mem_iff {c d : C} (f : c ⟶ d) : Groupoid.inv f ∈ S.arrows d c ↔ f ∈ S.arrows c d := by constructor · intro h simpa only [inv_eq_inv, IsIso.inv_inv] using S.inv h · apply S.inv theorem mul_mem_cancel_left {c d e : C} {f : c ⟶ d} {g : d ⟶ e} (hf : f ∈ S.arrows c d) : f ≫ g ∈ S.arrows c e ↔ g ∈ S.arrows d e := by constructor · rintro h suffices Groupoid.inv f ≫ f ≫ g ∈ S.arrows d e by simpa only [inv_eq_inv, IsIso.inv_hom_id_assoc] using this apply S.mul (S.inv hf) h · apply S.mul hf theorem mul_mem_cancel_right {c d e : C} {f : c ⟶ d} {g : d ⟶ e} (hg : g ∈ S.arrows d e) : f ≫ g ∈ S.arrows c e ↔ f ∈ S.arrows c d := by constructor · rintro h suffices (f ≫ g) ≫ Groupoid.inv g ∈ S.arrows c d by simpa only [inv_eq_inv, IsIso.hom_inv_id, Category.comp_id, Category.assoc] using this apply S.mul h (S.inv hg) · exact fun hf => S.mul hf hg /-- The vertices of `C` on which `S` has non-trivial isotropy -/ def objs : Set C := {c : C | (S.arrows c c).Nonempty} theorem mem_objs_of_src {c d : C} {f : c ⟶ d} (h : f ∈ S.arrows c d) : c ∈ S.objs := ⟨f ≫ Groupoid.inv f, S.mul h (S.inv h)⟩ theorem mem_objs_of_tgt {c d : C} {f : c ⟶ d} (h : f ∈ S.arrows c d) : d ∈ S.objs := ⟨Groupoid.inv f ≫ f, S.mul (S.inv h) h⟩ theorem id_mem_of_nonempty_isotropy (c : C) : c ∈ objs S → 𝟙 c ∈ S.arrows c c := by rintro ⟨γ, hγ⟩ convert S.mul hγ (S.inv hγ) simp only [inv_eq_inv, IsIso.hom_inv_id] theorem id_mem_of_src {c d : C} {f : c ⟶ d} (h : f ∈ S.arrows c d) : 𝟙 c ∈ S.arrows c c := id_mem_of_nonempty_isotropy S c (mem_objs_of_src S h) theorem id_mem_of_tgt {c d : C} {f : c ⟶ d} (h : f ∈ S.arrows c d) : 𝟙 d ∈ S.arrows d d := id_mem_of_nonempty_isotropy S d (mem_objs_of_tgt S h) /-- A subgroupoid seen as a quiver on vertex set `C` -/ def asWideQuiver : Quiver C := ⟨fun c d => Subtype <| S.arrows c d⟩ /-- The coercion of a subgroupoid as a groupoid -/ @[simps comp_coe, simps -isSimp inv_coe] instance coe : Groupoid S.objs where Hom a b := S.arrows a.val b.val id a := ⟨𝟙 a.val, id_mem_of_nonempty_isotropy S a.val a.prop⟩ comp p q := ⟨p.val ≫ q.val, S.mul p.prop q.prop⟩ inv p := ⟨Groupoid.inv p.val, S.inv p.prop⟩ @[simp] theorem coe_inv_coe' {c d : S.objs} (p : c ⟶ d) : (CategoryTheory.inv p).val = CategoryTheory.inv p.val := by simp only [← inv_eq_inv, coe_inv_coe] /-- The embedding of the coerced subgroupoid to its parent -/ def hom : S.objs ⥤ C where obj c := c.val map f := f.val map_id _ := rfl map_comp _ _ := rfl theorem hom.inj_on_objects : Function.Injective (hom S).obj := by rintro ⟨c, hc⟩ ⟨d, hd⟩ hcd simp only [Subtype.mk_eq_mk]; exact hcd theorem hom.faithful : ∀ c d, Function.Injective fun f : c ⟶ d => (hom S).map f := by rintro ⟨c, hc⟩ ⟨d, hd⟩ ⟨f, hf⟩ ⟨g, hg⟩ hfg; exact Subtype.eq hfg /-- The subgroup of the vertex group at `c` given by the subgroupoid -/ def vertexSubgroup {c : C} (hc : c ∈ S.objs) : Subgroup (c ⟶ c) where carrier := S.arrows c c mul_mem' hf hg := S.mul hf hg one_mem' := id_mem_of_nonempty_isotropy _ _ hc inv_mem' hf := S.inv hf /-- The set of all arrows of a subgroupoid, as a set in `Σ c d : C, c ⟶ d`. -/ @[coe] def toSet (S : Subgroupoid C) : Set (Σ c d : C, c ⟶ d) := {F | F.2.2 ∈ S.arrows F.1 F.2.1} instance : SetLike (Subgroupoid C) (Σ c d : C, c ⟶ d) where coe := toSet coe_injective' := fun ⟨S, _, _⟩ ⟨T, _, _⟩ h => by ext c d f; apply Set.ext_iff.1 h ⟨c, d, f⟩ theorem mem_iff (S : Subgroupoid C) (F : Σ c d, c ⟶ d) : F ∈ S ↔ F.2.2 ∈ S.arrows F.1 F.2.1 := Iff.rfl theorem le_iff (S T : Subgroupoid C) : S ≤ T ↔ ∀ {c d}, S.arrows c d ⊆ T.arrows c d := by rw [SetLike.le_def, Sigma.forall]; exact forall_congr' fun c => Sigma.forall instance : Top (Subgroupoid C) := ⟨{ arrows := fun _ _ => Set.univ mul := by intros; trivial inv := by intros; trivial }⟩ theorem mem_top {c d : C} (f : c ⟶ d) : f ∈ (⊤ : Subgroupoid C).arrows c d := trivial theorem mem_top_objs (c : C) : c ∈ (⊤ : Subgroupoid C).objs := by dsimp [Top.top, objs] simp only [univ_nonempty] instance : Bot (Subgroupoid C) := ⟨{ arrows := fun _ _ => ∅ mul := False.elim inv := False.elim }⟩ instance : Inhabited (Subgroupoid C) := ⟨⊤⟩ instance : Min (Subgroupoid C) := ⟨fun S T => { arrows := fun c d => S.arrows c d ∩ T.arrows c d inv := fun hp ↦ ⟨S.inv hp.1, T.inv hp.2⟩ mul := fun hp _ hq ↦ ⟨S.mul hp.1 hq.1, T.mul hp.2 hq.2⟩ }⟩ instance : InfSet (Subgroupoid C) := ⟨fun s => { arrows := fun c d => ⋂ S ∈ s, Subgroupoid.arrows S c d inv := fun hp ↦ by rw [mem_iInter₂] at hp ⊢; exact fun S hS => S.inv (hp S hS) mul := fun hp _ hq ↦ by rw [mem_iInter₂] at hp hq ⊢ exact fun S hS => S.mul (hp S hS) (hq S hS) }⟩ theorem mem_sInf_arrows {s : Set (Subgroupoid C)} {c d : C} {p : c ⟶ d} : p ∈ (sInf s).arrows c d ↔ ∀ S ∈ s, p ∈ S.arrows c d := mem_iInter₂ theorem mem_sInf {s : Set (Subgroupoid C)} {p : Σ c d : C, c ⟶ d} : p ∈ sInf s ↔ ∀ S ∈ s, p ∈ S := mem_sInf_arrows instance : CompleteLattice (Subgroupoid C) := { completeLatticeOfInf (Subgroupoid C) (by refine fun s => ⟨fun S Ss F => ?_, fun T Tl F fT => ?_⟩ <;> simp only [mem_sInf] exacts [fun hp => hp S Ss, fun S Ss => Tl Ss fT]) with bot := ⊥ bot_le := fun _ => empty_subset _ top := ⊤ le_top := fun _ => subset_univ _ inf := (· ⊓ ·) le_inf := fun _ _ _ RS RT _ pR => ⟨RS pR, RT pR⟩ inf_le_left := fun _ _ _ => And.left inf_le_right := fun _ _ _ => And.right } theorem le_objs {S T : Subgroupoid C} (h : S ≤ T) : S.objs ⊆ T.objs := fun s ⟨γ, hγ⟩ => ⟨γ, @h ⟨s, s, γ⟩ hγ⟩ /-- The functor associated to the embedding of subgroupoids -/ def inclusion {S T : Subgroupoid C} (h : S ≤ T) : S.objs ⥤ T.objs where obj s := ⟨s.val, le_objs h s.prop⟩ map f := ⟨f.val, @h ⟨_, _, f.val⟩ f.prop⟩ map_id _ := rfl map_comp _ _ := rfl theorem inclusion_inj_on_objects {S T : Subgroupoid C} (h : S ≤ T) : Function.Injective (inclusion h).obj := fun ⟨s, hs⟩ ⟨t, ht⟩ => by simpa only [inclusion, Subtype.mk_eq_mk] using id theorem inclusion_faithful {S T : Subgroupoid C} (h : S ≤ T) (s t : S.objs) : Function.Injective fun f : s ⟶ t => (inclusion h).map f := fun ⟨f, hf⟩ ⟨g, hg⟩ => by -- Porting note: was `...; simpa only [Subtype.mk_eq_mk] using id` dsimp only [inclusion]; rw [Subtype.mk_eq_mk, Subtype.mk_eq_mk]; exact id theorem inclusion_refl {S : Subgroupoid C} : inclusion (le_refl S) = 𝟭 S.objs := Functor.hext (fun _ => rfl) fun _ _ _ => HEq.refl _ theorem inclusion_trans {R S T : Subgroupoid C} (k : R ≤ S) (h : S ≤ T) : inclusion (k.trans h) = inclusion k ⋙ inclusion h := rfl theorem inclusion_comp_embedding {S T : Subgroupoid C} (h : S ≤ T) : inclusion h ⋙ T.hom = S.hom := rfl /-- The family of arrows of the discrete groupoid -/ inductive Discrete.Arrows : ∀ c d : C, (c ⟶ d) → Prop | id (c : C) : Discrete.Arrows c c (𝟙 c) /-- The only arrows of the discrete groupoid are the identity arrows. -/ def discrete : Subgroupoid C where arrows c d := {p | Discrete.Arrows c d p} inv := by rintro _ _ _ ⟨⟩; simp only [inv_eq_inv, IsIso.inv_id]; constructor mul := by rintro _ _ _ _ ⟨⟩ _ ⟨⟩; rw [Category.comp_id]; constructor theorem mem_discrete_iff {c d : C} (f : c ⟶ d) : f ∈ discrete.arrows c d ↔ ∃ h : c = d, f = eqToHom h := ⟨by rintro ⟨⟩; exact ⟨rfl, rfl⟩, by rintro ⟨rfl, rfl⟩; constructor⟩ /-- A subgroupoid is wide if its carrier set is all of `C`. -/ structure IsWide : Prop where wide : ∀ c, 𝟙 c ∈ S.arrows c c theorem isWide_iff_objs_eq_univ : S.IsWide ↔ S.objs = Set.univ := by constructor · rintro h ext x; constructor <;> simp only [top_eq_univ, mem_univ, imp_true_iff, forall_true_left] apply mem_objs_of_src S (h.wide x) · rintro h refine ⟨fun c => ?_⟩ obtain ⟨γ, γS⟩ := (le_of_eq h.symm : ⊤ ⊆ S.objs) (Set.mem_univ c) exact id_mem_of_src S γS theorem IsWide.id_mem {S : Subgroupoid C} (Sw : S.IsWide) (c : C) : 𝟙 c ∈ S.arrows c c := Sw.wide c theorem IsWide.eqToHom_mem {S : Subgroupoid C} (Sw : S.IsWide) {c d : C} (h : c = d) : eqToHom h ∈ S.arrows c d := by cases h; simp only [eqToHom_refl]; apply Sw.id_mem c /-- A subgroupoid is normal if it is wide and satisfies the expected stability under conjugacy. -/ structure IsNormal : Prop extends IsWide S where conj : ∀ {c d} (p : c ⟶ d) {γ : c ⟶ c}, γ ∈ S.arrows c c → Groupoid.inv p ≫ γ ≫ p ∈ S.arrows d d theorem IsNormal.conj' {S : Subgroupoid C} (Sn : IsNormal S) : ∀ {c d} (p : d ⟶ c) {γ : c ⟶ c}, γ ∈ S.arrows c c → p ≫ γ ≫ Groupoid.inv p ∈ S.arrows d d := fun p γ hs => by convert Sn.conj (Groupoid.inv p) hs; simp theorem IsNormal.conjugation_bij (Sn : IsNormal S) {c d} (p : c ⟶ d) : Set.BijOn (fun γ : c ⟶ c => Groupoid.inv p ≫ γ ≫ p) (S.arrows c c) (S.arrows d d) := by refine ⟨fun γ γS => Sn.conj p γS, fun γ₁ _ γ₂ _ h => ?_, fun δ δS => ⟨p ≫ δ ≫ Groupoid.inv p, Sn.conj' p δS, ?_⟩⟩ · simpa only [inv_eq_inv, Category.assoc, IsIso.hom_inv_id, Category.comp_id, IsIso.hom_inv_id_assoc] using p ≫= h =≫ inv p · simp only [inv_eq_inv, Category.assoc, IsIso.inv_hom_id, Category.comp_id, IsIso.inv_hom_id_assoc] theorem top_isNormal : IsNormal (⊤ : Subgroupoid C) := { wide := fun _ => trivial conj := fun _ _ _ => trivial } theorem sInf_isNormal (s : Set <| Subgroupoid C) (sn : ∀ S ∈ s, IsNormal S) : IsNormal (sInf s) := { wide := by simp_rw [sInf, mem_iInter₂]; exact fun c S Ss => (sn S Ss).wide c conj := by simp_rw [sInf, mem_iInter₂]; exact fun p γ hγ S Ss => (sn S Ss).conj p (hγ S Ss) } theorem discrete_isNormal : (@discrete C _).IsNormal := { wide := fun c => by constructor conj := fun f γ hγ => by cases hγ simp only [inv_eq_inv, Category.id_comp, IsIso.inv_hom_id]; constructor } theorem IsNormal.vertexSubgroup (Sn : IsNormal S) (c : C) (cS : c ∈ S.objs) : (S.vertexSubgroup cS).Normal where conj_mem x hx y := by rw [mul_assoc]; exact Sn.conj' y hx section GeneratedSubgroupoid -- TODO: proof that generated is just "words in X" and generatedNormal is similarly variable (X : ∀ c d : C, Set (c ⟶ d)) /-- The subgropoid generated by the set of arrows `X` -/ def generated : Subgroupoid C := sInf {S : Subgroupoid C | ∀ c d, X c d ⊆ S.arrows c d} theorem subset_generated (c d : C) : X c d ⊆ (generated X).arrows c d := by dsimp only [generated, sInf] simp only [subset_iInter₂_iff] exact fun S hS f fS => hS _ _ fS /-- The normal sugroupoid generated by the set of arrows `X` -/ def generatedNormal : Subgroupoid C := sInf {S : Subgroupoid C | (∀ c d, X c d ⊆ S.arrows c d) ∧ S.IsNormal} theorem generated_le_generatedNormal : generated X ≤ generatedNormal X := by apply @sInf_le_sInf (Subgroupoid C) _ exact fun S ⟨h, _⟩ => h theorem generatedNormal_isNormal : (generatedNormal X).IsNormal := sInf_isNormal _ fun _ h => h.right theorem IsNormal.generatedNormal_le {S : Subgroupoid C} (Sn : S.IsNormal) : generatedNormal X ≤ S ↔ ∀ c d, X c d ⊆ S.arrows c d := by constructor · rintro h c d have h' := generated_le_generatedNormal X rw [le_iff] at h h' exact ((subset_generated X c d).trans (@h' c d)).trans (@h c d) · rintro h apply @sInf_le (Subgroupoid C) _ exact ⟨h, Sn⟩ end GeneratedSubgroupoid section Hom variable {D : Type*} [Groupoid D] (φ : C ⥤ D) /-- A functor between groupoid defines a map of subgroupoids in the reverse direction by taking preimages. -/ def comap (S : Subgroupoid D) : Subgroupoid C where arrows c d := {f : c ⟶ d | φ.map f ∈ S.arrows (φ.obj c) (φ.obj d)} inv hp := by rw [mem_setOf, inv_eq_inv, φ.map_inv, ← inv_eq_inv]; exact S.inv hp mul := by intros simp only [mem_setOf, Functor.map_comp] apply S.mul <;> assumption theorem comap_mono (S T : Subgroupoid D) : S ≤ T → comap φ S ≤ comap φ T := fun ST _ =>
@ST ⟨_, _, _⟩ theorem isNormal_comap {S : Subgroupoid D} (Sn : IsNormal S) : IsNormal (comap φ S) where wide c := by rw [comap, mem_setOf, Functor.map_id]; apply Sn.wide
Mathlib/CategoryTheory/Groupoid/Subgroupoid.lean
384
387
/- Copyright (c) 2022 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.Topology.Connected.Basic /-! # Locally connected topological spaces A topological space is **locally connected** if each neighborhood filter admits a basis of connected *open* sets. Local connectivity is equivalent to each point having a basis of connected (not necessarily open) sets --- but in a non-trivial way, so we choose this definition and prove the equivalence later in `locallyConnectedSpace_iff_connected_basis`. -/ open Set Topology universe u v variable {α : Type u} {β : Type v} {ι : Type*} {π : ι → Type*} [TopologicalSpace α] {s t u v : Set α} section LocallyConnectedSpace /-- A topological space is **locally connected** if each neighborhood filter admits a basis of connected *open* sets. Note that it is equivalent to each point having a basis of connected (non necessarily open) sets but in a non-trivial way, so we choose this definition and prove the equivalence later in `locallyConnectedSpace_iff_connected_basis`. -/ class LocallyConnectedSpace (α : Type*) [TopologicalSpace α] : Prop where /-- Open connected neighborhoods form a basis of the neighborhoods filter. -/ open_connected_basis : ∀ x, (𝓝 x).HasBasis (fun s : Set α => IsOpen s ∧ x ∈ s ∧ IsConnected s) id theorem locallyConnectedSpace_iff_hasBasis_isOpen_isConnected : LocallyConnectedSpace α ↔ ∀ x, (𝓝 x).HasBasis (fun s : Set α => IsOpen s ∧ x ∈ s ∧ IsConnected s) id := ⟨@LocallyConnectedSpace.open_connected_basis _ _, LocallyConnectedSpace.mk⟩ @[deprecated (since := "2024-11-18")] alias locallyConnectedSpace_iff_open_connected_basis := locallyConnectedSpace_iff_hasBasis_isOpen_isConnected
theorem locallyConnectedSpace_iff_subsets_isOpen_isConnected : LocallyConnectedSpace α ↔ ∀ x, ∀ U ∈ 𝓝 x, ∃ V : Set α, V ⊆ U ∧ IsOpen V ∧ x ∈ V ∧ IsConnected V := by simp_rw [locallyConnectedSpace_iff_hasBasis_isOpen_isConnected] refine forall_congr' fun _ => ?_ constructor · intro h U hU rcases h.mem_iff.mp hU with ⟨V, hV, hVU⟩ exact ⟨V, hVU, hV⟩ · exact fun h => ⟨fun U => ⟨fun hU => let ⟨V, hVU, hV⟩ := h U hU
Mathlib/Topology/Connected/LocallyConnected.lean
41
52
/- 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,713
1,714
/- 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) namespace Precomp variable (X : C) /-- The map `Fin (n + 1 + 1) → C` which "shifts" `F.obj'` to the right and inserts `X` in the zeroth position. -/ def obj : Fin (n + 1 + 1) → C | ⟨0, _⟩ => X | ⟨i + 1, hi⟩ => F.obj' i @[simp] lemma obj_zero : obj F X 0 = X := rfl @[simp] lemma obj_one : obj F X 1 = F.obj' 0 := rfl @[simp] lemma obj_succ (i : ℕ) (hi : i + 1 < n + 1 + 1) : obj F X ⟨i + 1, hi⟩ = F.obj' i := rfl variable {X} (f : X ⟶ F.left) /-- Auxiliary definition for the action on maps of the functor `F.precomp f`. It sends `0 ≤ 1` to `f` and `i + 1 ≤ j + 1` to `F.map' i j`. -/ def map : ∀ (i j : Fin (n + 1 + 1)) (_ : i ≤ j), obj F X i ⟶ obj F X j | ⟨0, _⟩, ⟨0, _⟩, _ => 𝟙 X | ⟨0, _⟩, ⟨1, _⟩, _ => f | ⟨0, _⟩, ⟨j + 2, hj⟩, _ => f ≫ F.map' 0 (j + 1) | ⟨i + 1, hi⟩, ⟨j + 1, hj⟩, hij => F.map' i j (by simpa using hij) @[simp] lemma map_zero_zero : map F f 0 0 (by simp) = 𝟙 X := rfl @[simp] lemma map_one_one : map F f 1 1 (by simp) = F.map (𝟙 _) := rfl @[simp] lemma map_zero_one : map F f 0 1 (by simp) = f := rfl @[simp] lemma map_zero_one' : map F f 0 ⟨0 + 1, by simp⟩ (by simp) = f := rfl @[simp] lemma map_zero_succ_succ (j : ℕ) (hj : j + 2 < n + 1 + 1) : map F f 0 ⟨j + 2, hj⟩ (by simp) = f ≫ F.map' 0 (j+1) := rfl @[simp] lemma map_succ_succ (i j : ℕ) (hi : i + 1 < n + 1 + 1) (hj : j + 1 < n + 1 + 1) (hij : i + 1 ≤ j + 1) : map F f ⟨i + 1, hi⟩ ⟨j + 1, hj⟩ hij = F.map' i j := rfl @[simp] lemma map_one_succ (j : ℕ) (hj : j + 1 < n + 1 + 1) : map F f 1 ⟨j + 1, hj⟩ (by simp [Fin.le_def]) = F.map' 0 j := rfl lemma map_id (i : Fin (n + 1 + 1)) : map F f i i (by simp) = 𝟙 _ := by obtain ⟨_|_, hi⟩ := i <;> simp lemma map_comp {i j k : Fin (n + 1 + 1)} (hij : i ≤ j) (hjk : j ≤ k) : map F f i k (hij.trans hjk) = map F f i j hij ≫ map F f j k hjk := by obtain ⟨i, hi⟩ := i obtain ⟨j, hj⟩ := j obtain ⟨k, hk⟩ := k cases i · obtain _ | _ | j := j · dsimp rw [id_comp] · obtain _ | _ | k := k · simp [Nat.succ.injEq] at hjk · simp · rfl · obtain _ | _ | k := k · simp [Fin.ext_iff] at hjk · simp [Fin.le_def] at hjk omega · dsimp rw [assoc, ← F.map_comp, homOfLE_comp] · obtain _ | j := j · simp [Fin.ext_iff] at hij · obtain _ | k := k · simp [Fin.ext_iff] at hjk · dsimp rw [← F.map_comp, homOfLE_comp] end Precomp /-- "Precomposition" of `F : ComposableArrows C n` by a morphism `f : X ⟶ F.left`. -/ @[simps] def precomp {X : C} (f : X ⟶ F.left) : ComposableArrows C (n + 1) where obj := Precomp.obj F X map g := Precomp.map F f _ _ (leOfHom g) map_id := Precomp.map_id F f map_comp g g' := Precomp.map_comp F f (leOfHom g) (leOfHom g') /-- Constructor for `ComposableArrows C 2`. -/ @[simp] def mk₂ {X₀ X₁ X₂ : C} (f : X₀ ⟶ X₁) (g : X₁ ⟶ X₂) : ComposableArrows C 2 := (mk₁ g).precomp f /-- Constructor for `ComposableArrows C 3`. -/ @[simp] def mk₃ {X₀ X₁ X₂ X₃ : C} (f : X₀ ⟶ X₁) (g : X₁ ⟶ X₂) (h : X₂ ⟶ X₃) : ComposableArrows C 3 := (mk₂ g h).precomp f /-- Constructor for `ComposableArrows C 4`. -/ @[simp] def mk₄ {X₀ X₁ X₂ X₃ X₄ : C} (f : X₀ ⟶ X₁) (g : X₁ ⟶ X₂) (h : X₂ ⟶ X₃) (i : X₃ ⟶ X₄) : ComposableArrows C 4 := (mk₃ g h i).precomp f /-- Constructor for `ComposableArrows C 5`. -/ @[simp] def mk₅ {X₀ X₁ X₂ X₃ X₄ X₅ : C} (f : X₀ ⟶ X₁) (g : X₁ ⟶ X₂) (h : X₂ ⟶ X₃) (i : X₃ ⟶ X₄) (j : X₄ ⟶ X₅) : ComposableArrows C 5 := (mk₄ g h i j).precomp f section variable {X₀ X₁ X₂ X₃ X₄ : C} (f : X₀ ⟶ X₁) (g : X₁ ⟶ X₂) (h : X₂ ⟶ X₃) (i : X₃ ⟶ X₄) /-! These examples are meant to test the good definitional properties of `precomp`, and that `dsimp` can see through. -/ example : map' (mk₂ f g) 0 1 = f := by dsimp example : map' (mk₂ f g) 1 2 = g := by dsimp example : map' (mk₂ f g) 0 2 = f ≫ g := by dsimp example : (mk₂ f g).hom = f ≫ g := by dsimp example : map' (mk₂ f g) 0 0 = 𝟙 _ := by dsimp example : map' (mk₂ f g) 1 1 = 𝟙 _ := by dsimp example : map' (mk₂ f g) 2 2 = 𝟙 _ := by dsimp example : map' (mk₃ f g h) 0 1 = f := by dsimp example : map' (mk₃ f g h) 1 2 = g := by dsimp example : map' (mk₃ f g h) 2 3 = h := by dsimp example : map' (mk₃ f g h) 0 3 = f ≫ g ≫ h := by dsimp example : (mk₃ f g h).hom = f ≫ g ≫ h := by dsimp example : map' (mk₃ f g h) 0 2 = f ≫ g := by dsimp example : map' (mk₃ f g h) 1 3 = g ≫ h := by dsimp end /-- The map `ComposableArrows C m → ComposableArrows C n` obtained by precomposition with a functor `Fin (n + 1) ⥤ Fin (m + 1)`. -/ @[simps!] def whiskerLeft (F : ComposableArrows C m) (Φ : Fin (n + 1) ⥤ Fin (m + 1)) : ComposableArrows C n := Φ ⋙ F /-- The functor `ComposableArrows C m ⥤ ComposableArrows C n` obtained by precomposition with a functor `Fin (n + 1) ⥤ Fin (m + 1)`. -/ @[simps!] def whiskerLeftFunctor (Φ : Fin (n + 1) ⥤ Fin (m + 1)) : ComposableArrows C m ⥤ ComposableArrows C n where obj F := F.whiskerLeft Φ map f := CategoryTheory.whiskerLeft Φ f /-- The functor `Fin n ⥤ Fin (n + 1)` which sends `i` to `i.succ`. -/ @[simps] def _root_.Fin.succFunctor (n : ℕ) : Fin n ⥤ Fin (n + 1) where obj i := i.succ map {_ _} hij := homOfLE (Fin.succ_le_succ_iff.2 (leOfHom hij)) /-- The functor `ComposableArrows C (n + 1) ⥤ ComposableArrows C n` which forgets the first arrow. -/ @[simps!] def δ₀Functor : ComposableArrows C (n + 1) ⥤ ComposableArrows C n := whiskerLeftFunctor (Fin.succFunctor (n + 1)) /-- The `ComposableArrows C n` obtained by forgetting the first arrow. -/ abbrev δ₀ (F : ComposableArrows C (n + 1)) := δ₀Functor.obj F @[simp] lemma precomp_δ₀ {X : C} (f : X ⟶ F.left) : (F.precomp f).δ₀ = F := rfl /-- The functor `Fin n ⥤ Fin (n + 1)` which sends `i` to `i.castSucc`. -/ @[simps] def _root_.Fin.castSuccFunctor (n : ℕ) : Fin n ⥤ Fin (n + 1) where obj i := i.castSucc map hij := hij /-- The functor `ComposableArrows C (n + 1) ⥤ ComposableArrows C n` which forgets the last arrow. -/ @[simps!] def δlastFunctor : ComposableArrows C (n + 1) ⥤ ComposableArrows C n := whiskerLeftFunctor (Fin.castSuccFunctor (n + 1)) /-- The `ComposableArrows C n` obtained by forgetting the first arrow. -/ abbrev δlast (F : ComposableArrows C (n + 1)) := δlastFunctor.obj F section variable {F G : ComposableArrows C (n + 1)} /-- Inductive construction of morphisms in `ComposableArrows C (n + 1)`: in order to construct a morphism `F ⟶ G`, it suffices to provide `α : F.obj' 0 ⟶ G.obj' 0` and `β : F.δ₀ ⟶ G.δ₀` such that `F.map' 0 1 ≫ app' β 0 = α ≫ G.map' 0 1`. -/ def homMkSucc (α : F.obj' 0 ⟶ G.obj' 0) (β : F.δ₀ ⟶ G.δ₀) (w : F.map' 0 1 ≫ app' β 0 = α ≫ G.map' 0 1) : F ⟶ G := homMk (fun i => match i with | ⟨0, _⟩ => α | ⟨i + 1, hi⟩ => app' β i) (fun i hi => by obtain _ | i := i · exact w · exact naturality' β i (i + 1)) variable (α : F.obj' 0 ⟶ G.obj' 0) (β : F.δ₀ ⟶ G.δ₀) (w : F.map' 0 1 ≫ app' β 0 = α ≫ G.map' 0 1) @[simp] lemma homMkSucc_app_zero : (homMkSucc α β w).app 0 = α := rfl @[simp] lemma homMkSucc_app_succ (i : ℕ) (hi : i + 1 < n + 1 + 1) : (homMkSucc α β w).app ⟨i + 1, hi⟩ = app' β i := rfl end lemma hom_ext_succ {F G : ComposableArrows C (n + 1)} {f g : F ⟶ G} (h₀ : app' f 0 = app' g 0) (h₁ : δ₀Functor.map f = δ₀Functor.map g) : f = g := by ext ⟨i, hi⟩ obtain _ | i := i · exact h₀ · exact congr_app h₁ ⟨i, by valid⟩ /-- Inductive construction of isomorphisms in `ComposableArrows C (n + 1)`: in order to construct an isomorphism `F ≅ G`, it suffices to provide `α : F.obj' 0 ≅ G.obj' 0` and `β : F.δ₀ ≅ G.δ₀` such that `F.map' 0 1 ≫ app' β.hom 0 = α.hom ≫ G.map' 0 1`. -/ @[simps] def isoMkSucc {F G : ComposableArrows C (n + 1)} (α : F.obj' 0 ≅ G.obj' 0) (β : F.δ₀ ≅ G.δ₀) (w : F.map' 0 1 ≫ app' β.hom 0 = α.hom ≫ G.map' 0 1) : F ≅ G where hom := homMkSucc α.hom β.hom w inv := homMkSucc α.inv β.inv (by rw [← cancel_epi α.hom, ← reassoc_of% w, α.hom_inv_id_assoc, β.hom_inv_id_app] dsimp rw [comp_id]) hom_inv_id := by apply hom_ext_succ · simp · ext ⟨i, hi⟩ simp inv_hom_id := by apply hom_ext_succ · simp · ext ⟨i, hi⟩ simp lemma ext_succ {F G : ComposableArrows C (n + 1)} (h₀ : F.obj' 0 = G.obj' 0) (h : F.δ₀ = G.δ₀) (w : F.map' 0 1 = eqToHom h₀ ≫ G.map' 0 1 ≫ eqToHom (Functor.congr_obj h.symm 0)) : F = G := by have : ∀ i, F.obj i = G.obj i := by intro ⟨i, hi⟩ rcases i with - | i · exact h₀ · exact Functor.congr_obj h ⟨i, by valid⟩ exact Functor.ext_of_iso (isoMkSucc (eqToIso h₀) (eqToIso h) (by rw [w] dsimp [app'] rw [eqToHom_app, assoc, assoc, eqToHom_trans, eqToHom_refl, comp_id])) this (by rintro ⟨_|_, hi⟩ <;> simp) lemma precomp_surjective (F : ComposableArrows C (n + 1)) : ∃ (F₀ : ComposableArrows C n) (X₀ : C) (f₀ : X₀ ⟶ F₀.left), F = F₀.precomp f₀ := ⟨F.δ₀, _, F.map' 0 1, ext_succ rfl (by simp) (by simp)⟩ section variable {f g : ComposableArrows C 2} (app₀ : f.obj' 0 ⟶ g.obj' 0) (app₁ : f.obj' 1 ⟶ g.obj' 1) (app₂ : f.obj' 2 ⟶ g.obj' 2) (w₀ : f.map' 0 1 ≫ app₁ = app₀ ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂ = app₁ ≫ g.map' 1 2) /-- Constructor for morphisms in `ComposableArrows C 2`. -/ def homMk₂ : f ⟶ g := homMkSucc app₀ (homMk₁ app₁ app₂ w₁) w₀ @[simp] lemma homMk₂_app_zero : (homMk₂ app₀ app₁ app₂ w₀ w₁).app 0 = app₀ := rfl @[simp] lemma homMk₂_app_one : (homMk₂ app₀ app₁ app₂ w₀ w₁).app 1 = app₁ := rfl @[simp] lemma homMk₂_app_two : (homMk₂ app₀ app₁ app₂ w₀ w₁).app ⟨2, by valid⟩ = app₂ := rfl end @[ext] lemma hom_ext₂ {f g : ComposableArrows C 2} {φ φ' : f ⟶ g} (h₀ : app' φ 0 = app' φ' 0) (h₁ : app' φ 1 = app' φ' 1) (h₂ : app' φ 2 = app' φ' 2) : φ = φ' := hom_ext_succ h₀ (hom_ext₁ h₁ h₂) /-- Constructor for isomorphisms in `ComposableArrows C 2`. -/ @[simps] def isoMk₂ {f g : ComposableArrows C 2} (app₀ : f.obj' 0 ≅ g.obj' 0) (app₁ : f.obj' 1 ≅ g.obj' 1) (app₂ : f.obj' 2 ≅ g.obj' 2) (w₀ : f.map' 0 1 ≫ app₁.hom = app₀.hom ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂.hom = app₁.hom ≫ g.map' 1 2) : f ≅ g where hom := homMk₂ app₀.hom app₁.hom app₂.hom w₀ w₁ inv := homMk₂ app₀.inv app₁.inv app₂.inv (by rw [← cancel_epi app₀.hom, ← reassoc_of% w₀, app₁.hom_inv_id, comp_id, app₀.hom_inv_id_assoc]) (by rw [← cancel_epi app₁.hom, ← reassoc_of% w₁, app₂.hom_inv_id, comp_id, app₁.hom_inv_id_assoc]) lemma ext₂ {f g : ComposableArrows C 2} (h₀ : f.obj' 0 = g.obj' 0) (h₁ : f.obj' 1 = g.obj' 1) (h₂ : f.obj' 2 = g.obj' 2) (w₀ : f.map' 0 1 = eqToHom h₀ ≫ g.map' 0 1 ≫ eqToHom h₁.symm) (w₁ : f.map' 1 2 = eqToHom h₁ ≫ g.map' 1 2 ≫ eqToHom h₂.symm) : f = g := ext_succ h₀ (ext₁ h₁ h₂ w₁) w₀ lemma mk₂_surjective (X : ComposableArrows C 2) : ∃ (X₀ X₁ X₂ : C) (f₀ : X₀ ⟶ X₁) (f₁ : X₁ ⟶ X₂), X = mk₂ f₀ f₁ := ⟨_, _, _, X.map' 0 1, X.map' 1 2, ext₂ rfl rfl rfl (by simp) (by simp)⟩ section variable {f g : ComposableArrows C 3} (app₀ : f.obj' 0 ⟶ g.obj' 0) (app₁ : f.obj' 1 ⟶ g.obj' 1) (app₂ : f.obj' 2 ⟶ g.obj' 2) (app₃ : f.obj' 3 ⟶ g.obj' 3) (w₀ : f.map' 0 1 ≫ app₁ = app₀ ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂ = app₁ ≫ g.map' 1 2) (w₂ : f.map' 2 3 ≫ app₃ = app₂ ≫ g.map' 2 3) /-- Constructor for morphisms in `ComposableArrows C 3`. -/ def homMk₃ : f ⟶ g := homMkSucc app₀ (homMk₂ app₁ app₂ app₃ w₁ w₂) w₀ @[simp] lemma homMk₃_app_zero : (homMk₃ app₀ app₁ app₂ app₃ w₀ w₁ w₂).app 0 = app₀ := rfl @[simp] lemma homMk₃_app_one : (homMk₃ app₀ app₁ app₂ app₃ w₀ w₁ w₂).app 1 = app₁ := rfl @[simp] lemma homMk₃_app_two : (homMk₃ app₀ app₁ app₂ app₃ w₀ w₁ w₂).app ⟨2, by valid⟩ = app₂ := rfl @[simp] lemma homMk₃_app_three : (homMk₃ app₀ app₁ app₂ app₃ w₀ w₁ w₂).app ⟨3, by valid⟩ = app₃ := rfl end @[ext] lemma hom_ext₃ {f g : ComposableArrows C 3} {φ φ' : f ⟶ g} (h₀ : app' φ 0 = app' φ' 0) (h₁ : app' φ 1 = app' φ' 1) (h₂ : app' φ 2 = app' φ' 2) (h₃ : app' φ 3 = app' φ' 3) : φ = φ' := hom_ext_succ h₀ (hom_ext₂ h₁ h₂ h₃) /-- Constructor for isomorphisms in `ComposableArrows C 3`. -/ @[simps] def isoMk₃ {f g : ComposableArrows C 3} (app₀ : f.obj' 0 ≅ g.obj' 0) (app₁ : f.obj' 1 ≅ g.obj' 1) (app₂ : f.obj' 2 ≅ g.obj' 2) (app₃ : f.obj' 3 ≅ g.obj' 3) (w₀ : f.map' 0 1 ≫ app₁.hom = app₀.hom ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂.hom = app₁.hom ≫ g.map' 1 2) (w₂ : f.map' 2 3 ≫ app₃.hom = app₂.hom ≫ g.map' 2 3) : f ≅ g where hom := homMk₃ app₀.hom app₁.hom app₂.hom app₃.hom w₀ w₁ w₂ inv := homMk₃ app₀.inv app₁.inv app₂.inv app₃.inv (by rw [← cancel_epi app₀.hom, ← reassoc_of% w₀, app₁.hom_inv_id, comp_id, app₀.hom_inv_id_assoc]) (by rw [← cancel_epi app₁.hom, ← reassoc_of% w₁, app₂.hom_inv_id, comp_id, app₁.hom_inv_id_assoc]) (by rw [← cancel_epi app₂.hom, ← reassoc_of% w₂, app₃.hom_inv_id, comp_id, app₂.hom_inv_id_assoc]) lemma ext₃ {f g : ComposableArrows C 3} (h₀ : f.obj' 0 = g.obj' 0) (h₁ : f.obj' 1 = g.obj' 1) (h₂ : f.obj' 2 = g.obj' 2) (h₃ : f.obj' 3 = g.obj' 3) (w₀ : f.map' 0 1 = eqToHom h₀ ≫ g.map' 0 1 ≫ eqToHom h₁.symm) (w₁ : f.map' 1 2 = eqToHom h₁ ≫ g.map' 1 2 ≫ eqToHom h₂.symm) (w₂ : f.map' 2 3 = eqToHom h₂ ≫ g.map' 2 3 ≫ eqToHom h₃.symm) : f = g := ext_succ h₀ (ext₂ h₁ h₂ h₃ w₁ w₂) w₀ lemma mk₃_surjective (X : ComposableArrows C 3) : ∃ (X₀ X₁ X₂ X₃ : C) (f₀ : X₀ ⟶ X₁) (f₁ : X₁ ⟶ X₂) (f₂ : X₂ ⟶ X₃), X = mk₃ f₀ f₁ f₂ := ⟨_, _, _, _, X.map' 0 1, X.map' 1 2, X.map' 2 3, ext₃ rfl rfl rfl rfl (by simp) (by simp) (by simp)⟩ section variable {f g : ComposableArrows C 4} (app₀ : f.obj' 0 ⟶ g.obj' 0) (app₁ : f.obj' 1 ⟶ g.obj' 1) (app₂ : f.obj' 2 ⟶ g.obj' 2) (app₃ : f.obj' 3 ⟶ g.obj' 3) (app₄ : f.obj' 4 ⟶ g.obj' 4) (w₀ : f.map' 0 1 ≫ app₁ = app₀ ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂ = app₁ ≫ g.map' 1 2) (w₂ : f.map' 2 3 ≫ app₃ = app₂ ≫ g.map' 2 3) (w₃ : f.map' 3 4 ≫ app₄ = app₃ ≫ g.map' 3 4) /-- Constructor for morphisms in `ComposableArrows C 4`. -/ def homMk₄ : f ⟶ g := homMkSucc app₀ (homMk₃ app₁ app₂ app₃ app₄ w₁ w₂ w₃) w₀ @[simp] lemma homMk₄_app_zero : (homMk₄ app₀ app₁ app₂ app₃ app₄ w₀ w₁ w₂ w₃).app 0 = app₀ := rfl @[simp] lemma homMk₄_app_one : (homMk₄ app₀ app₁ app₂ app₃ app₄ w₀ w₁ w₂ w₃).app 1 = app₁ := rfl @[simp] lemma homMk₄_app_two : (homMk₄ app₀ app₁ app₂ app₃ app₄ w₀ w₁ w₂ w₃).app ⟨2, by valid⟩ = app₂ := rfl @[simp] lemma homMk₄_app_three : (homMk₄ app₀ app₁ app₂ app₃ app₄ w₀ w₁ w₂ w₃).app ⟨3, by valid⟩ = app₃ := rfl @[simp] lemma homMk₄_app_four : (homMk₄ app₀ app₁ app₂ app₃ app₄ w₀ w₁ w₂ w₃).app ⟨4, by valid⟩ = app₄ := rfl end @[ext] lemma hom_ext₄ {f g : ComposableArrows C 4} {φ φ' : f ⟶ g} (h₀ : app' φ 0 = app' φ' 0) (h₁ : app' φ 1 = app' φ' 1) (h₂ : app' φ 2 = app' φ' 2) (h₃ : app' φ 3 = app' φ' 3) (h₄ : app' φ 4 = app' φ' 4) : φ = φ' := hom_ext_succ h₀ (hom_ext₃ h₁ h₂ h₃ h₄) lemma map'_inv_eq_inv_map' {n m : ℕ} (h : n+1 ≤ m) {f g : ComposableArrows C m} (app : f.obj' n ≅ g.obj' n) (app' : f.obj' (n+1) ≅ g.obj' (n+1)) (w : f.map' n (n+1) ≫ app'.hom = app.hom ≫ g.map' n (n+1)) : map' g n (n+1) ≫ app'.inv = app.inv ≫ map' f n (n+1) := by rw [← cancel_epi app.hom, ← reassoc_of% w, app'.hom_inv_id, comp_id, app.hom_inv_id_assoc] /-- Constructor for isomorphisms in `ComposableArrows C 4`. -/ @[simps] def isoMk₄ {f g : ComposableArrows C 4} (app₀ : f.obj' 0 ≅ g.obj' 0) (app₁ : f.obj' 1 ≅ g.obj' 1) (app₂ : f.obj' 2 ≅ g.obj' 2) (app₃ : f.obj' 3 ≅ g.obj' 3) (app₄ : f.obj' 4 ≅ g.obj' 4) (w₀ : f.map' 0 1 ≫ app₁.hom = app₀.hom ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂.hom = app₁.hom ≫ g.map' 1 2) (w₂ : f.map' 2 3 ≫ app₃.hom = app₂.hom ≫ g.map' 2 3) (w₃ : f.map' 3 4 ≫ app₄.hom = app₃.hom ≫ g.map' 3 4) : f ≅ g where hom := homMk₄ app₀.hom app₁.hom app₂.hom app₃.hom app₄.hom w₀ w₁ w₂ w₃ inv := homMk₄ app₀.inv app₁.inv app₂.inv app₃.inv app₄.inv (by rw [map'_inv_eq_inv_map' (by valid) app₀ app₁ w₀]) (by rw [map'_inv_eq_inv_map' (by valid) app₁ app₂ w₁]) (by rw [map'_inv_eq_inv_map' (by valid) app₂ app₃ w₂]) (by rw [map'_inv_eq_inv_map' (by valid) app₃ app₄ w₃]) lemma ext₄ {f g : ComposableArrows C 4} (h₀ : f.obj' 0 = g.obj' 0) (h₁ : f.obj' 1 = g.obj' 1) (h₂ : f.obj' 2 = g.obj' 2) (h₃ : f.obj' 3 = g.obj' 3) (h₄ : f.obj' 4 = g.obj' 4) (w₀ : f.map' 0 1 = eqToHom h₀ ≫ g.map' 0 1 ≫ eqToHom h₁.symm) (w₁ : f.map' 1 2 = eqToHom h₁ ≫ g.map' 1 2 ≫ eqToHom h₂.symm) (w₂ : f.map' 2 3 = eqToHom h₂ ≫ g.map' 2 3 ≫ eqToHom h₃.symm) (w₃ : f.map' 3 4 = eqToHom h₃ ≫ g.map' 3 4 ≫ eqToHom h₄.symm) : f = g := ext_succ h₀ (ext₃ h₁ h₂ h₃ h₄ w₁ w₂ w₃) w₀ lemma mk₄_surjective (X : ComposableArrows C 4) : ∃ (X₀ X₁ X₂ X₃ X₄ : C) (f₀ : X₀ ⟶ X₁) (f₁ : X₁ ⟶ X₂) (f₂ : X₂ ⟶ X₃) (f₃ : X₃ ⟶ X₄), X = mk₄ f₀ f₁ f₂ f₃ := ⟨_, _, _, _, _, X.map' 0 1, X.map' 1 2, X.map' 2 3, X.map' 3 4, ext₄ rfl rfl rfl rfl rfl (by simp) (by simp) (by simp) (by simp)⟩ section variable {f g : ComposableArrows C 5} (app₀ : f.obj' 0 ⟶ g.obj' 0) (app₁ : f.obj' 1 ⟶ g.obj' 1) (app₂ : f.obj' 2 ⟶ g.obj' 2) (app₃ : f.obj' 3 ⟶ g.obj' 3) (app₄ : f.obj' 4 ⟶ g.obj' 4) (app₅ : f.obj' 5 ⟶ g.obj' 5) (w₀ : f.map' 0 1 ≫ app₁ = app₀ ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂ = app₁ ≫ g.map' 1 2) (w₂ : f.map' 2 3 ≫ app₃ = app₂ ≫ g.map' 2 3) (w₃ : f.map' 3 4 ≫ app₄ = app₃ ≫ g.map' 3 4) (w₄ : f.map' 4 5 ≫ app₅ = app₄ ≫ g.map' 4 5) /-- Constructor for morphisms in `ComposableArrows C 5`. -/ def homMk₅ : f ⟶ g := homMkSucc app₀ (homMk₄ app₁ app₂ app₃ app₄ app₅ w₁ w₂ w₃ w₄) w₀ @[simp] lemma homMk₅_app_zero : (homMk₅ app₀ app₁ app₂ app₃ app₄ app₅ w₀ w₁ w₂ w₃ w₄).app 0 = app₀ := rfl @[simp] lemma homMk₅_app_one : (homMk₅ app₀ app₁ app₂ app₃ app₄ app₅ w₀ w₁ w₂ w₃ w₄).app 1 = app₁ := rfl @[simp] lemma homMk₅_app_two : (homMk₅ app₀ app₁ app₂ app₃ app₄ app₅ w₀ w₁ w₂ w₃ w₄).app ⟨2, by valid⟩ = app₂ := rfl @[simp] lemma homMk₅_app_three : (homMk₅ app₀ app₁ app₂ app₃ app₄ app₅ w₀ w₁ w₂ w₃ w₄).app ⟨3, by valid⟩ = app₃ := rfl @[simp] lemma homMk₅_app_four : (homMk₅ app₀ app₁ app₂ app₃ app₄ app₅ w₀ w₁ w₂ w₃ w₄).app ⟨4, by valid⟩ = app₄ := rfl @[simp] lemma homMk₅_app_five : (homMk₅ app₀ app₁ app₂ app₃ app₄ app₅ w₀ w₁ w₂ w₃ w₄).app ⟨5, by valid⟩ = app₅ := rfl end @[ext] lemma hom_ext₅ {f g : ComposableArrows C 5} {φ φ' : f ⟶ g} (h₀ : app' φ 0 = app' φ' 0) (h₁ : app' φ 1 = app' φ' 1) (h₂ : app' φ 2 = app' φ' 2) (h₃ : app' φ 3 = app' φ' 3) (h₄ : app' φ 4 = app' φ' 4) (h₅ : app' φ 5 = app' φ' 5) : φ = φ' := hom_ext_succ h₀ (hom_ext₄ h₁ h₂ h₃ h₄ h₅) /-- Constructor for isomorphisms in `ComposableArrows C 5`. -/ @[simps] def isoMk₅ {f g : ComposableArrows C 5} (app₀ : f.obj' 0 ≅ g.obj' 0) (app₁ : f.obj' 1 ≅ g.obj' 1) (app₂ : f.obj' 2 ≅ g.obj' 2) (app₃ : f.obj' 3 ≅ g.obj' 3) (app₄ : f.obj' 4 ≅ g.obj' 4) (app₅ : f.obj' 5 ≅ g.obj' 5) (w₀ : f.map' 0 1 ≫ app₁.hom = app₀.hom ≫ g.map' 0 1) (w₁ : f.map' 1 2 ≫ app₂.hom = app₁.hom ≫ g.map' 1 2) (w₂ : f.map' 2 3 ≫ app₃.hom = app₂.hom ≫ g.map' 2 3) (w₃ : f.map' 3 4 ≫ app₄.hom = app₃.hom ≫ g.map' 3 4) (w₄ : f.map' 4 5 ≫ app₅.hom = app₄.hom ≫ g.map' 4 5) : f ≅ g where hom := homMk₅ app₀.hom app₁.hom app₂.hom app₃.hom app₄.hom app₅.hom w₀ w₁ w₂ w₃ w₄ inv := homMk₅ app₀.inv app₁.inv app₂.inv app₃.inv app₄.inv app₅.inv (by rw [map'_inv_eq_inv_map' (by valid) app₀ app₁ w₀]) (by rw [map'_inv_eq_inv_map' (by valid) app₁ app₂ w₁]) (by rw [map'_inv_eq_inv_map' (by valid) app₂ app₃ w₂]) (by rw [map'_inv_eq_inv_map' (by valid) app₃ app₄ w₃]) (by rw [map'_inv_eq_inv_map' (by valid) app₄ app₅ w₄]) lemma ext₅ {f g : ComposableArrows C 5} (h₀ : f.obj' 0 = g.obj' 0) (h₁ : f.obj' 1 = g.obj' 1) (h₂ : f.obj' 2 = g.obj' 2) (h₃ : f.obj' 3 = g.obj' 3) (h₄ : f.obj' 4 = g.obj' 4) (h₅ : f.obj' 5 = g.obj' 5) (w₀ : f.map' 0 1 = eqToHom h₀ ≫ g.map' 0 1 ≫ eqToHom h₁.symm) (w₁ : f.map' 1 2 = eqToHom h₁ ≫ g.map' 1 2 ≫ eqToHom h₂.symm) (w₂ : f.map' 2 3 = eqToHom h₂ ≫ g.map' 2 3 ≫ eqToHom h₃.symm) (w₃ : f.map' 3 4 = eqToHom h₃ ≫ g.map' 3 4 ≫ eqToHom h₄.symm) (w₄ : f.map' 4 5 = eqToHom h₄ ≫ g.map' 4 5 ≫ eqToHom h₅.symm) : f = g := ext_succ h₀ (ext₄ h₁ h₂ h₃ h₄ h₅ w₁ w₂ w₃ w₄) w₀ lemma mk₅_surjective (X : ComposableArrows C 5) : ∃ (X₀ X₁ X₂ X₃ X₄ X₅ : C) (f₀ : X₀ ⟶ X₁) (f₁ : X₁ ⟶ X₂) (f₂ : X₂ ⟶ X₃) (f₃ : X₃ ⟶ X₄) (f₄ : X₄ ⟶ X₅), X = mk₅ f₀ f₁ f₂ f₃ f₄ := ⟨_, _, _, _, _, _, X.map' 0 1, X.map' 1 2, X.map' 2 3, X.map' 3 4, X.map' 4 5, ext₅ rfl rfl rfl rfl rfl rfl (by simp) (by simp) (by simp) (by simp) (by simp)⟩ /-- The `i`th arrow of `F : ComposableArrows C n`. -/ def arrow (i : ℕ) (hi : i < n := by valid) : ComposableArrows C 1 := mk₁ (F.map' i (i + 1)) section mkOfObjOfMapSucc
variable (obj : Fin (n + 1) → C) (mapSucc : ∀ (i : Fin n), obj i.castSucc ⟶ obj i.succ) lemma mkOfObjOfMapSucc_exists : ∃ (F : ComposableArrows C n) (e : ∀ i, F.obj i ≅ obj i), ∀ (i : ℕ) (hi : i < n), mapSucc ⟨i, hi⟩ = (e ⟨i, _⟩).inv ≫ F.map' i (i + 1) ≫ (e ⟨i + 1, _⟩).hom := by
Mathlib/CategoryTheory/ComposableArrows.lean
835
839
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import Mathlib.Topology.UniformSpace.UniformEmbedding import Mathlib.Topology.UniformSpace.Equiv /-! # Abstract theory of Hausdorff completions of uniform spaces This file characterizes Hausdorff completions of a uniform space α as complete Hausdorff spaces equipped with a map from α which has dense image and induce the original uniform structure on α. Assuming these properties we "extend" uniformly continuous maps from α to complete Hausdorff spaces to the completions of α. This is the universal property expected from a completion. It is then used to extend uniformly continuous maps from α to α' to maps between completions of α and α'. This file does not construct any such completion, it only study consequences of their existence. The first advantage is that formal properties are clearly highlighted without interference from construction details. The second advantage is that this framework can then be used to compare different completion constructions. See `Topology/UniformSpace/CompareReals` for an example. Of course the comparison comes from the universal property as usual. A general explicit construction of completions is done in `UniformSpace/Completion`, leading to a functor from uniform spaces to complete Hausdorff uniform spaces that is left adjoint to the inclusion, see `UniformSpace/UniformSpaceCat` for the category packaging. ## Implementation notes A tiny technical advantage of using a characteristic predicate such as the properties listed in `AbstractCompletion` instead of stating the universal property is that the universal property derived from the predicate is more universe polymorphic. ## References We don't know any traditional text discussing this. Real world mathematics simply silently identify the results of any two constructions that lead to something one could reasonably call a completion. ## Tags uniform spaces, completion, universal property -/ noncomputable section open Filter Set Function universe u /-- A completion of `α` is the data of a complete separated uniform space (from the same universe) and a map from `α` with dense range and inducing the original uniform structure on `α`. -/ structure AbstractCompletion (α : Type u) [UniformSpace α] where /-- The underlying space of the completion. -/ space : Type u /-- A map from a space to its completion. -/ coe : α → space /-- The completion carries a uniform structure. -/ uniformStruct : UniformSpace space /-- The completion is complete. -/ complete : CompleteSpace space /-- The completion is a T₀ space. -/ separation : T0Space space /-- The map into the completion is uniform-inducing. -/ isUniformInducing : IsUniformInducing coe /-- The map into the completion has dense range. -/ dense : DenseRange coe attribute [local instance] AbstractCompletion.uniformStruct AbstractCompletion.complete AbstractCompletion.separation namespace AbstractCompletion variable {α : Type*} [UniformSpace α] (pkg : AbstractCompletion α) local notation "hatα" => pkg.space local notation "ι" => pkg.coe /-- If `α` is complete, then it is an abstract completion of itself. -/ def ofComplete [T0Space α] [CompleteSpace α] : AbstractCompletion α := mk α id inferInstance inferInstance inferInstance .id denseRange_id theorem closure_range : closure (range ι) = univ := pkg.dense.closure_range theorem isDenseInducing : IsDenseInducing ι := ⟨pkg.isUniformInducing.isInducing, pkg.dense⟩ theorem uniformContinuous_coe : UniformContinuous ι := IsUniformInducing.uniformContinuous pkg.isUniformInducing theorem continuous_coe : Continuous ι := pkg.uniformContinuous_coe.continuous @[elab_as_elim] theorem induction_on {p : hatα → Prop} (a : hatα) (hp : IsClosed { a | p a }) (ih : ∀ a, p (ι a)) : p a := isClosed_property pkg.dense hp ih a variable {β : Type*} protected theorem funext [TopologicalSpace β] [T2Space β] {f g : hatα → β} (hf : Continuous f) (hg : Continuous g) (h : ∀ a, f (ι a) = g (ι a)) : f = g := funext fun a => pkg.induction_on a (isClosed_eq hf hg) h variable [UniformSpace β] section Extend /-- Extension of maps to completions -/ protected def extend (f : α → β) : hatα → β := open scoped Classical in if UniformContinuous f then pkg.isDenseInducing.extend f else fun x => f (pkg.dense.some x) variable {f : α → β} theorem extend_def (hf : UniformContinuous f) : pkg.extend f = pkg.isDenseInducing.extend f := if_pos hf theorem extend_coe [T2Space β] (hf : UniformContinuous f) (a : α) : (pkg.extend f) (ι a) = f a := by rw [pkg.extend_def hf] exact pkg.isDenseInducing.extend_eq hf.continuous a variable [CompleteSpace β] theorem uniformContinuous_extend : UniformContinuous (pkg.extend f) := by by_cases hf : UniformContinuous f · rw [pkg.extend_def hf] exact uniformContinuous_uniformly_extend pkg.isUniformInducing pkg.dense hf · unfold AbstractCompletion.extend rw [if_neg hf] exact uniformContinuous_of_const fun a b => by congr 1 theorem continuous_extend : Continuous (pkg.extend f) := pkg.uniformContinuous_extend.continuous variable [T0Space β] theorem extend_unique (hf : UniformContinuous f) {g : hatα → β} (hg : UniformContinuous g) (h : ∀ a : α, f a = g (ι a)) : pkg.extend f = g := by apply pkg.funext pkg.continuous_extend hg.continuous simpa only [pkg.extend_coe hf] using h @[simp] theorem extend_comp_coe {f : hatα → β} (hf : UniformContinuous f) : pkg.extend (f ∘ ι) = f := funext fun x => pkg.induction_on x (isClosed_eq pkg.continuous_extend hf.continuous) fun y => pkg.extend_coe (hf.comp <| pkg.uniformContinuous_coe) y end Extend section MapSec variable (pkg' : AbstractCompletion β)
local notation "hatβ" => pkg'.space local notation "ι'" => pkg'.coe
Mathlib/Topology/UniformSpace/AbstractCompletion.lean
158
161
/- 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.Order.Group.Finset import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Algebra.Polynomial.Eval.SMul import Mathlib.Algebra.Polynomial.Roots import Mathlib.RingTheory.EuclideanDomain import Mathlib.RingTheory.UniqueFactorizationDomain.NormalizedFactors /-! # Theory of univariate polynomials This file starts looking like the ring theory of $R[X]$ -/ noncomputable section open Polynomial namespace Polynomial universe u v w y z variable {R : Type u} {S : Type v} {k : Type y} {A : Type z} {a b : R} {n : ℕ} section CommRing variable [CommRing R] theorem rootMultiplicity_sub_one_le_derivative_rootMultiplicity_of_ne_zero (p : R[X]) (t : R) (hnezero : derivative p ≠ 0) : p.rootMultiplicity t - 1 ≤ p.derivative.rootMultiplicity t := (le_rootMultiplicity_iff hnezero).2 <| pow_sub_one_dvd_derivative_of_pow_dvd (p.pow_rootMultiplicity_dvd t) theorem derivative_rootMultiplicity_of_root_of_mem_nonZeroDivisors {p : R[X]} {t : R} (hpt : Polynomial.IsRoot p t) (hnzd : (p.rootMultiplicity t : R) ∈ nonZeroDivisors R) : (derivative p).rootMultiplicity t = p.rootMultiplicity t - 1 := by by_cases h : p = 0 · simp only [h, map_zero, rootMultiplicity_zero] obtain ⟨g, hp, hndvd⟩ := p.exists_eq_pow_rootMultiplicity_mul_and_not_dvd h t set m := p.rootMultiplicity t have hm : m - 1 + 1 = m := Nat.sub_add_cancel <| (rootMultiplicity_pos h).2 hpt have hndvd : ¬(X - C t) ^ m ∣ derivative p := by rw [hp, derivative_mul, dvd_add_left (dvd_mul_right _ _), derivative_X_sub_C_pow, ← hm, pow_succ, hm, mul_comm (C _), mul_assoc, dvd_cancel_left_mem_nonZeroDivisors (monic_X_sub_C t |>.pow _ |>.mem_nonZeroDivisors)] rw [dvd_iff_isRoot, IsRoot] at hndvd ⊢ rwa [eval_mul, eval_C, mul_left_mem_nonZeroDivisors_eq_zero_iff hnzd] have hnezero : derivative p ≠ 0 := fun h ↦ hndvd (by rw [h]; exact dvd_zero _) exact le_antisymm (by rwa [rootMultiplicity_le_iff hnezero, hm]) (rootMultiplicity_sub_one_le_derivative_rootMultiplicity_of_ne_zero _ t hnezero) theorem isRoot_iterate_derivative_of_lt_rootMultiplicity {p : R[X]} {t : R} {n : ℕ} (hn : n < p.rootMultiplicity t) : (derivative^[n] p).IsRoot t := dvd_iff_isRoot.mp <| (dvd_pow_self _ <| Nat.sub_ne_zero_of_lt hn).trans (pow_sub_dvd_iterate_derivative_of_pow_dvd _ <| p.pow_rootMultiplicity_dvd t) open Finset in theorem eval_iterate_derivative_rootMultiplicity {p : R[X]} {t : R} : (derivative^[p.rootMultiplicity t] p).eval t = (p.rootMultiplicity t).factorial • (p /ₘ (X - C t) ^ p.rootMultiplicity t).eval t := by set m := p.rootMultiplicity t with hm conv_lhs => rw [← p.pow_mul_divByMonic_rootMultiplicity_eq t, ← hm] rw [iterate_derivative_mul, eval_finset_sum, sum_eq_single_of_mem _ (mem_range.mpr m.succ_pos)] · rw [m.choose_zero_right, one_smul, eval_mul, m.sub_zero, iterate_derivative_X_sub_pow_self, eval_natCast, nsmul_eq_mul]; rfl · intro b hb hb0 rw [iterate_derivative_X_sub_pow, eval_smul, eval_mul, eval_smul, eval_pow, Nat.sub_sub_self (mem_range_succ_iff.mp hb), eval_sub, eval_X, eval_C, sub_self, zero_pow hb0, smul_zero, zero_mul, smul_zero] theorem lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hroot : ∀ m ≤ n, (derivative^[m] p).IsRoot t) (hnzd : (n.factorial : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t := by by_contra! h' replace hroot := hroot _ h' simp only [IsRoot, eval_iterate_derivative_rootMultiplicity] at hroot obtain ⟨q, hq⟩ := Nat.cast_dvd_cast (α := R) <| Nat.factorial_dvd_factorial h' rw [hq, mul_mem_nonZeroDivisors] at hnzd rw [nsmul_eq_mul, mul_left_mem_nonZeroDivisors_eq_zero_iff hnzd.1] at hroot exact eval_divByMonic_pow_rootMultiplicity_ne_zero t h hroot theorem lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors' {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hroot : ∀ m ≤ n, (derivative^[m] p).IsRoot t) (hnzd : ∀ m ≤ n, m ≠ 0 → (m : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t := by apply lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors h hroot clear hroot induction n with | zero => simp only [Nat.factorial_zero, Nat.cast_one] exact Submonoid.one_mem _ | succ n ih => rw [Nat.factorial_succ, Nat.cast_mul, mul_mem_nonZeroDivisors] exact ⟨hnzd _ le_rfl n.succ_ne_zero, ih fun m h ↦ hnzd m (h.trans n.le_succ)⟩ theorem lt_rootMultiplicity_iff_isRoot_iterate_derivative_of_mem_nonZeroDivisors {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hnzd : (n.factorial : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t ↔ ∀ m ≤ n, (derivative^[m] p).IsRoot t := ⟨fun hn _ hm ↦ isRoot_iterate_derivative_of_lt_rootMultiplicity <| hm.trans_lt hn, fun hr ↦ lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors h hr hnzd⟩ theorem lt_rootMultiplicity_iff_isRoot_iterate_derivative_of_mem_nonZeroDivisors' {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hnzd : ∀ m ≤ n, m ≠ 0 → (m : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t ↔ ∀ m ≤ n, (derivative^[m] p).IsRoot t := ⟨fun hn _ hm ↦ isRoot_iterate_derivative_of_lt_rootMultiplicity <| Nat.lt_of_le_of_lt hm hn, fun hr ↦ lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors' h hr hnzd⟩ theorem one_lt_rootMultiplicity_iff_isRoot_iterate_derivative {p : R[X]} {t : R} (h : p ≠ 0) : 1 < p.rootMultiplicity t ↔ ∀ m ≤ 1, (derivative^[m] p).IsRoot t := lt_rootMultiplicity_iff_isRoot_iterate_derivative_of_mem_nonZeroDivisors h (by rw [Nat.factorial_one, Nat.cast_one]; exact Submonoid.one_mem _) theorem one_lt_rootMultiplicity_iff_isRoot {p : R[X]} {t : R} (h : p ≠ 0) : 1 < p.rootMultiplicity t ↔ p.IsRoot t ∧ (derivative p).IsRoot t := by rw [one_lt_rootMultiplicity_iff_isRoot_iterate_derivative h] refine ⟨fun h ↦ ⟨h 0 (by norm_num), h 1 (by norm_num)⟩, fun ⟨h0, h1⟩ m hm ↦ ?_⟩ obtain (_|_|m) := m exacts [h0, h1, by omega] end CommRing section IsDomain variable [CommRing R] [IsDomain R] theorem one_lt_rootMultiplicity_iff_isRoot_gcd [GCDMonoid R[X]] {p : R[X]} {t : R} (h : p ≠ 0) : 1 < p.rootMultiplicity t ↔ (gcd p (derivative p)).IsRoot t := by simp_rw [one_lt_rootMultiplicity_iff_isRoot h, ← dvd_iff_isRoot, dvd_gcd_iff] theorem derivative_rootMultiplicity_of_root [CharZero R] {p : R[X]} {t : R} (hpt : p.IsRoot t) : p.derivative.rootMultiplicity t = p.rootMultiplicity t - 1 := by by_cases h : p = 0 · rw [h, map_zero, rootMultiplicity_zero] exact derivative_rootMultiplicity_of_root_of_mem_nonZeroDivisors hpt <| mem_nonZeroDivisors_of_ne_zero <| Nat.cast_ne_zero.2 ((rootMultiplicity_pos h).2 hpt).ne' theorem rootMultiplicity_sub_one_le_derivative_rootMultiplicity [CharZero R] (p : R[X]) (t : R) : p.rootMultiplicity t - 1 ≤ p.derivative.rootMultiplicity t := by by_cases h : p.IsRoot t · exact (derivative_rootMultiplicity_of_root h).symm.le · rw [rootMultiplicity_eq_zero h, zero_tsub] exact zero_le _ theorem lt_rootMultiplicity_of_isRoot_iterate_derivative [CharZero R] {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hroot : ∀ m ≤ n, (derivative^[m] p).IsRoot t) : n < p.rootMultiplicity t := lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors h hroot <| mem_nonZeroDivisors_of_ne_zero <| Nat.cast_ne_zero.2 <| Nat.factorial_ne_zero n theorem lt_rootMultiplicity_iff_isRoot_iterate_derivative [CharZero R] {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) : n < p.rootMultiplicity t ↔ ∀ m ≤ n, (derivative^[m] p).IsRoot t := ⟨fun hn _ hm ↦ isRoot_iterate_derivative_of_lt_rootMultiplicity <| Nat.lt_of_le_of_lt hm hn, fun hr ↦ lt_rootMultiplicity_of_isRoot_iterate_derivative h hr⟩ /-- A sufficient condition for the set of roots of a nonzero polynomial `f` to be a subset of the set of roots of `g` is that `f` divides `f.derivative * g`. Over an algebraically closed field of characteristic zero, this is also a necessary condition. See `isRoot_of_isRoot_iff_dvd_derivative_mul` -/ theorem isRoot_of_isRoot_of_dvd_derivative_mul [CharZero R] {f g : R[X]} (hf0 : f ≠ 0) (hfd : f ∣ f.derivative * g) {a : R} (haf : f.IsRoot a) : g.IsRoot a := by rcases hfd with ⟨r, hr⟩ have hdf0 : derivative f ≠ 0 := by contrapose! haf rw [eq_C_of_derivative_eq_zero haf] at hf0 ⊢ exact not_isRoot_C _ _ <| C_ne_zero.mp hf0 by_contra hg have hdfg0 : f.derivative * g ≠ 0 := mul_ne_zero hdf0 (by rintro rfl; simp at hg) have hr' := congr_arg (rootMultiplicity a) hr rw [rootMultiplicity_mul hdfg0, derivative_rootMultiplicity_of_root haf, rootMultiplicity_eq_zero hg, add_zero, rootMultiplicity_mul (hr ▸ hdfg0), add_comm, Nat.sub_eq_iff_eq_add (Nat.succ_le_iff.2 ((rootMultiplicity_pos hf0).2 haf))] at hr' omega section NormalizationMonoid variable [NormalizationMonoid R] instance instNormalizationMonoid : NormalizationMonoid R[X] where normUnit p := ⟨C ↑(normUnit p.leadingCoeff), C ↑(normUnit p.leadingCoeff)⁻¹, by rw [← RingHom.map_mul, Units.mul_inv, C_1], by rw [← RingHom.map_mul, Units.inv_mul, C_1]⟩ normUnit_zero := Units.ext (by simp)
normUnit_mul hp0 hq0 := Units.ext
Mathlib/Algebra/Polynomial/FieldDivision.lean
201
202
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yaël Dillies -/ import Mathlib.Analysis.Normed.Group.Pointwise import Mathlib.Analysis.NormedSpace.Real /-! # Properties of pointwise scalar multiplication of sets in normed spaces. We explore the relationships between scalar multiplication of sets in vector spaces, and the norm. Notably, we express arbitrary balls as rescaling of other balls, and we show that the multiplication of bounded sets remain bounded. -/ open Metric Set open Pointwise Topology variable {𝕜 E : Type*} section SMulZeroClass variable [SeminormedAddCommGroup 𝕜] [SeminormedAddCommGroup E] variable [SMulZeroClass 𝕜 E] [IsBoundedSMul 𝕜 E] theorem ediam_smul_le (c : 𝕜) (s : Set E) : EMetric.diam (c • s) ≤ ‖c‖₊ • EMetric.diam s := (lipschitzWith_smul c).ediam_image_le s end SMulZeroClass section DivisionRing variable [NormedDivisionRing 𝕜] [SeminormedAddCommGroup E] variable [Module 𝕜 E] [IsBoundedSMul 𝕜 E] theorem ediam_smul₀ (c : 𝕜) (s : Set E) : EMetric.diam (c • s) = ‖c‖₊ • EMetric.diam s := by refine le_antisymm (ediam_smul_le c s) ?_ obtain rfl | hc := eq_or_ne c 0 · obtain rfl | hs := s.eq_empty_or_nonempty · simp simp [zero_smul_set hs, ← Set.singleton_zero] · have := (lipschitzWith_smul c⁻¹).ediam_image_le (c • s) rwa [← smul_eq_mul, ← ENNReal.smul_def, Set.image_smul, inv_smul_smul₀ hc s, nnnorm_inv, le_inv_smul_iff_of_pos (nnnorm_pos.2 hc)] at this theorem diam_smul₀ (c : 𝕜) (x : Set E) : diam (c • x) = ‖c‖ * diam x := by simp_rw [diam, ediam_smul₀, ENNReal.toReal_smul, NNReal.smul_def, coe_nnnorm, smul_eq_mul] theorem infEdist_smul₀ {c : 𝕜} (hc : c ≠ 0) (s : Set E) (x : E) : EMetric.infEdist (c • x) (c • s) = ‖c‖₊ • EMetric.infEdist x s := by simp_rw [EMetric.infEdist] have : Function.Surjective ((c • ·) : E → E) := Function.RightInverse.surjective (smul_inv_smul₀ hc) trans ⨅ (y) (_ : y ∈ s), ‖c‖₊ • edist x y · refine (this.iInf_congr _ fun y => ?_).symm simp_rw [smul_mem_smul_set_iff₀ hc, edist_smul₀] · have : (‖c‖₊ : ENNReal) ≠ 0 := by simp [hc] simp_rw [ENNReal.smul_def, smul_eq_mul, ENNReal.mul_iInf_of_ne this ENNReal.coe_ne_top] theorem infDist_smul₀ {c : 𝕜} (hc : c ≠ 0) (s : Set E) (x : E) : Metric.infDist (c • x) (c • s) = ‖c‖ * Metric.infDist x s := by simp_rw [Metric.infDist, infEdist_smul₀ hc s, ENNReal.toReal_smul, NNReal.smul_def, coe_nnnorm, smul_eq_mul] end DivisionRing variable [NormedField 𝕜] section SeminormedAddCommGroup variable [SeminormedAddCommGroup E] [NormedSpace 𝕜 E] theorem smul_ball {c : 𝕜} (hc : c ≠ 0) (x : E) (r : ℝ) : c • ball x r = ball (c • x) (‖c‖ * r) := by ext y rw [mem_smul_set_iff_inv_smul_mem₀ hc] conv_lhs => rw [← inv_smul_smul₀ hc x] simp [← div_eq_inv_mul, div_lt_iff₀ (norm_pos_iff.2 hc), mul_comm _ r, dist_smul₀] theorem smul_unitBall {c : 𝕜} (hc : c ≠ 0) : c • ball (0 : E) (1 : ℝ) = ball (0 : E) ‖c‖ := by rw [_root_.smul_ball hc, smul_zero, mul_one] theorem smul_sphere' {c : 𝕜} (hc : c ≠ 0) (x : E) (r : ℝ) : c • sphere x r = sphere (c • x) (‖c‖ * r) := by ext y rw [mem_smul_set_iff_inv_smul_mem₀ hc] conv_lhs => rw [← inv_smul_smul₀ hc x] simp only [mem_sphere, dist_smul₀, norm_inv, ← div_eq_inv_mul, div_eq_iff (norm_pos_iff.2 hc).ne', mul_comm r] theorem smul_closedBall' {c : 𝕜} (hc : c ≠ 0) (x : E) (r : ℝ) : c • closedBall x r = closedBall (c • x) (‖c‖ * r) := by simp only [← ball_union_sphere, Set.smul_set_union, _root_.smul_ball hc, smul_sphere' hc] theorem set_smul_sphere_zero {s : Set 𝕜} (hs : 0 ∉ s) (r : ℝ) : s • sphere (0 : E) r = (‖·‖) ⁻¹' ((‖·‖ * r) '' s) := calc s • sphere (0 : E) r = ⋃ c ∈ s, c • sphere (0 : E) r := iUnion_smul_left_image.symm _ = ⋃ c ∈ s, sphere (0 : E) (‖c‖ * r) := iUnion₂_congr fun c hc ↦ by rw [smul_sphere' (ne_of_mem_of_not_mem hc hs), smul_zero] _ = (‖·‖) ⁻¹' ((‖·‖ * r) '' s) := by ext; simp [eq_comm] /-- Image of a bounded set in a normed space under scalar multiplication by a constant is bounded. See also `Bornology.IsBounded.smul` for a similar lemma about an isometric action. -/ theorem Bornology.IsBounded.smul₀ {s : Set E} (hs : IsBounded s) (c : 𝕜) : IsBounded (c • s) := (lipschitzWith_smul c).isBounded_image hs /-- If `s` is a bounded set, then for small enough `r`, the set `{x} + r • s` is contained in any fixed neighborhood of `x`. -/ theorem eventually_singleton_add_smul_subset {x : E} {s : Set E} (hs : Bornology.IsBounded s) {u : Set E} (hu : u ∈ 𝓝 x) : ∀ᶠ r in 𝓝 (0 : 𝕜), {x} + r • s ⊆ u := by obtain ⟨ε, εpos, hε⟩ : ∃ ε : ℝ, 0 < ε ∧ closedBall x ε ⊆ u := nhds_basis_closedBall.mem_iff.1 hu obtain ⟨R, Rpos, hR⟩ : ∃ R : ℝ, 0 < R ∧ s ⊆ closedBall 0 R := hs.subset_closedBall_lt 0 0 have : Metric.closedBall (0 : 𝕜) (ε / R) ∈ 𝓝 (0 : 𝕜) := closedBall_mem_nhds _ (div_pos εpos Rpos) filter_upwards [this] with r hr simp only [image_add_left, singleton_add] intro y hy obtain ⟨z, zs, hz⟩ : ∃ z : E, z ∈ s ∧ r • z = -x + y := by simpa [mem_smul_set] using hy have I : ‖r • z‖ ≤ ε := calc ‖r • z‖ = ‖r‖ * ‖z‖ := norm_smul _ _ _ ≤ ε / R * R := (mul_le_mul (mem_closedBall_zero_iff.1 hr) (mem_closedBall_zero_iff.1 (hR zs)) (norm_nonneg _) (div_pos εpos Rpos).le) _ = ε := by field_simp have : y = x + r • z := by simp only [hz, add_neg_cancel_left] apply hε simpa only [this, dist_eq_norm, add_sub_cancel_left, mem_closedBall] using I variable [NormedSpace ℝ E] {x y z : E} {δ ε : ℝ} /-- In a real normed space, the image of the unit ball under scalar multiplication by a positive constant `r` is the ball of radius `r`. -/ theorem smul_unitBall_of_pos {r : ℝ} (hr : 0 < r) : r • ball (0 : E) 1 = ball (0 : E) r := by rw [smul_unitBall hr.ne', Real.norm_of_nonneg hr.le] lemma Ioo_smul_sphere_zero {a b r : ℝ} (ha : 0 ≤ a) (hr : 0 < r) : Ioo a b • sphere (0 : E) r = ball 0 (b * r) \ closedBall 0 (a * r) := by have : EqOn (‖·‖) id (Ioo a b) := fun x hx ↦ abs_of_pos (ha.trans_lt hx.1) rw [set_smul_sphere_zero (by simp [ha.not_lt]), ← image_image (· * r), this.image_eq, image_id, image_mul_right_Ioo _ _ hr] ext x; simp [and_comm] -- This is also true for `ℚ`-normed spaces theorem exists_dist_eq (x z : E) {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : ∃ y, dist x y = b * dist x z ∧ dist y z = a * dist x z := by use a • x + b • z nth_rw 1 [← one_smul ℝ x] nth_rw 4 [← one_smul ℝ z] simp [dist_eq_norm, ← hab, add_smul, ← smul_sub, norm_smul_of_nonneg, ha, hb] theorem exists_dist_le_le (hδ : 0 ≤ δ) (hε : 0 ≤ ε) (h : dist x z ≤ ε + δ) : ∃ y, dist x y ≤ δ ∧ dist y z ≤ ε := by obtain rfl | hε' := hε.eq_or_lt · exact ⟨z, by rwa [zero_add] at h, (dist_self _).le⟩ have hεδ := add_pos_of_pos_of_nonneg hε' hδ refine (exists_dist_eq x z (div_nonneg hε <| add_nonneg hε hδ) (div_nonneg hδ <| add_nonneg hε hδ) <| by rw [← add_div, div_self hεδ.ne']).imp fun y hy => ?_ rw [hy.1, hy.2, div_mul_comm, div_mul_comm ε] rw [← div_le_one hεδ] at h exact ⟨mul_le_of_le_one_left hδ h, mul_le_of_le_one_left hε h⟩ -- This is also true for `ℚ`-normed spaces theorem exists_dist_le_lt (hδ : 0 ≤ δ) (hε : 0 < ε) (h : dist x z < ε + δ) : ∃ y, dist x y ≤ δ ∧ dist y z < ε := by refine (exists_dist_eq x z (div_nonneg hε.le <| add_nonneg hε.le hδ) (div_nonneg hδ <| add_nonneg hε.le hδ) <| by rw [← add_div, div_self (add_pos_of_pos_of_nonneg hε hδ).ne']).imp fun y hy => ?_ rw [hy.1, hy.2, div_mul_comm, div_mul_comm ε] rw [← div_lt_one (add_pos_of_pos_of_nonneg hε hδ)] at h exact ⟨mul_le_of_le_one_left hδ h.le, mul_lt_of_lt_one_left hε h⟩ -- This is also true for `ℚ`-normed spaces theorem exists_dist_lt_le (hδ : 0 < δ) (hε : 0 ≤ ε) (h : dist x z < ε + δ) : ∃ y, dist x y < δ ∧ dist y z ≤ ε := by obtain ⟨y, yz, xy⟩ := exists_dist_le_lt hε hδ (show dist z x < δ + ε by simpa only [dist_comm, add_comm] using h) exact ⟨y, by simp [dist_comm x y, dist_comm y z, *]⟩ -- This is also true for `ℚ`-normed spaces theorem exists_dist_lt_lt (hδ : 0 < δ) (hε : 0 < ε) (h : dist x z < ε + δ) : ∃ y, dist x y < δ ∧ dist y z < ε := by refine (exists_dist_eq x z (div_nonneg hε.le <| add_nonneg hε.le hδ.le) (div_nonneg hδ.le <| add_nonneg hε.le hδ.le) <| by rw [← add_div, div_self (add_pos hε hδ).ne']).imp fun y hy => ?_ rw [hy.1, hy.2, div_mul_comm, div_mul_comm ε] rw [← div_lt_one (add_pos hε hδ)] at h exact ⟨mul_lt_of_lt_one_left hδ h, mul_lt_of_lt_one_left hε h⟩ -- This is also true for `ℚ`-normed spaces theorem disjoint_ball_ball_iff (hδ : 0 < δ) (hε : 0 < ε) : Disjoint (ball x δ) (ball y ε) ↔ δ + ε ≤ dist x y := by refine ⟨fun h => le_of_not_lt fun hxy => ?_, ball_disjoint_ball⟩ rw [add_comm] at hxy obtain ⟨z, hxz, hzy⟩ := exists_dist_lt_lt hδ hε hxy rw [dist_comm] at hxz exact h.le_bot ⟨hxz, hzy⟩ -- This is also true for `ℚ`-normed spaces theorem disjoint_ball_closedBall_iff (hδ : 0 < δ) (hε : 0 ≤ ε) : Disjoint (ball x δ) (closedBall y ε) ↔ δ + ε ≤ dist x y := by refine ⟨fun h => le_of_not_lt fun hxy => ?_, ball_disjoint_closedBall⟩ rw [add_comm] at hxy obtain ⟨z, hxz, hzy⟩ := exists_dist_lt_le hδ hε hxy rw [dist_comm] at hxz exact h.le_bot ⟨hxz, hzy⟩ -- This is also true for `ℚ`-normed spaces theorem disjoint_closedBall_ball_iff (hδ : 0 ≤ δ) (hε : 0 < ε) : Disjoint (closedBall x δ) (ball y ε) ↔ δ + ε ≤ dist x y := by rw [disjoint_comm, disjoint_ball_closedBall_iff hε hδ, add_comm, dist_comm] theorem disjoint_closedBall_closedBall_iff (hδ : 0 ≤ δ) (hε : 0 ≤ ε) : Disjoint (closedBall x δ) (closedBall y ε) ↔ δ + ε < dist x y := by refine ⟨fun h => lt_of_not_ge fun hxy => ?_, closedBall_disjoint_closedBall⟩ rw [add_comm] at hxy obtain ⟨z, hxz, hzy⟩ := exists_dist_le_le hδ hε hxy rw [dist_comm] at hxz exact h.le_bot ⟨hxz, hzy⟩
open EMetric ENNReal @[simp] theorem infEdist_thickening (hδ : 0 < δ) (s : Set E) (x : E) : infEdist x (thickening δ s) = infEdist x s - ENNReal.ofReal δ := by obtain hs | hs := lt_or_le (infEdist x s) (ENNReal.ofReal δ)
Mathlib/Analysis/NormedSpace/Pointwise.lean
227
233
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Yury Kudryashov -/ import Mathlib.Data.List.Forall2 import Mathlib.Data.List.Lex import Mathlib.Logic.Function.Iterate import Mathlib.Logic.Relation /-! # Relation chain This file provides basic results about `List.Chain` (definition in `Data.List.Defs`). A list `[a₂, ..., aₙ]` is a `Chain` starting at `a₁` with respect to the relation `r` if `r a₁ a₂` and `r a₂ a₃` and ... and `r aₙ₋₁ aₙ`. We write it `Chain r a₁ [a₂, ..., aₙ]`. A graph-specialized version is in development and will hopefully be added under `combinatorics.` sometime soon. -/ assert_not_imported Mathlib.Algebra.Order.Group.Nat universe u v open Nat namespace List variable {α : Type u} {β : Type v} {R r : α → α → Prop} {l l₁ l₂ : List α} {a b : α} mk_iff_of_inductive_prop List.Chain List.chain_iff theorem Chain.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {a : α} {l : List α} : Chain R a l ↔ Chain S a l := ⟨Chain.imp fun a b => (H a b).1, Chain.imp fun a b => (H a b).2⟩ theorem Chain.iff_mem {a : α} {l : List α} : Chain R a l ↔ Chain (fun x y => x ∈ a :: l ∧ y ∈ l ∧ R x y) a l := ⟨fun p => by induction p with | nil => exact nil | @cons _ _ _ r _ IH => constructor · exact ⟨mem_cons_self, mem_cons_self, r⟩ · exact IH.imp fun a b ⟨am, bm, h⟩ => ⟨mem_cons_of_mem _ am, mem_cons_of_mem _ bm, h⟩, Chain.imp fun _ _ h => h.2.2⟩ theorem chain_singleton {a b : α} : Chain R a [b] ↔ R a b := by simp only [chain_cons, Chain.nil, and_true] theorem chain_split {a b : α} {l₁ l₂ : List α} : Chain R a (l₁ ++ b :: l₂) ↔ Chain R a (l₁ ++ [b]) ∧ Chain R b l₂ := by induction' l₁ with x l₁ IH generalizing a <;> simp only [*, nil_append, cons_append, Chain.nil, chain_cons, and_true, and_assoc] @[simp] theorem chain_append_cons_cons {a b c : α} {l₁ l₂ : List α} : Chain R a (l₁ ++ b :: c :: l₂) ↔ Chain R a (l₁ ++ [b]) ∧ R b c ∧ Chain R c l₂ := by rw [chain_split, chain_cons] theorem chain_iff_forall₂ : ∀ {a : α} {l : List α}, Chain R a l ↔ l = [] ∨ Forall₂ R (a :: dropLast l) l | a, [] => by simp | a, b :: l => by by_cases h : l = [] <;> simp [@chain_iff_forall₂ b l, dropLast, *] theorem chain_append_singleton_iff_forall₂ :
Chain R a (l ++ [b]) ↔ Forall₂ R (a :: l) (l ++ [b]) := by simp [chain_iff_forall₂] theorem chain_map (f : β → α) {b : β} {l : List β} :
Mathlib/Data/List/Chain.lean
69
71
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.NoZeroSMulDivisors.Basic import Mathlib.Algebra.Order.GroupWithZero.Action.Synonym import Mathlib.Tactic.GCongr import Mathlib.Tactic.Positivity.Core /-! # Monotonicity of scalar multiplication by positive elements This file defines typeclasses to reason about monotonicity of the operations * `b ↦ a • b`, "left scalar multiplication" * `a ↦ a • b`, "right scalar multiplication" We use eight typeclasses to encode the various properties we care about for those two operations. These typeclasses are meant to be mostly internal to this file, to set up each lemma in the appropriate generality. Less granular typeclasses like `OrderedAddCommMonoid`, `LinearOrderedField`, `OrderedSMul` should be enough for most purposes, and the system is set up so that they imply the correct granular typeclasses here. If those are enough for you, you may stop reading here! Else, beware that what follows is a bit technical. ## Definitions In all that follows, `α` and `β` are orders which have a `0` and such that `α` acts on `β` by scalar multiplication. Note however that we do not use lawfulness of this action in most of the file. Hence `•` should be considered here as a mostly arbitrary function `α → β → β`. We use the following four typeclasses to reason about left scalar multiplication (`b ↦ a • b`): * `PosSMulMono`: If `a ≥ 0`, then `b₁ ≤ b₂` implies `a • b₁ ≤ a • b₂`. * `PosSMulStrictMono`: If `a > 0`, then `b₁ < b₂` implies `a • b₁ < a • b₂`. * `PosSMulReflectLT`: If `a ≥ 0`, then `a • b₁ < a • b₂` implies `b₁ < b₂`. * `PosSMulReflectLE`: If `a > 0`, then `a • b₁ ≤ a • b₂` implies `b₁ ≤ b₂`. We use the following four typeclasses to reason about right scalar multiplication (`a ↦ a • b`): * `SMulPosMono`: If `b ≥ 0`, then `a₁ ≤ a₂` implies `a₁ • b ≤ a₂ • b`. * `SMulPosStrictMono`: If `b > 0`, then `a₁ < a₂` implies `a₁ • b < a₂ • b`. * `SMulPosReflectLT`: If `b ≥ 0`, then `a₁ • b < a₂ • b` implies `a₁ < a₂`. * `SMulPosReflectLE`: If `b > 0`, then `a₁ • b ≤ a₂ • b` implies `a₁ ≤ a₂`. ## Constructors The four typeclasses about nonnegativity can usually be checked only on positive inputs due to their condition becoming trivial when `a = 0` or `b = 0`. We therefore make the following constructors available: `PosSMulMono.of_pos`, `PosSMulReflectLT.of_pos`, `SMulPosMono.of_pos`, `SMulPosReflectLT.of_pos` ## Implications As `α` and `β` get more and more structure, those typeclasses end up being equivalent. The commonly used implications are: * When `α`, `β` are partial orders: * `PosSMulStrictMono → PosSMulMono` * `SMulPosStrictMono → SMulPosMono` * `PosSMulReflectLE → PosSMulReflectLT` * `SMulPosReflectLE → SMulPosReflectLT` * When `β` is a linear order: * `PosSMulStrictMono → PosSMulReflectLE` * `PosSMulReflectLT → PosSMulMono` (not registered as instance) * `SMulPosReflectLT → SMulPosMono` (not registered as instance) * `PosSMulReflectLE → PosSMulStrictMono` (not registered as instance) * `SMulPosReflectLE → SMulPosStrictMono` (not registered as instance) * When `α` is a linear order: * `SMulPosStrictMono → SMulPosReflectLE` * When `α` is an ordered ring, `β` an ordered group and also an `α`-module: * `PosSMulMono → SMulPosMono` * `PosSMulStrictMono → SMulPosStrictMono` * When `α` is an linear ordered semifield, `β` is an `α`-module: * `PosSMulStrictMono → PosSMulReflectLT` * `PosSMulMono → PosSMulReflectLE` * When `α` is a semiring, `β` is an `α`-module with `NoZeroSMulDivisors`: * `PosSMulMono → PosSMulStrictMono` (not registered as instance) * When `α` is a ring, `β` is an `α`-module with `NoZeroSMulDivisors`: * `SMulPosMono → SMulPosStrictMono` (not registered as instance) Further, the bundled non-granular typeclasses imply the granular ones like so: * `OrderedSMul → PosSMulStrictMono` * `OrderedSMul → PosSMulReflectLT` Unless otherwise stated, all these implications are registered as instances, which means that in practice you should not worry about these implications. However, if you encounter a case where you think a statement is true but not covered by the current implications, please bring it up on Zulip! ## Implementation notes This file uses custom typeclasses instead of abbreviations of `CovariantClass`/`ContravariantClass` because: * They get displayed as classes in the docs. In particular, one can see their list of instances, instead of their instances being invariably dumped to the `CovariantClass`/`ContravariantClass` list. * They don't pollute other typeclass searches. Having many abbreviations of the same typeclass for different purposes always felt like a performance issue (more instances with the same key, for no added benefit), and indeed making the classes here abbreviation previous creates timeouts due to the higher number of `CovariantClass`/`ContravariantClass` instances. * `SMulPosReflectLT`/`SMulPosReflectLE` do not fit in the framework since they relate `≤` on two different types. So we would have to generalise `CovariantClass`/`ContravariantClass` to three types and two relations. * Very minor, but the constructors let you work with `a : α`, `h : 0 ≤ a` instead of `a : {a : α // 0 ≤ a}`. This actually makes some instances surprisingly cleaner to prove. * The `CovariantClass`/`ContravariantClass` framework is only useful to automate very simple logic anyway. It is easily copied over. In the future, it would be good to make the corresponding typeclasses in `Mathlib.Algebra.Order.GroupWithZero.Unbundled` custom typeclasses too. ## TODO This file acts as a substitute for `Mathlib.Algebra.Order.SMul`. We now need to * finish the transition by deleting the duplicate lemmas * rearrange the non-duplicate lemmas into new files * generalise (most of) the lemmas from `Mathlib.Algebra.Order.Module` to here * rethink `OrderedSMul` -/ open OrderDual variable (α β : Type*) section Defs variable [SMul α β] [Preorder α] [Preorder β] section Left variable [Zero α] /-- Typeclass for monotonicity of scalar multiplication by nonnegative elements on the left, namely `b₁ ≤ b₂ → a • b₁ ≤ a • b₂` if `0 ≤ a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulMono : Prop where /-- Do not use this. Use `smul_le_smul_of_nonneg_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 ≤ a) ⦃b₁ b₂ : β⦄ (hb : b₁ ≤ b₂) : a • b₁ ≤ a • b₂ /-- Typeclass for strict monotonicity of scalar multiplication by positive elements on the left, namely `b₁ < b₂ → a • b₁ < a • b₂` if `0 < a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulStrictMono : Prop where /-- Do not use this. Use `smul_lt_smul_of_pos_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 < a) ⦃b₁ b₂ : β⦄ (hb : b₁ < b₂) : a • b₁ < a • b₂ /-- Typeclass for strict reverse monotonicity of scalar multiplication by nonnegative elements on the left, namely `a • b₁ < a • b₂ → b₁ < b₂` if `0 ≤ a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulReflectLT : Prop where /-- Do not use this. Use `lt_of_smul_lt_smul_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 ≤ a) ⦃b₁ b₂ : β⦄ (hb : a • b₁ < a • b₂) : b₁ < b₂ /-- Typeclass for reverse monotonicity of scalar multiplication by positive elements on the left, namely `a • b₁ ≤ a • b₂ → b₁ ≤ b₂` if `0 < a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulReflectLE : Prop where /-- Do not use this. Use `le_of_smul_lt_smul_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 < a) ⦃b₁ b₂ : β⦄ (hb : a • b₁ ≤ a • b₂) : b₁ ≤ b₂ end Left section Right variable [Zero β] /-- Typeclass for monotonicity of scalar multiplication by nonnegative elements on the left, namely `a₁ ≤ a₂ → a₁ • b ≤ a₂ • b` if `0 ≤ b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosMono : Prop where /-- Do not use this. Use `smul_le_smul_of_nonneg_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 ≤ b) ⦃a₁ a₂ : α⦄ (ha : a₁ ≤ a₂) : a₁ • b ≤ a₂ • b /-- Typeclass for strict monotonicity of scalar multiplication by positive elements on the left, namely `a₁ < a₂ → a₁ • b < a₂ • b` if `0 < b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosStrictMono : Prop where /-- Do not use this. Use `smul_lt_smul_of_pos_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 < b) ⦃a₁ a₂ : α⦄ (ha : a₁ < a₂) : a₁ • b < a₂ • b /-- Typeclass for strict reverse monotonicity of scalar multiplication by nonnegative elements on the left, namely `a₁ • b < a₂ • b → a₁ < a₂` if `0 ≤ b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosReflectLT : Prop where /-- Do not use this. Use `lt_of_smul_lt_smul_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 ≤ b) ⦃a₁ a₂ : α⦄ (hb : a₁ • b < a₂ • b) : a₁ < a₂ /-- Typeclass for reverse monotonicity of scalar multiplication by positive elements on the left, namely `a₁ • b ≤ a₂ • b → a₁ ≤ a₂` if `0 < b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosReflectLE : Prop where /-- Do not use this. Use `le_of_smul_lt_smul_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 < b) ⦃a₁ a₂ : α⦄ (hb : a₁ • b ≤ a₂ • b) : a₁ ≤ a₂ end Right end Defs variable {α β} {a a₁ a₂ : α} {b b₁ b₂ : β} section Mul variable [Zero α] [Mul α] [Preorder α] -- See note [lower instance priority] instance (priority := 100) PosMulMono.toPosSMulMono [PosMulMono α] : PosSMulMono α α where elim _a ha _b₁ _b₂ hb := mul_le_mul_of_nonneg_left hb ha -- See note [lower instance priority] instance (priority := 100) PosMulStrictMono.toPosSMulStrictMono [PosMulStrictMono α] : PosSMulStrictMono α α where elim _a ha _b₁ _b₂ hb := mul_lt_mul_of_pos_left hb ha -- See note [lower instance priority] instance (priority := 100) PosMulReflectLT.toPosSMulReflectLT [PosMulReflectLT α] : PosSMulReflectLT α α where elim _a ha _b₁ _b₂ h := lt_of_mul_lt_mul_left h ha -- See note [lower instance priority] instance (priority := 100) PosMulReflectLE.toPosSMulReflectLE [PosMulReflectLE α] : PosSMulReflectLE α α where elim _a ha _b₁ _b₂ h := le_of_mul_le_mul_left h ha -- See note [lower instance priority] instance (priority := 100) MulPosMono.toSMulPosMono [MulPosMono α] : SMulPosMono α α where elim _b hb _a₁ _a₂ ha := mul_le_mul_of_nonneg_right ha hb -- See note [lower instance priority] instance (priority := 100) MulPosStrictMono.toSMulPosStrictMono [MulPosStrictMono α] : SMulPosStrictMono α α where elim _b hb _a₁ _a₂ ha := mul_lt_mul_of_pos_right ha hb -- See note [lower instance priority] instance (priority := 100) MulPosReflectLT.toSMulPosReflectLT [MulPosReflectLT α] : SMulPosReflectLT α α where elim _b hb _a₁ _a₂ h := lt_of_mul_lt_mul_right h hb -- See note [lower instance priority] instance (priority := 100) MulPosReflectLE.toSMulPosReflectLE [MulPosReflectLE α] : SMulPosReflectLE α α where elim _b hb _a₁ _a₂ h := le_of_mul_le_mul_right h hb end Mul section SMul variable [SMul α β] section Preorder variable [Preorder α] [Preorder β] section Left variable [Zero α] lemma monotone_smul_left_of_nonneg [PosSMulMono α β] (ha : 0 ≤ a) : Monotone ((a • ·) : β → β) := PosSMulMono.elim ha lemma strictMono_smul_left_of_pos [PosSMulStrictMono α β] (ha : 0 < a) : StrictMono ((a • ·) : β → β) := PosSMulStrictMono.elim ha @[gcongr] lemma smul_le_smul_of_nonneg_left [PosSMulMono α β] (hb : b₁ ≤ b₂) (ha : 0 ≤ a) : a • b₁ ≤ a • b₂ := monotone_smul_left_of_nonneg ha hb @[gcongr] lemma smul_lt_smul_of_pos_left [PosSMulStrictMono α β] (hb : b₁ < b₂) (ha : 0 < a) : a • b₁ < a • b₂ := strictMono_smul_left_of_pos ha hb lemma lt_of_smul_lt_smul_left [PosSMulReflectLT α β] (h : a • b₁ < a • b₂) (ha : 0 ≤ a) : b₁ < b₂ := PosSMulReflectLT.elim ha h lemma le_of_smul_le_smul_left [PosSMulReflectLE α β] (h : a • b₁ ≤ a • b₂) (ha : 0 < a) : b₁ ≤ b₂ := PosSMulReflectLE.elim ha h alias lt_of_smul_lt_smul_of_nonneg_left := lt_of_smul_lt_smul_left alias le_of_smul_le_smul_of_pos_left := le_of_smul_le_smul_left @[simp] lemma smul_le_smul_iff_of_pos_left [PosSMulMono α β] [PosSMulReflectLE α β] (ha : 0 < a) : a • b₁ ≤ a • b₂ ↔ b₁ ≤ b₂ := ⟨fun h ↦ le_of_smul_le_smul_left h ha, fun h ↦ smul_le_smul_of_nonneg_left h ha.le⟩ @[simp] lemma smul_lt_smul_iff_of_pos_left [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : a • b₁ < a • b₂ ↔ b₁ < b₂ := ⟨fun h ↦ lt_of_smul_lt_smul_left h ha.le, fun hb ↦ smul_lt_smul_of_pos_left hb ha⟩ end Left section Right variable [Zero β] lemma monotone_smul_right_of_nonneg [SMulPosMono α β] (hb : 0 ≤ b) : Monotone ((· • b) : α → β) := SMulPosMono.elim hb lemma strictMono_smul_right_of_pos [SMulPosStrictMono α β] (hb : 0 < b) : StrictMono ((· • b) : α → β) := SMulPosStrictMono.elim hb @[gcongr] lemma smul_le_smul_of_nonneg_right [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : 0 ≤ b) : a₁ • b ≤ a₂ • b := monotone_smul_right_of_nonneg hb ha @[gcongr] lemma smul_lt_smul_of_pos_right [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : 0 < b) : a₁ • b < a₂ • b := strictMono_smul_right_of_pos hb ha lemma lt_of_smul_lt_smul_right [SMulPosReflectLT α β] (h : a₁ • b < a₂ • b) (hb : 0 ≤ b) : a₁ < a₂ := SMulPosReflectLT.elim hb h lemma le_of_smul_le_smul_right [SMulPosReflectLE α β] (h : a₁ • b ≤ a₂ • b) (hb : 0 < b) : a₁ ≤ a₂ := SMulPosReflectLE.elim hb h alias lt_of_smul_lt_smul_of_nonneg_right := lt_of_smul_lt_smul_right alias le_of_smul_le_smul_of_pos_right := le_of_smul_le_smul_right @[simp] lemma smul_le_smul_iff_of_pos_right [SMulPosMono α β] [SMulPosReflectLE α β] (hb : 0 < b) : a₁ • b ≤ a₂ • b ↔ a₁ ≤ a₂ := ⟨fun h ↦ le_of_smul_le_smul_right h hb, fun ha ↦ smul_le_smul_of_nonneg_right ha hb.le⟩ @[simp] lemma smul_lt_smul_iff_of_pos_right [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : a₁ • b < a₂ • b ↔ a₁ < a₂ := ⟨fun h ↦ lt_of_smul_lt_smul_right h hb.le, fun ha ↦ smul_lt_smul_of_pos_right ha hb⟩ end Right section LeftRight variable [Zero α] [Zero β] lemma smul_lt_smul_of_le_of_lt [PosSMulStrictMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ < b₂) (h₁ : 0 < a₁) (h₂ : 0 ≤ b₂) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_left hb h₁).trans_le (smul_le_smul_of_nonneg_right ha h₂) lemma smul_lt_smul_of_le_of_lt' [PosSMulStrictMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ < b₂) (h₂ : 0 < a₂) (h₁ : 0 ≤ b₁) : a₁ • b₁ < a₂ • b₂ := (smul_le_smul_of_nonneg_right ha h₁).trans_lt (smul_lt_smul_of_pos_left hb h₂) lemma smul_lt_smul_of_lt_of_le [PosSMulMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ ≤ b₂) (h₁ : 0 ≤ a₁) (h₂ : 0 < b₂) : a₁ • b₁ < a₂ • b₂ := (smul_le_smul_of_nonneg_left hb h₁).trans_lt (smul_lt_smul_of_pos_right ha h₂) lemma smul_lt_smul_of_lt_of_le' [PosSMulMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ ≤ b₂) (h₂ : 0 ≤ a₂) (h₁ : 0 < b₁) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_right ha h₁).trans_le (smul_le_smul_of_nonneg_left hb h₂) lemma smul_lt_smul [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ < b₂) (h₁ : 0 < a₁) (h₂ : 0 < b₂) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_left hb h₁).trans (smul_lt_smul_of_pos_right ha h₂) lemma smul_lt_smul' [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ < b₂) (h₂ : 0 < a₂) (h₁ : 0 < b₁) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_right ha h₁).trans (smul_lt_smul_of_pos_left hb h₂) lemma smul_le_smul [PosSMulMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₁ : 0 ≤ a₁) (h₂ : 0 ≤ b₂) : a₁ • b₁ ≤ a₂ • b₂ := (smul_le_smul_of_nonneg_left hb h₁).trans (smul_le_smul_of_nonneg_right ha h₂) lemma smul_le_smul' [PosSMulMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₂ : 0 ≤ a₂) (h₁ : 0 ≤ b₁) : a₁ • b₁ ≤ a₂ • b₂ := (smul_le_smul_of_nonneg_right ha h₁).trans (smul_le_smul_of_nonneg_left hb h₂) end LeftRight end Preorder section LinearOrder variable [Preorder α] [LinearOrder β] section Left variable [Zero α] -- See note [lower instance priority] instance (priority := 100) PosSMulStrictMono.toPosSMulReflectLE [PosSMulStrictMono α β] : PosSMulReflectLE α β where elim _a ha _b₁ _b₂ := (strictMono_smul_left_of_pos ha).le_iff_le.1 lemma PosSMulReflectLE.toPosSMulStrictMono [PosSMulReflectLE α β] : PosSMulStrictMono α β where elim _a ha _b₁ _b₂ hb := not_le.1 fun h ↦ hb.not_le <| le_of_smul_le_smul_left h ha lemma posSMulStrictMono_iff_PosSMulReflectLE : PosSMulStrictMono α β ↔ PosSMulReflectLE α β := ⟨fun _ ↦ inferInstance, fun _ ↦ PosSMulReflectLE.toPosSMulStrictMono⟩ instance PosSMulMono.toPosSMulReflectLT [PosSMulMono α β] : PosSMulReflectLT α β where elim _a ha _b₁ _b₂ := (monotone_smul_left_of_nonneg ha).reflect_lt lemma PosSMulReflectLT.toPosSMulMono [PosSMulReflectLT α β] : PosSMulMono α β where elim _a ha _b₁ _b₂ hb := not_lt.1 fun h ↦ hb.not_lt <| lt_of_smul_lt_smul_left h ha lemma posSMulMono_iff_posSMulReflectLT : PosSMulMono α β ↔ PosSMulReflectLT α β := ⟨fun _ ↦ PosSMulMono.toPosSMulReflectLT, fun _ ↦ PosSMulReflectLT.toPosSMulMono⟩ lemma smul_max_of_nonneg [PosSMulMono α β] (ha : 0 ≤ a) (b₁ b₂ : β) : a • max b₁ b₂ = max (a • b₁) (a • b₂) := (monotone_smul_left_of_nonneg ha).map_max lemma smul_min_of_nonneg [PosSMulMono α β] (ha : 0 ≤ a) (b₁ b₂ : β) : a • min b₁ b₂ = min (a • b₁) (a • b₂) := (monotone_smul_left_of_nonneg ha).map_min end Left section Right variable [Zero β] lemma SMulPosReflectLE.toSMulPosStrictMono [SMulPosReflectLE α β] : SMulPosStrictMono α β where elim _b hb _a₁ _a₂ ha := not_le.1 fun h ↦ ha.not_le <| le_of_smul_le_smul_of_pos_right h hb lemma SMulPosReflectLT.toSMulPosMono [SMulPosReflectLT α β] : SMulPosMono α β where elim _b hb _a₁ _a₂ ha := not_lt.1 fun h ↦ ha.not_lt <| lt_of_smul_lt_smul_right h hb end Right end LinearOrder section LinearOrder variable [LinearOrder α] [Preorder β] section Right variable [Zero β] -- See note [lower instance priority] instance (priority := 100) SMulPosStrictMono.toSMulPosReflectLE [SMulPosStrictMono α β] : SMulPosReflectLE α β where elim _b hb _a₁ _a₂ h := not_lt.1 fun ha ↦ h.not_lt <| smul_lt_smul_of_pos_right ha hb lemma SMulPosMono.toSMulPosReflectLT [SMulPosMono α β] : SMulPosReflectLT α β where elim _b hb _a₁ _a₂ h := not_le.1 fun ha ↦ h.not_le <| smul_le_smul_of_nonneg_right ha hb end Right end LinearOrder section LinearOrder variable [LinearOrder α] [LinearOrder β] section Right variable [Zero β] lemma smulPosStrictMono_iff_SMulPosReflectLE : SMulPosStrictMono α β ↔ SMulPosReflectLE α β := ⟨fun _ ↦ SMulPosStrictMono.toSMulPosReflectLE, fun _ ↦ SMulPosReflectLE.toSMulPosStrictMono⟩ lemma smulPosMono_iff_smulPosReflectLT : SMulPosMono α β ↔ SMulPosReflectLT α β := ⟨fun _ ↦ SMulPosMono.toSMulPosReflectLT, fun _ ↦ SMulPosReflectLT.toSMulPosMono⟩ end Right end LinearOrder end SMul section SMulZeroClass variable [Zero α] [Zero β] [SMulZeroClass α β] section Preorder variable [Preorder α] [Preorder β] lemma smul_pos [PosSMulStrictMono α β] (ha : 0 < a) (hb : 0 < b) : 0 < a • b := by simpa only [smul_zero] using smul_lt_smul_of_pos_left hb ha lemma smul_neg_of_pos_of_neg [PosSMulStrictMono α β] (ha : 0 < a) (hb : b < 0) : a • b < 0 := by simpa only [smul_zero] using smul_lt_smul_of_pos_left hb ha @[simp] lemma smul_pos_iff_of_pos_left [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : 0 < a • b ↔ 0 < b := by simpa only [smul_zero] using smul_lt_smul_iff_of_pos_left ha (b₁ := 0) (b₂ := b) lemma smul_neg_iff_of_pos_left [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : a • b < 0 ↔ b < 0 := by simpa only [smul_zero] using smul_lt_smul_iff_of_pos_left ha (b₂ := (0 : β)) lemma smul_nonneg [PosSMulMono α β] (ha : 0 ≤ a) (hb : 0 ≤ b₁) : 0 ≤ a • b₁ := by simpa only [smul_zero] using smul_le_smul_of_nonneg_left hb ha lemma smul_nonpos_of_nonneg_of_nonpos [PosSMulMono α β] (ha : 0 ≤ a) (hb : b ≤ 0) : a • b ≤ 0 := by simpa only [smul_zero] using smul_le_smul_of_nonneg_left hb ha lemma pos_of_smul_pos_left [PosSMulReflectLT α β] (h : 0 < a • b) (ha : 0 ≤ a) : 0 < b := lt_of_smul_lt_smul_left (by rwa [smul_zero]) ha lemma neg_of_smul_neg_left [PosSMulReflectLT α β] (h : a • b < 0) (ha : 0 ≤ a) : b < 0 := lt_of_smul_lt_smul_left (by rwa [smul_zero]) ha end Preorder end SMulZeroClass section SMulWithZero variable [Zero α] [Zero β] [SMulWithZero α β] section Preorder variable [Preorder α] [Preorder β] lemma smul_pos' [SMulPosStrictMono α β] (ha : 0 < a) (hb : 0 < b) : 0 < a • b := by simpa only [zero_smul] using smul_lt_smul_of_pos_right ha hb lemma smul_neg_of_neg_of_pos [SMulPosStrictMono α β] (ha : a < 0) (hb : 0 < b) : a • b < 0 := by simpa only [zero_smul] using smul_lt_smul_of_pos_right ha hb @[simp] lemma smul_pos_iff_of_pos_right [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : 0 < a • b ↔ 0 < a := by simpa only [zero_smul] using smul_lt_smul_iff_of_pos_right hb (a₁ := 0) (a₂ := a) lemma smul_nonneg' [SMulPosMono α β] (ha : 0 ≤ a) (hb : 0 ≤ b₁) : 0 ≤ a • b₁ := by simpa only [zero_smul] using smul_le_smul_of_nonneg_right ha hb lemma smul_nonpos_of_nonpos_of_nonneg [SMulPosMono α β] (ha : a ≤ 0) (hb : 0 ≤ b) : a • b ≤ 0 := by simpa only [zero_smul] using smul_le_smul_of_nonneg_right ha hb lemma pos_of_smul_pos_right [SMulPosReflectLT α β] (h : 0 < a • b) (hb : 0 ≤ b) : 0 < a := lt_of_smul_lt_smul_right (by rwa [zero_smul]) hb lemma neg_of_smul_neg_right [SMulPosReflectLT α β] (h : a • b < 0) (hb : 0 ≤ b) : a < 0 := lt_of_smul_lt_smul_right (by rwa [zero_smul]) hb lemma pos_iff_pos_of_smul_pos [PosSMulReflectLT α β] [SMulPosReflectLT α β] (hab : 0 < a • b) : 0 < a ↔ 0 < b := ⟨pos_of_smul_pos_left hab ∘ le_of_lt, pos_of_smul_pos_right hab ∘ le_of_lt⟩ end Preorder section PartialOrder variable [PartialOrder α] [Preorder β] /-- A constructor for `PosSMulMono` requiring you to prove `b₁ ≤ b₂ → a • b₁ ≤ a • b₂` only when `0 < a` -/ lemma PosSMulMono.of_pos (h₀ : ∀ a : α, 0 < a → ∀ b₁ b₂ : β, b₁ ≤ b₂ → a • b₁ ≤ a • b₂) : PosSMulMono α β where elim a ha b₁ b₂ h := by obtain ha | ha := ha.eq_or_lt · simp [← ha] · exact h₀ _ ha _ _ h /-- A constructor for `PosSMulReflectLT` requiring you to prove `a • b₁ < a • b₂ → b₁ < b₂` only when `0 < a` -/ lemma PosSMulReflectLT.of_pos (h₀ : ∀ a : α, 0 < a → ∀ b₁ b₂ : β, a • b₁ < a • b₂ → b₁ < b₂) : PosSMulReflectLT α β where elim a ha b₁ b₂ h := by obtain ha | ha := ha.eq_or_lt · simp [← ha] at h · exact h₀ _ ha _ _ h end PartialOrder section PartialOrder variable [Preorder α] [PartialOrder β] /-- A constructor for `SMulPosMono` requiring you to prove `a₁ ≤ a₂ → a₁ • b ≤ a₂ • b` only when `0 < b` -/ lemma SMulPosMono.of_pos (h₀ : ∀ b : β, 0 < b → ∀ a₁ a₂ : α, a₁ ≤ a₂ → a₁ • b ≤ a₂ • b) : SMulPosMono α β where elim b hb a₁ a₂ h := by obtain hb | hb := hb.eq_or_lt · simp [← hb] · exact h₀ _ hb _ _ h /-- A constructor for `SMulPosReflectLT` requiring you to prove `a₁ • b < a₂ • b → a₁ < a₂` only when `0 < b` -/ lemma SMulPosReflectLT.of_pos (h₀ : ∀ b : β, 0 < b → ∀ a₁ a₂ : α, a₁ • b < a₂ • b → a₁ < a₂) : SMulPosReflectLT α β where elim b hb a₁ a₂ h := by obtain hb | hb := hb.eq_or_lt · simp [← hb] at h · exact h₀ _ hb _ _ h end PartialOrder section PartialOrder variable [PartialOrder α] [PartialOrder β] -- See note [lower instance priority] instance (priority := 100) PosSMulStrictMono.toPosSMulMono [PosSMulStrictMono α β] : PosSMulMono α β := PosSMulMono.of_pos fun _a ha ↦ (strictMono_smul_left_of_pos ha).monotone -- See note [lower instance priority] instance (priority := 100) SMulPosStrictMono.toSMulPosMono [SMulPosStrictMono α β] : SMulPosMono α β := SMulPosMono.of_pos fun _b hb ↦ (strictMono_smul_right_of_pos hb).monotone -- See note [lower instance priority] instance (priority := 100) PosSMulReflectLE.toPosSMulReflectLT [PosSMulReflectLE α β] : PosSMulReflectLT α β := PosSMulReflectLT.of_pos fun a ha b₁ b₂ h ↦ (le_of_smul_le_smul_of_pos_left h.le ha).lt_of_ne <| by rintro rfl; simp at h -- See note [lower instance priority] instance (priority := 100) SMulPosReflectLE.toSMulPosReflectLT [SMulPosReflectLE α β] : SMulPosReflectLT α β := SMulPosReflectLT.of_pos fun b hb a₁ a₂ h ↦ (le_of_smul_le_smul_of_pos_right h.le hb).lt_of_ne <| by rintro rfl; simp at h lemma smul_eq_smul_iff_eq_and_eq_of_pos [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₁ : 0 < a₁) (h₂ : 0 < b₂) : a₁ • b₁ = a₂ • b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := by refine ⟨fun h ↦ ?_, by rintro ⟨rfl, rfl⟩; rfl⟩ simp only [eq_iff_le_not_lt, ha, hb, true_and] refine ⟨fun ha ↦ h.not_lt ?_, fun hb ↦ h.not_lt ?_⟩ · exact (smul_le_smul_of_nonneg_left hb h₁.le).trans_lt (smul_lt_smul_of_pos_right ha h₂) · exact (smul_lt_smul_of_pos_left hb h₁).trans_le (smul_le_smul_of_nonneg_right ha h₂.le) lemma smul_eq_smul_iff_eq_and_eq_of_pos' [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₂ : 0 < a₂) (h₁ : 0 < b₁) : a₁ • b₁ = a₂ • b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := by refine ⟨fun h ↦ ?_, by rintro ⟨rfl, rfl⟩; rfl⟩ simp only [eq_iff_le_not_lt, ha, hb, true_and] refine ⟨fun ha ↦ h.not_lt ?_, fun hb ↦ h.not_lt ?_⟩ · exact (smul_lt_smul_of_pos_right ha h₁).trans_le (smul_le_smul_of_nonneg_left hb h₂.le) · exact (smul_le_smul_of_nonneg_right ha h₁.le).trans_lt (smul_lt_smul_of_pos_left hb h₂) end PartialOrder section LinearOrder variable [LinearOrder α] [LinearOrder β] lemma pos_and_pos_or_neg_and_neg_of_smul_pos [PosSMulMono α β] [SMulPosMono α β] (hab : 0 < a • b) : 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := by obtain ha | rfl | ha := lt_trichotomy a 0 · refine Or.inr ⟨ha, lt_imp_lt_of_le_imp_le (fun hb ↦ ?_) hab⟩ exact smul_nonpos_of_nonpos_of_nonneg ha.le hb · rw [zero_smul] at hab exact hab.false.elim · refine Or.inl ⟨ha, lt_imp_lt_of_le_imp_le (fun hb ↦ ?_) hab⟩ exact smul_nonpos_of_nonneg_of_nonpos ha.le hb lemma neg_of_smul_pos_right [PosSMulMono α β] [SMulPosMono α β] (h : 0 < a • b) (ha : a ≤ 0) : b < 0 := ((pos_and_pos_or_neg_and_neg_of_smul_pos h).resolve_left fun h ↦ h.1.not_le ha).2 lemma neg_of_smul_pos_left [PosSMulMono α β] [SMulPosMono α β] (h : 0 < a • b) (ha : b ≤ 0) : a < 0 := ((pos_and_pos_or_neg_and_neg_of_smul_pos h).resolve_left fun h ↦ h.2.not_le ha).1 lemma neg_iff_neg_of_smul_pos [PosSMulMono α β] [SMulPosMono α β] (hab : 0 < a • b) : a < 0 ↔ b < 0 := ⟨neg_of_smul_pos_right hab ∘ le_of_lt, neg_of_smul_pos_left hab ∘ le_of_lt⟩ lemma neg_of_smul_neg_left' [SMulPosMono α β] (h : a • b < 0) (ha : 0 ≤ a) : b < 0 := lt_of_not_ge fun hb ↦ (smul_nonneg' ha hb).not_lt h lemma neg_of_smul_neg_right' [PosSMulMono α β] (h : a • b < 0) (hb : 0 ≤ b) : a < 0 := lt_of_not_ge fun ha ↦ (smul_nonneg ha hb).not_lt h end LinearOrder end SMulWithZero section MulAction variable [Monoid α] [Zero β] [MulAction α β] section Preorder variable [Preorder α] [Preorder β] @[simp] lemma le_smul_iff_one_le_left [SMulPosMono α β] [SMulPosReflectLE α β] (hb : 0 < b) : b ≤ a • b ↔ 1 ≤ a := Iff.trans (by rw [one_smul]) (smul_le_smul_iff_of_pos_right hb) @[simp] lemma lt_smul_iff_one_lt_left [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : b < a • b ↔ 1 < a := Iff.trans (by rw [one_smul]) (smul_lt_smul_iff_of_pos_right hb) @[simp] lemma smul_le_iff_le_one_left [SMulPosMono α β] [SMulPosReflectLE α β] (hb : 0 < b) : a • b ≤ b ↔ a ≤ 1 := Iff.trans (by rw [one_smul]) (smul_le_smul_iff_of_pos_right hb) @[simp] lemma smul_lt_iff_lt_one_left [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : a • b < b ↔ a < 1 := Iff.trans (by rw [one_smul]) (smul_lt_smul_iff_of_pos_right hb) lemma smul_le_of_le_one_left [SMulPosMono α β] (hb : 0 ≤ b) (h : a ≤ 1) : a • b ≤ b := by simpa only [one_smul] using smul_le_smul_of_nonneg_right h hb lemma le_smul_of_one_le_left [SMulPosMono α β] (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a • b := by simpa only [one_smul] using smul_le_smul_of_nonneg_right h hb lemma smul_lt_of_lt_one_left [SMulPosStrictMono α β] (hb : 0 < b) (h : a < 1) : a • b < b := by simpa only [one_smul] using smul_lt_smul_of_pos_right h hb lemma lt_smul_of_one_lt_left [SMulPosStrictMono α β] (hb : 0 < b) (h : 1 < a) : b < a • b := by simpa only [one_smul] using smul_lt_smul_of_pos_right h hb end Preorder end MulAction section Semiring variable [Semiring α] [AddCommGroup β] [Module α β] [NoZeroSMulDivisors α β] section PartialOrder variable [Preorder α] [PartialOrder β] lemma PosSMulMono.toPosSMulStrictMono [PosSMulMono α β] : PosSMulStrictMono α β := ⟨fun _a ha _b₁ _b₂ hb ↦ (smul_le_smul_of_nonneg_left hb.le ha.le).lt_of_ne <| (smul_right_injective _ ha.ne').ne hb.ne⟩ instance PosSMulReflectLT.toPosSMulReflectLE [PosSMulReflectLT α β] : PosSMulReflectLE α β := ⟨fun _a ha _b₁ _b₂ h ↦ h.eq_or_lt.elim (fun h ↦ (smul_right_injective _ ha.ne' h).le) fun h' ↦ (lt_of_smul_lt_smul_left h' ha.le).le⟩ end PartialOrder section PartialOrder variable [PartialOrder α] [PartialOrder β] lemma posSMulMono_iff_posSMulStrictMono : PosSMulMono α β ↔ PosSMulStrictMono α β := ⟨fun _ ↦ PosSMulMono.toPosSMulStrictMono, fun _ ↦ inferInstance⟩ lemma PosSMulReflectLE_iff_posSMulReflectLT : PosSMulReflectLE α β ↔ PosSMulReflectLT α β := ⟨fun _ ↦ inferInstance, fun _ ↦ PosSMulReflectLT.toPosSMulReflectLE⟩ end PartialOrder end Semiring section Ring variable [Ring α] [AddCommGroup β] [Module α β] [NoZeroSMulDivisors α β] section PartialOrder variable [PartialOrder α] [PartialOrder β] lemma SMulPosMono.toSMulPosStrictMono [SMulPosMono α β] : SMulPosStrictMono α β := ⟨fun _b hb _a₁ _a₂ ha ↦ (smul_le_smul_of_nonneg_right ha.le hb.le).lt_of_ne <| (smul_left_injective _ hb.ne').ne ha.ne⟩ lemma smulPosMono_iff_smulPosStrictMono : SMulPosMono α β ↔ SMulPosStrictMono α β := ⟨fun _ ↦ SMulPosMono.toSMulPosStrictMono, fun _ ↦ inferInstance⟩ lemma SMulPosReflectLT.toSMulPosReflectLE [SMulPosReflectLT α β] : SMulPosReflectLE α β := ⟨fun _b hb _a₁ _a₂ h ↦ h.eq_or_lt.elim (fun h ↦ (smul_left_injective _ hb.ne' h).le) fun h' ↦ (lt_of_smul_lt_smul_right h' hb.le).le⟩ lemma SMulPosReflectLE_iff_smulPosReflectLT : SMulPosReflectLE α β ↔ SMulPosReflectLT α β := ⟨fun _ ↦ inferInstance, fun _ ↦ SMulPosReflectLT.toSMulPosReflectLE⟩ end PartialOrder end Ring section GroupWithZero variable [GroupWithZero α] [Preorder α] [Preorder β] [MulAction α β] lemma inv_smul_le_iff_of_pos [PosSMulMono α β] [PosSMulReflectLE α β] (ha : 0 < a) : a⁻¹ • b₁ ≤ b₂ ↔ b₁ ≤ a • b₂ := by rw [← smul_le_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] lemma le_inv_smul_iff_of_pos [PosSMulMono α β] [PosSMulReflectLE α β] (ha : 0 < a) : b₁ ≤ a⁻¹ • b₂ ↔ a • b₁ ≤ b₂ := by rw [← smul_le_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] lemma inv_smul_lt_iff_of_pos [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : a⁻¹ • b₁ < b₂ ↔ b₁ < a • b₂ := by rw [← smul_lt_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] lemma lt_inv_smul_iff_of_pos [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : b₁ < a⁻¹ • b₂ ↔ a • b₁ < b₂ := by rw [← smul_lt_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] /-- Right scalar multiplication as an order isomorphism. -/ @[simps!] def OrderIso.smulRight [PosSMulMono α β] [PosSMulReflectLE α β] {a : α} (ha : 0 < a) : β ≃o β where toEquiv := Equiv.smulRight ha.ne' map_rel_iff' := smul_le_smul_iff_of_pos_left ha end GroupWithZero namespace OrderDual section Left variable [Preorder α] [Preorder β] [SMul α β] [Zero α] instance instPosSMulMono [PosSMulMono α β] : PosSMulMono α βᵒᵈ where elim _a ha _b₁ _b₂ hb := smul_le_smul_of_nonneg_left (β := β) hb ha instance instPosSMulStrictMono [PosSMulStrictMono α β] : PosSMulStrictMono α βᵒᵈ where elim _a ha _b₁ _b₂ hb := smul_lt_smul_of_pos_left (β := β) hb ha instance instPosSMulReflectLT [PosSMulReflectLT α β] : PosSMulReflectLT α βᵒᵈ where elim _a ha _b₁ _b₂ h := lt_of_smul_lt_smul_of_nonneg_left (β := β) h ha instance instPosSMulReflectLE [PosSMulReflectLE α β] : PosSMulReflectLE α βᵒᵈ where elim _a ha _b₁ _b₂ h := le_of_smul_le_smul_of_pos_left (β := β) h ha end Left section Right variable [Preorder α] [Monoid α] [AddCommGroup β] [PartialOrder β] [IsOrderedAddMonoid β] [DistribMulAction α β] instance instSMulPosMono [SMulPosMono α β] : SMulPosMono α βᵒᵈ where elim _b hb a₁ a₂ ha := by rw [← neg_le_neg_iff, ← smul_neg, ← smul_neg] exact smul_le_smul_of_nonneg_right (β := β) ha <| neg_nonneg.2 hb instance instSMulPosStrictMono [SMulPosStrictMono α β] : SMulPosStrictMono α βᵒᵈ where elim _b hb a₁ a₂ ha := by rw [← neg_lt_neg_iff, ← smul_neg, ← smul_neg] exact smul_lt_smul_of_pos_right (β := β) ha <| neg_pos.2 hb instance instSMulPosReflectLT [SMulPosReflectLT α β] : SMulPosReflectLT α βᵒᵈ where elim _b hb a₁ a₂ h := by rw [← neg_lt_neg_iff, ← smul_neg, ← smul_neg] at h exact lt_of_smul_lt_smul_right (β := β) h <| neg_nonneg.2 hb instance instSMulPosReflectLE [SMulPosReflectLE α β] : SMulPosReflectLE α βᵒᵈ where elim _b hb a₁ a₂ h := by rw [← neg_le_neg_iff, ← smul_neg, ← smul_neg] at h exact le_of_smul_le_smul_right (β := β) h <| neg_pos.2 hb end Right end OrderDual section OrderedAddCommMonoid variable [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] [ExistsAddOfLE α] [AddCommMonoid β] [PartialOrder β] [IsOrderedCancelAddMonoid β] [Module α β] section PosSMulMono variable [PosSMulMono α β] {a₁ a₂ : α} {b₁ b₂ : β} /-- Binary **rearrangement inequality**. -/ lemma smul_add_smul_le_smul_add_smul (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) : a₁ • b₂ + a₂ • b₁ ≤ a₁ • b₁ + a₂ • b₂ := by obtain ⟨a, ha₀, rfl⟩ := exists_nonneg_add_of_le ha rw [add_smul, add_smul, add_left_comm] gcongr /-- Binary **rearrangement inequality**. -/ lemma smul_add_smul_le_smul_add_smul' (ha : a₂ ≤ a₁) (hb : b₂ ≤ b₁) : a₁ • b₂ + a₂ • b₁ ≤ a₁ • b₁ + a₂ • b₂ := by simp_rw [add_comm (a₁ • _)]; exact smul_add_smul_le_smul_add_smul ha hb end PosSMulMono section PosSMulStrictMono variable [PosSMulStrictMono α β] {a₁ a₂ : α} {b₁ b₂ : β} /-- Binary strict **rearrangement inequality**. -/ lemma smul_add_smul_lt_smul_add_smul (ha : a₁ < a₂) (hb : b₁ < b₂) : a₁ • b₂ + a₂ • b₁ < a₁ • b₁ + a₂ • b₂ := by obtain ⟨a, ha₀, rfl⟩ := lt_iff_exists_pos_add.1 ha rw [add_smul, add_smul, add_left_comm] gcongr /-- Binary strict **rearrangement inequality**. -/ lemma smul_add_smul_lt_smul_add_smul' (ha : a₂ < a₁) (hb : b₂ < b₁) : a₁ • b₂ + a₂ • b₁ < a₁ • b₁ + a₂ • b₂ := by simp_rw [add_comm (a₁ • _)]; exact smul_add_smul_lt_smul_add_smul ha hb end PosSMulStrictMono end OrderedAddCommMonoid section OrderedRing variable [Ring α] [PartialOrder α] [IsOrderedRing α] section OrderedAddCommGroup variable [AddCommGroup β] [PartialOrder β] [IsOrderedAddMonoid β] [Module α β] section PosSMulMono variable [PosSMulMono α β] lemma smul_le_smul_of_nonpos_left (h : b₁ ≤ b₂) (ha : a ≤ 0) : a • b₂ ≤ a • b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_le_neg_iff] exact smul_le_smul_of_nonneg_left h (neg_nonneg_of_nonpos ha) lemma antitone_smul_left (ha : a ≤ 0) : Antitone ((a • ·) : β → β) := fun _ _ h ↦ smul_le_smul_of_nonpos_left h ha instance PosSMulMono.toSMulPosMono : SMulPosMono α β where elim _b hb a₁ a₂ ha := by rw [← sub_nonneg, ← sub_smul]; exact smul_nonneg (sub_nonneg.2 ha) hb end PosSMulMono section PosSMulStrictMono variable [PosSMulStrictMono α β] lemma smul_lt_smul_of_neg_left (hb : b₁ < b₂) (ha : a < 0) : a • b₂ < a • b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_lt_neg_iff] exact smul_lt_smul_of_pos_left hb (neg_pos_of_neg ha) lemma strictAnti_smul_left (ha : a < 0) : StrictAnti ((a • ·) : β → β) := fun _ _ h ↦ smul_lt_smul_of_neg_left h ha instance PosSMulStrictMono.toSMulPosStrictMono : SMulPosStrictMono α β where elim _b hb a₁ a₂ ha := by rw [← sub_pos, ← sub_smul]; exact smul_pos (sub_pos.2 ha) hb end PosSMulStrictMono lemma le_of_smul_le_smul_of_neg [PosSMulReflectLE α β] (h : a • b₁ ≤ a • b₂) (ha : a < 0) : b₂ ≤ b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_le_neg_iff] at h exact le_of_smul_le_smul_of_pos_left h <| neg_pos.2 ha lemma lt_of_smul_lt_smul_of_nonpos [PosSMulReflectLT α β] (h : a • b₁ < a • b₂) (ha : a ≤ 0) :
b₂ < b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_lt_neg_iff] at h exact lt_of_smul_lt_smul_of_nonneg_left h (neg_nonneg_of_nonpos ha)
Mathlib/Algebra/Order/Module/Defs.lean
879
882
/- Copyright (c) 2022 María Inés de Frutos-Fernández. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: María Inés de Frutos-Fernández -/ import Mathlib.RingTheory.DedekindDomain.Ideal import Mathlib.RingTheory.Valuation.ExtendToLocalization import Mathlib.Topology.Algebra.Valued.ValuedField import Mathlib.Topology.Algebra.Valued.WithVal /-! # Adic valuations on Dedekind domains Given a Dedekind domain `R` of Krull dimension 1 and a maximal ideal `v` of `R`, we define the `v`-adic valuation on `R` and its extension to the field of fractions `K` of `R`. We prove several properties of this valuation, including the existence of uniformizers. We define the completion of `K` with respect to the `v`-adic valuation, denoted `v.adicCompletion`, and its ring of integers, denoted `v.adicCompletionIntegers`. ## Main definitions - `IsDedekindDomain.HeightOneSpectrum.intValuation v` is the `v`-adic valuation on `R`. - `IsDedekindDomain.HeightOneSpectrum.valuation v` is the `v`-adic valuation on `K`. - `IsDedekindDomain.HeightOneSpectrum.adicCompletion v` is the completion of `K` with respect to its `v`-adic valuation. - `IsDedekindDomain.HeightOneSpectrum.adicCompletionIntegers v` is the ring of integers of `v.adicCompletion`. ## Main results - `IsDedekindDomain.HeightOneSpectrum.intValuation_le_one` : The `v`-adic valuation on `R` is bounded above by 1. - `IsDedekindDomain.HeightOneSpectrum.intValuation_lt_one_iff_dvd` : The `v`-adic valuation of `r ∈ R` is less than 1 if and only if `v` divides the ideal `(r)`. - `IsDedekindDomain.HeightOneSpectrum.intValuation_le_pow_iff_dvd` : The `v`-adic valuation of `r ∈ R` is less than or equal to `Multiplicative.ofAdd (-n)` if and only if `vⁿ` divides the ideal `(r)`. - `IsDedekindDomain.HeightOneSpectrum.intValuation_exists_uniformizer` : There exists `π ∈ R` with `v`-adic valuation `Multiplicative.ofAdd (-1)`. - `IsDedekindDomain.HeightOneSpectrum.valuation_of_mk'` : The `v`-adic valuation of `r/s ∈ K` is the valuation of `r` divided by the valuation of `s`. - `IsDedekindDomain.HeightOneSpectrum.valuation_of_algebraMap` : The `v`-adic valuation on `K` extends the `v`-adic valuation on `R`. - `IsDedekindDomain.HeightOneSpectrum.valuation_exists_uniformizer` : There exists `π ∈ K` with `v`-adic valuation `Multiplicative.ofAdd (-1)`. ## Implementation notes We are only interested in Dedekind domains with Krull dimension 1. ## References * [G. J. Janusz, *Algebraic Number Fields*][janusz1996] * [J.W.S. Cassels, A. Fröhlich, *Algebraic Number Theory*][cassels1967algebraic] * [J. Neukirch, *Algebraic Number Theory*][Neukirch1992] ## Tags dedekind domain, dedekind ring, adic valuation -/ noncomputable section open scoped Multiplicative open Multiplicative IsDedekindDomain variable {R : Type*} [CommRing R] [IsDedekindDomain R] {K S : Type*} [Field K] [CommSemiring S] [Algebra R K] [IsFractionRing R K] (v : HeightOneSpectrum R) namespace IsDedekindDomain.HeightOneSpectrum /-! ### Adic valuations on the Dedekind domain R -/ open scoped Classical in /-- The additive `v`-adic valuation of `r ∈ R` is the exponent of `v` in the factorization of the ideal `(r)`, if `r` is nonzero, or infinity, if `r = 0`. `intValuationDef` is the corresponding multiplicative valuation. -/ def intValuationDef (r : R) : ℤₘ₀ := if r = 0 then 0 else ↑(Multiplicative.ofAdd (-(Associates.mk v.asIdeal).count (Associates.mk (Ideal.span {r} : Ideal R)).factors : ℤ)) theorem intValuationDef_if_pos {r : R} (hr : r = 0) : v.intValuationDef r = 0 := if_pos hr @[simp] theorem intValuationDef_zero : v.intValuationDef 0 = 0 := if_pos rfl open scoped Classical in theorem intValuationDef_if_neg {r : R} (hr : r ≠ 0) : v.intValuationDef r = Multiplicative.ofAdd (-(Associates.mk v.asIdeal).count (Associates.mk (Ideal.span {r} : Ideal R)).factors : ℤ) := if_neg hr /-- Nonzero elements have nonzero adic valuation. -/ theorem intValuation_ne_zero (x : R) (hx : x ≠ 0) : v.intValuationDef x ≠ 0 := by rw [intValuationDef, if_neg hx] exact WithZero.coe_ne_zero /-- Nonzero divisors have nonzero valuation. -/ theorem intValuation_ne_zero' (x : nonZeroDivisors R) : v.intValuationDef x ≠ 0 := v.intValuation_ne_zero x (nonZeroDivisors.coe_ne_zero x) /-- Nonzero divisors have valuation greater than zero. -/ theorem intValuation_zero_le (x : nonZeroDivisors R) : 0 < v.intValuationDef x := by rw [v.intValuationDef_if_neg (nonZeroDivisors.coe_ne_zero x)] exact WithZero.zero_lt_coe _ /-- The `v`-adic valuation on `R` is bounded above by 1. -/ theorem intValuation_le_one (x : R) : v.intValuationDef x ≤ 1 := by rw [intValuationDef] by_cases hx : x = 0 · rw [if_pos hx]; exact WithZero.zero_le 1 · rw [if_neg hx, ← WithZero.coe_one, ← ofAdd_zero, WithZero.coe_le_coe, ofAdd_le, Right.neg_nonpos_iff] exact Int.natCast_nonneg _ /-- The `v`-adic valuation of `r ∈ R` is less than 1 if and only if `v` divides the ideal `(r)`. -/ theorem intValuation_lt_one_iff_dvd (r : R) : v.intValuationDef r < 1 ↔ v.asIdeal ∣ Ideal.span {r} := by classical rw [intValuationDef] split_ifs with hr · simp [hr] · rw [← WithZero.coe_one, ← ofAdd_zero, WithZero.coe_lt_coe, ofAdd_lt, neg_lt_zero, ← Int.ofNat_zero, Int.ofNat_lt, zero_lt_iff] have h : (Ideal.span {r} : Ideal R) ≠ 0 := by rw [Ne, Ideal.zero_eq_bot, Ideal.span_singleton_eq_bot] exact hr apply Associates.count_ne_zero_iff_dvd h (by apply v.irreducible) /-- The `v`-adic valuation of `r ∈ R` is less than `Multiplicative.ofAdd (-n)` if and only if `vⁿ` divides the ideal `(r)`. -/ theorem intValuation_le_pow_iff_dvd (r : R) (n : ℕ) : v.intValuationDef r ≤ Multiplicative.ofAdd (-(n : ℤ)) ↔ v.asIdeal ^ n ∣ Ideal.span {r} := by classical rw [intValuationDef] split_ifs with hr · simp_rw [hr, Ideal.dvd_span_singleton, zero_le', Submodule.zero_mem] · rw [WithZero.coe_le_coe, ofAdd_le, neg_le_neg_iff, Int.ofNat_le, Ideal.dvd_span_singleton, ← Associates.le_singleton_iff, Associates.prime_pow_dvd_iff_le (Associates.mk_ne_zero'.mpr hr) (by apply v.associates_irreducible)] /-- The `v`-adic valuation of `0 : R` equals 0. -/ theorem intValuation.map_zero' : v.intValuationDef 0 = 0 := v.intValuationDef_if_pos (Eq.refl 0) /-- The `v`-adic valuation of `1 : R` equals 1. -/ theorem intValuation.map_one' : v.intValuationDef 1 = 1 := by classical rw [v.intValuationDef_if_neg (zero_ne_one.symm : (1 : R) ≠ 0), Ideal.span_singleton_one, ← Ideal.one_eq_top, Associates.mk_one, Associates.factors_one, Associates.count_zero (by apply v.associates_irreducible), Int.ofNat_zero, neg_zero, ofAdd_zero, WithZero.coe_one] /-- The `v`-adic valuation of a product equals the product of the valuations. -/ theorem intValuation.map_mul' (x y : R) : v.intValuationDef (x * y) = v.intValuationDef x * v.intValuationDef y := by classical simp only [intValuationDef] by_cases hx : x = 0 · rw [hx, zero_mul, if_pos (Eq.refl _), zero_mul] · by_cases hy : y = 0 · rw [hy, mul_zero, if_pos (Eq.refl _), mul_zero] · rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← WithZero.coe_mul, WithZero.coe_inj, ← ofAdd_add, ← Ideal.span_singleton_mul_span_singleton, ← Associates.mk_mul_mk, ← neg_add, Associates.count_mul (by apply Associates.mk_ne_zero'.mpr hx) (by apply Associates.mk_ne_zero'.mpr hy) (by apply v.associates_irreducible)] rfl theorem intValuation.le_max_iff_min_le {a b c : ℕ} : Multiplicative.ofAdd (-c : ℤ) ≤ max (Multiplicative.ofAdd (-a : ℤ)) (Multiplicative.ofAdd (-b : ℤ)) ↔ min a b ≤ c := by rw [le_max_iff, ofAdd_le, ofAdd_le, neg_le_neg_iff, neg_le_neg_iff, Int.ofNat_le, Int.ofNat_le, ← min_le_iff] /-- The `v`-adic valuation of a sum is bounded above by the maximum of the valuations. -/ theorem intValuation.map_add_le_max' (x y : R) : v.intValuationDef (x + y) ≤ max (v.intValuationDef x) (v.intValuationDef y) := by classical by_cases hx : x = 0 · rw [hx, zero_add] conv_rhs => rw [intValuationDef, if_pos (Eq.refl _)] rw [max_eq_right (WithZero.zero_le (v.intValuationDef y))] · by_cases hy : y = 0 · rw [hy, add_zero] conv_rhs => rw [max_comm, intValuationDef, if_pos (Eq.refl _)] rw [max_eq_right (WithZero.zero_le (v.intValuationDef x))] · by_cases hxy : x + y = 0 · rw [intValuationDef, if_pos hxy]; exact zero_le' · rw [v.intValuationDef_if_neg hxy, v.intValuationDef_if_neg hx, v.intValuationDef_if_neg hy, WithZero.le_max_iff, intValuation.le_max_iff_min_le] set nmin := min ((Associates.mk v.asIdeal).count (Associates.mk (Ideal.span { x })).factors) ((Associates.mk v.asIdeal).count (Associates.mk (Ideal.span { y })).factors) have h_dvd_x : x ∈ v.asIdeal ^ nmin := by rw [← Associates.le_singleton_iff x nmin _, Associates.prime_pow_dvd_iff_le (Associates.mk_ne_zero'.mpr hx) _] · exact min_le_left _ _ apply v.associates_irreducible have h_dvd_y : y ∈ v.asIdeal ^ nmin := by rw [← Associates.le_singleton_iff y nmin _, Associates.prime_pow_dvd_iff_le (Associates.mk_ne_zero'.mpr hy) _] · exact min_le_right _ _ apply v.associates_irreducible have h_dvd_xy : Associates.mk v.asIdeal ^ nmin ≤ Associates.mk (Ideal.span {x + y}) := by rw [Associates.le_singleton_iff] exact Ideal.add_mem (v.asIdeal ^ nmin) h_dvd_x h_dvd_y rw [Associates.prime_pow_dvd_iff_le (Associates.mk_ne_zero'.mpr hxy) _] at h_dvd_xy · exact h_dvd_xy apply v.associates_irreducible /-- The `v`-adic valuation on `R`. -/ @[simps] def intValuation : Valuation R ℤₘ₀ where toFun := v.intValuationDef map_zero' := intValuation.map_zero' v map_one' := intValuation.map_one' v map_mul' := intValuation.map_mul' v map_add_le_max' := intValuation.map_add_le_max' v theorem intValuation_apply {r : R} (v : IsDedekindDomain.HeightOneSpectrum R) : intValuation v r = intValuationDef v r := rfl /-- There exists `π ∈ R` with `v`-adic valuation `Multiplicative.ofAdd (-1)`. -/ theorem intValuation_exists_uniformizer : ∃ π : R, v.intValuationDef π = Multiplicative.ofAdd (-1 : ℤ) := by classical have hv : _root_.Irreducible (Associates.mk v.asIdeal) := v.associates_irreducible have hlt : v.asIdeal ^ 2 < v.asIdeal := by rw [← Ideal.dvdNotUnit_iff_lt]
exact ⟨v.ne_bot, v.asIdeal, (not_congr Ideal.isUnit_iff).mpr (Ideal.IsPrime.ne_top v.isPrime), sq v.asIdeal⟩ obtain ⟨π, mem, nmem⟩ := SetLike.exists_of_lt hlt have hπ : Associates.mk (Ideal.span {π}) ≠ 0 := by rw [Associates.mk_ne_zero'] intro h rw [h] at nmem exact nmem (Submodule.zero_mem (v.asIdeal ^ 2)) use π rw [intValuationDef, if_neg (Associates.mk_ne_zero'.mp hπ), WithZero.coe_inj] apply congr_arg rw [neg_inj, ← Int.ofNat_one, Int.natCast_inj] rw [← Ideal.dvd_span_singleton, ← Associates.mk_le_mk_iff_dvd] at mem nmem rw [← pow_one (Associates.mk v.asIdeal), Associates.prime_pow_dvd_iff_le hπ hv] at mem rw [Associates.mk_pow, Associates.prime_pow_dvd_iff_le hπ hv, not_le] at nmem exact Nat.eq_of_le_of_lt_succ mem nmem /-- The `I`-adic valuation of a generator of `I` equals `(-1 : ℤₘ₀)` -/ theorem intValuation_singleton {r : R} (hr : r ≠ 0) (hv : v.asIdeal = Ideal.span {r}) : v.intValuation r = Multiplicative.ofAdd (-1 : ℤ) := by classical
Mathlib/RingTheory/DedekindDomain/AdicValuation.lean
233
254
/- 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.Data.Finset.Sort import Mathlib.Data.Fin.VecNotation import Mathlib.Data.Sign import Mathlib.LinearAlgebra.AffineSpace.Combination import Mathlib.LinearAlgebra.AffineSpace.AffineEquiv import Mathlib.LinearAlgebra.Basis.VectorSpace /-! # Affine independence This file defines affinely independent families of points. ## Main definitions * `AffineIndependent` defines affinely independent families of points as those where no nontrivial weighted subtraction is `0`. This is proved equivalent to two other formulations: linear independence of the results of subtracting a base point in the family from the other points in the family, or any equal affine combinations having the same weights. A bundled type `Simplex` is provided for finite affinely independent families of points, with an abbreviation `Triangle` for the case of three points. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable section open Finset Function open scoped Affine section AffineIndependent variable (k : Type*) {V : Type*} {P : Type*} [Ring k] [AddCommGroup V] [Module k V] variable [AffineSpace V P] {ι : Type*} /-- An indexed family is said to be affinely independent if no nontrivial weighted subtractions (where the sum of weights is 0) are 0. -/ def AffineIndependent (p : ι → P) : Prop := ∀ (s : Finset ι) (w : ι → k), ∑ i ∈ s, w i = 0 → s.weightedVSub p w = (0 : V) → ∀ i ∈ s, w i = 0 /-- The definition of `AffineIndependent`. -/ theorem affineIndependent_def (p : ι → P) : AffineIndependent k p ↔ ∀ (s : Finset ι) (w : ι → k), ∑ i ∈ s, w i = 0 → s.weightedVSub p w = (0 : V) → ∀ i ∈ s, w i = 0 := Iff.rfl /-- A family with at most one point is affinely independent. -/ theorem affineIndependent_of_subsingleton [Subsingleton ι] (p : ι → P) : AffineIndependent k p := fun _ _ h _ i hi => Fintype.eq_of_subsingleton_of_sum_eq h i hi /-- A family indexed by a `Fintype` is affinely independent if and only if no nontrivial weighted subtractions over `Finset.univ` (where the sum of the weights is 0) are 0. -/ theorem affineIndependent_iff_of_fintype [Fintype ι] (p : ι → P) : AffineIndependent k p ↔ ∀ w : ι → k, ∑ i, w i = 0 → Finset.univ.weightedVSub p w = (0 : V) → ∀ i, w i = 0 := by constructor · exact fun h w hw hs i => h Finset.univ w hw hs i (Finset.mem_univ _) · intro h s w hw hs i hi rw [Finset.weightedVSub_indicator_subset _ _ (Finset.subset_univ s)] at hs rw [← Finset.sum_indicator_subset _ (Finset.subset_univ s)] at hw replace h := h ((↑s : Set ι).indicator w) hw hs i simpa [hi] using h @[simp] lemma affineIndependent_vadd {p : ι → P} {v : V} : AffineIndependent k (v +ᵥ p) ↔ AffineIndependent k p := by simp +contextual [AffineIndependent, weightedVSub_vadd] protected alias ⟨AffineIndependent.of_vadd, AffineIndependent.vadd⟩ := affineIndependent_vadd @[simp] lemma affineIndependent_smul {G : Type*} [Group G] [DistribMulAction G V] [SMulCommClass G k V] {p : ι → V} {a : G} : AffineIndependent k (a • p) ↔ AffineIndependent k p := by simp +contextual [AffineIndependent, weightedVSub_smul, ← smul_comm (α := V) a, ← smul_sum, smul_eq_zero_iff_eq] protected alias ⟨AffineIndependent.of_smul, AffineIndependent.smul⟩ := affineIndependent_smul /-- A family is affinely independent if and only if the differences from a base point in that family are linearly independent. -/ theorem affineIndependent_iff_linearIndependent_vsub (p : ι → P) (i1 : ι) : AffineIndependent k p ↔ LinearIndependent k fun i : { x // x ≠ i1 } => (p i -ᵥ p i1 : V) := by classical constructor · intro h rw [linearIndependent_iff'] intro s g hg i hi set f : ι → k := fun x => if hx : x = i1 then -∑ y ∈ s, g y else g ⟨x, hx⟩ with hfdef let s2 : Finset ι := insert i1 (s.map (Embedding.subtype _)) have hfg : ∀ x : { x // x ≠ i1 }, g x = f x := by intro x rw [hfdef] dsimp only rw [dif_neg x.property, Subtype.coe_eta] rw [hfg] have hf : ∑ ι ∈ s2, f ι = 0 := by rw [Finset.sum_insert (Finset.not_mem_map_subtype_of_not_property s (Classical.not_not.2 rfl)), Finset.sum_subtype_map_embedding fun x _ => (hfg x).symm] rw [hfdef] dsimp only rw [dif_pos rfl] exact neg_add_cancel _ have hs2 : s2.weightedVSub p f = (0 : V) := by set f2 : ι → V := fun x => f x • (p x -ᵥ p i1) with hf2def set g2 : { x // x ≠ i1 } → V := fun x => g x • (p x -ᵥ p i1) have hf2g2 : ∀ x : { x // x ≠ i1 }, f2 x = g2 x := by simp only [g2, hf2def] refine fun x => ?_ rw [hfg] rw [Finset.weightedVSub_eq_weightedVSubOfPoint_of_sum_eq_zero s2 f p hf (p i1), Finset.weightedVSubOfPoint_insert, Finset.weightedVSubOfPoint_apply, Finset.sum_subtype_map_embedding fun x _ => hf2g2 x] exact hg exact h s2 f hf hs2 i (Finset.mem_insert_of_mem (Finset.mem_map.2 ⟨i, hi, rfl⟩)) · intro h rw [linearIndependent_iff'] at h intro s w hw hs i hi rw [Finset.weightedVSub_eq_weightedVSubOfPoint_of_sum_eq_zero s w p hw (p i1), ← s.weightedVSubOfPoint_erase w p i1, Finset.weightedVSubOfPoint_apply] at hs let f : ι → V := fun i => w i • (p i -ᵥ p i1) have hs2 : (∑ i ∈ (s.erase i1).subtype fun i => i ≠ i1, f i) = 0 := by rw [← hs] convert Finset.sum_subtype_of_mem f fun x => Finset.ne_of_mem_erase have h2 := h ((s.erase i1).subtype fun i => i ≠ i1) (fun x => w x) hs2 simp_rw [Finset.mem_subtype] at h2 have h2b : ∀ i ∈ s, i ≠ i1 → w i = 0 := fun i his hi => h2 ⟨i, hi⟩ (Finset.mem_erase_of_ne_of_mem hi his) exact Finset.eq_zero_of_sum_eq_zero hw h2b i hi /-- A set is affinely independent if and only if the differences from a base point in that set are linearly independent. -/ theorem affineIndependent_set_iff_linearIndependent_vsub {s : Set P} {p₁ : P} (hp₁ : p₁ ∈ s) : AffineIndependent k (fun p => p : s → P) ↔ LinearIndependent k (fun v => v : (fun p => (p -ᵥ p₁ : V)) '' (s \ {p₁}) → V) := by rw [affineIndependent_iff_linearIndependent_vsub k (fun p => p : s → P) ⟨p₁, hp₁⟩] constructor · intro h have hv : ∀ v : (fun p => (p -ᵥ p₁ : V)) '' (s \ {p₁}), (v : V) +ᵥ p₁ ∈ s \ {p₁} := fun v => (vsub_left_injective p₁).mem_set_image.1 ((vadd_vsub (v : V) p₁).symm ▸ v.property) let f : (fun p : P => (p -ᵥ p₁ : V)) '' (s \ {p₁}) → { x : s // x ≠ ⟨p₁, hp₁⟩ } := fun x => ⟨⟨(x : V) +ᵥ p₁, Set.mem_of_mem_diff (hv x)⟩, fun hx => Set.not_mem_of_mem_diff (hv x) (Subtype.ext_iff.1 hx)⟩ convert h.comp f fun x1 x2 hx => Subtype.ext (vadd_right_cancel p₁ (Subtype.ext_iff.1 (Subtype.ext_iff.1 hx))) ext v exact (vadd_vsub (v : V) p₁).symm · intro h let f : { x : s // x ≠ ⟨p₁, hp₁⟩ } → (fun p : P => (p -ᵥ p₁ : V)) '' (s \ {p₁}) := fun x => ⟨((x : s) : P) -ᵥ p₁, ⟨x, ⟨⟨(x : s).property, fun hx => x.property (Subtype.ext hx)⟩, rfl⟩⟩⟩ convert h.comp f fun x1 x2 hx => Subtype.ext (Subtype.ext (vsub_left_cancel (Subtype.ext_iff.1 hx))) /-- A set of nonzero vectors is linearly independent if and only if, given a point `p₁`, the vectors added to `p₁` and `p₁` itself are affinely independent. -/ theorem linearIndependent_set_iff_affineIndependent_vadd_union_singleton {s : Set V} (hs : ∀ v ∈ s, v ≠ (0 : V)) (p₁ : P) : LinearIndependent k (fun v => v : s → V) ↔ AffineIndependent k (fun p => p : ({p₁} ∪ (fun v => v +ᵥ p₁) '' s : Set P) → P) := by rw [affineIndependent_set_iff_linearIndependent_vsub k (Set.mem_union_left _ (Set.mem_singleton p₁))] have h : (fun p => (p -ᵥ p₁ : V)) '' (({p₁} ∪ (fun v => v +ᵥ p₁) '' s) \ {p₁}) = s := by simp_rw [Set.union_diff_left, Set.image_diff (vsub_left_injective p₁), Set.image_image, Set.image_singleton, vsub_self, vadd_vsub, Set.image_id'] exact Set.diff_singleton_eq_self fun h => hs 0 h rfl rw [h] /-- A family is affinely independent if and only if any affine combinations (with sum of weights 1) that evaluate to the same point have equal `Set.indicator`. -/ theorem affineIndependent_iff_indicator_eq_of_affineCombination_eq (p : ι → P) : AffineIndependent k p ↔ ∀ (s1 s2 : Finset ι) (w1 w2 : ι → k), ∑ i ∈ s1, w1 i = 1 → ∑ i ∈ s2, w2 i = 1 → s1.affineCombination k p w1 = s2.affineCombination k p w2 → Set.indicator (↑s1) w1 = Set.indicator (↑s2) w2 := by classical constructor · intro ha s1 s2 w1 w2 hw1 hw2 heq ext i by_cases hi : i ∈ s1 ∪ s2 · rw [← sub_eq_zero] rw [← Finset.sum_indicator_subset w1 (s1.subset_union_left (s₂ := s2))] at hw1 rw [← Finset.sum_indicator_subset w2 (s1.subset_union_right)] at hw2 have hws : (∑ i ∈ s1 ∪ s2, (Set.indicator (↑s1) w1 - Set.indicator (↑s2) w2) i) = 0 := by simp [hw1, hw2] rw [Finset.affineCombination_indicator_subset w1 p (s1.subset_union_left (s₂ := s2)), Finset.affineCombination_indicator_subset w2 p s1.subset_union_right, ← @vsub_eq_zero_iff_eq V, Finset.affineCombination_vsub] at heq exact ha (s1 ∪ s2) (Set.indicator (↑s1) w1 - Set.indicator (↑s2) w2) hws heq i hi · rw [← Finset.mem_coe, Finset.coe_union] at hi have h₁ : Set.indicator (↑s1) w1 i = 0 := by simp only [Set.indicator, Finset.mem_coe, ite_eq_right_iff] intro h by_contra exact (mt (@Set.mem_union_left _ i ↑s1 ↑s2) hi) h have h₂ : Set.indicator (↑s2) w2 i = 0 := by simp only [Set.indicator, Finset.mem_coe, ite_eq_right_iff] intro h by_contra exact (mt (@Set.mem_union_right _ i ↑s2 ↑s1) hi) h simp [h₁, h₂] · intro ha s w hw hs i0 hi0 let w1 : ι → k := Function.update (Function.const ι 0) i0 1 have hw1 : ∑ i ∈ s, w1 i = 1 := by rw [Finset.sum_update_of_mem hi0] simp only [Finset.sum_const_zero, add_zero, const_apply] have hw1s : s.affineCombination k p w1 = p i0 := s.affineCombination_of_eq_one_of_eq_zero w1 p hi0 (Function.update_self ..) fun _ _ hne => Function.update_of_ne hne .. let w2 := w + w1 have hw2 : ∑ i ∈ s, w2 i = 1 := by simp_all only [w2, Pi.add_apply, Finset.sum_add_distrib, zero_add] have hw2s : s.affineCombination k p w2 = p i0 := by simp_all only [w2, ← Finset.weightedVSub_vadd_affineCombination, zero_vadd] replace ha := ha s s w2 w1 hw2 hw1 (hw1s.symm ▸ hw2s) have hws : w2 i0 - w1 i0 = 0 := by rw [← Finset.mem_coe] at hi0 rw [← Set.indicator_of_mem hi0 w2, ← Set.indicator_of_mem hi0 w1, ha, sub_self] simpa [w2] using hws /-- A finite family is affinely independent if and only if any affine combinations (with sum of weights 1) that evaluate to the same point are equal. -/ theorem affineIndependent_iff_eq_of_fintype_affineCombination_eq [Fintype ι] (p : ι → P) : AffineIndependent k p ↔ ∀ w1 w2 : ι → k, ∑ i, w1 i = 1 → ∑ i, w2 i = 1 → Finset.univ.affineCombination k p w1 = Finset.univ.affineCombination k p w2 → w1 = w2 := by rw [affineIndependent_iff_indicator_eq_of_affineCombination_eq] constructor · intro h w1 w2 hw1 hw2 hweq simpa only [Set.indicator_univ, Finset.coe_univ] using h _ _ w1 w2 hw1 hw2 hweq · intro h s1 s2 w1 w2 hw1 hw2 hweq have hw1' : (∑ i, (s1 : Set ι).indicator w1 i) = 1 := by rwa [Finset.sum_indicator_subset _ (Finset.subset_univ s1)] have hw2' : (∑ i, (s2 : Set ι).indicator w2 i) = 1 := by rwa [Finset.sum_indicator_subset _ (Finset.subset_univ s2)] rw [Finset.affineCombination_indicator_subset w1 p (Finset.subset_univ s1), Finset.affineCombination_indicator_subset w2 p (Finset.subset_univ s2)] at hweq exact h _ _ hw1' hw2' hweq variable {k} /-- If we single out one member of an affine-independent family of points and affinely transport all others along the line joining them to this member, the resulting new family of points is affine- independent. This is the affine version of `LinearIndependent.units_smul`. -/ theorem AffineIndependent.units_lineMap {p : ι → P} (hp : AffineIndependent k p) (j : ι) (w : ι → Units k) : AffineIndependent k fun i => AffineMap.lineMap (p j) (p i) (w i : k) := by rw [affineIndependent_iff_linearIndependent_vsub k _ j] at hp ⊢ simp only [AffineMap.lineMap_vsub_left, AffineMap.coe_const, AffineMap.lineMap_same, const_apply] exact hp.units_smul fun i => w i theorem AffineIndependent.indicator_eq_of_affineCombination_eq {p : ι → P} (ha : AffineIndependent k p) (s₁ s₂ : Finset ι) (w₁ w₂ : ι → k) (hw₁ : ∑ i ∈ s₁, w₁ i = 1) (hw₂ : ∑ i ∈ s₂, w₂ i = 1) (h : s₁.affineCombination k p w₁ = s₂.affineCombination k p w₂) : Set.indicator (↑s₁) w₁ = Set.indicator (↑s₂) w₂ := (affineIndependent_iff_indicator_eq_of_affineCombination_eq k p).1 ha s₁ s₂ w₁ w₂ hw₁ hw₂ h /-- An affinely independent family is injective, if the underlying ring is nontrivial. -/ protected theorem AffineIndependent.injective [Nontrivial k] {p : ι → P} (ha : AffineIndependent k p) : Function.Injective p := by intro i j hij rw [affineIndependent_iff_linearIndependent_vsub _ _ j] at ha by_contra hij' refine ha.ne_zero ⟨i, hij'⟩ (vsub_eq_zero_iff_eq.mpr ?_) simp_all only [ne_eq] /-- If a family is affinely independent, so is any subfamily given by composition of an embedding into index type with the original family. -/ theorem AffineIndependent.comp_embedding {ι2 : Type*} (f : ι2 ↪ ι) {p : ι → P} (ha : AffineIndependent k p) : AffineIndependent k (p ∘ f) := by classical intro fs w hw hs i0 hi0 let fs' := fs.map f let w' i := if h : ∃ i2, f i2 = i then w h.choose else 0 have hw' : ∀ i2 : ι2, w' (f i2) = w i2 := by intro i2 have h : ∃ i : ι2, f i = f i2 := ⟨i2, rfl⟩ have hs : h.choose = i2 := f.injective h.choose_spec simp_rw [w', dif_pos h, hs] have hw's : ∑ i ∈ fs', w' i = 0 := by rw [← hw, Finset.sum_map] simp [hw'] have hs' : fs'.weightedVSub p w' = (0 : V) := by rw [← hs, Finset.weightedVSub_map] congr with i simp_all only [comp_apply, EmbeddingLike.apply_eq_iff_eq, exists_eq, dite_true] rw [← ha fs' w' hw's hs' (f i0) ((Finset.mem_map' _).2 hi0), hw'] /-- If a family is affinely independent, so is any subfamily indexed by a subtype of the index type. -/ protected theorem AffineIndependent.subtype {p : ι → P} (ha : AffineIndependent k p) (s : Set ι) : AffineIndependent k fun i : s => p i := ha.comp_embedding (Embedding.subtype _) /-- If an indexed family of points is affinely independent, so is the corresponding set of points. -/ protected theorem AffineIndependent.range {p : ι → P} (ha : AffineIndependent k p) : AffineIndependent k (fun x => x : Set.range p → P) := by let f : Set.range p → ι := fun x => x.property.choose have hf : ∀ x, p (f x) = x := fun x => x.property.choose_spec let fe : Set.range p ↪ ι := ⟨f, fun x₁ x₂ he => Subtype.ext (hf x₁ ▸ hf x₂ ▸ he ▸ rfl)⟩ convert ha.comp_embedding fe ext simp [fe, hf] theorem affineIndependent_equiv {ι' : Type*} (e : ι ≃ ι') {p : ι' → P} : AffineIndependent k (p ∘ e) ↔ AffineIndependent k p := by refine ⟨?_, AffineIndependent.comp_embedding e.toEmbedding⟩ intro h have : p = p ∘ e ∘ e.symm.toEmbedding := by ext simp rw [this] exact h.comp_embedding e.symm.toEmbedding /-- If a set of points is affinely independent, so is any subset. -/ protected theorem AffineIndependent.mono {s t : Set P} (ha : AffineIndependent k (fun x => x : t → P)) (hs : s ⊆ t) : AffineIndependent k (fun x => x : s → P) := ha.comp_embedding (s.embeddingOfSubset t hs) /-- If the range of an injective indexed family of points is affinely independent, so is that family. -/ theorem AffineIndependent.of_set_of_injective {p : ι → P} (ha : AffineIndependent k (fun x => x : Set.range p → P)) (hi : Function.Injective p) : AffineIndependent k p := ha.comp_embedding (⟨fun i => ⟨p i, Set.mem_range_self _⟩, fun _ _ h => hi (Subtype.mk_eq_mk.1 h)⟩ : ι ↪ Set.range p) section Composition variable {V₂ P₂ : Type*} [AddCommGroup V₂] [Module k V₂] [AffineSpace V₂ P₂] /-- If the image of a family of points in affine space under an affine transformation is affine- independent, then the original family of points is also affine-independent. -/ theorem AffineIndependent.of_comp {p : ι → P} (f : P →ᵃ[k] P₂) (hai : AffineIndependent k (f ∘ p)) : AffineIndependent k p := by rcases isEmpty_or_nonempty ι with h | h · haveI := h apply affineIndependent_of_subsingleton obtain ⟨i⟩ := h rw [affineIndependent_iff_linearIndependent_vsub k p i] simp_rw [affineIndependent_iff_linearIndependent_vsub k (f ∘ p) i, Function.comp_apply, ← f.linearMap_vsub] at hai exact LinearIndependent.of_comp f.linear hai /-- The image of a family of points in affine space, under an injective affine transformation, is affine-independent. -/ theorem AffineIndependent.map' {p : ι → P} (hai : AffineIndependent k p) (f : P →ᵃ[k] P₂) (hf : Function.Injective f) : AffineIndependent k (f ∘ p) := by rcases isEmpty_or_nonempty ι with h | h · haveI := h apply affineIndependent_of_subsingleton obtain ⟨i⟩ := h rw [affineIndependent_iff_linearIndependent_vsub k p i] at hai simp_rw [affineIndependent_iff_linearIndependent_vsub k (f ∘ p) i, Function.comp_apply, ← f.linearMap_vsub] have hf' : LinearMap.ker f.linear = ⊥ := by rwa [LinearMap.ker_eq_bot, f.linear_injective_iff] exact LinearIndependent.map' hai f.linear hf' /-- Injective affine maps preserve affine independence. -/ theorem AffineMap.affineIndependent_iff {p : ι → P} (f : P →ᵃ[k] P₂) (hf : Function.Injective f) : AffineIndependent k (f ∘ p) ↔ AffineIndependent k p := ⟨AffineIndependent.of_comp f, fun hai => AffineIndependent.map' hai f hf⟩ /-- Affine equivalences preserve affine independence of families of points. -/ theorem AffineEquiv.affineIndependent_iff {p : ι → P} (e : P ≃ᵃ[k] P₂) : AffineIndependent k (e ∘ p) ↔ AffineIndependent k p := e.toAffineMap.affineIndependent_iff e.toEquiv.injective /-- Affine equivalences preserve affine independence of subsets. -/ theorem AffineEquiv.affineIndependent_set_of_eq_iff {s : Set P} (e : P ≃ᵃ[k] P₂) : AffineIndependent k ((↑) : e '' s → P₂) ↔ AffineIndependent k ((↑) : s → P) := by have : e ∘ ((↑) : s → P) = ((↑) : e '' s → P₂) ∘ (e : P ≃ P₂).image s := rfl -- This used to be `rw`, but we need `erw` after https://github.com/leanprover/lean4/pull/2644 erw [← e.affineIndependent_iff, this, affineIndependent_equiv] end Composition /-- If a family is affinely independent, and the spans of points indexed by two subsets of the index type have a point in common, those subsets of the index type have an element in common, if the underlying ring is nontrivial. -/ theorem AffineIndependent.exists_mem_inter_of_exists_mem_inter_affineSpan [Nontrivial k] {p : ι → P} (ha : AffineIndependent k p) {s1 s2 : Set ι} {p0 : P} (hp0s1 : p0 ∈ affineSpan k (p '' s1)) (hp0s2 : p0 ∈ affineSpan k (p '' s2)) : ∃ i : ι, i ∈ s1 ∩ s2 := by rw [Set.image_eq_range] at hp0s1 hp0s2 rw [mem_affineSpan_iff_eq_affineCombination, ← Finset.eq_affineCombination_subset_iff_eq_affineCombination_subtype] at hp0s1 hp0s2 rcases hp0s1 with ⟨fs1, hfs1, w1, hw1, hp0s1⟩ rcases hp0s2 with ⟨fs2, hfs2, w2, hw2, hp0s2⟩ rw [affineIndependent_iff_indicator_eq_of_affineCombination_eq] at ha replace ha := ha fs1 fs2 w1 w2 hw1 hw2 (hp0s1 ▸ hp0s2) have hnz : ∑ i ∈ fs1, w1 i ≠ 0 := hw1.symm ▸ one_ne_zero rcases Finset.exists_ne_zero_of_sum_ne_zero hnz with ⟨i, hifs1, hinz⟩ simp_rw [← Set.indicator_of_mem (Finset.mem_coe.2 hifs1) w1, ha] at hinz use i, hfs1 hifs1 exact hfs2 (Set.mem_of_indicator_ne_zero hinz) /-- If a family is affinely independent, the spans of points indexed by disjoint subsets of the index type are disjoint, if the underlying ring is nontrivial. -/ theorem AffineIndependent.affineSpan_disjoint_of_disjoint [Nontrivial k] {p : ι → P} (ha : AffineIndependent k p) {s1 s2 : Set ι} (hd : Disjoint s1 s2) : Disjoint (affineSpan k (p '' s1) : Set P) (affineSpan k (p '' s2)) := by refine Set.disjoint_left.2 fun p0 hp0s1 hp0s2 => ?_ obtain ⟨i, hi⟩ := ha.exists_mem_inter_of_exists_mem_inter_affineSpan hp0s1 hp0s2 exact Set.disjoint_iff.1 hd hi /-- If a family is affinely independent, a point in the family is in the span of some of the points given by a subset of the index type if and only if that point's index is in the subset, if the underlying ring is nontrivial. -/ @[simp]
protected theorem AffineIndependent.mem_affineSpan_iff [Nontrivial k] {p : ι → P} (ha : AffineIndependent k p) (i : ι) (s : Set ι) : p i ∈ affineSpan k (p '' s) ↔ i ∈ s := by constructor · intro hs have h := AffineIndependent.exists_mem_inter_of_exists_mem_inter_affineSpan ha hs
Mathlib/LinearAlgebra/AffineSpace/Independent.lean
433
438
/- 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.Analysis.Convex.Function import Mathlib.Analysis.Convex.StrictConvexSpace import Mathlib.MeasureTheory.Function.AEEqOfIntegral import Mathlib.MeasureTheory.Integral.Average /-! # Jensen's inequality for integrals In this file we prove several forms of Jensen's inequality for integrals. - for convex sets: `Convex.average_mem`, `Convex.set_average_mem`, `Convex.integral_mem`; - for convex functions: `ConvexOn.average_mem_epigraph`, `ConvexOn.map_average_le`, `ConvexOn.set_average_mem_epigraph`, `ConvexOn.map_set_average_le`, `ConvexOn.map_integral_le`; - for strictly convex sets: `StrictConvex.ae_eq_const_or_average_mem_interior`; - for a closed ball in a strictly convex normed space: `ae_eq_const_or_norm_integral_lt_of_norm_le_const`; - for strictly convex functions: `StrictConvexOn.ae_eq_const_or_map_average_lt`. ## TODO - Use a typeclass for strict convexity of a closed ball. ## Tags convex, integral, center mass, average value, Jensen's inequality -/ open MeasureTheory MeasureTheory.Measure Metric Set Filter TopologicalSpace Function open scoped Topology ENNReal Convex variable {α E : Type*} {m0 : MeasurableSpace α} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E] {μ : Measure α} {s : Set E} {t : Set α} {f : α → E} {g : E → ℝ} {C : ℝ} /-! ### Non-strict Jensen's inequality -/ /-- If `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `Convex.sum_mem` for a finite sum version of this lemma. -/ theorem Convex.integral_mem [IsProbabilityMeasure μ] (hs : Convex ℝ s) (hsc : IsClosed s) (hf : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) : (∫ x, f x ∂μ) ∈ s := by borelize E rcases hfi.aestronglyMeasurable with ⟨g, hgm, hfg⟩ haveI : SeparableSpace (range g ∩ s : Set E) := (hgm.isSeparable_range.mono inter_subset_left).separableSpace obtain ⟨y₀, h₀⟩ : (range g ∩ s).Nonempty := by rcases (hf.and hfg).exists with ⟨x₀, h₀⟩ exact ⟨f x₀, by simp only [h₀.2, mem_range_self], h₀.1⟩ rw [integral_congr_ae hfg]; rw [integrable_congr hfg] at hfi have hg : ∀ᵐ x ∂μ, g x ∈ closure (range g ∩ s) := by filter_upwards [hfg.rw (fun _ y => y ∈ s) hf] with x hx apply subset_closure exact ⟨mem_range_self _, hx⟩ set G : ℕ → SimpleFunc α E := SimpleFunc.approxOn _ hgm.measurable (range g ∩ s) y₀ h₀ have : Tendsto (fun n => (G n).integral μ) atTop (𝓝 <| ∫ x, g x ∂μ) := tendsto_integral_approxOn_of_measurable hfi _ hg _ (integrable_const _) refine hsc.mem_of_tendsto this (Eventually.of_forall fun n => hs.sum_mem ?_ ?_ ?_) · exact fun _ _ => ENNReal.toReal_nonneg · simp_rw [measureReal_def] rw [← ENNReal.toReal_sum, (G n).sum_range_measure_preimage_singleton, measure_univ, ENNReal.toReal_one] exact fun _ _ => measure_ne_top _ _ · simp only [SimpleFunc.mem_range, forall_mem_range] intro x apply (range g).inter_subset_right exact SimpleFunc.approxOn_mem hgm.measurable h₀ _ _ /-- If `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`: `⨍ x, f x ∂μ ∈ s`. See also `Convex.centerMass_mem` for a finite sum version of this lemma. -/ theorem Convex.average_mem [IsFiniteMeasure μ] [NeZero μ] (hs : Convex ℝ s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) : (⨍ x, f x ∂μ) ∈ s := hs.integral_mem hsc (ae_mono' smul_absolutelyContinuous hfs) hfi.to_average /-- If `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`: `⨍ x, f x ∂μ ∈ s`. See also `Convex.centerMass_mem` for a finite sum version of this lemma. -/ theorem Convex.set_average_mem (hs : Convex ℝ s) (hsc : IsClosed s) (h0 : μ t ≠ 0) (ht : μ t ≠ ∞) (hfs : ∀ᵐ x ∂μ.restrict t, f x ∈ s) (hfi : IntegrableOn f t μ) : (⨍ x in t, f x ∂μ) ∈ s := have := Fact.mk ht.lt_top have := NeZero.mk h0 hs.average_mem hsc hfs hfi /-- If `μ` is a non-zero finite measure on `α`, `s` is a convex set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `closure s`: `⨍ x, f x ∂μ ∈ s`. See also `Convex.centerMass_mem` for a finite sum version of this lemma. -/ theorem Convex.set_average_mem_closure (hs : Convex ℝ s) (h0 : μ t ≠ 0) (ht : μ t ≠ ∞) (hfs : ∀ᵐ x ∂μ.restrict t, f x ∈ s) (hfi : IntegrableOn f t μ) : (⨍ x in t, f x ∂μ) ∈ closure s := hs.closure.set_average_mem isClosed_closure h0 ht (hfs.mono fun _ hx => subset_closure hx) hfi theorem ConvexOn.average_mem_epigraph [IsFiniteMeasure μ] [NeZero μ] (hg : ConvexOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) (hgi : Integrable (g ∘ f) μ) : (⨍ x, f x ∂μ, ⨍ x, g (f x) ∂μ) ∈ {p : E × ℝ | p.1 ∈ s ∧ g p.1 ≤ p.2} := by have ht_mem : ∀ᵐ x ∂μ, (f x, g (f x)) ∈ {p : E × ℝ | p.1 ∈ s ∧ g p.1 ≤ p.2} := hfs.mono fun x hx => ⟨hx, le_rfl⟩ exact average_pair hfi hgi ▸ hg.convex_epigraph.average_mem (hsc.epigraph hgc) ht_mem (hfi.prodMk hgi) theorem ConcaveOn.average_mem_hypograph [IsFiniteMeasure μ] [NeZero μ] (hg : ConcaveOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) (hgi : Integrable (g ∘ f) μ) : (⨍ x, f x ∂μ, ⨍ x, g (f x) ∂μ) ∈ {p : E × ℝ | p.1 ∈ s ∧ p.2 ≤ g p.1} := by simpa only [mem_setOf_eq, Pi.neg_apply, average_neg, neg_le_neg_iff] using hg.neg.average_mem_epigraph hgc.neg hsc hfs hfi hgi.neg /-- **Jensen's inequality**: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `ConvexOn.map_centerMass_le` for a finite sum version of this lemma. -/ theorem ConvexOn.map_average_le [IsFiniteMeasure μ] [NeZero μ] (hg : ConvexOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) (hgi : Integrable (g ∘ f) μ) : g (⨍ x, f x ∂μ) ≤ ⨍ x, g (f x) ∂μ := (hg.average_mem_epigraph hgc hsc hfs hfi hgi).2 /-- **Jensen's inequality**: if a function `g : E → ℝ` is concave and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the average value of `g ∘ f` is less than or equal to the value of `g` at the average value of `f` provided that both `f` and `g ∘ f` are integrable. See also `ConcaveOn.le_map_centerMass` for a finite sum version of this lemma. -/ theorem ConcaveOn.le_map_average [IsFiniteMeasure μ] [NeZero μ] (hg : ConcaveOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) (hgi : Integrable (g ∘ f) μ) : (⨍ x, g (f x) ∂μ) ≤ g (⨍ x, f x ∂μ) := (hg.average_mem_hypograph hgc hsc hfs hfi hgi).2 /-- **Jensen's inequality**: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points of a set `t` to `s`, then the value of `g` at the average value of `f` over `t` is less than or equal to the average value of `g ∘ f` over `t` provided that both `f` and `g ∘ f` are integrable. -/ theorem ConvexOn.set_average_mem_epigraph (hg : ConvexOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (h0 : μ t ≠ 0) (ht : μ t ≠ ∞) (hfs : ∀ᵐ x ∂μ.restrict t, f x ∈ s) (hfi : IntegrableOn f t μ) (hgi : IntegrableOn (g ∘ f) t μ) : (⨍ x in t, f x ∂μ, ⨍ x in t, g (f x) ∂μ) ∈ {p : E × ℝ | p.1 ∈ s ∧ g p.1 ≤ p.2} := have := Fact.mk ht.lt_top have := NeZero.mk h0 hg.average_mem_epigraph hgc hsc hfs hfi hgi /-- **Jensen's inequality**: if a function `g : E → ℝ` is concave and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points of a set `t` to `s`, then the average value of `g ∘ f` over `t` is less than or equal to the value of `g` at the average value of `f` over `t` provided that both `f` and `g ∘ f` are integrable. -/ theorem ConcaveOn.set_average_mem_hypograph (hg : ConcaveOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (h0 : μ t ≠ 0) (ht : μ t ≠ ∞) (hfs : ∀ᵐ x ∂μ.restrict t, f x ∈ s) (hfi : IntegrableOn f t μ) (hgi : IntegrableOn (g ∘ f) t μ) : (⨍ x in t, f x ∂μ, ⨍ x in t, g (f x) ∂μ) ∈ {p : E × ℝ | p.1 ∈ s ∧ p.2 ≤ g p.1} := by simpa only [mem_setOf_eq, Pi.neg_apply, average_neg, neg_le_neg_iff] using hg.neg.set_average_mem_epigraph hgc.neg hsc h0 ht hfs hfi hgi.neg /-- **Jensen's inequality**: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points of a set `t` to `s`, then the value of `g` at the average value of `f` over `t` is less than or equal to the average value of `g ∘ f` over `t` provided that both `f` and `g ∘ f` are integrable. -/ theorem ConvexOn.map_set_average_le (hg : ConvexOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (h0 : μ t ≠ 0) (ht : μ t ≠ ∞) (hfs : ∀ᵐ x ∂μ.restrict t, f x ∈ s) (hfi : IntegrableOn f t μ) (hgi : IntegrableOn (g ∘ f) t μ) : g (⨍ x in t, f x ∂μ) ≤ ⨍ x in t, g (f x) ∂μ := (hg.set_average_mem_epigraph hgc hsc h0 ht hfs hfi hgi).2 /-- **Jensen's inequality**: if a function `g : E → ℝ` is concave and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points of a set `t` to `s`, then the average value of `g ∘ f` over `t` is less than or equal to the value of `g` at the average value of `f` over `t` provided that both `f` and `g ∘ f` are integrable. -/ theorem ConcaveOn.le_map_set_average (hg : ConcaveOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (h0 : μ t ≠ 0) (ht : μ t ≠ ∞) (hfs : ∀ᵐ x ∂μ.restrict t, f x ∈ s) (hfi : IntegrableOn f t μ) (hgi : IntegrableOn (g ∘ f) t μ) : (⨍ x in t, g (f x) ∂μ) ≤ g (⨍ x in t, f x ∂μ) := (hg.set_average_mem_hypograph hgc hsc h0 ht hfs hfi hgi).2 /-- **Jensen's inequality**: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `ConvexOn.map_centerMass_le` for a finite sum version of this lemma. -/ theorem ConvexOn.map_integral_le [IsProbabilityMeasure μ] (hg : ConvexOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) (hgi : Integrable (g ∘ f) μ) : g (∫ x, f x ∂μ) ≤ ∫ x, g (f x) ∂μ := by simpa only [average_eq_integral] using hg.map_average_le hgc hsc hfs hfi hgi /-- **Jensen's inequality**: if a function `g : E → ℝ` is concave and continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the expected value of `g ∘ f` is less than or equal to the value of `g` at the expected value of `f` provided that both `f` and `g ∘ f` are integrable. -/ theorem ConcaveOn.le_map_integral [IsProbabilityMeasure μ] (hg : ConcaveOn ℝ s g) (hgc : ContinuousOn g s) (hsc : IsClosed s) (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : Integrable f μ) (hgi : Integrable (g ∘ f) μ) : (∫ x, g (f x) ∂μ) ≤ g (∫ x, f x ∂μ) := by simpa only [average_eq_integral] using hg.le_map_average hgc hsc hfs hfi hgi /-!
### Strict Jensen's inequality -/
Mathlib/Analysis/Convex/Integral.lean
210
213
/- 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, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Basic /-! # Lemmas about `min` and `max` in an ordered monoid. -/ open Function variable {α β : Type*} /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ section CommSemigroup variable [LinearOrder α] [CommSemigroup β] @[to_additive] lemma fn_min_mul_fn_max (f : α → β) (a b : α) : f (min a b) * f (max a b) = f a * f b := by obtain h | h := le_total a b <;> simp [h, mul_comm] @[to_additive] lemma fn_max_mul_fn_min (f : α → β) (a b : α) : f (max a b) * f (min a b) = f a * f b := by obtain h | h := le_total a b <;> simp [h, mul_comm] variable [CommSemigroup α] @[to_additive (attr := simp)] lemma min_mul_max (a b : α) : min a b * max a b = a * b := fn_min_mul_fn_max id _ _ @[to_additive (attr := simp)] lemma max_mul_min (a b : α) : max a b * min a b = a * b := fn_max_mul_fn_min id _ _ end CommSemigroup section CovariantClassMulLe variable [LinearOrder α] section Mul variable [Mul α] section Left variable [MulLeftMono α] @[to_additive] theorem min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c := (monotone_id.const_mul' a).map_min.symm @[to_additive] theorem max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c := (monotone_id.const_mul' a).map_max.symm end Left section Right variable [MulRightMono α] @[to_additive] theorem min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c := (monotone_id.mul_const' c).map_min.symm @[to_additive] theorem max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c := (monotone_id.mul_const' c).map_max.symm end Right @[to_additive] theorem lt_or_lt_of_mul_lt_mul [MulLeftMono α] [MulRightMono α] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ < a₂ * b₂ → a₁ < a₂ ∨ b₁ < b₂ := by contrapose! exact fun h => mul_le_mul' h.1 h.2 @[to_additive] theorem le_or_lt_of_mul_le_mul [MulLeftMono α] [MulRightStrictMono α] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ ≤ a₂ * b₂ → a₁ ≤ a₂ ∨ b₁ < b₂ := by contrapose! exact fun h => mul_lt_mul_of_lt_of_le h.1 h.2 @[to_additive] theorem lt_or_le_of_mul_le_mul [MulLeftStrictMono α] [MulRightMono α] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ ≤ a₂ * b₂ → a₁ < a₂ ∨ b₁ ≤ b₂ := by contrapose! exact fun h => mul_lt_mul_of_le_of_lt h.1 h.2 @[to_additive] theorem le_or_le_of_mul_le_mul [MulLeftStrictMono α] [MulRightStrictMono α] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ ≤ a₂ * b₂ → a₁ ≤ a₂ ∨ b₁ ≤ b₂ := by contrapose!
exact fun h => mul_lt_mul_of_lt_of_lt h.1 h.2 @[to_additive] theorem mul_lt_mul_iff_of_le_of_le [MulLeftMono α] [MulRightMono α] [MulLeftStrictMono α]
Mathlib/Algebra/Order/Monoid/Unbundled/MinMax.lean
99
103
/- Copyright (c) 2024 Violeta Hernández Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta Hernández Palacios -/ import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.SetTheory.Ordinal.Principal /-! # Ordinal arithmetic with cardinals This file collects results about the cardinality of different ordinal operations. -/ universe u v open Cardinal Ordinal Set /-! ### Cardinal operations with ordinal indices -/ namespace Cardinal /-- Bounds the cardinal of an ordinal-indexed union of sets. -/ lemma mk_iUnion_Ordinal_lift_le_of_le {β : Type v} {o : Ordinal.{u}} {c : Cardinal.{v}} (ho : lift.{v} o.card ≤ lift.{u} c) (hc : ℵ₀ ≤ c) (A : Ordinal → Set β) (hA : ∀ j < o, #(A j) ≤ c) : #(⋃ j < o, A j) ≤ c := by simp_rw [← mem_Iio, biUnion_eq_iUnion, iUnion, iSup, ← o.enumIsoToType.symm.surjective.range_comp] rw [← lift_le.{u}] apply ((mk_iUnion_le_lift _).trans _).trans_eq (mul_eq_self (aleph0_le_lift.2 hc)) rw [mk_toType] refine mul_le_mul' ho (ciSup_le' ?_) intro i simpa using hA _ (o.enumIsoToType.symm i).2 lemma mk_iUnion_Ordinal_le_of_le {β : Type*} {o : Ordinal} {c : Cardinal} (ho : o.card ≤ c) (hc : ℵ₀ ≤ c) (A : Ordinal → Set β) (hA : ∀ j < o, #(A j) ≤ c) : #(⋃ j < o, A j) ≤ c := by apply mk_iUnion_Ordinal_lift_le_of_le _ hc A hA rwa [Cardinal.lift_le] end Cardinal @[deprecated mk_iUnion_Ordinal_le_of_le (since := "2024-11-02")] alias Ordinal.Cardinal.mk_iUnion_Ordinal_le_of_le := mk_iUnion_Ordinal_le_of_le /-! ### Cardinality of ordinals -/ namespace Ordinal theorem lift_card_iSup_le_sum_card {ι : Type u} [Small.{v} ι] (f : ι → Ordinal.{v}) : Cardinal.lift.{u} (⨆ i, f i).card ≤ Cardinal.sum fun i ↦ (f i).card := by simp_rw [← mk_toType] rw [← mk_sigma, ← Cardinal.lift_id'.{v} #(Σ _, _), ← Cardinal.lift_umax.{v, u}] apply lift_mk_le_lift_mk_of_surjective (f := enumIsoToType _ ∘ (⟨(enumIsoToType _).symm ·.2, (mem_Iio.mp ((enumIsoToType _).symm _).2).trans_le (Ordinal.le_iSup _ _)⟩)) rw [EquivLike.comp_surjective] rintro ⟨x, hx⟩ obtain ⟨i, hi⟩ := Ordinal.lt_iSup_iff.mp hx exact ⟨⟨i, enumIsoToType _ ⟨x, hi⟩⟩, by simp⟩ theorem card_iSup_le_sum_card {ι : Type u} (f : ι → Ordinal.{max u v}) : (⨆ i, f i).card ≤ Cardinal.sum (fun i ↦ (f i).card) := by have := lift_card_iSup_le_sum_card f rwa [Cardinal.lift_id'] at this theorem card_iSup_Iio_le_sum_card {o : Ordinal.{u}} (f : Iio o → Ordinal.{max u v}) : (⨆ a : Iio o, f a).card ≤ Cardinal.sum fun i ↦ (f ((enumIsoToType o).symm i)).card := by apply le_of_eq_of_le (congr_arg _ _).symm (card_iSup_le_sum_card _) simpa using (enumIsoToType o).symm.iSup_comp (g := fun x ↦ f x) theorem card_iSup_Iio_le_card_mul_iSup {o : Ordinal.{u}} (f : Iio o → Ordinal.{max u v}) : (⨆ a : Iio o, f a).card ≤ Cardinal.lift.{v} o.card * ⨆ a : Iio o, (f a).card := by apply (card_iSup_Iio_le_sum_card f).trans convert ← sum_le_iSup_lift _ · exact mk_toType o · exact (enumIsoToType o).symm.iSup_comp (g := fun x ↦ (f x).card) theorem card_opow_le_of_omega0_le_left {a : Ordinal} (ha : ω ≤ a) (b : Ordinal) : (a ^ b).card ≤ max a.card b.card := by refine limitRecOn b ?_ ?_ ?_ · simpa using one_lt_omega0.le.trans ha · intro b IH rw [opow_succ, card_mul, card_succ, Cardinal.mul_eq_max_of_aleph0_le_right, max_comm] · apply (max_le_max_left _ IH).trans rw [← max_assoc, max_self] exact max_le_max_left _ le_self_add · rw [ne_eq, card_eq_zero, opow_eq_zero] rintro ⟨rfl, -⟩ cases omega0_pos.not_le ha · rwa [aleph0_le_card] · intro b hb IH rw [(isNormal_opow (one_lt_omega0.trans_le ha)).apply_of_isLimit hb] apply (card_iSup_Iio_le_card_mul_iSup _).trans rw [Cardinal.lift_id, Cardinal.mul_eq_max_of_aleph0_le_right, max_comm] · apply max_le _ (le_max_right _ _) apply ciSup_le' intro c exact (IH c.1 c.2).trans (max_le_max_left _ (card_le_card c.2.le)) · simpa using hb.pos.ne' · refine le_ciSup_of_le ?_ ⟨1, one_lt_omega0.trans_le <| omega0_le_of_isLimit hb⟩ ?_ · exact Cardinal.bddAbove_of_small _ · simpa theorem card_opow_le_of_omega0_le_right (a : Ordinal) {b : Ordinal} (hb : ω ≤ b) : (a ^ b).card ≤ max a.card b.card := by obtain ⟨n, rfl⟩ | ha := eq_nat_or_omega0_le a · apply (card_le_card <| opow_le_opow_left b (nat_lt_omega0 n).le).trans apply (card_opow_le_of_omega0_le_left le_rfl _).trans simp [hb] · exact card_opow_le_of_omega0_le_left ha b theorem card_opow_le (a b : Ordinal) : (a ^ b).card ≤ max ℵ₀ (max a.card b.card) := by obtain ⟨n, rfl⟩ | ha := eq_nat_or_omega0_le a · obtain ⟨m, rfl⟩ | hb := eq_nat_or_omega0_le b · rw [← natCast_opow, card_nat] exact le_max_of_le_left (nat_lt_aleph0 _).le · exact (card_opow_le_of_omega0_le_right _ hb).trans (le_max_right _ _) · exact (card_opow_le_of_omega0_le_left ha _).trans (le_max_right _ _) theorem card_opow_eq_of_omega0_le_left {a b : Ordinal} (ha : ω ≤ a) (hb : 0 < b) : (a ^ b).card = max a.card b.card := by apply (card_opow_le_of_omega0_le_left ha b).antisymm (max_le _ _) <;> apply card_le_card · exact left_le_opow a hb · exact right_le_opow b (one_lt_omega0.trans_le ha) theorem card_opow_eq_of_omega0_le_right {a b : Ordinal} (ha : 1 < a) (hb : ω ≤ b) : (a ^ b).card = max a.card b.card := by apply (card_opow_le_of_omega0_le_right a hb).antisymm (max_le _ _) <;> apply card_le_card · exact left_le_opow a (omega0_pos.trans_le hb) · exact right_le_opow b ha theorem card_omega0_opow {a : Ordinal} (h : a ≠ 0) : card (ω ^ a) = max ℵ₀ a.card := by rw [card_opow_eq_of_omega0_le_left le_rfl h.bot_lt, card_omega0] theorem card_opow_omega0 {a : Ordinal} (h : 1 < a) : card (a ^ ω) = max ℵ₀ a.card := by rw [card_opow_eq_of_omega0_le_right h le_rfl, card_omega0, max_comm] theorem principal_opow_omega (o : Ordinal) : Principal (· ^ ·) (ω_ o) := by obtain rfl | ho := Ordinal.eq_zero_or_pos o · rw [omega_zero] exact principal_opow_omega0 · intro a b ha hb rw [lt_omega_iff_card_lt] at ha hb ⊢ apply (card_opow_le a b).trans_lt (max_lt _ (max_lt ha hb)) rwa [← aleph_zero, aleph_lt_aleph] theorem IsInitial.principal_opow {o : Ordinal} (h : IsInitial o) (ho : ω ≤ o) : Principal (· ^ ·) o := by obtain ⟨a, rfl⟩ := mem_range_omega_iff.2 ⟨ho, h⟩ exact principal_opow_omega a theorem principal_opow_ord {c : Cardinal} (hc : ℵ₀ ≤ c) : Principal (· ^ ·) c.ord := by apply (isInitial_ord c).principal_opow rwa [omega0_le_ord] /-! ### Initial ordinals are principal -/ theorem principal_add_ord {c : Cardinal} (hc : ℵ₀ ≤ c) : Principal (· + ·) c.ord := by intro a b ha hb rw [lt_ord, card_add] at * exact add_lt_of_lt hc ha hb theorem IsInitial.principal_add {o : Ordinal} (h : IsInitial o) (ho : ω ≤ o) : Principal (· + ·) o := by rw [← h.ord_card] apply principal_add_ord rwa [aleph0_le_card] theorem principal_add_omega (o : Ordinal) : Principal (· + ·) (ω_ o) := (isInitial_omega o).principal_add (omega0_le_omega o) theorem principal_mul_ord {c : Cardinal} (hc : ℵ₀ ≤ c) : Principal (· * ·) c.ord := by intro a b ha hb rw [lt_ord, card_mul] at * exact mul_lt_of_lt hc ha hb theorem IsInitial.principal_mul {o : Ordinal} (h : IsInitial o) (ho : ω ≤ o) : Principal (· * ·) o := by rw [← h.ord_card] apply principal_mul_ord rwa [aleph0_le_card] theorem principal_mul_omega (o : Ordinal) : Principal (· * ·) (ω_ o) := (isInitial_omega o).principal_mul (omega0_le_omega o) @[deprecated principal_add_omega (since := "2024-11-08")] theorem _root_.Cardinal.principal_add_aleph (o : Ordinal) : Principal (· + ·) (ℵ_ o).ord := principal_add_ord <| aleph0_le_aleph o end Ordinal
Mathlib/SetTheory/Cardinal/Ordinal.lean
948
953
/- Copyright (c) 2020 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Shing Tak Lam, Mario Carneiro -/ import Mathlib.Algebra.BigOperators.Intervals import Mathlib.Algebra.BigOperators.Ring.List import Mathlib.Data.Int.ModEq import Mathlib.Data.Nat.Bits import Mathlib.Data.Nat.Log import Mathlib.Data.List.Palindrome import Mathlib.Tactic.IntervalCases import Mathlib.Tactic.Linarith import Mathlib.Tactic.Ring /-! # Digits of a natural number This provides a basic API for extracting the digits of a natural number in a given base, and reconstructing numbers from their digits. We also prove some divisibility tests based on digits, in particular completing Theorem #85 from https://www.cs.ru.nl/~freek/100/. Also included is a bound on the length of `Nat.toDigits` from core. ## TODO A basic `norm_digits` tactic for proving goals of the form `Nat.digits a b = l` where `a` and `b` are numerals is not yet ported. -/ namespace Nat variable {n : ℕ} /-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/ def digitsAux0 : ℕ → List ℕ | 0 => [] | n + 1 => [n + 1] /-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/ def digitsAux1 (n : ℕ) : List ℕ := List.replicate n 1 /-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/ def digitsAux (b : ℕ) (h : 2 ≤ b) : ℕ → List ℕ | 0 => [] | n + 1 => ((n + 1) % b) :: digitsAux b h ((n + 1) / b) decreasing_by exact Nat.div_lt_self (Nat.succ_pos _) h @[simp] theorem digitsAux_zero (b : ℕ) (h : 2 ≤ b) : digitsAux b h 0 = [] := by rw [digitsAux] theorem digitsAux_def (b : ℕ) (h : 2 ≤ b) (n : ℕ) (w : 0 < n) : digitsAux b h n = (n % b) :: digitsAux b h (n / b) := by cases n · cases w · rw [digitsAux] /-- `digits b n` gives the digits, in little-endian order, of a natural number `n` in a specified base `b`. In any base, we have `ofDigits b L = L.foldr (fun x y ↦ x + b * y) 0`. * For any `2 ≤ b`, we have `l < b` for any `l ∈ digits b n`, and the last digit is not zero. This uniquely specifies the behaviour of `digits b`. * For `b = 1`, we define `digits 1 n = List.replicate n 1`. * For `b = 0`, we define `digits 0 n = [n]`, except `digits 0 0 = []`. Note this differs from the existing `Nat.toDigits` in core, which is used for printing numerals. In particular, `Nat.toDigits b 0 = ['0']`, while `digits b 0 = []`. -/ def digits : ℕ → ℕ → List ℕ | 0 => digitsAux0 | 1 => digitsAux1 | b + 2 => digitsAux (b + 2) (by norm_num) @[simp] theorem digits_zero (b : ℕ) : digits b 0 = [] := by rcases b with (_ | ⟨_ | ⟨_⟩⟩) <;> simp [digits, digitsAux0, digitsAux1] theorem digits_zero_zero : digits 0 0 = [] := rfl @[simp] theorem digits_zero_succ (n : ℕ) : digits 0 n.succ = [n + 1] := rfl
theorem digits_zero_succ' : ∀ {n : ℕ}, n ≠ 0 → digits 0 n = [n]
Mathlib/Data/Nat/Digits.lean
90
91
/- 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`. -/
Mathlib/LinearAlgebra/Matrix/ToLin.lean
497
499
/- 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.Polynomial.Module.AEval /-! # Polynomial module In this file, we define the polynomial module for an `R`-module `M`, i.e. the `R[X]`-module `M[X]`. This is defined as a type alias `PolynomialModule R M := ℕ →₀ M`, since there might be different module structures on `ℕ →₀ M` of interest. See the docstring of `PolynomialModule` for details. -/ universe u v open Polynomial /-- The `R[X]`-module `M[X]` for an `R`-module `M`. This is isomorphic (as an `R`-module) to `M[X]` when `M` is a ring. We require all the module instances `Module S (PolynomialModule R M)` to factor through `R` except `Module R[X] (PolynomialModule R M)`. In this constraint, we have the following instances for example : - `R` acts on `PolynomialModule R R[X]` - `R[X]` acts on `PolynomialModule R R[X]` as `R[Y]` acting on `R[X][Y]` - `R` acts on `PolynomialModule R[X] R[X]` - `R[X]` acts on `PolynomialModule R[X] R[X]` as `R[X]` acting on `R[X][Y]` - `R[X][X]` acts on `PolynomialModule R[X] R[X]` as `R[X][Y]` acting on itself This is also the reason why `R` is included in the alias, or else there will be two different instances of `Module R[X] (PolynomialModule R[X])`. See https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.2315065.20polynomial.20modules for the full discussion. -/ @[nolint unusedArguments] def PolynomialModule (R M : Type*) [CommRing R] [AddCommGroup M] [Module R M] := ℕ →₀ M variable (R M : Type*) [CommRing R] [AddCommGroup M] [Module R M] (I : Ideal R) -- The `Inhabited, AddCommGroup` instances should be constructed by a deriving handler. -- https://github.com/leanprover-community/mathlib4/issues/380 noncomputable instance : Inhabited (PolynomialModule R M) := Finsupp.instInhabited noncomputable instance : AddCommGroup (PolynomialModule R M) := Finsupp.instAddCommGroup variable {M} variable {S : Type*} [CommSemiring S] [Algebra S R] [Module S M] [IsScalarTower S R M] namespace PolynomialModule /-- This is required to have the `IsScalarTower S R M` instance to avoid diamonds. -/ @[nolint unusedArguments] noncomputable instance : Module S (PolynomialModule R M) := Finsupp.module ℕ M instance instFunLike : FunLike (PolynomialModule R M) ℕ M := Finsupp.instFunLike instance : CoeFun (PolynomialModule R M) fun _ => ℕ → M := inferInstanceAs <| CoeFun (_ →₀ _) _ theorem zero_apply (i : ℕ) : (0 : PolynomialModule R M) i = 0 := Finsupp.zero_apply theorem add_apply (g₁ g₂ : PolynomialModule R M) (a : ℕ) : (g₁ + g₂) a = g₁ a + g₂ a := Finsupp.add_apply g₁ g₂ a /-- The monomial `m * x ^ i`. This is defeq to `Finsupp.singleAddHom`, and is redefined here so that it has the desired type signature. -/ noncomputable def single (i : ℕ) : M →+ PolynomialModule R M := Finsupp.singleAddHom i theorem single_apply (i : ℕ) (m : M) (n : ℕ) : single R i m n = ite (i = n) m 0 := Finsupp.single_apply /-- `PolynomialModule.single` as a linear map. -/ noncomputable def lsingle (i : ℕ) : M →ₗ[R] PolynomialModule R M := Finsupp.lsingle i theorem lsingle_apply (i : ℕ) (m : M) (n : ℕ) : lsingle R i m n = ite (i = n) m 0 := Finsupp.single_apply theorem single_smul (i : ℕ) (r : R) (m : M) : single R i (r • m) = r • single R i m := (lsingle R i).map_smul r m variable {R} @[elab_as_elim] theorem induction_linear {motive : PolynomialModule R M → Prop} (f : PolynomialModule R M) (zero : motive 0) (add : ∀ f g, motive f → motive g → motive (f + g)) (single : ∀ a b, motive (single R a b)) : motive f := Finsupp.induction_linear f zero add single noncomputable instance polynomialModule : Module R[X] (PolynomialModule R M) := inferInstanceAs (Module R[X] (Module.AEval' (Finsupp.lmapDomain M R Nat.succ))) lemma smul_def (f : R[X]) (m : PolynomialModule R M) : f • m = aeval (Finsupp.lmapDomain M R Nat.succ) f m := by rfl instance (M : Type u) [AddCommGroup M] [Module R M] [Module S M] [IsScalarTower S R M] : IsScalarTower S R (PolynomialModule R M) := Finsupp.isScalarTower _ _ instance isScalarTower' (M : Type u) [AddCommGroup M] [Module R M] [Module S M] [IsScalarTower S R M] : IsScalarTower S R[X] (PolynomialModule R M) := by haveI : IsScalarTower R R[X] (PolynomialModule R M) := inferInstanceAs <| IsScalarTower R R[X] <| Module.AEval' <| Finsupp.lmapDomain M R Nat.succ constructor intro x y z rw [← @IsScalarTower.algebraMap_smul S R, ← @IsScalarTower.algebraMap_smul S R, smul_assoc] @[simp] theorem monomial_smul_single (i : ℕ) (r : R) (j : ℕ) (m : M) : monomial i r • single R j m = single R (i + j) (r • m) := by simp only [Module.End.mul_apply, Polynomial.aeval_monomial, Module.End.pow_apply, Module.algebraMap_end_apply, smul_def] induction i generalizing r j m with | zero => rw [Function.iterate_zero, zero_add] exact Finsupp.smul_single r j m | succ n hn => rw [Function.iterate_succ, Function.comp_apply, add_assoc, ← hn] congr 2 rw [Nat.one_add] exact Finsupp.mapDomain_single @[simp] theorem monomial_smul_apply (i : ℕ) (r : R) (g : PolynomialModule R M) (n : ℕ) : (monomial i r • g) n = ite (i ≤ n) (r • g (n - i)) 0 := by induction' g using PolynomialModule.induction_linear with p q hp hq · simp only [smul_zero, zero_apply, ite_self] · simp only [smul_add, add_apply, hp, hq] split_ifs exacts [rfl, zero_add 0] · rw [monomial_smul_single, single_apply, single_apply, smul_ite, smul_zero, ← ite_and] congr rw [eq_iff_iff] constructor · rintro rfl simp · rintro ⟨e, rfl⟩ rw [add_comm, tsub_add_cancel_of_le e] @[simp] theorem smul_single_apply (i : ℕ) (f : R[X]) (m : M) (n : ℕ) : (f • single R i m) n = ite (i ≤ n) (f.coeff (n - i) • m) 0 := by induction' f using Polynomial.induction_on' with p q hp hq · rw [add_smul, Finsupp.add_apply, hp, hq, coeff_add, add_smul] split_ifs exacts [rfl, zero_add 0] · rw [monomial_smul_single, single_apply, coeff_monomial, ite_smul, zero_smul] by_cases h : i ≤ n · simp_rw [eq_tsub_iff_add_eq_of_le h, if_pos h] · rw [if_neg h, if_neg]
omega theorem smul_apply (f : R[X]) (g : PolynomialModule R M) (n : ℕ) : (f • g) n = ∑ x ∈ Finset.antidiagonal n, f.coeff x.1 • g x.2 := by induction f using Polynomial.induction_on' with | add p q hp hq => rw [add_smul, Finsupp.add_apply, hp, hq, ← Finset.sum_add_distrib] congr ext rw [coeff_add, add_smul] | monomial f_n f_a => rw [Finset.Nat.sum_antidiagonal_eq_sum_range_succ fun i j => (monomial f_n f_a).coeff i • g j, monomial_smul_apply]
Mathlib/Algebra/Polynomial/Module/Basic.lean
157
169
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import Mathlib.Control.Basic import Mathlib.Data.Nat.Basic import Mathlib.Data.Option.Basic import Mathlib.Data.List.Defs import Mathlib.Data.List.Monad import Mathlib.Logic.OpClass import Mathlib.Logic.Unique import Mathlib.Order.Basic import Mathlib.Tactic.Common /-! # Basic properties of lists -/ assert_not_exists GroupWithZero assert_not_exists Lattice assert_not_exists Prod.swap_eq_iff_eq_swap assert_not_exists Ring assert_not_exists Set.range open Function open Nat hiding one_pos namespace List universe u v w variable {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {l₁ l₂ : List α} /-- There is only one list of an empty type -/ instance uniqueOfIsEmpty [IsEmpty α] : Unique (List α) := { instInhabitedList with uniq := fun l => match l with | [] => rfl | a :: _ => isEmptyElim a } instance : Std.LawfulIdentity (α := List α) Append.append [] where left_id := nil_append right_id := append_nil instance : Std.Associative (α := List α) Append.append where assoc := append_assoc @[simp] theorem cons_injective {a : α} : Injective (cons a) := fun _ _ => tail_eq_of_cons_eq theorem singleton_injective : Injective fun a : α => [a] := fun _ _ h => (cons_eq_cons.1 h).1 theorem set_of_mem_cons (l : List α) (a : α) : { x | x ∈ a :: l } = insert a { x | x ∈ l } := Set.ext fun _ => mem_cons /-! ### mem -/ theorem _root_.Decidable.List.eq_or_ne_mem_of_mem [DecidableEq α] {a b : α} {l : List α} (h : a ∈ b :: l) : a = b ∨ a ≠ b ∧ a ∈ l := by by_cases hab : a = b · exact Or.inl hab · exact ((List.mem_cons.1 h).elim Or.inl (fun h => Or.inr ⟨hab, h⟩)) lemma mem_pair {a b c : α} : a ∈ [b, c] ↔ a = b ∨ a = c := by rw [mem_cons, mem_singleton] -- The simpNF linter says that the LHS can be simplified via `List.mem_map`. -- However this is a higher priority lemma. -- It seems the side condition `hf` is not applied by `simpNF`. -- https://github.com/leanprover/std4/issues/207 @[simp 1100, nolint simpNF] theorem mem_map_of_injective {f : α → β} (H : Injective f) {a : α} {l : List α} : f a ∈ map f l ↔ a ∈ l := ⟨fun m => let ⟨_, m', e⟩ := exists_of_mem_map m; H e ▸ m', mem_map_of_mem⟩ @[simp] theorem _root_.Function.Involutive.exists_mem_and_apply_eq_iff {f : α → α} (hf : Function.Involutive f) (x : α) (l : List α) : (∃ y : α, y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by rintro ⟨y, h, rfl⟩; rwa [hf y], fun h => ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : α → α} (hf : Involutive f) {a : α} {l : List α} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] /-! ### length -/ alias ⟨_, length_pos_of_ne_nil⟩ := length_pos_iff theorem length_pos_iff_ne_nil {l : List α} : 0 < length l ↔ l ≠ [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ theorem exists_of_length_succ {n} : ∀ l : List α, l.length = n + 1 → ∃ h t, l = h :: t | [], H => absurd H.symm <| succ_ne_zero n | h :: t, _ => ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : Injective (List.length : List α → ℕ) ↔ Subsingleton α := by constructor · intro h; refine ⟨fun x y => ?_⟩; (suffices [x] = [y] by simpa using this); apply h; rfl · intros hα l1 l2 hl induction l1 generalizing l2 <;> cases l2 · rfl · cases hl · cases hl · next ih _ _ => congr · subsingleton · apply ih; simpa using hl @[simp default+1] -- Raise priority above `length_injective_iff`. lemma length_injective [Subsingleton α] : Injective (length : List α → ℕ) := length_injective_iff.mpr inferInstance theorem length_eq_two {l : List α} : l.length = 2 ↔ ∃ a b, l = [a, b] := ⟨fun _ => let [a, b] := l; ⟨a, b, rfl⟩, fun ⟨_, _, e⟩ => e ▸ rfl⟩ theorem length_eq_three {l : List α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] := ⟨fun _ => let [a, b, c] := l; ⟨a, b, c, rfl⟩, fun ⟨_, _, _, e⟩ => e ▸ rfl⟩ /-! ### set-theoretic notation of lists -/ instance instSingletonList : Singleton α (List α) := ⟨fun x => [x]⟩ instance [DecidableEq α] : Insert α (List α) := ⟨List.insert⟩ instance [DecidableEq α] : LawfulSingleton α (List α) := { insert_empty_eq := fun x => show (if x ∈ ([] : List α) then [] else [x]) = [x] from if_neg not_mem_nil } theorem singleton_eq (x : α) : ({x} : List α) = [x] := rfl theorem insert_neg [DecidableEq α] {x : α} {l : List α} (h : x ∉ l) : Insert.insert x l = x :: l := insert_of_not_mem h theorem insert_pos [DecidableEq α] {x : α} {l : List α} (h : x ∈ l) : Insert.insert x l = l := insert_of_mem h theorem doubleton_eq [DecidableEq α] {x y : α} (h : x ≠ y) : ({x, y} : List α) = [x, y] := by rw [insert_neg, singleton_eq] rwa [singleton_eq, mem_singleton] /-! ### bounded quantifiers over lists -/ theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : List α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : List α) (h : p a) : ∃ x ∈ a :: l, p x := ⟨a, mem_cons_self, h⟩ theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ l, p x) → ∃ x ∈ a :: l, p x := fun ⟨x, xl, px⟩ => ⟨x, mem_cons_of_mem _ xl, px⟩ theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : List α} : (∃ x ∈ a :: l, p x) → p a ∨ ∃ x ∈ l, p x := fun ⟨x, xal, px⟩ => Or.elim (eq_or_mem_of_mem_cons xal) (fun h : x = a => by rw [← h]; left; exact px) fun h : x ∈ l => Or.inr ⟨x, h, px⟩ theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : List α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := Iff.intro or_exists_of_exists_mem_cons fun h => Or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists /-! ### list subset -/ theorem cons_subset_of_subset_of_mem {a : α} {l m : List α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ l₂ l : List α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := fun _ h ↦ (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem map_subset_iff {l₁ l₂ : List α} (f : α → β) (h : Injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := by refine ⟨?_, map_subset f⟩; intro h2 x hx rcases mem_map.1 (h2 (mem_map_of_mem hx)) with ⟨x', hx', hxx'⟩ cases h hxx'; exact hx' /-! ### append -/ theorem append_eq_has_append {L₁ L₂ : List α} : List.append L₁ L₂ = L₁ ++ L₂ := rfl theorem append_right_injective (s : List α) : Injective fun t ↦ s ++ t := fun _ _ ↦ append_cancel_left theorem append_left_injective (t : List α) : Injective fun s ↦ s ++ t := fun _ _ ↦ append_cancel_right /-! ### replicate -/ theorem eq_replicate_length {a : α} : ∀ {l : List α}, l = replicate l.length a ↔ ∀ b ∈ l, b = a | [] => by simp | (b :: l) => by simp [eq_replicate_length, replicate_succ] theorem replicate_add (m n) (a : α) : replicate (m + n) a = replicate m a ++ replicate n a := by rw [replicate_append_replicate] theorem replicate_subset_singleton (n) (a : α) : replicate n a ⊆ [a] := fun _ h => mem_singleton.2 (eq_of_mem_replicate h) theorem subset_singleton_iff {a : α} {L : List α} : L ⊆ [a] ↔ ∃ n, L = replicate n a := by simp only [eq_replicate_iff, subset_def, mem_singleton, exists_eq_left'] theorem replicate_right_injective {n : ℕ} (hn : n ≠ 0) : Injective (@replicate α n) := fun _ _ h => (eq_replicate_iff.1 h).2 _ <| mem_replicate.2 ⟨hn, rfl⟩ theorem replicate_right_inj {a b : α} {n : ℕ} (hn : n ≠ 0) : replicate n a = replicate n b ↔ a = b := (replicate_right_injective hn).eq_iff theorem replicate_right_inj' {a b : α} : ∀ {n}, replicate n a = replicate n b ↔ n = 0 ∨ a = b | 0 => by simp | n + 1 => (replicate_right_inj n.succ_ne_zero).trans <| by simp only [n.succ_ne_zero, false_or] theorem replicate_left_injective (a : α) : Injective (replicate · a) := LeftInverse.injective (length_replicate (n := ·)) theorem replicate_left_inj {a : α} {n m : ℕ} : replicate n a = replicate m a ↔ n = m := (replicate_left_injective a).eq_iff @[simp] theorem head?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.head? = l.head? := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h induction l <;> simp [replicate] @[simp] theorem getLast?_flatten_replicate {n : ℕ} (h : n ≠ 0) (l : List α) : (List.replicate n l).flatten.getLast? = l.getLast? := by rw [← List.head?_reverse, ← List.head?_reverse, List.reverse_flatten, List.map_replicate, List.reverse_replicate, head?_flatten_replicate h] /-! ### pure -/ theorem mem_pure (x y : α) : x ∈ (pure y : List α) ↔ x = y := by simp /-! ### bind -/ @[simp] theorem bind_eq_flatMap {α β} (f : α → List β) (l : List α) : l >>= f = l.flatMap f := rfl /-! ### concat -/ /-! ### reverse -/ theorem reverse_cons' (a : α) (l : List α) : reverse (a :: l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] theorem reverse_concat' (l : List α) (a : α) : (l ++ [a]).reverse = a :: l.reverse := by rw [reverse_append]; rfl @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_involutive : Involutive (@reverse α) := reverse_reverse @[simp] theorem reverse_injective : Injective (@reverse α) := reverse_involutive.injective theorem reverse_surjective : Surjective (@reverse α) := reverse_involutive.surjective theorem reverse_bijective : Bijective (@reverse α) := reverse_involutive.bijective theorem concat_eq_reverse_cons (a : α) (l : List α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] theorem map_reverseAux (f : α → β) (l₁ l₂ : List α) : map f (reverseAux l₁ l₂) = reverseAux (map f l₁) (map f l₂) := by simp only [reverseAux_eq, map_append, map_reverse] -- TODO: Rename `List.reverse_perm` to `List.reverse_perm_self` @[simp] lemma reverse_perm' : l₁.reverse ~ l₂ ↔ l₁ ~ l₂ where mp := l₁.reverse_perm.symm.trans mpr := l₁.reverse_perm.trans @[simp] lemma perm_reverse : l₁ ~ l₂.reverse ↔ l₁ ~ l₂ where mp hl := hl.trans l₂.reverse_perm mpr hl := hl.trans l₂.reverse_perm.symm /-! ### getLast -/ attribute [simp] getLast_cons theorem getLast_append_singleton {a : α} (l : List α) : getLast (l ++ [a]) (append_ne_nil_of_right_ne_nil l (cons_ne_nil a _)) = a := by simp [getLast_append] theorem getLast_append_of_right_ne_nil (l₁ l₂ : List α) (h : l₂ ≠ []) : getLast (l₁ ++ l₂) (append_ne_nil_of_right_ne_nil l₁ h) = getLast l₂ h := by induction l₁ with | nil => simp | cons _ _ ih => simp only [cons_append]; rw [List.getLast_cons]; exact ih @[deprecated (since := "2025-02-06")] alias getLast_append' := getLast_append_of_right_ne_nil theorem getLast_concat' {a : α} (l : List α) : getLast (concat l a) (by simp) = a := by simp @[simp] theorem getLast_singleton' (a : α) : getLast [a] (cons_ne_nil a []) = a := rfl @[simp] theorem getLast_cons_cons (a₁ a₂ : α) (l : List α) : getLast (a₁ :: a₂ :: l) (cons_ne_nil _ _) = getLast (a₂ :: l) (cons_ne_nil a₂ l) := rfl theorem dropLast_append_getLast : ∀ {l : List α} (h : l ≠ []), dropLast l ++ [getLast l h] = l | [], h => absurd rfl h | [_], _ => rfl | a :: b :: l, h => by rw [dropLast_cons₂, cons_append, getLast_cons (cons_ne_nil _ _)] congr exact dropLast_append_getLast (cons_ne_nil b l) theorem getLast_congr {l₁ l₂ : List α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : getLast l₁ h₁ = getLast l₂ h₂ := by subst l₁; rfl theorem getLast_replicate_succ (m : ℕ) (a : α) : (replicate (m + 1) a).getLast (ne_nil_of_length_eq_add_one length_replicate) = a := by simp only [replicate_succ'] exact getLast_append_singleton _ @[deprecated (since := "2025-02-07")] alias getLast_filter' := getLast_filter_of_pos /-! ### getLast? -/ theorem mem_getLast?_eq_getLast : ∀ {l : List α} {x : α}, x ∈ l.getLast? → ∃ h, x = getLast l h | [], x, hx => False.elim <| by simp at hx | [a], x, hx => have : a = x := by simpa using hx this ▸ ⟨cons_ne_nil a [], rfl⟩ | a :: b :: l, x, hx => by rw [getLast?_cons_cons] at hx rcases mem_getLast?_eq_getLast hx with ⟨_, h₂⟩ use cons_ne_nil _ _ assumption theorem getLast?_eq_getLast_of_ne_nil : ∀ {l : List α} (h : l ≠ []), l.getLast? = some (l.getLast h) | [], h => (h rfl).elim | [_], _ => rfl | _ :: b :: l, _ => @getLast?_eq_getLast_of_ne_nil (b :: l) (cons_ne_nil _ _) theorem mem_getLast?_cons {x y : α} : ∀ {l : List α}, x ∈ l.getLast? → x ∈ (y :: l).getLast? | [], _ => by contradiction | _ :: _, h => h theorem dropLast_append_getLast? : ∀ {l : List α}, ∀ a ∈ l.getLast?, dropLast l ++ [a] = l | [], a, ha => (Option.not_mem_none a ha).elim | [a], _, rfl => rfl | a :: b :: l, c, hc => by rw [getLast?_cons_cons] at hc rw [dropLast_cons₂, cons_append, dropLast_append_getLast? _ hc] theorem getLastI_eq_getLast? [Inhabited α] : ∀ l : List α, l.getLastI = l.getLast?.iget | [] => by simp [getLastI, Inhabited.default] | [_] => rfl | [_, _] => rfl | [_, _, _] => rfl | _ :: _ :: c :: l => by simp [getLastI, getLastI_eq_getLast? (c :: l)] theorem getLast?_append_cons : ∀ (l₁ : List α) (a : α) (l₂ : List α), getLast? (l₁ ++ a :: l₂) = getLast? (a :: l₂) | [], _, _ => rfl | [_], _, _ => rfl | b :: c :: l₁, a, l₂ => by rw [cons_append, cons_append, getLast?_cons_cons, ← cons_append, getLast?_append_cons (c :: l₁)] theorem getLast?_append_of_ne_nil (l₁ : List α) : ∀ {l₂ : List α} (_ : l₂ ≠ []), getLast? (l₁ ++ l₂) = getLast? l₂ | [], hl₂ => by contradiction | b :: l₂, _ => getLast?_append_cons l₁ b l₂ theorem mem_getLast?_append_of_mem_getLast? {l₁ l₂ : List α} {x : α} (h : x ∈ l₂.getLast?) : x ∈ (l₁ ++ l₂).getLast? := by cases l₂ · contradiction · rw [List.getLast?_append_cons] exact h /-! ### head(!?) and tail -/ @[simp] theorem head!_nil [Inhabited α] : ([] : List α).head! = default := rfl @[simp] theorem head_cons_tail (x : List α) (h : x ≠ []) : x.head h :: x.tail = x := by cases x <;> simp at h ⊢ theorem head_eq_getElem_zero {l : List α} (hl : l ≠ []) : l.head hl = l[0]'(length_pos_iff.2 hl) := (getElem_zero _).symm theorem head!_eq_head? [Inhabited α] (l : List α) : head! l = (head? l).iget := by cases l <;> rfl theorem surjective_head! [Inhabited α] : Surjective (@head! α _) := fun x => ⟨[x], rfl⟩ theorem surjective_head? : Surjective (@head? α) := Option.forall.2 ⟨⟨[], rfl⟩, fun x => ⟨[x], rfl⟩⟩ theorem surjective_tail : Surjective (@tail α) | [] => ⟨[], rfl⟩ | a :: l => ⟨a :: a :: l, rfl⟩ theorem eq_cons_of_mem_head? {x : α} : ∀ {l : List α}, x ∈ l.head? → l = x :: tail l | [], h => (Option.not_mem_none _ h).elim | a :: l, h => by simp only [head?, Option.mem_def, Option.some_inj] at h exact h ▸ rfl @[simp] theorem head!_cons [Inhabited α] (a : α) (l : List α) : head! (a :: l) = a := rfl @[simp] theorem head!_append [Inhabited α] (t : List α) {s : List α} (h : s ≠ []) : head! (s ++ t) = head! s := by induction s · contradiction · rfl theorem mem_head?_append_of_mem_head? {s t : List α} {x : α} (h : x ∈ s.head?) : x ∈ (s ++ t).head? := by cases s · contradiction · exact h theorem head?_append_of_ne_nil : ∀ (l₁ : List α) {l₂ : List α} (_ : l₁ ≠ []), head? (l₁ ++ l₂) = head? l₁ | _ :: _, _, _ => rfl theorem tail_append_singleton_of_ne_nil {a : α} {l : List α} (h : l ≠ nil) : tail (l ++ [a]) = tail l ++ [a] := by induction l · contradiction · rw [tail, cons_append, tail] theorem cons_head?_tail : ∀ {l : List α} {a : α}, a ∈ head? l → a :: tail l = l | [], a, h => by contradiction | b :: l, a, h => by simp? at h says simp only [head?_cons, Option.mem_def, Option.some.injEq] at h simp [h] theorem head!_mem_head? [Inhabited α] : ∀ {l : List α}, l ≠ [] → head! l ∈ head? l | [], h => by contradiction | _ :: _, _ => rfl theorem cons_head!_tail [Inhabited α] {l : List α} (h : l ≠ []) : head! l :: tail l = l := cons_head?_tail (head!_mem_head? h) theorem head!_mem_self [Inhabited α] {l : List α} (h : l ≠ nil) : l.head! ∈ l := by have h' : l.head! ∈ l.head! :: l.tail := mem_cons_self rwa [cons_head!_tail h] at h' theorem get_eq_getElem? (l : List α) (i : Fin l.length) : l.get i = l[i]?.get (by simp [getElem?_eq_getElem]) := by simp @[deprecated (since := "2025-02-15")] alias get_eq_get? := get_eq_getElem? theorem exists_mem_iff_getElem {l : List α} {p : α → Prop} : (∃ x ∈ l, p x) ↔ ∃ (i : ℕ) (_ : i < l.length), p l[i] := by simp only [mem_iff_getElem] exact ⟨fun ⟨_x, ⟨i, hi, hix⟩, hxp⟩ ↦ ⟨i, hi, hix ▸ hxp⟩, fun ⟨i, hi, hp⟩ ↦ ⟨_, ⟨i, hi, rfl⟩, hp⟩⟩ theorem forall_mem_iff_getElem {l : List α} {p : α → Prop} : (∀ x ∈ l, p x) ↔ ∀ (i : ℕ) (_ : i < l.length), p l[i] := by simp [mem_iff_getElem, @forall_swap α] theorem get_tail (l : List α) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := (by simp only [length_tail] at h; omega)) : l.tail.get ⟨i, h⟩ = l.get ⟨i + 1, h'⟩ := by cases l <;> [cases h; rfl] /-! ### sublists -/ attribute [refl] List.Sublist.refl theorem Sublist.cons_cons {l₁ l₂ : List α} (a : α) (s : l₁ <+ l₂) : a :: l₁ <+ a :: l₂ := Sublist.cons₂ _ s lemma cons_sublist_cons' {a b : α} : a :: l₁ <+ b :: l₂ ↔ a :: l₁ <+ l₂ ∨ a = b ∧ l₁ <+ l₂ := by constructor · rintro (_ | _) · exact Or.inl ‹_› · exact Or.inr ⟨rfl, ‹_›⟩ · rintro (h | ⟨rfl, h⟩) · exact h.cons _ · rwa [cons_sublist_cons] theorem sublist_cons_of_sublist (a : α) (h : l₁ <+ l₂) : l₁ <+ a :: l₂ := h.cons _ @[deprecated (since := "2025-02-07")] alias sublist_nil_iff_eq_nil := sublist_nil @[simp] lemma sublist_singleton {l : List α} {a : α} : l <+ [a] ↔ l = [] ∨ l = [a] := by constructor <;> rintro (_ | _) <;> aesop theorem Sublist.antisymm (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := s₁.eq_of_length_le s₂.length_le /-- If the first element of two lists are different, then a sublist relation can be reduced. -/ theorem Sublist.of_cons_of_ne {a b} (h₁ : a ≠ b) (h₂ : a :: l₁ <+ b :: l₂) : a :: l₁ <+ l₂ := match h₁, h₂ with | _, .cons _ h => h /-! ### indexOf -/ section IndexOf variable [DecidableEq α] theorem idxOf_cons_eq {a b : α} (l : List α) : b = a → idxOf a (b :: l) = 0 | e => by rw [← e]; exact idxOf_cons_self @[deprecated (since := "2025-01-30")] alias indexOf_cons_eq := idxOf_cons_eq @[simp] theorem idxOf_cons_ne {a b : α} (l : List α) : b ≠ a → idxOf a (b :: l) = succ (idxOf a l) | h => by simp only [idxOf_cons, Bool.cond_eq_ite, beq_iff_eq, if_neg h] @[deprecated (since := "2025-01-30")] alias indexOf_cons_ne := idxOf_cons_ne theorem idxOf_eq_length_iff {a : α} {l : List α} : idxOf a l = length l ↔ a ∉ l := by induction l with | nil => exact iff_of_true rfl not_mem_nil | cons b l ih => simp only [length, mem_cons, idxOf_cons, eq_comm] rw [cond_eq_if] split_ifs with h <;> simp at h · exact iff_of_false (by rintro ⟨⟩) fun H => H <| Or.inl h.symm · simp only [Ne.symm h, false_or] rw [← ih] exact succ_inj @[simp] theorem idxOf_of_not_mem {l : List α} {a : α} : a ∉ l → idxOf a l = length l := idxOf_eq_length_iff.2 @[deprecated (since := "2025-01-30")] alias indexOf_of_not_mem := idxOf_of_not_mem theorem idxOf_le_length {a : α} {l : List α} : idxOf a l ≤ length l := by induction l with | nil => rfl | cons b l ih => ?_ simp only [length, idxOf_cons, cond_eq_if, beq_iff_eq] by_cases h : b = a · rw [if_pos h]; exact Nat.zero_le _ · rw [if_neg h]; exact succ_le_succ ih @[deprecated (since := "2025-01-30")] alias indexOf_le_length := idxOf_le_length theorem idxOf_lt_length_iff {a} {l : List α} : idxOf a l < length l ↔ a ∈ l := ⟨fun h => Decidable.byContradiction fun al => Nat.ne_of_lt h <| idxOf_eq_length_iff.2 al, fun al => (lt_of_le_of_ne idxOf_le_length) fun h => idxOf_eq_length_iff.1 h al⟩ @[deprecated (since := "2025-01-30")] alias indexOf_lt_length_iff := idxOf_lt_length_iff theorem idxOf_append_of_mem {a : α} (h : a ∈ l₁) : idxOf a (l₁ ++ l₂) = idxOf a l₁ := by induction l₁ with | nil => exfalso exact not_mem_nil h | cons d₁ t₁ ih => rw [List.cons_append] by_cases hh : d₁ = a · iterate 2 rw [idxOf_cons_eq _ hh] rw [idxOf_cons_ne _ hh, idxOf_cons_ne _ hh, ih (mem_of_ne_of_mem (Ne.symm hh) h)] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_mem := idxOf_append_of_mem theorem idxOf_append_of_not_mem {a : α} (h : a ∉ l₁) : idxOf a (l₁ ++ l₂) = l₁.length + idxOf a l₂ := by induction l₁ with | nil => rw [List.nil_append, List.length, Nat.zero_add] | cons d₁ t₁ ih => rw [List.cons_append, idxOf_cons_ne _ (ne_of_not_mem_cons h).symm, List.length, ih (not_mem_of_not_mem_cons h), Nat.succ_add] @[deprecated (since := "2025-01-30")] alias indexOf_append_of_not_mem := idxOf_append_of_not_mem end IndexOf /-! ### nth element -/ section deprecated @[simp] theorem getElem?_length (l : List α) : l[l.length]? = none := getElem?_eq_none le_rfl /-- A version of `getElem_map` that can be used for rewriting. -/ theorem getElem_map_rev (f : α → β) {l} {n : Nat} {h : n < l.length} : f l[n] = (map f l)[n]'((l.length_map f).symm ▸ h) := Eq.symm (getElem_map _) theorem get_length_sub_one {l : List α} (h : l.length - 1 < l.length) : l.get ⟨l.length - 1, h⟩ = l.getLast (by rintro rfl; exact Nat.lt_irrefl 0 h) := (getLast_eq_getElem _).symm theorem take_one_drop_eq_of_lt_length {l : List α} {n : ℕ} (h : n < l.length) : (l.drop n).take 1 = [l.get ⟨n, h⟩] := by rw [drop_eq_getElem_cons h, take, take] simp theorem ext_getElem?' {l₁ l₂ : List α} (h' : ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]?) : l₁ = l₂ := by apply ext_getElem? intro n rcases Nat.lt_or_ge n <| max l₁.length l₂.length with hn | hn · exact h' n hn · simp_all [Nat.max_le, getElem?_eq_none] @[deprecated (since := "2025-02-15")] alias ext_get?' := ext_getElem?' @[deprecated (since := "2025-02-15")] alias ext_get?_iff := List.ext_getElem?_iff theorem ext_get_iff {l₁ l₂ : List α} : l₁ = l₂ ↔ l₁.length = l₂.length ∧ ∀ n h₁ h₂, get l₁ ⟨n, h₁⟩ = get l₂ ⟨n, h₂⟩ := by constructor · rintro rfl exact ⟨rfl, fun _ _ _ ↦ rfl⟩ · intro ⟨h₁, h₂⟩ exact ext_get h₁ h₂ theorem ext_getElem?_iff' {l₁ l₂ : List α} : l₁ = l₂ ↔ ∀ n < max l₁.length l₂.length, l₁[n]? = l₂[n]? := ⟨by rintro rfl _ _; rfl, ext_getElem?'⟩ @[deprecated (since := "2025-02-15")] alias ext_get?_iff' := ext_getElem?_iff' /-- If two lists `l₁` and `l₂` are the same length and `l₁[n]! = l₂[n]!` for all `n`, then the lists are equal. -/ theorem ext_getElem! [Inhabited α] (hl : length l₁ = length l₂) (h : ∀ n : ℕ, l₁[n]! = l₂[n]!) : l₁ = l₂ := ext_getElem hl fun n h₁ h₂ ↦ by simpa only [← getElem!_pos] using h n @[simp] theorem getElem_idxOf [DecidableEq α] {a : α} : ∀ {l : List α} (h : idxOf a l < l.length), l[idxOf a l] = a | b :: l, h => by by_cases h' : b = a <;> simp [h', if_pos, if_false, getElem_idxOf] @[deprecated (since := "2025-01-30")] alias getElem_indexOf := getElem_idxOf -- This is incorrectly named and should be `get_idxOf`; -- this already exists, so will require a deprecation dance. theorem idxOf_get [DecidableEq α] {a : α} {l : List α} (h) : get l ⟨idxOf a l, h⟩ = a := by simp @[deprecated (since := "2025-01-30")] alias indexOf_get := idxOf_get @[simp] theorem getElem?_idxOf [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : l[idxOf a l]? = some a := by rw [getElem?_eq_getElem, getElem_idxOf (idxOf_lt_length_iff.2 h)] @[deprecated (since := "2025-01-30")] alias getElem?_indexOf := getElem?_idxOf @[deprecated (since := "2025-02-15")] alias idxOf_get? := getElem?_idxOf @[deprecated (since := "2025-01-30")] alias indexOf_get? := getElem?_idxOf theorem idxOf_inj [DecidableEq α] {l : List α} {x y : α} (hx : x ∈ l) (hy : y ∈ l) : idxOf x l = idxOf y l ↔ x = y := ⟨fun h => by have x_eq_y : get l ⟨idxOf x l, idxOf_lt_length_iff.2 hx⟩ = get l ⟨idxOf y l, idxOf_lt_length_iff.2 hy⟩ := by simp only [h] simp only [idxOf_get] at x_eq_y; exact x_eq_y, fun h => by subst h; rfl⟩ @[deprecated (since := "2025-01-30")] alias indexOf_inj := idxOf_inj theorem get_reverse' (l : List α) (n) (hn') : l.reverse.get n = l.get ⟨l.length - 1 - n, hn'⟩ := by simp theorem eq_cons_of_length_one {l : List α} (h : l.length = 1) : l = [l.get ⟨0, by omega⟩] := by refine ext_get (by convert h) fun n h₁ h₂ => ?_ simp congr omega end deprecated @[simp] theorem getElem_set_of_ne {l : List α} {i j : ℕ} (h : i ≠ j) (a : α) (hj : j < (l.set i a).length) : (l.set i a)[j] = l[j]'(by simpa using hj) := by rw [← Option.some_inj, ← List.getElem?_eq_getElem, List.getElem?_set_ne h, List.getElem?_eq_getElem] /-! ### map -/ -- `List.map_const` (the version with `Function.const` instead of a lambda) is already tagged -- `simp` in Core -- TODO: Upstream the tagging to Core? attribute [simp] map_const' theorem flatMap_pure_eq_map (f : α → β) (l : List α) : l.flatMap (pure ∘ f) = map f l := .symm <| map_eq_flatMap .. theorem flatMap_congr {l : List α} {f g : α → List β} (h : ∀ x ∈ l, f x = g x) : l.flatMap f = l.flatMap g := (congr_arg List.flatten <| map_congr_left h :) theorem infix_flatMap_of_mem {a : α} {as : List α} (h : a ∈ as) (f : α → List α) : f a <:+: as.flatMap f := infix_of_mem_flatten (mem_map_of_mem h) @[simp] theorem map_eq_map {α β} (f : α → β) (l : List α) : f <$> l = map f l := rfl /-- A single `List.map` of a composition of functions is equal to composing a `List.map` with another `List.map`, fully applied. This is the reverse direction of `List.map_map`. -/ theorem comp_map (h : β → γ) (g : α → β) (l : List α) : map (h ∘ g) l = map h (map g l) := map_map.symm /-- Composing a `List.map` with another `List.map` is equal to a single `List.map` of composed functions. -/ @[simp] theorem map_comp_map (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := by ext l; rw [comp_map, Function.comp_apply] section map_bijectivity theorem _root_.Function.LeftInverse.list_map {f : α → β} {g : β → α} (h : LeftInverse f g) : LeftInverse (map f) (map g) | [] => by simp_rw [map_nil] | x :: xs => by simp_rw [map_cons, h x, h.list_map xs] nonrec theorem _root_.Function.RightInverse.list_map {f : α → β} {g : β → α} (h : RightInverse f g) : RightInverse (map f) (map g) := h.list_map nonrec theorem _root_.Function.Involutive.list_map {f : α → α} (h : Involutive f) : Involutive (map f) := Function.LeftInverse.list_map h @[simp] theorem map_leftInverse_iff {f : α → β} {g : β → α} : LeftInverse (map f) (map g) ↔ LeftInverse f g := ⟨fun h x => by injection h [x], (·.list_map)⟩ @[simp] theorem map_rightInverse_iff {f : α → β} {g : β → α} : RightInverse (map f) (map g) ↔ RightInverse f g := map_leftInverse_iff @[simp] theorem map_involutive_iff {f : α → α} : Involutive (map f) ↔ Involutive f := map_leftInverse_iff theorem _root_.Function.Injective.list_map {f : α → β} (h : Injective f) : Injective (map f) | [], [], _ => rfl | x :: xs, y :: ys, hxy => by injection hxy with hxy hxys rw [h hxy, h.list_map hxys] @[simp] theorem map_injective_iff {f : α → β} : Injective (map f) ↔ Injective f := by refine ⟨fun h x y hxy => ?_, (·.list_map)⟩ suffices [x] = [y] by simpa using this apply h simp [hxy] theorem _root_.Function.Surjective.list_map {f : α → β} (h : Surjective f) : Surjective (map f) := let ⟨_, h⟩ := h.hasRightInverse; h.list_map.surjective @[simp] theorem map_surjective_iff {f : α → β} : Surjective (map f) ↔ Surjective f := by refine ⟨fun h x => ?_, (·.list_map)⟩ let ⟨[y], hxy⟩ := h [x] exact ⟨_, List.singleton_injective hxy⟩ theorem _root_.Function.Bijective.list_map {f : α → β} (h : Bijective f) : Bijective (map f) := ⟨h.1.list_map, h.2.list_map⟩ @[simp] theorem map_bijective_iff {f : α → β} : Bijective (map f) ↔ Bijective f := by simp_rw [Function.Bijective, map_injective_iff, map_surjective_iff] end map_bijectivity theorem eq_of_mem_map_const {b₁ b₂ : β} {l : List α} (h : b₁ ∈ map (const α b₂) l) : b₁ = b₂ := by rw [map_const] at h; exact eq_of_mem_replicate h /-- `eq_nil_or_concat` in simp normal form -/ lemma eq_nil_or_concat' (l : List α) : l = [] ∨ ∃ L b, l = L ++ [b] := by simpa using l.eq_nil_or_concat /-! ### foldl, foldr -/ theorem foldl_ext (f g : α → β → α) (a : α) {l : List β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := by induction l generalizing a with | nil => rfl | cons hd tl ih => unfold foldl rw [ih _ fun a b bin => H a b <| mem_cons_of_mem _ bin, H a hd mem_cons_self] theorem foldr_ext (f g : α → β → β) (b : β) {l : List α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := by induction l with | nil => rfl | cons hd tl ih => ?_ simp only [mem_cons, or_imp, forall_and, forall_eq] at H simp only [foldr, ih H.2, H.1] theorem foldl_concat (f : β → α → β) (b : β) (x : α) (xs : List α) : List.foldl f b (xs ++ [x]) = f (List.foldl f b xs) x := by simp only [List.foldl_append, List.foldl] theorem foldr_concat (f : α → β → β) (b : β) (x : α) (xs : List α) : List.foldr f b (xs ++ [x]) = (List.foldr f (f x b) xs) := by simp only [List.foldr_append, List.foldr] theorem foldl_fixed' {f : α → β → α} {a : α} (hf : ∀ b, f a b = a) : ∀ l : List β, foldl f a l = a | [] => rfl | b :: l => by rw [foldl_cons, hf b, foldl_fixed' hf l] theorem foldr_fixed' {f : α → β → β} {b : β} (hf : ∀ a, f a b = b) : ∀ l : List α, foldr f b l = b | [] => rfl | a :: l => by rw [foldr_cons, foldr_fixed' hf l, hf a] @[simp] theorem foldl_fixed {a : α} : ∀ l : List β, foldl (fun a _ => a) a l = a := foldl_fixed' fun _ => rfl @[simp] theorem foldr_fixed {b : β} : ∀ l : List α, foldr (fun _ b => b) b l = b := foldr_fixed' fun _ => rfl @[deprecated foldr_cons_nil (since := "2025-02-10")] theorem foldr_eta (l : List α) : foldr cons [] l = l := foldr_cons_nil theorem reverse_foldl {l : List α} : reverse (foldl (fun t h => h :: t) [] l) = l := by simp theorem foldl_hom₂ (l : List ι) (f : α → β → γ) (op₁ : α → ι → α) (op₂ : β → ι → β) (op₃ : γ → ι → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ a i) (op₂ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl op₂ b l) := Eq.symm <| by revert a b induction l <;> intros <;> [rfl; simp only [*, foldl]] theorem foldr_hom₂ (l : List ι) (f : α → β → γ) (op₁ : ι → α → α) (op₂ : ι → β → β) (op₃ : ι → γ → γ) (a : α) (b : β) (h : ∀ a b i, f (op₁ i a) (op₂ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr op₂ b l) := by revert a induction l <;> intros <;> [rfl; simp only [*, foldr]] theorem injective_foldl_comp {l : List (α → α)} {f : α → α} (hl : ∀ f ∈ l, Function.Injective f) (hf : Function.Injective f) : Function.Injective (@List.foldl (α → α) (α → α) Function.comp f l) := by induction l generalizing f with | nil => exact hf | cons lh lt l_ih => apply l_ih fun _ h => hl _ (List.mem_cons_of_mem _ h) apply Function.Injective.comp hf apply hl _ mem_cons_self /-- Consider two lists `l₁` and `l₂` with designated elements `a₁` and `a₂` somewhere in them: `l₁ = x₁ ++ [a₁] ++ z₁` and `l₂ = x₂ ++ [a₂] ++ z₂`. Assume the designated element `a₂` is present in neither `x₁` nor `z₁`. We conclude that the lists are equal (`l₁ = l₂`) if and only if their respective parts are equal (`x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂`). -/ lemma append_cons_inj_of_not_mem {x₁ x₂ z₁ z₂ : List α} {a₁ a₂ : α} (notin_x : a₂ ∉ x₁) (notin_z : a₂ ∉ z₁) : x₁ ++ a₁ :: z₁ = x₂ ++ a₂ :: z₂ ↔ x₁ = x₂ ∧ a₁ = a₂ ∧ z₁ = z₂ := by constructor · simp only [append_eq_append_iff, cons_eq_append_iff, cons_eq_cons] rintro (⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩ | ⟨c, rfl, ⟨rfl, rfl, rfl⟩ | ⟨d, rfl, rfl⟩⟩) <;> simp_all · rintro ⟨rfl, rfl, rfl⟩ rfl section FoldlEqFoldr -- foldl and foldr coincide when f is commutative and associative variable {f : α → α → α} theorem foldl1_eq_foldr1 [hassoc : Std.Associative f] : ∀ a b l, foldl f a (l ++ [b]) = foldr f b (a :: l) | _, _, nil => rfl | a, b, c :: l => by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l] rw [hassoc.assoc] theorem foldl_eq_of_comm_of_assoc [hcomm : Std.Commutative f] [hassoc : Std.Associative f] : ∀ a b l, foldl f a (b :: l) = f b (foldl f a l) | a, b, nil => hcomm.comm a b | a, b, c :: l => by simp only [foldl_cons] have : RightCommutative f := inferInstance rw [← foldl_eq_of_comm_of_assoc .., this.right_comm, foldl_cons] theorem foldl_eq_foldr [Std.Commutative f] [Std.Associative f] : ∀ a l, foldl f a l = foldr f a l | _, nil => rfl | a, b :: l => by simp only [foldr_cons, foldl_eq_of_comm_of_assoc] rw [foldl_eq_foldr a l] end FoldlEqFoldr section FoldlEqFoldlr' variable {f : α → β → α} variable (hf : ∀ a b c, f (f a b) c = f (f a c) b) include hf theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b :: l) = f (foldl f a l) b | _, _, [] => rfl | a, b, c :: l => by rw [foldl, foldl, foldl, ← foldl_eq_of_comm' .., foldl, hf] theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l | _, [] => rfl | a, b :: l => by rw [foldl_eq_of_comm' hf, foldr, foldl_eq_foldr' ..]; rfl end FoldlEqFoldlr' section FoldlEqFoldlr' variable {f : α → β → β} theorem foldr_eq_of_comm' (hf : ∀ a b c, f a (f b c) = f b (f a c)) : ∀ a b l, foldr f a (b :: l) = foldr f (f b a) l | _, _, [] => rfl | a, b, c :: l => by rw [foldr, foldr, foldr, hf, ← foldr_eq_of_comm' hf ..]; rfl end FoldlEqFoldlr' section variable {op : α → α → α} [ha : Std.Associative op] /-- Notation for `op a b`. -/ local notation a " ⋆ " b => op a b /-- Notation for `foldl op a l`. -/ local notation l " <*> " a => foldl op a l theorem foldl_op_eq_op_foldr_assoc : ∀ {l : List α} {a₁ a₂}, ((l <*> a₁) ⋆ a₂) = a₁ ⋆ l.foldr (· ⋆ ·) a₂ | [], _, _ => rfl | a :: l, a₁, a₂ => by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] variable [hc : Std.Commutative op] theorem foldl_assoc_comm_cons {l : List α} {a₁ a₂} : ((a₁ :: l) <*> a₂) = a₁ ⋆ l <*> a₂ := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### foldlM, foldrM, mapM -/ section FoldlMFoldrM variable {m : Type v → Type w} [Monad m] variable [LawfulMonad m] theorem foldrM_eq_foldr (f : α → β → m β) (b l) : foldrM f b l = foldr (fun a mb => mb >>= f a) (pure b) l := by induction l <;> simp [*] theorem foldlM_eq_foldl (f : β → α → m β) (b l) : List.foldlM f b l = foldl (fun mb a => mb >>= fun b => f b a) (pure b) l := by suffices h : ∀ mb : m β, (mb >>= fun b => List.foldlM f b l) = foldl (fun mb a => mb >>= fun b => f b a) mb l by simp [← h (pure b)] induction l with | nil => intro; simp | cons _ _ l_ih => intro; simp only [List.foldlM, foldl, ← l_ih, functor_norm] end FoldlMFoldrM /-! ### intersperse -/ @[deprecated (since := "2025-02-07")] alias intersperse_singleton := intersperse_single @[deprecated (since := "2025-02-07")] alias intersperse_cons_cons := intersperse_cons₂ /-! ### map for partial functions -/ @[deprecated "Deprecated without replacement." (since := "2025-02-07")] theorem sizeOf_lt_sizeOf_of_mem [SizeOf α] {x : α} {l : List α} (hx : x ∈ l) : SizeOf.sizeOf x < SizeOf.sizeOf l := by induction l with | nil => ?_ | cons h t ih => ?_ <;> cases hx <;> rw [cons.sizeOf_spec] · omega · specialize ih ‹_› omega /-! ### filter -/ theorem length_eq_length_filter_add {l : List (α)} (f : α → Bool) : l.length = (l.filter f).length + (l.filter (! f ·)).length := by simp_rw [← List.countP_eq_length_filter, l.length_eq_countP_add_countP f, Bool.not_eq_true, Bool.decide_eq_false] /-! ### filterMap -/ theorem filterMap_eq_flatMap_toList (f : α → Option β) (l : List α) : l.filterMap f = l.flatMap fun a ↦ (f a).toList := by induction l with | nil => ?_ | cons a l ih => ?_ <;> simp [filterMap_cons] rcases f a <;> simp [ih] theorem filterMap_congr {f g : α → Option β} {l : List α} (h : ∀ x ∈ l, f x = g x) : l.filterMap f = l.filterMap g := by induction l <;> simp_all [filterMap_cons] theorem filterMap_eq_map_iff_forall_eq_some {f : α → Option β} {g : α → β} {l : List α} : l.filterMap f = l.map g ↔ ∀ x ∈ l, f x = some (g x) where mp := by induction l with | nil => simp | cons a l ih => ?_ rcases ha : f a with - | b <;> simp [ha, filterMap_cons] · intro h simpa [show (filterMap f l).length = l.length + 1 from by simp[h], Nat.add_one_le_iff] using List.length_filterMap_le f l · rintro rfl h exact ⟨rfl, ih h⟩ mpr h := Eq.trans (filterMap_congr <| by simpa) (congr_fun filterMap_eq_map _) /-! ### filter -/ section Filter variable {p : α → Bool} theorem filter_singleton {a : α} : [a].filter p = bif p a then [a] else [] := rfl theorem filter_eq_foldr (p : α → Bool) (l : List α) : filter p l = foldr (fun a out => bif p a then a :: out else out) [] l := by induction l <;> simp [*, filter]; rfl #adaptation_note /-- nightly-2024-07-27 This has to be temporarily renamed to avoid an unintentional collision. The prime should be removed at nightly-2024-07-27. -/ @[simp] theorem filter_subset' (l : List α) : filter p l ⊆ l := filter_sublist.subset theorem of_mem_filter {a : α} {l} (h : a ∈ filter p l) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset' l h theorem mem_filter_of_mem {a : α} {l} (h₁ : a ∈ l) (h₂ : p a) : a ∈ filter p l := mem_filter.2 ⟨h₁, h₂⟩ @[deprecated (since := "2025-02-07")] alias monotone_filter_left := filter_subset variable (p) theorem monotone_filter_right (l : List α) ⦃p q : α → Bool⦄ (h : ∀ a, p a → q a) : l.filter p <+ l.filter q := by induction l with | nil => rfl | cons hd tl IH => by_cases hp : p hd · rw [filter_cons_of_pos hp, filter_cons_of_pos (h _ hp)] exact IH.cons_cons hd · rw [filter_cons_of_neg hp] by_cases hq : q hd · rw [filter_cons_of_pos hq] exact sublist_cons_of_sublist hd IH · rw [filter_cons_of_neg hq] exact IH lemma map_filter {f : α → β} (hf : Injective f) (l : List α) [DecidablePred fun b => ∃ a, p a ∧ f a = b] : (l.filter p).map f = (l.map f).filter fun b => ∃ a, p a ∧ f a = b := by simp [comp_def, filter_map, hf.eq_iff] @[deprecated (since := "2025-02-07")] alias map_filter' := map_filter lemma filter_attach' (l : List α) (p : {a // a ∈ l} → Bool) [DecidableEq α] : l.attach.filter p = (l.filter fun x => ∃ h, p ⟨x, h⟩).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := by classical refine map_injective_iff.2 Subtype.coe_injective ?_ simp [comp_def, map_filter _ Subtype.coe_injective] lemma filter_attach (l : List α) (p : α → Bool) : (l.attach.filter fun x => p x : List {x // x ∈ l}) = (l.filter p).attach.map (Subtype.map id fun _ => mem_of_mem_filter) := map_injective_iff.2 Subtype.coe_injective <| by simp_rw [map_map, comp_def, Subtype.map, id, ← Function.comp_apply (g := Subtype.val), ← filter_map, attach_map_subtype_val] lemma filter_comm (q) (l : List α) : filter p (filter q l) = filter q (filter p l) := by simp [Bool.and_comm] @[simp] theorem filter_true (l : List α) : filter (fun _ => true) l = l := by induction l <;> simp [*, filter] @[simp] theorem filter_false (l : List α) : filter (fun _ => false) l = [] := by induction l <;> simp [*, filter] end Filter /-! ### eraseP -/ section eraseP variable {p : α → Bool} @[simp] theorem length_eraseP_add_one {l : List α} {a} (al : a ∈ l) (pa : p a) : (l.eraseP p).length + 1 = l.length := by let ⟨_, l₁, l₂, _, _, h₁, h₂⟩ := exists_of_eraseP al pa rw [h₂, h₁, length_append, length_append] rfl end eraseP /-! ### erase -/ section Erase variable [DecidableEq α] @[simp] theorem length_erase_add_one {a : α} {l : List α} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_eraseP, length_eraseP_add_one h (decide_eq_true rfl)] theorem map_erase [DecidableEq β] {f : α → β} (finj : Injective f) {a : α} (l : List α) : map f (l.erase a) = (map f l).erase (f a) := by have this : (a == ·) = (f a == f ·) := by ext b; simp [beq_eq_decide, finj.eq_iff] rw [erase_eq_eraseP, erase_eq_eraseP, eraseP_map, this]; rfl theorem map_foldl_erase [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (foldl List.erase l₁ l₂) = foldl (fun l a => l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁ <;> [rfl; simp only [foldl_cons, map_erase finj, *]] theorem erase_getElem [DecidableEq ι] {l : List ι} {i : ℕ} (hi : i < l.length) : Perm (l.erase l[i]) (l.eraseIdx i) := by induction l generalizing i with | nil => simp | cons a l IH => cases i with | zero => simp | succ i => have hi' : i < l.length := by simpa using hi if ha : a = l[i] then simpa [ha] using .trans (perm_cons_erase (getElem_mem _)) (.cons _ (IH hi')) else simpa [ha] using IH hi' theorem length_eraseIdx_add_one {l : List ι} {i : ℕ} (h : i < l.length) : (l.eraseIdx i).length + 1 = l.length := by rw [length_eraseIdx] split <;> omega end Erase /-! ### diff -/ section Diff variable [DecidableEq α] @[simp] theorem map_diff [DecidableEq β] {f : α → β} (finj : Injective f) {l₁ l₂ : List α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] @[deprecated (since := "2025-04-10")] alias erase_diff_erase_sublist_of_sublist := Sublist.erase_diff_erase_sublist end Diff section Choose variable (p : α → Prop) [DecidablePred p] (l : List α) theorem choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (chooseX p l hp).property theorem choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 theorem choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end Choose /-! ### Forall -/ section Forall variable {p q : α → Prop} {l : List α} @[simp] theorem forall_cons (p : α → Prop) (x : α) : ∀ l : List α, Forall p (x :: l) ↔ p x ∧ Forall p l | [] => (and_iff_left_of_imp fun _ ↦ trivial).symm | _ :: _ => Iff.rfl @[simp] theorem forall_append {p : α → Prop} : ∀ {xs ys : List α}, Forall p (xs ++ ys) ↔ Forall p xs ∧ Forall p ys | [] => by simp | _ :: _ => by simp [forall_append, and_assoc] theorem forall_iff_forall_mem : ∀ {l : List α}, Forall p l ↔ ∀ x ∈ l, p x | [] => (iff_true_intro <| forall_mem_nil _).symm | x :: l => by rw [forall_mem_cons, forall_cons, forall_iff_forall_mem] theorem Forall.imp (h : ∀ x, p x → q x) : ∀ {l : List α}, Forall p l → Forall q l | [] => id | x :: l => by simp only [forall_cons, and_imp] rw [← and_imp] exact And.imp (h x) (Forall.imp h) @[simp] theorem forall_map_iff {p : β → Prop} (f : α → β) : Forall p (l.map f) ↔ Forall (p ∘ f) l := by induction l <;> simp [*] instance (p : α → Prop) [DecidablePred p] : DecidablePred (Forall p) := fun _ => decidable_of_iff' _ forall_iff_forall_mem end Forall /-! ### Miscellaneous lemmas -/ theorem get_attach (l : List α) (i) : (l.attach.get i).1 = l.get ⟨i, length_attach (l := l) ▸ i.2⟩ := by simp section Disjoint /-- The images of disjoint lists under a partially defined map are disjoint -/ theorem disjoint_pmap {p : α → Prop} {f : ∀ a : α, p a → β} {s t : List α} (hs : ∀ a ∈ s, p a) (ht : ∀ a ∈ t, p a) (hf : ∀ (a a' : α) (ha : p a) (ha' : p a'), f a ha = f a' ha' → a = a') (h : Disjoint s t) : Disjoint (s.pmap f hs) (t.pmap f ht) := by simp only [Disjoint, mem_pmap] rintro b ⟨a, ha, rfl⟩ ⟨a', ha', ha''⟩ apply h ha rwa [hf a a' (hs a ha) (ht a' ha') ha''.symm] /-- The images of disjoint lists under an injective map are disjoint -/ theorem disjoint_map {f : α → β} {s t : List α} (hf : Function.Injective f) (h : Disjoint s t) : Disjoint (s.map f) (t.map f) := by rw [← pmap_eq_map (fun _ _ ↦ trivial), ← pmap_eq_map (fun _ _ ↦ trivial)] exact disjoint_pmap _ _ (fun _ _ _ _ h' ↦ hf h') h alias Disjoint.map := disjoint_map theorem Disjoint.of_map {f : α → β} {s t : List α} (h : Disjoint (s.map f) (t.map f)) : Disjoint s t := fun _a has hat ↦ h (mem_map_of_mem has) (mem_map_of_mem hat) theorem Disjoint.map_iff {f : α → β} {s t : List α} (hf : Function.Injective f) : Disjoint (s.map f) (t.map f) ↔ Disjoint s t := ⟨fun h ↦ h.of_map, fun h ↦ h.map hf⟩ theorem Perm.disjoint_left {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l₁ l ↔ Disjoint l₂ l := by simp_rw [List.disjoint_left, p.mem_iff] theorem Perm.disjoint_right {l₁ l₂ l : List α} (p : List.Perm l₁ l₂) : Disjoint l l₁ ↔ Disjoint l l₂ := by simp_rw [List.disjoint_right, p.mem_iff] @[simp] theorem disjoint_reverse_left {l₁ l₂ : List α} : Disjoint l₁.reverse l₂ ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_left @[simp] theorem disjoint_reverse_right {l₁ l₂ : List α} : Disjoint l₁ l₂.reverse ↔ Disjoint l₁ l₂ := reverse_perm _ |>.disjoint_right end Disjoint section lookup variable [BEq α] [LawfulBEq α] lemma lookup_graph (f : α → β) {a : α} {as : List α} (h : a ∈ as) : lookup a (as.map fun x => (x, f x)) = some (f a) := by induction as with | nil => exact (not_mem_nil h).elim | cons a' as ih => by_cases ha : a = a' · simp [ha, lookup_cons] · simpa [lookup_cons, beq_false_of_ne ha] using ih (List.mem_of_ne_of_mem ha h) end lookup section range' @[simp] lemma range'_0 (a b : ℕ) : range' a b 0 = replicate b a := by induction b with | zero => simp | succ b ih => simp [range'_succ, ih, replicate_succ] lemma left_le_of_mem_range' {a b s x : ℕ} (hx : x ∈ List.range' a b s) : a ≤ x := by obtain ⟨i, _, rfl⟩ := List.mem_range'.mp hx exact le_add_right a (s * i) end range' end List
Mathlib/Data/List/Basic.lean
2,833
2,841
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Kim Morrison -/ import Mathlib.CategoryTheory.Opposites /-! # Morphisms from equations between objects. When working categorically, sometimes one encounters an equation `h : X = Y` between objects. Your initial aversion to this is natural and appropriate: you're in for some trouble, and if there is another way to approach the problem that won't rely on this equality, it may be worth pursuing. You have two options: 1. Use the equality `h` as one normally would in Lean (e.g. using `rw` and `subst`). This may immediately cause difficulties, because in category theory everything is dependently typed, and equations between objects quickly lead to nasty goals with `eq.rec`. 2. Promote `h` to a morphism using `eqToHom h : X ⟶ Y`, or `eqToIso h : X ≅ Y`. This file introduces various `simp` lemmas which in favourable circumstances result in the various `eqToHom` morphisms to drop out at the appropriate moment! -/ universe v₁ v₂ v₃ u₁ u₂ u₃ -- morphism levels before object levels. See note [CategoryTheory universes]. namespace CategoryTheory open Opposite variable {C : Type u₁} [Category.{v₁} C] /-- An equality `X = Y` gives us a morphism `X ⟶ Y`. It is typically better to use this, rather than rewriting by the equality then using `𝟙 _` which usually leads to dependent type theory hell. -/ def eqToHom {X Y : C} (p : X = Y) : X ⟶ Y := by rw [p]; exact 𝟙 _ @[simp] theorem eqToHom_refl (X : C) (p : X = X) : eqToHom p = 𝟙 X := rfl @[reassoc (attr := simp)] theorem eqToHom_trans {X Y Z : C} (p : X = Y) (q : Y = Z) : eqToHom p ≫ eqToHom q = eqToHom (p.trans q) := by cases p cases q simp /-- `eqToHom h` is heterogeneously equal to the identity of its domain. -/ lemma eqToHom_heq_id_dom (X Y : C) (h : X = Y) : HEq (eqToHom h) (𝟙 X) := by subst h; rfl /-- `eqToHom h` is heterogeneously equal to the identity of its codomain. -/ lemma eqToHom_heq_id_cod (X Y : C) (h : X = Y) : HEq (eqToHom h) (𝟙 Y) := by subst h; rfl /-- Two morphisms are conjugate via eqToHom if and only if they are heterogeneously equal. Note this used to be in the Functor namespace, where it doesn't belong. -/ theorem conj_eqToHom_iff_heq {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) (h : W = Y) (h' : X = Z) : f = eqToHom h ≫ g ≫ eqToHom h'.symm ↔ HEq f g := by cases h cases h' simp theorem conj_eqToHom_iff_heq' {C} [Category C] {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) (h : W = Y) (h' : Z = X) : f = eqToHom h ≫ g ≫ eqToHom h' ↔ HEq f g := conj_eqToHom_iff_heq _ _ _ h'.symm theorem comp_eqToHom_iff {X Y Y' : C} (p : Y = Y') (f : X ⟶ Y) (g : X ⟶ Y') : f ≫ eqToHom p = g ↔ f = g ≫ eqToHom p.symm := { mp := fun h => h ▸ by simp mpr := fun h => by simp [eq_whisker h (eqToHom p)] } theorem eqToHom_comp_iff {X X' Y : C} (p : X = X') (f : X ⟶ Y) (g : X' ⟶ Y) : eqToHom p ≫ g = f ↔ g = eqToHom p.symm ≫ f := { mp := fun h => h ▸ by simp mpr := fun h => h ▸ by simp [whisker_eq _ h] } theorem eqToHom_comp_heq {C} [Category C] {W X Y : C} (f : Y ⟶ X) (h : W = Y) : HEq (eqToHom h ≫ f) f := by rw [← conj_eqToHom_iff_heq _ _ h rfl, eqToHom_refl, Category.comp_id] @[simp] theorem eqToHom_comp_heq_iff {C} [Category C] {W X Y Z Z' : C} (f : Y ⟶ X) (g : Z ⟶ Z') (h : W = Y) : HEq (eqToHom h ≫ f) g ↔ HEq f g := ⟨(eqToHom_comp_heq ..).symm.trans, (eqToHom_comp_heq ..).trans⟩ @[simp] theorem heq_eqToHom_comp_iff {C} [Category C] {W X Y Z Z' : C} (f : Y ⟶ X) (g : Z ⟶ Z') (h : W = Y) : HEq g (eqToHom h ≫ f) ↔ HEq g f := ⟨(·.trans (eqToHom_comp_heq ..)), (·.trans (eqToHom_comp_heq ..).symm)⟩ theorem comp_eqToHom_heq {C} [Category C] {X Y Z : C} (f : X ⟶ Y) (h : Y = Z) : HEq (f ≫ eqToHom h) f := by rw [← conj_eqToHom_iff_heq' _ _ rfl h, eqToHom_refl, Category.id_comp] @[simp] theorem comp_eqToHom_heq_iff {C} [Category C] {W X Y Z Z' : C} (f : X ⟶ Y) (g : Z ⟶ Z') (h : Y = W) : HEq (f ≫ eqToHom h) g ↔ HEq f g := ⟨(comp_eqToHom_heq ..).symm.trans, (comp_eqToHom_heq ..).trans⟩ @[simp] theorem heq_comp_eqToHom_iff {C} [Category C] {W X Y Z Z' : C} (f : X ⟶ Y) (g : Z ⟶ Z') (h : Y = W) : HEq g (f ≫ eqToHom h) ↔ HEq g f := ⟨(·.trans (comp_eqToHom_heq ..)), (·.trans (comp_eqToHom_heq ..).symm)⟩ theorem heq_comp {C} [Category C] {X Y Z X' Y' Z' : C} {f : X ⟶ Y} {g : Y ⟶ Z} {f' : X' ⟶ Y'} {g' : Y' ⟶ Z'} (eq1 : X = X') (eq2 : Y = Y') (eq3 : Z = Z') (H1 : HEq f f') (H2 : HEq g g') : HEq (f ≫ g) (f' ≫ g') := by cases eq1; cases eq2; cases eq3; cases H1; cases H2; rfl variable {β : Sort*} /-- We can push `eqToHom` to the left through families of morphisms. -/ -- The simpNF linter incorrectly claims that this will never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 @[reassoc (attr := simp, nolint simpNF)] theorem eqToHom_naturality {f g : β → C} (z : ∀ b, f b ⟶ g b) {j j' : β} (w : j = j') : z j ≫ eqToHom (by simp [w]) = eqToHom (by simp [w]) ≫ z j' := by cases w simp /-- A variant on `eqToHom_naturality` that helps Lean identify the families `f` and `g`. -/ -- The simpNF linter incorrectly claims that this will never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 @[reassoc (attr := simp, nolint simpNF)] theorem eqToHom_iso_hom_naturality {f g : β → C} (z : ∀ b, f b ≅ g b) {j j' : β} (w : j = j') : (z j).hom ≫ eqToHom (by simp [w]) = eqToHom (by simp [w]) ≫ (z j').hom := by
cases w simp /-- A variant on `eqToHom_naturality` that helps Lean identify the families `f` and `g`. -/
Mathlib/CategoryTheory/EqToHom.lean
138
141
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.CategoryTheory.Sites.IsSheafFor import Mathlib.CategoryTheory.Limits.Types.Shapes import Mathlib.Tactic.ApplyFun /-! # The equalizer diagram sheaf condition for a presieve In `Mathlib/CategoryTheory/Sites/IsSheafFor.lean` it is defined what it means for a presheaf to be a sheaf *for* a particular presieve. In this file we provide equivalent conditions in terms of equalizer diagrams. * In `Equalizer.Presieve.sheaf_condition`, the sheaf condition at a presieve is shown to be equivalent to that of https://stacks.math.columbia.edu/tag/00VM (and combined with `isSheaf_pretopology`, this shows the notions of `IsSheaf` are exactly equivalent.) * In `Equalizer.Sieve.equalizer_sheaf_condition`, the sheaf condition at a sieve is shown to be equivalent to that of Equation (3) p. 122 in Maclane-Moerdijk [MM92]. ## References * [MM92]: *Sheaves in geometry and logic*, Saunders MacLane, and Ieke Moerdijk: Chapter III, Section 4. * https://stacks.math.columbia.edu/tag/00VL (sheaves on a pretopology or site) -/ universe w v u namespace CategoryTheory open Opposite CategoryTheory Category Limits Sieve namespace Equalizer variable {C : Type u} [Category.{v} C] (P : Cᵒᵖ ⥤ Type max v u) {X : C} (R : Presieve X) (S : Sieve X) noncomputable section /-- The middle object of the fork diagram given in Equation (3) of [MM92], as well as the fork diagram of the Stacks entry. -/ @[stacks 00VM "This is the middle object of the fork diagram there."] def FirstObj : Type max v u := ∏ᶜ fun f : ΣY, { f : Y ⟶ X // R f } => P.obj (op f.1) variable {P R} -- Porting note (https://github.com/leanprover-community/mathlib4/issues/10688): added to ease automation @[ext] lemma FirstObj.ext (z₁ z₂ : FirstObj P R) (h : ∀ (Y : C) (f : Y ⟶ X) (hf : R f), (Pi.π _ ⟨Y, f, hf⟩ : FirstObj P R ⟶ _) z₁ = (Pi.π _ ⟨Y, f, hf⟩ : FirstObj P R ⟶ _) z₂) : z₁ = z₂ := by apply Limits.Types.limit_ext rintro ⟨⟨Y, f, hf⟩⟩ exact h Y f hf variable (P R) /-- Show that `FirstObj` is isomorphic to `FamilyOfElements`. -/ @[simps] def firstObjEqFamily : FirstObj P R ≅ R.FamilyOfElements P where hom t _ _ hf := Pi.π (fun f : ΣY, { f : Y ⟶ X // R f } => P.obj (op f.1)) ⟨_, _, hf⟩ t inv := Pi.lift fun f x => x _ f.2.2 instance : Inhabited (FirstObj P (⊥ : Presieve X)) := (firstObjEqFamily P _).toEquiv.inhabited -- Porting note: was not needed in mathlib instance : Inhabited (FirstObj P ((⊥ : Sieve X) : Presieve X)) := (inferInstance : Inhabited (FirstObj P (⊥ : Presieve X))) /-- The left morphism of the fork diagram given in Equation (3) of [MM92], as well as the fork diagram of the Stacks entry. -/ @[stacks 00VM "This is the left morphism of the fork diagram there."] def forkMap : P.obj (op X) ⟶ FirstObj P R := Pi.lift fun f => P.map f.2.1.op /-! This section establishes the equivalence between the sheaf condition of Equation (3) [MM92] and the definition of `IsSheafFor`. -/ namespace Sieve /-- The rightmost object of the fork diagram of Equation (3) [MM92], which contains the data used to check a family is compatible. -/ def SecondObj : Type max v u := ∏ᶜ fun f : Σ (Y Z : _) (_ : Z ⟶ Y), { f' : Y ⟶ X // S f' } => P.obj (op f.2.1) variable {P S} -- Porting note (https://github.com/leanprover-community/mathlib4/issues/10688): added to ease automation @[ext] lemma SecondObj.ext (z₁ z₂ : SecondObj P S) (h : ∀ (Y Z : C) (g : Z ⟶ Y) (f : Y ⟶ X) (hf : S.arrows f), (Pi.π _ ⟨Y, Z, g, f, hf⟩ : SecondObj P S ⟶ _) z₁ = (Pi.π _ ⟨Y, Z, g, f, hf⟩ : SecondObj P S ⟶ _) z₂) : z₁ = z₂ := by apply Limits.Types.limit_ext rintro ⟨⟨Y, Z, g, f, hf⟩⟩ apply h variable (P S) /-- The map `p` of Equations (3,4) [MM92]. -/ def firstMap : FirstObj P (S : Presieve X) ⟶ SecondObj P S := Pi.lift fun fg => Pi.π _ (⟨_, _, S.downward_closed fg.2.2.2.2 fg.2.2.1⟩ : ΣY, { f : Y ⟶ X // S f }) instance : Inhabited (SecondObj P (⊥ : Sieve X)) := ⟨firstMap _ _ default⟩ /-- The map `a` of Equations (3,4) [MM92]. -/ def secondMap : FirstObj P (S : Presieve X) ⟶ SecondObj P S := Pi.lift fun fg => Pi.π _ ⟨_, fg.2.2.2⟩ ≫ P.map fg.2.2.1.op theorem w : forkMap P (S : Presieve X) ≫ firstMap P S = forkMap P S ≫ secondMap P S := by ext simp [firstMap, secondMap, forkMap] /-- The family of elements given by `x : FirstObj P S` is compatible iff `firstMap` and `secondMap` map it to the same point. -/ theorem compatible_iff (x : FirstObj P S.arrows) : ((firstObjEqFamily P S.arrows).hom x).Compatible ↔ firstMap P S x = secondMap P S x := by rw [Presieve.compatible_iff_sieveCompatible] constructor · intro t apply SecondObj.ext intros Y Z g f hf simpa [firstMap, secondMap] using t _ g hf · intro t Y Z f g hf rw [Types.limit_ext_iff'] at t simpa [firstMap, secondMap] using t ⟨⟨Y, Z, g, f, hf⟩⟩ /-- `P` is a sheaf for `S`, iff the fork given by `w` is an equalizer. -/ theorem equalizer_sheaf_condition : Presieve.IsSheafFor P (S : Presieve X) ↔ Nonempty (IsLimit (Fork.ofι _ (w P S))) := by rw [Types.type_equalizer_iff_unique, ← Equiv.forall_congr_right (firstObjEqFamily P (S : Presieve X)).toEquiv.symm] simp_rw [← compatible_iff] simp only [inv_hom_id_apply, Iso.toEquiv_symm_fun] apply forall₂_congr intro x _ apply existsUnique_congr intro t rw [← Iso.toEquiv_symm_fun] rw [Equiv.eq_symm_apply] constructor · intro q funext Y f hf simpa [firstObjEqFamily, forkMap] using q _ _ · intro q Y f hf rw [← q] simp [firstObjEqFamily, forkMap] end Sieve /-! This section establishes the equivalence between the sheaf condition of https://stacks.math.columbia.edu/tag/00VM and the definition of `isSheafFor`. -/ namespace Presieve variable [R.hasPullbacks] /-- The rightmost object of the fork diagram of the Stacks entry, which contains the data used to check a family of elements for a presieve is compatible. -/ @[simp, stacks 00VM "This is the rightmost object of the fork diagram there."] def SecondObj : Type max v u := ∏ᶜ fun fg : (ΣY, { f : Y ⟶ X // R f }) × ΣZ, { g : Z ⟶ X // R g } => haveI := Presieve.hasPullbacks.has_pullbacks fg.1.2.2 fg.2.2.2 P.obj (op (pullback fg.1.2.1 fg.2.2.1)) /-- The map `pr₀*` of the Stacks entry. -/ @[stacks 00VM "This is the map `pr₀*` there."] def firstMap : FirstObj P R ⟶ SecondObj P R := Pi.lift fun fg => haveI := Presieve.hasPullbacks.has_pullbacks fg.1.2.2 fg.2.2.2 Pi.π _ _ ≫ P.map (pullback.fst _ _).op instance [HasPullbacks C] : Inhabited (SecondObj P (⊥ : Presieve X)) := ⟨firstMap _ _ default⟩ /-- The map `pr₁*` of the Stacks entry. -/ @[stacks 00VM "This is the map `pr₁*` there."] def secondMap : FirstObj P R ⟶ SecondObj P R := Pi.lift fun fg => haveI := Presieve.hasPullbacks.has_pullbacks fg.1.2.2 fg.2.2.2 Pi.π _ _ ≫ P.map (pullback.snd _ _).op theorem w : forkMap P R ≫ firstMap P R = forkMap P R ≫ secondMap P R := by dsimp ext fg simp only [firstMap, secondMap, forkMap] simp only [limit.lift_π, limit.lift_π_assoc, assoc, Fan.mk_π_app] haveI := Presieve.hasPullbacks.has_pullbacks fg.1.2.2 fg.2.2.2 rw [← P.map_comp, ← op_comp, pullback.condition] simp
/-- The family of elements given by `x : FirstObj P S` is compatible iff `firstMap` and `secondMap` map it to the same point. -/ theorem compatible_iff (x : FirstObj P R) : ((firstObjEqFamily P R).hom x).Compatible ↔ firstMap P R x = secondMap P R x := by rw [Presieve.pullbackCompatible_iff] constructor
Mathlib/CategoryTheory/Sites/EqualizerSheafCondition.lean
216
223
/- 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.Algebra.CharP.Algebra import Mathlib.Algebra.CharP.Invertible import Mathlib.Algebra.CharP.Lemmas import Mathlib.Algebra.EuclideanDomain.Field import Mathlib.Algebra.Field.ZMod import Mathlib.Algebra.Polynomial.Roots import Mathlib.RingTheory.Polynomial.Chebyshev /-! # Dickson polynomials The (generalised) Dickson polynomials are a family of polynomials indexed by `ℕ × ℕ`, with coefficients in a commutative ring `R` depending on an element `a∈R`. More precisely, the they satisfy the recursion `dickson k a (n + 2) = X * (dickson k a n + 1) - a * (dickson k a n)` with starting values `dickson k a 0 = 3 - k` and `dickson k a 1 = X`. In the literature, `dickson k a n` is called the `n`-th Dickson polynomial of the `k`-th kind associated to the parameter `a : R`. They are closely related to the Chebyshev polynomials in the case that `a=1`. When `a=0` they are just the family of monomials `X ^ n`. ## Main definition * `Polynomial.dickson`: the generalised Dickson polynomials. ## Main statements * `Polynomial.dickson_one_one_mul`, the `(m * n)`-th Dickson polynomial of the first kind for parameter `1 : R` is the composition of the `m`-th and `n`-th Dickson polynomials of the first kind for `1 : R`. * `Polynomial.dickson_one_one_charP`, for a prime number `p`, the `p`-th Dickson polynomial of the first kind associated to parameter `1 : R` is congruent to `X ^ p` modulo `p`. ## References * [R. Lidl, G. L. Mullen and G. Turnwald, _Dickson polynomials_][MR1237403] ## TODO * Redefine `dickson` in terms of `LinearRecurrence`. * Show that `dickson 2 1` is equal to the characteristic polynomial of the adjacency matrix of a type A Dynkin diagram. * Prove that the adjacency matrices of simply laced Dynkin diagrams are precisely the adjacency matrices of simple connected graphs which annihilate `dickson 2 1`. -/ noncomputable section namespace Polynomial variable {R S : Type*} [CommRing R] [CommRing S] (k : ℕ) (a : R) /-- `dickson` is the `n`-th (generalised) Dickson polynomial of the `k`-th kind associated to the element `a ∈ R`. -/ noncomputable def dickson : ℕ → R[X] | 0 => 3 - k | 1 => X | n + 2 => X * dickson (n + 1) - C a * dickson n @[simp] theorem dickson_zero : dickson k a 0 = 3 - k := rfl @[simp] theorem dickson_one : dickson k a 1 = X := rfl theorem dickson_two : dickson k a 2 = X ^ 2 - C a * (3 - k : R[X]) := by simp only [dickson, sq] @[simp] theorem dickson_add_two (n : ℕ) : dickson k a (n + 2) = X * dickson k a (n + 1) - C a * dickson k a n := by rw [dickson] theorem dickson_of_two_le {n : ℕ} (h : 2 ≤ n) : dickson k a n = X * dickson k a (n - 1) - C a * dickson k a (n - 2) := by obtain ⟨n, rfl⟩ := Nat.exists_eq_add_of_le h rw [add_comm] exact dickson_add_two k a n variable {k a} theorem map_dickson (f : R →+* S) : ∀ n : ℕ, map f (dickson k a n) = dickson k (f a) n | 0 => by simp_rw [dickson_zero, Polynomial.map_sub, Polynomial.map_natCast, Polynomial.map_ofNat] | 1 => by simp only [dickson_one, map_X] | n + 2 => by simp only [dickson_add_two, Polynomial.map_sub, Polynomial.map_mul, map_X, map_C] rw [map_dickson f n, map_dickson f (n + 1)] @[simp] theorem dickson_two_zero : ∀ n : ℕ, dickson 2 (0 : R) n = X ^ n | 0 => by simp only [dickson_zero, pow_zero] norm_num | 1 => by simp only [dickson_one, pow_one] | n + 2 => by simp only [dickson_add_two, C_0, zero_mul, sub_zero] rw [dickson_two_zero (n + 1), pow_add X (n + 1) 1, mul_comm, pow_one] section Dickson /-! ### A Lambda structure on `ℤ[X]` Mathlib doesn't currently know what a Lambda ring is. But once it does, we can endow `ℤ[X]` with a Lambda structure in terms of the `dickson 1 1` polynomials defined below. There is exactly one other Lambda structure on `ℤ[X]` in terms of binomial polynomials. -/ theorem dickson_one_one_eval_add_inv (x y : R) (h : x * y = 1) : ∀ n, (dickson 1 (1 : R) n).eval (x + y) = x ^ n + y ^ n | 0 => by simp only [eval_one, eval_add, pow_zero, dickson_zero]; norm_num | 1 => by simp only [eval_X, dickson_one, pow_one] | n + 2 => by simp only [eval_sub, eval_mul, dickson_one_one_eval_add_inv x y h _, eval_X, dickson_add_two, C_1, eval_one] conv_lhs => simp only [pow_succ', add_mul, mul_add, h, ← mul_assoc, mul_comm y x, one_mul] ring variable (R) -- Porting note: Added 2 new theorems for convenience private theorem two_mul_C_half_eq_one [Invertible (2 : R)] : 2 * C (⅟ 2 : R) = 1 := by rw [two_mul, ← C_add, invOf_two_add_invOf_two, C_1] private theorem C_half_mul_two_eq_one [Invertible (2 : R)] : C (⅟ 2 : R) * 2 = 1 := by rw [mul_comm, two_mul_C_half_eq_one] theorem dickson_one_one_eq_chebyshev_C : ∀ n, dickson 1 (1 : R) n = Chebyshev.C R n | 0 => by simp only [Chebyshev.C_zero, mul_one, one_comp, dickson_zero] norm_num | 1 => by rw [dickson_one, Nat.cast_one, Chebyshev.C_one] | n + 2 => by rw [dickson_add_two, C_1, Nat.cast_add, Nat.cast_two, Chebyshev.C_add_two, dickson_one_one_eq_chebyshev_C (n + 1), dickson_one_one_eq_chebyshev_C n] push_cast ring theorem dickson_one_one_eq_chebyshev_T [Invertible (2 : R)] (n : ℕ) : dickson 1 (1 : R) n = 2 * (Chebyshev.T R n).comp (C (⅟ 2) * X) := (dickson_one_one_eq_chebyshev_C R n).trans (Chebyshev.C_eq_two_mul_T_comp_half_mul_X R n) theorem chebyshev_T_eq_dickson_one_one [Invertible (2 : R)] (n : ℕ) : Chebyshev.T R n = C (⅟ 2) * (dickson 1 1 n).comp (2 * X) := dickson_one_one_eq_chebyshev_C R n ▸ Chebyshev.T_eq_half_mul_C_comp_two_mul_X R n theorem dickson_two_one_eq_chebyshev_S : ∀ n, dickson 2 (1 : R) n = Chebyshev.S R n | 0 => by simp only [Chebyshev.S_zero, mul_one, one_comp, dickson_zero] norm_num | 1 => by rw [dickson_one, Nat.cast_one, Chebyshev.S_one] | n + 2 => by rw [dickson_add_two, C_1, Nat.cast_add, Nat.cast_two, Chebyshev.S_add_two,
dickson_two_one_eq_chebyshev_S (n + 1), dickson_two_one_eq_chebyshev_S n] push_cast ring
Mathlib/RingTheory/Polynomial/Dickson.lean
166
169
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import Mathlib.Algebra.Field.NegOnePow import Mathlib.Algebra.Field.Periodic import Mathlib.Algebra.QuadraticDiscriminant import Mathlib.Analysis.SpecialFunctions.Exp /-! # Trigonometric functions ## Main definitions This file contains the definition of `π`. See also `Analysis.SpecialFunctions.Trigonometric.Inverse` and `Analysis.SpecialFunctions.Trigonometric.Arctan` for the inverse trigonometric functions. See also `Analysis.SpecialFunctions.Complex.Arg` and `Analysis.SpecialFunctions.Complex.Log` for the complex argument function and the complex logarithm. ## Main statements Many basic inequalities on the real trigonometric functions are established. The continuity of the usual trigonometric functions is proved. Several facts about the real trigonometric functions have the proofs deferred to `Analysis.SpecialFunctions.Trigonometric.Complex`, as they are most easily proved by appealing to the corresponding fact for complex trigonometric functions. See also `Analysis.SpecialFunctions.Trigonometric.Chebyshev` for the multiple angle formulas in terms of Chebyshev polynomials. ## Tags sin, cos, tan, angle -/ noncomputable section open Topology Filter Set namespace Complex @[continuity, fun_prop] theorem continuous_sin : Continuous sin := by change Continuous fun z => (exp (-z * I) - exp (z * I)) * I / 2 fun_prop @[fun_prop] theorem continuousOn_sin {s : Set ℂ} : ContinuousOn sin s := continuous_sin.continuousOn @[continuity, fun_prop] theorem continuous_cos : Continuous cos := by change Continuous fun z => (exp (z * I) + exp (-z * I)) / 2 fun_prop @[fun_prop] theorem continuousOn_cos {s : Set ℂ} : ContinuousOn cos s := continuous_cos.continuousOn @[continuity, fun_prop] theorem continuous_sinh : Continuous sinh := by change Continuous fun z => (exp z - exp (-z)) / 2 fun_prop @[continuity, fun_prop] theorem continuous_cosh : Continuous cosh := by change Continuous fun z => (exp z + exp (-z)) / 2 fun_prop end Complex namespace Real variable {x y z : ℝ} @[continuity, fun_prop] theorem continuous_sin : Continuous sin := Complex.continuous_re.comp (Complex.continuous_sin.comp Complex.continuous_ofReal) @[fun_prop] theorem continuousOn_sin {s} : ContinuousOn sin s := continuous_sin.continuousOn @[continuity, fun_prop] theorem continuous_cos : Continuous cos := Complex.continuous_re.comp (Complex.continuous_cos.comp Complex.continuous_ofReal) @[fun_prop] theorem continuousOn_cos {s} : ContinuousOn cos s := continuous_cos.continuousOn @[continuity, fun_prop] theorem continuous_sinh : Continuous sinh := Complex.continuous_re.comp (Complex.continuous_sinh.comp Complex.continuous_ofReal) @[continuity, fun_prop] theorem continuous_cosh : Continuous cosh := Complex.continuous_re.comp (Complex.continuous_cosh.comp Complex.continuous_ofReal) end Real namespace Real theorem exists_cos_eq_zero : 0 ∈ cos '' Icc (1 : ℝ) 2 := intermediate_value_Icc' (by norm_num) continuousOn_cos ⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩ /-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from which one can derive all its properties. For explicit bounds on π, see `Data.Real.Pi.Bounds`. Denoted `π`, once the `Real` namespace is opened. -/ protected noncomputable def pi : ℝ := 2 * Classical.choose exists_cos_eq_zero @[inherit_doc] scoped notation "π" => Real.pi @[simp] theorem cos_pi_div_two : cos (π / 2) = 0 := by rw [Real.pi, mul_div_cancel_left₀ _ (two_ne_zero' ℝ)] exact (Classical.choose_spec exists_cos_eq_zero).2 theorem one_le_pi_div_two : (1 : ℝ) ≤ π / 2 := by rw [Real.pi, mul_div_cancel_left₀ _ (two_ne_zero' ℝ)] exact (Classical.choose_spec exists_cos_eq_zero).1.1 theorem pi_div_two_le_two : π / 2 ≤ 2 := by rw [Real.pi, mul_div_cancel_left₀ _ (two_ne_zero' ℝ)] exact (Classical.choose_spec exists_cos_eq_zero).1.2 theorem two_le_pi : (2 : ℝ) ≤ π := (div_le_div_iff_of_pos_right (show (0 : ℝ) < 2 by norm_num)).1 (by rw [div_self (two_ne_zero' ℝ)]; exact one_le_pi_div_two) theorem pi_le_four : π ≤ 4 := (div_le_div_iff_of_pos_right (show (0 : ℝ) < 2 by norm_num)).1 (calc π / 2 ≤ 2 := pi_div_two_le_two _ = 4 / 2 := by norm_num) @[bound] theorem pi_pos : 0 < π := lt_of_lt_of_le (by norm_num) two_le_pi @[bound] theorem pi_nonneg : 0 ≤ π := pi_pos.le theorem pi_ne_zero : π ≠ 0 := pi_pos.ne' theorem pi_div_two_pos : 0 < π / 2 := half_pos pi_pos theorem two_pi_pos : 0 < 2 * π := by linarith [pi_pos] end Real namespace Mathlib.Meta.Positivity open Lean.Meta Qq /-- Extension for the `positivity` tactic: `π` is always positive. -/ @[positivity Real.pi] def evalRealPi : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q(Real.pi) => assertInstancesCommute pure (.positive q(Real.pi_pos)) | _, _, _ => throwError "not Real.pi" end Mathlib.Meta.Positivity namespace NNReal open Real open Real NNReal /-- `π` considered as a nonnegative real. -/ noncomputable def pi : ℝ≥0 := ⟨π, Real.pi_pos.le⟩ @[simp] theorem coe_real_pi : (pi : ℝ) = π := rfl theorem pi_pos : 0 < pi := mod_cast Real.pi_pos theorem pi_ne_zero : pi ≠ 0 := pi_pos.ne' end NNReal namespace Real @[simp] theorem sin_pi : sin π = 0 := by rw [← mul_div_cancel_left₀ π (two_ne_zero' ℝ), two_mul, add_div, sin_add, cos_pi_div_two]; simp @[simp] theorem cos_pi : cos π = -1 := by rw [← mul_div_cancel_left₀ π (two_ne_zero' ℝ), mul_div_assoc, cos_two_mul, cos_pi_div_two] norm_num @[simp] theorem sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] theorem cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] theorem sin_antiperiodic : Function.Antiperiodic sin π := by simp [sin_add] theorem sin_periodic : Function.Periodic sin (2 * π) := sin_antiperiodic.periodic_two_mul @[simp] theorem sin_add_pi (x : ℝ) : sin (x + π) = -sin x := sin_antiperiodic x @[simp] theorem sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x := sin_periodic x @[simp] theorem sin_sub_pi (x : ℝ) : sin (x - π) = -sin x := sin_antiperiodic.sub_eq x @[simp] theorem sin_sub_two_pi (x : ℝ) : sin (x - 2 * π) = sin x := sin_periodic.sub_eq x @[simp] theorem sin_pi_sub (x : ℝ) : sin (π - x) = sin x := neg_neg (sin x) ▸ sin_neg x ▸ sin_antiperiodic.sub_eq' @[simp] theorem sin_two_pi_sub (x : ℝ) : sin (2 * π - x) = -sin x := sin_neg x ▸ sin_periodic.sub_eq' @[simp] theorem sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := sin_antiperiodic.nat_mul_eq_of_eq_zero sin_zero n @[simp] theorem sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := sin_antiperiodic.int_mul_eq_of_eq_zero sin_zero n @[simp] theorem sin_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x + n * (2 * π)) = sin x := sin_periodic.nat_mul n x @[simp] theorem sin_add_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x + n * (2 * π)) = sin x := sin_periodic.int_mul n x @[simp] theorem sin_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x - n * (2 * π)) = sin x := sin_periodic.sub_nat_mul_eq n @[simp] theorem sin_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x - n * (2 * π)) = sin x := sin_periodic.sub_int_mul_eq n @[simp] theorem sin_nat_mul_two_pi_sub (x : ℝ) (n : ℕ) : sin (n * (2 * π) - x) = -sin x := sin_neg x ▸ sin_periodic.nat_mul_sub_eq n @[simp] theorem sin_int_mul_two_pi_sub (x : ℝ) (n : ℤ) : sin (n * (2 * π) - x) = -sin x := sin_neg x ▸ sin_periodic.int_mul_sub_eq n theorem sin_add_int_mul_pi (x : ℝ) (n : ℤ) : sin (x + n * π) = (-1) ^ n * sin x := n.cast_negOnePow ℝ ▸ sin_antiperiodic.add_int_mul_eq n theorem sin_add_nat_mul_pi (x : ℝ) (n : ℕ) : sin (x + n * π) = (-1) ^ n * sin x := sin_antiperiodic.add_nat_mul_eq n theorem sin_sub_int_mul_pi (x : ℝ) (n : ℤ) : sin (x - n * π) = (-1) ^ n * sin x := n.cast_negOnePow ℝ ▸ sin_antiperiodic.sub_int_mul_eq n theorem sin_sub_nat_mul_pi (x : ℝ) (n : ℕ) : sin (x - n * π) = (-1) ^ n * sin x := sin_antiperiodic.sub_nat_mul_eq n theorem sin_int_mul_pi_sub (x : ℝ) (n : ℤ) : sin (n * π - x) = -((-1) ^ n * sin x) := by simpa only [sin_neg, mul_neg, Int.cast_negOnePow] using sin_antiperiodic.int_mul_sub_eq n theorem sin_nat_mul_pi_sub (x : ℝ) (n : ℕ) : sin (n * π - x) = -((-1) ^ n * sin x) := by simpa only [sin_neg, mul_neg] using sin_antiperiodic.nat_mul_sub_eq n theorem cos_antiperiodic : Function.Antiperiodic cos π := by simp [cos_add] theorem cos_periodic : Function.Periodic cos (2 * π) := cos_antiperiodic.periodic_two_mul @[simp] theorem abs_cos_int_mul_pi (k : ℤ) : |cos (k * π)| = 1 := by simp [abs_cos_eq_sqrt_one_sub_sin_sq] @[simp] theorem cos_add_pi (x : ℝ) : cos (x + π) = -cos x := cos_antiperiodic x @[simp] theorem cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x := cos_periodic x @[simp] theorem cos_sub_pi (x : ℝ) : cos (x - π) = -cos x := cos_antiperiodic.sub_eq x @[simp] theorem cos_sub_two_pi (x : ℝ) : cos (x - 2 * π) = cos x := cos_periodic.sub_eq x @[simp] theorem cos_pi_sub (x : ℝ) : cos (π - x) = -cos x := cos_neg x ▸ cos_antiperiodic.sub_eq' @[simp] theorem cos_two_pi_sub (x : ℝ) : cos (2 * π - x) = cos x := cos_neg x ▸ cos_periodic.sub_eq' @[simp] theorem cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := (cos_periodic.nat_mul_eq n).trans cos_zero @[simp] theorem cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := (cos_periodic.int_mul_eq n).trans cos_zero @[simp] theorem cos_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x + n * (2 * π)) = cos x := cos_periodic.nat_mul n x @[simp] theorem cos_add_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x + n * (2 * π)) = cos x := cos_periodic.int_mul n x @[simp] theorem cos_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x - n * (2 * π)) = cos x := cos_periodic.sub_nat_mul_eq n @[simp] theorem cos_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x - n * (2 * π)) = cos x := cos_periodic.sub_int_mul_eq n @[simp] theorem cos_nat_mul_two_pi_sub (x : ℝ) (n : ℕ) : cos (n * (2 * π) - x) = cos x := cos_neg x ▸ cos_periodic.nat_mul_sub_eq n @[simp] theorem cos_int_mul_two_pi_sub (x : ℝ) (n : ℤ) : cos (n * (2 * π) - x) = cos x := cos_neg x ▸ cos_periodic.int_mul_sub_eq n theorem cos_add_int_mul_pi (x : ℝ) (n : ℤ) : cos (x + n * π) = (-1) ^ n * cos x := n.cast_negOnePow ℝ ▸ cos_antiperiodic.add_int_mul_eq n theorem cos_add_nat_mul_pi (x : ℝ) (n : ℕ) : cos (x + n * π) = (-1) ^ n * cos x := cos_antiperiodic.add_nat_mul_eq n theorem cos_sub_int_mul_pi (x : ℝ) (n : ℤ) : cos (x - n * π) = (-1) ^ n * cos x := n.cast_negOnePow ℝ ▸ cos_antiperiodic.sub_int_mul_eq n theorem cos_sub_nat_mul_pi (x : ℝ) (n : ℕ) : cos (x - n * π) = (-1) ^ n * cos x := cos_antiperiodic.sub_nat_mul_eq n theorem cos_int_mul_pi_sub (x : ℝ) (n : ℤ) : cos (n * π - x) = (-1) ^ n * cos x := n.cast_negOnePow ℝ ▸ cos_neg x ▸ cos_antiperiodic.int_mul_sub_eq n theorem cos_nat_mul_pi_sub (x : ℝ) (n : ℕ) : cos (n * π - x) = (-1) ^ n * cos x := cos_neg x ▸ cos_antiperiodic.nat_mul_sub_eq n theorem cos_nat_mul_two_pi_add_pi (n : ℕ) : cos (n * (2 * π) + π) = -1 := by simpa only [cos_zero] using (cos_periodic.nat_mul n).add_antiperiod_eq cos_antiperiodic theorem cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simpa only [cos_zero] using (cos_periodic.int_mul n).add_antiperiod_eq cos_antiperiodic theorem cos_nat_mul_two_pi_sub_pi (n : ℕ) : cos (n * (2 * π) - π) = -1 := by simpa only [cos_zero] using (cos_periodic.nat_mul n).sub_antiperiod_eq cos_antiperiodic theorem cos_int_mul_two_pi_sub_pi (n : ℤ) : cos (n * (2 * π) - π) = -1 := by simpa only [cos_zero] using (cos_periodic.int_mul n).sub_antiperiod_eq cos_antiperiodic theorem sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x := if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2 else have : (2 : ℝ) + 2 = 4 := by norm_num have : π - x ≤ 2 := sub_le_iff_le_add.2 (le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)) sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this theorem sin_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo 0 π) : 0 < sin x := sin_pos_of_pos_of_lt_pi hx.1 hx.2 theorem sin_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc 0 π) : 0 ≤ sin x := by rw [← closure_Ioo pi_ne_zero.symm] at hx exact closure_lt_subset_le continuous_const continuous_sin (closure_mono (fun y => sin_pos_of_mem_Ioo) hx) theorem sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x := sin_nonneg_of_mem_Icc ⟨h0x, hxp⟩ theorem sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 := neg_pos.1 <| sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx) theorem sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 := neg_nonneg.1 <| sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx) @[simp] theorem sin_pi_div_two : sin (π / 2) = 1 := have : sin (π / 2) = 1 ∨ sin (π / 2) = -1 := by simpa [sq, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2) this.resolve_right fun h => show ¬(0 : ℝ) < -1 by norm_num <| h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos) theorem sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x := by simp [sin_add] theorem sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] theorem sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] theorem cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x := by simp [cos_add] theorem cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] theorem cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] theorem cos_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : 0 < cos x := sin_add_pi_div_two x ▸ sin_pos_of_mem_Ioo ⟨by linarith [hx.1], by linarith [hx.2]⟩ theorem cos_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : 0 ≤ cos x := sin_add_pi_div_two x ▸ sin_nonneg_of_mem_Icc ⟨by linarith [hx.1], by linarith [hx.2]⟩ theorem cos_nonneg_of_neg_pi_div_two_le_of_le {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) : 0 ≤ cos x := cos_nonneg_of_mem_Icc ⟨hl, hu⟩ theorem cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 := neg_pos.1 <| cos_pi_sub x ▸ cos_pos_of_mem_Ioo ⟨by linarith, by linarith⟩ theorem cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) : cos x ≤ 0 := neg_nonneg.1 <| cos_pi_sub x ▸ cos_nonneg_of_mem_Icc ⟨by linarith, by linarith⟩ theorem sin_eq_sqrt_one_sub_cos_sq {x : ℝ} (hl : 0 ≤ x) (hu : x ≤ π) : sin x = √(1 - cos x ^ 2) := by rw [← abs_sin_eq_sqrt_one_sub_cos_sq, abs_of_nonneg (sin_nonneg_of_nonneg_of_le_pi hl hu)] theorem cos_eq_sqrt_one_sub_sin_sq {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) : cos x = √(1 - sin x ^ 2) := by rw [← abs_cos_eq_sqrt_one_sub_sin_sq, abs_of_nonneg (cos_nonneg_of_mem_Icc ⟨hl, hu⟩)] lemma cos_half {x : ℝ} (hl : -π ≤ x) (hr : x ≤ π) : cos (x / 2) = sqrt ((1 + cos x) / 2) := by have : 0 ≤ cos (x / 2) := cos_nonneg_of_mem_Icc <| by constructor <;> linarith rw [← sqrt_sq this, cos_sq, add_div, two_mul, add_halves] lemma abs_sin_half (x : ℝ) : |sin (x / 2)| = sqrt ((1 - cos x) / 2) := by rw [← sqrt_sq_eq_abs, sin_sq_eq_half_sub, two_mul, add_halves, sub_div] lemma sin_half_eq_sqrt {x : ℝ} (hl : 0 ≤ x) (hr : x ≤ 2 * π) : sin (x / 2) = sqrt ((1 - cos x) / 2) := by rw [← abs_sin_half, abs_of_nonneg] apply sin_nonneg_of_nonneg_of_le_pi <;> linarith lemma sin_half_eq_neg_sqrt {x : ℝ} (hl : -(2 * π) ≤ x) (hr : x ≤ 0) : sin (x / 2) = -sqrt ((1 - cos x) / 2) := by rw [← abs_sin_half, abs_of_nonpos, neg_neg] apply sin_nonpos_of_nonnpos_of_neg_pi_le <;> linarith theorem sin_eq_zero_iff_of_lt_of_lt {x : ℝ} (hx₁ : -π < x) (hx₂ : x < π) : sin x = 0 ↔ x = 0 := ⟨fun h => by contrapose! h cases h.lt_or_lt with | inl h0 => exact (sin_neg_of_neg_of_neg_pi_lt h0 hx₁).ne | inr h0 => exact (sin_pos_of_pos_of_lt_pi h0 hx₂).ne', fun h => by simp [h]⟩ theorem sin_eq_zero_iff {x : ℝ} : sin x = 0 ↔ ∃ n : ℤ, (n : ℝ) * π = x := ⟨fun h => ⟨⌊x / π⌋, le_antisymm (sub_nonneg.1 (Int.sub_floor_div_mul_nonneg _ pi_pos)) (sub_nonpos.1 <| le_of_not_gt fun h₃ => (sin_pos_of_pos_of_lt_pi h₃ (Int.sub_floor_div_mul_lt _ pi_pos)).ne (by simp [sub_eq_add_neg, sin_add, h, sin_int_mul_pi]))⟩, fun ⟨_, hn⟩ => hn ▸ sin_int_mul_pi _⟩ theorem sin_ne_zero_iff {x : ℝ} : sin x ≠ 0 ↔ ∀ n : ℤ, (n : ℝ) * π ≠ x := by rw [← not_exists, not_iff_not, sin_eq_zero_iff] theorem sin_eq_zero_iff_cos_eq {x : ℝ} : sin x = 0 ↔ cos x = 1 ∨ cos x = -1 := by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq x, sq, sq, ← sub_eq_iff_eq_add, sub_self] exact ⟨fun h => by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ Eq.symm⟩ theorem cos_eq_one_iff (x : ℝ) : cos x = 1 ↔ ∃ n : ℤ, (n : ℝ) * (2 * π) = x := ⟨fun h => let ⟨n, hn⟩ := sin_eq_zero_iff.1 (sin_eq_zero_iff_cos_eq.2 (Or.inl h)) ⟨n / 2, (Int.emod_two_eq_zero_or_one n).elim (fun hn0 => by rwa [← mul_assoc, ← @Int.cast_two ℝ, ← Int.cast_mul, Int.ediv_mul_cancel (Int.dvd_iff_emod_eq_zero.2 hn0)]) fun hn1 => by rw [← Int.emod_add_ediv n 2, hn1, Int.cast_add, Int.cast_one, add_mul, one_mul, add_comm, mul_comm (2 : ℤ), Int.cast_mul, mul_assoc, Int.cast_two] at hn rw [← hn, cos_int_mul_two_pi_add_pi] at h exact absurd h (by norm_num)⟩, fun ⟨_, hn⟩ => hn ▸ cos_int_mul_two_pi _⟩ theorem cos_eq_one_iff_of_lt_of_lt {x : ℝ} (hx₁ : -(2 * π) < x) (hx₂ : x < 2 * π) : cos x = 1 ↔ x = 0 := ⟨fun h => by rcases (cos_eq_one_iff _).1 h with ⟨n, rfl⟩ rw [mul_lt_iff_lt_one_left two_pi_pos] at hx₂ rw [neg_lt, neg_mul_eq_neg_mul, mul_lt_iff_lt_one_left two_pi_pos] at hx₁ norm_cast at hx₁ hx₂ obtain rfl : n = 0 := le_antisymm (by omega) (by omega) simp, fun h => by simp [h]⟩ theorem sin_lt_sin_of_lt_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hy₂ : y ≤ π / 2) (hxy : x < y) : sin x < sin y := by rw [← sub_pos, sin_sub_sin] have : 0 < sin ((y - x) / 2) := by apply sin_pos_of_pos_of_lt_pi <;> linarith have : 0 < cos ((y + x) / 2) := by refine cos_pos_of_mem_Ioo ⟨?_, ?_⟩ <;> linarith positivity theorem strictMonoOn_sin : StrictMonoOn sin (Icc (-(π / 2)) (π / 2)) := fun _ hx _ hy hxy => sin_lt_sin_of_lt_of_le_pi_div_two hx.1 hy.2 hxy theorem cos_lt_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x < y) : cos y < cos x := by rw [← sin_pi_div_two_sub, ← sin_pi_div_two_sub] apply sin_lt_sin_of_lt_of_le_pi_div_two <;> linarith theorem cos_lt_cos_of_nonneg_of_le_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π / 2) (hxy : x < y) : cos y < cos x := cos_lt_cos_of_nonneg_of_le_pi hx₁ (hy₂.trans (by linarith)) hxy theorem strictAntiOn_cos : StrictAntiOn cos (Icc 0 π) := fun _ hx _ hy hxy => cos_lt_cos_of_nonneg_of_le_pi hx.1 hy.2 hxy theorem cos_le_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x ≤ y) : cos y ≤ cos x := (strictAntiOn_cos.le_iff_le ⟨hx₁.trans hxy, hy₂⟩ ⟨hx₁, hxy.trans hy₂⟩).2 hxy theorem sin_le_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hy₂ : y ≤ π / 2) (hxy : x ≤ y) : sin x ≤ sin y := (strictMonoOn_sin.le_iff_le ⟨hx₁, hxy.trans hy₂⟩ ⟨hx₁.trans hxy, hy₂⟩).2 hxy theorem injOn_sin : InjOn sin (Icc (-(π / 2)) (π / 2)) := strictMonoOn_sin.injOn theorem injOn_cos : InjOn cos (Icc 0 π) := strictAntiOn_cos.injOn theorem surjOn_sin : SurjOn sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) := by simpa only [sin_neg, sin_pi_div_two] using intermediate_value_Icc (neg_le_self pi_div_two_pos.le) continuous_sin.continuousOn theorem surjOn_cos : SurjOn cos (Icc 0 π) (Icc (-1) 1) := by simpa only [cos_zero, cos_pi] using intermediate_value_Icc' pi_pos.le continuous_cos.continuousOn theorem sin_mem_Icc (x : ℝ) : sin x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_sin x, sin_le_one x⟩ theorem cos_mem_Icc (x : ℝ) : cos x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_cos x, cos_le_one x⟩ theorem mapsTo_sin (s : Set ℝ) : MapsTo sin s (Icc (-1 : ℝ) 1) := fun x _ => sin_mem_Icc x theorem mapsTo_cos (s : Set ℝ) : MapsTo cos s (Icc (-1 : ℝ) 1) := fun x _ => cos_mem_Icc x theorem bijOn_sin : BijOn sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) := ⟨mapsTo_sin _, injOn_sin, surjOn_sin⟩ theorem bijOn_cos : BijOn cos (Icc 0 π) (Icc (-1) 1) := ⟨mapsTo_cos _, injOn_cos, surjOn_cos⟩ @[simp] theorem range_cos : range cos = (Icc (-1) 1 : Set ℝ) := Subset.antisymm (range_subset_iff.2 cos_mem_Icc) surjOn_cos.subset_range @[simp] theorem range_sin : range sin = (Icc (-1) 1 : Set ℝ) := Subset.antisymm (range_subset_iff.2 sin_mem_Icc) surjOn_sin.subset_range theorem range_cos_infinite : (range Real.cos).Infinite := by rw [Real.range_cos] exact Icc_infinite (by norm_num) theorem range_sin_infinite : (range Real.sin).Infinite := by rw [Real.range_sin] exact Icc_infinite (by norm_num) section CosDivSq variable (x : ℝ) /-- the series `sqrtTwoAddSeries x n` is `sqrt(2 + sqrt(2 + ... ))` with `n` square roots, starting with `x`. We define it here because `cos (pi / 2 ^ (n+1)) = sqrtTwoAddSeries 0 n / 2` -/ @[simp] noncomputable def sqrtTwoAddSeries (x : ℝ) : ℕ → ℝ | 0 => x | n + 1 => √(2 + sqrtTwoAddSeries x n) theorem sqrtTwoAddSeries_zero : sqrtTwoAddSeries x 0 = x := by simp theorem sqrtTwoAddSeries_one : sqrtTwoAddSeries 0 1 = √2 := by simp theorem sqrtTwoAddSeries_two : sqrtTwoAddSeries 0 2 = √(2 + √2) := by simp theorem sqrtTwoAddSeries_zero_nonneg : ∀ n : ℕ, 0 ≤ sqrtTwoAddSeries 0 n | 0 => le_refl 0 | _ + 1 => sqrt_nonneg _ theorem sqrtTwoAddSeries_nonneg {x : ℝ} (h : 0 ≤ x) : ∀ n : ℕ, 0 ≤ sqrtTwoAddSeries x n | 0 => h | _ + 1 => sqrt_nonneg _ theorem sqrtTwoAddSeries_lt_two : ∀ n : ℕ, sqrtTwoAddSeries 0 n < 2 | 0 => by norm_num | n + 1 => by refine lt_of_lt_of_le ?_ (sqrt_sq zero_lt_two.le).le rw [sqrtTwoAddSeries, sqrt_lt_sqrt_iff, ← lt_sub_iff_add_lt'] · refine (sqrtTwoAddSeries_lt_two n).trans_le ?_ norm_num · exact add_nonneg zero_le_two (sqrtTwoAddSeries_zero_nonneg n) theorem sqrtTwoAddSeries_succ (x : ℝ) : ∀ n : ℕ, sqrtTwoAddSeries x (n + 1) = sqrtTwoAddSeries (√(2 + x)) n | 0 => rfl | n + 1 => by rw [sqrtTwoAddSeries, sqrtTwoAddSeries_succ _ _, sqrtTwoAddSeries] theorem sqrtTwoAddSeries_monotone_left {x y : ℝ} (h : x ≤ y) : ∀ n : ℕ, sqrtTwoAddSeries x n ≤ sqrtTwoAddSeries y n | 0 => h | n + 1 => by rw [sqrtTwoAddSeries, sqrtTwoAddSeries] exact sqrt_le_sqrt (add_le_add_left (sqrtTwoAddSeries_monotone_left h _) _) @[simp] theorem cos_pi_over_two_pow : ∀ n : ℕ, cos (π / 2 ^ (n + 1)) = sqrtTwoAddSeries 0 n / 2 | 0 => by simp | n + 1 => by have A : (1 : ℝ) < 2 ^ (n + 1) := one_lt_pow₀ one_lt_two n.succ_ne_zero have B : π / 2 ^ (n + 1) < π := div_lt_self pi_pos A have C : 0 < π / 2 ^ (n + 1) := by positivity rw [pow_succ, div_mul_eq_div_div, cos_half, cos_pi_over_two_pow n, sqrtTwoAddSeries, add_div_eq_mul_add_div, one_mul, ← div_mul_eq_div_div, sqrt_div, sqrt_mul_self] <;> linarith [sqrtTwoAddSeries_nonneg le_rfl n] theorem sin_sq_pi_over_two_pow (n : ℕ) : sin (π / 2 ^ (n + 1)) ^ 2 = 1 - (sqrtTwoAddSeries 0 n / 2) ^ 2 := by rw [sin_sq, cos_pi_over_two_pow] theorem sin_sq_pi_over_two_pow_succ (n : ℕ) : sin (π / 2 ^ (n + 2)) ^ 2 = 1 / 2 - sqrtTwoAddSeries 0 n / 4 := by rw [sin_sq_pi_over_two_pow, sqrtTwoAddSeries, div_pow, sq_sqrt, add_div, ← sub_sub] · congr · norm_num · norm_num · exact add_nonneg two_pos.le (sqrtTwoAddSeries_zero_nonneg _) @[simp] theorem sin_pi_over_two_pow_succ (n : ℕ) : sin (π / 2 ^ (n + 2)) = √(2 - sqrtTwoAddSeries 0 n) / 2 := by rw [eq_div_iff_mul_eq two_ne_zero, eq_comm, sqrt_eq_iff_eq_sq, mul_pow, sin_sq_pi_over_two_pow_succ, sub_mul] · congr <;> norm_num · rw [sub_nonneg] exact (sqrtTwoAddSeries_lt_two _).le refine mul_nonneg (sin_nonneg_of_nonneg_of_le_pi ?_ ?_) zero_le_two · positivity · exact div_le_self pi_pos.le <| one_le_pow₀ one_le_two @[simp] theorem cos_pi_div_four : cos (π / 4) = √2 / 2 := by trans cos (π / 2 ^ 2) · congr norm_num · simp @[simp] theorem sin_pi_div_four : sin (π / 4) = √2 / 2 := by trans sin (π / 2 ^ 2) · congr norm_num · simp @[simp] theorem cos_pi_div_eight : cos (π / 8) = √(2 + √2) / 2 := by trans cos (π / 2 ^ 3) · congr norm_num · simp @[simp] theorem sin_pi_div_eight : sin (π / 8) = √(2 - √2) / 2 := by trans sin (π / 2 ^ 3) · congr norm_num · simp @[simp] theorem cos_pi_div_sixteen : cos (π / 16) = √(2 + √(2 + √2)) / 2 := by trans cos (π / 2 ^ 4) · congr norm_num · simp @[simp] theorem sin_pi_div_sixteen : sin (π / 16) = √(2 - √(2 + √2)) / 2 := by trans sin (π / 2 ^ 4) · congr norm_num · simp @[simp] theorem cos_pi_div_thirty_two : cos (π / 32) = √(2 + √(2 + √(2 + √2))) / 2 := by trans cos (π / 2 ^ 5) · congr norm_num · simp @[simp] theorem sin_pi_div_thirty_two : sin (π / 32) = √(2 - √(2 + √(2 + √2))) / 2 := by trans sin (π / 2 ^ 5) · congr norm_num · simp -- This section is also a convenient location for other explicit values of `sin` and `cos`. /-- The cosine of `π / 3` is `1 / 2`. -/ @[simp] theorem cos_pi_div_three : cos (π / 3) = 1 / 2 := by have h₁ : (2 * cos (π / 3) - 1) ^ 2 * (2 * cos (π / 3) + 2) = 0 := by have : cos (3 * (π / 3)) = cos π := by congr 1 ring linarith [cos_pi, cos_three_mul (π / 3)] rcases mul_eq_zero.mp h₁ with h | h · linarith [pow_eq_zero h] · have : cos π < cos (π / 3) := by refine cos_lt_cos_of_nonneg_of_le_pi ?_ le_rfl ?_ <;> linarith [pi_pos] linarith [cos_pi] /-- The cosine of `π / 6` is `√3 / 2`. -/ @[simp] theorem cos_pi_div_six : cos (π / 6) = √3 / 2 := by rw [show (6 : ℝ) = 3 * 2 by norm_num, div_mul_eq_div_div, cos_half, cos_pi_div_three, one_add_div, ← div_mul_eq_div_div, two_add_one_eq_three, sqrt_div, sqrt_mul_self] <;> linarith [pi_pos] /-- The square of the cosine of `π / 6` is `3 / 4` (this is sometimes more convenient than the result for cosine itself). -/ theorem sq_cos_pi_div_six : cos (π / 6) ^ 2 = 3 / 4 := by rw [cos_pi_div_six, div_pow, sq_sqrt] <;> norm_num /-- The sine of `π / 6` is `1 / 2`. -/ @[simp] theorem sin_pi_div_six : sin (π / 6) = 1 / 2 := by rw [← cos_pi_div_two_sub, ← cos_pi_div_three] congr ring /-- The square of the sine of `π / 3` is `3 / 4` (this is sometimes more convenient than the result for cosine itself). -/ theorem sq_sin_pi_div_three : sin (π / 3) ^ 2 = 3 / 4 := by rw [← cos_pi_div_two_sub, ← sq_cos_pi_div_six] congr ring /-- The sine of `π / 3` is `√3 / 2`. -/ @[simp] theorem sin_pi_div_three : sin (π / 3) = √3 / 2 := by rw [← cos_pi_div_two_sub, ← cos_pi_div_six] congr ring theorem quadratic_root_cos_pi_div_five : letI c := cos (π / 5) 4 * c ^ 2 - 2 * c - 1 = 0 := by set θ := π / 5 with hθ set c := cos θ set s := sin θ suffices 2 * c = 4 * c ^ 2 - 1 by simp [this] have hs : s ≠ 0 := by rw [ne_eq, sin_eq_zero_iff, hθ] push_neg intro n hn replace hn : n * 5 = 1 := by field_simp [mul_comm _ π, mul_assoc] at hn; norm_cast at hn omega suffices s * (2 * c) = s * (4 * c ^ 2 - 1) from mul_left_cancel₀ hs this calc s * (2 * c) = 2 * s * c := by rw [← mul_assoc, mul_comm 2] _ = sin (2 * θ) := by rw [sin_two_mul] _ = sin (π - 2 * θ) := by rw [sin_pi_sub] _ = sin (2 * θ + θ) := by congr; field_simp [hθ]; linarith _ = sin (2 * θ) * c + cos (2 * θ) * s := sin_add (2 * θ) θ _ = 2 * s * c * c + cos (2 * θ) * s := by rw [sin_two_mul] _ = 2 * s * c * c + (2 * c ^ 2 - 1) * s := by rw [cos_two_mul] _ = s * (2 * c * c) + s * (2 * c ^ 2 - 1) := by linarith _ = s * (4 * c ^ 2 - 1) := by linarith open Polynomial in theorem Polynomial.isRoot_cos_pi_div_five : (4 • X ^ 2 - 2 • X - C 1 : ℝ[X]).IsRoot (cos (π / 5)) := by simpa using quadratic_root_cos_pi_div_five /-- The cosine of `π / 5` is `(1 + √5) / 4`. -/ @[simp] theorem cos_pi_div_five : cos (π / 5) = (1 + √5) / 4 := by set c := cos (π / 5) have : 4 * (c * c) + (-2) * c + (-1) = 0 := by rw [← sq, neg_mul, ← sub_eq_add_neg, ← sub_eq_add_neg] exact quadratic_root_cos_pi_div_five have hd : discrim 4 (-2) (-1) = (2 * √5) * (2 * √5) := by norm_num [discrim, mul_mul_mul_comm] rcases (quadratic_eq_zero_iff (by norm_num) hd c).mp this with h | h · field_simp [h]; linarith · absurd (show 0 ≤ c from cos_nonneg_of_mem_Icc <| by constructor <;> linarith [pi_pos.le]) rw [not_le, h] exact div_neg_of_neg_of_pos (by norm_num [lt_sqrt]) (by positivity) end CosDivSq /-- `Real.sin` as an `OrderIso` between `[-(π / 2), π / 2]` and `[-1, 1]`. -/ def sinOrderIso : Icc (-(π / 2)) (π / 2) ≃o Icc (-1 : ℝ) 1 := (strictMonoOn_sin.orderIso _ _).trans <| OrderIso.setCongr _ _ bijOn_sin.image_eq @[simp] theorem coe_sinOrderIso_apply (x : Icc (-(π / 2)) (π / 2)) : (sinOrderIso x : ℝ) = sin x := rfl theorem sinOrderIso_apply (x : Icc (-(π / 2)) (π / 2)) : sinOrderIso x = ⟨sin x, sin_mem_Icc x⟩ := rfl @[simp] theorem tan_pi_div_four : tan (π / 4) = 1 := by rw [tan_eq_sin_div_cos, cos_pi_div_four, sin_pi_div_four] have h : √2 / 2 > 0 := by positivity exact div_self (ne_of_gt h) @[simp] theorem tan_pi_div_two : tan (π / 2) = 0 := by simp [tan_eq_sin_div_cos] @[simp] theorem tan_pi_div_six : tan (π / 6) = 1 / sqrt 3 := by rw [tan_eq_sin_div_cos, sin_pi_div_six, cos_pi_div_six] ring @[simp] theorem tan_pi_div_three : tan (π / 3) = sqrt 3 := by rw [tan_eq_sin_div_cos, sin_pi_div_three, cos_pi_div_three] ring theorem tan_pos_of_pos_of_lt_pi_div_two {x : ℝ} (h0x : 0 < x) (hxp : x < π / 2) : 0 < tan x := by rw [tan_eq_sin_div_cos] exact div_pos (sin_pos_of_pos_of_lt_pi h0x (by linarith)) (cos_pos_of_mem_Ioo ⟨by linarith, hxp⟩) theorem tan_nonneg_of_nonneg_of_le_pi_div_two {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π / 2) : 0 ≤ tan x := match lt_or_eq_of_le h0x, lt_or_eq_of_le hxp with | Or.inl hx0, Or.inl hxp => le_of_lt (tan_pos_of_pos_of_lt_pi_div_two hx0 hxp) | Or.inl _, Or.inr hxp => by simp [hxp, tan_eq_sin_div_cos] | Or.inr hx0, _ => by simp [hx0.symm] theorem tan_neg_of_neg_of_pi_div_two_lt {x : ℝ} (hx0 : x < 0) (hpx : -(π / 2) < x) : tan x < 0 := neg_pos.1 (tan_neg x ▸ tan_pos_of_pos_of_lt_pi_div_two (by linarith) (by linarith [pi_pos])) theorem tan_nonpos_of_nonpos_of_neg_pi_div_two_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -(π / 2) ≤ x) : tan x ≤ 0 := neg_nonneg.1 (tan_neg x ▸ tan_nonneg_of_nonneg_of_le_pi_div_two (by linarith) (by linarith)) theorem strictMonoOn_tan : StrictMonoOn tan (Ioo (-(π / 2)) (π / 2)) := by rintro x hx y hy hlt rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos, div_lt_div_iff₀ (cos_pos_of_mem_Ioo hx) (cos_pos_of_mem_Ioo hy), mul_comm, ← sub_pos, ← sin_sub] exact sin_pos_of_pos_of_lt_pi (sub_pos.2 hlt) <| by linarith [hx.1, hy.2] theorem tan_lt_tan_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y := strictMonoOn_tan ⟨hx₁, hxy.trans hy₂⟩ ⟨hx₁.trans hxy, hy₂⟩ hxy theorem tan_lt_tan_of_nonneg_of_lt_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y := tan_lt_tan_of_lt_of_lt_pi_div_two (by linarith) hy₂ hxy theorem injOn_tan : InjOn tan (Ioo (-(π / 2)) (π / 2)) := strictMonoOn_tan.injOn theorem tan_inj_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) (hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : tan x = tan y) : x = y := injOn_tan ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ hxy theorem tan_periodic : Function.Periodic tan π := by simpa only [Function.Periodic, tan_eq_sin_div_cos] using sin_antiperiodic.div cos_antiperiodic @[simp] theorem tan_pi : tan π = 0 := by rw [tan_periodic.eq, tan_zero] theorem tan_add_pi (x : ℝ) : tan (x + π) = tan x := tan_periodic x theorem tan_sub_pi (x : ℝ) : tan (x - π) = tan x := tan_periodic.sub_eq x theorem tan_pi_sub (x : ℝ) : tan (π - x) = -tan x := tan_neg x ▸ tan_periodic.sub_eq' theorem tan_pi_div_two_sub (x : ℝ) : tan (π / 2 - x) = (tan x)⁻¹ := by rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos, inv_div, sin_pi_div_two_sub, cos_pi_div_two_sub] theorem tan_nat_mul_pi (n : ℕ) : tan (n * π) = 0 := tan_zero ▸ tan_periodic.nat_mul_eq n theorem tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 := tan_zero ▸ tan_periodic.int_mul_eq n theorem tan_add_nat_mul_pi (x : ℝ) (n : ℕ) : tan (x + n * π) = tan x := tan_periodic.nat_mul n x theorem tan_add_int_mul_pi (x : ℝ) (n : ℤ) : tan (x + n * π) = tan x := tan_periodic.int_mul n x theorem tan_sub_nat_mul_pi (x : ℝ) (n : ℕ) : tan (x - n * π) = tan x := tan_periodic.sub_nat_mul_eq n theorem tan_sub_int_mul_pi (x : ℝ) (n : ℤ) : tan (x - n * π) = tan x := tan_periodic.sub_int_mul_eq n theorem tan_nat_mul_pi_sub (x : ℝ) (n : ℕ) : tan (n * π - x) = -tan x := tan_neg x ▸ tan_periodic.nat_mul_sub_eq n theorem tan_int_mul_pi_sub (x : ℝ) (n : ℤ) : tan (n * π - x) = -tan x := tan_neg x ▸ tan_periodic.int_mul_sub_eq n theorem tendsto_sin_pi_div_two : Tendsto sin (𝓝[<] (π / 2)) (𝓝 1) := by convert continuous_sin.continuousWithinAt.tendsto simp theorem tendsto_cos_pi_div_two : Tendsto cos (𝓝[<] (π / 2)) (𝓝[>] 0) := by apply tendsto_nhdsWithin_of_tendsto_nhds_of_eventually_within · convert continuous_cos.continuousWithinAt.tendsto simp · filter_upwards [Ioo_mem_nhdsLT (neg_lt_self pi_div_two_pos)] with x hx exact cos_pos_of_mem_Ioo hx theorem tendsto_tan_pi_div_two : Tendsto tan (𝓝[<] (π / 2)) atTop := by convert tendsto_cos_pi_div_two.inv_tendsto_nhdsGT_zero.atTop_mul_pos zero_lt_one tendsto_sin_pi_div_two using 1 simp only [Pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos] theorem tendsto_sin_neg_pi_div_two : Tendsto sin (𝓝[>] (-(π / 2))) (𝓝 (-1)) := by convert continuous_sin.continuousWithinAt.tendsto using 2 simp theorem tendsto_cos_neg_pi_div_two : Tendsto cos (𝓝[>] (-(π / 2))) (𝓝[>] 0) := by apply tendsto_nhdsWithin_of_tendsto_nhds_of_eventually_within · convert continuous_cos.continuousWithinAt.tendsto simp · filter_upwards [Ioo_mem_nhdsGT (neg_lt_self pi_div_two_pos)] with x hx exact cos_pos_of_mem_Ioo hx theorem tendsto_tan_neg_pi_div_two : Tendsto tan (𝓝[>] (-(π / 2))) atBot := by convert tendsto_cos_neg_pi_div_two.inv_tendsto_nhdsGT_zero.atTop_mul_neg (by norm_num) tendsto_sin_neg_pi_div_two using 1 simp only [Pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos] end Real namespace Complex open Real theorem sin_eq_zero_iff_cos_eq {z : ℂ} : sin z = 0 ↔ cos z = 1 ∨ cos z = -1 := by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq, sq, sq, ← sub_eq_iff_eq_add, sub_self] exact ⟨fun h => by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ Eq.symm⟩ @[simp] theorem cos_pi_div_two : cos (π / 2) = 0 := calc cos (π / 2) = Real.cos (π / 2) := by rw [ofReal_cos]; simp _ = 0 := by simp @[simp] theorem sin_pi_div_two : sin (π / 2) = 1 := calc sin (π / 2) = Real.sin (π / 2) := by rw [ofReal_sin]; simp _ = 1 := by simp @[simp] theorem sin_pi : sin π = 0 := by rw [← ofReal_sin, Real.sin_pi]; simp @[simp] theorem cos_pi : cos π = -1 := by rw [← ofReal_cos, Real.cos_pi]; simp @[simp] theorem sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] theorem cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] theorem sin_antiperiodic : Function.Antiperiodic sin π := by simp [sin_add] theorem sin_periodic : Function.Periodic sin (2 * π) := sin_antiperiodic.periodic_two_mul theorem sin_add_pi (x : ℂ) : sin (x + π) = -sin x := sin_antiperiodic x theorem sin_add_two_pi (x : ℂ) : sin (x + 2 * π) = sin x := sin_periodic x theorem sin_sub_pi (x : ℂ) : sin (x - π) = -sin x := sin_antiperiodic.sub_eq x theorem sin_sub_two_pi (x : ℂ) : sin (x - 2 * π) = sin x := sin_periodic.sub_eq x theorem sin_pi_sub (x : ℂ) : sin (π - x) = sin x := neg_neg (sin x) ▸ sin_neg x ▸ sin_antiperiodic.sub_eq' theorem sin_two_pi_sub (x : ℂ) : sin (2 * π - x) = -sin x := sin_neg x ▸ sin_periodic.sub_eq' theorem sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := sin_antiperiodic.nat_mul_eq_of_eq_zero sin_zero n theorem sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := sin_antiperiodic.int_mul_eq_of_eq_zero sin_zero n theorem sin_add_nat_mul_two_pi (x : ℂ) (n : ℕ) : sin (x + n * (2 * π)) = sin x := sin_periodic.nat_mul n x theorem sin_add_int_mul_two_pi (x : ℂ) (n : ℤ) : sin (x + n * (2 * π)) = sin x := sin_periodic.int_mul n x theorem sin_sub_nat_mul_two_pi (x : ℂ) (n : ℕ) : sin (x - n * (2 * π)) = sin x := sin_periodic.sub_nat_mul_eq n theorem sin_sub_int_mul_two_pi (x : ℂ) (n : ℤ) : sin (x - n * (2 * π)) = sin x := sin_periodic.sub_int_mul_eq n theorem sin_nat_mul_two_pi_sub (x : ℂ) (n : ℕ) : sin (n * (2 * π) - x) = -sin x := sin_neg x ▸ sin_periodic.nat_mul_sub_eq n theorem sin_int_mul_two_pi_sub (x : ℂ) (n : ℤ) : sin (n * (2 * π) - x) = -sin x := sin_neg x ▸ sin_periodic.int_mul_sub_eq n theorem cos_antiperiodic : Function.Antiperiodic cos π := by simp [cos_add] theorem cos_periodic : Function.Periodic cos (2 * π) := cos_antiperiodic.periodic_two_mul theorem cos_add_pi (x : ℂ) : cos (x + π) = -cos x := cos_antiperiodic x theorem cos_add_two_pi (x : ℂ) : cos (x + 2 * π) = cos x := cos_periodic x theorem cos_sub_pi (x : ℂ) : cos (x - π) = -cos x := cos_antiperiodic.sub_eq x theorem cos_sub_two_pi (x : ℂ) : cos (x - 2 * π) = cos x := cos_periodic.sub_eq x theorem cos_pi_sub (x : ℂ) : cos (π - x) = -cos x := cos_neg x ▸ cos_antiperiodic.sub_eq' theorem cos_two_pi_sub (x : ℂ) : cos (2 * π - x) = cos x := cos_neg x ▸ cos_periodic.sub_eq' theorem cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := (cos_periodic.nat_mul_eq n).trans cos_zero theorem cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := (cos_periodic.int_mul_eq n).trans cos_zero theorem cos_add_nat_mul_two_pi (x : ℂ) (n : ℕ) : cos (x + n * (2 * π)) = cos x := cos_periodic.nat_mul n x theorem cos_add_int_mul_two_pi (x : ℂ) (n : ℤ) : cos (x + n * (2 * π)) = cos x := cos_periodic.int_mul n x theorem cos_sub_nat_mul_two_pi (x : ℂ) (n : ℕ) : cos (x - n * (2 * π)) = cos x := cos_periodic.sub_nat_mul_eq n theorem cos_sub_int_mul_two_pi (x : ℂ) (n : ℤ) : cos (x - n * (2 * π)) = cos x := cos_periodic.sub_int_mul_eq n theorem cos_nat_mul_two_pi_sub (x : ℂ) (n : ℕ) : cos (n * (2 * π) - x) = cos x := cos_neg x ▸ cos_periodic.nat_mul_sub_eq n theorem cos_int_mul_two_pi_sub (x : ℂ) (n : ℤ) : cos (n * (2 * π) - x) = cos x := cos_neg x ▸ cos_periodic.int_mul_sub_eq n theorem cos_nat_mul_two_pi_add_pi (n : ℕ) : cos (n * (2 * π) + π) = -1 := by simpa only [cos_zero] using (cos_periodic.nat_mul n).add_antiperiod_eq cos_antiperiodic theorem cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simpa only [cos_zero] using (cos_periodic.int_mul n).add_antiperiod_eq cos_antiperiodic theorem cos_nat_mul_two_pi_sub_pi (n : ℕ) : cos (n * (2 * π) - π) = -1 := by simpa only [cos_zero] using (cos_periodic.nat_mul n).sub_antiperiod_eq cos_antiperiodic theorem cos_int_mul_two_pi_sub_pi (n : ℤ) : cos (n * (2 * π) - π) = -1 := by simpa only [cos_zero] using (cos_periodic.int_mul n).sub_antiperiod_eq cos_antiperiodic theorem sin_add_pi_div_two (x : ℂ) : sin (x + π / 2) = cos x := by simp [sin_add] theorem sin_sub_pi_div_two (x : ℂ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] theorem sin_pi_div_two_sub (x : ℂ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] theorem cos_add_pi_div_two (x : ℂ) : cos (x + π / 2) = -sin x := by simp [cos_add] theorem cos_sub_pi_div_two (x : ℂ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] theorem cos_pi_div_two_sub (x : ℂ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] theorem tan_periodic : Function.Periodic tan π := by simpa only [tan_eq_sin_div_cos] using sin_antiperiodic.div cos_antiperiodic theorem tan_add_pi (x : ℂ) : tan (x + π) = tan x :=
tan_periodic x
Mathlib/Analysis/SpecialFunctions/Trigonometric/Basic.lean
1,138
1,138
/- Copyright (c) 2021 Alex Kontorovich and Heather Macbeth and Marc Masdeu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex Kontorovich, Heather Macbeth, Marc Masdeu -/ import Mathlib.Analysis.Complex.Basic import Mathlib.Data.Fintype.Parity import Mathlib.LinearAlgebra.Matrix.GeneralLinearGroup.Defs /-! # The upper half plane and its automorphisms This file defines `UpperHalfPlane` to be the upper half plane in `ℂ`. We furthermore equip it with the structure of a `GLPos 2 ℝ` action by fractional linear transformations. We define the notation `ℍ` for the upper half plane available in the locale `UpperHalfPlane` so as not to conflict with the quaternions. -/ noncomputable section open Matrix Matrix.SpecialLinearGroup open scoped MatrixGroups /-- The open upper half plane, denoted as `ℍ` within the `UpperHalfPlane` namespace -/ def UpperHalfPlane := { point : ℂ // 0 < point.im } @[inherit_doc] scoped[UpperHalfPlane] notation "ℍ" => UpperHalfPlane open UpperHalfPlane namespace UpperHalfPlane /-- The coercion first into an element of `GL(2, ℝ)⁺`, then `GL(2, ℝ)` and finally a 2 × 2 matrix. This notation is scoped in namespace `UpperHalfPlane`. -/ scoped notation:1024 "↑ₘ" A:1024 => (((A : GL(2, ℝ)⁺) : GL (Fin 2) ℝ) : Matrix (Fin 2) (Fin 2) _) instance instCoeFun : CoeFun GL(2, ℝ)⁺ fun _ => Fin 2 → Fin 2 → ℝ where coe A := ↑ₘA /-- The coercion into an element of `GL(2, R)` and finally a 2 × 2 matrix over `R`. This is similar to `↑ₘ`, but without positivity requirements, and allows the user to specify the ring `R`, which can be useful to help Lean elaborate correctly. This notation is scoped in namespace `UpperHalfPlane`. -/ scoped notation:1024 "↑ₘ[" R "]" A:1024 => ((A : GL (Fin 2) R) : Matrix (Fin 2) (Fin 2) R) /-- Canonical embedding of the upper half-plane into `ℂ`. -/ @[coe] protected def coe (z : ℍ) : ℂ := z.1 instance : CoeOut ℍ ℂ := ⟨UpperHalfPlane.coe⟩ instance : Inhabited ℍ := ⟨⟨Complex.I, by simp⟩⟩ @[ext] theorem ext {a b : ℍ} (h : (a : ℂ) = b) : a = b := Subtype.eq h @[simp, norm_cast] theorem ext_iff' {a b : ℍ} : (a : ℂ) = b ↔ a = b := UpperHalfPlane.ext_iff.symm instance canLift : CanLift ℂ ℍ ((↑) : ℍ → ℂ) fun z => 0 < z.im := Subtype.canLift fun (z : ℂ) => 0 < z.im /-- Imaginary part -/ def im (z : ℍ) := (z : ℂ).im /-- Real part -/ def re (z : ℍ) := (z : ℂ).re /-- Extensionality lemma in terms of `UpperHalfPlane.re` and `UpperHalfPlane.im`. -/ theorem ext' {a b : ℍ} (hre : a.re = b.re) (him : a.im = b.im) : a = b := ext <| Complex.ext hre him /-- Constructor for `UpperHalfPlane`. It is useful if `⟨z, h⟩` makes Lean use a wrong typeclass instance. -/ def mk (z : ℂ) (h : 0 < z.im) : ℍ := ⟨z, h⟩ @[simp] theorem coe_im (z : ℍ) : (z : ℂ).im = z.im := rfl @[simp] theorem coe_re (z : ℍ) : (z : ℂ).re = z.re := rfl @[simp] theorem mk_re (z : ℂ) (h : 0 < z.im) : (mk z h).re = z.re := rfl @[simp] theorem mk_im (z : ℂ) (h : 0 < z.im) : (mk z h).im = z.im := rfl @[simp] theorem coe_mk (z : ℂ) (h : 0 < z.im) : (mk z h : ℂ) = z := rfl @[simp] lemma coe_mk_subtype {z : ℂ} (hz : 0 < z.im) : UpperHalfPlane.coe ⟨z, hz⟩ = z := by rfl @[simp] theorem mk_coe (z : ℍ) (h : 0 < (z : ℂ).im := z.2) : mk z h = z := rfl theorem re_add_im (z : ℍ) : (z.re + z.im * Complex.I : ℂ) = z := Complex.re_add_im z theorem im_pos (z : ℍ) : 0 < z.im := z.2 theorem im_ne_zero (z : ℍ) : z.im ≠ 0 := z.im_pos.ne' theorem ne_zero (z : ℍ) : (z : ℂ) ≠ 0 := mt (congr_arg Complex.im) z.im_ne_zero /-- Define I := √-1 as an element on the upper half plane. -/ def I : ℍ := ⟨Complex.I, by simp⟩ @[simp] lemma I_im : I.im = 1 := rfl @[simp] lemma I_re : I.re = 0 := rfl @[simp, norm_cast] lemma coe_I : I = Complex.I := rfl end UpperHalfPlane namespace Mathlib.Meta.Positivity open Lean Meta Qq /-- Extension for the `positivity` tactic: `UpperHalfPlane.im`. -/ @[positivity UpperHalfPlane.im _] def evalUpperHalfPlaneIm : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q(UpperHalfPlane.im $a) => assertInstancesCommute pure (.positive q(@UpperHalfPlane.im_pos $a)) | _, _, _ => throwError "not UpperHalfPlane.im" /-- Extension for the `positivity` tactic: `UpperHalfPlane.coe`. -/ @[positivity UpperHalfPlane.coe _] def evalUpperHalfPlaneCoe : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℂ), ~q(UpperHalfPlane.coe $a) => assertInstancesCommute pure (.nonzero q(@UpperHalfPlane.ne_zero $a)) | _, _, _ => throwError "not UpperHalfPlane.coe" end Mathlib.Meta.Positivity namespace UpperHalfPlane theorem normSq_pos (z : ℍ) : 0 < Complex.normSq (z : ℂ) := by rw [Complex.normSq_pos]; exact z.ne_zero theorem normSq_ne_zero (z : ℍ) : Complex.normSq (z : ℂ) ≠ 0 := (normSq_pos z).ne' theorem im_inv_neg_coe_pos (z : ℍ) : 0 < (-z : ℂ)⁻¹.im := by simpa [neg_div] using div_pos z.property (normSq_pos z) lemma ne_nat (z : ℍ) : ∀ n : ℕ, z.1 ≠ n := by intro n have h1 := z.2 aesop lemma ne_int (z : ℍ) : ∀ n : ℤ, z.1 ≠ n := by intro n have h1 := z.2 aesop /-- Numerator of the formula for a fractional linear transformation -/ def num (g : GL(2, ℝ)⁺) (z : ℍ) : ℂ := g 0 0 * z + g 0 1 /-- Denominator of the formula for a fractional linear transformation -/ def denom (g : GL(2, ℝ)⁺) (z : ℍ) : ℂ := g 1 0 * z + g 1 1 theorem linear_ne_zero (cd : Fin 2 → ℝ) (z : ℍ) (h : cd ≠ 0) : (cd 0 : ℂ) * z + cd 1 ≠ 0 := by contrapose! h have : cd 0 = 0 := by -- we will need this twice apply_fun Complex.im at h simpa only [z.im_ne_zero, Complex.add_im, add_zero, coe_im, zero_mul, or_false, Complex.ofReal_im, Complex.zero_im, Complex.mul_im, mul_eq_zero] using h simp only [this, zero_mul, Complex.ofReal_zero, zero_add, Complex.ofReal_eq_zero] at h ext i fin_cases i <;> assumption theorem denom_ne_zero (g : GL(2, ℝ)⁺) (z : ℍ) : denom g z ≠ 0 := by intro H have DET := (mem_glpos _).1 g.prop simp only [GeneralLinearGroup.val_det_apply] at DET obtain hg | hz : g 1 0 = 0 ∨ z.im = 0 := by simpa [num, denom] using congr_arg Complex.im H · simp only [hg, Complex.ofReal_zero, denom, zero_mul, zero_add, Complex.ofReal_eq_zero] at H simp only [Matrix.det_fin_two g.1.1, H, hg, mul_zero, sub_zero, lt_self_iff_false] at DET · exact z.prop.ne' hz theorem normSq_denom_pos (g : GL(2, ℝ)⁺) (z : ℍ) : 0 < Complex.normSq (denom g z) := Complex.normSq_pos.mpr (denom_ne_zero g z) theorem normSq_denom_ne_zero (g : GL(2, ℝ)⁺) (z : ℍ) : Complex.normSq (denom g z) ≠ 0 := ne_of_gt (normSq_denom_pos g z) /-- Fractional linear transformation, also known as the Moebius transformation -/ def smulAux' (g : GL(2, ℝ)⁺) (z : ℍ) : ℂ := num g z / denom g z theorem smulAux'_im (g : GL(2, ℝ)⁺) (z : ℍ) : (smulAux' g z).im = det ↑ₘg * z.im / Complex.normSq (denom g z) := by simp only [smulAux', num, denom, Complex.div_im, Complex.add_im, Complex.mul_im, Complex.ofReal_re, coe_im, Complex.ofReal_im, coe_re, zero_mul, add_zero, Complex.add_re, Complex.mul_re, sub_zero, ← sub_div, g.1.1.det_fin_two] ring /-- Fractional linear transformation, also known as the Moebius transformation -/ def smulAux (g : GL(2, ℝ)⁺) (z : ℍ) : ℍ := mk (smulAux' g z) <| by rw [smulAux'_im] convert mul_pos ((mem_glpos _).1 g.prop) (div_pos z.im_pos (Complex.normSq_pos.mpr (denom_ne_zero g z))) using 1 simp only [GeneralLinearGroup.val_det_apply] ring theorem denom_cocycle (x y : GL(2, ℝ)⁺) (z : ℍ) : denom (x * y) z = denom x (smulAux y z) * denom y z := by change _ = (_ * (_ / _) + _) * _ field_simp [denom_ne_zero] simp only [denom, Subgroup.coe_mul, Fin.isValue, Units.val_mul, mul_apply, Fin.sum_univ_succ, Finset.univ_unique, Fin.default_eq_zero, Finset.sum_singleton, Fin.succ_zero_eq_one, Complex.ofReal_add, Complex.ofReal_mul, num] ring theorem mul_smul' (x y : GL(2, ℝ)⁺) (z : ℍ) : smulAux (x * y) z = smulAux x (smulAux y z) := by ext1 change _ / _ = (_ * (_ / _) + _) / _ rw [denom_cocycle] field_simp [denom_ne_zero] simp only [num, Subgroup.coe_mul, Fin.isValue, Units.val_mul, mul_apply, Fin.sum_univ_succ, Finset.univ_unique, Fin.default_eq_zero, Finset.sum_singleton, Fin.succ_zero_eq_one, Complex.ofReal_add, Complex.ofReal_mul, denom] ring /-- The action of `GLPos 2 ℝ` on the upper half-plane by fractional linear transformations. -/ instance : MulAction GL(2, ℝ)⁺ ℍ where smul := smulAux one_smul z := by ext1 change _ / _ = _ simp [num, denom] mul_smul := mul_smul' instance SLAction {R : Type*} [CommRing R] [Algebra R ℝ] : MulAction SL(2, R) ℍ := MulAction.compHom ℍ <| SpecialLinearGroup.toGLPos.comp <| map (algebraMap R ℝ) -- Porting note: in the statement, we used to have coercions `↑· : ℝ` -- rather than `algebraMap R ℝ ·`. theorem specialLinearGroup_apply {R : Type*} [CommRing R] [Algebra R ℝ] (g : SL(2, R)) (z : ℍ) : g • z = mk (((algebraMap R ℝ (g 0 0) : ℂ) * z + (algebraMap R ℝ (g 0 1) : ℂ)) / ((algebraMap R ℝ (g 1 0) : ℂ) * z + (algebraMap R ℝ (g 1 1) : ℂ))) (g • z).property := rfl variable (g : GL(2, ℝ)⁺) (z : ℍ) @[simp] theorem coe_smul : ↑(g • z) = num g z / denom g z := rfl @[simp] theorem re_smul : (g • z).re = (num g z / denom g z).re := rfl theorem im_smul : (g • z).im = (num g z / denom g z).im := rfl theorem im_smul_eq_div_normSq : (g • z).im = det ↑ₘg * z.im / Complex.normSq (denom g z) := smulAux'_im g z theorem c_mul_im_sq_le_normSq_denom : (g 1 0 * z.im) ^ 2 ≤ Complex.normSq (denom g z) := by set c := g 1 0 set d := g 1 1 calc (c * z.im) ^ 2 ≤ (c * z.im) ^ 2 + (c * z.re + d) ^ 2 := by nlinarith _ = Complex.normSq (denom g z) := by dsimp [c, d, denom, Complex.normSq]; ring @[simp] theorem neg_smul : -g • z = g • z := by ext1 change _ / _ = _ / _ field_simp [denom_ne_zero] simp only [num, denom, Complex.ofReal_neg, neg_mul, GLPos.coe_neg_GL, Units.val_neg, neg_apply] ring_nf lemma denom_one : denom 1 z = 1 := by simp [denom] section PosRealAction instance posRealAction : MulAction { x : ℝ // 0 < x } ℍ where smul x z := mk ((x : ℝ) • (z : ℂ)) <| by simpa using mul_pos x.2 z.2 one_smul _ := Subtype.ext <| one_smul _ _ mul_smul x y z := Subtype.ext <| mul_smul (x : ℝ) y (z : ℂ) variable (x : { x : ℝ // 0 < x }) (z : ℍ) @[simp] theorem coe_pos_real_smul : ↑(x • z) = (x : ℝ) • (z : ℂ) := rfl @[simp] theorem pos_real_im : (x • z).im = x * z.im := Complex.smul_im _ _ @[simp] theorem pos_real_re : (x • z).re = x * z.re := Complex.smul_re _ _ end PosRealAction section RealAddAction instance : AddAction ℝ ℍ where vadd x z := mk (x + z) <| by simpa using z.im_pos zero_vadd _ := Subtype.ext <| by simp [HVAdd.hVAdd] add_vadd x y z := Subtype.ext <| by simp [HVAdd.hVAdd, add_assoc] variable (x : ℝ) (z : ℍ) @[simp] theorem coe_vadd : ↑(x +ᵥ z) = (x + z : ℂ) := rfl @[simp] theorem vadd_re : (x +ᵥ z).re = x + z.re := rfl @[simp] theorem vadd_im : (x +ᵥ z).im = z.im := zero_add _ end RealAddAction /- these next few lemmas are *not* flagged `@simp` because of the constructors on the RHS; instead we use the versions with coercions to `ℂ` as simp lemmas instead. -/ theorem modular_S_smul (z : ℍ) : ModularGroup.S • z = mk (-z : ℂ)⁻¹ z.im_inv_neg_coe_pos := by rw [specialLinearGroup_apply]; simp [ModularGroup.S, neg_div, inv_neg, toGL] theorem modular_T_zpow_smul (z : ℍ) (n : ℤ) : ModularGroup.T ^ n • z = (n : ℝ) +ᵥ z := by rw [UpperHalfPlane.ext_iff, coe_vadd, add_comm, specialLinearGroup_apply, coe_mk] simp [toGL, ModularGroup.coe_T_zpow, of_apply, cons_val_zero, algebraMap.coe_one, Complex.ofReal_one, one_mul, cons_val_one, head_cons, algebraMap.coe_zero, zero_mul, zero_add, div_one] theorem modular_T_smul (z : ℍ) : ModularGroup.T • z = (1 : ℝ) +ᵥ z := by simpa only [Int.cast_one] using modular_T_zpow_smul z 1 theorem exists_SL2_smul_eq_of_apply_zero_one_eq_zero (g : SL(2, ℝ)) (hc : g 1 0 = 0) : ∃ (u : { x : ℝ // 0 < x }) (v : ℝ), (g • · : ℍ → ℍ) = (v +ᵥ ·) ∘ (u • ·) := by obtain ⟨a, b, ha, rfl⟩ := g.fin_two_exists_eq_mk_of_apply_zero_one_eq_zero hc refine ⟨⟨_, mul_self_pos.mpr ha⟩, b * a, ?_⟩ ext1 ⟨z, hz⟩; ext1 suffices ↑a * z * a + b * a = b * a + a * a * z by simpa [toGL, specialLinearGroup_apply, add_mul] ring theorem exists_SL2_smul_eq_of_apply_zero_one_ne_zero (g : SL(2, ℝ)) (hc : g 1 0 ≠ 0) : ∃ (u : { x : ℝ // 0 < x }) (v w : ℝ), (g • · : ℍ → ℍ) = (w +ᵥ ·) ∘ (ModularGroup.S • · : ℍ → ℍ) ∘ (v +ᵥ · : ℍ → ℍ) ∘ (u • · : ℍ → ℍ) := by have h_denom := denom_ne_zero g induction g using Matrix.SpecialLinearGroup.fin_two_induction with | _ a b c d h => ?_ replace hc : c ≠ 0 := by simpa using hc refine ⟨⟨_, mul_self_pos.mpr hc⟩, c * d, a / c, ?_⟩ ext1 ⟨z, hz⟩; ext1 suffices (↑a * z + b) / (↑c * z + d) = a / c - (c * d + ↑c * ↑c * z)⁻¹ by simpa only [modular_S_smul, inv_neg, Function.comp_apply, coe_vadd, Complex.ofReal_mul, coe_pos_real_smul, Complex.real_smul, Complex.ofReal_div, coe_mk] replace hc : (c : ℂ) ≠ 0 := by norm_cast replace h_denom : ↑c * z + d ≠ 0 := by simpa using h_denom ⟨z, hz⟩ have h_aux : (c : ℂ) * d + ↑c * ↑c * z ≠ 0 := by rw [mul_assoc, ← mul_add, add_comm] exact mul_ne_zero hc h_denom replace h : (a * d - b * c : ℂ) = (1 : ℂ) := by norm_cast field_simp linear_combination (-(z * (c : ℂ) ^ 2) - c * d) * h end UpperHalfPlane namespace ModularGroup -- results specific to `SL(2, ℤ)` section ModularScalarTowers /-- Canonical embedding of `SL(2, ℤ)` into `GL(2, ℝ)⁺`. -/ @[coe] def coe (g : SL(2, ℤ)) : GL(2, ℝ)⁺ := ((g : SL(2, ℝ)) : GL(2, ℝ)⁺) @[deprecated (since := "2024-11-19")] noncomputable alias coe' := coe instance : Coe SL(2, ℤ) GL(2, ℝ)⁺ := ⟨coe⟩ @[simp] theorem coe_apply_complex {g : SL(2, ℤ)} {i j : Fin 2} : (Units.val <| Subtype.val <| coe g) i j = (Subtype.val g i j : ℂ) := rfl @[deprecated (since := "2024-11-19")] alias coe'_apply_complex := coe_apply_complex @[simp] theorem det_coe {g : SL(2, ℤ)} : det (Units.val <| Subtype.val <| coe g) = 1 := by simp only [SpecialLinearGroup.coe_GLPos_coe_GL_coe_matrix, SpecialLinearGroup.det_coe, coe] @[deprecated (since := "2024-11-19")] alias det_coe' := det_coe lemma coe_one : coe 1 = 1 := by simp only [coe, map_one] instance SLOnGLPos : SMul SL(2, ℤ) GL(2, ℝ)⁺ := ⟨fun s g => s * g⟩ theorem SLOnGLPos_smul_apply (s : SL(2, ℤ)) (g : GL(2, ℝ)⁺) (z : ℍ) : (s • g) • z = ((s : GL(2, ℝ)⁺) * g) • z :=
rfl
Mathlib/Analysis/Complex/UpperHalfPlane/Basic.lean
440
441
/- Copyright (c) 2020 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.LinearAlgebra.AffineSpace.AffineMap import Mathlib.Topology.Algebra.MulAction import Mathlib.Topology.Algebra.Group.Defs /-! # Topological properties of affine spaces and maps For now, this contains only a few facts regarding the continuity of affine maps in the special case when the point space and vector space are the same. TODO: Deal with the case where the point spaces are different from the vector spaces. Note that we do have some results in this direction under the assumption that the topologies are induced by (semi)norms. -/ namespace AffineMap variable {R E F : Type*} variable [AddCommGroup E] [TopologicalSpace E] variable [AddCommGroup F] [TopologicalSpace F] [IsTopologicalAddGroup F] section Ring variable [Ring R] [Module R E] [Module R F] /-- An affine map is continuous iff its underlying linear map is continuous. See also `AffineMap.continuous_linear_iff`. -/ theorem continuous_iff {f : E →ᵃ[R] F} : Continuous f ↔ Continuous f.linear := by constructor · intro hc rw [decomp' f] exact hc.sub continuous_const · intro hc rw [decomp f] exact hc.add continuous_const /-- The line map is continuous. -/ @[continuity] theorem lineMap_continuous [TopologicalSpace R] [ContinuousSMul R F] {p v : F} : Continuous (lineMap p v : R →ᵃ[R] F) := continuous_iff.mpr <| (continuous_id.smul continuous_const).add <| @continuous_const _ _ _ _ (0 : F) end Ring section CommRing variable [CommRing R] [Module R F] [ContinuousConstSMul R F] @[continuity] theorem homothety_continuous (x : F) (t : R) : Continuous <| homothety x t := by suffices ⇑(homothety x t) = fun y => t • (y - x) + x by rw [this] fun_prop ext y simp [homothety_apply] end CommRing section Field variable [Field R] [Module R F] [ContinuousConstSMul R F] theorem homothety_isOpenMap (x : F) (t : R) (ht : t ≠ 0) : IsOpenMap <| homothety x t := by apply IsOpenMap.of_inverse (homothety_continuous x t⁻¹) <;> intro e <;> simp [← AffineMap.comp_apply, ← homothety_mul, ht] end Field
end AffineMap
Mathlib/Topology/Algebra/Affine.lean
76
78
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import Mathlib.Analysis.InnerProductSpace.Projection import Mathlib.MeasureTheory.Function.ConditionalExpectation.Unique import Mathlib.MeasureTheory.Function.L2Space /-! # Conditional expectation in L2 This file contains one step of the construction of the conditional expectation, which is completed in `MeasureTheory.Function.ConditionalExpectation.Basic`. See that file for a description of the full process. We build the conditional expectation of an `L²` function, as an element of `L²`. This is the orthogonal projection on the subspace of almost everywhere `m`-measurable functions. ## Main definitions * `condExpL2`: Conditional expectation of a function in L2 with respect to a sigma-algebra: it is the orthogonal projection on the subspace `lpMeas`. ## Implementation notes Most of the results in this file are valid for a complete real normed space `F`. However, some lemmas also use `𝕜 : RCLike`: * `condExpL2` is defined only for an `InnerProductSpace` for now, and we use `𝕜` for its field. * results about scalar multiplication are stated not only for `ℝ` but also for `𝕜` if we happen to have `NormedSpace 𝕜 F`. -/ open TopologicalSpace Filter ContinuousLinearMap open scoped ENNReal Topology MeasureTheory namespace MeasureTheory variable {α E E' F G G' 𝕜 : Type*} [RCLike 𝕜] -- 𝕜 for ℝ or ℂ -- E for an inner product space [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] [CompleteSpace E] -- E' for an inner product space on which we compute integrals [NormedAddCommGroup E'] [InnerProductSpace 𝕜 E'] [CompleteSpace E'] [NormedSpace ℝ E'] -- F for a Lp submodule [NormedAddCommGroup F] [NormedSpace 𝕜 F] -- G for a Lp add_subgroup [NormedAddCommGroup G] -- G' for integrals on a Lp add_subgroup [NormedAddCommGroup G'] [NormedSpace ℝ G'] [CompleteSpace G'] variable {m m0 : MeasurableSpace α} {μ : Measure α} {s t : Set α} local notation "⟪" x ", " y "⟫" => @inner 𝕜 E _ x y local notation "⟪" x ", " y "⟫₂" => @inner 𝕜 (α →₂[μ] E) _ x y variable (E 𝕜) /-- Conditional expectation of a function in L2 with respect to a sigma-algebra -/ noncomputable def condExpL2 (hm : m ≤ m0) : (α →₂[μ] E) →L[𝕜] lpMeas E 𝕜 m 2 μ := haveI : Fact (m ≤ m0) := ⟨hm⟩ (lpMeas E 𝕜 m 2 μ).orthogonalProjection @[deprecated (since := "2025-01-21")] alias condexpL2 := condExpL2 variable {E 𝕜} theorem aestronglyMeasurable_condExpL2 (hm : m ≤ m0) (f : α →₂[μ] E) : AEStronglyMeasurable[m] (condExpL2 E 𝕜 hm f : α → E) μ := lpMeas.aestronglyMeasurable _ @[deprecated (since := "2025-01-24")] alias aeStronglyMeasurable'_condExpL2 := aestronglyMeasurable_condExpL2 @[deprecated (since := "2025-01-24")] alias aeStronglyMeasurable'_condexpL2 := aestronglyMeasurable_condExpL2 theorem integrableOn_condExpL2_of_measure_ne_top (hm : m ≤ m0) (hμs : μ s ≠ ∞) (f : α →₂[μ] E) : IntegrableOn (ε := E) (condExpL2 E 𝕜 hm f) s μ := integrableOn_Lp_of_measure_ne_top (condExpL2 E 𝕜 hm f : α →₂[μ] E) fact_one_le_two_ennreal.elim hμs @[deprecated (since := "2025-01-21")] alias integrableOn_condexpL2_of_measure_ne_top := integrableOn_condExpL2_of_measure_ne_top theorem integrable_condExpL2_of_isFiniteMeasure (hm : m ≤ m0) [IsFiniteMeasure μ] {f : α →₂[μ] E} : Integrable (ε := E) (condExpL2 E 𝕜 hm f) μ := integrableOn_univ.mp <| integrableOn_condExpL2_of_measure_ne_top hm (measure_ne_top _ _) f @[deprecated (since := "2025-01-21")] alias integrable_condexpL2_of_isFiniteMeasure := integrable_condExpL2_of_isFiniteMeasure theorem norm_condExpL2_le_one (hm : m ≤ m0) : ‖@condExpL2 α E 𝕜 _ _ _ _ _ _ μ hm‖ ≤ 1 := haveI : Fact (m ≤ m0) := ⟨hm⟩ Submodule.orthogonalProjection_norm_le _ @[deprecated (since := "2025-01-21")] alias norm_condexpL2_le_one := norm_condExpL2_le_one theorem norm_condExpL2_le (hm : m ≤ m0) (f : α →₂[μ] E) : ‖condExpL2 E 𝕜 hm f‖ ≤ ‖f‖ := ((@condExpL2 _ E 𝕜 _ _ _ _ _ _ μ hm).le_opNorm f).trans (mul_le_of_le_one_left (norm_nonneg _) (norm_condExpL2_le_one hm)) @[deprecated (since := "2025-01-21")] alias norm_condexpL2_le := norm_condExpL2_le theorem eLpNorm_condExpL2_le (hm : m ≤ m0) (f : α →₂[μ] E) : eLpNorm (ε := E) (condExpL2 E 𝕜 hm f) 2 μ ≤ eLpNorm f 2 μ := by rw [← ENNReal.toReal_le_toReal (Lp.eLpNorm_ne_top _) (Lp.eLpNorm_ne_top _), ← Lp.norm_def, ← Lp.norm_def, Submodule.norm_coe] exact norm_condExpL2_le hm f @[deprecated (since := "2025-01-21")] alias eLpNorm_condexpL2_le := eLpNorm_condExpL2_le theorem norm_condExpL2_coe_le (hm : m ≤ m0) (f : α →₂[μ] E) : ‖(condExpL2 E 𝕜 hm f : α →₂[μ] E)‖ ≤ ‖f‖ := by rw [Lp.norm_def, Lp.norm_def] exact ENNReal.toReal_mono (Lp.eLpNorm_ne_top _) (eLpNorm_condExpL2_le hm f) @[deprecated (since := "2025-01-21")] alias norm_condexpL2_coe_le := norm_condExpL2_coe_le theorem inner_condExpL2_left_eq_right (hm : m ≤ m0) {f g : α →₂[μ] E} : ⟪(condExpL2 E 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, (condExpL2 E 𝕜 hm g : α →₂[μ] E)⟫₂ := haveI : Fact (m ≤ m0) := ⟨hm⟩ Submodule.inner_orthogonalProjection_left_eq_right _ f g @[deprecated (since := "2025-01-21")] alias inner_condexpL2_left_eq_right := inner_condExpL2_left_eq_right theorem condExpL2_indicator_of_measurable (hm : m ≤ m0) (hs : MeasurableSet[m] s) (hμs : μ s ≠ ∞) (c : E) : (condExpL2 E 𝕜 hm (indicatorConstLp 2 (hm s hs) hμs c) : α →₂[μ] E) = indicatorConstLp 2 (hm s hs) hμs c := by rw [condExpL2] haveI : Fact (m ≤ m0) := ⟨hm⟩ have h_mem : indicatorConstLp 2 (hm s hs) hμs c ∈ lpMeas E 𝕜 m 2 μ := mem_lpMeas_indicatorConstLp hm hs hμs let ind := (⟨indicatorConstLp 2 (hm s hs) hμs c, h_mem⟩ : lpMeas E 𝕜 m 2 μ) have h_coe_ind : (ind : α →₂[μ] E) = indicatorConstLp 2 (hm s hs) hμs c := rfl have h_orth_mem := Submodule.orthogonalProjection_mem_subspace_eq_self ind rw [← h_coe_ind, h_orth_mem] @[deprecated (since := "2025-01-21")] alias condexpL2_indicator_of_measurable := condExpL2_indicator_of_measurable theorem inner_condExpL2_eq_inner_fun (hm : m ≤ m0) (f g : α →₂[μ] E) (hg : AEStronglyMeasurable[m] g μ) : ⟪(condExpL2 E 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, g⟫₂ := by symm rw [← sub_eq_zero, ← inner_sub_left, condExpL2] simp only [mem_lpMeas_iff_aestronglyMeasurable.mpr hg, Submodule.orthogonalProjection_inner_eq_zero f g] @[deprecated (since := "2025-01-21")] alias inner_condexpL2_eq_inner_fun := inner_condExpL2_eq_inner_fun
section Real variable {hm : m ≤ m0} theorem integral_condExpL2_eq_of_fin_meas_real (f : Lp 𝕜 2 μ) (hs : MeasurableSet[m] s) (hμs : μ s ≠ ∞) : ∫ x in s, (condExpL2 𝕜 𝕜 hm f : α → 𝕜) x ∂μ = ∫ x in s, f x ∂μ := by rw [← L2.inner_indicatorConstLp_one (𝕜 := 𝕜) (hm s hs) hμs f] have h_eq_inner : ∫ x in s, (condExpL2 𝕜 𝕜 hm f : α → 𝕜) x ∂μ = inner (indicatorConstLp 2 (hm s hs) hμs (1 : 𝕜)) (condExpL2 𝕜 𝕜 hm f) := by rw [L2.inner_indicatorConstLp_one (hm s hs) hμs] rw [h_eq_inner, ← inner_condExpL2_left_eq_right, condExpL2_indicator_of_measurable hm hs hμs] @[deprecated (since := "2025-01-21")] alias integral_condexpL2_eq_of_fin_meas_real := integral_condExpL2_eq_of_fin_meas_real theorem lintegral_nnnorm_condExpL2_le (hs : MeasurableSet[m] s) (hμs : μ s ≠ ∞) (f : Lp ℝ 2 μ) : ∫⁻ x in s, ‖(condExpL2 ℝ ℝ hm f : α → ℝ) x‖₊ ∂μ ≤ ∫⁻ x in s, ‖f x‖₊ ∂μ := by let h_meas := lpMeas.aestronglyMeasurable (condExpL2 ℝ ℝ hm f) let g := h_meas.choose have hg_meas : StronglyMeasurable[m] g := h_meas.choose_spec.1 have hg_eq : g =ᵐ[μ] condExpL2 ℝ ℝ hm f := h_meas.choose_spec.2.symm
Mathlib/MeasureTheory/Function/ConditionalExpectation/CondexpL2.lean
161
182
/- Copyright (c) 2022 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import Mathlib.Algebra.BigOperators.Group.List.Basic import Mathlib.Algebra.Group.Embedding import Mathlib.Algebra.Group.Nat.Defs import Mathlib.Data.Finsupp.Single import Mathlib.Data.List.GetD /-! # Lists as finsupp ## Main definitions - `List.toFinsupp`: Interpret a list as a finitely supported function, where the indexing type is `ℕ`, and the values are either the elements of the list (accessing by indexing) or `0` outside of the list. ## Main theorems - `List.toFinsupp_eq_sum_map_enum_single`: A `l : List M` over `M` an `AddMonoid`, when interpreted as a finitely supported function, is equal to the sum of `Finsupp.single` produced by mapping over `List.enum l`. ## Implementation details The functions defined here rely on a decidability predicate that each element in the list can be decidably determined to be not equal to zero or that one can decide one is out of the bounds of a list. For concretely defined lists that are made up of elements of decidable terms, this holds. More work will be needed to support lists over non-dec-eq types like `ℝ`, where the elements are beyond the dec-eq terms of casted values from `ℕ, ℤ, ℚ`. -/ namespace List variable {M : Type*} [Zero M] (l : List M) [DecidablePred (getD l · 0 ≠ 0)] (n : ℕ) /-- Indexing into a `l : List M`, as a finitely-supported function, where the support are all the indices within the length of the list that index to a non-zero value. Indices beyond the end of the list are sent to 0. This is a computable version of the `Finsupp.onFinset` construction. -/ def toFinsupp : ℕ →₀ M where toFun i := getD l i 0 support := {i ∈ Finset.range l.length | getD l i 0 ≠ 0} mem_support_toFun n := by simp only [Ne, Finset.mem_filter, Finset.mem_range, and_iff_right_iff_imp] contrapose! exact getD_eq_default _ _ @[norm_cast] theorem coe_toFinsupp : (l.toFinsupp : ℕ → M) = (l.getD · 0) := rfl @[simp, norm_cast] theorem toFinsupp_apply (i : ℕ) : (l.toFinsupp : ℕ → M) i = l.getD i 0 := rfl theorem toFinsupp_support : l.toFinsupp.support = {i ∈ Finset.range l.length | getD l i 0 ≠ 0} := rfl theorem toFinsupp_apply_lt (hn : n < l.length) : l.toFinsupp n = l[n] := getD_eq_getElem _ _ hn theorem toFinsupp_apply_fin (n : Fin l.length) : l.toFinsupp n = l[n] := getD_eq_getElem _ _ n.isLt theorem toFinsupp_apply_le (hn : l.length ≤ n) : l.toFinsupp n = 0 := getD_eq_default _ _ hn @[simp] theorem toFinsupp_nil [DecidablePred fun i => getD ([] : List M) i 0 ≠ 0] : toFinsupp ([] : List M) = 0 := by ext simp theorem toFinsupp_singleton (x : M) [DecidablePred (getD [x] · 0 ≠ 0)] : toFinsupp [x] = Finsupp.single 0 x := by ext ⟨_ | i⟩ <;> simp [Finsupp.single_apply, (Nat.zero_lt_succ _).ne] theorem toFinsupp_append {R : Type*} [AddZeroClass R] (l₁ l₂ : List R) [DecidablePred (getD (l₁ ++ l₂) · 0 ≠ 0)] [DecidablePred (getD l₁ · 0 ≠ 0)] [DecidablePred (getD l₂ · 0 ≠ 0)] : toFinsupp (l₁ ++ l₂) = toFinsupp l₁ + (toFinsupp l₂).embDomain (addLeftEmbedding l₁.length) := by ext n simp only [toFinsupp_apply, Finsupp.add_apply] cases lt_or_le n l₁.length with | inl h => rw [getD_append _ _ _ _ h, Finsupp.embDomain_notin_range, add_zero] rintro ⟨k, rfl : length l₁ + k = n⟩ omega | inr h => rcases Nat.exists_eq_add_of_le h with ⟨k, rfl⟩ rw [getD_append_right _ _ _ _ h, Nat.add_sub_cancel_left, getD_eq_default _ _ h, zero_add] exact Eq.symm (Finsupp.embDomain_apply _ _ _) theorem toFinsupp_cons_eq_single_add_embDomain {R : Type*} [AddZeroClass R] (x : R) (xs : List R) [DecidablePred (getD (x::xs) · 0 ≠ 0)] [DecidablePred (getD xs · 0 ≠ 0)] : toFinsupp (x::xs) = Finsupp.single 0 x + (toFinsupp xs).embDomain ⟨Nat.succ, Nat.succ_injective⟩ := by classical convert toFinsupp_append [x] xs using 3 · exact (toFinsupp_singleton x).symm · ext n exact add_comm n 1 theorem toFinsupp_concat_eq_toFinsupp_add_single {R : Type*} [AddZeroClass R] (x : R) (xs : List R) [DecidablePred fun i => getD (xs ++ [x]) i 0 ≠ 0] [DecidablePred fun i => getD xs i 0 ≠ 0] : toFinsupp (xs ++ [x]) = toFinsupp xs + Finsupp.single xs.length x := by classical rw [toFinsupp_append, toFinsupp_singleton, Finsupp.embDomain_single, addLeftEmbedding_apply, add_zero] theorem toFinsupp_eq_sum_mapIdx_single {R : Type*} [AddMonoid R] (l : List R) [DecidablePred (getD l · 0 ≠ 0)] : toFinsupp l = (l.mapIdx fun n r => Finsupp.single n r).sum := by /- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: `induction` fails to substitute `l = []` in `[DecidablePred (getD l · 0 ≠ 0)]`, so we manually do some `revert`/`intro` as a workaround -/ revert l; intro l induction l using List.reverseRecOn with | nil => exact toFinsupp_nil | append_singleton x xs ih => classical simp [toFinsupp_concat_eq_toFinsupp_add_single, ih] @[deprecated (since := "2025-01-28")] alias toFinsupp_eq_sum_map_enum_single := toFinsupp_eq_sum_mapIdx_single end List
Mathlib/Data/List/ToFinsupp.lean
139
143
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.Analysis.Calculus.Deriv.Inv import Mathlib.Analysis.Calculus.Deriv.MeanValue /-! # L'Hôpital's rule for 0/0 indeterminate forms In this file, we prove several forms of "L'Hôpital's rule" for computing 0/0 indeterminate forms. The proof of `HasDerivAt.lhopital_zero_right_on_Ioo` is based on the one given in the corresponding [Wikibooks](https://en.wikibooks.org/wiki/Calculus/L%27H%C3%B4pital%27s_Rule) chapter, and all other statements are derived from this one by composing by carefully chosen functions. Note that the filter `f'/g'` tends to isn't required to be one of `𝓝 a`, `atTop` or `atBot`. In fact, we give a slightly stronger statement by allowing it to be any filter on `ℝ`. Each statement is available in a `HasDerivAt` form and a `deriv` form, which is denoted by each statement being in either the `HasDerivAt` or the `deriv` namespace. ## Tags L'Hôpital's rule, L'Hopital's rule -/ open Filter Set open scoped Filter Topology Pointwise variable {a b : ℝ} {l : Filter ℝ} {f f' g g' : ℝ → ℝ} /-! ## Interval-based versions We start by proving statements where all conditions (derivability, `g' ≠ 0`) have to be satisfied on an explicitly-provided interval. -/ namespace HasDerivAt theorem lhopital_zero_right_on_Ioo (hab : a < b) (hff' : ∀ x ∈ Ioo a b, HasDerivAt f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, HasDerivAt g (g' x) x) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfa : Tendsto f (𝓝[>] a) (𝓝 0)) (hga : Tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[>] a) l) : Tendsto (fun x => f x / g x) (𝓝[>] a) l := by have sub : ∀ x ∈ Ioo a b, Ioo a x ⊆ Ioo a b := fun x hx => Ioo_subset_Ioo (le_refl a) (le_of_lt hx.2) have hg : ∀ x ∈ Ioo a b, g x ≠ 0 := by intro x hx h have : Tendsto g (𝓝[<] x) (𝓝 0) := by rw [← h, ← nhdsWithin_Ioo_eq_nhdsLT hx.1] exact ((hgg' x hx).continuousAt.continuousWithinAt.mono <| sub x hx).tendsto obtain ⟨y, hyx, hy⟩ : ∃ c ∈ Ioo a x, g' c = 0 := exists_hasDerivAt_eq_zero' hx.1 hga this fun y hy => hgg' y <| sub x hx hy exact hg' y (sub x hx hyx) hy have : ∀ x ∈ Ioo a b, ∃ c ∈ Ioo a x, f x * g' c = g x * f' c := by intro x hx rw [← sub_zero (f x), ← sub_zero (g x)] exact exists_ratio_hasDerivAt_eq_ratio_slope' g g' hx.1 f f' (fun y hy => hgg' y <| sub x hx hy) (fun y hy => hff' y <| sub x hx hy) hga hfa (tendsto_nhdsWithin_of_tendsto_nhds (hgg' x hx).continuousAt.tendsto) (tendsto_nhdsWithin_of_tendsto_nhds (hff' x hx).continuousAt.tendsto) choose! c hc using this have : ∀ x ∈ Ioo a b, ((fun x' => f' x' / g' x') ∘ c) x = f x / g x := by intro x hx rcases hc x hx with ⟨h₁, h₂⟩ field_simp [hg x hx, hg' (c x) ((sub x hx) h₁)] simp only [h₂] rw [mul_comm] have cmp : ∀ x ∈ Ioo a b, a < c x ∧ c x < x := fun x hx => (hc x hx).1 rw [← nhdsWithin_Ioo_eq_nhdsGT hab] apply tendsto_nhdsWithin_congr this apply hdiv.comp refine tendsto_nhdsWithin_of_tendsto_nhds_of_eventually_within _ (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_nhdsWithin_of_tendsto_nhds tendsto_id) ?_ ?_) ?_ all_goals apply eventually_nhdsWithin_of_forall intro x hx have := cmp x hx try simp linarith [this] theorem lhopital_zero_right_on_Ico (hab : a < b) (hff' : ∀ x ∈ Ioo a b, HasDerivAt f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, HasDerivAt g (g' x) x) (hcf : ContinuousOn f (Ico a b)) (hcg : ContinuousOn g (Ico a b)) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfa : f a = 0) (hga : g a = 0) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[>] a) l) : Tendsto (fun x => f x / g x) (𝓝[>] a) l := by refine lhopital_zero_right_on_Ioo hab hff' hgg' hg' ?_ ?_ hdiv · rw [← hfa, ← nhdsWithin_Ioo_eq_nhdsGT hab] exact ((hcf a <| left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto · rw [← hga, ← nhdsWithin_Ioo_eq_nhdsGT hab] exact ((hcg a <| left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto theorem lhopital_zero_left_on_Ioo (hab : a < b) (hff' : ∀ x ∈ Ioo a b, HasDerivAt f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, HasDerivAt g (g' x) x) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfb : Tendsto f (𝓝[<] b) (𝓝 0)) (hgb : Tendsto g (𝓝[<] b) (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[<] b) l) : Tendsto (fun x => f x / g x) (𝓝[<] b) l := by -- Here, we essentially compose by `Neg.neg`. The following is mostly technical details. have hdnf : ∀ x ∈ -Ioo a b, HasDerivAt (f ∘ Neg.neg) (f' (-x) * -1) x := fun x hx => comp x (hff' (-x) hx) (hasDerivAt_neg x) have hdng : ∀ x ∈ -Ioo a b, HasDerivAt (g ∘ Neg.neg) (g' (-x) * -1) x := fun x hx => comp x (hgg' (-x) hx) (hasDerivAt_neg x) rw [neg_Ioo] at hdnf rw [neg_Ioo] at hdng have := lhopital_zero_right_on_Ioo (neg_lt_neg hab) hdnf hdng (by intro x hx h apply hg' _ (by rw [← neg_Ioo] at hx; exact hx) rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h) (hfb.comp tendsto_neg_nhdsGT_neg) (hgb.comp tendsto_neg_nhdsGT_neg) (by simp only [neg_div_neg_eq, mul_one, mul_neg] exact hdiv.comp tendsto_neg_nhdsGT_neg) have := this.comp tendsto_neg_nhdsLT unfold Function.comp at this simpa only [neg_neg] theorem lhopital_zero_left_on_Ioc (hab : a < b) (hff' : ∀ x ∈ Ioo a b, HasDerivAt f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, HasDerivAt g (g' x) x) (hcf : ContinuousOn f (Ioc a b)) (hcg : ContinuousOn g (Ioc a b)) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfb : f b = 0) (hgb : g b = 0) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[<] b) l) : Tendsto (fun x => f x / g x) (𝓝[<] b) l := by refine lhopital_zero_left_on_Ioo hab hff' hgg' hg' ?_ ?_ hdiv · rw [← hfb, ← nhdsWithin_Ioo_eq_nhdsLT hab] exact ((hcf b <| right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto · rw [← hgb, ← nhdsWithin_Ioo_eq_nhdsLT hab] exact ((hcg b <| right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto theorem lhopital_zero_atTop_on_Ioi (hff' : ∀ x ∈ Ioi a, HasDerivAt f (f' x) x) (hgg' : ∀ x ∈ Ioi a, HasDerivAt g (g' x) x) (hg' : ∀ x ∈ Ioi a, g' x ≠ 0) (hftop : Tendsto f atTop (𝓝 0)) (hgtop : Tendsto g atTop (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) atTop l) : Tendsto (fun x => f x / g x) atTop l := by obtain ⟨a', haa', ha'⟩ : ∃ a', a < a' ∧ 0 < a' := ⟨1 + max a 0, ⟨lt_of_le_of_lt (le_max_left a 0) (lt_one_add _), lt_of_le_of_lt (le_max_right a 0) (lt_one_add _)⟩⟩ have fact1 : ∀ x : ℝ, x ∈ Ioo 0 a'⁻¹ → x ≠ 0 := fun _ hx => (ne_of_lt hx.1).symm have fact2 (x) (hx : x ∈ Ioo 0 a'⁻¹) : a < x⁻¹ := lt_trans haa' ((lt_inv_comm₀ ha' hx.1).mpr hx.2) have hdnf : ∀ x ∈ Ioo 0 a'⁻¹, HasDerivAt (f ∘ Inv.inv) (f' x⁻¹ * -(x ^ 2)⁻¹) x := fun x hx => comp x (hff' x⁻¹ <| fact2 x hx) (hasDerivAt_inv <| fact1 x hx) have hdng : ∀ x ∈ Ioo 0 a'⁻¹, HasDerivAt (g ∘ Inv.inv) (g' x⁻¹ * -(x ^ 2)⁻¹) x := fun x hx => comp x (hgg' x⁻¹ <| fact2 x hx) (hasDerivAt_inv <| fact1 x hx) have := lhopital_zero_right_on_Ioo (inv_pos.mpr ha') hdnf hdng (by intro x hx refine mul_ne_zero ?_ (neg_ne_zero.mpr <| inv_ne_zero <| pow_ne_zero _ <| fact1 x hx) exact hg' _ (fact2 x hx)) (hftop.comp tendsto_inv_nhdsGT_zero) (hgtop.comp tendsto_inv_nhdsGT_zero) (by refine (tendsto_congr' ?_).mp (hdiv.comp tendsto_inv_nhdsGT_zero) filter_upwards [self_mem_nhdsWithin] with x (hx : 0 < x) simp only [Function.comp_def] rw [mul_div_mul_right] exact neg_ne_zero.mpr (by positivity)) have := this.comp tendsto_inv_atTop_nhdsGT_zero unfold Function.comp at this simpa only [inv_inv] theorem lhopital_zero_atBot_on_Iio (hff' : ∀ x ∈ Iio a, HasDerivAt f (f' x) x) (hgg' : ∀ x ∈ Iio a, HasDerivAt g (g' x) x) (hg' : ∀ x ∈ Iio a, g' x ≠ 0) (hfbot : Tendsto f atBot (𝓝 0)) (hgbot : Tendsto g atBot (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) atBot l) : Tendsto (fun x => f x / g x) atBot l := by -- Here, we essentially compose by `Neg.neg`. The following is mostly technical details. have hdnf : ∀ x ∈ -Iio a, HasDerivAt (f ∘ Neg.neg) (f' (-x) * -1) x := fun x hx => comp x (hff' (-x) hx) (hasDerivAt_neg x) have hdng : ∀ x ∈ -Iio a, HasDerivAt (g ∘ Neg.neg) (g' (-x) * -1) x := fun x hx => comp x (hgg' (-x) hx) (hasDerivAt_neg x) rw [neg_Iio] at hdnf rw [neg_Iio] at hdng have := lhopital_zero_atTop_on_Ioi hdnf hdng (by intro x hx h apply hg' _ (by rw [← neg_Iio] at hx; exact hx) rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h) (hfbot.comp tendsto_neg_atTop_atBot) (hgbot.comp tendsto_neg_atTop_atBot) (by simp only [mul_one, mul_neg, neg_div_neg_eq] exact (hdiv.comp tendsto_neg_atTop_atBot)) have := this.comp tendsto_neg_atBot_atTop unfold Function.comp at this simpa only [neg_neg] end HasDerivAt namespace deriv theorem lhopital_zero_right_on_Ioo (hab : a < b) (hdf : DifferentiableOn ℝ f (Ioo a b)) (hg' : ∀ x ∈ Ioo a b, deriv g x ≠ 0) (hfa : Tendsto f (𝓝[>] a) (𝓝 0)) (hga : Tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝[>] a) l) : Tendsto (fun x => f x / g x) (𝓝[>] a) l := by have hdf : ∀ x ∈ Ioo a b, DifferentiableAt ℝ f x := fun x hx => (hdf x hx).differentiableAt (Ioo_mem_nhds hx.1 hx.2) have hdg : ∀ x ∈ Ioo a b, DifferentiableAt ℝ g x := fun x hx => by_contradiction fun h => hg' x hx (deriv_zero_of_not_differentiableAt h) exact HasDerivAt.lhopital_zero_right_on_Ioo hab (fun x hx => (hdf x hx).hasDerivAt) (fun x hx => (hdg x hx).hasDerivAt) hg' hfa hga hdiv theorem lhopital_zero_right_on_Ico (hab : a < b) (hdf : DifferentiableOn ℝ f (Ioo a b)) (hcf : ContinuousOn f (Ico a b)) (hcg : ContinuousOn g (Ico a b)) (hg' : ∀ x ∈ Ioo a b, (deriv g) x ≠ 0) (hfa : f a = 0) (hga : g a = 0) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝[>] a) l) : Tendsto (fun x => f x / g x) (𝓝[>] a) l := by refine lhopital_zero_right_on_Ioo hab hdf hg' ?_ ?_ hdiv · rw [← hfa, ← nhdsWithin_Ioo_eq_nhdsGT hab] exact ((hcf a <| left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto · rw [← hga, ← nhdsWithin_Ioo_eq_nhdsGT hab] exact ((hcg a <| left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto theorem lhopital_zero_left_on_Ioo (hab : a < b) (hdf : DifferentiableOn ℝ f (Ioo a b)) (hg' : ∀ x ∈ Ioo a b, (deriv g) x ≠ 0) (hfb : Tendsto f (𝓝[<] b) (𝓝 0)) (hgb : Tendsto g (𝓝[<] b) (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝[<] b) l) : Tendsto (fun x => f x / g x) (𝓝[<] b) l := by have hdf : ∀ x ∈ Ioo a b, DifferentiableAt ℝ f x := fun x hx => (hdf x hx).differentiableAt (Ioo_mem_nhds hx.1 hx.2) have hdg : ∀ x ∈ Ioo a b, DifferentiableAt ℝ g x := fun x hx => by_contradiction fun h => hg' x hx (deriv_zero_of_not_differentiableAt h) exact HasDerivAt.lhopital_zero_left_on_Ioo hab (fun x hx => (hdf x hx).hasDerivAt) (fun x hx => (hdg x hx).hasDerivAt) hg' hfb hgb hdiv theorem lhopital_zero_atTop_on_Ioi (hdf : DifferentiableOn ℝ f (Ioi a)) (hg' : ∀ x ∈ Ioi a, (deriv g) x ≠ 0) (hftop : Tendsto f atTop (𝓝 0)) (hgtop : Tendsto g atTop (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) atTop l) : Tendsto (fun x => f x / g x) atTop l := by have hdf : ∀ x ∈ Ioi a, DifferentiableAt ℝ f x := fun x hx => (hdf x hx).differentiableAt (Ioi_mem_nhds hx) have hdg : ∀ x ∈ Ioi a, DifferentiableAt ℝ g x := fun x hx => by_contradiction fun h => hg' x hx (deriv_zero_of_not_differentiableAt h) exact HasDerivAt.lhopital_zero_atTop_on_Ioi (fun x hx => (hdf x hx).hasDerivAt) (fun x hx => (hdg x hx).hasDerivAt) hg' hftop hgtop hdiv theorem lhopital_zero_atBot_on_Iio (hdf : DifferentiableOn ℝ f (Iio a)) (hg' : ∀ x ∈ Iio a, (deriv g) x ≠ 0) (hfbot : Tendsto f atBot (𝓝 0)) (hgbot : Tendsto g atBot (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) atBot l) : Tendsto (fun x => f x / g x) atBot l := by have hdf : ∀ x ∈ Iio a, DifferentiableAt ℝ f x := fun x hx => (hdf x hx).differentiableAt (Iio_mem_nhds hx) have hdg : ∀ x ∈ Iio a, DifferentiableAt ℝ g x := fun x hx => by_contradiction fun h => hg' x hx (deriv_zero_of_not_differentiableAt h) exact HasDerivAt.lhopital_zero_atBot_on_Iio (fun x hx => (hdf x hx).hasDerivAt) (fun x hx => (hdg x hx).hasDerivAt) hg' hfbot hgbot hdiv end deriv /-! ## Generic versions The following statements no longer any explicit interval, as they only require conditions holding eventually. -/ namespace HasDerivAt /-- L'Hôpital's rule for approaching a real from the right, `HasDerivAt` version -/ theorem lhopital_zero_nhdsGT (hff' : ∀ᶠ x in 𝓝[>] a, HasDerivAt f (f' x) x) (hgg' : ∀ᶠ x in 𝓝[>] a, HasDerivAt g (g' x) x) (hg' : ∀ᶠ x in 𝓝[>] a, g' x ≠ 0) (hfa : Tendsto f (𝓝[>] a) (𝓝 0)) (hga : Tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[>] a) l) : Tendsto (fun x => f x / g x) (𝓝[>] a) l := by rw [eventually_iff_exists_mem] at * rcases hff' with ⟨s₁, hs₁, hff'⟩ rcases hgg' with ⟨s₂, hs₂, hgg'⟩ rcases hg' with ⟨s₃, hs₃, hg'⟩ let s := s₁ ∩ s₂ ∩ s₃ have hs : s ∈ 𝓝[>] a := inter_mem (inter_mem hs₁ hs₂) hs₃ rw [mem_nhdsGT_iff_exists_Ioo_subset] at hs rcases hs with ⟨u, hau, hu⟩ refine lhopital_zero_right_on_Ioo hau ?_ ?_ ?_ hfa hga hdiv <;> intro x hx <;> apply_assumption <;> first | exact (hu hx).1.1 | exact (hu hx).1.2 | exact (hu hx).2 @[deprecated (since := "2025-03-02")] alias lhopital_zero_nhds_right := lhopital_zero_nhdsGT /-- L'Hôpital's rule for approaching a real from the left, `HasDerivAt` version -/ theorem lhopital_zero_nhdsLT (hff' : ∀ᶠ x in 𝓝[<] a, HasDerivAt f (f' x) x) (hgg' : ∀ᶠ x in 𝓝[<] a, HasDerivAt g (g' x) x) (hg' : ∀ᶠ x in 𝓝[<] a, g' x ≠ 0) (hfa : Tendsto f (𝓝[<] a) (𝓝 0)) (hga : Tendsto g (𝓝[<] a) (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[<] a) l) : Tendsto (fun x => f x / g x) (𝓝[<] a) l := by rw [eventually_iff_exists_mem] at * rcases hff' with ⟨s₁, hs₁, hff'⟩ rcases hgg' with ⟨s₂, hs₂, hgg'⟩ rcases hg' with ⟨s₃, hs₃, hg'⟩ let s := s₁ ∩ s₂ ∩ s₃ have hs : s ∈ 𝓝[<] a := inter_mem (inter_mem hs₁ hs₂) hs₃ rw [mem_nhdsLT_iff_exists_Ioo_subset] at hs rcases hs with ⟨l, hal, hl⟩ refine lhopital_zero_left_on_Ioo hal ?_ ?_ ?_ hfa hga hdiv <;> intro x hx <;> apply_assumption <;> first | exact (hl hx).1.1| exact (hl hx).1.2| exact (hl hx).2 @[deprecated (since := "2025-03-02")] alias lhopital_zero_nhds_left := lhopital_zero_nhdsLT /-- L'Hôpital's rule for approaching a real, `HasDerivAt` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhdsNE (hff' : ∀ᶠ x in 𝓝[≠] a, HasDerivAt f (f' x) x) (hgg' : ∀ᶠ x in 𝓝[≠] a, HasDerivAt g (g' x) x) (hg' : ∀ᶠ x in 𝓝[≠] a, g' x ≠ 0) (hfa : Tendsto f (𝓝[≠] a) (𝓝 0)) (hga : Tendsto g (𝓝[≠] a) (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝[≠] a) l) : Tendsto (fun x => f x / g x) (𝓝[≠] a) l := by simp only [← Iio_union_Ioi, nhdsWithin_union, tendsto_sup, eventually_sup] at * exact ⟨lhopital_zero_nhdsLT hff'.1 hgg'.1 hg'.1 hfa.1 hga.1 hdiv.1, lhopital_zero_nhdsGT hff'.2 hgg'.2 hg'.2 hfa.2 hga.2 hdiv.2⟩ @[deprecated (since := "2025-03-02")] alias lhopital_zero_nhds' := lhopital_zero_nhdsNE /-- **L'Hôpital's rule** for approaching a real, `HasDerivAt` version -/ theorem lhopital_zero_nhds (hff' : ∀ᶠ x in 𝓝 a, HasDerivAt f (f' x) x) (hgg' : ∀ᶠ x in 𝓝 a, HasDerivAt g (g' x) x) (hg' : ∀ᶠ x in 𝓝 a, g' x ≠ 0) (hfa : Tendsto f (𝓝 a) (𝓝 0)) (hga : Tendsto g (𝓝 a) (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) (𝓝 a) l) : Tendsto (fun x => f x / g x) (𝓝[≠] a) l := by apply @lhopital_zero_nhdsNE _ _ _ f' _ g' <;> (first | apply eventually_nhdsWithin_of_eventually_nhds | apply tendsto_nhdsWithin_of_tendsto_nhds) <;> assumption /-- L'Hôpital's rule for approaching +∞, `HasDerivAt` version -/ theorem lhopital_zero_atTop (hff' : ∀ᶠ x in atTop, HasDerivAt f (f' x) x) (hgg' : ∀ᶠ x in atTop, HasDerivAt g (g' x) x) (hg' : ∀ᶠ x in atTop, g' x ≠ 0) (hftop : Tendsto f atTop (𝓝 0)) (hgtop : Tendsto g atTop (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) atTop l) : Tendsto (fun x => f x / g x) atTop l := by rw [eventually_iff_exists_mem] at * rcases hff' with ⟨s₁, hs₁, hff'⟩ rcases hgg' with ⟨s₂, hs₂, hgg'⟩ rcases hg' with ⟨s₃, hs₃, hg'⟩ let s := s₁ ∩ s₂ ∩ s₃ have hs : s ∈ atTop := inter_mem (inter_mem hs₁ hs₂) hs₃ rw [mem_atTop_sets] at hs rcases hs with ⟨l, hl⟩ have hl' : Ioi l ⊆ s := fun x hx => hl x (le_of_lt hx) refine lhopital_zero_atTop_on_Ioi ?_ ?_ (fun x hx => hg' x <| (hl' hx).2) hftop hgtop hdiv <;> intro x hx <;> apply_assumption <;> first | exact (hl' hx).1.1| exact (hl' hx).1.2 /-- L'Hôpital's rule for approaching -∞, `HasDerivAt` version -/ theorem lhopital_zero_atBot (hff' : ∀ᶠ x in atBot, HasDerivAt f (f' x) x) (hgg' : ∀ᶠ x in atBot, HasDerivAt g (g' x) x) (hg' : ∀ᶠ x in atBot, g' x ≠ 0) (hfbot : Tendsto f atBot (𝓝 0)) (hgbot : Tendsto g atBot (𝓝 0)) (hdiv : Tendsto (fun x => f' x / g' x) atBot l) : Tendsto (fun x => f x / g x) atBot l := by rw [eventually_iff_exists_mem] at * rcases hff' with ⟨s₁, hs₁, hff'⟩ rcases hgg' with ⟨s₂, hs₂, hgg'⟩ rcases hg' with ⟨s₃, hs₃, hg'⟩ let s := s₁ ∩ s₂ ∩ s₃ have hs : s ∈ atBot := inter_mem (inter_mem hs₁ hs₂) hs₃ rw [mem_atBot_sets] at hs rcases hs with ⟨l, hl⟩ have hl' : Iio l ⊆ s := fun x hx => hl x (le_of_lt hx) refine lhopital_zero_atBot_on_Iio ?_ ?_ (fun x hx => hg' x <| (hl' hx).2) hfbot hgbot hdiv <;> intro x hx <;> apply_assumption <;> first | exact (hl' hx).1.1| exact (hl' hx).1.2 end HasDerivAt namespace deriv /-- **L'Hôpital's rule** for approaching a real from the right, `deriv` version -/ theorem lhopital_zero_nhdsGT (hdf : ∀ᶠ x in 𝓝[>] a, DifferentiableAt ℝ f x) (hg' : ∀ᶠ x in 𝓝[>] a, deriv g x ≠ 0) (hfa : Tendsto f (𝓝[>] a) (𝓝 0)) (hga : Tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝[>] a) l) : Tendsto (fun x => f x / g x) (𝓝[>] a) l := by have hdg : ∀ᶠ x in 𝓝[>] a, DifferentiableAt ℝ g x := hg'.mono fun _ hg' => by_contradiction fun h => hg' (deriv_zero_of_not_differentiableAt h) have hdf' : ∀ᶠ x in 𝓝[>] a, HasDerivAt f (deriv f x) x := hdf.mono fun _ => DifferentiableAt.hasDerivAt have hdg' : ∀ᶠ x in 𝓝[>] a, HasDerivAt g (deriv g x) x := hdg.mono fun _ => DifferentiableAt.hasDerivAt exact HasDerivAt.lhopital_zero_nhdsGT hdf' hdg' hg' hfa hga hdiv @[deprecated (since := "2025-03-02")] alias lhopital_zero_nhds_right := lhopital_zero_nhdsGT /-- **L'Hôpital's rule** for approaching a real from the left, `deriv` version -/ theorem lhopital_zero_nhdsLT (hdf : ∀ᶠ x in 𝓝[<] a, DifferentiableAt ℝ f x) (hg' : ∀ᶠ x in 𝓝[<] a, deriv g x ≠ 0) (hfa : Tendsto f (𝓝[<] a) (𝓝 0)) (hga : Tendsto g (𝓝[<] a) (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝[<] a) l) : Tendsto (fun x => f x / g x) (𝓝[<] a) l := by have hdg : ∀ᶠ x in 𝓝[<] a, DifferentiableAt ℝ g x := hg'.mono fun _ hg' => by_contradiction fun h => hg' (deriv_zero_of_not_differentiableAt h) have hdf' : ∀ᶠ x in 𝓝[<] a, HasDerivAt f (deriv f x) x := hdf.mono fun _ => DifferentiableAt.hasDerivAt have hdg' : ∀ᶠ x in 𝓝[<] a, HasDerivAt g (deriv g x) x := hdg.mono fun _ => DifferentiableAt.hasDerivAt exact HasDerivAt.lhopital_zero_nhdsLT hdf' hdg' hg' hfa hga hdiv @[deprecated (since := "2025-03-02")] alias lhopital_zero_nhds_left := lhopital_zero_nhdsLT /-- **L'Hôpital's rule** for approaching a real, `deriv` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhdsNE (hdf : ∀ᶠ x in 𝓝[≠] a, DifferentiableAt ℝ f x) (hg' : ∀ᶠ x in 𝓝[≠] a, deriv g x ≠ 0) (hfa : Tendsto f (𝓝[≠] a) (𝓝 0)) (hga : Tendsto g (𝓝[≠] a) (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝[≠] a) l) : Tendsto (fun x => f x / g x) (𝓝[≠] a) l := by simp only [← Iio_union_Ioi, nhdsWithin_union, tendsto_sup, eventually_sup] at * exact ⟨lhopital_zero_nhdsLT hdf.1 hg'.1 hfa.1 hga.1 hdiv.1, lhopital_zero_nhdsGT hdf.2 hg'.2 hfa.2 hga.2 hdiv.2⟩ @[deprecated (since := "2025-03-02")] alias lhopital_zero_nhds' := lhopital_zero_nhdsNE /-- **L'Hôpital's rule** for approaching a real, `deriv` version -/ theorem lhopital_zero_nhds (hdf : ∀ᶠ x in 𝓝 a, DifferentiableAt ℝ f x) (hg' : ∀ᶠ x in 𝓝 a, deriv g x ≠ 0) (hfa : Tendsto f (𝓝 a) (𝓝 0)) (hga : Tendsto g (𝓝 a) (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) (𝓝 a) l) : Tendsto (fun x => f x / g x) (𝓝[≠] a) l := by apply lhopital_zero_nhdsNE <;> (first | apply eventually_nhdsWithin_of_eventually_nhds | apply tendsto_nhdsWithin_of_tendsto_nhds) <;> assumption /-- **L'Hôpital's rule** for approaching +∞, `deriv` version -/ theorem lhopital_zero_atTop (hdf : ∀ᶠ x : ℝ in atTop, DifferentiableAt ℝ f x) (hg' : ∀ᶠ x : ℝ in atTop, deriv g x ≠ 0) (hftop : Tendsto f atTop (𝓝 0)) (hgtop : Tendsto g atTop (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) atTop l) : Tendsto (fun x => f x / g x) atTop l := by have hdg : ∀ᶠ x in atTop, DifferentiableAt ℝ g x := hg'.mp (Eventually.of_forall fun _ hg' => by_contradiction fun h => hg' (deriv_zero_of_not_differentiableAt h)) have hdf' : ∀ᶠ x in atTop, HasDerivAt f (deriv f x) x := hdf.mono fun _ => DifferentiableAt.hasDerivAt have hdg' : ∀ᶠ x in atTop, HasDerivAt g (deriv g x) x := hdg.mono fun _ => DifferentiableAt.hasDerivAt exact HasDerivAt.lhopital_zero_atTop hdf' hdg' hg' hftop hgtop hdiv /-- **L'Hôpital's rule** for approaching -∞, `deriv` version -/ theorem lhopital_zero_atBot (hdf : ∀ᶠ x : ℝ in atBot, DifferentiableAt ℝ f x) (hg' : ∀ᶠ x : ℝ in atBot, deriv g x ≠ 0) (hfbot : Tendsto f atBot (𝓝 0)) (hgbot : Tendsto g atBot (𝓝 0)) (hdiv : Tendsto (fun x => (deriv f) x / (deriv g) x) atBot l) : Tendsto (fun x => f x / g x) atBot l := by have hdg : ∀ᶠ x in atBot, DifferentiableAt ℝ g x := hg'.mono fun _ hg' => by_contradiction fun h => hg' (deriv_zero_of_not_differentiableAt h) have hdf' : ∀ᶠ x in atBot, HasDerivAt f (deriv f x) x := hdf.mono fun _ => DifferentiableAt.hasDerivAt have hdg' : ∀ᶠ x in atBot, HasDerivAt g (deriv g x) x := hdg.mono fun _ => DifferentiableAt.hasDerivAt exact HasDerivAt.lhopital_zero_atBot hdf' hdg' hg' hfbot hgbot hdiv
end deriv
Mathlib/Analysis/Calculus/LHopital.lean
449
460
/- 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.Group.Subgroup.Map import Mathlib.Algebra.Module.Submodule.Basic import Mathlib.Algebra.Module.Submodule.Lattice import Mathlib.Algebra.Module.Submodule.LinearMap /-! # `map` and `comap` for `Submodule`s ## Main declarations * `Submodule.map`: The pushforward of a submodule `p ⊆ M` by `f : M → M₂` * `Submodule.comap`: The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` * `Submodule.giMapComap`: `map f` and `comap f` form a `GaloisInsertion` when `f` is surjective. * `Submodule.gciMapComap`: `map f` and `comap f` form a `GaloisCoinsertion` when `f` is injective. ## Tags submodule, subspace, linear map, pushforward, pullback -/ open Function Pointwise Set variable {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} variable {M : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} namespace Submodule 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₃] variable {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} variable [RingHomCompTriple σ₁₂ σ₂₃ σ₁₃] variable (p p' : Submodule R M) (q q' : Submodule R₂ M₂) variable {x : M} section variable [RingHomSurjective σ₁₂] {F : Type*} [FunLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : F) (p : Submodule R M) : Submodule R₂ M₂ := { p.toAddSubmonoid.map f with carrier := f '' p smul_mem' := by rintro c x ⟨y, hy, rfl⟩ obtain ⟨a, rfl⟩ := σ₁₂.surjective c exact ⟨_, p.smul_mem a hy, map_smulₛₗ f _ _⟩ } @[simp] theorem map_coe (f : F) (p : Submodule R M) : (map f p : Set M₂) = f '' p := rfl @[simp] theorem map_coe_toLinearMap (f : F) (p : Submodule R M) : map (f : M →ₛₗ[σ₁₂] M₂) p = map f p := rfl theorem map_toAddSubmonoid (f : M →ₛₗ[σ₁₂] M₂) (p : Submodule R M) : (p.map f).toAddSubmonoid = p.toAddSubmonoid.map (f : M →+ M₂) := SetLike.coe_injective rfl theorem map_toAddSubmonoid' (f : M →ₛₗ[σ₁₂] M₂) (p : Submodule R M) : (p.map f).toAddSubmonoid = p.toAddSubmonoid.map f := SetLike.coe_injective rfl @[simp] theorem _root_.AddMonoidHom.coe_toIntLinearMap_map {A A₂ : Type*} [AddCommGroup A] [AddCommGroup A₂] (f : A →+ A₂) (s : AddSubgroup A) : (AddSubgroup.toIntSubmodule s).map f.toIntLinearMap = AddSubgroup.toIntSubmodule (s.map f) := rfl @[simp] theorem _root_.MonoidHom.coe_toAdditive_map {G G₂ : Type*} [Group G] [Group G₂] (f : G →* G₂) (s : Subgroup G) : s.toAddSubgroup.map (MonoidHom.toAdditive f) = Subgroup.toAddSubgroup (s.map f) := rfl @[simp] theorem _root_.AddMonoidHom.coe_toMultiplicative_map {G G₂ : Type*} [AddGroup G] [AddGroup G₂] (f : G →+ G₂) (s : AddSubgroup G) : s.toSubgroup.map (AddMonoidHom.toMultiplicative f) = AddSubgroup.toSubgroup (s.map f) := rfl @[simp] theorem mem_map {f : F} {p : Submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := Iff.rfl theorem mem_map_of_mem {f : F} {p : Submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := Set.mem_image_of_mem _ h theorem apply_coe_mem_map (f : F) {p : Submodule R M} (r : p) : f r ∈ map f p := mem_map_of_mem r.prop @[simp] theorem map_id : map (LinearMap.id : M →ₗ[R] M) p = p := Submodule.ext fun a => by simp theorem map_comp [RingHomSurjective σ₂₃] [RingHomSurjective σ₁₃] (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) (p : Submodule R M) : map (g.comp f : M →ₛₗ[σ₁₃] M₃) p = map g (map f p) := SetLike.coe_injective <| by simp only [← image_comp, map_coe, LinearMap.coe_comp, comp_apply] @[gcongr] theorem map_mono {f : F} {p p' : Submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] protected theorem map_zero : map (0 : M →ₛₗ[σ₁₂] M₂) p = ⊥ := have : ∃ x : M, x ∈ p := ⟨0, p.zero_mem⟩ ext <| by simp [this, eq_comm] theorem map_add_le (f g : M →ₛₗ[σ₁₂] M₂) : map (f + g) p ≤ map f p ⊔ map g p := by rintro x ⟨m, hm, rfl⟩ exact add_mem_sup (mem_map_of_mem hm) (mem_map_of_mem hm) theorem map_inf_le (f : F) {p q : Submodule R M} : (p ⊓ q).map f ≤ p.map f ⊓ q.map f := image_inter_subset f p q theorem map_inf (f : F) {p q : Submodule R M} (hf : Injective f) : (p ⊓ q).map f = p.map f ⊓ q.map f := SetLike.coe_injective <| Set.image_inter hf lemma map_iInf {ι : Type*} [Nonempty ι] {p : ι → Submodule R M} (f : F) (hf : Injective f) : (⨅ i, p i).map f = ⨅ i, (p i).map f := SetLike.coe_injective <| by simpa only [map_coe, iInf_coe] using hf.injOn.image_iInter_eq theorem range_map_nonempty (N : Submodule R M) : (Set.range (fun ϕ => Submodule.map ϕ N : (M →ₛₗ[σ₁₂] M₂) → Submodule R₂ M₂)).Nonempty := ⟨_, Set.mem_range.mpr ⟨0, rfl⟩⟩ end section SemilinearMap variable {σ₂₁ : R₂ →+* R} [RingHomInvPair σ₁₂ σ₂₁] [RingHomInvPair σ₂₁ σ₁₂] variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] /-- The pushforward of a submodule by an injective linear map is linearly equivalent to the original submodule. See also `LinearEquiv.submoduleMap` for a computable version when `f` has an explicit inverse. -/ noncomputable def equivMapOfInjective (f : F) (i : Injective f) (p : Submodule R M) : p ≃ₛₗ[σ₁₂] p.map f := { Equiv.Set.image f p i with map_add' := by intros simp only [coe_add, map_add, Equiv.toFun_as_coe, Equiv.Set.image_apply] rfl map_smul' := by intros -- Note: https://github.com/leanprover-community/mathlib4/pull/8386 changed `map_smulₛₗ` into `map_smulₛₗ _` simp only [coe_smul_of_tower, map_smulₛₗ _, Equiv.toFun_as_coe, Equiv.Set.image_apply] rfl } @[simp] theorem coe_equivMapOfInjective_apply (f : F) (i : Injective f) (p : Submodule R M) (x : p) : (equivMapOfInjective f i p x : M₂) = f x := rfl @[simp] theorem map_equivMapOfInjective_symm_apply (f : F) (i : Injective f) (p : Submodule R M) (x : p.map f) : f ((equivMapOfInjective f i p).symm x) = x := by rw [← LinearEquiv.apply_symm_apply (equivMapOfInjective f i p) x, coe_equivMapOfInjective_apply, i.eq_iff, LinearEquiv.apply_symm_apply] /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap [SemilinearMapClass F σ₁₂ M M₂] (f : F) (p : Submodule R₂ M₂) : Submodule R M := { p.toAddSubmonoid.comap f with carrier := f ⁻¹' p -- Note: https://github.com/leanprover-community/mathlib4/pull/8386 added `map_smulₛₗ _` smul_mem' := fun a x h => by simp [p.smul_mem (σ₁₂ a) h, map_smulₛₗ _] } @[simp] theorem comap_coe (f : F) (p : Submodule R₂ M₂) : (comap f p : Set M) = f ⁻¹' p := rfl @[simp] theorem comap_coe_toLinearMap (f : F) (p : Submodule R₂ M₂) : comap (f : M →ₛₗ[σ₁₂] M₂) p = comap f p := rfl @[simp] theorem AddMonoidHom.coe_toIntLinearMap_comap {A A₂ : Type*} [AddCommGroup A] [AddCommGroup A₂] (f : A →+ A₂) (s : AddSubgroup A₂) : (AddSubgroup.toIntSubmodule s).comap f.toIntLinearMap = AddSubgroup.toIntSubmodule (s.comap f) := rfl @[simp] theorem mem_comap {f : F} {p : Submodule R₂ M₂} : x ∈ comap f p ↔ f x ∈ p := Iff.rfl @[simp] theorem comap_id : comap (LinearMap.id : M →ₗ[R] M) p = p := SetLike.coe_injective rfl theorem comap_comp (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) (p : Submodule R₃ M₃) : comap (g.comp f : M →ₛₗ[σ₁₃] M₃) p = comap f (comap g p) := rfl @[gcongr] theorem comap_mono {f : F} {q q' : Submodule R₂ M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono theorem le_comap_pow_of_le_comap (p : Submodule R M) {f : M →ₗ[R] M} (h : p ≤ p.comap f) (k : ℕ) : p ≤ p.comap (f ^ k) := by induction k with | zero => simp [Module.End.one_eq_id] | succ k ih => simp [Module.End.iterate_succ, comap_comp, h.trans (comap_mono ih)] section variable [RingHomSurjective σ₁₂] theorem map_le_iff_le_comap {f : F} {p : Submodule R M} {q : Submodule R₂ M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff theorem gc_map_comap (f : F) : GaloisConnection (map f) (comap f) | _, _ => map_le_iff_le_comap @[simp] theorem map_bot (f : F) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] theorem map_sup (f : F) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_sup @[simp] theorem map_iSup {ι : Sort*} (f : F) (p : ι → Submodule R M) : map f (⨆ i, p i) = ⨆ i, map f (p i) := (gc_map_comap f : GaloisConnection (map f) (comap f)).l_iSup end @[simp] theorem comap_top (f : F) : comap f ⊤ = ⊤ := rfl @[simp] theorem comap_inf (f : F) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] theorem comap_iInf [RingHomSurjective σ₁₂] {ι : Sort*} (f : F) (p : ι → Submodule R₂ M₂) : comap f (⨅ i, p i) = ⨅ i, comap f (p i) := (gc_map_comap f : GaloisConnection (map f) (comap f)).u_iInf @[simp] theorem comap_zero : comap (0 : M →ₛₗ[σ₁₂] M₂) q = ⊤ := ext <| by simp theorem map_comap_le [RingHomSurjective σ₁₂] (f : F) (q : Submodule R₂ M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ theorem le_comap_map [RingHomSurjective σ₁₂] (f : F) (p : Submodule R M) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ section submoduleOf /-- For any `R` submodules `p` and `q`, `p ⊓ q` as a submodule of `q`. -/ def submoduleOf (p q : Submodule R M) : Submodule R q := Submodule.comap q.subtype p /-- If `p ≤ q`, then `p` as a subgroup of `q` is isomorphic to `p`. -/ def submoduleOfEquivOfLe {p q : Submodule R M} (h : p ≤ q) : p.submoduleOf q ≃ₗ[R] p where toFun m := ⟨m.1, m.2⟩ invFun m := ⟨⟨m.1, h m.2⟩, m.2⟩ left_inv _ := Subtype.ext rfl right_inv _ := Subtype.ext rfl map_add' _ _ := rfl map_smul' _ _ := rfl end submoduleOf section GaloisInsertion variable [RingHomSurjective σ₁₂] {f : F} /-- `map f` and `comap f` form a `GaloisInsertion` when `f` is surjective. -/ def giMapComap (hf : Surjective f) : GaloisInsertion (map f) (comap f) := (gc_map_comap f).toGaloisInsertion fun S x hx => by rcases hf x with ⟨y, rfl⟩ simp only [mem_map, mem_comap] exact ⟨y, hx, rfl⟩ variable (hf : Surjective f) include hf theorem map_comap_eq_of_surjective (p : Submodule R₂ M₂) : (p.comap f).map f = p := (giMapComap hf).l_u_eq _ theorem map_surjective_of_surjective : Function.Surjective (map f) := (giMapComap hf).l_surjective theorem comap_injective_of_surjective : Function.Injective (comap f) := (giMapComap hf).u_injective theorem map_sup_comap_of_surjective (p q : Submodule R₂ M₂) : (p.comap f ⊔ q.comap f).map f = p ⊔ q := (giMapComap hf).l_sup_u _ _ theorem map_iSup_comap_of_sujective {ι : Sort*} (S : ι → Submodule R₂ M₂) : (⨆ i, (S i).comap f).map f = iSup S := (giMapComap hf).l_iSup_u _ theorem map_inf_comap_of_surjective (p q : Submodule R₂ M₂) : (p.comap f ⊓ q.comap f).map f = p ⊓ q := (giMapComap hf).l_inf_u _ _ theorem map_iInf_comap_of_surjective {ι : Sort*} (S : ι → Submodule R₂ M₂) : (⨅ i, (S i).comap f).map f = iInf S := (giMapComap hf).l_iInf_u _ theorem comap_le_comap_iff_of_surjective {p q : Submodule R₂ M₂} : p.comap f ≤ q.comap f ↔ p ≤ q := (giMapComap hf).u_le_u_iff lemma comap_lt_comap_iff_of_surjective {p q : Submodule R₂ M₂} : p.comap f < q.comap f ↔ p < q := by apply lt_iff_lt_of_le_iff_le' <;> exact comap_le_comap_iff_of_surjective hf theorem comap_strictMono_of_surjective : StrictMono (comap f) := (giMapComap hf).strictMono_u variable {p q} theorem le_map_of_comap_le_of_surjective (h : q.comap f ≤ p) : q ≤ p.map f := map_comap_eq_of_surjective hf q ▸ map_mono h theorem lt_map_of_comap_lt_of_surjective (h : q.comap f < p) : q < p.map f := by rw [lt_iff_le_not_le] at h ⊢; rw [map_le_iff_le_comap] exact h.imp_left (le_map_of_comap_le_of_surjective hf) end GaloisInsertion section GaloisCoinsertion variable [RingHomSurjective σ₁₂] {f : F} /-- `map f` and `comap f` form a `GaloisCoinsertion` when `f` is injective. -/ def gciMapComap (hf : Injective f) : GaloisCoinsertion (map f) (comap f) := (gc_map_comap f).toGaloisCoinsertion fun S x => by simp only [mem_comap, mem_map, forall_exists_index, and_imp] intro y hy hxy rw [hf.eq_iff] at hxy rwa [← hxy] variable (hf : Injective f) include hf theorem comap_map_eq_of_injective (p : Submodule R M) : (p.map f).comap f = p := (gciMapComap hf).u_l_eq _ theorem comap_surjective_of_injective : Function.Surjective (comap f) := (gciMapComap hf).u_surjective theorem map_injective_of_injective : Function.Injective (map f) := (gciMapComap hf).l_injective theorem comap_inf_map_of_injective (p q : Submodule R M) : (p.map f ⊓ q.map f).comap f = p ⊓ q := (gciMapComap hf).u_inf_l _ _ theorem comap_iInf_map_of_injective {ι : Sort*} (S : ι → Submodule R M) : (⨅ i, (S i).map f).comap f = iInf S := (gciMapComap hf).u_iInf_l _ theorem comap_sup_map_of_injective (p q : Submodule R M) : (p.map f ⊔ q.map f).comap f = p ⊔ q := (gciMapComap hf).u_sup_l _ _ theorem comap_iSup_map_of_injective {ι : Sort*} (S : ι → Submodule R M) : (⨆ i, (S i).map f).comap f = iSup S := (gciMapComap hf).u_iSup_l _ theorem map_le_map_iff_of_injective (p q : Submodule R M) : p.map f ≤ q.map f ↔ p ≤ q := (gciMapComap hf).l_le_l_iff theorem map_strictMono_of_injective : StrictMono (map f) := (gciMapComap hf).strictMono_l lemma map_lt_map_iff_of_injective {p q : Submodule R M} : p.map f < q.map f ↔ p < q := by rw [lt_iff_le_and_ne, lt_iff_le_and_ne, map_le_map_iff_of_injective hf, (map_injective_of_injective hf).ne_iff] lemma comap_lt_of_lt_map_of_injective {p : Submodule R M} {q : Submodule R₂ M₂} (h : q < p.map f) : q.comap f < p := by rw [← map_lt_map_iff_of_injective hf] exact (map_comap_le _ _).trans_lt h lemma map_covBy_of_injective {p q : Submodule R M} (h : p ⋖ q) : p.map f ⋖ q.map f := by refine ⟨lt_of_le_of_ne (map_mono h.1.le) ((map_injective_of_injective hf).ne h.1.ne), ?_⟩ intro P h₁ h₂ refine h.2 ?_ (Submodule.comap_lt_of_lt_map_of_injective hf h₂) rw [← Submodule.map_lt_map_iff_of_injective hf] refine h₁.trans_le ?_ exact (Set.image_preimage_eq_of_subset (.trans h₂.le (Set.image_subset_range _ _))).superset end GaloisCoinsertion end SemilinearMap section OrderIso variable [RingHomSurjective σ₁₂] {F : Type*} /-- A linear isomorphism induces an order isomorphism of submodules. -/ @[simps symm_apply apply] def orderIsoMapComapOfBijective [FunLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] (f : F) (hf : Bijective f) : Submodule R M ≃o Submodule R₂ M₂ where toFun := map f invFun := comap f left_inv := comap_map_eq_of_injective hf.injective right_inv := map_comap_eq_of_surjective hf.surjective map_rel_iff' := map_le_map_iff_of_injective hf.injective _ _ /-- A linear isomorphism induces an order isomorphism of submodules. -/ @[simps! apply] def orderIsoMapComap [EquivLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] (f : F) : Submodule R M ≃o Submodule R₂ M₂ := orderIsoMapComapOfBijective f (EquivLike.bijective f) @[simp] lemma orderIsoMapComap_symm_apply [EquivLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] (f : F) (p : Submodule R₂ M₂) : (orderIsoMapComap f).symm p = comap f p := rfl variable [EquivLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] {e : F} variable {p} @[simp] protected lemma map_eq_bot_iff : p.map e = ⊥ ↔ p = ⊥ := map_eq_bot_iff (orderIsoMapComap e) @[simp] protected lemma map_eq_top_iff : p.map e = ⊤ ↔ p = ⊤ := map_eq_top_iff (orderIsoMapComap e) protected lemma map_ne_bot_iff : p.map e ≠ ⊥ ↔ p ≠ ⊥ := by simp protected lemma map_ne_top_iff : p.map e ≠ ⊤ ↔ p ≠ ⊤ := by simp end OrderIso variable {F : Type*} [FunLike F M M₂] [SemilinearMapClass F σ₁₂ M M₂] --TODO(Mario): is there a way to prove this from order properties? theorem map_inf_eq_map_inf_comap [RingHomSurjective σ₁₂] {f : F} {p : Submodule R M} {p' : Submodule R₂ M₂} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) @[simp] theorem map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext fun x => ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, fun ⟨h₁, h₂⟩ => ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ theorem eq_zero_of_bot_submodule : ∀ b : (⊥ : Submodule R M), b = 0 | ⟨b', hb⟩ => Subtype.eq <| show b' = 0 from (mem_bot R).1 hb /-- The infimum of a family of invariant submodule of an endomorphism is also an invariant submodule. -/ theorem _root_.LinearMap.iInf_invariant {σ : R →+* R} [RingHomSurjective σ] {ι : Sort*} (f : M →ₛₗ[σ] M) {p : ι → Submodule R M} (hf : ∀ i, ∀ v ∈ p i, f v ∈ p i) : ∀ v ∈ iInf p, f v ∈ iInf p := by have : ∀ i, (p i).map f ≤ p i := by rintro i - ⟨v, hv, rfl⟩ exact hf i v hv suffices (iInf p).map f ≤ iInf p by exact fun v hv => this ⟨v, hv, rfl⟩ exact le_iInf fun i => (Submodule.map_mono (iInf_le p i)).trans (this i) theorem disjoint_iff_comap_eq_bot {p q : Submodule R M} : Disjoint p q ↔ comap p.subtype q = ⊥ := by rw [← (map_injective_of_injective (show Injective p.subtype from Subtype.coe_injective)).eq_iff, map_comap_subtype, map_bot, disjoint_iff] end AddCommMonoid section AddCommGroup variable [Ring R] [AddCommGroup M] [Module R M] (p : Submodule R M) variable [AddCommGroup M₂] [Module R M₂] @[simp] protected theorem map_neg (f : M →ₗ[R] M₂) : map (-f) p = map f p := ext fun _ => ⟨fun ⟨x, hx, hy⟩ => hy ▸ ⟨-x, show -x ∈ p from neg_mem hx, map_neg f x⟩, fun ⟨x, hx, hy⟩ => hy ▸ ⟨-x, show -x ∈ p from neg_mem hx, (map_neg (-f) _).trans (neg_neg (f x))⟩⟩ @[simp] lemma comap_neg {f : M →ₗ[R] M₂} {p : Submodule R M₂} : p.comap (-f) = p.comap f := by ext; simp lemma map_toAddSubgroup (f : M →ₗ[R] M₂) (p : Submodule R M) : (p.map f).toAddSubgroup = p.toAddSubgroup.map (f : M →+ M₂) := rfl end AddCommGroup end Submodule namespace Submodule variable {K : Type*} {V : Type*} {V₂ : Type*} variable [Semifield K] variable [AddCommMonoid V] [Module K V] variable [AddCommMonoid V₂] [Module K V₂] theorem comap_smul (f : V →ₗ[K] V₂) (p : Submodule K V₂) (a : K) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [Submodule.mem_comap, p.smul_mem_iff h, LinearMap.smul_apply] protected theorem map_smul (f : V →ₗ[K] V₂) (p : Submodule K V) (a : K) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm (by rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap]) (by rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap]) theorem comap_smul' (f : V →ₗ[K] V₂) (p : Submodule K V₂) (a : K) : p.comap (a • f) = ⨅ _ : a ≠ 0, p.comap f := by classical by_cases h : a = 0 <;> simp [h, comap_smul] theorem map_smul' (f : V →ₗ[K] V₂) (p : Submodule K V) (a : K) : p.map (a • f) = ⨆ _ : a ≠ 0, map f p := by classical by_cases h : a = 0 <;> simp [h, Submodule.map_smul] end Submodule namespace Submodule section Module variable [Semiring R] [AddCommMonoid M] [Module R M] /-- If `s ≤ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ @[simps apply_coe symm_apply] def comapSubtypeEquivOfLe {p q : Submodule R M} (hpq : p ≤ q) : comap q.subtype p ≃ₗ[R] p where toFun x := ⟨x, x.2⟩ invFun x := ⟨⟨x, hpq x.2⟩, x.2⟩ left_inv x := by simp only [coe_mk, SetLike.eta, LinearEquiv.coe_coe] right_inv x := by simp only [Subtype.coe_mk, SetLike.eta, LinearEquiv.coe_coe] map_add' _ _ := rfl map_smul' _ _ := rfl end Module
end Submodule namespace Submodule variable [Semiring R] [Semiring R₂] variable [AddCommMonoid M] [AddCommMonoid M₂] [Module R M] [Module R₂ M₂] variable {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R}
Mathlib/Algebra/Module/Submodule/Map.lean
543
549
/- Copyright (c) 2021 Bryan Gin-ge Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bryan Gin-ge Chen, Yaël Dillies -/ import Mathlib.Algebra.Group.Idempotent import Mathlib.Algebra.Ring.Equiv import Mathlib.Algebra.Ring.PUnit import Mathlib.Order.Hom.BoundedLattice import Mathlib.Tactic.Abel import Mathlib.Tactic.Ring /-! # Boolean rings A Boolean ring is a ring where multiplication is idempotent. They are equivalent to Boolean algebras. ## Main declarations * `BooleanRing`: a typeclass for rings where multiplication is idempotent. * `BooleanRing.toBooleanAlgebra`: Turn a Boolean ring into a Boolean algebra. * `BooleanAlgebra.toBooleanRing`: Turn a Boolean algebra into a Boolean ring. * `AsBoolAlg`: Type-synonym for the Boolean algebra associated to a Boolean ring. * `AsBoolRing`: Type-synonym for the Boolean ring associated to a Boolean algebra. ## Implementation notes We provide two ways of turning a Boolean algebra/ring into a Boolean ring/algebra: * Instances on the same type accessible in locales `BooleanAlgebraOfBooleanRing` and `BooleanRingOfBooleanAlgebra`. * Type-synonyms `AsBoolAlg` and `AsBoolRing`. At this point in time, it is not clear the first way is useful, but we keep it for educational purposes and because it is easier than dealing with `ofBoolAlg`/`toBoolAlg`/`ofBoolRing`/`toBoolRing` explicitly. ## Tags boolean ring, boolean algebra -/ open scoped symmDiff variable {α β γ : Type*} /-- A Boolean ring is a ring where multiplication is idempotent. -/ class BooleanRing (α) extends Ring α where /-- Multiplication in a boolean ring is idempotent. -/ isIdempotentElem (a : α) : IsIdempotentElem a namespace BooleanRing variable [BooleanRing α] (a b : α) @[scoped simp] lemma mul_self : a * a = a := IsIdempotentElem.eq (isIdempotentElem a) instance : Std.IdempotentOp (α := α) (· * ·) := ⟨BooleanRing.mul_self⟩ @[scoped simp] theorem add_self : a + a = 0 := by have : a + a = a + a + (a + a) := calc a + a = (a + a) * (a + a) := by rw [mul_self] _ = a * a + a * a + (a * a + a * a) := by rw [add_mul, mul_add] _ = a + a + (a + a) := by rw [mul_self] rwa [right_eq_add] at this @[scoped simp] theorem neg_eq : -a = a := calc -a = -a + 0 := by rw [add_zero] _ = -a + -a + a := by rw [← neg_add_cancel, add_assoc] _ = a := by rw [add_self, zero_add] theorem add_eq_zero' : a + b = 0 ↔ a = b := calc a + b = 0 ↔ a = -b := add_eq_zero_iff_eq_neg _ ↔ a = b := by rw [neg_eq] @[simp] theorem mul_add_mul : a * b + b * a = 0 := by have : a + b = a + b + (a * b + b * a) := calc a + b = (a + b) * (a + b) := by rw [mul_self] _ = a * a + a * b + (b * a + b * b) := by rw [add_mul, mul_add, mul_add] _ = a + a * b + (b * a + b) := by simp only [mul_self] _ = a + b + (a * b + b * a) := by abel rwa [left_eq_add] at this @[scoped simp] theorem sub_eq_add : a - b = a + b := by rw [sub_eq_add_neg, add_right_inj, neg_eq] @[simp] theorem mul_one_add_self : a * (1 + a) = 0 := by rw [mul_add, mul_one, mul_self, add_self] -- Note [lower instance priority] instance (priority := 100) toCommRing : CommRing α := { (inferInstance : BooleanRing α) with mul_comm := fun a b => by rw [← add_eq_zero', mul_add_mul] } end BooleanRing instance : BooleanRing PUnit := ⟨fun _ => Subsingleton.elim _ _⟩ /-! ### Turning a Boolean ring into a Boolean algebra -/ section RingToAlgebra /-- Type synonym to view a Boolean ring as a Boolean algebra. -/ def AsBoolAlg (α : Type*) := α /-- The "identity" equivalence between `AsBoolAlg α` and `α`. -/ def toBoolAlg : α ≃ AsBoolAlg α := Equiv.refl _ /-- The "identity" equivalence between `α` and `AsBoolAlg α`. -/ def ofBoolAlg : AsBoolAlg α ≃ α := Equiv.refl _ @[simp] theorem toBoolAlg_symm_eq : (@toBoolAlg α).symm = ofBoolAlg := rfl @[simp] theorem ofBoolAlg_symm_eq : (@ofBoolAlg α).symm = toBoolAlg := rfl @[simp] theorem toBoolAlg_ofBoolAlg (a : AsBoolAlg α) : toBoolAlg (ofBoolAlg a) = a := rfl @[simp] theorem ofBoolAlg_toBoolAlg (a : α) : ofBoolAlg (toBoolAlg a) = a := rfl theorem toBoolAlg_inj {a b : α} : toBoolAlg a = toBoolAlg b ↔ a = b := Iff.rfl theorem ofBoolAlg_inj {a b : AsBoolAlg α} : ofBoolAlg a = ofBoolAlg b ↔ a = b := Iff.rfl instance [Inhabited α] : Inhabited (AsBoolAlg α) := ‹Inhabited α› variable [BooleanRing α] [BooleanRing β] [BooleanRing γ] namespace BooleanRing /-- The join operation in a Boolean ring is `x + y + x * y`. -/ def sup : Max α := ⟨fun x y => x + y + x * y⟩ /-- The meet operation in a Boolean ring is `x * y`. -/ def inf : Min α := ⟨(· * ·)⟩ -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: add priority 100. lower instance priority scoped [BooleanAlgebraOfBooleanRing] attribute [instance] BooleanRing.sup scoped [BooleanAlgebraOfBooleanRing] attribute [instance] BooleanRing.inf open BooleanAlgebraOfBooleanRing theorem sup_comm (a b : α) : a ⊔ b = b ⊔ a := by dsimp only [(· ⊔ ·)] ring theorem inf_comm (a b : α) : a ⊓ b = b ⊓ a := by dsimp only [(· ⊓ ·)] ring theorem sup_assoc (a b c : α) : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := by dsimp only [(· ⊔ ·)] ring theorem inf_assoc (a b c : α) : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := by dsimp only [(· ⊓ ·)] ring theorem sup_inf_self (a b : α) : a ⊔ a ⊓ b = a := by dsimp only [(· ⊔ ·), (· ⊓ ·)] rw [← mul_assoc, mul_self, add_assoc, add_self, add_zero] theorem inf_sup_self (a b : α) : a ⊓ (a ⊔ b) = a := by dsimp only [(· ⊔ ·), (· ⊓ ·)] rw [mul_add, mul_add, mul_self, ← mul_assoc, mul_self, add_assoc, add_self, add_zero] theorem le_sup_inf_aux (a b c : α) : (a + b + a * b) * (a + c + a * c) = a + b * c + a * (b * c) := calc (a + b + a * b) * (a + c + a * c) = a * a + b * c + a * (b * c) + (a * b + a * a * b) + (a * c + a * a * c) + (a * b * c + a * a * b * c) := by ring _ = a + b * c + a * (b * c) := by simp only [mul_self, add_self, add_zero] theorem le_sup_inf (a b c : α) : (a ⊔ b) ⊓ (a ⊔ c) ⊔ (a ⊔ b ⊓ c) = a ⊔ b ⊓ c := by dsimp only [(· ⊔ ·), (· ⊓ ·)] rw [le_sup_inf_aux, add_self, mul_self, zero_add] /-- The Boolean algebra structure on a Boolean ring. The data is defined so that: * `a ⊔ b` unfolds to `a + b + a * b` * `a ⊓ b` unfolds to `a * b` * `a ≤ b` unfolds to `a + b + a * b = b` * `⊥` unfolds to `0` * `⊤` unfolds to `1` * `aᶜ` unfolds to `1 + a` * `a \ b` unfolds to `a * (1 + b)` -/ def toBooleanAlgebra : BooleanAlgebra α := { Lattice.mk' sup_comm sup_assoc inf_comm inf_assoc sup_inf_self inf_sup_self with le_sup_inf := le_sup_inf top := 1 le_top := fun a => show a + 1 + a * 1 = 1 by rw [mul_one, add_comm a 1, add_assoc, add_self, add_zero] bot := 0 bot_le := fun a => show 0 + a + 0 * a = a by rw [zero_mul, zero_add, add_zero] compl := fun a => 1 + a inf_compl_le_bot := fun a => show a * (1 + a) + 0 + a * (1 + a) * 0 = 0 by norm_num [mul_add, mul_self, add_self] top_le_sup_compl := fun a => by change 1 + (a + (1 + a) + a * (1 + a)) + 1 * (a + (1 + a) + a * (1 + a)) = a + (1 + a) + a * (1 + a) norm_num [mul_add, mul_self, add_self] rw [← add_assoc, add_self] } -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: add priority 100. lower instance priority scoped[BooleanAlgebraOfBooleanRing] attribute [instance] BooleanRing.toBooleanAlgebra end BooleanRing open BooleanRing instance : BooleanAlgebra (AsBoolAlg α) := @BooleanRing.toBooleanAlgebra α _ @[simp] theorem ofBoolAlg_top : ofBoolAlg (⊤ : AsBoolAlg α) = 1 := rfl @[simp] theorem ofBoolAlg_bot : ofBoolAlg (⊥ : AsBoolAlg α) = 0 := rfl @[simp] theorem ofBoolAlg_sup (a b : AsBoolAlg α) : ofBoolAlg (a ⊔ b) = ofBoolAlg a + ofBoolAlg b + ofBoolAlg a * ofBoolAlg b := rfl @[simp] theorem ofBoolAlg_inf (a b : AsBoolAlg α) : ofBoolAlg (a ⊓ b) = ofBoolAlg a * ofBoolAlg b := rfl @[simp] theorem ofBoolAlg_compl (a : AsBoolAlg α) : ofBoolAlg aᶜ = 1 + ofBoolAlg a := rfl @[simp] theorem ofBoolAlg_sdiff (a b : AsBoolAlg α) : ofBoolAlg (a \ b) = ofBoolAlg a * (1 + ofBoolAlg b) := rfl private theorem of_boolalg_symmDiff_aux (a b : α) : (a + b + a * b) * (1 + a * b) = a + b := calc (a + b + a * b) * (1 + a * b) _ = a + b + (a * b + a * b * (a * b)) + (a * (b * b) + a * a * b) := by ring _ = a + b := by simp only [mul_self, add_self, add_zero] @[simp] theorem ofBoolAlg_symmDiff (a b : AsBoolAlg α) : ofBoolAlg (a ∆ b) = ofBoolAlg a + ofBoolAlg b := by rw [symmDiff_eq_sup_sdiff_inf] exact of_boolalg_symmDiff_aux _ _ @[simp] theorem ofBoolAlg_mul_ofBoolAlg_eq_left_iff {a b : AsBoolAlg α} : ofBoolAlg a * ofBoolAlg b = ofBoolAlg a ↔ a ≤ b := @inf_eq_left (AsBoolAlg α) _ _ _ @[simp] theorem toBoolAlg_zero : toBoolAlg (0 : α) = ⊥ := rfl @[simp] theorem toBoolAlg_one : toBoolAlg (1 : α) = ⊤ := rfl @[simp] theorem toBoolAlg_mul (a b : α) : toBoolAlg (a * b) = toBoolAlg a ⊓ toBoolAlg b := rfl @[simp] theorem toBoolAlg_add_add_mul (a b : α) : toBoolAlg (a + b + a * b) = toBoolAlg a ⊔ toBoolAlg b := rfl @[simp] theorem toBoolAlg_add (a b : α) : toBoolAlg (a + b) = toBoolAlg a ∆ toBoolAlg b := (ofBoolAlg_symmDiff a b).symm /-- Turn a ring homomorphism from Boolean rings `α` to `β` into a bounded lattice homomorphism from `α` to `β` considered as Boolean algebras. -/ @[simps]
protected def RingHom.asBoolAlg (f : α →+* β) : BoundedLatticeHom (AsBoolAlg α) (AsBoolAlg β) where toFun := toBoolAlg ∘ f ∘ ofBoolAlg map_sup' a b := by dsimp
Mathlib/Algebra/Ring/BooleanRing.lean
305
308
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Group.Basic import Mathlib.Algebra.Order.Monoid.Unbundled.Basic import Mathlib.Order.Lattice /-! # Ordered Subtraction This file proves lemmas relating (truncated) subtraction with an order. We provide a class `OrderedSub` stating that `a - b ≤ c ↔ a ≤ c + b`. The subtraction discussed here could both be normal subtraction in an additive group or truncated subtraction on a canonically ordered monoid (`ℕ`, `Multiset`, `PartENat`, `ENNReal`, ...) ## Implementation details `OrderedSub` is a mixin type-class, so that we can use the results in this file even in cases where we don't have a `CanonicallyOrderedAdd` instance (even though that is our main focus). Conversely, this means we can use `CanonicallyOrderedAdd` without necessarily having to define a subtraction. The results in this file are ordered by the type-class assumption needed to prove it. This means that similar results might not be close to each other. Furthermore, we don't prove implications if a bi-implication can be proven under the same assumptions. Lemmas using this class are named using `tsub` instead of `sub` (short for "truncated subtraction"). This is to avoid naming conflicts with similar lemmas about ordered groups. We provide a second version of most results that require `[AddLeftReflectLE α]`. In the second version we replace this type-class assumption by explicit `AddLECancellable` assumptions. TODO: maybe we should make a multiplicative version of this, so that we can replace some identical lemmas about subtraction/division in `Ordered[Add]CommGroup` with these. TODO: generalize `Nat.le_of_le_of_sub_le_sub_right`, `Nat.sub_le_sub_right_iff`, `Nat.mul_self_sub_mul_self_eq` -/ variable {α : Type*} /-- `OrderedSub α` means that `α` has a subtraction characterized by `a - b ≤ c ↔ a ≤ c + b`. In other words, `a - b` is the least `c` such that `a ≤ b + c`. This is satisfied both by the subtraction in additive ordered groups and by truncated subtraction in canonically ordered monoids on many specific types. -/ class OrderedSub (α : Type*) [LE α] [Add α] [Sub α] : Prop where /-- `a - b` provides a lower bound on `c` such that `a ≤ c + b`. -/ tsub_le_iff_right : ∀ a b c : α, a - b ≤ c ↔ a ≤ c + b section Add @[simp] theorem tsub_le_iff_right [LE α] [Add α] [Sub α] [OrderedSub α] {a b c : α} : a - b ≤ c ↔ a ≤ c + b := OrderedSub.tsub_le_iff_right a b c variable [Preorder α] [Add α] [Sub α] [OrderedSub α] {a b : α} /-- See `add_tsub_cancel_right` for the equality if `AddLeftReflectLE α`. -/ theorem add_tsub_le_right : a + b - b ≤ a := tsub_le_iff_right.mpr le_rfl theorem le_tsub_add : b ≤ b - a + a := tsub_le_iff_right.mp le_rfl end Add /-! ### Preorder -/ section OrderedAddCommSemigroup section Preorder variable [Preorder α] section AddCommSemigroup variable [AddCommSemigroup α] [Sub α] [OrderedSub α] {a b c d : α} /- TODO: Most results can be generalized to [Add α] [@Std.Commutative α (· + ·)] -/ theorem tsub_le_iff_left : a - b ≤ c ↔ a ≤ b + c := by rw [tsub_le_iff_right, add_comm] theorem le_add_tsub : a ≤ b + (a - b) := tsub_le_iff_left.mp le_rfl /-- See `add_tsub_cancel_left` for the equality if `AddLeftReflectLE α`. -/ theorem add_tsub_le_left : a + b - a ≤ b := tsub_le_iff_left.mpr le_rfl @[gcongr] theorem tsub_le_tsub_right (h : a ≤ b) (c : α) : a - c ≤ b - c := tsub_le_iff_left.mpr <| h.trans le_add_tsub theorem tsub_le_iff_tsub_le : a - b ≤ c ↔ a - c ≤ b := by rw [tsub_le_iff_left, tsub_le_iff_right] /-- See `tsub_tsub_cancel_of_le` for the equality. -/ theorem tsub_tsub_le : b - (b - a) ≤ a := tsub_le_iff_right.mpr le_add_tsub section Cov variable [AddLeftMono α] @[gcongr] theorem tsub_le_tsub_left (h : a ≤ b) (c : α) : c - b ≤ c - a := tsub_le_iff_left.mpr <| le_add_tsub.trans <| add_le_add_right h _ @[gcongr] theorem tsub_le_tsub (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := (tsub_le_tsub_right hab _).trans <| tsub_le_tsub_left hcd _ theorem antitone_const_tsub : Antitone fun x => c - x := fun _ _ hxy => tsub_le_tsub rfl.le hxy /-- See `add_tsub_assoc_of_le` for the equality. -/ theorem add_tsub_le_assoc : a + b - c ≤ a + (b - c) := by rw [tsub_le_iff_left, add_left_comm] exact add_le_add_left le_add_tsub a /-- See `tsub_add_eq_add_tsub` for the equality. -/ theorem add_tsub_le_tsub_add : a + b - c ≤ a - c + b := by rw [add_comm, add_comm _ b] exact add_tsub_le_assoc theorem add_le_add_add_tsub : a + b ≤ a + c + (b - c) := by rw [add_assoc] exact add_le_add_left le_add_tsub a theorem le_tsub_add_add : a + b ≤ a - c + (b + c) := by rw [add_comm a, add_comm (a - c)] exact add_le_add_add_tsub theorem tsub_le_tsub_add_tsub : a - c ≤ a - b + (b - c) := by rw [tsub_le_iff_left, ← add_assoc, add_right_comm] exact le_add_tsub.trans (add_le_add_right le_add_tsub _) theorem tsub_tsub_tsub_le_tsub : c - a - (c - b) ≤ b - a := by rw [tsub_le_iff_left, tsub_le_iff_left, add_left_comm] exact le_tsub_add.trans (add_le_add_left le_add_tsub _) theorem tsub_tsub_le_tsub_add {a b c : α} : a - (b - c) ≤ a - b + c := tsub_le_iff_right.2 <| calc a ≤ a - b + b := le_tsub_add _ ≤ a - b + (c + (b - c)) := add_le_add_left le_add_tsub _ _ = a - b + c + (b - c) := (add_assoc _ _ _).symm /-- See `tsub_add_tsub_comm` for the equality. -/ theorem add_tsub_add_le_tsub_add_tsub : a + b - (c + d) ≤ a - c + (b - d) := by rw [add_comm c, tsub_le_iff_left, add_assoc, ← tsub_le_iff_left, ← tsub_le_iff_left] refine (tsub_le_tsub_right add_tsub_le_assoc c).trans ?_ rw [add_comm a, add_comm (a - c)] exact add_tsub_le_assoc /-- See `add_tsub_add_eq_tsub_left` for the equality. -/ theorem add_tsub_add_le_tsub_left : a + b - (a + c) ≤ b - c := by rw [tsub_le_iff_left, add_assoc] exact add_le_add_left le_add_tsub _ /-- See `add_tsub_add_eq_tsub_right` for the equality. -/ theorem add_tsub_add_le_tsub_right : a + c - (b + c) ≤ a - b := by rw [tsub_le_iff_left, add_right_comm] exact add_le_add_right le_add_tsub c end Cov /-! #### Lemmas that assume that an element is `AddLECancellable` -/ namespace AddLECancellable protected theorem le_add_tsub_swap (hb : AddLECancellable b) : a ≤ b + a - b := hb le_add_tsub protected theorem le_add_tsub (hb : AddLECancellable b) : a ≤ a + b - b := by rw [add_comm] exact hb.le_add_tsub_swap protected theorem le_tsub_of_add_le_left (ha : AddLECancellable a) (h : a + b ≤ c) : b ≤ c - a := ha <| h.trans le_add_tsub protected theorem le_tsub_of_add_le_right (hb : AddLECancellable b) (h : a + b ≤ c) : a ≤ c - b := hb.le_tsub_of_add_le_left <| by rwa [add_comm] end AddLECancellable /-! ### Lemmas where addition is order-reflecting -/ section Contra variable [AddLeftReflectLE α] theorem le_add_tsub_swap : a ≤ b + a - b := Contravariant.AddLECancellable.le_add_tsub_swap theorem le_add_tsub' : a ≤ a + b - b := Contravariant.AddLECancellable.le_add_tsub theorem le_tsub_of_add_le_left (h : a + b ≤ c) : b ≤ c - a := Contravariant.AddLECancellable.le_tsub_of_add_le_left h theorem le_tsub_of_add_le_right (h : a + b ≤ c) : a ≤ c - b := Contravariant.AddLECancellable.le_tsub_of_add_le_right h end Contra end AddCommSemigroup variable [AddCommMonoid α] [Sub α] [OrderedSub α] {a b : α} theorem tsub_nonpos : a - b ≤ 0 ↔ a ≤ b := by rw [tsub_le_iff_left, add_zero] alias ⟨_, tsub_nonpos_of_le⟩ := tsub_nonpos end Preorder /-! ### Partial order -/ variable [PartialOrder α] [AddCommSemigroup α] [Sub α] [OrderedSub α] {a b c d : α} theorem tsub_tsub (b a c : α) : b - a - c = b - (a + c) := by apply le_antisymm · rw [tsub_le_iff_left, tsub_le_iff_left, ← add_assoc, ← tsub_le_iff_left] · rw [tsub_le_iff_left, add_assoc, ← tsub_le_iff_left, ← tsub_le_iff_left] theorem tsub_add_eq_tsub_tsub (a b c : α) : a - (b + c) = a - b - c := (tsub_tsub _ _ _).symm theorem tsub_add_eq_tsub_tsub_swap (a b c : α) : a - (b + c) = a - c - b := by rw [add_comm] apply tsub_add_eq_tsub_tsub theorem tsub_right_comm : a - b - c = a - c - b := by rw [← tsub_add_eq_tsub_tsub, tsub_add_eq_tsub_tsub_swap] /-! ### Lemmas that assume that an element is `AddLECancellable`. -/ namespace AddLECancellable /-- See `AddLECancellable.tsub_eq_of_eq_add'` for a version assuming that `a = c + b` itself is cancellable rather than `b`. -/ protected theorem tsub_eq_of_eq_add (hb : AddLECancellable b) (h : a = c + b) : a - b = c := le_antisymm (tsub_le_iff_right.mpr h.le) <| by rw [h] exact hb.le_add_tsub /-- Weaker version of `AddLECancellable.tsub_eq_of_eq_add` assuming that `a = c + b` itself is cancellable rather than `b`. -/ protected lemma tsub_eq_of_eq_add' [AddLeftMono α] (ha : AddLECancellable a) (h : a = c + b) : a - b = c := (h ▸ ha).of_add_right.tsub_eq_of_eq_add h /-- See `AddLECancellable.eq_tsub_of_add_eq'` for a version assuming that `b = a + c` itself is cancellable rather than `c`. -/ protected theorem eq_tsub_of_add_eq (hc : AddLECancellable c) (h : a + c = b) : a = b - c := (hc.tsub_eq_of_eq_add h.symm).symm /-- Weaker version of `AddLECancellable.eq_tsub_of_add_eq` assuming that `b = a + c` itself is cancellable rather than `c`. -/ protected lemma eq_tsub_of_add_eq' [AddLeftMono α] (hb : AddLECancellable b) (h : a + c = b) : a = b - c := (hb.tsub_eq_of_eq_add' h.symm).symm /-- See `AddLECancellable.tsub_eq_of_eq_add_rev'` for a version assuming that `a = b + c` itself is cancellable rather than `b`. -/ protected theorem tsub_eq_of_eq_add_rev (hb : AddLECancellable b) (h : a = b + c) : a - b = c := hb.tsub_eq_of_eq_add <| by rw [add_comm, h] /-- Weaker version of `AddLECancellable.tsub_eq_of_eq_add_rev` assuming that `a = b + c` itself is cancellable rather than `b`. -/ protected lemma tsub_eq_of_eq_add_rev' [AddLeftMono α] (ha : AddLECancellable a) (h : a = b + c) : a - b = c := ha.tsub_eq_of_eq_add' <| by rw [add_comm, h] @[simp] protected theorem add_tsub_cancel_right (hb : AddLECancellable b) : a + b - b = a := hb.tsub_eq_of_eq_add <| by rw [add_comm] @[simp] protected theorem add_tsub_cancel_left (ha : AddLECancellable a) : a + b - a = b := ha.tsub_eq_of_eq_add <| add_comm a b protected theorem lt_add_of_tsub_lt_left (hb : AddLECancellable b) (h : a - b < c) : a < b + c := by rw [lt_iff_le_and_ne, ← tsub_le_iff_left] refine ⟨h.le, ?_⟩ rintro rfl simp [hb] at h protected theorem lt_add_of_tsub_lt_right (hc : AddLECancellable c) (h : a - c < b) : a < b + c := by rw [lt_iff_le_and_ne, ← tsub_le_iff_right] refine ⟨h.le, ?_⟩ rintro rfl simp [hc] at h protected theorem lt_tsub_of_add_lt_right (hc : AddLECancellable c) (h : a + c < b) : a < b - c := (hc.le_tsub_of_add_le_right h.le).lt_of_ne <| by rintro rfl exact h.not_le le_tsub_add protected theorem lt_tsub_of_add_lt_left (ha : AddLECancellable a) (h : a + c < b) : c < b - a := ha.lt_tsub_of_add_lt_right <| by rwa [add_comm] end AddLECancellable /-! #### Lemmas where addition is order-reflecting. -/ section Contra variable [AddLeftReflectLE α] theorem tsub_eq_of_eq_add (h : a = c + b) : a - b = c := Contravariant.AddLECancellable.tsub_eq_of_eq_add h theorem eq_tsub_of_add_eq (h : a + c = b) : a = b - c := Contravariant.AddLECancellable.eq_tsub_of_add_eq h theorem tsub_eq_of_eq_add_rev (h : a = b + c) : a - b = c := Contravariant.AddLECancellable.tsub_eq_of_eq_add_rev h @[simp] theorem add_tsub_cancel_right (a b : α) : a + b - b = a := Contravariant.AddLECancellable.add_tsub_cancel_right @[simp] theorem add_tsub_cancel_left (a b : α) : a + b - a = b := Contravariant.AddLECancellable.add_tsub_cancel_left /-- A more general version of the reverse direction of `sub_eq_sub_iff_add_eq_add` -/ theorem tsub_eq_tsub_of_add_eq_add (h : a + d = c + b) : a - b = c - d := by calc a - b = a + d - d - b := by rw [add_tsub_cancel_right] _ = c + b - b - d := by rw [h, tsub_right_comm] _ = c - d := by rw [add_tsub_cancel_right] theorem lt_add_of_tsub_lt_left (h : a - b < c) : a < b + c := Contravariant.AddLECancellable.lt_add_of_tsub_lt_left h theorem lt_add_of_tsub_lt_right (h : a - c < b) : a < b + c := Contravariant.AddLECancellable.lt_add_of_tsub_lt_right h /-- This lemma (and some of its corollaries) also holds for `ENNReal`, but this proof doesn't work for it. Maybe we should add this lemma as field to `OrderedSub`? -/ theorem lt_tsub_of_add_lt_left : a + c < b → c < b - a := Contravariant.AddLECancellable.lt_tsub_of_add_lt_left theorem lt_tsub_of_add_lt_right : a + c < b → a < b - c := Contravariant.AddLECancellable.lt_tsub_of_add_lt_right end Contra section Both variable [AddLeftMono α] [AddLeftReflectLE α] theorem add_tsub_add_eq_tsub_right (a c b : α) : a + c - (b + c) = a - b := by refine add_tsub_add_le_tsub_right.antisymm (tsub_le_iff_right.2 <| ?_) apply le_of_add_le_add_right rw [add_assoc] exact le_tsub_add theorem add_tsub_add_eq_tsub_left (a b c : α) : a + b - (a + c) = b - c := by rw [add_comm a b, add_comm a c, add_tsub_add_eq_tsub_right] end Both end OrderedAddCommSemigroup /-! ### Lemmas in a linearly ordered monoid. -/ section LinearOrder variable {a b c : α} [LinearOrder α] [AddCommSemigroup α] [Sub α] [OrderedSub α] /-- See `lt_of_tsub_lt_tsub_right_of_le` for a weaker statement in a partial order. -/ theorem lt_of_tsub_lt_tsub_right (h : a - c < b - c) : a < b := lt_imp_lt_of_le_imp_le (fun h => tsub_le_tsub_right h c) h /-- See `lt_tsub_iff_right_of_le` for a weaker statement in a partial order. -/ theorem lt_tsub_iff_right : a < b - c ↔ a + c < b := lt_iff_lt_of_le_iff_le tsub_le_iff_right /-- See `lt_tsub_iff_left_of_le` for a weaker statement in a partial order. -/
theorem lt_tsub_iff_left : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le tsub_le_iff_left theorem lt_tsub_comm : a < b - c ↔ c < b - a := lt_tsub_iff_left.trans lt_tsub_iff_right.symm
Mathlib/Algebra/Order/Sub/Defs.lean
389
393
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.Algebra.Ring.Divisibility.Lemmas import Mathlib.Algebra.Lie.Nilpotent import Mathlib.Algebra.Lie.Engel import Mathlib.LinearAlgebra.Eigenspace.Pi import Mathlib.RingTheory.Artinian.Module import Mathlib.LinearAlgebra.Trace import Mathlib.LinearAlgebra.FreeModule.PID /-! # Weight spaces of Lie modules of nilpotent Lie algebras Just as a key tool when studying the behaviour of a linear operator is to decompose the space on which it acts into a sum of (generalised) eigenspaces, a key tool when studying a representation `M` of Lie algebra `L` is to decompose `M` into a sum of simultaneous eigenspaces of `x` as `x` ranges over `L`. These simultaneous generalised eigenspaces are known as the weight spaces of `M`. When `L` is nilpotent, it follows from the binomial theorem that weight spaces are Lie submodules. Basic definitions and properties of the above ideas are provided in this file. ## Main definitions * `LieModule.genWeightSpaceOf` * `LieModule.genWeightSpace` * `LieModule.Weight` * `LieModule.posFittingCompOf` * `LieModule.posFittingComp` * `LieModule.iSup_ucs_eq_genWeightSpace_zero` * `LieModule.iInf_lowerCentralSeries_eq_posFittingComp` * `LieModule.isCompl_genWeightSpace_zero_posFittingComp` * `LieModule.iSupIndep_genWeightSpace` * `LieModule.iSup_genWeightSpace_eq_top` ## References * [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 7--9*](bourbaki1975b) ## Tags lie character, eigenvalue, eigenspace, weight, weight vector, root, root vector -/ variable {K R L M : Type*} [CommRing R] [LieRing L] [LieAlgebra R L] [AddCommGroup M] [Module R M] [LieRingModule L M] [LieModule R L M] namespace LieModule open Set Function TensorProduct LieModule variable (M) in /-- If `M` is a representation of a Lie algebra `L` and `χ : L → R` is a family of scalars, then `weightSpace M χ` is the intersection of the `χ x`-eigenspaces of the action of `x` on `M` as `x` ranges over `L`. -/ def weightSpace (χ : L → R) : LieSubmodule R L M where __ := ⨅ x : L, (toEnd R L M x).eigenspace (χ x) lie_mem {x m} hm := by simp_all [smul_comm (χ x)] lemma mem_weightSpace (χ : L → R) (m : M) : m ∈ weightSpace M χ ↔ ∀ x, ⁅x, m⁆ = χ x • m := by simp [weightSpace] section notation_genWeightSpaceOf /-- Until we define `LieModule.genWeightSpaceOf`, it is useful to have some notation as follows: -/ local notation3 "𝕎("M", " χ", " x")" => (toEnd R L M x).maxGenEigenspace χ /-- See also `bourbaki1975b` Chapter VII §1.1, Proposition 2 (ii). -/ protected theorem weight_vector_multiplication (M₁ M₂ M₃ : Type*) [AddCommGroup M₁] [Module R M₁] [LieRingModule L M₁] [LieModule R L M₁] [AddCommGroup M₂] [Module R M₂] [LieRingModule L M₂] [LieModule R L M₂] [AddCommGroup M₃] [Module R M₃] [LieRingModule L M₃] [LieModule R L M₃] (g : M₁ ⊗[R] M₂ →ₗ⁅R,L⁆ M₃) (χ₁ χ₂ : R) (x : L) : LinearMap.range ((g : M₁ ⊗[R] M₂ →ₗ[R] M₃).comp (mapIncl 𝕎(M₁, χ₁, x) 𝕎(M₂, χ₂, x))) ≤ 𝕎(M₃, χ₁ + χ₂, x) := by -- Unpack the statement of the goal. intro m₃ simp only [TensorProduct.mapIncl, LinearMap.mem_range, LinearMap.coe_comp, LieModuleHom.coe_toLinearMap, Function.comp_apply, Pi.add_apply, exists_imp, Module.End.mem_maxGenEigenspace] rintro t rfl -- Set up some notation. let F : Module.End R M₃ := toEnd R L M₃ x - (χ₁ + χ₂) • ↑1 -- The goal is linear in `t` so use induction to reduce to the case that `t` is a pure tensor. refine t.induction_on ?_ ?_ ?_ · use 0; simp only [LinearMap.map_zero, LieModuleHom.map_zero] swap · rintro t₁ t₂ ⟨k₁, hk₁⟩ ⟨k₂, hk₂⟩; use max k₁ k₂ simp only [LieModuleHom.map_add, LinearMap.map_add, Module.End.pow_map_zero_of_le (le_max_left k₁ k₂) hk₁, Module.End.pow_map_zero_of_le (le_max_right k₁ k₂) hk₂, add_zero] -- Now the main argument: pure tensors. rintro ⟨m₁, hm₁⟩ ⟨m₂, hm₂⟩ change ∃ k, (F ^ k) ((g : M₁ ⊗[R] M₂ →ₗ[R] M₃) (m₁ ⊗ₜ m₂)) = (0 : M₃) -- Eliminate `g` from the picture. let f₁ : Module.End R (M₁ ⊗[R] M₂) := (toEnd R L M₁ x - χ₁ • ↑1).rTensor M₂ let f₂ : Module.End R (M₁ ⊗[R] M₂) := (toEnd R L M₂ x - χ₂ • ↑1).lTensor M₁ have h_comm_square : F ∘ₗ ↑g = (g : M₁ ⊗[R] M₂ →ₗ[R] M₃).comp (f₁ + f₂) := by ext m₁ m₂ simp only [f₁, f₂, F, ← g.map_lie x (m₁ ⊗ₜ m₂), add_smul, sub_tmul, tmul_sub, smul_tmul, lie_tmul_right, tmul_smul, toEnd_apply_apply, LieModuleHom.map_smul, Module.End.one_apply, LieModuleHom.coe_toLinearMap, LinearMap.smul_apply, Function.comp_apply, LinearMap.coe_comp, LinearMap.rTensor_tmul, LieModuleHom.map_add, LinearMap.add_apply, LieModuleHom.map_sub, LinearMap.sub_apply, LinearMap.lTensor_tmul, AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, LinearMap.toFun_eq_coe, LinearMap.coe_restrictScalars] abel rsuffices ⟨k, hk⟩ : ∃ k : ℕ, ((f₁ + f₂) ^ k) (m₁ ⊗ₜ m₂) = 0 · use k change (F ^ k) (g.toLinearMap (m₁ ⊗ₜ[R] m₂)) = 0 rw [← LinearMap.comp_apply, Module.End.commute_pow_left_of_commute h_comm_square, LinearMap.comp_apply, hk, LinearMap.map_zero] -- Unpack the information we have about `m₁`, `m₂`. simp only [Module.End.mem_maxGenEigenspace] at hm₁ hm₂ obtain ⟨k₁, hk₁⟩ := hm₁ obtain ⟨k₂, hk₂⟩ := hm₂ have hf₁ : (f₁ ^ k₁) (m₁ ⊗ₜ m₂) = 0 := by simp only [f₁, hk₁, zero_tmul, LinearMap.rTensor_tmul, LinearMap.rTensor_pow] have hf₂ : (f₂ ^ k₂) (m₁ ⊗ₜ m₂) = 0 := by simp only [f₂, hk₂, tmul_zero, LinearMap.lTensor_tmul, LinearMap.lTensor_pow] -- It's now just an application of the binomial theorem. use k₁ + k₂ - 1 have hf_comm : Commute f₁ f₂ := by ext m₁ m₂ simp only [f₁, f₂, Module.End.mul_apply, LinearMap.rTensor_tmul, LinearMap.lTensor_tmul, AlgebraTensorModule.curry_apply, LinearMap.toFun_eq_coe, LinearMap.lTensor_tmul, TensorProduct.curry_apply, LinearMap.coe_restrictScalars] rw [hf_comm.add_pow'] simp only [TensorProduct.mapIncl, Submodule.subtype_apply, Finset.sum_apply, Submodule.coe_mk, LinearMap.coeFn_sum, TensorProduct.map_tmul, LinearMap.smul_apply] -- The required sum is zero because each individual term is zero. apply Finset.sum_eq_zero rintro ⟨i, j⟩ hij -- Eliminate the binomial coefficients from the picture. suffices (f₁ ^ i * f₂ ^ j) (m₁ ⊗ₜ m₂) = 0 by rw [this]; apply smul_zero -- Finish off with appropriate case analysis. rcases Nat.le_or_le_of_add_eq_add_pred (Finset.mem_antidiagonal.mp hij) with hi | hj · rw [(hf_comm.pow_pow i j).eq, Module.End.mul_apply, Module.End.pow_map_zero_of_le hi hf₁, LinearMap.map_zero] · rw [Module.End.mul_apply, Module.End.pow_map_zero_of_le hj hf₂, LinearMap.map_zero] lemma lie_mem_maxGenEigenspace_toEnd {χ₁ χ₂ : R} {x y : L} {m : M} (hy : y ∈ 𝕎(L, χ₁, x)) (hm : m ∈ 𝕎(M, χ₂, x)) : ⁅y, m⁆ ∈ 𝕎(M, χ₁ + χ₂, x) := by apply LieModule.weight_vector_multiplication L M M (toModuleHom R L M) χ₁ χ₂ simp only [LieModuleHom.coe_toLinearMap, Function.comp_apply, LinearMap.coe_comp, TensorProduct.mapIncl, LinearMap.mem_range] use ⟨y, hy⟩ ⊗ₜ ⟨m, hm⟩ simp only [Submodule.subtype_apply, toModuleHom_apply, TensorProduct.map_tmul] variable (M) /-- If `M` is a representation of a nilpotent Lie algebra `L`, `χ` is a scalar, and `x : L`, then `genWeightSpaceOf M χ x` is the maximal generalized `χ`-eigenspace of the action of `x` on `M`. It is a Lie submodule because `L` is nilpotent. -/ def genWeightSpaceOf [LieRing.IsNilpotent L] (χ : R) (x : L) : LieSubmodule R L M := { 𝕎(M, χ, x) with lie_mem := by intro y m hm simp only [AddSubsemigroup.mem_carrier, AddSubmonoid.mem_toSubsemigroup, Submodule.mem_toAddSubmonoid] at hm ⊢ rw [← zero_add χ] exact lie_mem_maxGenEigenspace_toEnd (by simp) hm } end notation_genWeightSpaceOf variable (M) variable [LieRing.IsNilpotent L] theorem mem_genWeightSpaceOf (χ : R) (x : L) (m : M) : m ∈ genWeightSpaceOf M χ x ↔ ∃ k : ℕ, ((toEnd R L M x - χ • ↑1) ^ k) m = 0 := by simp [genWeightSpaceOf] theorem coe_genWeightSpaceOf_zero (x : L) : ↑(genWeightSpaceOf M (0 : R) x) = ⨆ k, LinearMap.ker (toEnd R L M x ^ k) := by simp [genWeightSpaceOf, ← Module.End.iSup_genEigenspace_eq] /-- If `M` is a representation of a nilpotent Lie algebra `L` and `χ : L → R` is a family of scalars, then `genWeightSpace M χ` is the intersection of the maximal generalized `χ x`-eigenspaces of the action of `x` on `M` as `x` ranges over `L`. It is a Lie submodule because `L` is nilpotent. -/ def genWeightSpace (χ : L → R) : LieSubmodule R L M := ⨅ x, genWeightSpaceOf M (χ x) x theorem mem_genWeightSpace (χ : L → R) (m : M) : m ∈ genWeightSpace M χ ↔ ∀ x, ∃ k : ℕ, ((toEnd R L M x - χ x • ↑1) ^ k) m = 0 := by simp [genWeightSpace, mem_genWeightSpaceOf] lemma genWeightSpace_le_genWeightSpaceOf (x : L) (χ : L → R) : genWeightSpace M χ ≤ genWeightSpaceOf M (χ x) x := iInf_le _ x lemma weightSpace_le_genWeightSpace (χ : L → R) : weightSpace M χ ≤ genWeightSpace M χ := by apply le_iInf intro x rw [← (LieSubmodule.toSubmodule_orderEmbedding R L M).le_iff_le] apply (iInf_le _ x).trans exact ((toEnd R L M x).genEigenspace (χ x)).monotone le_top variable (R L) in /-- A weight of a Lie module is a map `L → R` such that the corresponding weight space is non-trivial. -/ structure Weight where /-- The family of eigenvalues corresponding to a weight. -/ toFun : L → R genWeightSpace_ne_bot' : genWeightSpace M toFun ≠ ⊥ namespace Weight instance instFunLike : FunLike (Weight R L M) L R where coe χ := χ.1 coe_injective' χ₁ χ₂ h := by cases χ₁; cases χ₂; simp_all @[simp] lemma coe_weight_mk (χ : L → R) (h) : (↑(⟨χ, h⟩ : Weight R L M) : L → R) = χ := rfl lemma genWeightSpace_ne_bot (χ : Weight R L M) : genWeightSpace M χ ≠ ⊥ := χ.genWeightSpace_ne_bot' variable {M} @[ext] lemma ext {χ₁ χ₂ : Weight R L M} (h : ∀ x, χ₁ x = χ₂ x) : χ₁ = χ₂ := by obtain ⟨f₁, _⟩ := χ₁; obtain ⟨f₂, _⟩ := χ₂; aesop lemma ext_iff' {χ₁ χ₂ : Weight R L M} : (χ₁ : L → R) = χ₂ ↔ χ₁ = χ₂ := by simp lemma exists_ne_zero (χ : Weight R L M) : ∃ x ∈ genWeightSpace M χ, x ≠ 0 := by simpa [LieSubmodule.eq_bot_iff] using χ.genWeightSpace_ne_bot instance [Subsingleton M] : IsEmpty (Weight R L M) := ⟨fun h ↦ h.2 (Subsingleton.elim _ _)⟩ instance [Nontrivial (genWeightSpace M (0 : L → R))] : Zero (Weight R L M) := ⟨0, fun e ↦ not_nontrivial (⊥ : LieSubmodule R L M) (e ▸ ‹_›)⟩ @[simp] lemma coe_zero [Nontrivial (genWeightSpace M (0 : L → R))] : ((0 : Weight R L M) : L → R) = 0 := rfl lemma zero_apply [Nontrivial (genWeightSpace M (0 : L → R))] (x) : (0 : Weight R L M) x = 0 := rfl /-- The proposition that a weight of a Lie module is zero. We make this definition because we cannot define a `Zero (Weight R L M)` instance since the weight space of the zero function can be trivial. -/ def IsZero (χ : Weight R L M) := (χ : L → R) = 0 @[simp] lemma IsZero.eq {χ : Weight R L M} (hχ : χ.IsZero) : (χ : L → R) = 0 := hχ @[simp] lemma coe_eq_zero_iff (χ : Weight R L M) : (χ : L → R) = 0 ↔ χ.IsZero := Iff.rfl lemma isZero_iff_eq_zero [Nontrivial (genWeightSpace M (0 : L → R))] {χ : Weight R L M} : χ.IsZero ↔ χ = 0 := Weight.ext_iff' (χ₂ := 0) lemma isZero_zero [Nontrivial (genWeightSpace M (0 : L → R))] : IsZero (0 : Weight R L M) := rfl /-- The proposition that a weight of a Lie module is non-zero. -/ abbrev IsNonZero (χ : Weight R L M) := ¬ IsZero (χ : Weight R L M) lemma isNonZero_iff_ne_zero [Nontrivial (genWeightSpace M (0 : L → R))] {χ : Weight R L M} : χ.IsNonZero ↔ χ ≠ 0 := isZero_iff_eq_zero.not noncomputable instance : DecidablePred (IsNonZero (R := R) (L := L) (M := M)) := Classical.decPred _ variable (R L M) in /-- The set of weights is equivalent to a subtype. -/ def equivSetOf : Weight R L M ≃ {χ : L → R | genWeightSpace M χ ≠ ⊥} where toFun w := ⟨w.1, w.2⟩ invFun w := ⟨w.1, w.2⟩ left_inv w := by simp right_inv w := by simp lemma genWeightSpaceOf_ne_bot (χ : Weight R L M) (x : L) : genWeightSpaceOf M (χ x) x ≠ ⊥ := by have : ⨅ x, genWeightSpaceOf M (χ x) x ≠ ⊥ := χ.genWeightSpace_ne_bot contrapose! this rw [eq_bot_iff] exact le_of_le_of_eq (iInf_le _ _) this lemma hasEigenvalueAt (χ : Weight R L M) (x : L) : (toEnd R L M x).HasEigenvalue (χ x) := by obtain ⟨k : ℕ, hk : (toEnd R L M x).genEigenspace (χ x) k ≠ ⊥⟩ := by simpa [genWeightSpaceOf, ← Module.End.iSup_genEigenspace_eq] using χ.genWeightSpaceOf_ne_bot x exact Module.End.hasEigenvalue_of_hasGenEigenvalue hk lemma apply_eq_zero_of_isNilpotent [NoZeroSMulDivisors R M] [IsReduced R] (x : L) (h : _root_.IsNilpotent (toEnd R L M x)) (χ : Weight R L M) : χ x = 0 := ((χ.hasEigenvalueAt x).isNilpotent_of_isNilpotent h).eq_zero end Weight /-- See also the more useful form `LieModule.zero_genWeightSpace_eq_top_of_nilpotent`. -/ @[simp] theorem zero_genWeightSpace_eq_top_of_nilpotent' [IsNilpotent L M] : genWeightSpace M (0 : L → R) = ⊤ := by ext simp [genWeightSpace, genWeightSpaceOf] theorem coe_genWeightSpace_of_top (χ : L → R) : (genWeightSpace M (χ ∘ (⊤ : LieSubalgebra R L).incl) : Submodule R M) = genWeightSpace M χ := by ext m simp only [mem_genWeightSpace, LieSubmodule.mem_toSubmodule, Subtype.forall] apply forall_congr' simp @[simp] theorem zero_genWeightSpace_eq_top_of_nilpotent [IsNilpotent L M] : genWeightSpace M (0 : (⊤ : LieSubalgebra R L) → R) = ⊤ := by ext m simp only [mem_genWeightSpace, Pi.zero_apply, zero_smul, sub_zero, Subtype.forall, forall_true_left, LieSubalgebra.toEnd_mk, LieSubalgebra.mem_top, LieSubmodule.mem_top, iff_true] intro x obtain ⟨k, hk⟩ := exists_forall_pow_toEnd_eq_zero R L M exact ⟨k, by simp [hk x]⟩ theorem exists_genWeightSpace_le_ker_of_isNoetherian [IsNoetherian R M] (χ : L → R) (x : L) : ∃ k : ℕ, genWeightSpace M χ ≤ LinearMap.ker ((toEnd R L M x - algebraMap R _ (χ x)) ^ k) := by use (toEnd R L M x).maxGenEigenspaceIndex (χ x) intro m hm replace hm : m ∈ (toEnd R L M x).maxGenEigenspace (χ x) := genWeightSpace_le_genWeightSpaceOf M x χ hm rwa [Module.End.maxGenEigenspace_eq, Module.End.genEigenspace_nat] at hm variable (R) in theorem exists_genWeightSpace_zero_le_ker_of_isNoetherian [IsNoetherian R M] (x : L) : ∃ k : ℕ, genWeightSpace M (0 : L → R) ≤ LinearMap.ker (toEnd R L M x ^ k) := by simpa using exists_genWeightSpace_le_ker_of_isNoetherian M (0 : L → R) x lemma isNilpotent_toEnd_sub_algebraMap [IsNoetherian R M] (χ : L → R) (x : L) : _root_.IsNilpotent <| toEnd R L (genWeightSpace M χ) x - algebraMap R _ (χ x) := by have : toEnd R L (genWeightSpace M χ) x - algebraMap R _ (χ x) = (toEnd R L M x - algebraMap R _ (χ x)).restrict (fun m hm ↦ sub_mem (LieSubmodule.lie_mem _ hm) (Submodule.smul_mem _ _ hm)) := by rfl obtain ⟨k, hk⟩ := exists_genWeightSpace_le_ker_of_isNoetherian M χ x use k ext ⟨m, hm⟩ simp only [this, Module.End.pow_restrict _, LinearMap.zero_apply, ZeroMemClass.coe_zero, ZeroMemClass.coe_eq_zero] exact ZeroMemClass.coe_eq_zero.mp (hk hm) /-- A (nilpotent) Lie algebra acts nilpotently on the zero weight space of a Noetherian Lie module. -/ theorem isNilpotent_toEnd_genWeightSpace_zero [IsNoetherian R M] (x : L) : _root_.IsNilpotent <| toEnd R L (genWeightSpace M (0 : L → R)) x := by simpa using isNilpotent_toEnd_sub_algebraMap M (0 : L → R) x /-- By Engel's theorem, the zero weight space of a Noetherian Lie module is nilpotent. -/ instance [IsNoetherian R M] : IsNilpotent L (genWeightSpace M (0 : L → R)) := isNilpotent_iff_forall'.mpr <| isNilpotent_toEnd_genWeightSpace_zero M variable (R L) @[simp] lemma genWeightSpace_zero_normalizer_eq_self : (genWeightSpace M (0 : L → R)).normalizer = genWeightSpace M 0 := by refine le_antisymm ?_ (LieSubmodule.le_normalizer _) intro m hm rw [LieSubmodule.mem_normalizer] at hm simp only [mem_genWeightSpace, Pi.zero_apply, zero_smul, sub_zero] at hm ⊢ intro y obtain ⟨k, hk⟩ := hm y y use k + 1 simpa [pow_succ, Module.End.mul_eq_comp] lemma iSup_ucs_le_genWeightSpace_zero : ⨆ k, (⊥ : LieSubmodule R L M).ucs k ≤ genWeightSpace M (0 : L → R) := by simpa using LieSubmodule.ucs_le_of_normalizer_eq_self (genWeightSpace_zero_normalizer_eq_self R L M) /-- See also `LieModule.iInf_lowerCentralSeries_eq_posFittingComp`. -/ lemma iSup_ucs_eq_genWeightSpace_zero [IsNoetherian R M] : ⨆ k, (⊥ : LieSubmodule R L M).ucs k = genWeightSpace M (0 : L → R) := by obtain ⟨k, hk⟩ := (LieSubmodule.isNilpotent_iff_exists_self_le_ucs <| genWeightSpace M (0 : L → R)).mp inferInstance refine le_antisymm (iSup_ucs_le_genWeightSpace_zero R L M) (le_trans hk ?_) exact le_iSup (fun k ↦ (⊥ : LieSubmodule R L M).ucs k) k variable {L} /-- If `M` is a representation of a nilpotent Lie algebra `L`, and `x : L`, then `posFittingCompOf R M x` is the infimum of the decreasing system `range φₓ ⊇ range φₓ² ⊇ range φₓ³ ⊇ ⋯` where `φₓ : End R M := toEnd R L M x`. We call this the "positive Fitting component" because with appropriate assumptions (e.g., `R` is a field and `M` is finite-dimensional) `φₓ` induces the so-called Fitting decomposition: `M = M₀ ⊕ M₁` where `M₀ = genWeightSpaceOf M 0 x` and `M₁ = posFittingCompOf R M x`. It is a Lie submodule because `L` is nilpotent. -/ def posFittingCompOf (x : L) : LieSubmodule R L M := { toSubmodule := ⨅ k, LinearMap.range (toEnd R L M x ^ k) lie_mem := by set φ := toEnd R L M x intros y m hm simp only [AddSubsemigroup.mem_carrier, AddSubmonoid.mem_toSubsemigroup, Submodule.mem_toAddSubmonoid, Submodule.mem_iInf, LinearMap.mem_range] at hm ⊢ intro k obtain ⟨N, hN⟩ := LieAlgebra.nilpotent_ad_of_nilpotent_algebra R L obtain ⟨m, rfl⟩ := hm (N + k) let f₁ : Module.End R (L ⊗[R] M) := (LieAlgebra.ad R L x).rTensor M let f₂ : Module.End R (L ⊗[R] M) := φ.lTensor L replace hN : f₁ ^ N = 0 := by ext; simp [f₁, hN] have h₁ : Commute f₁ f₂ := by ext; simp [f₁, f₂] have h₂ : φ ∘ₗ toModuleHom R L M = toModuleHom R L M ∘ₗ (f₁ + f₂) := by ext; simp [φ, f₁, f₂] obtain ⟨q, hq⟩ := h₁.add_pow_dvd_pow_of_pow_eq_zero_right (N + k).le_succ hN use toModuleHom R L M (q (y ⊗ₜ m)) change (φ ^ k).comp ((toModuleHom R L M : L ⊗[R] M →ₗ[R] M)) _ = _ simp [φ, f₁, f₂, Module.End.commute_pow_left_of_commute h₂, LinearMap.comp_apply (g := (f₁ + f₂) ^ k), ← LinearMap.comp_apply (g := q), ← Module.End.mul_eq_comp, ← hq] } variable {M} in lemma mem_posFittingCompOf (x : L) (m : M) : m ∈ posFittingCompOf R M x ↔ ∀ (k : ℕ), ∃ n, (toEnd R L M x ^ k) n = m := by simp [posFittingCompOf] @[simp] lemma posFittingCompOf_le_lowerCentralSeries (x : L) (k : ℕ) : posFittingCompOf R M x ≤ lowerCentralSeries R L M k := by suffices ∀ m l, (toEnd R L M x ^ l) m ∈ lowerCentralSeries R L M l by intro m hm obtain ⟨n, rfl⟩ := (mem_posFittingCompOf R x m).mp hm k exact this n k intro m l induction l with | zero => simp | succ l ih => simp only [lowerCentralSeries_succ, pow_succ', Module.End.mul_apply] exact LieSubmodule.lie_mem_lie (LieSubmodule.mem_top x) ih @[simp] lemma posFittingCompOf_eq_bot_of_isNilpotent [IsNilpotent L M] (x : L) : posFittingCompOf R M x = ⊥ := by simp_rw [eq_bot_iff, ← iInf_lowerCentralSeries_eq_bot_of_isNilpotent, le_iInf_iff, posFittingCompOf_le_lowerCentralSeries, forall_const] variable (L) /-- If `M` is a representation of a nilpotent Lie algebra `L` with coefficients in `R`, then `posFittingComp R L M` is the span of the positive Fitting components of the action of `x` on `M`, as `x` ranges over `L`. It is a Lie submodule because `L` is nilpotent. -/ def posFittingComp : LieSubmodule R L M := ⨆ x, posFittingCompOf R M x lemma mem_posFittingComp (m : M) : m ∈ posFittingComp R L M ↔ m ∈ ⨆ (x : L), posFittingCompOf R M x := by rfl lemma posFittingCompOf_le_posFittingComp (x : L) : posFittingCompOf R M x ≤ posFittingComp R L M := by rw [posFittingComp]; exact le_iSup (posFittingCompOf R M) x lemma posFittingComp_le_iInf_lowerCentralSeries : posFittingComp R L M ≤ ⨅ k, lowerCentralSeries R L M k := by simp [posFittingComp] /-- See also `LieModule.iSup_ucs_eq_genWeightSpace_zero`. -/ @[simp] lemma iInf_lowerCentralSeries_eq_posFittingComp [IsNoetherian R M] [IsArtinian R M] : ⨅ k, lowerCentralSeries R L M k = posFittingComp R L M := by refine le_antisymm ?_ (posFittingComp_le_iInf_lowerCentralSeries R L M) apply iInf_lcs_le_of_isNilpotent_quot rw [LieModule.isNilpotent_iff_forall' (R := R)] intro x obtain ⟨k, hk⟩ := Filter.eventually_atTop.mp (toEnd R L M x).eventually_iInf_range_pow_eq use k ext ⟨m⟩ set F := posFittingComp R L M replace hk : (toEnd R L M x ^ k) m ∈ F := by apply posFittingCompOf_le_posFittingComp R L M x
simp_rw [← LieSubmodule.mem_toSubmodule, posFittingCompOf, hk k (le_refl k)] apply LinearMap.mem_range_self suffices (toEnd R L (M ⧸ F) x ^ k) (LieSubmodule.Quotient.mk (N := F) m) = LieSubmodule.Quotient.mk (N := F) ((toEnd R L M x ^ k) m) by simpa [Submodule.Quotient.quot_mk_eq_mk, this] have := LinearMap.congr_fun (Module.End.commute_pow_left_of_commute (LieSubmodule.Quotient.toEnd_comp_mk' F x) k) m simpa using this @[simp] lemma posFittingComp_eq_bot_of_isNilpotent
Mathlib/Algebra/Lie/Weights/Basic.lean
481
490
/- Copyright (c) 2022 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import Mathlib.GroupTheory.Complement import Mathlib.GroupTheory.Sylow /-! # The Transfer Homomorphism In this file we construct the transfer homomorphism. ## Main definitions - `diff ϕ S T` : The difference of two left transversals `S` and `T` under the homomorphism `ϕ`. - `transfer ϕ` : The transfer homomorphism induced by `ϕ`. - `transferCenterPow`: The transfer homomorphism `G →* center G`. ## Main results - `transferCenterPow_apply`: The transfer homomorphism `G →* center G` is given by `g ↦ g ^ (center G).index`. - `ker_transferSylow_isComplement'`: Burnside's transfer (or normal `p`-complement) theorem: If `hP : N(P) ≤ C(P)`, then `(transfer P hP).ker` is a normal `p`-complement. -/ variable {G : Type*} [Group G] {H : Subgroup G} {A : Type*} [CommGroup A] (ϕ : H →* A) namespace Subgroup namespace leftTransversals open Finset MulAction open scoped Pointwise variable (R S T : H.LeftTransversal) [FiniteIndex H] /-- The difference of two left transversals -/ @[to_additive "The difference of two left transversals"] noncomputable def diff : A := let α := S.2.leftQuotientEquiv let β := T.2.leftQuotientEquiv let _ := H.fintypeQuotientOfFiniteIndex ∏ q : G ⧸ H, ϕ ⟨(α q : G)⁻¹ * β q, QuotientGroup.leftRel_apply.mp <| Quotient.exact' ((α.symm_apply_apply q).trans (β.symm_apply_apply q).symm)⟩ @[to_additive] theorem diff_mul_diff : diff ϕ R S * diff ϕ S T = diff ϕ R T := prod_mul_distrib.symm.trans (prod_congr rfl fun q _ => (ϕ.map_mul _ _).symm.trans (congr_arg ϕ (by simp_rw [Subtype.ext_iff, coe_mul, mul_assoc, mul_inv_cancel_left]))) @[to_additive] theorem diff_self : diff ϕ T T = 1 := mul_eq_left.mp (diff_mul_diff ϕ T T T) @[to_additive] theorem diff_inv : (diff ϕ S T)⁻¹ = diff ϕ T S := inv_eq_of_mul_eq_one_right <| (diff_mul_diff ϕ S T S).trans <| diff_self ϕ S @[to_additive] theorem smul_diff_smul (g : G) : diff ϕ (g • S) (g • T) = diff ϕ S T := let _ := H.fintypeQuotientOfFiniteIndex Fintype.prod_equiv (MulAction.toPerm g).symm _ _ fun _ ↦ by simp only [smul_apply_eq_smul_apply_inv_smul, smul_eq_mul, mul_inv_rev, mul_assoc, inv_mul_cancel_left, toPerm_symm_apply] end leftTransversals open Equiv Function MemLeftTransversals MulAction ZMod variable (g : G) variable (H) in /-- The transfer transversal as a function. Given a `⟨g⟩`-orbit `q₀, g • q₀, ..., g ^ (m - 1) • q₀` in `G ⧸ H`, an element `g ^ k • q₀` is mapped to `g ^ k • g₀` for a fixed choice of representative `g₀` of `q₀`. -/ noncomputable def transferFunction : G ⧸ H → G := fun q => g ^ (cast (quotientEquivSigmaZMod H g q).2 : ℤ) * (quotientEquivSigmaZMod H g q).1.out.out lemma transferFunction_apply (q : G ⧸ H) : transferFunction H g q = g ^ (cast (quotientEquivSigmaZMod H g q).2 : ℤ) * (quotientEquivSigmaZMod H g q).1.out.out := rfl lemma coe_transferFunction (q : G ⧸ H) : ↑(transferFunction H g q) = q := by rw [transferFunction_apply, ← smul_eq_mul, Quotient.coe_smul_out, ← quotientEquivSigmaZMod_symm_apply, Sigma.eta, symm_apply_apply] variable (H) in /-- The transfer transversal as a set. Contains elements of the form `g ^ k • g₀` for fixed choices of representatives `g₀` of fixed choices of representatives `q₀` of `⟨g⟩`-orbits in `G ⧸ H`. -/ def transferSet : Set G := Set.range (transferFunction H g) lemma mem_transferSet (q : G ⧸ H) : transferFunction H g q ∈ transferSet H g := ⟨q, rfl⟩ variable (H) in /-- The transfer transversal. Contains elements of the form `g ^ k • g₀` for fixed choices of representatives `g₀` of fixed choices of representatives `q₀` of `⟨g⟩`-orbits in `G ⧸ H`. -/ def transferTransversal : H.LeftTransversal := ⟨transferSet H g, isComplement_range_left (coe_transferFunction g)⟩ lemma transferTransversal_apply (q : G ⧸ H) : ↑((transferTransversal H g).2.leftQuotientEquiv q) = transferFunction H g q := IsComplement.leftQuotientEquiv_apply (coe_transferFunction g) q lemma transferTransversal_apply' (q : orbitRel.Quotient (zpowers g) (G ⧸ H)) (k : ZMod (minimalPeriod (g • ·) q.out)) : ↑((transferTransversal H g).2.leftQuotientEquiv (g ^ (cast k : ℤ) • q.out)) = g ^ (cast k : ℤ) * q.out.out := by rw [transferTransversal_apply, transferFunction_apply, ← quotientEquivSigmaZMod_symm_apply, apply_symm_apply] lemma transferTransversal_apply'' (q : orbitRel.Quotient (zpowers g) (G ⧸ H)) (k : ZMod (minimalPeriod (g • ·) q.out)) : ↑((g • transferTransversal H g).2.leftQuotientEquiv (g ^ (cast k : ℤ) • q.out)) = if k = 0 then g ^ minimalPeriod (g • ·) q.out * q.out.out else g ^ (cast k : ℤ) * q.out.out := by rw [smul_apply_eq_smul_apply_inv_smul, transferTransversal_apply, transferFunction_apply, ← mul_smul, ← zpow_neg_one, ← zpow_add, quotientEquivSigmaZMod_apply, smul_eq_mul, ← mul_assoc, ← zpow_one_add, Int.cast_add, Int.cast_neg, Int.cast_one, intCast_cast, cast_id', id, ← sub_eq_neg_add, cast_sub_one, add_sub_cancel] by_cases hk : k = 0 · rw [if_pos hk, if_pos hk, zpow_natCast] · rw [if_neg hk, if_neg hk] end Subgroup namespace MonoidHom open MulAction Subgroup Subgroup.leftTransversals /-- Given `ϕ : H →* A` from `H : Subgroup G` to a commutative group `A`, the transfer homomorphism is `transfer ϕ : G →* A`. -/ @[to_additive "Given `ϕ : H →+ A` from `H : AddSubgroup G` to an additive commutative group `A`, the transfer homomorphism is `transfer ϕ : G →+ A`."] noncomputable def transfer [FiniteIndex H] : G →* A := let T : H.LeftTransversal := default { toFun := fun g => diff ϕ T (g • T) map_one' := by rw [one_smul, diff_self] map_mul' := fun g h => by rw [mul_smul, ← diff_mul_diff, smul_diff_smul] } variable (T : H.LeftTransversal) @[to_additive] theorem transfer_def [FiniteIndex H] (g : G) : transfer ϕ g = diff ϕ T (g • T) := by rw [transfer, ← diff_mul_diff, ← smul_diff_smul, mul_comm, diff_mul_diff] <;> rfl /-- Explicit computation of the transfer homomorphism. -/ theorem transfer_eq_prod_quotient_orbitRel_zpowers_quot [FiniteIndex H] (g : G) [Fintype (Quotient (orbitRel (zpowers g) (G ⧸ H)))] : transfer ϕ g = ∏ q : Quotient (orbitRel (zpowers g) (G ⧸ H)), ϕ ⟨q.out.out⁻¹ * g ^ Function.minimalPeriod (g • ·) q.out * q.out.out, QuotientGroup.out_conj_pow_minimalPeriod_mem H g q.out⟩ := by classical letI := H.fintypeQuotientOfFiniteIndex calc transfer ϕ g = ∏ q : G ⧸ H, _ := transfer_def ϕ (transferTransversal H g) g _ = _ := ((quotientEquivSigmaZMod H g).symm.prod_comp _).symm _ = _ := Finset.prod_sigma _ _ _ _ = _ := by refine Fintype.prod_congr _ _ (fun q => ?_) simp only [quotientEquivSigmaZMod_symm_apply, transferTransversal_apply', transferTransversal_apply''] rw [Fintype.prod_eq_single (0 : ZMod (Function.minimalPeriod (g • ·) q.out)) _] · simp only [if_pos, ZMod.cast_zero, zpow_zero, one_mul, mul_assoc] · intro k hk simp only [if_neg hk, inv_mul_cancel] exact map_one ϕ /-- Auxiliary lemma in order to state `transfer_eq_pow`. -/ theorem transfer_eq_pow_aux (g : G) (key : ∀ (k : ℕ) (g₀ : G), g₀⁻¹ * g ^ k * g₀ ∈ H → g₀⁻¹ * g ^ k * g₀ = g ^ k) : g ^ H.index ∈ H := by by_cases hH : H.index = 0
· rw [hH, pow_zero] exact H.one_mem letI := fintypeOfIndexNeZero hH classical
Mathlib/GroupTheory/Transfer.lean
184
187
/- Copyright (c) 2019 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo, Yaël Dillies, Moritz Doll -/ import Mathlib.Algebra.Order.Pi import Mathlib.Analysis.Convex.Function import Mathlib.Analysis.LocallyConvex.Basic import Mathlib.Data.Real.Pointwise /-! # Seminorms This file defines seminorms. A seminorm is a function to the reals which is positive-semidefinite, absolutely homogeneous, and subadditive. They are closely related to convex sets, and a topological vector space is locally convex if and only if its topology is induced by a family of seminorms. ## Main declarations For a module over a normed ring: * `Seminorm`: A function to the reals that is positive-semidefinite, absolutely homogeneous, and subadditive. * `normSeminorm 𝕜 E`: The norm on `E` as a seminorm. ## References * [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966] ## Tags seminorm, locally convex, LCTVS -/ assert_not_exists balancedCore open NormedField Set Filter open scoped NNReal Pointwise Topology Uniformity variable {R R' 𝕜 𝕜₂ 𝕜₃ 𝕝 E E₂ E₃ F ι : Type*} /-- A seminorm on a module over a normed ring is a function to the reals that is positive semidefinite, positive homogeneous, and subadditive. -/ structure Seminorm (𝕜 : Type*) (E : Type*) [SeminormedRing 𝕜] [AddGroup E] [SMul 𝕜 E] extends AddGroupSeminorm E where /-- The seminorm of a scalar multiplication is the product of the absolute value of the scalar and the original seminorm. -/ smul' : ∀ (a : 𝕜) (x : E), toFun (a • x) = ‖a‖ * toFun x attribute [nolint docBlame] Seminorm.toAddGroupSeminorm /-- `SeminormClass F 𝕜 E` states that `F` is a type of seminorms on the `𝕜`-module `E`. You should extend this class when you extend `Seminorm`. -/ class SeminormClass (F : Type*) (𝕜 E : outParam Type*) [SeminormedRing 𝕜] [AddGroup E] [SMul 𝕜 E] [FunLike F E ℝ] : Prop extends AddGroupSeminormClass F E ℝ where /-- The seminorm of a scalar multiplication is the product of the absolute value of the scalar and the original seminorm. -/ map_smul_eq_mul (f : F) (a : 𝕜) (x : E) : f (a • x) = ‖a‖ * f x export SeminormClass (map_smul_eq_mul) section Of /-- Alternative constructor for a `Seminorm` on an `AddCommGroup E` that is a module over a `SeminormedRing 𝕜`. -/ def Seminorm.of [SeminormedRing 𝕜] [AddCommGroup E] [Module 𝕜 E] (f : E → ℝ) (add_le : ∀ x y : E, f (x + y) ≤ f x + f y) (smul : ∀ (a : 𝕜) (x : E), f (a • x) = ‖a‖ * f x) : Seminorm 𝕜 E where toFun := f map_zero' := by rw [← zero_smul 𝕜 (0 : E), smul, norm_zero, zero_mul] add_le' := add_le smul' := smul neg' x := by rw [← neg_one_smul 𝕜, smul, norm_neg, ← smul, one_smul] /-- Alternative constructor for a `Seminorm` over a normed field `𝕜` that only assumes `f 0 = 0` and an inequality for the scalar multiplication. -/ def Seminorm.ofSMulLE [NormedField 𝕜] [AddCommGroup E] [Module 𝕜 E] (f : E → ℝ) (map_zero : f 0 = 0) (add_le : ∀ x y, f (x + y) ≤ f x + f y) (smul_le : ∀ (r : 𝕜) (x), f (r • x) ≤ ‖r‖ * f x) : Seminorm 𝕜 E := Seminorm.of f add_le fun r x => by refine le_antisymm (smul_le r x) ?_ by_cases h : r = 0 · simp [h, map_zero] rw [← mul_le_mul_left (inv_pos.mpr (norm_pos_iff.mpr h))] rw [inv_mul_cancel_left₀ (norm_ne_zero_iff.mpr h)] specialize smul_le r⁻¹ (r • x) rw [norm_inv] at smul_le convert smul_le simp [h] end Of namespace Seminorm section SeminormedRing variable [SeminormedRing 𝕜] section AddGroup variable [AddGroup E] section SMul variable [SMul 𝕜 E] instance instFunLike : FunLike (Seminorm 𝕜 E) E ℝ where coe f := f.toFun coe_injective' f g h := by rcases f with ⟨⟨_⟩⟩ rcases g with ⟨⟨_⟩⟩ congr instance instSeminormClass : SeminormClass (Seminorm 𝕜 E) 𝕜 E where map_zero f := f.map_zero' map_add_le_add f := f.add_le' map_neg_eq_map f := f.neg' map_smul_eq_mul f := f.smul' @[ext] theorem ext {p q : Seminorm 𝕜 E} (h : ∀ x, (p : E → ℝ) x = q x) : p = q := DFunLike.ext p q h instance instZero : Zero (Seminorm 𝕜 E) := ⟨{ AddGroupSeminorm.instZeroAddGroupSeminorm.zero with smul' := fun _ _ => (mul_zero _).symm }⟩ @[simp] theorem coe_zero : ⇑(0 : Seminorm 𝕜 E) = 0 := rfl @[simp] theorem zero_apply (x : E) : (0 : Seminorm 𝕜 E) x = 0 := rfl instance : Inhabited (Seminorm 𝕜 E) := ⟨0⟩ variable (p : Seminorm 𝕜 E) (x : E) (r : ℝ) /-- Any action on `ℝ` which factors through `ℝ≥0` applies to a seminorm. -/ instance instSMul [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : SMul R (Seminorm 𝕜 E) where smul r p := { r • p.toAddGroupSeminorm with toFun := fun x => r • p x smul' := fun _ _ => by simp only [← smul_one_smul ℝ≥0 r (_ : ℝ), NNReal.smul_def, smul_eq_mul] rw [map_smul_eq_mul, mul_left_comm] } instance [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] [SMul R' ℝ] [SMul R' ℝ≥0] [IsScalarTower R' ℝ≥0 ℝ] [SMul R R'] [IsScalarTower R R' ℝ] : IsScalarTower R R' (Seminorm 𝕜 E) where smul_assoc r a p := ext fun x => smul_assoc r a (p x) theorem coe_smul [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p : Seminorm 𝕜 E) : ⇑(r • p) = r • ⇑p := rfl @[simp] theorem smul_apply [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p : Seminorm 𝕜 E) (x : E) : (r • p) x = r • p x := rfl instance instAdd : Add (Seminorm 𝕜 E) where add p q := { p.toAddGroupSeminorm + q.toAddGroupSeminorm with toFun := fun x => p x + q x smul' := fun a x => by simp only [map_smul_eq_mul, map_smul_eq_mul, mul_add] } theorem coe_add (p q : Seminorm 𝕜 E) : ⇑(p + q) = p + q := rfl @[simp] theorem add_apply (p q : Seminorm 𝕜 E) (x : E) : (p + q) x = p x + q x := rfl instance instAddMonoid : AddMonoid (Seminorm 𝕜 E) := DFunLike.coe_injective.addMonoid _ rfl coe_add fun _ _ => by rfl instance instAddCommMonoid : AddCommMonoid (Seminorm 𝕜 E) := DFunLike.coe_injective.addCommMonoid _ rfl coe_add fun _ _ => by rfl instance instPartialOrder : PartialOrder (Seminorm 𝕜 E) := PartialOrder.lift _ DFunLike.coe_injective instance instIsOrderedCancelAddMonoid : IsOrderedCancelAddMonoid (Seminorm 𝕜 E) := DFunLike.coe_injective.isOrderedCancelAddMonoid _ rfl coe_add fun _ _ => rfl instance instMulAction [Monoid R] [MulAction R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : MulAction R (Seminorm 𝕜 E) := DFunLike.coe_injective.mulAction _ (by intros; rfl) variable (𝕜 E) /-- `coeFn` as an `AddMonoidHom`. Helper definition for showing that `Seminorm 𝕜 E` is a module. -/ @[simps] def coeFnAddMonoidHom : AddMonoidHom (Seminorm 𝕜 E) (E → ℝ) where toFun := (↑) map_zero' := coe_zero map_add' := coe_add theorem coeFnAddMonoidHom_injective : Function.Injective (coeFnAddMonoidHom 𝕜 E) := show @Function.Injective (Seminorm 𝕜 E) (E → ℝ) (↑) from DFunLike.coe_injective variable {𝕜 E} instance instDistribMulAction [Monoid R] [DistribMulAction R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : DistribMulAction R (Seminorm 𝕜 E) := (coeFnAddMonoidHom_injective 𝕜 E).distribMulAction _ (by intros; rfl) instance instModule [Semiring R] [Module R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] : Module R (Seminorm 𝕜 E) := (coeFnAddMonoidHom_injective 𝕜 E).module R _ (by intros; rfl) instance instSup : Max (Seminorm 𝕜 E) where max p q := { p.toAddGroupSeminorm ⊔ q.toAddGroupSeminorm with toFun := p ⊔ q smul' := fun x v => (congr_arg₂ max (map_smul_eq_mul p x v) (map_smul_eq_mul q x v)).trans <| (mul_max_of_nonneg _ _ <| norm_nonneg x).symm } @[simp] theorem coe_sup (p q : Seminorm 𝕜 E) : ⇑(p ⊔ q) = (p : E → ℝ) ⊔ (q : E → ℝ) := rfl theorem sup_apply (p q : Seminorm 𝕜 E) (x : E) : (p ⊔ q) x = p x ⊔ q x := rfl theorem smul_sup [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p q : Seminorm 𝕜 E) : r • (p ⊔ q) = r • p ⊔ r • q := have real.smul_max : ∀ x y : ℝ, r • max x y = max (r • x) (r • y) := fun x y => by simpa only [← smul_eq_mul, ← NNReal.smul_def, smul_one_smul ℝ≥0 r (_ : ℝ)] using mul_max_of_nonneg x y (r • (1 : ℝ≥0) : ℝ≥0).coe_nonneg ext fun _ => real.smul_max _ _ @[simp, norm_cast] theorem coe_le_coe {p q : Seminorm 𝕜 E} : (p : E → ℝ) ≤ q ↔ p ≤ q := Iff.rfl @[simp, norm_cast] theorem coe_lt_coe {p q : Seminorm 𝕜 E} : (p : E → ℝ) < q ↔ p < q := Iff.rfl theorem le_def {p q : Seminorm 𝕜 E} : p ≤ q ↔ ∀ x, p x ≤ q x := Iff.rfl theorem lt_def {p q : Seminorm 𝕜 E} : p < q ↔ p ≤ q ∧ ∃ x, p x < q x := @Pi.lt_def _ _ _ p q instance instSemilatticeSup : SemilatticeSup (Seminorm 𝕜 E) := Function.Injective.semilatticeSup _ DFunLike.coe_injective coe_sup end SMul end AddGroup section Module variable [SeminormedRing 𝕜₂] [SeminormedRing 𝕜₃] variable {σ₁₂ : 𝕜 →+* 𝕜₂} [RingHomIsometric σ₁₂] variable {σ₂₃ : 𝕜₂ →+* 𝕜₃} [RingHomIsometric σ₂₃] variable {σ₁₃ : 𝕜 →+* 𝕜₃} [RingHomIsometric σ₁₃] variable [AddCommGroup E] [AddCommGroup E₂] [AddCommGroup E₃] variable [Module 𝕜 E] [Module 𝕜₂ E₂] [Module 𝕜₃ E₃] variable [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] /-- Composition of a seminorm with a linear map is a seminorm. -/ def comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) : Seminorm 𝕜 E := { p.toAddGroupSeminorm.comp f.toAddMonoidHom with toFun := fun x => p (f x) -- Porting note: the `simp only` below used to be part of the `rw`. -- I'm not sure why this change was needed, and am worried by it! -- Note: https://github.com/leanprover-community/mathlib4/pull/8386 had to change `map_smulₛₗ` to `map_smulₛₗ _` smul' := fun _ _ => by simp only [map_smulₛₗ _]; rw [map_smul_eq_mul, RingHomIsometric.is_iso] } theorem coe_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) : ⇑(p.comp f) = p ∘ f := rfl @[simp] theorem comp_apply (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (x : E) : (p.comp f) x = p (f x) := rfl @[simp] theorem comp_id (p : Seminorm 𝕜 E) : p.comp LinearMap.id = p := ext fun _ => rfl @[simp] theorem comp_zero (p : Seminorm 𝕜₂ E₂) : p.comp (0 : E →ₛₗ[σ₁₂] E₂) = 0 := ext fun _ => map_zero p @[simp] theorem zero_comp (f : E →ₛₗ[σ₁₂] E₂) : (0 : Seminorm 𝕜₂ E₂).comp f = 0 := ext fun _ => rfl theorem comp_comp [RingHomCompTriple σ₁₂ σ₂₃ σ₁₃] (p : Seminorm 𝕜₃ E₃) (g : E₂ →ₛₗ[σ₂₃] E₃) (f : E →ₛₗ[σ₁₂] E₂) : p.comp (g.comp f) = (p.comp g).comp f := ext fun _ => rfl theorem add_comp (p q : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) : (p + q).comp f = p.comp f + q.comp f := ext fun _ => rfl theorem comp_add_le (p : Seminorm 𝕜₂ E₂) (f g : E →ₛₗ[σ₁₂] E₂) : p.comp (f + g) ≤ p.comp f + p.comp g := fun _ => map_add_le_add p _ _ theorem smul_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (c : R) : (c • p).comp f = c • p.comp f := ext fun _ => rfl theorem comp_mono {p q : Seminorm 𝕜₂ E₂} (f : E →ₛₗ[σ₁₂] E₂) (hp : p ≤ q) : p.comp f ≤ q.comp f := fun _ => hp _ /-- The composition as an `AddMonoidHom`. -/ @[simps] def pullback (f : E →ₛₗ[σ₁₂] E₂) : Seminorm 𝕜₂ E₂ →+ Seminorm 𝕜 E where toFun := fun p => p.comp f map_zero' := zero_comp f map_add' := fun p q => add_comp p q f instance instOrderBot : OrderBot (Seminorm 𝕜 E) where bot := 0 bot_le := apply_nonneg @[simp] theorem coe_bot : ⇑(⊥ : Seminorm 𝕜 E) = 0 := rfl theorem bot_eq_zero : (⊥ : Seminorm 𝕜 E) = 0 := rfl theorem smul_le_smul {p q : Seminorm 𝕜 E} {a b : ℝ≥0} (hpq : p ≤ q) (hab : a ≤ b) : a • p ≤ b • q := by simp_rw [le_def] intro x exact mul_le_mul hab (hpq x) (apply_nonneg p x) (NNReal.coe_nonneg b) theorem finset_sup_apply (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) : s.sup p x = ↑(s.sup fun i => ⟨p i x, apply_nonneg (p i) x⟩ : ℝ≥0) := by induction' s using Finset.cons_induction_on with a s ha ih · rw [Finset.sup_empty, Finset.sup_empty, coe_bot, _root_.bot_eq_zero, Pi.zero_apply] norm_cast · rw [Finset.sup_cons, Finset.sup_cons, coe_sup, Pi.sup_apply, NNReal.coe_max, NNReal.coe_mk, ih] theorem exists_apply_eq_finset_sup (p : ι → Seminorm 𝕜 E) {s : Finset ι} (hs : s.Nonempty) (x : E) : ∃ i ∈ s, s.sup p x = p i x := by rcases Finset.exists_mem_eq_sup s hs (fun i ↦ (⟨p i x, apply_nonneg _ _⟩ : ℝ≥0)) with ⟨i, hi, hix⟩ rw [finset_sup_apply] exact ⟨i, hi, congr_arg _ hix⟩ theorem zero_or_exists_apply_eq_finset_sup (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) : s.sup p x = 0 ∨ ∃ i ∈ s, s.sup p x = p i x := by rcases Finset.eq_empty_or_nonempty s with (rfl|hs) · left; rfl · right; exact exists_apply_eq_finset_sup p hs x theorem finset_sup_smul (p : ι → Seminorm 𝕜 E) (s : Finset ι) (C : ℝ≥0) : s.sup (C • p) = C • s.sup p := by ext x rw [smul_apply, finset_sup_apply, finset_sup_apply] symm exact congr_arg ((↑) : ℝ≥0 → ℝ) (NNReal.mul_finset_sup C s (fun i ↦ ⟨p i x, apply_nonneg _ _⟩)) theorem finset_sup_le_sum (p : ι → Seminorm 𝕜 E) (s : Finset ι) : s.sup p ≤ ∑ i ∈ s, p i := by classical refine Finset.sup_le_iff.mpr ?_ intro i hi rw [Finset.sum_eq_sum_diff_singleton_add hi, le_add_iff_nonneg_left] exact bot_le theorem finset_sup_apply_le {p : ι → Seminorm 𝕜 E} {s : Finset ι} {x : E} {a : ℝ} (ha : 0 ≤ a) (h : ∀ i, i ∈ s → p i x ≤ a) : s.sup p x ≤ a := by lift a to ℝ≥0 using ha rw [finset_sup_apply, NNReal.coe_le_coe] exact Finset.sup_le h theorem le_finset_sup_apply {p : ι → Seminorm 𝕜 E} {s : Finset ι} {x : E} {i : ι} (hi : i ∈ s) : p i x ≤ s.sup p x := (Finset.le_sup hi : p i ≤ s.sup p) x theorem finset_sup_apply_lt {p : ι → Seminorm 𝕜 E} {s : Finset ι} {x : E} {a : ℝ} (ha : 0 < a) (h : ∀ i, i ∈ s → p i x < a) : s.sup p x < a := by lift a to ℝ≥0 using ha.le rw [finset_sup_apply, NNReal.coe_lt_coe, Finset.sup_lt_iff] · exact h · exact NNReal.coe_pos.mpr ha theorem norm_sub_map_le_sub (p : Seminorm 𝕜 E) (x y : E) : ‖p x - p y‖ ≤ p (x - y) := abs_sub_map_le_sub p x y end Module end SeminormedRing section SeminormedCommRing variable [SeminormedRing 𝕜] [SeminormedCommRing 𝕜₂] variable {σ₁₂ : 𝕜 →+* 𝕜₂} [RingHomIsometric σ₁₂] variable [AddCommGroup E] [AddCommGroup E₂] [Module 𝕜 E] [Module 𝕜₂ E₂] theorem comp_smul (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (c : 𝕜₂) : p.comp (c • f) = ‖c‖₊ • p.comp f := ext fun _ => by rw [comp_apply, smul_apply, LinearMap.smul_apply, map_smul_eq_mul, NNReal.smul_def, coe_nnnorm, smul_eq_mul, comp_apply] theorem comp_smul_apply (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (c : 𝕜₂) (x : E) : p.comp (c • f) x = ‖c‖ * p (f x) := map_smul_eq_mul p _ _ end SeminormedCommRing section NormedField variable [NormedField 𝕜] [AddCommGroup E] [Module 𝕜 E] {p q : Seminorm 𝕜 E} {x : E} /-- Auxiliary lemma to show that the infimum of seminorms is well-defined. -/ theorem bddBelow_range_add : BddBelow (range fun u => p u + q (x - u)) := ⟨0, by rintro _ ⟨x, rfl⟩ dsimp; positivity⟩ noncomputable instance instInf : Min (Seminorm 𝕜 E) where min p q := { p.toAddGroupSeminorm ⊓ q.toAddGroupSeminorm with toFun := fun x => ⨅ u : E, p u + q (x - u) smul' := by intro a x obtain rfl | ha := eq_or_ne a 0 · rw [norm_zero, zero_mul, zero_smul] refine ciInf_eq_of_forall_ge_of_forall_gt_exists_lt (fun i => by positivity) fun x hx => ⟨0, by rwa [map_zero, sub_zero, map_zero, add_zero]⟩ simp_rw [Real.mul_iInf_of_nonneg (norm_nonneg a), mul_add, ← map_smul_eq_mul p, ← map_smul_eq_mul q, smul_sub] refine Function.Surjective.iInf_congr ((a⁻¹ • ·) : E → E) (fun u => ⟨a • u, inv_smul_smul₀ ha u⟩) fun u => ?_ rw [smul_inv_smul₀ ha] } @[simp] theorem inf_apply (p q : Seminorm 𝕜 E) (x : E) : (p ⊓ q) x = ⨅ u : E, p u + q (x - u) := rfl noncomputable instance instLattice : Lattice (Seminorm 𝕜 E) := { Seminorm.instSemilatticeSup with inf := (· ⊓ ·) inf_le_left := fun p q x => ciInf_le_of_le bddBelow_range_add x <| by simp only [sub_self, map_zero, add_zero]; rfl inf_le_right := fun p q x => ciInf_le_of_le bddBelow_range_add 0 <| by simp only [sub_self, map_zero, zero_add, sub_zero]; rfl le_inf := fun a _ _ hab hac _ => le_ciInf fun _ => (le_map_add_map_sub a _ _).trans <| add_le_add (hab _) (hac _) } theorem smul_inf [SMul R ℝ] [SMul R ℝ≥0] [IsScalarTower R ℝ≥0 ℝ] (r : R) (p q : Seminorm 𝕜 E) : r • (p ⊓ q) = r • p ⊓ r • q := by ext simp_rw [smul_apply, inf_apply, smul_apply, ← smul_one_smul ℝ≥0 r (_ : ℝ), NNReal.smul_def, smul_eq_mul, Real.mul_iInf_of_nonneg (NNReal.coe_nonneg _), mul_add] section Classical open Classical in /-- We define the supremum of an arbitrary subset of `Seminorm 𝕜 E` as follows: * if `s` is `BddAbove` *as a set of functions `E → ℝ`* (that is, if `s` is pointwise bounded above), we take the pointwise supremum of all elements of `s`, and we prove that it is indeed a seminorm. * otherwise, we take the zero seminorm `⊥`. There are two things worth mentioning here: * First, it is not trivial at first that `s` being bounded above *by a function* implies being bounded above *as a seminorm*. We show this in `Seminorm.bddAbove_iff` by using that the `Sup s` as defined here is then a bounding seminorm for `s`. So it is important to make the case disjunction on `BddAbove ((↑) '' s : Set (E → ℝ))` and not `BddAbove s`. * Since the pointwise `Sup` already gives `0` at points where a family of functions is not bounded above, one could hope that just using the pointwise `Sup` would work here, without the need for an additional case disjunction. As discussed on Zulip, this doesn't work because this can give a function which does *not* satisfy the seminorm axioms (typically sub-additivity). -/ noncomputable instance instSupSet : SupSet (Seminorm 𝕜 E) where sSup s := if h : BddAbove ((↑) '' s : Set (E → ℝ)) then { toFun := ⨆ p : s, ((p : Seminorm 𝕜 E) : E → ℝ) map_zero' := by rw [iSup_apply, ← @Real.iSup_const_zero s] congr! rename_i _ _ _ i exact map_zero i.1 add_le' := fun x y => by rcases h with ⟨q, hq⟩ obtain rfl | h := s.eq_empty_or_nonempty · simp [Real.iSup_of_isEmpty] haveI : Nonempty ↑s := h.coe_sort simp only [iSup_apply] refine ciSup_le fun i => ((i : Seminorm 𝕜 E).add_le' x y).trans <| add_le_add -- Porting note: `f` is provided to force `Subtype.val` to appear. -- A type ascription on `_` would have also worked, but would have been more verbose. (le_ciSup (f := fun i => (Subtype.val i : Seminorm 𝕜 E).toFun x) ⟨q x, ?_⟩ i) (le_ciSup (f := fun i => (Subtype.val i : Seminorm 𝕜 E).toFun y) ⟨q y, ?_⟩ i) <;> rw [mem_upperBounds, forall_mem_range] <;> exact fun j => hq (mem_image_of_mem _ j.2) _ neg' := fun x => by simp only [iSup_apply] congr! 2 rename_i _ _ _ i exact i.1.neg' _ smul' := fun a x => by simp only [iSup_apply] rw [← smul_eq_mul, Real.smul_iSup_of_nonneg (norm_nonneg a) fun i : s => (i : Seminorm 𝕜 E) x] congr! rename_i _ _ _ i exact i.1.smul' a x } else ⊥ protected theorem coe_sSup_eq' {s : Set <| Seminorm 𝕜 E} (hs : BddAbove ((↑) '' s : Set (E → ℝ))) : ↑(sSup s) = ⨆ p : s, ((p : Seminorm 𝕜 E) : E → ℝ) := congr_arg _ (dif_pos hs) protected theorem bddAbove_iff {s : Set <| Seminorm 𝕜 E} : BddAbove s ↔ BddAbove ((↑) '' s : Set (E → ℝ)) := ⟨fun ⟨q, hq⟩ => ⟨q, forall_mem_image.2 fun _ hp => hq hp⟩, fun H => ⟨sSup s, fun p hp x => by dsimp rw [Seminorm.coe_sSup_eq' H, iSup_apply] rcases H with ⟨q, hq⟩ exact le_ciSup ⟨q x, forall_mem_range.mpr fun i : s => hq (mem_image_of_mem _ i.2) x⟩ ⟨p, hp⟩⟩⟩ protected theorem bddAbove_range_iff {ι : Sort*} {p : ι → Seminorm 𝕜 E} : BddAbove (range p) ↔ ∀ x, BddAbove (range fun i ↦ p i x) := by rw [Seminorm.bddAbove_iff, ← range_comp, bddAbove_range_pi]; rfl protected theorem coe_sSup_eq {s : Set <| Seminorm 𝕜 E} (hs : BddAbove s) : ↑(sSup s) = ⨆ p : s, ((p : Seminorm 𝕜 E) : E → ℝ) := Seminorm.coe_sSup_eq' (Seminorm.bddAbove_iff.mp hs) protected theorem coe_iSup_eq {ι : Sort*} {p : ι → Seminorm 𝕜 E} (hp : BddAbove (range p)) : ↑(⨆ i, p i) = ⨆ i, ((p i : Seminorm 𝕜 E) : E → ℝ) := by rw [← sSup_range, Seminorm.coe_sSup_eq hp] exact iSup_range' (fun p : Seminorm 𝕜 E => (p : E → ℝ)) p protected theorem sSup_apply {s : Set (Seminorm 𝕜 E)} (hp : BddAbove s) {x : E} : (sSup s) x = ⨆ p : s, (p : E → ℝ) x := by rw [Seminorm.coe_sSup_eq hp, iSup_apply] protected theorem iSup_apply {ι : Sort*} {p : ι → Seminorm 𝕜 E} (hp : BddAbove (range p)) {x : E} : (⨆ i, p i) x = ⨆ i, p i x := by rw [Seminorm.coe_iSup_eq hp, iSup_apply] protected theorem sSup_empty : sSup (∅ : Set (Seminorm 𝕜 E)) = ⊥ := by ext rw [Seminorm.sSup_apply bddAbove_empty, Real.iSup_of_isEmpty] rfl private theorem isLUB_sSup (s : Set (Seminorm 𝕜 E)) (hs₁ : BddAbove s) (hs₂ : s.Nonempty) : IsLUB s (sSup s) := by refine ⟨fun p hp x => ?_, fun p hp x => ?_⟩ <;> haveI : Nonempty ↑s := hs₂.coe_sort <;> dsimp <;> rw [Seminorm.coe_sSup_eq hs₁, iSup_apply] · rcases hs₁ with ⟨q, hq⟩ exact le_ciSup ⟨q x, forall_mem_range.mpr fun i : s => hq i.2 x⟩ ⟨p, hp⟩ · exact ciSup_le fun q => hp q.2 x /-- `Seminorm 𝕜 E` is a conditionally complete lattice. Note that, while `inf`, `sup` and `sSup` have good definitional properties (corresponding to the instances given here for `Inf`, `Sup` and `SupSet` respectively), `sInf s` is just defined as the supremum of the lower bounds of `s`, which is not really useful in practice. If you need to use `sInf` on seminorms, then you should probably provide a more workable definition first, but this is unlikely to happen so we keep the "bad" definition for now. -/ noncomputable instance instConditionallyCompleteLattice : ConditionallyCompleteLattice (Seminorm 𝕜 E) := conditionallyCompleteLatticeOfLatticeOfsSup (Seminorm 𝕜 E) Seminorm.isLUB_sSup end Classical end NormedField /-! ### Seminorm ball -/ section SeminormedRing variable [SeminormedRing 𝕜] section AddCommGroup variable [AddCommGroup E] section SMul variable [SMul 𝕜 E] (p : Seminorm 𝕜 E) /-- The ball of radius `r` at `x` with respect to seminorm `p` is the set of elements `y` with `p (y - x) < r`. -/ def ball (x : E) (r : ℝ) := { y : E | p (y - x) < r } /-- The closed ball of radius `r` at `x` with respect to seminorm `p` is the set of elements `y` with `p (y - x) ≤ r`. -/ def closedBall (x : E) (r : ℝ) := { y : E | p (y - x) ≤ r } variable {x y : E} {r : ℝ} @[simp] theorem mem_ball : y ∈ ball p x r ↔ p (y - x) < r := Iff.rfl @[simp] theorem mem_closedBall : y ∈ closedBall p x r ↔ p (y - x) ≤ r := Iff.rfl theorem mem_ball_self (hr : 0 < r) : x ∈ ball p x r := by simp [hr] theorem mem_closedBall_self (hr : 0 ≤ r) : x ∈ closedBall p x r := by simp [hr] theorem mem_ball_zero : y ∈ ball p 0 r ↔ p y < r := by rw [mem_ball, sub_zero] theorem mem_closedBall_zero : y ∈ closedBall p 0 r ↔ p y ≤ r := by rw [mem_closedBall, sub_zero] theorem ball_zero_eq : ball p 0 r = { y : E | p y < r } := Set.ext fun _ => p.mem_ball_zero theorem closedBall_zero_eq : closedBall p 0 r = { y : E | p y ≤ r } := Set.ext fun _ => p.mem_closedBall_zero theorem ball_subset_closedBall (x r) : ball p x r ⊆ closedBall p x r := fun _ h => (mem_closedBall _).mpr ((mem_ball _).mp h).le theorem closedBall_eq_biInter_ball (x r) : closedBall p x r = ⋂ ρ > r, ball p x ρ := by ext y; simp_rw [mem_closedBall, mem_iInter₂, mem_ball, ← forall_lt_iff_le'] @[simp] theorem ball_zero' (x : E) (hr : 0 < r) : ball (0 : Seminorm 𝕜 E) x r = Set.univ := by rw [Set.eq_univ_iff_forall, ball] simp [hr] @[simp] theorem closedBall_zero' (x : E) (hr : 0 < r) : closedBall (0 : Seminorm 𝕜 E) x r = Set.univ := eq_univ_of_subset (ball_subset_closedBall _ _ _) (ball_zero' x hr) theorem ball_smul (p : Seminorm 𝕜 E) {c : NNReal} (hc : 0 < c) (r : ℝ) (x : E) : (c • p).ball x r = p.ball x (r / c) := by ext rw [mem_ball, mem_ball, smul_apply, NNReal.smul_def, smul_eq_mul, mul_comm, lt_div_iff₀ (NNReal.coe_pos.mpr hc)] theorem closedBall_smul (p : Seminorm 𝕜 E) {c : NNReal} (hc : 0 < c) (r : ℝ) (x : E) : (c • p).closedBall x r = p.closedBall x (r / c) := by ext rw [mem_closedBall, mem_closedBall, smul_apply, NNReal.smul_def, smul_eq_mul, mul_comm, le_div_iff₀ (NNReal.coe_pos.mpr hc)] theorem ball_sup (p : Seminorm 𝕜 E) (q : Seminorm 𝕜 E) (e : E) (r : ℝ) : ball (p ⊔ q) e r = ball p e r ∩ ball q e r := by simp_rw [ball, ← Set.setOf_and, coe_sup, Pi.sup_apply, sup_lt_iff] theorem closedBall_sup (p : Seminorm 𝕜 E) (q : Seminorm 𝕜 E) (e : E) (r : ℝ) : closedBall (p ⊔ q) e r = closedBall p e r ∩ closedBall q e r := by simp_rw [closedBall, ← Set.setOf_and, coe_sup, Pi.sup_apply, sup_le_iff] theorem ball_finset_sup' (p : ι → Seminorm 𝕜 E) (s : Finset ι) (H : s.Nonempty) (e : E) (r : ℝ) : ball (s.sup' H p) e r = s.inf' H fun i => ball (p i) e r := by induction H using Finset.Nonempty.cons_induction with | singleton => simp | cons _ _ _ hs ih => rw [Finset.sup'_cons hs, Finset.inf'_cons hs, ball_sup] -- Porting note: `rw` can't use `inf_eq_inter` here, but `simp` can? simp only [inf_eq_inter, ih] theorem closedBall_finset_sup' (p : ι → Seminorm 𝕜 E) (s : Finset ι) (H : s.Nonempty) (e : E) (r : ℝ) : closedBall (s.sup' H p) e r = s.inf' H fun i => closedBall (p i) e r := by induction H using Finset.Nonempty.cons_induction with | singleton => simp | cons _ _ _ hs ih => rw [Finset.sup'_cons hs, Finset.inf'_cons hs, closedBall_sup] -- Porting note: `rw` can't use `inf_eq_inter` here, but `simp` can? simp only [inf_eq_inter, ih] theorem ball_mono {p : Seminorm 𝕜 E} {r₁ r₂ : ℝ} (h : r₁ ≤ r₂) : p.ball x r₁ ⊆ p.ball x r₂ := fun _ (hx : _ < _) => hx.trans_le h theorem closedBall_mono {p : Seminorm 𝕜 E} {r₁ r₂ : ℝ} (h : r₁ ≤ r₂) : p.closedBall x r₁ ⊆ p.closedBall x r₂ := fun _ (hx : _ ≤ _) => hx.trans h theorem ball_antitone {p q : Seminorm 𝕜 E} (h : q ≤ p) : p.ball x r ⊆ q.ball x r := fun _ => (h _).trans_lt theorem closedBall_antitone {p q : Seminorm 𝕜 E} (h : q ≤ p) : p.closedBall x r ⊆ q.closedBall x r := fun _ => (h _).trans theorem ball_add_ball_subset (p : Seminorm 𝕜 E) (r₁ r₂ : ℝ) (x₁ x₂ : E) : p.ball (x₁ : E) r₁ + p.ball (x₂ : E) r₂ ⊆ p.ball (x₁ + x₂) (r₁ + r₂) := by rintro x ⟨y₁, hy₁, y₂, hy₂, rfl⟩ rw [mem_ball, add_sub_add_comm] exact (map_add_le_add p _ _).trans_lt (add_lt_add hy₁ hy₂) theorem closedBall_add_closedBall_subset (p : Seminorm 𝕜 E) (r₁ r₂ : ℝ) (x₁ x₂ : E) : p.closedBall (x₁ : E) r₁ + p.closedBall (x₂ : E) r₂ ⊆ p.closedBall (x₁ + x₂) (r₁ + r₂) := by rintro x ⟨y₁, hy₁, y₂, hy₂, rfl⟩ rw [mem_closedBall, add_sub_add_comm] exact (map_add_le_add p _ _).trans (add_le_add hy₁ hy₂) theorem sub_mem_ball (p : Seminorm 𝕜 E) (x₁ x₂ y : E) (r : ℝ) : x₁ - x₂ ∈ p.ball y r ↔ x₁ ∈ p.ball (x₂ + y) r := by simp_rw [mem_ball, sub_sub] theorem sub_mem_closedBall (p : Seminorm 𝕜 E) (x₁ x₂ y : E) (r : ℝ) : x₁ - x₂ ∈ p.closedBall y r ↔ x₁ ∈ p.closedBall (x₂ + y) r := by simp_rw [mem_closedBall, sub_sub] /-- The image of a ball under addition with a singleton is another ball. -/ theorem vadd_ball (p : Seminorm 𝕜 E) : x +ᵥ p.ball y r = p.ball (x +ᵥ y) r := letI := AddGroupSeminorm.toSeminormedAddCommGroup p.toAddGroupSeminorm Metric.vadd_ball x y r /-- The image of a closed ball under addition with a singleton is another closed ball. -/ theorem vadd_closedBall (p : Seminorm 𝕜 E) : x +ᵥ p.closedBall y r = p.closedBall (x +ᵥ y) r := letI := AddGroupSeminorm.toSeminormedAddCommGroup p.toAddGroupSeminorm Metric.vadd_closedBall x y r end SMul section Module variable [Module 𝕜 E] variable [SeminormedRing 𝕜₂] [AddCommGroup E₂] [Module 𝕜₂ E₂] variable {σ₁₂ : 𝕜 →+* 𝕜₂} [RingHomIsometric σ₁₂] theorem ball_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (x : E) (r : ℝ) : (p.comp f).ball x r = f ⁻¹' p.ball (f x) r := by ext simp_rw [ball, mem_preimage, comp_apply, Set.mem_setOf_eq, map_sub] theorem closedBall_comp (p : Seminorm 𝕜₂ E₂) (f : E →ₛₗ[σ₁₂] E₂) (x : E) (r : ℝ) : (p.comp f).closedBall x r = f ⁻¹' p.closedBall (f x) r := by ext simp_rw [closedBall, mem_preimage, comp_apply, Set.mem_setOf_eq, map_sub] variable (p : Seminorm 𝕜 E) theorem preimage_metric_ball {r : ℝ} : p ⁻¹' Metric.ball 0 r = { x | p x < r } := by ext x simp only [mem_setOf, mem_preimage, mem_ball_zero_iff, Real.norm_of_nonneg (apply_nonneg p _)] theorem preimage_metric_closedBall {r : ℝ} : p ⁻¹' Metric.closedBall 0 r = { x | p x ≤ r } := by ext x simp only [mem_setOf, mem_preimage, mem_closedBall_zero_iff, Real.norm_of_nonneg (apply_nonneg p _)] theorem ball_zero_eq_preimage_ball {r : ℝ} : p.ball 0 r = p ⁻¹' Metric.ball 0 r := by rw [ball_zero_eq, preimage_metric_ball] theorem closedBall_zero_eq_preimage_closedBall {r : ℝ} : p.closedBall 0 r = p ⁻¹' Metric.closedBall 0 r := by rw [closedBall_zero_eq, preimage_metric_closedBall] @[simp] theorem ball_bot {r : ℝ} (x : E) (hr : 0 < r) : ball (⊥ : Seminorm 𝕜 E) x r = Set.univ := ball_zero' x hr @[simp] theorem closedBall_bot {r : ℝ} (x : E) (hr : 0 < r) : closedBall (⊥ : Seminorm 𝕜 E) x r = Set.univ := closedBall_zero' x hr /-- Seminorm-balls at the origin are balanced. -/ theorem balanced_ball_zero (r : ℝ) : Balanced 𝕜 (ball p 0 r) := by rintro a ha x ⟨y, hy, hx⟩ rw [mem_ball_zero, ← hx, map_smul_eq_mul] calc _ ≤ p y := mul_le_of_le_one_left (apply_nonneg p _) ha _ < r := by rwa [mem_ball_zero] at hy /-- Closed seminorm-balls at the origin are balanced. -/ theorem balanced_closedBall_zero (r : ℝ) : Balanced 𝕜 (closedBall p 0 r) := by rintro a ha x ⟨y, hy, hx⟩ rw [mem_closedBall_zero, ← hx, map_smul_eq_mul] calc _ ≤ p y := mul_le_of_le_one_left (apply_nonneg p _) ha _ ≤ r := by rwa [mem_closedBall_zero] at hy theorem ball_finset_sup_eq_iInter (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) {r : ℝ} (hr : 0 < r) : ball (s.sup p) x r = ⋂ i ∈ s, ball (p i) x r := by lift r to NNReal using hr.le simp_rw [ball, iInter_setOf, finset_sup_apply, NNReal.coe_lt_coe, Finset.sup_lt_iff (show ⊥ < r from hr), ← NNReal.coe_lt_coe, NNReal.coe_mk] theorem closedBall_finset_sup_eq_iInter (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) {r : ℝ} (hr : 0 ≤ r) : closedBall (s.sup p) x r = ⋂ i ∈ s, closedBall (p i) x r := by lift r to NNReal using hr simp_rw [closedBall, iInter_setOf, finset_sup_apply, NNReal.coe_le_coe, Finset.sup_le_iff, ← NNReal.coe_le_coe, NNReal.coe_mk] theorem ball_finset_sup (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) {r : ℝ} (hr : 0 < r) : ball (s.sup p) x r = s.inf fun i => ball (p i) x r := by rw [Finset.inf_eq_iInf] exact ball_finset_sup_eq_iInter _ _ _ hr theorem closedBall_finset_sup (p : ι → Seminorm 𝕜 E) (s : Finset ι) (x : E) {r : ℝ} (hr : 0 ≤ r) : closedBall (s.sup p) x r = s.inf fun i => closedBall (p i) x r := by rw [Finset.inf_eq_iInf] exact closedBall_finset_sup_eq_iInter _ _ _ hr @[simp] theorem ball_eq_emptyset (p : Seminorm 𝕜 E) {x : E} {r : ℝ} (hr : r ≤ 0) : p.ball x r = ∅ := by ext rw [Seminorm.mem_ball, Set.mem_empty_iff_false, iff_false, not_lt] exact hr.trans (apply_nonneg p _) @[simp] theorem closedBall_eq_emptyset (p : Seminorm 𝕜 E) {x : E} {r : ℝ} (hr : r < 0) : p.closedBall x r = ∅ := by ext rw [Seminorm.mem_closedBall, Set.mem_empty_iff_false, iff_false, not_le] exact hr.trans_le (apply_nonneg _ _) theorem closedBall_smul_ball (p : Seminorm 𝕜 E) {r₁ : ℝ} (hr₁ : r₁ ≠ 0) (r₂ : ℝ) : Metric.closedBall (0 : 𝕜) r₁ • p.ball 0 r₂ ⊆ p.ball 0 (r₁ * r₂) := by simp only [smul_subset_iff, mem_ball_zero, mem_closedBall_zero_iff, map_smul_eq_mul] refine fun a ha b hb ↦ mul_lt_mul' ha hb (apply_nonneg _ _) ?_ exact hr₁.lt_or_lt.resolve_left <| ((norm_nonneg a).trans ha).not_lt theorem ball_smul_closedBall (p : Seminorm 𝕜 E) (r₁ : ℝ) {r₂ : ℝ} (hr₂ : r₂ ≠ 0) : Metric.ball (0 : 𝕜) r₁ • p.closedBall 0 r₂ ⊆ p.ball 0 (r₁ * r₂) := by simp only [smul_subset_iff, mem_ball_zero, mem_closedBall_zero, mem_ball_zero_iff, map_smul_eq_mul] intro a ha b hb rw [mul_comm, mul_comm r₁] refine mul_lt_mul' hb ha (norm_nonneg _) (hr₂.lt_or_lt.resolve_left ?_) exact ((apply_nonneg p b).trans hb).not_lt theorem ball_smul_ball (p : Seminorm 𝕜 E) (r₁ r₂ : ℝ) : Metric.ball (0 : 𝕜) r₁ • p.ball 0 r₂ ⊆ p.ball 0 (r₁ * r₂) := by rcases eq_or_ne r₂ 0 with rfl | hr₂ · simp · exact (smul_subset_smul_left (ball_subset_closedBall _ _ _)).trans (ball_smul_closedBall _ _ hr₂) theorem closedBall_smul_closedBall (p : Seminorm 𝕜 E) (r₁ r₂ : ℝ) : Metric.closedBall (0 : 𝕜) r₁ • p.closedBall 0 r₂ ⊆ p.closedBall 0 (r₁ * r₂) := by simp only [smul_subset_iff, mem_closedBall_zero, mem_closedBall_zero_iff, map_smul_eq_mul] intro a ha b hb gcongr exact (norm_nonneg _).trans ha theorem neg_mem_ball_zero {r : ℝ} {x : E} : -x ∈ ball p 0 r ↔ x ∈ ball p 0 r := by simp only [mem_ball_zero, map_neg_eq_map] theorem neg_mem_closedBall_zero {r : ℝ} {x : E} : -x ∈ closedBall p 0 r ↔ x ∈ closedBall p 0 r := by simp only [mem_closedBall_zero, map_neg_eq_map] @[simp] theorem neg_ball (p : Seminorm 𝕜 E) (r : ℝ) (x : E) : -ball p x r = ball p (-x) r := by ext rw [Set.mem_neg, mem_ball, mem_ball, ← neg_add', sub_neg_eq_add, map_neg_eq_map] @[simp] theorem neg_closedBall (p : Seminorm 𝕜 E) (r : ℝ) (x : E) : -closedBall p x r = closedBall p (-x) r := by ext rw [Set.mem_neg, mem_closedBall, mem_closedBall, ← neg_add', sub_neg_eq_add, map_neg_eq_map] end Module end AddCommGroup end SeminormedRing section NormedField variable [NormedField 𝕜] [AddCommGroup E] [Module 𝕜 E] (p : Seminorm 𝕜 E) {r : ℝ} {x : E} theorem closedBall_iSup {ι : Sort*} {p : ι → Seminorm 𝕜 E} (hp : BddAbove (range p)) (e : E) {r : ℝ} (hr : 0 < r) : closedBall (⨆ i, p i) e r = ⋂ i, closedBall (p i) e r := by cases isEmpty_or_nonempty ι · rw [iSup_of_empty', iInter_of_empty, Seminorm.sSup_empty] exact closedBall_bot _ hr · ext x have := Seminorm.bddAbove_range_iff.mp hp (x - e) simp only [mem_closedBall, mem_iInter, Seminorm.iSup_apply hp, ciSup_le_iff this] theorem ball_norm_mul_subset {p : Seminorm 𝕜 E} {k : 𝕜} {r : ℝ} : p.ball 0 (‖k‖ * r) ⊆ k • p.ball 0 r := by rcases eq_or_ne k 0 with (rfl | hk) · rw [norm_zero, zero_mul, ball_eq_emptyset _ le_rfl] exact empty_subset _ · intro x rw [Set.mem_smul_set, Seminorm.mem_ball_zero] refine fun hx => ⟨k⁻¹ • x, ?_, ?_⟩ · rwa [Seminorm.mem_ball_zero, map_smul_eq_mul, norm_inv, ← mul_lt_mul_left <| norm_pos_iff.mpr hk, ← mul_assoc, ← div_eq_mul_inv ‖k‖ ‖k‖, div_self (ne_of_gt <| norm_pos_iff.mpr hk), one_mul] rw [← smul_assoc, smul_eq_mul, ← div_eq_mul_inv, div_self hk, one_smul] theorem smul_ball_zero {p : Seminorm 𝕜 E} {k : 𝕜} {r : ℝ} (hk : k ≠ 0) : k • p.ball 0 r = p.ball 0 (‖k‖ * r) := by ext rw [mem_smul_set_iff_inv_smul_mem₀ hk, p.mem_ball_zero, p.mem_ball_zero, map_smul_eq_mul, norm_inv, ← div_eq_inv_mul, div_lt_iff₀ (norm_pos_iff.2 hk), mul_comm] theorem smul_closedBall_subset {p : Seminorm 𝕜 E} {k : 𝕜} {r : ℝ} : k • p.closedBall 0 r ⊆ p.closedBall 0 (‖k‖ * r) := by rintro x ⟨y, hy, h⟩ rw [Seminorm.mem_closedBall_zero, ← h, map_smul_eq_mul] rw [Seminorm.mem_closedBall_zero] at hy gcongr theorem smul_closedBall_zero {p : Seminorm 𝕜 E} {k : 𝕜} {r : ℝ} (hk : 0 < ‖k‖) : k • p.closedBall 0 r = p.closedBall 0 (‖k‖ * r) := by refine subset_antisymm smul_closedBall_subset ?_ intro x rw [Set.mem_smul_set, Seminorm.mem_closedBall_zero] refine fun hx => ⟨k⁻¹ • x, ?_, ?_⟩ · rwa [Seminorm.mem_closedBall_zero, map_smul_eq_mul, norm_inv, ← mul_le_mul_left hk, ← mul_assoc, ← div_eq_mul_inv ‖k‖ ‖k‖, div_self (ne_of_gt hk), one_mul] rw [← smul_assoc, smul_eq_mul, ← div_eq_mul_inv, div_self (norm_pos_iff.mp hk), one_smul] theorem ball_zero_absorbs_ball_zero (p : Seminorm 𝕜 E) {r₁ r₂ : ℝ} (hr₁ : 0 < r₁) : Absorbs 𝕜 (p.ball 0 r₁) (p.ball 0 r₂) := by rcases exists_pos_lt_mul hr₁ r₂ with ⟨r, hr₀, hr⟩ refine .of_norm ⟨r, fun a ha x hx => ?_⟩ rw [smul_ball_zero (norm_pos_iff.1 <| hr₀.trans_le ha), p.mem_ball_zero] rw [p.mem_ball_zero] at hx exact hx.trans (hr.trans_le <| by gcongr) /-- Seminorm-balls at the origin are absorbent. -/ protected theorem absorbent_ball_zero (hr : 0 < r) : Absorbent 𝕜 (ball p (0 : E) r) := absorbent_iff_forall_absorbs_singleton.2 fun _ => (p.ball_zero_absorbs_ball_zero hr).mono_right <| singleton_subset_iff.2 <| p.mem_ball_zero.2 <| lt_add_one _ /-- Closed seminorm-balls at the origin are absorbent. -/ protected theorem absorbent_closedBall_zero (hr : 0 < r) : Absorbent 𝕜 (closedBall p (0 : E) r) := (p.absorbent_ball_zero hr).mono (p.ball_subset_closedBall _ _) /-- Seminorm-balls containing the origin are absorbent. -/ protected theorem absorbent_ball (hpr : p x < r) : Absorbent 𝕜 (ball p x r) := by refine (p.absorbent_ball_zero <| sub_pos.2 hpr).mono fun y hy => ?_ rw [p.mem_ball_zero] at hy exact p.mem_ball.2 ((map_sub_le_add p _ _).trans_lt <| add_lt_of_lt_sub_right hy) /-- Seminorm-balls containing the origin are absorbent. -/ protected theorem absorbent_closedBall (hpr : p x < r) : Absorbent 𝕜 (closedBall p x r) := by refine (p.absorbent_closedBall_zero <| sub_pos.2 hpr).mono fun y hy => ?_ rw [p.mem_closedBall_zero] at hy exact p.mem_closedBall.2 ((map_sub_le_add p _ _).trans <| add_le_of_le_sub_right hy) @[simp] theorem smul_ball_preimage (p : Seminorm 𝕜 E) (y : E) (r : ℝ) (a : 𝕜) (ha : a ≠ 0) : (a • ·) ⁻¹' p.ball y r = p.ball (a⁻¹ • y) (r / ‖a‖) := Set.ext fun _ => by
rw [mem_preimage, mem_ball, mem_ball, lt_div_iff₀ (norm_pos_iff.mpr ha), mul_comm, ← map_smul_eq_mul p, smul_sub, smul_inv_smul₀ ha]
Mathlib/Analysis/Seminorm.lean
960
962
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Order.Ring.Nat import Mathlib.Algebra.Ring.Int.Defs import Mathlib.Data.Nat.Bitwise import Mathlib.Data.Nat.Cast.Order.Basic import Mathlib.Data.Nat.PSub import Mathlib.Data.Nat.Size import Mathlib.Data.Num.Bitwise /-! # Properties of the binary representation of integers -/ open Int attribute [local simp] add_assoc namespace PosNum variable {α : Type*} @[simp, norm_cast] theorem cast_one [One α] [Add α] : ((1 : PosNum) : α) = 1 := rfl @[simp] theorem cast_one' [One α] [Add α] : (PosNum.one : α) = 1 := rfl @[simp, norm_cast] theorem cast_bit0 [One α] [Add α] (n : PosNum) : (n.bit0 : α) = (n : α) + n := rfl @[simp, norm_cast] theorem cast_bit1 [One α] [Add α] (n : PosNum) : (n.bit1 : α) = ((n : α) + n) + 1 := rfl @[simp, norm_cast] theorem cast_to_nat [AddMonoidWithOne α] : ∀ n : PosNum, ((n : ℕ) : α) = n | 1 => Nat.cast_one | bit0 p => by dsimp; rw [Nat.cast_add, p.cast_to_nat] | bit1 p => by dsimp; rw [Nat.cast_add, Nat.cast_add, Nat.cast_one, p.cast_to_nat] @[norm_cast] theorem to_nat_to_int (n : PosNum) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int [AddGroupWithOne α] (n : PosNum) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, Int.cast_natCast, cast_to_nat] theorem succ_to_nat : ∀ n, (succ n : ℕ) = n + 1 | 1 => rfl | bit0 _ => rfl | bit1 p => (congr_arg (fun n ↦ n + n) (succ_to_nat p)).trans <| show ↑p + 1 + ↑p + 1 = ↑p + ↑p + 1 + 1 by simp [add_left_comm] theorem one_add (n : PosNum) : 1 + n = succ n := by cases n <;> rfl theorem add_one (n : PosNum) : n + 1 = succ n := by cases n <;> rfl @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : PosNum) : ℕ) = m + n | 1, b => by rw [one_add b, succ_to_nat, add_comm, cast_one] | a, 1 => by rw [add_one a, succ_to_nat, cast_one] | bit0 a, bit0 b => (congr_arg (fun n ↦ n + n) (add_to_nat a b)).trans <| add_add_add_comm _ _ _ _ | bit0 a, bit1 b => (congr_arg (fun n ↦ (n + n) + 1) (add_to_nat a b)).trans <| show (a + b + (a + b) + 1 : ℕ) = a + a + (b + b + 1) by simp [add_left_comm] | bit1 a, bit0 b => (congr_arg (fun n ↦ (n + n) + 1) (add_to_nat a b)).trans <| show (a + b + (a + b) + 1 : ℕ) = a + a + 1 + (b + b) by simp [add_comm, add_left_comm] | bit1 a, bit1 b => show (succ (a + b) + succ (a + b) : ℕ) = a + a + 1 + (b + b + 1) by rw [succ_to_nat, add_to_nat a b]; simp [add_left_comm] theorem add_succ : ∀ m n : PosNum, m + succ n = succ (m + n) | 1, b => by simp [one_add] | bit0 a, 1 => congr_arg bit0 (add_one a) | bit1 a, 1 => congr_arg bit1 (add_one a) | bit0 _, bit0 _ => rfl | bit0 a, bit1 b => congr_arg bit0 (add_succ a b) | bit1 _, bit0 _ => rfl | bit1 a, bit1 b => congr_arg bit1 (add_succ a b) theorem bit0_of_bit0 : ∀ n, n + n = bit0 n | 1 => rfl | bit0 p => congr_arg bit0 (bit0_of_bit0 p) | bit1 p => show bit0 (succ (p + p)) = _ by rw [bit0_of_bit0 p, succ] theorem bit1_of_bit1 (n : PosNum) : (n + n) + 1 = bit1 n := show (n + n) + 1 = bit1 n by rw [add_one, bit0_of_bit0, succ] @[norm_cast] theorem mul_to_nat (m) : ∀ n, ((m * n : PosNum) : ℕ) = m * n | 1 => (mul_one _).symm | bit0 p => show (↑(m * p) + ↑(m * p) : ℕ) = ↑m * (p + p) by rw [mul_to_nat m p, left_distrib] | bit1 p => (add_to_nat (bit0 (m * p)) m).trans <| show (↑(m * p) + ↑(m * p) + ↑m : ℕ) = ↑m * (p + p) + m by rw [mul_to_nat m p, left_distrib] theorem to_nat_pos : ∀ n : PosNum, 0 < (n : ℕ) | 1 => Nat.zero_lt_one | bit0 p => let h := to_nat_pos p add_pos h h | bit1 _p => Nat.succ_pos _ theorem cmp_to_nat_lemma {m n : PosNum} : (m : ℕ) < n → (bit1 m : ℕ) < bit0 n := show (m : ℕ) < n → (m + m + 1 + 1 : ℕ) ≤ n + n by intro h; rw [Nat.add_right_comm m m 1, add_assoc]; exact Nat.add_le_add h h theorem cmp_swap (m) : ∀ n, (cmp m n).swap = cmp n m := by induction' m with m IH m IH <;> intro n <;> obtain - | n | n := n <;> unfold cmp <;> try { rfl } <;> rw [← IH] <;> cases cmp m n <;> rfl theorem cmp_to_nat : ∀ m n, (Ordering.casesOn (cmp m n) ((m : ℕ) < n) (m = n) ((n : ℕ) < m) : Prop) | 1, 1 => rfl | bit0 a, 1 => let h : (1 : ℕ) ≤ a := to_nat_pos a Nat.add_le_add h h | bit1 a, 1 => Nat.succ_lt_succ <| to_nat_pos <| bit0 a | 1, bit0 b => let h : (1 : ℕ) ≤ b := to_nat_pos b Nat.add_le_add h h | 1, bit1 b => Nat.succ_lt_succ <| to_nat_pos <| bit0 b | bit0 a, bit0 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact Nat.add_lt_add this this · rw [this] · exact Nat.add_lt_add this this | bit0 a, bit1 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact Nat.le_succ_of_le (Nat.add_lt_add this this) · rw [this] apply Nat.lt_succ_self · exact cmp_to_nat_lemma this | bit1 a, bit0 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact cmp_to_nat_lemma this · rw [this] apply Nat.lt_succ_self · exact Nat.le_succ_of_le (Nat.add_lt_add this this) | bit1 a, bit1 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact Nat.succ_lt_succ (Nat.add_lt_add this this) · rw [this] · exact Nat.succ_lt_succ (Nat.add_lt_add this this) @[norm_cast] theorem lt_to_nat {m n : PosNum} : (m : ℕ) < n ↔ m < n := show (m : ℕ) < n ↔ cmp m n = Ordering.lt from match cmp m n, cmp_to_nat m n with | Ordering.lt, h => by simp only at h; simp [h] | Ordering.eq, h => by simp only at h; simp [h, lt_irrefl] | Ordering.gt, h => by simp [not_lt_of_gt h] @[norm_cast] theorem le_to_nat {m n : PosNum} : (m : ℕ) ≤ n ↔ m ≤ n := by rw [← not_lt]; exact not_congr lt_to_nat end PosNum namespace Num variable {α : Type*} open PosNum theorem add_zero (n : Num) : n + 0 = n := by cases n <;> rfl theorem zero_add (n : Num) : 0 + n = n := by cases n <;> rfl theorem add_one : ∀ n : Num, n + 1 = succ n | 0 => rfl | pos p => by cases p <;> rfl theorem add_succ : ∀ m n : Num, m + succ n = succ (m + n) | 0, n => by simp [zero_add] | pos p, 0 => show pos (p + 1) = succ (pos p + 0) by rw [PosNum.add_one, add_zero, succ, succ'] | pos _, pos _ => congr_arg pos (PosNum.add_succ _ _) theorem bit0_of_bit0 : ∀ n : Num, n + n = n.bit0 | 0 => rfl | pos p => congr_arg pos p.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : Num, (n + n) + 1 = n.bit1 | 0 => rfl | pos p => congr_arg pos p.bit1_of_bit1 @[simp] theorem ofNat'_zero : Num.ofNat' 0 = 0 := by simp [Num.ofNat'] theorem ofNat'_bit (b n) : ofNat' (Nat.bit b n) = cond b Num.bit1 Num.bit0 (ofNat' n) := Nat.binaryRec_eq _ _ (.inl rfl) @[simp] theorem ofNat'_one : Num.ofNat' 1 = 1 := by erw [ofNat'_bit true 0, cond, ofNat'_zero]; rfl theorem bit1_succ : ∀ n : Num, n.bit1.succ = n.succ.bit0 | 0 => rfl | pos _n => rfl theorem ofNat'_succ : ∀ {n}, ofNat' (n + 1) = ofNat' n + 1 := @(Nat.binaryRec (by simp [zero_add]) fun b n ih => by cases b · erw [ofNat'_bit true n, ofNat'_bit] simp only [← bit1_of_bit1, ← bit0_of_bit0, cond] · rw [show n.bit true + 1 = (n + 1).bit false by simp [Nat.bit, mul_add], ofNat'_bit, ofNat'_bit, ih] simp only [cond, add_one, bit1_succ]) @[simp] theorem add_ofNat' (m n) : Num.ofNat' (m + n) = Num.ofNat' m + Num.ofNat' n := by induction n · simp only [Nat.add_zero, ofNat'_zero, add_zero] · simp only [Nat.add_succ, Nat.add_zero, ofNat'_succ, add_one, add_succ, *] @[simp, norm_cast] theorem cast_zero [Zero α] [One α] [Add α] : ((0 : Num) : α) = 0 := rfl @[simp] theorem cast_zero' [Zero α] [One α] [Add α] : (Num.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [Zero α] [One α] [Add α] : ((1 : Num) : α) = 1 := rfl @[simp] theorem cast_pos [Zero α] [One α] [Add α] (n : PosNum) : (Num.pos n : α) = n := rfl theorem succ'_to_nat : ∀ n, (succ' n : ℕ) = n + 1 | 0 => (Nat.zero_add _).symm | pos _p => PosNum.succ_to_nat _ theorem succ_to_nat (n) : (succ n : ℕ) = n + 1 := succ'_to_nat n @[simp, norm_cast] theorem cast_to_nat [AddMonoidWithOne α] : ∀ n : Num, ((n : ℕ) : α) = n | 0 => Nat.cast_zero | pos p => p.cast_to_nat @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : Num) : ℕ) = m + n | 0, 0 => rfl | 0, pos _q => (Nat.zero_add _).symm | pos _p, 0 => rfl | pos _p, pos _q => PosNum.add_to_nat _ _ @[norm_cast] theorem mul_to_nat : ∀ m n, ((m * n : Num) : ℕ) = m * n | 0, 0 => rfl | 0, pos _q => (zero_mul _).symm | pos _p, 0 => rfl | pos _p, pos _q => PosNum.mul_to_nat _ _ theorem cmp_to_nat : ∀ m n, (Ordering.casesOn (cmp m n) ((m : ℕ) < n) (m = n) ((n : ℕ) < m) : Prop) | 0, 0 => rfl | 0, pos _ => to_nat_pos _ | pos _, 0 => to_nat_pos _ | pos a, pos b => by have := PosNum.cmp_to_nat a b; revert this; dsimp [cmp]; cases PosNum.cmp a b exacts [id, congr_arg pos, id] @[norm_cast] theorem lt_to_nat {m n : Num} : (m : ℕ) < n ↔ m < n := show (m : ℕ) < n ↔ cmp m n = Ordering.lt from match cmp m n, cmp_to_nat m n with | Ordering.lt, h => by simp only at h; simp [h] | Ordering.eq, h => by simp only at h; simp [h, lt_irrefl] | Ordering.gt, h => by simp [not_lt_of_gt h] @[norm_cast] theorem le_to_nat {m n : Num} : (m : ℕ) ≤ n ↔ m ≤ n := by rw [← not_lt]; exact not_congr lt_to_nat end Num namespace PosNum @[simp] theorem of_to_nat' : ∀ n : PosNum, Num.ofNat' (n : ℕ) = Num.pos n | 1 => by erw [@Num.ofNat'_bit true 0, Num.ofNat'_zero]; rfl | bit0 p => by simpa only [Nat.bit_false, cond_false, two_mul, of_to_nat' p] using Num.ofNat'_bit false p | bit1 p => by simpa only [Nat.bit_true, cond_true, two_mul, of_to_nat' p] using Num.ofNat'_bit true p end PosNum namespace Num @[simp, norm_cast] theorem of_to_nat' : ∀ n : Num, Num.ofNat' (n : ℕ) = n | 0 => ofNat'_zero | pos p => p.of_to_nat' lemma toNat_injective : Function.Injective (castNum : Num → ℕ) := Function.LeftInverse.injective of_to_nat' @[norm_cast] theorem to_nat_inj {m n : Num} : (m : ℕ) = n ↔ m = n := toNat_injective.eq_iff /-- This tactic tries to turn an (in)equality about `Num`s to one about `Nat`s by rewriting. ```lean example (n : Num) (m : Num) : n ≤ n + m := by transfer_rw exact Nat.le_add_right _ _ ``` -/ scoped macro (name := transfer_rw) "transfer_rw" : tactic => `(tactic| (repeat first | rw [← to_nat_inj] | rw [← lt_to_nat] | rw [← le_to_nat] repeat first | rw [add_to_nat] | rw [mul_to_nat] | rw [cast_one] | rw [cast_zero])) /-- This tactic tries to prove (in)equalities about `Num`s by transferring them to the `Nat` world and then trying to call `simp`. ```lean example (n : Num) (m : Num) : n ≤ n + m := by transfer ``` -/ scoped macro (name := transfer) "transfer" : tactic => `(tactic| (intros; transfer_rw; try simp)) instance addMonoid : AddMonoid Num where add := (· + ·) zero := 0 zero_add := zero_add add_zero := add_zero add_assoc := by transfer nsmul := nsmulRec instance addMonoidWithOne : AddMonoidWithOne Num := { Num.addMonoid with natCast := Num.ofNat' one := 1 natCast_zero := ofNat'_zero natCast_succ := fun _ => ofNat'_succ } instance commSemiring : CommSemiring Num where __ := Num.addMonoid __ := Num.addMonoidWithOne mul := (· * ·) npow := @npowRec Num ⟨1⟩ ⟨(· * ·)⟩ mul_zero _ := by rw [← to_nat_inj, mul_to_nat, cast_zero, mul_zero] zero_mul _ := by rw [← to_nat_inj, mul_to_nat, cast_zero, zero_mul] mul_one _ := by rw [← to_nat_inj, mul_to_nat, cast_one, mul_one] one_mul _ := by rw [← to_nat_inj, mul_to_nat, cast_one, one_mul] add_comm _ _ := by simp_rw [← to_nat_inj, add_to_nat, add_comm] mul_comm _ _ := by simp_rw [← to_nat_inj, mul_to_nat, mul_comm] mul_assoc _ _ _ := by simp_rw [← to_nat_inj, mul_to_nat, mul_assoc] left_distrib _ _ _ := by simp only [← to_nat_inj, mul_to_nat, add_to_nat, mul_add] right_distrib _ _ _ := by simp only [← to_nat_inj, mul_to_nat, add_to_nat, add_mul] instance partialOrder : PartialOrder Num where lt_iff_le_not_le a b := by simp only [← lt_to_nat, ← le_to_nat, lt_iff_le_not_le] le_refl := by transfer le_trans a b c := by transfer_rw; apply le_trans le_antisymm a b := by transfer_rw; apply le_antisymm instance isOrderedCancelAddMonoid : IsOrderedCancelAddMonoid Num where add_le_add_left a b h c := by revert h; transfer_rw; exact fun h => add_le_add_left h c le_of_add_le_add_left a b c := show a + b ≤ a + c → b ≤ c by transfer_rw; apply le_of_add_le_add_left instance linearOrder : LinearOrder Num := { le_total := by intro a b transfer_rw apply le_total toDecidableLT := Num.decidableLT toDecidableLE := Num.decidableLE -- This is relying on an automatically generated instance name, -- generated in a `deriving` handler. -- See https://github.com/leanprover/lean4/issues/2343 toDecidableEq := instDecidableEqNum } instance isStrictOrderedRing : IsStrictOrderedRing Num := { zero_le_one := by decide mul_lt_mul_of_pos_left := by intro a b c transfer_rw apply mul_lt_mul_of_pos_left mul_lt_mul_of_pos_right := by intro a b c transfer_rw apply mul_lt_mul_of_pos_right exists_pair_ne := ⟨0, 1, by decide⟩ } @[norm_cast] theorem add_of_nat (m n) : ((m + n : ℕ) : Num) = m + n := add_ofNat' _ _ @[norm_cast] theorem to_nat_to_int (n : Num) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int {α} [AddGroupWithOne α] (n : Num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, Int.cast_natCast, cast_to_nat] theorem to_of_nat : ∀ n : ℕ, ((n : Num) : ℕ) = n | 0 => by rw [Nat.cast_zero, cast_zero] | n + 1 => by rw [Nat.cast_succ, add_one, succ_to_nat, to_of_nat n] @[simp, norm_cast] theorem of_natCast {α} [AddMonoidWithOne α] (n : ℕ) : ((n : Num) : α) = n := by rw [← cast_to_nat, to_of_nat] @[norm_cast] theorem of_nat_inj {m n : ℕ} : (m : Num) = n ↔ m = n := ⟨fun h => Function.LeftInverse.injective to_of_nat h, congr_arg _⟩ -- The priority should be `high`er than `cast_to_nat`. @[simp high, norm_cast] theorem of_to_nat : ∀ n : Num, ((n : ℕ) : Num) = n := of_to_nat' @[norm_cast] theorem dvd_to_nat (m n : Num) : (m : ℕ) ∣ n ↔ m ∣ n := ⟨fun ⟨k, e⟩ => ⟨k, by rw [← of_to_nat n, e]; simp⟩, fun ⟨k, e⟩ => ⟨k, by simp [e, mul_to_nat]⟩⟩ end Num namespace PosNum variable {α : Type*} open Num -- The priority should be `high`er than `cast_to_nat`. @[simp high, norm_cast] theorem of_to_nat : ∀ n : PosNum, ((n : ℕ) : Num) = Num.pos n := of_to_nat' @[norm_cast] theorem to_nat_inj {m n : PosNum} : (m : ℕ) = n ↔ m = n := ⟨fun h => Num.pos.inj <| by rw [← PosNum.of_to_nat, ← PosNum.of_to_nat, h], congr_arg _⟩ theorem pred'_to_nat : ∀ n, (pred' n : ℕ) = Nat.pred n | 1 => rfl | bit0 n => have : Nat.succ ↑(pred' n) = ↑n := by rw [pred'_to_nat n, Nat.succ_pred_eq_of_pos (to_nat_pos n)] match (motive := ∀ k : Num, Nat.succ ↑k = ↑n → ↑(Num.casesOn k 1 bit1 : PosNum) = Nat.pred (n + n)) pred' n, this with | 0, (h : ((1 : Num) : ℕ) = n) => by rw [← to_nat_inj.1 h]; rfl | Num.pos p, (h : Nat.succ ↑p = n) => by rw [← h]; exact (Nat.succ_add p p).symm | bit1 _ => rfl @[simp] theorem pred'_succ' (n) : pred' (succ' n) = n := Num.to_nat_inj.1 <| by rw [pred'_to_nat, succ'_to_nat, Nat.add_one, Nat.pred_succ] @[simp] theorem succ'_pred' (n) : succ' (pred' n) = n := to_nat_inj.1 <| by rw [succ'_to_nat, pred'_to_nat, Nat.add_one, Nat.succ_pred_eq_of_pos (to_nat_pos _)] instance dvd : Dvd PosNum := ⟨fun m n => pos m ∣ pos n⟩ @[norm_cast] theorem dvd_to_nat {m n : PosNum} : (m : ℕ) ∣ n ↔ m ∣ n := Num.dvd_to_nat (pos m) (pos n) theorem size_to_nat : ∀ n, (size n : ℕ) = Nat.size n | 1 => Nat.size_one.symm | bit0 n => by rw [size, succ_to_nat, size_to_nat n, cast_bit0, ← two_mul] erw [@Nat.size_bit false n] have := to_nat_pos n dsimp [Nat.bit]; omega | bit1 n => by rw [size, succ_to_nat, size_to_nat n, cast_bit1, ← two_mul] erw [@Nat.size_bit true n] dsimp [Nat.bit]; omega theorem size_eq_natSize : ∀ n, (size n : ℕ) = natSize n | 1 => rfl | bit0 n => by rw [size, succ_to_nat, natSize, size_eq_natSize n] | bit1 n => by rw [size, succ_to_nat, natSize, size_eq_natSize n] theorem natSize_to_nat (n) : natSize n = Nat.size n := by rw [← size_eq_natSize, size_to_nat] theorem natSize_pos (n) : 0 < natSize n := by cases n <;> apply Nat.succ_pos /-- This tactic tries to turn an (in)equality about `PosNum`s to one about `Nat`s by rewriting. ```lean example (n : PosNum) (m : PosNum) : n ≤ n + m := by transfer_rw exact Nat.le_add_right _ _ ``` -/ scoped macro (name := transfer_rw) "transfer_rw" : tactic => `(tactic| (repeat first | rw [← to_nat_inj] | rw [← lt_to_nat] | rw [← le_to_nat] repeat first | rw [add_to_nat] | rw [mul_to_nat] | rw [cast_one] | rw [cast_zero])) /-- This tactic tries to prove (in)equalities about `PosNum`s by transferring them to the `Nat` world and then trying to call `simp`. ```lean example (n : PosNum) (m : PosNum) : n ≤ n + m := by transfer ``` -/ scoped macro (name := transfer) "transfer" : tactic => `(tactic| (intros; transfer_rw; try simp [add_comm, add_left_comm, mul_comm, mul_left_comm])) instance addCommSemigroup : AddCommSemigroup PosNum where add := (· + ·) add_assoc := by transfer add_comm := by transfer instance commMonoid : CommMonoid PosNum where mul := (· * ·) one := (1 : PosNum) npow := @npowRec PosNum ⟨1⟩ ⟨(· * ·)⟩ mul_assoc := by transfer one_mul := by transfer mul_one := by transfer mul_comm := by transfer instance distrib : Distrib PosNum where add := (· + ·) mul := (· * ·)
left_distrib := by transfer; simp [mul_add] right_distrib := by transfer; simp [mul_add, mul_comm]
Mathlib/Data/Num/Lemmas.lean
540
541
/- 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 @[simp] theorem disjoint_inv_left_iff : Disjoint f⁻¹ g ↔ Disjoint f g := by refine ⟨fun h => ?_, Disjoint.inv_left⟩ convert h.inv_left @[simp] theorem disjoint_inv_right_iff : Disjoint f g⁻¹ ↔ Disjoint f g := by rw [disjoint_comm, disjoint_inv_left_iff, disjoint_comm] theorem Disjoint.mul_left (H1 : Disjoint f h) (H2 : Disjoint g h) : Disjoint (f * g) h := fun x => by cases H1 x <;> cases H2 x <;> simp [*] theorem Disjoint.mul_right (H1 : Disjoint f g) (H2 : Disjoint f h) : Disjoint f (g * h) := by rw [disjoint_comm] exact H1.symm.mul_left H2.symm -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: make it `@[simp]` theorem disjoint_conj (h : Perm α) : Disjoint (h * f * h⁻¹) (h * g * h⁻¹) ↔ Disjoint f g := (h⁻¹).forall_congr fun {_} ↦ by simp only [mul_apply, eq_inv_iff_eq] theorem Disjoint.conj (H : Disjoint f g) (h : Perm α) : Disjoint (h * f * h⁻¹) (h * g * h⁻¹) := (disjoint_conj h).2 H theorem disjoint_prod_right (l : List (Perm α)) (h : ∀ g ∈ l, Disjoint f g) : Disjoint f l.prod := by induction' l with g l ih · exact disjoint_one_right _ · rw [List.prod_cons] exact (h _ List.mem_cons_self).mul_right (ih fun g hg => h g (List.mem_cons_of_mem _ hg)) theorem disjoint_noncommProd_right {ι : Type*} {k : ι → Perm α} {s : Finset ι} (hs : Set.Pairwise s fun i j ↦ Commute (k i) (k j)) (hg : ∀ i ∈ s, g.Disjoint (k i)) : Disjoint g (s.noncommProd k (hs)) := noncommProd_induction s k hs g.Disjoint (fun _ _ ↦ Disjoint.mul_right) (disjoint_one_right g) hg open scoped List in theorem disjoint_prod_perm {l₁ l₂ : List (Perm α)} (hl : l₁.Pairwise Disjoint) (hp : l₁ ~ l₂) : l₁.prod = l₂.prod := hp.prod_eq' <| hl.imp Disjoint.commute theorem nodup_of_pairwise_disjoint {l : List (Perm α)} (h1 : (1 : Perm α) ∉ l) (h2 : l.Pairwise Disjoint) : l.Nodup := by refine List.Pairwise.imp_of_mem ?_ h2 intro τ σ h_mem _ h_disjoint _ subst τ suffices (σ : Perm α) = 1 by rw [this] at h_mem exact h1 h_mem exact ext fun a => or_self_iff.mp (h_disjoint a) theorem pow_apply_eq_self_of_apply_eq_self {x : α} (hfx : f x = x) : ∀ n : ℕ, (f ^ n) x = x | 0 => rfl
| n + 1 => by rw [pow_succ, mul_apply, hfx, pow_apply_eq_self_of_apply_eq_self hfx n] theorem zpow_apply_eq_self_of_apply_eq_self {x : α} (hfx : f x = x) : ∀ n : ℤ, (f ^ n) x = x | (n : ℕ) => pow_apply_eq_self_of_apply_eq_self hfx n | Int.negSucc n => by rw [zpow_negSucc, inv_eq_iff_eq, pow_apply_eq_self_of_apply_eq_self hfx] theorem pow_apply_eq_of_apply_apply_eq_self {x : α} (hffx : f (f x) = x) : ∀ n : ℕ, (f ^ n) x = x ∨ (f ^ n) x = f x | 0 => Or.inl rfl
Mathlib/GroupTheory/Perm/Support.lean
144
152
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kevin Kappelmann -/ import Mathlib.Algebra.Order.Floor.Defs import Mathlib.Algebra.Order.Floor.Ring import Mathlib.Algebra.Order.Floor.Semiring deprecated_module (since := "2025-04-13")
Mathlib/Algebra/Order/Floor.lean
1,598
1,600
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer, Andrew Yang -/ import Mathlib.Algebra.Category.Ring.Colimits import Mathlib.Algebra.Category.Ring.FilteredColimits import Mathlib.Algebra.Category.Ring.Limits import Mathlib.Algebra.Order.Group.Nat import Mathlib.Geometry.RingedSpace.SheafedSpace import Mathlib.Topology.Sheaves.Stalks /-! # Ringed spaces We introduce the category of ringed spaces, as an alias for `SheafedSpace CommRingCat`. The facts collected in this file are typically stated for locally ringed spaces, but never actually make use of the locality of stalks. See for instance <https://stacks.math.columbia.edu/tag/01HZ>. -/ universe v u open CategoryTheory open TopologicalSpace open Opposite open TopCat open TopCat.Presheaf namespace AlgebraicGeometry /-- The type of Ringed spaces, as an abbreviation for `SheafedSpace CommRingCat`. -/ @[nolint checkUnivs] -- The universes appear together in the type, but separately in the value. abbrev RingedSpace : Type max (u+1) (v+1) := SheafedSpace.{v+1, v, u} CommRingCat.{v} namespace RingedSpace open SheafedSpace @[simp] lemma res_zero {X : RingedSpace.{u}} {U V : TopologicalSpace.Opens X} (hUV : U ≤ V) : (0 : X.presheaf.obj (op V)) |_ U = (0 : X.presheaf.obj (op U)) := RingHom.map_zero _ variable (X : RingedSpace) instance : CoeSort RingedSpace Type* where coe X := X.carrier /-- If the germ of a section `f` is zero in the stalk at `x`, then `f` is zero on some neighbourhood around `x`. -/ lemma exists_res_eq_zero_of_germ_eq_zero (U : Opens X) (f : X.presheaf.obj (op U)) (x : U) (h : X.presheaf.germ U x.val x.property f = 0) : ∃ (V : Opens X) (i : V ⟶ U) (_ : x.1 ∈ V), X.presheaf.map i.op f = 0 := by have h1 : X.presheaf.germ U x.val x.property f = X.presheaf.germ U x.val x.property 0 := by simpa obtain ⟨V, hv, i, _, (hv4 : (X.presheaf.map i.op) f = (X.presheaf.map _) 0)⟩ := TopCat.Presheaf.germ_eq X.presheaf x.1 x.2 x.2 f 0 h1 use V, i, hv simpa using hv4 /-- If the germ of a section `f` is a unit in the stalk at `x`, then `f` must be a unit on some small neighborhood around `x`. -/ theorem isUnit_res_of_isUnit_germ (U : Opens X) (f : X.presheaf.obj (op U)) (x : X) (hx : x ∈ U) (h : IsUnit (X.presheaf.germ U x hx f)) : ∃ (V : Opens X) (i : V ⟶ U) (_ : x ∈ V), IsUnit (X.presheaf.map i.op f) := by obtain ⟨g', heq⟩ := h.exists_right_inv obtain ⟨V, hxV, g, rfl⟩ := X.presheaf.germ_exist x g' let W := U ⊓ V have hxW : x ∈ W := ⟨hx, hxV⟩ -- Porting note: `erw` can't write into `HEq`, so this is replaced with another `HEq` in the -- desired form replace heq : (X.presheaf.germ _ x hxW) ((X.presheaf.map (U.infLELeft V).op) f * (X.presheaf.map (U.infLERight V).op) g) = (X.presheaf.germ _ x hxW) 1 := by dsimp [germ] erw [map_mul, map_one, show X.presheaf.germ _ x hxW ((X.presheaf.map (U.infLELeft V).op) f) = X.presheaf.germ U x hx f from X.presheaf.germ_res_apply (Opens.infLELeft U V) x hxW f, show X.presheaf.germ _ x hxW (X.presheaf.map (U.infLERight V).op g) = X.presheaf.germ _ x hxV g from X.presheaf.germ_res_apply (Opens.infLERight U V) x hxW g] exact heq -- note: we have to force lean to resynthesize this as <...>.hom _ = <...>.hom _ obtain ⟨W', hxW', i₁, i₂, (heq' : (X.presheaf.map i₁.op) _ = (X.presheaf.map i₂.op) 1)⟩ := X.presheaf.germ_eq x hxW hxW _ _ heq use W', i₁ ≫ Opens.infLELeft U V, hxW' simp only [map_mul, map_one] at heq' simpa using isUnit_of_mul_eq_one _ _ heq' @[deprecated (since := "2025-02-08")] alias _root_.CommRingCat.germ_res_apply := germ_res_apply @[deprecated (since := "2025-02-08")] alias _root_.CommRingCat.germ_res_apply' := germ_res_apply' /-- If a section `f` is a unit in each stalk, `f` must be a unit. -/ theorem isUnit_of_isUnit_germ (U : Opens X) (f : X.presheaf.obj (op U)) (h : ∀ (x) (hx : x ∈ U), IsUnit (X.presheaf.germ U x hx f)) : IsUnit f := by -- We pick a cover of `U` by open sets `V x`, such that `f` is a unit on each `V x`. choose V iVU m h_unit using fun x : U => X.isUnit_res_of_isUnit_germ U f x x.2 (h x.1 x.2) have hcover : U ≤ iSup V := by intro x hxU -- Porting note: in Lean3 `rw` is sufficient erw [Opens.mem_iSup] exact ⟨⟨x, hxU⟩, m ⟨x, hxU⟩⟩ -- Let `g x` denote the inverse of `f` in `U x`. choose g hg using fun x : U => IsUnit.exists_right_inv (h_unit x) have ic : IsCompatible (sheaf X).val V g := by intro x y apply section_ext X.sheaf (V x ⊓ V y) rintro z ⟨hzVx, hzVy⟩ rw [germ_res_apply, germ_res_apply] apply (h z ((iVU x).le hzVx)).mul_right_inj.mp -- Porting note: now need explicitly typing the rewrites -- note: this is bad, I think we should replace the `FunLike` on -- concrete category with `CoeFun` rw [← germ_res_apply X.presheaf (iVU x) z hzVx f] -- Porting note: change was not necessary in Lean3 change X.presheaf.germ _ z hzVx _ * (X.presheaf.germ _ z hzVx _) = X.presheaf.germ _ z hzVx _ * X.presheaf.germ _ z hzVy (g y) rw [← RingHom.map_mul, congr_arg (X.presheaf.germ (V x) z hzVx) (hg x), germ_res_apply X.presheaf _ _ _ f, ← germ_res_apply X.presheaf (iVU y) z hzVy f, ← RingHom.map_mul, congr_arg (X.presheaf.germ (V y) z hzVy) (hg y), RingHom.map_one, RingHom.map_one] -- We claim that these local inverses glue together to a global inverse of `f`. obtain ⟨gl, gl_spec, -⟩ : -- We need to rephrase the result from `HasForget` to `CommRingCat`. ∃ gl : X.presheaf.obj (op U), (∀ i, ((sheaf X).val.map (iVU i).op) gl = g i) ∧ _ := X.sheaf.existsUnique_gluing' V U iVU hcover g ic apply isUnit_of_mul_eq_one f gl apply X.sheaf.eq_of_locally_eq' V U iVU hcover intro i -- We need to rephrase the goal from `HasForget` to `CommRingCat`. show ((sheaf X).val.map (iVU i).op).hom (f * gl) = ((sheaf X).val.map (iVU i).op) 1 rw [RingHom.map_one, RingHom.map_mul, gl_spec] exact hg i /-- The basic open of a section `f` is the set of all points `x`, such that the germ of `f` at `x` is a unit. -/ def basicOpen {U : Opens X} (f : X.presheaf.obj (op U)) : Opens X where carrier := { x : X | ∃ (hx : x ∈ U), IsUnit (X.presheaf.germ U x hx f) } is_open' := by rw [isOpen_iff_forall_mem_open] rintro x ⟨hxU, hx⟩ obtain ⟨V, i, hxV, hf⟩ := X.isUnit_res_of_isUnit_germ U f x hxU hx use V.1 refine ⟨?_, V.2, hxV⟩ intro y hy use i.le hy convert RingHom.isUnit_map (X.presheaf.germ _ y hy).hom hf exact (X.presheaf.germ_res_apply i y hy f).symm theorem mem_basicOpen {U : Opens X} (f : X.presheaf.obj (op U)) (x : X) (hx : x ∈ U) : x ∈ X.basicOpen f ↔ IsUnit (X.presheaf.germ U x hx f) := ⟨Exists.choose_spec, (⟨hx, ·⟩)⟩ /-- A variant of `mem_basicOpen` with bundled `x : U`. -/ @[simp] theorem mem_basicOpen' {U : Opens X} (f : X.presheaf.obj (op U)) (x : U) : ↑x ∈ X.basicOpen f ↔ IsUnit (X.presheaf.germ U x.1 x.2 f) := mem_basicOpen X f x.1 x.2 @[simp] theorem mem_top_basicOpen (f : X.presheaf.obj (op ⊤)) (x : X) : x ∈ X.basicOpen f ↔ IsUnit (X.presheaf.Γgerm x f) := mem_basicOpen X f x .intro theorem basicOpen_le {U : Opens X} (f : X.presheaf.obj (op U)) : X.basicOpen f ≤ U := by rintro x ⟨h, _⟩; exact h /-- The restriction of a section `f` to the basic open of `f` is a unit. -/ theorem isUnit_res_basicOpen {U : Opens X} (f : X.presheaf.obj (op U)) : IsUnit (X.presheaf.map (@homOfLE (Opens X) _ _ _ (X.basicOpen_le f)).op f) := by apply isUnit_of_isUnit_germ rintro x ⟨hxU, hx⟩ convert hx exact X.presheaf.germ_res_apply _ _ _ _ @[simp] theorem basicOpen_res {U V : (Opens X)ᵒᵖ} (i : U ⟶ V) (f : X.presheaf.obj U) : @basicOpen X (unop V) (X.presheaf.map i f) = unop V ⊓ @basicOpen X (unop U) f := by ext x; constructor · rintro ⟨hxV, hx⟩ rw [germ_res_apply' X.presheaf] at hx exact ⟨hxV, i.unop.le hxV, hx⟩ · rintro ⟨hxV, _, hx⟩ refine ⟨hxV, ?_⟩ rw [germ_res_apply' X.presheaf] exact hx -- This should fire before `basicOpen_res`. -- Porting note: this lemma is not in simple normal form because of `basicOpen_res`, as in Lean3 -- it is specifically said "This should fire before `basic_open_res`", this lemma is marked with -- high priority @[simp (high)] theorem basicOpen_res_eq {U V : (Opens X)ᵒᵖ} (i : U ⟶ V) [IsIso i] (f : X.presheaf.obj U) : @basicOpen X (unop V) (X.presheaf.map i f) = @RingedSpace.basicOpen X (unop U) f := by apply le_antisymm · rw [X.basicOpen_res i f]; exact inf_le_right · have := X.basicOpen_res (inv i) (X.presheaf.map i f) rw [← CommRingCat.comp_apply, ← X.presheaf.map_comp, IsIso.hom_inv_id, X.presheaf.map_id, CommRingCat.id_apply] at this rw [this] exact inf_le_right @[simp] theorem basicOpen_mul {U : Opens X} (f g : X.presheaf.obj (op U)) : X.basicOpen (f * g) = X.basicOpen f ⊓ X.basicOpen g := by ext x by_cases hx : x ∈ U · simp [mem_basicOpen (hx := hx)] · simp [mt (basicOpen_le X _ ·) hx] @[simp] lemma basicOpen_pow {U : Opens X} (f : X.presheaf.obj (op U)) (n : ℕ) (h : 0 < n) : X.basicOpen (f ^ n) = X.basicOpen f := by obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le' h induction k with | zero => simp | succ n hn => rw [pow_add]; simp_all
theorem basicOpen_of_isUnit {U : Opens X} {f : X.presheaf.obj (op U)} (hf : IsUnit f) : X.basicOpen f = U := by apply le_antisymm · exact X.basicOpen_le f intro x hx rw [SetLike.mem_coe, X.mem_basicOpen f x hx]
Mathlib/Geometry/RingedSpace/Basic.lean
226
232
/- 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 := (f : E →L[𝕜] F).contDiff /-- The identity is `C^n`. -/ theorem contDiff_id : ContDiff 𝕜 n (id : E → E) := IsBoundedLinearMap.id.contDiff theorem contDiffWithinAt_id {s x} : ContDiffWithinAt 𝕜 n (id : E → E) s x := contDiff_id.contDiffWithinAt theorem contDiffAt_id {x} : ContDiffAt 𝕜 n (id : E → E) x := contDiff_id.contDiffAt theorem contDiffOn_id {s} : ContDiffOn 𝕜 n (id : E → E) s := contDiff_id.contDiffOn /-- Bilinear functions are `C^n`. -/ theorem IsBoundedBilinearMap.contDiff (hb : IsBoundedBilinearMap 𝕜 b) : ContDiff 𝕜 n b := (hb.toContinuousLinearMap.analyticOnNhd_bilinear _).contDiff /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor series whose `k`-th term is given by `g ∘ (p k)`. -/ theorem HasFTaylorSeriesUpToOn.continuousLinearMap_comp {n : WithTop ℕ∞} (g : F →L[𝕜] G) (hf : HasFTaylorSeriesUpToOn n f p s) : HasFTaylorSeriesUpToOn n (g ∘ f) (fun x k => g.compContinuousMultilinearMap (p x k)) s where zero_eq x hx := congr_arg g (hf.zero_eq x hx) fderivWithin m hm x hx := (ContinuousLinearMap.compContinuousMultilinearMapL 𝕜 (fun _ : Fin m => E) F G g).hasFDerivAt.comp_hasFDerivWithinAt x (hf.fderivWithin m hm x hx) cont m hm := (ContinuousLinearMap.compContinuousMultilinearMapL 𝕜 (fun _ : Fin m => E) F G g).continuous.comp_continuousOn (hf.cont m hm) /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ theorem ContDiffWithinAt.continuousLinearMap_comp (g : F →L[𝕜] G) (hf : ContDiffWithinAt 𝕜 n f s x) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := by match n with | ω => obtain ⟨u, hu, p, hp, h'p⟩ := hf refine ⟨u, hu, _, hp.continuousLinearMap_comp g, fun i ↦ ?_⟩ change AnalyticOn 𝕜 (fun x ↦ (ContinuousLinearMap.compContinuousMultilinearMapL 𝕜 (fun _ : Fin i ↦ E) F G g) (p x i)) u apply AnalyticOnNhd.comp_analyticOn _ (h'p i) (Set.mapsTo_univ _ _) exact ContinuousLinearMap.analyticOnNhd _ _ | (n : ℕ∞) => intro m hm rcases hf m hm with ⟨u, hu, p, hp⟩ exact ⟨u, hu, _, hp.continuousLinearMap_comp g⟩ /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ theorem ContDiffAt.continuousLinearMap_comp (g : F →L[𝕜] G) (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (g ∘ f) x := ContDiffWithinAt.continuousLinearMap_comp g hf /-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/ theorem ContDiffOn.continuousLinearMap_comp (g : F →L[𝕜] G) (hf : ContDiffOn 𝕜 n f s) : ContDiffOn 𝕜 n (g ∘ f) s := fun x hx => (hf x hx).continuousLinearMap_comp g /-- Composition by continuous linear maps on the left preserves `C^n` functions. -/ theorem ContDiff.continuousLinearMap_comp {f : E → F} (g : F →L[𝕜] G) (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n fun x => g (f x) := contDiffOn_univ.1 <| ContDiffOn.continuousLinearMap_comp _ (contDiffOn_univ.2 hf) /-- The iterated derivative within a set of the composition with a linear map on the left is obtained by applying the linear map to the iterated derivative. -/ theorem ContinuousLinearMap.iteratedFDerivWithin_comp_left {f : E → F} (g : F →L[𝕜] G) (hf : ContDiffWithinAt 𝕜 n f s x) (hs : UniqueDiffOn 𝕜 s) (hx : x ∈ s) {i : ℕ} (hi : i ≤ n) : iteratedFDerivWithin 𝕜 i (g ∘ f) s x = g.compContinuousMultilinearMap (iteratedFDerivWithin 𝕜 i f s x) := by rcases hf.contDiffOn' hi (by simp) with ⟨U, hU, hxU, hfU⟩ rw [← iteratedFDerivWithin_inter_open hU hxU, ← iteratedFDerivWithin_inter_open (f := f) hU hxU] rw [insert_eq_of_mem hx] at hfU exact .symm <| (hfU.ftaylorSeriesWithin (hs.inter hU)).continuousLinearMap_comp g |>.eq_iteratedFDerivWithin_of_uniqueDiffOn le_rfl (hs.inter hU) ⟨hx, hxU⟩ /-- The iterated derivative of the composition with a linear map on the left is obtained by applying the linear map to the iterated derivative. -/ theorem ContinuousLinearMap.iteratedFDeriv_comp_left {f : E → F} (g : F →L[𝕜] G) (hf : ContDiffAt 𝕜 n f x) {i : ℕ} (hi : i ≤ n) : iteratedFDeriv 𝕜 i (g ∘ f) x = g.compContinuousMultilinearMap (iteratedFDeriv 𝕜 i f x) := by simp only [← iteratedFDerivWithin_univ] exact g.iteratedFDerivWithin_comp_left hf.contDiffWithinAt uniqueDiffOn_univ (mem_univ x) hi /-- The iterated derivative within a set of the composition with a linear equiv on the left is obtained by applying the linear equiv to the iterated derivative. This is true without differentiability assumptions. -/ theorem ContinuousLinearEquiv.iteratedFDerivWithin_comp_left (g : F ≃L[𝕜] G) (f : E → F) (hs : UniqueDiffOn 𝕜 s) (hx : x ∈ s) (i : ℕ) : iteratedFDerivWithin 𝕜 i (g ∘ f) s x = (g : F →L[𝕜] G).compContinuousMultilinearMap (iteratedFDerivWithin 𝕜 i f s x) := by induction' i with i IH generalizing x · ext1 m simp only [iteratedFDerivWithin_zero_apply, comp_apply, ContinuousLinearMap.compContinuousMultilinearMap_coe, coe_coe] · ext1 m rw [iteratedFDerivWithin_succ_apply_left] have Z : fderivWithin 𝕜 (iteratedFDerivWithin 𝕜 i (g ∘ f) s) s x = fderivWithin 𝕜 (g.continuousMultilinearMapCongrRight (fun _ : Fin i => E) ∘ iteratedFDerivWithin 𝕜 i f s) s x := fderivWithin_congr' (@IH) hx simp_rw [Z] rw [(g.continuousMultilinearMapCongrRight fun _ : Fin i => E).comp_fderivWithin (hs x hx)] simp only [ContinuousLinearMap.coe_comp', ContinuousLinearEquiv.coe_coe, comp_apply, ContinuousLinearEquiv.continuousMultilinearMapCongrRight_apply, ContinuousLinearMap.compContinuousMultilinearMap_coe, EmbeddingLike.apply_eq_iff_eq] rw [iteratedFDerivWithin_succ_apply_left] /-- Composition with a linear isometry on the left preserves the norm of the iterated derivative within a set. -/ theorem LinearIsometry.norm_iteratedFDerivWithin_comp_left {f : E → F} (g : F →ₗᵢ[𝕜] G) (hf : ContDiffWithinAt 𝕜 n f s x) (hs : UniqueDiffOn 𝕜 s) (hx : x ∈ s) {i : ℕ} (hi : i ≤ n) : ‖iteratedFDerivWithin 𝕜 i (g ∘ f) s x‖ = ‖iteratedFDerivWithin 𝕜 i f s x‖ := by have : iteratedFDerivWithin 𝕜 i (g ∘ f) s x = g.toContinuousLinearMap.compContinuousMultilinearMap (iteratedFDerivWithin 𝕜 i f s x) := g.toContinuousLinearMap.iteratedFDerivWithin_comp_left hf hs hx hi rw [this] apply LinearIsometry.norm_compContinuousMultilinearMap /-- Composition with a linear isometry on the left preserves the norm of the iterated derivative. -/ theorem LinearIsometry.norm_iteratedFDeriv_comp_left {f : E → F} (g : F →ₗᵢ[𝕜] G) (hf : ContDiffAt 𝕜 n f x) {i : ℕ} (hi : i ≤ n) : ‖iteratedFDeriv 𝕜 i (g ∘ f) x‖ = ‖iteratedFDeriv 𝕜 i f x‖ := by simp only [← iteratedFDerivWithin_univ] exact g.norm_iteratedFDerivWithin_comp_left hf.contDiffWithinAt uniqueDiffOn_univ (mem_univ x) hi /-- Composition with a linear isometry equiv on the left preserves the norm of the iterated derivative within a set. -/ theorem LinearIsometryEquiv.norm_iteratedFDerivWithin_comp_left (g : F ≃ₗᵢ[𝕜] G) (f : E → F) (hs : UniqueDiffOn 𝕜 s) (hx : x ∈ s) (i : ℕ) : ‖iteratedFDerivWithin 𝕜 i (g ∘ f) s x‖ = ‖iteratedFDerivWithin 𝕜 i f s x‖ := by have : iteratedFDerivWithin 𝕜 i (g ∘ f) s x = (g : F →L[𝕜] G).compContinuousMultilinearMap (iteratedFDerivWithin 𝕜 i f s x) := g.toContinuousLinearEquiv.iteratedFDerivWithin_comp_left f hs hx i rw [this] apply LinearIsometry.norm_compContinuousMultilinearMap g.toLinearIsometry /-- Composition with a linear isometry equiv on the left preserves the norm of the iterated derivative. -/ theorem LinearIsometryEquiv.norm_iteratedFDeriv_comp_left (g : F ≃ₗᵢ[𝕜] G) (f : E → F) (x : E) (i : ℕ) : ‖iteratedFDeriv 𝕜 i (g ∘ f) x‖ = ‖iteratedFDeriv 𝕜 i f x‖ := by rw [← iteratedFDerivWithin_univ, ← iteratedFDerivWithin_univ] apply g.norm_iteratedFDerivWithin_comp_left f uniqueDiffOn_univ (mem_univ x) i /-- Composition by continuous linear equivs on the left respects higher differentiability at a point in a domain. -/ theorem ContinuousLinearEquiv.comp_contDiffWithinAt_iff (e : F ≃L[𝕜] G) : ContDiffWithinAt 𝕜 n (e ∘ f) s x ↔ ContDiffWithinAt 𝕜 n f s x := ⟨fun H => by simpa only [Function.comp_def, e.symm.coe_coe, e.symm_apply_apply] using H.continuousLinearMap_comp (e.symm : G →L[𝕜] F), fun H => H.continuousLinearMap_comp (e : F →L[𝕜] G)⟩ /-- Composition by continuous linear equivs on the left respects higher differentiability at a point. -/ theorem ContinuousLinearEquiv.comp_contDiffAt_iff (e : F ≃L[𝕜] G) : ContDiffAt 𝕜 n (e ∘ f) x ↔ ContDiffAt 𝕜 n f x := by simp only [← contDiffWithinAt_univ, e.comp_contDiffWithinAt_iff] /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ theorem ContinuousLinearEquiv.comp_contDiffOn_iff (e : F ≃L[𝕜] G) : ContDiffOn 𝕜 n (e ∘ f) s ↔ ContDiffOn 𝕜 n f s := by simp [ContDiffOn, e.comp_contDiffWithinAt_iff] /-- Composition by continuous linear equivs on the left respects higher differentiability. -/ theorem ContinuousLinearEquiv.comp_contDiff_iff (e : F ≃L[𝕜] G) : ContDiff 𝕜 n (e ∘ f) ↔ ContDiff 𝕜 n f := by simp only [← contDiffOn_univ, e.comp_contDiffOn_iff] /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/ theorem HasFTaylorSeriesUpToOn.compContinuousLinearMap (hf : HasFTaylorSeriesUpToOn n f p s) (g : G →L[𝕜] E) : HasFTaylorSeriesUpToOn n (f ∘ g) (fun x k => (p (g x) k).compContinuousLinearMap fun _ => g) (g ⁻¹' s) := by let A : ∀ m : ℕ, (E[×m]→L[𝕜] F) → G[×m]→L[𝕜] F := fun m h => h.compContinuousLinearMap fun _ => g have hA : ∀ m, IsBoundedLinearMap 𝕜 (A m) := fun m => isBoundedLinearMap_continuousMultilinearMap_comp_linear g constructor · intro x hx simp only [(hf.zero_eq (g x) hx).symm, Function.comp_apply] change (p (g x) 0 fun _ : Fin 0 => g 0) = p (g x) 0 0 rw [ContinuousLinearMap.map_zero] rfl · intro m hm x hx convert (hA m).hasFDerivAt.comp_hasFDerivWithinAt x ((hf.fderivWithin m hm (g x) hx).comp x g.hasFDerivWithinAt (Subset.refl _)) ext y v change p (g x) (Nat.succ m) (g ∘ cons y v) = p (g x) m.succ (cons (g y) (g ∘ v)) rw [comp_cons] · intro m hm exact (hA m).continuous.comp_continuousOn <| (hf.cont m hm).comp g.continuous.continuousOn <| Subset.refl _ /-- Composition by continuous linear maps on the right preserves `C^n` functions at a point on a domain. -/ theorem ContDiffWithinAt.comp_continuousLinearMap {x : G} (g : G →L[𝕜] E) (hf : ContDiffWithinAt 𝕜 n f s (g x)) : ContDiffWithinAt 𝕜 n (f ∘ g) (g ⁻¹' s) x := by match n with | ω => obtain ⟨u, hu, p, hp, h'p⟩ := hf refine ⟨g ⁻¹' u, ?_, _, hp.compContinuousLinearMap g, ?_⟩ · refine g.continuous.continuousWithinAt.tendsto_nhdsWithin ?_ hu exact (mapsTo_singleton.2 <| mem_singleton _).union_union (mapsTo_preimage _ _) · intro i change AnalyticOn 𝕜 (fun x ↦ ContinuousMultilinearMap.compContinuousLinearMapL (fun _ ↦ g) (p (g x) i)) (⇑g ⁻¹' u) apply AnalyticOn.comp _ _ (Set.mapsTo_univ _ _) · exact ContinuousLinearEquiv.analyticOn _ _ · exact (h'p i).comp (g.analyticOn _) (mapsTo_preimage _ _) | (n : ℕ∞) => intro m hm rcases hf m hm with ⟨u, hu, p, hp⟩ refine ⟨g ⁻¹' u, ?_, _, hp.compContinuousLinearMap g⟩ refine g.continuous.continuousWithinAt.tendsto_nhdsWithin ?_ hu exact (mapsTo_singleton.2 <| mem_singleton _).union_union (mapsTo_preimage _ _) /-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/ theorem ContDiffOn.comp_continuousLinearMap (hf : ContDiffOn 𝕜 n f s) (g : G →L[𝕜] E) : ContDiffOn 𝕜 n (f ∘ g) (g ⁻¹' s) := fun x hx => (hf (g x) hx).comp_continuousLinearMap g /-- Composition by continuous linear maps on the right preserves `C^n` functions. -/ theorem ContDiff.comp_continuousLinearMap {f : E → F} {g : G →L[𝕜] E} (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n (f ∘ g) := contDiffOn_univ.1 <| ContDiffOn.comp_continuousLinearMap (contDiffOn_univ.2 hf) _ /-- The iterated derivative within a set of the composition with a linear map on the right is obtained by composing the iterated derivative with the linear map. -/ theorem ContinuousLinearMap.iteratedFDerivWithin_comp_right {f : E → F} (g : G →L[𝕜] E) (hf : ContDiffOn 𝕜 n f s) (hs : UniqueDiffOn 𝕜 s) (h's : UniqueDiffOn 𝕜 (g ⁻¹' s)) {x : G} (hx : g x ∈ s) {i : ℕ} (hi : i ≤ n) : iteratedFDerivWithin 𝕜 i (f ∘ g) (g ⁻¹' s) x = (iteratedFDerivWithin 𝕜 i f s (g x)).compContinuousLinearMap fun _ => g := ((((hf.of_le hi).ftaylorSeriesWithin hs).compContinuousLinearMap g).eq_iteratedFDerivWithin_of_uniqueDiffOn le_rfl h's hx).symm /-- The iterated derivative within a set of the composition with a linear equiv on the right is obtained by composing the iterated derivative with the linear equiv. -/ theorem ContinuousLinearEquiv.iteratedFDerivWithin_comp_right (g : G ≃L[𝕜] E) (f : E → F) (hs : UniqueDiffOn 𝕜 s) {x : G} (hx : g x ∈ s) (i : ℕ) : iteratedFDerivWithin 𝕜 i (f ∘ g) (g ⁻¹' s) x = (iteratedFDerivWithin 𝕜 i f s (g x)).compContinuousLinearMap fun _ => g := by induction' i with i IH generalizing x · ext1 simp only [iteratedFDerivWithin_zero_apply, comp_apply, ContinuousMultilinearMap.compContinuousLinearMap_apply] · ext1 m simp only [ContinuousMultilinearMap.compContinuousLinearMap_apply, ContinuousLinearEquiv.coe_coe, iteratedFDerivWithin_succ_apply_left] have : fderivWithin 𝕜 (iteratedFDerivWithin 𝕜 i (f ∘ g) (g ⁻¹' s)) (g ⁻¹' s) x = fderivWithin 𝕜 (ContinuousLinearEquiv.continuousMultilinearMapCongrLeft _ (fun _x : Fin i => g) ∘ (iteratedFDerivWithin 𝕜 i f s ∘ g)) (g ⁻¹' s) x := fderivWithin_congr' (@IH) hx rw [this, ContinuousLinearEquiv.comp_fderivWithin _ (g.uniqueDiffOn_preimage_iff.2 hs x hx)] simp only [ContinuousLinearMap.coe_comp', ContinuousLinearEquiv.coe_coe, comp_apply, ContinuousLinearEquiv.continuousMultilinearMapCongrLeft_apply, ContinuousMultilinearMap.compContinuousLinearMap_apply] rw [ContinuousLinearEquiv.comp_right_fderivWithin _ (g.uniqueDiffOn_preimage_iff.2 hs x hx), ContinuousLinearMap.coe_comp', coe_coe, comp_apply, tail_def, tail_def] /-- The iterated derivative of the composition with a linear map on the right is obtained by composing the iterated derivative with the linear map. -/ theorem ContinuousLinearMap.iteratedFDeriv_comp_right (g : G →L[𝕜] E) {f : E → F} (hf : ContDiff 𝕜 n f) (x : G) {i : ℕ} (hi : i ≤ n) : iteratedFDeriv 𝕜 i (f ∘ g) x = (iteratedFDeriv 𝕜 i f (g x)).compContinuousLinearMap fun _ => g := by simp only [← iteratedFDerivWithin_univ] exact g.iteratedFDerivWithin_comp_right hf.contDiffOn uniqueDiffOn_univ uniqueDiffOn_univ (mem_univ _) hi /-- Composition with a linear isometry on the right preserves the norm of the iterated derivative within a set. -/ theorem LinearIsometryEquiv.norm_iteratedFDerivWithin_comp_right (g : G ≃ₗᵢ[𝕜] E) (f : E → F) (hs : UniqueDiffOn 𝕜 s) {x : G} (hx : g x ∈ s) (i : ℕ) : ‖iteratedFDerivWithin 𝕜 i (f ∘ g) (g ⁻¹' s) x‖ = ‖iteratedFDerivWithin 𝕜 i f s (g x)‖ := by have : iteratedFDerivWithin 𝕜 i (f ∘ g) (g ⁻¹' s) x = (iteratedFDerivWithin 𝕜 i f s (g x)).compContinuousLinearMap fun _ => g := g.toContinuousLinearEquiv.iteratedFDerivWithin_comp_right f hs hx i rw [this, ContinuousMultilinearMap.norm_compContinuous_linearIsometryEquiv] /-- Composition with a linear isometry on the right preserves the norm of the iterated derivative within a set. -/ theorem LinearIsometryEquiv.norm_iteratedFDeriv_comp_right (g : G ≃ₗᵢ[𝕜] E) (f : E → F) (x : G) (i : ℕ) : ‖iteratedFDeriv 𝕜 i (f ∘ g) x‖ = ‖iteratedFDeriv 𝕜 i f (g x)‖ := by simp only [← iteratedFDerivWithin_univ] apply g.norm_iteratedFDerivWithin_comp_right f uniqueDiffOn_univ (mem_univ (g x)) i /-- Composition by continuous linear equivs on the right respects higher differentiability at a point in a domain. -/ theorem ContinuousLinearEquiv.contDiffWithinAt_comp_iff (e : G ≃L[𝕜] E) : ContDiffWithinAt 𝕜 n (f ∘ e) (e ⁻¹' s) (e.symm x) ↔ ContDiffWithinAt 𝕜 n f s x := by constructor · intro H simpa [← preimage_comp, Function.comp_def] using H.comp_continuousLinearMap (e.symm : E →L[𝕜] G) · intro H rw [← e.apply_symm_apply x, ← e.coe_coe] at H exact H.comp_continuousLinearMap _ /-- Composition by continuous linear equivs on the right respects higher differentiability at a point. -/ theorem ContinuousLinearEquiv.contDiffAt_comp_iff (e : G ≃L[𝕜] E) : ContDiffAt 𝕜 n (f ∘ e) (e.symm x) ↔ ContDiffAt 𝕜 n f x := by rw [← contDiffWithinAt_univ, ← contDiffWithinAt_univ, ← preimage_univ] exact e.contDiffWithinAt_comp_iff /-- Composition by continuous linear equivs on the right respects higher differentiability on domains. -/ theorem ContinuousLinearEquiv.contDiffOn_comp_iff (e : G ≃L[𝕜] E) : ContDiffOn 𝕜 n (f ∘ e) (e ⁻¹' s) ↔ ContDiffOn 𝕜 n f s := ⟨fun H => by simpa [Function.comp_def] using H.comp_continuousLinearMap (e.symm : E →L[𝕜] G), fun H => H.comp_continuousLinearMap (e : G →L[𝕜] E)⟩ /-- Composition by continuous linear equivs on the right respects higher differentiability. -/ theorem ContinuousLinearEquiv.contDiff_comp_iff (e : G ≃L[𝕜] E) : ContDiff 𝕜 n (f ∘ e) ↔ ContDiff 𝕜 n f := by rw [← contDiffOn_univ, ← contDiffOn_univ, ← preimage_univ] exact e.contDiffOn_comp_iff end linear /-! ### The Cartesian product of two C^n functions is C^n. -/ section prod /-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/ theorem HasFTaylorSeriesUpToOn.prodMk {n : WithTop ℕ∞} (hf : HasFTaylorSeriesUpToOn n f p s) {g : E → G} {q : E → FormalMultilinearSeries 𝕜 E G} (hg : HasFTaylorSeriesUpToOn n g q s) : HasFTaylorSeriesUpToOn n (fun y => (f y, g y)) (fun y k => (p y k).prod (q y k)) s := by set L := fun m => ContinuousMultilinearMap.prodL 𝕜 (fun _ : Fin m => E) F G constructor · intro x hx; rw [← hf.zero_eq x hx, ← hg.zero_eq x hx]; rfl · intro m hm x hx convert (L m).hasFDerivAt.comp_hasFDerivWithinAt x ((hf.fderivWithin m hm x hx).prodMk (hg.fderivWithin m hm x hx)) · intro m hm exact (L m).continuous.comp_continuousOn ((hf.cont m hm).prodMk (hg.cont m hm)) @[deprecated (since := "2025-03-09")] alias HasFTaylorSeriesUpToOn.prod := HasFTaylorSeriesUpToOn.prodMk /-- The cartesian product of `C^n` functions at a point in a domain is `C^n`. -/ theorem ContDiffWithinAt.prodMk {s : Set E} {f : E → F} {g : E → G} (hf : ContDiffWithinAt 𝕜 n f s x) (hg : ContDiffWithinAt 𝕜 n g s x) : ContDiffWithinAt 𝕜 n (fun x : E => (f x, g x)) s x := by match n with | ω => obtain ⟨u, hu, p, hp, h'p⟩ := hf obtain ⟨v, hv, q, hq, h'q⟩ := hg refine ⟨u ∩ v, Filter.inter_mem hu hv, _, (hp.mono inter_subset_left).prodMk (hq.mono inter_subset_right), fun i ↦ ?_⟩ change AnalyticOn 𝕜 (fun x ↦ ContinuousMultilinearMap.prodL _ _ _ _ (p x i, q x i)) (u ∩ v) apply (LinearIsometryEquiv.analyticOnNhd _ _).comp_analyticOn _ (Set.mapsTo_univ _ _) exact ((h'p i).mono inter_subset_left).prod ((h'q i).mono inter_subset_right) | (n : ℕ∞) => intro m hm rcases hf m hm with ⟨u, hu, p, hp⟩ rcases hg m hm with ⟨v, hv, q, hq⟩ exact ⟨u ∩ v, Filter.inter_mem hu hv, _, (hp.mono inter_subset_left).prodMk (hq.mono inter_subset_right)⟩ @[deprecated (since := "2025-03-09")] alias ContDiffWithinAt.prod := ContDiffWithinAt.prodMk /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ theorem ContDiffOn.prodMk {s : Set E} {f : E → F} {g : E → G} (hf : ContDiffOn 𝕜 n f s) (hg : ContDiffOn 𝕜 n g s) : ContDiffOn 𝕜 n (fun x : E => (f x, g x)) s := fun x hx => (hf x hx).prodMk (hg x hx) @[deprecated (since := "2025-03-09")] alias ContDiffOn.prod := ContDiffOn.prodMk /-- The cartesian product of `C^n` functions at a point is `C^n`. -/ theorem ContDiffAt.prodMk {f : E → F} {g : E → G} (hf : ContDiffAt 𝕜 n f x) (hg : ContDiffAt 𝕜 n g x) : ContDiffAt 𝕜 n (fun x : E => (f x, g x)) x := contDiffWithinAt_univ.1 <| hf.contDiffWithinAt.prodMk hg.contDiffWithinAt @[deprecated (since := "2025-03-09")] alias ContDiffAt.prod := ContDiffAt.prodMk /-- The cartesian product of `C^n` functions is `C^n`. -/ theorem ContDiff.prodMk {f : E → F} {g : E → G} (hf : ContDiff 𝕜 n f) (hg : ContDiff 𝕜 n g) : ContDiff 𝕜 n fun x : E => (f x, g x) := contDiffOn_univ.1 <| hf.contDiffOn.prodMk hg.contDiffOn @[deprecated (since := "2025-03-09")] alias ContDiff.prod := ContDiff.prodMk end prod section comp /-! ### Composition of `C^n` functions We show that the composition of `C^n` functions is `C^n`. One way to do this would be to use the following simple inductive proof. Assume it is done for `n`. Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e., that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to `x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done. There are two difficulties in this proof. The first one is that it is an induction over all Banach spaces. In Lean, this is only possible if they belong to a fixed universe. One could formalize this by first proving the statement in this case, and then extending the result to general universes by embedding all the spaces we consider in a common universe through `ULift`. The second one is that it does not work cleanly for analytic maps: for this case, we need to exhibit a whole sequence of derivatives which are all analytic, not just finitely many of them, so an induction is never enough at a finite step. Both these difficulties can be overcome with some cost. However, we choose a different path: we write down an explicit formula for the `n`-th derivative of `g ∘ f` in terms of derivatives of `g` and `f` (this is the formula of Faa-Di Bruno) and use this formula to get a suitable Taylor expansion for `g ∘ f`. Writing down the formula of Faa-Di Bruno is not easy as the formula is quite intricate, but it is also useful for other purposes and once available it makes the proof here essentially trivial. -/ /-- The composition of `C^n` functions at points in domains is `C^n`. -/ theorem ContDiffWithinAt.comp {s : Set E} {t : Set F} {g : F → G} {f : E → F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) (st : MapsTo f s t) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := by match n with | ω => have h'f : ContDiffWithinAt 𝕜 ω f s x := hf obtain ⟨u, hu, p, hp, h'p⟩ := h'f obtain ⟨v, hv, q, hq, h'q⟩ := hg let w := insert x s ∩ (u ∩ f ⁻¹' v) have wv : w ⊆ f ⁻¹' v := fun y hy => hy.2.2 have wu : w ⊆ u := fun y hy => hy.2.1 refine ⟨w, ?_, fun y ↦ (q (f y)).taylorComp (p y), hq.comp (hp.mono wu) wv, ?_⟩ · apply inter_mem self_mem_nhdsWithin (inter_mem hu ?_) apply (continuousWithinAt_insert_self.2 hf.continuousWithinAt).preimage_mem_nhdsWithin' apply nhdsWithin_mono _ _ hv simp only [image_insert_eq] apply insert_subset_insert exact image_subset_iff.mpr st · have : AnalyticOn 𝕜 f w := by have : AnalyticOn 𝕜 (fun y ↦ (continuousMultilinearCurryFin0 𝕜 E F).symm (f y)) w := ((h'p 0).mono wu).congr fun y hy ↦ (hp.zero_eq' (wu hy)).symm have : AnalyticOn 𝕜 (fun y ↦ (continuousMultilinearCurryFin0 𝕜 E F) ((continuousMultilinearCurryFin0 𝕜 E F).symm (f y))) w := AnalyticOnNhd.comp_analyticOn (LinearIsometryEquiv.analyticOnNhd _ _ ) this (mapsTo_univ _ _) simpa using this exact analyticOn_taylorComp h'q (fun n ↦ (h'p n).mono wu) this wv | (n : ℕ∞) => intro m hm rcases hf m hm with ⟨u, hu, p, hp⟩ rcases hg m hm with ⟨v, hv, q, hq⟩ let w := insert x s ∩ (u ∩ f ⁻¹' v) have wv : w ⊆ f ⁻¹' v := fun y hy => hy.2.2 have wu : w ⊆ u := fun y hy => hy.2.1 refine ⟨w, ?_, fun y ↦ (q (f y)).taylorComp (p y), hq.comp (hp.mono wu) wv⟩ apply inter_mem self_mem_nhdsWithin (inter_mem hu ?_) apply (continuousWithinAt_insert_self.2 hf.continuousWithinAt).preimage_mem_nhdsWithin' apply nhdsWithin_mono _ _ hv simp only [image_insert_eq] apply insert_subset_insert exact image_subset_iff.mpr st /-- The composition of `C^n` functions on domains is `C^n`. -/ theorem ContDiffOn.comp {s : Set E} {t : Set F} {g : F → G} {f : E → F} (hg : ContDiffOn 𝕜 n g t) (hf : ContDiffOn 𝕜 n f s) (st : MapsTo f s t) : ContDiffOn 𝕜 n (g ∘ f) s := fun x hx ↦ ContDiffWithinAt.comp x (hg (f x) (st hx)) (hf x hx) st /-- The composition of `C^n` functions on domains is `C^n`. -/ theorem ContDiffOn.comp_inter {s : Set E} {t : Set F} {g : F → G} {f : E → F} (hg : ContDiffOn 𝕜 n g t) (hf : ContDiffOn 𝕜 n f s) : ContDiffOn 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) := hg.comp (hf.mono inter_subset_left) inter_subset_right @[deprecated (since := "2024-10-30")] alias ContDiffOn.comp' := ContDiffOn.comp_inter /-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/ theorem ContDiff.comp_contDiffOn {s : Set E} {g : F → G} {f : E → F} (hg : ContDiff 𝕜 n g) (hf : ContDiffOn 𝕜 n f s) : ContDiffOn 𝕜 n (g ∘ f) s := (contDiffOn_univ.2 hg).comp hf (mapsTo_univ _ _) theorem ContDiffOn.comp_contDiff {s : Set F} {g : F → G} {f : E → F} (hg : ContDiffOn 𝕜 n g s) (hf : ContDiff 𝕜 n f) (hs : ∀ x, f x ∈ s) : ContDiff 𝕜 n (g ∘ f) := by rw [← contDiffOn_univ] at * exact hg.comp hf fun x _ => hs x theorem ContDiffOn.image_comp_contDiff {s : Set E} {g : F → G} {f : E → F} (hg : ContDiffOn 𝕜 n g (f '' s)) (hf : ContDiff 𝕜 n f) : ContDiffOn 𝕜 n (g ∘ f) s := hg.comp hf.contDiffOn (s.mapsTo_image f) /-- The composition of `C^n` functions is `C^n`. -/ theorem ContDiff.comp {g : F → G} {f : E → F} (hg : ContDiff 𝕜 n g) (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n (g ∘ f) := contDiffOn_univ.1 <| ContDiffOn.comp (contDiffOn_univ.2 hg) (contDiffOn_univ.2 hf) (subset_univ _) /-- The composition of `C^n` functions at points in domains is `C^n`. -/ theorem ContDiffWithinAt.comp_of_eq {s : Set E} {t : Set F} {g : F → G} {f : E → F} {y : F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t y) (hf : ContDiffWithinAt 𝕜 n f s x) (st : MapsTo f s t) (hy : f x = y) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := by subst hy; exact hg.comp x hf st /-- The composition of `C^n` functions at points in domains is `C^n`, with a weaker condition on `s` and `t`. -/ theorem ContDiffWithinAt.comp_of_mem_nhdsWithin_image {s : Set E} {t : Set F} {g : F → G} {f : E → F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) (hs : t ∈ 𝓝[f '' s] f x) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := (hg.mono_of_mem_nhdsWithin hs).comp x hf (subset_preimage_image f s) /-- The composition of `C^n` functions at points in domains is `C^n`, with a weaker condition on `s` and `t`. -/ theorem ContDiffWithinAt.comp_of_mem_nhdsWithin_image_of_eq {s : Set E} {t : Set F} {g : F → G} {f : E → F} {y : F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t y) (hf : ContDiffWithinAt 𝕜 n f s x) (hs : t ∈ 𝓝[f '' s] f x) (hy : f x = y) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := by subst hy; exact hg.comp_of_mem_nhdsWithin_image x hf hs /-- The composition of `C^n` functions at points in domains is `C^n`. -/ theorem ContDiffWithinAt.comp_inter {s : Set E} {t : Set F} {g : F → G} {f : E → F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) : ContDiffWithinAt 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) x := hg.comp x (hf.mono inter_subset_left) inter_subset_right /-- The composition of `C^n` functions at points in domains is `C^n`. -/ theorem ContDiffWithinAt.comp_inter_of_eq {s : Set E} {t : Set F} {g : F → G} {f : E → F} {y : F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t y) (hf : ContDiffWithinAt 𝕜 n f s x) (hy : f x = y) : ContDiffWithinAt 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) x := by subst hy; exact hg.comp_inter x hf /-- The composition of `C^n` functions at points in domains is `C^n`, with a weaker condition on `s` and `t`. -/ theorem ContDiffWithinAt.comp_of_preimage_mem_nhdsWithin {s : Set E} {t : Set F} {g : F → G} {f : E → F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) (hs : f ⁻¹' t ∈ 𝓝[s] x) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := (hg.comp_inter x hf).mono_of_mem_nhdsWithin (inter_mem self_mem_nhdsWithin hs) /-- The composition of `C^n` functions at points in domains is `C^n`, with a weaker condition on `s` and `t`. -/ theorem ContDiffWithinAt.comp_of_preimage_mem_nhdsWithin_of_eq {s : Set E} {t : Set F} {g : F → G} {f : E → F} {y : F} (x : E) (hg : ContDiffWithinAt 𝕜 n g t y) (hf : ContDiffWithinAt 𝕜 n f s x) (hs : f ⁻¹' t ∈ 𝓝[s] x) (hy : f x = y) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := by subst hy; exact hg.comp_of_preimage_mem_nhdsWithin x hf hs theorem ContDiffAt.comp_contDiffWithinAt (x : E) (hg : ContDiffAt 𝕜 n g (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := hg.comp x hf (mapsTo_univ _ _) theorem ContDiffAt.comp_contDiffWithinAt_of_eq {y : F} (x : E) (hg : ContDiffAt 𝕜 n g y) (hf : ContDiffWithinAt 𝕜 n f s x) (hy : f x = y) : ContDiffWithinAt 𝕜 n (g ∘ f) s x := by subst hy; exact hg.comp_contDiffWithinAt x hf /-- The composition of `C^n` functions at points is `C^n`. -/ nonrec theorem ContDiffAt.comp (x : E) (hg : ContDiffAt 𝕜 n g (f x)) (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (g ∘ f) x := hg.comp x hf (mapsTo_univ _ _) theorem ContDiff.comp_contDiffWithinAt {g : F → G} {f : E → F} (h : ContDiff 𝕜 n g) (hf : ContDiffWithinAt 𝕜 n f t x) : ContDiffWithinAt 𝕜 n (g ∘ f) t x := haveI : ContDiffWithinAt 𝕜 n g univ (f x) := h.contDiffAt.contDiffWithinAt this.comp x hf (subset_univ _) theorem ContDiff.comp_contDiffAt {g : F → G} {f : E → F} (x : E) (hg : ContDiff 𝕜 n g) (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (g ∘ f) x := hg.comp_contDiffWithinAt hf theorem iteratedFDerivWithin_comp_of_eventually_mem {t : Set F} (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) (ht : UniqueDiffOn 𝕜 t) (hs : UniqueDiffOn 𝕜 s) (hxs : x ∈ s) (hst : ∀ᶠ y in 𝓝[s] x, f y ∈ t) {i : ℕ} (hi : i ≤ n) : iteratedFDerivWithin 𝕜 i (g ∘ f) s x = (ftaylorSeriesWithin 𝕜 g t (f x)).taylorComp (ftaylorSeriesWithin 𝕜 f s x) i := by obtain ⟨u, hxu, huo, hfu, hgu⟩ : ∃ u, x ∈ u ∧ IsOpen u ∧ HasFTaylorSeriesUpToOn i f (ftaylorSeriesWithin 𝕜 f s) (s ∩ u) ∧ HasFTaylorSeriesUpToOn i g (ftaylorSeriesWithin 𝕜 g t) (f '' (s ∩ u)) := by have hxt : f x ∈ t := hst.self_of_nhdsWithin hxs have hf_tendsto : Tendsto f (𝓝[s] x) (𝓝[t] (f x)) := tendsto_nhdsWithin_iff.mpr ⟨hf.continuousWithinAt, hst⟩ have H₁ : ∀ᶠ u in (𝓝[s] x).smallSets, HasFTaylorSeriesUpToOn i f (ftaylorSeriesWithin 𝕜 f s) u := hf.eventually_hasFTaylorSeriesUpToOn hs hxs hi have H₂ : ∀ᶠ u in (𝓝[s] x).smallSets, HasFTaylorSeriesUpToOn i g (ftaylorSeriesWithin 𝕜 g t) (f '' u) := hf_tendsto.image_smallSets.eventually (hg.eventually_hasFTaylorSeriesUpToOn ht hxt hi) rcases (nhdsWithin_basis_open _ _).smallSets.eventually_iff.mp (H₁.and H₂) with ⟨u, ⟨hxu, huo⟩, hu⟩ exact ⟨u, hxu, huo, hu (by simp [inter_comm])⟩ exact .symm <| (hgu.comp hfu (mapsTo_image _ _)).eq_iteratedFDerivWithin_of_uniqueDiffOn le_rfl (hs.inter huo) ⟨hxs, hxu⟩ |>.trans <| iteratedFDerivWithin_inter_open huo hxu theorem iteratedFDerivWithin_comp {t : Set F} (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContDiffWithinAt 𝕜 n f s x) (ht : UniqueDiffOn 𝕜 t) (hs : UniqueDiffOn 𝕜 s) (hx : x ∈ s) (hst : MapsTo f s t) {i : ℕ} (hi : i ≤ n) : iteratedFDerivWithin 𝕜 i (g ∘ f) s x = (ftaylorSeriesWithin 𝕜 g t (f x)).taylorComp (ftaylorSeriesWithin 𝕜 f s x) i := iteratedFDerivWithin_comp_of_eventually_mem hg hf ht hs hx (eventually_mem_nhdsWithin.mono hst) hi theorem iteratedFDeriv_comp (hg : ContDiffAt 𝕜 n g (f x)) (hf : ContDiffAt 𝕜 n f x) {i : ℕ} (hi : i ≤ n) : iteratedFDeriv 𝕜 i (g ∘ f) x = (ftaylorSeries 𝕜 g (f x)).taylorComp (ftaylorSeries 𝕜 f x) i := by simp only [← iteratedFDerivWithin_univ, ← ftaylorSeriesWithin_univ] exact iteratedFDerivWithin_comp hg.contDiffWithinAt hf.contDiffWithinAt uniqueDiffOn_univ uniqueDiffOn_univ (mem_univ _) (mapsTo_univ _ _) hi end comp /-! ### Smoothness of projections -/ /-- The first projection in a product is `C^∞`. -/ theorem contDiff_fst : ContDiff 𝕜 n (Prod.fst : E × F → E) := IsBoundedLinearMap.contDiff IsBoundedLinearMap.fst /-- Postcomposing `f` with `Prod.fst` is `C^n` -/ theorem ContDiff.fst {f : E → F × G} (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n fun x => (f x).1 := contDiff_fst.comp hf /-- Precomposing `f` with `Prod.fst` is `C^n` -/ theorem ContDiff.fst' {f : E → G} (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n fun x : E × F => f x.1 := hf.comp contDiff_fst /-- The first projection on a domain in a product is `C^∞`. -/ theorem contDiffOn_fst {s : Set (E × F)} : ContDiffOn 𝕜 n (Prod.fst : E × F → E) s := ContDiff.contDiffOn contDiff_fst theorem ContDiffOn.fst {f : E → F × G} {s : Set E} (hf : ContDiffOn 𝕜 n f s) : ContDiffOn 𝕜 n (fun x => (f x).1) s := contDiff_fst.comp_contDiffOn hf /-- The first projection at a point in a product is `C^∞`. -/ theorem contDiffAt_fst {p : E × F} : ContDiffAt 𝕜 n (Prod.fst : E × F → E) p := contDiff_fst.contDiffAt /-- Postcomposing `f` with `Prod.fst` is `C^n` at `(x, y)` -/ theorem ContDiffAt.fst {f : E → F × G} {x : E} (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (fun x => (f x).1) x := contDiffAt_fst.comp x hf /-- Precomposing `f` with `Prod.fst` is `C^n` at `(x, y)` -/ theorem ContDiffAt.fst' {f : E → G} {x : E} {y : F} (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (fun x : E × F => f x.1) (x, y) := ContDiffAt.comp (x, y) hf contDiffAt_fst /-- Precomposing `f` with `Prod.fst` is `C^n` at `x : E × F` -/ theorem ContDiffAt.fst'' {f : E → G} {x : E × F} (hf : ContDiffAt 𝕜 n f x.1) : ContDiffAt 𝕜 n (fun x : E × F => f x.1) x := hf.comp x contDiffAt_fst /-- The first projection within a domain at a point in a product is `C^∞`. -/ theorem contDiffWithinAt_fst {s : Set (E × F)} {p : E × F} : ContDiffWithinAt 𝕜 n (Prod.fst : E × F → E) s p := contDiff_fst.contDiffWithinAt /-- The second projection in a product is `C^∞`. -/ theorem contDiff_snd : ContDiff 𝕜 n (Prod.snd : E × F → F) := IsBoundedLinearMap.contDiff IsBoundedLinearMap.snd /-- Postcomposing `f` with `Prod.snd` is `C^n` -/ theorem ContDiff.snd {f : E → F × G} (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n fun x => (f x).2 := contDiff_snd.comp hf /-- Precomposing `f` with `Prod.snd` is `C^n` -/ theorem ContDiff.snd' {f : F → G} (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n fun x : E × F => f x.2 := hf.comp contDiff_snd /-- The second projection on a domain in a product is `C^∞`. -/ theorem contDiffOn_snd {s : Set (E × F)} : ContDiffOn 𝕜 n (Prod.snd : E × F → F) s := ContDiff.contDiffOn contDiff_snd theorem ContDiffOn.snd {f : E → F × G} {s : Set E} (hf : ContDiffOn 𝕜 n f s) : ContDiffOn 𝕜 n (fun x => (f x).2) s := contDiff_snd.comp_contDiffOn hf /-- The second projection at a point in a product is `C^∞`. -/ theorem contDiffAt_snd {p : E × F} : ContDiffAt 𝕜 n (Prod.snd : E × F → F) p := contDiff_snd.contDiffAt /-- Postcomposing `f` with `Prod.snd` is `C^n` at `x` -/ theorem ContDiffAt.snd {f : E → F × G} {x : E} (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (fun x => (f x).2) x := contDiffAt_snd.comp x hf /-- Precomposing `f` with `Prod.snd` is `C^n` at `(x, y)` -/ theorem ContDiffAt.snd' {f : F → G} {x : E} {y : F} (hf : ContDiffAt 𝕜 n f y) : ContDiffAt 𝕜 n (fun x : E × F => f x.2) (x, y) := ContDiffAt.comp (x, y) hf contDiffAt_snd /-- Precomposing `f` with `Prod.snd` is `C^n` at `x : E × F` -/ theorem ContDiffAt.snd'' {f : F → G} {x : E × F} (hf : ContDiffAt 𝕜 n f x.2) : ContDiffAt 𝕜 n (fun x : E × F => f x.2) x := hf.comp x contDiffAt_snd /-- The second projection within a domain at a point in a product is `C^∞`. -/ theorem contDiffWithinAt_snd {s : Set (E × F)} {p : E × F} : ContDiffWithinAt 𝕜 n (Prod.snd : E × F → F) s p := contDiff_snd.contDiffWithinAt section NAry variable {E₁ E₂ E₃ : Type*} variable [NormedAddCommGroup E₁] [NormedAddCommGroup E₂] [NormedAddCommGroup E₃] [NormedSpace 𝕜 E₁] [NormedSpace 𝕜 E₂] [NormedSpace 𝕜 E₃] theorem ContDiff.comp₂ {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} (hg : ContDiff 𝕜 n g) (hf₁ : ContDiff 𝕜 n f₁) (hf₂ : ContDiff 𝕜 n f₂) : ContDiff 𝕜 n fun x => g (f₁ x, f₂ x) := hg.comp <| hf₁.prodMk hf₂ theorem ContDiffAt.comp₂ {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {x : F} (hg : ContDiffAt 𝕜 n g (f₁ x, f₂ x)) (hf₁ : ContDiffAt 𝕜 n f₁ x) (hf₂ : ContDiffAt 𝕜 n f₂ x) : ContDiffAt 𝕜 n (fun x => g (f₁ x, f₂ x)) x := hg.comp x (hf₁.prodMk hf₂) theorem ContDiffAt.comp₂_contDiffWithinAt {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {s : Set F} {x : F} (hg : ContDiffAt 𝕜 n g (f₁ x, f₂ x)) (hf₁ : ContDiffWithinAt 𝕜 n f₁ s x) (hf₂ : ContDiffWithinAt 𝕜 n f₂ s x) : ContDiffWithinAt 𝕜 n (fun x => g (f₁ x, f₂ x)) s x := hg.comp_contDiffWithinAt x (hf₁.prodMk hf₂) @[deprecated (since := "2024-10-30")] alias ContDiffAt.comp_contDiffWithinAt₂ := ContDiffAt.comp₂_contDiffWithinAt theorem ContDiff.comp₂_contDiffAt {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {x : F} (hg : ContDiff 𝕜 n g) (hf₁ : ContDiffAt 𝕜 n f₁ x) (hf₂ : ContDiffAt 𝕜 n f₂ x) : ContDiffAt 𝕜 n (fun x => g (f₁ x, f₂ x)) x := hg.contDiffAt.comp₂ hf₁ hf₂ @[deprecated (since := "2024-10-30")] alias ContDiff.comp_contDiffAt₂ := ContDiff.comp₂_contDiffAt theorem ContDiff.comp₂_contDiffWithinAt {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {s : Set F} {x : F} (hg : ContDiff 𝕜 n g) (hf₁ : ContDiffWithinAt 𝕜 n f₁ s x) (hf₂ : ContDiffWithinAt 𝕜 n f₂ s x) : ContDiffWithinAt 𝕜 n (fun x => g (f₁ x, f₂ x)) s x := hg.contDiffAt.comp_contDiffWithinAt x (hf₁.prodMk hf₂) @[deprecated (since := "2024-10-30")] alias ContDiff.comp_contDiffWithinAt₂ := ContDiff.comp₂_contDiffWithinAt theorem ContDiff.comp₂_contDiffOn {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {s : Set F} (hg : ContDiff 𝕜 n g) (hf₁ : ContDiffOn 𝕜 n f₁ s) (hf₂ : ContDiffOn 𝕜 n f₂ s) : ContDiffOn 𝕜 n (fun x => g (f₁ x, f₂ x)) s := hg.comp_contDiffOn <| hf₁.prodMk hf₂ @[deprecated (since := "2024-10-30")] alias ContDiff.comp_contDiffOn₂ := ContDiff.comp₂_contDiffOn theorem ContDiff.comp₃ {g : E₁ × E₂ × E₃ → G} {f₁ : F → E₁} {f₂ : F → E₂} {f₃ : F → E₃} (hg : ContDiff 𝕜 n g) (hf₁ : ContDiff 𝕜 n f₁) (hf₂ : ContDiff 𝕜 n f₂) (hf₃ : ContDiff 𝕜 n f₃) : ContDiff 𝕜 n fun x => g (f₁ x, f₂ x, f₃ x) := hg.comp₂ hf₁ <| hf₂.prodMk hf₃ theorem ContDiff.comp₃_contDiffOn {g : E₁ × E₂ × E₃ → G} {f₁ : F → E₁} {f₂ : F → E₂} {f₃ : F → E₃} {s : Set F} (hg : ContDiff 𝕜 n g) (hf₁ : ContDiffOn 𝕜 n f₁ s) (hf₂ : ContDiffOn 𝕜 n f₂ s) (hf₃ : ContDiffOn 𝕜 n f₃ s) : ContDiffOn 𝕜 n (fun x => g (f₁ x, f₂ x, f₃ x)) s := hg.comp₂_contDiffOn hf₁ <| hf₂.prodMk hf₃ @[deprecated (since := "2024-10-30")] alias ContDiff.comp_contDiffOn₃ := ContDiff.comp₃_contDiffOn end NAry section SpecificBilinearMaps theorem ContDiff.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} (hg : ContDiff 𝕜 n g) (hf : ContDiff 𝕜 n f) : ContDiff 𝕜 n fun x => (g x).comp (f x) := isBoundedBilinearMap_comp.contDiff.comp₂ (g := fun p => p.1.comp p.2) hg hf theorem ContDiffOn.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} {s : Set X} (hg : ContDiffOn 𝕜 n g s) (hf : ContDiffOn 𝕜 n f s) : ContDiffOn 𝕜 n (fun x => (g x).comp (f x)) s := (isBoundedBilinearMap_comp (E := E) (F := F) (G := G)).contDiff.comp₂_contDiffOn hg hf theorem ContDiffAt.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} {x : X} (hg : ContDiffAt 𝕜 n g x) (hf : ContDiffAt 𝕜 n f x) : ContDiffAt 𝕜 n (fun x => (g x).comp (f x)) x := (isBoundedBilinearMap_comp (E := E) (G := G)).contDiff.comp₂_contDiffAt hg hf theorem ContDiffWithinAt.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} {s : Set X} {x : X} (hg : ContDiffWithinAt 𝕜 n g s x) (hf : ContDiffWithinAt 𝕜 n f s x) : ContDiffWithinAt 𝕜 n (fun x => (g x).comp (f x)) s x := (isBoundedBilinearMap_comp (E := E) (G := G)).contDiff.comp₂_contDiffWithinAt hg hf theorem ContDiff.clm_apply {f : E → F →L[𝕜] G} {g : E → F} (hf : ContDiff 𝕜 n f) (hg : ContDiff 𝕜 n g) : ContDiff 𝕜 n fun x => (f x) (g x) := isBoundedBilinearMap_apply.contDiff.comp₂ hf hg theorem ContDiffOn.clm_apply {f : E → F →L[𝕜] G} {g : E → F} (hf : ContDiffOn 𝕜 n f s) (hg : ContDiffOn 𝕜 n g s) : ContDiffOn 𝕜 n (fun x => (f x) (g x)) s := isBoundedBilinearMap_apply.contDiff.comp₂_contDiffOn hf hg theorem ContDiffAt.clm_apply {f : E → F →L[𝕜] G} {g : E → F} (hf : ContDiffAt 𝕜 n f x) (hg : ContDiffAt 𝕜 n g x) : ContDiffAt 𝕜 n (fun x => (f x) (g x)) x := isBoundedBilinearMap_apply.contDiff.comp₂_contDiffAt hf hg theorem ContDiffWithinAt.clm_apply {f : E → F →L[𝕜] G} {g : E → F} (hf : ContDiffWithinAt 𝕜 n f s x) (hg : ContDiffWithinAt 𝕜 n g s x) : ContDiffWithinAt 𝕜 n (fun x => (f x) (g x)) s x := isBoundedBilinearMap_apply.contDiff.comp₂_contDiffWithinAt hf hg theorem ContDiff.smulRight {f : E → F →L[𝕜] 𝕜} {g : E → G} (hf : ContDiff 𝕜 n f) (hg : ContDiff 𝕜 n g) : ContDiff 𝕜 n fun x => (f x).smulRight (g x) := isBoundedBilinearMap_smulRight.contDiff.comp₂ (g := fun p => p.1.smulRight p.2) hf hg theorem ContDiffOn.smulRight {f : E → F →L[𝕜] 𝕜} {g : E → G} (hf : ContDiffOn 𝕜 n f s) (hg : ContDiffOn 𝕜 n g s) : ContDiffOn 𝕜 n (fun x => (f x).smulRight (g x)) s := (isBoundedBilinearMap_smulRight (E := F)).contDiff.comp₂_contDiffOn hf hg theorem ContDiffAt.smulRight {f : E → F →L[𝕜] 𝕜} {g : E → G} (hf : ContDiffAt 𝕜 n f x) (hg : ContDiffAt 𝕜 n g x) : ContDiffAt 𝕜 n (fun x => (f x).smulRight (g x)) x := (isBoundedBilinearMap_smulRight (E := F)).contDiff.comp₂_contDiffAt hf hg theorem ContDiffWithinAt.smulRight {f : E → F →L[𝕜] 𝕜} {g : E → G} (hf : ContDiffWithinAt 𝕜 n f s x) (hg : ContDiffWithinAt 𝕜 n g s x) : ContDiffWithinAt 𝕜 n (fun x => (f x).smulRight (g x)) s x := (isBoundedBilinearMap_smulRight (E := F)).contDiff.comp₂_contDiffWithinAt hf hg end SpecificBilinearMaps section ClmApplyConst /-- Application of a `ContinuousLinearMap` to a constant commutes with `iteratedFDerivWithin`. -/ theorem iteratedFDerivWithin_clm_apply_const_apply {s : Set E} (hs : UniqueDiffOn 𝕜 s) {c : E → F →L[𝕜] G} (hc : ContDiffOn 𝕜 n c s) {i : ℕ} (hi : i ≤ n) {x : E} (hx : x ∈ s) {u : F} {m : Fin i → E} : (iteratedFDerivWithin 𝕜 i (fun y ↦ (c y) u) s x) m = (iteratedFDerivWithin 𝕜 i c s x) m u := by induction i generalizing x with | zero => simp | succ i ih => replace hi : (i : WithTop ℕ∞) < n := lt_of_lt_of_le (by norm_cast; simp) hi have h_deriv_apply : DifferentiableOn 𝕜 (iteratedFDerivWithin 𝕜 i (fun y ↦ (c y) u) s) s := (hc.clm_apply contDiffOn_const).differentiableOn_iteratedFDerivWithin hi hs have h_deriv : DifferentiableOn 𝕜 (iteratedFDerivWithin 𝕜 i c s) s := hc.differentiableOn_iteratedFDerivWithin hi hs simp only [iteratedFDerivWithin_succ_apply_left] rw [← fderivWithin_continuousMultilinear_apply_const_apply (hs x hx) (h_deriv_apply x hx)] rw [fderivWithin_congr' (fun x hx ↦ ih hi.le hx) hx] rw [fderivWithin_clm_apply (hs x hx) (h_deriv.continuousMultilinear_apply_const _ x hx) (differentiableWithinAt_const u)] rw [fderivWithin_const_apply] simp only [ContinuousLinearMap.flip_apply, ContinuousLinearMap.comp_zero, zero_add] rw [fderivWithin_continuousMultilinear_apply_const_apply (hs x hx) (h_deriv x hx)] /-- Application of a `ContinuousLinearMap` to a constant commutes with `iteratedFDeriv`. -/ theorem iteratedFDeriv_clm_apply_const_apply {c : E → F →L[𝕜] G} (hc : ContDiff 𝕜 n c) {i : ℕ} (hi : i ≤ n) {x : E} {u : F} {m : Fin i → E} : (iteratedFDeriv 𝕜 i (fun y ↦ (c y) u) x) m = (iteratedFDeriv 𝕜 i c x) m u := by simp only [← iteratedFDerivWithin_univ] exact iteratedFDerivWithin_clm_apply_const_apply uniqueDiffOn_univ hc.contDiffOn hi (mem_univ _) end ClmApplyConst /-- The natural equivalence `(E × F) × G ≃ E × (F × G)` is smooth. Warning: if you think you need this lemma, it is likely that you can simplify your proof by reformulating the lemma that you're applying next using the tips in Note [continuity lemma statement] -/ theorem contDiff_prodAssoc {n : WithTop ℕ∞} : ContDiff 𝕜 n <| Equiv.prodAssoc E F G := (LinearIsometryEquiv.prodAssoc 𝕜 E F G).contDiff /-- The natural equivalence `E × (F × G) ≃ (E × F) × G` is smooth. Warning: see remarks attached to `contDiff_prodAssoc` -/ theorem contDiff_prodAssoc_symm {n : WithTop ℕ∞} : ContDiff 𝕜 n <| (Equiv.prodAssoc E F G).symm := (LinearIsometryEquiv.prodAssoc 𝕜 E F G).symm.contDiff /-! ### Bundled derivatives are smooth -/ section bundled /-- One direction of `contDiffWithinAt_succ_iff_hasFDerivWithinAt`, but where all derivatives are taken within the same set. Version for partial derivatives / functions with parameters. If `f x` is a `C^n+1` family of functions and `g x` is a `C^n` family of points, then the derivative of `f x` at `g x` depends in a `C^n` way on `x`. We give a general version of this fact relative to sets which may not have unique derivatives, in the following form. If `f : E × F → G` is `C^n+1` at `(x₀, g(x₀))` in `(s ∪ {x₀}) × t ⊆ E × F` and `g : E → F` is `C^n` at `x₀` within some set `s ⊆ E`, then there is a function `f' : E → F →L[𝕜] G` that is `C^n` at `x₀` within `s` such that for all `x` sufficiently close to `x₀` within `s ∪ {x₀}` the function `y ↦ f x y` has derivative `f' x` at `g x` within `t ⊆ F`. For convenience, we return an explicit set of `x`'s where this holds that is a subset of `s ∪ {x₀}`. We need one additional condition, namely that `t` is a neighborhood of `g(x₀)` within `g '' s`. -/ theorem ContDiffWithinAt.hasFDerivWithinAt_nhds {f : E → F → G} {g : E → F} {t : Set F} (hn : n ≠ ∞) {x₀ : E} (hf : ContDiffWithinAt 𝕜 (n + 1) (uncurry f) (insert x₀ s ×ˢ t) (x₀, g x₀)) (hg : ContDiffWithinAt 𝕜 n g s x₀) (hgt : t ∈ 𝓝[g '' s] g x₀) : ∃ v ∈ 𝓝[insert x₀ s] x₀, v ⊆ insert x₀ s ∧ ∃ f' : E → F →L[𝕜] G, (∀ x ∈ v, HasFDerivWithinAt (f x) (f' x) t (g x)) ∧ ContDiffWithinAt 𝕜 n (fun x => f' x) s x₀ := by have hst : insert x₀ s ×ˢ t ∈ 𝓝[(fun x => (x, g x)) '' s] (x₀, g x₀) := by refine nhdsWithin_mono _ ?_ (nhdsWithin_prod self_mem_nhdsWithin hgt) simp_rw [image_subset_iff, mk_preimage_prod, preimage_id', subset_inter_iff, subset_insert, true_and, subset_preimage_image] obtain ⟨v, hv, hvs, f_an, f', hvf', hf'⟩ := (contDiffWithinAt_succ_iff_hasFDerivWithinAt' hn).mp hf refine ⟨(fun z => (z, g z)) ⁻¹' v ∩ insert x₀ s, ?_, inter_subset_right, fun z => (f' (z, g z)).comp (ContinuousLinearMap.inr 𝕜 E F), ?_, ?_⟩ · refine inter_mem ?_ self_mem_nhdsWithin have := mem_of_mem_nhdsWithin (mem_insert _ _) hv refine mem_nhdsWithin_insert.mpr ⟨this, ?_⟩ refine (continuousWithinAt_id.prodMk hg.continuousWithinAt).preimage_mem_nhdsWithin' ?_ rw [← nhdsWithin_le_iff] at hst hv ⊢ exact (hst.trans <| nhdsWithin_mono _ <| subset_insert _ _).trans hv · intro z hz have := hvf' (z, g z) hz.1 refine this.comp _ (hasFDerivAt_prodMk_right _ _).hasFDerivWithinAt ?_ exact mapsTo'.mpr (image_prodMk_subset_prod_right hz.2) · exact (hf'.continuousLinearMap_comp <| (ContinuousLinearMap.compL 𝕜 F (E × F) G).flip (ContinuousLinearMap.inr 𝕜 E F)).comp_of_mem_nhdsWithin_image x₀ (contDiffWithinAt_id.prodMk hg) hst /-- The most general lemma stating that `x ↦ fderivWithin 𝕜 (f x) t (g x)` is `C^n` at a point within a set. To show that `x ↦ D_yf(x,y)g(x)` (taken within `t`) is `C^m` at `x₀` within `s`, we require that * `f` is `C^n` at `(x₀, g(x₀))` within `(s ∪ {x₀}) × t` for `n ≥ m+1`. * `g` is `C^m` at `x₀` within `s`; * Derivatives are unique at `g(x)` within `t` for `x` sufficiently close to `x₀` within `s ∪ {x₀}`; * `t` is a neighborhood of `g(x₀)` within `g '' s`; -/ theorem ContDiffWithinAt.fderivWithin'' {f : E → F → G} {g : E → F} {t : Set F} (hf : ContDiffWithinAt 𝕜 n (Function.uncurry f) (insert x₀ s ×ˢ t) (x₀, g x₀)) (hg : ContDiffWithinAt 𝕜 m g s x₀) (ht : ∀ᶠ x in 𝓝[insert x₀ s] x₀, UniqueDiffWithinAt 𝕜 t (g x)) (hmn : m + 1 ≤ n) (hgt : t ∈ 𝓝[g '' s] g x₀) : ContDiffWithinAt 𝕜 m (fun x => fderivWithin 𝕜 (f x) t (g x)) s x₀ := by have : ∀ k : ℕ, k ≤ m → ContDiffWithinAt 𝕜 k (fun x => fderivWithin 𝕜 (f x) t (g x)) s x₀ := by intro k hkm obtain ⟨v, hv, -, f', hvf', hf'⟩ := (hf.of_le <| (add_le_add_right hkm 1).trans hmn).hasFDerivWithinAt_nhds (by simp) (hg.of_le hkm) hgt refine hf'.congr_of_eventuallyEq_insert ?_ filter_upwards [hv, ht] exact fun y hy h2y => (hvf' y hy).fderivWithin h2y match m with | ω => obtain rfl : n = ω := by simpa using hmn obtain ⟨v, hv, -, f', hvf', hf'⟩ := hf.hasFDerivWithinAt_nhds (by simp) hg hgt refine hf'.congr_of_eventuallyEq_insert ?_ filter_upwards [hv, ht] exact fun y hy h2y => (hvf' y hy).fderivWithin h2y | ∞ => rw [contDiffWithinAt_infty] exact fun k ↦ this k (by exact_mod_cast le_top) | (m : ℕ) => exact this _ le_rfl /-- A special case of `ContDiffWithinAt.fderivWithin''` where we require that `s ⊆ g⁻¹(t)`. -/ theorem ContDiffWithinAt.fderivWithin' {f : E → F → G} {g : E → F} {t : Set F} (hf : ContDiffWithinAt 𝕜 n (Function.uncurry f) (insert x₀ s ×ˢ t) (x₀, g x₀)) (hg : ContDiffWithinAt 𝕜 m g s x₀) (ht : ∀ᶠ x in 𝓝[insert x₀ s] x₀, UniqueDiffWithinAt 𝕜 t (g x)) (hmn : m + 1 ≤ n) (hst : s ⊆ g ⁻¹' t) : ContDiffWithinAt 𝕜 m (fun x => fderivWithin 𝕜 (f x) t (g x)) s x₀ := hf.fderivWithin'' hg ht hmn <| mem_of_superset self_mem_nhdsWithin <| image_subset_iff.mpr hst /-- A special case of `ContDiffWithinAt.fderivWithin'` where we require that `x₀ ∈ s` and there are unique derivatives everywhere within `t`. -/ protected theorem ContDiffWithinAt.fderivWithin {f : E → F → G} {g : E → F} {t : Set F} (hf : ContDiffWithinAt 𝕜 n (Function.uncurry f) (s ×ˢ t) (x₀, g x₀)) (hg : ContDiffWithinAt 𝕜 m g s x₀) (ht : UniqueDiffOn 𝕜 t) (hmn : m + 1 ≤ n) (hx₀ : x₀ ∈ s) (hst : s ⊆ g ⁻¹' t) : ContDiffWithinAt 𝕜 m (fun x => fderivWithin 𝕜 (f x) t (g x)) s x₀ := by rw [← insert_eq_self.mpr hx₀] at hf refine hf.fderivWithin' hg ?_ hmn hst rw [insert_eq_self.mpr hx₀] exact eventually_of_mem self_mem_nhdsWithin fun x hx => ht _ (hst hx) /-- `x ↦ fderivWithin 𝕜 (f x) t (g x) (k x)` is smooth at a point within a set. -/ theorem ContDiffWithinAt.fderivWithin_apply {f : E → F → G} {g k : E → F} {t : Set F} (hf : ContDiffWithinAt 𝕜 n (Function.uncurry f) (s ×ˢ t) (x₀, g x₀)) (hg : ContDiffWithinAt 𝕜 m g s x₀) (hk : ContDiffWithinAt 𝕜 m k s x₀) (ht : UniqueDiffOn 𝕜 t) (hmn : m + 1 ≤ n) (hx₀ : x₀ ∈ s) (hst : s ⊆ g ⁻¹' t) : ContDiffWithinAt 𝕜 m (fun x => fderivWithin 𝕜 (f x) t (g x) (k x)) s x₀ := (contDiff_fst.clm_apply contDiff_snd).contDiffAt.comp_contDiffWithinAt x₀ ((hf.fderivWithin hg ht hmn hx₀ hst).prodMk hk) /-- `fderivWithin 𝕜 f s` is smooth at `x₀` within `s`. -/ theorem ContDiffWithinAt.fderivWithin_right (hf : ContDiffWithinAt 𝕜 n f s x₀) (hs : UniqueDiffOn 𝕜 s) (hmn : m + 1 ≤ n) (hx₀s : x₀ ∈ s) : ContDiffWithinAt 𝕜 m (fderivWithin 𝕜 f s) s x₀ := ContDiffWithinAt.fderivWithin (ContDiffWithinAt.comp (x₀, x₀) hf contDiffWithinAt_snd <| prod_subset_preimage_snd s s) contDiffWithinAt_id hs hmn hx₀s (by rw [preimage_id']) /-- `x ↦ fderivWithin 𝕜 f s x (k x)` is smooth at `x₀` within `s`. -/ theorem ContDiffWithinAt.fderivWithin_right_apply {f : F → G} {k : F → F} {s : Set F} {x₀ : F} (hf : ContDiffWithinAt 𝕜 n f s x₀) (hk : ContDiffWithinAt 𝕜 m k s x₀) (hs : UniqueDiffOn 𝕜 s) (hmn : m + 1 ≤ n) (hx₀s : x₀ ∈ s) : ContDiffWithinAt 𝕜 m (fun x => fderivWithin 𝕜 f s x (k x)) s x₀ := ContDiffWithinAt.fderivWithin_apply (ContDiffWithinAt.comp (x₀, x₀) hf contDiffWithinAt_snd <| prod_subset_preimage_snd s s) contDiffWithinAt_id hk hs hmn hx₀s (by rw [preimage_id']) -- TODO: can we make a version of `ContDiffWithinAt.fderivWithin` for iterated derivatives? theorem ContDiffWithinAt.iteratedFDerivWithin_right {i : ℕ} (hf : ContDiffWithinAt 𝕜 n f s x₀) (hs : UniqueDiffOn 𝕜 s) (hmn : m + i ≤ n) (hx₀s : x₀ ∈ s) : ContDiffWithinAt 𝕜 m (iteratedFDerivWithin 𝕜 i f s) s x₀ := by induction' i with i hi generalizing m · simp only [CharP.cast_eq_zero, add_zero] at hmn exact (hf.of_le hmn).continuousLinearMap_comp ((continuousMultilinearCurryFin0 𝕜 E F).symm : _ →L[𝕜] E [×0]→L[𝕜] F) · rw [Nat.cast_succ, add_comm _ 1, ← add_assoc] at hmn exact ((hi hmn).fderivWithin_right hs le_rfl hx₀s).continuousLinearMap_comp ((continuousMultilinearCurryLeftEquiv 𝕜 (fun _ : Fin (i+1) ↦ E) F).symm : _ →L[𝕜] E [×(i+1)]→L[𝕜] F) @[deprecated (since := "2025-01-15")] alias ContDiffWithinAt.iteratedFderivWithin_right := ContDiffWithinAt.iteratedFDerivWithin_right /-- `x ↦ fderiv 𝕜 (f x) (g x)` is smooth at `x₀`. -/ protected theorem ContDiffAt.fderiv {f : E → F → G} {g : E → F} (hf : ContDiffAt 𝕜 n (Function.uncurry f) (x₀, g x₀)) (hg : ContDiffAt 𝕜 m g x₀) (hmn : m + 1 ≤ n) : ContDiffAt 𝕜 m (fun x => fderiv 𝕜 (f x) (g x)) x₀ := by simp_rw [← fderivWithin_univ] refine (ContDiffWithinAt.fderivWithin hf.contDiffWithinAt hg.contDiffWithinAt uniqueDiffOn_univ hmn (mem_univ x₀) ?_).contDiffAt univ_mem rw [preimage_univ] /-- `fderiv 𝕜 f` is smooth at `x₀`. -/ theorem ContDiffAt.fderiv_right (hf : ContDiffAt 𝕜 n f x₀) (hmn : m + 1 ≤ n) : ContDiffAt 𝕜 m (fderiv 𝕜 f) x₀ := ContDiffAt.fderiv (ContDiffAt.comp (x₀, x₀) hf contDiffAt_snd) contDiffAt_id hmn theorem ContDiffAt.iteratedFDeriv_right {i : ℕ} (hf : ContDiffAt 𝕜 n f x₀) (hmn : m + i ≤ n) : ContDiffAt 𝕜 m (iteratedFDeriv 𝕜 i f) x₀ := by rw [← iteratedFDerivWithin_univ, ← contDiffWithinAt_univ] at * exact hf.iteratedFDerivWithin_right uniqueDiffOn_univ hmn trivial /-- `x ↦ fderiv 𝕜 (f x) (g x)` is smooth. -/ protected theorem ContDiff.fderiv {f : E → F → G} {g : E → F} (hf : ContDiff 𝕜 m <| Function.uncurry f) (hg : ContDiff 𝕜 n g) (hnm : n + 1 ≤ m) : ContDiff 𝕜 n fun x => fderiv 𝕜 (f x) (g x) := contDiff_iff_contDiffAt.mpr fun _ => hf.contDiffAt.fderiv hg.contDiffAt hnm /-- `fderiv 𝕜 f` is smooth. -/ theorem ContDiff.fderiv_right (hf : ContDiff 𝕜 n f) (hmn : m + 1 ≤ n) : ContDiff 𝕜 m (fderiv 𝕜 f) := contDiff_iff_contDiffAt.mpr fun _x => hf.contDiffAt.fderiv_right hmn theorem ContDiff.iteratedFDeriv_right {i : ℕ} (hf : ContDiff 𝕜 n f) (hmn : m + i ≤ n) : ContDiff 𝕜 m (iteratedFDeriv 𝕜 i f) := contDiff_iff_contDiffAt.mpr fun _x => hf.contDiffAt.iteratedFDeriv_right hmn /-- `x ↦ fderiv 𝕜 (f x) (g x)` is continuous. -/ theorem Continuous.fderiv {f : E → F → G} {g : E → F} (hf : ContDiff 𝕜 n <| Function.uncurry f) (hg : Continuous g) (hn : 1 ≤ n) : Continuous fun x => fderiv 𝕜 (f x) (g x) := (hf.fderiv (contDiff_zero.mpr hg) hn).continuous /-- `x ↦ fderiv 𝕜 (f x) (g x) (k x)` is smooth. -/ theorem ContDiff.fderiv_apply {f : E → F → G} {g k : E → F} (hf : ContDiff 𝕜 m <| Function.uncurry f) (hg : ContDiff 𝕜 n g) (hk : ContDiff 𝕜 n k) (hnm : n + 1 ≤ m) : ContDiff 𝕜 n fun x => fderiv 𝕜 (f x) (g x) (k x) := (hf.fderiv hg hnm).clm_apply hk /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ theorem contDiffOn_fderivWithin_apply {s : Set E} {f : E → F} (hf : ContDiffOn 𝕜 n f s) (hs : UniqueDiffOn 𝕜 s) (hmn : m + 1 ≤ n) : ContDiffOn 𝕜 m (fun p : E × E => (fderivWithin 𝕜 f s p.1 : E →L[𝕜] F) p.2) (s ×ˢ univ) := ((hf.fderivWithin hs hmn).comp contDiffOn_fst (prod_subset_preimage_fst _ _)).clm_apply contDiffOn_snd /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ theorem ContDiffOn.continuousOn_fderivWithin_apply (hf : ContDiffOn 𝕜 n f s) (hs : UniqueDiffOn 𝕜 s) (hn : 1 ≤ n) : ContinuousOn (fun p : E × E => (fderivWithin 𝕜 f s p.1 : E → F) p.2) (s ×ˢ univ) := (contDiffOn_fderivWithin_apply (m := 0) hf hs hn).continuousOn /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ theorem ContDiff.contDiff_fderiv_apply {f : E → F} (hf : ContDiff 𝕜 n f) (hmn : m + 1 ≤ n) : ContDiff 𝕜 m fun p : E × E => (fderiv 𝕜 f p.1 : E →L[𝕜] F) p.2 := by rw [← contDiffOn_univ] at hf ⊢ rw [← fderivWithin_univ, ← univ_prod_univ] exact contDiffOn_fderivWithin_apply hf uniqueDiffOn_univ hmn end bundled section deriv /-! ### One dimension All results up to now have been expressed in terms of the general Fréchet derivative `fderiv`. For maps defined on the field, the one-dimensional derivative `deriv` is often easier to use. In this paragraph, we reformulate some higher smoothness results in terms of `deriv`. -/ variable {f₂ : 𝕜 → F} {s₂ : Set 𝕜} open ContinuousLinearMap (smulRight) /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `derivWithin`) is `C^n`. -/ theorem contDiffOn_succ_iff_derivWithin (hs : UniqueDiffOn 𝕜 s₂) : ContDiffOn 𝕜 (n + 1) f₂ s₂ ↔ DifferentiableOn 𝕜 f₂ s₂ ∧ (n = ω → AnalyticOn 𝕜 f₂ s₂) ∧ ContDiffOn 𝕜 n (derivWithin f₂ s₂) s₂ := by rw [contDiffOn_succ_iff_fderivWithin hs, and_congr_right_iff] intro _ constructor · rintro ⟨h', h⟩ refine ⟨h', ?_⟩ have : derivWithin f₂ s₂ = (fun u : 𝕜 →L[𝕜] F => u 1) ∘ fderivWithin 𝕜 f₂ s₂ := by ext x; rfl simp_rw [this] apply ContDiff.comp_contDiffOn _ h exact (isBoundedBilinearMap_apply.isBoundedLinearMap_left _).contDiff · rintro ⟨h', h⟩ refine ⟨h', ?_⟩ have : fderivWithin 𝕜 f₂ s₂ = smulRight (1 : 𝕜 →L[𝕜] 𝕜) ∘ derivWithin f₂ s₂ := by ext x; simp [derivWithin] simp only [this] apply ContDiff.comp_contDiffOn _ h have : IsBoundedBilinearMap 𝕜 fun _ : (𝕜 →L[𝕜] 𝕜) × F => _ := isBoundedBilinearMap_smulRight exact (this.isBoundedLinearMap_right _).contDiff theorem contDiffOn_infty_iff_derivWithin (hs : UniqueDiffOn 𝕜 s₂) : ContDiffOn 𝕜 ∞ f₂ s₂ ↔ DifferentiableOn 𝕜 f₂ s₂ ∧ ContDiffOn 𝕜 ∞ (derivWithin f₂ s₂) s₂ := by rw [show ∞ = ∞ + 1 by rfl, contDiffOn_succ_iff_derivWithin hs] simp @[deprecated (since := "2024-11-27")] alias contDiffOn_top_iff_derivWithin := contDiffOn_infty_iff_derivWithin /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^n`. -/ theorem contDiffOn_succ_iff_deriv_of_isOpen (hs : IsOpen s₂) : ContDiffOn 𝕜 (n + 1) f₂ s₂ ↔ DifferentiableOn 𝕜 f₂ s₂ ∧ (n = ω → AnalyticOn 𝕜 f₂ s₂) ∧ ContDiffOn 𝕜 n (deriv f₂) s₂ := by rw [contDiffOn_succ_iff_derivWithin hs.uniqueDiffOn] exact Iff.rfl.and (Iff.rfl.and (contDiffOn_congr fun _ => derivWithin_of_isOpen hs)) theorem contDiffOn_infty_iff_deriv_of_isOpen (hs : IsOpen s₂) : ContDiffOn 𝕜 ∞ f₂ s₂ ↔ DifferentiableOn 𝕜 f₂ s₂ ∧ ContDiffOn 𝕜 ∞ (deriv f₂) s₂ := by rw [show ∞ = ∞ + 1 by rfl, contDiffOn_succ_iff_deriv_of_isOpen hs] simp @[deprecated (since := "2024-11-27")] alias contDiffOn_top_iff_deriv_of_isOpen := contDiffOn_infty_iff_deriv_of_isOpen protected theorem ContDiffOn.derivWithin (hf : ContDiffOn 𝕜 n f₂ s₂) (hs : UniqueDiffOn 𝕜 s₂) (hmn : m + 1 ≤ n) : ContDiffOn 𝕜 m (derivWithin f₂ s₂) s₂ := ((contDiffOn_succ_iff_derivWithin hs).1 (hf.of_le hmn)).2.2 theorem ContDiffOn.deriv_of_isOpen (hf : ContDiffOn 𝕜 n f₂ s₂) (hs : IsOpen s₂) (hmn : m + 1 ≤ n) : ContDiffOn 𝕜 m (deriv f₂) s₂ := (hf.derivWithin hs.uniqueDiffOn hmn).congr fun _ hx => (derivWithin_of_isOpen hs hx).symm theorem ContDiffOn.continuousOn_derivWithin (h : ContDiffOn 𝕜 n f₂ s₂) (hs : UniqueDiffOn 𝕜 s₂) (hn : 1 ≤ n) : ContinuousOn (derivWithin f₂ s₂) s₂ := by rw [show (1 : WithTop ℕ∞) = 0 + 1 from rfl] at hn exact ((contDiffOn_succ_iff_derivWithin hs).1 (h.of_le hn)).2.2.continuousOn theorem ContDiffOn.continuousOn_deriv_of_isOpen (h : ContDiffOn 𝕜 n f₂ s₂) (hs : IsOpen s₂) (hn : 1 ≤ n) : ContinuousOn (deriv f₂) s₂ := by rw [show (1 : WithTop ℕ∞) = 0 + 1 from rfl] at hn exact ((contDiffOn_succ_iff_deriv_of_isOpen hs).1 (h.of_le hn)).2.2.continuousOn /-- A function is `C^(n + 1)` if and only if it is differentiable, and its derivative (formulated in terms of `deriv`) is `C^n`. -/ theorem contDiff_succ_iff_deriv : ContDiff 𝕜 (n + 1) f₂ ↔ Differentiable 𝕜 f₂ ∧ (n = ω → AnalyticOn 𝕜 f₂ univ) ∧ ContDiff 𝕜 n (deriv f₂) := by simp only [← contDiffOn_univ, contDiffOn_succ_iff_deriv_of_isOpen, isOpen_univ, differentiableOn_univ] theorem contDiff_one_iff_deriv : ContDiff 𝕜 1 f₂ ↔ Differentiable 𝕜 f₂ ∧ Continuous (deriv f₂) := by rw [show (1 : WithTop ℕ∞) = 0 + 1 from rfl, contDiff_succ_iff_deriv] simp theorem contDiff_infty_iff_deriv : ContDiff 𝕜 ∞ f₂ ↔ Differentiable 𝕜 f₂ ∧ ContDiff 𝕜 ∞ (deriv f₂) := by rw [show (∞ : WithTop ℕ∞) = ∞ + 1 from rfl, contDiff_succ_iff_deriv] simp @[deprecated (since := "2024-11-27")] alias contDiff_top_iff_deriv := contDiff_infty_iff_deriv theorem ContDiff.continuous_deriv (h : ContDiff 𝕜 n f₂) (hn : 1 ≤ n) : Continuous (deriv f₂) := by rw [show (1 : WithTop ℕ∞) = 0 + 1 from rfl] at hn exact (contDiff_succ_iff_deriv.mp (h.of_le hn)).2.2.continuous theorem ContDiff.iterate_deriv : ∀ (n : ℕ) {f₂ : 𝕜 → F}, ContDiff 𝕜 ∞ f₂ → ContDiff 𝕜 ∞ (deriv^[n] f₂) | 0, _, hf => hf | n + 1, _, hf => ContDiff.iterate_deriv n (contDiff_infty_iff_deriv.mp hf).2 theorem ContDiff.iterate_deriv' (n : ℕ) : ∀ (k : ℕ) {f₂ : 𝕜 → F}, ContDiff 𝕜 (n + k : ℕ) f₂ → ContDiff 𝕜 n (deriv^[k] f₂) | 0, _, hf => hf | k + 1, _, hf => ContDiff.iterate_deriv' _ k (contDiff_succ_iff_deriv.mp hf).2.2 end deriv
Mathlib/Analysis/Calculus/ContDiff/Basic.lean
1,370
1,383
/- 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
Mathlib/Data/Set/Card.lean
164
167
/- Copyright (c) 2022 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import Mathlib.Data.Sym.Sym2 import Mathlib.Logic.Relation /-! # Game addition relation This file defines, given relations `rα : α → α → Prop` and `rβ : β → β → Prop`, a relation `Prod.GameAdd` on pairs, such that `GameAdd rα rβ x y` iff `x` can be reached from `y` by decreasing either entry (with respect to `rα` and `rβ`). It is so called since it models the subsequency relation on the addition of combinatorial games. We also define `Sym2.GameAdd`, which is the unordered pair analog of `Prod.GameAdd`. ## Main definitions and results - `Prod.GameAdd`: the game addition relation on ordered pairs. - `WellFounded.prod_gameAdd`: formalizes induction on ordered pairs, where exactly one entry decreases at a time. - `Sym2.GameAdd`: the game addition relation on unordered pairs. - `WellFounded.sym2_gameAdd`: formalizes induction on unordered pairs, where exactly one entry decreases at a time. -/ variable {α β : Type*} {rα : α → α → Prop} {rβ : β → β → Prop} {a : α} {b : β} /-! ### `Prod.GameAdd` -/ namespace Prod variable (rα rβ) /-- `Prod.GameAdd rα rβ x y` means that `x` can be reached from `y` by decreasing either entry with respect to the relations `rα` and `rβ`. It is so called, as it models game addition within combinatorial game theory. If `rα a₁ a₂` means that `a₂ ⟶ a₁` is a valid move in game `α`, and `rβ b₁ b₂` means that `b₂ ⟶ b₁` is a valid move in game `β`, then `GameAdd rα rβ` specifies the valid moves in the juxtaposition of `α` and `β`: the player is free to choose one of the games and make a move in it, while leaving the other game unchanged. See `Sym2.GameAdd` for the unordered pair analog. -/ inductive GameAdd : α × β → α × β → Prop | fst {a₁ a₂ b} : rα a₁ a₂ → GameAdd (a₁, b) (a₂, b) | snd {a b₁ b₂} : rβ b₁ b₂ → GameAdd (a, b₁) (a, b₂) theorem gameAdd_iff {rα rβ} {x y : α × β} : GameAdd rα rβ x y ↔ rα x.1 y.1 ∧ x.2 = y.2 ∨ rβ x.2 y.2 ∧ x.1 = y.1 := by constructor · rintro (@⟨a₁, a₂, b, h⟩ | @⟨a, b₁, b₂, h⟩) exacts [Or.inl ⟨h, rfl⟩, Or.inr ⟨h, rfl⟩] · revert x y rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨h, rfl : b₁ = b₂⟩ | ⟨h, rfl : a₁ = a₂⟩) exacts [GameAdd.fst h, GameAdd.snd h] theorem gameAdd_mk_iff {rα rβ} {a₁ a₂ : α} {b₁ b₂ : β} : GameAdd rα rβ (a₁, b₁) (a₂, b₂) ↔ rα a₁ a₂ ∧ b₁ = b₂ ∨ rβ b₁ b₂ ∧ a₁ = a₂ := gameAdd_iff @[simp] theorem gameAdd_swap_swap : ∀ a b : α × β, GameAdd rβ rα a.swap b.swap ↔ GameAdd rα rβ a b := fun ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ => by rw [Prod.swap, Prod.swap, gameAdd_mk_iff, gameAdd_mk_iff, or_comm] theorem gameAdd_swap_swap_mk (a₁ a₂ : α) (b₁ b₂ : β) : GameAdd rα rβ (a₁, b₁) (a₂, b₂) ↔ GameAdd rβ rα (b₁, a₁) (b₂, a₂) :=
gameAdd_swap_swap rβ rα (b₁, a₁) (b₂, a₂)
Mathlib/Order/GameAdd.lean
73
74
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import Mathlib.CategoryTheory.Abelian.Exact import Mathlib.CategoryTheory.Comma.Over.Basic import Mathlib.Algebra.Category.ModuleCat.EpiMono /-! # Pseudoelements in abelian categories A *pseudoelement* of an object `X` in an abelian category `C` is an equivalence class of arrows ending in `X`, where two arrows are considered equivalent if we can find two epimorphisms with a common domain making a commutative square with the two arrows. While the construction shows that pseudoelements are actually subobjects of `X` rather than "elements", it is possible to chase these pseudoelements through commutative diagrams in an abelian category to prove exactness properties. This is done using some "diagram-chasing metatheorems" proved in this file. In many cases, a proof in the category of abelian groups can more or less directly be converted into a proof using pseudoelements. A classic application of pseudoelements are diagram lemmas like the four lemma or the snake lemma. Pseudoelements are in some ways weaker than actual elements in a concrete category. The most important limitation is that there is no extensionality principle: If `f g : X ⟶ Y`, then `∀ x ∈ X, f x = g x` does not necessarily imply that `f = g` (however, if `f = 0` or `g = 0`, it does). A corollary of this is that we can not define arrows in abelian categories by dictating their action on pseudoelements. Thus, a usual style of proofs in abelian categories is this: First, we construct some morphism using universal properties, and then we use diagram chasing of pseudoelements to verify that is has some desirable property such as exactness. It should be noted that the Freyd-Mitchell embedding theorem (see `CategoryTheory.Abelian.FreydMitchell`) gives a vastly stronger notion of pseudoelement (in particular one that gives extensionality) and this file should be updated to go use that instead! ## Main results We define the type of pseudoelements of an object and, in particular, the zero pseudoelement. We prove that every morphism maps the zero pseudoelement to the zero pseudoelement (`apply_zero`) and that a zero morphism maps every pseudoelement to the zero pseudoelement (`zero_apply`). Here are the metatheorems we provide: * A morphism `f` is zero if and only if it is the zero function on pseudoelements. * A morphism `f` is an epimorphism if and only if it is surjective on pseudoelements. * A morphism `f` is a monomorphism if and only if it is injective on pseudoelements if and only if `∀ a, f a = 0 → f = 0`. * A sequence `f, g` of morphisms is exact if and only if `∀ a, g (f a) = 0` and `∀ b, g b = 0 → ∃ a, f a = b`. * If `f` is a morphism and `a, a'` are such that `f a = f a'`, then there is some pseudoelement `a''` such that `f a'' = 0` and for every `g` we have `g a' = 0 → g a = g a''`. We can think of `a''` as `a - a'`, but don't get too carried away by that: pseudoelements of an object do not form an abelian group. ## Notations We introduce coercions from an object of an abelian category to the set of its pseudoelements and from a morphism to the function it induces on pseudoelements. These coercions must be explicitly enabled via local instances: `attribute [local instance] objectToSort homToFun` ## Implementation notes It appears that sometimes the coercion from morphisms to functions does not work, i.e., writing `g a` raises a "function expected" error. This error can be fixed by writing `(g : X ⟶ Y) a`. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ open CategoryTheory open CategoryTheory.Limits open CategoryTheory.Abelian open CategoryTheory.Preadditive universe v u namespace CategoryTheory.Abelian variable {C : Type u} [Category.{v} C] attribute [local instance] Over.coeFromHom /-- This is just composition of morphisms in `C`. Another way to express this would be `(Over.map f).obj a`, but our definition has nicer definitional properties. -/ def app {P Q : C} (f : P ⟶ Q) (a : Over P) : Over Q := a.hom ≫ f @[simp] theorem app_hom {P Q : C} (f : P ⟶ Q) (a : Over P) : (app f a).hom = a.hom ≫ f := rfl /-- Two arrows `f : X ⟶ P` and `g : Y ⟶ P` are called pseudo-equal if there is some object `R` and epimorphisms `p : R ⟶ X` and `q : R ⟶ Y` such that `p ≫ f = q ≫ g`. -/ def PseudoEqual (P : C) (f g : Over P) : Prop := ∃ (R : C) (p : R ⟶ f.1) (q : R ⟶ g.1) (_ : Epi p) (_ : Epi q), p ≫ f.hom = q ≫ g.hom theorem pseudoEqual_refl {P : C} : Reflexive (PseudoEqual P) := fun f => ⟨f.1, 𝟙 f.1, 𝟙 f.1, inferInstance, inferInstance, by simp⟩ theorem pseudoEqual_symm {P : C} : Symmetric (PseudoEqual P) := fun _ _ ⟨R, p, q, ep, Eq, comm⟩ => ⟨R, q, p, Eq, ep, comm.symm⟩ variable [Abelian.{v} C] section /-- Pseudoequality is transitive: Just take the pullback. The pullback morphisms will be epimorphisms since in an abelian category, pullbacks of epimorphisms are epimorphisms. -/ theorem pseudoEqual_trans {P : C} : Transitive (PseudoEqual P) := by intro f g h ⟨R, p, q, ep, Eq, comm⟩ ⟨R', p', q', ep', eq', comm'⟩ refine ⟨pullback q p', pullback.fst _ _ ≫ p, pullback.snd _ _ ≫ q', epi_comp _ _, epi_comp _ _, ?_⟩ rw [Category.assoc, comm, ← Category.assoc, pullback.condition, Category.assoc, comm', Category.assoc] end /-- The arrows with codomain `P` equipped with the equivalence relation of being pseudo-equal. -/ def Pseudoelement.setoid (P : C) : Setoid (Over P) := ⟨_, ⟨pseudoEqual_refl, @pseudoEqual_symm _ _ _, @pseudoEqual_trans _ _ _ _⟩⟩ attribute [local instance] Pseudoelement.setoid /-- A `Pseudoelement` of `P` is just an equivalence class of arrows ending in `P` by being pseudo-equal. -/ def Pseudoelement (P : C) : Type max u v := Quotient (Pseudoelement.setoid P) namespace Pseudoelement /-- A coercion from an object of an abelian category to its pseudoelements. -/ def objectToSort : CoeSort C (Type max u v) := ⟨fun P => Pseudoelement P⟩ attribute [local instance] objectToSort scoped[Pseudoelement] attribute [instance] CategoryTheory.Abelian.Pseudoelement.objectToSort /-- A coercion from an arrow with codomain `P` to its associated pseudoelement. -/ def overToSort {P : C} : Coe (Over P) (Pseudoelement P) := ⟨Quot.mk (PseudoEqual P)⟩ attribute [local instance] overToSort theorem over_coe_def {P Q : C} (a : Q ⟶ P) : (a : Pseudoelement P) = ⟦↑a⟧ := rfl /-- If two elements are pseudo-equal, then their composition with a morphism is, too. -/ theorem pseudoApply_aux {P Q : C} (f : P ⟶ Q) (a b : Over P) : a ≈ b → app f a ≈ app f b := fun ⟨R, p, q, ep, Eq, comm⟩ => ⟨R, p, q, ep, Eq, show p ≫ a.hom ≫ f = q ≫ b.hom ≫ f by rw [reassoc_of% comm]⟩ /-- A morphism `f` induces a function `pseudoApply f` on pseudoelements. -/ def pseudoApply {P Q : C} (f : P ⟶ Q) : P → Q := Quotient.map (fun g : Over P => app f g) (pseudoApply_aux f) /-- A coercion from morphisms to functions on pseudoelements. -/ def homToFun {P Q : C} : CoeFun (P ⟶ Q) fun _ => P → Q := ⟨pseudoApply⟩ attribute [local instance] homToFun scoped[Pseudoelement] attribute [instance] CategoryTheory.Abelian.Pseudoelement.homToFun theorem pseudoApply_mk' {P Q : C} (f : P ⟶ Q) (a : Over P) : f ⟦a⟧ = ⟦↑(a.hom ≫ f)⟧ := rfl /-- Applying a pseudoelement to a composition of morphisms is the same as composing with each morphism. Sadly, this is not a definitional equality, but at least it is true. -/ theorem comp_apply {P Q R : C} (f : P ⟶ Q) (g : Q ⟶ R) (a : P) : (f ≫ g) a = g (f a) := Quotient.inductionOn a fun x => Quotient.sound <| by simp only [app] rw [← Category.assoc, Over.coe_hom] /-- Composition of functions on pseudoelements is composition of morphisms. -/ theorem comp_comp {P Q R : C} (f : P ⟶ Q) (g : Q ⟶ R) : g ∘ f = f ≫ g := funext fun _ => (comp_apply _ _ _).symm section Zero /-! In this section we prove that for every `P` there is an equivalence class that contains precisely all the zero morphisms ending in `P` and use this to define *the* zero pseudoelement. -/ section attribute [local instance] HasBinaryBiproducts.of_hasBinaryProducts /-- The arrows pseudo-equal to a zero morphism are precisely the zero morphisms. -/ theorem pseudoZero_aux {P : C} (Q : C) (f : Over P) : f ≈ (0 : Q ⟶ P) ↔ f.hom = 0 := ⟨fun ⟨R, p, q, _, _, comm⟩ => zero_of_epi_comp p (by simp [comm]), fun hf => ⟨biprod f.1 Q, biprod.fst, biprod.snd, inferInstance, inferInstance, by rw [hf, Over.coe_hom, HasZeroMorphisms.comp_zero, HasZeroMorphisms.comp_zero]⟩⟩ end theorem zero_eq_zero' {P Q R : C} : (⟦((0 : Q ⟶ P) : Over P)⟧ : Pseudoelement P) = ⟦((0 : R ⟶ P) : Over P)⟧ := Quotient.sound <| (pseudoZero_aux R _).2 rfl /-- The zero pseudoelement is the class of a zero morphism. -/ def pseudoZero {P : C} : P := ⟦(0 : P ⟶ P)⟧ -- Porting note: in mathlib3, we couldn't make this an instance -- as it would have fired on `coe_sort`. -- However now that coercions are treated differently, this is a structural instance triggered by -- the appearance of `Pseudoelement`. instance hasZero {P : C} : Zero P := ⟨pseudoZero⟩ instance {P : C} : Inhabited P := ⟨0⟩ theorem pseudoZero_def {P : C} : (0 : Pseudoelement P) = ⟦↑(0 : P ⟶ P)⟧ := rfl @[simp] theorem zero_eq_zero {P Q : C} : ⟦((0 : Q ⟶ P) : Over P)⟧ = (0 : Pseudoelement P) := zero_eq_zero' /-- The pseudoelement induced by an arrow is zero precisely when that arrow is zero. -/ theorem pseudoZero_iff {P : C} (a : Over P) : a = (0 : P) ↔ a.hom = 0 := by rw [← pseudoZero_aux P a] exact Quotient.eq' end Zero open Pseudoelement /-- Morphisms map the zero pseudoelement to the zero pseudoelement. -/ @[simp] theorem apply_zero {P Q : C} (f : P ⟶ Q) : f 0 = 0 := by rw [pseudoZero_def, pseudoApply_mk'] simp /-- The zero morphism maps every pseudoelement to 0. -/ @[simp] theorem zero_apply {P : C} (Q : C) (a : P) : (0 : P ⟶ Q) a = 0 := Quotient.inductionOn a fun a' => by rw [pseudoZero_def, pseudoApply_mk'] simp /-- An extensionality lemma for being the zero arrow. -/ theorem zero_morphism_ext {P Q : C} (f : P ⟶ Q) : (∀ a, f a = 0) → f = 0 := fun h => by rw [← Category.id_comp f] exact (pseudoZero_iff (𝟙 P ≫ f : Over Q)).1 (h (𝟙 P)) theorem zero_morphism_ext' {P Q : C} (f : P ⟶ Q) : (∀ a, f a = 0) → 0 = f := Eq.symm ∘ zero_morphism_ext f theorem eq_zero_iff {P Q : C} (f : P ⟶ Q) : f = 0 ↔ ∀ a, f a = 0 := ⟨fun h a => by simp [h], zero_morphism_ext _⟩ /-- A monomorphism is injective on pseudoelements. -/ theorem pseudo_injective_of_mono {P Q : C} (f : P ⟶ Q) [Mono f] : Function.Injective f := by intro abar abar' refine Quotient.inductionOn₂ abar abar' fun a a' ha => ?_ apply Quotient.sound have : (⟦(a.hom ≫ f : Over Q)⟧ : Quotient (setoid Q)) = ⟦↑(a'.hom ≫ f)⟧ := by convert ha have ⟨R, p, q, ep, Eq, comm⟩ := Quotient.exact this exact ⟨R, p, q, ep, Eq, (cancel_mono f).1 <| by simp only [Category.assoc] exact comm⟩ /-- A morphism that is injective on pseudoelements only maps the zero element to zero. -/ theorem zero_of_map_zero {P Q : C} (f : P ⟶ Q) : Function.Injective f → ∀ a, f a = 0 → a = 0 := fun h a ha => by rw [← apply_zero f] at ha exact h ha /-- A morphism that only maps the zero pseudoelement to zero is a monomorphism. -/ theorem mono_of_zero_of_map_zero {P Q : C} (f : P ⟶ Q) : (∀ a, f a = 0 → a = 0) → Mono f := fun h => (mono_iff_cancel_zero _).2 fun _ g hg => (pseudoZero_iff (g : Over P)).1 <| h _ <| show f g = 0 from (pseudoZero_iff (g ≫ f : Over Q)).2 hg section /-- An epimorphism is surjective on pseudoelements. -/ theorem pseudo_surjective_of_epi {P Q : C} (f : P ⟶ Q) [Epi f] : Function.Surjective f := fun qbar => Quotient.inductionOn qbar fun q => ⟨(pullback.fst f q.hom : Over P), Quotient.sound <| ⟨pullback f q.hom, 𝟙 (pullback f q.hom), pullback.snd _ _, inferInstance, inferInstance, by rw [Category.id_comp, ← pullback.condition, app_hom, Over.coe_hom]⟩⟩ end /-- A morphism that is surjective on pseudoelements is an epimorphism. -/ theorem epi_of_pseudo_surjective {P Q : C} (f : P ⟶ Q) : Function.Surjective f → Epi f := by intro h have ⟨pbar, hpbar⟩ := h (𝟙 Q) have ⟨p, hp⟩ := Quotient.exists_rep pbar have : (⟦(p.hom ≫ f : Over Q)⟧ : Quotient (setoid Q)) = ⟦↑(𝟙 Q)⟧ := by rw [← hp] at hpbar exact hpbar have ⟨R, x, y, _, ey, comm⟩ := Quotient.exact this apply @epi_of_epi_fac _ _ _ _ _ (x ≫ p.hom) f y ey dsimp at comm rw [Category.assoc, comm] apply Category.comp_id section /-- Two morphisms in an exact sequence are exact on pseudoelements. -/ theorem pseudo_exact_of_exact {S : ShortComplex C} (hS : S.Exact) : ∀ b, S.g b = 0 → ∃ a, S.f a = b := fun b' => Quotient.inductionOn b' fun b hb => by have hb' : b.hom ≫ S.g = 0 := (pseudoZero_iff _).1 hb -- By exactness, `b` factors through `im f = ker g` via some `c`. obtain ⟨c, hc⟩ := KernelFork.IsLimit.lift' hS.isLimitImage _ hb' -- We compute the pullback of the map into the image and `c`. -- The pseudoelement induced by the first pullback map will be our preimage. use pullback.fst (Abelian.factorThruImage S.f) c -- It remains to show that the image of this element under `f` is pseudo-equal to `b`. apply Quotient.sound
refine ⟨pullback (Abelian.factorThruImage S.f) c, 𝟙 _, pullback.snd _ _, inferInstance, inferInstance, ?_⟩ -- Now we can verify that the diagram commutes. calc 𝟙 (pullback (Abelian.factorThruImage S.f) c) ≫ pullback.fst _ _ ≫ S.f = pullback.fst _ _ ≫ S.f := Category.id_comp _
Mathlib/CategoryTheory/Abelian/Pseudoelements.lean
330
336
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam, Yury Kudryashov -/ import Mathlib.Algebra.MvPolynomial.Derivation import Mathlib.Algebra.MvPolynomial.Variables /-! # Partial derivatives of polynomials This file defines the notion of the formal *partial derivative* of a polynomial, the derivative with respect to a single variable. This derivative is not connected to the notion of derivative from analysis. It is based purely on the polynomial exponents and coefficients. ## Main declarations * `MvPolynomial.pderiv i p` : the partial derivative of `p` with respect to `i`, as a bundled derivation of `MvPolynomial σ R`. ## Notation As in other polynomial files, we typically use the notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[CommRing R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : MvPolynomial σ R` -/ noncomputable section universe u v namespace MvPolynomial open Set Function Finsupp variable {R : Type u} {σ : Type v} {a a' a₁ a₂ : R} {s : σ →₀ ℕ} section PDeriv variable [CommSemiring R] /-- `pderiv i p` is the partial derivative of `p` with respect to `i` -/ def pderiv (i : σ) : Derivation R (MvPolynomial σ R) (MvPolynomial σ R) := letI := Classical.decEq σ mkDerivation R <| Pi.single i 1 theorem pderiv_def [DecidableEq σ] (i : σ) : pderiv i = mkDerivation R (Pi.single i 1) := by unfold pderiv; congr! @[simp] theorem pderiv_monomial {i : σ} : pderiv i (monomial s a) = monomial (s - single i 1) (a * s i) := by classical simp only [pderiv_def, mkDerivation_monomial, Finsupp.smul_sum, smul_eq_mul, ← smul_mul_assoc, ← (monomial _).map_smul] refine (Finset.sum_eq_single i (fun j _ hne => ?_) fun hi => ?_).trans ?_ · simp [Pi.single_eq_of_ne hne] · rw [Finsupp.not_mem_support_iff] at hi; simp [hi] · simp lemma X_mul_pderiv_monomial {i : σ} {m : σ →₀ ℕ} {r : R} : X i * pderiv i (monomial m r) = m i • monomial m r := by rw [pderiv_monomial, X, monomial_mul, smul_monomial] by_cases h : m i = 0 · simp_rw [h, Nat.cast_zero, mul_zero, zero_smul, monomial_zero] rw [one_mul, mul_comm, nsmul_eq_mul, add_comm, sub_add_single_one_cancel h] theorem pderiv_C {i : σ} : pderiv i (C a) = 0 := derivation_C _ _ theorem pderiv_one {i : σ} : pderiv i (1 : MvPolynomial σ R) = 0 := pderiv_C @[simp] theorem pderiv_X [DecidableEq σ] (i j : σ) : pderiv i (X j : MvPolynomial σ R) = Pi.single (f := fun _ => _) i 1 j := by rw [pderiv_def, mkDerivation_X] @[simp] theorem pderiv_X_self (i : σ) : pderiv i (X i : MvPolynomial σ R) = 1 := by classical simp @[simp] theorem pderiv_X_of_ne {i j : σ} (h : j ≠ i) : pderiv i (X j : MvPolynomial σ R) = 0 := by classical simp [h] theorem pderiv_eq_zero_of_not_mem_vars {i : σ} {f : MvPolynomial σ R} (h : i ∉ f.vars) : pderiv i f = 0 := derivation_eq_zero_of_forall_mem_vars fun _ hj => pderiv_X_of_ne <| ne_of_mem_of_not_mem hj h theorem pderiv_monomial_single {i : σ} {n : ℕ} : pderiv i (monomial (single i n) a) = monomial (single i (n - 1)) (a * n) := by simp theorem pderiv_mul {i : σ} {f g : MvPolynomial σ R} : pderiv i (f * g) = pderiv i f * g + f * pderiv i g := by simp only [(pderiv i).leibniz f g, smul_eq_mul, mul_comm, add_comm] theorem pderiv_pow {i : σ} {f : MvPolynomial σ R} {n : ℕ} : pderiv i (f ^ n) = n * f ^ (n - 1) * pderiv i f := by rw [(pderiv i).leibniz_pow f n, nsmul_eq_mul, smul_eq_mul, mul_assoc] theorem pderiv_C_mul {f : MvPolynomial σ R} {i : σ} : pderiv i (C a * f) = C a * pderiv i f := by rw [C_mul', Derivation.map_smul, C_mul'] theorem pderiv_map {S} [CommSemiring S] {φ : R →+* S} {f : MvPolynomial σ R} {i : σ} : pderiv i (map φ f) = map φ (pderiv i f) := by apply induction_on f (fun r ↦ by simp) (fun p q hp hq ↦ by simp [hp, hq]) fun p j eq ↦ ?_ obtain rfl | h := eq_or_ne j i · simp [eq] · simp [eq, h] lemma pderiv_rename {τ : Type*} {f : σ → τ} (hf : Function.Injective f)
(x : σ) (p : MvPolynomial σ R) : pderiv (f x) (rename f p) = rename f (pderiv x p) := by
Mathlib/Algebra/MvPolynomial/PDeriv.lean
125
126
/- 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.Group.Submonoid.Operations import Mathlib.Algebra.MonoidAlgebra.Defs import Mathlib.Algebra.Order.Monoid.Unbundled.WithTop import Mathlib.Algebra.Ring.Action.Rat import Mathlib.Data.Finset.Sort import Mathlib.Tactic.FastInstance /-! # Theory of univariate polynomials This file defines `Polynomial R`, the type of univariate polynomials over the semiring `R`, builds a semiring structure on it, and gives basic definitions that are expanded in other files in this directory. ## Main definitions * `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map. * `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism. * `X` is the polynomial `X`, i.e., `monomial 1 1`. * `p.sum f` is `∑ n ∈ p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied to coefficients of the polynomial `p`. * `p.erase n` is the polynomial `p` in which one removes the `c X^n` term. There are often two natural variants of lemmas involving sums, depending on whether one acts on the polynomials, or on the function. The naming convention is that one adds `index` when acting on the polynomials. For instance, * `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`; * `sum_add` states that `p.sum (fun n x ↦ f n x + g n x) = p.sum f + p.sum g`. * Notation to refer to `Polynomial R`, as `R[X]` or `R[t]`. ## Implementation Polynomials are defined using `R[ℕ]`, where `R` is a semiring. The variable `X` commutes with every polynomial `p`: lemma `X_mul` proves the identity `X * p = p * X`. The relationship to `R[ℕ]` is through a structure to make polynomials irreducible from the point of view of the kernel. Most operations are irreducible since Lean can not compute anyway with `AddMonoidAlgebra`. There are two exceptions that we make semireducible: * The zero polynomial, so that its coefficients are definitionally equal to `0`. * The scalar action, to permit typeclass search to unfold it to resolve potential instance diamonds. The raw implementation of the equivalence between `R[X]` and `R[ℕ]` is done through `ofFinsupp` and `toFinsupp` (or, equivalently, `rcases p` when `p` is a polynomial gives an element `q` of `R[ℕ]`, and conversely `⟨q⟩` gives back `p`). The equivalence is also registered as a ring equiv in `Polynomial.toFinsuppIso`. These should in general not be used once the basic API for polynomials is constructed. -/ noncomputable section /-- `Polynomial R` is the type of univariate polynomials over `R`, denoted as `R[X]` within the `Polynomial` namespace. Polynomials should be seen as (semi-)rings with the additional constructor `X`. The embedding from `R` is called `C`. -/ structure Polynomial (R : Type*) [Semiring R] where ofFinsupp :: toFinsupp : AddMonoidAlgebra R ℕ @[inherit_doc] scoped[Polynomial] notation:9000 R "[X]" => Polynomial R open AddMonoidAlgebra Finset open Finsupp hiding single open Function hiding Commute namespace Polynomial universe u variable {R : Type u} {a b : R} {m n : ℕ} section Semiring variable [Semiring R] {p q : R[X]} theorem forall_iff_forall_finsupp (P : R[X] → Prop) : (∀ p, P p) ↔ ∀ q : R[ℕ], P ⟨q⟩ := ⟨fun h q => h ⟨q⟩, fun h ⟨p⟩ => h p⟩ theorem exists_iff_exists_finsupp (P : R[X] → Prop) : (∃ p, P p) ↔ ∃ q : R[ℕ], P ⟨q⟩ := ⟨fun ⟨⟨p⟩, hp⟩ => ⟨p, hp⟩, fun ⟨q, hq⟩ => ⟨⟨q⟩, hq⟩⟩ @[simp] theorem eta (f : R[X]) : Polynomial.ofFinsupp f.toFinsupp = f := by cases f; rfl /-! ### Conversions to and from `AddMonoidAlgebra` Since `R[X]` is not defeq to `R[ℕ]`, but instead is a structure wrapping it, we have to copy across all the arithmetic operators manually, along with the lemmas about how they unfold around `Polynomial.ofFinsupp` and `Polynomial.toFinsupp`. -/ section AddMonoidAlgebra private irreducible_def add : R[X] → R[X] → R[X] | ⟨a⟩, ⟨b⟩ => ⟨a + b⟩ private irreducible_def neg {R : Type u} [Ring R] : R[X] → R[X] | ⟨a⟩ => ⟨-a⟩ private irreducible_def mul : R[X] → R[X] → R[X] | ⟨a⟩, ⟨b⟩ => ⟨a * b⟩ instance zero : Zero R[X] := ⟨⟨0⟩⟩ instance one : One R[X] := ⟨⟨1⟩⟩ instance add' : Add R[X] := ⟨add⟩ instance neg' {R : Type u} [Ring R] : Neg R[X] := ⟨neg⟩ instance sub {R : Type u} [Ring R] : Sub R[X] := ⟨fun a b => a + -b⟩ instance mul' : Mul R[X] := ⟨mul⟩ -- If the private definitions are accidentally exposed, simplify them away. @[simp] theorem add_eq_add : add p q = p + q := rfl @[simp] theorem mul_eq_mul : mul p q = p * q := rfl instance instNSMul : SMul ℕ R[X] where smul r p := ⟨r • p.toFinsupp⟩ instance smulZeroClass {S : Type*} [SMulZeroClass S R] : SMulZeroClass S R[X] where smul r p := ⟨r • p.toFinsupp⟩ smul_zero a := congr_arg ofFinsupp (smul_zero a) instance {S : Type*} [Zero S] [SMulZeroClass S R] [NoZeroSMulDivisors S R] : NoZeroSMulDivisors S R[X] where eq_zero_or_eq_zero_of_smul_eq_zero eq := (eq_zero_or_eq_zero_of_smul_eq_zero <| congr_arg toFinsupp eq).imp id (congr_arg ofFinsupp) -- to avoid a bug in the `ring` tactic instance (priority := 1) pow : Pow R[X] ℕ where pow p n := npowRec n p @[simp] theorem ofFinsupp_zero : (⟨0⟩ : R[X]) = 0 := rfl @[simp] theorem ofFinsupp_one : (⟨1⟩ : R[X]) = 1 := rfl @[simp] theorem ofFinsupp_add {a b} : (⟨a + b⟩ : R[X]) = ⟨a⟩ + ⟨b⟩ := show _ = add _ _ by rw [add_def] @[simp] theorem ofFinsupp_neg {R : Type u} [Ring R] {a} : (⟨-a⟩ : R[X]) = -⟨a⟩ := show _ = neg _ by rw [neg_def] @[simp] theorem ofFinsupp_sub {R : Type u} [Ring R] {a b} : (⟨a - b⟩ : R[X]) = ⟨a⟩ - ⟨b⟩ := by rw [sub_eq_add_neg, ofFinsupp_add, ofFinsupp_neg] rfl @[simp] theorem ofFinsupp_mul (a b) : (⟨a * b⟩ : R[X]) = ⟨a⟩ * ⟨b⟩ := show _ = mul _ _ by rw [mul_def] @[simp] theorem ofFinsupp_nsmul (a : ℕ) (b) : (⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl @[simp] theorem ofFinsupp_smul {S : Type*} [SMulZeroClass S R] (a : S) (b) : (⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl @[simp] theorem ofFinsupp_pow (a) (n : ℕ) : (⟨a ^ n⟩ : R[X]) = ⟨a⟩ ^ n := by change _ = npowRec n _ induction n with | zero => simp [npowRec] | succ n n_ih => simp [npowRec, n_ih, pow_succ] @[simp] theorem toFinsupp_zero : (0 : R[X]).toFinsupp = 0 := rfl @[simp] theorem toFinsupp_one : (1 : R[X]).toFinsupp = 1 := rfl @[simp] theorem toFinsupp_add (a b : R[X]) : (a + b).toFinsupp = a.toFinsupp + b.toFinsupp := by cases a cases b rw [← ofFinsupp_add] @[simp] theorem toFinsupp_neg {R : Type u} [Ring R] (a : R[X]) : (-a).toFinsupp = -a.toFinsupp := by cases a rw [← ofFinsupp_neg] @[simp] theorem toFinsupp_sub {R : Type u} [Ring R] (a b : R[X]) : (a - b).toFinsupp = a.toFinsupp - b.toFinsupp := by rw [sub_eq_add_neg, ← toFinsupp_neg, ← toFinsupp_add] rfl @[simp] theorem toFinsupp_mul (a b : R[X]) : (a * b).toFinsupp = a.toFinsupp * b.toFinsupp := by cases a cases b rw [← ofFinsupp_mul] @[simp] theorem toFinsupp_nsmul (a : ℕ) (b : R[X]) : (a • b).toFinsupp = a • b.toFinsupp := rfl @[simp] theorem toFinsupp_smul {S : Type*} [SMulZeroClass S R] (a : S) (b : R[X]) : (a • b).toFinsupp = a • b.toFinsupp := rfl @[simp] theorem toFinsupp_pow (a : R[X]) (n : ℕ) : (a ^ n).toFinsupp = a.toFinsupp ^ n := by cases a rw [← ofFinsupp_pow] theorem _root_.IsSMulRegular.polynomial {S : Type*} [SMulZeroClass S R] {a : S} (ha : IsSMulRegular R a) : IsSMulRegular R[X] a | ⟨_x⟩, ⟨_y⟩, h => congr_arg _ <| ha.finsupp (Polynomial.ofFinsupp.inj h) theorem toFinsupp_injective : Function.Injective (toFinsupp : R[X] → AddMonoidAlgebra _ _) := fun ⟨_x⟩ ⟨_y⟩ => congr_arg _ @[simp] theorem toFinsupp_inj {a b : R[X]} : a.toFinsupp = b.toFinsupp ↔ a = b := toFinsupp_injective.eq_iff @[simp] theorem toFinsupp_eq_zero {a : R[X]} : a.toFinsupp = 0 ↔ a = 0 := by rw [← toFinsupp_zero, toFinsupp_inj] @[simp] theorem toFinsupp_eq_one {a : R[X]} : a.toFinsupp = 1 ↔ a = 1 := by rw [← toFinsupp_one, toFinsupp_inj] /-- A more convenient spelling of `Polynomial.ofFinsupp.injEq` in terms of `Iff`. -/ theorem ofFinsupp_inj {a b} : (⟨a⟩ : R[X]) = ⟨b⟩ ↔ a = b := iff_of_eq (ofFinsupp.injEq _ _) @[simp] theorem ofFinsupp_eq_zero {a} : (⟨a⟩ : R[X]) = 0 ↔ a = 0 := by rw [← ofFinsupp_zero, ofFinsupp_inj] @[simp] theorem ofFinsupp_eq_one {a} : (⟨a⟩ : R[X]) = 1 ↔ a = 1 := by rw [← ofFinsupp_one, ofFinsupp_inj]
instance inhabited : Inhabited R[X] := ⟨0⟩
Mathlib/Algebra/Polynomial/Basic.lean
266
267
/- 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.Analysis.Analytic.Within import Mathlib.Analysis.Calculus.FDeriv.Analytic import Mathlib.Analysis.Calculus.ContDiff.FTaylorSeries /-! # Higher differentiability A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous. By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or, equivalently, if it is `C^1` and its derivative is `C^{n-1}`. It is `C^∞` if it is `C^n` for all n. Finally, it is `C^ω` if it is analytic (as well as all its derivative, which is automatic if the space is complete). We formalize these notions with predicates `ContDiffWithinAt`, `ContDiffAt`, `ContDiffOn` and `ContDiff` saying that the function is `C^n` within a set at a point, at a point, on a set and on the whole space respectively. To avoid the issue of choice when choosing a derivative in sets where the derivative is not necessarily unique, `ContDiffOn` is not defined directly in terms of the regularity of the specific choice `iteratedFDerivWithin 𝕜 n f s` inside `s`, but in terms of the existence of a nice sequence of derivatives, expressed with a predicate `HasFTaylorSeriesUpToOn` defined in the file `FTaylorSeries`. We prove basic properties of these notions. ## Main definitions and results Let `f : E → F` be a map between normed vector spaces over a nontrivially normed field `𝕜`. * `ContDiff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to rank `n`. * `ContDiffOn 𝕜 n f s`: expresses that `f` is `C^n` in `s`. * `ContDiffAt 𝕜 n f x`: expresses that `f` is `C^n` around `x`. * `ContDiffWithinAt 𝕜 n f s x`: expresses that `f` is `C^n` around `x` within the set `s`. In sets of unique differentiability, `ContDiffOn 𝕜 n f s` can be expressed in terms of the properties of `iteratedFDerivWithin 𝕜 m f s` for `m ≤ n`. In the whole space, `ContDiff 𝕜 n f` can be expressed in terms of the properties of `iteratedFDeriv 𝕜 m f` for `m ≤ n`. ## Implementation notes The definitions in this file are designed to work on any field `𝕜`. They are sometimes slightly more complicated than the naive definitions one would guess from the intuition over the real or complex numbers, but they are designed to circumvent the lack of gluing properties and partitions of unity in general. In the usual situations, they coincide with the usual definitions. ### Definition of `C^n` functions in domains One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this is what we do with `iteratedFDerivWithin`) and requiring that all these derivatives up to `n` are continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n` functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`. This definition still has the problem that a function which is locally `C^n` would not need to be `C^n`, as different choices of sequences of derivatives around different points might possibly not be glued together to give a globally defined sequence of derivatives. (Note that this issue can not happen over reals, thanks to partition of unity, but the behavior over a general field is not so clear, and we want a definition for general fields). Also, there are locality problems for the order parameter: one could image a function which, for each `n`, has a nice sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore not be glued to give rise to an infinite sequence of derivatives. This would give a function which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions in space and order in our definition of `ContDiffWithinAt` and `ContDiffOn`. The resulting definition is slightly more complicated to work with (in fact not so much), but it gives rise to completely satisfactory theorems. For instance, with this definition, a real function which is `C^m` (but not better) on `(-1/m, 1/m)` for each natural `m` is by definition `C^∞` at `0`. There is another issue with the definition of `ContDiffWithinAt 𝕜 n f s x`. We can require the existence and good behavior of derivatives up to order `n` on a neighborhood of `x` within `s`. However, this does not imply continuity or differentiability within `s` of the function at `x` when `x` does not belong to `s`. Therefore, we require such existence and good behavior on a neighborhood of `x` within `s ∪ {x}` (which appears as `insert x s` in this file). ## 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 `ω`. To avoid ambiguities with the two tops, the theorems name use either `infty` or `omega`. These notations are scoped in `ContDiff`. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ noncomputable section open Set Fin Filter Function open scoped NNReal Topology ContDiff universe u uE uF uG uX variable {𝕜 : Type u} [NontriviallyNormedField 𝕜] {E : Type uE} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {F : Type uF} [NormedAddCommGroup F] [NormedSpace 𝕜 F] {G : Type uG} [NormedAddCommGroup G] [NormedSpace 𝕜 G] {X : Type uX} [NormedAddCommGroup X] [NormedSpace 𝕜 X] {s s₁ t u : Set E} {f f₁ : E → F} {g : F → G} {x x₀ : E} {c : F} {m n : WithTop ℕ∞} {p : E → FormalMultilinearSeries 𝕜 E F} /-! ### Smooth functions within a set around a point -/ variable (𝕜) in /-- A function is continuously differentiable up to order `n` within a set `s` at a point `x` if it admits continuous derivatives up to order `n` in a neighborhood of `x` in `s ∪ {x}`. For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may depend on the finite order we consider). For `n = ω`, we require the function to be analytic within `s` at `x`. The precise definition we give (all the derivatives should be analytic) is more involved to work around issues when the space is not complete, but it is equivalent when the space is complete. For instance, a real function which is `C^m` on `(-1/m, 1/m)` for each natural `m`, but not better, is `C^∞` at `0` within `univ`. -/ def ContDiffWithinAt (n : WithTop ℕ∞) (f : E → F) (s : Set E) (x : E) : Prop := match n with | ω => ∃ u ∈ 𝓝[insert x s] x, ∃ p : E → FormalMultilinearSeries 𝕜 E F, HasFTaylorSeriesUpToOn ω f p u ∧ ∀ i, AnalyticOn 𝕜 (fun x ↦ p x i) u | (n : ℕ∞) => ∀ m : ℕ, m ≤ n → ∃ u ∈ 𝓝[insert x s] x, ∃ p : E → FormalMultilinearSeries 𝕜 E F, HasFTaylorSeriesUpToOn m f p u lemma HasFTaylorSeriesUpToOn.analyticOn (hf : HasFTaylorSeriesUpToOn ω f p s) (h : AnalyticOn 𝕜 (fun x ↦ p x 0) s) : AnalyticOn 𝕜 f s := by have : AnalyticOn 𝕜 (fun x ↦ (continuousMultilinearCurryFin0 𝕜 E F) (p x 0)) s := (LinearIsometryEquiv.analyticOnNhd _ _ ).comp_analyticOn h (Set.mapsTo_univ _ _) exact this.congr (fun y hy ↦ (hf.zero_eq _ hy).symm) lemma ContDiffWithinAt.analyticOn (h : ContDiffWithinAt 𝕜 ω f s x) : ∃ u ∈ 𝓝[insert x s] x, AnalyticOn 𝕜 f u := by obtain ⟨u, hu, p, hp, h'p⟩ := h exact ⟨u, hu, hp.analyticOn (h'p 0)⟩ lemma ContDiffWithinAt.analyticWithinAt (h : ContDiffWithinAt 𝕜 ω f s x) : AnalyticWithinAt 𝕜 f s x := by obtain ⟨u, hu, hf⟩ := h.analyticOn have xu : x ∈ u := mem_of_mem_nhdsWithin (by simp) hu exact (hf x xu).mono_of_mem_nhdsWithin (nhdsWithin_mono _ (subset_insert _ _) hu) theorem contDiffWithinAt_omega_iff_analyticWithinAt [CompleteSpace F] : ContDiffWithinAt 𝕜 ω f s x ↔ AnalyticWithinAt 𝕜 f s x := by refine ⟨fun h ↦ h.analyticWithinAt, fun h ↦ ?_⟩ obtain ⟨u, hu, p, hp, h'p⟩ := h.exists_hasFTaylorSeriesUpToOn ω exact ⟨u, hu, p, hp.of_le le_top, fun i ↦ h'p i⟩ theorem contDiffWithinAt_nat {n : ℕ} : ContDiffWithinAt 𝕜 n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, ∃ p : E → FormalMultilinearSeries 𝕜 E F, HasFTaylorSeriesUpToOn n f p u := ⟨fun H => H n le_rfl, fun ⟨u, hu, p, hp⟩ _m hm => ⟨u, hu, p, hp.of_le (mod_cast hm)⟩⟩ /-- When `n` is either a natural number or `ω`, one can characterize the property of being `C^n` as the existence of a neighborhood on which there is a Taylor series up to order `n`, requiring in addition that its terms are analytic in the `ω` case. -/ lemma contDiffWithinAt_iff_of_ne_infty (hn : n ≠ ∞) : ContDiffWithinAt 𝕜 n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, ∃ p : E → FormalMultilinearSeries 𝕜 E F, HasFTaylorSeriesUpToOn n f p u ∧ (n = ω → ∀ i, AnalyticOn 𝕜 (fun x ↦ p x i) u) := by match n with | ω => simp [ContDiffWithinAt] | ∞ => simp at hn | (n : ℕ) => simp [contDiffWithinAt_nat] theorem ContDiffWithinAt.of_le (h : ContDiffWithinAt 𝕜 n f s x) (hmn : m ≤ n) : ContDiffWithinAt 𝕜 m f s x := by match n with | ω => match m with | ω => exact h | (m : ℕ∞) => intro k _ obtain ⟨u, hu, p, hp, -⟩ := h exact ⟨u, hu, p, hp.of_le le_top⟩ | (n : ℕ∞) => match m with | ω => simp at hmn | (m : ℕ∞) => exact fun k hk ↦ h k (le_trans hk (mod_cast hmn)) /-- In a complete space, a function which is analytic within a set at a point is also `C^ω` there. Note that the same statement for `AnalyticOn` does not require completeness, see `AnalyticOn.contDiffOn`. -/ theorem AnalyticWithinAt.contDiffWithinAt [CompleteSpace F] (h : AnalyticWithinAt 𝕜 f s x) : ContDiffWithinAt 𝕜 n f s x := (contDiffWithinAt_omega_iff_analyticWithinAt.2 h).of_le le_top theorem contDiffWithinAt_iff_forall_nat_le {n : ℕ∞} : ContDiffWithinAt 𝕜 n f s x ↔ ∀ m : ℕ, ↑m ≤ n → ContDiffWithinAt 𝕜 m f s x := ⟨fun H _ hm => H.of_le (mod_cast hm), fun H m hm => H m hm _ le_rfl⟩ theorem contDiffWithinAt_infty : ContDiffWithinAt 𝕜 ∞ f s x ↔ ∀ n : ℕ, ContDiffWithinAt 𝕜 n f s x := contDiffWithinAt_iff_forall_nat_le.trans <| by simp only [forall_prop_of_true, le_top] @[deprecated (since := "2024-11-25")] alias contDiffWithinAt_top := contDiffWithinAt_infty theorem ContDiffWithinAt.continuousWithinAt (h : ContDiffWithinAt 𝕜 n f s x) : ContinuousWithinAt f s x := by have := h.of_le (zero_le _) simp only [ContDiffWithinAt, nonpos_iff_eq_zero, Nat.cast_eq_zero, mem_pure, forall_eq, CharP.cast_eq_zero] at this rcases this with ⟨u, hu, p, H⟩ rw [mem_nhdsWithin_insert] at hu exact (H.continuousOn.continuousWithinAt hu.1).mono_of_mem_nhdsWithin hu.2 theorem ContDiffWithinAt.congr_of_eventuallyEq (h : ContDiffWithinAt 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : ContDiffWithinAt 𝕜 n f₁ s x := by match n with | ω => obtain ⟨u, hu, p, H, H'⟩ := h exact ⟨{x ∈ u | f₁ x = f x}, Filter.inter_mem hu (mem_nhdsWithin_insert.2 ⟨hx, h₁⟩), p, (H.mono (sep_subset _ _)).congr fun _ ↦ And.right, fun i ↦ (H' i).mono (sep_subset _ _)⟩ | (n : ℕ∞) => intro m hm let ⟨u, hu, p, H⟩ := h m hm exact ⟨{ x ∈ u | f₁ x = f x }, Filter.inter_mem hu (mem_nhdsWithin_insert.2 ⟨hx, h₁⟩), p, (H.mono (sep_subset _ _)).congr fun _ ↦ And.right⟩ theorem Filter.EventuallyEq.congr_contDiffWithinAt (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : ContDiffWithinAt 𝕜 n f₁ s x ↔ ContDiffWithinAt 𝕜 n f s x := ⟨fun H ↦ H.congr_of_eventuallyEq h₁.symm hx.symm, fun H ↦ H.congr_of_eventuallyEq h₁ hx⟩ theorem ContDiffWithinAt.congr_of_eventuallyEq_insert (h : ContDiffWithinAt 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[insert x s] x] f) : ContDiffWithinAt 𝕜 n f₁ s x := h.congr_of_eventuallyEq (nhdsWithin_mono x (subset_insert x s) h₁) (mem_of_mem_nhdsWithin (mem_insert x s) h₁ :) theorem Filter.EventuallyEq.congr_contDiffWithinAt_of_insert (h₁ : f₁ =ᶠ[𝓝[insert x s] x] f) : ContDiffWithinAt 𝕜 n f₁ s x ↔ ContDiffWithinAt 𝕜 n f s x := ⟨fun H ↦ H.congr_of_eventuallyEq_insert h₁.symm, fun H ↦ H.congr_of_eventuallyEq_insert h₁⟩ theorem ContDiffWithinAt.congr_of_eventuallyEq_of_mem (h : ContDiffWithinAt 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : x ∈ s) : ContDiffWithinAt 𝕜 n f₁ s x := h.congr_of_eventuallyEq h₁ <| h₁.self_of_nhdsWithin hx theorem Filter.EventuallyEq.congr_contDiffWithinAt_of_mem (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : x ∈ s): ContDiffWithinAt 𝕜 n f₁ s x ↔ ContDiffWithinAt 𝕜 n f s x := ⟨fun H ↦ H.congr_of_eventuallyEq_of_mem h₁.symm hx, fun H ↦ H.congr_of_eventuallyEq_of_mem h₁ hx⟩ theorem ContDiffWithinAt.congr (h : ContDiffWithinAt 𝕜 n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : ContDiffWithinAt 𝕜 n f₁ s x := h.congr_of_eventuallyEq (Filter.eventuallyEq_of_mem self_mem_nhdsWithin h₁) hx theorem contDiffWithinAt_congr (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : ContDiffWithinAt 𝕜 n f₁ s x ↔ ContDiffWithinAt 𝕜 n f s x := ⟨fun h' ↦ h'.congr (fun x hx ↦ (h₁ x hx).symm) hx.symm, fun h' ↦ h'.congr h₁ hx⟩ theorem ContDiffWithinAt.congr_of_mem (h : ContDiffWithinAt 𝕜 n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) : ContDiffWithinAt 𝕜 n f₁ s x := h.congr h₁ (h₁ _ hx) theorem contDiffWithinAt_congr_of_mem (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) : ContDiffWithinAt 𝕜 n f₁ s x ↔ ContDiffWithinAt 𝕜 n f s x := contDiffWithinAt_congr h₁ (h₁ x hx) theorem ContDiffWithinAt.congr_of_insert (h : ContDiffWithinAt 𝕜 n f s x) (h₁ : ∀ y ∈ insert x s, f₁ y = f y) : ContDiffWithinAt 𝕜 n f₁ s x := h.congr (fun y hy ↦ h₁ y (mem_insert_of_mem _ hy)) (h₁ x (mem_insert _ _)) theorem contDiffWithinAt_congr_of_insert (h₁ : ∀ y ∈ insert x s, f₁ y = f y) : ContDiffWithinAt 𝕜 n f₁ s x ↔ ContDiffWithinAt 𝕜 n f s x := contDiffWithinAt_congr (fun y hy ↦ h₁ y (mem_insert_of_mem _ hy)) (h₁ x (mem_insert _ _)) theorem ContDiffWithinAt.mono_of_mem_nhdsWithin (h : ContDiffWithinAt 𝕜 n f s x) {t : Set E} (hst : s ∈ 𝓝[t] x) : ContDiffWithinAt 𝕜 n f t x := by match n with | ω => obtain ⟨u, hu, p, H, H'⟩ := h exact ⟨u, nhdsWithin_le_of_mem (insert_mem_nhdsWithin_insert hst) hu, p, H, H'⟩ | (n : ℕ∞) => intro m hm rcases h m hm with ⟨u, hu, p, H⟩ exact ⟨u, nhdsWithin_le_of_mem (insert_mem_nhdsWithin_insert hst) hu, p, H⟩ @[deprecated (since := "2024-10-30")] alias ContDiffWithinAt.mono_of_mem := ContDiffWithinAt.mono_of_mem_nhdsWithin theorem ContDiffWithinAt.mono (h : ContDiffWithinAt 𝕜 n f s x) {t : Set E} (hst : t ⊆ s) : ContDiffWithinAt 𝕜 n f t x := h.mono_of_mem_nhdsWithin <| Filter.mem_of_superset self_mem_nhdsWithin hst theorem ContDiffWithinAt.congr_mono (h : ContDiffWithinAt 𝕜 n f s x) (h' : EqOn f₁ f s₁) (h₁ : s₁ ⊆ s) (hx : f₁ x = f x) : ContDiffWithinAt 𝕜 n f₁ s₁ x := (h.mono h₁).congr h' hx theorem ContDiffWithinAt.congr_set (h : ContDiffWithinAt 𝕜 n f s x) {t : Set E} (hst : s =ᶠ[𝓝 x] t) : ContDiffWithinAt 𝕜 n f t x := by rw [← nhdsWithin_eq_iff_eventuallyEq] at hst apply h.mono_of_mem_nhdsWithin <| hst ▸ self_mem_nhdsWithin @[deprecated (since := "2024-10-23")] alias ContDiffWithinAt.congr_nhds := ContDiffWithinAt.congr_set theorem contDiffWithinAt_congr_set {t : Set E} (hst : s =ᶠ[𝓝 x] t) : ContDiffWithinAt 𝕜 n f s x ↔ ContDiffWithinAt 𝕜 n f t x := ⟨fun h => h.congr_set hst, fun h => h.congr_set hst.symm⟩ @[deprecated (since := "2024-10-23")] alias contDiffWithinAt_congr_nhds := contDiffWithinAt_congr_set theorem contDiffWithinAt_inter' (h : t ∈ 𝓝[s] x) : ContDiffWithinAt 𝕜 n f (s ∩ t) x ↔ ContDiffWithinAt 𝕜 n f s x := contDiffWithinAt_congr_set (mem_nhdsWithin_iff_eventuallyEq.1 h).symm theorem contDiffWithinAt_inter (h : t ∈ 𝓝 x) : ContDiffWithinAt 𝕜 n f (s ∩ t) x ↔ ContDiffWithinAt 𝕜 n f s x := contDiffWithinAt_inter' (mem_nhdsWithin_of_mem_nhds h) theorem contDiffWithinAt_insert_self : ContDiffWithinAt 𝕜 n f (insert x s) x ↔ ContDiffWithinAt 𝕜 n f s x := by match n with | ω => simp [ContDiffWithinAt] | (n : ℕ∞) => simp_rw [ContDiffWithinAt, insert_idem] theorem contDiffWithinAt_insert {y : E} : ContDiffWithinAt 𝕜 n f (insert y s) x ↔ ContDiffWithinAt 𝕜 n f s x := by rcases eq_or_ne x y with (rfl | hx) · exact contDiffWithinAt_insert_self refine ⟨fun h ↦ h.mono (subset_insert _ _), fun h ↦ ?_⟩ apply h.mono_of_mem_nhdsWithin simp [nhdsWithin_insert_of_ne hx, self_mem_nhdsWithin] alias ⟨ContDiffWithinAt.of_insert, ContDiffWithinAt.insert'⟩ := contDiffWithinAt_insert protected theorem ContDiffWithinAt.insert (h : ContDiffWithinAt 𝕜 n f s x) : ContDiffWithinAt 𝕜 n f (insert x s) x := h.insert' theorem contDiffWithinAt_diff_singleton {y : E} : ContDiffWithinAt 𝕜 n f (s \ {y}) x ↔ ContDiffWithinAt 𝕜 n f s x := by rw [← contDiffWithinAt_insert, insert_diff_singleton, contDiffWithinAt_insert] /-- If a function is `C^n` within a set at a point, with `n ≥ 1`, then it is differentiable within this set at this point. -/ theorem ContDiffWithinAt.differentiableWithinAt' (h : ContDiffWithinAt 𝕜 n f s x) (hn : 1 ≤ n) : DifferentiableWithinAt 𝕜 f (insert x s) x := by rcases contDiffWithinAt_nat.1 (h.of_le hn) with ⟨u, hu, p, H⟩ rcases mem_nhdsWithin.1 hu with ⟨t, t_open, xt, tu⟩ rw [inter_comm] at tu exact (differentiableWithinAt_inter (IsOpen.mem_nhds t_open xt)).1 <| ((H.mono tu).differentiableOn le_rfl) x ⟨mem_insert x s, xt⟩ theorem ContDiffWithinAt.differentiableWithinAt (h : ContDiffWithinAt 𝕜 n f s x) (hn : 1 ≤ n) : DifferentiableWithinAt 𝕜 f s x := (h.differentiableWithinAt' hn).mono (subset_insert x s) /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n` (and moreover the function is analytic when `n = ω`). -/ theorem contDiffWithinAt_succ_iff_hasFDerivWithinAt (hn : n ≠ ∞) : ContDiffWithinAt 𝕜 (n + 1) f s x ↔ ∃ u ∈ 𝓝[insert x s] x, (n = ω → AnalyticOn 𝕜 f u) ∧ ∃ f' : E → E →L[𝕜] F, (∀ x ∈ u, HasFDerivWithinAt f (f' x) u x) ∧ ContDiffWithinAt 𝕜 n f' u x := by have h'n : n + 1 ≠ ∞ := by simpa using hn constructor · intro h rcases (contDiffWithinAt_iff_of_ne_infty h'n).1 h with ⟨u, hu, p, Hp, H'p⟩ refine ⟨u, hu, ?_, fun y => (continuousMultilinearCurryFin1 𝕜 E F) (p y 1), fun y hy => Hp.hasFDerivWithinAt le_add_self hy, ?_⟩ · rintro rfl exact Hp.analyticOn (H'p rfl 0) apply (contDiffWithinAt_iff_of_ne_infty hn).2 refine ⟨u, ?_, fun y : E => (p y).shift, ?_⟩ · convert @self_mem_nhdsWithin _ _ x u have : x ∈ insert x s := by simp exact insert_eq_of_mem (mem_of_mem_nhdsWithin this hu) · rw [hasFTaylorSeriesUpToOn_succ_iff_right] at Hp refine ⟨Hp.2.2, ?_⟩ rintro rfl i change AnalyticOn 𝕜 (fun x ↦ (continuousMultilinearCurryRightEquiv' 𝕜 i E F) (p x (i + 1))) u apply (LinearIsometryEquiv.analyticOnNhd _ _).comp_analyticOn ?_ (Set.mapsTo_univ _ _) exact H'p rfl _ · rintro ⟨u, hu, hf, f', f'_eq_deriv, Hf'⟩ rw [contDiffWithinAt_iff_of_ne_infty h'n] rcases (contDiffWithinAt_iff_of_ne_infty hn).1 Hf' with ⟨v, hv, p', Hp', p'_an⟩ refine ⟨v ∩ u, ?_, fun x => (p' x).unshift (f x), ?_, ?_⟩ · apply Filter.inter_mem _ hu apply nhdsWithin_le_of_mem hu exact nhdsWithin_mono _ (subset_insert x u) hv · rw [hasFTaylorSeriesUpToOn_succ_iff_right] refine ⟨fun y _ => rfl, fun y hy => ?_, ?_⟩ · change HasFDerivWithinAt (fun z => (continuousMultilinearCurryFin0 𝕜 E F).symm (f z)) (FormalMultilinearSeries.unshift (p' y) (f y) 1).curryLeft (v ∩ u) y rw [← Function.comp_def _ f, LinearIsometryEquiv.comp_hasFDerivWithinAt_iff'] convert (f'_eq_deriv y hy.2).mono inter_subset_right rw [← Hp'.zero_eq y hy.1] ext z change ((p' y 0) (init (@cons 0 (fun _ => E) z 0))) (@cons 0 (fun _ => E) z 0 (last 0)) = ((p' y 0) 0) z congr norm_num [eq_iff_true_of_subsingleton] · convert (Hp'.mono inter_subset_left).congr fun x hx => Hp'.zero_eq x hx.1 using 1 · ext x y change p' x 0 (init (@snoc 0 (fun _ : Fin 1 => E) 0 y)) y = p' x 0 0 y rw [init_snoc] · ext x k v y change p' x k (init (@snoc k (fun _ : Fin k.succ => E) v y)) (@snoc k (fun _ : Fin k.succ => E) v y (last k)) = p' x k v y rw [snoc_last, init_snoc] · intro h i simp only [WithTop.add_eq_top, WithTop.one_ne_top, or_false] at h match i with | 0 => simp only [FormalMultilinearSeries.unshift] apply AnalyticOnNhd.comp_analyticOn _ ((hf h).mono inter_subset_right) (Set.mapsTo_univ _ _) exact LinearIsometryEquiv.analyticOnNhd _ _ | i + 1 => simp only [FormalMultilinearSeries.unshift, Nat.succ_eq_add_one] apply AnalyticOnNhd.comp_analyticOn _ ((p'_an h i).mono inter_subset_left) (Set.mapsTo_univ _ _) exact LinearIsometryEquiv.analyticOnNhd _ _ /-- A version of `contDiffWithinAt_succ_iff_hasFDerivWithinAt` where all derivatives are taken within the same set. -/ theorem contDiffWithinAt_succ_iff_hasFDerivWithinAt' (hn : n ≠ ∞) : ContDiffWithinAt 𝕜 (n + 1) f s x ↔ ∃ u ∈ 𝓝[insert x s] x, u ⊆ insert x s ∧ (n = ω → AnalyticOn 𝕜 f u) ∧ ∃ f' : E → E →L[𝕜] F, (∀ x ∈ u, HasFDerivWithinAt f (f' x) s x) ∧ ContDiffWithinAt 𝕜 n f' s x := by refine ⟨fun hf => ?_, ?_⟩ · obtain ⟨u, hu, f_an, f', huf', hf'⟩ := (contDiffWithinAt_succ_iff_hasFDerivWithinAt hn).mp hf obtain ⟨w, hw, hxw, hwu⟩ := mem_nhdsWithin.mp hu rw [inter_comm] at hwu refine ⟨insert x s ∩ w, inter_mem_nhdsWithin _ (hw.mem_nhds hxw), inter_subset_left, ?_, f', fun y hy => ?_, ?_⟩ · intro h apply (f_an h).mono hwu · refine ((huf' y <| hwu hy).mono hwu).mono_of_mem_nhdsWithin ?_ refine mem_of_superset ?_ (inter_subset_inter_left _ (subset_insert _ _)) exact inter_mem_nhdsWithin _ (hw.mem_nhds hy.2) · exact hf'.mono_of_mem_nhdsWithin (nhdsWithin_mono _ (subset_insert _ _) hu) · rw [← contDiffWithinAt_insert, contDiffWithinAt_succ_iff_hasFDerivWithinAt hn, insert_eq_of_mem (mem_insert _ _)] rintro ⟨u, hu, hus, f_an, f', huf', hf'⟩ exact ⟨u, hu, f_an, f', fun y hy => (huf' y hy).insert'.mono hus, hf'.insert.mono hus⟩ /-! ### Smooth functions within a set -/ variable (𝕜) in /-- A function is continuously differentiable up to `n` on `s` if, for any point `x` in `s`, it admits continuous derivatives up to order `n` on a neighborhood of `x` in `s`. For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may depend on the finite order we consider). -/ def ContDiffOn (n : WithTop ℕ∞) (f : E → F) (s : Set E) : Prop := ∀ x ∈ s, ContDiffWithinAt 𝕜 n f s x theorem HasFTaylorSeriesUpToOn.contDiffOn {n : ℕ∞} {f' : E → FormalMultilinearSeries 𝕜 E F} (hf : HasFTaylorSeriesUpToOn n f f' s) : ContDiffOn 𝕜 n f s := by intro x hx m hm use s simp only [Set.insert_eq_of_mem hx, self_mem_nhdsWithin, true_and] exact ⟨f', hf.of_le (mod_cast hm)⟩ theorem ContDiffOn.contDiffWithinAt (h : ContDiffOn 𝕜 n f s) (hx : x ∈ s) : ContDiffWithinAt 𝕜 n f s x := h x hx theorem ContDiffOn.of_le (h : ContDiffOn 𝕜 n f s) (hmn : m ≤ n) : ContDiffOn 𝕜 m f s := fun x hx => (h x hx).of_le hmn theorem ContDiffWithinAt.contDiffOn' (hm : m ≤ n) (h' : m = ∞ → n = ω) (h : ContDiffWithinAt 𝕜 n f s x) : ∃ u, IsOpen u ∧ x ∈ u ∧ ContDiffOn 𝕜 m f (insert x s ∩ u) := by rcases eq_or_ne n ω with rfl | hn · obtain ⟨t, ht, p, hp, h'p⟩ := h rcases mem_nhdsWithin.1 ht with ⟨u, huo, hxu, hut⟩ rw [inter_comm] at hut refine ⟨u, huo, hxu, ?_⟩ suffices ContDiffOn 𝕜 ω f (insert x s ∩ u) from this.of_le le_top intro y hy refine ⟨insert x s ∩ u, ?_, p, hp.mono hut, fun i ↦ (h'p i).mono hut⟩ simp only [insert_eq_of_mem, hy, self_mem_nhdsWithin] · match m with | ω => simp [hn] at hm | ∞ => exact (hn (h' rfl)).elim | (m : ℕ) => rcases contDiffWithinAt_nat.1 (h.of_le hm) with ⟨t, ht, p, hp⟩ rcases mem_nhdsWithin.1 ht with ⟨u, huo, hxu, hut⟩ rw [inter_comm] at hut exact ⟨u, huo, hxu, (hp.mono hut).contDiffOn⟩ theorem ContDiffWithinAt.contDiffOn (hm : m ≤ n) (h' : m = ∞ → n = ω) (h : ContDiffWithinAt 𝕜 n f s x) : ∃ u ∈ 𝓝[insert x s] x, u ⊆ insert x s ∧ ContDiffOn 𝕜 m f u := by obtain ⟨_u, uo, xu, h⟩ := h.contDiffOn' hm h' exact ⟨_, inter_mem_nhdsWithin _ (uo.mem_nhds xu), inter_subset_left, h⟩ theorem ContDiffOn.analyticOn (h : ContDiffOn 𝕜 ω f s) : AnalyticOn 𝕜 f s := fun x hx ↦ (h x hx).analyticWithinAt /-- A function is `C^n` within a set at a point, for `n : ℕ`, if and only if it is `C^n` on a neighborhood of this point. -/ theorem contDiffWithinAt_iff_contDiffOn_nhds (hn : n ≠ ∞) : ContDiffWithinAt 𝕜 n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, ContDiffOn 𝕜 n f u := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · rcases h.contDiffOn le_rfl (by simp [hn]) with ⟨u, hu, h'u⟩ exact ⟨u, hu, h'u.2⟩ · rcases h with ⟨u, u_mem, hu⟩ have : x ∈ u := mem_of_mem_nhdsWithin (mem_insert x s) u_mem exact (hu x this).mono_of_mem_nhdsWithin (nhdsWithin_mono _ (subset_insert x s) u_mem) protected theorem ContDiffWithinAt.eventually (h : ContDiffWithinAt 𝕜 n f s x) (hn : n ≠ ∞) : ∀ᶠ y in 𝓝[insert x s] x, ContDiffWithinAt 𝕜 n f s y := by rcases h.contDiffOn le_rfl (by simp [hn]) with ⟨u, hu, _, hd⟩ have : ∀ᶠ y : E in 𝓝[insert x s] x, u ∈ 𝓝[insert x s] y ∧ y ∈ u := (eventually_eventually_nhdsWithin.2 hu).and hu refine this.mono fun y hy => (hd y hy.2).mono_of_mem_nhdsWithin ?_ exact nhdsWithin_mono y (subset_insert _ _) hy.1 theorem ContDiffOn.of_succ (h : ContDiffOn 𝕜 (n + 1) f s) : ContDiffOn 𝕜 n f s := h.of_le le_self_add theorem ContDiffOn.one_of_succ (h : ContDiffOn 𝕜 (n + 1) f s) : ContDiffOn 𝕜 1 f s := h.of_le le_add_self theorem contDiffOn_iff_forall_nat_le {n : ℕ∞} : ContDiffOn 𝕜 n f s ↔ ∀ m : ℕ, ↑m ≤ n → ContDiffOn 𝕜 m f s := ⟨fun H _ hm => H.of_le (mod_cast hm), fun H x hx m hm => H m hm x hx m le_rfl⟩ theorem contDiffOn_infty : ContDiffOn 𝕜 ∞ f s ↔ ∀ n : ℕ, ContDiffOn 𝕜 n f s := contDiffOn_iff_forall_nat_le.trans <| by simp only [le_top, forall_prop_of_true] @[deprecated (since := "2024-11-27")] alias contDiffOn_top := contDiffOn_infty @[deprecated (since := "2024-11-27")] alias contDiffOn_infty_iff_contDiffOn_omega := contDiffOn_infty theorem contDiffOn_all_iff_nat : (∀ (n : ℕ∞), ContDiffOn 𝕜 n f s) ↔ ∀ n : ℕ, ContDiffOn 𝕜 n f s := by refine ⟨fun H n => H n, ?_⟩ rintro H (_ | n) exacts [contDiffOn_infty.2 H, H n] theorem ContDiffOn.continuousOn (h : ContDiffOn 𝕜 n f s) : ContinuousOn f s := fun x hx => (h x hx).continuousWithinAt theorem ContDiffOn.congr (h : ContDiffOn 𝕜 n f s) (h₁ : ∀ x ∈ s, f₁ x = f x) : ContDiffOn 𝕜 n f₁ s := fun x hx => (h x hx).congr h₁ (h₁ x hx) theorem contDiffOn_congr (h₁ : ∀ x ∈ s, f₁ x = f x) : ContDiffOn 𝕜 n f₁ s ↔ ContDiffOn 𝕜 n f s := ⟨fun H => H.congr fun x hx => (h₁ x hx).symm, fun H => H.congr h₁⟩ theorem ContDiffOn.mono (h : ContDiffOn 𝕜 n f s) {t : Set E} (hst : t ⊆ s) : ContDiffOn 𝕜 n f t := fun x hx => (h x (hst hx)).mono hst theorem ContDiffOn.congr_mono (hf : ContDiffOn 𝕜 n f s) (h₁ : ∀ x ∈ s₁, f₁ x = f x) (hs : s₁ ⊆ s) : ContDiffOn 𝕜 n f₁ s₁ := (hf.mono hs).congr h₁ /-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/ theorem ContDiffOn.differentiableOn (h : ContDiffOn 𝕜 n f s) (hn : 1 ≤ n) : DifferentiableOn 𝕜 f s := fun x hx => (h x hx).differentiableWithinAt hn /-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/ theorem contDiffOn_of_locally_contDiffOn (h : ∀ x ∈ s, ∃ u, IsOpen u ∧ x ∈ u ∧ ContDiffOn 𝕜 n f (s ∩ u)) : ContDiffOn 𝕜 n f s := by intro x xs rcases h x xs with ⟨u, u_open, xu, hu⟩ apply (contDiffWithinAt_inter _).1 (hu x ⟨xs, xu⟩) exact IsOpen.mem_nhds u_open xu /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem contDiffOn_succ_iff_hasFDerivWithinAt (hn : n ≠ ∞) : ContDiffOn 𝕜 (n + 1) f s ↔ ∀ x ∈ s, ∃ u ∈ 𝓝[insert x s] x, (n = ω → AnalyticOn 𝕜 f u) ∧ ∃ f' : E → E →L[𝕜] F, (∀ x ∈ u, HasFDerivWithinAt f (f' x) u x) ∧ ContDiffOn 𝕜 n f' u := by constructor · intro h x hx rcases (contDiffWithinAt_succ_iff_hasFDerivWithinAt hn).1 (h x hx) with ⟨u, hu, f_an, f', hf', Hf'⟩ rcases Hf'.contDiffOn le_rfl (by simp [hn]) with ⟨v, vu, v'u, hv⟩ rw [insert_eq_of_mem hx] at hu ⊢ have xu : x ∈ u := mem_of_mem_nhdsWithin hx hu rw [insert_eq_of_mem xu] at vu v'u exact ⟨v, nhdsWithin_le_of_mem hu vu, fun h ↦ (f_an h).mono v'u, f', fun y hy ↦ (hf' y (v'u hy)).mono v'u, hv⟩ · intro h x hx rw [contDiffWithinAt_succ_iff_hasFDerivWithinAt hn] rcases h x hx with ⟨u, u_nhbd, f_an, f', hu, hf'⟩ have : x ∈ u := mem_of_mem_nhdsWithin (mem_insert _ _) u_nhbd exact ⟨u, u_nhbd, f_an, f', hu, hf' x this⟩ /-! ### Iterated derivative within a set -/ @[simp] theorem contDiffOn_zero : ContDiffOn 𝕜 0 f s ↔ ContinuousOn f s := by refine ⟨fun H => H.continuousOn, fun H => fun x hx m hm ↦ ?_⟩ have : (m : WithTop ℕ∞) = 0 := le_antisymm (mod_cast hm) bot_le rw [this] refine ⟨insert x s, self_mem_nhdsWithin, ftaylorSeriesWithin 𝕜 f s, ?_⟩ rw [hasFTaylorSeriesUpToOn_zero_iff] exact ⟨by rwa [insert_eq_of_mem hx], fun x _ => by simp [ftaylorSeriesWithin]⟩ theorem contDiffWithinAt_zero (hx : x ∈ s) : ContDiffWithinAt 𝕜 0 f s x ↔ ∃ u ∈ 𝓝[s] x, ContinuousOn f (s ∩ u) := by constructor · intro h obtain ⟨u, H, p, hp⟩ := h 0 le_rfl refine ⟨u, ?_, ?_⟩ · simpa [hx] using H · simp only [Nat.cast_zero, hasFTaylorSeriesUpToOn_zero_iff] at hp exact hp.1.mono inter_subset_right · rintro ⟨u, H, hu⟩ rw [← contDiffWithinAt_inter' H] have h' : x ∈ s ∩ u := ⟨hx, mem_of_mem_nhdsWithin hx H⟩ exact (contDiffOn_zero.mpr hu).contDiffWithinAt h' /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylorSeriesWithin 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ protected theorem ContDiffOn.ftaylorSeriesWithin (h : ContDiffOn 𝕜 n f s) (hs : UniqueDiffOn 𝕜 s) : HasFTaylorSeriesUpToOn n f (ftaylorSeriesWithin 𝕜 f s) s := by constructor · intro x _ simp only [ftaylorSeriesWithin, ContinuousMultilinearMap.curry0_apply, iteratedFDerivWithin_zero_apply] · intro m hm x hx have : (m + 1 : ℕ) ≤ n := ENat.add_one_natCast_le_withTop_of_lt hm rcases (h x hx).of_le this _ le_rfl with ⟨u, hu, p, Hp⟩ rw [insert_eq_of_mem hx] at hu rcases mem_nhdsWithin.1 hu with ⟨o, o_open, xo, ho⟩ rw [inter_comm] at ho have : p x m.succ = ftaylorSeriesWithin 𝕜 f s x m.succ := by change p x m.succ = iteratedFDerivWithin 𝕜 m.succ f s x rw [← iteratedFDerivWithin_inter_open o_open xo] exact (Hp.mono ho).eq_iteratedFDerivWithin_of_uniqueDiffOn le_rfl (hs.inter o_open) ⟨hx, xo⟩ rw [← this, ← hasFDerivWithinAt_inter (IsOpen.mem_nhds o_open xo)] have A : ∀ y ∈ s ∩ o, p y m = ftaylorSeriesWithin 𝕜 f s y m := by rintro y ⟨hy, yo⟩ change p y m = iteratedFDerivWithin 𝕜 m f s y rw [← iteratedFDerivWithin_inter_open o_open yo] exact (Hp.mono ho).eq_iteratedFDerivWithin_of_uniqueDiffOn (mod_cast Nat.le_succ m) (hs.inter o_open) ⟨hy, yo⟩ exact ((Hp.mono ho).fderivWithin m (mod_cast lt_add_one m) x ⟨hx, xo⟩).congr (fun y hy => (A y hy).symm) (A x ⟨hx, xo⟩).symm · intro m hm apply continuousOn_of_locally_continuousOn intro x hx rcases (h x hx).of_le hm _ le_rfl with ⟨u, hu, p, Hp⟩ rcases mem_nhdsWithin.1 hu with ⟨o, o_open, xo, ho⟩ rw [insert_eq_of_mem hx] at ho rw [inter_comm] at ho refine ⟨o, o_open, xo, ?_⟩ have A : ∀ y ∈ s ∩ o, p y m = ftaylorSeriesWithin 𝕜 f s y m := by rintro y ⟨hy, yo⟩ change p y m = iteratedFDerivWithin 𝕜 m f s y rw [← iteratedFDerivWithin_inter_open o_open yo] exact (Hp.mono ho).eq_iteratedFDerivWithin_of_uniqueDiffOn le_rfl (hs.inter o_open) ⟨hy, yo⟩ exact ((Hp.mono ho).cont m le_rfl).congr fun y hy => (A y hy).symm theorem iteratedFDerivWithin_subset {n : ℕ} (st : s ⊆ t) (hs : UniqueDiffOn 𝕜 s) (ht : UniqueDiffOn 𝕜 t) (h : ContDiffOn 𝕜 n f t) (hx : x ∈ s) : iteratedFDerivWithin 𝕜 n f s x = iteratedFDerivWithin 𝕜 n f t x := (((h.ftaylorSeriesWithin ht).mono st).eq_iteratedFDerivWithin_of_uniqueDiffOn le_rfl hs hx).symm theorem ContDiffWithinAt.eventually_hasFTaylorSeriesUpToOn {f : E → F} {s : Set E} {a : E} (h : ContDiffWithinAt 𝕜 n f s a) (hs : UniqueDiffOn 𝕜 s) (ha : a ∈ s) {m : ℕ} (hm : m ≤ n) : ∀ᶠ t in (𝓝[s] a).smallSets, HasFTaylorSeriesUpToOn m f (ftaylorSeriesWithin 𝕜 f s) t := by rcases h.contDiffOn' hm (by simp) with ⟨U, hUo, haU, hfU⟩ have : ∀ᶠ t in (𝓝[s] a).smallSets, t ⊆ s ∩ U := by rw [eventually_smallSets_subset] exact inter_mem_nhdsWithin _ <| hUo.mem_nhds haU refine this.mono fun t ht ↦ .mono ?_ ht rw [insert_eq_of_mem ha] at hfU refine (hfU.ftaylorSeriesWithin (hs.inter hUo)).congr_series fun k hk x hx ↦ ?_ exact iteratedFDerivWithin_inter_open hUo hx.2 /-- On a set with unique differentiability, an analytic function is automatically `C^ω`, as its successive derivatives are also analytic. This does not require completeness of the space. See also `AnalyticOn.contDiffOn_of_completeSpace`. -/ theorem AnalyticOn.contDiffOn (h : AnalyticOn 𝕜 f s) (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 n f s := by suffices ContDiffOn 𝕜 ω f s from this.of_le le_top rcases h.exists_hasFTaylorSeriesUpToOn hs with ⟨p, hp⟩ intro x hx refine ⟨s, ?_, p, hp⟩ rw [insert_eq_of_mem hx] exact self_mem_nhdsWithin /-- On a set with unique differentiability, an analytic function is automatically `C^ω`, as its successive derivatives are also analytic. This does not require completeness of the space. See also `AnalyticOnNhd.contDiffOn_of_completeSpace`. -/ theorem AnalyticOnNhd.contDiffOn (h : AnalyticOnNhd 𝕜 f s) (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 n f s := h.analyticOn.contDiffOn hs /-- An analytic function is automatically `C^ω` in a complete space -/ theorem AnalyticOn.contDiffOn_of_completeSpace [CompleteSpace F] (h : AnalyticOn 𝕜 f s) : ContDiffOn 𝕜 n f s := fun x hx ↦ (h x hx).contDiffWithinAt /-- An analytic function is automatically `C^ω` in a complete space -/ theorem AnalyticOnNhd.contDiffOn_of_completeSpace [CompleteSpace F] (h : AnalyticOnNhd 𝕜 f s) : ContDiffOn 𝕜 n f s := h.analyticOn.contDiffOn_of_completeSpace theorem contDiffOn_of_continuousOn_differentiableOn {n : ℕ∞} (Hcont : ∀ m : ℕ, m ≤ n → ContinuousOn (fun x => iteratedFDerivWithin 𝕜 m f s x) s) (Hdiff : ∀ m : ℕ, m < n → DifferentiableOn 𝕜 (fun x => iteratedFDerivWithin 𝕜 m f s x) s) : ContDiffOn 𝕜 n f s := by intro x hx m hm rw [insert_eq_of_mem hx] refine ⟨s, self_mem_nhdsWithin, ftaylorSeriesWithin 𝕜 f s, ?_⟩ constructor · intro y _ simp only [ftaylorSeriesWithin, ContinuousMultilinearMap.curry0_apply, iteratedFDerivWithin_zero_apply] · intro k hk y hy convert (Hdiff k (lt_of_lt_of_le (mod_cast hk) (mod_cast hm)) y hy).hasFDerivWithinAt · intro k hk exact Hcont k (le_trans (mod_cast hk) (mod_cast hm)) theorem contDiffOn_of_differentiableOn {n : ℕ∞} (h : ∀ m : ℕ, m ≤ n → DifferentiableOn 𝕜 (iteratedFDerivWithin 𝕜 m f s) s) : ContDiffOn 𝕜 n f s := contDiffOn_of_continuousOn_differentiableOn (fun m hm => (h m hm).continuousOn) fun m hm => h m (le_of_lt hm) theorem contDiffOn_of_analyticOn_iteratedFDerivWithin (h : ∀ m, AnalyticOn 𝕜 (iteratedFDerivWithin 𝕜 m f s) s) : ContDiffOn 𝕜 n f s := by suffices ContDiffOn 𝕜 ω f s from this.of_le le_top intro x hx refine ⟨insert x s, self_mem_nhdsWithin, ftaylorSeriesWithin 𝕜 f s, ?_, ?_⟩ · rw [insert_eq_of_mem hx] constructor · intro y _ simp only [ftaylorSeriesWithin, ContinuousMultilinearMap.curry0_apply, iteratedFDerivWithin_zero_apply] · intro k _ y hy exact ((h k).differentiableOn y hy).hasFDerivWithinAt · intro k _ exact (h k).continuousOn · intro i rw [insert_eq_of_mem hx] exact h i theorem contDiffOn_omega_iff_analyticOn (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 ω f s ↔ AnalyticOn 𝕜 f s := ⟨fun h m ↦ h.analyticOn m, fun h ↦ h.contDiffOn hs⟩ theorem ContDiffOn.continuousOn_iteratedFDerivWithin {m : ℕ} (h : ContDiffOn 𝕜 n f s) (hmn : m ≤ n) (hs : UniqueDiffOn 𝕜 s) : ContinuousOn (iteratedFDerivWithin 𝕜 m f s) s := ((h.of_le hmn).ftaylorSeriesWithin hs).cont m le_rfl theorem ContDiffOn.differentiableOn_iteratedFDerivWithin {m : ℕ} (h : ContDiffOn 𝕜 n f s) (hmn : m < n) (hs : UniqueDiffOn 𝕜 s) : DifferentiableOn 𝕜 (iteratedFDerivWithin 𝕜 m f s) s := by intro x hx have : (m + 1 : ℕ) ≤ n := ENat.add_one_natCast_le_withTop_of_lt hmn apply (((h.of_le this).ftaylorSeriesWithin hs).fderivWithin m ?_ x hx).differentiableWithinAt exact_mod_cast lt_add_one m theorem ContDiffWithinAt.differentiableWithinAt_iteratedFDerivWithin {m : ℕ} (h : ContDiffWithinAt 𝕜 n f s x) (hmn : m < n) (hs : UniqueDiffOn 𝕜 (insert x s)) : DifferentiableWithinAt 𝕜 (iteratedFDerivWithin 𝕜 m f s) s x := by have : (m + 1 : WithTop ℕ∞) ≠ ∞ := Ne.symm (ne_of_beq_false rfl) rcases h.contDiffOn' (ENat.add_one_natCast_le_withTop_of_lt hmn) (by simp [this]) with ⟨u, uo, xu, hu⟩ set t := insert x s ∩ u have A : t =ᶠ[𝓝[≠] x] s := by simp only [set_eventuallyEq_iff_inf_principal, ← nhdsWithin_inter'] rw [← inter_assoc, nhdsWithin_inter_of_mem', ← diff_eq_compl_inter, insert_diff_of_mem, diff_eq_compl_inter] exacts [rfl, mem_nhdsWithin_of_mem_nhds (uo.mem_nhds xu)] have B : iteratedFDerivWithin 𝕜 m f s =ᶠ[𝓝 x] iteratedFDerivWithin 𝕜 m f t := iteratedFDerivWithin_eventually_congr_set' _ A.symm _ have C : DifferentiableWithinAt 𝕜 (iteratedFDerivWithin 𝕜 m f t) t x := hu.differentiableOn_iteratedFDerivWithin (Nat.cast_lt.2 m.lt_succ_self) (hs.inter uo) x ⟨mem_insert _ _, xu⟩ rw [differentiableWithinAt_congr_set' _ A] at C exact C.congr_of_eventuallyEq (B.filter_mono inf_le_left) B.self_of_nhds theorem contDiffOn_iff_continuousOn_differentiableOn {n : ℕ∞} (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 n f s ↔ (∀ m : ℕ, m ≤ n → ContinuousOn (fun x => iteratedFDerivWithin 𝕜 m f s x) s) ∧ ∀ m : ℕ, m < n → DifferentiableOn 𝕜 (fun x => iteratedFDerivWithin 𝕜 m f s x) s := ⟨fun h => ⟨fun _m hm => h.continuousOn_iteratedFDerivWithin (mod_cast hm) hs, fun _m hm => h.differentiableOn_iteratedFDerivWithin (mod_cast hm) hs⟩, fun h => contDiffOn_of_continuousOn_differentiableOn h.1 h.2⟩ theorem contDiffOn_nat_iff_continuousOn_differentiableOn {n : ℕ} (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 n f s ↔ (∀ m : ℕ, m ≤ n → ContinuousOn (fun x => iteratedFDerivWithin 𝕜 m f s x) s) ∧ ∀ m : ℕ, m < n → DifferentiableOn 𝕜 (fun x => iteratedFDerivWithin 𝕜 m f s x) s := by rw [← WithTop.coe_natCast, contDiffOn_iff_continuousOn_differentiableOn hs] simp theorem contDiffOn_succ_of_fderivWithin (hf : DifferentiableOn 𝕜 f s) (h' : n = ω → AnalyticOn 𝕜 f s) (h : ContDiffOn 𝕜 n (fun y => fderivWithin 𝕜 f s y) s) : ContDiffOn 𝕜 (n + 1) f s := by rcases eq_or_ne n ∞ with rfl | hn · rw [ENat.coe_top_add_one, contDiffOn_infty] intro m x hx apply ContDiffWithinAt.of_le _ (show (m : WithTop ℕ∞) ≤ m + 1 from le_self_add) rw [contDiffWithinAt_succ_iff_hasFDerivWithinAt (by simp), insert_eq_of_mem hx] exact ⟨s, self_mem_nhdsWithin, (by simp), fderivWithin 𝕜 f s, fun y hy => (hf y hy).hasFDerivWithinAt, (h x hx).of_le (mod_cast le_top)⟩ · intro x hx rw [contDiffWithinAt_succ_iff_hasFDerivWithinAt hn, insert_eq_of_mem hx] exact ⟨s, self_mem_nhdsWithin, h', fderivWithin 𝕜 f s, fun y hy => (hf y hy).hasFDerivWithinAt, h x hx⟩ theorem contDiffOn_of_analyticOn_of_fderivWithin (hf : AnalyticOn 𝕜 f s) (h : ContDiffOn 𝕜 ω (fun y ↦ fderivWithin 𝕜 f s y) s) : ContDiffOn 𝕜 n f s := by suffices ContDiffOn 𝕜 (ω + 1) f s from this.of_le le_top exact contDiffOn_succ_of_fderivWithin hf.differentiableOn (fun _ ↦ hf) h /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (expressed with `fderivWithin`) is `C^n`. -/ theorem contDiffOn_succ_iff_fderivWithin (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 (n + 1) f s ↔ DifferentiableOn 𝕜 f s ∧ (n = ω → AnalyticOn 𝕜 f s) ∧ ContDiffOn 𝕜 n (fderivWithin 𝕜 f s) s := by refine ⟨fun H => ?_, fun h => contDiffOn_succ_of_fderivWithin h.1 h.2.1 h.2.2⟩ refine ⟨H.differentiableOn le_add_self, ?_, fun x hx => ?_⟩ · rintro rfl exact H.analyticOn have A (m : ℕ) (hm : m ≤ n) : ContDiffWithinAt 𝕜 m (fun y => fderivWithin 𝕜 f s y) s x := by rcases (contDiffWithinAt_succ_iff_hasFDerivWithinAt (n := m) (ne_of_beq_false rfl)).1 (H.of_le (add_le_add_right hm 1) x hx) with ⟨u, hu, -, f', hff', hf'⟩ rcases mem_nhdsWithin.1 hu with ⟨o, o_open, xo, ho⟩ rw [inter_comm, insert_eq_of_mem hx] at ho have := hf'.mono ho rw [contDiffWithinAt_inter' (mem_nhdsWithin_of_mem_nhds (IsOpen.mem_nhds o_open xo))] at this apply this.congr_of_eventuallyEq_of_mem _ hx have : o ∩ s ∈ 𝓝[s] x := mem_nhdsWithin.2 ⟨o, o_open, xo, Subset.refl _⟩ rw [inter_comm] at this refine Filter.eventuallyEq_of_mem this fun y hy => ?_ have A : fderivWithin 𝕜 f (s ∩ o) y = f' y := ((hff' y (ho hy)).mono ho).fderivWithin (hs.inter o_open y hy) rwa [fderivWithin_inter (o_open.mem_nhds hy.2)] at A match n with | ω => exact (H.analyticOn.fderivWithin hs).contDiffOn hs (n := ω) x hx | ∞ => exact contDiffWithinAt_infty.2 (fun m ↦ A m (mod_cast le_top)) | (n : ℕ) => exact A n le_rfl theorem contDiffOn_succ_iff_hasFDerivWithinAt_of_uniqueDiffOn (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 (n + 1) f s ↔ (n = ω → AnalyticOn 𝕜 f s) ∧ ∃ f' : E → E →L[𝕜] F, ContDiffOn 𝕜 n f' s ∧ ∀ x, x ∈ s → HasFDerivWithinAt f (f' x) s x := by rw [contDiffOn_succ_iff_fderivWithin hs] refine ⟨fun h => ⟨h.2.1, fderivWithin 𝕜 f s, h.2.2, fun x hx => (h.1 x hx).hasFDerivWithinAt⟩, fun ⟨f_an, h⟩ => ?_⟩ rcases h with ⟨f', h1, h2⟩ refine ⟨fun x hx => (h2 x hx).differentiableWithinAt, f_an, fun x hx => ?_⟩ exact (h1 x hx).congr_of_mem (fun y hy => (h2 y hy).fderivWithin (hs y hy)) hx @[deprecated (since := "2024-11-27")] alias contDiffOn_succ_iff_hasFDerivWithin := contDiffOn_succ_iff_hasFDerivWithinAt_of_uniqueDiffOn theorem contDiffOn_infty_iff_fderivWithin (hs : UniqueDiffOn 𝕜 s) : ContDiffOn 𝕜 ∞ f s ↔ DifferentiableOn 𝕜 f s ∧ ContDiffOn 𝕜 ∞ (fderivWithin 𝕜 f s) s := by rw [← ENat.coe_top_add_one, contDiffOn_succ_iff_fderivWithin hs] simp @[deprecated (since := "2024-11-27")] alias contDiffOn_top_iff_fderivWithin := contDiffOn_infty_iff_fderivWithin /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (expressed with `fderiv`) is `C^n`. -/ theorem contDiffOn_succ_iff_fderiv_of_isOpen (hs : IsOpen s) : ContDiffOn 𝕜 (n + 1) f s ↔ DifferentiableOn 𝕜 f s ∧ (n = ω → AnalyticOn 𝕜 f s) ∧ ContDiffOn 𝕜 n (fderiv 𝕜 f) s := by rw [contDiffOn_succ_iff_fderivWithin hs.uniqueDiffOn, contDiffOn_congr fun x hx ↦ fderivWithin_of_isOpen hs hx] theorem contDiffOn_infty_iff_fderiv_of_isOpen (hs : IsOpen s) : ContDiffOn 𝕜 ∞ f s ↔ DifferentiableOn 𝕜 f s ∧ ContDiffOn 𝕜 ∞ (fderiv 𝕜 f) s := by rw [← ENat.coe_top_add_one, contDiffOn_succ_iff_fderiv_of_isOpen hs] simp @[deprecated (since := "2024-11-27")] alias contDiffOn_top_iff_fderiv_of_isOpen := contDiffOn_infty_iff_fderiv_of_isOpen protected theorem ContDiffOn.fderivWithin (hf : ContDiffOn 𝕜 n f s) (hs : UniqueDiffOn 𝕜 s) (hmn : m + 1 ≤ n) : ContDiffOn 𝕜 m (fderivWithin 𝕜 f s) s := ((contDiffOn_succ_iff_fderivWithin hs).1 (hf.of_le hmn)).2.2 theorem ContDiffOn.fderiv_of_isOpen (hf : ContDiffOn 𝕜 n f s) (hs : IsOpen s) (hmn : m + 1 ≤ n) : ContDiffOn 𝕜 m (fderiv 𝕜 f) s := (hf.fderivWithin hs.uniqueDiffOn hmn).congr fun _ hx => (fderivWithin_of_isOpen hs hx).symm theorem ContDiffOn.continuousOn_fderivWithin (h : ContDiffOn 𝕜 n f s) (hs : UniqueDiffOn 𝕜 s) (hn : 1 ≤ n) : ContinuousOn (fderivWithin 𝕜 f s) s := ((contDiffOn_succ_iff_fderivWithin hs).1 (h.of_le (show 0 + (1 : WithTop ℕ∞) ≤ n from hn))).2.2.continuousOn theorem ContDiffOn.continuousOn_fderiv_of_isOpen (h : ContDiffOn 𝕜 n f s) (hs : IsOpen s) (hn : 1 ≤ n) : ContinuousOn (fderiv 𝕜 f) s := ((contDiffOn_succ_iff_fderiv_of_isOpen hs).1 (h.of_le (show 0 + (1 : WithTop ℕ∞) ≤ n from hn))).2.2.continuousOn /-! ### Smooth functions at a point -/ variable (𝕜) in /-- A function is continuously differentiable up to `n` at a point `x` if, for any integer `k ≤ n`, there is a neighborhood of `x` where `f` admits derivatives up to order `n`, which are continuous. -/ def ContDiffAt (n : WithTop ℕ∞) (f : E → F) (x : E) : Prop := ContDiffWithinAt 𝕜 n f univ x theorem contDiffWithinAt_univ : ContDiffWithinAt 𝕜 n f univ x ↔ ContDiffAt 𝕜 n f x := Iff.rfl theorem contDiffAt_infty : ContDiffAt 𝕜 ∞ f x ↔ ∀ n : ℕ, ContDiffAt 𝕜 n f x := by simp [← contDiffWithinAt_univ, contDiffWithinAt_infty] @[deprecated (since := "2024-11-27")] alias contDiffAt_top := contDiffAt_infty theorem ContDiffAt.contDiffWithinAt (h : ContDiffAt 𝕜 n f x) : ContDiffWithinAt 𝕜 n f s x := h.mono (subset_univ _) theorem ContDiffWithinAt.contDiffAt (h : ContDiffWithinAt 𝕜 n f s x) (hx : s ∈ 𝓝 x) : ContDiffAt 𝕜 n f x := by rwa [ContDiffAt, ← contDiffWithinAt_inter hx, univ_inter] theorem contDiffWithinAt_iff_contDiffAt (h : s ∈ 𝓝 x) : ContDiffWithinAt 𝕜 n f s x ↔ ContDiffAt 𝕜 n f x := by rw [← univ_inter s, contDiffWithinAt_inter h, contDiffWithinAt_univ] theorem IsOpen.contDiffOn_iff (hs : IsOpen s) : ContDiffOn 𝕜 n f s ↔ ∀ ⦃a⦄, a ∈ s → ContDiffAt 𝕜 n f a := forall₂_congr fun _ => contDiffWithinAt_iff_contDiffAt ∘ hs.mem_nhds theorem ContDiffOn.contDiffAt (h : ContDiffOn 𝕜 n f s) (hx : s ∈ 𝓝 x) : ContDiffAt 𝕜 n f x := (h _ (mem_of_mem_nhds hx)).contDiffAt hx theorem ContDiffAt.congr_of_eventuallyEq (h : ContDiffAt 𝕜 n f x) (hg : f₁ =ᶠ[𝓝 x] f) : ContDiffAt 𝕜 n f₁ x := h.congr_of_eventuallyEq_of_mem (by rwa [nhdsWithin_univ]) (mem_univ x) theorem ContDiffAt.of_le (h : ContDiffAt 𝕜 n f x) (hmn : m ≤ n) : ContDiffAt 𝕜 m f x := ContDiffWithinAt.of_le h hmn theorem ContDiffAt.continuousAt (h : ContDiffAt 𝕜 n f x) : ContinuousAt f x := by simpa [continuousWithinAt_univ] using h.continuousWithinAt theorem ContDiffAt.analyticAt (h : ContDiffAt 𝕜 ω f x) : AnalyticAt 𝕜 f x := by rw [← contDiffWithinAt_univ] at h rw [← analyticWithinAt_univ] exact h.analyticWithinAt /-- In a complete space, a function which is analytic at a point is also `C^ω` there. Note that the same statement for `AnalyticOn` does not require completeness, see `AnalyticOn.contDiffOn`. -/ theorem AnalyticAt.contDiffAt [CompleteSpace F] (h : AnalyticAt 𝕜 f x) : ContDiffAt 𝕜 n f x := by rw [← contDiffWithinAt_univ] rw [← analyticWithinAt_univ] at h exact h.contDiffWithinAt @[simp] theorem contDiffWithinAt_compl_self : ContDiffWithinAt 𝕜 n f {x}ᶜ x ↔ ContDiffAt 𝕜 n f x := by rw [compl_eq_univ_diff, contDiffWithinAt_diff_singleton, contDiffWithinAt_univ] /-- If a function is `C^n` with `n ≥ 1` at a point, then it is differentiable there. -/ theorem ContDiffAt.differentiableAt (h : ContDiffAt 𝕜 n f x) (hn : 1 ≤ n) : DifferentiableAt 𝕜 f x := by simpa [hn, differentiableWithinAt_univ] using h.differentiableWithinAt nonrec lemma ContDiffAt.contDiffOn (h : ContDiffAt 𝕜 n f x) (hm : m ≤ n) (h' : m = ∞ → n = ω): ∃ u ∈ 𝓝 x, ContDiffOn 𝕜 m f u := by simpa [nhdsWithin_univ] using h.contDiffOn hm h' /-- A function is `C^(n + 1)` at a point iff locally, it has a derivative which is `C^n`. -/ theorem contDiffAt_succ_iff_hasFDerivAt {n : ℕ} : ContDiffAt 𝕜 (n + 1) f x ↔ ∃ f' : E → E →L[𝕜] F, (∃ u ∈ 𝓝 x, ∀ x ∈ u, HasFDerivAt f (f' x) x) ∧ ContDiffAt 𝕜 n f' x := by rw [← contDiffWithinAt_univ, contDiffWithinAt_succ_iff_hasFDerivWithinAt (by simp)] simp only [nhdsWithin_univ, exists_prop, mem_univ, insert_eq_of_mem] constructor · rintro ⟨u, H, -, f', h_fderiv, h_cont_diff⟩ rcases mem_nhds_iff.mp H with ⟨t, htu, ht, hxt⟩ refine ⟨f', ⟨t, ?_⟩, h_cont_diff.contDiffAt H⟩ refine ⟨mem_nhds_iff.mpr ⟨t, Subset.rfl, ht, hxt⟩, ?_⟩ intro y hyt refine (h_fderiv y (htu hyt)).hasFDerivAt ?_ exact mem_nhds_iff.mpr ⟨t, htu, ht, hyt⟩ · rintro ⟨f', ⟨u, H, h_fderiv⟩, h_cont_diff⟩ refine ⟨u, H, by simp, f', fun x hxu ↦ ?_, h_cont_diff.contDiffWithinAt⟩ exact (h_fderiv x hxu).hasFDerivWithinAt protected theorem ContDiffAt.eventually (h : ContDiffAt 𝕜 n f x) (h' : n ≠ ∞) : ∀ᶠ y in 𝓝 x, ContDiffAt 𝕜 n f y := by simpa [nhdsWithin_univ] using ContDiffWithinAt.eventually h h' theorem iteratedFDerivWithin_eq_iteratedFDeriv {n : ℕ} (hs : UniqueDiffOn 𝕜 s) (h : ContDiffAt 𝕜 n f x) (hx : x ∈ s) : iteratedFDerivWithin 𝕜 n f s x = iteratedFDeriv 𝕜 n f x := by rw [← iteratedFDerivWithin_univ] rcases h.contDiffOn' le_rfl (by simp) with ⟨u, u_open, xu, hu⟩ rw [← iteratedFDerivWithin_inter_open u_open xu, ← iteratedFDerivWithin_inter_open u_open xu (s := univ)] apply iteratedFDerivWithin_subset · exact inter_subset_inter_left _ (subset_univ _) · exact hs.inter u_open · apply uniqueDiffOn_univ.inter u_open · simpa using hu · exact ⟨hx, xu⟩ /-! ### Smooth functions -/ variable (𝕜) in /-- A function is continuously differentiable up to `n` if it admits derivatives up to order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives might not be unique) we do not need to localize the definition in space or time. -/ def ContDiff (n : WithTop ℕ∞) (f : E → F) : Prop := match n with | ω => ∃ p : E → FormalMultilinearSeries 𝕜 E F, HasFTaylorSeriesUpTo ⊤ f p ∧ ∀ i, AnalyticOnNhd 𝕜 (fun x ↦ p x i) univ | (n : ℕ∞) => ∃ p : E → FormalMultilinearSeries 𝕜 E F, HasFTaylorSeriesUpTo n f p /-- If `f` has a Taylor series up to `n`, then it is `C^n`. -/ theorem HasFTaylorSeriesUpTo.contDiff {n : ℕ∞} {f' : E → FormalMultilinearSeries 𝕜 E F} (hf : HasFTaylorSeriesUpTo n f f') : ContDiff 𝕜 n f := ⟨f', hf⟩ theorem contDiffOn_univ : ContDiffOn 𝕜 n f univ ↔ ContDiff 𝕜 n f := by match n with | ω => constructor · intro H use ftaylorSeriesWithin 𝕜 f univ rw [← hasFTaylorSeriesUpToOn_univ_iff] refine ⟨H.ftaylorSeriesWithin uniqueDiffOn_univ, fun i ↦ ?_⟩ rw [← analyticOn_univ] exact H.analyticOn.iteratedFDerivWithin uniqueDiffOn_univ _ · rintro ⟨p, hp, h'p⟩ x _ exact ⟨univ, Filter.univ_sets _, p, (hp.hasFTaylorSeriesUpToOn univ).of_le le_top, fun i ↦ (h'p i).analyticOn⟩ | (n : ℕ∞) => constructor · intro H use ftaylorSeriesWithin 𝕜 f univ rw [← hasFTaylorSeriesUpToOn_univ_iff] exact H.ftaylorSeriesWithin uniqueDiffOn_univ · rintro ⟨p, hp⟩ x _ m hm exact ⟨univ, Filter.univ_sets _, p, (hp.hasFTaylorSeriesUpToOn univ).of_le (mod_cast hm)⟩ theorem contDiff_iff_contDiffAt : ContDiff 𝕜 n f ↔ ∀ x, ContDiffAt 𝕜 n f x := by simp [← contDiffOn_univ, ContDiffOn, ContDiffAt] theorem ContDiff.contDiffAt (h : ContDiff 𝕜 n f) : ContDiffAt 𝕜 n f x := contDiff_iff_contDiffAt.1 h x theorem ContDiff.contDiffWithinAt (h : ContDiff 𝕜 n f) : ContDiffWithinAt 𝕜 n f s x := h.contDiffAt.contDiffWithinAt theorem contDiff_infty : ContDiff 𝕜 ∞ f ↔ ∀ n : ℕ, ContDiff 𝕜 n f := by simp [contDiffOn_univ.symm, contDiffOn_infty] @[deprecated (since := "2024-11-25")] alias contDiff_top := contDiff_infty @[deprecated (since := "2024-11-25")] alias contDiff_infty_iff_contDiff_omega := contDiff_infty theorem contDiff_all_iff_nat : (∀ n : ℕ∞, ContDiff 𝕜 n f) ↔ ∀ n : ℕ, ContDiff 𝕜 n f := by simp only [← contDiffOn_univ, contDiffOn_all_iff_nat] theorem ContDiff.contDiffOn (h : ContDiff 𝕜 n f) : ContDiffOn 𝕜 n f s := (contDiffOn_univ.2 h).mono (subset_univ _) @[simp] theorem contDiff_zero : ContDiff 𝕜 0 f ↔ Continuous f := by rw [← contDiffOn_univ, continuous_iff_continuousOn_univ] exact contDiffOn_zero theorem contDiffAt_zero : ContDiffAt 𝕜 0 f x ↔ ∃ u ∈ 𝓝 x, ContinuousOn f u := by rw [← contDiffWithinAt_univ]; simp [contDiffWithinAt_zero, nhdsWithin_univ] theorem contDiffAt_one_iff : ContDiffAt 𝕜 1 f x ↔ ∃ f' : E → E →L[𝕜] F, ∃ u ∈ 𝓝 x, ContinuousOn f' u ∧ ∀ x ∈ u, HasFDerivAt f (f' x) x := by rw [show (1 : WithTop ℕ∞) = (0 : ℕ) + 1 from rfl] simp_rw [contDiffAt_succ_iff_hasFDerivAt, show ((0 : ℕ) : WithTop ℕ∞) = 0 from rfl, contDiffAt_zero, exists_mem_and_iff antitone_bforall antitone_continuousOn, and_comm] theorem ContDiff.of_le (h : ContDiff 𝕜 n f) (hmn : m ≤ n) : ContDiff 𝕜 m f := contDiffOn_univ.1 <| (contDiffOn_univ.2 h).of_le hmn theorem ContDiff.of_succ (h : ContDiff 𝕜 (n + 1) f) : ContDiff 𝕜 n f := h.of_le le_self_add theorem ContDiff.one_of_succ (h : ContDiff 𝕜 (n + 1) f) : ContDiff 𝕜 1 f := by apply h.of_le le_add_self theorem ContDiff.continuous (h : ContDiff 𝕜 n f) : Continuous f := contDiff_zero.1 (h.of_le bot_le) /-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/ theorem ContDiff.differentiable (h : ContDiff 𝕜 n f) (hn : 1 ≤ n) : Differentiable 𝕜 f := differentiableOn_univ.1 <| (contDiffOn_univ.2 h).differentiableOn hn theorem contDiff_iff_forall_nat_le {n : ℕ∞} : ContDiff 𝕜 n f ↔ ∀ m : ℕ, ↑m ≤ n → ContDiff 𝕜 m f := by simp_rw [← contDiffOn_univ]; exact contDiffOn_iff_forall_nat_le /-- A function is `C^(n+1)` iff it has a `C^n` derivative. -/ theorem contDiff_succ_iff_hasFDerivAt {n : ℕ} : ContDiff 𝕜 (n + 1) f ↔ ∃ f' : E → E →L[𝕜] F, ContDiff 𝕜 n f' ∧ ∀ x, HasFDerivAt f (f' x) x := by simp only [← contDiffOn_univ, ← hasFDerivWithinAt_univ, Set.mem_univ, forall_true_left, contDiffOn_succ_iff_hasFDerivWithinAt_of_uniqueDiffOn uniqueDiffOn_univ, WithTop.natCast_ne_top, analyticOn_univ, false_implies, true_and] theorem contDiff_one_iff_hasFDerivAt : ContDiff 𝕜 1 f ↔ ∃ f' : E → E →L[𝕜] F, Continuous f' ∧ ∀ x, HasFDerivAt f (f' x) x := by convert contDiff_succ_iff_hasFDerivAt using 4; simp theorem AnalyticOn.contDiff (hf : AnalyticOn 𝕜 f univ) : ContDiff 𝕜 n f := by rw [← contDiffOn_univ] exact hf.contDiffOn (n := n) uniqueDiffOn_univ theorem AnalyticOnNhd.contDiff (hf : AnalyticOnNhd 𝕜 f univ) : ContDiff 𝕜 n f := hf.analyticOn.contDiff theorem ContDiff.analyticOnNhd (h : ContDiff 𝕜 ω f) : AnalyticOnNhd 𝕜 f s := by rw [← contDiffOn_univ] at h have := h.analyticOn rw [analyticOn_univ] at this exact this.mono (subset_univ _) theorem contDiff_omega_iff_analyticOnNhd : ContDiff 𝕜 ω f ↔ AnalyticOnNhd 𝕜 f univ := ⟨fun h ↦ h.analyticOnNhd, fun h ↦ h.contDiff⟩ /-! ### Iterated derivative -/ /-- When a function is `C^n`, it admits `ftaylorSeries 𝕜 f` as a Taylor series up to order `n` in `s`. -/ theorem ContDiff.ftaylorSeries (hf : ContDiff 𝕜 n f) : HasFTaylorSeriesUpTo n f (ftaylorSeries 𝕜 f) := by simp only [← contDiffOn_univ, ← hasFTaylorSeriesUpToOn_univ_iff, ← ftaylorSeriesWithin_univ] at hf ⊢ exact ContDiffOn.ftaylorSeriesWithin hf uniqueDiffOn_univ /-- For `n : ℕ∞`, a function is `C^n` iff it admits `ftaylorSeries 𝕜 f` as a Taylor series up to order `n`. -/ theorem contDiff_iff_ftaylorSeries {n : ℕ∞} : ContDiff 𝕜 n f ↔ HasFTaylorSeriesUpTo n f (ftaylorSeries 𝕜 f) := by constructor · rw [← contDiffOn_univ, ← hasFTaylorSeriesUpToOn_univ_iff, ← ftaylorSeriesWithin_univ] exact fun h ↦ ContDiffOn.ftaylorSeriesWithin h uniqueDiffOn_univ · exact fun h ↦ ⟨ftaylorSeries 𝕜 f, h⟩ theorem contDiff_iff_continuous_differentiable {n : ℕ∞} : ContDiff 𝕜 n f ↔ (∀ m : ℕ, m ≤ n → Continuous fun x => iteratedFDeriv 𝕜 m f x) ∧ ∀ m : ℕ, m < n → Differentiable 𝕜 fun x => iteratedFDeriv 𝕜 m f x := by simp [contDiffOn_univ.symm, continuous_iff_continuousOn_univ, differentiableOn_univ.symm, iteratedFDerivWithin_univ, contDiffOn_iff_continuousOn_differentiableOn uniqueDiffOn_univ] theorem contDiff_nat_iff_continuous_differentiable {n : ℕ} : ContDiff 𝕜 n f ↔ (∀ m : ℕ, m ≤ n → Continuous fun x => iteratedFDeriv 𝕜 m f x) ∧ ∀ m : ℕ, m < n → Differentiable 𝕜 fun x => iteratedFDeriv 𝕜 m f x := by rw [← WithTop.coe_natCast, contDiff_iff_continuous_differentiable] simp /-- If `f` is `C^n` then its `m`-times iterated derivative is continuous for `m ≤ n`. -/ theorem ContDiff.continuous_iteratedFDeriv {m : ℕ} (hm : m ≤ n) (hf : ContDiff 𝕜 n f) : Continuous fun x => iteratedFDeriv 𝕜 m f x := (contDiff_iff_continuous_differentiable.mp (hf.of_le hm)).1 m le_rfl /-- If `f` is `C^n` then its `m`-times iterated derivative is differentiable for `m < n`. -/ theorem ContDiff.differentiable_iteratedFDeriv {m : ℕ} (hm : m < n) (hf : ContDiff 𝕜 n f) : Differentiable 𝕜 fun x => iteratedFDeriv 𝕜 m f x := (contDiff_iff_continuous_differentiable.mp (hf.of_le (ENat.add_one_natCast_le_withTop_of_lt hm))).2 m (mod_cast lt_add_one m) theorem contDiff_of_differentiable_iteratedFDeriv {n : ℕ∞} (h : ∀ m : ℕ, m ≤ n → Differentiable 𝕜 (iteratedFDeriv 𝕜 m f)) : ContDiff 𝕜 n f := contDiff_iff_continuous_differentiable.2 ⟨fun m hm => (h m hm).continuous, fun m hm => h m (le_of_lt hm)⟩ /-- A function is `C^(n + 1)` if and only if it is differentiable, and its derivative (formulated in terms of `fderiv`) is `C^n`. -/ theorem contDiff_succ_iff_fderiv : ContDiff 𝕜 (n + 1) f ↔ Differentiable 𝕜 f ∧ (n = ω → AnalyticOnNhd 𝕜 f univ) ∧ ContDiff 𝕜 n (fderiv 𝕜 f) := by simp only [← contDiffOn_univ, ← differentiableOn_univ, ← fderivWithin_univ, contDiffOn_succ_iff_fderivWithin uniqueDiffOn_univ, analyticOn_univ] theorem contDiff_one_iff_fderiv : ContDiff 𝕜 1 f ↔ Differentiable 𝕜 f ∧ Continuous (fderiv 𝕜 f) := by rw [← zero_add 1, contDiff_succ_iff_fderiv] simp theorem contDiff_infty_iff_fderiv : ContDiff 𝕜 ∞ f ↔ Differentiable 𝕜 f ∧ ContDiff 𝕜 ∞ (fderiv 𝕜 f) := by rw [← ENat.coe_top_add_one, contDiff_succ_iff_fderiv] simp @[deprecated (since := "2024-11-27")] alias contDiff_top_iff_fderiv := contDiff_infty_iff_fderiv theorem ContDiff.continuous_fderiv (h : ContDiff 𝕜 n f) (hn : 1 ≤ n) : Continuous (fderiv 𝕜 f) := (contDiff_one_iff_fderiv.1 (h.of_le hn)).2 /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ theorem ContDiff.continuous_fderiv_apply (h : ContDiff 𝕜 n f) (hn : 1 ≤ n) : Continuous fun p : E × E => (fderiv 𝕜 f p.1 : E → F) p.2 := have A : Continuous fun q : (E →L[𝕜] F) × E => q.1 q.2 := isBoundedBilinearMap_apply.continuous have B : Continuous fun p : E × E => (fderiv 𝕜 f p.1, p.2) := ((h.continuous_fderiv hn).comp continuous_fst).prodMk continuous_snd A.comp B
Mathlib/Analysis/Calculus/ContDiff/Defs.lean
1,379
1,380
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import Mathlib.Algebra.Group.TypeTags.Basic import Mathlib.Data.Fin.VecNotation import Mathlib.Data.Finset.Piecewise import Mathlib.Order.Filter.Cofinite import Mathlib.Order.Filter.Curry import Mathlib.Topology.Constructions.SumProd import Mathlib.Topology.NhdsSet /-! # Constructions of new topological spaces from old ones This file constructs pi types, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X → Y × Z` is continuous if and only if both projections `X → Y`, `X → Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, subspace, quotient space -/ noncomputable section open Topology TopologicalSpace Set Filter Function open scoped Set.Notation universe u v u' v' variable {X : Type u} {Y : Type v} {Z W ε ζ : Type*} section Constructions instance {r : X → X → Prop} [t : TopologicalSpace X] : TopologicalSpace (Quot r) := coinduced (Quot.mk r) t instance instTopologicalSpaceQuotient {s : Setoid X} [t : TopologicalSpace X] : TopologicalSpace (Quotient s) := coinduced Quotient.mk' t instance instTopologicalSpaceSigma {ι : Type*} {X : ι → Type v} [t₂ : ∀ i, TopologicalSpace (X i)] : TopologicalSpace (Sigma X) := ⨆ i, coinduced (Sigma.mk i) (t₂ i) instance Pi.topologicalSpace {ι : Type*} {Y : ι → Type v} [t₂ : (i : ι) → TopologicalSpace (Y i)] : TopologicalSpace ((i : ι) → Y i) := ⨅ i, induced (fun f => f i) (t₂ i) instance ULift.topologicalSpace [t : TopologicalSpace X] : TopologicalSpace (ULift.{v, u} X) := t.induced ULift.down /-! ### `Additive`, `Multiplicative` The topology on those type synonyms is inherited without change. -/ section variable [TopologicalSpace X] open Additive Multiplicative instance : TopologicalSpace (Additive X) := ‹TopologicalSpace X› instance : TopologicalSpace (Multiplicative X) := ‹TopologicalSpace X› instance [DiscreteTopology X] : DiscreteTopology (Additive X) := ‹DiscreteTopology X› instance [DiscreteTopology X] : DiscreteTopology (Multiplicative X) := ‹DiscreteTopology X› theorem continuous_ofMul : Continuous (ofMul : X → Additive X) := continuous_id theorem continuous_toMul : Continuous (toMul : Additive X → X) := continuous_id theorem continuous_ofAdd : Continuous (ofAdd : X → Multiplicative X) := continuous_id theorem continuous_toAdd : Continuous (toAdd : Multiplicative X → X) := continuous_id theorem isOpenMap_ofMul : IsOpenMap (ofMul : X → Additive X) := IsOpenMap.id theorem isOpenMap_toMul : IsOpenMap (toMul : Additive X → X) := IsOpenMap.id theorem isOpenMap_ofAdd : IsOpenMap (ofAdd : X → Multiplicative X) := IsOpenMap.id theorem isOpenMap_toAdd : IsOpenMap (toAdd : Multiplicative X → X) := IsOpenMap.id theorem isClosedMap_ofMul : IsClosedMap (ofMul : X → Additive X) := IsClosedMap.id theorem isClosedMap_toMul : IsClosedMap (toMul : Additive X → X) := IsClosedMap.id theorem isClosedMap_ofAdd : IsClosedMap (ofAdd : X → Multiplicative X) := IsClosedMap.id theorem isClosedMap_toAdd : IsClosedMap (toAdd : Multiplicative X → X) := IsClosedMap.id theorem nhds_ofMul (x : X) : 𝓝 (ofMul x) = map ofMul (𝓝 x) := rfl theorem nhds_ofAdd (x : X) : 𝓝 (ofAdd x) = map ofAdd (𝓝 x) := rfl theorem nhds_toMul (x : Additive X) : 𝓝 x.toMul = map toMul (𝓝 x) := rfl theorem nhds_toAdd (x : Multiplicative X) : 𝓝 x.toAdd = map toAdd (𝓝 x) := rfl end /-! ### Order dual The topology on this type synonym is inherited without change. -/ section variable [TopologicalSpace X] open OrderDual instance OrderDual.instTopologicalSpace : TopologicalSpace Xᵒᵈ := ‹_› instance OrderDual.instDiscreteTopology [DiscreteTopology X] : DiscreteTopology Xᵒᵈ := ‹_› theorem continuous_toDual : Continuous (toDual : X → Xᵒᵈ) := continuous_id theorem continuous_ofDual : Continuous (ofDual : Xᵒᵈ → X) := continuous_id theorem isOpenMap_toDual : IsOpenMap (toDual : X → Xᵒᵈ) := IsOpenMap.id theorem isOpenMap_ofDual : IsOpenMap (ofDual : Xᵒᵈ → X) := IsOpenMap.id theorem isClosedMap_toDual : IsClosedMap (toDual : X → Xᵒᵈ) := IsClosedMap.id theorem isClosedMap_ofDual : IsClosedMap (ofDual : Xᵒᵈ → X) := IsClosedMap.id theorem nhds_toDual (x : X) : 𝓝 (toDual x) = map toDual (𝓝 x) := rfl theorem nhds_ofDual (x : X) : 𝓝 (ofDual x) = map ofDual (𝓝 x) := rfl variable [Preorder X] {x : X} instance OrderDual.instNeBotNhdsWithinIoi [(𝓝[<] x).NeBot] : (𝓝[>] toDual x).NeBot := ‹_› instance OrderDual.instNeBotNhdsWithinIio [(𝓝[>] x).NeBot] : (𝓝[<] toDual x).NeBot := ‹_› end theorem Quotient.preimage_mem_nhds [TopologicalSpace X] [s : Setoid X] {V : Set <| Quotient s} {x : X} (hs : V ∈ 𝓝 (Quotient.mk' x)) : Quotient.mk' ⁻¹' V ∈ 𝓝 x := preimage_nhds_coinduced hs /-- The image of a dense set under `Quotient.mk'` is a dense set. -/ theorem Dense.quotient [Setoid X] [TopologicalSpace X] {s : Set X} (H : Dense s) : Dense (Quotient.mk' '' s) := Quotient.mk''_surjective.denseRange.dense_image continuous_coinduced_rng H /-- The composition of `Quotient.mk'` and a function with dense range has dense range. -/ theorem DenseRange.quotient [Setoid X] [TopologicalSpace X] {f : Y → X} (hf : DenseRange f) : DenseRange (Quotient.mk' ∘ f) := Quotient.mk''_surjective.denseRange.comp hf continuous_coinduced_rng theorem continuous_map_of_le {α : Type*} [TopologicalSpace α] {s t : Setoid α} (h : s ≤ t) : Continuous (Setoid.map_of_le h) := continuous_coinduced_rng theorem continuous_map_sInf {α : Type*} [TopologicalSpace α] {S : Set (Setoid α)} {s : Setoid α} (h : s ∈ S) : Continuous (Setoid.map_sInf h) := continuous_coinduced_rng instance {p : X → Prop} [TopologicalSpace X] [DiscreteTopology X] : DiscreteTopology (Subtype p) := ⟨bot_unique fun s _ => ⟨(↑) '' s, isOpen_discrete _, preimage_image_eq _ Subtype.val_injective⟩⟩ instance Sum.discreteTopology [TopologicalSpace X] [TopologicalSpace Y] [h : DiscreteTopology X] [hY : DiscreteTopology Y] : DiscreteTopology (X ⊕ Y) := ⟨sup_eq_bot_iff.2 <| by simp [h.eq_bot, hY.eq_bot]⟩ instance Sigma.discreteTopology {ι : Type*} {Y : ι → Type v} [∀ i, TopologicalSpace (Y i)] [h : ∀ i, DiscreteTopology (Y i)] : DiscreteTopology (Sigma Y) := ⟨iSup_eq_bot.2 fun _ => by simp only [(h _).eq_bot, coinduced_bot]⟩ @[simp] lemma comap_nhdsWithin_range {α β} [TopologicalSpace β] (f : α → β) (y : β) : comap f (𝓝[range f] y) = comap f (𝓝 y) := comap_inf_principal_range section Top variable [TopologicalSpace X] /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype (s : Set X) (x : { x // x ∈ s }) (t : Set { x // x ∈ s }) : t ∈ 𝓝 x ↔ ∃ u ∈ 𝓝 (x : X), Subtype.val ⁻¹' u ⊆ t := mem_nhds_induced _ x t theorem nhds_subtype (s : Set X) (x : { x // x ∈ s }) : 𝓝 x = comap (↑) (𝓝 (x : X)) := nhds_induced _ x lemma nhds_subtype_eq_comap_nhdsWithin (s : Set X) (x : { x // x ∈ s }) : 𝓝 x = comap (↑) (𝓝[s] (x : X)) := by rw [nhds_subtype, ← comap_nhdsWithin_range, Subtype.range_val] theorem nhdsWithin_subtype_eq_bot_iff {s t : Set X} {x : s} : 𝓝[((↑) : s → X) ⁻¹' t] x = ⊥ ↔ 𝓝[t] (x : X) ⊓ 𝓟 s = ⊥ := by rw [inf_principal_eq_bot_iff_comap, nhdsWithin, nhdsWithin, comap_inf, comap_principal, nhds_induced] theorem nhds_ne_subtype_eq_bot_iff {S : Set X} {x : S} : 𝓝[≠] x = ⊥ ↔ 𝓝[≠] (x : X) ⊓ 𝓟 S = ⊥ := by rw [← nhdsWithin_subtype_eq_bot_iff, preimage_compl, ← image_singleton, Subtype.coe_injective.preimage_image] theorem nhds_ne_subtype_neBot_iff {S : Set X} {x : S} : (𝓝[≠] x).NeBot ↔ (𝓝[≠] (x : X) ⊓ 𝓟 S).NeBot := by rw [neBot_iff, neBot_iff, not_iff_not, nhds_ne_subtype_eq_bot_iff] theorem discreteTopology_subtype_iff {S : Set X} : DiscreteTopology S ↔ ∀ x ∈ S, 𝓝[≠] x ⊓ 𝓟 S = ⊥ := by simp_rw [discreteTopology_iff_nhds_ne, SetCoe.forall', nhds_ne_subtype_eq_bot_iff] end Top /-- A type synonym equipped with the topology whose open sets are the empty set and the sets with finite complements. -/ def CofiniteTopology (X : Type*) := X namespace CofiniteTopology /-- The identity equivalence between `` and `CofiniteTopology `. -/ def of : X ≃ CofiniteTopology X := Equiv.refl X instance [Inhabited X] : Inhabited (CofiniteTopology X) where default := of default instance : TopologicalSpace (CofiniteTopology X) where IsOpen s := s.Nonempty → Set.Finite sᶜ isOpen_univ := by simp isOpen_inter s t := by rintro hs ht ⟨x, hxs, hxt⟩ rw [compl_inter] exact (hs ⟨x, hxs⟩).union (ht ⟨x, hxt⟩) isOpen_sUnion := by rintro s h ⟨x, t, hts, hzt⟩ rw [compl_sUnion] exact Finite.sInter (mem_image_of_mem _ hts) (h t hts ⟨x, hzt⟩) theorem isOpen_iff {s : Set (CofiniteTopology X)} : IsOpen s ↔ s.Nonempty → sᶜ.Finite := Iff.rfl theorem isOpen_iff' {s : Set (CofiniteTopology X)} : IsOpen s ↔ s = ∅ ∨ sᶜ.Finite := by simp only [isOpen_iff, nonempty_iff_ne_empty, or_iff_not_imp_left] theorem isClosed_iff {s : Set (CofiniteTopology X)} : IsClosed s ↔ s = univ ∨ s.Finite := by simp only [← isOpen_compl_iff, isOpen_iff', compl_compl, compl_empty_iff] theorem nhds_eq (x : CofiniteTopology X) : 𝓝 x = pure x ⊔ cofinite := by ext U rw [mem_nhds_iff] constructor · rintro ⟨V, hVU, V_op, haV⟩ exact mem_sup.mpr ⟨hVU haV, mem_of_superset (V_op ⟨_, haV⟩) hVU⟩ · rintro ⟨hU : x ∈ U, hU' : Uᶜ.Finite⟩ exact ⟨U, Subset.rfl, fun _ => hU', hU⟩ theorem mem_nhds_iff {x : CofiniteTopology X} {s : Set (CofiniteTopology X)} : s ∈ 𝓝 x ↔ x ∈ s ∧ sᶜ.Finite := by simp [nhds_eq] end CofiniteTopology end Constructions section Prod variable [TopologicalSpace X] [TopologicalSpace Y] theorem MapClusterPt.curry_prodMap {α β : Type*} {f : α → X} {g : β → Y} {la : Filter α} {lb : Filter β} {x : X} {y : Y} (hf : MapClusterPt x la f) (hg : MapClusterPt y lb g) : MapClusterPt (x, y) (la.curry lb) (.map f g) := by rw [mapClusterPt_iff_frequently] at hf hg rw [((𝓝 x).basis_sets.prod_nhds (𝓝 y).basis_sets).mapClusterPt_iff_frequently] rintro ⟨s, t⟩ ⟨hs, ht⟩ rw [frequently_curry_iff] exact (hf s hs).mono fun x hx ↦ (hg t ht).mono fun y hy ↦ ⟨hx, hy⟩ theorem MapClusterPt.prodMap {α β : Type*} {f : α → X} {g : β → Y} {la : Filter α} {lb : Filter β} {x : X} {y : Y} (hf : MapClusterPt x la f) (hg : MapClusterPt y lb g) : MapClusterPt (x, y) (la ×ˢ lb) (.map f g) := (hf.curry_prodMap hg).mono <| map_mono curry_le_prod end Prod section Bool lemma continuous_bool_rng [TopologicalSpace X] {f : X → Bool} (b : Bool) : Continuous f ↔ IsClopen (f ⁻¹' {b}) := by rw [continuous_discrete_rng, Bool.forall_bool' b, IsClopen, ← isOpen_compl_iff, ← preimage_compl, Bool.compl_singleton, and_comm] end Bool section Subtype variable [TopologicalSpace X] [TopologicalSpace Y] {p : X → Prop} lemma Topology.IsInducing.subtypeVal {t : Set Y} : IsInducing ((↑) : t → Y) := ⟨rfl⟩ @[deprecated (since := "2024-10-28")] alias inducing_subtype_val := IsInducing.subtypeVal lemma Topology.IsInducing.of_codRestrict {f : X → Y} {t : Set Y} (ht : ∀ x, f x ∈ t) (h : IsInducing (t.codRestrict f ht)) : IsInducing f := subtypeVal.comp h @[deprecated (since := "2024-10-28")] alias Inducing.of_codRestrict := IsInducing.of_codRestrict lemma Topology.IsEmbedding.subtypeVal : IsEmbedding ((↑) : Subtype p → X) := ⟨.subtypeVal, Subtype.coe_injective⟩ @[deprecated (since := "2024-10-26")] alias embedding_subtype_val := IsEmbedding.subtypeVal theorem Topology.IsClosedEmbedding.subtypeVal (h : IsClosed {a | p a}) : IsClosedEmbedding ((↑) : Subtype p → X) := ⟨.subtypeVal, by rwa [Subtype.range_coe_subtype]⟩ @[continuity, fun_prop] theorem continuous_subtype_val : Continuous (@Subtype.val X p) := continuous_induced_dom theorem Continuous.subtype_val {f : Y → Subtype p} (hf : Continuous f) : Continuous fun x => (f x : X) := continuous_subtype_val.comp hf theorem IsOpen.isOpenEmbedding_subtypeVal {s : Set X} (hs : IsOpen s) : IsOpenEmbedding ((↑) : s → X) := ⟨.subtypeVal, (@Subtype.range_coe _ s).symm ▸ hs⟩ theorem IsOpen.isOpenMap_subtype_val {s : Set X} (hs : IsOpen s) : IsOpenMap ((↑) : s → X) := hs.isOpenEmbedding_subtypeVal.isOpenMap theorem IsOpenMap.restrict {f : X → Y} (hf : IsOpenMap f) {s : Set X} (hs : IsOpen s) : IsOpenMap (s.restrict f) := hf.comp hs.isOpenMap_subtype_val lemma IsClosed.isClosedEmbedding_subtypeVal {s : Set X} (hs : IsClosed s) : IsClosedEmbedding ((↑) : s → X) := .subtypeVal hs theorem IsClosed.isClosedMap_subtype_val {s : Set X} (hs : IsClosed s) : IsClosedMap ((↑) : s → X) := hs.isClosedEmbedding_subtypeVal.isClosedMap @[continuity, fun_prop] theorem Continuous.subtype_mk {f : Y → X} (h : Continuous f) (hp : ∀ x, p (f x)) : Continuous fun x => (⟨f x, hp x⟩ : Subtype p) := continuous_induced_rng.2 h theorem Continuous.subtype_map {f : X → Y} (h : Continuous f) {q : Y → Prop} (hpq : ∀ x, p x → q (f x)) : Continuous (Subtype.map f hpq) := (h.comp continuous_subtype_val).subtype_mk _ theorem continuous_inclusion {s t : Set X} (h : s ⊆ t) : Continuous (inclusion h) := continuous_id.subtype_map h theorem continuousAt_subtype_val {p : X → Prop} {x : Subtype p} : ContinuousAt ((↑) : Subtype p → X) x := continuous_subtype_val.continuousAt theorem Subtype.dense_iff {s : Set X} {t : Set s} : Dense t ↔ s ⊆ closure ((↑) '' t) := by rw [IsInducing.subtypeVal.dense_iff, SetCoe.forall] rfl theorem map_nhds_subtype_val {s : Set X} (x : s) : map ((↑) : s → X) (𝓝 x) = 𝓝[s] ↑x := by rw [IsInducing.subtypeVal.map_nhds_eq, Subtype.range_val] theorem map_nhds_subtype_coe_eq_nhds {x : X} (hx : p x) (h : ∀ᶠ x in 𝓝 x, p x) : map ((↑) : Subtype p → X) (𝓝 ⟨x, hx⟩) = 𝓝 x := map_nhds_induced_of_mem <| by rw [Subtype.range_val]; exact h theorem nhds_subtype_eq_comap {x : X} {h : p x} : 𝓝 (⟨x, h⟩ : Subtype p) = comap (↑) (𝓝 x) := nhds_induced _ _ theorem tendsto_subtype_rng {Y : Type*} {p : X → Prop} {l : Filter Y} {f : Y → Subtype p} : ∀ {x : Subtype p}, Tendsto f l (𝓝 x) ↔ Tendsto (fun x => (f x : X)) l (𝓝 (x : X)) | ⟨a, ha⟩ => by rw [nhds_subtype_eq_comap, tendsto_comap_iff]; rfl theorem closure_subtype {x : { a // p a }} {s : Set { a // p a }} : x ∈ closure s ↔ (x : X) ∈ closure (((↑) : _ → X) '' s) := closure_induced @[simp] theorem continuousAt_codRestrict_iff {f : X → Y} {t : Set Y} (h1 : ∀ x, f x ∈ t) {x : X} : ContinuousAt (codRestrict f t h1) x ↔ ContinuousAt f x := IsInducing.subtypeVal.continuousAt_iff alias ⟨_, ContinuousAt.codRestrict⟩ := continuousAt_codRestrict_iff theorem ContinuousAt.restrict {f : X → Y} {s : Set X} {t : Set Y} (h1 : MapsTo f s t) {x : s} (h2 : ContinuousAt f x) : ContinuousAt (h1.restrict f s t) x := (h2.comp continuousAt_subtype_val).codRestrict _ theorem ContinuousAt.restrictPreimage {f : X → Y} {s : Set Y} {x : f ⁻¹' s} (h : ContinuousAt f x) : ContinuousAt (s.restrictPreimage f) x := h.restrict _ @[continuity, fun_prop] theorem Continuous.codRestrict {f : X → Y} {s : Set Y} (hf : Continuous f) (hs : ∀ a, f a ∈ s) : Continuous (s.codRestrict f hs) := hf.subtype_mk hs @[continuity, fun_prop] theorem Continuous.restrict {f : X → Y} {s : Set X} {t : Set Y} (h1 : MapsTo f s t) (h2 : Continuous f) : Continuous (h1.restrict f s t) := (h2.comp continuous_subtype_val).codRestrict _ @[continuity, fun_prop] theorem Continuous.restrictPreimage {f : X → Y} {s : Set Y} (h : Continuous f) : Continuous (s.restrictPreimage f) := h.restrict _ lemma Topology.IsEmbedding.restrict {f : X → Y} (hf : IsEmbedding f) {s : Set X} {t : Set Y} (H : s.MapsTo f t) : IsEmbedding H.restrict := .of_comp (hf.continuous.restrict H) continuous_subtype_val (hf.comp .subtypeVal) lemma Topology.IsOpenEmbedding.restrict {f : X → Y} (hf : IsOpenEmbedding f) {s : Set X} {t : Set Y} (H : s.MapsTo f t) (hs : IsOpen s) : IsOpenEmbedding H.restrict := ⟨hf.isEmbedding.restrict H, (by rw [MapsTo.range_restrict] exact continuous_subtype_val.1 _ (hf.isOpenMap _ hs))⟩ theorem Topology.IsInducing.codRestrict {e : X → Y} (he : IsInducing e) {s : Set Y} (hs : ∀ x, e x ∈ s) : IsInducing (codRestrict e s hs) := he.of_comp (he.continuous.codRestrict hs) continuous_subtype_val @[deprecated (since := "2024-10-28")] alias Inducing.codRestrict := IsInducing.codRestrict protected lemma Topology.IsEmbedding.codRestrict {e : X → Y} (he : IsEmbedding e) (s : Set Y) (hs : ∀ x, e x ∈ s) : IsEmbedding (codRestrict e s hs) := he.of_comp (he.continuous.codRestrict hs) continuous_subtype_val @[deprecated (since := "2024-10-26")] alias Embedding.codRestrict := IsEmbedding.codRestrict variable {s t : Set X} protected lemma Topology.IsEmbedding.inclusion (h : s ⊆ t) : IsEmbedding (inclusion h) := IsEmbedding.subtypeVal.codRestrict _ _ protected lemma Topology.IsOpenEmbedding.inclusion (hst : s ⊆ t) (hs : IsOpen (t ↓∩ s)) : IsOpenEmbedding (inclusion hst) where toIsEmbedding := .inclusion _ isOpen_range := by rwa [range_inclusion] protected lemma Topology.IsClosedEmbedding.inclusion (hst : s ⊆ t) (hs : IsClosed (t ↓∩ s)) : IsClosedEmbedding (inclusion hst) where toIsEmbedding := .inclusion _ isClosed_range := by rwa [range_inclusion] @[deprecated (since := "2024-10-26")] alias embedding_inclusion := IsEmbedding.inclusion /-- Let `s, t ⊆ X` be two subsets of a topological space `X`. If `t ⊆ s` and the topology induced by `X`on `s` is discrete, then also the topology induces on `t` is discrete. -/ theorem DiscreteTopology.of_subset {X : Type*} [TopologicalSpace X] {s t : Set X} (_ : DiscreteTopology s) (ts : t ⊆ s) : DiscreteTopology t := (IsEmbedding.inclusion ts).discreteTopology /-- Let `s` be a discrete subset of a topological space. Then the preimage of `s` by a continuous injective map is also discrete. -/ theorem DiscreteTopology.preimage_of_continuous_injective {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y] (s : Set Y) [DiscreteTopology s] {f : X → Y} (hc : Continuous f) (hinj : Function.Injective f) : DiscreteTopology (f ⁻¹' s) := DiscreteTopology.of_continuous_injective (β := s) (Continuous.restrict (by exact fun _ x ↦ x) hc) ((MapsTo.restrict_inj _).mpr hinj.injOn) /-- If `f : X → Y` is a quotient map, then its restriction to the preimage of an open set is a quotient map too. -/ theorem Topology.IsQuotientMap.restrictPreimage_isOpen {f : X → Y} (hf : IsQuotientMap f) {s : Set Y} (hs : IsOpen s) : IsQuotientMap (s.restrictPreimage f) := by refine isQuotientMap_iff.2 ⟨hf.surjective.restrictPreimage _, fun U ↦ ?_⟩ rw [hs.isOpenEmbedding_subtypeVal.isOpen_iff_image_isOpen, ← hf.isOpen_preimage, (hs.preimage hf.continuous).isOpenEmbedding_subtypeVal.isOpen_iff_image_isOpen, image_val_preimage_restrictPreimage] @[deprecated (since := "2024-10-22")] alias QuotientMap.restrictPreimage_isOpen := IsQuotientMap.restrictPreimage_isOpen open scoped Set.Notation in lemma isClosed_preimage_val {s t : Set X} : IsClosed (s ↓∩ t) ↔ s ∩ closure (s ∩ t) ⊆ t := by rw [← closure_eq_iff_isClosed, IsEmbedding.subtypeVal.closure_eq_preimage_closure_image, ← Subtype.val_injective.image_injective.eq_iff, Subtype.image_preimage_coe, Subtype.image_preimage_coe, subset_antisymm_iff, and_iff_left, Set.subset_inter_iff, and_iff_right] exacts [Set.inter_subset_left, Set.subset_inter Set.inter_subset_left subset_closure] theorem frontier_inter_open_inter {s t : Set X} (ht : IsOpen t) : frontier (s ∩ t) ∩ t = frontier s ∩ t := by simp only [Set.inter_comm _ t, ← Subtype.preimage_coe_eq_preimage_coe_iff, ht.isOpenMap_subtype_val.preimage_frontier_eq_frontier_preimage continuous_subtype_val, Subtype.preimage_coe_self_inter] section SetNotation open scoped Set.Notation lemma IsOpen.preimage_val {s t : Set X} (ht : IsOpen t) : IsOpen (s ↓∩ t) := ht.preimage continuous_subtype_val lemma IsClosed.preimage_val {s t : Set X} (ht : IsClosed t) : IsClosed (s ↓∩ t) := ht.preimage continuous_subtype_val @[simp] lemma IsOpen.inter_preimage_val_iff {s t : Set X} (hs : IsOpen s) : IsOpen (s ↓∩ t) ↔ IsOpen (s ∩ t) := ⟨fun h ↦ by simpa using hs.isOpenMap_subtype_val _ h, fun h ↦ (Subtype.preimage_coe_self_inter _ _).symm ▸ h.preimage_val⟩ @[simp] lemma IsClosed.inter_preimage_val_iff {s t : Set X} (hs : IsClosed s) : IsClosed (s ↓∩ t) ↔ IsClosed (s ∩ t) := ⟨fun h ↦ by simpa using hs.isClosedMap_subtype_val _ h, fun h ↦ (Subtype.preimage_coe_self_inter _ _).symm ▸ h.preimage_val⟩ end SetNotation end Subtype section Quotient variable [TopologicalSpace X] [TopologicalSpace Y] variable {r : X → X → Prop} {s : Setoid X} theorem isQuotientMap_quot_mk : IsQuotientMap (@Quot.mk X r) := ⟨Quot.exists_rep, rfl⟩ @[deprecated (since := "2024-10-22")] alias quotientMap_quot_mk := isQuotientMap_quot_mk @[continuity, fun_prop] theorem continuous_quot_mk : Continuous (@Quot.mk X r) := continuous_coinduced_rng @[continuity, fun_prop] theorem continuous_quot_lift {f : X → Y} (hr : ∀ a b, r a b → f a = f b) (h : Continuous f) : Continuous (Quot.lift f hr : Quot r → Y) := continuous_coinduced_dom.2 h theorem isQuotientMap_quotient_mk' : IsQuotientMap (@Quotient.mk' X s) := isQuotientMap_quot_mk @[deprecated (since := "2024-10-22")] alias quotientMap_quotient_mk' := isQuotientMap_quotient_mk' theorem continuous_quotient_mk' : Continuous (@Quotient.mk' X s) := continuous_coinduced_rng theorem Continuous.quotient_lift {f : X → Y} (h : Continuous f) (hs : ∀ a b, a ≈ b → f a = f b) : Continuous (Quotient.lift f hs : Quotient s → Y) := continuous_coinduced_dom.2 h theorem Continuous.quotient_liftOn' {f : X → Y} (h : Continuous f) (hs : ∀ a b, s a b → f a = f b) : Continuous (fun x => Quotient.liftOn' x f hs : Quotient s → Y) := h.quotient_lift hs open scoped Relator in @[continuity, fun_prop] theorem Continuous.quotient_map' {t : Setoid Y} {f : X → Y} (hf : Continuous f) (H : (s.r ⇒ t.r) f f) : Continuous (Quotient.map' f H) := (continuous_quotient_mk'.comp hf).quotient_lift _ end Quotient section Pi variable {ι : Type*} {π : ι → Type*} {κ : Type*} [TopologicalSpace X] [T : ∀ i, TopologicalSpace (π i)] {f : X → ∀ i : ι, π i} theorem continuous_pi_iff : Continuous f ↔ ∀ i, Continuous fun a => f a i := by simp only [continuous_iInf_rng, continuous_induced_rng, comp_def] @[continuity, fun_prop] theorem continuous_pi (h : ∀ i, Continuous fun a => f a i) : Continuous f := continuous_pi_iff.2 h @[continuity, fun_prop] theorem continuous_apply (i : ι) : Continuous fun p : ∀ i, π i => p i := continuous_iInf_dom continuous_induced_dom @[continuity] theorem continuous_apply_apply {ρ : κ → ι → Type*} [∀ j i, TopologicalSpace (ρ j i)] (j : κ) (i : ι) : Continuous fun p : ∀ j, ∀ i, ρ j i => p j i := (continuous_apply i).comp (continuous_apply j) theorem continuousAt_apply (i : ι) (x : ∀ i, π i) : ContinuousAt (fun p : ∀ i, π i => p i) x := (continuous_apply i).continuousAt theorem Filter.Tendsto.apply_nhds {l : Filter Y} {f : Y → ∀ i, π i} {x : ∀ i, π i} (h : Tendsto f l (𝓝 x)) (i : ι) : Tendsto (fun a => f a i) l (𝓝 <| x i) := (continuousAt_apply i _).tendsto.comp h @[fun_prop] protected theorem Continuous.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} (hf : ∀ i, Continuous (f i)) : Continuous (Pi.map f) := continuous_pi fun i ↦ (hf i).comp (continuous_apply i) theorem nhds_pi {a : ∀ i, π i} : 𝓝 a = pi fun i => 𝓝 (a i) := by simp only [nhds_iInf, nhds_induced, Filter.pi] protected theorem IsOpenMap.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} (hfo : ∀ i, IsOpenMap (f i)) (hsurj : ∀ᶠ i in cofinite, Surjective (f i)) : IsOpenMap (Pi.map f) := by refine IsOpenMap.of_nhds_le fun x ↦ ?_ rw [nhds_pi, nhds_pi, map_piMap_pi hsurj] exact Filter.pi_mono fun i ↦ (hfo i).nhds_le _ protected theorem IsOpenQuotientMap.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} (hf : ∀ i, IsOpenQuotientMap (f i)) : IsOpenQuotientMap (Pi.map f) := ⟨.piMap fun i ↦ (hf i).1, .piMap fun i ↦ (hf i).2, .piMap (fun i ↦ (hf i).3) <| .of_forall fun i ↦ (hf i).1⟩ theorem tendsto_pi_nhds {f : Y → ∀ i, π i} {g : ∀ i, π i} {u : Filter Y} : Tendsto f u (𝓝 g) ↔ ∀ x, Tendsto (fun i => f i x) u (𝓝 (g x)) := by rw [nhds_pi, Filter.tendsto_pi] theorem continuousAt_pi {f : X → ∀ i, π i} {x : X} : ContinuousAt f x ↔ ∀ i, ContinuousAt (fun y => f y i) x := tendsto_pi_nhds @[fun_prop] theorem continuousAt_pi' {f : X → ∀ i, π i} {x : X} (hf : ∀ i, ContinuousAt (fun y => f y i) x) : ContinuousAt f x := continuousAt_pi.2 hf @[fun_prop] protected theorem ContinuousAt.piMap {Y : ι → Type*} [∀ i, TopologicalSpace (Y i)] {f : ∀ i, π i → Y i} {x : ∀ i, π i} (hf : ∀ i, ContinuousAt (f i) (x i)) : ContinuousAt (Pi.map f) x := continuousAt_pi.2 fun i ↦ (hf i).comp (continuousAt_apply i x) theorem Pi.continuous_precomp' {ι' : Type*} (φ : ι' → ι) : Continuous (fun (f : (∀ i, π i)) (j : ι') ↦ f (φ j)) := continuous_pi fun j ↦ continuous_apply (φ j) theorem Pi.continuous_precomp {ι' : Type*} (φ : ι' → ι) : Continuous (· ∘ φ : (ι → X) → (ι' → X)) := Pi.continuous_precomp' φ theorem Pi.continuous_postcomp' {X : ι → Type*} [∀ i, TopologicalSpace (X i)] {g : ∀ i, π i → X i} (hg : ∀ i, Continuous (g i)) : Continuous (fun (f : (∀ i, π i)) (i : ι) ↦ g i (f i)) := continuous_pi fun i ↦ (hg i).comp <| continuous_apply i theorem Pi.continuous_postcomp [TopologicalSpace Y] {g : X → Y} (hg : Continuous g) : Continuous (g ∘ · : (ι → X) → (ι → Y)) := Pi.continuous_postcomp' fun _ ↦ hg lemma Pi.induced_precomp' {ι' : Type*} (φ : ι' → ι) : induced (fun (f : (∀ i, π i)) (j : ι') ↦ f (φ j)) Pi.topologicalSpace = ⨅ i', induced (eval (φ i')) (T (φ i')) := by simp [Pi.topologicalSpace, induced_iInf, induced_compose, comp_def] lemma Pi.induced_precomp [TopologicalSpace Y] {ι' : Type*} (φ : ι' → ι) : induced (· ∘ φ) Pi.topologicalSpace = ⨅ i', induced (eval (φ i')) ‹TopologicalSpace Y› := induced_precomp' φ @[continuity, fun_prop] lemma Pi.continuous_restrict (S : Set ι) : Continuous (S.restrict : (∀ i : ι, π i) → (∀ i : S, π i)) := Pi.continuous_precomp' ((↑) : S → ι) @[continuity, fun_prop] lemma Pi.continuous_restrict₂ {s t : Set ι} (hst : s ⊆ t) : Continuous (restrict₂ (π := π) hst) := continuous_pi fun _ ↦ continuous_apply _ @[continuity, fun_prop] theorem Finset.continuous_restrict (s : Finset ι) : Continuous (s.restrict (π := π)) := continuous_pi fun _ ↦ continuous_apply _ @[continuity, fun_prop] theorem Finset.continuous_restrict₂ {s t : Finset ι} (hst : s ⊆ t) : Continuous (Finset.restrict₂ (π := π) hst) := continuous_pi fun _ ↦ continuous_apply _ variable [TopologicalSpace Z] @[continuity, fun_prop] theorem Pi.continuous_restrict_apply (s : Set X) {f : X → Z} (hf : Continuous f) : Continuous (s.restrict f) := hf.comp continuous_subtype_val @[continuity, fun_prop] theorem Pi.continuous_restrict₂_apply {s t : Set X} (hst : s ⊆ t) {f : t → Z} (hf : Continuous f) : Continuous (restrict₂ (π := fun _ ↦ Z) hst f) := hf.comp (continuous_inclusion hst) @[continuity, fun_prop] theorem Finset.continuous_restrict_apply (s : Finset X) {f : X → Z} (hf : Continuous f) : Continuous (s.restrict f) := hf.comp continuous_subtype_val @[continuity, fun_prop] theorem Finset.continuous_restrict₂_apply {s t : Finset X} (hst : s ⊆ t) {f : t → Z} (hf : Continuous f) : Continuous (restrict₂ (π := fun _ ↦ Z) hst f) := hf.comp (continuous_inclusion hst) lemma Pi.induced_restrict (S : Set ι) : induced (S.restrict) Pi.topologicalSpace = ⨅ i ∈ S, induced (eval i) (T i) := by simp +unfoldPartialApp [← iInf_subtype'', ← induced_precomp' ((↑) : S → ι), restrict] lemma Pi.induced_restrict_sUnion (𝔖 : Set (Set ι)) : induced (⋃₀ 𝔖).restrict (Pi.topologicalSpace (Y := fun i : (⋃₀ 𝔖) ↦ π i)) = ⨅ S ∈ 𝔖, induced S.restrict Pi.topologicalSpace := by simp_rw [Pi.induced_restrict, iInf_sUnion] theorem Filter.Tendsto.update [DecidableEq ι] {l : Filter Y} {f : Y → ∀ i, π i} {x : ∀ i, π i} (hf : Tendsto f l (𝓝 x)) (i : ι) {g : Y → π i} {xi : π i} (hg : Tendsto g l (𝓝 xi)) : Tendsto (fun a => update (f a) i (g a)) l (𝓝 <| update x i xi) := tendsto_pi_nhds.2 fun j => by rcases eq_or_ne j i with (rfl | hj) <;> simp [*, hf.apply_nhds] theorem ContinuousAt.update [DecidableEq ι] {x : X} (hf : ContinuousAt f x) (i : ι) {g : X → π i} (hg : ContinuousAt g x) : ContinuousAt (fun a => update (f a) i (g a)) x := hf.tendsto.update i hg theorem Continuous.update [DecidableEq ι] (hf : Continuous f) (i : ι) {g : X → π i} (hg : Continuous g) : Continuous fun a => update (f a) i (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.update i hg.continuousAt /-- `Function.update f i x` is continuous in `(f, x)`. -/ @[continuity, fun_prop] theorem continuous_update [DecidableEq ι] (i : ι) : Continuous fun f : (∀ j, π j) × π i => update f.1 i f.2 := continuous_fst.update i continuous_snd /-- `Pi.mulSingle i x` is continuous in `x`. -/ @[to_additive (attr := continuity) "`Pi.single i x` is continuous in `x`."] theorem continuous_mulSingle [∀ i, One (π i)] [DecidableEq ι] (i : ι) : Continuous fun x => (Pi.mulSingle i x : ∀ i, π i) := continuous_const.update _ continuous_id section Fin variable {n : ℕ} {π : Fin (n + 1) → Type*} [∀ i, TopologicalSpace (π i)] theorem Filter.Tendsto.finCons {f : Y → π 0} {g : Y → ∀ j : Fin n, π j.succ} {l : Filter Y} {x : π 0} {y : ∀ j, π (Fin.succ j)} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => Fin.cons (f a) (g a)) l (𝓝 <| Fin.cons x y) := tendsto_pi_nhds.2 fun j => Fin.cases (by simpa) (by simpa using tendsto_pi_nhds.1 hg) j theorem ContinuousAt.finCons {f : X → π 0} {g : X → ∀ j : Fin n, π (Fin.succ j)} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => Fin.cons (f a) (g a)) x := hf.tendsto.finCons hg theorem Continuous.finCons {f : X → π 0} {g : X → ∀ j : Fin n, π (Fin.succ j)} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => Fin.cons (f a) (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.finCons hg.continuousAt theorem Filter.Tendsto.matrixVecCons {f : Y → Z} {g : Y → Fin n → Z} {l : Filter Y} {x : Z} {y : Fin n → Z} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => Matrix.vecCons (f a) (g a)) l (𝓝 <| Matrix.vecCons x y) := hf.finCons hg theorem ContinuousAt.matrixVecCons {f : X → Z} {g : X → Fin n → Z} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => Matrix.vecCons (f a) (g a)) x := hf.finCons hg theorem Continuous.matrixVecCons {f : X → Z} {g : X → Fin n → Z} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => Matrix.vecCons (f a) (g a) := hf.finCons hg theorem Filter.Tendsto.finSnoc {f : Y → ∀ j : Fin n, π j.castSucc} {g : Y → π (Fin.last _)} {l : Filter Y} {x : ∀ j, π (Fin.castSucc j)} {y : π (Fin.last _)} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => Fin.snoc (f a) (g a)) l (𝓝 <| Fin.snoc x y) := tendsto_pi_nhds.2 fun j => Fin.lastCases (by simpa) (by simpa using tendsto_pi_nhds.1 hf) j theorem ContinuousAt.finSnoc {f : X → ∀ j : Fin n, π j.castSucc} {g : X → π (Fin.last _)} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => Fin.snoc (f a) (g a)) x := hf.tendsto.finSnoc hg theorem Continuous.finSnoc {f : X → ∀ j : Fin n, π j.castSucc} {g : X → π (Fin.last _)} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => Fin.snoc (f a) (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.finSnoc hg.continuousAt theorem Filter.Tendsto.finInsertNth (i : Fin (n + 1)) {f : Y → π i} {g : Y → ∀ j : Fin n, π (i.succAbove j)} {l : Filter Y} {x : π i} {y : ∀ j, π (i.succAbove j)} (hf : Tendsto f l (𝓝 x)) (hg : Tendsto g l (𝓝 y)) : Tendsto (fun a => i.insertNth (f a) (g a)) l (𝓝 <| i.insertNth x y) := tendsto_pi_nhds.2 fun j => Fin.succAboveCases i (by simpa) (by simpa using tendsto_pi_nhds.1 hg) j @[deprecated (since := "2025-01-02")] alias Filter.Tendsto.fin_insertNth := Filter.Tendsto.finInsertNth theorem ContinuousAt.finInsertNth (i : Fin (n + 1)) {f : X → π i} {g : X → ∀ j : Fin n, π (i.succAbove j)} {x : X} (hf : ContinuousAt f x) (hg : ContinuousAt g x) : ContinuousAt (fun a => i.insertNth (f a) (g a)) x := hf.tendsto.finInsertNth i hg @[deprecated (since := "2025-01-02")] alias ContinuousAt.fin_insertNth := ContinuousAt.finInsertNth theorem Continuous.finInsertNth (i : Fin (n + 1)) {f : X → π i} {g : X → ∀ j : Fin n, π (i.succAbove j)} (hf : Continuous f) (hg : Continuous g) : Continuous fun a => i.insertNth (f a) (g a) := continuous_iff_continuousAt.2 fun _ => hf.continuousAt.finInsertNth i hg.continuousAt @[deprecated (since := "2025-01-02")] alias Continuous.fin_insertNth := Continuous.finInsertNth theorem Filter.Tendsto.finInit {f : Y → ∀ j : Fin (n + 1), π j} {l : Filter Y} {x : ∀ j, π j} (hg : Tendsto f l (𝓝 x)) : Tendsto (fun a ↦ Fin.init (f a)) l (𝓝 <| Fin.init x) := tendsto_pi_nhds.2 fun j ↦ apply_nhds hg j.castSucc @[fun_prop] theorem ContinuousAt.finInit {f : X → ∀ j : Fin (n + 1), π j} {x : X} (hf : ContinuousAt f x) : ContinuousAt (fun a ↦ Fin.init (f a)) x := hf.tendsto.finInit @[fun_prop] theorem Continuous.finInit {f : X → ∀ j : Fin (n + 1), π j} (hf : Continuous f) : Continuous fun a ↦ Fin.init (f a) := continuous_iff_continuousAt.2 fun _ ↦ hf.continuousAt.finInit theorem Filter.Tendsto.finTail {f : Y → ∀ j : Fin (n + 1), π j} {l : Filter Y} {x : ∀ j, π j} (hg : Tendsto f l (𝓝 x)) : Tendsto (fun a ↦ Fin.tail (f a)) l (𝓝 <| Fin.tail x) := tendsto_pi_nhds.2 fun j ↦ apply_nhds hg j.succ @[fun_prop] theorem ContinuousAt.finTail {f : X → ∀ j : Fin (n + 1), π j} {x : X} (hf : ContinuousAt f x) : ContinuousAt (fun a ↦ Fin.tail (f a)) x := hf.tendsto.finTail @[fun_prop] theorem Continuous.finTail {f : X → ∀ j : Fin (n + 1), π j} (hf : Continuous f) : Continuous fun a ↦ Fin.tail (f a) := continuous_iff_continuousAt.2 fun _ ↦ hf.continuousAt.finTail end Fin theorem isOpen_set_pi {i : Set ι} {s : ∀ a, Set (π a)} (hi : i.Finite) (hs : ∀ a ∈ i, IsOpen (s a)) : IsOpen (pi i s) := by rw [pi_def]; exact hi.isOpen_biInter fun a ha => (hs _ ha).preimage (continuous_apply _) theorem isOpen_pi_iff {s : Set (∀ a, π a)} : IsOpen s ↔ ∀ f, f ∈ s → ∃ (I : Finset ι) (u : ∀ a, Set (π a)), (∀ a, a ∈ I → IsOpen (u a) ∧ f a ∈ u a) ∧ (I : Set ι).pi u ⊆ s := by rw [isOpen_iff_nhds] simp_rw [le_principal_iff, nhds_pi, Filter.mem_pi', mem_nhds_iff] refine forall₂_congr fun a _ => ⟨?_, ?_⟩ · rintro ⟨I, t, ⟨h1, h2⟩⟩ refine ⟨I, fun a => eval a '' (I : Set ι).pi fun a => (h1 a).choose, fun i hi => ?_, ?_⟩ · simp_rw [eval_image_pi (Finset.mem_coe.mpr hi) (pi_nonempty_iff.mpr fun i => ⟨_, fun _ => (h1 i).choose_spec.2.2⟩)] exact (h1 i).choose_spec.2 · exact Subset.trans (pi_mono fun i hi => (eval_image_pi_subset hi).trans (h1 i).choose_spec.1) h2 · rintro ⟨I, t, ⟨h1, h2⟩⟩ classical refine ⟨I, fun a => ite (a ∈ I) (t a) univ, fun i => ?_, ?_⟩ · by_cases hi : i ∈ I · use t i simp_rw [if_pos hi] exact ⟨Subset.rfl, (h1 i) hi⟩ · use univ simp_rw [if_neg hi] exact ⟨Subset.rfl, isOpen_univ, mem_univ _⟩ · rw [← univ_pi_ite] simp only [← ite_and, ← Finset.mem_coe, and_self_iff, univ_pi_ite, h2] theorem isOpen_pi_iff' [Finite ι] {s : Set (∀ a, π a)} : IsOpen s ↔ ∀ f, f ∈ s → ∃ u : ∀ a, Set (π a), (∀ a, IsOpen (u a) ∧ f a ∈ u a) ∧ univ.pi u ⊆ s := by cases nonempty_fintype ι rw [isOpen_iff_nhds] simp_rw [le_principal_iff, nhds_pi, Filter.mem_pi', mem_nhds_iff] refine forall₂_congr fun a _ => ⟨?_, ?_⟩ · rintro ⟨I, t, ⟨h1, h2⟩⟩ refine ⟨fun i => (h1 i).choose, ⟨fun i => (h1 i).choose_spec.2, (pi_mono fun i _ => (h1 i).choose_spec.1).trans (Subset.trans ?_ h2)⟩⟩ rw [← pi_inter_compl (I : Set ι)] exact inter_subset_left · exact fun ⟨u, ⟨h1, _⟩⟩ => ⟨Finset.univ, u, ⟨fun i => ⟨u i, ⟨rfl.subset, h1 i⟩⟩, by rwa [Finset.coe_univ]⟩⟩ theorem isClosed_set_pi {i : Set ι} {s : ∀ a, Set (π a)} (hs : ∀ a ∈ i, IsClosed (s a)) : IsClosed (pi i s) := by rw [pi_def]; exact isClosed_biInter fun a ha => (hs _ ha).preimage (continuous_apply _) theorem mem_nhds_of_pi_mem_nhds {I : Set ι} {s : ∀ i, Set (π i)} (a : ∀ i, π i) (hs : I.pi s ∈ 𝓝 a) {i : ι} (hi : i ∈ I) : s i ∈ 𝓝 (a i) := by rw [nhds_pi] at hs; exact mem_of_pi_mem_pi hs hi theorem set_pi_mem_nhds {i : Set ι} {s : ∀ a, Set (π a)} {x : ∀ a, π a} (hi : i.Finite) (hs : ∀ a ∈ i, s a ∈ 𝓝 (x a)) : pi i s ∈ 𝓝 x := by rw [pi_def, biInter_mem hi] exact fun a ha => (continuous_apply a).continuousAt (hs a ha) theorem set_pi_mem_nhds_iff {I : Set ι} (hI : I.Finite) {s : ∀ i, Set (π i)} (a : ∀ i, π i) : I.pi s ∈ 𝓝 a ↔ ∀ i : ι, i ∈ I → s i ∈ 𝓝 (a i) := by rw [nhds_pi, pi_mem_pi_iff hI] theorem interior_pi_set {I : Set ι} (hI : I.Finite) {s : ∀ i, Set (π i)} : interior (pi I s) = I.pi fun i => interior (s i) := by ext a simp only [Set.mem_pi, mem_interior_iff_mem_nhds, set_pi_mem_nhds_iff hI] theorem exists_finset_piecewise_mem_of_mem_nhds [DecidableEq ι] {s : Set (∀ a, π a)} {x : ∀ a, π a} (hs : s ∈ 𝓝 x) (y : ∀ a, π a) : ∃ I : Finset ι, I.piecewise x y ∈ s := by simp only [nhds_pi, Filter.mem_pi'] at hs rcases hs with ⟨I, t, htx, hts⟩ refine ⟨I, hts fun i hi => ?_⟩ simpa [Finset.mem_coe.1 hi] using mem_of_mem_nhds (htx i) theorem pi_generateFrom_eq {π : ι → Type*} {g : ∀ a, Set (Set (π a))} : (@Pi.topologicalSpace ι π fun a => generateFrom (g a)) = generateFrom { t | ∃ (s : ∀ a, Set (π a)) (i : Finset ι), (∀ a ∈ i, s a ∈ g a) ∧ t = pi (↑i) s } := by refine le_antisymm ?_ ?_ · apply le_generateFrom rintro _ ⟨s, i, hi, rfl⟩ letI := fun a => generateFrom (g a) exact isOpen_set_pi i.finite_toSet (fun a ha => GenerateOpen.basic _ (hi a ha)) · classical refine le_iInf fun i => coinduced_le_iff_le_induced.1 <| le_generateFrom fun s hs => ?_ refine GenerateOpen.basic _ ⟨update (fun i => univ) i s, {i}, ?_⟩ simp [hs] theorem pi_eq_generateFrom : Pi.topologicalSpace = generateFrom { g | ∃ (s : ∀ a, Set (π a)) (i : Finset ι), (∀ a ∈ i, IsOpen (s a)) ∧ g = pi (↑i) s } := calc Pi.topologicalSpace _ = @Pi.topologicalSpace ι π fun _ => generateFrom { s | IsOpen s } := by simp only [generateFrom_setOf_isOpen] _ = _ := pi_generateFrom_eq theorem pi_generateFrom_eq_finite {π : ι → Type*} {g : ∀ a, Set (Set (π a))} [Finite ι] (hg : ∀ a, ⋃₀ g a = univ) : (@Pi.topologicalSpace ι π fun a => generateFrom (g a)) = generateFrom { t | ∃ s : ∀ a, Set (π a), (∀ a, s a ∈ g a) ∧ t = pi univ s } := by cases nonempty_fintype ι rw [pi_generateFrom_eq] refine le_antisymm (generateFrom_anti ?_) (le_generateFrom ?_) · exact fun s ⟨t, ht, Eq⟩ => ⟨t, Finset.univ, by simp [ht, Eq]⟩ · rintro s ⟨t, i, ht, rfl⟩ letI := generateFrom { t | ∃ s : ∀ a, Set (π a), (∀ a, s a ∈ g a) ∧ t = pi univ s } refine isOpen_iff_forall_mem_open.2 fun f hf => ?_ choose c hcg hfc using fun a => sUnion_eq_univ_iff.1 (hg a) (f a) refine ⟨pi i t ∩ pi ((↑i)ᶜ : Set ι) c, inter_subset_left, ?_, ⟨hf, fun a _ => hfc a⟩⟩ classical rw [← univ_pi_piecewise] refine GenerateOpen.basic _ ⟨_, fun a => ?_, rfl⟩ by_cases a ∈ i <;> simp [*] theorem induced_to_pi {X : Type*} (f : X → ∀ i, π i) : induced f Pi.topologicalSpace = ⨅ i, induced (f · i) inferInstance := by simp_rw [Pi.topologicalSpace, induced_iInf, induced_compose, Function.comp_def] /-- Suppose `π i` is a family of topological spaces indexed by `i : ι`, and `X` is a type endowed with a family of maps `f i : X → π i` for every `i : ι`, hence inducing a map `g : X → Π i, π i`. This lemma shows that infimum of the topologies on `X` induced by the `f i` as `i : ι` varies is simply the topology on `X` induced by `g : X → Π i, π i` where `Π i, π i` is endowed with the usual product topology. -/ theorem inducing_iInf_to_pi {X : Type*} (f : ∀ i, X → π i) : @IsInducing X (∀ i, π i) (⨅ i, induced (f i) inferInstance) _ fun x i => f i x := letI := ⨅ i, induced (f i) inferInstance; ⟨(induced_to_pi _).symm⟩ variable [Finite ι] [∀ i, DiscreteTopology (π i)] /-- A finite product of discrete spaces is discrete. -/ instance Pi.discreteTopology : DiscreteTopology (∀ i, π i) := singletons_open_iff_discrete.mp fun x => by rw [← univ_pi_singleton] exact isOpen_set_pi finite_univ fun i _ => (isOpen_discrete {x i}) end Pi section Sigma variable {ι κ : Type*} {σ : ι → Type*} {τ : κ → Type*} [∀ i, TopologicalSpace (σ i)] [∀ k, TopologicalSpace (τ k)] [TopologicalSpace X] @[continuity, fun_prop] theorem continuous_sigmaMk {i : ι} : Continuous (@Sigma.mk ι σ i) := continuous_iSup_rng continuous_coinduced_rng theorem isOpen_sigma_iff {s : Set (Sigma σ)} : IsOpen s ↔ ∀ i, IsOpen (Sigma.mk i ⁻¹' s) := by rw [isOpen_iSup_iff] rfl theorem isClosed_sigma_iff {s : Set (Sigma σ)} : IsClosed s ↔ ∀ i, IsClosed (Sigma.mk i ⁻¹' s) := by simp only [← isOpen_compl_iff, isOpen_sigma_iff, preimage_compl] theorem isOpenMap_sigmaMk {i : ι} : IsOpenMap (@Sigma.mk ι σ i) := by intro s hs rw [isOpen_sigma_iff] intro j rcases eq_or_ne j i with (rfl | hne) · rwa [preimage_image_eq _ sigma_mk_injective] · rw [preimage_image_sigmaMk_of_ne hne] exact isOpen_empty theorem isOpen_range_sigmaMk {i : ι} : IsOpen (range (@Sigma.mk ι σ i)) := isOpenMap_sigmaMk.isOpen_range theorem isClosedMap_sigmaMk {i : ι} : IsClosedMap (@Sigma.mk ι σ i) := by intro s hs rw [isClosed_sigma_iff] intro j rcases eq_or_ne j i with (rfl | hne) · rwa [preimage_image_eq _ sigma_mk_injective] · rw [preimage_image_sigmaMk_of_ne hne] exact isClosed_empty theorem isClosed_range_sigmaMk {i : ι} : IsClosed (range (@Sigma.mk ι σ i)) := isClosedMap_sigmaMk.isClosed_range lemma Topology.IsOpenEmbedding.sigmaMk {i : ι} : IsOpenEmbedding (@Sigma.mk ι σ i) := .of_continuous_injective_isOpenMap continuous_sigmaMk sigma_mk_injective isOpenMap_sigmaMk @[deprecated (since := "2024-10-30")] alias isOpenEmbedding_sigmaMk := IsOpenEmbedding.sigmaMk lemma Topology.IsClosedEmbedding.sigmaMk {i : ι} : IsClosedEmbedding (@Sigma.mk ι σ i) := .of_continuous_injective_isClosedMap continuous_sigmaMk sigma_mk_injective isClosedMap_sigmaMk @[deprecated (since := "2024-10-30")] alias isClosedEmbedding_sigmaMk := IsClosedEmbedding.sigmaMk lemma Topology.IsEmbedding.sigmaMk {i : ι} : IsEmbedding (@Sigma.mk ι σ i) := IsClosedEmbedding.sigmaMk.1 @[deprecated (since := "2024-10-26")] alias embedding_sigmaMk := IsEmbedding.sigmaMk theorem Sigma.nhds_mk (i : ι) (x : σ i) : 𝓝 (⟨i, x⟩ : Sigma σ) = Filter.map (Sigma.mk i) (𝓝 x) := (IsOpenEmbedding.sigmaMk.map_nhds_eq x).symm theorem Sigma.nhds_eq (x : Sigma σ) : 𝓝 x = Filter.map (Sigma.mk x.1) (𝓝 x.2) := by cases x apply Sigma.nhds_mk theorem comap_sigmaMk_nhds (i : ι) (x : σ i) : comap (Sigma.mk i) (𝓝 ⟨i, x⟩) = 𝓝 x := (IsEmbedding.sigmaMk.nhds_eq_comap _).symm theorem isOpen_sigma_fst_preimage (s : Set ι) : IsOpen (Sigma.fst ⁻¹' s : Set (Σ a, σ a)) := by rw [← biUnion_of_singleton s, preimage_iUnion₂] simp only [← range_sigmaMk] exact isOpen_biUnion fun _ _ => isOpen_range_sigmaMk /-- A map out of a sum type is continuous iff its restriction to each summand is. -/ @[simp] theorem continuous_sigma_iff {f : Sigma σ → X} : Continuous f ↔ ∀ i, Continuous fun a => f ⟨i, a⟩ := by delta instTopologicalSpaceSigma rw [continuous_iSup_dom] exact forall_congr' fun _ => continuous_coinduced_dom /-- A map out of a sum type is continuous if its restriction to each summand is. -/ @[continuity, fun_prop] theorem continuous_sigma {f : Sigma σ → X} (hf : ∀ i, Continuous fun a => f ⟨i, a⟩) : Continuous f := continuous_sigma_iff.2 hf /-- A map defined on a sigma type (a.k.a. the disjoint union of an indexed family of topological spaces) is inducing iff its restriction to each component is inducing and each the image of each component under `f` can be separated from the images of all other components by an open set. -/ theorem inducing_sigma {f : Sigma σ → X} : IsInducing f ↔ (∀ i, IsInducing (f ∘ Sigma.mk i)) ∧ (∀ i, ∃ U, IsOpen U ∧ ∀ x, f x ∈ U ↔ x.1 = i) := by refine ⟨fun h ↦ ⟨fun i ↦ h.comp IsEmbedding.sigmaMk.1, fun i ↦ ?_⟩, ?_⟩ · rcases h.isOpen_iff.1 (isOpen_range_sigmaMk (i := i)) with ⟨U, hUo, hU⟩ refine ⟨U, hUo, ?_⟩ simpa [Set.ext_iff] using hU · refine fun ⟨h₁, h₂⟩ ↦ isInducing_iff_nhds.2 fun ⟨i, x⟩ ↦ ?_ rw [Sigma.nhds_mk, (h₁ i).nhds_eq_comap, comp_apply, ← comap_comap, map_comap_of_mem] rcases h₂ i with ⟨U, hUo, hU⟩ filter_upwards [preimage_mem_comap <| hUo.mem_nhds <| (hU _).2 rfl] with y hy simpa [hU] using hy @[simp 1100] theorem continuous_sigma_map {f₁ : ι → κ} {f₂ : ∀ i, σ i → τ (f₁ i)} : Continuous (Sigma.map f₁ f₂) ↔ ∀ i, Continuous (f₂ i) := continuous_sigma_iff.trans <| by simp only [Sigma.map, IsEmbedding.sigmaMk.continuous_iff, comp_def] @[continuity, fun_prop] theorem Continuous.sigma_map {f₁ : ι → κ} {f₂ : ∀ i, σ i → τ (f₁ i)} (hf : ∀ i, Continuous (f₂ i)) : Continuous (Sigma.map f₁ f₂) := continuous_sigma_map.2 hf theorem isOpenMap_sigma {f : Sigma σ → X} : IsOpenMap f ↔ ∀ i, IsOpenMap fun a => f ⟨i, a⟩ := by simp only [isOpenMap_iff_nhds_le, Sigma.forall, Sigma.nhds_eq, map_map, comp_def] theorem isOpenMap_sigma_map {f₁ : ι → κ} {f₂ : ∀ i, σ i → τ (f₁ i)} : IsOpenMap (Sigma.map f₁ f₂) ↔ ∀ i, IsOpenMap (f₂ i) := isOpenMap_sigma.trans <| forall_congr' fun i => (@IsOpenEmbedding.sigmaMk _ _ _ (f₁ i)).isOpenMap_iff.symm lemma Topology.isInducing_sigmaMap {f₁ : ι → κ} {f₂ : ∀ i, σ i → τ (f₁ i)} (h₁ : Injective f₁) : IsInducing (Sigma.map f₁ f₂) ↔ ∀ i, IsInducing (f₂ i) := by simp only [isInducing_iff_nhds, Sigma.forall, Sigma.nhds_mk, Sigma.map_mk, ← map_sigma_mk_comap h₁, map_inj sigma_mk_injective] @[deprecated (since := "2024-10-28")] alias inducing_sigma_map := isInducing_sigmaMap lemma Topology.isEmbedding_sigmaMap {f₁ : ι → κ} {f₂ : ∀ i, σ i → τ (f₁ i)} (h : Injective f₁) : IsEmbedding (Sigma.map f₁ f₂) ↔ ∀ i, IsEmbedding (f₂ i) := by simp only [isEmbedding_iff, Injective.sigma_map, isInducing_sigmaMap h, forall_and, h.sigma_map_iff] @[deprecated (since := "2024-10-26")] alias embedding_sigma_map := isEmbedding_sigmaMap lemma Topology.isOpenEmbedding_sigmaMap {f₁ : ι → κ} {f₂ : ∀ i, σ i → τ (f₁ i)} (h : Injective f₁) : IsOpenEmbedding (Sigma.map f₁ f₂) ↔ ∀ i, IsOpenEmbedding (f₂ i) := by simp only [isOpenEmbedding_iff_isEmbedding_isOpenMap, isOpenMap_sigma_map, isEmbedding_sigmaMap h, forall_and] @[deprecated (since := "2024-10-30")] alias isOpenEmbedding_sigma_map := isOpenEmbedding_sigmaMap end Sigma section ULift theorem ULift.isOpen_iff [TopologicalSpace X] {s : Set (ULift.{v} X)} : IsOpen s ↔ IsOpen (ULift.up ⁻¹' s) := by rw [ULift.topologicalSpace, ← Equiv.ulift_apply, ← Equiv.ulift.coinduced_symm, ← isOpen_coinduced] theorem ULift.isClosed_iff [TopologicalSpace X] {s : Set (ULift.{v} X)} : IsClosed s ↔ IsClosed (ULift.up ⁻¹' s) := by rw [← isOpen_compl_iff, ← isOpen_compl_iff, isOpen_iff, preimage_compl] @[continuity, fun_prop] theorem continuous_uliftDown [TopologicalSpace X] : Continuous (ULift.down : ULift.{v, u} X → X) := continuous_induced_dom @[continuity, fun_prop] theorem continuous_uliftUp [TopologicalSpace X] : Continuous (ULift.up : X → ULift.{v, u} X) := continuous_induced_rng.2 continuous_id @[deprecated (since := "2025-02-10")] alias continuous_uLift_down := continuous_uliftDown @[deprecated (since := "2025-02-10")] alias continuous_uLift_up := continuous_uliftUp @[continuity, fun_prop] theorem continuous_uliftMap [TopologicalSpace X] [TopologicalSpace Y] (f : X → Y) (hf : Continuous f) : Continuous (ULift.map f : ULift.{u'} X → ULift.{v'} Y) := by change Continuous (ULift.up ∘ f ∘ ULift.down) fun_prop lemma Topology.IsEmbedding.uliftDown [TopologicalSpace X] : IsEmbedding (ULift.down : ULift.{v, u} X → X) := ⟨⟨rfl⟩, ULift.down_injective⟩ @[deprecated (since := "2024-10-26")] alias embedding_uLift_down := IsEmbedding.uliftDown lemma Topology.IsClosedEmbedding.uliftDown [TopologicalSpace X] : IsClosedEmbedding (ULift.down : ULift.{v, u} X → X) := ⟨.uliftDown, by simp only [ULift.down_surjective.range_eq, isClosed_univ]⟩ @[deprecated (since := "2024-10-30")] alias ULift.isClosedEmbedding_down := IsClosedEmbedding.uliftDown instance [TopologicalSpace X] [DiscreteTopology X] : DiscreteTopology (ULift X) := IsEmbedding.uliftDown.discreteTopology end ULift section Monad variable [TopologicalSpace X] {s : Set X} {t : Set s} theorem IsOpen.trans (ht : IsOpen t) (hs : IsOpen s) : IsOpen (t : Set X) := by rcases isOpen_induced_iff.mp ht with ⟨s', hs', rfl⟩ rw [Subtype.image_preimage_coe] exact hs.inter hs' theorem IsClosed.trans (ht : IsClosed t) (hs : IsClosed s) : IsClosed (t : Set X) := by rcases isClosed_induced_iff.mp ht with ⟨s', hs', rfl⟩ rw [Subtype.image_preimage_coe] exact hs.inter hs' end Monad section NhdsSet variable [TopologicalSpace X] [TopologicalSpace Y] {s : Set X} {t : Set Y} /-- The product of a neighborhood of `s` and a neighborhood of `t` is a neighborhood of `s ×ˢ t`, formulated in terms of a filter inequality. -/ theorem nhdsSet_prod_le (s : Set X) (t : Set Y) : 𝓝ˢ (s ×ˢ t) ≤ 𝓝ˢ s ×ˢ 𝓝ˢ t := ((hasBasis_nhdsSet _).prod (hasBasis_nhdsSet _)).ge_iff.2 fun (_u, _v) ⟨⟨huo, hsu⟩, hvo, htv⟩ ↦ (huo.prod hvo).mem_nhdsSet.2 <| prod_mono hsu htv theorem Filter.eventually_nhdsSet_prod_iff {p : X × Y → Prop} : (∀ᶠ q in 𝓝ˢ (s ×ˢ t), p q) ↔ ∀ x ∈ s, ∀ y ∈ t, ∃ px : X → Prop, (∀ᶠ x' in 𝓝 x, px x') ∧ ∃ py : Y → Prop, (∀ᶠ y' in 𝓝 y, py y') ∧ ∀ {x : X}, px x → ∀ {y : Y}, py y → p (x, y) := by simp_rw [eventually_nhdsSet_iff_forall, forall_prod_set, nhds_prod_eq, eventually_prod_iff] theorem Filter.Eventually.prod_nhdsSet {p : X × Y → Prop} {px : X → Prop} {py : Y → Prop} (hp : ∀ {x : X}, px x → ∀ {y : Y}, py y → p (x, y)) (hs : ∀ᶠ x in 𝓝ˢ s, px x) (ht : ∀ᶠ y in 𝓝ˢ t, py y) : ∀ᶠ q in 𝓝ˢ (s ×ˢ t), p q := nhdsSet_prod_le _ _ (mem_of_superset (prod_mem_prod hs ht) fun _ ⟨hx, hy⟩ ↦ hp hx hy) end NhdsSet
Mathlib/Topology/Constructions.lean
1,428
1,430
/- 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.Finset.Card import Mathlib.Data.Fintype.Basic /-! # Cardinalities of finite types This file defines the cardinality `Fintype.card α` as the number of elements in `(univ : Finset α)`. We also include some elementary results on the values of `Fintype.card` on specific types. ## Main declarations * `Fintype.card α`: Cardinality of a fintype. Equal to `Finset.univ.card`. * `Finite.surjective_of_injective`: an injective function from a finite type to itself is also surjective. -/ assert_not_exists Monoid open Function universe u v variable {α β γ : Type*} open Finset Function namespace Fintype /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [Fintype α] : ℕ := (@univ α _).card theorem subtype_card {p : α → Prop} (s : Finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card { x // p x } (Fintype.subtype s H) = #s := Multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : Finset α) (H : ∀ x : α, x ∈ s ↔ p x) [Fintype { x // p x }] : card { x // p x } = #s := by rw [← subtype_card s H] congr! @[simp] theorem card_ofFinset {p : Set α} (s : Finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @Fintype.card p (ofFinset s H) = #s := Fintype.subtype_card s H theorem card_of_finset' {p : Set α} (s : Finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [Fintype p] : Fintype.card p = #s := by rw [← card_ofFinset s H]; congr! end Fintype namespace Fintype theorem ofEquiv_card [Fintype α] (f : α ≃ β) : @card β (ofEquiv α f) = card α := Multiset.card_map _ _ theorem card_congr {α β} [Fintype α] [Fintype β] (f : α ≃ β) : card α = card β := by rw [← ofEquiv_card f]; congr! @[congr] theorem card_congr' {α β} [Fintype α] [Fintype β] (h : α = β) : card α = card β := card_congr (by rw [h]) /-- Note: this lemma is specifically about `Fintype.ofSubsingleton`. For a statement about arbitrary `Fintype` instances, use either `Fintype.card_le_one_iff_subsingleton` or `Fintype.card_unique`. -/ theorem card_ofSubsingleton (a : α) [Subsingleton α] : @Fintype.card _ (ofSubsingleton a) = 1 := rfl @[simp] theorem card_unique [Unique α] [h : Fintype α] : Fintype.card α = 1 := Subsingleton.elim (ofSubsingleton default) h ▸ card_ofSubsingleton _ /-- Note: this lemma is specifically about `Fintype.ofIsEmpty`. For a statement about arbitrary `Fintype` instances, use `Fintype.card_eq_zero`. -/ theorem card_ofIsEmpty [IsEmpty α] : @Fintype.card α Fintype.ofIsEmpty = 0 := rfl end Fintype namespace Set variable {s t : Set α} -- We use an arbitrary `[Fintype s]` instance here, -- not necessarily coming from a `[Fintype α]`. @[simp] theorem toFinset_card {α : Type*} (s : Set α) [Fintype s] : s.toFinset.card = Fintype.card s := Multiset.card_map Subtype.val Finset.univ.val end Set @[simp] theorem Finset.card_univ [Fintype α] : #(univ : Finset α) = Fintype.card α := rfl theorem Finset.eq_univ_of_card [Fintype α] (s : Finset α) (hs : #s = Fintype.card α) : s = univ := eq_of_subset_of_card_le (subset_univ _) <| by rw [hs, Finset.card_univ] theorem Finset.card_eq_iff_eq_univ [Fintype α] (s : Finset α) : #s = Fintype.card α ↔ s = univ := ⟨s.eq_univ_of_card, by rintro rfl exact Finset.card_univ⟩ theorem Finset.card_le_univ [Fintype α] (s : Finset α) : #s ≤ Fintype.card α := card_le_card (subset_univ s) theorem Finset.card_lt_univ_of_not_mem [Fintype α] {s : Finset α} {x : α} (hx : x ∉ s) : #s < Fintype.card α := card_lt_card ⟨subset_univ s, not_forall.2 ⟨x, fun hx' => hx (hx' <| mem_univ x)⟩⟩ theorem Finset.card_lt_iff_ne_univ [Fintype α] (s : Finset α) : #s < Fintype.card α ↔ s ≠ Finset.univ := s.card_le_univ.lt_iff_ne.trans (not_congr s.card_eq_iff_eq_univ) theorem Finset.card_compl_lt_iff_nonempty [Fintype α] [DecidableEq α] (s : Finset α) : #sᶜ < Fintype.card α ↔ s.Nonempty := sᶜ.card_lt_iff_ne_univ.trans s.compl_ne_univ_iff_nonempty theorem Finset.card_univ_diff [DecidableEq α] [Fintype α] (s : Finset α) : #(univ \ s) = Fintype.card α - #s := Finset.card_sdiff (subset_univ s) theorem Finset.card_compl [DecidableEq α] [Fintype α] (s : Finset α) : #sᶜ = Fintype.card α - #s := Finset.card_univ_diff s @[simp] theorem Finset.card_add_card_compl [DecidableEq α] [Fintype α] (s : Finset α) : #s + #sᶜ = Fintype.card α := by rw [Finset.card_compl, ← Nat.add_sub_assoc (card_le_univ s), Nat.add_sub_cancel_left] @[simp] theorem Finset.card_compl_add_card [DecidableEq α] [Fintype α] (s : Finset α) : #sᶜ + #s = Fintype.card α := by rw [Nat.add_comm, card_add_card_compl] theorem Fintype.card_compl_set [Fintype α] (s : Set α) [Fintype s] [Fintype (↥sᶜ : Sort _)] : Fintype.card (↥sᶜ : Sort _) = Fintype.card α - Fintype.card s := by classical rw [← Set.toFinset_card, ← Set.toFinset_card, ← Finset.card_compl, Set.toFinset_compl] theorem Fintype.card_subtype_eq (y : α) [Fintype { x // x = y }] : Fintype.card { x // x = y } = 1 := Fintype.card_unique theorem Fintype.card_subtype_eq' (y : α) [Fintype { x // y = x }] : Fintype.card { x // y = x } = 1 := Fintype.card_unique theorem Fintype.card_empty : Fintype.card Empty = 0 := rfl theorem Fintype.card_pempty : Fintype.card PEmpty = 0 := rfl theorem Fintype.card_unit : Fintype.card Unit = 1 := rfl @[simp] theorem Fintype.card_punit : Fintype.card PUnit = 1 := rfl @[simp] theorem Fintype.card_bool : Fintype.card Bool = 2 := rfl @[simp] theorem Fintype.card_ulift (α : Type*) [Fintype α] : Fintype.card (ULift α) = Fintype.card α := Fintype.ofEquiv_card _ @[simp] theorem Fintype.card_plift (α : Type*) [Fintype α] : Fintype.card (PLift α) = Fintype.card α := Fintype.ofEquiv_card _ @[simp] theorem Fintype.card_orderDual (α : Type*) [Fintype α] : Fintype.card αᵒᵈ = Fintype.card α := rfl @[simp] theorem Fintype.card_lex (α : Type*) [Fintype α] : Fintype.card (Lex α) = Fintype.card α := rfl -- Note: The extra hypothesis `h` is there so that the rewrite lemma applies, -- no matter what instance of `Fintype (Set.univ : Set α)` is used. @[simp] theorem Fintype.card_setUniv [Fintype α] {h : Fintype (Set.univ : Set α)} : Fintype.card (Set.univ : Set α) = Fintype.card α := by apply Fintype.card_of_finset' simp @[simp] theorem Fintype.card_subtype_true [Fintype α] {h : Fintype {_a : α // True}} : @Fintype.card {_a // True} h = Fintype.card α := by apply Fintype.card_of_subtype simp /-- Given that `α ⊕ β` is a fintype, `α` is also a fintype. This is non-computable as it uses that `Sum.inl` is an injection, but there's no clear inverse if `α` is empty. -/ noncomputable def Fintype.sumLeft {α β} [Fintype (α ⊕ β)] : Fintype α := Fintype.ofInjective (Sum.inl : α → α ⊕ β) Sum.inl_injective /-- Given that `α ⊕ β` is a fintype, `β` is also a fintype. This is non-computable as it uses that `Sum.inr` is an injection, but there's no clear inverse if `β` is empty. -/ noncomputable def Fintype.sumRight {α β} [Fintype (α ⊕ β)] : Fintype β := Fintype.ofInjective (Sum.inr : β → α ⊕ β) Sum.inr_injective theorem Finite.exists_univ_list (α) [Finite α] : ∃ l : List α, l.Nodup ∧ ∀ x : α, x ∈ l := by cases nonempty_fintype α obtain ⟨l, e⟩ := Quotient.exists_rep (@univ α _).1 have := And.intro (@univ α _).2 (@mem_univ_val α _) exact ⟨_, by rwa [← e] at this⟩ theorem List.Nodup.length_le_card {α : Type*} [Fintype α] {l : List α} (h : l.Nodup) : l.length ≤ Fintype.card α := by classical exact List.toFinset_card_of_nodup h ▸ l.toFinset.card_le_univ namespace Fintype variable [Fintype α] [Fintype β] theorem card_le_of_injective (f : α → β) (hf : Function.Injective f) : card α ≤ card β := Finset.card_le_card_of_injOn f (fun _ _ => Finset.mem_univ _) fun _ _ _ _ h => hf h theorem card_le_of_embedding (f : α ↪ β) : card α ≤ card β := card_le_of_injective f f.2 theorem card_lt_of_injective_of_not_mem (f : α → β) (h : Function.Injective f) {b : β} (w : b ∉ Set.range f) : card α < card β := calc card α = (univ.map ⟨f, h⟩).card := (card_map _).symm _ < card β := Finset.card_lt_univ_of_not_mem (x := b) <| by rwa [← mem_coe, coe_map, coe_univ, Set.image_univ] theorem card_lt_of_injective_not_surjective (f : α → β) (h : Function.Injective f) (h' : ¬Function.Surjective f) : card α < card β := let ⟨_y, hy⟩ := not_forall.1 h' card_lt_of_injective_of_not_mem f h hy theorem card_le_of_surjective (f : α → β) (h : Function.Surjective f) : card β ≤ card α := card_le_of_injective _ (Function.injective_surjInv h) theorem card_range_le {α β : Type*} (f : α → β) [Fintype α] [Fintype (Set.range f)] : Fintype.card (Set.range f) ≤ Fintype.card α := Fintype.card_le_of_surjective (fun a => ⟨f a, by simp⟩) fun ⟨_, a, ha⟩ => ⟨a, by simpa using ha⟩ theorem card_range {α β F : Type*} [FunLike F α β] [EmbeddingLike F α β] (f : F) [Fintype α] [Fintype (Set.range f)] : Fintype.card (Set.range f) = Fintype.card α := Eq.symm <| Fintype.card_congr <| Equiv.ofInjective _ <| EmbeddingLike.injective f theorem card_eq_zero_iff : card α = 0 ↔ IsEmpty α := by rw [card, Finset.card_eq_zero, univ_eq_empty_iff] @[simp] theorem card_eq_zero [IsEmpty α] : card α = 0 := card_eq_zero_iff.2 ‹_› alias card_of_isEmpty := card_eq_zero /-- A `Fintype` with cardinality zero is equivalent to `Empty`. -/ def cardEqZeroEquivEquivEmpty : card α = 0 ≃ (α ≃ Empty) := (Equiv.ofIff card_eq_zero_iff).trans (Equiv.equivEmptyEquiv α).symm theorem card_pos_iff : 0 < card α ↔ Nonempty α := Nat.pos_iff_ne_zero.trans <| not_iff_comm.mp <| not_nonempty_iff.trans card_eq_zero_iff.symm theorem card_pos [h : Nonempty α] : 0 < card α := card_pos_iff.mpr h @[simp] theorem card_ne_zero [Nonempty α] : card α ≠ 0 := _root_.ne_of_gt card_pos instance [Nonempty α] : NeZero (card α) := ⟨card_ne_zero⟩ theorem existsUnique_iff_card_one {α} [Fintype α] (p : α → Prop) [DecidablePred p] : (∃! a : α, p a) ↔ #{x | p x} = 1 := by rw [Finset.card_eq_one] refine exists_congr fun x => ?_ simp only [forall_true_left, Subset.antisymm_iff, subset_singleton_iff', singleton_subset_iff, true_and, and_comm, mem_univ, mem_filter] @[deprecated (since := "2024-12-17")] alias exists_unique_iff_card_one := existsUnique_iff_card_one nonrec theorem two_lt_card_iff : 2 < card α ↔ ∃ a b c : α, a ≠ b ∧ a ≠ c ∧ b ≠ c := by simp_rw [← Finset.card_univ, two_lt_card_iff, mem_univ, true_and] theorem card_of_bijective {f : α → β} (hf : Bijective f) : card α = card β := card_congr (Equiv.ofBijective f hf) end Fintype namespace Finite variable [Finite α] theorem surjective_of_injective {f : α → α} (hinj : Injective f) : Surjective f := by intro x have := Classical.propDecidable cases nonempty_fintype α have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_rfl) have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ x obtain ⟨y, h⟩ := mem_image.1 h₂ exact ⟨y, h.2⟩ theorem injective_iff_surjective {f : α → α} : Injective f ↔ Surjective f := ⟨surjective_of_injective, fun hsurj => HasLeftInverse.injective ⟨surjInv hsurj, leftInverse_of_surjective_of_rightInverse
(surjective_of_injective (injective_surjInv _)) (rightInverse_surjInv _)⟩⟩ theorem injective_iff_bijective {f : α → α} : Injective f ↔ Bijective f := by simp [Bijective, injective_iff_surjective] theorem surjective_iff_bijective {f : α → α} : Surjective f ↔ Bijective f := by simp [Bijective, injective_iff_surjective]
Mathlib/Data/Fintype/Card.lean
315
322
/- 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.Set.Prod import Mathlib.Logic.Equiv.Fin.Basic import Mathlib.ModelTheory.LanguageMap import Mathlib.Algebra.Order.Group.Nat /-! # Basics on First-Order Syntax This file defines first-order terms, formulas, sentences, and theories in a style inspired by the [Flypitch project](https://flypitch.github.io/). ## Main Definitions - A `FirstOrder.Language.Term` is defined so that `L.Term α` is the type of `L`-terms with free variables indexed by `α`. - A `FirstOrder.Language.Formula` is defined so that `L.Formula α` is the type of `L`-formulas with free variables indexed by `α`. - A `FirstOrder.Language.Sentence` is a formula with no free variables. - A `FirstOrder.Language.Theory` is a set of sentences. - The variables of terms and formulas can be relabelled with `FirstOrder.Language.Term.relabel`, `FirstOrder.Language.BoundedFormula.relabel`, and `FirstOrder.Language.Formula.relabel`. - Given an operation on terms and an operation on relations, `FirstOrder.Language.BoundedFormula.mapTermRel` gives an operation on formulas. - `FirstOrder.Language.BoundedFormula.castLE` adds more `Fin`-indexed variables. - `FirstOrder.Language.BoundedFormula.liftAt` raises the indexes of the `Fin`-indexed variables above a particular index. - `FirstOrder.Language.Term.subst` and `FirstOrder.Language.BoundedFormula.subst` substitute variables with given terms. - Language maps can act on syntactic objects with functions such as `FirstOrder.Language.LHom.onFormula`. - `FirstOrder.Language.Term.constantsVarsEquiv` and `FirstOrder.Language.BoundedFormula.constantsVarsEquiv` switch terms and formulas between having constants in the language and having extra variables indexed by the same type. ## Implementation Notes - Formulas use a modified version of de Bruijn variables. Specifically, a `L.BoundedFormula α n` is a formula with some variables indexed by a type `α`, which cannot be quantified over, and some indexed by `Fin n`, which can. For any `φ : L.BoundedFormula α (n + 1)`, we define the formula `∀' φ : L.BoundedFormula α n` by universally quantifying over the variable indexed by `n : Fin (n + 1)`. ## 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 w u' v' namespace FirstOrder namespace Language variable (L : Language.{u, v}) {L' : Language} variable {M : Type w} {α : Type u'} {β : Type v'} {γ : Type*} open FirstOrder open Structure Fin /-- A term on `α` is either a variable indexed by an element of `α` or a function symbol applied to simpler terms. -/ inductive Term (α : Type u') : Type max u u' | var : α → Term α | func : ∀ {l : ℕ} (_f : L.Functions l) (_ts : Fin l → Term α), Term α export Term (var func) variable {L} namespace Term instance instDecidableEq [DecidableEq α] [∀ n, DecidableEq (L.Functions n)] : DecidableEq (L.Term α) | .var a, .var b => decidable_of_iff (a = b) <| by simp | @Term.func _ _ m f xs, @Term.func _ _ n g ys => if h : m = n then letI : DecidableEq (L.Term α) := instDecidableEq decidable_of_iff (f = h ▸ g ∧ ∀ i : Fin m, xs i = ys (Fin.cast h i)) <| by subst h simp [funext_iff] else .isFalse <| by simp [h] | .var _, .func _ _ | .func _ _, .var _ => .isFalse <| by simp open Finset /-- The `Finset` of variables used in a given term. -/ @[simp] def varFinset [DecidableEq α] : L.Term α → Finset α | var i => {i} | func _f ts => univ.biUnion fun i => (ts i).varFinset /-- The `Finset` of variables from the left side of a sum used in a given term. -/ @[simp] def varFinsetLeft [DecidableEq α] : L.Term (α ⊕ β) → Finset α | var (Sum.inl i) => {i} | var (Sum.inr _i) => ∅ | func _f ts => univ.biUnion fun i => (ts i).varFinsetLeft /-- Relabels a term's variables along a particular function. -/ @[simp] def relabel (g : α → β) : L.Term α → L.Term β | var i => var (g i) | func f ts => func f fun {i} => (ts i).relabel g theorem relabel_id (t : L.Term α) : t.relabel id = t := by induction t with | var => rfl | func _ _ ih => simp [ih] @[simp] theorem relabel_id_eq_id : (Term.relabel id : L.Term α → L.Term α) = id := funext relabel_id @[simp] theorem relabel_relabel (f : α → β) (g : β → γ) (t : L.Term α) : (t.relabel f).relabel g = t.relabel (g ∘ f) := by induction t with | var => rfl | func _ _ ih => simp [ih] @[simp] theorem relabel_comp_relabel (f : α → β) (g : β → γ) : (Term.relabel g ∘ Term.relabel f : L.Term α → L.Term γ) = Term.relabel (g ∘ f) := funext (relabel_relabel f g) /-- Relabels a term's variables along a bijection. -/ @[simps] def relabelEquiv (g : α ≃ β) : L.Term α ≃ L.Term β := ⟨relabel g, relabel g.symm, fun t => by simp, fun t => by simp⟩ /-- Restricts a term to use only a set of the given variables. -/ def restrictVar [DecidableEq α] : ∀ (t : L.Term α) (_f : t.varFinset → β), L.Term β | var a, f => var (f ⟨a, mem_singleton_self a⟩) | func F ts, f => func F fun i => (ts i).restrictVar (f ∘ Set.inclusion (subset_biUnion_of_mem (fun i => varFinset (ts i)) (mem_univ i))) /-- Restricts a term to use only a set of the given variables on the left side of a sum. -/ def restrictVarLeft [DecidableEq α] {γ : Type*} : ∀ (t : L.Term (α ⊕ γ)) (_f : t.varFinsetLeft → β), L.Term (β ⊕ γ) | var (Sum.inl a), f => var (Sum.inl (f ⟨a, mem_singleton_self a⟩)) | var (Sum.inr a), _f => var (Sum.inr a) | func F ts, f => func F fun i => (ts i).restrictVarLeft (f ∘ Set.inclusion (subset_biUnion_of_mem (fun i => varFinsetLeft (ts i)) (mem_univ i))) end Term /-- The representation of a constant symbol as a term. -/ def Constants.term (c : L.Constants) : L.Term α := func c default /-- Applies a unary function to a term. -/ def Functions.apply₁ (f : L.Functions 1) (t : L.Term α) : L.Term α := func f ![t] /-- Applies a binary function to two terms. -/ def Functions.apply₂ (f : L.Functions 2) (t₁ t₂ : L.Term α) : L.Term α := func f ![t₁, t₂] namespace Term /-- Sends a term with constants to a term with extra variables. -/ @[simp] def constantsToVars : L[[γ]].Term α → L.Term (γ ⊕ α) | var a => var (Sum.inr a) | @func _ _ 0 f ts => Sum.casesOn f (fun f => func f fun i => (ts i).constantsToVars) fun c => var (Sum.inl c) | @func _ _ (_n + 1) f ts => Sum.casesOn f (fun f => func f fun i => (ts i).constantsToVars) fun c => isEmptyElim c /-- Sends a term with extra variables to a term with constants. -/ @[simp] def varsToConstants : L.Term (γ ⊕ α) → L[[γ]].Term α | var (Sum.inr a) => var a | var (Sum.inl c) => Constants.term (Sum.inr c) | func f ts => func (Sum.inl f) fun i => (ts i).varsToConstants /-- A bijection between terms with constants and terms with extra variables. -/ @[simps] def constantsVarsEquiv : L[[γ]].Term α ≃ L.Term (γ ⊕ α) := ⟨constantsToVars, varsToConstants, by intro t induction t with | var => rfl | @func n f _ ih => cases n · cases f · simp [constantsToVars, varsToConstants, ih] · simp [constantsToVars, varsToConstants, Constants.term, eq_iff_true_of_subsingleton] · obtain - | f := f · simp [constantsToVars, varsToConstants, ih] · exact isEmptyElim f, by intro t induction t with | var x => cases x <;> rfl | @func n f _ ih => cases n <;> · simp [varsToConstants, constantsToVars, ih]⟩ /-- A bijection between terms with constants and terms with extra variables. -/ def constantsVarsEquivLeft : L[[γ]].Term (α ⊕ β) ≃ L.Term ((γ ⊕ α) ⊕ β) := constantsVarsEquiv.trans (relabelEquiv (Equiv.sumAssoc _ _ _)).symm @[simp] theorem constantsVarsEquivLeft_apply (t : L[[γ]].Term (α ⊕ β)) : constantsVarsEquivLeft t = (constantsToVars t).relabel (Equiv.sumAssoc _ _ _).symm := rfl @[simp] theorem constantsVarsEquivLeft_symm_apply (t : L.Term ((γ ⊕ α) ⊕ β)) : constantsVarsEquivLeft.symm t = varsToConstants (t.relabel (Equiv.sumAssoc _ _ _)) := rfl instance inhabitedOfVar [Inhabited α] : Inhabited (L.Term α) := ⟨var default⟩ instance inhabitedOfConstant [Inhabited L.Constants] : Inhabited (L.Term α) := ⟨(default : L.Constants).term⟩ /-- Raises all of the `Fin`-indexed variables of a term greater than or equal to `m` by `n'`. -/ def liftAt {n : ℕ} (n' m : ℕ) : L.Term (α ⊕ (Fin n)) → L.Term (α ⊕ (Fin (n + n'))) := relabel (Sum.map id fun i => if ↑i < m then Fin.castAdd n' i else Fin.addNat i n') /-- Substitutes the variables in a given term with terms. -/ @[simp] def subst : L.Term α → (α → L.Term β) → L.Term β | var a, tf => tf a | func f ts, tf => func f fun i => (ts i).subst tf end Term /-- `&n` is notation for the `n`-th free variable of a bounded formula. -/ scoped[FirstOrder] prefix:arg "&" => FirstOrder.Language.Term.var ∘ Sum.inr namespace LHom open Term /-- Maps a term's symbols along a language map. -/ @[simp] def onTerm (φ : L →ᴸ L') : L.Term α → L'.Term α | var i => var i | func f ts => func (φ.onFunction f) fun i => onTerm φ (ts i) @[simp] theorem id_onTerm : ((LHom.id L).onTerm : L.Term α → L.Term α) = id := by ext t induction t with | var => rfl | func _ _ ih => simp_rw [onTerm, ih]; rfl @[simp] theorem comp_onTerm {L'' : Language} (φ : L' →ᴸ L'') (ψ : L →ᴸ L') : ((φ.comp ψ).onTerm : L.Term α → L''.Term α) = φ.onTerm ∘ ψ.onTerm := by ext t induction t with | var => rfl | func _ _ ih => simp_rw [onTerm, ih]; rfl end LHom /-- Maps a term's symbols along a language equivalence. -/ @[simps] def LEquiv.onTerm (φ : L ≃ᴸ L') : L.Term α ≃ L'.Term α where toFun := φ.toLHom.onTerm invFun := φ.invLHom.onTerm left_inv := by rw [Function.leftInverse_iff_comp, ← LHom.comp_onTerm, φ.left_inv, LHom.id_onTerm] right_inv := by rw [Function.rightInverse_iff_comp, ← LHom.comp_onTerm, φ.right_inv, LHom.id_onTerm] /-- Maps a term's symbols along a language equivalence. Deprecated in favor of `LEquiv.onTerm`. -/ @[deprecated LEquiv.onTerm (since := "2025-03-31")] alias Lequiv.onTerm := LEquiv.onTerm variable (L) (α) /-- `BoundedFormula α n` is the type of formulas with free variables indexed by `α` and up to `n` additional free variables. -/ inductive BoundedFormula : ℕ → Type max u v u' | falsum {n} : BoundedFormula n | equal {n} (t₁ t₂ : L.Term (α ⊕ (Fin n))) : BoundedFormula n | rel {n l : ℕ} (R : L.Relations l) (ts : Fin l → L.Term (α ⊕ (Fin n))) : BoundedFormula n /-- The implication between two bounded formulas -/ | imp {n} (f₁ f₂ : BoundedFormula n) : BoundedFormula n /-- The universal quantifier over bounded formulas -/ | all {n} (f : BoundedFormula (n + 1)) : BoundedFormula n /-- `Formula α` is the type of formulas with all free variables indexed by `α`. -/ abbrev Formula := L.BoundedFormula α 0 /-- A sentence is a formula with no free variables. -/ abbrev Sentence := L.Formula Empty /-- A theory is a set of sentences. -/ abbrev Theory := Set L.Sentence variable {L} {α} {n : ℕ} /-- Applies a relation to terms as a bounded formula. -/ def Relations.boundedFormula {l : ℕ} (R : L.Relations n) (ts : Fin n → L.Term (α ⊕ (Fin l))) : L.BoundedFormula α l := BoundedFormula.rel R ts /-- Applies a unary relation to a term as a bounded formula. -/ def Relations.boundedFormula₁ (r : L.Relations 1) (t : L.Term (α ⊕ (Fin n))) : L.BoundedFormula α n := r.boundedFormula ![t] /-- Applies a binary relation to two terms as a bounded formula. -/ def Relations.boundedFormula₂ (r : L.Relations 2) (t₁ t₂ : L.Term (α ⊕ (Fin n))) : L.BoundedFormula α n := r.boundedFormula ![t₁, t₂] /-- The equality of two terms as a bounded formula. -/ def Term.bdEqual (t₁ t₂ : L.Term (α ⊕ (Fin n))) : L.BoundedFormula α n := BoundedFormula.equal t₁ t₂ /-- Applies a relation to terms as a bounded formula. -/ def Relations.formula (R : L.Relations n) (ts : Fin n → L.Term α) : L.Formula α := R.boundedFormula fun i => (ts i).relabel Sum.inl /-- Applies a unary relation to a term as a formula. -/ def Relations.formula₁ (r : L.Relations 1) (t : L.Term α) : L.Formula α := r.formula ![t] /-- Applies a binary relation to two terms as a formula. -/ def Relations.formula₂ (r : L.Relations 2) (t₁ t₂ : L.Term α) : L.Formula α := r.formula ![t₁, t₂] /-- The equality of two terms as a first-order formula. -/ def Term.equal (t₁ t₂ : L.Term α) : L.Formula α := (t₁.relabel Sum.inl).bdEqual (t₂.relabel Sum.inl) namespace BoundedFormula instance : Inhabited (L.BoundedFormula α n) := ⟨falsum⟩ instance : Bot (L.BoundedFormula α n) := ⟨falsum⟩ /-- The negation of a bounded formula is also a bounded formula. -/ @[match_pattern] protected def not (φ : L.BoundedFormula α n) : L.BoundedFormula α n := φ.imp ⊥ /-- Puts an `∃` quantifier on a bounded formula. -/ @[match_pattern] protected def ex (φ : L.BoundedFormula α (n + 1)) : L.BoundedFormula α n := φ.not.all.not instance : Top (L.BoundedFormula α n) := ⟨BoundedFormula.not ⊥⟩ instance : Min (L.BoundedFormula α n) := ⟨fun f g => (f.imp g.not).not⟩ instance : Max (L.BoundedFormula α n) := ⟨fun f g => f.not.imp g⟩ /-- The biimplication between two bounded formulas. -/ protected def iff (φ ψ : L.BoundedFormula α n) := φ.imp ψ ⊓ ψ.imp φ open Finset /-- The `Finset` of variables used in a given formula. -/ @[simp] def freeVarFinset [DecidableEq α] : ∀ {n}, L.BoundedFormula α n → Finset α | _n, falsum => ∅ | _n, equal t₁ t₂ => t₁.varFinsetLeft ∪ t₂.varFinsetLeft | _n, rel _R ts => univ.biUnion fun i => (ts i).varFinsetLeft | _n, imp f₁ f₂ => f₁.freeVarFinset ∪ f₂.freeVarFinset | _n, all f => f.freeVarFinset /-- Casts `L.BoundedFormula α m` as `L.BoundedFormula α n`, where `m ≤ n`. -/ @[simp] def castLE : ∀ {m n : ℕ} (_h : m ≤ n), L.BoundedFormula α m → L.BoundedFormula α n | _m, _n, _h, falsum => falsum | _m, _n, h, equal t₁ t₂ => equal (t₁.relabel (Sum.map id (Fin.castLE h))) (t₂.relabel (Sum.map id (Fin.castLE h))) | _m, _n, h, rel R ts => rel R (Term.relabel (Sum.map id (Fin.castLE h)) ∘ ts) | _m, _n, h, imp f₁ f₂ => (f₁.castLE h).imp (f₂.castLE h) | _m, _n, h, all f => (f.castLE (add_le_add_right h 1)).all @[simp] theorem castLE_rfl {n} (h : n ≤ n) (φ : L.BoundedFormula α n) : φ.castLE h = φ := by induction φ with | falsum => rfl | equal => simp [Fin.castLE_of_eq] | rel => simp [Fin.castLE_of_eq] | imp _ _ ih1 ih2 => simp [Fin.castLE_of_eq, ih1, ih2] | all _ ih3 => simp [Fin.castLE_of_eq, ih3] @[simp] theorem castLE_castLE {k m n} (km : k ≤ m) (mn : m ≤ n) (φ : L.BoundedFormula α k) : (φ.castLE km).castLE mn = φ.castLE (km.trans mn) := by revert m n induction φ with | falsum => intros; rfl | equal => simp | rel => intros simp only [castLE, eq_self_iff_true, heq_iff_eq] rw [← Function.comp_assoc, Term.relabel_comp_relabel] simp | imp _ _ ih1 ih2 => simp [ih1, ih2] | all _ ih3 => intros; simp only [castLE, ih3] @[simp] theorem castLE_comp_castLE {k m n} (km : k ≤ m) (mn : m ≤ n) : (BoundedFormula.castLE mn ∘ BoundedFormula.castLE km : L.BoundedFormula α k → L.BoundedFormula α n) = BoundedFormula.castLE (km.trans mn) := funext (castLE_castLE km mn) /-- Restricts a bounded formula to only use a particular set of free variables. -/ def restrictFreeVar [DecidableEq α] : ∀ {n : ℕ} (φ : L.BoundedFormula α n) (_f : φ.freeVarFinset → β), L.BoundedFormula β n | _n, falsum, _f => falsum | _n, equal t₁ t₂, f => equal (t₁.restrictVarLeft (f ∘ Set.inclusion subset_union_left)) (t₂.restrictVarLeft (f ∘ Set.inclusion subset_union_right)) | _n, rel R ts, f => rel R fun i => (ts i).restrictVarLeft (f ∘ Set.inclusion (subset_biUnion_of_mem (fun i => Term.varFinsetLeft (ts i)) (mem_univ i))) | _n, imp φ₁ φ₂, f => (φ₁.restrictFreeVar (f ∘ Set.inclusion subset_union_left)).imp (φ₂.restrictFreeVar (f ∘ Set.inclusion subset_union_right)) | _n, all φ, f => (φ.restrictFreeVar f).all /-- Places universal quantifiers on all extra variables of a bounded formula. -/ def alls : ∀ {n}, L.BoundedFormula α n → L.Formula α | 0, φ => φ | _n + 1, φ => φ.all.alls /-- Places existential quantifiers on all extra variables of a bounded formula. -/ def exs : ∀ {n}, L.BoundedFormula α n → L.Formula α | 0, φ => φ | _n + 1, φ => φ.ex.exs /-- Maps bounded formulas along a map of terms and a map of relations. -/ def mapTermRel {g : ℕ → ℕ} (ft : ∀ n, L.Term (α ⊕ (Fin n)) → L'.Term (β ⊕ (Fin (g n)))) (fr : ∀ n, L.Relations n → L'.Relations n) (h : ∀ n, L'.BoundedFormula β (g (n + 1)) → L'.BoundedFormula β (g n + 1)) : ∀ {n}, L.BoundedFormula α n → L'.BoundedFormula β (g n) | _n, falsum => falsum | _n, equal t₁ t₂ => equal (ft _ t₁) (ft _ t₂) | _n, rel R ts => rel (fr _ R) fun i => ft _ (ts i) | _n, imp φ₁ φ₂ => (φ₁.mapTermRel ft fr h).imp (φ₂.mapTermRel ft fr h) | n, all φ => (h n (φ.mapTermRel ft fr h)).all /-- Raises all of the `Fin`-indexed variables of a formula greater than or equal to `m` by `n'`. -/ def liftAt : ∀ {n : ℕ} (n' _m : ℕ), L.BoundedFormula α n → L.BoundedFormula α (n + n') := fun {_} n' m φ => φ.mapTermRel (fun _ t => t.liftAt n' m) (fun _ => id) fun _ => castLE (by rw [add_assoc, add_comm 1, add_assoc]) @[simp] theorem mapTermRel_mapTermRel {L'' : Language} (ft : ∀ n, L.Term (α ⊕ (Fin n)) → L'.Term (β ⊕ (Fin n))) (fr : ∀ n, L.Relations n → L'.Relations n) (ft' : ∀ n, L'.Term (β ⊕ Fin n) → L''.Term (γ ⊕ (Fin n))) (fr' : ∀ n, L'.Relations n → L''.Relations n) {n} (φ : L.BoundedFormula α n) : ((φ.mapTermRel ft fr fun _ => id).mapTermRel ft' fr' fun _ => id) = φ.mapTermRel (fun _ => ft' _ ∘ ft _) (fun _ => fr' _ ∘ fr _) fun _ => id := by induction φ with | falsum => rfl | equal => simp [mapTermRel] | rel => simp [mapTermRel] | imp _ _ ih1 ih2 => simp [mapTermRel, ih1, ih2] | all _ ih3 => simp [mapTermRel, ih3] @[simp] theorem mapTermRel_id_id_id {n} (φ : L.BoundedFormula α n) : (φ.mapTermRel (fun _ => id) (fun _ => id) fun _ => id) = φ := by induction φ with | falsum => rfl | equal => simp [mapTermRel] | rel => simp [mapTermRel] | imp _ _ ih1 ih2 => simp [mapTermRel, ih1, ih2] | all _ ih3 => simp [mapTermRel, ih3] /-- An equivalence of bounded formulas given by an equivalence of terms and an equivalence of relations. -/ @[simps] def mapTermRelEquiv (ft : ∀ n, L.Term (α ⊕ (Fin n)) ≃ L'.Term (β ⊕ (Fin n))) (fr : ∀ n, L.Relations n ≃ L'.Relations n) {n} : L.BoundedFormula α n ≃ L'.BoundedFormula β n := ⟨mapTermRel (fun n => ft n) (fun n => fr n) fun _ => id, mapTermRel (fun n => (ft n).symm) (fun n => (fr n).symm) fun _ => id, fun φ => by simp, fun φ => by simp⟩ /-- A function to help relabel the variables in bounded formulas. -/ def relabelAux (g : α → β ⊕ (Fin n)) (k : ℕ) : α ⊕ (Fin k) → β ⊕ (Fin (n + k)) := Sum.map id finSumFinEquiv ∘ Equiv.sumAssoc _ _ _ ∘ Sum.map g id @[simp] theorem sumElim_comp_relabelAux {m : ℕ} {g : α → β ⊕ (Fin n)} {v : β → M} {xs : Fin (n + m) → M} : Sum.elim v xs ∘ relabelAux g m = Sum.elim (Sum.elim v (xs ∘ castAdd m) ∘ g) (xs ∘ natAdd n) := by ext x rcases x with x | x · simp only [BoundedFormula.relabelAux, Function.comp_apply, Sum.map_inl, Sum.elim_inl] rcases g x with l | r <;> simp · simp [BoundedFormula.relabelAux] @[deprecated (since := "2025-02-21")] alias sum_elim_comp_relabelAux := sumElim_comp_relabelAux @[simp] theorem relabelAux_sumInl (k : ℕ) : relabelAux (Sum.inl : α → α ⊕ (Fin n)) k = Sum.map id (natAdd n) := by ext x cases x <;> · simp [relabelAux] @[deprecated (since := "2025-02-21")] alias relabelAux_sum_inl := relabelAux_sumInl /-- Relabels a bounded formula's variables along a particular function. -/ def relabel (g : α → β ⊕ (Fin n)) {k} (φ : L.BoundedFormula α k) : L.BoundedFormula β (n + k) := φ.mapTermRel (fun _ t => t.relabel (relabelAux g _)) (fun _ => id) fun _ => castLE (ge_of_eq (add_assoc _ _ _)) /-- Relabels a bounded formula's free variables along a bijection. -/ def relabelEquiv (g : α ≃ β) {k} : L.BoundedFormula α k ≃ L.BoundedFormula β k := mapTermRelEquiv (fun _n => Term.relabelEquiv (g.sumCongr (_root_.Equiv.refl _))) fun _n => _root_.Equiv.refl _ @[simp] theorem relabel_falsum (g : α → β ⊕ (Fin n)) {k} : (falsum : L.BoundedFormula α k).relabel g = falsum := rfl @[simp] theorem relabel_bot (g : α → β ⊕ (Fin n)) {k} : (⊥ : L.BoundedFormula α k).relabel g = ⊥ := rfl @[simp] theorem relabel_imp (g : α → β ⊕ (Fin n)) {k} (φ ψ : L.BoundedFormula α k) : (φ.imp ψ).relabel g = (φ.relabel g).imp (ψ.relabel g) := rfl @[simp] theorem relabel_not (g : α → β ⊕ (Fin n)) {k} (φ : L.BoundedFormula α k) : φ.not.relabel g = (φ.relabel g).not := by simp [BoundedFormula.not] @[simp] theorem relabel_all (g : α → β ⊕ (Fin n)) {k} (φ : L.BoundedFormula α (k + 1)) : φ.all.relabel g = (φ.relabel g).all := by rw [relabel, mapTermRel, relabel] simp @[simp] theorem relabel_ex (g : α → β ⊕ (Fin n)) {k} (φ : L.BoundedFormula α (k + 1)) : φ.ex.relabel g = (φ.relabel g).ex := by simp [BoundedFormula.ex] @[simp] theorem relabel_sumInl (φ : L.BoundedFormula α n) : (φ.relabel Sum.inl : L.BoundedFormula α (0 + n)) = φ.castLE (ge_of_eq (zero_add n)) := by simp only [relabel, relabelAux_sumInl] induction φ with | falsum => rfl | equal => simp [Fin.natAdd_zero, castLE_of_eq, mapTermRel] | rel => simp [Fin.natAdd_zero, castLE_of_eq, mapTermRel]; rfl | imp _ _ ih1 ih2 => simp_all [mapTermRel] | all _ ih3 => simp_all [mapTermRel] @[deprecated (since := "2025-02-21")] alias relabel_sum_inl := relabel_sumInl /-- Substitutes the variables in a given formula with terms. -/ def subst {n : ℕ} (φ : L.BoundedFormula α n) (f : α → L.Term β) : L.BoundedFormula β n := φ.mapTermRel (fun _ t => t.subst (Sum.elim (Term.relabel Sum.inl ∘ f) (var ∘ Sum.inr))) (fun _ => id) fun _ => id /-- A bijection sending formulas with constants to formulas with extra variables. -/ def constantsVarsEquiv : L[[γ]].BoundedFormula α n ≃ L.BoundedFormula (γ ⊕ α) n := mapTermRelEquiv (fun _ => Term.constantsVarsEquivLeft) fun _ => Equiv.sumEmpty _ _ /-- Turns the extra variables of a bounded formula into free variables. -/ @[simp] def toFormula : ∀ {n : ℕ}, L.BoundedFormula α n → L.Formula (α ⊕ (Fin n)) | _n, falsum => falsum | _n, equal t₁ t₂ => t₁.equal t₂ | _n, rel R ts => R.formula ts | _n, imp φ₁ φ₂ => φ₁.toFormula.imp φ₂.toFormula | _n, all φ => (φ.toFormula.relabel (Sum.elim (Sum.inl ∘ Sum.inl) (Sum.map Sum.inr id ∘ finSumFinEquiv.symm))).all /-- Take the disjunction of a finite set of formulas -/ noncomputable def iSup [Finite β] (f : β → L.BoundedFormula α n) : L.BoundedFormula α n := let _ := Fintype.ofFinite β ((Finset.univ : Finset β).toList.map f).foldr (· ⊔ ·) ⊥ /-- Take the conjunction of a finite set of formulas -/ noncomputable def iInf [Finite β] (f : β → L.BoundedFormula α n) : L.BoundedFormula α n := let _ := Fintype.ofFinite β ((Finset.univ : Finset β).toList.map f).foldr (· ⊓ ·) ⊤ end BoundedFormula namespace LHom open BoundedFormula /-- Maps a bounded formula's symbols along a language map. -/ @[simp] def onBoundedFormula (g : L →ᴸ L') : ∀ {k : ℕ}, L.BoundedFormula α k → L'.BoundedFormula α k | _k, falsum => falsum | _k, equal t₁ t₂ => (g.onTerm t₁).bdEqual (g.onTerm t₂) | _k, rel R ts => (g.onRelation R).boundedFormula (g.onTerm ∘ ts) | _k, imp f₁ f₂ => (onBoundedFormula g f₁).imp (onBoundedFormula g f₂) | _k, all f => (onBoundedFormula g f).all @[simp] theorem id_onBoundedFormula : ((LHom.id L).onBoundedFormula : L.BoundedFormula α n → L.BoundedFormula α n) = id := by ext f induction f with | falsum => rfl | equal => rw [onBoundedFormula, LHom.id_onTerm, id, id, id, Term.bdEqual] | rel => rw [onBoundedFormula, LHom.id_onTerm]; rfl | imp _ _ ih1 ih2 => rw [onBoundedFormula, ih1, ih2, id, id, id] | all _ ih3 => rw [onBoundedFormula, ih3, id, id] @[simp] theorem comp_onBoundedFormula {L'' : Language} (φ : L' →ᴸ L'') (ψ : L →ᴸ L') : ((φ.comp ψ).onBoundedFormula : L.BoundedFormula α n → L''.BoundedFormula α n) = φ.onBoundedFormula ∘ ψ.onBoundedFormula := by ext f induction f with | falsum => rfl | equal => simp [Term.bdEqual] | rel => simp only [onBoundedFormula, comp_onRelation, comp_onTerm, Function.comp_apply]; rfl | imp _ _ ih1 ih2 => simp only [onBoundedFormula, Function.comp_apply, ih1, ih2, eq_self_iff_true, and_self_iff] | all _ ih3 => simp only [ih3, onBoundedFormula, Function.comp_apply] /-- Maps a formula's symbols along a language map. -/ def onFormula (g : L →ᴸ L') : L.Formula α → L'.Formula α := g.onBoundedFormula /-- Maps a sentence's symbols along a language map. -/ def onSentence (g : L →ᴸ L') : L.Sentence → L'.Sentence := g.onFormula /-- Maps a theory's symbols along a language map. -/ def onTheory (g : L →ᴸ L') (T : L.Theory) : L'.Theory := g.onSentence '' T @[simp] theorem mem_onTheory {g : L →ᴸ L'} {T : L.Theory} {φ : L'.Sentence} : φ ∈ g.onTheory T ↔ ∃ φ₀, φ₀ ∈ T ∧ g.onSentence φ₀ = φ := Set.mem_image _ _ _ end LHom namespace LEquiv /-- Maps a bounded formula's symbols along a language equivalence. -/ @[simps] def onBoundedFormula (φ : L ≃ᴸ L') : L.BoundedFormula α n ≃ L'.BoundedFormula α n where toFun := φ.toLHom.onBoundedFormula invFun := φ.invLHom.onBoundedFormula left_inv := by rw [Function.leftInverse_iff_comp, ← LHom.comp_onBoundedFormula, φ.left_inv, LHom.id_onBoundedFormula] right_inv := by rw [Function.rightInverse_iff_comp, ← LHom.comp_onBoundedFormula, φ.right_inv, LHom.id_onBoundedFormula] theorem onBoundedFormula_symm (φ : L ≃ᴸ L') : (φ.onBoundedFormula.symm : L'.BoundedFormula α n ≃ L.BoundedFormula α n) = φ.symm.onBoundedFormula := rfl /-- Maps a formula's symbols along a language equivalence. -/ def onFormula (φ : L ≃ᴸ L') : L.Formula α ≃ L'.Formula α := φ.onBoundedFormula @[simp] theorem onFormula_apply (φ : L ≃ᴸ L') : (φ.onFormula : L.Formula α → L'.Formula α) = φ.toLHom.onFormula := rfl @[simp] theorem onFormula_symm (φ : L ≃ᴸ L') : (φ.onFormula.symm : L'.Formula α ≃ L.Formula α) = φ.symm.onFormula := rfl /-- Maps a sentence's symbols along a language equivalence. -/ @[simps!] def onSentence (φ : L ≃ᴸ L') : L.Sentence ≃ L'.Sentence := φ.onFormula end LEquiv @[inherit_doc] scoped[FirstOrder] infixl:88 " =' " => FirstOrder.Language.Term.bdEqual -- input \~- or \simeq @[inherit_doc] scoped[FirstOrder] infixr:62 " ⟹ " => FirstOrder.Language.BoundedFormula.imp -- input \==> @[inherit_doc] scoped[FirstOrder] prefix:110 "∀'" => FirstOrder.Language.BoundedFormula.all @[inherit_doc] scoped[FirstOrder] prefix:arg "∼" => FirstOrder.Language.BoundedFormula.not -- input \~, the ASCII character ~ has too low precedence @[inherit_doc] scoped[FirstOrder] infixl:61 " ⇔ " => FirstOrder.Language.BoundedFormula.iff -- input \<=> @[inherit_doc] scoped[FirstOrder] prefix:110 "∃'" => FirstOrder.Language.BoundedFormula.ex -- input \ex namespace Formula /-- Relabels a formula's variables along a particular function. -/ def relabel (g : α → β) : L.Formula α → L.Formula β := @BoundedFormula.relabel _ _ _ 0 (Sum.inl ∘ g) 0 /-- The graph of a function as a first-order formula. -/ def graph (f : L.Functions n) : L.Formula (Fin (n + 1)) := Term.equal (var 0) (func f fun i => var i.succ) /-- The negation of a formula. -/ protected nonrec abbrev not (φ : L.Formula α) : L.Formula α := φ.not /-- The implication between formulas, as a formula. -/ protected abbrev imp : L.Formula α → L.Formula α → L.Formula α := BoundedFormula.imp variable (β) in /-- `iAlls f φ` transforms a `L.Formula (α ⊕ β)` into a `L.Formula β` by universally quantifying over all variables `Sum.inr _`. -/ noncomputable def iAlls [Finite β] (φ : L.Formula (α ⊕ β)) : L.Formula α := let e := Classical.choice (Classical.choose_spec (Finite.exists_equiv_fin β)) (BoundedFormula.relabel (fun a => Sum.map id e a) φ).alls variable (β) in /-- `iExs f φ` transforms a `L.Formula (α ⊕ β)` into a `L.Formula β` by existentially quantifying over all variables `Sum.inr _`. -/ noncomputable def iExs [Finite β] (φ : L.Formula (α ⊕ β)) : L.Formula α := let e := Classical.choice (Classical.choose_spec (Finite.exists_equiv_fin β)) (BoundedFormula.relabel (fun a => Sum.map id e a) φ).exs variable (β) in /-- `iExsUnique f φ` transforms a `L.Formula (α ⊕ β)` into a `L.Formula β` by existentially quantifying over all variables `Sum.inr _` and asserting that the solution should be unique -/ noncomputable def iExsUnique [Finite β] (φ : L.Formula (α ⊕ β)) : L.Formula α := iExs β <| φ ⊓ iAlls β ((φ.relabel (fun a => Sum.elim (.inl ∘ .inl) .inr a)).imp <| .iInf fun g => Term.equal (var (.inr g)) (var (.inl (.inr g)))) /-- The biimplication between formulas, as a formula. -/ protected nonrec abbrev iff (φ ψ : L.Formula α) : L.Formula α := φ.iff ψ /-- A bijection sending formulas to sentences with constants. -/ def equivSentence : L.Formula α ≃ L[[α]].Sentence := (BoundedFormula.constantsVarsEquiv.trans (BoundedFormula.relabelEquiv (Equiv.sumEmpty _ _))).symm theorem equivSentence_not (φ : L.Formula α) : equivSentence φ.not = (equivSentence φ).not := rfl theorem equivSentence_inf (φ ψ : L.Formula α) : equivSentence (φ ⊓ ψ) = equivSentence φ ⊓ equivSentence ψ := rfl end Formula namespace Relations variable (r : L.Relations 2) /-- The sentence indicating that a basic relation symbol is reflexive. -/ protected def reflexive : L.Sentence := ∀'r.boundedFormula₂ (&0) &0 /-- The sentence indicating that a basic relation symbol is irreflexive. -/ protected def irreflexive : L.Sentence := ∀'∼(r.boundedFormula₂ (&0) &0) /-- The sentence indicating that a basic relation symbol is symmetric. -/ protected def symmetric : L.Sentence := ∀'∀'(r.boundedFormula₂ (&0) &1 ⟹ r.boundedFormula₂ (&1) &0) /-- The sentence indicating that a basic relation symbol is antisymmetric. -/ protected def antisymmetric : L.Sentence := ∀'∀'(r.boundedFormula₂ (&0) &1 ⟹ r.boundedFormula₂ (&1) &0 ⟹ Term.bdEqual (&0) &1) /-- The sentence indicating that a basic relation symbol is transitive. -/ protected def transitive : L.Sentence := ∀'∀'∀'(r.boundedFormula₂ (&0) &1 ⟹ r.boundedFormula₂ (&1) &2 ⟹ r.boundedFormula₂ (&0) &2) /-- The sentence indicating that a basic relation symbol is total. -/ protected def total : L.Sentence := ∀'∀'(r.boundedFormula₂ (&0) &1 ⊔ r.boundedFormula₂ (&1) &0) end Relations section Cardinality variable (L) /-- A sentence indicating that a structure has `n` distinct elements. -/ protected def Sentence.cardGe (n : ℕ) : L.Sentence := ((((List.finRange n ×ˢ List.finRange n).filter fun ij : _ × _ => ij.1 ≠ ij.2).map fun ij : _ × _ => ∼((&ij.1).bdEqual &ij.2)).foldr (· ⊓ ·) ⊤).exs /-- A theory indicating that a structure is infinite. -/ def infiniteTheory : L.Theory := Set.range (Sentence.cardGe L) /-- A theory that indicates a structure is nonempty. -/ def nonemptyTheory : L.Theory := {Sentence.cardGe L 1} /-- A theory indicating that each of a set of constants is distinct. -/ def distinctConstantsTheory (s : Set α) : L[[α]].Theory := (fun ab : α × α => ((L.con ab.1).term.equal (L.con ab.2).term).not) '' (s ×ˢ s ∩ (Set.diagonal α)ᶜ) variable {L} open Set theorem distinctConstantsTheory_mono {s t : Set α} (h : s ⊆ t) : L.distinctConstantsTheory s ⊆ L.distinctConstantsTheory t := by
unfold distinctConstantsTheory; gcongr theorem monotone_distinctConstantsTheory : Monotone (L.distinctConstantsTheory : Set α → L[[α]].Theory) := fun _s _t st => L.distinctConstantsTheory_mono st theorem directed_distinctConstantsTheory :
Mathlib/ModelTheory/Syntax.lean
842
848
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Alex Kontorovich, Heather Macbeth -/ import Mathlib.MeasureTheory.Group.Action import Mathlib.MeasureTheory.Group.Pointwise import Mathlib.MeasureTheory.Integral.Lebesgue.Map import Mathlib.MeasureTheory.Integral.Bochner.Set /-! # Fundamental domain of a group action A set `s` is said to be a *fundamental domain* of an action of a group `G` on a measurable space `α` with respect to a measure `μ` if * `s` is a measurable set; * the sets `g • s` over all `g : G` cover almost all points of the whole space; * the sets `g • s`, are pairwise a.e. disjoint, i.e., `μ (g₁ • s ∩ g₂ • s) = 0` whenever `g₁ ≠ g₂`; we require this for `g₂ = 1` in the definition, then deduce it for any two `g₁ ≠ g₂`. In this file we prove that in case of a countable group `G` and a measure preserving action, any two fundamental domains have the same measure, and for a `G`-invariant function, its integrals over any two fundamental domains are equal to each other. We also generate additive versions of all theorems in this file using the `to_additive` attribute. * We define the `HasFundamentalDomain` typeclass, in particular to be able to define the `covolume` of a quotient of `α` by a group `G`, which under reasonable conditions does not depend on the choice of fundamental domain. * We define the `QuotientMeasureEqMeasurePreimage` typeclass to describe a situation in which a measure `μ` on `α ⧸ G` can be computed by taking a measure `ν` on `α` of the intersection of the pullback with a fundamental domain. ## Main declarations * `MeasureTheory.IsFundamentalDomain`: Predicate for a set to be a fundamental domain of the action of a group * `MeasureTheory.fundamentalFrontier`: Fundamental frontier of a set under the action of a group. Elements of `s` that belong to some other translate of `s`. * `MeasureTheory.fundamentalInterior`: Fundamental interior of a set under the action of a group. Elements of `s` that do not belong to any other translate of `s`. -/ open scoped ENNReal Pointwise Topology NNReal ENNReal MeasureTheory open MeasureTheory MeasureTheory.Measure Set Function TopologicalSpace Filter namespace MeasureTheory /-- A measurable set `s` is a *fundamental domain* for an additive action of an additive group `G` on a measurable space `α` with respect to a measure `α` if the sets `g +ᵥ s`, `g : G`, are pairwise a.e. disjoint and cover the whole space. -/ structure IsAddFundamentalDomain (G : Type*) {α : Type*} [Zero G] [VAdd G α] [MeasurableSpace α] (s : Set α) (μ : Measure α := by volume_tac) : Prop where protected nullMeasurableSet : NullMeasurableSet s μ protected ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g +ᵥ x ∈ s protected aedisjoint : Pairwise <| (AEDisjoint μ on fun g : G => g +ᵥ s) /-- A measurable set `s` is a *fundamental domain* for an action of a group `G` on a measurable space `α` with respect to a measure `α` if the sets `g • s`, `g : G`, are pairwise a.e. disjoint and cover the whole space. -/ @[to_additive IsAddFundamentalDomain] structure IsFundamentalDomain (G : Type*) {α : Type*} [One G] [SMul G α] [MeasurableSpace α] (s : Set α) (μ : Measure α := by volume_tac) : Prop where protected nullMeasurableSet : NullMeasurableSet s μ protected ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g • x ∈ s protected aedisjoint : Pairwise <| (AEDisjoint μ on fun g : G => g • s) variable {G H α β E : Type*} namespace IsFundamentalDomain variable [Group G] [Group H] [MulAction G α] [MeasurableSpace α] [MulAction H β] [MeasurableSpace β] [NormedAddCommGroup E] {s t : Set α} {μ : Measure α} /-- If for each `x : α`, exactly one of `g • x`, `g : G`, belongs to a measurable set `s`, then `s` is a fundamental domain for the action of `G` on `α`. -/ @[to_additive "If for each `x : α`, exactly one of `g +ᵥ x`, `g : G`, belongs to a measurable set `s`, then `s` is a fundamental domain for the additive action of `G` on `α`."] theorem mk' (h_meas : NullMeasurableSet s μ) (h_exists : ∀ x : α, ∃! g : G, g • x ∈ s) : IsFundamentalDomain G s μ where nullMeasurableSet := h_meas ae_covers := Eventually.of_forall fun x => (h_exists x).exists aedisjoint a b hab := Disjoint.aedisjoint <| disjoint_left.2 fun x hxa hxb => by rw [mem_smul_set_iff_inv_smul_mem] at hxa hxb exact hab (inv_injective <| (h_exists x).unique hxa hxb) /-- For `s` to be a fundamental domain, it's enough to check `MeasureTheory.AEDisjoint (g • s) s` for `g ≠ 1`. -/ @[to_additive "For `s` to be a fundamental domain, it's enough to check `MeasureTheory.AEDisjoint (g +ᵥ s) s` for `g ≠ 0`."] theorem mk'' (h_meas : NullMeasurableSet s μ) (h_ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g • x ∈ s) (h_ae_disjoint : ∀ g, g ≠ (1 : G) → AEDisjoint μ (g • s) s) (h_qmp : ∀ g : G, QuasiMeasurePreserving ((g • ·) : α → α) μ μ) : IsFundamentalDomain G s μ where nullMeasurableSet := h_meas ae_covers := h_ae_covers aedisjoint := pairwise_aedisjoint_of_aedisjoint_forall_ne_one h_ae_disjoint h_qmp /-- If a measurable space has a finite measure `μ` and a countable group `G` acts quasi-measure-preservingly, then to show that a set `s` is a fundamental domain, it is sufficient to check that its translates `g • s` are (almost) disjoint and that the sum `∑' g, μ (g • s)` is sufficiently large. -/ @[to_additive "If a measurable space has a finite measure `μ` and a countable additive group `G` acts quasi-measure-preservingly, then to show that a set `s` is a fundamental domain, it is sufficient to check that its translates `g +ᵥ s` are (almost) disjoint and that the sum `∑' g, μ (g +ᵥ s)` is sufficiently large."] theorem mk_of_measure_univ_le [IsFiniteMeasure μ] [Countable G] (h_meas : NullMeasurableSet s μ) (h_ae_disjoint : ∀ g ≠ (1 : G), AEDisjoint μ (g • s) s) (h_qmp : ∀ g : G, QuasiMeasurePreserving (g • · : α → α) μ μ) (h_measure_univ_le : μ (univ : Set α) ≤ ∑' g : G, μ (g • s)) : IsFundamentalDomain G s μ := have aedisjoint : Pairwise (AEDisjoint μ on fun g : G => g • s) := pairwise_aedisjoint_of_aedisjoint_forall_ne_one h_ae_disjoint h_qmp { nullMeasurableSet := h_meas aedisjoint ae_covers := by replace h_meas : ∀ g : G, NullMeasurableSet (g • s) μ := fun g => by rw [← inv_inv g, ← preimage_smul]; exact h_meas.preimage (h_qmp g⁻¹) have h_meas' : NullMeasurableSet {a | ∃ g : G, g • a ∈ s} μ := by rw [← iUnion_smul_eq_setOf_exists]; exact .iUnion h_meas rw [ae_iff_measure_eq h_meas', ← iUnion_smul_eq_setOf_exists] refine le_antisymm (measure_mono <| subset_univ _) ?_ rw [measure_iUnion₀ aedisjoint h_meas] exact h_measure_univ_le } @[to_additive] theorem iUnion_smul_ae_eq (h : IsFundamentalDomain G s μ) : ⋃ g : G, g • s =ᵐ[μ] univ := eventuallyEq_univ.2 <| h.ae_covers.mono fun _ ⟨g, hg⟩ => mem_iUnion.2 ⟨g⁻¹, _, hg, inv_smul_smul _ _⟩ @[to_additive] theorem measure_ne_zero [Countable G] [SMulInvariantMeasure G α μ] (hμ : μ ≠ 0) (h : IsFundamentalDomain G s μ) : μ s ≠ 0 := by have hc := measure_univ_pos.mpr hμ contrapose! hc rw [← measure_congr h.iUnion_smul_ae_eq] refine le_trans (measure_iUnion_le _) ?_ simp_rw [measure_smul, hc, tsum_zero, le_refl] @[to_additive] theorem mono (h : IsFundamentalDomain G s μ) {ν : Measure α} (hle : ν ≪ μ) : IsFundamentalDomain G s ν := ⟨h.1.mono_ac hle, hle h.2, h.aedisjoint.mono fun _ _ h => hle h⟩ @[to_additive] theorem preimage_of_equiv {ν : Measure β} (h : IsFundamentalDomain G s μ) {f : β → α} (hf : QuasiMeasurePreserving f ν μ) {e : G → H} (he : Bijective e) (hef : ∀ g, Semiconj f (e g • ·) (g • ·)) : IsFundamentalDomain H (f ⁻¹' s) ν where nullMeasurableSet := h.nullMeasurableSet.preimage hf ae_covers := (hf.ae h.ae_covers).mono fun x ⟨g, hg⟩ => ⟨e g, by rwa [mem_preimage, hef g x]⟩ aedisjoint a b hab := by lift e to G ≃ H using he have : (e.symm a⁻¹)⁻¹ ≠ (e.symm b⁻¹)⁻¹ := by simp [hab] have := (h.aedisjoint this).preimage hf simp only [Semiconj] at hef simpa only [onFun, ← preimage_smul_inv, preimage_preimage, ← hef, e.apply_symm_apply, inv_inv] using this @[to_additive] theorem image_of_equiv {ν : Measure β} (h : IsFundamentalDomain G s μ) (f : α ≃ β) (hf : QuasiMeasurePreserving f.symm ν μ) (e : H ≃ G) (hef : ∀ g, Semiconj f (e g • ·) (g • ·)) : IsFundamentalDomain H (f '' s) ν := by rw [f.image_eq_preimage] refine h.preimage_of_equiv hf e.symm.bijective fun g x => ?_ rcases f.surjective x with ⟨x, rfl⟩ rw [← hef _ _, f.symm_apply_apply, f.symm_apply_apply, e.apply_symm_apply] @[to_additive] theorem pairwise_aedisjoint_of_ac {ν} (h : IsFundamentalDomain G s μ) (hν : ν ≪ μ) : Pairwise fun g₁ g₂ : G => AEDisjoint ν (g₁ • s) (g₂ • s) := h.aedisjoint.mono fun _ _ H => hν H @[to_additive] theorem smul_of_comm {G' : Type*} [Group G'] [MulAction G' α] [MeasurableSpace G'] [MeasurableSMul G' α] [SMulInvariantMeasure G' α μ] [SMulCommClass G' G α] (h : IsFundamentalDomain G s μ) (g : G') : IsFundamentalDomain G (g • s) μ := h.image_of_equiv (MulAction.toPerm g) (measurePreserving_smul _ _).quasiMeasurePreserving (Equiv.refl _) <| smul_comm g variable [MeasurableSpace G] [MeasurableSMul G α] [SMulInvariantMeasure G α μ] @[to_additive] theorem nullMeasurableSet_smul (h : IsFundamentalDomain G s μ) (g : G) : NullMeasurableSet (g • s) μ := h.nullMeasurableSet.smul g @[to_additive] theorem restrict_restrict (h : IsFundamentalDomain G s μ) (g : G) (t : Set α) : (μ.restrict t).restrict (g • s) = μ.restrict (g • s ∩ t) := restrict_restrict₀ ((h.nullMeasurableSet_smul g).mono restrict_le_self) @[to_additive] theorem smul (h : IsFundamentalDomain G s μ) (g : G) : IsFundamentalDomain G (g • s) μ := h.image_of_equiv (MulAction.toPerm g) (measurePreserving_smul _ _).quasiMeasurePreserving ⟨fun g' => g⁻¹ * g' * g, fun g' => g * g' * g⁻¹, fun g' => by simp [mul_assoc], fun g' => by simp [mul_assoc]⟩ fun g' x => by simp [smul_smul, mul_assoc] variable [Countable G] {ν : Measure α} @[to_additive] theorem sum_restrict_of_ac (h : IsFundamentalDomain G s μ) (hν : ν ≪ μ) : (sum fun g : G => ν.restrict (g • s)) = ν := by rw [← restrict_iUnion_ae (h.aedisjoint.mono fun i j h => hν h) fun g => (h.nullMeasurableSet_smul g).mono_ac hν, restrict_congr_set (hν h.iUnion_smul_ae_eq), restrict_univ] @[to_additive] theorem lintegral_eq_tsum_of_ac (h : IsFundamentalDomain G s μ) (hν : ν ≪ μ) (f : α → ℝ≥0∞) : ∫⁻ x, f x ∂ν = ∑' g : G, ∫⁻ x in g • s, f x ∂ν := by rw [← lintegral_sum_measure, h.sum_restrict_of_ac hν] @[to_additive] theorem sum_restrict (h : IsFundamentalDomain G s μ) : (sum fun g : G => μ.restrict (g • s)) = μ := h.sum_restrict_of_ac (refl _) @[to_additive] theorem lintegral_eq_tsum (h : IsFundamentalDomain G s μ) (f : α → ℝ≥0∞) : ∫⁻ x, f x ∂μ = ∑' g : G, ∫⁻ x in g • s, f x ∂μ := h.lintegral_eq_tsum_of_ac (refl _) f @[to_additive] theorem lintegral_eq_tsum' (h : IsFundamentalDomain G s μ) (f : α → ℝ≥0∞) : ∫⁻ x, f x ∂μ = ∑' g : G, ∫⁻ x in s, f (g⁻¹ • x) ∂μ := calc ∫⁻ x, f x ∂μ = ∑' g : G, ∫⁻ x in g • s, f x ∂μ := h.lintegral_eq_tsum f _ = ∑' g : G, ∫⁻ x in g⁻¹ • s, f x ∂μ := ((Equiv.inv G).tsum_eq _).symm _ = ∑' g : G, ∫⁻ x in s, f (g⁻¹ • x) ∂μ := tsum_congr fun g => Eq.symm <| (measurePreserving_smul g⁻¹ μ).setLIntegral_comp_emb (measurableEmbedding_const_smul _) _ _ @[to_additive] lemma lintegral_eq_tsum'' (h : IsFundamentalDomain G s μ) (f : α → ℝ≥0∞) : ∫⁻ x, f x ∂μ = ∑' g : G, ∫⁻ x in s, f (g • x) ∂μ := (lintegral_eq_tsum' h f).trans ((Equiv.inv G).tsum_eq (fun g ↦ ∫⁻ (x : α) in s, f (g • x) ∂μ)) @[to_additive] theorem setLIntegral_eq_tsum (h : IsFundamentalDomain G s μ) (f : α → ℝ≥0∞) (t : Set α) : ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in t ∩ g • s, f x ∂μ := calc ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in g • s, f x ∂μ.restrict t := h.lintegral_eq_tsum_of_ac restrict_le_self.absolutelyContinuous _ _ = ∑' g : G, ∫⁻ x in t ∩ g • s, f x ∂μ := by simp only [h.restrict_restrict, inter_comm] @[to_additive] theorem setLIntegral_eq_tsum' (h : IsFundamentalDomain G s μ) (f : α → ℝ≥0∞) (t : Set α) : ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := calc ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in t ∩ g • s, f x ∂μ := h.setLIntegral_eq_tsum f t _ = ∑' g : G, ∫⁻ x in t ∩ g⁻¹ • s, f x ∂μ := ((Equiv.inv G).tsum_eq _).symm _ = ∑' g : G, ∫⁻ x in g⁻¹ • (g • t ∩ s), f x ∂μ := by simp only [smul_set_inter, inv_smul_smul] _ = ∑' g : G, ∫⁻ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := tsum_congr fun g => Eq.symm <| (measurePreserving_smul g⁻¹ μ).setLIntegral_comp_emb (measurableEmbedding_const_smul _) _ _ @[to_additive] theorem measure_eq_tsum_of_ac (h : IsFundamentalDomain G s μ) (hν : ν ≪ μ) (t : Set α) : ν t = ∑' g : G, ν (t ∩ g • s) := by have H : ν.restrict t ≪ μ := Measure.restrict_le_self.absolutelyContinuous.trans hν simpa only [setLIntegral_one, Pi.one_def, Measure.restrict_apply₀ ((h.nullMeasurableSet_smul _).mono_ac H), inter_comm] using h.lintegral_eq_tsum_of_ac H 1 @[to_additive] theorem measure_eq_tsum' (h : IsFundamentalDomain G s μ) (t : Set α) : μ t = ∑' g : G, μ (t ∩ g • s) := h.measure_eq_tsum_of_ac AbsolutelyContinuous.rfl t @[to_additive] theorem measure_eq_tsum (h : IsFundamentalDomain G s μ) (t : Set α) : μ t = ∑' g : G, μ (g • t ∩ s) := by simpa only [setLIntegral_one] using h.setLIntegral_eq_tsum' (fun _ => 1) t @[to_additive] theorem measure_zero_of_invariant (h : IsFundamentalDomain G s μ) (t : Set α) (ht : ∀ g : G, g • t = t) (hts : μ (t ∩ s) = 0) : μ t = 0 := by rw [measure_eq_tsum h]; simp [ht, hts] /-- Given a measure space with an action of a finite group `G`, the measure of any `G`-invariant set is determined by the measure of its intersection with a fundamental domain for the action of `G`. -/ @[to_additive measure_eq_card_smul_of_vadd_ae_eq_self "Given a measure space with an action of a finite additive group `G`, the measure of any `G`-invariant set is determined by the measure of its intersection with a fundamental domain for the action of `G`."] theorem measure_eq_card_smul_of_smul_ae_eq_self [Finite G] (h : IsFundamentalDomain G s μ) (t : Set α) (ht : ∀ g : G, (g • t : Set α) =ᵐ[μ] t) : μ t = Nat.card G • μ (t ∩ s) := by haveI : Fintype G := Fintype.ofFinite G rw [h.measure_eq_tsum] replace ht : ∀ g : G, (g • t ∩ s : Set α) =ᵐ[μ] (t ∩ s : Set α) := fun g => ae_eq_set_inter (ht g) (ae_eq_refl s) simp_rw [measure_congr (ht _), tsum_fintype, Finset.sum_const, Nat.card_eq_fintype_card, Finset.card_univ] @[to_additive] protected theorem setLIntegral_eq (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) (f : α → ℝ≥0∞) (hf : ∀ (g : G) (x), f (g • x) = f x) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in t, f x ∂μ := calc ∫⁻ x in s, f x ∂μ = ∑' g : G, ∫⁻ x in s ∩ g • t, f x ∂μ := ht.setLIntegral_eq_tsum _ _ _ = ∑' g : G, ∫⁻ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := by simp only [hf, inter_comm] _ = ∫⁻ x in t, f x ∂μ := (hs.setLIntegral_eq_tsum' _ _).symm @[to_additive] theorem measure_set_eq (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) {A : Set α} (hA₀ : MeasurableSet A) (hA : ∀ g : G, (fun x => g • x) ⁻¹' A = A) : μ (A ∩ s) = μ (A ∩ t) := by have : ∫⁻ x in s, A.indicator 1 x ∂μ = ∫⁻ x in t, A.indicator 1 x ∂μ := by refine hs.setLIntegral_eq ht (Set.indicator A fun _ => 1) fun g x ↦ ?_ convert (Set.indicator_comp_right (g • · : α → α) (g := fun _ ↦ (1 : ℝ≥0∞))).symm rw [hA g] simpa [Measure.restrict_apply hA₀, lintegral_indicator hA₀] using this /-- If `s` and `t` are two fundamental domains of the same action, then their measures are equal. -/ @[to_additive "If `s` and `t` are two fundamental domains of the same action, then their measures are equal."] protected theorem measure_eq (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) : μ s = μ t := by simpa only [setLIntegral_one] using hs.setLIntegral_eq ht (fun _ => 1) fun _ _ => rfl @[to_additive] protected theorem aestronglyMeasurable_on_iff {β : Type*} [TopologicalSpace β] [PseudoMetrizableSpace β] (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) {f : α → β} (hf : ∀ (g : G) (x), f (g • x) = f x) : AEStronglyMeasurable f (μ.restrict s) ↔ AEStronglyMeasurable f (μ.restrict t) := calc AEStronglyMeasurable f (μ.restrict s) ↔ AEStronglyMeasurable f (Measure.sum fun g : G => μ.restrict (g • t ∩ s)) := by simp only [← ht.restrict_restrict, ht.sum_restrict_of_ac restrict_le_self.absolutelyContinuous] _ ↔ ∀ g : G, AEStronglyMeasurable f (μ.restrict (g • (g⁻¹ • s ∩ t))) := by simp only [smul_set_inter, inter_comm, smul_inv_smul, aestronglyMeasurable_sum_measure_iff] _ ↔ ∀ g : G, AEStronglyMeasurable f (μ.restrict (g⁻¹ • (g⁻¹⁻¹ • s ∩ t))) := inv_surjective.forall _ ↔ ∀ g : G, AEStronglyMeasurable f (μ.restrict (g⁻¹ • (g • s ∩ t))) := by simp only [inv_inv] _ ↔ ∀ g : G, AEStronglyMeasurable f (μ.restrict (g • s ∩ t)) := by refine forall_congr' fun g => ?_ have he : MeasurableEmbedding (g⁻¹ • · : α → α) := measurableEmbedding_const_smul _ rw [← image_smul, ← ((measurePreserving_smul g⁻¹ μ).restrict_image_emb he _).aestronglyMeasurable_comp_iff he] simp only [Function.comp_def, hf] _ ↔ AEStronglyMeasurable f (μ.restrict t) := by simp only [← aestronglyMeasurable_sum_measure_iff, ← hs.restrict_restrict, hs.sum_restrict_of_ac restrict_le_self.absolutelyContinuous] @[deprecated (since := "2025-04-09")] alias aEStronglyMeasurable_on_iff := MeasureTheory.IsFundamentalDomain.aestronglyMeasurable_on_iff @[deprecated (since := "2025-04-09")] alias _root_.MeasureTheory.IsAddFundamentalDomain.aEStronglyMeasurable_on_iff := MeasureTheory.IsAddFundamentalDomain.aestronglyMeasurable_on_iff @[to_additive] protected theorem hasFiniteIntegral_on_iff (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) {f : α → E} (hf : ∀ (g : G) (x), f (g • x) = f x) : HasFiniteIntegral f (μ.restrict s) ↔ HasFiniteIntegral f (μ.restrict t) := by dsimp only [HasFiniteIntegral] rw [hs.setLIntegral_eq ht] intro g x; rw [hf] @[to_additive] protected theorem integrableOn_iff (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) {f : α → E} (hf : ∀ (g : G) (x), f (g • x) = f x) : IntegrableOn f s μ ↔ IntegrableOn f t μ := and_congr (hs.aestronglyMeasurable_on_iff ht hf) (hs.hasFiniteIntegral_on_iff ht hf) variable [NormedSpace ℝ E] @[to_additive] theorem integral_eq_tsum_of_ac (h : IsFundamentalDomain G s μ) (hν : ν ≪ μ) (f : α → E) (hf : Integrable f ν) : ∫ x, f x ∂ν = ∑' g : G, ∫ x in g • s, f x ∂ν := by rw [← MeasureTheory.integral_sum_measure, h.sum_restrict_of_ac hν] rw [h.sum_restrict_of_ac hν] exact hf @[to_additive] theorem integral_eq_tsum (h : IsFundamentalDomain G s μ) (f : α → E) (hf : Integrable f μ) : ∫ x, f x ∂μ = ∑' g : G, ∫ x in g • s, f x ∂μ := integral_eq_tsum_of_ac h (by rfl) f hf @[to_additive] theorem integral_eq_tsum' (h : IsFundamentalDomain G s μ) (f : α → E) (hf : Integrable f μ) : ∫ x, f x ∂μ = ∑' g : G, ∫ x in s, f (g⁻¹ • x) ∂μ := calc ∫ x, f x ∂μ = ∑' g : G, ∫ x in g • s, f x ∂μ := h.integral_eq_tsum f hf _ = ∑' g : G, ∫ x in g⁻¹ • s, f x ∂μ := ((Equiv.inv G).tsum_eq _).symm _ = ∑' g : G, ∫ x in s, f (g⁻¹ • x) ∂μ := tsum_congr fun g => (measurePreserving_smul g⁻¹ μ).setIntegral_image_emb (measurableEmbedding_const_smul _) _ _ @[to_additive] lemma integral_eq_tsum'' (h : IsFundamentalDomain G s μ) (f : α → E) (hf : Integrable f μ) : ∫ x, f x ∂μ = ∑' g : G, ∫ x in s, f (g • x) ∂μ := (integral_eq_tsum' h f hf).trans ((Equiv.inv G).tsum_eq (fun g ↦ ∫ (x : α) in s, f (g • x) ∂μ)) @[to_additive] theorem setIntegral_eq_tsum (h : IsFundamentalDomain G s μ) {f : α → E} {t : Set α} (hf : IntegrableOn f t μ) : ∫ x in t, f x ∂μ = ∑' g : G, ∫ x in t ∩ g • s, f x ∂μ := calc ∫ x in t, f x ∂μ = ∑' g : G, ∫ x in g • s, f x ∂μ.restrict t := h.integral_eq_tsum_of_ac restrict_le_self.absolutelyContinuous f hf _ = ∑' g : G, ∫ x in t ∩ g • s, f x ∂μ := by simp only [h.restrict_restrict, measure_smul, inter_comm] @[to_additive] theorem setIntegral_eq_tsum' (h : IsFundamentalDomain G s μ) {f : α → E} {t : Set α} (hf : IntegrableOn f t μ) : ∫ x in t, f x ∂μ = ∑' g : G, ∫ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := calc ∫ x in t, f x ∂μ = ∑' g : G, ∫ x in t ∩ g • s, f x ∂μ := h.setIntegral_eq_tsum hf _ = ∑' g : G, ∫ x in t ∩ g⁻¹ • s, f x ∂μ := ((Equiv.inv G).tsum_eq _).symm _ = ∑' g : G, ∫ x in g⁻¹ • (g • t ∩ s), f x ∂μ := by simp only [smul_set_inter, inv_smul_smul] _ = ∑' g : G, ∫ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := tsum_congr fun g => (measurePreserving_smul g⁻¹ μ).setIntegral_image_emb (measurableEmbedding_const_smul _) _ _ @[to_additive] protected theorem setIntegral_eq (hs : IsFundamentalDomain G s μ) (ht : IsFundamentalDomain G t μ) {f : α → E} (hf : ∀ (g : G) (x), f (g • x) = f x) : ∫ x in s, f x ∂μ = ∫ x in t, f x ∂μ := by by_cases hfs : IntegrableOn f s μ · have hft : IntegrableOn f t μ := by rwa [ht.integrableOn_iff hs hf] calc ∫ x in s, f x ∂μ = ∑' g : G, ∫ x in s ∩ g • t, f x ∂μ := ht.setIntegral_eq_tsum hfs _ = ∑' g : G, ∫ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := by simp only [hf, inter_comm] _ = ∫ x in t, f x ∂μ := (hs.setIntegral_eq_tsum' hft).symm · rw [integral_undef hfs, integral_undef] rwa [hs.integrableOn_iff ht hf] at hfs /-- If the action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` such that the sets `g • t ∩ s` are pairwise a.e.-disjoint has measure at most `μ s`. -/ @[to_additive "If the additive action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` such that the sets `g +ᵥ t ∩ s` are pairwise a.e.-disjoint has measure at most `μ s`."] theorem measure_le_of_pairwise_disjoint (hs : IsFundamentalDomain G s μ) (ht : NullMeasurableSet t μ) (hd : Pairwise (AEDisjoint μ on fun g : G => g • t ∩ s)) : μ t ≤ μ s := calc μ t = ∑' g : G, μ (g • t ∩ s) := hs.measure_eq_tsum t _ = μ (⋃ g : G, g • t ∩ s) := Eq.symm <| measure_iUnion₀ hd fun _ => (ht.smul _).inter hs.nullMeasurableSet _ ≤ μ s := measure_mono (iUnion_subset fun _ => inter_subset_right) /-- If the action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` of measure strictly greater than `μ s` contains two points `x y` such that `g • x = y` for some `g ≠ 1`. -/ @[to_additive "If the additive action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` of measure strictly greater than `μ s` contains two points `x y` such that `g +ᵥ x = y` for some `g ≠ 0`."] theorem exists_ne_one_smul_eq (hs : IsFundamentalDomain G s μ) (htm : NullMeasurableSet t μ) (ht : μ s < μ t) : ∃ x ∈ t, ∃ y ∈ t, ∃ g, g ≠ (1 : G) ∧ g • x = y := by contrapose! ht refine hs.measure_le_of_pairwise_disjoint htm (Pairwise.aedisjoint fun g₁ g₂ hne => ?_) dsimp [Function.onFun] refine (Disjoint.inf_left _ ?_).inf_right _ rw [Set.disjoint_left] rintro _ ⟨x, hx, rfl⟩ ⟨y, hy, hxy : g₂ • y = g₁ • x⟩ refine ht x hx y hy (g₂⁻¹ * g₁) (mt inv_mul_eq_one.1 hne.symm) ?_ rw [mul_smul, ← hxy, inv_smul_smul] /-- If `f` is invariant under the action of a countable group `G`, and `μ` is a `G`-invariant measure with a fundamental domain `s`, then the `essSup` of `f` restricted to `s` is the same as that of `f` on all of its domain. -/ @[to_additive "If `f` is invariant under the action of a countable additive group `G`, and `μ` is a `G`-invariant measure with a fundamental domain `s`, then the `essSup` of `f` restricted to `s` is the same as that of `f` on all of its domain."] theorem essSup_measure_restrict (hs : IsFundamentalDomain G s μ) {f : α → ℝ≥0∞} (hf : ∀ γ : G, ∀ x : α, f (γ • x) = f x) : essSup f (μ.restrict s) = essSup f μ := by refine le_antisymm (essSup_mono_measure' Measure.restrict_le_self) ?_ rw [essSup_eq_sInf (μ.restrict s) f, essSup_eq_sInf μ f] refine sInf_le_sInf ?_ rintro a (ha : (μ.restrict s) {x : α | a < f x} = 0) rw [Measure.restrict_apply₀' hs.nullMeasurableSet] at ha refine measure_zero_of_invariant hs _ ?_ ha intro γ ext x rw [mem_smul_set_iff_inv_smul_mem] simp only [mem_setOf_eq, hf γ⁻¹ x] end IsFundamentalDomain /-! ### Interior/frontier of a fundamental domain -/ section MeasurableSpace variable (G) [Group G] [MulAction G α] (s : Set α) {x : α} /-- The boundary of a fundamental domain, those points of the domain that also lie in a nontrivial translate. -/ @[to_additive MeasureTheory.addFundamentalFrontier "The boundary of a fundamental domain, those points of the domain that also lie in a nontrivial translate."] def fundamentalFrontier : Set α := s ∩ ⋃ (g : G) (_ : g ≠ 1), g • s /-- The interior of a fundamental domain, those points of the domain not lying in any translate. -/ @[to_additive MeasureTheory.addFundamentalInterior "The interior of a fundamental domain, those points of the domain not lying in any translate."] def fundamentalInterior : Set α := s \ ⋃ (g : G) (_ : g ≠ 1), g • s variable {G s} @[to_additive (attr := simp) MeasureTheory.mem_addFundamentalFrontier] theorem mem_fundamentalFrontier : x ∈ fundamentalFrontier G s ↔ x ∈ s ∧ ∃ g : G, g ≠ 1 ∧ x ∈ g • s := by simp [fundamentalFrontier] @[to_additive (attr := simp) MeasureTheory.mem_addFundamentalInterior] theorem mem_fundamentalInterior : x ∈ fundamentalInterior G s ↔ x ∈ s ∧ ∀ g : G, g ≠ 1 → x ∉ g • s := by simp [fundamentalInterior] @[to_additive MeasureTheory.addFundamentalFrontier_subset] theorem fundamentalFrontier_subset : fundamentalFrontier G s ⊆ s := inter_subset_left @[to_additive MeasureTheory.addFundamentalInterior_subset] theorem fundamentalInterior_subset : fundamentalInterior G s ⊆ s := diff_subset variable (G s) @[to_additive MeasureTheory.disjoint_addFundamentalInterior_addFundamentalFrontier] theorem disjoint_fundamentalInterior_fundamentalFrontier : Disjoint (fundamentalInterior G s) (fundamentalFrontier G s) := disjoint_sdiff_self_left.mono_right inf_le_right @[to_additive (attr := simp) MeasureTheory.addFundamentalInterior_union_addFundamentalFrontier] theorem fundamentalInterior_union_fundamentalFrontier : fundamentalInterior G s ∪ fundamentalFrontier G s = s := diff_union_inter _ _ @[to_additive (attr := simp) MeasureTheory.addFundamentalFrontier_union_addFundamentalInterior] theorem fundamentalFrontier_union_fundamentalInterior : fundamentalFrontier G s ∪ fundamentalInterior G s = s := inter_union_diff _ _ @[to_additive (attr := simp) MeasureTheory.sdiff_addFundamentalInterior] theorem sdiff_fundamentalInterior : s \ fundamentalInterior G s = fundamentalFrontier G s := sdiff_sdiff_right_self @[to_additive (attr := simp) MeasureTheory.sdiff_addFundamentalFrontier] theorem sdiff_fundamentalFrontier : s \ fundamentalFrontier G s = fundamentalInterior G s := diff_self_inter @[to_additive (attr := simp) MeasureTheory.addFundamentalFrontier_vadd] theorem fundamentalFrontier_smul [Group H] [MulAction H α] [SMulCommClass H G α] (g : H) : fundamentalFrontier G (g • s) = g • fundamentalFrontier G s := by simp_rw [fundamentalFrontier, smul_set_inter, smul_set_iUnion, smul_comm g (_ : G) (_ : Set α)] @[to_additive (attr := simp) MeasureTheory.addFundamentalInterior_vadd] theorem fundamentalInterior_smul [Group H] [MulAction H α] [SMulCommClass H G α] (g : H) : fundamentalInterior G (g • s) = g • fundamentalInterior G s := by simp_rw [fundamentalInterior, smul_set_sdiff, smul_set_iUnion, smul_comm g (_ : G) (_ : Set α)] @[to_additive MeasureTheory.pairwise_disjoint_addFundamentalInterior] theorem pairwise_disjoint_fundamentalInterior : Pairwise (Disjoint on fun g : G => g • fundamentalInterior G s) := by refine fun a b hab => disjoint_left.2 ?_ rintro _ ⟨x, hx, rfl⟩ ⟨y, hy, hxy⟩ rw [mem_fundamentalInterior] at hx hy refine hx.2 (a⁻¹ * b) ?_ ?_ · rwa [Ne, inv_mul_eq_iff_eq_mul, mul_one, eq_comm] · simpa [mul_smul, ← hxy, mem_inv_smul_set_iff] using hy.1 variable [Countable G] [MeasurableSpace G] [MeasurableSpace α] [MeasurableSMul G α] {μ : Measure α} [SMulInvariantMeasure G α μ] @[to_additive MeasureTheory.NullMeasurableSet.addFundamentalFrontier] protected theorem NullMeasurableSet.fundamentalFrontier (hs : NullMeasurableSet s μ) : NullMeasurableSet (fundamentalFrontier G s) μ := hs.inter <| .iUnion fun _ => .iUnion fun _ => hs.smul _ @[to_additive MeasureTheory.NullMeasurableSet.addFundamentalInterior] protected theorem NullMeasurableSet.fundamentalInterior (hs : NullMeasurableSet s μ) : NullMeasurableSet (fundamentalInterior G s) μ := hs.diff <| .iUnion fun _ => .iUnion fun _ => hs.smul _ end MeasurableSpace namespace IsFundamentalDomain variable [Countable G] [Group G] [MulAction G α] [MeasurableSpace α] {μ : Measure α} {s : Set α} (hs : IsFundamentalDomain G s μ) include hs section Group @[to_additive MeasureTheory.IsAddFundamentalDomain.measure_addFundamentalFrontier] theorem measure_fundamentalFrontier : μ (fundamentalFrontier G s) = 0 := by simpa only [fundamentalFrontier, iUnion₂_inter, one_smul, measure_iUnion_null_iff, inter_comm s, Function.onFun] using fun g (hg : g ≠ 1) => hs.aedisjoint hg @[to_additive MeasureTheory.IsAddFundamentalDomain.measure_addFundamentalInterior] theorem measure_fundamentalInterior : μ (fundamentalInterior G s) = μ s := measure_diff_null' hs.measure_fundamentalFrontier end Group variable [MeasurableSpace G] [MeasurableSMul G α] [SMulInvariantMeasure G α μ] protected theorem fundamentalInterior : IsFundamentalDomain G (fundamentalInterior G s) μ where nullMeasurableSet := hs.nullMeasurableSet.fundamentalInterior _ _ ae_covers := by simp_rw [ae_iff, not_exists, ← mem_inv_smul_set_iff, setOf_forall, ← compl_setOf, setOf_mem_eq, ← compl_iUnion] have : ((⋃ g : G, g⁻¹ • s) \ ⋃ g : G, g⁻¹ • fundamentalFrontier G s) ⊆ ⋃ g : G, g⁻¹ • fundamentalInterior G s := by simp_rw [diff_subset_iff, ← iUnion_union_distrib, ← smul_set_union (α := G) (β := α), fundamentalFrontier_union_fundamentalInterior]; rfl refine eq_bot_mono (μ.mono <| compl_subset_compl.2 this) ?_ simp only [iUnion_inv_smul, compl_sdiff, ENNReal.bot_eq_zero, himp_eq, sup_eq_union, @iUnion_smul_eq_setOf_exists _ _ _ _ s] exact measure_union_null (measure_iUnion_null fun _ => measure_smul_null hs.measure_fundamentalFrontier _) hs.ae_covers aedisjoint := (pairwise_disjoint_fundamentalInterior _ _).mono fun _ _ => Disjoint.aedisjoint end IsFundamentalDomain section FundamentalDomainMeasure variable (G) [Group G] [MulAction G α] [MeasurableSpace α] (μ : Measure α) local notation "α_mod_G" => MulAction.orbitRel G α local notation "π" => @Quotient.mk _ α_mod_G variable {G} @[to_additive addMeasure_map_restrict_apply] lemma measure_map_restrict_apply (s : Set α) {U : Set (Quotient α_mod_G)} (meas_U : MeasurableSet U) : (μ.restrict s).map π U = μ ((π ⁻¹' U) ∩ s) := by rw [map_apply (f := π) (fun V hV ↦ measurableSet_quotient.mp hV) meas_U, Measure.restrict_apply (t := (Quotient.mk α_mod_G ⁻¹' U)) (measurableSet_quotient.mp meas_U)] @[to_additive] lemma IsFundamentalDomain.quotientMeasure_eq [Countable G] [MeasurableSpace G] {s t : Set α} [SMulInvariantMeasure G α μ] [MeasurableSMul G α] (fund_dom_s : IsFundamentalDomain G s μ) (fund_dom_t : IsFundamentalDomain G t μ) : (μ.restrict s).map π = (μ.restrict t).map π := by ext U meas_U rw [measure_map_restrict_apply (meas_U := meas_U), measure_map_restrict_apply (meas_U := meas_U)] apply MeasureTheory.IsFundamentalDomain.measure_set_eq fund_dom_s fund_dom_t · exact measurableSet_quotient.mp meas_U · intro g ext x have : Quotient.mk α_mod_G (g • x) = Quotient.mk α_mod_G x := by apply Quotient.sound use g simp only [mem_preimage, this] end FundamentalDomainMeasure /-! ## `HasFundamentalDomain` typeclass We define `HasFundamentalDomain` in order to be able to define the `covolume` of a quotient of `α` by a group `G`, which under reasonable conditions does not depend on the choice of fundamental domain. Even though any "sensible" action should have a fundamental domain, this is a rather
delicate question which was recently addressed by Misha Kapovich: https://arxiv.org/abs/2301.05325 TODO: Formalize the existence of a Dirichlet domain as in Kapovich's paper.
Mathlib/MeasureTheory/Group/FundamentalDomain.lean
658
660
/- 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
Mathlib/SetTheory/Cardinal/Basic.lean
565
565
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Yury Kudryashov -/ import Mathlib.Data.Set.Lattice.Image import Mathlib.Order.Interval.Set.LinearOrder /-! # Extra lemmas about intervals This file contains lemmas about intervals that cannot be included into `Order.Interval.Set.Basic` because this would create an `import` cycle. Namely, lemmas in this file can use definitions from `Data.Set.Lattice`, including `Disjoint`. We consider various intersections and unions of half infinite intervals. -/ universe u v w variable {ι : Sort u} {α : Type v} {β : Type w} open Set open OrderDual (toDual) namespace Set section Preorder variable [Preorder α] {a b c : α} @[simp] theorem Iic_disjoint_Ioi (h : a ≤ b) : Disjoint (Iic a) (Ioi b) := disjoint_left.mpr fun _ ha hb => (h.trans_lt hb).not_le ha @[simp] theorem Iio_disjoint_Ici (h : a ≤ b) : Disjoint (Iio a) (Ici b) := disjoint_left.mpr fun _ ha hb => (h.trans_lt' ha).not_le hb @[simp] theorem Iic_disjoint_Ioc (h : a ≤ b) : Disjoint (Iic a) (Ioc b c) := (Iic_disjoint_Ioi h).mono le_rfl Ioc_subset_Ioi_self @[simp] theorem Ioc_disjoint_Ioc_of_le {d : α} (h : b ≤ c) : Disjoint (Ioc a b) (Ioc c d) := (Iic_disjoint_Ioc h).mono Ioc_subset_Iic_self le_rfl @[deprecated Ioc_disjoint_Ioc_of_le (since := "2025-03-04")] theorem Ioc_disjoint_Ioc_same : Disjoint (Ioc a b) (Ioc b c) := (Iic_disjoint_Ioc le_rfl).mono Ioc_subset_Iic_self le_rfl @[simp] theorem Ico_disjoint_Ico_same : Disjoint (Ico a b) (Ico b c) := disjoint_left.mpr fun _ hab hbc => hab.2.not_le hbc.1 @[simp] theorem Ici_disjoint_Iic : Disjoint (Ici a) (Iic b) ↔ ¬a ≤ b := by rw [Set.disjoint_iff_inter_eq_empty, Ici_inter_Iic, Icc_eq_empty_iff] @[simp] theorem Iic_disjoint_Ici : Disjoint (Iic a) (Ici b) ↔ ¬b ≤ a := disjoint_comm.trans Ici_disjoint_Iic @[simp] theorem Ioc_disjoint_Ioi (h : b ≤ c) : Disjoint (Ioc a b) (Ioi c) := disjoint_left.mpr (fun _ hx hy ↦ (hx.2.trans h).not_lt hy) theorem Ioc_disjoint_Ioi_same : Disjoint (Ioc a b) (Ioi b) := Ioc_disjoint_Ioi le_rfl @[simp] theorem iUnion_Iic : ⋃ a : α, Iic a = univ := iUnion_eq_univ_iff.2 fun x => ⟨x, right_mem_Iic⟩ @[simp] theorem iUnion_Ici : ⋃ a : α, Ici a = univ := iUnion_eq_univ_iff.2 fun x => ⟨x, left_mem_Ici⟩ @[simp] theorem iUnion_Icc_right (a : α) : ⋃ b, Icc a b = Ici a := by simp only [← Ici_inter_Iic, ← inter_iUnion, iUnion_Iic, inter_univ] @[simp] theorem iUnion_Ioc_right (a : α) : ⋃ b, Ioc a b = Ioi a := by simp only [← Ioi_inter_Iic, ← inter_iUnion, iUnion_Iic, inter_univ] @[simp] theorem iUnion_Icc_left (b : α) : ⋃ a, Icc a b = Iic b := by simp only [← Ici_inter_Iic, ← iUnion_inter, iUnion_Ici, univ_inter] @[simp] theorem iUnion_Ico_left (b : α) : ⋃ a, Ico a b = Iio b := by simp only [← Ici_inter_Iio, ← iUnion_inter, iUnion_Ici, univ_inter] @[simp] theorem iUnion_Iio [NoMaxOrder α] : ⋃ a : α, Iio a = univ := iUnion_eq_univ_iff.2 exists_gt @[simp] theorem iUnion_Ioi [NoMinOrder α] : ⋃ a : α, Ioi a = univ := iUnion_eq_univ_iff.2 exists_lt @[simp] theorem iUnion_Ico_right [NoMaxOrder α] (a : α) : ⋃ b, Ico a b = Ici a := by simp only [← Ici_inter_Iio, ← inter_iUnion, iUnion_Iio, inter_univ] @[simp] theorem iUnion_Ioo_right [NoMaxOrder α] (a : α) : ⋃ b, Ioo a b = Ioi a := by simp only [← Ioi_inter_Iio, ← inter_iUnion, iUnion_Iio, inter_univ] @[simp] theorem iUnion_Ioc_left [NoMinOrder α] (b : α) : ⋃ a, Ioc a b = Iic b := by simp only [← Ioi_inter_Iic, ← iUnion_inter, iUnion_Ioi, univ_inter] @[simp] theorem iUnion_Ioo_left [NoMinOrder α] (b : α) : ⋃ a, Ioo a b = Iio b := by simp only [← Ioi_inter_Iio, ← iUnion_inter, iUnion_Ioi, univ_inter] end Preorder section LinearOrder variable [LinearOrder α] {a₁ a₂ b₁ b₂ : α} @[simp] theorem Ico_disjoint_Ico : Disjoint (Ico a₁ a₂) (Ico b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ := by simp_rw [Set.disjoint_iff_inter_eq_empty, Ico_inter_Ico, Ico_eq_empty_iff, not_lt] @[simp] theorem Ioc_disjoint_Ioc : Disjoint (Ioc a₁ a₂) (Ioc b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ := by have h : _ ↔ min (toDual a₁) (toDual b₁) ≤ max (toDual a₂) (toDual b₂) := Ico_disjoint_Ico simpa only [Ico_toDual] using h @[simp] theorem Ioo_disjoint_Ioo [DenselyOrdered α] : Disjoint (Set.Ioo a₁ a₂) (Set.Ioo b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ := by simp_rw [Set.disjoint_iff_inter_eq_empty, Ioo_inter_Ioo, Ioo_eq_empty_iff, not_lt] /-- If two half-open intervals are disjoint and the endpoint of one lies in the other, then it must be equal to the endpoint of the other. -/ theorem eq_of_Ico_disjoint {x₁ x₂ y₁ y₂ : α} (h : Disjoint (Ico x₁ x₂) (Ico y₁ y₂)) (hx : x₁ < x₂) (h2 : x₂ ∈ Ico y₁ y₂) : y₁ = x₂ := by rw [Ico_disjoint_Ico, min_eq_left (le_of_lt h2.2), le_max_iff] at h apply le_antisymm h2.1 exact h.elim (fun h => absurd hx (not_lt_of_le h)) id @[simp] theorem iUnion_Ico_eq_Iio_self_iff {f : ι → α} {a : α} : ⋃ i, Ico (f i) a = Iio a ↔ ∀ x < a, ∃ i, f i ≤ x := by simp [← Ici_inter_Iio, ← iUnion_inter, subset_def] @[simp] theorem iUnion_Ioc_eq_Ioi_self_iff {f : ι → α} {a : α} : ⋃ i, Ioc a (f i) = Ioi a ↔ ∀ x, a < x → ∃ i, x ≤ f i := by simp [← Ioi_inter_Iic, ← inter_iUnion, subset_def] @[simp] theorem biUnion_Ico_eq_Iio_self_iff {p : ι → Prop} {f : ∀ i, p i → α} {a : α} : ⋃ (i) (hi : p i), Ico (f i hi) a = Iio a ↔ ∀ x < a, ∃ i hi, f i hi ≤ x := by simp [← Ici_inter_Iio, ← iUnion_inter, subset_def] @[simp] theorem biUnion_Ioc_eq_Ioi_self_iff {p : ι → Prop} {f : ∀ i, p i → α} {a : α} : ⋃ (i) (hi : p i), Ioc a (f i hi) = Ioi a ↔ ∀ x, a < x → ∃ i hi, x ≤ f i hi := by simp [← Ioi_inter_Iic, ← inter_iUnion, subset_def] end LinearOrder end Set section UnionIxx variable [LinearOrder α] {s : Set α} {a : α} {f : ι → α} theorem IsGLB.biUnion_Ioi_eq (h : IsGLB s a) : ⋃ x ∈ s, Ioi x = Ioi a := by refine (iUnion₂_subset fun x hx => ?_).antisymm fun x hx => ?_ · exact Ioi_subset_Ioi (h.1 hx) · rcases h.exists_between hx with ⟨y, hys, _, hyx⟩ exact mem_biUnion hys hyx theorem IsGLB.iUnion_Ioi_eq (h : IsGLB (range f) a) : ⋃ x, Ioi (f x) = Ioi a := biUnion_range.symm.trans h.biUnion_Ioi_eq theorem IsLUB.biUnion_Iio_eq (h : IsLUB s a) : ⋃ x ∈ s, Iio x = Iio a := h.dual.biUnion_Ioi_eq theorem IsLUB.iUnion_Iio_eq (h : IsLUB (range f) a) : ⋃ x, Iio (f x) = Iio a := h.dual.iUnion_Ioi_eq theorem IsGLB.biUnion_Ici_eq_Ioi (a_glb : IsGLB s a) (a_not_mem : a ∉ s) : ⋃ x ∈ s, Ici x = Ioi a := by refine (iUnion₂_subset fun x hx => ?_).antisymm fun x hx => ?_ · exact Ici_subset_Ioi.mpr (lt_of_le_of_ne (a_glb.1 hx) fun h => (h ▸ a_not_mem) hx) · rcases a_glb.exists_between hx with ⟨y, hys, _, hyx⟩ rw [mem_iUnion₂] exact ⟨y, hys, hyx.le⟩ theorem IsGLB.biUnion_Ici_eq_Ici (a_glb : IsGLB s a) (a_mem : a ∈ s) : ⋃ x ∈ s, Ici x = Ici a := by refine (iUnion₂_subset fun x hx => ?_).antisymm fun x hx => ?_ · exact Ici_subset_Ici.mpr (mem_lowerBounds.mp a_glb.1 x hx) · exact mem_iUnion₂.mpr ⟨a, a_mem, hx⟩ theorem IsLUB.biUnion_Iic_eq_Iio (a_lub : IsLUB s a) (a_not_mem : a ∉ s) : ⋃ x ∈ s, Iic x = Iio a := a_lub.dual.biUnion_Ici_eq_Ioi a_not_mem theorem IsLUB.biUnion_Iic_eq_Iic (a_lub : IsLUB s a) (a_mem : a ∈ s) : ⋃ x ∈ s, Iic x = Iic a := a_lub.dual.biUnion_Ici_eq_Ici a_mem theorem iUnion_Ici_eq_Ioi_iInf {R : Type*} [CompleteLinearOrder R] {f : ι → R} (no_least_elem : ⨅ i, f i ∉ range f) : ⋃ i : ι, Ici (f i) = Ioi (⨅ i, f i) := by simp only [← IsGLB.biUnion_Ici_eq_Ioi (@isGLB_iInf _ _ _ f) no_least_elem, mem_range, iUnion_exists, iUnion_iUnion_eq'] theorem iUnion_Iic_eq_Iio_iSup {R : Type*} [CompleteLinearOrder R] {f : ι → R} (no_greatest_elem : (⨆ i, f i) ∉ range f) : ⋃ i : ι, Iic (f i) = Iio (⨆ i, f i) := @iUnion_Ici_eq_Ioi_iInf ι (OrderDual R) _ f no_greatest_elem theorem iUnion_Ici_eq_Ici_iInf {R : Type*} [CompleteLinearOrder R] {f : ι → R} (has_least_elem : (⨅ i, f i) ∈ range f) : ⋃ i : ι, Ici (f i) = Ici (⨅ i, f i) := by simp only [← IsGLB.biUnion_Ici_eq_Ici (@isGLB_iInf _ _ _ f) has_least_elem, mem_range, iUnion_exists, iUnion_iUnion_eq'] theorem iUnion_Iic_eq_Iic_iSup {R : Type*} [CompleteLinearOrder R] {f : ι → R} (has_greatest_elem : (⨆ i, f i) ∈ range f) : ⋃ i : ι, Iic (f i) = Iic (⨆ i, f i) := @iUnion_Ici_eq_Ici_iInf ι (OrderDual R) _ f has_greatest_elem theorem iUnion_Iio_eq_univ_iff : ⋃ i, Iio (f i) = univ ↔ (¬ BddAbove (range f)) := by simp [not_bddAbove_iff, Set.eq_univ_iff_forall] theorem iUnion_Iic_of_not_bddAbove_range (hf : ¬ BddAbove (range f)) : ⋃ i, Iic (f i) = univ := by refine Set.eq_univ_of_subset ?_ (iUnion_Iio_eq_univ_iff.mpr hf) gcongr exact Iio_subset_Iic_self theorem iInter_Iic_eq_empty_iff : ⋂ i, Iic (f i) = ∅ ↔ ¬ BddBelow (range f) := by simp [not_bddBelow_iff, Set.eq_empty_iff_forall_not_mem] theorem iInter_Iio_of_not_bddBelow_range (hf : ¬ BddBelow (range f)) : ⋂ i, Iio (f i) = ∅ := by refine eq_empty_of_subset_empty ?_ rw [← iInter_Iic_eq_empty_iff.mpr hf]
gcongr exact Iio_subset_Iic_self end UnionIxx
Mathlib/Order/Interval/Set/Disjoint.lean
245
248
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kevin Kappelmann -/ import Mathlib.Algebra.Order.Floor.Defs import Mathlib.Algebra.Order.Floor.Ring import Mathlib.Algebra.Order.Floor.Semiring deprecated_module (since := "2025-04-13")
Mathlib/Algebra/Order/Floor.lean
522
524
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import Mathlib.RingTheory.WittVector.InitTail /-! # Truncated Witt vectors The ring of truncated Witt vectors (of length `n`) is a quotient of the ring of Witt vectors. It retains the first `n` coefficients of each Witt vector. In this file, we set up the basic quotient API for this ring. The ring of Witt vectors is the projective limit of all the rings of truncated Witt vectors. ## Main declarations - `TruncatedWittVector`: the underlying type of the ring of truncated Witt vectors - `TruncatedWittVector.instCommRing`: the ring structure on truncated Witt vectors - `WittVector.truncate`: the quotient homomorphism that truncates a Witt vector, to obtain a truncated Witt vector - `TruncatedWittVector.truncate`: the homomorphism that truncates a truncated Witt vector of length `n` to one of length `m` (for some `m ≤ n`) - `WittVector.lift`: the unique ring homomorphism into the ring of Witt vectors that is compatible with a family of ring homomorphisms to the truncated Witt vectors: this realizes the ring of Witt vectors as projective limit of the rings of truncated Witt vectors ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ open Function (Injective Surjective) noncomputable section variable {p : ℕ} (n : ℕ) (R : Type*) local notation "𝕎" => WittVector p -- type as `\bbW` /-- A truncated Witt vector over `R` is a vector of elements of `R`, i.e., the first `n` coefficients of a Witt vector. We will define operations on this type that are compatible with the (untruncated) Witt vector operations. `TruncatedWittVector p n R` takes a parameter `p : ℕ` that is not used in the definition. In practice, this number `p` is assumed to be a prime number, and under this assumption we construct a ring structure on `TruncatedWittVector p n R`. (`TruncatedWittVector p₁ n R` and `TruncatedWittVector p₂ n R` are definitionally equal as types but will have different ring operations.) -/ @[nolint unusedArguments] def TruncatedWittVector (_ : ℕ) (n : ℕ) (R : Type*) := Fin n → R instance (p n : ℕ) (R : Type*) [Inhabited R] : Inhabited (TruncatedWittVector p n R) := ⟨fun _ => default⟩ variable {n R} namespace TruncatedWittVector variable (p) in /-- Create a `TruncatedWittVector` from a vector `x`. -/ def mk (x : Fin n → R) : TruncatedWittVector p n R := x /-- `x.coeff i` is the `i`th entry of `x`. -/ def coeff (i : Fin n) (x : TruncatedWittVector p n R) : R := x i @[ext] theorem ext {x y : TruncatedWittVector p n R} (h : ∀ i, x.coeff i = y.coeff i) : x = y := funext h @[simp] theorem coeff_mk (x : Fin n → R) (i : Fin n) : (mk p x).coeff i = x i := rfl @[simp] theorem mk_coeff (x : TruncatedWittVector p n R) : (mk p fun i => x.coeff i) = x := by ext i; rw [coeff_mk] variable [CommRing R] /-- We can turn a truncated Witt vector `x` into a Witt vector by setting all coefficients after `x` to be 0. -/ def out (x : TruncatedWittVector p n R) : 𝕎 R := @WittVector.mk' p _ fun i => if h : i < n then x.coeff ⟨i, h⟩ else 0 @[simp] theorem coeff_out (x : TruncatedWittVector p n R) (i : Fin n) : x.out.coeff i = x.coeff i := by rw [out]; dsimp only; rw [dif_pos i.is_lt, Fin.eta] theorem out_injective : Injective (@out p n R _) := by intro x y h ext i rw [WittVector.ext_iff] at h simpa only [coeff_out] using h ↑i end TruncatedWittVector namespace WittVector variable (n) section /-- `truncateFun n x` uses the first `n` entries of `x` to construct a `TruncatedWittVector`, which has the same base `p` as `x`. This function is bundled into a ring homomorphism in `WittVector.truncate` -/ def truncateFun (x : 𝕎 R) : TruncatedWittVector p n R := TruncatedWittVector.mk p fun i => x.coeff i end variable {n} @[simp] theorem coeff_truncateFun (x : 𝕎 R) (i : Fin n) : (truncateFun n x).coeff i = x.coeff i := by rw [truncateFun, TruncatedWittVector.coeff_mk] variable [CommRing R] @[simp] theorem out_truncateFun (x : 𝕎 R) : (truncateFun n x).out = init n x := by ext i dsimp [TruncatedWittVector.out, init, select, coeff_mk] split_ifs with hi; swap; · rfl rw [coeff_truncateFun, Fin.val_mk] end WittVector namespace TruncatedWittVector variable [CommRing R] @[simp] theorem truncateFun_out (x : TruncatedWittVector p n R) : x.out.truncateFun n = x := by simp only [WittVector.truncateFun, coeff_out, mk_coeff] open WittVector variable (p n R) variable [Fact p.Prime] instance : Zero (TruncatedWittVector p n R) := ⟨truncateFun n 0⟩ instance : One (TruncatedWittVector p n R) := ⟨truncateFun n 1⟩ instance : NatCast (TruncatedWittVector p n R) := ⟨fun i => truncateFun n i⟩ instance : IntCast (TruncatedWittVector p n R) := ⟨fun i => truncateFun n i⟩ instance : Add (TruncatedWittVector p n R) :=
⟨fun x y => truncateFun n (x.out + y.out)⟩
Mathlib/RingTheory/WittVector/Truncated.lean
166
167
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro -/ import Mathlib.Data.List.AList import Mathlib.Data.Finset.Sigma import Mathlib.Data.Part /-! # Finite maps over `Multiset` -/ universe u v w open List variable {α : Type u} {β : α → Type v} /-! ### Multisets of sigma types -/ namespace Multiset /-- Multiset of keys of an association multiset. -/ def keys (s : Multiset (Sigma β)) : Multiset α := s.map Sigma.fst @[simp] theorem coe_keys {l : List (Sigma β)} : keys (l : Multiset (Sigma β)) = (l.keys : Multiset α) := rfl @[simp] theorem keys_zero : keys (0 : Multiset (Sigma β)) = 0 := rfl @[simp] theorem keys_cons {a : α} {b : β a} {s : Multiset (Sigma β)} : keys (⟨a, b⟩ ::ₘ s) = a ::ₘ keys s := by simp [keys] @[simp] theorem keys_singleton {a : α} {b : β a} : keys ({⟨a, b⟩} : Multiset (Sigma β)) = {a} := rfl /-- `NodupKeys s` means that `s` has no duplicate keys. -/ def NodupKeys (s : Multiset (Sigma β)) : Prop := Quot.liftOn s List.NodupKeys fun _ _ p => propext <| perm_nodupKeys p @[simp] theorem coe_nodupKeys {l : List (Sigma β)} : @NodupKeys α β l ↔ l.NodupKeys := Iff.rfl lemma nodup_keys {m : Multiset (Σ a, β a)} : m.keys.Nodup ↔ m.NodupKeys := by rcases m with ⟨l⟩; rfl alias ⟨_, NodupKeys.nodup_keys⟩ := nodup_keys protected lemma NodupKeys.nodup {m : Multiset (Σ a, β a)} (h : m.NodupKeys) : m.Nodup := h.nodup_keys.of_map _ end Multiset /-! ### Finmap -/ /-- `Finmap β` is the type of finite maps over a multiset. It is effectively a quotient of `AList β` by permutation of the underlying list. -/ structure Finmap (β : α → Type v) : Type max u v where /-- The underlying `Multiset` of a `Finmap` -/ entries : Multiset (Sigma β) /-- There are no duplicate keys in `entries` -/ nodupKeys : entries.NodupKeys /-- The quotient map from `AList` to `Finmap`. -/ def AList.toFinmap (s : AList β) : Finmap β := ⟨s.entries, s.nodupKeys⟩ local notation:arg "⟦" a "⟧" => AList.toFinmap a theorem AList.toFinmap_eq {s₁ s₂ : AList β} : toFinmap s₁ = toFinmap s₂ ↔ s₁.entries ~ s₂.entries := by cases s₁ cases s₂ simp [AList.toFinmap] @[simp] theorem AList.toFinmap_entries (s : AList β) : ⟦s⟧.entries = s.entries := rfl /-- Given `l : List (Sigma β)`, create a term of type `Finmap β` by removing entries with duplicate keys. -/ def List.toFinmap [DecidableEq α] (s : List (Sigma β)) : Finmap β := s.toAList.toFinmap namespace Finmap open AList lemma nodup_entries (f : Finmap β) : f.entries.Nodup := f.nodupKeys.nodup /-! ### Lifting from AList -/ /-- Lift a permutation-respecting function on `AList` to `Finmap`. -/ def liftOn {γ} (s : Finmap β) (f : AList β → γ) (H : ∀ a b : AList β, a.entries ~ b.entries → f a = f b) : γ := by refine (Quotient.liftOn s.entries (fun (l : List (Sigma β)) => (⟨_, fun nd => f ⟨l, nd⟩⟩ : Part γ)) (fun l₁ l₂ p => Part.ext' (perm_nodupKeys p) ?_) : Part γ).get ?_ · exact fun h1 h2 => H _ _ p · have := s.nodupKeys revert this rcases s.entries with ⟨l⟩ exact id @[simp] theorem liftOn_toFinmap {γ} (s : AList β) (f : AList β → γ) (H) : liftOn ⟦s⟧ f H = f s := by cases s rfl /-- Lift a permutation-respecting function on 2 `AList`s to 2 `Finmap`s. -/ def liftOn₂ {γ} (s₁ s₂ : Finmap β) (f : AList β → AList β → γ) (H : ∀ a₁ b₁ a₂ b₂ : AList β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries → f a₁ b₁ = f a₂ b₂) : γ := liftOn s₁ (fun l₁ => liftOn s₂ (f l₁) fun _ _ p => H _ _ _ _ (Perm.refl _) p) fun a₁ a₂ p => by have H' : f a₁ = f a₂ := funext fun _ => H _ _ _ _ p (Perm.refl _) simp only [H'] @[simp] theorem liftOn₂_toFinmap {γ} (s₁ s₂ : AList β) (f : AList β → AList β → γ) (H) : liftOn₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ := by cases s₁; cases s₂; rfl /-! ### Induction -/ @[elab_as_elim] theorem induction_on {C : Finmap β → Prop} (s : Finmap β) (H : ∀ a : AList β, C ⟦a⟧) : C s := by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩ @[elab_as_elim] theorem induction_on₂ {C : Finmap β → Finmap β → Prop} (s₁ s₂ : Finmap β) (H : ∀ a₁ a₂ : AList β, C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ := induction_on s₁ fun l₁ => induction_on s₂ fun l₂ => H l₁ l₂ @[elab_as_elim] theorem induction_on₃ {C : Finmap β → Finmap β → Finmap β → Prop} (s₁ s₂ s₃ : Finmap β) (H : ∀ a₁ a₂ a₃ : AList β, C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ := induction_on₂ s₁ s₂ fun l₁ l₂ => induction_on s₃ fun l₃ => H l₁ l₂ l₃ /-! ### extensionality -/ @[ext] theorem ext : ∀ {s t : Finmap β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩, ⟨l₂, _⟩, H => by congr @[simp] theorem ext_iff' {s t : Finmap β} : s.entries = t.entries ↔ s = t := Finmap.ext_iff.symm /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : Membership α (Finmap β) := ⟨fun s a => a ∈ s.entries.keys⟩ theorem mem_def {a : α} {s : Finmap β} : a ∈ s ↔ a ∈ s.entries.keys := Iff.rfl @[simp] theorem mem_toFinmap {a : α} {s : AList β} : a ∈ toFinmap s ↔ a ∈ s := Iff.rfl /-! ### keys -/ /-- The set of keys of a finite map. -/ def keys (s : Finmap β) : Finset α := ⟨s.entries.keys, s.nodupKeys.nodup_keys⟩ @[simp] theorem keys_val (s : AList β) : (keys ⟦s⟧).val = s.keys := rfl @[simp] theorem keys_ext {s₁ s₂ : AList β} : keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys := by simp [keys, AList.keys] theorem mem_keys {a : α} {s : Finmap β} : a ∈ s.keys ↔ a ∈ s := induction_on s fun _ => AList.mem_keys /-! ### empty -/ /-- The empty map. -/ instance : EmptyCollection (Finmap β) := ⟨⟨0, nodupKeys_nil⟩⟩ instance : Inhabited (Finmap β) := ⟨∅⟩ @[simp] theorem empty_toFinmap : (⟦∅⟧ : Finmap β) = ∅ := rfl @[simp] theorem toFinmap_nil [DecidableEq α] : ([].toFinmap : Finmap β) = ∅ := rfl theorem not_mem_empty {a : α} : a ∉ (∅ : Finmap β) := Multiset.not_mem_zero a @[simp] theorem keys_empty : (∅ : Finmap β).keys = ∅ := rfl /-! ### singleton -/ /-- The singleton map. -/ def singleton (a : α) (b : β a) : Finmap β := ⟦AList.singleton a b⟧ @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = {a} := rfl @[simp] theorem mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y := by simp [singleton, mem_def] section variable [DecidableEq α] instance decidableEq [∀ a, DecidableEq (β a)] : DecidableEq (Finmap β) | _, _ => decidable_of_iff _ Finmap.ext_iff.symm /-! ### lookup -/ /-- Look up the value associated to a key in a map. -/ def lookup (a : α) (s : Finmap β) : Option (β a) := liftOn s (AList.lookup a) fun _ _ => perm_lookup @[simp] theorem lookup_toFinmap (a : α) (s : AList β) : lookup a ⟦s⟧ = s.lookup a := rfl @[simp] theorem dlookup_list_toFinmap (a : α) (s : List (Sigma β)) : lookup a s.toFinmap = s.dlookup a := by rw [List.toFinmap, lookup_toFinmap, lookup_to_alist] @[simp] theorem lookup_empty (a) : lookup a (∅ : Finmap β) = none := rfl theorem lookup_isSome {a : α} {s : Finmap β} : (s.lookup a).isSome ↔ a ∈ s := induction_on s fun _ => AList.lookup_isSome theorem lookup_eq_none {a} {s : Finmap β} : lookup a s = none ↔ a ∉ s := induction_on s fun _ => AList.lookup_eq_none lemma mem_lookup_iff {s : Finmap β} {a : α} {b : β a} : b ∈ s.lookup a ↔ Sigma.mk a b ∈ s.entries := by rcases s with ⟨⟨l⟩, hl⟩; exact List.mem_dlookup_iff hl lemma lookup_eq_some_iff {s : Finmap β} {a : α} {b : β a} : s.lookup a = b ↔ Sigma.mk a b ∈ s.entries := mem_lookup_iff @[simp] lemma sigma_keys_lookup (s : Finmap β) : s.keys.sigma (fun i => (s.lookup i).toFinset) = ⟨s.entries, s.nodup_entries⟩ := by ext x have : x ∈ s.entries → x.1 ∈ s.keys := Multiset.mem_map_of_mem _ simpa [lookup_eq_some_iff] @[simp] theorem lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b := by rw [singleton, lookup_toFinmap, AList.singleton, AList.lookup, dlookup_cons_eq] instance (a : α) (s : Finmap β) : Decidable (a ∈ s) := decidable_of_iff _ lookup_isSome theorem mem_iff {a : α} {s : Finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b := induction_on s fun s => Iff.trans List.mem_keys <| exists_congr fun _ => (mem_dlookup_iff s.nodupKeys).symm theorem mem_of_lookup_eq_some {a : α} {b : β a} {s : Finmap β} (h : s.lookup a = some b) : a ∈ s := mem_iff.mpr ⟨_, h⟩ theorem ext_lookup {s₁ s₂ : Finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ := induction_on₂ s₁ s₂ fun s₁ s₂ h => by simp only [AList.lookup, lookup_toFinmap] at h rw [AList.toFinmap_eq] apply lookup_ext s₁.nodupKeys s₂.nodupKeys intro x y rw [h] /-- An equivalence between `Finmap β` and pairs `(keys : Finset α, lookup : ∀ a, Option (β a))` such that `(lookup a).isSome ↔ a ∈ keys`. -/ @[simps apply_coe_fst apply_coe_snd] def keysLookupEquiv : Finmap β ≃ { f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1 } where toFun s := ⟨(s.keys, fun i => s.lookup i), fun _ => lookup_isSome⟩ invFun f := mk (f.1.1.sigma fun i => (f.1.2 i).toFinset).val <| by refine Multiset.nodup_keys.1 ((Finset.nodup _).map_on ?_) simp only [Finset.mem_val, Finset.mem_sigma, Option.mem_toFinset, Option.mem_def] rintro ⟨i, x⟩ ⟨_, hx⟩ ⟨j, y⟩ ⟨_, hy⟩ (rfl : i = j) simpa using hx.symm.trans hy left_inv f := ext <| by simp right_inv := fun ⟨(s, f), hf⟩ => by dsimp only at hf ext · simp [keys, Multiset.keys, ← hf, Option.isSome_iff_exists] · simp +contextual [lookup_eq_some_iff, ← hf] @[simp] lemma keysLookupEquiv_symm_apply_keys : ∀ f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}, (keysLookupEquiv.symm f).keys = f.1.1 := keysLookupEquiv.surjective.forall.2 fun _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_fst] @[simp] lemma keysLookupEquiv_symm_apply_lookup : ∀ (f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}) a, (keysLookupEquiv.symm f).lookup a = f.1.2 a := keysLookupEquiv.surjective.forall.2 fun _ _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_snd] /-! ### replace -/ /-- Replace a key with a given value in a finite map. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.replace a b t)) fun _ _ p => toFinmap_eq.2 <| perm_replace p @[simp] theorem replace_toFinmap (a : α) (b : β a) (s : AList β) : replace a b ⟦s⟧ = (⟦s.replace a b⟧ : Finmap β) := by simp [replace] @[simp] theorem keys_replace (a : α) (b : β a) (s : Finmap β) : (replace a b s).keys = s.keys := induction_on s fun s => by simp @[simp] theorem mem_replace {a a' : α} {b : β a} {s : Finmap β} : a' ∈ replace a b s ↔ a' ∈ s := induction_on s fun s => by simp end /-! ### foldl -/ /-- Fold a commutative function over the key-value pairs in the map -/ def foldl {δ : Type w} (f : δ → ∀ a, β a → δ) (H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁) (d : δ) (m : Finmap β) : δ := letI : RightCommutative fun d (s : Sigma β) ↦ f d s.1 s.2 := ⟨fun _ _ _ ↦ H _ _ _ _ _⟩ m.entries.foldl (fun d s => f d s.1 s.2) d /-- `any f s` returns `true` iff there exists a value `v` in `s` such that `f v = true`. -/ def any (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x || f y z) (fun _ _ _ _ => by simp_rw [Bool.or_assoc, Bool.or_comm, imp_true_iff]) false /-- `all f s` returns `true` iff `f v = true` for all values `v` in `s`. -/ def all (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x && f y z) (fun _ _ _ _ => by simp_rw [Bool.and_assoc, Bool.and_comm, imp_true_iff]) true /-! ### erase -/ section variable [DecidableEq α] /-- Erase a key from the map. If the key is not present it does nothing. -/ def erase (a : α) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.erase a t)) fun _ _ p => toFinmap_eq.2 <| perm_erase p @[simp] theorem erase_toFinmap (a : α) (s : AList β) : erase a ⟦s⟧ = AList.toFinmap (s.erase a) := by simp [erase] @[simp] theorem keys_erase_toFinset (a : α) (s : AList β) : keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a := by simp [Finset.erase, keys, AList.erase, keys_kerase] @[simp] theorem keys_erase (a : α) (s : Finmap β) : (erase a s).keys = s.keys.erase a := induction_on s fun s => by simp @[simp] theorem mem_erase {a a' : α} {s : Finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := induction_on s fun s => by simp theorem not_mem_erase_self {a : α} {s : Finmap β} : ¬a ∈ erase a s := by rw [mem_erase, not_and_or, not_not] left rfl @[simp] theorem lookup_erase (a) (s : Finmap β) : lookup a (erase a s) = none := induction_on s <| AList.lookup_erase a @[simp] theorem lookup_erase_ne {a a'} {s : Finmap β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := induction_on s fun _ => AList.lookup_erase_ne h theorem erase_erase {a a' : α} {s : Finmap β} : erase a (erase a' s) = erase a' (erase a s) := induction_on s fun s => ext (by simp only [AList.erase_erase, erase_toFinmap]) /-! ### sdiff -/ /-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or `s'` but not both. -/ def sdiff (s s' : Finmap β) : Finmap β := s'.foldl (fun s x _ => s.erase x) (fun _ _ _ _ _ => erase_erase) s instance : SDiff (Finmap β) := ⟨sdiff⟩ /-! ### insert -/ /-- Insert a key-value pair into a finite map, replacing any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.insert a b t)) fun _ _ p => toFinmap_eq.2 <| perm_insert p @[simp] theorem insert_toFinmap (a : α) (b : β a) (s : AList β) : insert a b (AList.toFinmap s) = AList.toFinmap (s.insert a b) := by simp [insert] theorem entries_insert_of_not_mem {a : α} {b : β a} {s : Finmap β} : a ∉ s → (insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries := induction_on s fun s h => by simp [AList.entries_insert_of_not_mem (mt mem_toFinmap.1 h), -entries_insert] @[deprecated (since := "2024-12-14")] alias insert_entries_of_neg := entries_insert_of_not_mem @[simp] theorem mem_insert {a a' : α} {b' : β a'} {s : Finmap β} : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := induction_on s AList.mem_insert @[simp] theorem lookup_insert {a} {b : β a} (s : Finmap β) : lookup a (insert a b s) = some b := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, AList.lookup_insert] @[simp] theorem lookup_insert_of_ne {a a'} {b : β a} (s : Finmap β) (h : a' ≠ a) : lookup a' (insert a b s) = lookup a' s := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, lookup_insert_ne h] @[simp] theorem insert_insert {a} {b b' : β a} (s : Finmap β) : (s.insert a b).insert a b' = s.insert a b' := induction_on s fun s => by simp only [insert_toFinmap, AList.insert_insert] theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : Finmap β) (h : a ≠ a') : (s.insert a b).insert a' b' = (s.insert a' b').insert a b := induction_on s fun s => by simp only [insert_toFinmap, AList.toFinmap_eq, AList.insert_insert_of_ne _ h] theorem toFinmap_cons (a : α) (b : β a) (xs : List (Sigma β)) : List.toFinmap (⟨a, b⟩ :: xs) = insert a b xs.toFinmap := rfl theorem mem_list_toFinmap (a : α) (xs : List (Sigma β)) : a ∈ xs.toFinmap ↔ ∃ b : β a, Sigma.mk a b ∈ xs := by induction' xs with x xs · simp only [toFinmap_nil, not_mem_empty, find?, not_mem_nil, exists_false] obtain ⟨fst_i, snd_i⟩ := x simp only [toFinmap_cons, *, exists_or, mem_cons, mem_insert, exists_and_left, Sigma.mk.inj_iff] refine (or_congr_left <| and_iff_left_of_imp ?_).symm rintro rfl simp only [exists_eq, heq_iff_eq] @[simp] theorem insert_singleton_eq {a : α} {b b' : β a} : insert a b (singleton a b') = singleton a b := by simp only [singleton, Finmap.insert_toFinmap, AList.insert_singleton_eq] /-! ### extract -/ /-- Erase a key from the map, and return the corresponding value, if found. -/ def extract (a : α) (s : Finmap β) : Option (β a) × Finmap β := (liftOn s fun t => Prod.map id AList.toFinmap (AList.extract a t)) fun s₁ s₂ p => by simp [perm_lookup p, toFinmap_eq, perm_erase p] @[simp] theorem extract_eq_lookup_erase (a : α) (s : Finmap β) : extract a s = (lookup a s, erase a s) := induction_on s fun s => by simp [extract] /-! ### union -/ /-- `s₁ ∪ s₂` is the key-based union of two finite maps. It is left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/ def union (s₁ s₂ : Finmap β) : Finmap β := (liftOn₂ s₁ s₂ fun s₁ s₂ => (AList.toFinmap (s₁ ∪ s₂))) fun _ _ _ _ p₁₃ p₂₄ => toFinmap_eq.mpr <| perm_union p₁₃ p₂₄ instance : Union (Finmap β) := ⟨union⟩ @[simp] theorem mem_union {a} {s₁ s₂ : Finmap β} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_union @[simp]
theorem union_toFinmap (s₁ s₂ : AList β) : (toFinmap s₁) ∪ (toFinmap s₂) = toFinmap (s₁ ∪ s₂) := by simp [(· ∪ ·), union]
Mathlib/Data/Finmap.lean
502
504
/- 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.BoxIntegral.DivergenceTheorem import Mathlib.Analysis.BoxIntegral.Integrability import Mathlib.Analysis.Calculus.Deriv.Basic import Mathlib.Analysis.Calculus.FDeriv.Equiv import Mathlib.MeasureTheory.Integral.Prod import Mathlib.MeasureTheory.Integral.IntervalIntegral.Basic /-! # Divergence theorem for Bochner integral In this file we prove the Divergence theorem for Bochner integral on a box in `ℝⁿ⁺¹ = Fin (n + 1) → ℝ`. More precisely, we prove the following theorem. Let `E` be a complete normed space. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is continuous on a rectangular box `[a, b] : Set ℝⁿ⁺¹`, `a ≤ b`, differentiable on its interior with derivative `f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹`, and the divergence `fun x ↦ ∑ i, f' x eᵢ i` is integrable on `[a, b]`, where `eᵢ = Pi.single i 1` is the `i`-th basis vector, then its integral is equal to the sum of integrals of `f` over the faces of `[a, b]`, taken with appropriate signs. Moreover, the same is true if the function is not differentiable at countably many points of the interior of `[a, b]`. Once we prove the general theorem, we deduce corollaries for functions `ℝ → E` and pairs of functions `(ℝ × ℝ) → E`. ## Notations We use the following local notation to make the statement more readable. Note that the documentation website shows the actual terms, not those abbreviated using local notations. Porting note (Yury Kudryashov): I disabled some of these notations because I failed to make them work with Lean 4. * `ℝⁿ`, `ℝⁿ⁺¹`, `Eⁿ⁺¹`: `Fin n → ℝ`, `Fin (n + 1) → ℝ`, `Fin (n + 1) → E`; * `face i`: the `i`-th face of the box `[a, b]` as a closed segment in `ℝⁿ`, namely `[a ∘ Fin.succAbove i, b ∘ Fin.succAbove i]`; * `e i` : `i`-th basis vector `Pi.single i 1`; * `frontFace i`, `backFace i`: embeddings `ℝⁿ → ℝⁿ⁺¹` corresponding to the front face `{x | x i = b i}` and back face `{x | x i = a i}` of the box `[a, b]`, respectively. They are given by `Fin.insertNth i (b i)` and `Fin.insertNth i (a i)`. ## TODO * Add a version that assumes existence and integrability of partial derivatives. * Restore local notations for find another way to make the statements more readable. ## Tags divergence theorem, Bochner integral -/ open Set Finset TopologicalSpace Function BoxIntegral MeasureTheory Filter open scoped Topology Interval universe u namespace MeasureTheory variable {E : Type u} [NormedAddCommGroup E] [NormedSpace ℝ E] section variable {n : ℕ} local macro:arg t:term:max noWs "ⁿ" : term => `(Fin n → $t) local macro:arg t:term:max noWs "ⁿ⁺¹" : term => `(Fin (n + 1) → $t) local notation "e " i => Pi.single i 1 section /-! ### Divergence theorem for functions on `ℝⁿ⁺¹ = Fin (n + 1) → ℝ`. In this section we use the divergence theorem for a Henstock-Kurzweil-like integral `BoxIntegral.hasIntegral_GP_divergence_of_forall_hasDerivWithinAt` to prove the divergence theorem for Bochner integral. The divergence theorem for Bochner integral `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable` assumes that the function itself is continuous on a closed box, differentiable at all but countably many points of its interior, and the divergence is integrable on the box. This statement differs from `BoxIntegral.hasIntegral_GP_divergence_of_forall_hasDerivWithinAt` in several aspects. * We use Bochner integral instead of a Henstock-Kurzweil integral. This modification is done in `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable_aux₁`. As a side effect of this change, we need to assume that the divergence is integrable. * We don't assume differentiability on the boundary of the box. This modification is done in `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable_aux₂`. To prove it, we choose an increasing sequence of smaller boxes that cover the interior of the original box, then apply the previous lemma to these smaller boxes and take the limit of both sides of the equation. * We assume `a ≤ b` instead of `∀ i, a i < b i`. This is the last step of the proof, and it is done in the main theorem `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable`. -/ /-- An auxiliary lemma for `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable`. This is exactly `BoxIntegral.hasIntegral_GP_divergence_of_forall_hasDerivWithinAt` reformulated for the Bochner integral. -/ theorem integral_divergence_of_hasFDerivWithinAt_off_countable_aux₁ (I : Box (Fin (n + 1))) (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : Set ℝⁿ⁺¹) (hs : s.Countable) (Hc : ContinuousOn f (Box.Icc I)) (Hd : ∀ x ∈ (Box.Icc I) \ s, HasFDerivWithinAt f (f' x) (Box.Icc I) x) (Hi : IntegrableOn (fun x => ∑ i, f' x (e i) i) (Box.Icc I)) : (∫ x in Box.Icc I, ∑ i, f' x (e i) i) = ∑ i : Fin (n + 1), ((∫ x in Box.Icc (I.face i), f (i.insertNth (I.upper i) x) i) - ∫ x in Box.Icc (I.face i), f (i.insertNth (I.lower i) x) i) := by wlog hE : CompleteSpace E generalizing · simp [integral, hE] simp only [← setIntegral_congr_set (Box.coe_ae_eq_Icc _)] have A := (Hi.mono_set Box.coe_subset_Icc).hasBoxIntegral ⊥ rfl have B := hasIntegral_GP_divergence_of_forall_hasDerivWithinAt I f f' (s ∩ Box.Icc I) (hs.mono inter_subset_left) (fun x hx => Hc _ hx.2) fun x hx => Hd _ ⟨hx.1, fun h => hx.2 ⟨h, hx.1⟩⟩ rw [continuousOn_pi] at Hc refine (A.unique B).trans (sum_congr rfl fun i _ => ?_) refine congr_arg₂ Sub.sub ?_ ?_ · have := Box.continuousOn_face_Icc (Hc i) (Set.right_mem_Icc.2 (I.lower_le_upper i)) have := (this.integrableOn_compact (μ := volume) (Box.isCompact_Icc _)).mono_set Box.coe_subset_Icc exact (this.hasBoxIntegral ⊥ rfl).integral_eq · have := Box.continuousOn_face_Icc (Hc i) (Set.left_mem_Icc.2 (I.lower_le_upper i)) have := (this.integrableOn_compact (μ := volume) (Box.isCompact_Icc _)).mono_set Box.coe_subset_Icc exact (this.hasBoxIntegral ⊥ rfl).integral_eq /-- An auxiliary lemma for `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable`. Compared to the previous lemma, here we drop the assumption of differentiability on the boundary of the box. -/ theorem integral_divergence_of_hasFDerivWithinAt_off_countable_aux₂ (I : Box (Fin (n + 1))) (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : Set ℝⁿ⁺¹) (hs : s.Countable) (Hc : ContinuousOn f (Box.Icc I)) (Hd : ∀ x ∈ Box.Ioo I \ s, HasFDerivAt f (f' x) x) (Hi : IntegrableOn (∑ i, f' · (e i) i) (Box.Icc I)) : (∫ x in Box.Icc I, ∑ i, f' x (e i) i) = ∑ i : Fin (n + 1), ((∫ x in Box.Icc (I.face i), f (i.insertNth (I.upper i) x) i) - ∫ x in Box.Icc (I.face i), f (i.insertNth (I.lower i) x) i) := by /- Choose a monotone sequence `J k` of subboxes that cover the interior of `I` and prove that these boxes satisfy the assumptions of the previous lemma. -/ rcases I.exists_seq_mono_tendsto with ⟨J, hJ_sub, hJl, hJu⟩ have hJ_sub' : ∀ k, Box.Icc (J k) ⊆ Box.Icc I := fun k => (hJ_sub k).trans I.Ioo_subset_Icc have hJ_le : ∀ k, J k ≤ I := fun k => Box.le_iff_Icc.2 (hJ_sub' k) have HcJ : ∀ k, ContinuousOn f (Box.Icc (J k)) := fun k => Hc.mono (hJ_sub' k) have HdJ : ∀ (k), ∀ x ∈ (Box.Icc (J k)) \ s, HasFDerivWithinAt f (f' x) (Box.Icc (J k)) x := fun k x hx => (Hd x ⟨hJ_sub k hx.1, hx.2⟩).hasFDerivWithinAt have HiJ : ∀ k, IntegrableOn (∑ i, f' · (e i) i) (Box.Icc (J k)) volume := fun k => Hi.mono_set (hJ_sub' k) -- Apply the previous lemma to `J k`. have HJ_eq := fun k => integral_divergence_of_hasFDerivWithinAt_off_countable_aux₁ (J k) f f' s hs (HcJ k) (HdJ k) (HiJ k) -- Note that the LHS of `HJ_eq k` tends to the LHS of the goal as `k → ∞`. have hI_tendsto : Tendsto (fun k => ∫ x in Box.Icc (J k), ∑ i, f' x (e i) i) atTop (𝓝 (∫ x in Box.Icc I, ∑ i, f' x (e i) i)) := by simp only [IntegrableOn, ← Measure.restrict_congr_set (Box.Ioo_ae_eq_Icc _)] at Hi ⊢ rw [← Box.iUnion_Ioo_of_tendsto J.monotone hJl hJu] at Hi ⊢ exact tendsto_setIntegral_of_monotone (fun k => (J k).measurableSet_Ioo) (Box.Ioo.comp J).monotone Hi -- Thus it suffices to prove the same about the RHS. refine tendsto_nhds_unique_of_eventuallyEq hI_tendsto ?_ (Eventually.of_forall HJ_eq) clear hI_tendsto rw [tendsto_pi_nhds] at hJl hJu /- We'll need to prove a similar statement about the integrals over the front sides and the integrals over the back sides. In order to avoid repeating ourselves, we formulate a lemma. -/ suffices ∀ (i : Fin (n + 1)) (c : ℕ → ℝ) (d), (∀ k, c k ∈ Icc (I.lower i) (I.upper i)) → Tendsto c atTop (𝓝 d) → Tendsto (fun k => ∫ x in Box.Icc ((J k).face i), f (i.insertNth (c k) x) i) atTop (𝓝 <| ∫ x in Box.Icc (I.face i), f (i.insertNth d x) i) by rw [Box.Icc_eq_pi] at hJ_sub' refine tendsto_finset_sum _ fun i _ => (this _ _ _ ?_ (hJu _)).sub (this _ _ _ ?_ (hJl _)) exacts [fun k => hJ_sub' k (J k).upper_mem_Icc _ trivial, fun k => hJ_sub' k (J k).lower_mem_Icc _ trivial] intro i c d hc hcd /- First we prove that the integrals of the restriction of `f` to `{x | x i = d}` over increasing boxes `((J k).face i).Icc` tend to the desired limit. The proof mostly repeats the one above. -/ have hd : d ∈ Icc (I.lower i) (I.upper i) := isClosed_Icc.mem_of_tendsto hcd (Eventually.of_forall hc) have Hic : ∀ k, IntegrableOn (fun x => f (i.insertNth (c k) x) i) (Box.Icc (I.face i)) := fun k => (Box.continuousOn_face_Icc ((continuous_apply i).comp_continuousOn Hc) (hc k)).integrableOn_Icc have Hid : IntegrableOn (fun x => f (i.insertNth d x) i) (Box.Icc (I.face i)) := (Box.continuousOn_face_Icc ((continuous_apply i).comp_continuousOn Hc) hd).integrableOn_Icc have H : Tendsto (fun k => ∫ x in Box.Icc ((J k).face i), f (i.insertNth d x) i) atTop (𝓝 <| ∫ x in Box.Icc (I.face i), f (i.insertNth d x) i) := by have hIoo : (⋃ k, Box.Ioo ((J k).face i)) = Box.Ioo (I.face i) := Box.iUnion_Ioo_of_tendsto ((Box.monotone_face i).comp J.monotone) (tendsto_pi_nhds.2 fun _ => hJl _) (tendsto_pi_nhds.2 fun _ => hJu _) simp only [IntegrableOn, ← Measure.restrict_congr_set (Box.Ioo_ae_eq_Icc _), ← hIoo] at Hid ⊢ exact tendsto_setIntegral_of_monotone (fun k => ((J k).face i).measurableSet_Ioo) (Box.Ioo.monotone.comp ((Box.monotone_face i).comp J.monotone)) Hid /- Thus it suffices to show that the distance between the integrals of the restrictions of `f` to `{x | x i = c k}` and `{x | x i = d}` over `((J k).face i).Icc` tends to zero as `k → ∞`. Choose `ε > 0`. -/ refine H.congr_dist (Metric.nhds_basis_closedBall.tendsto_right_iff.2 fun ε εpos => ?_) have hvol_pos : ∀ J : Box (Fin n), 0 < ∏ j, (J.upper j - J.lower j) := fun J => prod_pos fun j hj => sub_pos.2 <| J.lower_lt_upper _ /- Choose `δ > 0` such that for any `x y ∈ I.Icc` at distance at most `δ`, the distance between `f x` and `f y` is at most `ε / volume (I.face i).Icc`, then the distance between the integrals is at most `(ε / volume (I.face i).Icc) * volume ((J k).face i).Icc ≤ ε`. -/ rcases Metric.uniformContinuousOn_iff_le.1 (I.isCompact_Icc.uniformContinuousOn_of_continuous Hc) (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) (div_pos εpos (hvol_pos (I.face i))) with ⟨δ, δpos, hδ⟩ refine (hcd.eventually (Metric.ball_mem_nhds _ δpos)).mono fun k hk => ?_ have Hsub : Box.Icc ((J k).face i) ⊆ Box.Icc (I.face i) := Box.le_iff_Icc.1 (Box.face_mono (hJ_le _) i) rw [mem_closedBall_zero_iff, Real.norm_eq_abs, abs_of_nonneg dist_nonneg, dist_eq_norm, ← integral_sub (Hid.mono_set Hsub) ((Hic _).mono_set Hsub)] calc ‖∫ x in Box.Icc ((J k).face i), f (i.insertNth d x) i - f (i.insertNth (c k) x) i‖ ≤ (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) * (volume (Box.Icc ((J k).face i))).toReal := by refine norm_setIntegral_le_of_norm_le_const (((J k).face i).measure_Icc_lt_top _) fun x hx => ?_ rw [← dist_eq_norm] calc dist (f (i.insertNth d x) i) (f (i.insertNth (c k) x) i) ≤ dist (f (i.insertNth d x)) (f (i.insertNth (c k) x)) := dist_le_pi_dist (f (i.insertNth d x)) (f (i.insertNth (c k) x)) i _ ≤ ε / ∏ j, ((I.face i).upper j - (I.face i).lower j) := hδ _ (I.mapsTo_insertNth_face_Icc hd <| Hsub hx) _ (I.mapsTo_insertNth_face_Icc (hc _) <| Hsub hx) ?_ rw [Fin.dist_insertNth_insertNth, dist_self, dist_comm] exact max_le hk.le δpos.lt.le _ ≤ ε := by rw [Box.Icc_def, Real.volume_Icc_pi_toReal ((J k).face i).lower_le_upper, ← le_div_iff₀ (hvol_pos _)] gcongr exacts [hvol_pos _, fun _ _ ↦ sub_nonneg.2 (Box.lower_le_upper _ _), (hJ_sub' _ (J _).upper_mem_Icc).2 _, (hJ_sub' _ (J _).lower_mem_Icc).1 _] variable (a b : Fin (n + 1) → ℝ) local notation "face " i => Set.Icc (a ∘ Fin.succAbove i) (b ∘ Fin.succAbove i) local notation:max "frontFace " i:arg => Fin.insertNth i (b i) local notation:max "backFace " i:arg => Fin.insertNth i (a i) /-- **Divergence theorem** for Bochner integral. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is continuous on a rectangular box `[a, b] : Set ℝⁿ⁺¹`, `a ≤ b`, is differentiable on its interior with derivative `f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹` and the divergence `fun x ↦ ∑ i, f' x eᵢ i` is integrable on `[a, b]`, where `eᵢ = Pi.single i 1` is the `i`-th basis vector, then its integral is equal to the sum of integrals of `f` over the faces of `[a, b]`, taken with appropriate signs. Moreover, the same is true if the function is not differentiable at countably many points of the interior of `[a, b]`. We represent both faces `x i = a i` and `x i = b i` as the box `face i = [a ∘ Fin.succAbove i, b ∘ Fin.succAbove i]` in `ℝⁿ`, where `Fin.succAbove : Fin n ↪o Fin (n + 1)` is the order embedding with range `{i}ᶜ`. The restrictions of `f : ℝⁿ⁺¹ → Eⁿ⁺¹` to these faces are given by `f ∘ backFace i` and `f ∘ frontFace i`, where `backFace i = Fin.insertNth i (a i)` and `frontFace i = Fin.insertNth i (b i)` are embeddings `ℝⁿ → ℝⁿ⁺¹` that take `y : ℝⁿ` and insert `a i` (resp., `b i`) as `i`-th coordinate. -/ theorem integral_divergence_of_hasFDerivWithinAt_off_countable (hle : a ≤ b) (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : Set ℝⁿ⁺¹) (hs : s.Countable) (Hc : ContinuousOn f (Icc a b)) (Hd : ∀ x ∈ (Set.pi univ fun i => Ioo (a i) (b i)) \ s, HasFDerivAt f (f' x) x) (Hi : IntegrableOn (fun x => ∑ i, f' x (e i) i) (Icc a b)) : (∫ x in Icc a b, ∑ i, f' x (e i) i) = ∑ i : Fin (n + 1), ((∫ x in face i, f (frontFace i x) i) - ∫ x in face i, f (backFace i x) i) := by rcases em (∃ i, a i = b i) with (⟨i, hi⟩ | hne) · -- First we sort out the trivial case `∃ i, a i = b i`. rw [volume_pi, ← setIntegral_congr_set Measure.univ_pi_Ioc_ae_eq_Icc] have hi' : Ioc (a i) (b i) = ∅ := Ioc_eq_empty hi.not_lt have : (pi Set.univ fun j => Ioc (a j) (b j)) = ∅ := univ_pi_eq_empty hi' rw [this, setIntegral_empty, sum_eq_zero] rintro j - rcases eq_or_ne i j with (rfl | hne) · simp [hi] · rcases Fin.exists_succAbove_eq hne with ⟨i, rfl⟩ have : Icc (a ∘ j.succAbove) (b ∘ j.succAbove) =ᵐ[volume] (∅ : Set ℝⁿ) := by rw [ae_eq_empty, Real.volume_Icc_pi, prod_eq_zero (Finset.mem_univ i)] simp [hi] rw [setIntegral_congr_set this, setIntegral_congr_set this, setIntegral_empty, setIntegral_empty, sub_self] · -- In the non-trivial case `∀ i, a i < b i`, we apply a lemma we proved above. have hlt : ∀ i, a i < b i := fun i => (hle i).lt_of_ne fun hi => hne ⟨i, hi⟩ exact integral_divergence_of_hasFDerivWithinAt_off_countable_aux₂ ⟨a, b, hlt⟩ f f' s hs Hc Hd Hi /-- **Divergence theorem** for a family of functions `f : Fin (n + 1) → ℝⁿ⁺¹ → E`. See also `MeasureTheory.integral_divergence_of_hasFDerivWithinAt_off_countable'` for a version formulated in terms of a vector-valued function `f : ℝⁿ⁺¹ → Eⁿ⁺¹`. -/ theorem integral_divergence_of_hasFDerivWithinAt_off_countable' (hle : a ≤ b) (f : Fin (n + 1) → ℝⁿ⁺¹ → E) (f' : Fin (n + 1) → ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] E) (s : Set ℝⁿ⁺¹) (hs : s.Countable) (Hc : ∀ i, ContinuousOn (f i) (Icc a b)) (Hd : ∀ x ∈ (pi Set.univ fun i => Ioo (a i) (b i)) \ s, ∀ (i), HasFDerivAt (f i) (f' i x) x) (Hi : IntegrableOn (fun x => ∑ i, f' i x (e i)) (Icc a b)) : (∫ x in Icc a b, ∑ i, f' i x (e i)) = ∑ i : Fin (n + 1), ((∫ x in face i, f i (frontFace i x)) - ∫ x in face i, f i (backFace i x)) := integral_divergence_of_hasFDerivWithinAt_off_countable a b hle (fun x i => f i x) (fun x => ContinuousLinearMap.pi fun i => f' i x) s hs (continuousOn_pi.2 Hc) (fun x hx => hasFDerivAt_pi.2 (Hd x hx)) Hi end /-- An auxiliary lemma that is used to specialize the general divergence theorem to spaces that do not have the form `Fin n → ℝ`. -/ theorem integral_divergence_of_hasFDerivWithinAt_off_countable_of_equiv {F : Type*} [NormedAddCommGroup F] [NormedSpace ℝ F] [Preorder F] [MeasureSpace F] [BorelSpace F] (eL : F ≃L[ℝ] ℝⁿ⁺¹) (he_ord : ∀ x y, eL x ≤ eL y ↔ x ≤ y) (he_vol : MeasurePreserving eL volume volume) (f : Fin (n + 1) → F → E) (f' : Fin (n + 1) → F → F →L[ℝ] E) (s : Set F) (hs : s.Countable) (a b : F) (hle : a ≤ b) (Hc : ∀ i, ContinuousOn (f i) (Icc a b)) (Hd : ∀ x ∈ interior (Icc a b) \ s, ∀ (i), HasFDerivAt (f i) (f' i x) x) (DF : F → E) (hDF : ∀ x, DF x = ∑ i, f' i x (eL.symm <| e i)) (Hi : IntegrableOn DF (Icc a b)) : ∫ x in Icc a b, DF x = ∑ i : Fin (n + 1), ((∫ x in Icc (eL a ∘ i.succAbove) (eL b ∘ i.succAbove), f i (eL.symm <| i.insertNth (eL b i) x)) - ∫ x in Icc (eL a ∘ i.succAbove) (eL b ∘ i.succAbove), f i (eL.symm <| i.insertNth (eL a i) x)) := have he_emb : MeasurableEmbedding eL := eL.toHomeomorph.measurableEmbedding have hIcc : eL ⁻¹' Icc (eL a) (eL b) = Icc a b := by ext1 x; simp only [Set.mem_preimage, Set.mem_Icc, he_ord] have hIcc' : Icc (eL a) (eL b) = eL.symm ⁻¹' Icc a b := by rw [← hIcc, eL.symm_preimage_preimage] calc ∫ x in Icc a b, DF x = ∫ x in Icc a b, ∑ i, f' i x (eL.symm <| e i) := by simp only [hDF] _ = ∫ x in Icc (eL a) (eL b), ∑ i, f' i (eL.symm x) (eL.symm <| e i) := by rw [← he_vol.setIntegral_preimage_emb he_emb] simp only [hIcc, eL.symm_apply_apply] _ = ∑ i : Fin (n + 1), ((∫ x in Icc (eL a ∘ i.succAbove) (eL b ∘ i.succAbove), f i (eL.symm <| i.insertNth (eL b i) x)) - ∫ x in Icc (eL a ∘ i.succAbove) (eL b ∘ i.succAbove), f i (eL.symm <| i.insertNth (eL a i) x)) := by refine integral_divergence_of_hasFDerivWithinAt_off_countable' (eL a) (eL b) ((he_ord _ _).2 hle) (fun i x => f i (eL.symm x)) (fun i x => f' i (eL.symm x) ∘L (eL.symm : ℝⁿ⁺¹ →L[ℝ] F)) (eL.symm ⁻¹' s) (hs.preimage eL.symm.injective) ?_ ?_ ?_ · exact fun i => (Hc i).comp eL.symm.continuousOn hIcc'.subset · refine fun x hx i => (Hd (eL.symm x) ⟨?_, hx.2⟩ i).comp x eL.symm.hasFDerivAt rw [← hIcc] refine preimage_interior_subset_interior_preimage eL.continuous ?_ simpa only [Set.mem_preimage, eL.apply_symm_apply, ← pi_univ_Icc, interior_pi_set (@finite_univ (Fin _) _), interior_Icc] using hx.1 · rw [← he_vol.integrableOn_comp_preimage he_emb, hIcc] simp [← hDF, Function.comp_def, Hi] end open scoped Interval open ContinuousLinearMap (smulRight) local macro:arg t:term:max noWs "¹" : term => `(Fin 1 → $t) local macro:arg t:term:max noWs "²" : term => `(Fin 2 → $t) /-- **Fundamental theorem of calculus, part 2**. This version assumes that `f` is continuous on the interval and is differentiable off a countable set `s`. See also * `intervalIntegral.integral_eq_sub_of_hasDeriv_right_of_le` for a version that only assumes right differentiability of `f`; * `MeasureTheory.integral_eq_of_hasDerivWithinAt_off_countable` for a version that works both for `a ≤ b` and `b ≤ a` at the expense of using unordered intervals instead of `Set.Icc`. -/ theorem integral_eq_of_hasDerivWithinAt_off_countable_of_le [CompleteSpace E] (f f' : ℝ → E) {a b : ℝ} (hle : a ≤ b) {s : Set ℝ} (hs : s.Countable) (Hc : ContinuousOn f (Icc a b)) (Hd : ∀ x ∈ Ioo a b \ s, HasDerivAt f (f' x) x) (Hi : IntervalIntegrable f' volume a b) : ∫ x in a..b, f' x = f b - f a := by set e : ℝ ≃L[ℝ] ℝ¹ := (ContinuousLinearEquiv.funUnique (Fin 1) ℝ ℝ).symm have e_symm : ∀ x, e.symm x = x 0 := fun x => rfl set F' : ℝ → ℝ →L[ℝ] E := fun x => smulRight (1 : ℝ →L[ℝ] ℝ) (f' x) have hF' : ∀ x y, F' x y = y • f' x := fun x y => rfl calc ∫ x in a..b, f' x = ∫ x in Icc a b, f' x := by rw [intervalIntegral.integral_of_le hle, setIntegral_congr_set Ioc_ae_eq_Icc] _ = ∑ i : Fin 1, ((∫ x in Icc (e a ∘ i.succAbove) (e b ∘ i.succAbove), f (e.symm <| i.insertNth (e b i) x)) - ∫ x in Icc (e a ∘ i.succAbove) (e b ∘ i.succAbove), f (e.symm <| i.insertNth (e a i) x)) := by simp only [← interior_Icc] at Hd refine integral_divergence_of_hasFDerivWithinAt_off_countable_of_equiv e ?_ ?_ (fun _ => f) (fun _ => F') s hs a b hle (fun _ => Hc) (fun x hx _ => Hd x hx) _ ?_ ?_ · exact fun x y => (OrderIso.funUnique (Fin 1) ℝ).symm.le_iff_le · exact (volume_preserving_funUnique (Fin 1) ℝ).symm _ · intro x; rw [Fin.sum_univ_one, hF', e_symm, Pi.single_eq_same, one_smul] · rw [intervalIntegrable_iff_integrableOn_Ioc_of_le hle] at Hi exact Hi.congr_set_ae Ioc_ae_eq_Icc.symm _ = f b - f a := by simp only [e, Fin.sum_univ_one, e_symm] have : ∀ c : ℝ, const (Fin 0) c = isEmptyElim := fun c => Subsingleton.elim _ _ simp [this, volume_pi, Measure.pi_of_empty fun _ : Fin 0 => volume] /-- **Fundamental theorem of calculus, part 2**. This version assumes that `f` is continuous on the interval and is differentiable off a countable set `s`. See also `intervalIntegral.integral_eq_sub_of_hasDeriv_right` for a version that only assumes right differentiability of `f`. -/ theorem integral_eq_of_hasDerivWithinAt_off_countable [CompleteSpace E] (f f' : ℝ → E) {a b : ℝ} {s : Set ℝ} (hs : s.Countable) (Hc : ContinuousOn f [[a, b]]) (Hd : ∀ x ∈ Ioo (min a b) (max a b) \ s, HasDerivAt f (f' x) x) (Hi : IntervalIntegrable f' volume a b) : ∫ x in a..b, f' x = f b - f a := by rcases le_total a b with hab | hab · simp only [uIcc_of_le hab, min_eq_left hab, max_eq_right hab] at * exact integral_eq_of_hasDerivWithinAt_off_countable_of_le f f' hab hs Hc Hd Hi · simp only [uIcc_of_ge hab, min_eq_right hab, max_eq_left hab] at * rw [intervalIntegral.integral_symm, neg_eq_iff_eq_neg, neg_sub] exact integral_eq_of_hasDerivWithinAt_off_countable_of_le f f' hab hs Hc Hd Hi.symm /-- **Divergence theorem** for functions on the plane along rectangles. It is formulated in terms of two functions `f g : ℝ × ℝ → E` and an integral over `Icc a b = [a.1, b.1] × [a.2, b.2]`, where `a b : ℝ × ℝ`, `a ≤ b`. When thinking of `f` and `g` as the two coordinates of a single function `F : ℝ × ℝ → E × E` and when `E = ℝ`, this is the usual statement that the integral of the divergence of `F` inside the rectangle equals the integral of the normal derivative of `F` along the boundary. See also `MeasureTheory.integral2_divergence_prod_of_hasFDerivWithinAt_off_countable` for a version that does not assume `a ≤ b` and uses iterated interval integral instead of the integral over `Icc a b`. -/ theorem integral_divergence_prod_Icc_of_hasFDerivWithinAt_off_countable_of_le (f g : ℝ × ℝ → E) (f' g' : ℝ × ℝ → ℝ × ℝ →L[ℝ] E) (a b : ℝ × ℝ) (hle : a ≤ b) (s : Set (ℝ × ℝ)) (hs : s.Countable) (Hcf : ContinuousOn f (Icc a b)) (Hcg : ContinuousOn g (Icc a b)) (Hdf : ∀ x ∈ Ioo a.1 b.1 ×ˢ Ioo a.2 b.2 \ s, HasFDerivAt f (f' x) x) (Hdg : ∀ x ∈ Ioo a.1 b.1 ×ˢ Ioo a.2 b.2 \ s, HasFDerivAt g (g' x) x) (Hi : IntegrableOn (fun x => f' x (1, 0) + g' x (0, 1)) (Icc a b)) : (∫ x in Icc a b, f' x (1, 0) + g' x (0, 1)) =
(((∫ x in a.1..b.1, g (x, b.2)) - ∫ x in a.1..b.1, g (x, a.2)) + ∫ y in a.2..b.2, f (b.1, y)) - ∫ y in a.2..b.2, f (a.1, y) := let e : (ℝ × ℝ) ≃L[ℝ] ℝ² := (ContinuousLinearEquiv.finTwoArrow ℝ ℝ).symm calc (∫ x in Icc a b, f' x (1, 0) + g' x (0, 1)) = ∑ i : Fin 2, ((∫ x in Icc (e a ∘ i.succAbove) (e b ∘ i.succAbove), ![f, g] i (e.symm <| i.insertNth (e b i) x)) - ∫ x in Icc (e a ∘ i.succAbove) (e b ∘ i.succAbove), ![f, g] i (e.symm <| i.insertNth (e a i) x)) := by refine integral_divergence_of_hasFDerivWithinAt_off_countable_of_equiv e ?_ ?_ ![f, g] ![f', g'] s hs a b hle ?_ (fun x hx => ?_) _ ?_ Hi · exact fun x y => (OrderIso.finTwoArrowIso ℝ).symm.le_iff_le · exact (volume_preserving_finTwoArrow ℝ).symm _ · exact Fin.forall_fin_two.2 ⟨Hcf, Hcg⟩ · rw [Icc_prod_eq, interior_prod_eq, interior_Icc, interior_Icc] at hx exact Fin.forall_fin_two.2 ⟨Hdf x hx, Hdg x hx⟩ · intro x; rw [Fin.sum_univ_two]; rfl _ = ((∫ y in Icc a.2 b.2, f (b.1, y)) - ∫ y in Icc a.2 b.2, f (a.1, y)) + ((∫ x in Icc a.1 b.1, g (x, b.2)) - ∫ x in Icc a.1 b.1, g (x, a.2)) := by have : ∀ (a b : ℝ¹) (f : ℝ¹ → E), ∫ x in Icc a b, f x = ∫ x in Icc (a 0) (b 0), f fun _ => x := fun a b f ↦ by convert (((volume_preserving_funUnique (Fin 1) ℝ).symm _).setIntegral_preimage_emb (MeasurableEquiv.measurableEmbedding _) f _).symm exact ((OrderIso.funUnique (Fin 1) ℝ).symm.preimage_Icc a b).symm simp only [Fin.sum_univ_two, this] rfl _ = (((∫ x in a.1..b.1, g (x, b.2)) - ∫ x in a.1..b.1, g (x, a.2)) + ∫ y in a.2..b.2, f (b.1, y)) - ∫ y in a.2..b.2, f (a.1, y) := by simp only [intervalIntegral.integral_of_le hle.1, intervalIntegral.integral_of_le hle.2, setIntegral_congr_set (Ioc_ae_eq_Icc (α := ℝ) (μ := volume))] abel /-- **Divergence theorem** for functions on the plane. It is formulated in terms of two functions `f g : ℝ × ℝ → E` and iterated integral `∫ x in a₁..b₁, ∫ y in a₂..b₂, _`, where `a₁ a₂ b₁ b₂ : ℝ`. When thinking of `f` and `g` as the two coordinates of a single function `F : ℝ × ℝ → E × E` and when `E = ℝ`, this is the usual statement that the integral of the divergence of `F` inside the rectangle with vertices `(aᵢ, bⱼ)`, `i, j =1,2`, equals the integral of the normal derivative of `F` along the boundary.
Mathlib/MeasureTheory/Integral/DivergenceTheorem.lean
441
480
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Heather Macbeth -/ import Mathlib.Analysis.InnerProductSpace.TwoDim import Mathlib.Geometry.Euclidean.Angle.Unoriented.Basic /-! # Oriented angles. This file defines oriented angles in real inner product spaces. ## Main definitions * `Orientation.oangle` is the oriented angle between two vectors with respect to an orientation. ## Implementation notes The definitions here use the `Real.angle` type, angles modulo `2 * π`. For some purposes, angles modulo `π` are more convenient, because results are true for such angles with less configuration dependence. Results that are only equalities modulo `π` can be represented modulo `2 * π` as equalities of `(2 : ℤ) • θ`. ## References * Evan Chen, Euclidean Geometry in Mathematical Olympiads. -/ noncomputable section open Module Complex open scoped Real RealInnerProductSpace ComplexConjugate namespace Orientation attribute [local instance] Complex.finrank_real_complex_fact variable {V V' : Type*} variable [NormedAddCommGroup V] [NormedAddCommGroup V'] variable [InnerProductSpace ℝ V] [InnerProductSpace ℝ V'] variable [Fact (finrank ℝ V = 2)] [Fact (finrank ℝ V' = 2)] (o : Orientation ℝ V (Fin 2)) local notation "ω" => o.areaForm /-- The oriented angle from `x` to `y`, modulo `2 * π`. If either vector is 0, this is 0. See `InnerProductGeometry.angle` for the corresponding unoriented angle definition. -/ def oangle (x y : V) : Real.Angle := Complex.arg (o.kahler x y) /-- Oriented angles are continuous when the vectors involved are nonzero. -/ @[fun_prop] theorem continuousAt_oangle {x : V × V} (hx1 : x.1 ≠ 0) (hx2 : x.2 ≠ 0) : ContinuousAt (fun y : V × V => o.oangle y.1 y.2) x := by refine (Complex.continuousAt_arg_coe_angle ?_).comp ?_ · exact o.kahler_ne_zero hx1 hx2 exact ((continuous_ofReal.comp continuous_inner).add ((continuous_ofReal.comp o.areaForm'.continuous₂).mul continuous_const)).continuousAt /-- If the first vector passed to `oangle` is 0, the result is 0. -/ @[simp] theorem oangle_zero_left (x : V) : o.oangle 0 x = 0 := by simp [oangle] /-- If the second vector passed to `oangle` is 0, the result is 0. -/ @[simp] theorem oangle_zero_right (x : V) : o.oangle x 0 = 0 := by simp [oangle] /-- If the two vectors passed to `oangle` are the same, the result is 0. -/ @[simp] theorem oangle_self (x : V) : o.oangle x x = 0 := by rw [oangle, kahler_apply_self, ← ofReal_pow] convert QuotientAddGroup.mk_zero (AddSubgroup.zmultiples (2 * π)) apply arg_ofReal_of_nonneg positivity /-- If the angle between two vectors is nonzero, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : x ≠ 0 := by rintro rfl; simp at h /-- If the angle between two vectors is nonzero, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : y ≠ 0 := by rintro rfl; simp at h /-- If the angle between two vectors is nonzero, the vectors are not equal. -/ theorem ne_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : x ≠ y := by rintro rfl; simp at h /-- If the angle between two vectors is `π`, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π`, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π`, the vectors are not equal. -/ theorem ne_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the vectors are not equal. -/ theorem ne_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ Real.Angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ Real.Angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the vectors are not equal. -/ theorem ne_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ Real.Angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the sign of the angle between two vectors is nonzero, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (Real.Angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is nonzero, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (Real.Angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is nonzero, the vectors are not equal. -/ theorem ne_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : x ≠ y := o.ne_of_oangle_ne_zero (Real.Angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is positive, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : x ≠ 0 := o.left_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is positive, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : y ≠ 0 := o.right_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is positive, the vectors are not equal. -/ theorem ne_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : x ≠ y := o.ne_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the first vector is nonzero. -/ theorem left_ne_zero_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : x ≠ 0 := o.left_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the second vector is nonzero. -/ theorem right_ne_zero_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : y ≠ 0 := o.right_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the vectors are not equal. -/ theorem ne_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : x ≠ y :=
o.ne_of_oangle_sign_ne_zero (h.symm ▸ by decide : (o.oangle x y).sign ≠ 0)
Mathlib/Geometry/Euclidean/Angle/Oriented/Basic.lean
163
164
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import Mathlib.MeasureTheory.Integral.Lebesgue.Basic import Mathlib.MeasureTheory.Integral.Lebesgue.Countable import Mathlib.MeasureTheory.Integral.Lebesgue.MeasurePreserving import Mathlib.MeasureTheory.Integral.Lebesgue.Norm deprecated_module (since := "2025-04-13")
Mathlib/MeasureTheory/Integral/Lebesgue.lean
175
176
/- 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,386
1,391
/- Copyright (c) 2023 Yaël Dillies, Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Christopher Hoskin -/ import Mathlib.Data.Finset.Lattice.Prod import Mathlib.Data.Finset.Powerset import Mathlib.Data.Set.Finite.Basic import Mathlib.Order.Closure import Mathlib.Order.ConditionallyCompleteLattice.Finset /-! # Sets closed under join/meet This file defines predicates for sets closed under `⊔` and shows that each set in a join-semilattice generates a join-closed set and that a semilattice where every directed set has a least upper bound is automatically complete. All dually for `⊓`. ## Main declarations * `SupClosed`: Predicate for a set to be closed under join (`a ∈ s` and `b ∈ s` imply `a ⊔ b ∈ s`). * `InfClosed`: Predicate for a set to be closed under meet (`a ∈ s` and `b ∈ s` imply `a ⊓ b ∈ s`). * `IsSublattice`: Predicate for a set to be closed under meet and join. * `supClosure`: Sup-closure. Smallest sup-closed set containing a given set. * `infClosure`: Inf-closure. Smallest inf-closed set containing a given set. * `latticeClosure`: Smallest sublattice containing a given set. * `SemilatticeSup.toCompleteSemilatticeSup`: A join-semilattice where every sup-closed set has a least upper bound is automatically complete. * `SemilatticeInf.toCompleteSemilatticeInf`: A meet-semilattice where every inf-closed set has a greatest lower bound is automatically complete. -/ variable {ι : Sort*} {F α β : Type*} section SemilatticeSup variable [SemilatticeSup α] [SemilatticeSup β] section Set variable {ι : Sort*} {S : Set (Set α)} {f : ι → Set α} {s t : Set α} {a : α} open Set /-- A set `s` is *sup-closed* if `a ⊔ b ∈ s` for all `a ∈ s`, `b ∈ s`. -/ def SupClosed (s : Set α) : Prop := ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → a ⊔ b ∈ s @[simp] lemma supClosed_empty : SupClosed (∅ : Set α) := by simp [SupClosed] @[simp] lemma supClosed_singleton : SupClosed ({a} : Set α) := by simp [SupClosed] @[simp] lemma supClosed_univ : SupClosed (univ : Set α) := by simp [SupClosed] lemma SupClosed.inter (hs : SupClosed s) (ht : SupClosed t) : SupClosed (s ∩ t) := fun _a ha _b hb ↦ ⟨hs ha.1 hb.1, ht ha.2 hb.2⟩ lemma supClosed_sInter (hS : ∀ s ∈ S, SupClosed s) : SupClosed (⋂₀ S) := fun _a ha _b hb _s hs ↦ hS _ hs (ha _ hs) (hb _ hs) lemma supClosed_iInter (hf : ∀ i, SupClosed (f i)) : SupClosed (⋂ i, f i) := supClosed_sInter <| forall_mem_range.2 hf lemma SupClosed.directedOn (hs : SupClosed s) : DirectedOn (· ≤ ·) s := fun _a ha _b hb ↦ ⟨_, hs ha hb, le_sup_left, le_sup_right⟩ lemma IsUpperSet.supClosed (hs : IsUpperSet s) : SupClosed s := fun _a _ _b ↦ hs le_sup_right lemma SupClosed.preimage [FunLike F β α] [SupHomClass F β α] (hs : SupClosed s) (f : F) : SupClosed (f ⁻¹' s) := fun a ha b hb ↦ by simpa [map_sup] using hs ha hb lemma SupClosed.image [FunLike F α β] [SupHomClass F α β] (hs : SupClosed s) (f : F) : SupClosed (f '' s) := by rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ rw [← map_sup] exact Set.mem_image_of_mem _ <| hs ha hb lemma supClosed_range [FunLike F α β] [SupHomClass F α β] (f : F) : SupClosed (Set.range f) := by simpa using supClosed_univ.image f lemma SupClosed.prod {t : Set β} (hs : SupClosed s) (ht : SupClosed t) : SupClosed (s ×ˢ t) := fun _a ha _b hb ↦ ⟨hs ha.1 hb.1, ht ha.2 hb.2⟩ lemma supClosed_pi {ι : Type*} {α : ι → Type*} [∀ i, SemilatticeSup (α i)] {s : Set ι} {t : ∀ i, Set (α i)} (ht : ∀ i ∈ s, SupClosed (t i)) : SupClosed (s.pi t) := fun _a ha _b hb _i hi ↦ ht _ hi (ha _ hi) (hb _ hi) lemma SupClosed.insert_upperBounds {s : Set α} {a : α} (hs : SupClosed s) (ha : a ∈ upperBounds s) : SupClosed (insert a s) := by rw [SupClosed] aesop lemma SupClosed.insert_lowerBounds {s : Set α} {a : α} (h : SupClosed s) (ha : a ∈ lowerBounds s) : SupClosed (insert a s) := by rw [SupClosed] have ha' : ∀ b ∈ s, a ≤ b := fun _ a ↦ ha a aesop end Set section Finset variable {ι : Type*} {f : ι → α} {s : Set α} {t : Finset ι} {a : α} open Finset lemma SupClosed.finsetSup'_mem (hs : SupClosed s) (ht : t.Nonempty) : (∀ i ∈ t, f i ∈ s) → t.sup' ht f ∈ s := sup'_induction _ _ hs lemma SupClosed.finsetSup_mem [OrderBot α] (hs : SupClosed s) (ht : t.Nonempty) : (∀ i ∈ t, f i ∈ s) → t.sup f ∈ s := sup'_eq_sup ht f ▸ hs.finsetSup'_mem ht end Finset end SemilatticeSup section SemilatticeInf variable [SemilatticeInf α] [SemilatticeInf β] section Set variable {ι : Sort*} {S : Set (Set α)} {f : ι → Set α} {s t : Set α} {a : α} open Set /-- A set `s` is *inf-closed* if `a ⊓ b ∈ s` for all `a ∈ s`, `b ∈ s`. -/ def InfClosed (s : Set α) : Prop := ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → a ⊓ b ∈ s @[simp] lemma infClosed_empty : InfClosed (∅ : Set α) := by simp [InfClosed] @[simp] lemma infClosed_singleton : InfClosed ({a} : Set α) := by simp [InfClosed] @[simp] lemma infClosed_univ : InfClosed (univ : Set α) := by simp [InfClosed] lemma InfClosed.inter (hs : InfClosed s) (ht : InfClosed t) : InfClosed (s ∩ t) := fun _a ha _b hb ↦ ⟨hs ha.1 hb.1, ht ha.2 hb.2⟩ lemma infClosed_sInter (hS : ∀ s ∈ S, InfClosed s) : InfClosed (⋂₀ S) := fun _a ha _b hb _s hs ↦ hS _ hs (ha _ hs) (hb _ hs) lemma infClosed_iInter (hf : ∀ i, InfClosed (f i)) : InfClosed (⋂ i, f i) :=
infClosed_sInter <| forall_mem_range.2 hf lemma InfClosed.codirectedOn (hs : InfClosed s) : DirectedOn (· ≥ ·) s := fun _a ha _b hb ↦ ⟨_, hs ha hb, inf_le_left, inf_le_right⟩
Mathlib/Order/SupClosed.lean
132
136
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.Multiset.FinsetOps import Mathlib.Data.Multiset.Fold /-! # Lattice operations on multisets -/ namespace Multiset variable {α : Type*} /-! ### sup -/ section Sup -- can be defined with just `[Bot α]` where some lemmas hold without requiring `[OrderBot α]` variable [SemilatticeSup α] [OrderBot α] /-- Supremum of a multiset: `sup {a, b, c} = a ⊔ b ⊔ c` -/ def sup (s : Multiset α) : α := s.fold (· ⊔ ·) ⊥ @[simp] theorem sup_coe (l : List α) : sup (l : Multiset α) = l.foldr (· ⊔ ·) ⊥ := rfl @[simp] theorem sup_zero : (0 : Multiset α).sup = ⊥ := fold_zero _ _ @[simp] theorem sup_cons (a : α) (s : Multiset α) : (a ::ₘ s).sup = a ⊔ s.sup := fold_cons_left _ _ _ _ @[simp] theorem sup_singleton {a : α} : ({a} : Multiset α).sup = a := sup_bot_eq _ @[simp] theorem sup_add (s₁ s₂ : Multiset α) : (s₁ + s₂).sup = s₁.sup ⊔ s₂.sup := Eq.trans (by simp [sup]) (fold_add _ _ _ _ _) @[simp] theorem sup_le {s : Multiset α} {a : α} : s.sup ≤ a ↔ ∀ b ∈ s, b ≤ a := Multiset.induction_on s (by simp) (by simp +contextual [or_imp, forall_and])
theorem le_sup {s : Multiset α} {a : α} (h : a ∈ s) : a ≤ s.sup :=
Mathlib/Data/Multiset/Lattice.lean
53
54