blob_id
stringlengths
40
40
directory_id
stringlengths
40
40
path
stringlengths
7
139
content_id
stringlengths
40
40
detected_licenses
listlengths
0
16
license_type
stringclasses
2 values
repo_name
stringlengths
7
55
snapshot_id
stringlengths
40
40
revision_id
stringlengths
40
40
branch_name
stringclasses
6 values
visit_date
int64
1,471B
1,694B
revision_date
int64
1,378B
1,694B
committer_date
int64
1,378B
1,694B
github_id
float64
1.33M
604M
star_events_count
int64
0
43.5k
fork_events_count
int64
0
1.5k
gha_license_id
stringclasses
6 values
gha_event_created_at
int64
1,402B
1,695B
gha_created_at
int64
1,359B
1,637B
gha_language
stringclasses
19 values
src_encoding
stringclasses
2 values
language
stringclasses
1 value
is_vendor
bool
1 class
is_generated
bool
1 class
length_bytes
int64
3
6.4M
extension
stringclasses
4 values
content
stringlengths
3
6.12M
8308a769a01c36869936163e1757c82e65049add
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/fintype/basic.lean
8c795d36e5b576d916da4286abf12f810030bec9
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
9,152
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Finite type (type class). -/ import data.list.perm data.list.as_type data.bool data.equiv open list bool unit decidable option function structure fintype [class] (A : Type) : Type := (elems : list A) (unique : nodup elems) (complete : ∀ a, a ∈ elems) definition elements_of (A : Type) [h : fintype A] : list A := @fintype.elems A h section open equiv definition fintype_of_equiv {A B : Type} [h : fintype A] : A ≃ B → fintype B | (mk f g l r) := fintype.mk (map f (elements_of A)) (nodup_map (injective_of_left_inverse l) !fintype.unique) (λ b, have g b ∈ elements_of A, from fintype.complete (g b), assert f (g b) ∈ map f (elements_of A), from mem_map f this, by rewrite r at this; exact this) end definition fintype_unit [instance] : fintype unit := fintype.mk [star] dec_trivial (λ u, match u with star := dec_trivial end) definition fintype_bool [instance] : fintype bool := fintype.mk [ff, tt] dec_trivial (λ b, match b with | tt := dec_trivial | ff := dec_trivial end) definition fintype_product [instance] {A B : Type} : Π [h₁ : fintype A] [h₂ : fintype B], fintype (A × B) | (fintype.mk e₁ u₁ c₁) (fintype.mk e₂ u₂ c₂) := fintype.mk (product e₁ e₂) (nodup_product u₁ u₂) (λ p, match p with (a, b) := mem_product (c₁ a) (c₂ b) end) /- auxiliary function for finding 'a' s.t. f a ≠ g a -/ section find_discr variables {A B : Type} variable [h : decidable_eq B] include h definition find_discr (f g : A → B) : list A → option A | [] := none | (a::l) := if f a = g a then find_discr l else some a theorem find_discr_nil (f g : A → B) : find_discr f g [] = none := rfl theorem find_discr_cons_of_ne {f g : A → B} {a : A} (l : list A) : f a ≠ g a → find_discr f g (a::l) = some a := assume ne, if_neg ne theorem find_discr_cons_of_eq {f g : A → B} {a : A} (l : list A) : f a = g a → find_discr f g (a::l) = find_discr f g l := assume eq, if_pos eq theorem ne_of_find_discr_eq_some {f g : A → B} {a : A} : ∀ {l}, find_discr f g l = some a → f a ≠ g a | [] e := by contradiction | (x::l) e := by_cases (suppose f x = g x, have find_discr f g l = some a, by rewrite [find_discr_cons_of_eq l this at e]; exact e, ne_of_find_discr_eq_some this) (assume h : f x ≠ g x, assert some x = some a, by rewrite [find_discr_cons_of_ne l h at e]; exact e, by clear ne_of_find_discr_eq_some; injection this; subst a; exact h) theorem all_eq_of_find_discr_eq_none {f g : A → B} : ∀ {l}, find_discr f g l = none → ∀ a, a ∈ l → f a = g a | [] e a i := absurd i !not_mem_nil | (x::l) e a i := by_cases (assume fx_eq_gx : f x = g x, or.elim (eq_or_mem_of_mem_cons i) (suppose a = x, by rewrite [-this at fx_eq_gx]; exact fx_eq_gx) (suppose a ∈ l, have aux : find_discr f g l = none, by rewrite [find_discr_cons_of_eq l fx_eq_gx at e]; exact e, all_eq_of_find_discr_eq_none aux a this)) (suppose f x ≠ g x, by rewrite [find_discr_cons_of_ne l this at e]; contradiction) end find_discr definition decidable_eq_fun [instance] {A B : Type} [h₁ : fintype A] [h₂ : decidable_eq B] : decidable_eq (A → B) := λ f g, match h₁ with | fintype.mk e u c := match find_discr f g e with | some a := λ h : find_discr f g e = some a, inr (λ f_eq_g : f = g, absurd (by rewrite f_eq_g; reflexivity) (ne_of_find_discr_eq_some h)) | none := λ h : find_discr f g e = none, inl (show f = g, from funext (λ a : A, all_eq_of_find_discr_eq_none h a (c a))) end rfl end section check_pred variables {A : Type} definition check_pred (p : A → Prop) [h : decidable_pred p] : list A → bool | [] := tt | (a::l) := if p a then check_pred l else ff theorem check_pred_cons_of_pos {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : p a → check_pred p (a::l) = check_pred p l := assume pa, if_pos pa theorem check_pred_cons_of_neg {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : ¬ p a → check_pred p (a::l) = ff := assume npa, if_neg npa theorem all_of_check_pred_eq_tt {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = tt → ∀ {a}, a ∈ l → p a | [] eqtt a ainl := absurd ainl !not_mem_nil | (b::l) eqtt a ainbl := by_cases (suppose p b, or.elim (eq_or_mem_of_mem_cons ainbl) (suppose a = b, by rewrite [this]; exact `p b`) (suppose a ∈ l, have check_pred p l = tt, by rewrite [check_pred_cons_of_pos _ `p b` at eqtt]; exact eqtt, all_of_check_pred_eq_tt this `a ∈ l`)) (suppose ¬ p b, by rewrite [check_pred_cons_of_neg _ this at eqtt]; exact (bool.no_confusion eqtt)) theorem ex_of_check_pred_eq_ff {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = ff → ∃ w, ¬ p w | [] eqtt := bool.no_confusion eqtt | (a::l) eqtt := by_cases (suppose p a, have check_pred p l = ff, by rewrite [check_pred_cons_of_pos _ this at eqtt]; exact eqtt, ex_of_check_pred_eq_ff this) (suppose ¬ p a, exists.intro a this) end check_pred definition decidable_forall_finite [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p] : decidable (∀ x : A, p x) := match h₁ with | fintype.mk e u c := match check_pred p e with | tt := suppose check_pred p e = tt, inl (take a : A, all_of_check_pred_eq_tt this (c a)) | ff := suppose check_pred p e = ff, inr (suppose ∀ x, p x, obtain (a : A) (w : ¬ p a), from ex_of_check_pred_eq_ff `check_pred p e = ff`, absurd (this a) w) end rfl end definition decidable_exists_finite [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p] : decidable (∃ x : A, p x) := match h₁ with | fintype.mk e u c := match check_pred (λ a, ¬ p a) e with | tt := λ h : check_pred (λ a, ¬ p a) e = tt, inr (λ ex : (∃ x, p x), obtain x px, from ex, absurd px (all_of_check_pred_eq_tt h (c x))) | ff := λ h : check_pred (λ a, ¬ p a) e = ff, inl ( assert ∃ x, ¬¬p x, from ex_of_check_pred_eq_ff h, obtain x nnpx, from this, exists.intro x (not_not_elim nnpx)) end rfl end open list.as_type -- Auxiliary function for returning a list with all elements of the type: (list.as_type l) -- Remark ⟪s⟫ is notation for (list.as_type l) -- We use this function to define the instance for (fintype ⟪s⟫) private definition ltype_elems {A : Type} {s : list A} : Π {l : list A}, l ⊆ s → list ⟪s⟫ | [] h := [] | (a::l) h := lval a (h a !mem_cons) :: ltype_elems (sub_of_cons_sub h) private theorem mem_of_mem_ltype_elems {A : Type} {a : A} {s : list A} : Π {l : list A} {h : l ⊆ s} {m : a ∈ s}, mk a m ∈ ltype_elems h → a ∈ l | [] h m lin := absurd lin !not_mem_nil | (b::l) h m lin := or.elim (eq_or_mem_of_mem_cons lin) (suppose mk a m = mk b (h b (mem_cons b l)), as_type.no_confusion this (λ aeqb em, by rewrite [aeqb]; exact !mem_cons)) (suppose mk a m ∈ ltype_elems (sub_of_cons_sub h), have a ∈ l, from mem_of_mem_ltype_elems this, mem_cons_of_mem _ this) private theorem nodup_ltype_elems {A : Type} {s : list A} : Π {l : list A} (d : nodup l) (h : l ⊆ s), nodup (ltype_elems h) | [] d h := nodup_nil | (a::l) d h := have d₁ : nodup l, from nodup_of_nodup_cons d, have nainl : a ∉ l, from not_mem_of_nodup_cons d, let h₁ : l ⊆ s := sub_of_cons_sub h in have d₂ : nodup (ltype_elems h₁), from nodup_ltype_elems d₁ h₁, have nin : mk a (h a (mem_cons a l)) ∉ ltype_elems h₁, from assume ab, absurd (mem_of_mem_ltype_elems ab) nainl, nodup_cons nin d₂ private theorem mem_ltype_elems {A : Type} {s : list A} {a : ⟪s⟫} : Π {l : list A} (h : l ⊆ s), value a ∈ l → a ∈ ltype_elems h | [] h vainl := absurd vainl !not_mem_nil | (b::l) h vainbl := or.elim (eq_or_mem_of_mem_cons vainbl) (λ vaeqb : value a = b, begin revert vaeqb h, -- TODO(Leo): check why 'cases a with va, ma' produces an incorrect proof eapply as_type.cases_on a, intro va ma vaeqb, rewrite -vaeqb, intro h, apply mem_cons end) (λ vainl : value a ∈ l, have aux : a ∈ ltype_elems (sub_of_cons_sub h), from mem_ltype_elems (sub_of_cons_sub h) vainl, mem_cons_of_mem _ aux) definition fintype_list_as_type [instance] {A : Type} [h : decidable_eq A] {s : list A} : fintype ⟪s⟫ := let nds : list A := erase_dup s in have sub₁ : nds ⊆ s, from erase_dup_sub s, have sub₂ : s ⊆ nds, from sub_erase_dup s, have dnds : nodup nds, from nodup_erase_dup s, let e : list ⟪s⟫ := ltype_elems sub₁ in fintype.mk e (nodup_ltype_elems dnds sub₁) (take a : ⟪s⟫, show a ∈ e, from have value a ∈ s, from is_member a, have value a ∈ nds, from sub₂ this, mem_ltype_elems sub₁ this)
bd9def9edcaaab9d7075b5f6894c1c113741a171
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/integral/vitali_caratheodory.lean
fd375ddc1f4c539b37bee258a13148463a6d74ee
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
29,834
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.measure.regular import topology.semicontinuous import measure_theory.integral.bochner import topology.instances.ereal /-! # Vitali-Carathéodory theorem Vitali-Carathéodory theorem asserts the following. Consider an integrable function `f : α → ℝ` on a space with a regular measure. Then there exists a function `g : α → ereal` such that `f x < g x` everywhere, `g` is lower semicontinuous, and the integral of `g` is arbitrarily close to that of `f`. This theorem is proved in this file, as `exists_lt_lower_semicontinuous_integral_lt`. Symmetrically, there exists `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. It follows from the previous statement applied to `-f`. It is formalized under the name `exists_upper_semicontinuous_lt_integral_gt`. The most classical version of Vitali-Carathéodory theorem only ensures a large inequality `f x ≤ g x`. For applications to the fundamental theorem of calculus, though, the strict inequality `f x < g x` is important. Therefore, we prove the stronger version with strict inequalities in this file. There is a price to pay: we require that the measure is `σ`-finite, which is not necessary for the classical Vitali-Carathéodory theorem. Since this is satisfied in all applications, this is not a real problem. ## Sketch of proof Decomposing `f` as the difference of its positive and negative parts, it suffices to show that a positive function can be bounded from above by a lower semicontinuous function, and from below by an upper semicontinuous function, with integrals close to that of `f`. For the bound from above, write `f` as a series `∑' n, cₙ * indicator (sₙ)` of simple functions. Then, approximate `sₙ` by a larger open set `uₙ` with measure very close to that of `sₙ` (this is possible by regularity of the measure), and set `g = ∑' n, cₙ * indicator (uₙ)`. It is lower semicontinuous as a series of lower semicontinuous functions, and its integral is arbitrarily close to that of `f`. For the bound from below, use finitely many terms in the series, and approximate `sₙ` from inside by a closed set `Fₙ`. Then `∑ n < N, cₙ * indicator (Fₙ)` is bounded from above by `f`, it is upper semicontinuous as a finite sum of upper semicontinuous functions, and its integral is arbitrarily close to that of `f`. The main pain point in the implementation is that one needs to jump between the spaces `ℝ`, `ℝ≥0`, `ℝ≥0∞` and `ereal` (and be careful that addition is not well behaved on `ereal`), and between `lintegral` and `integral`. We first show the bound from above for simple functions and the nonnegative integral (this is the main nontrivial mathematical point), then deduce it for general nonnegative functions, first for the nonnegative integral and then for the Bochner integral. Then we follow the same steps for the lower bound. Finally, we glue them together to obtain the main statement `exists_lt_lower_semicontinuous_integral_lt`. ## Related results Are you looking for a result on approximation by continuous functions (not just semicontinuous)? See result `measure_theory.Lp.continuous_map_dense`, in the file `measure_theory.continuous_map_dense`. ## References [Rudin, *Real and Complex Analysis* (Theorem 2.24)][rudin2006real] -/ open_locale ennreal nnreal open measure_theory measure_theory.measure variables {α : Type*} [topological_space α] [measurable_space α] [borel_space α] (μ : measure α) [weakly_regular μ] namespace measure_theory local infixr ` →ₛ `:25 := simple_func /-! ### Lower semicontinuous upper bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_le_lower_semicontinuous_lintegral_ge (f : α →ₛ ℝ≥0) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin induction f using measure_theory.simple_func.induction with c s hs f₁ f₂ H h₁ h₂ generalizing ε, { let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases h : ∫⁻ x, f x ∂μ = ⊤, { refine ⟨λ x, c, λ x, _, lower_semicontinuous_const, by simp only [ennreal.top_add, le_top, h]⟩, simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_self _ _ _ }, by_cases hc : c = 0, { refine ⟨λ x, 0, _, lower_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [lintegral_const, zero_mul, zero_le, ennreal.coe_zero] } }, have : μ s < μ s + ε / c, { have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨ε0, ennreal.coe_ne_top⟩, simpa using ennreal.add_lt_add_left _ this, simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, or_false, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and, restrict_apply] using h }, obtain ⟨u, su, u_open, μu⟩ : ∃ u ⊇ s, is_open u ∧ μ u < μ s + ε / c := s.exists_is_open_lt_of_lt _ this, refine ⟨set.indicator u (λ x, c), λ x, _, u_open.lower_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset su (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ u ≤ c * μ s + ε, by simpa only [hs, u_open.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ u ≤ c * (μ s + ε / c) : ennreal.mul_le_mul le_rfl μu.le ... = c * μ s + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { rcases h₁ (ennreal.half_pos ε0).ne' with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.half_pos ε0).ne' with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add_left f₁.measurable.coe_nnreal_ennreal, lintegral_add_left g₁cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, simp only [], conv_lhs { rw ← ennreal.add_halves ε }, abel } end open simple_func (eapprox_diff tsum_eapprox_diff) /-- Given a measurable function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_le_lower_semicontinuous_lintegral_ge (f : α → ℝ≥0∞) (hf : measurable f) {ε : ℝ≥0∞} (εpos : ε ≠ 0) : ∃ g : α → ℝ≥0∞, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases ennreal.exists_pos_sum_of_countable' εpos ℕ with ⟨δ, δpos, hδ⟩, have : ∀ n, ∃ g : α → ℝ≥0, (∀ x, simple_func.eapprox_diff f n x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, simple_func.eapprox_diff f n x ∂μ + δ n) := λ n, simple_func.exists_le_lower_semicontinuous_lintegral_ge μ (simple_func.eapprox_diff f n) (δpos n).ne', choose g f_le_g gcont hg using this, refine ⟨λ x, (∑' n, g n x), λ x, _, _, _⟩, { rw ← tsum_eapprox_diff f hf, exact ennreal.tsum_le_tsum (λ n, ennreal.coe_le_coe.2 (f_le_g n x)) }, { apply lower_semicontinuous_tsum (λ n, _), exact ennreal.continuous_coe.comp_lower_semicontinuous (gcont n) (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { calc ∫⁻ x, ∑' (n : ℕ), g n x ∂μ = ∑' n, ∫⁻ x, g n x ∂μ : by rw lintegral_tsum (λ n, (gcont n).measurable.coe_nnreal_ennreal.ae_measurable) ... ≤ ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ + δ n) : ennreal.tsum_le_tsum hg ... = ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ) + ∑' n, δ n : ennreal.tsum_add ... ≤ ∫⁻ (x : α), f x ∂μ + ε : begin refine add_le_add _ hδ.le, rw [← lintegral_tsum], { simp_rw [tsum_eapprox_diff f hf, le_refl] }, { assume n, exact (simple_func.measurable _).coe_nnreal_ennreal.ae_measurable } end } end /-- Given a measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge [sigma_finite μ] (f : α → ℝ≥0) (fmeas : measurable f) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin have : ε / 2 ≠ 0 := (ennreal.half_pos ε0).ne', rcases exists_pos_lintegral_lt_of_sigma_finite μ this with ⟨w, wpos, wmeas, wint⟩, let f' := λ x, ((f x + w x : ℝ≥0) : ℝ≥0∞), rcases exists_le_lower_semicontinuous_lintegral_ge μ f' (fmeas.add wmeas).coe_nnreal_ennreal this with ⟨g, le_g, gcont, gint⟩, refine ⟨g, λ x, _, gcont, _⟩, { calc (f x : ℝ≥0∞) < f' x : by simpa [← ennreal.coe_lt_coe] using add_lt_add_left (wpos x) (f x) ... ≤ g x : le_g x }, { calc ∫⁻ (x : α), g x ∂μ ≤ ∫⁻ (x : α), f x + w x ∂μ + ε / 2 : gint ... = ∫⁻ (x : α), f x ∂ μ + ∫⁻ (x : α), w x ∂ μ + (ε / 2) : by rw lintegral_add_right _ wmeas.coe_nnreal_ennreal ... ≤ ∫⁻ (x : α), f x ∂ μ + ε / 2 + ε / 2 : add_le_add_right (add_le_add_left wint.le _) _ ... = ∫⁻ (x : α), f x ∂μ + ε : by rw [add_assoc, ennreal.add_halves] }, end /-- Given an almost everywhere measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable [sigma_finite μ] (f : α → ℝ≥0) (fmeas : ae_measurable f μ) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin have : ε / 2 ≠ 0 := (ennreal.half_pos ε0).ne', rcases exists_lt_lower_semicontinuous_lintegral_ge μ (fmeas.mk f) fmeas.measurable_mk this with ⟨g0, f_lt_g0, g0_cont, g0_int⟩, rcases exists_measurable_superset_of_null fmeas.ae_eq_mk with ⟨s, hs, smeas, μs⟩, rcases exists_le_lower_semicontinuous_lintegral_ge μ (s.indicator (λ x, ∞)) (measurable_const.indicator smeas) this with ⟨g1, le_g1, g1_cont, g1_int⟩, refine ⟨λ x, g0 x + g1 x, λ x, _, g0_cont.add g1_cont, _⟩, { by_cases h : x ∈ s, { have := le_g1 x, simp only [h, set.indicator_of_mem, top_le_iff] at this, simp [this] }, { have : f x = fmeas.mk f x, by { rw set.compl_subset_comm at hs, exact hs h }, rw this, exact (f_lt_g0 x).trans_le le_self_add } }, { calc ∫⁻ x, g0 x + g1 x ∂μ = ∫⁻ x, g0 x ∂μ + ∫⁻ x, g1 x ∂μ : lintegral_add_left g0_cont.measurable _ ... ≤ (∫⁻ x, f x ∂μ + ε / 2) + (0 + ε / 2) : begin refine add_le_add _ _, { convert g0_int using 2, exact lintegral_congr_ae (fmeas.ae_eq_mk.fun_comp _) }, { convert g1_int, simp only [smeas, μs, lintegral_const, set.univ_inter, measurable_set.univ, lintegral_indicator, mul_zero, restrict_apply] } end ... = ∫⁻ x, f x ∂μ + ε : by simp only [add_assoc, ennreal.add_halves, zero_add] } end variable {μ} /-- Given an integrable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_integral_gt_nnreal [sigma_finite μ] (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (integrable (λ x, (g x).to_real) μ) ∧ (∫ x, (g x).to_real ∂μ < ∫ x, f x ∂μ + ε) := begin have fmeas : ae_measurable f μ, by { convert fint.ae_strongly_measurable.real_to_nnreal.ae_measurable, ext1 x, simp only [real.to_nnreal_coe] }, lift ε to ℝ≥0 using εpos.le, obtain ⟨δ, δpos, hδε⟩ : ∃ δ : ℝ≥0, 0 < δ ∧ δ < ε, from exists_between εpos, have int_f_ne_top : ∫⁻ (a : α), (f a) ∂μ ≠ ∞ := (has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral).ne, rcases exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable μ f fmeas (ennreal.coe_ne_zero.2 δpos.ne') with ⟨g, f_lt_g, gcont, gint⟩, have gint_ne : ∫⁻ (x : α), g x ∂μ ≠ ∞ := ne_top_of_le_ne_top (by simpa) gint, have g_lt_top : ∀ᵐ (x : α) ∂μ, g x < ∞ := ae_lt_top gcont.measurable gint_ne, have Ig : ∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ = ∫⁻ (a : α), g a ∂μ, { apply lintegral_congr_ae, filter_upwards [g_lt_top] with _ hx, simp only [hx.ne, ennreal.of_real_to_real, ne.def, not_false_iff], }, refine ⟨g, f_lt_g, gcont, g_lt_top, _, _⟩, { refine ⟨gcont.measurable.ennreal_to_real.ae_measurable.ae_strongly_measurable, _⟩, simp only [has_finite_integral_iff_norm, real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg], convert gint_ne.lt_top using 1 }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { calc ennreal.to_real (∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ) = ennreal.to_real (∫⁻ (a : α), g a ∂μ) : by congr' 1 ... ≤ ennreal.to_real (∫⁻ (a : α), f a ∂μ + δ) : begin apply ennreal.to_real_mono _ gint, simpa using int_f_ne_top, end ... = ennreal.to_real (∫⁻ (a : α), f a ∂μ) + δ : by rw [ennreal.to_real_add int_f_ne_top ennreal.coe_ne_top, ennreal.coe_to_real] ... < ennreal.to_real (∫⁻ (a : α), f a ∂μ) + ε : add_lt_add_left hδε _ ... = (∫⁻ (a : α), ennreal.of_real ↑(f a) ∂μ).to_real + ε : by simp }, { apply filter.eventually_of_forall (λ x, _), simp }, { exact fmeas.coe_nnreal_real.ae_strongly_measurable, }, { apply filter.eventually_of_forall (λ x, _), simp }, { apply gcont.measurable.ennreal_to_real.ae_measurable.ae_strongly_measurable } } end /-! ### Upper semicontinuous lower bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_upper_semicontinuous_le_lintegral_le (f : α →ₛ ℝ≥0) (int_f : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin induction f using measure_theory.simple_func.induction with c s hs f₁ f₂ H h₁ h₂ generalizing ε, { let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases hc : c = 0, { refine ⟨λ x, 0, _, upper_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [hc, set.indicator_zero', lintegral_const, zero_mul, pi.zero_apply, simple_func.const_zero, zero_add, zero_le', simple_func.coe_zero, set.piecewise_eq_indicator, ennreal.coe_zero, simple_func.coe_piecewise, zero_le] } }, have μs_lt_top : μ s < ∞, by simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, or_false, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, restrict_apply measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, function.const_apply, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and] using int_f, have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨ε0, ennreal.coe_ne_top⟩, obtain ⟨F, Fs, F_closed, μF⟩ : ∃ F ⊆ s, is_closed F ∧ μ s < μ F + ε / c := hs.exists_is_closed_lt_add μs_lt_top.ne this.ne', refine ⟨set.indicator F (λ x, c), λ x, _, F_closed.upper_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset Fs (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ s ≤ c * μ F + ε, by simpa only [hs, F_closed.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ s ≤ c * (μ F + ε / c) : ennreal.mul_le_mul le_rfl μF.le ... = c * μ F + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { have A : ∫⁻ (x : α), f₁ x ∂μ + ∫⁻ (x : α), f₂ x ∂μ ≠ ⊤, by rwa ← lintegral_add_left f₁.measurable.coe_nnreal_ennreal, rcases h₁ (ennreal.add_ne_top.1 A).1 (ennreal.half_pos ε0).ne' with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.add_ne_top.1 A).2 (ennreal.half_pos ε0).ne' with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add_left f₁.measurable.coe_nnreal_ennreal, lintegral_add_left g₁cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, simp only [], conv_lhs { rw ← ennreal.add_halves ε }, abel } end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_lintegral_le (f : α → ℝ≥0) (int_f : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin obtain ⟨fs, fs_le_f, int_fs⟩ : ∃ (fs : α →ₛ ℝ≥0), (∀ x, fs x ≤ f x) ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε/2) := begin have := ennreal.lt_add_right int_f (ennreal.half_pos ε0).ne', conv_rhs at this { rw lintegral_eq_nnreal (λ x, (f x : ℝ≥0∞)) μ }, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp only [lt_supr_iff] at this, rcases this with ⟨fs, fs_le_f, int_fs⟩, refine ⟨fs, λ x, by simpa only [ennreal.coe_le_coe] using fs_le_f x, _⟩, convert int_fs.le, rw ← simple_func.lintegral_eq_lintegral, refl end, have int_fs_lt_top : ∫⁻ x, fs x ∂μ ≠ ∞, { apply ne_top_of_le_ne_top int_f (lintegral_mono (λ x, _)), simpa only [ennreal.coe_le_coe] using fs_le_f x }, obtain ⟨g, g_le_fs, gcont, gint⟩ : ∃ g : α → ℝ≥0, (∀ x, g x ≤ fs x) ∧ upper_semicontinuous g ∧ (∫⁻ x, fs x ∂μ ≤ ∫⁻ x, g x ∂μ + ε/2) := fs.exists_upper_semicontinuous_le_lintegral_le int_fs_lt_top (ennreal.half_pos ε0).ne', refine ⟨g, λ x, (g_le_fs x).trans (fs_le_f x), gcont, _⟩, calc ∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε / 2 : int_fs ... ≤ (∫⁻ x, g x ∂μ + ε / 2) + ε / 2 : add_le_add gint le_rfl ... = ∫⁻ x, g x ∂μ + ε : by rw [add_assoc, ennreal.add_halves] end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_integral_le (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, (g x : ℝ)) μ) ∧ (∫ x, (f x : ℝ) ∂μ - ε ≤ ∫ x, g x ∂μ) := begin lift ε to ℝ≥0 using εpos.le, rw [nnreal.coe_pos, ← ennreal.coe_pos] at εpos, have If : ∫⁻ x, f x ∂ μ < ∞ := has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral, rcases exists_upper_semicontinuous_le_lintegral_le f If.ne εpos.ne' with ⟨g, gf, gcont, gint⟩, have Ig : ∫⁻ x, g x ∂ μ < ∞, { apply lt_of_le_of_lt (lintegral_mono (λ x, _)) If, simpa using gf x }, refine ⟨g, gf, gcont, _, _⟩, { refine integrable.mono fint gcont.measurable.coe_nnreal_real.ae_measurable.ae_strongly_measurable _, exact filter.eventually_of_forall (λ x, by simp [gf x]) }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { rw sub_le_iff_le_add, convert ennreal.to_real_mono _ gint, { simp, }, { rw ennreal.to_real_add Ig.ne ennreal.coe_ne_top, simp }, { simpa using Ig.ne } }, { apply filter.eventually_of_forall, simp }, { exact gcont.measurable.coe_nnreal_real.ae_measurable.ae_strongly_measurable }, { apply filter.eventually_of_forall, simp }, { exact fint.ae_strongly_measurable } } end /-! ### Vitali-Carathéodory theorem -/ /-- **Vitali-Carathéodory Theorem**: given an integrable real function `f`, there exists an integrable function `g > f` which is lower semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_lt_lower_semicontinuous_integral_lt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (f x : ereal) < g x) ∧ lower_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (∫ x, ereal.to_real (g x) ∂μ < ∫ x, f x ∂μ + ε) := begin let δ : ℝ≥0 := ⟨ε/2, (half_pos εpos).le⟩, have δpos : 0 < δ := half_pos εpos, let fp : α → ℝ≥0 := λ x, real.to_nnreal (f x), have int_fp : integrable (λ x, (fp x : ℝ)) μ := hf.real_to_nnreal, rcases exists_lt_lower_semicontinuous_integral_gt_nnreal fp int_fp δpos with ⟨gp, fp_lt_gp, gpcont, gp_lt_top, gp_integrable, gpint⟩, let fm : α → ℝ≥0 := λ x, real.to_nnreal (-f x), have int_fm : integrable (λ x, (fm x : ℝ)) μ := hf.neg.real_to_nnreal, rcases exists_upper_semicontinuous_le_integral_le fm int_fm δpos with ⟨gm, gm_le_fm, gmcont, gm_integrable, gmint⟩, let g : α → ereal := λ x, (gp x : ereal) - (gm x), have ae_g : ∀ᵐ x ∂ μ, (g x).to_real = (gp x : ereal).to_real - (gm x : ereal).to_real, { filter_upwards [gp_lt_top] with _ hx, rw ereal.to_real_sub; simp [hx.ne], }, refine ⟨g, _, _, _, _, _⟩, show integrable (λ x, ereal.to_real (g x)) μ, { rw integrable_congr ae_g, convert gp_integrable.sub gm_integrable, ext x, simp }, show ∫ (x : α), (g x).to_real ∂μ < ∫ (x : α), f x ∂μ + ε, from calc ∫ (x : α), (g x).to_real ∂μ = ∫ (x : α), ereal.to_real (gp x) - ereal.to_real (gm x) ∂μ : integral_congr_ae ae_g ... = ∫ (x : α), ereal.to_real (gp x) ∂ μ - ∫ (x : α), gm x ∂μ : begin simp only [ereal.to_real_coe_ennreal, ennreal.coe_to_real, coe_coe], exact integral_sub gp_integrable gm_integrable, end ... < ∫ (x : α), ↑(fp x) ∂μ + ↑δ - ∫ (x : α), gm x ∂μ : begin apply sub_lt_sub_right, convert gpint, simp only [ereal.to_real_coe_ennreal], end ... ≤ ∫ (x : α), ↑(fp x) ∂μ + ↑δ - (∫ (x : α), fm x ∂μ - δ) : sub_le_sub_left gmint _ ... = ∫ (x : α), f x ∂μ + 2 * δ : by { simp_rw [integral_eq_integral_pos_part_sub_integral_neg_part hf, fp, fm], ring } ... = ∫ (x : α), f x ∂μ + ε : by { congr' 1, field_simp [δ, mul_comm] }, show ∀ᵐ (x : α) ∂μ, g x < ⊤, { filter_upwards [gp_lt_top] with _ hx, simp only [g, sub_eq_add_neg, coe_coe, ne.def, (ereal.add_lt_top _ _).ne, lt_top_iff_ne_top, lt_top_iff_ne_top.1 hx, ereal.coe_ennreal_eq_top_iff, not_false_iff, ereal.neg_eq_top_iff, ereal.coe_ennreal_ne_bot] }, show ∀ x, (f x : ereal) < g x, { assume x, rw ereal.coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (f x), refine ereal.sub_lt_sub_of_lt_of_le _ _ _ _, { simp only [ereal.coe_ennreal_lt_coe_ennreal_iff, coe_coe], exact (fp_lt_gp x) }, { simp only [ennreal.coe_le_coe, ereal.coe_ennreal_le_coe_ennreal_iff, coe_coe], exact (gm_le_fm x) }, { simp only [ereal.coe_ennreal_ne_bot, ne.def, not_false_iff, coe_coe] }, { simp only [ereal.coe_nnreal_ne_top, ne.def, not_false_iff, coe_coe] } }, show lower_semicontinuous g, { apply lower_semicontinuous.add', { exact continuous_coe_ennreal_ereal.comp_lower_semicontinuous gpcont (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy) }, { apply ereal.continuous_neg.comp_upper_semicontinuous_antitone _ (λ x y hxy, ereal.neg_le_neg_iff.2 hxy), dsimp, apply continuous_coe_ennreal_ereal.comp_upper_semicontinuous _ (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy), exact ennreal.continuous_coe.comp_upper_semicontinuous gmcont (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { assume x, exact ereal.continuous_at_add (by simp) (by simp) } } end /-- **Vitali-Carathéodory Theorem**: given an integrable real function `f`, there exists an integrable function `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_upper_semicontinuous_lt_integral_gt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (g x : ereal) < f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂μ, ⊥ < g x) ∧ (∫ x, f x ∂μ < ∫ x, ereal.to_real (g x) ∂μ + ε) := begin rcases exists_lt_lower_semicontinuous_integral_lt (λ x, - f x) hf.neg εpos with ⟨g, g_lt_f, gcont, g_integrable, g_lt_top, gint⟩, refine ⟨λ x, - g x, _, _, _, _, _⟩, { exact λ x, ereal.neg_lt_iff_neg_lt.1 (by simpa only [ereal.coe_neg] using g_lt_f x) }, { exact ereal.continuous_neg.comp_lower_semicontinuous_antitone gcont (λ x y hxy, ereal.neg_le_neg_iff.2 hxy) }, { convert g_integrable.neg, ext x, simp }, { simpa [bot_lt_iff_ne_bot, lt_top_iff_ne_top] using g_lt_top }, { simp_rw [integral_neg, lt_neg_add_iff_add_lt] at gint, rw add_comm at gint, simpa [integral_neg] using gint } end end measure_theory
49ad6201aef039fc7349a5549e09a32aba436862
2fbe653e4bc441efde5e5d250566e65538709888
/src/linear_algebra/basis.lean
8634c93d4f027c19262821bd76d43c7ec30e7d31
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
53,764
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp -/ import linear_algebra.basic linear_algebra.finsupp order.zorn /-! # Linear independence and bases This file defines linear independence and bases in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where M is the module or vectorspace and `ι : Type*` is an arbitrary indexing type. * `linear_independent R v` states that the elements of the family `v` are linear independent * `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)` on the linear independent vectors `v`, given `hv : linear_independent R v` (using classical choice). `linear_independent.repr hv` is provided as a linear map. * `is_basis R v` states that the vector family `v` is a basis, i.e. it is linear independent and spans the entire space * `is_basis.repr hv x` is the basis version of `linear_independent.repr hv x`. It returns the linear combination representing `x : M` on a basis `v` of `M` (using classical choice). The argument `hv` must be a proof that `is_basis R v`. `is_basis.repr hv` is given as a linear map as well. * `is_basis.constr hv f` constructs a linear map `M₁ →ₗ[R] M₂` given the values `f : ι → M₂` at the basis `v : ι → M₁`, given `hv : is_basis R v`. ## Main statements * `is_basis.ext` states that two linear maps are equal if they coincide on a basis. * `exists_is_basis` states that every vector space has a basis. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. For bases, this is useful as well because we can easily derive ordered bases by using an ordered index type ι. If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas `linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two worlds. ## Tags linearly dependent, linear dependence, linearly independent, linear independence, basis -/ noncomputable theory open function lattice set submodule open_locale classical variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} {M : Type*} {M' : Type*} {V : Type*} {V' : Type*} section module variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] variables [module R M] [module R M'] variables {a b : R} {x y : M} include R variables (R) (v) /-- Linearly independent family of vectors -/ def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥ variables {R} {v} theorem linear_independent_iff : linear_independent R v ↔ ∀l, finsupp.total ι M R v l = 0 → l = 0 := by simp [linear_independent, linear_map.ker_eq_bot'] theorem linear_independent_iff' : linear_independent R v ↔ ∀ s : finset ι, ∀ g : ι → R, s.sum (λ i, g i • v i) = 0 → ∀ i ∈ s, g i = 0 := linear_independent_iff.trans ⟨λ hf s g hg i his, have h : _ := hf (s.sum $ λ i, finsupp.single i (g i)) $ by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) : by rw [finsupp.lapply_apply, finsupp.single_eq_same] ... = s.sum (λ j, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j))) : eq.symm $ finset.sum_eq_single i (λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji]) (λ hnis, hnis.elim his) ... = s.sum (λ j, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm ... = 0 : finsupp.ext_iff.1 h i, λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $ finsupp.mem_support_iff.2 hni⟩ lemma linear_independent_empty_type (h : ¬ nonempty ι) : linear_independent R v := begin rw [linear_independent_iff], intros, ext i, exact false.elim (not_nonempty_iff_imp_false.1 h i) end lemma ne_zero_of_linear_independent {i : ι} (ne : 0 ≠ (1:R)) (hv : linear_independent R v) : v i ≠ 0 := λ h, ne $ eq.symm begin suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa}, rw linear_independent_iff.1 hv (finsupp.single i 1), {simp}, {simp [h]} end lemma linear_independent.comp (h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) := begin rw [linear_independent_iff, finsupp.total_comp], intros l hl, have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0, by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp, ext, convert h_map_domain a, simp only [finsupp.map_domain_apply hf], end lemma linear_independent_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : linear_independent R v := linear_independent_iff.2 (λ l hl, finsupp.eq_zero_of_zero_eq_one zero_eq_one _) lemma linear_independent.unique (hv : linear_independent R v) {l₁ l₂ : ι →₀ R} : finsupp.total ι M R v l₁ = finsupp.total ι M R v l₂ → l₁ = l₂ := by apply linear_map.ker_eq_bot.1 hv lemma linear_independent.injective (zero_ne_one : (0 : R) ≠ 1) (hv : linear_independent R v) : injective v := begin intros i j hij, let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1, have h_total : finsupp.total ι M R v l = 0, { rw finsupp.total_apply, rw finsupp.sum_sub_index, { simp [finsupp.sum_single_index, hij] }, { intros, apply sub_smul } }, have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1, { rw linear_independent_iff at hv, simp [eq_add_of_sub_eq' (hv l h_total)] }, show i = j, { apply or.elim ((finsupp.single_eq_single_iff _ _ _ _).1 h_single_eq), simp, exact λ h, false.elim (zero_ne_one.symm h.1) } end lemma linear_independent_span (hs : linear_independent R v) : @linear_independent ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ := begin rw linear_independent_iff at *, intros l hl, apply hs l, have := congr_arg (submodule.subtype (span R (range v))) hl, convert this, rw [finsupp.total_apply, finsupp.total_apply], unfold finsupp.sum, rw linear_map.map_sum (submodule.subtype (span R (range v))), simp end section subtype /- The following lemmas use the subtype defined by a set in M as the index set ι. -/ theorem linear_independent_comp_subtype {s : set ι} : linear_independent R (v ∘ subtype.val : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 := begin rw [linear_independent_iff, finsupp.total_comp], simp only [linear_map.comp_apply], split, { intros h l hl₁ hl₂, have h_bij : bij_on subtype.val (subtype.val ⁻¹' l.support.to_set : set s) l.support.to_set, { apply bij_on.mk, { unfold maps_to }, { apply set.inj_on_of_injective _ subtype.val_injective }, intros i hi, rw mem_image, use subtype.mk i (((finsupp.mem_supported _ _).1 hl₁ : ↑(l.support) ⊆ s) hi), rw mem_preimage, exact ⟨hi, rfl⟩ }, show l = 0, { apply finsupp.eq_zero_of_comap_domain_eq_zero (subtype.val : s → ι) _ h_bij, apply h, convert hl₂, rw [finsupp.lmap_domain_apply, finsupp.map_domain_comap_domain], apply subtype.val_injective, rw subtype.range_val, exact (finsupp.mem_supported _ _).1 hl₁ } }, { intros h l hl, have hl' : finsupp.total ι M R v (finsupp.emb_domain ⟨subtype.val, subtype.val_injective⟩ l) = 0, { rw finsupp.emb_domain_eq_map_domain ⟨subtype.val, subtype.val_injective⟩ l, apply hl }, apply finsupp.emb_domain_inj.1, rw [h (finsupp.emb_domain ⟨subtype.val, subtype.val_injective⟩ l) _ hl', finsupp.emb_domain_zero], rw [finsupp.mem_supported, finsupp.support_emb_domain], intros x hx, rw [finset.mem_coe, finset.mem_map] at hx, rcases hx with ⟨i, x', hx'⟩, rw ←hx', simp } end theorem linear_independent_subtype {s : set M} : linear_independent R (λ x, x : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 := by apply @linear_independent_comp_subtype _ _ _ id theorem linear_independent_comp_subtype_disjoint {s : set ι} : linear_independent R (v ∘ subtype.val : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker := by rw [linear_independent_comp_subtype, linear_map.disjoint_ker] theorem linear_independent_subtype_disjoint {s : set M} : linear_independent R (λ x, x : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker := by apply @linear_independent_comp_subtype_disjoint _ _ _ id theorem linear_independent_iff_total_on {s : set M} : linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ := by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot, linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype, map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff] lemma linear_independent.to_subtype_range (hv : linear_independent R v) : linear_independent R (λ x, x : range v → M) := begin by_cases zero_eq_one : (0 : R) = 1, { apply linear_independent_of_zero_eq_one zero_eq_one }, rw linear_independent_subtype, intros l hl₁ hl₂, have h_bij : bij_on v (v ⁻¹' finset.to_set (l.support)) (finset.to_set (l.support)), { apply bij_on.mk, { unfold maps_to }, { apply set.inj_on_of_injective _ (linear_independent.injective zero_eq_one hv) }, intros x hx, rcases mem_range.1 (((finsupp.mem_supported _ _).1 hl₁ : ↑(l.support) ⊆ range v) hx) with ⟨i, hi⟩, rw mem_image, use i, rw [mem_preimage, hi], exact ⟨hx, rfl⟩ }, apply finsupp.eq_zero_of_comap_domain_eq_zero v l, apply linear_independent_iff.1 hv, rw [finsupp.total_comap_domain, finset.sum_preimage v l.support h_bij (λ (x : M), l x • x)], rw [finsupp.total_apply, finsupp.sum] at hl₂, apply hl₂ end lemma linear_independent.of_subtype_range (hv : injective v) (h : linear_independent R (λ x, x : range v → M)) : linear_independent R v := begin rw linear_independent_iff, intros l hl, apply finsupp.injective_map_domain hv, apply linear_independent_subtype.1 h (l.map_domain v), { rw finsupp.mem_supported, intros x hx, have := finset.mem_coe.2 (finsupp.map_domain_support hx), rw finset.coe_image at this, apply set.image_subset_range _ _ this, }, { rwa [finsupp.total_map_domain _ _ hv, left_id] } end lemma linear_independent.restrict_of_comp_subtype {s : set ι} (hs : linear_independent R (v ∘ subtype.val : s → M)) : linear_independent R (function.restrict v s) := begin have h_restrict : restrict v s = v ∘ (λ x, x.val) := rfl, rw [linear_independent_iff, h_restrict, finsupp.total_comp], intros l hl, have h_map_domain_subtype_eq_0 : l.map_domain subtype.val = 0, { rw linear_independent_comp_subtype at hs, apply hs (finsupp.lmap_domain R R (λ x : subtype s, x.val) l) _ hl, rw finsupp.mem_supported, simp, intros x hx, have := finset.mem_coe.2 (finsupp.map_domain_support (finset.mem_coe.1 hx)), rw finset.coe_image at this, exact subtype.val_image_subset _ _ this }, apply @finsupp.injective_map_domain _ (subtype s) ι, { apply subtype.val_injective }, { simpa }, end lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) := by simp [linear_independent_subtype_disjoint] lemma linear_independent.mono {t s : set M} (h : t ⊆ s) : linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) := begin simp only [linear_independent_subtype_disjoint], exact (disjoint_mono_left (finsupp.supported_mono h)) end lemma linear_independent_union {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M)) (hst : disjoint (span R s) (span R t)) : linear_independent R (λ x, x : (s ∪ t) → M) := begin rw [linear_independent_subtype_disjoint, disjoint_def, finsupp.supported_union], intros l h₁ h₂, rw mem_sup at h₁, rcases h₁ with ⟨ls, hls, lt, hlt, rfl⟩, have h_ls_mem_t : finsupp.total M M R id ls ∈ span R t, { rw [← image_id t, finsupp.span_eq_map_total], apply (add_mem_iff_left (map _ _) (mem_image_of_mem _ hlt)).1, rw [← linear_map.map_add, linear_map.mem_ker.1 h₂], apply zero_mem }, have h_lt_mem_s : finsupp.total M M R id lt ∈ span R s, { rw [← image_id s, finsupp.span_eq_map_total], apply (add_mem_iff_left (map _ _) (mem_image_of_mem _ hls)).1, rw [← linear_map.map_add, add_comm, linear_map.mem_ker.1 h₂], apply zero_mem }, have h_ls_mem_s : (finsupp.total M M R id) ls ∈ span R s, { rw ← image_id s, apply (finsupp.mem_span_iff_total _).2 ⟨ls, hls, rfl⟩ }, have h_lt_mem_t : (finsupp.total M M R id) lt ∈ span R t, { rw ← image_id t, apply (finsupp.mem_span_iff_total _).2 ⟨lt, hlt, rfl⟩ }, have h_ls_0 : ls = 0 := disjoint_def.1 (linear_independent_subtype_disjoint.1 hs) _ hls (linear_map.mem_ker.2 $ disjoint_def.1 hst (finsupp.total M M R id ls) h_ls_mem_s h_ls_mem_t), have h_lt_0 : lt = 0 := disjoint_def.1 (linear_independent_subtype_disjoint.1 ht) _ hlt (linear_map.mem_ker.2 $ disjoint_def.1 hst (finsupp.total M M R id lt) h_lt_mem_s h_lt_mem_t), show ls + lt = 0, by simp [h_ls_0, h_lt_0], end lemma linear_independent_of_finite (s : set M) (H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) : linear_independent R (λ x, x : s → M) := linear_independent_subtype.2 $ λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _) lemma linear_independent_Union_of_directed {η : Type*} {s : η → set M} (hs : directed (⊆) s) (h : ∀ i, linear_independent R (λ x, x : s i → M)) : linear_independent R (λ x, x : (⋃ i, s i) → M) := begin haveI := classical.dec (nonempty η), by_cases hη : nonempty η, { refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _), rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩, rcases hs.finset_le hη fi.to_finset with ⟨i, hi⟩, exact (h i).mono (subset.trans hI $ bUnion_subset $ λ j hj, hi j (finite.mem_to_finset.2 hj)) }, { refine linear_independent_empty.mono _, rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ } end lemma linear_independent_sUnion_of_directed {s : set (set M)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) : linear_independent R (λ x, x : (⋃₀ s) → M) := by rw sUnion_eq_Union; exact linear_independent_Union_of_directed ((directed_on_iff_directed _).1 hs) (by simpa using h) lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M} (hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) : linear_independent R (λ x, x : (⋃a∈s, t a) → M) := by rw bUnion_eq_Union; exact linear_independent_Union_of_directed ((directed_comp _ _ _).2 $ (directed_on_iff_directed _).1 hs) (by simpa using h) lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M} (hl : ∀i, linear_independent R (λ x, x : f i → M)) (hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) : linear_independent R (λ x, x : (⋃i, f i) → M) := begin rw [Union_eq_Union_finset f], apply linear_independent_Union_of_directed, apply directed_of_sup, exact (assume t₁ t₂ ht, Union_subset_Union $ assume i, Union_subset_Union_const $ assume h, ht h), assume t, rw [set.Union, ← finset.sup_eq_supr], refine t.induction_on _ _, { rw finset.sup_empty, apply linear_independent_empty_type (not_nonempty_iff_imp_false.2 _), exact λ x, set.not_mem_empty x (subtype.mem x) }, { rintros ⟨i⟩ s his ih, rw [finset.sup_insert], apply linear_independent_union, { apply hl }, { apply ih }, rw [finset.sup_eq_supr], refine disjoint_mono (le_refl _) _ (hd i _ _ his), { simp only [(span_Union _).symm], refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _), rintros ⟨i⟩, exact ⟨i, le_refl _⟩ }, { change finite (plift.up ⁻¹' s.to_set), exact finite_preimage (inj_on_of_injective _ (assume i j, plift.up.inj)) s.finite_to_set } } end lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*} {f : Π j : η, ιs j → M} (hindep : ∀j, linear_independent R (f j)) (hd : ∀i, ∀t:set η, finite t → i ∉ t → disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) : linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) := begin by_cases zero_eq_one : (0 : R) = 1, { apply linear_independent_of_zero_eq_one zero_eq_one }, apply linear_independent.of_subtype_range, { rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy, by_cases h_cases : x₁ = y₁, subst h_cases, { apply sigma.eq, rw linear_independent.injective zero_eq_one (hindep _) hxy, refl }, { have h0 : f x₁ x₂ = 0, { apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁) (λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)), rw supr_singleton, simp only [] at hxy, rw hxy, exact (subset_span (mem_range_self y₂)) }, exact false.elim (ne_zero_of_linear_independent zero_eq_one (hindep x₁) h0) } }, rw range_sigma_eq_Union_range, apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd, end end subtype section repr variables (hv : linear_independent R v) /-- Canonical isomorphism between linear combinations and the span of linearly independent vectors. -/ def linear_independent.total_equiv (hv : linear_independent R v) : (ι →₀ R) ≃ₗ[R] span R (range v) := begin apply linear_equiv.of_bijective (linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _), { rw linear_map.ker_cod_restrict, apply hv }, { rw [linear_map.range, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap, range_subtype, map_top], rw finsupp.range_total, apply le_refl (span R (range v)) }, { intro l, rw ← finsupp.range_total, rw linear_map.mem_range, apply mem_range_self l } end /-- Linear combination representing a vector in the span of linearly independent vectors. Given a family of linearly independent vectors, we can represent any vector in their span as a linear combination of these vectors. These are provided by this linear map. It is simply one direction of `linear_independent.total_equiv` -/ def linear_independent.repr (hv : linear_independent R v) : span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := subtype.coe_ext.1 (linear_equiv.apply_symm_apply hv.total_equiv x) lemma linear_independent.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = submodule.subtype _ := linear_map.ext $ hv.total_repr lemma linear_independent.repr_ker : hv.repr.ker = ⊥ := by rw [linear_independent.repr, linear_equiv.ker] lemma linear_independent.repr_range : hv.repr.range = ⊤ := by rw [linear_independent.repr, linear_equiv.range] lemma linear_independent.repr_eq {l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) : hv.repr x = l := begin have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l) = finsupp.total ι M R v l := rfl, have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x, { rw eq at this, exact subtype.coe_ext.2 this }, rw ←linear_equiv.symm_apply_apply hv.total_equiv l, rw ←this, refl, end lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) : hv.repr x = finsupp.single i 1 := begin apply hv.repr_eq, simp [finsupp.total_single, hx] end lemma linear_independent_iff_not_smul_mem_span : linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) := ⟨ λ hv i a ha, begin rw [finsupp.span_eq_map_total, mem_map] at ha, rcases ha with ⟨l, hl, e⟩, rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl, by_contra hn, exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _), end, λ H, linear_independent_iff.2 $ λ l hl, begin ext i, simp, by_contra hn, refine hn (H i _ _), refine (finsupp.mem_span_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩, { rw finsupp.mem_supported', intros j hj, have hij : j = i := classical.not_not.1 (λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)), simp [hij] }, { simp [hl] } end⟩ end repr lemma surjective_of_linear_independent_of_span (hv : linear_independent R v) (f : ι' ↪ ι) (hss : range v ⊆ span R (range (v ∘ f))) (zero_ne_one : 0 ≠ (1 : R)): surjective f := begin intros i, let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.inj).repr, let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f, have h_total_l : finsupp.total ι M R v l = v i, { dsimp only [l], rw finsupp.total_map_domain, rw (hv.comp f f.inj).total_repr, { refl }, { exact f.inj } }, have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1), by rw [h_total_l, finsupp.total_single, one_smul], have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq, dsimp only [l] at l_eq, rw ←finsupp.emb_domain_eq_map_domain at l_eq, rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq with ⟨i', hi'⟩, use i', exact hi'.2 end lemma eq_of_linear_independent_of_span_subtype {s t : set M} (zero_ne_one : (0 : R) ≠ 1) (hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t := begin let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.val_injective (subtype.mk.inj hab)⟩, have h_surj : surjective f, { apply surjective_of_linear_independent_of_span hs f _ zero_ne_one, convert hst; simp [f, comp], }, show s = t, { apply subset.antisymm _ h, intros x hx, rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩, convert y.mem, rw ← subtype.mk.inj hy, refl } end open linear_map lemma linear_independent.image (hv : linear_independent R v) {f : M →ₗ M'} (hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) := begin rw [disjoint, ← set.image_univ, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap, map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj, unfold linear_independent at hv, rw hv at hf_inj, haveI : inhabited M := ⟨0⟩, rw [linear_independent, finsupp.total_comp], rw [@finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f, ker_comp, eq_bot_iff], apply hf_inj, exact λ _, rfl, end lemma linear_independent.image_subtype {s : set M} {f : M →ₗ M'} (hs : linear_independent R (λ x, x : s → M)) (hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') := begin rw [disjoint, ← set.image_id s, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap, map_le_iff_le_comap, comap_bot] at hf_inj, haveI : inhabited M := ⟨0⟩, rw [linear_independent_subtype_disjoint, disjoint, ← finsupp.lmap_domain_supported _ _ f, map_inf_eq_map_inf_comap, map_le_iff_le_comap, ← ker_comp], rw [@finsupp.lmap_domain_total _ _ R _ _ _, ker_comp], { exact le_trans (le_inf inf_le_left hf_inj) (le_trans (linear_independent_subtype_disjoint.1 hs) bot_le) }, { simp } end lemma linear_independent_inl_union_inr {s : set M} {t : set M'} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M')) : linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') := begin apply linear_independent_union, exact (hs.image_subtype $ by simp), exact (ht.image_subtype $ by simp), rw [span_image, span_image]; simp [disjoint_iff, prod_inf_prod] end lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'} (hv : linear_independent R v) (hv' : linear_independent R v') : linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := begin by_cases zero_eq_one : (0 : R) = 1, { apply linear_independent_of_zero_eq_one zero_eq_one }, have inj_v : injective v := (linear_independent.injective zero_eq_one hv), have inj_v' : injective v' := (linear_independent.injective zero_eq_one hv'), apply linear_independent.of_subtype_range, { apply sum.elim_injective, { exact injective_comp prod.injective_inl inj_v }, { exact injective_comp prod.injective_inr inj_v' }, { intros, simp [ne_zero_of_linear_independent zero_eq_one hv] } }, { rw sum.elim_range, apply linear_independent_union, { apply linear_independent.to_subtype_range, apply linear_independent.image hv, simp [ker_inl] }, { apply linear_independent.to_subtype_range, apply linear_independent.image hv', simp [ker_inr] }, { apply disjoint_mono _ _ disjoint_inl_inr, { rw [set.range_comp, span_image], apply linear_map.map_le_range }, { rw [set.range_comp, span_image], apply linear_map.map_le_range } } } end /-- Dedekind's linear independence of characters -/ -- See, for example, Keith Conrad's note <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf> theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [integral_domain L] : @linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ := by letI := classical.dec_eq (G →* L); letI : mul_action L L := distrib_mul_action.to_mul_action L L; -- We prove linear independence by showing that only the trivial linear combination vanishes. exact linear_independent_iff'.2 -- To do this, we use `finset` induction, (λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg, -- Here -- * `a` is a new character we will insert into the `finset` of characters `s`, -- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero -- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero -- and it remains to prove that `g` vanishes on `insert a s`. -- We now make the key calculation: -- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the monoid `G`. have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G, -- We prove these expressions are equal by showing -- the differences of their values on each monoid element `x` is zero eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x) (funext $ λ y : G, calc -- After that, it's just a chase scene. s.sum (λ i, ((g i * i x - g i * a x) • i : G → L)) y = s.sum (λ i, (g i * i x - g i * a x) * i y) : pi.finset_sum_apply _ _ _ ... = s.sum (λ i, g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl (λ _ _, sub_mul _ _ _) ... = s.sum (λ i, g i * i x * i y) - s.sum (λ i, g i * a x * i y) : finset.sum_sub_distrib ... = (g a * a x * a y + s.sum (λ i, g i * i x * i y)) - (g a * a x * a y + s.sum (λ i, g i * a x * i y)) : by rw add_sub_add_left_eq_sub ... = (insert a s).sum (λ i, g i * i x * i y) - (insert a s).sum (λ i, g i * a x * i y) : by rw [finset.sum_insert has, finset.sum_insert has] ... = (insert a s).sum (λ i, g i * i (x * y)) - (insert a s).sum (λ i, a x * (g i * i y)) : congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc])) (finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm]) ... = (insert a s).sum (λ i, (g i • i : G → L)) (x * y) - a x * (insert a s).sum (λ i, (g i • i : G → L)) y : by rw [pi.finset_sum_apply, pi.finset_sum_apply, finset.mul_sum]; refl ... = 0 - a x * 0 : by rw hg; refl ... = 0 : by rw [mul_zero, sub_zero]) i his, -- On the other hand, since `a` is not already in `s`, for any character `i ∈ s` -- there is some element of the monoid on which it differs from `a`. have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his, classical.by_contradiction $ λ h, have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩, has $ hia ▸ his, -- From these two facts we deduce that `g` actually vanishes on `s`, have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in have h : g i • i y = g i • a y, from congr_fun (h1 i his) y, or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy), -- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish, -- we deduce that `g a = 0`. have h4 : g a = 0, from calc g a = g a * 1 : (mul_one _).symm ... = (g a • a : G → L) 1 : by rw ← a.map_one; refl ... = (insert a s).sum (λ i, (g i • i : G → L)) 1 : begin rw finset.sum_eq_single a, { intros i his hia, rw finset.mem_insert at his, rw [h3 i (his.resolve_left hia), zero_smul] }, { intros haas, exfalso, apply haas, exact finset.mem_insert_self a s } end ... = 0 : by rw hg; refl, -- Now we're done; the last two facts together imply that `g` vanishes on every element of `insert a s`. (finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩) lemma le_of_span_le_span {s t u: set M} (zero_ne_one : (0 : R) ≠ 1) (hl : linear_independent R (subtype.val : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u) (hst : span R s ≤ span R t) : s ⊆ t := begin have := eq_of_linear_independent_of_span_subtype zero_ne_one (hl.mono (set.union_subset hsu htu)) (set.subset_union_right _ _) (set.union_subset (set.subset.trans subset_span hst) subset_span), rw ← this, apply set.subset_union_left end lemma span_le_span_iff {s t u: set M} (zero_ne_one : (0 : R) ≠ 1) (hl : linear_independent R (subtype.val : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u) : span R s ≤ span R t ↔ s ⊆ t := ⟨le_of_span_le_span zero_ne_one hl hsu htu, span_mono⟩ variables (R) (v) /-- A family of vectors is a basis if it is linearly independent and all vectors are in the span. -/ def is_basis := linear_independent R v ∧ span R (range v) = ⊤ variables {R} {v} section is_basis variables {s t : set M} (hv : is_basis R v) lemma is_basis.mem_span (hv : is_basis R v) : ∀ x, x ∈ span R (range v) := eq_top_iff'.1 hv.2 lemma is_basis.comp (hv : is_basis R v) (f : ι' → ι) (hf : bijective f) : is_basis R (v ∘ f) := begin split, { apply hv.1.comp f hf.1 }, { rw[set.range_comp, range_iff_surjective.2 hf.2, image_univ, hv.2] } end lemma is_basis.injective (hv : is_basis R v) (zero_ne_one : (0 : R) ≠ 1) : injective v := λ x y h, linear_independent.injective zero_ne_one hv.1 h /- Given a basis, any vector can be written as a linear combination of the basis vectors. They are given by this linear map. This is one direction of `module_equiv_finsupp` -/ def is_basis.repr : M →ₗ (ι →₀ R) := (hv.1.repr).comp (linear_map.id.cod_restrict _ hv.mem_span) lemma is_basis.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := hv.1.total_repr ⟨x, _⟩ lemma is_basis.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = linear_map.id := linear_map.ext hv.total_repr lemma is_basis.repr_ker : hv.repr.ker = ⊥ := linear_map.ker_eq_bot.2 $ injective_of_left_inverse hv.total_repr lemma is_basis.repr_range : hv.repr.range = finsupp.supported R R univ := by rw [is_basis.repr, linear_map.range, submodule.map_comp, linear_map.map_cod_restrict, submodule.map_id, comap_top, map_top, hv.1.repr_range, finsupp.supported_univ] lemma is_basis.repr_total (x : ι →₀ R) (hx : x ∈ finsupp.supported R R (univ : set ι)) : hv.repr (finsupp.total ι M R v x) = x := begin rw [← hv.repr_range, linear_map.mem_range] at hx, cases hx with w hw, rw [← hw, hv.total_repr], end lemma is_basis.repr_eq_single {i} : hv.repr (v i) = finsupp.single i 1 := by apply hv.1.repr_eq_single; simp /-- Construct a linear map given the value at the basis. -/ def is_basis.constr (f : ι → M') : M →ₗ[R] M' := (finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f).comp hv.repr theorem is_basis.constr_apply (f : ι → M') (x : M) : (hv.constr f : M → M') x = (hv.repr x).sum (λb a, a • f b) := by dsimp [is_basis.constr]; rw [finsupp.total_apply, finsupp.sum_map_domain_index]; simp [add_smul] lemma is_basis.ext {f g : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, f (v i) = g (v i)) : f = g := begin apply linear_map.ext (λ x, linear_eq_on (range v) _ (hv.mem_span x)), exact (λ y hy, exists.elim (set.mem_range.1 hy) (λ i hi, by rw ←hi; exact h i)) end lemma constr_basis {f : ι → M'} {i : ι} (hv : is_basis R v) : (hv.constr f : M → M') (v i) = f i := by simp [is_basis.constr_apply, hv.repr_eq_single, finsupp.sum_single_index] lemma constr_eq {g : ι → M'} {f : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, g i = f (v i)) : hv.constr g = f := hv.ext $ λ i, (constr_basis hv).trans (h i) lemma constr_self (f : M →ₗ[R] M') : hv.constr (λ i, f (v i)) = f := constr_eq hv $ λ x, rfl lemma constr_zero (hv : is_basis R v) : hv.constr (λi, (0 : M')) = 0 := constr_eq hv $ λ x, rfl lemma constr_add {g f : ι → M'} (hv : is_basis R v) : hv.constr (λi, f i + g i) = hv.constr f + hv.constr g := constr_eq hv $ by simp [constr_basis hv] {contextual := tt} lemma constr_neg {f : ι → M'} (hv : is_basis R v) : hv.constr (λi, - f i) = - hv.constr f := constr_eq hv $ by simp [constr_basis hv] {contextual := tt} lemma constr_sub {g f : ι → M'} (hs : is_basis R v) : hv.constr (λi, f i - g i) = hs.constr f - hs.constr g := by simp [constr_add, constr_neg] -- this only works on functions if `R` is a commutative ring lemma constr_smul {ι R M M'} [comm_ring R] [add_comm_group M] [add_comm_group M'] [module R M] [module R M'] {v : ι → R} {f : ι → M'} {a : R} (hv : is_basis R v) {b : M} : hv.constr (λb, a • f b) = a • hv.constr f := constr_eq hv $ by simp [constr_basis hv] {contextual := tt} lemma constr_range [inhabited ι] (hv : is_basis R v) {f : ι → M'} : (hv.constr f).range = span R (range f) := by rw [is_basis.constr, linear_map.range_comp, linear_map.range_comp, is_basis.repr_range, finsupp.lmap_domain_supported, ←set.image_univ, ←finsupp.span_eq_map_total, image_id] /-- Canonical equivalence between a module and the linear combinations of basis vectors. -/ def module_equiv_finsupp (hv : is_basis R v) : M ≃ₗ[R] ι →₀ R := (hv.1.total_equiv.trans (linear_equiv.of_top _ hv.2)).symm /-- Isomorphism between the two modules, given two modules M and M' with respective bases v and v' and a bijection between the two bases. -/ def equiv_of_is_basis {v : ι → M} {v' : ι' → M'} {f : M → M'} {g : M' → M} (hv : is_basis R v) (hv' : is_basis R v') (hf : ∀i, f (v i) ∈ range v') (hg : ∀i, g (v' i) ∈ range v) (hgf : ∀i, g (f (v i)) = v i) (hfg : ∀i, f (g (v' i)) = v' i) : M ≃ₗ M' := { inv_fun := hv'.constr (g ∘ v'), left_inv := have (hv'.constr (g ∘ v')).comp (hv.constr (f ∘ v)) = linear_map.id, from hv.ext $ λ i, exists.elim (hf i) (λ i' hi', by simp [constr_basis, hi'.symm]; rw [hi', hgf]), λ x, congr_arg (λ h:M →ₗ[R] M, h x) this, right_inv := have (hv.constr (f ∘ v)).comp (hv'.constr (g ∘ v')) = linear_map.id, from hv'.ext $ λ i', exists.elim (hg i') (λ i hi, by simp [constr_basis, hi.symm]; rw [hi, hfg]), λ y, congr_arg (λ h:M' →ₗ[R] M', h y) this, ..hv.constr (f ∘ v) } lemma is_basis_inl_union_inr {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') : is_basis R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := begin split, apply linear_independent_inl_union_inr' hv.1 hv'.1, rw [sum.elim_range, span_union, set.range_comp, span_image (inl R M M'), hv.2, map_top, set.range_comp, span_image (inr R M M'), hv'.2, map_top], exact linear_map.sup_range_inl_inr end end is_basis lemma is_basis_singleton_one (R : Type*) [unique ι] [ring R] : is_basis R (λ (_ : ι), (1 : R)) := begin split, { refine linear_independent_iff.2 (λ l, _), rw [finsupp.unique_single l, finsupp.total_single, smul_eq_mul, mul_one], intro hi, simp [hi] }, { refine top_unique (λ _ _, _), simp [submodule.mem_span_singleton] } end protected lemma linear_equiv.is_basis (hs : is_basis R v) (f : M ≃ₗ[R] M') : is_basis R (f ∘ v) := begin split, { apply @linear_independent.image _ _ _ _ _ _ _ _ _ _ hs.1 (f : M →ₗ[R] M'), simp [linear_equiv.ker f] }, { rw set.range_comp, have : span R ((f : M →ₗ[R] M') '' range v) = ⊤, { rw [span_image (f : M →ₗ[R] M'), hs.2], simp }, exact this } end lemma is_basis_span (hs : linear_independent R v) : @is_basis ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self _)⟩) _ _ _ := begin split, { apply linear_independent_span hs }, { rw eq_top_iff', intro x, have h₁ : subtype.val '' set.range (λ i, subtype.mk (v i) _) = range v, by rw ←set.range_comp, have h₂ : map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))) = span R (range v), by rw [←span_image, submodule.subtype_eq_val, h₁], have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))), by rw h₂; apply subtype.mem x, rcases mem_map.1 h₃ with ⟨y, hy₁, hy₂⟩, have h_x_eq_y : x = y, by rw [subtype.coe_ext, ← hy₂]; simp, rw h_x_eq_y, exact hy₁ } end lemma is_basis_empty (h_empty : ¬ nonempty ι) (h : ∀x:M, x = 0) : is_basis R (λ x : ι, (0 : M)) := ⟨ linear_independent_empty_type h_empty, eq_top_iff'.2 $ assume x, (h x).symm ▸ submodule.zero_mem _ ⟩ lemma is_basis_empty_bot (h_empty : ¬ nonempty ι) : is_basis R (λ _ : ι, (0 : (⊥ : submodule R M))) := begin apply is_basis_empty h_empty, intro x, apply subtype.ext.2, exact (submodule.mem_bot R).1 (subtype.mem x), end open fintype variables [fintype ι] (h : is_basis R v) /-- A module over R with a finite basis is linearly equivalent to functions from its basis to R. -/ def equiv_fun_basis : M ≃ₗ[R] (ι → R) := linear_equiv.trans (module_equiv_finsupp h) { to_fun := finsupp.to_fun, add := λ x y, by ext; exact finsupp.add_apply, smul := λ x y, by ext; exact finsupp.smul_apply, ..finsupp.equiv_fun_on_fintype } theorem module.card_fintype [fintype R] [fintype M] : card M = (card R) ^ (card ι) := calc card M = card (ι → R) : card_congr (equiv_fun_basis h).to_equiv ... = card R ^ card ι : card_fun /-- Given a basis `v` indexed by `ι`, the canonical linear equivalence between `ι → R` and `M` maps a function `x : ι → R` to the linear combination `∑_i x i • v i`. -/ @[simp] lemma equiv_fun_basis_symm_apply (x : ι → R) : (equiv_fun_basis h).symm x = finset.sum finset.univ (λi, x i • v i) := begin change finsupp.sum ((finsupp.equiv_fun_on_fintype.symm : (ι → R) ≃ (ι →₀ R)) x) (λ (i : ι) (a : R), a • v i) = finset.sum finset.univ (λi, x i • v i), dsimp [finsupp.equiv_fun_on_fintype, finsupp.sum], rw finset.sum_filter, refine finset.sum_congr rfl (λi hi, _), by_cases H : x i = 0, { simp [H] }, { simp [H], refl } end end module section vector_space variables {v : ι → V} [discrete_field K] [add_comm_group V] [add_comm_group V'] [vector_space K V] [vector_space K V'] {s t : set V} {x y z : V} include K open submodule /- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class (instead of a data containing type class) -/ section set_option class.instance_max_depth 36 lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) := begin simp [mem_span_insert], rintro a z hz rfl h, refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩, have a0 : a ≠ 0, {rintro rfl, simp * at *}, simp [a0, smul_add, smul_smul] end end lemma linear_independent_iff_not_mem_span : linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) := begin apply linear_independent_iff_not_smul_mem_span.trans, split, { intros h i h_in_span, apply one_ne_zero (h i 1 (by simp [h_in_span])) }, { intros h i a ha, by_contradiction ha', exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) } end lemma linear_independent_unique [unique ι] (h : v (default ι) ≠ 0): linear_independent K v := begin rw linear_independent_iff, intros l hl, ext i, rw [unique.eq_default i, finsupp.zero_apply], by_contra hc, have := smul_smul (l (default ι))⁻¹ (l (default ι)) (v (default ι)), rw [finsupp.unique_single l, finsupp.total_single] at hl, rw [hl, inv_mul_cancel hc, smul_zero, one_smul] at this, exact h this.symm end lemma linear_independent_singleton {x : V} (hx : x ≠ 0) : linear_independent K (λ x, x : ({x} : set V) → V) := begin apply @linear_independent_unique _ _ _ _ _ _ _ _ _, apply set.unique_singleton, apply hx, end lemma disjoint_span_singleton {p : submodule K V} {x : V} (x0 : x ≠ 0) : disjoint p (span K {x}) ↔ x ∉ p := ⟨λ H xp, x0 (disjoint_def.1 H _ xp (singleton_subset_iff.1 subset_span:_)), begin simp [disjoint_def, mem_span_singleton], rintro xp y yp a rfl, by_cases a0 : a = 0, {simp [a0]}, exact xp.elim ((smul_mem_iff p a0).1 yp), end⟩ lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) : linear_independent K (λ b, b : insert x s → V) := begin rw ← union_singleton, have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx, apply linear_independent_union hs (linear_independent_singleton x0), rwa [disjoint_span_singleton x0] end lemma exists_linear_independent (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : ∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (λ x, x : b → V) := begin rcases zorn.zorn_subset₀ {b | b ⊆ t ∧ linear_independent K (λ x, x : b → V)} _ _ ⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩, { refine ⟨b, bt, sb, λ x xt, _, bi⟩, haveI := classical.dec (x ∈ span K b), by_contra hn, apply hn, rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _), exact subset_span (mem_insert _ _) }, { refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩, { exact sUnion_subset (λ x xc, (hc xc).1) }, { exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) }, { exact subset_sUnion_of_mem } } end lemma exists_subset_is_basis (hs : linear_independent K (λ x, x : s → V)) : ∃b, s ⊆ b ∧ is_basis K (λ i : b, i.val) := let ⟨b, hb₀, hx, hb₂, hb₃⟩ := exists_linear_independent hs (@subset_univ _ _) in ⟨ b, hx, @linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ hb₃, by simp; exact eq_top_iff.2 hb₂⟩ variables (K V) lemma exists_is_basis : ∃b : set V, is_basis K (λ i : b, i.val) := let ⟨b, _, hb⟩ := exists_subset_is_basis linear_independent_empty in ⟨b, hb⟩ variables {K V} -- TODO(Mario): rewrite? lemma exists_of_linear_independent_of_finite_span {t : finset V} (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) : ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card := have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) → ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card := assume t, finset.induction_on t (assume s' hs' _ hss', have s = ↑s', from eq_of_linear_independent_of_span_subtype (@zero_ne_one K _) hs hs' $ by simpa using hss', ⟨s', by simp [this]⟩) (assume b₁ t hb₁t ih s' hs' hst hss', have hb₁s : b₁ ∉ s, from assume h, have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩, by rwa [hst] at this, have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h, have hst : s ∩ ↑t = ∅, from eq_empty_of_subset_empty $ subset.trans (by simp [inter_subset_inter, subset.refl]) (le_of_eq hst), classical.by_cases (assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t, ⟨insert b₁ u, by simp [insert_subset_insert hust], subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩) (assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h, have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from assume b₃ hb₃, have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V), by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl, subset_union_right], have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)), from span_mono this (hss' hb₃), have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V), by simpa [insert_eq, -singleton_union, -union_singleton] using hss', have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)), from mem_span_insert_exchange (this hb₂s) hb₂t, by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃, let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in ⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]), hsu, by rw [finset.union_comm] at hb₂t'; simp [eq, hb₂t', hb₁t, hb₁s']⟩)), begin letI := classical.dec_pred (λx, x ∈ s), have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t, { apply finset.ext.mpr, intro x, by_cases x ∈ s; simp *, finish }, apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s)) (by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])), intros u h, exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}), h.2.1, by simp only [h.2.2, eq]⟩ end lemma exists_finite_card_le_of_finite_of_linear_independent_of_span (ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) : ∃h : finite s, h.to_finset.card ≤ ht.to_finset.card := have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption, let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in have finite s, from finite_subset u.finite_to_set hsu, ⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩ lemma exists_left_inverse_linear_map_of_injective {f : V →ₗ[K] V'} (hf_inj : f.ker = ⊥) : ∃g:V' →ₗ V, g.comp f = linear_map.id := begin rcases exists_is_basis K V with ⟨B, hB⟩, have hB₀ : _ := hB.1.to_subtype_range, have : linear_independent K (λ x, x : f '' B → V'), { have h₁ := hB₀.image_subtype (show disjoint (span K (range (λ i : B, i.val))) (linear_map.ker f), by simp [hf_inj]), have h₂ : range (λ (i : B), i.val) = B := subtype.range_val B, rwa h₂ at h₁ }, rcases exists_subset_is_basis this with ⟨C, BC, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (function.restrict (inv_fun f) C : C → V), apply @is_basis.ext _ _ _ _ _ _ _ _ _ _ _ _ hB, intros b, rw image_subset_iff at BC, simp, have := BC (subtype.mem b), rw mem_preimage at this, have : f (b.val) = (subtype.mk (f ↑b) (begin rw ←mem_preimage, exact BC (subtype.mem b) end) : C).val, by simp; unfold_coes, rw this, rw [constr_basis hC], exact left_inverse_inv_fun (linear_map.ker_eq_bot.1 hf_inj) _, end lemma exists_right_inverse_linear_map_of_surjective {f : V →ₗ[K] V'} (hf_surj : f.range = ⊤) : ∃g:V' →ₗ V, f.comp g = linear_map.id := begin rcases exists_is_basis K V' with ⟨C, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (function.restrict (inv_fun f) C : C → V), apply @is_basis.ext _ _ _ _ _ _ _ _ _ _ _ _ hC, intros c, simp [constr_basis hC], exact right_inverse_inv_fun (linear_map.range_eq_top.1 hf_surj) _ end set_option class.instance_max_depth 49 open submodule linear_map theorem quotient_prod_linear_equiv (p : submodule K V) : nonempty ((p.quotient × p) ≃ₗ[K] V) := begin haveI := classical.dec_eq (quotient p), rcases exists_right_inverse_linear_map_of_surjective p.range_mkq with ⟨f, hf⟩, have mkf : ∀ x, submodule.quotient.mk (f x) = x := linear_map.ext_iff.1 hf, have fp : ∀ x, x - f (p.mkq x) ∈ p := λ x, (submodule.quotient.eq p).1 (mkf (p.mkq x)).symm, refine ⟨linear_equiv.of_linear (f.copair p.subtype) (p.mkq.pair (cod_restrict p (linear_map.id - f.comp p.mkq) fp)) (by ext; simp) _⟩, ext ⟨⟨x⟩, y, hy⟩; simp, { apply (submodule.quotient.eq p).2, simpa using sub_mem p hy (fp x) }, { refine subtype.coe_ext.2 _, simp [mkf, (submodule.quotient.mk_eq_zero p).2 hy] } end open fintype theorem vector_space.card_fintype [fintype K] [fintype V] : ∃ n : ℕ, card V = (card K) ^ n := begin apply exists.elim (exists_is_basis K V), intros b hb, haveI := classical.dec_pred (λ x, x ∈ b), use card b, exact module.card_fintype hb, end end vector_space namespace pi open set linear_map section module variables {η : Type*} {ιs : η → Type*} {Ms : η → Type*} variables [ring R] [∀i, add_comm_group (Ms i)] [∀i, module R (Ms i)] [fintype η] lemma linear_independent_std_basis (v : Πj, ιs j → (Ms j)) (hs : ∀i, linear_independent R (v i)) : linear_independent R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (v ji.1 ji.2)) := begin have hs' : ∀j : η, linear_independent R (λ i : ιs j, std_basis R Ms j (v j i)), { intro j, apply linear_independent.image (hs j), simp [ker_std_basis] }, apply linear_independent_Union_finite hs', { assume j J _ hiJ, simp [(set.Union.equations._eqn_1 _).symm, submodule.span_image, submodule.span_Union], have h₀ : ∀ j, span R (range (λ (i : ιs j), std_basis R Ms j (v j i))) ≤ range (std_basis R Ms j), { intro j, rw [span_le, linear_map.range_coe], apply range_comp_subset_range }, have h₁ : span R (range (λ (i : ιs j), std_basis R Ms j (v j i))) ≤ ⨆ i ∈ {j}, range (std_basis R Ms i), { rw @supr_singleton _ _ _ (λ i, linear_map.range (std_basis R (λ (j : η), Ms j) i)), apply h₀ }, have h₂ : (⨆ j ∈ J, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))) ≤ ⨆ j ∈ J, range (std_basis R (λ (j : η), Ms j) j) := supr_le_supr (λ i, supr_le_supr (λ H, h₀ i)), have h₃ : disjoint (λ (i : η), i ∈ {j}) J, { convert set.disjoint_singleton_left.2 hiJ, rw ←@set_of_mem_eq _ {j}, refl }, refine disjoint_mono h₁ h₂ (disjoint_std_basis_std_basis _ _ _ _ h₃), } end lemma is_basis_std_basis (s : Πj, ιs j → (Ms j)) (hs : ∀j, is_basis R (s j)) : is_basis R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (s ji.1 ji.2)) := begin split, { apply linear_independent_std_basis _ (assume i, (hs i).1) }, have h₁ : Union (λ j, set.range (std_basis R Ms j ∘ s j)) ⊆ range (λ (ji : Σ (j : η), ιs j), (std_basis R Ms (ji.fst)) (s (ji.fst) (ji.snd))), { apply Union_subset, intro i, apply range_comp_subset_range (λ x : ιs i, (⟨i, x⟩ : Σ (j : η), ιs j)) (λ (ji : Σ (j : η), ιs j), std_basis R Ms (ji.fst) (s (ji.fst) (ji.snd))) }, have h₂ : ∀ i, span R (range (std_basis R Ms i ∘ s i)) = range (std_basis R Ms i), { intro i, rw [set.range_comp, submodule.span_image, (assume i, (hs i).2), submodule.map_top] }, apply eq_top_mono, apply span_mono h₁, rw span_Union, simp only [h₂], apply supr_range_std_basis end section variables (R η) lemma is_basis_fun₀ : is_basis R (λ (ji : Σ (j : η), (λ _, unit) j), (std_basis R (λ (i : η), R) (ji.fst)) 1) := begin haveI := classical.dec_eq, apply @is_basis_std_basis R η (λi:η, unit) (λi:η, R) _ _ _ _ (λ _ _, (1 : R)) (assume i, @is_basis_singleton_one _ _ _ _), end lemma is_basis_fun : is_basis R (λ i, std_basis R (λi:η, R) i 1) := begin apply is_basis.comp (is_basis_fun₀ R η) (λ i, ⟨i, punit.star⟩), apply bijective_iff_has_inverse.2, use (λ x, x.1), simp [function.left_inverse, function.right_inverse], intros _ b, rw [unique.eq_default b, unique.eq_default punit.star] end end end module end pi
f08cff2b6914a9d829b7f2bac5bc7a1eb3b62bf1
7cef822f3b952965621309e88eadf618da0c8ae9
/src/group_theory/order_of_element.lean
be1c2dab952f9d89211caa41c0a2508a9f692f99
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
22,692
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.set.finite group_theory.coset data.nat.totient open function variables {α : Type*} {s : set α} {a a₁ a₂ b c: α} -- TODO this lemma isn't used anywhere in this file, and should be moved elsewhere. namespace finset open finset lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) end finset lemma conj_inj [group α] {x : α} : function.injective (λ (g : α), x * g * x⁻¹) := λ a b h, by simpa [mul_left_inj, mul_right_inj] using h lemma mem_normalizer_fintype [group α] {s : set α} [fintype s] {x : α} (h : ∀ n, n ∈ s → x * n * x⁻¹ ∈ s) : x ∈ is_subgroup.normalizer s := by haveI := classical.prop_decidable; haveI := set.fintype_image s (λ n, x * n * x⁻¹); exact λ n, ⟨h n, λ h₁, have heq : (λ n, x * n * x⁻¹) '' s = s := set.eq_of_subset_of_card_le (λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective s conj_inj), have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' s := heq.symm ▸ h₁, let ⟨y, hy⟩ := this in conj_inj hy.2 ▸ hy.1⟩ section order_of variables [group α] [fintype α] [decidable_eq α] open quotient_group set instance quotient_group.fintype (s : set α) [is_subgroup s] [d : decidable_pred s] : fintype (quotient s) := @quotient.fintype _ _ (left_rel s) (λ _ _, d _) lemma card_eq_card_quotient_mul_card_subgroup (s : set α) [hs : is_subgroup s] [fintype s] [decidable_pred s] : fintype.card α = fintype.card (quotient s) * fintype.card s := by rw ← fintype.card_prod; exact fintype.card_congr (is_subgroup.group_equiv_quotient_times_subgroup hs) lemma card_subgroup_dvd_card (s : set α) [is_subgroup s] [fintype s] : fintype.card s ∣ fintype.card α := by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s] lemma card_quotient_dvd_card (s : set α) [is_subgroup s] [decidable_pred s] [fintype s] : fintype.card (quotient s) ∣ fintype.card α := by simp [card_eq_card_quotient_mul_card_subgroup s] @[simp] lemma card_trivial [fintype (is_subgroup.trivial α)] : fintype.card (is_subgroup.trivial α) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : α), by simp⟩, λ ⟨y, hy⟩, subtype.eq $ is_subgroup.mem_trivial.1 hy⟩ lemma exists_gpow_eq_one (a : α) : ∃i≠0, a ^ (i:ℤ) = 1 := have ¬ injective (λi, a ^ i), from not_injective_int_fintype, let ⟨i, j, a_eq, ne⟩ := show ∃(i j : ℤ), a ^ i = a ^ j ∧ i ≠ j, by rw [injective] at this; simpa [classical.not_forall] in have a ^ (i - j) = 1, by simp [gpow_add, gpow_neg, a_eq], ⟨i - j, sub_ne_zero.mpr ne, this⟩ lemma exists_pow_eq_one (a : α) : ∃i > 0, a ^ i = 1 := let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in begin cases i, { exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ }, { exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ } end /-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/ def order_of (a : α) : ℕ := nat.find (exists_pow_eq_one a) lemma pow_order_of_eq_one (a : α) : a ^ order_of a = 1 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₂ lemma order_of_pos (a : α) : order_of a > 0 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₁ private lemma pow_injective_aux {n m : ℕ} (a : α) (h : n ≤ m) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := decidable.by_contradiction $ assume ne : n ≠ m, have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]), have h₂ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq], have le : order_of a ≤ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, h₂⟩, have lt : m - n < order_of a, from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm, lt_irrefl _ (lt_of_le_of_lt le lt) lemma pow_injective_of_lt_order_of {n m : ℕ} (a : α) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := (le_total n m).elim (assume h, pow_injective_aux a h hn hm eq) (assume h, (pow_injective_aux a h hm hn eq.symm).symm) lemma order_of_le_card_univ : order_of a ≤ fintype.card α := finset.card_le_of_inj_on ((^) a) (assume n _, fintype.complete _) (assume i j, pow_injective_of_lt_order_of a) lemma pow_eq_mod_order_of {n : ℕ} : a ^ n = a ^ (n % order_of a) := calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) : by rw [nat.mod_add_div] ... = a ^ (n % order_of a) : by simp [pow_add, pow_mul, pow_order_of_eq_one] lemma gpow_eq_mod_order_of {i : ℤ} : a ^ i = a ^ (i % order_of a) := calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) : by rw [int.mod_add_div] ... = a ^ (i % order_of a) : by simp [gpow_add, gpow_mul, pow_order_of_eq_one] lemma mem_gpowers_iff_mem_range_order_of {a a' : α} : a' ∈ gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : ℕ → α) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) (assume i, gpow_eq_mod_order_of.symm) instance decidable_gpowers : decidable_pred (gpowers a) := assume a', decidable_of_iff' (a' ∈ (finset.range (order_of a)).image ((^) a)) mem_gpowers_iff_mem_range_order_of lemma order_of_dvd_of_pow_eq_one {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n := by_contradiction (λ h₁, nat.find_min _ (show n % order_of a < order_of a, from nat.mod_lt _ (order_of_pos _)) ⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩) lemma order_of_le_of_pow_eq_one {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n := nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩ lemma sum_card_order_of_eq_card_pow_eq_one {n : ℕ} (hn : 0 < n) : ((finset.range n.succ).filter (∣ n)).sum (λ m, (finset.univ.filter (λ a : α, order_of a = m)).card) = (finset.univ.filter (λ a : α, a ^ n = 1)).card := calc ((finset.range n.succ).filter (∣ n)).sum (λ m, (finset.univ.filter (λ a : α, order_of a = m)).card) = _ : (finset.card_bind (by { intros, apply finset.disjoint_filter.2, cc })).symm ... = _ : congr_arg finset.card (finset.ext.2 (begin assume a, suffices : order_of a ≤ n ∧ order_of a ∣ n ↔ a ^ n = 1, { simpa [nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) section local attribute [instance] set_fintype lemma order_eq_card_gpowers : order_of a = fintype.card (gpowers a) := begin refine (finset.card_eq_of_bijective _ _ _ _).symm, { exact λn hn, ⟨gpow a n, ⟨n, rfl⟩⟩ }, { exact assume ⟨_, i, rfl⟩ _, have pos: (0:int) < order_of a, from int.coe_nat_lt.mpr $ order_of_pos a, have 0 ≤ i % (order_of a), from int.mod_nonneg _ $ ne_of_gt pos, ⟨int.to_nat (i % order_of a), by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ }, { intros, exact finset.mem_univ _ }, { exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq } end @[simp] lemma order_of_one : order_of (1 : α) = 1 := by rw [order_eq_card_gpowers, fintype.card_eq_one_iff]; exact ⟨⟨1, 0, rfl⟩, λ ⟨a, i, ha⟩, by simp [ha.symm]⟩ @[simp] lemma order_of_eq_one_iff : order_of a = 1 ↔ a = 1 := ⟨λ h, by conv { to_lhs, rw [← pow_one a, ← h, pow_order_of_eq_one] }, λ h, by simp [h]⟩ section classical open_locale classical open quotient_group /- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/ lemma order_of_dvd_card_univ : order_of a ∣ fintype.card α := have ft_prod : fintype (quotient (gpowers a) × (gpowers a)), from fintype.of_equiv α (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup, have ft_s : fintype (gpowers a), from @fintype.fintype_prod_right _ _ _ ft_prod _, have ft_cosets : fintype (quotient (gpowers a)), from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, is_submonoid.one_mem (gpowers a)⟩⟩, have ft : fintype (quotient (gpowers a) × (gpowers a)), from @prod.fintype _ _ ft_cosets ft_s, have eq₁ : fintype.card α = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card α = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eq₂ : order_of a = @fintype.card _ ft_s, from calc order_of a = _ : order_eq_card_gpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, dvd.intro (@fintype.card (quotient (gpowers a)) ft_cosets) $ by rw [eq₁, eq₂, mul_comm] end classical @[simp] lemma pow_card_eq_one (a : α) : a ^ fintype.card α = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] lemma powers_eq_gpowers (a : α) : powers a = gpowers a := set.ext (λ x, ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, λ ⟨i, hi⟩, ⟨(i % order_of a).nat_abs, by rwa [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of]⟩⟩) open nat lemma order_of_pow (a : α) (n : ℕ) : order_of (a ^ n) = order_of a / gcd (order_of a) n := dvd_antisymm (order_of_dvd_of_pow_eq_one (by rw [← pow_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), pow_mul, pow_order_of_eq_one, _root_.one_pow])) (have gcd_pos : 0 < gcd (order_of a) n, from gcd_pos_of_pos_left n (order_of_pos a), have hdvd : order_of a ∣ n * order_of (a ^ n), from order_of_dvd_of_pow_eq_one (by rw [pow_mul, pow_order_of_eq_one]), coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd gcd_pos) (dvd_of_mul_dvd_mul_right gcd_pos (by rwa [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm]))) lemma pow_gcd_card_eq_one_iff {n : ℕ} {a : α} : a ^ n = 1 ↔ a ^ (gcd n (fintype.card α)) = 1 := ⟨λ h, have hn : order_of a ∣ n, from dvd_of_mod_eq_zero $ by_contradiction (λ ha, by rw pow_eq_mod_order_of at h; exact (not_le_of_gt (nat.mod_lt n (order_of_pos a))) (order_of_le_of_pow_eq_one (nat.pos_of_ne_zero ha) h)), let ⟨m, hm⟩ := dvd_gcd hn order_of_dvd_card_univ in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card α) in by rw [hm, pow_mul, h, _root_.one_pow]⟩ end end order_of section cyclic local attribute [instance] set_fintype class is_cyclic (α : Type*) [group α] : Prop := (exists_generator : ∃ g : α, ∀ x, x ∈ gpowers g) def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α := { mul_comm := λ x y, show x * y = y * x, from let ⟨g, hg⟩ := is_cyclic.exists_generator α in let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in hm ▸ hn ▸ gpow_mul_comm _ _ _, ..hg } lemma is_cyclic_of_order_of_eq_card [group α] [fintype α] [decidable_eq α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := ⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le (set.subset_univ _) (by rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers])⟩⟩ lemma order_of_eq_card_of_forall_mem_gpowers [group α] [fintype α] [decidable_eq α] {g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α := by rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers]; simp [hx]; congr instance [group α] : is_cyclic (is_subgroup.trivial α) := ⟨⟨(1 : is_subgroup.trivial α), λ x, ⟨0, subtype.eq $ eq.symm (is_subgroup.mem_trivial.1 x.2)⟩⟩⟩ instance is_subgroup.is_cyclic [group α] [is_cyclic α] (H : set α) [is_subgroup H] : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator α in if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx in let ⟨k, hk⟩ := hg x in have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]), match k, hk with | (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← is_subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex), from ⟨k / nat.find hex, eq.symm $ gpow_mul _ _ _⟩, have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H, by rw gpow_mul; exact is_subgroup.gpow_mem (nat.find_spec hex).2, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (is_subgroup.mul_mem_cancel_left H hk₂).1 $ by rw [← gpow_add, int.mod_add_div, hk]; exact hx, have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← gpow_coe_nat, ← hk₄], have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0, from by_contradiction (λ h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hk₅⟩), ⟨k / (nat.find hex : ℤ), subtype.coe_ext.2 begin suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x, { simpa [gpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = is_subgroup.trivial α, from set.ext $ λ x, ⟨λ h, by simp at *; tauto, λ h, by rw [is_subgroup.mem_trivial.1 h]; exact is_submonoid.one_mem _⟩, by clear _let_match; subst this; apply_instance open finset nat lemma is_cyclic.card_pow_eq_one_le [group α] [fintype α] [decidable_eq α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n := let ⟨g, hg⟩ := is_cyclic.exists_generator α in calc (univ.filter (λ a : α, a ^ n = 1)).card ≤ (gpowers (g ^ (fintype.card α / (gcd n (fintype.card α))))).to_finset.card : card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ powers g, from (powers_eq_gpowers g).symm ▸ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card α / (gcd n (fintype.card α))) : ℕ), have hgmn : g ^ (m * gcd n (fintype.card α)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _, conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]}, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≤ n : let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card α) in have hm0 : 0 < m, from nat.pos_of_ne_zero (λ hm0, (by rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm; exact hm 1)), begin rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_gpowers, order_of_pow, order_of_eq_card_of_forall_mem_gpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (gcd_dvd_left _ _) end section totient variables [group α] [fintype α] [decidable_eq α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n) include hn lemma card_pow_eq_one_eq_order_of_aux (a : α) : (finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos _)) (calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers ... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (fintype.of_finset _ (λ _, iff.rfl)) : @fintype.card_le_of_injective (gpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat, pow_order_of_eq_one, one_gpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use φ for nat.totient private lemma card_order_of_eq_totient_aux₁ : ∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card → (univ.filter (λ a : α, order_of a = d)).card = φ d | 0 := λ hd hd0, absurd hd0 (mt card_pos.1 (by simp [finset.ext, nat.pos_iff_ne_zero.1 (order_of_pos _)])) | (d+1) := λ hd hd0, let ⟨a, ha⟩ := exists_mem_of_ne_empty (card_pos.1 hd0) in have ha : order_of a = d.succ, from (mem_filter.1 ha).2, have h : ((range d.succ).filter (∣ d.succ)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) = ((range d.succ).filter (∣ d.succ)).sum φ, from finset.sum_congr rfl (λ m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1, have hm : m ∣ d.succ, from (mem_filter.1 hm).2, card_order_of_eq_totient_aux₁ (dvd.trans hm hd) (finset.card_pos.2 (ne_empty_of_mem (show a ^ (d.succ / m) ∈ _, from mem_filter.2 ⟨mem_univ _, by rw [order_of_pow, ha, gcd_eq_right (div_dvd_of_dvd hm), nat.div_div_self hm (succ_pos _)]⟩)))), have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ)) = (range d.succ.succ).filter (∣ d.succ), from (finset.ext.2 $ λ x, ⟨λ h, (mem_insert.1 h).elim (λ h, by simp [h, range_succ]) (by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩), have hinsert₁ : d.succ ∉ (range d.succ).filter (∣ d.succ), by simp [mem_range, zero_le_one, le_succ], (add_right_inj (((range d.succ).filter (∣ d.succ)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card))).1 (calc _ = (insert d.succ (filter (∣ d.succ) (range d.succ))).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ])) ... = ((range d.succ.succ).filter (∣ d.succ)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : sum_congr hinsert (λ _ _, rfl) ... = (univ.filter (λ a : α, a ^ d.succ = 1)).card : sum_card_order_of_eq_card_pow_eq_one (succ_pos d) ... = ((range d.succ.succ).filter (∣ d.succ)).sum φ : ha ▸ (card_pow_eq_one_eq_order_of_aux hn a).symm ▸ (sum_totient _).symm ... = _ : by rw [h, ← sum_insert hinsert₁]; exact finset.sum_congr hinsert.symm (λ _ _, rfl)) lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = φ d := by_contradiction $ λ h, have h0 : (univ.filter (λ a : α , order_of a = d)).card = 0 := not_not.1 (mt nat.pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)), let c := fintype.card α in have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩, lt_irrefl c $ calc c = (univ.filter (λ a : α, a ^ c = 1)).card : congr_arg card $ by simp [finset.ext, c] ... = ((range c.succ).filter (∣ c)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : (sum_card_order_of_eq_card_pow_eq_one hc0).symm ... = (((range c.succ).filter (∣ c)).erase d).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : eq.symm (sum_subset (erase_subset _ _) (λ m hm₁ hm₂, have m = d, by simp at *; cc, by simp [*, finset.ext] at *; exact h0)) ... ≤ (((range c.succ).filter (∣ c)).erase d).sum φ : sum_le_sum (λ m hm, have hmc : m ∣ c, by simp at hm; tauto, (imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim (λ h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le]) (by simp [le_refl] {contextual := tt})) ... < φ d + (((range c.succ).filter (∣ c)).erase d).sum φ : lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero (λ h, nat.pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h ▸ hd)))) ... = (insert d (((range c.succ).filter (∣ c)).erase d)).sum φ : eq.symm (sum_insert (by simp)) ... = ((range c.succ).filter (∣ c)).sum φ : finset.sum_congr (finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (λ _ _, rfl) ... = c : sum_totient _ lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α := have ∃ x, x ∈ univ.filter (λ a : α, order_of a = fintype.card α), from exists_mem_of_ne_empty (card_pos.1 $ by rw [card_order_of_eq_totient_aux₂ hn (dvd_refl _)]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 end totient lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [fintype α] [decidable_eq α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d := card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd end cyclic
a32fe6d5618f17a832a8ea3ddbbf1f8b01be5f98
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/chain.lean
f17c14e8058678e9208a0e7e44e45ee83140fba5
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,143
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Mario Carneiro -/ import tactic.ext open interactive namespace tactic /- This file defines a `chain` tactic, which takes a list of tactics, and exhaustively tries to apply them to the goals, until no tactic succeeds on any goal. Along the way, it generates auxiliary declarations, in order to speed up elaboration time of the resulting (sometimes long!) proofs. This tactic is used by the `tidy` tactic. -/ -- α is the return type of our tactics. When `chain` is called by `tidy`, this is string, -- describing what that tactic did as an interactive tactic. variable {α : Type} inductive tactic_script (α : Type) : Type | base : α → tactic_script | work (index : ℕ) (first : α) (later : list tactic_script) (closed : bool) : tactic_script meta def tactic_script.to_string : tactic_script string → string | (tactic_script.base a) := a | (tactic_script.work n a l c) := "work_on_goal " ++ (to_string n) ++ " { " ++ (", ".intercalate (a :: l.map tactic_script.to_string)) ++ " }" meta instance : has_to_string (tactic_script string) := { to_string := λ s, s.to_string } meta instance tactic_script_unit_has_to_string : has_to_string (tactic_script unit) := { to_string := λ s, "[chain tactic]" } meta def abstract_if_success (tac : expr → tactic α) (g : expr) : tactic α := do type ← infer_type g, is_lemma ← is_prop type, if is_lemma then -- there's no point making the abstraction, and indeed it's slower tac g else do m ← mk_meta_var type, a ← tac m, do { val ← instantiate_mvars m, guard (val.list_meta_vars = []), c ← new_aux_decl_name, gs ← get_goals, set_goals [g], add_aux_decl c type val ff >>= unify g, set_goals gs } <|> unify m g, return a /-- `chain_many tac` recursively tries `tac` on all goals, working depth-first on generated subgoals, until it no longer succeeds on any goal. `chain_many` automatically makes auxiliary definitions. -/ meta mutual def chain_single, chain_many, chain_iter {α} (tac : tactic α) with chain_single : expr → tactic (α × list (tactic_script α)) | g := do set_goals [g], a ← tac, l ← get_goals >>= chain_many, return (a, l) with chain_many : list expr → tactic (list (tactic_script α)) | [] := return [] | [g] := do { (a, l) ← chain_single g, return (tactic_script.base a :: l) } <|> return [] | gs := chain_iter gs [] with chain_iter : list expr → list expr → tactic (list (tactic_script α)) | [] _ := return [] | (g :: later_goals) stuck_goals := do { (a, l) ← abstract_if_success chain_single g, new_goals ← get_goals, let w := tactic_script.work stuck_goals.length a l (new_goals = []), let current_goals := stuck_goals.reverse ++ new_goals ++ later_goals, set_goals current_goals, -- we keep the goals up to date, so they are correct at the end l' ← chain_many current_goals, return (w :: l') } <|> chain_iter later_goals (g :: stuck_goals) meta def chain_core {α : Type} [has_to_string (tactic_script α)] (tactics : list (tactic α)) : tactic (list string) := do results ← (get_goals >>= chain_many (first tactics)), when results.empty (fail "`chain` tactic made no progress"), return (results.map to_string) variables [has_to_string (tactic_script α)] [has_to_format α] declare_trace chain meta def trace_output (t : tactic α) : tactic α := do tgt ← target, r ← t, name ← decl_name, trace format!"`chain` successfully applied a tactic during elaboration of {name}:", tgt ← pp tgt, trace format!"previous target: {tgt}", trace format!"tactic result: {r}", tgt ← try_core target, tgt ← match tgt with | (some tgt) := pp tgt | none := return "no goals" end, trace format!"new target: {tgt}", pure r meta def chain (tactics : list (tactic α)) : tactic (list string) := chain_core (if is_trace_enabled_for `chain then (tactics.map trace_output) else tactics) end tactic
dcfd42c3663779422e5e93cf0490af4e38103238
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/monoidal/category.lean
638289fbe4d245c8dc76778944f48115ddeb6965
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
21,020
lean
/- Copyright (c) 2018 Michael Jendrusch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Jendrusch, Scott Morrison -/ import category_theory.products.basic /-! # Monoidal categories A monoidal category is a category equipped with a tensor product, unitors, and an associator. In the definition, we provide the tensor product as a pair of functions * `tensor_obj : C → C → C` * `tensor_hom : (X₁ ⟶ Y₁) → (X₂ ⟶ Y₂) → ((X₁ ⊗ X₂) ⟶ (Y₁ ⊗ Y₂))` and allow use of the overloaded notation `⊗` for both. The unitors and associator are provided componentwise. The tensor product can be expressed as a functor via `tensor : C × C ⥤ C`. The unitors and associator are gathered together as natural isomorphisms in `left_unitor_nat_iso`, `right_unitor_nat_iso` and `associator_nat_iso`. Some consequences of the definition are proved in other files, e.g. `(λ_ (𝟙_ C)).hom = (ρ_ (𝟙_ C)).hom` in `category_theory.monoidal.unitors_equal`. ## Implementation Dealing with unitors and associators is painful, and at this stage we do not have a useful implementation of coherence for monoidal categories. In an effort to lessen the pain, we put some effort into choosing the right `simp` lemmas. Generally, the rule is that the component index of a natural transformation "weighs more" in considering the complexity of an expression than does a structural isomorphism (associator, etc). As an example when we prove Proposition 2.2.4 of <http://www-math.mit.edu/~etingof/egnobookfinal.pdf> we state it as a `@[simp]` lemma as ``` (λ_ (X ⊗ Y)).hom = (α_ (𝟙_ C) X Y).inv ≫ (λ_ X).hom ⊗ (𝟙 Y) ``` This is far from completely effective, but seems to prove a useful principle. ## References * Tensor categories, Etingof, Gelaki, Nikshych, Ostrik, http://www-math.mit.edu/~etingof/egnobookfinal.pdf * https://stacks.math.columbia.edu/tag/0FFK. -/ open category_theory universes v u open category_theory open category_theory.category open category_theory.iso namespace category_theory /-- In a monoidal category, we can take the tensor product of objects, `X ⊗ Y` and of morphisms `f ⊗ g`. Tensor product does not need to be strictly associative on objects, but there is a specified associator, `α_ X Y Z : (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z)`. There is a tensor unit `𝟙_ C`, with specified left and right unitor isomorphisms `λ_ X : 𝟙_ C ⊗ X ≅ X` and `ρ_ X : X ⊗ 𝟙_ C ≅ X`. These associators and unitors satisfy the pentagon and triangle equations. See https://stacks.math.columbia.edu/tag/0FFK. -/ class monoidal_category (C : Type u) [𝒞 : category.{v} C] := -- curried tensor product of objects: (tensor_obj : C → C → C) (infixr ` ⊗ `:70 := tensor_obj) -- This notation is only temporary -- curried tensor product of morphisms: (tensor_hom : Π {X₁ Y₁ X₂ Y₂ : C}, (X₁ ⟶ Y₁) → (X₂ ⟶ Y₂) → ((X₁ ⊗ X₂) ⟶ (Y₁ ⊗ Y₂))) (infixr ` ⊗' `:69 := tensor_hom) -- This notation is only temporary -- tensor product laws: (tensor_id' : ∀ (X₁ X₂ : C), (𝟙 X₁) ⊗' (𝟙 X₂) = 𝟙 (X₁ ⊗ X₂) . obviously) (tensor_comp' : ∀ {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : Y₁ ⟶ Z₁) (g₂ : Y₂ ⟶ Z₂), (f₁ ≫ g₁) ⊗' (f₂ ≫ g₂) = (f₁ ⊗' f₂) ≫ (g₁ ⊗' g₂) . obviously) -- tensor unit: (tensor_unit [] : C) (notation `𝟙_` := tensor_unit) -- associator: (associator : Π X Y Z : C, (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z)) (notation `α_` := associator) (associator_naturality' : ∀ {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃), ((f₁ ⊗' f₂) ⊗' f₃) ≫ (α_ Y₁ Y₂ Y₃).hom = (α_ X₁ X₂ X₃).hom ≫ (f₁ ⊗' (f₂ ⊗' f₃)) . obviously) -- left unitor: (left_unitor : Π X : C, 𝟙_ ⊗ X ≅ X) (notation `λ_` := left_unitor) (left_unitor_naturality' : ∀ {X Y : C} (f : X ⟶ Y), ((𝟙 𝟙_) ⊗' f) ≫ (λ_ Y).hom = (λ_ X).hom ≫ f . obviously) -- right unitor: (right_unitor : Π X : C, X ⊗ 𝟙_ ≅ X) (notation `ρ_` := right_unitor) (right_unitor_naturality' : ∀ {X Y : C} (f : X ⟶ Y), (f ⊗' (𝟙 𝟙_)) ≫ (ρ_ Y).hom = (ρ_ X).hom ≫ f . obviously) -- pentagon identity: (pentagon' : ∀ W X Y Z : C, ((α_ W X Y).hom ⊗' (𝟙 Z)) ≫ (α_ W (X ⊗ Y) Z).hom ≫ ((𝟙 W) ⊗' (α_ X Y Z).hom) = (α_ (W ⊗ X) Y Z).hom ≫ (α_ W X (Y ⊗ Z)).hom . obviously) -- triangle identity: (triangle' : ∀ X Y : C, (α_ X 𝟙_ Y).hom ≫ ((𝟙 X) ⊗' (λ_ Y).hom) = (ρ_ X).hom ⊗' (𝟙 Y) . obviously) restate_axiom monoidal_category.tensor_id' attribute [simp] monoidal_category.tensor_id restate_axiom monoidal_category.tensor_comp' attribute [reassoc] monoidal_category.tensor_comp -- This would be redundant in the simp set. attribute [simp] monoidal_category.tensor_comp restate_axiom monoidal_category.associator_naturality' attribute [reassoc] monoidal_category.associator_naturality restate_axiom monoidal_category.left_unitor_naturality' attribute [reassoc] monoidal_category.left_unitor_naturality restate_axiom monoidal_category.right_unitor_naturality' attribute [reassoc] monoidal_category.right_unitor_naturality restate_axiom monoidal_category.pentagon' restate_axiom monoidal_category.triangle' attribute [simp, reassoc] monoidal_category.triangle open monoidal_category infixr ` ⊗ `:70 := tensor_obj infixr ` ⊗ `:70 := tensor_hom notation `𝟙_` := tensor_unit notation `α_` := associator notation `λ_` := left_unitor notation `ρ_` := right_unitor /-- The tensor product of two isomorphisms is an isomorphism. -/ @[simps] def tensor_iso {C : Type u} {X Y X' Y' : C} [category.{v} C] [monoidal_category.{v} C] (f : X ≅ Y) (g : X' ≅ Y') : X ⊗ X' ≅ Y ⊗ Y' := { hom := f.hom ⊗ g.hom, inv := f.inv ⊗ g.inv, hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id], inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] } infixr ` ⊗ `:70 := tensor_iso namespace monoidal_category section variables {C : Type u} [category.{v} C] [monoidal_category.{v} C] instance tensor_is_iso {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] : is_iso (f ⊗ g) := { ..(as_iso f ⊗ as_iso g) } @[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] : inv (f ⊗ g) = inv f ⊗ inv g := rfl variables {U V W X Y Z : C} -- When `rewrite_search` lands, add @[search] attributes to -- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality -- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality -- monoidal_category.pentagon monoidal_category.triangle -- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id -- triangle_assoc_comp_left triangle_assoc_comp_right -- triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv -- left_unitor_tensor left_unitor_tensor_inv -- right_unitor_tensor right_unitor_tensor_inv -- pentagon_inv -- associator_inv_naturality -- left_unitor_inv_naturality -- right_unitor_inv_naturality @[simp] lemma comp_tensor_id (f : W ⟶ X) (g : X ⟶ Y) : (f ≫ g) ⊗ (𝟙 Z) = (f ⊗ (𝟙 Z)) ≫ (g ⊗ (𝟙 Z)) := by { rw ←tensor_comp, simp } @[simp] lemma id_tensor_comp (f : W ⟶ X) (g : X ⟶ Y) : (𝟙 Z) ⊗ (f ≫ g) = (𝟙 Z ⊗ f) ≫ (𝟙 Z ⊗ g) := by { rw ←tensor_comp, simp } @[simp, reassoc] lemma id_tensor_comp_tensor_id (f : W ⟶ X) (g : Y ⟶ Z) : ((𝟙 Y) ⊗ f) ≫ (g ⊗ (𝟙 X)) = g ⊗ f := by { rw [←tensor_comp], simp } @[simp, reassoc] lemma tensor_id_comp_id_tensor (f : W ⟶ X) (g : Y ⟶ Z) : (g ⊗ (𝟙 W)) ≫ ((𝟙 Z) ⊗ f) = g ⊗ f := by { rw [←tensor_comp], simp } lemma left_unitor_inv_naturality {X X' : C} (f : X ⟶ X') : f ≫ (λ_ X').inv = (λ_ X).inv ≫ (𝟙 _ ⊗ f) := begin apply (cancel_mono (λ_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma right_unitor_inv_naturality {X X' : C} (f : X ⟶ X') : f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f ⊗ 𝟙 _) := begin apply (cancel_mono (ρ_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end @[simp] lemma right_unitor_conjugation {X Y : C} (f : X ⟶ Y) : (ρ_ X).inv ≫ (f ⊗ (𝟙 (𝟙_ C))) ≫ (ρ_ Y).hom = f := by rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma left_unitor_conjugation {X Y : C} (f : X ⟶ Y) : (λ_ X).inv ≫ ((𝟙 (𝟙_ C)) ⊗ f) ≫ (λ_ Y).hom = f := by rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma tensor_left_iff {X Y : C} (f g : X ⟶ Y) : ((𝟙 (𝟙_ C)) ⊗ f = (𝟙 (𝟙_ C)) ⊗ g) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (λ k, (λ_ _).inv ≫ k) h, dsimp at h', rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h', exact (cancel_mono _).1 h', }, { intro h, subst h, } end @[simp] lemma tensor_right_iff {X Y : C} (f g : X ⟶ Y) : (f ⊗ (𝟙 (𝟙_ C)) = g ⊗ (𝟙 (𝟙_ C))) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (λ k, (ρ_ _).inv ≫ k) h, dsimp at h', rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h', exact (cancel_mono _).1 h' }, { intro h, subst h, } end -- We now prove: -- ((α_ (𝟙_ C) X Y).hom) ≫ -- ((λ_ (X ⊗ Y)).hom) -- = ((λ_ X).hom ⊗ (𝟙 Y)) -- (and the corresponding fact for right unitors) -- following the proof on nLab: -- Lemma 2.2 at <https://ncatlab.org/nlab/revision/monoidal+category/115> lemma left_unitor_product_aux_perimeter (X Y : C) : ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫ (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom) = (((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y)) ≫ (α_ (𝟙_ C) X Y).hom := begin conv_lhs { congr, skip, rw [←category.assoc] }, rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id, ←monoidal_category.triangle, ←category.assoc] end lemma left_unitor_product_aux_triangle (X Y : C) : ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫ (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) = ((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y) := by rw [←comp_tensor_id, ←monoidal_category.triangle] lemma left_unitor_product_aux_square (X Y : C) : (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom ⊗ (𝟙 Y)) = (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) ≫ (α_ (𝟙_ C) X Y).hom := by rw associator_naturality lemma left_unitor_product_aux (X Y : C) : ((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom) = (𝟙 (𝟙_ C)) ⊗ ((λ_ X).hom ⊗ (𝟙 Y)) := begin rw ←(cancel_epi (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom), rw left_unitor_product_aux_square, rw ←(cancel_epi ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y))), slice_rhs 1 2 { rw left_unitor_product_aux_triangle }, conv_lhs { rw [left_unitor_product_aux_perimeter] } end lemma right_unitor_product_aux_perimeter (X Y : C) : ((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫ ((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom) = ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ X Y (𝟙_ C)).hom := begin transitivity (((α_ X Y _).hom ⊗ 𝟙 _) ≫ (α_ X _ _).hom ≫ (𝟙 X ⊗ (α_ Y _ _).hom)) ≫ (𝟙 X ⊗ 𝟙 Y ⊗ (λ_ _).hom), { conv_lhs { congr, skip, rw [←category.assoc] }, conv_rhs { rw [category.assoc] } }, { conv_lhs { congr, rw [monoidal_category.pentagon] }, conv_rhs { congr, rw [←monoidal_category.triangle] }, conv_rhs { rw [category.assoc] }, conv_rhs { congr, skip, congr, congr, rw [←tensor_id] }, conv_rhs { congr, skip, rw [associator_naturality] }, conv_rhs { rw [←category.assoc] } } end lemma right_unitor_product_aux_triangle (X Y : C) : ((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom) = (𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)) := by rw [←id_tensor_comp, ←monoidal_category.triangle] lemma right_unitor_product_aux_square (X Y : C) : (α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫ ((𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C))) = (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) ≫ (α_ X Y (𝟙_ C)).hom := by rw [associator_naturality] lemma right_unitor_product_aux (X Y : C) : ((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) = ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) := begin rw ←(cancel_mono (α_ X Y (𝟙_ C)).hom), slice_lhs 2 3 { rw ←right_unitor_product_aux_square }, rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter], end -- See Proposition 2.2.4 of <http://www-math.mit.edu/~etingof/egnobookfinal.pdf> lemma left_unitor_tensor' (X Y : C) : ((α_ (𝟙_ C) X Y).hom) ≫ ((λ_ (X ⊗ Y)).hom) = ((λ_ X).hom ⊗ (𝟙 Y)) := by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux] @[simp] lemma left_unitor_tensor (X Y : C) : ((λ_ (X ⊗ Y)).hom) = ((α_ (𝟙_ C) X Y).inv) ≫ ((λ_ X).hom ⊗ (𝟙 Y)) := by { rw [←left_unitor_tensor'], simp } lemma left_unitor_tensor_inv' (X Y : C) : ((λ_ (X ⊗ Y)).inv) ≫ ((α_ (𝟙_ C) X Y).inv) = ((λ_ X).inv ⊗ (𝟙 Y)) := eq_of_inv_eq_inv (by simp) @[simp] lemma left_unitor_tensor_inv (X Y : C) : ((λ_ (X ⊗ Y)).inv) = ((λ_ X).inv ⊗ (𝟙 Y)) ≫ ((α_ (𝟙_ C) X Y).hom) := by { rw [←left_unitor_tensor_inv'], simp } @[simp] lemma right_unitor_tensor (X Y : C) : ((ρ_ (X ⊗ Y)).hom) = ((α_ X Y (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (ρ_ Y).hom) := by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux] @[simp] lemma right_unitor_tensor_inv (X Y : C) : ((ρ_ (X ⊗ Y)).inv) = ((𝟙 X) ⊗ (ρ_ Y).inv) ≫ ((α_ X Y (𝟙_ C)).inv) := eq_of_inv_eq_inv (by simp) lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') : (f ⊗ (g ⊗ h)) ≫ (α_ X' Y' Z').inv = (α_ X Y Z).inv ≫ ((f ⊗ g) ⊗ h) := begin apply (cancel_mono (α_ X' Y' Z').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma pentagon_inv (W X Y Z : C) : ((𝟙 W) ⊗ (α_ X Y Z).inv) ≫ (α_ W (X ⊗ Y) Z).inv ≫ ((α_ W X Y).inv ⊗ (𝟙 Z)) = (α_ W X (Y ⊗ Z)).inv ≫ (α_ (W ⊗ X) Y Z).inv := begin apply category_theory.eq_of_inv_eq_inv, dsimp, rw [category.assoc, monoidal_category.pentagon] end lemma triangle_assoc_comp_left (X Y : C) : (α_ X (𝟙_ C) Y).hom ≫ ((𝟙 X) ⊗ (λ_ Y).hom) = (ρ_ X).hom ⊗ 𝟙 Y := monoidal_category.triangle X Y @[simp] lemma triangle_assoc_comp_right (X Y : C) : (α_ X (𝟙_ C) Y).inv ≫ ((ρ_ X).hom ⊗ 𝟙 Y) = ((𝟙 X) ⊗ (λ_ Y).hom) := by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma triangle_assoc_comp_right_inv (X Y : C) : ((ρ_ X).inv ⊗ 𝟙 Y) ≫ (α_ X (𝟙_ C) Y).hom = ((𝟙 X) ⊗ (λ_ Y).inv) := begin apply (cancel_mono (𝟙 X ⊗ (λ_ Y).hom)).1, simp only [assoc, triangle_assoc_comp_left], rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id] end @[simp] lemma triangle_assoc_comp_left_inv (X Y : C) : ((𝟙 X) ⊗ (λ_ Y).inv) ≫ (α_ X (𝟙_ C) Y).inv = ((ρ_ X).inv ⊗ 𝟙 Y) := begin apply (cancel_mono ((ρ_ X).hom ⊗ 𝟙 Y)).1, simp only [triangle_assoc_comp_right, assoc], rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id] end end section variables (C : Type u) [category.{v} C] [monoidal_category.{v} C] /-- The tensor product expressed as a functor. -/ def tensor : (C × C) ⥤ C := { obj := λ X, X.1 ⊗ X.2, map := λ {X Y : C × C} (f : X ⟶ Y), f.1 ⊗ f.2 } /-- The left-associated triple tensor product as a functor. -/ def left_assoc_tensor : (C × C × C) ⥤ C := { obj := λ X, (X.1 ⊗ X.2.1) ⊗ X.2.2, map := λ {X Y : C × C × C} (f : X ⟶ Y), (f.1 ⊗ f.2.1) ⊗ f.2.2 } @[simp] lemma left_assoc_tensor_obj (X) : (left_assoc_tensor C).obj X = (X.1 ⊗ X.2.1) ⊗ X.2.2 := rfl @[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟶ Y) : (left_assoc_tensor C).map f = (f.1 ⊗ f.2.1) ⊗ f.2.2 := rfl /-- The right-associated triple tensor product as a functor. -/ def right_assoc_tensor : (C × C × C) ⥤ C := { obj := λ X, X.1 ⊗ (X.2.1 ⊗ X.2.2), map := λ {X Y : C × C × C} (f : X ⟶ Y), f.1 ⊗ (f.2.1 ⊗ f.2.2) } @[simp] lemma right_assoc_tensor_obj (X) : (right_assoc_tensor C).obj X = X.1 ⊗ (X.2.1 ⊗ X.2.2) := rfl @[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟶ Y) : (right_assoc_tensor C).map f = f.1 ⊗ (f.2.1 ⊗ f.2.2) := rfl /-- The functor `λ X, 𝟙_ C ⊗ X`. -/ def tensor_unit_left : C ⥤ C := { obj := λ X, 𝟙_ C ⊗ X, map := λ {X Y : C} (f : X ⟶ Y), (𝟙 (𝟙_ C)) ⊗ f } /-- The functor `λ X, X ⊗ 𝟙_ C`. -/ def tensor_unit_right : C ⥤ C := { obj := λ X, X ⊗ 𝟙_ C, map := λ {X Y : C} (f : X ⟶ Y), f ⊗ (𝟙 (𝟙_ C)) } -- We can express the associator and the unitors, given componentwise above, -- as natural isomorphisms. /-- The associator as a natural isomorphism. -/ @[simps {rhs_md := semireducible}] def associator_nat_iso : left_assoc_tensor C ≅ right_assoc_tensor C := nat_iso.of_components (by { intros, apply monoidal_category.associator }) (by { intros, apply monoidal_category.associator_naturality }) /-- The left unitor as a natural isomorphism. -/ @[simps {rhs_md := semireducible}] def left_unitor_nat_iso : tensor_unit_left C ≅ 𝟭 C := nat_iso.of_components (by { intros, apply monoidal_category.left_unitor }) (by { intros, apply monoidal_category.left_unitor_naturality }) /-- The right unitor as a natural isomorphism. -/ @[simps {rhs_md := semireducible}] def right_unitor_nat_iso : tensor_unit_right C ≅ 𝟭 C := nat_iso.of_components (by { intros, apply monoidal_category.right_unitor }) (by { intros, apply monoidal_category.right_unitor_naturality }) section variables {C} /-- Tensoring on the left with a fixed object, as a functor. -/ @[simps] def tensor_left (X : C) : C ⥤ C := { obj := λ Y, X ⊗ Y, map := λ Y Y' f, (𝟙 X) ⊗ f, } /-- Tensoring on the left with `X ⊗ Y` is naturally isomorphic to tensoring on the left with `Y`, and then again with `X`. -/ def tensor_left_tensor (X Y : C) : tensor_left (X ⊗ Y) ≅ tensor_left Y ⋙ tensor_left X := nat_iso.of_components (associator _ _) (λ Z Z' f, by { dsimp, rw[←tensor_id], apply associator_naturality }) @[simp] lemma tensor_left_tensor_hom_app (X Y Z : C) : (tensor_left_tensor X Y).hom.app Z = (associator X Y Z).hom := rfl @[simp] lemma tensor_left_tensor_inv_app (X Y Z : C) : (tensor_left_tensor X Y).inv.app Z = (associator X Y Z).inv := rfl /-- Tensoring on the right with a fixed object, as a functor. -/ @[simps] def tensor_right (X : C) : C ⥤ C := { obj := λ Y, Y ⊗ X, map := λ Y Y' f, f ⊗ (𝟙 X), } variables (C) /-- Tensoring on the right, as a functor from `C` into endofunctors of `C`. We later show this is a monoidal functor. -/ @[simps] def tensoring_right : C ⥤ (C ⥤ C) := { obj := tensor_right, map := λ X Y f, { app := λ Z, (𝟙 Z) ⊗ f } } instance : faithful (tensoring_right C) := { map_injective' := λ X Y f g h, begin injections with h, replace h := congr_fun h (𝟙_ C), simpa using h, end } variables {C} /-- Tensoring on the right with `X ⊗ Y` is naturally isomorphic to tensoring on the right with `X`, and then again with `Y`. -/ def tensor_right_tensor (X Y : C) : tensor_right (X ⊗ Y) ≅ tensor_right X ⋙ tensor_right Y := nat_iso.of_components (λ Z, (associator Z X Y).symm) (λ Z Z' f, by { dsimp, rw[←tensor_id], apply associator_inv_naturality }) @[simp] lemma tensor_right_tensor_hom_app (X Y Z : C) : (tensor_right_tensor X Y).hom.app Z = (associator Z X Y).inv := rfl @[simp] lemma tensor_right_tensor_inv_app (X Y Z : C) : (tensor_right_tensor X Y).inv.app Z = (associator Z X Y).hom := rfl end end end monoidal_category end category_theory
4524b7f8f67f50e2f2f58413404d54140b9f140e
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/list/default.lean
c96488d95214059d29d06c4abd858cf14c9edf8b
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
268
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.list.basic init.data.list.instances init.data.list.lemmas init.data.list.qsort
1ca74300cf57f0282144c05aabb8842b6e35c277
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/limits/shapes/default.lean
f107712a326af686ce8c1b82dbda83e5d84ca6ed
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
194
lean
import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.products import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.pullbacks
c050772196e1ee91df49b813b22047777851548e
1437b3495ef9020d5413178aa33c0a625f15f15f
/group_theory/subgroup.lean
558302ceff50ea548229fc6e68198d50c6a32bbd
[ "Apache-2.0" ]
permissive
jean002/mathlib
c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30
dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd
refs/heads/master
1,587,027,806,375
1,547,306,358,000
1,547,306,358,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
22,474
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro -/ import group_theory.submonoid open set function variables {α : Type*} {β : Type*} {a a₁ a₂ b c: α} section group variables [group α] [add_group β] @[to_additive injective_add] lemma injective_mul {a : α} : injective ((*) a) := assume a₁ a₂ h, have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h], by rwa [inv_mul_self, one_mul, one_mul] at this /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ class is_subgroup (s : set α) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup (s : set β) extends is_add_submonoid s : Prop := (neg_mem {a} : a ∈ s → -a ∈ s) attribute [to_additive is_add_subgroup] is_subgroup attribute [to_additive is_add_subgroup.to_is_add_submonoid] is_subgroup.to_is_submonoid attribute [to_additive is_add_subgroup.neg_mem] is_subgroup.inv_mem attribute [to_additive is_add_subgroup.mk] is_subgroup.mk instance additive.is_add_subgroup (s : set α) [is_subgroup s] : @is_add_subgroup (additive α) _ s := ⟨@is_subgroup.inv_mem _ _ _ _⟩ theorem additive.is_add_subgroup_iff {s : set α} : @is_add_subgroup (additive α) _ s ↔ is_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk α _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ instance multiplicative.is_subgroup (s : set β) [is_add_subgroup s] : @is_subgroup (multiplicative β) _ s := ⟨@is_add_subgroup.neg_mem _ _ _ _⟩ theorem multiplicative.is_subgroup_iff {s : set β} : @is_subgroup (multiplicative β) _ s ↔ is_add_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk β _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ instance subtype.group {s : set α} [is_subgroup s] : group s := by subtype_instance instance subtype.add_group {s : set β} [is_add_subgroup s] : add_group s := by subtype_instance attribute [to_additive subtype.add_group] subtype.group @[simp, to_additive is_add_subgroup.coe_neg] lemma is_subgroup.coe_inv {s : set α} [is_subgroup s] (a : s) : ((a⁻¹ : s) : α) = a⁻¹ := rfl @[simp] lemma is_subgroup.coe_gpow {s : set α} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : α) = a ^ n := by induction n; simp [is_submonoid.coe_pow a] @[simp] lemma is_add_subgroup.gsmul_coe {β : Type*} [add_group β] {s : set β} [is_add_subgroup s] (a : s) (n : ℤ) : ((gsmul n a : s) : β) = gsmul n a := by induction n; simp [is_add_submonoid.smul_coe a] attribute [to_additive is_add_subgroup.gsmul_coe] is_subgroup.coe_gpow theorem is_subgroup.of_div (s : set α) (one_mem : (1:α) ∈ s) (div_mem : ∀{a b:α}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s): is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } theorem is_add_subgroup.of_sub (s : set β) (zero_mem : (0:β) ∈ s) (sub_mem : ∀{a b:β}, a ∈ s → b ∈ s → a - b ∈ s): is_add_subgroup s := multiplicative.is_subgroup_iff.1 $ @is_subgroup.of_div (multiplicative β) _ _ zero_mem @sub_mem def gpowers (x : α) : set α := set.range ((^) x : ℤ → α) def gmultiples (x : β) : set β := set.range (λ i, gsmul i x) attribute [to_additive gmultiples] gpowers instance gpowers.is_subgroup (x : α) : is_subgroup (gpowers x) := { one_mem := ⟨(0:ℤ), by simp⟩, mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩, inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ } instance gmultiples.is_add_subgroup (x : β) : is_add_subgroup (gmultiples x) := multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _ attribute [to_additive gmultiples.is_add_subgroup] gpowers.is_subgroup lemma is_subgroup.gpow_mem {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s | (n : ℕ) := is_submonoid.pow_mem h | -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h) lemma is_add_subgroup.gsmul_mem {a : β} {s : set β} [is_add_subgroup s] : a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s := @is_subgroup.gpow_mem (multiplicative β) _ _ _ _ lemma mem_gpowers {a : α} : a ∈ gpowers a := ⟨1, by simp⟩ lemma mem_gmultiples {a : β} : a ∈ gmultiples a := ⟨1, by simp⟩ attribute [to_additive mem_gmultiples] mem_gpowers end group namespace is_subgroup open is_submonoid variables [group α] (s : set α) [is_subgroup s] @[to_additive is_add_subgroup.neg_mem_iff] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := ⟨λ h, by simpa using inv_mem h, inv_mem⟩ @[to_additive is_add_subgroup.add_mem_cancel_left] lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive is_add_subgroup.add_mem_cancel_right] lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ end is_subgroup theorem is_add_subgroup.sub_mem {α} [add_group α] (s : set α) [is_add_subgroup s] (a b : α) (ha : a ∈ s) (hb : b ∈ s) : a - b ∈ s := is_add_submonoid.add_mem ha (is_add_subgroup.neg_mem hb) namespace group open is_submonoid is_subgroup variables [group α] {s : set α} inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : α} : in_closure a → in_closure a⁻¹ | mul {a b : α} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ def closure (s : set α) : set α := {a | in_closure s a } lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := in_closure.basic instance closure.is_subgroup (s : set α) : is_subgroup (closure s) := { one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } theorem subset_closure {s : set α} : s ⊆ closure s := λ a, mem_closure theorem closure_subset {s t : set α} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] lemma closure_subset_iff (s t : set α) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t := ⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩ theorem gpowers_eq_closure {a : α} : gpowers a = closure {a} := subset.antisymm (assume x h, match x, h with _, ⟨i, rfl⟩ := gpow_mem (mem_closure $ by simp) end) (closure_subset $ by simp [mem_gpowers]) end group namespace add_group open is_add_submonoid is_add_subgroup variables [add_group α] {s : set α} /-- `add_group.closure s` is the additive subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ def closure (s : set α) : set α := @group.closure (multiplicative α) _ s attribute [to_additive add_group.closure] group.closure lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := group.mem_closure attribute [to_additive add_group.mem_closure] group.mem_closure instance closure.is_add_subgroup (s : set α) : is_add_subgroup (closure s) := multiplicative.is_subgroup_iff.1 $ group.closure.is_subgroup _ attribute [to_additive add_group.closure.is_add_subgroup] group.closure.is_subgroup attribute [to_additive add_group.subset_closure] group.subset_closure theorem closure_subset {s t : set α} [is_add_subgroup t] : s ⊆ t → closure s ⊆ t := group.closure_subset attribute [to_additive add_group.closure_subset] group.closure_subset attribute [to_additive add_group.closure_subset_iff] group.closure_subset_iff theorem gmultiples_eq_closure {a : α} : gmultiples a = closure {a} := group.gpowers_eq_closure attribute [to_additive add_group.gmultiples_eq_closure] group.gpowers_eq_closure @[elab_as_eliminator] theorem in_closure.rec_on {C : α → Prop} {a : α} (H : a ∈ closure s) (H1 : ∀ {a : α}, a ∈ s → C a) (H2 : C 0) (H3 : ∀ {a : α}, a ∈ closure s → C a → C (-a)) (H4 : ∀ {a b : α}, a ∈ closure s → b ∈ closure s → C a → C b → C (a + b)) : C a := group.in_closure.rec_on H (λ _, H1) H2 (λ _, H3) (λ _ _, H4) end add_group class normal_subgroup [group α] (s : set α) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g * n * g⁻¹ ∈ s) class normal_add_subgroup [add_group α] (s : set α) extends is_add_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g + n - g ∈ s) attribute [to_additive normal_add_subgroup] normal_subgroup attribute [to_additive normal_add_subgroup.to_is_add_subgroup] normal_subgroup.to_is_subgroup attribute [to_additive normal_add_subgroup.normal] normal_subgroup.normal attribute [to_additive normal_add_subgroup.mk] normal_subgroup.mk @[to_additive normal_add_subgroup_of_add_comm_group] lemma normal_subgroup_of_comm_group [comm_group α] (s : set α) [hs : is_subgroup s] : normal_subgroup s := { normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul], ..hs } instance additive.normal_add_subgroup [group α] (s : set α) [normal_subgroup s] : @normal_add_subgroup (additive α) _ s := ⟨@normal_subgroup.normal _ _ _ _⟩ theorem additive.normal_add_subgroup_iff [group α] {s : set α} : @normal_add_subgroup (additive α) _ s ↔ normal_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_subgroup.mk α _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ instance multiplicative.normal_subgroup [add_group α] (s : set α) [normal_add_subgroup s] : @normal_subgroup (multiplicative α) _ s := ⟨@normal_add_subgroup.normal _ _ _ _⟩ theorem multiplicative.normal_subgroup_iff [add_group α] {s : set α} : @normal_subgroup (multiplicative α) _ s ↔ normal_add_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_add_subgroup.mk α _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ namespace is_subgroup variable [group α] -- Normal subgroup properties lemma mem_norm_comm {s : set α} [normal_subgroup s] {a b : α} (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h lemma mem_norm_comm_iff {s : set α} [normal_subgroup s] {a b : α} : a * b ∈ s ↔ b * a ∈ s := ⟨mem_norm_comm, mem_norm_comm⟩ /-- The trivial subgroup -/ def trivial (α : Type*) [group α] : set α := {1} @[simp] lemma mem_trivial [group α] {g : α} : g ∈ trivial α ↔ g = 1 := mem_singleton_iff instance trivial_normal : normal_subgroup (trivial α) := by refine {..}; simp [trivial] {contextual := tt} lemma trivial_eq_closure : trivial α = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) lemma eq_trivial_iff {H : set α} [is_subgroup H] : H = trivial α ↔ (∀ x ∈ H, x = (1 : α)) := by simp only [set.ext_iff, is_subgroup.mem_trivial]; exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem H⟩⟩ instance univ_subgroup : normal_subgroup (@univ α) := by refine {..}; simp def center (α : Type*) [group α] : set α := {z | ∀ g, g * z = z * g} lemma mem_center {a : α} : a ∈ center α ↔ ∀g, g * a = a * g := iff.rfl instance center_normal : normal_subgroup (center α) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } def normalizer (s : set α) : set α := {g : α | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s} instance (s : set α) [is_subgroup s] : is_subgroup (normalizer s) := { one_mem := by simp [normalizer], mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) (hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n, by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb], inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n, by rw [ha (a⁻¹ * n * a⁻¹⁻¹)]; simp [mul_assoc] } lemma subset_normalizer (s : set α) [is_subgroup s] : s ⊆ normalizer s := λ g hg n, by rw [is_subgroup.mul_mem_cancel_left _ ((is_subgroup.inv_mem_iff _).2 hg), is_subgroup.mul_mem_cancel_right _ hg] instance (s : set α) [is_subgroup s] : normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) := { one_mem := show (1 : α) ∈ s, from is_submonoid.one_mem _, mul_mem := λ a b ha hb, show (a * b : α) ∈ s, from is_submonoid.mul_mem ha hb, inv_mem := λ a ha, show (a⁻¹ : α) ∈ s, from is_subgroup.inv_mem ha, normal := λ a ha ⟨m, hm⟩, (hm a).1 ha } end is_subgroup namespace is_add_subgroup variable [add_group α] attribute [to_additive is_add_subgroup.mem_norm_comm] is_subgroup.mem_norm_comm attribute [to_additive is_add_subgroup.mem_norm_comm_iff] is_subgroup.mem_norm_comm_iff /-- The trivial subgroup -/ def trivial (α : Type*) [add_group α] : set α := {0} attribute [to_additive is_add_subgroup.trivial] is_subgroup.trivial attribute [to_additive is_add_subgroup.mem_trivial] is_subgroup.mem_trivial instance trivial_normal : normal_add_subgroup (trivial α) := multiplicative.normal_subgroup_iff.1 is_subgroup.trivial_normal attribute [to_additive is_add_subgroup.trivial_normal] is_subgroup.trivial_normal attribute [to_additive is_add_subgroup.trivial_eq_closure] is_subgroup.trivial_eq_closure attribute [to_additive is_add_subgroup.eq_trivial_iff] is_subgroup.eq_trivial_iff instance univ_add_subgroup : normal_add_subgroup (@univ α) := multiplicative.normal_subgroup_iff.1 is_subgroup.univ_subgroup attribute [to_additive is_add_subgroup.univ_add_subgroup] is_subgroup.univ_subgroup def center (α : Type*) [add_group α] : set α := {z | ∀ g, g + z = z + g} attribute [to_additive is_add_subgroup.center] is_subgroup.center attribute [to_additive is_add_subgroup.mem_center] is_subgroup.mem_center instance center_normal : normal_add_subgroup (center α) := multiplicative.normal_subgroup_iff.1 is_subgroup.center_normal end is_add_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup variables [group α] [group β] @[to_additive is_add_group_hom.ker] def ker (f : α → β) [is_group_hom f] : set α := preimage f (trivial β) attribute [to_additive is_add_group_hom.ker.equations._eqn_1] ker.equations._eqn_1 @[to_additive is_add_group_hom.mem_ker] lemma mem_ker (f : α → β) [is_group_hom f] {x : α} : x ∈ ker f ↔ f x = 1 := mem_trivial @[to_additive is_add_group_hom.zero_ker_neg] lemma one_ker_inv (f : α → β) [is_group_hom f] {a b : α} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [mul f, inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end @[to_additive is_add_group_hom.neg_ker_zero] lemma inv_ker_one (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←inv f, ←mul f] at this @[to_additive is_add_group_hom.zero_iff_ker_neg] lemma one_iff_ker_inv (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ @[to_additive is_add_group_hom.neg_iff_ker] lemma inv_iff_ker (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ instance image_subgroup (f : α → β) [is_group_hom f] (s : set α) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, mul f]⟩, one_mem := ⟨1, one_mem s, one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw inv f; simp *⟩ } attribute [to_additive is_add_group_hom.image_add_subgroup._match_1] is_group_hom.image_subgroup._match_1 attribute [to_additive is_add_group_hom.image_add_subgroup._match_2] is_group_hom.image_subgroup._match_2 attribute [to_additive is_add_group_hom.image_add_subgroup._match_3] is_group_hom.image_subgroup._match_3 attribute [to_additive is_add_group_hom.image_add_subgroup] is_group_hom.image_subgroup attribute [to_additive is_add_group_hom.image_add_subgroup._match_1.equations._eqn_1] is_group_hom.image_subgroup._match_1.equations._eqn_1 attribute [to_additive is_add_group_hom.image_add_subgroup._match_2.equations._eqn_1] is_group_hom.image_subgroup._match_2.equations._eqn_1 attribute [to_additive is_add_group_hom.image_add_subgroup._match_3.equations._eqn_1] is_group_hom.image_subgroup._match_3.equations._eqn_1 attribute [to_additive is_add_group_hom.image_add_subgroup.equations._eqn_1] is_group_hom.image_subgroup.equations._eqn_1 instance range_subgroup (f : α → β) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ attribute [to_additive is_add_group_hom.range_add_subgroup] is_group_hom.range_subgroup attribute [to_additive is_add_group_hom.range_add_subgroup.equations._eqn_1] is_group_hom.range_subgroup.equations._eqn_1 local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal instance preimage (f : α → β) [is_group_hom f] (s : set β) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [mul f, one f, inv f, @inv_mem β _ s] {contextual:=tt} attribute [to_additive is_add_group_hom.preimage] is_group_hom.preimage attribute [to_additive is_add_group_hom.preimage.equations._eqn_1] is_group_hom.preimage.equations._eqn_1 instance preimage_normal (f : α → β) [is_group_hom f] (s : set β) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [mul f, inv f] {contextual:=tt}⟩ attribute [to_additive is_add_group_hom.preimage_normal] is_group_hom.preimage_normal attribute [to_additive is_add_group_hom.preimage_normal.equations._eqn_1] is_group_hom.preimage_normal.equations._eqn_1 instance normal_subgroup_ker (f : α → β) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial β) attribute [to_additive is_add_group_hom.normal_subgroup_ker] is_group_hom.normal_subgroup_ker attribute [to_additive is_add_group_hom.normal_subgroup_ker.equations._eqn_1] is_group_hom.normal_subgroup_ker.equations._eqn_1 lemma inj_of_trivial_ker (f : α → β) [is_group_hom f] (h : ker f = trivial α) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end lemma trivial_ker_of_inj (f : α → β) [is_group_hom f] (h : function.injective f) : ker f = trivial α := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.one f] {contextual := tt}) lemma inj_iff_trivial_ker (f : α → β) [is_group_hom f] : function.injective f ↔ ker f = trivial α := ⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩ instance (s : set α) [is_subgroup s] : is_group_hom (subtype.val : s → α) := ⟨λ _ _, rfl⟩ end is_group_hom section simple_group class simple_group (α : Type*) [group α] : Prop := (simple : ∀ (N : set α) [normal_subgroup N], N = is_subgroup.trivial α ∨ N = set.univ) class simple_add_group (α : Type*) [add_group α] : Prop := (simple : ∀ (N : set α) [normal_add_subgroup N], N = is_add_subgroup.trivial α ∨ N = set.univ) attribute [to_additive simple_add_group] simple_group theorem additive.simple_add_group_iff [group α] : simple_add_group (additive α) ↔ simple_group α := ⟨λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.1 h)⟩⟩ instance additive.simple_add_group [group α] [simple_group α] : simple_add_group (additive α) := additive.simple_add_group_iff.2 (by apply_instance) theorem multiplicative.simple_group_iff [add_group α] : simple_group (multiplicative α) ↔ simple_add_group α := ⟨λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.1 h)⟩⟩ instance multiplicative.simple_group [add_group α] [simple_add_group α] : simple_group (multiplicative α) := multiplicative.simple_group_iff.2 (by apply_instance) lemma simple_group_of_surjective [group α] [group β] [simple_group α] (f : α → β) [is_group_hom f] (hf : function.surjective f) : simple_group β := ⟨λ H iH, have normal_subgroup (f ⁻¹' H), by resetI; apply_instance, begin resetI, cases simple_group.simple (f ⁻¹' H) with h h, { refine or.inl (is_subgroup.eq_trivial_iff.2 (λ x hx, _)), cases hf x with y hy, rw ← hy at hx, rw [← hy, is_subgroup.eq_trivial_iff.1 h y hx, is_group_hom.one f] }, { refine or.inr (set.eq_univ_of_forall (λ x, _)), cases hf x with y hy, rw set.eq_univ_iff_forall at h, rw ← hy, exact h y } end⟩ lemma simple_add_group_of_surjective [add_group α] [add_group β] [simple_add_group α] (f : α → β) [is_add_group_hom f] (hf : function.surjective f) : simple_add_group β := multiplicative.simple_group_iff.1 (@simple_group_of_surjective (multiplicative α) (multiplicative β) _ _ _ f _ hf) attribute [to_additive simple_add_group_of_surjective] simple_group_of_surjective end simple_group
29baea62cb06cfa2b9ceff03866b761cbfa18065
ff5230333a701471f46c57e8c115a073ebaaa448
/library/init/meta/interaction_monad.lean
724b4f49632d83d55dbebe61d34875c6ba85021d
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
4,214
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import init.function init.data.option.basic init.util import init.category.combinators init.category.monad init.category.alternative init.category.monad_fail import init.data.nat.div init.meta.exceptional init.meta.format init.meta.environment import init.meta.pexpr init.data.repr init.data.string.basic init.data.to_string universes u v meta inductive interaction_monad.result (state : Type) (α : Type u) | success : α → state → interaction_monad.result | exception {} : option (unit → format) → option pos → state → interaction_monad.result open interaction_monad.result section variables {state : Type} {α : Type u} variables [has_to_string α] meta def interaction_monad.result_to_string : result state α → string | (success a s) := to_string a | (exception (some t) ref s) := "Exception: " ++ to_string (t ()) | (exception none ref s) := "[silent exception]" meta instance interaction_monad.result_has_string : has_to_string (result state α) := ⟨interaction_monad.result_to_string⟩ end meta def interaction_monad.result.clamp_pos {state : Type} {α : Type u} (line0 line col : ℕ) : result state α → result state α | (success a s) := success a s | (exception msg (some p) s) := exception msg (some $ if p.line < line0 then ⟨line, col⟩ else p) s | (exception msg none s) := exception msg (some ⟨line, col⟩) s @[reducible] meta def interaction_monad (state : Type) (α : Type u) := state → result state α section parameter {state : Type} variables {α : Type u} {β : Type v} local notation `m` := interaction_monad state @[inline] meta def interaction_monad_fmap (f : α → β) (t : m α) : m β := λ s, interaction_monad.result.cases_on (t s) (λ a s', success (f a) s') (λ e s', exception e s') @[inline] meta def interaction_monad_bind (t₁ : m α) (t₂ : α → m β) : m β := λ s, interaction_monad.result.cases_on (t₁ s) (λ a s', t₂ a s') (λ e s', exception e s') @[inline] meta def interaction_monad_return (a : α) : m α := λ s, success a s meta def interaction_monad_orelse {α : Type u} (t₁ t₂ : m α) : m α := λ s, interaction_monad.result.cases_on (t₁ s) success (λ e₁ ref₁ s', interaction_monad.result.cases_on (t₂ s) success exception) @[inline] meta def interaction_monad_seq (t₁ : m α) (t₂ : m β) : m β := interaction_monad_bind t₁ (λ a, t₂) meta instance interaction_monad.monad : monad m := {map := @interaction_monad_fmap, pure := @interaction_monad_return, bind := @interaction_monad_bind} meta def interaction_monad.mk_exception {α : Type u} {β : Type v} [has_to_format β] (msg : β) (ref : option expr) (s : state) : result state α := exception (some (λ _, to_fmt msg)) none s meta def interaction_monad.fail {α : Type u} {β : Type v} [has_to_format β] (msg : β) : m α := λ s, interaction_monad.mk_exception msg none s meta def interaction_monad.silent_fail {α : Type u} : m α := λ s, exception none none s meta def interaction_monad.failed {α : Type u} : m α := interaction_monad.fail "failed" /- Alternative orelse operator that allows to select which exception should be used. The default is to use the first exception since the standard `orelse` uses the second. -/ meta def interaction_monad.orelse' {α : Type u} (t₁ t₂ : m α) (use_first_ex := tt) : m α := λ s, interaction_monad.result.cases_on (t₁ s) success (λ e₁ ref₁ s₁', interaction_monad.result.cases_on (t₂ s) success (λ e₂ ref₂ s₂', if use_first_ex then (exception e₁ ref₁ s₁') else (exception e₂ ref₂ s₂'))) meta instance interaction_monad.monad_fail : monad_fail m := { fail := λ α s, interaction_monad.fail (to_fmt s), ..interaction_monad.monad } -- TODO: unify `parser` and `tactic` behavior? -- meta instance interaction_monad.alternative : alternative m := -- ⟨@interaction_monad_fmap, (λ α a s, success a s), (@fapp _ _), @interaction_monad.failed, @interaction_monad_orelse⟩ end
bac87185a0278d6fc5c07b5c1b53fa91e365e638
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/function/l1_space.lean
2f1c344e116b09c74476e2cd67f91adf9b7378f2
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
55,394
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.function.lp_order /-! # Integrable functions and `L¹` space In the first part of this file, the predicate `integrable` is defined and basic properties of integrable functions are proved. Such a predicate is already available under the name `mem_ℒp 1`. We give a direct definition which is easier to use, and show that it is equivalent to `mem_ℒp 1` In the second part, we establish an API between `integrable` and the space `L¹` of equivalence classes of integrable functions, already defined as a special case of `L^p` spaces for `p = 1`. ## Notation * `α →₁[μ] β` is the type of `L¹` space, where `α` is a `measure_space` and `β` is a `normed_add_comm_group` with a `second_countable_topology`. `f : α →ₘ β` is a "function" in `L¹`. In comments, `[f]` is also used to denote an `L¹` function. `₁` can be typed as `\1`. ## Main definitions * Let `f : α → β` be a function, where `α` is a `measure_space` and `β` a `normed_add_comm_group`. Then `has_finite_integral f` means `(∫⁻ a, ‖f a‖₊) < ∞`. * If `β` is moreover a `measurable_space` then `f` is called `integrable` if `f` is `measurable` and `has_finite_integral f` holds. ## Implementation notes To prove something for an arbitrary integrable function, a useful theorem is `integrable.induction` in the file `set_integral`. ## Tags integrable, function space, l1 -/ noncomputable theory open_locale classical topology big_operators ennreal measure_theory nnreal open set filter topological_space ennreal emetric measure_theory variables {α β γ δ : Type*} {m : measurable_space α} {μ ν : measure α} [measurable_space δ] variables [normed_add_comm_group β] variables [normed_add_comm_group γ] namespace measure_theory /-! ### Some results about the Lebesgue integral involving a normed group -/ lemma lintegral_nnnorm_eq_lintegral_edist (f : α → β) : ∫⁻ a, ‖f a‖₊ ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [edist_eq_coe_nnnorm] lemma lintegral_norm_eq_lintegral_edist (f : α → β) : ∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [of_real_norm_eq_coe_nnnorm, edist_eq_coe_nnnorm] lemma lintegral_edist_triangle {f g h : α → β} (hf : ae_strongly_measurable f μ) (hh : ae_strongly_measurable h μ) : ∫⁻ a, edist (f a) (g a) ∂μ ≤ ∫⁻ a, edist (f a) (h a) ∂μ + ∫⁻ a, edist (g a) (h a) ∂μ := begin rw ← lintegral_add_left' (hf.edist hh), refine lintegral_mono (λ a, _), apply edist_triangle_right end lemma lintegral_nnnorm_zero : ∫⁻ a : α, ‖(0 : β)‖₊ ∂μ = 0 := by simp lemma lintegral_nnnorm_add_left {f : α → β} (hf : ae_strongly_measurable f μ) (g : α → γ) : ∫⁻ a, ‖f a‖₊ + ‖g a‖₊ ∂μ = ∫⁻ a, ‖f a‖₊ ∂μ + ∫⁻ a, ‖g a‖₊ ∂μ := lintegral_add_left' hf.ennnorm _ lemma lintegral_nnnorm_add_right (f : α → β) {g : α → γ} (hg : ae_strongly_measurable g μ) : ∫⁻ a, ‖f a‖₊ + ‖g a‖₊ ∂μ = ∫⁻ a, ‖f a‖₊ ∂μ + ∫⁻ a, ‖g a‖₊ ∂μ := lintegral_add_right' _ hg.ennnorm lemma lintegral_nnnorm_neg {f : α → β} : ∫⁻ a, ‖(-f) a‖₊ ∂μ = ∫⁻ a, ‖f a‖₊ ∂μ := by simp only [pi.neg_apply, nnnorm_neg] /-! ### The predicate `has_finite_integral` -/ /-- `has_finite_integral f μ` means that the integral `∫⁻ a, ‖f a‖ ∂μ` is finite. `has_finite_integral f` means `has_finite_integral f volume`. -/ def has_finite_integral {m : measurable_space α} (f : α → β) (μ : measure α . volume_tac) : Prop := ∫⁻ a, ‖f a‖₊ ∂μ < ∞ lemma has_finite_integral_iff_norm (f : α → β) : has_finite_integral f μ ↔ ∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ < ∞ := by simp only [has_finite_integral, of_real_norm_eq_coe_nnnorm] lemma has_finite_integral_iff_edist (f : α → β) : has_finite_integral f μ ↔ ∫⁻ a, edist (f a) 0 ∂μ < ∞ := by simp only [has_finite_integral_iff_norm, edist_dist, dist_zero_right] lemma has_finite_integral_iff_of_real {f : α → ℝ} (h : 0 ≤ᵐ[μ] f) : has_finite_integral f μ ↔ ∫⁻ a, ennreal.of_real (f a) ∂μ < ∞ := by rw [has_finite_integral, lintegral_nnnorm_eq_of_ae_nonneg h] lemma has_finite_integral_iff_of_nnreal {f : α → ℝ≥0} : has_finite_integral (λ x, (f x : ℝ)) μ ↔ ∫⁻ a, f a ∂μ < ∞ := by simp [has_finite_integral_iff_norm] lemma has_finite_integral.mono {f : α → β} {g : α → γ} (hg : has_finite_integral g μ) (h : ∀ᵐ a ∂μ, ‖f a‖ ≤ ‖g a‖) : has_finite_integral f μ := begin simp only [has_finite_integral_iff_norm] at *, calc ∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ ≤ ∫⁻ (a : α), (ennreal.of_real ‖g a‖) ∂μ : lintegral_mono_ae (h.mono $ assume a h, of_real_le_of_real h) ... < ∞ : hg end lemma has_finite_integral.mono' {f : α → β} {g : α → ℝ} (hg : has_finite_integral g μ) (h : ∀ᵐ a ∂μ, ‖f a‖ ≤ g a) : has_finite_integral f μ := hg.mono $ h.mono $ λ x hx, le_trans hx (le_abs_self _) lemma has_finite_integral.congr' {f : α → β} {g : α → γ} (hf : has_finite_integral f μ) (h : ∀ᵐ a ∂μ, ‖f a‖ = ‖g a‖) : has_finite_integral g μ := hf.mono $ eventually_eq.le $ eventually_eq.symm h lemma has_finite_integral_congr' {f : α → β} {g : α → γ} (h : ∀ᵐ a ∂μ, ‖f a‖ = ‖g a‖) : has_finite_integral f μ ↔ has_finite_integral g μ := ⟨λ hf, hf.congr' h, λ hg, hg.congr' $ eventually_eq.symm h⟩ lemma has_finite_integral.congr {f g : α → β} (hf : has_finite_integral f μ) (h : f =ᵐ[μ] g) : has_finite_integral g μ := hf.congr' $ h.fun_comp norm lemma has_finite_integral_congr {f g : α → β} (h : f =ᵐ[μ] g) : has_finite_integral f μ ↔ has_finite_integral g μ := has_finite_integral_congr' $ h.fun_comp norm lemma has_finite_integral_const_iff {c : β} : has_finite_integral (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ∞ := by simp [has_finite_integral, lintegral_const, lt_top_iff_ne_top, ennreal.mul_eq_top, or_iff_not_imp_left] lemma has_finite_integral_const [is_finite_measure μ] (c : β) : has_finite_integral (λ x : α, c) μ := has_finite_integral_const_iff.2 (or.inr $ measure_lt_top _ _) lemma has_finite_integral_of_bounded [is_finite_measure μ] {f : α → β} {C : ℝ} (hC : ∀ᵐ a ∂μ, ‖f a‖ ≤ C) : has_finite_integral f μ := (has_finite_integral_const C).mono' hC lemma has_finite_integral.mono_measure {f : α → β} (h : has_finite_integral f ν) (hμ : μ ≤ ν) : has_finite_integral f μ := lt_of_le_of_lt (lintegral_mono' hμ le_rfl) h lemma has_finite_integral.add_measure {f : α → β} (hμ : has_finite_integral f μ) (hν : has_finite_integral f ν) : has_finite_integral f (μ + ν) := begin simp only [has_finite_integral, lintegral_add_measure] at *, exact add_lt_top.2 ⟨hμ, hν⟩ end lemma has_finite_integral.left_of_add_measure {f : α → β} (h : has_finite_integral f (μ + ν)) : has_finite_integral f μ := h.mono_measure $ measure.le_add_right $ le_rfl lemma has_finite_integral.right_of_add_measure {f : α → β} (h : has_finite_integral f (μ + ν)) : has_finite_integral f ν := h.mono_measure $ measure.le_add_left $ le_rfl @[simp] lemma has_finite_integral_add_measure {f : α → β} : has_finite_integral f (μ + ν) ↔ has_finite_integral f μ ∧ has_finite_integral f ν := ⟨λ h, ⟨h.left_of_add_measure, h.right_of_add_measure⟩, λ h, h.1.add_measure h.2⟩ lemma has_finite_integral.smul_measure {f : α → β} (h : has_finite_integral f μ) {c : ℝ≥0∞} (hc : c ≠ ∞) : has_finite_integral f (c • μ) := begin simp only [has_finite_integral, lintegral_smul_measure] at *, exact mul_lt_top hc h.ne end @[simp] lemma has_finite_integral_zero_measure {m : measurable_space α} (f : α → β) : has_finite_integral f (0 : measure α) := by simp only [has_finite_integral, lintegral_zero_measure, with_top.zero_lt_top] variables (α β μ) @[simp] lemma has_finite_integral_zero : has_finite_integral (λa:α, (0:β)) μ := by simp [has_finite_integral] variables {α β μ} lemma has_finite_integral.neg {f : α → β} (hfi : has_finite_integral f μ) : has_finite_integral (-f) μ := by simpa [has_finite_integral] using hfi @[simp] lemma has_finite_integral_neg_iff {f : α → β} : has_finite_integral (-f) μ ↔ has_finite_integral f μ := ⟨λ h, neg_neg f ▸ h.neg, has_finite_integral.neg⟩ lemma has_finite_integral.norm {f : α → β} (hfi : has_finite_integral f μ) : has_finite_integral (λa, ‖f a‖) μ := have eq : (λa, (nnnorm ‖f a‖ : ℝ≥0∞)) = λa, (‖f a‖₊ : ℝ≥0∞), by { funext, rw nnnorm_norm }, by { rwa [has_finite_integral, eq] } lemma has_finite_integral_norm_iff (f : α → β) : has_finite_integral (λa, ‖f a‖) μ ↔ has_finite_integral f μ := has_finite_integral_congr' $ eventually_of_forall $ λ x, norm_norm (f x) lemma has_finite_integral_to_real_of_lintegral_ne_top {f : α → ℝ≥0∞} (hf : ∫⁻ x, f x ∂μ ≠ ∞) : has_finite_integral (λ x, (f x).to_real) μ := begin have : ∀ x, (‖(f x).to_real‖₊ : ℝ≥0∞) = @coe ℝ≥0 ℝ≥0∞ _ (⟨(f x).to_real, ennreal.to_real_nonneg⟩ : ℝ≥0), { intro x, rw real.nnnorm_of_nonneg }, simp_rw [has_finite_integral, this], refine lt_of_le_of_lt (lintegral_mono (λ x, _)) (lt_top_iff_ne_top.2 hf), by_cases hfx : f x = ∞, { simp [hfx] }, { lift f x to ℝ≥0 using hfx with fx, simp [← h] } end lemma is_finite_measure_with_density_of_real {f : α → ℝ} (hfi : has_finite_integral f μ) : is_finite_measure (μ.with_density (λ x, ennreal.of_real $ f x)) := begin refine is_finite_measure_with_density ((lintegral_mono $ λ x, _).trans_lt hfi).ne, exact real.of_real_le_ennnorm (f x) end section dominated_convergence variables {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} lemma all_ae_of_real_F_le_bound (h : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ‖F n a‖ ≤ ennreal.of_real (bound a) := λn, (h n).mono $ λ a h, ennreal.of_real_le_of_real h lemma all_ae_tendsto_of_real_norm (h : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top $ 𝓝 $ f a) : ∀ᵐ a ∂μ, tendsto (λn, ennreal.of_real ‖F n a‖) at_top $ 𝓝 $ ennreal.of_real ‖f a‖ := h.mono $ λ a h, tendsto_of_real $ tendsto.comp (continuous.tendsto continuous_norm _) h lemma all_ae_of_real_f_le_bound (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : ∀ᵐ a ∂μ, ennreal.of_real ‖f a‖ ≤ ennreal.of_real (bound a) := begin have F_le_bound := all_ae_of_real_F_le_bound h_bound, rw ← ae_all_iff at F_le_bound, apply F_le_bound.mp ((all_ae_tendsto_of_real_norm h_lim).mono _), assume a tendsto_norm F_le_bound, exact le_of_tendsto' tendsto_norm (F_le_bound) end lemma has_finite_integral_of_dominated_convergence {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (bound_has_finite_integral : has_finite_integral bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : has_finite_integral f μ := /- `‖F n a‖ ≤ bound a` and `‖F n a‖ --> ‖f a‖` implies `‖f a‖ ≤ bound a`, and so `∫ ‖f‖ ≤ ∫ bound < ∞` since `bound` is has_finite_integral -/ begin rw has_finite_integral_iff_norm, calc ∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ ≤ ∫⁻ a, ennreal.of_real (bound a) ∂μ : lintegral_mono_ae $ all_ae_of_real_f_le_bound h_bound h_lim ... < ∞ : begin rw ← has_finite_integral_iff_of_real, { exact bound_has_finite_integral }, exact (h_bound 0).mono (λ a h, le_trans (norm_nonneg _) h) end end lemma tendsto_lintegral_norm_of_dominated_convergence {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (F_measurable : ∀ n, ae_strongly_measurable (F n) μ) (bound_has_finite_integral : has_finite_integral bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, (ennreal.of_real ‖F n a - f a‖) ∂μ) at_top (𝓝 0) := have f_measurable : ae_strongly_measurable f μ := ae_strongly_measurable_of_tendsto_ae _ F_measurable h_lim, let b := λ a, 2 * ennreal.of_real (bound a) in /- `‖F n a‖ ≤ bound a` and `F n a --> f a` implies `‖f a‖ ≤ bound a`, and thus by the triangle inequality, have `‖F n a - f a‖ ≤ 2 * (bound a). -/ have hb : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ‖F n a - f a‖ ≤ b a, begin assume n, filter_upwards [all_ae_of_real_F_le_bound h_bound n, all_ae_of_real_f_le_bound h_bound h_lim] with a h₁ h₂, calc ennreal.of_real ‖F n a - f a‖ ≤ (ennreal.of_real ‖F n a‖) + (ennreal.of_real ‖f a‖) : begin rw [← ennreal.of_real_add], apply of_real_le_of_real, { apply norm_sub_le }, { exact norm_nonneg _ }, { exact norm_nonneg _ } end ... ≤ (ennreal.of_real (bound a)) + (ennreal.of_real (bound a)) : add_le_add h₁ h₂ ... = b a : by rw ← two_mul end, /- On the other hand, `F n a --> f a` implies that `‖F n a - f a‖ --> 0` -/ have h : ∀ᵐ a ∂μ, tendsto (λ n, ennreal.of_real ‖F n a - f a‖) at_top (𝓝 0), begin rw ← ennreal.of_real_zero, refine h_lim.mono (λ a h, (continuous_of_real.tendsto _).comp _), rwa ← tendsto_iff_norm_tendsto_zero end, /- Therefore, by the dominated convergence theorem for nonnegative integration, have ` ∫ ‖f a - F n a‖ --> 0 ` -/ begin suffices h : tendsto (λn, ∫⁻ a, (ennreal.of_real ‖F n a - f a‖) ∂μ) at_top (𝓝 (∫⁻ (a:α), 0 ∂μ)), { rwa lintegral_zero at h }, -- Using the dominated convergence theorem. refine tendsto_lintegral_of_dominated_convergence' _ _ hb _ _, -- Show `λa, ‖f a - F n a‖` is almost everywhere measurable for all `n` { exact λ n, measurable_of_real.comp_ae_measurable ((F_measurable n).sub f_measurable).norm.ae_measurable }, -- Show `2 * bound` is has_finite_integral { rw has_finite_integral_iff_of_real at bound_has_finite_integral, { calc ∫⁻ a, b a ∂μ = 2 * ∫⁻ a, ennreal.of_real (bound a) ∂μ : by { rw lintegral_const_mul', exact coe_ne_top } ... ≠ ∞ : mul_ne_top coe_ne_top bound_has_finite_integral.ne }, filter_upwards [h_bound 0] with _ h using le_trans (norm_nonneg _) h }, -- Show `‖f a - F n a‖ --> 0` { exact h } end end dominated_convergence section pos_part /-! Lemmas used for defining the positive part of a `L¹` function -/ lemma has_finite_integral.max_zero {f : α → ℝ} (hf : has_finite_integral f μ) : has_finite_integral (λa, max (f a) 0) μ := hf.mono $ eventually_of_forall $ λ x, by simp [abs_le, le_abs_self] lemma has_finite_integral.min_zero {f : α → ℝ} (hf : has_finite_integral f μ) : has_finite_integral (λa, min (f a) 0) μ := hf.mono $ eventually_of_forall $ λ x, by simp [abs_le, neg_le, neg_le_abs_self, abs_eq_max_neg, le_total] end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma has_finite_integral.smul (c : 𝕜) {f : α → β} : has_finite_integral f μ → has_finite_integral (c • f) μ := begin simp only [has_finite_integral], assume hfi, calc ∫⁻ (a : α), ‖c • f a‖₊ ∂μ = ∫⁻ (a : α), (‖c‖₊) * ‖f a‖₊ ∂μ : by simp only [nnnorm_smul, ennreal.coe_mul] ... < ∞ : begin rw lintegral_const_mul', exacts [mul_lt_top coe_ne_top hfi.ne, coe_ne_top] end end lemma has_finite_integral_smul_iff {c : 𝕜} (hc : c ≠ 0) (f : α → β) : has_finite_integral (c • f) μ ↔ has_finite_integral f μ := begin split, { assume h, simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.smul c⁻¹ }, exact has_finite_integral.smul _ end lemma has_finite_integral.const_mul {f : α → ℝ} (h : has_finite_integral f μ) (c : ℝ) : has_finite_integral (λ x, c * f x) μ := (has_finite_integral.smul c h : _) lemma has_finite_integral.mul_const {f : α → ℝ} (h : has_finite_integral f μ) (c : ℝ) : has_finite_integral (λ x, f x * c) μ := by simp_rw [mul_comm, h.const_mul _] end normed_space /-! ### The predicate `integrable` -/ -- variables [measurable_space β] [measurable_space γ] [measurable_space δ] /-- `integrable f μ` means that `f` is measurable and that the integral `∫⁻ a, ‖f a‖ ∂μ` is finite. `integrable f` means `integrable f volume`. -/ def integrable {α} {m : measurable_space α} (f : α → β) (μ : measure α . volume_tac) : Prop := ae_strongly_measurable f μ ∧ has_finite_integral f μ lemma mem_ℒp_one_iff_integrable {f : α → β} : mem_ℒp f 1 μ ↔ integrable f μ := by simp_rw [integrable, has_finite_integral, mem_ℒp, snorm_one_eq_lintegral_nnnorm] lemma integrable.ae_strongly_measurable {f : α → β} (hf : integrable f μ) : ae_strongly_measurable f μ := hf.1 lemma integrable.ae_measurable [measurable_space β] [borel_space β] {f : α → β} (hf : integrable f μ) : ae_measurable f μ := hf.ae_strongly_measurable.ae_measurable lemma integrable.has_finite_integral {f : α → β} (hf : integrable f μ) : has_finite_integral f μ := hf.2 lemma integrable.mono {f : α → β} {g : α → γ} (hg : integrable g μ) (hf : ae_strongly_measurable f μ) (h : ∀ᵐ a ∂μ, ‖f a‖ ≤ ‖g a‖) : integrable f μ := ⟨hf, hg.has_finite_integral.mono h⟩ lemma integrable.mono' {f : α → β} {g : α → ℝ} (hg : integrable g μ) (hf : ae_strongly_measurable f μ) (h : ∀ᵐ a ∂μ, ‖f a‖ ≤ g a) : integrable f μ := ⟨hf, hg.has_finite_integral.mono' h⟩ lemma integrable.congr' {f : α → β} {g : α → γ} (hf : integrable f μ) (hg : ae_strongly_measurable g μ) (h : ∀ᵐ a ∂μ, ‖f a‖ = ‖g a‖) : integrable g μ := ⟨hg, hf.has_finite_integral.congr' h⟩ lemma integrable_congr' {f : α → β} {g : α → γ} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (h : ∀ᵐ a ∂μ, ‖f a‖ = ‖g a‖) : integrable f μ ↔ integrable g μ := ⟨λ h2f, h2f.congr' hg h, λ h2g, h2g.congr' hf $ eventually_eq.symm h⟩ lemma integrable.congr {f g : α → β} (hf : integrable f μ) (h : f =ᵐ[μ] g) : integrable g μ := ⟨hf.1.congr h, hf.2.congr h⟩ lemma integrable_congr {f g : α → β} (h : f =ᵐ[μ] g) : integrable f μ ↔ integrable g μ := ⟨λ hf, hf.congr h, λ hg, hg.congr h.symm⟩ lemma integrable_const_iff {c : β} : integrable (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ∞ := begin have : ae_strongly_measurable (λ (x : α), c) μ := ae_strongly_measurable_const, rw [integrable, and_iff_right this, has_finite_integral_const_iff] end lemma integrable_const [is_finite_measure μ] (c : β) : integrable (λ x : α, c) μ := integrable_const_iff.2 $ or.inr $ measure_lt_top _ _ lemma mem_ℒp.integrable_norm_rpow {f : α → β} {p : ℝ≥0∞} (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : integrable (λ (x : α), ‖f x‖ ^ p.to_real) μ := begin rw ← mem_ℒp_one_iff_integrable, exact hf.norm_rpow hp_ne_zero hp_ne_top, end lemma mem_ℒp.integrable_norm_rpow' [is_finite_measure μ] {f : α → β} {p : ℝ≥0∞} (hf : mem_ℒp f p μ) : integrable (λ (x : α), ‖f x‖ ^ p.to_real) μ := begin by_cases h_zero : p = 0, { simp [h_zero, integrable_const] }, by_cases h_top : p = ∞, { simp [h_top, integrable_const] }, exact hf.integrable_norm_rpow h_zero h_top end lemma integrable.mono_measure {f : α → β} (h : integrable f ν) (hμ : μ ≤ ν) : integrable f μ := ⟨h.ae_strongly_measurable.mono_measure hμ, h.has_finite_integral.mono_measure hμ⟩ lemma integrable.of_measure_le_smul {μ' : measure α} (c : ℝ≥0∞) (hc : c ≠ ∞) (hμ'_le : μ' ≤ c • μ) {f : α → β} (hf : integrable f μ) : integrable f μ' := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.of_measure_le_smul c hc hμ'_le, } lemma integrable.add_measure {f : α → β} (hμ : integrable f μ) (hν : integrable f ν) : integrable f (μ + ν) := begin simp_rw ← mem_ℒp_one_iff_integrable at hμ hν ⊢, refine ⟨hμ.ae_strongly_measurable.add_measure hν.ae_strongly_measurable, _⟩, rw [snorm_one_add_measure, ennreal.add_lt_top], exact ⟨hμ.snorm_lt_top, hν.snorm_lt_top⟩, end lemma integrable.left_of_add_measure {f : α → β} (h : integrable f (μ + ν)) : integrable f μ := by { rw ← mem_ℒp_one_iff_integrable at h ⊢, exact h.left_of_add_measure, } lemma integrable.right_of_add_measure {f : α → β} (h : integrable f (μ + ν)) : integrable f ν := by { rw ← mem_ℒp_one_iff_integrable at h ⊢, exact h.right_of_add_measure, } @[simp] lemma integrable_add_measure {f : α → β} : integrable f (μ + ν) ↔ integrable f μ ∧ integrable f ν := ⟨λ h, ⟨h.left_of_add_measure, h.right_of_add_measure⟩, λ h, h.1.add_measure h.2⟩ @[simp] lemma integrable_zero_measure {m : measurable_space α} {f : α → β} : integrable f (0 : measure α) := ⟨ae_strongly_measurable_zero_measure f, has_finite_integral_zero_measure f⟩ theorem integrable_finset_sum_measure {ι} {m : measurable_space α} {f : α → β} {μ : ι → measure α} {s : finset ι} : integrable f (∑ i in s, μ i) ↔ ∀ i ∈ s, integrable f (μ i) := by induction s using finset.induction_on; simp [*] lemma integrable.smul_measure {f : α → β} (h : integrable f μ) {c : ℝ≥0∞} (hc : c ≠ ∞) : integrable f (c • μ) := by { rw ← mem_ℒp_one_iff_integrable at h ⊢, exact h.smul_measure hc, } lemma integrable_smul_measure {f : α → β} {c : ℝ≥0∞} (h₁ : c ≠ 0) (h₂ : c ≠ ∞) : integrable f (c • μ) ↔ integrable f μ := ⟨λ h, by simpa only [smul_smul, ennreal.inv_mul_cancel h₁ h₂, one_smul] using h.smul_measure (ennreal.inv_ne_top.2 h₁), λ h, h.smul_measure h₂⟩ lemma integrable_inv_smul_measure {f : α → β} {c : ℝ≥0∞} (h₁ : c ≠ 0) (h₂ : c ≠ ∞) : integrable f (c⁻¹ • μ) ↔ integrable f μ := integrable_smul_measure (by simpa using h₂) (by simpa using h₁) lemma integrable.to_average {f : α → β} (h : integrable f μ) : integrable f ((μ univ)⁻¹ • μ) := begin rcases eq_or_ne μ 0 with rfl|hne, { rwa smul_zero }, { apply h.smul_measure, simpa } end lemma integrable_average [is_finite_measure μ] {f : α → β} : integrable f ((μ univ)⁻¹ • μ) ↔ integrable f μ := (eq_or_ne μ 0).by_cases (λ h, by simp [h]) $ λ h, integrable_smul_measure (ennreal.inv_ne_zero.2 $ measure_ne_top _ _) (ennreal.inv_ne_top.2 $ mt measure.measure_univ_eq_zero.1 h) lemma integrable_map_measure {f : α → δ} {g : δ → β} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : integrable g (measure.map f μ) ↔ integrable (g ∘ f) μ := by { simp_rw ← mem_ℒp_one_iff_integrable, exact mem_ℒp_map_measure_iff hg hf, } lemma integrable.comp_ae_measurable {f : α → δ} {g : δ → β} (hg : integrable g (measure.map f μ)) (hf : ae_measurable f μ) : integrable (g ∘ f) μ := (integrable_map_measure hg.ae_strongly_measurable hf).mp hg lemma integrable.comp_measurable {f : α → δ} {g : δ → β} (hg : integrable g (measure.map f μ)) (hf : measurable f) : integrable (g ∘ f) μ := hg.comp_ae_measurable hf.ae_measurable lemma _root_.measurable_embedding.integrable_map_iff {f : α → δ} (hf : measurable_embedding f) {g : δ → β} : integrable g (measure.map f μ) ↔ integrable (g ∘ f) μ := by { simp_rw ← mem_ℒp_one_iff_integrable, exact hf.mem_ℒp_map_measure_iff, } lemma integrable_map_equiv (f : α ≃ᵐ δ) (g : δ → β) : integrable g (measure.map f μ) ↔ integrable (g ∘ f) μ := by { simp_rw ← mem_ℒp_one_iff_integrable, exact f.mem_ℒp_map_measure_iff, } lemma measure_preserving.integrable_comp {ν : measure δ} {g : δ → β} {f : α → δ} (hf : measure_preserving f μ ν) (hg : ae_strongly_measurable g ν) : integrable (g ∘ f) μ ↔ integrable g ν := by { rw ← hf.map_eq at hg ⊢, exact (integrable_map_measure hg hf.measurable.ae_measurable).symm } lemma measure_preserving.integrable_comp_emb {f : α → δ} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) {g : δ → β} : integrable (g ∘ f) μ ↔ integrable g ν := h₁.map_eq ▸ iff.symm h₂.integrable_map_iff lemma lintegral_edist_lt_top {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : ∫⁻ a, edist (f a) (g a) ∂μ < ∞ := lt_of_le_of_lt (lintegral_edist_triangle hf.ae_strongly_measurable ae_strongly_measurable_zero) (ennreal.add_lt_top.2 $ by { simp_rw [pi.zero_apply, ← has_finite_integral_iff_edist], exact ⟨hf.has_finite_integral, hg.has_finite_integral⟩ }) variables (α β μ) @[simp] lemma integrable_zero : integrable (λ _, (0 : β)) μ := by simp [integrable, ae_strongly_measurable_const] variables {α β μ} lemma integrable.add' {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : has_finite_integral (f + g) μ := calc ∫⁻ a, ‖f a + g a‖₊ ∂μ ≤ ∫⁻ a, ‖f a‖₊ + ‖g a‖₊ ∂μ : lintegral_mono (λ a, by exact_mod_cast nnnorm_add_le _ _) ... = _ : lintegral_nnnorm_add_left hf.ae_strongly_measurable _ ... < ∞ : add_lt_top.2 ⟨hf.has_finite_integral, hg.has_finite_integral⟩ lemma integrable.add {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f + g) μ := ⟨hf.ae_strongly_measurable.add hg.ae_strongly_measurable, hf.add' hg⟩ lemma integrable_finset_sum' {ι} (s : finset ι) {f : ι → α → β} (hf : ∀ i ∈ s, integrable (f i) μ) : integrable (∑ i in s, f i) μ := finset.sum_induction f (λ g, integrable g μ) (λ _ _, integrable.add) (integrable_zero _ _ _) hf lemma integrable_finset_sum {ι} (s : finset ι) {f : ι → α → β} (hf : ∀ i ∈ s, integrable (f i) μ) : integrable (λ a, ∑ i in s, f i a) μ := by simpa only [← finset.sum_apply] using integrable_finset_sum' s hf lemma integrable.neg {f : α → β} (hf : integrable f μ) : integrable (-f) μ := ⟨hf.ae_strongly_measurable.neg, hf.has_finite_integral.neg⟩ @[simp] lemma integrable_neg_iff {f : α → β} : integrable (-f) μ ↔ integrable f μ := ⟨λ h, neg_neg f ▸ h.neg, integrable.neg⟩ lemma integrable.sub {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f - g) μ := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma integrable.norm {f : α → β} (hf : integrable f μ) : integrable (λ a, ‖f a‖) μ := ⟨hf.ae_strongly_measurable.norm, hf.has_finite_integral.norm⟩ lemma integrable.inf {β} [normed_lattice_add_comm_group β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f ⊓ g) μ := by { rw ← mem_ℒp_one_iff_integrable at hf hg ⊢, exact hf.inf hg, } lemma integrable.sup {β} [normed_lattice_add_comm_group β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f ⊔ g) μ := by { rw ← mem_ℒp_one_iff_integrable at hf hg ⊢, exact hf.sup hg, } lemma integrable.abs {β} [normed_lattice_add_comm_group β] {f : α → β} (hf : integrable f μ) : integrable (λ a, |f a|) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.abs, } lemma integrable.bdd_mul {F : Type*} [normed_division_ring F] {f g : α → F} (hint : integrable g μ) (hm : ae_strongly_measurable f μ) (hfbdd : ∃ C, ∀ x, ‖f x‖ ≤ C) : integrable (λ x, f x * g x) μ := begin casesI is_empty_or_nonempty α with hα hα, { rw μ.eq_zero_of_is_empty, exact integrable_zero_measure }, { refine ⟨hm.mul hint.1, _⟩, obtain ⟨C, hC⟩ := hfbdd, have hCnonneg : 0 ≤ C := le_trans (norm_nonneg _) (hC hα.some), have : (λ x, ‖f x * g x‖₊) ≤ λ x, ⟨C, hCnonneg⟩ * ‖g x‖₊, { intro x, simp only [nnnorm_mul], exact mul_le_mul_of_nonneg_right (hC x) (zero_le _) }, refine lt_of_le_of_lt (lintegral_mono_nnreal this) _, simp only [ennreal.coe_mul], rw lintegral_const_mul' _ _ ennreal.coe_ne_top, exact ennreal.mul_lt_top ennreal.coe_ne_top (ne_of_lt hint.2) }, end /-- Hölder's inequality for integrable functions: the scalar multiplication of an integrable vector-valued function by a scalar function with finite essential supremum is integrable. -/ lemma integrable.ess_sup_smul {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] {f : α → β} (hf : integrable f μ) {g : α → 𝕜} (g_ae_strongly_measurable : ae_strongly_measurable g μ) (ess_sup_g : ess_sup (λ x, (‖g x‖₊ : ℝ≥0∞)) μ ≠ ∞) : integrable (λ (x : α), g x • f x) μ := begin rw ← mem_ℒp_one_iff_integrable at *, refine ⟨g_ae_strongly_measurable.smul hf.1, _⟩, have h : (1:ℝ≥0∞) / 1 = 1 / ∞ + 1 / 1 := by norm_num, have hg' : snorm g ∞ μ ≠ ∞ := by rwa snorm_exponent_top, calc snorm (λ (x : α), g x • f x) 1 μ ≤ _ : measure_theory.snorm_smul_le_mul_snorm hf.1 g_ae_strongly_measurable h ... < ∞ : ennreal.mul_lt_top hg' hf.2.ne, end /-- Hölder's inequality for integrable functions: the scalar multiplication of an integrable scalar-valued function by a vector-value function with finite essential supremum is integrable. -/ lemma integrable.smul_ess_sup {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] {f : α → 𝕜} (hf : integrable f μ) {g : α → β} (g_ae_strongly_measurable : ae_strongly_measurable g μ) (ess_sup_g : ess_sup (λ x, (‖g x‖₊ : ℝ≥0∞)) μ ≠ ∞) : integrable (λ (x : α), f x • g x) μ := begin rw ← mem_ℒp_one_iff_integrable at *, refine ⟨hf.1.smul g_ae_strongly_measurable, _⟩, have h : (1:ℝ≥0∞) / 1 = 1 / 1 + 1 / ∞ := by norm_num, have hg' : snorm g ∞ μ ≠ ∞ := by rwa snorm_exponent_top, calc snorm (λ (x : α), f x • g x) 1 μ ≤ _ : measure_theory.snorm_smul_le_mul_snorm g_ae_strongly_measurable hf.1 h ... < ∞ : ennreal.mul_lt_top hf.2.ne hg', end lemma integrable_norm_iff {f : α → β} (hf : ae_strongly_measurable f μ) : integrable (λa, ‖f a‖) μ ↔ integrable f μ := by simp_rw [integrable, and_iff_right hf, and_iff_right hf.norm, has_finite_integral_norm_iff] lemma integrable_of_norm_sub_le {f₀ f₁ : α → β} {g : α → ℝ} (hf₁_m : ae_strongly_measurable f₁ μ) (hf₀_i : integrable f₀ μ) (hg_i : integrable g μ) (h : ∀ᵐ a ∂μ, ‖f₀ a - f₁ a‖ ≤ g a) : integrable f₁ μ := begin have : ∀ᵐ a ∂μ, ‖f₁ a‖ ≤ ‖f₀ a‖ + g a, { apply h.mono, intros a ha, calc ‖f₁ a‖ ≤ ‖f₀ a‖ + ‖f₀ a - f₁ a‖ : norm_le_insert _ _ ... ≤ ‖f₀ a‖ + g a : add_le_add_left ha _ }, exact integrable.mono' (hf₀_i.norm.add hg_i) hf₁_m this end lemma integrable.prod_mk {f : α → β} {g : α → γ} (hf : integrable f μ) (hg : integrable g μ) : integrable (λ x, (f x, g x)) μ := ⟨hf.ae_strongly_measurable.prod_mk hg.ae_strongly_measurable, (hf.norm.add' hg.norm).mono $ eventually_of_forall $ λ x, calc max ‖f x‖ ‖g x‖ ≤ ‖f x‖ + ‖g x‖ : max_le_add_of_nonneg (norm_nonneg _) (norm_nonneg _) ... ≤ ‖(‖f x‖ + ‖g x‖)‖ : le_abs_self _⟩ lemma mem_ℒp.integrable {q : ℝ≥0∞} (hq1 : 1 ≤ q) {f : α → β} [is_finite_measure μ] (hfq : mem_ℒp f q μ) : integrable f μ := mem_ℒp_one_iff_integrable.mp (hfq.mem_ℒp_of_exponent_le hq1) /-- A non-quantitative version of Markov inequality for integrable functions: the measure of points where `‖f x‖ ≥ ε` is finite for all positive `ε`. -/ lemma integrable.measure_ge_lt_top {f : α → β} (hf : integrable f μ) {ε : ℝ} (hε : 0 < ε) : μ {x | ε ≤ ‖f x‖} < ∞ := begin rw show {x | ε ≤ ‖f x‖} = {x | ennreal.of_real ε ≤ ‖f x‖₊}, by simp only [ennreal.of_real, real.to_nnreal_le_iff_le_coe, ennreal.coe_le_coe, coe_nnnorm], refine (meas_ge_le_mul_pow_snorm μ one_ne_zero ennreal.one_ne_top hf.1 _).trans_lt _, { simpa only [ne.def, ennreal.of_real_eq_zero, not_le] using hε }, apply ennreal.mul_lt_top, { simpa only [ennreal.one_to_real, ennreal.rpow_one, ne.def, ennreal.inv_eq_top, ennreal.of_real_eq_zero, not_le] using hε }, simpa only [ennreal.one_to_real, ennreal.rpow_one] using (mem_ℒp_one_iff_integrable.2 hf).snorm_ne_top, end lemma lipschitz_with.integrable_comp_iff_of_antilipschitz {K K'} {f : α → β} {g : β → γ} (hg : lipschitz_with K g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : integrable (g ∘ f) μ ↔ integrable f μ := by simp [← mem_ℒp_one_iff_integrable, hg.mem_ℒp_comp_iff_of_antilipschitz hg' g0] lemma integrable.real_to_nnreal {f : α → ℝ} (hf : integrable f μ) : integrable (λ x, ((f x).to_nnreal : ℝ)) μ := begin refine ⟨hf.ae_strongly_measurable.ae_measurable .real_to_nnreal.coe_nnreal_real.ae_strongly_measurable, _⟩, rw has_finite_integral_iff_norm, refine lt_of_le_of_lt _ ((has_finite_integral_iff_norm _).1 hf.has_finite_integral), apply lintegral_mono, assume x, simp [ennreal.of_real_le_of_real, abs_le, le_abs_self], end lemma of_real_to_real_ae_eq {f : α → ℝ≥0∞} (hf : ∀ᵐ x ∂μ, f x < ∞) : (λ x, ennreal.of_real (f x).to_real) =ᵐ[μ] f := begin filter_upwards [hf], assume x hx, simp only [hx.ne, of_real_to_real, ne.def, not_false_iff], end lemma coe_to_nnreal_ae_eq {f : α → ℝ≥0∞} (hf : ∀ᵐ x ∂μ, f x < ∞) : (λ x, ((f x).to_nnreal : ℝ≥0∞)) =ᵐ[μ] f := begin filter_upwards [hf], assume x hx, simp only [hx.ne, ne.def, not_false_iff, coe_to_nnreal], end section variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] lemma integrable_with_density_iff_integrable_coe_smul {f : α → ℝ≥0} (hf : measurable f) {g : α → E} : integrable g (μ.with_density (λ x, f x)) ↔ integrable (λ x, (f x : ℝ) • g x) μ := begin by_cases H : ae_strongly_measurable (λ (x : α), (f x : ℝ) • g x) μ, { simp only [integrable, ae_strongly_measurable_with_density_iff hf, has_finite_integral, H, true_and], rw lintegral_with_density_eq_lintegral_mul₀' hf.coe_nnreal_ennreal.ae_measurable, { congr', ext1 x, simp only [nnnorm_smul, nnreal.nnnorm_eq, coe_mul, pi.mul_apply] }, { rw ae_measurable_with_density_ennreal_iff hf, convert H.ennnorm, ext1 x, simp only [nnnorm_smul, nnreal.nnnorm_eq, coe_mul] } }, { simp only [integrable, ae_strongly_measurable_with_density_iff hf, H, false_and] } end lemma integrable_with_density_iff_integrable_smul {f : α → ℝ≥0} (hf : measurable f) {g : α → E} : integrable g (μ.with_density (λ x, f x)) ↔ integrable (λ x, f x • g x) μ := integrable_with_density_iff_integrable_coe_smul hf lemma integrable_with_density_iff_integrable_smul' {f : α → ℝ≥0∞} (hf : measurable f) (hflt : ∀ᵐ x ∂μ, f x < ∞) {g : α → E} : integrable g (μ.with_density f) ↔ integrable (λ x, (f x).to_real • g x) μ := begin rw [← with_density_congr_ae (coe_to_nnreal_ae_eq hflt), integrable_with_density_iff_integrable_smul], { refl }, { exact hf.ennreal_to_nnreal }, end lemma integrable_with_density_iff_integrable_coe_smul₀ {f : α → ℝ≥0} (hf : ae_measurable f μ) {g : α → E} : integrable g (μ.with_density (λ x, f x)) ↔ integrable (λ x, (f x : ℝ) • g x) μ := calc integrable g (μ.with_density (λ x, f x)) ↔ integrable g (μ.with_density (λ x, hf.mk f x)) : begin suffices : (λ x, (f x : ℝ≥0∞)) =ᵐ[μ] (λ x, hf.mk f x), by rw with_density_congr_ae this, filter_upwards [hf.ae_eq_mk] with x hx, simp [hx], end ... ↔ integrable (λ x, (hf.mk f x : ℝ) • g x) μ : integrable_with_density_iff_integrable_coe_smul hf.measurable_mk ... ↔ integrable (λ x, (f x : ℝ) • g x) μ : begin apply integrable_congr, filter_upwards [hf.ae_eq_mk] with x hx, simp [hx], end lemma integrable_with_density_iff_integrable_smul₀ {f : α → ℝ≥0} (hf : ae_measurable f μ) {g : α → E} : integrable g (μ.with_density (λ x, f x)) ↔ integrable (λ x, f x • g x) μ := integrable_with_density_iff_integrable_coe_smul₀ hf end lemma integrable_with_density_iff {f : α → ℝ≥0∞} (hf : measurable f) (hflt : ∀ᵐ x ∂μ, f x < ∞) {g : α → ℝ} : integrable g (μ.with_density f) ↔ integrable (λ x, g x * (f x).to_real) μ := begin have : (λ x, g x * (f x).to_real) = (λ x, (f x).to_real • g x), by simp [mul_comm], rw this, exact integrable_with_density_iff_integrable_smul' hf hflt, end section variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] lemma mem_ℒ1_smul_of_L1_with_density {f : α → ℝ≥0} (f_meas : measurable f) (u : Lp E 1 (μ.with_density (λ x, f x))) : mem_ℒp (λ x, f x • u x) 1 μ := mem_ℒp_one_iff_integrable.2 $ (integrable_with_density_iff_integrable_smul f_meas).1 $ mem_ℒp_one_iff_integrable.1 (Lp.mem_ℒp u) variable (μ) /-- The map `u ↦ f • u` is an isometry between the `L^1` spaces for `μ.with_density f` and `μ`. -/ noncomputable def with_density_smul_li {f : α → ℝ≥0} (f_meas : measurable f) : Lp E 1 (μ.with_density (λ x, f x)) →ₗᵢ[ℝ] Lp E 1 μ := { to_fun := λ u, (mem_ℒ1_smul_of_L1_with_density f_meas u).to_Lp _, map_add' := begin assume u v, ext1, filter_upwards [(mem_ℒ1_smul_of_L1_with_density f_meas u).coe_fn_to_Lp, (mem_ℒ1_smul_of_L1_with_density f_meas v).coe_fn_to_Lp, (mem_ℒ1_smul_of_L1_with_density f_meas (u + v)).coe_fn_to_Lp, Lp.coe_fn_add ((mem_ℒ1_smul_of_L1_with_density f_meas u).to_Lp _) ((mem_ℒ1_smul_of_L1_with_density f_meas v).to_Lp _), (ae_with_density_iff f_meas.coe_nnreal_ennreal).1 (Lp.coe_fn_add u v)], assume x hu hv huv h' h'', rw [huv, h', pi.add_apply, hu, hv], rcases eq_or_ne (f x) 0 with hx|hx, { simp only [hx, zero_smul, add_zero] }, { rw [h'' _, pi.add_apply, smul_add], simpa only [ne.def, ennreal.coe_eq_zero] using hx } end, map_smul' := begin assume r u, ext1, filter_upwards [(ae_with_density_iff f_meas.coe_nnreal_ennreal).1 (Lp.coe_fn_smul r u), (mem_ℒ1_smul_of_L1_with_density f_meas (r • u)).coe_fn_to_Lp, Lp.coe_fn_smul r ((mem_ℒ1_smul_of_L1_with_density f_meas u).to_Lp _), (mem_ℒ1_smul_of_L1_with_density f_meas u).coe_fn_to_Lp], assume x h h' h'' h''', rw [ring_hom.id_apply, h', h'', pi.smul_apply, h'''], rcases eq_or_ne (f x) 0 with hx|hx, { simp only [hx, zero_smul, smul_zero] }, { rw [h _, smul_comm, pi.smul_apply], simpa only [ne.def, ennreal.coe_eq_zero] using hx } end, norm_map' := begin assume u, simp only [snorm, linear_map.coe_mk, Lp.norm_to_Lp, one_ne_zero, ennreal.one_ne_top, ennreal.one_to_real, if_false, snorm', ennreal.rpow_one, _root_.div_one, Lp.norm_def], rw lintegral_with_density_eq_lintegral_mul_non_measurable _ f_meas.coe_nnreal_ennreal (filter.eventually_of_forall (λ x, ennreal.coe_lt_top)), congr' 1, apply lintegral_congr_ae, filter_upwards [(mem_ℒ1_smul_of_L1_with_density f_meas u).coe_fn_to_Lp] with x hx, rw [hx, pi.mul_apply], change ↑‖(f x : ℝ) • u x‖₊ = ↑(f x) * ↑‖u x‖₊, simp only [nnnorm_smul, nnreal.nnnorm_eq, ennreal.coe_mul], end } @[simp] lemma with_density_smul_li_apply {f : α → ℝ≥0} (f_meas : measurable f) (u : Lp E 1 (μ.with_density (λ x, f x))) : with_density_smul_li μ f_meas u = (mem_ℒ1_smul_of_L1_with_density f_meas u).to_Lp (λ x, f x • u x) := rfl end lemma mem_ℒ1_to_real_of_lintegral_ne_top {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) : mem_ℒp (λ x, (f x).to_real) 1 μ := begin rw [mem_ℒp, snorm_one_eq_lintegral_nnnorm], exact ⟨(ae_measurable.ennreal_to_real hfm).ae_strongly_measurable, has_finite_integral_to_real_of_lintegral_ne_top hfi⟩ end lemma integrable_to_real_of_lintegral_ne_top {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) : integrable (λ x, (f x).to_real) μ := mem_ℒp_one_iff_integrable.1 $ mem_ℒ1_to_real_of_lintegral_ne_top hfm hfi section pos_part /-! ### Lemmas used for defining the positive part of a `L¹` function -/ lemma integrable.pos_part {f : α → ℝ} (hf : integrable f μ) : integrable (λ a, max (f a) 0) μ := ⟨(hf.ae_strongly_measurable.ae_measurable.max ae_measurable_const).ae_strongly_measurable, hf.has_finite_integral.max_zero⟩ lemma integrable.neg_part {f : α → ℝ} (hf : integrable f μ) : integrable (λ a, max (-f a) 0) μ := hf.neg.pos_part end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul (c : 𝕜) {f : α → β} (hf : integrable f μ) : integrable (c • f) μ := ⟨hf.ae_strongly_measurable.const_smul c, hf.has_finite_integral.smul c⟩ lemma integrable_smul_iff {c : 𝕜} (hc : c ≠ 0) (f : α → β) : integrable (c • f) μ ↔ integrable f μ := and_congr (ae_strongly_measurable_const_smul_iff₀ hc) (has_finite_integral_smul_iff hc f) lemma integrable.smul_of_top_right {f : α → β} {φ : α → 𝕜} (hf : integrable f μ) (hφ : mem_ℒp φ ∞ μ) : integrable (φ • f) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact mem_ℒp.smul_of_top_right hf hφ } lemma integrable.smul_of_top_left {f : α → β} {φ : α → 𝕜} (hφ : integrable φ μ) (hf : mem_ℒp f ∞ μ) : integrable (φ • f) μ := by { rw ← mem_ℒp_one_iff_integrable at hφ ⊢, exact mem_ℒp.smul_of_top_left hf hφ } lemma integrable.smul_const {f : α → 𝕜} (hf : integrable f μ) (c : β) : integrable (λ x, f x • c) μ := hf.smul_of_top_left (mem_ℒp_top_const c) end normed_space section normed_space_over_complete_field variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [complete_space 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] lemma integrable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) : integrable (λ x, f x • c) μ ↔ integrable f μ := begin simp_rw [integrable, ae_strongly_measurable_smul_const_iff hc, and.congr_right_iff, has_finite_integral, nnnorm_smul, ennreal.coe_mul], intro hf, rw [lintegral_mul_const' _ _ ennreal.coe_ne_top, ennreal.mul_lt_top_iff], have : ∀ x : ℝ≥0∞, x = 0 → x < ∞ := by simp, simp [hc, or_iff_left_of_imp (this _)] end end normed_space_over_complete_field section is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {f : α → 𝕜} lemma integrable.const_mul {f : α → 𝕜} (h : integrable f μ) (c : 𝕜) : integrable (λ x, c * f x) μ := integrable.smul c h lemma integrable.const_mul' {f : α → 𝕜} (h : integrable f μ) (c : 𝕜) : integrable ((λ (x : α), c) * f) μ := integrable.smul c h lemma integrable.mul_const {f : α → 𝕜} (h : integrable f μ) (c : 𝕜) : integrable (λ x, f x * c) μ := by simp_rw [mul_comm, h.const_mul _] lemma integrable.mul_const' {f : α → 𝕜} (h : integrable f μ) (c : 𝕜) : integrable (f * (λ (x : α), c)) μ := integrable.mul_const h c lemma integrable.div_const {f : α → 𝕜} (h : integrable f μ) (c : 𝕜) : integrable (λ x, f x / c) μ := by simp_rw [div_eq_mul_inv, h.mul_const] lemma integrable.bdd_mul' {f g : α → 𝕜} {c : ℝ} (hg : integrable g μ) (hf : ae_strongly_measurable f μ) (hf_bound : ∀ᵐ x ∂μ, ‖f x‖ ≤ c) : integrable (λ x, f x * g x) μ := begin refine integrable.mono' (hg.norm.smul c) (hf.mul hg.1) _, filter_upwards [hf_bound] with x hx, rw [pi.smul_apply, smul_eq_mul], exact (norm_mul_le _ _).trans (mul_le_mul_of_nonneg_right hx (norm_nonneg _)), end lemma integrable.of_real {f : α → ℝ} (hf : integrable f μ) : integrable (λ x, (f x : 𝕜)) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.of_real } lemma integrable.re_im_iff : integrable (λ x, is_R_or_C.re (f x)) μ ∧ integrable (λ x, is_R_or_C.im (f x)) μ ↔ integrable f μ := by { simp_rw ← mem_ℒp_one_iff_integrable, exact mem_ℒp_re_im_iff } lemma integrable.re (hf : integrable f μ) : integrable (λ x, is_R_or_C.re (f x)) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.re, } lemma integrable.im (hf : integrable f μ) : integrable (λ x, is_R_or_C.im (f x)) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.im, } end is_R_or_C section inner_product variables {𝕜 E : Type*} variables [is_R_or_C 𝕜] [normed_add_comm_group E] [inner_product_space 𝕜 E] {f : α → E} local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y lemma integrable.const_inner (c : E) (hf : integrable f μ) : integrable (λ x, ⟪c, f x⟫) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.const_inner c, } lemma integrable.inner_const (hf : integrable f μ) (c : E) : integrable (λ x, ⟪f x, c⟫) μ := by { rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact hf.inner_const c, } end inner_product section trim variables {H : Type*} [normed_add_comm_group H] {m0 : measurable_space α} {μ' : measure α} {f : α → H} lemma integrable.trim (hm : m ≤ m0) (hf_int : integrable f μ') (hf : strongly_measurable[m] f) : integrable f (μ'.trim hm) := begin refine ⟨hf.ae_strongly_measurable, _⟩, rw [has_finite_integral, lintegral_trim hm _], { exact hf_int.2, }, { exact @strongly_measurable.ennnorm _ m _ _ f hf }, end lemma integrable_of_integrable_trim (hm : m ≤ m0) (hf_int : integrable f (μ'.trim hm)) : integrable f μ' := begin obtain ⟨hf_meas_ae, hf⟩ := hf_int, refine ⟨ae_strongly_measurable_of_ae_strongly_measurable_trim hm hf_meas_ae, _⟩, rw has_finite_integral at hf ⊢, rwa lintegral_trim_ae hm _ at hf, exact ae_strongly_measurable.ennnorm hf_meas_ae end end trim section sigma_finite variables {E : Type*} {m0 : measurable_space α} [normed_add_comm_group E] lemma integrable_of_forall_fin_meas_le' {μ : measure α} (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (C : ℝ≥0∞) (hC : C < ∞) {f : α → E} (hf_meas : ae_strongly_measurable f μ) (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, ‖f x‖₊ ∂μ ≤ C) : integrable f μ := ⟨hf_meas, (lintegral_le_of_forall_fin_meas_le' hm C hf_meas.ennnorm hf).trans_lt hC⟩ lemma integrable_of_forall_fin_meas_le [sigma_finite μ] (C : ℝ≥0∞) (hC : C < ∞) {f : α → E} (hf_meas : ae_strongly_measurable f μ) (hf : ∀ s : set α, measurable_set s → μ s ≠ ∞ → ∫⁻ x in s, ‖f x‖₊ ∂μ ≤ C) : integrable f μ := @integrable_of_forall_fin_meas_le' _ _ _ _ _ _ _ (by rwa trim_eq_self) C hC _ hf_meas hf end sigma_finite /-! ### The predicate `integrable` on measurable functions modulo a.e.-equality -/ namespace ae_eq_fun section /-- A class of almost everywhere equal functions is `integrable` if its function representative is integrable. -/ def integrable (f : α →ₘ[μ] β) : Prop := integrable f μ lemma integrable_mk {f : α → β} (hf : ae_strongly_measurable f μ ) : (integrable (mk f hf : α →ₘ[μ] β)) ↔ measure_theory.integrable f μ := begin simp [integrable], apply integrable_congr, exact coe_fn_mk f hf end lemma integrable_coe_fn {f : α →ₘ[μ] β} : (measure_theory.integrable f μ) ↔ integrable f := by rw [← integrable_mk, mk_coe_fn] lemma integrable_zero : integrable (0 : α →ₘ[μ] β) := (integrable_zero α β μ).congr (coe_fn_mk _ _).symm end section lemma integrable.neg {f : α →ₘ[μ] β} : integrable f → integrable (-f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 ((integrable_mk hfm).1 hfi).neg section lemma integrable_iff_mem_L1 {f : α →ₘ[μ] β} : integrable f ↔ f ∈ (α →₁[μ] β) := by rw [← integrable_coe_fn, ← mem_ℒp_one_iff_integrable, Lp.mem_Lp_iff_mem_ℒp] lemma integrable.add {f g : α →ₘ[μ] β} : integrable f → integrable g → integrable (f + g) := begin refine induction_on₂ f g (λ f hf g hg hfi hgi, _), simp only [integrable_mk, mk_add_mk] at hfi hgi ⊢, exact hfi.add hgi end lemma integrable.sub {f g : α →ₘ[μ] β} (hf : integrable f) (hg : integrable g) : integrable (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg end section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul {c : 𝕜} {f : α →ₘ[μ] β} : integrable f → integrable (c • f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 $ ((integrable_mk hfm).1 hfi).smul _ end normed_space end end ae_eq_fun namespace L1 lemma integrable_coe_fn (f : α →₁[μ] β) : integrable f μ := by { rw ← mem_ℒp_one_iff_integrable, exact Lp.mem_ℒp f } lemma has_finite_integral_coe_fn (f : α →₁[μ] β) : has_finite_integral f μ := (integrable_coe_fn f).has_finite_integral lemma strongly_measurable_coe_fn (f : α →₁[μ] β) : strongly_measurable f := Lp.strongly_measurable f lemma measurable_coe_fn [measurable_space β] [borel_space β] (f : α →₁[μ] β) : measurable f := (Lp.strongly_measurable f).measurable lemma ae_strongly_measurable_coe_fn (f : α →₁[μ] β) : ae_strongly_measurable f μ := Lp.ae_strongly_measurable f lemma ae_measurable_coe_fn [measurable_space β] [borel_space β] (f : α →₁[μ] β) : ae_measurable f μ := (Lp.strongly_measurable f).measurable.ae_measurable lemma edist_def (f g : α →₁[μ] β) : edist f g = ∫⁻ a, edist (f a) (g a) ∂μ := by { simp [Lp.edist_def, snorm, snorm'], simp [edist_eq_coe_nnnorm_sub] } lemma dist_def (f g : α →₁[μ] β) : dist f g = (∫⁻ a, edist (f a) (g a) ∂μ).to_real := by { simp [Lp.dist_def, snorm, snorm'], simp [edist_eq_coe_nnnorm_sub] } lemma norm_def (f : α →₁[μ] β) : ‖f‖ = (∫⁻ a, ‖f a‖₊ ∂μ).to_real := by { simp [Lp.norm_def, snorm, snorm'] } /-- Computing the norm of a difference between two L¹-functions. Note that this is not a special case of `norm_def` since `(f - g) x` and `f x - g x` are not equal (but only a.e.-equal). -/ lemma norm_sub_eq_lintegral (f g : α →₁[μ] β) : ‖f - g‖ = (∫⁻ x, (‖f x - g x‖₊ : ℝ≥0∞) ∂μ).to_real := begin rw [norm_def], congr' 1, rw lintegral_congr_ae, filter_upwards [Lp.coe_fn_sub f g] with _ ha, simp only [ha, pi.sub_apply], end lemma of_real_norm_eq_lintegral (f : α →₁[μ] β) : ennreal.of_real ‖f‖ = ∫⁻ x, (‖f x‖₊ : ℝ≥0∞) ∂μ := by { rw [norm_def, ennreal.of_real_to_real], exact ne_of_lt (has_finite_integral_coe_fn f) } /-- Computing the norm of a difference between two L¹-functions. Note that this is not a special case of `of_real_norm_eq_lintegral` since `(f - g) x` and `f x - g x` are not equal (but only a.e.-equal). -/ lemma of_real_norm_sub_eq_lintegral (f g : α →₁[μ] β) : ennreal.of_real ‖f - g‖ = ∫⁻ x, (‖f x - g x‖₊ : ℝ≥0∞) ∂μ := begin simp_rw [of_real_norm_eq_lintegral, ← edist_eq_coe_nnnorm], apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_sub f g] with _ ha, simp only [ha, pi.sub_apply], end end L1 namespace integrable /-- Construct the equivalence class `[f]` of an integrable function `f`, as a member of the space `L1 β 1 μ`. -/ def to_L1 (f : α → β) (hf : integrable f μ) : α →₁[μ] β := (mem_ℒp_one_iff_integrable.2 hf).to_Lp f @[simp] lemma to_L1_coe_fn (f : α →₁[μ] β) (hf : integrable f μ) : hf.to_L1 f = f := by simp [integrable.to_L1] lemma coe_fn_to_L1 {f : α → β} (hf : integrable f μ) : hf.to_L1 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk _ _ @[simp] lemma to_L1_zero (h : integrable (0 : α → β) μ) : h.to_L1 0 = 0 := rfl @[simp] lemma to_L1_eq_mk (f : α → β) (hf : integrable f μ) : (hf.to_L1 f : α →ₘ[μ] β) = ae_eq_fun.mk f hf.ae_strongly_measurable := rfl @[simp] lemma to_L1_eq_to_L1_iff (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : to_L1 f hf = to_L1 g hg ↔ f =ᵐ[μ] g := mem_ℒp.to_Lp_eq_to_Lp_iff _ _ lemma to_L1_add (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f + g) (hf.add hg) = to_L1 f hf + to_L1 g hg := rfl lemma to_L1_neg (f : α → β) (hf : integrable f μ) : to_L1 (- f) (integrable.neg hf) = - to_L1 f hf := rfl lemma to_L1_sub (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f - g) (hf.sub hg) = to_L1 f hf - to_L1 g hg := rfl lemma norm_to_L1 (f : α → β) (hf : integrable f μ) : ‖hf.to_L1 f‖ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := by { simp [to_L1, snorm, snorm'], simp [edist_eq_coe_nnnorm] } lemma norm_to_L1_eq_lintegral_norm (f : α → β) (hf : integrable f μ) : ‖hf.to_L1 f‖ = ennreal.to_real (∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ) := by { rw [norm_to_L1, lintegral_norm_eq_lintegral_edist] } @[simp] lemma edist_to_L1_to_L1 (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : edist (hf.to_L1 f) (hg.to_L1 g) = ∫⁻ a, edist (f a) (g a) ∂μ := by { simp [integrable.to_L1, snorm, snorm'], simp [edist_eq_coe_nnnorm_sub] } @[simp] lemma edist_to_L1_zero (f : α → β) (hf : integrable f μ) : edist (hf.to_L1 f) 0 = ∫⁻ a, edist (f a) 0 ∂μ := by { simp [integrable.to_L1, snorm, snorm'], simp [edist_eq_coe_nnnorm] } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma to_L1_smul (f : α → β) (hf : integrable f μ) (k : 𝕜) : to_L1 (λ a, k • f a) (hf.smul k) = k • to_L1 f hf := rfl lemma to_L1_smul' (f : α → β) (hf : integrable f μ) (k : 𝕜) : to_L1 (k • f) (hf.smul k) = k • to_L1 f hf := rfl end integrable end measure_theory open measure_theory variables {E : Type*} [normed_add_comm_group E] {𝕜 : Type*} [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] {H : Type*} [normed_add_comm_group H] [normed_space 𝕜 H] lemma measure_theory.integrable.apply_continuous_linear_map {φ : α → H →L[𝕜] E} (φ_int : integrable φ μ) (v : H) : integrable (λ a, φ a v) μ := (φ_int.norm.mul_const ‖v‖).mono' (φ_int.ae_strongly_measurable.apply_continuous_linear_map v) (eventually_of_forall $ λ a, (φ a).le_op_norm v) lemma continuous_linear_map.integrable_comp {φ : α → H} (L : H →L[𝕜] E) (φ_int : integrable φ μ) : integrable (λ (a : α), L (φ a)) μ := ((integrable.norm φ_int).const_mul ‖L‖).mono' (L.continuous.comp_ae_strongly_measurable φ_int.ae_strongly_measurable) (eventually_of_forall $ λ a, L.le_op_norm (φ a))
5b7a57576bcf95abe7d398ffa1a3a591b9a71f46
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/lie/classical.lean
211c3dd2e08d720ad2dd46839982061e44400277
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
13,933
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.invertible import algebra.lie.skew_adjoint import algebra.lie.abelian import linear_algebra.matrix.trace /-! # Classical Lie algebras This file is the place to find definitions and basic properties of the classical Lie algebras: * Aₗ = sl(l+1) * Bₗ ≃ so(l+1, l) ≃ so(2l+1) * Cₗ = sp(l) * Dₗ ≃ so(l, l) ≃ so(2l) ## Main definitions * `lie_algebra.special_linear.sl` * `lie_algebra.symplectic.sp` * `lie_algebra.orthogonal.so` * `lie_algebra.orthogonal.so'` * `lie_algebra.orthogonal.so_indefinite_equiv` * `lie_algebra.orthogonal.type_D` * `lie_algebra.orthogonal.type_B` * `lie_algebra.orthogonal.type_D_equiv_so'` * `lie_algebra.orthogonal.type_B_equiv_so'` ## Implementation notes ### Matrices or endomorphisms Given a finite type and a commutative ring, the corresponding square matrices are equivalent to the endomorphisms of the corresponding finite-rank free module as Lie algebras, see `lie_equiv_matrix'`. We can thus define the classical Lie algebras as Lie subalgebras either of matrices or of endomorphisms. We have opted for the former. At the time of writing (August 2020) it is unclear which approach should be preferred so the choice should be assumed to be somewhat arbitrary. ### Diagonal quadratic form or diagonal Cartan subalgebra For the algebras of type `B` and `D`, there are two natural definitions. For example since the the `2l × 2l` matrix: $$ J = \left[\begin{array}{cc} 0_l & 1_l\\ 1_l & 0_l \end{array}\right] $$ defines a symmetric bilinear form equivalent to that defined by the identity matrix `I`, we can define the algebras of type `D` to be the Lie subalgebra of skew-adjoint matrices either for `J` or for `I`. Both definitions have their advantages (in particular the `J`-skew-adjoint matrices define a Lie algebra for which the diagonal matrices form a Cartan subalgebra) and so we provide both. We thus also provide equivalences `type_D_equiv_so'`, `so_indefinite_equiv` which show the two definitions are equivalent. Similarly for the algebras of type `B`. ## Tags classical lie algebra, special linear, symplectic, orthogonal -/ universes u₁ u₂ namespace lie_algebra open_locale matrix variables (n p q l : Type*) (R : Type u₂) variables [fintype n] [fintype l] [fintype p] [fintype q] variables [decidable_eq n] [decidable_eq p] [decidable_eq q] [decidable_eq l] variables [comm_ring R] @[simp] lemma matrix_trace_commutator_zero (X Y : matrix n n R) : matrix.trace n R R ⁅X, Y⁆ = 0 := calc _ = matrix.trace n R R (X ⬝ Y) - matrix.trace n R R (Y ⬝ X) : linear_map.map_sub _ _ _ ... = matrix.trace n R R (X ⬝ Y) - matrix.trace n R R (X ⬝ Y) : congr_arg (λ x, _ - x) (matrix.trace_mul_comm X Y) ... = 0 : sub_self _ namespace special_linear /-- The special linear Lie algebra: square matrices of trace zero. -/ def sl : lie_subalgebra R (matrix n n R) := { lie_mem' := λ X Y _ _, linear_map.mem_ker.2 $ matrix_trace_commutator_zero _ _ _ _, ..linear_map.ker (matrix.trace n R R) } lemma sl_bracket (A B : sl n R) : ⁅A, B⁆.val = A.val ⬝ B.val - B.val ⬝ A.val := rfl section elementary_basis variables {n} (i j : n) /-- It is useful to define these matrices for explicit calculations in sl n R. -/ abbreviation E : matrix n n R := λ i' j', if i = i' ∧ j = j' then 1 else 0 @[simp] lemma E_apply_one : E R i j i j = 1 := if_pos (and.intro rfl rfl) @[simp] lemma E_apply_zero (i' j' : n) (h : ¬(i = i' ∧ j = j')) : E R i j i' j' = 0 := if_neg h @[simp] lemma E_diag_zero (h : j ≠ i) : matrix.diag n R R (E R i j) = 0 := funext $ λ k, if_neg $ λ ⟨e₁, e₂⟩, h (e₂.trans e₁.symm) lemma E_trace_zero (h : j ≠ i) : matrix.trace n R R (E R i j) = 0 := by simp [h] /-- When j ≠ i, the elementary matrices are elements of sl n R, in fact they are part of a natural basis of sl n R. -/ def Eb (h : j ≠ i) : sl n R := ⟨E R i j, show E R i j ∈ linear_map.ker (matrix.trace n R R), from E_trace_zero R i j h⟩ @[simp] lemma Eb_val (h : j ≠ i) : (Eb R i j h).val = E R i j := rfl end elementary_basis lemma sl_non_abelian [nontrivial R] (h : 1 < fintype.card n) : ¬is_lie_abelian ↥(sl n R) := begin rcases fintype.exists_pair_of_one_lt_card h with ⟨j, i, hij⟩, let A := Eb R i j hij, let B := Eb R j i hij.symm, intros c, have c' : A.val ⬝ B.val = B.val ⬝ A.val, by { rw [← sub_eq_zero, ← sl_bracket, c.trivial], refl }, have : (1 : R) = 0 := by simpa [matrix.mul_apply, hij] using (congr_fun (congr_fun c' i) i), exact one_ne_zero this, end end special_linear namespace symplectic /-- The matrix defining the canonical skew-symmetric bilinear form. -/ def J : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 (-1) 1 0 /-- The symplectic Lie algebra: skew-adjoint matrices with respect to the canonical skew-symmetric bilinear form. -/ def sp : lie_subalgebra R (matrix (l ⊕ l) (l ⊕ l) R) := skew_adjoint_matrices_lie_subalgebra (J l R) end symplectic namespace orthogonal /-- The definite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric bilinear form defined by the identity matrix. -/ def so : lie_subalgebra R (matrix n n R) := skew_adjoint_matrices_lie_subalgebra (1 : matrix n n R) @[simp] lemma mem_so (A : matrix n n R) : A ∈ so n R ↔ Aᵀ = -A := begin erw mem_skew_adjoint_matrices_submodule, simp only [matrix.is_skew_adjoint, matrix.is_adjoint_pair, matrix.mul_one, matrix.one_mul], end /-- The indefinite diagonal matrix with `p` 1s and `q` -1s. -/ def indefinite_diagonal : matrix (p ⊕ q) (p ⊕ q) R := matrix.diagonal $ sum.elim (λ _, 1) (λ _, -1) /-- The indefinite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric bilinear form defined by the indefinite diagonal matrix. -/ def so' : lie_subalgebra R (matrix (p ⊕ q) (p ⊕ q) R) := skew_adjoint_matrices_lie_subalgebra $ indefinite_diagonal p q R /-- A matrix for transforming the indefinite diagonal bilinear form into the definite one, provided the parameter `i` is a square root of -1. -/ def Pso (i : R) : matrix (p ⊕ q) (p ⊕ q) R := matrix.diagonal $ sum.elim (λ _, 1) (λ _, i) lemma Pso_inv {i : R} (hi : i*i = -1) : (Pso p q R i) * (Pso p q R (-i)) = 1 := begin ext x y, rcases x; rcases y, { -- x y : p by_cases h : x = y; simp [Pso, indefinite_diagonal, h], }, { -- x : p, y : q simp [Pso, indefinite_diagonal], }, { -- x : q, y : p simp [Pso, indefinite_diagonal], }, { -- x y : q by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], }, end lemma is_unit_Pso {i : R} (hi : i*i = -1) : is_unit (Pso p q R i) := ⟨{ val := Pso p q R i, inv := Pso p q R (-i), val_inv := Pso_inv p q R hi, inv_val := by { apply matrix.nonsing_inv_left_right, exact Pso_inv p q R hi, }, }, rfl⟩ lemma indefinite_diagonal_transform {i : R} (hi : i*i = -1) : (Pso p q R i)ᵀ ⬝ (indefinite_diagonal p q R) ⬝ (Pso p q R i) = 1 := begin ext x y, rcases x; rcases y, { -- x y : p by_cases h : x = y; simp [Pso, indefinite_diagonal, h], }, { -- x : p, y : q simp [Pso, indefinite_diagonal], }, { -- x : q, y : p simp [Pso, indefinite_diagonal], }, { -- x y : q by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], }, end /-- An equivalence between the indefinite and definite orthogonal Lie algebras, over a ring containing a square root of -1. -/ noncomputable def so_indefinite_equiv {i : R} (hi : i*i = -1) : so' p q R ≃ₗ⁅R⁆ so (p ⊕ q) R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (indefinite_diagonal p q R) (Pso p q R i) (is_unit_Pso p q R hi)).trans, apply lie_equiv.of_eq, ext A, rw indefinite_diagonal_transform p q R hi, refl, end lemma so_indefinite_equiv_apply {i : R} (hi : i*i = -1) (A : so' p q R) : (so_indefinite_equiv p q R hi A : matrix (p ⊕ q) (p ⊕ q) R) = (Pso p q R i)⁻¹ ⬝ (A : matrix (p ⊕ q) (p ⊕ q) R) ⬝ (Pso p q R i) := by erw [lie_equiv.trans_apply, lie_equiv.of_eq_apply, skew_adjoint_matrices_lie_subalgebra_equiv_apply] /-- A matrix defining a canonical even-rank symmetric bilinear form. It looks like this as a `2l x 2l` matrix of `l x l` blocks: [ 0 1 ] [ 1 0 ] -/ def JD : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 1 1 0 /-- The classical Lie algebra of type D as a Lie subalgebra of matrices associated to the matrix `JD`. -/ def type_D := skew_adjoint_matrices_lie_subalgebra (JD l R) /-- A matrix transforming the bilinear form defined by the matrix `JD` into a split-signature diagonal matrix. It looks like this as a `2l x 2l` matrix of `l x l` blocks: [ 1 -1 ] [ 1 1 ] -/ def PD : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 1 (-1) 1 1 /-- The split-signature diagonal matrix. -/ def S := indefinite_diagonal l l R lemma S_as_blocks : S l R = matrix.from_blocks 1 0 0 (-1) := begin rw [← matrix.diagonal_one, matrix.diagonal_neg, matrix.from_blocks_diagonal], refl, end lemma JD_transform : (PD l R)ᵀ ⬝ (JD l R) ⬝ (PD l R) = (2 : R) • (S l R) := begin have h : (PD l R)ᵀ ⬝ (JD l R) = matrix.from_blocks 1 1 1 (-1) := by { simp [PD, JD, matrix.from_blocks_transpose, matrix.from_blocks_multiply], }, erw [h, S_as_blocks, matrix.from_blocks_multiply, matrix.from_blocks_smul], congr; simp [two_smul], end lemma PD_inv [invertible (2 : R)] : (PD l R) * (⅟(2 : R) • (PD l R)ᵀ) = 1 := begin have h : ⅟(2 : R) • (1 : matrix l l R) + ⅟(2 : R) • 1 = 1 := by rw [← smul_add, ← (two_smul R _), smul_smul, inv_of_mul_self, one_smul], erw [matrix.from_blocks_transpose, matrix.from_blocks_smul, matrix.mul_eq_mul, matrix.from_blocks_multiply], simp [h], end lemma is_unit_PD [invertible (2 : R)] : is_unit (PD l R) := ⟨{ val := PD l R, inv := ⅟(2 : R) • (PD l R)ᵀ, val_inv := PD_inv l R, inv_val := by { apply matrix.nonsing_inv_left_right, exact PD_inv l R, }, }, rfl⟩ /-- An equivalence between two possible definitions of the classical Lie algebra of type D. -/ noncomputable def type_D_equiv_so' [invertible (2 : R)] : type_D l R ≃ₗ⁅R⁆ so' l l R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (JD l R) (PD l R) (is_unit_PD l R)).trans, apply lie_equiv.of_eq, ext A, rw [JD_transform, ← unit_of_invertible_val (2 : R), ←units.smul_def, lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul], refl, end /-- A matrix defining a canonical odd-rank symmetric bilinear form. It looks like this as a `(2l+1) x (2l+1)` matrix of blocks: [ 2 0 0 ] [ 0 0 1 ] [ 0 1 0 ] where sizes of the blocks are: [`1 x 1` `1 x l` `1 x l`] [`l x 1` `l x l` `l x l`] [`l x 1` `l x l` `l x l`] -/ def JB := matrix.from_blocks ((2 : R) • 1 : matrix unit unit R) 0 0 (JD l R) /-- The classical Lie algebra of type B as a Lie subalgebra of matrices associated to the matrix `JB`. -/ def type_B := skew_adjoint_matrices_lie_subalgebra (JB l R) /-- A matrix transforming the bilinear form defined by the matrix `JB` into an almost-split-signature diagonal matrix. It looks like this as a `(2l+1) x (2l+1)` matrix of blocks: [ 1 0 0 ] [ 0 1 -1 ] [ 0 1 1 ] where sizes of the blocks are: [`1 x 1` `1 x l` `1 x l`] [`l x 1` `l x l` `l x l`] [`l x 1` `l x l` `l x l`] -/ def PB := matrix.from_blocks (1 : matrix unit unit R) 0 0 (PD l R) lemma PB_inv [invertible (2 : R)] : (PB l R) * (matrix.from_blocks 1 0 0 (PD l R)⁻¹) = 1 := begin simp [PB, matrix.from_blocks_multiply, (PD l R).mul_nonsing_inv, is_unit_PD, ← (PD l R).is_unit_iff_is_unit_det] end lemma is_unit_PB [invertible (2 : R)] : is_unit (PB l R) := ⟨{ val := PB l R, inv := matrix.from_blocks 1 0 0 (PD l R)⁻¹, val_inv := PB_inv l R, inv_val := by { apply matrix.nonsing_inv_left_right, exact PB_inv l R, }, }, rfl⟩ lemma JB_transform : (PB l R)ᵀ ⬝ (JB l R) ⬝ (PB l R) = (2 : R) • matrix.from_blocks 1 0 0 (S l R) := by simp [PB, JB, JD_transform, matrix.from_blocks_transpose, matrix.from_blocks_multiply, matrix.from_blocks_smul] lemma indefinite_diagonal_assoc : indefinite_diagonal (unit ⊕ l) l R = matrix.reindex_lie_equiv (equiv.sum_assoc unit l l).symm (matrix.from_blocks 1 0 0 (indefinite_diagonal l l R)) := begin ext i j, rcases i with ⟨⟨i₁ | i₂⟩ | i₃⟩; rcases j with ⟨⟨j₁ | j₂⟩ | j₃⟩; simp only [indefinite_diagonal, matrix.diagonal, equiv.sum_assoc_apply_in1, matrix.reindex_lie_equiv_apply, matrix.minor_apply, equiv.symm_symm, matrix.reindex_apply, sum.elim_inl, if_true, eq_self_iff_true, matrix.one_apply_eq, matrix.from_blocks_apply₁₁, dmatrix.zero_apply, equiv.sum_assoc_apply_in2, if_false, matrix.from_blocks_apply₁₂, matrix.from_blocks_apply₂₁, matrix.from_blocks_apply₂₂, equiv.sum_assoc_apply_in3, sum.elim_inr]; congr, end /-- An equivalence between two possible definitions of the classical Lie algebra of type B. -/ noncomputable def type_B_equiv_so' [invertible (2 : R)] : type_B l R ≃ₗ⁅R⁆ so' (unit ⊕ l) l R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (JB l R) (PB l R) (is_unit_PB l R)).trans, symmetry, apply (skew_adjoint_matrices_lie_subalgebra_equiv_transpose (indefinite_diagonal (unit ⊕ l) l R) (matrix.reindex_alg_equiv (equiv.sum_assoc punit l l)) (matrix.transpose_reindex _ _)).trans, apply lie_equiv.of_eq, ext A, rw [JB_transform, ← unit_of_invertible_val (2 : R), ←units.smul_def, lie_subalgebra.mem_coe, lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul], simpa [indefinite_diagonal_assoc], end end orthogonal end lie_algebra
004efac56d2c3c5e6b795d282734364ef2a8e341
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/category/Top/limits.lean
616f80c16070ff2c60ebe5b8df3b9bebf88072fd
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
40,931
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Scott Morrison, Mario Carneiro, Andrew Yang -/ import topology.category.Top.epi_mono import category_theory.limits.preserves.limits import category_theory.category.ulift import category_theory.limits.shapes.types import category_theory.limits.concrete_category /-! # The category of topological spaces has all limits and colimits Further, these limits and colimits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ open topological_space open category_theory open category_theory.limits open opposite universes u v w noncomputable theory namespace Top variables {J : Type v} [small_category J] local notation `forget` := forget Top /-- A choice of limit cone for a functor `F : J ⥤ Top`. Generally you should just use `limit.cone F`, unless you need the actual definition (which is in terms of `types.limit_cone`). -/ def limit_cone (F : J ⥤ Top.{max v u}) : cone F := { X := Top.of {u : Π j : J, F.obj j | ∀ {i j : J} (f : i ⟶ j), F.map f (u i) = u j}, π := { app := λ j, { to_fun := λ u, u.val j, continuous_to_fun := show continuous ((λ u : Π j : J, F.obj j, u j) ∘ subtype.val), by continuity } } } /-- A choice of limit cone for a functor `F : J ⥤ Top` whose topology is defined as an infimum of topologies infimum. Generally you should just use `limit.cone F`, unless you need the actual definition (which is in terms of `types.limit_cone`). -/ def limit_cone_infi (F : J ⥤ Top.{max v u}) : cone F := { X := ⟨(types.limit_cone (F ⋙ forget)).X, ⨅j, (F.obj j).str.induced ((types.limit_cone (F ⋙ forget)).π.app j)⟩, π := { app := λ j, ⟨(types.limit_cone (F ⋙ forget)).π.app j, continuous_iff_le_induced.mpr (infi_le _ _)⟩, naturality' := λ j j' f, continuous_map.coe_injective ((types.limit_cone (F ⋙ forget)).π.naturality f) } } /-- The chosen cone `Top.limit_cone F` for a functor `F : J ⥤ Top` is a limit cone. Generally you should just use `limit.is_limit F`, unless you need the actual definition (which is in terms of `types.limit_cone_is_limit`). -/ def limit_cone_is_limit (F : J ⥤ Top.{max v u}) : is_limit (limit_cone F) := { lift := λ S, { to_fun := λ x, ⟨λ j, S.π.app _ x, λ i j f, by { dsimp, erw ← S.w f, refl }⟩ }, uniq' := λ S m h, by { ext : 3, simpa [← h] } } /-- The chosen cone `Top.limit_cone_infi F` for a functor `F : J ⥤ Top` is a limit cone. Generally you should just use `limit.is_limit F`, unless you need the actual definition (which is in terms of `types.limit_cone_is_limit`). -/ def limit_cone_infi_is_limit (F : J ⥤ Top.{max v u}) : is_limit (limit_cone_infi F) := by { refine is_limit.of_faithful forget (types.limit_cone_is_limit _) (λ s, ⟨_, _⟩) (λ s, rfl), exact continuous_iff_coinduced_le.mpr (le_infi $ λ j, coinduced_le_iff_le_induced.mp $ (continuous_iff_coinduced_le.mp (s.π.app j).continuous : _) ) } instance Top_has_limits_of_size : has_limits_of_size.{v} Top.{max v u} := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } instance Top_has_limits : has_limits Top.{u} := Top.Top_has_limits_of_size.{u u} instance forget_preserves_limits_of_size : preserves_limits_of_size.{v v} (forget : Top.{max v u} ⥤ Type (max v u)) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by exactI preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget)) } } instance forget_preserves_limits : preserves_limits (forget : Top.{u} ⥤ Type u) := Top.forget_preserves_limits_of_size.{u u} /-- A choice of colimit cocone for a functor `F : J ⥤ Top`. Generally you should just use `colimit.coone F`, unless you need the actual definition (which is in terms of `types.colimit_cocone`). -/ def colimit_cocone (F : J ⥤ Top.{max v u}) : cocone F := { X := ⟨(types.colimit_cocone (F ⋙ forget)).X, ⨆ j, (F.obj j).str.coinduced ((types.colimit_cocone (F ⋙ forget)).ι.app j)⟩, ι := { app := λ j, ⟨(types.colimit_cocone (F ⋙ forget)).ι.app j, continuous_iff_coinduced_le.mpr (le_supr _ j)⟩, naturality' := λ j j' f, continuous_map.coe_injective ((types.colimit_cocone (F ⋙ forget)).ι.naturality f) } } /-- The chosen cocone `Top.colimit_cocone F` for a functor `F : J ⥤ Top` is a colimit cocone. Generally you should just use `colimit.is_colimit F`, unless you need the actual definition (which is in terms of `types.colimit_cocone_is_colimit`). -/ def colimit_cocone_is_colimit (F : J ⥤ Top.{max v u}) : is_colimit (colimit_cocone F) := by { refine is_colimit.of_faithful forget (types.colimit_cocone_is_colimit _) (λ s, ⟨_, _⟩) (λ s, rfl), exact continuous_iff_le_induced.mpr (supr_le $ λ j, coinduced_le_iff_le_induced.mp $ (continuous_iff_coinduced_le.mp (s.ι.app j).continuous : _) ) } instance Top_has_colimits_of_size : has_colimits_of_size.{v} Top.{max v u} := { has_colimits_of_shape := λ J 𝒥, by exactI { has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit := colimit_cocone_is_colimit F } } } instance Top_has_colimits : has_colimits Top.{u} := Top.Top_has_colimits_of_size.{u u} instance forget_preserves_colimits_of_size : preserves_colimits_of_size.{v v} (forget : Top.{max v u} ⥤ Type (max v u)) := { preserves_colimits_of_shape := λ J 𝒥, { preserves_colimit := λ F, by exactI preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit F) (types.colimit_cocone_is_colimit (F ⋙ forget)) } } instance forget_preserves_colimits : preserves_colimits (forget : Top.{u} ⥤ Type u) := Top.forget_preserves_colimits_of_size.{u u} /-- The projection from the product as a bundled continous map. -/ abbreviation pi_π {ι : Type v} (α : ι → Top.{max v u}) (i : ι) : Top.of (Π i, α i) ⟶ α i := ⟨λ f, f i, continuous_apply i⟩ /-- The explicit fan of a family of topological spaces given by the pi type. -/ @[simps X π_app] def pi_fan {ι : Type v} (α : ι → Top.{max v u}) : fan α := fan.mk (Top.of (Π i, α i)) (pi_π α) /-- The constructed fan is indeed a limit -/ def pi_fan_is_limit {ι : Type v} (α : ι → Top.{max v u}) : is_limit (pi_fan α) := { lift := λ S, { to_fun := λ s i, S.π.app i s }, uniq' := by { intros S m h, ext x i, simp [← h i] } } /-- The product is homeomorphic to the product of the underlying spaces, equipped with the product topology. -/ def pi_iso_pi {ι : Type v} (α : ι → Top.{max v u}) : ∏ α ≅ Top.of (Π i, α i) := (limit.is_limit _).cone_point_unique_up_to_iso (pi_fan_is_limit α) @[simp, reassoc] lemma pi_iso_pi_inv_π {ι : Type v} (α : ι → Top.{max v u}) (i : ι) : (pi_iso_pi α).inv ≫ pi.π α i = pi_π α i := by simp [pi_iso_pi] @[simp] lemma pi_iso_pi_inv_π_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : Π i, α i) : (pi.π α i : _) ((pi_iso_pi α).inv x) = x i := concrete_category.congr_hom (pi_iso_pi_inv_π α i) x @[simp] lemma pi_iso_pi_hom_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : ∏ α) : (pi_iso_pi α).hom x i = (pi.π α i : _) x := begin have := pi_iso_pi_inv_π α i, rw iso.inv_comp_eq at this, exact concrete_category.congr_hom this x end /-- The inclusion to the coproduct as a bundled continous map. -/ abbreviation sigma_ι {ι : Type v} (α : ι → Top.{max v u}) (i : ι) : α i ⟶ Top.of (Σ i, α i) := ⟨sigma.mk i⟩ /-- The explicit cofan of a family of topological spaces given by the sigma type. -/ @[simps X ι_app] def sigma_cofan {ι : Type v} (α : ι → Top.{max v u}) : cofan α := cofan.mk (Top.of (Σ i, α i)) (sigma_ι α) /-- The constructed cofan is indeed a colimit -/ def sigma_cofan_is_colimit {ι : Type v} (α : ι → Top.{max v u}) : is_colimit (sigma_cofan α) := { desc := λ S, { to_fun := λ s, S.ι.app s.1 s.2, continuous_to_fun := by { continuity, dsimp only, continuity } }, uniq' := by { intros S m h, ext ⟨i, x⟩, simp [← h i] } } /-- The coproduct is homeomorphic to the disjoint union of the topological spaces. -/ def sigma_iso_sigma {ι : Type v} (α : ι → Top.{max v u}) : ∐ α ≅ Top.of (Σ i, α i) := (colimit.is_colimit _).cocone_point_unique_up_to_iso (sigma_cofan_is_colimit α) @[simp, reassoc] lemma sigma_iso_sigma_hom_ι {ι : Type v} (α : ι → Top.{max v u}) (i : ι) : sigma.ι α i ≫ (sigma_iso_sigma α).hom = sigma_ι α i := by simp [sigma_iso_sigma] @[simp] lemma sigma_iso_sigma_hom_ι_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : α i) : (sigma_iso_sigma α).hom ((sigma.ι α i : _) x) = sigma.mk i x := concrete_category.congr_hom (sigma_iso_sigma_hom_ι α i) x @[simp] lemma sigma_iso_sigma_inv_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : α i) : (sigma_iso_sigma α).inv ⟨i, x⟩ = (sigma.ι α i : _) x := by { rw [← sigma_iso_sigma_hom_ι_apply, ← comp_app], simp, } lemma induced_of_is_limit {F : J ⥤ Top.{max v u}} (C : cone F) (hC : is_limit C) : C.X.topological_space = ⨅ j, (F.obj j).topological_space.induced (C.π.app j) := begin let homeo := homeo_of_iso (hC.cone_point_unique_up_to_iso (limit_cone_infi_is_limit F)), refine homeo.inducing.induced.trans _, change induced homeo (⨅ (j : J), _) = _, simpa [induced_infi, induced_compose], end lemma limit_topology (F : J ⥤ Top.{max v u}) : (limit F).topological_space = ⨅ j, (F.obj j).topological_space.induced (limit.π F j) := induced_of_is_limit _ (limit.is_limit F) section prod /-- The first projection from the product. -/ abbreviation prod_fst {X Y : Top.{u}} : Top.of (X × Y) ⟶ X := ⟨prod.fst⟩ /-- The second projection from the product. -/ abbreviation prod_snd {X Y : Top.{u}} : Top.of (X × Y) ⟶ Y := ⟨prod.snd⟩ /-- The explicit binary cofan of `X, Y` given by `X × Y`. -/ def prod_binary_fan (X Y : Top.{u}) : binary_fan X Y := binary_fan.mk prod_fst prod_snd /-- The constructed binary fan is indeed a limit -/ def prod_binary_fan_is_limit (X Y : Top.{u}) : is_limit (prod_binary_fan X Y) := { lift := λ (S : binary_fan X Y), { to_fun := λ s, (S.fst s, S.snd s) }, fac' := begin rintros S (_|_), tidy end, uniq' := begin intros S m h, ext x, { specialize h walking_pair.left, apply_fun (λ e, (e x)) at h, exact h }, { specialize h walking_pair.right, apply_fun (λ e, (e x)) at h, exact h }, end } /-- The homeomorphism between `X ⨯ Y` and the set-theoretic product of `X` and `Y`, equipped with the product topology. -/ def prod_iso_prod (X Y : Top.{u}) : X ⨯ Y ≅ Top.of (X × Y) := (limit.is_limit _).cone_point_unique_up_to_iso (prod_binary_fan_is_limit X Y) @[simp, reassoc] lemma prod_iso_prod_hom_fst (X Y : Top.{u}) : (prod_iso_prod X Y).hom ≫ prod_fst = limits.prod.fst := by simpa [← iso.eq_inv_comp, prod_iso_prod] @[simp, reassoc] lemma prod_iso_prod_hom_snd (X Y : Top.{u}) : (prod_iso_prod X Y).hom ≫ prod_snd = limits.prod.snd := by simpa [← iso.eq_inv_comp, prod_iso_prod] @[simp] lemma prod_iso_prod_hom_apply {X Y : Top.{u}} (x : X ⨯ Y) : (prod_iso_prod X Y).hom x = ((limits.prod.fst : X ⨯ Y ⟶ _) x, (limits.prod.snd : X ⨯ Y ⟶ _) x) := begin ext, { exact concrete_category.congr_hom (prod_iso_prod_hom_fst X Y) x }, { exact concrete_category.congr_hom (prod_iso_prod_hom_snd X Y) x } end @[simp, reassoc, elementwise] lemma prod_iso_prod_inv_fst (X Y : Top.{u}) : (prod_iso_prod X Y).inv ≫ limits.prod.fst = prod_fst := by simp [iso.inv_comp_eq] @[simp, reassoc, elementwise] lemma prod_iso_prod_inv_snd (X Y : Top.{u}) : (prod_iso_prod X Y).inv ≫ limits.prod.snd = prod_snd := by simp [iso.inv_comp_eq] lemma prod_topology {X Y : Top} : (X ⨯ Y).topological_space = induced (limits.prod.fst : X ⨯ Y ⟶ _) X.topological_space ⊓ induced (limits.prod.snd : X ⨯ Y ⟶ _) Y.topological_space := begin let homeo := homeo_of_iso (prod_iso_prod X Y), refine homeo.inducing.induced.trans _, change induced homeo (_ ⊓ _) = _, simpa [induced_compose] end lemma range_prod_map {W X Y Z : Top.{u}} (f : W ⟶ Y) (g : X ⟶ Z) : set.range (limits.prod.map f g) = (limits.prod.fst : Y ⨯ Z ⟶ _) ⁻¹' (set.range f) ∩ (limits.prod.snd : Y ⨯ Z ⟶ _) ⁻¹' (set.range g) := begin ext, split, { rintros ⟨y, rfl⟩, simp only [set.mem_preimage, set.mem_range, set.mem_inter_eq, ←comp_apply], simp only [limits.prod.map_fst, limits.prod.map_snd, exists_apply_eq_apply, comp_apply, and_self] }, { rintros ⟨⟨x₁, hx₁⟩, ⟨x₂, hx₂⟩⟩, use (prod_iso_prod W X).inv (x₁, x₂), apply concrete.limit_ext, rintro ⟨⟩, { simp only [← comp_apply, category.assoc], erw limits.prod.map_fst, simp [hx₁] }, { simp only [← comp_apply, category.assoc], erw limits.prod.map_snd, simp [hx₂] } } end lemma inducing_prod_map {W X Y Z : Top} {f : W ⟶ X} {g : Y ⟶ Z} (hf : inducing f) (hg : inducing g) : inducing (limits.prod.map f g) := begin constructor, simp only [prod_topology, induced_compose, ←coe_comp, limits.prod.map_fst, limits.prod.map_snd, induced_inf], simp only [coe_comp], rw [← @induced_compose _ _ _ _ _ f, ← @induced_compose _ _ _ _ _ g, ← hf.induced, ← hg.induced] end lemma embedding_prod_map {W X Y Z : Top} {f : W ⟶ X} {g : Y ⟶ Z} (hf : embedding f) (hg : embedding g) : embedding (limits.prod.map f g) := ⟨inducing_prod_map hf.to_inducing hg.to_inducing, begin haveI := (Top.mono_iff_injective _).mpr hf.inj, haveI := (Top.mono_iff_injective _).mpr hg.inj, exact (Top.mono_iff_injective _).mp infer_instance end⟩ end prod section pullback variables {X Y Z : Top.{u}} /-- The first projection from the pullback. -/ abbreviation pullback_fst (f : X ⟶ Z) (g : Y ⟶ Z) : Top.of { p : X × Y // f p.1 = g p.2 } ⟶ X := ⟨prod.fst ∘ subtype.val⟩ /-- The second projection from the pullback. -/ abbreviation pullback_snd (f : X ⟶ Z) (g : Y ⟶ Z) : Top.of { p : X × Y // f p.1 = g p.2 } ⟶ Y := ⟨prod.snd ∘ subtype.val⟩ /-- The explicit pullback cone of `X, Y` given by `{ p : X × Y // f p.1 = g p.2 }`. -/ def pullback_cone (f : X ⟶ Z) (g : Y ⟶ Z) : pullback_cone f g := pullback_cone.mk (pullback_fst f g) (pullback_snd f g) (by { ext ⟨x, h⟩, simp [h] }) /-- The constructed cone is a limit. -/ def pullback_cone_is_limit (f : X ⟶ Z) (g : Y ⟶ Z) : is_limit (pullback_cone f g) := pullback_cone.is_limit_aux' _ begin intro s, split, swap, exact { to_fun := λ x, ⟨⟨s.fst x, s.snd x⟩, by simpa using concrete_category.congr_hom s.condition x⟩ }, refine ⟨_,_,_⟩, { ext, delta pullback_cone, simp }, { ext, delta pullback_cone, simp }, { intros m h₁ h₂, ext x, { simpa using concrete_category.congr_hom h₁ x }, { simpa using concrete_category.congr_hom h₂ x } } end /-- The pullback of two maps can be identified as a subspace of `X × Y`. -/ def pullback_iso_prod_subtype (f : X ⟶ Z) (g : Y ⟶ Z) : pullback f g ≅ Top.of { p : X × Y // f p.1 = g p.2 } := (limit.is_limit _).cone_point_unique_up_to_iso (pullback_cone_is_limit f g) @[simp, reassoc] lemma pullback_iso_prod_subtype_inv_fst (f : X ⟶ Z) (g : Y ⟶ Z) : (pullback_iso_prod_subtype f g).inv ≫ pullback.fst = pullback_fst f g := by simpa [pullback_iso_prod_subtype] @[simp] lemma pullback_iso_prod_subtype_inv_fst_apply (f : X ⟶ Z) (g : Y ⟶ Z) (x : { p : X × Y // f p.1 = g p.2 }) : (pullback.fst : pullback f g ⟶ _) ((pullback_iso_prod_subtype f g).inv x) = (x : X × Y).fst := concrete_category.congr_hom (pullback_iso_prod_subtype_inv_fst f g) x @[simp, reassoc] lemma pullback_iso_prod_subtype_inv_snd (f : X ⟶ Z) (g : Y ⟶ Z) : (pullback_iso_prod_subtype f g).inv ≫ pullback.snd = pullback_snd f g := by simpa [pullback_iso_prod_subtype] @[simp] lemma pullback_iso_prod_subtype_inv_snd_apply (f : X ⟶ Z) (g : Y ⟶ Z) (x : { p : X × Y // f p.1 = g p.2 }) : (pullback.snd : pullback f g ⟶ _) ((pullback_iso_prod_subtype f g).inv x) = (x : X × Y).snd := concrete_category.congr_hom (pullback_iso_prod_subtype_inv_snd f g) x lemma pullback_iso_prod_subtype_hom_fst (f : X ⟶ Z) (g : Y ⟶ Z) : (pullback_iso_prod_subtype f g).hom ≫ pullback_fst f g = pullback.fst := by rw [←iso.eq_inv_comp, pullback_iso_prod_subtype_inv_fst] lemma pullback_iso_prod_subtype_hom_snd (f : X ⟶ Z) (g : Y ⟶ Z) : (pullback_iso_prod_subtype f g).hom ≫ pullback_snd f g = pullback.snd := by rw [←iso.eq_inv_comp, pullback_iso_prod_subtype_inv_snd] @[simp] lemma pullback_iso_prod_subtype_hom_apply {f : X ⟶ Z} {g : Y ⟶ Z} (x : pullback f g) : (pullback_iso_prod_subtype f g).hom x = ⟨⟨(pullback.fst : pullback f g ⟶ _) x, (pullback.snd : pullback f g ⟶ _) x⟩, by simpa using concrete_category.congr_hom pullback.condition x⟩ := begin ext, exacts [concrete_category.congr_hom (pullback_iso_prod_subtype_hom_fst f g) x, concrete_category.congr_hom (pullback_iso_prod_subtype_hom_snd f g) x] end lemma pullback_topology {X Y Z : Top.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) : (pullback f g).topological_space = induced (pullback.fst : pullback f g ⟶ _) X.topological_space ⊓ induced (pullback.snd : pullback f g ⟶ _) Y.topological_space := begin let homeo := homeo_of_iso (pullback_iso_prod_subtype f g), refine homeo.inducing.induced.trans _, change induced homeo (induced _ (_ ⊓ _)) = _, simpa [induced_compose] end lemma range_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) : set.range (prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) = { x | (limits.prod.fst ≫ f) x = (limits.prod.snd ≫ g) x } := begin ext x, split, { rintros ⟨y, rfl⟩, simp only [←comp_apply, set.mem_set_of_eq], congr' 1, simp [pullback.condition] }, { intro h, use (pullback_iso_prod_subtype f g).inv ⟨⟨_, _⟩, h⟩, apply concrete.limit_ext, rintro ⟨⟩; simp } end lemma inducing_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) : inducing ⇑(prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) := ⟨by simp [prod_topology, pullback_topology, induced_compose, ←coe_comp]⟩ lemma embedding_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) : embedding ⇑(prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) := ⟨inducing_pullback_to_prod f g, (Top.mono_iff_injective _).mp infer_instance⟩ /-- If the map `S ⟶ T` is mono, then there is a description of the image of `W ×ₛ X ⟶ Y ×ₜ Z`. -/ lemma range_pullback_map {W X Y Z S T : Top} (f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) [H₃ : mono i₃] (eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) : set.range (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) = (pullback.fst : pullback g₁ g₂ ⟶ _) ⁻¹' (set.range i₁) ∩ (pullback.snd : pullback g₁ g₂ ⟶ _) ⁻¹' (set.range i₂) := begin ext, split, { rintro ⟨y, rfl⟩, simp, }, rintros ⟨⟨x₁, hx₁⟩, ⟨x₂, hx₂⟩⟩, have : f₁ x₁ = f₂ x₂, { apply (Top.mono_iff_injective _).mp H₃, simp only [←comp_apply, eq₁, eq₂], simp only [comp_apply, hx₁, hx₂], simp only [←comp_apply, pullback.condition] }, use (pullback_iso_prod_subtype f₁ f₂).inv ⟨⟨x₁, x₂⟩, this⟩, apply concrete.limit_ext, rintros (_|_|_), { simp only [Top.comp_app, limit.lift_π_apply, category.assoc, pullback_cone.mk_π_app_one, hx₁, pullback_iso_prod_subtype_inv_fst_apply, subtype.coe_mk], simp only [← comp_apply], congr, apply limit.w _ walking_cospan.hom.inl }, { simp [hx₁] }, { simp [hx₂] }, end lemma pullback_fst_range {X Y S : Top} (f : X ⟶ S) (g : Y ⟶ S) : set.range (pullback.fst : pullback f g ⟶ _) = { x : X | ∃ y : Y, f x = g y} := begin ext x, split, { rintro ⟨y, rfl⟩, use (pullback.snd : pullback f g ⟶ _) y, exact concrete_category.congr_hom pullback.condition y }, { rintro ⟨y, eq⟩, use (Top.pullback_iso_prod_subtype f g).inv ⟨⟨x, y⟩, eq⟩, simp }, end lemma pullback_snd_range {X Y S : Top} (f : X ⟶ S) (g : Y ⟶ S) : set.range (pullback.snd : pullback f g ⟶ _) = { y : Y | ∃ x : X, f x = g y} := begin ext y, split, { rintro ⟨x, rfl⟩, use (pullback.fst : pullback f g ⟶ _) x, exact concrete_category.congr_hom pullback.condition x }, { rintro ⟨x, eq⟩, use (Top.pullback_iso_prod_subtype f g).inv ⟨⟨x, y⟩, eq⟩, simp }, end /-- If there is a diagram where the morphisms `W ⟶ Y` and `X ⟶ Z` are embeddings, then the induced morphism `W ×ₛ X ⟶ Y ×ₜ Z` is also an embedding. W ⟶ Y ↘ ↘ S ⟶ T ↗ ↗ X ⟶ Z -/ lemma pullback_map_embedding_of_embeddings {W X Y Z S T : Top} (f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) {i₁ : W ⟶ Y} {i₂ : X ⟶ Z} (H₁ : embedding i₁) (H₂ : embedding i₂) (i₃ : S ⟶ T) (eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) : embedding (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) := begin refine embedding_of_embedding_compose (continuous_map.continuous_to_fun _) (show continuous (prod.lift pullback.fst pullback.snd : pullback g₁ g₂ ⟶ Y ⨯ Z), from continuous_map.continuous_to_fun _) _, suffices : embedding (prod.lift pullback.fst pullback.snd ≫ limits.prod.map i₁ i₂ : pullback f₁ f₂ ⟶ _), { simpa [←coe_comp] using this }, rw coe_comp, refine embedding.comp (embedding_prod_map H₁ H₂) (embedding_pullback_to_prod _ _) end /-- If there is a diagram where the morphisms `W ⟶ Y` and `X ⟶ Z` are open embeddings, and `S ⟶ T` is mono, then the induced morphism `W ×ₛ X ⟶ Y ×ₜ Z` is also an open embedding. W ⟶ Y ↘ ↘ S ⟶ T ↗ ↗ X ⟶ Z -/ lemma pullback_map_open_embedding_of_open_embeddings {W X Y Z S T : Top} (f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) {i₁ : W ⟶ Y} {i₂ : X ⟶ Z} (H₁ : open_embedding i₁) (H₂ : open_embedding i₂) (i₃ : S ⟶ T) [H₃ : mono i₃] (eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) : open_embedding (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) := begin split, { apply pullback_map_embedding_of_embeddings f₁ f₂ g₁ g₂ H₁.to_embedding H₂.to_embedding i₃ eq₁ eq₂ }, { rw range_pullback_map, apply is_open.inter; apply continuous.is_open_preimage, continuity, exacts [H₁.open_range, H₂.open_range] } end lemma snd_embedding_of_left_embedding {X Y S : Top} {f : X ⟶ S} (H : embedding f) (g : Y ⟶ S) : embedding ⇑(pullback.snd : pullback f g ⟶ Y) := begin convert (homeo_of_iso (as_iso (pullback.snd : pullback (𝟙 S) g ⟶ _))).embedding.comp (pullback_map_embedding_of_embeddings f g (𝟙 _) g H (homeo_of_iso (iso.refl _)).embedding (𝟙 _) rfl (by simp)), erw ←coe_comp, simp end lemma fst_embedding_of_right_embedding {X Y S : Top} (f : X ⟶ S) {g : Y ⟶ S} (H : embedding g) : embedding ⇑(pullback.fst : pullback f g ⟶ X) := begin convert (homeo_of_iso (as_iso (pullback.fst : pullback f (𝟙 S) ⟶ _))).embedding.comp (pullback_map_embedding_of_embeddings f g f (𝟙 _) (homeo_of_iso (iso.refl _)).embedding H (𝟙 _) rfl (by simp)), erw ←coe_comp, simp end lemma embedding_of_pullback_embeddings {X Y S : Top} {f : X ⟶ S} {g : Y ⟶ S} (H₁ : embedding f) (H₂ : embedding g) : embedding (limit.π (cospan f g) walking_cospan.one) := begin convert H₂.comp (snd_embedding_of_left_embedding H₁ g), erw ←coe_comp, congr, exact (limit.w _ walking_cospan.hom.inr).symm end lemma snd_open_embedding_of_left_open_embedding {X Y S : Top} {f : X ⟶ S} (H : open_embedding f) (g : Y ⟶ S) : open_embedding ⇑(pullback.snd : pullback f g ⟶ Y) := begin convert (homeo_of_iso (as_iso (pullback.snd : pullback (𝟙 S) g ⟶ _))).open_embedding.comp (pullback_map_open_embedding_of_open_embeddings f g (𝟙 _) g H (homeo_of_iso (iso.refl _)).open_embedding (𝟙 _) rfl (by simp)), erw ←coe_comp, simp end lemma fst_open_embedding_of_right_open_embedding {X Y S : Top} (f : X ⟶ S) {g : Y ⟶ S} (H : open_embedding g) : open_embedding ⇑(pullback.fst : pullback f g ⟶ X) := begin convert (homeo_of_iso (as_iso (pullback.fst : pullback f (𝟙 S) ⟶ _))).open_embedding.comp (pullback_map_open_embedding_of_open_embeddings f g f (𝟙 _) (homeo_of_iso (iso.refl _)).open_embedding H (𝟙 _) rfl (by simp)), erw ←coe_comp, simp end /-- If `X ⟶ S`, `Y ⟶ S` are open embeddings, then so is `X ×ₛ Y ⟶ S`. -/ lemma open_embedding_of_pullback_open_embeddings {X Y S : Top} {f : X ⟶ S} {g : Y ⟶ S} (H₁ : open_embedding f) (H₂ : open_embedding g) : open_embedding (limit.π (cospan f g) walking_cospan.one) := begin convert H₂.comp (snd_open_embedding_of_left_open_embedding H₁ g), erw ←coe_comp, congr, exact (limit.w _ walking_cospan.hom.inr).symm end lemma fst_iso_of_right_embedding_range_subset {X Y S : Top} (f : X ⟶ S) {g : Y ⟶ S} (hg : embedding g) (H : set.range f ⊆ set.range g) : is_iso (pullback.fst : pullback f g ⟶ X) := begin let : (pullback f g : Top) ≃ₜ X := (homeomorph.of_embedding _ (fst_embedding_of_right_embedding f hg)).trans { to_fun := coe, inv_fun := (λ x, ⟨x, by { rw pullback_fst_range, exact ⟨_, (H (set.mem_range_self x)).some_spec.symm⟩ }⟩), left_inv := λ ⟨_,_⟩, rfl, right_inv := λ x, rfl }, convert is_iso.of_iso (iso_of_homeo this), ext, refl end lemma snd_iso_of_left_embedding_range_subset {X Y S : Top} {f : X ⟶ S} (hf : embedding f) (g : Y ⟶ S) (H : set.range g ⊆ set.range f) : is_iso (pullback.snd : pullback f g ⟶ Y) := begin let : (pullback f g : Top) ≃ₜ Y := (homeomorph.of_embedding _ (snd_embedding_of_left_embedding hf g)).trans { to_fun := coe, inv_fun := (λ x, ⟨x, by { rw pullback_snd_range, exact ⟨_, (H (set.mem_range_self x)).some_spec⟩ }⟩), left_inv := λ ⟨_,_⟩, rfl, right_inv := λ x, rfl }, convert is_iso.of_iso (iso_of_homeo this), ext, refl end lemma pullback_snd_image_fst_preimage (f : X ⟶ Z) (g : Y ⟶ Z) (U : set X) : (pullback.snd : pullback f g ⟶ _) '' ((pullback.fst : pullback f g ⟶ _) ⁻¹' U) = g ⁻¹' (f '' U) := begin ext x, split, { rintros ⟨y, hy, rfl⟩, exact ⟨(pullback.fst : pullback f g ⟶ _) y, hy, concrete_category.congr_hom pullback.condition y⟩ }, { rintros ⟨y, hy, eq⟩, exact ⟨(Top.pullback_iso_prod_subtype f g).inv ⟨⟨_,_⟩, eq⟩, by simpa, by simp⟩ }, end lemma pullback_fst_image_snd_preimage (f : X ⟶ Z) (g : Y ⟶ Z) (U : set Y) : (pullback.fst : pullback f g ⟶ _) '' ((pullback.snd : pullback f g ⟶ _) ⁻¹' U) = f ⁻¹' (g '' U) := begin ext x, split, { rintros ⟨y, hy, rfl⟩, exact ⟨(pullback.snd : pullback f g ⟶ _) y, hy, (concrete_category.congr_hom pullback.condition y).symm⟩ }, { rintros ⟨y, hy, eq⟩, exact ⟨(Top.pullback_iso_prod_subtype f g).inv ⟨⟨_,_⟩,eq.symm⟩, by simpa, by simp⟩ }, end end pullback --TODO: Add analogous constructions for `coprod` and `pushout`. lemma coinduced_of_is_colimit {F : J ⥤ Top.{max v u}} (c : cocone F) (hc : is_colimit c) : c.X.topological_space = ⨆ j, (F.obj j).topological_space.coinduced (c.ι.app j) := begin let homeo := homeo_of_iso (hc.cocone_point_unique_up_to_iso (colimit_cocone_is_colimit F)), ext, refine homeo.symm.is_open_preimage.symm.trans (iff.trans _ is_open_supr_iff.symm), exact is_open_supr_iff end lemma colimit_topology (F : J ⥤ Top.{max v u}) : (colimit F).topological_space = ⨆ j, (F.obj j).topological_space.coinduced (colimit.ι F j) := coinduced_of_is_colimit _ (colimit.is_colimit F) lemma colimit_is_open_iff (F : J ⥤ Top.{max v u}) (U : set ((colimit F : _) : Type (max v u))) : is_open U ↔ ∀ j, is_open (colimit.ι F j ⁻¹' U) := begin conv_lhs { rw colimit_topology F }, exact is_open_supr_iff end lemma coequalizer_is_open_iff (F : walking_parallel_pair.{u} ⥤ Top.{u}) (U : set ((colimit F : _) : Type u)) : is_open U ↔ is_open (colimit.ι F walking_parallel_pair.one ⁻¹' U) := begin rw colimit_is_open_iff.{u}, split, { intro H, exact H _ }, { intros H j, cases j, { rw ←colimit.w F walking_parallel_pair_hom.left, exact (F.map walking_parallel_pair_hom.left).continuous_to_fun.is_open_preimage _ H }, { exact H } } end end Top namespace Top section cofiltered_limit variables {J : Type v} [small_category J] [is_cofiltered J] (F : J ⥤ Top.{max v u}) (C : cone F) (hC : is_limit C) include hC /-- Given a *compatible* collection of topological bases for the factors in a cofiltered limit which contain `set.univ` and are closed under intersections, the induced *naive* collection of sets in the limit is, in fact, a topological basis. -/ theorem is_topological_basis_cofiltered_limit (T : Π j, set (set (F.obj j))) (hT : ∀ j, is_topological_basis (T j)) (univ : ∀ (i : J), set.univ ∈ T i) (inter : ∀ i (U1 U2 : set (F.obj i)), U1 ∈ T i → U2 ∈ T i → U1 ∩ U2 ∈ T i) (compat : ∀ (i j : J) (f : i ⟶ j) (V : set (F.obj j)) (hV : V ∈ T j), (F.map f) ⁻¹' V ∈ T i) : is_topological_basis { U : set C.X | ∃ j (V : set (F.obj j)), V ∈ T j ∧ U = C.π.app j ⁻¹' V } := begin classical, -- The limit cone for `F` whose topology is defined as an infimum. let D := limit_cone_infi F, -- The isomorphism between the cone point of `C` and the cone point of `D`. let E : C.X ≅ D.X := hC.cone_point_unique_up_to_iso (limit_cone_infi_is_limit _), have hE : inducing E.hom := (Top.homeo_of_iso E).inducing, -- Reduce to the assertion of the theorem with `D` instead of `C`. suffices : is_topological_basis { U : set D.X | ∃ j (V : set (F.obj j)), V ∈ T j ∧ U = D.π.app j ⁻¹' V }, { convert this.inducing hE, ext U0, split, { rintro ⟨j, V, hV, rfl⟩, refine ⟨D.π.app j ⁻¹' V, ⟨j, V, hV, rfl⟩, rfl⟩ }, { rintro ⟨W, ⟨j, V, hV, rfl⟩, rfl⟩, refine ⟨j, V, hV, rfl⟩ } }, -- Using `D`, we can apply the characterization of the topological basis of a -- topology defined as an infimum... convert is_topological_basis_infi hT (λ j (x : D.X), D.π.app j x), ext U0, split, { rintros ⟨j, V, hV, rfl⟩, let U : Π i, set (F.obj i) := λ i, if h : i = j then (by {rw h, exact V}) else set.univ, refine ⟨U,{j},_,_⟩, { rintro i h, rw finset.mem_singleton at h, dsimp [U], rw dif_pos h, subst h, exact hV }, { dsimp [U], simp } }, { rintros ⟨U, G, h1, h2⟩, obtain ⟨j, hj⟩ := is_cofiltered.inf_objs_exists G, let g : ∀ e (he : e ∈ G), j ⟶ e := λ _ he, (hj he).some, let Vs : J → set (F.obj j) := λ e, if h : e ∈ G then F.map (g e h) ⁻¹' (U e) else set.univ, let V : set (F.obj j) := ⋂ (e : J) (he : e ∈ G), Vs e, refine ⟨j, V, _, _⟩, { -- An intermediate claim used to apply induction along `G : finset J` later on. have : ∀ (S : set (set (F.obj j))) (E : finset J) (P : J → set (F.obj j)) (univ : set.univ ∈ S) (inter : ∀ A B : set (F.obj j), A ∈ S → B ∈ S → A ∩ B ∈ S) (cond : ∀ (e : J) (he : e ∈ E), P e ∈ S), (⋂ e (he : e ∈ E), P e) ∈ S, { intros S E, apply E.induction_on, { intros P he hh, simpa }, { intros a E ha hh1 hh2 hh3 hh4 hh5, rw finset.set_bInter_insert, refine hh4 _ _ (hh5 _ (finset.mem_insert_self _ _)) (hh1 _ hh3 hh4 _), intros e he, exact hh5 e (finset.mem_insert_of_mem he) } }, -- use the intermediate claim to finish off the goal using `univ` and `inter`. refine this _ _ _ (univ _) (inter _) _, intros e he, dsimp [Vs], rw dif_pos he, exact compat j e (g e he) (U e) (h1 e he), }, { -- conclude... rw h2, dsimp [V], rw set.preimage_Inter, congr' 1, ext1 e, rw set.preimage_Inter, congr' 1, ext1 he, dsimp [Vs], rw [dif_pos he, ← set.preimage_comp], congr' 1, change _ = ⇑(D.π.app j ≫ F.map (g e he)), rw D.w } } end end cofiltered_limit section topological_konig /-! ## Topological Kőnig's lemma A topological version of Kőnig's lemma is that the inverse limit of nonempty compact Hausdorff spaces is nonempty. (Note: this can be generalized further to inverse limits of nonempty compact T0 spaces, where all the maps are closed maps; see [Stone1979] --- however there is an erratum for Theorem 4 that the element in the inverse limit can have cofinally many components that are not closed points.) We give this in a more general form, which is that cofiltered limits of nonempty compact Hausdorff spaces are nonempty (`nonempty_limit_cone_of_compact_t2_cofiltered_system`). This also applies to inverse limits, where `{J : Type u} [preorder J] [is_directed J (≤)]` and `F : Jᵒᵖ ⥤ Top`. The theorem is specialized to nonempty finite types (which are compact Hausdorff with the discrete topology) in `nonempty_sections_of_fintype_cofiltered_system` and `nonempty_sections_of_fintype_inverse_system`. (See <https://stacks.math.columbia.edu/tag/086J> for the Set version.) -/ variables {J : Type u} [small_category J] variables (F : J ⥤ Top.{u}) private abbreviation finite_diagram_arrow {J : Type u} [small_category J] (G : finset J) := Σ' (X Y : J) (mX : X ∈ G) (mY : Y ∈ G), X ⟶ Y private abbreviation finite_diagram (J : Type u) [small_category J] := Σ (G : finset J), finset (finite_diagram_arrow G) /-- Partial sections of a cofiltered limit are sections when restricted to a finite subset of objects and morphisms of `J`. -/ def partial_sections {J : Type u} [small_category J] (F : J ⥤ Top.{u}) {G : finset J} (H : finset (finite_diagram_arrow G)) : set (Π j, F.obj j) := { u | ∀ {f : finite_diagram_arrow G} (hf : f ∈ H), F.map f.2.2.2.2 (u f.1) = u f.2.1 } lemma partial_sections.nonempty [is_cofiltered J] [h : Π (j : J), nonempty (F.obj j)] {G : finset J} (H : finset (finite_diagram_arrow G)) : (partial_sections F H).nonempty := begin classical, use λ (j : J), if hj : j ∈ G then F.map (is_cofiltered.inf_to G H hj) (h (is_cofiltered.inf G H)).some else (h _).some, rintros ⟨X, Y, hX, hY, f⟩ hf, dsimp only, rwa [dif_pos hX, dif_pos hY, ←comp_app, ←F.map_comp, @is_cofiltered.inf_to_commutes _ _ _ G H], end lemma partial_sections.directed : directed superset (λ (G : finite_diagram J), partial_sections F G.2) := begin classical, intros A B, let ιA : finite_diagram_arrow A.1 → finite_diagram_arrow (A.1 ⊔ B.1) := λ f, ⟨f.1, f.2.1, finset.mem_union_left _ f.2.2.1, finset.mem_union_left _ f.2.2.2.1, f.2.2.2.2⟩, let ιB : finite_diagram_arrow B.1 → finite_diagram_arrow (A.1 ⊔ B.1) := λ f, ⟨f.1, f.2.1, finset.mem_union_right _ f.2.2.1, finset.mem_union_right _ f.2.2.2.1, f.2.2.2.2⟩, refine ⟨⟨A.1 ⊔ B.1, A.2.image ιA ⊔ B.2.image ιB⟩, _, _⟩, { rintro u hu f hf, have : ιA f ∈ A.2.image ιA ⊔ B.2.image ιB, { apply finset.mem_union_left, rw finset.mem_image, refine ⟨f, hf, rfl⟩ }, exact hu this }, { rintro u hu f hf, have : ιB f ∈ A.2.image ιA ⊔ B.2.image ιB, { apply finset.mem_union_right, rw finset.mem_image, refine ⟨f, hf, rfl⟩ }, exact hu this } end lemma partial_sections.closed [Π (j : J), t2_space (F.obj j)] {G : finset J} (H : finset (finite_diagram_arrow G)) : is_closed (partial_sections F H) := begin have : partial_sections F H = ⋂ {f : finite_diagram_arrow G} (hf : f ∈ H), { u | F.map f.2.2.2.2 (u f.1) = u f.2.1 }, { ext1, simp only [set.mem_Inter, set.mem_set_of_eq], refl, }, rw this, apply is_closed_bInter, intros f hf, apply is_closed_eq, continuity, end /-- Cofiltered limits of nonempty compact Hausdorff spaces are nonempty topological spaces. --/ lemma nonempty_limit_cone_of_compact_t2_cofiltered_system [is_cofiltered J] [Π (j : J), nonempty (F.obj j)] [Π (j : J), compact_space (F.obj j)] [Π (j : J), t2_space (F.obj j)] : nonempty (Top.limit_cone.{u} F).X := begin classical, obtain ⟨u, hu⟩ := is_compact.nonempty_Inter_of_directed_nonempty_compact_closed (λ G, partial_sections F _) (partial_sections.directed F) (λ G, partial_sections.nonempty F _) (λ G, is_closed.is_compact (partial_sections.closed F _)) (λ G, partial_sections.closed F _), use u, intros X Y f, let G : finite_diagram J := ⟨{X, Y}, {⟨X, Y, by simp only [true_or, eq_self_iff_true, finset.mem_insert], by simp only [eq_self_iff_true, or_true, finset.mem_insert, finset.mem_singleton], f⟩}⟩, exact hu _ ⟨G, rfl⟩ (finset.mem_singleton_self _), end end topological_konig end Top section fintype_konig /-- This bootstraps `nonempty_sections_of_fintype_inverse_system`. In this version, the `F` functor is between categories of the same universe, and it is an easy corollary to `Top.nonempty_limit_cone_of_compact_t2_inverse_system`. -/ lemma nonempty_sections_of_fintype_cofiltered_system.init {J : Type u} [small_category J] [is_cofiltered J] (F : J ⥤ Type u) [hf : Π (j : J), fintype (F.obj j)] [hne : Π (j : J), nonempty (F.obj j)] : F.sections.nonempty := begin let F' : J ⥤ Top := F ⋙ Top.discrete, haveI : Π (j : J), fintype (F'.obj j) := hf, haveI : Π (j : J), nonempty (F'.obj j) := hne, obtain ⟨⟨u, hu⟩⟩ := Top.nonempty_limit_cone_of_compact_t2_cofiltered_system F', exact ⟨u, λ _ _ f, hu f⟩, end /-- The cofiltered limit of nonempty finite types is nonempty. See `nonempty_sections_of_fintype_inverse_system` for a specialization to inverse limits. -/ theorem nonempty_sections_of_fintype_cofiltered_system {J : Type u} [category.{w} J] [is_cofiltered J] (F : J ⥤ Type v) [Π (j : J), fintype (F.obj j)] [Π (j : J), nonempty (F.obj j)] : F.sections.nonempty := begin -- Step 1: lift everything to the `max u v w` universe. let J' : Type (max w v u) := as_small.{max w v} J, let down : J' ⥤ J := as_small.down, let F' : J' ⥤ Type (max u v w) := down ⋙ F ⋙ ulift_functor.{(max u w) v}, haveI : ∀ i, nonempty (F'.obj i) := λ i, ⟨⟨classical.arbitrary (F.obj (down.obj i))⟩⟩, haveI : ∀ i, fintype (F'.obj i) := λ i, fintype.of_equiv (F.obj (down.obj i)) equiv.ulift.symm, -- Step 2: apply the bootstrap theorem obtain ⟨u, hu⟩ := nonempty_sections_of_fintype_cofiltered_system.init F', -- Step 3: interpret the results use λ j, (u ⟨j⟩).down, intros j j' f, have h := @hu (⟨j⟩ : J') (⟨j'⟩ : J') (ulift.up f), simp only [as_small.down, functor.comp_map, ulift_functor_map, functor.op_map] at h, simp_rw [←h], refl, end /-- The inverse limit of nonempty finite types is nonempty. See `nonempty_sections_of_fintype_cofiltered_system` for a generalization to cofiltered limits. That version applies in almost all cases, and the only difference is that this version allows `J` to be empty. This may be regarded as a generalization of Kőnig's lemma. To specialize: given a locally finite connected graph, take `Jᵒᵖ` to be `ℕ` and `F j` to be length-`j` paths that start from an arbitrary fixed vertex. Elements of `F.sections` can be read off as infinite rays in the graph. -/ theorem nonempty_sections_of_fintype_inverse_system {J : Type u} [preorder J] [is_directed J (≤)] (F : Jᵒᵖ ⥤ Type v) [Π (j : Jᵒᵖ), fintype (F.obj j)] [Π (j : Jᵒᵖ), nonempty (F.obj j)] : F.sections.nonempty := begin casesI is_empty_or_nonempty J, { haveI : is_empty Jᵒᵖ := ⟨λ j, is_empty_elim j.unop⟩, -- TODO: this should be a global instance exact ⟨is_empty_elim, is_empty_elim⟩, }, { exact nonempty_sections_of_fintype_cofiltered_system _, }, end end fintype_konig
a17141fd27cf55ae26555f35577455c6a732ee99
1437b3495ef9020d5413178aa33c0a625f15f15f
/analysis/limits.lean
867b893a8cdc1f24409906d106b94c330c570773
[ "Apache-2.0" ]
permissive
jean002/mathlib
c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30
dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd
refs/heads/master
1,587,027,806,375
1,547,306,358,000
1,547,306,358,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,717
lean
/- 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 A collection of limit properties. -/ import algebra.big_operators algebra.group_power tactic.norm_num analysis.ennreal analysis.topology.infinite_sum noncomputable theory open classical finset function filter local attribute [instance] prop_decidable section real lemma has_sum_of_absolute_convergence {f : ℕ → ℝ} (hf : ∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) : has_sum f := let f' := λs:finset ℕ, s.sum (λi, abs (f i)) in suffices cauchy (map (λs:finset ℕ, s.sum f) at_top), from complete_space.complete this, cauchy_iff.mpr $ and.intro (map_ne_bot at_top_ne_bot) $ assume s hs, let ⟨ε, hε, hsε⟩ := mem_uniformity_dist.mp hs, ⟨r, hr⟩ := hf in have hε' : {p : ℝ × ℝ | dist p.1 p.2 < ε / 2} ∈ (@uniformity ℝ _).sets, from mem_uniformity_dist.mpr ⟨ε / 2, div_pos_of_pos_of_pos hε two_pos, assume a b h, h⟩, have cauchy (at_top.map $ λn, f' (range n)), from cauchy_downwards cauchy_nhds (map_ne_bot at_top_ne_bot) hr, have ∃n, ∀{n'}, n ≤ n' → dist (f' (range n)) (f' (range n')) < ε / 2, by simp [cauchy_iff, mem_at_top_sets] at this; from let ⟨t, ⟨u, hu⟩, ht⟩ := this _ hε' in ⟨u, assume n' hn, ht $ set.prod_mk_mem_set_prod_eq.mpr ⟨hu _ (le_refl _), hu _ hn⟩⟩, let ⟨n, hn⟩ := this in have ∀{s}, range n ⊆ s → abs ((s \ range n).sum f) < ε / 2, from assume s hs, let ⟨n', hn'⟩ := @exists_nat_subset_range s in have range n ⊆ range n', from finset.subset.trans hs hn', have f'_nn : 0 ≤ f' (range n' \ range n), from zero_le_sum $ assume _ _, abs_nonneg _, calc abs ((s \ range n).sum f) ≤ f' (s \ range n) : abs_sum_le_sum_abs ... ≤ f' (range n' \ range n) : sum_le_sum_of_subset_of_nonneg (finset.sdiff_subset_sdiff hn' (finset.subset.refl _)) (assume _ _ _, abs_nonneg _) ... = abs (f' (range n' \ range n)) : (abs_of_nonneg f'_nn).symm ... = abs (f' (range n') - f' (range n)) : by simp [f', (sum_sdiff ‹range n ⊆ range n'›).symm] ... = abs (f' (range n) - f' (range n')) : abs_sub _ _ ... < ε / 2 : hn $ range_subset.mp this, have ∀{s t}, range n ⊆ s → range n ⊆ t → dist (s.sum f) (t.sum f) < ε, from assume s t hs ht, calc abs (s.sum f - t.sum f) = abs ((s \ range n).sum f + - (t \ range n).sum f) : by rw [←sum_sdiff hs, ←sum_sdiff ht]; simp ... ≤ abs ((s \ range n).sum f) + abs ((t \ range n).sum f) : le_trans (abs_add_le_abs_add_abs _ _) $ by rw [abs_neg]; exact le_refl _ ... < ε / 2 + ε / 2 : add_lt_add (this hs) (this ht) ... = ε : by rw [←add_div, add_self_div_two], ⟨(λs:finset ℕ, s.sum f) '' {s | range n ⊆ s}, image_mem_map $ mem_at_top (range n), assume ⟨a, b⟩ ⟨⟨t, ht, ha⟩, ⟨s, hs, hb⟩⟩, by simp at ha hb; exact ha ▸ hb ▸ hsε (this ht hs)⟩ lemma is_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} {r : ℝ} (hf : ∀n, 0 ≤ f n) : is_sum f r ↔ tendsto (λn, (range n).sum f) at_top (nhds r) := ⟨tendsto_sum_nat_of_is_sum, assume hr, have tendsto (λn, (range n).sum (λn, abs (f n))) at_top (nhds r), by simp [(λi, abs_of_nonneg (hf i)), hr], let ⟨p, h⟩ := has_sum_of_absolute_convergence ⟨r, this⟩ in have hp : tendsto (λn, (range n).sum f) at_top (nhds p), from tendsto_sum_nat_of_is_sum h, have p = r, from tendsto_nhds_unique at_top_ne_bot hp hr, this ▸ h⟩ end real lemma mul_add_one_le_pow {r : ℝ} (hr : 0 ≤ r) : ∀{n:ℕ}, (n:ℝ) * r + 1 ≤ (r + 1) ^ n | 0 := by simp; exact le_refl 1 | (n + 1) := let h : (n:ℝ) ≥ 0 := nat.cast_nonneg n in calc ↑(n + 1) * r + 1 ≤ ((n + 1) * r + 1) + r * r * n : le_add_of_le_of_nonneg (le_refl _) (mul_nonneg (mul_nonneg hr hr) h) ... = (r + 1) * (n * r + 1) : by simp [mul_add, add_mul, mul_comm, mul_assoc] ... ≤ (r + 1) * (r + 1) ^ n : mul_le_mul (le_refl _) mul_add_one_le_pow (add_nonneg (mul_nonneg h hr) zero_le_one) (add_nonneg hr zero_le_one) lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi.2 $ assume p, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n, have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ n * (r - 1) + 1 : le_add_of_le_of_nonneg (le_refl _) zero_le_one ... ≤ ((r - 1) + 1) ^ n : mul_add_one_le_pow $ le_of_lt this ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma map_succ_at_top_eq : map nat.succ at_top = at_top := le_antisymm (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b, assume c hc, hb (c + 1) $ le_trans hc $ nat.le_succ _⟩) (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b + 1, assume c, match c with | 0 := assume h, have 0 > 0, from lt_of_lt_of_le (lt_add_of_le_of_pos (nat.zero_le _) zero_lt_one) h, (lt_irrefl 0 this).elim | (c+1) := assume h, hb _ (nat.le_of_succ_le_succ h) end⟩) lemma tendsto_comp_succ_at_top_iff {α : Type*} {f : ℕ → α} {x : filter α} : tendsto (λn, f (nat.succ n)) at_top x ↔ tendsto f at_top x := calc tendsto (f ∘ nat.succ) at_top x ↔ tendsto f (map nat.succ at_top) x : by simp [tendsto, filter.map_map] ... ↔ _ : by rw [map_succ_at_top_eq] lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (nhds 0) := by_cases (assume : r = 0, tendsto_comp_succ_at_top_iff.mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp tendsto_inverse_at_top_nhds_0, tendsto_cong this $ univ_mem_sets' $ by simp *) lemma tendsto_coe_iff {f : ℕ → ℕ} : tendsto (λ n, (f n : ℝ)) at_top at_top ↔ tendsto f at_top at_top := ⟨ λ h, tendsto_infi.2 $ λ i, tendsto_principal.2 (have _, from tendsto_infi.1 h i, by simpa using tendsto_principal.1 this), λ h, tendsto.comp h tendsto_of_nat_at_top_at_top ⟩ lemma tendsto_pow_at_top_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) : tendsto (λn:ℕ, k ^ n) at_top at_top := tendsto_coe_iff.1 $ have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h, by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (nhds 0) := tendsto.comp (tendsto_coe_iff.2 tendsto_id) tendsto_inverse_at_top_nhds_0 lemma tendsto_one_div_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1/(n : ℝ)) at_top (nhds 0) := by simpa only [inv_eq_one_div] using tendsto_inverse_at_top_nhds_0_nat lemma sum_geometric' {r : ℝ} (h : r ≠ 0) : ∀{n}, (finset.range n).sum (λi, (r + 1) ^ i) = ((r + 1) ^ n - 1) / r | 0 := by simp [zero_div] | (n+1) := by simp [@sum_geometric' n, h, pow_succ, range_succ, add_div_eq_mul_add_div, add_mul, mul_comm, mul_assoc] lemma sum_geometric {r : ℝ} {n : ℕ} (h : r ≠ 1) : (range n).sum (λi, r ^ i) = (r ^ n - 1) / (r - 1) := calc (range n).sum (λi, r ^ i) = (range n).sum (λi, ((r - 1) + 1) ^ i) : by simp ... = (((r - 1) + 1) ^ n - 1) / (r - 1) : sum_geometric' $ by simp [sub_eq_iff_eq_add, -sub_eq_add_neg, h] ... = (r ^ n - 1) / (r - 1) : by simp lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn:ℕ, r ^ n) (1 / (1 - r)) := have r ≠ 1, from ne_of_lt h₂, have r + -1 ≠ 0, by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg $ pow_nonneg h₁).mpr $ by simp [neg_inv, sum_geometric, div_eq_mul_inv, *] at * lemma is_sum_geometric_two (a : ℝ) : is_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert is_sum_mul_left (a / 2) (is_sum_geometric (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, rw ← pow_inv; [refl, exact two_ne_zero] }, { norm_num, rw div_mul_cancel _ two_ne_zero } end def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, is_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : is_sum f ε := is_sum_geometric_two _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, let g : ℕ → ℝ := λ n, option.cases_on (encodable.decode2 ι n) 0 (f ∘ encodable.encode), have : ∀ n, g n = 0 ∨ g n = f n, { intro n, dsimp [g], cases e : encodable.decode2 ι n with a, { exact or.inl rfl }, { simp [encodable.mem_decode2.1 e] } }, cases has_sum_of_has_sum_of_sub ⟨_, hf⟩ this with c hg, have cε : c ≤ ε, { refine is_sum_le (λ n, _) hg hf, cases this n; rw h, exact le_of_lt (f0 _) }, have hs : ∀ n, g n ≠ 0 → (encodable.decode2 ι n).is_some, { intros n h, dsimp [g] at h, cases encodable.decode2 ι n, exact (h rfl).elim, exact rfl }, refine ⟨c, _, cε⟩, refine is_sum_of_is_sum_ne_zero (λ n h, option.get (hs n h)) (λ n _, ne_of_gt (f0 _)) (λ i _, encodable.encode i) (λ n h, ne_of_gt _) (λ n h, _) (λ i _, _) (λ i _, _) hg, { dsimp [g], rw encodable.encodek2, exact f0 _ }, { exact encodable.mem_decode2.1 (option.get_mem _) }, { exact option.get_of_mem _ (encodable.encodek2 _) }, { dsimp [g], rw encodable.encodek2 } end namespace nnreal theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, is_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := dense hε in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt.2 $ hε' i, ⟨c, is_sum_le (assume i, le_of_lt $ hε' i) is_sum_zero hc ⟩, nnreal.is_sum_coe.1 hc, lt_of_le_of_lt (nnreal.coe_le.1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑ i, (ε' i : ennreal)) < ε := begin rcases dense hε with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end end ennreal
8401ec9236d1f80d31c9a6bbc5137b4cfc5cfdea
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/filter/prod.lean
2d8134c16d6b69573550f53f0d9369b64ac4b36a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
22,119
lean
/- Copyright (c) 2022 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johanes Hölzl, Patrick Massot, Yury Kudryashov, Kevin Wilson, Heather Macbeth -/ import order.filter.basic /-! # Product and coproduct filters In this file we define `filter.prod f g` (notation: `f ×ᶠ g`) and `filter.coprod f g`. The product of two filters is the largest filter `l` such that `filter.tendsto prod.fst l f` and `filter.tendsto prod.snd l g`. ## Implementation details The product filter cannot be defined using the monad structure on filters. For example: ```lean F := do {x ← seq, y ← top, return (x, y)} G := do {y ← top, x ← seq, return (x, y)} ``` hence: ```lean s ∈ F ↔ ∃ n, [n..∞] × univ ⊆ s s ∈ G ↔ ∀ i:ℕ, ∃ n, [n..∞] × {i} ⊆ s ``` Now `⋃ i, [i..∞] × {i}` is in `G` but not in `F`. As product filter we want to have `F` as result. ## Notations * `f ×ᶠ g` : `filter.prod f g`, localized in `filter`. -/ open set open_locale filter namespace filter variables {α β γ δ : Type*} {ι : Sort*} section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd localized "infix (name := filter.prod) ` ×ᶠ `:60 := filter.prod" in filter lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : s ×ˢ t ∈ f ×ᶠ g := inter_mem_inf (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ×ˢ t₂ ⊆ s) := begin simp only [filter.prod], split, { rintro ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, rfl⟩, exact ⟨s₁, hs₁, s₂, hs₂, λ p ⟨h, h'⟩, ⟨hts₁ h, hts₂ h'⟩⟩ }, { rintro ⟨t₁, ht₁, t₂, ht₂, h⟩, exact mem_inf_of_inter (preimage_mem_comap ht₁) (preimage_mem_comap ht₂) h } end @[simp] lemma prod_mem_prod_iff {s : set α} {t : set β} {f : filter α} {g : filter β} [f.ne_bot] [g.ne_bot] : s ×ˢ t ∈ f ×ᶠ g ↔ s ∈ f ∧ t ∈ g := ⟨λ h, let ⟨s', hs', t', ht', H⟩ := mem_prod_iff.1 h in (prod_subset_prod_iff.1 H).elim (λ ⟨hs's, ht't⟩, ⟨mem_of_superset hs' hs's, mem_of_superset ht' ht't⟩) (λ h, h.elim (λ hs'e, absurd hs'e (nonempty_of_mem hs').ne_empty) (λ ht'e, absurd ht'e (nonempty_of_mem ht').ne_empty)), λ h, prod_mem_prod h.1 h.2⟩ lemma mem_prod_principal {f : filter α} {s : set (α × β)} {t : set β}: s ∈ f ×ᶠ 𝓟 t ↔ {a | ∀ b ∈ t, (a, b) ∈ s} ∈ f := begin rw [← @exists_mem_subset_iff _ f, mem_prod_iff], refine exists₂_congr (λ u u_in, ⟨_, λ h, ⟨t, mem_principal_self t, _⟩⟩), { rintros ⟨v, v_in, hv⟩ a a_in b b_in, exact hv (mk_mem_prod a_in $ v_in b_in) }, { rintro ⟨x, y⟩ ⟨hx, hy⟩, exact h hx y hy } end lemma mem_prod_top {f : filter α} {s : set (α × β)} : s ∈ f ×ᶠ (⊤ : filter β) ↔ {a | ∀ b, (a, b) ∈ s} ∈ f := begin rw [← principal_univ, mem_prod_principal], simp only [mem_univ, forall_true_left] end lemma eventually_prod_principal_iff {p : α × β → Prop} {s : set β} : (∀ᶠ (x : α × β) in (f ×ᶠ (𝓟 s)), p x) ↔ ∀ᶠ (x : α) in f, ∀ (y : β), y ∈ s → p (x, y) := by { rw [eventually_iff, eventually_iff, mem_prod_principal], simp only [mem_set_of_eq], } lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) : comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) := by erw [comap_inf, filter.comap_comap, filter.comap_comap] lemma prod_top {f : filter α} : f ×ᶠ (⊤ : filter β) = f.comap prod.fst := by rw [filter.prod, comap_top, inf_top_eq] lemma sup_prod (f₁ f₂ : filter α) (g : filter β) : (f₁ ⊔ f₂) ×ᶠ g = (f₁ ×ᶠ g) ⊔ (f₂ ×ᶠ g) := by rw [filter.prod, comap_sup, inf_sup_right, ← filter.prod, ← filter.prod] lemma prod_sup (f : filter α) (g₁ g₂ : filter β) : f ×ᶠ (g₁ ⊔ g₂) = (f ×ᶠ g₁) ⊔ (f ×ᶠ g₂) := by rw [filter.prod, comap_sup, inf_sup_left, ← filter.prod, ← filter.prod] lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} : (∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x) (pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λ x, (m₁ x, m₂ x)) f (g ×ᶠ h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma tendsto_prod_swap {α1 α2 : Type*} {a1 : filter α1} {a2 : filter α2} : tendsto (prod.swap : α1 × α2 → α2 × α1) (a1 ×ᶠ a2) (a2 ×ᶠ a1) := tendsto_snd.prod_mk tendsto_fst lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 := tendsto_fst.eventually h lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 := tendsto_snd.eventually h lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x) {lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) : ∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl lb).and (hb.prod_inr la) lemma eventually_eq.prod_map {δ} {la : filter α} {fa ga : α → γ} (ha : fa =ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb =ᶠ[lb] gb) : prod.map fa fb =ᶠ[la ×ᶠ lb] prod.map ga gb := (eventually.prod_mk ha hb).mono $ λ x h, prod.ext h.1 h.2 lemma eventually_le.prod_map {δ} [has_le γ] [has_le δ] {la : filter α} {fa ga : α → γ} (ha : fa ≤ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb ≤ᶠ[lb] gb) : prod.map fa fb ≤ᶠ[la ×ᶠ lb] prod.map ga gb := eventually.prod_mk ha hb lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop} (h : ∀ᶠ x in la ×ᶠ lb, p x) : ∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) := begin rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩, exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb) end /-- A fact that is eventually true about all pairs `l ×ᶠ l` is eventually true about all diagonal pairs `(i, i)` -/ lemma eventually.diag_of_prod {f : filter α} {p : α × α → Prop} (h : ∀ᶠ i in f ×ᶠ f, p i) : (∀ᶠ i in f, p (i, i)) := begin obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h, apply (ht.and hs).mono (λ x hx, hst hx.1 hx.2), end lemma eventually.diag_of_prod_left {f : filter α} {g : filter γ} {p : (α × α) × γ → Prop} : (∀ᶠ x in (f ×ᶠ f ×ᶠ g), p x) → (∀ᶠ (x : α × γ) in (f ×ᶠ g), p ((x.1, x.1), x.2)) := begin intros h, obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h, refine (ht.diag_of_prod.prod_mk hs).mono (λ x hx, by simp only [hst hx.1 hx.2, prod.mk.eta]), end lemma eventually.diag_of_prod_right {f : filter α} {g : filter γ} {p : α × γ × γ → Prop} : (∀ᶠ x in (f ×ᶠ (g ×ᶠ g)), p x) → (∀ᶠ (x : α × γ) in (f ×ᶠ g), p (x.1, x.2, x.2)) := begin intros h, obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h, refine (ht.prod_mk hs.diag_of_prod).mono (λ x hx, by simp only [hst hx.1 hx.2, prod.mk.eta]), end lemma tendsto_diag : tendsto (λ i, (i, i)) f (f ×ᶠ f) := tendsto_iff_eventually.mpr (λ _ hpr, hpr.diag_of_prod) lemma prod_infi_left [nonempty ι] {f : ι → filter α} {g : filter β}: (⨅ i, f i) ×ᶠ g = (⨅ i, (f i) ×ᶠ g) := by { rw [filter.prod, comap_infi, infi_inf], simp only [filter.prod, eq_self_iff_true] } lemma prod_infi_right [nonempty ι] {f : filter α} {g : ι → filter β} : f ×ᶠ (⨅ i, g i) = (⨅ i, f ×ᶠ (g i)) := by { rw [filter.prod, comap_infi, inf_infi], simp only [filter.prod, eq_self_iff_true] } @[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_mono_left (g : filter β) {f₁ f₂ : filter α} (hf : f₁ ≤ f₂) : f₁ ×ᶠ g ≤ f₂ ×ᶠ g := filter.prod_mono hf rfl.le lemma prod_mono_right (f : filter α) {g₁ g₂ : filter β} (hf : g₁ ≤ g₂) : f ×ᶠ g₁ ≤ f ×ᶠ g₂ := filter.prod_mono rfl.le hf lemma {u v w x} prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : (comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λ p : β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf] lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) := by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : f ×ᶠ g = map (λ p : β×α, (p.2, p.1)) (g ×ᶠ f) := by { rw [prod_comm', ← map_swap_eq_comap_swap], refl } @[simp] lemma map_fst_prod (f : filter α) (g : filter β) [ne_bot g] : map prod.fst (f ×ᶠ g) = f := begin refine le_antisymm tendsto_fst (λ s hs, _), rw [mem_map, mem_prod_iff] at hs, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, rw [← image_subset_iff, fst_image_prod] at hs, exacts [mem_of_superset h₁ hs, nonempty_of_mem h₂] end @[simp] lemma map_snd_prod (f : filter α) (g : filter β) [ne_bot f] : map prod.snd (f ×ᶠ g) = g := by rw [prod_comm, map_map, (∘), map_fst_prod] @[simp] lemma prod_le_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} [ne_bot f₁] [ne_bot g₁] : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ ↔ f₁ ≤ f₂ ∧ g₁ ≤ g₂ := ⟨λ h, ⟨map_fst_prod f₁ g₁ ▸ tendsto_fst.mono_left h, map_snd_prod f₁ g₁ ▸ tendsto_snd.mono_left h⟩, λ h, prod_mono h.1 h.2⟩ @[simp] lemma prod_inj {f₁ f₂ : filter α} {g₁ g₂ : filter β} [ne_bot f₁] [ne_bot g₁] : f₁ ×ᶠ g₁ = f₂ ×ᶠ g₂ ↔ f₁ = f₂ ∧ g₁ = g₂ := begin refine ⟨λ h, _, λ h, h.1 ▸ h.2 ▸ rfl⟩, have hle : f₁ ≤ f₂ ∧ g₁ ≤ g₂ := prod_le_prod.1 h.le, haveI := ne_bot_of_le hle.1, haveI := ne_bot_of_le hle.2, exact ⟨hle.1.antisymm $ (prod_le_prod.1 h.ge).1, hle.2.antisymm $ (prod_le_prod.1 h.ge).2⟩ end lemma eventually_swap_iff {p : (α × β) → Prop} : (∀ᶠ (x : α × β) in (f ×ᶠ g), p x) ↔ ∀ᶠ (y : β × α) in (g ×ᶠ f), p y.swap := by { rw [prod_comm, eventually_map], simpa, } lemma prod_assoc (f : filter α) (g : filter β) (h : filter γ) : map (equiv.prod_assoc α β γ) ((f ×ᶠ g) ×ᶠ h) = f ×ᶠ (g ×ᶠ h) := by simp_rw [← comap_equiv_symm, filter.prod, comap_inf, comap_comap, inf_assoc, function.comp, equiv.prod_assoc_symm_apply] theorem prod_assoc_symm (f : filter α) (g : filter β) (h : filter γ) : map (equiv.prod_assoc α β γ).symm (f ×ᶠ (g ×ᶠ h)) = (f ×ᶠ g) ×ᶠ h := by simp_rw [map_equiv_symm, filter.prod, comap_inf, comap_comap, inf_assoc, function.comp, equiv.prod_assoc_apply] lemma tendsto_prod_assoc {f : filter α} {g : filter β} {h : filter γ} : tendsto (equiv.prod_assoc α β γ) (f ×ᶠ g ×ᶠ h) (f ×ᶠ (g ×ᶠ h)) := (prod_assoc f g h).le lemma tendsto_prod_assoc_symm {f : filter α} {g : filter β} {h : filter γ} : tendsto (equiv.prod_assoc α β γ).symm (f ×ᶠ (g ×ᶠ h)) (f ×ᶠ g ×ᶠ h) := (prod_assoc_symm f g h).le /-- A useful lemma when dealing with uniformities. -/ lemma map_swap4_prod {f : filter α} {g : filter β} {h : filter γ} {k : filter δ} : map (λ p : (α × β) × (γ × δ), ((p.1.1, p.2.1), (p.1.2, p.2.2))) ((f ×ᶠ g) ×ᶠ (h ×ᶠ k)) = (f ×ᶠ h) ×ᶠ (g ×ᶠ k) := by simp_rw [map_swap4_eq_comap, filter.prod, comap_inf, comap_comap, inf_assoc, inf_left_comm] lemma tendsto_swap4_prod {f : filter α} {g : filter β} {h : filter γ} {k : filter δ} : tendsto (λ p : (α × β) × (γ × δ), ((p.1.1, p.2.1), (p.1.2, p.2.2))) ((f ×ᶠ g) ×ᶠ (h ×ᶠ k)) ((f ×ᶠ h) ×ᶠ (g ×ᶠ k)) := map_swap4_prod.le lemma {u v w x} prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : (map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := le_antisymm (λ s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc (m₁ '' s₁) ×ˢ (m₂ '' s₂) = (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) '' s₁ ×ˢ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto.comp le_rfl tendsto_fst).prod_mk (tendsto.comp le_rfl tendsto_snd)) lemma prod_map_map_eq' {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} (f : α₁ → α₂) (g : β₁ → β₂) (F : filter α₁) (G : filter β₁) : (map f F) ×ᶠ (map g G) = map (prod.map f g) (F ×ᶠ G) := prod_map_map_eq lemma le_prod_map_fst_snd {f : filter (α × β)} : f ≤ map prod.fst f ×ᶠ map prod.snd f := le_inf le_comap_map le_comap_map lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) := begin erw [tendsto, ← prod_map_map_eq], exact filter.prod_mono hf hg, end protected lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f ×ᶠ g) = (f.map (λ a b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], intro s, split, exact λ ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, λ x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact λ ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, λ ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f ×ᶠ g = (f.map prod.mk).seq g := have h : _ := f.map_prod id g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : (f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm] @[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : (𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (s ×ˢ t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma pure_prod {a : α} {f : filter β} : pure a ×ᶠ f = map (prod.mk a) f := by rw [prod_eq, map_pure, pure_seq_eq_map] lemma map_pure_prod (f : α → β → γ) (a : α) (B : filter β) : filter.map (function.uncurry f) (pure a ×ᶠ B) = filter.map (f a) B := by { rw filter.pure_prod, refl } @[simp] lemma prod_pure {f : filter α} {b : β} : f ×ᶠ pure b = map (λ a, (a, b)) f := by rw [prod_eq, seq_pure, map_map] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) := by simp lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { intro h, rcases mem_prod_iff.1 (empty_mem_iff_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_mem_iff_bot.1 (s_eq ▸ hs) }, { right, exact empty_mem_iff_bot.1 (t_eq ▸ ht) } }, { rintro (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) := by simp only [ne_bot_iff, ne, prod_eq_bot, not_or_distrib] lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) : ne_bot (f ×ᶠ g) := prod_ne_bot.2 ⟨hf, hg⟩ instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] : ne_bot (f ×ᶠ g) := hf.prod hg lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (x ×ᶠ y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] lemma tendsto_prod_iff' {f : filter α} {g : filter β} {g' : filter γ} {s : α → β × γ} : tendsto s f (g ×ᶠ g') ↔ tendsto (λ n, (s n).1) f g ∧ tendsto (λ n, (s n).2) f g' := by { unfold filter.prod, simp only [tendsto_inf, tendsto_comap_iff, iff_self] } end prod /-! ### Coproducts of filters -/ section coprod variables {f : filter α} {g : filter β} /-- Coproduct of filters. -/ protected def coprod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊔ g.comap prod.snd lemma mem_coprod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f.coprod g ↔ ((∃ t₁ ∈ f, prod.fst ⁻¹' t₁ ⊆ s) ∧ (∃ t₂ ∈ g, prod.snd ⁻¹' t₂ ⊆ s)) := by simp [filter.coprod] @[simp] lemma bot_coprod (l : filter β) : (⊥ : filter α).coprod l = comap prod.snd l := by simp [filter.coprod] @[simp] lemma coprod_bot (l : filter α) : l.coprod (⊥ : filter β) = comap prod.fst l := by simp [filter.coprod] lemma bot_coprod_bot : (⊥ : filter α).coprod (⊥ : filter β) = ⊥ := by simp lemma compl_mem_coprod {s : set (α × β)} {la : filter α} {lb : filter β} : sᶜ ∈ la.coprod lb ↔ (prod.fst '' s)ᶜ ∈ la ∧ (prod.snd '' s)ᶜ ∈ lb := by simp only [filter.coprod, mem_sup, compl_mem_comap] @[mono] lemma coprod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.coprod g₁ ≤ f₂.coprod g₂ := sup_le_sup (comap_mono hf) (comap_mono hg) lemma coprod_ne_bot_iff : (f.coprod g).ne_bot ↔ f.ne_bot ∧ nonempty β ∨ nonempty α ∧ g.ne_bot := by simp [filter.coprod] @[instance] lemma coprod_ne_bot_left [ne_bot f] [nonempty β] : (f.coprod g).ne_bot := coprod_ne_bot_iff.2 (or.inl ⟨‹_›, ‹_›⟩) @[instance] lemma coprod_ne_bot_right [ne_bot g] [nonempty α] : (f.coprod g).ne_bot := coprod_ne_bot_iff.2 (or.inr ⟨‹_›, ‹_›⟩) lemma principal_coprod_principal (s : set α) (t : set β) : (𝓟 s).coprod (𝓟 t) = 𝓟 (sᶜ ×ˢ tᶜ)ᶜ := by rw [filter.coprod, comap_principal, comap_principal, sup_principal, set.prod_eq, compl_inter, preimage_compl, preimage_compl, compl_compl, compl_compl] -- this inequality can be strict; see `map_const_principal_coprod_map_id_principal` and -- `map_prod_map_const_id_principal_coprod_principal` below. lemma {u v w x} map_prod_map_coprod_le {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : map (prod.map m₁ m₂) (f₁.coprod f₂) ≤ (map m₁ f₁).coprod (map m₂ f₂) := begin intros s, simp only [mem_map, mem_coprod_iff], rintro ⟨⟨u₁, hu₁, h₁⟩, u₂, hu₂, h₂⟩, refine ⟨⟨m₁ ⁻¹' u₁, hu₁, λ _ hx, h₁ _⟩, ⟨m₂ ⁻¹' u₂, hu₂, λ _ hx, h₂ _⟩⟩; convert hx end /-- Characterization of the coproduct of the `filter.map`s of two principal filters `𝓟 {a}` and `𝓟 {i}`, the first under the constant function `λ a, b` and the second under the identity function. Together with the next lemma, `map_prod_map_const_id_principal_coprod_principal`, this provides an example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/ lemma map_const_principal_coprod_map_id_principal {α β ι : Type*} (a : α) (b : β) (i : ι) : (map (λ _ : α, b) (𝓟 {a})).coprod (map id (𝓟 {i})) = 𝓟 (({b} : set β) ×ˢ univ ∪ univ ×ˢ ({i} : set ι)) := by simp only [map_principal, filter.coprod, comap_principal, sup_principal, image_singleton, image_id, prod_univ, univ_prod] /-- Characterization of the `filter.map` of the coproduct of two principal filters `𝓟 {a}` and `𝓟 {i}`, under the `prod.map` of two functions, respectively the constant function `λ a, b` and the identity function. Together with the previous lemma, `map_const_principal_coprod_map_id_principal`, this provides an example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/ lemma map_prod_map_const_id_principal_coprod_principal {α β ι : Type*} (a : α) (b : β) (i : ι) : map (prod.map (λ _ : α, b) id) ((𝓟 {a}).coprod (𝓟 {i})) = 𝓟 (({b} : set β) ×ˢ (univ : set ι)) := begin rw [principal_coprod_principal, map_principal], congr, ext ⟨b', i'⟩, split, { rintro ⟨⟨a'', i''⟩, h₁, h₂, h₃⟩, simp }, { rintro ⟨h₁, h₂⟩, use (a, i'), simpa using h₁.symm } end lemma tendsto.prod_map_coprod {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a.coprod b) (c.coprod d) := map_prod_map_coprod_le.trans (coprod_mono hf hg) end coprod end filter
55d0ea3e0a52cdd0104b559aeb7d5b45cc388233
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/special_functions/trigonometric/angle.lean
67809c59791101701105fd2b4ef520695b5b15d6
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
21,105
lean
/- Copyright (c) 2019 Calle Sönne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Calle Sönne -/ import analysis.special_functions.trigonometric.basic import algebra.char_zero.quotient import algebra.order.to_interval_mod import topology.instances.sign /-! # The type of angles In this file we define `real.angle` to be the quotient group `ℝ/2πℤ` and prove a few simple lemmas about trigonometric functions and angles. -/ open_locale real noncomputable theory namespace real /-- The type of angles -/ @[derive [add_comm_group, topological_space, topological_add_group]] def angle : Type := ℝ ⧸ (add_subgroup.zmultiples (2 * π)) namespace angle instance : inhabited angle := ⟨0⟩ instance : has_coe ℝ angle := ⟨quotient_add_group.mk' _⟩ @[continuity] lemma continuous_coe : continuous (coe : ℝ → angle) := continuous_quotient_mk /-- Coercion `ℝ → angle` as an additive homomorphism. -/ def coe_hom : ℝ →+ angle := quotient_add_group.mk' _ @[simp] lemma coe_coe_hom : (coe_hom : ℝ → angle) = coe := rfl /-- An induction principle to deduce results for `angle` from those for `ℝ`, used with `induction θ using real.angle.induction_on`. -/ @[elab_as_eliminator] protected lemma induction_on {p : angle → Prop} (θ : angle) (h : ∀ x : ℝ, p x) : p θ := quotient.induction_on' θ h @[simp] lemma coe_zero : ↑(0 : ℝ) = (0 : angle) := rfl @[simp] lemma coe_add (x y : ℝ) : ↑(x + y : ℝ) = (↑x + ↑y : angle) := rfl @[simp] lemma coe_neg (x : ℝ) : ↑(-x : ℝ) = -(↑x : angle) := rfl @[simp] lemma coe_sub (x y : ℝ) : ↑(x - y : ℝ) = (↑x - ↑y : angle) := rfl lemma coe_nsmul (n : ℕ) (x : ℝ) : ↑(n • x : ℝ) = (n • ↑x : angle) := rfl lemma coe_zsmul (z : ℤ) (x : ℝ) : ↑(z • x : ℝ) = (z • ↑x : angle) := rfl @[simp, norm_cast] lemma coe_nat_mul_eq_nsmul (x : ℝ) (n : ℕ) : ↑((n : ℝ) * x) = n • (↑x : angle) := by simpa only [nsmul_eq_mul] using coe_hom.map_nsmul x n @[simp, norm_cast] lemma coe_int_mul_eq_zsmul (x : ℝ) (n : ℤ) : ↑((n : ℝ) * x : ℝ) = n • (↑x : angle) := by simpa only [zsmul_eq_mul] using coe_hom.map_zsmul x n lemma angle_eq_iff_two_pi_dvd_sub {ψ θ : ℝ} : (θ : angle) = ψ ↔ ∃ k : ℤ, θ - ψ = 2 * π * k := by simp only [quotient_add_group.eq, add_subgroup.zmultiples_eq_closure, add_subgroup.mem_closure_singleton, zsmul_eq_mul', (sub_eq_neg_add _ _).symm, eq_comm] @[simp] lemma coe_two_pi : ↑(2 * π : ℝ) = (0 : angle) := angle_eq_iff_two_pi_dvd_sub.2 ⟨1, by rw [sub_zero, int.cast_one, mul_one]⟩ @[simp] lemma neg_coe_pi : -(π : angle) = π := begin rw [←coe_neg, angle_eq_iff_two_pi_dvd_sub], use -1, simp [two_mul, sub_eq_add_neg] end lemma sub_coe_pi_eq_add_coe_pi (θ : angle) : θ - π = θ + π := by rw [sub_eq_add_neg, neg_coe_pi] @[simp] lemma two_nsmul_coe_pi : (2 : ℕ) • (π : angle) = 0 := by simp [←coe_nat_mul_eq_nsmul] @[simp] lemma two_zsmul_coe_pi : (2 : ℤ) • (π : angle) = 0 := by simp [←coe_int_mul_eq_zsmul] @[simp] lemma coe_pi_add_coe_pi : (π : real.angle) + π = 0 := by rw [←two_nsmul, two_nsmul_coe_pi] lemma zsmul_eq_iff {ψ θ : angle} {z : ℤ} (hz : z ≠ 0) : z • ψ = z • θ ↔ (∃ k : fin z.nat_abs, ψ = θ + (k : ℕ) • (2 * π / z : ℝ)) := quotient_add_group.zmultiples_zsmul_eq_zsmul_iff hz lemma nsmul_eq_iff {ψ θ : angle} {n : ℕ} (hz : n ≠ 0) : n • ψ = n • θ ↔ (∃ k : fin n, ψ = θ + (k : ℕ) • (2 * π / n : ℝ)) := quotient_add_group.zmultiples_nsmul_eq_nsmul_iff hz lemma two_zsmul_eq_iff {ψ θ : angle} : (2 : ℤ) • ψ = (2 : ℤ) • θ ↔ (ψ = θ ∨ ψ = θ + π) := by rw [zsmul_eq_iff two_ne_zero, int.nat_abs_bit0, int.nat_abs_one, fin.exists_fin_two, fin.coe_zero, fin.coe_one, zero_smul, add_zero, one_smul, int.cast_two, mul_div_cancel_left (_ : ℝ) two_ne_zero] lemma two_nsmul_eq_iff {ψ θ : angle} : (2 : ℕ) • ψ = (2 : ℕ) • θ ↔ (ψ = θ ∨ ψ = θ + π) := by simp_rw [←coe_nat_zsmul, int.coe_nat_bit0, int.coe_nat_one, two_zsmul_eq_iff] lemma two_nsmul_eq_zero_iff {θ : angle} : (2 : ℕ) • θ = 0 ↔ (θ = 0 ∨ θ = π) := by convert two_nsmul_eq_iff; simp lemma two_nsmul_ne_zero_iff {θ : angle} : (2 : ℕ) • θ ≠ 0 ↔ θ ≠ 0 ∧ θ ≠ π := by rw [←not_or_distrib, ←two_nsmul_eq_zero_iff] lemma two_zsmul_eq_zero_iff {θ : angle} : (2 : ℤ) • θ = 0 ↔ (θ = 0 ∨ θ = π) := by simp_rw [two_zsmul, ←two_nsmul, two_nsmul_eq_zero_iff] lemma two_zsmul_ne_zero_iff {θ : angle} : (2 : ℤ) • θ ≠ 0 ↔ θ ≠ 0 ∧ θ ≠ π := by rw [←not_or_distrib, ←two_zsmul_eq_zero_iff] lemma eq_neg_self_iff {θ : angle} : θ = -θ ↔ θ = 0 ∨ θ = π := by rw [←add_eq_zero_iff_eq_neg, ←two_nsmul, two_nsmul_eq_zero_iff] lemma ne_neg_self_iff {θ : angle} : θ ≠ -θ ↔ θ ≠ 0 ∧ θ ≠ π := by rw [←not_or_distrib, ←eq_neg_self_iff.not] lemma neg_eq_self_iff {θ : angle} : -θ = θ ↔ θ = 0 ∨ θ = π := by rw [eq_comm, eq_neg_self_iff] lemma neg_ne_self_iff {θ : angle} : -θ ≠ θ ↔ θ ≠ 0 ∧ θ ≠ π := by rw [←not_or_distrib, ←neg_eq_self_iff.not] theorem cos_eq_iff_coe_eq_or_eq_neg {θ ψ : ℝ} : cos θ = cos ψ ↔ (θ : angle) = ψ ∨ (θ : angle) = -ψ := begin split, { intro Hcos, rw [← sub_eq_zero, cos_sub_cos, mul_eq_zero, mul_eq_zero, neg_eq_zero, eq_false_intro two_ne_zero, false_or, sin_eq_zero_iff, sin_eq_zero_iff] at Hcos, rcases Hcos with ⟨n, hn⟩ | ⟨n, hn⟩, { right, rw [eq_div_iff_mul_eq (@two_ne_zero ℝ _ _), ← sub_eq_iff_eq_add] at hn, rw [← hn, coe_sub, eq_neg_iff_add_eq_zero, sub_add_cancel, mul_assoc, coe_int_mul_eq_zsmul, mul_comm, coe_two_pi, zsmul_zero] }, { left, rw [eq_div_iff_mul_eq (@two_ne_zero ℝ _ _), eq_sub_iff_add_eq] at hn, rw [← hn, coe_add, mul_assoc, coe_int_mul_eq_zsmul, mul_comm, coe_two_pi, zsmul_zero, zero_add] }, apply_instance, }, { rw [angle_eq_iff_two_pi_dvd_sub, ← coe_neg, angle_eq_iff_two_pi_dvd_sub], rintro (⟨k, H⟩ | ⟨k, H⟩), rw [← sub_eq_zero, cos_sub_cos, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero], rw [← sub_eq_zero, cos_sub_cos, ← sub_neg_eq_add, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul] } end theorem sin_eq_iff_coe_eq_or_add_eq_pi {θ ψ : ℝ} : sin θ = sin ψ ↔ (θ : angle) = ψ ∨ (θ : angle) + ψ = π := begin split, { intro Hsin, rw [← cos_pi_div_two_sub, ← cos_pi_div_two_sub] at Hsin, cases cos_eq_iff_coe_eq_or_eq_neg.mp Hsin with h h, { left, rw [coe_sub, coe_sub] at h, exact sub_right_inj.1 h }, right, rw [coe_sub, coe_sub, eq_neg_iff_add_eq_zero, add_sub, sub_add_eq_add_sub, ← coe_add, add_halves, sub_sub, sub_eq_zero] at h, exact h.symm }, { rw [angle_eq_iff_two_pi_dvd_sub, ←eq_sub_iff_add_eq, ←coe_sub, angle_eq_iff_two_pi_dvd_sub], rintro (⟨k, H⟩ | ⟨k, H⟩), rw [← sub_eq_zero, sin_sub_sin, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul], have H' : θ + ψ = (2 * k) * π + π := by rwa [←sub_add, sub_add_eq_add_sub, sub_eq_iff_eq_add, mul_assoc, mul_comm π _, ←mul_assoc] at H, rw [← sub_eq_zero, sin_sub_sin, H', add_div, mul_assoc 2 _ π, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), cos_add_pi_div_two, sin_int_mul_pi, neg_zero, mul_zero] } end theorem cos_sin_inj {θ ψ : ℝ} (Hcos : cos θ = cos ψ) (Hsin : sin θ = sin ψ) : (θ : angle) = ψ := begin cases cos_eq_iff_coe_eq_or_eq_neg.mp Hcos with hc hc, { exact hc }, cases sin_eq_iff_coe_eq_or_add_eq_pi.mp Hsin with hs hs, { exact hs }, rw [eq_neg_iff_add_eq_zero, hs] at hc, obtain ⟨n, hn⟩ : ∃ n, n • _ = _ := quotient_add_group.left_rel_apply.mp (quotient.exact' hc), rw [← neg_one_mul, add_zero, ← sub_eq_zero, zsmul_eq_mul, ← mul_assoc, ← sub_mul, mul_eq_zero, eq_false_intro (ne_of_gt pi_pos), or_false, sub_neg_eq_add, ← int.cast_zero, ← int.cast_one, ← int.cast_bit0, ← int.cast_mul, ← int.cast_add, int.cast_inj] at hn, have : (n * 2 + 1) % (2:ℤ) = 0 % (2:ℤ) := congr_arg (%(2:ℤ)) hn, rw [add_comm, int.add_mul_mod_self] at this, exact absurd this one_ne_zero end /-- The sine of a `real.angle`. -/ def sin (θ : angle) : ℝ := sin_periodic.lift θ @[simp] lemma sin_coe (x : ℝ) : sin (x : angle) = real.sin x := rfl @[continuity] lemma continuous_sin : continuous sin := real.continuous_sin.quotient_lift_on' _ /-- The cosine of a `real.angle`. -/ def cos (θ : angle) : ℝ := cos_periodic.lift θ @[simp] lemma cos_coe (x : ℝ) : cos (x : angle) = real.cos x := rfl @[continuity] lemma continuous_cos : continuous cos := real.continuous_cos.quotient_lift_on' _ lemma cos_eq_real_cos_iff_eq_or_eq_neg {θ : angle} {ψ : ℝ} : cos θ = real.cos ψ ↔ θ = ψ ∨ θ = -ψ := begin induction θ using real.angle.induction_on, exact cos_eq_iff_coe_eq_or_eq_neg end lemma cos_eq_iff_eq_or_eq_neg {θ ψ : angle} : cos θ = cos ψ ↔ θ = ψ ∨ θ = -ψ := begin induction ψ using real.angle.induction_on, exact cos_eq_real_cos_iff_eq_or_eq_neg end lemma sin_eq_real_sin_iff_eq_or_add_eq_pi {θ : angle} {ψ : ℝ} : sin θ = real.sin ψ ↔ θ = ψ ∨ θ + ψ = π := begin induction θ using real.angle.induction_on, exact sin_eq_iff_coe_eq_or_add_eq_pi end lemma sin_eq_iff_eq_or_add_eq_pi {θ ψ : angle} : sin θ = sin ψ ↔ θ = ψ ∨ θ + ψ = π := begin induction ψ using real.angle.induction_on, exact sin_eq_real_sin_iff_eq_or_add_eq_pi end @[simp] lemma sin_zero : sin (0 : angle) = 0 := by rw [←coe_zero, sin_coe, real.sin_zero] @[simp] lemma sin_coe_pi : sin (π : angle) = 0 := by rw [sin_coe, real.sin_pi] lemma sin_eq_zero_iff {θ : angle} : sin θ = 0 ↔ θ = 0 ∨ θ = π := begin nth_rewrite 0 ←sin_zero, rw sin_eq_iff_eq_or_add_eq_pi, simp end lemma sin_ne_zero_iff {θ : angle} : sin θ ≠ 0 ↔ θ ≠ 0 ∧ θ ≠ π := by rw [←not_or_distrib, ←sin_eq_zero_iff] @[simp] lemma sin_neg (θ : angle) : sin (-θ) = -sin θ := begin induction θ using real.angle.induction_on, exact real.sin_neg _ end lemma sin_antiperiodic : function.antiperiodic sin (π : angle) := begin intro θ, induction θ using real.angle.induction_on, exact real.sin_antiperiodic θ end @[simp] lemma sin_add_pi (θ : angle) : sin (θ + π) = -sin θ := sin_antiperiodic θ @[simp] lemma sin_sub_pi (θ : angle) : sin (θ - π) = -sin θ := sin_antiperiodic.sub_eq θ @[simp] lemma cos_zero : cos (0 : angle) = 1 := by rw [←coe_zero, cos_coe, real.cos_zero] @[simp] lemma cos_coe_pi : cos (π : angle) = -1 := by rw [cos_coe, real.cos_pi] @[simp] lemma cos_neg (θ : angle) : cos (-θ) = cos θ := begin induction θ using real.angle.induction_on, exact real.cos_neg _ end lemma cos_antiperiodic : function.antiperiodic cos (π : angle) := begin intro θ, induction θ using real.angle.induction_on, exact real.cos_antiperiodic θ end @[simp] lemma cos_add_pi (θ : angle) : cos (θ + π) = -cos θ := cos_antiperiodic θ @[simp] lemma cos_sub_pi (θ : angle) : cos (θ - π) = -cos θ := cos_antiperiodic.sub_eq θ @[simp] lemma coe_to_Ico_mod (θ ψ : ℝ) : ↑(to_Ico_mod ψ two_pi_pos θ) = (θ : angle) := begin rw angle_eq_iff_two_pi_dvd_sub, refine ⟨to_Ico_div ψ two_pi_pos θ, _⟩, rw [to_Ico_mod_sub_self, zsmul_eq_mul, mul_comm] end @[simp] lemma coe_to_Ioc_mod (θ ψ : ℝ) : ↑(to_Ioc_mod ψ two_pi_pos θ) = (θ : angle) := begin rw angle_eq_iff_two_pi_dvd_sub, refine ⟨to_Ioc_div ψ two_pi_pos θ, _⟩, rw [to_Ioc_mod_sub_self, zsmul_eq_mul, mul_comm] end /-- Convert a `real.angle` to a real number in the interval `Ioc (-π) π`. -/ def to_real (θ : angle) : ℝ := (to_Ioc_mod_periodic (-π) two_pi_pos).lift θ lemma to_real_coe (θ : ℝ) : (θ : angle).to_real = to_Ioc_mod (-π) two_pi_pos θ := rfl lemma to_real_coe_eq_self_iff {θ : ℝ} : (θ : angle).to_real = θ ↔ -π < θ ∧ θ ≤ π := begin rw [to_real_coe, to_Ioc_mod_eq_self two_pi_pos], ring_nf end lemma to_real_coe_eq_self_iff_mem_Ioc {θ : ℝ} : (θ : angle).to_real = θ ↔ θ ∈ set.Ioc (-π) π := by rw [to_real_coe_eq_self_iff, ←set.mem_Ioc] lemma to_real_injective : function.injective to_real := begin intros θ ψ h, induction θ using real.angle.induction_on, induction ψ using real.angle.induction_on, simpa [to_real_coe, to_Ioc_mod_eq_to_Ioc_mod, zsmul_eq_mul, mul_comm _ (2 * π), ←angle_eq_iff_two_pi_dvd_sub, eq_comm] using h, end @[simp] lemma to_real_inj {θ ψ : angle} : θ.to_real = ψ.to_real ↔ θ = ψ := to_real_injective.eq_iff @[simp] lemma coe_to_real (θ : angle): (θ.to_real : angle) = θ := begin induction θ using real.angle.induction_on, exact coe_to_Ioc_mod _ _ end lemma neg_pi_lt_to_real (θ : angle) : -π < θ.to_real := begin induction θ using real.angle.induction_on, exact left_lt_to_Ioc_mod _ two_pi_pos _ end lemma to_real_le_pi (θ : angle) : θ.to_real ≤ π := begin induction θ using real.angle.induction_on, convert to_Ioc_mod_le_right _ two_pi_pos _, ring end lemma abs_to_real_le_pi (θ : angle) : |θ.to_real| ≤ π := abs_le.2 ⟨(neg_pi_lt_to_real _).le, to_real_le_pi _⟩ lemma to_real_mem_Ioc (θ : angle) : θ.to_real ∈ set.Ioc (-π) π := ⟨neg_pi_lt_to_real _, to_real_le_pi _⟩ @[simp] lemma to_Ioc_mod_to_real (θ : angle): to_Ioc_mod (-π) two_pi_pos θ.to_real = θ.to_real := begin induction θ using real.angle.induction_on, rw to_real_coe, exact to_Ioc_mod_to_Ioc_mod _ _ _ _ end @[simp] lemma to_real_zero : (0 : angle).to_real = 0 := begin rw [←coe_zero, to_real_coe_eq_self_iff], exact ⟨(left.neg_neg_iff.2 real.pi_pos), real.pi_pos.le⟩ end @[simp] lemma to_real_eq_zero_iff {θ : angle} : θ.to_real = 0 ↔ θ = 0 := begin nth_rewrite 0 ←to_real_zero, exact to_real_inj end @[simp] lemma to_real_pi : (π : angle).to_real = π := begin rw [to_real_coe_eq_self_iff], exact ⟨left.neg_lt_self real.pi_pos, le_refl _⟩ end @[simp] lemma to_real_eq_pi_iff {θ : angle} : θ.to_real = π ↔ θ = π := begin nth_rewrite 0 ←to_real_pi, exact to_real_inj end lemma pi_ne_zero : (π : angle) ≠ 0 := begin rw [←to_real_injective.ne_iff, to_real_pi, to_real_zero], exact pi_ne_zero end lemma abs_to_real_coe_eq_self_iff {θ : ℝ} : |(θ : angle).to_real| = θ ↔ 0 ≤ θ ∧ θ ≤ π := ⟨λ h, h ▸ ⟨abs_nonneg _, abs_to_real_le_pi _⟩, λ h, (to_real_coe_eq_self_iff.2 ⟨(left.neg_neg_iff.2 real.pi_pos).trans_le h.1, h.2⟩).symm ▸ abs_eq_self.2 h.1⟩ lemma abs_to_real_neg_coe_eq_self_iff {θ : ℝ} : |(-θ : angle).to_real| = θ ↔ 0 ≤ θ ∧ θ ≤ π := begin refine ⟨λ h, h ▸ ⟨abs_nonneg _, abs_to_real_le_pi _⟩, λ h, _⟩, by_cases hnegpi : θ = π, { simp [hnegpi, real.pi_pos.le] }, rw [←coe_neg, to_real_coe_eq_self_iff.2 ⟨neg_lt_neg (lt_of_le_of_ne h.2 hnegpi), (neg_nonpos.2 h.1).trans real.pi_pos.le⟩, abs_neg, abs_eq_self.2 h.1] end @[simp] lemma sin_to_real (θ : angle) : real.sin θ.to_real = sin θ := begin conv_rhs { rw ←coe_to_real θ }, refl end @[simp] lemma cos_to_real (θ : angle) : real.cos θ.to_real = cos θ := begin conv_rhs { rw ←coe_to_real θ }, refl end /-- The sign of a `real.angle` is `0` if the angle is `0` or `π`, `1` if the angle is strictly between `0` and `π` and `-1` is the angle is strictly between `-π` and `0`. It is defined as the sign of the sine of the angle. -/ def sign (θ : angle) : sign_type := sign (sin θ) @[simp] lemma sign_zero : (0 : angle).sign = 0 := by rw [sign, sin_zero, sign_zero] @[simp] lemma sign_coe_pi : (π : angle).sign = 0 := by rw [sign, sin_coe_pi, _root_.sign_zero] @[simp] lemma sign_neg (θ : angle) : (-θ).sign = - θ.sign := by simp_rw [sign, sin_neg, left.sign_neg] lemma sign_antiperiodic : function.antiperiodic sign (π : angle) := λ θ, by rw [sign, sign, sin_add_pi, left.sign_neg] @[simp] lemma sign_add_pi (θ : angle) : (θ + π).sign = -θ.sign := sign_antiperiodic θ @[simp] lemma sign_pi_add (θ : angle) : ((π : angle) + θ).sign = -θ.sign := by rw [add_comm, sign_add_pi] @[simp] lemma sign_sub_pi (θ : angle) : (θ - π).sign = -θ.sign := sign_antiperiodic.sub_eq θ @[simp] lemma sign_pi_sub (θ : angle) : ((π : angle) - θ).sign = θ.sign := by simp [sign_antiperiodic.sub_eq'] lemma sign_eq_zero_iff {θ : angle} : θ.sign = 0 ↔ θ = 0 ∨ θ = π := by rw [sign, sign_eq_zero_iff, sin_eq_zero_iff] lemma sign_ne_zero_iff {θ : angle} : θ.sign ≠ 0 ↔ θ ≠ 0 ∧ θ ≠ π := by rw [←not_or_distrib, ←sign_eq_zero_iff] lemma to_real_neg_iff_sign_neg {θ : angle} : θ.to_real < 0 ↔ θ.sign = -1 := begin rw [sign, ←sin_to_real, sign_eq_neg_one_iff], rcases lt_trichotomy θ.to_real 0 with (h|h|h), { exact ⟨λ _, real.sin_neg_of_neg_of_neg_pi_lt h (neg_pi_lt_to_real θ), λ _, h⟩ }, { simp [h] }, { exact ⟨λ hn, false.elim (h.asymm hn), λ hn, false.elim (hn.not_le (sin_nonneg_of_nonneg_of_le_pi h.le (to_real_le_pi θ)))⟩ } end lemma to_real_nonneg_iff_sign_nonneg {θ : angle} : 0 ≤ θ.to_real ↔ 0 ≤ θ.sign := begin rcases lt_trichotomy θ.to_real 0 with (h|h|h), { refine ⟨λ hn, false.elim (h.not_le hn), λ hn, _⟩, rw [to_real_neg_iff_sign_neg.1 h] at hn, exact false.elim (hn.not_lt dec_trivial) }, { simp [h, sign, ←sin_to_real] }, { refine ⟨λ _, _, λ _, h.le⟩, rw [sign, ←sin_to_real, sign_nonneg_iff], exact sin_nonneg_of_nonneg_of_le_pi h.le (to_real_le_pi θ) } end @[simp] lemma sign_to_real {θ : angle} (h : θ ≠ π) : _root_.sign θ.to_real = θ.sign := begin rcases lt_trichotomy θ.to_real 0 with (ht|ht|ht), { simp [ht, to_real_neg_iff_sign_neg.1 ht] }, { simp [sign, ht, ←sin_to_real] }, { rw [sign, ←sin_to_real, sign_pos ht, sign_pos (sin_pos_of_pos_of_lt_pi ht ((to_real_le_pi θ).lt_of_ne (to_real_eq_pi_iff.not.2 h)))] } end lemma coe_abs_to_real_of_sign_nonneg {θ : angle} (h : 0 ≤ θ.sign) : ↑|θ.to_real| = θ := by rw [abs_eq_self.2 (to_real_nonneg_iff_sign_nonneg.2 h), coe_to_real] lemma neg_coe_abs_to_real_of_sign_nonpos {θ : angle} (h : θ.sign ≤ 0) : -↑|θ.to_real| = θ := begin rw sign_type.nonpos_iff at h, rcases h with h|h, { rw [abs_of_neg (to_real_neg_iff_sign_neg.2 h), coe_neg, neg_neg, coe_to_real] }, { rw sign_eq_zero_iff at h, rcases h with rfl|rfl; simp [abs_of_pos real.pi_pos] } end lemma eq_iff_sign_eq_and_abs_to_real_eq {θ ψ : angle} : θ = ψ ↔ θ.sign = ψ.sign ∧ |θ.to_real| = |ψ.to_real| := begin refine ⟨_, λ h, _⟩, { rintro rfl, exact ⟨rfl, rfl⟩ }, rcases h with ⟨hs, hr⟩, rw abs_eq_abs at hr, rcases hr with (hr|hr), { exact to_real_injective hr }, { by_cases h : θ = π, { rw [h, to_real_pi, eq_neg_iff_eq_neg] at hr, exact false.elim ((neg_pi_lt_to_real ψ).ne hr.symm) }, { by_cases h' : ψ = π, { rw [h', to_real_pi] at hr, exact false.elim ((neg_pi_lt_to_real θ).ne hr.symm) }, { rw [←sign_to_real h, ←sign_to_real h', hr, left.sign_neg, sign_type.neg_eq_self_iff, _root_.sign_eq_zero_iff, to_real_eq_zero_iff] at hs, rw [hs, to_real_zero, neg_zero, to_real_eq_zero_iff] at hr, rw [hr, hs] } } } end lemma eq_iff_abs_to_real_eq_of_sign_eq {θ ψ : angle} (h : θ.sign = ψ.sign) : θ = ψ ↔ |θ.to_real| = |ψ.to_real| := by simpa [h] using @eq_iff_sign_eq_and_abs_to_real_eq θ ψ lemma continuous_at_sign {θ : angle} (h0 : θ ≠ 0) (hpi : θ ≠ π) : continuous_at sign θ := (continuous_at_sign_of_ne_zero (sin_ne_zero_iff.2 ⟨h0, hpi⟩)).comp continuous_sin.continuous_at lemma _root_.continuous_on.angle_sign_comp {α : Type*} [topological_space α] {f : α → angle} {s : set α} (hf : continuous_on f s) (hs : ∀ z ∈ s, f z ≠ 0 ∧ f z ≠ π) : continuous_on (sign ∘ f) s := begin refine (continuous_at.continuous_on (λ θ hθ, _)).comp hf (set.maps_to_image f s), obtain ⟨z, hz, rfl⟩ := hθ, exact continuous_at_sign (hs _ hz).1 (hs _ hz).2 end /-- Suppose a function to angles is continuous on a connected set and never takes the values `0` or `π` on that set. Then the values of the function on that set all have the same sign. -/ lemma sign_eq_of_continuous_on {α : Type*} [topological_space α] {f : α → angle} {s : set α} {x y : α} (hc : is_connected s) (hf : continuous_on f s) (hs : ∀ z ∈ s, f z ≠ 0 ∧ f z ≠ π) (hx : x ∈ s) (hy : y ∈ s) : (f y).sign = (f x).sign := (hc.image _ (hf.angle_sign_comp hs)).is_preconnected.subsingleton (set.mem_image_of_mem _ hy) (set.mem_image_of_mem _ hx) end angle end real
5c98cc62d4c32e088e57811a744434ba7dbd5826
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/lattice_group.lean
2a18a28a237f676313503d438ab9e594d89e9bcb
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
19,215
lean
/- Copyright (c) 2021 Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Christopher Hoskin -/ import algebra.group_power.basic -- Needed for squares import algebra.order.group.abs import tactic.nth_rewrite /-! # Lattice ordered groups Lattice ordered groups were introduced by [Birkhoff][birkhoff1942]. They form the algebraic underpinnings of vector lattices, Banach lattices, AL-space, AM-space etc. This file develops the basic theory, concentrating on the commutative case. ## Main statements - `pos_div_neg`: Every element `a` of a lattice ordered commutative group has a decomposition `a⁺-a⁻` into the difference of the positive and negative component. - `pos_inf_neg_eq_one`: The positive and negative components are coprime. - `abs_triangle`: The absolute value operation satisfies the triangle inequality. It is shown that the inf and sup operations are related to the absolute value operation by a number of equations and inequalities. ## Notations - `a⁺ = a ⊔ 0`: The *positive component* of an element `a` of a lattice ordered commutative group - `a⁻ = (-a) ⊔ 0`: The *negative component* of an element `a` of a lattice ordered commutative group - `|a| = a⊔(-a)`: The *absolute value* of an element `a` of a lattice ordered commutative group ## Implementation notes A lattice ordered commutative group is a type `α` satisfying: * `[lattice α]` * `[comm_group α]` * `[covariant_class α α (*) (≤)]` The remainder of the file establishes basic properties of lattice ordered commutative groups. A number of these results also hold in the non-commutative case ([Birkhoff][birkhoff1942], [Fuchs][fuchs1963]) but we have not developed that here, since we are primarily interested in vector lattices. ## References * [Birkhoff, Lattice-ordered Groups][birkhoff1942] * [Bourbaki, Algebra II][bourbaki1981] * [Fuchs, Partially Ordered Algebraic Systems][fuchs1963] * [Zaanen, Lectures on "Riesz Spaces"][zaanen1966] * [Banasiak, Banach Lattices in Applications][banasiak] ## Tags lattice, ordered, group -/ universe u -- A linearly ordered additive commutative group is a lattice ordered commutative group @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_covariant_class (α : Type u) [linear_ordered_comm_group α] : covariant_class α α (*) (≤) := { elim := λ a b c bc, linear_ordered_comm_group.mul_le_mul_left _ _ bc a } variables {α : Type u} [lattice α] [comm_group α] -- Special case of Bourbaki A.VI.9 (1) -- c + (a ⊔ b) = (c + a) ⊔ (c + b) @[to_additive] lemma mul_sup [covariant_class α α (*) (≤)] (a b c : α) : c * (a ⊔ b) = (c * a) ⊔ (c * b) := begin refine le_antisymm _ (by simp), rw [← mul_le_mul_iff_left (c⁻¹), ← mul_assoc, inv_mul_self, one_mul], exact sup_le (by simp) (by simp), end @[to_additive] lemma mul_inf [covariant_class α α (*) (≤)] (a b c : α) : c * (a ⊓ b) = (c * a) ⊓ (c * b) := begin refine le_antisymm (by simp) _, rw [← mul_le_mul_iff_left (c⁻¹), ← mul_assoc, inv_mul_self, one_mul], exact le_inf (by simp) (by simp), end -- Special case of Bourbaki A.VI.9 (2) -- -(a ⊔ b)=(-a) ⊓ (-b) @[to_additive] lemma inv_sup_eq_inv_inf_inv [covariant_class α α (*) (≤)] (a b : α) : (a ⊔ b)⁻¹ = a⁻¹ ⊓ b⁻¹ := begin apply le_antisymm, { refine le_inf _ _, { rw inv_le_inv_iff, exact le_sup_left, }, { rw inv_le_inv_iff, exact le_sup_right, } }, { rw [← inv_le_inv_iff, inv_inv], refine sup_le _ _, { rw ← inv_le_inv_iff, simp, }, { rw ← inv_le_inv_iff, simp, } } end -- -(a ⊓ b) = -a ⊔ -b @[to_additive] lemma inv_inf_eq_sup_inv [covariant_class α α (*) (≤)] (a b : α) : (a ⊓ b)⁻¹ = a⁻¹ ⊔ b⁻¹ := by rw [← inv_inv (a⁻¹ ⊔ b⁻¹), inv_sup_eq_inv_inf_inv a⁻¹ b⁻¹, inv_inv, inv_inv] -- Bourbaki A.VI.10 Prop 7 -- a ⊓ b + (a ⊔ b) = a + b @[to_additive] lemma inf_mul_sup [covariant_class α α (*) (≤)] (a b : α) : (a ⊓ b) * (a ⊔ b) = a * b := calc (a ⊓ b) * (a ⊔ b) = (a ⊓ b) * ((a * b) * (b⁻¹ ⊔ a⁻¹)) : by { rw mul_sup b⁻¹ a⁻¹ (a * b), simp, } ... = (a ⊓ b) * ((a * b) * (a ⊓ b)⁻¹) : by rw [inv_inf_eq_sup_inv, sup_comm] ... = a * b : by rw [mul_comm, inv_mul_cancel_right] namespace lattice_ordered_comm_group /-- Let `α` be a lattice ordered commutative group with identity `1`. For an element `a` of type `α`, the element `a ⊔ 1` is said to be the *positive component* of `a`, denoted `a⁺`. -/ @[to_additive /-" Let `α` be a lattice ordered commutative group with identity `0`. For an element `a` of type `α`, the element `a ⊔ 0` is said to be the *positive component* of `a`, denoted `a⁺`. "-/, priority 100] -- see Note [lower instance priority] instance has_one_lattice_has_pos_part : has_pos_part (α) := ⟨λ a, a ⊔ 1⟩ @[to_additive pos_part_def] lemma m_pos_part_def (a : α) : a⁺ = a ⊔ 1 := rfl /-- Let `α` be a lattice ordered commutative group with identity `1`. For an element `a` of type `α`, the element `(-a) ⊔ 1` is said to be the *negative component* of `a`, denoted `a⁻`. -/ @[to_additive /-" Let `α` be a lattice ordered commutative group with identity `0`. For an element `a` of type `α`, the element `(-a) ⊔ 0` is said to be the *negative component* of `a`, denoted `a⁻`. "-/, priority 100] -- see Note [lower instance priority] instance has_one_lattice_has_neg_part : has_neg_part (α) := ⟨λ a, a⁻¹ ⊔ 1⟩ @[to_additive neg_part_def] lemma m_neg_part_def (a : α) : a⁻ = a⁻¹ ⊔ 1 := rfl @[simp, to_additive] lemma pos_one : (1 : α)⁺ = 1 := sup_idem @[simp, to_additive] lemma neg_one : (1 : α)⁻ = 1 := by rw [m_neg_part_def, inv_one, sup_idem] -- a⁻ = -(a ⊓ 0) @[to_additive] lemma neg_eq_inv_inf_one [covariant_class α α (*) (≤)] (a : α) : a⁻ = (a ⊓ 1)⁻¹ := by rw [m_neg_part_def, ← inv_inj, inv_sup_eq_inv_inf_inv, inv_inv, inv_inv, inv_one] @[to_additive le_abs] lemma le_mabs (a : α) : a ≤ |a| := le_sup_left @[to_additive] -- -a ≤ |a| lemma inv_le_abs (a : α) : a⁻¹ ≤ |a| := le_sup_right -- 0 ≤ a⁺ @[to_additive pos_nonneg] lemma one_le_pos (a : α) : 1 ≤ a⁺ := le_sup_right -- 0 ≤ a⁻ @[to_additive neg_nonneg] lemma one_le_neg (a : α) : 1 ≤ a⁻ := le_sup_right @[to_additive] -- pos_nonpos_iff lemma pos_le_one_iff {a : α} : a⁺ ≤ 1 ↔ a ≤ 1 := by { rw [m_pos_part_def, sup_le_iff], simp, } @[to_additive] -- neg_nonpos_iff lemma neg_le_one_iff {a : α} : a⁻ ≤ 1 ↔ a⁻¹ ≤ 1 := by { rw [m_neg_part_def, sup_le_iff], simp, } @[to_additive] lemma pos_eq_one_iff {a : α} : a⁺ = 1 ↔ a ≤ 1 := by { rw le_antisymm_iff, simp only [one_le_pos, and_true], exact pos_le_one_iff, } @[to_additive] lemma neg_eq_one_iff' {a : α} : a⁻ = 1 ↔ a⁻¹ ≤ 1 := by { rw le_antisymm_iff, simp only [one_le_neg, and_true], rw neg_le_one_iff, } @[to_additive] lemma neg_eq_one_iff [covariant_class α α has_mul.mul has_le.le] {a : α} : a⁻ = 1 ↔ 1 ≤ a := by { rw le_antisymm_iff, simp only [one_le_neg, and_true], rw [neg_le_one_iff, inv_le_one'], } @[to_additive le_pos] lemma m_le_pos (a : α) : a ≤ a⁺ := le_sup_left -- -a ≤ a⁻ @[to_additive] lemma inv_le_neg (a : α) : a⁻¹ ≤ a⁻ := le_sup_left -- Bourbaki A.VI.12 -- a⁻ = (-a)⁺ @[to_additive] lemma neg_eq_pos_inv (a : α) : a⁻ = (a⁻¹)⁺ := rfl -- a⁺ = (-a)⁻ @[to_additive] lemma pos_eq_neg_inv (a : α) : a⁺ = (a⁻¹)⁻ := by simp [neg_eq_pos_inv] -- We use this in Bourbaki A.VI.12 Prop 9 a) -- c + (a ⊓ b) = (c + a) ⊓ (c + b) @[to_additive] lemma mul_inf_eq_mul_inf_mul [covariant_class α α (*) (≤)] (a b c : α) : c * (a ⊓ b) = (c * a) ⊓ (c * b) := begin refine le_antisymm (by simp) _, rw [← mul_le_mul_iff_left c⁻¹, ← mul_assoc, inv_mul_self, one_mul, le_inf_iff], simp, end -- Bourbaki A.VI.12 Prop 9 a) -- a = a⁺ - a⁻ @[simp, to_additive] lemma pos_div_neg [covariant_class α α (*) (≤)] (a : α) : a⁺ / a⁻ = a := begin symmetry, rw div_eq_mul_inv, apply eq_mul_inv_of_mul_eq, rw [m_neg_part_def, mul_sup, mul_one, mul_right_inv, sup_comm, m_pos_part_def], end -- Bourbaki A.VI.12 Prop 9 a) -- a⁺ ⊓ a⁻ = 0 (`a⁺` and `a⁻` are co-prime, and, since they are positive, disjoint) @[to_additive] lemma pos_inf_neg_eq_one [covariant_class α α (*) (≤)] (a : α) : a⁺ ⊓ a⁻ = 1 := by rw [←mul_right_inj (a⁻)⁻¹, mul_inf_eq_mul_inf_mul, mul_one, mul_left_inv, mul_comm, ← div_eq_mul_inv, pos_div_neg, neg_eq_inv_inf_one, inv_inv] -- Bourbaki A.VI.12 (with a and b swapped) -- a⊔b = b + (a - b)⁺ @[to_additive] lemma sup_eq_mul_pos_div [covariant_class α α (*) (≤)] (a b : α) : a ⊔ b = b * (a / b)⁺ := calc a ⊔ b = (b * (a / b)) ⊔ (b * 1) : by rw [mul_one b, div_eq_mul_inv, mul_comm a, mul_inv_cancel_left] ... = b * ((a / b) ⊔ 1) : by rw ← mul_sup (a / b) 1 b -- Bourbaki A.VI.12 (with a and b swapped) -- a⊓b = a - (a - b)⁺ @[to_additive] lemma inf_eq_div_pos_div [covariant_class α α (*) (≤)] (a b : α) : a ⊓ b = a / (a / b)⁺ := calc a ⊓ b = (a * 1) ⊓ (a * (b / a)) : by { rw [mul_one a, div_eq_mul_inv, mul_comm b, mul_inv_cancel_left], } ... = a * (1 ⊓ (b / a)) : by rw ← mul_inf_eq_mul_inf_mul 1 (b / a) a ... = a * ((b / a) ⊓ 1) : by rw inf_comm ... = a * ((a / b)⁻¹ ⊓ 1) : by { rw div_eq_mul_inv, nth_rewrite 0 ← inv_inv b, rw [← mul_inv, mul_comm b⁻¹, ← div_eq_mul_inv], } ... = a * ((a / b)⁻¹ ⊓ 1⁻¹) : by rw inv_one ... = a / ((a / b) ⊔ 1) : by rw [← inv_sup_eq_inv_inf_inv, ← div_eq_mul_inv] -- Bourbaki A.VI.12 Prop 9 c) @[to_additive le_iff_pos_le_neg_ge] lemma m_le_iff_pos_le_neg_ge [covariant_class α α (*) (≤)] (a b : α) : a ≤ b ↔ a⁺ ≤ b⁺ ∧ b⁻ ≤ a⁻ := begin split; intro h, { split, { exact sup_le (h.trans (m_le_pos b)) (one_le_pos b), }, { rw ← inv_le_inv_iff at h, exact sup_le (h.trans (inv_le_neg a)) (one_le_neg a), } }, { rw [← pos_div_neg a, ← pos_div_neg b], exact div_le_div'' h.1 h.2, } end @[to_additive neg_abs] lemma m_neg_abs [covariant_class α α (*) (≤)] (a : α) : |a|⁻ = 1 := begin refine le_antisymm _ _, { rw ← pos_inf_neg_eq_one a, apply le_inf, { rw pos_eq_neg_inv, exact ((m_le_iff_pos_le_neg_ge _ _).mp (inv_le_abs a)).right, }, { exact and.right (iff.elim_left (m_le_iff_pos_le_neg_ge _ _) (le_mabs a)), } }, { exact one_le_neg _, } end @[to_additive pos_abs] lemma m_pos_abs [covariant_class α α (*) (≤)] (a : α) : |a|⁺ = |a| := begin nth_rewrite 1 ← pos_div_neg (|a|), rw div_eq_mul_inv, symmetry, rw [mul_right_eq_self, inv_eq_one], exact m_neg_abs a, end @[to_additive abs_nonneg] lemma one_le_abs [covariant_class α α (*) (≤)] (a : α) : 1 ≤ |a| := by { rw ← m_pos_abs, exact one_le_pos _, } -- The proof from Bourbaki A.VI.12 Prop 9 d) -- |a| = a⁺ - a⁻ @[to_additive] lemma pos_mul_neg [covariant_class α α (*) (≤)] (a : α) : |a| = a⁺ * a⁻ := begin refine le_antisymm _ _, { refine sup_le _ _, { nth_rewrite 0 ← mul_one a, exact mul_le_mul' (m_le_pos a) (one_le_neg a) }, { nth_rewrite 0 ← one_mul (a⁻¹), exact mul_le_mul' (one_le_pos a) (inv_le_neg a) } }, { rw [← inf_mul_sup, pos_inf_neg_eq_one, one_mul, ← m_pos_abs a], apply sup_le, { exact ((m_le_iff_pos_le_neg_ge _ _).mp (le_mabs a)).left, }, { rw neg_eq_pos_inv, exact ((m_le_iff_pos_le_neg_ge _ _).mp (inv_le_abs a)).left, }, } end -- a ⊔ b - (a ⊓ b) = |b - a| @[to_additive] lemma sup_div_inf_eq_abs_div [covariant_class α α (*) (≤)] (a b : α) : (a ⊔ b) / (a ⊓ b) = |b / a| := begin rw [sup_eq_mul_pos_div, inf_comm, inf_eq_div_pos_div, div_eq_mul_inv], nth_rewrite 1 div_eq_mul_inv, rw [mul_inv_rev, inv_inv, mul_comm, ← mul_assoc, inv_mul_cancel_right, pos_eq_neg_inv (a / b)], nth_rewrite 1 div_eq_mul_inv, rw [mul_inv_rev, ← div_eq_mul_inv, inv_inv, ← pos_mul_neg], end -- 2•(a ⊔ b) = a + b + |b - a| @[to_additive two_sup_eq_add_add_abs_sub] lemma sup_sq_eq_mul_mul_abs_div [covariant_class α α (*) (≤)] (a b : α) : (a ⊔ b)^2 = a * b * |b / a| := by rw [← inf_mul_sup a b, ← sup_div_inf_eq_abs_div, div_eq_mul_inv, ← mul_assoc, mul_comm, mul_assoc, ← pow_two, inv_mul_cancel_left] -- 2•(a ⊓ b) = a + b - |b - a| @[to_additive two_inf_eq_add_sub_abs_sub] lemma inf_sq_eq_mul_div_abs_div [covariant_class α α (*) (≤)] (a b : α) : (a ⊓ b)^2 = a * b / |b / a| := by rw [← inf_mul_sup a b, ← sup_div_inf_eq_abs_div, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv, mul_assoc, mul_inv_cancel_comm_assoc, ← pow_two] /-- Every lattice ordered commutative group is a distributive lattice -/ @[to_additive "Every lattice ordered commutative additive group is a distributive lattice" ] def lattice_ordered_comm_group_to_distrib_lattice (α : Type u) [s: lattice α] [comm_group α] [covariant_class α α (*) (≤)] : distrib_lattice α := { le_sup_inf := begin intros, rw [← mul_le_mul_iff_left (x ⊓ (y ⊓ z)), inf_mul_sup x (y ⊓ z), ← inv_mul_le_iff_le_mul, le_inf_iff], split, { rw [inv_mul_le_iff_le_mul, ← inf_mul_sup x y], apply mul_le_mul', { apply inf_le_inf_left, apply inf_le_left, }, { apply inf_le_left, } }, { rw [inv_mul_le_iff_le_mul, ← inf_mul_sup x z], apply mul_le_mul', { apply inf_le_inf_left, apply inf_le_right, }, { apply inf_le_right, }, } end, ..s } -- See, e.g. Zaanen, Lectures on Riesz Spaces -- 3rd lecture -- |a ⊔ c - (b ⊔ c)| + |a ⊓ c-b ⊓ c| = |a - b| @[to_additive] theorem abs_div_sup_mul_abs_div_inf [covariant_class α α (*) (≤)] (a b c : α) : |(a ⊔ c) / (b ⊔ c)| * |(a ⊓ c) / (b ⊓ c)| = |a / b| := begin letI : distrib_lattice α := lattice_ordered_comm_group_to_distrib_lattice α, calc |(a ⊔ c) / (b ⊔ c)| * |(a ⊓ c) / (b ⊓ c)| = ((b ⊔ c ⊔ (a ⊔ c)) / ((b ⊔ c) ⊓ (a ⊔ c))) * |(a ⊓ c) / (b ⊓ c)| : by rw sup_div_inf_eq_abs_div ... = (b ⊔ c ⊔ (a ⊔ c)) / ((b ⊔ c) ⊓ (a ⊔ c)) * (((b ⊓ c) ⊔ (a ⊓ c)) / ((b ⊓ c) ⊓ (a ⊓ c))) : by rw sup_div_inf_eq_abs_div (b ⊓ c) (a ⊓ c) ... = (b ⊔ a ⊔ c) / ((b ⊓ a) ⊔ c) * (((b ⊔ a) ⊓ c) / (b ⊓ a ⊓ c)) : by { rw [← sup_inf_right, ← inf_sup_right, sup_assoc], nth_rewrite 1 sup_comm, rw [sup_right_idem, sup_assoc, inf_assoc], nth_rewrite 3 inf_comm, rw [inf_right_idem, inf_assoc], } ... = (b ⊔ a ⊔ c) * ((b ⊔ a) ⊓ c) /(((b ⊓ a) ⊔ c) * (b ⊓ a ⊓ c)) : by rw div_mul_div_comm ... = (b ⊔ a) * c / ((b ⊓ a) * c) : by rw [mul_comm, inf_mul_sup, mul_comm (b ⊓ a ⊔ c), inf_mul_sup] ... = (b ⊔ a) / (b ⊓ a) : by rw [div_eq_mul_inv, mul_inv_rev, mul_assoc, mul_inv_cancel_left, ← div_eq_mul_inv] ... = |a / b| : by rw sup_div_inf_eq_abs_div end /-- If `a` is positive, then it is equal to its positive component `a⁺`. -/ -- pos_of_nonneg @[to_additive "If `a` is positive, then it is equal to its positive component `a⁺`."] lemma pos_of_one_le (a : α) (h : 1 ≤ a) : a⁺ = a := by { rw m_pos_part_def, exact sup_of_le_left h, } @[to_additive] -- pos_eq_self_of_pos_pos lemma pos_eq_self_of_one_lt_pos {α} [linear_order α] [comm_group α] {x : α} (hx : 1 < x⁺) : x⁺ = x := begin rw [m_pos_part_def, right_lt_sup, not_le] at hx, rw [m_pos_part_def, sup_eq_left], exact hx.le end -- 0 ≤ a implies a⁺ = a @[to_additive] -- pos_of_nonpos lemma pos_of_le_one (a : α) (h : a ≤ 1) : a⁺ = 1 := pos_eq_one_iff.mpr h @[to_additive neg_of_inv_nonneg] lemma neg_of_one_le_inv (a : α) (h : 1 ≤ a⁻¹) : a⁻ = a⁻¹ := by { rw neg_eq_pos_inv, exact pos_of_one_le _ h, } @[to_additive] -- neg_of_neg_nonpos lemma neg_of_inv_le_one (a : α) (h : a⁻¹ ≤ 1) : a⁻ = 1 := neg_eq_one_iff'.mpr h @[to_additive] -- neg_of_nonpos lemma neg_of_le_one [covariant_class α α (*) (≤)] (a : α) (h : a ≤ 1) : a⁻ = a⁻¹ := by { refine neg_of_one_le_inv _ _, rw one_le_inv', exact h, } @[to_additive] -- neg_of_nonneg' lemma neg_of_one_le [covariant_class α α (*) (≤)] (a : α) (h : 1 ≤ a) : a⁻ = 1 := neg_eq_one_iff.mpr h -- 0 ≤ a implies |a| = a @[to_additive abs_of_nonneg] lemma mabs_of_one_le [covariant_class α α (*) (≤)] (a : α) (h : 1 ≤ a) : |a| = a := begin unfold has_abs.abs, rw [sup_eq_mul_pos_div, div_eq_mul_inv, inv_inv, ← pow_two, inv_mul_eq_iff_eq_mul, ← pow_two, pos_of_one_le], rw pow_two, apply one_le_mul h h, end /-- The unary operation of taking the absolute value is idempotent. -/ @[simp, to_additive abs_abs "The unary operation of taking the absolute value is idempotent."] lemma mabs_mabs [covariant_class α α (*) (≤)] (a : α) : | |a| | = |a| := mabs_of_one_le _ (one_le_abs _) @[to_additive abs_sup_sub_sup_le_abs] lemma mabs_sup_div_sup_le_mabs [covariant_class α α (*) (≤)] (a b c : α) : |(a ⊔ c) / (b ⊔ c)| ≤ |a / b| := begin apply le_of_mul_le_of_one_le_left, { rw abs_div_sup_mul_abs_div_inf, }, { exact one_le_abs _, }, end @[to_additive abs_inf_sub_inf_le_abs] lemma mabs_inf_div_inf_le_mabs [covariant_class α α (*) (≤)] (a b c : α) : |(a ⊓ c) / (b ⊓ c)| ≤ |a / b| := begin apply le_of_mul_le_of_one_le_right, { rw abs_div_sup_mul_abs_div_inf, }, { exact one_le_abs _, }, end -- Commutative case, Zaanen, 3rd lecture -- For the non-commutative case, see Birkhoff Theorem 19 (27) -- |(a ⊔ c) - (b ⊔ c)| ⊔ |(a ⊓ c) - (b ⊓ c)| ≤ |a - b| @[to_additive Birkhoff_inequalities] theorem m_Birkhoff_inequalities [covariant_class α α (*) (≤)] (a b c : α) : |(a ⊔ c) / (b ⊔ c)| ⊔ |(a ⊓ c) / (b ⊓ c)| ≤ |a / b| := sup_le (mabs_sup_div_sup_le_mabs a b c) (mabs_inf_div_inf_le_mabs a b c) -- Banasiak Proposition 2.12, Zaanen 2nd lecture /-- The absolute value satisfies the triangle inequality. -/ @[to_additive abs_add_le "The absolute value satisfies the triangle inequality."] lemma mabs_mul_le [covariant_class α α (*) (≤)] (a b : α) : |a * b| ≤ |a| * |b| := begin apply sup_le, { exact mul_le_mul' (le_mabs a) (le_mabs b), }, { rw mul_inv, exact mul_le_mul' (inv_le_abs _) (inv_le_abs _), } end -- |a - b| = |b - a| @[to_additive] lemma abs_inv_comm (a b : α) : |a/b| = |b/a| := begin unfold has_abs.abs, rw [inv_div a b, ← inv_inv (a / b), inv_div, sup_comm], end -- | |a| - |b| | ≤ |a - b| @[to_additive] lemma abs_abs_div_abs_le [covariant_class α α (*) (≤)] (a b : α) : | |a| / |b| | ≤ |a / b| := begin unfold has_abs.abs, rw sup_le_iff, split, { apply div_le_iff_le_mul.2, convert mabs_mul_le (a/b) b, { rw div_mul_cancel', }, { rw div_mul_cancel', }, { exact covariant_swap_mul_le_of_covariant_mul_le α, } }, { rw [div_eq_mul_inv, mul_inv_rev, inv_inv, mul_inv_le_iff_le_mul, ← abs_eq_sup_inv (a / b), abs_inv_comm], convert mabs_mul_le (b/a) a, { rw div_mul_cancel', }, {rw div_mul_cancel', } }, end end lattice_ordered_comm_group
177af15113299bf7983ed30f16396a668a4feebb
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Meta/SizeOf.lean
a709ef14c1776ef1f653f9b4be9ebd2dd5898e89
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
23,409
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.AppBuilder import Lean.Meta.Instances namespace Lean.Meta /-- Create `SizeOf` local instances for applicable parameters, and execute `k` using them. -/ private partial def mkLocalInstances (params : Array Expr) (k : Array Expr → MetaM α) : MetaM α := loop 0 #[] where loop (i : Nat) (insts : Array Expr) : MetaM α := do if i < params.size then let param := params[i]! let paramType ← inferType param let instType? ← forallTelescopeReducing paramType fun xs _ => do let type := mkAppN param xs try let sizeOf ← mkAppM `SizeOf #[type] let instType ← mkForallFVars xs sizeOf return some instType catch _ => return none match instType? with | none => loop (i+1) insts | some instType => let instName ← mkFreshUserName `inst withLocalDecl instName BinderInfo.instImplicit instType fun inst => loop (i+1) (insts.push inst) else k insts /-- Return `some x` if `fvar` has type of the form `... -> motive ... fvar` where `motive` in `motiveFVars`. That is, `x` "produces" one of the recursor motives. -/ private def isInductiveHypothesis? (motiveFVars : Array Expr) (fvar : Expr) : MetaM (Option Expr) := do forallTelescopeReducing (← inferType fvar) fun _ type => if type.isApp && motiveFVars.contains type.getAppFn then return some type.appArg! else return none private def isInductiveHypothesis (motiveFVars : Array Expr) (fvar : Expr) : MetaM Bool := return (← isInductiveHypothesis? motiveFVars fvar).isSome /-- Let `motiveFVars` be free variables for each motive in a kernel recursor, and `minorFVars` the free variables for a minor premise. Then, return `some idx` if `minorFVars[idx]` has a type of the form `... -> motive ... fvar` for some `motive` in `motiveFVars`. -/ private def isRecField? (motiveFVars : Array Expr) (minorFVars : Array Expr) (fvar : Expr) : MetaM (Option Nat) := do let mut idx := 0 for minorFVar in minorFVars do if let some fvar' ← isInductiveHypothesis? motiveFVars minorFVar then if fvar == fvar'.getAppFn then return some idx idx := idx + 1 return none private partial def mkSizeOfMotives (motiveFVars : Array Expr) (k : Array Expr → MetaM α) : MetaM α := loop 0 #[] where loop (i : Nat) (motives : Array Expr) : MetaM α := do if i < motiveFVars.size then let type ← inferType motiveFVars[i]! let motive ← forallTelescopeReducing type fun xs _ => do mkLambdaFVars xs <| mkConst ``Nat trace[Meta.sizeOf] "motive: {motive}" loop (i+1) (motives.push motive) else k motives private partial def ignoreField (x : Expr) : MetaM Bool := do let type ← whnf (← inferType x) if type.isForall then -- TODO: add support for finite domains if type.isArrow && type.bindingDomain!.isConstOf ``Unit then ignoreField type.bindingBody! else return true else return false /-- See `ignoreField`. We have support for functions of the form `Unit → ...` -/ private partial def mkSizeOfRecFieldFormIH (ih : Expr) : MetaM Expr := do if (← whnf (← inferType ih)).isForall then mkSizeOfRecFieldFormIH (mkApp ih (mkConst ``Unit.unit)) else return ih private partial def mkSizeOfMinors (motiveFVars : Array Expr) (minorFVars : Array Expr) (minorFVars' : Array Expr) (k : Array Expr → MetaM α) : MetaM α := assert! minorFVars.size == minorFVars'.size loop 0 #[] where loop (i : Nat) (minors : Array Expr) : MetaM α := do if i < minorFVars.size then forallTelescopeReducing (← inferType minorFVars[i]!) fun xs _ => do forallBoundedTelescope (← inferType minorFVars'[i]!) xs.size fun xs' _ => do let mut minor ← mkNumeral (mkConst ``Nat) 1 for x in xs, x' in xs' do unless (← isInductiveHypothesis motiveFVars x) do unless (← ignoreField x) do -- we suppress higher-order fields match (← isRecField? motiveFVars xs x) with | some idx => minor ← mkAdd minor (← mkSizeOfRecFieldFormIH xs'[idx]!) | none => minor ← mkAdd minor (← mkAppM ``SizeOf.sizeOf #[x']) minor ← mkLambdaFVars xs' minor trace[Meta.sizeOf] "minor: {minor}" loop (i+1) (minors.push minor) else k minors /-- Create a "sizeOf" function with name `declName` using the recursor `recName`. -/ partial def mkSizeOfFn (recName : Name) (declName : Name): MetaM Unit := do trace[Meta.sizeOf] "recName: {recName}" let recInfo : RecursorVal ← getConstInfoRec recName forallTelescopeReducing recInfo.type fun xs _ => let levelParams := recInfo.levelParams.tail! -- universe parameters for declaration being defined let params := xs[:recInfo.numParams] let motiveFVars := xs[recInfo.numParams : recInfo.numParams + recInfo.numMotives] let minorFVars := xs[recInfo.getFirstMinorIdx : recInfo.getFirstMinorIdx + recInfo.numMinors] let indices := xs[recInfo.getFirstIndexIdx : recInfo.getFirstIndexIdx + recInfo.numIndices] let major := xs[recInfo.getMajorIdx]! let nat := mkConst ``Nat mkLocalInstances params fun localInsts => mkSizeOfMotives motiveFVars fun motives => do let us := levelOne :: levelParams.map mkLevelParam -- universe level parameters for `rec`-application let recFn := mkConst recName us let val := mkAppN recFn (params ++ motives) forallBoundedTelescope (← inferType val) recInfo.numMinors fun minorFVars' _ => mkSizeOfMinors motiveFVars minorFVars minorFVars' fun minors => do withInstImplicitAsImplict params do let sizeOfParams := params ++ localInsts ++ indices ++ #[major] let sizeOfType ← mkForallFVars sizeOfParams nat let val := mkAppN val (minors ++ indices ++ #[major]) let sizeOfValue ← mkLambdaFVars sizeOfParams val trace[Meta.sizeOf] "declName: {declName}" trace[Meta.sizeOf] "type: {sizeOfType}" trace[Meta.sizeOf] "val: {sizeOfValue}" addDecl <| Declaration.defnDecl { name := declName levelParams := levelParams type := sizeOfType value := sizeOfValue safety := DefinitionSafety.safe hints := ReducibilityHints.abbrev } /-- Create `sizeOf` functions for all inductive datatypes in the mutual inductive declaration containing `typeName` The resulting array contains the generated functions names. The `NameMap` maps recursor names into the generated function names. There is a function for each element of the mutual inductive declaration, and for auxiliary recursors for nested inductive types. -/ def mkSizeOfFns (typeName : Name) : MetaM (Array Name × NameMap Name) := do let indInfo ← getConstInfoInduct typeName let recInfo ← getConstInfoRec (mkRecName typeName) let numExtra := recInfo.numMotives - indInfo.all.length -- numExtra > 0 for nested inductive types let mut result := #[] let baseName := indInfo.all.head! ++ `_sizeOf -- we use the first inductive type as the base name for `sizeOf` functions let mut i := 1 let mut recMap : NameMap Name := {} for indTypeName in indInfo.all do let sizeOfName := baseName.appendIndexAfter i let recName := mkRecName indTypeName mkSizeOfFn recName sizeOfName recMap := recMap.insert recName sizeOfName result := result.push sizeOfName i := i + 1 for j in [:numExtra] do let recName := (mkRecName indInfo.all.head!).appendIndexAfter (j+1) let sizeOfName := baseName.appendIndexAfter i mkSizeOfFn recName sizeOfName recMap := recMap.insert recName sizeOfName result := result.push sizeOfName i := i + 1 return (result, recMap) def mkSizeOfSpecLemmaName (ctorName : Name) : Name := ctorName ++ `sizeOf_spec def mkSizeOfSpecLemmaInstance (ctorApp : Expr) : MetaM Expr := matchConstCtor ctorApp.getAppFn (fun _ => throwError "failed to apply 'sizeOf' spec, constructor expected{indentExpr ctorApp}") fun ctorInfo _ => do let ctorArgs := ctorApp.getAppArgs let ctorParams := ctorArgs[:ctorInfo.numParams] let ctorFields := ctorArgs[ctorInfo.numParams:] let lemmaName := mkSizeOfSpecLemmaName ctorInfo.name let lemmaInfo ← getConstInfo lemmaName let lemmaArity ← forallTelescopeReducing lemmaInfo.type fun xs _ => return xs.size let lemmaArgMask := ctorParams.toArray.map some let lemmaArgMask := lemmaArgMask ++ mkArray (lemmaArity - ctorInfo.numParams - ctorInfo.numFields) (none (α := Expr)) let lemmaArgMask := lemmaArgMask ++ ctorFields.toArray.map some mkAppOptM lemmaName lemmaArgMask /-! # SizeOf spec theorem for nested inductive types -/ namespace SizeOfSpecNested structure Context where indInfo : InductiveVal sizeOfFns : Array Name ctorName : Name params : Array Expr localInsts : Array Expr recMap : NameMap Name -- mapping from recursor name into `_sizeOf_<idx>` function name (see `mkSizeOfFns`) abbrev M := ReaderT Context MetaM def throwUnexpected {α} (msg : MessageData) : M α := do throwError "failed to generate sizeOf theorem for {(← read).ctorName} (use `set_option genSizeOfSpec false` to disable theorem generation), {msg}" def throwFailed {α} : M α := do throwError "failed to generate sizeOf theorem for {(← read).ctorName}, (use `set_option genSizeOfSpec false` to disable theorem generation)" /-- Convert a recursor application into a `_sizeOf_<idx>` application. -/ private def recToSizeOf (e : Expr) : M Expr := do matchConstRec e.getAppFn (fun _ => throwFailed) fun info us => do match (← read).recMap.find? info.name with | none => throwUnexpected m!"expected recursor application {indentExpr e}" | some sizeOfName => let args := e.getAppArgs let indices := args[info.getFirstIndexIdx : info.getFirstIndexIdx + info.numIndices] let major := args[info.getMajorIdx]! return mkAppN (mkConst sizeOfName us.tail!) ((← read).params ++ (← read).localInsts ++ indices ++ #[major]) mutual /-- Construct minor premise proof for `mkSizeOfAuxLemmaProof`. `ys` contains fields and inductive hypotheses for the minor premise. -/ private partial def mkMinorProof (ys : Array Expr) (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.minor] "{lhs} =?= {rhs}" if (← isDefEq lhs rhs) then mkEqRefl rhs else match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with | some (a₁, b₁), some (a₂, b₂) => let p₁ ← mkMinorProof ys a₁ a₂ let p₂ ← mkMinorProofStep ys b₁ b₂ mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂ | _, _ => throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}" /-- Helper method for `mkMinorProof`. The proof step is one of the following - Reflexivity - Assumption (i.e., using an inductive hypotheses from `ys`) - `mkSizeOfAuxLemma` application. This case happens when we have multiple levels of nesting -/ private partial def mkMinorProofStep (ys : Array Expr) (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else let lhs ← recToSizeOf lhs trace[Meta.sizeOf.minor.step] "{lhs} =?= {rhs}" let target ← mkEq lhs rhs for y in ys do if (← isDefEq (← inferType y) target) then return y mkSizeOfAuxLemma lhs rhs /-- Construct proof of auxiliary lemma. See `mkSizeOfAuxLemma` -/ private partial def mkSizeOfAuxLemmaProof (info : InductiveVal) (lhs : Expr) : M Expr := do let lhsArgs := lhs.getAppArgs let sizeOfBaseArgs := lhsArgs[:lhsArgs.size - info.numIndices - 1] let indicesMajor := lhsArgs[lhsArgs.size - info.numIndices - 1:] let sizeOfLevels := lhs.getAppFn.constLevels! let rec /-- Auxiliary function for constructing an `_sizeOf_<idx>` for `ys`, where `ys` are the indices + major. Recall that if `info.name` is part of a mutually inductive declaration, then the resulting application is not necessarily a `lhs.getAppFn` application. The result is an application of one of the `(← read),sizeOfFns` functions. We use this auxiliary function to builtin the motive of the recursor. -/ mkSizeOf (ys : Array Expr) : M Expr := do for sizeOfFn in (← read).sizeOfFns do let candidate := mkAppN (mkAppN (mkConst sizeOfFn sizeOfLevels) sizeOfBaseArgs) ys if (← isTypeCorrect candidate) then return candidate throwFailed let major := lhs.appArg! let majorType ← whnf (← inferType major) let majorTypeArgs := majorType.getAppArgs match majorType.getAppFn.const? with | none => throwFailed | some (_, us) => let recName := mkRecName info.name let recInfo ← getConstInfoRec recName let r := mkConst recName (levelZero :: us) let r := mkAppN r majorTypeArgs[:info.numParams] forallBoundedTelescope (← inferType r) recInfo.numMotives fun motiveFVars _ => do let mut r := r -- Add motives for motiveFVar in motiveFVars do let motive ← forallTelescopeReducing (← inferType motiveFVar) fun ys _ => do let lhs ← mkSizeOf ys let rhs ← mkAppM ``SizeOf.sizeOf #[ys.back] mkLambdaFVars ys (← mkEq lhs rhs) r := mkApp r motive forallBoundedTelescope (← inferType r) recInfo.numMinors fun minorFVars _ => do let mut r := r -- Add minors for minorFVar in minorFVars do let minor ← forallTelescopeReducing (← inferType minorFVar) fun ys target => do let target ← whnf target match target.eq? with | none => throwFailed | some (_, lhs, rhs) => if (← isDefEq lhs rhs) then mkLambdaFVars ys (← mkEqRefl rhs) else let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` -- rhs is of the form `sizeOf (ctor ...)` let ctorApp := rhs.appArg! let specLemma ← mkSizeOfSpecLemmaInstance ctorApp let specEq ← whnf (← inferType specLemma) match specEq.eq? with | none => throwFailed | some (_, _, rhsExpanded) => let lhs_eq_rhsExpanded ← mkMinorProof ys lhs rhsExpanded let rhsExpanded_eq_rhs ← mkEqSymm specLemma mkLambdaFVars ys (← mkEqTrans lhs_eq_rhsExpanded rhsExpanded_eq_rhs) r := mkApp r minor -- Add indices and major return mkAppN r indicesMajor /-- Generate proof for `C._sizeOf_<idx> t = sizeOf t` where `C._sizeOf_<idx>` is a auxiliary function generated for a nested inductive type in `C`. For example, given ```lean inductive Expr where | app (f : String) (args : List Expr) ``` We generate the auxiliary function `Expr._sizeOf_1 : List Expr → Nat`. To generate the `sizeOf` spec lemma ``` sizeOf (Expr.app f args) = 1 + sizeOf f + sizeOf args ``` we need an auxiliary lemma for showing `Expr._sizeOf_1 args = sizeOf args`. Recall that `sizeOf (Expr.app f args)` is definitionally equal to `1 + sizeOf f + Expr._sizeOf_1 args`, but `Expr._sizeOf_1 args` is **not** definitionally equal to `sizeOf args`. We need a proof by induction. -/ private partial def mkSizeOfAuxLemma (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.aux] "{lhs} =?= {rhs}" match lhs.getAppFn.const? with | none => throwFailed | some (fName, us) => let thmLevelParams ← us.mapM fun | Level.param n => return n | _ => throwFailed let thmName := fName.appendAfter "_eq" if (← getEnv).contains thmName then -- Auxiliary lemma has already been defined return mkAppN (mkConst thmName us) lhs.getAppArgs else -- Define auxiliary lemma -- First, generalize indices let x := lhs.appArg! let xType ← whnf (← inferType x) matchConstInduct xType.getAppFn (fun _ => throwFailed) fun info _ => do let params := xType.getAppArgs[:info.numParams] forallTelescopeReducing (← inferType (mkAppN xType.getAppFn params)) fun indices _ => do let majorType := mkAppN (mkAppN xType.getAppFn params) indices withLocalDeclD `x majorType fun major => do let lhsArgs := lhs.getAppArgs let lhsArgsNew := lhsArgs[:lhsArgs.size - 1 - indices.size] ++ indices ++ #[major] let lhsNew := mkAppN lhs.getAppFn lhsArgsNew let rhsNew ← mkAppM ``SizeOf.sizeOf #[major] let eq ← mkEq lhsNew rhsNew let thmParams := lhsArgsNew let thmType ← mkForallFVars thmParams eq let thmValue ← mkSizeOfAuxLemmaProof info lhsNew let thmValue ← mkLambdaFVars thmParams thmValue trace[Meta.sizeOf] "thmValue: {thmValue}" addDecl <| Declaration.thmDecl { name := thmName levelParams := thmLevelParams type := thmType value := thmValue } return mkAppN (mkConst thmName us) lhs.getAppArgs end /- Prove SizeOf spec lemma of the form `sizeOf <ctor-application> = 1 + sizeOf <field_1> + ... + sizeOf <field_n> -/ partial def main (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else /- Expand lhs and rhs to obtain `Nat.add` applications -/ let lhs ← whnfI lhs -- Expand `sizeOf (ctor ...)` into `_sizeOf_<idx>` application let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` application into `HAdd.hAdd` application loop lhs rhs where loop (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.loop] "{lhs} =?= {rhs}" if (← isDefEq lhs rhs) then mkEqRefl rhs else match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with | some (a₁, b₁), some (a₂, b₂) => let p₁ ← loop a₁ a₂ let p₂ ← step b₁ b₂ mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂ | _, _ => throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}" step (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else let lhs ← recToSizeOf lhs mkSizeOfAuxLemma lhs rhs end SizeOfSpecNested private def mkSizeOfSpecTheorem (indInfo : InductiveVal) (sizeOfFns : Array Name) (recMap : NameMap Name) (ctorName : Name) : MetaM Unit := do let ctorInfo ← getConstInfoCtor ctorName let us := ctorInfo.levelParams.map mkLevelParam let simpAttr ← ofExcept <| getAttributeImpl (← getEnv) `simp forallTelescopeReducing ctorInfo.type fun xs _ => do let params := xs[:ctorInfo.numParams] let fields := xs[ctorInfo.numParams:] let ctorApp := mkAppN (mkConst ctorName us) xs mkLocalInstances params fun localInsts => do let lhs ← mkAppM ``SizeOf.sizeOf #[ctorApp] let mut rhs ← mkNumeral (mkConst ``Nat) 1 for field in fields do unless (← ignoreField field) do rhs ← mkAdd rhs (← mkAppM ``SizeOf.sizeOf #[field]) let target ← mkEq lhs rhs trace[Meta.sizeOf] "ctor: {ctorInfo.name}, target: {target}" let thmName := mkSizeOfSpecLemmaName ctorName let thmParams := params ++ localInsts ++ fields let thmType ← mkForallFVars thmParams target let thmValue ← if indInfo.isNested then SizeOfSpecNested.main lhs rhs |>.run { indInfo, sizeOfFns, ctorName, params, localInsts, recMap } else mkEqRefl rhs let thmValue ← mkLambdaFVars thmParams thmValue trace[Meta.sizeOf] "sizeOf spec theorem name: {thmName}" trace[Meta.sizeOf] "sizeOf spec theorem type: {thmType}" trace[Meta.sizeOf] "sizeOf spec theorem value: {thmValue}" unless (← isDefEq (← inferType thmValue) thmType) do throwError "type mismatch" addDecl <| Declaration.thmDecl { name := thmName levelParams := ctorInfo.levelParams type := thmType value := thmValue } simpAttr.add thmName default AttributeKind.global private def mkSizeOfSpecTheorems (indTypeNames : Array Name) (sizeOfFns : Array Name) (recMap : NameMap Name) : MetaM Unit := do for indTypeName in indTypeNames do let indInfo ← getConstInfoInduct indTypeName for ctorName in indInfo.ctors do mkSizeOfSpecTheorem indInfo sizeOfFns recMap ctorName return () register_builtin_option genSizeOf : Bool := { defValue := true descr := "generate `SizeOf` instance for inductive types and structures" } register_builtin_option genSizeOfSpec : Bool := { defValue := true descr := "generate `SizeOf` specificiation theorems for automatically generated instances" } def mkSizeOfInstances (typeName : Name) : MetaM Unit := do if (← getEnv).contains ``SizeOf && genSizeOf.get (← getOptions) && !(← isInductivePredicate typeName) then let indInfo ← getConstInfoInduct typeName unless indInfo.isUnsafe do let (fns, recMap) ← mkSizeOfFns typeName for indTypeName in indInfo.all, fn in fns do let indInfo ← getConstInfoInduct indTypeName forallTelescopeReducing indInfo.type fun xs _ => let params := xs[:indInfo.numParams] withInstImplicitAsImplict params do let indices := xs[indInfo.numParams:] mkLocalInstances params fun localInsts => do let us := indInfo.levelParams.map mkLevelParam let indType := mkAppN (mkConst indTypeName us) xs let sizeOfIndType ← mkAppM ``SizeOf #[indType] withLocalDeclD `m indType fun m => do let v ← mkLambdaFVars #[m] <| mkAppN (mkConst fn us) (params ++ localInsts ++ indices ++ #[m]) let sizeOfMk ← mkAppM ``SizeOf.mk #[v] let instDeclName := indTypeName ++ `_sizeOf_inst let instDeclType ← mkForallFVars (xs ++ localInsts) sizeOfIndType let instDeclValue ← mkLambdaFVars (xs ++ localInsts) sizeOfMk trace[Meta.sizeOf] ">> {instDeclName} : {instDeclType}" addDecl <| Declaration.defnDecl { name := instDeclName levelParams := indInfo.levelParams type := instDeclType value := instDeclValue safety := .safe hints := .abbrev } addInstance instDeclName AttributeKind.global (eval_prio default) if genSizeOfSpec.get (← getOptions) then mkSizeOfSpecTheorems indInfo.all.toArray fns recMap builtin_initialize registerTraceClass `Meta.sizeOf end Lean.Meta
6ad9987b1fbbfa6273c3fe7664382580ecd6f5db
b82c5bb4c3b618c23ba67764bc3e93f4999a1a39
/src/formal_ml/nat.lean
1ba14b07a6446c057f614684ab28e1866195c105
[ "Apache-2.0" ]
permissive
nouretienne/formal-ml
83c4261016955bf9bcb55bd32b4f2621b44163e0
40b6da3b6e875f47412d50c7cd97936cb5091a2b
refs/heads/master
1,671,216,448,724
1,600,472,285,000
1,600,472,285,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,267
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import algebra.ordered_ring import data.nat.basic import formal_ml.core /- In order to understand all of the results about natural numbers, it is helpful to look at the instances for nat that are defined. Specifically, the naturals are: 1. a canonically ordered commutative semiring 2. a decidable linear ordered semiring. -/ lemma lt_antirefl (n:ℕ):(n < n)→ false := begin apply nat.lt_irrefl, end --While this does solve the problem, it is not identical. lemma lt_succ (n:ℕ):(n < nat.succ n) := begin apply nat.le_refl, end lemma lt_succ_of_lt (m n:ℕ):(m < n) → (m < nat.succ n) := begin intros, apply nat.lt_succ_of_le, apply ((@nat.lt_iff_le_not_le m n).mp a).left, end lemma nat_lt_def (n m:ℕ): n < m ↔ (nat.succ n) ≤ m := begin refl, end lemma lt_succ_imp_le (a b:ℕ):a < nat.succ b → a ≤ b := begin intros, rw nat_lt_def at a_1, apply nat.le_of_succ_le_succ, assumption, end lemma nat_fact_pos {n:ℕ}:0 < nat.fact n := begin induction n, { simp, }, { simp, apply n_ih, } end lemma nat_minus_cancel_of_le {k n:ℕ}:k≤ n → (n - k) + k = n := begin rw add_comm, apply nat.add_sub_cancel', end lemma nat_lt_of_add_lt {a b c:ℕ}:a + b < c → b < c := begin induction a, { simp, }, { rw nat.succ_add, intro A1, apply a_ih, apply nat.lt_of_succ_lt, apply A1, } end lemma nat_fact_nonzero {n:ℕ}:nat.fact n ≠ 0 := begin intro A1, have A2:0 < nat.fact n := nat_fact_pos, rw A1 at A2, apply lt_irrefl 0 A2, end lemma nat_lt_sub_of_add_lt {k n n':ℕ}:n + k < n' → n < n' - k := begin revert n', revert n, induction k, { intros n n' A1, rw add_zero at A1, simp, apply A1, }, { intros n n' A1, cases n', { exfalso, apply nat.not_lt_zero, apply A1, }, { rw nat.succ_sub_succ_eq_sub, apply k_ih, rw nat.add_succ at A1, apply nat.lt_of_succ_lt_succ, apply A1, } } end lemma nat_zero_lt_of_nonzero {n:ℕ}:(n≠ 0)→ (0 < n) := begin intro A1, cases n, { exfalso, apply A1, refl, }, { apply nat.zero_lt_succ, } end --In an earlier version, this was solvable via simp. lemma nat_succ_one_add {n:ℕ}:nat.succ n = 1 + n := begin rw add_comm, end lemma nat.le_add {a b:ℕ}:a ≤ a + b := begin simp, end -- mul_left_cancel' is the inspiration here. -- However, it doesn't exist for nat. lemma nat.mul_left_cancel_trichotomy {x : ℕ}:x ≠ 0 → ∀ {y z : ℕ}, (x * y = x * z ↔ y = z) ∧ (x * y < x * z ↔ y < z) := begin induction x, { intros A1 y z, exfalso, apply A1, refl, }, { intros A1 y z, rw nat_succ_one_add, rw right_distrib, rw one_mul, rw right_distrib, rw one_mul, cases x_n, { rw zero_mul, rw zero_mul, rw add_zero, rw add_zero, simp, }, { have B1:nat.succ x_n ≠ 0, { have A2A:0 < nat.succ x_n, { apply nat.zero_lt_succ, }, intro A2B, rw A2B at A2A, apply lt_irrefl 0 A2A, }, have A2 := @x_ih B1 y z, have B2 := @x_ih B1 z y, cases A2 with A3 A4, cases B2 with B3 B4, have A5:y < z ∨ y = z ∨ z < y := lt_trichotomy y z, split;split;intros A6, { cases A5, { exfalso, have A7:y + nat.succ x_n * y < z + nat.succ x_n * z, { apply add_lt_add A5 (A4.mpr A5), }, rw A6 at A7, apply lt_irrefl _ A7, }, cases A5, { exact A5, }, { exfalso, have A7:z + nat.succ x_n * z < y + nat.succ x_n * y, { apply add_lt_add A5 (B4.mpr A5), }, rw A6 at A7, apply lt_irrefl _ A7, }, }, { rw A6, }, { cases A5, { apply A5, }, cases A5, { rw A5 at A6, exfalso, apply lt_irrefl _ A6, }, { exfalso, apply not_lt_of_lt A6, apply add_lt_add A5 (B4.mpr A5), }, }, { apply add_lt_add A6, apply A4.mpr A6, } } } end lemma nat.mul_left_cancel' {x : ℕ}:x ≠ 0 → ∀ {y z : ℕ}, (x * y = x * z → y = z) := begin intros A1 y z A2, apply (@nat.mul_left_cancel_trichotomy x A1 y z).left.mp A2, end lemma nat.mul_eq_zero_iff_eq_zero_or_eq_zero {a b:ℕ}: a * b = 0 ↔ (a = 0 ∨ b = 0) := begin split;intros A1, { have A2:(a=0) ∨ (a ≠ (0:ℕ)) := eq_or_ne, cases A2, { left, apply A2, }, { have A1:a * b = a * 0, { rw mul_zero, apply A1, }, right, apply nat.mul_left_cancel' A2 A1, } }, { cases A1;rw A1, { rw zero_mul, }, { rw mul_zero, } } end lemma double_ne {m n:ℕ}:2 * m ≠ 1 + 2 * n := begin intro A1, have A2:=lt_trichotomy m n, cases A2, { have A3:0 + 2 * m < 1 + 2 * n, { apply add_lt_add, apply zero_lt_one, apply mul_lt_mul_of_pos_left A2 zero_lt_two, }, rw zero_add at A3, rw A1 at A3, apply lt_irrefl _ A3, }, cases A2, { rw A2 at A1, have A3:0 + 2 * n < 1 + 2 * n, { rw add_comm 0 _, rw add_comm 1 _, apply add_lt_add_left, apply zero_lt_one, }, rw zero_add at A3, rw ← A1 at A3, apply lt_irrefl _ A3, }, { have A4:nat.succ n ≤ m, { apply nat.succ_le_of_lt A2, }, have A5 := lt_or_eq_of_le A4, cases A5, { have A6:2 * nat.succ n < 2 * m, { apply mul_lt_mul_of_pos_left A5 zero_lt_two, }, rw nat_succ_one_add at A6, rw left_distrib at A6, rw mul_one at A6, have A7:1 + 2 * n < 2 * m, { apply lt_trans _ A6, apply add_lt_add_right, apply one_lt_two, }, rw A1 at A7, apply lt_irrefl _ A7, }, { subst m, rw nat_succ_one_add at A1, rw left_distrib at A1, rw mul_one at A1, simp at A1, have A2:1 < 2 := one_lt_two, rw A1 at A2, apply lt_irrefl _ A2, } }, end lemma nat.one_le_of_zero_lt {x:ℕ}:0 < x → 1 ≤ x := begin intro A1, apply nat.succ_le_of_lt, apply A1, end lemma nat.succ_le_iff_lt {m n:ℕ}:m.succ ≤ n ↔ m < n := begin apply nat.succ_le_iff, end lemma nat.zero_of_lt_one {x:ℕ}:x < 1 → x = 0 := begin intro A1, have B1 := nat.le_pred_of_lt A1, simp at B1, apply B1, end
1c2529fbe8b258888bfea7afb4421a2fd0b62f5c
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/HasCoe.lean
be54e9a8b7a35176620fd106cb4f47a49021a201
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
6,547
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ /- The Elaborator tries to insert coercions automatically. Only instances of HasCoe type class are considered in the process. Lean also provides a "lifting" operator: ↑a. It uses all instances of HasLift type class. Every HasCoe instance is also a HasLift instance. We recommend users only use HasCoe for coercions that do not produce a lot of ambiguity. All coercions and lifts can be identified with the constant coe. We use the HasCoeToFun type class for encoding coercions from a Type to a Function space. We use the HasCoeToSort type class for encoding coercions from a Type to a sort. -/ prelude import Init.Data.List.Basic universes u v class HasLift (a : Sort u) (b : Sort v) := (lift : a → b) /-- Auxiliary class that contains the transitive closure of HasLift. -/ class HasLiftT (a : Sort u) (b : Sort v) := (lift : a → b) class HasCoe (a : Sort u) (b : Sort v) := (coe : a → b) /-- Auxiliary class that contains the transitive closure of HasCoe. -/ class HasCoeT (a : Sort u) (b : Sort v) := (coe : a → b) class HasCoeToFun (a : Sort u) : Sort (max u (v+1)) := (F : a → Sort v) (coe : ∀ x, F x) class HasCoeToSort (a : Sort u) : Type (max u (v+1)) := (S : Sort v) (coe : a → S) @[inline] def lift {a : Sort u} {b : Sort v} [HasLift a b] : a → b := @HasLift.lift a b _ @[inline] def liftT {a : Sort u} {b : Sort v} [HasLiftT a b] : a → b := @HasLiftT.lift a b _ @[inline] def oldCoeB {a : Sort u} {b : Sort v} [HasCoe a b] : a → b := @HasCoe.coe a b _ @[inline] def oldCoeT {a : Sort u} {b : Sort v} [HasCoeT a b] : a → b := @HasCoeT.coe a b _ @[inline] def oldCoeFnB {a : Sort u} [HasCoeToFun.{u, v} a] : ∀ (x : a), HasCoeToFun.F.{u, v} x := HasCoeToFun.coe /- User Level coercion operators -/ @[reducible, inline] def oldCoe {a : Sort u} {b : Sort v} [HasLiftT a b] : a → b := liftT @[reducible, inline] def oldCoeFn {a : Sort u} [HasCoeToFun.{u, v} a] : ∀ (x : a), HasCoeToFun.F.{u, v} x := HasCoeToFun.coe @[reducible, inline] def oldCoeSort {a : Sort u} [HasCoeToSort.{u, v} a] : a → HasCoeToSort.S.{u, v} a := HasCoeToSort.coe /- Notation -/ universes u₁ u₂ u₃ /- Transitive closure for HasLift, HasCoe, HasCoeToFun -/ namespace Legacy instance liftTrans {a : Sort u₁} {b : Sort u₂} {c : Sort u₃} [HasLiftT b c] [HasLift a b] : HasLiftT a c := ⟨fun x => liftT (lift x : b)⟩ instance liftRefl {a : Sort u} : HasLiftT a a := ⟨id⟩ instance coeoeTrans {a : Sort u₁} {b : Sort u₂} {c : Sort u₃} [HasCoeT b c] [HasCoe a b] : HasCoeT a c := ⟨fun x => oldCoeT (oldCoeB x : b)⟩ instance coeBase {a : Sort u} {b : Sort v} [HasCoe a b] : HasCoeT a b := ⟨oldCoeB⟩ /- We add this instance directly into HasCoeT to avoid non-termination. Suppose coeOption had Type (HasCoe a (Option a)). Then, we can loop when searching a coercion from α to β (HasCoeT α β) 1- coeTrans at (HasCoeT α β) (HasCoe α ?b₁) and (HasCoeT ?b₁ c) 2- coeOption at (HasCoe α ?b₁) ?b₁ := Option α 3- coeTrans at (HasCoeT (Option α) β) (HasCoe (Option α) ?b₂) and (HasCoeT ?b₂ β) 4- coeOption at (HasCoe (Option α) ?b₂) ?b₂ := Option (Option α)) ... -/ instance oldCoeOption {a : Type u} : HasCoeT a (Option a) := ⟨fun x => some x⟩ /- Auxiliary transitive closure for HasCoe which does not contain instances such as coeOption. They would produce non-termination when combined with coeFnTrans and coeSortTrans. -/ class HasCoeTAux (a : Sort u) (b : Sort v) := (coe : a → b) instance coeTransAux {a : Sort u₁} {b : Sort u₂} {c : Sort u₃} [HasCoeTAux b c] [HasCoe a b] : HasCoeTAux a c := ⟨fun x => @HasCoeTAux.coe b c _ (oldCoeB x)⟩ instance coeBaseAux {a : Sort u} {b : Sort v} [HasCoe a b] : HasCoeTAux a b := ⟨oldCoeB⟩ instance coeFnTrans {a : Sort u₁} {b : Sort u₂} [HasCoeToFun.{u₂, u₃} b] [HasCoeTAux a b] : HasCoeToFun.{u₁, u₃} a := { F := fun x => @HasCoeToFun.F.{u₂, u₃} b _ (@HasCoeTAux.coe a b _ x), coe := fun x => oldCoeFn (@HasCoeTAux.coe a b _ x) } instance coeSortTrans {a : Sort u₁} {b : Sort u₂} [HasCoeToSort.{u₂, u₃} b] [HasCoeTAux a b] : HasCoeToSort.{u₁, u₃} a := { S := HasCoeToSort.S.{u₂, u₃} b, coe := fun x => oldCoeSort (@HasCoeTAux.coe a b _ x) } /- Every coercion is also a lift -/ instance coeToLift {a : Sort u} {b : Sort v} [HasCoeT a b] : HasLiftT a b := ⟨oldCoeT⟩ /- basic coercions -/ @[inline] instance coeBoolToProp : HasCoe Bool Prop := ⟨fun y => y = true⟩ @[inline] instance coeDecidableEq (x : Bool) : Decidable (oldCoe x) := inferInstanceAs (Decidable (x = true)) instance coeSubtype {a : Sort u} {p : a → Prop} : HasCoe {x // p x} a := ⟨Subtype.val⟩ /- basic lifts -/ universes ua ua₁ ua₂ ub ub₁ ub₂ /- Remark: we can't use [HasLiftT a₂ a₁] since it will produce non-termination whenever a type class resolution problem does not have a solution. -/ instance liftFn {a₁ : Sort ua₁} {a₂ : Sort ua₂} {b₁ : Sort ub₁} {b₂ : Sort ub₂} [HasLiftT b₁ b₂] [HasLift a₂ a₁] : HasLift (a₁ → b₁) (a₂ → b₂) := ⟨fun f x => oldCoe (f (oldCoe x))⟩ instance liftFnRange {a : Sort ua} {b₁ : Sort ub₁} {b₂ : Sort ub₂} [HasLiftT b₁ b₂] : HasLift (a → b₁) (a → b₂) := ⟨fun f x => oldCoe (f x)⟩ instance liftFnDom {a₁ : Sort ua₁} {a₂ : Sort ua₂} {b : Sort ub} [HasLift a₂ a₁] : HasLift (a₁ → b) (a₂ → b) := ⟨fun f x => f (oldCoe x)⟩ instance liftPair {a₁ : Type ua₁} {a₂ : Type ub₂} {b₁ : Type ub₁} {b₂ : Type ub₂} [HasLiftT a₁ a₂] [HasLiftT b₁ b₂] : HasLift (a₁ × b₁) (a₂ × b₂) := ⟨fun p => Prod.casesOn p (fun x y => (oldCoe x, oldCoe y))⟩ instance liftPair₁ {a₁ : Type ua₁} {a₂ : Type ua₂} {b : Type ub} [HasLiftT a₁ a₂] : HasLift (a₁ × b) (a₂ × b) := ⟨fun p => Prod.casesOn p (fun x y => (oldCoe x, y))⟩ instance liftPair₂ {a : Type ua} {b₁ : Type ub₁} {b₂ : Type ub₂} [HasLiftT b₁ b₂] : HasLift (a × b₁) (a × b₂) := ⟨fun p => Prod.casesOn p (fun x y => (x, oldCoe y))⟩ instance liftList {a : Type u} {b : Type v} [HasLiftT a b] : HasLift (List a) (List b) := ⟨fun l => List.map (@oldCoe a b _) l⟩ end Legacy instance oldCoeToLift {a : Sort u} {b : Sort v} [HasCoeT a b] : HasLiftT a b := ⟨oldCoeT⟩
97693b09645dde5a9287c895ca80978335cb9100
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/algebra/order/monoid.lean
7a716cf98b023dbfd5fe70fe351eb76c4d7db64d
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,182
lean
/- 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 algebra.group.with_one import algebra.group.type_tags import algebra.group.prod import algebra.order.monoid_lemmas import order.bounded_lattice import order.min_max import order.rel_iso /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ set_option old_structure_cmd true open function universe u variable {α : Type u} /-- An ordered commutative monoid is a commutative monoid with a partial order such that `a ≤ b → c * a ≤ c * b` (multiplication is monotone) -/ @[protect_proj, ancestor comm_monoid partial_order] class ordered_comm_monoid (α : Type*) extends comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that `a ≤ b → c + a ≤ c + b` (addition is monotone) -/ @[protect_proj, ancestor add_comm_monoid partial_order] class ordered_add_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) attribute [to_additive] ordered_comm_monoid section ordered_instances @[to_additive] instance ordered_comm_monoid.to_covariant_class_left (M : Type*) [ordered_comm_monoid M] : covariant_class M M (*) (≤) := { elim := λ a b c bc, ordered_comm_monoid.mul_le_mul_left _ _ bc a } /- This instance can be proven with `by apply_instance`. However, `with_bot ℕ` does not pick up a `covariant_class M M (function.swap (*)) (≤)` instance without it (see PR #7940). -/ @[to_additive] instance ordered_comm_monoid.to_covariant_class_right (M : Type*) [ordered_comm_monoid M] : covariant_class M M (swap (*)) (≤) := covariant_swap_mul_le_of_covariant_mul_le M end ordered_instances /-- An `ordered_comm_monoid` with one-sided 'division' in the sense that if `a ≤ b`, there is some `c` for which `a * c = b`. This is a weaker version of the condition on canonical orderings defined by `canonically_ordered_monoid`. -/ class has_exists_mul_of_le (α : Type u) [ordered_comm_monoid α] : Prop := (exists_mul_of_le : ∀ {a b : α}, a ≤ b → ∃ (c : α), b = a * c) /-- An `ordered_add_comm_monoid` with one-sided 'subtraction' in the sense that if `a ≤ b`, then there is some `c` for which `a + c = b`. This is a weaker version of the condition on canonical orderings defined by `canonically_ordered_add_monoid`. -/ class has_exists_add_of_le (α : Type u) [ordered_add_comm_monoid α] : Prop := (exists_add_of_le : ∀ {a b : α}, a ≤ b → ∃ (c : α), b = a + c) attribute [to_additive] has_exists_mul_of_le export has_exists_mul_of_le (exists_mul_of_le) export has_exists_add_of_le (exists_add_of_le) /-- A linearly ordered additive commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_add_comm_monoid] class linear_ordered_add_comm_monoid (α : Type*) extends linear_order α, ordered_add_comm_monoid α. /-- A linearly ordered commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_comm_monoid, to_additive] class linear_ordered_comm_monoid (α : Type*) extends linear_order α, ordered_comm_monoid α. /-- A linearly ordered commutative monoid with a zero element. -/ class linear_ordered_comm_monoid_with_zero (α : Type*) extends linear_ordered_comm_monoid α, comm_monoid_with_zero α := (zero_le_one : (0 : α) ≤ 1) /-- A linearly ordered commutative monoid with an additively absorbing `⊤` element. Instances should include number systems with an infinite element adjoined.` -/ @[protect_proj, ancestor linear_ordered_add_comm_monoid order_top] class linear_ordered_add_comm_monoid_with_top (α : Type*) extends linear_ordered_add_comm_monoid α, order_top α := (top_add' : ∀ x : α, ⊤ + x = ⊤) section linear_ordered_add_comm_monoid_with_top variables [linear_ordered_add_comm_monoid_with_top α] {a b : α} @[simp] lemma top_add (a : α) : ⊤ + a = ⊤ := linear_ordered_add_comm_monoid_with_top.top_add' a @[simp] lemma add_top (a : α) : a + ⊤ = ⊤ := trans (add_comm _ _) (top_add _) -- TODO: Generalize to a not-yet-existing typeclass extending `linear_order` and `order_top` @[simp] lemma min_top_left (a : α) : min (⊤ : α) a = a := min_eq_right le_top @[simp] lemma min_top_right (a : α) : min a ⊤ = a := min_eq_left le_top end linear_ordered_add_comm_monoid_with_top /-- Pullback an `ordered_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.ordered_comm_monoid [ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_comm_monoid β := { mul_le_mul_left := λ a b ab c, show f (c * a) ≤ f (c * b), by { rw [mul, mul], apply mul_le_mul_left', exact ab }, ..partial_order.lift f hf, ..hf.comm_monoid f one mul } /-- Pullback a `linear_ordered_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_comm_monoid [linear_ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_comm_monoid β := { .. hf.ordered_comm_monoid f one mul, .. linear_order.lift f hf } lemma bit0_pos [ordered_add_comm_monoid α] {a : α} (h : 0 < a) : 0 < bit0 a := add_pos h h namespace units @[to_additive] instance [monoid α] [preorder α] : preorder (units α) := preorder.lift (coe : units α → α) @[simp, norm_cast, to_additive] theorem coe_le_coe [monoid α] [preorder α] {a b : units α} : (a : α) ≤ b ↔ a ≤ b := iff.rfl @[simp, norm_cast, to_additive] theorem coe_lt_coe [monoid α] [preorder α] {a b : units α} : (a : α) < b ↔ a < b := iff.rfl @[to_additive] instance [monoid α] [partial_order α] : partial_order (units α) := partial_order.lift coe units.ext @[to_additive] instance [monoid α] [linear_order α] : linear_order (units α) := linear_order.lift coe units.ext @[simp, norm_cast, to_additive] theorem max_coe [monoid α] [linear_order α] {a b : units α} : (↑(max a b) : α) = max a b := by by_cases b ≤ a; simp [max_def, h] @[simp, norm_cast, to_additive] theorem min_coe [monoid α] [linear_order α] {a b : units α} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [min_def, h] end units namespace with_zero local attribute [semireducible] with_zero instance [preorder α] : preorder (with_zero α) := with_bot.preorder instance [partial_order α] : partial_order (with_zero α) := with_bot.partial_order instance [partial_order α] : order_bot (with_zero α) := with_bot.order_bot lemma zero_le [partial_order α] (a : with_zero α) : 0 ≤ a := order_bot.bot_le a lemma zero_lt_coe [preorder α] (a : α) : (0 : with_zero α) < a := with_bot.bot_lt_coe a @[simp, norm_cast] lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_zero α) < b ↔ a < b := with_bot.coe_lt_coe @[simp, norm_cast] lemma coe_le_coe [partial_order α] {a b : α} : (a : with_zero α) ≤ b ↔ a ≤ b := with_bot.coe_le_coe instance [lattice α] : lattice (with_zero α) := with_bot.lattice instance [linear_order α] : linear_order (with_zero α) := with_bot.linear_order lemma mul_le_mul_left {α : Type u} [has_mul α] [preorder α] [covariant_class α α (*) (≤)] : ∀ (a b : with_zero α), a ≤ b → ∀ (c : with_zero α), c * a ≤ c * b := begin rintro (_ | a) (_ | b) h (_ | c); try { exact λ f hf, option.no_confusion hf }, { exact false.elim (not_lt_of_le h (with_zero.zero_lt_coe a))}, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, exact covariant_class.elim _ h } end lemma lt_of_mul_lt_mul_left {α : Type u} [has_mul α] [partial_order α] [contravariant_class α α (*) (<)] : ∀ (a b c : with_zero α), a * b < a * c → b < c := begin rintro (_ | a) (_ | b) (_ | c) h; try { exact false.elim (lt_irrefl none h) }, { exact with_zero.zero_lt_coe c }, { exact false.elim (not_le_of_lt h (with_zero.zero_le _)) }, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, apply lt_of_mul_lt_mul_left' h } end instance [ordered_comm_monoid α] : ordered_comm_monoid (with_zero α) := { mul_le_mul_left := with_zero.mul_le_mul_left, ..with_zero.comm_monoid_with_zero, ..with_zero.partial_order } /- Note 1 : the below is not an instance because it requires `zero_le`. It seems like a rather pathological definition because α already has a zero. Note 2 : there is no multiplicative analogue because it does not seem necessary. Mathematicians might be more likely to use the order-dual version, where all elements are ≤ 1 and then 1 is the top element. -/ /-- If `0` is the least element in `α`, then `with_zero α` is an `ordered_add_comm_monoid`. -/ def ordered_add_comm_monoid [ordered_add_comm_monoid α] (zero_le : ∀ a : α, 0 ≤ a) : ordered_add_comm_monoid (with_zero α) := begin suffices, refine { add_le_add_left := this, ..with_zero.partial_order, ..with_zero.add_comm_monoid, .. }, { intros a b h c ca h₂, cases b with b, { rw le_antisymm h bot_le at h₂, exact ⟨_, h₂, le_refl _⟩ }, cases a with a, { change c + 0 = some ca at h₂, simp at h₂, simp [h₂], exact ⟨_, rfl, by simpa using add_le_add_left (zero_le b) _⟩ }, { simp at h, cases c with c; change some _ = _ at h₂; simp [-add_comm] at h₂; subst ca; refine ⟨_, rfl, _⟩, { exact h }, { exact add_le_add_left h _ } } } end end with_zero namespace with_top section has_one variables [has_one α] @[to_additive] instance : has_one (with_top α) := ⟨(1 : α)⟩ @[simp, norm_cast, to_additive] lemma coe_one : ((1 : α) : with_top α) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_eq_one {a : α} : (a : with_top α) = 1 ↔ a = 1 := coe_eq_coe @[simp, norm_cast, to_additive] theorem one_eq_coe {a : α} : 1 = (a : with_top α) ↔ a = 1 := trans eq_comm coe_eq_one @[simp, to_additive] theorem top_ne_one : ⊤ ≠ (1 : with_top α) . @[simp, to_additive] theorem one_ne_top : (1 : with_top α) ≠ ⊤ . end has_one instance [has_add α] : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a + b))⟩ @[norm_cast] lemma coe_add [has_add α] {a b : α} : ((a + b : α) : with_top α) = a + b := rfl @[norm_cast] lemma coe_bit0 [has_add α] {a : α} : ((bit0 a : α) : with_top α) = bit0 a := rfl @[norm_cast] lemma coe_bit1 [has_add α] [has_one α] {a : α} : ((bit1 a : α) : with_top α) = bit1 a := rfl @[simp] lemma add_top [has_add α] : ∀{a : with_top α}, a + ⊤ = ⊤ | none := rfl | (some a) := rfl @[simp] lemma top_add [has_add α] {a : with_top α} : ⊤ + a = ⊤ := rfl lemma add_eq_top [has_add α] {a b : with_top α} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by cases a; cases b; simp [none_eq_top, some_eq_coe, ←with_top.coe_add, ←with_zero.coe_add] lemma add_lt_top [has_add α] [partial_order α] {a b : with_top α} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := by simp [lt_top_iff_ne_top, add_eq_top, not_or_distrib] lemma add_eq_coe [has_add α] : ∀ {a b : with_top α} {c : α}, a + b = c ↔ ∃ (a' b' : α), ↑a' = a ∧ ↑b' = b ∧ a' + b' = c | none b c := by simp [none_eq_top] | (some a) none c := by simp [none_eq_top] | (some a) (some b) c := by simp only [some_eq_coe, ← coe_add, coe_eq_coe, exists_and_distrib_left, exists_eq_left] @[simp] lemma add_coe_eq_top_iff [has_add α] {x : with_top α} {y : α} : x + y = ⊤ ↔ x = ⊤ := by { induction x using with_top.rec_top_coe; simp [← coe_add, -with_zero.coe_add] } @[simp] lemma coe_add_eq_top_iff [has_add α] {x : α} {y : with_top α} : ↑x + y = ⊤ ↔ y = ⊤ := by { induction y using with_top.rec_top_coe; simp [← coe_add, -with_zero.coe_add] } instance [add_semigroup α] : add_semigroup (with_top α) := { add_assoc := begin repeat { refine with_top.rec_top_coe _ _; try { intro }}; simp [←with_top.coe_add, add_assoc] end, ..with_top.has_add } instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) := { add_comm := begin repeat { refine with_top.rec_top_coe _ _; try { intro }}; simp [←with_top.coe_add, add_comm] end, ..with_top.add_semigroup } instance [add_monoid α] : add_monoid (with_top α) := { zero_add := begin refine with_top.rec_top_coe _ _, { simpa }, { intro, rw [←with_top.coe_zero, ←with_top.coe_add, zero_add] } end, add_zero := begin refine with_top.rec_top_coe _ _, { simpa }, { intro, rw [←with_top.coe_zero, ←with_top.coe_add, add_zero] } end, ..with_top.has_zero, ..with_top.add_semigroup } instance [add_comm_monoid α] : add_comm_monoid (with_top α) := { ..with_top.add_monoid, ..with_top.add_comm_semigroup } instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_top α) := { add_le_add_left := begin rintros a b h (_|c), { simp [none_eq_top] }, rcases b with (_|b), { simp [none_eq_top] }, rcases le_coe_iff.1 h with ⟨a, rfl, h⟩, simp only [some_eq_coe, ← coe_add, coe_le_coe] at h ⊢, exact add_le_add_left h c end, ..with_top.partial_order, ..with_top.add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid_with_top (with_top α) := { top_add' := λ x, with_top.top_add, ..with_top.order_top, ..with_top.linear_order, ..with_top.ordered_add_comm_monoid, ..option.nontrivial } /-- Coercion from `α` to `with_top α` as an `add_monoid_hom`. -/ def coe_add_hom [add_monoid α] : α →+ with_top α := ⟨coe, rfl, λ _ _, rfl⟩ @[simp] lemma coe_coe_add_hom [add_monoid α] : ⇑(coe_add_hom : α →+ with_top α) = coe := rfl @[simp] lemma zero_lt_top [ordered_add_comm_monoid α] : (0 : with_top α) < ⊤ := coe_lt_top 0 @[simp, norm_cast] lemma zero_lt_coe [ordered_add_comm_monoid α] (a : α) : (0 : with_top α) < a ↔ 0 < a := coe_lt_coe end with_top namespace with_bot instance [has_zero α] : has_zero (with_bot α) := with_top.has_zero instance [has_one α] : has_one (with_bot α) := with_top.has_one instance [add_semigroup α] : add_semigroup (with_bot α) := with_top.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (with_bot α) := with_top.add_comm_semigroup instance [add_monoid α] : add_monoid (with_bot α) := with_top.add_monoid instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_bot α) := begin suffices, refine { add_le_add_left := this, ..with_bot.partial_order, ..with_bot.add_comm_monoid, ..}, { intros a b h c ca h₂, cases c with c, {cases h₂}, cases a with a; cases h₂, cases b with b, {cases le_antisymm h bot_le}, simp at h, exact ⟨_, rfl, add_le_add_left h _⟩, } end instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid (with_bot α) := { ..with_bot.linear_order, ..with_bot.ordered_add_comm_monoid } -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_zero [has_zero α] : ((0 : α) : with_bot α) = 0 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_one [has_one α] : ((1 : α) : with_bot α) = 1 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_eq_zero {α : Type*} [add_monoid α] {a : α} : (a : with_bot α) = 0 ↔ a = 0 := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_add [add_semigroup α] (a b : α) : ((a + b : α) : with_bot α) = a + b := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit0 [add_semigroup α] {a : α} : ((bit0 a : α) : with_bot α) = bit0 a := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit1 [add_semigroup α] [has_one α] {a : α} : ((bit1 a : α) : with_bot α) = bit1 a := by norm_cast @[simp] lemma bot_add [add_semigroup α] (a : with_bot α) : ⊥ + a = ⊥ := rfl @[simp] lemma add_bot [add_semigroup α] (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl @[simp] lemma add_eq_bot [add_semigroup α] {m n : with_bot α} : m + n = ⊥ ↔ m = ⊥ ∨ n = ⊥ := with_top.add_eq_top end with_bot /-- A canonically ordered additive monoid is an ordered commutative additive monoid in which the ordering coincides with the subtractibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other nontrivial `ordered_add_comm_group`s. -/ @[protect_proj, ancestor ordered_add_comm_monoid order_bot] class canonically_ordered_add_monoid (α : Type*) extends ordered_add_comm_monoid α, order_bot α := (le_iff_exists_add : ∀ a b : α, a ≤ b ↔ ∃ c, b = a + c) /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a * c`. Examples seem rare; it seems more likely that the `order_dual` of a naturally-occurring lattice satisfies this than the lattice itself (for example, dual of the lattice of ideals of a PID or Dedekind domain satisfy this; collections of all things ≤ 1 seem to be more natural that collections of all things ≥ 1). -/ @[protect_proj, ancestor ordered_comm_monoid order_bot, to_additive] class canonically_ordered_monoid (α : Type*) extends ordered_comm_monoid α, order_bot α := (le_iff_exists_mul : ∀ a b : α, a ≤ b ↔ ∃ c, b = a * c) section canonically_ordered_monoid variables [canonically_ordered_monoid α] {a b c d : α} @[to_additive] lemma le_iff_exists_mul : a ≤ b ↔ ∃c, b = a * c := canonically_ordered_monoid.le_iff_exists_mul a b @[to_additive] lemma self_le_mul_right (a b : α) : a ≤ a * b := le_iff_exists_mul.mpr ⟨b, rfl⟩ @[to_additive] lemma self_le_mul_left (a b : α) : a ≤ b * a := by { rw [mul_comm], exact self_le_mul_right a b } @[simp, to_additive zero_le] lemma one_le (a : α) : 1 ≤ a := le_iff_exists_mul.mpr ⟨a, (one_mul _).symm⟩ @[simp, to_additive] lemma bot_eq_one : (⊥ : α) = 1 := le_antisymm bot_le (one_le ⊥) @[simp, to_additive] lemma mul_eq_one_iff : a * b = 1 ↔ a = 1 ∧ b = 1 := mul_eq_one_iff' (one_le _) (one_le _) @[simp, to_additive] lemma le_one_iff_eq_one : a ≤ 1 ↔ a = 1 := iff.intro (assume h, le_antisymm h (one_le a)) (assume h, h ▸ le_refl a) @[to_additive] lemma one_lt_iff_ne_one : 1 < a ↔ a ≠ 1 := iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (one_le _) hne.symm @[to_additive] lemma exists_pos_mul_of_lt (h : a < b) : ∃ c > 1, a * c = b := begin obtain ⟨c, hc⟩ := le_iff_exists_mul.1 h.le, refine ⟨c, one_lt_iff_ne_one.2 _, hc.symm⟩, rintro rfl, simpa [hc, lt_irrefl] using h end @[to_additive] lemma le_mul_left (h : a ≤ c) : a ≤ b * c := calc a = 1 * a : by simp ... ≤ b * c : mul_le_mul' (one_le _) h @[to_additive] lemma le_mul_self : a ≤ b * a := le_mul_left (le_refl a) @[to_additive] lemma le_mul_right (h : a ≤ b) : a ≤ b * c := calc a = a * 1 : by simp ... ≤ b * c : mul_le_mul' h (one_le _) @[to_additive] lemma le_self_mul : a ≤ a * c := le_mul_right (le_refl a) @[to_additive] lemma lt_iff_exists_mul [covariant_class α α (*) (<)] : a < b ↔ ∃ c > 1, b = a * c := begin simp_rw [lt_iff_le_and_ne, and_comm, le_iff_exists_mul, ← exists_and_distrib_left, exists_prop], apply exists_congr, intro c, rw [and.congr_left_iff, gt_iff_lt], rintro rfl, split, { rw [one_lt_iff_ne_one], apply mt, rintro rfl, rw [mul_one] }, { rw [← (self_le_mul_right a c).lt_iff_ne], apply lt_mul_of_one_lt_right' } end -- This instance looks absurd: a monoid already has a zero /-- Adding a new zero to a canonically ordered additive monoid produces another one. -/ instance with_zero.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_zero α) := { le_iff_exists_add := λ a b, begin apply with_zero.cases_on a, { exact iff_of_true bot_le ⟨b, (zero_add b).symm⟩ }, apply with_zero.cases_on b, { intro b', refine iff_of_false (mt (le_antisymm bot_le) (by simp)) (not_exists.mpr (λ c, _)), apply with_zero.cases_on c; simp [←with_zero.coe_add] }, { simp only [le_iff_exists_add, with_zero.coe_le_coe], intros, split; rintro ⟨c, h⟩, { exact ⟨c, congr_arg coe h⟩ }, { induction c using with_zero.cases_on, { refine ⟨0, _⟩, simpa using h }, { refine ⟨c, _⟩, simpa [←with_zero.coe_add] using h } } } end, .. with_zero.order_bot, .. with_zero.ordered_add_comm_monoid zero_le } instance with_top.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_top α) := { le_iff_exists_add := assume a b, match a, b with | a, none := show a ≤ ⊤ ↔ ∃c, ⊤ = a + c, by simp; refine ⟨⊤, _⟩; cases a; refl | (some a), (some b) := show (a:with_top α) ≤ ↑b ↔ ∃c:with_top α, ↑b = ↑a + c, begin simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm], split, { rintro ⟨c, rfl⟩, refine ⟨c, _⟩, norm_cast }, { exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end } end | none, some b := show (⊤ : with_top α) ≤ b ↔ ∃c:with_top α, ↑b = ⊤ + c, by simp end, .. with_top.order_bot, .. with_top.ordered_add_comm_monoid } @[priority 100, to_additive] instance canonically_ordered_monoid.has_exists_mul_of_le (α : Type u) [canonically_ordered_monoid α] : has_exists_mul_of_le α := { exists_mul_of_le := λ a b hab, le_iff_exists_mul.mp hab } end canonically_ordered_monoid lemma pos_of_gt {M : Type*} [canonically_ordered_add_monoid M] {n m : M} (h : n < m) : 0 < m := lt_of_le_of_lt (zero_le _) h /-- A canonically linear-ordered additive monoid is a canonically ordered additive monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_add_monoid linear_order] class canonically_linear_ordered_add_monoid (α : Type*) extends canonically_ordered_add_monoid α, linear_order α /-- A canonically linear-ordered monoid is a canonically ordered monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_monoid linear_order, to_additive] class canonically_linear_ordered_monoid (α : Type*) extends canonically_ordered_monoid α, linear_order α section canonically_linear_ordered_monoid variables [canonically_linear_ordered_monoid α] @[priority 100, to_additive] -- see Note [lower instance priority] instance canonically_linear_ordered_monoid.semilattice_sup_bot : semilattice_sup_bot α := { ..lattice_of_linear_order, ..canonically_ordered_monoid.to_order_bot α } instance with_top.canonically_linear_ordered_add_monoid (α : Type*) [canonically_linear_ordered_add_monoid α] : canonically_linear_ordered_add_monoid (with_top α) := { .. (infer_instance : canonically_ordered_add_monoid (with_top α)), .. (infer_instance : linear_order (with_top α)) } @[to_additive] lemma min_mul_distrib (a b c : α) : min a (b * c) = min a (min a b * min a c) := begin cases le_total a b with hb hb, { simp [hb, le_mul_right] }, { cases le_total a c with hc hc, { simp [hc, le_mul_left] }, { simp [hb, hc] } } end @[to_additive] lemma min_mul_distrib' (a b c : α) : min (a * b) c = min (min a c * min b c) c := by simpa [min_comm _ c] using min_mul_distrib c a b @[simp, to_additive] lemma one_min (a : α) : min 1 a = 1 := min_eq_left (one_le a) @[simp, to_additive] lemma min_one (a : α) : min a 1 = 1 := min_eq_right (one_le a) end canonically_linear_ordered_monoid /-- An ordered cancellative additive commutative monoid is an additive commutative monoid with a partial order, in which addition is cancellative and monotone. -/ @[protect_proj, ancestor add_cancel_comm_monoid partial_order] class ordered_cancel_add_comm_monoid (α : Type u) extends add_cancel_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c) /-- An ordered cancellative commutative monoid is a commutative monoid with a partial order, in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor cancel_comm_monoid partial_order, to_additive] class ordered_cancel_comm_monoid (α : Type u) extends cancel_comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (le_of_mul_le_mul_left : ∀ a b c : α, a * b ≤ a * c → b ≤ c) section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid α] {a b c d : α} @[to_additive] lemma ordered_cancel_comm_monoid.lt_of_mul_lt_mul_left : ∀ a b c : α, a * b < a * c → b < c := λ a b c h, lt_of_le_not_le (ordered_cancel_comm_monoid.le_of_mul_le_mul_left a b c h.le) $ mt (λ h, ordered_cancel_comm_monoid.mul_le_mul_left _ _ h _) (not_le_of_gt h) @[to_additive] instance ordered_cancel_comm_monoid.to_contravariant_class_left (M : Type*) [ordered_cancel_comm_monoid M] : contravariant_class M M (*) (<) := { elim := λ a b c, ordered_cancel_comm_monoid.lt_of_mul_lt_mul_left _ _ _ } /- This instance can be proven with `by apply_instance`. However, by analogy with the instance `ordered_cancel_comm_monoid.to_covariant_class_right` above, I imagine that without this instance, some Type would not have a `contravariant_class M M (function.swap (*)) (<)` instance. -/ @[to_additive] instance ordered_cancel_comm_monoid.to_contravariant_class_right (M : Type*) [ordered_cancel_comm_monoid M] : contravariant_class M M (swap (*)) (<) := contravariant_swap_mul_lt_of_contravariant_mul_lt M @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid α := { ..‹ordered_cancel_comm_monoid α› } /-- Pullback an `ordered_cancel_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_cancel_add_comm_monoid "Pullback an `ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_cancel_comm_monoid β := { le_of_mul_le_mul_left := λ a b c (bc : f (a * b) ≤ f (a * c)), (mul_le_mul_iff_left (f a)).mp (by rwa [← mul, ← mul]), ..hf.left_cancel_semigroup f mul, ..hf.ordered_comm_monoid f one mul } end ordered_cancel_comm_monoid /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ @[to_additive] lemma fn_min_mul_fn_max {β} [linear_order α] [comm_semigroup β] (f : α → β) (n m : α) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } @[to_additive] lemma min_mul_max [linear_order α] [comm_semigroup α] (n m : α) : min n m * max n m = n * m := fn_min_mul_fn_max id n m /-- A linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_add_comm_monoid linear_ordered_add_comm_monoid] class linear_ordered_cancel_add_comm_monoid (α : Type u) extends ordered_cancel_add_comm_monoid α, linear_ordered_add_comm_monoid α /-- A linearly ordered cancellative commutative monoid is a commutative monoid with a linear order in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_comm_monoid linear_ordered_comm_monoid, to_additive] class linear_ordered_cancel_comm_monoid (α : Type u) extends ordered_cancel_comm_monoid α, linear_ordered_comm_monoid α section covariant_class_mul_le variables [linear_order α] section has_mul variable [has_mul α] section left variable [covariant_class α α (*) (≤)] @[to_additive] lemma 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] lemma 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 [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma 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] lemma 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 end has_mul variable [monoid α] @[to_additive] lemma min_le_mul_of_one_le_right [covariant_class α α (*) (≤)] {a b : α} (hb : 1 ≤ b) : min a b ≤ a * b := min_le_iff.2 $ or.inl $ le_mul_of_one_le_right' hb @[to_additive] lemma min_le_mul_of_one_le_left [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) : min a b ≤ a * b := min_le_iff.2 $ or.inr $ le_mul_of_one_le_left' ha @[to_additive] lemma max_le_mul_of_one_le [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : max a b ≤ a * b := max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩ end covariant_class_mul_le section linear_ordered_cancel_comm_monoid variables [linear_ordered_cancel_comm_monoid α] /-- Pullback a `linear_ordered_cancel_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_cancel_add_comm_monoid "Pullback a `linear_ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_cancel_comm_monoid β := { ..hf.linear_ordered_comm_monoid f one mul, ..hf.ordered_cancel_comm_monoid f one mul } end linear_ordered_cancel_comm_monoid namespace order_dual @[to_additive] instance [h : has_mul α] : has_mul (order_dual α) := h @[to_additive] instance [h : has_one α] : has_one (order_dual α) := h @[to_additive] instance [h : monoid α] : monoid (order_dual α) := h @[to_additive] instance [h : comm_monoid α] : comm_monoid (order_dual α) := h @[to_additive] instance [h : cancel_comm_monoid α] : cancel_comm_monoid (order_dual α) := h @[to_additive] instance contravariant_class_mul_le [has_le α] [has_mul α] [c : contravariant_class α α (*) (≤)] : contravariant_class (order_dual α) (order_dual α) (*) (≤) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_mul_le [has_le α] [has_mul α] [c : covariant_class α α (*) (≤)] : covariant_class (order_dual α) (order_dual α) (*) (≤) := ⟨c.1.flip⟩ @[to_additive] instance contravariant_class_swap_mul_le [has_le α] [has_mul α] [c : contravariant_class α α (swap (*)) (≤)] : contravariant_class (order_dual α) (order_dual α) (swap (*)) (≤) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_swap_mul_le [has_le α] [has_mul α] [c : covariant_class α α (swap (*)) (≤)] : covariant_class (order_dual α) (order_dual α) (swap (*)) (≤) := ⟨c.1.flip⟩ @[to_additive] instance contravariant_class_mul_lt [has_lt α] [has_mul α] [c : contravariant_class α α (*) (<)] : contravariant_class (order_dual α) (order_dual α) (*) (<) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_mul_lt [has_lt α] [has_mul α] [c : covariant_class α α (*) (<)] : covariant_class (order_dual α) (order_dual α) (*) (<) := ⟨c.1.flip⟩ @[to_additive] instance contravariant_class_swap_mul_lt [has_lt α] [has_mul α] [c : contravariant_class α α (swap (*)) (<)] : contravariant_class (order_dual α) (order_dual α) (swap (*)) (<) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_swap_mul_lt [has_lt α] [has_mul α] [c : covariant_class α α (swap (*)) (<)] : covariant_class (order_dual α) (order_dual α) (swap (*)) (<) := ⟨c.1.flip⟩ @[to_additive] instance [ordered_comm_monoid α] : ordered_comm_monoid (order_dual α) := { mul_le_mul_left := λ a b h c, mul_le_mul_left' h c, .. order_dual.partial_order α, .. order_dual.comm_monoid } @[to_additive ordered_cancel_add_comm_monoid.to_contravariant_class] instance ordered_cancel_comm_monoid.to_contravariant_class [ordered_cancel_comm_monoid α] : contravariant_class (order_dual α) (order_dual α) has_mul.mul has_le.le := { elim := λ a b c bc, (ordered_cancel_comm_monoid.le_of_mul_le_mul_left a c b (dual_le.mp bc)) } @[to_additive] instance [ordered_cancel_comm_monoid α] : ordered_cancel_comm_monoid (order_dual α) := { le_of_mul_le_mul_left := λ a b c : α, le_of_mul_le_mul_left', .. order_dual.ordered_comm_monoid, .. order_dual.cancel_comm_monoid } @[to_additive] instance [linear_ordered_cancel_comm_monoid α] : linear_ordered_cancel_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_cancel_comm_monoid } @[to_additive] instance [linear_ordered_comm_monoid α] : linear_ordered_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_comm_monoid } end order_dual section ordered_cancel_add_comm_monoid variable [ordered_cancel_add_comm_monoid α] namespace with_top lemma add_lt_add_iff_left {a b c : with_top α} (ha : a ≠ ⊤) : a + b < a + c ↔ b < c := begin lift a to α using ha, cases b; cases c, { simp [none_eq_top] }, { simp [some_eq_coe, none_eq_top, coe_lt_top] }, { simp [some_eq_coe, none_eq_top, ← coe_add, coe_lt_top] }, { simp [some_eq_coe, ← coe_add, coe_lt_coe] } end lemma add_lt_add_iff_right {a b c : with_top α} (ha : a ≠ ⊤) : (c + a < b + a ↔ c < b) := by simp only [← add_comm a, add_lt_add_iff_left ha] instance contravariant_class_add_lt : contravariant_class (with_top α) (with_top α) (+) (<) := begin refine ⟨λ a b c h, _⟩, cases a, { rw [none_eq_top, top_add, top_add] at h, exact (lt_irrefl ⊤ h).elim }, { exact (add_lt_add_iff_left coe_ne_top).1 h } end end with_top namespace with_bot lemma add_lt_add_iff_left {a b c : with_bot α} (ha : a ≠ ⊥) : a + b < a + c ↔ b < c := @with_top.add_lt_add_iff_left (order_dual α) _ a c b ha lemma add_lt_add_iff_right {a b c : with_bot α} (ha : a ≠ ⊥) : b + a < c + a ↔ b < c := @with_top.add_lt_add_iff_right (order_dual α) _ _ _ _ ha instance contravariant_class_add_lt : contravariant_class (with_bot α) (with_bot α) (+) (<) := @order_dual.contravariant_class_add_lt (with_top $ order_dual α) _ _ _ end with_bot end ordered_cancel_add_comm_monoid namespace prod variables {M N : Type*} @[to_additive] instance [ordered_cancel_comm_monoid M] [ordered_cancel_comm_monoid N] : ordered_cancel_comm_monoid (M × N) := { mul_le_mul_left := λ a b h c, ⟨mul_le_mul_left' h.1 _, mul_le_mul_left' h.2 _⟩, le_of_mul_le_mul_left := λ a b c h, ⟨le_of_mul_le_mul_left' h.1, le_of_mul_le_mul_left' h.2⟩, .. prod.cancel_comm_monoid, .. prod.partial_order M N } end prod section type_tags instance : Π [preorder α], preorder (multiplicative α) := id instance : Π [preorder α], preorder (additive α) := id instance : Π [partial_order α], partial_order (multiplicative α) := id instance : Π [partial_order α], partial_order (additive α) := id instance : Π [linear_order α], linear_order (multiplicative α) := id instance : Π [linear_order α], linear_order (additive α) := id instance [ordered_add_comm_monoid α] : ordered_comm_monoid (multiplicative α) := { mul_le_mul_left := @ordered_add_comm_monoid.add_le_add_left α _, ..multiplicative.partial_order, ..multiplicative.comm_monoid } instance [ordered_comm_monoid α] : ordered_add_comm_monoid (additive α) := { add_le_add_left := @ordered_comm_monoid.mul_le_mul_left α _, ..additive.partial_order, ..additive.add_comm_monoid } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_comm_monoid (multiplicative α) := { le_of_mul_le_mul_left := @ordered_cancel_add_comm_monoid.le_of_add_le_add_left α _, ..multiplicative.left_cancel_semigroup, ..multiplicative.ordered_comm_monoid } instance [ordered_cancel_comm_monoid α] : ordered_cancel_add_comm_monoid (additive α) := { le_of_add_le_add_left := @ordered_cancel_comm_monoid.le_of_mul_le_mul_left α _, ..additive.add_left_cancel_semigroup, ..additive.ordered_add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_comm_monoid (multiplicative α) := { ..multiplicative.linear_order, ..multiplicative.ordered_comm_monoid } instance [linear_ordered_comm_monoid α] : linear_ordered_add_comm_monoid (additive α) := { ..additive.linear_order, ..additive.ordered_add_comm_monoid } end type_tags /-- The order embedding sending `b` to `a * b`, for some fixed `a`. See also `order_iso.mul_left` when working in an ordered group. -/ @[to_additive "The order embedding sending `b` to `a + b`, for some fixed `a`. See also `order_iso.add_left` when working in an additive ordered group.", simps] def order_embedding.mul_left {α : Type*} [has_mul α] [linear_order α] [covariant_class α α (*) (<)] (m : α) : α ↪o α := order_embedding.of_strict_mono (λ n, m * n) (λ a b w, mul_lt_mul_left' w m) /-- The order embedding sending `b` to `b * a`, for some fixed `a`. See also `order_iso.mul_right` when working in an ordered group. -/ @[to_additive "The order embedding sending `b` to `b + a`, for some fixed `a`. See also `order_iso.add_right` when working in an additive ordered group.", simps] def order_embedding.mul_right {α : Type*} [has_mul α] [linear_order α] [covariant_class α α (swap (*)) (<)] (m : α) : α ↪o α := order_embedding.of_strict_mono (λ n, n * m) (λ a b w, mul_lt_mul_right' w m)
387f5c4a879249bcb2e40c2cdb6ca23d24f148fb
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/struct_inst_exprs.lean
e95e1bb6ea467a2beef6b3c920ef7565da43dabb
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
475
lean
open nat prod set_option pp.coercions true definition s : nat × nat := {| prod, pr1 := 10, pr2 := 20 |} structure test := (A : Type) (a : A) (B : A → Type) (b : B a) definition s2 := ⦃ test, a := 3, b := 10 ⦄ eval s2 definition s3 := {| test, a := 20, s2 |} eval s3 definition s4 := ⦃ test, A := nat, B := λ a, nat, a := 10, b := 10 ⦄ definition s5 : Σ a : nat, a > 0 := ⦃ sigma, pr1 := 10, pr2 := of_is_true trivial ⦄ eval s5 check ⦃ unit ⦄
6d08e624cb9e9a30fb953db2d547fbca4647e511
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/geometry/euclidean/sphere.lean
673a6916643a4e2bbbae0faf58c9ec9c33405d45
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
8,411
lean
/- Copyright (c) 2021 Manuel Candales. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Manuel Candales, Benjamin Davidson -/ import geometry.euclidean.triangle /-! # Spheres This file proves basic geometrical results about distances and angles in spheres in real inner product spaces and Euclidean affine spaces. ## Main theorems * `mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_pi`: Intersecting Chords Theorem (Freek No. 55). * `mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_zero`: Intersecting Secants Theorem. * `mul_dist_add_mul_dist_eq_mul_dist_of_cospherical`: Ptolemy’s Theorem (Freek No. 95). TODO: The current statement of Ptolemy’s theorem works around the lack of a "cyclic polygon" concept in mathlib, which is what the theorem statement would naturally use (or two such concepts, since both a strict version, where all vertices must be distinct, and a weak version, where consecutive vertices may be equal, would be useful; Ptolemy's theorem should then use the weak one). An API needs to be built around that concept, which would include: - strict cyclic implies weak cyclic, - weak cyclic and consecutive points distinct implies strict cyclic, - weak/strict cyclic implies weak/strict cyclic for any subsequence, - any three points on a sphere are weakly or strictly cyclic according to whether they are distinct, - any number of points on a sphere intersected with a two-dimensional affine subspace are cyclic in some order, - a list of points is cyclic if and only if its reversal is, - a list of points is cyclic if and only if any cyclic permutation is, while other permutations are not when the points are distinct, - a point P where the diagonals of a cyclic polygon cross exists (and is unique) with weak/strict betweenness depending on weak/strict cyclicity, - four points on a sphere with such a point P are cyclic in the appropriate order, and so on. -/ open real open_locale euclidean_geometry real_inner_product_space real variables {V : Type*} [inner_product_space ℝ V] namespace inner_product_geometry /-! ### Geometrical results on spheres in real inner product spaces This section develops some results on spheres in real inner product spaces, which are used to deduce corresponding results for Euclidean affine spaces. -/ lemma mul_norm_eq_abs_sub_sq_norm {x y z : V} (h₁ : ∃ k : ℝ, k ≠ 1 ∧ x + y = k • (x - y)) (h₂ : ∥z - y∥ = ∥z + y∥) : ∥x - y∥ * ∥x + y∥ = abs (∥z + y∥ ^ 2 - ∥z - x∥ ^ 2) := begin obtain ⟨k, hk_ne_one, hk⟩ := h₁, let r := (k - 1)⁻¹ * (k + 1), have hxy : x = r • y, { rw [← smul_smul, eq_inv_smul_iff' (sub_ne_zero.mpr hk_ne_one), ← sub_eq_zero], calc (k - 1) • x - (k + 1) • y = (k • x - x) - (k • y + y) : by simp_rw [sub_smul, add_smul, one_smul] ... = (k • x - k • y) - (x + y) : by simp_rw [← sub_sub, sub_right_comm] ... = k • (x - y) - (x + y) : by rw ← smul_sub k x y ... = 0 : sub_eq_zero.mpr hk.symm }, have hzy : ⟪z, y⟫ = 0, { rw [← eq_of_sq_eq_sq (norm_nonneg (z - y)) (norm_nonneg (z + y)), norm_add_sq_real, norm_sub_sq_real] at h₂, linarith }, have hzx : ⟪z, x⟫ = 0 := by rw [hxy, inner_smul_right, hzy, mul_zero], calc ∥x - y∥ * ∥x + y∥ = ∥(r - 1) • y∥ * ∥(r + 1) • y∥ : by simp [sub_smul, add_smul, hxy] ... = ∥r - 1∥ * ∥y∥ * (∥r + 1∥ * ∥y∥) : by simp_rw [norm_smul] ... = ∥r - 1∥ * ∥r + 1∥ * ∥y∥ ^ 2 : by ring ... = abs ((r - 1) * (r + 1) * ∥y∥ ^ 2) : by simp [abs_mul, norm_eq_abs] ... = abs (r ^ 2 * ∥y∥ ^ 2 - ∥y∥ ^ 2) : by ring_nf ... = abs (∥x∥ ^ 2 - ∥y∥ ^ 2) : by simp [hxy, norm_smul, mul_pow, norm_eq_abs, sq_abs] ... = abs (∥z + y∥ ^ 2 - ∥z - x∥ ^ 2) : by simp [norm_add_sq_real, norm_sub_sq_real, hzy, hzx, abs_sub_comm], end end inner_product_geometry namespace euclidean_geometry /-! ### Geometrical results on spheres in Euclidean affine spaces This section develops some results on spheres in Euclidean affine spaces. -/ open inner_product_geometry variables {P : Type*} [metric_space P] [normed_add_torsor V P] include V /-- If `P` is a point on the line `AB` and `Q` is equidistant from `A` and `B`, then `AP * BP = abs (BQ ^ 2 - PQ ^ 2)`. -/ lemma mul_dist_eq_abs_sub_sq_dist {a b p q : P} (hp : ∃ k : ℝ, k ≠ 1 ∧ b -ᵥ p = k • (a -ᵥ p)) (hq : dist a q = dist b q) : dist a p * dist b p = abs (dist b q ^ 2 - dist p q ^ 2) := begin let m : P := midpoint ℝ a b, obtain ⟨v, h1, h2, h3⟩ := ⟨vsub_sub_vsub_cancel_left, v a p m, v p q m, v a q m⟩, have h : ∀ r, b -ᵥ r = (m -ᵥ r) + (m -ᵥ a) := λ r, by rw [midpoint_vsub_left, ← right_vsub_midpoint, add_comm, vsub_add_vsub_cancel], iterate 4 { rw dist_eq_norm_vsub V }, rw [← h1, ← h2, h, h], rw [← h1, h] at hp, rw [dist_eq_norm_vsub V a q, dist_eq_norm_vsub V b q, ← h3, h] at hq, exact mul_norm_eq_abs_sub_sq_norm hp hq, end /-- If `A`, `B`, `C`, `D` are cospherical and `P` is on both lines `AB` and `CD`, then `AP * BP = CP * DP`. -/ lemma mul_dist_eq_mul_dist_of_cospherical {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hapb : ∃ k₁ : ℝ, k₁ ≠ 1 ∧ b -ᵥ p = k₁ • (a -ᵥ p)) (hcpd : ∃ k₂ : ℝ, k₂ ≠ 1 ∧ d -ᵥ p = k₂ • (c -ᵥ p)) : dist a p * dist b p = dist c p * dist d p := begin obtain ⟨q, r, h'⟩ := (cospherical_def {a, b, c, d}).mp h, obtain ⟨ha, hb, hc, hd⟩ := ⟨h' a _, h' b _, h' c _, h' d _⟩, { rw ← hd at hc, rw ← hb at ha, rw [mul_dist_eq_abs_sub_sq_dist hapb ha, hb, mul_dist_eq_abs_sub_sq_dist hcpd hc, hd] }, all_goals { simp }, end /-- Intersecting Chords Theorem. -/ theorem mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_pi {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hapb : ∠ a p b = π) (hcpd : ∠ c p d = π) : dist a p * dist b p = dist c p * dist d p := begin obtain ⟨-, k₁, _, hab⟩ := angle_eq_pi_iff.mp hapb, obtain ⟨-, k₂, _, hcd⟩ := angle_eq_pi_iff.mp hcpd, exact mul_dist_eq_mul_dist_of_cospherical h ⟨k₁, (by linarith), hab⟩ ⟨k₂, (by linarith), hcd⟩, end /-- Intersecting Secants Theorem. -/ theorem mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_zero {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hab : a ≠ b) (hcd : c ≠ d) (hapb : ∠ a p b = 0) (hcpd : ∠ c p d = 0) : dist a p * dist b p = dist c p * dist d p := begin obtain ⟨-, k₁, -, hab₁⟩ := angle_eq_zero_iff.mp hapb, obtain ⟨-, k₂, -, hcd₁⟩ := angle_eq_zero_iff.mp hcpd, refine mul_dist_eq_mul_dist_of_cospherical h ⟨k₁, _, hab₁⟩ ⟨k₂, _, hcd₁⟩; by_contra hnot; simp only [not_not, *, one_smul] at *, exacts [hab (vsub_left_cancel hab₁).symm, hcd (vsub_left_cancel hcd₁).symm], end /-- Ptolemy’s Theorem. -/ theorem mul_dist_add_mul_dist_eq_mul_dist_of_cospherical {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hapc : ∠ a p c = π) (hbpd : ∠ b p d = π) : dist a b * dist c d + dist b c * dist d a = dist a c * dist b d := begin have h' : cospherical ({a, c, b, d} : set P), { rwa set.insert_comm c b {d} }, have hmul := mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_pi h' hapc hbpd, have hbp := left_dist_ne_zero_of_angle_eq_pi hbpd, have h₁ : dist c d = dist c p / dist b p * dist a b, { rw [dist_mul_of_eq_angle_of_dist_mul b p a c p d, dist_comm a b], { rw [angle_eq_angle_of_angle_eq_pi_of_angle_eq_pi hbpd hapc, angle_comm] }, all_goals { field_simp [mul_comm, hmul] } }, have h₂ : dist d a = dist a p / dist b p * dist b c, { rw [dist_mul_of_eq_angle_of_dist_mul c p b d p a, dist_comm c b], { rwa [angle_comm, angle_eq_angle_of_angle_eq_pi_of_angle_eq_pi], rwa angle_comm }, all_goals { field_simp [mul_comm, hmul] } }, have h₃ : dist d p = dist a p * dist c p / dist b p, { field_simp [mul_comm, hmul] }, have h₄ : ∀ x y : ℝ, x * (y * x) = x * x * y := λ x y, by rw [mul_left_comm, mul_comm], field_simp [h₁, h₂, dist_eq_add_dist_of_angle_eq_pi hbpd, h₃, hbp, dist_comm a b, h₄, ← sq, dist_sq_mul_dist_add_dist_sq_mul_dist b, hapc], end end euclidean_geometry
031008ff9119b672ea0a2161e48b63bdccad9200
f57749ca63d6416f807b770f67559503fdb21001
/hott/types/eq2.hlean
a33a05ac3933f4791c7002c07f13f7a36496a474
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,468
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about 2-dimensional paths -/ import .cubical.square open function namespace eq variables {A B C : Type} {f : A → B} {a a' a₁ a₂ a₃ a₄ : A} {b b' : B} theorem ap_weakly_constant_eq (p : Πx, f x = b) (q : a = a') : ap_weakly_constant p q = eq_con_inv_of_con_eq ((eq_of_square (square_of_pathover (apdo p q)))⁻¹ ⬝ whisker_left (p a) (ap_constant q b)) := begin induction q, esimp, generalize (p a), intro p, cases p, apply idpath idp end definition ap_inv2 {p q : a = a'} (r : p = q) : square (ap (ap f) (inverse2 r)) (inverse2 (ap (ap f) r)) (ap_inv f p) (ap_inv f q) := by induction r;exact hrfl definition ap_con2 {p₁ q₁ : a₁ = a₂} {p₂ q₂ : a₂ = a₃} (r₁ : p₁ = q₁) (r₂ : p₂ = q₂) : square (ap (ap f) (r₁ ◾ r₂)) (ap (ap f) r₁ ◾ ap (ap f) r₂) (ap_con f p₁ p₂) (ap_con f q₁ q₂) := by induction r₂;induction r₁;exact hrfl theorem ap_con_right_inv_sq {A B : Type} {a1 a2 : A} (f : A → B) (p : a1 = a2) : square (ap (ap f) (con.right_inv p)) (con.right_inv (ap f p)) (ap_con f p p⁻¹ ⬝ whisker_left _ (ap_inv f p)) idp := by cases p;apply hrefl theorem ap_con_left_inv_sq {A B : Type} {a1 a2 : A} (f : A → B) (p : a1 = a2) : square (ap (ap f) (con.left_inv p)) (con.left_inv (ap f p)) (ap_con f p⁻¹ p ⬝ whisker_right (ap_inv f p) _) idp := by cases p;apply vrefl theorem ap_ap_weakly_constant {A B C : Type} (g : B → C) {f : A → B} {b : B} (p : Πx, f x = b) {x y : A} (q : x = y) : square (ap (ap g) (ap_weakly_constant p q)) (ap_weakly_constant (λa, ap g (p a)) q) (ap_compose g f q)⁻¹ (!ap_con ⬝ whisker_left _ !ap_inv) := begin induction q, esimp, generalize (p x), intro p, cases p, apply ids -- induction q, rewrite [↑ap_compose,ap_inv], apply hinverse, apply ap_con_right_inv_sq, end theorem ap_ap_compose {A B C D : Type} (h : C → D) (g : B → C) (f : A → B) {x y : A} (p : x = y) : square (ap_compose (h ∘ g) f p) (ap (ap h) (ap_compose g f p)) (ap_compose h (g ∘ f) p) (ap_compose h g (ap f p)) := by induction p;exact ids theorem ap_compose_inv {A B C : Type} (g : B → C) (f : A → B) {x y : A} (p : x = y) : square (ap_compose g f p⁻¹) (inverse2 (ap_compose g f p) ⬝ (ap_inv g (ap f p))⁻¹) (ap_inv (g ∘ f) p) (ap (ap g) (ap_inv f p)) := by induction p;exact ids theorem ap_compose_con (g : B → C) (f : A → B) (p : a₁ = a₂) (q : a₂ = a₃) : square (ap_compose g f (p ⬝ q)) (ap_compose g f p ◾ ap_compose g f q ⬝ (ap_con g (ap f p) (ap f q))⁻¹) (ap_con (g ∘ f) p q) (ap (ap g) (ap_con f p q)) := by induction q;induction p;exact ids theorem ap_compose_natural {A B C : Type} (g : B → C) (f : A → B) {x y : A} {p q : x = y} (r : p = q) : square (ap (ap (g ∘ f)) r) (ap (ap g ∘ ap f) r) (ap_compose g f p) (ap_compose g f q) := natural_square (ap_compose g f) r theorem ap_eq_of_con_inv_eq_idp (f : A → B) {p q : a₁ = a₂} (r : p ⬝ q⁻¹ = idp) : ap02 f (eq_of_con_inv_eq_idp r) = eq_of_con_inv_eq_idp (whisker_left _ !ap_inv⁻¹ ⬝ !ap_con⁻¹ ⬝ ap02 f r) := by induction q;esimp at *;cases r;reflexivity -- definition naturality_apdo {A : Type} {B : A → Type} {a a₂ : A} {f g : Πa, B a} -- (H : f ~ g) (p : a = a₂) -- : squareover B vrfl (apdo f p) (apdo g p) -- (pathover_idp_of_eq (H a)) (pathover_idp_of_eq (H a₂)) := -- by induction p;esimp;exact hrflo definition naturality_apdo_eq {A : Type} {B : A → Type} {a a₂ : A} {f g : Πa, B a} (H : f ~ g) (p : a = a₂) : apdo f p = concato_eq (eq_concato (H a) (apdo g p)) (H a₂)⁻¹ := begin induction p, esimp, generalizes [H a, g a], intro ga Ha, induction Ha, reflexivity end theorem con_tr_idp {P : A → Type} {x y : A} (q : x = y) (u : P x) : con_tr idp q u = ap (λp, p ▸ u) (idp_con q) := by induction q;reflexivity end eq
f0a9578d05cb683bb10a1df0048ac00fec5457cd
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/geometry/manifold/times_cont_mdiff_map.lean
ff67cf256cd4e449f1b95539b919ca919b0cb150
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,835
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import geometry.manifold.times_cont_mdiff import topology.continuous_function.basic /-! # Smooth bundled map In this file we define the type `times_cont_mdiff_map` of `n` times continuously differentiable bundled maps. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') (M : Type*) [topological_space M] [charted_space H M] (M' : Type*) [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] (n : with_top ℕ) /-- Bundled `n` times continuously differentiable maps. -/ @[protect_proj] structure times_cont_mdiff_map := (to_fun : M → M') (times_cont_mdiff_to_fun : times_cont_mdiff I I' n to_fun) /-- Bundled smooth maps. -/ @[reducible] def smooth_map := times_cont_mdiff_map I I' M M' ⊤ localized "notation `C^` n `⟮` I `, ` M `; ` I' `, ` M' `⟯` := times_cont_mdiff_map I I' M M' n" in manifold localized "notation `C^` n `⟮` I `, ` M `; ` k `⟯` := times_cont_mdiff_map I (model_with_corners_self k k) M k n" in manifold open_locale manifold namespace times_cont_mdiff_map variables {I} {I'} {M} {M'} {n} instance : has_coe_to_fun C^n⟮I, M; I', M'⟯ := ⟨_, times_cont_mdiff_map.to_fun⟩ instance : has_coe C^n⟮I, M; I', M'⟯ C(M, M') := ⟨λ f, ⟨f.to_fun, f.times_cont_mdiff_to_fun.continuous⟩⟩ attribute [to_additive_ignore_args 21] times_cont_mdiff_map times_cont_mdiff_map.has_coe_to_fun times_cont_mdiff_map.continuous_map.has_coe variables {f g : C^n⟮I, M; I', M'⟯} protected lemma times_cont_mdiff (f : C^n⟮I, M; I', M'⟯) : times_cont_mdiff I I' n f := f.times_cont_mdiff_to_fun protected lemma smooth (f : C^∞⟮I, M; I', M'⟯) : smooth I I' f := f.times_cont_mdiff_to_fun protected lemma mdifferentiable' (f : C^n⟮I, M; I', M'⟯) (hn : 1 ≤ n) : mdifferentiable I I' f := f.times_cont_mdiff.mdifferentiable hn protected lemma mdifferentiable (f : C^∞⟮I, M; I', M'⟯) : mdifferentiable I I' f := f.times_cont_mdiff.mdifferentiable le_top protected lemma mdifferentiable_at (f : C^∞⟮I, M; I', M'⟯) {x} : mdifferentiable_at I I' f x := f.mdifferentiable x lemma coe_inj ⦃f g : C^n⟮I, M; I', M'⟯⦄ (h : (f : M → M') = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext (h : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext h /-- The identity as a smooth map. -/ def id : C^n⟮I, M; I, M⟯ := ⟨id, times_cont_mdiff_id⟩ /-- The composition of smooth maps, as a smooth map. -/ def comp (f : C^n⟮I', M'; I'', M''⟯) (g : C^n⟮I, M; I', M'⟯) : C^n⟮I, M; I'', M''⟯ := { to_fun := λ a, f (g a), times_cont_mdiff_to_fun := f.times_cont_mdiff_to_fun.comp g.times_cont_mdiff_to_fun, } @[simp] lemma comp_apply (f : C^n⟮I', M'; I'', M''⟯) (g : C^n⟮I, M; I', M'⟯) (x : M) : f.comp g x = f (g x) := rfl instance [inhabited M'] : inhabited C^n⟮I, M; I', M'⟯ := ⟨⟨λ _, default _, times_cont_mdiff_const⟩⟩ /-- Constant map as a smooth map -/ def const (y : M') : C^n⟮I, M; I', M'⟯ := ⟨λ x, y, times_cont_mdiff_const⟩ end times_cont_mdiff_map instance continuous_linear_map.has_coe_to_times_cont_mdiff_map : has_coe (E →L[𝕜] E') C^n⟮𝓘(𝕜, E), E; 𝓘(𝕜, E'), E'⟯ := ⟨λ f, ⟨f.to_fun, f.times_cont_mdiff⟩⟩
c02b92a335ff47cf75691e9a5bb91f1bbbd0af0e
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/equiv/nat.lean
164bc3a9ff9a15ce76a1131c54b6d300373a28dd
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
2,055
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.nat.pairing import data.pnat.basic /-! # Equivalences involving `ℕ` This file defines some additional constructive equivalences using `encodable` and the pairing function on `ℕ`. -/ open nat namespace equiv /-- An equivalence between `ℕ × ℕ` and `ℕ`, using the `mkpair` and `unpair` functions in `data.nat.pairing`. -/ @[simp] def nat_prod_nat_equiv_nat : ℕ × ℕ ≃ ℕ := ⟨λ p, nat.mkpair p.1 p.2, nat.unpair, λ p, begin cases p, apply nat.unpair_mkpair end, nat.mkpair_unpair⟩ /-- An equivalence between `bool × ℕ` and `ℕ`, by mapping `(tt, x)` to `2 * x + 1` and `(ff, x)` to `2 * x`. -/ @[simp] def bool_prod_nat_equiv_nat : bool × ℕ ≃ ℕ := ⟨λ ⟨b, n⟩, bit b n, bodd_div2, λ ⟨b, n⟩, by simp [bool_prod_nat_equiv_nat._match_1, bodd_bit, div2_bit], λ n, by simp [bool_prod_nat_equiv_nat._match_1, bit_decomp]⟩ /-- An equivalence between `ℕ ⊕ ℕ` and `ℕ`, by mapping `(sum.inl x)` to `2 * x` and `(sum.inr x)` to `2 * x + 1`. -/ @[simp] def nat_sum_nat_equiv_nat : ℕ ⊕ ℕ ≃ ℕ := (bool_prod_equiv_sum ℕ).symm.trans bool_prod_nat_equiv_nat /-- An equivalence between `ℤ` and `ℕ`, through `ℤ ≃ ℕ ⊕ ℕ` and `ℕ ⊕ ℕ ≃ ℕ`. -/ def int_equiv_nat : ℤ ≃ ℕ := int_equiv_nat_sum_nat.trans nat_sum_nat_equiv_nat /-- An equivalence between `α × α` and `α`, given that there is an equivalence between `α` and `ℕ`. -/ def prod_equiv_of_equiv_nat {α : Sort*} (e : α ≃ ℕ) : α × α ≃ α := calc α × α ≃ ℕ × ℕ : prod_congr e e ... ≃ ℕ : nat_prod_nat_equiv_nat ... ≃ α : e.symm /-- An equivalence between `ℕ+` and `ℕ`, by mapping `x` in `ℕ+` to `x - 1` in `ℕ`. -/ def pnat_equiv_nat : ℕ+ ≃ ℕ := ⟨λ n, pred n.1, succ_pnat, λ ⟨n, h⟩, by { cases n, cases h, simp [succ_pnat, h] }, λ n, by simp [succ_pnat] ⟩ end equiv
5160cb57c675905c1b14a81aa10481fa5d6ba03c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Data/OpenDecl.lean
f6c0d0b262cfd95786ef30b50f1363c67f69d9f5
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
824
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Data.Name namespace Lean /-- Data for representing `open` commands -/ inductive OpenDecl where | simple (ns : Name) (except : List Name) | explicit (id : Name) (declName : Name) deriving BEq namespace OpenDecl instance : Inhabited OpenDecl := ⟨simple Name.anonymous []⟩ instance : ToString OpenDecl := ⟨fun decl => match decl with | explicit id decl => toString id ++ " → " ++ toString decl | simple ns ex => toString ns ++ (if ex == [] then "" else " hiding " ++ toString ex)⟩ end OpenDecl def rootNamespace := `_root_ def removeRoot (n : Name) : Name := n.replacePrefix rootNamespace Name.anonymous end Lean
3c63bb9c08d7255c42e49dd48ec7beed57626a21
bdb33f8b7ea65f7705fc342a178508e2722eb851
/logic/embedding.lean
0b2499dc74fa98dc6bffb1744c177c697e0dffe3
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
5,018
lean
/- 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 Injective functions. -/ import data.equiv universes u v w x namespace function structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ end function protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.bijective.1⟩ @[simp] theorem equiv.to_embedding_coe_fn {α : Sort u} {β : Sort v} (f : α ≃ β) : (f.to_embedding : α → β) = f := rfl namespace function namespace embedding @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem inj' {α β} : ∀ (f : α ↪ β), injective f | ⟨f, hf⟩ := hf @[refl] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨_, injective_comp g.inj' f.inj'⟩ @[simp] theorem refl_apply {α} (x : α) : embedding.refl α x = x := rfl @[simp] theorem trans_apply {α β γ} (f : α ↪ β) (g : β ↪ γ) (a : α) : (f.trans g) a = g (f a) := rfl protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) protected noncomputable def of_surjective {α : Type u} {β : Type v} {f : β → α} (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ protected noncomputable def equiv_of_surjective {α : Type u} {β : Type v} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective ⟨f.inj, hf⟩ protected def of_not_nonempty {α : Sort u} {β : Sort v} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.inj (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl def prod_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨assume ⟨a, b⟩, (e₁ a, e₂ b), assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, have a₁ = a₂ ∧ b₁ = b₂, from (prod.mk.inj h).imp (assume h, e₁.inj h) (assume h, e₂.inj h), this.left ▸ this.right ▸ rfl⟩ section sum open sum def sum_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨assume s, match s with inl a := inl (e₁ a) | inr b := inr (e₂ b) end, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.inj $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.inj $ inr.inj h end⟩ @[simp] theorem sum_congr_apply_inl {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) (a) : sum_congr e₁ e₂ (inl a) = inl (e₁ a) := rfl @[simp] theorem sum_congr_apply_inr {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) (b) : sum_congr e₁ e₂ (inr b) = inr (e₂ b) := rfl end sum section sigma open sigma def sigma_congr_right {α : Type*} {β γ : α → Type*} (e : ∀ a, β a ↪ γ a) : sigma β ↪ sigma γ := ⟨λ ⟨a, b⟩, ⟨a, e a b⟩, λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, begin injection h with h₁ h₂, subst a₂, congr, exact (e a₁).2 (eq_of_heq h₂) end⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).inj (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.inj $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ end embedding end function namespace set /-- The injection map is an embedding between subsets. -/ def embedding_of_subset {α} {s t : set α} (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by congr; injection h⟩ end set
62703623f7d6212f4514472ffc5bfabe27d2010e
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/choiceExpectedTypeBug.lean
490925dcfd0196f76c8dbb35cb06e498a047801a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
615
lean
import Lean structure A := (x : Nat := 10) def f : A := { } theorem ex : f = { x := 10 } := rfl #check f syntax (name := emptyS) "⟨" "⟩" : term -- overload `⟨ ⟩` notation open Lean open Lean.Elab open Lean.Elab.Term @[termElab emptyS] def elabEmptyS : TermElab := fun stx expectedType? => do tryPostponeIfNoneOrMVar expectedType? let stxNew ← `(Nat.zero) withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? def foo (x : Unit) := x def f1 : Unit := let x := ⟨ ⟩ foo x def f2 : Unit := let x := ⟨ ⟩ x def f3 : Nat := let x := ⟨ ⟩ x theorem ex2 : f3 = 0 := rfl
1629a29921ca67e339484e002ed86b991edec251
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebraic_geometry/morphisms/quasi_separated.lean
f481d10e88d38a0dacf63e8351f87a779ea6f85c
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
23,334
lean
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import algebraic_geometry.morphisms.quasi_compact import topology.quasi_separated /-! # Quasi-separated morphisms A morphism of schemes `f : X ⟶ Y` is quasi-separated if the diagonal morphism `X ⟶ X ×[Y] X` is quasi-compact. A scheme is quasi-separated if the intersections of any two affine open sets is quasi-compact. (`algebraic_geometry.quasi_separated_space_iff_affine`) We show that a morphism is quasi-separated if the preimage of every affine open is quasi-separated. We also show that this property is local at the target, and is stable under compositions and base-changes. ## Main result - `is_localization_basic_open_of_qcqs` (**Qcqs lemma**): If `U` is qcqs, then `Γ(X, D(f)) ≃ Γ(X, U)_f` for every `f : Γ(X, U)`. -/ noncomputable theory open category_theory category_theory.limits opposite topological_space universe u open_locale algebraic_geometry namespace algebraic_geometry variables {X Y : Scheme.{u}} (f : X ⟶ Y) /-- A morphism is `quasi_separated` if diagonal map is quasi-compact. -/ @[mk_iff] class quasi_separated (f : X ⟶ Y) : Prop := (diagonal_quasi_compact : quasi_compact (pullback.diagonal f)) /-- The `affine_target_morphism_property` corresponding to `quasi_separated`, asserting that the domain is a quasi-separated scheme. -/ def quasi_separated.affine_property : affine_target_morphism_property := (λ X Y f _, quasi_separated_space X.carrier) lemma quasi_separated_space_iff_affine (X : Scheme) : quasi_separated_space X.carrier ↔ ∀ (U V : X.affine_opens), is_compact (U ∩ V : set X.carrier) := begin rw quasi_separated_space_iff, split, { intros H U V, exact H U V U.1.2 U.2.is_compact V.1.2 V.2.is_compact }, { intros H, suffices : ∀ (U : opens X.carrier) (hU : is_compact U.1) (V : opens X.carrier) (hV : is_compact V.1), is_compact (U ⊓ V).1, { intros U V hU hU' hV hV', exact this ⟨U, hU⟩ hU' ⟨V, hV⟩ hV' }, intros U hU V hV, apply compact_open_induction_on V hV, { simp }, { intros S hS V hV, change is_compact (U.1 ∩ (S.1 ∪ V.1)), rw set.inter_union_distrib_left, apply hV.union, clear hV, apply compact_open_induction_on U hU, { simp }, { intros S hS W hW, change is_compact ((S.1 ∪ W.1) ∩ V.1), rw set.union_inter_distrib_right, apply hW.union, apply H } } } end lemma quasi_compact_affine_property_iff_quasi_separated_space {X Y : Scheme} [is_affine Y] (f : X ⟶ Y) : quasi_compact.affine_property.diagonal f ↔ quasi_separated_space X.carrier := begin delta affine_target_morphism_property.diagonal, rw quasi_separated_space_iff_affine, split, { intros H U V, haveI : is_affine _ := U.2, haveI : is_affine _ := V.2, let g : pullback (X.of_restrict U.1.open_embedding) (X.of_restrict V.1.open_embedding) ⟶ X := pullback.fst ≫ X.of_restrict _, have : is_open_immersion g := infer_instance, have e := homeomorph.of_embedding _ this.base_open.to_embedding, rw is_open_immersion.range_pullback_to_base_of_left at e, erw [subtype.range_coe, subtype.range_coe] at e, rw is_compact_iff_compact_space, exact @@homeomorph.compact_space _ _ (H _ _) e }, { introv H h₁ h₂, resetI, let g : pullback f₁ f₂ ⟶ X := pullback.fst ≫ f₁, have : is_open_immersion g := infer_instance, have e := homeomorph.of_embedding _ this.base_open.to_embedding, rw is_open_immersion.range_pullback_to_base_of_left at e, simp_rw is_compact_iff_compact_space at H, exact @@homeomorph.compact_space _ _ (H ⟨⟨_, h₁.base_open.open_range⟩, range_is_affine_open_of_open_immersion _⟩ ⟨⟨_, h₂.base_open.open_range⟩, range_is_affine_open_of_open_immersion _⟩) e.symm }, end lemma quasi_separated_eq_diagonal_is_quasi_compact : @quasi_separated = morphism_property.diagonal @quasi_compact := by { ext, exact quasi_separated_iff _ } lemma quasi_compact_affine_property_diagonal_eq : quasi_compact.affine_property.diagonal = quasi_separated.affine_property := by { ext, rw quasi_compact_affine_property_iff_quasi_separated_space, refl } lemma quasi_separated_eq_affine_property_diagonal : @quasi_separated = target_affine_locally quasi_compact.affine_property.diagonal := begin rw [quasi_separated_eq_diagonal_is_quasi_compact, quasi_compact_eq_affine_property], exact diagonal_target_affine_locally_eq_target_affine_locally _ quasi_compact.affine_property_is_local end lemma quasi_separated_eq_affine_property : @quasi_separated = target_affine_locally quasi_separated.affine_property := by rw [quasi_separated_eq_affine_property_diagonal, quasi_compact_affine_property_diagonal_eq] lemma quasi_separated.affine_property_is_local : quasi_separated.affine_property.is_local := quasi_compact_affine_property_diagonal_eq ▸ quasi_compact.affine_property_is_local.diagonal @[priority 900] instance quasi_separated_of_mono {X Y : Scheme} (f : X ⟶ Y) [mono f] : quasi_separated f := ⟨infer_instance⟩ lemma quasi_separated_stable_under_composition : morphism_property.stable_under_composition @quasi_separated := quasi_separated_eq_diagonal_is_quasi_compact.symm ▸ quasi_compact_stable_under_composition.diagonal quasi_compact_respects_iso quasi_compact_stable_under_base_change lemma quasi_separated_stable_under_base_change : morphism_property.stable_under_base_change @quasi_separated := quasi_separated_eq_diagonal_is_quasi_compact.symm ▸ quasi_compact_stable_under_base_change.diagonal quasi_compact_respects_iso instance quasi_separated_comp {X Y Z : Scheme} (f : X ⟶ Y) (g : Y ⟶ Z) [quasi_separated f] [quasi_separated g] : quasi_separated (f ≫ g) := quasi_separated_stable_under_composition f g infer_instance infer_instance lemma quasi_separated_respects_iso : morphism_property.respects_iso @quasi_separated := quasi_separated_eq_diagonal_is_quasi_compact.symm ▸ quasi_compact_respects_iso.diagonal lemma quasi_separated.affine_open_cover_tfae {X Y : Scheme.{u}} (f : X ⟶ Y) : tfae [quasi_separated f, ∃ (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)], ∀ (i : 𝒰.J), quasi_separated_space (pullback f (𝒰.map i)).carrier, ∀ (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)] (i : 𝒰.J), quasi_separated_space (pullback f (𝒰.map i)).carrier, ∀ {U : Scheme} (g : U ⟶ Y) [is_affine U] [is_open_immersion g], quasi_separated_space (pullback f g).carrier, ∃ (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)] (𝒰' : Π (i : 𝒰.J), Scheme.open_cover.{u} (pullback f (𝒰.map i))) [∀ i j, is_affine ((𝒰' i).obj j)], by exactI ∀ (i : 𝒰.J) (j k : (𝒰' i).J), compact_space (pullback ((𝒰' i).map j) ((𝒰' i).map k)).carrier] := begin have := quasi_compact.affine_property_is_local.diagonal_affine_open_cover_tfae f, simp_rw [← quasi_compact_eq_affine_property, ← quasi_separated_eq_diagonal_is_quasi_compact, quasi_compact_affine_property_diagonal_eq] at this, exact this end lemma quasi_separated.is_local_at_target : property_is_local_at_target @quasi_separated := quasi_separated_eq_affine_property_diagonal.symm ▸ quasi_compact.affine_property_is_local.diagonal.target_affine_locally_is_local lemma quasi_separated.open_cover_tfae {X Y : Scheme.{u}} (f : X ⟶ Y) : tfae [quasi_separated f, ∃ (𝒰 : Scheme.open_cover.{u} Y), ∀ (i : 𝒰.J), quasi_separated (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i), ∀ (𝒰 : Scheme.open_cover.{u} Y) (i : 𝒰.J), quasi_separated (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i), ∀ (U : opens Y.carrier), quasi_separated (f ∣_ U), ∀ {U : Scheme} (g : U ⟶ Y) [is_open_immersion g], quasi_separated (pullback.snd : pullback f g ⟶ _), ∃ {ι : Type u} (U : ι → opens Y.carrier) (hU : supr U = ⊤), ∀ i, quasi_separated (f ∣_ (U i))] := quasi_separated.is_local_at_target.open_cover_tfae f lemma quasi_separated_over_affine_iff {X Y : Scheme} (f : X ⟶ Y) [is_affine Y] : quasi_separated f ↔ quasi_separated_space X.carrier := by rw [quasi_separated_eq_affine_property, quasi_separated.affine_property_is_local.affine_target_iff f, quasi_separated.affine_property] lemma quasi_separated_space_iff_quasi_separated (X : Scheme) : quasi_separated_space X.carrier ↔ quasi_separated (terminal.from X) := (quasi_separated_over_affine_iff _).symm lemma quasi_separated.affine_open_cover_iff {X Y : Scheme.{u}} (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)] (f : X ⟶ Y) : quasi_separated f ↔ ∀ i, quasi_separated_space (pullback f (𝒰.map i)).carrier := begin rw [quasi_separated_eq_affine_property, quasi_separated.affine_property_is_local.affine_open_cover_iff f 𝒰], refl, end lemma quasi_separated.open_cover_iff {X Y : Scheme.{u}} (𝒰 : Scheme.open_cover.{u} Y) (f : X ⟶ Y) : quasi_separated f ↔ ∀ i, quasi_separated (pullback.snd : pullback f (𝒰.map i) ⟶ _) := quasi_separated.is_local_at_target.open_cover_iff f 𝒰 instance {X Y S : Scheme} (f : X ⟶ S) (g : Y ⟶ S) [quasi_separated g] : quasi_separated (pullback.fst : pullback f g ⟶ X) := quasi_separated_stable_under_base_change.fst f g infer_instance instance {X Y S : Scheme} (f : X ⟶ S) (g : Y ⟶ S) [quasi_separated f] : quasi_separated (pullback.snd : pullback f g ⟶ Y) := quasi_separated_stable_under_base_change.snd f g infer_instance instance {X Y Z: Scheme} (f : X ⟶ Y) (g : Y ⟶ Z) [quasi_separated f] [quasi_separated g] : quasi_separated (f ≫ g) := quasi_separated_stable_under_composition f g infer_instance infer_instance lemma quasi_separated_space_of_quasi_separated {X Y : Scheme} (f : X ⟶ Y) [hY : quasi_separated_space Y.carrier] [quasi_separated f] : quasi_separated_space X.carrier := begin rw quasi_separated_space_iff_quasi_separated at hY ⊢, have : f ≫ terminal.from Y = terminal.from X := terminal_is_terminal.hom_ext _ _, rw ← this, resetI, apply_instance end instance quasi_separated_space_of_is_affine (X : Scheme) [is_affine X] : quasi_separated_space X.carrier := begin constructor, intros U V hU hU' hV hV', obtain ⟨s, hs, e⟩ := (is_compact_open_iff_eq_basic_open_union _).mp ⟨hU', hU⟩, obtain ⟨s', hs', e'⟩ := (is_compact_open_iff_eq_basic_open_union _).mp ⟨hV', hV⟩, rw [e, e', set.Union₂_inter], simp_rw [set.inter_Union₂], apply hs.is_compact_bUnion, { intros i hi, apply hs'.is_compact_bUnion, intros i' hi', change is_compact (X.basic_open i ⊓ X.basic_open i').1, rw ← Scheme.basic_open_mul, exact ((top_is_affine_open _).basic_open_is_affine _).is_compact } end lemma is_affine_open.is_quasi_separated {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : is_quasi_separated (U : set X.carrier) := begin rw is_quasi_separated_iff_quasi_separated_space, exacts [@@algebraic_geometry.quasi_separated_space_of_is_affine _ hU, U.prop], end lemma quasi_separated_of_comp {X Y Z : Scheme} (f : X ⟶ Y) (g : Y ⟶ Z) [H : quasi_separated (f ≫ g)] : quasi_separated f := begin rw (quasi_separated.affine_open_cover_tfae f).out 0 1, rw (quasi_separated.affine_open_cover_tfae (f ≫ g)).out 0 2 at H, use (Z.affine_cover.pullback_cover g).bind (λ x, Scheme.affine_cover _), split, { intro i, dsimp, apply_instance }, rintro ⟨i, j⟩, dsimp at *, specialize H _ i, refine @@quasi_separated_space_of_quasi_separated _ H _, { exact pullback.map _ _ _ _ (𝟙 _) _ _ (by simp) (category.comp_id _) ≫ (pullback_right_pullback_fst_iso g (Z.affine_cover.map i) f).hom }, { apply algebraic_geometry.quasi_separated_of_mono } end lemma exists_eq_pow_mul_of_is_affine_open (X : Scheme) (U : opens X.carrier) (hU : is_affine_open U) (f : X.presheaf.obj (op U)) (x : X.presheaf.obj (op $ X.basic_open f)) : ∃ (n : ℕ) (y : X.presheaf.obj (op U)), y |_ X.basic_open f = (f |_ X.basic_open f) ^ n * x := begin have := (is_localization_basic_open hU f).2, obtain ⟨⟨y, _, n, rfl⟩, d⟩ := this x, use [n, y], delta Top.presheaf.restrict_open Top.presheaf.restrict, simpa [mul_comm x] using d.symm, end lemma exists_eq_pow_mul_of_is_compact_of_quasi_separated_space_aux (X : Scheme) (S : X.affine_opens) (U₁ U₂ : opens X.carrier) {n₁ n₂ : ℕ} {y₁ : X.presheaf.obj (op U₁)} {y₂ : X.presheaf.obj (op U₂)} {f : X.presheaf.obj (op $ U₁ ⊔ U₂)} {x : X.presheaf.obj (op $ X.basic_open f)} (h₁ : S.1 ≤ U₁) (h₂ : S.1 ≤ U₂) (e₁ : X.presheaf.map (hom_of_le $ X.basic_open_le (X.presheaf.map (hom_of_le le_sup_left).op f) : _ ⟶ U₁).op y₁ = X.presheaf.map (hom_of_le (by { erw X.basic_open_res, exact inf_le_left })).op (X.presheaf.map (hom_of_le le_sup_left).op f) ^ n₁ * (X.presheaf.map (hom_of_le (by { erw X.basic_open_res, exact inf_le_right })).op) x) (e₂ : X.presheaf.map (hom_of_le $ X.basic_open_le (X.presheaf.map (hom_of_le le_sup_right).op f) : _ ⟶ U₂).op y₂ = X.presheaf.map (hom_of_le (by { rw X.basic_open_res, exact inf_le_left })).op (X.presheaf.map (hom_of_le le_sup_right).op f) ^ n₂ * (X.presheaf.map (hom_of_le (by { rw X.basic_open_res, exact inf_le_right })).op) x) : ∃ n : ℕ, X.presheaf.map (hom_of_le $ h₁).op ((X.presheaf.map (hom_of_le le_sup_left).op f) ^ (n + n₂) * y₁) = X.presheaf.map (hom_of_le $ h₂).op ((X.presheaf.map (hom_of_le le_sup_right).op f) ^ (n + n₁) * y₂) := begin have := (is_localization_basic_open S.2 (X.presheaf.map (hom_of_le $ le_trans h₁ le_sup_left).op f)), obtain ⟨⟨_, n, rfl⟩, e⟩ := (@is_localization.eq_iff_exists _ _ _ _ _ _ this (X.presheaf.map (hom_of_le $ h₁).op ((X.presheaf.map (hom_of_le le_sup_left).op f) ^ n₂ * y₁)) (X.presheaf.map (hom_of_le $ h₂).op ((X.presheaf.map (hom_of_le le_sup_right).op f) ^ n₁ * y₂))).mp _, swap, { simp only [map_pow, ring_hom.algebra_map_to_algebra, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp, hom_of_le_comp], have h₃ : X.basic_open ((X.presheaf.map (hom_of_le (h₁.trans le_sup_left)).op) f) ≤ S.val, { simpa only [X.basic_open_res] using inf_le_left, }, transitivity X.presheaf.map (hom_of_le $ h₃.trans $ h₁.trans le_sup_left).op f ^ (n₂ + n₁) * X.presheaf.map (hom_of_le $ (X.basic_open_res f _).trans_le inf_le_right).op x, { rw [pow_add, mul_assoc], congr' 1, convert congr_arg (X.presheaf.map (hom_of_le _).op) e₁, { simp only [map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp], congr }, { simp only [map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp], congr }, { rw [X.basic_open_res, X.basic_open_res], rintros x ⟨H₁, H₂⟩, exact ⟨h₁ H₁, H₂⟩ } }, { rw [add_comm, pow_add, mul_assoc], congr' 1, convert congr_arg (X.presheaf.map (hom_of_le _).op) e₂.symm, { simp only [map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp], congr }, { simp only [map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp], congr }, { simp only [X.basic_open_res], rintros x ⟨H₁, H₂⟩, exact ⟨h₂ H₁, H₂⟩ } } }, use n, conv_lhs at e { rw mul_comm }, conv_rhs at e { rw mul_comm }, simp only [pow_add, map_pow, map_mul, ← comp_apply, ← mul_assoc, ← functor.map_comp, subtype.coe_mk] at e ⊢, convert e end lemma exists_eq_pow_mul_of_is_compact_of_is_quasi_separated (X : Scheme) (U : opens X.carrier) (hU : is_compact U.1) (hU' : is_quasi_separated U.1) (f : X.presheaf.obj (op U)) (x : X.presheaf.obj (op $ X.basic_open f)) : ∃ (n : ℕ) (y : X.presheaf.obj (op U)), y |_ X.basic_open f = (f |_ X.basic_open f) ^ n * x := begin delta Top.presheaf.restrict_open Top.presheaf.restrict, revert hU' f x, apply compact_open_induction_on U hU, { intros hU' f x, use [0, f], refine @@subsingleton.elim (CommRing.subsingleton_of_is_terminal (X.sheaf.is_terminal_of_eq_empty _)) _ _, erw eq_bot_iff, exact X.basic_open_le f }, { -- Given `f : 𝒪(S ∪ U), x : 𝒪(X_f)`, we need to show that `f ^ n * x` is the restriction of -- some `y : 𝒪(S ∪ U)` for some `n : ℕ`. intros S hS U hU hSU f x, -- We know that such `y₁, n₁` exists on `S` by the induction hypothesis. obtain ⟨n₁, y₁, hy₁⟩ := hU (hSU.of_subset $ set.subset_union_left _ _) (X.presheaf.map (hom_of_le le_sup_left).op f) (X.presheaf.map (hom_of_le _).op x), swap, { rw X.basic_open_res, exact inf_le_right }, -- We know that such `y₂, n₂` exists on `U` since `U` is affine. obtain ⟨n₂, y₂, hy₂⟩ := exists_eq_pow_mul_of_is_affine_open X _ U.2 (X.presheaf.map (hom_of_le le_sup_right).op f) (X.presheaf.map (hom_of_le _).op x), delta Top.presheaf.restrict_open Top.presheaf.restrict at hy₂, swap, { rw X.basic_open_res, exact inf_le_right }, -- Since `S ∪ U` is quasi-separated, `S ∩ U` can be covered by finite affine opens. obtain ⟨s, hs', hs⟩ := (is_compact_open_iff_eq_finset_affine_union _).mp ⟨hSU _ _ (set.subset_union_left _ _) S.2 hS (set.subset_union_right _ _) U.1.2 U.2.is_compact, (S ⊓ U.1).2⟩, haveI := hs'.to_subtype, casesI nonempty_fintype s, replace hs : S ⊓ U.1 = supr (λ i : s, (i : opens X.carrier)) := by { ext1, simpa using hs }, have hs₁ : ∀ i : s, i.1.1 ≤ S, { intro i, change (i : opens X.carrier) ≤ S, refine le_trans _ inf_le_left, use U.1, erw hs, exact le_supr _ _ }, have hs₂ : ∀ i : s, i.1.1 ≤ U.1, { intro i, change (i : opens X.carrier) ≤ U, refine le_trans _ inf_le_right, use S, erw hs, exact le_supr _ _ }, -- On each affine open in the intersection, we have `f ^ (n + n₂) * y₁ = f ^ (n + n₁) * y₂` -- for some `n` since `f ^ n₂ * y₁ = f ^ (n₁ + n₂) * x = f ^ n₁ * y₂` on `X_f`. have : ∀ i : s, ∃ n : ℕ, X.presheaf.map (hom_of_le $ hs₁ i).op ((X.presheaf.map (hom_of_le le_sup_left).op f) ^ (n + n₂) * y₁) = X.presheaf.map (hom_of_le $ hs₂ i).op ((X.presheaf.map (hom_of_le le_sup_right).op f) ^ (n + n₁) * y₂), { intro i, exact exists_eq_pow_mul_of_is_compact_of_quasi_separated_space_aux X i.1 S U (hs₁ i) (hs₂ i) hy₁ hy₂ }, choose n hn using this, -- We can thus choose a big enough `n` such that `f ^ (n + n₂) * y₁ = f ^ (n + n₁) * y₂` -- on `S ∩ U`. have : X.presheaf.map (hom_of_le $ inf_le_left).op ((X.presheaf.map (hom_of_le le_sup_left).op f) ^ (finset.univ.sup n + n₂) * y₁) = X.presheaf.map (hom_of_le $ inf_le_right).op ((X.presheaf.map (hom_of_le le_sup_right).op f) ^ (finset.univ.sup n + n₁) * y₂), { fapply X.sheaf.eq_of_locally_eq' (λ i : s, i.1.1), { refine λ i, hom_of_le _, erw hs, exact le_supr _ _ }, { exact le_of_eq hs }, { intro i, replace hn := congr_arg (λ x, X.presheaf.map (hom_of_le (le_trans (hs₁ i) le_sup_left)).op f ^ (finset.univ.sup n - n i) * x) (hn i), dsimp only at hn, delta Scheme.sheaf SheafedSpace.sheaf, simp only [← map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp, ← mul_assoc] at hn ⊢, erw [← map_mul, ← map_mul] at hn, rw [← pow_add, ← pow_add, ← add_assoc, ← add_assoc, tsub_add_cancel_of_le] at hn, convert hn, exact finset.le_sup (finset.mem_univ _) } }, use finset.univ.sup n + n₁ + n₂, -- By the sheaf condition, since `f ^ (n + n₂) * y₁ = f ^ (n + n₁) * y₂`, it can be glued into -- the desired section on `S ∪ U`. use (X.sheaf.obj_sup_iso_prod_eq_locus S U.1).inv ⟨⟨_ * _, _ * _⟩, this⟩, refine X.sheaf.eq_of_locally_eq₂ (hom_of_le (_ : X.basic_open (X.presheaf.map (hom_of_le le_sup_left).op f) ≤ _)) (hom_of_le (_ : X.basic_open (X.presheaf.map (hom_of_le le_sup_right).op f) ≤ _)) _ _ _ _ _, { rw X.basic_open_res, exact inf_le_right }, { rw X.basic_open_res, exact inf_le_right }, { rw [X.basic_open_res, X.basic_open_res], erw ← inf_sup_right, refine le_inf_iff.mpr ⟨X.basic_open_le f, le_of_eq rfl⟩ }, { convert congr_arg (X.presheaf.map (hom_of_le _).op) (X.sheaf.obj_sup_iso_prod_eq_locus_inv_fst S U.1 ⟨⟨_ * _, _ * _⟩, this⟩) using 1, { delta Scheme.sheaf SheafedSpace.sheaf, simp only [← comp_apply (X.presheaf.map _) (X.presheaf.map _), ← functor.map_comp, ← op_comp], congr }, { delta Scheme.sheaf SheafedSpace.sheaf, simp only [map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp, mul_assoc, pow_add], erw hy₁, congr' 1, rw [← mul_assoc, ← mul_assoc], congr' 1, rw [mul_comm, ← comp_apply, ← functor.map_comp], congr } }, { convert congr_arg (X.presheaf.map (hom_of_le _).op) (X.sheaf.obj_sup_iso_prod_eq_locus_inv_snd S U.1 ⟨⟨_ * _, _ * _⟩, this⟩) using 1, { delta Scheme.sheaf SheafedSpace.sheaf, simp only [← comp_apply (X.presheaf.map _) (X.presheaf.map _), ← functor.map_comp, ← op_comp], congr }, { delta Scheme.sheaf SheafedSpace.sheaf, simp only [map_pow, map_mul, ← comp_apply, ← functor.map_comp, ← op_comp, mul_assoc, pow_add], erw hy₂, rw [← comp_apply, ← functor.map_comp], congr } } } end /-- If `U` is qcqs, then `Γ(X, D(f)) ≃ Γ(X, U)_f` for every `f : Γ(X, U)`. This is known as the **Qcqs lemma** in [R. Vakil, *The rising sea*][RisingSea]. -/ lemma is_localization_basic_open_of_qcqs {X : Scheme} {U : opens X.carrier} (hU : is_compact U.1) (hU' : is_quasi_separated U.1) (f : X.presheaf.obj (op U)) : is_localization.away f (X.presheaf.obj (op $ X.basic_open f)) := begin constructor, { rintro ⟨_, n, rfl⟩, simp only [map_pow, subtype.coe_mk, ring_hom.algebra_map_to_algebra], exact is_unit.pow _ (RingedSpace.is_unit_res_basic_open _ f), }, { intro z, obtain ⟨n, y, e⟩ := exists_eq_pow_mul_of_is_compact_of_is_quasi_separated X U hU hU' f z, refine ⟨⟨y, _, n, rfl⟩, _⟩, simpa only [map_pow, subtype.coe_mk, ring_hom.algebra_map_to_algebra, mul_comm z] using e.symm }, { intros x y, rw [← sub_eq_zero, ← map_sub, ring_hom.algebra_map_to_algebra], simp_rw [← @sub_eq_zero _ _ (x * _) (y * _), ← sub_mul], generalize : x - y = z, split, { intro H, obtain ⟨n, e⟩ := exists_pow_mul_eq_zero_of_res_basic_open_eq_zero_of_is_compact X hU _ _ H, refine ⟨⟨_, n, rfl⟩, _⟩, simpa [mul_comm z] using e }, { rintro ⟨⟨_, n, rfl⟩, e : z * f ^ n = 0⟩, rw [← ((RingedSpace.is_unit_res_basic_open _ f).pow n).mul_left_inj, zero_mul, ← map_pow, ← map_mul, e, map_zero] } } end end algebraic_geometry
1aba7ded4670c087095721887bd5cca77577f456
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/trans.lean
417886186be747bf034d237c92f1eef91f91d09f
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
434
lean
instance : Trans (α := Nat) (β := Nat) (γ := Nat) (.≤.) (.≤.) (.≤.) where trans := Nat.le_trans instance : Trans (α := Int) (β := Int) (γ := Int) (.≤.) (.≤.) (.≤.) where trans := sorry theorem ex1 {a b c d : Nat} (h1 : a ≤ b) (h2 : b ≤ c) (h3 : c ≤ d) : a ≤ d := trans h1 <| trans h2 h3 theorem ex2 {a b c d : Int} (h1 : a ≤ b) (h2 : b ≤ c) (h3 : c ≤ d) : a ≤ d := trans h1 <| trans h2 h3
69c364976b549cc5dbb113d789f43ee61b69bba4
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/tests/lean/run/dsimp_partial_app.lean
007abf7d8fafea8480de792d566d1949a18c0184
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
465
lean
open tactic meta def check_expr (p : pexpr) (t : expr) : tactic unit := do e ← to_expr p, guard (expr.alpha_eqv t e) meta def check_target (p : pexpr) : tactic unit := do t ← target, check_expr p t example (a : list nat) : a = [1, 2] → a^.for nat.succ = [2, 3] := begin intros, dsimp [list.for, flip], check_target `(list.map nat.succ a = [2, 3]), subst a, dsimp [list.map], check_target `([nat.succ 1, nat.succ 2] = [2, 3]), reflexivity end
aba3e354119836a4582db39739ce81dd962e0bb6
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/instructor/lectures/lecture_4.lean
08322954c0e65cd629fae7667c628ab3cb4d568b
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
3,712
lean
/- We've seen that logics start with axioms that can then be combined (with other information) using *inference rules* to derive theorems. In this file we review what we've covered so far and then we introduce: (1) The concept of introduction and elimination rules for a given logical construct. (2) We distinguish the reflexivity axiom as an *introduction* rule (one that produces a proof of an equality), and the substitutability of equals as an *elimination* rule (one that uses, or consumes) a proof of an equality to produce some other kind of result. (3) We explicitly identity the introduction rules for ∀ and for →. To produce a proof of ∀ (x : T), P x (where T is a type and P is a predicate that asserts some property of x), we *assume* that we're given an arbitrary but specific (x : T) ["x of type T"], and then we prove (P x) *for that x*. Because we made no assumptions whatsoever about x, if we can show that (P x) is true, then it must be true *for all* (x : T). -/ /- Introduction rule for equality. -/ axiom eq_refl : ∀ (T : Type) -- if T is any type (of thing) (t : T), -- and t is thing of that type, T t = t -- the result type: proof of t = t /- Elimination rule for equality. -/ axiom eq_subst : ∀ (T : Type) -- if T is a type (P : T → Prop) -- and P is a property of T objects (x y : T) -- and x and y are T objects (e : x = y) -- and you have a proof that x = y (px : P x), -- and you have a proof that x has property P P y -- then you can deduce (and get a proof) of P y /- The Lean versions of these axioms are called eq.refl and eq.subst. They're defined in ways that allow (and require) one not to give the T, P, x, or y parameters explicitly when applying eq_subst. More details come later. -/ /- CONJECTURES (review) -/ -- A conjecture: equality is symmetric. def eq_symm : Prop := ∀ (T : Type) (x y : T), x = y → y = x -- A conjecture: equality is transitive. def eq_trans : Prop := ∀ (T : Type) (x y z : T), x = y → y = z → x = z /- PROOFS: From conjectures to theorems -/ example : eq_symm := begin unfold eq_symm, -- replace name with definition assume T x y e, -- Introduction rule for ∀ rw e, -- Elimination rule for = -- QED. end /- A different proof, now using eq.subst explicitly. Any proof of a proposition is as good as any other for showing the truth of a proposition. We do not care what proofs you give, as long as they're correct (unless stated otherwise). -/ example : eq_symm := begin unfold eq_symm, -- replace name with definition assume T x y e, -- introduction rule for ∀ apply eq.subst e, -- elimination rule for = exact eq.refl x, -- introduction rule for = -- QED. end /- Review: Proof: equality is transitive. -/ example : eq_trans := begin unfold eq_trans, assume T x y z e1 e2, -- introduction rule for ∀ rw e1, -- elimination rule for = exact e2, end /- Note: Lean defines these rules as - eq.refl - eq.subst - eq.symm - eq.trans -/ /- Practice -/ example : ∀ (T : Type) (x y z : T), x = y → y = z → z = x := begin assume T x y z h1 h2, -- introduction rule for ∀ apply eq.symm _, -- application of symm *theorem* apply eq.trans h1 h2, -- application of trans theorem end /- INTRODUCTION and ELIMINATION RULES -/ /- For = - introduction rule: eq.refl - elimination rule: eq.subst -/ /- For ∀ x, P x - introduction rule: assume arbitrary x, then show P x - elimination rule: next time! -/ /- For P → Q - introduction rule: assume arbitrary P, then show Q - elimination rule: next time. -/
6b21b8f75639bd5d6ce92bb67e6f0aadca37395c
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/instances/nnreal.lean
0f8c94e0ba68a0975ff6b1f551f14e48f63e64ff
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
9,411
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import topology.algebra.infinite_sum import topology.algebra.group_with_zero /-! # Topology on `ℝ≥0` The natural topology on `ℝ≥0` (the one induced from `ℝ`), and a basic API. ## Main definitions Instances for the following typeclasses are defined: * `topological_space ℝ≥0` * `topological_semiring ℝ≥0` * `second_countable_topology ℝ≥0` * `order_topology ℝ≥0` * `has_continuous_sub ℝ≥0` * `has_continuous_inv₀ ℝ≥0` (continuity of `x⁻¹` away from `0`) * `has_continuous_smul ℝ≥0 ℝ` Everything is inherited from the corresponding structures on the reals. ## Main statements Various mathematically trivial lemmas are proved about the compatibility of limits and sums in `ℝ≥0` and `ℝ`. For example * `tendsto_coe {f : filter α} {m : α → ℝ≥0} {x : ℝ≥0} : tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x)` says that the limit of a filter along a map to `ℝ≥0` is the same in `ℝ` and `ℝ≥0`, and * `coe_tsum {f : α → ℝ≥0} : ((∑'a, f a) : ℝ) = (∑'a, (f a : ℝ))` says that says that a sum of elements in `ℝ≥0` is the same in `ℝ` and `ℝ≥0`. Similarly, some mathematically trivial lemmas about infinite sums are proved, a few of which rely on the fact that subtraction is continuous. -/ noncomputable theory open set topological_space metric filter open_locale topological_space namespace nnreal open_locale nnreal big_operators filter instance : topological_space ℝ≥0 := infer_instance -- short-circuit type class inference instance : topological_semiring ℝ≥0 := { continuous_mul := (continuous_subtype_val.fst'.mul continuous_subtype_val.snd').subtype_mk _, continuous_add := (continuous_subtype_val.fst'.add continuous_subtype_val.snd').subtype_mk _ } instance : second_countable_topology ℝ≥0 := topological_space.subtype.second_countable_topology _ _ instance : order_topology ℝ≥0 := @order_topology_of_ord_connected _ _ _ _ (Ici 0) _ section coe variable {α : Type*} open filter finset lemma _root_.continuous_real_to_nnreal : continuous real.to_nnreal := (continuous_id.max continuous_const).subtype_mk _ lemma continuous_coe : continuous (coe : ℝ≥0 → ℝ) := continuous_subtype_val /-- Embedding of `ℝ≥0` to `ℝ` as a bundled continuous map. -/ @[simps { fully_applied := ff }] def _root_.continuous_map.coe_nnreal_real : C(ℝ≥0, ℝ) := ⟨coe, continuous_coe⟩ instance {X : Type*} [topological_space X] : can_lift C(X, ℝ) C(X, ℝ≥0) := { coe := continuous_map.coe_nnreal_real.comp, cond := λ f, ∀ x, 0 ≤ f x, prf := λ f hf, ⟨⟨λ x, ⟨f x, hf x⟩, f.2.subtype_mk _⟩, fun_like.ext' rfl⟩ } @[simp, norm_cast] lemma tendsto_coe {f : filter α} {m : α → ℝ≥0} {x : ℝ≥0} : tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x) := tendsto_subtype_rng.symm lemma tendsto_coe' {f : filter α} [ne_bot f] {m : α → ℝ≥0} {x : ℝ} : tendsto (λ a, m a : α → ℝ) f (𝓝 x) ↔ ∃ hx : 0 ≤ x, tendsto m f (𝓝 ⟨x, hx⟩) := ⟨λ h, ⟨ge_of_tendsto' h (λ c, (m c).2), tendsto_coe.1 h⟩, λ ⟨hx, hm⟩, tendsto_coe.2 hm⟩ @[simp] lemma map_coe_at_top : map (coe : ℝ≥0 → ℝ) at_top = at_top := map_coe_Ici_at_top 0 lemma comap_coe_at_top : comap (coe : ℝ≥0 → ℝ) at_top = at_top := (at_top_Ici_eq 0).symm @[simp, norm_cast] lemma tendsto_coe_at_top {f : filter α} {m : α → ℝ≥0} : tendsto (λ a, (m a : ℝ)) f at_top ↔ tendsto m f at_top := tendsto_Ici_at_top.symm lemma tendsto_real_to_nnreal {f : filter α} {m : α → ℝ} {x : ℝ} (h : tendsto m f (𝓝 x)) : tendsto (λa, real.to_nnreal (m a)) f (𝓝 (real.to_nnreal x)) := (continuous_real_to_nnreal.tendsto _).comp h lemma nhds_zero : 𝓝 (0 : ℝ≥0) = ⨅a ≠ 0, 𝓟 (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot] lemma nhds_zero_basis : (𝓝 (0 : ℝ≥0)).has_basis (λ a : ℝ≥0, 0 < a) (λ a, Iio a) := nhds_bot_basis instance : has_continuous_sub ℝ≥0 := ⟨((continuous_coe.fst'.sub continuous_coe.snd').max continuous_const).subtype_mk _⟩ instance : has_continuous_inv₀ ℝ≥0 := ⟨λ x hx, tendsto_coe.1 $ (real.tendsto_inv $ nnreal.coe_ne_zero.2 hx).comp continuous_coe.continuous_at⟩ instance : has_continuous_smul ℝ≥0 ℝ := { continuous_smul := real.continuous_mul.comp $ (continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd } @[norm_cast] lemma has_sum_coe {f : α → ℝ≥0} {r : ℝ≥0} : has_sum (λa, (f a : ℝ)) (r : ℝ) ↔ has_sum f r := by simp only [has_sum, coe_sum.symm, tendsto_coe] lemma has_sum_real_to_nnreal_of_nonneg {f : α → ℝ} (hf_nonneg : ∀ n, 0 ≤ f n) (hf : summable f) : has_sum (λ n, real.to_nnreal (f n)) (real.to_nnreal (∑' n, f n)) := begin have h_sum : (λ s, ∑ b in s, real.to_nnreal (f b)) = λ s, real.to_nnreal (∑ b in s, f b), from funext (λ _, (real.to_nnreal_sum_of_nonneg (λ n _, hf_nonneg n)).symm), simp_rw [has_sum, h_sum], exact tendsto_real_to_nnreal hf.has_sum, end @[norm_cast] lemma summable_coe {f : α → ℝ≥0} : summable (λa, (f a : ℝ)) ↔ summable f := begin split, exact assume ⟨a, ha⟩, ⟨⟨a, has_sum_le (λa, (f a).2) has_sum_zero ha⟩, has_sum_coe.1 ha⟩, exact assume ⟨a, ha⟩, ⟨a.1, has_sum_coe.2 ha⟩ end lemma summable_coe_of_nonneg {f : α → ℝ} (hf₁ : ∀ n, 0 ≤ f n) : @summable (ℝ≥0) _ _ _ (λ n, ⟨f n, hf₁ n⟩) ↔ summable f := begin lift f to α → ℝ≥0 using hf₁ with f rfl hf₁, simp only [summable_coe, subtype.coe_eta] end open_locale classical @[norm_cast] lemma coe_tsum {f : α → ℝ≥0} : ↑∑'a, f a = ∑'a, (f a : ℝ) := if hf : summable f then (eq.symm $ (has_sum_coe.2 $ hf.has_sum).tsum_eq) else by simp [tsum, hf, mt summable_coe.1 hf] lemma coe_tsum_of_nonneg {f : α → ℝ} (hf₁ : ∀ n, 0 ≤ f n) : (⟨∑' n, f n, tsum_nonneg hf₁⟩ : ℝ≥0) = (∑' n, ⟨f n, hf₁ n⟩ : ℝ≥0) := begin lift f to α → ℝ≥0 using hf₁ with f rfl hf₁, simp_rw [← nnreal.coe_tsum, subtype.coe_eta] end lemma tsum_mul_left (a : ℝ≥0) (f : α → ℝ≥0) : ∑' x, a * f x = a * ∑' x, f x := nnreal.eq $ by simp only [coe_tsum, nnreal.coe_mul, tsum_mul_left] lemma tsum_mul_right (f : α → ℝ≥0) (a : ℝ≥0) : (∑' x, f x * a) = (∑' x, f x) * a := nnreal.eq $ by simp only [coe_tsum, nnreal.coe_mul, tsum_mul_right] lemma summable_comp_injective {β : Type*} {f : α → ℝ≥0} (hf : summable f) {i : β → α} (hi : function.injective i) : summable (f ∘ i) := nnreal.summable_coe.1 $ show summable ((coe ∘ f) ∘ i), from (nnreal.summable_coe.2 hf).comp_injective hi lemma summable_nat_add (f : ℕ → ℝ≥0) (hf : summable f) (k : ℕ) : summable (λ i, f (i + k)) := summable_comp_injective hf $ add_left_injective k lemma summable_nat_add_iff {f : ℕ → ℝ≥0} (k : ℕ) : summable (λ i, f (i + k)) ↔ summable f := begin rw [← summable_coe, ← summable_coe], exact @summable_nat_add_iff ℝ _ _ _ (λ i, (f i : ℝ)) k, end lemma has_sum_nat_add_iff {f : ℕ → ℝ≥0} (k : ℕ) {a : ℝ≥0} : has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) := by simp [← has_sum_coe, coe_sum, nnreal.coe_add, ← has_sum_nat_add_iff k] lemma sum_add_tsum_nat_add {f : ℕ → ℝ≥0} (k : ℕ) (hf : summable f) : ∑' i, f i = (∑ i in range k, f i) + ∑' i, f (i + k) := by rw [←nnreal.coe_eq, coe_tsum, nnreal.coe_add, coe_sum, coe_tsum, sum_add_tsum_nat_add k (nnreal.summable_coe.2 hf)] lemma infi_real_pos_eq_infi_nnreal_pos [complete_lattice α] {f : ℝ → α} : (⨅ (n : ℝ) (h : 0 < n), f n) = (⨅ (n : ℝ≥0) (h : 0 < n), f n) := le_antisymm (infi_mono' $ λ r, ⟨r, le_rfl⟩) (infi₂_mono' $ λ r hr, ⟨⟨r, hr.le⟩, hr, le_rfl⟩) end coe lemma tendsto_cofinite_zero_of_summable {α} {f : α → ℝ≥0} (hf : summable f) : tendsto f cofinite (𝓝 0) := begin have h_f_coe : f = λ n, real.to_nnreal (f n : ℝ), from funext (λ n, real.to_nnreal_coe.symm), rw [h_f_coe, ← @real.to_nnreal_coe 0], exact tendsto_real_to_nnreal ((summable_coe.mpr hf).tendsto_cofinite_zero), end lemma tendsto_at_top_zero_of_summable {f : ℕ → ℝ≥0} (hf : summable f) : tendsto f at_top (𝓝 0) := by { rw ←nat.cofinite_eq_at_top, exact tendsto_cofinite_zero_of_summable hf } /-- The sum over the complement of a finset tends to `0` when the finset grows to cover the whole space. This does not need a summability assumption, as otherwise all sums are zero. -/ lemma tendsto_tsum_compl_at_top_zero {α : Type*} (f : α → ℝ≥0) : tendsto (λ (s : finset α), ∑' b : {x // x ∉ s}, f b) at_top (𝓝 0) := begin simp_rw [← tendsto_coe, coe_tsum, nnreal.coe_zero], exact tendsto_tsum_compl_at_top_zero (λ (a : α), (f a : ℝ)) end /-- `x ↦ x ^ n` as an order isomorphism of `ℝ≥0`. -/ def pow_order_iso (n : ℕ) (hn : n ≠ 0) : ℝ≥0 ≃o ℝ≥0 := strict_mono.order_iso_of_surjective (λ x, x ^ n) (λ x y h, strict_mono_on_pow hn.bot_lt (zero_le x) (zero_le y) h) $ (continuous_id.pow _).surjective (tendsto_pow_at_top hn) $ by simpa [order_bot.at_bot_eq, pos_iff_ne_zero] end nnreal
9f8bd86773386694438e550e44c51a79534a6efb
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/algebra/module.lean
11947b9d3278f0f4e451b7917fb19b7e238052d2
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
14,184
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro Modules over a ring. -/ import algebra.ring algebra.big_operators group_theory.subgroup group_theory.group_action open function universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} -- /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/ -- class has_scalar (α : Type u) (γ : Type v) := (smul : α → γ → γ) -- infixr ` • `:73 := has_scalar.smul /-- A semimodule is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `α` and an additive monoid of "vectors" `β`, connected by a "scalar multiplication" operation `r • x : β` (where `r : α` and `x : β`) with some natural associativity and distributivity axioms similar to those on a ring. -/ class semimodule (α : Type u) (β : Type v) [semiring α] [add_comm_monoid β] extends distrib_mul_action α β := (add_smul : ∀(r s : α) (x : β), (r + s) • x = r • x + s • x) (zero_smul : ∀x : β, (0 : α) • x = 0) section semimodule variables [R:semiring α] [add_comm_monoid β] [semimodule α β] (r s : α) (x y : β) include R theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x variables (α) @[simp] theorem zero_smul : (0 : α) • x = 0 := semimodule.zero_smul α x lemma smul_smul : r • s • x = (r * s) • x := (mul_smul _ _ _).symm instance smul.is_add_monoid_hom {r : α} : is_add_monoid_hom (λ x : β, r • x) := { map_add := smul_add _, map_zero := smul_zero _ } end semimodule /-- A module is a generalization of vector spaces to a scalar ring. It consists of a scalar ring `α` and an additive group of "vectors" `β`, connected by a "scalar multiplication" operation `r • x : β` (where `r : α` and `x : β`) with some natural associativity and distributivity axioms similar to those on a ring. -/ class module (α : Type u) (β : Type v) [ring α] [add_comm_group β] extends semimodule α β structure module.core (α β) [ring α] [add_comm_group β] extends has_scalar α β := (smul_add : ∀(r : α) (x y : β), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : α) (x : β), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : α) (x : β), (r * s) • x = r • s • x) (one_smul : ∀x : β, (1 : α) • x = x) def module.of_core {α β} [ring α] [add_comm_group β] (M : module.core α β) : module α β := by letI := M.to_has_scalar; exact { zero_smul := λ x, have (0 : α) • x + (0 : α) • x = (0 : α) • x + 0, by rw ← M.add_smul; simp, add_left_cancel this, smul_zero := λ r, have r • (0:β) + r • 0 = r • 0 + 0, by rw ← M.smul_add; simp, add_left_cancel this, ..M } section module variables [ring α] [add_comm_group β] [module α β] (r s : α) (x y : β) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) variables (α) theorem neg_one_smul (x : β) : (-1 : α) • x = -x := by simp variables {α} @[simp] theorem smul_neg : r • (-x) = -(r • x) := by rw [← neg_one_smul α, ← mul_smul, mul_neg_one, neg_smul] theorem smul_sub (r : α) (x y : β) : r • (x - y) = r • x - r • y := by simp [smul_add]; rw smul_neg theorem sub_smul (r s : α) (y : β) : (r - s) • y = r • y - s • y := by simp [add_smul] end module instance semiring.to_semimodule [r : semiring α] : semimodule α α := { smul := (*), smul_add := mul_add, add_smul := add_mul, mul_smul := mul_assoc, one_smul := one_mul, zero_smul := zero_mul, smul_zero := mul_zero, ..r } @[simp] lemma smul_eq_mul [semiring α] {a a' : α} : a • a' = a * a' := rfl instance ring.to_module [r : ring α] : module α α := { ..semiring.to_semimodule } def is_ring_hom.to_module [ring α] [ring β] (f : α → β) [h : is_ring_hom f] : module α β := module.of_core { smul := λ r x, f r * x, smul_add := λ r x y, by unfold has_scalar.smul; rw [mul_add], add_smul := λ r s x, by unfold has_scalar.smul; rw [h.map_add, add_mul], mul_smul := λ r s x, by unfold has_scalar.smul; rw [h.map_mul, mul_assoc], one_smul := λ x, show f 1 * x = _, by rw [h.map_one, one_mul] } class is_linear_map (α : Type u) {β : Type v} {γ : Type w} [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] (f : β → γ) : Prop := (add : ∀x y, f (x + y) = f x + f y) (smul : ∀(c : α) x, f (c • x) = c • f x) structure linear_map (α : Type u) (β : Type v) (γ : Type w) [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] := (to_fun : β → γ) (add : ∀x y, to_fun (x + y) = to_fun x + to_fun y) (smul : ∀(c : α) x, to_fun (c • x) = c • to_fun x) infixr ` →ₗ `:25 := linear_map _ notation β ` →ₗ[`:25 α `] ` γ := linear_map α β γ namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f g : β →ₗ[α] γ) include α instance : has_coe_to_fun (β →ₗ[α] γ) := ⟨_, to_fun⟩ theorem is_linear : is_linear_map α f := {..f} @[extensionality] theorem ext {f g : β →ₗ[α] γ} (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H theorem ext_iff {f g : β →ₗ[α] γ} : f = g ↔ ∀ x, f x = g x := ⟨by rintro rfl; simp, ext⟩ @[simp] lemma map_add (x y : β) : f (x + y) = f x + f y := f.add x y @[simp] lemma map_smul (c : α) (x : β) : f (c • x) = c • f x := f.smul c x @[simp] lemma map_zero : f 0 = 0 := by rw [← zero_smul α, map_smul f 0 0, zero_smul] instance : is_add_group_hom f := ⟨map_add f⟩ @[simp] lemma map_neg (x : β) : f (- x) = - f x := by rw [← neg_one_smul α, map_smul, neg_one_smul] @[simp] lemma map_sub (x y : β) : f (x - y) = f x - f y := by simp [map_neg, map_add] @[simp] lemma map_sum {ι} {t : finset ι} {g : ι → β} : f (t.sum g) = t.sum (λi, f (g i)) := (finset.sum_hom f).symm def comp (f : γ →ₗ[α] δ) (g : β →ₗ[α] γ) : β →ₗ[α] δ := ⟨f ∘ g, by simp, by simp⟩ @[simp] lemma comp_apply (f : γ →ₗ[α] δ) (g : β →ₗ[α] γ) (x : β) : f.comp g x = f (g x) := rfl def id : β →ₗ[α] β := ⟨id, by simp, by simp⟩ @[simp] lemma id_apply (x : β) : @id α β _ _ _ x = x := rfl end linear_map namespace is_linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] variables [module α β] [module α γ] include α def mk' (f : β → γ) (H : is_linear_map α f) : β →ₗ γ := {to_fun := f, ..H} @[simp] theorem mk'_apply {f : β → γ} (H : is_linear_map α f) (x : β) : mk' f H x = f x := rfl lemma is_linear_map_neg : is_linear_map α (λ (z : β), -z) := is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm) lemma is_linear_map_smul {α R : Type*} [add_comm_group α] [comm_ring R] [module R α] (c : R): is_linear_map R (λ (z : α), c • z) := begin refine is_linear_map.mk (smul_add c) _, intros _ _, simp [smul_smul], ac_refl end --TODO: move lemma is_linear_map_smul' {α R : Type*} [add_comm_group α] [comm_ring R] [module R α] (a : α): is_linear_map R (λ (c : R), c • a) := begin refine is_linear_map.mk (λ x y, add_smul x y a) _, intros _ _, simp [smul_smul] end variables {f : β → γ} (lin : is_linear_map α f) include β γ lin @[simp] lemma map_zero : f (0 : β) = (0 : γ) := by rw [← zero_smul α (0 : β), lin.smul, zero_smul] @[simp] lemma map_add (x y : β) : f (x + y) = f x + f y := by rw [lin.add] @[simp] lemma map_neg (x : β) : f (- x) = - f x := by rw [← neg_one_smul α, lin.smul, neg_one_smul] @[simp] lemma map_sub (x y : β) : f (x - y) = f x - f y := by simp [lin.map_neg, lin.map_add] end is_linear_map /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (α : Type u) (β : Type v) [ring α] [add_comm_group β] [module α β] : Type v := (carrier : set β) (zero : (0:β) ∈ carrier) (add : ∀ {x y}, x ∈ carrier → y ∈ carrier → x + y ∈ carrier) (smul : ∀ (c:α) {x}, x ∈ carrier → c • x ∈ carrier) namespace submodule variables [ring α] [add_comm_group β] [add_comm_group γ] variables [module α β] [module α γ] variables (p p' : submodule α β) variables {r : α} {x y : β} instance : has_coe (submodule α β) (set β) := ⟨submodule.carrier⟩ instance : has_mem β (submodule α β) := ⟨λ x p, x ∈ (p : set β)⟩ @[simp] theorem mem_coe : x ∈ (p : set β) ↔ x ∈ p := iff.rfl theorem ext' {s t : submodule α β} (h : (s : set β) = t) : s = t := by cases s; cases t; congr' protected theorem ext'_iff {s t : submodule α β} : (s : set β) = t ↔ s = t := ⟨ext', λ h, h ▸ rfl⟩ @[extensionality] theorem ext {s t : submodule α β} (h : ∀ x, x ∈ s ↔ x ∈ t) : s = t := ext' $ set.ext h @[simp] lemma zero_mem : (0 : β) ∈ p := p.zero lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := p.add h₁ h₂ lemma smul_mem (r : α) (h : x ∈ p) : r • x ∈ p := p.smul r h lemma neg_mem (hx : x ∈ p) : -x ∈ p := by rw ← neg_one_smul α; exact p.smul_mem _ hx lemma sub_mem (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := p.add_mem hx (p.neg_mem hy) lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := ⟨λ h, by simpa using neg_mem p h, neg_mem p⟩ lemma add_mem_iff_left (h₁ : y ∈ p) : x + y ∈ p ↔ x ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, λ h₂, add_mem _ h₂ h₁⟩ lemma add_mem_iff_right (h₁ : x ∈ p) : x + y ∈ p ↔ y ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, add_mem _ h₁⟩ lemma sum_mem {ι : Type w} [decidable_eq ι] {t : finset ι} {f : ι → β} : (∀c∈t, f c ∈ p) → t.sum f ∈ p := finset.induction_on t (by simp [p.zero_mem]) (by simp [p.add_mem] {contextual := tt}) instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem _ x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩ instance : has_scalar α p := ⟨λ c x, ⟨c • x.1, smul_mem _ c x.2⟩⟩ @[simp] lemma coe_add (x y : p) : (↑(x + y) : β) = ↑x + ↑y := rfl @[simp] lemma coe_zero : ((0 : p) : β) = 0 := rfl @[simp] lemma coe_neg (x : p) : ((-x : p) : β) = -x := rfl @[simp] lemma coe_smul (r : α) (x : p) : ((r • x : p) : β) = r • ↑x := rfl instance : add_comm_group p := by refine {add := (+), zero := 0, neg := has_neg.neg, ..}; { intros, apply set_coe.ext, simp } instance submodule_is_add_subgroup : is_add_subgroup (p : set β) := { zero_mem := p.zero, add_mem := p.add, neg_mem := λ _, p.neg_mem } lemma coe_sub (x y : p) : (↑(x - y) : β) = ↑x - ↑y := by simp instance : module α p := by refine {smul := (•), ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } protected def subtype : p →ₗ[α] β := by refine {to_fun := coe, ..}; simp [coe_smul] @[simp] theorem subtype_apply (x : p) : p.subtype x = x := rfl end submodule @[reducible] def ideal (α : Type u) [comm_ring α] := submodule α α namespace ideal variables [comm_ring α] (I : ideal α) {a b : α} protected lemma zero_mem : (0 : α) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _ lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h end ideal /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ class vector_space (α : Type u) (β : Type v) [discrete_field α] [add_comm_group β] extends module α β instance discrete_field.to_vector_space {α : Type*} [discrete_field α] : vector_space α α := { .. ring.to_module } /-- Subspace of a vector space. Defined to equal `submodule`. -/ @[reducible] def subspace (α : Type u) (β : Type v) [discrete_field α] [add_comm_group β] [vector_space α β] : Type v := submodule α β instance subspace.vector_space {α β} {f : discrete_field α} [add_comm_group β] [vector_space α β] (p : subspace α β) : vector_space α p := {..submodule.module p} namespace submodule variables {R:discrete_field α} [add_comm_group β] [add_comm_group γ] variables [vector_space α β] [vector_space α γ] variables (p p' : submodule α β) variables {r : α} {x y : β} include R set_option class.instance_max_depth 36 theorem smul_mem_iff (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p := ⟨λ h, by simpa [smul_smul, inv_mul_cancel r0] using p.smul_mem (r⁻¹) h, p.smul_mem r⟩ end submodule namespace add_comm_monoid open add_monoid variables {M : Type*} [add_comm_monoid M] instance : semimodule ℕ M := { smul := smul, smul_add := λ _ _ _, smul_add _ _ _, add_smul := λ _ _ _, add_smul _ _ _, mul_smul := λ _ _ _, mul_smul _ _ _, one_smul := one_smul, zero_smul := zero_smul, smul_zero := smul_zero } end add_comm_monoid namespace add_comm_group variables {M : Type*} [add_comm_group M] instance : module ℤ M := { smul := gsmul, smul_add := λ _ _ _, gsmul_add _ _ _, add_smul := λ _ _ _, add_gsmul _ _ _, mul_smul := λ _ _ _, gsmul_mul _ _ _, one_smul := one_gsmul, zero_smul := zero_gsmul, smul_zero := gsmul_zero } end add_comm_group
4da8657b9306fb30639440bbf2e13f46770f003c
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/class1.lean
14f7ef36b47fb3a72d96434ac93ae02a38980c28
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
114
lean
open prod inhabited definition H : inhabited (Prop × num × (num → num)) := by tactic.apply_instance print H
c6b16ccafac14c5ca1360796dcba6387eccc88e2
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/620.lean
df4a5e6c690e68539ea638d5c31c1b649239b589
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
309
lean
structure Foo (α : Type) where foo : α class Bar (α β : Type) where coe : α → β variable {α : Type} (x : Foo (Foo α)) #reduce @Coe.coe (Foo (Foo α)) (Foo α) (Coe.mk fun y => y.foo) x -- x.1 #reduce (@Coe.coe (Foo (Foo α)) (Foo α) (Coe.mk fun y => y.foo) x).1 -- (Coe.coe x).1 instead of x.1.1
b73917f32155daa7f14804641b147c3897d412e9
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Init/Control/ExceptCps.lean
c95f37a98960cc1d08b9f9a22d9b72d4057f193b
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,957
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Control.Lawful /- The Exception monad transformer using CPS style. -/ def ExceptCpsT (ε : Type u) (m : Type u → Type v) (α : Type u) := (β : Type u) → (α → m β) → (ε → m β) → m β namespace ExceptCpsT @[inline] def run {ε α : Type u} [Monad m] (x : ExceptCpsT ε m α) : m (Except ε α) := x _ (fun a => pure (Except.ok a)) (fun e => pure (Except.error e)) @[inline] def runK {ε α : Type u} (x : ExceptCpsT ε m α) (s : ε) (ok : α → m β) (error : ε → m β) : m β := x _ ok error @[inline] def runCatch [Monad m] (x : ExceptCpsT α m α) : m α := x α pure pure instance : Monad (ExceptCpsT ε m) where map f x := fun _ k₁ k₂ => x _ (fun a => k₁ (f a)) k₂ pure a := fun _ k _ => k a bind x f := fun _ k₁ k₂ => x _ (fun a => f a _ k₁ k₂) k₂ instance : LawfulMonad (ExceptCpsT σ m) := by refine' { .. } <;> intros <;> rfl instance : MonadExceptOf ε (ExceptCpsT ε m) where throw e := fun _ _ k => k e tryCatch x handle := fun _ k₁ k₂ => x _ k₁ (fun e => handle e _ k₁ k₂) @[inline] def lift [Monad m] (x : m α) : ExceptCpsT ε m α := fun _ k _ => x >>= k instance [Monad m] : MonadLift m (ExceptCpsT σ m) where monadLift := ExceptCpsT.lift instance [Inhabited ε] : Inhabited (ExceptCpsT ε m α) where default := fun _ k₁ k₂ => k₂ arbitrary @[simp] theorem run_pure [Monad m] : run (pure x : ExceptCpsT ε m α) = pure (Except.ok x) := rfl @[simp] theorem run_lift {α ε : Type u} [Monad m] (x : m α) : run (ExceptCpsT.lift x : ExceptCpsT ε m α) = (x >>= fun a => pure (Except.ok a) : m (Except ε α)) := rfl @[simp] theorem run_throw [Monad m] : run (throw e : ExceptCpsT ε m β) = pure (Except.error e) := rfl @[simp] theorem run_bind_lift [Monad m] (x : m α) (f : α → ExceptCpsT ε m β) : run (ExceptCpsT.lift x >>= f : ExceptCpsT ε m β) = x >>= fun a => run (f a) := rfl @[simp] theorem run_bind_throw [Monad m] (e : ε) (f : α → ExceptCpsT ε m β) : run (throw e >>= f : ExceptCpsT ε m β) = run (throw e) := rfl @[simp] theorem runCatch_pure [Monad m] : runCatch (pure x : ExceptCpsT α m α) = pure x := rfl @[simp] theorem runCatch_lift {α : Type u} [Monad m] [LawfulMonad m] (x : m α) : runCatch (ExceptCpsT.lift x : ExceptCpsT α m α) = x := by simp [runCatch, lift] @[simp] theorem runCatch_throw [Monad m] : runCatch (throw a : ExceptCpsT α m α) = pure a := rfl @[simp] theorem runCatch_bind_lift [Monad m] (x : m α) (f : α → ExceptCpsT β m β) : runCatch (ExceptCpsT.lift x >>= f : ExceptCpsT β m β) = x >>= fun a => runCatch (f a) := rfl @[simp] theorem runCatch_bind_throw [Monad m] (e : β) (f : α → ExceptCpsT β m β) : runCatch (throw e >>= f : ExceptCpsT β m β) = pure e := rfl end ExceptCpsT
cbc7d4063e26b50f31005c0fec5fb3f9032f09e2
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/apply_fun.lean
843a7ecba598f742538fff2520dbde8ee9f2c3b6
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
2,590
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import tactic.monotonicity import order.basic open tactic interactive (parse) interactive (loc.ns) interactive.types (texpr location) lean.parser (tk) local postfix `?`:9001 := optional meta def apply_fun_name (e : pexpr) (h : name) (M : option pexpr) : tactic unit := do { H ← get_local h, t ← infer_type H, match t with | `(%%l = %%r) := do ltp ← infer_type l, mv ← mk_mvar, to_expr ``(congr_arg (%%e : %%ltp → %%mv) %%H) >>= note h, clear H | `(%%l ≤ %%r) := do if M.is_some then do Hmono ← M >>= tactic.i_to_expr, to_expr ``(%%Hmono %%H) >>= note h >> skip else do { n ← get_unused_name `mono, to_expr ``(monotone %%e) >>= assert n, do { intro_lst [`x, `y, `h], `[dsimp, mono], skip } <|> swap, Hmono ← get_local n, to_expr ``(%%Hmono %%H) >>= note h >> skip }, clear H | _ := skip end, -- let's try to force β-reduction at `h` try (tactic.interactive.dsimp tt [] [] (loc.ns [h]) {eta := false, beta := true}) } <|> fail ("failed to apply " ++ to_string e ++ " at " ++ to_string h) namespace tactic.interactive /-- Apply a function to some local assumptions which are either equalities or inequalities. For instance, if the context contains `h : a = b` and some function `f` then `apply_fun f at h` turns `h` into `h : f a = f b`. When the assumption is an inequality `h : a ≤ b`, a side goal `monotone f` is created, unless this condition is provided using `apply_fun f at h using P` where `P : monotone f`, or the `mono` tactic can prove it. Typical usage is: ```lean open function example (X Y Z : Type) (f : X → Y) (g : Y → Z) (H : injective $ g ∘ f) : injective f := begin intros x x' h, apply_fun g at h, exact H h end ``` -/ meta def apply_fun (q : parse texpr) (locs : parse location) (lem : parse (tk "using" *> texpr)?) : tactic unit := --do e ← tactic.i_to_expr q, match locs with | (loc.ns l) := do l.mmap' (λ l, match l with | some h := apply_fun_name q h lem | none := skip end) | wildcard := do ctx ← local_context, ctx.mmap' (λ h, apply_fun_name q h.local_pp_name lem) end end tactic.interactive add_tactic_doc { name := "apply_fun", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_fun], tags := ["context management"] }
4858362563fb78f39ca54677d5783a23222df154
618003631150032a5676f229d13a079ac875ff77
/src/group_theory/monoid_localization.lean
c39398a99e805bdc62aa52647eaf6b508a11b4f5
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
44,129
lean
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import group_theory.congruence import algebra.group.units import algebra.punit_instances /-! # Localizations of commutative monoids Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so we can generalize localizations to commutative monoids. We characterize the localization of a commutative monoid `M` at a submonoid `S` up to isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a monoid homomorphism `f : M →* N` satisfying 3 properties: 1. For all `y ∈ S`, `f y` is a unit; 2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`; 3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`. We also define the quotient of `M × S` by the unique congruence relation (equivalence relation preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S` satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s` whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard localization relation. This defines the localization as a quotient type, but the majority of subsequent lemmas in the file are given in terms of localizations up to isomorphism, using maps which satisfy the characteristic predicate. Given such a localization map `f : M →* N`, we can define the surjection `localization_map.mk'` sending `(x, y) : M × S` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `N` induced by a homomorphism from `M` which maps elements of `S` to invertible elements of the codomain. Similarly, given commutative monoids `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : M →* P` such that `g(S) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `N` to `Q`. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. The infimum form of the localization congruence relation is chosen as 'canonical' here, since it shortens some proofs. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. ## Tags localization, monoid localization, quotient monoid, congruence relation, characteristic predicate, commutative monoid -/ set_option old_structure_cmd true namespace add_submonoid variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N] /-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends add_monoid_hom M N := (map_add_units' : ∀ y : S, is_add_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c) /-- The add_monoid hom underlying a `localization_map` of `add_comm_monoid`s. -/ add_decl_doc localization_map.to_add_monoid_hom end add_submonoid variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N] {P : Type*} [comm_monoid P] namespace submonoid /-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends monoid_hom M N := (map_units' : ∀ y : S, is_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c) attribute [to_additive add_submonoid.localization_map] submonoid.localization_map attribute [to_additive add_submonoid.localization_map.to_add_monoid_hom] submonoid.localization_map.to_monoid_hom /-- The monoid hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_monoid_hom namespace localization /-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/ @[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S` an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`."] def r (S : submonoid M) : con (M × S) := Inf {c | ∀ y : S, c 1 (y, y)} /-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`. Its equivalence to `r` can be useful for proofs. -/ @[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`. Its equivalence to `r` can be useful for proofs."] def r' : con (M × S) := begin refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c, iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩, .. }, { rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use b.2 * t₁ * t₂, simp only [submonoid.coe_mul], calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl ... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl } ... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } }, { rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use t₁ * t₂, calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) : by ac_refl ... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } } end /-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `localization.r`) or explicitly (see `localization.r'`). -/ @[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `localization.r`) or explicitly (see `localization.r'`)."] theorem r_eq_r' : r S = r' S := le_antisymm (Inf_le $ λ _, ⟨1, by simp⟩) $ le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩, begin rw [← mul_one (p, q), ← mul_one y], refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _, convert b.symm (b.mul (b.refl y) (H (q * t))) using 1, rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc], refl end variables {S} @[to_additive] lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c := by rw r_eq_r' S; refl end localization /-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/ @[to_additive "The localization of an `add_comm_monoid` at one of its submonoids (as a quotient type)."] def localization := (localization.r S).quotient @[to_additive] instance localization.inhabited : inhabited (localization S) := con.quotient.inhabited end submonoid variables {S N} namespace monoid_hom /-- Makes a localization map from a `comm_monoid` hom satisfying the characteristic predicate. -/ @[to_additive "Makes a localization map from an `add_comm_monoid` hom satisfying the characteristic predicate."] def to_localization_map (f : M →* N) (H1 : ∀ y : S, is_unit (f y)) (H2 : ∀ z, ∃ x : M × S, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : S, x * c = y * c) : submonoid.localization_map S N := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end monoid_hom namespace submonoid namespace localization_map /-- Short for `to_monoid_hom`; used to apply a localization map as a function. -/ @[to_additive "Short for `to_add_monoid_hom`; used to apply a localization map as a function."] abbreviation to_map (f : localization_map S N) := f.to_monoid_hom @[to_additive, ext] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) : f = g := by cases f; cases g; simp only []; exact funext h attribute [ext] add_submonoid.localization_map.ext @[to_additive] lemma ext_iff {f g : localization_map S N} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ @[to_additive] lemma to_map_injective : function.injective (@localization_map.to_map _ _ S N _) := λ _ _ h, ext $ monoid_hom.ext_iff.1 h @[to_additive] lemma map_units (f : localization_map S N) (y : S) : is_unit (f.to_map y) := f.4 y @[to_additive] lemma surj (f : localization_map S N) (z : N) : ∃ x : M × S, z * f.to_map x.2 = f.to_map x.1 := f.5 z @[to_additive] lemma eq_iff_exists (f : localization_map S N) {x y} : f.to_map x = f.to_map y ↔ ∃ c : S, x * c = y * c := f.6 x y /-- Given a localization map `f : M →* N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x * (f y)⁻¹ = z`. -/ @[to_additive "Given a localization map `f : M →+ N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x - f y = z`."] noncomputable def sec (f : localization_map S N) (z : N) : M × S := classical.some $ f.surj z @[to_additive] lemma sec_spec {f : localization_map S N} (z : N) : z * f.to_map (f.sec z).2 = f.to_map (f.sec z).1 := classical.some_spec $ f.surj z @[to_additive] lemma sec_spec' {f : localization_map S N} (z : N) : f.to_map (f.sec z).1 = f.to_map (f.sec z).2 * z := by rw [mul_comm, sec_spec] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `w * (f y)⁻¹ = z ↔ w = f y * z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `w - f y = z ↔ w = f y + z`."] lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z ↔ w = f y * z := by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) h _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w * (f y)⁻¹ ↔ z * f y = w`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w - f y ↔ z + f y = w`."] lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : z = w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ ↔ z * f y = w := by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ * (f y₁)⁻¹ = f x₂ * (f y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁)`. -/ @[simp, to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ - f y₁ = f x₂ - f y₂ ↔ f (x₁ + y₂) = f (x₂ + y₁)`."] lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} : f x₁ * ↑(is_unit.lift_right (f.mrestrict S) h y₁)⁻¹ = f x₂ * ↑(is_unit.lift_right (f.mrestrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) := by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂, f.map_mul, f.map_mul] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y, z ∈ S`, we have `(f y)⁻¹ = (f z)⁻¹ → f y = f z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y, z ∈ S`, we have `- (f y) = - (f z) → f y = f z`."] lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z} (h : (is_unit.lift_right (f.mrestrict S) hf y)⁻¹ = (is_unit.lift_right (f.mrestrict S) hf z)⁻¹) : f y = f z := by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h]; convert units.inv_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) hf _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y ∈ S`, `(f y)⁻¹` is unique. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y ∈ S`, `- (f y)` is unique."] lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S} {z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z := by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H] variables (f : localization_map S N) @[to_additive] lemma map_right_cancel {x y} {c : S} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := begin rw [f.to_map.map_mul, f.to_map.map_mul] at h, cases f.map_units c with u hu, rw ←hu at h, exact (units.mul_right_inj u).1 h, end @[to_additive] lemma map_left_cancel {x y} {c : S} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.map_right_cancel $ by rw [mul_comm _ x, mul_comm _ y, h] /-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to `f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S` to `f x - f y`."] noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N := f.to_map x * ↑(is_unit.lift_right (f.to_map.mrestrict S) f.map_units y)⁻¹ @[to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := (mul_inv_left f.map_units _ _ _).2 $ show _ = _ * (_ * _ * (_ * _)), by rw [←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, mul_assoc, mul_assoc, mul_comm _ (f.to_map x₂), ←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, submonoid.coe_mul, f.to_map.map_mul, f.to_map.map_mul]; ac_refl @[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.to_map x := by rw [mk', monoid_hom.map_one]; exact mul_one _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z * f y = f x`, then `f x * (f y)⁻¹ = z`. -/ @[simp, to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z + f y = f x`, then `f x - f y = z`."] lemma mk'_sec (z : N) : f.mk' (f.sec z).1 (f.sec z).2 = z := show _ * _ = _, by rw [←sec_spec, mul_inv_left, mul_comm] @[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z := ⟨(f.sec z).1, (f.sec z).2, f.mk'_sec z⟩ @[to_additive] lemma mk'_spec (x) (y : S) : f.mk' x y * f.to_map y = f.to_map x := show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.to_map y), ←mul_assoc, mul_inv_left, mul_comm] @[to_additive] lemma mk'_spec' (x) (y : S) : f.to_map y * f.mk' x y = f.to_map x := by rw [mul_comm, mk'_spec] @[to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := ⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩ @[to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm] @[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := ⟨λ H, by rw [f.to_map.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc, mul_comm (f.to_map _), ←mul_assoc, mk'_spec, f.to_map.map_mul], λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.to_map y₁), ←mul_assoc, ←f.to_map.map_mul, ←H, f.to_map.map_mul, mul_inv_right f.map_units]⟩ @[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c := f.mk'_eq_iff_eq.trans $ f.eq_iff_exists @[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) := by rw [f.eq, localization.r_iff_exists] @[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.eq_iff_exists.trans g.eq_iff_exists.symm @[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.eq'.trans g.eq'.symm /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `f x₁ * (f y₁)⁻¹ * f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ * y₂ * c = x₂ * y₁ * c`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `(f x₁ - f y₁) + f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ + y₂ + c = x₂ + y₁ + c`."] lemma exists_of_sec_mk' (x) (y : S) : ∃ c : S, x * (f.sec $ f.mk' x y).2 * c = (f.sec $ f.mk' x y).1 * y * c := f.eq_iff_exists.1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm @[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.mk'_eq_iff_eq.2 $ H ▸ rfl @[simp, to_additive] lemma mk'_self (y : S) : f.mk' (y : M) y = 1 := show _ * _ = _, by rw [mul_inv_left, mul_one] @[simp, to_additive] lemma mk'_self' (x) (H : x ∈ S) : f.mk' x ⟨x, H⟩ = 1 := by convert mk'_self _ _; refl @[to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) : f.to_map x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y := by rw [←mk'_one, ←mk'_mul, one_mul] @[to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) : f.mk' x₂ y * f.to_map x₁ = f.mk' (x₁ * x₂) y := by rw [mul_comm, mul_mk'_eq_mk'_of_mul] @[to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) : f.to_map x * f.mk' 1 y = f.mk' x y := by rw [mul_mk'_eq_mk'_of_mul, mul_one] @[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) : f.mk' (x * y) y = f.to_map x := by rw [←mul_mk'_one_eq_mk', f.to_map.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self, mul_one] @[to_additive] lemma mk'_mul_cancel_left (x) (y : S) : f.mk' ((y : M) * x) y = f.to_map x := by rw [mul_comm, mk'_mul_cancel_right] @[to_additive] lemma is_unit_comp (j : N →* P) (y : S) : is_unit (j.comp f.to_map y) := ⟨units.map j $ is_unit.lift_right (f.to_map.mrestrict S) f.map_units y, show j _ = j _, from congr_arg j $ (is_unit.coe_lift_right (f.to_map.mrestrict S) f.map_units _)⟩ variables {g : M →* P} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g(S) ⊆ units P`, `f x = f y → g x = g y` for all `x y : M`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g(S) ⊆ add_units P`, `f x = f y → g x = g y` for all `x y : M`."] lemma eq_of_eq (hg : ∀ y : S, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := begin obtain ⟨c, hc⟩ := f.eq_iff_exists.1 h, rw [←mul_one (g x), ←is_unit.mul_lift_right_inv (g.mrestrict S) hg c], show _ * (g c * _) = _, rw [←mul_assoc, ←g.map_mul, hc, mul_inv_left hg, g.map_mul, mul_comm], end /-- Given `comm_monoid`s `M, P`, localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, and `g : M →* P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`. -/ @[to_additive "Given `add_comm_monoid`s `M, P`, localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, and `g : M →+ P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`."] lemma comp_eq_of_eq {T : submonoid P} {Q : Type*} [comm_monoid Q] (hg : ∀ y : S, g y ∈ T) (k : localization_map T Q) {x y} (h : f.to_map x = f.to_map y) : k.to_map (g x) = k.to_map (g y) := f.eq_of_eq (λ y : S, show is_unit (k.to_map.comp g y), from k.map_units ⟨g y, hg y⟩) h variables (hg : ∀ y : S, is_unit (g y)) /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x - g y`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def lift : N →* P := { to_fun := λ z, g (f.sec z).1 * ↑(is_unit.lift_right (g.mrestrict S) hg (f.sec z).2)⁻¹, map_one' := by rw [mul_inv_left, mul_one]; exact f.eq_of_eq hg (by rw [←sec_spec, one_mul]), map_mul' := λ x y, begin rw [mul_inv_left hg, ←mul_assoc, ←mul_assoc, mul_inv_right hg, mul_comm _ (g (f.sec y).1), ←mul_assoc, ←mul_assoc, mul_inv_right hg], repeat { rw ←g.map_mul }, exact f.eq_of_eq hg (by repeat { rw f.to_map.map_mul <|> rw sec_spec' }; ac_refl) end } variables {S g} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : M, y ∈ S`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x - f y` to `g x - g y` for all `x : M, y ∈ S`."] lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.mrestrict S) hg y)⁻¹ := (mul_inv hg).2 $ f.eq_of_eq hg $ by rw [f.to_map.map_mul, f.to_map.map_mul, sec_spec', mul_assoc, f.mk'_spec, mul_comm] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec (z v) : f.lift hg z = v ↔ g (f.sec z).1 = g (f.sec z).2 * v := mul_inv_left hg _ _ v /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v w : P`, we have `f.lift hg z * w = v ↔ g x * w = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v w : P`, we have `f.lift hg z + w = v ↔ g x + w = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec_mul (z w v) : f.lift hg z * w = v ↔ g (f.sec z).1 * w = g (f.sec z).2 * v := begin rw mul_comm, show _ * (_ * _) = _ ↔ _, rw [←mul_assoc, mul_inv_left hg, mul_comm], end @[to_additive] lemma lift_mk'_spec (x v) (y : S) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := by rw f.lift_mk' hg; exact mul_inv_left hg _ _ _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `f.lift hg z * g y = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `f.lift hg z + g y = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_right (z) : f.lift hg z * g (f.sec z).2 = g (f.sec z).1 := show _ * _ * _ = _, by erw [mul_assoc, is_unit.lift_right_inv_mul, mul_one] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `g y * f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `g y + f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_left (z) : g (f.sec z).2 * f.lift hg z = g (f.sec z).1 := by rw [mul_comm, lift_mul_right] @[simp, to_additive] lemma lift_eq (x : M) : f.lift hg (f.to_map x) = g x := by rw [lift_spec, ←g.map_mul]; exact f.eq_of_eq hg (by rw [sec_spec', f.to_map.map_mul]) @[to_additive] lemma lift_eq_iff {x y : M × S} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := by rw [lift_mk', lift_mk', mul_inv hg] @[simp, to_additive] lemma lift_comp : (f.lift hg).comp f.to_map = g := by ext; exact f.lift_eq hg _ @[simp, to_additive] lemma lift_of_comp (j : N →* P) : f.lift (f.is_unit_comp j) = j := begin ext, rw lift_spec, show j _ = j _ * _, erw [←j.map_mul, sec_spec'], end @[to_additive] lemma epic_of_localization_map {j k : N →* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := begin rw [←f.lift_of_comp j, ←f.lift_of_comp k], congr' 1, ext, exact h x, end @[to_additive] lemma lift_unique {j : N →* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := begin ext, rw [lift_spec, ←hj, ←hj, ←j.map_mul], apply congr_arg, rw ←sec_spec', end @[simp, to_additive] lemma lift_id (x) : f.lift f.map_units x = x := monoid_hom.ext_iff.1 (f.lift_of_comp $ monoid_hom.id N) x /-- Given two localization maps `f : M →* N, k : M →* P` for a submonoid `S ⊆ M`, the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P` induced by `k`. -/ @[simp, to_additive] lemma lift_left_inverse {k : localization_map S P} (z : N) : k.lift f.map_units (f.lift k.map_units z) = z := begin rw lift_spec, cases f.surj z with x hx, conv_rhs {congr, skip, rw f.eq_mk'_iff_mul_eq.2 hx}, rw [mk', ←mul_assoc, mul_inv_right f.map_units, ←f.to_map.map_mul, ←f.to_map.map_mul], apply k.eq_of_eq f.map_units, rw [k.to_map.map_mul, k.to_map.map_mul, ←sec_spec, mul_assoc, lift_spec_mul], repeat { rw ←k.to_map.map_mul }, apply f.eq_of_eq k.map_units, repeat { rw f.to_map.map_mul }, rw [sec_spec', ←hx], ac_refl, end @[to_additive] lemma lift_surjective_iff : function.surjective (f.lift hg) ↔ ∀ v : P, ∃ x : M × S, v * g x.2 = g x.1 := begin split, { intros H v, obtain ⟨z, hz⟩ := H v, obtain ⟨x, hx⟩ := f.surj z, use x, rw [←hz, f.eq_mk'_iff_mul_eq.2 hx, lift_mk', mul_assoc, mul_comm _ (g ↑x.2)], erw [is_unit.mul_lift_right_inv (g.mrestrict S) hg, mul_one] }, { intros H v, obtain ⟨x, hx⟩ := H v, use f.mk' x.1 x.2, rw [lift_mk', mul_inv_left hg, mul_comm, ←hx] } end @[to_additive] lemma lift_injective_iff : function.injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := begin split, { intros H x y, split, { exact f.eq_of_eq hg }, { intro h, rw [←f.lift_eq hg, ←f.lift_eq hg] at h, exact H h }}, { intros H z w h, obtain ⟨x, hx⟩ := f.surj z, obtain ⟨y, hy⟩ := f.surj w, rw [←f.mk'_sec z, ←f.mk'_sec w], exact (mul_inv f.map_units).2 ((H _ _).2 $ (mul_inv hg).1 h) } end variables {T : submonoid P} (hy : ∀ y : S, g y ∈ T) {Q : Type*} [comm_monoid Q] (k : localization_map T Q) /-- Given a `comm_monoid` homomorphism `g : M →* P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →* N` and `k : P →* Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a `add_comm_monoid` homomorphism `g : M →+ P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced add_monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →+ N` and `k : P →+ Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) - k (g y)`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def map : N →* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} @[to_additive] lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp, to_additive] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ @[to_additive] lemma map_mk' (x) (y : S) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := begin rw [map, lift_mk', mul_inv_left], { show k.to_map (g x) = k.to_map (g y) * _, rw mul_mk'_eq_mk'_of_mul, exact (k.mk'_mul_cancel_left (g x) ⟨(g y), hy y⟩).symm }, end /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) * u` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) + u` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_spec (z u) : f.map hy k z = u ↔ k.to_map (g (f.sec z).1) = k.to_map (g (f.sec z).2) * u := f.lift_spec (λ y, k.map_units ⟨g y, hy y⟩) _ _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `f.map hy k z * k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `f.map hy k z + k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_right (z) : f.map hy k z * (k.to_map (g (f.sec z).2)) = k.to_map (g (f.sec z).1) := f.lift_mul_right (λ y, k.map_units ⟨g y, hy y⟩) _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `k (g y) * f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `k (g y) + f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_left (z) : k.to_map (g (f.sec z).2) * f.map hy k z = k.to_map (g (f.sec z).1) := by rw [mul_comm, f.map_mul_right] @[simp, to_additive] lemma map_id (z : N) : f.map (λ y, show monoid_hom.id M y ∈ S, from y.2) f z = z := f.lift_id z /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_comp_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := begin ext z, show j.to_map _ * _ = j.to_map (l _) * _, { rw [mul_inv_left, ←mul_assoc, mul_inv_right], show j.to_map _ * j.to_map (l (g _)) = j.to_map (l _) * _, rw [←j.to_map.map_mul, ←j.to_map.map_mul, ←l.map_mul, ←l.map_mul], exact k.comp_eq_of_eq hl j (by rw [k.to_map.map_mul, k.to_map.map_mul, sec_spec', mul_assoc, map_mul_right]) }, end /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl variables {g} /-- If `f : M →* N` and `k : M →* P` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `P`. -/ @[to_additive "If `f : M →+ N` and `k : M →+ R` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `R`."] noncomputable def mul_equiv_of_localizations (k : localization_map S P) : N ≃* P := ⟨f.lift k.map_units, k.lift f.map_units, f.lift_left_inverse, k.lift_left_inverse, monoid_hom.map_mul _⟩ @[to_additive, simp] lemma mul_equiv_of_localizations_apply {k : localization_map S P} {x} : f.mul_equiv_of_localizations k x = f.lift k.map_units x := rfl @[to_additive, simp] lemma mul_equiv_of_localizations_symm_apply {k : localization_map S P} {x} : (f.mul_equiv_of_localizations k).symm x = k.lift f.map_units x := rfl @[to_additive] lemma mul_equiv_of_localizations_symm_eq_mul_equiv_of_localizations {k : localization_map S P} : (k.mul_equiv_of_localizations f).symm = f.mul_equiv_of_localizations k := rfl /-- If `f : M →* N` is a localization map for a submonoid `S` and `k : N ≃* P` is an isomorphism of `comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`. -/ @[to_additive "If `f : M →+ N` is a localization map for a submonoid `S` and `k : N ≃+ P` is an isomorphism of `add_comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`."] def of_mul_equiv_of_localizations (k : N ≃* P) : localization_map S P := (k.to_monoid_hom.comp f.to_map).to_localization_map (λ y, is_unit_comp f k.to_monoid_hom y) (λ v, let ⟨z, hz⟩ := k.to_equiv.surjective v in let ⟨x, hx⟩ := f.surj z in ⟨x, show v * k _ = k _, by rw [←hx, k.map_mul, ←hz]; refl⟩) (λ x y, (k.to_equiv.apply_eq_iff_eq _ _).trans $ f.eq_iff_exists) @[to_additive, simp] lemma of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : (f.of_mul_equiv_of_localizations k).to_map x = k (f.to_map x) := rfl @[to_additive] lemma of_mul_equiv_of_localizations_eq {k : N ≃* P} : (f.of_mul_equiv_of_localizations k).to_map = k.to_monoid_hom.comp f.to_map := rfl @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : k.symm ((f.of_mul_equiv_of_localizations k).to_map x) = f.to_map x := k.symm_apply_apply (f.to_map x) @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply' {k : P ≃* N} (x) : k ((f.of_mul_equiv_of_localizations k.symm).to_map x) = f.to_map x := k.apply_symm_apply (f.to_map x) @[to_additive] lemma of_mul_equiv_of_localizations_eq_iff_eq {k : N ≃* P} {x y} : (f.of_mul_equiv_of_localizations k).to_map x = y ↔ f.to_map x = k.symm y := k.to_equiv.eq_symm_apply.symm @[to_additive] lemma mul_equiv_of_localizations_right_inv (k : localization_map S P) : f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k) = k := to_map_injective $ f.lift_comp k.map_units @[to_additive, simp] lemma mul_equiv_of_localizations_right_inv_apply {k : localization_map S P} {x} : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k)).to_map x = k.to_map x := ext_iff.1 (f.mul_equiv_of_localizations_right_inv k) x @[to_additive] lemma mul_equiv_of_localizations_left_inv (k : N ≃* P) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) = k := mul_equiv.ext $ monoid_hom.ext_iff.1 $ f.lift_of_comp k.to_monoid_hom @[to_additive, simp] lemma mul_equiv_of_localizations_left_inv_apply {k : N ≃* P} (x) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) x = k x := by rw mul_equiv_of_localizations_left_inv @[simp, to_additive] lemma of_mul_equiv_of_localizations_id : f.of_mul_equiv_of_localizations (mul_equiv.refl N) = f := by ext; refl @[to_additive] lemma of_mul_equiv_of_localizations_comp {k : N ≃* P} {j : P ≃* Q} : (f.of_mul_equiv_of_localizations (k.trans j)).to_map = j.to_monoid_hom.comp (f.of_mul_equiv_of_localizations k).to_map := by ext; refl /-- Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`. -/ @[to_additive "Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`."] def of_mul_equiv_of_dom {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : localization_map T N := let H' : S.comap k.to_monoid_hom = T := H ▸ (submonoid.ext' $ T.1.preimage_image_eq k.to_equiv.injective) in (f.to_map.comp k.to_monoid_hom).to_localization_map (λ y, let ⟨z, hz⟩ := f.map_units ⟨k y, H ▸ set.mem_image_of_mem k y.2⟩ in ⟨z, hz⟩) (λ z, let ⟨x, hx⟩ := f.surj z in let ⟨v, hv⟩ := k.to_equiv.surjective x.1 in let ⟨w, hw⟩ := k.to_equiv.surjective x.2 in ⟨(v, ⟨w, H' ▸ show k w ∈ S, from hw.symm ▸ x.2.2⟩), show z * f.to_map (k.to_equiv w) = f.to_map (k.to_equiv v), by erw [hv, hw, hx]; refl⟩) (λ x y, show f.to_map _ = f.to_map _ ↔ _, by erw f.eq_iff_exists; exact ⟨λ ⟨c, hc⟩, let ⟨d, hd⟩ := k.to_equiv.surjective c in ⟨⟨d, H' ▸ show k d ∈ S, from hd.symm ▸ c.2⟩, by erw [←hd, ←k.map_mul, ←k.map_mul] at hc; exact k.to_equiv.injective hc⟩, λ ⟨c, hc⟩, ⟨⟨k c, H ▸ set.mem_image_of_mem k c.2⟩, by erw ←k.map_mul; rw [hc, k.map_mul]; refl⟩⟩) @[to_additive, simp] lemma of_mul_equiv_of_dom_apply {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map x = f.to_map (k x) := rfl @[to_additive] lemma of_mul_equiv_of_dom_eq {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : (f.of_mul_equiv_of_dom H).to_map = f.to_map.comp k.to_monoid_hom := rfl @[to_additive] lemma of_mul_equiv_of_dom_comp_symm {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k.symm x) = f.to_map x := congr_arg f.to_map $ k.apply_symm_apply x @[to_additive] lemma of_mul_equiv_of_dom_comp {k : M ≃* P} (H : T.map k.symm.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k x) = f.to_map x := congr_arg f.to_map $ k.symm_apply_apply x /-- A special case of `f ∘ id = f`, `f` a localization map. -/ @[simp, to_additive "A special case of `f ∘ id = f`, `f` a localization map."] lemma of_mul_equiv_of_dom_id : f.of_mul_equiv_of_dom (show S.map (mul_equiv.refl M).to_monoid_hom = S, from submonoid.ext $ λ x, ⟨λ ⟨y, hy, h⟩, h ▸ hy, λ h, ⟨x, h, rfl⟩⟩) = f := by ext; refl /-- Given localization maps `f : M →* N, k : P →* U` for submonoids `S, T` respectively, an isomorphism `j : M ≃* P` such that `j(S) = T` induces an isomorphism of localizations `N ≃* U`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ U` for submonoids `S, T` respectively, an isomorphism `j : M ≃+ P` such that `j(S) = T` induces an isomorphism of localizations `N ≃+ U`."] noncomputable def mul_equiv_of_mul_equiv (k : localization_map T Q) {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : N ≃* Q := f.mul_equiv_of_localizations $ k.of_mul_equiv_of_dom H @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq_map_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H x = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl @[to_additive] lemma mul_equiv_of_mul_equiv_eq_map {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.mul_equiv_of_mul_equiv k H).to_monoid_hom = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H (f.to_map x) = k.to_map (j x) := f.map_eq (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ @[simp, to_additive] lemma mul_equiv_of_mul_equiv_mk' {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x y) : f.mul_equiv_of_mul_equiv k H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.map_mk' (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ _ @[to_additive, simp] lemma of_mul_equiv_of_mul_equiv_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map x = k.to_map (j x) := ext_iff.1 (f.mul_equiv_of_localizations_right_inv (k.of_mul_equiv_of_dom H)) x @[to_additive] lemma of_mul_equiv_of_mul_equiv {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map = k.to_map.comp j.to_monoid_hom := monoid_hom.ext $ f.of_mul_equiv_of_mul_equiv_apply H end localization_map end submonoid
27a4e9170df6cb32622d0eaa2471d4e00eba8af0
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/tests/lean/1657.lean
ff753d0b0f4ffbb2f270246fbcac035085e537b8
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
742
lean
abbrev natrec_inner {C} (n: Nat) (z: Option C) (s: C -> Option C) : Option C := match n with | 0 => z | n + 1 => (natrec_inner n z s).bind s def natrec_int {C} (n: Option Nat) -- ERROR (z: Option C) (s: C -> Option C) : Option C := n.bind (λn => natrec_inner n z s) @[inlineIfReduce] def foo (xs : List Nat) := match xs with | [] => 0 | _::xs => foo xs + 1 def error : Nat := -- ERROR foo [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] set_option compiler.maxRecInlineIfReduce 32 def ok : Nat := foo [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] @[inline] def foldr (f : Nat → β → β) (acc : β) : Nat → β | 0 => acc | n+1 => foldr f (f n acc) n def toList (r : Nat) : List Nat := foldr (· :: ·) [] r
804bc00cb28168e6c07aac918b2e75d64d20d6b9
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/data/complex/basic.lean
0d038d1a8d02ff29cb512a9fea7bb96b2d35ae45
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,373
lean
/- Copyright (c) 2017 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Mario Carneiro -/ import data.real.sqrt /-! # The complex numbers The complex numbers are modelled as ℝ^2 in the obvious way and it is shown that they form a field of characteristic zero. The result that the complex numbers are algebraically closed, see `field_theory.algebraic_closure`. -/ open_locale big_operators /-! ### Definition and basic arithmmetic -/ /-- Complex numbers consist of two `real`s: a real part `re` and an imaginary part `im`. -/ structure complex : Type := (re : ℝ) (im : ℝ) notation `ℂ` := complex namespace complex noncomputable instance : decidable_eq ℂ := classical.dec_eq _ /-- The equivalence between the complex numbers and `ℝ × ℝ`. -/ def equiv_real_prod : ℂ ≃ (ℝ × ℝ) := { to_fun := λ z, ⟨z.re, z.im⟩, inv_fun := λ p, ⟨p.1, p.2⟩, left_inv := λ ⟨x, y⟩, rfl, right_inv := λ ⟨x, y⟩, rfl } @[simp] theorem equiv_real_prod_apply (z : ℂ) : equiv_real_prod z = (z.re, z.im) := rfl theorem equiv_real_prod_symm_re (x y : ℝ) : (equiv_real_prod.symm (x, y)).re = x := rfl theorem equiv_real_prod_symm_im (x y : ℝ) : (equiv_real_prod.symm (x, y)).im = y := rfl @[simp] theorem eta : ∀ z : ℂ, complex.mk z.re z.im = z | ⟨a, b⟩ := rfl @[ext] theorem ext : ∀ {z w : ℂ}, z.re = w.re → z.im = w.im → z = w | ⟨zr, zi⟩ ⟨_, _⟩ rfl rfl := rfl theorem ext_iff {z w : ℂ} : z = w ↔ z.re = w.re ∧ z.im = w.im := ⟨λ H, by simp [H], and.rec ext⟩ instance : has_coe ℝ ℂ := ⟨λ r, ⟨r, 0⟩⟩ @[simp, norm_cast] lemma of_real_re (r : ℝ) : (r : ℂ).re = r := rfl @[simp, norm_cast] lemma of_real_im (r : ℝ) : (r : ℂ).im = 0 := rfl lemma of_real_def (r : ℝ) : (r : ℂ) = ⟨r, 0⟩ := rfl @[simp, norm_cast] theorem of_real_inj {z w : ℝ} : (z : ℂ) = w ↔ z = w := ⟨congr_arg re, congr_arg _⟩ instance : has_zero ℂ := ⟨(0 : ℝ)⟩ instance : inhabited ℂ := ⟨0⟩ @[simp] lemma zero_re : (0 : ℂ).re = 0 := rfl @[simp] lemma zero_im : (0 : ℂ).im = 0 := rfl @[simp, norm_cast] lemma of_real_zero : ((0 : ℝ) : ℂ) = 0 := rfl @[simp] theorem of_real_eq_zero {z : ℝ} : (z : ℂ) = 0 ↔ z = 0 := of_real_inj theorem of_real_ne_zero {z : ℝ} : (z : ℂ) ≠ 0 ↔ z ≠ 0 := not_congr of_real_eq_zero instance : has_one ℂ := ⟨(1 : ℝ)⟩ @[simp] lemma one_re : (1 : ℂ).re = 1 := rfl @[simp] lemma one_im : (1 : ℂ).im = 0 := rfl @[simp, norm_cast] lemma of_real_one : ((1 : ℝ) : ℂ) = 1 := rfl instance : has_add ℂ := ⟨λ z w, ⟨z.re + w.re, z.im + w.im⟩⟩ @[simp] lemma add_re (z w : ℂ) : (z + w).re = z.re + w.re := rfl @[simp] lemma add_im (z w : ℂ) : (z + w).im = z.im + w.im := rfl @[simp] lemma bit0_re (z : ℂ) : (bit0 z).re = bit0 z.re := rfl @[simp] lemma bit1_re (z : ℂ) : (bit1 z).re = bit1 z.re := rfl @[simp] lemma bit0_im (z : ℂ) : (bit0 z).im = bit0 z.im := eq.refl _ @[simp] lemma bit1_im (z : ℂ) : (bit1 z).im = bit0 z.im := add_zero _ @[simp, norm_cast] lemma of_real_add (r s : ℝ) : ((r + s : ℝ) : ℂ) = r + s := ext_iff.2 $ by simp @[simp, norm_cast] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : ℂ) = bit0 r := ext_iff.2 $ by simp [bit0] @[simp, norm_cast] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : ℂ) = bit1 r := ext_iff.2 $ by simp [bit1] instance : has_neg ℂ := ⟨λ z, ⟨-z.re, -z.im⟩⟩ @[simp] lemma neg_re (z : ℂ) : (-z).re = -z.re := rfl @[simp] lemma neg_im (z : ℂ) : (-z).im = -z.im := rfl @[simp, norm_cast] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : ℂ) = -r := ext_iff.2 $ by simp instance : has_sub ℂ := ⟨λ z w, ⟨z.re - w.re, z.im - w.im⟩⟩ instance : has_mul ℂ := ⟨λ z w, ⟨z.re * w.re - z.im * w.im, z.re * w.im + z.im * w.re⟩⟩ @[simp] lemma mul_re (z w : ℂ) : (z * w).re = z.re * w.re - z.im * w.im := rfl @[simp] lemma mul_im (z w : ℂ) : (z * w).im = z.re * w.im + z.im * w.re := rfl @[simp, norm_cast] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : ℂ) = r * s := ext_iff.2 $ by simp lemma of_real_mul_re (r : ℝ) (z : ℂ) : (↑r * z).re = r * z.re := by simp lemma of_real_mul_im (r : ℝ) (z : ℂ) : (↑r * z).im = r * z.im := by simp lemma of_real_mul' (r : ℝ) (z : ℂ) : (↑r * z) = ⟨r * z.re, r * z.im⟩ := ext (of_real_mul_re _ _) (of_real_mul_im _ _) /-! ### The imaginary unit, `I` -/ /-- The imaginary unit. -/ def I : ℂ := ⟨0, 1⟩ @[simp] lemma I_re : I.re = 0 := rfl @[simp] lemma I_im : I.im = 1 := rfl @[simp] lemma I_mul_I : I * I = -1 := ext_iff.2 $ by simp lemma I_mul (z : ℂ) : I * z = ⟨-z.im, z.re⟩ := ext_iff.2 $ by simp lemma I_ne_zero : (I : ℂ) ≠ 0 := mt (congr_arg im) zero_ne_one.symm lemma mk_eq_add_mul_I (a b : ℝ) : complex.mk a b = a + b * I := ext_iff.2 $ by simp @[simp] lemma re_add_im (z : ℂ) : (z.re : ℂ) + z.im * I = z := ext_iff.2 $ by simp /-! ### Commutative ring instance and lemmas -/ instance : comm_ring ℂ := by refine_struct { zero := (0 : ℂ), add := (+), neg := has_neg.neg, sub := has_sub.sub, one := 1, mul := (*), nsmul := @nsmul_rec _ ⟨(0)⟩ ⟨(+)⟩, npow := @npow_rec _ ⟨(1)⟩ ⟨(*)⟩, gsmul := @gsmul_rec _ ⟨(0)⟩ ⟨(+)⟩ ⟨has_neg.neg⟩ }; intros; try { refl }; apply ext_iff.2; split; simp; {ring1 <|> ring_nf} /-- This shortcut instance ensures we do not find `ring` via the noncomputable `complex.field` instance. -/ instance : ring ℂ := by apply_instance instance re.is_add_group_hom : is_add_group_hom complex.re := { map_add := complex.add_re } instance im.is_add_group_hom : is_add_group_hom complex.im := { map_add := complex.add_im } @[simp] lemma I_pow_bit0 (n : ℕ) : I ^ (bit0 n) = (-1) ^ n := by rw [pow_bit0', I_mul_I] @[simp] lemma I_pow_bit1 (n : ℕ) : I ^ (bit1 n) = (-1) ^ n * I := by rw [pow_bit1', I_mul_I] /-! ### Complex conjugation -/ /-- The complex conjugate. -/ def conj : ℂ →+* ℂ := begin refine_struct { to_fun := λ z : ℂ, (⟨z.re, -z.im⟩ : ℂ), .. }; { intros, ext; simp [add_comm], }, end @[simp] lemma conj_re (z : ℂ) : (conj z).re = z.re := rfl @[simp] lemma conj_im (z : ℂ) : (conj z).im = -z.im := rfl @[simp] lemma conj_of_real (r : ℝ) : conj r = r := ext_iff.2 $ by simp [conj] @[simp] lemma conj_I : conj I = -I := ext_iff.2 $ by simp @[simp] lemma conj_bit0 (z : ℂ) : conj (bit0 z) = bit0 (conj z) := ext_iff.2 $ by simp [bit0] @[simp] lemma conj_bit1 (z : ℂ) : conj (bit1 z) = bit1 (conj z) := ext_iff.2 $ by simp [bit0] @[simp] lemma conj_neg_I : conj (-I) = I := ext_iff.2 $ by simp @[simp] lemma conj_conj (z : ℂ) : conj (conj z) = z := ext_iff.2 $ by simp lemma conj_involutive : function.involutive conj := conj_conj lemma conj_bijective : function.bijective conj := conj_involutive.bijective lemma conj_inj {z w : ℂ} : conj z = conj w ↔ z = w := conj_bijective.1.eq_iff @[simp] lemma conj_eq_zero {z : ℂ} : conj z = 0 ↔ z = 0 := by simpa using @conj_inj z 0 lemma eq_conj_iff_real {z : ℂ} : conj z = z ↔ ∃ r : ℝ, z = r := ⟨λ h, ⟨z.re, ext rfl $ eq_zero_of_neg_eq (congr_arg im h)⟩, λ ⟨h, e⟩, by rw [e, conj_of_real]⟩ lemma eq_conj_iff_re {z : ℂ} : conj z = z ↔ (z.re : ℂ) = z := eq_conj_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp, λ h, ⟨_, h.symm⟩⟩ lemma conj_sub (z z': ℂ) : conj (z - z') = conj z - conj z' := conj.map_sub z z' lemma conj_one : conj 1 = 1 := by rw conj.map_one lemma eq_conj_iff_im {z : ℂ} : conj z = z ↔ z.im = 0 := ⟨λ h, add_self_eq_zero.mp (neg_eq_iff_add_eq_zero.mp (congr_arg im h)), λ h, ext rfl (neg_eq_iff_add_eq_zero.mpr (add_self_eq_zero.mpr h))⟩ instance : star_ring ℂ := { star := λ z, conj z, star_involutive := λ z, by simp, star_mul := λ r s, by { ext; simp [mul_comm], }, star_add := by simp, } /-! ### Norm squared -/ /-- The norm squared function. -/ @[pp_nodot] def norm_sq : monoid_with_zero_hom ℂ ℝ := { to_fun := λ z, z.re * z.re + z.im * z.im, map_zero' := by simp, map_one' := by simp, map_mul' := λ z w, by { dsimp, ring } } lemma norm_sq_apply (z : ℂ) : norm_sq z = z.re * z.re + z.im * z.im := rfl @[simp] lemma norm_sq_of_real (r : ℝ) : norm_sq r = r * r := by simp [norm_sq] lemma norm_sq_eq_conj_mul_self {z : ℂ} : (norm_sq z : ℂ) = conj z * z := by { ext; simp [norm_sq, mul_comm], } @[simp] lemma norm_sq_zero : norm_sq 0 = 0 := norm_sq.map_zero @[simp] lemma norm_sq_one : norm_sq 1 = 1 := norm_sq.map_one @[simp] lemma norm_sq_I : norm_sq I = 1 := by simp [norm_sq] lemma norm_sq_nonneg (z : ℂ) : 0 ≤ norm_sq z := add_nonneg (mul_self_nonneg _) (mul_self_nonneg _) lemma norm_sq_eq_zero {z : ℂ} : norm_sq z = 0 ↔ z = 0 := ⟨λ h, ext (eq_zero_of_mul_self_add_mul_self_eq_zero h) (eq_zero_of_mul_self_add_mul_self_eq_zero $ (add_comm _ _).trans h), λ h, h.symm ▸ norm_sq_zero⟩ @[simp] lemma norm_sq_pos {z : ℂ} : 0 < norm_sq z ↔ z ≠ 0 := (norm_sq_nonneg z).lt_iff_ne.trans $ not_congr (eq_comm.trans norm_sq_eq_zero) @[simp] lemma norm_sq_neg (z : ℂ) : norm_sq (-z) = norm_sq z := by simp [norm_sq] @[simp] lemma norm_sq_conj (z : ℂ) : norm_sq (conj z) = norm_sq z := by simp [norm_sq] lemma norm_sq_mul (z w : ℂ) : norm_sq (z * w) = norm_sq z * norm_sq w := norm_sq.map_mul z w lemma norm_sq_add (z w : ℂ) : norm_sq (z + w) = norm_sq z + norm_sq w + 2 * (z * conj w).re := by dsimp [norm_sq]; ring lemma re_sq_le_norm_sq (z : ℂ) : z.re * z.re ≤ norm_sq z := le_add_of_nonneg_right (mul_self_nonneg _) lemma im_sq_le_norm_sq (z : ℂ) : z.im * z.im ≤ norm_sq z := le_add_of_nonneg_left (mul_self_nonneg _) theorem mul_conj (z : ℂ) : z * conj z = norm_sq z := ext_iff.2 $ by simp [norm_sq, mul_comm, sub_eq_neg_add, add_comm] theorem add_conj (z : ℂ) : z + conj z = (2 * z.re : ℝ) := ext_iff.2 $ by simp [two_mul] /-- The coercion `ℝ → ℂ` as a `ring_hom`. -/ def of_real : ℝ →+* ℂ := ⟨coe, of_real_one, of_real_mul, of_real_zero, of_real_add⟩ @[simp] lemma of_real_eq_coe (r : ℝ) : of_real r = r := rfl @[simp] lemma I_sq : I ^ 2 = -1 := by rw [sq, I_mul_I] @[simp] lemma sub_re (z w : ℂ) : (z - w).re = z.re - w.re := rfl @[simp] lemma sub_im (z w : ℂ) : (z - w).im = z.im - w.im := rfl @[simp, norm_cast] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : ℂ) = r - s := ext_iff.2 $ by simp @[simp, norm_cast] lemma of_real_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : ℂ) = r ^ n := by induction n; simp [*, of_real_mul, pow_succ] theorem sub_conj (z : ℂ) : z - conj z = (2 * z.im : ℝ) * I := ext_iff.2 $ by simp [two_mul, sub_eq_add_neg] lemma norm_sq_sub (z w : ℂ) : norm_sq (z - w) = norm_sq z + norm_sq w - 2 * (z * conj w).re := by rw [sub_eq_add_neg, norm_sq_add]; simp [-mul_re, add_comm, add_left_comm, sub_eq_add_neg] /-! ### Inversion -/ noncomputable instance : has_inv ℂ := ⟨λ z, conj z * ((norm_sq z)⁻¹:ℝ)⟩ theorem inv_def (z : ℂ) : z⁻¹ = conj z * ((norm_sq z)⁻¹:ℝ) := rfl @[simp] lemma inv_re (z : ℂ) : (z⁻¹).re = z.re / norm_sq z := by simp [inv_def, division_def] @[simp] lemma inv_im (z : ℂ) : (z⁻¹).im = -z.im / norm_sq z := by simp [inv_def, division_def] @[simp, norm_cast] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : ℂ) = r⁻¹ := ext_iff.2 $ by simp protected lemma inv_zero : (0⁻¹ : ℂ) = 0 := by rw [← of_real_zero, ← of_real_inv, inv_zero] protected theorem mul_inv_cancel {z : ℂ} (h : z ≠ 0) : z * z⁻¹ = 1 := by rw [inv_def, ← mul_assoc, mul_conj, ← of_real_mul, mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one] /-! ### Field instance and lemmas -/ noncomputable instance : field ℂ := { inv := has_inv.inv, exists_pair_ne := ⟨0, 1, mt (congr_arg re) zero_ne_one⟩, mul_inv_cancel := @complex.mul_inv_cancel, inv_zero := complex.inv_zero, ..complex.comm_ring } @[simp] lemma I_fpow_bit0 (n : ℤ) : I ^ (bit0 n) = (-1) ^ n := by rw [fpow_bit0', I_mul_I] @[simp] lemma I_fpow_bit1 (n : ℤ) : I ^ (bit1 n) = (-1) ^ n * I := by rw [fpow_bit1', I_mul_I] lemma div_re (z w : ℂ) : (z / w).re = z.re * w.re / norm_sq w + z.im * w.im / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg] lemma div_im (z w : ℂ) : (z / w).im = z.im * w.re / norm_sq w - z.re * w.im / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm] @[simp, norm_cast] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : ℂ) = r / s := of_real.map_div r s @[simp, norm_cast] lemma of_real_fpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : ℂ) = (r : ℂ) ^ n := of_real.map_fpow r n @[simp] lemma div_I (z : ℂ) : z / I = -(z * I) := (div_eq_iff_mul_eq I_ne_zero).2 $ by simp [mul_assoc] @[simp] lemma inv_I : I⁻¹ = -I := by simp [inv_eq_one_div] @[simp] lemma norm_sq_inv (z : ℂ) : norm_sq z⁻¹ = (norm_sq z)⁻¹ := norm_sq.map_inv' z @[simp] lemma norm_sq_div (z w : ℂ) : norm_sq (z / w) = norm_sq z / norm_sq w := norm_sq.map_div z w /-! ### Cast lemmas -/ @[simp, norm_cast] theorem of_real_nat_cast (n : ℕ) : ((n : ℝ) : ℂ) = n := of_real.map_nat_cast n @[simp, norm_cast] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n := by rw [← of_real_nat_cast, of_real_re] @[simp, norm_cast] lemma nat_cast_im (n : ℕ) : (n : ℂ).im = 0 := by rw [← of_real_nat_cast, of_real_im] @[simp, norm_cast] theorem of_real_int_cast (n : ℤ) : ((n : ℝ) : ℂ) = n := of_real.map_int_cast n @[simp, norm_cast] lemma int_cast_re (n : ℤ) : (n : ℂ).re = n := by rw [← of_real_int_cast, of_real_re] @[simp, norm_cast] lemma int_cast_im (n : ℤ) : (n : ℂ).im = 0 := by rw [← of_real_int_cast, of_real_im] @[simp, norm_cast] theorem of_real_rat_cast (n : ℚ) : ((n : ℝ) : ℂ) = n := of_real.map_rat_cast n @[simp, norm_cast] lemma rat_cast_re (q : ℚ) : (q : ℂ).re = q := by rw [← of_real_rat_cast, of_real_re] @[simp, norm_cast] lemma rat_cast_im (q : ℚ) : (q : ℂ).im = 0 := by rw [← of_real_rat_cast, of_real_im] /-! ### Characteristic zero -/ instance char_zero_complex : char_zero ℂ := char_zero_of_inj_zero $ λ n h, by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h /-- A complex number `z` plus its conjugate `conj z` is `2` times its real part. -/ theorem re_eq_add_conj (z : ℂ) : (z.re : ℂ) = (z + conj z) / 2 := by simp only [add_conj, of_real_mul, of_real_one, of_real_bit0, mul_div_cancel_left (z.re:ℂ) two_ne_zero'] /-- A complex number `z` minus its conjugate `conj z` is `2i` times its imaginary part. -/ theorem im_eq_sub_conj (z : ℂ) : (z.im : ℂ) = (z - conj(z))/(2 * I) := by simp only [sub_conj, of_real_mul, of_real_one, of_real_bit0, mul_right_comm, mul_div_cancel_left _ (mul_ne_zero two_ne_zero' I_ne_zero : 2 * I ≠ 0)] /-! ### Absolute value -/ /-- The complex absolute value function, defined as the square root of the norm squared. -/ @[pp_nodot] noncomputable def abs (z : ℂ) : ℝ := (norm_sq z).sqrt local notation `abs'` := _root_.abs @[simp, norm_cast] lemma abs_of_real (r : ℝ) : abs r = abs' r := by simp [abs, norm_sq_of_real, real.sqrt_mul_self_eq_abs] lemma abs_of_nonneg {r : ℝ} (h : 0 ≤ r) : abs r = r := (abs_of_real _).trans (abs_of_nonneg h) lemma abs_of_nat (n : ℕ) : complex.abs n = n := calc complex.abs n = complex.abs (n:ℝ) : by rw [of_real_nat_cast] ... = _ : abs_of_nonneg (nat.cast_nonneg n) lemma mul_self_abs (z : ℂ) : abs z * abs z = norm_sq z := real.mul_self_sqrt (norm_sq_nonneg _) @[simp] lemma abs_zero : abs 0 = 0 := by simp [abs] @[simp] lemma abs_one : abs 1 = 1 := by simp [abs] @[simp] lemma abs_I : abs I = 1 := by simp [abs] @[simp] lemma abs_two : abs 2 = 2 := calc abs 2 = abs (2 : ℝ) : by rw [of_real_bit0, of_real_one] ... = (2 : ℝ) : abs_of_nonneg (by norm_num) lemma abs_nonneg (z : ℂ) : 0 ≤ abs z := real.sqrt_nonneg _ @[simp] lemma abs_eq_zero {z : ℂ} : abs z = 0 ↔ z = 0 := (real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero lemma abs_ne_zero {z : ℂ} : abs z ≠ 0 ↔ z ≠ 0 := not_congr abs_eq_zero @[simp] lemma abs_conj (z : ℂ) : abs (conj z) = abs z := by simp [abs] @[simp] lemma abs_mul (z w : ℂ) : abs (z * w) = abs z * abs w := by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl lemma abs_re_le_abs (z : ℂ) : abs' z.re ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.re) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply re_sq_le_norm_sq lemma abs_im_le_abs (z : ℂ) : abs' z.im ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.im) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply im_sq_le_norm_sq lemma re_le_abs (z : ℂ) : z.re ≤ abs z := (abs_le.1 (abs_re_le_abs _)).2 lemma im_le_abs (z : ℂ) : z.im ≤ abs z := (abs_le.1 (abs_im_le_abs _)).2 /-- The **triangle inequality** for complex numbers. -/ lemma abs_add (z w : ℂ) : abs (z + w) ≤ abs z + abs w := (mul_self_le_mul_self_iff (abs_nonneg _) (add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $ begin rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs, add_right_comm, norm_sq_add, add_le_add_iff_left, mul_assoc, mul_le_mul_left (@zero_lt_two ℝ _ _)], simpa [-mul_re] using re_le_abs (z * conj w) end instance : is_absolute_value abs := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value @[simp] lemma abs_abs (z : ℂ) : abs' (abs z) = abs z := _root_.abs_of_nonneg (abs_nonneg _) @[simp] lemma abs_pos {z : ℂ} : 0 < abs z ↔ z ≠ 0 := abv_pos abs @[simp] lemma abs_neg : ∀ z, abs (-z) = abs z := abv_neg abs lemma abs_sub_comm : ∀ z w, abs (z - w) = abs (w - z) := abv_sub abs lemma abs_sub_le : ∀ a b c, abs (a - c) ≤ abs (a - b) + abs (b - c) := abv_sub_le abs @[simp] theorem abs_inv : ∀ z, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs @[simp] theorem abs_div : ∀ z w, abs (z / w) = abs z / abs w := abv_div abs lemma abs_abs_sub_le_abs_sub : ∀ z w, abs' (abs z - abs w) ≤ abs (z - w) := abs_abv_sub_le_abv_sub abs lemma abs_le_abs_re_add_abs_im (z : ℂ) : abs z ≤ abs' z.re + abs' z.im := by simpa [re_add_im] using abs_add z.re (z.im * I) lemma abs_re_div_abs_le_one (z : ℂ) : abs' (z.re / z.abs) ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else by { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_re_le_abs] } lemma abs_im_div_abs_le_one (z : ℂ) : abs' (z.im / z.abs) ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else by { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_im_le_abs] } @[simp, norm_cast] lemma abs_cast_nat (n : ℕ) : abs (n : ℂ) = n := by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)] @[simp, norm_cast] lemma int_cast_abs (n : ℤ) : ↑(abs' n) = abs n := by rw [← of_real_int_cast, abs_of_real, int.cast_abs] lemma norm_sq_eq_abs (x : ℂ) : norm_sq x = abs x ^ 2 := by rw [abs, sq, real.mul_self_sqrt (norm_sq_nonneg _)] /-- We put a partial order on ℂ so that `z ≤ w` exactly if `w - z` is real and nonnegative. Complex numbers with different imaginary parts are incomparable. -/ def complex_order : partial_order ℂ := { le := λ z w, ∃ x : ℝ, 0 ≤ x ∧ w = z + x, le_refl := λ x, ⟨0, by simp⟩, le_trans := λ x y z h₁ h₂, begin obtain ⟨w₁, l₁, rfl⟩ := h₁, obtain ⟨w₂, l₂, rfl⟩ := h₂, refine ⟨w₁ + w₂, _, _⟩, { linarith, }, { simp [add_assoc], }, end, le_antisymm := λ z w h₁ h₂, begin obtain ⟨w₁, l₁, rfl⟩ := h₁, obtain ⟨w₂, l₂, e⟩ := h₂, have h₃ : w₁ + w₂ = 0, { symmetry, rw add_assoc at e, apply of_real_inj.mp, apply add_left_cancel, convert e; simp, }, have h₄ : w₁ = 0, linarith, simp [h₄], end, } localized "attribute [instance] complex_order" in complex_order section complex_order open_locale complex_order lemma le_def {z w : ℂ} : z ≤ w ↔ ∃ x : ℝ, 0 ≤ x ∧ w = z + x := iff.refl _ lemma lt_def {z w : ℂ} : z < w ↔ ∃ x : ℝ, 0 < x ∧ w = z + x := begin rw [lt_iff_le_not_le], fsplit, { rintro ⟨⟨x, l, rfl⟩, h⟩, by_cases hx : x = 0, { simpa [hx] using h }, { replace l : 0 < x := l.lt_of_ne (ne.symm hx), exact ⟨x, l, rfl⟩, } }, { rintro ⟨x, l, rfl⟩, fsplit, { exact ⟨x, l.le, rfl⟩, }, { rintro ⟨x', l', e⟩, rw [add_assoc] at e, replace e := add_left_cancel (by { convert e, simp }), norm_cast at e, linarith, } } end @[simp, norm_cast] lemma real_le_real {x y : ℝ} : (x : ℂ) ≤ (y : ℂ) ↔ x ≤ y := begin rw [le_def], fsplit, { rintro ⟨r, l, e⟩, norm_cast at e, subst e, exact le_add_of_nonneg_right l, }, { intro h, exact ⟨y - x, sub_nonneg.mpr h, (by simp)⟩, }, end @[simp, norm_cast] lemma real_lt_real {x y : ℝ} : (x : ℂ) < (y : ℂ) ↔ x < y := begin rw [lt_def], fsplit, { rintro ⟨r, l, e⟩, norm_cast at e, subst e, exact lt_add_of_pos_right x l, }, { intro h, exact ⟨y - x, sub_pos.mpr h, (by simp)⟩, }, end @[simp, norm_cast] lemma zero_le_real {x : ℝ} : (0 : ℂ) ≤ (x : ℂ) ↔ 0 ≤ x := real_le_real @[simp, norm_cast] lemma zero_lt_real {x : ℝ} : (0 : ℂ) < (x : ℂ) ↔ 0 < x := real_lt_real /-- With `z ≤ w` iff `w - z` is real and nonnegative, `ℂ` is an ordered ring. -/ def complex_ordered_comm_ring : ordered_comm_ring ℂ := { zero_le_one := ⟨1, zero_le_one, by simp⟩, add_le_add_left := λ w z h y, begin obtain ⟨x, l, rfl⟩ := h, exact ⟨x, l, by simp [add_assoc]⟩, end, mul_pos := λ z w hz hw, begin obtain ⟨zx, lz, rfl⟩ := lt_def.mp hz, obtain ⟨wx, lw, rfl⟩ := lt_def.mp hw, norm_cast, simp only [mul_pos, lz, lw, zero_add], end, le_of_add_le_add_left := λ u v z h, begin obtain ⟨x, l, e⟩ := h, rw add_assoc at e, exact ⟨x, l, add_left_cancel e⟩, end, mul_lt_mul_of_pos_left := λ u v z h₁ h₂, begin obtain ⟨x₁, l₁, rfl⟩ := lt_def.mp h₁, obtain ⟨x₂, l₂, rfl⟩ := lt_def.mp h₂, simp only [mul_add, zero_add], exact lt_def.mpr ⟨x₂ * x₁, mul_pos l₂ l₁, (by norm_cast)⟩, end, mul_lt_mul_of_pos_right := λ u v z h₁ h₂, begin obtain ⟨x₁, l₁, rfl⟩ := lt_def.mp h₁, obtain ⟨x₂, l₂, rfl⟩ := lt_def.mp h₂, simp only [add_mul, zero_add], exact lt_def.mpr ⟨x₁ * x₂, mul_pos l₁ l₂, (by norm_cast)⟩, end, -- we need more instances here because comm_ring doesn't have zero_add et al as fields, -- they are derived as lemmas ..(by apply_instance : partial_order ℂ), ..(by apply_instance : comm_ring ℂ), ..(by apply_instance : comm_semiring ℂ), ..(by apply_instance : add_cancel_monoid ℂ) } localized "attribute [instance] complex_ordered_comm_ring" in complex_order /-- With `z ≤ w` iff `w - z` is real and nonnegative, `ℂ` is a star ordered ring. (That is, an ordered ring in which every element of the form `star z * z` is nonnegative.) In fact, the nonnegative elements are precisely those of this form. This hold in any `C^*`-algebra, e.g. `ℂ`, but we don't yet have `C^*`-algebras in mathlib. -/ def complex_star_ordered_ring : star_ordered_ring ℂ := { star_mul_self_nonneg := λ z, begin refine ⟨z.abs^2, pow_nonneg (abs_nonneg z) 2, _⟩, simp only [has_star.star, of_real_pow, zero_add], norm_cast, rw [←norm_sq_eq_abs, norm_sq_eq_conj_mul_self], end, } localized "attribute [instance] complex_star_ordered_ring" in complex_order end complex_order /-! ### Cauchy sequences -/ theorem is_cau_seq_re (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).re) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij) theorem is_cau_seq_im (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).im) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij) /-- The real part of a complex Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_re (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_re f⟩ /-- The imaginary part of a complex Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_im (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_im f⟩ lemma is_cau_seq_abs {f : ℕ → ℂ} (hf : is_cau_seq abs f) : is_cau_seq abs' (abs ∘ f) := λ ε ε0, let ⟨i, hi⟩ := hf ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩ /-- The limit of a Cauchy sequence of complex numbers. -/ noncomputable def lim_aux (f : cau_seq ℂ abs) : ℂ := ⟨cau_seq.lim (cau_seq_re f), cau_seq.lim (cau_seq_im f)⟩ theorem equiv_lim_aux (f : cau_seq ℂ abs) : f ≈ cau_seq.const abs (lim_aux f) := λ ε ε0, (exists_forall_ge_and (cau_seq.equiv_lim ⟨_, is_cau_seq_re f⟩ _ (half_pos ε0)) (cau_seq.equiv_lim ⟨_, is_cau_seq_im f⟩ _ (half_pos ε0))).imp $ λ i H j ij, begin cases H _ ij with H₁ H₂, apply lt_of_le_of_lt (abs_le_abs_re_add_abs_im _), dsimp [lim_aux] at *, have := add_lt_add H₁ H₂, rwa add_halves at this, end noncomputable instance : cau_seq.is_complete ℂ abs := ⟨λ f, ⟨lim_aux f, equiv_lim_aux f⟩⟩ open cau_seq lemma lim_eq_lim_im_add_lim_re (f : cau_seq ℂ abs) : lim f = ↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I := lim_eq_of_equiv_const $ calc f ≈ _ : equiv_lim_aux f ... = cau_seq.const abs (↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I) : cau_seq.ext (λ _, complex.ext (by simp [lim_aux, cau_seq_re]) (by simp [lim_aux, cau_seq_im])) lemma lim_re (f : cau_seq ℂ abs) : lim (cau_seq_re f) = (lim f).re := by rw [lim_eq_lim_im_add_lim_re]; simp lemma lim_im (f : cau_seq ℂ abs) : lim (cau_seq_im f) = (lim f).im := by rw [lim_eq_lim_im_add_lim_re]; simp lemma is_cau_seq_conj (f : cau_seq ℂ abs) : is_cau_seq abs (λ n, conj (f n)) := λ ε ε0, let ⟨i, hi⟩ := f.2 ε ε0 in ⟨i, λ j hj, by rw [← conj.map_sub, abs_conj]; exact hi j hj⟩ /-- The complex conjugate of a complex Cauchy sequence, as a complex Cauchy sequence. -/ noncomputable def cau_seq_conj (f : cau_seq ℂ abs) : cau_seq ℂ abs := ⟨_, is_cau_seq_conj f⟩ lemma lim_conj (f : cau_seq ℂ abs) : lim (cau_seq_conj f) = conj (lim f) := complex.ext (by simp [cau_seq_conj, (lim_re _).symm, cau_seq_re]) (by simp [cau_seq_conj, (lim_im _).symm, cau_seq_im, (lim_neg _).symm]; refl) /-- The absolute value of a complex Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_abs (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_abs f.2⟩ lemma lim_abs (f : cau_seq ℂ abs) : lim (cau_seq_abs f) = abs (lim f) := lim_eq_of_equiv_const (λ ε ε0, let ⟨i, hi⟩ := equiv_lim f ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩) @[simp, norm_cast] lemma of_real_prod {α : Type*} (s : finset α) (f : α → ℝ) : ((∏ i in s, f i : ℝ) : ℂ) = ∏ i in s, (f i : ℂ) := ring_hom.map_prod of_real _ _ @[simp, norm_cast] lemma of_real_sum {α : Type*} (s : finset α) (f : α → ℝ) : ((∑ i in s, f i : ℝ) : ℂ) = ∑ i in s, (f i : ℂ) := ring_hom.map_sum of_real _ _ end complex
e795e8feab94b909c87bd7516d35209c4a925073
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/pow/nnreal.lean
680cad323280691d61ed731232e475fc6e901893
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
31,023
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import analysis.special_functions.pow.real /-! # Power function on `ℝ≥0` and `ℝ≥0∞` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We construct the power functions `x ^ y` where * `x` is a nonnegative real number and `y` is a real number; * `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable theory open_locale classical real nnreal ennreal big_operators complex_conjugate open finset set namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := nnreal.eq $ real.rpow_zero _ @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := begin rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero], exact real.rpow_eq_zero_iff_of_nonneg x.2 end @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := nnreal.eq $ real.zero_rpow h @[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := nnreal.eq $ real.rpow_one _ @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := nnreal.eq $ real.one_rpow _ lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add (pos_iff_ne_zero.2 hx) _ _ lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add' x.2 h lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq $ real.rpow_mul x.2 y z lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := nnreal.eq $ real.rpow_neg x.2 _ lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub (pos_iff_ne_zero.2 hx) y z lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub' x.2 h lemma rpow_inv_rpow_self {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ (1 / y) = x := by field_simp [← rpow_mul] lemma rpow_self_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ (1 / y)) ^ y = x := by field_simp [← rpow_mul] lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := nnreal.eq $ real.inv_rpow x.2 y lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq $ real.div_rpow x.2 y.2 z lemma sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1/(2:ℝ)) := begin refine nnreal.eq _, push_cast, exact real.sqrt_eq_rpow x.1, end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n @[simp] lemma rpow_two (x : ℝ≥0) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z := nnreal.eq $ real.mul_rpow x.2 y.2 lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := real.rpow_le_rpow x.2 h₁ h₂ lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff x.2 y.2 hz lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff x.2 y.2 hz lemma le_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne'] lemma rpow_one_div_le_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z := by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne'] lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z := real.rpow_lt_rpow_of_exponent_lt hx hyz lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_le hx hyz lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz lemma rpow_pos {p : ℝ} {x : ℝ≥0} (hx_pos : 0 < x) : 0 < x^p := begin have rpow_pos_of_nonneg : ∀ {p : ℝ}, 0 < p → 0 < x^p, { intros p hp_pos, rw ←zero_rpow hp_pos.ne', exact rpow_lt_rpow hx_pos hp_pos }, rcases lt_trichotomy 0 p with hp_pos|rfl|hp_neg, { exact rpow_pos_of_nonneg hp_pos }, { simp only [zero_lt_one, rpow_zero] }, { rw [←neg_neg p, rpow_neg, inv_pos], exact rpow_pos_of_nonneg (neg_pos.mpr hp_neg) }, end lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one (coe_nonneg x) hx1 hz lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := real.rpow_le_one x.2 hx2 hz lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz lemma rpow_le_self_of_le_one {x : ℝ≥0} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin rcases eq_bot_or_bot_lt x with rfl | (h : 0 < x), { have : z ≠ 0 := by linarith, simp [this] }, nth_rewrite 1 ←nnreal.rpow_one x, exact nnreal.rpow_le_rpow_of_exponent_ge h hx h_one_le, end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0, y^x) := λ y z hyz, by simpa only [rpow_inv_rpow_self hx] using congr_arg (λ y, y ^ (1 / x)) hyz lemma rpow_eq_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y := (rpow_left_injective hz).eq_iff lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ lemma eq_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x = y ^ (1 / z) ↔ x ^ z = y := by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz] lemma rpow_one_div_eq_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ (1 / z) = y ↔ x = y ^ z := by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz] lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn } lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn } lemma _root_.real.to_nnreal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x ^ y) = (real.to_nnreal x) ^ y := begin nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_rpow, real.to_nnreal_coe], end end nnreal namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞ | (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none y := if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : has_pow ℝ≥0∞ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] lemma rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 := by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] } lemma top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, asymm h, ne_of_gt h], end @[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, ne_of_gt h], end lemma zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := begin rcases lt_trichotomy 0 y with H|rfl|H, { simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] }, { simp [lt_irrefl] }, { simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] } end @[simp] lemma zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * 0 ^ y = 0 ^ y := by { rw zero_rpow_def, split_ifs, exacts [zero_mul _, one_mul _, top_mul_top] } @[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin rw [← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h] end @[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin by_cases hx : x = 0, { rcases le_iff_eq_or_lt.1 h with H|H, { simp [hx, H.symm] }, { simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } }, { exact coe_rpow_of_ne_zero hx _ } end lemma coe_rpow_def (x : ℝ≥0) (y : ℝ) : (x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) := rfl @[simp] lemma rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x := begin cases x, { exact dif_pos zero_lt_one }, { change ite _ _ _ = _, simp only [nnreal.rpow_one, some_eq_coe, ite_eq_right_iff, top_ne_coe, and_imp], exact λ _, zero_le_one.not_lt } end @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 := by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp } @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end @[simp] lemma rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end lemma rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := by simp [rpow_eq_top_iff, hy, asymm hy] lemma rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := begin rw ennreal.rpow_eq_top_iff, intro h, cases h, { exfalso, rw lt_iff_not_ge at h, exact h.right hy0, }, { exact h.left, }, end lemma rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (ennreal.rpow_eq_top_of_nonneg x hy0) h lemma rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := lt_top_iff_ne_top.mpr (ennreal.rpow_ne_top_of_nonneg hy0 h) lemma rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := begin cases x, { exact (h'x rfl).elim }, have : x ≠ 0 := λ h, by simpa [h] using hx, simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this] end lemma rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] }, { have A : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } } end lemma rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv] lemma rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { have : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } } end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0∞) (n : ℕ) : x ^ (n : ℝ) = x ^ n := begin cases x, { cases n; simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] }, { simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] } end @[simp] lemma rpow_two (x : ℝ≥0∞) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma mul_rpow_eq_ite (x y : ℝ≥0∞) (z : ℝ) : (x * y) ^ z = if (x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0) ∧ z < 0 then ⊤ else x ^ z * y ^ z := begin rcases eq_or_ne z 0 with rfl|hz, { simp }, replace hz := hz.lt_or_lt, wlog hxy : x ≤ y, { convert this y x z hz (le_of_not_le hxy) using 2; simp only [mul_comm, and_comm, or_comm], }, rcases eq_or_ne x 0 with rfl|hx0, { induction y using with_top.rec_top_coe; cases hz with hz hz; simp [*, hz.not_lt] }, rcases eq_or_ne y 0 with rfl|hy0, { exact (hx0 (bot_unique hxy)).elim }, induction x using with_top.rec_top_coe, { cases hz with hz hz; simp [hz, top_unique hxy] }, induction y using with_top.rec_top_coe, { cases hz with hz hz; simp * }, simp only [*, false_and, and_false, false_or, if_false], norm_cast at *, rw [coe_rpow_of_ne_zero (mul_ne_zero hx0 hy0), nnreal.mul_rpow] end lemma mul_rpow_of_ne_top {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x^z * y^z := by simp [*, mul_rpow_eq_ite] @[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ℝ≥0∞) * y) ^ z = x^z * y^z := mul_rpow_of_ne_top coe_ne_top coe_ne_top z lemma mul_rpow_of_ne_zero {x y : ℝ≥0∞} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := by simp [*, mul_rpow_eq_ite] lemma mul_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := by simp [hz.not_lt, mul_rpow_eq_ite] lemma inv_rpow (x : ℝ≥0∞) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := begin rcases eq_or_ne y 0 with rfl|hy, { simp only [rpow_zero, inv_one] }, replace hy := hy.lt_or_lt, rcases eq_or_ne x 0 with rfl|h0, { cases hy; simp * }, rcases eq_or_ne x ⊤ with rfl|h_top, { cases hy; simp * }, apply ennreal.eq_inv_of_mul_eq_one_left, rw [← mul_rpow_of_ne_zero (ennreal.inv_ne_zero.2 h_top) h0, ennreal.inv_mul_cancel h0 h_top, one_rpow] end lemma div_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x / y) ^ z = x ^ z / y ^ z := by rw [div_eq_mul_inv, mul_rpow_of_nonneg _ _ hz, inv_rpow, div_eq_mul_inv] lemma strict_mono_rpow_of_pos {z : ℝ} (h : 0 < z) : strict_mono (λ x : ℝ≥0∞, x ^ z) := begin intros x y hxy, lift x to ℝ≥0 using ne_top_of_lt hxy, rcases eq_or_ne y ∞ with rfl|hy, { simp only [top_rpow_of_pos h, coe_rpow_of_nonneg _ h.le, coe_lt_top] }, { lift y to ℝ≥0 using hy, simp only [coe_rpow_of_nonneg _ h.le, nnreal.rpow_lt_rpow (coe_lt_coe.1 hxy) h, coe_lt_coe] } end lemma monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : monotone (λ x : ℝ≥0∞, x ^ z) := h.eq_or_lt.elim (λ h0, h0 ▸ by simp only [rpow_zero, monotone_const]) (λ h0, (strict_mono_rpow_of_pos h0).monotone) /-- Bundles `λ x : ℝ≥0∞, x ^ y` into an order isomorphism when `y : ℝ` is positive, where the inverse is `λ x : ℝ≥0∞, x ^ (1 / y)`. -/ @[simps apply] def order_iso_rpow (y : ℝ) (hy : 0 < y) : ℝ≥0∞ ≃o ℝ≥0∞ := (strict_mono_rpow_of_pos hy).order_iso_of_right_inverse (λ x, x ^ y) (λ x, x ^ (1 / y)) (λ x, by { dsimp, rw [←rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] }) lemma order_iso_rpow_symm_apply (y : ℝ) (hy : 0 < y) : (order_iso_rpow y hy).symm = order_iso_rpow (1 / y) (one_div_pos.2 hy) := by { simp only [order_iso_rpow, one_div_one_div], refl } lemma rpow_le_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := monotone_rpow_of_nonneg h₂ h₁ lemma rpow_lt_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := strict_mono_rpow_of_pos h₂ h₁ lemma rpow_le_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := (strict_mono_rpow_of_pos hz).le_iff_le lemma rpow_lt_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := (strict_mono_rpow_of_pos hz).lt_iff_lt lemma le_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z hz.ne', rw [rpow_mul, ←one_div, @rpow_le_rpow_iff _ _ (1/z) (by simp [hz])], end lemma lt_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x < y ^ (1 / z) ↔ x ^ z < y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z (ne_of_lt hz).symm, rw [rpow_mul, ←one_div, @rpow_lt_rpow_iff _ _ (1/z) (by simp [hz])], end lemma rpow_one_div_le_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z := begin nth_rewrite 0 ← ennreal.rpow_one y, nth_rewrite 1 ← @_root_.mul_inv_cancel _ _ z hz.ne.symm, rw [ennreal.rpow_mul, ← one_div, ennreal.rpow_le_rpow_iff (one_div_pos.2 hz)], end lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0∞} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x^y < x^z := begin lift x to ℝ≥0 using hx', rw [one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_rpow_of_exponent_lt hx hyz] end lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0∞} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl]; linarith }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_rpow_of_exponent_le hx hyz] } end lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0∞} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp only [coe_lt_one_iff, coe_pos] at hx0 hx1, simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] end lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0∞} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top), by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl]; linarith }, { rw [coe_le_one_iff] at hx1, simp [coe_rpow_of_ne_zero h, nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] } end lemma rpow_le_self_of_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin nth_rewrite 1 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_ge hx h_one_le, end lemma le_rpow_self_of_one_le {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z := begin nth_rewrite 0 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_le hx h_one_le, end lemma rpow_pos_of_nonneg {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x^p := begin by_cases hp_zero : p = 0, { simp [hp_zero, zero_lt_one], }, { rw ←ne.def at hp_zero, have hp_pos := lt_of_le_of_ne hp_nonneg hp_zero.symm, rw ←zero_rpow_of_pos hp_pos, exact rpow_lt_rpow hx_pos hp_pos, }, end lemma rpow_pos {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x^p := begin cases lt_or_le 0 p with hp_pos hp_nonpos, { exact rpow_pos_of_nonneg hx_pos (le_of_lt hp_pos), }, { rw [←neg_neg p, rpow_neg, ennreal.inv_pos], exact rpow_ne_top_of_nonneg (right.nonneg_neg_iff.mpr hp_nonpos) hx_ne_top, }, end lemma rpow_lt_one {x : ℝ≥0∞} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x^z < 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top), simp only [coe_lt_one_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one hx hz], end lemma rpow_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top), simp only [coe_le_one_iff] at hx, simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx hz], end lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := begin cases x, { simp [top_rpow_of_neg hz, zero_lt_one] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_one_of_one_lt_of_neg hx hz] }, end lemma rpow_le_one_of_one_le_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x^z ≤ 1 := begin cases x, { simp [top_rpow_of_neg hz, zero_lt_one] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] }, end lemma one_lt_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] } end lemma one_le_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_le_rpow hx (le_of_lt hz)] }, end lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top), simp only [coe_lt_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz], end lemma one_le_rpow_of_pos_of_le_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z < 0) : 1 ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top), simp only [coe_le_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 (le_of_lt hz)], end lemma to_nnreal_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_nnreal) ^ z = (x ^ z).to_nnreal := begin rcases lt_trichotomy z 0 with H|H|H, { cases x, { simp [H, ne_of_lt] }, by_cases hx : x = 0, { simp [hx, H, ne_of_lt] }, { simp [coe_rpow_of_ne_zero hx] } }, { simp [H] }, { cases x, { simp [H, ne_of_gt] }, simp [coe_rpow_of_nonneg _ (le_of_lt H)] } end lemma to_real_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real := by rw [ennreal.to_real, ennreal.to_real, ←nnreal.coe_rpow, ennreal.to_nnreal_rpow] lemma of_real_rpow_of_pos {x p : ℝ} (hx_pos : 0 < x) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin simp_rw ennreal.of_real, rw [coe_rpow_of_ne_zero, coe_eq_coe, real.to_nnreal_rpow_of_nonneg hx_pos.le], simp [hx_pos], end lemma of_real_rpow_of_nonneg {x p : ℝ} (hx_nonneg : 0 ≤ x) (hp_nonneg : 0 ≤ p) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hx0 : x = 0, { rw ← ne.def at hp0, have hp_pos : 0 < p := lt_of_le_of_ne hp_nonneg hp0.symm, simp [hx0, hp_pos, hp_pos.ne.symm], }, rw ← ne.def at hx0, exact of_real_rpow_of_pos (hx_nonneg.lt_of_ne hx0.symm), end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0∞, y^x) := begin intros y z hyz, dsimp only at hyz, rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul, rpow_mul, hyz], end lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0∞, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0∞, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ end ennreal section tactics /-! ## Tactic extensions for powers on `ℝ≥0` and `ℝ≥0∞` -/ namespace norm_num theorem nnrpow_pos (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c : ℝ≥0) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, nnreal.rpow_nat_cast] theorem nnrpow_neg (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, nnreal.rpow_neg, nnreal.rpow_nat_cast] theorem ennrpow_pos (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c : ℝ≥0∞) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, ennreal.rpow_nat_cast] theorem ennrpow_neg (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0∞) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, ennreal.rpow_neg, ennreal.rpow_nat_cast] /-- Evaluate `nnreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_nnrpow : expr → expr → tactic (expr × expr) := prove_rpow' ``nnrpow_pos ``nnrpow_neg ``nnreal.rpow_zero `(ℝ≥0) `(ℝ) `(1:ℝ≥0) /-- Evaluate `ennreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_ennrpow : expr → expr → tactic (expr × expr) := prove_rpow' ``ennrpow_pos ``ennrpow_neg ``ennreal.rpow_zero `(ℝ≥0∞) `(ℝ) `(1:ℝ≥0∞) /-- Evaluates expressions of the form `rpow a b` and `a ^ b` in the special case where `b` is an integer and `a` is a positive rational (so it's really just a rational power). -/ @[norm_num] meta def eval_nnrpow_ennrpow : expr → tactic (expr × expr) | `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := b.to_int >> prove_nnrpow a b | `(nnreal.rpow %%a %%b) := b.to_int >> prove_nnrpow a b | `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := b.to_int >> prove_ennrpow a b | `(ennreal.rpow %%a %%b) := b.to_int >> prove_ennrpow a b | _ := tactic.failed end norm_num namespace tactic namespace positivity private lemma nnrpow_pos {a : ℝ≥0} (ha : 0 < a) (b : ℝ) : 0 < a ^ b := nnreal.rpow_pos ha /-- Auxiliary definition for the `positivity` tactic to handle real powers of nonnegative reals. -/ meta def prove_nnrpow (a b : expr) : tactic strictness := do strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``nnrpow_pos [p, b] | _ := failed -- We already know `0 ≤ x` for all `x : ℝ≥0` end private lemma ennrpow_pos {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hb : 0 < b) : 0 < a ^ b := ennreal.rpow_pos_of_nonneg ha hb.le /-- Auxiliary definition for the `positivity` tactic to handle real powers of extended nonnegative reals. -/ meta def prove_ennrpow (a b : expr) : tactic strictness := do strictness_a ← core a, strictness_b ← core b, match strictness_a, strictness_b with | positive pa, positive pb := positive <$> mk_app ``ennrpow_pos [pa, pb] | positive pa, nonnegative pb := positive <$> mk_app ``ennreal.rpow_pos_of_nonneg [pa, pb] | _, _ := failed -- We already know `0 ≤ x` for all `x : ℝ≥0∞` end end positivity open positivity /-- Extension for the `positivity` tactic: exponentiation by a real number is nonnegative when the base is nonnegative and positive when the base is positive. -/ @[positivity] meta def positivity_nnrpow_ennrpow : expr → tactic strictness | `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := prove_nnrpow a b | `(nnreal.rpow %%a %%b) := prove_nnrpow a b | `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := prove_ennrpow a b | `(ennreal.rpow %%a %%b) := prove_ennrpow a b | _ := failed end tactic end tactics
01e96089b2b22eb39361225c0449e41d04699b13
abd85493667895c57a7507870867b28124b3998f
/src/number_theory/sum_four_squares.lean
ce64a12af5ff2fa387c246c2d57a2230595ff4f1
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
11,904
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes ## Lagrange's four square theorem The main result in this file is `sum_four_squares`, a proof that every natural number is the sum of four square numbers. # Implementation Notes The proof used is close to Lagrange's original proof. -/ import data.zmod.basic import field_theory.finite import data.int.parity import data.fintype.card open finset polynomial finite_field equiv namespace int lemma sum_two_squares_of_two_mul_sum_two_squares {m x y : ℤ} (h : 2 * m = x^2 + y^2) : m = ((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2 := have (x^2 + y^2).even, by simp [h.symm, even_mul], have hxaddy : (x + y).even, by simpa [pow_two] with parity_simps, have hxsuby : (x - y).even, by simpa [pow_two] with parity_simps, have (x^2 + y^2) % 2 = 0, by simp [h.symm], (domain.mul_right_inj (show (2*2 : ℤ) ≠ 0, from dec_trivial)).1 $ calc 2 * 2 * m = (x - y)^2 + (x + y)^2 : by rw [mul_assoc, h]; ring ... = (2 * ((x - y) / 2))^2 + (2 * ((x + y) / 2))^2 : by rw [int.mul_div_cancel' hxsuby, int.mul_div_cancel' hxaddy] ... = 2 * 2 * (((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2) : by simp [mul_add, pow_succ, mul_comm, mul_assoc, mul_left_comm] lemma exists_sum_two_squares_add_one_eq_k (p : ℕ) [hp : fact p.prime] : ∃ (a b : ℤ) (k : ℕ), a^2 + b^2 + 1 = k * p ∧ k < p := hp.eq_two_or_odd.elim (λ hp2, hp2.symm ▸ ⟨1, 0, 1, rfl, dec_trivial⟩) $ λ hp1, let ⟨a, b, hab⟩ := zmod.sum_two_squares p (-1) in have hab' : (p : ℤ) ∣ a.val_min_abs ^ 2 + b.val_min_abs ^ 2 + 1, from (char_p.int_cast_eq_zero_iff (zmod p) p _).1 $ by simpa [eq_neg_iff_add_eq_zero] using hab, let ⟨k, hk⟩ := hab' in have hk0 : 0 ≤ k, from nonneg_of_mul_nonneg_left (by rw ← hk; exact (add_nonneg (add_nonneg (pow_two_nonneg _) (pow_two_nonneg _)) zero_le_one)) (int.coe_nat_pos.2 hp.pos), ⟨a.val_min_abs, b.val_min_abs, k.nat_abs, by rw [hk, int.nat_abs_of_nonneg hk0, mul_comm], lt_of_mul_lt_mul_left (calc p * k.nat_abs = a.val_min_abs.nat_abs ^ 2 + b.val_min_abs.nat_abs ^ 2 + 1 : by rw [← int.coe_nat_inj', int.coe_nat_add, int.coe_nat_add, int.coe_nat_pow, int.coe_nat_pow, int.nat_abs_pow_two, int.nat_abs_pow_two, int.coe_nat_one, hk, int.coe_nat_mul, int.nat_abs_of_nonneg hk0] ... ≤ (p / 2) ^ 2 + (p / 2)^2 + 1 : add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (le_refl _) ... < (p / 2) ^ 2 + (p / 2)^ 2 + (p % 2)^2 + ((2 * (p / 2)^2 + (4 * (p / 2) * (p % 2)))) : by rw [hp1, nat.one_pow, mul_one]; exact (lt_add_iff_pos_right _).2 (add_pos_of_nonneg_of_pos (nat.zero_le _) (mul_pos dec_trivial (nat.div_pos hp.two_le dec_trivial))) ... = p * p : by { conv_rhs { rw [← nat.mod_add_div p 2] }, ring }) (show 0 ≤ p, from nat.zero_le _)⟩ end int namespace nat open int open_locale classical private lemma sum_four_squares_of_two_mul_sum_four_squares {m a b c d : ℤ} (h : a^2 + b^2 + c^2 + d^2 = 2 * m) : ∃ w x y z : ℤ, w^2 + x^2 + y^2 + z^2 = m := have ∀ f : fin 4 → zmod 2, (f 0)^2 + (f 1)^2 + (f 2)^2 + (f 3)^2 = 0 → ∃ i : (fin 4), (f i)^2 + f (swap i 0 1)^2 = 0 ∧ f (swap i 0 2)^2 + f (swap i 0 3)^2 = 0, from dec_trivial, let f : fin 4 → ℤ := vector.nth (a::b::c::d::vector.nil) in let ⟨i, hσ⟩ := this (coe ∘ f) (by rw [← @zero_mul (zmod 2) _ m, ← show ((2 : ℤ) : zmod 2) = 0, from rfl, ← int.cast_mul, ← h]; simp only [int.cast_add, int.cast_pow]; refl) in let σ := swap i 0 in have h01 : 2 ∣ f (σ 0) ^ 2 + f (σ 1) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa [σ] using hσ.1, have h23 : 2 ∣ f (σ 2) ^ 2 + f (σ 3) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa using hσ.2, let ⟨x, hx⟩ := h01 in let ⟨y, hy⟩ := h23 in ⟨(f (σ 0) - f (σ 1)) / 2, (f (σ 0) + f (σ 1)) / 2, (f (σ 2) - f (σ 3)) / 2, (f (σ 2) + f (σ 3)) / 2, begin rw [← int.sum_two_squares_of_two_mul_sum_two_squares hx.symm, add_assoc, ← int.sum_two_squares_of_two_mul_sum_two_squares hy.symm, ← domain.mul_right_inj (show (2 : ℤ) ≠ 0, from dec_trivial), ← h, mul_add, ← hx, ← hy], have : univ.sum (λ x, f (σ x)^2) = univ.sum (λ x, f x^2), { conv_rhs { rw ← finset.sum_equiv σ } }, have fin4univ : (univ : finset (fin 4)).1 = 0::1::2::3::0, from dec_trivial, simpa [finset.sum_eq_multiset_sum, fin4univ, multiset.sum_cons, f, add_assoc] end⟩ private lemma prime_sum_four_squares (p : ℕ) [hp : _root_.fact p.prime] : ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = p := have hm : ∃ m < p, 0 < m ∧ ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = m * p, from let ⟨a, b, k, hk⟩ := exists_sum_two_squares_add_one_eq_k p in ⟨k, hk.2, nat.pos_of_ne_zero $ (λ hk0, by rw [hk0, int.coe_nat_zero, zero_mul] at hk; exact ne_of_gt (show a^2 + b^2 + 1 > 0, from add_pos_of_nonneg_of_pos (add_nonneg (pow_two_nonneg _) (pow_two_nonneg _)) zero_lt_one) hk.1), a, b, 1, 0, by simpa [_root_.pow_two] using hk.1⟩, let m := nat.find hm in let ⟨a, b, c, d, (habcd : a^2 + b^2 + c^2 + d^2 = m * p)⟩ := (nat.find_spec hm).snd.2 in by haveI hm0 : _root_.fact (0 < m) := (nat.find_spec hm).snd.1; exact have hmp : m < p, from (nat.find_spec hm).fst, m.mod_two_eq_zero_or_one.elim (λ hm2 : m % 2 = 0, let ⟨k, hk⟩ := (nat.dvd_iff_mod_eq_zero _ _).2 hm2 in have hk0 : 0 < k, from nat.pos_of_ne_zero $ λ _, by { simp [*, lt_irrefl] at *, exact hm0 }, have hkm : k < m, by rw [hk, two_mul]; exact (lt_add_iff_pos_left _).2 hk0, false.elim $ nat.find_min hm hkm ⟨lt_trans hkm hmp, hk0, sum_four_squares_of_two_mul_sum_four_squares (show a^2 + b^2 + c^2 + d^2 = 2 * (k * p), by rw [habcd, hk, int.coe_nat_mul, mul_assoc]; simp)⟩) (λ hm2 : m % 2 = 1, if hm1 : m = 1 then ⟨a, b, c, d, by simp only [hm1, habcd, int.coe_nat_one, one_mul]⟩ else --have hm1 : 1 < m, from lt_of_le_of_ne hm0 (ne.symm hm1), let w := (a : zmod m).val_min_abs, x := (b : zmod m).val_min_abs, y := (c : zmod m).val_min_abs, z := (d : zmod m).val_min_abs in have hnat_abs : w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ), by simp [_root_.pow_two], have hwxyzlt : w^2 + x^2 + y^2 + z^2 < m^2, from calc w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ) : hnat_abs ... ≤ ((m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 : ℕ) : int.coe_nat_le.2 $ add_le_add (add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) ... = 4 * (m / 2 : ℕ) ^ 2 : by simp [_root_.pow_two, bit0, bit1, mul_add, add_mul, add_assoc] ... < 4 * (m / 2 : ℕ) ^ 2 + ((4 * (m / 2) : ℕ) * (m % 2 : ℕ) + (m % 2 : ℕ)^2) : (lt_add_iff_pos_right _).2 (by rw [hm2, int.coe_nat_one, _root_.one_pow, mul_one]; exact add_pos_of_nonneg_of_pos (int.coe_nat_nonneg _) zero_lt_one) ... = m ^ 2 : by conv_rhs {rw [← nat.mod_add_div m 2]}; simp [-nat.mod_add_div, mul_add, add_mul, bit0, bit1, mul_comm, mul_assoc, mul_left_comm, _root_.pow_add, add_comm, add_left_comm], have hwxyzabcd : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = ((a^2 + b^2 + c^2 + d^2 : ℤ) : zmod m), by simp [w, x, y, z, pow_two], have hwxyz0 : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = 0, by rw [hwxyzabcd, habcd, int.cast_mul, cast_coe_nat, zmod.cast_self, zero_mul], let ⟨n, hn⟩ := ((char_p.int_cast_eq_zero_iff _ m _).1 hwxyz0) in have hn0 : 0 < n.nat_abs, from int.nat_abs_pos_of_ne_zero (λ hn0, have hwxyz0 : (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs^2 + z.nat_abs^2 : ℕ) = 0, by { rw [← int.coe_nat_eq_zero, ← hnat_abs], rwa [hn0, mul_zero] at hn }, have habcd0 : (m : ℤ) ∣ a ∧ (m : ℤ) ∣ b ∧ (m : ℤ) ∣ c ∧ (m : ℤ) ∣ d, by simpa [@add_eq_zero_iff_eq_zero_of_nonneg ℤ _ _ _ (pow_two_nonneg _) (pow_two_nonneg _), nat.pow_two, w, x, y, z, (char_p.int_cast_eq_zero_iff _ m _), and.assoc] using hwxyz0, let ⟨ma, hma⟩ := habcd0.1, ⟨mb, hmb⟩ := habcd0.2.1, ⟨mc, hmc⟩ := habcd0.2.2.1, ⟨md, hmd⟩ := habcd0.2.2.2 in have hmdvdp : m ∣ p, from int.coe_nat_dvd.1 ⟨ma^2 + mb^2 + mc^2 + md^2, (domain.mul_right_inj (show (m : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 hm0)).1 $ by rw [← habcd, hma, hmb, hmc, hmd]; ring⟩, (hp.2 _ hmdvdp).elim hm1 (λ hmeqp, by simpa [lt_irrefl, hmeqp] using hmp)), have hawbxcydz : ((m : ℕ) : ℤ) ∣ a * w + b * x + c * y + d * z, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by rw [← hwxyz0]; simp; ring, have haxbwczdy : ((m : ℕ) : ℤ) ∣ a * x - b * w - c * z + d * y, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by simp [sub_eq_add_neg]; ring, have haybzcwdx : ((m : ℕ) : ℤ) ∣ a * y + b * z - c * w - d * x, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by simp [sub_eq_add_neg]; ring, have hazbycxdw : ((m : ℕ) : ℤ) ∣ a * z - b * y + c * x - d * w, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by simp [sub_eq_add_neg]; ring, let ⟨s, hs⟩ := hawbxcydz, ⟨t, ht⟩ := haxbwczdy, ⟨u, hu⟩ := haybzcwdx, ⟨v, hv⟩ := hazbycxdw in have hn_nonneg : 0 ≤ n, from nonneg_of_mul_nonneg_left (by erw [← hn]; repeat {try {refine add_nonneg _ _}, try {exact pow_two_nonneg _}}) (int.coe_nat_pos.2 hm0), have hnm : n.nat_abs < m, from int.coe_nat_lt.1 (lt_of_mul_lt_mul_left (by rw [int.nat_abs_of_nonneg hn_nonneg, ← hn, ← _root_.pow_two]; exact hwxyzlt) (int.coe_nat_nonneg m)), have hstuv : s^2 + t^2 + u^2 + v^2 = n.nat_abs * p, from (domain.mul_right_inj (show (m^2 : ℤ) ≠ 0, from pow_ne_zero 2 (int.coe_nat_ne_zero_iff_pos.2 hm0))).1 $ calc (m : ℤ)^2 * (s^2 + t^2 + u^2 + v^2) = ((m : ℕ) * s)^2 + ((m : ℕ) * t)^2 + ((m : ℕ) * u)^2 + ((m : ℕ) * v)^2 : by simp [_root_.mul_pow]; ring ... = (w^2 + x^2 + y^2 + z^2) * (a^2 + b^2 + c^2 + d^2) : by simp only [hs.symm, ht.symm, hu.symm, hv.symm]; ring ... = _ : by rw [hn, habcd, int.nat_abs_of_nonneg hn_nonneg]; dsimp [m]; ring, false.elim $ nat.find_min hm hnm ⟨lt_trans hnm hmp, hn0, s, t, u, v, hstuv⟩) lemma sum_four_squares : ∀ n : ℕ, ∃ a b c d : ℕ, a^2 + b^2 + c^2 + d^2 = n | 0 := ⟨0, 0, 0, 0, rfl⟩ | 1 := ⟨1, 0, 0, 0, rfl⟩ | n@(k+2) := have hm : _root_.fact (min_fac (k+2)).prime := min_fac_prime dec_trivial, have n / min_fac n < n := factors_lemma, let ⟨a, b, c, d, h₁⟩ := show ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = min_fac n, by exactI prime_sum_four_squares (min_fac (k+2)) in let ⟨w, x, y, z, h₂⟩ := sum_four_squares (n / min_fac n) in ⟨(a * x - b * w - c * z + d * y).nat_abs, (a * y + b * z - c * w - d * x).nat_abs, (a * z - b * y + c * x - d * w).nat_abs, (a * w + b * x + c * y + d * z).nat_abs, begin rw [← int.coe_nat_inj', ← nat.mul_div_cancel' (min_fac_dvd (k+2)), int.coe_nat_mul, ← h₁, ← h₂], simp, ring end⟩ end nat
fc4385a9fb0239e5bdbc60bb4772a82bb5fb8c14
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/undergraduate/MAS114/closest_integer.lean
137cc59ec228ee1d4303c564bb53041df530f991
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,653
lean
lemma abs_neg_of_pos {α : Type*} [decidable_linear_ordered_comm_group α] (a : α) : a > 0 → abs (- a) = a := λ a_pos, (abs_of_neg (neg_neg_of_pos a_pos)).trans (neg_neg a) def half_Q : ℚ := 1 / 2 def neg_half_Q : ℚ := - half_Q lemma dist_neg_Q (n : ℤ) (h : n ≥ 0) : abs (neg_half_Q - ((- (1 + n)) : ℤ)) = (n : ℚ) + half_Q := begin let n_Q : ℚ := n, have e0 : neg_half_Q + 1 = half_Q := dec_trivial, have e1 := calc neg_half_Q - ((- (1 + n)) : ℤ) = n + (neg_half_Q + 1) : by {rw[int.cast_neg,int.cast_add],ring} ... = n_Q + half_Q : by rw[e0], have e2 : n_Q + half_Q > 0 := add_pos_of_nonneg_of_pos (int.cast_nonneg.mpr h) one_half_pos, have e3 : abs (n_Q + half_Q) = n_Q + half_Q := @abs_of_pos ℚ _ (n_Q + half_Q) e2, rw[e1,e3], end lemma dist_nonneg_Q (n : ℤ) (h : n ≥ 0) : abs (neg_half_Q - n) = (n : ℚ) + half_Q := begin let n_Q : ℚ := n, have e1 : neg_half_Q - n = - (n_Q + half_Q) := by ring, have e2 : n_Q + half_Q > 0 := add_pos_of_nonneg_of_pos (int.cast_nonneg.mpr h) one_half_pos, have e3 : abs (- (n_Q + half_Q)) = n_Q + half_Q := @abs_neg_of_pos ℚ _ (n_Q + half_Q) e2, rw[e1,e3], end lemma dist_neg (n : ℤ) (h : n ≥ 0) : abs (neg_half_R - (-(1 + n) : ℤ)) = (n : ℝ) + half_R := begin let x_Q : ℚ := neg_half_Q - (-(1 + n) : ℤ), let x_R : ℝ := neg_half_R - (-(1 + n) : ℤ), let d_Q : ℚ := abs x_Q, let d_R : ℝ := abs x_R, have e0 : x_R = x_Q := by { dsimp[x_R,x_Q,neg_half_R], rw[rat.cast_add,rat.cast_neg,rat.cast_coe_int], }, have e1 : d_R = d_Q := by { dsimp[d_R,d_Q],rw[e0,rat.cast_abs] }, have e2 : d_Q = (n : ℚ) + half_Q := dist_neg_Q n h, exact calc d_R = d_Q : e1 ... = (n : ℚ) + half_Q : by {rw[e2,rat.cast_add]} ... = (n : ℝ) + half_R : by {dsimp[half_R],rw[rat.cast_coe_int]} end lemma dist_nonneg (n : ℤ) (h : n ≥ 0) : abs(neg_half_R - n) = (n : ℝ) + half_R := begin let x_Q : ℚ := neg_half_Q - n, let x_R : ℝ := neg_half_R - n, let d_Q : ℚ := abs x_Q, let d_R : ℝ := abs x_R, have e0 : x_R = x_Q := by { dsimp[x_R,x_Q,neg_half_R], rw[rat.cast_add,rat.cast_neg,rat.cast_coe_int], }, have e1 : d_R = d_Q := by { dsimp[d_R,d_Q],rw[e0,rat.cast_abs] }, have e2 : d_Q = (n : ℚ) + half_Q := dist_nonneg_Q n h, exact calc d_R = d_Q : e1 ... = (n : ℚ) + half_Q : by {rw[e2,rat.cast_add]} ... = (n : ℝ) + half_R : by {dsimp[half_R],rw[rat.cast_coe_int]} end lemma no_closest_integer (n : ℤ) : ¬ (is_closest_integer n neg_half_R) := begin have hh : ∀ k : ℤ, k ≥ 0 → ¬ ((k : ℝ) + half_R < half_R) := begin intros k k_nonneg k_half_lt, have k_half_ge : (k : ℝ) + half_R ≥ half_R := le_add_of_nonneg_left (int.cast_nonneg.mpr k_nonneg), exact not_le_of_gt k_half_lt k_half_ge, end, intro h0, by_cases h1 : n ≥ 0, {let m : ℤ := -1, have : ¬ (m ≥ 0) := dec_trivial, have h2 : m ≠ n := λ e, this (e.symm.subst h1), let h3 := h0 (-1) h2, let h4 := dist_neg 0 (le_refl 0), rw[add_zero] at h4, rw[h4,dist_nonneg n h1,int.cast_zero,zero_add] at h3, exact hh n h1 h3, },{ have h1a : n < 0 := lt_of_not_ge h1, let m : ℤ := 0, have : ¬ (m < 0) := dec_trivial, have h2 : m ≠ n := λ e, this (e.symm.subst h1a), let h3 := h0 0 h2, let h4 := dist_nonneg 0 (le_refl 0), rw[h4,int.cast_zero,zero_add] at h3, let k := - (1 + n), have h5 : n = - (1 + k) := by {dsimp[k],ring}, let h6 := (neg_nonneg_of_nonpos (int.add_one_le_iff.mpr (lt_of_not_ge h1))), rw[add_comm n 1] at h6, have h6a : 0 ≤ k := h6, let h7 := dist_neg k h6a, rw[← h5] at h7, rw[h7] at h3, exact hh k h6a h3, } end
d34d964063d78d590615772a4857e466812cb5ca
d5b53bc87e7f4dda87570c8ef6ee4b4de685f315
/src/add_group_hom/basic.lean
262988541878ad753c4ea232a3bd4f2e0341f601
[]
no_license
Shenyang1995/M4R
3bec366fba7262ed29d7f64b4ba7cc978494c022
a6a3399c4d1935b39a22f64c30f293ef2a32fdeb
refs/heads/master
1,597,008,096,640
1,591,722,931,000
1,591,722,931,000
214,177,424
5
0
null
null
null
null
UTF-8
Lean
false
false
4,702
lean
/-additive group homomorphisms -/ import add_subgroup.basic @[reducible] def add_group_hom (G : Type*) (H : Type*) [add_group G] [add_group H] := add_monoid_hom G H def add_group_hom.comp {G : Type*} {H : Type*} {K : Type*} [add_group G] [add_group H] [add_group K] (f : add_group_hom H K) (g : add_group_hom G H): add_group_hom G K := add_monoid_hom.comp f g def add_group_hom.mk {G : Type*} [add_group G] {H : Type*} [add_group H] (f : G → H) (h : ∀ a b : G, f(a + b) = f a + f b) : add_group_hom G H := { to_fun := f, map_zero' := begin have h1: f (0:G) + f 0=f((0:G)+(0:G)), rw h, norm_num at h1, exact h1, end, map_add' := h } namespace add_group_hom variables {G : Type*} {H : Type*} [add_group G] [add_group H] def map {G₁ : Type*} [add_group G₁] {G₂ : Type*} [add_group G₂] (f : add_group_hom G₁ G₂) (H : add_subgroup G₁) : add_subgroup G₂ := { carrier := (f : G₁ → G₂) '' H.carrier, is_add_subgroup := { zero_mem := ⟨0, H.zero_mem, f.map_zero⟩, add_mem := begin rintros _ _ ⟨c, hc, rfl⟩ ⟨d, hd, rfl⟩, exact ⟨c + d, H.add_mem hc hd, f.map_add c d⟩ end, neg_mem := begin rintros _ ⟨c, hc, rfl⟩, exact ⟨-c, H.neg_mem hc, f.map_neg c⟩ end } } def comap {G₁ : Type*} [add_group G₁] {G₂ : Type*} [add_group G₂] (f : add_group_hom G₁ G₂) (H : add_subgroup G₂) : add_subgroup G₁ := { carrier := (f : G₁ → G₂) ⁻¹' H.carrier, is_add_subgroup := { zero_mem := begin show f 0 ∈ H, rw f.map_zero, exact H.zero_mem end, add_mem := begin intros a b ha hb, show f (a + b) ∈ H, rw f.map_add, exact H.add_mem ha hb, end, neg_mem := begin intros a ha, show f (-a) ∈ H, rw f.map_neg, exact H.neg_mem ha end } } lemma comap_comp {G₁ : Type*} [add_group G₁] {G₂ : Type*} [add_group G₂] {G₃ : Type*} [add_group G₃] (f : add_group_hom G₁ G₂) (g : add_group_hom G₂ G₃) (H : add_subgroup G₃) : add_group_hom.comap (g.comp f) H = add_group_hom.comap f (add_group_hom.comap g H) := rfl -- this works: --def ker (f : add_monoid_hom G H) : add_submonoid G := f.comap ⊥ def ker (f : add_group_hom G H) : add_subgroup G := add_group_hom.comap f ⊥ lemma mem_ker (f : add_group_hom G H) (g : G) : g ∈ ker f ↔ f g = 0 := iff.rfl -- but f.comap ⊥ doesn't :-/ def range (f : add_group_hom G H) : add_subgroup H := add_group_hom.map f ⊤ def induced {f : add_group_hom G H} {G₁ : add_subgroup G} {H₁ : add_subgroup H} (h : f '' G₁ ⊆ H₁) : add_group_hom G₁ H₁ := { to_fun := λ g, ⟨f g.1, h ⟨g.1, g.2, rfl⟩⟩, map_zero' := begin rw subtype.ext, simp, convert f.map_zero, end, map_add' := begin intros, simp [subtype.ext], apply f.map_add, end } end add_group_hom /- quotients -/ namespace add_subgroup variables {A : Type*} [add_comm_group A] @[derive add_comm_group] def quotient (B : add_subgroup A) := quotient_add_group.quotient B.carrier variable (B : add_subgroup A) def quotient.mk : add_group_hom A B.quotient := { to_fun := quotient.mk', map_zero' := rfl, map_add' := λ _ _, rfl } lemma quotient.ker_mk : add_group_hom.ker (quotient.mk B) = B := begin rw ←ext_iff, intro g, rw add_group_hom.mem_ker, show quotient.mk' g = quotient.mk' 0 ↔ _, rw quotient.eq', show -g + 0 ∈ B ↔ _, rw add_zero, rw neg_mem_iff, end def quotient.lift {A : Type*} [add_comm_group A] {C : Type*} [add_comm_group C] (f : add_group_hom A C) (B : add_subgroup A) (hB : B ≤ add_group_hom.ker f) : add_group_hom B.quotient C := { to_fun := λ q, q.lift_on' ⇑f begin intros a₁ a₂ h, change (-a₁) + a₂ ∈ B at h, have h2 : f (-a₁ + a₂) = 0 := (f.mem_ker _).1 (hB h), rwa [f.map_add, f.map_neg, neg_add_eq_iff_eq_add, add_zero, eq_comm] at h2, end, map_zero' := begin show f 0= 0, exact f.map_zero, end, map_add' := begin intros x y, apply quotient.induction_on₂' x y, exact f.map_add, end } def quotient.map {A₁ : Type*} [add_comm_group A₁] {A₂ : Type*} [add_comm_group A₂] (B₁ : add_subgroup A₁) (B₂ : add_subgroup A₂) (f : add_group_hom A₁ A₂) (hf : B₁ ≤ add_group_hom.comap f B₂) : add_group_hom (quotient B₁) (quotient B₂) := { to_fun := quotient.lift ((quotient.mk B₂).comp f) B₁ $ le_trans hf begin unfold add_group_hom.ker, rw add_group_hom.comap_comp, apply le_of_eq _, congr', symmetry, apply quotient.ker_mk, end, map_zero' := begin apply add_monoid_hom.map_zero, end, map_add' := by apply add_monoid_hom.map_add } end add_subgroup
1d316bd9357903dd4a8b40f2a6b9874015a0da76
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/category/monad/cont.lean
6331826049b82dfb645c0e479c3e3558bab2a8b4
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
8,773
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon Monad encapsulating continuation passing programming style, similar to Haskell's `Cont`, `ContT` and `MonadCont`: http://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Cont.html -/ import tactic.ext import category.monad.basic category.monad.writer universes u v w structure monad_cont.label (α : Type w) (m : Type u → Type v) (β : Type u) := (apply : α → m β) def monad_cont.goto {α β} {m : Type u → Type v} (f : monad_cont.label α m β) (x : α) := f.apply x class monad_cont (m : Type u → Type v) := (call_cc : Π {α β}, ((monad_cont.label α m β) → m α) → m α) open monad_cont class is_lawful_monad_cont (m : Type u → Type v) [monad m] [monad_cont m] extends is_lawful_monad m := (call_cc_bind_right {α ω γ} (cmd : m α) (next : (label ω m γ) → α → m ω) : call_cc (λ f, cmd >>= next f) = cmd >>= λ x, call_cc (λ f, next f x)) (call_cc_bind_left {α} (β) (x : α) (dead : label α m β → β → m α) : call_cc (λ f : label α m β, goto f x >>= dead f) = pure x) (call_cc_dummy {α β} (dummy : m α) : call_cc (λ f : label α m β, dummy) = dummy) export is_lawful_monad_cont def cont_t (r : Type u) (m : Type u → Type v) (α : Type w) := (α → m r) → m r @[reducible] def cont (r : Type u) (α : Type w) := cont_t r id α namespace cont_t export monad_cont (label goto) variables {r : Type u} {m : Type u → Type v} {α β γ ω : Type w} def run : cont_t r m α → (α → m r) → m r := id def map (f : m r → m r) (x : cont_t r m α) : cont_t r m α := f ∘ x lemma run_cont_t_map_cont_t (f : m r → m r) (x : cont_t r m α) : run (map f x) = f ∘ run x := rfl def with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : cont_t r m β := λ g, x $ f g lemma run_with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : run (with_cont_t f x) = run x ∘ f := rfl @[extensionality] protected lemma ext {x y : cont_t r m α} (h : ∀ f, x.run f = y.run f) : x = y := by { ext; apply h } instance : monad (cont_t r m) := { pure := λ α x f, f x, bind := λ α β x f g, x $ λ i, f i g } instance : is_lawful_monad (cont_t r m) := { id_map := by { intros, refl }, pure_bind := by { intros, ext, refl }, bind_assoc := by { intros, ext, refl } } def monad_lift [monad m] {α} : m α → cont_t r m α := λ x f, x >>= f instance [monad m] : has_monad_lift m (cont_t r m) := { monad_lift := λ α, cont_t.monad_lift } lemma monad_lift_bind [monad m] [is_lawful_monad m] {α β} (x : m α) (f : α → m β) : (monad_lift (x >>= f) : cont_t r m β) = monad_lift x >>= monad_lift ∘ f := by { ext, simp only [monad_lift,has_monad_lift.monad_lift,(∘),(>>=),bind_assoc,id.def,run,cont_t.monad_lift] } instance : monad_cont (cont_t r m) := { call_cc := λ α β f g, f ⟨λ x h, g x⟩ g } instance : is_lawful_monad_cont (cont_t r m) := { call_cc_bind_right := by intros; ext; refl, call_cc_bind_left := by intros; ext; refl, call_cc_dummy := by intros; ext; refl } instance (ε) [monad_except ε m] : monad_except ε (cont_t r m) := { throw := λ x e f, throw e, catch := λ α act h f, catch (act f) (λ e, h e f) } instance : monad_run (λ α, (α → m r) → ulift.{u v} (m r)) (cont_t.{u v u} r m) := { run := λ α f x, ⟨ f x ⟩ } end cont_t variables {m : Type u → Type v} [monad m] def except_t.mk_label {α β ε} : label (except.{u u} ε α) m β → label α (except_t ε m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (except.ok a) ⟩ lemma except_t.goto_mk_label {α β ε : Type*} (x : label (except.{u u} ε α) m β) (i : α) : goto (except_t.mk_label x) i = ⟨ except.ok <$> goto x (except.ok i) ⟩ := by cases x; refl def except_t.call_cc {ε} [monad_cont m] {α β : Type*} (f : label α (except_t ε m) β → except_t ε m α) : except_t ε m α := except_t.mk (call_cc $ λ x : label _ m β, except_t.run $ f (except_t.mk_label x) : m (except ε α)) instance {ε} [monad_cont m] : monad_cont (except_t ε m) := { call_cc := λ α β, except_t.call_cc } instance {ε} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (except_t ε m) := { call_cc_bind_right := by { intros, simp [call_cc,except_t.call_cc,call_cc_bind_right], ext, dsimp, congr, ext ⟨ ⟩; simp [except_t.bind_cont,@call_cc_dummy m _], }, call_cc_bind_left := by { intros, simp [call_cc,except_t.call_cc,call_cc_bind_right,except_t.goto_mk_label,map_eq_bind_pure_comp,bind_assoc,@call_cc_bind_left m _], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,except_t.call_cc,@call_cc_dummy m _], ext, refl }, } def option_t.mk_label {α β} : label (option.{u} α) m β → label α (option_t m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (some a) ⟩ lemma option_t.goto_mk_label {α β : Type*} (x : label (option.{u} α) m β) (i : α) : goto (option_t.mk_label x) i = ⟨ some <$> goto x (some i) ⟩ := by cases x; refl def option_t.call_cc [monad_cont m] {α β : Type*} (f : label α (option_t m) β → option_t m α) : option_t m α := option_t.mk (call_cc $ λ x : label _ m β, option_t.run $ f (option_t.mk_label x) : m (option α)) instance [monad_cont m] : monad_cont (option_t m) := { call_cc := λ α β, option_t.call_cc } instance [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (option_t m) := { call_cc_bind_right := by { intros, simp [call_cc,option_t.call_cc,call_cc_bind_right], ext, dsimp, congr, ext ⟨ ⟩; simp [option_t.bind_cont,@call_cc_dummy m _], }, call_cc_bind_left := by { intros, simp [call_cc,option_t.call_cc,call_cc_bind_right,option_t.goto_mk_label,map_eq_bind_pure_comp,bind_assoc,@call_cc_bind_left m _], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,option_t.call_cc,@call_cc_dummy m _], ext, refl }, } def writer_t.mk_label {α β ω} [has_one ω] : label (α × ω) m β → label α (writer_t ω m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (a,1) ⟩ lemma writer_t.goto_mk_label {α β ω : Type*} [has_one ω] (x : label (α × ω) m β) (i : α) : goto (writer_t.mk_label x) i = monad_lift (goto x (i,1)) := by cases x; refl def writer_t.call_cc [monad_cont m] {α β ω : Type*} [has_one ω] (f : label α (writer_t ω m) β → writer_t ω m α) : writer_t ω m α := ⟨ call_cc (writer_t.run ∘ f ∘ writer_t.mk_label : label (α × ω) m β → m (α × ω)) ⟩ instance (ω) [monad m] [has_one ω] [monad_cont m] : monad_cont (writer_t ω m) := { call_cc := λ α β, writer_t.call_cc } def state_t.mk_label {α β σ : Type u} : label (α × σ) m (β × σ) → label α (state_t σ m) β | ⟨ f ⟩ := ⟨ λ a, ⟨ λ s, f (a,s) ⟩ ⟩ lemma state_t.goto_mk_label {α β σ : Type u} (x : label (α × σ) m (β × σ)) (i : α) : goto (state_t.mk_label x) i = ⟨ λ s, (goto x (i,s)) ⟩ := by cases x; refl def state_t.call_cc {σ} [monad_cont m] {α β : Type*} (f : label α (state_t σ m) β → state_t σ m α) : state_t σ m α := ⟨ λ r, call_cc (λ f', (f $ state_t.mk_label f').run r) ⟩ instance {σ} [monad_cont m] : monad_cont (state_t σ m) := { call_cc := λ α β, state_t.call_cc } instance {σ} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (state_t σ m) := { call_cc_bind_right := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_right,(>>=),state_t.bind], ext, dsimp, congr, ext ⟨x₀,x₁⟩, refl }, call_cc_bind_left := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_left,(>>=),state_t.bind,state_t.goto_mk_label], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_right,(>>=),state_t.bind,@call_cc_dummy m _], ext, refl }, } def reader_t.mk_label {α β} (ρ) : label α m β → label α (reader_t ρ m) β | ⟨ f ⟩ := ⟨ monad_lift ∘ f ⟩ lemma reader_t.goto_mk_label {α ρ β} (x : label α m β) (i : α) : goto (reader_t.mk_label ρ x) i = monad_lift (goto x i) := by cases x; refl def reader_t.call_cc {ε} [monad_cont m] {α β : Type*} (f : label α (reader_t ε m) β → reader_t ε m α) : reader_t ε m α := ⟨ λ r, call_cc (λ f', (f $ reader_t.mk_label _ f').run r) ⟩ instance {ρ} [monad_cont m] : monad_cont (reader_t ρ m) := { call_cc := λ α β, reader_t.call_cc } instance {ρ} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (reader_t ρ m) := { call_cc_bind_right := by { intros, simp [call_cc,reader_t.call_cc,call_cc_bind_right], ext, refl }, call_cc_bind_left := by { intros, simp [call_cc,reader_t.call_cc,call_cc_bind_left,reader_t.goto_mk_label], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,reader_t.call_cc,@call_cc_dummy m _], ext, refl } }
0b3092e18b435ef3c7425c94f842e3b0f3f2fe9a
46125763b4dbf50619e8846a1371029346f4c3db
/src/analysis/complex/polynomial.lean
1276e62cef114b517adc66f1834c2da17ea9da65
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
6,003
lean
/- Copyright (c) 2019 Chris Hughes All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.polynomial topology.algebra.polynomial analysis.complex.exponential open complex polynomial metric filter is_absolute_value set lattice namespace complex lemma exists_forall_abs_polynomial_eval_le (p : polynomial ℂ) : ∃ x, ∀ y, (p.eval x).abs ≤ (p.eval y).abs := if hp0 : 0 < degree p then let ⟨r, hr0, hr⟩ := polynomial.tendsto_infinity complex.abs hp0 ((p.eval 0).abs) in let ⟨x, hx₁, hx₂⟩ := (proper_space.compact_ball (0:ℂ) r).exists_forall_le ⟨0, by simp [le_of_lt hr0]⟩ (continuous_abs.comp p.continuous_eval).continuous_on in ⟨x, λ y, if hy : y.abs ≤ r then hx₂ y $ by simpa [complex.dist_eq] using hy else le_trans (hx₂ _ (by simp [le_of_lt hr0])) (le_of_lt (hr y (lt_of_not_ge hy)))⟩ else ⟨p.coeff 0, by rw [eq_C_of_degree_le_zero (le_of_not_gt hp0)]; simp⟩ /- The following proof uses the method given at <https://ncatlab.org/nlab/show/fundamental+theorem+of+algebra#classical_fta_via_advanced_calculus> -/ /-- The fundamental theorem of algebra. Every non constant complex polynomial has a root -/ lemma exists_root {f : polynomial ℂ} (hf : 0 < degree f) : ∃ z : ℂ, is_root f z := let ⟨z₀, hz₀⟩ := exists_forall_abs_polynomial_eval_le f in exists.intro z₀ $ classical.by_contradiction $ λ hf0, have hfX : f - C (f.eval z₀) ≠ 0, from mt sub_eq_zero.1 (λ h, not_le_of_gt hf (h.symm ▸ degree_C_le)), let n := root_multiplicity z₀ (f - C (f.eval z₀)) in let g := (f - C (f.eval z₀)) /ₘ ((X - C z₀) ^ n) in have hg0 : g.eval z₀ ≠ 0, from eval_div_by_monic_pow_root_multiplicity_ne_zero _ hfX, have hg : g * (X - C z₀) ^ n = f - C (f.eval z₀), from div_by_monic_mul_pow_root_multiplicity_eq _ _, have hn0 : 0 < n, from nat.pos_of_ne_zero $ λ hn0, by simpa [g, hn0] using hg0, let ⟨δ', hδ'₁, hδ'₂⟩ := continuous_iff.1 (polynomial.continuous_eval g) z₀ ((g.eval z₀).abs) (complex.abs_pos.2 hg0) in let δ := min (min (δ' / 2) 1) (((f.eval z₀).abs / (g.eval z₀).abs) / 2) in have hf0' : 0 < (f.eval z₀).abs, from complex.abs_pos.2 hf0, have hfg0 : 0 < abs (eval z₀ f) * (abs (eval z₀ g))⁻¹, from div_pos hf0' (complex.abs_pos.2 hg0), have hδ0 : 0 < δ, from lt_min (lt_min (half_pos hδ'₁) (by norm_num)) (half_pos hfg0), have hδ : ∀ z : ℂ, abs (z - z₀) = δ → abs (g.eval z - g.eval z₀) < (g.eval z₀).abs, from λ z hz, hδ'₂ z (by rw [complex.dist_eq, hz]; exact lt_of_le_of_lt (le_trans (min_le_left _ _) (min_le_left _ _)) (half_lt_self hδ'₁)), have hδ1 : δ ≤ 1, from le_trans (min_le_left _ _) (min_le_right _ _), let F : polynomial ℂ := C (f.eval z₀) + C (g.eval z₀) * (X - C z₀) ^ n in let z' := (-f.eval z₀ * (g.eval z₀).abs * δ ^ n / ((f.eval z₀).abs * g.eval z₀)) ^ (n⁻¹ : ℂ) + z₀ in have hF₁ : F.eval z' = f.eval z₀ - f.eval z₀ * (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs, by simp only [F, cpow_nat_inv_pow _ hn0, div_eq_mul_inv, eval_pow, mul_assoc, mul_comm (g.eval z₀), mul_left_comm (g.eval z₀), mul_left_comm (g.eval z₀)⁻¹, mul_inv', inv_mul_cancel hg0, eval_C, eval_add, eval_neg, sub_eq_add_neg, eval_mul, eval_X, add_neg_cancel_right, neg_mul_eq_neg_mul_symm, mul_one, div_eq_mul_inv]; simp only [mul_comm, mul_left_comm, mul_assoc], have hδs : (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs < 1, by rw [div_eq_mul_inv, mul_right_comm, mul_comm, ← @inv_inv' _ _ (complex.abs _ * _), mul_inv', inv_inv', ← div_eq_mul_inv, div_lt_iff hfg0, one_mul]; calc δ ^ n ≤ δ ^ 1 : pow_le_pow_of_le_one (le_of_lt hδ0) hδ1 hn0 ... = δ : pow_one _ ... ≤ ((f.eval z₀).abs / (g.eval z₀).abs) / 2 : min_le_right _ _ ... < _ : half_lt_self hfg0, have hF₂ : (F.eval z').abs = (f.eval z₀).abs - (g.eval z₀).abs * δ ^ n, from calc (F.eval z').abs = (f.eval z₀ - f.eval z₀ * (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs).abs : congr_arg abs hF₁ ... = abs (f.eval z₀) * complex.abs (1 - (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs : ℝ) : by rw [← complex.abs_mul]; exact congr_arg complex.abs (by simp [mul_add, add_mul, mul_assoc, div_eq_mul_inv, sub_eq_add_neg]) ... = _ : by rw [complex.abs_of_nonneg (sub_nonneg.2 (le_of_lt hδs)), mul_sub, mul_div_cancel' _ (ne.symm (ne_of_lt hf0')), mul_one], have hef0 : abs (eval z₀ g) * (eval z₀ f).abs ≠ 0, from mul_ne_zero (mt complex.abs_eq_zero.1 hg0) (mt complex.abs_eq_zero.1 hf0), have hz'z₀ : abs (z' - z₀) = δ, by simp [z', mul_assoc, mul_left_comm _ (_ ^ n), mul_comm _ (_ ^ n), mul_comm (eval z₀ f).abs, _root_.mul_div_cancel _ hef0, of_real_mul, neg_mul_eq_neg_mul_symm, neg_div, is_absolute_value.abv_pow complex.abs, complex.abs_of_nonneg (le_of_lt hδ0), real.pow_nat_rpow_nat_inv (le_of_lt hδ0) hn0], have hF₃ : (f.eval z' - F.eval z').abs < (g.eval z₀).abs * δ ^ n, from calc (f.eval z' - F.eval z').abs = (g.eval z' - g.eval z₀).abs * (z' - z₀).abs ^ n : by rw [← eq_sub_iff_add_eq.1 hg, ← is_absolute_value.abv_pow complex.abs, ← complex.abs_mul, sub_mul]; simp [F, eval_pow, eval_add, eval_mul, eval_sub, eval_C, eval_X, eval_neg, add_sub_cancel, sub_eq_add_neg] ... = (g.eval z' - g.eval z₀).abs * δ ^ n : by rw hz'z₀ ... < _ : (mul_lt_mul_right (pow_pos hδ0 _)).2 (hδ _ hz'z₀), lt_irrefl (f.eval z₀).abs $ calc (f.eval z₀).abs ≤ (f.eval z').abs : hz₀ _ ... = (F.eval z' + (f.eval z' - F.eval z')).abs : by simp ... ≤ (F.eval z').abs + (f.eval z' - F.eval z').abs : complex.abs_add _ _ ... < (f.eval z₀).abs - (g.eval z₀).abs * δ ^ n + (g.eval z₀).abs * δ ^ n : add_lt_add_of_le_of_lt (by rw hF₂) hF₃ ... = (f.eval z₀).abs : sub_add_cancel _ _ end complex
c9f3e14e8b7b1bde21183fca9064a0e5fcb5dc59
aa5a655c05e5359a70646b7154e7cac59f0b4132
/stage0/src/Lean/Elab/InfoTree.lean
a7901c8903a1853cb97d38d5f6d548187dea4ef5
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,326
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Leonardo de Moura -/ import Lean.Data.Position import Lean.Expr import Lean.Message import Lean.Data.Json import Lean.Meta.Basic import Lean.Meta.PPGoal namespace Lean.Elab open Std (PersistentArray PersistentArray.empty PersistentHashMap) /- Context after executing `liftTermElabM`. Note that the term information collected during elaboration may contain metavariables, and their assignments are stored at `mctx`. -/ structure ContextInfo where env : Environment fileMap : FileMap mctx : MetavarContext := {} options : Options := {} currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] deriving Inhabited structure TermInfo where lctx : LocalContext -- The local context when the term was elaborated. expr : Expr stx : Syntax deriving Inhabited structure FieldInfo where name : Name lctx : LocalContext val : Expr stx : Syntax deriving Inhabited /- We store the list of goals before and after the execution of a tactic. We also store the metavariable context at each time since, we want to unassigned metavariables at tactic execution time to be displayed as `?m...`. -/ structure TacticInfo where mctxBefore : MetavarContext goalsBefore : List MVarId stx : Syntax mctxAfter : MetavarContext goalsAfter : List MVarId deriving Inhabited structure MacroExpansionInfo where lctx : LocalContext -- The local context when the macro was expanded. before : Syntax after : Syntax deriving Inhabited inductive Info where | ofTacticInfo (i : TacticInfo) | ofTermInfo (i : TermInfo) | ofMacroExpansionInfo (i : MacroExpansionInfo) | ofFieldInfo (i : FieldInfo) deriving Inhabited inductive InfoTree where | context (i : ContextInfo) (t : InfoTree) -- The context object is created by `liftTermElabM` at `Command.lean` | node (i : Info) (children : PersistentArray InfoTree) -- The children contains information for nested term elaboration and tactic evaluation | ofJson (j : Json) -- For user data | hole (mvarId : MVarId) -- The elaborator creates holes (aka metavariables) for tactics and postponed terms deriving Inhabited structure InfoState where enabled : Bool := false assignment : PersistentHashMap MVarId InfoTree := {} -- map from holeId to InfoTree trees : PersistentArray InfoTree := {} deriving Inhabited class MonadInfoTree (m : Type → Type) where getInfoState : m InfoState modifyInfoState : (InfoState → InfoState) → m Unit export MonadInfoTree (getInfoState modifyInfoState) instance (m n) [MonadLift m n] [MonadInfoTree m] : MonadInfoTree n where getInfoState := liftM (getInfoState : m _) modifyInfoState f := liftM (modifyInfoState f : m _) partial def InfoTree.substitute (tree : InfoTree) (assignment : PersistentHashMap MVarId InfoTree) : InfoTree := match tree with | node i c => node i <| c.map (substitute · assignment) | context i t => context i (substitute t assignment) | ofJson j => ofJson j | hole id => match assignment.find? id with | none => hole id | some tree => substitute tree assignment def ContextInfo.runMetaM (info : ContextInfo) (lctx : LocalContext) (x : MetaM α) : IO α := do let x := x.run { lctx := lctx } { mctx := info.mctx } let ((a, _), _) ← x.toIO { options := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls } { env := info.env } return a def ContextInfo.toPPContext (info : ContextInfo) (lctx : LocalContext) : PPContext := { env := info.env, mctx := info.mctx, lctx := lctx, opts := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls } def ContextInfo.ppSyntax (info : ContextInfo) (lctx : LocalContext) (stx : Syntax) : IO Format := do ppTerm (info.toPPContext lctx) stx private def formatStxRange (cinfo : ContextInfo) (stx : Syntax) : Format := do let pos := stx.getPos?.getD 0 let endPos := stx.getTailPos?.getD pos return f!"{fmtPos pos stx.getHeadInfo}-{fmtPos endPos stx.getTailInfo}" where fmtPos pos info := let pos := format <| cinfo.fileMap.toPosition pos match info with | SourceInfo.original .. => pos | _ => f!"{pos}†" def TermInfo.format (cinfo : ContextInfo) (info : TermInfo) : IO Format := do cinfo.runMetaM info.lctx do return f!"{← Meta.ppExpr info.expr} : {← Meta.ppExpr (← Meta.inferType info.expr)} @ {formatStxRange cinfo info.stx}" def FieldInfo.format (cinfo : ContextInfo) (info : FieldInfo) : IO Format := do cinfo.runMetaM info.lctx do return f!"{info.name} : {← Meta.ppExpr (← Meta.inferType info.val)} := {← Meta.ppExpr info.val} @ {formatStxRange cinfo info.stx}" def ContextInfo.ppGoals (cinfo : ContextInfo) (goals : List MVarId) : IO Format := if goals.isEmpty then return "no goals" else cinfo.runMetaM {} (return Std.Format.prefixJoin "\n" (← goals.mapM Meta.ppGoal)) def TacticInfo.format (cinfo : ContextInfo) (info : TacticInfo) : IO Format := do let cinfoB := { cinfo with mctx := info.mctxBefore } let cinfoA := { cinfo with mctx := info.mctxAfter } let goalsBefore ← cinfoB.ppGoals info.goalsBefore let goalsAfter ← cinfoA.ppGoals info.goalsAfter return f!"Tactic\nbefore {goalsBefore}\nafter {goalsAfter}" def MacroExpansionInfo.format (cinfo : ContextInfo) (info : MacroExpansionInfo) : IO Format := do let before ← cinfo.ppSyntax info.lctx info.before let after ← cinfo.ppSyntax info.lctx info.after return f!"Macro expansion\n{before}\n===>\n{after}" def Info.format (cinfo : ContextInfo) : Info → IO Format | ofTacticInfo i => i.format cinfo | ofTermInfo i => i.format cinfo | ofMacroExpansionInfo i => i.format cinfo | ofFieldInfo i => i.format cinfo partial def InfoTree.format (tree : InfoTree) (cinfo? : Option ContextInfo := none) : IO Format := do match tree with | ofJson j => return toString j | hole id => return toString id | context i t => format t i | node i cs => match cinfo? with | none => return "<context-not-available>" | some cinfo => if cs.size == 0 then i.format cinfo else return f!"{← i.format cinfo}{Std.Format.nestD <| Std.Format.prefixJoin "\n" (← cs.toList.mapM fun c => format c cinfo?)}" section variable [Monad m] [MonadInfoTree m] @[inline] private def modifyInfoTrees (f : PersistentArray InfoTree → PersistentArray InfoTree) : m Unit := modifyInfoState fun s => { s with trees := f s.trees } private def getResetInfoTrees : m (PersistentArray InfoTree) := do let trees := (← getInfoState).trees modifyInfoTrees fun _ => {} return trees def pushInfoTree (t : InfoTree) : m Unit := do if (← getInfoState).enabled then modifyInfoTrees fun ts => ts.push t def mkInfoNode (info : Info) : m Unit := do if (← getInfoState).enabled then modifyInfoTrees fun ts => PersistentArray.empty.push <| InfoTree.node info ts @[inline] def withInfoContext' [MonadFinally m] (x : m α) (mkInfo : α → m (Sum Info MVarId)) : m α := do if (← getInfoState).enabled then let treesSaved ← getResetInfoTrees Prod.fst <$> MonadFinally.tryFinally' x fun a? => do match a? with | none => modifyInfoTrees fun _ => treesSaved | some a => modifyInfoTrees fun trees => match (← mkInfo a) with | Sum.inl info => treesSaved.push <| InfoTree.node info trees | Sum.inr mvaId => treesSaved.push <| InfoTree.hole mvaId else x @[inline] def withInfoContext [MonadFinally m] (x : m α) (mkInfo : m Info) : m α := withInfoContext' x fun _ => return Sum.inl (← mkInfo) def getInfoHoleIdAssignment? (mvarId : MVarId) : m (Option InfoTree) := return (← getInfoState).assignment[mvarId] def assignInfoHoleId (mvarId : MVarId) (infoTree : InfoTree) : m Unit := do assert! (← getInfoHoleIdAssignment? mvarId).isNone modifyInfoState fun s => { s with assignment := s.assignment.insert mvarId infoTree } end def withMacroExpansionInfo [MonadFinally m] [Monad m] [MonadInfoTree m] [MonadLCtx m] (before after : Syntax) (x : m α) : m α := let mkInfo : m Info := do return Info.ofMacroExpansionInfo { lctx := (← getLCtx) before := before after := after } withInfoContext x mkInfo @[inline] def withInfoHole [MonadFinally m] [Monad m] [MonadInfoTree m] (mvarId : MVarId) (x : m α) : m α := do if (← getInfoState).enabled then let treesSaved ← getResetInfoTrees Prod.fst <$> MonadFinally.tryFinally' x fun a? => do match a? with | none => modifyInfoTrees fun _ => treesSaved | some a => modifyInfoState fun s => assert! s.trees.size == 1 -- if size is not one, then API is being misused. { s with trees := treesSaved, assignment := s.assignment.insert mvarId s.trees[0] } else x def enableInfoTree [MonadInfoTree m] (flag := true) : m Unit := modifyInfoState fun s => { s with enabled := flag } def getInfoTrees [MonadInfoTree m] [Monad m] : m (PersistentArray InfoTree) := return (← getInfoState).trees end Lean.Elab
4e4d4778c8b6344823ca1d17d47f78594d8bc311
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/discriminant.lean
57849e8c1f9820a4cd520aaa7b7721f63fc228fb
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
16,533
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import ring_theory.trace import ring_theory.norm import number_theory.number_field.basic /-! # Discriminant of a family of vectors > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Given an `A`-algebra `B` and `b`, an `ι`-indexed family of elements of `B`, we define the *discriminant* of `b` as the determinant of the matrix whose `(i j)`-th element is the trace of `b i * b j`. ## Main definition * `algebra.discr A b` : the discriminant of `b : ι → B`. ## Main results * `algebra.discr_zero_of_not_linear_independent` : if `b` is not linear independent, then `algebra.discr A b = 0`. * `algebra.discr_of_matrix_vec_mul` and `discr_of_matrix_mul_vec` : formulas relating `algebra.discr A ι b` with `algebra.discr A ((P.map (algebra_map A B)).vec_mul b)` and `algebra.discr A ((P.map (algebra_map A B)).mul_vec b)`. * `algebra.discr_not_zero_of_basis` : over a field, if `b` is a basis, then `algebra.discr K b ≠ 0`. * `algebra.discr_eq_det_embeddings_matrix_reindex_pow_two` : if `L/K` is a field extension and `b : ι → L`, then `discr K b` is the square of the determinant of the matrix whose `(i, j)` coefficient is `σⱼ (b i)`, where `σⱼ : L →ₐ[K] E` is the embedding in an algebraically closed field `E` corresponding to `j : ι` via a bijection `e : ι ≃ (L →ₐ[K] E)`. * `algebra.discr_of_power_basis_eq_prod` : the discriminant of a power basis. * `discr_is_integral` : if `K` and `L` are fields and `is_scalar_tower R K L`, is `b : ι → L` satisfies ` ∀ i, is_integral R (b i)`, then `is_integral R (discr K b)`. * `discr_mul_is_integral_mem_adjoin` : let `K` be the fraction field of an integrally closed domain `R` and let `L` be a finite separable extension of `K`. Let `B : power_basis K L` be such that `is_integral R B.gen`. Then for all, `z : L` we have `(discr K B.basis) • z ∈ adjoin R ({B.gen} : set L)`. ## Implementation details Our definition works for any `A`-algebra `B`, but note that if `B` is not free as an `A`-module, then `trace A B = 0` by definition, so `discr A b = 0` for any `b`. -/ universes u v w z open_locale matrix big_operators open matrix finite_dimensional fintype polynomial finset intermediate_field namespace algebra variables (A : Type u) {B : Type v} (C : Type z) {ι : Type w} variables [comm_ring A] [comm_ring B] [algebra A B] [comm_ring C] [algebra A C] section discr /-- Given an `A`-algebra `B` and `b`, an `ι`-indexed family of elements of `B`, we define `discr A ι b` as the determinant of `trace_matrix A ι b`. -/ noncomputable def discr (A : Type u) {B : Type v} [comm_ring A] [comm_ring B] [algebra A B] [fintype ι] (b : ι → B) := by { classical, exact (trace_matrix A b).det } lemma discr_def [decidable_eq ι] [fintype ι] (b : ι → B) : discr A b = (trace_matrix A b).det := by convert rfl variables {ι' : Type*} [fintype ι'] [fintype ι] section basic @[simp] lemma discr_reindex (b : basis ι A B) (f : ι ≃ ι') : discr A (b ∘ ⇑(f.symm)) = discr A b := begin classical, rw [← basis.coe_reindex, discr_def, trace_matrix_reindex, det_reindex_self, ← discr_def] end /-- If `b` is not linear independent, then `algebra.discr A b = 0`. -/ lemma discr_zero_of_not_linear_independent [is_domain A] {b : ι → B} (hli : ¬linear_independent A b) : discr A b = 0 := begin classical, obtain ⟨g, hg, i, hi⟩ := fintype.not_linear_independent_iff.1 hli, have : (trace_matrix A b).mul_vec g = 0, { ext i, have : ∀ j, (trace A B) (b i * b j) * g j = (trace A B) (((g j) • (b j)) * b i), { intro j, simp [mul_comm], }, simp only [mul_vec, dot_product, trace_matrix_apply, pi.zero_apply, trace_form_apply, λ j, this j, ← linear_map.map_sum, ← sum_mul, hg, zero_mul, linear_map.map_zero] }, by_contra h, rw discr_def at h, simpa [matrix.eq_zero_of_mul_vec_eq_zero h this] using hi, end variable {A} /-- Relation between `algebra.discr A ι b` and `algebra.discr A ((P.map (algebra_map A B)).vec_mul b)`. -/ lemma discr_of_matrix_vec_mul [decidable_eq ι] (b : ι → B) (P : matrix ι ι A) : discr A ((P.map (algebra_map A B)).vec_mul b) = P.det ^ 2 * discr A b := by rw [discr_def, trace_matrix_of_matrix_vec_mul, det_mul, det_mul, det_transpose, mul_comm, ← mul_assoc, discr_def, pow_two] /-- Relation between `algebra.discr A ι b` and `algebra.discr A ((P.map (algebra_map A B)).mul_vec b)`. -/ lemma discr_of_matrix_mul_vec [decidable_eq ι] (b : ι → B) (P : matrix ι ι A) : discr A ((P.map (algebra_map A B)).mul_vec b) = P.det ^ 2 * discr A b := by rw [discr_def, trace_matrix_of_matrix_mul_vec, det_mul, det_mul, det_transpose, mul_comm, ← mul_assoc, discr_def, pow_two] end basic section field variables (K : Type u) {L : Type v} (E : Type z) [field K] [field L] [field E] variables [algebra K L] [algebra K E] variables [module.finite K L] [is_alg_closed E] /-- Over a field, if `b` is a basis, then `algebra.discr K b ≠ 0`. -/ lemma discr_not_zero_of_basis [is_separable K L] (b : basis ι K L) : discr K b ≠ 0 := begin casesI is_empty_or_nonempty ι, { simp [discr] }, { have := span_eq_top_of_linear_independent_of_card_eq_finrank b.linear_independent (finrank_eq_card_basis b).symm, classical, rw [discr_def, trace_matrix], simp_rw [← basis.mk_apply b.linear_independent this.ge], rw [← trace_matrix, trace_matrix_of_basis, ← bilin_form.nondegenerate_iff_det_ne_zero], exact trace_form_nondegenerate _ _ }, end /-- Over a field, if `b` is a basis, then `algebra.discr K b` is a unit. -/ lemma discr_is_unit_of_basis [is_separable K L] (b : basis ι K L) : is_unit (discr K b) := is_unit.mk0 _ (discr_not_zero_of_basis _ _) variables (b : ι → L) (pb : power_basis K L) /-- If `L/K` is a field extension and `b : ι → L`, then `discr K b` is the square of the determinant of the matrix whose `(i, j)` coefficient is `σⱼ (b i)`, where `σⱼ : L →ₐ[K] E` is the embedding in an algebraically closed field `E` corresponding to `j : ι` via a bijection `e : ι ≃ (L →ₐ[K] E)`. -/ lemma discr_eq_det_embeddings_matrix_reindex_pow_two [decidable_eq ι] [is_separable K L] (e : ι ≃ (L →ₐ[K] E)) : algebra_map K E (discr K b) = (embeddings_matrix_reindex K E b e).det ^ 2 := by rw [discr_def, ring_hom.map_det, ring_hom.map_matrix_apply, trace_matrix_eq_embeddings_matrix_reindex_mul_trans, det_mul, det_transpose, pow_two] /-- The discriminant of a power basis. -/ lemma discr_power_basis_eq_prod (e : fin pb.dim ≃ (L →ₐ[K] E)) [is_separable K L] : algebra_map K E (discr K pb.basis) = ∏ i : fin pb.dim, ∏ j in Ioi i, (e j pb.gen- (e i pb.gen)) ^ 2 := begin rw [discr_eq_det_embeddings_matrix_reindex_pow_two K E pb.basis e, embeddings_matrix_reindex_eq_vandermonde, det_transpose, det_vandermonde, ← prod_pow], congr, ext i, rw [← prod_pow] end /-- A variation of `of_power_basis_eq_prod`. -/ lemma discr_power_basis_eq_prod' [is_separable K L] (e : fin pb.dim ≃ (L →ₐ[K] E)) : algebra_map K E (discr K pb.basis) = ∏ i : fin pb.dim, ∏ j in Ioi i, -((e j pb.gen - e i pb.gen) * (e i pb.gen - e j pb.gen)) := begin rw [discr_power_basis_eq_prod _ _ _ e], congr, ext i, congr, ext j, ring end local notation `n` := finrank K L /-- A variation of `of_power_basis_eq_prod`. -/ lemma discr_power_basis_eq_prod'' [is_separable K L] (e : fin pb.dim ≃ (L →ₐ[K] E)) : algebra_map K E (discr K pb.basis) = (-1) ^ (n * (n - 1) / 2) * ∏ i : fin pb.dim, ∏ j in Ioi i, (e j pb.gen - e i pb.gen) * (e i pb.gen - e j pb.gen) := begin rw [discr_power_basis_eq_prod' _ _ _ e], simp_rw [λ i j, neg_eq_neg_one_mul ((e j pb.gen- (e i pb.gen)) * (e i pb.gen- (e j pb.gen))), prod_mul_distrib], congr, simp only [prod_pow_eq_pow_sum, prod_const], congr, rw [← @nat.cast_inj ℚ, nat.cast_sum], have : ∀ (x : fin pb.dim), (↑x + 1) ≤ pb.dim := by simp [nat.succ_le_iff, fin.is_lt], simp_rw [fin.card_Ioi, nat.sub_sub, add_comm 1], simp only [nat.cast_sub, this, finset.card_fin, nsmul_eq_mul, sum_const, sum_sub_distrib, nat.cast_add, nat.cast_one, sum_add_distrib, mul_one], rw [← nat.cast_sum, ← @finset.sum_range ℕ _ pb.dim (λ i, i), sum_range_id ], have hn : n = pb.dim, { rw [← alg_hom.card K L E, ← fintype.card_fin pb.dim], exact card_congr (equiv.symm e) }, have h₂ : 2 ∣ (pb.dim * (pb.dim - 1)) := even_iff_two_dvd.1 (nat.even_mul_self_pred _), have hne : ((2 : ℕ) : ℚ) ≠ 0 := by simp, have hle : 1 ≤ pb.dim, { rw [← hn, nat.one_le_iff_ne_zero, ← zero_lt_iff, finite_dimensional.finrank_pos_iff], apply_instance }, rw [hn, nat.cast_div h₂ hne, nat.cast_mul, nat.cast_sub hle], field_simp, ring, end /-- Formula for the discriminant of a power basis using the norm of the field extension. -/ lemma discr_power_basis_eq_norm [is_separable K L] : discr K pb.basis = (-1) ^ (n * (n - 1) / 2) * (norm K (aeval pb.gen (minpoly K pb.gen).derivative)) := begin let E := algebraic_closure L, letI := λ (a b : E), classical.prop_decidable (eq a b), have e : fin pb.dim ≃ (L →ₐ[K] E), { refine equiv_of_card_eq _, rw [fintype.card_fin, alg_hom.card], exact (power_basis.finrank pb).symm }, have hnodup : (map (algebra_map K E) (minpoly K pb.gen)).roots.nodup := nodup_roots (separable.map (is_separable.separable K pb.gen)), have hroots : ∀ σ : L →ₐ[K] E, σ pb.gen ∈ (map (algebra_map K E) (minpoly K pb.gen)).roots, { intro σ, rw [mem_roots, is_root.def, eval_map, ← aeval_def, aeval_alg_hom_apply], repeat { simp [minpoly.ne_zero (is_separable.is_integral K pb.gen)] } }, apply (algebra_map K E).injective, rw [ring_hom.map_mul, ring_hom.map_pow, ring_hom.map_neg, ring_hom.map_one, discr_power_basis_eq_prod'' _ _ _ e], congr, rw [norm_eq_prod_embeddings, prod_prod_Ioi_mul_eq_prod_prod_off_diag], conv_rhs { congr, skip, funext, rw [← aeval_alg_hom_apply, aeval_root_derivative_of_splits (minpoly.monic (is_separable.is_integral K pb.gen)) (is_alg_closed.splits_codomain _) (hroots σ), ← finset.prod_mk _ (hnodup.erase _)] }, rw [prod_sigma', prod_sigma'], refine prod_bij (λ i hi, ⟨e i.2, e i.1 pb.gen⟩) (λ i hi, _) (λ i hi, by simp at hi) (λ i j hi hj hij, _) (λ σ hσ, _), { simp only [true_and, finset.mem_mk, mem_univ, mem_sigma], rw [multiset.mem_erase_of_ne (λ h, _)], { exact hroots _ }, { simp only [true_and, mem_univ, ne.def, mem_sigma, mem_compl, mem_singleton] at hi, rw [← power_basis.lift_equiv_apply_coe, ← power_basis.lift_equiv_apply_coe] at h, exact hi (e.injective $ pb.lift_equiv.injective $ subtype.eq h.symm) } }, { simp only [equiv.apply_eq_iff_eq, heq_iff_eq] at hij, have h := hij.2, rw [← power_basis.lift_equiv_apply_coe, ← power_basis.lift_equiv_apply_coe] at h, refine sigma.eq (equiv.injective e (equiv.injective _ (subtype.eq h))) (by simp [hij.1]) }, { simp only [true_and, finset.mem_mk, mem_univ, mem_sigma] at ⊢ hσ, simp only [sigma.exists, exists_prop, mem_compl, mem_singleton, ne.def], refine ⟨e.symm (power_basis.lift pb σ.2 _), e.symm σ.1, ⟨λ h, _, sigma.eq _ _⟩⟩, { rw [aeval_def, eval₂_eq_eval_map, ← is_root.def, ← mem_roots], { exact multiset.erase_subset _ _ hσ }, { simp [minpoly.ne_zero (is_separable.is_integral K pb.gen)] } }, { replace h := alg_hom.congr_fun (equiv.injective _ h) pb.gen, rw [power_basis.lift_gen] at h, rw [← h] at hσ, exact hnodup.not_mem_erase hσ }, all_goals { simp } } end section integral variables {R : Type z} [comm_ring R] [algebra R K] [algebra R L] [is_scalar_tower R K L] /-- If `K` and `L` are fields and `is_scalar_tower R K L`, and `b : ι → L` satisfies ` ∀ i, is_integral R (b i)`, then `is_integral R (discr K b)`. -/ lemma discr_is_integral {b : ι → L} (h : ∀ i, is_integral R (b i)) : is_integral R (discr K b) := begin classical, rw [discr_def], exact is_integral.det (λ i j, is_integral_trace (is_integral_mul (h i) (h j))) end /-- If `b` and `b'` are `ℚ`-bases of a number field `K` such that `∀ i j, is_integral ℤ (b.to_matrix b' i j)` and `∀ i j, is_integral ℤ (b'.to_matrix b i j)` then `discr ℚ b = discr ℚ b'`. -/ lemma discr_eq_discr_of_to_matrix_coeff_is_integral [number_field K] {b : basis ι ℚ K} {b' : basis ι' ℚ K} (h : ∀ i j, is_integral ℤ (b.to_matrix b' i j)) (h' : ∀ i j, is_integral ℤ (b'.to_matrix b i j)) : discr ℚ b = discr ℚ b' := begin replace h' : ∀ i j, is_integral ℤ (b'.to_matrix ((b.reindex (b.index_equiv b'))) i j), { intros i j, convert h' i ((b.index_equiv b').symm j), simpa }, classical, rw [← (b.reindex (b.index_equiv b')).to_matrix_map_vec_mul b', discr_of_matrix_vec_mul, ← one_mul (discr ℚ b), basis.coe_reindex, discr_reindex], congr, have hint : is_integral ℤ (((b.reindex (b.index_equiv b')).to_matrix b').det) := is_integral.det (λ i j, h _ _), obtain ⟨r, hr⟩ := is_integrally_closed.is_integral_iff.1 hint, have hunit : is_unit r, { have : is_integral ℤ ((b'.to_matrix (b.reindex (b.index_equiv b'))).det) := is_integral.det (λ i j, h' _ _), obtain ⟨r', hr'⟩ := is_integrally_closed.is_integral_iff.1 this, refine is_unit_iff_exists_inv.2 ⟨r', _⟩, suffices : algebra_map ℤ ℚ (r * r') = 1, { rw [← ring_hom.map_one (algebra_map ℤ ℚ)] at this, exact (is_fraction_ring.injective ℤ ℚ) this }, rw [ring_hom.map_mul, hr, hr', ← det_mul, basis.to_matrix_mul_to_matrix_flip, det_one] }, rw [← ring_hom.map_one (algebra_map ℤ ℚ), ← hr], cases int.is_unit_iff.1 hunit with hp hm, { simp [hp] }, { simp [hm] } end /-- Let `K` be the fraction field of an integrally closed domain `R` and let `L` be a finite separable extension of `K`. Let `B : power_basis K L` be such that `is_integral R B.gen`. Then for all, `z : L` that are integral over `R`, we have `(discr K B.basis) • z ∈ adjoin R ({B.gen} : set L)`. -/ lemma discr_mul_is_integral_mem_adjoin [is_domain R] [is_separable K L] [is_integrally_closed R] [is_fraction_ring R K] {B : power_basis K L} (hint : is_integral R B.gen) {z : L} (hz : is_integral R z) : (discr K B.basis) • z ∈ adjoin R ({B.gen} : set L) := begin have hinv : is_unit (trace_matrix K B.basis).det := by simpa [← discr_def] using discr_is_unit_of_basis _ B.basis, have H : (trace_matrix K B.basis).det • (trace_matrix K B.basis).mul_vec (B.basis.equiv_fun z) = (trace_matrix K B.basis).det • (λ i, trace K L (z * B.basis i)), { congr, exact trace_matrix_of_basis_mul_vec _ _ }, have cramer := mul_vec_cramer (trace_matrix K B.basis) (λ i, trace K L (z * B.basis i)), suffices : ∀ i, ((trace_matrix K B.basis).det • (B.basis.equiv_fun z)) i ∈ (⊥ : subalgebra R K), { rw [← B.basis.sum_repr z, finset.smul_sum], refine subalgebra.sum_mem _ (λ i hi, _), replace this := this i, rw [← discr_def, pi.smul_apply, mem_bot] at this, obtain ⟨r, hr⟩ := this, rw [basis.equiv_fun_apply] at hr, rw [← smul_assoc, ← hr, algebra_map_smul], refine subalgebra.smul_mem _ _ _, rw [B.basis_eq_pow i], refine subalgebra.pow_mem _ (subset_adjoin (set.mem_singleton _)) _}, intro i, rw [← H, ← mul_vec_smul] at cramer, replace cramer := congr_arg (mul_vec (trace_matrix K B.basis)⁻¹) cramer, rw [mul_vec_mul_vec, nonsing_inv_mul _ hinv, mul_vec_mul_vec, nonsing_inv_mul _ hinv, one_mul_vec, one_mul_vec] at cramer, rw [← congr_fun cramer i, cramer_apply, det_apply], refine subalgebra.sum_mem _ (λ σ _, subalgebra.zsmul_mem _ (subalgebra.prod_mem _ (λ j _, _)) _), by_cases hji : j = i, { simp only [update_column_apply, hji, eq_self_iff_true, power_basis.coe_basis], exact mem_bot.2 (is_integrally_closed.is_integral_iff.1 $ is_integral_trace $ is_integral_mul hz $ is_integral.pow hint _) }, { simp only [update_column_apply, hji, power_basis.coe_basis], exact mem_bot.2 (is_integrally_closed.is_integral_iff.1 $ is_integral_trace $ is_integral_mul (is_integral.pow hint _) (is_integral.pow hint _)) } end end integral end field end discr end algebra
e7d55c31ed60a5b977f67fe0fe6badcff83d7806
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/complex/module.lean
e5287b5b3742bec9879e02f149fd15ecdc32170a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
16,514
lean
/- Copyright (c) 2020 Alexander Bentkamp, Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Sébastien Gouëzel, Eric Wieser -/ import linear_algebra.orientation import algebra.order.smul import data.complex.basic import data.fin.vec_notation import field_theory.tower import algebra.char_p.invertible /-! # Complex number as a vector space over `ℝ` This file contains the following instances: * Any `•`-structure (`has_smul`, `mul_action`, `distrib_mul_action`, `module`, `algebra`) on `ℝ` imbues a corresponding structure on `ℂ`. This includes the statement that `ℂ` is an `ℝ` algebra. * any complex vector space is a real vector space; * any finite dimensional complex vector space is a finite dimensional real vector space; * the space of `ℝ`-linear maps from a real vector space to a complex vector space is a complex vector space. It also defines bundled versions of four standard maps (respectively, the real part, the imaginary part, the embedding of `ℝ` in `ℂ`, and the complex conjugate): * `complex.re_lm` (`ℝ`-linear map); * `complex.im_lm` (`ℝ`-linear map); * `complex.of_real_am` (`ℝ`-algebra (homo)morphism); * `complex.conj_ae` (`ℝ`-algebra equivalence). It also provides a universal property of the complex numbers `complex.lift`, which constructs a `ℂ →ₐ[ℝ] A` into any `ℝ`-algebra `A` given a square root of `-1`. In addition, this file provides a decomposition into `real_part` and `imaginary_part` for any element of a `star_module` over `ℂ`. ## Notation * `ℜ` and `ℑ` for the `real_part` and `imaginary_part`, respectively, in the locale `complex_star_module`. -/ namespace complex open_locale complex_conjugate variables {R : Type*} {S : Type*} section variables [has_smul R ℝ] /- The useless `0` multiplication in `smul` is to make sure that `restrict_scalars.module ℝ ℂ ℂ = complex.module` definitionally. -/ instance : has_smul R ℂ := { smul := λ r x, ⟨r • x.re - 0 * x.im, r • x.im + 0 * x.re⟩ } lemma smul_re (r : R) (z : ℂ) : (r • z).re = r • z.re := by simp [(•)] lemma smul_im (r : R) (z : ℂ) : (r • z).im = r • z.im := by simp [(•)] @[simp] lemma real_smul {x : ℝ} {z : ℂ} : x • z = x * z := rfl end instance [has_smul R ℝ] [has_smul S ℝ] [smul_comm_class R S ℝ] : smul_comm_class R S ℂ := { smul_comm := λ r s x, by ext; simp [smul_re, smul_im, smul_comm] } instance [has_smul R S] [has_smul R ℝ] [has_smul S ℝ] [is_scalar_tower R S ℝ] : is_scalar_tower R S ℂ := { smul_assoc := λ r s x, by ext; simp [smul_re, smul_im, smul_assoc] } instance [has_smul R ℝ] [has_smul Rᵐᵒᵖ ℝ] [is_central_scalar R ℝ] : is_central_scalar R ℂ := { op_smul_eq_smul := λ r x, by ext; simp [smul_re, smul_im, op_smul_eq_smul] } instance [monoid R] [mul_action R ℝ] : mul_action R ℂ := { one_smul := λ x, by ext; simp [smul_re, smul_im, one_smul], mul_smul := λ r s x, by ext; simp [smul_re, smul_im, mul_smul] } instance [distrib_smul R ℝ] : distrib_smul R ℂ := { smul_add := λ r x y, by ext; simp [smul_re, smul_im, smul_add], smul_zero := λ r, by ext; simp [smul_re, smul_im, smul_zero] } instance [semiring R] [distrib_mul_action R ℝ] : distrib_mul_action R ℂ := { ..complex.distrib_smul } instance [semiring R] [module R ℝ] : module R ℂ := { add_smul := λ r s x, by ext; simp [smul_re, smul_im, add_smul], zero_smul := λ r, by ext; simp [smul_re, smul_im, zero_smul] } instance [comm_semiring R] [algebra R ℝ] : algebra R ℂ := { smul := (•), smul_def' := λ r x, by ext; simp [smul_re, smul_im, algebra.smul_def], commutes' := λ r ⟨xr, xi⟩, by ext; simp [smul_re, smul_im, algebra.commutes], ..complex.of_real.comp (algebra_map R ℝ) } instance : star_module ℝ ℂ := ⟨λ r x, by simp only [star_def, star_trivial, real_smul, map_mul, conj_of_real]⟩ @[simp] lemma coe_algebra_map : (algebra_map ℝ ℂ : ℝ → ℂ) = coe := rfl section variables {A : Type*} [semiring A] [algebra ℝ A] /-- We need this lemma since `complex.coe_algebra_map` diverts the simp-normal form away from `alg_hom.commutes`. -/ @[simp] lemma _root_.alg_hom.map_coe_real_complex (f : ℂ →ₐ[ℝ] A) (x : ℝ) : f x = algebra_map ℝ A x := f.commutes x /-- Two `ℝ`-algebra homomorphisms from ℂ are equal if they agree on `complex.I`. -/ @[ext] lemma alg_hom_ext ⦃f g : ℂ →ₐ[ℝ] A⦄ (h : f I = g I) : f = g := begin ext ⟨x, y⟩, simp only [mk_eq_add_mul_I, alg_hom.map_add, alg_hom.map_coe_real_complex, alg_hom.map_mul, h] end end section open_locale complex_order protected lemma ordered_smul : ordered_smul ℝ ℂ := ordered_smul.mk' $ λ a b r hab hr, ⟨by simp [hr, hab.1.le], by simp [hab.2]⟩ localized "attribute [instance] complex.ordered_smul" in complex_order end open submodule finite_dimensional /-- `ℂ` has a basis over `ℝ` given by `1` and `I`. -/ noncomputable def basis_one_I : basis (fin 2) ℝ ℂ := basis.of_equiv_fun { to_fun := λ z, ![z.re, z.im], inv_fun := λ c, c 0 + c 1 • I, left_inv := λ z, by simp, right_inv := λ c, by { ext i, fin_cases i; simp }, map_add' := λ z z', by simp, -- why does `simp` not know how to apply `smul_cons`, which is a `@[simp]` lemma, here? map_smul' := λ c z, by simp [matrix.smul_cons c z.re, matrix.smul_cons c z.im] } @[simp] lemma coe_basis_one_I_repr (z : ℂ) : ⇑(basis_one_I.repr z) = ![z.re, z.im] := rfl @[simp] lemma coe_basis_one_I : ⇑basis_one_I = ![1, I] := funext $ λ i, basis.apply_eq_iff.mpr $ finsupp.ext $ λ j, by fin_cases i; fin_cases j; simp only [coe_basis_one_I_repr, finsupp.single_eq_same, finsupp.single_eq_of_ne, matrix.cons_val_zero, matrix.cons_val_one, matrix.head_cons, nat.one_ne_zero, fin.one_eq_zero_iff, fin.zero_eq_one_iff, ne.def, not_false_iff, one_re, one_im, I_re, I_im] instance : finite_dimensional ℝ ℂ := of_fintype_basis basis_one_I @[simp] lemma finrank_real_complex : finite_dimensional.finrank ℝ ℂ = 2 := by rw [finrank_eq_card_basis basis_one_I, fintype.card_fin] @[simp] lemma dim_real_complex : module.rank ℝ ℂ = 2 := by simp [← finrank_eq_dim, finrank_real_complex] lemma {u} dim_real_complex' : cardinal.lift.{u} (module.rank ℝ ℂ) = 2 := by simp [← finrank_eq_dim, finrank_real_complex, bit0] /-- `fact` version of the dimension of `ℂ` over `ℝ`, locally useful in the definition of the circle. -/ lemma finrank_real_complex_fact : fact (finrank ℝ ℂ = 2) := ⟨finrank_real_complex⟩ /-- The standard orientation on `ℂ`. -/ protected noncomputable def orientation : orientation ℝ ℂ (fin 2) := complex.basis_one_I.orientation end complex /- Register as an instance (with low priority) the fact that a complex vector space is also a real vector space. -/ @[priority 900] instance module.complex_to_real (E : Type*) [add_comm_group E] [module ℂ E] : module ℝ E := restrict_scalars.module ℝ ℂ E instance module.real_complex_tower (E : Type*) [add_comm_group E] [module ℂ E] : is_scalar_tower ℝ ℂ E := restrict_scalars.is_scalar_tower ℝ ℂ E @[simp, norm_cast] lemma complex.coe_smul {E : Type*} [add_comm_group E] [module ℂ E] (x : ℝ) (y : E) : (x : ℂ) • y = x • y := rfl /-- The scalar action of `ℝ` on a `ℂ`-module `E` induced by `module.complex_to_real` commutes with another scalar action of `M` on `E` whenever the action of `ℂ` commutes with the action of `M`. -/ @[priority 900] instance smul_comm_class.complex_to_real {M E : Type*} [add_comm_group E] [module ℂ E] [has_smul M E] [smul_comm_class ℂ M E] : smul_comm_class ℝ M E := { smul_comm := λ r _ _, (smul_comm (r : ℂ) _ _ : _) } @[priority 100] instance finite_dimensional.complex_to_real (E : Type*) [add_comm_group E] [module ℂ E] [finite_dimensional ℂ E] : finite_dimensional ℝ E := finite_dimensional.trans ℝ ℂ E lemma dim_real_of_complex (E : Type*) [add_comm_group E] [module ℂ E] : module.rank ℝ E = 2 * module.rank ℂ E := cardinal.lift_inj.1 $ by { rw [← dim_mul_dim' ℝ ℂ E, complex.dim_real_complex], simp [bit0] } lemma finrank_real_of_complex (E : Type*) [add_comm_group E] [module ℂ E] : finite_dimensional.finrank ℝ E = 2 * finite_dimensional.finrank ℂ E := by rw [← finite_dimensional.finrank_mul_finrank ℝ ℂ E, complex.finrank_real_complex] @[priority 900] instance star_module.complex_to_real {E : Type*} [add_comm_group E] [has_star E] [module ℂ E] [star_module ℂ E] : star_module ℝ E := ⟨λ r a, by rw [star_trivial r, restrict_scalars_smul_def, restrict_scalars_smul_def, star_smul, complex.coe_algebra_map, complex.star_def, complex.conj_of_real]⟩ namespace complex open_locale complex_conjugate /-- Linear map version of the real part function, from `ℂ` to `ℝ`. -/ def re_lm : ℂ →ₗ[ℝ] ℝ := { to_fun := λx, x.re, map_add' := add_re, map_smul' := by simp, } @[simp] lemma re_lm_coe : ⇑re_lm = re := rfl /-- Linear map version of the imaginary part function, from `ℂ` to `ℝ`. -/ def im_lm : ℂ →ₗ[ℝ] ℝ := { to_fun := λx, x.im, map_add' := add_im, map_smul' := by simp, } @[simp] lemma im_lm_coe : ⇑im_lm = im := rfl /-- `ℝ`-algebra morphism version of the canonical embedding of `ℝ` in `ℂ`. -/ def of_real_am : ℝ →ₐ[ℝ] ℂ := algebra.of_id ℝ ℂ @[simp] lemma of_real_am_coe : ⇑of_real_am = coe := rfl /-- `ℝ`-algebra isomorphism version of the complex conjugation function from `ℂ` to `ℂ` -/ def conj_ae : ℂ ≃ₐ[ℝ] ℂ := { inv_fun := conj, left_inv := star_star, right_inv := star_star, commutes' := conj_of_real, .. conj } @[simp] lemma conj_ae_coe : ⇑conj_ae = conj := rfl /-- The matrix representation of `conj_ae`. -/ @[simp] lemma to_matrix_conj_ae : linear_map.to_matrix basis_one_I basis_one_I conj_ae.to_linear_map = !![1, 0; 0, -1] := begin ext i j, simp [linear_map.to_matrix_apply], fin_cases i; fin_cases j; simp end /-- The identity and the complex conjugation are the only two `ℝ`-algebra homomorphisms of `ℂ`. -/ lemma real_alg_hom_eq_id_or_conj (f : ℂ →ₐ[ℝ] ℂ) : f = alg_hom.id ℝ ℂ ∨ f = conj_ae := begin refine (eq_or_eq_neg_of_sq_eq_sq (f I) I $ by rw [← map_pow, I_sq, map_neg, map_one]).imp _ _; refine λ h, alg_hom_ext _, exacts [h, conj_I.symm ▸ h], end section lift variables {A : Type*} [ring A] [algebra ℝ A] /-- There is an alg_hom from `ℂ` to any `ℝ`-algebra with an element that squares to `-1`. See `complex.lift` for this as an equiv. -/ def lift_aux (I' : A) (hf : I' * I' = -1) : ℂ →ₐ[ℝ] A := alg_hom.of_linear_map ((algebra.of_id ℝ A).to_linear_map.comp re_lm + (linear_map.to_span_singleton _ _ I').comp im_lm) (show algebra_map ℝ A 1 + (0 : ℝ) • I' = 1, by rw [ring_hom.map_one, zero_smul, add_zero]) (λ ⟨x₁, y₁⟩ ⟨x₂, y₂⟩, show algebra_map ℝ A (x₁ * x₂ - y₁ * y₂) + (x₁ * y₂ + y₁ * x₂) • I' = (algebra_map ℝ A x₁ + y₁ • I') * (algebra_map ℝ A x₂ + y₂ • I'), begin rw [add_mul, mul_add, mul_add, add_comm _ (y₁ • I' * y₂ • I'), add_add_add_comm], congr' 1, -- equate "real" and "imaginary" parts { rw [smul_mul_smul, hf, smul_neg, ←algebra.algebra_map_eq_smul_one, ←sub_eq_add_neg, ←ring_hom.map_mul, ←ring_hom.map_sub], }, { rw [algebra.smul_def, algebra.smul_def, algebra.smul_def, ←algebra.right_comm _ x₂, ←mul_assoc, ←add_mul, ←ring_hom.map_mul, ←ring_hom.map_mul, ←ring_hom.map_add] } end) @[simp] lemma lift_aux_apply (I' : A) (hI') (z : ℂ) : lift_aux I' hI' z = algebra_map ℝ A z.re + z.im • I' := rfl lemma lift_aux_apply_I (I' : A) (hI') : lift_aux I' hI' I = I' := by simp /-- A universal property of the complex numbers, providing a unique `ℂ →ₐ[ℝ] A` for every element of `A` which squares to `-1`. This can be used to embed the complex numbers in the `quaternion`s. This isomorphism is named to match the very similar `zsqrtd.lift`. -/ @[simps {simp_rhs := tt}] def lift : {I' : A // I' * I' = -1} ≃ (ℂ →ₐ[ℝ] A) := { to_fun := λ I', lift_aux I' I'.prop, inv_fun := λ F, ⟨F I, by rw [←F.map_mul, I_mul_I, alg_hom.map_neg, alg_hom.map_one]⟩, left_inv := λ I', subtype.ext $ lift_aux_apply_I I' I'.prop, right_inv := λ F, alg_hom_ext $ lift_aux_apply_I _ _, } /- When applied to `complex.I` itself, `lift` is the identity. -/ @[simp] lemma lift_aux_I : lift_aux I I_mul_I = alg_hom.id ℝ ℂ := alg_hom_ext $ lift_aux_apply_I _ _ /- When applied to `-complex.I`, `lift` is conjugation, `conj`. -/ @[simp] lemma lift_aux_neg_I : lift_aux (-I) ((neg_mul_neg _ _).trans I_mul_I) = conj_ae := alg_hom_ext $ (lift_aux_apply_I _ _).trans conj_I.symm end lift end complex section real_imaginary_part open complex variables {A : Type*} [add_comm_group A] [module ℂ A] [star_add_monoid A] [star_module ℂ A] /-- Create a `self_adjoint` element from a `skew_adjoint` element by multiplying by the scalar `-complex.I`. -/ @[simps] def skew_adjoint.neg_I_smul : skew_adjoint A →ₗ[ℝ] self_adjoint A := { to_fun := λ a, ⟨-I • a, by simp only [self_adjoint.mem_iff, neg_smul, star_neg, star_smul, star_def, conj_I, skew_adjoint.star_coe_eq, neg_smul_neg]⟩, map_add' := λ a b, by { ext, simp only [add_subgroup.coe_add, smul_add, add_mem_class.mk_add_mk]}, map_smul' := λ a b, by { ext, simp only [neg_smul, skew_adjoint.coe_smul, add_subgroup.coe_mk, ring_hom.id_apply, self_adjoint.coe_smul, smul_neg, neg_inj], rw smul_comm, } } lemma skew_adjoint.I_smul_neg_I (a : skew_adjoint A) : I • (skew_adjoint.neg_I_smul a : A) = a := by simp only [smul_smul, skew_adjoint.neg_I_smul_apply_coe, neg_smul, smul_neg, I_mul_I, one_smul, neg_neg] /-- The real part `ℜ a` of an element `a` of a star module over `ℂ`, as a linear map. This is just `self_adjoint_part ℝ`, but we provide it as a separate definition in order to link it with lemmas concerning the `imaginary_part`, which doesn't exist in star modules over other rings. -/ noncomputable def real_part : A →ₗ[ℝ] self_adjoint A := self_adjoint_part ℝ /-- The imaginary part `ℑ a` of an element `a` of a star module over `ℂ`, as a linear map into the self adjoint elements. In a general star module, we have a decomposition into the `self_adjoint` and `skew_adjoint` parts, but in a star module over `ℂ` we have `real_part_add_I_smul_imaginary_part`, which allows us to decompose into a linear combination of `self_adjoint`s. -/ noncomputable def imaginary_part : A →ₗ[ℝ] self_adjoint A := skew_adjoint.neg_I_smul.comp (skew_adjoint_part ℝ) localized "notation `ℜ` := real_part" in complex_star_module localized "notation `ℑ` := imaginary_part" in complex_star_module @[simp] lemma real_part_apply_coe (a : A) : (ℜ a : A) = (2 : ℝ)⁻¹ • (a + star a) := by { unfold real_part, simp only [self_adjoint_part_apply_coe, inv_of_eq_inv]} @[simp] lemma imaginary_part_apply_coe (a : A) : (ℑ a : A) = -I • (2 : ℝ)⁻¹ • (a - star a) := begin unfold imaginary_part, simp only [linear_map.coe_comp, skew_adjoint.neg_I_smul_apply_coe, skew_adjoint_part_apply_coe, inv_of_eq_inv], end /-- The standard decomposition of `ℜ a + complex.I • ℑ a = a` of an element of a star module over `ℂ` into a linear combination of self adjoint elements. -/ lemma real_part_add_I_smul_imaginary_part (a : A) : (ℜ a + I • ℑ a : A) = a := by simpa only [smul_smul, real_part_apply_coe, imaginary_part_apply_coe, neg_smul, I_mul_I, one_smul, neg_sub, add_add_sub_cancel, smul_sub, smul_add, neg_sub_neg, inv_of_eq_inv] using inv_of_two_smul_add_inv_of_two_smul ℝ a @[simp] lemma real_part_I_smul (a : A) : ℜ (I • a) = - ℑ a := by { ext, simp [smul_comm I, smul_sub, sub_eq_add_neg, add_comm] } @[simp] lemma imaginary_part_I_smul (a : A) : ℑ (I • a) = ℜ a := by { ext, simp [smul_comm I, smul_smul I] } lemma real_part_smul (z : ℂ) (a : A) : ℜ (z • a) = z.re • ℜ a - z.im • ℑ a := by { nth_rewrite 0 ←re_add_im z, simp [-re_add_im, add_smul, ←smul_smul, sub_eq_add_neg] } lemma imaginary_part_smul (z : ℂ) (a : A) : ℑ (z • a) = z.re • ℑ a + z.im • ℜ a := by { nth_rewrite 0 ←re_add_im z, simp [-re_add_im, add_smul, ←smul_smul] } end real_imaginary_part
e1e30e61ac0c5b3bb9d797e62d6b471fdf0d9131
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/combinatorics/additive/e_transform.lean
cee3e26fab0fd6b388a2cdcad7efc877be3ce1bc
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,652
lean
/- 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 data.finset.pointwise /-! # e-transforms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. e-transforms are a family of transformations of pairs of finite sets that aim to reduce the size of the sumset while keeping some invariant the same. This file defines a few of them, to be used as internals of other proofs. ## Main declarations * `finset.mul_dyson_e_transform`: The Dyson e-transform. Replaces `(s, t)` by `(s ∪ e • t, t ∩ e⁻¹ • s)`. The additive version preserves `|s ∩ [1, m]| + |t ∩ [1, m - e]|`. * `finset.mul_e_transform_left`/`finset.mul_e_transform_right`: Replace `(s, t)` by `(s ∩ s • e, t ∪ e⁻¹ • t)` and `(s ∪ s • e, t ∩ e⁻¹ • t)`. Preserve (together) the sum of the cardinalities (see `finset.mul_e_transform.card`). In particular, one of the two transforms increases the sum of the cardinalities and the other one decreases it. See `le_or_lt_of_add_le_add` and around. ## TODO Prove the invariance property of the Dyson e-transform. -/ open mul_opposite open_locale pointwise variables {α : Type*} [decidable_eq α] namespace finset /-! ### Dyson e-transform -/ section comm_group variables [comm_group α] (e : α) (x : finset α × finset α) /-- The **Dyson e-transform**. Turns `(s, t)` into `(s ∪ e • t, t ∩ e⁻¹ • s)`. This reduces the product of the two sets. -/ @[to_additive "The **Dyson e-transform**. Turns `(s, t)` into `(s ∪ e +ᵥ t, t ∩ -e +ᵥ s)`. This reduces the sum of the two sets.", simps] def mul_dyson_e_transform : finset α × finset α := (x.1 ∪ e • x.2, x.2 ∩ e⁻¹ • x.1) @[to_additive] lemma mul_dyson_e_transform.subset : (mul_dyson_e_transform e x).1 * (mul_dyson_e_transform e x).2 ⊆ x.1 * x.2 := begin refine union_mul_inter_subset_union.trans (union_subset subset.rfl _), rw [mul_smul_comm, smul_mul_assoc, inv_smul_smul, mul_comm], refl, end @[to_additive] lemma mul_dyson_e_transform.card : (mul_dyson_e_transform e x).1.card + (mul_dyson_e_transform e x).2.card = x.1.card + x.2.card := begin dsimp, rw [←card_smul_finset e (_ ∩ _), smul_finset_inter, smul_inv_smul, inter_comm, card_union_add_card_inter, card_smul_finset], end @[simp, to_additive] lemma mul_dyson_e_transform_idem : mul_dyson_e_transform e (mul_dyson_e_transform e x) = mul_dyson_e_transform e x := begin ext : 1; dsimp, { rw [smul_finset_inter, smul_inv_smul, inter_comm, union_eq_left_iff_subset], exact inter_subset_union }, { rw [smul_finset_union, inv_smul_smul, union_comm, inter_eq_left_iff_subset], exact inter_subset_union } end variables {e x} @[to_additive] lemma mul_dyson_e_transform.smul_finset_snd_subset_fst : e • (mul_dyson_e_transform e x).2 ⊆ (mul_dyson_e_transform e x).1 := by { dsimp, rw [smul_finset_inter, smul_inv_smul, inter_comm], exact inter_subset_union } end comm_group /-! ### Two unnamed e-transforms The following two transforms both reduce the product/sum of the two sets. Further, one of them must decrease the sum of the size of the sets (and then the other increases it). This pair of transforms doesn't seem to be named in the literature. It is used by Sanders in his bound on Roth numbers, and by DeVos in his proof of Cauchy-Davenport. -/ section group variables [group α] (e : α) (x : finset α × finset α) /-- An **e-transform**. Turns `(s, t)` into `(s ∩ s • e, t ∪ e⁻¹ • t)`. This reduces the product of the two sets. -/ @[to_additive "An **e-transform**. Turns `(s, t)` into `(s ∩ s +ᵥ e, t ∪ -e +ᵥ t)`. This reduces the sum of the two sets.", simps] def mul_e_transform_left : finset α × finset α := (x.1 ∩ op e • x.1, x.2 ∪ e⁻¹ • x.2) /-- An **e-transform**. Turns `(s, t)` into `(s ∪ s • e, t ∩ e⁻¹ • t)`. This reduces the product of the two sets. -/ @[to_additive "An **e-transform**. Turns `(s, t)` into `(s ∪ s +ᵥ e, t ∩ -e +ᵥ t)`. This reduces the sum of the two sets.", simps] def mul_e_transform_right : finset α × finset α := (x.1 ∪ op e • x.1, x.2 ∩ e⁻¹ • x.2) @[simp, to_additive] lemma mul_e_transform_left_one : mul_e_transform_left 1 x = x := by simp [mul_e_transform_left] @[simp, to_additive] lemma mul_e_transform_right_one : mul_e_transform_right 1 x = x := by simp [mul_e_transform_right] @[to_additive] lemma mul_e_transform_left.fst_mul_snd_subset : (mul_e_transform_left e x).1 * (mul_e_transform_left e x).2 ⊆ x.1 * x.2 := begin refine inter_mul_union_subset_union.trans (union_subset subset.rfl _), rw [op_smul_finset_mul_eq_mul_smul_finset, smul_inv_smul], refl, end @[to_additive] lemma mul_e_transform_right.fst_mul_snd_subset : (mul_e_transform_right e x).1 * (mul_e_transform_right e x).2 ⊆ x.1 * x.2 := begin refine union_mul_inter_subset_union.trans (union_subset subset.rfl _), rw [op_smul_finset_mul_eq_mul_smul_finset, smul_inv_smul], refl, end @[to_additive] lemma mul_e_transform_left.card : (mul_e_transform_left e x).1.card + (mul_e_transform_right e x).1.card = 2 * x.1.card := (card_inter_add_card_union _ _).trans $ by rw [card_smul_finset, two_mul] @[to_additive] lemma mul_e_transform_right.card : (mul_e_transform_left e x).2.card + (mul_e_transform_right e x).2.card = 2 * x.2.card := (card_union_add_card_inter _ _).trans $ by rw [card_smul_finset, two_mul] /-- This statement is meant to be combined with `le_or_lt_of_add_le_add` and similar lemmas. -/ @[to_additive add_e_transform.card "This statement is meant to be combined with `le_or_lt_of_add_le_add` and similar lemmas."] protected lemma mul_e_transform.card : (mul_e_transform_left e x).1.card + (mul_e_transform_left e x).2.card + ((mul_e_transform_right e x).1.card + (mul_e_transform_right e x).2.card) = x.1.card + x.2.card + (x.1.card + x.2.card) := by rw [add_add_add_comm, mul_e_transform_left.card, mul_e_transform_right.card, ←mul_add, two_mul] end group section comm_group variables [comm_group α] (e : α) (x : finset α × finset α) @[simp, to_additive] lemma mul_e_transform_left_inv : mul_e_transform_left e⁻¹ x = (mul_e_transform_right e x.swap).swap := by simp [-op_inv, op_smul_eq_smul, mul_e_transform_left, mul_e_transform_right] @[simp, to_additive] lemma mul_e_transform_right_inv : mul_e_transform_right e⁻¹ x = (mul_e_transform_left e x.swap).swap := by simp [-op_inv, op_smul_eq_smul, mul_e_transform_left, mul_e_transform_right] end comm_group end finset
7617a3f20cc34da66b82263a440d4485fce41bd6
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/logic/embedding.lean
6ed9b3119014d32bf36e9a74185fea74e87708e6
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
9,206
lean
/- 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 data.equiv.basic import data.sigma.basic /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ end function /-- Convert an `α ≃ β` to `α ↪ β`. -/ @[simps] protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ namespace function namespace embedding @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; simpa using funext h lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[refl, simps {simp_rhs := tt}] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans, simps {simp_rhs := tt}] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : e.to_embedding.trans e.symm.to_embedding = embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : e.symm.to_embedding.trans e.to_embedding = embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ @[simps to_fun] def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ @[simps to_fun] def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ @[simps to_fun] protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ end embedding end function namespace equiv @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ @[simps to_fun] def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by { congr, injection h }⟩ end set /-- The embedding of a left cancellative semigroup into itself by left multiplication by a fixed element. -/ @[to_additive "The embedding of a left cancellative additive semigroup into itself by left translation by a fixed element."] def mul_left_embedding {G : Type u} [left_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, g * h, inj' := λ h h', (mul_right_inj g).mp, } /-- The embedding of a right cancellative semigroup into itself by right multiplication by a fixed element. -/ @[to_additive "The embedding of a right cancellative additive semigroup into itself by right translation by a fixed element."] def mul_right_embedding {G : Type u} [right_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, h * g, inj' := λ h h', (mul_left_inj g).mp, } attribute [simps] mul_left_embedding add_left_embedding mul_right_embedding add_right_embedding
5ad39b8089d0c6c0ddc70d6f868dc44bd3a726bb
2fbe653e4bc441efde5e5d250566e65538709888
/src/topology/basic.lean
a5be5445cfe712cebcb9a5e5eca3d480219e4a22
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
35,163
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Jeremy Avigad -/ import order.filter /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in `docs/theories/topology.md`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ open set filter lattice classical open_locale classical universes u v w /-- A topology on `α`. -/ structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open (-s) @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open is_open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open is_open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open is_open_interior @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩ lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) := λ _ _, closure_mono lemma closure_inter_subset_inter_closure (s t : set α) : closure (s ∩ t) ⊆ closure s ∩ closure t := (monotone_closure α).map_inf_le s t lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_is_closed is_closed_empty lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := ⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩ @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_is_closed is_closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_is_closed is_closed_closure @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) ((monotone_closure α).le_map_sup s t) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty := ⟨λ h o oo ao, classical.by_contradiction $ λ os, have s ⊆ -o, from λ x xs xo, os ⟨x, xo, xs⟩, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := (H _ h₁ nc) in hc (h₂ hs)⟩ lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty := begin split ; intro h, { rintros U U_op ⟨x, x_in⟩, exact mem_closure_iff.1 (by simp only [h]) U U_op x_in }, { apply eq_univ_of_forall, intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op ⟨_, x_in⟩ }, end lemma dense_of_subset_dense {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : closure s₁ = univ) : closure s₂ = univ := by { rw [← univ_subset_iff, ← hd], exact closure_mono h } /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure (- s) := by rw [closure_compl, frontier, diff_eq] /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s := by simp only [frontier_eq_closure_inter_closure, lattice.neg_neg, inter_comm] lemma frontier_inter_subset (s t : set α) : frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) := begin simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union], convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t), simp only [inter_distrib_left, inter_distrib_right, inter_assoc], congr' 2, apply inter_comm end lemma frontier_union_subset (s t : set α) : frontier (s ∪ t) ⊆ (frontier s ∩ closure (-t)) ∪ (closure (-s) ∩ frontier t) := by simpa only [frontier_compl, (compl_union _ _).symm] using frontier_inter_subset (-s) (-t) lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s := by rw [frontier, closure_eq_of_is_closed hs] lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s := by rw [frontier, interior_eq_of_open hs] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure /-- The frontier of a set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, from h.frontier_eq, have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end /-- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) localized "notation `𝓝` := nhds" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) := rfl lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : principal s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma nhds_sets {a : α} : (𝓝 a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} := calc (𝓝 a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : binfi_sets_eq (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = {s | ∃t⊆s, is_open t ∧ a ∈ t} : le_antisymm (supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩) (assume t ⟨i, hi₁, hi₂, hi₃⟩, mem_Union.2 ⟨i, mem_Union.2 ⟨⟨hi₃, hi₂⟩, hi₁⟩⟩) lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) := calc map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) : map_binfi_eq (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = _ : by simp only [map_principal] attribute [irreducible] nhds lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃t⊆s, is_open t ∧ a ∈ t := by simp only [nhds_sets, mem_set_of_eq, exists_prop] lemma mem_of_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := iff.intro (λ h s os xs, h s (mem_nhds_sets os xs)) (λ h t, begin change t ∈ (𝓝 x).sets → P t, rw nhds_sets, rintros ⟨s, hs, opens, xs⟩, exact hP _ _ hs (h s opens xs), end) theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a s hs, mem_pure_sets.2 $ mem_of_nhds hs lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := begin rw [tendsto, filter.map_pure], exact pure_le_nhds (f a) end @[simp] lemma nhds_ne_bot {a : α} : 𝓝 a ≠ ⊥ := ne_bot_of_le_ne_bot pure_ne_bot (pure_le_nhds a) lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ principal s} := set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by simp only [interior_eq_nhds, le_principal_iff]; refl lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ principal s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff lemma closure_eq_nhds {s : set α} : closure s = {a | 𝓝 a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ 𝓝 a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | 𝓝 a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ]) (by simp only [inf_principal, inter_compl_self, principal_empty])).symm theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty := mem_closure_iff.trans ⟨λ H t ht, nonempty.mono (inter_subset_inter_left _ interior_subset) (H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)), λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val ∧ u.val ≤ 𝓝 x := begin rw closure_eq_nhds, change 𝓝 x ⊓ principal s ≠ ⊥ ↔ _, symmetry, convert exists_ultrafilter_iff _, ext u, rw [←le_principal_iff, inf_comm, le_inf_iff] end lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, 𝓝 a ⊓ principal s ≠ ⊥ → a ∈ s := calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed] ... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, 𝓝 a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ 𝓝 a, from mem_nhds_sets h hs, have 𝓝 a ⊓ principal s = 𝓝 a, from inf_of_le_left $ by rwa le_principal_iff, have 𝓝 a ⊓ principal (s ∩ t) ≠ ⊥, from calc 𝓝 a ⊓ principal (s ∩ t) = 𝓝 a ⊓ (principal s ⊓ principal t) : by rw inf_principal ... = 𝓝 a ⊓ principal t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption, by rw [closure_eq_nhds]; assumption lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) := calc closure s \ closure t = (- closure t) ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (𝓝 a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b) : a ∈ s := have b.map f ≤ 𝓝 a ⊓ principal s, from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)), is_closed_iff_nhds.mp hs a $ ne_bot_of_le_ne_bot (map_ne_bot hb) this lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α} (hf : tendsto f x (𝓝 a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s := is_closed_iff_nhds.mp hs _ $ ne_bot_of_le_ne_bot (@map_ne_bot _ _ _ f h) $ le_inf (le_trans (map_mono $ inf_le_left) hf) $ le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f) lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (𝓝 a)) (h : f ⁻¹' s ∈ b) : a ∈ closure s := mem_of_closed_of_tendsto hb hf (is_closed_closure) $ filter.mem_sets_of_superset h (preimage_mono subset_closure) /-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter. Then `f` tends to `a` along `l` restricted to `s` if and only it tends to `a` along `l`. -/ lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β} {a : α} (h : ∀ x ∉ s, f x = a) : tendsto f (l ⊓ principal s) (𝓝 a) ↔ tendsto f l (𝓝 a) := begin rw [tendsto_iff_comap, tendsto_iff_comap], replace h : principal (-s) ≤ comap f (𝓝 a), { rintros U ⟨t, ht, htU⟩ x hx, have : f x ∈ t, from (h x hx).symm ▸ mem_of_nhds ht, exact htU this }, refine ⟨λ h', _, le_trans inf_le_left⟩, have := sup_le h' h, rw [sup_inf_right, sup_principal, union_compl_self, principal_univ, inf_top_eq, sup_le_iff] at this, exact this.1 end section lim variables [inhabited α] /-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/ noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a lemma lim_spec {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (lim f) := epsilon_spec h end lim /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, finite_subset ht₂ $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, - f i ∈ (𝓝 a).sets, by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | (f i ∩ t).nonempty })⟩ := h₁ a in calc 𝓝 a ≤ principal (t ∩ (⋂ i∈{i | (f i ∩ t).nonempty }, - f i)) : begin rw [le_principal_iff], apply @filter.inter_mem_sets _ (𝓝 a) _ _ h_sets, apply @filter.Inter_mem_sets _ (𝓝 a) _ _ _ h_fin, exact assume i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, exists_imp_distrib, ne_empty_iff_nonempty, set.nonempty], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite end topological_space section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) | s := show s ∈ 𝓝 (f x) → s ∈ map f (𝓝 x), by simp [nhds_sets]; exact assume t t_subset t_open fx_in_t, ⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩ lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩, show is_open (f ⁻¹' s), by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x := continuous_const.continuous_at lemma continuous_at_id {x : α} : continuous_at id x := continuous_id.continuous_at lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔ ∀ g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] /-- A piecewise defined function `if p then f else g` is continuous, if both `f` and `g` are continuous, and they coincide on the frontier (boundary) of the set `{a | p a}`. -/ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) /- Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', rw [ptendsto'_def], change ∀ (s : set β), s ∈ (𝓝 y).sets → pfun.preimage f s ∈ (𝓝 x).sets, rw [nhds_sets, nhds_sets], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal_sets], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_sets_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), 𝓝 a ⊓ principal s ≠ ⊥ → 𝓝 (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (𝓝 a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (𝓝 a ⊓ principal s) ≤ 𝓝 (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), ne_bot_of_le_ne_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $ (mem_image_of_mem f ha) end continuous
e9018efaae7b38937e369b008b6b7420bc5faef0
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/dynamics/ergodic/measure_preserving.lean
d6b48e285a77112003b206a363dedc44e14fd296
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,578
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.constructions.prod /-! # Measure preserving maps We say that `f : α → β` is a measure preserving map w.r.t. measures `μ : measure α` and `ν : measure β` if `f` is measurable and `map f μ = ν`. In this file we define the predicate `measure_theory.measure_preserving` and prove its basic properties. We use the term "measure preserving" because in many applications `α = β` and `μ = ν`. ## References Partially based on [this](https://www.isa-afp.org/browser_info/current/AFP/Ergodic_Theory/Measure_Preserving_Transformations.html) Isabelle formalization. ## Tags measure preserving map, measure -/ variables {α β γ δ : Type*} [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] namespace measure_theory open measure function set variables {μa : measure α} {μb : measure β} {μc : measure γ} {μd : measure δ} /-- `f` is a measure preserving map w.r.t. measures `μa` and `μb` if `f` is measurable and `map f μa = μb`. -/ @[protect_proj] structure measure_preserving (f : α → β) (μa : measure α . volume_tac) (μb : measure β . volume_tac) : Prop := (measurable : measurable f) (map_eq : map f μa = μb) namespace measure_preserving protected lemma id (μ : measure α) : measure_preserving id μ μ := ⟨measurable_id, map_id⟩ protected lemma quasi_measure_preserving {f : α → β} (hf : measure_preserving f μa μb) : quasi_measure_preserving f μa μb := ⟨hf.1, hf.2.absolutely_continuous⟩ lemma comp {g : β → γ} {f : α → β} (hg : measure_preserving g μb μc) (hf : measure_preserving f μa μb) : measure_preserving (g ∘ f) μa μc := ⟨hg.1.comp hf.1, by rw [← map_map hg.1 hf.1, hf.2, hg.2]⟩ protected lemma sigma_finite {f : α → β} (hf : measure_preserving f μa μb) [sigma_finite μb] : sigma_finite μa := sigma_finite.of_map μa hf.1 (by rwa hf.map_eq) lemma measure_preimage {f : α → β} (hf : measure_preserving f μa μb) {s : set β} (hs : measurable_set s) : μa (f ⁻¹' s) = μb s := by rw [← hf.map_eq, map_apply hf.1 hs] protected lemma iterate {f : α → α} (hf : measure_preserving f μa μa) : ∀ n, measure_preserving (f^[n]) μa μa | 0 := measure_preserving.id μa | (n + 1) := (iterate n).comp hf lemma skew_product [sigma_finite μb] [sigma_finite μd] {f : α → β} (hf : measure_preserving f μa μb) {g : α → γ → δ} (hgm : measurable (uncurry g)) (hg : ∀ᵐ x ∂μa, map (g x) μc = μd) : measure_preserving (λ p : α × γ, (f p.1, g p.1 p.2)) (μa.prod μc) (μb.prod μd) := begin classical, have : measurable (λ p : α × γ, (f p.1, g p.1 p.2)) := (hf.1.comp measurable_fst).prod_mk hgm, /- if `μa = 0`, then the lemma is trivial, otherwise we can use `hg` to deduce `sigma_finite μc`. -/ by_cases ha : μa = 0, { rw [← hf.map_eq, ha, zero_prod, (map f).map_zero, zero_prod], exact ⟨this, (map _).map_zero⟩ }, haveI : μa.ae.ne_bot := ae_ne_bot.2 ha, rcases hg.exists with ⟨x, hx⟩, haveI : sigma_finite μc := sigma_finite.of_map _ hgm.of_uncurry_left (by rwa hx), clear hx x, refine ⟨this, (prod_eq $ λ s t hs ht, _).symm⟩, rw [map_apply this (hs.prod ht)], refine (prod_apply (this $ hs.prod ht)).trans _, have : ∀ᵐ x ∂μa, μc ((λ y, (f x, g x y)) ⁻¹' s.prod t) = indicator (f ⁻¹' s) (λ y, μd t) x, { refine hg.mono (λ x hx, _), simp only [mk_preimage_prod_right_fn_eq_if, indicator_apply, mem_preimage], split_ifs, { rw [← map_apply hgm.of_uncurry_left ht, hx] }, { exact measure_empty } }, simp only [preimage_preimage], rw [lintegral_congr_ae this, lintegral_indicator _ (hf.1 hs), set_lintegral_const, hf.measure_preimage hs, mul_comm] end /-- If `f : α → β` sends the measure `μa` to `μb` and `g : γ → δ` sends the measure `μc` to `μd`, then `prod.map f g` sends `μa.prod μc` to `μb.prod μd`. -/ lemma prod [sigma_finite μb] [sigma_finite μd] {f : α → β} {g : γ → δ} (hf : measure_preserving f μa μb) (hg : measure_preserving g μc μd) : measure_preserving (prod.map f g) (μa.prod μc) (μb.prod μd) := have measurable (uncurry $ λ _ : α, g), from (hg.1.comp measurable_snd), hf.skew_product this $ filter.eventually_of_forall $ λ _, hg.map_eq variables {μ : measure α} {f : α → α} {s : set α} /-- If `μ univ < n * μ s` and `f` is a map preserving measure `μ`, then for some `x ∈ s` and `0 < m < n`, `f^[m] x ∈ s`. -/ lemma exists_mem_image_mem_of_volume_lt_mul_volume (hf : measure_preserving f μ μ) (hs : measurable_set s) {n : ℕ} (hvol : μ (univ : set α) < n * μ s) : ∃ (x ∈ s) (m ∈ Ioo 0 n), f^[m] x ∈ s := begin have A : ∀ m, measurable_set (f^[m] ⁻¹' s) := λ m, (hf.iterate m).measurable hs, have B : ∀ m, μ (f^[m] ⁻¹' s) = μ s, from λ m, (hf.iterate m).measure_preimage hs, have : μ (univ : set α) < (finset.range n).sum (λ m, μ (f^[m] ⁻¹' s)), by simpa only [B, nsmul_eq_mul, finset.sum_const, finset.card_range], rcases exists_nonempty_inter_of_measure_univ_lt_sum_measure μ (λ m hm, A m) this with ⟨i, hi, j, hj, hij, x, hxi, hxj⟩, -- without `tactic.skip` Lean closes the extra goal but it takes a long time; not sure why wlog hlt : i < j := hij.lt_or_lt using [i j, j i] tactic.skip, { simp only [set.mem_preimage, finset.mem_range] at hi hj hxi hxj, refine ⟨f^[i] x, hxi, j - i, ⟨nat.sub_pos_of_lt hlt, lt_of_le_of_lt (j.sub_le i) hj⟩, _⟩, rwa [← iterate_add_apply, nat.sub_add_cancel hlt.le] }, { exact λ hi hj hij hxi hxj, this hj hi hij.symm hxj hxi } end /-- A self-map preserving a finite measure is conservative: if `μ s ≠ 0`, then at least one point `x ∈ s` comes back to `s` under iterations of `f`. Actually, a.e. point of `s` comes back to `s` infinitely many times, see `measure_theory.measure_preserving.conservative` and theorems about `measure_theory.conservative`. -/ lemma exists_mem_image_mem [finite_measure μ] (hf : measure_preserving f μ μ) (hs : measurable_set s) (hs' : μ s ≠ 0) : ∃ (x ∈ s) (m ≠ 0), f^[m] x ∈ s := begin rcases ennreal.exists_nat_mul_gt hs' (measure_ne_top μ (univ : set α)) with ⟨N, hN⟩, rcases hf.exists_mem_image_mem_of_volume_lt_mul_volume hs hN with ⟨x, hx, m, hm, hmx⟩, exact ⟨x, hx, m, hm.1.ne', hmx⟩ end end measure_preserving end measure_theory
4ff607afbc859a8eb67df04b95ecb4d0c89a8f3f
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/W/cardinal.lean
99ccac0676287f9b92b5c4cfca6bede89e87b989
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,859
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import set_theory.cardinal_ordinal import data.W.basic /-! # Cardinality of W-types This file proves some theorems about the cardinality of W-types. The main result is `cardinal_mk_le_max_omega_of_fintype` which says that if for any `a : α`, `β a` is finite, then the cardinality of `W_type β` is at most the maximum of the cardinality of `α` and `cardinal.omega`. This can be used to prove theorems about the cardinality of algebraic constructions such as polynomials. There is a surjection from a `W_type` to `mv_polynomial` for example, and this surjection can be used to put an upper bound on the cardinality of `mv_polynomial`. ## Tags W, W type, cardinal, first order -/ universe u variables {α : Type u} {β : α → Type u} noncomputable theory namespace W_type open_locale cardinal open cardinal lemma cardinal_mk_eq_sum : #(W_type β) = sum (λ a : α, #(W_type β) ^ #(β a)) := begin simp only [cardinal.power_def, cardinal.sum_mk], exact cardinal.eq.2 ⟨equiv_sigma β⟩ end /-- `#(W_type β)` is the least cardinal `κ` such that `sum (λ a : α, κ ^ #(β a)) ≤ κ` -/ lemma cardinal_mk_le_of_le {κ : cardinal.{u}} (hκ : sum (λ a : α, κ ^ #(β a)) ≤ κ) : #(W_type β) ≤ κ := begin conv_rhs { rw ← cardinal.mk_out κ}, rw [← cardinal.mk_out κ] at hκ, simp only [cardinal.power_def, cardinal.sum_mk, cardinal.le_def] at hκ, cases hκ, exact cardinal.mk_le_of_injective (elim_injective _ hκ.1 hκ.2) end /-- If, for any `a : α`, `β a` is finite, then the cardinality of `W_type β` is at most the maximum of the cardinality of `α` and `ω` -/ lemma cardinal_mk_le_max_omega_of_fintype [Π a, fintype (β a)] : #(W_type β) ≤ max (#α) ω := (is_empty_or_nonempty α).elim (begin introI h, rw [cardinal.mk_eq_zero (W_type β)], exact zero_le _ end) $ λ hn, let m := max (#α) ω in cardinal_mk_le_of_le $ calc cardinal.sum (λ a : α, m ^ #(β a)) ≤ #α * cardinal.sup.{u u} (λ a : α, m ^ cardinal.mk (β a)) : cardinal.sum_le_sup _ ... ≤ m * cardinal.sup.{u u} (λ a : α, m ^ #(β a)) : mul_le_mul' (le_max_left _ _) (le_refl _) ... = m : mul_eq_left.{u} (le_max_right _ _) (cardinal.sup_le.2 (λ i, begin cases lt_omega.1 (lt_omega_iff_fintype.2 ⟨show fintype (β i), by apply_instance⟩) with n hn, rw [hn], exact power_nat_le (le_max_right _ _) end)) (pos_iff_ne_zero.1 (succ_le.1 begin rw [succ_zero], obtain ⟨a⟩ : nonempty α, from hn, refine le_trans _ (le_sup _ a), rw [← @power_zero m], exact power_le_power_left (pos_iff_ne_zero.1 (lt_of_lt_of_le omega_pos (le_max_right _ _))) (zero_le _) end)) end W_type
54849eb135f2d3d66968b23b936ecf42bdbaaad2
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/forInElabBug.lean
8e88cc7a889aa56985eb0d4e305fd1a595e4e50a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
576
lean
structure HeapNodeAux (α : Type u) (h : Type u) where val : α children : List h -- A `Heap` is a forest of binomial trees. inductive Heap (α : Type u) : Type u where | heap (ns : List (HeapNodeAux α (Heap α))) : Heap α deriving Inhabited open Heap partial def toArrayUnordered' (h : Heap α) : Array α := go #[] h where go (acc : Array α) : Heap α → Array α | heap ns => Id.run do let mut acc := acc for h₁ : n in ns do acc := acc.push n.val for h₂ : h in n.children do acc := go acc h return acc
690fcf10eecfa64b24189777c62b5aaf6c03c11f
46125763b4dbf50619e8846a1371029346f4c3db
/src/algebra/archimedean.lean
2c12f4a495ea7d2494677a6c1aa09155a56619c8
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
10,103
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Archimedean groups and fields. -/ import algebra.group_power algebra.field_power algebra.floor import data.rat tactic.linarith variables {α : Type*} open_locale add_monoid class archimedean (α) [ordered_comm_monoid α] : Prop := (arch : ∀ (x : α) {y}, 0 < y → ∃ n : ℕ, x ≤ n • y) theorem exists_nat_gt [linear_ordered_semiring α] [archimedean α] (x : α) : ∃ n : ℕ, x < n := let ⟨n, h⟩ := archimedean.arch x zero_lt_one in ⟨n+1, lt_of_le_of_lt (by rwa ← add_monoid.smul_one) (nat.cast_lt.2 (nat.lt_succ_self _))⟩ section linear_ordered_ring variables [linear_ordered_ring α] [archimedean α] lemma pow_unbounded_of_one_lt (x : α) {y : α} (hy1 : 1 < y) : ∃ n : ℕ, x < y ^ n := have hy0 : 0 < y - 1 := sub_pos_of_lt hy1, -- TODO `by linarith` fails to prove hy1' have hy1' : (-1:α) ≤ y, from le_trans (neg_le_self zero_le_one) (le_of_lt hy1), let ⟨n, h⟩ := archimedean.arch x hy0 in ⟨n, calc x ≤ n • (y - 1) : h ... < 1 + n • (y - 1) : lt_one_add _ ... ≤ y ^ n : one_add_sub_mul_le_pow hy1' n⟩ /-- Every x greater than 1 is between two successive natural-number powers of another y greater than one. -/ lemma exists_nat_pow_near {x : α} {y : α} (hx : 1 < x) (hy : 1 < y) : ∃ n : ℕ, y ^ n ≤ x ∧ x < y ^ (n + 1) := have h : ∃ n : ℕ, x < y ^ n, from pow_unbounded_of_one_lt _ hy, by classical; exact let n := nat.find h in have hn : x < y ^ n, from nat.find_spec h, have hnp : 0 < n, from nat.pos_iff_ne_zero.2 (λ hn0, by rw [hn0, pow_zero] at hn; exact (not_lt_of_gt hn hx)), have hnsp : nat.pred n + 1 = n, from nat.succ_pred_eq_of_pos hnp, have hltn : nat.pred n < n, from nat.pred_lt (ne_of_gt hnp), ⟨nat.pred n, le_of_not_lt (nat.find_min h hltn), by rwa hnsp⟩ theorem exists_int_gt (x : α) : ∃ n : ℤ, x < n := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa ← coe_coe⟩ theorem exists_int_lt (x : α) : ∃ n : ℤ, (n : α) < x := let ⟨n, h⟩ := exists_int_gt (-x) in ⟨-n, by rw int.cast_neg; exact neg_lt.1 h⟩ theorem exists_floor (x : α) : ∃ (fl : ℤ), ∀ (z : ℤ), z ≤ fl ↔ (z : α) ≤ x := begin haveI := classical.prop_decidable, have : ∃ (ub : ℤ), (ub:α) ≤ x ∧ ∀ (z : ℤ), (z:α) ≤ x → z ≤ ub := int.exists_greatest_of_bdd (let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h', int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩) (let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩), refine this.imp (λ fl h z, _), cases h with h₁ h₂, exact ⟨λ h, le_trans (int.cast_le.2 h) h₁, h₂ z⟩, end end linear_ordered_ring section linear_ordered_field /-- Every positive x is between two successive integer powers of another y greater than one. This is the same as `exists_int_pow_near'`, but with ≤ and < the other way around. -/ lemma exists_int_pow_near [discrete_linear_ordered_field α] [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1) := by classical; exact let ⟨N, hN⟩ := pow_unbounded_of_one_lt x⁻¹ hy in have he: ∃ m : ℤ, y ^ m ≤ x, from ⟨-N, le_of_lt (by rw [(fpow_neg y (↑N)), one_div_eq_inv]; exact (inv_lt hx (lt_trans (inv_pos hx) hN)).1 hN)⟩, let ⟨M, hM⟩ := pow_unbounded_of_one_lt x hy in have hb: ∃ b : ℤ, ∀ m, y ^ m ≤ x → m ≤ b, from ⟨M, λ m hm, le_of_not_lt (λ hlt, not_lt_of_ge (fpow_le_of_le (le_of_lt hy) (le_of_lt hlt)) (lt_of_le_of_lt hm hM))⟩, let ⟨n, hn₁, hn₂⟩ := int.exists_greatest_of_bdd hb he in ⟨n, hn₁, lt_of_not_ge (λ hge, not_le_of_gt (int.lt_succ _) (hn₂ _ hge))⟩ /-- Every positive x is between two successive integer powers of another y greater than one. This is the same as `exists_int_pow_near`, but with ≤ and < the other way around. -/ lemma exists_int_pow_near' [discrete_linear_ordered_field α] [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1) := let ⟨m, hle, hlt⟩ := exists_int_pow_near (inv_pos hx) hy in have hyp : 0 < y, from lt_trans (discrete_linear_ordered_field.zero_lt_one α) hy, ⟨-(m+1), by rwa [fpow_neg, one_div_eq_inv, inv_lt (fpow_pos_of_pos hyp _) hx], by rwa [neg_add, neg_add_cancel_right, fpow_neg, one_div_eq_inv, le_inv hx (fpow_pos_of_pos hyp _)]⟩ variables [linear_ordered_field α] [floor_ring α] lemma sub_floor_div_mul_nonneg (x : α) {y : α} (hy : 0 < y) : 0 ≤ x - ⌊x / y⌋ * y := begin conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← sub_mul, exact mul_nonneg (sub_nonneg.2 (floor_le _)) (le_of_lt hy) end lemma sub_floor_div_mul_lt (x : α) {y : α} (hy : 0 < y) : x - ⌊x / y⌋ * y < y := sub_lt_iff_lt_add.2 begin conv in y {rw ← one_mul y}, conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← add_mul, exact (mul_lt_mul_right hy).2 (by rw add_comm; exact lt_floor_add_one _), end end linear_ordered_field instance : archimedean ℕ := ⟨λ n m m0, ⟨n, by simpa only [mul_one, nat.smul_eq_mul] using nat.mul_le_mul_left n m0⟩⟩ instance : archimedean ℤ := ⟨λ n m m0, ⟨n.to_nat, le_trans (int.le_to_nat _) $ by simpa only [add_monoid.smul_eq_mul, int.nat_cast_eq_coe_nat, zero_add, mul_one] using mul_le_mul_of_nonneg_left (int.add_one_le_iff.2 m0) (int.coe_zero_le n.to_nat)⟩⟩ noncomputable def archimedean.floor_ring (α) [linear_ordered_ring α] [archimedean α] : floor_ring α := { floor := λ x, classical.some (exists_floor x), le_floor := λ z x, classical.some_spec (exists_floor x) z } section linear_ordered_field variables [linear_ordered_field α] theorem archimedean_iff_nat_lt : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x < n := ⟨@exists_nat_gt α _, λ H, ⟨λ x y y0, (H (x / y)).imp $ λ n h, le_of_lt $ by rwa [div_lt_iff y0, ← add_monoid.smul_eq_mul] at h⟩⟩ theorem archimedean_iff_nat_le : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x ≤ n := archimedean_iff_nat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (nat.cast_lt.2 (lt_add_one _))⟩⟩ theorem exists_rat_gt [archimedean α] (x : α) : ∃ q : ℚ, x < q := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa rat.cast_coe_nat⟩ theorem archimedean_iff_rat_lt : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x < q := ⟨@exists_rat_gt α _, λ H, archimedean_iff_nat_lt.2 $ λ x, let ⟨q, h⟩ := H x in ⟨nat_ceil q, lt_of_lt_of_le h $ by simpa only [rat.cast_coe_nat] using (@rat.cast_le α _ _ _).2 (le_nat_ceil _)⟩⟩ theorem archimedean_iff_rat_le : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x ≤ q := archimedean_iff_rat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (rat.cast_lt.2 (lt_add_one _))⟩⟩ variable [archimedean α] theorem exists_rat_lt (x : α) : ∃ q : ℚ, (q : α) < x := let ⟨n, h⟩ := exists_int_lt x in ⟨n, by rwa rat.cast_coe_int⟩ theorem exists_rat_btwn {x y : α} (h : x < y) : ∃ q : ℚ, x < q ∧ (q:α) < y := begin cases exists_nat_gt (y - x)⁻¹ with n nh, cases exists_floor (x * n) with z zh, refine ⟨(z + 1 : ℤ) / n, _⟩, have n0 := nat.cast_pos.1 (lt_trans (inv_pos (sub_pos.2 h)) nh), have n0' := (@nat.cast_pos α _ _).2 n0, rw [rat.cast_div_of_ne_zero, rat.cast_coe_nat, rat.cast_coe_int, div_lt_iff n0'], refine ⟨(lt_div_iff n0').2 $ (lt_iff_lt_of_le_iff_le (zh _)).1 (lt_add_one _), _⟩, rw [int.cast_add, int.cast_one], refine lt_of_le_of_lt (add_le_add_right ((zh _).1 (le_refl _)) _) _, rwa [← lt_sub_iff_add_lt', ← sub_mul, ← div_lt_iff' (sub_pos.2 h), one_div_eq_inv], { rw [rat.coe_int_denom, nat.cast_one], exact one_ne_zero }, { intro H, rw [rat.coe_nat_num, ← coe_coe, nat.cast_eq_zero] at H, subst H, cases n0 }, { rw [rat.coe_nat_denom, nat.cast_one], exact one_ne_zero } end theorem exists_nat_one_div_lt {ε : α} (hε : 0 < ε) : ∃ n : ℕ, 1 / (n + 1: α) < ε := begin cases archimedean_iff_nat_lt.1 (by apply_instance) (1/ε) with n hn, existsi n, apply div_lt_of_mul_lt_of_pos, { simp, apply add_pos_of_nonneg_of_pos, apply nat.cast_nonneg, apply zero_lt_one }, { apply (div_lt_iff' hε).1, transitivity, { exact hn }, { simp [zero_lt_one] }} end theorem exists_pos_rat_lt {x : α} (x0 : 0 < x) : ∃ q : ℚ, 0 < q ∧ (q : α) < x := by simpa only [rat.cast_pos] using exists_rat_btwn x0 include α @[simp] theorem rat.cast_floor (x : ℚ) : by haveI := archimedean.floor_ring α; exact ⌊(x:α)⌋ = ⌊x⌋ := begin haveI := archimedean.floor_ring α, apply le_antisymm, { rw [le_floor, ← @rat.cast_le α, rat.cast_coe_int], apply floor_le }, { rw [le_floor, ← rat.cast_coe_int, rat.cast_le], apply floor_le } end end linear_ordered_field section variables [discrete_linear_ordered_field α] /-- `round` rounds a number to the nearest integer. `round (1 / 2) = 1` -/ def round [floor_ring α] (x : α) : ℤ := ⌊x + 1 / 2⌋ lemma abs_sub_round [floor_ring α] (x : α) : abs (x - round x) ≤ 1 / 2 := begin rw [round, abs_sub_le_iff], have := floor_le (x + 1 / 2), have := lt_floor_add_one (x + 1 / 2), split; linarith end variable [archimedean α] theorem exists_rat_near (x : α) {ε : α} (ε0 : 0 < ε) : ∃ q : ℚ, abs (x - q) < ε := let ⟨q, h₁, h₂⟩ := exists_rat_btwn $ lt_trans ((sub_lt_self_iff x).2 ε0) ((lt_add_iff_pos_left x).2 ε0) in ⟨q, abs_sub_lt_iff.2 ⟨sub_lt.1 h₁, sub_lt_iff_lt_add.2 h₂⟩⟩ instance : archimedean ℚ := archimedean_iff_rat_le.2 $ λ q, ⟨q, by rw rat.cast_id⟩ @[simp] theorem rat.cast_round (x : ℚ) : by haveI := archimedean.floor_ring α; exact round (x:α) = round x := have ((x + (1 : ℚ) / (2 : ℚ) : ℚ) : α) = x + 1 / 2, by simp, by rw [round, round, ← this, rat.cast_floor] end
3ab1fed6579fb10e973a19a1d00b7507e4444b70
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/constantCompilerBug.lean
df2f6cb9d8bf2eb7a17b67153d399287defad9cd
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
259
lean
import Lean open Lean open Lean.Parser def regBlaParserAttribute : IO Unit := registerBuiltinDynamicParserAttribute (Name.mkSimple "blaParser") (Name.mkSimple "bla") @[inline] def parser : Parser := categoryParser (Name.mkSimple "bla") 0 #check @parser
909a1fc62cfec5ee3a4cba739ee00ef98357bc48
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/category_theory/groupoid.lean
b5dc1b32032b3fa5ff9b9509dc76c56e33ed7e9a
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
1,013
lean
/- Copyright (c) 2018 Reid Barton All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton -/ import category_theory.category import category_theory.isomorphism namespace category_theory universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation class groupoid (obj : Type u) extends category.{v} obj := (inv : Π {X Y : obj}, (X ⟶ Y) → (Y ⟶ X)) (inv_comp' : ∀ {X Y : obj} (f : X ⟶ Y), comp (inv f) f = id Y . obviously) (comp_inv' : ∀ {X Y : obj} (f : X ⟶ Y), comp f (inv f) = id X . obviously) restate_axiom groupoid.inv_comp' restate_axiom groupoid.comp_inv' attribute [simp] groupoid.inv_comp groupoid.comp_inv abbreviation large_groupoid (C : Type (u+1)) : Type (u+1) := groupoid.{u} C abbreviation small_groupoid (C : Type u) : Type (u+1) := groupoid.{u} C instance of_groupoid {C : Type u} [groupoid.{v} C] {X Y : C} (f : X ⟶ Y) : is_iso f := { inv := groupoid.inv f } end category_theory
cf89bade6b12fbf1df700ef8ee89eae06cbc6979
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/char_p/quotient.lean
12b4c9e2c689bb26456639bda8c7b6a088617d9f
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,460
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Eric Wieser -/ import algebra.char_p.basic import ring_theory.ideal.quotient /-! # Characteristic of quotients rings -/ universes u v namespace char_p theorem quotient (R : Type u) [comm_ring R] (p : ℕ) [hp1 : fact p.prime] (hp2 : ↑p ∈ nonunits R) : char_p (R ⧸ (ideal.span {p} : ideal R)) p := have hp0 : (p : R ⧸ (ideal.span {p} : ideal R)) = 0, from map_nat_cast (ideal.quotient.mk (ideal.span {p} : ideal R)) p ▸ ideal.quotient.eq_zero_iff_mem.2 (ideal.subset_span $ set.mem_singleton _), ring_char.of_eq $ or.resolve_left ((nat.dvd_prime hp1.1).1 $ ring_char.dvd hp0) $ λ h1, hp2 $ is_unit_iff_dvd_one.2 $ ideal.mem_span_singleton.1 $ ideal.quotient.eq_zero_iff_mem.1 $ @@subsingleton.elim (@@char_p.subsingleton _ $ ring_char.of_eq h1) _ _ /-- If an ideal does not contain any coercions of natural numbers other than zero, then its quotient inherits the characteristic of the underlying ring. -/ lemma quotient' {R : Type*} [comm_ring R] (p : ℕ) [char_p R p] (I : ideal R) (h : ∀ x : ℕ, (x : R) ∈ I → (x : R) = 0) : char_p (R ⧸ I) p := ⟨λ x, begin rw [←cast_eq_zero_iff R p x, ←map_nat_cast (ideal.quotient.mk I)], refine ideal.quotient.eq.trans (_ : ↑x - 0 ∈ I ↔ _), rw sub_zero, exact ⟨h x, λ h', h'.symm ▸ I.zero_mem⟩, end⟩ end char_p
f0e02b55a7297038c3f4a38e1732bef2195e491a
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/num4.lean
362b14cf8fd65aca6c74c83b4d23690abe24aa8d
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
284
lean
import data.num set_option pp.notation false set_option pp.implicit true namespace foo constant N : Type.{1} constant z : N constant o : N constant a : N notation 0 := z notation 1 := o check a = 0 end foo check (2:nat) = 1 check #foo foo.a = 1 open foo check a = 1
e777f33758c634bc8bef97cc75726297e61a9872
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/continued_fractions/convergents_equiv.lean
5ce9e51c91fbf4681122f57531f274ce994b2d57
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,979
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.continuants_recurrence import algebra.continued_fractions.terminated_stable import tactic.linarith import tactic.field_simp /-! # Equivalence of Recursive and Direct Computations of `gcf` Convergents ## Summary We show the equivalence of two computations of convergents (recurrence relation (`convergents`) vs. direct evaluation (`convergents'`)) for `gcf`s on linear ordered fields. We follow the proof from [hardy2008introduction], Chapter 10. Here's a sketch: Let `c` be a continued fraction `[h; (a₀, b₀), (a₁, b₁), (a₂, b₂),...]`, visually: a₀ h + --------------------------- a₁ b₀ + -------------------- a₂ b₁ + -------------- a₃ b₂ + -------- b₃ + ... One can compute the convergents of `c` in two ways: 1. Directly evaluating the fraction described by `c` up to a given `n` (`convergents'`) 2. Using the recurrence (`convergents`): - `A₋₁ = 1, A₀ = h, Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂`, and - `B₋₁ = 0, B₀ = 1, Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂`. To show the equivalence of the computations in the main theorem of this file `convergents_eq_convergents'`, we proceed by induction. The case `n = 0` is trivial. For `n + 1`, we first "squash" the `n + 1`th position of `c` into the `n`th position to obtain another continued fraction `c' := [h; (a₀, b₀),..., (aₙ-₁, bₙ-₁), (aₙ, bₙ + aₙ₊₁ / bₙ₊₁), (aₙ₊₁, bₙ₊₁),...]`. This squashing process is formalised in section `squash`. Note that directly evaluating `c` up to position `n + 1` is equal to evaluating `c'` up to `n`. This is shown in lemma `succ_nth_convergent'_eq_squash_gcf_nth_convergent'`. By the inductive hypothesis, the two computations for the `n`th convergent of `c` coincide. So all that is left to show is that the recurrence relation for `c` at `n + 1` and and `c'` at `n` coincide. This can be shown by another induction. The corresponding lemma in this file is `succ_nth_convergent_eq_squash_gcf_nth_convergent`. ## Main Theorems - `generalized_continued_fraction.convergents_eq_convergents'` shows the equivalence under a strict positivity restriction on the sequence. - `continued_fractions.convergents_eq_convergents'` shows the equivalence for (regular) continued fractions. ## References - https://en.wikipedia.org/wiki/Generalized_continued_fraction - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] ## Tags fractions, recurrence, equivalence -/ variables {K : Type*} {n : ℕ} namespace generalized_continued_fraction variables {g : generalized_continued_fraction K} {s : seq $ pair K} section squash /-! We will show the equivalence of the computations by induction. To make the induction work, we need to be able to *squash* the nth and (n + 1)th value of a sequence. This squashing itself and the lemmas about it are not very interesting. As a reader, you hence might want to skip this section. -/ section with_division_ring variable [division_ring K] /-- Given a sequence of gcf.pairs `s = [(a₀, bₒ), (a₁, b₁), ...]`, `squash_seq s n` combines `⟨aₙ, bₙ⟩` and `⟨aₙ₊₁, bₙ₊₁⟩` at position `n` to `⟨aₙ, bₙ + aₙ₊₁ / bₙ₊₁⟩`. For example, `squash_seq s 0 = [(a₀, bₒ + a₁ / b₁), (a₁, b₁),...]`. If `s.terminated_at (n + 1)`, then `squash_seq s n = s`. -/ def squash_seq (s : seq $ pair K) (n : ℕ) : seq (pair K) := match prod.mk (s.nth n) (s.nth (n + 1)) with | ⟨some gp_n, some gp_succ_n⟩ := seq.nats.zip_with -- return the squashed value at position `n`; otherwise, do nothing. (λ n' gp, if n' = n then ⟨gp_n.a, gp_n.b + gp_succ_n.a / gp_succ_n.b⟩ else gp) s | _ := s end /-! We now prove some simple lemmas about the squashed sequence -/ /-- If the sequence already terminated at position `n + 1`, nothing gets squashed. -/ lemma squash_seq_eq_self_of_terminated (terminated_at_succ_n : s.terminated_at (n + 1)) : squash_seq s n = s := begin change s.nth (n + 1) = none at terminated_at_succ_n, cases s_nth_eq : (s.nth n); simp only [*, squash_seq] end /-- If the sequence has not terminated before position `n + 1`, the value at `n + 1` gets squashed into position `n`. -/ lemma squash_seq_nth_of_not_terminated {gp_n gp_succ_n : pair K} (s_nth_eq : s.nth n = some gp_n) (s_succ_nth_eq : s.nth (n + 1) = some gp_succ_n) : (squash_seq s n).nth n = some ⟨gp_n.a, gp_n.b + gp_succ_n.a / gp_succ_n.b⟩ := by simp [*, squash_seq, (seq.zip_with_nth_some (seq.nats_nth n) s_nth_eq _)] /-- The values before the squashed position stay the same. -/ lemma squash_seq_nth_of_lt {m : ℕ} (m_lt_n : m < n) : (squash_seq s n).nth m = s.nth m := begin cases s_succ_nth_eq : s.nth (n + 1), case option.none { rw (squash_seq_eq_self_of_terminated s_succ_nth_eq) }, case option.some { obtain ⟨gp_n, s_nth_eq⟩ : ∃ gp_n, s.nth n = some gp_n, from s.ge_stable n.le_succ s_succ_nth_eq, obtain ⟨gp_m, s_mth_eq⟩ : ∃ gp_m, s.nth m = some gp_m, from s.ge_stable (le_of_lt m_lt_n) s_nth_eq, simp [*, squash_seq, (seq.zip_with_nth_some (seq.nats_nth m) s_mth_eq _), (ne_of_lt m_lt_n)] } end /-- Squashing at position `n + 1` and taking the tail is the same as squashing the tail of the sequence at position `n`. -/ lemma squash_seq_succ_n_tail_eq_squash_seq_tail_n : (squash_seq s (n + 1)).tail = squash_seq s.tail n := begin cases s_succ_succ_nth_eq : s.nth (n + 2) with gp_succ_succ_n, case option.none { have : squash_seq s (n + 1) = s, from squash_seq_eq_self_of_terminated s_succ_succ_nth_eq, cases s_succ_nth_eq : (s.nth (n + 1)); simp only [squash_seq, seq.nth_tail, s_succ_nth_eq, s_succ_succ_nth_eq] }, case option.some { obtain ⟨gp_succ_n, s_succ_nth_eq⟩ : ∃ gp_succ_n, s.nth (n + 1) = some gp_succ_n, from s.ge_stable (n + 1).le_succ s_succ_succ_nth_eq, -- apply extensionality with `m` and continue by cases `m = n`. ext m, cases decidable.em (m = n) with m_eq_n m_ne_n, { have : s.tail.nth n = some gp_succ_n, from (s.nth_tail n).trans s_succ_nth_eq, simp [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_some (seq.nats_nth n) this), (seq.zip_with_nth_some (seq.nats_nth (n + 1)) s_succ_nth_eq)] }, { have : s.tail.nth m = s.nth (m + 1), from s.nth_tail m, cases s_succ_mth_eq : s.nth (m + 1), all_goals { have s_tail_mth_eq, from this.trans s_succ_mth_eq }, { simp only [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_none' s_succ_mth_eq), (seq.zip_with_nth_none' s_tail_mth_eq)] }, { simp [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_some (seq.nats_nth (m + 1)) s_succ_mth_eq), (seq.zip_with_nth_some (seq.nats_nth m) s_tail_mth_eq)] } } } end /-- The auxiliary function `convergents'_aux` returns the same value for a sequence and the corresponding squashed sequence at the squashed position. -/ lemma succ_succ_nth_convergent'_aux_eq_succ_nth_convergent'_aux_squash_seq : convergents'_aux s (n + 2) = convergents'_aux (squash_seq s n) (n + 1) := begin cases s_succ_nth_eq : (s.nth $ n + 1) with gp_succ_n, case option.none { rw [(squash_seq_eq_self_of_terminated s_succ_nth_eq), (convergents'_aux_stable_step_of_terminated s_succ_nth_eq)] }, case option.some { induction n with m IH generalizing s gp_succ_n, case nat.zero { obtain ⟨gp_head, s_head_eq⟩ : ∃ gp_head, s.head = some gp_head, from s.ge_stable zero_le_one s_succ_nth_eq, have : (squash_seq s 0).head = some ⟨gp_head.a, gp_head.b + gp_succ_n.a / gp_succ_n.b⟩, from squash_seq_nth_of_not_terminated s_head_eq s_succ_nth_eq, simp [*, convergents'_aux, seq.head, seq.nth_tail] }, case nat.succ { obtain ⟨gp_head, s_head_eq⟩ : ∃ gp_head, s.head = some gp_head, from s.ge_stable (m + 2).zero_le s_succ_nth_eq, suffices : gp_head.a / (gp_head.b + convergents'_aux s.tail (m + 2)) = convergents'_aux (squash_seq s (m + 1)) (m + 2), by simpa only [convergents'_aux, s_head_eq], have : convergents'_aux s.tail (m + 2) = convergents'_aux (squash_seq s.tail m) (m + 1), by { refine (IH gp_succ_n _), simpa [seq.nth_tail] using s_succ_nth_eq }, have : (squash_seq s (m + 1)).head = some gp_head, from (squash_seq_nth_of_lt m.succ_pos).trans s_head_eq, simp only [*, convergents'_aux, squash_seq_succ_n_tail_eq_squash_seq_tail_n] } } end /-! Let us now lift the squashing operation to gcfs. -/ /-- Given a gcf `g = [h; (a₀, bₒ), (a₁, b₁), ...]`, we have - `squash_nth.gcf g 0 = [h + a₀ / b₀); (a₀, bₒ), ...]`, - `squash_nth.gcf g (n + 1) = ⟨g.h, squash_seq g.s n⟩` -/ def squash_gcf (g : generalized_continued_fraction K) : ℕ → generalized_continued_fraction K | 0 := match g.s.nth 0 with | none := g | some gp := ⟨g.h + gp.a / gp.b, g.s⟩ end | (n + 1) := ⟨g.h, squash_seq g.s n⟩ /-! Again, we derive some simple lemmas that are not really of interest. This time for the squashed gcf. -/ /-- If the gcf already terminated at position `n`, nothing gets squashed. -/ lemma squash_gcf_eq_self_of_terminated (terminated_at_n : terminated_at g n) : squash_gcf g n = g := begin cases n, case nat.zero { change g.s.nth 0 = none at terminated_at_n, simp only [convergents', squash_gcf, convergents'_aux, terminated_at_n] }, case nat.succ { cases g, simp [(squash_seq_eq_self_of_terminated terminated_at_n), squash_gcf] } end /-- The values before the squashed position stay the same. -/ lemma squash_gcf_nth_of_lt {m : ℕ} (m_lt_n : m < n) : (squash_gcf g (n + 1)).s.nth m = g.s.nth m := by simp only [squash_gcf, (squash_seq_nth_of_lt m_lt_n)] /-- `convergents'` returns the same value for a gcf and the corresponding squashed gcf at the squashed position. -/ lemma succ_nth_convergent'_eq_squash_gcf_nth_convergent' : g.convergents' (n + 1) = (squash_gcf g n).convergents' n := begin cases n, case nat.zero { cases g_s_head_eq : (g.s.nth 0); simp [g_s_head_eq, squash_gcf, convergents', convergents'_aux, seq.head] }, case nat.succ { simp only [succ_succ_nth_convergent'_aux_eq_succ_nth_convergent'_aux_squash_seq, convergents', squash_gcf] } end /-- The auxiliary continuants before the squashed position stay the same. -/ lemma continuants_aux_eq_continuants_aux_squash_gcf_of_le {m : ℕ} : m ≤ n → continuants_aux g m = (squash_gcf g n).continuants_aux m := nat.strong_induction_on m (begin clear m, assume m IH m_le_n, cases m with m', { refl }, { cases n with n', { exact (m'.not_succ_le_zero m_le_n).elim }, -- 1 ≰ 0 { cases m' with m'', { refl }, { -- get some inequalities to instantiate the IH for m'' and m'' + 1 have m'_lt_n : m'' + 1 < n' + 1 := m_le_n, have succ_m''th_conts_aux_eq := IH (m'' + 1) (lt_add_one (m'' + 1)) m'_lt_n.le, have : m'' < m'' + 2 := lt_add_of_pos_right m'' zero_lt_two, have m''th_conts_aux_eq := IH m'' this (le_trans this.le m_le_n), have : (squash_gcf g (n' + 1)).s.nth m'' = g.s.nth m'', from squash_gcf_nth_of_lt (nat.succ_lt_succ_iff.mp m'_lt_n), simp [continuants_aux, succ_m''th_conts_aux_eq, m''th_conts_aux_eq, this] } } } end) end with_division_ring /-- The convergents coincide in the expected way at the squashed position if the partial denominator at the squashed position is not zero. -/ lemma succ_nth_convergent_eq_squash_gcf_nth_convergent [field K] (nth_part_denom_ne_zero : ∀ {b : K}, g.partial_denominators.nth n = some b → b ≠ 0) : g.convergents (n + 1) = (squash_gcf g n).convergents n := begin cases decidable.em (g.terminated_at n) with terminated_at_n not_terminated_at_n, { have : squash_gcf g n = g, from squash_gcf_eq_self_of_terminated terminated_at_n, simp only [this, (convergents_stable_of_terminated n.le_succ terminated_at_n)] }, { obtain ⟨⟨a, b⟩, s_nth_eq⟩ : ∃ gp_n, g.s.nth n = some gp_n, from option.ne_none_iff_exists'.mp not_terminated_at_n, have b_ne_zero : b ≠ 0, from nth_part_denom_ne_zero (part_denom_eq_s_b s_nth_eq), cases n with n', case nat.zero { suffices : (b * g.h + a) / b = g.h + a / b, by simpa [squash_gcf, s_nth_eq, convergent_eq_conts_a_div_conts_b, (continuants_recurrence_aux s_nth_eq zeroth_continuant_aux_eq_one_zero first_continuant_aux_eq_h_one)], calc (b * g.h + a) / b = b * g.h / b + a / b : by ring -- requires `field`, not `division_ring` ... = g.h + a / b : by rw (mul_div_cancel_left _ b_ne_zero) }, case nat.succ { obtain ⟨⟨pa, pb⟩, s_n'th_eq⟩ : ∃ gp_n', g.s.nth n' = some gp_n' := g.s.ge_stable n'.le_succ s_nth_eq, -- Notations let g' := squash_gcf g (n' + 1), set pred_conts := g.continuants_aux (n' + 1) with succ_n'th_conts_aux_eq, set ppred_conts := g.continuants_aux n' with n'th_conts_aux_eq, let pA := pred_conts.a, let pB := pred_conts.b, let ppA := ppred_conts.a, let ppB := ppred_conts.b, set pred_conts' := g'.continuants_aux (n' + 1) with succ_n'th_conts_aux_eq', set ppred_conts' := g'.continuants_aux n' with n'th_conts_aux_eq', let pA' := pred_conts'.a, let pB' := pred_conts'.b, let ppA' := ppred_conts'.a, let ppB' := ppred_conts'.b, -- first compute the convergent of the squashed gcf have : g'.convergents (n' + 1) = ((pb + a / b) * pA' + pa * ppA') / ((pb + a / b) * pB' + pa * ppB'), { have : g'.s.nth n' = some ⟨pa, pb + a / b⟩ := squash_seq_nth_of_not_terminated s_n'th_eq s_nth_eq, rw [convergent_eq_conts_a_div_conts_b, continuants_recurrence_aux this n'th_conts_aux_eq'.symm succ_n'th_conts_aux_eq'.symm], }, rw this, -- then compute the convergent of the original gcf by recursively unfolding the continuants -- computation twice have : g.convergents (n' + 2) = (b * (pb * pA + pa * ppA) + a * pA) / (b * (pb * pB + pa * ppB) + a * pB), { -- use the recurrence once have : g.continuants_aux (n' + 2) = ⟨pb * pA + pa * ppA, pb * pB + pa * ppB⟩ := continuants_aux_recurrence s_n'th_eq n'th_conts_aux_eq.symm succ_n'th_conts_aux_eq.symm, -- and a second time rw [convergent_eq_conts_a_div_conts_b, continuants_recurrence_aux s_nth_eq succ_n'th_conts_aux_eq.symm this] }, rw this, suffices : ((pb + a / b) * pA + pa * ppA) / ((pb + a / b) * pB + pa * ppB) = (b * (pb * pA + pa * ppA) + a * pA) / (b * (pb * pB + pa * ppB) + a * pB), { obtain ⟨eq1, eq2, eq3, eq4⟩ : pA' = pA ∧ pB' = pB ∧ ppA' = ppA ∧ ppB' = ppB, { simp [*, (continuants_aux_eq_continuants_aux_squash_gcf_of_le $ le_refl $ n' + 1).symm, (continuants_aux_eq_continuants_aux_squash_gcf_of_le n'.le_succ).symm] }, symmetry, simpa only [eq1, eq2, eq3, eq4, mul_div_cancel _ b_ne_zero] }, field_simp, congr' 1; ring } } end end squash /-- Shows that the recurrence relation (`convergents`) and direct evaluation (`convergents'`) of the gcf coincide at position `n` if the sequence of fractions contains strictly positive values only. Requiring positivity of all values is just one possible condition to obtain this result. For example, the dual - sequences with strictly negative values only - would also work. In practice, one most commonly deals with (regular) continued fractions, which satisfy the positivity criterion required here. The analogous result for them (see `continued_fractions.convergents_eq_convergents`) hence follows directly from this theorem. -/ theorem convergents_eq_convergents' [linear_ordered_field K] (s_pos : ∀ {gp : pair K} {m : ℕ}, m < n → g.s.nth m = some gp → 0 < gp.a ∧ 0 < gp.b) : g.convergents n = g.convergents' n := begin induction n with n IH generalizing g, case nat.zero { simp }, case nat.succ { let g' := squash_gcf g n, -- first replace the rhs with the squashed computation suffices : g.convergents (n + 1) = g'.convergents' n, by rwa [succ_nth_convergent'_eq_squash_gcf_nth_convergent'], cases decidable.em (terminated_at g n) with terminated_at_n not_terminated_at_n, { have g'_eq_g : g' = g, from squash_gcf_eq_self_of_terminated terminated_at_n, rw [(convergents_stable_of_terminated n.le_succ terminated_at_n), g'_eq_g, (IH _)], assume _ _ m_lt_n s_mth_eq, exact (s_pos (nat.lt.step m_lt_n) s_mth_eq) }, { suffices : g.convergents (n + 1) = g'.convergents n, by -- invoke the IH for the squashed gcf { rwa ← IH, assume gp' m m_lt_n s_mth_eq', -- case distinction on m + 1 = n or m + 1 < n cases m_lt_n with n succ_m_lt_n, { -- the difficult case at the squashed position: we first obtain the values from -- the sequence obtain ⟨gp_succ_m, s_succ_mth_eq⟩ : ∃ gp_succ_m, g.s.nth (m + 1) = some gp_succ_m, from option.ne_none_iff_exists'.mp not_terminated_at_n, obtain ⟨gp_m, mth_s_eq⟩ : ∃ gp_m, g.s.nth m = some gp_m, from g.s.ge_stable m.le_succ s_succ_mth_eq, -- we then plug them into the recurrence suffices : 0 < gp_m.a ∧ 0 < gp_m.b + gp_succ_m.a / gp_succ_m.b, by { have ot : g'.s.nth m = some ⟨gp_m.a, gp_m.b + gp_succ_m.a / gp_succ_m.b⟩, from squash_seq_nth_of_not_terminated mth_s_eq s_succ_mth_eq, have : gp' = ⟨gp_m.a, gp_m.b + gp_succ_m.a / gp_succ_m.b⟩, by cc, rwa this }, refine ⟨(s_pos (nat.lt.step m_lt_n) mth_s_eq).left, _⟩, refine add_pos (s_pos (nat.lt.step m_lt_n) mth_s_eq).right _, have : 0 < gp_succ_m.a ∧ 0 < gp_succ_m.b := s_pos (lt_add_one $ m + 1) s_succ_mth_eq, exact (div_pos this.left this.right) }, { -- the easy case: before the squashed position, nothing changes refine s_pos (nat.lt.step $ nat.lt.step succ_m_lt_n) _, exact eq.trans (squash_gcf_nth_of_lt succ_m_lt_n).symm s_mth_eq' } }, -- now the result follows from the fact that the convergents coincide at the squashed position -- as established in `succ_nth_convergent_eq_squash_gcf_nth_convergent`. have : ∀ ⦃b⦄, g.partial_denominators.nth n = some b → b ≠ 0, by { assume b nth_part_denom_eq, obtain ⟨gp, s_nth_eq, ⟨refl⟩⟩ : ∃ gp, g.s.nth n = some gp ∧ gp.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, exact (ne_of_lt (s_pos (lt_add_one n) s_nth_eq).right).symm }, exact succ_nth_convergent_eq_squash_gcf_nth_convergent this } } end end generalized_continued_fraction open generalized_continued_fraction namespace continued_fraction /-- Shows that the recurrence relation (`convergents`) and direct evaluation (`convergents'`) of a (regular) continued fraction coincide. -/ theorem convergents_eq_convergents' [linear_ordered_field K] {c : continued_fraction K} : (↑c : generalized_continued_fraction K).convergents = (↑c : generalized_continued_fraction K).convergents' := begin ext n, apply convergents_eq_convergents', assume gp m m_lt_n s_nth_eq, exact ⟨zero_lt_one.trans_le ((c : simple_continued_fraction K).property m gp.a (part_num_eq_s_a s_nth_eq)).symm.le, c.property m gp.b $ part_denom_eq_s_b s_nth_eq⟩ end end continued_fraction
0ca11906e098ac7768d7b9fe932a8aebc8999c6d
0851884047bb567d19e188e8f1ad959c5ae9c5ce
/src/finite_dimensional_vector_spaces/basic.lean
f39feaa788e56f59eba72ab9bda5300a14bd8819
[ "Apache-2.0" ]
permissive
yz5216/xena-UROP-2018
00d3f71a831324966d40d302544ed2cbbec3fd0f
5e9da9dc64dc51897677f8b73ab9f94061a8d977
refs/heads/master
1,584,922,436,989
1,531,487,250,000
1,531,487,250,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,543
lean
/- Copyright (c) 2018 Blair Shi. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kevin Buzzard, Blair Shi This file is inspired by Johannes Hölzl's implementation of linear algebra in mathlib. The thing we improved is this file describes finite dimentional vector spaces -/ import algebra.module -- for definition of vector_space import linear_algebra.basic -- for definition of is_basis import data.list.basic universes u v class finite_dimensional_vector_space (k : Type u) (V : Type v) [field k] extends vector_space k V := (ordered_basis : list V) (is_ordered_basis : is_basis {v : V | v ∈ ordered_basis}) -- Now all we need is some theorems! variables {k : Type u} {V : Type v} variable [field k] variables [ring k] [module k V] variables (a : k) (b : V) include k definition f_dimention (k : Type u) (V : Type v) [field k] (fvs : finite_dimensional_vector_space k V) : ℕ := fvs.ordered_basis.length def f_span (l : list V) : set V := span {vc : V | vc ∈ l} def f_linear_independent (l : list V) : Prop := linear_independent {vc : V | vc ∈ l} -- helper function to check whether two basis are equal def are_basis_equal (l₀ : list V) (l₁ : list V) : Prop := ∀vc : V, vc ∈ (f_span l₀) ∧ vc ∈ (f_span l₁) def is_basis_of_vecsp (l : list V) (fvs : finite_dimensional_vector_space k V) : Prop := (are_basis_equal l fvs.ordered_basis) ∧ (f_linear_independent l) def is_in_vecsp (v : V) (fvs : finite_dimensional_vector_space k V) : Prop := v ∈ span {v₁ : V | v₁ ∈ fvs.ordered_basis} variables (v₀ v₁ v₂ : V) variables (l₀ l₁: list V) variables (h₀ h₁ res₀ res₁ : Prop) variable (fvs : finite_dimensional_vector_space k V) -- 2.4 theorem linear_dependence_th (l : list V) (h₀ : ¬(f_linear_independent l)) (h₁ : ∃vc ∈ l, (vc ≠ (0:V))) : ∃v₀, (v₀ ∈ l) ∧ (v₀ ∈ span {vr : V | vr ∈ l ∧ vr ≠ v₀}) ∧ (span {vr : V | vr ∈ l ∧ vr ≠ v₀} = f_span l) := begin apply exists.elim h₁, sorry end -- 2.5 In a finite-dimensional vector space, the length of -- every linearly independent list of vectors is less -- than or equal to the length of every spanning list of vectors. theorem len_of_lide_le_dimention (fvs : finite_dimensional_vector_space k V) (l : list V) (h₀ : f_linear_independent l) (h₁ : are_basis_equal l₀ fvs.ordered_basis) : l.length <= l₀.length := sorry -- 2.8 theorem is_f_basis (l : list V) (fvs : finite_dimensional_vector_space k V): (∀ v₀, (is_in_vecsp v₀ fvs) ∧ (v₀ ∈ f_span l)) ↔ (is_basis_of_vecsp l fvs) := sorry -- 2.10 Every spanning list in a vector space can be reduced to a basis of the vector space. theorem span_set_can_be_basis (fvs : finite_dimensional_vector_space k V) : ∀l₀, (f_span l₀ = f_span fvs.ordered_basis) → ∃l₁ ⊆ l₀, is_basis_of_vecsp l₁ fvs := sorry -- 2.12 Every linearly independent list of vectors in a finite- dimensional vector space can be extended to a basis of the vector space. theorem liide_list_can_be_basis (fvs : finite_dimensional_vector_space k V) : ∀l₀, linear_independent {vc : V | vc ∈ l₀} → ∃l₁, is_basis_of_vecsp (l₀ ++ l₁) fvs := begin sorry end -- Any two bases of a finite-dimensional vector space have the same length. theorem any_basis_have_len: ∀l₀ l₁, (is_basis_of_vecsp l₀ fvs) ∧ (is_basis_of_vecsp l₁ fvs) → (l₀.length = l₁.length) := sorry -- If V is finite dimensional, then every spanning list of vectors in V with length dimV is a basis of V. theorem span_with_dim_is_basis: ∀l₀ , (are_basis_equal l₀ fvs.ordered_basis ∧ l₀.length = f_dimention k V fvs) → is_basis_of_vecsp l₀ fvs := sorry theorem liide_list_with_dim_is_basis: ∀(l₀ : list V) , (f_linear_independent l₀ ∧ l₀.length = f_dimention k V fvs) → is_basis_of_vecsp l₀ fvs := sorry -- define subspace -- theorem: If V is finite dimensional and U is a subspace of V, then dimU ≤ dimV. -- Proposition: Suppose V is finite dimensional and U is a subspace of V. -- Then there is a subspace W of V such that V = U ⊕ W. -- Theorem: If U1 and U2 are subspaces of a finite-dimensional vector space, then -- dim(U1 +U2)=dimU1 +dimU2 −dim(U1 ∩U2). -- 2.19 Proposition: Suppose V is finite dimensional and U1 , . . . , Um are subspaces of V such that -- 2.20 V=U1+···+Um -- 2.21 dimV =dimU1 +···+dimUm. Then V = U1 ⊕ · · · ⊕ Um. -- define linear map
90d928c9bbd65557328027ab6939344e70576201
b73bd2854495d87ad5ce4f247cfcd6faa7e71c7e
/src/game/world1/level5.lean
3fbf6dda03a3b59965365494f2d0a5bc3dc5a9eb
[]
no_license
agusakov/category-theory-game
20db0b26270e0c95a3d5605498570273d72f731d
652dd7e90ae706643b2a597e2c938403653e167d
refs/heads/master
1,669,201,216,310
1,595,740,057,000
1,595,740,057,000
280,895,295
12
0
null
null
null
null
UTF-8
Lean
false
false
678
lean
import category_theory.category.default import game.world1.level3 universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted namespace category_theory variables (C : Type u) [category.{v} C] /- # Category world ## Level 5: More tactic reviews -/ /-blah blah -/ /- Lemma If $$f : X ⟶ Y$$ and $$g : X ⟶ Y$$ are morphisms such that $$f = g$$, then $$f ≫ h = g ≫ h$$. -/ lemma id_of_comp_left_id' (X : C) (f : X ⟶ X) (w : ∀ {Y : C} (g : X ⟶ Y), f ≫ g = g) : f = 𝟙 X := begin apply eq_of_comp_left_eq'', intros Z h, rw category.id_comp h, apply w, end end category_theory
4066790910c502cf54bad24f1b51ae4cb9e6645c
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/category_theory/core.lean
9e01e6d2a3ac8aee82750e25d2959a26c440a629
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,514
lean
/- Copyright (c) 2019 Scott Morrison All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.groupoid import control.equiv_functor import category_theory.types namespace category_theory universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- The core of a category C is the groupoid whose morphisms are all the isomorphisms of C. -/ @[nolint has_inhabited_instance] def core (C : Type u₁) := C variables {C : Type u₁} [category.{v₁} C] instance core_category : groupoid.{v₁} (core C) := { hom := λ X Y : C, X ≅ Y, inv := λ X Y f, iso.symm f, id := λ X, iso.refl X, comp := λ X Y Z f g, iso.trans f g } namespace core @[simp] lemma id_hom (X : core C) : iso.hom (𝟙 X) = 𝟙 X := rfl @[simp] lemma comp_hom {X Y Z : core C} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).hom = f.hom ≫ g.hom := rfl /-- The core of a category is naturally included in the category. -/ def inclusion : core C ⥤ C := { obj := id, map := λ X Y f, f.hom } variables {G : Type u₂} [groupoid.{v₂} G] /-- A functor from a groupoid to a category C factors through the core of C. -/ -- Note that this function is not functorial -- (consider the two functors from [0] to [1], and the natural transformation between them). noncomputable def functor_to_core (F : G ⥤ C) : G ⥤ core C := { obj := λ X, F.obj X, map := λ X Y f, ⟨F.map f, F.map (inv f)⟩ } /-- We can functorially associate to any functor from a groupoid to the core of a category `C`, a functor from the groupoid to `C`, simply by composing with the embedding `core C ⥤ C`. -/ def forget_functor_to_core : (G ⥤ core C) ⥤ (G ⥤ C) := (whiskering_right _ _ _).obj inclusion end core /-- `of_equiv_functor m` lifts a type-level `equiv_functor` to a categorical functor `core (Type u₁) ⥤ core (Type u₂)`. -/ def of_equiv_functor (m : Type u₁ → Type u₂) [equiv_functor m] : core (Type u₁) ⥤ core (Type u₂) := { obj := m, map := λ α β f, (equiv_functor.map_equiv m f.to_equiv).to_iso, -- These are not very pretty. map_id' := λ α, begin ext, exact (congr_fun (equiv_functor.map_refl _) x), end, map_comp' := λ α β γ f g, begin ext, simp only [equiv_functor.map_equiv_apply, equiv.to_iso_hom, function.comp_app, core.comp_hom, types_comp], erw [iso.to_equiv_comp, equiv_functor.map_trans], end, } end category_theory
313fcf87866eb4befc9f559b3c1b4051284c55e9
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/analysis/specific_limits.lean
7f8d37c64b3b18b3f2dbd7ebf05c97c837a6d47c
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
44,481
lean
/- 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 algebra.geom_sum import analysis.asymptotics.asymptotics import order.filter.archimedean import order.iterate import topology.instances.ennreal /-! # A collection of specific limit computations -/ noncomputable theory open classical set function filter finset metric asymptotics open_locale classical topological_space nat big_operators uniformity nnreal ennreal variables {α : Type*} {β : Type*} {ι : Type*} lemma tendsto_norm_at_top_at_top : tendsto (norm : ℝ → ℝ) at_top at_top := tendsto_abs_at_top_at_top lemma summable_of_absolute_convergence_real {f : ℕ → ℝ} : (∃r, tendsto (λn, (∑ i in range n, abs (f i))) at_top (𝓝 r)) → summable f | ⟨r, hr⟩ := begin refine summable_of_summable_norm ⟨r, (has_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩, exact assume i, norm_nonneg _, simpa only using hr end lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (𝓝 0) := tendsto_inv_at_top_zero.comp tendsto_coe_nat_at_top_at_top lemma tendsto_const_div_at_top_nhds_0_nat (C : ℝ) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) := by simpa only [mul_zero] using tendsto_const_nhds.mul tendsto_inverse_at_top_nhds_0_nat lemma nnreal.tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ≥0)⁻¹) at_top (𝓝 0) := by { rw ← nnreal.tendsto_coe, convert tendsto_inverse_at_top_nhds_0_nat, simp } lemma nnreal.tendsto_const_div_at_top_nhds_0_nat (C : ℝ≥0) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) := by simpa using tendsto_const_nhds.mul nnreal.tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (𝓝 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (𝓝 0), by simpa, (tendsto_add_at_top_iff_nat 1).2 (tendsto_const_div_at_top_nhds_0_nat 1) /-! ### Powers -/ lemma tendsto_add_one_pow_at_top_at_top_of_pos [linear_ordered_semiring α] [archimedean α] {r : α} (h : 0 < r) : tendsto (λ n:ℕ, (r + 1)^n) at_top at_top := tendsto_at_top_at_top_of_monotone' (λ n m, pow_le_pow (le_add_of_nonneg_left (le_of_lt h))) $ not_bdd_above_iff.2 $ λ x, set.exists_range_iff.2 $ add_one_pow_unbounded_of_pos _ h lemma tendsto_pow_at_top_at_top_of_one_lt [linear_ordered_ring α] [archimedean α] {r : α} (h : 1 < r) : tendsto (λn:ℕ, r ^ n) at_top at_top := sub_add_cancel r 1 ▸ tendsto_add_one_pow_at_top_at_top_of_pos (sub_pos.2 h) lemma nat.tendsto_pow_at_top_at_top_of_one_lt {m : ℕ} (h : 1 < m) : tendsto (λn:ℕ, m ^ n) at_top at_top := nat.sub_add_cancel (le_of_lt h) ▸ tendsto_add_one_pow_at_top_at_top_of_pos (nat.sub_pos_of_lt h) lemma tendsto_norm_zero' {𝕜 : Type*} [normed_group 𝕜] : tendsto (norm : 𝕜 → ℝ) (𝓝[{0}ᶜ] 0) (𝓝[set.Ioi 0] 0) := tendsto_norm_zero.inf $ tendsto_principal_principal.2 $ λ x hx, norm_pos_iff.2 hx namespace normed_field lemma tendsto_norm_inverse_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] : tendsto (λ x:𝕜, ∥x⁻¹∥) (𝓝[{0}ᶜ] 0) at_top := (tendsto_inv_zero_at_top.comp tendsto_norm_zero').congr $ λ x, (normed_field.norm_inv x).symm lemma tendsto_norm_fpow_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] {m : ℤ} (hm : m < 0) : tendsto (λ x : 𝕜, ∥x ^ m∥) (𝓝[{0}ᶜ] 0) at_top := begin rcases neg_surjective m with ⟨m, rfl⟩, rw neg_lt_zero at hm, lift m to ℕ using hm.le, rw int.coe_nat_pos at hm, simp only [normed_field.norm_pow, fpow_neg, gpow_coe_nat, ← inv_pow'], exact (tendsto_pow_at_top hm).comp normed_field.tendsto_norm_inverse_nhds_within_0_at_top end @[simp] lemma continuous_at_fpow {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {m : ℤ} {x : 𝕜} : continuous_at (λ x, x ^ m) x ↔ x ≠ 0 ∨ 0 ≤ m := begin refine ⟨_, continuous_at_fpow _ _⟩, contrapose!, rintro ⟨rfl, hm⟩ hc, exact not_tendsto_at_top_of_tendsto_nhds (hc.tendsto.mono_left nhds_within_le_nhds).norm (tendsto_norm_fpow_nhds_within_0_at_top hm) end @[simp] lemma continuous_at_inv {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {x : 𝕜} : continuous_at has_inv.inv x ↔ x ≠ 0 := by simpa [(@zero_lt_one ℤ _ _).not_le] using @continuous_at_fpow _ _ (-1) x end normed_field lemma tendsto_pow_at_top_nhds_0_of_lt_1 {𝕜 : Type*} [linear_ordered_field 𝕜] [archimedean 𝕜] [topological_space 𝕜] [order_topology 𝕜] {r : 𝕜} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝 0) := h₁.eq_or_lt.elim (assume : 0 = r, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, ← this, tendsto_const_nhds]) (assume : 0 < r, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (𝓝 0), from tendsto_inv_at_top_zero.comp (tendsto_pow_at_top_at_top_of_one_lt $ one_lt_inv this h₂), this.congr (λ n, by simp)) lemma tendsto_pow_at_top_nhds_within_0_of_lt_1 {𝕜 : Type*} [linear_ordered_field 𝕜] [archimedean 𝕜] [topological_space 𝕜] [order_topology 𝕜] {r : 𝕜} (h₁ : 0 < r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝[Ioi 0] 0) := tendsto_inf.2 ⟨tendsto_pow_at_top_nhds_0_of_lt_1 h₁.le h₂, tendsto_principal.2 $ eventually_of_forall $ λ n, pow_pos h₁ _⟩ lemma is_o_pow_pow_of_lt_left {r₁ r₂ : ℝ} (h₁ : 0 ≤ r₁) (h₂ : r₁ < r₂) : is_o (λ n : ℕ, r₁ ^ n) (λ n, r₂ ^ n) at_top := have H : 0 < r₂ := h₁.trans_lt h₂, is_o_of_tendsto (λ n hn, false.elim $ H.ne' $ pow_eq_zero hn) $ (tendsto_pow_at_top_nhds_0_of_lt_1 (div_nonneg h₁ (h₁.trans h₂.le)) ((div_lt_one H).2 h₂)).congr (λ n, div_pow _ _ _) lemma is_O_pow_pow_of_le_left {r₁ r₂ : ℝ} (h₁ : 0 ≤ r₁) (h₂ : r₁ ≤ r₂) : is_O (λ n : ℕ, r₁ ^ n) (λ n, r₂ ^ n) at_top := h₂.eq_or_lt.elim (λ h, h ▸ is_O_refl _ _) (λ h, (is_o_pow_pow_of_lt_left h₁ h).is_O) lemma is_o_pow_pow_of_abs_lt_left {r₁ r₂ : ℝ} (h : abs r₁ < abs r₂) : is_o (λ n : ℕ, r₁ ^ n) (λ n, r₂ ^ n) at_top := begin refine (is_o.of_norm_left _).of_norm_right, exact (is_o_pow_pow_of_lt_left (abs_nonneg r₁) h).congr (pow_abs r₁) (pow_abs r₂) end /-- Various statements equivalent to the fact that `f n` grows exponentially slower than `R ^ n`. * 0: $f n = o(a ^ n)$ for some $-R < a < R$; * 1: $f n = o(a ^ n)$ for some $0 < a < R$; * 2: $f n = O(a ^ n)$ for some $-R < a < R$; * 3: $f n = O(a ^ n)$ for some $0 < a < R$; * 4: there exist `a < R` and `C` such that one of `C` and `R` is positive and $|f n| ≤ Ca^n$ for all `n`; * 5: there exists `0 < a < R` and a positive `C` such that $|f n| ≤ Ca^n$ for all `n`; * 6: there exists `a < R` such that $|f n| ≤ a ^ n$ for sufficiently large `n`; * 7: there exists `0 < a < R` such that $|f n| ≤ a ^ n$ for sufficiently large `n`. NB: For backwards compatibility, if you add more items to the list, please append them at the end of the list. -/ lemma tfae_exists_lt_is_o_pow (f : ℕ → ℝ) (R : ℝ) : tfae [∃ a ∈ Ioo (-R) R, is_o f (pow a) at_top, ∃ a ∈ Ioo 0 R, is_o f (pow a) at_top, ∃ a ∈ Ioo (-R) R, is_O f (pow a) at_top, ∃ a ∈ Ioo 0 R, is_O f (pow a) at_top, ∃ (a < R) C (h₀ : 0 < C ∨ 0 < R), ∀ n, abs (f n) ≤ C * a ^ n, ∃ (a ∈ Ioo 0 R) (C > 0), ∀ n, abs (f n) ≤ C * a ^ n, ∃ a < R, ∀ᶠ n in at_top, abs (f n) ≤ a ^ n, ∃ a ∈ Ioo 0 R, ∀ᶠ n in at_top, abs (f n) ≤ a ^ n] := begin have A : Ico 0 R ⊆ Ioo (-R) R, from λ x hx, ⟨(neg_lt_zero.2 (hx.1.trans_lt hx.2)).trans_le hx.1, hx.2⟩, have B : Ioo 0 R ⊆ Ioo (-R) R := subset.trans Ioo_subset_Ico_self A, -- First we prove that 1-4 are equivalent using 2 → 3 → 4, 1 → 3, and 2 → 1 tfae_have : 1 → 3, from λ ⟨a, ha, H⟩, ⟨a, ha, H.is_O⟩, tfae_have : 2 → 1, from λ ⟨a, ha, H⟩, ⟨a, B ha, H⟩, tfae_have : 3 → 2, { rintro ⟨a, ha, H⟩, rcases exists_between (abs_lt.2 ha) with ⟨b, hab, hbR⟩, exact ⟨b, ⟨(abs_nonneg a).trans_lt hab, hbR⟩, H.trans_is_o (is_o_pow_pow_of_abs_lt_left (hab.trans_le (le_abs_self b)))⟩ }, tfae_have : 2 → 4, from λ ⟨a, ha, H⟩, ⟨a, ha, H.is_O⟩, tfae_have : 4 → 3, from λ ⟨a, ha, H⟩, ⟨a, B ha, H⟩, -- Add 5 and 6 using 4 → 6 → 5 → 3 tfae_have : 4 → 6, { rintro ⟨a, ha, H⟩, rcases bound_of_is_O_nat_at_top H with ⟨C, hC₀, hC⟩, refine ⟨a, ha, C, hC₀, λ n, _⟩, simpa only [real.norm_eq_abs, abs_pow, abs_of_nonneg ha.1.le] using hC (pow_ne_zero n ha.1.ne') }, tfae_have : 6 → 5, from λ ⟨a, ha, C, H₀, H⟩, ⟨a, ha.2, C, or.inl H₀, H⟩, tfae_have : 5 → 3, { rintro ⟨a, ha, C, h₀, H⟩, rcases sign_cases_of_C_mul_pow_nonneg (λ n, (abs_nonneg _).trans (H n)) with rfl | ⟨hC₀, ha₀⟩, { obtain rfl : f = 0, by { ext n, simpa using H n }, simp only [lt_irrefl, false_or] at h₀, exact ⟨0, ⟨neg_lt_zero.2 h₀, h₀⟩, is_O_zero _ _⟩ }, exact ⟨a, A ⟨ha₀, ha⟩, is_O_of_le' _ (λ n, (H n).trans $ mul_le_mul_of_nonneg_left (le_abs_self _) hC₀.le)⟩ }, -- Add 7 and 8 using 2 → 8 → 7 → 3 tfae_have : 2 → 8, { rintro ⟨a, ha, H⟩, refine ⟨a, ha, (H.def zero_lt_one).mono (λ n hn, _)⟩, rwa [real.norm_eq_abs, real.norm_eq_abs, one_mul, abs_pow, abs_of_pos ha.1] at hn }, tfae_have : 8 → 7, from λ ⟨a, ha, H⟩, ⟨a, ha.2, H⟩, tfae_have : 7 → 3, { rintro ⟨a, ha, H⟩, have : 0 ≤ a, from nonneg_of_eventually_pow_nonneg (H.mono $ λ n, (abs_nonneg _).trans), refine ⟨a, A ⟨this, ha⟩, is_O.of_bound 1 _⟩, simpa only [real.norm_eq_abs, one_mul, abs_pow, abs_of_nonneg this] }, tfae_finish end lemma uniformity_basis_dist_pow_of_lt_1 {α : Type*} [pseudo_metric_space α] {r : ℝ} (h₀ : 0 < r) (h₁ : r < 1) : (𝓤 α).has_basis (λ k : ℕ, true) (λ k, {p : α × α | dist p.1 p.2 < r ^ k}) := metric.mk_uniformity_basis (λ i _, pow_pos h₀ _) $ λ ε ε0, (exists_pow_lt_of_lt_one ε0 h₁).imp $ λ k hk, ⟨trivial, hk.le⟩ lemma geom_lt {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) {n : ℕ} (hn : 0 < n) (h : ∀ k < n, c * u k < u (k + 1)) : c ^ n * u 0 < u n := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_le_of_lt hn _ _ h, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma geom_le {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) (n : ℕ) (h : ∀ k < n, c * u k ≤ u (k + 1)) : c ^ n * u 0 ≤ u n := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ _ h; simp [pow_succ, mul_assoc, le_refl] lemma lt_geom {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) {n : ℕ} (hn : 0 < n) (h : ∀ k < n, u (k + 1) < c * u k) : u n < c ^ n * u 0 := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_lt_of_le hn _ h _, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma le_geom {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) (n : ℕ) (h : ∀ k < n, u (k + 1) ≤ c * u k) : u n ≤ (c ^ n) * u 0 := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ h _; simp [pow_succ, mul_assoc, le_refl] /-- For any natural `k` and a real `r > 1` we have `n ^ k = o(r ^ n)` as `n → ∞`. -/ lemma is_o_pow_const_const_pow_of_one_lt {R : Type*} [normed_ring R] (k : ℕ) {r : ℝ} (hr : 1 < r) : is_o (λ n, n ^ k : ℕ → R) (λ n, r ^ n) at_top := begin have : tendsto (λ x : ℝ, x ^ k) (𝓝[Ioi 1] 1) (𝓝 1), from ((continuous_id.pow k).tendsto' (1 : ℝ) 1 (one_pow _)).mono_left inf_le_left, obtain ⟨r' : ℝ, hr' : r' ^ k < r, h1 : 1 < r'⟩ := ((this.eventually (gt_mem_nhds hr)).and self_mem_nhds_within).exists, have h0 : 0 ≤ r' := zero_le_one.trans h1.le, suffices : is_O _ (λ n : ℕ, (r' ^ k) ^ n) at_top, from this.trans_is_o (is_o_pow_pow_of_lt_left (pow_nonneg h0 _) hr'), conv in ((r' ^ _) ^ _) { rw [← pow_mul, mul_comm, pow_mul] }, suffices : ∀ n : ℕ, ∥(n : R)∥ ≤ (r' - 1)⁻¹ * ∥(1 : R)∥ * ∥r' ^ n∥, from (is_O_of_le' _ this).pow _, intro n, rw mul_right_comm, refine n.norm_cast_le.trans (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), simpa [div_eq_inv_mul, real.norm_eq_abs, abs_of_nonneg h0] using n.cast_le_pow_div_sub h1 end /-- For a real `r > 1` we have `n = o(r ^ n)` as `n → ∞`. -/ lemma is_o_coe_const_pow_of_one_lt {R : Type*} [normed_ring R] {r : ℝ} (hr : 1 < r) : is_o (coe : ℕ → R) (λ n, r ^ n) at_top := by simpa only [pow_one] using is_o_pow_const_const_pow_of_one_lt 1 hr /-- If `∥r₁∥ < r₂`, then for any naturak `k` we have `n ^ k r₁ ^ n = o (r₂ ^ n)` as `n → ∞`. -/ lemma is_o_pow_const_mul_const_pow_const_pow_of_norm_lt {R : Type*} [normed_ring R] (k : ℕ) {r₁ : R} {r₂ : ℝ} (h : ∥r₁∥ < r₂) : is_o (λ n, n ^ k * r₁ ^ n : ℕ → R) (λ n, r₂ ^ n) at_top := begin by_cases h0 : r₁ = 0, { refine (is_o_zero _ _).congr' (mem_at_top_sets.2 $ ⟨1, λ n hn, _⟩) eventually_eq.rfl, simp [zero_pow (zero_lt_one.trans_le hn), h0] }, rw [← ne.def, ← norm_pos_iff] at h0, have A : is_o (λ n, n ^ k : ℕ → R) (λ n, (r₂ / ∥r₁∥) ^ n) at_top, from is_o_pow_const_const_pow_of_one_lt k ((one_lt_div h0).2 h), suffices : is_O (λ n, r₁ ^ n) (λ n, ∥r₁∥ ^ n) at_top, by simpa [div_mul_cancel _ (pow_pos h0 _).ne'] using A.mul_is_O this, exact is_O.of_bound 1 (by simpa using eventually_norm_pow_le r₁) end lemma tendsto_pow_const_div_const_pow_of_one_lt (k : ℕ) {r : ℝ} (hr : 1 < r) : tendsto (λ n, n ^ k / r ^ n : ℕ → ℝ) at_top (𝓝 0) := (is_o_pow_const_const_pow_of_one_lt k hr).tendsto_0 /-- If `|r| < 1`, then `n ^ k r ^ n` tends to zero for any natural `k`. -/ lemma tendsto_pow_const_mul_const_pow_of_abs_lt_one (k : ℕ) {r : ℝ} (hr : abs r < 1) : tendsto (λ n, n ^ k * r ^ n : ℕ → ℝ) at_top (𝓝 0) := begin by_cases h0 : r = 0, { exact tendsto_const_nhds.congr' (mem_at_top_sets.2 ⟨1, λ n hn, by simp [zero_lt_one.trans_le hn, h0]⟩) }, have hr' : 1 < (abs r)⁻¹, from one_lt_inv (abs_pos.2 h0) hr, rw tendsto_zero_iff_norm_tendsto_zero, simpa [div_eq_mul_inv] using tendsto_pow_const_div_const_pow_of_one_lt k hr' end /-- If a sequence `v` of real numbers satisfies `k * v n ≤ v (n+1)` with `1 < k`, then it goes to +∞. -/ lemma tendsto_at_top_of_geom_le {v : ℕ → ℝ} {c : ℝ} (h₀ : 0 < v 0) (hc : 1 < c) (hu : ∀ n, c * v n ≤ v (n + 1)) : tendsto v at_top at_top := tendsto_at_top_mono (λ n, geom_le (zero_le_one.trans hc.le) n (λ k hk, hu k)) $ (tendsto_pow_at_top_at_top_of_one_lt hc).at_top_mul_const h₀ lemma nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ≥0} (hr : r < 1) : tendsto (λ n:ℕ, r^n) at_top (𝓝 0) := nnreal.tendsto_coe.1 $ by simp only [nnreal.coe_pow, nnreal.coe_zero, tendsto_pow_at_top_nhds_0_of_lt_1 r.coe_nonneg hr] lemma ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ≥0∞} (hr : r < 1) : tendsto (λ n:ℕ, r^n) at_top (𝓝 0) := begin rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, rw [← ennreal.coe_zero], norm_cast at *, apply nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 hr end /-- In a normed ring, the powers of an element x with `∥x∥ < 1` tend to zero. -/ lemma tendsto_pow_at_top_nhds_0_of_norm_lt_1 {R : Type*} [normed_ring R] {x : R} (h : ∥x∥ < 1) : tendsto (λ (n : ℕ), x ^ n) at_top (𝓝 0) := begin apply squeeze_zero_norm' (eventually_norm_pow_le x), exact tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg _) h, end lemma tendsto_pow_at_top_nhds_0_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝 0) := tendsto_pow_at_top_nhds_0_of_norm_lt_1 h /-! ### Geometric series-/ section geometric lemma has_sum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ := have r ≠ 1, from ne_of_lt h₂, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (𝓝 ((0 - 1) * (r - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂).sub tendsto_const_nhds).mul tendsto_const_nhds, have (λ n, (∑ i in range n, r ^ i)) = (λ n, geom_sum r n) := rfl, (has_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $ by simp [neg_inv, geom_sum_eq, div_eq_mul_inv, *] at * lemma summable_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, has_sum_geometric_of_lt_1 h₁ h₂⟩ lemma tsum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := (has_sum_geometric_of_lt_1 h₁ h₂).tsum_eq lemma has_sum_geometric_two : has_sum (λn:ℕ, ((1:ℝ)/2) ^ n) 2 := by convert has_sum_geometric_of_lt_1 _ _; norm_num lemma summable_geometric_two : summable (λn:ℕ, ((1:ℝ)/2) ^ n) := ⟨_, has_sum_geometric_two⟩ lemma tsum_geometric_two : ∑'n:ℕ, ((1:ℝ)/2) ^ n = 2 := has_sum_geometric_two.tsum_eq lemma sum_geometric_two_le (n : ℕ) : ∑ (i : ℕ) in range n, (1 / (2 : ℝ)) ^ i ≤ 2 := begin have : ∀ i, 0 ≤ (1 / (2 : ℝ)) ^ i, { intro i, apply pow_nonneg, norm_num }, convert sum_le_tsum (range n) (λ i _, this i) summable_geometric_two, exact tsum_geometric_two.symm end lemma has_sum_geometric_two' (a : ℝ) : has_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert has_sum.mul_left (a / 2) (has_sum_geometric_of_lt_1 (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, refl, }, { norm_num } end lemma summable_geometric_two' (a : ℝ) : summable (λ n:ℕ, (a / 2) / 2 ^ n) := ⟨a, has_sum_geometric_two' a⟩ lemma tsum_geometric_two' (a : ℝ) : ∑' n:ℕ, (a / 2) / 2^n = a := (has_sum_geometric_two' a).tsum_eq /-- **Sum of a Geometric Series** -/ lemma nnreal.has_sum_geometric {r : ℝ≥0} (hr : r < 1) : has_sum (λ n : ℕ, r ^ n) (1 - r)⁻¹ := begin apply nnreal.has_sum_coe.1, push_cast, rw [nnreal.coe_sub (le_of_lt hr)], exact has_sum_geometric_of_lt_1 r.coe_nonneg hr end lemma nnreal.summable_geometric {r : ℝ≥0} (hr : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, nnreal.has_sum_geometric hr⟩ lemma tsum_geometric_nnreal {r : ℝ≥0} (hr : r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := (nnreal.has_sum_geometric hr).tsum_eq /-- The series `pow r` converges to `(1-r)⁻¹`. For `r < 1` the RHS is a finite number, and for `1 ≤ r` the RHS equals `∞`. -/ @[simp] lemma ennreal.tsum_geometric (r : ℝ≥0∞) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := begin cases lt_or_le r 1 with hr hr, { rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, norm_cast at *, convert ennreal.tsum_coe_eq (nnreal.has_sum_geometric hr), rw [ennreal.coe_inv $ ne_of_gt $ nnreal.sub_pos.2 hr] }, { rw [ennreal.sub_eq_zero_of_le hr, ennreal.inv_zero, ennreal.tsum_eq_supr_nat, supr_eq_top], refine λ a ha, (ennreal.exists_nat_gt (lt_top_iff_ne_top.1 ha)).imp (λ n hn, lt_of_lt_of_le hn _), have : ∀ k:ℕ, 1 ≤ r^k, by simpa using canonically_ordered_comm_semiring.pow_le_pow_of_le_left hr, calc (n:ℝ≥0∞) = (∑ i in range n, 1) : by rw [sum_const, nsmul_one, card_range] ... ≤ ∑ i in range n, r ^ i : sum_le_sum (λ k _, this k) } end variables {K : Type*} [normed_field K] {ξ : K} lemma has_sum_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : has_sum (λn:ℕ, ξ ^ n) (1 - ξ)⁻¹ := begin have xi_ne_one : ξ ≠ 1, by { contrapose! h, simp [h] }, have A : tendsto (λn, (ξ ^ n - 1) * (ξ - 1)⁻¹) at_top (𝓝 ((0 - 1) * (ξ - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_norm_lt_1 h).sub tendsto_const_nhds).mul tendsto_const_nhds, have B : (λ n, (∑ i in range n, ξ ^ i)) = (λ n, geom_sum ξ n) := rfl, rw [has_sum_iff_tendsto_nat_of_summable_norm, B], { simpa [geom_sum_eq, xi_ne_one, neg_inv, div_eq_mul_inv] using A }, { simp [normed_field.norm_pow, summable_geometric_of_lt_1 (norm_nonneg _) h] } end lemma summable_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : summable (λn:ℕ, ξ ^ n) := ⟨_, has_sum_geometric_of_norm_lt_1 h⟩ lemma tsum_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : ∑'n:ℕ, ξ ^ n = (1 - ξ)⁻¹ := (has_sum_geometric_of_norm_lt_1 h).tsum_eq lemma has_sum_geometric_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ := has_sum_geometric_of_norm_lt_1 h lemma summable_geometric_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : summable (λn:ℕ, r ^ n) := summable_geometric_of_norm_lt_1 h lemma tsum_geometric_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := tsum_geometric_of_norm_lt_1 h /-- A geometric series in a normed field is summable iff the norm of the common ratio is less than one. -/ @[simp] lemma summable_geometric_iff_norm_lt_1 : summable (λ n : ℕ, ξ ^ n) ↔ ∥ξ∥ < 1 := begin refine ⟨λ h, _, summable_geometric_of_norm_lt_1⟩, obtain ⟨k : ℕ, hk : dist (ξ ^ k) 0 < 1⟩ := (h.tendsto_cofinite_zero.eventually (ball_mem_nhds _ zero_lt_one)).exists, simp only [normed_field.norm_pow, dist_zero_right] at hk, rw [← one_pow k] at hk, exact lt_of_pow_lt_pow _ zero_le_one hk end end geometric section mul_geometric lemma summable_norm_pow_mul_geometric_of_norm_lt_1 {R : Type*} [normed_ring R] (k : ℕ) {r : R} (hr : ∥r∥ < 1) : summable (λ n : ℕ, ∥(n ^ k * r ^ n : R)∥) := begin rcases exists_between hr with ⟨r', hrr', h⟩, exact summable_of_is_O_nat (summable_geometric_of_lt_1 ((norm_nonneg _).trans hrr'.le) h) (is_o_pow_const_mul_const_pow_const_pow_of_norm_lt _ hrr').is_O.norm_left end lemma summable_pow_mul_geometric_of_norm_lt_1 {R : Type*} [normed_ring R] [complete_space R] (k : ℕ) {r : R} (hr : ∥r∥ < 1) : summable (λ n, n ^ k * r ^ n : ℕ → R) := summable_of_summable_norm $ summable_norm_pow_mul_geometric_of_norm_lt_1 _ hr /-- If `∥r∥ < 1`, then `∑' n : ℕ, n * r ^ n = r / (1 - r) ^ 2`, `has_sum` version. -/ lemma has_sum_coe_mul_geometric_of_norm_lt_1 {𝕜 : Type*} [normed_field 𝕜] [complete_space 𝕜] {r : 𝕜} (hr : ∥r∥ < 1) : has_sum (λ n, n * r ^ n : ℕ → 𝕜) (r / (1 - r) ^ 2) := begin have A : summable (λ n, n * r ^ n : ℕ → 𝕜), by simpa using summable_pow_mul_geometric_of_norm_lt_1 1 hr, have B : has_sum (pow r : ℕ → 𝕜) (1 - r)⁻¹, from has_sum_geometric_of_norm_lt_1 hr, refine A.has_sum_iff.2 _, have hr' : r ≠ 1, by { rintro rfl, simpa [lt_irrefl] using hr }, set s : 𝕜 := ∑' n : ℕ, n * r ^ n, calc s = (1 - r) * s / (1 - r) : (mul_div_cancel_left _ (sub_ne_zero.2 hr'.symm)).symm ... = (s - r * s) / (1 - r) : by rw [sub_mul, one_mul] ... = ((0 : ℕ) * r ^ 0 + (∑' n : ℕ, (n + 1) * r ^ (n + 1)) - r * s) / (1 - r) : by { congr, exact tsum_eq_zero_add A } ... = (r * (∑' n : ℕ, (n + 1) * r ^ n) - r * s) / (1 - r) : by simp [pow_succ, mul_left_comm _ r, tsum_mul_left] ... = r / (1 - r) ^ 2 : by simp [add_mul, tsum_add A B.summable, mul_add, B.tsum_eq, ← div_eq_mul_inv, sq, div_div_eq_div_mul] end /-- If `∥r∥ < 1`, then `∑' n : ℕ, n * r ^ n = r / (1 - r) ^ 2`. -/ lemma tsum_coe_mul_geometric_of_norm_lt_1 {𝕜 : Type*} [normed_field 𝕜] [complete_space 𝕜] {r : 𝕜} (hr : ∥r∥ < 1) : (∑' n : ℕ, n * r ^ n : 𝕜) = (r / (1 - r) ^ 2) := (has_sum_coe_mul_geometric_of_norm_lt_1 hr).tsum_eq end mul_geometric /-! ### Sequences with geometrically decaying distance in metric spaces In this paragraph, we discuss sequences in metric spaces or emetric spaces for which the distance between two consecutive terms decays geometrically. We show that such sequences are Cauchy sequences, and bound their distances to the limit. We also discuss series with geometrically decaying terms. -/ section edist_le_geometric variables [pseudo_emetric_space α] (r C : ℝ≥0∞) (hr : r < 1) (hC : C ≠ ⊤) {f : ℕ → α} (hu : ∀n, edist (f n) (f (n+1)) ≤ C * r^n) include hr hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, `C ≠ ∞`, `r < 1`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric : cauchy_seq f := begin refine cauchy_seq_of_edist_le_of_tsum_ne_top _ hu _, rw [ennreal.tsum_mul_left, ennreal.tsum_geometric], refine ennreal.mul_ne_top hC (ennreal.inv_ne_top.2 _), exact ne_of_gt (ennreal.zero_lt_sub_iff_lt.2 hr) end omit hr hC /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : edist (f n) a ≤ (C * r^n) / (1 - r) := begin convert edist_le_tsum_of_edist_le_of_tendsto _ hu ha _, simp only [pow_add, ennreal.tsum_mul_left, ennreal.tsum_geometric, div_eq_mul_inv, mul_assoc] end /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≤ C / (1 - r) := by simpa only [pow_zero, mul_one] using edist_le_of_edist_le_geometric_of_tendsto r C hu ha 0 end edist_le_geometric section edist_le_geometric_two variables [pseudo_emetric_space α] (C : ℝ≥0∞) (hC : C ≠ ⊤) {f : ℕ → α} (hu : ∀n, edist (f n) (f (n+1)) ≤ C / 2^n) {a : α} (ha : tendsto f at_top (𝓝 a)) include hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric_two : cauchy_seq f := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at hu, refine cauchy_seq_of_edist_le_geometric 2⁻¹ C _ hC hu, simp [ennreal.one_lt_two] end omit hC include ha /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f n` to the limit of `f` is bounded above by `2 * C * 2^-n`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto (n : ℕ) : edist (f n) a ≤ 2 * C / 2^n := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at *, rw [mul_assoc, mul_comm], convert edist_le_of_edist_le_geometric_of_tendsto 2⁻¹ C hu ha n, rw [ennreal.one_sub_inv_two, ennreal.inv_inv] end /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f 0` to the limit of `f` is bounded above by `2 * C`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto₀: edist (f 0) a ≤ 2 * C := by simpa only [pow_zero, div_eq_mul_inv, ennreal.inv_one, mul_one] using edist_le_of_edist_le_geometric_two_of_tendsto C hu ha 0 end edist_le_geometric_two section le_geometric variables [pseudo_metric_space α] {r C : ℝ} (hr : r < 1) {f : ℕ → α} (hu : ∀n, dist (f n) (f (n+1)) ≤ C * r^n) include hr hu lemma aux_has_sum_of_le_geometric : has_sum (λ n : ℕ, C * r^n) (C / (1 - r)) := begin rcases sign_cases_of_C_mul_pow_nonneg (λ n, dist_nonneg.trans (hu n)) with rfl | ⟨C₀, r₀⟩, { simp [has_sum_zero] }, { refine has_sum.mul_left C _, simpa using has_sum_geometric_of_lt_1 r₀ hr } end variables (r C) /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then `f` is a Cauchy sequence. Note that this lemma does not assume `0 ≤ C` or `0 ≤ r`. -/ lemma cauchy_seq_of_le_geometric : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ C / (1 - r) := (aux_has_sum_of_le_geometric hr hu).tsum_eq ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ ha /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ (C * r^n) / (1 - r) := begin have := aux_has_sum_of_le_geometric hr hu, convert dist_le_tsum_of_dist_le_of_tendsto _ hu ⟨_, this⟩ ha n, simp only [pow_add, mul_left_comm C, mul_div_right_comm], rw [mul_comm], exact (this.mul_left _).tsum_eq.symm end omit hr hu variable (hu₂ : ∀ n, dist (f n) (f (n+1)) ≤ (C / 2) / 2^n) /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then `f` is a Cauchy sequence. -/ lemma cauchy_seq_of_le_geometric_two : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu₂ $ ⟨_, has_sum_geometric_two' C⟩ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C`. -/ lemma dist_le_of_le_geometric_two_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ C := (tsum_geometric_two' C) ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu₂ (summable_geometric_two' C) ha include hu₂ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f n` to the limit of `f` is bounded above by `C / 2^n`. -/ lemma dist_le_of_le_geometric_two_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ C / 2^n := begin convert dist_le_tsum_of_dist_le_of_tendsto _ hu₂ (summable_geometric_two' C) ha n, simp only [add_comm n, pow_add, ← div_div_eq_div_mul], symmetry, exact ((has_sum_geometric_two' C).div_const _).tsum_eq end end le_geometric section summable_le_geometric variables [semi_normed_group α] {r C : ℝ} {f : ℕ → α} lemma semi_normed_group.cauchy_seq_of_le_geometric {C : ℝ} {r : ℝ} (hr : r < 1) {u : ℕ → α} (h : ∀ n, ∥u n - u (n + 1)∥ ≤ C*r^n) : cauchy_seq u := cauchy_seq_of_le_geometric r C hr (by simpa [dist_eq_norm] using h) lemma dist_partial_sum_le_of_le_geometric (hf : ∀n, ∥f n∥ ≤ C * r^n) (n : ℕ) : dist (∑ i in range n, f i) (∑ i in range (n+1), f i) ≤ C * r ^ n := begin rw [sum_range_succ, dist_eq_norm, ← norm_neg, neg_sub, add_sub_cancel'], exact hf n, end /-- If `∥f n∥ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` form a Cauchy sequence. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/ lemma cauchy_seq_finset_of_geometric_bound (hr : r < 1) (hf : ∀n, ∥f n∥ ≤ C * r^n) : cauchy_seq (λ s : finset (ℕ), ∑ x in s, f x) := cauchy_seq_finset_of_norm_bounded _ (aux_has_sum_of_le_geometric hr (dist_partial_sum_le_of_le_geometric hf)).summable hf /-- If `∥f n∥ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` are within distance `C * r ^ n / (1 - r)` of the sum of the series. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/ lemma norm_sub_le_of_geometric_bound_of_has_sum (hr : r < 1) (hf : ∀n, ∥f n∥ ≤ C * r^n) {a : α} (ha : has_sum f a) (n : ℕ) : ∥(∑ x in finset.range n, f x) - a∥ ≤ (C * r ^ n) / (1 - r) := begin rw ← dist_eq_norm, apply dist_le_of_le_geometric_of_tendsto r C hr (dist_partial_sum_le_of_le_geometric hf), exact ha.tendsto_sum_nat end @[simp] lemma dist_partial_sum (u : ℕ → α) (n : ℕ) : dist (∑ k in range (n + 1), u k) (∑ k in range n, u k) = ∥u n∥ := by simp [dist_eq_norm, sum_range_succ] @[simp] lemma dist_partial_sum' (u : ℕ → α) (n : ℕ) : dist (∑ k in range n, u k) (∑ k in range (n+1), u k) = ∥u n∥ := by simp [dist_eq_norm', sum_range_succ] lemma cauchy_series_of_le_geometric {C : ℝ} {u : ℕ → α} {r : ℝ} (hr : r < 1) (h : ∀ n, ∥u n∥ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range n, u k) := cauchy_seq_of_le_geometric r C hr (by simp [h]) lemma normed_group.cauchy_series_of_le_geometric' {C : ℝ} {u : ℕ → α} {r : ℝ} (hr : r < 1) (h : ∀ n, ∥u n∥ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := begin by_cases hC : C = 0, { subst hC, simp at h, exact cauchy_seq_of_le_geometric 0 0 zero_lt_one (by simp [h]) }, have : 0 ≤ C, { simpa using (norm_nonneg _).trans (h 0) }, replace hC : 0 < C, from (ne.symm hC).le_iff_lt.mp this, have : 0 ≤ r, { have := (norm_nonneg _).trans (h 1), rw pow_one at this, exact (zero_le_mul_left hC).mp this }, simp_rw finset.sum_range_succ_comm, have : cauchy_seq u, { apply tendsto.cauchy_seq, apply squeeze_zero_norm h, rw show 0 = C*0, by simp, exact tendsto_const_nhds.mul (tendsto_pow_at_top_nhds_0_of_lt_1 this hr) }, exact this.add (cauchy_series_of_le_geometric hr h), end lemma normed_group.cauchy_series_of_le_geometric'' {C : ℝ} {u : ℕ → α} {N : ℕ} {r : ℝ} (hr₀ : 0 < r) (hr₁ : r < 1) (h : ∀ n ≥ N, ∥u n∥ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := begin set v : ℕ → α := λ n, if n < N then 0 else u n, have hC : 0 ≤ C, from (zero_le_mul_right $ pow_pos hr₀ N).mp ((norm_nonneg _).trans $ h N $ le_refl N), have : ∀ n ≥ N, u n = v n, { intros n hn, simp [v, hn, if_neg (not_lt.mpr hn)] }, refine cauchy_seq_sum_of_eventually_eq this (normed_group.cauchy_series_of_le_geometric' hr₁ _), { exact C }, intro n, dsimp [v], split_ifs with H H, { rw norm_zero, exact mul_nonneg hC (pow_nonneg hr₀.le _) }, { push_neg at H, exact h _ H } end end summable_le_geometric section normed_ring_geometric variables {R : Type*} [normed_ring R] [complete_space R] open normed_space /-- A geometric series in a complete normed ring is summable. Proved above (same name, different namespace) for not-necessarily-complete normed fields. -/ lemma normed_ring.summable_geometric_of_norm_lt_1 (x : R) (h : ∥x∥ < 1) : summable (λ (n:ℕ), x ^ n) := begin have h1 : summable (λ (n:ℕ), ∥x∥ ^ n) := summable_geometric_of_lt_1 (norm_nonneg _) h, refine summable_of_norm_bounded_eventually _ h1 _, rw nat.cofinite_eq_at_top, exact eventually_norm_pow_le x, end /-- Bound for the sum of a geometric series in a normed ring. This formula does not assume that the normed ring satisfies the axiom `∥1∥ = 1`. -/ lemma normed_ring.tsum_geometric_of_norm_lt_1 (x : R) (h : ∥x∥ < 1) : ∥∑' n:ℕ, x ^ n∥ ≤ ∥(1:R)∥ - 1 + (1 - ∥x∥)⁻¹ := begin rw tsum_eq_zero_add (normed_ring.summable_geometric_of_norm_lt_1 x h), simp only [pow_zero], refine le_trans (norm_add_le _ _) _, have : ∥∑' b : ℕ, (λ n, x ^ (n + 1)) b∥ ≤ (1 - ∥x∥)⁻¹ - 1, { refine tsum_of_norm_bounded _ (λ b, norm_pow_le' _ (nat.succ_pos b)), convert (has_sum_nat_add_iff' 1).mpr (has_sum_geometric_of_lt_1 (norm_nonneg x) h), simp }, linarith end lemma geom_series_mul_neg (x : R) (h : ∥x∥ < 1) : (∑' i:ℕ, x ^ i) * (1 - x) = 1 := begin have := ((normed_ring.summable_geometric_of_norm_lt_1 x h).has_sum.mul_right (1 - x)), refine tendsto_nhds_unique this.tendsto_sum_nat _, have : tendsto (λ (n : ℕ), 1 - x ^ n) at_top (𝓝 1), { simpa using tendsto_const_nhds.sub (tendsto_pow_at_top_nhds_0_of_norm_lt_1 h) }, convert ← this, ext n, rw [←geom_sum_mul_neg, geom_sum_def, finset.sum_mul], end lemma mul_neg_geom_series (x : R) (h : ∥x∥ < 1) : (1 - x) * ∑' i:ℕ, x ^ i = 1 := begin have := (normed_ring.summable_geometric_of_norm_lt_1 x h).has_sum.mul_left (1 - x), refine tendsto_nhds_unique this.tendsto_sum_nat _, have : tendsto (λ (n : ℕ), 1 - x ^ n) at_top (nhds 1), { simpa using tendsto_const_nhds.sub (tendsto_pow_at_top_nhds_0_of_norm_lt_1 h) }, convert ← this, ext n, rw [←mul_neg_geom_sum, geom_sum_def, finset.mul_sum] end end normed_ring_geometric /-! ### Summability tests based on comparison with geometric series -/ lemma summable_of_ratio_norm_eventually_le {α : Type*} [semi_normed_group α] [complete_space α] {f : ℕ → α} {r : ℝ} (hr₁ : r < 1) (h : ∀ᶠ n in at_top, ∥f (n+1)∥ ≤ r * ∥f n∥) : summable f := begin by_cases hr₀ : 0 ≤ r, { rw eventually_at_top at h, rcases h with ⟨N, hN⟩, rw ← @summable_nat_add_iff α _ _ _ _ N, refine summable_of_norm_bounded (λ n, ∥f N∥ * r^n) (summable.mul_left _ $ summable_geometric_of_lt_1 hr₀ hr₁) (λ n, _), conv_rhs {rw [mul_comm, ← zero_add N]}, refine le_geom hr₀ n (λ i _, _), convert hN (i + N) (N.le_add_left i) using 3, ac_refl }, { push_neg at hr₀, refine summable_of_norm_bounded_eventually 0 summable_zero _, rw nat.cofinite_eq_at_top, filter_upwards [h], intros n hn, by_contra h, push_neg at h, exact not_lt.mpr (norm_nonneg _) (lt_of_le_of_lt hn $ mul_neg_of_neg_of_pos hr₀ h) } end lemma summable_of_ratio_test_tendsto_lt_one {α : Type*} [normed_group α] [complete_space α] {f : ℕ → α} {l : ℝ} (hl₁ : l < 1) (hf : ∀ᶠ n in at_top, f n ≠ 0) (h : tendsto (λ n, ∥f (n+1)∥/∥f n∥) at_top (𝓝 l)) : summable f := begin rcases exists_between hl₁ with ⟨r, hr₀, hr₁⟩, refine summable_of_ratio_norm_eventually_le hr₁ _, filter_upwards [eventually_le_of_tendsto_lt hr₀ h, hf], intros n h₀ h₁, rwa ← div_le_iff (norm_pos_iff.mpr h₁) end lemma not_summable_of_ratio_norm_eventually_ge {α : Type*} [semi_normed_group α] {f : ℕ → α} {r : ℝ} (hr : 1 < r) (hf : ∃ᶠ n in at_top, ∥f n∥ ≠ 0) (h : ∀ᶠ n in at_top, r * ∥f n∥ ≤ ∥f (n+1)∥) : ¬ summable f := begin rw eventually_at_top at h, rcases h with ⟨N₀, hN₀⟩, rw frequently_at_top at hf, rcases hf N₀ with ⟨N, hNN₀ : N₀ ≤ N, hN⟩, rw ← @summable_nat_add_iff α _ _ _ _ N, refine mt summable.tendsto_at_top_zero (λ h', not_tendsto_at_top_of_tendsto_nhds (tendsto_norm_zero.comp h') _), convert tendsto_at_top_of_geom_le _ hr _, { refine lt_of_le_of_ne (norm_nonneg _) _, intro h'', specialize hN₀ N hNN₀, simp only [comp_app, zero_add] at h'', exact hN h''.symm }, { intro i, dsimp only [comp_app], convert (hN₀ (i + N) (hNN₀.trans (N.le_add_left i))) using 3, ac_refl } end lemma not_summable_of_ratio_test_tendsto_gt_one {α : Type*} [semi_normed_group α] {f : ℕ → α} {l : ℝ} (hl : 1 < l) (h : tendsto (λ n, ∥f (n+1)∥/∥f n∥) at_top (𝓝 l)) : ¬ summable f := begin have key : ∀ᶠ n in at_top, ∥f n∥ ≠ 0, { filter_upwards [eventually_ge_of_tendsto_gt hl h], intros n hn hc, rw [hc, div_zero] at hn, linarith }, rcases exists_between hl with ⟨r, hr₀, hr₁⟩, refine not_summable_of_ratio_norm_eventually_ge hr₀ key.frequently _, filter_upwards [eventually_ge_of_tendsto_gt hr₁ h, key], intros n h₀ h₁, rwa ← le_div_iff (lt_of_le_of_ne (norm_nonneg _) h₁.symm) end /-- A series whose terms are bounded by the terms of a converging geometric series converges. -/ lemma summable_one_div_pow_of_le {m : ℝ} {f : ℕ → ℕ} (hm : 1 < m) (fi : ∀ i, i ≤ f i) : summable (λ i, 1 / m ^ f i) := begin refine summable_of_nonneg_of_le (λ a, one_div_nonneg.mpr (pow_nonneg (zero_le_one.trans hm.le) _)) (λ a, _) (summable_geometric_of_lt_1 (one_div_nonneg.mpr (zero_le_one.trans hm.le)) ((one_div_lt (zero_lt_one.trans hm) zero_lt_one).mpr (one_div_one.le.trans_lt hm))), rw [div_pow, one_pow], refine (one_div_le_one_div _ _).mpr (pow_le_pow hm.le (fi a)); exact pow_pos (zero_lt_one.trans hm) _ end /-! ### Positive sequences with small sums on encodable types -/ /-- For any positive `ε`, define on an encodable type a positive sequence with sum less than `ε` -/ def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, has_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : has_sum f ε := has_sum_geometric_two' _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos zero_lt_two _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, rcases hf.summable.comp_injective (@encodable.encode_injective ι _) with ⟨c, hg⟩, refine ⟨c, hg, has_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩, { assume i _, exact le_of_lt (f0 _) }, { assume n, exact le_refl _ } end namespace nnreal theorem exists_pos_sum_of_encodable {ε : ℝ≥0} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → ℝ≥0, (∀ i, 0 < ε' i) ∧ ∃c, has_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := exists_between hε in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt_coe.2 $ hε' i, ⟨c, has_sum_le (assume i, le_of_lt $ hε' i) has_sum_zero hc ⟩, nnreal.has_sum_coe.1 hc, lt_of_le_of_lt (nnreal.coe_le_coe.1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ℝ≥0∞} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → ℝ≥0, (∀ i, 0 < ε' i) ∧ ∑' i, (ε' i : ℝ≥0∞) < ε := begin rcases exists_between hε with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end theorem exists_pos_sum_of_encodable' {ε : ℝ≥0∞} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → ℝ≥0∞, (∀ i, 0 < ε' i) ∧ (∑' i, ε' i) < ε := let ⟨δ, δpos, hδ⟩ := exists_pos_sum_of_encodable hε ι in ⟨λ i, δ i, λ i, ennreal.coe_pos.2 (δpos i), hδ⟩ end ennreal /-! ### Factorial -/ lemma factorial_tendsto_at_top : tendsto nat.factorial at_top at_top := tendsto_at_top_at_top_of_monotone nat.monotone_factorial (λ n, ⟨n, n.self_le_factorial⟩) lemma tendsto_factorial_div_pow_self_at_top : tendsto (λ n, n! / n^n : ℕ → ℝ) at_top (𝓝 0) := tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_const_div_at_top_nhds_0_nat 1) (eventually_of_forall $ λ n, div_nonneg (by exact_mod_cast n.factorial_pos.le) (pow_nonneg (by exact_mod_cast n.zero_le) _)) begin refine (eventually_gt_at_top 0).mono (λ n hn, _), rcases nat.exists_eq_succ_of_ne_zero hn.ne.symm with ⟨k, rfl⟩, rw [← prod_range_add_one_eq_factorial, pow_eq_prod_const, div_eq_mul_inv, ← inv_eq_one_div, prod_nat_cast, nat.cast_succ, ← prod_inv_distrib', ← prod_mul_distrib, finset.prod_range_succ'], simp only [prod_range_succ', one_mul, nat.cast_add, zero_add, nat.cast_one], refine mul_le_of_le_one_left (inv_nonneg.mpr $ by exact_mod_cast hn.le) (prod_le_one _ _); intros x hx; rw finset.mem_range at hx, { refine mul_nonneg _ (inv_nonneg.mpr _); norm_cast; linarith }, { refine (div_le_one $ by exact_mod_cast hn).mpr _, norm_cast, linarith } end /-! ### Ceil and floor -/ section variables {R : Type*} [topological_space R] [linear_ordered_field R] [order_topology R] [floor_ring R] lemma tendsto_nat_floor_mul_div_at_top {a : R} (ha : 0 ≤ a) : tendsto (λ x, (⌊a * x⌋₊ : R) / x) at_top (𝓝 a) := begin have A : tendsto (λ (x : R), a - x⁻¹) at_top (𝓝 (a - 0)) := tendsto_const_nhds.sub tendsto_inv_at_top_zero, rw sub_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' A tendsto_const_nhds, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, simp only [le_div_iff (zero_lt_one.trans_le hx), sub_mul, inv_mul_cancel (zero_lt_one.trans_le hx).ne'], have := lt_nat_floor_add_one (a * x), linarith }, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, rw div_le_iff (zero_lt_one.trans_le hx), simp [nat_floor_le (mul_nonneg ha (zero_le_one.trans hx))] } end lemma tendsto_nat_ceil_mul_div_at_top {a : R} (ha : 0 ≤ a) : tendsto (λ x, (⌈a * x⌉₊ : R) / x) at_top (𝓝 a) := begin have A : tendsto (λ (x : R), a + x⁻¹) at_top (𝓝 (a + 0)) := tendsto_const_nhds.add tendsto_inv_at_top_zero, rw add_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds A, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, rw le_div_iff (zero_lt_one.trans_le hx), exact le_nat_ceil _ }, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, simp [div_le_iff (zero_lt_one.trans_le hx), inv_mul_cancel (zero_lt_one.trans_le hx).ne', (nat_ceil_lt_add_one ((mul_nonneg ha (zero_le_one.trans hx)))).le, add_mul] } end end
303489eff648cc284d8e736775b10a3a543e57d0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/hom/lattice.lean
771552ff68c303b7655822a62a4d0005832012dc
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
49,342
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.hom.bounded import order.symm_diff /-! # Lattice homomorphisms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines (bounded) lattice homomorphisms. We use the `fun_like` design, so each type of morphisms has a companion typeclass which is meant to be satisfied by itself and all stricter types. ## Types of morphisms * `sup_hom`: Maps which preserve `⊔`. * `inf_hom`: Maps which preserve `⊓`. * `sup_bot_hom`: Finitary supremum homomorphisms. Maps which preserve `⊔` and `⊥`. * `inf_top_hom`: Finitary infimum homomorphisms. Maps which preserve `⊓` and `⊤`. * `lattice_hom`: Lattice homomorphisms. Maps which preserve `⊔` and `⊓`. * `bounded_lattice_hom`: Bounded lattice homomorphisms. Maps which preserve `⊤`, `⊥`, `⊔` and `⊓`. ## Typeclasses * `sup_hom_class` * `inf_hom_class` * `sup_bot_hom_class` * `inf_top_hom_class` * `lattice_hom_class` * `bounded_lattice_hom_class` ## TODO Do we need more intersections between `bot_hom`, `top_hom` and lattice homomorphisms? -/ open function order_dual variables {F ι α β γ δ : Type*} /-- The type of `⊔`-preserving functions from `α` to `β`. -/ structure sup_hom (α β : Type*) [has_sup α] [has_sup β] := (to_fun : α → β) (map_sup' (a b : α) : to_fun (a ⊔ b) = to_fun a ⊔ to_fun b) /-- The type of `⊓`-preserving functions from `α` to `β`. -/ structure inf_hom (α β : Type*) [has_inf α] [has_inf β] := (to_fun : α → β) (map_inf' (a b : α) : to_fun (a ⊓ b) = to_fun a ⊓ to_fun b) /-- The type of finitary supremum-preserving homomorphisms from `α` to `β`. -/ structure sup_bot_hom (α β : Type*) [has_sup α] [has_sup β] [has_bot α] [has_bot β] extends sup_hom α β := (map_bot' : to_fun ⊥ = ⊥) /-- The type of finitary infimum-preserving homomorphisms from `α` to `β`. -/ structure inf_top_hom (α β : Type*) [has_inf α] [has_inf β] [has_top α] [has_top β] extends inf_hom α β := (map_top' : to_fun ⊤ = ⊤) /-- The type of lattice homomorphisms from `α` to `β`. -/ structure lattice_hom (α β : Type*) [lattice α] [lattice β] extends sup_hom α β := (map_inf' (a b : α) : to_fun (a ⊓ b) = to_fun a ⊓ to_fun b) /-- The type of bounded lattice homomorphisms from `α` to `β`. -/ structure bounded_lattice_hom (α β : Type*) [lattice α] [lattice β] [bounded_order α] [bounded_order β] extends lattice_hom α β := (map_top' : to_fun ⊤ = ⊤) (map_bot' : to_fun ⊥ = ⊥) section set_option old_structure_cmd true /-- `sup_hom_class F α β` states that `F` is a type of `⊔`-preserving morphisms. You should extend this class when you extend `sup_hom`. -/ class sup_hom_class (F : Type*) (α β : out_param $ Type*) [has_sup α] [has_sup β] extends fun_like F α (λ _, β) := (map_sup (f : F) (a b : α) : f (a ⊔ b) = f a ⊔ f b) /-- `inf_hom_class F α β` states that `F` is a type of `⊓`-preserving morphisms. You should extend this class when you extend `inf_hom`. -/ class inf_hom_class (F : Type*) (α β : out_param $ Type*) [has_inf α] [has_inf β] extends fun_like F α (λ _, β) := (map_inf (f : F) (a b : α) : f (a ⊓ b) = f a ⊓ f b) /-- `sup_bot_hom_class F α β` states that `F` is a type of finitary supremum-preserving morphisms. You should extend this class when you extend `sup_bot_hom`. -/ class sup_bot_hom_class (F : Type*) (α β : out_param $ Type*) [has_sup α] [has_sup β] [has_bot α] [has_bot β] extends sup_hom_class F α β := (map_bot (f : F) : f ⊥ = ⊥) /-- `inf_top_hom_class F α β` states that `F` is a type of finitary infimum-preserving morphisms. You should extend this class when you extend `sup_bot_hom`. -/ class inf_top_hom_class (F : Type*) (α β : out_param $ Type*) [has_inf α] [has_inf β] [has_top α] [has_top β] extends inf_hom_class F α β := (map_top (f : F) : f ⊤ = ⊤) /-- `lattice_hom_class F α β` states that `F` is a type of lattice morphisms. You should extend this class when you extend `lattice_hom`. -/ class lattice_hom_class (F : Type*) (α β : out_param $ Type*) [lattice α] [lattice β] extends sup_hom_class F α β := (map_inf (f : F) (a b : α) : f (a ⊓ b) = f a ⊓ f b) /-- `bounded_lattice_hom_class F α β` states that `F` is a type of bounded lattice morphisms. You should extend this class when you extend `bounded_lattice_hom`. -/ class bounded_lattice_hom_class (F : Type*) (α β : out_param $ Type*) [lattice α] [lattice β] [bounded_order α] [bounded_order β] extends lattice_hom_class F α β := (map_top (f : F) : f ⊤ = ⊤) (map_bot (f : F) : f ⊥ = ⊥) end export sup_hom_class (map_sup) export inf_hom_class (map_inf) attribute [simp] map_top map_bot map_sup map_inf @[priority 100] -- See note [lower instance priority] instance sup_hom_class.to_order_hom_class [semilattice_sup α] [semilattice_sup β] [sup_hom_class F α β] : order_hom_class F α β := { map_rel := λ f a b h, by rw [←sup_eq_right, ←map_sup, sup_eq_right.2 h], ..‹sup_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance inf_hom_class.to_order_hom_class [semilattice_inf α] [semilattice_inf β] [inf_hom_class F α β] : order_hom_class F α β := { map_rel := λ f a b h, by rw [←inf_eq_left, ←map_inf, inf_eq_left.2 h] ..‹inf_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance sup_bot_hom_class.to_bot_hom_class [has_sup α] [has_sup β] [has_bot α] [has_bot β] [sup_bot_hom_class F α β] : bot_hom_class F α β := { .. ‹sup_bot_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance inf_top_hom_class.to_top_hom_class [has_inf α] [has_inf β] [has_top α] [has_top β] [inf_top_hom_class F α β] : top_hom_class F α β := { .. ‹inf_top_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance lattice_hom_class.to_inf_hom_class [lattice α] [lattice β] [lattice_hom_class F α β] : inf_hom_class F α β := { .. ‹lattice_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance bounded_lattice_hom_class.to_sup_bot_hom_class [lattice α] [lattice β] [bounded_order α] [bounded_order β] [bounded_lattice_hom_class F α β] : sup_bot_hom_class F α β := { .. ‹bounded_lattice_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance bounded_lattice_hom_class.to_inf_top_hom_class [lattice α] [lattice β] [bounded_order α] [bounded_order β] [bounded_lattice_hom_class F α β] : inf_top_hom_class F α β := { .. ‹bounded_lattice_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance bounded_lattice_hom_class.to_bounded_order_hom_class [lattice α] [lattice β] [bounded_order α] [bounded_order β] [bounded_lattice_hom_class F α β] : bounded_order_hom_class F α β := { .. show order_hom_class F α β, from infer_instance, .. ‹bounded_lattice_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_sup_hom_class [semilattice_sup α] [semilattice_sup β] [order_iso_class F α β] : sup_hom_class F α β := { map_sup := λ f a b, eq_of_forall_ge_iff $ λ c, by simp only [←le_map_inv_iff, sup_le_iff], .. show order_hom_class F α β, from infer_instance } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_inf_hom_class [semilattice_inf α] [semilattice_inf β] [order_iso_class F α β] : inf_hom_class F α β := { map_inf := λ f a b, eq_of_forall_le_iff $ λ c, by simp only [←map_inv_le_iff, le_inf_iff], .. show order_hom_class F α β, from infer_instance } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_sup_bot_hom_class [semilattice_sup α] [order_bot α] [semilattice_sup β] [order_bot β] [order_iso_class F α β] : sup_bot_hom_class F α β := { ..order_iso_class.to_sup_hom_class, ..order_iso_class.to_bot_hom_class } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_inf_top_hom_class [semilattice_inf α] [order_top α] [semilattice_inf β] [order_top β] [order_iso_class F α β] : inf_top_hom_class F α β := { ..order_iso_class.to_inf_hom_class, ..order_iso_class.to_top_hom_class } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_lattice_hom_class [lattice α] [lattice β] [order_iso_class F α β] : lattice_hom_class F α β := { ..order_iso_class.to_sup_hom_class, ..order_iso_class.to_inf_hom_class } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_bounded_lattice_hom_class [lattice α] [lattice β] [bounded_order α] [bounded_order β] [order_iso_class F α β] : bounded_lattice_hom_class F α β := { ..order_iso_class.to_lattice_hom_class, ..order_iso_class.to_bounded_order_hom_class } section bounded_lattice variables [lattice α] [bounded_order α] [lattice β] [bounded_order β] [bounded_lattice_hom_class F α β] (f : F) {a b : α} include β lemma disjoint.map (h : disjoint a b) : disjoint (f a) (f b) := by rw [disjoint_iff, ←map_inf, h.eq_bot, map_bot] lemma codisjoint.map (h : codisjoint a b) : codisjoint (f a) (f b) := by rw [codisjoint_iff, ←map_sup, h.eq_top, map_top] lemma is_compl.map (h : is_compl a b) : is_compl (f a) (f b) := ⟨h.1.map _, h.2.map _⟩ end bounded_lattice section boolean_algebra variables [boolean_algebra α] [boolean_algebra β] [bounded_lattice_hom_class F α β] (f : F) include β /-- Special case of `map_compl` for boolean algebras. -/ lemma map_compl' (a : α) : f aᶜ = (f a)ᶜ := (is_compl_compl.map _).compl_eq.symm /-- Special case of `map_sdiff` for boolean algebras. -/ lemma map_sdiff' (a b : α) : f (a \ b) = f a \ f b := by rw [sdiff_eq, sdiff_eq, map_inf, map_compl'] /-- Special case of `map_symm_diff` for boolean algebras. -/ lemma map_symm_diff' (a b : α) : f (a ∆ b) = f a ∆ f b := by rw [symm_diff, symm_diff, map_sup, map_sdiff', map_sdiff'] end boolean_algebra instance [has_sup α] [has_sup β] [sup_hom_class F α β] : has_coe_t F (sup_hom α β) := ⟨λ f, ⟨f, map_sup f⟩⟩ instance [has_inf α] [has_inf β] [inf_hom_class F α β] : has_coe_t F (inf_hom α β) := ⟨λ f, ⟨f, map_inf f⟩⟩ instance [has_sup α] [has_sup β] [has_bot α] [has_bot β] [sup_bot_hom_class F α β] : has_coe_t F (sup_bot_hom α β) := ⟨λ f, ⟨f, map_bot f⟩⟩ instance [has_inf α] [has_inf β] [has_top α] [has_top β] [inf_top_hom_class F α β] : has_coe_t F (inf_top_hom α β) := ⟨λ f, ⟨f, map_top f⟩⟩ instance [lattice α] [lattice β] [lattice_hom_class F α β] : has_coe_t F (lattice_hom α β) := ⟨λ f, { to_fun := f, map_sup' := map_sup f, map_inf' := map_inf f }⟩ instance [lattice α] [lattice β] [bounded_order α] [bounded_order β] [bounded_lattice_hom_class F α β] : has_coe_t F (bounded_lattice_hom α β) := ⟨λ f, { to_fun := f, map_top' := map_top f, map_bot' := map_bot f, ..(f : lattice_hom α β) }⟩ /-! ### Supremum homomorphisms -/ namespace sup_hom variables [has_sup α] section has_sup variables [has_sup β] [has_sup γ] [has_sup δ] instance : sup_hom_class (sup_hom α β) α β := { coe := sup_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_sup := sup_hom.map_sup' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (sup_hom α β) (λ _, α → β) := ⟨λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : sup_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : sup_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `sup_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : sup_hom α β) (f' : α → β) (h : f' = f) : sup_hom α β := { to_fun := f', map_sup' := h.symm ▸ f.map_sup' } @[simp] lemma coe_copy (f : sup_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : sup_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `sup_hom`. -/ protected def id : sup_hom α α := ⟨id, λ a b, rfl⟩ instance : inhabited (sup_hom α α) := ⟨sup_hom.id α⟩ @[simp] lemma coe_id : ⇑(sup_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : sup_hom.id α a = a := rfl /-- Composition of `sup_hom`s as a `sup_hom`. -/ def comp (f : sup_hom β γ) (g : sup_hom α β) : sup_hom α γ := { to_fun := f ∘ g, map_sup' := λ a b, by rw [comp_apply, map_sup, map_sup] } @[simp] lemma coe_comp (f : sup_hom β γ) (g : sup_hom α β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : sup_hom β γ) (g : sup_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : sup_hom γ δ) (g : sup_hom β γ) (h : sup_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : sup_hom α β) : f.comp (sup_hom.id α) = f := sup_hom.ext $ λ a, rfl @[simp] lemma id_comp (f : sup_hom α β) : (sup_hom.id β).comp f = f := sup_hom.ext $ λ a, rfl lemma cancel_right {g₁ g₂ : sup_hom β γ} {f : sup_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, sup_hom.ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : sup_hom β γ} {f₁ f₂ : sup_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, sup_hom.ext $ λ a, hg $ by rw [←sup_hom.comp_apply, h, sup_hom.comp_apply], congr_arg _⟩ end has_sup variables (α) [semilattice_sup β] /-- The constant function as a `sup_hom`. -/ def const (b : β) : sup_hom α β := ⟨λ _, b, λ _ _, sup_idem.symm⟩ @[simp] lemma coe_const (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] lemma const_apply (b : β) (a : α) : const α b a = b := rfl variables {α} instance : has_sup (sup_hom α β) := ⟨λ f g, ⟨f ⊔ g, λ a b, by { rw [pi.sup_apply, map_sup, map_sup], exact sup_sup_sup_comm _ _ _ _ }⟩⟩ instance : semilattice_sup (sup_hom α β) := fun_like.coe_injective.semilattice_sup _ $ λ f g, rfl instance [has_bot β] : has_bot (sup_hom α β) := ⟨sup_hom.const α ⊥⟩ instance [has_top β] : has_top (sup_hom α β) := ⟨sup_hom.const α ⊤⟩ instance [order_bot β] : order_bot (sup_hom α β) := order_bot.lift (coe_fn : _ → α → β) (λ _ _, id) rfl instance [order_top β] : order_top (sup_hom α β) := order_top.lift (coe_fn : _ → α → β) (λ _ _, id) rfl instance [bounded_order β] : bounded_order (sup_hom α β) := bounded_order.lift (coe_fn : _ → α → β) (λ _ _, id) rfl rfl @[simp] lemma coe_sup (f g : sup_hom α β) : ⇑(f ⊔ g) = f ⊔ g := rfl @[simp] lemma coe_bot [has_bot β] : ⇑(⊥ : sup_hom α β) = ⊥ := rfl @[simp] lemma coe_top [has_top β] : ⇑(⊤ : sup_hom α β) = ⊤ := rfl @[simp] lemma sup_apply (f g : sup_hom α β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl @[simp] lemma bot_apply [has_bot β] (a : α) : (⊥ : sup_hom α β) a = ⊥ := rfl @[simp] lemma top_apply [has_top β] (a : α) : (⊤ : sup_hom α β) a = ⊤ := rfl end sup_hom /-! ### Infimum homomorphisms -/ namespace inf_hom variables [has_inf α] section has_inf variables [has_inf β] [has_inf γ] [has_inf δ] instance : inf_hom_class (inf_hom α β) α β := { coe := inf_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_inf := inf_hom.map_inf' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (inf_hom α β) (λ _, α → β) := ⟨λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : inf_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : inf_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of an `inf_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : inf_hom α β) (f' : α → β) (h : f' = f) : inf_hom α β := { to_fun := f', map_inf' := h.symm ▸ f.map_inf' } @[simp] lemma coe_copy (f : inf_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : inf_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as an `inf_hom`. -/ protected def id : inf_hom α α := ⟨id, λ a b, rfl⟩ instance : inhabited (inf_hom α α) := ⟨inf_hom.id α⟩ @[simp] lemma coe_id : ⇑(inf_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : inf_hom.id α a = a := rfl /-- Composition of `inf_hom`s as an `inf_hom`. -/ def comp (f : inf_hom β γ) (g : inf_hom α β) : inf_hom α γ := { to_fun := f ∘ g, map_inf' := λ a b, by rw [comp_apply, map_inf, map_inf] } @[simp] lemma coe_comp (f : inf_hom β γ) (g : inf_hom α β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : inf_hom β γ) (g : inf_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : inf_hom γ δ) (g : inf_hom β γ) (h : inf_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : inf_hom α β) : f.comp (inf_hom.id α) = f := inf_hom.ext $ λ a, rfl @[simp] lemma id_comp (f : inf_hom α β) : (inf_hom.id β).comp f = f := inf_hom.ext $ λ a, rfl lemma cancel_right {g₁ g₂ : inf_hom β γ} {f : inf_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, inf_hom.ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : inf_hom β γ} {f₁ f₂ : inf_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, inf_hom.ext $ λ a, hg $ by rw [←inf_hom.comp_apply, h, inf_hom.comp_apply], congr_arg _⟩ end has_inf variables (α) [semilattice_inf β] /-- The constant function as an `inf_hom`. -/ def const (b : β) : inf_hom α β := ⟨λ _, b, λ _ _, inf_idem.symm⟩ @[simp] lemma coe_const (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] lemma const_apply (b : β) (a : α) : const α b a = b := rfl variables {α} instance : has_inf (inf_hom α β) := ⟨λ f g, ⟨f ⊓ g, λ a b, by { rw [pi.inf_apply, map_inf, map_inf], exact inf_inf_inf_comm _ _ _ _ }⟩⟩ instance : semilattice_inf (inf_hom α β) := fun_like.coe_injective.semilattice_inf _ $ λ f g, rfl instance [has_bot β] : has_bot (inf_hom α β) := ⟨inf_hom.const α ⊥⟩ instance [has_top β] : has_top (inf_hom α β) := ⟨inf_hom.const α ⊤⟩ instance [order_bot β] : order_bot (inf_hom α β) := order_bot.lift (coe_fn : _ → α → β) (λ _ _, id) rfl instance [order_top β] : order_top (inf_hom α β) := order_top.lift (coe_fn : _ → α → β) (λ _ _, id) rfl instance [bounded_order β] : bounded_order (inf_hom α β) := bounded_order.lift (coe_fn : _ → α → β) (λ _ _, id) rfl rfl @[simp] lemma coe_inf (f g : inf_hom α β) : ⇑(f ⊓ g) = f ⊓ g := rfl @[simp] lemma coe_bot [has_bot β] : ⇑(⊥ : inf_hom α β) = ⊥ := rfl @[simp] lemma coe_top [has_top β] : ⇑(⊤ : inf_hom α β) = ⊤ := rfl @[simp] lemma inf_apply (f g : inf_hom α β) (a : α) : (f ⊓ g) a = f a ⊓ g a := rfl @[simp] lemma bot_apply [has_bot β] (a : α) : (⊥ : inf_hom α β) a = ⊥ := rfl @[simp] lemma top_apply [has_top β] (a : α) : (⊤ : inf_hom α β) a = ⊤ := rfl end inf_hom /-! ### Finitary supremum homomorphisms -/ namespace sup_bot_hom variables [has_sup α] [has_bot α] section has_sup variables [has_sup β] [has_bot β] [has_sup γ] [has_bot γ] [has_sup δ] [has_bot δ] /-- Reinterpret a `sup_bot_hom` as a `bot_hom`. -/ def to_bot_hom (f : sup_bot_hom α β) : bot_hom α β := { ..f } instance : sup_bot_hom_class (sup_bot_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_sup := λ f, f.map_sup', map_bot := λ f, f.map_bot' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (sup_bot_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : sup_bot_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : sup_bot_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `sup_bot_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : sup_bot_hom α β) (f' : α → β) (h : f' = f) : sup_bot_hom α β := { to_sup_hom := f.to_sup_hom.copy f' h, ..f.to_bot_hom.copy f' h } @[simp] lemma coe_copy (f : sup_bot_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : sup_bot_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `sup_bot_hom`. -/ @[simps] protected def id : sup_bot_hom α α := ⟨sup_hom.id α, rfl⟩ instance : inhabited (sup_bot_hom α α) := ⟨sup_bot_hom.id α⟩ @[simp] lemma coe_id : ⇑(sup_bot_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : sup_bot_hom.id α a = a := rfl /-- Composition of `sup_bot_hom`s as a `sup_bot_hom`. -/ def comp (f : sup_bot_hom β γ) (g : sup_bot_hom α β) : sup_bot_hom α γ := { ..f.to_sup_hom.comp g.to_sup_hom, ..f.to_bot_hom.comp g.to_bot_hom } @[simp] lemma coe_comp (f : sup_bot_hom β γ) (g : sup_bot_hom α β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : sup_bot_hom β γ) (g : sup_bot_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : sup_bot_hom γ δ) (g : sup_bot_hom β γ) (h : sup_bot_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : sup_bot_hom α β) : f.comp (sup_bot_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : sup_bot_hom α β) : (sup_bot_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : sup_bot_hom β γ} {f : sup_bot_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : sup_bot_hom β γ} {f₁ f₂ : sup_bot_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, sup_bot_hom.ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end has_sup variables [semilattice_sup β] [order_bot β] instance : has_sup (sup_bot_hom α β) := ⟨λ f g, { to_sup_hom := f.to_sup_hom ⊔ g.to_sup_hom, ..f.to_bot_hom ⊔ g.to_bot_hom }⟩ instance : semilattice_sup (sup_bot_hom α β) := fun_like.coe_injective.semilattice_sup _ $ λ f g, rfl instance : order_bot (sup_bot_hom α β) := { bot := ⟨⊥, rfl⟩, bot_le := λ f, bot_le } @[simp] lemma coe_sup (f g : sup_bot_hom α β) : ⇑(f ⊔ g) = f ⊔ g := rfl @[simp] lemma coe_bot : ⇑(⊥ : sup_bot_hom α β) = ⊥ := rfl @[simp] lemma sup_apply (f g : sup_bot_hom α β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl @[simp] lemma bot_apply (a : α) : (⊥ : sup_bot_hom α β) a = ⊥ := rfl end sup_bot_hom /-! ### Finitary infimum homomorphisms -/ namespace inf_top_hom variables [has_inf α] [has_top α] section has_inf variables [has_inf β] [has_top β] [has_inf γ] [has_top γ] [has_inf δ] [has_top δ] /-- Reinterpret an `inf_top_hom` as a `top_hom`. -/ def to_top_hom (f : inf_top_hom α β) : top_hom α β := { ..f } instance : inf_top_hom_class (inf_top_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_inf := λ f, f.map_inf', map_top := λ f, f.map_top' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (inf_top_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : inf_top_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : inf_top_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of an `inf_top_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : inf_top_hom α β) (f' : α → β) (h : f' = f) : inf_top_hom α β := { to_inf_hom := f.to_inf_hom.copy f' h, ..f.to_top_hom.copy f' h } @[simp] lemma coe_copy (f : inf_top_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : inf_top_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as an `inf_top_hom`. -/ @[simps] protected def id : inf_top_hom α α := ⟨inf_hom.id α, rfl⟩ instance : inhabited (inf_top_hom α α) := ⟨inf_top_hom.id α⟩ @[simp] lemma coe_id : ⇑(inf_top_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : inf_top_hom.id α a = a := rfl /-- Composition of `inf_top_hom`s as an `inf_top_hom`. -/ def comp (f : inf_top_hom β γ) (g : inf_top_hom α β) : inf_top_hom α γ := { ..f.to_inf_hom.comp g.to_inf_hom, ..f.to_top_hom.comp g.to_top_hom } @[simp] lemma coe_comp (f : inf_top_hom β γ) (g : inf_top_hom α β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : inf_top_hom β γ) (g : inf_top_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : inf_top_hom γ δ) (g : inf_top_hom β γ) (h : inf_top_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : inf_top_hom α β) : f.comp (inf_top_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : inf_top_hom α β) : (inf_top_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : inf_top_hom β γ} {f : inf_top_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : inf_top_hom β γ} {f₁ f₂ : inf_top_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, inf_top_hom.ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end has_inf variables [semilattice_inf β] [order_top β] instance : has_inf (inf_top_hom α β) := ⟨λ f g, { to_inf_hom := f.to_inf_hom ⊓ g.to_inf_hom, ..f.to_top_hom ⊓ g.to_top_hom }⟩ instance : semilattice_inf (inf_top_hom α β) := fun_like.coe_injective.semilattice_inf _ $ λ f g, rfl instance : order_top (inf_top_hom α β) := { top := ⟨⊤, rfl⟩, le_top := λ f, le_top } @[simp] lemma coe_inf (f g : inf_top_hom α β) : ⇑(f ⊓ g) = f ⊓ g := rfl @[simp] lemma coe_top : ⇑(⊤ : inf_top_hom α β) = ⊤ := rfl @[simp] lemma inf_apply (f g : inf_top_hom α β) (a : α) : (f ⊓ g) a = f a ⊓ g a := rfl @[simp] lemma top_apply (a : α) : (⊤ : inf_top_hom α β) a = ⊤ := rfl end inf_top_hom /-! ### Lattice homomorphisms -/ namespace lattice_hom variables [lattice α] [lattice β] [lattice γ] [lattice δ] /-- Reinterpret a `lattice_hom` as an `inf_hom`. -/ def to_inf_hom (f : lattice_hom α β) : inf_hom α β := { ..f } instance : lattice_hom_class (lattice_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by obtain ⟨⟨_, _⟩, _⟩ := f; obtain ⟨⟨_, _⟩, _⟩ := g; congr', map_sup := λ f, f.map_sup', map_inf := λ f, f.map_inf' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (lattice_hom α β) (λ _, α → β) := ⟨λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : lattice_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : lattice_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `lattice_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : lattice_hom α β) (f' : α → β) (h : f' = f) : lattice_hom α β := { .. f.to_sup_hom.copy f' h, .. f.to_inf_hom.copy f' h } @[simp] lemma coe_copy (f : lattice_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : lattice_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `lattice_hom`. -/ protected def id : lattice_hom α α := { to_fun := id, map_sup' := λ _ _, rfl, map_inf' := λ _ _, rfl } instance : inhabited (lattice_hom α α) := ⟨lattice_hom.id α⟩ @[simp] lemma coe_id : ⇑(lattice_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : lattice_hom.id α a = a := rfl /-- Composition of `lattice_hom`s as a `lattice_hom`. -/ def comp (f : lattice_hom β γ) (g : lattice_hom α β) : lattice_hom α γ := { ..f.to_sup_hom.comp g.to_sup_hom, ..f.to_inf_hom.comp g.to_inf_hom } @[simp] lemma coe_comp (f : lattice_hom β γ) (g : lattice_hom α β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : lattice_hom β γ) (g : lattice_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma coe_comp_sup_hom (f : lattice_hom β γ) (g : lattice_hom α β) : (f.comp g : sup_hom α γ) = (f : sup_hom β γ).comp g := rfl @[simp] lemma coe_comp_inf_hom (f : lattice_hom β γ) (g : lattice_hom α β) : (f.comp g : inf_hom α γ) = (f : inf_hom β γ).comp g := rfl @[simp] lemma comp_assoc (f : lattice_hom γ δ) (g : lattice_hom β γ) (h : lattice_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : lattice_hom α β) : f.comp (lattice_hom.id α) = f := lattice_hom.ext $ λ a, rfl @[simp] lemma id_comp (f : lattice_hom α β) : (lattice_hom.id β).comp f = f := lattice_hom.ext $ λ a, rfl lemma cancel_right {g₁ g₂ : lattice_hom β γ} {f : lattice_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, lattice_hom.ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : lattice_hom β γ} {f₁ f₂ : lattice_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, lattice_hom.ext $ λ a, hg $ by rw [←lattice_hom.comp_apply, h, lattice_hom.comp_apply], congr_arg _⟩ end lattice_hom namespace order_hom_class variables (α β) [linear_order α] [lattice β] [order_hom_class F α β] /-- An order homomorphism from a linear order is a lattice homomorphism. -/ @[reducible] def to_lattice_hom_class : lattice_hom_class F α β := { map_sup := λ f a b, begin obtain h | h := le_total a b, { rw [sup_eq_right.2 h, sup_eq_right.2 (order_hom_class.mono f h : f a ≤ f b)] }, { rw [sup_eq_left.2 h, sup_eq_left.2 (order_hom_class.mono f h : f b ≤ f a)] } end, map_inf := λ f a b, begin obtain h | h := le_total a b, { rw [inf_eq_left.2 h, inf_eq_left.2 (order_hom_class.mono f h : f a ≤ f b)] }, { rw [inf_eq_right.2 h, inf_eq_right.2 (order_hom_class.mono f h : f b ≤ f a)] } end, .. ‹order_hom_class F α β› } /-- Reinterpret an order homomorphism to a linear order as a `lattice_hom`. -/ def to_lattice_hom (f : F) : lattice_hom α β := by { haveI : lattice_hom_class F α β := order_hom_class.to_lattice_hom_class α β, exact f } @[simp] lemma coe_to_lattice_hom (f : F) : ⇑(to_lattice_hom α β f) = f := rfl @[simp] lemma to_lattice_hom_apply (f : F) (a : α) : to_lattice_hom α β f a = f a := rfl end order_hom_class /-! ### Bounded lattice homomorphisms -/ namespace bounded_lattice_hom variables [lattice α] [lattice β] [lattice γ] [lattice δ] [bounded_order α] [bounded_order β] [bounded_order γ] [bounded_order δ] /-- Reinterpret a `bounded_lattice_hom` as a `sup_bot_hom`. -/ def to_sup_bot_hom (f : bounded_lattice_hom α β) : sup_bot_hom α β := { ..f } /-- Reinterpret a `bounded_lattice_hom` as an `inf_top_hom`. -/ def to_inf_top_hom (f : bounded_lattice_hom α β) : inf_top_hom α β := { ..f } /-- Reinterpret a `bounded_lattice_hom` as a `bounded_order_hom`. -/ def to_bounded_order_hom (f : bounded_lattice_hom α β) : bounded_order_hom α β := { ..f, ..(f.to_lattice_hom : α →o β) } instance : bounded_lattice_hom_class (bounded_lattice_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := f; obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := g; congr', map_sup := λ f, f.map_sup', map_inf := λ f, f.map_inf', map_top := λ f, f.map_top', map_bot := λ f, f.map_bot' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (bounded_lattice_hom α β) (λ _, α → β) := ⟨λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : bounded_lattice_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : bounded_lattice_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `bounded_lattice_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : bounded_lattice_hom α β) (f' : α → β) (h : f' = f) : bounded_lattice_hom α β := { .. f.to_lattice_hom.copy f' h, .. f.to_bounded_order_hom.copy f' h } @[simp] lemma coe_copy (f : bounded_lattice_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : bounded_lattice_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `bounded_lattice_hom`. -/ protected def id : bounded_lattice_hom α α := { ..lattice_hom.id α, ..bounded_order_hom.id α } instance : inhabited (bounded_lattice_hom α α) := ⟨bounded_lattice_hom.id α⟩ @[simp] lemma coe_id : ⇑(bounded_lattice_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : bounded_lattice_hom.id α a = a := rfl /-- Composition of `bounded_lattice_hom`s as a `bounded_lattice_hom`. -/ def comp (f : bounded_lattice_hom β γ) (g : bounded_lattice_hom α β) : bounded_lattice_hom α γ := { ..f.to_lattice_hom.comp g.to_lattice_hom, ..f.to_bounded_order_hom.comp g.to_bounded_order_hom } @[simp] lemma coe_comp (f : bounded_lattice_hom β γ) (g : bounded_lattice_hom α β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] lemma comp_apply (f : bounded_lattice_hom β γ) (g : bounded_lattice_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma coe_comp_lattice_hom (f : bounded_lattice_hom β γ) (g : bounded_lattice_hom α β) : (f.comp g : lattice_hom α γ) = (f : lattice_hom β γ).comp g := rfl @[simp] lemma coe_comp_sup_hom (f : bounded_lattice_hom β γ) (g : bounded_lattice_hom α β) : (f.comp g : sup_hom α γ) = (f : sup_hom β γ).comp g := rfl @[simp] lemma coe_comp_inf_hom (f : bounded_lattice_hom β γ) (g : bounded_lattice_hom α β) : (f.comp g : inf_hom α γ) = (f : inf_hom β γ).comp g := rfl @[simp] lemma comp_assoc (f : bounded_lattice_hom γ δ) (g : bounded_lattice_hom β γ) (h : bounded_lattice_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : bounded_lattice_hom α β) : f.comp (bounded_lattice_hom.id α) = f := bounded_lattice_hom.ext $ λ a, rfl @[simp] lemma id_comp (f : bounded_lattice_hom α β) : (bounded_lattice_hom.id β).comp f = f := bounded_lattice_hom.ext $ λ a, rfl lemma cancel_right {g₁ g₂ : bounded_lattice_hom β γ} {f : bounded_lattice_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, bounded_lattice_hom.ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : bounded_lattice_hom β γ} {f₁ f₂ : bounded_lattice_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end bounded_lattice_hom /-! ### Dual homs -/ namespace sup_hom variables [has_sup α] [has_sup β] [has_sup γ] /-- Reinterpret a supremum homomorphism as an infimum homomorphism between the dual lattices. -/ @[simps] protected def dual : sup_hom α β ≃ inf_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f, f.map_sup'⟩, inv_fun := λ f, ⟨f, f.map_inf'⟩, left_inv := λ f, sup_hom.ext $ λ _, rfl, right_inv := λ f, inf_hom.ext $ λ _, rfl } @[simp] lemma dual_id : (sup_hom.id α).dual = inf_hom.id _ := rfl @[simp] lemma dual_comp (g : sup_hom β γ) (f : sup_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : sup_hom.dual.symm (inf_hom.id _) = sup_hom.id α := rfl @[simp] lemma symm_dual_comp (g : inf_hom βᵒᵈ γᵒᵈ) (f : inf_hom αᵒᵈ βᵒᵈ) : sup_hom.dual.symm (g.comp f) = (sup_hom.dual.symm g).comp (sup_hom.dual.symm f) := rfl end sup_hom namespace inf_hom variables [has_inf α] [has_inf β] [has_inf γ] /-- Reinterpret an infimum homomorphism as a supremum homomorphism between the dual lattices. -/ @[simps] protected def dual : inf_hom α β ≃ sup_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f, f.map_inf'⟩, inv_fun := λ f, ⟨f, f.map_sup'⟩, left_inv := λ f, inf_hom.ext $ λ _, rfl, right_inv := λ f, sup_hom.ext $ λ _, rfl } @[simp] lemma dual_id : (inf_hom.id α).dual = sup_hom.id _ := rfl @[simp] lemma dual_comp (g : inf_hom β γ) (f : inf_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : inf_hom.dual.symm (sup_hom.id _) = inf_hom.id α := rfl @[simp] lemma symm_dual_comp (g : sup_hom βᵒᵈ γᵒᵈ) (f : sup_hom αᵒᵈ βᵒᵈ) : inf_hom.dual.symm (g.comp f) = (inf_hom.dual.symm g).comp (inf_hom.dual.symm f) := rfl end inf_hom namespace sup_bot_hom variables [has_sup α] [has_bot α] [has_sup β] [has_bot β] [has_sup γ] [has_bot γ] /-- Reinterpret a finitary supremum homomorphism as a finitary infimum homomorphism between the dual lattices. -/ def dual : sup_bot_hom α β ≃ inf_top_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f.to_sup_hom.dual, f.map_bot'⟩, inv_fun := λ f, ⟨sup_hom.dual.symm f.to_inf_hom, f.map_top'⟩, left_inv := λ f, sup_bot_hom.ext $ λ _, rfl, right_inv := λ f, inf_top_hom.ext $ λ _, rfl } @[simp] lemma dual_id : (sup_bot_hom.id α).dual = inf_top_hom.id _ := rfl @[simp] lemma dual_comp (g : sup_bot_hom β γ) (f : sup_bot_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : sup_bot_hom.dual.symm (inf_top_hom.id _) = sup_bot_hom.id α := rfl @[simp] lemma symm_dual_comp (g : inf_top_hom βᵒᵈ γᵒᵈ) (f : inf_top_hom αᵒᵈ βᵒᵈ) : sup_bot_hom.dual.symm (g.comp f) = (sup_bot_hom.dual.symm g).comp (sup_bot_hom.dual.symm f) := rfl end sup_bot_hom namespace inf_top_hom variables [has_inf α] [has_top α] [has_inf β] [has_top β] [has_inf γ] [has_top γ] /-- Reinterpret a finitary infimum homomorphism as a finitary supremum homomorphism between the dual lattices. -/ @[simps] protected def dual : inf_top_hom α β ≃ sup_bot_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f.to_inf_hom.dual, f.map_top'⟩, inv_fun := λ f, ⟨inf_hom.dual.symm f.to_sup_hom, f.map_bot'⟩, left_inv := λ f, inf_top_hom.ext $ λ _, rfl, right_inv := λ f, sup_bot_hom.ext $ λ _, rfl } @[simp] lemma dual_id : (inf_top_hom.id α).dual = sup_bot_hom.id _ := rfl @[simp] lemma dual_comp (g : inf_top_hom β γ) (f : inf_top_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : inf_top_hom.dual.symm (sup_bot_hom.id _) = inf_top_hom.id α := rfl @[simp] lemma symm_dual_comp (g : sup_bot_hom βᵒᵈ γᵒᵈ) (f : sup_bot_hom αᵒᵈ βᵒᵈ) : inf_top_hom.dual.symm (g.comp f) = (inf_top_hom.dual.symm g).comp (inf_top_hom.dual.symm f) := rfl end inf_top_hom namespace lattice_hom variables [lattice α] [lattice β] [lattice γ] /-- Reinterpret a lattice homomorphism as a lattice homomorphism between the dual lattices. -/ @[simps] protected def dual : lattice_hom α β ≃ lattice_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f.to_inf_hom.dual, f.map_sup'⟩, inv_fun := λ f, ⟨f.to_inf_hom.dual, f.map_sup'⟩, left_inv := λ f, ext $ λ a, rfl, right_inv := λ f, ext $ λ a, rfl } @[simp] lemma dual_id : (lattice_hom.id α).dual = lattice_hom.id _ := rfl @[simp] lemma dual_comp (g : lattice_hom β γ) (f : lattice_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : lattice_hom.dual.symm (lattice_hom.id _) = lattice_hom.id α := rfl @[simp] lemma symm_dual_comp (g : lattice_hom βᵒᵈ γᵒᵈ) (f : lattice_hom αᵒᵈ βᵒᵈ) : lattice_hom.dual.symm (g.comp f) = (lattice_hom.dual.symm g).comp (lattice_hom.dual.symm f) := rfl end lattice_hom namespace bounded_lattice_hom variables [lattice α] [bounded_order α] [lattice β] [bounded_order β] [lattice γ] [bounded_order γ] /-- Reinterpret a bounded lattice homomorphism as a bounded lattice homomorphism between the dual bounded lattices. -/ @[simps] protected def dual : bounded_lattice_hom α β ≃ bounded_lattice_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f.to_lattice_hom.dual, f.map_bot', f.map_top'⟩, inv_fun := λ f, ⟨lattice_hom.dual.symm f.to_lattice_hom, f.map_bot', f.map_top'⟩, left_inv := λ f, ext $ λ a, rfl, right_inv := λ f, ext $ λ a, rfl } @[simp] lemma dual_id : (bounded_lattice_hom.id α).dual = bounded_lattice_hom.id _ := rfl @[simp] lemma dual_comp (g : bounded_lattice_hom β γ) (f : bounded_lattice_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : bounded_lattice_hom.dual.symm (bounded_lattice_hom.id _) = bounded_lattice_hom.id α := rfl @[simp] lemma symm_dual_comp (g : bounded_lattice_hom βᵒᵈ γᵒᵈ) (f : bounded_lattice_hom αᵒᵈ βᵒᵈ) : bounded_lattice_hom.dual.symm (g.comp f) = (bounded_lattice_hom.dual.symm g).comp (bounded_lattice_hom.dual.symm f) := rfl end bounded_lattice_hom /-! ### `with_top`, `with_bot` -/ namespace sup_hom variables [semilattice_sup α] [semilattice_sup β] [semilattice_sup γ] /-- Adjoins a `⊤` to the domain and codomain of a `sup_hom`. -/ @[simps] protected def with_top (f : sup_hom α β) : sup_hom (with_top α) (with_top β) := { to_fun := option.map f, map_sup' := λ a b, match a, b with | ⊤, ⊤ := rfl | ⊤, (b : α) := rfl | (a : α), ⊤ := rfl | (a : α), (b : α) := congr_arg _ (f.map_sup' _ _) end } @[simp] lemma with_top_id : (sup_hom.id α).with_top = sup_hom.id _ := fun_like.coe_injective option.map_id @[simp] lemma with_top_comp (f : sup_hom β γ) (g : sup_hom α β) : (f.comp g).with_top = f.with_top.comp g.with_top := fun_like.coe_injective (option.map_comp_map _ _).symm /-- Adjoins a `⊥` to the domain and codomain of a `sup_hom`. -/ @[simps] protected def with_bot (f : sup_hom α β) : sup_bot_hom (with_bot α) (with_bot β) := { to_fun := option.map f, map_sup' := λ a b, match a, b with | ⊥, ⊥ := rfl | ⊥, (b : α) := rfl | (a : α), ⊥ := rfl | (a : α), (b : α) := congr_arg _ (f.map_sup' _ _) end, map_bot' := rfl } @[simp] lemma with_bot_id : (sup_hom.id α).with_bot = sup_bot_hom.id _ := fun_like.coe_injective option.map_id @[simp] lemma with_bot_comp (f : sup_hom β γ) (g : sup_hom α β) : (f.comp g).with_bot = f.with_bot.comp g.with_bot := fun_like.coe_injective (option.map_comp_map _ _).symm /-- Adjoins a `⊤` to the codomain of a `sup_hom`. -/ @[simps] def with_top' [order_top β] (f : sup_hom α β) : sup_hom (with_top α) β := { to_fun := λ a, a.elim ⊤ f, map_sup' := λ a b, match a, b with | ⊤, ⊤ := top_sup_eq.symm | ⊤, (b : α) := top_sup_eq.symm | (a : α), ⊤ := sup_top_eq.symm | (a : α), (b : α) := f.map_sup' _ _ end } /-- Adjoins a `⊥` to the domain of a `sup_hom`. -/ @[simps] def with_bot' [order_bot β] (f : sup_hom α β) : sup_bot_hom (with_bot α) β := { to_fun := λ a, a.elim ⊥ f, map_sup' := λ a b, match a, b with | ⊥, ⊥ := bot_sup_eq.symm | ⊥, (b : α) := bot_sup_eq.symm | (a : α), ⊥ := sup_bot_eq.symm | (a : α), (b : α) := f.map_sup' _ _ end, map_bot' := rfl } end sup_hom namespace inf_hom variables [semilattice_inf α] [semilattice_inf β] [semilattice_inf γ] /-- Adjoins a `⊤` to the domain and codomain of an `inf_hom`. -/ @[simps] protected def with_top (f : inf_hom α β) : inf_top_hom (with_top α) (with_top β) := { to_fun := option.map f, map_inf' := λ a b, match a, b with | ⊤, ⊤ := rfl | ⊤, (b : α) := rfl | (a : α), ⊤ := rfl | (a : α), (b : α) := congr_arg _ (f.map_inf' _ _) end, map_top' := rfl } @[simp] lemma with_top_id : (inf_hom.id α).with_top = inf_top_hom.id _ := fun_like.coe_injective option.map_id @[simp] lemma with_top_comp (f : inf_hom β γ) (g : inf_hom α β) : (f.comp g).with_top = f.with_top.comp g.with_top := fun_like.coe_injective (option.map_comp_map _ _).symm /-- Adjoins a `⊥ to the domain and codomain of an `inf_hom`. -/ @[simps] protected def with_bot (f : inf_hom α β) : inf_hom (with_bot α) (with_bot β) := { to_fun := option.map f, map_inf' := λ a b, match a, b with | ⊥, ⊥ := rfl | ⊥, (b : α) := rfl | (a : α), ⊥ := rfl | (a : α), (b : α) := congr_arg _ (f.map_inf' _ _) end } @[simp] lemma with_bot_id : (inf_hom.id α).with_bot = inf_hom.id _ := fun_like.coe_injective option.map_id @[simp] lemma with_bot_comp (f : inf_hom β γ) (g : inf_hom α β) : (f.comp g).with_bot = f.with_bot.comp g.with_bot := fun_like.coe_injective (option.map_comp_map _ _).symm /-- Adjoins a `⊤` to the codomain of an `inf_hom`. -/ @[simps] def with_top' [order_top β] (f : inf_hom α β) : inf_top_hom (with_top α) β := { to_fun := λ a, a.elim ⊤ f, map_inf' := λ a b, match a, b with | ⊤, ⊤ := top_inf_eq.symm | ⊤, (b : α) := top_inf_eq.symm | (a : α), ⊤ := inf_top_eq.symm | (a : α), (b : α) := f.map_inf' _ _ end, map_top' := rfl } /-- Adjoins a `⊥` to the codomain of an `inf_hom`. -/ @[simps] def with_bot' [order_bot β] (f : inf_hom α β) : inf_hom (with_bot α) β := { to_fun := λ a, a.elim ⊥ f, map_inf' := λ a b, match a, b with | ⊥, ⊥ := bot_inf_eq.symm | ⊥, (b : α) := bot_inf_eq.symm | (a : α), ⊥ := inf_bot_eq.symm | (a : α), (b : α) := f.map_inf' _ _ end } end inf_hom namespace lattice_hom variables [lattice α] [lattice β] [lattice γ] /-- Adjoins a `⊤` to the domain and codomain of a `lattice_hom`. -/ @[simps] protected def with_top (f : lattice_hom α β) : lattice_hom (with_top α) (with_top β) := { to_sup_hom := f.to_sup_hom.with_top, ..f.to_inf_hom.with_top } @[simp] lemma with_top_id : (lattice_hom.id α).with_top = lattice_hom.id _ := fun_like.coe_injective option.map_id @[simp] lemma with_top_comp (f : lattice_hom β γ) (g : lattice_hom α β) : (f.comp g).with_top = f.with_top.comp g.with_top := fun_like.coe_injective (option.map_comp_map _ _).symm /-- Adjoins a `⊥` to the domain and codomain of a `lattice_hom`. -/ @[simps] protected def with_bot (f : lattice_hom α β) : lattice_hom (with_bot α) (with_bot β) := { to_sup_hom := f.to_sup_hom.with_bot, ..f.to_inf_hom.with_bot } @[simp] lemma with_bot_id : (lattice_hom.id α).with_bot = lattice_hom.id _ := fun_like.coe_injective option.map_id @[simp] lemma with_bot_comp (f : lattice_hom β γ) (g : lattice_hom α β) : (f.comp g).with_bot = f.with_bot.comp g.with_bot := fun_like.coe_injective (option.map_comp_map _ _).symm /-- Adjoins a `⊤` and `⊥` to the domain and codomain of a `lattice_hom`. -/ @[simps] def with_top_with_bot (f : lattice_hom α β) : bounded_lattice_hom (with_top $ with_bot α) (with_top $ with_bot β) := ⟨f.with_bot.with_top, rfl, rfl⟩ @[simp] lemma with_top_with_bot_id : (lattice_hom.id α).with_top_with_bot = bounded_lattice_hom.id _ := fun_like.coe_injective $ begin refine (congr_arg option.map _).trans option.map_id, rw with_bot_id, refl, end @[simp] lemma with_top_with_bot_comp (f : lattice_hom β γ) (g : lattice_hom α β) : (f.comp g).with_top_with_bot = f.with_top_with_bot.comp g.with_top_with_bot := fun_like.coe_injective $ (congr_arg option.map $ (option.map_comp_map _ _).symm).trans (option.map_comp_map _ _).symm /-- Adjoins a `⊥` to the codomain of a `lattice_hom`. -/ @[simps] def with_top' [order_top β] (f : lattice_hom α β) : lattice_hom (with_top α) β := { ..f.to_sup_hom.with_top', ..f.to_inf_hom.with_top' } /-- Adjoins a `⊥` to the domain and codomain of a `lattice_hom`. -/ @[simps] def with_bot' [order_bot β] (f : lattice_hom α β) : lattice_hom (with_bot α) β := { ..f.to_sup_hom.with_bot', ..f.to_inf_hom.with_bot' } /-- Adjoins a `⊤` and `⊥` to the codomain of a `lattice_hom`. -/ @[simps] def with_top_with_bot' [bounded_order β] (f : lattice_hom α β) : bounded_lattice_hom (with_top $ with_bot α) β := { to_lattice_hom := f.with_bot'.with_top', map_top' := rfl, map_bot' := rfl } end lattice_hom
cb2a9654e0431e4ca127f3fba707a092a458d887
4fa161becb8ce7378a709f5992a594764699e268
/src/group_theory/group_action.lean
fcd21cc5d3d58b2c6fe0883dc8b05959440df7da
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
10,609
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.set.finite import group_theory.coset universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open_locale big_operators /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/ class has_scalar (α : Type u) (γ : Type v) := (smul : α → γ → γ) infixr ` • `:73 := has_scalar.smul section prio set_option default_priority 100 -- see Note [default priority] /-- Typeclass for multiplicative actions by monoids. This generalizes group actions. -/ @[protect_proj] class mul_action (α : Type u) (β : Type v) [monoid α] extends has_scalar α β := (one_smul : ∀ b : β, (1 : α) • b = b) (mul_smul : ∀ (x y : α) (b : β), (x * y) • b = x • y • b) end prio section variables [monoid α] [mul_action α β] theorem mul_smul (a₁ a₂ : α) (b : β) : (a₁ * a₂) • b = a₁ • a₂ • b := mul_action.mul_smul _ _ _ lemma smul_smul (a₁ a₂ : α) (b : β) : a₁ • a₂ • b = (a₁ * a₂) • b := (mul_smul _ _ _).symm lemma smul_comm {α : Type u} {β : Type v} [comm_monoid α] [mul_action α β] (a₁ a₂ : α) (b : β) : a₁ • a₂ • b = a₂ • a₁ • b := by rw [←mul_smul, ←mul_smul, mul_comm] variable (α) @[simp] theorem one_smul (b : β) : (1 : α) • b = b := mul_action.one_smul _ variables {α} @[simp] lemma units.inv_smul_smul (u : units α) (x : β) : (↑u⁻¹:α) • (u:α) • x = x := by rw [smul_smul, u.inv_mul, one_smul] @[simp] lemma units.smul_inv_smul (u : units α) (x : β) : (u:α) • (↑u⁻¹:α) • x = x := by rw [smul_smul, u.mul_inv, one_smul] section gwz variables {G : Type*} [group_with_zero G] [mul_action G β] lemma inv_smul_smul' {c : G} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := (units.mk0 c hc).inv_smul_smul x lemma smul_inv_smul' {c : G} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := (units.mk0 c hc).smul_inv_smul x end gwz variables (p : Prop) [decidable p] lemma ite_smul (a₁ a₂ : α) (b : β) : (ite p a₁ a₂) • b = ite p (a₁ • b) (a₂ • b) := by split_ifs; refl lemma smul_ite (a : α) (b₁ b₂ : β) : a • (ite p b₁ b₂) = ite p (a • b₁) (a • b₂) := by split_ifs; refl end namespace mul_action variables (α) [monoid α] /-- The regular action of a monoid on itself by left multiplication. -/ def regular : mul_action α α := { smul := λ a₁ a₂, a₁ * a₂, one_smul := λ a, one_mul a, mul_smul := λ a₁ a₂ a₃, mul_assoc _ _ _, } variables [mul_action α β] /-- The orbit of an element under an action. -/ def orbit (b : β) := set.range (λ x : α, x • b) variable {α} lemma mem_orbit_iff {b₁ b₂ : β} : b₂ ∈ orbit α b₁ ↔ ∃ x : α, x • b₁ = b₂ := iff.rfl @[simp] lemma mem_orbit (b : β) (x : α) : x • b ∈ orbit α b := ⟨x, rfl⟩ @[simp] lemma mem_orbit_self (b : β) : b ∈ orbit α b := ⟨1, by simp [mul_action.one_smul]⟩ instance orbit_fintype (b : β) [fintype α] [decidable_eq β] : fintype (orbit α b) := set.fintype_range _ variable (α) /-- The stabilizer of an element under an action, i.e. what sends the element to itself. -/ def stabilizer (b : β) : set α := {x : α | x • b = b} variable {α} @[simp] lemma mem_stabilizer_iff {b : β} {x : α} : x ∈ stabilizer α b ↔ x • b = b := iff.rfl variables (α) (β) /-- The set of elements fixed under the whole action. -/ def fixed_points : set β := {b : β | ∀ x, x ∈ stabilizer α b} variables {α} (β) @[simp] lemma mem_fixed_points {b : β} : b ∈ fixed_points α β ↔ ∀ x : α, x • b = b := iff.rfl lemma mem_fixed_points' {b : β} : b ∈ fixed_points α β ↔ (∀ b', b' ∈ orbit α b → b' = b) := ⟨λ h b h₁, let ⟨x, hx⟩ := mem_orbit_iff.1 h₁ in hx ▸ h x, λ h b, mem_stabilizer_iff.2 (h _ (mem_orbit _ _))⟩ /-- An action of `α` on `β` and a monoid homomorphism `γ → α` induce an action of `γ` on `β`. -/ def comp_hom [monoid γ] (g : γ → α) [is_monoid_hom g] : mul_action γ β := { smul := λ x b, (g x) • b, one_smul := by simp [is_monoid_hom.map_one g, mul_action.one_smul], mul_smul := by simp [is_monoid_hom.map_mul g, mul_action.mul_smul] } instance (b : β) : is_submonoid (stabilizer α b) := { one_mem := one_smul _ b, mul_mem := λ a a' (ha : a • b = b) (hb : a' • b = b), by rw [mem_stabilizer_iff, ←smul_smul, hb, ha] } end mul_action namespace mul_action variables [group α] [mul_action α β] section open mul_action quotient_group @[simp] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := (to_units α c).inv_smul_smul x @[simp] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := (to_units α c).smul_inv_smul x variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ def to_perm (g : α) : equiv.perm β := { to_fun := (•) g, inv_fun := (•) g⁻¹, left_inv := inv_smul_smul g, right_inv := smul_inv_smul g } variables {α} {β} instance : is_group_hom (to_perm α β) := { map_mul := λ x y, equiv.ext (λ a, mul_action.mul_smul x y a) } lemma bijective (g : α) : function.bijective (λ b : β, g • b) := (to_perm α β g).bijective lemma orbit_eq_iff {a b : β} : orbit α a = orbit α b ↔ a ∈ orbit α b:= ⟨λ h, h ▸ mem_orbit_self _, λ ⟨x, (hx : x • b = a)⟩, set.ext (λ c, ⟨λ ⟨y, (hy : y • a = c)⟩, ⟨y * x, show (y * x) • b = c, by rwa [mul_action.mul_smul, hx]⟩, λ ⟨y, (hy : y • b = c)⟩, ⟨y * x⁻¹, show (y * x⁻¹) • a = c, by conv {to_rhs, rw [← hy, ← mul_one y, ← inv_mul_self x, ← mul_assoc, mul_action.mul_smul, hx]}⟩⟩)⟩ instance (b : β) : is_subgroup (stabilizer α b) := { one_mem := mul_action.one_smul _, mul_mem := λ x y (hx : x • b = b) (hy : y • b = b), show (x * y) • b = b, by rw mul_action.mul_smul; simp *, inv_mem := λ x (hx : x • b = b), show x⁻¹ • b = b, by rw [← hx, ← mul_action.mul_smul, inv_mul_self, mul_action.one_smul, hx] } @[simp] lemma mem_orbit_smul (g : α) (a : β) : a ∈ orbit α (g • a) := ⟨g⁻¹, by simp⟩ @[simp] lemma smul_mem_orbit_smul (g h : α) (a : β) : g • a ∈ orbit α (h • a) := ⟨g * h⁻¹, by simp [mul_smul]⟩ variables (α) (β) /-- The relation "in the same orbit". -/ def orbit_rel : setoid β := { r := λ a b, a ∈ orbit α b, iseqv := ⟨mem_orbit_self, λ a b, by simp [orbit_eq_iff.symm, eq_comm], λ a b, by simp [orbit_eq_iff.symm, eq_comm] {contextual := tt}⟩ } variables {β} open quotient_group /-- Orbit-stabilizer theorem. -/ noncomputable def orbit_equiv_quotient_stabilizer (b : β) : orbit α b ≃ quotient (stabilizer α b) := equiv.symm (equiv.of_bijective (λ x : quotient (stabilizer α b), quotient.lift_on' x (λ x, (⟨x • b, mem_orbit _ _⟩ : orbit α b)) (λ g h (H : _ = _), subtype.eq $ (mul_action.bijective (g⁻¹)).1 $ show g⁻¹ • (g • b) = g⁻¹ • (h • b), by rw [← mul_action.mul_smul, ← mul_action.mul_smul, H, inv_mul_self, mul_action.one_smul])) ⟨λ g h, quotient.induction_on₂' g h (λ g h H, quotient.sound' $ have H : g • b = h • b := subtype.mk.inj H, show (g⁻¹ * h) • b = b, by rw [mul_action.mul_smul, ← H, ← mul_action.mul_smul, inv_mul_self, mul_action.one_smul]), λ ⟨b, ⟨g, hgb⟩⟩, ⟨g, subtype.eq hgb⟩⟩) @[simp] theorem orbit_equiv_quotient_stabilizer_symm_apply (b : β) (a : α) : ((orbit_equiv_quotient_stabilizer α b).symm a : β) = a • b := rfl end open quotient_group mul_action is_subgroup /-- Action on left cosets. -/ def mul_left_cosets (H : set α) [is_subgroup H] (x : α) (y : quotient H) : quotient H := quotient.lift_on' y (λ y, quotient_group.mk ((x : α) * y)) (λ a b (hab : _ ∈ H), quotient_group.eq.2 (by rwa [mul_inv_rev, ← mul_assoc, mul_assoc (a⁻¹), inv_mul_self, mul_one])) instance (H : set α) [is_subgroup H] : mul_action α (quotient H) := { smul := mul_left_cosets H, one_smul := λ a, quotient.induction_on' a (λ a, quotient_group.eq.2 (by simp [is_submonoid.one_mem])), mul_smul := λ x y a, quotient.induction_on' a (λ a, quotient_group.eq.2 (by simp [mul_inv_rev, is_submonoid.one_mem, mul_assoc])) } instance mul_left_cosets_comp_subtype_val (H I : set α) [is_subgroup H] [is_subgroup I] : mul_action I (quotient H) := mul_action.comp_hom (quotient H) (subtype.val : I → α) end mul_action section prio set_option default_priority 100 -- see Note [default priority] /-- Typeclass for multiplicative actions on additive structures. This generalizes group modules. -/ class distrib_mul_action (α : Type u) (β : Type v) [monoid α] [add_monoid β] extends mul_action α β := (smul_add : ∀(r : α) (x y : β), r • (x + y) = r • x + r • y) (smul_zero : ∀(r : α), r • (0 : β) = 0) end prio section variables [monoid α] [add_monoid β] [distrib_mul_action α β] theorem smul_add (a : α) (b₁ b₂ : β) : a • (b₁ + b₂) = a • b₁ + a • b₂ := distrib_mul_action.smul_add _ _ _ @[simp] theorem smul_zero (a : α) : a • (0 : β) = 0 := distrib_mul_action.smul_zero _ theorem units.smul_eq_zero (u : units α) {x : β} : (u : α) • x = 0 ↔ x = 0 := ⟨λ h, by rw [← u.inv_smul_smul x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem units.smul_ne_zero (u : units α) {x : β} : (u : α) • x ≠ 0 ↔ x ≠ 0 := not_congr u.smul_eq_zero @[simp] theorem is_unit.smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ u.smul_eq_zero variable (β) /-- Scalar multiplication by `r` as an `add_monoid_hom`. -/ def const_smul_hom (r : α) : β →+ β := { to_fun := (•) r, map_zero' := smul_zero r, map_add' := smul_add r } variable {β} @[simp] lemma const_smul_hom_apply (r : α) (x : β) : const_smul_hom β r x = r • x := rfl lemma list.smul_sum {r : α} {l : list β} : r • l.sum = (l.map ((•) r)).sum := (const_smul_hom β r).map_list_sum l end section variables [monoid α] [add_comm_monoid β] [distrib_mul_action α β] lemma multiset.smul_sum {r : α} {s : multiset β} : r • s.sum = (s.map ((•) r)).sum := (const_smul_hom β r).map_multiset_sum s lemma finset.smul_sum {r : α} {f : γ → β} {s : finset γ} : r • ∑ x in s, f x = ∑ x in s, r • f x := (const_smul_hom β r).map_sum f s end
cb7f1c2558452558de5d79ed737339501b6fac5c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/affine_space/combination.lean
d0b7262a004bb993a97d4a5016196f2930d66ded
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
48,219
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import algebra.invertible import algebra.indicator_function import algebra.module.big_operators import linear_algebra.affine_space.affine_map import linear_algebra.affine_space.affine_subspace import linear_algebra.finsupp import tactic.fin_cases /-! # Affine combinations of points This file defines affine combinations of points. ## Main definitions * `weighted_vsub_of_point` is a general weighted combination of subtractions with an explicit base point, yielding a vector. * `weighted_vsub` uses an arbitrary choice of base point and is intended to be used when the sum of weights is 0, in which case the result is independent of the choice of base point. * `affine_combination` adds the weighted combination to the arbitrary base point, yielding a point rather than a vector, and is intended to be used when the sum of weights is 1, in which case the result is independent of the choice of base point. These definitions are for sums over a `finset`; versions for a `fintype` may be obtained using `finset.univ`, while versions for a `finsupp` may be obtained using `finsupp.support`. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable theory open_locale big_operators affine namespace finset lemma univ_fin2 : (univ : finset (fin 2)) = {0, 1} := by { ext x, fin_cases x; simp } variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] variables [S : affine_space V P] include S variables {ι : Type*} (s : finset ι) variables {ι₂ : Type*} (s₂ : finset ι₂) /-- A weighted sum of the results of subtracting a base point from the given points, as a linear map on the weights. The main cases of interest are where the sum of the weights is 0, in which case the sum is independent of the choice of base point, and where the sum of the weights is 1, in which case the sum added to the base point is independent of the choice of base point. -/ def weighted_vsub_of_point (p : ι → P) (b : P) : (ι → k) →ₗ[k] V := ∑ i in s, (linear_map.proj i : (ι → k) →ₗ[k] k).smul_right (p i -ᵥ b) @[simp] lemma weighted_vsub_of_point_apply (w : ι → k) (p : ι → P) (b : P) : s.weighted_vsub_of_point p b w = ∑ i in s, w i • (p i -ᵥ b) := by simp [weighted_vsub_of_point, linear_map.sum_apply] /-- The value of `weighted_vsub_of_point`, where the given points are equal. -/ @[simp] lemma weighted_vsub_of_point_apply_const (w : ι → k) (p : P) (b : P) : s.weighted_vsub_of_point (λ _, p) b w = (∑ i in s, w i) • (p -ᵥ b) := by rw [weighted_vsub_of_point_apply, sum_smul] /-- Given a family of points, if we use a member of the family as a base point, the `weighted_vsub_of_point` does not depend on the value of the weights at this point. -/ lemma weighted_vsub_of_point_eq_of_weights_eq (p : ι → P) (j : ι) (w₁ w₂ : ι → k) (hw : ∀ i, i ≠ j → w₁ i = w₂ i) : s.weighted_vsub_of_point p (p j) w₁ = s.weighted_vsub_of_point p (p j) w₂ := begin simp only [finset.weighted_vsub_of_point_apply], congr, ext i, cases eq_or_ne i j with h h, { simp [h], }, { simp [hw i h], }, end /-- The weighted sum is independent of the base point when the sum of the weights is 0. -/ lemma weighted_vsub_of_point_eq_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w = s.weighted_vsub_of_point p b₂ w := begin apply eq_of_sub_eq_zero, rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←sum_sub_distrib], conv_lhs { congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, zero_smul] end /-- The weighted sum, added to the base point, is independent of the base point when the sum of the weights is 1. -/ lemma weighted_vsub_of_point_vadd_eq_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w +ᵥ b₁ = s.weighted_vsub_of_point p b₂ w +ᵥ b₂ := begin erw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, ←add_sub_assoc, add_comm, add_sub_assoc, ←sum_sub_distrib], conv_lhs { congr, skip, congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, one_smul, vsub_add_vsub_cancel, vsub_self] end /-- The weighted sum is unaffected by removing the base point, if present, from the set of points. -/ @[simp] lemma weighted_vsub_of_point_erase [decidable_eq ι] (w : ι → k) (p : ι → P) (i : ι) : (s.erase i).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_erase, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by adding the base point, whether or not present, to the set of points. -/ @[simp] lemma weighted_vsub_of_point_insert [decidable_eq ι] (w : ι → k) (p : ι → P) (i : ι) : (insert i s).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_insert_zero, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_of_point_indicator_subset (w : ι → k) (p : ι → P) (b : P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point p b (set.indicator ↑s₁ w) := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], exact set.sum_indicator_subset_of_eq_zero w (λ i wi, wi • (p i -ᵥ b : V)) h (λ i, zero_smul k _) end /-- A weighted sum, over the image of an embedding, equals a weighted sum with the same points and weights over the original `finset`. -/ lemma weighted_vsub_of_point_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) (b : P) : (s₂.map e).weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point (p ∘ e) b (w ∘ e) := begin simp_rw [weighted_vsub_of_point_apply], exact finset.sum_map _ _ _ end /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `weighted_vsub_of_point` expressions. -/ lemma sum_smul_vsub_eq_weighted_vsub_of_point_sub (w : ι → k) (p₁ p₂ : ι → P) (b : P) : ∑ i in s, w i • (p₁ i -ᵥ p₂ i) = s.weighted_vsub_of_point p₁ b w - s.weighted_vsub_of_point p₂ b w := by simp_rw [weighted_vsub_of_point_apply, ←sum_sub_distrib, ←smul_sub, vsub_sub_vsub_cancel_right] /-- A weighted sum of pairwise subtractions, where the point on the right is constant, expressed as a subtraction involving a `weighted_vsub_of_point` expression. -/ lemma sum_smul_vsub_const_eq_weighted_vsub_of_point_sub (w : ι → k) (p₁ : ι → P) (p₂ b : P) : ∑ i in s, w i • (p₁ i -ᵥ p₂) = s.weighted_vsub_of_point p₁ b w - (∑ i in s, w i) • (p₂ -ᵥ b) := by rw [sum_smul_vsub_eq_weighted_vsub_of_point_sub, weighted_vsub_of_point_apply_const] /-- A weighted sum of pairwise subtractions, where the point on the left is constant, expressed as a subtraction involving a `weighted_vsub_of_point` expression. -/ lemma sum_smul_const_vsub_eq_sub_weighted_vsub_of_point (w : ι → k) (p₂ : ι → P) (p₁ b : P) : ∑ i in s, w i • (p₁ -ᵥ p₂ i) = (∑ i in s, w i) • (p₁ -ᵥ b) - s.weighted_vsub_of_point p₂ b w := by rw [sum_smul_vsub_eq_weighted_vsub_of_point_sub, weighted_vsub_of_point_apply_const] /-- A weighted sum may be split into such sums over two subsets. -/ lemma weighted_vsub_of_point_sdiff [decidable_eq ι] {s₂ : finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) (b : P) : (s \ s₂).weighted_vsub_of_point p b w + s₂.weighted_vsub_of_point p b w = s.weighted_vsub_of_point p b w := by simp_rw [weighted_vsub_of_point_apply, sum_sdiff h] /-- A weighted sum may be split into a subtraction of such sums over two subsets. -/ lemma weighted_vsub_of_point_sdiff_sub [decidable_eq ι] {s₂ : finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) (b : P) : (s \ s₂).weighted_vsub_of_point p b w - s₂.weighted_vsub_of_point p b (-w) = s.weighted_vsub_of_point p b w := by rw [map_neg, sub_neg_eq_add, s.weighted_vsub_of_point_sdiff h] /-- A weighted sum over `s.subtype pred` equals one over `s.filter pred`. -/ lemma weighted_vsub_of_point_subtype_eq_filter (w : ι → k) (p : ι → P) (b : P) (pred : ι → Prop) [decidable_pred pred] : (s.subtype pred).weighted_vsub_of_point (λ i, p i) b (λ i, w i) = (s.filter pred).weighted_vsub_of_point p b w := by rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←sum_subtype_eq_sum_filter] /-- A weighted sum over `s.filter pred` equals one over `s` if all the weights at indices in `s` not satisfying `pred` are zero. -/ lemma weighted_vsub_of_point_filter_of_ne (w : ι → k) (p : ι → P) (b : P) {pred : ι → Prop} [decidable_pred pred] (h : ∀ i ∈ s, w i ≠ 0 → pred i) : (s.filter pred).weighted_vsub_of_point p b w = s.weighted_vsub_of_point p b w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, sum_filter_of_ne], intros i hi hne, refine h i hi _, intro hw, simpa [hw] using hne, end /-- A weighted sum of the results of subtracting a default base point from the given points, as a linear map on the weights. This is intended to be used when the sum of the weights is 0; that condition is specified as a hypothesis on those lemmas that require it. -/ def weighted_vsub (p : ι → P) : (ι → k) →ₗ[k] V := s.weighted_vsub_of_point p (classical.choice S.nonempty) /-- Applying `weighted_vsub` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `weighted_vsub` would involve selecting a preferred base point with `weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero` and then using `weighted_vsub_of_point_apply`. -/ lemma weighted_vsub_apply (w : ι → k) (p : ι → P) : s.weighted_vsub p w = ∑ i in s, w i • (p i -ᵥ (classical.choice S.nonempty)) := by simp [weighted_vsub, linear_map.sum_apply] /-- `weighted_vsub` gives the sum of the results of subtracting any base point, when the sum of the weights is 0. -/ lemma weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b : P) : s.weighted_vsub p w = s.weighted_vsub_of_point p b w := s.weighted_vsub_of_point_eq_of_sum_eq_zero w p h _ _ /-- The value of `weighted_vsub`, where the given points are equal and the sum of the weights is 0. -/ @[simp] lemma weighted_vsub_apply_const (w : ι → k) (p : P) (h : ∑ i in s, w i = 0) : s.weighted_vsub (λ _, p) w = 0 := by rw [weighted_vsub, weighted_vsub_of_point_apply_const, h, zero_smul] /-- The `weighted_vsub` for an empty set is 0. -/ @[simp] lemma weighted_vsub_empty (w : ι → k) (p : ι → P) : (∅ : finset ι).weighted_vsub p w = (0:V) := by simp [weighted_vsub_apply] /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub p w = s₂.weighted_vsub p (set.indicator ↑s₁ w) := weighted_vsub_of_point_indicator_subset _ _ _ h /-- A weighted subtraction, over the image of an embedding, equals a weighted subtraction with the same points and weights over the original `finset`. -/ lemma weighted_vsub_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).weighted_vsub p w = s₂.weighted_vsub (p ∘ e) (w ∘ e) := s₂.weighted_vsub_of_point_map _ _ _ _ /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `weighted_vsub` expressions. -/ lemma sum_smul_vsub_eq_weighted_vsub_sub (w : ι → k) (p₁ p₂ : ι → P) : ∑ i in s, w i • (p₁ i -ᵥ p₂ i) = s.weighted_vsub p₁ w - s.weighted_vsub p₂ w := s.sum_smul_vsub_eq_weighted_vsub_of_point_sub _ _ _ _ /-- A weighted sum of pairwise subtractions, where the point on the right is constant and the sum of the weights is 0. -/ lemma sum_smul_vsub_const_eq_weighted_vsub (w : ι → k) (p₁ : ι → P) (p₂ : P) (h : ∑ i in s, w i = 0) : ∑ i in s, w i • (p₁ i -ᵥ p₂) = s.weighted_vsub p₁ w := by rw [sum_smul_vsub_eq_weighted_vsub_sub, s.weighted_vsub_apply_const _ _ h, sub_zero] /-- A weighted sum of pairwise subtractions, where the point on the left is constant and the sum of the weights is 0. -/ lemma sum_smul_const_vsub_eq_neg_weighted_vsub (w : ι → k) (p₂ : ι → P) (p₁ : P) (h : ∑ i in s, w i = 0) : ∑ i in s, w i • (p₁ -ᵥ p₂ i) = -s.weighted_vsub p₂ w := by rw [sum_smul_vsub_eq_weighted_vsub_sub, s.weighted_vsub_apply_const _ _ h, zero_sub] /-- A weighted sum may be split into such sums over two subsets. -/ lemma weighted_vsub_sdiff [decidable_eq ι] {s₂ : finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) : (s \ s₂).weighted_vsub p w + s₂.weighted_vsub p w = s.weighted_vsub p w := s.weighted_vsub_of_point_sdiff h _ _ _ /-- A weighted sum may be split into a subtraction of such sums over two subsets. -/ lemma weighted_vsub_sdiff_sub [decidable_eq ι] {s₂ : finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) : (s \ s₂).weighted_vsub p w - s₂.weighted_vsub p (-w) = s.weighted_vsub p w := s.weighted_vsub_of_point_sdiff_sub h _ _ _ /-- A weighted sum over `s.subtype pred` equals one over `s.filter pred`. -/ lemma weighted_vsub_subtype_eq_filter (w : ι → k) (p : ι → P) (pred : ι → Prop) [decidable_pred pred] : (s.subtype pred).weighted_vsub (λ i, p i) (λ i, w i) = (s.filter pred).weighted_vsub p w := s.weighted_vsub_of_point_subtype_eq_filter _ _ _ _ /-- A weighted sum over `s.filter pred` equals one over `s` if all the weights at indices in `s` not satisfying `pred` are zero. -/ lemma weighted_vsub_filter_of_ne (w : ι → k) (p : ι → P) {pred : ι → Prop} [decidable_pred pred] (h : ∀ i ∈ s, w i ≠ 0 → pred i) : (s.filter pred).weighted_vsub p w = s.weighted_vsub p w := s.weighted_vsub_of_point_filter_of_ne _ _ _ h /-- A weighted sum of the results of subtracting a default base point from the given points, added to that base point, as an affine map on the weights. This is intended to be used when the sum of the weights is 1, in which case it is an affine combination (barycenter) of the points with the given weights; that condition is specified as a hypothesis on those lemmas that require it. -/ def affine_combination (p : ι → P) : (ι → k) →ᵃ[k] P := { to_fun := λ w, s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty), linear := s.weighted_vsub p, map_vadd' := λ w₁ w₂, by simp_rw [vadd_vadd, weighted_vsub, vadd_eq_add, linear_map.map_add] } /-- The linear map corresponding to `affine_combination` is `weighted_vsub`. -/ @[simp] lemma affine_combination_linear (p : ι → P) : (s.affine_combination p : (ι → k) →ᵃ[k] P).linear = s.weighted_vsub p := rfl /-- Applying `affine_combination` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `affine_combination` would involve selecting a preferred base point with `affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one` and then using `weighted_vsub_of_point_apply`. -/ lemma affine_combination_apply (w : ι → k) (p : ι → P) : s.affine_combination p w = s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty) := rfl /-- The value of `affine_combination`, where the given points are equal. -/ @[simp] lemma affine_combination_apply_const (w : ι → k) (p : P) (h : ∑ i in s, w i = 1) : s.affine_combination (λ _, p) w = p := by rw [affine_combination_apply, s.weighted_vsub_of_point_apply_const, h, one_smul, vsub_vadd] /-- `affine_combination` gives the sum with any base point, when the sum of the weights is 1. -/ lemma affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b : P) : s.affine_combination p w = s.weighted_vsub_of_point p b w +ᵥ b := s.weighted_vsub_of_point_vadd_eq_of_sum_eq_one w p h _ _ /-- Adding a `weighted_vsub` to an `affine_combination`. -/ lemma weighted_vsub_vadd_affine_combination (w₁ w₂ : ι → k) (p : ι → P) : s.weighted_vsub p w₁ +ᵥ s.affine_combination p w₂ = s.affine_combination p (w₁ + w₂) := by rw [←vadd_eq_add, affine_map.map_vadd, affine_combination_linear] /-- Subtracting two `affine_combination`s. -/ lemma affine_combination_vsub (w₁ w₂ : ι → k) (p : ι → P) : s.affine_combination p w₁ -ᵥ s.affine_combination p w₂ = s.weighted_vsub p (w₁ - w₂) := by rw [←affine_map.linear_map_vsub, affine_combination_linear, vsub_eq_sub] lemma attach_affine_combination_of_injective [decidable_eq P] (s : finset P) (w : P → k) (f : s → P) (hf : function.injective f) : s.attach.affine_combination f (w ∘ f) = (image f univ).affine_combination id w := begin simp only [affine_combination, weighted_vsub_of_point_apply, id.def, vadd_right_cancel_iff, function.comp_app, affine_map.coe_mk], let g₁ : s → V := λ i, w (f i) • (f i -ᵥ classical.choice S.nonempty), let g₂ : P → V := λ i, w i • (i -ᵥ classical.choice S.nonempty), change univ.sum g₁ = (image f univ).sum g₂, have hgf : g₁ = g₂ ∘ f, { ext, simp, }, rw [hgf, sum_image], exact λ _ _ _ _ hxy, hf hxy, end lemma attach_affine_combination_coe (s : finset P) (w : P → k) : s.attach.affine_combination (coe : s → P) (w ∘ coe) = s.affine_combination id w := by classical; rw [attach_affine_combination_of_injective s w (coe : s → P) subtype.coe_injective, univ_eq_attach, attach_image_coe] omit S /-- Viewing a module as an affine space modelled on itself, a `weighted_vsub` is just a linear combination. -/ @[simp] lemma weighted_vsub_eq_linear_combination {ι} (s : finset ι) {w : ι → k} {p : ι → V} (hw : s.sum w = 0) : s.weighted_vsub p w = ∑ i in s, w i • p i := by simp [s.weighted_vsub_apply, vsub_eq_sub, smul_sub, ← finset.sum_smul, hw] /-- Viewing a module as an affine space modelled on itself, affine combinations are just linear combinations. -/ @[simp] lemma affine_combination_eq_linear_combination (s : finset ι) (p : ι → V) (w : ι → k) (hw : ∑ i in s, w i = 1) : s.affine_combination p w = ∑ i in s, w i • p i := by simp [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p hw 0] include S /-- An `affine_combination` equals a point if that point is in the set and has weight 1 and the other points in the set have weight 0. -/ @[simp] lemma affine_combination_of_eq_one_of_eq_zero (w : ι → k) (p : ι → P) {i : ι} (his : i ∈ s) (hwi : w i = 1) (hw0 : ∀ i2 ∈ s, i2 ≠ i → w i2 = 0) : s.affine_combination p w = p i := begin have h1 : ∑ i in s, w i = 1 := hwi ▸ sum_eq_single i hw0 (λ h, false.elim (h his)), rw [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p h1 (p i), weighted_vsub_of_point_apply], convert zero_vadd V (p i), convert sum_eq_zero _, intros i2 hi2, by_cases h : i2 = i, { simp [h] }, { simp [hw0 i2 hi2 h] } end /-- An affine combination is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma affine_combination_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.affine_combination p w = s₂.affine_combination p (set.indicator ↑s₁ w) := by rw [affine_combination_apply, affine_combination_apply, weighted_vsub_of_point_indicator_subset _ _ _ h] /-- An affine combination, over the image of an embedding, equals an affine combination with the same points and weights over the original `finset`. -/ lemma affine_combination_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).affine_combination p w = s₂.affine_combination (p ∘ e) (w ∘ e) := by simp_rw [affine_combination_apply, weighted_vsub_of_point_map] /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `affine_combination` expressions. -/ lemma sum_smul_vsub_eq_affine_combination_vsub (w : ι → k) (p₁ p₂ : ι → P) : ∑ i in s, w i • (p₁ i -ᵥ p₂ i) = s.affine_combination p₁ w -ᵥ s.affine_combination p₂ w := begin simp_rw [affine_combination_apply, vadd_vsub_vadd_cancel_right], exact s.sum_smul_vsub_eq_weighted_vsub_of_point_sub _ _ _ _ end /-- A weighted sum of pairwise subtractions, where the point on the right is constant and the sum of the weights is 1. -/ lemma sum_smul_vsub_const_eq_affine_combination_vsub (w : ι → k) (p₁ : ι → P) (p₂ : P) (h : ∑ i in s, w i = 1) : ∑ i in s, w i • (p₁ i -ᵥ p₂) = s.affine_combination p₁ w -ᵥ p₂ := by rw [sum_smul_vsub_eq_affine_combination_vsub, affine_combination_apply_const _ _ _ h] /-- A weighted sum of pairwise subtractions, where the point on the left is constant and the sum of the weights is 1. -/ lemma sum_smul_const_vsub_eq_vsub_affine_combination (w : ι → k) (p₂ : ι → P) (p₁ : P) (h : ∑ i in s, w i = 1) : ∑ i in s, w i • (p₁ -ᵥ p₂ i) = p₁ -ᵥ s.affine_combination p₂ w := by rw [sum_smul_vsub_eq_affine_combination_vsub, affine_combination_apply_const _ _ _ h] /-- A weighted sum may be split into a subtraction of affine combinations over two subsets. -/ lemma affine_combination_sdiff_sub [decidable_eq ι] {s₂ : finset ι} (h : s₂ ⊆ s) (w : ι → k) (p : ι → P) : (s \ s₂).affine_combination p w -ᵥ s₂.affine_combination p (-w) = s.weighted_vsub p w := begin simp_rw [affine_combination_apply, vadd_vsub_vadd_cancel_right], exact s.weighted_vsub_sdiff_sub h _ _ end /-- If a weighted sum is zero and one of the weights is `-1`, the corresponding point is the affine combination of the other points with the given weights. -/ lemma affine_combination_eq_of_weighted_vsub_eq_zero_of_eq_neg_one {w : ι → k} {p : ι → P} (hw : s.weighted_vsub p w = (0 : V)) {i : ι} [decidable_pred (≠ i)] (his : i ∈ s) (hwi : w i = -1) : (s.filter (≠ i)).affine_combination p w = p i := begin classical, rw [←@vsub_eq_zero_iff_eq V, ←hw, ←s.affine_combination_sdiff_sub (singleton_subset_iff.2 his), sdiff_singleton_eq_erase, ←filter_ne'], congr, refine (affine_combination_of_eq_one_of_eq_zero _ _ _ (mem_singleton_self _) _ _).symm, { simp [hwi] }, { simp } end /-- An affine combination over `s.subtype pred` equals one over `s.filter pred`. -/ lemma affine_combination_subtype_eq_filter (w : ι → k) (p : ι → P) (pred : ι → Prop) [decidable_pred pred] : (s.subtype pred).affine_combination (λ i, p i) (λ i, w i) = (s.filter pred).affine_combination p w := by rw [affine_combination_apply, affine_combination_apply, weighted_vsub_of_point_subtype_eq_filter] /-- An affine combination over `s.filter pred` equals one over `s` if all the weights at indices in `s` not satisfying `pred` are zero. -/ lemma affine_combination_filter_of_ne (w : ι → k) (p : ι → P) {pred : ι → Prop} [decidable_pred pred] (h : ∀ i ∈ s, w i ≠ 0 → pred i) : (s.filter pred).affine_combination p w = s.affine_combination p w := by rw [affine_combination_apply, affine_combination_apply, s.weighted_vsub_of_point_filter_of_ne _ _ _ h] variables {V} /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub_of_point` using a `finset` lying within that subset and with a given sum of weights if and only if it can be expressed as `weighted_vsub_of_point` with that sum of weights for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype {v : V} {x : k} {s : set ι} {p : ι → P} {b : P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point p b w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point (λ (i : s), p i) b w := begin classical, simp_rw weighted_vsub_of_point_apply, split, { rintros ⟨fs, hfs, w, rfl, rfl⟩, use [fs.subtype s, λ i, w i, sum_subtype_of_mem _ hfs, (sum_subtype_of_mem _ hfs).symm] }, { rintros ⟨fs, w, rfl, rfl⟩, refine ⟨fs.map (function.embedding.subtype _), map_subtype_subset _, λ i, if h : i ∈ s then w ⟨i, h⟩ else 0, _, _⟩; simp } end variables (k) /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub` using a `finset` lying within that subset and with sum of weights 0 if and only if it can be expressed as `weighted_vsub` with sum of weights 0 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_subset_iff_eq_weighted_vsub_subtype {v : V} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub (λ (i : s), p i) w := eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype variables (V) /-- Suppose an indexed family of points is given, along with a subset of the index type. A point can be expressed as an `affine_combination` using a `finset` lying within that subset and with sum of weights 1 if and only if it can be expressed an `affine_combination` with sum of weights 1 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_affine_combination_subset_iff_eq_affine_combination_subtype {p0 : P} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination (λ (i : s), p i) w := begin simp_rw [affine_combination_apply, eq_vadd_iff_vsub_eq], exact eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype end variables {k V} /-- Affine maps commute with affine combinations. -/ lemma map_affine_combination {V₂ P₂ : Type*} [add_comm_group V₂] [module k V₂] [affine_space V₂ P₂] (p : ι → P) (w : ι → k) (hw : s.sum w = 1) (f : P →ᵃ[k] P₂) : f (s.affine_combination p w) = s.affine_combination (f ∘ p) w := begin have b := classical.choice (infer_instance : affine_space V P).nonempty, have b₂ := classical.choice (infer_instance : affine_space V₂ P₂).nonempty, rw [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p hw b, s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w (f ∘ p) hw b₂, ← s.weighted_vsub_of_point_vadd_eq_of_sum_eq_one w (f ∘ p) hw (f b) b₂], simp only [weighted_vsub_of_point_apply, ring_hom.id_apply, affine_map.map_vadd, linear_map.map_smulₛₗ, affine_map.linear_map_vsub, linear_map.map_sum], end end finset namespace finset variables (k : Type*) {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) {ι₂ : Type*} (s₂ : finset ι₂) /-- The weights for the centroid of some points. -/ def centroid_weights : ι → k := function.const ι (card s : k) ⁻¹ /-- `centroid_weights` at any point. -/ @[simp] lemma centroid_weights_apply (i : ι) : s.centroid_weights k i = (card s : k) ⁻¹ := rfl /-- `centroid_weights` equals a constant function. -/ lemma centroid_weights_eq_const : s.centroid_weights k = function.const ι ((card s : k) ⁻¹) := rfl variables {k} /-- The weights in the centroid sum to 1, if the number of points, converted to `k`, is not zero. -/ lemma sum_centroid_weights_eq_one_of_cast_card_ne_zero (h : (card s : k) ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] variables (k) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_eq_one_of_card_ne_zero [char_zero k] (h : card s ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] /-- In the characteristic zero case, the weights in the centroid sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_eq_one_of_nonempty [char_zero k] (h : s.nonempty) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (ne_of_gt (card_pos.2 h)) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_eq_one_of_card_eq_add_one [char_zero k] {n : ℕ} (h : card s = n + 1) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (h.symm ▸ nat.succ_ne_zero n) include V /-- The centroid of some points. Although defined for any `s`, this is intended to be used in the case where the number of points, converted to `k`, is not zero. -/ def centroid (p : ι → P) : P := s.affine_combination p (s.centroid_weights k) /-- The definition of the centroid. -/ lemma centroid_def (p : ι → P) : s.centroid k p = s.affine_combination p (s.centroid_weights k) := rfl lemma centroid_univ (s : finset P) : univ.centroid k (coe : s → P) = s.centroid k id := by { rw [centroid, centroid, ← s.attach_affine_combination_coe], congr, ext, simp, } /-- The centroid of a single point. -/ @[simp] lemma centroid_singleton (p : ι → P) (i : ι) : ({i} : finset ι).centroid k p = p i := by simp [centroid_def, affine_combination_apply] /-- The centroid of two points, expressed directly as adding a vector to a point. -/ lemma centroid_pair [decidable_eq ι] [invertible (2 : k)] (p : ι → P) (i₁ i₂ : ι) : ({i₁, i₂} : finset ι).centroid k p = (2 ⁻¹ : k) • (p i₂ -ᵥ p i₁) +ᵥ p i₁ := begin by_cases h : i₁ = i₂, { simp [h] }, { have hc : (card ({i₁, i₂} : finset ι) : k) ≠ 0, { rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton], norm_num, exact nonzero_of_invertible _ }, rw [centroid_def, affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one _ _ _ (sum_centroid_weights_eq_one_of_cast_card_ne_zero _ hc) (p i₁)], simp [h] } end /-- The centroid of two points indexed by `fin 2`, expressed directly as adding a vector to the first point. -/ lemma centroid_pair_fin [invertible (2 : k)] (p : fin 2 → P) : univ.centroid k p = (2 ⁻¹ : k) • (p 1 -ᵥ p 0) +ᵥ p 0 := begin rw univ_fin2, convert centroid_pair k p 0 1 end /-- A centroid, over the image of an embedding, equals a centroid with the same points and weights over the original `finset`. -/ lemma centroid_map (e : ι₂ ↪ ι) (p : ι → P) : (s₂.map e).centroid k p = s₂.centroid k (p ∘ e) := by simp [centroid_def, affine_combination_map, centroid_weights] omit V /-- `centroid_weights` gives the weights for the centroid as a constant function, which is suitable when summing over the points whose centroid is being taken. This function gives the weights in a form suitable for summing over a larger set of points, as an indicator function that is zero outside the set whose centroid is being taken. In the case of a `fintype`, the sum may be over `univ`. -/ def centroid_weights_indicator : ι → k := set.indicator ↑s (s.centroid_weights k) /-- The definition of `centroid_weights_indicator`. -/ lemma centroid_weights_indicator_def : s.centroid_weights_indicator k = set.indicator ↑s (s.centroid_weights k) := rfl /-- The sum of the weights for the centroid indexed by a `fintype`. -/ lemma sum_centroid_weights_indicator [fintype ι] : ∑ i, s.centroid_weights_indicator k i = ∑ i in s, s.centroid_weights k i := (set.sum_indicator_subset _ (subset_univ _)).symm /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_ne_zero [char_zero k] [fintype ι] (h : card s ≠ 0) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_ne_zero k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_indicator_eq_one_of_nonempty [char_zero k] [fintype ι] (h : s.nonempty) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_nonempty k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_eq_add_one [char_zero k] [fintype ι] {n : ℕ} (h : card s = n + 1) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_eq_add_one k h end include V /-- The centroid as an affine combination over a `fintype`. -/ lemma centroid_eq_affine_combination_fintype [fintype ι] (p : ι → P) : s.centroid k p = univ.affine_combination p (s.centroid_weights_indicator k) := affine_combination_indicator_subset _ _ (subset_univ _) /-- An indexed family of points that is injective on the given `finset` has the same centroid as the image of that `finset`. This is stated in terms of a set equal to the image to provide control of definitional equality for the index type used for the centroid of the image. -/ lemma centroid_eq_centroid_image_of_inj_on {p : ι → P} (hi : ∀ i j ∈ s, p i = p j → i = j) {ps : set P} [fintype ps] (hps : ps = p '' ↑s) : s.centroid k p = (univ : finset ps).centroid k (λ x, x) := begin let f : p '' ↑s → ι := λ x, x.property.some, have hf : ∀ x, f x ∈ s ∧ p (f x) = x := λ x, x.property.some_spec, let f' : ps → ι := λ x, f ⟨x, hps ▸ x.property⟩, have hf' : ∀ x, f' x ∈ s ∧ p (f' x) = x := λ x, hf ⟨x, hps ▸ x.property⟩, have hf'i : function.injective f', { intros x y h, rw [subtype.ext_iff, ←(hf' x).2, ←(hf' y).2, h] }, let f'e : ps ↪ ι := ⟨f', hf'i⟩, have hu : finset.univ.map f'e = s, { ext x, rw mem_map, split, { rintros ⟨i, _, rfl⟩, exact (hf' i).1 }, { intro hx, use [⟨p x, hps.symm ▸ set.mem_image_of_mem _ hx⟩, mem_univ _], refine hi _ (hf' _).1 _ hx _, rw (hf' _).2, refl } }, rw [←hu, centroid_map], congr' with x, change p (f' x) = ↑x, rw (hf' x).2 end /-- Two indexed families of points that are injective on the given `finset`s and with the same points in the image of those `finset`s have the same centroid. -/ lemma centroid_eq_of_inj_on_of_image_eq {p : ι → P} (hi : ∀ i j ∈ s, p i = p j → i = j) {p₂ : ι₂ → P} (hi₂ : ∀ i j ∈ s₂, p₂ i = p₂ j → i = j) (he : p '' ↑s = p₂ '' ↑s₂) : s.centroid k p = s₂.centroid k p₂ := by classical; rw [s.centroid_eq_centroid_image_of_inj_on k hi rfl, s₂.centroid_eq_centroid_image_of_inj_on k hi₂ he] end finset section affine_space' variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] [affine_space V P] variables {ι : Type*} include V /-- A `weighted_vsub` with sum of weights 0 is in the `vector_span` of an indexed family. -/ lemma weighted_vsub_mem_vector_span {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 0) (p : ι → P) : s.weighted_vsub p w ∈ vector_span k (set.range p) := begin classical, rcases is_empty_or_nonempty ι with hι|⟨⟨i0⟩⟩, { resetI, simp [finset.eq_empty_of_is_empty s] }, { rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_image_iff_total, finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s w p h (p i0), finset.weighted_vsub_of_point_apply], let w' := set.indicator ↑s w, have hwx : ∀ i, w' i ≠ 0 → i ∈ s := λ i, set.mem_of_indicator_ne_zero, use [finsupp.on_finset s w' hwx, set.subset_univ _], rw [finsupp.total_apply, finsupp.on_finset_sum hwx], { apply finset.sum_congr rfl, intros i hi, simp [w', set.indicator_apply, if_pos hi] }, { exact λ _, zero_smul k _ } }, end /-- An `affine_combination` with sum of weights 1 is in the `affine_span` of an indexed family, if the underlying ring is nontrivial. -/ lemma affine_combination_mem_affine_span [nontrivial k] {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 1) (p : ι → P) : s.affine_combination p w ∈ affine_span k (set.range p) := begin classical, have hnz : ∑ i in s, w i ≠ 0 := h.symm ▸ one_ne_zero, have hn : s.nonempty := finset.nonempty_of_sum_ne_zero hnz, cases hn with i1 hi1, let w1 : ι → k := function.update (function.const ι 0) i1 1, have hw1 : ∑ i in s, w1 i = 1, { rw [finset.sum_update_of_mem hi1, finset.sum_const_zero, add_zero] }, have hw1s : s.affine_combination p w1 = p i1 := s.affine_combination_of_eq_one_of_eq_zero w1 p hi1 (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), have hv : s.affine_combination p w -ᵥ p i1 ∈ (affine_span k (set.range p)).direction, { rw [direction_affine_span, ←hw1s, finset.affine_combination_vsub], apply weighted_vsub_mem_vector_span, simp [pi.sub_apply, h, hw1] }, rw ←vsub_vadd (s.affine_combination p w) (p i1), exact affine_subspace.vadd_mem_of_mem_direction hv (mem_affine_span k (set.mem_range_self _)) end variables (k) {V} /-- A vector is in the `vector_span` of an indexed family if and only if it is a `weighted_vsub` with sum of weights 0. -/ lemma mem_vector_span_iff_eq_weighted_vsub {v : V} {p : ι → P} : v ∈ vector_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (h : ∑ i in s, w i = 0), v = s.weighted_vsub p w := begin classical, split, { rcases is_empty_or_nonempty ι with hι|⟨⟨i0⟩⟩, swap, { rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_image_iff_total], rintros ⟨l, hl, hv⟩, use insert i0 l.support, set w := (l : ι → k) - function.update (function.const ι 0 : ι → k) i0 (∑ i in l.support, l i) with hwdef, use w, have hw : ∑ i in insert i0 l.support, w i = 0, { rw hwdef, simp_rw [pi.sub_apply, finset.sum_sub_distrib, finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, finset.sum_insert_of_eq_zero_if_not_mem finsupp.not_mem_support_iff.1, add_zero, sub_self] }, use hw, have hz : w i0 • (p i0 -ᵥ p i0 : V) = 0 := (vsub_self (p i0)).symm ▸ smul_zero _, change (λ i, w i • (p i -ᵥ p i0 : V)) i0 = 0 at hz, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero _ w p hw (p i0), finset.weighted_vsub_of_point_apply, ←hv, finsupp.total_apply, finset.sum_insert_zero hz], change ∑ i in l.support, l i • _ = _, congr' with i, by_cases h : i = i0, { simp [h] }, { simp [hwdef, h] } }, { resetI, rw [set.range_eq_empty, vector_span_empty, submodule.mem_bot], rintro rfl, use [∅], simp } }, { rintros ⟨s, w, hw, rfl⟩, exact weighted_vsub_mem_vector_span hw p } end variables {k} /-- A point in the `affine_span` of an indexed family is an `affine_combination` with sum of weights 1. See also `eq_affine_combination_of_mem_affine_span_of_fintype`. -/ lemma eq_affine_combination_of_mem_affine_span {p1 : P} {p : ι → P} (h : p1 ∈ affine_span k (set.range p)) : ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin classical, have hn : ((affine_span k (set.range p)) : set P).nonempty := ⟨p1, h⟩, rw [affine_span_nonempty, set.range_nonempty_iff_nonempty] at hn, cases hn with i0, have h0 : p i0 ∈ affine_span k (set.range p) := mem_affine_span k (set.mem_range_self i0), have hd : p1 -ᵥ p i0 ∈ (affine_span k (set.range p)).direction := affine_subspace.vsub_mem_direction h h0, rw [direction_affine_span, mem_vector_span_iff_eq_weighted_vsub] at hd, rcases hd with ⟨s, w, h, hs⟩, let s' := insert i0 s, let w' := set.indicator ↑s w, have h' : ∑ i in s', w' i = 0, { rw [←h, set.sum_indicator_subset _ (finset.subset_insert i0 s)] }, have hs' : s'.weighted_vsub p w' = p1 -ᵥ p i0, { rw hs, exact (finset.weighted_vsub_indicator_subset _ _ (finset.subset_insert i0 s)).symm }, let w0 : ι → k := function.update (function.const ι 0) i0 1, have hw0 : ∑ i in s', w0 i = 1, { rw [finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, add_zero] }, have hw0s : s'.affine_combination p w0 = p i0 := s'.affine_combination_of_eq_one_of_eq_zero w0 p (finset.mem_insert_self _ _) (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), use [s', w0 + w'], split, { simp [pi.add_apply, finset.sum_add_distrib, hw0, h'] }, { rw [add_comm, ←finset.weighted_vsub_vadd_affine_combination, hw0s, hs', vsub_vadd] } end lemma eq_affine_combination_of_mem_affine_span_of_fintype [fintype ι] {p1 : P} {p : ι → P} (h : p1 ∈ affine_span k (set.range p)) : ∃ (w : ι → k) (hw : ∑ i, w i = 1), p1 = finset.univ.affine_combination p w := begin classical, obtain ⟨s, w, hw, rfl⟩ := eq_affine_combination_of_mem_affine_span h, refine ⟨(s : set ι).indicator w, _, finset.affine_combination_indicator_subset w p s.subset_univ⟩, simp only [finset.mem_coe, set.indicator_apply, ← hw], rw fintype.sum_extend_by_zero s w, end variables (k V) /-- A point is in the `affine_span` of an indexed family if and only if it is an `affine_combination` with sum of weights 1, provided the underlying ring is nontrivial. -/ lemma mem_affine_span_iff_eq_affine_combination [nontrivial k] {p1 : P} {p : ι → P} : p1 ∈ affine_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin split, { exact eq_affine_combination_of_mem_affine_span }, { rintros ⟨s, w, hw, rfl⟩, exact affine_combination_mem_affine_span hw p } end /-- Given a family of points together with a chosen base point in that family, membership of the affine span of this family corresponds to an identity in terms of `weighted_vsub_of_point`, with weights that are not required to sum to 1. -/ lemma mem_affine_span_iff_eq_weighted_vsub_of_point_vadd [nontrivial k] (p : ι → P) (j : ι) (q : P) : q ∈ affine_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k), q = s.weighted_vsub_of_point p (p j) w +ᵥ (p j) := begin split, { intros hq, obtain ⟨s, w, hw, rfl⟩ := eq_affine_combination_of_mem_affine_span hq, exact ⟨s, w, s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p hw (p j)⟩, }, { rintros ⟨s, w, rfl⟩, classical, let w' : ι → k := function.update w j (1 - (s \ {j}).sum w), have h₁ : (insert j s).sum w' = 1, { by_cases hj : j ∈ s, { simp [finset.sum_update_of_mem hj, finset.insert_eq_of_mem hj], }, { simp [w', finset.sum_insert hj, finset.sum_update_of_not_mem hj, hj], }, }, have hww : ∀ i, i ≠ j → w i = w' i, { intros i hij, simp [w', hij], }, rw [s.weighted_vsub_of_point_eq_of_weights_eq p j w w' hww, ← s.weighted_vsub_of_point_insert w' p j, ← (insert j s).affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w' p h₁ (p j)], exact affine_combination_mem_affine_span h₁ p, }, end variables {k V} /-- Given a set of points, together with a chosen base point in this set, if we affinely transport all other members of the set along the line joining them to this base point, the affine span is unchanged. -/ lemma affine_span_eq_affine_span_line_map_units [nontrivial k] {s : set P} {p : P} (hp : p ∈ s) (w : s → units k) : affine_span k (set.range (λ (q : s), affine_map.line_map p ↑q (w q : k))) = affine_span k s := begin have : s = set.range (coe : s → P), { simp, }, conv_rhs { rw this, }, apply le_antisymm; intros q hq; erw mem_affine_span_iff_eq_weighted_vsub_of_point_vadd k V _ (⟨p, hp⟩ : s) q at hq ⊢; obtain ⟨t, μ, rfl⟩ := hq; use t; [use λ x, (μ x) * ↑(w x), use λ x, (μ x) * ↑(w x)⁻¹]; simp [smul_smul], end end affine_space' section division_ring variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V open set finset /-- The centroid lies in the affine span if the number of points, converted to `k`, is not zero. -/ lemma centroid_mem_affine_span_of_cast_card_ne_zero {s : finset ι} (p : ι → P) (h : (card s : k) ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_cast_card_ne_zero h) p variables (k) /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is not zero. -/ lemma centroid_mem_affine_span_of_card_ne_zero [char_zero k] {s : finset ι} (p : ι → P) (h : card s ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_ne_zero k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the set is nonempty. -/ lemma centroid_mem_affine_span_of_nonempty [char_zero k] {s : finset ι} (p : ι → P) (h : s.nonempty) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_nonempty k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is `n + 1`. -/ lemma centroid_mem_affine_span_of_card_eq_add_one [char_zero k] {s : finset ι} (p : ι → P) {n : ℕ} (h : card s = n + 1) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_eq_add_one k h) p end division_ring namespace affine_map variables {k : Type*} {V : Type*} (P : Type*) [comm_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) include V -- TODO: define `affine_map.proj`, `affine_map.fst`, `affine_map.snd` /-- A weighted sum, as an affine map on the points involved. -/ def weighted_vsub_of_point (w : ι → k) : ((ι → P) × P) →ᵃ[k] V := { to_fun := λ p, s.weighted_vsub_of_point p.fst p.snd w, linear := ∑ i in s, w i • ((linear_map.proj i).comp (linear_map.fst _ _ _) - linear_map.snd _ _ _), map_vadd' := begin rintros ⟨p, b⟩ ⟨v, b'⟩, simp [linear_map.sum_apply, finset.weighted_vsub_of_point, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub, ← sub_add_eq_add_sub, smul_add, finset.sum_add_distrib] end } end affine_map
26245aa37d73ea599575ac12599d2069c9f8ccf1
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/special_functions/trigonometric/inverse.lean
0a3499d72f030a3a7751f03803dc9e919f46da03
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
12,929
lean
/- 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 analysis.special_functions.trigonometric.basic import topology.algebra.ordered.proj_Icc /-! # Inverse trigonometric functions. See also `analysis.special_functions.trigonometric.arctan` for the inverse tan function. (This is delayed as it is easier to set up after developing complex trigonometric functions.) Basic inequalities on trigonometric functions. -/ noncomputable theory open_locale classical topological_space filter open set filter open_locale real namespace real /-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x ≤ π / 2`. It defaults to `-π / 2` on `(-∞, -1)` and to `π / 2` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arcsin : ℝ → ℝ := coe ∘ Icc_extend (neg_le_self zero_le_one) sin_order_iso.symm lemma arcsin_mem_Icc (x : ℝ) : arcsin x ∈ Icc (-(π / 2)) (π / 2) := subtype.coe_prop _ @[simp] lemma range_arcsin : range arcsin = Icc (-(π / 2)) (π / 2) := by { rw [arcsin, range_comp coe], simp [Icc] } lemma arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := (arcsin_mem_Icc x).2 lemma neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := (arcsin_mem_Icc x).1 lemma arcsin_proj_Icc (x : ℝ) : arcsin (proj_Icc (-1) 1 (neg_le_self $ @zero_le_one ℝ _) x) = arcsin x := by rw [arcsin, function.comp_app, Icc_extend_coe, function.comp_app, Icc_extend] lemma sin_arcsin' {x : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) : sin (arcsin x) = x := by simpa [arcsin, Icc_extend_of_mem _ _ hx, -order_iso.apply_symm_apply] using subtype.ext_iff.1 (sin_order_iso.apply_symm_apply ⟨x, hx⟩) lemma sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x := sin_arcsin' ⟨hx₁, hx₂⟩ lemma arcsin_sin' {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin (sin x) = x := inj_on_sin (arcsin_mem_Icc _) hx $ by rw [sin_arcsin (neg_one_le_sin _) (sin_le_one _)] lemma arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x := arcsin_sin' ⟨hx₁, hx₂⟩ lemma strict_mono_on_arcsin : strict_mono_on arcsin (Icc (-1) 1) := (subtype.strict_mono_coe _).comp_strict_mono_on $ sin_order_iso.symm.strict_mono.strict_mono_on_Icc_extend _ lemma monotone_arcsin : monotone arcsin := (subtype.mono_coe _).comp $ sin_order_iso.symm.monotone.Icc_extend _ lemma inj_on_arcsin : inj_on arcsin (Icc (-1) 1) := strict_mono_on_arcsin.inj_on lemma arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arcsin x = arcsin y ↔ x = y := inj_on_arcsin.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[continuity] lemma continuous_arcsin : continuous arcsin := continuous_subtype_coe.comp sin_order_iso.symm.continuous.Icc_extend' lemma continuous_at_arcsin {x : ℝ} : continuous_at arcsin x := continuous_arcsin.continuous_at lemma arcsin_eq_of_sin_eq {x y : ℝ} (h₁ : sin x = y) (h₂ : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin y = x := begin subst y, exact inj_on_sin (arcsin_mem_Icc _) h₂ (sin_arcsin' (sin_mem_Icc x)) end @[simp] lemma arcsin_zero : arcsin 0 = 0 := arcsin_eq_of_sin_eq sin_zero ⟨neg_nonpos.2 pi_div_two_pos.le, pi_div_two_pos.le⟩ @[simp] lemma arcsin_one : arcsin 1 = π / 2 := arcsin_eq_of_sin_eq sin_pi_div_two $ right_mem_Icc.2 (neg_le_self pi_div_two_pos.le) lemma arcsin_of_one_le {x : ℝ} (hx : 1 ≤ x) : arcsin x = π / 2 := by rw [← arcsin_proj_Icc, proj_Icc_of_right_le _ hx, subtype.coe_mk, arcsin_one] lemma arcsin_neg_one : arcsin (-1) = -(π / 2) := arcsin_eq_of_sin_eq (by rw [sin_neg, sin_pi_div_two]) $ left_mem_Icc.2 (neg_le_self pi_div_two_pos.le) lemma arcsin_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arcsin x = -(π / 2) := by rw [← arcsin_proj_Icc, proj_Icc_of_le_left _ hx, subtype.coe_mk, arcsin_neg_one] @[simp] lemma arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x := begin cases le_total x (-1) with hx₁ hx₁, { rw [arcsin_of_le_neg_one hx₁, neg_neg, arcsin_of_one_le (le_neg.2 hx₁)] }, cases le_total 1 x with hx₂ hx₂, { rw [arcsin_of_one_le hx₂, arcsin_of_le_neg_one (neg_le_neg hx₂)] }, refine arcsin_eq_of_sin_eq _ _, { rw [sin_neg, sin_arcsin hx₁ hx₂] }, { exact ⟨neg_le_neg (arcsin_le_pi_div_two _), neg_le.2 (neg_pi_div_two_le_arcsin _)⟩ } end lemma arcsin_le_iff_le_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rw [← arcsin_sin' hy, strict_mono_on_arcsin.le_iff_le hx (sin_mem_Icc _), arcsin_sin' hy] lemma arcsin_le_iff_le_sin' {x y : ℝ} (hy : y ∈ Ico (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := begin cases le_total x (-1) with hx₁ hx₁, { simp [arcsin_of_le_neg_one hx₁, hy.1, hx₁.trans (neg_one_le_sin _)] }, cases lt_or_le 1 x with hx₂ hx₂, { simp [arcsin_of_one_le hx₂.le, hy.2.not_le, (sin_le_one y).trans_lt hx₂] }, exact arcsin_le_iff_le_sin ⟨hx₁, hx₂⟩ (mem_Icc_of_Ico hy) end lemma le_arcsin_iff_sin_le {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin ⟨neg_le_neg hy.2, neg_le.2 hy.1⟩ ⟨neg_le_neg hx.2, neg_le.2 hx.1⟩, sin_neg, neg_le_neg_iff] lemma le_arcsin_iff_sin_le' {x y : ℝ} (hx : x ∈ Ioc (-(π / 2)) (π / 2)) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin' ⟨neg_le_neg hx.2, neg_lt.2 hx.1⟩, sin_neg, neg_le_neg_iff] lemma arcsin_lt_iff_lt_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le hy hx).trans not_le lemma arcsin_lt_iff_lt_sin' {x y : ℝ} (hy : y ∈ Ioc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le' hy).trans not_le lemma lt_arcsin_iff_sin_lt {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x < arcsin y ↔ sin x < y := not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin hy hx).trans not_le lemma lt_arcsin_iff_sin_lt' {x y : ℝ} (hx : x ∈ Ico (-(π / 2)) (π / 2)) : x < arcsin y ↔ sin x < y := not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin' hx).trans not_le lemma arcsin_eq_iff_eq_sin {x y : ℝ} (hy : y ∈ Ioo (-(π / 2)) (π / 2)) : arcsin x = y ↔ x = sin y := by simp only [le_antisymm_iff, arcsin_le_iff_le_sin' (mem_Ico_of_Ioo hy), le_arcsin_iff_sin_le' (mem_Ioc_of_Ioo hy)] @[simp] lemma arcsin_nonneg {x : ℝ} : 0 ≤ arcsin x ↔ 0 ≤ x := (le_arcsin_iff_sin_le' ⟨neg_lt_zero.2 pi_div_two_pos, pi_div_two_pos.le⟩).trans $ by rw [sin_zero] @[simp] lemma arcsin_nonpos {x : ℝ} : arcsin x ≤ 0 ↔ x ≤ 0 := neg_nonneg.symm.trans $ arcsin_neg x ▸ arcsin_nonneg.trans neg_nonneg @[simp] lemma arcsin_eq_zero_iff {x : ℝ} : arcsin x = 0 ↔ x = 0 := by simp [le_antisymm_iff] @[simp] lemma zero_eq_arcsin_iff {x} : 0 = arcsin x ↔ x = 0 := eq_comm.trans arcsin_eq_zero_iff @[simp] lemma arcsin_pos {x : ℝ} : 0 < arcsin x ↔ 0 < x := lt_iff_lt_of_le_iff_le arcsin_nonpos @[simp] lemma arcsin_lt_zero {x : ℝ} : arcsin x < 0 ↔ x < 0 := lt_iff_lt_of_le_iff_le arcsin_nonneg @[simp] lemma arcsin_lt_pi_div_two {x : ℝ} : arcsin x < π / 2 ↔ x < 1 := (arcsin_lt_iff_lt_sin' (right_mem_Ioc.2 $ neg_lt_self pi_div_two_pos)).trans $ by rw sin_pi_div_two @[simp] lemma neg_pi_div_two_lt_arcsin {x : ℝ} : -(π / 2) < arcsin x ↔ -1 < x := (lt_arcsin_iff_sin_lt' $ left_mem_Ico.2 $ neg_lt_self pi_div_two_pos).trans $ by rw [sin_neg, sin_pi_div_two] @[simp] lemma arcsin_eq_pi_div_two {x : ℝ} : arcsin x = π / 2 ↔ 1 ≤ x := ⟨λ h, not_lt.1 $ λ h', (arcsin_lt_pi_div_two.2 h').ne h, arcsin_of_one_le⟩ @[simp] lemma pi_div_two_eq_arcsin {x} : π / 2 = arcsin x ↔ 1 ≤ x := eq_comm.trans arcsin_eq_pi_div_two @[simp] lemma pi_div_two_le_arcsin {x} : π / 2 ≤ arcsin x ↔ 1 ≤ x := (arcsin_le_pi_div_two x).le_iff_eq.trans pi_div_two_eq_arcsin @[simp] lemma arcsin_eq_neg_pi_div_two {x : ℝ} : arcsin x = -(π / 2) ↔ x ≤ -1 := ⟨λ h, not_lt.1 $ λ h', (neg_pi_div_two_lt_arcsin.2 h').ne' h, arcsin_of_le_neg_one⟩ @[simp] lemma neg_pi_div_two_eq_arcsin {x} : -(π / 2) = arcsin x ↔ x ≤ -1 := eq_comm.trans arcsin_eq_neg_pi_div_two @[simp] lemma arcsin_le_neg_pi_div_two {x} : arcsin x ≤ -(π / 2) ↔ x ≤ -1 := (neg_pi_div_two_le_arcsin x).le_iff_eq.trans arcsin_eq_neg_pi_div_two @[simp] lemma pi_div_four_le_arcsin {x} : π / 4 ≤ arcsin x ↔ sqrt 2 / 2 ≤ x := by { rw [← sin_pi_div_four, le_arcsin_iff_sin_le'], have := pi_pos, split; linarith } lemma maps_to_sin_Ioo : maps_to sin (Ioo (-(π / 2)) (π / 2)) (Ioo (-1) 1) := λ x h, by rwa [mem_Ioo, ← arcsin_lt_pi_div_two, ← neg_pi_div_two_lt_arcsin, arcsin_sin h.1.le h.2.le] /-- `real.sin` as a `local_homeomorph` between `(-π / 2, π / 2)` and `(-1, 1)`. -/ @[simp] def sin_local_homeomorph : local_homeomorph ℝ ℝ := { to_fun := sin, inv_fun := arcsin, source := Ioo (-(π / 2)) (π / 2), target := Ioo (-1) 1, map_source' := maps_to_sin_Ioo, map_target' := λ y hy, ⟨neg_pi_div_two_lt_arcsin.2 hy.1, arcsin_lt_pi_div_two.2 hy.2⟩, left_inv' := λ x hx, arcsin_sin hx.1.le hx.2.le, right_inv' := λ y hy, sin_arcsin hy.1.le hy.2.le, open_source := is_open_Ioo, open_target := is_open_Ioo, continuous_to_fun := continuous_sin.continuous_on, continuous_inv_fun := continuous_arcsin.continuous_on } lemma cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) := cos_nonneg_of_mem_Icc ⟨neg_pi_div_two_le_arcsin _, arcsin_le_pi_div_two _⟩ lemma cos_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arcsin x) = sqrt (1 - x ^ 2) := have sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x), begin rw [← eq_sub_iff_add_eq', ← sqrt_inj (sq_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))), sq, sqrt_mul_self (cos_arcsin_nonneg _)] at this, rw [this, sin_arcsin hx₁ hx₂], end /-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`. If the argument is not between `-1` and `1` it defaults to `π / 2` -/ @[pp_nodot] noncomputable def arccos (x : ℝ) : ℝ := π / 2 - arcsin x lemma arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl lemma arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x := by simp [arccos] lemma arccos_le_pi (x : ℝ) : arccos x ≤ π := by unfold arccos; linarith [neg_pi_div_two_le_arcsin x] lemma arccos_nonneg (x : ℝ) : 0 ≤ arccos x := by unfold arccos; linarith [arcsin_le_pi_div_two x] lemma cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x := by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂] lemma arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x := by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin]; simp [sub_eq_add_neg]; linarith lemma strict_anti_on_arccos : strict_anti_on arccos (Icc (-1) 1) := λ x hx y hy h, sub_lt_sub_left (strict_mono_on_arcsin hx hy h) _ lemma arccos_inj_on : inj_on arccos (Icc (-1) 1) := strict_anti_on_arccos.inj_on lemma arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arccos x = arccos y ↔ x = y := arccos_inj_on.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[simp] lemma arccos_zero : arccos 0 = π / 2 := by simp [arccos] @[simp] lemma arccos_one : arccos 1 = 0 := by simp [arccos] @[simp] lemma arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves] @[simp] lemma arccos_eq_zero {x} : arccos x = 0 ↔ 1 ≤ x := by simp [arccos, sub_eq_zero] @[simp] lemma arccos_eq_pi_div_two {x} : arccos x = π / 2 ↔ x = 0 := by simp [arccos] @[simp] lemma arccos_eq_pi {x} : arccos x = π ↔ x ≤ -1 := by rw [arccos, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', div_two_sub_self, neg_pi_div_two_eq_arcsin] lemma arccos_neg (x : ℝ) : arccos (-x) = π - arccos x := by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self, sub_neg_eq_add] lemma sin_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arccos x) = sqrt (1 - x ^ 2) := by rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin hx₁ hx₂] @[simp] lemma arccos_le_pi_div_two {x} : arccos x ≤ π / 2 ↔ 0 ≤ x := by simp [arccos] @[simp] lemma arccos_le_pi_div_four {x} : arccos x ≤ π / 4 ↔ sqrt 2 / 2 ≤ x := by { rw [arccos, ← pi_div_four_le_arcsin], split; { intro, linarith } } @[continuity] lemma continuous_arccos : continuous arccos := continuous_const.sub continuous_arcsin end real
acc0d79dc039c2ebb3680a63f09257ca14ae6818
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Std/Data/BinomialHeap.lean
8c63fa6759fbebbb85026a7234abfd581fbe59cc
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
8,412
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jannis Limperg -/ namespace Std universe u namespace BinomialHeapImp structure HeapNodeAux (α : Type u) (h : Type u) where val : α rank : Nat children : List h -- A `Heap` is a forest of binomial trees. inductive Heap (α : Type u) : Type u where | heap (ns : List (HeapNodeAux α (Heap α))) : Heap α deriving Inhabited open Heap -- A `HeapNode` is a binomial tree. If a `HeapNode` has rank `k`, its children -- have ranks between `0` and `k - 1`. They are ordered by rank. Additionally, -- the value of each child must be less than or equal to the value of its -- parent node. abbrev HeapNode α := HeapNodeAux α (Heap α) variable {α : Type u} def hRank : List (HeapNode α) → Nat | [] => 0 | h::_ => h.rank def isEmpty : Heap α → Bool | heap [] => true | _ => false def empty : Heap α := heap [] def singleton (a : α) : Heap α := heap [{ val := a, rank := 1, children := [] }] -- Combine two binomial trees of rank `r`, creating a binomial tree of rank -- `r + 1`. @[specialize] def combine (le : α → α → Bool) (n₁ n₂ : HeapNode α) : HeapNode α := if le n₂.val n₁.val then { n₂ with rank := n₂.rank + 1, children := n₂.children ++ [heap [n₁]] } else { n₁ with rank := n₁.rank + 1, children := n₁.children ++ [heap [n₂]] } -- Merge two forests of binomial trees. The forests are assumed to be ordered -- by rank and `mergeNodes` maintains this invariant. @[specialize] partial def mergeNodes (le : α → α → Bool) : List (HeapNode α) → List (HeapNode α) → List (HeapNode α) | [], h => h | h, [] => h | f@(h₁ :: t₁), s@(h₂ :: t₂) => if h₁.rank < h₂.rank then h₁ :: mergeNodes le t₁ s else if h₂.rank < h₁.rank then h₂ :: mergeNodes le t₂ f else let merged := combine le h₁ h₂ let r := merged.rank if r != hRank t₁ then if r != hRank t₂ then merged :: mergeNodes le t₁ t₂ else mergeNodes le (merged :: t₁) t₂ else if r != hRank t₂ then mergeNodes le t₁ (merged :: t₂) else merged :: mergeNodes le t₁ t₂ @[specialize] def merge (le : α → α → Bool) : Heap α → Heap α → Heap α | heap h₁, heap h₂ => heap (mergeNodes le h₁ h₂) @[specialize] def head? (le : α → α → Bool) : Heap α → Option α | heap [] => none | heap (h::hs) => some $ hs.foldl (init := h.val) fun r n => if le r n.val then r else n.val @[inline] def head [Inhabited α] (le : α → α → Bool) (h : Heap α) : α := head? le h |>.getD arbitrary @[specialize] def findMin (le : α → α → Bool) : List (HeapNode α) → Nat → HeapNode α × Nat → HeapNode α × Nat | [], _, r => r | h::hs, idx, (h', idx') => if le h'.val h.val then findMin le hs (idx+1) (h', idx') else findMin le hs (idx+1) (h, idx) -- It is important that we check `le h'.val h.val` here, not the other way -- around. This ensures that head? and findMin find the same element even -- when we have `le h'.val h.val` and `le h.val h'.val` (i.e. le is not -- irreflexive). def deleteMin (le : α → α → Bool) : Heap α → Option (α × Heap α) | heap [] => none | heap [h] => let tail := match h.children with | [] => empty | (h::hs) => hs.foldl (merge le) h some (h.val, tail) | heap hhs@(h::hs) => let (min, minIdx) := findMin le hs 1 (h, 0) let rest := hhs.eraseIdx minIdx let tail := min.children.foldl (merge le) (heap rest) some (min.val, tail) @[inline] def tail? (le : α → α → Bool) (h : Heap α) : Option (Heap α) := deleteMin le h |>.map (·.snd) @[inline] def tail (le : α → α → Bool) (h : Heap α) : Heap α := tail? le h |>.getD empty partial def toList (le : α → α → Bool) (h : Heap α) : List α := match deleteMin le h with | none => [] | some (hd, tl) => hd :: toList le tl partial def toArray (le : α → α → Bool) (h : Heap α) : Array α := go #[] h where go (acc : Array α) (h : Heap α) : Array α := match deleteMin le h with | none => acc | some (hd, tl) => go (acc.push hd) tl partial def toListUnordered : Heap α → List α | heap ns => ns.bind fun n => n.val :: n.children.bind toListUnordered partial def toArrayUnordered (h : Heap α) : Array α := go #[] h where go (acc : Array α) : Heap α → Array α | heap ns => do let mut acc := acc for n in ns do acc := acc.push n.val for h in n.children do acc := go acc h return acc inductive WellFormed (le : α → α → Bool) : Heap α → Prop where | empty : WellFormed le empty | singleton (a) : WellFormed le (singleton a) | merge (h₁ h₂) : WellFormed le h₁ → WellFormed le h₂ → WellFormed le (merge le h₁ h₂) | deleteMin (a) (h tl) : WellFormed le h → deleteMin le h = some (a, tl) → WellFormed le tl theorem WellFormed.tail? {le} (h tl : Heap α) (hwf : WellFormed le h) (eq : tail? le h = some tl) : WellFormed le tl := by simp only [BinomialHeapImp.tail?] at eq match eq₂: BinomialHeapImp.deleteMin le h with | none => rw [eq₂] at eq; cases eq | some (a, tl) => rw [eq₂] at eq; cases eq exact deleteMin _ _ _ hwf eq₂ theorem WellFormed.tail {le} (h : Heap α) (hwf : WellFormed le h) : WellFormed le (tail le h) := by simp only [BinomialHeapImp.tail] match eq: BinomialHeapImp.tail? le h with | none => exact empty | some tl => exact tail? _ _ hwf eq end BinomialHeapImp open BinomialHeapImp def BinomialHeap (α : Type u) (le : α → α → Bool) := { h : Heap α // WellFormed le h } @[inline] def mkBinomialHeap (α : Type u) (le : α → α → Bool) : BinomialHeap α le := ⟨empty, WellFormed.empty⟩ namespace BinomialHeap variable {α : Type u} {le : α → α → Bool} @[inline] def empty : BinomialHeap α le := mkBinomialHeap α le @[inline] def isEmpty : BinomialHeap α le → Bool | ⟨b, _⟩ => BinomialHeapImp.isEmpty b /- O(1) -/ @[inline] def singleton (a : α) : BinomialHeap α le := ⟨BinomialHeapImp.singleton a, WellFormed.singleton a⟩ /- O(log n) -/ @[inline] def merge : BinomialHeap α le → BinomialHeap α le → BinomialHeap α le | ⟨b₁, h₁⟩, ⟨b₂, h₂⟩ => ⟨BinomialHeapImp.merge le b₁ b₂, WellFormed.merge b₁ b₂ h₁ h₂⟩ /- O(log n) -/ @[inline] def insert (a : α) (h : BinomialHeap α le) : BinomialHeap α le := merge (singleton a) h /- O(n log n) -/ def ofList (le : α → α → Bool) (as : List α) : BinomialHeap α le := as.foldl (flip insert) empty /- O(n log n) -/ def ofArray (le : α → α → Bool) (as : Array α) : BinomialHeap α le := as.foldl (flip insert) empty /- O(log n) -/ @[inline] def deleteMin : BinomialHeap α le → Option (α × BinomialHeap α le) | ⟨b, h⟩ => match eq: BinomialHeapImp.deleteMin le b with | none => none | some (a, tl) => some (a, ⟨tl, WellFormed.deleteMin a b tl h eq⟩) /- O(log n) -/ @[inline] def head [Inhabited α] : BinomialHeap α le → α | ⟨b, _⟩ => BinomialHeapImp.head le b /- O(log n) -/ @[inline] def head? : BinomialHeap α le → Option α | ⟨b, _⟩ => BinomialHeapImp.head? le b /- O(log n) -/ @[inline] def tail? : BinomialHeap α le → Option (BinomialHeap α le) | ⟨b, h⟩ => match eq: BinomialHeapImp.tail? le b with | none => none | some tl => some ⟨tl, WellFormed.tail? b tl h eq⟩ /- O(log n) -/ @[inline] def tail : BinomialHeap α le → BinomialHeap α le | ⟨b, h⟩ => ⟨BinomialHeapImp.tail le b, WellFormed.tail b h⟩ /- O(n log n) -/ @[inline] def toList : BinomialHeap α le → List α | ⟨b, _⟩ => BinomialHeapImp.toList le b /- O(n log n) -/ @[inline] def toArray : BinomialHeap α le → Array α | ⟨b, _⟩ => BinomialHeapImp.toArray le b /- O(n) -/ @[inline] def toListUnordered : BinomialHeap α le → List α | ⟨b, _⟩ => BinomialHeapImp.toListUnordered b /- O(n) -/ @[inline] def toArrayUnordered : BinomialHeap α le → Array α | ⟨b, _⟩ => BinomialHeapImp.toArrayUnordered b end BinomialHeap end Std
8c61675e8ebc207b5422dc5c4d1312f7abcee7cf
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/t6.lean
03ce609546b23728557ad2d8c630c2ee0ca2183c
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
337
lean
prelude definition Prop : Type := Sort 0 section variable {A : Type*} -- Mark A as implicit parameter variable R : A → A → Prop definition id (a : A) : A := a definition refl : Prop := forall (a : A), R a a definition symm : Prop := forall (a b : A), R a b -> R b a end check id.{2} check refl.{1} check symm.{1}
70c49ce70e129c6032c041c5a9a17bfe6472a9b4
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/ring_theory/unique_factorization_domain.lean
5c44ccc287e059ae776eaa15715e0148d4221bcb
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
46,923
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import algebra.gcd_monoid import ring_theory.integral_domain import ring_theory.noetherian /-- # Unique factorization ## Main Definitions * `wf_dvd_monoid` holds for `monoid`s for which a strict divisibility relation is well-founded. * `unique_factorization_monoid` holds for `wf_dvd_monoid`s where `irreducible` is equivalent to `prime` ## To do * set up the complete lattice structure on `factor_set`. -/ variables {α : Type*} local infix ` ~ᵤ ` : 50 := associated /-- Well-foundedness of the strict version of |, which is equivalent to the descending chain condition on divisibility and to the ascending chain condition on principal ideals in an integral domain. -/ class wf_dvd_monoid (α : Type*) [comm_monoid_with_zero α] : Prop := (well_founded_dvd_not_unit : well_founded (@dvd_not_unit α _)) export wf_dvd_monoid (well_founded_dvd_not_unit) @[priority 100] -- see Note [lower instance priority] instance is_noetherian_ring.wf_dvd_monoid [integral_domain α] [is_noetherian_ring α] : wf_dvd_monoid α := ⟨by { convert inv_image.wf (λ a, ideal.span ({a} : set α)) (well_founded_submodule_gt _ _), ext, exact ideal.span_singleton_lt_span_singleton.symm }⟩ instance polynomial.wf_dvd_monoid [integral_domain α] [wf_dvd_monoid α] : wf_dvd_monoid (polynomial α) := { well_founded_dvd_not_unit := begin classical, refine rel_hom.well_founded ⟨λ p, (if p = 0 then ⊤ else ↑p.degree, p.leading_coeff), _⟩ (prod.lex_wf (with_top.well_founded_lt $ with_bot.well_founded_lt nat.lt_wf) _inst_2.well_founded_dvd_not_unit), rintros a b ⟨ane0, ⟨c, ⟨not_unit_c, rfl⟩⟩⟩, rw [polynomial.degree_mul, if_neg ane0], split_ifs with hac, { rw [hac, polynomial.leading_coeff_zero], apply prod.lex.left, exact lt_of_le_of_ne le_top with_top.coe_ne_top }, have cne0 : c ≠ 0 := right_ne_zero_of_mul hac, simp only [cne0, ane0, polynomial.leading_coeff_mul], by_cases hdeg : c.degree = 0, { simp only [hdeg, add_zero], refine prod.lex.right _ ⟨_, ⟨c.leading_coeff, (λ unit_c, not_unit_c _), rfl⟩⟩, { rwa [ne, polynomial.leading_coeff_eq_zero] }, rw [polynomial.is_unit_iff, polynomial.eq_C_of_degree_eq_zero hdeg], use [c.leading_coeff, unit_c], rw [polynomial.leading_coeff, polynomial.nat_degree_eq_of_degree_eq_some hdeg] }, { apply prod.lex.left, rw polynomial.degree_eq_nat_degree cne0 at *, rw [with_top.coe_lt_coe, polynomial.degree_eq_nat_degree ane0, ← with_bot.coe_add, with_bot.coe_lt_coe], exact lt_add_of_pos_right _ (nat.pos_of_ne_zero (λ h, hdeg (h.symm ▸ with_bot.coe_zero))) }, end } namespace wf_dvd_monoid variables [comm_monoid_with_zero α] open associates nat theorem of_wf_dvd_monoid_associates (h : wf_dvd_monoid (associates α)): wf_dvd_monoid α := ⟨begin haveI := h, refine (surjective.well_founded_iff mk_surjective _).2 wf_dvd_monoid.well_founded_dvd_not_unit, intros, rw mk_dvd_not_unit_mk_iff end⟩ variables [wf_dvd_monoid α] instance wf_dvd_monoid_associates : wf_dvd_monoid (associates α) := ⟨begin refine (surjective.well_founded_iff mk_surjective _).1 wf_dvd_monoid.well_founded_dvd_not_unit, intros, rw mk_dvd_not_unit_mk_iff end⟩ theorem well_founded_associates : well_founded ((<) : associates α → associates α → Prop) := subrelation.wf (λ x y, dvd_not_unit_of_lt) wf_dvd_monoid.well_founded_dvd_not_unit local attribute [elab_as_eliminator] well_founded.fix lemma exists_irreducible_factor {a : α} (ha : ¬ is_unit a) (ha0 : a ≠ 0) : ∃ i, irreducible i ∧ i ∣ a := (irreducible_or_factor a ha).elim (λ hai, ⟨a, hai, dvd_refl _⟩) (well_founded.fix wf_dvd_monoid.well_founded_dvd_not_unit (λ a ih ha ha0 ⟨x, y, hx, hy, hxy⟩, have hx0 : x ≠ 0, from λ hx0, ha0 (by rw [← hxy, hx0, zero_mul]), (irreducible_or_factor x hx).elim (λ hxi, ⟨x, hxi, hxy ▸ by simp⟩) (λ hxf, let ⟨i, hi⟩ := ih x ⟨hx0, y, hy, hxy.symm⟩ hx hx0 hxf in ⟨i, hi.1, dvd.trans hi.2 (hxy ▸ by simp)⟩)) a ha ha0) @[elab_as_eliminator] lemma induction_on_irreducible {P : α → Prop} (a : α) (h0 : P 0) (hu : ∀ u : α, is_unit u → P u) (hi : ∀ a i : α, a ≠ 0 → irreducible i → P a → P (i * a)) : P a := by haveI := classical.dec; exact well_founded.fix wf_dvd_monoid.well_founded_dvd_not_unit (λ a ih, if ha0 : a = 0 then ha0.symm ▸ h0 else if hau : is_unit a then hu a hau else let ⟨i, hii, ⟨b, hb⟩⟩ := exists_irreducible_factor hau ha0 in have hb0 : b ≠ 0, from λ hb0, by simp * at *, hb.symm ▸ hi _ _ hb0 hii (ih _ ⟨hb0, i, hii.1, by rw [hb, mul_comm]⟩)) a lemma exists_factors (a : α) : a ≠ 0 → ∃f : multiset α, (∀b ∈ f, irreducible b) ∧ associated f.prod a := wf_dvd_monoid.induction_on_irreducible a (λ h, (h rfl).elim) (λ u hu _, ⟨0, ⟨by simp [hu], associated.symm (by simp [hu, associated_one_iff_is_unit])⟩⟩) (λ a i ha0 hii ih hia0, let ⟨s, hs⟩ := ih ha0 in ⟨i::s, ⟨by clear _let_match; finish, by { rw multiset.prod_cons, exact associated_mul_mul (by refl) hs.2 }⟩⟩) end wf_dvd_monoid theorem wf_dvd_monoid.of_well_founded_associates [comm_cancel_monoid_with_zero α] (h : well_founded ((<) : associates α → associates α → Prop)) : wf_dvd_monoid α := wf_dvd_monoid.of_wf_dvd_monoid_associates ⟨by { convert h, ext, exact associates.dvd_not_unit_iff_lt }⟩ theorem wf_dvd_monoid.iff_well_founded_associates [comm_cancel_monoid_with_zero α] : wf_dvd_monoid α ↔ well_founded ((<) : associates α → associates α → Prop) := ⟨by apply wf_dvd_monoid.well_founded_associates, wf_dvd_monoid.of_well_founded_associates⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- unique factorization monoids. These are defined as `comm_cancel_monoid_with_zero`s with well-founded strict divisibility relations, but this is equivalent to more familiar definitions: Each element (except zero) is uniquely represented as a multiset of irreducible factors. Uniqueness is only up to associated elements. Each element (except zero) is non-uniquely represented as a multiset of prime factors. To define a UFD using the definition in terms of multisets of irreducible factors, use the definition `of_unique_irreducible_factorization` To define a UFD using the definition in terms of multisets of prime factors, use the definition `of_exists_prime_factorization` -/ class unique_factorization_monoid (α : Type*) [comm_cancel_monoid_with_zero α] extends wf_dvd_monoid α : Prop := (irreducible_iff_prime : ∀ {a : α}, irreducible a ↔ prime a) instance ufm_of_gcd_of_wf_dvd_monoid [nontrivial α] [comm_cancel_monoid_with_zero α] [wf_dvd_monoid α] [gcd_monoid α] : unique_factorization_monoid α := { irreducible_iff_prime := λ _, gcd_monoid.irreducible_iff_prime .. ‹wf_dvd_monoid α› } instance associates.ufm [comm_cancel_monoid_with_zero α] [unique_factorization_monoid α] : unique_factorization_monoid (associates α) := { irreducible_iff_prime := by { rw ← associates.irreducible_iff_prime_iff, apply unique_factorization_monoid.irreducible_iff_prime, } .. (wf_dvd_monoid.wf_dvd_monoid_associates : wf_dvd_monoid (associates α)) } end prio namespace unique_factorization_monoid variables [comm_cancel_monoid_with_zero α] [unique_factorization_monoid α] theorem exists_prime_of_factor (a : α) : a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a := by { simp_rw ← unique_factorization_monoid.irreducible_iff_prime, apply wf_dvd_monoid.exists_factors a } @[elab_as_eliminator] lemma induction_on_prime {P : α → Prop} (a : α) (h₁ : P 0) (h₂ : ∀ x : α, is_unit x → P x) (h₃ : ∀ a p : α, a ≠ 0 → prime p → P a → P (p * a)) : P a := begin simp_rw ← unique_factorization_monoid.irreducible_iff_prime at h₃, exact wf_dvd_monoid.induction_on_irreducible a h₁ h₂ h₃, end lemma factors_unique : ∀{f g : multiset α}, (∀x∈f, irreducible x) → (∀x∈g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g := by haveI := classical.dec_eq α; exact λ f, multiset.induction_on f (λ g _ hg h, multiset.rel_zero_left.2 $ multiset.eq_zero_of_forall_not_mem (λ x hx, have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm, (hg x hx).1 (is_unit_iff_dvd_one.2 (dvd.trans (multiset.dvd_prod hx) (is_unit_iff_dvd_one.1 this))))) (λ p f ih g hf hg hfg, let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod (irreducible_iff_prime.1 (hf p (by simp))) (λ q hq, irreducible_iff_prime.1 (hg _ hq)) $ (dvd_iff_dvd_of_rel_right hfg).1 (show p ∣ (p :: f).prod, by simp) in begin rw ← multiset.cons_erase hbg, exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq])) (λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq)) (associated_mul_left_cancel (by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb (hf p (by simp)).ne_zero)) end) end unique_factorization_monoid lemma prime_factors_unique [comm_cancel_monoid_with_zero α] : ∀ {f g : multiset α}, (∀ x ∈ f, prime x) → (∀ x ∈ g, prime x) → f.prod ~ᵤ g.prod → multiset.rel associated f g := by haveI := classical.dec_eq α; exact λ f, multiset.induction_on f (λ g _ hg h, multiset.rel_zero_left.2 $ multiset.eq_zero_of_forall_not_mem (λ x hx, have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm, (irreducible_of_prime $ hg x hx).1 (is_unit_iff_dvd_one.2 (dvd.trans (multiset.dvd_prod hx) (is_unit_iff_dvd_one.1 this))))) (λ p f ih g hf hg hfg, let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod (hf p (by simp)) (λ q hq, hg _ hq) $ (dvd_iff_dvd_of_rel_right hfg).1 (show p ∣ (p :: f).prod, by simp) in begin rw ← multiset.cons_erase hbg, exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq])) (λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq)) (associated_mul_left_cancel (by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb (hf p (by simp)).ne_zero)) end) /-- If an irreducible has a prime factorization, then it is an associate of one of its prime factors. -/ lemma prime_factors_irreducible [comm_cancel_monoid_with_zero α] {a : α} {f : multiset α} (ha : irreducible a) (pfa : (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) : ∃ p, a ~ᵤ p ∧ f = p :: 0 := begin haveI := classical.dec_eq α, refine multiset.induction_on f (λ h, (ha.1 (associated_one_iff_is_unit.1 (associated.symm h))).elim) _ pfa.2 pfa.1, rintros p s _ ⟨u, hu⟩ hs, use p, have hs0 : s = 0, { by_contra hs0, obtain ⟨q, hq⟩ := multiset.exists_mem_of_ne_zero hs0, apply (hs q (by simp [hq])).2.1, refine (ha.2 ((p * ↑u) * (s.erase q).prod) _ _).resolve_left _, { rw [mul_right_comm _ _ q, mul_assoc, ← multiset.prod_cons, multiset.cons_erase hq, ← hu, mul_comm, mul_comm p _, mul_assoc], simp, }, apply mt is_unit_of_mul_is_unit_left (mt is_unit_of_mul_is_unit_left _), apply (hs p (multiset.mem_cons_self _ _)).2.1 }, simp only [mul_one, multiset.prod_cons, multiset.prod_zero, hs0] at *, exact ⟨associated.symm ⟨u, hu⟩, rfl⟩, end section exists_prime_of_factor variables [comm_cancel_monoid_with_zero α] variables (pf : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) include pf lemma wf_dvd_monoid_of_exists_prime_of_factor : wf_dvd_monoid α := ⟨begin classical, apply rel_hom.well_founded (rel_hom.mk _ _) (with_top.well_founded_lt nat.lt_wf), { intro a, by_cases h : a = 0, { exact ⊤ }, exact (classical.some (pf a h)).card }, rintros a b ⟨ane0, ⟨c, hc, b_eq⟩⟩, rw dif_neg ane0, by_cases h : b = 0, { simp [h, lt_top_iff_ne_top] }, rw [dif_neg h, with_top.coe_lt_coe], have cne0 : c ≠ 0, { refine mt (λ con, _) h, rw [b_eq, con, mul_zero] }, calc multiset.card (classical.some (pf a ane0)) < _ + multiset.card (classical.some (pf c cne0)) : lt_add_of_pos_right _ (multiset.card_pos.mpr (λ con, hc (associated_one_iff_is_unit.mp _))) ... = multiset.card (classical.some (pf a ane0) + classical.some (pf c cne0)) : (multiset.card_add _ _).symm ... = multiset.card (classical.some (pf b h)) : multiset.card_eq_card_of_rel (prime_factors_unique _ (classical.some_spec (pf _ h)).1 _), { convert (classical.some_spec (pf c cne0)).2.symm, rw [con, multiset.prod_zero] }, { intros x hadd, rw multiset.mem_add at hadd, cases hadd; apply (classical.some_spec (pf _ _)).1 _ hadd }, { rw multiset.prod_add, transitivity a * c, { apply associated_mul_mul; apply (classical.some_spec (pf _ _)).2 }, { rw ← b_eq, apply (classical.some_spec (pf _ _)).2.symm, } } end⟩ lemma irreducible_iff_prime_of_exists_prime_of_factor {p : α} : irreducible p ↔ prime p := begin by_cases hp0 : p = 0, { simp [hp0] }, refine ⟨λ h, _, irreducible_of_prime⟩, obtain ⟨f, hf⟩ := pf p hp0, obtain ⟨q, hq, rfl⟩ := prime_factors_irreducible h hf, rw prime_iff_of_associated hq, exact hf.1 q (multiset.mem_cons_self _ _) end theorem unique_factorization_monoid_of_exists_prime_of_factor : unique_factorization_monoid α := { irreducible_iff_prime := λ _, irreducible_iff_prime_of_exists_prime_of_factor pf, .. wf_dvd_monoid_of_exists_prime_of_factor pf } end exists_prime_of_factor theorem unique_factorization_monoid_iff_exists_prime_of_factor [comm_cancel_monoid_with_zero α] : unique_factorization_monoid α ↔ (∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) := ⟨λ h, @unique_factorization_monoid.exists_prime_of_factor _ _ h, unique_factorization_monoid_of_exists_prime_of_factor⟩ theorem irreducible_iff_prime_of_exists_unique_irreducible_of_factor [comm_cancel_monoid_with_zero α] (eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a) (uif : ∀ (f g : multiset α), (∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g) (p : α) : irreducible p ↔ prime p := ⟨by letI := classical.dec_eq α; exact λ hpi, ⟨hpi.ne_zero, hpi.1, λ a b ⟨x, hx⟩, if hab0 : a * b = 0 then (eq_zero_or_eq_zero_of_mul_eq_zero hab0).elim (λ ha0, by simp [ha0]) (λ hb0, by simp [hb0]) else have hx0 : x ≠ 0, from λ hx0, by simp * at *, have ha0 : a ≠ 0, from left_ne_zero_of_mul hab0, have hb0 : b ≠ 0, from right_ne_zero_of_mul hab0, begin cases eif x hx0 with fx hfx, cases eif a ha0 with fa hfa, cases eif b hb0 with fb hfb, have h : multiset.rel associated (p :: fx) (fa + fb), { apply uif, { exact λ i hi, (multiset.mem_cons.1 hi).elim (λ hip, hip.symm ▸ hpi) (hfx.1 _), }, { exact λ i hi, (multiset.mem_add.1 hi).elim (hfa.1 _) (hfb.1 _), }, calc multiset.prod (p :: fx) ~ᵤ a * b : by rw [hx, multiset.prod_cons]; exact associated_mul_mul (by refl) hfx.2 ... ~ᵤ (fa).prod * (fb).prod : associated_mul_mul hfa.2.symm hfb.2.symm ... = _ : by rw multiset.prod_add, }, exact let ⟨q, hqf, hq⟩ := multiset.exists_mem_of_rel_of_mem h (multiset.mem_cons_self p _) in (multiset.mem_add.1 hqf).elim (λ hqa, or.inl $ (dvd_iff_dvd_of_rel_left hq).2 $ (dvd_iff_dvd_of_rel_right hfa.2).1 (multiset.dvd_prod hqa)) (λ hqb, or.inr $ (dvd_iff_dvd_of_rel_left hq).2 $ (dvd_iff_dvd_of_rel_right hfb.2).1 (multiset.dvd_prod hqb)) end⟩, irreducible_of_prime⟩ theorem unique_factorization_monoid.of_exists_unique_irreducible_of_factor [comm_cancel_monoid_with_zero α] (eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a) (uif : ∀ (f g : multiset α), (∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g) : unique_factorization_monoid α := unique_factorization_monoid_of_exists_prime_of_factor (by { convert eif, simp_rw irreducible_iff_prime_of_exists_unique_irreducible_of_factor eif uif }) namespace unique_factorization_monoid variables [comm_cancel_monoid_with_zero α] [decidable_eq α] [nontrivial α] [normalization_monoid α] variables [unique_factorization_monoid α] /-- Noncomputably determines the multiset of prime factors. -/ noncomputable def factors (a : α) : multiset α := if h : a = 0 then 0 else multiset.map normalize $ classical.some (unique_factorization_monoid.exists_prime_of_factor a h) theorem factors_prod {a : α} (ane0 : a ≠ 0) : associated (factors a).prod a := begin rw [factors, dif_neg ane0], refine associated.trans _ (classical.some_spec (exists_prime_of_factor a ane0)).2, rw [← associates.mk_eq_mk_iff_associated, ← associates.prod_mk, ← associates.prod_mk, multiset.map_map], congr' 2, ext, rw [function.comp_apply, associates.mk_normalize], end theorem prime_of_factor {a : α} : ∀ (x : α), x ∈ factors a → prime x := begin rw [factors], split_ifs with ane0, { simp }, intros x hx, rcases multiset.mem_map.1 hx with ⟨y, ⟨hy, rfl⟩⟩, rw prime_iff_of_associated (normalize_associated), exact (classical.some_spec (unique_factorization_monoid.exists_prime_of_factor a ane0)).1 y hy, end theorem irreducible_of_factor {a : α} : ∀ (x : α), x ∈ factors a → irreducible x := λ x h, irreducible_of_prime (prime_of_factor x h) theorem normalize_factor {a : α} : ∀ (x : α), x ∈ factors a → normalize x = x := begin rw factors, split_ifs with h, { simp }, intros x hx, obtain ⟨y, hy, rfl⟩ := multiset.mem_map.1 hx, apply normalize_idem end lemma factors_irreducible {a : α} (ha : irreducible a) : factors a = normalize a :: 0 := begin obtain ⟨p, a_assoc, hp⟩ := prime_factors_irreducible ha ⟨prime_of_factor, factors_prod ha.ne_zero⟩, have p_mem : p ∈ factors a, { rw hp, apply multiset.mem_cons_self }, convert hp, rwa [← normalize_factor p p_mem, normalize_eq_normalize_iff, dvd_dvd_iff_associated] end lemma exists_mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a → ∃ q ∈ factors a, p ~ᵤ q := λ ⟨b, hb⟩, have hb0 : b ≠ 0, from λ hb0, by simp * at *, have multiset.rel associated (p :: factors b) (factors a), from factors_unique (λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp) (irreducible_of_factor _)) irreducible_of_factor (associated.symm $ calc multiset.prod (factors a) ~ᵤ a : factors_prod ha0 ... = p * b : hb ... ~ᵤ multiset.prod (p :: factors b) : by rw multiset.prod_cons; exact associated_mul_mul (associated.refl _) (associated.symm (factors_prod hb0))), multiset.exists_mem_of_rel_of_mem this (by simp) end unique_factorization_monoid namespace unique_factorization_monoid variables {R : Type*} [comm_cancel_monoid_with_zero R] [unique_factorization_monoid R] lemma no_factors_of_no_prime_of_factor {a b : R} (ha : a ≠ 0) (h : (∀ {d}, d ∣ a → d ∣ b → ¬ prime d)) : ∀ {d}, d ∣ a → d ∣ b → is_unit d := λ d, induction_on_prime d (by { simp only [zero_dvd_iff], intros, contradiction }) (λ x hx _ _, hx) (λ d q hp hq ih dvd_a dvd_b, absurd hq (h (dvd_of_mul_right_dvd dvd_a) (dvd_of_mul_right_dvd dvd_b))) /-- Euclid's lemma: if `a ∣ b * c` and `a` and `c` have no common prime factors, `a ∣ b`. Compare `is_coprime.dvd_of_dvd_mul_left`. -/ lemma dvd_of_dvd_mul_left_of_no_prime_of_factor {a b c : R} (ha : a ≠ 0) : (∀ {d}, d ∣ a → d ∣ c → ¬ prime d) → a ∣ b * c → a ∣ b := begin refine induction_on_prime c _ _ _, { intro no_factors, simp only [dvd_zero, mul_zero, forall_prop_of_true], haveI := classical.prop_decidable, exact is_unit_iff_forall_dvd.mp (no_factors_of_no_prime_of_factor ha @no_factors (dvd_refl a) (dvd_zero a)) _ }, { rintros _ ⟨x, rfl⟩ _ a_dvd_bx, apply units.dvd_mul_right.mp a_dvd_bx }, { intros c p hc hp ih no_factors a_dvd_bpc, apply ih (λ q dvd_a dvd_c hq, no_factors dvd_a (dvd_mul_of_dvd_right dvd_c _) hq), rw mul_left_comm at a_dvd_bpc, refine or.resolve_left (left_dvd_or_dvd_right_of_dvd_prime_mul hp a_dvd_bpc) (λ h, _), exact no_factors h (dvd_mul_right p c) hp } end /-- Euclid's lemma: if `a ∣ b * c` and `a` and `b` have no common prime factors, `a ∣ c`. Compare `is_coprime.dvd_of_dvd_mul_right`. -/ lemma dvd_of_dvd_mul_right_of_no_prime_of_factor {a b c : R} (ha : a ≠ 0) (no_factors : ∀ {d}, d ∣ a → d ∣ b → ¬ prime d) : a ∣ b * c → a ∣ c := by simpa [mul_comm b c] using dvd_of_dvd_mul_left_of_no_prime_of_factor ha @no_factors /-- If `a ≠ 0, b` are elements of a unique factorization domain, then dividing out their common factor `c'` gives `a'` and `b'` with no factors in common. -/ lemma exists_reduced_factors : ∀ (a ≠ (0 : R)) b, ∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b := begin haveI := classical.prop_decidable, intros a, refine induction_on_prime a _ _ _, { intros, contradiction }, { intros a a_unit a_ne_zero b, use [a, b, 1], split, { intros p p_dvd_a _, exact is_unit_of_dvd_unit p_dvd_a a_unit }, { simp } }, { intros a p a_ne_zero p_prime ih_a pa_ne_zero b, by_cases p ∣ b, { rcases h with ⟨b, rfl⟩, obtain ⟨a', b', c', no_factor, ha', hb'⟩ := ih_a a_ne_zero b, refine ⟨a', b', p * c', @no_factor, _, _⟩, { rw [mul_assoc, ha'] }, { rw [mul_assoc, hb'] } }, { obtain ⟨a', b', c', coprime, rfl, rfl⟩ := ih_a a_ne_zero b, refine ⟨p * a', b', c', _, mul_left_comm _ _ _, rfl⟩, intros q q_dvd_pa' q_dvd_b', cases left_dvd_or_dvd_right_of_dvd_prime_mul p_prime q_dvd_pa' with p_dvd_q q_dvd_a', { have : p ∣ c' * b' := dvd_mul_of_dvd_right (dvd_trans p_dvd_q q_dvd_b') _, contradiction }, exact coprime q_dvd_a' q_dvd_b' } } end lemma exists_reduced_factors' (a b : R) (hb : b ≠ 0) : ∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b := let ⟨b', a', c', no_factor, hb, ha⟩ := exists_reduced_factors b hb a in ⟨a', b', c', λ _ hpb hpa, no_factor hpa hpb, ha, hb⟩ end unique_factorization_monoid namespace associates open unique_factorization_monoid associated multiset variables [comm_cancel_monoid_with_zero α] /-- `factor_set α` representation elements of unique factorization domain as multisets. `multiset α` produced by `factors` are only unique up to associated elements, while the multisets in `factor_set α` are unqiue by equality and restricted to irreducible elements. This gives us a representation of each element as a unique multisets (or the added ⊤ for 0), which has a complete lattice struture. Infimum is the greatest common divisor and supremum is the least common multiple. -/ @[reducible] def {u} factor_set (α : Type u) [comm_cancel_monoid_with_zero α] : Type u := with_top (multiset { a : associates α // irreducible a }) local attribute [instance] associated.setoid theorem factor_set.coe_add {a b : multiset { a : associates α // irreducible a }} : (↑(a + b) : factor_set α) = a + b := by norm_cast lemma factor_set.sup_add_inf_eq_add [decidable_eq (associates α)] : ∀(a b : factor_set α), a ⊔ b + a ⊓ b = a + b | none b := show ⊤ ⊔ b + ⊤ ⊓ b = ⊤ + b, by simp | a none := show a ⊔ ⊤ + a ⊓ ⊤ = a + ⊤, by simp | (some a) (some b) := show (a : factor_set α) ⊔ b + a ⊓ b = a + b, from begin rw [← with_top.coe_sup, ← with_top.coe_inf, ← with_top.coe_add, ← with_top.coe_add, with_top.coe_eq_coe], exact multiset.union_add_inter _ _ end /-- Evaluates the product of a `factor_set` to be the product of the corresponding multiset, or `0` if there is none. -/ def factor_set.prod : factor_set α → associates α | none := 0 | (some s) := (s.map coe).prod @[simp] theorem prod_top : (⊤ : factor_set α).prod = 0 := rfl @[simp] theorem prod_coe {s : multiset { a : associates α // irreducible a }} : (s : factor_set α).prod = (s.map coe).prod := rfl @[simp] theorem prod_add : ∀(a b : factor_set α), (a + b).prod = a.prod * b.prod | none b := show (⊤ + b).prod = (⊤:factor_set α).prod * b.prod, by simp | a none := show (a + ⊤).prod = a.prod * (⊤:factor_set α).prod, by simp | (some a) (some b) := show (↑a + ↑b:factor_set α).prod = (↑a:factor_set α).prod * (↑b:factor_set α).prod, by rw [← factor_set.coe_add, prod_coe, prod_coe, prod_coe, multiset.map_add, multiset.prod_add] theorem prod_mono : ∀{a b : factor_set α}, a ≤ b → a.prod ≤ b.prod | none b h := have b = ⊤, from top_unique h, by rw [this, prod_top]; exact le_refl _ | a none h := show a.prod ≤ (⊤ : factor_set α).prod, by simp; exact le_top | (some a) (some b) h := prod_le_prod $ multiset.map_le_map $ with_top.coe_le_coe.1 $ h /-- `bcount p s` is the multiplicity of `p` in the factor_set `s` (with bundled `p`)-/ def bcount [decidable_eq (associates α)] (p : {a : associates α // irreducible a}) : factor_set α → ℕ | none := 0 | (some s) := s.count p variables [dec_irr : Π (p : associates α), decidable (irreducible p)] include dec_irr /-- `count p s` is the multiplicity of the irreducible `p` in the factor_set `s`. If `p` is not irreducible, `count p s` is defined to be `0`. -/ def count [decidable_eq (associates α)] (p : associates α) : factor_set α → ℕ := if hp : irreducible p then bcount ⟨p, hp⟩ else 0 @[simp] lemma count_some [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) (s : multiset _) : count p (some s) = s.count ⟨p, hp⟩:= by { dunfold count, split_ifs, refl } @[simp] lemma count_zero [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) : count p (0 : factor_set α) = 0 := by { dunfold count, split_ifs, refl } lemma count_reducible [decidable_eq (associates α)] {p : associates α} (hp : ¬ irreducible p) : count p = 0 := dif_neg hp omit dec_irr /-- membership in a factor_set (bundled version) -/ def bfactor_set_mem : {a : associates α // irreducible a} → (factor_set α) → Prop | _ ⊤ := true | p (some l) := p ∈ l include dec_irr /-- `factor_set_mem p s` is the predicate that the irreducible `p` is a member of `s : factor_set α`. If `p` is not irreducible, `p` is not a member of any `factor_set`. -/ def factor_set_mem (p : associates α) (s : factor_set α) : Prop := if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false instance : has_mem (associates α) (factor_set α) := ⟨factor_set_mem⟩ @[simp] lemma factor_set_mem_eq_mem (p : associates α) (s : factor_set α) : factor_set_mem p s = (p ∈ s) := rfl lemma mem_factor_set_top {p : associates α} {hp : irreducible p} : p ∈ (⊤ : factor_set α) := begin dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, exact trivial end lemma mem_factor_set_some {p : associates α} {hp : irreducible p} {l : multiset {a : associates α // irreducible a }} : p ∈ (l : factor_set α) ↔ subtype.mk p hp ∈ l := begin dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, refl end lemma reducible_not_mem_factor_set {p : associates α} (hp : ¬ irreducible p) (s : factor_set α) : ¬ p ∈ s := λ (h : if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false), by rwa [dif_neg hp] at h omit dec_irr variable [unique_factorization_monoid α] theorem unique' {p q : multiset (associates α)} : (∀a∈p, irreducible a) → (∀a∈q, irreducible a) → p.prod = q.prod → p = q := begin apply multiset.induction_on_multiset_quot p, apply multiset.induction_on_multiset_quot q, assume s t hs ht eq, refine multiset.map_mk_eq_map_mk_of_rel (unique_factorization_monoid.factors_unique _ _ _), { exact assume a ha, ((irreducible_mk _).1 $ hs _ $ multiset.mem_map_of_mem _ ha) }, { exact assume a ha, ((irreducible_mk _).1 $ ht _ $ multiset.mem_map_of_mem _ ha) }, simpa [quot_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated] using eq end variables [nontrivial α] [normalization_monoid α] private theorem forall_map_mk_factors_irreducible [decidable_eq α] (x : α) (hx : x ≠ 0) : ∀(a : associates α), a ∈ multiset.map associates.mk (factors x) → irreducible a := begin assume a ha, rcases multiset.mem_map.1 ha with ⟨c, hc, rfl⟩, exact (irreducible_mk c).2 (irreducible_of_factor _ hc) end theorem prod_le_prod_iff_le {p q : multiset (associates α)} (hp : ∀a∈p, irreducible a) (hq : ∀a∈q, irreducible a) : p.prod ≤ q.prod ↔ p ≤ q := iff.intro begin classical, rintros ⟨⟨c⟩, eqc⟩, have : c ≠ 0, from (mt mk_eq_zero.2 $ assume (hc : quot.mk setoid.r c = 0), have (0 : associates α) ∈ q, from prod_eq_zero_iff.1 $ eqc.symm ▸ hc.symm ▸ mul_zero _, not_irreducible_zero ((irreducible_mk 0).1 $ hq _ this)), have : associates.mk (factors c).prod = quot.mk setoid.r c, from mk_eq_mk_iff_associated.2 (factors_prod this), refine multiset.le_iff_exists_add.2 ⟨(factors c).map associates.mk, unique' hq _ _⟩, { assume x hx, rcases multiset.mem_add.1 hx with h | h, exact hp x h, exact forall_map_mk_factors_irreducible c ‹c ≠ 0› _ h }, { simp [multiset.prod_add, prod_mk, *] at * } end prod_le_prod variables [dec : decidable_eq α] [dec' : decidable_eq (associates α)] include dec /-- This returns the multiset of irreducible factors as a `factor_set`, a multiset of irreducible associates `with_top`. -/ noncomputable def factors' (a : α) : multiset { a : associates α // irreducible a } := (factors a).pmap (λa ha, ⟨associates.mk a, (irreducible_mk _).2 ha⟩) (irreducible_of_factor) @[simp] theorem map_subtype_coe_factors' {a : α} : (factors' a).map coe = (factors a).map associates.mk := by simp [factors', multiset.map_pmap, multiset.pmap_eq_map] theorem factors'_cong {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : a ~ᵤ b) : factors' a = factors' b := have multiset.rel associated (factors a) (factors b), from factors_unique irreducible_of_factor irreducible_of_factor ((factors_prod ha).trans $ h.trans $ (factors_prod hb).symm), by simpa [(multiset.map_eq_map subtype.coe_injective).symm, rel_associated_iff_map_eq_map.symm] include dec' /-- This returns the multiset of irreducible factors of an associate as a `factor_set`, a multiset of irreducible associates `with_top`. -/ noncomputable def factors (a : associates α) : factor_set α := begin refine (if h : a = 0 then ⊤ else quotient.hrec_on a (λx h, some $ factors' x) _ h), assume a b hab, apply function.hfunext, { have : a ~ᵤ 0 ↔ b ~ᵤ 0, from iff.intro (assume ha0, hab.symm.trans ha0) (assume hb0, hab.trans hb0), simp only [associated_zero_iff_eq_zero] at this, simp only [quotient_mk_eq_mk, this, mk_eq_zero] }, exact (assume ha hb eq, heq_of_eq $ congr_arg some $ factors'_cong (λ c, ha (mk_eq_zero.2 c)) (λ c, hb (mk_eq_zero.2 c)) hab) end @[simp] theorem factors_0 : (0 : associates α).factors = ⊤ := dif_pos rfl @[simp] theorem factors_mk (a : α) (h : a ≠ 0) : (associates.mk a).factors = factors' a := by { classical, apply dif_neg, apply (mt mk_eq_zero.1 h) } theorem prod_factors : ∀(s : factor_set α), s.prod.factors = s | none := by simp [factor_set.prod]; refl | (some s) := begin unfold factor_set.prod, generalize eq_a : (s.map coe).prod = a, rcases a with ⟨a⟩, rw quot_mk_eq_mk at *, have : (s.map (coe : _ → associates α)).prod ≠ 0, from assume ha, let ⟨⟨a, ha⟩, h, eq⟩ := multiset.mem_map.1 (prod_eq_zero_iff.1 ha) in have irreducible (0 : associates α), from eq ▸ ha, not_irreducible_zero ((irreducible_mk _).1 this), have ha : a ≠ 0, by simp [*] at *, suffices : (unique_factorization_monoid.factors a).map associates.mk = s.map coe, { rw [factors_mk a ha], apply congr_arg some _, simpa [(multiset.map_eq_map subtype.coe_injective).symm] }, refine unique' (forall_map_mk_factors_irreducible _ ha) (assume a ha, let ⟨⟨x, hx⟩, ha, eq⟩ := multiset.mem_map.1 ha in eq ▸ hx) _, rw [prod_mk, eq_a, mk_eq_mk_iff_associated], exact factors_prod ha end @[simp] theorem factors_prod (a : associates α) : a.factors.prod = a := quotient.induction_on a $ assume a, decidable.by_cases (assume : associates.mk a = 0, by simp [quotient_mk_eq_mk, this]) (assume : associates.mk a ≠ 0, have a ≠ 0, by simp * at *, by simp [this, quotient_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated.2 (factors_prod this)]) theorem eq_of_factors_eq_factors {a b : associates α} (h : a.factors = b.factors) : a = b := have a.factors.prod = b.factors.prod, by rw h, by rwa [factors_prod, factors_prod] at this omit dec dec' theorem eq_of_prod_eq_prod {a b : factor_set α} (h : a.prod = b.prod) : a = b := begin classical, have : a.prod.factors = b.prod.factors, by rw h, rwa [prod_factors, prod_factors] at this end include dec dec' @[simp] theorem factors_mul (a b : associates α) : (a * b).factors = a.factors + b.factors := eq_of_prod_eq_prod $ eq_of_factors_eq_factors $ by rw [prod_add, factors_prod, factors_prod, factors_prod] theorem factors_mono : ∀{a b : associates α}, a ≤ b → a.factors ≤ b.factors | s t ⟨d, rfl⟩ := by rw [factors_mul] ; exact le_add_of_nonneg_right bot_le theorem factors_le {a b : associates α} : a.factors ≤ b.factors ↔ a ≤ b := iff.intro (assume h, have a.factors.prod ≤ b.factors.prod, from prod_mono h, by rwa [factors_prod, factors_prod] at this) factors_mono omit dec dec' theorem prod_le {a b : factor_set α} : a.prod ≤ b.prod ↔ a ≤ b := begin classical, exact iff.intro (assume h, have a.prod.factors ≤ b.prod.factors, from factors_mono h, by rwa [prod_factors, prod_factors] at this) prod_mono end include dec dec' noncomputable instance : has_sup (associates α) := ⟨λa b, (a.factors ⊔ b.factors).prod⟩ noncomputable instance : has_inf (associates α) := ⟨λa b, (a.factors ⊓ b.factors).prod⟩ noncomputable instance : bounded_lattice (associates α) := { sup := (⊔), inf := (⊓), sup_le := assume a b c hac hbc, factors_prod c ▸ prod_mono (sup_le (factors_mono hac) (factors_mono hbc)), le_sup_left := assume a b, le_trans (le_of_eq (factors_prod a).symm) $ prod_mono $ le_sup_left, le_sup_right := assume a b, le_trans (le_of_eq (factors_prod b).symm) $ prod_mono $ le_sup_right, le_inf := assume a b c hac hbc, factors_prod a ▸ prod_mono (le_inf (factors_mono hac) (factors_mono hbc)), inf_le_left := assume a b, le_trans (prod_mono inf_le_left) (le_of_eq (factors_prod a)), inf_le_right := assume a b, le_trans (prod_mono inf_le_right) (le_of_eq (factors_prod b)), .. associates.partial_order, .. associates.order_top, .. associates.order_bot } lemma sup_mul_inf (a b : associates α) : (a ⊔ b) * (a ⊓ b) = a * b := show (a.factors ⊔ b.factors).prod * (a.factors ⊓ b.factors).prod = a * b, begin refine eq_of_factors_eq_factors _, rw [← prod_add, prod_factors, factors_mul, factor_set.sup_add_inf_eq_add] end include dec_irr lemma dvd_of_mem_factors {a p : associates α} {hp : irreducible p} (hm : p ∈ factors a) : p ∣ a := begin by_cases ha0 : a = 0, { rw ha0, exact dvd_zero p }, obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha0, rw [← associates.factors_prod a], rw [← ha', factors_mk a0 nza] at hm ⊢, erw prod_coe, apply multiset.dvd_prod, apply multiset.mem_map.mpr, exact ⟨⟨p, hp⟩, mem_factor_set_some.mp hm, rfl⟩ end omit dec' lemma dvd_of_mem_factors' {a : α} {p : associates α} {hp : irreducible p} {hz : a ≠ 0} (h_mem : subtype.mk p hp ∈ factors' a) : p ∣ associates.mk a := by { haveI := classical.dec_eq (associates α), apply @dvd_of_mem_factors _ _ _ _ _ _ _ _ _ _ hp, rw factors_mk _ hz, apply mem_factor_set_some.2 h_mem } omit dec_irr lemma mem_factors'_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) : subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a := begin obtain ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd ha0 hp hd, apply multiset.mem_pmap.mpr, use q, use hq, exact subtype.eq (eq.symm (mk_eq_mk_iff_associated.mpr hpq)) end include dec_irr lemma mem_factors'_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a ↔ p ∣ a := begin split, { rw ← mk_dvd_mk, apply dvd_of_mem_factors', apply ha0 }, { apply mem_factors'_of_dvd ha0 } end include dec' lemma mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) : (associates.mk p) ∈ factors (associates.mk a) := begin rw factors_mk _ ha0, exact mem_factor_set_some.mpr (mem_factors'_of_dvd ha0 hp hd) end lemma mem_factors_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : (associates.mk p) ∈ factors (associates.mk a) ↔ p ∣ a := begin split, { rw ← mk_dvd_mk, apply dvd_of_mem_factors, exact (irreducible_mk p).mpr hp }, { apply mem_factors_of_dvd ha0 hp } end lemma exists_prime_dvd_of_not_inf_one {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : (associates.mk a) ⊓ (associates.mk b) ≠ 1) : ∃ (p : α), prime p ∧ p ∣ a ∧ p ∣ b := begin have hz : (factors (associates.mk a)) ⊓ (factors (associates.mk b)) ≠ 0, { contrapose! h with hf, change ((factors (associates.mk a)) ⊓ (factors (associates.mk b))).prod = 1, rw hf, exact multiset.prod_zero }, rw [factors_mk a ha, factors_mk b hb, ← with_top.coe_inf] at hz, obtain ⟨⟨p0, p0_irr⟩, p0_mem⟩ := multiset.exists_mem_of_ne_zero ((mt with_top.coe_eq_coe.mpr) hz), rw multiset.inf_eq_inter at p0_mem, obtain ⟨p, rfl⟩ : ∃ p, associates.mk p = p0 := quot.exists_rep p0, refine ⟨p, _, _, _⟩, { rw [← irreducible_iff_prime, ← irreducible_mk], exact p0_irr }, { apply dvd_of_mk_le_mk, apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).left, apply ha, }, { apply dvd_of_mk_le_mk, apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).right, apply hb } end theorem coprime_iff_inf_one {a b : α} (ha0 : a ≠ 0) (hb0 : b ≠ 0) : (associates.mk a) ⊓ (associates.mk b) = 1 ↔ ∀ {d : α}, d ∣ a → d ∣ b → ¬ prime d := begin split, { intros hg p ha hb hp, refine ((associates.prime_mk _).mpr hp).not_unit (is_unit_of_dvd_one _ _), rw ← hg, exact le_inf (mk_le_mk_of_dvd ha) (mk_le_mk_of_dvd hb) }, { contrapose, intros hg hc, obtain ⟨p, hp, hpa, hpb⟩ := exists_prime_dvd_of_not_inf_one ha0 hb0 hg, exact hc hpa hpb hp } end omit dec_irr theorem factors_prime_pow {p : associates α} (hp : irreducible p) (k : ℕ) : factors (p ^ k) = some (multiset.repeat ⟨p, hp⟩ k) := eq_of_prod_eq_prod (by rw [associates.factors_prod, factor_set.prod, multiset.map_repeat, multiset.prod_repeat, subtype.coe_mk]) include dec_irr theorem prime_pow_dvd_iff_le {m p : associates α} (h₁ : m ≠ 0) (h₂ : irreducible p) {k : ℕ} : p ^ k ≤ m ↔ k ≤ count p m.factors := begin obtain ⟨a, nz, rfl⟩ := associates.exists_non_zero_rep h₁, rw [factors_mk _ nz, ← with_top.some_eq_coe, count_some, multiset.le_count_iff_repeat_le, ← factors_le, factors_prime_pow h₂, factors_mk _ nz], exact with_top.coe_le_coe end theorem le_of_count_ne_zero {m p : associates α} (h0 : m ≠ 0) (hp : irreducible p) : count p m.factors ≠ 0 → p ≤ m := begin rw [← nat.pos_iff_ne_zero], intro h, rw [← pow_one p], apply (prime_pow_dvd_iff_le h0 hp).2, simpa only end theorem count_mul {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) : count p (factors (a * b)) = count p a.factors + count p b.factors := begin obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha, obtain ⟨b0, nzb, hb'⟩ := exists_non_zero_rep hb, rw [factors_mul, ← ha', ← hb', factors_mk a0 nza, factors_mk b0 nzb, ← factor_set.coe_add, ← with_top.some_eq_coe, ← with_top.some_eq_coe, ← with_top.some_eq_coe, count_some hp, multiset.count_add, count_some hp, count_some hp] end theorem count_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {p : associates α} (hp : irreducible p) : count p a.factors = 0 ∨ count p b.factors = 0 := begin rw [or_iff_not_imp_left, ← ne.def], intro hca, contrapose! hab with hcb, exact ⟨p, le_of_count_ne_zero ha hp hca, le_of_count_ne_zero hb hp hcb, (irreducible_iff_prime.mp hp)⟩, end theorem count_mul_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) : count p a.factors = 0 ∨ count p a.factors = count p (a * b).factors := begin cases count_of_coprime ha hb hab hp with hz hb0, { tauto }, apply or.intro_right, rw [count_mul ha hb hp, hb0, add_zero] end theorem count_mul_of_coprime' {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) : count p (a * b).factors = count p a.factors ∨ count p (a * b).factors = count p b.factors := begin rw [count_mul ha hb hp], cases count_of_coprime ha hb hab hp with ha0 hb0, { apply or.intro_right, rw [ha0, zero_add] }, { apply or.intro_left, rw [hb0, add_zero] } end theorem dvd_count_of_dvd_count_mul {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (habk : k ∣ count p (a * b).factors) : k ∣ count p a.factors := begin cases count_of_coprime ha hb hab hp with hz h, { rw hz, exact dvd_zero k }, { rw [count_mul ha hb hp, h] at habk, exact habk } end omit dec_irr @[simp] lemma factors_one : factors (1 : associates α) = 0 := begin apply eq_of_prod_eq_prod, rw associates.factors_prod, exact multiset.prod_zero, end @[simp] theorem pow_factors {a : associates α} {k : ℕ} : (a ^ k).factors = k •ℕ a.factors := begin induction k with n h, { rw [zero_nsmul, pow_zero], exact factors_one }, { rw [pow_succ, succ_nsmul, factors_mul, h] } end include dec_irr lemma count_pow {a : associates α} (ha : a ≠ 0) {p : associates α} (hp : irreducible p) (k : ℕ) : count p (a ^ k).factors = k * count p a.factors := begin induction k with n h, { rw [pow_zero, factors_one, zero_mul, count_zero hp] }, { rw [pow_succ, count_mul ha (pow_ne_zero' _ ha) hp, h, nat.succ_eq_add_one], ring } end theorem dvd_count_pow {a : associates α} (ha : a ≠ 0) {p : associates α} (hp : irreducible p) (k : ℕ) : k ∣ count p (a ^ k).factors := by { rw count_pow ha hp, apply dvd_mul_right } theorem is_pow_of_dvd_count {a : associates α} (ha : a ≠ 0) {k : ℕ} (hk : ∀ (p : associates α) (hp : irreducible p), k ∣ count p a.factors) : ∃ (b : associates α), a = b ^ k := begin obtain ⟨a0, hz, rfl⟩ := exists_non_zero_rep ha, rw [factors_mk a0 hz] at hk, have hk' : ∀ (p : {a : associates α // irreducible a}), k ∣ (factors' a0).count p, { intro p, have pp : p = ⟨p.val, p.2⟩, { simp only [subtype.coe_eta, subtype.val_eq_coe] }, rw [pp, ← count_some p.2], exact hk p.val p.2 }, obtain ⟨u, hu⟩ := multiset.exists_smul_of_dvd_count _ hk', use (u : factor_set α).prod, apply eq_of_factors_eq_factors, rw [pow_factors, prod_factors, factors_mk a0 hz, ← with_top.some_eq_coe, hu], exact with_bot.coe_nsmul u k end omit dec omit dec_irr omit dec' theorem eq_pow_of_mul_eq_pow {a b c : associates α} (ha : a ≠ 0) (hb : b ≠ 0) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (h : a * b = c ^ k) : ∃ (d : associates α), a = d ^ k := begin classical, by_cases hk0 : k = 0, { use 1, rw [hk0, pow_zero] at h ⊢, apply (mul_eq_one_iff.1 h).1 }, { refine is_pow_of_dvd_count ha _, intros p hp, apply dvd_count_of_dvd_count_mul ha hb hp hab, rw h, apply dvd_count_pow _ hp, rintros rfl, rw zero_pow' _ hk0 at h, cases mul_eq_zero.mp h; contradiction } end end associates section open associates unique_factorization_monoid /-- `to_gcd_monoid` constructs a GCD monoid out of a normalization on a unique factorization domain. -/ noncomputable def unique_factorization_monoid.to_gcd_monoid (α : Type*) [integral_domain α] [unique_factorization_monoid α] [normalization_monoid α] [decidable_eq (associates α)] [decidable_eq α] : gcd_monoid α := { gcd := λa b, (associates.mk a ⊓ associates.mk b).out, lcm := λa b, (associates.mk a ⊔ associates.mk b).out, gcd_dvd_left := assume a b, (out_dvd_iff a (associates.mk a ⊓ associates.mk b)).2 $ inf_le_left, gcd_dvd_right := assume a b, (out_dvd_iff b (associates.mk a ⊓ associates.mk b)).2 $ inf_le_right, dvd_gcd := assume a b c hac hab, show a ∣ (associates.mk c ⊓ associates.mk b).out, by rw [dvd_out_iff, le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff]; exact ⟨hac, hab⟩, lcm_zero_left := assume a, show (⊤ ⊔ associates.mk a).out = 0, by simp, lcm_zero_right := assume a, show (associates.mk a ⊔ ⊤).out = 0, by simp, gcd_mul_lcm := assume a b, show (associates.mk a ⊓ associates.mk b).out * (associates.mk a ⊔ associates.mk b).out = normalize (a * b), by rw [← out_mk, ← out_mul, mul_comm, sup_mul_inf]; refl, normalize_gcd := assume a b, by convert normalize_out _, .. ‹normalization_monoid α› } end
9d91ca52880227fc78b4bdb1b4dc4765a211990d
23c79f5a2b724d0a6865a697f74bc337c40a1c17
/verified-counting/src/zero_knowledge_proof.lean
94f235197bb6cae07110eb5c9fcc70885e7c2ac5
[]
no_license
mukeshtiwari/Puzzles
145e679e637d1c36363c90863cba7c0fda0190b1
4a760e7dc6c65cab0fde86d0296d863c62fdda29
refs/heads/master
1,608,024,738,873
1,607,504,305,000
1,607,504,305,000
5,691,003
0
0
null
null
null
null
UTF-8
Lean
false
false
2,948
lean
import data.zmod.basic data.nat.prime number_theory.quadratic_reciprocity tactic.find tactic.omega list_lemma elgamal_encryption namespace nzkp variables (p q k : ℕ) (g h : zmod p) (Hk : 2 ≤ k) (Hp : fact (nat.prime p)) (Hq : fact (nat.prime q)) (Hdiv : p = q * k + 1) (H₁ : h ≠ 0) (H₂ : h^k ≠ 1) (H₃ : g = h^k) variables hash : list (zmod p) -> zmod p inductive communication : Type* | commitment : Π (t : zmod q), communication | challenge : Π (t c : zmod q), communication | response : Π (t c s : zmod q), communication open communication /- Add more data to hash function https://tools.ietf.org/html/rfc8235 -/ inductive zkp_transcript (w x : zmod q) (h : zmod p) (Hf : h = g^x.val) : communication p -> Type* | commitment_step (t : zmod q) : t = g^w.val -> zkp_transcript (commitment t) | challenge_step (t c : zmod q) : c = hash [g, h, t] -> zkp_transcript (commitment t) -> zkp_transcript (challenge t c) | response_step (t c s : zmod q) : s = w + c * x -> zkp_transcript (challenge t c) -> zkp_transcript (response t c s) open zkp_transcript /- We can construct a sigma protocol (discrete logarithm). we can always construct a valid certificate. Moreover, we will prove this formally that this function always constructs a valid certificate which checks out -/ def construct_nzkp_certificate (w x : zmod q) (h : zmod p) (H : h = g^x.val) : Σ (t c s : zmod q), zkp_transcript p q g hash w x h H (response t c s) := let t : zmod q := g^w.val in let c : zmod q := hash [g, h, t] in let s : zmod q := w + c * x in ⟨t, c, s, response_step t c s rfl (challenge_step t c rfl (commitment_step t rfl)) ⟩ /- a given certificate is a valid one -/ def accept_nzkp_certificate (w x : zmod q) (h : zmod p) (H : h = g^x.val) (t c s : zmod q) (Hnzkp : zkp_transcript p q g hash w x h H (response t c s)) := g^s.val = t * h^c.val /- invalid transcript -/ def reject_nzkp_certificate (w x : zmod q) (h : zmod p) (H : h = g^x.val) (t c s : zmod q) (Hnzkp : zkp_transcript p q g hash w x h H (response t c s)) := g^s.val ≠ t * h^c.val /- Proof that the construct_nzkp_certificate function always constructs a valid certificate. Each valid certificate always checks out. Completeness -/ theorem proof_of_completeness : ∀ (w x : zmod q) (h : zmod p) (H₀ : h = g^x.val) cert (H₁ : cert = construct_nzkp_certificate p q g hash w x h H₀), accept_nzkp_certificate p q g hash w x h H₀ cert.1 cert.2.1 cert.2.2.1 cert.2.2.2 = true := begin sorry end /- If you give me two valid ceritificate for same randomness, then I can extract a witness x : Soundenss -/ lemma proof_of_soundness : ∀ (w x : zmod q) (h : zmod p) (H₀ : h = g^x.val) cert₁ cert₂ (H₁ : cert₁ = construct_nzkp_certificate p q g hash w x h H₀) (H₂ : cert₂ = construct_nzkp_certificate p q g hash w x h H₀), /- -/ end nzkp
72602768f7d5d870b3930d32ae76817c4c8c91a2
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/tactic/aesop/util.lean
646045d60a761322f5acd038a0e415f9872d27d0
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,684
lean
/- Copyright (c) 2021 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg -/ import tactic.core namespace list def find_and_remove {α} (p : α → Prop) [decidable_pred p] : list α → option (α × list α) | [] := none | (a :: as) := if p a then some (a, as) else do (x, as) ← find_and_remove as, pure (x, a :: as) def minimum_by₁ {α} (lt : α → α → Prop) [decidable_rel lt] (a : α) (as : list α) : α := as.foldl (λ a a', if lt a' a then a' else a) a def minimum_by {α} (lt : α → α → Prop) [decidable_rel lt] : list α → option α | [] := none | (a :: as) := some $ minimum_by₁ lt a as def minimum' {α} [has_lt α] [decidable_rel ((<) : α → α → Prop)] : list α → option α := minimum_by (<) def minimum₁ {α} [has_lt α] [decidable_rel ((<) : α → α → Prop)] : α → list α → α := minimum_by₁ (<) end list namespace native namespace rb_lmap meta def insert_list {α β} (m : rb_lmap α β) (a : α) (bs : list β) : rb_lmap α β := match rb_map.find m a with | none := rb_map.insert m a bs | some bs' := rb_map.insert m a (bs' ++ bs) end meta def join {α β} (m₁ m₂ : rb_lmap α β) : rb_lmap α β := m₂.fold m₁ $ λ a bs m, m.insert_list a bs meta instance {α β} : has_append (rb_lmap α β) := ⟨join⟩ end rb_lmap end native namespace format open tactic meta def unlines : list format → format := intercalate line meta def nested (n : ℕ) (f : format) : format := if f.is_nil then nil else nest n $ format.line ++ f meta def of_goal (e : expr) : tactic format := if e.is_mvar then with_local_goals' [e] $ read >>= pp else pp e end format namespace expr meta def format_goal : expr → tactic format := format.of_goal meta def set_pretty_name {elab} (n : name) : expr elab → expr elab | (mvar unique _ type) := mvar unique n type | (local_const unique _ bi type) := local_const unique n bi type | (lam _ bi type body) := lam n bi type body | (pi _ bi type body) := pi n bi type body | (elet _ type assignment body) := elet n type assignment body | e := e -- TODO open_pis is unnecessarily slow here meta def conclusion_head_constant (e : expr) : tactic (option name) := do (_, conclusion) ← tactic.open_pis e, let f := conclusion.get_app_fn, pure $ if f.is_constant then some f.const_name else none end expr namespace lean namespace parser meta def small_int : parser ℤ := do negate ← succeeds $ tk "-", n ← small_nat, if negate then if n = 0 then pure 0 else pure $ int.neg_succ_of_nat (n - 1) else pure n end parser end lean
8b86a1f3a6c0275ab2f124060e5e4c58f45f610e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/group_with_zero/semiconj.lean
cfc9340f2c34921da76d290bf63721ead5aabcbe
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,002
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.group_with_zero.units.basic import algebra.group.semiconj /-! # Lemmas about semiconjugate elements in a `group_with_zero`. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/757 > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α M₀ G₀ M₀' G₀' F F' : Type*} namespace semiconj_by @[simp] lemma zero_right [mul_zero_class G₀] (a : G₀) : semiconj_by a 0 0 := by simp only [semiconj_by, mul_zero, zero_mul] @[simp] lemma zero_left [mul_zero_class G₀] (x y : G₀) : semiconj_by 0 x y := by simp only [semiconj_by, mul_zero, zero_mul] variables [group_with_zero G₀] {a x y x' y' : G₀} @[simp] lemma inv_symm_left_iff₀ : semiconj_by a⁻¹ x y ↔ semiconj_by a y x := classical.by_cases (λ ha : a = 0, by simp only [ha, inv_zero, semiconj_by.zero_left]) (λ ha, @units_inv_symm_left_iff _ _ (units.mk0 a ha) _ _) lemma inv_symm_left₀ (h : semiconj_by a x y) : semiconj_by a⁻¹ y x := semiconj_by.inv_symm_left_iff₀.2 h lemma inv_right₀ (h : semiconj_by a x y) : semiconj_by a x⁻¹ y⁻¹ := begin by_cases ha : a = 0, { simp only [ha, zero_left] }, by_cases hx : x = 0, { subst x, simp only [semiconj_by, mul_zero, @eq_comm _ _ (y * a), mul_eq_zero] at h, simp [h.resolve_right ha] }, { have := mul_ne_zero ha hx, rw [h.eq, mul_ne_zero_iff] at this, exact @units_inv_right _ _ _ (units.mk0 x hx) (units.mk0 y this.1) h }, end @[simp] lemma inv_right_iff₀ : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y := ⟨λ h, inv_inv x ▸ inv_inv y ▸ h.inv_right₀, inv_right₀⟩ lemma div_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x / x') (y / y') := by { rw [div_eq_mul_inv, div_eq_mul_inv], exact h.mul_right h'.inv_right₀ } end semiconj_by
f220a87888b718c0ca669fc0a14b730d11334fe1
7cef822f3b952965621309e88eadf618da0c8ae9
/test/monotonicity.lean
94ac538f80d5f67b0a3cc1b0f041746fd00c56eb
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
8,236
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import tactic.monotonicity tactic.norm_num import algebra.ordered_ring import data.list.basic open list tactic tactic.interactive example (h : 3 + 6 ≤ 4 + 5) : 1 + 3 + 2 + 6 ≤ 4 + 2 + 1 + 5 := begin ac_mono, end example (h : 3 ≤ (4 : ℤ)) (h' : 5 ≤ (6 : ℤ)) : (1 + 3 + 2) - 6 ≤ (4 + 2 + 1 : ℤ) - 5 := by ac_mono example (h : 3 ≤ (4 : ℤ)) (h' : 5 ≤ (6 : ℤ)) : (1 + 3 + 2) - 6 ≤ (4 + 2 + 1 : ℤ) - 5 := begin transitivity (1 + 3 + 2 - 5 : ℤ), { ac_mono }, { ac_mono }, end example (x y z k : ℤ) (h : 3 ≤ (4 : ℤ)) (h' : z ≤ y) : (k + 3 + x) - y ≤ (k + 4 + x) - z := begin mono, norm_num end example (x y z a b : ℤ) (h : a ≤ (b : ℤ)) (h' : z ≤ y) : (1 + a + x) - y ≤ (1 + b + x) - z := begin transitivity (1 + a + x - z), { mono, }, { mono, mono, mono }, end example (x y z : ℤ) (h' : z ≤ y) : (1 + 3 + x) - y ≤ (1 + 4 + x) - z := begin transitivity (1 + 3 + x - z), { mono }, { mono, mono, norm_num }, end example (x y z : ℤ) (h : 3 ≤ (4 : ℤ)) (h' : z ≤ y) : (1 + 3 + x) - y ≤ (1 + 4 + x) - z := begin ac_mono, mono* end @[simp] def list.le' {α : Type*} [has_le α] : list α → list α → Prop | (x::xs) (y::ys) := x ≤ y ∧ list.le' xs ys | [] [] := true | _ _ := false @[simp] instance list_has_le {α : Type*} [has_le α] : has_le (list α) := ⟨ list.le' ⟩ lemma list.le_refl {α : Type*} [preorder α] {xs : list α} : xs ≤ xs := begin induction xs with x xs, { trivial }, { simp [has_le.le,list.le], split, apply le_refl, apply xs_ih } end -- @[trans] lemma list.le_trans {α : Type*} [preorder α] {xs zs : list α} (ys : list α) (h : xs ≤ ys) (h' : ys ≤ zs) : xs ≤ zs := begin revert ys zs, induction xs with x xs ; intros ys zs h h' ; cases ys with y ys ; cases zs with z zs ; try { cases h ; cases h' ; done }, { apply list.le_refl }, { simp [has_le.le,list.le], split, apply le_trans h.left h'.left, apply xs_ih _ h.right h'.right, } end @[mono] lemma list_le_mono_left {α : Type*} [preorder α] {xs ys zs : list α} (h : xs ≤ ys) : xs ++ zs ≤ ys ++ zs := begin revert ys, induction xs with x xs ; intros ys h, { cases ys, apply list.le_refl, cases h }, { cases ys with y ys, cases h, simp [has_le.le,list.le] at *, revert h, apply and.imp_right, apply xs_ih } end @[mono] lemma list_le_mono_right {α : Type*} [preorder α] {xs ys zs : list α} (h : xs ≤ ys) : zs ++ xs ≤ zs ++ ys := begin revert ys zs, induction xs with x xs ; intros ys zs h, { cases ys, { simp, apply list.le_refl }, cases h }, { cases ys with y ys, cases h, simp [has_le.le,list.le] at *, suffices : list.le' ((zs ++ [x]) ++ xs) ((zs ++ [y]) ++ ys), { refine cast _ this, simp, }, apply list.le_trans (zs ++ [y] ++ xs), { apply list_le_mono_left, induction zs with z zs, { simp [has_le.le,list.le], apply h.left }, { simp [has_le.le,list.le], split, apply le_refl, apply zs_ih, } }, { apply xs_ih h.right, } } end lemma bar_bar' (h : [] ++ [3] ++ [2] ≤ [1] ++ [5] ++ [4]) : [] ++ [3] ++ [2] ++ [2] ≤ [1] ++ [5] ++ ([4] ++ [2]) := begin ac_mono, end lemma bar_bar'' (h : [3] ++ [2] ++ [2] ≤ [5] ++ [4] ++ []) : [1] ++ ([3] ++ [2]) ++ [2] ≤ [1] ++ [5] ++ ([4] ++ []) := begin ac_mono, end lemma bar_bar (h : [3] ++ [2] ≤ [5] ++ [4]) : [1] ++ [3] ++ [2] ++ [2] ≤ [1] ++ [5] ++ ([4] ++ [2]) := begin ac_mono, end def P (x : ℕ) := 7 ≤ x def Q (x : ℕ) := x ≤ 7 @[mono] lemma P_mono {x y : ℕ} (h : x ≤ y) : P x → P y := by { intro h', apply le_trans h' h } @[mono] lemma Q_mono {x y : ℕ} (h : y ≤ x) : Q x → Q y := by apply le_trans h example (x y z : ℕ) (h : x ≤ y) : P (x + z) → P (z + y) := begin ac_mono, ac_mono, end example (x y z : ℕ) (h : y ≤ x) : Q (x + z) → Q (z + y) := begin ac_mono, ac_mono, end example (x y z k m n : ℤ) (h₀ : z ≤ 0) (h₁ : y ≤ x) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono, ac_mono, ac_mono, end example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : x ≤ y) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono, ac_mono, ac_mono, end example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : x ≤ y) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono, -- ⊢ (m + x + n) * z ≤ z * (y + n + m) ac_mono, -- ⊢ m + x + n ≤ y + n + m ac_mono, end example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : x ≤ y) : (m + x + n) * z + k ≤ z * (y + n + m) + k := by { ac_mono* := h₁ } example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : m + x + n ≤ y + n + m) : (m + x + n) * z + k ≤ z * (y + n + m) + k := by { ac_mono* := h₁ } example (x y z k m n : ℕ) (h₀ : z ≥ 0) (h₁ : n + x + m ≤ y + n + m) : (m + x + n) * z + k ≤ z * (y + n + m) + k := begin ac_mono* : m + x + n ≤ y + n + m, transitivity ; [ skip , apply h₁ ], apply le_of_eq, ac_refl, end example (x y z k m n : ℤ) (h₁ : x ≤ y) : true := begin have : (m + x + n) * z + k ≤ z * (y + n + m) + k, { ac_mono, success_if_fail { ac_mono }, admit }, trivial end example (x y z k m n : ℕ) (h₁ : x ≤ y) : true := begin have : (m + x + n) * z + k ≤ z * (y + n + m) + k, { ac_mono*, change 0 ≤ z, apply nat.zero_le, }, trivial end example (x y z k m n : ℕ) (h₁ : x ≤ y) : true := begin have : (m + x + n) * z + k ≤ z * (y + n + m) + k, { ac_mono, change (m + x + n) * z ≤ z * (y + n + m), admit }, trivial, end example (x y z k m n i j : ℕ) (h₁ : x + i = y + j) : (m + x + n + i) * z + k = z * (j + n + m + y) + k := begin ac_mono^3, simp [h₁], end example (x y z k m n i j : ℕ) (h₁ : x + i = y + j) : z * (x + i + n + m) + k = z * (y + j + n + m) + k := begin congr, simp [h₁], end example (x y z k m n i j : ℕ) (h₁ : x + i = y + j) : (m + x + n + i) * z + k = z * (j + n + m + y) + k := begin ac_mono*, simp [h₁], end example (x y : ℕ) (h : x ≤ y) : true := begin (do v ← mk_mvar, p ← to_expr ```(%%v + x ≤ y + %%v), assert `h' p), ac_mono := h, trivial, exact 1, end example {x y z : ℕ} : true := begin have : y + x ≤ y + z, { mono, guard_target' x ≤ z, admit }, trivial end example {x y z : ℕ} : true := begin suffices : x + y ≤ z + y, trivial, mono, guard_target' x ≤ z, admit, end example {x y z w : ℕ} : true := begin have : x + y ≤ z + w, { mono, guard_target' x ≤ z, admit, guard_target' y ≤ w, admit }, trivial end example {x y z w : ℕ} : true := begin have : x * y ≤ z * w, { mono with [0 ≤ z,0 ≤ y], { guard_target 0 ≤ z, admit }, { guard_target 0 ≤ y, admit }, guard_target' x ≤ z, admit, guard_target' y ≤ w, admit }, trivial end example {x y z w : Prop} : true := begin have : x ∧ y → z ∧ w, { mono, guard_target' x → z, admit, guard_target' y → w, admit }, trivial end example {x y z w : Prop} : true := begin have : x ∨ y → z ∨ w, { mono, guard_target' x → z, admit, guard_target' y → w, admit }, trivial end example {x y z w : ℤ} : true := begin suffices : x + y < w + z, trivial, have : x < w, admit, have : y ≤ z, admit, mono right, end example {x y z w : ℤ} : true := begin suffices : x * y < w * z, trivial, have : x < w, admit, have : y ≤ z, admit, mono right, { guard_target' 0 < y, admit }, { guard_target' 0 ≤ w, admit }, end open tactic example (x y : ℕ) (h : x ≤ y) : true := begin (do v ← mk_mvar, p ← to_expr ```(%%v + x ≤ y + %%v), assert `h' p), ac_mono := h, trivial, exact 3 end example {α} [decidable_linear_order α] (a b c d e : α) : max a b ≤ e → b ≤ e := by { mono, apply le_max_right } example (a b c d e : Prop) (h : d → a) (h' : c → e) : (a ∧ b → c) ∨ d → (d ∧ b → e) ∨ a := begin mono, mono, mono, end
013bb18a219499d16797a50f7a7de6ba8d5c20ca
6b10c15e653d49d146378acda9f3692e9b5b1950
/examples/basic_skills/unnamed_850.lean
549dd22afbb0be9a9ec41fd45dc76ad1cc4c7447
[]
no_license
gebner/mathematics_in_lean
3cf7f18767208ea6c3307ec3a67c7ac266d8514d
6d1462bba46d66a9b948fc1aef2714fd265cde0b
refs/heads/master
1,655,301,945,565
1,588,697,505,000
1,588,697,505,000
261,523,603
0
0
null
1,588,695,611,000
1,588,695,610,000
null
UTF-8
Lean
false
false
182
lean
variables (G : Type*) [group G] #check (mul_assoc : ∀ a b c : G, a * b * c = a * (b * c)) #check (one_mul : ∀ a : G, 1 * a = a) #check (mul_left_inv : ∀ a : G, a⁻¹ * a = 1)
8eea49776ab8a15a25694054d0ba32eb3331a5bd
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/category_theory/limits/shapes/equalizers.lean
8bcdcd0f8140aa52d6e809a25b100422996445cb
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
7,091
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.fintype import category_theory.limits.limits open category_theory namespace category_theory.limits local attribute [tidy] tactic.case_bash universes v u /-- The type of objects for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq] inductive walking_parallel_pair : Type v | zero | one instance fintype_walking_parallel_pair : fintype walking_parallel_pair := { elems := [walking_parallel_pair.zero, walking_parallel_pair.one].to_finset, complete := λ x, by { cases x; simp } } open walking_parallel_pair /-- The type family of morphisms for the diagram indexing a (co)equalizer. -/ inductive walking_parallel_pair_hom : walking_parallel_pair → walking_parallel_pair → Type v | left : walking_parallel_pair_hom zero one | right : walking_parallel_pair_hom zero one | id : Π X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X open walking_parallel_pair_hom def walking_parallel_pair_hom.comp : Π (X Y Z : walking_parallel_pair) (f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z), walking_parallel_pair_hom X Z | _ _ _ (id _) h := h | _ _ _ left (id one) := left | _ _ _ right (id one) := right . instance walking_parallel_pair_hom_category : small_category.{v} walking_parallel_pair := { hom := walking_parallel_pair_hom, id := walking_parallel_pair_hom.id, comp := walking_parallel_pair_hom.comp } lemma walking_parallel_pair_hom_id (X : walking_parallel_pair.{v}) : walking_parallel_pair_hom.id X = 𝟙 X := rfl variables {C : Type u} [𝒞 : category.{v+1} C] include 𝒞 variables {X Y : C} def parallel_pair (f g : X ⟶ Y) : walking_parallel_pair.{v} ⥤ C := { obj := λ x, match x with | zero := X | one := Y end, map := λ x y h, match x, y, h with | _, _, (id _) := 𝟙 _ | _, _, left := f | _, _, right := g end }. @[simp] lemma parallel_pair_map_left (f g : X ⟶ Y) : (parallel_pair f g).map left = f := rfl @[simp] lemma parallel_pair_map_right (f g : X ⟶ Y) : (parallel_pair f g).map right = g := rfl @[simp] lemma parallel_pair_functor_obj {F : walking_parallel_pair.{v} ⥤ C} (j : walking_parallel_pair.{v}) : (parallel_pair (F.map left) (F.map right)).obj j = F.obj j := begin cases j; refl end abbreviation fork (f g : X ⟶ Y) := cone (parallel_pair f g) abbreviation cofork (f g : X ⟶ Y) := cocone (parallel_pair f g) variables {f g : X ⟶ Y} attribute [simp] walking_parallel_pair_hom_id def fork.of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork f g := { X := P, π := { app := λ X, begin cases X, exact ι, exact ι ≫ f, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, exact w end }} def cofork.of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork f g := { X := P, ι := { app := λ X, begin cases X, exact f ≫ π, exact π, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, exact eq.symm w end }} @[simp] lemma fork.of_ι_app_zero {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).π.app zero = ι := rfl @[simp] lemma fork.of_ι_app_one {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).π.app one = ι ≫ f := rfl def fork.ι (t : fork f g) := t.π.app zero def cofork.π (t : cofork f g) := t.ι.app one def fork.condition (t : fork f g) : (fork.ι t) ≫ f = (fork.ι t) ≫ g := begin erw [t.w left, ← t.w right], refl end def cofork.condition (t : cofork f g) : f ≫ (cofork.π t) = g ≫ (cofork.π t) := begin erw [t.w left, ← t.w right], refl end def cone.of_fork {F : walking_parallel_pair.{v} ⥤ C} (t : fork (F.map left) (F.map right)) : cone F := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy), naturality' := λ j j' g, begin cases j; cases j'; cases g; dsimp; simp, erw ← t.w left, refl, erw ← t.w right, refl, end } }. def cocone.of_cofork {F : walking_parallel_pair.{v} ⥤ C} (t : cofork (F.map left) (F.map right)) : cocone F := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X, naturality' := λ j j' g, begin cases j; cases j'; cases g; dsimp; simp, erw ← t.w left, refl, erw ← t.w right, refl, end } }. @[simp] lemma cone.of_fork_π {F : walking_parallel_pair.{v} ⥤ C} (t : fork (F.map left) (F.map right)) (j) : (cone.of_fork t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cocone.of_cofork_ι {F : walking_parallel_pair.{v} ⥤ C} (t : cofork (F.map left) (F.map right)) (j) : (cocone.of_cofork t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl def fork.of_cone {F : walking_parallel_pair.{v} ⥤ C} (t : cone F) : fork (F.map left) (F.map right) := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy) } } def cofork.of_cocone {F : walking_parallel_pair.{v} ⥤ C} (t : cocone F) : cofork (F.map left) (F.map right) := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X } } @[simp] lemma fork.of_cone_π {F : walking_parallel_pair.{v} ⥤ C} (t : cone F) (j) : (fork.of_cone t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cofork.of_cocone_ι {F : walking_parallel_pair.{v} ⥤ C} (t : cocone F) (j) : (cofork.of_cocone t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl variables (f g) section variables [has_limit (parallel_pair f g)] abbreviation equalizer := limit (parallel_pair f g) abbreviation equalizer.ι : equalizer f g ⟶ X := limit.π (parallel_pair f g) zero @[simp, reassoc] lemma equalizer.condition : equalizer.ι f g ≫ f = equalizer.ι f g ≫ g := begin erw limit.w (parallel_pair f g) walking_parallel_pair_hom.left, erw limit.w (parallel_pair f g) walking_parallel_pair_hom.right end abbreviation equalizer.lift {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : W ⟶ equalizer f g := limit.lift (parallel_pair f g) (fork.of_ι k h) end section variables [has_colimit (parallel_pair f g)] abbreviation coequalizer := colimit (parallel_pair f g) abbreviation coequalizer.π : Y ⟶ coequalizer f g := colimit.ι (parallel_pair f g) one @[simp, reassoc] lemma coequalizer.condition : f ≫ coequalizer.π f g = g ≫ coequalizer.π f g := begin erw colimit.w (parallel_pair f g) walking_parallel_pair_hom.left, erw colimit.w (parallel_pair f g) walking_parallel_pair_hom.right end abbreviation coequalizer.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟶ W := colimit.desc (parallel_pair f g) (cofork.of_π k h) end variables (C) class has_equalizers := (has_limits_of_shape : has_limits_of_shape.{v} walking_parallel_pair C) class has_coequalizers := (has_colimits_of_shape : has_colimits_of_shape.{v} walking_parallel_pair C) attribute [instance] has_equalizers.has_limits_of_shape has_coequalizers.has_colimits_of_shape end category_theory.limits
f3495ffc8d377ecd87a4a1091407b1842872e14c
4727251e0cd73359b15b664c3170e5d754078599
/src/algebraic_geometry/ringed_space.lean
97ef0e5241d69c8fe0cb89c3627770b763a573e3
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
6,971
lean
/- 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 algebra.category.Ring.filtered_colimits import algebraic_geometry.sheafed_space import topology.sheaves.stalks import algebra.category.Ring.colimits import algebra.category.Ring.limits /-! # Ringed spaces We introduce the category of ringed spaces, as an alias for `SheafedSpace CommRing`. 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 open category_theory open topological_space open opposite open Top open Top.presheaf namespace algebraic_geometry /-- The type of Ringed spaces, as an abbreviation for `SheafedSpace CommRing`. -/ abbreviation RingedSpace : Type* := SheafedSpace CommRing namespace RingedSpace open SheafedSpace variables (X : RingedSpace.{v}) /-- 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`. -/ lemma is_unit_res_of_is_unit_germ (U : opens X) (f : X.presheaf.obj (op U)) (x : U) (h : is_unit (X.presheaf.germ x f)) : ∃ (V : opens X) (i : V ⟶ U) (hxV : x.1 ∈ V), is_unit (X.presheaf.map i.op f) := begin obtain ⟨g', heq⟩ := h.exists_right_inv, obtain ⟨V, hxV, g, rfl⟩ := X.presheaf.germ_exist x.1 g', let W := U ⊓ V, have hxW : x.1 ∈ W := ⟨x.2, hxV⟩, erw [← X.presheaf.germ_res_apply (opens.inf_le_left U V) ⟨x.1, hxW⟩ f, ← X.presheaf.germ_res_apply (opens.inf_le_right U V) ⟨x.1, hxW⟩ g, ← ring_hom.map_mul, ← ring_hom.map_one (X.presheaf.germ (⟨x.1, hxW⟩ : W))] at heq, obtain ⟨W', hxW', i₁, i₂, heq'⟩ := X.presheaf.germ_eq x.1 hxW hxW _ _ heq, use [W', i₁ ≫ opens.inf_le_left U V, hxW'], rw [ring_hom.map_one, ring_hom.map_mul, ← comp_apply, ← X.presheaf.map_comp, ← op_comp] at heq', exact is_unit_of_mul_eq_one _ _ heq', end /-- If a section `f` is a unit in each stalk, `f` must be a unit. -/ lemma is_unit_of_is_unit_germ (U : opens X) (f : X.presheaf.obj (op U)) (h : ∀ x : U, is_unit (X.presheaf.germ x f)) : is_unit f := begin -- 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 λ x : U, X.is_unit_res_of_is_unit_germ U f x (h x), have hcover : U ≤ supr V, { intros x hxU, rw [opens.mem_coe, opens.mem_supr], exact ⟨⟨x, hxU⟩, m ⟨x, hxU⟩⟩ }, -- Let `g x` denote the inverse of `f` in `U x`. choose g hg using λ x : U, is_unit.exists_right_inv (h_unit x), -- We claim that these local inverses glue together to a global inverse of `f`. obtain ⟨gl, gl_spec, -⟩ := X.sheaf.exists_unique_gluing' V U iVU hcover g _, swap, { intros x y, apply section_ext X.sheaf (V x ⊓ V y), rintro ⟨z, hzVx, hzVy⟩, rw [germ_res_apply, germ_res_apply], apply (is_unit.mul_right_inj (h ⟨z, (iVU x).le hzVx⟩)).mp, erw [← X.presheaf.germ_res_apply (iVU x) ⟨z, hzVx⟩ f, ← ring_hom.map_mul, congr_arg (X.presheaf.germ (⟨z, hzVx⟩ : V x)) (hg x), germ_res_apply, ← X.presheaf.germ_res_apply (iVU y) ⟨z, hzVy⟩ f, ← ring_hom.map_mul, congr_arg (X.presheaf.germ (⟨z, hzVy⟩ : V y)) (hg y), ring_hom.map_one, ring_hom.map_one] }, apply is_unit_of_mul_eq_one f gl, apply X.sheaf.eq_of_locally_eq' V U iVU hcover, intro i, rw [ring_hom.map_one, ring_hom.map_mul, gl_spec], exact hg i, end /-- 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 basic_open {U : opens X} (f : X.presheaf.obj (op U)) : opens X := { val := coe '' { x : U | is_unit (X.presheaf.germ x f) }, property := begin rw is_open_iff_forall_mem_open, rintros _ ⟨x, hx, rfl⟩, obtain ⟨V, i, hxV, hf⟩ := X.is_unit_res_of_is_unit_germ U f x hx, use V.1, refine ⟨_, V.2, hxV⟩, intros y hy, use (⟨y, i.le hy⟩ : U), rw set.mem_set_of_eq, split, { convert ring_hom.is_unit_map (X.presheaf.germ ⟨y, hy⟩) hf, exact (X.presheaf.germ_res_apply i ⟨y, hy⟩ f).symm }, { refl } end } @[simp] lemma mem_basic_open {U : opens X} (f : X.presheaf.obj (op U)) (x : U) : ↑x ∈ X.basic_open f ↔ is_unit (X.presheaf.germ x f) := begin split, { rintro ⟨x, hx, a⟩, cases subtype.eq a, exact hx }, { intro h, exact ⟨x, h, rfl⟩ }, end @[simp] lemma mem_top_basic_open (f : X.presheaf.obj (op ⊤)) (x : X) : x ∈ X.basic_open f ↔ is_unit (X.presheaf.germ ⟨x, show x ∈ (⊤ : opens X), by trivial⟩ f) := mem_basic_open X f ⟨x, _⟩ lemma basic_open_subset {U : opens X} (f : X.presheaf.obj (op U)) : X.basic_open f ⊆ U := by { rintros _ ⟨x, hx, rfl⟩, exact x.2 } /-- The restriction of a section `f` to the basic open of `f` is a unit. -/ lemma is_unit_res_basic_open {U : opens X} (f : X.presheaf.obj (op U)) : is_unit (X.presheaf.map (@hom_of_le (opens X) _ _ _ (X.basic_open_subset f)).op f) := begin apply is_unit_of_is_unit_germ, rintro ⟨_, ⟨x, hx, rfl⟩⟩, convert hx, rw germ_res_apply, refl, end @[simp] lemma basic_open_res {U V : (opens X)ᵒᵖ} (i : U ⟶ V) (f : X.presheaf.obj U) : @basic_open X (unop V) (X.presheaf.map i f) = (unop V) ∩ @basic_open X (unop U) f := begin induction U using opposite.rec, induction V using opposite.rec, let g := i.unop, have : i = g.op := rfl, clear_value g, subst this, ext, split, { rintro ⟨x, (hx : is_unit _), rfl⟩, rw germ_res_apply at hx, exact ⟨x.2, g x, hx, rfl⟩ }, { rintros ⟨hxV, x, hx, rfl⟩, refine ⟨⟨x, hxV⟩, (_ : is_unit _), rfl⟩, rwa germ_res_apply } end -- This should fire before `basic_open_res`. @[simp, priority 1100] lemma basic_open_res_eq {U V : (opens X)ᵒᵖ} (i : U ⟶ V) [is_iso i] (f : X.presheaf.obj U) : @basic_open X (unop V) (X.presheaf.map i f) = @RingedSpace.basic_open X (unop U) f := begin apply le_antisymm, { rw X.basic_open_res i f, exact inf_le_right }, { have := X.basic_open_res (inv i) (X.presheaf.map i f), rw [← comp_apply, ← X.presheaf.map_comp, is_iso.hom_inv_id, X.presheaf.map_id] at this, erw this, exact inf_le_right } end @[simp] lemma basic_open_mul {U : opens X} (f g : X.presheaf.obj (op U)) : X.basic_open (f * g) = X.basic_open f ⊓ X.basic_open g := begin ext1, dsimp [RingedSpace.basic_open], rw set.image_inter subtype.coe_injective, congr, ext, simp_rw map_mul, exact is_unit.mul_iff, end lemma basic_open_of_is_unit {U : opens X} {f : X.presheaf.obj (op U)} (hf : is_unit f) : X.basic_open f = U := begin apply le_antisymm, { exact X.basic_open_subset f }, intros x hx, erw X.mem_basic_open f (⟨x, hx⟩ : U), exact ring_hom.is_unit_map _ hf end end RingedSpace end algebraic_geometry
d6a76ea39187c7e730708c9bf814c7239e1ed7ed
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/algebra/order/functions.lean
5a6a6f5f78696fc1619cc7dfaa820105f110b9a1
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,551
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.order.basic import order.lattice /-! # `max` and `min` This file proves basic properties about maxima and minima on a `linear_order`. ## Tags min, max -/ universes u v variables {α : Type u} {β : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right section variables [linear_order α] [linear_order β] {f : α → β} {s : set α} {a b c d : α} -- translate from lattices to linear orders (sup → max, inf → min) @[simp] lemma le_min_iff : c ≤ min a b ↔ c ≤ a ∧ c ≤ b := le_inf_iff @[simp] lemma max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf lemma le_max_of_le_left : a ≤ b → a ≤ max b c := le_sup_of_le_left lemma le_max_of_le_right : a ≤ c → a ≤ max b c := le_sup_of_le_right lemma lt_max_of_lt_left (h : a < b) : a < max b c := h.trans_le (le_max_left b c) lemma lt_max_of_lt_right (h : a < c) : a < max b c := h.trans_le (le_max_right b c) lemma min_le_of_left_le : a ≤ c → min a b ≤ c := inf_le_of_left_le lemma min_le_of_right_le : b ≤ c → min a b ≤ c := inf_le_of_right_le lemma min_lt_of_left_lt (h : a < c) : min a b < c := (min_le_left a b).trans_lt h lemma min_lt_of_right_lt (h : b < c) : min a b < c := (min_le_right a b).trans_lt h lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right lemma min_le_max : min a b ≤ max a b := le_trans (min_le_left a b) (le_max_left a b) @[simp] lemma min_eq_left_iff : min a b = a ↔ a ≤ b := inf_eq_left @[simp] lemma min_eq_right_iff : min a b = b ↔ b ≤ a := inf_eq_right @[simp] lemma max_eq_left_iff : max a b = a ↔ b ≤ a := sup_eq_left @[simp] lemma max_eq_right_iff : max a b = b ↔ a ≤ b := sup_eq_right lemma min_eq_iff : min a b = c ↔ a = c ∧ a ≤ b ∨ b = c ∧ b ≤ a := begin split, { intro h, refine or.imp (λ h', _) (λ h', _) (le_total a b); exact ⟨by simpa [h'] using h, h'⟩ }, { rintro (⟨rfl, h⟩|⟨rfl, h⟩); simp [h] } end lemma max_eq_iff : max a b = c ↔ a = c ∧ b ≤ a ∨ b = c ∧ a ≤ b := @min_eq_iff (order_dual α) _ a b c /-- An instance asserting that `max a a = a` -/ instance max_idem : is_idempotent α max := by apply_instance -- short-circuit type class inference /-- An instance asserting that `min a a = a` -/ instance min_idem : is_idempotent α min := by apply_instance -- short-circuit type class inference @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := sup_lt_iff @[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := lt_inf_iff @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := lt_sup_iff @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := @lt_max_iff (order_dual α) _ _ _ _ @[simp] lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c := inf_le_iff @[simp] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := @min_le_iff (order_dual α) _ _ _ _ lemma max_lt_max (h₁ : a < c) (h₂ : b < d) : max a b < max c d := by simp [lt_max_iff, max_lt_iff, *] lemma min_lt_min (h₁ : a < c) (h₂ : b < d) : min a b < min c d := @max_lt_max (order_dual α) _ _ _ _ _ h₁ h₂ theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma monotone_on.map_max (hf : monotone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp only [max_eq_right, max_eq_left, hf ha hb, hf hb ha, h] lemma monotone_on.map_min (hf : monotone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (min a b) = min (f a) (f b) := hf.dual.map_max ha hb lemma antitone_on.map_max (hf : antitone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (max a b) = min (f a) (f b) := hf.dual_right.map_max ha hb lemma antitone_on.map_min (hf : antitone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (min a b) = max (f a) (f b) := hf.dual.map_max ha hb lemma monotone.map_max (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma monotone.map_min (hf : monotone f) : f (min a b) = min (f a) (f b) := hf.dual.map_max lemma antitone.map_max (hf : antitone f) : f (max a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma antitone.map_min (hf : antitone f) : f (min a b) = max (f a) (f b) := hf.dual.map_max lemma min_rec {p : α → Prop} {x y : α} (hx : x ≤ y → p x) (hy : y ≤ x → p y) : p (min x y) := (le_total x y).rec (λ h, (min_eq_left h).symm.subst (hx h)) (λ h, (min_eq_right h).symm.subst (hy h)) lemma max_rec {p : α → Prop} {x y : α} (hx : y ≤ x → p x) (hy : x ≤ y → p y) : p (max x y) := @min_rec (order_dual α) _ _ _ _ hx hy lemma min_rec' (p : α → Prop) {x y : α} (hx : p x) (hy : p y) : p (min x y) := min_rec (λ _, hx) (λ _, hy) lemma max_rec' (p : α → Prop) {x y : α} (hx : p x) (hy : p y) : p (max x y) := max_rec (λ _, hx) (λ _, hy) theorem min_choice (a b : α) : min a b = a ∨ min a b = b := by cases le_total a b; simp * theorem max_choice (a b : α) : max a b = a ∨ max a b = b := @min_choice (order_dual α) _ a b lemma le_of_max_le_left {a b c : α} (h : max a b ≤ c) : a ≤ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : α} (h : max a b ≤ c) : b ≤ c := le_trans (le_max_right _ _) h lemma max_commutative : commutative (max : α → α → α) := max_comm lemma max_associative : associative (max : α → α → α) := max_assoc lemma max_left_commutative : left_commutative (max : α → α → α) := max_left_comm lemma min_commutative : commutative (min : α → α → α) := min_comm lemma min_associative : associative (min : α → α → α) := min_assoc lemma min_left_commutative : left_commutative (min : α → α → α) := min_left_comm end
f1528d452268c0f3719e15a375c9f0008221ef9c
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/nakayama.lean
58714ac58936865fa715e9d4a69cad20fa2b681d
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,458
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.noetherian import ring_theory.jacobson_ideal /-! # Nakayama's lemma This file contains some alternative statements of Nakayama's Lemma as found in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). ## Main statements * `submodule.eq_smul_of_le_smul_of_le_jacobson` - A version of (2) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV)., generalising to the Jacobson of any ideal. * `submodule.eq_bot_of_le_smul_of_le_jacobson_bot` - Statement (2) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). * `submodule.smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson` - A version of (4) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV)., generalising to the Jacobson of any ideal. * `submodule.smul_sup_eq_of_le_smul_of_le_jacobson_bot` - Statement (4) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). Note that a version of Statement (1) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV) can be found in `ring_theory/noetherian` under the name `submodule.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul` ## References * [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV) ## Tags Nakayama, Jacobson -/ variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] open ideal namespace submodule /-- *Nakayama's Lemma** - A slightly more general version of (2) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `eq_bot_of_le_smul_of_le_jacobson_bot` for the special case when `J = ⊥`. -/ lemma eq_smul_of_le_smul_of_le_jacobson {I J : ideal R} {N : submodule R M} (hN : N.fg) (hIN : N ≤ I • N) (hIjac : I ≤ jacobson J) : N = J • N := begin refine le_antisymm _ (submodule.smul_le.2 (λ _ _ _, submodule.smul_mem _ _)), intros n hn, cases submodule.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul I N hN hIN with r hr, cases exists_mul_sub_mem_of_sub_one_mem_jacobson r (hIjac hr.1) with s hs, have : n = (-(s * r - 1) • n), { rw [neg_sub, sub_smul, mul_smul, hr.2 n hn, one_smul, smul_zero, sub_zero] }, rw this, exact submodule.smul_mem_smul (submodule.neg_mem _ hs) hn end /-- *Nakayama's Lemma** - Statement (2) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `eq_smul_of_le_smul_of_le_jacobson` for a generalisation to the `jacobson` of any ideal -/ lemma eq_bot_of_le_smul_of_le_jacobson_bot (I : ideal R) (N : submodule R M) (hN : N.fg) (hIN : N ≤ I • N) (hIjac : I ≤ jacobson ⊥) : N = ⊥ := by rw [eq_smul_of_le_smul_of_le_jacobson hN hIN hIjac, submodule.bot_smul] /-- *Nakayama's Lemma** - A slightly more general version of (4) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `smul_sup_eq_of_le_smul_of_le_jacobson_bot` for the special case when `J = ⊥`. -/ lemma smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson {I J : ideal R} {N N' : submodule R M} (hN' : N'.fg) (hIJ : I ≤ jacobson J) (hNN : N ⊔ N' ≤ N ⊔ I • N') : N ⊔ I • N' = N ⊔ J • N' := begin have hNN' : N ⊔ N' = N ⊔ I • N', from le_antisymm hNN (sup_le_sup_left (submodule.smul_le.2 (λ _ _ _, submodule.smul_mem _ _)) _), have h_comap := submodule.comap_injective_of_surjective (linear_map.range_eq_top.1 (N.range_mkq)), have : (I • N').map N.mkq = N'.map N.mkq, { rw ←h_comap.eq_iff, simpa [comap_map_eq, sup_comm, eq_comm] using hNN' }, have := @submodule.eq_smul_of_le_smul_of_le_jacobson _ _ _ _ _ I J (N'.map N.mkq) (hN'.map _) (by rw [← map_smul'', this]; exact le_rfl) hIJ, rw [← map_smul'', ←h_comap.eq_iff, comap_map_eq, comap_map_eq, submodule.ker_mkq, sup_comm, hNN'] at this, rw [this, sup_comm] end /-- *Nakayama's Lemma** - Statement (4) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson` for a generalisation to the `jacobson` of any ideal -/ lemma smul_sup_le_of_le_smul_of_le_jacobson_bot {I : ideal R} {N N' : submodule R M} (hN' : N'.fg) (hIJ : I ≤ jacobson ⊥) (hNN : N ⊔ N' ≤ N ⊔ I • N') : I • N' ≤ N := by rw [← sup_eq_left, smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson hN' hIJ hNN, bot_smul, sup_bot_eq] end submodule
d762a82f8ac784193637fa768e7cc2777fd04efe
82e44445c70db0f03e30d7be725775f122d72f3e
/src/logic/embedding.lean
e2e667575be1bfa157deaf8a095c73805e4f9565
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
12,611
lean
/- 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 data.equiv.basic import data.sigma.basic /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ @[nolint has_inhabited_instance] -- depending on cardinalities, an injective function may not exist structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ initialize_simps_projections embedding (to_fun → apply) end function section equiv variables {α : Sort u} {β : Sort v} (f : α ≃ β) /-- Convert an `α ≃ β` to `α ↪ β`. This is also available as a coercion `equiv.coe_embedding`. The explicit `equiv.to_embedding` version is preferred though, since the coercion can have issues inferring the type of the resulting embedding. For example: ```lean -- Works: example (s : finset (fin 3)) (f : equiv.perm (fin 3)) : s.map f.to_embedding = s.map f := by simp -- Error, `f` has type `fin 3 ≃ fin 3` but is expected to have type `fin 3 ↪ ?m_1 : Type ?` example (s : finset (fin 3)) (f : equiv.perm (fin 3)) : s.map f = s.map f.to_embedding := by simp ``` -/ @[simps] protected def equiv.to_embedding : α ↪ β := ⟨f, f.injective⟩ instance equiv.coe_embedding : has_coe (α ≃ β) (α ↪ β) := ⟨equiv.to_embedding⟩ @[reducible] instance equiv.perm.coe_embedding : has_coe (equiv.perm α) (α ↪ α) := equiv.coe_embedding @[simp] lemma equiv.coe_eq_to_embedding : ↑f = f.to_embedding := rfl /-- Given an equivalence to a subtype, produce an embedding to the elements of the corresponding set. -/ @[simps] def equiv.as_embedding {p : β → Prop} (e : α ≃ subtype p) : α ↪ β := ⟨coe ∘ e, subtype.coe_injective.comp e.injective⟩ @[simp] lemma equiv.as_embedding_range {α β : Sort*} {p : β → Prop} (e : α ≃ subtype p) : set.range e.as_embedding = set_of p := set.ext $ λ x, ⟨λ ⟨y, h⟩, h ▸ subtype.coe_prop (e y), λ hs, ⟨e.symm ⟨x, hs⟩, by simp⟩⟩ end equiv namespace function namespace embedding lemma coe_injective {α β} : @function.injective (α ↪ β) (α → β) coe_fn | ⟨x, _⟩ ⟨y, _⟩ rfl := rfl @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl @[simp] lemma mk_coe {α β : Type*} (f : α ↪ β) (inj) : (⟨f, inj⟩ : α ↪ β) = f := by { ext, simp } theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[simp] lemma apply_eq_iff_eq {α β : Type*} (f : α ↪ β) (x y : α) : f x = f y ↔ x = y := f.injective.eq_iff @[refl, simps {simp_rhs := tt}] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans, simps {simp_rhs := tt}] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : e.to_embedding.trans e.symm.to_embedding = embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : e.symm.to_embedding.trans e.to_embedding = embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ /-- There is always an embedding from an empty type. --/ protected def of_is_empty {α β} [is_empty α] : α ↪ β := ⟨is_empty_elim, is_empty_elim⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ @[simps] def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ @[simps] def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ @[simps apply] def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ @[simps apply] def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ @[simps apply] protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ lemma swap_apply {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y z : α) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := f.injective.swap_apply x y z lemma swap_comp {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y : α) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := f.injective.swap_comp x y end embedding end function namespace equiv open function.embedding /-- The type of embeddings `α ↪ β` is equivalent to the subtype of all injective functions `α → β`. -/ def subtype_injective_equiv_embedding (α β : Sort*) : {f : α → β // function.injective f} ≃ (α ↪ β) := { to_fun := λ f, ⟨f.val, f.property⟩, inv_fun := λ f, ⟨f, f.injective⟩, left_inv := λ f, by simp, right_inv := λ f, by {ext, refl} } /-- If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then the type of embeddings `α₁ ↪ β₁` is equivalent to the type of embeddings `α₂ ↪ β₂`. -/ @[congr, simps apply] def embedding_congr {α β γ δ : Sort*} (h : α ≃ β) (h' : γ ≃ δ) : (α ↪ γ) ≃ (β ↪ δ) := { to_fun := λ f, h.symm.to_embedding.trans $ f.trans $ h'.to_embedding, inv_fun := λ f, h.to_embedding.trans $ f.trans $ h'.symm.to_embedding, left_inv := λ x, by {ext, simp}, right_inv := λ x, by {ext, simp} } @[simp] lemma embedding_congr_refl {α β : Sort*} : embedding_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ↪ β) := by {ext, refl} @[simp] lemma embedding_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : embedding_congr (e₁.trans e₂) (e₁'.trans e₂') = (embedding_congr e₁ e₁').trans (embedding_congr e₂ e₂') := rfl @[simp] lemma embedding_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (embedding_congr e₁ e₂).symm = embedding_congr e₁.symm e₂.symm := rfl lemma embedding_congr_apply_trans {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ ↪ β₁) (g : β₁ ↪ γ₁) : equiv.embedding_congr ea ec (f.trans g) = (equiv.embedding_congr ea eb f).trans (equiv.embedding_congr eb ec g) := by {ext, simp} @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ @[simps apply] def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by { congr, injection h }⟩ end set
1af8a9297c3ce0d23dd5399b1cc1095dfae60836
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/data/real/nnreal.lean
883435dd492d6e645bd48f5fd6f9f214d5fccbe0
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
25,542
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import algebra.linear_ordered_comm_group_with_zero import data.finset.lattice import data.real.basic noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : nnreal) : n.val = n := rfl instance : can_lift ℝ nnreal := { coe := coe, cond := λ r, r ≥ 0, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := subtype.ext_iff_val.symm @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝ≥0 := { zero_ne_one := assume h, zero_ne_one $ nnreal.eq_iff.2 h, inv_zero := nnreal.eq $ show (0⁻¹ : ℝ) = 0, from inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝ≥0), .. (_ : comm_semiring ℝ≥0), .. (_ : semiring ℝ≥0) } @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ @[norm_cast] lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n •ℕ r) = n •ℕ (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := to_real_hom.map_nat_cast n instance : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective @[norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring } instance : linear_ordered_comm_group_with_zero ℝ≥0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝ≥0 := { .. nnreal.linear_ordered_semiring, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝ≥0, by apply_instance), .. (show nonzero ℝ≥0, by apply_instance) } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : nnreal → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : nnreal → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_decidable_linear_order, .. nnreal.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n •ℕ y : nnreal), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : nnreal) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : nnreal) : ((max x y : nnreal) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : nnreal) : ((min x y : nnreal) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p ≥ 0) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r ≥ 0) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact eq_of_mul_eq_mul_left (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end @[field_simps] theorem mul_ne_zero' {a b : nnreal} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := mul_ne_zero h₁ h₂ end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : nnreal} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : nnreal} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : nnreal) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [nnreal.div_def, finset.sum_mul] @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := mul_pos hr (inv_pos.2 hp) @[simp] lemma inv_one : (1:ℝ≥0)⁻¹ = 1 := nnreal.eq $ inv_one @[simp] lemma div_one {r : ℝ≥0} : r / 1 = r := by rw [div_def, inv_one, mul_one] protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv' _ _ protected lemma inv_pow {r : ℝ≥0} {n : ℕ} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma div_self {r : ℝ≥0} (h : r ≠ 0) : r / r = 1 := mul_inv_cancel h lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma mul_div_cancel {r p : ℝ≥0} (h : p ≠ 0) : r * p / p = r := by rw [div_def, mul_assoc, mul_inv_cancel h, mul_one] @[simp] lemma mul_div_cancel' {r p : ℝ≥0} (h : r ≠ 0) : r * (p / r) = p := by rw [mul_comm, div_mul_cancel _ h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq (inv_inv' _) @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ zero_lt_iff_ne_zero.2 hr lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa [div_def] using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (zero_lt_iff_ne_zero.mpr hc) end lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] theorem div_pow {a b : ℝ≥0} (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := div_pow _ _ _ @[field_simps] lemma mul_div_assoc' (a b c : ℝ≥0) : a * (b / c) = (a * b) / c := by rw [div_def, div_def, mul_assoc] @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma inv_eq_one_div (a : ℝ≥0) : a⁻¹ = 1/a := by rw [div_def, one_mul] @[field_simps] lemma div_mul_eq_mul_div (a b c : ℝ≥0) : (a / b) * c = (a * c) / b := by { rw [div_def, div_def], ac_refl } @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma one_div_eq_inv (a : ℝ≥0) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma one_div_div (a b : ℝ≥0) : 1 / (a / b) = b / a := by { rw ← nnreal.eq_iff, simp [one_div_div] } lemma div_eq_mul_one_div (a b : ℝ≥0) : a / b = a * (1 / b) := by rw [div_def, div_def, one_mul] @[field_simps] lemma div_div_eq_mul_div (a b c : ℝ≥0) : a / (b / c) = (a * c) / b := by { rw ← nnreal.eq_iff, simp [div_div_eq_mul_div] } @[field_simps] lemma div_div_eq_div_mul (a b c : ℝ≥0) : (a / b) / c = a / (b * c) := by { rw ← nnreal.eq_iff, simp [div_div_eq_div_mul] } @[field_simps] lemma div_eq_div_iff {a b c d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := div_eq_div_iff hb hd @[field_simps] lemma div_eq_iff {a b c : ℝ≥0} (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff {a b c : ℝ≥0} (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff c 1 a b one_ne_zero hb end inv section pow theorem pow_eq_zero {a : ℝ≥0} {n : ℕ} (h : a^n = 0) : a = 0 := begin rw ← nnreal.eq_iff, rw [← nnreal.eq_iff, coe_pow] at h, exact pow_eq_zero h end @[field_simps] theorem pow_ne_zero {a : ℝ≥0} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h end pow end nnreal
27db322c76207576314e245f1578293f2b36dedb
94e33a31faa76775069b071adea97e86e218a8ee
/src/ring_theory/ideal/quotient.lean
e810dbd7ae5bbeccfba695c72e68805b11308bb7
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
16,098
lean
/- 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, Anne Baanen -/ import linear_algebra.quotient import ring_theory.ideal.basic /-! # Ideal quotients This file defines ideal quotients as a special case of submodule quotients and proves some basic results about these quotients. See `algebra.ring_quot` for quotients of non-commutative rings. ## Main definitions - `ideal.quotient`: the quotient of a commutative ring `R` by an ideal `I : ideal R` ## Main results - `ideal.quotient_inf_ring_equiv_pi_quotient`: the **Chinese Remainder Theorem** -/ universes u v w namespace ideal open set open_locale big_operators variables {R : Type u} [comm_ring R] (I : ideal R) {a b : R} variables {S : Type v} /-- The quotient `R/I` of a ring `R` by an ideal `I`. The ideal quotient of `I` is defined to equal the quotient of `I` as an `R`-submodule of `R`. This definition is marked `reducible` so that typeclass instances can be shared between `ideal.quotient I` and `submodule.quotient I`. -/ -- Note that at present `ideal` means a left-ideal, -- so this quotient is only useful in a commutative ring. -- We should develop quotients by two-sided ideals as well. @[reducible] instance : has_quotient R (ideal R) := submodule.has_quotient namespace quotient variables {I} {x y : R} instance has_one (I : ideal R) : has_one (R ⧸ I) := ⟨submodule.quotient.mk 1⟩ instance has_mul (I : ideal R) : has_mul (R ⧸ I) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, submodule.quotient.mk (a * b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, quot.sound $ begin rw submodule.quotient_rel_r_def at h₁ h₂ ⊢, have F := I.add_mem (I.mul_mem_left a₂ h₁) (I.mul_mem_right b₁ h₂), have : a₁ * a₂ - b₁ * b₂ = a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁, { rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁] }, rw ← this at F, change _ ∈ _, convert F, end⟩ instance comm_ring (I : ideal R) : comm_ring (R ⧸ I) := { mul := (*), one := 1, nat_cast := λ n, submodule.quotient.mk n, nat_cast_zero := by simp [nat.cast], nat_cast_succ := by simp [nat.cast]; refl, mul_assoc := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (mul_assoc a b c), mul_comm := λ a b, quotient.induction_on₂' a b $ λ a b, congr_arg submodule.quotient.mk (mul_comm a b), one_mul := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (one_mul a), mul_one := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (mul_one a), left_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (left_distrib a b c), right_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (right_distrib a b c), ..submodule.quotient.add_comm_group I } /-- The ring homomorphism from a ring `R` to a quotient ring `R/I`. -/ def mk (I : ideal R) : R →+* (R ⧸ I) := ⟨λ a, submodule.quotient.mk a, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ /- Two `ring_homs`s from the quotient by an ideal are equal if their compositions with `ideal.quotient.mk'` are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext [non_assoc_semiring S] ⦃f g : R ⧸ I →+* S⦄ (h : f.comp (mk I) = g.comp (mk I)) : f = g := ring_hom.ext $ λ x, quotient.induction_on' x $ (ring_hom.congr_fun h : _) instance inhabited : inhabited (R ⧸ I) := ⟨mk I 37⟩ protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I @[simp] theorem mk_eq_mk (x : R) : (submodule.quotient.mk x : R ⧸ I) = mk I x := rfl lemma eq_zero_iff_mem {I : ideal R} : mk I a = 0 ↔ a ∈ I := submodule.quotient.mk_eq_zero _ theorem zero_eq_one_iff {I : ideal R} : (0 : R ⧸ I) = 1 ↔ I = ⊤ := eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm theorem zero_ne_one_iff {I : ideal R} : (0 : R ⧸ I) ≠ 1 ↔ I ≠ ⊤ := not_congr zero_eq_one_iff protected theorem nontrivial {I : ideal R} (hI : I ≠ ⊤) : nontrivial (R ⧸ I) := ⟨⟨0, 1, zero_ne_one_iff.2 hI⟩⟩ lemma subsingleton_iff {I : ideal R} : subsingleton (R ⧸ I) ↔ I = ⊤ := by rw [eq_top_iff_one, ← subsingleton_iff_zero_eq_one, eq_comm, ← I^.quotient.mk^.map_one, quotient.eq_zero_iff_mem] instance : unique (R ⧸ (⊤ : ideal R)) := ⟨⟨0⟩, by rintro ⟨x⟩; exact quotient.eq_zero_iff_mem.mpr submodule.mem_top⟩ lemma mk_surjective : function.surjective (mk I) := λ y, quotient.induction_on' y (λ x, exists.intro x rfl) /-- If `I` is an ideal of a commutative ring `R`, if `q : R → R/I` is the quotient map, and if `s ⊆ R` is a subset, then `q⁻¹(q(s)) = ⋃ᵢ(i + s)`, the union running over all `i ∈ I`. -/ lemma quotient_ring_saturate (I : ideal R) (s : set R) : mk I ⁻¹' (mk I '' s) = (⋃ x : I, (λ y, x.1 + y) '' s) := begin ext x, simp only [mem_preimage, mem_image, mem_Union, ideal.quotient.eq], exact ⟨λ ⟨a, a_in, h⟩, ⟨⟨_, I.neg_mem h⟩, a, a_in, by simp⟩, λ ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by rw [← eq, sub_add_eq_sub_sub_swap, sub_self, zero_sub]; exact I.neg_mem hi⟩⟩ end instance is_domain (I : ideal R) [hI : I.is_prime] : is_domain (R ⧸ I) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, quotient.induction_on₂' a b $ λ a b hab, (hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), .. quotient.nontrivial hI.1 } lemma is_domain_iff_prime (I : ideal R) : is_domain (R ⧸ I) ↔ I.is_prime := ⟨ λ ⟨h1, h2⟩, ⟨zero_ne_one_iff.1 $ @zero_ne_one _ _ ⟨h2⟩, λ x y h, by { simp only [←eq_zero_iff_mem, (mk I).map_mul] at ⊢ h, exact h1 h}⟩, λ h, by { resetI, apply_instance }⟩ lemma exists_inv {I : ideal R} [hI : I.is_maximal] : ∀ {a : (R ⧸ I)}, a ≠ 0 → ∃ b : (R ⧸ I), a * b = 1 := begin rintro ⟨a⟩ h, rcases hI.exists_inv (mt eq_zero_iff_mem.2 h) with ⟨b, c, hc, abc⟩, rw [mul_comm] at abc, refine ⟨mk _ b, quot.sound _⟩, --quot.sound hb rw ← eq_sub_iff_add_eq' at abc, rw [abc, ← neg_mem_iff, neg_sub] at hc, rw submodule.quotient_rel_r_def, convert hc, end open_locale classical /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications. See note [reducible non-instances]. -/ @[reducible] protected noncomputable def field (I : ideal R) [hI : I.is_maximal] : field (R ⧸ I) := { inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_zero := dif_pos rfl, ..quotient.comm_ring I, ..quotient.is_domain I } /-- If the quotient by an ideal is a field, then the ideal is maximal. -/ theorem maximal_of_is_field (I : ideal R) (hqf : is_field (R ⧸ I)) : I.is_maximal := begin apply ideal.is_maximal_iff.2, split, { intro h, rcases hqf.exists_pair_ne with ⟨⟨x⟩, ⟨y⟩, hxy⟩, exact hxy (ideal.quotient.eq.2 (mul_one (x - y) ▸ I.mul_mem_left _ h)) }, { intros J x hIJ hxnI hxJ, rcases hqf.mul_inv_cancel (mt ideal.quotient.eq_zero_iff_mem.1 hxnI) with ⟨⟨y⟩, hy⟩, rw [← zero_add (1 : R), ← sub_self (x * y), sub_add], refine J.sub_mem (J.mul_mem_right _ hxJ) (hIJ (ideal.quotient.eq.1 hy)) } end /-- The quotient of a ring by an ideal is a field iff the ideal is maximal. -/ theorem maximal_ideal_iff_is_field_quotient (I : ideal R) : I.is_maximal ↔ is_field (R ⧸ I) := ⟨λ h, by { letI := @quotient.field _ _ I h, exact field.to_is_field _ }, maximal_of_is_field _⟩ variable [comm_ring S] /-- Given a ring homomorphism `f : R →+* S` sending all elements of an ideal to zero, lift it to the quotient by this ideal. -/ def lift (I : ideal R) (f : R →+* S) (H : ∀ (a : R), a ∈ I → f a = 0) : R ⧸ I →+* S := { map_one' := f.map_one, map_zero' := f.map_zero, map_add' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_add, map_mul' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_mul, .. quotient_add_group.lift I.to_add_subgroup f.to_add_monoid_hom H } @[simp] lemma lift_mk (I : ideal R) (f : R →+* S) (H : ∀ (a : R), a ∈ I → f a = 0) : lift I f H (mk I a) = f a := rfl /-- The ring homomorphism from the quotient by a smaller ideal to the quotient by a larger ideal. This is the `ideal.quotient` version of `quot.factor` -/ def factor (S T : ideal R) (H : S ≤ T) : R ⧸ S →+* R ⧸ T := ideal.quotient.lift S (T^.quotient.mk) (λ x hx, eq_zero_iff_mem.2 (H hx)) @[simp] lemma factor_mk (S T : ideal R) (H : S ≤ T) (x : R) : factor S T H (mk S x) = mk T x := rfl @[simp] lemma factor_comp_mk (S T : ideal R) (H : S ≤ T) : (factor S T H).comp (mk S) = mk T := by { ext x, rw [ring_hom.comp_apply, factor_mk] } end quotient /-- Quotienting by equal ideals gives equivalent rings. See also `submodule.quot_equiv_of_eq`. -/ def quot_equiv_of_eq {R : Type*} [comm_ring R] {I J : ideal R} (h : I = J) : (R ⧸ I) ≃+* R ⧸ J := { map_mul' := by { rintro ⟨x⟩ ⟨y⟩, refl }, .. submodule.quot_equiv_of_eq I J h } @[simp] lemma quot_equiv_of_eq_mk {R : Type*} [comm_ring R] {I J : ideal R} (h : I = J) (x : R) : quot_equiv_of_eq h (ideal.quotient.mk I x) = ideal.quotient.mk J x := rfl section pi variables (ι : Type v) /-- `R^n/I^n` is a `R/I`-module. -/ instance module_pi : module (R ⧸ I) ((ι → R) ⧸ I.pi ι) := { smul := λ c m, quotient.lift_on₂' c m (λ r m, submodule.quotient.mk $ r • m) begin intros c₁ m₁ c₂ m₂ hc hm, apply ideal.quotient.eq.2, rw submodule.quotient_rel_r_def at hc hm, intro i, exact I.mul_sub_mul_mem hc (hm i), end, one_smul := begin rintro ⟨a⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact one_mul (a i), end, mul_smul := begin rintro ⟨a⟩ ⟨b⟩ ⟨c⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, simp only [(•)], congr' with i, exact mul_assoc a b (c i), end, smul_add := begin rintro ⟨a⟩ ⟨b⟩ ⟨c⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact mul_add a (b i) (c i), end, smul_zero := begin rintro ⟨a⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact mul_zero a, end, add_smul := begin rintro ⟨a⟩ ⟨b⟩ ⟨c⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact add_mul a b (c i), end, zero_smul := begin rintro ⟨a⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact zero_mul (a i), end, } /-- `R^n/I^n` is isomorphic to `(R/I)^n` as an `R/I`-module. -/ noncomputable def pi_quot_equiv : ((ι → R) ⧸ I.pi ι) ≃ₗ[(R ⧸ I)] (ι → (R ⧸ I)) := { to_fun := λ x, quotient.lift_on' x (λ f i, ideal.quotient.mk I (f i)) $ λ a b hab, funext (λ i, (submodule.quotient.eq' _).2 (quotient_add_group.left_rel_apply.mp hab i)), map_add' := by { rintros ⟨_⟩ ⟨_⟩, refl }, map_smul' := by { rintros ⟨_⟩ ⟨_⟩, refl }, inv_fun := λ x, ideal.quotient.mk (I.pi ι) $ λ i, quotient.out' (x i), left_inv := begin rintro ⟨x⟩, exact ideal.quotient.eq.2 (λ i, ideal.quotient.eq.1 (quotient.out_eq' _)) end, right_inv := begin intro x, ext i, obtain ⟨r, hr⟩ := @quot.exists_rep _ _ (x i), simp_rw ←hr, convert quotient.out_eq' _ end } /-- If `f : R^n → R^m` is an `R`-linear map and `I ⊆ R` is an ideal, then the image of `I^n` is contained in `I^m`. -/ lemma map_pi {ι} [fintype ι] {ι' : Type w} (x : ι → R) (hi : ∀ i, x i ∈ I) (f : (ι → R) →ₗ[R] (ι' → R)) (i : ι') : f x i ∈ I := begin classical, rw pi_eq_sum_univ x, simp only [finset.sum_apply, smul_eq_mul, linear_map.map_sum, pi.smul_apply, linear_map.map_smul], exact I.sum_mem (λ j hj, I.mul_mem_right _ (hi j)) end end pi section chinese_remainder variables {ι : Type v} theorem exists_sub_one_mem_and_mem (s : finset ι) {f : ι → ideal R} (hf : ∀ i ∈ s, ∀ j ∈ s, i ≠ j → f i ⊔ f j = ⊤) (i : ι) (his : i ∈ s) : ∃ r : R, r - 1 ∈ f i ∧ ∀ j ∈ s, j ≠ i → r ∈ f j := begin have : ∀ j ∈ s, j ≠ i → ∃ r : R, ∃ H : r - 1 ∈ f i, r ∈ f j, { intros j hjs hji, specialize hf i his j hjs hji.symm, rw [eq_top_iff_one, submodule.mem_sup] at hf, rcases hf with ⟨r, hri, s, hsj, hrs⟩, refine ⟨1 - r, _, _⟩, { rw [sub_right_comm, sub_self, zero_sub], exact (f i).neg_mem hri }, { rw [← hrs, add_sub_cancel'], exact hsj } }, classical, have : ∃ g : ι → R, (∀ j, g j - 1 ∈ f i) ∧ ∀ j ∈ s, j ≠ i → g j ∈ f j, { choose g hg1 hg2, refine ⟨λ j, if H : j ∈ s ∧ j ≠ i then g j H.1 H.2 else 1, λ j, _, λ j, _⟩, { split_ifs with h, { apply hg1 }, rw sub_self, exact (f i).zero_mem }, { intros hjs hji, rw dif_pos, { apply hg2 }, exact ⟨hjs, hji⟩ } }, rcases this with ⟨g, hgi, hgj⟩, use (∏ x in s.erase i, g x), split, { rw [← quotient.eq, ring_hom.map_one, ring_hom.map_prod], apply finset.prod_eq_one, intros, rw [← ring_hom.map_one, quotient.eq], apply hgi }, intros j hjs hji, rw [← quotient.eq_zero_iff_mem, ring_hom.map_prod], refine finset.prod_eq_zero (finset.mem_erase_of_ne_of_mem hji hjs) _, rw quotient.eq_zero_iff_mem, exact hgj j hjs hji end theorem exists_sub_mem [fintype ι] {f : ι → ideal R} (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) (g : ι → R) : ∃ r : R, ∀ i, r - g i ∈ f i := begin have : ∃ φ : ι → R, (∀ i, φ i - 1 ∈ f i) ∧ (∀ i j, i ≠ j → φ i ∈ f j), { have := exists_sub_one_mem_and_mem (finset.univ : finset ι) (λ i _ j _ hij, hf i j hij), choose φ hφ, existsi λ i, φ i (finset.mem_univ i), exact ⟨λ i, (hφ i _).1, λ i j hij, (hφ i _).2 j (finset.mem_univ j) hij.symm⟩ }, rcases this with ⟨φ, hφ1, hφ2⟩, use ∑ i, g i * φ i, intros i, rw [← quotient.eq, ring_hom.map_sum], refine eq.trans (finset.sum_eq_single i _ _) _, { intros j _ hji, rw quotient.eq_zero_iff_mem, exact (f i).mul_mem_left _ (hφ2 j i hji) }, { intros hi, exact (hi $ finset.mem_univ i).elim }, specialize hφ1 i, rw [← quotient.eq, ring_hom.map_one] at hφ1, rw [ring_hom.map_mul, hφ1, mul_one] end /-- The homomorphism from `R/(⋂ i, f i)` to `∏ i, (R / f i)` featured in the Chinese Remainder Theorem. It is bijective if the ideals `f i` are comaximal. -/ def quotient_inf_to_pi_quotient (f : ι → ideal R) : R ⧸ (⨅ i, f i) →+* Π i, R ⧸ f i := quotient.lift (⨅ i, f i) (pi.ring_hom (λ i : ι, (quotient.mk (f i) : _))) $ λ r hr, begin rw submodule.mem_infi at hr, ext i, exact quotient.eq_zero_iff_mem.2 (hr i) end theorem quotient_inf_to_pi_quotient_bijective [fintype ι] {f : ι → ideal R} (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) : function.bijective (quotient_inf_to_pi_quotient f) := ⟨λ x y, quotient.induction_on₂' x y $ λ r s hrs, quotient.eq.2 $ (submodule.mem_infi _).2 $ λ i, quotient.eq.1 $ show quotient_inf_to_pi_quotient f (quotient.mk' r) i = _, by rw hrs; refl, λ g, let ⟨r, hr⟩ := exists_sub_mem hf (λ i, quotient.out' (g i)) in ⟨quotient.mk _ r, funext $ λ i, quotient.out_eq' (g i) ▸ quotient.eq.2 (hr i)⟩⟩ /-- Chinese Remainder Theorem. Eisenbud Ex.2.6. Similar to Atiyah-Macdonald 1.10 and Stacks 00DT -/ noncomputable def quotient_inf_ring_equiv_pi_quotient [fintype ι] (f : ι → ideal R) (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) : R ⧸ (⨅ i, f i) ≃+* Π i, R ⧸ f i := { .. equiv.of_bijective _ (quotient_inf_to_pi_quotient_bijective hf), .. quotient_inf_to_pi_quotient f } end chinese_remainder end ideal
86d4422e1979c7426fb8cb8a46d3ed25685982f4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/opposites.lean
a5917dbb5572f190ac67397346eab771a36c6a75
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
17,484
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison -/ import category_theory.equivalence /-! # Opposite categories We provide a category instance on `Cᵒᵖ`. The morphisms `X ⟶ Y` are defined to be the morphisms `unop Y ⟶ unop X` in `C`. Here `Cᵒᵖ` is an irreducible typeclass synonym for `C` (it is the same one used in the algebra library). We also provide various mechanisms for constructing opposite morphisms, functors, and natural transformations. Unfortunately, because we do not have a definitional equality `op (op X) = X`, there are quite a few variations that are needed in practice. -/ universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. open opposite variables {C : Type u₁} section quiver variables [quiver.{v₁} C] lemma quiver.hom.op_inj {X Y : C} : function.injective (quiver.hom.op : (X ⟶ Y) → (op Y ⟶ op X)) := λ _ _ H, congr_arg quiver.hom.unop H lemma quiver.hom.unop_inj {X Y : Cᵒᵖ} : function.injective (quiver.hom.unop : (X ⟶ Y) → (unop Y ⟶ unop X)) := λ _ _ H, congr_arg quiver.hom.op H @[simp] lemma quiver.hom.unop_op {X Y : C} (f : X ⟶ Y) : f.op.unop = f := rfl @[simp] lemma quiver.hom.op_unop {X Y : Cᵒᵖ} (f : X ⟶ Y) : f.unop.op = f := rfl end quiver namespace category_theory variables [category.{v₁} C] /-- The opposite category. See <https://stacks.math.columbia.edu/tag/001M>. -/ instance category.opposite : category.{v₁} Cᵒᵖ := { comp := λ _ _ _ f g, (g.unop ≫ f.unop).op, id := λ X, (𝟙 (unop X)).op } @[simp] lemma op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).op = g.op ≫ f.op := rfl @[simp] lemma op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl @[simp] lemma unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).unop = g.unop ≫ f.unop := rfl @[simp] lemma unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl @[simp] lemma unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl @[simp] lemma op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl section variables (C) /-- The functor from the double-opposite of a category to the underlying category. -/ @[simps] def op_op : (Cᵒᵖ)ᵒᵖ ⥤ C := { obj := λ X, unop (unop X), map := λ X Y f, f.unop.unop } /-- The functor from a category to its double-opposite. -/ @[simps] def unop_unop : C ⥤ Cᵒᵖᵒᵖ := { obj := λ X, op (op X), map := λ X Y f, f.op.op } /-- The double opposite category is equivalent to the original. -/ @[simps] def op_op_equivalence : Cᵒᵖᵒᵖ ≌ C := { functor := op_op C, inverse := unop_unop C, unit_iso := iso.refl (𝟭 Cᵒᵖᵒᵖ), counit_iso := iso.refl (unop_unop C ⋙ op_op C) } end /-- If `f` is an isomorphism, so is `f.op` -/ instance is_iso_op {X Y : C} (f : X ⟶ Y) [is_iso f] : is_iso f.op := ⟨⟨(inv f).op, ⟨quiver.hom.unop_inj (by tidy), quiver.hom.unop_inj (by tidy)⟩⟩⟩ /-- If `f.op` is an isomorphism `f` must be too. (This cannot be an instance as it would immediately loop!) -/ lemma is_iso_of_op {X Y : C} (f : X ⟶ Y) [is_iso f.op] : is_iso f := ⟨⟨(inv (f.op)).unop, ⟨quiver.hom.op_inj (by simp), quiver.hom.op_inj (by simp)⟩⟩⟩ lemma is_iso_op_iff {X Y : C} (f : X ⟶ Y) : is_iso f.op ↔ is_iso f := ⟨λ hf, by exactI is_iso_of_op _, λ hf, by exactI infer_instance⟩ lemma is_iso_unop_iff {X Y : Cᵒᵖ} (f : X ⟶ Y) : is_iso f.unop ↔ is_iso f := by rw [← is_iso_op_iff f.unop, quiver.hom.op_unop] instance is_iso_unop {X Y : Cᵒᵖ} (f : X ⟶ Y) [is_iso f] : is_iso f.unop := (is_iso_unop_iff _).2 infer_instance @[simp] lemma op_inv {X Y : C} (f : X ⟶ Y) [is_iso f] : (inv f).op = inv f.op := by { ext, rw [← op_comp, is_iso.inv_hom_id, op_id] } @[simp] lemma unop_inv {X Y : Cᵒᵖ} (f : X ⟶ Y) [is_iso f] : (inv f).unop = inv f.unop := by { ext, rw [← unop_comp, is_iso.inv_hom_id, unop_id] } namespace functor section variables {D : Type u₂} [category.{v₂} D] variables {C D} /-- The opposite of a functor, i.e. considering a functor `F : C ⥤ D` as a functor `Cᵒᵖ ⥤ Dᵒᵖ`. In informal mathematics no distinction is made between these. -/ @[simps] protected def op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (unop X)), map := λ X Y f, (F.map f.unop).op } /-- Given a functor `F : Cᵒᵖ ⥤ Dᵒᵖ` we can take the "unopposite" functor `F : C ⥤ D`. In informal mathematics no distinction is made between these. -/ @[simps] protected def unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D := { obj := λ X, unop (F.obj (op X)), map := λ X Y f, (F.map f.op).unop } /-- The isomorphism between `F.op.unop` and `F`. -/ @[simps] def op_unop_iso (F : C ⥤ D) : F.op.unop ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- The isomorphism between `F.unop.op` and `F`. -/ @[simps] def unop_op_iso (F : Cᵒᵖ ⥤ Dᵒᵖ) : F.unop.op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) variables (C D) /-- Taking the opposite of a functor is functorial. -/ @[simps] def op_hom : (C ⥤ D)ᵒᵖ ⥤ (Cᵒᵖ ⥤ Dᵒᵖ) := { obj := λ F, (unop F).op, map := λ F G α, { app := λ X, (α.unop.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj (α.unop.naturality f.unop).symm } } /-- Take the "unopposite" of a functor is functorial. -/ @[simps] def op_inv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ := { obj := λ F, op F.unop, map := λ F G α, quiver.hom.op { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj $ (α.naturality f.op).symm } } variables {C D} /-- Another variant of the opposite of functor, turning a functor `C ⥤ Dᵒᵖ` into a functor `Cᵒᵖ ⥤ D`. In informal mathematics no distinction is made. -/ @[simps] protected def left_op (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D := { obj := λ X, unop (F.obj (unop X)), map := λ X Y f, (F.map f.unop).unop } /-- Another variant of the opposite of functor, turning a functor `Cᵒᵖ ⥤ D` into a functor `C ⥤ Dᵒᵖ`. In informal mathematics no distinction is made. -/ @[simps] protected def right_op (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (op X)), map := λ X Y f, (F.map f.op).op } instance {F : C ⥤ D} [full F] : full F.op := { preimage := λ X Y f, (F.preimage f.unop).op } instance {F : C ⥤ D} [faithful F] : faithful F.op := { map_injective' := λ X Y f g h, quiver.hom.unop_inj $ by simpa using map_injective F (quiver.hom.op_inj h) } /-- If F is faithful then the right_op of F is also faithful. -/ instance right_op_faithful {F : Cᵒᵖ ⥤ D} [faithful F] : faithful F.right_op := { map_injective' := λ X Y f g h, quiver.hom.op_inj (map_injective F (quiver.hom.op_inj h)) } /-- If F is faithful then the left_op of F is also faithful. -/ instance left_op_faithful {F : C ⥤ Dᵒᵖ} [faithful F] : faithful F.left_op := { map_injective' := λ X Y f g h, quiver.hom.unop_inj (map_injective F (quiver.hom.unop_inj h)) } /-- The isomorphism between `F.left_op.right_op` and `F`. -/ @[simps] def left_op_right_op_iso (F : C ⥤ Dᵒᵖ) : F.left_op.right_op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- The isomorphism between `F.right_op.left_op` and `F`. -/ @[simps] def right_op_left_op_iso (F : Cᵒᵖ ⥤ D) : F.right_op.left_op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- Whenever possible, it is advisable to use the isomorphism `right_op_left_op_iso` instead of this equality of functors. -/ lemma right_op_left_op_eq (F : Cᵒᵖ ⥤ D) : F.right_op.left_op = F := by { cases F, refl, } end end functor namespace nat_trans variables {D : Type u₂} [category.{v₂} D] section variables {F G : C ⥤ D} /-- The opposite of a natural transformation. -/ @[simps] protected def op (α : F ⟶ G) : G.op ⟶ F.op := { app := λ X, (α.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj (by simp) } @[simp] lemma op_id (F : C ⥤ D) : nat_trans.op (𝟙 F) = 𝟙 (F.op) := rfl /-- The "unopposite" of a natural transformation. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) : G.unop ⟶ F.unop := { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj (by simp) } @[simp] lemma unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : nat_trans.unop (𝟙 F) = 𝟙 (F.unop) := rfl /-- Given a natural transformation `α : F.op ⟶ G.op`, we can take the "unopposite" of each component obtaining a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_op (α : F.op ⟶ G.op) : G ⟶ F := { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj $ by simpa only [functor.op_map] using (α.naturality f.op).symm } @[simp] lemma remove_op_id (F : C ⥤ D) : nat_trans.remove_op (𝟙 F.op) = 𝟙 F := rfl /-- Given a natural transformation `α : F.unop ⟶ G.unop`, we can take the opposite of each component obtaining a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F.unop ⟶ G.unop) : G ⟶ F := { app := λ X, (α.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj $ by simpa only [functor.unop_map] using (α.naturality f.unop).symm } @[simp] lemma remove_unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : nat_trans.remove_unop (𝟙 F.unop) = 𝟙 F := rfl end section variables {F G H : C ⥤ Dᵒᵖ} /-- Given a natural transformation `α : F ⟶ G`, for `F G : C ⥤ Dᵒᵖ`, taking `unop` of each component gives a natural transformation `G.left_op ⟶ F.left_op`. -/ @[simps] protected def left_op (α : F ⟶ G) : G.left_op ⟶ F.left_op := { app := λ X, (α.app (unop X)).unop, naturality' := λ X Y f, quiver.hom.op_inj (by simp) } @[simp] lemma left_op_id : (𝟙 F : F ⟶ F).left_op = 𝟙 F.left_op := rfl @[simp] lemma left_op_comp (α : F ⟶ G) (β : G ⟶ H) : (α ≫ β).left_op = β.left_op ≫ α.left_op := rfl /-- Given a natural transformation `α : F.left_op ⟶ G.left_op`, for `F G : C ⥤ Dᵒᵖ`, taking `op` of each component gives a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_left_op (α : F.left_op ⟶ G.left_op) : G ⟶ F := { app := λ X, (α.app (op X)).op, naturality' := λ X Y f, quiver.hom.unop_inj $ by simpa only [functor.left_op_map] using (α.naturality f.op).symm } @[simp] lemma remove_left_op_id : nat_trans.remove_left_op (𝟙 F.left_op) = 𝟙 F := rfl end section variables {F G H : Cᵒᵖ ⥤ D} /-- Given a natural transformation `α : F ⟶ G`, for `F G : Cᵒᵖ ⥤ D`, taking `op` of each component gives a natural transformation `G.right_op ⟶ F.right_op`. -/ @[simps] protected def right_op (α : F ⟶ G) : G.right_op ⟶ F.right_op := { app := λ X, (α.app _).op, naturality' := λ X Y f, quiver.hom.unop_inj (by simp) } @[simp] lemma right_op_id : (𝟙 F : F ⟶ F).right_op = 𝟙 F.right_op := rfl @[simp] lemma right_op_comp (α : F ⟶ G) (β : G ⟶ H) : (α ≫ β).right_op = β.right_op ≫ α.right_op := rfl /-- Given a natural transformation `α : F.right_op ⟶ G.right_op`, for `F G : Cᵒᵖ ⥤ D`, taking `unop` of each component gives a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_right_op (α : F.right_op ⟶ G.right_op) : G ⟶ F := { app := λ X, (α.app X.unop).unop, naturality' := λ X Y f, quiver.hom.op_inj $ by simpa only [functor.right_op_map] using (α.naturality f.unop).symm } @[simp] lemma remove_right_op_id : nat_trans.remove_right_op (𝟙 F.right_op) = 𝟙 F := rfl end end nat_trans namespace iso variables {X Y : C} /-- The opposite isomorphism. -/ @[simps] protected def op (α : X ≅ Y) : op Y ≅ op X := { hom := α.hom.op, inv := α.inv.op, hom_inv_id' := quiver.hom.unop_inj α.inv_hom_id, inv_hom_id' := quiver.hom.unop_inj α.hom_inv_id } /-- The isomorphism obtained from an isomorphism in the opposite category. -/ @[simps] def unop {X Y : Cᵒᵖ} (f : X ≅ Y) : Y.unop ≅ X.unop := { hom := f.hom.unop, inv := f.inv.unop, hom_inv_id' := by simp only [← unop_comp, f.inv_hom_id, unop_id], inv_hom_id' := by simp only [← unop_comp, f.hom_inv_id, unop_id] } @[simp] lemma unop_op {X Y : Cᵒᵖ} (f : X ≅ Y) : f.unop.op = f := by ext; refl @[simp] lemma op_unop {X Y : C} (f : X ≅ Y) : f.op.unop = f := by ext; refl end iso namespace nat_iso variables {D : Type u₂} [category.{v₂} D] variables {F G : C ⥤ D} /-- The natural isomorphism between opposite functors `G.op ≅ F.op` induced by a natural isomorphism between the original functors `F ≅ G`. -/ @[simps] protected def op (α : F ≅ G) : G.op ≅ F.op := { hom := nat_trans.op α.hom, inv := nat_trans.op α.inv, hom_inv_id' := begin ext, dsimp, rw ←op_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←op_comp, rw α.hom_inv_id_app, refl, end } /-- The natural isomorphism between functors `G ≅ F` induced by a natural isomorphism between the opposite functors `F.op ≅ G.op`. -/ @[simps] protected def remove_op (α : F.op ≅ G.op) : G ≅ F := { hom := nat_trans.remove_op α.hom, inv := nat_trans.remove_op α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end } /-- The natural isomorphism between functors `G.unop ≅ F.unop` induced by a natural isomorphism between the original functors `F ≅ G`. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ≅ G) : G.unop ≅ F.unop := { hom := nat_trans.unop α.hom, inv := nat_trans.unop α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end } end nat_iso namespace equivalence variables {D : Type u₂} [category.{v₂} D] /-- An equivalence between categories gives an equivalence between the opposite categories. -/ @[simps] def op (e : C ≌ D) : Cᵒᵖ ≌ Dᵒᵖ := { functor := e.functor.op, inverse := e.inverse.op, unit_iso := (nat_iso.op e.unit_iso).symm, counit_iso := (nat_iso.op e.counit_iso).symm, functor_unit_iso_comp' := λ X, by { apply quiver.hom.unop_inj, dsimp, simp, }, } /-- An equivalence between opposite categories gives an equivalence between the original categories. -/ @[simps] def unop (e : Cᵒᵖ ≌ Dᵒᵖ) : C ≌ D := { functor := e.functor.unop, inverse := e.inverse.unop, unit_iso := (nat_iso.unop e.unit_iso).symm, counit_iso := (nat_iso.unop e.counit_iso).symm, functor_unit_iso_comp' := λ X, by { apply quiver.hom.op_inj, dsimp, simp, }, } end equivalence /-- The equivalence between arrows of the form `A ⟶ B` and `B.unop ⟶ A.unop`. Useful for building adjunctions. Note that this (definitionally) gives variants ``` def op_equiv' (A : C) (B : Cᵒᵖ) : (opposite.op A ⟶ B) ≃ (B.unop ⟶ A) := op_equiv _ _ def op_equiv'' (A : Cᵒᵖ) (B : C) : (A ⟶ opposite.op B) ≃ (B ⟶ A.unop) := op_equiv _ _ def op_equiv''' (A B : C) : (opposite.op A ⟶ opposite.op B) ≃ (B ⟶ A) := op_equiv _ _ ``` -/ @[simps] def op_equiv (A B : Cᵒᵖ) : (A ⟶ B) ≃ (B.unop ⟶ A.unop) := { to_fun := λ f, f.unop, inv_fun := λ g, g.op, left_inv := λ _, rfl, right_inv := λ _, rfl } instance subsingleton_of_unop (A B : Cᵒᵖ) [subsingleton (unop B ⟶ unop A)] : subsingleton (A ⟶ B) := (op_equiv A B).subsingleton instance decidable_eq_of_unop (A B : Cᵒᵖ) [decidable_eq (unop B ⟶ unop A)] : decidable_eq (A ⟶ B) := (op_equiv A B).decidable_eq /-- The equivalence between isomorphisms of the form `A ≅ B` and `B.unop ≅ A.unop`. Note this is definitionally the same as the other three variants: * `(opposite.op A ≅ B) ≃ (B.unop ≅ A)` * `(A ≅ opposite.op B) ≃ (B ≅ A.unop)` * `(opposite.op A ≅ opposite.op B) ≃ (B ≅ A)` -/ @[simps] def iso_op_equiv (A B : Cᵒᵖ) : (A ≅ B) ≃ (B.unop ≅ A.unop) := { to_fun := λ f, f.unop, inv_fun := λ g, g.op, left_inv := λ _, by { ext, refl, }, right_inv := λ _, by { ext, refl, } } namespace functor variables (C) variables (D : Type u₂) [category.{v₂} D] /-- The equivalence of functor categories induced by `op` and `unop`. -/ @[simps] def op_unop_equiv : (C ⥤ D)ᵒᵖ ≌ Cᵒᵖ ⥤ Dᵒᵖ := { functor := op_hom _ _, inverse := op_inv _ _, unit_iso := nat_iso.of_components (λ F, F.unop.op_unop_iso.op) begin intros F G f, dsimp [op_unop_iso], rw [(show f = f.unop.op, by simp), ← op_comp, ← op_comp], congr' 1, tidy, end, counit_iso := nat_iso.of_components (λ F, F.unop_op_iso) (by tidy) }. /-- The equivalence of functor categories induced by `left_op` and `right_op`. -/ @[simps] def left_op_right_op_equiv : (Cᵒᵖ ⥤ D)ᵒᵖ ≌ (C ⥤ Dᵒᵖ) := { functor := { obj := λ F, F.unop.right_op, map := λ F G η, η.unop.right_op }, inverse := { obj := λ F, op F.left_op, map := λ F G η, η.left_op.op }, unit_iso := nat_iso.of_components (λ F, F.unop.right_op_left_op_iso.op) begin intros F G η, dsimp, rw [(show η = η.unop.op, by simp), ← op_comp, ← op_comp], congr' 1, tidy, end, counit_iso := nat_iso.of_components (λ F, F.left_op_right_op_iso) (by tidy) } end functor end category_theory
45023d3a5837696791d9af9464c996dfa522e607
c9ba4946202cfd1e2586e71960dfed00503dcdf4
/src/meta_k/meta_pattern.lean
a212b4f525965e2c1e9c56a8fa10b148239ac28c
[]
no_license
ammkrn/learning_semantics_of_k
f55f669b369e32ef8407c16521b21ac5c106dc4d
c1487b538e1decc0f1fd389cd36bc36d2da012ab
refs/heads/master
1,588,081,593,954
1,552,449,093,000
1,552,449,093,000
175,315,800
0
0
null
null
null
null
UTF-8
Lean
false
false
15,037
lean
import .meta_sort import .meta_var import .meta_symbol open nat open has_sort -- ######################################################### -- ##### meta_pattern ################################ inductive meta_pattern : Type | meta_variable_as_pattern : #Variable → meta_pattern -- ?? | meta_application : #Symbol → list meta_pattern → meta_pattern | meta_and : #Sort → meta_pattern → meta_pattern → meta_pattern | meta_not : #Sort → meta_pattern → meta_pattern | meta_exsts : #Sort → #Variable → meta_pattern → meta_pattern instance : decidable_eq meta_pattern := by tactic.mk_dec_eq_instance open meta_pattern notation `#Pattern` := meta_pattern notation `#variableAsPattern` := meta_variable_as_pattern notation `#PatternList` := list meta_pattern notation `#apply` σ . L := meta_application σ L notation `#and` := meta_and notation φ `#∧` s `;` ψ := meta_and s φ ψ notation `#∃` s `,` v `:`s `.` φ := meta_exsts s (meta_variable.mk v s) φ notation `#∃` s `,` v `.` φ := meta_exsts s v φ notation `#∃*` v `.` φ := meta_exsts (getSort v) v φ notation `#¬` := meta_not notation `#¬` := meta_not -- φ₁ ∨ φ₂ ≣ ¬ (¬ φ₁ ∧ ¬ φ₂) --| s φ ψ := (#¬s φ) #∧s; (#¬s ψ) notation φ `#∨` s `;` ψ := meta_not s (meta_and s (meta_not s φ) (meta_not s ψ)) -- φ → ψ ≣ ¬ φ ∨ ψ notation φ `~` s `~>` ψ := (#¬s φ) #∨ s; (ψ) --notation φ `~` s `~>` ψ := --| s φ ψ := meta_and (s) (meta_implies (s) (φ) (ψ)) (meta_implies (s) (ψ) (φ)) -- φ ↔ ψ ≣ φ →s ψ ∧s ψ →s φ notation φ `<~>` s `;` ψ := (φ ~s~> ψ) #∧s; (ψ ~s~> φ) notation `#∀` s `,` name `:` sort `.` pat := #¬s (#∃s, name : sort . pat) notation `#∀` s `,` v `.` φ := #¬s (#∃s, v . #¬s φ) notation `#∀` v `:` s `.` φ := #∀ (getSort v) , v : s . φ -- #'ceil is defined as a symbol. -- ⌈_⌉ (s1, s2) ∈ Σ s1, s2 def meta_'ceil : #Sort → #Sort → #Symbol | s1 s2 := ⟨ "#'ceil", [s1, s2], [s1], s2 ⟩ notation `#'ceil` (s1, s2) := meta_'ceil s1 s2 notation `⌈` φ `⌉` (s1, s2) := #apply (meta_'ceil s1 s2) . [φ] notation `⌊` φ `⌋` (s1, s2) := #¬s2 ⌈#¬s1 φ⌉ (s1, s2) notation φ1 `#=` (s1, s2) `;` φ2 := ⌊ ((φ1) <~> s1; (φ2)) ⌋ (s1, s2) notation φ1 `¬#=` (s1, s2) `;` φ2 := meta_not s1 (⌊ ((φ1) <~> s1; (φ2)) ⌋ (s1, s2)) notation φ `#∈` (s1, s2) `;` ψ := ⌈(φ #∧s1; ψ)⌉ (s1, s2) --notation φ `#∈` (s1, s2) `;` ψ := ⌈meta_and s1 φ ψ⌉ (s1, s2) notation `#⊤` s := meta_exsts s (#variable "⊤" s) (#variableAsPattern (#variable "⊤" s)) notation `#⊥` s := #¬s (#⊤s) open meta_pattern definition meta_var_elems_to_pat : string → meta_sort → meta_pattern | str sort := #variableAsPattern (meta_variable.mk str sort) definition meta_get_pattern_sort : meta_pattern → meta_sort | (meta_variable_as_pattern v) := getSort v | (meta_application (σ) (Sigma)) := getSort σ | (meta_and s φ₁ φ₂) := s | (meta_not s φ) := s | (meta_exsts s v φ) := s -- instance meta_pattern.has_sort : has_sort meta_pattern meta_sort := ⟨ meta_get_pattern_sort ⟩ -- ################ Auxiliary stuff for ⊤ -- FIXME The naming of the variable for ⊤ is just so the pretty printer will -- display it as ⊤ and not as an exists pattern. def meta_btop : bool → (#Sort → #Pattern) | tt s := #⊤s | ff s := #⊥s def meta_is_top : meta_pattern → bool | (meta_exsts _ (#variable "⊤" _) (#variableAsPattern (#variable "⊤" _))) := tt | _ := ff def meta_is_bottom : meta_pattern → bool | (meta_not _ φ) := if meta_is_top φ then tt else ff | _ := ff def pat_conj : meta_pattern → meta_pattern → bool | φ1 φ2 := (meta_is_top φ1) && (meta_is_top φ2) infix `#&&`: 90 := pat_conj mutual def meta_pattern_to_string, meta_pattern_list_to_string with meta_pattern_to_string : meta_pattern → string | (#variableAsPattern (v)) := "(var : " ++ (repr v) ++ ")" | (meta_application σ L) := "(#apply " ++ (repr σ) ++ " . (" ++ (meta_pattern_list_to_string L) ++ ")" ++ ")" | (meta_and (s) (φ1) (φ2)) := "(" ++ (meta_pattern_to_string φ1) ++ " ∧" ++ (repr s) ++ " " ++ (meta_pattern_to_string φ2) ++ ")" | (meta_not (s) (φ)) := if meta_is_top φ then ("( ⊥" ++ (repr (getSort φ))) ++ " )" else "( ¬" ++ (repr s) ++ " " ++ (meta_pattern_to_string φ) ++ " )" | (meta_exsts (s) (v) (φ)) := if (meta_is_top (meta_exsts s v φ)) then ("( ⊤" ++ repr s) ++ " )" else "(∃" ++ (repr s) ++ ", (" ++ (repr v) ++ ":" ++ (repr (getSort v)) ++ ") " ++ " . " ++ (meta_pattern_to_string φ) ++ " )" with meta_pattern_list_to_string : list meta_pattern → string | [] := "" | (hd :: tl) := (meta_pattern_to_string hd) ++ ", " ++ meta_pattern_list_to_string tl instance : has_repr meta_pattern := ⟨ meta_pattern_to_string ⟩ -- FIXME delete if poly version works --def delete_sort_list : #Sort → #SortList → #SortList --| s [] := [] --| s (hd :: tl) := if s = hd then delete_sort_list s tl else hd :: delete_sort_list s tl -- FIXME why is this here -- replace equality -- FIXME delete? ----symbols (3/7) --def meta_get_fv_pred : #Symbol := #symbol "#meta_get_fv" [] [%PatternList] %VariableList --def meta_get_fv_from_patterns_pred : #Symbol := #symbol "#meta_get_fv_from_patterns" [] [%PatternList] %VariableList -- #apply meta_get_fv . [φ1, φ2, ...] -- This definition is based on the axioms outlined on p. 33 -- The interesting case is the existential quantifier, which acts -- as variable binding, and therefore removes the bound variable -- from the eventual list of free variables. mutual def meta_get_fv, meta_get_fv_from_patterns with meta_get_fv : #Pattern → #VariableList | (#variableAsPattern v) := [v] | (meta_application σ L) := meta_get_fv_from_patterns L | (meta_and s φ1 φ2) := meta_get_fv φ1 ++ meta_get_fv φ2 | (meta_not s φ) := meta_get_fv φ | (meta_exsts s v φ) := delete_variable_list v (meta_get_fv φ) with meta_get_fv_from_patterns : #PatternList → #VariableList | [] := [] | (hd :: tl) := meta_get_fv hd ++ meta_get_fv_from_patterns tl def meta_occurs_free : #Sort → #Variable → #Pattern → #Pattern | carrier v φ := meta_btop (list.mem v (meta_get_fv φ)) carrier -- 3/7 def longest_string : list #String → #String | l := list.foldl (λ s : #String, (λ acc : #String, if string.length (s) > string.length (acc) then s else acc)) ("") (l) def meta_fresh_name : #PatternList → #String | l := let longest_varname := longest_string (list.map get_meta_variable_name (meta_get_fv_from_patterns (l))) in (longest_varname ++ "_a") definition unwrap : option #Pattern → #Pattern | none := #⊥ %Sort | (some φ) := φ -- a decidable definition of variable substitution; uses a gas parameter to ease termination -- proof, and is wrapped in an option to flag termination via 'out of gas' --φ [ψ / v] definition substitute : ℕ → #Pattern → #Pattern → #Variable → option #Pattern | zero a b c := a | (succ n) (#variableAsPattern (u)) (ψ) (v) := if u = v then some ψ else some (#variableAsPattern (u)) --| (succ n) (meta_application (σ) (L)) ψ v := #apply σ . (list.map (λ p : meta_pattern, substitute n p ψ v) L) | (succ n) (meta_application (σ) (L)) ψ v := let mapped : list (option #Pattern) := list.map (λ p : meta_pattern, substitute n p ψ v) L, terminated : bool := list.any mapped (λ x : option #Pattern, x = none), unwrapped : #PatternList := list.map(λ p : option #Pattern, unwrap p) mapped in if terminated then none else some (#apply σ . unwrapped) | (succ n) (meta_and (s) (φ1) (φ2)) (ψ) (v) := let lhs : option #Pattern := substitute n φ1 ψ v, rhs : option #Pattern := substitute n φ2 ψ v in match (lhs, rhs) with (none, none) := none, (some l, none) := none, (none, some r) := none, (some l, some r) := some (l #∧s; r) end | (succ n) (meta_not (s) (φ)) (ψ) (v) := let mapped : option #Pattern := substitute n φ ψ v in match mapped with (some φ') := some (#¬s φ'), (none) := none end | (succ n) (meta_exsts (s') (x) φ) (ψ) (v) := let x'_var := meta_variable.mk (meta_fresh_name [φ, ψ, #variableAsPattern v]) s', x'_pat := #variableAsPattern x'_var, mapped_one : option #Pattern := substitute (n) (φ) (x'_pat) (x) in match mapped_one with none := none, (some φ') := let mapped_two : option #Pattern := substitute (n) (φ') (ψ) (v) in match mapped_two with none := none, (some φ'') := some (#∃s', (x'_var) . (x'_pat) #∧s'; #∃s', (x'_var) . (φ'')) end end -- in meta_exsts (s') (x'_var) /-such that-/ (meta_and (s') (x'_pat) (meta_exsts (s') (x'_var) (sub_two))) -- gas parameter is made implicit by giving some default amount of gas. definition meta_substitute_i : #Pattern → #Pattern → #Variable → option #Pattern := substitute 100 -- subst_rel v φ ψ τ ; the result of the substitution of ψ for all -- occurences of v in φ。 -- This is modeled as v → original pattern → substituting pattern → substitution result. -- So for the assertion ' the pattern τ properly represents substitution of ψ for v -- in pattern φ, you would prove 'subst v φ ψ τ' for the appropriate construction. mutual inductive subst, subst_list with subst : #Variable → #Pattern → #Pattern → #Pattern → Prop | application : ∀ {σ : #Symbol} {L ζ : #PatternList} {φ ψ : #Pattern} {v : #Variable}, subst_list v L ψ ζ → subst v (#apply σ . L) ψ (#apply σ . ζ) | and : ∀ {s : #Sort} {φ1 φ2 τ1 τ2 ψ: #Pattern} {v : #Variable}, subst v φ1 ψ (τ1) → subst v φ2 ψ (τ2) → subst v (φ1 #∧s; φ2) ψ (τ1 #∧s; τ2) | not : ∀ {s : #Sort} {φ ψ τ : #Pattern} {v : #Variable}, subst v φ ψ τ → subst v (#¬s φ) ψ (#¬s τ) | var_eq : ∀ {v : #Variable} {φ ψ : #Pattern}, φ = (#variableAsPattern v) → subst v φ ψ ψ | var_neq : ∀ {v : #Variable} {φ ψ : #Pattern}, (∃ u, #variableAsPattern u = φ ∧ v ≠ u) → subst v φ ψ φ | exsts : ∀ {s s' : #Sort} {v : #Variable} {φ ψ τ : #Pattern}, -- if the substitution φ [ x':s / x:s ] is properly represented in τ, subst (#variable "x" s) φ (#variableAsPattern (#variable "x'" s)) τ -- and the substitution φ [ ψ / v ] is properly represented in τ, → subst (v) φ ψ τ -- then the big conjuctive substitution result is good. → subst v (#∃s', "x":s . φ) ψ (#∃s', " x' ":s . (#variableAsPattern $ #variable (meta_fresh_name [φ, ψ, (#variableAsPattern v)]) (s)) #∧s; (#∃s', " x' ":s . τ)) with subst_list : #Variable → list #Pattern → #Pattern → list #Pattern → Prop | nil : ∀ {v : #Variable} {ψ τ : #Pattern}, subst_list v [] ψ [] | cons : ∀ {v : #Variable} {L ζ: list #Pattern} {φ ψ τ : #Pattern} , subst_list v L ψ ζ → subst v φ ψ τ → subst_list v (φ :: L) ψ (τ :: ζ) -- This one is subtle; the second relationship of #sc{#s} φ' φ can chase -- nested context lists forever since it has a sort of transitivity; -- if φ' ≠ φ, but φ' is a (σ L) construction -- that holds some symbol matching φ, #sc{#s} of ψ φ is still satisfied (eventually). -- The left hand side of the original conjunction can be left -- out since membership in L implicit in the function's construction. mutual def meta_sc, meta_sc_l with meta_sc : #Sort → #Pattern → #Pattern → #Pattern | carrier (φ) (#apply σ . L) := meta_sc_l carrier φ L | carrier (φ) (ψ) := if φ = ψ then #⊤ carrier else #⊥ carrier with meta_sc_l : #Sort → #Pattern → #PatternList → #Pattern | carrier φ [] := #⊥ carrier | carrier (φ) (hd :: tl) := have 2 < 1 + (1 + (1 + list.sizeof tl)), by { intros, generalize : (list.sizeof tl = n), simp, apply nat.lt_add_left, apply nat.lt.base, }, have 3 < 1 + (1 + (1 + (1 + list.sizeof tl))), by { intros, generalize : (list.sizeof tl = n), simp, apply nat.lt_add_left, apply nat.lt.base,}, if (meta_sc carrier φ hd = #⊤ carrier) then #⊤ carrier else (meta_sc_l carrier φ tl) -- ####### Coercions/Lifts -- #Sort → #Pattern def meta_sort.to_meta_pattern : #Sort → #Pattern | (#sort name params) := #variableAsPattern (#variable name %Sort) instance meta_sort_to_meta_pattern_lift : has_lift #Sort #Pattern := ⟨ meta_sort.to_meta_pattern ⟩ -- #SortList → #Pattern def meta_sort_list.to_meta_pattern_list : #SortList → #PatternList | L := list.map (λ s, lift s) L instance meta_sort_list_to_meta_pattern_list_lift : has_lift #SortList #PatternList := ⟨ meta_sort_list.to_meta_pattern_list ⟩ -- #Symbol → #Pattern def meta_symbol.to_meta_pattern : #Symbol → #Pattern | σ := #apply σ . [] instance meta_symbol_to_meta_pattern_lift : has_lift #Symbol #Pattern := ⟨ meta_symbol.to_meta_pattern ⟩ -- #SymbolList → #PatternList def meta_symbol_list.to_meta_pattern_list : #SymbolList → #PatternList | L := list.map (λ σ : #Symbol, lift σ) L instance meta_symbol_list_to_meta_pattern_list_lift : has_lift #SymbolList #PatternList := ⟨ meta_symbol_list.to_meta_pattern_list ⟩ -- #Variable → #Pattern def meta_variable.to_meta_pattern_lift : #Variable → #Pattern | v := #variableAsPattern v instance meta_variable_to_meta_pattern_lift : has_lift #Variable #Pattern := ⟨ meta_variable.to_meta_pattern_lift ⟩ def meta_sort_variable.to_meta_pattern_lift : meta_sort_variable → #Pattern | (meta_sort_variable.mk str) := #variableAsPattern (#variable ("#" ++ str) (%Sort)) instance meta_sort_variable_to_meta_pattern_lift : has_lift meta_sort_variable #Pattern := ⟨ meta_sort_variable.to_meta_pattern_lift ⟩ def list_meta_sort.to_meta_pattern : #SortList → #Pattern | [] := #apply (#symbol "#nilSortList" [] [] %SortList) . [] | (hd :: tl) := #apply (#symbol "consSortList" [] [%Sort, %SortList] %SortList) . [list_meta_sort.to_meta_pattern tl] instance : has_lift (list meta_sort) (meta_pattern) := ⟨ list_meta_sort.to_meta_pattern ⟩ def list_meta_symbol.to_meta_pattern : #SymbolList → #Pattern | [] := #apply (#symbol "nilSymbolList" [] [] %SymbolList) . [] | (hd :: tl) := #apply (#symbol "consSymbolList" [] [%Symbol, %SymbolList] %SymbolList) . [list_meta_symbol.to_meta_pattern tl] instance list_meta_symbol.to_meta_pattern_lift : has_lift (list meta_symbol) meta_pattern := ⟨list_meta_symbol.to_meta_pattern⟩ instance : is_Pattern meta_pattern := ⟨ #Variable , #Sort , meta_is_top , meta_is_bottom , meta_get_fv , meta_get_fv_from_patterns , meta_occurs_free , meta_btop , meta_sc , meta_sc_l , meta_fresh_name , meta_substitute_i ⟩
481c1d9e4c1207f0e8f152500c46dbc9e425e25c
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/number_theory/ramification_inertia.lean
34e1b5a5596cbf414a6efb49551d778c3de69dd2
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
39,264
lean
/- Copyright (c) 2022 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.is_prime_pow import field_theory.separable import linear_algebra.free_module.finite.rank import linear_algebra.free_module.pid import linear_algebra.matrix.nonsingular_inverse import ring_theory.dedekind_domain.ideal import ring_theory.localization.module /-! # Ramification index and inertia degree Given `P : ideal S` lying over `p : ideal R` for the ring extension `f : R →+* S` (assuming `P` and `p` are prime or maximal where needed), the **ramification index** `ideal.ramification_idx f p P` is the multiplicity of `P` in `map f p`, and the **inertia degree** `ideal.inertia_deg f p P` is the degree of the field extension `(S / P) : (R / p)`. ## Main results The main theorem `ideal.sum_ramification_inertia` states that for all coprime `P` lying over `p`, `Σ P, ramification_idx f p P * inertia_deg f p P` equals the degree of the field extension `Frac(S) : Frac(R)`. ## Implementation notes Often the above theory is set up in the case where: * `R` is the ring of integers of a number field `K`, * `L` is a finite separable extension of `K`, * `S` is the integral closure of `R` in `L`, * `p` and `P` are maximal ideals, * `P` is an ideal lying over `p` We will try to relax the above hypotheses as much as possible. ## Notation In this file, `e` stands for the ramification index and `f` for the inertia degree of `P` over `p`, leaving `p` and `P` implicit. -/ namespace ideal universes u v variables {R : Type u} [comm_ring R] variables {S : Type v} [comm_ring S] (f : R →+* S) variables (p : ideal R) (P : ideal S) open finite_dimensional open unique_factorization_monoid section dec_eq open_locale classical /-- The ramification index of `P` over `p` is the largest exponent `n` such that `p` is contained in `P^n`. In particular, if `p` is not contained in `P^n`, then the ramification index is 0. If there is no largest such `n` (e.g. because `p = ⊥`), then `ramification_idx` is defined to be 0. -/ noncomputable def ramification_idx : ℕ := Sup {n | map f p ≤ P ^ n} variables {f p P} lemma ramification_idx_eq_find (h : ∃ n, ∀ k, map f p ≤ P ^ k → k ≤ n) : ramification_idx f p P = nat.find h := nat.Sup_def h lemma ramification_idx_eq_zero (h : ∀ n : ℕ, ∃ k, map f p ≤ P ^ k ∧ n < k) : ramification_idx f p P = 0 := dif_neg (by push_neg; exact h) lemma ramification_idx_spec {n : ℕ} (hle : map f p ≤ P ^ n) (hgt : ¬ map f p ≤ P ^ (n + 1)) : ramification_idx f p P = n := begin have : ∀ (k : ℕ), map f p ≤ P ^ k → k ≤ n, { intros k hk, refine le_of_not_lt (λ hnk, _), exact hgt (hk.trans (ideal.pow_le_pow hnk)) }, rw ramification_idx_eq_find ⟨n, this⟩, { refine le_antisymm (nat.find_min' _ this) (le_of_not_gt (λ (h : nat.find _ < n), _)), obtain this' := nat.find_spec ⟨n, this⟩, exact h.not_le (this' _ hle) }, end lemma ramification_idx_lt {n : ℕ} (hgt : ¬ (map f p ≤ P ^ n)) : ramification_idx f p P < n := begin cases n, { simpa using hgt }, rw nat.lt_succ_iff, have : ∀ k, map f p ≤ P ^ k → k ≤ n, { refine λ k hk, le_of_not_lt (λ hnk, _), exact hgt (hk.trans (ideal.pow_le_pow hnk)) }, rw ramification_idx_eq_find ⟨n, this⟩, exact nat.find_min' ⟨n, this⟩ this end @[simp] lemma ramification_idx_bot : ramification_idx f ⊥ P = 0 := dif_neg $ not_exists.mpr $ λ n hn, n.lt_succ_self.not_le (hn _ (by simp)) @[simp] lemma ramification_idx_of_not_le (h : ¬ map f p ≤ P) : ramification_idx f p P = 0 := ramification_idx_spec (by simp) (by simpa using h) lemma ramification_idx_ne_zero {e : ℕ} (he : e ≠ 0) (hle : map f p ≤ P ^ e) (hnle : ¬ map f p ≤ P ^ (e + 1)): ramification_idx f p P ≠ 0 := by rwa ramification_idx_spec hle hnle lemma le_pow_of_le_ramification_idx {n : ℕ} (hn : n ≤ ramification_idx f p P) : map f p ≤ P ^ n := begin contrapose! hn, exact ramification_idx_lt hn end lemma le_pow_ramification_idx : map f p ≤ P ^ ramification_idx f p P := le_pow_of_le_ramification_idx (le_refl _) lemma le_comap_pow_ramification_idx : p ≤ comap f (P ^ ramification_idx f p P) := map_le_iff_le_comap.mp le_pow_ramification_idx lemma le_comap_of_ramification_idx_ne_zero (h : ramification_idx f p P ≠ 0) : p ≤ comap f P := ideal.map_le_iff_le_comap.mp $ le_pow_ramification_idx.trans $ ideal.pow_le_self $ h namespace is_dedekind_domain variables [is_domain S] [is_dedekind_domain S] lemma ramification_idx_eq_normalized_factors_count (hp0 : map f p ≠ ⊥) (hP : P.is_prime) (hP0 : P ≠ ⊥) : ramification_idx f p P = (normalized_factors (map f p)).count P := begin have hPirr := (ideal.prime_of_is_prime hP0 hP).irreducible, refine ramification_idx_spec (ideal.le_of_dvd _) (mt ideal.dvd_iff_le.mpr _); rw [dvd_iff_normalized_factors_le_normalized_factors (pow_ne_zero _ hP0) hp0, normalized_factors_pow, normalized_factors_irreducible hPirr, normalize_eq, multiset.nsmul_singleton, ← multiset.le_count_iff_repeat_le], { exact (nat.lt_succ_self _).not_le }, end lemma ramification_idx_eq_factors_count (hp0 : map f p ≠ ⊥) (hP : P.is_prime) (hP0 : P ≠ ⊥) : ramification_idx f p P = (factors (map f p)).count P := by rw [is_dedekind_domain.ramification_idx_eq_normalized_factors_count hp0 hP hP0, factors_eq_normalized_factors] lemma ramification_idx_ne_zero (hp0 : map f p ≠ ⊥) (hP : P.is_prime) (le : map f p ≤ P) : ramification_idx f p P ≠ 0 := begin have hP0 : P ≠ ⊥, { unfreezingI { rintro rfl }, have := le_bot_iff.mp le, contradiction }, have hPirr := (ideal.prime_of_is_prime hP0 hP).irreducible, rw is_dedekind_domain.ramification_idx_eq_normalized_factors_count hp0 hP hP0, obtain ⟨P', hP', P'_eq⟩ := exists_mem_normalized_factors_of_dvd hp0 hPirr (ideal.dvd_iff_le.mpr le), rwa [multiset.count_ne_zero, associated_iff_eq.mp P'_eq], end end is_dedekind_domain variables (f p P) local attribute [instance] ideal.quotient.field /-- The inertia degree of `P : ideal S` lying over `p : ideal R` is the degree of the extension `(S / P) : (R / p)`. We do not assume `P` lies over `p` in the definition; we return `0` instead. See `inertia_deg_algebra_map` for the common case where `f = algebra_map R S` and there is an algebra structure `R / p → S / P`. -/ noncomputable def inertia_deg [hp : p.is_maximal] : ℕ := if hPp : comap f P = p then @finrank (R ⧸ p) (S ⧸ P) _ _ $ @algebra.to_module _ _ _ _ $ ring_hom.to_algebra $ ideal.quotient.lift p (P^.quotient.mk^.comp f) $ λ a ha, quotient.eq_zero_iff_mem.mpr $ mem_comap.mp $ hPp.symm ▸ ha else 0 -- Useful for the `nontriviality` tactic using `comap_eq_of_scalar_tower_quotient`. @[simp] lemma inertia_deg_of_subsingleton [hp : p.is_maximal] [hQ : subsingleton (S ⧸ P)] : inertia_deg f p P = 0 := begin have := ideal.quotient.subsingleton_iff.mp hQ, unfreezingI { subst this }, exact dif_neg (λ h, hp.ne_top $ h.symm.trans comap_top) end @[simp] lemma inertia_deg_algebra_map [algebra R S] [algebra (R ⧸ p) (S ⧸ P)] [is_scalar_tower R (R ⧸ p) (S ⧸ P)] [hp : p.is_maximal] : inertia_deg (algebra_map R S) p P = finrank (R ⧸ p) (S ⧸ P) := begin nontriviality (S ⧸ P) using [inertia_deg_of_subsingleton, finrank_zero_of_subsingleton], have := comap_eq_of_scalar_tower_quotient (algebra_map (R ⧸ p) (S ⧸ P)).injective, rw [inertia_deg, dif_pos this], congr, refine algebra.algebra_ext _ _ (λ x', quotient.induction_on' x' $ λ x, _), change ideal.quotient.lift p _ _ (ideal.quotient.mk p x) = algebra_map _ _ (ideal.quotient.mk p x), rw [ideal.quotient.lift_mk, ← ideal.quotient.algebra_map_eq, ← is_scalar_tower.algebra_map_eq, ← ideal.quotient.algebra_map_eq, ← is_scalar_tower.algebra_map_apply] end end dec_eq section finrank_quotient_map open_locale big_operators open_locale non_zero_divisors variables [algebra R S] variables {K : Type*} [field K] [algebra R K] [hRK : is_fraction_ring R K] variables {L : Type*} [field L] [algebra S L] [is_fraction_ring S L] variables {V V' V'' : Type*} variables [add_comm_group V] [module R V] [module K V] [is_scalar_tower R K V] variables [add_comm_group V'] [module R V'] [module S V'] [is_scalar_tower R S V'] variables [add_comm_group V''] [module R V''] variables (K) include hRK /-- Let `V` be a vector space over `K = Frac(R)`, `S / R` a ring extension and `V'` a module over `S`. If `b`, in the intersection `V''` of `V` and `V'`, is linear independent over `S` in `V'`, then it is linear independent over `R` in `V`. The statement we prove is actually slightly more general: * it suffices that the inclusion `algebra_map R S : R → S` is nontrivial * the function `f' : V'' → V'` doesn't need to be injective -/ lemma finrank_quotient_map.linear_independent_of_nontrivial [is_domain R] [is_dedekind_domain R] (hRS : (algebra_map R S).ker ≠ ⊤) (f : V'' →ₗ[R] V) (hf : function.injective f) (f' : V'' →ₗ[R] V') {ι : Type*} {b : ι → V''} (hb' : linear_independent S (f' ∘ b)) : linear_independent K (f ∘ b) := begin contrapose! hb' with hb, -- Informally, if we have a nontrivial linear dependence with coefficients `g` in `K`, -- then we can find a linear dependence with coefficients `I.quotient.mk g'` in `R/I`, -- where `I = ker (algebra_map R S)`. -- We make use of the same principle but stay in `R` everywhere. simp only [linear_independent_iff', not_forall] at hb ⊢, obtain ⟨s, g, eq, j', hj's, hj'g⟩ := hb, use s, obtain ⟨a, hag, j, hjs, hgI⟩ := ideal.exist_integer_multiples_not_mem hRS s g hj's hj'g, choose g'' hg'' using hag, letI := classical.prop_decidable, let g' := λ i, if h : i ∈ s then g'' i h else 0, have hg' : ∀ i ∈ s, algebra_map _ _ (g' i) = a * g i, { intros i hi, exact (congr_arg _ (dif_pos hi)).trans (hg'' i hi) }, -- Because `R/I` is nontrivial, we can lift `g` to a nontrivial linear dependence in `S`. have hgI : algebra_map R S (g' j) ≠ 0, { simp only [fractional_ideal.mem_coe_ideal, not_exists, not_and'] at hgI, exact hgI _ (hg' j hjs) }, refine ⟨λ i, algebra_map R S (g' i), _, j, hjs, hgI⟩, have eq : f (∑ i in s, g' i • (b i)) = 0, { rw [linear_map.map_sum, ← smul_zero a, ← eq, finset.smul_sum, finset.sum_congr rfl], intros i hi, rw [linear_map.map_smul, ← is_scalar_tower.algebra_map_smul K, hg' i hi, ← smul_assoc, smul_eq_mul], apply_instance }, simp only [is_scalar_tower.algebra_map_smul, ← linear_map.map_smul, ← linear_map.map_sum, (f.map_eq_zero_iff hf).mp eq, linear_map.map_zero], end open_locale matrix variables {K} omit hRK /-- If `b` mod `p` spans `S/p` as `R/p`-space, then `b` itself spans `Frac(S)` as `K`-space. Here, * `p` is an ideal of `R` such that `R / p` is nontrivial * `K` is a field that has an embedding of `R` (in particular we can take `K = Frac(R)`) * `L` is a field extension of `K` * `S` is the integral closure of `R` in `L` More precisely, we avoid quotients in this statement and instead require that `b ∪ pS` spans `S`. -/ lemma finrank_quotient_map.span_eq_top [is_domain R] [is_domain S] [algebra K L] [is_noetherian R S] [algebra R L] [is_scalar_tower R S L] [is_scalar_tower R K L] [is_integral_closure S R L] [no_zero_smul_divisors R K] (hp : p ≠ ⊤) (b : set S) (hb' : submodule.span R b ⊔ (p.map (algebra_map R S)).restrict_scalars R = ⊤) : submodule.span K (algebra_map S L '' b) = ⊤ := begin have hRL : function.injective (algebra_map R L), { rw is_scalar_tower.algebra_map_eq R K L, exact (algebra_map K L).injective.comp (no_zero_smul_divisors.algebra_map_injective R K) }, -- Let `M` be the `R`-module spanned by the proposed basis elements. set M : submodule R S := submodule.span R b with hM, -- Then `S / M` is generated by some finite set of `n` vectors `a`. letI h : module.finite R (S ⧸ M) := module.finite.of_surjective (submodule.mkq _) (submodule.quotient.mk_surjective _), obtain ⟨n, a, ha⟩ := @@module.finite.exists_fin _ _ _ h, -- Because the image of `p` in `S / M` is `⊤`, have smul_top_eq : p • (⊤ : submodule R (S ⧸ M)) = ⊤, { calc p • ⊤ = submodule.map M.mkq (p • ⊤) : by rw [submodule.map_smul'', submodule.map_top, M.range_mkq] ... = ⊤ : by rw [ideal.smul_top_eq_map, (submodule.map_mkq_eq_top M _).mpr hb'] }, -- we can write the elements of `a` as `p`-linear combinations of other elements of `a`. have exists_sum : ∀ x : (S ⧸ M), ∃ a' : fin n → R, (∀ i, a' i ∈ p) ∧ ∑ i, a' i • a i = x, { intro x, obtain ⟨a'', ha'', hx⟩ := (submodule.mem_ideal_smul_span_iff_exists_sum p a x).1 _, { refine ⟨λ i, a'' i, λ i, ha'' _, _⟩, rw [← hx, finsupp.sum_fintype], exact λ _, zero_smul _ _ }, { rw [ha, smul_top_eq], exact submodule.mem_top } }, choose A' hA'p hA' using λ i, exists_sum (a i), -- This gives us a(n invertible) matrix `A` such that `det A ∈ (M = span R b)`, let A : matrix (fin n) (fin n) R := A' - 1, let B := A.adjugate, have A_smul : ∀ i, ∑ j, A i j • a j = 0, { intros, simp only [A, pi.sub_apply, sub_smul, finset.sum_sub_distrib, hA', matrix.one_apply, ite_smul, one_smul, zero_smul, finset.sum_ite_eq, finset.mem_univ, if_true, sub_self] }, -- since `span S {det A} / M = 0`. have d_smul : ∀ i, A.det • a i = 0, { intro i, calc A.det • a i = ∑ j, (B ⬝ A) i j • a j : _ ... = ∑ k, B i k • ∑ j, (A k j • a j) : _ ... = 0 : finset.sum_eq_zero (λ k _, _), { simp only [matrix.adjugate_mul, pi.smul_apply, matrix.one_apply, mul_ite, ite_smul, smul_eq_mul, mul_one, mul_zero, one_smul, zero_smul, finset.sum_ite_eq, finset.mem_univ, if_true] }, { simp only [matrix.mul_apply, finset.smul_sum, finset.sum_smul, smul_smul], rw finset.sum_comm }, { rw [A_smul, smul_zero] } }, -- In the rings of integers we have the desired inclusion. have span_d : (submodule.span S ({algebra_map R S A.det} : set S)).restrict_scalars R ≤ M, { intros x hx, rw submodule.restrict_scalars_mem at hx, obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hx, rw [smul_eq_mul, mul_comm, ← algebra.smul_def] at ⊢ hx, rw [← submodule.quotient.mk_eq_zero, submodule.quotient.mk_smul], obtain ⟨a', _, quot_x_eq⟩ := exists_sum (submodule.quotient.mk x'), simp_rw [← quot_x_eq, finset.smul_sum, smul_comm A.det, d_smul, smul_zero, finset.sum_const_zero] }, -- So now we lift everything to the fraction field. refine top_le_iff.mp (calc ⊤ = (ideal.span {algebra_map R L A.det}).restrict_scalars K : _ ... ≤ submodule.span K (algebra_map S L '' b) : _), -- Because `det A ≠ 0`, we have `span L {det A} = ⊤`. { rw [eq_comm, submodule.restrict_scalars_eq_top_iff, ideal.span_singleton_eq_top], refine is_unit.mk0 _ ((map_ne_zero_iff ((algebra_map R L)) hRL).mpr ( @ne_zero_of_map _ _ _ _ _ _ (ideal.quotient.mk p) _ _)), haveI := ideal.quotient.nontrivial hp, calc ideal.quotient.mk p (A.det) = matrix.det ((ideal.quotient.mk p).map_matrix A) : by rw [ring_hom.map_det, ring_hom.map_matrix_apply] ... = matrix.det ((ideal.quotient.mk p).map_matrix (A' - 1)) : rfl ... = matrix.det (λ i j, (ideal.quotient.mk p) (A' i j) - (1 : matrix (fin n) (fin n) (R ⧸ p)) i j) : _ ... = matrix.det (-1 : matrix (fin n) (fin n) (R ⧸ p)) : _ ... = (-1 : R ⧸ p) ^ n : by rw [matrix.det_neg, fintype.card_fin, matrix.det_one, mul_one] ... ≠ 0 : is_unit.ne_zero (is_unit_one.neg.pow _), { refine congr_arg matrix.det (matrix.ext (λ i j, _)), rw [map_sub, ring_hom.map_matrix_apply, map_one], refl }, { refine congr_arg matrix.det (matrix.ext (λ i j, _)), rw [ideal.quotient.eq_zero_iff_mem.mpr (hA'p i j), zero_sub], refl } }, -- And we conclude `L = span L {det A} ≤ span K b`, so `span K b` spans everything. { intros x hx, rw [submodule.restrict_scalars_mem, is_scalar_tower.algebra_map_apply R S L] at hx, refine is_fraction_ring.ideal_span_singleton_map_subset R _ hRL span_d hx, haveI : no_zero_smul_divisors R L := no_zero_smul_divisors.of_algebra_map_injective hRL, rw ← is_fraction_ring.is_algebraic_iff' R S, intros x, exact is_integral.is_algebraic _ (is_integral_of_noetherian infer_instance _) }, end include hRK variables (K L) /-- If `p` is a maximal ideal of `R`, and `S` is the integral closure of `R` in `L`, then the dimension `[S/pS : R/p]` is equal to `[Frac(S) : Frac(R)]`. -/ lemma finrank_quotient_map [is_domain R] [is_domain S] [is_dedekind_domain R] [algebra K L] [algebra R L] [is_scalar_tower R K L] [is_scalar_tower R S L] [is_integral_closure S R L] [hp : p.is_maximal] [is_noetherian R S] : finrank (R ⧸ p) (S ⧸ map (algebra_map R S) p) = finrank K L := begin -- Choose an arbitrary basis `b` for `[S/pS : R/p]`. -- We'll use the previous results to turn it into a basis on `[Frac(S) : Frac(R)]`. letI : field (R ⧸ p) := ideal.quotient.field _, let ι := module.free.choose_basis_index (R ⧸ p) (S ⧸ map (algebra_map R S) p), let b : basis ι (R ⧸ p) (S ⧸ map (algebra_map R S) p) := module.free.choose_basis _ _, -- Namely, choose a representative `b' i : S` for each `b i : S / pS`. let b' : ι → S := λ i, (ideal.quotient.mk_surjective (b i)).some, have b_eq_b' : ⇑ b = (submodule.mkq _).restrict_scalars R ∘ b' := funext (λ i, (ideal.quotient.mk_surjective (b i)).some_spec.symm), -- We claim `b'` is a basis for `Frac(S)` over `Frac(R)` because it is linear independent -- and spans the whole of `Frac(S)`. let b'' : ι → L := algebra_map S L ∘ b', have b''_li : linear_independent _ b'' := _, have b''_sp : submodule.span _ (set.range b'') = ⊤ := _, -- Since the two bases have the same index set, the spaces have the same dimension. let c : basis ι K L := basis.mk b''_li b''_sp.ge, rw [finrank_eq_card_basis b, finrank_eq_card_basis c], -- It remains to show that the basis is indeed linear independent and spans the whole space. { rw set.range_comp, refine finrank_quotient_map.span_eq_top p hp.ne_top _ (top_le_iff.mp _), -- The nicest way to show `S ≤ span b' ⊔ pS` is by reducing both sides modulo pS. -- However, this would imply distinguishing between `pS` as `S`-ideal, -- and `pS` as `R`-submodule, since they have different (non-defeq) quotients. -- Instead we'll lift `x mod pS ∈ span b` to `y ∈ span b'` for some `y - x ∈ pS`. intros x hx, have mem_span_b : ((submodule.mkq (map (algebra_map R S) p)) x : S ⧸ map (algebra_map R S) p) ∈ submodule.span (R ⧸ p) (set.range b) := b.mem_span _, rw [← @submodule.restrict_scalars_mem R, algebra.span_restrict_scalars_eq_span_of_surjective (show function.surjective (algebra_map R (R ⧸ p)), from ideal.quotient.mk_surjective) _, b_eq_b', set.range_comp, ← submodule.map_span] at mem_span_b, obtain ⟨y, y_mem, y_eq⟩ := submodule.mem_map.mp mem_span_b, suffices : y + -(y - x) ∈ _, { simpa }, rw [linear_map.restrict_scalars_apply, submodule.mkq_apply, submodule.mkq_apply, submodule.quotient.eq] at y_eq, exact add_mem (submodule.mem_sup_left y_mem) (neg_mem $ submodule.mem_sup_right y_eq) }, { have := b.linear_independent, rw b_eq_b' at this, convert finrank_quotient_map.linear_independent_of_nontrivial K _ ((algebra.linear_map S L).restrict_scalars R) _ ((submodule.mkq _).restrict_scalars R) this, { rw [quotient.algebra_map_eq, ideal.mk_ker], exact hp.ne_top }, { exact is_fraction_ring.injective S L } }, end end finrank_quotient_map section fact_le_comap local notation `e` := ramification_idx f p P /-- `R / p` has a canonical map to `S / (P ^ e)`, where `e` is the ramification index of `P` over `p`. -/ noncomputable instance quotient.algebra_quotient_pow_ramification_idx : algebra (R ⧸ p) (S ⧸ (P ^ e)) := quotient.algebra_quotient_of_le_comap (ideal.map_le_iff_le_comap.mp le_pow_ramification_idx) @[simp] lemma quotient.algebra_map_quotient_pow_ramification_idx (x : R) : algebra_map (R ⧸ p) (S ⧸ P ^ e) (ideal.quotient.mk p x) = ideal.quotient.mk _ (f x) := rfl variables [hfp : fact (ramification_idx f p P ≠ 0)] include hfp /-- If `P` lies over `p`, then `R / p` has a canonical map to `S / P`. This can't be an instance since the map `f : R → S` is generally not inferrable. -/ def quotient.algebra_quotient_of_ramification_idx_ne_zero : algebra (R ⧸ p) (S ⧸ P) := quotient.algebra_quotient_of_le_comap (le_comap_of_ramification_idx_ne_zero hfp.out) -- In this file, the value for `f` can be inferred. local attribute [instance] ideal.quotient.algebra_quotient_of_ramification_idx_ne_zero @[simp] lemma quotient.algebra_map_quotient_of_ramification_idx_ne_zero (x : R) : algebra_map (R ⧸ p) (S ⧸ P) (ideal.quotient.mk p x) = ideal.quotient.mk _ (f x) := rfl omit hfp /-- The inclusion `(P^(i + 1) / P^e) ⊂ (P^i / P^e)`. -/ @[simps] def pow_quot_succ_inclusion (i : ℕ) : ideal.map (P^e)^.quotient.mk (P ^ (i + 1)) →ₗ[R ⧸ p] ideal.map (P^e)^.quotient.mk (P ^ i) := { to_fun := λ x, ⟨x, ideal.map_mono (ideal.pow_le_pow i.le_succ) x.2⟩, map_add' := λ x y, rfl, map_smul' := λ c x, rfl } lemma pow_quot_succ_inclusion_injective (i : ℕ) : function.injective (pow_quot_succ_inclusion f p P i) := begin rw [← linear_map.ker_eq_bot, linear_map.ker_eq_bot'], rintro ⟨x, hx⟩ hx0, rw subtype.ext_iff at hx0 ⊢, rwa pow_quot_succ_inclusion_apply_coe at hx0 end /-- `S ⧸ P` embeds into the quotient by `P^(i+1) ⧸ P^e` as a subspace of `P^i ⧸ P^e`. See `quotient_to_quotient_range_pow_quot_succ` for this as a linear map, and `quotient_range_pow_quot_succ_inclusion_equiv` for this as a linear equivalence. -/ noncomputable def quotient_to_quotient_range_pow_quot_succ_aux {i : ℕ} {a : S} (a_mem : a ∈ P^i) : S ⧸ P → ((P ^ i).map (P ^ e)^.quotient.mk ⧸ (pow_quot_succ_inclusion f p P i).range) := quotient.map' (λ (x : S), ⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_left _ x a_mem)⟩) (λ x y h, begin rw submodule.quotient_rel_r_def at ⊢ h, simp only [_root_.map_mul, linear_map.mem_range], refine ⟨⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_mul h a_mem)⟩, _⟩, ext, rw [pow_quot_succ_inclusion_apply_coe, subtype.coe_mk, submodule.coe_sub, subtype.coe_mk, subtype.coe_mk, _root_.map_mul, map_sub, sub_mul] end) lemma quotient_to_quotient_range_pow_quot_succ_aux_mk {i : ℕ} {a : S} (a_mem : a ∈ P^i) (x : S) : quotient_to_quotient_range_pow_quot_succ_aux f p P a_mem (submodule.quotient.mk x) = submodule.quotient.mk ⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_left _ x a_mem)⟩ := by apply quotient.map'_mk' include hfp /-- `S ⧸ P` embeds into the quotient by `P^(i+1) ⧸ P^e` as a subspace of `P^i ⧸ P^e`. -/ noncomputable def quotient_to_quotient_range_pow_quot_succ {i : ℕ} {a : S} (a_mem : a ∈ P^i) : S ⧸ P →ₗ[R ⧸ p] ((P ^ i).map (P ^ e)^.quotient.mk ⧸ (pow_quot_succ_inclusion f p P i).range) := { to_fun := quotient_to_quotient_range_pow_quot_succ_aux f p P a_mem, map_add' := begin intros x y, refine quotient.induction_on' x (λ x, quotient.induction_on' y (λ y, _)), simp only [submodule.quotient.mk'_eq_mk, ← submodule.quotient.mk_add, quotient_to_quotient_range_pow_quot_succ_aux_mk, add_mul], refine congr_arg submodule.quotient.mk _, ext, refl end, map_smul' := begin intros x y, refine quotient.induction_on' x (λ x, quotient.induction_on' y (λ y, _)), simp only [submodule.quotient.mk'_eq_mk, ← submodule.quotient.mk_add, quotient_to_quotient_range_pow_quot_succ_aux_mk, ring_hom.id_apply], refine congr_arg submodule.quotient.mk _, ext, simp only [subtype.coe_mk, _root_.map_mul, algebra.smul_def, submodule.coe_mk, mul_assoc, ideal.quotient.mk_eq_mk, submodule.coe_smul_of_tower, ideal.quotient.algebra_map_quotient_pow_ramification_idx] end } lemma quotient_to_quotient_range_pow_quot_succ_mk {i : ℕ} {a : S} (a_mem : a ∈ P^i) (x : S) : quotient_to_quotient_range_pow_quot_succ f p P a_mem (submodule.quotient.mk x) = submodule.quotient.mk ⟨_, ideal.mem_map_of_mem _ (ideal.mul_mem_left _ x a_mem)⟩ := quotient_to_quotient_range_pow_quot_succ_aux_mk f p P a_mem x lemma quotient_to_quotient_range_pow_quot_succ_injective [is_domain S] [is_dedekind_domain S] [P.is_prime] {i : ℕ} (hi : i < e) {a : S} (a_mem : a ∈ P^i) (a_not_mem : a ∉ P^(i + 1)) : function.injective (quotient_to_quotient_range_pow_quot_succ f p P a_mem) := λ x, quotient.induction_on' x $ λ x y, quotient.induction_on' y $ λ y h, begin have Pe_le_Pi1 : P^e ≤ P^(i + 1) := ideal.pow_le_pow hi, simp only [submodule.quotient.mk'_eq_mk, quotient_to_quotient_range_pow_quot_succ_mk, submodule.quotient.eq, linear_map.mem_range, subtype.ext_iff, subtype.coe_mk, submodule.coe_sub] at ⊢ h, rcases h with ⟨⟨⟨z⟩, hz⟩, h⟩, rw [submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk, ideal.mem_quotient_iff_mem_sup, sup_eq_left.mpr Pe_le_Pi1] at hz, rw [pow_quot_succ_inclusion_apply_coe, subtype.coe_mk, submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk, ← map_sub, ideal.quotient.eq, ← sub_mul] at h, exact (ideal.is_prime.mul_mem_pow _ ((submodule.sub_mem_iff_right _ hz).mp (Pe_le_Pi1 h))).resolve_right a_not_mem, end lemma quotient_to_quotient_range_pow_quot_succ_surjective [is_domain S] [is_dedekind_domain S] (hP0 : P ≠ ⊥) [hP : P.is_prime] {i : ℕ} (hi : i < e) {a : S} (a_mem : a ∈ P^i) (a_not_mem : a ∉ P^(i + 1)) : function.surjective (quotient_to_quotient_range_pow_quot_succ f p P a_mem) := begin rintro ⟨⟨⟨x⟩, hx⟩⟩, have Pe_le_Pi : P^e ≤ P^i := ideal.pow_le_pow hi.le, have Pe_le_Pi1 : P^e ≤ P^(i + 1) := ideal.pow_le_pow hi, rw [submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk, ideal.mem_quotient_iff_mem_sup, sup_eq_left.mpr Pe_le_Pi] at hx, suffices hx' : x ∈ ideal.span {a} ⊔ P^(i+1), { obtain ⟨y', hy', z, hz, rfl⟩ := submodule.mem_sup.mp hx', obtain ⟨y, rfl⟩ := ideal.mem_span_singleton.mp hy', refine ⟨submodule.quotient.mk y, _⟩, simp only [submodule.quotient.quot_mk_eq_mk, quotient_to_quotient_range_pow_quot_succ_mk, submodule.quotient.eq, linear_map.mem_range, subtype.ext_iff, subtype.coe_mk, submodule.coe_sub], refine ⟨⟨_, ideal.mem_map_of_mem _ (submodule.neg_mem _ hz)⟩, _⟩, rw [pow_quot_succ_inclusion_apply_coe, subtype.coe_mk, ideal.quotient.mk_eq_mk, map_add, mul_comm y a, sub_add_cancel', map_neg] }, letI := classical.dec_eq (ideal S), rw [sup_eq_prod_inf_factors _ (pow_ne_zero _ hP0), normalized_factors_pow, normalized_factors_irreducible ((ideal.prime_iff_is_prime hP0).mpr hP).irreducible, normalize_eq, multiset.nsmul_singleton, multiset.inter_repeat, multiset.prod_repeat], rw [← submodule.span_singleton_le_iff_mem, ideal.submodule_span_eq] at a_mem a_not_mem, rwa [ideal.count_normalized_factors_eq a_mem a_not_mem, min_eq_left i.le_succ], { intro ha, rw ideal.span_singleton_eq_bot.mp ha at a_not_mem, have := (P^(i+1)).zero_mem, contradiction }, end /-- Quotienting `P^i / P^e` by its subspace `P^(i+1) ⧸ P^e` is `R ⧸ p`-linearly isomorphic to `S ⧸ P`. -/ noncomputable def quotient_range_pow_quot_succ_inclusion_equiv [is_domain S] [is_dedekind_domain S] [P.is_prime] (hP : P ≠ ⊥) {i : ℕ} (hi : i < e) : ((P ^ i).map (P ^ e)^.quotient.mk ⧸ (pow_quot_succ_inclusion f p P i).range) ≃ₗ[R ⧸ p] S ⧸ P := begin choose a a_mem a_not_mem using set_like.exists_of_lt (ideal.strict_anti_pow P hP (ideal.is_prime.ne_top infer_instance) (le_refl i.succ)), refine (linear_equiv.of_bijective _ _ _).symm, { exact quotient_to_quotient_range_pow_quot_succ f p P a_mem }, { exact quotient_to_quotient_range_pow_quot_succ_injective f p P hi a_mem a_not_mem }, { exact quotient_to_quotient_range_pow_quot_succ_surjective f p P hP hi a_mem a_not_mem } end /-- Since the inclusion `(P^(i + 1) / P^e) ⊂ (P^i / P^e)` has a kernel isomorphic to `P / S`, `[P^i / P^e : R / p] = [P^(i+1) / P^e : R / p] + [P / S : R / p]` -/ lemma dim_pow_quot_aux [is_domain S] [is_dedekind_domain S] [p.is_maximal] [P.is_prime] (hP0 : P ≠ ⊥) {i : ℕ} (hi : i < e) : module.rank (R ⧸ p) (ideal.map (P^e)^.quotient.mk (P ^ i)) = module.rank (R ⧸ p) (S ⧸ P) + module.rank (R ⧸ p) (ideal.map (P^e)^.quotient.mk (P ^ (i + 1))) := begin letI : field (R ⧸ p) := ideal.quotient.field _, rw [dim_eq_of_injective _ (pow_quot_succ_inclusion_injective f p P i), (quotient_range_pow_quot_succ_inclusion_equiv f p P hP0 hi).symm.dim_eq], exact (dim_quotient_add_dim (linear_map.range (pow_quot_succ_inclusion f p P i))).symm, end lemma dim_pow_quot [is_domain S] [is_dedekind_domain S] [p.is_maximal] [P.is_prime] (hP0 : P ≠ ⊥) (i : ℕ) (hi : i ≤ e) : module.rank (R ⧸ p) (ideal.map (P^e)^.quotient.mk (P ^ i)) = (e - i) • module.rank (R ⧸ p) (S ⧸ P) := begin refine @nat.decreasing_induction' _ i e (λ j lt_e le_j ih, _) hi _, { rw [dim_pow_quot_aux f p P _ lt_e, ih, ← succ_nsmul, nat.sub_succ, ← nat.succ_eq_add_one, nat.succ_pred_eq_of_pos (nat.sub_pos_of_lt lt_e)], assumption }, { rw [nat.sub_self, zero_nsmul, map_quotient_self], exact dim_bot (R ⧸ p) (S ⧸ (P^e)) } end omit hfp /-- If `p` is a maximal ideal of `R`, `S` extends `R` and `P^e` lies over `p`, then the dimension `[S/(P^e) : R/p]` is equal to `e * [S/P : R/p]`. -/ lemma dim_prime_pow_ramification_idx [is_domain S] [is_dedekind_domain S] [p.is_maximal] [P.is_prime] (hP0 : P ≠ ⊥) (he : e ≠ 0) : module.rank (R ⧸ p) (S ⧸ P^e) = e • @module.rank (R ⧸ p) (S ⧸ P) _ _ (@algebra.to_module _ _ _ _ $ @@quotient.algebra_quotient_of_ramification_idx_ne_zero _ _ _ _ _ ⟨he⟩) := begin letI : fact (e ≠ 0) := ⟨he⟩, have := dim_pow_quot f p P hP0 0 (nat.zero_le e), rw [pow_zero, nat.sub_zero, ideal.one_eq_top, ideal.map_top] at this, exact (dim_top (R ⧸ p) _).symm.trans this end /-- If `p` is a maximal ideal of `R`, `S` extends `R` and `P^e` lies over `p`, then the dimension `[S/(P^e) : R/p]`, as a natural number, is equal to `e * [S/P : R/p]`. -/ lemma finrank_prime_pow_ramification_idx [is_domain S] [is_dedekind_domain S] (hP0 : P ≠ ⊥) [p.is_maximal] [P.is_prime] (he : e ≠ 0) : finrank (R ⧸ p) (S ⧸ P^e) = e * @finrank (R ⧸ p) (S ⧸ P) _ _ (@algebra.to_module _ _ _ _ $ @@quotient.algebra_quotient_of_ramification_idx_ne_zero _ _ _ _ _ ⟨he⟩) := begin letI : fact (e ≠ 0) := ⟨he⟩, letI : algebra (R ⧸ p) (S ⧸ P) := quotient.algebra_quotient_of_ramification_idx_ne_zero f p P, letI := ideal.quotient.field p, have hdim := dim_prime_pow_ramification_idx _ _ _ hP0 he, by_cases hP : finite_dimensional (R ⧸ p) (S ⧸ P), { haveI := hP, haveI := (finite_dimensional_iff_of_rank_eq_nsmul he hdim).mpr hP, refine cardinal.nat_cast_injective _, rw [finrank_eq_dim, nat.cast_mul, finrank_eq_dim, hdim, nsmul_eq_mul] }, have hPe := mt (finite_dimensional_iff_of_rank_eq_nsmul he hdim).mp hP, simp only [finrank_of_infinite_dimensional hP, finrank_of_infinite_dimensional hPe, mul_zero], end end fact_le_comap section factors_map open_locale classical /-! ## Properties of the factors of `p.map (algebra_map R S)` -/ variables [is_domain S] [is_dedekind_domain S] [algebra R S] lemma factors.ne_bot (P : (factors (map (algebra_map R S) p)).to_finset) : (P : ideal S) ≠ ⊥ := (prime_of_factor _ (multiset.mem_to_finset.mp P.2)).ne_zero instance factors.is_prime (P : (factors (map (algebra_map R S) p)).to_finset) : is_prime (P : ideal S) := ideal.is_prime_of_prime (prime_of_factor _ (multiset.mem_to_finset.mp P.2)) lemma factors.ramification_idx_ne_zero (P : (factors (map (algebra_map R S) p)).to_finset) : ramification_idx (algebra_map R S) p P ≠ 0 := is_dedekind_domain.ramification_idx_ne_zero (ne_zero_of_mem_factors (multiset.mem_to_finset.mp P.2)) (factors.is_prime p P) (ideal.le_of_dvd (dvd_of_mem_factors (multiset.mem_to_finset.mp P.2))) instance factors.fact_ramification_idx_ne_zero (P : (factors (map (algebra_map R S) p)).to_finset) : fact (ramification_idx (algebra_map R S) p P ≠ 0) := ⟨factors.ramification_idx_ne_zero p P⟩ local attribute [instance] quotient.algebra_quotient_of_ramification_idx_ne_zero instance factors.is_scalar_tower (P : (factors (map (algebra_map R S) p)).to_finset) : is_scalar_tower R (R ⧸ p) (S ⧸ (P : ideal S)) := is_scalar_tower.of_algebra_map_eq (λ x, by simp) local attribute [instance] ideal.quotient.field lemma factors.finrank_pow_ramification_idx [p.is_maximal] (P : (factors (map (algebra_map R S) p)).to_finset) : finrank (R ⧸ p) (S ⧸ (P : ideal S) ^ ramification_idx (algebra_map R S) p P) = ramification_idx (algebra_map R S) p P * inertia_deg (algebra_map R S) p P := begin rw [finrank_prime_pow_ramification_idx, inertia_deg_algebra_map], exact factors.ne_bot p P, end instance factors.finite_dimensional_quotient [is_noetherian R S] [p.is_maximal] (P : (factors (map (algebra_map R S) p)).to_finset) : finite_dimensional (R ⧸ p) (S ⧸ (P : ideal S)) := is_noetherian.iff_fg.mp $ is_noetherian_of_tower R $ is_noetherian_of_surjective S (ideal.quotient.mkₐ _ _).to_linear_map $ linear_map.range_eq_top.mpr ideal.quotient.mk_surjective lemma factors.inertia_deg_ne_zero [is_noetherian R S] [p.is_maximal] (P : (factors (map (algebra_map R S) p)).to_finset) : inertia_deg (algebra_map R S) p P ≠ 0 := by { rw inertia_deg_algebra_map, exact (finite_dimensional.finrank_pos_iff.mpr infer_instance).ne' } instance factors.finite_dimensional_quotient_pow [is_noetherian R S] [p.is_maximal] (P : (factors (map (algebra_map R S) p)).to_finset) : finite_dimensional (R ⧸ p) (S ⧸ (P : ideal S) ^ ramification_idx (algebra_map R S) p P) := begin refine finite_dimensional.finite_dimensional_of_finrank _, rw [pos_iff_ne_zero, factors.finrank_pow_ramification_idx], exact mul_ne_zero (factors.ramification_idx_ne_zero p P) (factors.inertia_deg_ne_zero p P) end universes w /-- **Chinese remainder theorem** for a ring of integers: if the prime ideal `p : ideal R` factors in `S` as `∏ i, P i ^ e i`, then `S ⧸ I` factors as `Π i, R ⧸ (P i ^ e i)`. -/ noncomputable def factors.pi_quotient_equiv (p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) : (S ⧸ map (algebra_map R S) p) ≃+* Π (P : (factors (map (algebra_map R S) p)).to_finset), S ⧸ ((P : ideal S) ^ ramification_idx (algebra_map R S) p P) := (is_dedekind_domain.quotient_equiv_pi_factors hp).trans $ (@ring_equiv.Pi_congr_right (factors (map (algebra_map R S) p)).to_finset (λ P, S ⧸ (P : ideal S) ^ (factors (map (algebra_map R S) p)).count P) (λ P, S ⧸ (P : ideal S) ^ ramification_idx (algebra_map R S) p P) _ _ (λ P : (factors (map (algebra_map R S) p)).to_finset, ideal.quot_equiv_of_eq $ by rw is_dedekind_domain.ramification_idx_eq_factors_count hp (factors.is_prime p P) (factors.ne_bot p P))) @[simp] lemma factors.pi_quotient_equiv_mk (p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) (x : S) : factors.pi_quotient_equiv p hp (ideal.quotient.mk _ x) = λ P, ideal.quotient.mk _ x := rfl @[simp] lemma factors.pi_quotient_equiv_map (p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) (x : R) : factors.pi_quotient_equiv p hp (algebra_map _ _ x) = λ P, ideal.quotient.mk _ (algebra_map _ _ x) := rfl /-- **Chinese remainder theorem** for a ring of integers: if the prime ideal `p : ideal R` factors in `S` as `∏ i, P i ^ e i`, then `S ⧸ I` factors `R ⧸ I`-linearly as `Π i, R ⧸ (P i ^ e i)`. -/ noncomputable def factors.pi_quotient_linear_equiv (p : ideal R) (hp : map (algebra_map R S) p ≠ ⊥) : (S ⧸ map (algebra_map R S) p) ≃ₗ[R ⧸ p] Π (P : (factors (map (algebra_map R S) p)).to_finset), S ⧸ ((P : ideal S) ^ ramification_idx (algebra_map R S) p P) := { map_smul' := begin rintro ⟨c⟩ ⟨x⟩, ext P, simp only [ideal.quotient.mk_algebra_map, factors.pi_quotient_equiv_mk, factors.pi_quotient_equiv_map, submodule.quotient.quot_mk_eq_mk, pi.algebra_map_apply, ring_equiv.to_fun_eq_coe, pi.mul_apply, ideal.quotient.algebra_map_quotient_map_quotient, ideal.quotient.mk_eq_mk, algebra.smul_def, _root_.map_mul, ring_hom_comp_triple.comp_apply], congr end, .. factors.pi_quotient_equiv p hp } open_locale big_operators /-- The **fundamental identity** of ramification index `e` and inertia degree `f`: for `P` ranging over the primes lying over `p`, `∑ P, e P * f P = [Frac(S) : Frac(R)]`; here `S` is a finite `R`-module (and thus `Frac(S) : Frac(R)` is a finite extension) and `p` is maximal. -/ theorem sum_ramification_inertia (K L : Type*) [field K] [field L] [is_domain R] [is_dedekind_domain R] [algebra R K] [is_fraction_ring R K] [algebra S L] [is_fraction_ring S L] [algebra K L] [algebra R L] [is_scalar_tower R S L] [is_scalar_tower R K L] [is_noetherian R S] [is_integral_closure S R L] [p.is_maximal] (hp0 : p ≠ ⊥) : ∑ P in (factors (map (algebra_map R S) p)).to_finset, ramification_idx (algebra_map R S) p P * inertia_deg (algebra_map R S) p P = finrank K L := begin set e := ramification_idx (algebra_map R S) p, set f := inertia_deg (algebra_map R S) p, have inj_RL : function.injective (algebra_map R L), { rw [is_scalar_tower.algebra_map_eq R K L, ring_hom.coe_comp], exact (ring_hom.injective _).comp (is_fraction_ring.injective R K) }, have inj_RS : function.injective (algebra_map R S), { refine function.injective.of_comp (show function.injective (algebra_map S L ∘ _), from _), rw [← ring_hom.coe_comp, ← is_scalar_tower.algebra_map_eq], exact inj_RL }, calc ∑ P in (factors (map (algebra_map R S) p)).to_finset, e P * f P = ∑ P in (factors (map (algebra_map R S) p)).to_finset.attach, finrank (R ⧸ p) (S ⧸ (P : ideal S)^(e P)) : _ ... = finrank (R ⧸ p) (Π P : (factors (map (algebra_map R S) p)).to_finset, (S ⧸ (P : ideal S)^(e P))) : (module.free.finrank_pi_fintype (R ⧸ p)).symm ... = finrank (R ⧸ p) (S ⧸ map (algebra_map R S) p) : _ ... = finrank K L : _, { rw ← finset.sum_attach, refine finset.sum_congr rfl (λ P _, _), rw factors.finrank_pow_ramification_idx }, { refine linear_equiv.finrank_eq (factors.pi_quotient_linear_equiv p _).symm, rwa [ne.def, ideal.map_eq_bot_iff_le_ker, (ring_hom.injective_iff_ker_eq_bot _).mp inj_RS, le_bot_iff] }, { exact finrank_quotient_map p K L }, end end factors_map end ideal
478d8057dc1e6e204da808f528439696a1ca44d3
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_1/kb_solutions/Part_C_functions_solutions.lean
45eebe43b154dc9617d874ba63c340f357e5385d
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/formalising-mathematics
b54c83c94b5c315024ff09997fcd6b303892a749
7cf1d51c27e2038d2804561d63c74711924044a1
refs/heads/master
1,651,267,046,302
1,638,888,459,000
1,638,888,459,000
331,592,375
284
24
Apache-2.0
1,669,593,705,000
1,611,224,849,000
Lean
UTF-8
Lean
false
false
4,023
lean
import tactic -- injective and surjective functions are already in Lean. -- They are called `function.injective` and `function.surjective`. -- It gets a bit boring typing `function.` a lot so we start -- by opening the `function` namespace open function -- We now move into the `xena` namespace namespace xena -- let X, Y, Z be "types", i.e. sets, and let `f : X → Y` and `g : Y → Z` -- be functions variables {X Y Z : Type} {f : X → Y} {g : Y → Z} -- let a,b,x be elements of X, let y be an element of Y and let z be an -- element of Z variables (a b x : X) (y : Y) (z : Z) /-! # Injective functions -/ -- let's start by checking the definition of injective is -- what we think it is. lemma injective_def : injective f ↔ ∀ a b : X, f a = f b → a = b := begin -- true by definition refl end -- You can now `rw injective_def` to change `injective f` into its definition. -- The identity function id : X → X is defined by id(x) = x. Let's check this lemma id_def : id x = x := begin -- true by definition refl end -- you can now `rw id_def` to change `id x` into `x` /-- The identity function is injective -/ lemma injective_id : injective (id : X → X) := begin -- these rewrites are not necessary, but I'll -- put them in just this once -- ⊢ injective id rw injective_def, -- ⊢ ∀ (a b : X), id a = id b → a = b intros a b hab, -- hab: id a = id b -- again another rewrite which isn't actually necessary rw id_def at hab, rw id_def at hab, -- hab : a = b exact hab, end -- function composition g ∘ f is satisfies (g ∘ f) (x) = g(f(x)). This -- is true by definition. Let's check this lemma comp_def : (g ∘ f) x = g (f x) := begin -- true by definition refl end /-- Composite of two injective functions is injective -/ lemma injective_comp (hf : injective f) (hg : injective g) : injective (g ∘ f) := begin -- I'll do this without any definitional rewrites -- ⊢ injective (g ∘ f) -- so goal is by definition "for all a, b, ..." and I can just intro intros a b h, -- `hf : injective f`, so `hf : ∀ a b : X, f a = f b → a = b` -- and my goal is `a = b` so this will work apply hf, apply hg, exact h, -- again `h` is not syntactically equal to the goal, -- but it is definitionally equal to the goal end /-! ### Surjective functions -/ -- Let's start by checking the definition of surjectivity is what we think it is lemma surjective_def : surjective f ↔ ∀ y : Y, ∃ x : X, f x = y := begin -- true by definition refl end /-- The identity function is surjective -/ lemma surjective_id : surjective (id : X → X) := begin -- you can start with `rw surjective_def` if you like. intro x, use x, refl, end -- If you started with `rw surjective_def` -- try deleting it. -- Probably your proof still works! This is because -- `surjective_def` is true *by definition*. The proof is `refl`. -- For this next one, the `have` tactic is helpful. /-- Composite of two surjective functions is surjective -/ lemma surjective_comp (hf : surjective f) (hg : surjective g) : surjective (g ∘ f) := begin intro z, cases hg z with y hy, cases hf y with x hx, use x, show g(f(x)) = z, rw hx, exact hy, end /-! ### Bijective functions In Lean a function is defined to be bijective if it is injective and surjective. Let's check this. -/ lemma bijective_def : bijective f ↔ injective f ∧ surjective f := begin -- true by definition refl end -- You can now use the lemmas you've proved already to make these -- proofs very short. /-- The identity function is bijective. -/ lemma bijective_id : bijective (id : X → X) := begin exact ⟨injective_id, surjective_id⟩, end /-- A composite of bijective functions is bijective. -/ lemma bijective_comp (hf : bijective f) (hg : bijective g) : bijective (g ∘ f) := begin cases hf with hf_inj hf_surj, cases hg with hg_inj hg_surj, exact ⟨injective_comp hf_inj hg_inj, surjective_comp hf_surj hg_surj⟩ end end xena
87067a9a72dd1f7fb46d8646c31179af54c19075
618003631150032a5676f229d13a079ac875ff77
/src/data/nat/enat.lean
a57e2c8e79f7df48d754cca504ac572ca59a1c4b
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
12,606
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Natural numbers with infinity, represented as roption ℕ. -/ import data.pfun import tactic.norm_num open roption def enat : Type := roption ℕ namespace enat instance : has_zero enat := ⟨some 0⟩ instance : inhabited enat := ⟨0⟩ instance : has_one enat := ⟨some 1⟩ instance : has_add enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, get x h.1 + get y h.2⟩⟩ instance : has_coe ℕ enat := ⟨some⟩ instance (n : ℕ) : decidable (n : enat).dom := is_true trivial @[simp] lemma coe_inj {x y : ℕ} : (x : enat) = y ↔ x = y := roption.some_inj instance : add_comm_monoid enat := { add := (+), zero := (0), add_comm := λ x y, roption.ext' and.comm (λ _ _, add_comm _ _), zero_add := λ x, roption.ext' (true_and _) (λ _ _, zero_add _), add_zero := λ x, roption.ext' (and_true _) (λ _ _, add_zero _), add_assoc := λ x y z, roption.ext' and.assoc (λ _ _, add_assoc _ _ _) } instance : has_le enat := ⟨λ x y, ∃ h : y.dom → x.dom, ∀ hy : y.dom, x.get (h hy) ≤ y.get hy⟩ instance : has_top enat := ⟨none⟩ instance : has_bot enat := ⟨0⟩ instance : has_sup enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, x.get h.1 ⊔ y.get h.2⟩⟩ @[elab_as_eliminator] protected lemma cases_on {P : enat → Prop} : ∀ a : enat, P ⊤ → (∀ n : ℕ, P n) → P a := roption.induction_on @[simp] lemma top_add (x : enat) : ⊤ + x = ⊤ := roption.ext' (false_and _) (λ h, h.left.elim) @[simp] lemma add_top (x : enat) : x + ⊤ = ⊤ := by rw [add_comm, top_add] @[simp, norm_cast] lemma coe_zero : ((0 : ℕ) : enat) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : ℕ) : enat) = 1 := rfl @[simp, norm_cast] lemma coe_add (x y : ℕ) : ((x + y : ℕ) : enat) = x + y := roption.ext' (and_true _).symm (λ _ _, rfl) @[simp, norm_cast] lemma get_coe {x : ℕ} : get (x : enat) true.intro = x := rfl lemma coe_add_get {x : ℕ} {y : enat} (h : ((x : enat) + y).dom) : get ((x : enat) + y) h = x + get y h.2 := rfl @[simp] lemma get_add {x y : enat} (h : (x + y).dom) : get (x + y) h = x.get h.1 + y.get h.2 := rfl @[simp] lemma coe_get {x : enat} (h : x.dom) : (x.get h : enat) = x := roption.ext' (iff_of_true trivial h) (λ _ _, rfl) @[simp] lemma get_zero (h : (0 : enat).dom) : (0 : enat).get h = 0 := rfl @[simp] lemma get_one (h : (1 : enat).dom) : (1 : enat).get h = 1 := rfl lemma dom_of_le_some {x : enat} {y : ℕ} : x ≤ y → x.dom := λ ⟨h, _⟩, h trivial instance : partial_order enat := { le := (≤), le_refl := λ x, ⟨id, λ _, le_refl _⟩, le_trans := λ x y z ⟨hxy₁, hxy₂⟩ ⟨hyz₁, hyz₂⟩, ⟨hxy₁ ∘ hyz₁, λ _, le_trans (hxy₂ _) (hyz₂ _)⟩, le_antisymm := λ x y ⟨hxy₁, hxy₂⟩ ⟨hyx₁, hyx₂⟩, roption.ext' ⟨hyx₁, hxy₁⟩ (λ _ _, le_antisymm (hxy₂ _) (hyx₂ _)) } @[simp, norm_cast] lemma coe_le_coe {x y : ℕ} : (x : enat) ≤ y ↔ x ≤ y := ⟨λ ⟨_, h⟩, h trivial, λ h, ⟨λ _, trivial, λ _, h⟩⟩ @[simp, norm_cast] lemma coe_lt_coe {x y : ℕ} : (x : enat) < y ↔ x < y := by rw [lt_iff_le_not_le, lt_iff_le_not_le, coe_le_coe, coe_le_coe] lemma get_le_get {x y : enat} {hx : x.dom} {hy : y.dom} : x.get hx ≤ y.get hy ↔ x ≤ y := by conv { to_lhs, rw [← coe_le_coe, coe_get, coe_get]} instance semilattice_sup_bot : semilattice_sup_bot enat := { sup := (⊔), bot := (⊥), bot_le := λ _, ⟨λ _, trivial, λ _, nat.zero_le _⟩, le_sup_left := λ _ _, ⟨and.left, λ _, le_sup_left⟩, le_sup_right := λ _ _, ⟨and.right, λ _, le_sup_right⟩, sup_le := λ x y z ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩, ⟨λ hz, ⟨hx₁ hz, hy₁ hz⟩, λ _, sup_le (hx₂ _) (hy₂ _)⟩, ..enat.partial_order } instance order_top : order_top enat := { top := (⊤), le_top := λ x, ⟨λ h, false.elim h, λ hy, false.elim hy⟩, ..enat.semilattice_sup_bot } lemma top_eq_none : (⊤ : enat) = none := rfl lemma coe_lt_top (x : ℕ) : (x : enat) < ⊤ := lt_of_le_of_ne le_top (λ h, absurd (congr_arg dom h) true_ne_false) @[simp] lemma coe_ne_top (x : ℕ) : (x : enat) ≠ ⊤ := ne_of_lt (coe_lt_top x) lemma ne_top_iff {x : enat} : x ≠ ⊤ ↔ ∃(n : ℕ), x = n := roption.ne_none_iff lemma ne_top_iff_dom {x : enat} : x ≠ ⊤ ↔ x.dom := by classical; exact not_iff_comm.1 roption.eq_none_iff'.symm lemma ne_top_of_lt {x y : enat} (h : x < y) : x ≠ ⊤ := ne_of_lt $ lt_of_lt_of_le h le_top lemma pos_iff_one_le {x : enat} : 0 < x ↔ 1 ≤ x := enat.cases_on x ⟨λ _, le_top, λ _, coe_lt_top _⟩ (λ n, ⟨λ h, enat.coe_le_coe.2 (enat.coe_lt_coe.1 h), λ h, enat.coe_lt_coe.2 (enat.coe_le_coe.1 h)⟩) noncomputable instance : decidable_linear_order enat := { le_total := λ x y, enat.cases_on x (or.inr le_top) (enat.cases_on y (λ _, or.inl le_top) (λ x y, (le_total x y).elim (or.inr ∘ coe_le_coe.2) (or.inl ∘ coe_le_coe.2))), decidable_le := classical.dec_rel _, ..enat.partial_order } noncomputable instance : bounded_lattice enat := { inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := λ _ _ _, le_min, ..enat.order_top, ..enat.semilattice_sup_bot } lemma sup_eq_max {a b : enat} : a ⊔ b = max a b := le_antisymm (sup_le (le_max_left _ _) (le_max_right _ _)) (max_le le_sup_left le_sup_right) lemma inf_eq_min {a b : enat} : a ⊓ b = min a b := rfl instance : ordered_add_comm_monoid enat := { add_le_add_left := λ a b ⟨h₁, h₂⟩ c, enat.cases_on c (by simp) (λ c, ⟨λ h, and.intro trivial (h₁ h.2), λ _, add_le_add_left (h₂ _) c⟩), lt_of_add_lt_add_left := λ a b c, enat.cases_on a (λ h, by simpa [lt_irrefl] using h) (λ a, enat.cases_on b (λ h, absurd h (not_lt_of_ge (by rw add_top; exact le_top))) (λ b, enat.cases_on c (λ _, coe_lt_top _) (λ c h, coe_lt_coe.2 (by rw [← coe_add, ← coe_add, coe_lt_coe] at h; exact lt_of_add_lt_add_left h)))), ..enat.decidable_linear_order, ..enat.add_comm_monoid } instance : canonically_ordered_add_monoid enat := { le_iff_exists_add := λ a b, enat.cases_on b (iff_of_true le_top ⟨⊤, (add_top _).symm⟩) (λ b, enat.cases_on a (iff_of_false (not_le_of_gt (coe_lt_top _)) (not_exists.2 (λ x, ne_of_lt (by rw [top_add]; exact coe_lt_top _)))) (λ a, ⟨λ h, ⟨(b - a : ℕ), by rw [← coe_add, coe_inj, add_comm, nat.sub_add_cancel (coe_le_coe.1 h)]⟩, (λ ⟨c, hc⟩, enat.cases_on c (λ hc, hc.symm ▸ show (a : enat) ≤ a + ⊤, by rw [add_top]; exact le_top) (λ c (hc : (b : enat) = a + c), coe_le_coe.2 (by rw [← coe_add, coe_inj] at hc; rw hc; exact nat.le_add_right _ _)) hc)⟩)), ..enat.semilattice_sup_bot, ..enat.ordered_add_comm_monoid } protected lemma add_lt_add_right {x y z : enat} (h : x < y) (hz : z ≠ ⊤) : x + z < y + z := begin rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩, rcases ne_top_iff.mp hz with ⟨k, rfl⟩, induction y using enat.cases_on with n, { rw [top_add], apply_mod_cast coe_lt_top }, norm_cast at h, apply_mod_cast add_lt_add_right h end protected lemma add_lt_add_iff_right {x y z : enat} (hz : z ≠ ⊤) : x + z < y + z ↔ x < y := ⟨lt_of_add_lt_add_right', λ h, enat.add_lt_add_right h hz⟩ protected lemma add_lt_add_iff_left {x y z : enat} (hz : z ≠ ⊤) : z + x < z + y ↔ x < y := by rw [add_comm z, add_comm z, enat.add_lt_add_iff_right hz] protected lemma lt_add_iff_pos_right {x y : enat} (hx : x ≠ ⊤) : x < x + y ↔ 0 < y := by { conv_rhs { rw [← enat.add_lt_add_iff_left hx] }, rw [add_zero] } lemma lt_add_one {x : enat} (hx : x ≠ ⊤) : x < x + 1 := by { rw [enat.lt_add_iff_pos_right hx], norm_cast, norm_num } lemma le_of_lt_add_one {x y : enat} (h : x < y + 1) : x ≤ y := begin induction y using enat.cases_on with n, apply le_top, rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩, apply_mod_cast nat.le_of_lt_succ, apply_mod_cast h end lemma add_one_le_of_lt {x y : enat} (h : x < y) : x + 1 ≤ y := begin induction y using enat.cases_on with n, apply le_top, rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩, apply_mod_cast nat.succ_le_of_lt, apply_mod_cast h end lemma add_one_le_iff_lt {x y : enat} (hx : x ≠ ⊤) : x + 1 ≤ y ↔ x < y := begin split, swap, exact add_one_le_of_lt, intro h, rcases ne_top_iff.mp hx with ⟨m, rfl⟩, induction y using enat.cases_on with n, apply coe_lt_top, apply_mod_cast nat.lt_of_succ_le, apply_mod_cast h end lemma lt_add_one_iff_lt {x y : enat} (hx : x ≠ ⊤) : x < y + 1 ↔ x ≤ y := begin split, exact le_of_lt_add_one, intro h, rcases ne_top_iff.mp hx with ⟨m, rfl⟩, induction y using enat.cases_on with n, { rw [top_add], apply coe_lt_top }, apply_mod_cast nat.lt_succ_of_le, apply_mod_cast h end lemma add_eq_top_iff {a b : enat} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by apply enat.cases_on a; apply enat.cases_on b; simp; simp only [(enat.coe_add _ _).symm, enat.coe_ne_top]; simp protected lemma add_right_cancel_iff {a b c : enat} (hc : c ≠ ⊤) : a + c = b + c ↔ a = b := begin rcases ne_top_iff.1 hc with ⟨c, rfl⟩, apply enat.cases_on a; apply enat.cases_on b; simp [add_eq_top_iff, coe_ne_top, @eq_comm _ (⊤ : enat)]; simp only [(enat.coe_add _ _).symm, add_left_cancel_iff, enat.coe_inj, add_comm]; tauto end protected lemma add_left_cancel_iff {a b c : enat} (ha : a ≠ ⊤) : a + b = a + c ↔ b = c := by rw [add_comm a, add_comm a, enat.add_right_cancel_iff ha] section with_top /-- Computably converts an `enat` to a `with_top ℕ`. -/ def to_with_top (x : enat) [decidable x.dom]: with_top ℕ := x.to_option lemma to_with_top_top : to_with_top ⊤ = ⊤ := rfl @[simp] lemma to_with_top_top' {h : decidable (⊤ : enat).dom} : to_with_top ⊤ = ⊤ := by convert to_with_top_top lemma to_with_top_zero : to_with_top 0 = 0 := rfl @[simp] lemma to_with_top_zero' {h : decidable (0 : enat).dom}: to_with_top 0 = 0 := by convert to_with_top_zero lemma to_with_top_coe (n : ℕ) : to_with_top n = n := rfl @[simp] lemma to_with_top_coe' (n : ℕ) {h : decidable (n : enat).dom} : to_with_top (n : enat) = n := by convert to_with_top_coe n @[simp] lemma to_with_top_le {x y : enat} : Π [decidable x.dom] [decidable y.dom], by exactI to_with_top x ≤ to_with_top y ↔ x ≤ y := enat.cases_on y (by simp) (enat.cases_on x (by simp) (by intros; simp)) @[simp] lemma to_with_top_lt {x y : enat} [decidable x.dom] [decidable y.dom] : to_with_top x < to_with_top y ↔ x < y := by simp only [lt_iff_le_not_le, to_with_top_le] end with_top section with_top_equiv open_locale classical /-- Order isomorphism between `enat` and `with_top ℕ`. -/ noncomputable def with_top_equiv : enat ≃ with_top ℕ := { to_fun := λ x, to_with_top x, inv_fun := λ x, match x with (some n) := coe n | none := ⊤ end, left_inv := λ x, by apply enat.cases_on x; intros; simp; refl, right_inv := λ x, by cases x; simp [with_top_equiv._match_1]; refl } @[simp] lemma with_top_equiv_top : with_top_equiv ⊤ = ⊤ := to_with_top_top' @[simp] lemma with_top_equiv_coe (n : nat) : with_top_equiv n = n := to_with_top_coe' _ @[simp] lemma with_top_equiv_zero : with_top_equiv 0 = 0 := with_top_equiv_coe _ @[simp] lemma with_top_equiv_le {x y : enat} : with_top_equiv x ≤ with_top_equiv y ↔ x ≤ y := to_with_top_le @[simp] lemma with_top_equiv_lt {x y : enat} : with_top_equiv x < with_top_equiv y ↔ x < y := to_with_top_lt @[simp] lemma with_top_equiv_symm_top : with_top_equiv.symm ⊤ = ⊤ := rfl @[simp] lemma with_top_equiv_symm_coe (n : nat) : with_top_equiv.symm n = n := rfl @[simp] lemma with_top_equiv_symm_zero : with_top_equiv.symm 0 = 0 := rfl @[simp] lemma with_top_equiv_symm_le {x y : with_top ℕ} : with_top_equiv.symm x ≤ with_top_equiv.symm y ↔ x ≤ y := by rw ← with_top_equiv_le; simp @[simp] lemma with_top_equiv_symm_lt {x y : with_top ℕ} : with_top_equiv.symm x < with_top_equiv.symm y ↔ x < y := by rw ← with_top_equiv_lt; simp end with_top_equiv lemma lt_wf : well_founded ((<) : enat → enat → Prop) := show well_founded (λ a b : enat, a < b), by haveI := classical.dec; simp only [to_with_top_lt.symm] {eta := ff}; exact inv_image.wf _ (with_top.well_founded_lt nat.lt_wf) instance : has_well_founded enat := ⟨(<), lt_wf⟩ end enat
7a5a8ff71b9dbe0dea90177d267ac6848becdd0f
4bddde0d06fbd53be6f23d7f5899998e8f63410b
/src/tactic/iconfig/types.lean
068372a7577bd1d9e394a072f552fde7814fb398
[]
no_license
khoek/libiconfig
4816290a5862af14b07683b3d2663e8e62832ef4
6f55c50bc5d852d26ee5ee4c5b52b2cda2a852e5
refs/heads/master
1,586,109,683,212
1,559,567,916,000
1,559,567,916,000
157,085,466
0
1
null
1,559,567,917,000
1,541,945,134,000
Lean
UTF-8
Lean
false
false
494
lean
import .cfgopt namespace iconfig @[derive has_reflect] inductive overload_policy | ignore | override (n : option name) : overload_policy | default (n : option name) : overload_policy @[derive has_reflect] meta structure schema := (default : option cfgopt.value := none) (overload : overload_policy := overload_policy.default none) @[derive has_reflect] meta structure result := (opts : list cfgopt) (sch : list (name × schema)) meta def empty_result : result := ⟨[], []⟩ end iconfig
dc848ee3f29a0d4664cdd9c0e631ae3389eea2d7
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/basic.lean
5fd4164769da2e268424e6f813940c6b9441584b
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
71,528
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Jeremy Avigad -/ import order.filter.ultrafilter import order.filter.partial import algebra.support /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. A filter `F` on `α` has `x` as a cluster point if `cluster_pt x F : 𝓝 x ⊓ F ≠ ⊥`. A map `f : ι → α` clusters at `x` along `F : filter ι` if `map_cluster_pt x F f : cluster_pt x (map f F)`. In particular the notion of cluster point of a sequence `u` is `map_cluster_pt x at_top u`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Notation * `𝓝 x`: the filter `nhds x` of neighborhoods of a point `x`; * `𝓟 s`: the principal filter of a set `s`; * `𝓝[s] x`: the filter `nhds_within x s` of neighborhoods of a point `x` within a set `s`; * `𝓝[≤] x`: the filter `nhds_within x (set.Iic x)` of left-neighborhoods of `x`; * `𝓝[≥] x`: the filter `nhds_within x (set.Ici x)` of right-neighborhoods of `x`; * `𝓝[<] x`: the filter `nhds_within x (set.Iio x)` of punctured left-neighborhoods of `x`; * `𝓝[>] x`: the filter `nhds_within x (set.Ioi x)` of punctured right-neighborhoods of `x`; * `𝓝[≠] x`: the filter `nhds_within x {x}ᶜ` of punctured neighborhoods of `x`. ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in <https://leanprover-community.github.io/theories/topology.html>. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ noncomputable theory open set filter classical open_locale classical filter universes u v w /-! ### Topological spaces -/ /-- A topology on `α`. -/ @[protect_proj] structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space /-- A constructor for topologies by specifying the closed sets, and showing that they satisfy the appropriate conditions. -/ def topological_space.of_closed {α : Type u} (T : set (set α)) (empty_mem : ∅ ∈ T) (sInter_mem : ∀ A ⊆ T, ⋂₀ A ∈ T) (union_mem : ∀ A B ∈ T, A ∪ B ∈ T) : topological_space α := { is_open := λ X, Xᶜ ∈ T, is_open_univ := by simp [empty_mem], is_open_inter := λ s t hs ht, by simpa [set.compl_inter] using union_mem sᶜ hs tᶜ ht, is_open_sUnion := λ s hs, by rw set.compl_sUnion; exact sInter_mem (set.compl '' s) (λ z ⟨y, hy, hz⟩, by simpa [hz.symm] using hs y hy) } section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open.inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma topological_space_eq_iff {t t' : topological_space α} : t = t' ↔ ∀ s, @is_open α t s ↔ @is_open α t' s := ⟨λ h s, h ▸ iff.rfl, λ h, by { ext, exact h _ }⟩ lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open.union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open.inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open.inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open.and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open.inter /-- A set is closed if its complement is open -/ class is_closed (s : set α) : Prop := (is_open_compl : is_open sᶜ) @[simp] lemma is_open_compl_iff {s : set α} : is_open sᶜ ↔ is_closed s := ⟨λ h, ⟨h⟩, λ h, h.is_open_compl⟩ @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by { rw [← is_open_compl_iff, compl_empty], exact is_open_univ } @[simp] lemma is_closed_univ : is_closed (univ : set α) := by { rw [← is_open_compl_iff, compl_univ], exact is_open_empty } lemma is_closed.union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by { rw [← is_open_compl_iff] at *, rw compl_union, exact is_open.inter h₁ h₂ } lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simpa only [← is_open_compl_iff, compl_sInter, sUnion_image] using is_open_bUnion lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i lemma is_closed_bInter {s : set β} {f : β → set α} (h : ∀ i ∈ s, is_closed (f i)) : is_closed (⋂ i ∈ s, f i) := is_closed_Inter $ λ i, is_closed_Inter $ h i @[simp] lemma is_closed_compl_iff {s : set α} : is_closed sᶜ ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open.is_closed_compl {s : set α} (hs : is_open s) : is_closed sᶜ := is_closed_compl_iff.2 hs lemma is_open.sdiff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open.inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed.inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by { rw [← is_open_compl_iff] at *, rw compl_inter, exact is_open.union h₁ h₂ } lemma is_closed.sdiff {s t : set α} (h₁ : is_closed s) (h₂ : is_open t) : is_closed (s \ t) := is_closed.inter h₁ (is_closed_compl_iff.mpr h₂) lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed.union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = {x | p x}ᶜ ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed.union (is_closed_compl_iff.mpr hp) hq lemma is_closed.not : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-! ### Interior of a set -/ /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma is_open.interior_eq {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, is_open.interior_eq⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma subset_interior_iff {s t : set α} : t ⊆ interior s ↔ ∃ U, is_open U ∧ t ⊆ U ∧ U ⊆ s := ⟨λ h, ⟨interior s, is_open_interior, h, interior_subset⟩, λ ⟨U, hU, htU, hUs⟩, htU.trans (interior_maximal hUs hU)⟩ @[mono] lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := is_open_empty.interior_eq @[simp] lemma interior_univ : interior (univ : set α) = univ := is_open_univ.interior_eq @[simp] lemma interior_eq_univ {s : set α} : interior s = univ ↔ s = univ := ⟨λ h, univ_subset_iff.mp $ h.symm.trans_le interior_subset, λ h, h.symm ▸ interior_univ⟩ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := is_open_interior.interior_eq @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open.inter is_open_interior is_open_interior) @[simp] lemma finset.interior_Inter {ι : Type*} (s : finset ι) (f : ι → set α) : interior (⋂ i ∈ s, f i) = ⋂ i ∈ s, interior (f i) := begin classical, refine s.induction_on (by simp) _, intros i s h₁ h₂, simp [h₂], end @[simp] lemma interior_Inter_of_fintype {ι : Type*} [fintype ι] (f : ι → set α) : interior (⋂ i, f i) = ⋂ i, interior (f i) := by { convert finset.univ.interior_Inter f; simp, } lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open.sdiff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] lemma interior_Inter_subset (s : ι → set α) : interior (⋂ i, s i) ⊆ ⋂ i, interior (s i) := subset_Inter $ λ i, interior_mono $ Inter_subset _ _ lemma interior_Inter₂_subset (p : ι → Sort*) (s : Π i, p i → set α) : interior (⋂ i j, s i j) ⊆ ⋂ i j, interior (s i j) := (interior_Inter_subset _).trans $ Inter_mono $ λ i, interior_Inter_subset _ lemma interior_sInter_subset (S : set (set α)) : interior (⋂₀ S) ⊆ ⋂ s ∈ S, interior s := calc interior (⋂₀ S) = interior (⋂ s ∈ S, s) : by rw sInter_eq_bInter ... ⊆ ⋂ s ∈ S, interior s : interior_Inter₂_subset _ _ /-! ### Closure of a set -/ /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma not_mem_of_not_mem_closure {s : set α} {P : α} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma disjoint.closure_left {s t : set α} (hd : disjoint s t) (ht : is_open t) : disjoint (closure s) t := disjoint_compl_left.mono_left $ closure_minimal (disjoint_iff_subset_compl_right.1 hd) ht.is_closed_compl lemma disjoint.closure_right {s t : set α} (hd : disjoint s t) (hs : is_open s) : disjoint s (closure t) := (hd.symm.closure_left hs).symm lemma is_closed.closure_eq {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma is_closed.closure_subset {s : set α} (hs : is_closed s) : closure s ⊆ s := closure_minimal (subset.refl _) hs lemma is_closed.closure_subset_iff {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma is_closed.mem_iff_closure_subset {α : Type*} [topological_space α] {U : set α} (hU : is_closed U) {x : α} : x ∈ U ↔ closure ({x} : set α) ⊆ U := (hU.closure_subset_iff.trans set.singleton_subset_iff).symm @[mono] lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) := λ _ _, closure_mono lemma diff_subset_closure_iff {s t : set α} : s \ t ⊆ closure t ↔ s ⊆ closure t := by rw [diff_subset_iff, union_eq_self_of_subset_left subset_closure] lemma closure_inter_subset_inter_closure (s t : set α) : closure (s ∩ t) ⊆ closure s ∩ closure t := (monotone_closure α).map_inf_le s t lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, is_closed.closure_eq⟩ lemma closure_subset_iff_is_closed {s : set α} : closure s ⊆ s ↔ is_closed s := ⟨is_closed_of_closure_subset, is_closed.closure_subset⟩ @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := is_closed_empty.closure_eq @[simp] lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := ⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩ @[simp] lemma closure_nonempty_iff {s : set α} : (closure s).nonempty ↔ s.nonempty := by simp only [← ne_empty_iff_nonempty, ne.def, closure_empty_iff] alias closure_nonempty_iff ↔ set.nonempty.of_closure set.nonempty.closure @[simp] lemma closure_univ : closure (univ : set α) = univ := is_closed_univ.closure_eq @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := is_closed_closure.closure_eq @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed.union is_closed_closure is_closed_closure) ((monotone_closure α).le_map_sup s t) @[simp] lemma finset.closure_bUnion {ι : Type*} (s : finset ι) (f : ι → set α) : closure (⋃ i ∈ s, f i) = ⋃ i ∈ s, closure (f i) := begin classical, refine s.induction_on (by simp) _, intros i s h₁ h₂, simp [h₂], end @[simp] lemma closure_Union_of_fintype {ι : Type*} [fintype ι] (f : ι → set α) : closure (⋃ i, f i) = ⋃ i, closure (f i) := by { convert finset.univ.closure_bUnion f; simp, } lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = (interior sᶜ)ᶜ := begin rw [interior, closure, compl_sUnion, compl_image_set_of], simp only [compl_subset_compl, is_open_compl_iff], end @[simp] lemma interior_compl {s : set α} : interior sᶜ = (closure s)ᶜ := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure sᶜ = (interior s)ᶜ := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty := ⟨λ h o oo ao, classical.by_contradiction $ λ os, have s ⊆ oᶜ, from λ x xs xo, os ⟨x, xo, xs⟩, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := (H _ h₁.is_open_compl nc) in hc (h₂ hs)⟩ /-- A set is dense in a topological space if every point belongs to its closure. -/ def dense (s : set α) : Prop := ∀ x, x ∈ closure s lemma dense_iff_closure_eq {s : set α} : dense s ↔ closure s = univ := eq_univ_iff_forall.symm lemma dense.closure_eq {s : set α} (h : dense s) : closure s = univ := dense_iff_closure_eq.mp h lemma interior_eq_empty_iff_dense_compl {s : set α} : interior s = ∅ ↔ dense sᶜ := by rw [dense_iff_closure_eq, closure_compl, compl_univ_iff] lemma dense.interior_compl {s : set α} (h : dense s) : interior sᶜ = ∅ := interior_eq_empty_iff_dense_compl.2 $ by rwa compl_compl /-- The closure of a set `s` is dense if and only if `s` is dense. -/ @[simp] lemma dense_closure {s : set α} : dense (closure s) ↔ dense s := by rw [dense, dense, closure_closure] alias dense_closure ↔ dense.of_closure dense.closure @[simp] lemma dense_univ : dense (univ : set α) := λ x, subset_closure trivial /-- A set is dense if and only if it has a nonempty intersection with each nonempty open set. -/ lemma dense_iff_inter_open {s : set α} : dense s ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty := begin split ; intro h, { rintros U U_op ⟨x, x_in⟩, exact mem_closure_iff.1 (by simp only [h.closure_eq]) U U_op x_in }, { intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op ⟨_, x_in⟩ }, end alias dense_iff_inter_open ↔ dense.inter_open_nonempty _ lemma dense.exists_mem_open {s : set α} (hs : dense s) {U : set α} (ho : is_open U) (hne : U.nonempty) : ∃ x ∈ s, x ∈ U := let ⟨x, hx⟩ := hs.inter_open_nonempty U ho hne in ⟨x, hx.2, hx.1⟩ lemma dense.nonempty_iff {s : set α} (hs : dense s) : s.nonempty ↔ nonempty α := ⟨λ ⟨x, hx⟩, ⟨x⟩, λ ⟨x⟩, let ⟨y, hy⟩ := hs.inter_open_nonempty _ is_open_univ ⟨x, trivial⟩ in ⟨y, hy.2⟩⟩ lemma dense.nonempty [h : nonempty α] {s : set α} (hs : dense s) : s.nonempty := hs.nonempty_iff.2 h @[mono] lemma dense.mono {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : dense s₁) : dense s₂ := λ x, closure_mono h (hd x) /-- Complement to a singleton is dense if and only if the singleton is not an open set. -/ lemma dense_compl_singleton_iff_not_open {x : α} : dense ({x}ᶜ : set α) ↔ ¬is_open ({x} : set α) := begin fsplit, { intros hd ho, exact (hd.inter_open_nonempty _ ho (singleton_nonempty _)).ne_empty (inter_compl_self _) }, { refine λ ho, dense_iff_inter_open.2 (λ U hU hne, inter_compl_nonempty_iff.2 $ λ hUx, _), obtain rfl : U = {x}, from eq_singleton_iff_nonempty_unique_mem.2 ⟨hne, hUx⟩, exact ho hU } end /-! ### Frontier of a set -/ /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure sᶜ := by rw [closure_compl, frontier, diff_eq] lemma frontier_subset_closure {s : set α} : frontier s ⊆ closure s := diff_subset _ _ /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier sᶜ = frontier s := by simp only [frontier_eq_closure_inter_closure, compl_compl, inter_comm] @[simp] lemma frontier_univ : frontier (univ : set α) = ∅ := by simp [frontier] @[simp] lemma frontier_empty : frontier (∅ : set α) = ∅ := by simp [frontier] lemma frontier_inter_subset (s t : set α) : frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) := begin simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union], convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t), simp only [inter_distrib_left, inter_distrib_right, inter_assoc], congr' 2, apply inter_comm end lemma frontier_union_subset (s t : set α) : frontier (s ∪ t) ⊆ (frontier s ∩ closure tᶜ) ∪ (closure sᶜ ∩ frontier t) := by simpa only [frontier_compl, ← compl_union] using frontier_inter_subset sᶜ tᶜ lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s := by rw [frontier, hs.closure_eq] lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s := by rw [frontier, hs.interior_eq] lemma is_open.inter_frontier_eq {s : set α} (hs : is_open s) : s ∩ frontier s = ∅ := by rw [hs.frontier_eq, inter_diff_self] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed.inter is_closed_closure is_closed_closure /-- The frontier of a closed set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, from h.frontier_eq, have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end lemma closure_eq_interior_union_frontier (s : set α) : closure s = interior s ∪ frontier s := (union_diff_cancel interior_subset_closure).symm lemma closure_eq_self_union_frontier (s : set α) : closure s = s ∪ frontier s := (union_diff_cancel' interior_subset subset_closure).symm lemma is_open.inter_frontier_eq_empty_of_disjoint {s t : set α} (ht : is_open t) (hd : disjoint s t) : t ∩ frontier s = ∅ := begin rw [inter_comm, ← subset_compl_iff_disjoint], exact subset.trans frontier_subset_closure (closure_minimal (λ _, disjoint_left.1 hd) (is_closed_compl_iff.2 ht)) end lemma frontier_eq_inter_compl_interior {s : set α} : frontier s = (interior s)ᶜ ∩ (interior (sᶜ))ᶜ := by { rw [←frontier_compl, ←closure_compl], refl } lemma compl_frontier_eq_union_interior {s : set α} : (frontier s)ᶜ = interior s ∪ interior sᶜ := begin rw frontier_eq_inter_compl_interior, simp only [compl_inter, compl_compl], end /-! ### Neighborhoods -/ /-- A set is called a neighborhood of `a` if it contains an open set around `a`. The set of all neighborhoods of `a` forms a filter, the neighborhood filter at `a`, is here defined as the infimum over the principal filters of all open sets containing `a`. -/ @[irreducible] def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) localized "notation `𝓝` := nhds" in topological_space /-- The "neighborhood within" filter. Elements of `𝓝[s] a` are sets containing the intersection of `s` and a neighborhood of `a`. -/ def nhds_within (a : α) (s : set α) : filter α := 𝓝 a ⊓ 𝓟 s localized "notation `𝓝[` s `] ` x:100 := nhds_within x s" in topological_space localized "notation `𝓝[≠] ` x:100 := nhds_within x {x}ᶜ" in topological_space localized "notation `𝓝[≥] ` x:100 := nhds_within x (set.Ici x)" in topological_space localized "notation `𝓝[≤] ` x:100 := nhds_within x (set.Iic x)" in topological_space localized "notation `𝓝[>] ` x:100 := nhds_within x (set.Ioi x)" in topological_space localized "notation `𝓝[<] ` x:100 := nhds_within x (set.Iio x)" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) := by rw nhds /-- The open sets containing `a` are a basis for the neighborhood filter. See `nhds_basis_opens'` for a variant using open neighborhoods instead. -/ lemma nhds_basis_opens (a : α) : (𝓝 a).has_basis (λ s : set α, a ∈ s ∧ is_open s) (λ x, x) := begin rw nhds_def, exact has_basis_binfi_principal (λ s ⟨has, hs⟩ t ⟨hat, ht⟩, ⟨s ∩ t, ⟨⟨has, hat⟩, is_open.inter hs ht⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩) ⟨univ, ⟨mem_univ a, is_open_univ⟩⟩ end /-- A filter lies below the neighborhood filter at `a` iff it contains every open set around `a`. -/ lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] /-- To show a filter is above the neighborhood filter at `a`, it suffices to show that it is above the principal filter of some open set `s` containing `a`. -/ lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : 𝓟 s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma mem_nhds_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃ t ⊆ s, is_open t ∧ a ∈ t := (nhds_basis_opens a).mem_iff.trans ⟨λ ⟨t, ⟨hat, ht⟩, hts⟩, ⟨t, hts, ht, hat⟩, λ ⟨t, hts, ht, hat⟩, ⟨t, ⟨hat, ht⟩, hts⟩⟩ /-- A predicate is true in a neighborhood of `a` iff it is true for all the points in an open set containing `a`. -/ lemma eventually_nhds_iff {a : α} {p : α → Prop} : (∀ᶠ x in 𝓝 a, p x) ↔ ∃ (t : set α), (∀ x ∈ t, p x) ∧ is_open t ∧ a ∈ t := mem_nhds_iff.trans $ by simp only [subset_def, exists_prop, mem_set_of_eq] lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 (image f s)) := ((nhds_basis_opens a).map f).eq_binfi lemma mem_of_mem_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_iff.1 H in ht hs /-- If a predicate is true in a neighborhood of `a`, then it is true for `a`. -/ lemma filter.eventually.self_of_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : p a := mem_of_mem_nhds h lemma is_open.mem_nhds {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_iff.2 ⟨s, subset.refl _, hs, ha⟩ lemma is_open.mem_nhds_iff {a : α} {s : set α} (hs : is_open s) : s ∈ 𝓝 a ↔ a ∈ s := ⟨mem_of_mem_nhds, λ ha, mem_nhds_iff.2 ⟨s, subset.refl _, hs, ha⟩⟩ lemma is_closed.compl_mem_nhds {a : α} {s : set α} (hs : is_closed s) (ha : a ∉ s) : sᶜ ∈ 𝓝 a := hs.is_open_compl.mem_nhds (mem_compl ha) lemma is_open.eventually_mem {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : ∀ᶠ x in 𝓝 a, x ∈ s := is_open.mem_nhds hs ha /-- The open neighborhoods of `a` are a basis for the neighborhood filter. See `nhds_basis_opens` for a variant using open sets around `a` instead. -/ lemma nhds_basis_opens' (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_open s) (λ x, x) := begin convert nhds_basis_opens a, ext s, split, { rintros ⟨s_in, s_op⟩, exact ⟨mem_of_mem_nhds s_in, s_op⟩ }, { rintros ⟨a_in, s_op⟩, exact ⟨is_open.mem_nhds s_op a_in, s_op⟩ }, end /-- If `U` is a neighborhood of each point of a set `s` then it is a neighborhood of `s`: it contains an open set containing `s`. -/ lemma exists_open_set_nhds {s U : set α} (h : ∀ x ∈ s, U ∈ 𝓝 x) : ∃ V : set α, s ⊆ V ∧ is_open V ∧ V ⊆ U := begin have := λ x hx, (nhds_basis_opens x).mem_iff.1 (h x hx), choose! Z hZ hZ' using this, refine ⟨⋃ x ∈ s, Z x, λ x hx, mem_bUnion hx (hZ x hx).1, is_open_Union _, Union₂_subset hZ'⟩, intro x, by_cases hx : x ∈ s ; simp [hx], exact (hZ x hx).2, end /-- If `U` is a neighborhood of each point of a set `s` then it is a neighborhood of s: it contains an open set containing `s`. -/ lemma exists_open_set_nhds' {s U : set α} (h : U ∈ ⨆ x ∈ s, 𝓝 x) : ∃ V : set α, s ⊆ V ∧ is_open V ∧ V ⊆ U := exists_open_set_nhds (by simpa using h) /-- If a predicate is true in a neighbourhood of `a`, then for `y` sufficiently close to `a` this predicate is true in a neighbourhood of `y`. -/ lemma filter.eventually.eventually_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : ∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x := let ⟨t, htp, hto, ha⟩ := eventually_nhds_iff.1 h in eventually_nhds_iff.2 ⟨t, λ x hx, eventually_nhds_iff.2 ⟨t, htp, hto, hx⟩, hto, ha⟩ @[simp] lemma eventually_eventually_nhds {p : α → Prop} {a : α} : (∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x) ↔ ∀ᶠ x in 𝓝 a, p x := ⟨λ h, h.self_of_nhds, λ h, h.eventually_nhds⟩ @[simp] lemma eventually_mem_nhds {s : set α} {a : α} : (∀ᶠ x in 𝓝 a, s ∈ 𝓝 x) ↔ s ∈ 𝓝 a := eventually_eventually_nhds @[simp] lemma nhds_bind_nhds : (𝓝 a).bind 𝓝 = 𝓝 a := filter.ext $ λ s, eventually_eventually_nhds @[simp] lemma eventually_eventually_eq_nhds {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g) ↔ f =ᶠ[𝓝 a] g := eventually_eventually_nhds lemma filter.eventually_eq.eq_of_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : f a = g a := h.self_of_nhds @[simp] lemma eventually_eventually_le_nhds [has_le β] {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g) ↔ f ≤ᶠ[𝓝 a] g := eventually_eventually_nhds /-- If two functions are equal in a neighbourhood of `a`, then for `y` sufficiently close to `a` these functions are equal in a neighbourhood of `y`. -/ lemma filter.eventually_eq.eventually_eq_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g := h.eventually_nhds /-- If `f x ≤ g x` in a neighbourhood of `a`, then for `y` sufficiently close to `a` we have `f x ≤ g x` in a neighbourhood of `y`. -/ lemma filter.eventually_le.eventually_le_nhds [has_le β] {f g : α → β} {a : α} (h : f ≤ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g := h.eventually_nhds theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := ((nhds_basis_opens x).forall_iff hP).trans $ by simp only [and_comm (x ∈ _), and_imp] theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem' $ assume _, ha lemma tendsto_at_top_of_eventually_const {ι : Type*} [semilattice_sup ι] [nonempty ι] {x : α} {u : ι → α} {i₀ : ι} (h : ∀ i ≥ i₀, u i = x) : tendsto u at_top (𝓝 x) := tendsto.congr' (eventually_eq.symm (eventually_at_top.mpr ⟨i₀, h⟩)) tendsto_const_nhds lemma tendsto_at_bot_of_eventually_const {ι : Type*} [semilattice_inf ι] [nonempty ι] {x : α} {u : ι → α} {i₀ : ι} (h : ∀ i ≤ i₀, u i = x) : tendsto u at_bot (𝓝 x) := tendsto.congr' (eventually_eq.symm (eventually_at_bot.mpr ⟨i₀, h⟩)) tendsto_const_nhds lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a s hs, mem_pure.2 $ mem_of_mem_nhds hs lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := (tendsto_pure_pure f a).mono_right (pure_le_nhds _) lemma order_top.tendsto_at_top_nhds {α : Type*} [partial_order α] [order_top α] [topological_space β] (f : α → β) : tendsto f at_top (𝓝 $ f ⊤) := (tendsto_at_top_pure f).mono_right (pure_le_nhds _) @[simp] instance nhds_ne_bot {a : α} : ne_bot (𝓝 a) := ne_bot_of_le (pure_le_nhds a) /-! ### Cluster points In this section we define [cluster points](https://en.wikipedia.org/wiki/Limit_point) (also known as limit points and accumulation points) of a filter and of a sequence. -/ /-- A point `x` is a cluster point of a filter `F` if 𝓝 x ⊓ F ≠ ⊥. Also known as an accumulation point or a limit point. -/ def cluster_pt (x : α) (F : filter α) : Prop := ne_bot (𝓝 x ⊓ F) lemma cluster_pt.ne_bot {x : α} {F : filter α} (h : cluster_pt x F) : ne_bot (𝓝 x ⊓ F) := h lemma filter.has_basis.cluster_pt_iff {ιa ιF} {pa : ιa → Prop} {sa : ιa → set α} {pF : ιF → Prop} {sF : ιF → set α} {F : filter α} (ha : (𝓝 a).has_basis pa sa) (hF : F.has_basis pF sF) : cluster_pt a F ↔ ∀ ⦃i⦄ (hi : pa i) ⦃j⦄ (hj : pF j), (sa i ∩ sF j).nonempty := ha.inf_basis_ne_bot_iff hF lemma cluster_pt_iff {x : α} {F : filter α} : cluster_pt x F ↔ ∀ ⦃U : set α⦄ (hU : U ∈ 𝓝 x) ⦃V⦄ (hV : V ∈ F), (U ∩ V).nonempty := inf_ne_bot_iff /-- `x` is a cluster point of a set `s` if every neighbourhood of `x` meets `s` on a nonempty set. -/ lemma cluster_pt_principal_iff {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∀ U ∈ 𝓝 x, (U ∩ s).nonempty := inf_principal_ne_bot_iff lemma cluster_pt_principal_iff_frequently {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∃ᶠ y in 𝓝 x, y ∈ s := by simp only [cluster_pt_principal_iff, frequently_iff, set.nonempty, exists_prop, mem_inter_iff] lemma cluster_pt.of_le_nhds {x : α} {f : filter α} (H : f ≤ 𝓝 x) [ne_bot f] : cluster_pt x f := by rwa [cluster_pt, inf_eq_right.mpr H] lemma cluster_pt.of_le_nhds' {x : α} {f : filter α} (H : f ≤ 𝓝 x) (hf : ne_bot f) : cluster_pt x f := cluster_pt.of_le_nhds H lemma cluster_pt.of_nhds_le {x : α} {f : filter α} (H : 𝓝 x ≤ f) : cluster_pt x f := by simp only [cluster_pt, inf_eq_left.mpr H, nhds_ne_bot] lemma cluster_pt.mono {x : α} {f g : filter α} (H : cluster_pt x f) (h : f ≤ g) : cluster_pt x g := ⟨ne_bot_of_le_ne_bot H.ne $ inf_le_inf_left _ h⟩ lemma cluster_pt.of_inf_left {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x f := H.mono inf_le_left lemma cluster_pt.of_inf_right {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x g := H.mono inf_le_right lemma ultrafilter.cluster_pt_iff {x : α} {f : ultrafilter α} : cluster_pt x f ↔ ↑f ≤ 𝓝 x := ⟨f.le_of_inf_ne_bot', λ h, cluster_pt.of_le_nhds h⟩ /-- A point `x` is a cluster point of a sequence `u` along a filter `F` if it is a cluster point of `map u F`. -/ def map_cluster_pt {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : Prop := cluster_pt x (map u F) lemma map_cluster_pt_iff {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : map_cluster_pt x F u ↔ ∀ s ∈ 𝓝 x, ∃ᶠ a in F, u a ∈ s := by { simp_rw [map_cluster_pt, cluster_pt, inf_ne_bot_iff_frequently_left, frequently_map], refl } lemma map_cluster_pt_of_comp {ι δ :Type*} {F : filter ι} {φ : δ → ι} {p : filter δ} {x : α} {u : ι → α} [ne_bot p] (h : tendsto φ p F) (H : tendsto (u ∘ φ) p (𝓝 x)) : map_cluster_pt x F u := begin have := calc map (u ∘ φ) p = map u (map φ p) : map_map ... ≤ map u F : map_mono h, have : map (u ∘ φ) p ≤ 𝓝 x ⊓ map u F, from le_inf H this, exact ne_bot_of_le this end /-! ### Interior, closure and frontier in terms of neighborhoods -/ lemma interior_eq_nhds' {s : set α} : interior s = {a | s ∈ 𝓝 a} := set.ext $ λ x, by simp only [mem_interior, mem_nhds_iff, mem_set_of_eq] lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ 𝓟 s} := interior_eq_nhds'.trans $ by simp only [le_principal_iff] lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by rw [interior_eq_nhds', mem_set_of_eq] @[simp] lemma interior_mem_nhds {s : set α} {a : α} : interior s ∈ 𝓝 a ↔ s ∈ 𝓝 a := ⟨λ h, mem_of_superset h interior_subset, λ h, is_open.mem_nhds is_open_interior (mem_interior_iff_mem_nhds.2 h)⟩ lemma interior_set_of_eq {p : α → Prop} : interior {x | p x} = {x | ∀ᶠ y in 𝓝 x, p y} := interior_eq_nhds' lemma is_open_set_of_eventually_nhds {p : α → Prop} : is_open {x | ∀ᶠ y in 𝓝 x, p y} := by simp only [← interior_set_of_eq, is_open_interior] lemma subset_interior_iff_nhds {s V : set α} : s ⊆ interior V ↔ ∀ x ∈ s, V ∈ 𝓝 x := show (∀ x, x ∈ s → x ∈ _) ↔ _, by simp_rw mem_interior_iff_mem_nhds lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ 𝓟 s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ 𝓟 s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff theorem is_open_iff_ultrafilter {s : set α} : is_open s ↔ (∀ (x ∈ s) (l : ultrafilter α), ↑l ≤ 𝓝 x → s ∈ l) := by simp_rw [is_open_iff_mem_nhds, ← mem_iff_ultrafilter] lemma is_open_singleton_iff_nhds_eq_pure {α : Type*} [topological_space α] (a : α) : is_open ({a} : set α) ↔ 𝓝 a = pure a := begin split, { intros h, apply le_antisymm _ (pure_le_nhds a), rw le_pure_iff, exact h.mem_nhds (mem_singleton a) }, { intros h, simp [is_open_iff_nhds, h] } end lemma mem_closure_iff_frequently {s : set α} {a : α} : a ∈ closure s ↔ ∃ᶠ x in 𝓝 a, x ∈ s := by rw [filter.frequently, filter.eventually, ← mem_interior_iff_mem_nhds, closure_eq_compl_interior_compl]; refl alias mem_closure_iff_frequently ↔ _ filter.frequently.mem_closure /-- The set of cluster points of a filter is closed. In particular, the set of limit points of a sequence is closed. -/ lemma is_closed_set_of_cluster_pt {f : filter α} : is_closed {x | cluster_pt x f} := begin simp only [cluster_pt, inf_ne_bot_iff_frequently_left, set_of_forall, imp_iff_not_or], refine is_closed_Inter (λ p, is_closed.union _ _); apply is_closed_compl_iff.2, exacts [is_open_set_of_eventually_nhds, is_open_const] end theorem mem_closure_iff_cluster_pt {s : set α} {a : α} : a ∈ closure s ↔ cluster_pt a (𝓟 s) := mem_closure_iff_frequently.trans cluster_pt_principal_iff_frequently.symm lemma mem_closure_iff_nhds_ne_bot {s : set α} : a ∈ closure s ↔ 𝓝 a ⊓ 𝓟 s ≠ ⊥ := mem_closure_iff_cluster_pt.trans ne_bot_iff lemma mem_closure_iff_nhds_within_ne_bot {s : set α} {x : α} : x ∈ closure s ↔ ne_bot (𝓝[s] x) := mem_closure_iff_cluster_pt /-- If `x` is not an isolated point of a topological space, then `{x}ᶜ` is dense in the whole space. -/ lemma dense_compl_singleton (x : α) [ne_bot (𝓝[≠] x)] : dense ({x}ᶜ : set α) := begin intro y, unfreezingI { rcases eq_or_ne y x with rfl|hne }, { rwa mem_closure_iff_nhds_within_ne_bot }, { exact subset_closure hne } end /-- If `x` is not an isolated point of a topological space, then the closure of `{x}ᶜ` is the whole space. -/ @[simp] lemma closure_compl_singleton (x : α) [ne_bot (𝓝[≠] x)] : closure {x}ᶜ = (univ : set α) := (dense_compl_singleton x).closure_eq /-- If `x` is not an isolated point of a topological space, then the interior of `{x}` is empty. -/ @[simp] lemma interior_singleton (x : α) [ne_bot (𝓝[≠] x)] : interior {x} = (∅ : set α) := interior_eq_empty_iff_dense_compl.2 (dense_compl_singleton x) lemma closure_eq_cluster_pts {s : set α} : closure s = {a | cluster_pt a (𝓟 s)} := set.ext $ λ x, mem_closure_iff_cluster_pt theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty := mem_closure_iff_cluster_pt.trans cluster_pt_principal_iff theorem mem_closure_iff_nhds' {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, ∃ y : s, ↑y ∈ t := by simp only [mem_closure_iff_nhds, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_comap_ne_bot {A : set α} {x : α} : x ∈ closure A ↔ ne_bot (comap (coe : A → α) (𝓝 x)) := by simp_rw [mem_closure_iff_nhds, comap_ne_bot_iff, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_nhds_basis' {a : α} {p : ι → Prop} {s : ι → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → (s i ∩ t).nonempty := mem_closure_iff_cluster_pt.trans $ (h.cluster_pt_iff (has_basis_principal _)).trans $ by simp only [exists_prop, forall_const] theorem mem_closure_iff_nhds_basis {a : α} {p : ι → Prop} {s : ι → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → ∃ y ∈ t, y ∈ s i := (mem_closure_iff_nhds_basis' h).trans $ by simp only [set.nonempty, mem_inter_eq, exists_prop, and_comm] /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u ∧ ↑u ≤ 𝓝 x := by simp [closure_eq_cluster_pts, cluster_pt, ← exists_ultrafilter_iff, and.comm] lemma is_closed_iff_cluster_pt {s : set α} : is_closed s ↔ ∀a, cluster_pt a (𝓟 s) → a ∈ s := calc is_closed s ↔ closure s ⊆ s : closure_subset_iff_is_closed.symm ... ↔ (∀a, cluster_pt a (𝓟 s) → a ∈ s) : by simp only [subset_def, mem_closure_iff_cluster_pt] lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀ x, (∀ U ∈ 𝓝 x, (U ∩ s).nonempty) → x ∈ s := by simp_rw [is_closed_iff_cluster_pt, cluster_pt, inf_principal_ne_bot_iff] lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := begin rintro a ⟨hs, ht⟩, have : s ∈ 𝓝 a := is_open.mem_nhds h hs, rw mem_closure_iff_nhds_ne_bot at ht ⊢, rwa [← inf_principal, ← inf_assoc, inf_eq_left.2 (le_principal_iff.2 this)], end lemma closure_inter_open' {s t : set α} (h : is_open t) : closure s ∩ t ⊆ closure (s ∩ t) := by simpa only [inter_comm] using closure_inter_open h lemma dense.open_subset_closure_inter {s t : set α} (hs : dense s) (ht : is_open t) : t ⊆ closure (t ∩ s) := calc t = t ∩ closure s : by rw [hs.closure_eq, inter_univ] ... ⊆ closure (t ∩ s) : closure_inter_open ht lemma mem_closure_of_mem_closure_union {s₁ s₂ : set α} {x : α} (h : x ∈ closure (s₁ ∪ s₂)) (h₁ : s₁ᶜ ∈ 𝓝 x) : x ∈ closure s₂ := begin rw mem_closure_iff_nhds_ne_bot at *, rwa ← calc 𝓝 x ⊓ principal (s₁ ∪ s₂) = 𝓝 x ⊓ (principal s₁ ⊔ principal s₂) : by rw sup_principal ... = (𝓝 x ⊓ principal s₁) ⊔ (𝓝 x ⊓ principal s₂) : inf_sup_left ... = ⊥ ⊔ 𝓝 x ⊓ principal s₂ : by rw inf_principal_eq_bot.mpr h₁ ... = 𝓝 x ⊓ principal s₂ : bot_sup_eq end /-- The intersection of an open dense set with a dense set is a dense set. -/ lemma dense.inter_of_open_left {s t : set α} (hs : dense s) (ht : dense t) (hso : is_open s) : dense (s ∩ t) := λ x, (closure_minimal (closure_inter_open hso) is_closed_closure) $ by simp [hs.closure_eq, ht.closure_eq] /-- The intersection of a dense set with an open dense set is a dense set. -/ lemma dense.inter_of_open_right {s t : set α} (hs : dense s) (ht : dense t) (hto : is_open t) : dense (s ∩ t) := inter_comm t s ▸ ht.inter_of_open_left hs hto lemma dense.inter_nhds_nonempty {s t : set α} (hs : dense s) {x : α} (ht : t ∈ 𝓝 x) : (s ∩ t).nonempty := let ⟨U, hsub, ho, hx⟩ := mem_nhds_iff.1 ht in (hs.inter_open_nonempty U ho ⟨x, hx⟩).mono $ λ y hy, ⟨hy.2, hsub hy.1⟩ lemma closure_diff {s t : set α} : closure s \ closure t ⊆ closure (s \ t) := calc closure s \ closure t = (closure t)ᶜ ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure ((closure t)ᶜ ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma filter.frequently.mem_of_closed {a : α} {s : set α} (h : ∃ᶠ x in 𝓝 a, x ∈ s) (hs : is_closed s) : a ∈ s := hs.closure_subset h.mem_closure lemma is_closed.mem_of_frequently_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hs : is_closed s) (h : ∃ᶠ x in b, f x ∈ s) (hf : tendsto f b (𝓝 a)) : a ∈ s := (hf.frequently $ show ∃ᶠ x in b, (λ y, y ∈ s) (f x), from h).mem_of_closed hs lemma is_closed.mem_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hs : is_closed s) (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ s := hs.mem_of_frequently_of_tendsto h.frequently hf lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ closure s := is_closed_closure.mem_of_tendsto hf $ h.mono (preimage_mono subset_closure) /-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter. Then `f` tends to `a` along `l` restricted to `s` if and only if it tends to `a` along `l`. -/ lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β} {a : α} (h : ∀ x ∉ s, f x = a) : tendsto f (l ⊓ 𝓟 s) (𝓝 a) ↔ tendsto f l (𝓝 a) := begin rw [tendsto_iff_comap, tendsto_iff_comap], replace h : 𝓟 sᶜ ≤ comap f (𝓝 a), { rintros U ⟨t, ht, htU⟩ x hx, have : f x ∈ t, from (h x hx).symm ▸ mem_of_mem_nhds ht, exact htU this }, refine ⟨λ h', _, le_trans inf_le_left⟩, have := sup_le h' h, rw [sup_inf_right, sup_principal, union_compl_self, principal_univ, inf_top_eq, sup_le_iff] at this, exact this.1 end /-! ### Limits of filters in topological spaces -/ section lim /-- If `f` is a filter, then `Lim f` is a limit of the filter, if it exists. -/ noncomputable def Lim [nonempty α] (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a /-- If `f` is a filter satisfying `ne_bot f`, then `Lim' f` is a limit of the filter, if it exists. -/ def Lim' (f : filter α) [ne_bot f] : α := @Lim _ _ (nonempty_of_ne_bot f) f /-- If `F` is an ultrafilter, then `filter.ultrafilter.Lim F` is a limit of the filter, if it exists. Note that dot notation `F.Lim` can be used for `F : ultrafilter α`. -/ def ultrafilter.Lim : ultrafilter α → α := λ F, Lim' F /-- If `f` is a filter in `β` and `g : β → α` is a function, then `lim f` is a limit of `g` at `f`, if it exists. -/ noncomputable def lim [nonempty α] (f : filter β) (g : β → α) : α := Lim (f.map g) /-- If a filter `f` is majorated by some `𝓝 a`, then it is majorated by `𝓝 (Lim f)`. We formulate this lemma with a `[nonempty α]` argument of `Lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma le_nhds_Lim {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (@Lim _ _ (nonempty_of_exists h) f) := epsilon_spec h /-- If `g` tends to some `𝓝 a` along `f`, then it tends to `𝓝 (lim f g)`. We formulate this lemma with a `[nonempty α]` argument of `lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma tendsto_nhds_lim {f : filter β} {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) : tendsto g f (𝓝 $ @lim _ _ _ (nonempty_of_exists h) f g) := le_nhds_Lim h end lim /-! ### Locally finite families -/ /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty } lemma locally_finite.point_finite {f : β → set α} (hf : locally_finite f) (x : α) : finite {b | x ∈ f b} := let ⟨t, hxt, ht⟩ := hf x in ht.subset $ λ b hb, ⟨x, hb, mem_of_mem_nhds hxt⟩ lemma locally_finite_of_fintype [fintype β] (f : β → set α) : locally_finite f := assume x, ⟨univ, univ_mem, finite.of_fintype _⟩ lemma locally_finite.subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, ht₂.subset $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma locally_finite.comp_injective {ι} {f : β → set α} {g : ι → β} (hf : locally_finite f) (hg : function.injective g) : locally_finite (f ∘ g) := λ x, let ⟨t, htx, htf⟩ := hf x in ⟨t, htx, htf.preimage (hg.inj_on _)⟩ lemma locally_finite.closure {f : β → set α} (hf : locally_finite f) : locally_finite (λ i, closure (f i)) := begin intro x, rcases hf x with ⟨s, hsx, hsf⟩, refine ⟨interior s, interior_mem_nhds.2 hsx, hsf.subset $ λ i hi, _⟩, exact (hi.mono (closure_inter_open' is_open_interior)).of_closure.mono (inter_subset_inter_right _ interior_subset) end lemma locally_finite.is_closed_Union {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := begin simp only [← is_open_compl_iff, compl_Union, is_open_iff_mem_nhds, mem_Inter], intros a ha, replace ha : ∀ i, (f i)ᶜ ∈ 𝓝 a := λ i, (h₂ i).is_open_compl.mem_nhds (ha i), rcases h₁ a with ⟨t, h_nhds, h_fin⟩, have : t ∩ (⋂ i ∈ {i | (f i ∩ t).nonempty}, (f i)ᶜ) ∈ 𝓝 a, from inter_mem h_nhds ((bInter_mem h_fin).2 (λ i _, ha i)), filter_upwards [this], simp only [mem_inter_eq, mem_Inter], rintros b ⟨hbt, hn⟩ i hfb, exact hn i ⟨b, hfb, hbt⟩ hfb, end lemma locally_finite.closure_Union {f : β → set α} (h : locally_finite f) : closure (⋃ i, f i) = ⋃ i, closure (f i) := subset.antisymm (closure_minimal (Union_mono $ λ _, subset_closure) $ h.closure.is_closed_Union $ λ _, is_closed_closure) (Union_subset $ λ i, closure_mono $ subset_Union _ _) end locally_finite end topological_space /-! ### Continuity -/ section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. Registered as a structure to make sure it is not unfolded by Lean. -/ structure continuous (f : α → β) : Prop := (is_open_preimage : ∀s, is_open s → is_open (f ⁻¹' s)) lemma continuous_def {f : α → β} : continuous f ↔ (∀s, is_open s → is_open (f ⁻¹' s)) := ⟨λ hf s hs, hf.is_open_preimage s hs, λ h, ⟨h⟩⟩ lemma is_open.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_open s) : is_open (f ⁻¹' s) := hf.is_open_preimage s h lemma continuous.congr {f g : α → β} (h : continuous f) (h' : ∀ x, f x = g x) : continuous g := by { convert h, ext, rw h' } /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.tendsto {f : α → β} {x : α} (h : continuous_at f x) : tendsto f (𝓝 x) (𝓝 (f x)) := h lemma continuous_at_def {f : α → β} {x : α} : continuous_at f x ↔ ∀ A ∈ 𝓝 (f x), f ⁻¹' A ∈ 𝓝 x := iff.rfl lemma continuous_at_congr {f g : α → β} {x : α} (h : f =ᶠ[𝓝 x] g) : continuous_at f x ↔ continuous_at g x := by simp only [continuous_at, tendsto_congr' h, h.eq_of_nhds] lemma continuous_at.congr {f g : α → β} {x : α} (hf : continuous_at f x) (h : f =ᶠ[𝓝 x] g) : continuous_at g x := (continuous_at_congr h).1 hf lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma eventually_eq_zero_nhds {M₀} [has_zero M₀] {a : α} {f : α → M₀} : f =ᶠ[𝓝 a] 0 ↔ a ∉ closure (function.support f) := by rw [← mem_compl_eq, ← interior_compl, mem_interior_iff_mem_nhds, function.compl_support]; refl lemma cluster_pt.map {x : α} {la : filter α} {lb : filter β} (H : cluster_pt x la) {f : α → β} (hfc : continuous_at f x) (hf : tendsto f la lb) : cluster_pt (f x) lb := ⟨ne_bot_of_le_ne_bot ((map_ne_bot_iff f).2 H).ne $ hfc.tendsto.inf hf⟩ /-- See also `interior_preimage_subset_preimage_interior`. -/ lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β} (hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) := interior_maximal (preimage_mono interior_subset) (is_open_interior.preimage hf) lemma continuous_id : continuous (id : α → α) := continuous_def.2 $ assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := continuous_def.2 $ assume s h, (h.preimage hg).preimage hf lemma continuous.iterate {f : α → α} (h : continuous f) (n : ℕ) : continuous (f^[n]) := nat.rec_on n continuous_id (λ n ihn, ihn.comp h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) := ((nhds_basis_opens x).tendsto_iff $ nhds_basis_opens $ f x).2 $ λ t ⟨hxt, ht⟩, ⟨f ⁻¹' t, ⟨hxt, ht.preimage hf⟩, subset.refl _⟩ /-- A version of `continuous.tendsto` that allows one to specify a simpler form of the limit. E.g., one can write `continuous_exp.tendsto' 0 1 exp_zero`. -/ lemma continuous.tendsto' {f : α → β} (hf : continuous f) (x : α) (y : β) (h : f x = y) : tendsto f (𝓝 x) (𝓝 y) := h ▸ hf.tendsto x lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), continuous_def.2 $ assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), from λ a ha, is_open.mem_nhds hs ha, show is_open (f ⁻¹' s), from is_open_iff_nhds.2 $ λ a ha, le_principal_iff.2 $ hf _ (this a ha)⟩ lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x := tendsto_const_nhds lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, continuous_at_const lemma filter.eventually_eq.continuous_at {x : α} {f : α → β} {y : β} (h : f =ᶠ[𝓝 x] (λ _, y)) : continuous_at f x := (continuous_at_congr h).2 tendsto_const_nhds lemma continuous_of_const {f : α → β} (h : ∀ x y, f x = f y) : continuous f := continuous_iff_continuous_at.mpr $ λ x, filter.eventually_eq.continuous_at $ eventually_of_forall (λ y, h y x) lemma continuous_at_id {x : α} : continuous_at id x := continuous_id.continuous_at lemma continuous_at.iterate {f : α → α} {x : α} (hf : continuous_at f x) (hx : f x = x) (n : ℕ) : continuous_at (f^[n]) x := nat.rec_on n continuous_at_id $ λ n ihn, show continuous_at (f^[n] ∘ f) x, from continuous_at.comp (hx.symm ▸ ihn) hf lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, by simpa using (continuous_def.1 hf sᶜ hs.is_open_compl).is_closed_compl, assume hf, continuous_def.2 $ assume s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma is_closed.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_closed s) : is_closed (f ⁻¹' s) := continuous_iff_is_closed.mp hf s h lemma mem_closure_image {f : α → β} {x : α} {s : set α} (hf : continuous_at f x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) := begin rw [mem_closure_iff_nhds_ne_bot] at hx ⊢, rw ← bot_lt_iff_ne_bot, haveI : ne_bot _ := ⟨hx⟩, calc ⊥ < map f (𝓝 x ⊓ principal s) : bot_lt_iff_ne_bot.mpr ne_bot.ne' ... ≤ (map f $ 𝓝 x) ⊓ (map f $ principal s) : map_inf_le ... = (map f $ 𝓝 x) ⊓ (principal $ f '' s) : by rw map_principal ... ≤ 𝓝 (f x) ⊓ (principal $ f '' s) : inf_le_inf hf le_rfl end lemma continuous_at_iff_ultrafilter {f : α → β} {x} : continuous_at f x ↔ ∀ g : ultrafilter α, ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x (g : ultrafilter α), ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] lemma continuous.closure_preimage_subset {f : α → β} (hf : continuous f) (t : set β) : closure (f ⁻¹' t) ⊆ f ⁻¹' (closure t) := begin rw ← (is_closed_closure.preimage hf).closure_eq, exact closure_mono (preimage_mono subset_closure), end lemma continuous.frontier_preimage_subset {f : α → β} (hf : continuous f) (t : set β) : frontier (f ⁻¹' t) ⊆ f ⁻¹' (frontier t) := diff_subset_diff (hf.closure_preimage_subset t) (preimage_interior_subset_interior_preimage hf) /-! ### Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', simp only [ptendsto'_def, mem_nhds_iff], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end /-- If a continuous map `f` maps `s` to `t`, then it maps `closure s` to `closure t`. -/ lemma set.maps_to.closure {s : set α} {t : set β} {f : α → β} (h : maps_to f s t) (hc : continuous f) : maps_to f (closure s) (closure t) := begin simp only [maps_to, mem_closure_iff_cluster_pt], exact λ x hx, hx.map hc.continuous_at (tendsto_principal_principal.2 h) end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := ((maps_to_image f s).closure h).image_subset lemma closure_subset_preimage_closure_image {f : α → β} {s : set α} (h : continuous f) : closure s ⊆ f ⁻¹' (closure (f '' s)) := by { rw ← set.image_subset_iff, exact image_closure_subset_closure_image h } lemma map_mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := set.maps_to.closure ht hf ha /-! ### Function with dense range -/ section dense_range variables {κ ι : Type*} (f : κ → β) (g : β → γ) /-- `f : ι → β` has dense range if its range (image) is a dense subset of β. -/ def dense_range := dense (range f) variables {f} /-- A surjective map has dense range. -/ lemma function.surjective.dense_range (hf : function.surjective f) : dense_range f := λ x, by simp [hf.range_eq] lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ := dense_iff_closure_eq lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ := h.closure_eq lemma dense.dense_range_coe {s : set α} (h : dense s) : dense_range (coe : s → α) := by simpa only [dense_range, subtype.range_coe_subtype] lemma continuous.range_subset_closure_image_dense {f : α → β} (hf : continuous f) {s : set α} (hs : dense s) : range f ⊆ closure (f '' s) := by { rw [← image_univ, ← hs.closure_eq], exact image_closure_subset_closure_image hf } /-- The image of a dense set under a continuous map with dense range is a dense set. -/ lemma dense_range.dense_image {f : α → β} (hf' : dense_range f) (hf : continuous f) {s : set α} (hs : dense s) : dense (f '' s) := (hf'.mono $ hf.range_subset_closure_image_dense hs).of_closure /-- If `f` has dense range and `s` is an open set in the codomain of `f`, then the image of the preimage of `s` under `f` is dense in `s`. -/ lemma dense_range.subset_closure_image_preimage_of_is_open (hf : dense_range f) {s : set β} (hs : is_open s) : s ⊆ closure (f '' (f ⁻¹' s)) := by { rw image_preimage_eq_inter_range, exact hf.open_subset_closure_inter hs } /-- If a continuous map with dense range maps a dense set to a subset of `t`, then `t` is a dense set. -/ lemma dense_range.dense_of_maps_to {f : α → β} (hf' : dense_range f) (hf : continuous f) {s : set α} (hs : dense s) {t : set β} (ht : maps_to f s t) : dense t := (hf'.dense_image hf hs).mono ht.image_subset /-- Composition of a continuous map with dense range and a function with dense range has dense range. -/ lemma dense_range.comp {g : β → γ} {f : κ → β} (hg : dense_range g) (hf : dense_range f) (cg : continuous g) : dense_range (g ∘ f) := by { rw [dense_range, range_comp], exact hg.dense_image cg hf } lemma dense_range.nonempty_iff (hf : dense_range f) : nonempty κ ↔ nonempty β := range_nonempty_iff_nonempty.symm.trans hf.nonempty_iff lemma dense_range.nonempty [h : nonempty β] (hf : dense_range f) : nonempty κ := hf.nonempty_iff.mpr h /-- Given a function `f : α → β` with dense range and `b : β`, returns some `a : α`. -/ def dense_range.some (hf : dense_range f) (b : β) : κ := classical.choice $ hf.nonempty_iff.mpr ⟨b⟩ lemma dense_range.exists_mem_open (hf : dense_range f) {s : set β} (ho : is_open s) (hs : s.nonempty) : ∃ a, f a ∈ s := exists_range_iff.1 $ hf.exists_mem_open ho hs lemma dense_range.mem_nhds {f : κ → β} (h : dense_range f) {b : β} {U : set β} (U_in : U ∈ nhds b) : ∃ a, f a ∈ U := begin rcases (mem_closure_iff_nhds.mp ((dense_range_iff_closure_range.mp h).symm ▸ mem_univ b : b ∈ closure (range f)) U U_in) with ⟨_, h, a, rfl⟩, exact ⟨a, h⟩ end end dense_range end continuous /-- The library contains many lemmas stating that functions/operations are continuous. There are many ways to formulate the continuity of operations. Some are more convenient than others. Note: for the most part this note also applies to other properties (`measurable`, `differentiable`, `continuous_on`, ...). ### The traditional way As an example, let's look at addition `(+) : M → M → M`. We can state that this is continuous in different definitionally equal ways (omitting some typing information) * `continuous (λ p, p.1 + p.2)`; * `continuous (function.uncurry (+))`; * `continuous ↿(+)`. (`↿` is notation for recursively uncurrying a function) However, lemmas with this conclusion are not nice to use in practice because 1. They confuse the elaborator. The following two examples fail, because of limitations in the elaboration process. ``` variables {M : Type*} [has_mul M] [topological_space M] [has_continuous_mul M] example : continuous (λ x : M, x + x) := continuous_add.comp _ example : continuous (λ x : M, x + x) := continuous_add.comp (continuous_id.prod_mk continuous_id) ``` The second is a valid proof, which is accepted if you write it as `continuous_add.comp (continuous_id.prod_mk continuous_id : _)` 2. If the operation has more than 2 arguments, they are impractical to use, because in your application the arguments in the domain might be in a different order or associated differently. ### The convenient way A much more convenient way to write continuity lemmas is like `continuous.add`: ``` continuous.add {f g : X → M} (hf : continuous f) (hg : continuous g) : continuous (λ x, f x + g x) ``` The conclusion can be `continuous (f + g)`, which is definitionally equal. This has the following advantages * It supports projection notation, so is shorter to write. * `continuous.add _ _` is recognized correctly by the elaborator and gives useful new goals. * It works generally, since the domain is a variable. As an example for an unary operation, we have `continuous.neg`. ``` continuous.neg {f : α → G} (hf : continuous f) : continuous (λ x, -f x) ``` For unary functions, the elaborator is not confused when applying the traditional lemma (like `continuous_neg`), but it's still convenient to have the short version available (compare `hf.neg.neg.neg` with `continuous_neg.comp $ continuous_neg.comp $ continuous_neg.comp hf`). As a harder example, consider an operation of the following type: ``` def strans {x : F} (γ γ' : path x x) (t₀ : I) : path x x ``` The precise definition is not important, only its type. The correct continuity principle for this operation is something like this: ``` {f : X → F} {γ γ' : ∀ x, path (f x) (f x)} {t₀ s : X → I} (hγ : continuous ↿γ) (hγ' : continuous ↿γ') (ht : continuous t₀) (hs : continuous s) : continuous (λ x, strans (γ x) (γ' x) (t x) (s x)) ``` Note that *all* arguments of `strans` are indexed over `X`, even the basepoint `x`, and the last argument `s` that arises since `path x x` has a coercion to `I → F`. The paths `γ` and `γ'` (which are unary functions from `I`) become binary functions in the continuity lemma. ### Summary * Make sure that your continuity lemmas are stated in the most general way, and in a convenient form. That means that: - The conclusion has a variable `X` as domain (not something like `Y × Z`); - Wherever possible, all point arguments `c : Y` are replaced by functions `c : X → Y`; - All `n`-ary function arguments are replaced by `n+1`-ary functions (`f : Y → Z` becomes `f : X → Y → Z`); - All (relevant) arguments have continuity assumptions, and perhaps there are additional assumptions needed to make the operation continuous; - The function in the conclusion is fully applied. * These remarks are mostly about the format of the *conclusion* of a continuity lemma. In assumptions it's fine to state that a function with more than 1 argument is continuous using `↿` or `function.uncurry`. ### Functions with discontinuities In some cases, you want to work with discontinuous functions, and in certain expressions they are still continuous. For example, consider the fractional part of a number, `fract : ℝ → ℝ`. In this case, you want to add conditions to when a function involving `fract` is continuous, so you get something like this: (assumption `hf` could be weakened, but the important thing is the shape of the conclusion) ``` lemma continuous_on.comp_fract {X Y : Type*} [topological_space X] [topological_space Y] {f : X → ℝ → Y} {g : X → ℝ} (hf : continuous ↿f) (hg : continuous g) (h : ∀ s, f s 0 = f s 1) : continuous (λ x, f x (fract (g x))) ``` With `continuous_at` you can be even more precise about what to prove in case of discontinuities, see e.g. `continuous_at.comp_div_cases`. -/ library_note "continuity lemma statement"
8472ec2c7735efccbcda9c70186eeb7b4f0a2acd
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/list/lex.lean
4065e72068c49b81a2874a93d91bf67cdb1794d6
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,555
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import order.rel_classes /-! # Lexicographic ordering of lists. The lexicographic order on `list α` is defined by `L < M` iff * `[] < (a :: L)` for any `a` and `L`, * `(a :: L) < (b :: M)` where `a < b`, or * `(a :: L) < (a :: M)` where `L < M`. See also `order.lexicographic` for the lexicographic order on pairs. -/ namespace list open nat universes u variables {α : Type u} /-! ### lexicographic ordering -/ /-- Given a strict order `<` on `α`, the lexicographic strict order on `list α`, for which `[a0, ..., an] < [b0, ..., b_k]` if `a0 < b0` or `a0 = b0` and `[a1, ..., an] < [b1, ..., bk]`. The definition is given for any relation `r`, not only strict orders. -/ inductive lex (r : α → α → Prop) : list α → list α → Prop | nil {a l} : lex [] (a :: l) | cons {a l₁ l₂} (h : lex l₁ l₂) : lex (a :: l₁) (a :: l₂) | rel {a₁ l₁ a₂ l₂} (h : r a₁ a₂) : lex (a₁ :: l₁) (a₂ :: l₂) namespace lex theorem cons_iff {r : α → α → Prop} [is_irrefl α r] {a l₁ l₂} : lex r (a :: l₁) (a :: l₂) ↔ lex r l₁ l₂ := ⟨λ h, by cases h with _ _ _ _ _ h _ _ _ _ h; [exact h, exact (irrefl_of r a h).elim], lex.cons⟩ @[simp] theorem not_nil_right (r : α → α → Prop) (l : list α) : ¬ lex r l []. instance is_order_connected (r : α → α → Prop) [is_order_connected α r] [is_trichotomous α r] : is_order_connected (list α) (lex r) := ⟨λ l₁, match l₁ with | _, [], c::l₃, nil := or.inr nil | _, [], c::l₃, rel _ := or.inr nil | _, [], c::l₃, cons _ := or.inr nil | _, b::l₂, c::l₃, nil := or.inl nil | a::l₁, b::l₂, c::l₃, rel h := (is_order_connected.conn _ b _ h).imp rel rel | a::l₁, b::l₂, _::l₃, cons h := begin rcases trichotomous_of r a b with ab | rfl | ab, { exact or.inl (rel ab) }, { exact (_match _ l₂ _ h).imp cons cons }, { exact or.inr (rel ab) } end end⟩ instance is_trichotomous (r : α → α → Prop) [is_trichotomous α r] : is_trichotomous (list α) (lex r) := ⟨λ l₁, match l₁ with | [], [] := or.inr (or.inl rfl) | [], b::l₂ := or.inl nil | a::l₁, [] := or.inr (or.inr nil) | a::l₁, b::l₂ := begin rcases trichotomous_of r a b with ab | rfl | ab, { exact or.inl (rel ab) }, { exact (_match l₁ l₂).imp cons (or.imp (congr_arg _) cons) }, { exact or.inr (or.inr (rel ab)) } end end⟩ instance is_asymm (r : α → α → Prop) [is_asymm α r] : is_asymm (list α) (lex r) := ⟨λ l₁, match l₁ with | a::l₁, b::l₂, lex.rel h₁, lex.rel h₂ := asymm h₁ h₂ | a::l₁, b::l₂, lex.rel h₁, lex.cons h₂ := asymm h₁ h₁ | a::l₁, b::l₂, lex.cons h₁, lex.rel h₂ := asymm h₂ h₂ | a::l₁, b::l₂, lex.cons h₁, lex.cons h₂ := by exact _match _ _ h₁ h₂ end⟩ instance is_strict_total_order (r : α → α → Prop) [is_strict_total_order' α r] : is_strict_total_order' (list α) (lex r) := {..is_strict_weak_order_of_is_order_connected} instance decidable_rel [decidable_eq α] (r : α → α → Prop) [decidable_rel r] : decidable_rel (lex r) | l₁ [] := is_false $ λ h, by cases h | [] (b::l₂) := is_true lex.nil | (a::l₁) (b::l₂) := begin haveI := decidable_rel l₁ l₂, refine decidable_of_iff (r a b ∨ a = b ∧ lex r l₁ l₂) ⟨λ h, _, λ h, _⟩, { rcases h with h | ⟨rfl, h⟩, { exact lex.rel h }, { exact lex.cons h } }, { rcases h with _|⟨_,_,_,h⟩|⟨_,_,_,_,h⟩, { exact or.inr ⟨rfl, h⟩ }, { exact or.inl h } } end theorem append_right (r : α → α → Prop) : ∀ {s₁ s₂} t, lex r s₁ s₂ → lex r s₁ (s₂ ++ t) | _ _ t nil := nil | _ _ t (cons h) := cons (append_right _ h) | _ _ t (rel r) := rel r theorem append_left (R : α → α → Prop) {t₁ t₂} (h : lex R t₁ t₂) : ∀ s, lex R (s ++ t₁) (s ++ t₂) | [] := h | (a::l) := cons (append_left l) theorem imp {r s : α → α → Prop} (H : ∀ a b, r a b → s a b) : ∀ l₁ l₂, lex r l₁ l₂ → lex s l₁ l₂ | _ _ nil := nil | _ _ (cons h) := cons (imp _ _ h) | _ _ (rel r) := rel (H _ _ r) theorem to_ne : ∀ {l₁ l₂ : list α}, lex (≠) l₁ l₂ → l₁ ≠ l₂ | _ _ (cons h) e := to_ne h (list.cons.inj e).2 | _ _ (rel r) e := r (list.cons.inj e).1 theorem _root_.decidable.list.lex.ne_iff [decidable_eq α] {l₁ l₂ : list α} (H : length l₁ ≤ length l₂) : lex (≠) l₁ l₂ ↔ l₁ ≠ l₂ := ⟨to_ne, λ h, begin induction l₁ with a l₁ IH generalizing l₂; cases l₂ with b l₂, { contradiction }, { apply nil }, { exact (not_lt_of_ge H).elim (succ_pos _) }, { by_cases ab : a = b, { subst b, apply cons, exact IH (le_of_succ_le_succ H) (mt (congr_arg _) h) }, { exact rel ab } } end⟩ theorem ne_iff {l₁ l₂ : list α} (H : length l₁ ≤ length l₂) : lex (≠) l₁ l₂ ↔ l₁ ≠ l₂ := by classical; exact decidable.list.lex.ne_iff H end lex --Note: this overrides an instance in core lean instance has_lt' [has_lt α] : has_lt (list α) := ⟨lex (<)⟩ theorem nil_lt_cons [has_lt α] (a : α) (l : list α) : [] < a :: l := lex.nil instance [linear_order α] : linear_order (list α) := linear_order_of_STO' (lex (<)) --Note: this overrides an instance in core lean instance has_le' [linear_order α] : has_le (list α) := preorder.to_has_le _ end list
4e10f15ae05e9039a2906391d6110d9ab2c720ef
92b50235facfbc08dfe7f334827d47281471333b
/library/data/empty.lean
0ed1583e2f3bbf97479d43bf76c1c06df82d0940
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
1,351
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Floris van Doorn -/ import logic.cast namespace empty protected theorem elim (A : Type) (H : empty) : A := empty.rec (λe, A) H protected theorem subsingleton [instance] : subsingleton empty := subsingleton.intro (λ a b, !empty.elim a) end empty protected definition empty.has_decidable_eq [instance] : decidable_eq empty := take (a b : empty), decidable.inl (!empty.elim a) definition tneg.tneg (A : Type) := A → empty prefix `~` := tneg.tneg namespace tneg variables {A B : Type} protected definition intro (H : A → empty) : ~A := H protected definition elim (H1 : ~A) (H2 : A) : empty := H1 H2 protected definition empty : ~empty := λH : empty, H definition tabsurd (H1 : A) (H2 : ~A) : B := !empty.elim (H2 H1) definition tneg_tneg_intro (H : A) : ~~A := λH2 : ~A, tneg.elim H2 H definition tmt (H1 : A → B) (H2 : ~B) : ~A := λHA : A, tabsurd (H1 HA) H2 definition tneg_pi_left {B : A → Type} (H : ~Πa, B a) : ~~A := λHnA : ~A, tneg.elim H (λHA : A, tabsurd HA HnA) definition tneg_function_right (H : ~(A → B)) : ~B := λHB : B, tneg.elim H (λHA : A, HB) end tneg
7f132124226bfd23b84298437774f5324e7da1e4
42610cc2e5db9c90269470365e6056df0122eaa0
/hott/homotopy/interval.hlean
c25e103f3e2ff37173f9e4753c8848cc12a4a661
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
3,386
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Declaration of the interval -/ import .susp types.eq types.prod cubical.square open eq susp unit equiv is_trunc nat prod definition interval : Type₀ := susp unit namespace interval definition zero : interval := north definition one : interval := south definition seg : zero = one := merid star protected definition rec {P : interval → Type} (P0 : P zero) (P1 : P one) (Ps : P0 =[seg] P1) (x : interval) : P x := begin fapply susp.rec_on x, { exact P0}, { exact P1}, { intro x, cases x, exact Ps} end protected definition rec_on [reducible] {P : interval → Type} (x : interval) (P0 : P zero) (P1 : P one) (Ps : P0 =[seg] P1) : P x := interval.rec P0 P1 Ps x theorem rec_seg {P : interval → Type} (P0 : P zero) (P1 : P one) (Ps : P0 =[seg] P1) : apd (interval.rec P0 P1 Ps) seg = Ps := !rec_merid protected definition elim {P : Type} (P0 P1 : P) (Ps : P0 = P1) (x : interval) : P := interval.rec P0 P1 (pathover_of_eq Ps) x protected definition elim_on [reducible] {P : Type} (x : interval) (P0 P1 : P) (Ps : P0 = P1) : P := interval.elim P0 P1 Ps x theorem elim_seg {P : Type} (P0 P1 : P) (Ps : P0 = P1) : ap (interval.elim P0 P1 Ps) seg = Ps := begin apply eq_of_fn_eq_fn_inv !(pathover_constant seg), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑interval.elim,rec_seg], end protected definition elim_type (P0 P1 : Type) (Ps : P0 ≃ P1) (x : interval) : Type := interval.elim P0 P1 (ua Ps) x protected definition elim_type_on [reducible] (x : interval) (P0 P1 : Type) (Ps : P0 ≃ P1) : Type := interval.elim_type P0 P1 Ps x theorem elim_type_seg (P0 P1 : Type) (Ps : P0 ≃ P1) : transport (interval.elim_type P0 P1 Ps) seg = Ps := by rewrite [tr_eq_cast_ap_fn,↑interval.elim_type,elim_seg];apply cast_ua_fn definition is_contr_interval [instance] [priority 900] : is_contr interval := is_contr.mk zero (λx, interval.rec_on x idp seg !pathover_eq_r_idp) open is_equiv equiv definition naive_funext_of_interval : naive_funext := λA P f g p, ap (λ(i : interval) (x : A), interval.elim_on i (f x) (g x) (p x)) seg end interval open interval definition cube : ℕ → Type₀ | cube 0 := unit | cube (succ n) := cube n × interval abbreviation square := cube (succ (succ nat.zero)) definition cube_one_equiv_interval : cube 1 ≃ interval := !prod_comm_equiv ⬝e !prod_unit_equiv definition prod_square {A B : Type} {a a' : A} {b b' : B} (p : a = a') (q : b = b') : square (pair_eq p idp) (pair_eq p idp) (pair_eq idp q) (pair_eq idp q) := by cases p; cases q; exact ids namespace square definition tl : square := (star, zero, zero) definition tr : square := (star, one, zero) definition bl : square := (star, zero, one ) definition br : square := (star, one, one ) -- s stands for "square" in the following definitions definition st : tl = tr := pair_eq (pair_eq idp seg) idp definition sb : bl = br := pair_eq (pair_eq idp seg) idp definition sl : tl = bl := pair_eq idp seg definition sr : tr = br := pair_eq idp seg definition sfill : square st sb sl sr := !prod_square definition fill : st ⬝ sr = sl ⬝ sb := !square_equiv_eq sfill end square
75e9c60c2f54f9dd9025696be94a207bcd619f59
367134ba5a65885e863bdc4507601606690974c1
/src/analysis/special_functions/integrals.lean
c5d6bff7f2733552d41fee39d36f28dec5157ce7
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
3,594
lean
/- Copyright (c) 2021 Benjamin Davidson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Benjamin Davidson -/ import measure_theory.interval_integral /-! # Integration of specific interval integrals This file contains proofs of the integrals of various simple functions, including `pow`, `exp`, `inv`/`one_div`, `sin`, `cos`, and `λ x, 1 / (1 + x^2)`. With these lemmas, many simple integrals can be computed by `simp` or `norm_num`. Scroll to the bottom of the file for examples. This file is incomplete; we are working on expanding it. -/ open real set interval_integral variables {a b : ℝ} namespace interval_integral variable {f : ℝ → ℝ} @[simp] lemma integral_const_mul (c : ℝ) : ∫ x in a..b, c * f x = c * ∫ x in a..b, f x := integral_smul c @[simp] lemma integral_mul_const (c : ℝ) : ∫ x in a..b, f x * c = (∫ x in a..b, f x) * c := by simp only [mul_comm, integral_const_mul] @[simp] lemma integral_div (c : ℝ) : ∫ x in a..b, f x / c = (∫ x in a..b, f x) / c := integral_mul_const c⁻¹ end interval_integral @[simp] lemma integral_pow (n : ℕ) : ∫ x in a..b, x ^ n = (b^(n+1) - a^(n+1)) / (n + 1) := begin have hderiv : deriv (λ x : ℝ, x^(n + 1) / (n + 1)) = λ x, x ^ n, { have hne : (n + 1 : ℝ) ≠ 0 := by exact_mod_cast nat.succ_ne_zero n, ext, simp [mul_div_assoc, mul_div_cancel' _ hne] }, rw integral_deriv_eq_sub' _ hderiv; norm_num [div_sub_div_same, (continuous_pow n).continuous_on], end @[simp] lemma integral_id : ∫ x in a..b, x = (b^2 - a^2) / 2 := by simpa using integral_pow 1 @[simp] lemma integral_one : ∫ x in a..b, (1:ℝ) = b - a := by simp @[simp] lemma integral_exp : ∫ x in a..b, exp x = exp b - exp a := by rw integral_deriv_eq_sub'; norm_num [continuous_exp.continuous_on] @[simp] lemma integral_inv (h : (0:ℝ) ∉ interval a b) : ∫ x in a..b, x⁻¹ = log (b / a) := begin have h' := λ x hx, ne_of_mem_of_not_mem hx h, rw [integral_deriv_eq_sub' _ deriv_log' (λ x hx, differentiable_at_log (h' x hx)) (continuous_on_inv'.mono (subset_compl_singleton_iff.mpr h)), log_div (h' b right_mem_interval) (h' a left_mem_interval)], end @[simp] lemma integral_inv_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x in a..b, x⁻¹ = log (b / a) := integral_inv $ not_mem_interval_of_lt ha hb @[simp] lemma integral_inv_of_neg (ha : a < 0) (hb : b < 0) : ∫ x in a..b, x⁻¹ = log (b / a) := integral_inv $ not_mem_interval_of_gt ha hb lemma integral_one_div (h : (0:ℝ) ∉ interval a b) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv h] lemma integral_one_div_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv_of_pos ha hb] lemma integral_one_div_of_neg (ha : a < 0) (hb : b < 0) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv_of_neg ha hb] @[simp] lemma integral_sin : ∫ x in a..b, sin x = cos a - cos b := by rw integral_deriv_eq_sub' (λ x, -cos x); norm_num [continuous_on_sin] @[simp] lemma integral_cos : ∫ x in a..b, cos x = sin b - sin a := by rw integral_deriv_eq_sub'; norm_num [continuous_on_cos] @[simp] lemma integral_inv_one_add_sq : ∫ x : ℝ in a..b, (1 + x^2)⁻¹ = arctan b - arctan a := begin simp only [← one_div], refine integral_deriv_eq_sub' _ _ _ (continuous_const.div _ (λ x, _)).continuous_on; norm_num, { continuity }, { nlinarith }, end lemma integral_one_div_one_add_sq : ∫ x : ℝ in a..b, 1 / (1 + x^2) = arctan b - arctan a := by simp
cc4600c63423f102d4afd2f1b0f2d0ce923e4b99
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/abstract_ns2.lean
912f590e906ec3b99b559671805ec935fbe59fbf
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
170
lean
-- Companion file for the abstract_ns test. namespace ns2 def foo : fin 7 := ⟨3, dec_trivial⟩ def foo' : fin 7 := ⟨3, by abstract {exact dec_trivial}⟩ end ns2
a857128b5a0753f21e506412d4dbbad987971c9a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/set_theory/zfc/basic.lean
2c38bbbcc4d590693f935e7f9881675e49c20a0c
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
42,504
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.set.lattice import logic.small.basic import order.well_founded /-! # A model of ZFC In this file, we model Zermelo-Fraenkel set theory (+ Choice) using Lean's underlying type theory. We do this in four main steps: * Define pre-sets inductively. * Define extensional equivalence on pre-sets and give it a `setoid` instance. * Define ZFC sets by quotienting pre-sets by extensional equivalence. * Define classes as sets of ZFC sets. Then the rest is usual set theory. ## The model * `pSet`: Pre-set. A pre-set is inductively defined by its indexing type and its members, which are themselves pre-sets. * `Set`: ZFC set. Defined as `pSet` quotiented by `pSet.equiv`, the extensional equivalence. * `Class`: Class. Defined as `set Set`. * `Set.choice`: Axiom of choice. Proved from Lean's axiom of choice. ## Other definitions * `arity α n`: `n`-ary function `α → α → ... → α`. Defined inductively. * `arity.const a n`: `n`-ary constant function equal to `a`. * `pSet.type`: Underlying type of a pre-set. * `pSet.func`: Underlying family of pre-sets of a pre-set. * `pSet.equiv`: Extensional equivalence of pre-sets. Defined inductively. * `pSet.omega`, `Set.omega`: The von Neumann ordinal `ω` as a `pSet`, as a `Set`. * `pSet.arity.equiv`: Extensional equivalence of `n`-ary `pSet`-valued functions. Extension of `pSet.equiv`. * `pSet.resp`: Collection of `n`-ary `pSet`-valued functions that respect extensional equivalence. * `pSet.eval`: Turns a `pSet`-valued function that respect extensional equivalence into a `Set`-valued function. * `classical.all_definable`: All functions are classically definable. * `Set.is_func` : Predicate that a ZFC set is a subset of `x × y` that can be considered as a ZFC function `x → y`. That is, each member of `x` is related by the ZFC set to exactly one member of `y`. * `Set.funs`: ZFC set of ZFC functions `x → y`. * `Class.iota`: Definite description operator. ## Notes To avoid confusion between the Lean `set` and the ZFC `Set`, docstrings in this file refer to them respectively as "`set`" and "ZFC set". ## TODO Prove `Set.map_definable_aux` computably. -/ universes u v /-- The type of `n`-ary functions `α → α → ... → α`. -/ def arity (α : Type u) : ℕ → Type u | 0 := α | (n+1) := α → arity n @[simp] theorem arity_zero (α : Type u) : arity α 0 = α := rfl @[simp] theorem arity_succ (α : Type u) (n : ℕ) : arity α n.succ = (α → arity α n) := rfl namespace arity /-- Constant `n`-ary function with value `a`. -/ def const {α : Type u} (a : α) : ∀ n, arity α n | 0 := a | (n+1) := λ _, const n @[simp] theorem const_zero {α : Type u} (a : α) : const a 0 = a := rfl @[simp] theorem const_succ {α : Type u} (a : α) (n : ℕ) : const a n.succ = λ _, const a n := rfl theorem const_succ_apply {α : Type u} (a : α) (n : ℕ) (x : α) : const a n.succ x = const a n := rfl instance arity.inhabited {α n} [inhabited α] : inhabited (arity α n) := ⟨const default _⟩ end arity /-- The type of pre-sets in universe `u`. A pre-set is a family of pre-sets indexed by a type in `Type u`. The ZFC universe is defined as a quotient of this to ensure extensionality. -/ inductive pSet : Type (u+1) | mk (α : Type u) (A : α → pSet) : pSet namespace pSet /-- The underlying type of a pre-set -/ def type : pSet → Type u | ⟨α, A⟩ := α /-- The underlying pre-set family of a pre-set -/ def func : Π (x : pSet), x.type → pSet | ⟨α, A⟩ := A @[simp] theorem mk_type (α A) : type ⟨α, A⟩ = α := rfl @[simp] theorem mk_func (α A) : func ⟨α, A⟩ = A := rfl @[simp] theorem eta : Π (x : pSet), mk x.type x.func = x | ⟨α, A⟩ := rfl /-- Two pre-sets are extensionally equivalent if every element of the first family is extensionally equivalent to some element of the second family and vice-versa. -/ def equiv (x y : pSet) : Prop := pSet.rec (λ α z m ⟨β, B⟩, (∀ a, ∃ b, m a (B b)) ∧ (∀ b, ∃ a, m a (B b))) x y theorem equiv_iff : Π {x y : pSet}, equiv x y ↔ (∀ i, ∃ j, equiv (x.func i) (y.func j)) ∧ (∀ j, ∃ i, equiv (x.func i) (y.func j)) | ⟨α, A⟩ ⟨β, B⟩ := iff.rfl theorem equiv.exists_left {x y : pSet} (h : equiv x y) : ∀ i, ∃ j, equiv (x.func i) (y.func j) := (equiv_iff.1 h).1 theorem equiv.exists_right {x y : pSet} (h : equiv x y) : ∀ j, ∃ i, equiv (x.func i) (y.func j) := (equiv_iff.1 h).2 @[refl] protected theorem equiv.refl (x) : equiv x x := pSet.rec_on x $ λ α A IH, ⟨λ a, ⟨a, IH a⟩, λ a, ⟨a, IH a⟩⟩ protected theorem equiv.rfl : ∀ {x}, equiv x x := equiv.refl protected theorem equiv.euc {x} : Π {y z}, equiv x y → equiv z y → equiv x z := pSet.rec_on x $ λ α A IH y, pSet.cases_on y $ λ β B ⟨γ, Γ⟩ ⟨αβ, βα⟩ ⟨γβ, βγ⟩, ⟨λ a, let ⟨b, ab⟩ := αβ a, ⟨c, bc⟩ := βγ b in ⟨c, IH a ab bc⟩, λ c, let ⟨b, cb⟩ := γβ c, ⟨a, ba⟩ := βα b in ⟨a, IH a ba cb⟩⟩ @[symm] protected theorem equiv.symm {x y} : equiv x y → equiv y x := (equiv.refl y).euc @[trans] protected theorem equiv.trans {x y z} (h1 : equiv x y) (h2 : equiv y z) : equiv x z := h1.euc h2.symm protected theorem equiv_of_is_empty (x y : pSet) [is_empty x.type] [is_empty y.type] : equiv x y := equiv_iff.2 $ by simp instance setoid : setoid pSet := ⟨pSet.equiv, equiv.refl, λ x y, equiv.symm, λ x y z, equiv.trans⟩ /-- A pre-set is a subset of another pre-set if every element of the first family is extensionally equivalent to some element of the second family.-/ protected def subset (x y : pSet) : Prop := ∀ a, ∃ b, equiv (x.func a) (y.func b) instance : has_subset pSet := ⟨pSet.subset⟩ instance : is_refl pSet (⊆) := ⟨λ x a, ⟨a, equiv.refl _⟩⟩ instance : is_trans pSet (⊆) := ⟨λ x y z hxy hyz a, begin cases hxy a with b hb, cases hyz b with c hc, exact ⟨c, hb.trans hc⟩ end⟩ theorem equiv.ext : Π (x y : pSet), equiv x y ↔ (x ⊆ y ∧ y ⊆ x) | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ ⟨αβ, βα⟩, ⟨αβ, λ b, let ⟨a, h⟩ := βα b in ⟨a, equiv.symm h⟩⟩, λ ⟨αβ, βα⟩, ⟨αβ, λ b, let ⟨a, h⟩ := βα b in ⟨a, equiv.symm h⟩⟩⟩ theorem subset.congr_left : Π {x y z : pSet}, equiv x y → (x ⊆ z ↔ y ⊆ z) | ⟨α, A⟩ ⟨β, B⟩ ⟨γ, Γ⟩ ⟨αβ, βα⟩ := ⟨λ αγ b, let ⟨a, ba⟩ := βα b, ⟨c, ac⟩ := αγ a in ⟨c, (equiv.symm ba).trans ac⟩, λ βγ a, let ⟨b, ab⟩ := αβ a, ⟨c, bc⟩ := βγ b in ⟨c, equiv.trans ab bc⟩⟩ theorem subset.congr_right : Π {x y z : pSet}, equiv x y → (z ⊆ x ↔ z ⊆ y) | ⟨α, A⟩ ⟨β, B⟩ ⟨γ, Γ⟩ ⟨αβ, βα⟩ := ⟨λ γα c, let ⟨a, ca⟩ := γα c, ⟨b, ab⟩ := αβ a in ⟨b, ca.trans ab⟩, λ γβ c, let ⟨b, cb⟩ := γβ c, ⟨a, ab⟩ := βα b in ⟨a, cb.trans (equiv.symm ab)⟩⟩ /-- `x ∈ y` as pre-sets if `x` is extensionally equivalent to a member of the family `y`. -/ protected def mem (x y : pSet.{u}) : Prop := ∃ b, equiv x (y.func b) instance : has_mem pSet pSet := ⟨pSet.mem⟩ theorem mem.mk {α : Type u} (A : α → pSet) (a : α) : A a ∈ mk α A := ⟨a, equiv.refl (A a)⟩ theorem func_mem (x : pSet) (i : x.type) : x.func i ∈ x := by { cases x, apply mem.mk } theorem mem.ext : Π {x y : pSet.{u}}, (∀ w : pSet.{u}, w ∈ x ↔ w ∈ y) → equiv x y | ⟨α, A⟩ ⟨β, B⟩ h := ⟨λ a, (h (A a)).1 (mem.mk A a), λ b, let ⟨a, ha⟩ := (h (B b)).2 (mem.mk B b) in ⟨a, ha.symm⟩⟩ theorem mem.congr_right : Π {x y : pSet.{u}}, equiv x y → (∀ {w : pSet.{u}}, w ∈ x ↔ w ∈ y) | ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩ w := ⟨λ ⟨a, ha⟩, let ⟨b, hb⟩ := αβ a in ⟨b, ha.trans hb⟩, λ ⟨b, hb⟩, let ⟨a, ha⟩ := βα b in ⟨a, hb.euc ha⟩⟩ theorem equiv_iff_mem {x y : pSet.{u}} : equiv x y ↔ (∀ {w : pSet.{u}}, w ∈ x ↔ w ∈ y) := ⟨mem.congr_right, match x, y with | ⟨α, A⟩, ⟨β, B⟩, h := ⟨λ a, h.1 (mem.mk A a), λ b, let ⟨a, h⟩ := h.2 (mem.mk B b) in ⟨a, h.symm⟩⟩ end⟩ theorem mem.congr_left : Π {x y : pSet.{u}}, equiv x y → (∀ {w : pSet.{u}}, x ∈ w ↔ y ∈ w) | x y h ⟨α, A⟩ := ⟨λ ⟨a, ha⟩, ⟨a, h.symm.trans ha⟩, λ ⟨a, ha⟩, ⟨a, h.trans ha⟩⟩ private theorem mem_wf_aux : Π {x y : pSet.{u}}, equiv x y → acc (∈) y | ⟨α, A⟩ ⟨β, B⟩ H := ⟨_, begin rintros ⟨γ, C⟩ ⟨b, hc⟩, cases H.exists_right b with a ha, have H := ha.trans hc.symm, rw mk_func at H, exact mem_wf_aux H end⟩ theorem mem_wf : @well_founded pSet (∈) := ⟨λ x, mem_wf_aux $ equiv.refl x⟩ instance : has_well_founded pSet := ⟨_, mem_wf⟩ instance : is_asymm pSet (∈) := mem_wf.is_asymm theorem mem_asymm {x y : pSet} : x ∈ y → y ∉ x := asymm theorem mem_irrefl (x : pSet) : x ∉ x := irrefl x /-- Convert a pre-set to a `set` of pre-sets. -/ def to_set (u : pSet.{u}) : set pSet.{u} := {x | x ∈ u} @[simp] theorem mem_to_set (a u : pSet.{u}) : a ∈ u.to_set ↔ a ∈ u := iff.rfl /-- A nonempty set is one that contains some element. -/ protected def nonempty (u : pSet) : Prop := u.to_set.nonempty theorem nonempty_def (u : pSet) : u.nonempty ↔ ∃ x, x ∈ u := iff.rfl theorem nonempty_of_mem {x u : pSet} (h : x ∈ u) : u.nonempty := ⟨x, h⟩ @[simp] theorem nonempty_to_set_iff {u : pSet} : u.to_set.nonempty ↔ u.nonempty := iff.rfl theorem nonempty_type_iff_nonempty {x : pSet} : nonempty x.type ↔ pSet.nonempty x := ⟨λ ⟨i⟩, ⟨_, func_mem _ i⟩, λ ⟨i, j, h⟩, ⟨j⟩⟩ theorem nonempty_of_nonempty_type (x : pSet) [h : nonempty x.type] : pSet.nonempty x := nonempty_type_iff_nonempty.1 h /-- Two pre-sets are equivalent iff they have the same members. -/ theorem equiv.eq {x y : pSet} : equiv x y ↔ to_set x = to_set y := equiv_iff_mem.trans set.ext_iff.symm instance : has_coe pSet (set pSet) := ⟨to_set⟩ /-- The empty pre-set -/ protected def empty : pSet := ⟨_, pempty.elim⟩ instance : has_emptyc pSet := ⟨pSet.empty⟩ instance : inhabited pSet := ⟨∅⟩ instance : is_empty (type (∅)) := pempty.is_empty @[simp] theorem mem_empty (x : pSet.{u}) : x ∉ (∅ : pSet.{u}) := is_empty.exists_iff.1 @[simp] theorem to_set_empty : to_set ∅ = ∅ := by simp [to_set] @[simp] theorem empty_subset (x : pSet.{u}) : (∅ : pSet) ⊆ x := λ x, x.elim @[simp] theorem not_nonempty_empty : ¬ pSet.nonempty ∅ := by simp [pSet.nonempty] protected theorem equiv_empty (x : pSet) [is_empty x.type] : equiv x ∅ := pSet.equiv_of_is_empty x _ /-- Insert an element into a pre-set -/ protected def insert (x y : pSet) : pSet := ⟨option y.type, λ o, option.rec x y.func o⟩ instance : has_insert pSet pSet := ⟨pSet.insert⟩ instance : has_singleton pSet pSet := ⟨λ s, insert s ∅⟩ instance : is_lawful_singleton pSet pSet := ⟨λ _, rfl⟩ instance (x y : pSet) : inhabited (insert x y).type := option.inhabited _ /-- The n-th von Neumann ordinal -/ def of_nat : ℕ → pSet | 0 := ∅ | (n+1) := insert (of_nat n) (of_nat n) /-- The von Neumann ordinal ω -/ def omega : pSet := ⟨ulift ℕ, λ n, of_nat n.down⟩ /-- The pre-set separation operation `{x ∈ a | p x}` -/ protected def sep (p : pSet → Prop) (x : pSet) : pSet := ⟨{a // p (x.func a)}, λ y, x.func y.1⟩ instance : has_sep pSet pSet := ⟨pSet.sep⟩ /-- The pre-set powerset operator -/ def powerset (x : pSet) : pSet := ⟨set x.type, λ p, ⟨{a // p a}, λ y, x.func y.1⟩⟩ @[simp] theorem mem_powerset : Π {x y : pSet}, y ∈ powerset x ↔ y ⊆ x | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ ⟨p, e⟩, (subset.congr_left e).2 $ λ ⟨a, pa⟩, ⟨a, equiv.refl (A a)⟩, λ βα, ⟨{a | ∃ b, equiv (B b) (A a)}, λ b, let ⟨a, ba⟩ := βα b in ⟨⟨a, b, ba⟩, ba⟩, λ ⟨a, b, ba⟩, ⟨b, ba⟩⟩⟩ /-- The pre-set union operator -/ def sUnion (a : pSet) : pSet := ⟨Σ x, (a.func x).type, λ ⟨x, y⟩, (a.func x).func y⟩ prefix (name := pSet.sUnion) `⋃₀ `:110 := pSet.sUnion @[simp] theorem mem_sUnion : Π {x y : pSet.{u}}, y ∈ ⋃₀ x ↔ ∃ z ∈ x, y ∈ z | ⟨α, A⟩ y := ⟨λ ⟨⟨a, c⟩, (e : equiv y ((A a).func c))⟩, have func (A a) c ∈ mk (A a).type (A a).func, from mem.mk (A a).func c, ⟨_, mem.mk _ _, (mem.congr_left e).2 (by rwa eta at this)⟩, λ ⟨⟨β, B⟩, ⟨a, (e : equiv (mk β B) (A a))⟩, ⟨b, yb⟩⟩, by { rw ←(eta (A a)) at e, exact let ⟨βt, tβ⟩ := e, ⟨c, bc⟩ := βt b in ⟨⟨a, c⟩, yb.trans bc⟩ }⟩ @[simp] theorem to_set_sUnion (x : pSet.{u}) : (⋃₀ x).to_set = ⋃₀ (to_set '' x.to_set) := by { ext, simp } /-- The image of a function from pre-sets to pre-sets. -/ def image (f : pSet.{u} → pSet.{u}) (x : pSet.{u}) : pSet := ⟨x.type, f ∘ x.func⟩ theorem mem_image {f : pSet.{u} → pSet.{u}} (H : ∀ {x y}, equiv x y → equiv (f x) (f y)) : Π {x y : pSet.{u}}, y ∈ image f x ↔ ∃ z ∈ x, equiv y (f z) | ⟨α, A⟩ y := ⟨λ ⟨a, ya⟩, ⟨A a, mem.mk A a, ya⟩, λ ⟨z, ⟨a, za⟩, yz⟩, ⟨a, yz.trans (H za)⟩⟩ /-- Universe lift operation -/ protected def lift : pSet.{u} → pSet.{max u v} | ⟨α, A⟩ := ⟨ulift α, λ ⟨x⟩, lift (A x)⟩ /-- Embedding of one universe in another -/ @[nolint check_univs] -- intended to be used with explicit universe parameters def embed : pSet.{max (u+1) v} := ⟨ulift.{v u+1} pSet, λ ⟨x⟩, pSet.lift.{u (max (u+1) v)} x⟩ theorem lift_mem_embed : Π (x : pSet.{u}), pSet.lift.{u (max (u+1) v)} x ∈ embed.{u v} := λ x, ⟨⟨x⟩, equiv.rfl⟩ /-- Function equivalence is defined so that `f ~ g` iff `∀ x y, x ~ y → f x ~ g y`. This extends to equivalence of `n`-ary functions. -/ def arity.equiv : Π {n}, arity pSet.{u} n → arity pSet.{u} n → Prop | 0 a b := equiv a b | (n+1) a b := ∀ x y, equiv x y → arity.equiv (a x) (b y) lemma arity.equiv_const {a : pSet.{u}} : ∀ n, arity.equiv (arity.const a n) (arity.const a n) | 0 := equiv.rfl | (n+1) := λ x y h, arity.equiv_const _ /-- `resp n` is the collection of n-ary functions on `pSet` that respect equivalence, i.e. when the inputs are equivalent the output is as well. -/ def resp (n) := {x : arity pSet.{u} n // arity.equiv x x} instance resp.inhabited {n} : inhabited (resp n) := ⟨⟨arity.const default _, arity.equiv_const _⟩⟩ /-- The `n`-ary image of a `(n + 1)`-ary function respecting equivalence as a function respecting equivalence. -/ def resp.f {n} (f : resp (n+1)) (x : pSet) : resp n := ⟨f.1 x, f.2 _ _ $ equiv.refl x⟩ /-- Function equivalence for functions respecting equivalence. See `pSet.arity.equiv`. -/ def resp.equiv {n} (a b : resp n) : Prop := arity.equiv a.1 b.1 protected theorem resp.equiv.refl {n} (a : resp n) : resp.equiv a a := a.2 protected theorem resp.equiv.euc : Π {n} {a b c : resp n}, resp.equiv a b → resp.equiv c b → resp.equiv a c | 0 a b c hab hcb := equiv.euc hab hcb | (n+1) a b c hab hcb := λ x y h, @resp.equiv.euc n (a.f x) (b.f y) (c.f y) (hab _ _ h) (hcb _ _ $ equiv.refl y) protected theorem resp.equiv.symm {n} {a b : resp n} : resp.equiv a b → resp.equiv b a := (resp.equiv.refl b).euc protected theorem resp.equiv.trans {n} {x y z : resp n} (h1 : resp.equiv x y) (h2 : resp.equiv y z) : resp.equiv x z := h1.euc h2.symm instance resp.setoid {n} : setoid (resp n) := ⟨resp.equiv, resp.equiv.refl, λ x y, resp.equiv.symm, λ x y z, resp.equiv.trans⟩ end pSet /-- The ZFC universe of sets consists of the type of pre-sets, quotiented by extensional equivalence. -/ def Set : Type (u+1) := quotient pSet.setoid.{u} namespace pSet namespace resp /-- Helper function for `pSet.eval`. -/ def eval_aux : Π {n}, {f : resp n → arity Set.{u} n // ∀ (a b : resp n), resp.equiv a b → f a = f b} | 0 := ⟨λ a, ⟦a.1⟧, λ a b h, quotient.sound h⟩ | (n+1) := let F : resp (n + 1) → arity Set (n + 1) := λ a, @quotient.lift _ _ pSet.setoid (λ x, eval_aux.1 (a.f x)) (λ b c h, eval_aux.2 _ _ (a.2 _ _ h)) in ⟨F, λ b c h, funext $ @quotient.ind _ _ (λ q, F b q = F c q) $ λ z, eval_aux.2 (resp.f b z) (resp.f c z) (h _ _ (pSet.equiv.refl z))⟩ /-- An equivalence-respecting function yields an n-ary ZFC set function. -/ def eval (n) : resp n → arity Set.{u} n := eval_aux.1 theorem eval_val {n f x} : (@eval (n+1) f : Set → arity Set n) ⟦x⟧ = eval n (resp.f f x) := rfl end resp /-- A set function is "definable" if it is the image of some n-ary pre-set function. This isn't exactly definability, but is useful as a sufficient condition for functions that have a computable image. -/ class inductive definable (n) : arity Set.{u} n → Type (u+1) | mk (f) : definable (resp.eval n f) attribute [instance] definable.mk /-- The evaluation of a function respecting equivalence is definable, by that same function. -/ def definable.eq_mk {n} (f) : Π {s : arity Set.{u} n} (H : resp.eval _ f = s), definable n s | ._ rfl := ⟨f⟩ /-- Turns a definable function into a function that respects equivalence. -/ def definable.resp {n} : Π (s : arity Set.{u} n) [definable n s], resp n | ._ ⟨f⟩ := f theorem definable.eq {n} : Π (s : arity Set.{u} n) [H : definable n s], (@definable.resp n s H).eval _ = s | ._ ⟨f⟩ := rfl end pSet namespace classical open pSet /-- All functions are classically definable. -/ noncomputable def all_definable : Π {n} (F : arity Set.{u} n), definable n F | 0 F := let p := @quotient.exists_rep pSet _ F in definable.eq_mk ⟨some p, equiv.rfl⟩ (some_spec p) | (n+1) (F : arity Set.{u} (n + 1)) := begin have I := λ x, (all_definable (F x)), refine definable.eq_mk ⟨λ x : pSet, (@definable.resp _ _ (I ⟦x⟧)).1, _⟩ _, { dsimp [arity.equiv], introsI x y h, rw @quotient.sound pSet _ _ _ h, exact (definable.resp (F ⟦y⟧)).2 }, refine funext (λ q, quotient.induction_on q $ λ x, _), simp_rw [resp.eval_val, resp.f, subtype.val_eq_coe, subtype.coe_eta], exact @definable.eq _ (F ⟦x⟧) (I ⟦x⟧), end end classical namespace Set open pSet /-- Turns a pre-set into a ZFC set. -/ def mk : pSet → Set := quotient.mk @[simp] theorem mk_eq (x : pSet) : @eq Set ⟦x⟧ (mk x) := rfl @[simp] theorem mk_out : ∀ x : Set, mk x.out = x := quotient.out_eq theorem eq {x y : pSet} : mk x = mk y ↔ equiv x y := quotient.eq theorem sound {x y : pSet} (h : pSet.equiv x y) : mk x = mk y := quotient.sound h theorem exact {x y : pSet} : mk x = mk y → pSet.equiv x y := quotient.exact @[simp] lemma eval_mk {n f x} : (@resp.eval (n+1) f : Set → arity Set n) (mk x) = resp.eval n (resp.f f x) := rfl /-- The membership relation for ZFC sets is inherited from the membership relation for pre-sets. -/ protected def mem : Set → Set → Prop := quotient.lift₂ pSet.mem (λ x y x' y' hx hy, propext ((mem.congr_left hx).trans (mem.congr_right hy))) instance : has_mem Set Set := ⟨Set.mem⟩ @[simp] theorem mk_mem_iff {x y : pSet} : mk x ∈ mk y ↔ x ∈ y := iff.rfl /-- Convert a ZFC set into a `set` of ZFC sets -/ def to_set (u : Set.{u}) : set Set.{u} := {x | x ∈ u} @[simp] theorem mem_to_set (a u : Set.{u}) : a ∈ u.to_set ↔ a ∈ u := iff.rfl instance small_to_set (x : Set.{u}) : small.{u} x.to_set := quotient.induction_on x $ λ a, begin let f : a.type → (mk a).to_set := λ i, ⟨mk $ a.func i, func_mem a i⟩, suffices : function.surjective f, { exact small_of_surjective this }, rintro ⟨y, hb⟩, induction y using quotient.induction_on, cases hb with i h, exact ⟨i, subtype.coe_injective (quotient.sound h.symm)⟩ end /-- A nonempty set is one that contains some element. -/ protected def nonempty (u : Set) : Prop := u.to_set.nonempty theorem nonempty_def (u : Set) : u.nonempty ↔ ∃ x, x ∈ u := iff.rfl theorem nonempty_of_mem {x u : Set} (h : x ∈ u) : u.nonempty := ⟨x, h⟩ @[simp] theorem nonempty_to_set_iff {u : Set} : u.to_set.nonempty ↔ u.nonempty := iff.rfl /-- `x ⊆ y` as ZFC sets means that all members of `x` are members of `y`. -/ protected def subset (x y : Set.{u}) := ∀ ⦃z⦄, z ∈ x → z ∈ y instance has_subset : has_subset Set := ⟨Set.subset⟩ lemma subset_def {x y : Set.{u}} : x ⊆ y ↔ ∀ ⦃z⦄, z ∈ x → z ∈ y := iff.rfl instance : is_refl Set (⊆) := ⟨λ x a, id⟩ instance : is_trans Set (⊆) := ⟨λ x y z hxy hyz a ha, hyz (hxy ha)⟩ @[simp] theorem subset_iff : Π {x y : pSet}, mk x ⊆ mk y ↔ x ⊆ y | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ h a, @h ⟦A a⟧ (mem.mk A a), λ h z, quotient.induction_on z (λ z ⟨a, za⟩, let ⟨b, ab⟩ := h a in ⟨b, za.trans ab⟩)⟩ @[simp] theorem to_set_subset_iff {x y : Set} : x.to_set ⊆ y.to_set ↔ x ⊆ y := by simp [subset_def, set.subset_def] @[ext] theorem ext {x y : Set.{u}} : (∀ z : Set.{u}, z ∈ x ↔ z ∈ y) → x = y := quotient.induction_on₂ x y (λ u v h, quotient.sound (mem.ext (λ w, h ⟦w⟧))) theorem ext_iff {x y : Set.{u}} : x = y ↔ (∀ z : Set.{u}, z ∈ x ↔ z ∈ y) := ⟨λ h, by simp [h], ext⟩ theorem to_set_injective : function.injective to_set := λ x y h, ext $ set.ext_iff.1 h @[simp] theorem to_set_inj {x y : Set} : x.to_set = y.to_set ↔ x = y := to_set_injective.eq_iff instance : is_antisymm Set (⊆) := ⟨λ a b hab hba, ext $ λ c, ⟨@hab c, @hba c⟩⟩ /-- The empty ZFC set -/ protected def empty : Set := mk ∅ instance : has_emptyc Set := ⟨Set.empty⟩ instance : inhabited Set := ⟨∅⟩ @[simp] theorem mem_empty (x) : x ∉ (∅ : Set.{u}) := quotient.induction_on x pSet.mem_empty @[simp] theorem to_set_empty : to_set ∅ = ∅ := by simp [to_set] @[simp] theorem empty_subset (x : Set.{u}) : (∅ : Set) ⊆ x := quotient.induction_on x $ λ y, subset_iff.2 $ pSet.empty_subset y @[simp] theorem not_nonempty_empty : ¬ Set.nonempty ∅ := by simp [Set.nonempty] @[simp] theorem nonempty_mk_iff {x : pSet} : (mk x).nonempty ↔ x.nonempty := begin refine ⟨_, λ ⟨a, h⟩, ⟨mk a, h⟩⟩, rintro ⟨a, h⟩, induction a using quotient.induction_on, exact ⟨a, h⟩ end theorem eq_empty (x : Set.{u}) : x = ∅ ↔ ∀ y : Set.{u}, y ∉ x := ⟨λ h y, (h.symm ▸ mem_empty y), λ h, ext (λ y, ⟨λ yx, absurd yx (h y), λ y0, absurd y0 (mem_empty _)⟩)⟩ /-- `insert x y` is the set `{x} ∪ y` -/ protected def insert : Set → Set → Set := resp.eval 2 ⟨pSet.insert, λ u v uv ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λ o, match o with | some a := let ⟨b, hb⟩ := αβ a in ⟨some b, hb⟩ | none := ⟨none, uv⟩ end, λ o, match o with | some b := let ⟨a, ha⟩ := βα b in ⟨some a, ha⟩ | none := ⟨none, uv⟩ end⟩⟩ instance : has_insert Set Set := ⟨Set.insert⟩ instance : has_singleton Set Set := ⟨λ x, insert x ∅⟩ instance : is_lawful_singleton Set Set := ⟨λ x, rfl⟩ @[simp] theorem mem_insert_iff {x y z : Set.{u}} : x ∈ insert y z ↔ x = y ∨ x ∈ z := quotient.induction_on₃ x y z (λ x y ⟨α, A⟩, show x ∈ pSet.mk (option α) (λ o, option.rec y A o) ↔ mk x = mk y ∨ x ∈ pSet.mk α A, from ⟨λ m, match m with | ⟨some a, ha⟩ := or.inr ⟨a, ha⟩ | ⟨none, h⟩ := or.inl (quotient.sound h) end, λ m, match m with | or.inr ⟨a, ha⟩ := ⟨some a, ha⟩ | or.inl h := ⟨none, quotient.exact h⟩ end⟩) theorem mem_insert (x y : Set) : x ∈ insert x y := mem_insert_iff.2 $ or.inl rfl theorem mem_insert_of_mem {y z : Set} (x) (h : z ∈ y): z ∈ insert x y := mem_insert_iff.2 $ or.inr h @[simp] theorem to_set_insert (x y : Set) : (insert x y).to_set = insert x y.to_set := by { ext, simp } @[simp] theorem mem_singleton {x y : Set.{u}} : x ∈ @singleton Set.{u} Set.{u} _ y ↔ x = y := iff.trans mem_insert_iff ⟨λ o, or.rec (λ h, h) (λ n, absurd n (mem_empty _)) o, or.inl⟩ @[simp] theorem to_set_singleton (x : Set) : ({x} : Set).to_set = {x} := by { ext, simp } @[simp] theorem mem_pair {x y z : Set.{u}} : x ∈ ({y, z} : Set) ↔ x = y ∨ x = z := iff.trans mem_insert_iff $ or_congr iff.rfl mem_singleton /-- `omega` is the first infinite von Neumann ordinal -/ def omega : Set := mk omega @[simp] theorem omega_zero : ∅ ∈ omega := ⟨⟨0⟩, equiv.rfl⟩ @[simp] theorem omega_succ {n} : n ∈ omega.{u} → insert n n ∈ omega.{u} := quotient.induction_on n (λ x ⟨⟨n⟩, h⟩, ⟨⟨n+1⟩, Set.exact $ show insert (mk x) (mk x) = insert (mk $ of_nat n) (mk $ of_nat n), { rw Set.sound h, refl } ⟩) /-- `{x ∈ a | p x}` is the set of elements in `a` satisfying `p` -/ protected def sep (p : Set → Prop) : Set → Set := resp.eval 1 ⟨pSet.sep (λ y, p (mk y)), λ ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λ ⟨a, pa⟩, let ⟨b, hb⟩ := αβ a in ⟨⟨b, by rwa [mk_func, ←Set.sound hb]⟩, hb⟩, λ ⟨b, pb⟩, let ⟨a, ha⟩ := βα b in ⟨⟨a, by rwa [mk_func, Set.sound ha]⟩, ha⟩⟩⟩ instance : has_sep Set Set := ⟨Set.sep⟩ @[simp] theorem mem_sep {p : Set.{u} → Prop} {x y : Set.{u}} : y ∈ {y ∈ x | p y} ↔ y ∈ x ∧ p y := quotient.induction_on₂ x y (λ ⟨α, A⟩ y, ⟨λ ⟨⟨a, pa⟩, h⟩, ⟨⟨a, h⟩, by rwa (@quotient.sound pSet _ _ _ h)⟩, λ ⟨⟨a, h⟩, pa⟩, ⟨⟨a, by { rw mk_func at h, rwa [mk_func, ←Set.sound h] }⟩, h⟩⟩) @[simp] theorem to_set_sep (a : Set) (p : Set → Prop) : {x ∈ a | p x}.to_set = {x ∈ a.to_set | p x} := by { ext, simp } /-- The powerset operation, the collection of subsets of a ZFC set -/ def powerset : Set → Set := resp.eval 1 ⟨powerset, λ ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λ p, ⟨{b | ∃ a, p a ∧ equiv (A a) (B b)}, λ ⟨a, pa⟩, let ⟨b, ab⟩ := αβ a in ⟨⟨b, a, pa, ab⟩, ab⟩, λ ⟨b, a, pa, ab⟩, ⟨⟨a, pa⟩, ab⟩⟩, λ q, ⟨{a | ∃ b, q b ∧ equiv (A a) (B b)}, λ ⟨a, b, qb, ab⟩, ⟨⟨b, qb⟩, ab⟩, λ ⟨b, qb⟩, let ⟨a, ab⟩ := βα b in ⟨⟨a, b, qb, ab⟩, ab⟩⟩⟩⟩ @[simp] theorem mem_powerset {x y : Set.{u}} : y ∈ powerset x ↔ y ⊆ x := quotient.induction_on₂ x y ( λ ⟨α, A⟩ ⟨β, B⟩, show (⟨β, B⟩ : pSet.{u}) ∈ (pSet.powerset.{u} ⟨α, A⟩) ↔ _, by simp [mem_powerset, subset_iff]) theorem sUnion_lem {α β : Type u} (A : α → pSet) (B : β → pSet) (αβ : ∀ a, ∃ b, equiv (A a) (B b)) : ∀ a, ∃ b, (equiv ((sUnion ⟨α, A⟩).func a) ((sUnion ⟨β, B⟩).func b)) | ⟨a, c⟩ := let ⟨b, hb⟩ := αβ a in begin induction ea : A a with γ Γ, induction eb : B b with δ Δ, rw [ea, eb] at hb, cases hb with γδ δγ, exact let c : type (A a) := c, ⟨d, hd⟩ := γδ (by rwa ea at c) in have pSet.equiv ((A a).func c) ((B b).func (eq.rec d (eq.symm eb))), from match A a, B b, ea, eb, c, d, hd with ._, ._, rfl, rfl, x, y, hd := hd end, ⟨⟨b, by { rw mk_func, exact eq.rec d (eq.symm eb) }⟩, this⟩ end /-- The union operator, the collection of elements of elements of a ZFC set -/ def sUnion : Set → Set := resp.eval 1 ⟨pSet.sUnion, λ ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨sUnion_lem A B αβ, λ a, exists.elim (sUnion_lem B A (λ b, exists.elim (βα b) (λ c hc, ⟨c, pSet.equiv.symm hc⟩)) a) (λ b hb, ⟨b, pSet.equiv.symm hb⟩)⟩⟩ prefix (name := Set.sUnion) `⋃₀ `:110 := Set.sUnion @[simp] theorem mem_sUnion {x y : Set.{u}} : y ∈ ⋃₀ x ↔ ∃ z ∈ x, y ∈ z := quotient.induction_on₂ x y (λ x y, iff.trans mem_sUnion ⟨λ ⟨z, h⟩, ⟨⟦z⟧, h⟩, λ ⟨z, h⟩, quotient.induction_on z (λ z h, ⟨z, h⟩) h⟩) theorem mem_sUnion_of_mem {x y z : Set} (hy : y ∈ z) (hz : z ∈ x) : y ∈ ⋃₀ x := mem_sUnion.2 ⟨z, hz, hy⟩ @[simp] theorem sUnion_singleton {x : Set.{u}} : ⋃₀ ({x} : Set) = x := ext $ λ y, by simp_rw [mem_sUnion, exists_prop, mem_singleton, exists_eq_left] theorem singleton_injective : function.injective (@singleton Set Set _) := λ x y H, let this := congr_arg sUnion H in by rwa [sUnion_singleton, sUnion_singleton] at this @[simp] theorem singleton_inj {x y : Set} : ({x} : Set) = {y} ↔ x = y := singleton_injective.eq_iff @[simp] theorem to_set_sUnion (x : Set.{u}) : (⋃₀ x).to_set = ⋃₀ (to_set '' x.to_set) := by { ext, simp } /-- The binary union operation -/ protected def union (x y : Set.{u}) : Set.{u} := ⋃₀ {x, y} /-- The binary intersection operation -/ protected def inter (x y : Set.{u}) : Set.{u} := {z ∈ x | z ∈ y} /-- The set difference operation -/ protected def diff (x y : Set.{u}) : Set.{u} := {z ∈ x | z ∉ y} instance : has_union Set := ⟨Set.union⟩ instance : has_inter Set := ⟨Set.inter⟩ instance : has_sdiff Set := ⟨Set.diff⟩ @[simp] theorem to_set_union (x y : Set.{u}) : (x ∪ y).to_set = x.to_set ∪ y.to_set := by { unfold has_union.union, rw Set.union, simp } @[simp] theorem to_set_inter (x y : Set.{u}) : (x ∩ y).to_set = x.to_set ∩ y.to_set := by { unfold has_inter.inter, rw Set.inter, ext, simp } @[simp] theorem to_set_sdiff (x y : Set.{u}) : (x \ y).to_set = x.to_set \ y.to_set := by { change {z ∈ x | z ∉ y}.to_set = _, ext, simp } @[simp] theorem mem_union {x y z : Set.{u}} : z ∈ x ∪ y ↔ z ∈ x ∨ z ∈ y := by { rw ←mem_to_set, simp } @[simp] theorem mem_inter {x y z : Set.{u}} : z ∈ x ∩ y ↔ z ∈ x ∧ z ∈ y := @@mem_sep (λ z : Set.{u}, z ∈ y) @[simp] theorem mem_diff {x y z : Set.{u}} : z ∈ x \ y ↔ z ∈ x ∧ z ∉ y := @@mem_sep (λ z : Set.{u}, z ∉ y) /-- Induction on the `∈` relation. -/ @[elab_as_eliminator] theorem induction_on {p : Set → Prop} (x) (h : ∀ x, (∀ y ∈ x, p y) → p x) : p x := quotient.induction_on x $ λ u, pSet.rec_on u $ λ α A IH, h _ $ λ y, show @has_mem.mem _ _ Set.has_mem y ⟦⟨α, A⟩⟧ → p y, from quotient.induction_on y (λ v ⟨a, ha⟩, by { rw (@quotient.sound pSet _ _ _ ha), exact IH a }) theorem mem_wf : @well_founded Set (∈) := ⟨λ x, induction_on x acc.intro⟩ instance : has_well_founded Set := ⟨_, mem_wf⟩ instance : is_asymm Set (∈) := mem_wf.is_asymm theorem mem_asymm {x y : Set} : x ∈ y → y ∉ x := asymm theorem mem_irrefl (x : Set) : x ∉ x := irrefl x theorem regularity (x : Set.{u}) (h : x ≠ ∅) : ∃ y ∈ x, x ∩ y = ∅ := classical.by_contradiction $ λ ne, h $ (eq_empty x).2 $ λ y, induction_on y $ λ z (IH : ∀ w : Set.{u}, w ∈ z → w ∉ x), show z ∉ x, from λ zx, ne ⟨z, zx, (eq_empty _).2 (λ w wxz, let ⟨wx, wz⟩ := mem_inter.1 wxz in IH w wz wx)⟩ /-- The image of a (definable) ZFC set function -/ def image (f : Set → Set) [H : definable 1 f] : Set → Set := let r := @definable.resp 1 f _ in resp.eval 1 ⟨image r.1, λ x y e, mem.ext $ λ z, iff.trans (mem_image r.2) $ iff.trans (by exact ⟨λ ⟨w, h1, h2⟩, ⟨w, (mem.congr_right e).1 h1, h2⟩, λ ⟨w, h1, h2⟩, ⟨w, (mem.congr_right e).2 h1, h2⟩⟩) $ iff.symm (mem_image r.2)⟩ theorem image.mk : Π (f : Set.{u} → Set.{u}) [H : definable 1 f] (x) {y} (h : y ∈ x), f y ∈ @image f H x | ._ ⟨F⟩ x y := quotient.induction_on₂ x y $ λ ⟨α, A⟩ y ⟨a, ya⟩, ⟨a, F.2 _ _ ya⟩ @[simp] theorem mem_image : Π {f : Set.{u} → Set.{u}} [H : definable 1 f] {x y : Set.{u}}, y ∈ @image f H x ↔ ∃ z ∈ x, f z = y | ._ ⟨F⟩ x y := quotient.induction_on₂ x y $ λ ⟨α, A⟩ y, ⟨λ ⟨a, ya⟩, ⟨⟦A a⟧, mem.mk A a, eq.symm $ quotient.sound ya⟩, λ ⟨z, hz, e⟩, e ▸ image.mk _ _ hz⟩ @[simp] theorem to_set_image (f : Set → Set) [H : definable 1 f] (x : Set) : (image f x).to_set = f '' x.to_set := by { ext, simp } /-- Kuratowski ordered pair -/ def pair (x y : Set.{u}) : Set.{u} := {{x}, {x, y}} @[simp] theorem to_set_pair (x y : Set.{u}) : (pair x y).to_set = {{x}, {x, y}} := by simp [pair] /-- A subset of pairs `{(a, b) ∈ x × y | p a b}` -/ def pair_sep (p : Set.{u} → Set.{u} → Prop) (x y : Set.{u}) : Set.{u} := {z ∈ powerset (powerset (x ∪ y)) | ∃ a ∈ x, ∃ b ∈ y, z = pair a b ∧ p a b} @[simp] theorem mem_pair_sep {p} {x y z : Set.{u}} : z ∈ pair_sep p x y ↔ ∃ a ∈ x, ∃ b ∈ y, z = pair a b ∧ p a b := begin refine mem_sep.trans ⟨and.right, λ e, ⟨_, e⟩⟩, rcases e with ⟨a, ax, b, bY, rfl, pab⟩, simp only [mem_powerset, subset_def, mem_union, pair, mem_pair], rintros u (rfl|rfl) v; simp only [mem_singleton, mem_pair], { rintro rfl, exact or.inl ax }, { rintro (rfl|rfl); [left, right]; assumption } end theorem pair_injective : function.injective2 pair := λ x x' y y' H, begin have ae := ext_iff.1 H, simp only [pair, mem_pair] at ae, obtain rfl : x = x', { cases (ae {x}).1 (by simp) with h h, { exact singleton_injective h }, { have m : x' ∈ ({x} : Set), { simp [h] }, rw mem_singleton.mp m } }, have he : x = y → y = y', { rintro rfl, cases (ae {x, y'}).2 (by simp only [eq_self_iff_true, or_true]) with xy'x xy'xx, { rw [eq_comm, ←mem_singleton, ←xy'x, mem_pair], exact or.inr rfl }, { simpa [eq_comm] using (ext_iff.1 xy'xx y').1 (by simp) } }, obtain xyx | xyy' := (ae {x, y}).1 (by simp), { obtain rfl := mem_singleton.mp ((ext_iff.1 xyx y).1 $ by simp), simp [he rfl] }, { obtain rfl | yy' := mem_pair.mp ((ext_iff.1 xyy' y).1 $ by simp), { simp [he rfl] }, { simp [yy'] } } end @[simp] theorem pair_inj {x y x' y' : Set} : pair x y = pair x' y' ↔ x = x' ∧ y = y' := pair_injective.eq_iff /-- The cartesian product, `{(a, b) | a ∈ x, b ∈ y}` -/ def prod : Set.{u} → Set.{u} → Set.{u} := pair_sep (λ a b, true) @[simp] theorem mem_prod {x y z : Set.{u}} : z ∈ prod x y ↔ ∃ a ∈ x, ∃ b ∈ y, z = pair a b := by simp [prod] @[simp] theorem pair_mem_prod {x y a b : Set.{u}} : pair a b ∈ prod x y ↔ a ∈ x ∧ b ∈ y := ⟨λ h, let ⟨a', a'x, b', b'y, e⟩ := mem_prod.1 h in match a', b', pair_injective e, a'x, b'y with ._, ._, ⟨rfl, rfl⟩, ax, bY := ⟨ax, bY⟩ end, λ ⟨ax, bY⟩, mem_prod.2 ⟨a, ax, b, bY, rfl⟩⟩ /-- `is_func x y f` is the assertion that `f` is a subset of `x × y` which relates to each element of `x` a unique element of `y`, so that we can consider `f`as a ZFC function `x → y`. -/ def is_func (x y f : Set.{u}) : Prop := f ⊆ prod x y ∧ ∀ z : Set.{u}, z ∈ x → ∃! w, pair z w ∈ f /-- `funs x y` is `y ^ x`, the set of all set functions `x → y` -/ def funs (x y : Set.{u}) : Set.{u} := {f ∈ powerset (prod x y) | is_func x y f} @[simp] theorem mem_funs {x y f : Set.{u}} : f ∈ funs x y ↔ is_func x y f := by simp [funs, is_func] -- TODO(Mario): Prove this computably noncomputable instance map_definable_aux (f : Set → Set) [H : definable 1 f] : definable 1 (λ y, pair y (f y)) := @classical.all_definable 1 _ /-- Graph of a function: `map f x` is the ZFC function which maps `a ∈ x` to `f a` -/ noncomputable def map (f : Set → Set) [H : definable 1 f] : Set → Set := image (λ y, pair y (f y)) @[simp] theorem mem_map {f : Set → Set} [H : definable 1 f] {x y : Set} : y ∈ map f x ↔ ∃ z ∈ x, pair z (f z) = y := mem_image theorem map_unique {f : Set.{u} → Set.{u}} [H : definable 1 f] {x z : Set.{u}} (zx : z ∈ x) : ∃! w, pair z w ∈ map f x := ⟨f z, image.mk _ _ zx, λ y yx, let ⟨w, wx, we⟩ := mem_image.1 yx, ⟨wz, fy⟩ := pair_injective we in by rw[←fy, wz]⟩ @[simp] theorem map_is_func {f : Set → Set} [H : definable 1 f] {x y : Set} : is_func x y (map f x) ↔ ∀ z ∈ x, f z ∈ y := ⟨λ ⟨ss, h⟩ z zx, let ⟨t, t1, t2⟩ := h z zx in (t2 (f z) (image.mk _ _ zx)).symm ▸ (pair_mem_prod.1 (ss t1)).right, λ h, ⟨λ y yx, let ⟨z, zx, ze⟩ := mem_image.1 yx in ze ▸ pair_mem_prod.2 ⟨zx, h z zx⟩, λ z, map_unique⟩⟩ end Set /-- The collection of all classes. We define `Class` as `set Set`, as this allows us to get many instances automatically. However, in practice, we treat it as (the definitionally equal) `Set → Prop`. This means, the preferred way to state that `x : Set` belongs to `A : Class` is to write `A x`. -/ @[derive [has_subset, has_sep Set, has_emptyc, inhabited, has_insert Set, has_union, has_inter, has_compl, has_sdiff]] def Class := set Set namespace Class /-- Coerce a ZFC set into a class -/ def of_Set (x : Set.{u}) : Class.{u} := {y | y ∈ x} instance : has_coe Set Class := ⟨of_Set⟩ /-- The universal class -/ def univ : Class := set.univ /-- Assert that `A` is a ZFC set satisfying `B` -/ def to_Set (B : Class.{u}) (A : Class.{u}) : Prop := ∃ x, ↑x = A ∧ B x /-- `A ∈ B` if `A` is a ZFC set which satisfies `B` -/ protected def mem (A B : Class.{u}) : Prop := to_Set.{u} B A instance : has_mem Class Class := ⟨Class.mem⟩ theorem mem_univ {A : Class.{u}} : A ∈ univ.{u} ↔ ∃ x : Set.{u}, ↑x = A := exists_congr $ λ x, and_true _ theorem mem_wf : @well_founded Class.{u} (∈) := ⟨begin have H : ∀ x : Set.{u}, @acc Class.{u} (∈) ↑x, { refine λ a, Set.induction_on a (λ x IH, ⟨x, _⟩), rintros A ⟨z, rfl, hz⟩, exact IH z hz }, { refine λ A, ⟨A, _⟩, rintros B ⟨x, rfl, hx⟩, exact H x } end⟩ instance : has_well_founded Class := ⟨_, mem_wf⟩ instance : is_asymm Class (∈) := mem_wf.is_asymm theorem mem_asymm {x y : Class} : x ∈ y → y ∉ x := asymm theorem mem_irrefl (x : Class) : x ∉ x := irrefl x /-- There is no universal set. -/ theorem univ_not_mem_univ : univ ∉ univ := mem_irrefl _ /-- Convert a conglomerate (a collection of classes) into a class -/ def Cong_to_Class (x : set Class.{u}) : Class.{u} := {y | ↑y ∈ x} /-- Convert a class into a conglomerate (a collection of classes) -/ def Class_to_Cong (x : Class.{u}) : set Class.{u} := {y | y ∈ x} /-- The power class of a class is the class of all subclasses that are ZFC sets -/ def powerset (x : Class) : Class := Cong_to_Class (set.powerset x) /-- The union of a class is the class of all members of ZFC sets in the class -/ def sUnion (x : Class) : Class := ⋃₀ (Class_to_Cong x) prefix (name := Class.sUnion) `⋃₀ `:110 := Class.sUnion theorem of_Set.inj {x y : Set.{u}} (h : (x : Class.{u}) = y) : x = y := Set.ext $ λ z, by { change (x : Class.{u}) z ↔ (y : Class.{u}) z, rw h } @[simp] theorem to_Set_of_Set (A : Class.{u}) (x : Set.{u}) : to_Set A x ↔ A x := ⟨λ ⟨y, yx, py⟩, by rwa of_Set.inj yx at py, λ px, ⟨x, rfl, px⟩⟩ @[simp] theorem mem_hom_left (x : Set.{u}) (A : Class.{u}) : (x : Class.{u}) ∈ A ↔ A x := to_Set_of_Set _ _ @[simp] theorem mem_hom_right (x y : Set.{u}) : (y : Class.{u}) x ↔ x ∈ y := iff.rfl @[simp] theorem subset_hom (x y : Set.{u}) : (x : Class.{u}) ⊆ y ↔ x ⊆ y := iff.rfl @[simp] theorem sep_hom (p : Class.{u}) (x : Set.{u}) : (↑{y ∈ x | p y} : Class.{u}) = {y ∈ x | p y} := set.ext $ λ y, Set.mem_sep @[simp] theorem empty_hom : ↑(∅ : Set.{u}) = (∅ : Class.{u}) := set.ext $ λ y, (iff_false _).2 (Set.mem_empty y) @[simp] theorem insert_hom (x y : Set.{u}) : (@insert Set.{u} Class.{u} _ x y) = ↑(insert x y) := set.ext $ λ z, iff.symm Set.mem_insert_iff @[simp] theorem union_hom (x y : Set.{u}) : (x : Class.{u}) ∪ y = (x ∪ y : Set.{u}) := set.ext $ λ z, iff.symm Set.mem_union @[simp] theorem inter_hom (x y : Set.{u}) : (x : Class.{u}) ∩ y = (x ∩ y : Set.{u}) := set.ext $ λ z, iff.symm Set.mem_inter @[simp] theorem diff_hom (x y : Set.{u}) : (x : Class.{u}) \ y = (x \ y : Set.{u}) := set.ext $ λ z, iff.symm Set.mem_diff @[simp] theorem powerset_hom (x : Set.{u}) : powerset.{u} x = Set.powerset x := set.ext $ λ z, iff.symm Set.mem_powerset @[simp] theorem sUnion_hom (x : Set.{u}) : ⋃₀ (x : Class.{u}) = ⋃₀ x := set.ext $ λ z, by { refine iff.trans _ Set.mem_sUnion.symm, exact ⟨λ ⟨._, ⟨a, rfl, ax⟩, za⟩, ⟨a, ax, za⟩, λ ⟨a, ax, za⟩, ⟨_, ⟨a, rfl, ax⟩, za⟩⟩ } /-- The definite description operator, which is `{x}` if `{y | A y} = {x}` and `∅` otherwise. -/ def iota (A : Class) : Class := ⋃₀ {x | ∀ y, A y ↔ y = x} theorem iota_val (A : Class) (x : Set) (H : ∀ y, A y ↔ y = x) : iota A = ↑x := set.ext $ λ y, ⟨λ ⟨._, ⟨x', rfl, h⟩, yx'⟩, by rwa ←((H x').1 $ (h x').2 rfl), λ yx, ⟨_, ⟨x, rfl, H⟩, yx⟩⟩ /-- Unlike the other set constructors, the `iota` definite descriptor is a set for any set input, but not constructively so, so there is no associated `Class → Set` function. -/ theorem iota_ex (A) : iota.{u} A ∈ univ.{u} := mem_univ.2 $ or.elim (classical.em $ ∃ x, ∀ y, A y ↔ y = x) (λ ⟨x, h⟩, ⟨x, eq.symm $ iota_val A x h⟩) (λ hn, ⟨∅, set.ext (λ z, empty_hom.symm ▸ ⟨false.rec _, λ ⟨._, ⟨x, rfl, H⟩, zA⟩, hn ⟨x, H⟩⟩)⟩) /-- Function value -/ def fval (F A : Class.{u}) : Class.{u} := iota (λ y, to_Set (λ x, F (Set.pair x y)) A) infixl ` ′ `:100 := fval theorem fval_ex (F A : Class.{u}) : F ′ A ∈ univ.{u} := iota_ex _ end Class namespace Set @[simp] theorem map_fval {f : Set.{u} → Set.{u}} [H : pSet.definable 1 f] {x y : Set.{u}} (h : y ∈ x) : (Set.map f x ′ y : Class.{u}) = f y := Class.iota_val _ _ (λ z, by { rw [Class.to_Set_of_Set, Class.mem_hom_right, mem_map], exact ⟨λ ⟨w, wz, pr⟩, let ⟨wy, fw⟩ := Set.pair_injective pr in by rw[←fw, wy], λ e, by { subst e, exact ⟨_, h, rfl⟩ }⟩ }) variables (x : Set.{u}) (h : ∅ ∉ x) /-- A choice function on the class of nonempty ZFC sets. -/ noncomputable def choice : Set := @map (λ y, classical.epsilon (λ z, z ∈ y)) (classical.all_definable _) x include h theorem choice_mem_aux (y : Set.{u}) (yx : y ∈ x) : classical.epsilon (λ z : Set.{u}, z ∈ y) ∈ y := @classical.epsilon_spec _ (λ z : Set.{u}, z ∈ y) $ classical.by_contradiction $ λ n, h $ by rwa ←((eq_empty y).2 $ λ z zx, n ⟨z, zx⟩) theorem choice_is_func : is_func x (⋃₀ x) (choice x) := (@map_is_func _ (classical.all_definable _) _ _).2 $ λ y yx, mem_sUnion.2 ⟨y, yx, choice_mem_aux x h y yx⟩ theorem choice_mem (y : Set.{u}) (yx : y ∈ x) : (choice x ′ y : Class.{u}) ∈ (y : Class.{u}) := begin delta choice, rw [map_fval yx, Class.mem_hom_left, Class.mem_hom_right], exact choice_mem_aux x h y yx end end Set
bab692a147804a25b9ba9280fb97d4f1c2250cf9
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/linear_algebra/clifford_algebra/conjugation.lean
4944137e62d15d872d3b189e9f786a082456da5f
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,028
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import linear_algebra.clifford_algebra.basic import algebra.module.opposites /-! # Conjugations This file defines the grade reversal and grade involution functions on multivectors, `reverse` and `involute`. Together, these operations compose to form the "Clifford conjugate", hence the name of this file. https://en.wikipedia.org/wiki/Clifford_algebra#Antiautomorphisms ## Main definitions * `clifford_algebra.involute`: the grade involution, negating each basis vector * `clifford_algebra.reverse`: the grade reversion, reversing the order of a product of vectors ## Main statements * `clifford_algebra.involute_involutive` * `clifford_algebra.reverse_involutive` * `clifford_algebra.reverse_involute_commute` -/ variables {R : Type*} [comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] variables {Q : quadratic_form R M} namespace clifford_algebra section involute /-- Grade involution, inverting the sign of each basis vector. -/ def involute : clifford_algebra Q →ₐ[R] clifford_algebra Q := clifford_algebra.lift Q ⟨-(ι Q), λ m, by simp⟩ @[simp] lemma involute_ι (m : M) : involute (ι Q m) = -ι Q m := lift_ι_apply _ _ m @[simp] lemma involute_comp_involute : involute.comp involute = alg_hom.id R (clifford_algebra Q) := by { ext, simp } lemma involute_involutive : function.involutive (involute : _ → clifford_algebra Q) := alg_hom.congr_fun involute_comp_involute @[simp] lemma involute_involute : ∀ a : clifford_algebra Q, involute (involute a) = a := involute_involutive end involute section reverse open opposite /-- Grade reversion, inverting the multiplication order of basis vectors. Also called *transpose* in some literature. -/ def reverse : clifford_algebra Q →ₗ[R] clifford_algebra Q := (op_linear_equiv R).symm.to_linear_map.comp ( clifford_algebra.lift Q ⟨(opposite.op_linear_equiv R).to_linear_map.comp (ι Q), λ m, unop_injective $ by simp⟩).to_linear_map @[simp] lemma reverse_ι (m : M) : reverse (ι Q m) = ι Q m := by simp [reverse] @[simp] lemma reverse.commutes (r : R) : reverse (algebra_map R (clifford_algebra Q) r) = algebra_map R _ r := by simp [reverse] @[simp] lemma reverse.map_one : reverse (1 : clifford_algebra Q) = 1 := by convert reverse.commutes (1 : R); simp @[simp] lemma reverse.map_mul (a b : clifford_algebra Q) : reverse (a * b) = reverse b * reverse a := by simp [reverse] @[simp] lemma reverse_comp_reverse : reverse.comp reverse = (linear_map.id : _ →ₗ[R] clifford_algebra Q) := begin ext m, simp only [linear_map.id_apply, linear_map.comp_apply], induction m using clifford_algebra.induction, -- simp can close these goals, but is slow case h_grade0 : { rw [reverse.commutes, reverse.commutes] }, case h_grade1 : { rw [reverse_ι, reverse_ι] }, case h_mul : a b ha hb { rw [reverse.map_mul, reverse.map_mul, ha, hb], }, case h_add : a b ha hb { rw [reverse.map_add, reverse.map_add, ha, hb], }, end @[simp] lemma reverse_involutive : function.involutive (reverse : _ → clifford_algebra Q) := linear_map.congr_fun reverse_comp_reverse @[simp] lemma reverse_reverse : ∀ a : clifford_algebra Q, reverse (reverse a) = a := reverse_involutive lemma reverse_comp_involute : reverse.comp involute.to_linear_map = (involute.to_linear_map.comp reverse : _ →ₗ[R] clifford_algebra Q) := begin ext, simp only [linear_map.comp_apply, alg_hom.to_linear_map_apply], induction x using clifford_algebra.induction, case h_grade0 : { simp }, case h_grade1 : { simp }, case h_mul : a b ha hb { simp only [ha, hb, reverse.map_mul, alg_hom.map_mul], }, case h_add : a b ha hb { simp only [ha, hb, reverse.map_add, alg_hom.map_add], }, end /-- `clifford_algebra.reverse` and `clifford_algebra.inverse` commute. Note that the composition is sometimes referred to as the "clifford conjugate". -/ lemma reverse_involute_commute : function.commute (reverse : _ → clifford_algebra Q) involute := linear_map.congr_fun reverse_comp_involute lemma reverse_involute : ∀ a : clifford_algebra Q, reverse (involute a) = involute (reverse a) := reverse_involute_commute end reverse /-! ### Statements about conjugations of products of lists -/ section list /-- Taking the reverse of the product a list of $n$ vectors lifted via `ι` is equivalent to taking the product of the reverse of that list. -/ lemma reverse_prod_map_ι : ∀ (l : list M), reverse (l.map $ ι Q).prod = (l.map $ ι Q).reverse.prod | [] := by simp | (x :: xs) := by simp [reverse_prod_map_ι xs] /-- Taking the involute of the product a list of $n$ vectors lifted via `ι` is equivalent to premultiplying by ${-1}^n$. -/ lemma involute_prod_map_ι : ∀ l : list M, involute (l.map $ ι Q).prod = ((-1 : R)^l.length) • (l.map $ ι Q).prod | [] := by simp | (x :: xs) := by simp [pow_add, involute_prod_map_ι xs] end list end clifford_algebra