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
a5675bf4a3f9f0e0a060bc5964b274d5bca3a3be
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/nat/succ_pred.lean
f07c8d8b6bbb4485661580a014d6f04d5f4c317c
[ "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,124
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.fin.basic import order.succ_pred.basic /-! # Successors and predecessors of naturals In this file, we show that `ℕ` is both an archimedean `succ_order` and an archimedean `pred_order`. -/ open function order namespace nat @[reducible] -- so that Lean reads `nat.succ` through `succ_order.succ` instance : succ_order ℕ := { succ := succ, ..succ_order.of_succ_le_iff succ (λ a b, iff.rfl) } @[reducible] -- so that Lean reads `nat.pred` through `pred_order.pred` instance : pred_order ℕ := { pred := pred, pred_le := pred_le, min_of_le_pred := λ a ha, begin cases a, { exact is_min_bot }, { exact (not_succ_le_self _ ha).elim } end, le_pred_of_lt := λ a b h, begin cases b, { exact (a.not_lt_zero h).elim }, { exact le_of_succ_le_succ h } end, le_of_pred_lt := λ a b h, begin cases a, { exact b.zero_le }, { exact h } end } @[simp] lemma succ_eq_succ : order.succ = succ := rfl @[simp] lemma pred_eq_pred : order.pred = pred := rfl lemma succ_iterate (a : ℕ) : ∀ n, succ^[n] a = a + n | 0 := rfl | (n + 1) := by { rw [function.iterate_succ', add_succ], exact congr_arg _ n.succ_iterate } lemma pred_iterate (a : ℕ) : ∀ n, pred^[n] a = a - n | 0 := rfl | (n + 1) := by { rw [function.iterate_succ', sub_succ], exact congr_arg _ n.pred_iterate } instance : is_succ_archimedean ℕ := ⟨λ a b h, ⟨b - a, by rw [succ_eq_succ, succ_iterate, add_tsub_cancel_of_le h]⟩⟩ instance : is_pred_archimedean ℕ := ⟨λ a b h, ⟨b - a, by rw [pred_eq_pred, pred_iterate, tsub_tsub_cancel_of_le h]⟩⟩ /-! ### Covering relation -/ protected lemma covby_iff_succ_eq {m n : ℕ} : m ⋖ n ↔ m + 1 = n := succ_eq_iff_covby.symm end nat @[simp, norm_cast] lemma fin.coe_covby_iff {n : ℕ} {a b : fin n} : (a : ℕ) ⋖ b ↔ a ⋖ b := and_congr_right' ⟨λ h c hc, h hc, λ h c ha hb, @h ⟨c, hb.trans b.prop⟩ ha hb⟩ alias fin.coe_covby_iff ↔ _ covby.coe_fin
f002097b71a42531afff8fb3b481de736b44fa1b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/json.lean
f7b2591b4abcf97d7fe9dc12372d331b430ded67
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
271
lean
/- Copyright (c) E.W.Ayers 2020. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: E.W.Ayers -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.default import Mathlib.Lean3Lib.init.meta.float namespace Mathlib
adf2481bd08ab898057e837793fa07cadf6f69be
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1202.lean
7dc31d54f5273a801277c70cac1916e3f59acebf
[ "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
513
lean
opaque f : Bool → Bool → Bool axiom f_comm (a b : Bool) : f a b = f b a axiom f_assoc (a b c : Bool) : f (f a b) c = f a (f b c) instance : Lean.IsCommutative f := ⟨f_comm⟩ instance : Lean.IsAssociative f := ⟨f_assoc⟩ example (a b c : Bool) : f (f a b) c = f (f a c) b := by ac_rfl -- good example (a b c : Bool) : (f (f a b) c = f (f a c) b) ∧ true := And.intro (by ac_rfl) rfl -- good example (a b c : Bool) : (f (f a b) c = f (f a c) b) ∧ true := by apply And.intro . ac_rfl . rfl
53b55fe6106b85d2a7b7dbc4fc4c30d739decb09
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/dofun_prec.lean
d3da42f66995e9ea98bb61b416b132784a2f011a
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
178
lean
new_frontend def tst1 (x : Nat) : IO Unit := when (x > 0) do IO.println "hello" IO.println "world" def tst2 (xs : List Nat) : IO Unit := xs.forM fun x => do IO.println x
e7b305d44975d50f3e8e4cef99b1ecd146391bdc
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch2/ex0602.lean
fdff9282fb0d67600edd9d971f0ea0732f53a7f5
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
211
lean
variables (α β γ : Type) def compose (g : β → γ) (f : α → β) (x : α) : γ := g (f x) def do_twice (h : α → α) (x : α) : α := h (h x) def do_thrice (h : α → α) (x : α) : α := h (h (h x))
550a71839c139536bed7d705243129cf257b6969
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/function/conditional_expectation/basic.lean
dab82b38e673d9bb32252364e0483e39873ea056
[ "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
20,307
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import measure_theory.function.conditional_expectation.condexp_L1 /-! # Conditional expectation > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We build the conditional expectation of an integrable function `f` with value in a Banach space with respect to a measure `μ` (defined on a measurable space structure `m0`) and a measurable space structure `m` with `hm : m ≤ m0` (a sub-sigma-algebra). This is an `m`-strongly measurable function `μ[f|hm]` which is integrable and verifies `∫ x in s, μ[f|hm] x ∂μ = ∫ x in s, f x ∂μ` for all `m`-measurable sets `s`. It is unique as an element of `L¹`. The construction is done in four steps: * Define the conditional expectation of an `L²` function, as an element of `L²`. This is the orthogonal projection on the subspace of almost everywhere `m`-measurable functions. * Show that the conditional expectation of the indicator of a measurable set with finite measure is integrable and define a map `set α → (E →L[ℝ] (α →₁[μ] E))` which to a set associates a linear map. That linear map sends `x ∈ E` to the conditional expectation of the indicator of the set with value `x`. * Extend that map to `condexp_L1_clm : (α →₁[μ] E) →L[ℝ] (α →₁[μ] E)`. This is done using the same construction as the Bochner integral (see the file `measure_theory/integral/set_to_L1`). * Define the conditional expectation of a function `f : α → E`, which is an integrable function `α → E` equal to 0 if `f` is not integrable, and equal to an `m`-measurable representative of `condexp_L1_clm` applied to `[f]`, the equivalence class of `f` in `L¹`. The first step is done in `measure_theory.function.conditional_expectation.condexp_L2`, the two next steps in `measure_theory.function.conditional_expectation.condexp_L1` and the final step is performed in this file. ## Main results The conditional expectation and its properties * `condexp (m : measurable_space α) (μ : measure α) (f : α → E)`: conditional expectation of `f` with respect to `m`. * `integrable_condexp` : `condexp` is integrable. * `strongly_measurable_condexp` : `condexp` is `m`-strongly-measurable. * `set_integral_condexp (hf : integrable f μ) (hs : measurable_set[m] s)` : if `m ≤ m0` (the σ-algebra over which the measure is defined), then the conditional expectation verifies `∫ x in s, condexp m μ f x ∂μ = ∫ x in s, f x ∂μ` for any `m`-measurable set `s`. While `condexp` is function-valued, we also define `condexp_L1` with value in `L1` and a continuous linear map `condexp_L1_clm` from `L1` to `L1`. `condexp` should be used in most cases. Uniqueness of the conditional expectation * `ae_eq_condexp_of_forall_set_integral_eq`: an a.e. `m`-measurable function which verifies the equality of integrals is a.e. equal to `condexp`. ## Notations For a measure `μ` defined on a measurable space structure `m0`, another measurable space structure `m` with `hm : m ≤ m0` (a sub-σ-algebra) and a function `f`, we define the notation * `μ[f|m] = condexp m μ f`. ## Tags conditional expectation, conditional expected value -/ open topological_space measure_theory.Lp filter open_locale ennreal topology big_operators measure_theory namespace measure_theory variables {α F F' 𝕜 : Type*} {p : ℝ≥0∞} [is_R_or_C 𝕜] -- 𝕜 for ℝ or ℂ -- F for a Lp submodule [normed_add_comm_group F] [normed_space 𝕜 F] -- F' for integrals on a Lp submodule [normed_add_comm_group F'] [normed_space 𝕜 F'] [normed_space ℝ F'] [complete_space F'] open_locale classical variables {𝕜} {m m0 : measurable_space α} {μ : measure α} {f g : α → F'} {s : set α} /-- Conditional expectation of a function. It is defined as 0 if any one of the following conditions is true: - `m` is not a sub-σ-algebra of `m0`, - `μ` is not σ-finite with respect to `m`, - `f` is not integrable. -/ @[irreducible] noncomputable def condexp (m : measurable_space α) {m0 : measurable_space α} (μ : measure α) (f : α → F') : α → F' := if hm : m ≤ m0 then if h : sigma_finite (μ.trim hm) ∧ integrable f μ then if strongly_measurable[m] f then f else (@ae_strongly_measurable'_condexp_L1 _ _ _ _ _ m m0 μ hm h.1 _).mk (@condexp_L1 _ _ _ _ _ _ _ hm μ h.1 f) else 0 else 0 -- We define notation `μ[f|m]` for the conditional expectation of `f` with respect to `m`. localized "notation (name := measure_theory.condexp) μ `[` f `|` m `]` := measure_theory.condexp m μ f" in measure_theory lemma condexp_of_not_le (hm_not : ¬ m ≤ m0) : μ[f|m] = 0 := by rw [condexp, dif_neg hm_not] lemma condexp_of_not_sigma_finite (hm : m ≤ m0) (hμm_not : ¬ sigma_finite (μ.trim hm)) : μ[f|m] = 0 := by { rw [condexp, dif_pos hm, dif_neg], push_neg, exact λ h, absurd h hμm_not, } lemma condexp_of_sigma_finite (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] : μ[f|m] = if integrable f μ then if strongly_measurable[m] f then f else ae_strongly_measurable'_condexp_L1.mk (condexp_L1 hm μ f) else 0 := begin rw [condexp, dif_pos hm], simp only [hμm, ne.def, true_and], by_cases hf : integrable f μ, { rw [dif_pos hf, if_pos hf], }, { rw [dif_neg hf, if_neg hf], }, end lemma condexp_of_strongly_measurable (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] {f : α → F'} (hf : strongly_measurable[m] f) (hfi : integrable f μ) : μ[f|m] = f := by { rw [condexp_of_sigma_finite hm, if_pos hfi, if_pos hf], apply_instance, } lemma condexp_const (hm : m ≤ m0) (c : F') [is_finite_measure μ] : μ[(λ x : α, c)|m] = λ _, c := condexp_of_strongly_measurable hm (@strongly_measurable_const _ _ m _ _) (integrable_const c) lemma condexp_ae_eq_condexp_L1 (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] (f : α → F') : μ[f|m] =ᵐ[μ] condexp_L1 hm μ f := begin rw condexp_of_sigma_finite hm, by_cases hfi : integrable f μ, { rw if_pos hfi, by_cases hfm : strongly_measurable[m] f, { rw if_pos hfm, exact (condexp_L1_of_ae_strongly_measurable' (strongly_measurable.ae_strongly_measurable' hfm) hfi).symm, }, { rw if_neg hfm, exact (ae_strongly_measurable'.ae_eq_mk ae_strongly_measurable'_condexp_L1).symm, }, }, rw [if_neg hfi, condexp_L1_undef hfi], exact (coe_fn_zero _ _ _).symm, end lemma condexp_ae_eq_condexp_L1_clm (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hf : integrable f μ) : μ[f|m] =ᵐ[μ] condexp_L1_clm hm μ (hf.to_L1 f) := begin refine (condexp_ae_eq_condexp_L1 hm f).trans (eventually_of_forall (λ x, _)), rw condexp_L1_eq hf, end lemma condexp_undef (hf : ¬ integrable f μ) : μ[f|m] = 0 := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, rw [condexp_of_sigma_finite, if_neg hf], end @[simp] lemma condexp_zero : μ[(0 : α → F')|m] = 0 := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, exact condexp_of_strongly_measurable hm (@strongly_measurable_zero _ _ m _ _) (integrable_zero _ _ _), end lemma strongly_measurable_condexp : strongly_measurable[m] (μ[f|m]) := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, exact strongly_measurable_zero, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, exact strongly_measurable_zero, }, haveI : sigma_finite (μ.trim hm) := hμm, rw condexp_of_sigma_finite hm, swap, { apply_instance, }, split_ifs with hfi hfm, { exact hfm, }, { exact ae_strongly_measurable'.strongly_measurable_mk _, }, { exact strongly_measurable_zero, }, end lemma condexp_congr_ae (h : f =ᵐ[μ] g) : μ[f | m] =ᵐ[μ] μ[g | m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, exact (condexp_ae_eq_condexp_L1 hm f).trans (filter.eventually_eq.trans (by rw condexp_L1_congr_ae hm h) (condexp_ae_eq_condexp_L1 hm g).symm), end lemma condexp_of_ae_strongly_measurable' (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] {f : α → F'} (hf : ae_strongly_measurable' m f μ) (hfi : integrable f μ) : μ[f|m] =ᵐ[μ] f := begin refine ((condexp_congr_ae hf.ae_eq_mk).trans _).trans hf.ae_eq_mk.symm, rw condexp_of_strongly_measurable hm hf.strongly_measurable_mk ((integrable_congr hf.ae_eq_mk).mp hfi), end lemma integrable_condexp : integrable (μ[f|m]) μ := begin by_cases hm : m ≤ m0, swap, { rw condexp_of_not_le hm, exact integrable_zero _ _ _, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { rw condexp_of_not_sigma_finite hm hμm, exact integrable_zero _ _ _, }, haveI : sigma_finite (μ.trim hm) := hμm, exact (integrable_condexp_L1 f).congr (condexp_ae_eq_condexp_L1 hm f).symm, end /-- The integral of the conditional expectation `μ[f|hm]` over an `m`-measurable set is equal to the integral of `f` on that set. -/ lemma set_integral_condexp (hm : m ≤ m0) [sigma_finite (μ.trim hm)] (hf : integrable f μ) (hs : measurable_set[m] s) : ∫ x in s, μ[f|m] x ∂μ = ∫ x in s, f x ∂μ := begin rw set_integral_congr_ae (hm s hs) ((condexp_ae_eq_condexp_L1 hm f).mono (λ x hx _, hx)), exact set_integral_condexp_L1 hf hs, end lemma integral_condexp (hm : m ≤ m0) [hμm : sigma_finite (μ.trim hm)] (hf : integrable f μ) : ∫ x, μ[f|m] x ∂μ = ∫ x, f x ∂μ := begin suffices : ∫ x in set.univ, μ[f|m] x ∂μ = ∫ x in set.univ, f x ∂μ, by { simp_rw integral_univ at this, exact this, }, exact set_integral_condexp hm hf (@measurable_set.univ _ m), end /-- **Uniqueness of the conditional expectation** If a function is a.e. `m`-measurable, verifies an integrability condition and has same integral as `f` on all `m`-measurable sets, then it is a.e. equal to `μ[f|hm]`. -/ lemma ae_eq_condexp_of_forall_set_integral_eq (hm : m ≤ m0) [sigma_finite (μ.trim hm)] {f g : α → F'} (hf : integrable f μ) (hg_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on g s μ) (hg_eq : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, g x ∂μ = ∫ x in s, f x ∂μ) (hgm : ae_strongly_measurable' m g μ) : g =ᵐ[μ] μ[f|m] := begin refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' hm hg_int_finite (λ s hs hμs, integrable_condexp.integrable_on) (λ s hs hμs, _) hgm (strongly_measurable.ae_strongly_measurable' strongly_measurable_condexp), rw [hg_eq s hs hμs, set_integral_condexp hm hf hs], end lemma condexp_bot' [hμ : μ.ae.ne_bot] (f : α → F') : μ[f|⊥] = λ _, (μ set.univ).to_real⁻¹ • ∫ x, f x ∂μ := begin by_cases hμ_finite : is_finite_measure μ, swap, { have h : ¬ sigma_finite (μ.trim bot_le), { rwa sigma_finite_trim_bot_iff, }, rw not_is_finite_measure_iff at hμ_finite, rw [condexp_of_not_sigma_finite bot_le h], simp only [hμ_finite, ennreal.top_to_real, inv_zero, zero_smul], refl, }, haveI : is_finite_measure μ := hμ_finite, by_cases hf : integrable f μ, swap, { rw [integral_undef hf, smul_zero, condexp_undef hf], refl, }, have h_meas : strongly_measurable[⊥] (μ[f|⊥]) := strongly_measurable_condexp, obtain ⟨c, h_eq⟩ := strongly_measurable_bot_iff.mp h_meas, rw h_eq, have h_integral : ∫ x, μ[f|⊥] x ∂μ = ∫ x, f x ∂μ := integral_condexp bot_le hf, simp_rw [h_eq, integral_const] at h_integral, rw [← h_integral, ← smul_assoc, smul_eq_mul, inv_mul_cancel, one_smul], rw [ne.def, ennreal.to_real_eq_zero_iff, auto.not_or_eq, measure.measure_univ_eq_zero, ← ae_eq_bot, ← ne.def, ← ne_bot_iff], exact ⟨hμ, measure_ne_top μ set.univ⟩, end lemma condexp_bot_ae_eq (f : α → F') : μ[f|⊥] =ᵐ[μ] λ _, (μ set.univ).to_real⁻¹ • ∫ x, f x ∂μ := begin by_cases μ.ae.ne_bot, { refine eventually_of_forall (λ x, _), rw condexp_bot' f, exact h, }, { rw [ne_bot_iff, not_not, ae_eq_bot] at h, simp only [h, ae_zero], }, end lemma condexp_bot [is_probability_measure μ] (f : α → F') : μ[f|⊥] = λ _, ∫ x, f x ∂μ := by { refine (condexp_bot' f).trans _, rw [measure_univ, ennreal.one_to_real, inv_one, one_smul], } lemma condexp_add (hf : integrable f μ) (hg : integrable g μ) : μ[f + g | m] =ᵐ[μ] μ[f|m] + μ[g|m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, simp, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, simp, }, haveI : sigma_finite (μ.trim hm) := hμm, refine (condexp_ae_eq_condexp_L1 hm _).trans _, rw condexp_L1_add hf hg, exact (coe_fn_add _ _).trans ((condexp_ae_eq_condexp_L1 hm _).symm.add (condexp_ae_eq_condexp_L1 hm _).symm), end lemma condexp_finset_sum {ι : Type*} {s : finset ι} {f : ι → α → F'} (hf : ∀ i ∈ s, integrable (f i) μ) : μ[∑ i in s, f i | m] =ᵐ[μ] ∑ i in s, μ[f i | m] := begin induction s using finset.induction_on with i s his heq hf, { rw [finset.sum_empty, finset.sum_empty, condexp_zero] }, { rw [finset.sum_insert his, finset.sum_insert his], exact (condexp_add (hf i $ finset.mem_insert_self i s) $ integrable_finset_sum' _ (λ j hmem, hf j $ finset.mem_insert_of_mem hmem)).trans ((eventually_eq.refl _ _).add (heq $ λ j hmem, hf j $ finset.mem_insert_of_mem hmem)) } end lemma condexp_smul (c : 𝕜) (f : α → F') : μ[c • f | m] =ᵐ[μ] c • μ[f|m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, simp, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, simp, }, haveI : sigma_finite (μ.trim hm) := hμm, refine (condexp_ae_eq_condexp_L1 hm _).trans _, rw condexp_L1_smul c f, refine (@condexp_ae_eq_condexp_L1 _ _ _ _ _ m _ _ hm _ f).mp _, refine (coe_fn_smul c (condexp_L1 hm μ f)).mono (λ x hx1 hx2, _), rw [hx1, pi.smul_apply, pi.smul_apply, hx2], end lemma condexp_neg (f : α → F') : μ[-f|m] =ᵐ[μ] - μ[f|m] := by letI : module ℝ (α → F') := @pi.module α (λ _, F') ℝ _ _ (λ _, infer_instance); calc μ[-f|m] = μ[(-1 : ℝ) • f|m] : by rw neg_one_smul ℝ f ... =ᵐ[μ] (-1 : ℝ) • μ[f|m] : condexp_smul (-1) f ... = -μ[f|m] : neg_one_smul ℝ (μ[f|m]) lemma condexp_sub (hf : integrable f μ) (hg : integrable g μ) : μ[f - g | m] =ᵐ[μ] μ[f|m] - μ[g|m] := begin simp_rw sub_eq_add_neg, exact (condexp_add hf hg.neg).trans (eventually_eq.rfl.add (condexp_neg g)), end lemma condexp_condexp_of_le {m₁ m₂ m0 : measurable_space α} {μ : measure α} (hm₁₂ : m₁ ≤ m₂) (hm₂ : m₂ ≤ m0) [sigma_finite (μ.trim hm₂)] : μ[ μ[f|m₂] | m₁] =ᵐ[μ] μ[f | m₁] := begin by_cases hμm₁ : sigma_finite (μ.trim (hm₁₂.trans hm₂)), swap, { simp_rw condexp_of_not_sigma_finite (hm₁₂.trans hm₂) hμm₁, }, haveI : sigma_finite (μ.trim (hm₁₂.trans hm₂)) := hμm₁, by_cases hf : integrable f μ, swap, { simp_rw [condexp_undef hf, condexp_zero], }, refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hm₁₂.trans hm₂) (λ s hs hμs, integrable_condexp.integrable_on) (λ s hs hμs, integrable_condexp.integrable_on) _ (strongly_measurable.ae_strongly_measurable' strongly_measurable_condexp) (strongly_measurable.ae_strongly_measurable' strongly_measurable_condexp), intros s hs hμs, rw set_integral_condexp (hm₁₂.trans hm₂) integrable_condexp hs, swap, { apply_instance, }, rw [set_integral_condexp (hm₁₂.trans hm₂) hf hs, set_integral_condexp hm₂ hf (hm₁₂ s hs)], end lemma condexp_mono {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f g : α → E} (hf : integrable f μ) (hg : integrable g μ) (hfg : f ≤ᵐ[μ] g) : μ[f | m] ≤ᵐ[μ] μ[g | m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, exact (condexp_ae_eq_condexp_L1 hm _).trans_le ((condexp_L1_mono hf hg hfg).trans_eq (condexp_ae_eq_condexp_L1 hm _).symm), end lemma condexp_nonneg {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f : α → E} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ᵐ[μ] μ[f | m] := begin by_cases hfint : integrable f μ, { rw (condexp_zero.symm : (0 : α → E) = μ[0 | m]), exact condexp_mono (integrable_zero _ _ _) hfint hf }, { rw condexp_undef hfint, } end lemma condexp_nonpos {E} [normed_lattice_add_comm_group E] [complete_space E] [normed_space ℝ E] [ordered_smul ℝ E] {f : α → E} (hf : f ≤ᵐ[μ] 0) : μ[f | m] ≤ᵐ[μ] 0 := begin by_cases hfint : integrable f μ, { rw (condexp_zero.symm : (0 : α → E) = μ[0 | m]), exact condexp_mono hfint (integrable_zero _ _ _) hf }, { rw condexp_undef hfint, } end /-- **Lebesgue dominated convergence theorem**: sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their image by `condexp_L1`. -/ lemma tendsto_condexp_L1_of_dominated_convergence (hm : m ≤ m0) [sigma_finite (μ.trim hm)] {fs : ℕ → α → F'} {f : α → F'} (bound_fs : α → ℝ) (hfs_meas : ∀ n, ae_strongly_measurable (fs n) μ) (h_int_bound_fs : integrable bound_fs μ) (hfs_bound : ∀ n, ∀ᵐ x ∂μ, ‖fs n x‖ ≤ bound_fs x) (hfs : ∀ᵐ x ∂μ, tendsto (λ n, fs n x) at_top (𝓝 (f x))) : tendsto (λ n, condexp_L1 hm μ (fs n)) at_top (𝓝 (condexp_L1 hm μ f)) := tendsto_set_to_fun_of_dominated_convergence _ bound_fs hfs_meas h_int_bound_fs hfs_bound hfs /-- If two sequences of functions have a.e. equal conditional expectations at each step, converge and verify dominated convergence hypotheses, then the conditional expectations of their limits are a.e. equal. -/ lemma tendsto_condexp_unique (fs gs : ℕ → α → F') (f g : α → F') (hfs_int : ∀ n, integrable (fs n) μ) (hgs_int : ∀ n, integrable (gs n) μ) (hfs : ∀ᵐ x ∂μ, tendsto (λ n, fs n x) at_top (𝓝 (f x))) (hgs : ∀ᵐ x ∂μ, tendsto (λ n, gs n x) at_top (𝓝 (g x))) (bound_fs : α → ℝ) (h_int_bound_fs : integrable bound_fs μ) (bound_gs : α → ℝ) (h_int_bound_gs : integrable bound_gs μ) (hfs_bound : ∀ n, ∀ᵐ x ∂μ, ‖fs n x‖ ≤ bound_fs x) (hgs_bound : ∀ n, ∀ᵐ x ∂μ, ‖gs n x‖ ≤ bound_gs x) (hfg : ∀ n, μ[fs n | m] =ᵐ[μ] μ[gs n | m]) : μ[f | m] =ᵐ[μ] μ[g | m] := begin by_cases hm : m ≤ m0, swap, { simp_rw condexp_of_not_le hm, }, by_cases hμm : sigma_finite (μ.trim hm), swap, { simp_rw condexp_of_not_sigma_finite hm hμm, }, haveI : sigma_finite (μ.trim hm) := hμm, refine (condexp_ae_eq_condexp_L1 hm f).trans ((condexp_ae_eq_condexp_L1 hm g).trans _).symm, rw ← Lp.ext_iff, have hn_eq : ∀ n, condexp_L1 hm μ (gs n) = condexp_L1 hm μ (fs n), { intros n, ext1, refine (condexp_ae_eq_condexp_L1 hm (gs n)).symm.trans ((hfg n).symm.trans _), exact (condexp_ae_eq_condexp_L1 hm (fs n)), }, have hcond_fs : tendsto (λ n, condexp_L1 hm μ (fs n)) at_top (𝓝 (condexp_L1 hm μ f)), from tendsto_condexp_L1_of_dominated_convergence hm _ (λ n, (hfs_int n).1) h_int_bound_fs hfs_bound hfs, have hcond_gs : tendsto (λ n, condexp_L1 hm μ (gs n)) at_top (𝓝 (condexp_L1 hm μ g)), from tendsto_condexp_L1_of_dominated_convergence hm _ (λ n, (hgs_int n).1) h_int_bound_gs hgs_bound hgs, exact tendsto_nhds_unique_of_eventually_eq hcond_gs hcond_fs (eventually_of_forall hn_eq), end end measure_theory
bae16ec55faf3c245472f89bc822f769bd2ebbee
f2fbd9ce3f46053c664b74a5294d7d2f584e72d3
/src/Huber_ring.lean
8a5ed340fa69e600982f39bb9ce081aeb76bdcbc
[ "Apache-2.0" ]
permissive
jcommelin/lean-perfectoid-spaces
c656ae26a2338ee7a0072dab63baf577f079ca12
d5ed816bcc116fd4cde5ce9aaf03905d00ee391c
refs/heads/master
1,584,610,432,107
1,538,491,594,000
1,538,491,594,000
136,299,168
0
0
null
1,528,274,452,000
1,528,274,452,000
null
UTF-8
Lean
false
false
951
lean
import analysis.topology.topological_structures import for_mathlib.ideals import ring_theory.subring -- f-adic rings are called Huber rings by Scholze. -- Topological ring A contains on open subring A0 such that the subspace topology on A0 is -- I-adic, where I is a finitely generated ideal of A0 . instance subring_has_zero (R : Type*) [comm_ring R] (S : set R) [HS : is_subring S] : has_zero S := ⟨⟨0, is_add_submonoid.zero_mem S⟩⟩ class Huber_ring (R : Type*) extends comm_ring R, topological_space R, topological_ring R := (S : set R) [HS : is_subring S] (J : set S) [HJ : is_ideal J] (HJ_fin : ∃ gen : set S, set.finite gen ∧ span gen = J) (H1 : ∀ n, @topological_space.is_open S (topological_space.induced subtype.val to_topological_space) (pow_ideal J n)) (H2 : ∀ K : set S, (0 : S) ∈ K → @topological_space.is_open S (topological_space.induced subtype.val to_topological_space) K → ∃ n, pow_ideal J n ⊆ K)
189affab7a415e61dee1f92db2d01de0c6458a42
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/finset_induction_bug.lean
aa0e7c3e2575d931b30386b46b2d9d84c637b863
[ "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
947
lean
import data.finset open list namespace finset variable {A : Type} variable [h : decidable_eq A] include h set_option pp.implicit true set_option pp.notation false protected theorem induction₂ {P : finset A → Prop} (H1 : P empty) (H2 : ∀⦃s : finset A⦄, ∀{a : A}, a ∉ s → P s → P (insert a s)) : ∀s, P s := take s, quot.induction_on s take u, subtype.destruct u take l, list.induction_on l (assume nodup_l, H1) (take a l', assume IH nodup_al', assert anl' : a ∉ l', from not_mem_of_nodup_cons nodup_al', assert H3 : list.insert a l' = a :: l', from insert_eq_of_not_mem anl', assert nodup_l' : nodup l', from nodup_of_nodup_cons nodup_al', assert P_l' : P (quot.mk (subtype.tag l' nodup_l')), from IH nodup_l', assert H4 : P (insert a (quot.mk (subtype.tag l' nodup_l'))), from H2 anl' P_l', begin rewrite [eq.symm H3], apply H4 end)
7da459161bd8912654ebedbdfae12772db035388
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/mvar2.lean
3bae591d80351ef17f98c9f2502de7fe15c8b310
[ "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
1,435
lean
import Init.Lean.MetavarContext open Lean def check (b : Bool) : IO Unit := unless b (throw $ IO.userError "error") def f := mkConst `f [] def g := mkConst `g [] def a := mkConst `a [] def b := mkConst `b [] def c := mkConst `c [] def b0 := mkBVar 0 def b1 := mkBVar 1 def b2 := mkBVar 2 def u := mkLevelParam `u def typeE := mkSort levelOne def natE := mkConst `Nat [] def boolE := mkConst `Bool [] def vecE := mkConst `Vec [levelZero] def α := mkFVar `α def x := mkFVar `x def y := mkFVar `y def z := mkFVar `z def w := mkFVar `w def m1 := mkMVar `m1 def m2 := mkMVar `m2 def m3 := mkMVar `m3 def bi := BinderInfo.default def arrow (d b : Expr) := mkForall `_ bi d b def lctx1 : LocalContext := {} def lctx2 := lctx1.mkLocalDecl `α `α typeE def lctx3 := lctx2.mkLocalDecl `x `x m1 def lctx4 := lctx3.mkLocalDecl `y `y (arrow natE (mkAppN m3 #[α, x])) def mctx1 : MetavarContext := {} def mctx2 := mctx1.addExprMVarDecl `m1 `m1 lctx1 #[] typeE def mctx3 := mctx2.addExprMVarDecl `m2 `m2 lctx3 #[] natE def mctx4 := mctx3.addExprMVarDecl `m3 `m3 lctx1 #[] (arrow typeE (arrow natE natE)) def mctx5 := mctx4.assignDelayed `m3 lctx3 #[α, x] m2 def mctx6 := mctx5.assignExpr `m2 (arrow α α) def mctx7 := mctx6.assignExpr `m1 natE def t2 := lctx4.mkLambda #[α, x, y] $ mkAppN f #[mkAppN m3 #[α, x], x] #eval check (!t2.hasFVar) #eval t2 #eval (mctx6.instantiateMVars t2).1 #eval (mctx7.instantiateMVars t2).1
bb1b6ef4b6572ac91caef6ac6fd17921d6131305
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1158.lean
8ba0a3f12ca6419deb609ea0a1756d48e6361929
[ "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
1,796
lean
class magma (α) where op : α → α → α infix:70 " ⋆ " => magma.op (self := inferInstance) class leftIdMagma (α) extends magma α where identity : α id_op (a : α) : identity ⋆ a = a := by intros; rfl class rightIdMagma (α) extends magma α where identity : α op_id (a : α) : a ⋆ identity = a := by intros; rfl class semigroup (α) extends magma α where assoc (a b c : α) : (a ⋆ b) ⋆ c = a ⋆ (b ⋆ c) := by intros; rfl class idMagma (α) extends leftIdMagma α, rightIdMagma α class monoid (α) extends idMagma α, semigroup α def magmaMonoid : leftIdMagma (base → base) := { op := Function.comp identity := id } def fnCompMonoid : monoid (base → base) := { op := Function.comp, identity := id } namespace Ex2 structure A (α) where subsingleton : ∀ a b : α, a = b := by assumption structure B (α) where op : α → α → α idempotent : ∀ a : α, op a a = a := by assumption fav : α := by assumption structure C (α) where op : α → α → α comm : ∀ a b : α, op a b = op b a := by assumption structure D (α) extends A α, B α structure E (α) extends C α, B α -- Let's reuse these theorem s (a b : Unit) : a = b := rfl def op (_ _ : Unit) : Unit := () def i (a : Unit) : op a a = a := s _ a def c (a b : Unit) : op a b = op b a := s _ _ -- Successfully defined def d : D Unit := have := s; have := i; have := () { op } def e : E Unit := have := c; have := i; have := () { op } structure F (α) extends D α, E α structure G (α) extends E α, D α def f : F Unit := have := s; have := i; have := c; have := () { op } -- `idempotent`, `fav` missing def g : G Unit := have := s; have := i; have := c; have := () { op } end Ex2
c0b9b5de6f2acb433d18cbbb36b1c649be6193ec
618003631150032a5676f229d13a079ac875ff77
/src/topology/separation.lean
fd6b50a8a515eb0666700dd65435c82c761ca9e0
[ "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
15,652
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 Separation properties of topological spaces. -/ import topology.subset_properties open set filter open_locale topological_space local attribute [instance] classical.prop_decidable -- TODO: use "open_locale classical" universes u v variables {α : Type u} {β : Type v} [topological_space α] section separation /-- A T₀ space, also known as a Kolmogorov space, is a topological space where for every pair `x ≠ y`, there is an open set containing one but not the other. -/ class t0_space (α : Type u) [topological_space α] : Prop := (t0 : ∀ x y, x ≠ y → ∃ U:set α, is_open U ∧ (xor (x ∈ U) (y ∈ U))) theorem exists_open_singleton_of_open_finset [t0_space α] (s : finset α) (sne : s.nonempty) (hso : is_open (↑s : set α)) : ∃ x ∈ s, is_open ({x} : set α):= begin induction s using finset.strong_induction_on with s ihs, by_cases hs : set.subsingleton (↑s : set α), { rcases sne with ⟨x, hx⟩, refine ⟨x, hx, _⟩, have : (↑s : set α) = {x}, from hs.eq_singleton_of_mem hx, rwa this at hso }, { dunfold set.subsingleton at hs, push_neg at hs, rcases hs with ⟨x, hx, y, hy, hxy⟩, rcases t0_space.t0 x y hxy with ⟨U, hU, hxyU⟩, wlog H : x ∈ U ∧ y ∉ U := hxyU using [x y, y x], obtain ⟨z, hzs, hz⟩ : ∃ z ∈ s.filter (λ z, z ∈ U), is_open ({z} : set α), { refine ihs _ (finset.filter_ssubset.2 ⟨y, hy, H.2⟩) ⟨x, finset.mem_filter.2 ⟨hx, H.1⟩⟩ _, rw [finset.coe_filter], exact is_open_inter hso hU }, exact ⟨z, (finset.mem_filter.1 hzs).1, hz⟩ } end theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [ha : nonempty α] : ∃ x:α, is_open ({x}:set α) := begin refine ha.elim (λ x, _), have : is_open (↑(finset.univ : finset α) : set α), { simp }, rcases exists_open_singleton_of_open_finset _ ⟨x, finset.mem_univ x⟩ this with ⟨x, _, hx⟩, exact ⟨x, hx⟩ end instance subtype.t0_space [t0_space α] {p : α → Prop} : t0_space (subtype p) := ⟨λ x y hxy, let ⟨U, hU, hxyU⟩ := t0_space.t0 (x:α) y ((not_congr subtype.coe_ext).1 hxy) in ⟨(coe : subtype p → α) ⁻¹' U, is_open_induced hU, hxyU⟩⟩ /-- A T₁ space, also known as a Fréchet space, is a topological space where every singleton set is closed. Equivalently, for every pair `x ≠ y`, there is an open set containing `x` and not `y`. -/ class t1_space (α : Type u) [topological_space α] : Prop := (t1 : ∀x, is_closed ({x} : set α)) lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) := t1_space.t1 x lemma is_open_ne [t1_space α] {x : α} : is_open {y | y ≠ x} := compl_singleton_eq x ▸ is_open_compl_iff.2 (t1_space.t1 x) instance subtype.t1_space {α : Type u} [topological_space α] [t1_space α] {p : α → Prop} : t1_space (subtype p) := ⟨λ ⟨x, hx⟩, is_closed_induced_iff.2 $ ⟨{x}, is_closed_singleton, set.ext $ λ y, by simp [subtype.coe_ext]⟩⟩ @[priority 100] -- see Note [lower instance priority] instance t1_space.t0_space [t1_space α] : t0_space α := ⟨λ x y h, ⟨{z | z ≠ y}, is_open_ne, or.inl ⟨h, not_not_intro rfl⟩⟩⟩ lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : - {x} ∈ 𝓝 y := mem_nhds_sets is_closed_singleton $ by rwa [mem_compl_eq, mem_singleton_iff] @[simp] lemma closure_singleton [t1_space α] {a : α} : closure ({a} : set α) = {a} := closure_eq_of_is_closed is_closed_singleton /-- A T₂ space, also known as a Hausdorff space, is one in which for every `x ≠ y` there exists disjoint open sets around `x` and `y`. This is the most widely used of the separation axioms. -/ class t2_space (α : Type u) [topological_space α] : Prop := (t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := t2_space.t2 x y h @[priority 100] -- see Note [lower instance priority] instance t2_space.t1_space [t2_space α] : t1_space α := ⟨λ x, is_open_iff_forall_mem_open.2 $ λ y hxy, let ⟨u, v, hu, hv, hyu, hxv, huv⟩ := t2_separation (mt mem_singleton_of_eq hxy) in ⟨u, λ z hz1 hz2, (ext_iff.1 huv x).1 ⟨mem_singleton_iff.1 hz2 ▸ hz1, hxv⟩, hu, hyu⟩⟩ lemma eq_of_nhds_ne_bot [ht : t2_space α] {x y : α} (h : 𝓝 x ⊓ 𝓝 y ≠ ⊥) : x = y := classical.by_contradiction $ assume : x ≠ y, let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in absurd huv $ (inf_ne_bot_iff.1 h (mem_nhds_sets hu hx) (mem_nhds_sets hv hy)).ne_empty lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, 𝓝 x ⊓ 𝓝 y ≠ ⊥ → x = y := ⟨assume h, by exactI λ x y, eq_of_nhds_ne_bot, assume h, ⟨assume x y xy, have 𝓝 x ⊓ 𝓝 y = ⊥ := classical.by_contradiction (mt h xy), let ⟨u', hu', v', hv', u'v'⟩ := empty_in_sets_eq_bot.mpr this, ⟨u, uu', uo, hu⟩ := mem_nhds_sets_iff.mp hu', ⟨v, vv', vo, hv⟩ := mem_nhds_sets_iff.mp hv' in ⟨u, v, uo, vo, hu, hv, disjoint.eq_bot $ disjoint.mono uu' vv' u'v'⟩⟩⟩ lemma t2_iff_ultrafilter : t2_space α ↔ ∀ f {x y : α}, is_ultrafilter f → f ≤ 𝓝 x → f ≤ 𝓝 y → x = y := t2_iff_nhds.trans ⟨assume h f x y u fx fy, h $ ne_bot_of_le_ne_bot u.1 (le_inf fx fy), assume h x y xy, let ⟨f, hf, uf⟩ := exists_ultrafilter xy in h f uf (le_trans hf inf_le_left) (le_trans hf inf_le_right)⟩ @[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : 𝓝 a = 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_ne_bot $ by rw [h, inf_idem]; exact nhds_ne_bot, assume h, h ▸ rfl⟩ @[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : 𝓝 a ≤ 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_ne_bot $ by rw [inf_of_le_left h]; exact nhds_ne_bot, assume h, h ▸ le_refl _⟩ lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : l ≠ ⊥) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le_ne_bot (map_ne_bot hl) $ le_inf ha hb section lim variables [nonempty α] [t2_space α] {f : filter α} lemma lim_eq {a : α} (hf : f ≠ ⊥) (h : f ≤ 𝓝 a) : lim f = a := eq_of_nhds_ne_bot $ ne_bot_of_le_ne_bot hf $ le_inf (lim_spec ⟨_, h⟩) h @[simp] lemma lim_nhds_eq {a : α} : lim (𝓝 a) = a := lim_eq nhds_ne_bot (le_refl _) @[simp] lemma lim_nhds_eq_of_closure {a : α} {s : set α} (h : a ∈ closure s) : lim (𝓝 a ⊓ principal s) = a := lim_eq begin rw [closure_eq_nhds] at h, exact h end inf_le_left end lim @[priority 100] -- see Note [lower instance priority] instance t2_space_discrete {α : Type*} [topological_space α] [discrete_topology α] : t2_space α := { t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, rfl, rfl, eq_empty_iff_forall_not_mem.2 $ by intros z hz; cases eq_of_mem_singleton hz.1; cases eq_of_mem_singleton hz.2; cc⟩ } private lemma separated_by_f {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] [t2_space β] (f : α → β) (hf : tα ≤ tβ.induced f) {x y : α} (h : f x ≠ f y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in ⟨f ⁻¹' u, f ⁻¹' v, hf _ ⟨u, uo, rfl⟩, hf _ ⟨v, vo, rfl⟩, xu, yv, by rw [←preimage_inter, uv, preimage_empty]⟩ instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) := ⟨assume x y h, separated_by_f subtype.val (le_refl _) (mt subtype.eq h)⟩ instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] : t2_space (α × β) := ⟨assume ⟨x₁,x₂⟩ ⟨y₁,y₂⟩ h, or.elim (not_and_distrib.mp (mt prod.ext_iff.mpr h)) (λ h₁, separated_by_f prod.fst inf_le_left h₁) (λ h₂, separated_by_f prod.snd inf_le_right h₂)⟩ instance Pi.t2_space {α : Type*} {β : α → Type v} [t₂ : Πa, topological_space (β a)] [Πa, t2_space (β a)] : t2_space (Πa, β a) := ⟨assume x y h, let ⟨i, hi⟩ := not_forall.mp (mt funext h) in separated_by_f (λz, z i) (infi_le _ i) hi⟩ lemma is_closed_diagonal [t2_space α] : is_closed {p:α×α | p.1 = p.2} := is_closed_iff_nhds.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_ne_bot $ assume : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h $ let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ := by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in begin change t₁ ∈ 𝓝 a₁ at ht₁, change t₂ ∈ 𝓝 a₂ at ht₂, rw [nhds_prod_eq, ←empty_in_sets_eq_bot], apply filter.sets_of_superset, apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)), exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩, show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩ end variables [topological_space β] lemma is_closed_eq [t2_space α] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal lemma diagonal_eq_range_diagonal_map {α : Type*} : {p:α×α | p.1 = p.2} = range (λx, (x,x)) := ext $ assume p, iff.intro (assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩) (assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx) lemma prod_subset_compl_diagonal_iff_disjoint {α : Type*} {s t : set α} : set.prod s t ⊆ - {p:α×α | p.1 = p.2} ↔ s ∩ t = ∅ := by rw [eq_empty_iff_forall_not_mem, subset_compl_comm, diagonal_eq_range_diagonal_map, range_subset_iff]; simp lemma compact_compact_separated [t2_space α] {s t : set α} (hs : compact s) (ht : compact t) (hst : s ∩ t = ∅) : ∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ := by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst; exact generalized_tube_lemma hs ht is_closed_diagonal hst lemma closed_of_compact [t2_space α] (s : set α) (hs : compact s) : is_closed s := is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (compact_singleton : compact {x}) (by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in have v ⊆ -s, from subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)), ⟨v, this, vo, by simpa using xv⟩ lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ compact s) : locally_compact_space α := ⟨assume x n hn, let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in let ⟨k, kx, kc⟩ := h x in -- K is compact but not necessarily contained in N. -- K \ U is again compact and doesn't contain x, so -- we may find open sets V, W separating x from K \ U. -- Then K \ W is a compact neighborhood of x contained in U. let ⟨v, w, vo, wo, xv, kuw, vw⟩ := compact_compact_separated compact_singleton (compact_diff kc uo) (by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in have wn : -w ∈ 𝓝 x, from mem_nhds_sets_iff.mpr ⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩, ⟨k - w, filter.inter_mem_sets kx wn, subset.trans (diff_subset_comm.mp kuw) un, compact_diff kc wo⟩⟩ @[priority 100] -- see Note [lower instance priority] instance locally_compact_of_compact [t2_space α] [compact_space α] : locally_compact_space α := locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩) end separation section regularity section prio set_option default_priority 100 -- see Note [default priority] /-- A T₃ space, also known as a regular space (although this condition sometimes omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist disjoint open sets containing `x` and `C` respectively. -/ class regular_space (α : Type u) [topological_space α] extends t1_space α : Prop := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ 𝓝 a ⊓ principal t = ⊥) end prio lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ 𝓝 a) : ∃t∈(𝓝 a), t ⊆ s ∧ is_closed t := let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in have ∃t, is_open t ∧ -s' ⊆ t ∧ 𝓝 a ⊓ principal t = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨-t, mem_sets_of_eq_bot $ by rwa [compl_compl], subset.trans (compl_subset_comm.1 ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ instance subtype.regular_space [regular_space α] {p : α → Prop} : regular_space (subtype p) := ⟨begin intros s a hs ha, rcases is_closed_induced_iff.1 hs with ⟨s, hs', rfl⟩, rcases regular_space.regular hs' ha with ⟨t, ht, hst, hat⟩, refine ⟨coe ⁻¹' t, is_open_induced ht, preimage_mono hst, _⟩, rw [nhds_induced, ← comap_principal, ← comap_inf, hat, comap_bot] end⟩ variable (α) @[priority 100] -- see Note [lower instance priority] instance regular_space.t2_space [regular_space α] : t2_space α := ⟨λ x y hxy, let ⟨s, hs, hys, hxs⟩ := regular_space.regular is_closed_singleton (mt mem_singleton_iff.1 hxy), ⟨t, hxt, u, hsu, htu⟩ := empty_in_sets_eq_bot.2 hxs, ⟨v, hvt, hv, hxv⟩ := mem_nhds_sets_iff.1 hxt in ⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys, eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, htu ⟨hvt hzv, hsu hzs⟩⟩⟩ end regularity section normality section prio set_option default_priority 100 -- see Note [default priority] /-- A T₄ space, also known as a normal space (although this condition sometimes omits T₂), is one in which for every pair of disjoint closed sets `C` and `D`, there exist disjoint open sets containing `C` and `D` respectively. -/ class normal_space (α : Type u) [topological_space α] extends t1_space α : Prop := (normal : ∀ s t : set α, is_closed s → is_closed t → disjoint s t → ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v) end prio theorem normal_separation [normal_space α] (s t : set α) (H1 : is_closed s) (H2 : is_closed t) (H3 : disjoint s t) : ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v := normal_space.normal s t H1 H2 H3 @[priority 100] -- see Note [lower instance priority] instance normal_space.regular_space [normal_space α] : regular_space α := { regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ := normal_separation s {x} hs is_closed_singleton (λ _ ⟨hx, hy⟩, hxs $ set.mem_of_eq_of_mem (set.eq_of_mem_singleton hy).symm hx) in ⟨u, hu, hsu, filter.empty_in_sets_eq_bot.1 $ filter.mem_inf_sets.2 ⟨v, mem_nhds_sets hv (set.singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, set.inter_comm u v ▸ huv⟩⟩ } -- We can't make this an instance because it could cause an instance loop. lemma normal_of_compact_t2 [compact_space α] [t2_space α] : normal_space α := begin refine ⟨assume s t hs ht st, _⟩, simp only [disjoint_iff], exact compact_compact_separated hs.compact ht.compact st.eq_bot end end normality
dcc8112c74dc27a86dfa7a7fe55c8dbb089a81a2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/homology/short_exact/preadditive.lean
aea22167cbedc24b204c1dc676edaa975fdaacdf
[ "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
10,607
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Andrew Yang -/ import algebra.homology.exact import category_theory.preadditive.additive_functor /-! # Short exact sequences, and splittings. `short_exact f g` is the proposition that `0 ⟶ A -f⟶ B -g⟶ C ⟶ 0` is an exact sequence. We define when a short exact sequence is left-split, right-split, and split. ## See also In `algebra.homology.short_exact.abelian` we show that in an abelian category a left-split short exact sequences admits a splitting. -/ noncomputable theory open category_theory category_theory.limits category_theory.preadditive variables {𝒜 : Type*} [category 𝒜] namespace category_theory variables {A B C A' B' C' : 𝒜} (f : A ⟶ B) (g : B ⟶ C) (f' : A' ⟶ B') (g' : B' ⟶ C') section has_zero_morphisms variables [has_zero_morphisms 𝒜] [has_kernels 𝒜] [has_images 𝒜] /-- If `f : A ⟶ B` and `g : B ⟶ C` then `short_exact f g` is the proposition saying the resulting diagram `0 ⟶ A ⟶ B ⟶ C ⟶ 0` is an exact sequence. -/ structure short_exact : Prop := [mono : mono f] [epi : epi g] (exact : exact f g) /-- An exact sequence `A -f⟶ B -g⟶ C` is *left split* if there exists a morphism `φ : B ⟶ A` such that `f ≫ φ = 𝟙 A` and `g` is epi. Such a sequence is automatically short exact (i.e., `f` is mono). -/ structure left_split : Prop := (left_split : ∃ φ : B ⟶ A, f ≫ φ = 𝟙 A) [epi : epi g] (exact : exact f g) lemma left_split.short_exact {f : A ⟶ B} {g : B ⟶ C} (h : left_split f g) : short_exact f g := { mono := begin obtain ⟨φ, hφ⟩ := h.left_split, haveI : mono (f ≫ φ) := by { rw hφ, apply_instance }, exact mono_of_mono f φ, end, epi := h.epi, exact := h.exact } /-- An exact sequence `A -f⟶ B -g⟶ C` is *right split* if there exists a morphism `φ : C ⟶ B` such that `f ≫ φ = 𝟙 A` and `f` is mono. Such a sequence is automatically short exact (i.e., `g` is epi). -/ structure right_split : Prop := (right_split : ∃ χ : C ⟶ B, χ ≫ g = 𝟙 C) [mono : mono f] (exact : exact f g) lemma right_split.short_exact {f : A ⟶ B} {g : B ⟶ C} (h : right_split f g) : short_exact f g := { epi := begin obtain ⟨χ, hχ⟩ := h.right_split, haveI : epi (χ ≫ g) := by { rw hχ, apply_instance }, exact epi_of_epi χ g, end, mono := h.mono, exact := h.exact } end has_zero_morphisms section preadditive variables [preadditive 𝒜] /-- An exact sequence `A -f⟶ B -g⟶ C` is *split* if there exist `φ : B ⟶ A` and `χ : C ⟶ B` such that: * `f ≫ φ = 𝟙 A` * `χ ≫ g = 𝟙 C` * `f ≫ g = 0` * `χ ≫ φ = 0` * `φ ≫ f + g ≫ χ = 𝟙 B` Such a sequence is automatically short exact (i.e., `f` is mono and `g` is epi). -/ structure split : Prop := (split : ∃ (φ : B ⟶ A) (χ : C ⟶ B), f ≫ φ = 𝟙 A ∧ χ ≫ g = 𝟙 C ∧ f ≫ g = 0 ∧ χ ≫ φ = 0 ∧ φ ≫ f + g ≫ χ = 𝟙 B) variables [has_kernels 𝒜] [has_images 𝒜] lemma exact_of_split {A B C : 𝒜} {f : A ⟶ B} {g : B ⟶ C} {χ : C ⟶ B} {φ : B ⟶ A} (hfg : f ≫ g = 0) (H : φ ≫ f + g ≫ χ = 𝟙 B) : exact f g := { w := hfg, epi := begin let ψ : (kernel_subobject g : 𝒜) ⟶ image_subobject f := subobject.arrow _ ≫ φ ≫ factor_thru_image_subobject f, suffices : ψ ≫ image_to_kernel f g hfg = 𝟙 _, { convert epi_of_epi ψ _, rw this, apply_instance }, rw ← cancel_mono (subobject.arrow _), swap, { apply_instance }, simp only [image_to_kernel_arrow, image_subobject_arrow_comp, category.id_comp, category.assoc], calc (kernel_subobject g).arrow ≫ φ ≫ f = (kernel_subobject g).arrow ≫ 𝟙 B : _ ... = (kernel_subobject g).arrow : category.comp_id _, rw [← H, preadditive.comp_add], simp only [add_zero, zero_comp, kernel_subobject_arrow_comp_assoc], end } section variables {f g} lemma split.exact (h : split f g) : exact f g := by { obtain ⟨φ, χ, -, -, h1, -, h2⟩ := h, exact exact_of_split h1 h2 } lemma split.left_split (h : split f g) : left_split f g := { left_split := by { obtain ⟨φ, χ, h1, -⟩ := h, exact ⟨φ, h1⟩, }, epi := begin obtain ⟨φ, χ, -, h2, -⟩ := h, have : epi (χ ≫ g), { rw h2, apply_instance }, exactI epi_of_epi χ g, end, exact := h.exact } lemma split.right_split (h : split f g) : right_split f g := { right_split := by { obtain ⟨φ, χ, -, h1, -⟩ := h, exact ⟨χ, h1⟩, }, mono := begin obtain ⟨φ, χ, h1, -⟩ := h, have : mono (f ≫ φ), { rw h1, apply_instance }, exactI mono_of_mono f φ, end, exact := h.exact } lemma split.short_exact (h : split f g) : short_exact f g := h.left_split.short_exact end lemma split.map {𝒜 ℬ : Type*} [category 𝒜] [preadditive 𝒜] [category ℬ] [preadditive ℬ] (F : 𝒜 ⥤ ℬ) [functor.additive F] {A B C : 𝒜} {f : A ⟶ B} {g : B ⟶ C} (h : split f g) : split (F.map f) (F.map g) := begin obtain ⟨φ, χ, h1, h2, h3, h4, h5⟩ := h, refine ⟨⟨F.map φ, F.map χ, _⟩⟩, simp only [← F.map_comp, ← F.map_id, ← F.map_add, F.map_zero, *, eq_self_iff_true, and_true], end /-- The sequence `A ⟶ A ⊞ B ⟶ B` is exact. -/ lemma exact_inl_snd [has_binary_biproducts 𝒜] (A B : 𝒜) : exact (biprod.inl : A ⟶ A ⊞ B) biprod.snd := exact_of_split biprod.inl_snd biprod.total /-- The sequence `B ⟶ A ⊞ B ⟶ A` is exact. -/ lemma exact_inr_fst [has_binary_biproducts 𝒜] (A B : 𝒜) : exact (biprod.inr : B ⟶ A ⊞ B) biprod.fst := exact_of_split biprod.inr_fst ((add_comm _ _).trans biprod.total) end preadditive /-- A *splitting* of a sequence `A -f⟶ B -g⟶ C` is an isomorphism to the short exact sequence `0 ⟶ A ⟶ A ⊞ C ⟶ C ⟶ 0` such that the vertical maps on the left and the right are the identity. -/ @[nolint has_nonempty_instance] structure splitting [has_zero_morphisms 𝒜] [has_binary_biproducts 𝒜] := (iso : B ≅ A ⊞ C) (comp_iso_eq_inl : f ≫ iso.hom = biprod.inl) (iso_comp_snd_eq : iso.hom ≫ biprod.snd = g) variables {f g} namespace splitting section has_zero_morphisms variables [has_zero_morphisms 𝒜] [has_binary_biproducts 𝒜] attribute [simp, reassoc] comp_iso_eq_inl iso_comp_snd_eq variables (h : splitting f g) @[simp, reassoc] lemma inl_comp_iso_eq : biprod.inl ≫ h.iso.inv = f := by rw [iso.comp_inv_eq, h.comp_iso_eq_inl] @[simp, reassoc] lemma iso_comp_eq_snd : h.iso.inv ≫ g = biprod.snd := by rw [iso.inv_comp_eq, h.iso_comp_snd_eq] /-- If `h` is a splitting of `A -f⟶ B -g⟶ C`, then `h.section : C ⟶ B` is the morphism satisfying `h.section ≫ g = 𝟙 C`. -/ def _root_.category_theory.splitting.section : C ⟶ B := biprod.inr ≫ h.iso.inv /-- If `h` is a splitting of `A -f⟶ B -g⟶ C`, then `h.retraction : B ⟶ A` is the morphism satisfying `f ≫ h.retraction = 𝟙 A`. -/ def retraction : B ⟶ A := h.iso.hom ≫ biprod.fst @[simp, reassoc] lemma section_π : h.section ≫ g = 𝟙 C := by { delta splitting.section, simp } @[simp, reassoc] lemma ι_retraction : f ≫ h.retraction = 𝟙 A := by { delta retraction, simp } @[simp, reassoc] lemma section_retraction : h.section ≫ h.retraction = 0 := by { delta splitting.section retraction, simp } /-- The retraction in a splitting is a split mono. -/ protected def split_mono : split_mono f := ⟨h.retraction, by simp⟩ /-- The section in a splitting is a split epi. -/ protected def split_epi : split_epi g := ⟨h.section, by simp⟩ @[simp, reassoc] lemma inr_iso_inv : biprod.inr ≫ h.iso.inv = h.section := rfl @[simp, reassoc] lemma iso_hom_fst : h.iso.hom ≫ biprod.fst = h.retraction := rfl /-- A short exact sequence of the form `X -f⟶ Y -0⟶ Z` where `f` is an iso and `Z` is zero has a splitting. -/ def splitting_of_is_iso_zero {X Y Z : 𝒜} (f : X ⟶ Y) [is_iso f] (hZ : is_zero Z) : splitting f (0 : Y ⟶ Z) := ⟨(as_iso f).symm ≪≫ iso_biprod_zero hZ, by simp [hZ.eq_of_tgt _ 0], by simp⟩ include h protected lemma mono : mono f := begin apply mono_of_mono _ h.retraction, rw h.ι_retraction, apply_instance end protected lemma epi : epi g := begin apply_with (epi_of_epi h.section) { instances := ff }, rw h.section_π, apply_instance end instance : mono h.section := by { delta splitting.section, apply_instance } instance : epi h.retraction := by { delta retraction, apply epi_comp } end has_zero_morphisms section preadditive variables [preadditive 𝒜] [has_binary_biproducts 𝒜] variables (h : splitting f g) lemma split_add : h.retraction ≫ f + g ≫ h.section = 𝟙 _ := begin delta splitting.section retraction, rw [← cancel_mono h.iso.hom, ← cancel_epi h.iso.inv], simp only [category.comp_id, category.id_comp, category.assoc, iso.inv_hom_id_assoc, iso.inv_hom_id, limits.biprod.total, preadditive.comp_add, preadditive.add_comp, splitting.comp_iso_eq_inl, splitting.iso_comp_eq_snd_assoc] end @[reassoc] lemma retraction_ι_eq_id_sub : h.retraction ≫ f = 𝟙 _ - g ≫ h.section := eq_sub_iff_add_eq.mpr h.split_add @[reassoc] lemma π_section_eq_id_sub : g ≫ h.section = 𝟙 _ - h.retraction ≫ f := eq_sub_iff_add_eq.mpr ((add_comm _ _).trans h.split_add) lemma splittings_comm (h h' : splitting f g) : h'.section ≫ h.retraction = - h.section ≫ h'.retraction := begin haveI := h.mono, rw ← cancel_mono f, simp [retraction_ι_eq_id_sub], end include h lemma split : split f g := begin let φ := h.iso.hom ≫ biprod.fst, let χ := biprod.inr ≫ h.iso.inv, refine ⟨⟨h.retraction, h.section, h.ι_retraction, h.section_π, _, h.section_retraction, h.split_add⟩⟩, rw [← h.inl_comp_iso_eq, category.assoc, h.iso_comp_eq_snd, biprod.inl_snd], end @[reassoc] lemma comp_eq_zero : f ≫ g = 0 := h.split.1.some_spec.some_spec.2.2.1 variables [has_kernels 𝒜] [has_images 𝒜] [has_zero_object 𝒜] [has_cokernels 𝒜] protected lemma exact : exact f g := begin rw exact_iff_exact_of_iso f g (biprod.inl : A ⟶ A ⊞ C) (biprod.snd : A ⊞ C ⟶ C) _ _ _, { exact exact_inl_snd _ _ }, { refine arrow.iso_mk (iso.refl _) h.iso _, simp only [iso.refl_hom, arrow.mk_hom, category.id_comp, comp_iso_eq_inl], }, { refine arrow.iso_mk h.iso (iso.refl _) _, dsimp, simp, }, { refl } end protected lemma short_exact : short_exact f g := { mono := h.mono, epi := h.epi, exact := h.exact } end preadditive end splitting end category_theory
e60260b0c940bf4d0d17d3e555cf503ed22d881d
dc253be9829b840f15d96d986e0c13520b085033
/algebra/ring.hlean
97ee0f627d2aadcce8c79f421d4203039ebb1d97
[ "Apache-2.0" ]
permissive
cmu-phil/Spectral
4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea
3b078f5f1de251637decf04bd3fc8aa01930a6b3
refs/heads/master
1,685,119,195,535
1,684,169,772,000
1,684,169,772,000
46,450,197
42
13
null
1,505,516,767,000
1,447,883,921,000
Lean
UTF-8
Lean
false
false
2,780
hlean
-- Authors: Floris van Doorn import algebra.ring .direct_sum ..heq ..move_to_lib open algebra group eq is_trunc sigma namespace algebra definition AddAbGroup_of_Ring [constructor] (R : Ring) : AddAbGroup := AddAbGroup.mk R (add_ab_group_of_ring R) definition AddGroup_of_Ring [constructor] (R : Ring) : AddGroup := AddGroup.mk R (add_group_of_add_ab_group R) definition ring_AddAbGroup_of_Ring [instance] (R : Ring) : ring (AddAbGroup_of_Ring R) := Ring.struct R /- we give the following instance very high priority, otherwise type class inference would sometimes find the additive structure of R as the group structure. -/ definition monoid_AddAbGroup_of_Ring [instance] [priority 3000] [constructor] (R : Ring) : monoid (Group_of_AbGroup (AddAbGroup_of_Ring R)) := @monoid_of_ring _ (Ring.struct R) definition ring_right_action [constructor] {R : Ring} (r : R) : AddAbGroup_of_Ring R →a AddAbGroup_of_Ring R := homomorphism.mk (λs, s * r) (λs s', !right_distrib) definition ring_of_ab_group [constructor] (G : Type) [ab_group G] (m : G → G → G) (o : G) (lm : Πg, m o g = g) (rm : Πg, m g o = g) (am : Πg₁ g₂ g₃, m (m g₁ g₂) g₃ = m g₁ (m g₂ g₃)) (ld : Πg₁ g₂ g₃, m g₁ (g₂ * g₃) = m g₁ g₂ * m g₁ g₃) (rd : Πg₁ g₂ g₃, m (g₁ * g₂) g₃ = m g₁ g₃ * m g₂ g₃) : ring G := ring.mk _ mul mul.assoc 1 one_mul mul_one inv mul.left_inv mul.comm m am o lm rm ld rd definition Ring_of_AbGroup [constructor] (G : AbGroup) (m : G → G → G) (o : G) (lm : Πg, m o g = g) (rm : Πg, m g o = g) (am : Πg₁ g₂ g₃, m (m g₁ g₂) g₃ = m g₁ (m g₂ g₃)) (ld : Πg₁ g₂ g₃, m g₁ (g₂ * g₃) = m g₁ g₂ * m g₁ g₃) (rd : Πg₁ g₂ g₃, m (g₁ * g₂) g₃ = m g₁ g₃ * m g₂ g₃) : Ring := Ring.mk G (ring_of_ab_group G m o lm rm am ld rd) /- graded ring -/ structure graded_ring (M : Monoid) := (R : M → AddAbGroup) (mul : Π⦃m m'⦄, R m → R m' → R (m * m')) (one : R 1) (mul_one : Π⦃m⦄ (r : R m), mul r one ==[R] r) (one_mul : Π⦃m⦄ (r : R m), mul one r ==[R] r) (mul_assoc : Π⦃m₁ m₂ m₃⦄ (r₁ : R m₁) (r₂ : R m₂) (r₃ : R m₃), mul (mul r₁ r₂) r₃ ==[R] mul r₁ (mul r₂ r₃)) (mul_left_distrib : Π⦃m₁ m₂⦄ (r₁ : R m₁) (r₂ r₂' : R m₂), mul r₁ (r₂ + r₂') = mul r₁ r₂ + mul r₁ r₂') (mul_right_distrib : Π⦃m₁ m₂⦄ (r₁ r₁' : R m₁) (r₂ : R m₂), mul (r₁ + r₁') r₂ = mul r₁ r₂ + mul r₁' r₂) attribute graded_ring.R [coercion] infixl ` ** `:71 := graded_ring.mul -- definition ring_direct_sum {M : Monoid} (R : graded_ring M) : Ring := -- Ring_of_AbGroup (dirsum R) _ (dirsum_incl R 1 (graded_ring.one R)) _ _ _ _ _ end algebra
8703560780b797e616df6899ea7bab135df50a56
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/nonsingular_inverse.lean
00a9f981188280c8bc7b107e63b2f75c4f62e512
[ "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
17,358
lean
/- Copyright (c) 2019 Tim Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Tim Baanen. -/ import algebra.associated import linear_algebra.determinant import tactic.linarith import tactic.ring_exp /-! # Nonsingular inverses In this file, we define an inverse for square matrices of invertible determinant. For matrices that are not square or not of full rank, there is a more general notion of pseudoinverses which we do not consider here. The definition of inverse used in this file is the adjugate divided by the determinant. The adjugate is calculated with Cramer's rule, which we introduce first. The vectors returned by Cramer's rule are given by the linear map `cramer`, which sends a matrix `A` and vector `b` to the vector consisting of the determinant of replacing the `i`th column of `A` with `b` at index `i` (written as `(A.update_column i b).det`). Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`. The entries of the adjugate are the determinants of each minor of `A`. Instead of defining a minor to be `A` with row `i` and column `j` deleted, we replace the `i`th row of `A` with the `j`th basis vector; this has the same determinant as the minor but more importantly equals Cramer's rule applied to `A` and the `j`th basis vector, simplifying the subsequent proofs. We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will result in a multiplicative inverse to `A`. ## References * https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix ## Tags matrix inverse, cramer, cramer's rule, adjugate -/ namespace matrix universes u v variables {n : Type u} [decidable_eq n] [fintype n] {α : Type v} [comm_ring α] open_locale matrix big_operators open equiv equiv.perm finset section cramer /-! ### `cramer` section Introduce the linear map `cramer` with values defined by `cramer_map`. After defining `cramer_map` and showing it is linear, we will restrict our proofs to using `cramer`. -/ variables (A : matrix n n α) (b : n → α) /-- `cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer_map A` sends the vector `b` to `A.det • x`. Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful. -/ def cramer_map (i : n) : α := (A.update_column i b).det lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) := begin have : Π {f : n → n} {i : n} (x : n → α), (∏ i' : n, (update_column A i x) (f i') i') = (∏ i' : n, if i' = i then x (f i') else A (f i') i'), { intros, congr' with i', apply update_column_apply, }, split, { intros x y, repeat { rw [cramer_map, det] }, rw [←sum_add_distrib], congr' with σ, rw [←mul_add ↑↑(sign σ)], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, prod_singleton, ←add_mul], refl }, { intros c x, repeat { rw [cramer_map, det] }, rw [smul_eq_mul, mul_sum], congr' with σ, rw [←mul_assoc, mul_comm c, mul_assoc], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, mul_assoc], } end lemma cramer_is_linear : is_linear_map α (cramer_map A) := begin split; intros; ext i, { apply (cramer_map_is_linear A i).1 }, { apply (cramer_map_is_linear A i).2 } end /-- `cramer A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer A` sends the vector `b` to `A.det • x`. Otherwise, the outcome of `cramer` is well-defined but not necessarily useful. -/ def cramer (A : matrix n n α) : (n → α) →ₗ[α] (n → α) := is_linear_map.mk' (cramer_map A) (cramer_is_linear A) lemma cramer_apply (i : n) : cramer A b i = (A.update_column i b).det := rfl lemma cramer_transpose_row_self (i : n) : Aᵀ.cramer (A i) = λ j, ite (i = j) A.det 0 := begin ext j, rw cramer_apply, by_cases h : i = j, { -- i = j: this entry should be `A.det` rw [update_column_transpose, det_transpose], simp [update_row, h], }, { -- i ≠ j: this entry should be 0 rw [if_neg h, update_column_transpose, det_transpose], apply det_zero_of_row_eq h, rw [update_row_self, update_row_ne], apply h } end /-- Use linearity of `cramer` to take it out of a summation. -/ lemma sum_cramer {β} (s : finset β) (f : β → n → α) : ∑ x in s, cramer A (f x) = cramer A (∑ x in s, f x) := (linear_map.map_sum (cramer A)).symm /-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/ lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) : ∑ x in s, cramer A (λ j, f j x) i = cramer A (λ (j : n), ∑ x in s, f j x) i := calc ∑ x in s, cramer A (λ j, f j x) i = (∑ x in s, cramer A (λ j, f j x)) i : (finset.sum_apply i s _).symm ... = cramer A (λ (j : n), ∑ x in s, f j x) i : by { rw [sum_cramer, cramer_apply], congr' with j, apply finset.sum_apply } end cramer section adjugate /-! ### `adjugate` section Define the `adjugate` matrix and a few equations. These will hold for any matrix over a commutative ring, while the `inv` section is specifically for invertible matrices. -/ /-- The adjugate matrix is the transpose of the cofactor matrix. Typically, the cofactor matrix is defined by taking the determinant of minors, i.e. the matrix with a row and column removed. However, the proof of `mul_adjugate` becomes a lot easier if we define the minor as replacing a column with a basis vector, since it allows us to use facts about the `cramer` map. -/ def adjugate (A : matrix n n α) : matrix n n α := λ i, cramer Aᵀ (λ j, if i = j then 1 else 0) lemma adjugate_def (A : matrix n n α) : adjugate A = λ i, cramer Aᵀ (λ j, if i = j then 1 else 0) := rfl lemma adjugate_apply (A : matrix n n α) (i j : n) : adjugate A i j = (A.update_row j (λ j, if i = j then 1 else 0)).det := by { rw adjugate_def, simp only, rw [cramer_apply, update_column_transpose, det_transpose], } lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) := begin ext i j, rw [transpose_apply, adjugate_apply, adjugate_apply, update_row_transpose, det_transpose], apply finset.sum_congr rfl, intros σ _, congr' 1, by_cases i = σ j, { -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`. congr; ext j', have := (@equiv.injective _ _ σ j j' : σ j = σ j' → j = j'), rw [update_row_apply, update_column_apply], finish }, { -- Otherwise, we need to show that there is a `0` somewhere in the product. have : (∏ j' : n, update_column A j (λ (i' : n), ite (i = i') 1 0) (σ j') j') = 0, { apply prod_eq_zero (mem_univ j), rw [update_column_self], exact if_neg h }, rw this, apply prod_eq_zero (mem_univ (σ⁻¹ i)), erw [apply_symm_apply σ i, update_row_self], apply if_neg, intro h', exact h ((symm_apply_eq σ).mp h'.symm) } end /-- Since the map `b ↦ cramer A b` is linear in `b`, it must be multiplication by some matrix. This matrix is `A.adjugate`. -/ lemma cramer_eq_adjugate_mul_vec (A : matrix n n α) (b : n → α) : cramer A b = A.adjugate.mul_vec b := begin nth_rewrite 1 ← A.transpose_transpose, rw [← adjugate_transpose, adjugate_def], have : b = ∑ i, (b i) • (λ j, if i = j then 1 else 0), { ext i, simp, }, rw this, ext k, simp [mul_vec, dot_product, mul_comm], end lemma mul_adjugate_apply (A : matrix n n α) (i j k) : A i k * adjugate A k j = cramer Aᵀ (λ j, if k = j then A i k else 0) j := begin erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul], congr' with l, rw [pi.smul_apply, smul_eq_mul, mul_boole], end lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 := begin ext i j, rw [mul_apply, smul_apply, one_apply, mul_boole], simp [mul_adjugate_apply, sum_cramer_apply, cramer_transpose_row_self], end lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 := calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ : by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose] ... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one] /-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`, used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels on the left of the equation `A.det * b = A.det ^ n`. -/ lemma det_adjugate_of_cancel {A : matrix n n α} (h : ∀ b, A.det * b = A.det ^ fintype.card n → b = A.det ^ (fintype.card n - 1)) : (adjugate A).det = A.det ^ (fintype.card n - 1) := h (adjugate A).det (calc A.det * (adjugate A).det = (A ⬝ adjugate A).det : (det_mul _ _).symm ... = A.det ^ fintype.card n : by simp [mul_adjugate]) lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 := begin ext i j, have univ_eq_i := univ_eq_singleton_of_card_one i h, have univ_eq_j := univ_eq_singleton_of_card_one j h, have i_eq_j : i = j := singleton_inj.mp (by rw [←univ_eq_i, univ_eq_j]), have perm_eq : (univ : finset (perm n)) = {1} := univ_eq_singleton_of_card_one (1 : perm n) (by simp [card_univ, fintype.card_perm, h]), simp [adjugate_apply, det, univ_eq_i, perm_eq, i_eq_j] end @[simp] lemma adjugate_zero (h : 1 < fintype.card n) : adjugate (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := fintype.exists_ne_of_one_lt_card h j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'], end lemma det_adjugate_eq_one {A : matrix n n α} (h : A.det = 1) : (adjugate A).det = 1 := calc (adjugate A).det = A.det ^ (fintype.card n - 1) : det_adjugate_of_cancel (λ b hb, by simpa [h] using hb) ... = 1 : by rw [h, one_pow] /-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse. -/ lemma det_adjugate_of_is_unit {A : matrix n n α} (h : is_unit A.det) : (adjugate A).det = A.det ^ (fintype.card n - 1) := begin rcases is_unit_iff_exists_inv'.mp h with ⟨a, ha⟩, by_cases card_lt_zero : fintype.card n ≤ 0, { have h : fintype.card n = 0 := by linarith, simp [det_eq_one_of_card_eq_zero h] }, have zero_lt_card : 0 < fintype.card n := by linarith, have n_nonempty : nonempty n := fintype.card_pos_iff.mp zero_lt_card, by_cases card_lt_one : fintype.card n ≤ 1, { have h : fintype.card n = 1 := by linarith, simp [h, adjugate_eq_one_of_card_eq_one h] }, have one_lt_card : 1 < fintype.card n := by linarith, have zero_lt_card_sub_one : 0 < fintype.card n - 1 := (nat.sub_lt_sub_right_iff (refl 1)).mpr one_lt_card, apply det_adjugate_of_cancel, intros b hb, calc b = a * (det A ^ (fintype.card n - 1 + 1)) : by rw [←one_mul b, ←ha, mul_assoc, hb, nat.sub_add_cancel zero_lt_card] ... = a * det A * det A ^ (fintype.card n - 1) : by ring_exp ... = det A ^ (fintype.card n - 1) : by rw [ha, one_mul] end end adjugate section inv /-! ### `inv` section Defines the matrix `nonsing_inv A` and proves it is the inverse matrix of a square matrix `A` as long as `det A` has a multiplicative inverse. -/ variables (A : matrix n n α) open_locale classical lemma is_unit_det_transpose (h : is_unit A.det) : is_unit Aᵀ.det := by { rw det_transpose, exact h, } /-- The inverse of a square matrix, when it is invertible (and zero otherwise).-/ noncomputable def nonsing_inv : matrix n n α := if h : is_unit A.det then (↑h.unit⁻¹ : α) • A.adjugate else 0 noncomputable instance : has_inv (matrix n n α) := ⟨matrix.nonsing_inv⟩ lemma nonsing_inv_apply (h : is_unit A.det) : A⁻¹ = (↑h.unit⁻¹ : α) • A.adjugate := by { change A.nonsing_inv = _, dunfold nonsing_inv, simp only [dif_pos, h], } lemma transpose_nonsing_inv (h : is_unit A.det) : (A⁻¹)ᵀ = (Aᵀ)⁻¹ := begin have h' := A.is_unit_det_transpose h, have dets_eq : (↑h.unit : α) = ↑h'.unit := by rw [h.unit_spec, h'.unit_spec, det_transpose], rw [A.nonsing_inv_apply h, Aᵀ.nonsing_inv_apply h', units.inv_unique dets_eq, A.adjugate_transpose.symm], refl, end /-- The `nonsing_inv` of `A` is a right inverse. -/ @[simp] lemma mul_nonsing_inv (h : is_unit A.det) : A ⬝ A⁻¹ = 1 := by rw [A.nonsing_inv_apply h, mul_smul, mul_adjugate, smul_smul, units.inv_mul_of_eq h.unit_spec, one_smul] /-- The `nonsing_inv` of `A` is a left inverse. -/ @[simp] lemma nonsing_inv_mul (h : is_unit A.det) : A⁻¹ ⬝ A = 1 := calc A⁻¹ ⬝ A = (Aᵀ ⬝ (Aᵀ)⁻¹)ᵀ : by { rw [transpose_mul, Aᵀ.transpose_nonsing_inv (A.is_unit_det_transpose h), transpose_transpose], } ... = 1ᵀ : by { rw Aᵀ.mul_nonsing_inv, exact A.is_unit_det_transpose h, } ... = 1 : transpose_one @[simp] lemma nonsing_inv_det (h : is_unit A.det) : A⁻¹.det * A.det = 1 := by rw [←det_mul, A.nonsing_inv_mul h, det_one] lemma is_unit_nonsing_inv_det (h : is_unit A.det) : is_unit A⁻¹.det := is_unit_of_mul_eq_one _ _ (A.nonsing_inv_det h) @[simp] lemma nonsing_inv_nonsing_inv (h : is_unit A.det) : (A⁻¹)⁻¹ = A := calc (A⁻¹)⁻¹ = 1 ⬝ (A⁻¹)⁻¹ : by rw matrix.one_mul ... = A ⬝ A⁻¹ ⬝ (A⁻¹)⁻¹ : by rw A.mul_nonsing_inv h ... = A : by { rw [matrix.mul_assoc, (A⁻¹).mul_nonsing_inv (A.is_unit_nonsing_inv_det h), matrix.mul_one], } /-- A matrix whose determinant is a unit is itself a unit. -/ noncomputable def nonsing_inv_unit (h : is_unit A.det) : units (matrix n n α) := { val := A, inv := A⁻¹, val_inv := by { rw matrix.mul_eq_mul, apply A.mul_nonsing_inv h, }, inv_val := by { rw matrix.mul_eq_mul, apply A.nonsing_inv_mul h, } } lemma is_unit_iff_is_unit_det : is_unit A ↔ is_unit A.det := begin split; intros h, { -- is_unit A → is_unit A.det suffices : ∃ (B : matrix n n α), A ⬝ B = 1, { rcases this with ⟨B, hB⟩, apply is_unit_of_mul_eq_one _ B.det, rw [←det_mul, hB, det_one], }, refine ⟨↑h.unit⁻¹, _⟩, conv_lhs { congr, rw ←h.unit_spec, }, exact h.unit.mul_inv, }, { -- is_unit A.det → is_unit A exact is_unit_unit (A.nonsing_inv_unit h), }, end lemma is_unit_det_of_left_inverse (B : matrix n n α) (h : B ⬝ A = 1) : is_unit A.det := ⟨{ val := A.det, inv := B.det, val_inv := by rw [mul_comm, ← det_mul, h, det_one], inv_val := by rw [← det_mul, h, det_one], }, rfl⟩ lemma is_unit_det_of_right_inverse (B : matrix n n α) (h : A ⬝ B = 1) : is_unit A.det := ⟨{ val := A.det, inv := B.det, val_inv := by rw [← det_mul, h, det_one], inv_val := by rw [mul_comm, ← det_mul, h, det_one], }, rfl⟩ lemma nonsing_inv_left_right (B : matrix n n α) (h : A ⬝ B = 1) : B ⬝ A = 1 := begin have h' : is_unit B.det := B.is_unit_det_of_left_inverse A h, calc B ⬝ A = (B ⬝ A) ⬝ (B ⬝ B⁻¹) : by simp only [h', matrix.mul_one, mul_nonsing_inv] ... = B ⬝ ((A ⬝ B) ⬝ B⁻¹) : by simp only [matrix.mul_assoc] ... = B ⬝ B⁻¹ : by simp only [h, matrix.one_mul] ... = 1 : mul_nonsing_inv B h', end lemma nonsing_inv_right_left (B : matrix n n α) (h : B ⬝ A = 1) : A ⬝ B = 1 := B.nonsing_inv_left_right A h end inv /- One form of Cramer's rule. -/ @[simp] lemma det_smul_inv_mul_vec_eq_cramer (A : matrix n n α) (b : n → α) (h : is_unit A.det) : A.det • A⁻¹.mul_vec b = cramer A b := begin rw [cramer_eq_adjugate_mul_vec, A.nonsing_inv_apply h, ← smul_mul_vec_assoc], conv_lhs { congr, congr, rw ← h.unit_spec, }, rw units.smul_inv_smul, end /- A stronger form of Cramer's rule that allows us to solve some instances of `A ⬝ x = b` even if the determinant is not a unit. A sufficient (but still not necessary) condition is that `A.det` divides `b`. -/ @[simp] lemma mul_vec_cramer (A : matrix n n α) (b : n → α) : A.mul_vec (cramer A b) = A.det • b := by rw [cramer_eq_adjugate_mul_vec, mul_vec_mul_vec, mul_adjugate, smul_mul_vec_assoc, mul_vec_one] end matrix
80ffaa2d13ef3f4f054b4a913ca329fe76b3d892
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/integral/set_integral.lean
a5f595292c4fb38bbd231703c7cf751f0868a19d
[ "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
48,879
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import measure_theory.integral.integrable_on import measure_theory.integral.bochner import order.filter.indicator_function import topology.metric_space.thickened_indicator /-! # Set integral In this file we prove some properties of `∫ x in s, f x ∂μ`. Recall that this notation is defined as `∫ x, f x ∂(μ.restrict s)`. In `integral_indicator` we prove that for a measurable function `f` and a measurable set `s` this definition coincides with another natural definition: `∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ`, where `indicator s f x` is equal to `f x` for `x ∈ s` and is zero otherwise. Since `∫ x in s, f x ∂μ` is a notation, one can rewrite or apply any theorem about `∫ x, f x ∂μ` directly. In this file we prove some theorems about dependence of `∫ x in s, f x ∂μ` on `s`, e.g. `integral_union`, `integral_empty`, `integral_univ`. We use the property `integrable_on f s μ := integrable f (μ.restrict s)`, defined in `measure_theory.integrable_on`. We also defined in that same file a predicate `integrable_at_filter (f : α → E) (l : filter α) (μ : measure α)` saying that `f` is integrable at some set `s ∈ l`. Finally, we prove a version of the [Fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus) for set integral, see `filter.tendsto.integral_sub_linear_is_o_ae` and its corollaries. Namely, consider a measurably generated filter `l`, a measure `μ` finite at this filter, and a function `f` that has a finite limit `c` at `l ⊓ μ.ae`. Then `∫ x in s, f x ∂μ = μ s • c + o(μ s)` as `s` tends to `l.small_sets`, i.e. for any `ε>0` there exists `t ∈ l` such that `∥∫ x in s, f x ∂μ - μ s • c∥ ≤ ε * μ s` whenever `s ⊆ t`. We also formulate a version of this theorem for a locally finite measure `μ` and a function `f` continuous at a point `a`. ## Notation We provide the following notations for expressing the integral of a function on a set : * `∫ a in s, f a ∂μ` is `measure_theory.integral (μ.restrict s) f` * `∫ a in s, f a` is `∫ a in s, f a ∂volume` Note that the set notations are defined in the file `measure_theory/integral/bochner`, but we reference them here because all theorems about set integrals are in this file. -/ noncomputable theory open set filter topological_space measure_theory function open_locale classical topological_space interval big_operators filter ennreal nnreal measure_theory variables {α β E F : Type*} [measurable_space α] namespace measure_theory section normed_group variables [normed_group E] {f g : α → E} {s t : set α} {μ ν : measure α} {l l' : filter α} variables [complete_space E] [normed_space ℝ E] lemma set_integral_congr_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x = g x) : ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ := integral_congr_ae ((ae_restrict_iff' hs).2 h) lemma set_integral_congr (hs : measurable_set s) (h : eq_on f g s) : ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ := set_integral_congr_ae hs $ eventually_of_forall h lemma set_integral_congr_set_ae (hst : s =ᵐ[μ] t) : ∫ x in s, f x ∂μ = ∫ x in t, f x ∂μ := by rw measure.restrict_congr_set hst lemma integral_union_ae (hst : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ := by simp only [integrable_on, measure.restrict_union₀ hst ht, integral_add_measure hfs hft] lemma integral_union (hst : disjoint s t) (ht : measurable_set t) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ := integral_union_ae hst.ae_disjoint ht.null_measurable_set hfs hft lemma integral_diff (ht : measurable_set t) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) (hts : t ⊆ s) : ∫ x in s \ t, f x ∂μ = ∫ x in s, f x ∂μ - ∫ x in t, f x ∂μ := begin rw [eq_sub_iff_add_eq, ← integral_union, diff_union_of_subset hts], exacts [disjoint_diff.symm, ht, hfs.mono_set (diff_subset _ _), hft] end lemma integral_finset_bUnion {ι : Type*} (t : finset ι) {s : ι → set α} (hs : ∀ i ∈ t, measurable_set (s i)) (h's : set.pairwise ↑t (disjoint on s)) (hf : ∀ i ∈ t, integrable_on f (s i) μ) : ∫ x in (⋃ i ∈ t, s i), f x ∂ μ = ∑ i in t, ∫ x in s i, f x ∂ μ := begin induction t using finset.induction_on with a t hat IH hs h's, { simp }, { simp only [finset.coe_insert, finset.forall_mem_insert, set.pairwise_insert, finset.set_bUnion_insert] at hs hf h's ⊢, rw [integral_union _ _ hf.1 (integrable_on_finset_Union.2 hf.2)], { rw [finset.sum_insert hat, IH hs.2 h's.1 hf.2] }, { simp only [disjoint_Union_right], exact (λ i hi, (h's.2 i hi (ne_of_mem_of_not_mem hi hat).symm).1) }, { exact finset.measurable_set_bUnion _ hs.2 } } end lemma integral_fintype_Union {ι : Type*} [fintype ι] {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (h's : pairwise (disjoint on s)) (hf : ∀ i, integrable_on f (s i) μ) : ∫ x in (⋃ i, s i), f x ∂ μ = ∑ i, ∫ x in s i, f x ∂ μ := begin convert integral_finset_bUnion finset.univ (λ i hi, hs i) _ (λ i _, hf i), { simp }, { simp [pairwise_univ, h's] } end lemma integral_empty : ∫ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, integral_zero_measure] lemma integral_univ : ∫ x in univ, f x ∂μ = ∫ x, f x ∂μ := by rw [measure.restrict_univ] lemma integral_add_compl (hs : measurable_set s) (hfi : integrable f μ) : ∫ x in s, f x ∂μ + ∫ x in sᶜ, f x ∂μ = ∫ x, f x ∂μ := by rw [← integral_union (@disjoint_compl_right (set α) _ _) hs.compl hfi.integrable_on hfi.integrable_on, union_compl_self, integral_univ] /-- For a function `f` and a measurable set `s`, the integral of `indicator s f` over the whole space is equal to `∫ x in s, f x ∂μ` defined as `∫ x, f x ∂(μ.restrict s)`. -/ lemma integral_indicator (hs : measurable_set s) : ∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ := begin by_cases hfi : integrable_on f s μ, swap, { rwa [integral_undef, integral_undef], rwa integrable_indicator_iff hs }, calc ∫ x, indicator s f x ∂μ = ∫ x in s, indicator s f x ∂μ + ∫ x in sᶜ, indicator s f x ∂μ : (integral_add_compl hs (hfi.indicator hs)).symm ... = ∫ x in s, f x ∂μ + ∫ x in sᶜ, 0 ∂μ : congr_arg2 (+) (integral_congr_ae (indicator_ae_eq_restrict hs)) (integral_congr_ae (indicator_ae_eq_restrict_compl hs)) ... = ∫ x in s, f x ∂μ : by simp end lemma set_integral_indicator (ht : measurable_set t) : ∫ x in s, t.indicator f x ∂μ = ∫ x in s ∩ t, f x ∂μ := by rw [integral_indicator ht, measure.restrict_restrict ht, set.inter_comm] lemma of_real_set_integral_one_of_measure_ne_top {α : Type*} {m : measurable_space α} {μ : measure α} {s : set α} (hs : μ s ≠ ∞) : ennreal.of_real (∫ x in s, (1 : ℝ) ∂μ) = μ s := calc ennreal.of_real (∫ x in s, (1 : ℝ) ∂μ) = ennreal.of_real (∫ x in s, ∥(1 : ℝ)∥ ∂μ) : by simp only [norm_one] ... = ∫⁻ x in s, 1 ∂μ : begin rw of_real_integral_norm_eq_lintegral_nnnorm (integrable_on_const.2 (or.inr hs.lt_top)), simp only [nnnorm_one, ennreal.coe_one], end ... = μ s : set_lintegral_one _ lemma of_real_set_integral_one {α : Type*} {m : measurable_space α} (μ : measure α) [is_finite_measure μ] (s : set α) : ennreal.of_real (∫ x in s, (1 : ℝ) ∂μ) = μ s := of_real_set_integral_one_of_measure_ne_top (measure_ne_top μ s) lemma integral_piecewise [decidable_pred (∈ s)] (hs : measurable_set s) {f g : α → E} (hf : integrable_on f s μ) (hg : integrable_on g sᶜ μ) : ∫ x, s.piecewise f g x ∂μ = ∫ x in s, f x ∂μ + ∫ x in sᶜ, g x ∂μ := by rw [← set.indicator_add_compl_eq_piecewise, integral_add' (hf.indicator hs) (hg.indicator hs.compl), integral_indicator hs, integral_indicator hs.compl] lemma tendsto_set_integral_of_monotone {ι : Type*} [encodable ι] [semilattice_sup ι] {s : ι → set α} {f : α → E} (hsm : ∀ i, measurable_set (s i)) (h_mono : monotone s) (hfi : integrable_on f (⋃ n, s n) μ) : tendsto (λ i, ∫ a in s i, f a ∂μ) at_top (𝓝 (∫ a in (⋃ n, s n), f a ∂μ)) := begin have hfi' : ∫⁻ x in ⋃ n, s n, ∥f x∥₊ ∂μ < ∞ := hfi.2, set S := ⋃ i, s i, have hSm : measurable_set S := measurable_set.Union hsm, have hsub : ∀ {i}, s i ⊆ S, from subset_Union s, rw [← with_density_apply _ hSm] at hfi', set ν := μ.with_density (λ x, ∥f x∥₊) with hν, refine metric.nhds_basis_closed_ball.tendsto_right_iff.2 (λ ε ε0, _), lift ε to ℝ≥0 using ε0.le, have : ∀ᶠ i in at_top, ν (s i) ∈ Icc (ν S - ε) (ν S + ε), from tendsto_measure_Union h_mono (ennreal.Icc_mem_nhds hfi'.ne (ennreal.coe_pos.2 ε0).ne'), refine this.mono (λ i hi, _), rw [mem_closed_ball_iff_norm', ← integral_diff (hsm i) hfi (hfi.mono_set hsub) hsub, ← coe_nnnorm, nnreal.coe_le_coe, ← ennreal.coe_le_coe], refine (ennnorm_integral_le_lintegral_ennnorm _).trans _, rw [← with_density_apply _ (hSm.diff (hsm _)), ← hν, measure_diff hsub (hsm _)], exacts [tsub_le_iff_tsub_le.mp hi.1, (hi.2.trans_lt $ ennreal.add_lt_top.2 ⟨hfi', ennreal.coe_lt_top⟩).ne] end lemma has_sum_integral_Union_ae {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s)) (hfi : integrable_on f (⋃ i, s i) μ) : has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) := begin simp only [integrable_on, measure.restrict_Union_ae hd hm] at hfi ⊢, exact has_sum_integral_measure hfi end lemma has_sum_integral_Union {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (hfi : integrable_on f (⋃ i, s i) μ) : has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) := has_sum_integral_Union_ae (λ i, (hm i).null_measurable_set) (hd.mono (λ i j h, h.ae_disjoint)) hfi lemma integral_Union {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (hfi : integrable_on f (⋃ i, s i) μ) : (∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ := (has_sum.tsum_eq (has_sum_integral_Union hm hd hfi)).symm lemma integral_Union_ae {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s)) (hfi : integrable_on f (⋃ i, s i) μ) : (∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ := (has_sum.tsum_eq (has_sum_integral_Union_ae hm hd hfi)).symm lemma set_integral_eq_zero_of_forall_eq_zero {f : α → E} (hf : strongly_measurable f) (ht_eq : ∀ x ∈ t, f x = 0) : ∫ x in t, f x ∂μ = 0 := begin refine integral_eq_zero_of_ae _, rw [eventually_eq, ae_restrict_iff (hf.measurable_set_eq_fun strongly_measurable_zero)], refine eventually_of_forall (λ x hx, _), rw pi.zero_apply, exact ht_eq x hx, end lemma set_integral_union_eq_left {f : α → E} (hf : strongly_measurable f) (hfi : integrable f μ) (hs : measurable_set s) (ht_eq : ∀ x ∈ t, f x = 0) : ∫ x in (s ∪ t), f x ∂μ = ∫ x in s, f x ∂μ := begin rw [← set.union_diff_self, union_comm, integral_union, set_integral_eq_zero_of_forall_eq_zero _ (λ x hx, ht_eq x (diff_subset _ _ hx)), zero_add], exacts [hf, disjoint_diff.symm, hs, hfi.integrable_on, hfi.integrable_on] end lemma set_integral_neg_eq_set_integral_nonpos [linear_order E] [order_closed_topology E] {f : α → E} (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x in {x | f x < 0}, f x ∂μ = ∫ x in {x | f x ≤ 0}, f x ∂μ := begin have h_union : {x | f x ≤ 0} = {x | f x < 0} ∪ {x | f x = 0}, by { ext, simp_rw [set.mem_union_eq, set.mem_set_of_eq], exact le_iff_lt_or_eq, }, rw h_union, exact (set_integral_union_eq_left hf hfi (hf.measurable_set_lt strongly_measurable_const) (λ x hx, hx)).symm, end lemma integral_norm_eq_pos_sub_neg {f : α → ℝ} (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ := have h_meas : measurable_set {x | 0 ≤ f x}, from strongly_measurable_const.measurable_set_le hf, calc ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, ∥f x∥ ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ : by rw ← integral_add_compl h_meas hfi.norm ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ : begin congr' 1, refine set_integral_congr h_meas (λ x hx, _), dsimp only, rw [real.norm_eq_abs, abs_eq_self.mpr _], exact hx, end ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | 0 ≤ f x}ᶜ, f x ∂μ : begin congr' 1, rw ← integral_neg, refine set_integral_congr h_meas.compl (λ x hx, _), dsimp only, rw [real.norm_eq_abs, abs_eq_neg_self.mpr _], rw [set.mem_compl_iff, set.nmem_set_of_eq] at hx, linarith, end ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ : by { rw ← set_integral_neg_eq_set_integral_nonpos hf hfi, congr, ext1 x, simp, } lemma set_integral_const (c : E) : ∫ x in s, c ∂μ = (μ s).to_real • c := by rw [integral_const, measure.restrict_apply_univ] @[simp] lemma integral_indicator_const (e : E) ⦃s : set α⦄ (s_meas : measurable_set s) : ∫ (a : α), s.indicator (λ (x : α), e) a ∂μ = (μ s).to_real • e := by rw [integral_indicator s_meas, ← set_integral_const] @[simp] lemma integral_indicator_one ⦃s : set α⦄ (hs : measurable_set s) : ∫ a, s.indicator 1 a ∂μ = (μ s).to_real := (integral_indicator_const 1 hs).trans ((smul_eq_mul _).trans (mul_one _)) lemma set_integral_indicator_const_Lp {p : ℝ≥0∞} (hs : measurable_set s) (ht : measurable_set t) (hμt : μ t ≠ ∞) (x : E) : ∫ a in s, indicator_const_Lp p ht hμt x a ∂μ = (μ (t ∩ s)).to_real • x := calc ∫ a in s, indicator_const_Lp p ht hμt x a ∂μ = (∫ a in s, t.indicator (λ _, x) a ∂μ) : by rw set_integral_congr_ae hs (indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx)) ... = (μ (t ∩ s)).to_real • x : by rw [integral_indicator_const _ ht, measure.restrict_apply ht] lemma integral_indicator_const_Lp {p : ℝ≥0∞} (ht : measurable_set t) (hμt : μ t ≠ ∞) (x : E) : ∫ a, indicator_const_Lp p ht hμt x a ∂μ = (μ t).to_real • x := calc ∫ a, indicator_const_Lp p ht hμt x a ∂μ = ∫ a in univ, indicator_const_Lp p ht hμt x a ∂μ : by rw integral_univ ... = (μ (t ∩ univ)).to_real • x : set_integral_indicator_const_Lp measurable_set.univ ht hμt x ... = (μ t).to_real • x : by rw inter_univ lemma set_integral_map {β} [measurable_space β] {g : α → β} {f : β → E} {s : set β} (hs : measurable_set s) (hf : ae_strongly_measurable f (measure.map g μ)) (hg : ae_measurable g μ) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := begin rw [measure.restrict_map_of_ae_measurable hg hs, integral_map (hg.mono_measure measure.restrict_le_self) (hf.mono_measure _)], exact measure.map_mono_of_ae_measurable measure.restrict_le_self hg end lemma _root_.measurable_embedding.set_integral_map {β} {_ : measurable_space β} {f : α → β} (hf : measurable_embedding f) (g : β → E) (s : set β) : ∫ y in s, g y ∂(measure.map f μ) = ∫ x in f ⁻¹' s, g (f x) ∂μ := by rw [hf.restrict_map, hf.integral_map] lemma _root_.closed_embedding.set_integral_map [topological_space α] [borel_space α] {β} [measurable_space β] [topological_space β] [borel_space β] {g : α → β} {f : β → E} (s : set β) (hg : closed_embedding g) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := hg.measurable_embedding.set_integral_map _ _ lemma measure_preserving.set_integral_preimage_emb {β} {_ : measurable_space β} {f : α → β} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) (s : set β) : ∫ x in f ⁻¹' s, g (f x) ∂μ = ∫ y in s, g y ∂ν := (h₁.restrict_preimage_emb h₂ s).integral_comp h₂ _ lemma measure_preserving.set_integral_image_emb {β} {_ : measurable_space β} {f : α → β} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) (s : set α) : ∫ y in f '' s, g y ∂ν = ∫ x in s, g (f x) ∂μ := eq.symm $ (h₁.restrict_image_emb h₂ s).integral_comp h₂ _ lemma set_integral_map_equiv {β} [measurable_space β] (e : α ≃ᵐ β) (f : β → E) (s : set β) : ∫ y in s, f y ∂(measure.map e μ) = ∫ x in e ⁻¹' s, f (e x) ∂μ := e.measurable_embedding.set_integral_map f s lemma norm_set_integral_le_of_norm_le_const_ae {C : ℝ} (hs : μ s < ∞) (hC : ∀ᵐ x ∂μ.restrict s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin rw ← measure.restrict_apply_univ at *, haveI : is_finite_measure (μ.restrict s) := ⟨‹_›⟩, exact norm_integral_le_of_norm_le_const hC end lemma norm_set_integral_le_of_norm_le_const_ae' {C : ℝ} (hs : μ s < ∞) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) (hfm : ae_strongly_measurable f (μ.restrict s)) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin apply norm_set_integral_le_of_norm_le_const_ae hs, have A : ∀ᵐ (x : α) ∂μ, x ∈ s → ∥ae_strongly_measurable.mk f hfm x∥ ≤ C, { filter_upwards [hC, hfm.ae_mem_imp_eq_mk] with _ h1 h2 h3, rw [← h2 h3], exact h1 h3 }, have B : measurable_set {x | ∥(hfm.mk f) x∥ ≤ C} := hfm.strongly_measurable_mk.norm.measurable measurable_set_Iic, filter_upwards [hfm.ae_eq_mk, (ae_restrict_iff B).2 A] with _ h1 _, rwa h1, end lemma norm_set_integral_le_of_norm_le_const_ae'' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae hs $ by rwa [ae_restrict_eq hsm, eventually_inf_principal] lemma norm_set_integral_le_of_norm_le_const {C : ℝ} (hs : μ s < ∞) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) (hfm : ae_strongly_measurable f (μ.restrict s)) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae' hs (eventually_of_forall hC) hfm lemma norm_set_integral_le_of_norm_le_const' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae'' hs hsm $ eventually_of_forall hC lemma set_integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f) (hfi : integrable_on f s μ) : ∫ x in s, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict s] 0 := integral_eq_zero_iff_of_nonneg_ae hf hfi lemma set_integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f) (hfi : integrable_on f s μ) : 0 < ∫ x in s, f x ∂μ ↔ 0 < μ (support f ∩ s) := begin rw [integral_pos_iff_support_of_nonneg_ae hf hfi, measure.restrict_apply₀], rw support_eq_preimage, exact hfi.ae_strongly_measurable.ae_measurable.null_measurable (measurable_set_singleton 0).compl end lemma set_integral_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → E} (hf_meas : strongly_measurable[m] f) {s : set α} (hs : measurable_set[m] s) : ∫ x in s, f x ∂μ = ∫ x in s, f x ∂(μ.trim hm) := by rwa [integral_trim hm hf_meas, restrict_trim hm μ] lemma integral_Icc_eq_integral_Ioc' [partial_order α] {f : α → E} {a b : α} (ha : μ {a} = 0) : ∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ := set_integral_congr_set_ae (Ioc_ae_eq_Icc' ha).symm lemma integral_Ioc_eq_integral_Ioo' [partial_order α] {f : α → E} {a b : α} (hb : μ {b} = 0) : ∫ t in Ioc a b, f t ∂μ = ∫ t in Ioo a b, f t ∂μ := set_integral_congr_set_ae (Ioo_ae_eq_Ioc' hb).symm lemma integral_Icc_eq_integral_Ioc [partial_order α] {f : α → E} {a b : α} [has_no_atoms μ] : ∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ := integral_Icc_eq_integral_Ioc' $ measure_singleton a lemma integral_Ioc_eq_integral_Ioo [partial_order α] {f : α → E} {a b : α} [has_no_atoms μ] : ∫ t in Ioc a b, f t ∂μ = ∫ t in Ioo a b, f t ∂μ := integral_Ioc_eq_integral_Ioo' $ measure_singleton b end normed_group section mono variables {μ : measure α} {f g : α → ℝ} {s t : set α} (hf : integrable_on f s μ) (hg : integrable_on g s μ) lemma set_integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict s] g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := integral_mono_ae hf hg h lemma set_integral_mono_ae (h : f ≤ᵐ[μ] g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := set_integral_mono_ae_restrict hf hg (ae_restrict_of_ae h) lemma set_integral_mono_on (hs : measurable_set s) (h : ∀ x ∈ s, f x ≤ g x) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := set_integral_mono_ae_restrict hf hg (by simp [hs, eventually_le, eventually_inf_principal, ae_of_all _ h]) include hf hg -- why do I need this include, but we don't need it in other lemmas? lemma set_integral_mono_on_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x ≤ g x) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := by { refine set_integral_mono_ae_restrict hf hg _, rwa [eventually_le, ae_restrict_iff' hs], } omit hf hg lemma set_integral_mono (h : f ≤ g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := integral_mono hf hg h lemma set_integral_mono_set (hfi : integrable_on f t μ) (hf : 0 ≤ᵐ[μ.restrict t] f) (hst : s ≤ᵐ[μ] t) : ∫ x in s, f x ∂μ ≤ ∫ x in t, f x ∂μ := integral_mono_measure (measure.restrict_mono_ae hst) hf hfi lemma set_integral_ge_of_const_le {c : ℝ} (hs : measurable_set s) (hμs : μ s ≠ ∞) (hf : ∀ x ∈ s, c ≤ f x) (hfint : integrable_on (λ (x : α), f x) s μ) : c * (μ s).to_real ≤ ∫ x in s, f x ∂μ := begin rw [mul_comm, ← smul_eq_mul, ← set_integral_const c], exact set_integral_mono_on (integrable_on_const.2 (or.inr hμs.lt_top)) hfint hs hf, end end mono section nonneg variables {μ : measure α} {f : α → ℝ} {s : set α} lemma set_integral_nonneg_of_ae_restrict (hf : 0 ≤ᵐ[μ.restrict s] f) : 0 ≤ ∫ a in s, f a ∂μ := integral_nonneg_of_ae hf lemma set_integral_nonneg_of_ae (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a in s, f a ∂μ := set_integral_nonneg_of_ae_restrict (ae_restrict_of_ae hf) lemma set_integral_nonneg (hs : measurable_set s) (hf : ∀ a, a ∈ s → 0 ≤ f a) : 0 ≤ ∫ a in s, f a ∂μ := set_integral_nonneg_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf)) lemma set_integral_nonneg_ae (hs : measurable_set s) (hf : ∀ᵐ a ∂μ, a ∈ s → 0 ≤ f a) : 0 ≤ ∫ a in s, f a ∂μ := set_integral_nonneg_of_ae_restrict $ by rwa [eventually_le, ae_restrict_iff' hs] lemma set_integral_le_nonneg {s : set α} (hs : measurable_set s) (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x in s, f x ∂μ ≤ ∫ x in {y | 0 ≤ f y}, f x ∂μ := begin rw [← integral_indicator hs, ← integral_indicator (strongly_measurable_const.measurable_set_le hf)], exact integral_mono (hfi.indicator hs) (hfi.indicator (strongly_measurable_const.measurable_set_le hf)) (indicator_le_indicator_nonneg s f), end lemma set_integral_nonpos_of_ae_restrict (hf : f ≤ᵐ[μ.restrict s] 0) : ∫ a in s, f a ∂μ ≤ 0 := integral_nonpos_of_ae hf lemma set_integral_nonpos_of_ae (hf : f ≤ᵐ[μ] 0) : ∫ a in s, f a ∂μ ≤ 0 := set_integral_nonpos_of_ae_restrict (ae_restrict_of_ae hf) lemma set_integral_nonpos (hs : measurable_set s) (hf : ∀ a, a ∈ s → f a ≤ 0) : ∫ a in s, f a ∂μ ≤ 0 := set_integral_nonpos_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf)) lemma set_integral_nonpos_ae (hs : measurable_set s) (hf : ∀ᵐ a ∂μ, a ∈ s → f a ≤ 0) : ∫ a in s, f a ∂μ ≤ 0 := set_integral_nonpos_of_ae_restrict $ by rwa [eventually_le, ae_restrict_iff' hs] lemma set_integral_nonpos_le {s : set α} (hs : measurable_set s) (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x in {y | f y ≤ 0}, f x ∂μ ≤ ∫ x in s, f x ∂μ := begin rw [← integral_indicator hs, ← integral_indicator (hf.measurable_set_le strongly_measurable_const)], exact integral_mono (hfi.indicator (hf.measurable_set_le strongly_measurable_const)) (hfi.indicator hs) (indicator_nonpos_le_indicator s f), end end nonneg section tendsto_mono variables {μ : measure α} [normed_group E] [complete_space E] [normed_space ℝ E] {s : ℕ → set α} {f : α → E} lemma _root_.antitone.tendsto_set_integral (hsm : ∀ i, measurable_set (s i)) (h_anti : antitone s) (hfi : integrable_on f (s 0) μ) : tendsto (λi, ∫ a in s i, f a ∂μ) at_top (𝓝 (∫ a in (⋂ n, s n), f a ∂μ)) := begin let bound : α → ℝ := indicator (s 0) (λ a, ∥f a∥), have h_int_eq : (λ i, ∫ a in s i, f a ∂μ) = (λ i, ∫ a, (s i).indicator f a ∂μ), from funext (λ i, (integral_indicator (hsm i)).symm), rw h_int_eq, rw ← integral_indicator (measurable_set.Inter hsm), refine tendsto_integral_of_dominated_convergence bound _ _ _ _, { intro n, rw ae_strongly_measurable_indicator_iff (hsm n), exact (integrable_on.mono_set hfi (h_anti (zero_le n))).1 }, { rw integrable_indicator_iff (hsm 0), exact hfi.norm, }, { simp_rw norm_indicator_eq_indicator_norm, refine λ n, eventually_of_forall (λ x, _), exact indicator_le_indicator_of_subset (h_anti (zero_le n)) (λ a, norm_nonneg _) _ }, { filter_upwards with a using le_trans (h_anti.tendsto_indicator _ _ _) (pure_le_nhds _), }, end end tendsto_mono /-! ### Continuity of the set integral We prove that for any set `s`, the function `λ f : α →₁[μ] E, ∫ x in s, f x ∂μ` is continuous. -/ section continuous_set_integral variables [normed_group E] {𝕜 : Type*} [normed_field 𝕜] [normed_group F] [normed_space 𝕜 F] {p : ℝ≥0∞} {μ : measure α} /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is additive. -/ lemma Lp_to_Lp_restrict_add (f g : Lp E p μ) (s : set α) : ((Lp.mem_ℒp (f + g)).restrict s).to_Lp ⇑(f + g) = ((Lp.mem_ℒp f).restrict s).to_Lp f + ((Lp.mem_ℒp g).restrict s).to_Lp g := begin ext1, refine (ae_restrict_of_ae (Lp.coe_fn_add f g)).mp _, refine (Lp.coe_fn_add (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s)) (mem_ℒp.to_Lp g ((Lp.mem_ℒp g).restrict s))).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp g).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (f+g)).restrict s)).mono (λ x hx1 hx2 hx3 hx4 hx5, _), rw [hx4, hx1, pi.add_apply, hx2, hx3, hx5, pi.add_apply], end /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map commutes with scalar multiplication. -/ lemma Lp_to_Lp_restrict_smul (c : 𝕜) (f : Lp F p μ) (s : set α) : ((Lp.mem_ℒp (c • f)).restrict s).to_Lp ⇑(c • f) = c • (((Lp.mem_ℒp f).restrict s).to_Lp f) := begin ext1, refine (ae_restrict_of_ae (Lp.coe_fn_smul c f)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (c • f)).restrict s)).mp _, refine (Lp.coe_fn_smul c (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s))).mono (λ x hx1 hx2 hx3 hx4, _), rw [hx2, hx1, pi.smul_apply, hx3, hx4, pi.smul_apply], end /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is non-expansive. -/ lemma norm_Lp_to_Lp_restrict_le (s : set α) (f : Lp E p μ) : ∥((Lp.mem_ℒp f).restrict s).to_Lp f∥ ≤ ∥f∥ := begin rw [Lp.norm_def, Lp.norm_def, ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _)], refine (le_of_eq _).trans (snorm_mono_measure _ measure.restrict_le_self), { exact s, }, exact snorm_congr_ae (mem_ℒp.coe_fn_to_Lp _), end variables (α F 𝕜) /-- Continuous linear map sending a function of `Lp F p μ` to the same function in `Lp F p (μ.restrict s)`. -/ def Lp_to_Lp_restrict_clm (μ : measure α) (p : ℝ≥0∞) [hp : fact (1 ≤ p)] (s : set α) : Lp F p μ →L[𝕜] Lp F p (μ.restrict s) := @linear_map.mk_continuous 𝕜 𝕜 (Lp F p μ) (Lp F p (μ.restrict s)) _ _ _ _ _ _ (ring_hom.id 𝕜) ⟨λ f, mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s), λ f g, Lp_to_Lp_restrict_add f g s, λ c f, Lp_to_Lp_restrict_smul c f s⟩ 1 (by { intro f, rw one_mul, exact norm_Lp_to_Lp_restrict_le s f, }) variables {α F 𝕜} variables (𝕜) lemma Lp_to_Lp_restrict_clm_coe_fn [hp : fact (1 ≤ p)] (s : set α) (f : Lp F p μ) : Lp_to_Lp_restrict_clm α F 𝕜 μ p s f =ᵐ[μ.restrict s] f := mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s) variables {𝕜} @[continuity] lemma continuous_set_integral [normed_space ℝ E] [complete_space E] (s : set α) : continuous (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) := begin haveI : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩, have h_comp : (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) = (integral (μ.restrict s)) ∘ (λ f, Lp_to_Lp_restrict_clm α E ℝ μ 1 s f), { ext1 f, rw [function.comp_apply, integral_congr_ae (Lp_to_Lp_restrict_clm_coe_fn ℝ s f)], }, rw h_comp, exact continuous_integral.comp (Lp_to_Lp_restrict_clm α E ℝ μ 1 s).continuous, end end continuous_set_integral end measure_theory open measure_theory asymptotics metric variables {ι : Type*} [normed_group E] /-- Fundamental theorem of calculus for set integrals: if `μ` is a measure that is finite at a filter `l` and `f` is a measurable function that has a finite limit `b` at `l ⊓ μ.ae`, then `∫ x in s i, f x ∂μ = μ (s i) • b + o(μ (s i))` at a filter `li` provided that `s i` tends to `l.small_sets` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma filter.tendsto.integral_sub_linear_is_o_ae [normed_space ℝ E] [complete_space E] {μ : measure α} {l : filter α} [l.is_measurably_generated] {f : α → E} {b : E} (h : tendsto f (l ⊓ μ.ae) (𝓝 b)) (hfm : strongly_measurable_at_filter f l μ) (hμ : μ.finite_at_filter l) {s : ι → set α} {li : filter ι} (hs : tendsto s li l.small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • b) =o[li] m := begin suffices : (λ s, ∫ x in s, f x ∂μ - (μ s).to_real • b) =o[l.small_sets] (λ s, (μ s).to_real), from (this.comp_tendsto hs).congr' (hsμ.mono $ λ a ha, ha ▸ rfl) hsμ, refine is_o_iff.2 (λ ε ε₀, _), have : ∀ᶠ s in l.small_sets, ∀ᶠ x in μ.ae, x ∈ s → f x ∈ closed_ball b ε := eventually_small_sets_eventually.2 (h.eventually $ closed_ball_mem_nhds _ ε₀), filter_upwards [hμ.eventually, (hμ.integrable_at_filter_of_tendsto_ae hfm h).eventually, hfm.eventually, this], simp only [mem_closed_ball, dist_eq_norm], intros s hμs h_integrable hfm h_norm, rw [← set_integral_const, ← integral_sub h_integrable (integrable_on_const.2 $ or.inr hμs), real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg], exact norm_set_integral_le_of_norm_le_const_ae' hμs h_norm (hfm.sub ae_strongly_measurable_const) end /-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a` within a measurable set `t`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at a filter `li` provided that `s i` tends to `(𝓝[t] a).small_sets` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_within_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [complete_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α} {f : α → E} (ha : continuous_within_at f t a) (ht : measurable_set t) (hfm : strongly_measurable_at_filter f (𝓝[t] a) μ) {s : ι → set α} {li : filter ι} (hs : tendsto s li (𝓝[t] a).small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • f a) =o[li] m := by haveI : (𝓝[t] a).is_measurably_generated := ht.nhds_within_is_measurably_generated _; exact (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds_within a t) hs m hsμ /-- Fundamental theorem of calculus for set integrals, `nhds` version: if `μ` is a locally finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s` tends to `(𝓝 a).small_sets` along `li. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [complete_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {f : α → E} (ha : continuous_at f a) (hfm : strongly_measurable_at_filter f (𝓝 a) μ) {s : ι → set α} {li : filter ι} (hs : tendsto s li (𝓝 a).small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • f a) =o[li] m := (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds a) hs m hsμ /-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally finite measure, `f` is continuous on a measurable set `t`, and `a ∈ t`, then `∫ x in (s i), f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s i` tends to `(𝓝[t] a).small_sets` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_on.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [complete_space E] [second_countable_topology_either α E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α} {f : α → E} (hft : continuous_on f t) (ha : a ∈ t) (ht : measurable_set t) {s : ι → set α} {li : filter ι} (hs : tendsto s li (𝓝[t] a).small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • f a) =o[li] m := (hft a ha).integral_sub_linear_is_o_ae ht ⟨t, self_mem_nhds_within, hft.ae_strongly_measurable ht⟩ hs m hsμ section /-! ### Continuous linear maps composed with integration The goal of this section is to prove that integration commutes with continuous linear maps. This holds for simple functions. The general result follows from the continuity of all involved operations on the space `L¹`. Note that composition by a continuous linear map on `L¹` is not just the composition, as we are dealing with classes of functions, but it has already been defined as `continuous_linear_map.comp_Lp`. We take advantage of this construction here. -/ open_locale complex_conjugate variables {μ : measure α} {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] [normed_group F] [normed_space 𝕜 F] {p : ennreal} namespace continuous_linear_map variables [complete_space F] [normed_space ℝ F] lemma integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) : ∫ a, (L.comp_Lp φ) a ∂μ = ∫ a, L (φ a) ∂μ := integral_congr_ae $ coe_fn_comp_Lp _ _ lemma set_integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) {s : set α} (hs : measurable_set s) : ∫ a in s, (L.comp_Lp φ) a ∂μ = ∫ a in s, L (φ a) ∂μ := set_integral_congr_ae hs ((L.coe_fn_comp_Lp φ).mono (λ x hx hx2, hx)) lemma continuous_integral_comp_L1 (L : E →L[𝕜] F) : continuous (λ (φ : α →₁[μ] E), ∫ (a : α), L (φ a) ∂μ) := by { rw ← funext L.integral_comp_Lp, exact continuous_integral.comp (L.comp_LpL 1 μ).continuous, } variables [complete_space E] [normed_space ℝ E] lemma integral_comp_comm (L : E →L[𝕜] F) {φ : α → E} (φ_int : integrable φ μ) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin apply integrable.induction (λ φ, ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ)), { intros e s s_meas s_finite, rw [integral_indicator_const e s_meas, ← @smul_one_smul E ℝ 𝕜 _ _ _ _ _ (μ s).to_real e, continuous_linear_map.map_smul, @smul_one_smul F ℝ 𝕜 _ _ _ _ _ (μ s).to_real (L e), ← integral_indicator_const (L e) s_meas], congr' 1 with a, rw set.indicator_comp_of_zero L.map_zero }, { intros f g H f_int g_int hf hg, simp [L.map_add, integral_add f_int g_int, integral_add (L.integrable_comp f_int) (L.integrable_comp g_int), hf, hg] }, { exact is_closed_eq L.continuous_integral_comp_L1 (L.continuous.comp continuous_integral) }, { intros f g hfg f_int hf, convert hf using 1 ; clear hf, { exact integral_congr_ae (hfg.fun_comp L).symm }, { rw integral_congr_ae hfg.symm } }, all_goals { assumption } end lemma integral_apply {H : Type*} [normed_group H] [normed_space 𝕜 H] {φ : α → H →L[𝕜] E} (φ_int : integrable φ μ) (v : H) : (∫ a, φ a ∂μ) v = ∫ a, φ a v ∂μ := ((continuous_linear_map.apply 𝕜 E v).integral_comp_comm φ_int).symm lemma integral_comp_comm' (L : E →L[𝕜] F) {K} (hL : antilipschitz_with K L) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin by_cases h : integrable φ μ, { exact integral_comp_comm L h }, have : ¬ (integrable (L ∘ φ) μ), by rwa lipschitz_with.integrable_comp_iff_of_antilipschitz L.lipschitz hL (L.map_zero), simp [integral_undef, h, this] end lemma integral_comp_L1_comm (L : E →L[𝕜] F) (φ : α →₁[μ] E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.integral_comp_comm (L1.integrable_coe_fn φ) end continuous_linear_map namespace linear_isometry variables [complete_space F] [normed_space ℝ F] [complete_space E] [normed_space ℝ E] lemma integral_comp_comm (L : E →ₗᵢ[𝕜] F) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.to_continuous_linear_map.integral_comp_comm' L.antilipschitz _ end linear_isometry variables [complete_space E] [normed_space ℝ E] [complete_space F] [normed_space ℝ F] @[norm_cast] lemma integral_of_real {f : α → ℝ} : ∫ a, (f a : 𝕜) ∂μ = ↑∫ a, f a ∂μ := (@is_R_or_C.of_real_li 𝕜 _).integral_comp_comm f lemma integral_re {f : α → 𝕜} (hf : integrable f μ) : ∫ a, is_R_or_C.re (f a) ∂μ = is_R_or_C.re ∫ a, f a ∂μ := (@is_R_or_C.re_clm 𝕜 _).integral_comp_comm hf lemma integral_im {f : α → 𝕜} (hf : integrable f μ) : ∫ a, is_R_or_C.im (f a) ∂μ = is_R_or_C.im ∫ a, f a ∂μ := (@is_R_or_C.im_clm 𝕜 _).integral_comp_comm hf lemma integral_conj {f : α → 𝕜} : ∫ a, conj (f a) ∂μ = conj ∫ a, f a ∂μ := (@is_R_or_C.conj_lie 𝕜 _).to_linear_isometry.integral_comp_comm f lemma integral_coe_re_add_coe_im {f : α → 𝕜} (hf : integrable f μ) : ∫ x, (is_R_or_C.re (f x) : 𝕜) ∂μ + ∫ x, is_R_or_C.im (f x) ∂μ * is_R_or_C.I = ∫ x, f x ∂μ := begin rw [mul_comm, ← smul_eq_mul, ← integral_smul, ← integral_add], { congr, ext1 x, rw [smul_eq_mul, mul_comm, is_R_or_C.re_add_im] }, { exact hf.re.of_real }, { exact hf.im.of_real.smul is_R_or_C.I } end lemma integral_re_add_im {f : α → 𝕜} (hf : integrable f μ) : ((∫ x, is_R_or_C.re (f x) ∂μ : ℝ) : 𝕜) + (∫ x, is_R_or_C.im (f x) ∂μ : ℝ) * is_R_or_C.I = ∫ x, f x ∂μ := by { rw [← integral_of_real, ← integral_of_real, integral_coe_re_add_coe_im hf] } lemma set_integral_re_add_im {f : α → 𝕜} {i : set α} (hf : integrable_on f i μ) : ((∫ x in i, is_R_or_C.re (f x) ∂μ : ℝ) : 𝕜) + (∫ x in i, is_R_or_C.im (f x) ∂μ : ℝ) * is_R_or_C.I = ∫ x in i, f x ∂μ := integral_re_add_im hf lemma fst_integral {f : α → E × F} (hf : integrable f μ) : (∫ x, f x ∂μ).1 = ∫ x, (f x).1 ∂μ := ((continuous_linear_map.fst ℝ E F).integral_comp_comm hf).symm lemma snd_integral {f : α → E × F} (hf : integrable f μ) : (∫ x, f x ∂μ).2 = ∫ x, (f x).2 ∂μ := ((continuous_linear_map.snd ℝ E F).integral_comp_comm hf).symm lemma integral_pair {f : α → E} {g : α → F} (hf : integrable f μ) (hg : integrable g μ) : ∫ x, (f x, g x) ∂μ = (∫ x, f x ∂μ, ∫ x, g x ∂μ) := have _ := hf.prod_mk hg, prod.ext (fst_integral this) (snd_integral this) lemma integral_smul_const {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] (f : α → 𝕜) (c : E) : ∫ x, f x • c ∂μ = (∫ x, f x ∂μ) • c := begin by_cases hf : integrable f μ, { exact ((1 : 𝕜 →L[𝕜] 𝕜).smul_right c).integral_comp_comm hf }, { by_cases hc : c = 0, { simp only [hc, integral_zero, smul_zero] }, rw [integral_undef hf, integral_undef, zero_smul], simp_rw [integrable_smul_const hc, hf, not_false_iff] } end section inner variables {E' : Type*} [inner_product_space 𝕜 E'] [complete_space E'] [normed_space ℝ E'] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E' _ x y lemma integral_inner {f : α → E'} (hf : integrable f μ) (c : E') : ∫ x, ⟪c, f x⟫ ∂μ = ⟪c, ∫ x, f x ∂μ⟫ := ((@innerSL 𝕜 E' _ _ c).restrict_scalars ℝ).integral_comp_comm hf lemma integral_eq_zero_of_forall_integral_inner_eq_zero (f : α → E') (hf : integrable f μ) (hf_int : ∀ (c : E'), ∫ x, ⟪c, f x⟫ ∂μ = 0) : ∫ x, f x ∂μ = 0 := by { specialize hf_int (∫ x, f x ∂μ), rwa [integral_inner hf, inner_self_eq_zero] at hf_int } end inner lemma integral_with_density_eq_integral_smul {f : α → ℝ≥0} (f_meas : measurable f) (g : α → E) : ∫ a, g a ∂(μ.with_density (λ x, f x)) = ∫ a, f a • g a ∂μ := begin by_cases hg : integrable g (μ.with_density (λ x, f x)), swap, { rw [integral_undef hg, integral_undef], rwa [← integrable_with_density_iff_integrable_smul f_meas]; apply_instance }, refine integrable.induction _ _ _ _ _ hg, { assume c s s_meas hs, rw integral_indicator s_meas, simp_rw [← indicator_smul_apply, integral_indicator s_meas], simp only [s_meas, integral_const, measure.restrict_apply', univ_inter, with_density_apply], rw [lintegral_coe_eq_integral, ennreal.to_real_of_real, ← integral_smul_const], { refl }, { exact integral_nonneg (λ x, nnreal.coe_nonneg _) }, { refine ⟨(f_meas.coe_nnreal_real).ae_measurable.ae_strongly_measurable, _⟩, rw with_density_apply _ s_meas at hs, rw has_finite_integral, convert hs, ext1 x, simp only [nnreal.nnnorm_eq] } }, { assume u u' h_disj u_int u'_int h h', change ∫ (a : α), (u a + u' a) ∂μ.with_density (λ (x : α), ↑(f x)) = ∫ (a : α), f a • (u a + u' a) ∂μ, simp_rw [smul_add], rw [integral_add u_int u'_int, h, h', integral_add], { exact (integrable_with_density_iff_integrable_smul f_meas).1 u_int }, { exact (integrable_with_density_iff_integrable_smul f_meas).1 u'_int } }, { have C1 : continuous (λ (u : Lp E 1 (μ.with_density (λ x, f x))), ∫ x, u x ∂(μ.with_density (λ x, f x))) := continuous_integral, have C2 : continuous (λ (u : Lp E 1 (μ.with_density (λ x, f x))), ∫ x, f x • u x ∂μ), { have : continuous ((λ (u : Lp E 1 μ), ∫ x, u x ∂μ) ∘ (with_density_smul_li μ f_meas)) := continuous_integral.comp (with_density_smul_li μ f_meas).continuous, convert this, ext1 u, simp only [function.comp_app, with_density_smul_li_apply], exact integral_congr_ae (mem_ℒ1_smul_of_L1_with_density f_meas u).coe_fn_to_Lp.symm }, exact is_closed_eq C1 C2 }, { assume u v huv u_int hu, rw [← integral_congr_ae huv, hu], apply integral_congr_ae, filter_upwards [(ae_with_density_iff f_meas.coe_nnreal_ennreal).1 huv] with x hx, rcases eq_or_ne (f x) 0 with h'x|h'x, { simp only [h'x, zero_smul]}, { rw [hx _], simpa only [ne.def, ennreal.coe_eq_zero] using h'x } } end lemma integral_with_density_eq_integral_smul₀ {f : α → ℝ≥0} (hf : ae_measurable f μ) (g : α → E) : ∫ a, g a ∂(μ.with_density (λ x, f x)) = ∫ a, f a • g a ∂μ := begin let f' := hf.mk _, calc ∫ a, g a ∂(μ.with_density (λ x, f x)) = ∫ a, g a ∂(μ.with_density (λ x, f' x)) : begin congr' 1, apply with_density_congr_ae, filter_upwards [hf.ae_eq_mk] with x hx, rw hx, end ... = ∫ a, f' a • g a ∂μ : integral_with_density_eq_integral_smul hf.measurable_mk _ ... = ∫ a, f a • g a ∂μ : begin apply integral_congr_ae, filter_upwards [hf.ae_eq_mk] with x hx, rw hx, end end lemma set_integral_with_density_eq_set_integral_smul {f : α → ℝ≥0} (f_meas : measurable f) (g : α → E) {s : set α} (hs : measurable_set s) : ∫ a in s, g a ∂(μ.with_density (λ x, f x)) = ∫ a in s, f a • g a ∂μ := by rw [restrict_with_density hs, integral_with_density_eq_integral_smul f_meas] lemma set_integral_with_density_eq_set_integral_smul₀ {f : α → ℝ≥0} {s : set α} (hf : ae_measurable f (μ.restrict s)) (g : α → E) (hs : measurable_set s) : ∫ a in s, g a ∂(μ.with_density (λ x, f x)) = ∫ a in s, f a • g a ∂μ := by rw [restrict_with_density hs, integral_with_density_eq_integral_smul₀ hf] end section thickened_indicator variables [pseudo_emetric_space α] lemma measure_le_lintegral_thickened_indicator_aux (μ : measure α) {E : set α} (E_mble : measurable_set E) (δ : ℝ) : μ E ≤ ∫⁻ a, (thickened_indicator_aux δ E a : ℝ≥0∞) ∂μ := begin convert_to lintegral μ (E.indicator (λ _, (1 : ℝ≥0∞))) ≤ lintegral μ (thickened_indicator_aux δ E), { rw [lintegral_indicator _ E_mble], simp only [lintegral_one, measure.restrict_apply, measurable_set.univ, univ_inter], }, { apply lintegral_mono, apply indicator_le_thickened_indicator_aux, }, end lemma measure_le_lintegral_thickened_indicator (μ : measure α) {E : set α} (E_mble : measurable_set E) {δ : ℝ} (δ_pos : 0 < δ) : μ E ≤ ∫⁻ a, (thickened_indicator δ_pos E a : ℝ≥0∞) ∂μ := begin convert measure_le_lintegral_thickened_indicator_aux μ E_mble δ, dsimp, simp only [thickened_indicator_aux_lt_top.ne, ennreal.coe_to_nnreal, ne.def, not_false_iff], end end thickened_indicator
855ef3d203eff3b953add09d1f7bf8d86d6b1aa1
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Elab/Declaration.lean
93c1fea50fe069d3cb1d87ef9ec84b97496891a7
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,187
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, Sebastian Ullrich -/ import Lean.Util.CollectLevelParams import Lean.Elab.DeclUtil import Lean.Elab.DefView import Lean.Elab.Inductive import Lean.Elab.Structure import Lean.Elab.MutualDef namespace Lean namespace Elab namespace Command open Meta /- Auxiliary function for `expandDeclNamespace?` -/ def expandDeclIdNamespace? (declId : Syntax) : Option (Name × Syntax) := let (id, optUnivDeclStx) := expandDeclIdCore declId; let scpView := extractMacroScopes id; match scpView.name with | Name.str Name.anonymous s _ => none | Name.str pre s _ => let nameNew := { scpView with name := mkNameSimple s }.review; if declId.isIdent then some (pre, mkIdentFrom declId nameNew) else some (pre, declId.setArg 0 (mkIdentFrom declId nameNew)) | _ => none /- given declarations such as `@[...] def Foo.Bla.f ...` return `some (Foo.Bla, @[...] def f ...)` -/ def expandDeclNamespace? (stx : Syntax) : Option (Name × Syntax) := if !stx.isOfKind `Lean.Parser.Command.declaration then none else let decl := stx.getArg 1; let k := decl.getKind; if k == `Lean.Parser.Command.abbrev || k == `Lean.Parser.Command.def || k == `Lean.Parser.Command.theorem || k == `Lean.Parser.Command.constant || k == `Lean.Parser.Command.axiom || k == `Lean.Parser.Command.inductive || k == `Lean.Parser.Command.structure then match expandDeclIdNamespace? (decl.getArg 1) with | some (ns, declId) => some (ns, stx.setArg 1 (decl.setArg 1 declId)) | none => none else if k == `Lean.Parser.Command.instance then let optDeclId := decl.getArg 1; if optDeclId.isNone then none else match expandDeclIdNamespace? (optDeclId.getArg 0) with | some (ns, declId) => some (ns, stx.setArg 1 (decl.setArg 1 (optDeclId.setArg 0 declId))) | none => none else if k == `Lean.Parser.Command.classInductive then match expandDeclIdNamespace? (decl.getArg 2) with | some (ns, declId) => some (ns, stx.setArg 1 (decl.setArg 2 declId)) | none => none else none def elabAxiom (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do -- parser! "axiom " >> declId >> declSig let declId := stx.getArg 1; let (binders, typeStx) := expandDeclSig (stx.getArg 2); scopeLevelNames ← getLevelNames; ⟨name, declName, allUserLevelNames⟩ ← expandDeclId declId modifiers; runTermElabM declName $ fun vars => Term.withLevelNames allUserLevelNames $ Term.elabBinders binders.getArgs fun xs => do Term.applyAttributesAt declName modifiers.attrs AttributeApplicationTime.beforeElaboration; type ← Term.elabType typeStx; Term.synthesizeSyntheticMVarsNoPostponing; type ← instantiateMVars type; type ← mkForallFVars xs type; (type, _) ← mkForallUsedOnly vars type; (type, _) ← Term.levelMVarToParam type; let usedParams := (collectLevelParams {} type).params; match sortDeclLevelParams scopeLevelNames allUserLevelNames usedParams with | Except.error msg => throwErrorAt stx msg | Except.ok levelParams => do let decl := Declaration.axiomDecl { name := declName, lparams := levelParams, type := type, isUnsafe := modifiers.isUnsafe }; Term.ensureNoUnassignedMVars decl; addDecl decl; Term.applyAttributesAt declName modifiers.attrs AttributeApplicationTime.afterTypeChecking; Term.applyAttributesAt declName modifiers.attrs AttributeApplicationTime.afterCompilation /- parser! "inductive " >> declId >> optDeclSig >> many ctor parser! try ("class " >> "inductive ") >> declId >> optDeclSig >> many ctor Remark: numTokens == 1 for regular `inductive` and 2 for `class inductive`. -/ private def inductiveSyntaxToView (modifiers : Modifiers) (decl : Syntax) (numTokens := 1) : CommandElabM InductiveView := do checkValidInductiveModifier modifiers; let (binders, type?) := expandOptDeclSig (decl.getArg (numTokens + 1)); let declId := decl.getArg numTokens; ⟨name, declName, levelNames⟩ ← expandDeclId declId modifiers; ctors ← (decl.getArg (numTokens + 2)).getArgs.mapM fun ctor => withRef ctor do { -- def ctor := parser! " | " >> declModifiers >> ident >> optional inferMod >> optDeclSig ctorModifiers ← elabModifiers (ctor.getArg 1); when (ctorModifiers.isPrivate && modifiers.isPrivate) $ throwError "invalid 'private' constructor in a 'private' inductive datatype"; when (ctorModifiers.isProtected && modifiers.isPrivate) $ throwError "invalid 'protected' constructor in a 'private' inductive datatype"; checkValidCtorModifier ctorModifiers; let ctorName := ctor.getIdAt 2; let ctorName := declName ++ ctorName; ctorName ← withRef (ctor.getArg 2) $ applyVisibility ctorModifiers.visibility ctorName; let inferMod := !(ctor.getArg 3).isNone; let (binders, type?) := expandOptDeclSig (ctor.getArg 4); pure { ref := ctor, modifiers := ctorModifiers, declName := ctorName, inferMod := inferMod, binders := binders, type? := type? : CtorView } }; pure { ref := decl, modifiers := modifiers, shortDeclName := name, declName := declName, levelNames := levelNames, binders := binders, type? := type?, ctors := ctors } private def classInductiveSyntaxToView (modifiers : Modifiers) (decl : Syntax) : CommandElabM InductiveView := inductiveSyntaxToView modifiers decl 2 def elabInductive (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do v ← inductiveSyntaxToView modifiers stx; elabInductiveViews #[v] def elabClassInductive (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do let modifiers := modifiers.addAttribute { name := `class }; v ← classInductiveSyntaxToView modifiers stx; elabInductiveViews #[v] @[builtinCommandElab declaration] def elabDeclaration : CommandElab := fun stx => match expandDeclNamespace? stx with | some (ns, newStx) => do let ns := mkIdentFrom stx ns; newStx ← `(namespace $ns:ident $newStx end $ns:ident); withMacroExpansion stx newStx $ elabCommand newStx | none => do modifiers ← elabModifiers (stx.getArg 0); let decl := stx.getArg 1; let declKind := decl.getKind; if declKind == `Lean.Parser.Command.axiom then elabAxiom modifiers decl else if declKind == `Lean.Parser.Command.inductive then elabInductive modifiers decl else if declKind == `Lean.Parser.Command.classInductive then elabClassInductive modifiers decl else if declKind == `Lean.Parser.Command.structure then elabStructure modifiers decl else if isDefLike decl then do elabMutualDef #[stx] else throwError "unexpected declaration" /- Return true if all elements of the mutual-block are inductive declarations. -/ private def isMutualInductive (stx : Syntax) : Bool := (stx.getArg 1).getArgs.all $ fun elem => let decl := elem.getArg 1; let declKind := decl.getKind; declKind == `Lean.Parser.Command.inductive private def elabMutualInductive (elems : Array Syntax) : CommandElabM Unit := do views ← elems.mapM $ fun stx => do { modifiers ← elabModifiers (stx.getArg 0); inductiveSyntaxToView modifiers (stx.getArg 1) }; elabInductiveViews views /- Return true if all elements of the mutual-block are definitions/theorems/abbrevs. -/ private def isMutualDef (stx : Syntax) : Bool := (stx.getArg 1).getArgs.all $ fun elem => let decl := elem.getArg 1; isDefLike decl private def isMutualPreambleCommand (stx : Syntax) : Bool := let k := stx.getKind; k == `Lean.Parser.Command.variable || k == `Lean.Parser.Command.variables || k == `Lean.Parser.Command.universe || k == `Lean.Parser.Command.universes || k == `Lean.Parser.Command.check || k == `Lean.Parser.Command.set_option || k == `Lean.Parser.Command.open private partial def splitMutualPreamble (elems : Array Syntax) : Nat → Option (Array Syntax × Array Syntax) | i => if h : i < elems.size then let elem := elems.get ⟨i, h⟩; if isMutualPreambleCommand elem then splitMutualPreamble (i+1) else if i == 0 then none -- `mutual` block does not contain any preamble commands else some (elems.extract 0 i, elems.extract i elems.size) else none -- a `mutual` block containing only preamble commands is not a valid `mutual` block @[builtinMacro Lean.Parser.Command.mutual] def expandMutualNamespace : Macro := fun stx => do let elems := (stx.getArg 1).getArgs; (ns?, elems) ← elems.foldlM (fun (acc : Option Name × Array Syntax) (elem : Syntax) => let (ns?, elems) := acc; match ns?, expandDeclNamespace? elem with | _, none => pure (ns?, elems.push elem) | none, some (ns, elem) => pure (some ns, elems.push elem) | some nsCurr, some (nsNew, elem) => if nsCurr == nsNew then pure (ns?, elems.push elem) else Macro.throwError elem ("conflicting namespaces in mutual declaration, using namespace '" ++ toString nsNew ++ "', but used '" ++ toString nsCurr ++ "' in previous declaration")) (none, #[]); match ns? with | some ns => let ns := mkIdentFrom stx ns; let stxNew := stx.setArg 1 (mkNullNode elems); `(namespace $ns:ident $stxNew end $ns:ident) | none => Macro.throwUnsupported @[builtinMacro Lean.Parser.Command.mutual] def expandMutualElement : Macro := fun stx => do let elems := (stx.getArg 1).getArgs; (elemsNew, modified) ← elems.foldlM (fun (acc : Array Syntax × Bool) elem => do let (elemsNew, modified) := acc; elem? ← expandMacro? elem; match elem? with | some elemNew => pure (elemsNew.push elemNew, true) | none => pure (elemsNew.push elem, modified)) (#[], false); if modified then pure $ stx.setArg 1 (mkNullNode elemsNew) else Macro.throwUnsupported @[builtinMacro Lean.Parser.Command.mutual] def expandMutualPreamble : Macro := fun stx => match splitMutualPreamble (stx.getArg 1).getArgs 0 with | none => Macro.throwUnsupported | some (preamble, rest) => do secCmd ← `(section); let newMutual := stx.setArg 1 (mkNullNode rest); endCmd ← `(end); pure $ mkNullNode (#[secCmd] ++ preamble ++ #[newMutual] ++ #[endCmd]) @[builtinCommandElab «mutual»] def elabMutual : CommandElab := fun stx => do if isMutualInductive stx then elabMutualInductive (stx.getArg 1).getArgs else if isMutualDef stx then elabMutualDef (stx.getArg 1).getArgs else throwError "invalid mutual block" /- parser! optional "local " >> "attribute " >> "[" >> sepBy1 Term.attrInstance ", " >> "]" >> many1 ident -/ @[builtinCommandElab «attribute»] def elabAttr : CommandElab := fun stx => do let persistent := (stx.getArg 0).isNone; attrs ← elabAttrs (stx.getArg 3); let idents := (stx.getArg 5).getArgs; idents.forM fun ident => withRef ident $ liftTermElabM none do declName ← Term.resolveGlobalConstNoOverload ident.getId; Term.applyAttributes declName attrs persistent end Command end Elab end Lean
f71aea3d092d11da50692146cc8c064b9aa7f556
c777c32c8e484e195053731103c5e52af26a25d1
/src/data/typevec.lean
60416b60ebafbe5ef307c6d9bc7cca6d289a95eb
[ "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
24,744
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Simon Hudon -/ import data.fin.fin2 import logic.function.basic import tactic.basic /-! # Tuples of types, and their categorical structure. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Features * `typevec n` - n-tuples of types * `α ⟹ β` - n-tuples of maps * `f ⊚ g` - composition Also, support functions for operating with n-tuples of types, such as: * `append1 α β` - append type `β` to n-tuple `α` to obtain an (n+1)-tuple * `drop α` - drops the last element of an (n+1)-tuple * `last α` - returns the last element of an (n+1)-tuple * `append_fun f g` - appends a function g to an n-tuple of functions * `drop_fun f` - drops the last function from an n+1-tuple * `last_fun f` - returns the last function of a tuple. Since e.g. `append1 α.drop α.last` is propositionally equal to `α` but not definitionally equal to it, we need support functions and lemmas to mediate between constructions. -/ universes u v w /-- n-tuples of types, as a category -/ def typevec (n : ℕ) := fin2 n → Type* instance {n} : inhabited (typevec.{u} n) := ⟨ λ _, punit ⟩ namespace typevec variable {n : ℕ} /-- arrow in the category of `typevec` -/ def arrow (α β : typevec n) := Π i : fin2 n, α i → β i localized "infixl (name := typevec.arrow) ` ⟹ `:40 := typevec.arrow" in mvfunctor instance arrow.inhabited (α β : typevec n) [Π i, inhabited (β i)] : inhabited (α ⟹ β) := ⟨ λ _ _, default ⟩ /-- identity of arrow composition -/ def id {α : typevec n} : α ⟹ α := λ i x, x /-- arrow composition in the category of `typevec` -/ def comp {α β γ : typevec n} (g : β ⟹ γ) (f : α ⟹ β) : α ⟹ γ := λ i x, g i (f i x) localized "infixr (name := typevec.comp) ` ⊚ `:80 := typevec.comp" in mvfunctor -- type as \oo @[simp] theorem id_comp {α β : typevec n} (f : α ⟹ β) : id ⊚ f = f := rfl @[simp] theorem comp_id {α β : typevec n} (f : α ⟹ β) : f ⊚ id = f := rfl theorem comp_assoc {α β γ δ : typevec n} (h : γ ⟹ δ) (g : β ⟹ γ) (f : α ⟹ β) : (h ⊚ g) ⊚ f = h ⊚ g ⊚ f := rfl /-- Support for extending a typevec by one element. -/ def append1 (α : typevec n) (β : Type*) : typevec (n+1) | (fin2.fs i) := α i | fin2.fz := β infixl (name := typevec.append1) ` ::: `:67 := append1 /-- retain only a `n-length` prefix of the argument -/ def drop (α : typevec.{u} (n+1)) : typevec n := λ i, α i.fs /-- take the last value of a `(n+1)-length` vector -/ def last (α : typevec.{u} (n+1)) : Type* := α fin2.fz instance last.inhabited (α : typevec (n+1)) [inhabited (α fin2.fz)] : inhabited (last α) := ⟨show α fin2.fz, from default⟩ theorem drop_append1 {α : typevec n} {β : Type*} {i : fin2 n} : drop (append1 α β) i = α i := rfl theorem drop_append1' {α : typevec n} {β : Type*} : drop (append1 α β) = α := by ext; apply drop_append1 theorem last_append1 {α : typevec n} {β : Type*} : last (append1 α β) = β := rfl @[simp] theorem append1_drop_last (α : typevec (n+1)) : append1 (drop α) (last α) = α := funext $ λ i, by cases i; refl /-- cases on `(n+1)-length` vectors -/ @[elab_as_eliminator] def append1_cases {C : typevec (n+1) → Sort u} (H : ∀ α β, C (append1 α β)) (γ) : C γ := by rw [← @append1_drop_last _ γ]; apply H @[simp] theorem append1_cases_append1 {C : typevec (n+1) → Sort u} (H : ∀ α β, C (append1 α β)) (α β) : @append1_cases _ C H (append1 α β) = H α β := rfl /-- append an arrow and a function for arbitrary source and target type vectors -/ def split_fun {α α' : typevec (n+1)} (f : drop α ⟹ drop α') (g : last α → last α') : α ⟹ α' | (fin2.fs i) := f i | fin2.fz := g /-- append an arrow and a function as well as their respective source and target types / typevecs -/ def append_fun {α α' : typevec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : append1 α β ⟹ append1 α' β' := split_fun f g infixl (name := typevec.append_fun) ` ::: ` := append_fun /-- split off the prefix of an arrow -/ def drop_fun {α β : typevec (n+1)} (f : α ⟹ β) : drop α ⟹ drop β := λ i, f i.fs /-- split off the last function of an arrow -/ def last_fun {α β : typevec (n+1)} (f : α ⟹ β) : last α → last β := f fin2.fz /-- arrow in the category of `0-length` vectors -/ def nil_fun {α : typevec 0} {β : typevec 0} : α ⟹ β := λ i, fin2.elim0 i theorem eq_of_drop_last_eq {α β : typevec (n+1)} {f g : α ⟹ β} (h₀ : drop_fun f = drop_fun g) (h₁ : last_fun f = last_fun g) : f = g := by replace h₀ := congr_fun h₀; ext1 ⟨⟩; apply_assumption @[simp] theorem drop_fun_split_fun {α α' : typevec (n+1)} (f : drop α ⟹ drop α') (g : last α → last α') : drop_fun (split_fun f g) = f := rfl /-- turn an equality into an arrow -/ def arrow.mp {α β : typevec n} (h : α = β) : α ⟹ β | i := eq.mp (congr_fun h _) /-- turn an equality into an arrow, with reverse direction -/ def arrow.mpr {α β : typevec n} (h : α = β) : β ⟹ α | i := eq.mpr (congr_fun h _) /-- decompose a vector into its prefix appended with its last element -/ def to_append1_drop_last {α : typevec (n+1)} : α ⟹ drop α ::: last α := arrow.mpr (append1_drop_last _) /-- stitch two bits of a vector back together -/ def from_append1_drop_last {α : typevec (n+1)} : drop α ::: last α ⟹ α := arrow.mp (append1_drop_last _) @[simp] theorem last_fun_split_fun {α α' : typevec (n+1)} (f : drop α ⟹ drop α') (g : last α → last α') : last_fun (split_fun f g) = g := rfl @[simp] theorem drop_fun_append_fun {α α' : typevec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : drop_fun (f ::: g) = f := rfl @[simp] theorem last_fun_append_fun {α α' : typevec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : last_fun (f ::: g) = g := rfl theorem split_drop_fun_last_fun {α α' : typevec (n+1)} (f : α ⟹ α') : split_fun (drop_fun f) (last_fun f) = f := eq_of_drop_last_eq rfl rfl theorem split_fun_inj {α α' : typevec (n+1)} {f f' : drop α ⟹ drop α'} {g g' : last α → last α'} (H : split_fun f g = split_fun f' g') : f = f' ∧ g = g' := by rw [← drop_fun_split_fun f g, H, ← last_fun_split_fun f g, H]; simp theorem append_fun_inj {α α' : typevec n} {β β' : Type*} {f f' : α ⟹ α'} {g g' : β → β'} : f ::: g = f' ::: g' → f = f' ∧ g = g' := split_fun_inj theorem split_fun_comp {α₀ α₁ α₂ : typevec (n+1)} (f₀ : drop α₀ ⟹ drop α₁) (f₁ : drop α₁ ⟹ drop α₂) (g₀ : last α₀ → last α₁) (g₁ : last α₁ → last α₂) : split_fun (f₁ ⊚ f₀) (g₁ ∘ g₀) = split_fun f₁ g₁ ⊚ split_fun f₀ g₀ := eq_of_drop_last_eq rfl rfl theorem append_fun_comp_split_fun {α γ : typevec n} {β δ : Type*} {ε : typevec (n + 1)} (f₀ : drop ε ⟹ α) (f₁ : α ⟹ γ) (g₀ : last ε → β) (g₁ : β → δ) : append_fun f₁ g₁ ⊚ split_fun f₀ g₀ = split_fun (f₁ ⊚ f₀) (g₁ ∘ g₀) := (split_fun_comp _ _ _ _).symm lemma append_fun_comp {α₀ α₁ α₂ : typevec n} {β₀ β₁ β₂ : Type*} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : f₁ ⊚ f₀ ::: g₁ ∘ g₀ = (f₁ ::: g₁) ⊚ (f₀ ::: g₀) := eq_of_drop_last_eq rfl rfl lemma append_fun_comp' {α₀ α₁ α₂ : typevec n} {β₀ β₁ β₂ : Type*} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : (f₁ ::: g₁) ⊚ (f₀ ::: g₀) = f₁ ⊚ f₀ ::: g₁ ∘ g₀ := eq_of_drop_last_eq rfl rfl lemma nil_fun_comp {α₀ : typevec 0} (f₀ : α₀ ⟹ fin2.elim0) : nil_fun ⊚ f₀ = f₀ := funext $ λ x, fin2.elim0 x theorem append_fun_comp_id {α : typevec n} {β₀ β₁ β₂ : Type*} (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : @id _ α ::: g₁ ∘ g₀ = (id ::: g₁) ⊚ (id ::: g₀) := eq_of_drop_last_eq rfl rfl @[simp] theorem drop_fun_comp {α₀ α₁ α₂ : typevec (n+1)} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) : drop_fun (f₁ ⊚ f₀) = drop_fun f₁ ⊚ drop_fun f₀ := rfl @[simp] theorem last_fun_comp {α₀ α₁ α₂ : typevec (n+1)} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) : last_fun (f₁ ⊚ f₀) = last_fun f₁ ∘ last_fun f₀ := rfl theorem append_fun_aux {α α' : typevec n} {β β' : Type*} (f : α ::: β ⟹ α' ::: β') : drop_fun f ::: last_fun f = f := eq_of_drop_last_eq rfl rfl theorem append_fun_id_id {α : typevec n} {β : Type*} : @typevec.id n α ::: @_root_.id β = typevec.id := eq_of_drop_last_eq rfl rfl instance subsingleton0 : subsingleton (typevec 0) := ⟨ λ a b, funext $ λ a, fin2.elim0 a ⟩ run_cmd do mk_simp_attr `typevec, tactic.add_doc_string `simp_attr.typevec "simp set for the manipulation of typevec and arrow expressions" local prefix `♯`:0 := cast (by try { simp }; congr' 1; try { simp }) /-- cases distinction for 0-length type vector -/ protected def cases_nil {β : typevec 0 → Sort*} (f : β fin2.elim0) : Π v, β v := λ v, ♯ f /-- cases distinction for (n+1)-length type vector -/ protected def cases_cons (n : ℕ) {β : typevec (n+1) → Sort*} (f : Π t (v : typevec n), β (v ::: t)) : Π v, β v := λ v : typevec (n+1), ♯ f v.last v.drop protected lemma cases_nil_append1 {β : typevec 0 → Sort*} (f : β fin2.elim0) : typevec.cases_nil f fin2.elim0 = f := rfl protected lemma cases_cons_append1 (n : ℕ) {β : typevec (n+1) → Sort*} (f : Π t (v : typevec n), β (v ::: t)) (v : typevec n) (α) : typevec.cases_cons n f (v ::: α) = f α v := rfl /-- cases distinction for an arrow in the category of 0-length type vectors -/ def typevec_cases_nil₃ {β : Π v v' : typevec 0, v ⟹ v' → Sort*} (f : β fin2.elim0 fin2.elim0 nil_fun) : Π v v' fs, β v v' fs := λ v v' fs, begin refine cast _ f; congr' 1; ext; try { intros; casesm fin2 0 }, refl end /-- cases distinction for an arrow in the category of (n+1)-length type vectors -/ def typevec_cases_cons₃ (n : ℕ) {β : Π v v' : typevec (n+1), v ⟹ v' → Sort*} (F : Π t t' (f : t → t') (v v' : typevec n) (fs : v ⟹ v'), β (v ::: t) (v' ::: t') (fs ::: f)) : Π v v' fs, β v v' fs := begin intros v v', rw [←append1_drop_last v, ←append1_drop_last v'], intro fs, rw [←split_drop_fun_last_fun fs], apply F end /-- specialized cases distinction for an arrow in the category of 0-length type vectors -/ def typevec_cases_nil₂ {β : fin2.elim0 ⟹ fin2.elim0 → Sort*} (f : β nil_fun) : Π f, β f := begin intro g, have : g = nil_fun, ext ⟨ ⟩, rw this, exact f end /-- specialized cases distinction for an arrow in the category of (n+1)-length type vectors -/ def typevec_cases_cons₂ (n : ℕ) (t t' : Type*) (v v' : typevec (n)) {β : (v ::: t) ⟹ (v' ::: t') → Sort*} (F : Π (f : t → t') (fs : v ⟹ v'), β (fs ::: f)) : Π fs, β fs := begin intro fs, rw [←split_drop_fun_last_fun fs], apply F end lemma typevec_cases_nil₂_append_fun {β : fin2.elim0 ⟹ fin2.elim0 → Sort*} (f : β nil_fun) : typevec_cases_nil₂ f nil_fun = f := rfl lemma typevec_cases_cons₂_append_fun (n : ℕ) (t t' : Type*) (v v' : typevec (n)) {β : (v ::: t) ⟹ (v' ::: t') → Sort*} (F : Π (f : t → t') (fs : v ⟹ v'), β (fs ::: f)) (f fs) : typevec_cases_cons₂ n t t' v v' F (fs ::: f) = F f fs := rfl /- for lifting predicates and relations -/ /-- `pred_last α p x` predicates `p` of the last element of `x : α.append1 β`. -/ def pred_last (α : typevec n) {β : Type*} (p : β → Prop) : Π ⦃i⦄, (α.append1 β) i → Prop | (fin2.fs i) := λ x, true | fin2.fz := p /-- `rel_last α r x y` says that `p` the last elements of `x y : α.append1 β` are related by `r` and all the other elements are equal. -/ def rel_last (α : typevec n) {β γ : Type*} (r : β → γ → Prop) : Π ⦃i⦄, (α.append1 β) i → (α.append1 γ) i → Prop | (fin2.fs i) := eq | fin2.fz := r section liftp' open nat /-- `repeat n t` is a `n-length` type vector that contains `n` occurences of `t` -/ def repeat : Π (n : ℕ) (t : Sort*), typevec n | 0 t := fin2.elim0 | (nat.succ i) t := append1 (repeat i t) t /-- `prod α β` is the pointwise product of the components of `α` and `β` -/ def prod : Π {n} (α β : typevec.{u} n), typevec n | 0 α β := fin2.elim0 | (n+1) α β := prod (drop α) (drop β) ::: (last α × last β) localized "infix (name := typevec.prod) ` ⊗ `:45 := typevec.prod" in mvfunctor /-- `const x α` is an arrow that ignores its source and constructs a `typevec` that contains nothing but `x` -/ protected def const {β} (x : β) : Π {n} (α : typevec n), α ⟹ repeat _ β | (succ n) α (fin2.fs i) := const (drop α) _ | (succ n) α fin2.fz := λ _, x open function (uncurry) /-- vector of equality on a product of vectors -/ def repeat_eq : Π {n} (α : typevec n), α ⊗ α ⟹ repeat _ Prop | 0 α := nil_fun | (succ n) α := repeat_eq (drop α) ::: uncurry eq lemma const_append1 {β γ} (x : γ) {n} (α : typevec n) : typevec.const x (α ::: β) = append_fun (typevec.const x α) (λ _, x) := by ext i : 1; cases i; refl lemma eq_nil_fun {α β : typevec 0} (f : α ⟹ β) : f = nil_fun := by ext x; cases x lemma id_eq_nil_fun {α : typevec 0} : @id _ α = nil_fun := by ext x; cases x lemma const_nil {β} (x : β) (α : typevec 0) : typevec.const x α = nil_fun := by ext i : 1; cases i; refl @[typevec] lemma repeat_eq_append1 {β} {n} (α : typevec n) : repeat_eq (α ::: β) = split_fun (repeat_eq α) (uncurry eq) := by induction n; refl @[typevec] lemma repeat_eq_nil (α : typevec 0) : repeat_eq α = nil_fun := by ext i : 1; cases i; refl /-- predicate on a type vector to constrain only the last object -/ def pred_last' (α : typevec n) {β : Type*} (p : β → Prop) : α ::: β ⟹ repeat (n+1) Prop := split_fun (typevec.const true α) p /-- predicate on the product of two type vectors to constrain only their last object -/ def rel_last' (α : typevec n) {β : Type*} (p : β → β → Prop) : (α ::: β ⊗ α ::: β) ⟹ repeat (n+1) Prop := split_fun (repeat_eq α) (uncurry p) /-- given `F : typevec.{u} (n+1) → Type u`, `curry F : Type u → typevec.{u} → Type u`, i.e. its first argument can be fed in separately from the rest of the vector of arguments -/ def curry (F : typevec.{u} (n+1) → Type*) (α : Type u) (β : typevec.{u} n) : Type* := F (β ::: α) instance curry.inhabited (F : typevec.{u} (n+1) → Type*) (α : Type u) (β : typevec.{u} n) [I : inhabited (F $ β ::: α)]: inhabited (curry F α β) := I /-- arrow to remove one element of a `repeat` vector -/ def drop_repeat (α : Type*) : Π {n}, drop (repeat (succ n) α) ⟹ repeat n α | (succ n) (fin2.fs i) := drop_repeat i | (succ n) fin2.fz := _root_.id /-- projection for a repeat vector -/ def of_repeat {α : Sort*} : Π {n i}, repeat n α i → α | ._ fin2.fz := _root_.id | ._ (fin2.fs i) := @of_repeat _ i lemma const_iff_true {α : typevec n} {i x p} : of_repeat (typevec.const p α i x) ↔ p := by induction i; [refl, erw [typevec.const,@i_ih (drop α) x]] -- variables {F : typevec.{u} n → Type*} [mvfunctor F] variables {α β γ : typevec.{u} n} variables (p : α ⟹ repeat n Prop) (r : α ⊗ α ⟹ repeat n Prop) /-- left projection of a `prod` vector -/ def prod.fst : Π {n} {α β : typevec.{u} n}, α ⊗ β ⟹ α | (succ n) α β (fin2.fs i) := @prod.fst _ (drop α) (drop β) i | (succ n) α β fin2.fz := _root_.prod.fst /-- right projection of a `prod` vector -/ def prod.snd : Π {n} {α β : typevec.{u} n}, α ⊗ β ⟹ β | (succ n) α β (fin2.fs i) := @prod.snd _ (drop α) (drop β) i | (succ n) α β fin2.fz := _root_.prod.snd /-- introduce a product where both components are the same -/ def prod.diag : Π {n} {α : typevec.{u} n}, α ⟹ α ⊗ α | (succ n) α (fin2.fs i) x := @prod.diag _ (drop α) _ x | (succ n) α fin2.fz x := (x,x) /-- constructor for `prod` -/ def prod.mk : Π {n} {α β : typevec.{u} n} (i : fin2 n), α i → β i → (α ⊗ β) i | (succ n) α β (fin2.fs i) := prod.mk i | (succ n) α β fin2.fz := _root_.prod.mk @[simp] lemma prod_fst_mk {α β : typevec n} (i : fin2 n) (a : α i) (b : β i) : typevec.prod.fst i (prod.mk i a b) = a := by induction i; simp [prod.fst, prod.mk, *] at * @[simp] lemma prod_snd_mk {α β : typevec n} (i : fin2 n) (a : α i) (b : β i) : typevec.prod.snd i (prod.mk i a b) = b := by induction i; simp [prod.snd, prod.mk, *] at * /-- `prod` is functorial -/ protected def prod.map : Π {n} {α α' β β' : typevec.{u} n}, (α ⟹ β) → (α' ⟹ β') → α ⊗ α' ⟹ β ⊗ β' | (succ n) α α' β β' x y (fin2.fs i) a := @prod.map _ (drop α) (drop α') (drop β) (drop β') (drop_fun x) (drop_fun y) _ a | (succ n) α α' β β' x y fin2.fz a := (x _ a.1,y _ a.2) localized "infix (name := typevec.prod.map) ` ⊗' `:45 := typevec.prod.map" in mvfunctor theorem fst_prod_mk {α α' β β' : typevec n} (f : α ⟹ β) (g : α' ⟹ β') : typevec.prod.fst ⊚ (f ⊗' g) = f ⊚ typevec.prod.fst := by ext i; induction i; [refl, apply i_ih] theorem snd_prod_mk {α α' β β' : typevec n} (f : α ⟹ β) (g : α' ⟹ β') : typevec.prod.snd ⊚ (f ⊗' g) = g ⊚ typevec.prod.snd := by ext i; induction i; [refl, apply i_ih] theorem fst_diag {α : typevec n} : typevec.prod.fst ⊚ (prod.diag : α ⟹ _) = id := by ext i; induction i; [refl, apply i_ih] theorem snd_diag {α : typevec n} : typevec.prod.snd ⊚ (prod.diag : α ⟹ _) = id := by ext i; induction i; [refl, apply i_ih] lemma repeat_eq_iff_eq {α : typevec n} {i x y} : of_repeat (repeat_eq α i (prod.mk _ x y)) ↔ x = y := by induction i; [refl, erw [repeat_eq,@i_ih (drop α) x y]] /-- given a predicate vector `p` over vector `α`, `subtype_ p` is the type of vectors that contain an `α` that satisfies `p` -/ def subtype_ : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), typevec n | ._ α p fin2.fz := _root_.subtype (λ x, p fin2.fz x) | ._ α p (fin2.fs i) := subtype_ (drop_fun p) i /-- projection on `subtype_` -/ def subtype_val : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), subtype_ p ⟹ α | (succ n) α p (fin2.fs i) := @subtype_val n _ _ i | (succ n) α p fin2.fz := _root_.subtype.val /-- arrow that rearranges the type of `subtype_` to turn a subtype of vector into a vector of subtypes -/ def to_subtype : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), (λ (i : fin2 n), { x // of_repeat $ p i x }) ⟹ subtype_ p | (succ n) α p (fin2.fs i) x := to_subtype (drop_fun p) i x | (succ n) α p fin2.fz x := x /-- arrow that rearranges the type of `subtype_` to turn a vector of subtypes into a subtype of vector -/ def of_subtype : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), subtype_ p ⟹ (λ (i : fin2 n), { x // of_repeat $ p i x }) | (succ n) α p (fin2.fs i) x := of_subtype _ i x | (succ n) α p fin2.fz x := x /-- similar to `to_subtype` adapted to relations (i.e. predicate on product) -/ def to_subtype' : Π {n} {α : typevec.{u} n} (p : α ⊗ α ⟹ repeat n Prop), (λ (i : fin2 n), { x : α i × α i // of_repeat $ p i (prod.mk _ x.1 x.2) }) ⟹ subtype_ p | (succ n) α p (fin2.fs i) x := to_subtype' (drop_fun p) i x | (succ n) α p fin2.fz x := ⟨x.val,cast (by congr; simp [prod.mk]) x.property⟩ /-- similar to `of_subtype` adapted to relations (i.e. predicate on product) -/ def of_subtype' : Π {n} {α : typevec.{u} n} (p : α ⊗ α ⟹ repeat n Prop), subtype_ p ⟹ (λ (i : fin2 n), { x : α i × α i // of_repeat $ p i (prod.mk _ x.1 x.2) }) | ._ α p (fin2.fs i) x := of_subtype' _ i x | ._ α p fin2.fz x := ⟨x.val,cast (by congr; simp [prod.mk]) x.property⟩ /-- similar to `diag` but the target vector is a `subtype_` guaranteeing the equality of the components -/ def diag_sub : Π {n} {α : typevec.{u} n}, α ⟹ subtype_ (repeat_eq α) | (succ n) α (fin2.fs i) x := @diag_sub _ (drop α) _ x | (succ n) α fin2.fz x := ⟨(x,x), rfl⟩ lemma subtype_val_nil {α : typevec.{u} 0} (ps : α ⟹ repeat 0 Prop) : typevec.subtype_val ps = nil_fun := funext $ by rintro ⟨ ⟩; refl lemma diag_sub_val {n} {α : typevec.{u} n} : subtype_val (repeat_eq α) ⊚ diag_sub = prod.diag := by ext i; induction i; [refl, apply i_ih] lemma prod_id : Π {n} {α β : typevec.{u} n}, (id ⊗' id) = (id : α ⊗ β ⟹ _) := begin intros, ext i a, induction i, { cases a, refl }, { apply i_ih }, end lemma append_prod_append_fun {n} {α α' β β' : typevec.{u} n} {φ φ' ψ ψ' : Type u} {f₀ : α ⟹ α'} {g₀ : β ⟹ β'} {f₁ : φ → φ'} {g₁ : ψ → ψ'} : (f₀ ⊗' g₀) ::: _root_.prod.map f₁ g₁ = ((f₀ ::: f₁) ⊗' (g₀ ::: g₁)) := by ext i a; cases i; [cases a, skip]; refl end liftp' @[simp] lemma drop_fun_diag {α} : drop_fun (@prod.diag (n+1) α) = prod.diag := by { ext i : 2, induction i; simp [drop_fun, *]; refl } @[simp] lemma drop_fun_subtype_val {α} (p : α ⟹ repeat (n+1) Prop) : drop_fun (subtype_val p) = subtype_val _ := rfl @[simp] lemma last_fun_subtype_val {α} (p : α ⟹ repeat (n+1) Prop) : last_fun (subtype_val p) = subtype.val := rfl @[simp] lemma drop_fun_to_subtype {α} (p : α ⟹ repeat (n+1) Prop) : drop_fun (to_subtype p) = to_subtype _ := by { ext i : 2, induction i; simp [drop_fun, *]; refl } @[simp] lemma last_fun_to_subtype {α} (p : α ⟹ repeat (n+1) Prop) : last_fun (to_subtype p) = _root_.id := by { ext i : 2, induction i; simp [drop_fun, *]; refl } @[simp] lemma drop_fun_of_subtype {α} (p : α ⟹ repeat (n+1) Prop) : drop_fun (of_subtype p) = of_subtype _ := by { ext i : 2, induction i; simp [drop_fun, *]; refl } @[simp] lemma last_fun_of_subtype {α} (p : α ⟹ repeat (n+1) Prop) : last_fun (of_subtype p) = _root_.id := by { ext i : 2, induction i; simp [drop_fun, *]; refl } @[simp] lemma drop_fun_rel_last {α : typevec n} {β} (R : β → β → Prop) : drop_fun (rel_last' α R) = repeat_eq α := rfl attribute [simp] drop_append1' open_locale mvfunctor @[simp] lemma drop_fun_prod {α α' β β' : typevec (n+1)} (f : α ⟹ β) (f' : α' ⟹ β') : drop_fun (f ⊗' f') = (drop_fun f ⊗' drop_fun f') := by { ext i : 2, induction i; simp [drop_fun, *]; refl } @[simp] lemma last_fun_prod {α α' β β' : typevec (n+1)} (f : α ⟹ β) (f' : α' ⟹ β') : last_fun (f ⊗' f') = _root_.prod.map (last_fun f) (last_fun f') := by { ext i : 1, induction i; simp [last_fun, *]; refl } @[simp] lemma drop_fun_from_append1_drop_last {α : typevec (n+1)} : drop_fun (@from_append1_drop_last _ α) = id := rfl @[simp] lemma last_fun_from_append1_drop_last {α : typevec (n+1)} : last_fun (@from_append1_drop_last _ α) = _root_.id := rfl @[simp] lemma drop_fun_id {α : typevec (n+1)} : drop_fun (@typevec.id _ α) = id := rfl @[simp] lemma prod_map_id {α β : typevec n} : (@typevec.id _ α ⊗' @typevec.id _ β) = id := by { ext i : 2, induction i; simp only [typevec.prod.map, *, drop_fun_id], cases x, refl, refl } @[simp] lemma subtype_val_diag_sub {α : typevec n} : subtype_val (repeat_eq α) ⊚ diag_sub = prod.diag := by { clear_except, ext i, induction i; [refl, apply i_ih], } @[simp] lemma to_subtype_of_subtype {α : typevec n} (p : α ⟹ repeat n Prop) : to_subtype p ⊚ of_subtype p = id := by ext i x; induction i; dsimp only [id, to_subtype, comp, of_subtype] at *; simp * @[simp] lemma subtype_val_to_subtype {α : typevec n} (p : α ⟹ repeat n Prop) : subtype_val p ⊚ to_subtype p = λ _, subtype.val := by ext i x; induction i; dsimp only [to_subtype, comp, subtype_val] at *; simp * @[simp] lemma to_subtype_of_subtype_assoc {α β : typevec n} (p : α ⟹ repeat n Prop) (f : β ⟹ subtype_ p) : @to_subtype n _ p ⊚ of_subtype _ ⊚ f = f := by rw [← comp_assoc,to_subtype_of_subtype]; simp @[simp] lemma to_subtype'_of_subtype' {α : typevec n} (r : α ⊗ α ⟹ repeat n Prop) : to_subtype' r ⊚ of_subtype' r = id := by ext i x; induction i; dsimp only [id, to_subtype', comp, of_subtype'] at *; simp [subtype.eta, *] lemma subtype_val_to_subtype' {α : typevec n} (r : α ⊗ α ⟹ repeat n Prop) : subtype_val r ⊚ to_subtype' r = λ i x, prod.mk i x.1.fst x.1.snd := by ext i x; induction i; dsimp only [id, to_subtype', comp, subtype_val, prod.mk] at *; simp * end typevec
5bcff3b997bfe19a70264baa81632f0d00dbc107
4727251e0cd73359b15b664c3170e5d754078599
/src/data/mv_polynomial/pderiv.lean
4cd9c1e64069746defdd53adf4010717b3a21c0b
[ "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
3,304
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam, Yury Kudryashov -/ import data.mv_polynomial.variables import data.mv_polynomial.derivation /-! # Partial derivatives of polynomials This file defines the notion of the formal *partial derivative* of a polynomial, the derivative with respect to a single variable. This derivative is not connected to the notion of derivative from analysis. It is based purely on the polynomial exponents and coefficients. ## Main declarations * `mv_polynomial.pderiv i p` : the partial derivative of `p` with respect to `i`, as a bundled derivation of `mv_polynomial σ R`. ## Notation As in other polynomial files, we typically use the notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_ring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` -/ noncomputable theory universes u v namespace mv_polynomial open set function finsupp add_monoid_algebra open_locale classical big_operators variables {R : Type u} {σ : Type v} {a a' a₁ a₂ : R} {s : σ →₀ ℕ} section pderiv variables {R} [comm_semiring R] /-- `pderiv i p` is the partial derivative of `p` with respect to `i` -/ def pderiv (i : σ) : derivation R (mv_polynomial σ R) (mv_polynomial σ R) := mk_derivation R $ pi.single i 1 @[simp] lemma pderiv_monomial {i : σ} : pderiv i (monomial s a) = monomial (s - single i 1) (a * (s i)) := begin simp only [pderiv, mk_derivation_monomial, finsupp.smul_sum, smul_eq_mul, ← smul_mul_assoc, ← (monomial _).map_smul], refine (finset.sum_eq_single i (λ j hj hne, _) (λ hi, _)).trans _, { simp [pi.single_eq_of_ne hne] }, { rw [finsupp.not_mem_support_iff] at hi, simp [hi] }, { simp } end lemma pderiv_C {i : σ} : pderiv i (C a) = 0 := derivation_C _ _ lemma pderiv_one {i : σ} : pderiv i (1 : mv_polynomial σ R) = 0 := pderiv_C @[simp] lemma pderiv_X [d : decidable_eq σ] (i j : σ) : pderiv i (X j : mv_polynomial σ R) = @pi.single σ _ d _ i 1 j := (mk_derivation_X _ _ _).trans (by congr) @[simp] lemma pderiv_X_self (i : σ) : pderiv i (X i : mv_polynomial σ R) = 1 := by simp @[simp] lemma pderiv_X_of_ne {i j : σ} (h : j ≠ i) : pderiv i (X j : mv_polynomial σ R) = 0 := by simp [h] lemma pderiv_eq_zero_of_not_mem_vars {i : σ} {f : mv_polynomial σ R} (h : i ∉ f.vars) : pderiv i f = 0 := derivation_eq_zero_of_forall_mem_vars $ λ j hj, pderiv_X_of_ne $ ne_of_mem_of_not_mem hj h lemma pderiv_monomial_single {i : σ} {n : ℕ} : pderiv i (monomial (single i n) a) = monomial (single i (n-1)) (a * n) := by simp lemma pderiv_mul {i : σ} {f g : mv_polynomial σ R} : pderiv i (f * g) = pderiv i f * g + f * pderiv i g := by simp only [(pderiv i).leibniz f g, smul_eq_mul, mul_comm, add_comm] @[simp] lemma pderiv_C_mul {f : mv_polynomial σ R} {i : σ} : pderiv i (C a * f) = C a * pderiv i f := (derivation_C_mul _ _ _).trans C_mul'.symm end pderiv end mv_polynomial
a9bb525e10c0d8e703343bc45519bfa683670347
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/bad_print.lean
b66b451600dcf42533fe571662dc95a2ae0d704a
[ "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
87
lean
constant boo : nat #print definition boo #print 2 #print fields nat #print nat.addd
6ee0650365e960825a3197a0b8b2d8eb02ff6c63
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_425.lean
e9ddd042bf953e9f57475faebdd5dfec3e051e70
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
231
lean
import data.set.basic data.nat.parity open set nat def evens : set ℕ := {n | even n} def odds : set ℕ := {n | ¬ even n} example : evens ∪ odds = univ := begin rw [evens, odds], ext n, simp, apply classical.em end
304d54fba6370a6c14b7180c8d4bd970f52520ca
8b9f17008684d796c8022dab552e42f0cb6fb347
/hott/algebra/binary.hlean
9645529e0198b2f7881edec4a772f5282297e698
[ "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
2,263
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.binary Authors: Leonardo de Moura, Jeremy Avigad General properties of binary operations. -/ open eq namespace binary section variable {A : Type} variables (op₁ : A → A → A) (inv : A → A) (one : A) local notation a * b := op₁ a b local notation a ⁻¹ := inv a local notation 1 := one definition commutative := ∀a b, a*b = b*a definition associative := ∀a b c, (a*b)*c = a*(b*c) definition left_identity := ∀a, 1 * a = a definition right_identity := ∀a, a * 1 = a definition left_inverse := ∀a, a⁻¹ * a = 1 definition right_inverse := ∀a, a * a⁻¹ = 1 definition left_cancelative := ∀a b c, a * b = a * c → b = c definition right_cancelative := ∀a b c, a * b = c * b → a = c definition inv_op_cancel_left := ∀a b, a⁻¹ * (a * b) = b definition op_inv_cancel_left := ∀a b, a * (a⁻¹ * b) = b definition inv_op_cancel_right := ∀a b, a * b⁻¹ * b = a definition op_inv_cancel_right := ∀a b, a * b * b⁻¹ = a variable (op₂ : A → A → A) local notation a + b := op₂ a b definition left_distributive := ∀a b c, a * (b + c) = a * b + a * c definition right_distributive := ∀a b c, (a + b) * c = a * c + b * c end section variable {A : Type} variable {f : A → A → A} variable H_comm : commutative f variable H_assoc : associative f local infixl `*` := f theorem left_comm : ∀a b c, a*(b*c) = b*(a*c) := take a b c, calc a*(b*c) = (a*b)*c : H_assoc ... = (b*a)*c : H_comm ... = b*(a*c) : H_assoc theorem right_comm : ∀a b c, (a*b)*c = (a*c)*b := take a b c, calc (a*b)*c = a*(b*c) : H_assoc ... = a*(c*b) : H_comm ... = (a*c)*b : H_assoc end section variable {A : Type} variable {f : A → A → A} variable H_assoc : associative f local infixl `*` := f theorem assoc4helper (a b c d) : (a*b)*(c*d) = a*((b*c)*d) := calc (a*b)*(c*d) = a*(b*(c*d)) : H_assoc ... = a*((b*c)*d) : H_assoc end end binary
abfbd34bfb08eb369a4053b56c7b9893927cf1ce
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/analysis/convex/topology.lean
49f858cffc9be67bdfa12cd22075368aa50a5fba
[ "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
7,620
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import analysis.convex.basic analysis.normed_space.finite_dimension /-! # Topological and metric properties of convex sets We prove the following facts: * `convex.interior` : interior of a convex set is convex; * `convex.closure` : closure of a convex set is convex; * `set.finite.compact_convex_hull` : convex hull of a finite set is compact; * `set.finite.is_closed_convex_hull` : convex hull of a finite set is closed; * `convex_on_dist` : distance to a fixed point is convex on any convex set; * `convex_hull_ediam`, `convex_hull_diam` : convex hull of a set has the same (e)metric diameter as the original set; * `bounded_convex_hull` : convex hull of a set is bounded if and only if the original set is bounded. * `bounded_std_simplex`, `is_closed_std_simplex`, `compact_std_simplex`: topological properties of the standard simplex; -/ variables {ι : Type*} {E : Type*} open set /-! ### Standard simplex -/ section std_simplex variables [fintype ι] /-- Every vector in `std_simplex ι` has `max`-norm at most `1`. -/ lemma std_simplex_subset_closed_ball : std_simplex ι ⊆ metric.closed_ball 0 1 := begin assume f hf, rw [metric.mem_closed_ball, dist_zero_right], refine (nnreal.coe_one ▸ nnreal.coe_le_coe.2 $ finset.sup_le $ λ x hx, _), change abs (f x) ≤ 1, rw [abs_of_nonneg $ hf.1 x], exact (mem_Icc_of_mem_std_simplex hf x).2 end variable (ι) /-- `std_simplex ι` is bounded. -/ lemma bounded_std_simplex : metric.bounded (std_simplex ι) := (metric.bounded_iff_subset_ball 0).2 ⟨1, std_simplex_subset_closed_ball⟩ /-- `std_simplex ι` is closed. -/ lemma is_closed_std_simplex : is_closed (std_simplex ι) := (std_simplex_eq_inter ι).symm ▸ is_closed_inter (is_closed_Inter $ λ i, is_closed_le continuous_const (continuous_apply i)) (is_closed_eq (continuous_finset_sum _ $ λ x _, continuous_apply x) continuous_const) /-- `std_simplex ι` is compact. -/ lemma compact_std_simplex : compact (std_simplex ι) := metric.compact_iff_closed_bounded.2 ⟨is_closed_std_simplex ι, bounded_std_simplex ι⟩ end std_simplex /-! ### Topological vector space -/ section topological_vector_space variables [add_comm_group E] [vector_space ℝ E] [topological_space E] [topological_add_group E] [topological_vector_space ℝ E] local attribute [instance] set.pointwise_add set.smul_set /-- In a topological vector space, the interior of a convex set is convex. -/ lemma convex.interior {s : set E} (hs : convex s) : convex (interior s) := convex_iff_pointwise_add_subset.mpr $ λ a b ha hb hab, have h : is_open (a • interior s + b • interior s), from or.elim (classical.em (a = 0)) (λ heq, have hne : b ≠ 0, by { rw [heq, zero_add] at hab, rw hab, exact one_ne_zero }, (smul_set_eq_image b (interior s)).symm ▸ (is_open_pointwise_add_left ((is_open_map_smul_of_ne_zero hne _) is_open_interior))) (λ hne, (smul_set_eq_image a (interior s)).symm ▸ (is_open_pointwise_add_right ((is_open_map_smul_of_ne_zero hne _) is_open_interior))), (subset_interior_iff_subset_of_open h).mpr $ subset.trans begin apply pointwise_add_subset_add; rw [smul_set_eq_image, smul_set_eq_image]; exact image_subset _ interior_subset end (convex_iff_pointwise_add_subset.mp hs ha hb hab) /-- In a topological vector space, the closure of a convex set is convex. -/ lemma convex.closure {s : set E} (hs : convex s) : convex (closure s) := λ x y hx hy a b ha hb hab, let f : E → E → E := λ x' y', a • x' + b • y' in have hf : continuous (λ p : E × E, f p.1 p.2), from (continuous_const.smul continuous_fst).add (continuous_const.smul continuous_snd), show f x y ∈ closure s, from mem_closure_of_continuous2 hf hx hy (λ x' hx' y' hy', subset_closure (hs hx' hy' ha hb hab)) /-- Convex hull of a finite set is compact. -/ lemma set.finite.compact_convex_hull {s : set E} (hs : finite s) : compact (convex_hull s) := begin rw [hs.convex_hull_eq_image], apply (compact_std_simplex _).image, haveI := hs.fintype, apply linear_map.continuous_on_pi end /-- Convex hull of a finite set is closed. -/ lemma set.finite.is_closed_convex_hull [t2_space E] {s : set E} (hs : finite s) : is_closed (convex_hull s) := closed_of_compact _ hs.compact_convex_hull end topological_vector_space /-! ### Normed vector space -/ section normed_space variables [normed_group E] [normed_space ℝ E] lemma convex_on_dist (z : E) (s : set E) (hs : convex s) : convex_on s (λz', dist z' z) := and.intro hs $ assume x y hx hy a b ha hb hab, calc dist (a • x + b • y) z = ∥ (a • x + b • y) - (a + b) • z ∥ : by rw [hab, one_smul, normed_group.dist_eq] ... = ∥a • (x - z) + b • (y - z)∥ : by rw [add_smul, smul_sub, smul_sub, sub_eq_add_neg, sub_eq_add_neg, sub_eq_add_neg, neg_add, ←add_assoc, add_assoc (a • x), add_comm (b • y)]; simp only [add_assoc] ... ≤ ∥a • (x - z)∥ + ∥b • (y - z)∥ : norm_add_le (a • (x - z)) (b • (y - z)) ... = a * dist x z + b * dist y z : by simp [norm_smul, normed_group.dist_eq, real.norm_eq_abs, abs_of_nonneg ha, abs_of_nonneg hb] lemma convex_ball (a : E) (r : ℝ) : convex (metric.ball a r) := by simpa only [metric.ball, sep_univ] using (convex_on_dist a _ convex_univ).convex_lt r lemma convex_closed_ball (a : E) (r : ℝ) : convex (metric.closed_ball a r) := by simpa only [metric.closed_ball, sep_univ] using (convex_on_dist a _ convex_univ).convex_le r /-- Given a point `x` in the convex hull of `s` and a point `y`, there exists a point of `s` at distance at least `dist x y` from `y`. -/ lemma convex_hull_exists_dist_ge {s : set E} {x : E} (hx : x ∈ convex_hull s) (y : E) : ∃ x' ∈ s, dist x y ≤ dist x' y := (convex_on_dist y _ (convex_convex_hull _)).exists_ge_of_mem_convex_hull hx /-- Given a point `x` in the convex hull of `s` and a point `y` in the convex hull of `t`, there exist points `x' ∈ s` and `y' ∈ t` at distance at least `dist x y`. -/ lemma convex_hull_exists_dist_ge2 {s t : set E} {x y : E} (hx : x ∈ convex_hull s) (hy : y ∈ convex_hull t) : ∃ (x' ∈ s) (y' ∈ t), dist x y ≤ dist x' y' := begin rcases convex_hull_exists_dist_ge hx y with ⟨x', hx', Hx'⟩, rcases convex_hull_exists_dist_ge hy x' with ⟨y', hy', Hy'⟩, use [x', hx', y', hy'], exact le_trans Hx' (dist_comm y x' ▸ dist_comm y' x' ▸ Hy') end /-- Emetric diameter of the convex hull of a set `s` equals the emetric diameter of `s. -/ @[simp] lemma convex_hull_ediam (s : set E) : emetric.diam (convex_hull s) = emetric.diam s := begin refine le_antisymm (emetric.diam_le_of_forall_edist_le $ λ x hx y hy, _) (emetric.diam_mono $ subset_convex_hull s), rcases convex_hull_exists_dist_ge2 hx hy with ⟨x', hx', y', hy', H⟩, rw edist_dist, apply le_trans (ennreal.of_real_le_of_real H), rw ← edist_dist, exact emetric.edist_le_diam_of_mem hx' hy' end /-- Diameter of the convex hull of a set `s` equals the emetric diameter of `s. -/ @[simp] lemma convex_hull_diam (s : set E) : metric.diam (convex_hull s) = metric.diam s := by simp only [metric.diam, convex_hull_ediam] /-- Convex hull of `s` is bounded if and only if `s` is bounded. -/ @[simp] lemma bounded_convex_hull {s : set E} : metric.bounded (convex_hull s) ↔ metric.bounded s := by simp only [metric.bounded_iff_ediam_ne_top, convex_hull_ediam] end normed_space
335ba7c2c645a5775107897fad090865d1395bdb
8cd68b0e4eb405ef573e16a6d92dcbcdb92d1c29
/tests/lean/run/parser_itactic.lean
2a3a7c7b084db42f3efbac71195573d316b8bf0a
[ "Apache-2.0" ]
permissive
ratmice/lean
5d7a7bbaa652899941fe73dff2154ddc5ab2f20a
139ac0d773dbf0f54cc682612bf8f02297c211dd
refs/heads/master
1,590,259,859,627
1,557,951,491,000
1,558,099,319,000
186,896,142
0
0
Apache-2.0
1,557,951,143,000
1,557,951,143,000
null
UTF-8
Lean
false
false
640
lean
open tactic namespace tactic.interactive meta def my_tac (p : interactive.parse lean.parser.itactic) : tactic unit := p meta def my_tac_optional (p : interactive.parse (optional lean.parser.itactic)) : tactic unit := match p with | some p := p | none := tactic.trace "nothing" end end tactic.interactive meta def quote_tac : tactic unit := `[my_tac {tactic.trace "hi2"}] meta def quote_tac_optional : tactic unit := `[my_tac_optional] example : true := begin my_tac {tactic.trace "hi"}, my_tac_optional {tactic.trace "hi"}, my_tac_optional {}, my_tac_optional, quote_tac, quote_tac_optional, trivial end
df390048825d4fbe93bd8668da4dd82fe89f6fc5
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/02_Dependent_Type_Theory.org.42.lean
b03d71556f7bbf471703dacad090dbe3122d716c
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
276
lean
/- page 31 -/ import standard definition id {A : Type} (x : A) := x variables A B : Type variables (a : A) (b : B) -- BEGIN check @id -- Π {A : Type}, A → A check @id A -- A → A check @id B -- B → B check @id A a -- A check @id B b -- B -- END
32eabb74827b18ae19d5eefc85ea7438a39b7900
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/481.hlean
48b156c7eb51c64a58caf7b71aea4d1c92e5e20f
[ "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
184
hlean
open equiv eq is_equiv is_trunc definition foo {A B : Type} (f : A ≃ B) (f' : A → B) (H' : is_equiv f') (p : to_fun f = f') : p = p := begin cases p, esimp end
92c4fbebb611fe59a9c78c6c1a7389b1b63e2298
77d4797caedd029b8264d51f1f0dfbb0b030558d
/src/mario_sheaf.lean
204cc1139d48c243b7a06afb2c4efeeb84a89dec
[]
no_license
argoopjmc/xena
fbad1d671be7a1b03d03217f6e718239c546a3dc
4b05fb3ef617dbc1080744c39027207846abf273
refs/heads/master
1,606,553,558,993
1,577,046,319,000
1,577,046,319,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,647
lean
/- Presheaf (of types). https://stacks.math.columbia.edu/tag/006D -/ import topology.basic import topology.opens import order.bounds universes u v -- Definition of a presheaf. open topological_space lattice structure presheaf (α : Type u) [semilattice_inf α] := (F : α → Type v) (res : ∀ (U V) (HVU : V ≤ U), F U → F V) (Hid : ∀ (U), res U U (le_refl U) = id) (Hcomp : ∀ (U V W) (HWV : W ≤ V) (HVU : V ≤ U), res U W (le_trans HWV HVU) = res V W HWV ∘ res U V HVU) namespace presheaf variables {α : Type u} [semilattice_inf α] instance : has_coe_to_fun (presheaf α) := { F := λ _, α → Type v, coe := presheaf.F } -- Simplification lemmas for Hid and Hcomp. @[simp] lemma Hcomp' (F : presheaf α) : ∀ (U V W) (HWV : W ≤ V) (HVU : V ≤ U) (s : F U), (F.res U W (le_trans HWV HVU)) s = (F.res V W HWV) ((F.res U V HVU) s) := λ U V W HWV HVU s, by rw F.Hcomp U V W HWV HVU @[simp] lemma Hid' (F : presheaf α) : ∀ (U) (s : F U), (F.res U U (le_refl U)) s = s := λ U s, by rw F.Hid U; simp def total (F : presheaf α) : Type (max u v) := Σ U, F.F U instance (F : presheaf α) (U : α) : has_coe_t (F U) F.total := ⟨sigma.mk _⟩ @[elab_as_eliminator] theorem total.cases_on (F : presheaf α) {C : F.total → Sort*} (x) (H : ∀ U (x : F U), C x) : C x := by cases x; apply H def res' (F : presheaf α) (V : α) : F.total → F.total | ⟨U, x⟩ := F.res U (U ⊓ V) inf_le_left x theorem res'_def (F : presheaf α) {U V} (x : F U) : F.res' V x = F.res U (U ⊓ V) inf_le_left x := rfl theorem res'_val (F : presheaf α) {U V} (x : F U) (h : V ≤ U) : F.res' V x = F.res U V h x := have ∀ W (H : W ≤ U), W = V → (F.res U W H x : F.total) = F.res U V h x := by rintro _ _ rfl; refl, this _ _ (inf_of_le_right h) theorem res'_eq_inf (F : presheaf α) {U V} (x : F U) : F.res' V x = F.res' (U ⊓ V) x := by rw [res'_def, ← res'_val _ _ inf_le_left] theorem res'_eq_left (F : presheaf α) {U V W} (x : F U) (H : U ⊓ V = U ⊓ W) : F.res' V x = F.res' W x := by rw [res'_eq_inf, H, ← res'_eq_inf] @[simp] theorem res'_id {F : presheaf α} {U} (x : F U) : F.res' U x = x := by rw [res'_val _ _ (le_refl U), F.Hid]; refl @[simp] theorem res'_comp {F : presheaf α} {U V} (x : F.total) : F.res' U (F.res' V x) = F.res' (U ⊓ V) x := total.cases_on F x $ λ W x, by rw [res'_def, res'_def, ← F.Hcomp', ← res'_val, res'_eq_left]; simp [inf_left_comm, inf_comm] -- Morphism of presheaves. -- Kenny wants morphisms on the subsheaves structure morphism (F G : presheaf α) := (map : ∀ (U), F U → G U) (commutes : ∀ (U V) (HVU : V ≤ U), (G.res U V HVU) ∘ (map U) = (map V) ∘ (F.res U V HVU)) infix ` ⟶ `:80 := morphism section morphism def comp {F G H : presheaf α} (fg : F ⟶ G) (gh : G ⟶ H) : F ⟶ H := { map := λ U, gh.map U ∘ fg.map U, commutes := λ U V HVU, begin rw [←function.comp.assoc, gh.commutes U V HVU], symmetry, rw [function.comp.assoc, ←fg.commutes U V HVU] end } infix ` ⊚ `:80 := comp def id (F : presheaf α) : F ⟶ F := { map := λ U, id, commutes := λ U V HVU, by simp, } structure iso (F G : presheaf α) := (mor : F ⟶ G) (inv : G ⟶ F) (mor_inv_id : mor ⊚ inv = id F) (inv_mor_id : inv ⊚ mor = id G) infix ` ≅ `:80 := iso end morphism section sheaf_condition -- Sheaf condition. def locality (F : presheaf α) := ∀ {{U S}}, is_lub S U → ∀ {{s t : F U}}, (∀ V ∈ S, F.res' V s = F.res' V t) → s = t def gluing (F : presheaf α) := ∀ {{U : α}} {{S}}, is_lub S U → ∀ (s : Π V : S, F V), (∀ V W : S, res' F (V ⊓ W) (s V) = res' F (V ⊓ W) (s W)) → ∃ x : F U, ∀ V:S, F.res' V x = s V def is_sheaf (F : presheaf α) := locality F ∧ gluing F end sheaf_condition end presheaf structure semilattice_inf_hom (α β : Type*) [semilattice_inf α] [semilattice_inf β] := (to_fun : α → β) (mono : monotone to_fun) (map_inf' : ∀ a b, to_fun (a ⊓ b) = to_fun a ⊓ to_fun b) namespace semilattice_inf_hom infixr ` →⊓ `:25 := semilattice_inf_hom instance {α β : Type*} [semilattice_inf α] [semilattice_inf β] : has_coe_to_fun (α →⊓ β) := ⟨_, to_fun⟩ @[simp] theorem map_inf {α β : Type*} [semilattice_inf α] [semilattice_inf β] (f : α →⊓ β) (a b : α) : f (a ⊓ b) = f a ⊓ f b := map_inf' _ _ _ def id {α : Type*} [semilattice_inf α] : α →⊓ α := ⟨id, monotone_id, λ _ _, rfl⟩ def comp {α β γ : Type*} [semilattice_inf α] [semilattice_inf β] [semilattice_inf γ] (g : β →⊓ γ) (f : α →⊓ β) : α →⊓ γ := ⟨g ∘ f, monotone_comp f.mono g.mono, λ a b, by simp⟩ end semilattice_inf_hom structure semilattice_inf_lub_emb (α β : Type*) [semilattice_inf α] [semilattice_inf β] extends α →⊓ β := (inj : function.injective to_fun) (map_lub : ∀ {{S a}}, is_lub S a → is_lub (to_fun '' S) (to_fun a)) namespace semilattice_inf_lub_emb infixr ` ↪⊓⨆ `:25 := semilattice_inf_lub_emb instance {α β : Type*} [semilattice_inf α] [semilattice_inf β] : has_coe (α ↪⊓⨆ β) (α →⊓ β) := ⟨to_semilattice_inf_hom⟩ @[simp] theorem map_inf {α β : Type*} [semilattice_inf α] [semilattice_inf β] (f : α ↪⊓⨆ β) (a b : α) : f (a ⊓ b) = f a ⊓ f b := f.map_inf' _ _ def id {α : Type*} [semilattice_inf α] : α ↪⊓⨆ α := ⟨semilattice_inf_hom.id, function.injective_id, λ S a h, by simp [semilattice_inf_hom.id, h]⟩ def comp {α β γ : Type*} [semilattice_inf α] [semilattice_inf β] [semilattice_inf γ] (g : β ↪⊓⨆ γ) (f : α ↪⊓⨆ β) : α ↪⊓⨆ γ := ⟨(g : β →⊓ γ).comp f, function.injective_comp g.inj f.inj, λ S a h, by have := g.map_lub (f.map_lub h); rwa [set.image_image] at this⟩ end semilattice_inf_lub_emb namespace presheaf variables {α : Type*} {β : Type*} [semilattice_inf α] [semilattice_inf β] def comap (f : α →⊓ β) (F : presheaf β) : presheaf α := { F := λ a, F (f a), res := λ a b h, F.res _ _ (f.mono h), Hid := λ a, F.Hid _, Hcomp := λ a b c bc ab, F.Hcomp _ _ _ _ _ } theorem comap_res (f : α →⊓ β) (F : presheaf β) (a b h) : (F.comap f).res a b h = F.res _ _ (f.mono h) := rfl instance (f : α →⊓ β) (F : presheaf β) : has_coe (F.comap f).total F.total := ⟨λ x, ⟨_, x.2⟩⟩ theorem comap_coe_inj (f : α →⊓ β) (F : presheaf β) (h : function.injective f) : function.injective (coe : (F.comap f).total → F.total) | ⟨a, x⟩ ⟨b, y⟩ e := by cases h (congr_arg sigma.fst e:_); cases e; refl @[simp] theorem coe_total_coe (f : α →⊓ β) (F : presheaf β) (U : α) (x : (F.comap f) U) : ((x : (F.comap f).total) : F.total) = @coe (F (f U)) _ _ x := rfl theorem comap_res' (f : α →⊓ β) (F : presheaf β) (U : α) (x : (F.comap f).total) : ↑((F.comap f).res' U x) = F.res' (f U) x := total.cases_on (F.comap f) x $ λ W x, by rw [res'_def, coe_total_coe, coe_total_coe, comap_res, ← res'_val, f.map_inf, ← res'_eq_inf] end presheaf structure sheaf (α : Type u) [semilattice_inf α] extends presheaf α := (locality : to_presheaf.locality) (gluing : to_presheaf.gluing) structure sheaf_of_rings (α : Type u) [semilattice_inf α] extends sheaf α := [ring' : ∀ U, ring (F U)] [ring_hom : ∀ U V h, is_ring_hom (res U V h)] namespace sheaf variables {α : Type*} {β : Type*} [semilattice_inf α] [semilattice_inf β] def total (F : sheaf α) := F.to_presheaf.total def res' (F : sheaf α) : α → F.total → F.total := F.to_presheaf.res' instance : has_coe_to_fun (sheaf α) := { F := λ _, α → Type v, coe := λ F, F.to_presheaf } instance (F : sheaf α) (U : α) : has_coe_t (F U) F.total := ⟨sigma.mk _⟩ def comap (f : α ↪⊓⨆ β) (F : sheaf β) : sheaf α := { locality := λ a S lub (s t : F (f a)) H, F.locality (f.map_lub lub) begin rintro _ ⟨b, hb, rfl⟩, let G := F.to_presheaf.comap (f : α →⊓ β), have : G.res' b (@coe (G a) _ _ s) = G.res' b (@coe (G a) _ _ t) := H b hb, have H1 := F.to_presheaf.comap_res' (f : α →⊓ β) b _, rw [this, F.to_presheaf.comap_res' (f : α →⊓ β) b] at H1, simp at H1, exact H1.symm end, gluing := λ a S lub (g : ∀ U : S, F (f U)), begin let G := F.to_presheaf.comap (f : α →⊓ β), change ∀_: ∀ V W : S, G.res' (V ⊓ W) (@coe (G V) _ _ (g V)) = G.res' (V ⊓ W) (@coe (G W) _ _ (g W)), ∃ U : F (f a), ∀ V : S, G.res' V (@coe (G a) _ _ U) = @coe (G V) _ _ (g V), choose k hk using show ∀ V : f '' S, ∃ U : S, (V:β) = f U, by rintro ⟨_, U, hU, rfl⟩; exact ⟨⟨U, hU⟩, rfl⟩, let g' : ∀ V : f '' S, F (V:β) := λ V, by rw hk V; exact g (k V), have g'eq : ∀ U (hU : U ∈ S), (g' ⟨f U, U, hU, rfl⟩ : F.total) = g ⟨U, hU⟩, { intros, suffices : ∀ U' (h' : U' = k ⟨f U, _⟩), (by rw h'; exact g (k ⟨f U, U, hU, rfl⟩) : F (f U')) = g U', { congr, exact this ⟨U, hU⟩ (subtype.eq (f.inj (hk ⟨f U, _⟩))) }, rintro _ rfl, refl }, intro h, cases F.gluing (f.map_lub lub) g' _ with z hz, { use z, rintro ⟨U, hU⟩, apply presheaf.comap_coe_inj _ _ f.inj, rw [presheaf.comap_res'], exact (hz ⟨_, U, hU, rfl⟩).trans (g'eq _ _) }, { rintro ⟨_, V, hV, rfl⟩ ⟨_, W, hW, rfl⟩, have := congr_arg coe (h ⟨V, hV⟩ ⟨W, hW⟩), rw [presheaf.comap_res', presheaf.comap_res'] at this, simp at this, rwa [← g'eq, ← g'eq] at this } end, ..F.to_presheaf.comap f.to_semilattice_inf_hom } end sheaf
0d17a309c5cea57dfd937ffb9b29d47901a18608
6e41ee3ac9b96e8980a16295cc21f131e731884f
/hott/init/trunc.hlean
c6ef3f9eacfc2b5e9fb5ac688bdaf45066f5ae79
[ "Apache-2.0" ]
permissive
EgbertRijke/lean
3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3
4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183
refs/heads/master
1,610,834,871,476
1,422,159,801,000
1,422,159,801,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,432
hlean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Jeremy Avigad, Floris van Doorn -- Ported from Coq HoTT prelude import .path .logic .datatypes .equiv .types.empty .types.sigma open eq nat sigma unit set_option pp.universes true -- Truncation levels -- ----------------- -- TODO: make everything universe polymorphic -- TODO: everything definition with a hprop as codomain can be a theorem? /- truncation indices -/ namespace truncation inductive trunc_index : Type₁ := minus_two : trunc_index, trunc_S : trunc_index → trunc_index postfix `.+1`:(max+1) := trunc_index.trunc_S postfix `.+2`:(max+1) := λn, (n .+1 .+1) notation `-2` := trunc_index.minus_two notation `-1` := (-2.+1) namespace trunc_index definition add (n m : trunc_index) : trunc_index := trunc_index.rec_on m n (λ k l, l .+1) definition leq (n m : trunc_index) : Type₁ := trunc_index.rec_on n (λm, unit) (λ n p m, trunc_index.rec_on m (λ p, empty) (λ m q p, p m) p) m end trunc_index -- Coq calls this `-2+`, but `+2+` looks more natural, since trunc_index_add 0 0 = 2 infix `+2+`:65 := trunc_index.add notation x <= y := trunc_index.leq x y notation x ≤ y := trunc_index.leq x y namespace trunc_index definition succ_le {n m : trunc_index} (H : n ≤ m) : n.+1 ≤ m.+1 := H definition succ_le_cancel {n m : trunc_index} (H : n.+1 ≤ m.+1) : n ≤ m := H definition minus_two_le (n : trunc_index) : -2 ≤ n := star definition not_succ_le_minus_two {n : trunc_index} (H : n .+1 ≤ -2) : empty := H end trunc_index definition nat_to_trunc_index [coercion] (n : nat) : trunc_index := nat.rec_on n (-1.+1) (λ n k, k.+1) /- truncated types -/ /- Just as in Coq HoTT we define an internal version of contractibility and is_trunc, but we only use `is_trunc` and `is_contr` -/ structure contr_internal (A : Type) := (center : A) (contr : Π(a : A), center = a) definition is_trunc_internal (n : trunc_index) : Type → Type := trunc_index.rec_on n (λA, contr_internal A) (λn trunc_n A, (Π(x y : A), trunc_n (x = y))) structure is_trunc [class] (n : trunc_index) (A : Type) := (to_internal : is_trunc_internal n A) -- should this be notation or definitions? notation `is_contr` := is_trunc -2 notation `is_hprop` := is_trunc -1 notation `is_hset` := is_trunc (nat_to_trunc_index nat.zero) -- definition is_contr := is_trunc -2 -- definition is_hprop := is_trunc -1 -- definition is_hset := is_trunc 0 variables {A B : Type} -- TODO: rename to is_trunc_succ definition is_trunc_succ (A : Type) (n : trunc_index) [H : ∀x y : A, is_trunc n (x = y)] : is_trunc n.+1 A := is_trunc.mk (λ x y, !is_trunc.to_internal) -- TODO: rename to is_trunc_path definition succ_is_trunc (n : trunc_index) [H : is_trunc (n.+1) A] (x y : A) : is_trunc n (x = y) := is_trunc.mk (!is_trunc.to_internal x y) /- contractibility -/ definition is_contr.mk (center : A) (contr : Π(a : A), center = a) : is_contr A := is_trunc.mk (contr_internal.mk center contr) definition center (A : Type) [H : is_contr A] : A := @contr_internal.center A !is_trunc.to_internal definition contr [H : is_contr A] (a : A) : !center = a := @contr_internal.contr A !is_trunc.to_internal a definition path_contr [H : is_contr A] (x y : A) : x = y := contr x⁻¹ ⬝ (contr y) definition path2_contr {A : Type} [H : is_contr A] {x y : A} (p q : x = y) : p = q := have K : ∀ (r : x = y), path_contr x y = r, from (λ r, eq.rec_on r !concat_Vp), K p⁻¹ ⬝ K q definition contr_paths_contr [instance] {A : Type} [H : is_contr A] (x y : A) : is_contr (x = y) := is_contr.mk !path_contr (λ p, !path2_contr) /- truncation is upward close -/ -- n-types are also (n+1)-types definition trunc_succ [instance] (A : Type) (n : trunc_index) [H : is_trunc n A] : is_trunc (n.+1) A := trunc_index.rec_on n (λ A (H : is_contr A), !is_trunc_succ) (λ n IH A (H : is_trunc (n.+1) A), @is_trunc_succ _ _ (λ x y, IH _ !succ_is_trunc)) A H --in the proof the type of H is given explicitly to make it available for class inference definition trunc_leq (A : Type) (n m : trunc_index) (Hnm : n ≤ m) [Hn : is_trunc n A] : is_trunc m A := have base : ∀k A, k ≤ -2 → is_trunc k A → (is_trunc -2 A), from λ k A, trunc_index.cases_on k (λh1 h2, h2) (λk h1 h2, empty.elim (is_trunc -2 A) (trunc_index.not_succ_le_minus_two h1)), have step : Π (m : trunc_index) (IHm : Π (n : trunc_index) (A : Type), n ≤ m → is_trunc n A → is_trunc m A) (n : trunc_index) (A : Type) (Hnm : n ≤ m .+1) (Hn : is_trunc n A), is_trunc m .+1 A, from λm IHm n, trunc_index.rec_on n (λA Hnm Hn, @trunc_succ A m (IHm -2 A star Hn)) (λn IHn A Hnm (Hn : is_trunc n.+1 A), @is_trunc_succ A m (λx y, IHm n (x = y) (trunc_index.succ_le_cancel Hnm) !succ_is_trunc)), trunc_index.rec_on m base step n A Hnm Hn -- the following cannot be instances in their current form, because it is looping definition trunc_contr (A : Type) (n : trunc_index) [H : is_contr A] : is_trunc n A := trunc_index.rec_on n H _ definition trunc_hprop (A : Type) (n : trunc_index) [H : is_hprop A] : is_trunc (n.+1) A := trunc_leq A -1 (n.+1) star definition trunc_hset (A : Type) (n : trunc_index) [H : is_hset A] : is_trunc (n.+2) A := trunc_leq A nat.zero (n.+2) star /- hprops -/ definition is_hprop.elim [H : is_hprop A] (x y : A) : x = y := @center _ !succ_is_trunc definition contr_inhabited_hprop {A : Type} [H : is_hprop A] (x : A) : is_contr A := is_contr.mk x (λy, !is_hprop.elim) --Coq has the following as instance, but doesn't look too useful definition hprop_inhabited_contr {A : Type} (H : A → is_contr A) : is_hprop A := @is_trunc_succ A -2 (λx y, have H2 [visible] : is_contr A, from H x, !contr_paths_contr) definition is_hprop.mk {A : Type} (H : ∀x y : A, x = y) : is_hprop A := hprop_inhabited_contr (λ x, is_contr.mk x (H x)) /- hsets -/ definition is_hset.mk (A : Type) (H : ∀(x y : A) (p q : x = y), p = q) : is_hset A := @is_trunc_succ _ _ (λ x y, is_hprop.mk (H x y)) definition is_hset.elim [H : is_hset A] ⦃x y : A⦄ (p q : x = y) : p = q := @is_hprop.elim _ !succ_is_trunc p q /- instances -/ definition contr_basedpaths [instance] {A : Type} (a : A) : is_contr (Σ(x : A), a = x) := is_contr.mk (sigma.mk a idp) (λp, sigma.rec_on p (λ b q, eq.rec_on q idp)) definition unit_contr [instance] : is_contr unit := is_contr.mk star (λp, unit.rec_on p idp) definition empty_hprop [instance] : is_hprop empty := is_hprop.mk (λx, !empty.elim x) /- truncated universe -/ structure trunctype (n : trunc_index) := (trunctype_type : Type) (is_trunc_trunctype_type : is_trunc n trunctype_type) attribute trunctype.trunctype_type [coercion] notation n `-Type` := trunctype n notation `hprop` := -1-Type notation `hset` := 0-Type definition hprop.mk := @trunctype.mk -1 definition hset.mk := @trunctype.mk nat.zero --what does the following line in Coq do? --Canonical Structure default_TruncType := fun n T P => (@BuildTruncType n T P). /- interaction with equivalences -/ section open is_equiv equiv --should we remove the following two theorems as they are special cases of "trunc_equiv" definition equiv_preserves_contr (f : A → B) [Hf : is_equiv f] [HA: is_contr A] : (is_contr B) := is_contr.mk (f (center A)) (λp, moveR_M f !contr) theorem contr_equiv (H : A ≃ B) [HA: is_contr A] : is_contr B := @equiv_preserves_contr _ _ (to_fun H) (to_is_equiv H) _ definition contr_equiv_contr [HA : is_contr A] [HB : is_contr B] : A ≃ B := equiv.mk (λa, center B) (is_equiv.adjointify (λa, center B) (λb, center A) contr contr) definition trunc_equiv (n : trunc_index) (f : A → B) [H : is_equiv f] [HA : is_trunc n A] : is_trunc n B := trunc_index.rec_on n (λA (HA : is_contr A) B f (H : is_equiv f), !equiv_preserves_contr) (λn IH A (HA : is_trunc n.+1 A) B f (H : is_equiv f), @is_trunc_succ _ _ (λ x y : B, IH (f⁻¹ x = f⁻¹ y) !succ_is_trunc (x = y) ((ap (f⁻¹))⁻¹) !inv_closed)) A HA B f H definition trunc_equiv' (n : trunc_index) (f : A ≃ B) [HA : is_trunc n A] : is_trunc n B := trunc_equiv n (to_fun f) definition isequiv_iff_hprop [HA : is_hprop A] [HB : is_hprop B] (f : A → B) (g : B → A) : is_equiv f := is_equiv.adjointify f g (λb, !is_hprop.elim) (λa, !is_hprop.elim) -- definition equiv_iff_hprop_uncurried [HA : is_hprop A] [HB : is_hprop B] : (A ↔ B) → (A ≃ B) := sorry definition equiv_iff_hprop [HA : is_hprop A] [HB : is_hprop B] (f : A → B) (g : B → A) : A ≃ B := equiv.mk f (isequiv_iff_hprop f g) end /- interaction with the Unit type -/ -- A contractible type is equivalent to [Unit]. *) definition equiv_contr_unit [H : is_contr A] : A ≃ unit := equiv.mk (λ (x : A), ⋆) (is_equiv.mk (λ (u : unit), center A) (λ (u : unit), unit.rec_on u idp) (λ (x : A), contr x) (λ (x : A), (!ap_const)⁻¹)) -- TODO: port "Truncated morphisms" end truncation
3104ca424d16799bad1e4309a9acb82bf013626b
0dcd6d70b36595095a3659da02037388779a74fb
/01_Equality/06_properties.lean
d7d8124e557757c4a5c2f9d0888f7e95ead12881
[]
no_license
mcv6tr/cs-dm
fd7a7019328e9c33baef5415f32e7ecf2e5dde49
d4499e53abbecb30268a25bd43ffed54e22c5116
refs/heads/master
1,585,355,219,723
1,536,084,295,000
1,536,084,295,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,124
lean
/- HOMEWORK: Read, complete, submit. We've seen that equality is reflexive. That is, everything is equal to itself. It is also symmetric in the sense that if any value, a, is equal to some other value, b, i.e., if a = b, then b is also equal to a, i.e., b = a. What this means is we have an inference rule that both expresses the symmetric property of equality and allows us to compute a proof of b = a from any proof of a = b, no matter what a and b are (no matter what type, T, a and b have, and no matter what values they have of this type). -/ /- (T: Type) (a b: T) (ab: a = b) ------------------------------ (eq.symm) ba: b = a -/ -- Let's see it in action in four lines def a := 1 def b := 2 - 1 lemma ab : a = b := rfl #check ab -- a proof of a = b #check (eq.symm ab) -- a proof of b = a! /- This is a big success. We understand not only the property of being symmetric but that we can use symmetry to derive new proofs from proofs that we already have. In fact, eq.symm is a program that does just this derivation, as we see here! -/ /- Finally we come to the notion that equality is also transitive. That means that for any values, a, b, and c, if a = b, and if b = c, then it must be that consequently a = c as well. {T : Type}, { a b c: T }, ab: a = b, bc: b = c ------------ (eq.trans) ac: a = c That is, if given proofs of a = b and b = c, eq.symm constructs and returns a proof of a = c. Let's see it in action. We've already got variables a and b to work with. We need one more, c. -/ def c := 1 /- We've also already got a proof of a = b. It's easy to generate one of b = c. -/ lemma bc : b = c := rfl /- And now we can apply eq.trans to these two premise-proofs and it will construct and return a proof of the conclusion. The expression that applies eq.trans to these two proofs is (eq.trans ab bc). Now for the fun part! -/ #check eq.trans ab bc /- EXERCISE: First write a textual inference rule, let's call it eq_snart. It says that if T is any type; if a, b, and c are values of this type, T; and you are given proofs of a = b and c = b then you can derive a proof of a = c. -/ /- EXERCISE: Now "prove" that this rule is valid by implementing it as a program that, when given any argument values of the specified types, returns a proof of the specified type (of the conclusion). Hint: Use eq.trans to construct the proof of a = c. It's first argument will be ab, the proof that a = b. It's second argument has to be a proof of b = c for it to work to derive a proof of a = c; but all we've got is a proof of c = b (in the reverse order). How can we pass a second argument of type b = c to eq.trans, so that it can do its job, when we have at hand is a proof of c = b. Now a major hint: we already have a way to construct a proof of b = c from a proof of c = b. Just use it. Ignore the error message in the following incomplete code. The problem is simply that the definition is incomplete, due to the underscore placeholder. Replace the underscore with your answer. Leave parenthesis around your expression so that it gets evaluated as its own term. -/ def eq_snart { T : Type} { a b c: T } (ab: a = b) (cb: c = b) := eq.trans ab (_) /- EXERCISE: Use lean to implement a new rule that that, from a proof of c = b and a proof of b = a, derives (constructs and returns) a proof of a = c. Call the proof eq_snart' (why not, it sounds funny). -/ /- * Conclusion * -/ /- You now begin to see the power of proof assistants. We can now compute with proofs and the type checker makes sure that we can (for all intents and purposes) never use a proof that is not a valid proof of the proposition that it claims to prove. This represents a discontinuous leap over the the state of mathematical practice, where proofs are beautiful little essays rather than values we can compute with. -/ /- EXERCISE: Here we use eq_snart rather than eq.trans directly to prove a = c, given proofs of a = b and c = b. -/ lemma cb : c = b := rfl #check cb theorem aeqc : a = c := eq_snart _ _ /- In general, there are many ways to prove a given theorem. Each distinct proof is nevertheless an inhabitant of the type of the proposition that it proves, and each suffices as evidence to justify a truth judgment for the proposition. In many cases, the particular proof object that is used -/ /- SUMMARY: In this section (1) you first recalled that the equality relation is reflexive, symmetric, and transitive. (2) You saw that in Lean, these are not just abstract ideas; there are also inference rules that you can apply to to arguments of the right types to build proofs of new propositions. (3) You also saw that you can prove your own inference rules by writing programs that implement them! Such programs can use already accepted inference rules (such as eq.refl, eq.symm, eq.trans) in their implementations. Thereafter, the new rules are as good as the old, and can then also be used to construct proofs that might be needed. -/
b4aae7bd5c41c8367a896561a1aec34917cfe09e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/evalInstMessage.lean
4e92376442090ce4671472bb8b3e73823155a1aa
[ "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
42
lean
def double (x : Nat) := 2*x #eval double
14b3a17ad0083ffb11d1ee28fa990ddcfb963b50
b2c4bd81ed12cc14c20704365f094339d4c894a2
/src/graph_theory/path.lean
66f8e3dcf53d5c03b1975ca8cda399afcdb149be
[]
no_license
agusakov/graph_theory_2020
711639d9d9b25fd83899620da11ae1753d11d48b
83a8afc31aa28dbec39a768d6042d3cb515f7a16
refs/heads/master
1,669,901,277,114
1,596,931,643,000
1,596,931,643,000
285,154,458
5
0
null
null
null
null
UTF-8
Lean
false
false
11,231
lean
import .basic universes u variables {V : Type u} section list lemma list.cons_nth_le_succ {α : Type*} {hd : α} (tl : list α) (n : ℕ) (hn : n + 1 < (tl.cons hd).length) (h_auto : n < tl.length := by tidy) : (tl.cons hd).nth_le (n + 1) hn = tl.nth_le n h_auto := rfl lemma list.cons_nth_le_succ' {α : Type*} (l : list α) (n : ℕ) (hn : n + 1 < l.length) (h_auto : n < l.tail.length) : l.nth_le (n + 1) hn = l.tail.nth_le n h_auto := by { cases l, { cases hn }, simp; refl } @[simp] lemma list.nth_le_succ_tail {α : Type*} (l : list α) (n : ℕ) (hn : n < l.tail.length) (h_auto : n + 1 < l.length) -- (h_auto : n + 1 < l.length := by { simp at hn, omega }) : l.nth_le (n + 1) h_auto = l.tail.nth_le n hn := by { cases l, tidy } @[simp] lemma list.tail_nth_le_zero {α : Type*} (l : list α) [inhabited α] (h_auto : 0 < l.length) : list.nth_le l 0 h_auto = l.head := by { cases l, { cases h_auto }, tidy, } end list namespace simple_graph variables (G : simple_graph V) /-- Morally, a path is an alternating list of vertices and edges, with incidences between adjacent objects -/ @[ext] structure path := (head : V) (tail : list V) (edges : list G.E) (length_eq : edges.length = tail.length) (adj : ∀ (n : ℕ) (hn : n < edges.length), let u := (list.cons head tail).nth_le n (by { simp; omega }) in let v := (list.cons head tail).nth_le (n + 1) (by { simp, cc }) in u ≠ v ∧ u ∈ edges.nth_le n hn ∧ v ∈ edges.nth_le n hn) namespace path variables {G} variables (p : G.path) section classical open_locale classical /-- The last vertex in p. -/ noncomputable def last : V := if h : p.tail = list.nil then p.head else p.tail.last h end classical /-- The ordered list of all vertices in p, starting at p.head and ending at p.sink. -/ def vertices : list V := p.head :: p.tail /-- The number of edges in p. -/ def length : ℕ := p.tail.length -- should probably make sure this doesn't become a problem if we run into infinite graphs? @[simp] lemma tail_length_eq : p.tail.length = p.length := rfl @[simp] lemma edges_length_eq : p.edges.length = p.length := by simp [p.length_eq] @[simp] lemma vertices_tail : p.vertices.tail = p.tail := by simp [path.vertices] @[simp] lemma vertices_length : p.vertices.length = p.length + 1 := by simp [path.vertices] #check list.nth_eq_some lemma head_ne_tail_head [inhabited V] (h : p.tail ≠ list.nil) : p.head ≠ p.tail.head := begin rcases p.adj 0 _ with ⟨hp, _⟩, dsimp at hp, convert hp, cases hp1 : p.tail, { contradiction }, { simp [hp1] }, { revert h, rw ← list.length_pos_iff_ne_nil, simp }, end -- begin -- end -- variables {s t : V} variables {p} /-- p.edge_mem e holds if e is an edge along path p. -/ def edge_mem (e : G.E) (p : G.path) : Prop := e ∈ p.edges -- this instance doesn't fire and I don't know why instance has_mem_edge : has_mem G.E G.path := { mem := edge_mem } /-- p.vertex_mem v holds if v is a vertex along path p. -/ def vertex_mem (v : V) (p : G.path) : Prop := v ∈ p.vertices instance has_mem_vertices : has_mem V G.path := { mem := vertex_mem } variables (p) /-- The empty path based at vertex v. -/ @[simps] def empty (G : simple_graph V) (v : V) : G.path := { head := v, tail := list.nil, edges := list.nil, length_eq := rfl, adj := by rintros _ ⟨_⟩ } instance [inhabited V] : inhabited G.path := { default := empty G (arbitrary V) } @[simp] lemma empty_length (v : V) : (empty G v).length = 0 := by refl lemma edge_mem_empty {v : V} (e : G.E) : ¬ (empty G v).edge_mem e := by simp [empty, edge_mem] lemma vertex_mem_empty {u v : V} : u ∈ (empty G v) ↔ u = v := by { unfold has_mem.mem vertex_mem, simp [empty, vertices], apply or_false } /-- p.cons e hp hs is the path extending `p` by edge `e`. -/ def cons {s : V} (e : G.E) (hp : p.head ∈ e) (hs : s ∈ e) (hsp : s ≠ p.head) : G.path := { head := s, tail := p.vertices, edges := list.cons e p.edges, length_eq := by { simp [vertices] }, adj := begin intros n hn _ _, cases n, { simp, tauto }, simp; apply p.adj, end } @[simp] lemma edge_mem_cons {s : V} (hd e : G.E) (hp : p.head ∈ hd) (hs : s ∈ hd) (hsp : s ≠ p.head) : (p.cons hd hp hs hsp).edge_mem e ↔ e = hd ∨ p.edge_mem e := by simp [path.cons, path.vertices, edge_mem] @[simp] lemma vertex_mem_cons {v s : V} (hd : G.E) (hp : p.head ∈ hd) (hs : s ∈ hd) (hsp : s ≠ p.head) : v ∈ (p.cons hd hp hs hsp) ↔ v = s ∨ v ∈ p := by refl @[simp] lemma cons_length {s : V} (hd : G.E) (hp : p.head ∈ hd) (hs : s ∈ hd) (hsp : s ≠ p.head) : (p.cons hd hp hs hsp).length = p.length + 1 := by { unfold cons length, simp [vertices] } @[simp] lemma cons_vertices {s : V} (hd : G.E) (hp : p.head ∈ hd) (hs : s ∈ hd) (hsp : s ≠ p.head) : (p.cons hd hp hs hsp).vertices = list.cons s p.vertices := by { dsimp [vertices, cons], simp } lemma edges_eq_nil_iff : p.edges = list.nil ↔ p.tail = list.nil := by rw [← list.length_eq_zero, p.length_eq, list.length_eq_zero] lemma length_eq_zero_iff_eq_empty : p.length = 0 ↔ p = empty G p.head := begin erw list.length_eq_zero, split; intro h, swap, rw h, simp [empty], ext, work_on_goal 2 {rw ← edges_eq_nil_iff at h}, any_goals {simp [empty, h]}, end lemma cases_on' [inhabited V] : (∃ v, p = empty G v) ∨ ∃ (tl : G.path) v e (hs : tl.head ∈ e) (hv : v ∈ e) (hvp : v ≠ tl.head), p = tl.cons e hs hv hvp := begin cases hp : p.edges with hd tl, { left, use p.head, ext, { simp [empty] }, { suffices : p.tail = list.nil, { simp [empty, this] }, rwa edges_eq_nil_iff at hp }, simp [hp, empty] }, have hp_nil : ¬ p.edges = list.nil, { simp [hp] }, rw edges_eq_nil_iff at hp_nil, rcases p.adj 0 _ with ⟨hvs, hv, hs⟩, simp only [list.nth_le, zero_add] at hs, repeat { rw list.nth_le_zero at * }, set q : G.path := { head := p.tail.head, tail := list.tail p.tail, edges := list.tail p.edges, length_eq := by simp [p.length_eq], adj := _}, swap, { intros, have := p.adj (n + 1) (by { simp only [list.length_tail] at hn, exact nat.add_lt_of_lt_sub_right hn, }), simp only [list.nth_le] at this ⊢, cases this with huv this, cases hp_tail : p.tail with p_hd p_tl, { contradiction }, dsimp [u,v], simp [hp_tail], split, { contrapose! huv, symmetry, convert huv using 1; simp [hp_tail, huv]; refl }, -- simp [hp_tail, this], -- rw list.tail_nth_le, convert this using 2, any_goals { simp [hp_tail]; refl }, -- { rw list.cons_head_tail hp_nil, refl }, iterate 2 { rwa list.cons_nth_le_succ' }, }, right, use [q, p.head, hd], simp only [hp, list.head] at hs hv, swap, { simp [hp] }, dsimp, use [hs, hv], split, ext, { simp [path.cons] }, { dsimp [q, path.cons, path.vertices], rwa list.cons_head_tail, }, { dsimp [q, path.cons], simp [hp] }, revert hvs, simp, end @[elab_as_eliminator] lemma induction_on [inhabited V] (P : G.path → Prop) (P_empty : ∀ v, P $ empty G v) (P_inductive : ∀ tl hd hs {v} (hv : v ∈ hd) (hsv), P tl → P (tl.cons hd hs hv hsv)) : P p := begin suffices : ∀ k (q : G.path), q.length = k → P q, { apply this p.length, refl }, intro k, induction k with k hk, { intros, convert P_empty _, rwa ← length_eq_zero_iff_eq_empty }, intro q, rcases q.cases_on' with ⟨_,rfl⟩|⟨_,_,_,_,_,_,rfl⟩, { intro, apply P_empty }, intro, apply P_inductive, apply hk, simp at a, omega, end lemma consecutive_vertex_ne [inhabited V] {n} (h : n < p.length) : p.vertices.nth_le n (by { simp, linarith }) ≠ p.vertices.nth_le (n+1) (by { simp, linarith }) := begin revert n, convert p.induction_on (λ q, ∀ n (h : n < q.length), q.vertices.nth_le n _ ≠ q.vertices.nth_le (n+1) _) _ _, { simp }, iterate 2 { simp, omega }, { rintros _ _ ⟨⟩ }, clear p, intros _ _ _ _ _ _ h n hn, cases n, { simpa }, convert h _ _, simp at hn, omega, end lemma consecutive_edge_ne [inhabited V] {n} (h : n < p.length) : p.edges.nth_le n (by { simp, linarith }) ≠ p.edges.nth_le (n+1) (by { simp, sorry, }) := begin sorry, end @[ext] lemma eq_of_vertices_eq [inhabited V] (q : G.path) : p = q ↔ p.vertices = q.vertices := { mp := by tidy, mpr := begin intro h, have h_tail : p.tail = q.tail, { dsimp [vertices] at h, simp only [] at h, tauto }, have h_length : p.length = q.length := by rw [← tail_length_eq, h_tail, tail_length_eq], ext, { tidy }, { simp [h_tail] }, by_cases hn : n < p.length; have hnq := by { rw h_length at hn, exact hn }, swap, { simp [hn, hnq, list.nth_eq_some] }, suffices : p.edges.nth_le n _ = q.edges.nth_le n _, { simp [hn, hnq, list.nth_eq_some]; refine eq.congr this rfl }, rw edge_eq_iff, set u := p.vertices.nth_le n (by { simp [path.vertices], linarith }), set v := p.vertices.nth_le (n+1) (by { simp [path.vertices], linarith }), use [u, v], split, symmetry, apply consecutive_vertex_ne, rcases p.adj n _ with ⟨_, hpu, hpv⟩, rcases q.adj n _ with ⟨_, hqu, hqv⟩, split, { exact hpu }, split, { convert hqu, dsimp [u], simp_rw h, refl }, split, { exact hpv }, { convert hqv, dsimp [v], simp_rw h, refl }, end } /-- p.is_cycle if p starts and ends in the same place. -/ def is_cycle : Prop := p.head = p.last /-- p.is_trail if p has no repeated edges. -/ def is_trail : Prop := list.nodup p.edges /-- p.is_tour if p has no repeated vertices. -/ def is_tour : Prop := list.nodup p.vertices -- maybe prove that `is_tour → is_Eulerian` /-- p.is_Eulerian if p hits each edge exactly once. -/ def is_Eulerian : Prop := p.is_trail ∧ ∀ e : G.E, p.edge_mem e /-- p.is_maximal if the length of p is greater than or equal to the length of every other path. -/ def is_maximal : Prop := p.is_tour ∧ ∀ (q : path G), q.length ≤ p.length --should probably figure out some fintype stuff here lol structure simple_cycle : Prop := (is_cycle : p.is_cycle) (is_tour : p.is_tour) /-example {V : Type u} {G : simple_graph V} (p : G.path) [inhabited V] (h : p.vertices.nodup) (tl : G.path) (hd : G.E) (hs : tl.head ∈ hd) {v : V} (hv : v ∈ hd) (hsv : v ≠ tl.head) (a : list.pairwise ne tl.edges) (f : G.E) (hf : f ∈ tl.edges) : hd ≠ f := begin contrapose! h, subst hd, end-/ -- should have this lemma lemma edge_path_mem_vertices {p : G.path} {e : G.E} {u : V} (h1 : e ∈ p.edges) : u ∈ e → (u ∈ p.vertices) := begin --apply p.adj, sorry, end lemma tour_is_trail [inhabited V] : p.is_tour → p.is_trail := begin intro h, unfold is_trail, unfold is_tour at h, unfold list.nodup, /-apply p.induction_on, -- this seems to be where it all goes wrong { intro, rw list.pairwise_iff, simp }, intros, intros, rw list.pairwise_iff, right, use [hd, tl.edges], split, swap, { tauto }, intros f hf, contrapose! hf, subst hf, suffices : v ∉ tl.vertices, contrapose! this, apply edge_path_mem_vertices this hv,-/ -- i think this may be impossible to prove at this point and i don't know where it all went wrong -- something like `apply h`, sorry end end path end simple_graph
d3d0a83057b69cf72355836dea9a91f9a22b0d1d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/sheaves/forget.lean
fed5cc40b5b124d730c463a17a6bf8c1002d5b58
[ "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
8,298
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.preserves.shapes.products import topology.sheaves.sheaf_condition.equalizer_products /-! # Checking the sheaf condition on the underlying presheaf of types. If `G : C ⥤ D` is a functor which reflects isomorphisms and preserves limits (we assume all limits exist in both `C` and `D`), then checking the sheaf condition for a presheaf `F : presheaf C X` is equivalent to checking the sheaf condition for `F ⋙ G`. The important special case is when `C` is a concrete category with a forgetful functor that preserves limits and reflects isomorphisms. Then to check the sheaf condition it suffices to check it on the underlying sheaf of types. ## References * https://stacks.math.columbia.edu/tag/0073 -/ noncomputable theory open category_theory open category_theory.limits open topological_space open opposite namespace Top namespace presheaf namespace sheaf_condition open sheaf_condition_equalizer_products universes v u₁ u₂ variables {C : Type u₁} [category.{v} C] [has_limits C] variables {D : Type u₂} [category.{v} D] [has_limits D] variables (G : C ⥤ D) [preserves_limits G] variables {X : Top.{v}} (F : presheaf C X) variables {ι : Type v} (U : ι → opens X) local attribute [reducible] diagram left_res right_res /-- When `G` preserves limits, the sheaf condition diagram for `F` composed with `G` is naturally isomorphic to the sheaf condition diagram for `F ⋙ G`. -/ def diagram_comp_preserves_limits : diagram F U ⋙ G ≅ diagram.{v} (F ⋙ G) U := begin fapply nat_iso.of_components, rintro ⟨j⟩, exact (preserves_product.iso _ _), exact (preserves_product.iso _ _), rintros ⟨⟩ ⟨⟩ ⟨⟩, { ext, simp, dsimp, simp, }, -- non-terminal `simp`, but `squeeze_simp` fails { ext, simp only [limit.lift_π, functor.comp_map, map_lift_pi_comparison, fan.mk_π_app, preserves_product.iso_hom, parallel_pair_map_left, functor.map_comp, category.assoc], dsimp, simp, }, { ext, simp only [limit.lift_π, functor.comp_map, parallel_pair_map_right, fan.mk_π_app, preserves_product.iso_hom, map_lift_pi_comparison, functor.map_comp, category.assoc], dsimp, simp, }, { ext, simp, dsimp, simp, }, end local attribute [reducible] res /-- When `G` preserves limits, the image under `G` of the sheaf condition fork for `F` is the sheaf condition fork for `F ⋙ G`, postcomposed with the inverse of the natural isomorphism `diagram_comp_preserves_limits`. -/ def map_cone_fork : G.map_cone (fork.{v} F U) ≅ (cones.postcompose (diagram_comp_preserves_limits G F U).inv).obj (fork (F ⋙ G) U) := cones.ext (iso.refl _) (λ j, begin dsimp, simp [diagram_comp_preserves_limits], cases j; dsimp, { rw iso.eq_comp_inv, ext, simp, dsimp, simp, }, { rw iso.eq_comp_inv, ext, simp, -- non-terminal `simp`, but `squeeze_simp` fails dsimp, simp only [limit.lift_π, fan.mk_π_app, ←G.map_comp, limit.lift_π_assoc, fan.mk_π_app] } end) end sheaf_condition universes v u₁ u₂ open sheaf_condition sheaf_condition_equalizer_products variables {C : Type u₁} [category.{v} C] {D : Type u₂} [category.{v} D] variables (G : C ⥤ D) variables [reflects_isomorphisms G] variables [has_limits C] [has_limits D] [preserves_limits G] variables {X : Top.{v}} (F : presheaf C X) /-- If `G : C ⥤ D` is a functor which reflects isomorphisms and preserves limits (we assume all limits exist in both `C` and `D`), then checking the sheaf condition for a presheaf `F : presheaf C X` is equivalent to checking the sheaf condition for `F ⋙ G`. The important special case is when `C` is a concrete category with a forgetful functor that preserves limits and reflects isomorphisms. Then to check the sheaf condition it suffices to check it on the underlying sheaf of types. Another useful example is the forgetful functor `TopCommRing ⥤ Top`. See <https://stacks.math.columbia.edu/tag/0073>. In fact we prove a stronger version with arbitrary complete target category. -/ lemma is_sheaf_iff_is_sheaf_comp : presheaf.is_sheaf F ↔ presheaf.is_sheaf (F ⋙ G) := begin rw [presheaf.is_sheaf_iff_is_sheaf_equalizer_products, presheaf.is_sheaf_iff_is_sheaf_equalizer_products], split, { intros S ι U, -- We have that the sheaf condition fork for `F` is a limit fork, obtain ⟨t₁⟩ := S U, -- and since `G` preserves limits, the image under `G` of this fork is a limit fork too. letI := preserves_smallest_limits_of_preserves_limits G, have t₂ := @preserves_limit.preserves _ _ _ _ _ _ _ G _ _ t₁, -- As we established above, that image is just the sheaf condition fork -- for `F ⋙ G` postcomposed with some natural isomorphism, have t₃ := is_limit.of_iso_limit t₂ (map_cone_fork G F U), -- and as postcomposing by a natural isomorphism preserves limit cones, have t₄ := is_limit.postcompose_inv_equiv _ _ t₃, -- we have our desired conclusion. exact ⟨t₄⟩, }, { intros S ι U, refine ⟨_⟩, -- Let `f` be the universal morphism from `F.obj U` to the equalizer -- of the sheaf condition fork, whatever it is. -- Our goal is to show that this is an isomorphism. let f := equalizer.lift _ (w F U), -- If we can do that, suffices : is_iso (G.map f), { resetI, -- we have that `f` itself is an isomorphism, since `G` reflects isomorphisms haveI : is_iso f := is_iso_of_reflects_iso f G, -- TODO package this up as a result elsewhere: apply is_limit.of_iso_limit (limit.is_limit _), apply iso.symm, fapply cones.ext, exact (as_iso f), rintro ⟨_|_⟩; { dsimp [f], simp, }, }, { -- Returning to the task of shwoing that `G.map f` is an isomorphism, -- we note that `G.map f` is almost but not quite (see below) a morphism -- from the sheaf condition cone for `F ⋙ G` to the -- image under `G` of the equalizer cone for the sheaf condition diagram. let c := fork (F ⋙ G) U, obtain ⟨hc⟩ := S U, let d := G.map_cone (equalizer.fork (left_res.{v} F U) (right_res F U)), letI := preserves_smallest_limits_of_preserves_limits G, have hd : is_limit d := preserves_limit.preserves (limit.is_limit _), -- Since both of these are limit cones -- (`c` by our hypothesis `S`, and `d` because `G` preserves limits), -- we hope to be able to conclude that `f` is an isomorphism. -- We say "not quite" above because `c` and `d` don't quite have the same shape: -- we need to postcompose by the natural isomorphism `diagram_comp_preserves_limits` -- introduced above. let d' := (cones.postcompose (diagram_comp_preserves_limits G F U).hom).obj d, have hd' : is_limit d' := (is_limit.postcompose_hom_equiv (diagram_comp_preserves_limits G F U : _) d).symm hd, -- Now everything works: we verify that `f` really is a morphism between these cones: let f' : c ⟶ d' := fork.mk_hom (G.map f) begin dsimp only [c, d, d', f, diagram_comp_preserves_limits, res], dunfold fork.ι, ext1 j, dsimp, simp only [category.assoc, ←functor.map_comp_assoc, equalizer.lift_ι, map_lift_pi_comparison_assoc], dsimp [res], simp, end, -- conclude that it is an isomorphism, -- just because it's a morphism between two limit cones. haveI : is_iso f' := is_limit.hom_is_iso hc hd' f', -- A cone morphism is an isomorphism exactly if the morphism between the cone points is, -- so we're done! exact is_iso.of_iso ((cones.forget _).map_iso (as_iso f')) }, }, end /-! As an example, we now have everything we need to check the sheaf condition for a presheaf of commutative rings, merely by checking the sheaf condition for the underlying sheaf of types. ``` import algebra.category.Ring.limits example (X : Top) (F : presheaf CommRing X) (h : presheaf.is_sheaf (F ⋙ (forget CommRing))) : F.is_sheaf := (is_sheaf_iff_is_sheaf_comp (forget CommRing) F).mpr h ``` -/ end presheaf end Top
23bdab346e8c4250286089937545924913d07643
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/simp_dif.lean
f7f9f6358042325a9aa1889ae607dd58cf007d18
[ "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
1,034
lean
constant safe_div (a b : nat) : b ≠ 0 → nat example (a b : nat) (h : ¬b ≠ 0) : (if h : b ≠ 0 then safe_div a b h else a) = a := by simp [dif_neg h] example (a b : nat) (h : b ≠ 0) : (if h : b ≠ 0 then safe_div a b h else a) = safe_div a b h := by simp [dif_pos h] example (a b : nat) (h : ¬b ≠ 0) : (if h : b ≠ 0 then safe_div a b h else a) = a := by rw [dif_neg h] example (a b : nat) (h : b ≠ 0) : (if h : b ≠ 0 then safe_div a b h else a) = safe_div a b h := by rw [dif_pos h] example (a b : nat) : (if h : b ≠ 0 then safe_div a b h else a) = a ∨ ∃ h, (if h : b ≠ 0 then safe_div a b h else a) = safe_div a b h := begin by_cases (b ≠ 0), {apply or.inr, rw [dif_pos h], existsi h, refl}, {apply or.inl, rw [dif_neg h]} end example (a b : nat) : (if h : b ≠ 0 then safe_div a b h else a) = a ∨ ∃ h, (if h : b ≠ 0 then safe_div a b h else a) = safe_div a b h := begin by_cases (b ≠ 0), {apply or.inr, simp [dif_pos h], existsi h, trivial}, {apply or.inl, simp [dif_neg h]} end
2b7e680f00f37ca615420a992540032946312b5d
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/order/default.lean
721a6f716efd630f3a2dfe7700bb357254f2b1e4
[ "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
127
lean
import order.basic order.lattice order.bounded_lattice order.boolean_algebra order.complete_lattice order.bounds
e72074d500f21deff3ca9f6e99ff05bc9d0e9672
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/idempotents/functor_extension.lean
b1fb977c16832d4e025a39fd6fb72b6a94a4e890
[ "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
4,157
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import category_theory.idempotents.karoubi import category_theory.natural_isomorphism /-! # Extension of functors to the idempotent completion In this file, we construct an extension `functor_extension₁` of functors `C ⥤ karoubi D` to functors `karoubi C ⥤ karoubi D`. TODO : Obtain the equivalences `karoubi_universal₁ C D : C ⥤ karoubi D ≌ karoubi C ⥤ karoubi D` for all categories, and `karoubi_universal C D : C ⥤ D ≌ karoubi C ⥤ D`. when `D` is idempotent complete -/ open category_theory.category open category_theory.idempotents.karoubi namespace category_theory namespace idempotents variables {C D E : Type*} [category C] [category D] [category E] /-- A natural transformation between functors `karoubi C ⥤ D` is determined by its value on objects coming from `C`. -/ lemma nat_trans_eq {F G : karoubi C ⥤ D} (φ : F ⟶ G) (P : karoubi C) : φ.app P = F.map (decomp_id_i P) ≫ φ.app P.X ≫ G.map (decomp_id_p P) := begin rw [← φ.naturality, ← assoc, ← F.map_comp], conv { to_lhs, rw [← id_comp (φ.app P), ← F.map_id], }, congr, apply decomp_id, end namespace functor_extension₁ /-- The canonical extension of a functor `C ⥤ karoubi D` to a functor `karoubi C ⥤ karoubi D` -/ @[simps] def obj (F : C ⥤ karoubi D) : karoubi C ⥤ karoubi D := { obj := λ P, ⟨(F.obj P.X).X, (F.map P.p).f, by simpa only [F.map_comp, hom_ext] using F.congr_map P.idem⟩, map := λ P Q f, ⟨(F.map f.f).f, by simpa only [F.map_comp, hom_ext] using F.congr_map f.comm⟩, } /-- Extension of a natural transformation `φ` between functors `C ⥤ karoubi D` to a natural transformation between the extension of these functors to `karoubi C ⥤ karoubi D` -/ @[simps] def map {F G : C ⥤ karoubi D} (φ : F ⟶ G) : obj F ⟶ obj G := { app := λ P, { f := (F.map P.p).f ≫ (φ.app P.X).f, comm := begin have h := φ.naturality P.p, have h' := F.congr_map P.idem, simp only [hom_ext, karoubi.comp, F.map_comp] at h h', simp only [obj_obj_p, assoc, ← h], slice_rhs 1 3 { rw [h', h'], }, end, }, naturality' := λ P Q f, begin ext, dsimp [obj], have h := φ.naturality f.f, have h' := F.congr_map (comp_p f), have h'' := F.congr_map (p_comp f), simp only [hom_ext, functor.map_comp, comp] at ⊢ h h' h'', slice_rhs 2 3 { rw ← h, }, slice_lhs 1 2 { rw h', }, slice_rhs 1 2 { rw h'', }, end } end functor_extension₁ variables (C D E) /-- The canonical functor `(C ⥤ karoubi D) ⥤ (karoubi C ⥤ karoubi D)` -/ @[simps] def functor_extension₁ : (C ⥤ karoubi D) ⥤ (karoubi C ⥤ karoubi D) := { obj := functor_extension₁.obj, map := λ F G, functor_extension₁.map, map_id' := λ F, by { ext P, exact comp_p (F.map P.p), }, map_comp' := λ F G H φ φ', begin ext P, simp only [comp, functor_extension₁.map_app_f, nat_trans.comp_app, assoc], have h := φ.naturality P.p, have h' := F.congr_map P.idem, simp only [hom_ext, comp, F.map_comp] at h h', slice_rhs 2 3 { rw ← h, }, slice_rhs 1 2 { rw h', }, simp only [assoc], end, } lemma functor_extension₁_comp_whiskering_left_to_karoubi : functor_extension₁ C D ⋙ (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C) = 𝟭 _ := begin refine functor.ext _ _, { intro F, refine functor.ext _ _, { intro X, ext, { dsimp, rw [id_comp, comp_id, F.map_id, id_eq], }, { refl, }, }, { intros X Y f, ext, dsimp, simp only [comp_id, eq_to_hom_f, eq_to_hom_refl, comp_p, functor_extension₁.obj_obj_p, to_karoubi_obj_p, comp], dsimp, simp only [functor.map_id, id_eq, p_comp], }, }, { intros F G φ, ext X, dsimp, simp only [eq_to_hom_app, F.map_id, karoubi.comp, eq_to_hom_f, id_eq, p_comp, eq_to_hom_refl, comp_id, comp_p, functor_extension₁.obj_obj_p, to_karoubi_obj_p, F.map_id X], }, end end idempotents end category_theory
f9fb70caa101b0fda0e6f9cc41598a6a96388090
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/algebra/group/basic.lean
46d906ddb2aa7eec826007df65713f035fa3049a
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,298
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro -/ import algebra.group.defs import logic.function.basic /-! # Basic lemmas about semigroups, monoids, and groups This file lists various basic lemmas about semigroups, monoids, and groups. Most proofs are one-liners from the corresponding axioms. For the definitions of semigroups, monoids and groups, see `algebra/group/defs.lean`. -/ universe u section associative variables {α : Type u} (f : α → α → α) [is_associative α f] (x y : α) /-- Composing two associative operations of `f : α → α → α` on the left is equal to an associative operation on the left. -/ lemma comp_assoc_left : (f x) ∘ (f y) = (f (f x y)) := by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] } /-- Composing two associative operations of `f : α → α → α` on the right is equal to an associative operation on the right. -/ lemma comp_assoc_right : (λ z, f z x) ∘ (λ z, f z y) = (λ z, f z (f y x)) := by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] } end associative section semigroup variables {α : Type*} /-- Composing two multiplications on the left by `y` then `x` is equal to a multiplication on the left by `x * y`. -/ @[simp, to_additive "Composing two additions on the left by `y` then `x` is equal to a addition on the left by `x + y`."] lemma comp_mul_left [semigroup α] (x y : α) : ((*) x) ∘ ((*) y) = ((*) (x * y)) := comp_assoc_left _ _ _ /-- Composing two multiplications on the right by `y` and `x` is equal to a multiplication on the right by `y * x`. -/ @[simp, to_additive "Composing two additions on the right by `y` and `x` is equal to a addition on the right by `y + x`."] lemma comp_mul_right [semigroup α] (x y : α) : (* x) ∘ (* y) = (* (y * x)) := comp_assoc_right _ _ _ end semigroup section mul_one_class variables {M : Type u} [mul_one_class M] @[to_additive] lemma ite_mul_one {P : Prop} [decidable P] {a b : M} : ite P (a * b) 1 = ite P a 1 * ite P b 1 := by { by_cases h : P; simp [h], } @[to_additive] lemma eq_one_iff_eq_one_of_mul_eq_one {a b : M} (h : a * b = 1) : a = 1 ↔ b = 1 := by split; { rintro rfl, simpa using h } @[to_additive] lemma one_mul_eq_id : ((*) (1 : M)) = id := funext one_mul @[to_additive] lemma mul_one_eq_id : (* (1 : M)) = id := funext mul_one end mul_one_class section comm_semigroup variables {G : Type u} [comm_semigroup G] @[no_rsimp, to_additive] lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) := left_comm has_mul.mul mul_comm mul_assoc attribute [no_rsimp] add_left_comm @[to_additive] lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b := right_comm has_mul.mul mul_comm mul_assoc @[to_additive] theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) := by simp only [mul_left_comm, mul_assoc] end comm_semigroup local attribute [simp] mul_assoc sub_eq_add_neg section add_monoid variables {M : Type u} [add_monoid M] {a b c : M} @[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 := by rw [bit1, bit0_zero, zero_add] end add_monoid section comm_monoid variables {M : Type u} [comm_monoid M] {x y z : M} @[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z := left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz end comm_monoid section left_cancel_monoid variables {M : Type u} [left_cancel_monoid M] {a b : M} @[simp, to_additive] lemma mul_right_eq_self : a * b = a ↔ b = 1 := calc a * b = a ↔ a * b = a * 1 : by rw mul_one ... ↔ b = 1 : mul_left_cancel_iff @[simp, to_additive] lemma self_eq_mul_right : a = a * b ↔ b = 1 := eq_comm.trans mul_right_eq_self end left_cancel_monoid section right_cancel_monoid variables {M : Type u} [right_cancel_monoid M] {a b : M} @[simp, to_additive] lemma mul_left_eq_self : a * b = b ↔ a = 1 := calc a * b = b ↔ a * b = 1 * b : by rw one_mul ... ↔ a = 1 : mul_right_cancel_iff @[simp, to_additive] lemma self_eq_mul_left : b = a * b ↔ a = 1 := eq_comm.trans mul_left_eq_self end right_cancel_monoid section div_inv_monoid variables {G : Type u} [div_inv_monoid G] @[to_additive] lemma inv_eq_one_div (x : G) : x⁻¹ = 1 / x := by rw [div_eq_mul_inv, one_mul] @[to_additive] lemma mul_one_div (x y : G) : x * (1 / y) = x / y := by rw [div_eq_mul_inv, one_mul, div_eq_mul_inv] lemma mul_div_assoc {a b c : G} : a * b / c = a * (b / c) := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_assoc _ _ _] lemma mul_div_assoc' (a b c : G) : a * (b / c) = (a * b) / c := mul_div_assoc.symm @[simp, to_additive] lemma one_div (a : G) : 1 / a = a⁻¹ := (inv_eq_one_div a).symm end div_inv_monoid section group variables {G : Type u} [group G] {a b c : G} @[simp, to_additive] lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a := by simp [mul_assoc] @[simp, to_additive neg_zero] lemma one_inv : 1⁻¹ = (1 : G) := inv_eq_of_mul_eq_one (one_mul 1) @[to_additive] theorem left_inverse_inv (G) [group G] : function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) := inv_inv @[simp, to_additive] lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv @[simp, to_additive] lemma inv_surjective : function.surjective (has_inv.inv : G → G) := inv_involutive.surjective @[to_additive] lemma inv_injective : function.injective (has_inv.inv : G → G) := inv_involutive.injective @[simp, to_additive] theorem inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff @[simp, to_additive] lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b := by rw [← mul_assoc, mul_right_inv, one_mul] @[to_additive] theorem mul_left_surjective (a : G) : function.surjective ((*) a) := λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩ @[to_additive] theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) := λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩ @[simp, to_additive neg_add_rev] lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := inv_eq_of_mul_eq_one $ by simp @[to_additive] lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ := by simp [h] @[to_additive] lemma eq_inv_of_mul_eq_one (h : a * b = 1) : a = b⁻¹ := have a⁻¹ = b, from inv_eq_of_mul_eq_one h, by simp [this.symm] @[to_additive] lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ := by simp [h.symm] @[to_additive] lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c := by simp [h.symm] @[to_additive] lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c := by simp [h] @[to_additive] lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c := by simp [h] @[to_additive] lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c := by simp [h.symm] @[to_additive] lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c := by simp [h.symm, mul_inv_cancel_left] @[to_additive] lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c := by rw [h, mul_inv_cancel_left] @[to_additive] lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c := by simp [h] @[simp, to_additive] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj _ _ a 1, one_inv] @[simp, to_additive] theorem one_eq_inv : 1 = a⁻¹ ↔ a = 1 := by rw [eq_comm, inv_eq_one] @[to_additive] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm @[to_additive] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := ⟨eq_inv_of_mul_eq_one, λ h, by rw [h, mul_left_inv]⟩ @[to_additive] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive] theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive] theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inj] @[to_additive] lemma div_left_injective : function.injective (λ a, a / b) := by simpa only [div_eq_mul_inv] using λ a a' h, mul_left_injective (b⁻¹) h @[to_additive] lemma div_right_injective : function.injective (λ a, b / a) := by simpa only [div_eq_mul_inv] using λ a a' h, inv_injective (mul_right_injective b h) -- The unprimed version is used by `group_with_zero`. This is the preferred choice. -- See https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/.60div_one'.60 @[simp, to_additive sub_zero] lemma div_one' (a : G) : a / 1 = a := calc a / 1 = a * 1⁻¹ : div_eq_mul_inv a 1 ... = a * 1 : congr_arg _ one_inv ... = a : mul_one a end group section add_group -- TODO: Generalize the contents of this section with to_additive as per -- https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.238667 variables {G : Type u} [add_group G] {a b c d : G} @[simp] lemma sub_self (a : G) : a - a = 0 := by rw [sub_eq_add_neg, add_right_neg a] @[simp] lemma sub_add_cancel (a b : G) : a - b + b = a := by rw [sub_eq_add_neg, neg_add_cancel_right a b] @[simp] lemma add_sub_cancel (a b : G) : a + b - b = a := by rw [sub_eq_add_neg, add_neg_cancel_right a b] lemma add_sub_assoc (a b c : G) : a + b - c = a + (b - c) := by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg] lemma eq_of_sub_eq_zero (h : a - b = 0) : a = b := calc a = a - b + b : (sub_add_cancel a b).symm ... = b : by rw [h, zero_add] lemma sub_ne_zero_of_ne (h : a ≠ b) : a - b ≠ 0 := mt eq_of_sub_eq_zero h @[simp] lemma sub_neg_eq_add (a b : G) : a - (-b) = a + b := by rw [sub_eq_add_neg, neg_neg] @[simp] lemma neg_sub (a b : G) : -(a - b) = b - a := neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, add_right_neg]) local attribute [simp] add_assoc lemma add_sub (a b c : G) : a + (b - c) = a + b - c := by simp lemma sub_add_eq_sub_sub_swap (a b c : G) : a - (b + c) = a - c - b := by simp @[simp] lemma add_sub_add_right_eq_sub (a b c : G) : (a + c) - (b + c) = a - b := by rw [sub_add_eq_sub_sub_swap]; simp lemma eq_sub_of_add_eq (h : a + c = b) : a = b - c := by simp [← h] lemma sub_eq_of_eq_add (h : a = c + b) : a - b = c := by simp [h] lemma eq_add_of_sub_eq (h : a - c = b) : a = b + c := by simp [← h] lemma add_eq_of_eq_sub (h : a = c - b) : a + b = c := by simp [h] @[simp] lemma sub_right_inj : a - b = a - c ↔ b = c := sub_right_injective.eq_iff @[simp] lemma sub_left_inj : b - a = c - a ↔ b = c := by { rw [sub_eq_add_neg, sub_eq_add_neg], exact add_left_inj _ } @[simp] lemma sub_add_sub_cancel (a b c : G) : (a - b) + (b - c) = a - c := by rw [← add_sub_assoc, sub_add_cancel] @[simp] lemma sub_sub_sub_cancel_right (a b c : G) : (a - c) - (b - c) = a - b := by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel] theorem sub_sub_assoc_swap : a - (b - c) = a + c - b := by simp theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩ alias sub_eq_zero ↔ _ sub_eq_zero_of_eq theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero @[simp] theorem sub_eq_self : a - b = a ↔ b = 0 := by rw [sub_eq_add_neg, add_right_eq_self, neg_eq_zero] theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := by rw [sub_eq_add_neg, eq_add_neg_iff_add_eq] theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := by rw [sub_eq_add_neg, add_neg_eq_iff_eq_add] theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : G) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : G) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : G) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : G) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section comm_group variables {G : Type u} [comm_group G] @[to_additive neg_add] lemma mul_inv (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev, mul_comm] @[to_additive] lemma div_eq_of_eq_mul' {a b c : G} (h : a = b * c) : a / b = c := by rw [h, div_eq_mul_inv, mul_comm, inv_mul_cancel_left] @[to_additive] lemma div_mul_comm (a b c d : G) : a / b * (c / d) = a * c / (b * d) := by rw [div_eq_mul_inv, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, mul_assoc, mul_assoc, mul_left_cancel_iff, mul_comm, mul_assoc] end comm_group section add_comm_group -- TODO: Generalize the contents of this section with to_additive as per -- https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.238667 variables {G : Type u} [add_comm_group G] {a b c d : G} local attribute [simp] add_assoc add_comm add_left_comm sub_eq_add_neg lemma sub_add_eq_sub_sub (a b c : G) : a - (b + c) = a - b - c := by simp lemma neg_add_eq_sub (a b : G) : -a + b = b - a := by simp lemma sub_add_eq_add_sub (a b c : G) : a - b + c = a + c - b := by simp lemma sub_sub (a b c : G) : a - b - c = a - (b + c) := by simp lemma sub_add (a b c : G) : a - b + c = a - (b - c) := by simp @[simp] lemma add_sub_add_left_eq_sub (a b c : G) : (c + a) - (c + b) = a - b := by simp lemma eq_sub_of_add_eq' (h : c + a = b) : a = b - c := by simp [h.symm] lemma eq_add_of_sub_eq' (h : a - b = c) : a = b + c := by simp [h.symm] lemma add_eq_of_eq_sub' (h : b = c - a) : a + b = c := begin simp [h], rw [add_comm c, add_neg_cancel_left] end lemma sub_sub_self (a b : G) : a - (a - b) = b := begin simp, rw [add_comm b, add_neg_cancel_left] end lemma add_sub_comm (a b c d : G) : a + b - (c + d) = (a - c) + (b - d) := by simp lemma sub_eq_sub_add_sub (a b c : G) : a - b = c - b + (a - c) := begin simp, rw [add_left_comm c], simp end lemma neg_neg_sub_neg (a b : G) : - (-a - -b) = a - b := by simp @[simp] lemma sub_sub_cancel (a b : G) : a - (a - b) = b := sub_sub_self a b @[simp] lemma sub_sub_cancel_left (a b : G) : a - b - a = -b := by simp lemma sub_eq_neg_add (a b : G) : a - b = -b + a := by rw [sub_eq_add_neg, add_comm _ _] theorem neg_add' (a b : G) : -(a + b) = -a - b := by rw [sub_eq_add_neg, neg_add a b] @[simp] lemma neg_sub_neg (a b : G) : -a - -b = b - a := by simp [sub_eq_neg_add, add_comm] lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] @[simp] lemma add_sub_cancel' (a b : G) : a + b - a = b := by rw [sub_eq_neg_add, neg_add_cancel_left] @[simp] lemma add_sub_cancel'_right (a b : G) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] -- This lemma is in the `simp` set under the name `add_neg_cancel_comm_assoc`, -- defined in `algebra/group/commute` lemma add_add_neg_cancel'_right (a b : G) : a + (b + -a) = b := by rw [← sub_eq_add_neg, add_sub_cancel'_right a b] lemma sub_right_comm (a b c : G) : a - b - c = a - c - b := by { repeat { rw sub_eq_add_neg }, exact add_right_comm _ _ _ } @[simp] lemma add_add_sub_cancel (a b c : G) : (a + c) + (b - c) = a + b := by rw [add_assoc, add_sub_cancel'_right] @[simp] lemma sub_add_add_cancel (a b c : G) : (a - c) + (b + c) = a + b := by rw [add_left_comm, sub_add_cancel, add_comm] @[simp] lemma sub_add_sub_cancel' (a b c : G) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel @[simp] lemma add_sub_sub_cancel (a b c : G) : (a + b) - (a - c) = b + c := by rw [← sub_add, add_sub_cancel'] @[simp] lemma sub_sub_sub_cancel_left (a b c : G) : (c - a) - (c - b) = b - a := by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel] lemma sub_eq_sub_iff_add_eq_add : a - b = c - d ↔ a + d = c + b := begin rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'], simp only [add_comm, eq_comm] end lemma sub_eq_sub_iff_sub_eq_sub : a - b = c - d ↔ a - c = b - d := by rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, sub_eq_iff_eq_add', add_sub_assoc] end add_comm_group
feab4de12b010550801671dace3890f0707c3d2b
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/ProjFns.lean
9f2f866ceaf1a25f866d2cdad9d748154881db74
[ "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,475
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 -/ import Lean.Environment namespace Lean /- Given a structure `S`, Lean automatically creates an auxiliary definition (projection function) for each field. This structure caches information about these auxiliary definitions. -/ structure ProjectionFunctionInfo where ctorName : Name -- Constructor associated with the auxiliary projection function. numParams : Nat -- Number of parameters in the structure i : Nat -- The field index associated with the auxiliary projection function. fromClass : Bool -- `true` if the structure is a class deriving Inhabited @[export lean_mk_projection_info] def mkProjectionInfoEx (ctorName : Name) (numParams : Nat) (i : Nat) (fromClass : Bool) : ProjectionFunctionInfo := { ctorName, numParams, i, fromClass } @[export lean_projection_info_from_class] def ProjectionFunctionInfo.fromClassEx (info : ProjectionFunctionInfo) : Bool := info.fromClass builtin_initialize projectionFnInfoExt : MapDeclarationExtension ProjectionFunctionInfo ← mkMapDeclarationExtension `projinfo @[export lean_add_projection_info] def addProjectionFnInfo (env : Environment) (projName : Name) (ctorName : Name) (numParams : Nat) (i : Nat) (fromClass : Bool) : Environment := projectionFnInfoExt.insert env projName { ctorName, numParams, i, fromClass } namespace Environment @[export lean_get_projection_info] def getProjectionFnInfo? (env : Environment) (projName : Name) : Option ProjectionFunctionInfo := projectionFnInfoExt.find? env projName def isProjectionFn (env : Environment) (declName : Name) : Bool := projectionFnInfoExt.contains env declName /-- If `projName` is the name of a projection function, return the associated structure name -/ def getProjectionStructureName? (env : Environment) (projName : Name) : Option Name := match env.getProjectionFnInfo? projName with | none => none | some projInfo => match env.find? projInfo.ctorName with | some (ConstantInfo.ctorInfo val) => some val.induct | _ => none end Environment def isProjectionFn [MonadEnv m] [Monad m] (declName : Name) : m Bool := return (← getEnv).isProjectionFn declName def getProjectionFnInfo? [MonadEnv m] [Monad m] (declName : Name) : m (Option ProjectionFunctionInfo) := return (← getEnv).getProjectionFnInfo? declName end Lean
1f214c93f62fc4a01afc3698fc2bb09fc18dd266
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/subst_bug.lean
a086dd62abe5ec9baf70d773f86f285865324659
[ "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
289
lean
open subtype example (f : nat → nat) (a b : nat) : f a = a → f (f a) = a := begin intro h₁, subst h₁ -- ERROR end open nat example (f : nat → nat) (a b : nat) : f a = a → a = 0 → f (f a) = a := begin intro h₁ h₂, subst a, -- should use h₂ rewrite +h₁ end
d9eaa8adbefd6d3424982e4215d28c44b5f84ef8
e0b0b1648286e442507eb62344760d5cd8d13f2d
/src/Lean/Data/Lsp/LanguageFeatures.lean
9650660af036b034f4f2edba1d799651f7859ae4
[ "Apache-2.0" ]
permissive
MULXCODE/lean4
743ed389e05e26e09c6a11d24607ad5a697db39b
4675817a9e89824eca37192364cd47a4027c6437
refs/heads/master
1,682,231,879,857
1,620,423,501,000
1,620,423,501,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,701
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Json import Lean.Data.Lsp.Basic namespace Lean namespace Lsp open Json structure CompletionOptions where triggerCharacters? : Option (Array String) := none allCommitCharacters? : Option (Array String) := none resolveProvider : Bool := false deriving FromJson, ToJson structure CompletionItem where label : String detail? : Option String documentation? : Option MarkupContent /- kind? : CompletionItemKind tags? : CompletionItemTag[] deprecated? : boolean preselect? : boolean sortText? : string filterText? : string insertText? : string insertTextFormat? : InsertTextFormat insertTextMode? : InsertTextMode textEdit? : TextEdit | InsertReplaceEdit additionalTextEdits? : TextEdit[] commitCharacters? : string[] command? : Command data? : any -/ deriving FromJson, ToJson, Inhabited structure CompletionList where isIncomplete : Bool items : Array CompletionItem deriving FromJson, ToJson structure CompletionParams extends TextDocumentPositionParams where -- context? : CompletionContext deriving FromJson, ToJson structure Hover where /- NOTE we should also accept MarkedString/MarkedString[] here but they are deprecated, so maybe can get away without. -/ contents : MarkupContent range? : Option Range := none deriving ToJson, FromJson structure HoverParams extends TextDocumentPositionParams deriving FromJson, ToJson structure DeclarationParams extends TextDocumentPositionParams deriving FromJson, ToJson structure DefinitionParams extends TextDocumentPositionParams deriving FromJson, ToJson structure TypeDefinitionParams extends TextDocumentPositionParams deriving FromJson, ToJson structure DocumentHighlightParams extends TextDocumentPositionParams deriving FromJson, ToJson inductive DocumentHighlightKind where | text | read | write instance : ToJson DocumentHighlightKind where toJson | DocumentHighlightKind.text => 1 | DocumentHighlightKind.read => 2 | DocumentHighlightKind.write => 3 structure DocumentHighlight where range : Range kind? : Option DocumentHighlightKind := none deriving ToJson abbrev DocumentHighlightResult := Array DocumentHighlight structure DocumentSymbolParams where textDocument : TextDocumentIdentifier deriving FromJson, ToJson inductive SymbolKind where | file | module | «namespace» | package | «class» | method | property | field | constructor | enum | interface | function | «variable» | «constant» | string | number | boolean | array | object | key | null | enumMember | struct | event | operator | typeParameter instance : ToJson SymbolKind where toJson | SymbolKind.file => 1 | SymbolKind.module => 2 | SymbolKind.namespace => 3 | SymbolKind.package => 4 | SymbolKind.class => 5 | SymbolKind.method => 6 | SymbolKind.property => 7 | SymbolKind.field => 8 | SymbolKind.constructor => 9 | SymbolKind.enum => 10 | SymbolKind.interface => 11 | SymbolKind.function => 12 | SymbolKind.variable => 13 | SymbolKind.constant => 14 | SymbolKind.string => 15 | SymbolKind.number => 16 | SymbolKind.boolean => 17 | SymbolKind.array => 18 | SymbolKind.object => 19 | SymbolKind.key => 20 | SymbolKind.null => 21 | SymbolKind.enumMember => 22 | SymbolKind.struct => 23 | SymbolKind.event => 24 | SymbolKind.operator => 25 | SymbolKind.typeParameter => 26 structure DocumentSymbolAux (Self : Type) where name : String detail? : Option String := none kind : SymbolKind -- tags? : Array SymbolTag range : Range selectionRange : Range children? : Option (Array Self) := none deriving ToJson inductive DocumentSymbol where | mk (sym : DocumentSymbolAux DocumentSymbol) partial instance : ToJson DocumentSymbol where toJson := let rec go | DocumentSymbol.mk sym => have ToJson DocumentSymbol from ⟨go⟩ toJson sym go structure DocumentSymbolResult where syms : Array DocumentSymbol instance : ToJson DocumentSymbolResult where toJson dsr := toJson dsr.syms inductive SemanticTokenType where | keyword | «variable» | property /- | «namespace» | type | «class» | enum | interface | struct | typeParameter | parameter | enumMember | event | function | method | «macro» | modifier | comment | string | number | regexp | operator -/ def SemanticTokenType.names : Array String := #["keyword", "variable", "property"] -- must be the correct index in `names` def SemanticTokenType.toNat : SemanticTokenType → Nat | keyword => 0 | «variable» => 1 | property => 2 /- inductive SemanticTokenModifier where | declaration | definition | readonly | static | deprecated | abstract | async | modification | documentation | defaultLibrary -/ structure SemanticTokensLegend where tokenTypes : Array String tokenModifiers : Array String deriving FromJson, ToJson structure SemanticTokensOptions where legend : SemanticTokensLegend range : Bool full : Bool /- | { delta?: boolean; } -/ deriving FromJson, ToJson structure SemanticTokensParams where textDocument : TextDocumentIdentifier deriving FromJson, ToJson structure SemanticTokensRangeParams where textDocument : TextDocumentIdentifier range : Range deriving FromJson, ToJson structure SemanticTokens where -- resultId?: string; data : Array Nat deriving FromJson, ToJson end Lsp end Lean
0a92c796e62928cf2cf50d4ca57c552cb58af870
f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58
/linear_algebra/basic.lean
e86e23a53be38b2bf18010935cada01ae96452a7
[ "Apache-2.0" ]
permissive
semorrison/mathlib
1be6f11086e0d24180fec4b9696d3ec58b439d10
20b4143976dad48e664c4847b75a85237dca0a89
refs/heads/master
1,583,799,212,170
1,535,634,130,000
1,535,730,505,000
129,076,205
0
0
Apache-2.0
1,551,697,998,000
1,523,442,265,000
Lean
UTF-8
Lean
false
false
37,204
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Linear algebra -- classical This file is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. We define the following concepts: * `lc α β`: linear combinations over `β` (`α` is the scalar ring) * `span s`: the submodule generated by `s` * `linear_independent s`: states that `s` are linear independent * `linear_independent.repr s b`: choose the linear combination representing `b` on the linear independent vectors `s`. `b` should be in `span b` (uses classical choice) * `is_basis s`: if `s` is a basis, i.e. linear independent and spans the entire space * `is_basis.repr s b`: like `linear_independent.repr` but as a `linear_map` * `is_basis.constr s g`: constructs a `linear_map` by extending `g` from the basis `s` -/ import algebra algebra.big_operators order.zorn data.finset data.finsupp noncomputable theory open classical set function lattice local attribute [instance] prop_decidable reserve infix `≃ₗ` : 50 universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ι : Type x} @[simp] lemma set.diff_self {s : set α} : s \ s = ∅ := set.ext $ by simp lemma zero_ne_one_or_forall_eq_0 (α : Type u) [ring α] : (0 : α) ≠ 1 ∨ (∀a:α, a = 0) := not_or_of_imp $ λ h a, by simpa using congr_arg ((*) a) h.symm namespace finset lemma smul_sum [ring γ] [module γ β] {s : finset α} {a : γ} {f : α → β} : a • (s.sum f) = s.sum (λc, a • f c) := (finset.sum_hom ((•) a) (@smul_zero γ β _ _ a) (assume _ _, smul_add)).symm end finset namespace finsupp lemma smul_sum [has_zero β] [ring γ] [module γ δ] {v : α →₀ β} {c : γ} {h : α → β → δ} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp /-- The type of linear coefficients, which are simply the finitely supported functions from the module `β` to the scalar ring `α`. -/ @[reducible] def lc (α : Type u) (β : Type v) [ring α] [module α β] : Type (max u v) := β →₀ α namespace lc variables [ring α] [module α β] instance : has_scalar α (lc α β) := finsupp.to_has_scalar instance : module α (lc α β) := finsupp.to_module α lemma is_linear_map_sum [module α γ] [module α δ] {f : β → α → γ} {g : δ → lc α β} (hf : ∀b, is_linear_map (f b)) (hg : is_linear_map g) : is_linear_map (λd, (g d).sum f) := ⟨assume d₁ d₂, by simp [hg.add, finsupp.sum_add_index, (hf _).zero, (hf _).add], assume a d, by simp [hg.smul, finsupp.sum_smul_index, (hf _).zero, finsupp.smul_sum, ((hf _).smul _ _).symm]⟩ end lc namespace is_linear_map @[simp] lemma finsupp_sum [ring α] [module α β] [module α γ] [has_zero δ] {f : β → γ} {t : ι →₀ δ} {g : ι → δ → β} (hf : is_linear_map f) : f (t.sum g) = t.sum (λi d, f (g i d)) := hf.sum end is_linear_map structure linear_equiv {α : Type u} [ring α] (β : Type v) (γ : Type w) [module α β] [module α γ] extends equiv β γ := (linear_fun : is_linear_map to_fun) infix ` ≃ₗ ` := linear_equiv namespace linear_equiv variables [ring α] [module α β] [module α γ] [module α δ] include α lemma linear_inv (e : β ≃ₗ γ) : is_linear_map e.inv_fun := e.linear_fun.inverse e.left_inv e.right_inv section variable (β) def refl : β ≃ₗ β := { linear_fun := is_linear_map.id, .. equiv.refl β } end def symm (e : β ≃ₗ γ) : γ ≃ₗ β := { linear_fun := e.linear_inv, .. e.to_equiv.symm } def trans (e₁ : β ≃ₗ γ) (e₂ : γ ≃ₗ δ) : β ≃ₗ δ := { linear_fun := is_linear_map.comp e₂.linear_fun e₁.linear_fun, .. e₁.to_equiv.trans e₂.to_equiv } end linear_equiv section module variables [ring α] [module α β] [module α γ] [module α δ] variables {a a' : α} {s t : set β} {b b' b₁ b₂ : β} include α /-- Linear span of a set of vectors -/ def span (s : set β) : set β := { x | ∃(v : lc α β), (∀x∉s, v x = 0) ∧ x = v.sum (λb a, a • b) } instance is_submodule_span : is_submodule (span s) := { zero_ := ⟨0, by simp [finsupp.sum_zero_index]⟩, add_ := assume x y ⟨vx, hx, eqx⟩ ⟨vy, hy, eqy⟩, ⟨vx + vy, by simp [hx, hy, eqx, eqy, finsupp.sum_add_index, add_smul] {contextual := tt}⟩, smul := assume a b ⟨v, hv, veq⟩, ⟨a • v, by simp [hv, veq, finsupp.sum_smul_index, finsupp.smul_sum, smul_smul] {contextual := tt}⟩ } lemma subset_span : s ⊆ span s := assume b (hb : b ∈ s), have ∀b'∉s, b ≠ b', by intros b' hb' ne; cc, ⟨finsupp.single b 1, by simp [finsupp.sum_single_index, this] {contextual := tt}⟩ lemma span_eq_of_is_submodule (hs : is_submodule s) : span s = s := have span s ⊆ s, from assume b ⟨v, hv, eq⟩, have ∀c, v c • c ∈ s, from assume c, is_submodule.smul_ne_0 $ not_imp_comm.mp $ hv c, eq.symm ▸ is_submodule.sum (by simp [this] {contextual := tt}), subset.antisymm this subset_span lemma span_mono (h : t ⊆ s) : span t ⊆ span s := assume b ⟨v, hv, eq⟩, ⟨v, assume b, hv b ∘ mt (@h b), eq⟩ lemma span_minimal (hs : is_submodule s) (h : t ⊆ s) : span t ⊆ s := calc span t ⊆ span s : span_mono h ... = s : span_eq_of_is_submodule hs lemma span_eq (hs : is_submodule s) (hts : t ⊆ s) (hst : s ⊆ span t) : span t = s := subset.antisymm (span_minimal hs hts) hst @[simp] lemma span_empty : span (∅ : set β) = {0} := span_eq is_submodule.single_zero (empty_subset _) (by simp [subset_def, is_submodule.zero]) lemma is_submodule_range_smul : is_submodule $ range (λa, a • b) := is_submodule.range $ is_linear_map.map_smul_left is_linear_map.id lemma span_singleton : span {b} = range (λa, a • b) := span_eq is_submodule_range_smul (assume b' hb', ⟨1, by simp * at *⟩) (assume b' ⟨a, eq⟩, eq ▸ is_submodule.smul _ $ subset_span $ mem_singleton _) lemma span_union : span (s ∪ t) = {z | ∃x∈span s, ∃y∈span t, z = x + y } := span_eq is_submodule.add_submodule (union_subset (assume x hx, ⟨x, subset_span hx, 0, is_submodule.zero, by simp⟩) (assume y hy, ⟨0, is_submodule.zero, y, subset_span hy, by simp⟩)) (assume b ⟨x, hx, y, hy, eq⟩, eq.symm ▸ is_submodule.add (span_mono (subset_union_left _ _) hx) (span_mono (subset_union_right _ _) hy)) lemma span_insert_eq_span (h : b ∈ span s) : span (insert b s) = span s := span_eq is_submodule_span (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_insert : span (insert b s) = {z | ∃a, ∃x∈span s, z = a • b + x } := set.ext $ assume b', begin split; simp [insert_eq, span_union, span_singleton, set.ext_iff, range, -add_comm], exact (assume y a eq_y x hx eq, ⟨a, x, hx, by simp [eq_y, eq]⟩), exact (assume a b₂ hb₂ eq, ⟨a • b, ⟨a, rfl⟩, b₂, hb₂, eq⟩) end lemma mem_span_insert : b₁ ∈ span (insert b s) ↔ ∃a, b₁ + a • b ∈ span s := begin simp [span_insert], constructor, exact assume ⟨a, b, hb, eq⟩, ⟨-a, by simp [eq, hb]⟩, exact assume ⟨a, hb⟩, ⟨-a, _, hb, by simp⟩ end @[simp] lemma span_span : span (span s) = span s := span_eq_of_is_submodule is_submodule_span @[simp] lemma span_image_of_linear_map {f : β → γ} (hf : is_linear_map f) : span (f '' s) = f '' span s := subset.antisymm (span_minimal (is_submodule.image hf) (image_subset _ subset_span)) (image_subset_iff.mpr $ span_minimal (is_submodule.preimage hf) (image_subset_iff.mp subset_span)) lemma linear_eq_on {f g : β → γ} (hf : is_linear_map f) (hg : is_linear_map g) (h : ∀x∈s, f x = g x) : ∀{x}, x ∈ span s → f x = g x | _ ⟨l, hl, rfl⟩ := begin simp [hf.finsupp_sum, hg.finsupp_sum], apply finset.sum_congr rfl, assume b hb, have : b ∈ s, { by_contradiction, simp * at * }, simp [this, h, hf.smul, hg.smul] end /-- Linearly independent set of vectors -/ def linear_independent (s : set β) : Prop := ∀l : lc α β, (∀x∉s, l x = 0) → l.sum (λv c, c • v) = 0 → l = 0 lemma linear_independent_empty : linear_independent (∅ : set β) := assume l hl eq, finsupp.ext $ by simp * at * lemma linear_independent.mono (hs : linear_independent s) (h : t ⊆ s) : linear_independent t := assume l hl eq, hs l (assume b, hl b ∘ mt (@h b)) eq lemma zero_not_mem_of_linear_independent (ne : 0 ≠ (1:α)) (hs : linear_independent s) : (0:β) ∉ s := assume (h : 0 ∈ s), let l : lc α β := finsupp.single 0 1 in have l = 0, from hs l (by intro x; by_cases 0 = x; simp [l, finsupp.single_apply, *] at *) (by simp [finsupp.sum_single_index]), have l 0 = 1, from finsupp.single_eq_same, by rw [‹l = 0›] at this; simp * at * lemma linear_independent_union {s t : set β} (hs : linear_independent s) (ht : linear_independent t) (hst : span s ∩ span t = {0}) : linear_independent (s ∪ t) := (zero_ne_one_or_forall_eq_0 α).elim (assume ne l hl eq0, let ls := l.filter $ λb, b ∈ s, lt := l.filter $ λb, b ∈ t in have hls : ↑ls.support ⊆ s, by simp [ls, subset_def], have hlt : ↑lt.support ⊆ t, by simp [ls, subset_def], have lt.sum (λb a, a • b) ∈ span t, from is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hlt hb, have l = ls + lt, from have ∀b, b ∈ s → b ∉ t, from assume b hbs hbt, have b ∈ span s ∩ span t, from ⟨subset_span hbs, subset_span hbt⟩, have b = 0, by rw [hst] at this; simp * at *, zero_not_mem_of_linear_independent ne hs $ this ▸ hbs, have lt = l.filter (λb, b ∉ s), from finsupp.ext $ assume b, by by_cases b ∈ t; by_cases b ∈ s; simp * at *, by rw [this]; exact finsupp.filter_pos_add_filter_neg.symm, have ls.sum (λb a, a • b) + lt.sum (λb a, a • b) = l.sum (λb a, a • b), by rw [this, finsupp.sum_add_index]; simp [add_smul], have ls_eq_neg_lt : ls.sum (λb a, a • b) = - lt.sum (λb a, a • b), from eq_of_sub_eq_zero $ by simp [this, eq0], have ls_sum_eq : ls.sum (λb a, a • b) = 0, from have - lt.sum (λb a, a • b) ∈ span t, from is_submodule.neg $ is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hlt hb, have ls.sum (λb a, a • b) ∈ span s ∩ span t, from ⟨is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hls hb, ls_eq_neg_lt.symm ▸ this⟩, by rw [hst] at this; simp * at *, have ls = 0, from hs _ (finsupp.support_subset_iff.mp hls) ls_sum_eq, have lt_sum_eq : lt.sum (λb a, a • b) = 0, from eq_of_neg_eq_neg $ by rw [←ls_eq_neg_lt, ls_sum_eq]; simp, have lt = 0, from ht _ (finsupp.support_subset_iff.mp hlt) lt_sum_eq, by simp [‹l = ls + lt›, ‹ls = 0›, ‹lt = 0›]) (assume eq_0 l _ _, finsupp.ext $ assume b, eq_0 _) lemma linear_independent_Union_of_directed {s : set (set β)} (hs : ∀a∈s, ∀b∈s, ∃c∈s, a ∪ b ⊆ c) (h : ∀a∈s, linear_independent a) : linear_independent (⋃₀s) := assume l hl eq, have ∀f:finset β, {x | x ∈ f} ⊆ ⋃₀ s → f = ∅ ∨ (∃t∈s, {x | x ∈ f} ⊆ t), from assume f, finset.induction_on f (by simp) $ assume a f haf ih haf_s, let ⟨t, ht, hat⟩ := haf_s $ finset.mem_insert_self _ _ in have f = ∅ ∨ ∃ (t : set β) (H : t ∈ s), {x : β | x ∈ f} ⊆ t, from ih $ assume x hx, haf_s $ finset.mem_insert_of_mem hx, or.inr $ this.elim (assume : f = ∅, ⟨t, ht, by simp [this, hat, subset_def]⟩) (assume ⟨t', ht', hft⟩, let ⟨t'', ht''s, ht''⟩ := hs t ht t' ht' in have a ∈ t'', from ht'' $ or.inl hat, have ∀x, x ∈ f → x ∈ t'', from subset.trans (subset.trans hft $ subset_union_right _ _) ht'', ⟨t'', ht''s, by simp [subset_def, or_imp_distrib, *] {contextual := tt}⟩), have l.support = ∅ ∨ (∃t∈s, {x | x ∈ l.support} ⊆ t), from this _ $ by intros x hx; by_contradiction; simp * at *, this.elim (assume : l.support = ∅, by simp [finset.ext] at this; exact finsupp.ext this) (assume ⟨t, ht, hts⟩, have ∀x, l x ≠ 0 → x ∈ t, by simpa using hts, h t ht l (assume x, not_imp_comm.mp $ this x) eq) lemma linear_independent_bUnion_of_directed {ι : Type w} {i : set ι} {s : ι → set β} (hs : ∀a∈i, ∀b∈i, ∃c∈i, s a ∪ s b ⊆ s c) (h : ∀a∈i, linear_independent (s a)) : linear_independent (⋃a∈i, s a) := have linear_independent (⋃₀ (s '' i)), from linear_independent_Union_of_directed (assume a ⟨j, hj, a_eq⟩ b ⟨l, hl, b_eq⟩, let ⟨k, hk, h⟩ := hs j hj l hl in ⟨s k, mem_image_of_mem _ hk, a_eq ▸ b_eq ▸ h⟩) (assume a ⟨j, hj, a_eq⟩, a_eq ▸ h j hj), by rwa [sUnion_image] at this lemma linear_independent.unique (hs : linear_independent s) {l₁ l₂ : lc α β} (h₁ : ∀x∉s, l₁ x = 0) (h₂ : ∀x∉s, l₂ x = 0) (eq : l₁.sum (λv c, c • v) = l₂.sum (λv c, c • v)) : l₁ = l₂ := eq_of_sub_eq_zero $ show l₁ - l₂ = 0, from hs (l₁ - l₂) (by simp [h₁, h₂] {contextual:=tt}) (by simp [finsupp.sum_sub_index, eq, sub_smul, -sub_eq_add_neg, sub_self]) section repr variables (hs : linear_independent s) def linear_independent.repr (hs : linear_independent s) (b : β) : lc α β := if h : b ∈ span s then classical.some h else 0 lemma repr_not_span (h : b ∉ span s) : hs.repr b = 0 := dif_neg h lemma repr_spec (h : b ∈ span s) : (∀b'∉s, hs.repr b b' = 0) ∧ b = (hs.repr b).sum (λb a, a • b) := have hs.repr b = classical.some h, from dif_pos h, by rw [this]; exact classical.some_spec h lemma repr_eq_zero (hb' : b' ∉ s) : hs.repr b b' = 0 := by_cases (assume : b ∈ span s, (repr_spec hs this).left _ hb') (assume : b ∉ span s, by rw [repr_not_span hs this]; refl) lemma repr_sum_eq (hb : b ∈ span s) : (hs.repr b).sum (λb a, a • b) = b := (repr_spec hs hb).right.symm lemma repr_eq {l : lc α β} (hb : b ∈ span s) (h : ∀x∉s, l x = 0) (eq : l.sum (λv c, c • v) = b) : hs.repr b = l := hs.unique (assume b, repr_eq_zero hs) h (by rw [repr_sum_eq hs hb, eq]) lemma repr_eq_single (hb : b ∈ s) : hs.repr b = finsupp.single b 1 := repr_eq hs (subset_span hb) (assume b' hb', finsupp.single_eq_of_ne $ show b ≠ b', from assume eq, by simp * at *) (by simp [finsupp.sum_single_index, add_smul]) @[simp] lemma repr_zero : hs.repr 0 = 0 := repr_eq hs is_submodule.zero (by simp) (by simp [finsupp.sum_zero_index]) lemma repr_support : ↑(hs.repr b).support ⊆ s := assume x hx, classical.by_contradiction $ assume hxs, by simp at hx; exact hx (repr_eq_zero hs hxs) @[simp] lemma repr_add (hb : b ∈ span s) (hb' : b' ∈ span s) : hs.repr (b + b') = hs.repr b + hs.repr b' := repr_eq hs (is_submodule.add hb hb') (by simp [repr_eq_zero] {contextual := tt}) (by simp [finsupp.sum_add_index, add_smul, repr_sum_eq hs, hb, hb']) @[simp] lemma repr_smul (hb : b ∈ span s) : hs.repr (a • b) = a • hs.repr b := repr_eq hs (is_submodule.smul _ hb) (by simp [repr_eq_zero] {contextual := tt}) (calc (a • hs.repr b).sum (λb a, a • b) = (hs.repr b).sum (λb a', a • (a' • b)) : by simp [finsupp.sum_smul_index, add_smul, smul_smul] ... = a • (hs.repr b).sum (λb a', a' • b) : finsupp.smul_sum.symm ... = a • b : by rw [repr_sum_eq hs hb]) @[simp] lemma repr_neg : hs.repr (- b) = - hs.repr b := by_cases (assume hb : b ∈ span s, have hs.repr ((-1) • b) = (-1) • hs.repr b, from repr_smul hs hb, by simpa) (assume hb : b ∉ span s, have -b ∉ span s, from assume hb, have - - b ∈ span s, from is_submodule.neg hb, by simpa, by simp [repr_not_span, this, hb]) @[simp] lemma repr_sub (hb : b ∈ span s) (hb' : b' ∈ span s) : hs.repr (b - b') = hs.repr b - hs.repr b' := by simp [repr_add hs hb, repr_neg hs, is_submodule.neg hb'] @[simp] lemma repr_sum {ι : Type w} {f : finset ι} {b : ι → β} : (∀i∈f, b i ∈ span s) → hs.repr (f.sum b) = f.sum (λi, hs.repr (b i)) := by apply f.induction_on; simp [or_imp_distrib, forall_and_distrib, repr_add hs, is_submodule.sum] {contextual := tt} @[simp] lemma repr_finsupp_sum {ι : Type w} {δ : Type x} [has_zero δ] {f : ι →₀ δ} {b : ι → δ → β} : (∀i∈f.support, b i (f i) ∈ span s) → hs.repr (f.sum b) = f.sum (λi d, hs.repr (b i d)) := repr_sum hs lemma repr_eq_repr_of_subset {ht : linear_independent t} (h : t ⊆ s) (hb : b ∈ span t) : ht.repr b = hs.repr b := eq.symm $ repr_eq hs (span_mono h hb) (assume x hx, repr_eq_zero _ $ assume hxt, hx $ h hxt) (repr_sum_eq ht hb) end repr section variables {f : β → γ} {l : lc α β} (hs : linear_independent (f '' s)) (hf : is_linear_map f) (hf_inj : ∀a∈s, ∀b∈s, f a = f b → a = b) (hl : ∀x∉s, l x = 0) include hs hf hf_inj private lemma l_eq_0 (h : f (l.sum (λb a, a • b)) = 0) : l = 0 := have l_imp_s : ∀{x}, l x ≠ 0 → x ∈ s, from assume x hx, classical.by_contradiction $ assume hnx, hx $ hl _ $ hnx, have ∀c, c ∉ f '' s → c ∉ (l.map_domain f).support, from assume c, mt $ assume hb, have c ∈ l.support.image f, from finsupp.map_domain_support hb, have ∃b, l b ≠ 0 ∧ f b = c, by simpa, let ⟨b, hb, c_eq⟩ := this in ⟨b, l_imp_s hb, c_eq⟩, have l.map_domain f = 0, from hs _ (by simpa) $ calc (l.map_domain f).sum (λb a, a • b) = f (l.sum (λb a, a • b)): by simp [finsupp.sum_map_domain_index, add_smul, hf.finsupp_sum, hf.smul] ... = 0 : h, calc l = l.map_domain id : by rw [finsupp.map_domain_id] ... = l.map_domain (@inv_fun_on _ ⟨0⟩ _ f s ∘ f) : finsupp.map_domain_congr $ assume b hb, (@inv_fun_on_eq' _ ⟨0⟩ _ _ _ _ hf_inj $ l_imp_s $ by simpa using hb).symm ... = 0 : by rw [finsupp.map_domain_comp, this, finsupp.map_domain_zero] lemma linear_independent.of_image : linear_independent s := assume l hl eq, l_eq_0 hs hf hf_inj hl $ by simp [eq, hf.zero] lemma linear_independent.eq_0_of_span : ∀a∈span s, f a = 0 → a = 0 | _ ⟨l, hl, rfl⟩ eq_0 := by simp [l_eq_0 hs hf hf_inj hl eq_0, finsupp.sum_zero_index] end /-- A set of vectors is a basis if it is linearly independent and all vectors are in the span -/ def is_basis (s : set β) := linear_independent s ∧ (∀x, x ∈ span s) section is_basis lemma is_basis.map_repr (hs : is_basis s) : is_linear_map hs.1.repr := ⟨assume b₁ b₂, repr_add hs.1 (hs.2 _) (hs.2 _), assume a b, repr_smul hs.1 (hs.2 _)⟩ def is_basis.constr (hs : is_basis s) (f : β → γ) (b : β) : γ := (hs.1.repr b).sum (λb a, a • f b) lemma is_basis.map_constr (hs : is_basis s) {f : β → γ} : is_linear_map (hs.constr f) := lc.is_linear_map_sum (assume b, is_linear_map.map_smul_left is_linear_map.id) hs.map_repr lemma is_basis.eq_linear_map {f g : β → γ} (hf : is_linear_map f) (hg : is_linear_map g) (hs : is_basis s) (h : ∀b∈s, f b = g b) : f = g := funext $ assume b, linear_eq_on hf hg h (hs.2 b) lemma constr_congr {f g : β → γ} {b : β} (hs : is_basis s) (h : ∀b∈s, f b = g b) : hs.constr f = hs.constr g := funext $ assume b', finset.sum_congr rfl $ assume b hb, have b ∈ s, from repr_support hs.1 hb, by simp [h b this] lemma constr_basis {f : β → γ} {b : β} (hs : is_basis s) (hb : b ∈ s) : (hs.constr f : β → γ) b = f b := show (hs.1.repr b).sum (λb a, a • f b) = f b, by simp [hs.1, hs.2, hb, repr_eq_single, finsupp.sum_single_index] lemma constr_eq {g : β → γ} {f : β → γ} (hs : is_basis s) (hf : is_linear_map f) (h : ∀x∈s, g x = f x) : hs.constr g = f := hs.eq_linear_map hs.map_constr hf $ assume b hb, h b hb ▸ constr_basis hs hb lemma constr_zero (hs : is_basis s) : hs.constr (λb, (0 : γ)) = (λb, 0) := constr_eq hs is_linear_map.map_zero $ by simp lemma constr_add {g f : β → γ} (hs : is_basis s) : hs.constr (λb, f b + g b) = (λb, hs.constr f b + hs.constr g b) := constr_eq hs (is_linear_map.map_add hs.map_constr hs.map_constr) $ by simp [constr_basis hs] {contextual := tt} lemma constr_sub {g f : β → γ} (hs : is_basis s) : hs.constr (λb, f b - g b) = (λb, hs.constr f b - hs.constr g b) := constr_eq hs (is_linear_map.map_sub hs.map_constr hs.map_constr) $ by simp [constr_basis hs] {contextual := tt} lemma constr_neg {f : β → γ} (hs : is_basis s) : hs.constr (λb, - f b) = (λb, - hs.constr f b) := constr_eq hs hs.map_constr.map_neg $ by simp [constr_basis hs] {contextual := tt} -- this only works on functions if `α` is a commutative ring lemma constr_smul {α : Type u} {β : Type v} {γ : Type w} [comm_ring α] [module α β] [module α γ] {f : β → γ} {a : α} {s : set β} (hs : is_basis s) {b : β} : hs.constr (λb, a • f b) = (λb, a • (hs.constr f) b) := constr_eq hs hs.map_constr.map_smul_right $ by simp [constr_basis hs] {contextual := tt} lemma constr_mem_span (hs : is_basis s) {f : β → γ} : (hs.constr f : β → γ) b ∈ span (f '' s) := is_submodule.sum $ assume b' hb', have b' ∈ s, from repr_support hs.1 hb', is_submodule.smul _ $ subset_span $ mem_image_of_mem _ this lemma constr_im_eq_span (hs : is_basis s) {f : β → γ} : range (hs.constr f) = span (f '' s) := eq.symm $ span_eq (is_submodule.range hs.map_constr) (assume b' ⟨b, hb, eq⟩, ⟨b, eq ▸ constr_basis hs hb⟩) (assume b' ⟨b, hb⟩, hb ▸ constr_mem_span hs) def module_equiv_lc (hs : is_basis s) : β ≃ (s →₀ α) := { to_fun := assume b, (hs.1.repr b).subtype_domain _, inv_fun := assume v, v.sum $ λb a, a • b.1, left_inv := assume b, calc ((hs.1.repr b).subtype_domain s).sum (λb a, a • b.1) = (hs.1.repr b).sum (λb a, a • b) : @finsupp.sum_subtype_domain_index β _ _ _ _ (λx, x ∈ s) _ _ _ _ (λb a, a • b) (repr_support hs.1) ... = _ : repr_sum_eq _ $ hs.2 _, right_inv := assume v, finsupp.ext $ assume ⟨b, hb⟩, have v.sum (λb' a, hs.1.repr (a • b'.val) b) = v ⟨b, hb⟩, from calc v.sum (λb' a, hs.1.repr (a • b'.val) b) = v.sum (λb' a, a * (finsupp.single b'.val 1 : lc α β) b) : finset.sum_congr rfl $ assume ⟨b', hb'⟩ h', by dsimp; rw [repr_smul hs.1 (hs.2 _), repr_eq_single _ hb']; refl ... = ({⟨b, hb⟩} : finset s).sum (λb', v b' * (finsupp.single b'.val 1 : lc α β) b) : finset.sum_bij_ne_zero (λx hx x0, x) (assume ⟨x, hx⟩, by by_cases x = b; simp [*]) (by simp) (assume ⟨x, hx⟩, by simp; intro e; subst x; exact assume h, ⟨b, hb, assume h', by simp * at *, h, rfl⟩) (by simp) ... = v ⟨b, hb⟩ : by simp, begin dsimp, rw [repr_finsupp_sum, finsupp.sum_apply], { exact this }, { simp [hs.2] } end } def equiv_of_is_basis {s : set β} {t : set γ} {f : β → γ} {g : γ → β} (hs : is_basis s) (ht : is_basis t) (hf : ∀b∈s, f b ∈ t) (hg : ∀c∈t, g c ∈ s) (hgf : ∀b∈s, g (f b) = b) (hfg : ∀c∈t, f (g c) = c) : β ≃ₗ γ := { to_fun := hs.constr f, inv_fun := ht.constr g, left_inv := assume b, congr_fun (hs.eq_linear_map (ht.map_constr.comp hs.map_constr) is_linear_map.id $ by simp [constr_basis, hs, ht, hf, hgf, (∘)] {contextual := tt}) b, right_inv := assume c, congr_fun (ht.eq_linear_map (hs.map_constr.comp ht.map_constr) is_linear_map.id $ by simp [constr_basis, hs, ht, hg, hfg, (∘)] {contextual := tt}) c, linear_fun := hs.map_constr } end is_basis lemma linear_independent.inj_span_iff_inj {s : set β} {f : β → γ} (hf : is_linear_map f) (hfs : linear_independent (f '' s)) : (∀a∈span s, ∀b∈span s, f a = f b → a = b) ↔ (∀a∈s, ∀b∈s, f a = f b → a = b) := iff.intro (assume h a ha b hb eq, h a (subset_span ha) b (subset_span hb) eq) (assume h a ha b hb eq, eq_of_sub_eq_zero $ hfs.eq_0_of_span hf h _ (is_submodule.sub ha hb) (by simp [eq, hf.add, hf.neg])) -- TODO: clean up proof / alternative proof lemma linear_independent.image {s : set β} {f : β → γ} (hf : is_linear_map f) (hs : linear_independent s) (hf_inj : ∀a∈span s, ∀b∈span s, f a = f b → a = b) : linear_independent (f '' s) := let g := @inv_fun_on _ ⟨0⟩ _ f (span s) in have hg : ∀x∈span s, g (f x) = x, from assume x, @inv_fun_on_eq' _ ⟨0⟩ _ _ _ _ hf_inj, assume l hl eq, have l_g : ∀b∈(l.map_domain g).support, b ∈ s, from assume b hb, have b ∈ l.support.image g, from finsupp.map_domain_support hb, have ∃c, l c ≠ 0 ∧ g c = b, by simpa, let ⟨c, hc, b_eq⟩ := this in have c ∈ f '' s, by by_contradiction h; simp * at *, let ⟨b', hb', c_eq⟩ := this in have b' = b, from b_eq ▸ c_eq ▸ (hg _ $ subset_span hb').symm, this ▸ hb', have l_f_g : l.map_domain (f ∘ g) = l.map_domain id, from finsupp.map_domain_congr $ assume c hc, have c ∈ f '' s, by by_contradiction h; simp * at *, let ⟨b, hb, c_eq⟩ := this in by simp [c_eq.symm, (∘), hg, subset_span hb], have l.map_domain g = 0, from have l_g_s : (l.map_domain g).sum (λb a, a • b) ∈ span s, from is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ l_g b hb, have f_sum : f ((l.map_domain g).sum (λb a, a • b)) = 0, from calc f ((l.map_domain g).sum (λb a, a • b)) = ((l.map_domain g).map_domain f).sum (λb a, a • b) : by simp [finsupp.sum_map_domain_index, add_smul, hf.finsupp_sum, hf.smul] ... = 0 : by rw [←finsupp.map_domain_comp, l_f_g, finsupp.map_domain_id, eq], have ∀b∉s, (l.map_domain g) b = 0, from assume b hb, classical.by_contradiction $ assume hnb, hb $ l_g b $ by simp *, hs _ this $ hf_inj _ l_g_s _ is_submodule.zero (by simpa [hf.zero] using f_sum), calc l = (l.map_domain g).map_domain f : by rw [←finsupp.map_domain_comp, l_f_g, finsupp.map_domain_id] ... = 0 : by rw [this, finsupp.map_domain_zero] lemma linear_map.linear_independent_image_iff {s : set β} {f : β → γ} (hf : is_linear_map f) (hf_inj : ∀a∈span s, ∀b∈span s, f a = f b → a = b) : linear_independent (f '' s) ↔ linear_independent s := iff.intro (assume h, h.of_image hf $ assume x hx y hy, hf_inj x (subset_span hx) y (subset_span hy)) (assume h, h.image hf hf_inj) end module section vector_space variables [field α] [vector_space α β] [vector_space α γ] {s t : set β} {b b₁ b₂ : β} include α local attribute [instance] is_submodule_span /- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class (instead of a data containing type classs) -/ lemma mem_span_insert_exchange : b₁ ∈ span (insert b₂ s) → b₁ ∉ span s → b₂ ∈ span (insert b₁ s) := begin simp [span_insert], exact assume a b₃ hb₃ b₁_eq hb₁, have a ≠ 0, from assume a0, by simp * at *, ⟨1/a, (- 1/a) • b₃, is_submodule.smul _ hb₃, by simp [b₁_eq, smul_add, smul_smul, mul_inv_cancel, this, neg_div]⟩ end lemma linear_independent_iff_not_mem_span : linear_independent s ↔ (∀b∈s, b ∉ span (s \ {b})) := iff.intro (assume (hs : linear_independent s) b hb ⟨l, hl, b_eq⟩, let l' := l - finsupp.single b 1 in have ∀b', b' ∉ s → l' b' = 0, from assume b' hb', have ne: b ≠ b', from assume h, hb' $ h ▸ hb, have b' ∉ s \ {b}, from assume ⟨h₁, h₂⟩, hb' h₁, by simp [ne, hl b' this], have l' = 0, from hs l' this $ by simp [l', finsupp.sum_add_index, finsupp.sum_neg_index, add_smul, b_eq.symm, finsupp.sum_single_index], have - l' b = 1, from have b ∉ s \ {b}, by simp, by simp [hl _ this], by rw [‹l' = 0›] at this; simp at this; assumption) (assume hs l hl eq, finsupp.ext $ assume b, classical.by_contradiction $ assume h : l b ≠ 0, let a := -1 / l b in hs b (show b ∈ s, from classical.by_contradiction $ assume hnb, h $ hl b hnb) ⟨a • l + finsupp.single b 1, assume b', by_cases (assume : b' = b, by simp [this, h, neg_div, a]) (assume : b' ≠ b, by simp [finsupp.sub_apply, hl, this, this.symm] {contextual:=tt}), have l.sum (λb a', (a * a') • b) = a • l.sum (λb a, a • b), by simp [finsupp.smul_sum, smul_smul], by simp [-sub_eq_add_neg, add_smul, finsupp.sum_add_index, finsupp.sum_single_index, finsupp.sum_smul_index, this, eq]⟩) lemma linear_independent_singleton {b : β} (hb : b ≠ 0) : linear_independent ({b} : set β) := linear_independent_iff_not_mem_span.mpr $ by simp [hb] {contextual := tt} lemma linear_independent.insert (hs : linear_independent s) (hb : b ∉ span s) : linear_independent (insert b s) := assume l hl eq, by_cases (assume : l b = 0, hs l (assume x hx, by_cases (assume h : x = b, h.symm ▸ this) (assume h', hl _ $ by simp [not_or_distrib, hx, h'])) eq) (assume lb_ne_zero : l b ≠ 0, have (1 / l b) • (- (- l b • b)) ∈ span s, from is_submodule.smul _ $ is_submodule.neg ⟨l - finsupp.single b (l b), assume x hx, by_cases (assume : b = x, by simp [this.symm]) (assume ne : b ≠ x, have x ∉ insert b s, by simp [not_or_distrib, hx, ne.symm], by simp [hl x this, ne] {contextual := tt}), by simp [finsupp.sum_sub_index, finsupp.sum_single_index, -sub_eq_add_neg, sub_smul, eq]; simp⟩, have (1 / l b) • (- (- l b • b)) = b, by simp [smul_smul, mul_comm, mul_inv_cancel lb_ne_zero], by simp * at *) lemma exists_linear_independent (hs : linear_independent s) (hst : s ⊆ t) : ∃b⊆t, s ⊆ b ∧ t ⊆ span b ∧ linear_independent b := let C := { b : set β // s ⊆ b ∧ b ⊆ t ∧ linear_independent b }, s' : C := ⟨s, le_refl s, hst, hs⟩ in have ∀c, zorn.chain (λa b:C, a.val ⊆ b.val) c → c ≠ ∅ → ∃(m : C), ∀a:C, a ∈ c → a.val ⊆ m.val, from assume c hc ne, let ⟨a, ha⟩ := exists_mem_of_ne_empty ne in ⟨⟨(⋃a ∈ c, (a : C).val), subset.trans a.property.1 $ subset_bUnion_of_mem ha, bUnion_subset $ assume c hc, c.property.right.left, linear_independent_bUnion_of_directed (assume a ha b hb, by_cases (assume h : a = b, ⟨a, ha, h ▸ le_of_eq (@sup_idem (set _) _ a.val)⟩) (assume h : a ≠ b, (hc a ha b hb h).elim (assume h, ⟨b, hb, union_subset h (subset.refl _)⟩) (assume h, ⟨a, ha, union_subset (subset.refl _) h⟩))) (assume a ha, a.property.2.2)⟩, assume a ha, subset_bUnion_of_mem ha⟩, have ∃m:C, ∀a:C, m.val ⊆ a.val → a.val ⊆ m.val, from zorn.zorn (assume c hc, by_cases (assume : c = ∅, ⟨s', assume a, this.symm ▸ false.elim⟩) (this c hc)) (assume a b c, subset.trans), let ⟨⟨m, hsm, hmt, hml⟩, hm⟩ := this in have t ⊆ span m, from classical.by_contradiction $ assume : ¬ t ⊆ span m, let ⟨b, hb⟩ := classical.not_forall.mp this, ⟨hbt, hbm⟩ := not_imp.mp hb in have insert b m ⊆ m, from hm ⟨_, subset.trans hsm $ subset_insert _ _, by simp [set.insert_subset, hmt, hbt], hml.insert hbm⟩ (subset_insert _ _), have b ∈ span m, from subset_span $ this $ mem_insert _ _, hbm this, ⟨m, hmt, hsm, this, hml⟩ lemma exists_subset_is_basis (hs : linear_independent s) : ∃b, s ⊆ b ∧ is_basis b := let ⟨b, hb₀, hb₁, hb₂, hb₃⟩ := exists_linear_independent hs (@subset_univ _ _) in ⟨b, hb₁, hb₃, assume x, hb₂ trivial⟩ variable (β) lemma exists_is_basis : ∃b : set β, is_basis b := let ⟨b, _, hb⟩ := exists_subset_is_basis linear_independent_empty in ⟨b, hb⟩ variable {β} lemma eq_of_linear_independent_of_span (hs : linear_independent s) (h : t ⊆ s) (hst : s ⊆ span t) : s = t := suffices s ⊆ t, from subset.antisymm this h, assume b hb, have (hs.mono h).repr b = finsupp.single b 1, from calc (hs.mono h).repr b = hs.repr b : repr_eq_repr_of_subset hs h $ hst hb ... = finsupp.single b 1 : repr_eq_single hs hb, have b ∈ (↑((hs.mono h).repr b).support : set β), by simp [this], repr_support _ this lemma exists_of_linear_independent_of_finite_span {t : finset β} (hs : linear_independent s) (hst : s ⊆ span ↑t) : ∃t':finset β, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card := have ∀t, ∀(s' : finset β), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ span ↑(s' ∪ t) → ∃t':finset β, ↑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 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), by_cases (assume : s ⊆ span ↑(s' ∪ t), 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 [set.insert_subset_insert hust], subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩) (assume : ¬ s ⊆ span ↑(s' ∪ t), let ⟨b₂, hb₂s, hb₂t⟩ := set.not_subset.mp this in have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h, have s ⊆ span ↑(insert b₂ s' ∪ t), from assume b₃ hb₃, have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set β), by simp [insert_eq, union_subset_union, subset.refl, subset_union_right], have hb₃ : b₃ ∈ span (insert b₁ (insert b₂ ↑(s' ∪ t) : set β)), from span_mono this (hss' hb₃), have s ⊆ span (insert b₁ ↑(s' ∪ t)), by simpa [insert_eq] using hss', have hb₁ : b₁ ∈ span (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 [set.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']⟩)), have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t, from finset.ext.mpr $ assume x, by by_cases x ∈ s; simp *, let ⟨u, h₁, h₂, h⟩ := 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]) in ⟨u, subset.trans h₁ (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}), h₂, by rwa [eq] at h⟩ lemma exists_finite_card_le_of_finite_of_linear_independent_of_span (ht : finite t) (hs : linear_independent s) (hst : s ⊆ span t) : ∃h : finite s, h.to_finset.card ≤ ht.to_finset.card := have s ⊆ span ↑(ht.to_finset), 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 : β → γ} (hf : is_linear_map f) (hf_inj : injective f) : ∃g:γ → β, is_linear_map g ∧ g ∘ f = id := let ⟨bβ, hbβ⟩ := exists_is_basis β in have linear_independent (f '' bβ), from hbβ.1.image hf $ assume b₁ _ b₂ _ eq, hf_inj eq, let ⟨bγ, hbγ₁, hbγ₂⟩ := exists_subset_is_basis this in have ∀b∈bβ, (hbγ₂.constr (@inv_fun _ ⟨0⟩ _ f) : γ → β) (f b) = b, begin assume b hb, rw [constr_basis], { exact @inv_fun_on_eq' β ⟨0⟩ γ f univ b (assume b₁ _ b₂ _ eq, hf_inj eq) trivial }, { exact hbγ₁ (mem_image_of_mem _ hb) } end, ⟨hbγ₂.constr $ @inv_fun _ ⟨0⟩ _ f, hbγ₂.map_constr, hbβ.eq_linear_map (hbγ₂.map_constr.comp hf) is_linear_map.id this⟩ lemma exists_right_inverse_linear_map_of_surjective {f : β → γ} (hf : is_linear_map f) (hf_surj : surjective f) : ∃g:γ → β, is_linear_map g ∧ f ∘ g = id := let g := @inv_fun _ ⟨0⟩ _ f in have ri_gf : right_inverse g f, from @right_inverse_inv_fun _ ⟨0⟩ _ _ hf_surj, have injective g, from injective_of_left_inverse ri_gf, let ⟨bγ, hbγ⟩ := exists_is_basis γ in have ∀c∈bγ, f ((hbγ.constr g : γ → β) c) = c, from assume c hc, by rw [constr_basis hbγ hc, ri_gf], ⟨hbγ.constr g, hbγ.map_constr, hbγ.eq_linear_map (hf.comp hbγ.map_constr) is_linear_map.id this⟩ end vector_space
c3756e1c4697834425067bcb23e004457e39935a
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/tests/pkg/misc/lakefile.lean
dfd791ca281caee8e0a00f10592dc4f543d513d3
[ "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
78
lean
import Lake open System Lake DSL package misc @[defaultTarget] lean_lib Misc
12ee6c3068b779f3d98a79f2c6d1da0434bacdd7
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/rat/basic.lean
4bf4b982ebc5c23775e4235549dd6b4a949b12dc
[ "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
24,156
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The rational numbers as a field generated by the integers, defined as the usual quotient. -/ import data.int algebra.field open int quot eq.ops record prerat : Type := (num : ℤ) (denom : ℤ) (denom_pos : denom > 0) /- prerat: the representations of the rationals as integers num, denom, with denom > 0. note: names are not protected, because it is not expected that users will open prerat. -/ namespace prerat /- the equivalence relation -/ definition equiv (a b : prerat) : Prop := num a * denom b = num b * denom a infix ≡ := equiv theorem equiv.refl [refl] (a : prerat) : a ≡ a := rfl theorem equiv.symm [symm] {a b : prerat} (H : a ≡ b) : b ≡ a := !eq.symm H theorem num_eq_zero_of_equiv {a b : prerat} (H : a ≡ b) (na_zero : num a = 0) : num b = 0 := have num a * denom b = 0, from !zero_mul ▸ na_zero ▸ rfl, have num b * denom a = 0, from H ▸ this, show num b = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero this) (ne_of_gt (denom_pos a)) theorem num_pos_of_equiv {a b : prerat} (H : a ≡ b) (na_pos : num a > 0) : num b > 0 := have num a * denom b > 0, from mul_pos na_pos (denom_pos b), have num b * denom a > 0, from H ▸ this, show num b > 0, from pos_of_mul_pos_right this (le_of_lt (denom_pos a)) theorem num_neg_of_equiv {a b : prerat} (H : a ≡ b) (na_neg : num a < 0) : num b < 0 := assert H₁ : num a * denom b = num b * denom a, from H, assert num a * denom b < 0, from mul_neg_of_neg_of_pos na_neg (denom_pos b), have -(-num b * denom a) < 0, begin rewrite [neg_mul_eq_neg_mul, neg_neg, -H₁], exact this end, have -num b > 0, from pos_of_mul_pos_right (pos_of_neg_neg this) (le_of_lt (denom_pos a)), neg_of_neg_pos this theorem equiv_of_num_eq_zero {a b : prerat} (H1 : num a = 0) (H2 : num b = 0) : a ≡ b := by rewrite [↑equiv, H1, H2, *zero_mul] theorem equiv.trans [trans] {a b c : prerat} (H1 : a ≡ b) (H2 : b ≡ c) : a ≡ c := decidable.by_cases (suppose num b = 0, have num a = 0, from num_eq_zero_of_equiv (equiv.symm H1) `num b = 0`, have num c = 0, from num_eq_zero_of_equiv H2 `num b = 0`, equiv_of_num_eq_zero `num a = 0` `num c = 0`) (suppose num b ≠ 0, have H3 : num b * denom b ≠ 0, from mul_ne_zero this (ne_of_gt (denom_pos b)), have H4 : (num b * denom b) * (num a * denom c) = (num b * denom b) * (num c * denom a), from calc (num b * denom b) * (num a * denom c) = (num a * denom b) * (num b * denom c) : by rewrite [*mul.assoc, *mul.left_comm (num a), *mul.left_comm (num b)] ... = (num b * denom a) * (num b * denom c) : {H1} ... = (num b * denom a) * (num c * denom b) : {H2} ... = (num b * denom b) * (num c * denom a) : by rewrite [*mul.assoc, *mul.left_comm (denom a), *mul.left_comm (denom b), mul.comm (denom a)], eq_of_mul_eq_mul_left H3 H4) theorem equiv.is_equivalence : equivalence equiv := mk_equivalence equiv equiv.refl @equiv.symm @equiv.trans definition setoid : setoid prerat := setoid.mk equiv equiv.is_equivalence /- field operations -/ definition of_int (i : int) : prerat := prerat.mk i 1 !of_nat_succ_pos definition zero : prerat := of_int 0 definition one : prerat := of_int 1 private theorem mul_denom_pos (a b : prerat) : denom a * denom b > 0 := mul_pos (denom_pos a) (denom_pos b) definition add (a b : prerat) : prerat := prerat.mk (num a * denom b + num b * denom a) (denom a * denom b) (mul_denom_pos a b) definition mul (a b : prerat) : prerat := prerat.mk (num a * num b) (denom a * denom b) (mul_denom_pos a b) definition neg (a : prerat) : prerat := prerat.mk (- num a) (denom a) (denom_pos a) definition smul (a : ℤ) (b : prerat) (H : a > 0) : prerat := prerat.mk (a * num b) (a * denom b) (mul_pos H (denom_pos b)) theorem of_int_add (a b : ℤ) : of_int (a + b) ≡ add (of_int a) (of_int b) := by esimp [equiv, num, denom, one, add, of_int]; rewrite [*int.mul_one] theorem of_int_mul (a b : ℤ) : of_int (a * b) ≡ mul (of_int a) (of_int b) := !equiv.refl theorem of_int_neg (a : ℤ) : of_int (-a) ≡ neg (of_int a) := !equiv.refl theorem of_int.inj {a b : ℤ} : of_int a ≡ of_int b → a = b := by rewrite [↑of_int, ↑equiv, *mul_one]; intros; assumption definition inv : prerat → prerat | inv (prerat.mk nat.zero d dp) := zero | inv (prerat.mk (nat.succ n) d dp) := prerat.mk d (nat.succ n) !of_nat_succ_pos | inv (prerat.mk -[1+n] d dp) := prerat.mk (-d) (nat.succ n) !of_nat_succ_pos theorem equiv_zero_of_num_eq_zero {a : prerat} (H : num a = 0) : a ≡ zero := by rewrite [↑equiv, H, ↑zero, ↑num, ↑of_int, *zero_mul] theorem num_eq_zero_of_equiv_zero {a : prerat} : a ≡ zero → num a = 0 := by rewrite [↑equiv, ↑zero, ↑of_int, mul_one, zero_mul]; intro H; exact H theorem inv_zero {d : int} (dp : d > 0) : inv (mk nat.zero d dp) = zero := begin rewrite [↑inv, ▸*] end theorem inv_zero' : inv zero = zero := inv_zero (of_nat_succ_pos nat.zero) open nat theorem inv_of_pos {n d : int} (np : n > 0) (dp : d > 0) : inv (mk n d dp) ≡ mk d n np := obtain (n' : nat) (Hn' : n = of_nat n'), from exists_eq_of_nat (le_of_lt np), have (n' > nat.zero), from lt_of_of_nat_lt_of_nat (Hn' ▸ np), obtain (k : nat) (Hk : n' = nat.succ k), from nat.exists_eq_succ_of_lt this, have d * n = d * nat.succ k, by rewrite [Hn', Hk], Hn'⁻¹ ▸ (Hk⁻¹ ▸ this) theorem inv_neg {n d : int} (np : n > 0) (dp : d > 0) : inv (mk (-n) d dp) ≡ mk (-d) n np := obtain (n' : nat) (Hn' : n = of_nat n'), from exists_eq_of_nat (le_of_lt np), have (n' > nat.zero), from lt_of_of_nat_lt_of_nat (Hn' ▸ np), obtain (k : nat) (Hk : n' = nat.succ k), from nat.exists_eq_succ_of_lt this, have -d * n = -d * nat.succ k, by rewrite [Hn', Hk], have H3 : inv (mk -[1+k] d dp) ≡ mk (-d) n np, from this, have H4 : -[1+k] = -n, from calc -[1+k] = -(nat.succ k) : rfl ... = -n : by rewrite [Hk⁻¹, Hn'], H4 ▸ H3 theorem inv_of_neg {n d : int} (nn : n < 0) (dp : d > 0) : inv (mk n d dp) ≡ mk (-d) (-n) (neg_pos_of_neg nn) := have inv (mk (-(-n)) d dp) ≡ mk (-d) (-n) (neg_pos_of_neg nn), from inv_neg (neg_pos_of_neg nn) dp, !neg_neg ▸ this /- operations respect equiv -/ theorem add_equiv_add {a1 b1 a2 b2 : prerat} (eqv1 : a1 ≡ a2) (eqv2 : b1 ≡ b2) : add a1 b1 ≡ add a2 b2 := calc (num a1 * denom b1 + num b1 * denom a1) * (denom a2 * denom b2) = num a1 * denom a2 * denom b1 * denom b2 + num b1 * denom b2 * denom a1 * denom a2 : by rewrite [right_distrib, *mul.assoc, mul.left_comm (denom b1), mul.comm (denom b2), *mul.assoc] ... = num a2 * denom a1 * denom b1 * denom b2 + num b2 * denom b1 * denom a1 * denom a2 : by rewrite [↑equiv at *, eqv1, eqv2] ... = (num a2 * denom b2 + num b2 * denom a2) * (denom a1 * denom b1) : by rewrite [right_distrib, *mul.assoc, *mul.left_comm (denom b2), *mul.comm (denom b1), *mul.assoc, mul.left_comm (denom a2)] theorem mul_equiv_mul {a1 b1 a2 b2 : prerat} (eqv1 : a1 ≡ a2) (eqv2 : b1 ≡ b2) : mul a1 b1 ≡ mul a2 b2 := calc (num a1 * num b1) * (denom a2 * denom b2) = (num a1 * denom a2) * (num b1 * denom b2) : by rewrite [*mul.assoc, mul.left_comm (num b1)] ... = (num a2 * denom a1) * (num b2 * denom b1) : by rewrite [↑equiv at *, eqv1, eqv2] ... = (num a2 * num b2) * (denom a1 * denom b1) : by rewrite [*mul.assoc, mul.left_comm (num b2)] theorem neg_equiv_neg {a b : prerat} (eqv : a ≡ b) : neg a ≡ neg b := calc -num a * denom b = -(num a * denom b) : neg_mul_eq_neg_mul ... = -(num b * denom a) : {eqv} ... = -num b * denom a : neg_mul_eq_neg_mul theorem inv_equiv_inv : ∀{a b : prerat}, a ≡ b → inv a ≡ inv b | (mk an ad adp) (mk bn bd bdp) := assume H, lt.by_cases (assume an_neg : an < 0, have bn_neg : bn < 0, from num_neg_of_equiv H an_neg, calc inv (mk an ad adp) ≡ mk (-ad) (-an) (neg_pos_of_neg an_neg) : inv_of_neg an_neg adp ... ≡ mk (-bd) (-bn) (neg_pos_of_neg bn_neg) : by rewrite [↑equiv at *, ▸*, *neg_mul_neg, mul.comm ad, mul.comm bd, H] ... ≡ inv (mk bn bd bdp) : (inv_of_neg bn_neg bdp)⁻¹) (assume an_zero : an = 0, have bn_zero : bn = 0, from num_eq_zero_of_equiv H an_zero, eq.subst (calc inv (mk an ad adp) = inv (mk 0 ad adp) : {an_zero} ... = zero : inv_zero adp ... = inv (mk 0 bd bdp) : inv_zero bdp ... = inv (mk bn bd bdp) : bn_zero) !equiv.refl) (assume an_pos : an > 0, have bn_pos : bn > 0, from num_pos_of_equiv H an_pos, calc inv (mk an ad adp) ≡ mk ad an an_pos : inv_of_pos an_pos adp ... ≡ mk bd bn bn_pos : by rewrite [↑equiv at *, ▸*, mul.comm ad, mul.comm bd, H] ... ≡ inv (mk bn bd bdp) : (inv_of_pos bn_pos bdp)⁻¹) theorem smul_equiv {a : ℤ} {b : prerat} (H : a > 0) : smul a b H ≡ b := by esimp[equiv, smul]; rewrite[mul.assoc, mul.left_comm] /- properties -/ protected theorem add.comm (a b : prerat) : add a b ≡ add b a := by rewrite [↑add, ↑equiv, ▸*, add.comm, mul.comm (denom a)] protected theorem add.assoc (a b c : prerat) : add (add a b) c ≡ add a (add b c) := by rewrite [↑add, ↑equiv, ▸*, *(mul.comm (num c)), *(λy, mul.comm y (denom a)), *left_distrib, *right_distrib, *mul.assoc, *add.assoc] protected theorem add_zero (a : prerat) : add a zero ≡ a := by rewrite [↑add, ↑equiv, ↑zero, ↑of_int, ▸*, *mul_one, zero_mul, add_zero] protected theorem add_left_inv (a : prerat) : add (neg a) a ≡ zero := by rewrite [↑add, ↑equiv, ↑neg, ↑zero, ↑of_int, ▸*, -neg_mul_eq_neg_mul, add.left_inv, *zero_mul] protected theorem mul_comm (a b : prerat) : mul a b ≡ mul b a := by rewrite [↑mul, ↑equiv, mul.comm (num a), mul.comm (denom a)] protected theorem mul_assoc (a b c : prerat) : mul (mul a b) c ≡ mul a (mul b c) := by rewrite [↑mul, ↑equiv, *mul.assoc] protected theorem mul_one (a : prerat) : mul a one ≡ a := by rewrite [↑mul, ↑one, ↑of_int, ↑equiv, ▸*, *mul_one] protected theorem mul_left_distrib (a b c : prerat) : mul a (add b c) ≡ add (mul a b) (mul a c) := have H : smul (denom a) (mul a (add b c)) (denom_pos a) = add (mul a b) (mul a c), from begin rewrite[↑smul, ↑mul, ↑add], congruence, rewrite[*left_distrib, *right_distrib, -+(int.mul_assoc)], have T : ∀ {x y z w : ℤ}, x*y*z*w=y*z*x*w, from λx y z w, (!int.mul_assoc ⬝ !int.mul_comm) ▸ rfl, exact !congr_arg2 T T, rewrite [mul.left_comm (denom a) (denom b) (denom c)], rewrite int.mul_assoc end, equiv.symm (H ▸ smul_equiv (denom_pos a)) theorem mul_inv_cancel : ∀{a : prerat}, ¬ a ≡ zero → mul a (inv a) ≡ one | (mk an ad adp) := assume H, let a := mk an ad adp in lt.by_cases (assume an_neg : an < 0, let ia := mk (-ad) (-an) (neg_pos_of_neg an_neg) in calc mul a (inv a) ≡ mul a ia : mul_equiv_mul !equiv.refl (inv_of_neg an_neg adp) ... ≡ one : begin esimp [equiv, num, denom, one, mul, of_int], rewrite [*int.mul_one, *int.one_mul, mul.comm, neg_mul_comm] end) (assume an_zero : an = 0, absurd (equiv_zero_of_num_eq_zero an_zero) H) (assume an_pos : an > 0, let ia := mk ad an an_pos in calc mul a (inv a) ≡ mul a ia : mul_equiv_mul !equiv.refl (inv_of_pos an_pos adp) ... ≡ one : begin esimp [equiv, num, denom, one, mul, of_int], rewrite [*int.mul_one, *int.one_mul, mul.comm] end) theorem zero_not_equiv_one : ¬ zero ≡ one := begin esimp [equiv, zero, one, of_int], rewrite [zero_mul, int.mul_one], exact zero_ne_one end theorem mul_denom_equiv (a : prerat) : mul a (of_int (denom a)) ≡ of_int (num a) := by esimp [mul, of_int, equiv]; rewrite [*int.mul_one] /- Reducing a fraction to lowest terms. Needed to choose a canonical representative of rat, and define numerator and denominator. -/ definition reduce : prerat → prerat | (mk an ad adpos) := have pos : ad / gcd an ad > 0, from div_pos_of_pos_of_dvd adpos !gcd_nonneg !gcd_dvd_right, if an = 0 then prerat.zero else mk (an / gcd an ad) (ad / gcd an ad) pos protected theorem eq {a b : prerat} (Hn : num a = num b) (Hd : denom a = denom b) : a = b := begin cases a with [an, ad, adpos], cases b with [bn, bd, bdpos], generalize adpos, generalize bdpos, esimp at *, rewrite [Hn, Hd], intros, apply rfl end theorem reduce_equiv : ∀ a : prerat, reduce a ≡ a | (mk an ad adpos) := decidable.by_cases (assume anz : an = 0, begin rewrite [↑reduce, if_pos anz, ↑equiv, anz], krewrite zero_mul end) (assume annz : an ≠ 0, by rewrite [↑reduce, if_neg annz, ↑equiv, mul.comm, -!int.mul_div_assoc !gcd_dvd_left, -!int.mul_div_assoc !gcd_dvd_right, mul.comm]) theorem reduce_eq_reduce : ∀ {a b : prerat}, a ≡ b → reduce a = reduce b | (mk an ad adpos) (mk bn bd bdpos) := assume H : an * bd = bn * ad, decidable.by_cases (assume anz : an = 0, have H' : bn * ad = 0, by rewrite [-H, anz, zero_mul], assert bnz : bn = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H') (ne_of_gt adpos), by rewrite [↑reduce, if_pos anz, if_pos bnz]) (assume annz : an ≠ 0, assert bnnz : bn ≠ 0, from assume bnz, have H' : an * bd = 0, by rewrite [H, bnz, zero_mul], have anz : an = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H') (ne_of_gt bdpos), show false, from annz anz, begin rewrite [↑reduce, if_neg annz, if_neg bnnz], apply prerat.eq, {apply div_gcd_eq_div_gcd H adpos bdpos}, {esimp, rewrite [gcd.comm, gcd.comm bn], apply div_gcd_eq_div_gcd_of_nonneg, rewrite [mul.comm, -H, mul.comm], apply annz, apply bnnz, apply le_of_lt adpos, apply le_of_lt bdpos}, end) end prerat /- the rationals -/ definition rat : Type.{1} := quot prerat.setoid notation `ℚ` := rat local attribute prerat.setoid [instance] namespace rat /- operations -/ definition of_int [coercion] (i : ℤ) : ℚ := ⟦prerat.of_int i⟧ definition of_nat [coercion] (n : ℕ) : ℚ := n definition of_num [coercion] [reducible] (n : num) : ℚ := n protected definition prio := num.pred int.prio definition rat_has_zero [reducible] [instance] [priority rat.prio] : has_zero rat := has_zero.mk (of_int 0) definition rat_has_one [reducible] [instance] [priority rat.prio] : has_one rat := has_one.mk (of_int 1) theorem of_int_zero : of_int (0:int) = (0:rat) := rfl theorem of_int_one : of_int (1:int) = (1:rat) := rfl protected definition add : ℚ → ℚ → ℚ := quot.lift₂ (λ a b : prerat, ⟦prerat.add a b⟧) (take a1 a2 b1 b2, assume H1 H2, quot.sound (prerat.add_equiv_add H1 H2)) protected definition mul : ℚ → ℚ → ℚ := quot.lift₂ (λ a b : prerat, ⟦prerat.mul a b⟧) (take a1 a2 b1 b2, assume H1 H2, quot.sound (prerat.mul_equiv_mul H1 H2)) protected definition neg : ℚ → ℚ := quot.lift (λ a : prerat, ⟦prerat.neg a⟧) (take a1 a2, assume H, quot.sound (prerat.neg_equiv_neg H)) protected definition inv : ℚ → ℚ := quot.lift (λ a : prerat, ⟦prerat.inv a⟧) (take a1 a2, assume H, quot.sound (prerat.inv_equiv_inv H)) definition reduce : ℚ → prerat := quot.lift (λ a : prerat, prerat.reduce a) @prerat.reduce_eq_reduce definition num (a : ℚ) : ℤ := prerat.num (reduce a) definition denom (a : ℚ) : ℤ := prerat.denom (reduce a) theorem denom_pos (a : ℚ): denom a > 0 := prerat.denom_pos (reduce a) definition rat_has_add [reducible] [instance] [priority rat.prio] : has_add rat := has_add.mk rat.add definition rat_has_mul [reducible] [instance] [priority rat.prio] : has_mul rat := has_mul.mk rat.mul definition rat_has_neg [reducible] [instance] [priority rat.prio] : has_neg rat := has_neg.mk rat.neg definition rat_has_inv [reducible] [instance] [priority rat.prio] : has_inv rat := has_inv.mk rat.inv protected definition sub [reducible] (a b : ℚ) : rat := a + (-b) definition rat_has_sub [reducible] [instance] [priority rat.prio] : has_sub rat := has_sub.mk rat.sub lemma sub.def (a b : ℚ) : a - b = a + (-b) := rfl /- properties -/ theorem of_int_add (a b : ℤ) : of_int (a + b) = of_int a + of_int b := quot.sound (prerat.of_int_add a b) theorem of_int_mul (a b : ℤ) : of_int (a * b) = of_int a * of_int b := quot.sound (prerat.of_int_mul a b) theorem of_int_neg (a : ℤ) : of_int (-a) = -(of_int a) := quot.sound (prerat.of_int_neg a) theorem of_int_sub (a b : ℤ) : of_int (a - b) = of_int a - of_int b := calc of_int (a - b) = of_int a + of_int (-b) : of_int_add ... = of_int a - of_int b : {of_int_neg b} theorem of_int.inj {a b : ℤ} (H : of_int a = of_int b) : a = b := prerat.of_int.inj (quot.exact H) theorem eq_of_of_int_eq_of_int {a b : ℤ} (H : of_int a = of_int b) : a = b := of_int.inj H theorem of_int_eq_of_int_iff (a b : ℤ) : of_int a = of_int b ↔ a = b := iff.intro eq_of_of_int_eq_of_int !congr_arg theorem of_nat_eq (a : ℕ) : of_nat a = of_int (int.of_nat a) := rfl open nat theorem of_nat_add (a b : ℕ) : of_nat (a + b) = of_nat a + of_nat b := by rewrite [of_nat_eq, int.of_nat_add, rat.of_int_add] theorem of_nat_mul (a b : ℕ) : of_nat (a * b) = of_nat a * of_nat b := by rewrite [of_nat_eq, int.of_nat_mul, rat.of_int_mul] theorem of_nat_sub {a b : ℕ} (H : a ≥ b) : of_nat (a - b) = of_nat a - of_nat b := begin rewrite of_nat_eq, rewrite [int.of_nat_sub H], rewrite [rat.of_int_sub] end theorem of_nat.inj {a b : ℕ} (H : of_nat a = of_nat b) : a = b := int.of_nat.inj (of_int.inj H) theorem eq_of_of_nat_eq_of_nat {a b : ℕ} (H : of_nat a = of_nat b) : a = b := of_nat.inj H theorem of_nat_eq_of_nat_iff (a b : ℕ) : of_nat a = of_nat b ↔ a = b := iff.intro of_nat.inj !congr_arg protected theorem add_comm (a b : ℚ) : a + b = b + a := quot.induction_on₂ a b (take u v, quot.sound !prerat.add.comm) protected theorem add_assoc (a b c : ℚ) : a + b + c = a + (b + c) := quot.induction_on₃ a b c (take u v w, quot.sound !prerat.add.assoc) protected theorem add_zero (a : ℚ) : a + 0 = a := quot.induction_on a (take u, quot.sound !prerat.add_zero) protected theorem zero_add (a : ℚ) : 0 + a = a := !rat.add_comm ▸ !rat.add_zero protected theorem add_left_inv (a : ℚ) : -a + a = 0 := quot.induction_on a (take u, quot.sound !prerat.add_left_inv) protected theorem mul_comm (a b : ℚ) : a * b = b * a := quot.induction_on₂ a b (take u v, quot.sound !prerat.mul_comm) protected theorem mul_assoc (a b c : ℚ) : a * b * c = a * (b * c) := quot.induction_on₃ a b c (take u v w, quot.sound !prerat.mul_assoc) protected theorem mul_one (a : ℚ) : a * 1 = a := quot.induction_on a (take u, quot.sound !prerat.mul_one) protected theorem one_mul (a : ℚ) : 1 * a = a := !rat.mul_comm ▸ !rat.mul_one protected theorem left_distrib (a b c : ℚ) : a * (b + c) = a * b + a * c := quot.induction_on₃ a b c (take u v w, quot.sound !prerat.mul_left_distrib) protected theorem right_distrib (a b c : ℚ) : (a + b) * c = a * c + b * c := by rewrite [rat.mul_comm, rat.left_distrib, *rat.mul_comm c] protected theorem mul_inv_cancel {a : ℚ} : a ≠ 0 → a * a⁻¹ = 1 := quot.induction_on a (take u, assume H, quot.sound (!prerat.mul_inv_cancel (assume H1, H (quot.sound H1)))) protected theorem inv_mul_cancel {a : ℚ} (H : a ≠ 0) : a⁻¹ * a = 1 := !rat.mul_comm ▸ rat.mul_inv_cancel H protected theorem zero_ne_one : (0 : ℚ) ≠ 1 := assume H, prerat.zero_not_equiv_one (quot.exact H) definition has_decidable_eq [instance] : decidable_eq ℚ := take a b, quot.rec_on_subsingleton₂ a b (take u v, if H : prerat.num u * prerat.denom v = prerat.num v * prerat.denom u then decidable.inl (quot.sound H) else decidable.inr (assume H1, H (quot.exact H1))) protected theorem inv_zero : inv 0 = (0 : ℚ) := quot.sound (prerat.inv_zero' ▸ !prerat.equiv.refl) theorem quot_reduce (a : ℚ) : ⟦reduce a⟧ = a := quot.induction_on a (take u, quot.sound !prerat.reduce_equiv) section local attribute rat [reducible] theorem mul_denom (a : ℚ) : a * denom a = num a := have H : ⟦reduce a⟧ * of_int (denom a) = of_int (num a), from quot.sound (!prerat.mul_denom_equiv), quot_reduce a ▸ H end theorem coprime_num_denom (a : ℚ) : coprime (num a) (denom a) := decidable.by_cases (suppose a = 0, by substvars) (quot.induction_on a (take u H, assert H' : prerat.num u ≠ 0, from take H'', H (quot.sound (prerat.equiv_zero_of_num_eq_zero H'')), begin cases u with un ud udpos, rewrite [▸*, ↑num, ↑denom, ↑reduce, ↑prerat.reduce, if_neg H', ▸*], have gcd un ud ≠ 0, from ne_of_gt (!gcd_pos_of_ne_zero_left H'), apply coprime_div_gcd_div_gcd this end)) protected definition discrete_field [reducible] [trans_instance] : discrete_field rat := ⦃discrete_field, add := rat.add, add_assoc := rat.add_assoc, zero := 0, zero_add := rat.zero_add, add_zero := rat.add_zero, neg := rat.neg, add_left_inv := rat.add_left_inv, add_comm := rat.add_comm, mul := rat.mul, mul_assoc := rat.mul_assoc, one := 1, one_mul := rat.one_mul, mul_one := rat.mul_one, left_distrib := rat.left_distrib, right_distrib := rat.right_distrib, mul_comm := rat.mul_comm, mul_inv_cancel := @rat.mul_inv_cancel, inv_mul_cancel := @rat.inv_mul_cancel, zero_ne_one := rat.zero_ne_one, inv_zero := rat.inv_zero, has_decidable_eq := has_decidable_eq⦄ definition rat_has_div [instance] [reducible] [priority rat.prio] : has_div rat := has_div.mk has_div.div definition rat_has_pow_nat [instance] [reducible] [priority rat.prio] : has_pow_nat rat := has_pow_nat.mk has_pow_nat.pow_nat theorem eq_num_div_denom (a : ℚ) : a = num a / denom a := have H : of_int (denom a) ≠ 0, from assume H', ne_of_gt (denom_pos a) (of_int.inj H'), iff.mpr (!eq_div_iff_mul_eq H) (mul_denom a) theorem of_int_div {a b : ℤ} (H : b ∣ a) : of_int (a / b) = of_int a / of_int b := decidable.by_cases (assume bz : b = 0, by rewrite [bz, int.div_zero, of_int_zero, div_zero]) (assume bnz : b ≠ 0, have bnz' : of_int b ≠ 0, from assume oibz, bnz (of_int.inj oibz), have H' : of_int (a / b) * of_int b = of_int a, from dvd.elim H (take c, assume Hc : a = b * c, by rewrite [Hc, !int.mul_div_cancel_left bnz, mul.comm]), iff.mpr (!eq_div_iff_mul_eq bnz') H') theorem of_nat_div {a b : ℕ} (H : b ∣ a) : of_nat (a / b) = of_nat a / of_nat b := have H' : (int.of_nat b ∣ int.of_nat a), by rewrite [int.of_nat_dvd_of_nat_iff]; exact H, by+ rewrite [of_nat_eq, int.of_nat_div, of_int_div H'] theorem of_int_pow (a : ℤ) (n : ℕ) : of_int (a^n) = (of_int a)^n := begin induction n with n ih, apply eq.refl, rewrite [pow_succ, pow_succ, of_int_mul, ih] end theorem of_nat_pow (a : ℕ) (n : ℕ) : of_nat (a^n) = (of_nat a)^n := by rewrite [of_nat_eq, int.of_nat_pow, of_int_pow] end rat
66ec995bffabc50c837fac216f4203c3074600e9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/Tactic/Induction.lean
d8bb03cf24585cd625e286e94e7a1c5834e07f06
[ "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
12,096
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.Meta.RecursorInfo import Lean.Meta.SynthInstance import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Revert import Lean.Meta.Tactic.Intro import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.FVarSubst namespace Lean.Meta private partial def getTargetArity : Expr → Nat | Expr.mdata _ b => getTargetArity b | Expr.forallE _ _ b _ => getTargetArity b + 1 | e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0 private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) → Expr → MetaM Expr | [], recursor => pure recursor | some pos :: rest, recursor => if h : pos < majorTypeArgs.size then addRecParams mvarId majorTypeArgs rest (mkApp recursor (majorTypeArgs.get ⟨pos, h⟩)) else throwTacticEx `induction mvarId "ill-formed recursor" | none :: rest, recursor => do let recursorType ← inferType recursor let recursorType ← whnfForall recursorType match recursorType with | Expr.forallE _ d _ _ => do let param ← try synthInstance d catch _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter" addRecParams mvarId majorTypeArgs rest (mkApp recursor param) | _ => throwTacticEx `induction mvarId "ill-formed recursor" structure InductionSubgoal where mvarId : MVarId fields : Array Expr := #[] subst : FVarSubst := {} deriving Inhabited private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do let type ← whnfForall type match type with | Expr.forallE _ _ b _ => pure $ b.instantiate1 x | _ => throwTacticEx `induction mvarId "ill-formed recursor" structure AltVarNames where explicit : Bool := false -- true if `@` modifier was used varNames : List Name := [] deriving Inhabited private partial def finalize (mvarId : MVarId) (givenNames : Array AltVarNames) (recursorInfo : RecursorInfo) (reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (recursor : Expr) : MetaM (Array InductionSubgoal) := do let target ← mvarId.getType let initialArity := getTargetArity target let recursorType ← inferType recursor let numMinors := recursorInfo.produceMotive.length let rec loop (pos : Nat) (minorIdx : Nat) (recursor recursorType : Expr) (consumedMajor : Bool) (subgoals : Array InductionSubgoal) := do let recursorType ← whnfForall recursorType if recursorType.isForall && pos < recursorInfo.numArgs then if pos == recursorInfo.firstIndexPos then let (recursor, recursorType) ← indices.foldlM (init := (recursor, recursorType)) fun (recursor, recursorType) index => do let recursor := mkApp recursor index let recursorType ← getTypeBody mvarId recursorType index pure (recursor, recursorType) let recursor := mkApp recursor major let recursorType ← getTypeBody mvarId recursorType major loop (pos+1+indices.size) minorIdx recursor recursorType true subgoals else -- consume motive let tag ← mvarId.getTag if minorIdx ≥ numMinors then throwTacticEx `induction mvarId "ill-formed recursor" match recursorType with | Expr.forallE n d _ c => let d := d.headBeta -- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise if c.isInstImplicit && givenNames.isEmpty then match (← synthInstance? d) with | some inst => let recursor := mkApp recursor inst let recursorType ← getTypeBody mvarId recursorType inst loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor subgoals | none => do -- Add newSubgoal if type class resolution failed let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n) let recursor := mkApp recursor mvar let recursorType ← getTypeBody mvarId recursorType mvar loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvar.mvarId! }) else let arity := getTargetArity d if arity < initialArity then throwTacticEx `induction mvarId "ill-formed recursor" let nparams := arity - initialArity -- number of fields due to minor premise let nextra := reverted.size - indices.size - 1 -- extra dependencies that have been reverted let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get ⟨minorIdx, h⟩ else {} let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n) let recursor := mkApp recursor mvar let recursorType ← getTypeBody mvarId recursorType mvar -- Try to clear major premise from new goal let mvarId' ← mvar.mvarId!.tryClear major.fvarId! let (fields, mvarId') ← mvarId'.introN nparams minorGivenNames.varNames (useNamesForExplicitOnly := !minorGivenNames.explicit) let (extra, mvarId') ← mvarId'.introNP nextra let subst := reverted.size.fold (init := baseSubst) fun i (subst : FVarSubst) => if i < indices.size + 1 then subst else let revertedFVarId := reverted[i]! let newFVarId := extra[i - indices.size - 1]! subst.insert revertedFVarId (mkFVar newFVarId) let fields := fields.map mkFVar loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst }) | _ => unreachable! else unless consumedMajor do throwTacticEx `induction mvarId "ill-formed recursor" mvarId.assign recursor pure subgoals loop (recursorInfo.paramsPos.length + 1) 0 recursor recursorType false #[] private def throwUnexpectedMajorType {α} (mvarId : MVarId) (majorType : Expr) : MetaM α := throwTacticEx `induction mvarId m!"unexpected major premise type{indentExpr majorType}" def _root_.Lean.MVarId.induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array AltVarNames := #[]) : MetaM (Array InductionSubgoal) := mvarId.withContext do trace[Meta.Tactic.induction] "initial\n{MessageData.ofGoal mvarId}" mvarId.checkNotAssigned `induction let majorLocalDecl ← majorFVarId.getDecl let recursorInfo ← mkRecursorInfo recursorName let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type majorType.withApp fun _ majorTypeArgs => do recursorInfo.paramsPos.forM fun paramPos? => do match paramPos? with | none => pure () | some paramPos => if paramPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}" let indices ← recursorInfo.indicesPos.toArray.mapM fun idxPos => do if idxPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}" let idx := majorTypeArgs.get! idxPos unless idx.isFVar do throwTacticEx `induction mvarId m!"major premise type index {idx} is not a variable{indentExpr majorType}" majorTypeArgs.size.forM fun i => do let arg := majorTypeArgs[i]! if i != idxPos && arg == idx then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs more than once{indentExpr majorType}" if i < idxPos then if (← exprDependsOn arg idx.fvarId!) then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs in previous arguments{indentExpr majorType}" -- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`. -- Note that if `arg` is not a variable, we will fail anyway when we visit it. if i > idxPos && recursorInfo.indicesPos.contains i && arg.isFVar then let idxDecl ← idx.fvarId!.getDecl if (← localDeclDependsOn idxDecl arg.fvarId!) then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it depends on index occurring at position #{i+1}" pure idx let target ← mvarId.getType if (← pure !recursorInfo.depElim <&&> exprDependsOn target majorFVarId) then throwTacticEx `induction mvarId m!"recursor '{recursorName}' does not support dependent elimination, but conclusion depends on major premise" -- Revert indices and major premise preserving variable order let (reverted, mvarId) ← mvarId.revert ((indices.map Expr.fvarId!).push majorFVarId) true -- Re-introduce indices and major let (indices', mvarId) ← mvarId.introNP indices.size let (majorFVarId', mvarId) ← mvarId.intro1P -- Create FVarSubst with indices let baseSubst := Id.run do let mut subst : FVarSubst := {} let mut i := 0 for index in indices do subst := subst.insert index.fvarId! (mkFVar indices'[i]!) i := i + 1 pure subst trace[Meta.Tactic.induction] "after revert&intro\n{MessageData.ofGoal mvarId}" -- Update indices and major let indices := indices'.map mkFVar let majorFVarId := majorFVarId' let major := mkFVar majorFVarId mvarId.withContext do let target ← mvarId.getType let targetLevel ← getLevel target let targetLevel ← normalizeLevel targetLevel let majorLocalDecl ← majorFVarId.getDecl let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type majorType.withApp fun majorTypeFn majorTypeArgs => do match majorTypeFn with | Expr.const _ majorTypeFnLevels => do let majorTypeFnLevels := majorTypeFnLevels.toArray let (recursorLevels, foundTargetLevel) ← recursorInfo.univLevelPos.foldlM (init := (#[], false)) fun (recursorLevels, foundTargetLevel) (univPos : RecursorUnivLevelPos) => do match univPos with | RecursorUnivLevelPos.motive => pure (recursorLevels.push targetLevel, true) | RecursorUnivLevelPos.majorType idx => if idx ≥ majorTypeFnLevels.size then throwTacticEx `induction mvarId "ill-formed recursor" pure (recursorLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel) if !foundTargetLevel && !targetLevel.isZero then throwTacticEx `induction mvarId m!"recursor '{recursorName}' can only eliminate into Prop" let recursor := mkConst recursorName recursorLevels.toList let recursor ← addRecParams mvarId majorTypeArgs recursorInfo.paramsPos recursor -- Compute motive let motive := target let motive ← if recursorInfo.depElim then pure <| mkLambda `x BinderInfo.default (← inferType major) (← motive.abstractM #[major]) else pure motive let motive ← mkLambdaFVars indices motive let recursor := mkApp recursor motive finalize mvarId givenNames recursorInfo reverted major indices baseSubst recursor | _ => throwTacticEx `induction mvarId "major premise is not of the form (C ...)" @[deprecated MVarId.induction] def induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array AltVarNames := #[]) : MetaM (Array InductionSubgoal) := mvarId.induction majorFVarId recursorName givenNames builtin_initialize registerTraceClass `Meta.Tactic.induction end Lean.Meta
50b788b4a7df38caf6a223316f1062bc7d9d98ab
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/algebra/monoid_algebra/to_direct_sum.lean
9b5b45c7fc22083466d4201afca9fb22f886a394
[ "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
7,853
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.direct_sum.algebra import algebra.monoid_algebra.basic import data.finsupp.to_dfinsupp /-! # Conversion between `add_monoid_algebra` and homogenous `direct_sum` This module provides conversions between `add_monoid_algebra` and `direct_sum`. The latter is essentially a dependent version of the former. Note that since `direct_sum.has_mul` combines indices additively, there is no equivalent to `monoid_algebra`. ## Main definitions * `add_monoid_algebra.to_direct_sum : add_monoid_algebra M ι → (⨁ i : ι, M)` * `direct_sum.to_add_monoid_algebra : (⨁ i : ι, M) → add_monoid_algebra M ι` * Bundled equiv versions of the above: * `add_monoid_algebra_equiv_direct_sum : add_monoid_algebra M ι ≃ (⨁ i : ι, M)` * `add_monoid_algebra_add_equiv_direct_sum : add_monoid_algebra M ι ≃+ (⨁ i : ι, M)` * `add_monoid_algebra_ring_equiv_direct_sum R : add_monoid_algebra M ι ≃+* (⨁ i : ι, M)` * `add_monoid_algebra_alg_equiv_direct_sum R : add_monoid_algebra A ι ≃ₐ[R] (⨁ i : ι, A)` ## Theorems The defining feature of these operations is that they map `finsupp.single` to `direct_sum.of` and vice versa: * `add_monoid_algebra.to_direct_sum_single` * `direct_sum.to_add_monoid_algebra_of` as well as preserving arithmetic operations. For the bundled equivalences, we provide lemmas that they reduce to `add_monoid_algebra.to_direct_sum`: * `add_monoid_algebra_add_equiv_direct_sum_apply` * `add_monoid_algebra_lequiv_direct_sum_apply` * `add_monoid_algebra_add_equiv_direct_sum_symm_apply` * `add_monoid_algebra_lequiv_direct_sum_symm_apply` ## Implementation notes This file largely just copies the API of `data/finsupp/to_dfinsupp`, and reuses the proofs. Recall that `add_monoid_algebra M ι` is defeq to `ι →₀ M` and `⨁ i : ι, M` is defeq to `Π₀ i : ι, M`. Note that there is no `add_monoid_algebra` equivalent to `finsupp.single`, so many statements still involve this definition. -/ variables {ι : Type*} {R : Type*} {M : Type*} {A : Type*} open_locale direct_sum /-! ### Basic definitions and lemmas -/ section defs /-- Interpret a `add_monoid_algebra` as a homogenous `direct_sum`. -/ def add_monoid_algebra.to_direct_sum [semiring M] (f : add_monoid_algebra M ι) : ⨁ i : ι, M := finsupp.to_dfinsupp f section variables [decidable_eq ι] [semiring M] @[simp] lemma add_monoid_algebra.to_direct_sum_single (i : ι) (m : M) : add_monoid_algebra.to_direct_sum (finsupp.single i m) = direct_sum.of _ i m := finsupp.to_dfinsupp_single i m variables [Π m : M, decidable (m ≠ 0)] /-- Interpret a homogenous `direct_sum` as a `add_monoid_algebra`. -/ def direct_sum.to_add_monoid_algebra (f : ⨁ i : ι, M) : add_monoid_algebra M ι := dfinsupp.to_finsupp f @[simp] lemma direct_sum.to_add_monoid_algebra_of (i : ι) (m : M) : (direct_sum.of _ i m : ⨁ i : ι, M).to_add_monoid_algebra = finsupp.single i m := dfinsupp.to_finsupp_single i m @[simp] lemma add_monoid_algebra.to_direct_sum_to_add_monoid_algebra (f : add_monoid_algebra M ι) : f.to_direct_sum.to_add_monoid_algebra = f := finsupp.to_dfinsupp_to_finsupp f @[simp] lemma direct_sum.to_add_monoid_algebra_to_direct_sum (f : ⨁ i : ι, M) : f.to_add_monoid_algebra.to_direct_sum = f := dfinsupp.to_finsupp_to_dfinsupp f end end defs /-! ### Lemmas about arithmetic operations -/ section lemmas namespace add_monoid_algebra @[simp] lemma to_direct_sum_zero [semiring M] : (0 : add_monoid_algebra M ι).to_direct_sum = 0 := finsupp.to_dfinsupp_zero @[simp] lemma to_direct_sum_add [semiring M] (f g : add_monoid_algebra M ι) : (f + g).to_direct_sum = f.to_direct_sum + g.to_direct_sum := finsupp.to_dfinsupp_add _ _ @[simp] lemma to_direct_sum_mul [decidable_eq ι] [add_monoid ι] [semiring M] (f g : add_monoid_algebra M ι) : (f * g).to_direct_sum = f.to_direct_sum * g.to_direct_sum := begin let to_hom : add_monoid_algebra M ι →+ (⨁ i : ι, M) := ⟨to_direct_sum, to_direct_sum_zero, to_direct_sum_add⟩, have : ⇑to_hom = to_direct_sum := rfl, rw ←this, revert f g, rw add_monoid_hom.map_mul_iff, ext xi xv yi yv : 4, dsimp only [add_monoid_hom.comp_apply, add_monoid_hom.compl₂_apply, add_monoid_hom.compr₂_apply, add_monoid_hom.mul_apply, add_equiv.coe_to_add_monoid_hom, finsupp.single_add_hom_apply], simp only [add_monoid_algebra.single_mul_single, this, add_monoid_algebra.to_direct_sum_single], rw [direct_sum.of_mul_of, semiring.direct_sum_mul], end end add_monoid_algebra namespace direct_sum variables [decidable_eq ι] @[simp] lemma to_add_monoid_algebra_zero [semiring M] [Π m : M, decidable (m ≠ 0)] : to_add_monoid_algebra 0 = (0 : add_monoid_algebra M ι) := dfinsupp.to_finsupp_zero @[simp] lemma to_add_monoid_algebra_add [semiring M] [Π m : M, decidable (m ≠ 0)] (f g : ⨁ i : ι, M) : (f + g).to_add_monoid_algebra = to_add_monoid_algebra f + to_add_monoid_algebra g := dfinsupp.to_finsupp_add _ _ @[simp] lemma to_add_monoid_algebra_mul [add_monoid ι] [semiring M] [Π m : M, decidable (m ≠ 0)] (f g : ⨁ i : ι, M) : (f * g).to_add_monoid_algebra = to_add_monoid_algebra f * to_add_monoid_algebra g := begin apply_fun add_monoid_algebra.to_direct_sum, { simp }, { apply function.left_inverse.injective, apply add_monoid_algebra.to_direct_sum_to_add_monoid_algebra } end end direct_sum end lemmas /-! ### Bundled `equiv`s -/ section equivs /-- `add_monoid_algebra.to_direct_sum` and `direct_sum.to_add_monoid_algebra` together form an equiv. -/ @[simps {fully_applied := ff}] def add_monoid_algebra_equiv_direct_sum [decidable_eq ι] [semiring M] [Π m : M, decidable (m ≠ 0)] : add_monoid_algebra M ι ≃ (⨁ i : ι, M) := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, ..finsupp_equiv_dfinsupp } /-- The additive version of `add_monoid_algebra.to_add_monoid_algebra`. Note that this is `noncomputable` because `add_monoid_algebra.has_add` is noncomputable. -/ @[simps {fully_applied := ff}] noncomputable def add_monoid_algebra_add_equiv_direct_sum [decidable_eq ι] [semiring M] [Π m : M, decidable (m ≠ 0)] : add_monoid_algebra M ι ≃+ (⨁ i : ι, M) := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, map_add' := add_monoid_algebra.to_direct_sum_add, .. add_monoid_algebra_equiv_direct_sum} /-- The ring version of `add_monoid_algebra.to_add_monoid_algebra`. Note that this is `noncomputable` because `add_monoid_algebra.has_add` is noncomputable. -/ @[simps {fully_applied := ff}] noncomputable def add_monoid_algebra_ring_equiv_direct_sum [decidable_eq ι] [add_monoid ι] [semiring M] [Π m : M, decidable (m ≠ 0)] : add_monoid_algebra M ι ≃+* ⨁ i : ι, M := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, map_mul' := add_monoid_algebra.to_direct_sum_mul, ..(add_monoid_algebra_add_equiv_direct_sum : add_monoid_algebra M ι ≃+ ⨁ i : ι, M) } /-- The algebra version of `add_monoid_algebra.to_add_monoid_algebra`. Note that this is `noncomputable` because `add_monoid_algebra.has_add` is noncomputable. -/ @[simps {fully_applied := ff}] noncomputable def add_monoid_algebra_alg_equiv_direct_sum [decidable_eq ι] [add_monoid ι] [comm_semiring R] [semiring A] [algebra R A] [Π m : A, decidable (m ≠ 0)] : add_monoid_algebra A ι ≃ₐ[R] ⨁ i : ι, A := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, commutes' := λ r, add_monoid_algebra.to_direct_sum_single _ _, ..(add_monoid_algebra_ring_equiv_direct_sum : add_monoid_algebra A ι ≃+* ⨁ i : ι, A) } end equivs
a2b52f9202ef95e93afe0fa81d35bee81e8c5882
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/combinatorics/quiver.lean
a25e12afb81643ade8de9a90761dcca2f9c8134d
[ "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,865
lean
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import data.equiv.basic import order.well_founded import data.nat.basic import data.opposite /-! # Quivers This module defines quivers. A quiver on a type `V` of vertices assigns to every pair `a b : V` of vertices a type `a ⟶ b` of arrows from `a` to `b`. This is a very permissive notion of directed graph. ## Implementation notes Currently `quiver` is defined with `arrow : V → V → Sort v`. This is different from the category theory setup, where we insist that morphisms live in some `Type`. There's some balance here: it's nice to allow `Prop` to ensure there are no multiple arrows, but it is also results in error-prone universe signatures when constraints require a `Type`. -/ open opposite -- We use the same universe order as in category theory. -- See note [category_theory universes] universes v v₁ v₂ u u₁ u₂ /-- A quiver `G` on a type `V` of vertices assigns to every pair `a b : V` of vertices a type `a ⟶ b` of arrows from `a` to `b`. For graphs with no repeated edges, one can use `quiver.{0} V`, which ensures `a ⟶ b : Prop`. For multigraphs, one can use `quiver.{v+1} V`, which ensures `a ⟶ b : Type v`. Because `category` will later extend this class, we call the field `hom`. Except when constructing instances, you should rarely see this, and use the `⟶` notation instead. -/ class quiver (V : Type u) := (hom : V → V → Sort v) infixr ` ⟶ `:10 := quiver.hom -- type as \h /-- A morphism of quivers. As we will later have categorical functors extend this structure, we call it a `prefunctor`. -/ structure prefunctor (V : Type u₁) [quiver.{v₁} V] (W : Type u₂) [quiver.{v₂} W] := (obj [] : V → W) (map : Π {X Y : V}, (X ⟶ Y) → (obj X ⟶ obj Y)) namespace prefunctor /-- The identity morphism between quivers. -/ @[simps] def id (V : Type*) [quiver V] : prefunctor V V := { obj := id, map := λ X Y f, f, } instance (V : Type*) [quiver V] : inhabited (prefunctor V V) := ⟨id V⟩ /-- Composition of morphisms between quivers. -/ @[simps] def comp {U : Type*} [quiver U] {V : Type*} [quiver V] {W : Type*} [quiver W] (F : prefunctor U V) (G : prefunctor V W) : prefunctor U W := { obj := λ X, G.obj (F.obj X), map := λ X Y f, G.map (F.map f), } end prefunctor /-- A wide subquiver `H` of `G` picks out a set `H a b` of arrows from `a` to `b` for every pair of vertices `a b`. NB: this does not work for `Prop`-valued quivers. It requires `G : quiver.{v+1} V`. -/ def wide_subquiver (V) [quiver.{v+1} V] := Π a b : V, set (a ⟶ b) /-- A type synonym for `V`, when thought of as a quiver having only the arrows from some `wide_subquiver`. -/ @[nolint unused_arguments has_inhabited_instance] def wide_subquiver.to_Type (V) [quiver V] (H : wide_subquiver V) : Type u := V instance wide_subquiver_has_coe_to_sort {V} [quiver V] : has_coe_to_sort (wide_subquiver V) := { S := Type u, coe := λ H, wide_subquiver.to_Type V H, } /-- A wide subquiver viewed as a quiver on its own. -/ instance wide_subquiver.quiver {V} [quiver V] (H : wide_subquiver V) : quiver H := ⟨λ a b, H a b⟩ namespace quiver /-- A type synonym for a quiver with no arrows. -/ @[nolint has_inhabited_instance] def empty (V) : Type u := V instance empty_quiver (V : Type u) : quiver.{u} (empty V) := ⟨λ a b, pempty⟩ @[simp] lemma empty_arrow {V : Type u} (a b : empty V) : (a ⟶ b) = pempty := rfl instance {V} [quiver V] : has_bot (wide_subquiver V) := ⟨λ a b, ∅⟩ instance {V} [quiver V] : has_top (wide_subquiver V) := ⟨λ a b, set.univ⟩ instance {V} [quiver V] : inhabited (wide_subquiver V) := ⟨⊤⟩ /-- `Vᵒᵖ` reverses the direction of all arrows of `V`. -/ instance opposite {V} [quiver V] : quiver Vᵒᵖ := ⟨λ a b, (unop b) ⟶ (unop a)⟩ /-- The opposite of an arrow in `V`. -/ def hom.op {V} [quiver V] {X Y : V} (f : X ⟶ Y) : op Y ⟶ op X := f /-- Given an arrow in `Vᵒᵖ`, we can take the "unopposite" back in `V`. -/ def hom.unop {V} [quiver V] {X Y : Vᵒᵖ} (f : X ⟶ Y) : unop Y ⟶ unop X := f attribute [irreducible] quiver.opposite /-- A type synonym for the symmetrized quiver (with an arrow both ways for each original arrow). NB: this does not work for `Prop`-valued quivers. It requires `[quiver.{v+1} V]`. -/ @[nolint has_inhabited_instance] def symmetrify (V) : Type u := V instance symmetrify_quiver (V : Type u) [quiver V] : quiver (symmetrify V) := ⟨λ a b : V, (a ⟶ b) ⊕ (b ⟶ a)⟩ /-- `total V` is the type of _all_ arrows of `V`. -/ -- TODO Unify with `category_theory.arrow`? (The fields have been named to match.) @[ext, nolint has_inhabited_instance] structure total (V : Type u) [quiver.{v} V] : Sort (max (u+1) v) := (left : V) (right : V) (hom : left ⟶ right) /-- A wide subquiver `H` of `G.symmetrify` determines a wide subquiver of `G`, containing an an arrow `e` if either `e` or its reversal is in `H`. -/ -- Without the explicit universe level in `quiver.{v+1}` Lean comes up with -- `quiver.{max u_2 u_3 + 1}`. This causes problems elsewhere, so we write `quiver.{v+1}`. def wide_subquiver_symmetrify {V} [quiver.{v+1} V] : wide_subquiver (symmetrify V) → wide_subquiver V := λ H a b, { e | sum.inl e ∈ H a b ∨ sum.inr e ∈ H b a } /-- A wide subquiver of `G` can equivalently be viewed as a total set of arrows. -/ def wide_subquiver_equiv_set_total {V} [quiver V] : wide_subquiver V ≃ set (total V) := { to_fun := λ H, { e | e.hom ∈ H e.left e.right }, inv_fun := λ S a b, { e | total.mk a b e ∈ S }, left_inv := λ H, rfl, right_inv := by { intro S, ext, cases x, refl } } /-- `G.path a b` is the type of paths from `a` to `b` through the arrows of `G`. -/ inductive path {V : Type u} [quiver.{v} V] (a : V) : V → Sort (max (u+1) v) | nil : path a | cons : Π {b c : V}, path b → (b ⟶ c) → path c /-- An arrow viewed as a path of length one. -/ def hom.to_path {V} [quiver V] {a b : V} (e : a ⟶ b) : path a b := path.nil.cons e namespace path variables {V : Type u} [quiver V] /-- The length of a path is the number of arrows it uses. -/ def length {a : V} : Π {b : V}, path a b → ℕ | _ path.nil := 0 | _ (path.cons p _) := p.length + 1 @[simp] lemma length_nil {a : V} : (path.nil : path a a).length = 0 := rfl @[simp] lemma length_cons (a b c : V) (p : path a b) (e : b ⟶ c) : (p.cons e).length = p.length + 1 := rfl /-- Composition of paths. -/ def comp {a b : V} : Π {c}, path a b → path b c → path a c | _ p (path.nil) := p | _ p (path.cons q e) := (p.comp q).cons e @[simp] lemma comp_cons {a b c d : V} (p : path a b) (q : path b c) (e : c ⟶ d) : p.comp (q.cons e) = (p.comp q).cons e := rfl @[simp] lemma comp_nil {a b : V} (p : path a b) : p.comp path.nil = p := rfl @[simp] lemma nil_comp {a : V} : ∀ {b} (p : path a b), path.nil.comp p = p | a path.nil := rfl | b (path.cons p e) := by rw [comp_cons, nil_comp] @[simp] lemma comp_assoc {a b c : V} : ∀ {d} (p : path a b) (q : path b c) (r : path c d), (p.comp q).comp r = p.comp (q.comp r) | c p q path.nil := rfl | d p q (path.cons r e) := by rw [comp_cons, comp_cons, comp_cons, comp_assoc] end path end quiver namespace prefunctor open quiver variables {V : Type u₁} [quiver.{v₁} V] {W : Type u₂} [quiver.{v₂} W] (F : prefunctor V W) /-- The image of a path under a prefunctor. -/ def map_path {a : V} : Π {b : V}, path a b → path (F.obj a) (F.obj b) | _ path.nil := path.nil | _ (path.cons p e) := path.cons (map_path p) (F.map e) @[simp] lemma map_path_nil (a : V) : F.map_path (path.nil : path a a) = path.nil := rfl @[simp] lemma map_path_cons {a b c : V} (p : path a b) (e : b ⟶ c) : F.map_path (path.cons p e) = path.cons (F.map_path p) (F.map e) := rfl @[simp] lemma map_path_comp {a b : V} (p : path a b) : ∀ {c : V} (q : path b c), F.map_path (p.comp q) = (F.map_path p).comp (F.map_path q) | _ path.nil := rfl | _ (path.cons p e) := begin dsimp, rw [map_path_comp], end end prefunctor namespace quiver /-- A quiver is an arborescence when there is a unique path from the default vertex to every other vertex. -/ class arborescence (V : Type u) [quiver.{v} V] : Type (max u v) := (root : V) (unique_path : Π (b : V), unique (path root b)) /-- The root of an arborescence. -/ def root (V : Type u) [quiver V] [arborescence V] : V := arborescence.root instance {V : Type u} [quiver V] [arborescence V] (b : V) : unique (path (root V) b) := arborescence.unique_path b /-- An `L`-labelling of a quiver assigns to every arrow an element of `L`. -/ def labelling (V : Type u) [quiver V] (L : Sort*) := Π ⦃a b : V⦄, (a ⟶ b) → L instance {V : Type u} [quiver V] (L) [inhabited L] : inhabited (labelling V L) := ⟨λ a b e, default L⟩ /-- To show that `[quiver V]` is an arborescence with root `r : V`, it suffices to - provide a height function `V → ℕ` such that every arrow goes from a lower vertex to a higher vertex, - show that every vertex has at most one arrow to it, and - show that every vertex other than `r` has an arrow to it. -/ noncomputable def arborescence_mk {V : Type u} [quiver V] (r : V) (height : V → ℕ) (height_lt : ∀ ⦃a b⦄, (a ⟶ b) → height a < height b) (unique_arrow : ∀ ⦃a b c : V⦄ (e : a ⟶ c) (f : b ⟶ c), a = b ∧ e == f) (root_or_arrow : ∀ b, b = r ∨ ∃ a, nonempty (a ⟶ b)) : arborescence V := { root := r, unique_path := λ b, ⟨classical.inhabited_of_nonempty begin rcases (show ∃ n, height b < n, from ⟨_, lt_add_one _⟩) with ⟨n, hn⟩, induction n with n ih generalizing b, { exact false.elim (nat.not_lt_zero _ hn) }, rcases root_or_arrow b with ⟨⟨⟩⟩ | ⟨a, ⟨e⟩⟩, { exact ⟨path.nil⟩ }, { rcases ih a (lt_of_lt_of_le (height_lt e) (nat.lt_succ_iff.mp hn)) with ⟨p⟩, exact ⟨p.cons e⟩ } end, begin have height_le : ∀ {a b}, path a b → height a ≤ height b, { intros a b p, induction p with b c p e ih, refl, exact le_of_lt (lt_of_le_of_lt ih (height_lt e)) }, suffices : ∀ p q : path r b, p = q, { intro p, apply this }, intros p q, induction p with a c p e ih; cases q with b _ q f, { refl }, { exact false.elim (lt_irrefl _ (lt_of_le_of_lt (height_le q) (height_lt f))) }, { exact false.elim (lt_irrefl _ (lt_of_le_of_lt (height_le p) (height_lt e))) }, { rcases unique_arrow e f with ⟨⟨⟩, ⟨⟩⟩, rw ih }, end ⟩ } /-- `rooted_connected r` means that there is a path from `r` to any other vertex. -/ class rooted_connected {V : Type u} [quiver V] (r : V) : Prop := (nonempty_path : ∀ b : V, nonempty (path r b)) attribute [instance] rooted_connected.nonempty_path section geodesic_subtree variables {V : Type u} [quiver.{v+1} V] (r : V) [rooted_connected r] /-- A path from `r` of minimal length. -/ noncomputable def shortest_path (b : V) : path r b := well_founded.min (measure_wf path.length) set.univ set.univ_nonempty /-- The length of a path is at least the length of the shortest path -/ lemma shortest_path_spec {a : V} (p : path r a) : (shortest_path r a).length ≤ p.length := not_lt.mp (well_founded.not_lt_min (measure_wf _) set.univ _ trivial) /-- A subquiver which by construction is an arborescence. -/ def geodesic_subtree : wide_subquiver V := λ a b, { e | ∃ p : path r a, shortest_path r b = p.cons e } noncomputable instance geodesic_arborescence : arborescence (geodesic_subtree r) := arborescence_mk r (λ a, (shortest_path r a).length) (by { rintros a b ⟨e, p, h⟩, rw [h, path.length_cons, nat.lt_succ_iff], apply shortest_path_spec }) (by { rintros a b c ⟨e, p, h⟩ ⟨f, q, j⟩, cases h.symm.trans j, split; refl }) (by { intro b, have : ∃ p, shortest_path r b = p := ⟨_, rfl⟩, rcases this with ⟨p, hp⟩, cases p with a _ p e, { exact or.inl rfl }, { exact or.inr ⟨a, ⟨⟨e, p, hp⟩⟩⟩ } }) end geodesic_subtree variables (V : Type u) [quiver.{v+1} V] /-- A quiver `has_reverse` if we can reverse an arrow `p` from `a` to `b` to get an arrow `p.reverse` from `b` to `a`.-/ class has_reverse := (reverse' : Π {a b : V}, (a ⟶ b) → (b ⟶ a)) instance : has_reverse (symmetrify V) := ⟨λ a b e, e.swap⟩ variables {V} [has_reverse V] /-- Reverse the direction of an arrow. -/ def reverse {a b : V} : (a ⟶ b) → (b ⟶ a) := has_reverse.reverse' /-- Reverse the direction of a path. -/ def path.reverse {a : V} : Π {b}, path a b → path b a | a path.nil := path.nil | b (path.cons p e) := (reverse e).to_path.comp p.reverse variables (V) /-- Two vertices are related in the zigzag setoid if there is a zigzag of arrows from one to the other. -/ def zigzag_setoid : setoid V := ⟨λ a b, nonempty (path (a : symmetrify V) (b : symmetrify V)), λ a, ⟨path.nil⟩, λ a b ⟨p⟩, ⟨p.reverse⟩, λ a b c ⟨p⟩ ⟨q⟩, ⟨p.comp q⟩⟩ /-- The type of weakly connected components of a directed graph. Two vertices are in the same weakly connected component if there is a zigzag of arrows from one to the other. -/ def weakly_connected_component : Type* := quotient (zigzag_setoid V) namespace weakly_connected_component variable {V} /-- The weakly connected component corresponding to a vertex. -/ protected def mk : V → weakly_connected_component V := quotient.mk' instance : has_coe_t V (weakly_connected_component V) := ⟨weakly_connected_component.mk⟩ instance [inhabited V] : inhabited (weakly_connected_component V) := ⟨↑(default V)⟩ protected lemma eq (a b : V) : (a : weakly_connected_component V) = b ↔ nonempty (path (a : symmetrify V) (b : symmetrify V)) := quotient.eq' end weakly_connected_component end quiver
35505267d52bad17e5125ad172135eac0cf5f36b
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/tests/lean/run/instance_naming.lean
3aa67ada44e8121b2f96e31bf99ddc36c044a7a0
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
2,261
lean
def foo := ℕ namespace foo instance : has_add foo := nat.has_add end foo -- verify that `foo.has_add` was created example := foo.has_add namespace category_theory def functor : Type := ℕ class is_right_adjoint (F : functor) : Prop class preserves_limits (F : functor) : Prop def forgetful_functor : functor := nat.zero instance : is_right_adjoint forgetful_functor := ⟨⟩ instance : is_right_adjoint nat.zero := ⟨⟩ -- satisfy the inhabited linter instance : inhabited functor := ⟨forgetful_functor⟩ end category_theory -- here we end up with a name with a repeated component section open category_theory instance : category_theory.preserves_limits forgetful_functor := ⟨⟩ end class lie_algebra (α : Type) : Type := (bracket : α → α → α) namespace lie_algebra instance : lie_algebra ℤ := ⟨λ x y, x * y - y * x⟩ def gl : Type := unit instance : lie_algebra gl := ⟨λ _ _, ()⟩ end lie_algebra class moo (α : Type*) class zoo (β : Type*) namespace zoo -- Every zoo is a moo. instance (β : Type*) [zoo β] : moo β := ⟨⟩ end zoo -- A boo is a bundled moo. structure boo : Type 1 := (β : Type) [is_zoo : moo β] instance : has_coe_to_sort boo := ⟨_, boo.β⟩ namespace boo -- Instance naming for variable arguments works (generating `boo.moo`) -- even if the variable is actually being implicitly coerced to something else. instance (b : boo) : moo b := ⟨⟩ -- Instance naming works for projections instance (b : boo) : moo b.β := ⟨⟩ example := boo.β.moo end boo -- Instance naming works for explicit type ascriptions instance : moo (@set ℕ : Type) := ⟨⟩ example := set.moo -- also for sorts instance : moo Type := ⟨⟩ example := sort.moo -- and pis instance : moo (ℕ → ℕ) := ⟨⟩ example := pi.moo section parameter (n : ℕ) def cow := fin n -- and parameters instance : moo cow := ⟨⟩ example := cow.moo end example := category_theory.forgetful_functor.is_right_adjoint example := category_theory.nat.zero.is_right_adjoint example := category_theory.functor.inhabited example := category_theory.forgetful_functor.category_theory.preserves_limits example := lie_algebra.int.lie_algebra example := lie_algebra.gl.lie_algebra example := zoo.moo example := boo.moo
12a93588678d3c4acef20f0b45aa55ee6f7cad32
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/shapes/functor_category.lean
0b056b578ea5f2de7caf751533874f292e96833a
[ "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
933
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.finite_limits import category_theory.limits.functor_category /-! # If `D` has finite (co)limits, so do the functor categories `C ⥤ D`. These are boiler-plate instances, in their own file as neither import otherwise needs the other. -/ open category_theory namespace category_theory.limits universes z w v u variables {C : Type (max v u)} [category.{v} C] variables {D : Type w} [category.{max z v u} D] instance functor_category_has_finite_limits [has_finite_limits D] : has_finite_limits (C ⥤ D) := { out := λ J _ _, by exactI infer_instance, } instance functor_category_has_finite_colimits [has_finite_colimits D] : has_finite_colimits (C ⥤ D) := { out := λ J _ _, by exactI infer_instance, } end category_theory.limits
b6b7954e4d614cf1e9061413887976ffee610f4e
7cef822f3b952965621309e88eadf618da0c8ae9
/src/order/lexicographic.lean
872b7fbba90022535f17f5d7f57398fc5b98938c
[ "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,454
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison, Minchao Wu Lexicographic preorder / partial_order / linear_order / decidable_linear_order, for pairs and dependent pairs. -/ import tactic.basic import algebra.order universes u v def lex (α : Type u) (β : Type v) := α × β variables {α : Type u} {β : Type v} /-- Dictionary / lexicographic ordering on pairs. -/ instance lex_has_le [preorder α] [preorder β] : has_le (lex α β) := { le := prod.lex (<) (≤) } instance lex_has_lt [preorder α] [preorder β] : has_lt (lex α β) := { lt := prod.lex (<) (<) } /-- Dictionary / lexicographic preorder for pairs. -/ instance lex_preorder [preorder α] [preorder β] : preorder (lex α β) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. lex_has_le, .. lex_has_lt } /-- Dictionary / lexicographic partial_order for pairs. -/ instance lex_partial_order [partial_order α] [partial_order β] : partial_order (lex α β) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. lex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance lex_linear_order [linear_order α] [linear_order β] : linear_order (lex α β) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases le_total a₁ a₂ with ha | ha; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, rcases le_total b₁ b₂ with hb | hb }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end .. lex_partial_order }. /-- Dictionary / lexicographic decidable_linear_order for pairs. -/ instance lex_decidable_linear_order [decidable_linear_order α] [decidable_linear_order β] : decidable_linear_order (lex α β) := { decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases decidable_linear_order.decidable_le α a₁ a₂ with a_lt | a_le, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { rw h, rcases decidable_linear_order.decidable_le _ b₁ b₂ with b_lt | b_le, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. lex_linear_order } variables {Z : α → Type v} /-- Dictionary / lexicographic ordering on dependent pairs. The 'pointwise' partial order `prod.has_le` doesn't make sense for dependent pairs, so it's safe to mark these as instances here. -/ instance dlex_has_le [preorder α] [∀ a, preorder (Z a)] : has_le (Σ' a, Z a) := { le := psigma.lex (<) (λ a, (≤)) } instance dlex_has_lt [preorder α] [∀ a, preorder (Z a)] : has_lt (Σ' a, Z a) := { lt := psigma.lex (<) (λ a, (<)) } /-- Dictionary / lexicographic preorder on dependent pairs. -/ instance dlex_preorder [preorder α] [∀ a, preorder (Z a)] : preorder (Σ' a, Z a) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. dlex_has_le, .. dlex_has_lt } /-- Dictionary / lexicographic partial_order for dependent pairs. -/ instance dlex_partial_order [partial_order α] [∀ a, partial_order (Z a)] : partial_order (Σ' a, Z a) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. dlex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance dlex_linear_order [linear_order α] [∀ a, linear_order (Z a)] : linear_order (Σ' a, Z a) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases le_total a₁ a₂ with ha | ha; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, rcases le_total b₁ b₂ with hb | hb }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end .. dlex_partial_order }. /-- Dictionary / lexicographic decidable_linear_order for dependent pairs. -/ instance dlex_decidable_linear_order [decidable_linear_order α] [∀ a, decidable_linear_order (Z a)] : decidable_linear_order (Σ' a, Z a) := { decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases decidable_linear_order.decidable_le α a₁ a₂ with a_lt | a_le, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { subst h, rcases decidable_linear_order.decidable_le _ b₁ b₂ with b_lt | b_le, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₁, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. dlex_linear_order }
e89ce6271f84cf2b9feff4a0cea2d38afdfa481a
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/ring_theory/localization.lean
9c83c6857b94d13b0be28cba86cf0bca1dc250fe
[ "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
73,054
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston -/ import data.equiv.ring import group_theory.monoid_localization import ring_theory.ideal.operations import ring_theory.algebraic import ring_theory.integral_closure import ring_theory.non_zero_divisors /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. In the following, let `R, P` be commutative rings, `S, Q` be `R`- and `P`-algebras and `M, T` be submonoids of `R` and `P` respectively, e.g.: ``` variables (R S P Q : Type*) [comm_ring R] [comm_ring S] [comm_ring P] [comm_ring Q] variables [algebra R S] [algebra P Q] (M : submonoid R) (T : submonoid P) ``` ## Main definitions * `is_localization (M : submonoid R) (S : Type*)` is a typeclass expressing that `S` is a localization of `R` at `M`, i.e. the canonical map `algebra_map R S : R →+* S` is a localization map (satisfying the above properties). * `is_localization.mk' S` is a surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹` * `is_localization.lift` is the ring homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. * `is_localization.map S Q` is the ring homomorphism from `S` to `Q` which maps elements of `M` to elements of `T` * `is_localization.ring_equiv_of_ring_equiv`: if `R` and `P` are isomorphic by an isomorphism sending `M` to `T`, then `S` and `Q` are isomorphic * `is_localization.alg_equiv`: if `Q` is another localization of `R` at `M`, then `S` and `Q` are isomorphic as `R`-algebras * `is_localization.is_integer` is a predicate stating that `x : S` is in the image of `R` * `is_localization.away (x : R) S` expresses that `S` is a localization away from `x`, as an abbreviation of `is_localization (submonoid.powers x) S` * `is_localization.at_prime (I : ideal R) [is_prime I] (S : Type*)` expresses that `S` is a localization at (the complement of) a prime ideal `I`, as an abbreviation of `is_localization I.prime_compl S` * `is_fraction_ring R K` expresses that `K` is a field of fractions of `R`, as an abbreviation of `is_localization (non_zero_divisors R) K` ## Main results * `localization M S`, a construction of the localization as a quotient type, defined in `group_theory.monoid_localization`, has `comm_ring`, `algebra R` and `is_localization M` instances if `R` is a ring. `localization.away`, `localization.at_prime` and `fraction_ring` are abbreviations for `localization`s and have their corresponding `is_localization` instances * `is_localization.at_prime.local_ring`: a theorem (not an instance) stating a localization at the complement of a prime ideal is a local ring * `is_fraction_ring.field`: a definition (not an instance) stating the localization of an integral domain `R` at `R \ {0}` is a field * `rat.is_fraction_ring_int` is an instance stating `ℚ` is the field of fractions of `ℤ` ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A previous version of this file used a fully bundled type of ring localization maps, then used a type synonym `f.codomain` for `f : localization_map M S` to instantiate the `R`-algebra structure on `S`. This results in defining ad-hoc copies for everything already defined on `S`. By making `is_localization` a predicate on the `algebra_map R S`, we can ensure the localization map commutes nicely with other `algebra_map`s. To prove most lemmas about a localization map `algebra_map R S` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `is_localization.to_localization_map M S`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → localization M` equals the surjection `localization_map.mk'` induced by the map `algebra_map : R →+* localization M`. The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] variables [algebra R S] {P : Type*} [comm_ring P] open function open_locale big_operators /-- The typeclass `is_localization (M : submodule R) S` where `S` is an `R`-algebra expresses that `S` is isomorphic to the localization of `R` at `M`. -/ class is_localization : Prop := (map_units [] : ∀ y : M, is_unit (algebra_map R S y)) (surj [] : ∀ z : S, ∃ x : R × M, z * algebra_map R S x.2 = algebra_map R S x.1) (eq_iff_exists [] : ∀ {x y}, algebra_map R S x = algebra_map R S y ↔ ∃ c : M, x * c = y * c) variables {M S} namespace is_localization section is_localization variables [is_localization M S] section variables (M S) /-- `is_localization.to_localization_map M S` shows `S` is the monoid localization of `R` at `M`. -/ @[simps] def to_localization_map : submonoid.localization_map M S := { to_fun := algebra_map R S, map_units' := is_localization.map_units _, surj' := is_localization.surj _, eq_iff_exists' := λ _ _, is_localization.eq_iff_exists _ _, .. algebra_map R S } @[simp] lemma to_localization_map_to_map : (to_localization_map M S).to_map = (algebra_map R S : R →* S) := rfl lemma to_localization_map_to_map_apply (x) : (to_localization_map M S).to_map x = algebra_map R S x := rfl end section variables (R) -- TODO: define a subalgebra of `is_integer`s /-- Given `a : S`, `S` a localization of `R`, `is_integer R a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer (a : S) : Prop := a ∈ (algebra_map R S).range end lemma is_integer_zero : is_integer R (0 : S) := subring.zero_mem _ lemma is_integer_one : is_integer R (1 : S) := subring.one_mem _ lemma is_integer_add {a b : S} (ha : is_integer R a) (hb : is_integer R b) : is_integer R (a + b) := subring.add_mem _ ha hb lemma is_integer_mul {a b : S} (ha : is_integer R a) (hb : is_integer R b) : is_integer R (a * b) := subring.mul_mem _ ha hb lemma is_integer_smul {a : R} {b : S} (hb : is_integer R b) : is_integer R (a • b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, (algebra_map R S).map_mul, algebra.smul_def] end variables (M) /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ lemma exists_integer_multiple' (a : S) : ∃ (b : M), is_integer R (a * algebra_map R S b) := let ⟨⟨num, denom⟩, h⟩ := is_localization.surj _ a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩ /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ lemma exists_integer_multiple (a : S) : ∃ (b : M), is_integer R ((b : R) • a) := by { simp_rw [algebra.smul_def, mul_comm _ a], apply exists_integer_multiple' } /-- 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 (z : S) : R × M := classical.some $ is_localization.surj _ z @[simp] lemma to_localization_map_sec : (to_localization_map M S).sec = sec M := rfl /-- Given `z : S`, `is_localization.sec M z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x` (so this lemma is true by definition). -/ lemma sec_spec (z : S) : z * algebra_map R S (is_localization.sec M z).2 = algebra_map R S (is_localization.sec M z).1 := classical.some_spec $ is_localization.surj _ z /-- Given `z : S`, `is_localization.sec M z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ lemma sec_spec' (z : S) : algebra_map R S (is_localization.sec M z).1 = algebra_map R S (is_localization.sec M z).2 * z := by rw [mul_comm, sec_spec] open_locale big_operators /-- We can clear the denominators of a finite set of fractions. -/ lemma exist_integer_multiples_of_finset (s : finset S) : ∃ (b : M), ∀ a ∈ s, is_integer R ((b : R) • a) := begin haveI := classical.prop_decidable, use ∏ a in s, (is_localization.sec M a).2, intros a ha, use (∏ x in s.erase a, (is_localization.sec M x).2) * (is_localization.sec M a).1, rw [ring_hom.map_mul, sec_spec', ←mul_assoc, ←(algebra_map R S).map_mul, ← algebra.smul_def], congr' 2, refine trans _ ((submonoid.subtype M).map_prod _ _).symm, rw [mul_comm, ←finset.prod_insert (s.not_mem_erase a), finset.insert_erase ha], refl, end variables {R M} lemma map_right_cancel {x y} {c : M} (h : algebra_map R S (c * x) = algebra_map R S (c * y)) : algebra_map R S x = algebra_map R S y := (to_localization_map M S).map_right_cancel h lemma map_left_cancel {x y} {c : M} (h : algebra_map R S (x * c) = algebra_map R S (y * c)) : algebra_map R S x = algebra_map R S y := (to_localization_map M S).map_left_cancel h lemma eq_zero_of_fst_eq_zero {z x} {y : M} (h : z * algebra_map R S y = algebra_map R S x) (hx : x = 0) : z = 0 := by { rw [hx, (algebra_map R S).map_zero] at h, exact (is_unit.mul_left_eq_zero (is_localization.map_units S y)).1 h} variables (S) /-- `is_localization.mk' S` is the surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹`. -/ noncomputable def mk' (x : R) (y : M) : S := (to_localization_map M S).mk' x y @[simp] lemma mk'_sec (z : S) : mk' S (is_localization.sec M z).1 (is_localization.sec M z).2 = z := (to_localization_map M S).mk'_sec _ lemma mk'_mul (x₁ x₂ : R) (y₁ y₂ : M) : mk' S (x₁ * x₂) (y₁ * y₂) = mk' S x₁ y₁ * mk' S x₂ y₂ := (to_localization_map M S).mk'_mul _ _ _ _ lemma mk'_one (x) : mk' S x (1 : M) = algebra_map R S x := (to_localization_map M S).mk'_one _ @[simp] lemma mk'_spec (x) (y : M) : mk' S x y * algebra_map R S y = algebra_map R S x := (to_localization_map M S).mk'_spec _ _ @[simp] lemma mk'_spec' (x) (y : M) : algebra_map R S y * mk' S x y = algebra_map R S x := (to_localization_map M S).mk'_spec' _ _ variables {S} theorem eq_mk'_iff_mul_eq {x} {y : M} {z} : z = mk' S x y ↔ z * algebra_map R S y = algebra_map R S x := (to_localization_map M S).eq_mk'_iff_mul_eq theorem mk'_eq_iff_eq_mul {x} {y : M} {z} : mk' S x y = z ↔ algebra_map R S x = z * algebra_map R S y := (to_localization_map M S).mk'_eq_iff_eq_mul variables (M) lemma mk'_surjective (z : S) : ∃ x (y : M), mk' S x y = z := let ⟨r, hr⟩ := is_localization.surj _ z in ⟨r.1, r.2, (eq_mk'_iff_mul_eq.2 hr).symm⟩ variables {M} lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : M} : mk' S x₁ y₁ = mk' S x₂ y₂ ↔ algebra_map R S (x₁ * y₂) = algebra_map R S (x₂ * y₁) := (to_localization_map M S).mk'_eq_iff_eq lemma mk'_mem_iff {x} {y : M} {I : ideal S} : mk' S x y ∈ I ↔ algebra_map R S x ∈ I := begin split; intro h, { rw [← mk'_spec S x y, mul_comm], exact I.mul_mem_left ((algebra_map R S) y) h }, { rw ← mk'_spec S x y at h, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (map_units S y), have := I.mul_mem_left b h, rwa [mul_comm, mul_assoc, hb, mul_one] at this } end protected lemma eq {a₁ b₁} {a₂ b₂ : M} : mk' S a₁ a₂ = mk' S b₁ b₂ ↔ ∃ c : M, a₁ * b₂ * c = b₁ * a₂ * c := (to_localization_map M S).eq section ext variables [algebra R P] [is_localization M P] lemma eq_iff_eq {x y} : algebra_map R S x = algebra_map R S y ↔ algebra_map R P x = algebra_map R P y := (to_localization_map M S).eq_iff_eq (to_localization_map M P) lemma mk'_eq_iff_mk'_eq {x₁ x₂} {y₁ y₂ : M} : mk' S x₁ y₁ = mk' S x₂ y₂ ↔ mk' P x₁ y₁ = mk' P x₂ y₂ := (to_localization_map M S).mk'_eq_iff_mk'_eq (to_localization_map M P) lemma mk'_eq_of_eq {a₁ b₁ : R} {a₂ b₂ : M} (H : b₁ * a₂ = a₁ * b₂) : mk' S a₁ a₂ = mk' S b₁ b₂ := (to_localization_map M S).mk'_eq_of_eq H variables (S) @[simp] lemma mk'_self {x : R} (hx : x ∈ M) : mk' S x ⟨x, hx⟩ = 1 := (to_localization_map M S).mk'_self _ hx @[simp] lemma mk'_self' {x : M} : mk' S (x : R) x = 1 := (to_localization_map M S).mk'_self' _ lemma mk'_self'' {x : M} : mk' S x.1 x = 1 := mk'_self' _ end ext lemma mul_mk'_eq_mk'_of_mul (x y : R) (z : M) : (algebra_map R S) x * mk' S y z = mk' S (x * y) z := (to_localization_map M S).mul_mk'_eq_mk'_of_mul _ _ _ lemma mk'_eq_mul_mk'_one (x : R) (y : M) : mk' S x y = (algebra_map R S) x * mk' S 1 y := ((to_localization_map M S).mul_mk'_one_eq_mk' _ _).symm @[simp] lemma mk'_mul_cancel_left (x : R) (y : M) : mk' S (y * x : R) y = (algebra_map R S) x := (to_localization_map M S).mk'_mul_cancel_left _ _ lemma mk'_mul_cancel_right (x : R) (y : M) : mk' S (x * y) y = (algebra_map R S) x := (to_localization_map M S).mk'_mul_cancel_right _ _ @[simp] lemma mk'_mul_mk'_eq_one (x y : M) : mk' S (x : R) y * mk' S (y : R) x = 1 := by rw [←mk'_mul, mul_comm]; exact mk'_self _ _ lemma mk'_mul_mk'_eq_one' (x : R) (y : M) (h : x ∈ M) : mk' S x y * mk' S (y : R) ⟨x, h⟩ = 1 := mk'_mul_mk'_eq_one ⟨x, h⟩ _ section variables (M) lemma is_unit_comp (j : S →+* P) (y : M) : is_unit (j.comp (algebra_map R S) y) := (to_localization_map M S).is_unit_comp j.to_monoid_hom _ end /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g(M) ⊆ units P`, `f x = f y → g x = g y` for all `x y : R`. -/ lemma eq_of_eq {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) {x y} (h : (algebra_map R S) x = (algebra_map R S) y) : g x = g y := @submonoid.localization_map.eq_of_eq _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom hg _ _ h lemma mk'_add (x₁ x₂ : R) (y₁ y₂ : M) : mk' S (x₁ * y₂ + x₂ * y₁) (y₁ * y₂) = mk' S x₁ y₁ + mk' S x₂ y₂ := mk'_eq_iff_eq_mul.2 $ eq.symm begin rw [mul_comm (_ + _), mul_add, mul_mk'_eq_mk'_of_mul, ←eq_sub_iff_add_eq, mk'_eq_iff_eq_mul, mul_comm _ ((algebra_map R S) _), mul_sub, eq_sub_iff_add_eq, ←eq_sub_iff_add_eq', ←mul_assoc, ←(algebra_map R S).map_mul, mul_mk'_eq_mk'_of_mul, mk'_eq_iff_eq_mul], simp only [(algebra_map R S).map_add, submonoid.coe_mul, (algebra_map R S).map_mul], ring_exp, end /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) : S →+* P := ring_hom.mk' (@submonoid.localization_map.lift _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom hg) $ begin intros x y, rw [(to_localization_map M S).lift_spec, mul_comm, add_mul, ←sub_eq_iff_eq_add, eq_comm, (to_localization_map M S).lift_spec_mul, mul_comm _ (_ - _), sub_mul, eq_sub_iff_add_eq', ←eq_sub_iff_add_eq, mul_assoc, (to_localization_map M S).lift_spec_mul], show g _ * (g _ * g _) = g _ * (g _ * g _ - g _ * g _), simp only [← g.map_sub, ← g.map_mul, to_localization_map_sec], apply eq_of_eq hg, rw [(algebra_map R S).map_mul, sec_spec', mul_sub, (algebra_map R S).map_sub], simp only [ring_hom.map_mul, sec_spec'], ring, assumption end variables {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ lemma lift_mk' (x y) : lift hg (mk' S x y) = g x * ↑(is_unit.lift_right (g.to_monoid_hom.mrestrict M) hg y)⁻¹ := (to_localization_map M S).lift_mk' _ _ _ lemma lift_mk'_spec (x v) (y : M) : lift hg (mk' S x y) = v ↔ g x = g y * v := (to_localization_map M S).lift_mk'_spec _ _ _ _ @[simp] lemma lift_eq (x : R) : lift hg ((algebra_map R S) x) = g x := (to_localization_map M S).lift_eq _ _ lemma lift_eq_iff {x y : R × M} : lift hg (mk' S x.1 x.2) = lift hg (mk' S y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := (to_localization_map M S).lift_eq_iff _ @[simp] lemma lift_comp : (lift hg).comp (algebra_map R S) = g := ring_hom.ext $ monoid_hom.ext_iff.1 $ (to_localization_map M S).lift_comp _ @[simp] lemma lift_of_comp (j : S →+* P) : lift (is_unit_comp M j) = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ (to_localization_map M S).lift_of_comp j.to_monoid_hom variables (M) lemma epic_of_localization_map (j k : S →+* P) (h : ∀ a, j.comp (algebra_map R S) a = k.comp (algebra_map R S) a) : j = k := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.epic_of_localization_map _ _ _ _ _ _ _ (to_localization_map M S) j.to_monoid_hom k.to_monoid_hom h variables {M} lemma lift_unique {j : S →+* P} (hj : ∀ x, j ((algebra_map R S) x) = g x) : lift hg = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.lift_unique _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom hg j.to_monoid_hom hj @[simp] lemma lift_id (x) : lift (map_units S : ∀ y : M, is_unit _) x = x := (to_localization_map M S).lift_id _ lemma lift_surjective_iff : surjective (lift hg : S → P) ↔ ∀ v : P, ∃ x : R × M, v * g x.2 = g x.1 := (to_localization_map M S).lift_surjective_iff hg lemma lift_injective_iff : injective (lift hg : S → P) ↔ ∀ x y, algebra_map R S x = algebra_map R S y ↔ g x = g y := (to_localization_map M S).lift_injective_iff hg section map variables {T : submonoid P} {Q : Type*} [comm_ring Q] (hy : M ≤ T.comap g) variables [algebra P Q] [is_localization T Q] section variables (Q) /-- Map a homomorphism `g : R →+* P` to `S →+* Q`, where `S` and `Q` are localizations of `R` and `P` at `M` and `T` respectively, such that `g(M) ⊆ T`. We send `z : S` to `algebra_map P Q (g x) * (algebra_map P Q (g y))⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map (g : R →+* P) (hy : M ≤ T.comap g) : S →+* Q := @lift R _ M _ _ _ _ _ _ ((algebra_map P Q).comp g) (λ y, map_units _ ⟨g y, hy y.2⟩) end lemma map_eq (x) : map Q g hy ((algebra_map R S) x) = algebra_map P Q (g x) := lift_eq (λ y, map_units _ ⟨g y, hy y.2⟩) x @[simp] lemma map_comp : (map Q g hy).comp (algebra_map R S) = (algebra_map P Q).comp g := lift_comp $ λ y, map_units _ ⟨g y, hy y.2⟩ lemma map_mk' (x) (y : M) : map Q g hy (mk' S x y) = mk' Q (g x) ⟨g y, hy y.2⟩ := @submonoid.localization_map.map_mk' _ _ _ _ _ _ _ (to_localization_map M S) g.to_monoid_hom _ (λ y, hy y.2) _ _ (to_localization_map T Q) _ _ @[simp] lemma map_id (z : S) (h : M ≤ M.comap (ring_hom.id R) := le_refl M) : map S (ring_hom.id _) h z = z := lift_id _ lemma map_unique (j : S →+* Q) (hj : ∀ x : R, j (algebra_map R S x) = algebra_map P Q (g x)) : map Q g hy = j := lift_unique (λ y, map_units _ ⟨g y, hy y.2⟩) hj /-- If `comm_ring` homs `g : R →+* 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_ring A] {U : submonoid A} {W} [comm_ring W] [algebra A W] [is_localization U W] {l : P →+* A} (hl : T ≤ U.comap l) : (map W l hl).comp (map Q g hy : S →+* _) = map W (l.comp g) (λ x hx, hl (hy hx)) := ring_hom.ext $ λ x, @submonoid.localization_map.map_map _ _ _ _ _ P _ (to_localization_map M S) g _ _ _ _ _ _ _ _ _ _ (to_localization_map U W) l _ x /-- If `comm_ring` homs `g : R →+* 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_ring A] {U : submonoid A} {W} [comm_ring W] [algebra A W] [is_localization U W] {l : P →+* A} (hl : T ≤ U.comap l) (x : S) : map W l hl (map Q g hy x) = map W (l.comp g) (λ x hx, hl (hy hx)) x := by rw ←map_comp_map hy hl; refl section variables (S Q) /-- If `S`, `Q` are localizations of `R` and `P` at submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ @[simps] noncomputable def ring_equiv_of_ring_equiv (h : R ≃+* P) (H : M.map h.to_monoid_hom = T) : S ≃+* Q := have H' : T.map h.symm.to_monoid_hom = M, by { rw [← M.map_id, ← H, submonoid.map_map], congr, ext, apply h.symm_apply_apply }, { to_fun := map Q (h : R →+* P) (M.le_comap_of_map_le (le_of_eq H)), inv_fun := map S (h.symm : P →+* R) (T.le_comap_of_map_le (le_of_eq H')), left_inv := λ x, by { rw [map_map, map_unique _ (ring_hom.id _), ring_hom.id_apply], intro x, convert congr_arg (algebra_map R S) (h.symm_apply_apply x).symm }, right_inv := λ x, by { rw [map_map, map_unique _ (ring_hom.id _), ring_hom.id_apply], intro x, convert congr_arg (algebra_map P Q) (h.apply_symm_apply x).symm }, .. map Q (h : R →+* P) _ } end lemma ring_equiv_of_ring_equiv_eq_map {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) : (ring_equiv_of_ring_equiv S Q j H : S →+* Q) = map Q (j : R →+* P) (M.le_comap_of_map_le (le_of_eq H)) := rfl @[simp] lemma ring_equiv_of_ring_equiv_eq {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : ring_equiv_of_ring_equiv S Q j H ((algebra_map R S) x) = algebra_map P Q (j x) := map_eq _ _ lemma ring_equiv_of_ring_equiv_mk' {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x : R) (y : M) : ring_equiv_of_ring_equiv S Q j H (mk' S x y) = mk' Q (j x) ⟨j y, show j y ∈ T, from H ▸ set.mem_image_of_mem j y.2⟩ := map_mk' _ _ _ end map section alg_equiv variables {Q : Type*} [comm_ring Q] [algebra R Q] [is_localization M Q] section variables (M S Q) /-- If `S`, `Q` are localizations of `R` at the submonoid `M` respectively, there is an isomorphism of localizations `S ≃ₐ[R] Q`. -/ @[simps] noncomputable def alg_equiv : S ≃ₐ[R] Q := { commutes' := ring_equiv_of_ring_equiv_eq _, .. ring_equiv_of_ring_equiv S Q (ring_equiv.refl R) M.map_id } end @[simp] lemma alg_equiv_mk' (x : R) (y : M) : alg_equiv M S Q (mk' S x y) = mk' Q x y:= map_mk' _ _ _ @[simp] lemma alg_equiv_symm_mk' (x : R) (y : M) : (alg_equiv M S Q).symm (mk' Q x y) = mk' S x y:= map_mk' _ _ _ end alg_equiv end is_localization section away variables (x : R) /-- Given `x : R`, the typeclass `is_localization.away x S` states that `S` is isomorphic to the localization of `R` at the submonoid generated by `x`. -/ abbreviation away (S : Type*) [comm_ring S] [algebra R S] := is_localization (submonoid.powers x) S namespace away variables [is_localization.away x S] /-- Given `x : R` and a localization map `F : R →+* S` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def inv_self : S := mk' S 1 ⟨x, submonoid.mem_powers _⟩ variables {g : R →+* P} /-- Given `x : R`, a localization map `F : R →+* S` away from `x`, and a map of `comm_ring`s `g : R →+* P` such that `g x` is invertible, the homomorphism induced from `S` to `P` sending `z : S` to `g y * (g x)⁻ⁿ`, where `y : R, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def lift (hg : is_unit (g x)) : S →+* P := is_localization.lift $ λ (y : submonoid.powers x), show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.map (monoid_hom.of $ ((^ n) : P → P)) hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : R) : lift x hg ((algebra_map R S) a) = g a := lift_eq _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (lift x hg).comp (algebra_map R S) = g := lift_comp _ /-- Given `x y : R` and localizations `S`, `P` away from `x` and `x * y` respectively, the homomorphism induced from `S` to `P`. -/ noncomputable def away_to_away_right (y : R) [algebra R P] [is_localization.away (x * y) P] : S →+* P := lift x $ show is_unit ((algebra_map R P) x), from is_unit_of_mul_eq_one ((algebra_map R P) x) (mk' P y ⟨x * y, submonoid.mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away end away end is_localization namespace localization open is_localization /-! ### Constructing a localization at a given submonoid -/ variables {M} instance : has_add (localization M) := ⟨λ z w, con.lift_on₂ z w (λ x y : R × M, mk ((x.2 : R) * y.1 + y.2 * x.1) (x.2 * y.2)) $ λ r1 r2 r3 r4 h1 h2, (con.eq _).2 begin rw r_eq_r' at h1 h2 ⊢, cases h1 with t₅ ht₅, cases h2 with t₆ ht₆, use t₆ * t₅, calc ((r1.2 : R) * r2.1 + r2.2 * r1.1) * (r3.2 * r4.2) * (t₆ * t₅) = (r2.1 * r4.2 * t₆) * (r1.2 * r3.2 * t₅) + (r1.1 * r3.2 * t₅) * (r2.2 * r4.2 * t₆) : by ring ... = (r3.2 * r4.1 + r4.2 * r3.1) * (r1.2 * r2.2) * (t₆ * t₅) : by rw [ht₆, ht₅]; ring end⟩ instance : has_neg (localization M) := ⟨λ z, con.lift_on z (λ x : R × M, mk (-x.1) x.2) $ λ r1 r2 h, (con.eq _).2 begin rw r_eq_r' at h ⊢, cases h with t ht, use t, rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht], ring_nf, end⟩ instance : has_zero (localization M) := ⟨mk 0 1⟩ private meta def tac := `[{ intros, refine quotient.sound' (r_of_eq _), simp only [prod.snd_mul, prod.fst_mul, submonoid.coe_mul], ring }] instance : comm_ring (localization M) := { zero := 0, one := 1, add := (+), mul := (*), add_assoc := λ m n k, quotient.induction_on₃' m n k (by tac), zero_add := λ y, quotient.induction_on' y (by tac), add_zero := λ y, quotient.induction_on' y (by tac), neg := has_neg.neg, sub := λ x y, x + -y, sub_eq_add_neg := λ x y, rfl, add_left_neg := λ y, by exact quotient.induction_on' y (by tac), add_comm := λ y z, quotient.induction_on₂' z y (by tac), left_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), right_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), ..localization.comm_monoid M } instance : algebra R (localization M) := ring_hom.to_algebra $ { map_zero' := rfl, map_add' := λ x y, (con.eq _).2 $ r_of_eq $ by simp [add_comm], .. localization.monoid_of M } instance : is_localization M (localization M) := { map_units := (localization.monoid_of M).map_units, surj := (localization.monoid_of M).surj, eq_iff_exists := λ _ _, (localization.monoid_of M).eq_iff_exists } lemma monoid_of_eq_algebra_map (x) : (monoid_of M).to_map x = algebra_map R (localization M) x := rfl lemma mk_one_eq_algebra_map (x) : mk x 1 = algebra_map R (localization M) x := rfl lemma mk_eq_mk'_apply (x y) : mk x y = is_localization.mk' (localization M) x y := mk_eq_monoid_of_mk'_apply _ _ @[simp] lemma mk_eq_mk' : (mk : R → M → (localization M)) = is_localization.mk' (localization M) := mk_eq_monoid_of_mk' variables [is_localization M S] section variables (M S) /-- The localization of `R` at `M` as a quotient type is isomorphic to any other localization. -/ @[simps] noncomputable def alg_equiv : localization M ≃ₐ[R] S := is_localization.alg_equiv M _ _ end @[simp] lemma alg_equiv_mk' (x : R) (y : M) : alg_equiv M S (mk' (localization M) x y) = mk' S x y := alg_equiv_mk' _ _ @[simp] lemma alg_equiv_symm_mk' (x : R) (y : M) : (alg_equiv M S).symm (mk' S x y) = mk' (localization M) x y := alg_equiv_symm_mk' _ _ lemma alg_equiv_mk (x y) : alg_equiv M S (mk x y) = mk' S x y := by rw [mk_eq_mk', alg_equiv_mk'] lemma alg_equiv_symm_mk (x : R) (y : M) : (alg_equiv M S).symm (mk' S x y) = mk x y := by rw [mk_eq_mk', alg_equiv_symm_mk'] end localization variables {M} section at_prime variables (I : ideal R) [hp : I.is_prime] include hp namespace ideal /-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/ def prime_compl : submonoid R := { carrier := (Iᶜ : set R), one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl, mul_mem' := λ x y hnx hny hxy, or.cases_on (hp.mem_or_mem hxy) hnx hny } end ideal variables (S) /-- Given a prime ideal `P`, the typeclass `is_localization.at_prime S P` states that `S` is isomorphic to the localization of `R` at the complement of `P`. -/ protected abbreviation is_localization.at_prime := is_localization I.prime_compl S /-- Given a prime ideal `P`, `localization.at_prime S P` is a localization of `R` at the complement of `P`, as a quotient type. -/ protected abbreviation localization.at_prime := localization I.prime_compl namespace is_localization theorem at_prime.local_ring [is_localization.at_prime S I] : local_ring S := local_of_nonunits_ideal (λ hze, begin rw [←(algebra_map R S).map_one, ←(algebra_map R S).map_zero] at hze, obtain ⟨t, ht⟩ := (eq_iff_exists I.prime_compl S).1 hze, exact ((show (t : R) ∉ I, from t.2) (have htz : (t : R) = 0, by simpa using ht.symm, htz.symm ▸ I.zero_mem)) end) (begin intros x y hx hy hu, cases is_unit_iff_exists_inv.1 hu with z hxyz, have : ∀ {r : R} {s : I.prime_compl}, mk' S r s ∈ nonunits S → r ∈ I, from λ (r : R) (s : I.prime_compl), not_imp_comm.1 (λ nr, is_unit_iff_exists_inv.2 ⟨mk' S ↑s (⟨r, nr⟩ : I.prime_compl), mk'_mul_mk'_eq_one' _ _ nr⟩), rcases mk'_surjective I.prime_compl x with ⟨rx, sx, hrx⟩, rcases mk'_surjective I.prime_compl y with ⟨ry, sy, hry⟩, rcases mk'_surjective I.prime_compl z with ⟨rz, sz, hrz⟩, rw [←hrx, ←hry, ←hrz, ←mk'_add, ←mk'_mul, ←mk'_self S I.prime_compl.one_mem] at hxyz, rw ←hrx at hx, rw ←hry at hy, obtain ⟨t, ht⟩ := is_localization.eq.1 hxyz, simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht, rw [←sub_eq_zero, ←sub_mul] at ht, have hr := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right t.2, rw sub_eq_add_neg at hr, have := I.neg_mem_iff.1 ((ideal.add_mem_iff_right _ _).1 hr), { exact not_or (mt hp.mem_or_mem (not_or sx.2 sy.2)) sz.2 (hp.mem_or_mem this)}, { exact I.mul_mem_right _ (I.add_mem (I.mul_mem_right _ (this hx)) (I.mul_mem_right _ (this hy)))} end) end is_localization namespace localization /-- The localization of `R` at the complement of a prime ideal is a local ring. -/ instance at_prime.local_ring : local_ring (localization I.prime_compl) := is_localization.at_prime.local_ring (localization I.prime_compl) I end localization end at_prime namespace is_localization variables [is_localization M S] section ideals variables (M) (S) include M /-- Explicit characterization of the ideal given by `ideal.map (algebra_map R S) I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_to_map_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ private def map_ideal (I : ideal R) : ideal S := { carrier := { z : S | ∃ x : I × M, z * algebra_map R S x.2 = algebra_map R S x.1}, zero_mem' := ⟨⟨0, 1⟩, by simp⟩, add_mem' := begin rintros a b ⟨a', ha⟩ ⟨b', hb⟩, use ⟨a'.2 * b'.1 + b'.2 * a'.1, I.add_mem (I.mul_mem_left _ b'.1.2) (I.mul_mem_left _ a'.1.2)⟩, use a'.2 * b'.2, simp only [ring_hom.map_add, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], rw [add_mul, ← mul_assoc a, ha, mul_comm (algebra_map R S a'.2) (algebra_map R S b'.2), ← mul_assoc b, hb], ring end, smul_mem' := begin rintros c x ⟨x', hx⟩, obtain ⟨c', hc⟩ := is_localization.surj M c, use ⟨c'.1 * x'.1, I.mul_mem_left c'.1 x'.1.2⟩, use c'.2 * x'.2, simp only [←hx, ←hc, smul_eq_mul, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], ring end } theorem mem_map_algebra_map_iff {I : ideal R} {z} : z ∈ ideal.map (algebra_map R S) I ↔ ∃ x : I × M, z * algebra_map R S x.2 = algebra_map R S x.1 := begin split, { change _ → z ∈ map_ideal M S I, refine λ h, ideal.mem_Inf.1 h (λ z hz, _), obtain ⟨y, hy⟩ := hz, use ⟨⟨⟨y, hy.left⟩, 1⟩, by simp [hy.right]⟩ }, { rintros ⟨⟨a, s⟩, h⟩, rw [← ideal.unit_mul_mem_iff_mem _ (map_units S s), mul_comm], exact h.symm ▸ ideal.mem_map_of_mem _ a.2 } end theorem map_comap (J : ideal S) : ideal.map (algebra_map R S) (ideal.comap (algebra_map R S) J) = J := le_antisymm (ideal.map_le_iff_le_comap.2 (le_refl _)) $ λ x hJ, begin obtain ⟨r, s, hx⟩ := mk'_surjective M x, rw ←hx at ⊢ hJ, exact ideal.mul_mem_right _ _ (ideal.mem_map_of_mem _ (show (algebra_map R S) r ∈ J, from mk'_spec S r s ▸ J.mul_mem_right ((algebra_map R S) s) hJ)), end theorem comap_map_of_is_prime_disjoint (I : ideal R) (hI : I.is_prime) (hM : disjoint (M : set R) I) : ideal.comap (algebra_map R S) (ideal.map (algebra_map R S) I) = I := begin refine le_antisymm (λ a ha, _) ideal.le_comap_map, rw [ideal.mem_comap, mem_map_algebra_map_iff M S] at ha, obtain ⟨⟨b, s⟩, h⟩ := ha, have : (algebra_map R S) (a * ↑s - b) = 0 := by simpa [sub_eq_zero] using h, rw [← (algebra_map R S).map_zero, eq_iff_exists M S] at this, obtain ⟨c, hc⟩ := this, have : a * s ∈ I, { rw zero_mul at hc, let this : (a * ↑s - ↑b) * ↑c ∈ I := hc.symm ▸ I.zero_mem, cases hI.mem_or_mem this with h1 h2, { simpa using I.add_mem h1 b.2 }, { exfalso, refine hM ⟨c.2, h2⟩ } }, cases hI.mem_or_mem this with h1 h2, { exact h1 }, { exfalso, refine hM ⟨s.2, h2⟩ } end /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def order_embedding : ideal S ↪o ideal R := { to_fun := λ J, ideal.comap (algebra_map R S) J, inj' := function.left_inverse.injective (map_comap M S), map_rel_iff' := λ J₁ J₂, ⟨λ hJ, (map_comap M S) J₁ ▸ (map_comap M S) J₂ ▸ ideal.map_mono hJ, ideal.comap_mono⟩ } /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its comap, see `le_rel_iso_of_prime` for the more general relation isomorphism -/ lemma is_prime_iff_is_prime_disjoint (J : ideal S) : J.is_prime ↔ (ideal.comap (algebra_map R S) J).is_prime ∧ disjoint (M : set R) ↑(ideal.comap (algebra_map R S) J) := begin split, { refine λ h, ⟨⟨_, _⟩, λ m hm, h.ne_top (ideal.eq_top_of_is_unit_mem _ hm.2 (map_units S ⟨m, hm.left⟩))⟩, { refine λ hJ, h.ne_top _, rw [eq_top_iff, ← (order_embedding M S).le_iff_le], exact le_of_eq hJ.symm }, { intros x y hxy, rw [ideal.mem_comap, ring_hom.map_mul] at hxy, exact h.mem_or_mem hxy } }, { refine λ h, ⟨λ hJ, h.left.ne_top (eq_top_iff.2 _), _⟩, { rwa [eq_top_iff, ← (order_embedding M S).le_iff_le] at hJ }, { intros x y hxy, obtain ⟨a, s, ha⟩ := mk'_surjective M x, obtain ⟨b, t, hb⟩ := mk'_surjective M y, have : mk' S (a * b) (s * t) ∈ J := by rwa [mk'_mul, ha, hb], rw [mk'_mem_iff, ← ideal.mem_comap] at this, replace this := h.left.mem_or_mem this, rw [ideal.mem_comap, ideal.mem_comap] at this, rwa [← ha, ← hb, mk'_mem_iff, mk'_mem_iff] } } end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its map, see `le_rel_iso_of_prime` for the more general relation isomorphism, and the reverse implication -/ lemma is_prime_of_is_prime_disjoint (I : ideal R) (hp : I.is_prime) (hd : disjoint (M : set R) ↑I) : (ideal.map (algebra_map R S) I).is_prime := begin rw [is_prime_iff_is_prime_disjoint M S, comap_map_of_is_prime_disjoint M S I hp hd], exact ⟨hp, hd⟩ end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M` -/ def order_iso_of_prime : {p : ideal S // p.is_prime} ≃o {p : ideal R // p.is_prime ∧ disjoint (M : set R) ↑p} := { to_fun := λ p, ⟨ideal.comap (algebra_map R S) p.1, (is_prime_iff_is_prime_disjoint M S p.1).1 p.2⟩, inv_fun := λ p, ⟨ideal.map (algebra_map R S) p.1, is_prime_of_is_prime_disjoint M S p.1 p.2.1 p.2.2⟩, left_inv := λ J, subtype.eq (map_comap M S J), right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint M S I.1 I.2.1 I.2.2), map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val, from (map_comap M S I.val) ▸ (map_comap M S I'.val) ▸ (ideal.map_mono h)), λ h x hx, h hx⟩ } /-- `quotient_map` applied to maximal ideals of a localization is `surjective`. The quotient by a maximal ideal is a field, so inverses to elements already exist, and the localization necessarily maps the equivalence class of the inverse in the localization -/ lemma surjective_quotient_map_of_maximal_of_localization {I : ideal S} [I.is_prime] {J : ideal R} {H : J ≤ I.comap (algebra_map R S)} (hI : (I.comap (algebra_map R S)).is_maximal) : function.surjective (I.quotient_map (algebra_map R S) H) := begin intro s, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective s, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := mk'_surjective M s, by_cases hM : (ideal.quotient.mk (I.comap (algebra_map R S))) m = 0, { have : I = ⊤, { rw ideal.eq_top_iff_one, rw [ideal.quotient.eq_zero_iff_mem, ideal.mem_comap] at hM, convert I.mul_mem_right (mk' S 1 ⟨m, hm⟩) hM, rw [← mk'_eq_mul_mk'_one, mk'_self] }, exact ⟨0, eq_comm.1 (by simp [ideal.quotient.eq_zero_iff_mem, this])⟩ }, { rw ideal.quotient.maximal_ideal_iff_is_field_quotient at hI, obtain ⟨n, hn⟩ := hI.3 hM, obtain ⟨rn, rfl⟩ := ideal.quotient.mk_surjective n, refine ⟨(ideal.quotient.mk J) (r * rn), _⟩, -- The rest of the proof is essentially just algebraic manipulations to prove the equality rw ← ring_hom.map_mul at hn, replace hn := congr_arg (ideal.quotient_map I (algebra_map R S) le_rfl) hn, simp only [ring_hom.map_one, ideal.quotient_map_mk, ring_hom.map_mul] at hn, rw [ideal.quotient_map_mk, ← sub_eq_zero, ← ring_hom.map_sub, ideal.quotient.eq_zero_iff_mem, ← ideal.quotient.eq_zero_iff_mem, ring_hom.map_sub, sub_eq_zero, mk'_eq_mul_mk'_one], simp only [mul_eq_mul_left_iff, ring_hom.map_mul], exact or.inl (mul_left_cancel' (λ hn, hM (ideal.quotient.eq_zero_iff_mem.2 (ideal.mem_comap.2 (ideal.quotient.eq_zero_iff_mem.1 hn)))) (trans hn (by rw [← ring_hom.map_mul, ← mk'_eq_mul_mk'_one, mk'_self, ring_hom.map_one]))) } end end ideals variables (S) /-- Map from ideals of `R` to submodules of `S` induced by `f`. -/ -- This was previously a `has_coe` instance, but if `S = R` then this will loop. -- It could be a `has_coe_t` instance, but we keep it explicit here to avoid slowing down -- the rest of the library. def coe_submodule (I : ideal R) : submodule R S := submodule.map (algebra.linear_map R S) I lemma mem_coe_submodule (I : ideal R) {x : S} : x ∈ coe_submodule S I ↔ ∃ y : R, y ∈ I ∧ algebra_map R S y = x := iff.rfl lemma coe_submodule_mono {I J : ideal R} (h : I ≤ J) : coe_submodule S I ≤ coe_submodule S J := submodule.map_mono h @[simp] lemma coe_submodule_top : coe_submodule S (⊤ : ideal R) = 1 := by rw [coe_submodule, submodule.map_top, submodule.one_eq_range] lemma coe_submodule_fg (hS : function.injective (algebra_map R S)) (I : ideal R) : submodule.fg (coe_submodule S I) ↔ submodule.fg I := ⟨submodule.fg_of_fg_map _ (linear_map.ker_eq_bot.mpr hS), submodule.fg_map⟩ variables {g : R →+* P} variables {T : submonoid P} (hy : M ≤ T.comap g) {Q : Type*} [comm_ring Q] variables [algebra P Q] [is_localization T Q] lemma map_smul (x : S) (z : R) : map Q g hy (z • x : S) = g z • map Q g hy x := by rw [algebra.smul_def, algebra.smul_def, ring_hom.map_mul, map_eq] section include M lemma is_noetherian_ring (h : is_noetherian_ring R) : is_noetherian_ring S := begin rw [is_noetherian_ring_iff, is_noetherian_iff_well_founded] at h ⊢, exact order_embedding.well_founded ((is_localization.order_embedding M S).dual) h end end section integer_normalization open polynomial open_locale classical variables (M) {S} /-- `coeff_integer_normalization p` gives the coefficients of the polynomial `integer_normalization p` -/ noncomputable def coeff_integer_normalization (p : polynomial S) (i : ℕ) : R := if hi : i ∈ p.support then classical.some (classical.some_spec (exist_integer_multiples_of_finset M (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) else 0 lemma coeff_integer_normalization_of_not_mem_support (p : polynomial S) (i : ℕ) (h : coeff p i = 0) : coeff_integer_normalization M p i = 0 := by simp only [coeff_integer_normalization, h, mem_support_iff, eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff] lemma coeff_integer_normalization_mem_support (p : polynomial S) (i : ℕ) (h : coeff_integer_normalization M p i ≠ 0) : i ∈ p.support := begin contrapose h, rw [ne.def, not_not, coeff_integer_normalization, dif_neg h] end /-- `integer_normalization g` normalizes `g` to have integer coefficients by clearing the denominators -/ noncomputable def integer_normalization (p : polynomial S) : polynomial R := ∑ i in p.support, monomial i (coeff_integer_normalization M p i) @[simp] lemma integer_normalization_coeff (p : polynomial S) (i : ℕ) : (integer_normalization M p).coeff i = coeff_integer_normalization M p i := by simp [integer_normalization, coeff_monomial, coeff_integer_normalization_of_not_mem_support] {contextual := tt} lemma integer_normalization_spec (p : polynomial S) : ∃ (b : M), ∀ i, algebra_map R S ((integer_normalization M p).coeff i) = (b : R) • p.coeff i := begin use classical.some (exist_integer_multiples_of_finset M (p.support.image p.coeff)), intro i, rw [integer_normalization_coeff, coeff_integer_normalization], split_ifs with hi, { exact classical.some_spec (classical.some_spec (exist_integer_multiples_of_finset M (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) }, { convert (smul_zero _).symm, { apply ring_hom.map_zero }, { exact not_mem_support_iff.mp hi } } end lemma integer_normalization_map_to_map (p : polynomial S) : ∃ (b : M), (integer_normalization M p).map (algebra_map R S) = (b : R) • p := let ⟨b, hb⟩ := integer_normalization_spec M p in ⟨b, polynomial.ext (λ i, by { rw [coeff_map, coeff_smul], exact hb i })⟩ variables {R' : Type*} [comm_ring R'] lemma integer_normalization_eval₂_eq_zero (g : S →+* R') (p : polynomial S) {x : R'} (hx : eval₂ g x p = 0) : eval₂ (g.comp (algebra_map R S)) x (integer_normalization M p) = 0 := let ⟨b, hb⟩ := integer_normalization_map_to_map M p in trans (eval₂_map (algebra_map R S) g x).symm (by rw [hb, ← is_scalar_tower.algebra_map_smul S (b : R) p, eval₂_smul, hx, mul_zero]) lemma integer_normalization_aeval_eq_zero [algebra R R'] [algebra S R'] [is_scalar_tower R S R'] (p : polynomial S) {x : R'} (hx : aeval x p = 0) : aeval x (integer_normalization M p) = 0 := by rw [aeval_def, is_scalar_tower.algebra_map_eq R S R', integer_normalization_eval₂_eq_zero _ _ _ hx] end integer_normalization variables {R M} (S) {A K : Type*} [integral_domain A] lemma to_map_eq_zero_iff {x : R} (hM : M ≤ non_zero_divisors R) : algebra_map R S x = 0 ↔ x = 0 := begin rw ← (algebra_map R S).map_zero, split; intro h, { cases (eq_iff_exists M S).mp h with c hc, rw zero_mul at hc, exact hM c.2 x hc }, { rw h }, end protected lemma injective (hM : M ≤ non_zero_divisors R) : injective (algebra_map R S) := begin rw ring_hom.injective_iff (algebra_map R S), intros a ha, rwa to_map_eq_zero_iff S hM at ha end protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] (hM : M ≤ non_zero_divisors R) {x : R} (hx : x ∈ non_zero_divisors R) : algebra_map R S x ≠ 0 := map_ne_zero_of_mem_non_zero_divisors (is_localization.injective S hM) hx variables (S Q M) /-- Injectivity of a map descends to the map induced on localizations. -/ lemma map_injective_of_injective (hg : function.injective g) [is_localization (M.map g : submonoid P) Q] (hM : (M.map g : submonoid P) ≤ non_zero_divisors P) : function.injective (map Q g M.le_comap_map : S → Q) := begin rintros x y hxy, obtain ⟨a, b, rfl⟩ := mk'_surjective M x, obtain ⟨c, d, rfl⟩ := mk'_surjective M y, rw [map_mk' _ a b, map_mk' _ c d, mk'_eq_iff_eq] at hxy, refine mk'_eq_iff_eq.2 (congr_arg (algebra_map _ _) (hg _)), convert is_localization.injective _ hM hxy; simp, end variables {S Q M} @[mono] lemma coe_submodule_le_coe_submodule (h : M ≤ non_zero_divisors R) {I J : ideal R} : coe_submodule S I ≤ coe_submodule S J ↔ I ≤ J := submodule.map_le_map_iff_of_injective (is_localization.injective _ h) _ _ @[mono] lemma coe_submodule_strict_mono (h : M ≤ non_zero_divisors R) : strict_mono (coe_submodule S : ideal R → submodule R S) := strict_mono_of_le_iff_le (λ _ _, (coe_submodule_le_coe_submodule h).symm) variables (S) {Q M} /-- A `comm_ring` `S` which is the localization of an integral domain `R` at a subset of non-zero elements is an integral domain. -/ def integral_domain_of_le_non_zero_divisors [algebra A S] {M : submonoid A} [is_localization M S] (hM : M ≤ non_zero_divisors A) : integral_domain S := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros z w h, cases surj M z with x hx, cases surj M w with y hy, have : z * w * algebra_map A S y.2 * algebra_map A S x.2 = algebra_map A S x.1 * algebra_map A S y.1, by rw [mul_assoc z, hy, ←hx]; ac_refl, rw [h, zero_mul, zero_mul, ← (algebra_map A S).map_mul] at this, cases eq_zero_or_eq_zero_of_mul_eq_zero ((to_map_eq_zero_iff S hM).mp this.symm) with H H, { exact or.inl (eq_zero_of_fst_eq_zero hx H) }, { exact or.inr (eq_zero_of_fst_eq_zero hy H) }, end, exists_pair_ne := ⟨(algebra_map A S) 0, (algebra_map A S) 1, λ h, zero_ne_one (is_localization.injective S hM h)⟩, .. ‹comm_ring S› } /-- The localization at of an integral domain to a set of non-zero elements is an integral domain -/ def integral_domain_localization {M : submonoid A} (hM : M ≤ non_zero_divisors A) : integral_domain (localization M) := integral_domain_of_le_non_zero_divisors _ hM /-- The localization of an integral domain at the complement of a prime ideal is an integral domain. -/ instance integral_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) : integral_domain (localization.at_prime P) := integral_domain_localization (le_non_zero_divisors_of_domain (by simpa only [] using P.zero_mem)) namespace at_prime variables (I : ideal R) [hI : I.is_prime] [is_localization.at_prime S I] include hI lemma is_unit_to_map_iff (x : R) : is_unit ((algebra_map R S) x) ↔ x ∈ I.prime_compl := ⟨λ h hx, (is_prime_of_is_prime_disjoint I.prime_compl S I hI disjoint_compl_left).ne_top $ (ideal.map (algebra_map R S) I).eq_top_of_is_unit_mem (ideal.mem_map_of_mem _ hx) h, λ h, map_units S ⟨x, h⟩⟩ -- Can't use typeclasses to infer the `local_ring` instance, so use an `opt_param` instead -- (since `local_ring` is a `Prop`, there should be no unification issues.) lemma to_map_mem_maximal_iff (x : R) (h : _root_.local_ring S := local_ring S I) : algebra_map R S x ∈ local_ring.maximal_ideal S ↔ x ∈ I := not_iff_not.mp $ by simpa only [@local_ring.mem_maximal_ideal S, mem_nonunits_iff, not_not] using is_unit_to_map_iff S I x lemma is_unit_mk'_iff (x : R) (y : I.prime_compl) : is_unit (mk' S x y) ↔ x ∈ I.prime_compl := ⟨λ h hx, mk'_mem_iff.mpr ((to_map_mem_maximal_iff S I x).mpr hx) h, λ h, is_unit_iff_exists_inv.mpr ⟨mk' S ↑y ⟨x, h⟩, mk'_mul_mk'_eq_one ⟨x, h⟩ y⟩⟩ lemma mk'_mem_maximal_iff (x : R) (y : I.prime_compl) (h : _root_.local_ring S := local_ring S I) : mk' S x y ∈ local_ring.maximal_ideal S ↔ x ∈ I := not_iff_not.mp $ by simpa only [@local_ring.mem_maximal_ideal S, mem_nonunits_iff, not_not] using is_unit_mk'_iff S I x y end at_prime end is_localization namespace localization open is_localization local attribute [instance] classical.prop_decidable variables (I : ideal R) [hI : I.is_prime] include hI variables {I} /-- The unique maximal ideal of the localization at `I.prime_compl` lies over the ideal `I`. -/ lemma at_prime.comap_maximal_ideal : ideal.comap (algebra_map R (localization.at_prime I)) (local_ring.maximal_ideal (localization I.prime_compl)) = I := ideal.ext $ λ x, by simpa only [ideal.mem_comap] using at_prime.to_map_mem_maximal_iff _ I x /-- The image of `I` in the localization at `I.prime_compl` is a maximal ideal, and in particular it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/ lemma at_prime.map_eq_maximal_ideal : ideal.map (algebra_map R (localization.at_prime I)) I = (local_ring.maximal_ideal (localization I.prime_compl)) := begin convert congr_arg (ideal.map _) at_prime.comap_maximal_ideal.symm, rw map_comap I.prime_compl end lemma le_comap_prime_compl_iff {J : ideal P} [hJ : J.is_prime] {f : R →+* P} : I.prime_compl ≤ J.prime_compl.comap f ↔ J.comap f ≤ I := ⟨λ h x hx, by { contrapose! hx, exact h hx }, λ h x hx hfxJ, hx (h hfxJ)⟩ variables (I) /-- For a ring hom `f : R →+* S` and a prime ideal `J` in `S`, the induced ring hom from the localization of `R` at `J.comap f` to the localization of `S` at `J`. To make this definition more flexible, we allow any ideal `I` of `R` as input, together with a proof that `I = J.comap f`. This can be useful when `I` is not definitionally equal to `J.comap f`. -/ noncomputable def local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R →+* P) (hIJ : I = J.comap f) : localization.at_prime I →+* localization.at_prime J := is_localization.map (localization.at_prime J) f (le_comap_prime_compl_iff.mpr (ge_of_eq hIJ)) lemma local_ring_hom_to_map (J : ideal P) [hJ : J.is_prime] (f : R →+* P) (hIJ : I = J.comap f) (x : R) : local_ring_hom I J f hIJ (algebra_map _ _ x) = algebra_map _ _ (f x) := map_eq _ _ lemma local_ring_hom_mk' (J : ideal P) [hJ : J.is_prime] (f : R →+* P) (hIJ : I = J.comap f) (x : R) (y : I.prime_compl) : local_ring_hom I J f hIJ (is_localization.mk' _ x y) = is_localization.mk' (localization.at_prime J) (f x) (⟨f y, le_comap_prime_compl_iff.mpr (ge_of_eq hIJ) y.2⟩ : J.prime_compl) := map_mk' _ _ _ instance is_local_ring_hom_local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R →+* P) (hIJ : I = J.comap f) : is_local_ring_hom (local_ring_hom I J f hIJ) := is_local_ring_hom.mk $ λ x hx, begin rcases is_localization.mk'_surjective I.prime_compl x with ⟨r, s, rfl⟩, rw local_ring_hom_mk' at hx, rw at_prime.is_unit_mk'_iff at hx ⊢, exact λ hr, hx ((set_like.ext_iff.mp hIJ r).mp hr), end lemma local_ring_hom_unique (J : ideal P) [hJ : J.is_prime] (f : R →+* P) (hIJ : I = J.comap f) {j : localization.at_prime I →+* localization.at_prime J} (hj : ∀ x : R, j (algebra_map _ _ x) = algebra_map _ _ (f x)) : local_ring_hom I J f hIJ = j := map_unique _ _ hj @[simp] lemma local_ring_hom_id : local_ring_hom I I (ring_hom.id R) (ideal.comap_id I).symm = ring_hom.id _ := local_ring_hom_unique _ _ _ _ (λ x, rfl) @[simp] lemma local_ring_hom_comp {S : Type*} [comm_ring S] (J : ideal S) [hJ : J.is_prime] (K : ideal P) [hK : K.is_prime] (f : R →+* S) (hIJ : I = J.comap f) (g : S →+* P) (hJK : J = K.comap g) : local_ring_hom I K (g.comp f) (by rw [hIJ, hJK, ideal.comap_comap f g]) = (local_ring_hom J K g hJK).comp (local_ring_hom I J f hIJ) := local_ring_hom_unique _ _ _ _ (λ r, by simp only [function.comp_app, ring_hom.coe_comp, local_ring_hom_to_map]) end localization open is_localization /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ lemma localization_map_bijective_of_field {R Rₘ : Type*} [integral_domain R] [comm_ring Rₘ] {M : submonoid R} (hM : (0 : R) ∉ M) (hR : is_field R) [algebra R Rₘ] [is_localization M Rₘ] : function.bijective (algebra_map R Rₘ) := begin refine ⟨is_localization.injective _ (le_non_zero_divisors_of_domain hM), λ x, _⟩, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := mk'_surjective M x, obtain ⟨n, hn⟩ := hR.mul_inv_cancel (λ hm0, hM (hm0 ▸ hm) : m ≠ 0), exact ⟨r * n, by erw [eq_mk'_iff_mul_eq, ← ring_hom.map_mul, mul_assoc, mul_comm n, hn, mul_one]⟩ end variables (R) {A : Type*} [integral_domain A] variables (K : Type*) /-- `is_fraction_ring R K` states `K` is the field of fractions of an integral domain `R`. -/ -- TODO: should this extend `algebra` instead of assuming it? abbreviation is_fraction_ring [comm_ring K] [algebra R K] := is_localization (non_zero_divisors R) K /-- The cast from `int` to `rat` as a `fraction_ring`. -/ instance rat.is_fraction_ring : is_fraction_ring ℤ ℚ := { map_units := begin rintro ⟨x, hx⟩, rw mem_non_zero_divisors_iff_ne_zero at hx, simpa only [ring_hom.eq_int_cast, is_unit_iff_ne_zero, int.cast_eq_zero, ne.def, subtype.coe_mk] using hx, end, surj := begin rintro ⟨n, d, hd, h⟩, refine ⟨⟨n, ⟨d, _⟩⟩, rat.mul_denom_eq_num⟩, rwa [mem_non_zero_divisors_iff_ne_zero, int.coe_nat_ne_zero_iff_pos] end, eq_iff_exists := begin intros x y, rw [ring_hom.eq_int_cast, ring_hom.eq_int_cast, int.cast_inj], refine ⟨by { rintro rfl, use 1 }, _⟩, rintro ⟨⟨c, hc⟩, h⟩, apply int.eq_of_mul_eq_mul_right _ h, rwa mem_non_zero_divisors_iff_ne_zero at hc, end } namespace is_fraction_ring variables {R K} section comm_ring variables [comm_ring K] [algebra R K] [is_fraction_ring R K] [algebra A K] [is_fraction_ring A K] lemma to_map_eq_zero_iff {x : R} : algebra_map R K x = 0 ↔ x = 0 := to_map_eq_zero_iff _ (le_of_eq rfl) variables (R K) protected theorem injective : function.injective (algebra_map R K) := is_localization.injective _ (le_of_eq rfl) variables {R K} @[simp, mono] lemma coe_submodule_le_coe_submodule {I J : ideal R} : coe_submodule K I ≤ coe_submodule K J ↔ I ≤ J := is_localization.coe_submodule_le_coe_submodule (le_refl _) @[mono] lemma coe_submodule_strict_mono : strict_mono (coe_submodule K : ideal R → submodule R K) := strict_mono_of_le_iff_le (λ _ _, coe_submodule_le_coe_submodule.symm) protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] {x : R} (hx : x ∈ non_zero_divisors R) : algebra_map R K x ≠ 0 := is_localization.to_map_ne_zero_of_mem_non_zero_divisors _ (le_refl _) hx variables (A) /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ def to_integral_domain : integral_domain K := integral_domain_of_le_non_zero_divisors K (le_refl (non_zero_divisors A)) local attribute [instance] classical.dec_eq /-- The inverse of an element in the field of fractions of an integral domain. -/ protected noncomputable def inv (z : K) : K := if h : z = 0 then 0 else mk' K ↑(sec (non_zero_divisors A) z).2 ⟨(sec _ z).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, h $ eq_zero_of_fst_eq_zero (sec_spec (non_zero_divisors A) z) h0⟩ protected lemma mul_inv_cancel (x : K) (hx : x ≠ 0) : x * is_fraction_ring.inv A x = 1 := show x * dite _ _ _ = 1, by rw [dif_neg hx, ←is_unit.mul_left_inj (map_units K ⟨(sec _ x).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, hx $ eq_zero_of_fst_eq_zero (sec_spec (non_zero_divisors A) x) h0⟩), one_mul, mul_assoc, mk'_spec, ←eq_mk'_iff_mul_eq]; exact (mk'_sec _ x).symm /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. -/ noncomputable def to_field : field K := { inv := is_fraction_ring.inv A, mul_inv_cancel := is_fraction_ring.mul_inv_cancel A, inv_zero := dif_pos rfl, .. to_integral_domain A } end comm_ring variables {B : Type*} [integral_domain B] [field K] {L : Type*} [field L] [algebra A K] [is_fraction_ring A K] {g : A →+* L} lemma mk'_mk_eq_div {r s} (hs : s ∈ non_zero_divisors A) : mk' K r ⟨s, hs⟩ = algebra_map A K r / algebra_map A K s := mk'_eq_iff_eq_mul.2 $ (div_mul_cancel (algebra_map A K r) (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors hs)).symm lemma mk'_eq_div {r} (s : non_zero_divisors A) : mk' K r s = algebra_map A K r / algebra_map A K s := mk'_mk_eq_div s.2 lemma is_unit_map_of_injective (hg : function.injective g) (y : non_zero_divisors A) : is_unit (g y) := is_unit.mk0 (g y) $ map_ne_zero_of_mem_non_zero_divisors hg y.2 /-- Given an integral domain `A` with field of fractions `K`, and an injective ring hom `g : A →+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift (hg : injective g) : K →+* L := lift $ λ (y : non_zero_divisors A), is_unit_map_of_injective hg y /-- Given an integral domain `A` with field of fractions `K`, and an injective ring hom `g : A →+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ @[simp] lemma lift_mk' (hg : injective g) (x) (y : non_zero_divisors A) : lift hg (mk' K x y) = g x / g y := begin erw lift_mk' (is_unit_map_of_injective hg), erw submonoid.localization_map.mul_inv_left (λ y : non_zero_divisors A, show is_unit (g.to_monoid_hom y), from is_unit_map_of_injective hg y), exact (mul_div_cancel' _ (map_ne_zero_of_mem_non_zero_divisors hg y.2)).symm, end /-- Given integral domains `A, B` with fields of fractions `K`, `L` and an injective ring hom `j : A →+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map [algebra B L] [is_fraction_ring B L] {j : A →+* B} (hj : injective j) : K →+* L := map L j (show non_zero_divisors A ≤ (non_zero_divisors B).comap j, from λ y hy, map_mem_non_zero_divisors hj hy) /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ noncomputable def field_equiv_of_ring_equiv [algebra B L] [is_fraction_ring B L] (h : A ≃+* B) : K ≃+* L := ring_equiv_of_ring_equiv K L h begin ext b, show b ∈ h.to_equiv '' _ ↔ _, erw [h.to_equiv.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero], exact h.symm.map_ne_zero_iff end lemma integer_normalization_eq_zero_iff {p : polynomial K} : integer_normalization (non_zero_divisors A) p = 0 ↔ p = 0 := begin refine (polynomial.ext_iff.trans (polynomial.ext_iff.trans _).symm), obtain ⟨⟨b, nonzero⟩, hb⟩ := integer_normalization_spec _ p, split; intros h i, { apply to_map_eq_zero_iff.mp, rw [hb i, h i], apply smul_zero, assumption }, { have hi := h i, rw [polynomial.coeff_zero, ← @to_map_eq_zero_iff A _ K, hb i, algebra.smul_def] at hi, apply or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hi), intro h, apply mem_non_zero_divisors_iff_ne_zero.mp nonzero, exact to_map_eq_zero_iff.mp h } end /-- A field is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ lemma comap_is_algebraic_iff [algebra A L] [algebra K L] [is_scalar_tower A K L] : algebra.is_algebraic A L ↔ algebra.is_algebraic K L := begin split; intros h x; obtain ⟨p, hp, px⟩ := h x, { refine ⟨p.map (algebra_map A K), λ h, hp (polynomial.ext (λ i, _)), _⟩, { have : algebra_map A K (p.coeff i) = 0 := trans (polynomial.coeff_map _ _).symm (by simp [h]), exact to_map_eq_zero_iff.mp this }, { rwa is_scalar_tower.aeval_apply _ K at px } }, { exact ⟨integer_normalization _ p, mt integer_normalization_eq_zero_iff.mp hp, integer_normalization_aeval_eq_zero _ p px⟩ }, end section num_denom variables (A) [unique_factorization_monoid A] lemma exists_reduced_fraction (x : K) : ∃ (a : A) (b : non_zero_divisors A), (∀ {d}, d ∣ a → d ∣ b → is_unit d) ∧ mk' K a b = x := begin obtain ⟨⟨b, b_nonzero⟩, a, hab⟩ := exists_integer_multiple (non_zero_divisors A) x, obtain ⟨a', b', c', no_factor, rfl, rfl⟩ := unique_factorization_monoid.exists_reduced_factors' a b (mem_non_zero_divisors_iff_ne_zero.mp b_nonzero), obtain ⟨c'_nonzero, b'_nonzero⟩ := mul_mem_non_zero_divisors.mp b_nonzero, refine ⟨a', ⟨b', b'_nonzero⟩, @no_factor, _⟩, refine mul_left_cancel' (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors b_nonzero) _, simp only [subtype.coe_mk, ring_hom.map_mul, algebra.smul_def] at *, erw [←hab, mul_assoc, mk'_spec' _ a' ⟨b', b'_nonzero⟩], end /-- `f.num x` is the numerator of `x : f.codomain` as a reduced fraction. -/ noncomputable def num (x : K) : A := classical.some (exists_reduced_fraction A x) /-- `f.num x` is the denominator of `x : f.codomain` as a reduced fraction. -/ noncomputable def denom (x : K) : non_zero_divisors A := classical.some (classical.some_spec (exists_reduced_fraction A x)) lemma num_denom_reduced (x : K) : ∀ {d}, d ∣ num A x → d ∣ denom A x → is_unit d := (classical.some_spec (classical.some_spec (exists_reduced_fraction A x))).1 @[simp] lemma mk'_num_denom (x : K) : mk' K (num A x) (denom A x) = x := (classical.some_spec (classical.some_spec (exists_reduced_fraction A x))).2 variables {A} lemma num_mul_denom_eq_num_iff_eq {x y : K} : x * algebra_map A K (denom A y) = algebra_map A K (num A y) ↔ x = y := ⟨λ h, by simpa only [mk'_num_denom] using eq_mk'_iff_mul_eq.mpr h, λ h, eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom])⟩ lemma num_mul_denom_eq_num_iff_eq' {x y : K} : y * algebra_map A K (denom A x) = algebra_map A K (num A x) ↔ x = y := ⟨λ h, by simpa only [eq_comm, mk'_num_denom] using eq_mk'_iff_mul_eq.mpr h, λ h, eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom])⟩ lemma num_mul_denom_eq_num_mul_denom_iff_eq {x y : K} : num A y * denom A x = num A x * denom A y ↔ x = y := ⟨λ h, by simpa only [mk'_num_denom] using mk'_eq_of_eq h, λ h, by rw h⟩ lemma eq_zero_of_num_eq_zero {x : K} (h : num A x = 0) : x = 0 := num_mul_denom_eq_num_iff_eq'.mp (by rw [zero_mul, h, ring_hom.map_zero]) lemma is_integer_of_is_unit_denom {x : K} (h : is_unit (denom A x : A)) : is_integer A x := begin cases h with d hd, have d_ne_zero : algebra_map A K (denom A x) ≠ 0 := is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors (denom A x).2, use ↑d⁻¹ * num A x, refine trans _ (mk'_num_denom A x), rw [ring_hom.map_mul, ring_hom.map_units_inv, hd], apply mul_left_cancel' d_ne_zero, rw [←mul_assoc, mul_inv_cancel d_ne_zero, one_mul, mk'_spec'] end lemma is_unit_denom_of_num_eq_zero {x : K} (h : num A x = 0) : is_unit (denom A x : A) := num_denom_reduced A x (h.symm ▸ dvd_zero _) (dvd_refl _) end num_denom end is_fraction_ring section algebra section is_integral variables {R S} {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] variables [algebra R Rₘ] [is_localization M Rₘ] variables [algebra S Sₘ] [is_localization (algebra.algebra_map_submonoid S M) Sₘ] section variables (S M) /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebra_map R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps -/ noncomputable def localization_algebra : algebra Rₘ Sₘ := (map Sₘ (algebra_map R S) (show _ ≤ (algebra.algebra_map_submonoid S M).comap _, from M.le_comap_map) : Rₘ →+* Sₘ).to_algebra end lemma algebra_map_mk' (r : R) (m : M) : (@algebra_map Rₘ Sₘ _ _ (localization_algebra M S)) (mk' Rₘ r m) = mk' Sₘ (algebra_map R S r) ⟨algebra_map R S m, algebra.mem_algebra_map_submonoid_of_mem m⟩ := map_mk' _ _ _ variables (Rₘ Sₘ) /-- Injectivity of the underlying `algebra_map` descends to the algebra induced by localization. -/ lemma localization_algebra_injective (hRS : function.injective (algebra_map R S)) (hM : algebra.algebra_map_submonoid S M ≤ non_zero_divisors S) : function.injective (@algebra_map Rₘ Sₘ _ _ (localization_algebra M S)) := is_localization.map_injective_of_injective M Rₘ Sₘ hRS hM variables {Rₘ Sₘ} open polynomial lemma ring_hom.is_integral_elem_localization_at_leading_coeff {R S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S) (x : S) (p : polynomial R) (hf : p.eval₂ f x = 0) (M : submonoid R) (hM : p.leading_coeff ∈ M) {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R Rₘ] [is_localization M Rₘ] [algebra S Sₘ] [is_localization (M.map f : submonoid S) Sₘ] : (map Sₘ f M.le_comap_map : Rₘ →+* _).is_integral_elem (algebra_map S Sₘ x) := begin by_cases triv : (1 : Rₘ) = 0, { exact ⟨0, ⟨trans leading_coeff_zero triv.symm, eval₂_zero _ _⟩⟩ }, haveI : nontrivial Rₘ := nontrivial_of_ne 1 0 triv, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.mp (map_units Rₘ ⟨p.leading_coeff, hM⟩), refine ⟨(p.map (algebra_map R Rₘ)) * C b, ⟨_, _⟩⟩, { refine monic_mul_C_of_leading_coeff_mul_eq_one _, rwa leading_coeff_map_of_leading_coeff_ne_zero (algebra_map R Rₘ), refine λ hfp, zero_ne_one (trans (zero_mul b).symm (hfp ▸ hb) : (0 : Rₘ) = 1) }, { refine eval₂_mul_eq_zero_of_left _ _ _ _, erw [eval₂_map, is_localization.map_comp, ← hom_eval₂ _ f (algebra_map S Sₘ) x], exact trans (congr_arg (algebra_map S Sₘ) hf) (ring_hom.map_zero _) } end /-- Given a particular witness to an element being algebraic over an algebra `R → S`, We can localize to a submonoid containing the leading coefficient to make it integral. Explicitly, the map between the localizations will be an integral ring morphism -/ theorem is_integral_localization_at_leading_coeff {x : S} (p : polynomial R) (hp : aeval x p = 0) (hM : p.leading_coeff ∈ M) : (map Sₘ (algebra_map R S) (show _ ≤ (algebra.algebra_map_submonoid S M).comap _, from M.le_comap_map) : Rₘ →+* _).is_integral_elem (algebra_map S Sₘ x) := (algebra_map R S).is_integral_elem_localization_at_leading_coeff x p hp M hM /-- If `R → S` is an integral extension, `M` is a submonoid of `R`, `Rₘ` is the localization of `R` at `M`, and `Sₘ` is the localization of `S` at the image of `M` under the extension map, then the induced map `Rₘ → Sₘ` is also an integral extension -/ theorem is_integral_localization (H : algebra.is_integral R S) : (map Sₘ (algebra_map R S) (show _ ≤ (algebra.algebra_map_submonoid S M).comap _, from M.le_comap_map) : Rₘ →+* _).is_integral := begin intro x, by_cases triv : (1 : R) = 0, { have : (1 : Rₘ) = 0 := by convert congr_arg (algebra_map R Rₘ) triv; simp, exact ⟨0, ⟨trans leading_coeff_zero this.symm, eval₂_zero _ _⟩⟩ }, { haveI : nontrivial R := nontrivial_of_ne 1 0 triv, obtain ⟨⟨s, ⟨u, hu⟩⟩, hx⟩ := surj (algebra.algebra_map_submonoid S M) x, obtain ⟨v, hv⟩ := hu, obtain ⟨v', hv'⟩ := is_unit_iff_exists_inv'.1 (map_units Rₘ ⟨v, hv.1⟩), refine @is_integral_of_is_integral_mul_unit Rₘ _ _ _ (localization_algebra M S) x (algebra_map S Sₘ u) v' _ _, { replace hv' := congr_arg (@algebra_map Rₘ Sₘ _ _ (localization_algebra M S)) hv', rw [ring_hom.map_mul, ring_hom.map_one, ← ring_hom.comp_apply _ (algebra_map R Rₘ)] at hv', erw is_localization.map_comp at hv', exact hv.2 ▸ hv' }, { obtain ⟨p, hp⟩ := H s, exact hx.symm ▸ is_integral_localization_at_leading_coeff p hp.2 (hp.1.symm ▸ M.one_mem) } } end lemma is_integral_localization' {R S : Type*} [comm_ring R] [comm_ring S] {f : R →+* S} (hf : f.is_integral) (M : submonoid R) : (map (localization (M.map (f : R →* S))) f M.le_comap_map : localization M →+* _).is_integral := @is_integral_localization R _ M S _ f.to_algebra _ _ _ _ _ _ _ _ hf end is_integral namespace integral_closure variables {L : Type*} [field K] [field L] [algebra A K] [is_fraction_ring A K] open algebra /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ lemma is_fraction_ring_of_algebraic [algebra A L] (alg : is_algebraic A L) (inj : ∀ x, algebra_map A L x = 0 → x = 0) : is_fraction_ring (integral_closure A L) L := ⟨(λ ⟨⟨y, integral⟩, nonzero⟩, have y ≠ 0 := λ h, mem_non_zero_divisors_iff_ne_zero.mp nonzero (subtype.ext_iff_val.mpr h), show is_unit y, from ⟨⟨y, y⁻¹, mul_inv_cancel this, inv_mul_cancel this⟩, rfl⟩), (λ z, let ⟨x, y, hy, hxy⟩ := exists_integral_multiple (alg z) inj in ⟨⟨x, ⟨y, mem_non_zero_divisors_iff_ne_zero.mpr hy⟩⟩, hxy⟩), (λ x y, ⟨λ (h : x.1 = y.1), ⟨1, by simpa using subtype.ext_iff_val.mpr h⟩, λ ⟨c, hc⟩, congr_arg (algebra_map _ L) (mul_right_cancel' (mem_non_zero_divisors_iff_ne_zero.mp c.2) hc)⟩)⟩ variables (K L) /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ lemma is_fraction_ring_of_finite_extension [algebra A L] [algebra K L] [is_scalar_tower A K L] [finite_dimensional K L] : is_fraction_ring (integral_closure A L) L := is_fraction_ring_of_algebraic (is_fraction_ring.comap_is_algebraic_iff.mpr (is_algebraic_of_finite : is_algebraic K L)) (λ x hx, is_fraction_ring.to_map_eq_zero_iff.mp ((algebra_map K L).map_eq_zero.mp $ (is_scalar_tower.algebra_map_apply _ _ _ _).symm.trans hx)) end integral_closure end algebra variables (A) /-- The fraction field of an integral domain as a quotient type. -/ @[reducible] def fraction_ring := localization (non_zero_divisors A) namespace fraction_ring variables {A} noncomputable instance : field (fraction_ring A) := is_fraction_ring.to_field A @[simp] lemma mk_eq_div {r s} : (localization.mk r s : fraction_ring A) = (algebra_map _ _ r / algebra_map A _ s : fraction_ring A) := by rw [localization.mk_eq_mk', is_fraction_ring.mk'_eq_div] variables (A) /-- Given an integral domain `A` and a localization map to a field of fractions `f : A →+* K`, we get an `A`-isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ noncomputable def alg_equiv (K : Type*) [field K] [algebra A K] [is_fraction_ring A K] : fraction_ring A ≃ₐ[A] K := localization.alg_equiv (non_zero_divisors A) K end fraction_ring
6231a4c4586beaf2afce879a70f18bec88b492c6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/infinite_sum.lean
8e9bccace25ac1360ac6293d7d94b4d37bd97a48
[ "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
70,443
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.big_operators.intervals import algebra.big_operators.nat_antidiagonal import logic.encodable.lattice import topology.algebra.mul_action import topology.algebra.order.monotone_convergence import topology.instances.real /-! # Infinite sum over a topological monoid This sum is known as unconditionally convergent, as it sums to the same value under all possible permutations. For Euclidean spaces (finite dimensional Banach spaces) this is equivalent to absolute convergence. Note: There are summable sequences which are not unconditionally convergent! The other way holds generally, see `has_sum.tendsto_sum_nat`. ## References * Bourbaki: General Topology (1995), Chapter 3 §5 (Infinite sums in commutative groups) -/ noncomputable theory open finset filter function classical open_locale topological_space classical big_operators nnreal variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section has_sum variables [add_comm_monoid α] [topological_space α] /-- Infinite sum on a topological monoid The `at_top` filter on `finset β` is the limit of all finite sets towards the entire type. So we sum up bigger and bigger sets. This sum operation is invariant under reordering. In particular, the function `ℕ → ℝ` sending `n` to `(-1)^n / (n+1)` does not have a sum for this definition, but a series which is absolutely convergent will have the correct sum. This is based on Mario Carneiro's [infinite sum `df-tsms` in Metamath](http://us.metamath.org/mpeuni/df-tsms.html). For the definition or many statements, `α` does not need to be a topological monoid. We only add this assumption later, for the lemmas where it is relevant. -/ def has_sum (f : β → α) (a : α) : Prop := tendsto (λs:finset β, ∑ b in s, f b) at_top (𝓝 a) /-- `summable f` means that `f` has some (infinite) sum. Use `tsum` to get the value. -/ def summable (f : β → α) : Prop := ∃a, has_sum f a /-- `∑' i, f i` is the sum of `f` it exists, or 0 otherwise -/ @[irreducible] def tsum {β} (f : β → α) := if h : summable f then classical.some h else 0 -- see Note [operator precedence of big operators] notation `∑'` binders `, ` r:(scoped:67 f, tsum f) := r variables {f g : β → α} {a b : α} {s : finset β} lemma summable.has_sum (ha : summable f) : has_sum f (∑'b, f b) := by simp [ha, tsum]; exact some_spec ha lemma has_sum.summable (h : has_sum f a) : summable f := ⟨a, h⟩ /-- Constant zero function has sum `0` -/ lemma has_sum_zero : has_sum (λb, 0 : β → α) 0 := by simp [has_sum, tendsto_const_nhds] lemma has_sum_empty [is_empty β] : has_sum f 0 := by convert has_sum_zero lemma summable_zero : summable (λb, 0 : β → α) := has_sum_zero.summable lemma summable_empty [is_empty β] : summable f := has_sum_empty.summable lemma tsum_eq_zero_of_not_summable (h : ¬ summable f) : ∑'b, f b = 0 := by simp [tsum, h] lemma summable_congr (hfg : ∀b, f b = g b) : summable f ↔ summable g := iff_of_eq (congr_arg summable $ funext hfg) lemma summable.congr (hf : summable f) (hfg : ∀b, f b = g b) : summable g := (summable_congr hfg).mp hf lemma has_sum.has_sum_of_sum_eq {g : γ → α} (h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b) (hf : has_sum g a) : has_sum f a := le_trans (map_at_top_finset_sum_le_of_sum_eq h_eq) hf lemma has_sum_iff_has_sum {g : γ → α} (h₁ : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b) (h₂ : ∀v:finset β, ∃u:finset γ, ∀u', u ⊆ u' → ∃v', v ⊆ v' ∧ ∑ b in v', f b = ∑ x in u', g x) : has_sum f a ↔ has_sum g a := ⟨has_sum.has_sum_of_sum_eq h₂, has_sum.has_sum_of_sum_eq h₁⟩ lemma function.injective.has_sum_iff {g : γ → β} (hg : injective g) (hf : ∀ x ∉ set.range g, f x = 0) : has_sum (f ∘ g) a ↔ has_sum f a := by simp only [has_sum, tendsto, hg.map_at_top_finset_sum_eq hf] lemma function.injective.summable_iff {g : γ → β} (hg : injective g) (hf : ∀ x ∉ set.range g, f x = 0) : summable (f ∘ g) ↔ summable f := exists_congr $ λ _, hg.has_sum_iff hf lemma has_sum_subtype_iff_of_support_subset {s : set β} (hf : support f ⊆ s) : has_sum (f ∘ coe : s → α) a ↔ has_sum f a := subtype.coe_injective.has_sum_iff $ by simpa using support_subset_iff'.1 hf lemma has_sum_subtype_iff_indicator {s : set β} : has_sum (f ∘ coe : s → α) a ↔ has_sum (s.indicator f) a := by rw [← set.indicator_range_comp, subtype.range_coe, has_sum_subtype_iff_of_support_subset set.support_indicator_subset] lemma summable_subtype_iff_indicator {s : set β} : summable (f ∘ coe : s → α) ↔ summable (s.indicator f) := exists_congr (λ _, has_sum_subtype_iff_indicator) @[simp] lemma has_sum_subtype_support : has_sum (f ∘ coe : support f → α) a ↔ has_sum f a := has_sum_subtype_iff_of_support_subset $ set.subset.refl _ lemma has_sum_fintype [fintype β] (f : β → α) : has_sum f (∑ b, f b) := order_top.tendsto_at_top_nhds _ protected lemma finset.has_sum (s : finset β) (f : β → α) : has_sum (f ∘ coe : (↑s : set β) → α) (∑ b in s, f b) := by { rw ← sum_attach, exact has_sum_fintype _ } protected lemma finset.summable (s : finset β) (f : β → α) : summable (f ∘ coe : (↑s : set β) → α) := (s.has_sum f).summable protected lemma set.finite.summable {s : set β} (hs : s.finite) (f : β → α) : summable (f ∘ coe : s → α) := by convert hs.to_finset.summable f; simp only [hs.coe_to_finset] /-- If a function `f` vanishes outside of a finite set `s`, then it `has_sum` `∑ b in s, f b`. -/ lemma has_sum_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : has_sum f (∑ b in s, f b) := (has_sum_subtype_iff_of_support_subset $ support_subset_iff'.2 hf).1 $ s.has_sum f lemma summable_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : summable f := (has_sum_sum_of_ne_finset_zero hf).summable lemma has_sum_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : has_sum f (f b) := suffices has_sum f (∑ b' in {b}, f b'), by simpa using this, has_sum_sum_of_ne_finset_zero $ by simpa [hf] lemma has_sum_ite_eq (b : β) [decidable_pred (= b)] (a : α) : has_sum (λb', if b' = b then a else 0) a := begin convert has_sum_single b _, { exact (if_pos rfl).symm }, assume b' hb', exact if_neg hb' end lemma equiv.has_sum_iff (e : γ ≃ β) : has_sum (f ∘ e) a ↔ has_sum f a := e.injective.has_sum_iff $ by simp lemma function.injective.has_sum_range_iff {g : γ → β} (hg : injective g) : has_sum (λ x : set.range g, f x) a ↔ has_sum (f ∘ g) a := (equiv.of_injective g hg).has_sum_iff.symm lemma equiv.summable_iff (e : γ ≃ β) : summable (f ∘ e) ↔ summable f := exists_congr $ λ a, e.has_sum_iff lemma summable.prod_symm {f : β × γ → α} (hf : summable f) : summable (λ p : γ × β, f p.swap) := (equiv.prod_comm γ β).summable_iff.2 hf lemma equiv.has_sum_iff_of_support {g : γ → α} (e : support f ≃ support g) (he : ∀ x : support f, g (e x) = f x) : has_sum f a ↔ has_sum g a := have (g ∘ coe) ∘ e = f ∘ coe, from funext he, by rw [← has_sum_subtype_support, ← this, e.has_sum_iff, has_sum_subtype_support] lemma has_sum_iff_has_sum_of_ne_zero_bij {g : γ → α} (i : support g → β) (hi : ∀ ⦃x y⦄, i x = i y → (x : γ) = y) (hf : support f ⊆ set.range i) (hfg : ∀ x, f (i x) = g x) : has_sum f a ↔ has_sum g a := iff.symm $ equiv.has_sum_iff_of_support (equiv.of_bijective (λ x, ⟨i x, λ hx, x.coe_prop $ hfg x ▸ hx⟩) ⟨λ x y h, subtype.ext $ hi $ subtype.ext_iff.1 h, λ y, (hf y.coe_prop).imp $ λ x hx, subtype.ext hx⟩) hfg lemma equiv.summable_iff_of_support {g : γ → α} (e : support f ≃ support g) (he : ∀ x : support f, g (e x) = f x) : summable f ↔ summable g := exists_congr $ λ _, e.has_sum_iff_of_support he protected lemma has_sum.map [add_comm_monoid γ] [topological_space γ] (hf : has_sum f a) {G} [add_monoid_hom_class G α γ] (g : G) (hg : continuous g) : has_sum (g ∘ f) (g a) := have g ∘ (λs:finset β, ∑ b in s, f b) = (λs:finset β, ∑ b in s, g (f b)), from funext $ map_sum g _, show tendsto (λs:finset β, ∑ b in s, g (f b)) at_top (𝓝 (g a)), from this ▸ (hg.tendsto a).comp hf protected lemma summable.map [add_comm_monoid γ] [topological_space γ] (hf : summable f) {G} [add_monoid_hom_class G α γ] (g : G) (hg : continuous g) : summable (g ∘ f) := (hf.has_sum.map g hg).summable protected lemma summable.map_iff_of_left_inverse [add_comm_monoid γ] [topological_space γ] {G G'} [add_monoid_hom_class G α γ] [add_monoid_hom_class G' γ α] (g : G) (g' : G') (hg : continuous g) (hg' : continuous g') (hinv : function.left_inverse g' g) : summable (g ∘ f) ↔ summable f := ⟨λ h, begin have := h.map _ hg', rwa [←function.comp.assoc, hinv.id] at this, end, λ h, h.map _ hg⟩ /-- A special case of `summable.map_iff_of_left_inverse` for convenience -/ protected lemma summable.map_iff_of_equiv [add_comm_monoid γ] [topological_space γ] {G} [add_equiv_class G α γ] (g : G) (hg : continuous g) (hg' : continuous (add_equiv_class.inv g : γ → α)) : summable (g ∘ f) ↔ summable f := summable.map_iff_of_left_inverse g (g : α ≃+ γ).symm hg hg' (add_equiv_class.left_inv g) /-- If `f : ℕ → α` has sum `a`, then the partial sums `∑_{i=0}^{n-1} f i` converge to `a`. -/ lemma has_sum.tendsto_sum_nat {f : ℕ → α} (h : has_sum f a) : tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := h.comp tendsto_finset_range lemma has_sum.unique {a₁ a₂ : α} [t2_space α] : has_sum f a₁ → has_sum f a₂ → a₁ = a₂ := tendsto_nhds_unique lemma summable.has_sum_iff_tendsto_nat [t2_space α] {f : ℕ → α} {a : α} (hf : summable f) : has_sum f a ↔ tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := begin refine ⟨λ h, h.tendsto_sum_nat, λ h, _⟩, rw tendsto_nhds_unique h hf.has_sum.tendsto_sum_nat, exact hf.has_sum end lemma function.surjective.summable_iff_of_has_sum_iff {α' : Type*} [add_comm_monoid α'] [topological_space α'] {e : α' → α} (hes : function.surjective e) {f : β → α} {g : γ → α'} (he : ∀ {a}, has_sum f (e a) ↔ has_sum g a) : summable f ↔ summable g := hes.exists.trans $ exists_congr $ @he section mul_opposite open mul_opposite lemma has_sum.op (hf : has_sum f a) : has_sum (λ a, op (f a)) (op a) := (hf.map (@op_add_equiv α _) continuous_op : _) lemma summable.op (hf : summable f) : summable (op ∘ f) := hf.has_sum.op.summable lemma has_sum.unop {f : β → αᵐᵒᵖ} {a : αᵐᵒᵖ} (hf : has_sum f a) : has_sum (λ a, unop (f a)) (unop a) := (hf.map (@op_add_equiv α _).symm continuous_unop : _) lemma summable.unop {f : β → αᵐᵒᵖ} (hf : summable f) : summable (unop ∘ f) := hf.has_sum.unop.summable @[simp] lemma has_sum_op : has_sum (λ a, op (f a)) (op a) ↔ has_sum f a := ⟨has_sum.unop, has_sum.op⟩ @[simp] lemma has_sum_unop {f : β → αᵐᵒᵖ} {a : αᵐᵒᵖ} : has_sum (λ a, unop (f a)) (unop a) ↔ has_sum f a := ⟨has_sum.op, has_sum.unop⟩ @[simp] lemma summable_op : summable (λ a, op (f a)) ↔ summable f := ⟨summable.unop, summable.op⟩ @[simp] lemma summable_unop {f : β → αᵐᵒᵖ} : summable (λ a, unop (f a)) ↔ summable f := ⟨summable.op, summable.unop⟩ end mul_opposite section has_continuous_star variables [star_add_monoid α] [has_continuous_star α] lemma has_sum.star (h : has_sum f a) : has_sum (λ b, star (f b)) (star a) := by simpa only using h.map (star_add_equiv : α ≃+ α) continuous_star lemma summable.star (hf : summable f) : summable (λ b, star (f b)) := hf.has_sum.star.summable lemma summable.of_star (hf : summable (λ b, star (f b))) : summable f := by simpa only [star_star] using hf.star @[simp] lemma summable_star_iff : summable (λ b, star (f b)) ↔ summable f := ⟨summable.of_star, summable.star⟩ @[simp] lemma summable_star_iff' : summable (star f) ↔ summable f := summable_star_iff end has_continuous_star variable [has_continuous_add α] lemma has_sum.add (hf : has_sum f a) (hg : has_sum g b) : has_sum (λb, f b + g b) (a + b) := by simp only [has_sum, sum_add_distrib]; exact hf.add hg lemma summable.add (hf : summable f) (hg : summable g) : summable (λb, f b + g b) := (hf.has_sum.add hg.has_sum).summable lemma has_sum_sum {f : γ → β → α} {a : γ → α} {s : finset γ} : (∀i∈s, has_sum (f i) (a i)) → has_sum (λb, ∑ i in s, f i b) (∑ i in s, a i) := finset.induction_on s (by simp only [has_sum_zero, sum_empty, forall_true_iff]) (by simp only [has_sum.add, sum_insert, mem_insert, forall_eq_or_imp, forall_2_true_iff, not_false_iff, forall_true_iff] {contextual := tt}) lemma summable_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) : summable (λb, ∑ i in s, f i b) := (has_sum_sum $ assume i hi, (hf i hi).has_sum).summable lemma has_sum.add_disjoint {s t : set β} (hs : disjoint s t) (ha : has_sum (f ∘ coe : s → α) a) (hb : has_sum (f ∘ coe : t → α) b) : has_sum (f ∘ coe : s ∪ t → α) (a + b) := begin rw has_sum_subtype_iff_indicator at *, rw set.indicator_union_of_disjoint hs, exact ha.add hb end lemma has_sum.add_is_compl {s t : set β} (hs : is_compl s t) (ha : has_sum (f ∘ coe : s → α) a) (hb : has_sum (f ∘ coe : t → α) b) : has_sum f (a + b) := by simpa [← hs.compl_eq] using (has_sum_subtype_iff_indicator.1 ha).add (has_sum_subtype_iff_indicator.1 hb) lemma has_sum.add_compl {s : set β} (ha : has_sum (f ∘ coe : s → α) a) (hb : has_sum (f ∘ coe : sᶜ → α) b) : has_sum f (a + b) := ha.add_is_compl is_compl_compl hb lemma summable.add_compl {s : set β} (hs : summable (f ∘ coe : s → α)) (hsc : summable (f ∘ coe : sᶜ → α)) : summable f := (hs.has_sum.add_compl hsc.has_sum).summable lemma has_sum.compl_add {s : set β} (ha : has_sum (f ∘ coe : sᶜ → α) a) (hb : has_sum (f ∘ coe : s → α) b) : has_sum f (a + b) := ha.add_is_compl is_compl_compl.symm hb lemma has_sum.even_add_odd {f : ℕ → α} (he : has_sum (λ k, f (2 * k)) a) (ho : has_sum (λ k, f (2 * k + 1)) b) : has_sum f (a + b) := begin have := mul_right_injective₀ (two_ne_zero' ℕ), replace he := this.has_sum_range_iff.2 he, replace ho := ((add_left_injective 1).comp this).has_sum_range_iff.2 ho, refine he.add_is_compl _ ho, simpa [(∘)] using nat.is_compl_even_odd end lemma summable.compl_add {s : set β} (hs : summable (f ∘ coe : sᶜ → α)) (hsc : summable (f ∘ coe : s → α)) : summable f := (hs.has_sum.compl_add hsc.has_sum).summable lemma summable.even_add_odd {f : ℕ → α} (he : summable (λ k, f (2 * k))) (ho : summable (λ k, f (2 * k + 1))) : summable f := (he.has_sum.even_add_odd ho.has_sum).summable lemma has_sum.sigma [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (ha : has_sum f a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) : has_sum g a := begin refine (at_top_basis.tendsto_iff (closed_nhds_basis a)).mpr _, rintros s ⟨hs, hsc⟩, rcases mem_at_top_sets.mp (ha hs) with ⟨u, hu⟩, use [u.image sigma.fst, trivial], intros bs hbs, simp only [set.mem_preimage, ge_iff_le, finset.le_iff_subset] at hu, have : tendsto (λ t : finset (Σ b, γ b), ∑ p in t.filter (λ p, p.1 ∈ bs), f p) at_top (𝓝 $ ∑ b in bs, g b), { simp only [← sigma_preimage_mk, sum_sigma], refine tendsto_finset_sum _ (λ b hb, _), change tendsto (λ t, (λ t, ∑ s in t, f ⟨b, s⟩) (preimage t (sigma.mk b) _)) at_top (𝓝 (g b)), exact tendsto.comp (hf b) (tendsto_finset_preimage_at_top_at_top _) }, refine hsc.mem_of_tendsto this (eventually_at_top.2 ⟨u, λ t ht, hu _ (λ x hx, _)⟩), exact mem_filter.2 ⟨ht hx, hbs $ mem_image_of_mem _ hx⟩ end /-- If a series `f` on `β × γ` has sum `a` and for each `b` the restriction of `f` to `{b} × γ` has sum `g b`, then the series `g` has sum `a`. -/ lemma has_sum.prod_fiberwise [regular_space α] {f : β × γ → α} {g : β → α} {a : α} (ha : has_sum f a) (hf : ∀b, has_sum (λc, f (b, c)) (g b)) : has_sum g a := has_sum.sigma ((equiv.sigma_equiv_prod β γ).has_sum_iff.2 ha) hf lemma summable.sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) (hf : ∀b, summable (λc, f ⟨b, c⟩)) : summable (λb, ∑'c, f ⟨b, c⟩) := (ha.has_sum.sigma (assume b, (hf b).has_sum)).summable lemma has_sum.sigma_of_has_sum [t3_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (ha : has_sum g a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) (hf' : summable f) : has_sum f a := by simpa [(hf'.has_sum.sigma hf).unique ha] using hf'.has_sum end has_sum section tsum variables [add_comm_monoid α] [topological_space α] lemma tsum_congr_subtype (f : β → α) {s t : set β} (h : s = t) : ∑' (x : s), f x = ∑' (x : t), f x := by rw h lemma tsum_zero' (hz : is_closed ({0} : set α)) : ∑' b : β, (0 : α) = 0 := begin classical, rw [tsum, dif_pos summable_zero], suffices : ∀ (x : α), has_sum (λ (b : β), (0 : α)) x → x = 0, { exact this _ (classical.some_spec _) }, intros x hx, contrapose! hx, simp only [has_sum, tendsto_nhds, finset.sum_const_zero, filter.mem_at_top_sets, ge_iff_le, finset.le_eq_subset, set.mem_preimage, not_forall, not_exists, exists_prop, exists_and_distrib_right], refine ⟨{0}ᶜ, ⟨is_open_compl_iff.mpr hz, _⟩, λ y, ⟨⟨y, subset_refl _⟩, _⟩⟩, { simpa using hx }, { simp } end @[simp] lemma tsum_zero [t1_space α] : ∑' b : β, (0 : α) = 0 := tsum_zero' is_closed_singleton variables [t2_space α] {f g : β → α} {a a₁ a₂ : α} lemma has_sum.tsum_eq (ha : has_sum f a) : ∑'b, f b = a := (summable.has_sum ⟨a, ha⟩).unique ha lemma summable.has_sum_iff (h : summable f) : has_sum f a ↔ ∑'b, f b = a := iff.intro has_sum.tsum_eq (assume eq, eq ▸ h.has_sum) @[simp] lemma tsum_empty [is_empty β] : ∑'b, f b = 0 := has_sum_empty.tsum_eq lemma tsum_eq_sum {f : β → α} {s : finset β} (hf : ∀b∉s, f b = 0) : ∑' b, f b = ∑ b in s, f b := (has_sum_sum_of_ne_finset_zero hf).tsum_eq lemma tsum_congr {α β : Type*} [add_comm_monoid α] [topological_space α] {f g : β → α} (hfg : ∀ b, f b = g b) : ∑' b, f b = ∑' b, g b := congr_arg tsum (funext hfg) lemma tsum_fintype [fintype β] (f : β → α) : ∑'b, f b = ∑ b, f b := (has_sum_fintype f).tsum_eq lemma tsum_bool (f : bool → α) : ∑' i : bool, f i = f false + f true := by { rw [tsum_fintype, finset.sum_eq_add]; simp } lemma tsum_eq_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : ∑'b, f b = f b := (has_sum_single b hf).tsum_eq lemma tsum_tsum_eq_single (f : β → γ → α) (b : β) (c : γ) (hfb : ∀ b' ≠ b, f b' c = 0) (hfc : ∀ (b' : β) (c' : γ), c' ≠ c → f b' c' = 0) : ∑' b' c', f b' c' = f b c := calc ∑' b' c', f b' c' = ∑' b', f b' c : tsum_congr $ λ b', tsum_eq_single _ (hfc b') ... = f b c : tsum_eq_single _ hfb @[simp] lemma tsum_ite_eq (b : β) [decidable_pred (= b)] (a : α) : ∑' b', (if b' = b then a else 0) = a := (has_sum_ite_eq b a).tsum_eq lemma tsum_dite_right (P : Prop) [decidable P] (x : β → ¬ P → α) : ∑' (b : β), (if h : P then (0 : α) else x b h) = if h : P then (0 : α) else ∑' (b : β), x b h := by by_cases hP : P; simp [hP] lemma tsum_dite_left (P : Prop) [decidable P] (x : β → P → α) : ∑' (b : β), (if h : P then x b h else 0) = if h : P then (∑' (b : β), x b h) else 0 := by by_cases hP : P; simp [hP] lemma function.surjective.tsum_eq_tsum_of_has_sum_iff_has_sum {α' : Type*} [add_comm_monoid α'] [topological_space α'] {e : α' → α} (hes : function.surjective e) (h0 : e 0 = 0) {f : β → α} {g : γ → α'} (h : ∀ {a}, has_sum f (e a) ↔ has_sum g a) : ∑' b, f b = e (∑' c, g c) := by_cases (assume : summable g, (h.mpr this.has_sum).tsum_eq) (assume hg : ¬ summable g, have hf : ¬ summable f, from mt (hes.summable_iff_of_has_sum_iff @h).1 hg, by simp [tsum, hf, hg, h0]) lemma tsum_eq_tsum_of_has_sum_iff_has_sum {f : β → α} {g : γ → α} (h : ∀{a}, has_sum f a ↔ has_sum g a) : ∑'b, f b = ∑'c, g c := surjective_id.tsum_eq_tsum_of_has_sum_iff_has_sum rfl @h lemma equiv.tsum_eq (j : γ ≃ β) (f : β → α) : ∑'c, f (j c) = ∑'b, f b := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ a, j.has_sum_iff lemma equiv.tsum_eq_tsum_of_support {f : β → α} {g : γ → α} (e : support f ≃ support g) (he : ∀ x, g (e x) = f x) : (∑' x, f x) = ∑' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, e.has_sum_iff_of_support he lemma tsum_eq_tsum_of_ne_zero_bij {g : γ → α} (i : support g → β) (hi : ∀ ⦃x y⦄, i x = i y → (x : γ) = y) (hf : support f ⊆ set.range i) (hfg : ∀ x, f (i x) = g x) : ∑' x, f x = ∑' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, has_sum_iff_has_sum_of_ne_zero_bij i hi hf hfg lemma tsum_op : ∑' x, mul_opposite.op (f x) = mul_opposite.op (∑' x, f x) := begin by_cases h : summable f, { exact h.has_sum.op.tsum_eq, }, { have ho := summable_op.not.mpr h, rw [tsum_eq_zero_of_not_summable h, tsum_eq_zero_of_not_summable ho, mul_opposite.op_zero] }, end lemma tsum_unop {f : β → αᵐᵒᵖ} : ∑' x, mul_opposite.unop (f x) = mul_opposite.unop (∑' x, f x) := mul_opposite.op_injective tsum_op.symm /-! ### `tsum` on subsets -/ @[simp] lemma finset.tsum_subtype (s : finset β) (f : β → α) : ∑' x : {x // x ∈ s}, f x = ∑ x in s, f x := (s.has_sum f).tsum_eq @[simp] lemma finset.tsum_subtype' (s : finset β) (f : β → α) : ∑' x : (s : set β), f x = ∑ x in s, f x := s.tsum_subtype f lemma tsum_subtype (s : set β) (f : β → α) : ∑' x : s, f x = ∑' x, s.indicator f x := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, has_sum_subtype_iff_indicator lemma tsum_subtype_eq_of_support_subset {f : β → α} {s : set β} (hs : support f ⊆ s) : ∑' x : s, f x = ∑' x, f x := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ x, has_sum_subtype_iff_of_support_subset hs @[simp] lemma tsum_univ (f : β → α) : ∑' x : (set.univ : set β), f x = ∑' x, f x := tsum_subtype_eq_of_support_subset $ set.subset_univ _ @[simp] lemma tsum_singleton (b : β) (f : β → α) : ∑' x : ({b} : set β), f x = f b := begin rw [tsum_subtype, tsum_eq_single b], { simp }, { intros b' hb', rw set.indicator_of_not_mem, rwa set.mem_singleton_iff }, { apply_instance } end lemma tsum_image {g : γ → β} (f : β → α) {s : set γ} (hg : set.inj_on g s) : ∑' x : g '' s, f x = ∑' x : s, f (g x) := ((equiv.set.image_of_inj_on _ _ hg).tsum_eq (λ x, f x)).symm lemma tsum_range {g : γ → β} (f : β → α) (hg : injective g) : ∑' x : set.range g, f x = ∑' x, f (g x) := by rw [← set.image_univ, tsum_image f (hg.inj_on _), tsum_univ (f ∘ g)] section has_continuous_add variable [has_continuous_add α] lemma tsum_add (hf : summable f) (hg : summable g) : ∑'b, (f b + g b) = (∑'b, f b) + (∑'b, g b) := (hf.has_sum.add hg.has_sum).tsum_eq lemma tsum_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) : ∑'b, ∑ i in s, f i b = ∑ i in s, ∑'b, f i b := (has_sum_sum $ assume i hi, (hf i hi).has_sum).tsum_eq variables [add_comm_monoid δ] [topological_space δ] [t3_space δ] [has_continuous_add δ] lemma tsum_sigma' {γ : β → Type*} {f : (Σb:β, γ b) → δ} (h₁ : ∀b, summable (λc, f ⟨b, c⟩)) (h₂ : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := (h₂.has_sum.sigma (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_prod' {f : β × γ → δ} (h : summable f) (h₁ : ∀b, summable (λc, f (b, c))) : ∑'p, f p = ∑'b c, f (b, c) := (h.has_sum.prod_fiberwise (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_comm' {f : β → γ → δ} (h : summable (function.uncurry f)) (h₁ : ∀b, summable (f b)) (h₂ : ∀ c, summable (λ b, f b c)) : ∑' c b, f b c = ∑' b c, f b c := begin erw [← tsum_prod' h h₁, ← tsum_prod' h.prod_symm h₂, ← (equiv.prod_comm γ β).tsum_eq (uncurry f)], refl end end has_continuous_add section has_continuous_star variables [star_add_monoid α] [has_continuous_star α] lemma tsum_star : star (∑' b, f b) = ∑' b, star (f b) := begin by_cases hf : summable f, { exact hf.has_sum.star.tsum_eq.symm, }, { rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt summable.of_star hf), star_zero] }, end end has_continuous_star open encodable section encodable variable [encodable γ] /-- You can compute a sum over an encodably type by summing over the natural numbers and taking a supremum. This is useful for outer measures. -/ theorem tsum_supr_decode₂ [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (s : γ → β) : ∑' i : ℕ, m (⨆ b ∈ decode₂ γ i, s b) = ∑' b : γ, m (s b) := begin have H : ∀ n, m (⨆ b ∈ decode₂ γ n, s b) ≠ 0 → (decode₂ γ n).is_some, { intros n h, cases decode₂ γ n with b, { refine (h $ by simp [m0]).elim }, { exact rfl } }, symmetry, refine tsum_eq_tsum_of_ne_zero_bij (λ a, option.get (H a.1 a.2)) _ _ _, { rintros ⟨m, hm⟩ ⟨n, hn⟩ e, have := mem_decode₂.1 (option.get_mem (H n hn)), rwa [← e, mem_decode₂.1 (option.get_mem (H m hm))] at this }, { intros b h, refine ⟨⟨encode b, _⟩, _⟩, { simp only [mem_support, encodek₂] at h ⊢, convert h, simp [set.ext_iff, encodek₂] }, { exact option.get_of_mem _ (encodek₂ _) } }, { rintros ⟨n, h⟩, dsimp only [subtype.coe_mk], transitivity, swap, rw [show decode₂ γ n = _, from option.get_mem (H n h)], congr, simp [ext_iff, -option.some_get] } end /-- `tsum_supr_decode₂` specialized to the complete lattice of sets. -/ theorem tsum_Union_decode₂ (m : set β → α) (m0 : m ∅ = 0) (s : γ → set β) : ∑' i, m (⋃ b ∈ decode₂ γ i, s b) = ∑' b, m (s b) := tsum_supr_decode₂ m m0 s end encodable /-! Some properties about measure-like functions. These could also be functions defined on complete sublattices of sets, with the property that they are countably sub-additive. `R` will probably be instantiated with `(≤)` in all applications. -/ section countable variables [countable γ] /-- If a function is countably sub-additive then it is sub-additive on countable types -/ theorem rel_supr_tsum [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) ∑' i, m (s i)) (s : γ → β) : R (m (⨆ b : γ, s b)) ∑' b : γ, m (s b) := by { casesI nonempty_encodable γ, rw [←supr_decode₂, ←tsum_supr_decode₂ _ m0 s], exact m_supr _ } /-- If a function is countably sub-additive then it is sub-additive on finite sets -/ theorem rel_supr_sum [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) (∑' i, m (s i))) (s : δ → β) (t : finset δ) : R (m (⨆ d ∈ t, s d)) (∑ d in t, m (s d)) := by { rw [supr_subtype', ←finset.tsum_subtype], exact rel_supr_tsum m m0 R m_supr _ } /-- If a function is countably sub-additive then it is binary sub-additive -/ theorem rel_sup_add [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) (∑' i, m (s i))) (s₁ s₂ : β) : R (m (s₁ ⊔ s₂)) (m s₁ + m s₂) := begin convert rel_supr_tsum m m0 R m_supr (λ b, cond b s₁ s₂), { simp only [supr_bool_eq, cond] }, { rw [tsum_fintype, fintype.sum_bool, cond, cond] } end end countable variables [has_continuous_add α] lemma tsum_add_tsum_compl {s : set β} (hs : summable (f ∘ coe : s → α)) (hsc : summable (f ∘ coe : sᶜ → α)) : (∑' x : s, f x) + (∑' x : sᶜ, f x) = ∑' x, f x := (hs.has_sum.add_compl hsc.has_sum).tsum_eq.symm lemma tsum_union_disjoint {s t : set β} (hd : disjoint s t) (hs : summable (f ∘ coe : s → α)) (ht : summable (f ∘ coe : t → α)) : (∑' x : s ∪ t, f x) = (∑' x : s, f x) + (∑' x : t, f x) := (hs.has_sum.add_disjoint hd ht.has_sum).tsum_eq lemma tsum_even_add_odd {f : ℕ → α} (he : summable (λ k, f (2 * k))) (ho : summable (λ k, f (2 * k + 1))) : (∑' k, f (2 * k)) + (∑' k, f (2 * k + 1)) = ∑' k, f k := (he.has_sum.even_add_odd ho.has_sum).tsum_eq.symm end tsum section prod variables [add_comm_monoid α] [topological_space α] [add_comm_monoid γ] [topological_space γ] lemma has_sum.prod_mk {f : β → α} {g : β → γ} {a : α} {b : γ} (hf : has_sum f a) (hg : has_sum g b) : has_sum (λ x, (⟨f x, g x⟩ : α × γ)) ⟨a, b⟩ := by simp [has_sum, ← prod_mk_sum, filter.tendsto.prod_mk_nhds hf hg] end prod section pi variables {ι : Type*} {π : α → Type*} [∀ x, add_comm_monoid (π x)] [∀ x, topological_space (π x)] lemma pi.has_sum {f : ι → ∀ x, π x} {g : ∀ x, π x} : has_sum f g ↔ ∀ x, has_sum (λ i, f i x) (g x) := by simp only [has_sum, tendsto_pi_nhds, sum_apply] lemma pi.summable {f : ι → ∀ x, π x} : summable f ↔ ∀ x, summable (λ i, f i x) := by simp only [summable, pi.has_sum, skolem] lemma tsum_apply [∀ x, t2_space (π x)] {f : ι → ∀ x, π x}{x : α} (hf : summable f) : (∑' i, f i) x = ∑' i, f i x := (pi.has_sum.mp hf.has_sum x).tsum_eq.symm end pi section topological_group variables [add_comm_group α] [topological_space α] [topological_add_group α] variables {f g : β → α} {a a₁ a₂ : α} -- `by simpa using` speeds up elaboration. Why? lemma has_sum.neg (h : has_sum f a) : has_sum (λb, - f b) (- a) := by simpa only using h.map (-add_monoid_hom.id α) continuous_neg lemma summable.neg (hf : summable f) : summable (λb, - f b) := hf.has_sum.neg.summable lemma summable.of_neg (hf : summable (λb, - f b)) : summable f := by simpa only [neg_neg] using hf.neg lemma summable_neg_iff : summable (λ b, - f b) ↔ summable f := ⟨summable.of_neg, summable.neg⟩ lemma has_sum.sub (hf : has_sum f a₁) (hg : has_sum g a₂) : has_sum (λb, f b - g b) (a₁ - a₂) := by { simp only [sub_eq_add_neg], exact hf.add hg.neg } lemma summable.sub (hf : summable f) (hg : summable g) : summable (λb, f b - g b) := (hf.has_sum.sub hg.has_sum).summable lemma summable.trans_sub (hg : summable g) (hfg : summable (λb, f b - g b)) : summable f := by simpa only [sub_add_cancel] using hfg.add hg lemma summable_iff_of_summable_sub (hfg : summable (λb, f b - g b)) : summable f ↔ summable g := ⟨λ hf, hf.trans_sub $ by simpa only [neg_sub] using hfg.neg, λ hg, hg.trans_sub hfg⟩ lemma has_sum.update (hf : has_sum f a₁) (b : β) [decidable_eq β] (a : α) : has_sum (update f b a) (a - f b + a₁) := begin convert ((has_sum_ite_eq b _).add hf), ext b', by_cases h : b' = b, { rw [h, update_same], simp only [eq_self_iff_true, if_true, sub_add_cancel] }, simp only [h, update_noteq, if_false, ne.def, zero_add, not_false_iff], end lemma summable.update (hf : summable f) (b : β) [decidable_eq β] (a : α) : summable (update f b a) := (hf.has_sum.update b a).summable lemma has_sum.has_sum_compl_iff {s : set β} (hf : has_sum (f ∘ coe : s → α) a₁) : has_sum (f ∘ coe : sᶜ → α) a₂ ↔ has_sum f (a₁ + a₂) := begin refine ⟨λ h, hf.add_compl h, λ h, _⟩, rw [has_sum_subtype_iff_indicator] at hf ⊢, rw [set.indicator_compl], simpa only [add_sub_cancel'] using h.sub hf end lemma has_sum.has_sum_iff_compl {s : set β} (hf : has_sum (f ∘ coe : s → α) a₁) : has_sum f a₂ ↔ has_sum (f ∘ coe : sᶜ → α) (a₂ - a₁) := iff.symm $ hf.has_sum_compl_iff.trans $ by rw [add_sub_cancel'_right] lemma summable.summable_compl_iff {s : set β} (hf : summable (f ∘ coe : s → α)) : summable (f ∘ coe : sᶜ → α) ↔ summable f := ⟨λ ⟨a, ha⟩, (hf.has_sum.has_sum_compl_iff.1 ha).summable, λ ⟨a, ha⟩, (hf.has_sum.has_sum_iff_compl.1 ha).summable⟩ protected lemma finset.has_sum_compl_iff (s : finset β) : has_sum (λ x : {x // x ∉ s}, f x) a ↔ has_sum f (a + ∑ i in s, f i) := (s.has_sum f).has_sum_compl_iff.trans $ by rw [add_comm] protected lemma finset.has_sum_iff_compl (s : finset β) : has_sum f a ↔ has_sum (λ x : {x // x ∉ s}, f x) (a - ∑ i in s, f i) := (s.has_sum f).has_sum_iff_compl protected lemma finset.summable_compl_iff (s : finset β) : summable (λ x : {x // x ∉ s}, f x) ↔ summable f := (s.summable f).summable_compl_iff lemma set.finite.summable_compl_iff {s : set β} (hs : s.finite) : summable (f ∘ coe : sᶜ → α) ↔ summable f := (hs.summable f).summable_compl_iff lemma has_sum_ite_eq_extract [decidable_eq β] (hf : has_sum f a) (b : β) : has_sum (λ n, ite (n = b) 0 (f n)) (a - f b) := begin convert hf.update b 0 using 1, { ext n, rw function.update_apply, }, { rw [sub_add_eq_add_sub, zero_add], }, end section tsum variables [t2_space α] lemma tsum_neg : ∑'b, - f b = - ∑'b, f b := begin by_cases hf : summable f, { exact hf.has_sum.neg.tsum_eq, }, { simp [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt summable.of_neg hf)] }, end lemma tsum_sub (hf : summable f) (hg : summable g) : ∑'b, (f b - g b) = ∑'b, f b - ∑'b, g b := (hf.has_sum.sub hg.has_sum).tsum_eq lemma sum_add_tsum_compl {s : finset β} (hf : summable f) : (∑ x in s, f x) + (∑' x : (↑s : set β)ᶜ, f x) = ∑' x, f x := ((s.has_sum f).add_compl (s.summable_compl_iff.2 hf).has_sum).tsum_eq.symm /-- Let `f : β → α` be a sequence with summable series and let `b ∈ β` be an index. Lemma `tsum_ite_eq_extract` writes `Σ f n` as the sum of `f b` plus the series of the remaining terms. -/ lemma tsum_ite_eq_extract [decidable_eq β] (hf : summable f) (b : β) : ∑' n, f n = f b + ∑' n, ite (n = b) 0 (f n) := begin rw (has_sum_ite_eq_extract hf.has_sum b).tsum_eq, exact (add_sub_cancel'_right _ _).symm, end end tsum /-! ### Sums on subtypes If `s` is a finset of `α`, we show that the summability of `f` in the whole space and on the subtype `univ - s` are equivalent, and relate their sums. For a function defined on `ℕ`, we deduce the formula `(∑ i in range k, f i) + (∑' i, f (i + k)) = (∑' i, f i)`, in `sum_add_tsum_nat_add`. -/ section subtype lemma has_sum_nat_add_iff {f : ℕ → α} (k : ℕ) {a : α} : has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) := begin refine iff.trans _ ((range k).has_sum_compl_iff), rw [← (not_mem_range_equiv k).symm.has_sum_iff], refl end lemma summable_nat_add_iff {f : ℕ → α} (k : ℕ) : summable (λ n, f (n + k)) ↔ summable f := iff.symm $ (equiv.add_right (∑ i in range k, f i)).surjective.summable_iff_of_has_sum_iff $ λ a, (has_sum_nat_add_iff k).symm lemma has_sum_nat_add_iff' {f : ℕ → α} (k : ℕ) {a : α} : has_sum (λ n, f (n + k)) (a - ∑ i in range k, f i) ↔ has_sum f a := by simp [has_sum_nat_add_iff] lemma sum_add_tsum_nat_add [t2_space α] {f : ℕ → α} (k : ℕ) (h : summable f) : (∑ i in range k, f i) + (∑' i, f (i + k)) = ∑' i, f i := by simpa only [add_comm] using ((has_sum_nat_add_iff k).1 ((summable_nat_add_iff k).2 h).has_sum).unique h.has_sum lemma tsum_eq_zero_add [t2_space α] {f : ℕ → α} (hf : summable f) : ∑'b, f b = f 0 + ∑'b, f (b + 1) := by simpa only [sum_range_one] using (sum_add_tsum_nat_add 1 hf).symm /-- For `f : ℕ → α`, then `∑' k, f (k + i)` tends to zero. This does not require a summability assumption on `f`, as otherwise all sums are zero. -/ lemma tendsto_sum_nat_add [t2_space α] (f : ℕ → α) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin by_cases hf : summable f, { have h₀ : (λ i, (∑' i, f i) - ∑ j in range i, f j) = λ i, ∑' (k : ℕ), f (k + i), { ext1 i, rw [sub_eq_iff_eq_add, add_comm, sum_add_tsum_nat_add i hf] }, have h₁ : tendsto (λ i : ℕ, ∑' i, f i) at_top (𝓝 (∑' i, f i)) := tendsto_const_nhds, simpa only [h₀, sub_self] using tendsto.sub h₁ hf.has_sum.tendsto_sum_nat }, { convert tendsto_const_nhds, ext1 i, rw ← summable_nat_add_iff i at hf, { exact tsum_eq_zero_of_not_summable hf }, { apply_instance } } end /-- If `f₀, f₁, f₂, ...` and `g₀, g₁, g₂, ...` are both convergent then so is the `ℤ`-indexed sequence: `..., g₂, g₁, g₀, f₀, f₁, f₂, ...`. -/ lemma has_sum.int_rec {b : α} {f g : ℕ → α} (hf : has_sum f a) (hg : has_sum g b) : @has_sum α _ _ _ (@int.rec (λ _, α) f g : ℤ → α) (a + b) := begin -- note this proof works for any two-case inductive have h₁ : injective (coe : ℕ → ℤ) := @int.of_nat.inj, have h₂ : injective int.neg_succ_of_nat := @int.neg_succ_of_nat.inj, have : is_compl (set.range (coe : ℕ → ℤ)) (set.range int.neg_succ_of_nat), { split, { rw disjoint_iff_inf_le, rintros _ ⟨⟨i, rfl⟩, ⟨j, ⟨⟩⟩⟩ }, { rw codisjoint_iff_le_sup, rintros (i | j) h, exacts [or.inl ⟨_, rfl⟩, or.inr ⟨_, rfl⟩] } }, exact has_sum.add_is_compl this (h₁.has_sum_range_iff.mpr hf) (h₂.has_sum_range_iff.mpr hg), end lemma has_sum.nonneg_add_neg {b : α} {f : ℤ → α} (hnonneg : has_sum (λ n : ℕ, f n) a) (hneg : has_sum (λ (n : ℕ), f (-n.succ)) b) : has_sum f (a + b) := begin simp_rw ← int.neg_succ_of_nat_coe at hneg, convert hnonneg.int_rec hneg using 1, ext (i | j); refl, end lemma has_sum.pos_add_zero_add_neg {b : α} {f : ℤ → α} (hpos : has_sum (λ n:ℕ, f(n + 1)) a) (hneg : has_sum (λ (n : ℕ), f (-n.succ)) b) : has_sum f (a + f 0 + b) := begin have : ∀ g : ℕ → α, has_sum (λ k, g (k + 1)) a → has_sum g (a + g 0), { intros g hg, simpa using (has_sum_nat_add_iff _).mp hg }, exact (this (λ n, f n) hpos).nonneg_add_neg hneg, end end subtype end topological_group section topological_semiring variables [non_unital_non_assoc_semiring α] [topological_space α] [topological_semiring α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum.mul_left (a₂) (h : has_sum f a₁) : has_sum (λb, a₂ * f b) (a₂ * a₁) := by simpa only using h.map (add_monoid_hom.mul_left a₂) (continuous_const.mul continuous_id) lemma has_sum.mul_right (a₂) (hf : has_sum f a₁) : has_sum (λb, f b * a₂) (a₁ * a₂) := by simpa only using hf.map (add_monoid_hom.mul_right a₂) (continuous_id.mul continuous_const) lemma summable.mul_left (a) (hf : summable f) : summable (λb, a * f b) := (hf.has_sum.mul_left _).summable lemma summable.mul_right (a) (hf : summable f) : summable (λb, f b * a) := (hf.has_sum.mul_right _).summable section tsum variables [t2_space α] lemma summable.tsum_mul_left (a) (hf : summable f) : ∑'b, a * f b = a * ∑'b, f b := (hf.has_sum.mul_left _).tsum_eq lemma summable.tsum_mul_right (a) (hf : summable f) : (∑'b, f b * a) = (∑'b, f b) * a := (hf.has_sum.mul_right _).tsum_eq lemma commute.tsum_right (a) (h : ∀ b, commute a (f b)) : commute a (∑' b, f b) := if hf : summable f then (hf.tsum_mul_left a).symm.trans ((congr_arg _ $ funext h).trans (hf.tsum_mul_right a)) else (tsum_eq_zero_of_not_summable hf).symm ▸ commute.zero_right _ lemma commute.tsum_left (a) (h : ∀ b, commute (f b) a) : commute (∑' b, f b) a := (commute.tsum_right _ $ λ b, (h b).symm).symm end tsum end topological_semiring section const_smul variables {R : Type*} [monoid R] [topological_space α] [add_comm_monoid α] [distrib_mul_action R α] [has_continuous_const_smul R α] {f : β → α} lemma has_sum.const_smul {a : α} {r : R} (hf : has_sum f a) : has_sum (λ z, r • f z) (r • a) := hf.map (distrib_mul_action.to_add_monoid_hom α r) (continuous_const_smul r) lemma summable.const_smul {r : R} (hf : summable f) : summable (λ z, r • f z) := hf.has_sum.const_smul.summable lemma tsum_const_smul [t2_space α] {r : R} (hf : summable f) : ∑' z, r • f z = r • ∑' z, f z := hf.has_sum.const_smul.tsum_eq end const_smul section smul_const variables {R : Type*} [semiring R] [topological_space R] [topological_space α] [add_comm_monoid α] [module R α] [has_continuous_smul R α] {f : β → R} lemma has_sum.smul_const {a : α} {r : R} (hf : has_sum f r) : has_sum (λ z, f z • a) (r • a) := hf.map ((smul_add_hom R α).flip a) (continuous_id.smul continuous_const) lemma summable.smul_const {a : α} (hf : summable f) : summable (λ z, f z • a) := hf.has_sum.smul_const.summable lemma tsum_smul_const [t2_space α] {a : α} (hf : summable f) : ∑' z, f z • a = (∑' z, f z) • a := hf.has_sum.smul_const.tsum_eq end smul_const section division_ring variables [division_ring α] [topological_space α] [topological_ring α] {f g : β → α} {a a₁ a₂ : α} lemma has_sum.div_const (h : has_sum f a) (b : α) : has_sum (λ x, f x / b) (a / b) := by simp only [div_eq_mul_inv, h.mul_right b⁻¹] lemma summable.div_const (h : summable f) (b : α) : summable (λ x, f x / b) := (h.has_sum.div_const b).summable lemma has_sum_mul_left_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, a₂ * f b) (a₂ * a₁) := ⟨has_sum.mul_left _, λ H, by simpa only [inv_mul_cancel_left₀ h] using H.mul_left a₂⁻¹⟩ lemma has_sum_mul_right_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, f b * a₂) (a₁ * a₂) := ⟨has_sum.mul_right _, λ H, by simpa only [mul_inv_cancel_right₀ h] using H.mul_right a₂⁻¹⟩ lemma summable_mul_left_iff (h : a ≠ 0) : summable f ↔ summable (λb, a * f b) := ⟨λ H, H.mul_left _, λ H, by simpa only [inv_mul_cancel_left₀ h] using H.mul_left a⁻¹⟩ lemma summable_mul_right_iff (h : a ≠ 0) : summable f ↔ summable (λb, f b * a) := ⟨λ H, H.mul_right _, λ H, by simpa only [mul_inv_cancel_right₀ h] using H.mul_right a⁻¹⟩ lemma tsum_mul_left [t2_space α] : (∑' x, a * f x) = a * ∑' x, f x := if hf : summable f then hf.tsum_mul_left a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_left_iff ha).2 hf), mul_zero] lemma tsum_mul_right [t2_space α] : (∑' x, f x * a) = (∑' x, f x) * a := if hf : summable f then hf.tsum_mul_right a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_right_iff ha).2 hf), zero_mul] end division_ring section order_topology variables [ordered_add_comm_monoid α] [topological_space α] [order_closed_topology α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum_le (h : ∀b, f b ≤ g b) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ := le_of_tendsto_of_tendsto' hf hg $ assume s, sum_le_sum $ assume b _, h b @[mono] lemma has_sum_mono (hf : has_sum f a₁) (hg : has_sum g a₂) (h : f ≤ g) : a₁ ≤ a₂ := has_sum_le h hf hg lemma has_sum_le_of_sum_le (hf : has_sum f a) (h : ∀ s : finset β, ∑ b in s, f b ≤ a₂) : a ≤ a₂ := le_of_tendsto' hf h lemma le_has_sum_of_le_sum (hf : has_sum f a) (h : ∀ s : finset β, a₂ ≤ ∑ b in s, f b) : a₂ ≤ a := ge_of_tendsto' hf h lemma has_sum_le_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ := have has_sum (λc, (partial_inv i c).cases_on' 0 f) a₁, begin refine (has_sum_iff_has_sum_of_ne_zero_bij (i ∘ coe) _ _ _).2 hf, { exact assume c₁ c₂ eq, hi eq }, { intros c hc, rw [mem_support] at hc, cases eq : partial_inv i c with b; rw eq at hc, { contradiction }, { rw [partial_inv_of_injective hi] at eq, exact ⟨⟨b, hc⟩, eq⟩ } }, { assume c, simp [partial_inv_left hi, option.cases_on'] } end, begin refine has_sum_le (assume c, _) this hg, by_cases c ∈ set.range i, { rcases h with ⟨b, rfl⟩, rw [partial_inv_left hi, option.cases_on'], exact h _ }, { have : partial_inv i c = none := dif_neg h, rw [this, option.cases_on'], exact hs _ h } end lemma tsum_le_tsum_of_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : summable f) (hg : summable g) : tsum f ≤ tsum g := has_sum_le_inj i hi hs h hf.has_sum hg.has_sum lemma sum_le_has_sum (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : has_sum f a) : ∑ b in s, f b ≤ a := ge_of_tendsto hf (eventually_at_top.2 ⟨s, λ t hst, sum_le_sum_of_subset_of_nonneg hst $ λ b hbt hbs, hs b hbs⟩) lemma is_lub_has_sum (h : ∀ b, 0 ≤ f b) (hf : has_sum f a) : is_lub (set.range (λ s : finset β, ∑ b in s, f b)) a := is_lub_of_tendsto_at_top (finset.sum_mono_set_of_nonneg h) hf lemma le_has_sum (hf : has_sum f a) (b : β) (hb : ∀ b' ≠ b, 0 ≤ f b') : f b ≤ a := calc f b = ∑ b in {b}, f b : finset.sum_singleton.symm ... ≤ a : sum_le_has_sum _ (by { convert hb, simp }) hf lemma sum_le_tsum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : summable f) : ∑ b in s, f b ≤ ∑' b, f b := sum_le_has_sum s hs hf.has_sum lemma le_tsum (hf : summable f) (b : β) (hb : ∀ b' ≠ b, 0 ≤ f b') : f b ≤ ∑' b, f b := le_has_sum (summable.has_sum hf) b hb lemma tsum_le_tsum (h : ∀b, f b ≤ g b) (hf : summable f) (hg : summable g) : ∑'b, f b ≤ ∑'b, g b := has_sum_le h hf.has_sum hg.has_sum @[mono] lemma tsum_mono (hf : summable f) (hg : summable g) (h : f ≤ g) : ∑' n, f n ≤ ∑' n, g n := tsum_le_tsum h hf hg lemma tsum_le_of_sum_le (hf : summable f) (h : ∀ s : finset β, ∑ b in s, f b ≤ a₂) : ∑' b, f b ≤ a₂ := has_sum_le_of_sum_le hf.has_sum h lemma tsum_le_of_sum_le' (ha₂ : 0 ≤ a₂) (h : ∀ s : finset β, ∑ b in s, f b ≤ a₂) : ∑' b, f b ≤ a₂ := begin by_cases hf : summable f, { exact tsum_le_of_sum_le hf h }, { rw tsum_eq_zero_of_not_summable hf, exact ha₂ } end lemma has_sum.nonneg (h : ∀ b, 0 ≤ g b) (ha : has_sum g a) : 0 ≤ a := has_sum_le h has_sum_zero ha lemma has_sum.nonpos (h : ∀ b, g b ≤ 0) (ha : has_sum g a) : a ≤ 0 := has_sum_le h ha has_sum_zero lemma tsum_nonneg (h : ∀ b, 0 ≤ g b) : 0 ≤ ∑'b, g b := begin by_cases hg : summable g, { exact hg.has_sum.nonneg h }, { simp [tsum_eq_zero_of_not_summable hg] } end lemma tsum_nonpos (h : ∀ b, f b ≤ 0) : ∑'b, f b ≤ 0 := begin by_cases hf : summable f, { exact hf.has_sum.nonpos h }, { simp [tsum_eq_zero_of_not_summable hf] } end end order_topology section ordered_topological_group variables [ordered_add_comm_group α] [topological_space α] [topological_add_group α] [order_closed_topology α] {f g : β → α} {a₁ a₂ : α} lemma has_sum_lt {i : β} (h : ∀ (b : β), f b ≤ g b) (hi : f i < g i) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ < a₂ := have update f i 0 ≤ update g i 0 := update_le_update_iff.mpr ⟨rfl.le, λ i _, h i⟩, have 0 - f i + a₁ ≤ 0 - g i + a₂ := has_sum_le this (hf.update i 0) (hg.update i 0), by simpa only [zero_sub, add_neg_cancel_left] using add_lt_add_of_lt_of_le hi this @[mono] lemma has_sum_strict_mono (hf : has_sum f a₁) (hg : has_sum g a₂) (h : f < g) : a₁ < a₂ := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in has_sum_lt hle hi hf hg lemma tsum_lt_tsum {i : β} (h : ∀ (b : β), f b ≤ g b) (hi : f i < g i) (hf : summable f) (hg : summable g) : ∑' n, f n < ∑' n, g n := has_sum_lt h hi hf.has_sum hg.has_sum @[mono] lemma tsum_strict_mono (hf : summable f) (hg : summable g) (h : f < g) : ∑' n, f n < ∑' n, g n := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in tsum_lt_tsum hle hi hf hg lemma tsum_pos (hsum : summable g) (hg : ∀ b, 0 ≤ g b) (i : β) (hi : 0 < g i) : 0 < ∑' b, g b := by { rw ← tsum_zero, exact tsum_lt_tsum hg hi summable_zero hsum } lemma has_sum_zero_iff_of_nonneg (hf : ∀ i, 0 ≤ f i) : has_sum f 0 ↔ f = 0 := begin split, { intros hf', ext i, by_contra hi', have hi : 0 < f i := lt_of_le_of_ne (hf i) (ne.symm hi'), simpa using has_sum_lt hf hi has_sum_zero hf' }, { rintros rfl, exact has_sum_zero }, end end ordered_topological_group section canonically_ordered variables [canonically_ordered_add_monoid α] [topological_space α] [order_closed_topology α] variables {f : β → α} {a : α} lemma le_has_sum' (hf : has_sum f a) (b : β) : f b ≤ a := le_has_sum hf b $ λ _ _, zero_le _ lemma le_tsum' (hf : summable f) (b : β) : f b ≤ ∑' b, f b := le_tsum hf b $ λ _ _, zero_le _ lemma has_sum_zero_iff : has_sum f 0 ↔ ∀ x, f x = 0 := begin refine ⟨_, λ h, _⟩, { contrapose!, exact λ ⟨x, hx⟩ h, irrefl _ (lt_of_lt_of_le (pos_iff_ne_zero.2 hx) (le_has_sum' h x)) }, { convert has_sum_zero, exact funext h } end lemma tsum_eq_zero_iff (hf : summable f) : ∑' i, f i = 0 ↔ ∀ x, f x = 0 := by rw [←has_sum_zero_iff, hf.has_sum_iff] lemma tsum_ne_zero_iff (hf : summable f) : ∑' i, f i ≠ 0 ↔ ∃ x, f x ≠ 0 := by rw [ne.def, tsum_eq_zero_iff hf, not_forall] lemma is_lub_has_sum' (hf : has_sum f a) : is_lub (set.range (λ s : finset β, ∑ b in s, f b)) a := is_lub_of_tendsto_at_top (finset.sum_mono_set f) hf end canonically_ordered section uniform_group variables [add_comm_group α] [uniform_space α] /-- The **Cauchy criterion** for infinite sums, also known as the **Cauchy convergence test** -/ lemma summable_iff_cauchy_seq_finset [complete_space α] {f : β → α} : summable f ↔ cauchy_seq (λ (s : finset β), ∑ b in s, f b) := cauchy_map_iff_exists_tendsto.symm variables [uniform_add_group α] {f g : β → α} {a a₁ a₂ : α} lemma cauchy_seq_finset_iff_vanishing : cauchy_seq (λ (s : finset β), ∑ b in s, f b) ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) := begin simp only [cauchy_seq, cauchy_map_iff, and_iff_right at_top_ne_bot, prod_at_top_at_top_eq, uniformity_eq_comap_nhds_zero α, tendsto_comap_iff, (∘)], rw [tendsto_at_top'], split, { assume h e he, rcases h e he with ⟨⟨s₁, s₂⟩, h⟩, use [s₁ ∪ s₂], assume t ht, specialize h (s₁ ∪ s₂, (s₁ ∪ s₂) ∪ t) ⟨le_sup_left, le_sup_of_le_left le_sup_right⟩, simpa only [finset.sum_union ht.symm, add_sub_cancel'] using h }, { assume h e he, rcases exists_nhds_half_neg he with ⟨d, hd, hde⟩, rcases h d hd with ⟨s, h⟩, use [(s, s)], rintros ⟨t₁, t₂⟩ ⟨ht₁, ht₂⟩, have : ∑ b in t₂, f b - ∑ b in t₁, f b = ∑ b in t₂ \ s, f b - ∑ b in t₁ \ s, f b, { simp only [(finset.sum_sdiff ht₁).symm, (finset.sum_sdiff ht₂).symm, add_sub_add_right_eq_sub] }, simp only [this], exact hde _ (h _ finset.sdiff_disjoint) _ (h _ finset.sdiff_disjoint) } end local attribute [instance] topological_add_group.t3_space /-- 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 (f : β → α) : tendsto (λ (s : finset β), ∑' b : {x // x ∉ s}, f b) at_top (𝓝 0) := begin by_cases H : summable f, { assume e he, rcases exists_mem_nhds_is_closed_subset he with ⟨o, ho, o_closed, oe⟩, simp only [le_eq_subset, set.mem_preimage, mem_at_top_sets, filter.mem_map, ge_iff_le], obtain ⟨s, hs⟩ : ∃ (s : finset β), ∀ (t : finset β), disjoint t s → ∑ (b : β) in t, f b ∈ o := cauchy_seq_finset_iff_vanishing.1 (tendsto.cauchy_seq H.has_sum) o ho, refine ⟨s, λ a sa, oe _⟩, have A : summable (λ b : {x // x ∉ a}, f b) := a.summable_compl_iff.2 H, apply is_closed.mem_of_tendsto o_closed A.has_sum (eventually_of_forall (λ b, _)), have : disjoint (finset.image (λ (i : {x // x ∉ a}), (i : β)) b) s, { apply disjoint_left.2 (λ i hi his, _), rcases mem_image.1 hi with ⟨i', hi', rfl⟩, exact i'.2 (sa his), }, convert hs _ this using 1, rw sum_image, assume i hi j hj hij, exact subtype.ext hij }, { convert tendsto_const_nhds, ext s, apply tsum_eq_zero_of_not_summable, rwa finset.summable_compl_iff } end variable [complete_space α] lemma summable_iff_vanishing : summable f ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) := by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing] /- TODO: generalize to monoid with a uniform continuous subtraction operator: `(a + b) - b = a` -/ lemma summable.summable_of_eq_zero_or_self (hf : summable f) (h : ∀b, g b = 0 ∨ g b = f b) : summable g := summable_iff_vanishing.2 $ assume e he, let ⟨s, hs⟩ := summable_iff_vanishing.1 hf e he in ⟨s, assume t ht, have eq : ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t, g b := calc ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t.filter (λb, g b = f b), g b : finset.sum_congr rfl (assume b hb, (finset.mem_filter.1 hb).2.symm) ... = ∑ b in t, g b : begin refine finset.sum_subset (finset.filter_subset _ _) _, assume b hbt hb, simp only [(∉), finset.mem_filter, and_iff_right hbt] at hb, exact (h b).resolve_right hb end, eq ▸ hs _ $ finset.disjoint_of_subset_left (finset.filter_subset _ _) ht⟩ protected lemma summable.indicator (hf : summable f) (s : set β) : summable (s.indicator f) := hf.summable_of_eq_zero_or_self $ set.indicator_eq_zero_or_self _ _ lemma summable.comp_injective {i : γ → β} (hf : summable f) (hi : injective i) : summable (f ∘ i) := begin simpa only [set.indicator_range_comp] using (hi.summable_iff _).2 (hf.indicator (set.range i)), exact λ x hx, set.indicator_of_not_mem hx _ end lemma summable.subtype (hf : summable f) (s : set β) : summable (f ∘ coe : s → α) := hf.comp_injective subtype.coe_injective lemma summable_subtype_and_compl {s : set β} : summable (λ x : s, f x) ∧ summable (λ x : sᶜ, f x) ↔ summable f := ⟨and_imp.2 summable.add_compl, λ h, ⟨h.subtype s, h.subtype sᶜ⟩⟩ lemma summable.sigma_factor {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) (b : β) : summable (λc, f ⟨b, c⟩) := ha.comp_injective sigma_mk_injective lemma summable.sigma {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) : summable (λb, ∑'c, f ⟨b, c⟩) := ha.sigma' (λ b, ha.sigma_factor b) lemma summable.prod_factor {f : β × γ → α} (h : summable f) (b : β) : summable (λ c, f (b, c)) := h.comp_injective $ λ c₁ c₂ h, (prod.ext_iff.1 h).2 lemma tsum_sigma [t1_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := tsum_sigma' (λ b, ha.sigma_factor b) ha lemma tsum_prod [t1_space α] {f : β × γ → α} (h : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := tsum_prod' h h.prod_factor lemma tsum_comm [t1_space α] {f : β → γ → α} (h : summable (function.uncurry f)) : ∑' c b, f b c = ∑' b c, f b c := tsum_comm' h h.prod_factor h.prod_symm.prod_factor lemma has_sum.sum_nat_of_sum_int [t2_space α] {f : ℤ → α} (hf : has_sum f a) : has_sum (λ n:ℕ, f(n + 1) + f(-n.succ)) (a - f 0) := begin obtain ⟨b₁, h₁⟩ : summable (λ n : ℕ, f(n + 1)) := hf.summable.comp_injective (λ x₁ x₂, by simp), obtain ⟨b₂, h₂⟩ : summable (λ n : ℕ, f(-n.succ)) := hf.summable.comp_injective (λ x₁ x₂, by simp), convert h₁.add h₂, rw hf.unique (h₁.pos_add_zero_add_neg h₂), abel, end lemma tsum_subtype_add_tsum_subtype_compl [t2_space α] {f : β → α} (hf : summable f) (s : set β) : ∑' x : s, f x + ∑' x : sᶜ, f x = ∑' x, f x := ((hf.subtype s).has_sum.add_compl (hf.subtype {x | x ∉ s}).has_sum).unique hf.has_sum lemma sum_add_tsum_subtype_compl [t2_space α] {f : β → α} (hf : summable f) (s : finset β) : ∑ x in s, f x + ∑' x : {x // x ∉ s}, f x = ∑' x, f x := begin rw ← tsum_subtype_add_tsum_subtype_compl hf s, simp only [finset.tsum_subtype', add_right_inj], refl, end end uniform_group section topological_group variables {G : Type*} [topological_space G] [add_comm_group G] [topological_add_group G] {f : α → G} lemma summable.vanishing (hf : summable f) ⦃e : set G⦄ (he : e ∈ 𝓝 (0 : G)) : ∃ s : finset α, ∀ t, disjoint t s → ∑ k in t, f k ∈ e := begin letI : uniform_space G := topological_add_group.to_uniform_space G, letI : uniform_add_group G := topological_add_comm_group_is_uniform, rcases hf with ⟨y, hy⟩, exact cauchy_seq_finset_iff_vanishing.1 hy.cauchy_seq e he end /-- Series divergence test: if `f` is a convergent series, then `f x` tends to zero along `cofinite`. -/ lemma summable.tendsto_cofinite_zero (hf : summable f) : tendsto f cofinite (𝓝 0) := begin intros e he, rw [filter.mem_map], rcases hf.vanishing he with ⟨s, hs⟩, refine s.eventually_cofinite_nmem.mono (λ x hx, _), by simpa using hs {x} (disjoint_singleton_left.2 hx) end lemma summable.tendsto_at_top_zero {f : ℕ → G} (hf : summable f) : tendsto f at_top (𝓝 0) := by { rw ←nat.cofinite_eq_at_top, exact hf.tendsto_cofinite_zero } lemma summable.tendsto_top_of_pos {α : Type*} [linear_ordered_field α] [topological_space α] [order_topology α] {f : ℕ → α} (hf : summable f⁻¹) (hf' : ∀ n, 0 < f n) : tendsto f at_top at_top := begin rw [show f = f⁻¹⁻¹, by { ext, simp }], apply filter.tendsto.inv_tendsto_zero, apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (summable.tendsto_at_top_zero hf), rw eventually_iff_exists_mem, refine ⟨set.Ioi 0, Ioi_mem_at_top _, λ _ _, _⟩, rw [set.mem_Ioi, inv_eq_one_div, one_div, pi.inv_apply, _root_.inv_pos], exact hf' _, end end topological_group section preorder variables {E : Type*} [preorder E] [add_comm_monoid E] [topological_space E] [order_closed_topology E] [t2_space E] lemma tsum_le_of_sum_range_le {f : ℕ → E} {c : E} (hsum : summable f) (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c := let ⟨l, hl⟩ := hsum in hl.tsum_eq.symm ▸ le_of_tendsto' hl.tendsto_sum_nat h end preorder section linear_order /-! For infinite sums taking values in a linearly ordered monoid, the existence of a least upper bound for the finite sums is a criterion for summability. This criterion is useful when applied in a linearly ordered monoid which is also a complete or conditionally complete linear order, such as `ℝ`, `ℝ≥0`, `ℝ≥0∞`, because it is then easy to check the existence of a least upper bound. -/ lemma has_sum_of_is_lub_of_nonneg [linear_ordered_add_comm_monoid β] [topological_space β] [order_topology β] {f : α → β} (b : β) (h : ∀ b, 0 ≤ f b) (hf : is_lub (set.range (λ s, ∑ a in s, f a)) b) : has_sum f b := tendsto_at_top_is_lub (finset.sum_mono_set_of_nonneg h) hf lemma has_sum_of_is_lub [canonically_linear_ordered_add_monoid β] [topological_space β] [order_topology β] {f : α → β} (b : β) (hf : is_lub (set.range (λ s, ∑ a in s, f a)) b) : has_sum f b := tendsto_at_top_is_lub (finset.sum_mono_set f) hf lemma summable_abs_iff [linear_ordered_add_comm_group β] [uniform_space β] [uniform_add_group β] [complete_space β] {f : α → β} : summable (λ x, |f x|) ↔ summable f := have h1 : ∀ x : {x | 0 ≤ f x}, |f x| = f x := λ x, abs_of_nonneg x.2, have h2 : ∀ x : {x | 0 ≤ f x}ᶜ, |f x| = -f x := λ x, abs_of_neg (not_le.1 x.2), calc summable (λ x, |f x|) ↔ summable (λ x : {x | 0 ≤ f x}, |f x|) ∧ summable (λ x : {x | 0 ≤ f x}ᶜ, |f x|) : summable_subtype_and_compl.symm ... ↔ summable (λ x : {x | 0 ≤ f x}, f x) ∧ summable (λ x : {x | 0 ≤ f x}ᶜ, -f x) : by simp only [h1, h2] ... ↔ _ : by simp only [summable_neg_iff, summable_subtype_and_compl] alias summable_abs_iff ↔ summable.of_abs summable.abs lemma finite_of_summable_const [linear_ordered_add_comm_group β] [archimedean β] [topological_space β] [order_closed_topology β] {b : β} (hb : 0 < b) (hf : summable (λ a : α, b)) : set.finite (set.univ : set α) := begin have H : ∀ s : finset α, s.card • b ≤ ∑' a : α, b, { intros s, simpa using sum_le_has_sum s (λ a ha, hb.le) hf.has_sum }, obtain ⟨n, hn⟩ := archimedean.arch (∑' a : α, b) hb, have : ∀ s : finset α, s.card ≤ n, { intros s, simpa [nsmul_le_nsmul_iff hb] using (H s).trans hn }, haveI : fintype α := fintype_of_finset_card_le n this, exact set.finite_univ end end linear_order section cauchy_seq open filter /-- If the extended distance between consecutive points of a sequence is estimated by a summable series of `nnreal`s, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_edist_le_of_summable [pseudo_emetric_space α] {f : ℕ → α} (d : ℕ → ℝ≥0) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f := begin refine emetric.cauchy_seq_iff_nnreal.2 (λ ε εpos, _), -- Actually we need partial sums of `d` to be a Cauchy sequence replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, -- Now we take the same `N` as in one of the definitions of a Cauchy sequence refine (metric.cauchy_seq_iff'.1 hd ε (nnreal.coe_pos.2 εpos)).imp (λ N hN n hn, _), have hsum := hN n hn, -- We simplify the known inequality rw [dist_nndist, nnreal.nndist_eq, ← sum_range_add_sum_Ico _ hn, add_tsub_cancel_left] at hsum, norm_cast at hsum, replace hsum := lt_of_le_of_lt (le_max_left _ _) hsum, rw edist_comm, -- Then use `hf` to simplify the goal to the same form apply lt_of_le_of_lt (edist_le_Ico_sum_of_edist_le hn (λ k _ _, hf k)), assumption_mod_cast end /-- If the distance between consecutive points of a sequence is estimated by a summable series, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_dist_le_of_summable [pseudo_metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f := begin refine metric.cauchy_seq_iff'.2 (λε εpos, _), replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, refine (metric.cauchy_seq_iff'.1 hd ε εpos).imp (λ N hN n hn, _), have hsum := hN n hn, rw [real.dist_eq, ← sum_Ico_eq_sub _ hn] at hsum, calc dist (f n) (f N) = dist (f N) (f n) : dist_comm _ _ ... ≤ ∑ x in Ico N n, d x : dist_le_Ico_sum_of_dist_le hn (λ k _ _, hf k) ... ≤ |∑ x in Ico N n, d x| : le_abs_self _ ... < ε : hsum end lemma cauchy_seq_of_summable_dist [pseudo_metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ (λ _, le_rfl) h lemma dist_le_tsum_of_dist_le_of_tendsto [pseudo_metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ ∑' m, d (n + m) := begin refine le_of_tendsto (tendsto_const_nhds.dist ha) (eventually_at_top.2 ⟨n, λ m hnm, _⟩), refine le_trans (dist_le_Ico_sum_of_dist_le hnm (λ k _ _, hf k)) _, rw [sum_Ico_eq_sum_range], refine sum_le_tsum (range _) (λ _ _, le_trans dist_nonneg (hf _)) _, exact hd.comp_injective (add_right_injective n) end lemma dist_le_tsum_of_dist_le_of_tendsto₀ [pseudo_metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ tsum d := by simpa only [zero_add] using dist_le_tsum_of_dist_le_of_tendsto d hf hd ha 0 lemma dist_le_tsum_dist_of_tendsto [pseudo_metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) (n) : dist (f n) a ≤ ∑' m, dist (f (n+m)) (f (n+m).succ) := show dist (f n) a ≤ ∑' m, (λx, dist (f x) (f x.succ)) (n + m), from dist_le_tsum_of_dist_le_of_tendsto (λ n, dist (f n) (f n.succ)) (λ _, le_rfl) h ha n lemma dist_le_tsum_dist_of_tendsto₀ [pseudo_metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ ∑' n, dist (f n) (f n.succ) := by simpa only [zero_add] using dist_le_tsum_dist_of_tendsto h ha 0 end cauchy_seq /-! ## Multipliying two infinite sums In this section, we prove various results about `(∑' x : β, f x) * (∑' y : γ, g y)`. Note that we always assume that the family `λ x : β × γ, f x.1 * g x.2` is summable, since there is no way to deduce this from the summmabilities of `f` and `g` in general, but if you are working in a normed space, you may want to use the analogous lemmas in `analysis/normed_space/basic` (e.g `tsum_mul_tsum_of_summable_norm`). We first establish results about arbitrary index types, `β` and `γ`, and then we specialize to `β = γ = ℕ` to prove the Cauchy product formula (see `tsum_mul_tsum_eq_tsum_sum_antidiagonal`). ### Arbitrary index types -/ section tsum_mul_tsum variables [topological_space α] [t3_space α] [non_unital_non_assoc_semiring α] [topological_semiring α] {f : β → α} {g : γ → α} {s t u : α} lemma has_sum.mul_eq (hf : has_sum f s) (hg : has_sum g t) (hfg : has_sum (λ (x : β × γ), f x.1 * g x.2) u) : s * t = u := have key₁ : has_sum (λ b, f b * t) (s * t), from hf.mul_right t, have this : ∀ b : β, has_sum (λ c : γ, f b * g c) (f b * t), from λ b, hg.mul_left (f b), have key₂ : has_sum (λ b, f b * t) u, from has_sum.prod_fiberwise hfg this, key₁.unique key₂ lemma has_sum.mul (hf : has_sum f s) (hg : has_sum g t) (hfg : summable (λ (x : β × γ), f x.1 * g x.2)) : has_sum (λ (x : β × γ), f x.1 * g x.2) (s * t) := let ⟨u, hu⟩ := hfg in (hf.mul_eq hg hu).symm ▸ hu /-- Product of two infinites sums indexed by arbitrary types. See also `tsum_mul_tsum_of_summable_norm` if `f` and `g` are abolutely summable. -/ lemma tsum_mul_tsum (hf : summable f) (hg : summable g) (hfg : summable (λ (x : β × γ), f x.1 * g x.2)) : (∑' x, f x) * (∑' y, g y) = (∑' z : β × γ, f z.1 * g z.2) := hf.has_sum.mul_eq hg.has_sum hfg.has_sum end tsum_mul_tsum section cauchy_product /-! ### `ℕ`-indexed families (Cauchy product) We prove two versions of the Cauchy product formula. The first one is `tsum_mul_tsum_eq_tsum_sum_range`, where the `n`-th term is a sum over `finset.range (n+1)` involving `nat` substraction. In order to avoid `nat` substraction, we also provide `tsum_mul_tsum_eq_tsum_sum_antidiagonal`, where the `n`-th term is a sum over all pairs `(k, l)` such that `k+l=n`, which corresponds to the `finset` `finset.nat.antidiagonal n` -/ variables {f : ℕ → α} {g : ℕ → α} open finset variables [topological_space α] [non_unital_non_assoc_semiring α] /- The family `(k, l) : ℕ × ℕ ↦ f k * g l` is summable if and only if the family `(n, k, l) : Σ (n : ℕ), nat.antidiagonal n ↦ f k * g l` is summable. -/ lemma summable_mul_prod_iff_summable_mul_sigma_antidiagonal {f g : ℕ → α} : summable (λ x : ℕ × ℕ, f x.1 * g x.2) ↔ summable (λ x : (Σ (n : ℕ), nat.antidiagonal n), f (x.2 : ℕ × ℕ).1 * g (x.2 : ℕ × ℕ).2) := nat.sigma_antidiagonal_equiv_prod.summable_iff.symm variables [t3_space α] [topological_semiring α] lemma summable_sum_mul_antidiagonal_of_summable_mul {f g : ℕ → α} (h : summable (λ x : ℕ × ℕ, f x.1 * g x.2)) : summable (λ n, ∑ kl in nat.antidiagonal n, f kl.1 * g kl.2) := begin rw summable_mul_prod_iff_summable_mul_sigma_antidiagonal at h, conv {congr, funext, rw [← finset.sum_finset_coe, ← tsum_fintype]}, exact h.sigma' (λ n, (has_sum_fintype _).summable), end /-- The Cauchy product formula for the product of two infinites sums indexed by `ℕ`, expressed by summing on `finset.nat.antidiagonal`. See also `tsum_mul_tsum_eq_tsum_sum_antidiagonal_of_summable_norm` if `f` and `g` are absolutely summable. -/ lemma tsum_mul_tsum_eq_tsum_sum_antidiagonal (hf : summable f) (hg : summable g) (hfg : summable (λ (x : ℕ × ℕ), f x.1 * g x.2)) : (∑' n, f n) * (∑' n, g n) = (∑' n, ∑ kl in nat.antidiagonal n, f kl.1 * g kl.2) := begin conv_rhs {congr, funext, rw [← finset.sum_finset_coe, ← tsum_fintype]}, rw [tsum_mul_tsum hf hg hfg, ← nat.sigma_antidiagonal_equiv_prod.tsum_eq (_ : ℕ × ℕ → α)], exact tsum_sigma' (λ n, (has_sum_fintype _).summable) (summable_mul_prod_iff_summable_mul_sigma_antidiagonal.mp hfg) end lemma summable_sum_mul_range_of_summable_mul {f g : ℕ → α} (h : summable (λ x : ℕ × ℕ, f x.1 * g x.2)) : summable (λ n, ∑ k in range (n+1), f k * g (n - k)) := begin simp_rw ← nat.sum_antidiagonal_eq_sum_range_succ (λ k l, f k * g l), exact summable_sum_mul_antidiagonal_of_summable_mul h end /-- The Cauchy product formula for the product of two infinites sums indexed by `ℕ`, expressed by summing on `finset.range`. See also `tsum_mul_tsum_eq_tsum_sum_range_of_summable_norm` if `f` and `g` are absolutely summable. -/ lemma tsum_mul_tsum_eq_tsum_sum_range (hf : summable f) (hg : summable g) (hfg : summable (λ (x : ℕ × ℕ), f x.1 * g x.2)) : (∑' n, f n) * (∑' n, g n) = (∑' n, ∑ k in range (n+1), f k * g (n - k)) := begin simp_rw ← nat.sum_antidiagonal_eq_sum_range_succ (λ k l, f k * g l), exact tsum_mul_tsum_eq_tsum_sum_antidiagonal hf hg hfg end end cauchy_product
557382906043ffff6608a84e20d3e4eabcf9313b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/polynomial/mirror.lean
e91fcd0faf5c7110e1a7b424733784d0247b0a5d
[ "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
7,393
lean
/- Copyright (c) 2020 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import data.polynomial.ring_division import algebra.big_operators.nat_antidiagonal /-! # "Mirror" of a univariate polynomial In this file we define `polynomial.mirror`, a variant of `polynomial.reverse`. The difference between `reverse` and `mirror` is that `reverse` will decrease the degree if the polynomial is divisible by `X`. We also define `polynomial.norm2`, which is the sum of the squares of the coefficients of a polynomial. It is also a coefficient of `p * p.mirror`. ## Main definitions - `polynomial.mirror` - `polynomial.norm2` ## Main results - `polynomial.mirror_mul_of_domain`: `mirror` preserves multiplication. - `polynomial.irreducible_of_mirror`: an irreducibility criterion involving `mirror` - `polynomial.norm2_eq_mul_reverse_coeff`: `norm2` is a coefficient of `p * p.mirror` -/ namespace polynomial variables {R : Type*} [semiring R] (p : polynomial R) section mirror /-- mirror of a polynomial: reverses the coefficients while preserving `polynomial.nat_degree` -/ noncomputable def mirror := p.reverse * X ^ p.nat_trailing_degree @[simp] lemma mirror_zero : (0 : polynomial R).mirror = 0 := by simp [mirror] lemma mirror_monomial (n : ℕ) (a : R) : (monomial n a).mirror = (monomial n a) := begin by_cases ha : a = 0, { rw [ha, monomial_zero_right, mirror_zero] }, { rw [mirror, reverse, nat_degree_monomial n a ha, nat_trailing_degree_monomial ha, ←C_mul_X_pow_eq_monomial, reflect_C_mul_X_pow, rev_at_le (le_refl n), nat.sub_self, pow_zero, mul_one] }, end lemma mirror_C (a : R) : (C a).mirror = C a := mirror_monomial 0 a lemma mirror_X : X.mirror = (X : polynomial R) := mirror_monomial 1 (1 : R) lemma mirror_nat_degree : p.mirror.nat_degree = p.nat_degree := begin by_cases hp : p = 0, { rw [hp, mirror_zero] }, by_cases hR : nontrivial R, { haveI := hR, rw [mirror, nat_degree_mul', reverse_nat_degree, nat_degree_X_pow, nat.sub_add_cancel p.nat_trailing_degree_le_nat_degree], rwa [leading_coeff_X_pow, mul_one, reverse_leading_coeff, ne, trailing_coeff_eq_zero] }, { haveI := not_nontrivial_iff_subsingleton.mp hR, exact congr_arg nat_degree (subsingleton.elim p.mirror p) }, end lemma mirror_nat_trailing_degree : p.mirror.nat_trailing_degree = p.nat_trailing_degree := begin by_cases hp : p = 0, { rw [hp, mirror_zero] }, { rw [mirror, nat_trailing_degree_mul_X_pow ((mt reverse_eq_zero.mp) hp), reverse_nat_trailing_degree, zero_add] }, end lemma coeff_mirror (n : ℕ) : p.mirror.coeff n = p.coeff (rev_at (p.nat_degree + p.nat_trailing_degree) n) := begin by_cases h2 : p.nat_degree < n, { rw [coeff_eq_zero_of_nat_degree_lt (by rwa mirror_nat_degree)], by_cases h1 : n ≤ p.nat_degree + p.nat_trailing_degree, { rw [rev_at_le h1, coeff_eq_zero_of_lt_nat_trailing_degree], exact (nat.sub_lt_left_iff_lt_add h1).mpr (nat.add_lt_add_right h2 _) }, { rw [←rev_at_fun_eq, rev_at_fun, if_neg h1, coeff_eq_zero_of_nat_degree_lt h2] } }, rw not_lt at h2, rw [rev_at_le (h2.trans (nat.le_add_right _ _))], by_cases h3 : p.nat_trailing_degree ≤ n, { rw [←nat.sub_add_eq_add_sub h2, ←nat.sub_sub_assoc h2 h3, mirror, coeff_mul_X_pow', if_pos h3, coeff_reverse, rev_at_le ((nat.sub_le_self _ _).trans h2)] }, rw not_le at h3, rw coeff_eq_zero_of_nat_degree_lt (nat.lt_sub_right_iff_add_lt.mpr (nat.add_lt_add_left h3 _)), exact coeff_eq_zero_of_lt_nat_trailing_degree (by rwa mirror_nat_trailing_degree), end --TODO: Extract `finset.sum_range_rev_at` lemma. lemma mirror_eval_one : p.mirror.eval 1 = p.eval 1 := begin simp_rw [eval_eq_finset_sum, one_pow, mul_one, mirror_nat_degree], refine finset.sum_bij_ne_zero _ _ _ _ _, { exact λ n hn hp, rev_at (p.nat_degree + p.nat_trailing_degree) n }, { intros n hn hp, rw finset.mem_range_succ_iff at *, rw rev_at_le (hn.trans (nat.le_add_right _ _)), rw [nat.sub_le_iff, add_comm, nat.add_sub_cancel, ←mirror_nat_trailing_degree], exact nat_trailing_degree_le_of_ne_zero hp }, { exact λ n₁ n₂ hn₁ hp₁ hn₂ hp₂ h, by rw [←@rev_at_invol _ n₁, h, rev_at_invol] }, { intros n hn hp, use rev_at (p.nat_degree + p.nat_trailing_degree) n, refine ⟨_, _, rev_at_invol.symm⟩, { rw finset.mem_range_succ_iff at *, rw rev_at_le (hn.trans (nat.le_add_right _ _)), rw [nat.sub_le_iff, add_comm, nat.add_sub_cancel], exact nat_trailing_degree_le_of_ne_zero hp }, { change p.mirror.coeff _ ≠ 0, rwa [coeff_mirror, rev_at_invol] } }, { exact λ n hn hp, p.coeff_mirror n }, end lemma mirror_mirror : p.mirror.mirror = p := polynomial.ext (λ n, by rw [coeff_mirror, coeff_mirror, mirror_nat_degree, mirror_nat_trailing_degree, rev_at_invol]) lemma mirror_eq_zero : p.mirror = 0 ↔ p = 0 := ⟨λ h, by rw [←p.mirror_mirror, h, mirror_zero], λ h, by rw [h, mirror_zero]⟩ lemma mirror_trailing_coeff : p.mirror.trailing_coeff = p.leading_coeff := by rw [leading_coeff, trailing_coeff, mirror_nat_trailing_degree, coeff_mirror, rev_at_le (nat.le_add_left _ _), nat.add_sub_cancel] lemma mirror_leading_coeff : p.mirror.leading_coeff = p.trailing_coeff := by rw [←p.mirror_mirror, mirror_trailing_coeff, p.mirror_mirror] lemma mirror_mul_of_domain {R : Type*} [integral_domain R] (p q : polynomial R) : (p * q).mirror = p.mirror * q.mirror := begin by_cases hp : p = 0, { rw [hp, zero_mul, mirror_zero, zero_mul] }, by_cases hq : q = 0, { rw [hq, mul_zero, mirror_zero, mul_zero] }, rw [mirror, mirror, mirror, reverse_mul_of_domain, nat_trailing_degree_mul hp hq, pow_add], ring, end lemma mirror_smul {R : Type*} [integral_domain R] (p : polynomial R) (a : R) : (a • p).mirror = a • p.mirror := by rw [←C_mul', ←C_mul', mirror_mul_of_domain, mirror_C] lemma mirror_neg {R : Type*} [ring R] (p : polynomial R) : (-p).mirror = -(p.mirror) := by rw [mirror, mirror, reverse_neg, nat_trailing_degree_neg, neg_mul_eq_neg_mul] lemma irreducible_of_mirror {R : Type*} [integral_domain R] {f : polynomial R} (h1 : ¬ is_unit f) (h2 : ∀ k, f * f.mirror = k * k.mirror → k = f ∨ k = -f ∨ k = f.mirror ∨ k = -f.mirror) (h3 : ∀ g, g ∣ f → g ∣ f.mirror → is_unit g) : irreducible f := begin split, { exact h1 }, { intros g h fgh, let k := g * h.mirror, have key : f * f.mirror = k * k.mirror, { rw [fgh, mirror_mul_of_domain, mirror_mul_of_domain, mirror_mirror, mul_assoc, mul_comm h, mul_comm g.mirror, mul_assoc, ←mul_assoc] }, have g_dvd_f : g ∣ f, { rw fgh, exact dvd_mul_right g h }, have h_dvd_f : h ∣ f, { rw fgh, exact dvd_mul_left h g }, have g_dvd_k : g ∣ k, { exact dvd_mul_right g h.mirror }, have h_dvd_k_rev : h ∣ k.mirror, { rw [mirror_mul_of_domain, mirror_mirror], exact dvd_mul_left h g.mirror }, have hk := h2 k key, rcases hk with hk | hk | hk | hk, { exact or.inr (h3 h h_dvd_f (by rwa ← hk)) }, { exact or.inr (h3 h h_dvd_f (by rwa [eq_neg_iff_eq_neg.mp hk, mirror_neg, dvd_neg])) }, { exact or.inl (h3 g g_dvd_f (by rwa ← hk)) }, { exact or.inl (h3 g g_dvd_f (by rwa [eq_neg_iff_eq_neg.mp hk, dvd_neg])) } }, end end mirror end polynomial
8fabd09013915c9bb00192025ff1cef914bf8dba
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/linear_algebra/prod.lean
20d5b436c8b09aba0609019901638bafab2566b8
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,293
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, Kevin Buzzard, Yury Kudryashov, Eric Wieser -/ import linear_algebra.basic import order.partial_sups /-! ### Products of modules This file defines constructors for linear maps whose domains or codomains are products. It contains theorems relating these to each other, as well as to `submodule.prod`, `submodule.map`, `submodule.comap`, `linear_map.range`, and `linear_map.ker`. ## Main definitions - products in the domain: - `linear_map.fst` - `linear_map.snd` - `linear_map.coprod` - `linear_map.prod_ext` - products in the codomain: - `linear_map.inl` - `linear_map.inr` - `linear_map.prod` - products in both domain and codomain: - `linear_map.prod_map` - `linear_equiv.prod_map` - `linear_equiv.skew_prod` -/ universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} section prod namespace linear_map variables (S : Type*) [semiring R] [semiring S] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [module R M] [module R M₂] [module R M₃] [module R M₄] variables (f : M →ₗ[R] M₂) section variables (R M M₂) /-- The first projection of a product is a linear map. -/ def fst : M × M₂ →ₗ[R] M := { to_fun := prod.fst, map_add' := λ x y, rfl, map_smul' := λ x y, rfl } /-- The second projection of a product is a linear map. -/ def snd : M × M₂ →ₗ[R] M₂ := { to_fun := prod.snd, map_add' := λ x y, rfl, map_smul' := λ x y, rfl } end @[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl @[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl theorem fst_surjective : function.surjective (fst R M M₂) := λ x, ⟨(x, 0), rfl⟩ theorem snd_surjective : function.surjective (snd R M M₂) := λ x, ⟨(0, x), rfl⟩ /-- The prod of two linear maps is a linear map. -/ @[simps] def prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (M →ₗ[R] M₂ × M₃) := { to_fun := λ x, (f x, g x), map_add' := λ x y, by simp only [prod.mk_add_mk, map_add], map_smul' := λ c x, by simp only [prod.smul_mk, map_smul] } @[simp] theorem fst_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (fst R M₂ M₃).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (snd R M₂ M₃).comp (prod f g) = g := by ext; refl @[simp] theorem pair_fst_snd : prod (fst R M M₂) (snd R M M₂) = linear_map.id := by ext; refl /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def prod_equiv [module S M₂] [module S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] : ((M →ₗ[R] M₂) × (M →ₗ[R] M₃)) ≃ₗ[S] (M →ₗ[R] M₂ × M₃) := { to_fun := λ f, f.1.prod f.2, inv_fun := λ f, ((fst _ _ _).comp f, (snd _ _ _).comp f), left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl, map_add' := λ a b, rfl, map_smul' := λ r a, rfl } section variables (R M M₂) /-- The left injection into a product is a linear map. -/ def inl : M →ₗ[R] M × M₂ := prod linear_map.id 0 /-- The right injection into a product is a linear map. -/ def inr : M₂ →ₗ[R] M × M₂ := prod 0 linear_map.id theorem range_inl : range (inl R M M₂) = ker (snd R M M₂) := begin ext x, simp only [mem_ker, mem_range], split, { rintros ⟨y, rfl⟩, refl }, { intro h, exact ⟨x.fst, prod.ext rfl h.symm⟩ } end theorem ker_snd : ker (snd R M M₂) = range (inl R M M₂) := eq.symm $ range_inl R M M₂ theorem range_inr : range (inr R M M₂) = ker (fst R M M₂) := begin ext x, simp only [mem_ker, mem_range], split, { rintros ⟨y, rfl⟩, refl }, { intro h, exact ⟨x.snd, prod.ext h.symm rfl⟩ } end theorem ker_fst : ker (fst R M M₂) = range (inr R M M₂) := eq.symm $ range_inr R M M₂ end @[simp] theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl @[simp] theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl theorem inl_eq_prod : inl R M M₂ = prod linear_map.id 0 := rfl theorem inr_eq_prod : inr R M M₂ = prod 0 linear_map.id := rfl theorem inl_injective : function.injective (inl R M M₂) := λ _, by simp theorem inr_injective : function.injective (inr R M M₂) := λ _, by simp /-- The coprod function `λ x : M × M₂, f.1 x.1 + f.2 x.2` is a linear map. -/ def coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ := f.comp (fst _ _ _) + g.comp (snd _ _ _) @[simp] theorem coprod_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M × M₂) : coprod f g x = f x.1 + g x.2 := rfl @[simp] theorem coprod_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inl R M M₂) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inr R M M₂) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M M₂) (inr R M M₂) = linear_map.id := by ext; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem comp_coprod (f : M₃ →ₗ[R] M₄) (g₁ : M →ₗ[R] M₃) (g₂ : M₂ →ₗ[R] M₃) : f.comp (g₁.coprod g₂) = (f.comp g₁).coprod (f.comp g₂) := ext $ λ x, f.map_add (g₁ x.1) (g₂ x.2) theorem fst_eq_coprod : fst R M M₂ = coprod linear_map.id 0 := by ext; simp theorem snd_eq_coprod : snd R M M₂ = coprod 0 linear_map.id := by ext; simp @[simp] theorem coprod_comp_prod (f : M₂ →ₗ[R] M₄) (g : M₃ →ₗ[R] M₄) (f' : M →ₗ[R] M₂) (g' : M →ₗ[R] M₃) : (f.coprod g).comp (f'.prod g') = f.comp f' + g.comp g' := rfl @[simp] lemma coprod_map_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (S : submodule R M) (S' : submodule R M₂) : (submodule.prod S S').map (linear_map.coprod f g) = S.map f ⊔ S'.map g := set_like.coe_injective $ begin simp only [linear_map.coprod_apply, submodule.coe_sup, submodule.map_coe], rw [←set.image2_add, set.image2_image_left, set.image2_image_right], exact set.image_prod (λ m m₂, f m + g m₂), end /-- Taking the product of two maps with the same codomain is equivalent to taking the product of their domains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def coprod_equiv [module S M₃] [smul_comm_class R S M₃] : ((M →ₗ[R] M₃) × (M₂ →ₗ[R] M₃)) ≃ₗ[S] (M × M₂ →ₗ[R] M₃) := { to_fun := λ f, f.1.coprod f.2, inv_fun := λ f, (f.comp (inl _ _ _), f.comp (inr _ _ _)), left_inv := λ f, by simp only [prod.mk.eta, coprod_inl, coprod_inr], right_inv := λ f, by simp only [←comp_coprod, comp_id, coprod_inl_inr], map_add' := λ a b, by { ext, simp only [prod.snd_add, add_apply, coprod_apply, prod.fst_add], ac_refl }, map_smul' := λ r a, by { ext, simp only [smul_add, smul_apply, prod.smul_snd, prod.smul_fst, coprod_apply] } } theorem prod_ext_iff {f g : M × M₂ →ₗ[R] M₃} : f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) := (coprod_equiv ℕ).symm.injective.eq_iff.symm.trans prod.ext_iff /-- Split equality of linear maps from a product into linear maps over each component, to allow `ext` to apply lemmas specific to `M →ₗ M₃` and `M₂ →ₗ M₃`. See note [partially-applied ext lemmas]. -/ @[ext] theorem prod_ext {f g : M × M₂ →ₗ[R] M₃} (hl : f.comp (inl _ _ _) = g.comp (inl _ _ _)) (hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) : f = g := prod_ext_iff.2 ⟨hl, hr⟩ /-- `prod.map` of two linear maps. -/ def prod_map (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : (M × M₂) →ₗ[R] (M₃ × M₄) := (f.comp (fst R M M₂)).prod (g.comp (snd R M M₂)) @[simp] theorem prod_map_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (x) : f.prod_map g x = (f x.1, g x.2) := rfl lemma prod_map_comap_prod (f : M →ₗ[R] M₂) (g : M₃ →ₗ[R] M₄) (S : submodule R M₂) (S' : submodule R M₄) : (submodule.prod S S').comap (linear_map.prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ lemma ker_prod_map (f : M →ₗ[R] M₂) (g : M₃ →ₗ[R] M₄) : (linear_map.prod_map f g).ker = submodule.prod f.ker g.ker := begin dsimp only [ker], rw [←prod_map_comap_prod, submodule.prod_bot], end section map_mul variables {A : Type*} [non_unital_non_assoc_semiring A] [module R A] variables {B : Type*} [non_unital_non_assoc_semiring B] [module R B] lemma inl_map_mul (a₁ a₂ : A) : linear_map.inl R A B (a₁ * a₂) = linear_map.inl R A B a₁ * linear_map.inl R A B a₂ := prod.ext rfl (by simp) lemma inr_map_mul (b₁ b₂ : B) : linear_map.inr R A B (b₁ * b₂) = linear_map.inr R A B b₁ * linear_map.inr R A B b₂ := prod.ext (by simp) rfl end map_mul end linear_map end prod namespace linear_map open submodule variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] [module R M] [module R M₂] [module R M₃] [module R M₄] lemma range_coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (f.coprod g).range = f.range ⊔ g.range := submodule.ext $ λ x, by simp [mem_sup] lemma is_compl_range_inl_inr : is_compl (inl R M M₂).range (inr R M M₂).range := begin split, { rintros ⟨_, _⟩ ⟨⟨x, hx⟩, ⟨y, hy⟩⟩, simp only [prod.ext_iff, inl_apply, inr_apply, mem_bot] at hx hy ⊢, exact ⟨hy.1.symm, hx.2.symm⟩ }, { rintros ⟨x, y⟩ -, simp only [mem_sup, mem_range, exists_prop], refine ⟨(x, 0), ⟨x, rfl⟩, (0, y), ⟨y, rfl⟩, _⟩, simp } end lemma sup_range_inl_inr : (inl R M M₂).range ⊔ (inr R M M₂).range = ⊤ := is_compl_range_inl_inr.sup_eq_top lemma disjoint_inl_inr : disjoint (inl R M M₂).range (inr R M M₂).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] {contextual := tt}; intros; refl theorem map_coprod_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (p : submodule R M) (q : submodule R M₂) : map (coprod f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw set_like.le_def, rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_prod_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (p : submodule R M₂) (q : submodule R M₃) : comap (prod f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R M₂) : p.prod q = p.comap (linear_map.fst R M M₂) ⊓ q.comap (linear_map.snd R M M₂) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R M₂) : p.prod q = p.map (linear_map.inl R M M₂) ⊔ q.map (linear_map.inr R M M₂) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set M₂} : span R (inl R M M₂ '' s ∪ inr R M M₂ '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image] @[simp] lemma ker_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ker (prod f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; refl lemma range_prod_le (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : range (prod f g) ≤ (range f).prod (range g) := begin simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib], rintro _ x rfl, exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ end lemma ker_prod_ker_le_ker_coprod {M₂ : Type*} [add_comm_group M₂] [module R M₂] {M₃ : Type*} [add_comm_group M₃] [module R M₃] (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (ker f).prod (ker g) ≤ ker (f.coprod g) := by { rintros ⟨y, z⟩, simp {contextual := tt} } lemma ker_coprod_of_disjoint_range {M₂ : Type*} [add_comm_group M₂] [module R M₂] {M₃ : Type*} [add_comm_group M₃] [module R M₃] (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (hd : disjoint f.range g.range) : ker (f.coprod g) = (ker f).prod (ker g) := begin apply le_antisymm _ (ker_prod_ker_le_ker_coprod f g), rintros ⟨y, z⟩ h, simp only [mem_ker, mem_prod, coprod_apply] at h ⊢, have : f y ∈ f.range ⊓ g.range, { simp only [true_and, mem_range, mem_inf, exists_apply_eq_apply], use -z, rwa [eq_comm, map_neg, ← sub_eq_zero, sub_neg_eq_add] }, rw [hd.eq_bot, mem_bot] at this, rw [this] at h, simpa [this] using h, end end linear_map namespace submodule open linear_map variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R M₂] lemma sup_eq_range (p q : submodule R M) : p ⊔ q = (p.subtype.coprod q.subtype).range := submodule.ext $ λ x, by simp [submodule.mem_sup, set_like.exists] variables (p : submodule R M) (q : submodule R M₂) @[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ := by { ext ⟨x, y⟩, simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot, exists_eq_left', mem_prod] } @[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M M₂).range = ⊤ := by rw [range_eq_map, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M M₂).range = ⊤ := by rw [range_eq_map, ← prod_top, prod_map_snd] variables (R M M₂) /-- `M` as a submodule of `M × N`. -/ def fst : submodule R (M × M₂) := (⊥ : submodule R M₂).comap (linear_map.snd R M M₂) /-- `M` as a submodule of `M × N` is isomorphic to `M`. -/ @[simps] def fst_equiv : submodule.fst R M M₂ ≃ₗ[R] M := { to_fun := λ x, x.1.1, inv_fun := λ m, ⟨⟨m, 0⟩, by tidy⟩, map_add' := by simp, map_smul' := by simp, left_inv := by tidy, right_inv := by tidy, } lemma fst_map_fst : (submodule.fst R M M₂).map (linear_map.fst R M M₂) = ⊤ := by tidy lemma fst_map_snd : (submodule.fst R M M₂).map (linear_map.snd R M M₂) = ⊥ := by { tidy, exact 0, } /-- `N` as a submodule of `M × N`. -/ def snd : submodule R (M × M₂) := (⊥ : submodule R M).comap (linear_map.fst R M M₂) /-- `N` as a submodule of `M × N` is isomorphic to `N`. -/ @[simps] def snd_equiv : submodule.snd R M M₂ ≃ₗ[R] M₂ := { to_fun := λ x, x.1.2, inv_fun := λ n, ⟨⟨0, n⟩, by tidy⟩, map_add' := by simp, map_smul' := by simp, left_inv := by tidy, right_inv := by tidy, } lemma snd_map_fst : (submodule.snd R M M₂).map (linear_map.fst R M M₂) = ⊥ := by { tidy, exact 0, } lemma snd_map_snd : (submodule.snd R M M₂).map (linear_map.snd R M M₂) = ⊤ := by tidy lemma fst_sup_snd : submodule.fst R M M₂ ⊔ submodule.snd R M M₂ = ⊤ := begin rw eq_top_iff, rintro ⟨m, n⟩ -, rw [show (m, n) = (m, 0) + (0, n), by simp], apply submodule.add_mem (submodule.fst R M M₂ ⊔ submodule.snd R M M₂), { exact submodule.mem_sup_left (submodule.mem_comap.mpr (by simp)), }, { exact submodule.mem_sup_right (submodule.mem_comap.mpr (by simp)), }, end lemma fst_inf_snd : submodule.fst R M M₂ ⊓ submodule.snd R M M₂ = ⊥ := by tidy end submodule namespace linear_equiv section variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables {module_M : module R M} {module_M₂ : module R M₂} variables {module_M₃ : module R M₃} {module_M₄ : module R M₄} variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) /-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/ protected def prod : (M × M₃) ≃ₗ[R] (M₂ × M₄) := { map_add' := λ x y, prod.ext (e₁.map_add _ _) (e₂.map_add _ _), map_smul' := λ c x, prod.ext (e₁.map_smul c _) (e₂.map_smul c _), .. equiv.prod_congr e₁.to_equiv e₂.to_equiv } lemma prod_symm : (e₁.prod e₂).symm = e₁.symm.prod e₂.symm := rfl @[simp] lemma prod_apply (p) : e₁.prod e₂ p = (e₁ p.1, e₂ p.2) := rfl @[simp, norm_cast] lemma coe_prod : (e₁.prod e₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = (e₁ : M →ₗ[R] M₂).prod_map (e₂ : M₃ →ₗ[R] M₄) := rfl end section variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_group M₄] variables {module_M : module R M} {module_M₂ : module R M₂} variables {module_M₃ : module R M₃} {module_M₄ : module R M₄} variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) /-- Equivalence given by a block lower diagonal matrix. `e₁` and `e₂` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ protected def skew_prod (f : M →ₗ[R] M₄) : (M × M₃) ≃ₗ[R] M₂ × M₄ := { inv_fun := λ p : M₂ × M₄, (e₁.symm p.1, e₂.symm (p.2 - f (e₁.symm p.1))), left_inv := λ p, by simp, right_inv := λ p, by simp, .. ((e₁ : M →ₗ[R] M₂).comp (linear_map.fst R M M₃)).prod ((e₂ : M₃ →ₗ[R] M₄).comp (linear_map.snd R M M₃) + f.comp (linear_map.fst R M M₃)) } @[simp] lemma skew_prod_apply (f : M →ₗ[R] M₄) (x) : e₁.skew_prod e₂ f x = (e₁ x.1, e₂ x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (f : M →ₗ[R] M₄) (x) : (e₁.skew_prod e₂ f).symm x = (e₁.symm x.1, e₂.symm (x.2 - f (e₁.symm x.1))) := rfl end end linear_equiv namespace linear_map open submodule variables [ring R] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] /-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of `prod f g` is equal to the product of `range f` and `range g`. -/ lemma range_prod_eq {f : M →ₗ[R] M₂} {g : M →ₗ[R] M₃} (h : ker f ⊔ ker g = ⊤) : range (prod f g) = (range f).prod (range g) := begin refine le_antisymm (f.range_prod_le g) _, simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib, and_imp, prod.forall], rintros _ _ x rfl y rfl, simp only [prod.mk.inj_iff, ← sub_mem_ker_iff], have : y - x ∈ ker f ⊔ ker g, { simp only [h, mem_top] }, rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩, refine ⟨x' + x, _, _⟩, { rwa add_sub_cancel }, { rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub, add_sub_cancel'] } end end linear_map namespace linear_map /-! ## Tunnels and tailings Some preliminary work for establishing the strong rank condition for noetherian rings. Given a morphism `f : M × N →ₗ[R] M` which is `i : injective f`, we can find an infinite decreasing `tunnel f i n` of copies of `M` inside `M`, and sitting beside these, an infinite sequence of copies of `N`. We picturesquely name these as `tailing f i n` for each individual copy of `N`, and `tailings f i n` for the supremum of the first `n+1` copies: they are the pieces left behind, sitting inside the tunnel. By construction, each `tailing f i (n+1)` is disjoint from `tailings f i n`; later, when we assume `M` is noetherian, this implies that `N` must be trivial, and establishes the strong rank condition for any left-noetherian ring. -/ section tunnel -- (This doesn't work over a semiring: we need to use that `submodule R M` is a modular lattice, -- which requires cancellation.) variables [ring R] variables {N : Type*} [add_comm_group M] [module R M] [add_comm_group N] [module R N] open function /-- An auxiliary construction for `tunnel`. The composition of `f`, followed by the isomorphism back to `K`, followed by the inclusion of this submodule back into `M`. -/ def tunnel_aux (f : M × N →ₗ[R] M) (Kφ : Σ K : submodule R M, K ≃ₗ[R] M) : M × N →ₗ[R] M := (Kφ.1.subtype.comp Kφ.2.symm.to_linear_map).comp f lemma tunnel_aux_injective (f : M × N →ₗ[R] M) (i : injective f) (Kφ : Σ K : submodule R M, K ≃ₗ[R] M) : injective (tunnel_aux f Kφ) := (subtype.val_injective.comp Kφ.2.symm.injective).comp i noncomputable theory /-- Auxiliary definition for `tunnel`. -/ -- Even though we have `noncomputable theory`, -- we get an error without another `noncomputable` here. noncomputable def tunnel' (f : M × N →ₗ[R] M) (i : injective f) : ℕ → Σ (K : submodule R M), K ≃ₗ[R] M | 0 := ⟨⊤, linear_equiv.of_top ⊤ rfl⟩ | (n+1) := ⟨(submodule.fst R M N).map (tunnel_aux f (tunnel' n)), ((submodule.fst R M N).equiv_map_of_injective _ (tunnel_aux_injective f i (tunnel' n))).symm.trans (submodule.fst_equiv R M N)⟩ /-- Give an injective map `f : M × N →ₗ[R] M` we can find a nested sequence of submodules all isomorphic to `M`. -/ def tunnel (f : M × N →ₗ[R] M) (i : injective f) : ℕ →ₘ order_dual (submodule R M) := ⟨λ n, (tunnel' f i n).1, monotone_nat_of_le_succ (λ n, begin dsimp [tunnel', tunnel_aux], rw [submodule.map_comp, submodule.map_comp], apply submodule.map_subtype_le, end)⟩ /-- Give an injective map `f : M × N →ₗ[R] M` we can find a sequence of submodules all isomorphic to `N`. -/ def tailing (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : submodule R M := (submodule.snd R M N).map (tunnel_aux f (tunnel' f i n)) /-- Each `tailing f i n` is a copy of `N`. -/ def tailing_linear_equiv (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : tailing f i n ≃ₗ[R] N := ((submodule.snd R M N).equiv_map_of_injective _ (tunnel_aux_injective f i (tunnel' f i n))).symm.trans (submodule.snd_equiv R M N) lemma tailing_le_tunnel (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : tailing f i n ≤ tunnel f i n := begin dsimp [tailing, tunnel_aux], rw [submodule.map_comp, submodule.map_comp], apply submodule.map_subtype_le, end lemma tailing_disjoint_tunnel_succ (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : disjoint (tailing f i n) (tunnel f i (n+1)) := begin rw disjoint_iff, dsimp [tailing, tunnel, tunnel'], rw [submodule.map_inf_eq_map_inf_comap, submodule.comap_map_eq_of_injective (tunnel_aux_injective _ i _), inf_comm, submodule.fst_inf_snd, submodule.map_bot], end lemma tailing_sup_tunnel_succ_le_tunnel (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : tailing f i n ⊔ tunnel f i (n+1) ≤ tunnel f i n := begin dsimp [tailing, tunnel, tunnel', tunnel_aux], rw [←submodule.map_sup, sup_comm, submodule.fst_sup_snd, submodule.map_comp, submodule.map_comp], apply submodule.map_subtype_le, end /-- The supremum of all the copies of `N` found inside the tunnel. -/ def tailings (f : M × N →ₗ[R] M) (i : injective f) : ℕ → submodule R M := partial_sups (tailing f i) @[simp] lemma tailings_zero (f : M × N →ₗ[R] M) (i : injective f) : tailings f i 0 = tailing f i 0 := by simp [tailings] @[simp] lemma tailings_succ (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : tailings f i (n+1) = tailings f i n ⊔ tailing f i (n+1) := by simp [tailings] lemma tailings_disjoint_tunnel (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : disjoint (tailings f i n) (tunnel f i (n+1)) := begin induction n with n ih, { simp only [tailings_zero], apply tailing_disjoint_tunnel_succ, }, { simp only [tailings_succ], refine disjoint.disjoint_sup_left_of_disjoint_sup_right _ _, apply tailing_disjoint_tunnel_succ, apply disjoint.mono_right _ ih, apply tailing_sup_tunnel_succ_le_tunnel, }, end lemma tailings_disjoint_tailing (f : M × N →ₗ[R] M) (i : injective f) (n : ℕ) : disjoint (tailings f i n) (tailing f i (n+1)) := disjoint.mono_right (tailing_le_tunnel f i _) (tailings_disjoint_tunnel f i _) end tunnel end linear_map
6f4687e3b5f24c72c4e4bc632f339f326c3dcbc3
d642a6b1261b2cbe691e53561ac777b924751b63
/src/algebra/order.lean
886a0805ed7365d8af1b3d347584778fed637e1d
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
9,449
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ universe u variables {α : Type u} @[simp] lemma ge_iff_le [preorder α] {a b : α} : a ≥ b ↔ b ≤ a := iff.refl _ @[simp] lemma gt_iff_lt [preorder α] {a b : α} : a > b ↔ b < a := iff.refl _ lemma not_le_of_lt [preorder α] {a b : α} (h : a < b) : ¬ b ≤ a := (le_not_le_of_lt h).right lemma not_lt_of_le [preorder α] {a b : α} (h : a ≤ b) : ¬ b < a | hab := not_le_of_gt hab h lemma le_iff_eq_or_lt [partial_order α] {a b : α} : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or.comm lemma lt_of_le_of_ne' [partial_order α] {a b : α} (h₁ : a ≤ b) (h₂ : a ≠ b) : a < b := lt_of_le_not_le h₁ $ mt (le_antisymm h₁) h₂ lemma lt_iff_le_and_ne [partial_order α] {a b : α} : a < b ↔ a ≤ b ∧ a ≠ b := ⟨λ h, ⟨le_of_lt h, ne_of_lt h⟩, λ ⟨h1, h2⟩, lt_of_le_of_ne h1 h2⟩ lemma eq_iff_le_not_lt [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ ¬ a < b := ⟨λ h, ⟨le_of_eq h, h ▸ lt_irrefl _⟩, λ ⟨h₁, h₂⟩, le_antisymm h₁ $ classical.by_contradiction $ λ h₃, h₂ (lt_of_le_not_le h₁ h₃)⟩ lemma eq_or_lt_of_le [partial_order α] {a b : α} (h : a ≤ b) : a = b ∨ a < b := (lt_or_eq_of_le h).symm lemma lt_of_not_ge' [linear_order α] {a b : α} (h : ¬ b ≤ a) : a < b := lt_of_le_not_le ((le_total _ _).resolve_right h) h lemma lt_iff_not_ge' [linear_order α] {x y : α} : x < y ↔ ¬ y ≤ x := ⟨not_le_of_gt, lt_of_not_ge'⟩ @[simp] lemma not_lt [linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_gt, not_lt_of_ge⟩ lemma le_of_not_lt [linear_order α] {a b : α} : ¬ a < b → b ≤ a := not_lt.1 @[simp] lemma not_le [linear_order α] {a b : α} : ¬ a ≤ b ↔ b < a := lt_iff_not_ge'.symm lemma lt_or_le [linear_order α] : ∀ a b : α, a < b ∨ b ≤ a := lt_or_ge lemma le_or_lt [linear_order α] : ∀ a b : α, a ≤ b ∨ b < a := le_or_gt lemma not_lt_iff_eq_or_lt [linear_order α] {a b : α} : ¬ a < b ↔ a = b ∨ b < a := not_lt.trans $ le_iff_eq_or_lt.trans $ or_congr eq_comm iff.rfl lemma exists_ge_of_linear [linear_order α] (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | or.inl h := ⟨_, h, le_refl _⟩ | or.inr h := ⟨_, le_refl _, h⟩ end lemma lt_imp_lt_of_le_imp_le {β} [linear_order α] [preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_ge' $ λ h', not_lt_of_ge (H h') h lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_gt $ λ h', not_le_of_gt (H h') h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [preorder α] [preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans $ (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [linear_order α] [linear_order β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans $ iff.trans (not_congr H) $ not_le lemma le_iff_le_iff_lt_iff_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ iff.trans (not_congr H) $ not_lt⟩ lemma eq_of_forall_le_iff [partial_order α] {a b : α} (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := le_antisymm ((H _).1 (le_refl _)) ((H _).2 (le_refl _)) lemma le_of_forall_le [preorder α] {a b : α} (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ (le_refl _) lemma le_of_forall_le' [preorder α] {a b : α} (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ (le_refl _) lemma le_of_forall_lt [linear_order α] {a b : α} (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma le_of_forall_lt' [linear_order α] {a b : α} (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma eq_of_forall_ge_iff [partial_order α] {a b : α} (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := le_antisymm ((H _).2 (le_refl _)) ((H _).1 (le_refl _)) /-- monotonicity of `≤` with respect to `→` -/ lemma le_implies_le_of_le_of_le {a b c d : α} [preorder α] (h₀ : c ≤ a) (h₁ : b ≤ d) : a ≤ b → c ≤ d := assume h₂ : a ≤ b, calc c ≤ a : h₀ ... ≤ b : h₂ ... ≤ d : h₁ namespace decidable lemma lt_or_eq_of_le [partial_order α] [@decidable_rel α (≤)] {a b : α} (hab : a ≤ b) : a < b ∨ a = b := if hba : b ≤ a then or.inr (le_antisymm hab hba) else or.inl (lt_of_le_not_le hab hba) lemma eq_or_lt_of_le [partial_order α] [@decidable_rel α (≤)] {a b : α} (hab : a ≤ b) : a = b ∨ a < b := (lt_or_eq_of_le hab).swap lemma le_iff_lt_or_eq [partial_order α] [@decidable_rel α (≤)] {a b : α} : a ≤ b ↔ a < b ∨ a = b := ⟨lt_or_eq_of_le, le_of_lt_or_eq⟩ lemma le_of_not_lt [decidable_linear_order α] {a b : α} (h : ¬ b < a) : a ≤ b := decidable.by_contradiction $ λ h', h $ lt_of_le_not_le ((le_total _ _).resolve_right h') h' lemma not_lt [decidable_linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_lt, not_lt_of_ge⟩ lemma lt_or_le [decidable_linear_order α] (a b : α) : a < b ∨ b ≤ a := if hba : b ≤ a then or.inr hba else or.inl $ not_le.1 hba lemma le_or_lt [decidable_linear_order α] (a b : α) : a ≤ b ∨ b < a := (lt_or_le b a).swap lemma lt_trichotomy [decidable_linear_order α] (a b : α) : a < b ∨ a = b ∨ b < a := (lt_or_le _ _).imp_right $ λ h, (eq_or_lt_of_le h).imp_left eq.symm lemma lt_or_gt_of_ne [decidable_linear_order α] {a b : α} (h : a ≠ b) : a < b ∨ b < a := (lt_trichotomy a b).imp_right $ λ h', h'.resolve_left h def lt_by_cases [decidable_linear_order α] (x y : α) {P : Sort*} (h₁ : x < y → P) (h₂ : x = y → P) (h₃ : y < x → P) : P := begin by_cases h : x < y, { exact h₁ h }, by_cases h' : y < x, { exact h₃ h' }, apply h₂, apply le_antisymm; apply le_of_not_gt; assumption end lemma ne_iff_lt_or_gt [decidable_linear_order α] {a b : α} : a ≠ b ↔ a < b ∨ b < a := ⟨lt_or_gt_of_ne, λo, o.elim ne_of_lt ne_of_gt⟩ lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [decidable_linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_lt $ λ h', not_le_of_gt (H h') h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [decidable_linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma le_iff_le_iff_lt_iff_lt {β} [decidable_linear_order α] [decidable_linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ iff.trans (not_congr H) $ not_lt⟩ lemma min_le_max [decidable_linear_order α] (a b : α) : min a b ≤ max a b := le_trans (min_le_left a b) (le_max_left a b) end decidable namespace ordering /-- `compares o a b` means that `a` and `b` have the ordering relation `o` between them, assuming that the relation `a < b` is defined -/ @[simp] def compares [has_lt α] : ordering → α → α → Prop | lt a b := a < b | eq a b := a = b | gt a b := a > b theorem compares.eq_lt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = lt ↔ a < b) | lt a b h := ⟨λ _, h, λ _, rfl⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h' h).elim⟩ | gt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ theorem compares.eq_eq [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = eq ↔ a = b) | lt a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h h').elim⟩ | eq a b h := ⟨λ _, h, λ _, rfl⟩ | gt a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h h').elim⟩ theorem compares.eq_gt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = gt ↔ a > b) | lt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h' h).elim⟩ | gt a b h := ⟨λ _, h, λ _, rfl⟩ theorem compares.inj [preorder α] {o₁} : ∀ {o₂} {a b : α}, compares o₁ a b → compares o₂ a b → o₁ = o₂ | lt a b h₁ h₂ := h₁.eq_lt.2 h₂ | eq a b h₁ h₂ := h₁.eq_eq.2 h₂ | gt a b h₁ h₂ := h₁.eq_gt.2 h₂ theorem swap_or_else (o₁ o₂) : (or_else o₁ o₂).swap = or_else o₁.swap o₂.swap := by cases o₁; try {refl}; cases o₂; refl theorem or_else_eq_lt (o₁ o₂) : or_else o₁ o₂ = lt ↔ o₁ = lt ∨ (o₁ = eq ∧ o₂ = lt) := by cases o₁; cases o₂; exact dec_trivial end ordering theorem cmp_compares [decidable_linear_order α] (a b : α) : (cmp a b).compares a b := begin unfold cmp cmp_using, by_cases a < b; simp [h], by_cases h₂ : b < a; simp [h₂, gt], exact (lt_or_eq_of_le (le_of_not_gt h₂)).resolve_left h end theorem cmp_swap [preorder α] [@decidable_rel α (<)] (a b : α) : (cmp a b).swap = cmp b a := begin unfold cmp cmp_using, by_cases a < b; by_cases h₂ : b < a; simp [h, h₂, gt, ordering.swap], exact lt_asymm h h₂ end
b1dcade54580e73bfab2a0fb597d5dc97f917b8d
e61a235b8468b03aee0120bf26ec615c045005d2
/src/Init/Data/PersistentHashMap/Basic.lean
69ba4d189dd363dbde72f8d5901f6becbc85fe27
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,435
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 -/ prelude import Init.Data.Array import Init.Data.Hashable universes u v w w' namespace PersistentHashMap inductive Entry (α : Type u) (β : Type v) (σ : Type w) | entry (key : α) (val : β) : Entry | ref (node : σ) : Entry | null : Entry instance Entry.inhabited {α β σ} : Inhabited (Entry α β σ) := ⟨Entry.null⟩ inductive Node (α : Type u) (β : Type v) : Type (max u v) | entries (es : Array (Entry α β Node)) : Node | collision (ks : Array α) (vs : Array β) (h : ks.size = vs.size) : Node instance Node.inhabited {α β} : Inhabited (Node α β) := ⟨Node.entries #[]⟩ abbrev shift : USize := 5 abbrev branching : USize := USize.ofNat (2 ^ shift.toNat) abbrev maxDepth : USize := 7 abbrev maxCollisions : Nat := 4 def mkEmptyEntriesArray {α β} : Array (Entry α β (Node α β)) := (Array.mkArray PersistentHashMap.branching.toNat PersistentHashMap.Entry.null) end PersistentHashMap structure PersistentHashMap (α : Type u) (β : Type v) [HasBeq α] [Hashable α] := (root : PersistentHashMap.Node α β := PersistentHashMap.Node.entries PersistentHashMap.mkEmptyEntriesArray) (size : Nat := 0) abbrev PHashMap (α : Type u) (β : Type v) [HasBeq α] [Hashable α] := PersistentHashMap α β namespace PersistentHashMap variables {α : Type u} {β : Type v} def empty [HasBeq α] [Hashable α] : PersistentHashMap α β := {} instance [HasBeq α] [Hashable α] : HasEmptyc (PersistentHashMap α β) := ⟨empty⟩ def isEmpty [HasBeq α] [Hashable α] (m : PersistentHashMap α β) : Bool := m.size == 0 instance [HasBeq α] [Hashable α] : Inhabited (PersistentHashMap α β) := ⟨{}⟩ def mkEmptyEntries {α β} : Node α β := Node.entries mkEmptyEntriesArray abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1) inductive IsCollisionNode : Node α β → Prop | mk (keys : Array α) (vals : Array β) (h : keys.size = vals.size) : IsCollisionNode (Node.collision keys vals h) abbrev CollisionNode (α β) := { n : Node α β // IsCollisionNode n } inductive IsEntriesNode : Node α β → Prop | mk (entries : Array (Entry α β (Node α β))) : IsEntriesNode (Node.entries entries) abbrev EntriesNode (α β) := { n : Node α β // IsEntriesNode n } private theorem setSizeEq {ks : Array α} {vs : Array β} (h : ks.size = vs.size) (i : Fin ks.size) (j : Fin vs.size) (k : α) (v : β) : (ks.set i k).size = (vs.set j v).size := have h₁ : (ks.set i k).size = ks.size from Array.szFSetEq _ _ _; have h₂ : (vs.set j v).size = vs.size from Array.szFSetEq _ _ _; (h₁.trans h).trans h₂.symm private theorem pushSizeEq {ks : Array α} {vs : Array β} (h : ks.size = vs.size) (k : α) (v : β) : (ks.push k).size = (vs.push v).size := have h₁ : (ks.push k).size = ks.size + 1 from Array.szPushEq _ _; have h₂ : (vs.push v).size = vs.size + 1 from Array.szPushEq _ _; have h₃ : ks.size + 1 = vs.size + 1 from h ▸ rfl; (h₁.trans h₃).trans h₂.symm partial def insertAtCollisionNodeAux [HasBeq α] : CollisionNode α β → Nat → α → β → CollisionNode α β | n@⟨Node.collision keys vals heq, _⟩, i, k, v => if h : i < keys.size then let idx : Fin keys.size := ⟨i, h⟩; let k' := keys.get idx; if k == k' then let j : Fin vals.size := ⟨i, heq ▸ h⟩; ⟨Node.collision (keys.set idx k) (vals.set j v) (setSizeEq heq idx j k v), IsCollisionNode.mk _ _ _⟩ else insertAtCollisionNodeAux n (i+1) k v else ⟨Node.collision (keys.push k) (vals.push v) (pushSizeEq heq k v), IsCollisionNode.mk _ _ _⟩ | ⟨Node.entries _, h⟩, _, _, _ => False.elim (nomatch h) def insertAtCollisionNode [HasBeq α] : CollisionNode α β → α → β → CollisionNode α β := fun n k v => insertAtCollisionNodeAux n 0 k v def getCollisionNodeSize : CollisionNode α β → Nat | ⟨Node.collision keys _ _, _⟩ => keys.size | ⟨Node.entries _, h⟩ => False.elim (nomatch h) def mkCollisionNode (k₁ : α) (v₁ : β) (k₂ : α) (v₂ : β) : Node α β := let ks : Array α := Array.mkEmpty maxCollisions; let ks := (ks.push k₁).push k₂; let vs : Array β := Array.mkEmpty maxCollisions; let vs := (vs.push v₁).push v₂; Node.collision ks vs rfl partial def insertAux [HasBeq α] [Hashable α] : Node α β → USize → USize → α → β → Node α β | Node.collision keys vals heq, _, depth, k, v => let newNode := insertAtCollisionNode ⟨Node.collision keys vals heq, IsCollisionNode.mk _ _ _⟩ k v; if depth >= maxDepth || getCollisionNodeSize newNode < maxCollisions then newNode.val else match newNode with | ⟨Node.entries _, h⟩ => False.elim (nomatch h) | ⟨Node.collision keys vals heq, _⟩ => let entries : Node α β := mkEmptyEntries; keys.iterate entries $ fun i k entries => let v := vals.get ⟨i.val, heq ▸ i.isLt⟩; let h := hash k; -- dbgTrace ("toCollision " ++ toString i ++ ", h: " ++ toString h ++ ", depth: " ++ toString depth ++ ", h': " ++ -- toString (div2Shift h (shift * (depth - 1)))) $ fun _ => let h := div2Shift h (shift * (depth - 1)); insertAux entries h depth k v | Node.entries entries, h, depth, k, v => let j := (mod2Shift h shift).toNat; Node.entries $ entries.modify j $ fun entry => match entry with | Entry.null => Entry.entry k v | Entry.ref node => Entry.ref $ insertAux node (div2Shift h shift) (depth+1) k v | Entry.entry k' v' => if k == k' then Entry.entry k v else Entry.ref $ mkCollisionNode k' v' k v def insert [HasBeq α] [Hashable α] : PersistentHashMap α β → α → β → PersistentHashMap α β | { root := n, size := sz }, k, v => { root := insertAux n (hash k) 1 k v, size := sz + 1 } partial def findAtAux [HasBeq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) : Nat → α → Option β | i, k => if h : i < keys.size then let k' := keys.get ⟨i, h⟩; if k == k' then some (vals.get ⟨i, heq ▸ h⟩) else findAtAux (i+1) k else none partial def findAux [HasBeq α] : Node α β → USize → α → Option β | Node.entries entries, h, k => let j := (mod2Shift h shift).toNat; match entries.get! j with | Entry.null => none | Entry.ref node => findAux node (div2Shift h shift) k | Entry.entry k' v => if k == k' then some v else none | Node.collision keys vals heq, _, k => findAtAux keys vals heq 0 k def find? [HasBeq α] [Hashable α] : PersistentHashMap α β → α → Option β | { root := n, .. }, k => findAux n (hash k) k @[inline] def getOp [HasBeq α] [Hashable α] (self : PersistentHashMap α β) (idx : α) : Option β := self.find? idx @[inline] def findD [HasBeq α] [Hashable α] (m : PersistentHashMap α β) (a : α) (b₀ : β) : β := (m.find? a).getD b₀ @[inline] def find! [HasBeq α] [Hashable α] [Inhabited β] (m : PersistentHashMap α β) (a : α) : β := match m.find? a with | some b => b | none => panic! "key is not in the map" partial def findEntryAtAux [HasBeq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) : Nat → α → Option (α × β) | i, k => if h : i < keys.size then let k' := keys.get ⟨i, h⟩; if k == k' then some (k', vals.get ⟨i, heq ▸ h⟩) else findEntryAtAux (i+1) k else none partial def findEntryAux [HasBeq α] : Node α β → USize → α → Option (α × β) | Node.entries entries, h, k => let j := (mod2Shift h shift).toNat; match entries.get! j with | Entry.null => none | Entry.ref node => findEntryAux node (div2Shift h shift) k | Entry.entry k' v => if k == k' then some (k', v) else none | Node.collision keys vals heq, _, k => findEntryAtAux keys vals heq 0 k def findEntry? [HasBeq α] [Hashable α] : PersistentHashMap α β → α → Option (α × β) | { root := n, .. }, k => findEntryAux n (hash k) k partial def containsAtAux [HasBeq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) : Nat → α → Bool | i, k => if h : i < keys.size then let k' := keys.get ⟨i, h⟩; if k == k' then true else containsAtAux (i+1) k else false partial def containsAux [HasBeq α] : Node α β → USize → α → Bool | Node.entries entries, h, k => let j := (mod2Shift h shift).toNat; match entries.get! j with | Entry.null => false | Entry.ref node => containsAux node (div2Shift h shift) k | Entry.entry k' v => k == k' | Node.collision keys vals heq, _, k => containsAtAux keys vals heq 0 k def contains [HasBeq α] [Hashable α] : PersistentHashMap α β → α → Bool | { root := n, .. }, k => containsAux n (hash k) k partial def isUnaryEntries (a : Array (Entry α β (Node α β))) : Nat → Option (α × β) → Option (α × β) | i, acc => if h : i < a.size then match a.get ⟨i, h⟩ with | Entry.null => isUnaryEntries (i+1) acc | Entry.ref _ => none | Entry.entry k v => match acc with | none => isUnaryEntries (i+1) (some (k, v)) | some _ => none else acc def isUnaryNode : Node α β → Option (α × β) | Node.entries entries => isUnaryEntries entries 0 none | Node.collision keys vals heq => if h : 1 = keys.size then have 0 < keys.size from h ▸ (Nat.zeroLtSucc _); some (keys.get ⟨0, this⟩, vals.get ⟨0, heq ▸ this⟩) else none partial def eraseAux [HasBeq α] : Node α β → USize → α → Node α β × Bool | n@(Node.collision keys vals heq), _, k => match keys.indexOf k with | some idx => let ⟨keys', keq⟩ := keys.eraseIdx' idx; let ⟨vals', veq⟩ := vals.eraseIdx' (Eq.rec idx heq); have keys.size - 1 = vals.size - 1 from heq ▸ rfl; (Node.collision keys' vals' (keq.trans (this.trans veq.symm)), true) | none => (n, false) | n@(Node.entries entries), h, k => let j := (mod2Shift h shift).toNat; let entry := entries.get! j; match entry with | Entry.null => (n, false) | Entry.entry k' v => if k == k' then (Node.entries (entries.set! j Entry.null), true) else (n, false) | Entry.ref node => let entries := entries.set! j Entry.null; let (newNode, deleted) := eraseAux node (div2Shift h shift) k; if !deleted then (n, false) else match isUnaryNode newNode with | none => (Node.entries (entries.set! j (Entry.ref newNode)), true) | some (k, v) => (Node.entries (entries.set! j (Entry.entry k v)), true) def erase [HasBeq α] [Hashable α] : PersistentHashMap α β → α → PersistentHashMap α β | { root := n, size := sz }, k => let h := hash k; let (n, del) := eraseAux n h k; { root := n, size := if del then sz - 1 else sz } section variables {m : Type w → Type w'} [Monad m] variables {σ : Type w} @[specialize] partial def foldlMAux (f : σ → α → β → m σ) : Node α β → σ → m σ | Node.collision keys vals heq, acc => keys.iterateM acc $ fun i k acc => f acc k (vals.get ⟨i.val, heq ▸ i.isLt⟩) | Node.entries entries, acc => entries.foldlM (fun acc entry => match entry with | Entry.null => pure acc | Entry.entry k v => f acc k v | Entry.ref node => foldlMAux node acc) acc @[specialize] def foldlM [HasBeq α] [Hashable α] (map : PersistentHashMap α β) (f : σ → α → β → m σ) (acc : σ) : m σ := foldlMAux f map.root acc @[specialize] def forM [HasBeq α] [Hashable α] (map : PersistentHashMap α β) (f : α → β → m PUnit) : m PUnit := map.foldlM (fun _ => f) ⟨⟩ @[specialize] def foldl [HasBeq α] [Hashable α] (map : PersistentHashMap α β) (f : σ → α → β → σ) (acc : σ) : σ := Id.run $ map.foldlM f acc end def toList [HasBeq α] [Hashable α] (m : PersistentHashMap α β) : List (α × β) := m.foldl (fun ps k v => (k, v) :: ps) [] structure Stats := (numNodes : Nat := 0) (numNull : Nat := 0) (numCollisions : Nat := 0) (maxDepth : Nat := 0) partial def collectStats : Node α β → Stats → Nat → Stats | Node.collision keys _ _, stats, depth => { stats with numNodes := stats.numNodes + 1, numCollisions := stats.numCollisions + keys.size - 1, maxDepth := Nat.max stats.maxDepth depth } | Node.entries entries, stats, depth => let stats := { stats with numNodes := stats.numNodes + 1, maxDepth := Nat.max stats.maxDepth depth }; entries.foldl (fun stats entry => match entry with | Entry.null => { stats with numNull := stats.numNull + 1 } | Entry.ref node => collectStats node stats (depth + 1) | Entry.entry _ _ => stats) stats def stats [HasBeq α] [Hashable α] (m : PersistentHashMap α β) : Stats := collectStats m.root {} 1 def Stats.toString (s : Stats) : String := "{ nodes := " ++ toString s.numNodes ++ ", null := " ++ toString s.numNull ++ ", collisions := " ++ toString s.numCollisions ++ ", depth := " ++ toString s.maxDepth ++ "}" instance : HasToString Stats := ⟨Stats.toString⟩ end PersistentHashMap
c5b7f458112229c8a8b5fec7cf2b52b780727ba0
4727251e0cd73359b15b664c3170e5d754078599
/test/delta_instance.lean
41110f7e75785a4bd2f559e500bd22b122f3b137
[ "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
955
lean
/- Copyright (c) 2019 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.set import algebra.category.Mon.basic def X : Type := set ℕ instance : has_coe_to_sort X Type := set.has_coe_to_sort @[derive ring] def T := ℤ class binclass (T1 T2 : Type) instance : binclass ℤ ℤ := ⟨⟩ @[derive [ring, binclass ℤ]] def U := ℤ @[derive λ α, binclass α ℤ] def V := ℤ -- test instance naming example := U.ring example := U.binclass example := V.binclass @[derive ring] def id_ring (α) [ring α] : Type := α @[derive decidable_eq] def S := ℕ @[derive decidable_eq] inductive P | a | b | c open category_theory -- Test that `delta_instance` works in the presence of universe metavariables. attribute [derive large_category] Mon -- test deriving instances on function types @[derive monad] meta def my_tactic : Type → Type := tactic
170889bc966555da9cebcf3930c086ec8d1a3e23
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/order_iso_nat.lean
5ff7e647a673b02f69da67a05cf728a782d5cd19
[ "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
8,949
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 data.equiv.denumerable import order.preorder_hom import data.nat.lattice /-! # Relation embeddings from the naturals This file allows translation from monotone functions `ℕ → α` to order embeddings `ℕ ↪ α` and defines the limit value of an eventually-constant sequence. ## Main declarations * `nat_lt`/`nat_gt`: Make an order embedding `ℕ ↪ α` from an increasing/decreasing function `ℕ → α`. * `monotonic_sequence_limit`: The limit of an eventually-constant monotone sequence `ℕ →ₘ α`. * `monotonic_sequence_limit_index`: The index of the first occurence of `monotonic_sequence_limit` in the sequence. -/ namespace rel_embedding variables {α : Type*} {r : α → α → Prop} [is_strict_order α r] /-- If `f` is a strictly `r`-increasing sequence, then this returns `f` as an order embedding. -/ def nat_lt (f : ℕ → α) (H : ∀ n : ℕ, r (f n) (f (n + 1))) : ((<) : ℕ → ℕ → Prop) ↪r r := of_monotone f $ λ a b h, begin induction b with b IH, {exact (nat.not_lt_zero _ h).elim}, cases nat.lt_succ_iff_lt_or_eq.1 h with h e, { exact trans (IH h) (H _) }, { subst b, apply H } end @[simp] lemma nat_lt_apply {f : ℕ → α} {H : ∀ n : ℕ, r (f n) (f (n + 1))} {n : ℕ} : nat_lt f H n = f n := rfl /-- If `f` is a strictly `r`-decreasing sequence, then this returns `f` as an order embedding. -/ def nat_gt (f : ℕ → α) (H : ∀ n : ℕ, r (f (n + 1)) (f n)) : ((>) : ℕ → ℕ → Prop) ↪r r := by haveI := is_strict_order.swap r; exact rel_embedding.swap (nat_lt f H) theorem well_founded_iff_no_descending_seq : well_founded r ↔ is_empty (((>) : ℕ → ℕ → Prop) ↪r r) := ⟨λ ⟨h⟩, ⟨λ ⟨f, o⟩, suffices ∀ a, acc r a → ∀ n, a ≠ f n, from this (f 0) (h _) 0 rfl, λ a ac, begin induction ac with a _ IH, intros n h, subst a, exact IH (f (n+1)) (o.2 (nat.lt_succ_self _)) _ rfl end⟩, λ E, ⟨λ a, classical.by_contradiction $ λ na, let ⟨f, h⟩ := classical.axiom_of_choice $ show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1, from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $ ⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in E.elim' (nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n, by { rw [function.iterate_succ'], apply h })⟩⟩ end rel_embedding namespace nat variables (s : set ℕ) [decidable_pred (∈ s)] [infinite s] /-- An order embedding from `ℕ` to itself with a specified range -/ def order_embedding_of_set : ℕ ↪o ℕ := (rel_embedding.order_embedding_of_lt_embedding (rel_embedding.nat_lt (nat.subtype.of_nat s) (λ n, nat.subtype.lt_succ_self _))).trans (order_embedding.subtype s) /-- `nat.subtype.of_nat` as an order isomorphism between `ℕ` and an infinite decidable subset. -/ noncomputable def subtype.order_iso_of_nat : ℕ ≃o s := rel_iso.of_surjective (rel_embedding.order_embedding_of_lt_embedding (rel_embedding.nat_lt (nat.subtype.of_nat s) (λ n, nat.subtype.lt_succ_self _))) nat.subtype.of_nat_surjective variable {s} @[simp] lemma order_embedding_of_set_apply {n : ℕ} : order_embedding_of_set s n = subtype.of_nat s n := rfl @[simp] lemma subtype.order_iso_of_nat_apply {n : ℕ} : subtype.order_iso_of_nat s n = subtype.of_nat s n := by { simp [subtype.order_iso_of_nat] } variable (s) @[simp] lemma order_embedding_of_set_range : set.range (nat.order_embedding_of_set s) = s := begin ext x, rw [set.mem_range, nat.order_embedding_of_set], split; intro h, { obtain ⟨y, rfl⟩ := h, simp }, { refine ⟨(nat.subtype.order_iso_of_nat s).symm ⟨x, h⟩, _⟩, simp only [rel_embedding.coe_trans, rel_embedding.order_embedding_of_lt_embedding_apply, rel_embedding.nat_lt_apply, function.comp_app, order_embedding.subtype_apply], rw [← subtype.order_iso_of_nat_apply, order_iso.apply_symm_apply, subtype.coe_mk] } end end nat theorem exists_increasing_or_nonincreasing_subseq' {α : Type*} (r : α → α → Prop) (f : ℕ → α) : ∃ (g : ℕ ↪o ℕ), (∀ n : ℕ, r (f (g n)) (f (g (n + 1)))) ∨ (∀ m n : ℕ, m < n → ¬ r (f (g m)) (f (g n))) := begin classical, let bad : set ℕ := { m | ∀ n, m < n → ¬ r (f m) (f n) }, by_cases hbad : infinite bad, { haveI := hbad, refine ⟨nat.order_embedding_of_set bad, or.intro_right _ (λ m n mn, _)⟩, have h := set.mem_range_self m, rw nat.order_embedding_of_set_range bad at h, exact h _ ((order_embedding.lt_iff_lt _).2 mn) }, { rw [set.infinite_coe_iff, set.infinite, not_not] at hbad, obtain ⟨m, hm⟩ : ∃ m, ∀ n, m ≤ n → ¬ n ∈ bad, { by_cases he : hbad.to_finset.nonempty, { refine ⟨(hbad.to_finset.max' he).succ, λ n hn nbad, nat.not_succ_le_self _ (hn.trans (hbad.to_finset.le_max' n (hbad.mem_to_finset.2 nbad)))⟩ }, { exact ⟨0, λ n hn nbad, he ⟨n, hbad.mem_to_finset.2 nbad⟩⟩ } }, have h : ∀ (n : ℕ), ∃ (n' : ℕ), n < n' ∧ r (f (n + m)) (f (n' + m)), { intro n, have h := hm _ (le_add_of_nonneg_left n.zero_le), simp only [exists_prop, not_not, set.mem_set_of_eq, not_forall] at h, obtain ⟨n', hn1, hn2⟩ := h, obtain ⟨x, hpos, rfl⟩ := exists_pos_add_of_lt hn1, refine ⟨n + x, add_lt_add_left hpos n, _⟩, rw [add_assoc, add_comm x m, ← add_assoc], exact hn2 }, let g' : ℕ → ℕ := @nat.rec (λ _, ℕ) m (λ n gn, nat.find (h gn)), exact ⟨(rel_embedding.nat_lt (λ n, g' n + m) (λ n, nat.add_lt_add_right (nat.find_spec (h (g' n))).1 m)).order_embedding_of_lt_embedding, or.intro_left _ (λ n, (nat.find_spec (h (g' n))).2)⟩ } end theorem exists_increasing_or_nonincreasing_subseq {α : Type*} (r : α → α → Prop) [is_trans α r] (f : ℕ → α) : ∃ (g : ℕ ↪o ℕ), (∀ m n : ℕ, m < n → r (f (g m)) (f (g n))) ∨ (∀ m n : ℕ, m < n → ¬ r (f (g m)) (f (g n))) := begin obtain ⟨g, hr | hnr⟩ := exists_increasing_or_nonincreasing_subseq' r f, { refine ⟨g, or.intro_left _ (λ m n mn, _)⟩, obtain ⟨x, rfl⟩ := le_iff_exists_add.1 (nat.succ_le_iff.2 mn), induction x with x ih, { apply hr }, { apply is_trans.trans _ _ _ _ (hr _), exact ih (lt_of_lt_of_le m.lt_succ_self (nat.le_add_right _ _)) } }, { exact ⟨g, or.intro_right _ hnr⟩ } end /-- The "monotone chain condition" below is sometimes a convenient form of well foundedness. -/ lemma well_founded.monotone_chain_condition (α : Type*) [partial_order α] : well_founded ((>) : α → α → Prop) ↔ ∀ (a : ℕ →ₘ α), ∃ n, ∀ m, n ≤ m → a n = a m := begin split; intros h, { rw well_founded.well_founded_iff_has_max' at h, intros a, have hne : (set.range a).nonempty, { use a 0, simp, }, obtain ⟨x, ⟨n, hn⟩, range_bounded⟩ := h _ hne, use n, intros m hm, rw ← hn at range_bounded, symmetry, apply range_bounded (a m) (set.mem_range_self _) (a.monotone hm), }, { rw rel_embedding.well_founded_iff_no_descending_seq, refine ⟨λ a, _⟩, obtain ⟨n, hn⟩ := h (a.swap : ((<) : ℕ → ℕ → Prop) →r ((<) : α → α → Prop)).to_preorder_hom, exact n.succ_ne_self.symm (rel_embedding.to_preorder_hom_injective _ (hn _ n.le_succ)), }, end /-- Given an eventually-constant monotone sequence `a₀ ≤ a₁ ≤ a₂ ≤ ...` in a partially-ordered type, `monotonic_sequence_limit_index a` is the least natural number `n` for which `aₙ` reaches the constant value. For sequences that are not eventually constant, `monotonic_sequence_limit_index a` is defined, but is a junk value. -/ noncomputable def monotonic_sequence_limit_index {α : Type*} [partial_order α] (a : ℕ →ₘ α) : ℕ := Inf { n | ∀ m, n ≤ m → a n = a m } /-- The constant value of an eventually-constant monotone sequence `a₀ ≤ a₁ ≤ a₂ ≤ ...` in a partially-ordered type. -/ noncomputable def monotonic_sequence_limit {α : Type*} [partial_order α] (a : ℕ →ₘ α) := a (monotonic_sequence_limit_index a) lemma well_founded.supr_eq_monotonic_sequence_limit {α : Type*} [complete_lattice α] (h : well_founded ((>) : α → α → Prop)) (a : ℕ →ₘ α) : (⨆ m, a m) = monotonic_sequence_limit a := begin suffices : (⨆ (m : ℕ), a m) ≤ monotonic_sequence_limit a, { exact le_antisymm this (le_supr a _), }, apply supr_le, intros m, by_cases hm : m ≤ monotonic_sequence_limit_index a, { exact a.monotone hm, }, { replace hm := le_of_not_le hm, let S := { n | ∀ m, n ≤ m → a n = a m }, have hInf : Inf S ∈ S, { refine nat.Inf_mem _, rw well_founded.monotone_chain_condition at h, exact h a, }, change Inf S ≤ m at hm, change a m ≤ a (Inf S), rw hInf m hm, }, end
362dd29c89515dc97fcf285e186dfd11f17e6f58
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/analysis/normed/group/basic.lean
a7a303642196709461982d92c40119a0d3560e62
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
43,272
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import order.liminf_limsup import topology.algebra.uniform_group import topology.metric_space.algebra import topology.metric_space.isometry import topology.sequences /-! # Normed (semi)groups In this file we define four classes: * `has_norm`, `has_nnnorm`: auxiliary classes endowing a type `α` with a function `norm : α → ℝ` (notation: `∥x∥`) and `nnnorm : α → ℝ≥0` (notation: `∥x∥₊`), respectively; * `semi_normed_group`: a seminormed group is an additive group with a norm and a pseudo metric space structures that agree with each other: `∀ x y, dist x y = ∥x - y∥`; * `normed_group`: a normed group is an additive group with a norm and a metric space structures that agree with each other: `∀ x y, dist x y = ∥x - y∥`. We also prove basic properties of (semi)normed groups and provide some instances. ## Tags normed group -/ variables {α ι E F : Type*} open filter metric open_locale topological_space big_operators nnreal ennreal uniformity pointwise /-- Auxiliary class, endowing a type `E` with a function `norm : E → ℝ`. This class is designed to be extended in more interesting classes specifying the properties of the norm. -/ class has_norm (E : Type*) := (norm : E → ℝ) export has_norm (norm) notation `∥` e `∥` := norm e /-- A seminormed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines a pseudometric space structure. -/ class semi_normed_group (E : Type*) extends has_norm E, add_comm_group E, pseudo_metric_space E := (dist_eq : ∀ x y : E, dist x y = norm (x - y)) /-- A normed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines a metric space structure. -/ class normed_group (E : Type*) extends has_norm E, add_comm_group E, metric_space E := (dist_eq : ∀ x y : E, dist x y = norm (x - y)) /-- A normed group is a seminormed group. -/ @[priority 100] -- see Note [lower instance priority] instance normed_group.to_semi_normed_group [h : normed_group E] : semi_normed_group E := { .. h } /-- Construct a seminormed group from a translation invariant pseudodistance. -/ def semi_normed_group.of_add_dist [has_norm E] [add_comm_group E] [pseudo_metric_space E] (H1 : ∀ x : E, ∥x∥ = dist x 0) (H2 : ∀ x y z : E, dist x y ≤ dist (x + z) (y + z)) : semi_normed_group E := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }, { have := H2 (x - y) 0 y, rwa [sub_add_cancel, zero_add] at this } end } /-- Construct a seminormed group from a translation invariant pseudodistance -/ def semi_normed_group.of_add_dist' [has_norm E] [add_comm_group E] [pseudo_metric_space E] (H1 : ∀ x : E, ∥x∥ = dist x 0) (H2 : ∀ x y z : E, dist (x + z) (y + z) ≤ dist x y) : semi_normed_group E := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { have := H2 (x - y) 0 y, rwa [sub_add_cancel, zero_add] at this }, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 } end } /-- A seminormed group can be built from a seminorm that satisfies algebraic properties. This is formalised in this structure. -/ structure semi_normed_group.core (E : Type*) [add_comm_group E] [has_norm E] : Prop := (norm_zero : ∥(0 : E)∥ = 0) (triangle : ∀ x y : E, ∥x + y∥ ≤ ∥x∥ + ∥y∥) (norm_neg : ∀ x : E, ∥-x∥ = ∥x∥) /-- Constructing a seminormed group from core properties of a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`). -/ def semi_normed_group.of_core (E : Type*) [add_comm_group E] [has_norm E] (C : semi_normed_group.core E) : semi_normed_group E := { dist := λ x y, ∥x - y∥, dist_eq := assume x y, by refl, dist_self := assume x, by simp [C.norm_zero], dist_triangle := assume x y z, calc ∥x - z∥ = ∥x - y + (y - z)∥ : by rw sub_add_sub_cancel ... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _, dist_comm := assume x y, calc ∥x - y∥ = ∥ -(y - x)∥ : by simp ... = ∥y - x∥ : by { rw [C.norm_neg] } } instance : normed_group punit := { norm := function.const _ 0, dist_eq := λ _ _, rfl, } @[simp] lemma punit.norm_eq_zero (r : punit) : ∥r∥ = 0 := rfl noncomputable instance : normed_group ℝ := { norm := λ x, |x|, dist_eq := assume x y, rfl } lemma real.norm_eq_abs (r : ℝ) : ∥r∥ = |r| := rfl section semi_normed_group variables [semi_normed_group E] [semi_normed_group F] lemma dist_eq_norm (g h : E) : dist g h = ∥g - h∥ := semi_normed_group.dist_eq _ _ lemma dist_eq_norm' (g h : E) : dist g h = ∥h - g∥ := by rw [dist_comm, dist_eq_norm] @[simp] lemma dist_zero_right (g : E) : dist g 0 = ∥g∥ := by rw [dist_eq_norm, sub_zero] @[simp] lemma dist_zero_left : dist (0 : E) = norm := funext $ λ g, by rw [dist_comm, dist_zero_right] lemma tendsto_norm_cocompact_at_top [proper_space E] : tendsto norm (cocompact E) at_top := by simpa only [dist_zero_right] using tendsto_dist_right_cocompact_at_top (0 : E) lemma norm_sub_rev (g h : E) : ∥g - h∥ = ∥h - g∥ := by simpa only [dist_eq_norm] using dist_comm g h @[simp] lemma norm_neg (g : E) : ∥-g∥ = ∥g∥ := by simpa using norm_sub_rev 0 g @[simp] lemma dist_add_left (g h₁ h₂ : E) : dist (g + h₁) (g + h₂) = dist h₁ h₂ := by simp [dist_eq_norm] @[simp] lemma dist_add_right (g₁ g₂ h : E) : dist (g₁ + h) (g₂ + h) = dist g₁ g₂ := by simp [dist_eq_norm] @[simp] lemma dist_neg_neg (g h : E) : dist (-g) (-h) = dist g h := by simp only [dist_eq_norm, neg_sub_neg, norm_sub_rev] @[simp] lemma dist_sub_left (g h₁ h₂ : E) : dist (g - h₁) (g - h₂) = dist h₁ h₂ := by simp only [sub_eq_add_neg, dist_add_left, dist_neg_neg] @[simp] lemma dist_sub_right (g₁ g₂ h : E) : dist (g₁ - h) (g₂ - h) = dist g₁ g₂ := by simpa only [sub_eq_add_neg] using dist_add_right _ _ _ /-- **Triangle inequality** for the norm. -/ lemma norm_add_le (g h : E) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ := by simpa [dist_eq_norm] using dist_triangle g 0 (-h) lemma norm_add_le_of_le {g₁ g₂ : E} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) : ∥g₁ + g₂∥ ≤ n₁ + n₂ := le_trans (norm_add_le g₁ g₂) (add_le_add H₁ H₂) lemma dist_add_add_le (g₁ g₂ h₁ h₂ : E) : dist (g₁ + g₂) (h₁ + h₂) ≤ dist g₁ h₁ + dist g₂ h₂ := by simpa only [dist_add_left, dist_add_right] using dist_triangle (g₁ + g₂) (h₁ + g₂) (h₁ + h₂) lemma dist_add_add_le_of_le {g₁ g₂ h₁ h₂ : E} {d₁ d₂ : ℝ} (H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) : dist (g₁ + g₂) (h₁ + h₂) ≤ d₁ + d₂ := le_trans (dist_add_add_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂) lemma dist_sub_sub_le (g₁ g₂ h₁ h₂ : E) : dist (g₁ - g₂) (h₁ - h₂) ≤ dist g₁ h₁ + dist g₂ h₂ := by simpa only [sub_eq_add_neg, dist_neg_neg] using dist_add_add_le g₁ (-g₂) h₁ (-h₂) lemma dist_sub_sub_le_of_le {g₁ g₂ h₁ h₂ : E} {d₁ d₂ : ℝ} (H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) : dist (g₁ - g₂) (h₁ - h₂) ≤ d₁ + d₂ := le_trans (dist_sub_sub_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂) lemma abs_dist_sub_le_dist_add_add (g₁ g₂ h₁ h₂ : E) : |dist g₁ h₁ - dist g₂ h₂| ≤ dist (g₁ + g₂) (h₁ + h₂) := by simpa only [dist_add_left, dist_add_right, dist_comm h₂] using abs_dist_sub_le (g₁ + g₂) (h₁ + h₂) (h₁ + g₂) @[simp] lemma norm_nonneg (g : E) : 0 ≤ ∥g∥ := by { rw[←dist_zero_right], exact dist_nonneg } @[simp] lemma norm_zero : ∥(0 : E)∥ = 0 := by rw [← dist_zero_right, dist_self] @[nontriviality] lemma norm_of_subsingleton [subsingleton E] (x : E) : ∥x∥ = 0 := by rw [subsingleton.elim x 0, norm_zero] lemma norm_sum_le (s : finset ι) (f : ι → E) : ∥∑ i in s, f i∥ ≤ ∑ i in s, ∥f i∥ := s.le_sum_of_subadditive norm norm_zero norm_add_le f lemma norm_sum_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ} (h : ∀ b ∈ s, ∥f b∥ ≤ n b) : ∥∑ b in s, f b∥ ≤ ∑ b in s, n b := le_trans (norm_sum_le s f) (finset.sum_le_sum h) lemma dist_sum_sum_le_of_le (s : finset ι) {f g : ι → E} {d : ι → ℝ} (h : ∀ b ∈ s, dist (f b) (g b) ≤ d b) : dist (∑ b in s, f b) (∑ b in s, g b) ≤ ∑ b in s, d b := begin simp only [dist_eq_norm, ← finset.sum_sub_distrib] at *, exact norm_sum_le_of_le s h end lemma dist_sum_sum_le (s : finset ι) (f g : ι → E) : dist (∑ b in s, f b) (∑ b in s, g b) ≤ ∑ b in s, dist (f b) (g b) := dist_sum_sum_le_of_le s (λ _ _, le_rfl) lemma norm_sub_le (g h : E) : ∥g - h∥ ≤ ∥g∥ + ∥h∥ := by simpa [dist_eq_norm] using dist_triangle g 0 h lemma norm_sub_le_of_le {g₁ g₂ : E} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) : ∥g₁ - g₂∥ ≤ n₁ + n₂ := le_trans (norm_sub_le g₁ g₂) (add_le_add H₁ H₂) lemma dist_le_norm_add_norm (g h : E) : dist g h ≤ ∥g∥ + ∥h∥ := by { rw dist_eq_norm, apply norm_sub_le } lemma abs_norm_sub_norm_le (g h : E) : |∥g∥ - ∥h∥| ≤ ∥g - h∥ := by simpa [dist_eq_norm] using abs_dist_sub_le g h 0 lemma norm_sub_norm_le (g h : E) : ∥g∥ - ∥h∥ ≤ ∥g - h∥ := le_trans (le_abs_self _) (abs_norm_sub_norm_le g h) lemma dist_norm_norm_le (g h : E) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ := abs_norm_sub_norm_le g h lemma norm_le_insert (u v : E) : ∥v∥ ≤ ∥u∥ + ∥u - v∥ := calc ∥v∥ = ∥u - (u - v)∥ : by abel ... ≤ ∥u∥ + ∥u - v∥ : norm_sub_le u _ lemma norm_le_insert' (u v : E) : ∥u∥ ≤ ∥v∥ + ∥u - v∥ := by { rw norm_sub_rev, exact norm_le_insert v u } lemma norm_le_add_norm_add (u v : E) : ∥u∥ ≤ ∥u + v∥ + ∥v∥ := calc ∥u∥ = ∥u + v - v∥ : by rw add_sub_cancel ... ≤ ∥u + v∥ + ∥v∥ : norm_sub_le _ _ lemma ball_zero_eq (ε : ℝ) : ball (0 : E) ε = {x | ∥x∥ < ε} := set.ext $ assume a, by simp lemma mem_ball_iff_norm {g h : E} {r : ℝ} : h ∈ ball g r ↔ ∥h - g∥ < r := by rw [mem_ball, dist_eq_norm] lemma add_mem_ball_iff_norm {g h : E} {r : ℝ} : g + h ∈ ball g r ↔ ∥h∥ < r := by rw [mem_ball_iff_norm, add_sub_cancel'] lemma mem_ball_iff_norm' {g h : E} {r : ℝ} : h ∈ ball g r ↔ ∥g - h∥ < r := by rw [mem_ball', dist_eq_norm] @[simp] lemma mem_ball_zero_iff {ε : ℝ} {x : E} : x ∈ ball (0 : E) ε ↔ ∥x∥ < ε := by rw [mem_ball, dist_zero_right] lemma mem_closed_ball_iff_norm {g h : E} {r : ℝ} : h ∈ closed_ball g r ↔ ∥h - g∥ ≤ r := by rw [mem_closed_ball, dist_eq_norm] lemma add_mem_closed_ball_iff_norm {g h : E} {r : ℝ} : g + h ∈ closed_ball g r ↔ ∥h∥ ≤ r := by rw [mem_closed_ball_iff_norm, add_sub_cancel'] lemma mem_closed_ball_iff_norm' {g h : E} {r : ℝ} : h ∈ closed_ball g r ↔ ∥g - h∥ ≤ r := by rw [mem_closed_ball', dist_eq_norm] lemma norm_le_of_mem_closed_ball {g h : E} {r : ℝ} (H : h ∈ closed_ball g r) : ∥h∥ ≤ ∥g∥ + r := calc ∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right] ... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _ ... ≤ ∥g∥ + r : by { apply add_le_add_left, rw ← dist_eq_norm, exact H } lemma norm_le_norm_add_const_of_dist_le {a b : E} {c : ℝ} (h : dist a b ≤ c) : ∥a∥ ≤ ∥b∥ + c := norm_le_of_mem_closed_ball h lemma norm_lt_of_mem_ball {g h : E} {r : ℝ} (H : h ∈ ball g r) : ∥h∥ < ∥g∥ + r := calc ∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right] ... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _ ... < ∥g∥ + r : by { apply add_lt_add_left, rw ← dist_eq_norm, exact H } lemma norm_lt_norm_add_const_of_dist_lt {a b : E} {c : ℝ} (h : dist a b < c) : ∥a∥ < ∥b∥ + c := norm_lt_of_mem_ball h lemma bounded_iff_forall_norm_le {s : set E} : bounded s ↔ ∃ C, ∀ x ∈ s, ∥x∥ ≤ C := by simpa only [set.subset_def, mem_closed_ball_iff_norm, sub_zero] using bounded_iff_subset_ball (0 : E) lemma preimage_add_ball (x y : E) (r : ℝ) : ((+) y) ⁻¹' (ball x r) = ball (x - y) r := begin ext z, simp only [dist_eq_norm, set.mem_preimage, mem_ball], abel end lemma preimage_add_closed_ball (x y : E) (r : ℝ) : ((+) y) ⁻¹' (closed_ball x r) = closed_ball (x - y) r := begin ext z, simp only [dist_eq_norm, set.mem_preimage, mem_closed_ball], abel end @[simp] lemma mem_sphere_iff_norm (v w : E) (r : ℝ) : w ∈ sphere v r ↔ ∥w - v∥ = r := by simp [dist_eq_norm] @[simp] lemma mem_sphere_zero_iff_norm {w : E} {r : ℝ} : w ∈ sphere (0:E) r ↔ ∥w∥ = r := by simp [dist_eq_norm] @[simp] lemma norm_eq_of_mem_sphere {r : ℝ} (x : sphere (0:E) r) : ∥(x:E)∥ = r := mem_sphere_zero_iff_norm.mp x.2 lemma preimage_add_sphere (x y : E) (r : ℝ) : ((+) y) ⁻¹' (sphere x r) = sphere (x - y) r := begin ext z, simp only [set.mem_preimage, mem_sphere_iff_norm], abel end lemma ne_zero_of_norm_pos {g : E} : 0 < ∥ g ∥ → g ≠ 0 := begin intros hpos hzero, rw [hzero, norm_zero] at hpos, exact lt_irrefl 0 hpos, end lemma nonzero_of_mem_sphere {r : ℝ} (hr : 0 < r) (x : sphere (0:E) r) : (x:E) ≠ 0 := begin refine ne_zero_of_norm_pos _, rwa norm_eq_of_mem_sphere x, end lemma nonzero_of_mem_unit_sphere (x : sphere (0:E) 1) : (x:E) ≠ 0 := by { apply nonzero_of_mem_sphere, norm_num } /-- We equip the sphere, in a seminormed group, with a formal operation of negation, namely the antipodal map. -/ instance {r : ℝ} : has_neg (sphere (0:E) r) := { neg := λ w, ⟨-↑w, by simp⟩ } @[simp] lemma coe_neg_sphere {r : ℝ} (v : sphere (0:E) r) : (((-v) : sphere _ _) : E) = - (v:E) := rfl namespace isometric -- TODO This material is superseded by similar constructions such as -- `affine_isometry_equiv.const_vadd`; deduplicate /-- Addition `y ↦ y + x` as an `isometry`. -/ protected def add_right (x : E) : E ≃ᵢ E := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_right _ _ _, .. equiv.add_right x } @[simp] lemma add_right_to_equiv (x : E) : (isometric.add_right x).to_equiv = equiv.add_right x := rfl @[simp] lemma coe_add_right (x : E) : (isometric.add_right x : E → E) = λ y, y + x := rfl lemma add_right_apply (x y : E) : (isometric.add_right x : E → E) y = y + x := rfl @[simp] lemma add_right_symm (x : E) : (isometric.add_right x).symm = isometric.add_right (-x) := ext $ λ y, rfl /-- Addition `y ↦ x + y` as an `isometry`. -/ protected def add_left (x : E) : E ≃ᵢ E := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_left _ _ _, to_equiv := equiv.add_left x } @[simp] lemma add_left_to_equiv (x : E) : (isometric.add_left x).to_equiv = equiv.add_left x := rfl @[simp] lemma coe_add_left (x : E) : ⇑(isometric.add_left x) = (+) x := rfl @[simp] lemma add_left_symm (x : E) : (isometric.add_left x).symm = isometric.add_left (-x) := ext $ λ y, rfl variable (E) /-- Negation `x ↦ -x` as an `isometry`. -/ protected def neg : E ≃ᵢ E := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ x y, dist_neg_neg _ _, to_equiv := equiv.neg E } variable {E} @[simp] lemma neg_symm : (isometric.neg E).symm = isometric.neg E := rfl @[simp] lemma neg_to_equiv : (isometric.neg E).to_equiv = equiv.neg E := rfl @[simp] lemma coe_neg : ⇑(isometric.neg E) = has_neg.neg := rfl end isometric theorem normed_group.tendsto_nhds_zero {f : α → E} {l : filter α} : tendsto f l (𝓝 0) ↔ ∀ ε > 0, ∀ᶠ x in l, ∥ f x ∥ < ε := metric.tendsto_nhds.trans $ by simp only [dist_zero_right] lemma normed_group.tendsto_nhds_nhds {f : E → F} {x : E} {y : F} : tendsto f (𝓝 x) (𝓝 y) ↔ ∀ ε > 0, ∃ δ > 0, ∀ x', ∥x' - x∥ < δ → ∥f x' - y∥ < ε := by simp_rw [metric.tendsto_nhds_nhds, dist_eq_norm] lemma normed_group.cauchy_seq_iff [nonempty α] [semilattice_sup α] {u : α → E} : cauchy_seq u ↔ ∀ ε > 0, ∃ N, ∀ m n, N ≤ m → N ≤ n → ∥u m - u n∥ < ε := by simp [metric.cauchy_seq_iff, dist_eq_norm] open finset /-- A homomorphism `f` of seminormed groups is Lipschitz, if there exists a constant `C` such that for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. The analogous condition for a linear map of (semi)normed spaces is in `normed_space.operator_norm`. -/ lemma add_monoid_hom.lipschitz_of_bound (f : E →+ F) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : lipschitz_with (real.to_nnreal C) f := lipschitz_with.of_dist_le' $ λ x y, by simpa only [dist_eq_norm, f.map_sub] using h (x - y) lemma lipschitz_on_with_iff_norm_sub_le {f : E → F} {C : ℝ≥0} {s : set E} : lipschitz_on_with C f s ↔ ∀ (x ∈ s) (y ∈ s), ∥f x - f y∥ ≤ C * ∥x - y∥ := by simp only [lipschitz_on_with_iff_dist_le_mul, dist_eq_norm] lemma lipschitz_on_with.norm_sub_le {f : E → F} {C : ℝ≥0} {s : set E} (h : lipschitz_on_with C f s) {x y : E} (x_in : x ∈ s) (y_in : y ∈ s) : ∥f x - f y∥ ≤ C * ∥x - y∥ := lipschitz_on_with_iff_norm_sub_le.mp h x x_in y y_in lemma lipschitz_on_with.norm_sub_le_of_le {f : E → F} {C : ℝ≥0} {s : set E} (h : lipschitz_on_with C f s){x y : E} (x_in : x ∈ s) (y_in : y ∈ s) {d : ℝ} (hd : ∥x - y∥ ≤ d) : ∥f x - f y∥ ≤ C * d := (h.norm_sub_le x_in y_in).trans $ mul_le_mul_of_nonneg_left hd C.2 lemma lipschitz_with_iff_norm_sub_le {f : E → F} {C : ℝ≥0} : lipschitz_with C f ↔ ∀ x y, ∥f x - f y∥ ≤ C * ∥x - y∥ := by simp only [lipschitz_with_iff_dist_le_mul, dist_eq_norm] alias lipschitz_with_iff_norm_sub_le ↔ lipschitz_with.norm_sub_le _ lemma lipschitz_with.norm_sub_le_of_le {f : E → F} {C : ℝ≥0} (h : lipschitz_with C f) {x y : E} {d : ℝ} (hd : ∥x - y∥ ≤ d) : ∥f x - f y∥ ≤ C * d := (h.norm_sub_le x y).trans $ mul_le_mul_of_nonneg_left hd C.2 /-- A homomorphism `f` of seminormed groups is continuous, if there exists a constant `C` such that for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. The analogous condition for a linear map of normed spaces is in `normed_space.operator_norm`. -/ lemma add_monoid_hom.continuous_of_bound (f : E →+ F) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : continuous f := (f.lipschitz_of_bound C h).continuous lemma is_compact.exists_bound_of_continuous_on [topological_space α] {s : set α} (hs : is_compact s) {f : α → E} (hf : continuous_on f s) : ∃ C, ∀ x ∈ s, ∥f x∥ ≤ C := begin have : bounded (f '' s) := (hs.image_of_continuous_on hf).bounded, rcases bounded_iff_forall_norm_le.1 this with ⟨C, hC⟩, exact ⟨C, λ x hx, hC _ (set.mem_image_of_mem _ hx)⟩, end lemma add_monoid_hom.isometry_iff_norm (f : E →+ F) : isometry f ↔ ∀ x, ∥f x∥ = ∥x∥ := begin simp only [isometry_emetric_iff_metric, dist_eq_norm, ← f.map_sub], refine ⟨λ h x, _, λ h x y, h _⟩, simpa using h x 0 end lemma add_monoid_hom.isometry_of_norm (f : E →+ F) (hf : ∀ x, ∥f x∥ = ∥x∥) : isometry f := f.isometry_iff_norm.2 hf lemma controlled_sum_of_mem_closure {s : add_subgroup E} {g : E} (hg : g ∈ closure (s : set E)) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) : ∃ v : ℕ → E, tendsto (λ n, ∑ i in range (n+1), v i) at_top (𝓝 g) ∧ (∀ n, v n ∈ s) ∧ ∥v 0 - g∥ < b 0 ∧ ∀ n > 0, ∥v n∥ < b n := begin obtain ⟨u : ℕ → E, u_in : ∀ n, u n ∈ s, lim_u : tendsto u at_top (𝓝 g)⟩ := mem_closure_iff_seq_limit.mp hg, obtain ⟨n₀, hn₀⟩ : ∃ n₀, ∀ n ≥ n₀, ∥u n - g∥ < b 0, { have : {x | ∥x - g∥ < b 0} ∈ 𝓝 g, { simp_rw ← dist_eq_norm, exact metric.ball_mem_nhds _ (b_pos _) }, exact filter.tendsto_at_top'.mp lim_u _ this }, set z : ℕ → E := λ n, u (n + n₀), have lim_z : tendsto z at_top (𝓝 g) := lim_u.comp (tendsto_add_at_top_nat n₀), have mem_𝓤 : ∀ n, {p : E × E | ∥p.1 - p.2∥ < b (n + 1)} ∈ 𝓤 E := λ n, by simpa [← dist_eq_norm] using metric.dist_mem_uniformity (b_pos $ n+1), obtain ⟨φ : ℕ → ℕ, φ_extr : strict_mono φ, hφ : ∀ n, ∥z (φ $ n + 1) - z (φ n)∥ < b (n + 1)⟩ := lim_z.cauchy_seq.subseq_mem mem_𝓤, set w : ℕ → E := z ∘ φ, have hw : tendsto w at_top (𝓝 g), from lim_z.comp φ_extr.tendsto_at_top, set v : ℕ → E := λ i, if i = 0 then w 0 else w i - w (i - 1), refine ⟨v, tendsto.congr (finset.eq_sum_range_sub' w) hw , _, hn₀ _ (n₀.le_add_left _), _⟩, { rintro ⟨⟩, { change w 0 ∈ s, apply u_in }, { apply s.sub_mem ; apply u_in }, }, { intros l hl, obtain ⟨k, rfl⟩ : ∃ k, l = k+1, exact nat.exists_eq_succ_of_ne_zero (ne_of_gt hl), apply hφ }, end lemma controlled_sum_of_mem_closure_range {j : E →+ F} {h : F} (Hh : h ∈ (closure $ (j.range : set F))) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) : ∃ g : ℕ → E, tendsto (λ n, ∑ i in range (n+1), j (g i)) at_top (𝓝 h) ∧ ∥j (g 0) - h∥ < b 0 ∧ ∀ n > 0, ∥j (g n)∥ < b n := begin rcases controlled_sum_of_mem_closure Hh b_pos with ⟨v, sum_v, v_in, hv₀, hv_pos⟩, choose g hg using v_in, change ∀ (n : ℕ), j (g n) = v n at hg, refine ⟨g, by simpa [← hg] using sum_v, by simpa [hg 0] using hv₀, λ n hn, by simpa [hg] using hv_pos n hn⟩ end section nnnorm /-- Auxiliary class, endowing a type `α` with a function `nnnorm : α → ℝ≥0`. -/ class has_nnnorm (E : Type*) := (nnnorm : E → ℝ≥0) export has_nnnorm (nnnorm) notation `∥`e`∥₊` := nnnorm e @[priority 100] -- see Note [lower instance priority] instance semi_normed_group.to_has_nnnorm : has_nnnorm E := ⟨λ a, ⟨norm a, norm_nonneg a⟩⟩ @[simp, norm_cast] lemma coe_nnnorm (a : E) : (∥a∥₊ : ℝ) = norm a := rfl lemma nndist_eq_nnnorm (a b : E) : nndist a b = ∥a - b∥₊ := nnreal.eq $ dist_eq_norm _ _ @[simp] lemma nnnorm_zero : ∥(0 : E)∥₊ = 0 := nnreal.eq norm_zero lemma nnnorm_add_le (g h : E) : ∥g + h∥₊ ≤ ∥g∥₊ + ∥h∥₊ := nnreal.coe_le_coe.2 $ norm_add_le g h @[simp] lemma nnnorm_neg (g : E) : ∥-g∥₊ = ∥g∥₊ := nnreal.eq $ norm_neg g lemma nndist_nnnorm_nnnorm_le (g h : E) : nndist ∥g∥₊ ∥h∥₊ ≤ ∥g - h∥₊ := nnreal.coe_le_coe.2 $ dist_norm_norm_le g h lemma of_real_norm_eq_coe_nnnorm (x : E) : ennreal.of_real ∥x∥ = (∥x∥₊ : ℝ≥0∞) := ennreal.of_real_eq_coe_nnreal _ lemma edist_eq_coe_nnnorm_sub (x y : E) : edist x y = (∥x - y∥₊ : ℝ≥0∞) := by rw [edist_dist, dist_eq_norm, of_real_norm_eq_coe_nnnorm] lemma edist_eq_coe_nnnorm (x : E) : edist x 0 = (∥x∥₊ : ℝ≥0∞) := by rw [edist_eq_coe_nnnorm_sub, _root_.sub_zero] lemma mem_emetric_ball_zero_iff {x : E} {r : ℝ≥0∞} : x ∈ emetric.ball (0 : E) r ↔ ↑∥x∥₊ < r := by rw [emetric.mem_ball, edist_eq_coe_nnnorm] lemma nndist_add_add_le (g₁ g₂ h₁ h₂ : E) : nndist (g₁ + g₂) (h₁ + h₂) ≤ nndist g₁ h₁ + nndist g₂ h₂ := nnreal.coe_le_coe.2 $ dist_add_add_le g₁ g₂ h₁ h₂ lemma edist_add_add_le (g₁ g₂ h₁ h₂ : E) : edist (g₁ + g₂) (h₁ + h₂) ≤ edist g₁ h₁ + edist g₂ h₂ := by { simp only [edist_nndist], norm_cast, apply nndist_add_add_le } lemma nnnorm_sum_le (s : finset ι) (f : ι → E) : ∥∑ a in s, f a∥₊ ≤ ∑ a in s, ∥f a∥₊ := s.le_sum_of_subadditive nnnorm nnnorm_zero nnnorm_add_le f lemma add_monoid_hom.lipschitz_of_bound_nnnorm (f : E →+ F) (C : ℝ≥0) (h : ∀ x, ∥f x∥₊ ≤ C * ∥x∥₊) : lipschitz_with C f := @real.to_nnreal_coe C ▸ f.lipschitz_of_bound C h end nnnorm namespace lipschitz_with variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E} lemma neg (hf : lipschitz_with K f) : lipschitz_with K (λ x, -f x) := λ x y, by simpa only [edist_dist, dist_neg_neg] using hf x y lemma add (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x + g x) := λ x y, calc edist (f x + g x) (f y + g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_add_add_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma sub (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x - g x) := by simpa only [sub_eq_add_neg] using hf.add hg.neg end lipschitz_with namespace antilipschitz_with variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E} lemma add_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg g) (hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ (λ x, f x + g x) := begin letI : pseudo_metric_space α := pseudo_emetric_space.to_pseudo_metric_space hf.edist_ne_top, refine antilipschitz_with.of_le_mul_dist (λ x y, _), rw [nnreal.coe_inv, ← div_eq_inv_mul], rw le_div_iff (nnreal.coe_pos.2 $ tsub_pos_iff_lt.2 hK), rw [mul_comm, nnreal.coe_sub hK.le, sub_mul], calc ↑Kf⁻¹ * dist x y - Kg * dist x y ≤ dist (f x) (f y) - dist (g x) (g y) : sub_le_sub (hf.mul_le_dist x y) (hg.dist_le_mul x y) ... ≤ _ : le_trans (le_abs_self _) (abs_dist_sub_le_dist_add_add _ _ _ _) end lemma add_sub_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg (g - f)) (hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ g := by simpa only [pi.sub_apply, add_sub_cancel'_right] using hf.add_lipschitz_with hg hK end antilipschitz_with /-- A group homomorphism from an `add_comm_group` to a `semi_normed_group` induces a `semi_normed_group` structure on the domain. See note [reducible non-instances] -/ @[reducible] def semi_normed_group.induced {E} [add_comm_group E] (f : E →+ F) : semi_normed_group E := { norm := λ x, ∥f x∥, dist_eq := λ x y, by simpa only [add_monoid_hom.map_sub, ← dist_eq_norm], .. pseudo_metric_space.induced f semi_normed_group.to_pseudo_metric_space, } /-- A subgroup of a seminormed group is also a seminormed group, with the restriction of the norm. -/ instance add_subgroup.semi_normed_group (s : add_subgroup E) : semi_normed_group s := semi_normed_group.induced s.subtype /-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to its norm in `E`. -/ @[simp] lemma coe_norm_subgroup {E : Type*} [semi_normed_group E] {s : add_subgroup E} (x : s) : ∥x∥ = ∥(x:E)∥ := rfl /-- A submodule of a seminormed group is also a seminormed group, with the restriction of the norm. See note [implicit instance arguments]. -/ instance submodule.semi_normed_group {𝕜 : Type*} {_ : ring 𝕜} {E : Type*} [semi_normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : semi_normed_group s := { norm := λx, norm (x : E), dist_eq := λx y, dist_eq_norm (x : E) (y : E) } /-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `E` is equal to its norm in `s`. See note [implicit instance arguments]. -/ @[simp, norm_cast] lemma submodule.norm_coe {𝕜 : Type*} {_ : ring 𝕜} {E : Type*} [semi_normed_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) : ∥(x : E)∥ = ∥x∥ := rfl @[simp] lemma submodule.norm_mk {𝕜 : Type*} {_ : ring 𝕜} {E : Type*} [semi_normed_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : E) (hx : x ∈ s) : ∥(⟨x, hx⟩ : s)∥ = ∥x∥ := rfl /-- seminormed group instance on the product of two seminormed groups, using the sup norm. -/ noncomputable instance prod.semi_normed_group : semi_normed_group (E × F) := { norm := λx, max ∥x.1∥ ∥x.2∥, dist_eq := assume (x y : E × F), show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] } lemma prod.semi_norm_def (x : E × F) : ∥x∥ = (max ∥x.1∥ ∥x.2∥) := rfl lemma prod.nnsemi_norm_def (x : E × F) : ∥x∥₊ = max (∥x.1∥₊) (∥x.2∥₊) := by { have := x.semi_norm_def, simp only [← coe_nnnorm] at this, exact_mod_cast this } lemma semi_norm_fst_le (x : E × F) : ∥x.1∥ ≤ ∥x∥ := le_max_left _ _ lemma semi_norm_snd_le (x : E × F) : ∥x.2∥ ≤ ∥x∥ := le_max_right _ _ lemma semi_norm_prod_le_iff {x : E × F} {r : ℝ} : ∥x∥ ≤ r ↔ ∥x.1∥ ≤ r ∧ ∥x.2∥ ≤ r := max_le_iff /-- seminormed group instance on the product of finitely many seminormed groups, using the sup norm. -/ noncomputable instance pi.semi_normed_group {π : ι → Type*} [fintype ι] [Π i, semi_normed_group (π i)] : semi_normed_group (Π i, π i) := { norm := λf, ((finset.sup finset.univ (λ b, ∥f b∥₊) : ℝ≥0) : ℝ), dist_eq := assume x y, congr_arg (coe : ℝ≥0 → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a, show nndist (x a) (y a) = ∥x a - y a∥₊, from nndist_eq_nnnorm _ _ } /-- The seminorm of an element in a product space is `≤ r` if and only if the norm of each component is. -/ lemma pi_semi_norm_le_iff {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] {r : ℝ} (hr : 0 ≤ r) {x : Πi, π i} : ∥x∥ ≤ r ↔ ∀i, ∥x i∥ ≤ r := by simp only [← dist_zero_right, dist_pi_le_iff hr, pi.zero_apply] /-- The seminorm of an element in a product space is `< r` if and only if the norm of each component is. -/ lemma pi_semi_norm_lt_iff {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] {r : ℝ} (hr : 0 < r) {x : Πi, π i} : ∥x∥ < r ↔ ∀i, ∥x i∥ < r := by simp only [← dist_zero_right, dist_pi_lt_iff hr, pi.zero_apply] lemma semi_norm_le_pi_norm {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] (x : Πi, π i) (i : ι) : ∥x i∥ ≤ ∥x∥ := (pi_semi_norm_le_iff (norm_nonneg x)).1 (le_refl _) i @[simp] lemma pi_semi_norm_const [nonempty ι] [fintype ι] (a : E) : ∥(λ i : ι, a)∥ = ∥a∥ := by simpa only [← dist_zero_right] using dist_pi_const a 0 @[simp] lemma pi_nnsemi_norm_const [nonempty ι] [fintype ι] (a : E) : ∥(λ i : ι, a)∥₊ = ∥a∥₊ := nnreal.eq $ pi_semi_norm_const a lemma tendsto_iff_norm_tendsto_zero {f : α → E} {a : filter α} {b : E} : tendsto f a (𝓝 b) ↔ tendsto (λ e, ∥f e - b∥) a (𝓝 0) := by { convert tendsto_iff_dist_tendsto_zero, simp [dist_eq_norm] } lemma is_bounded_under_of_tendsto {l : filter α} {f : α → E} {c : E} (h : filter.tendsto f l (𝓝 c)) : is_bounded_under (≤) l (λ x, ∥f x∥) := ⟨∥c∥ + 1, @tendsto.eventually α E f _ _ (λ k, ∥k∥ ≤ ∥c∥ + 1) h (filter.eventually_iff_exists_mem.mpr ⟨metric.closed_ball c 1, metric.closed_ball_mem_nhds c zero_lt_one, λ y hy, norm_le_norm_add_const_of_dist_le hy⟩)⟩ lemma tendsto_zero_iff_norm_tendsto_zero {f : α → E} {a : filter α} : tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥f e∥) a (𝓝 0) := by { rw [tendsto_iff_norm_tendsto_zero], simp only [sub_zero] } /-- Special case of the sandwich theorem: if the norm of `f` is eventually bounded by a real function `g` which tends to `0`, then `f` tends to `0`. In this pair of lemmas (`squeeze_zero_norm'` and `squeeze_zero_norm`), following a convention of similar lemmas in `topology.metric_space.basic` and `topology.algebra.ordered`, the `'` version is phrased using "eventually" and the non-`'` version is phrased absolutely. -/ lemma squeeze_zero_norm' {f : α → E} {g : α → ℝ} {t₀ : filter α} (h : ∀ᶠ n in t₀, ∥f n∥ ≤ g n) (h' : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) := tendsto_zero_iff_norm_tendsto_zero.mpr (squeeze_zero' (eventually_of_forall (λ n, norm_nonneg _)) h h') /-- Special case of the sandwich theorem: if the norm of `f` is bounded by a real function `g` which tends to `0`, then `f` tends to `0`. -/ lemma squeeze_zero_norm {f : α → E} {g : α → ℝ} {t₀ : filter α} (h : ∀ n, ∥f n∥ ≤ g n) (h' : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) := squeeze_zero_norm' (eventually_of_forall h) h' lemma tendsto_norm_sub_self (x : E) : tendsto (λ g : E, ∥g - x∥) (𝓝 x) (𝓝 0) := by simpa [dist_eq_norm] using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (x:E)) (𝓝 x) _) lemma tendsto_norm {x : E} : tendsto (λg : E, ∥g∥) (𝓝 x) (𝓝 ∥x∥) := by simpa using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (0:E)) _ _) lemma tendsto_norm_zero : tendsto (λg : E, ∥g∥) (𝓝 0) (𝓝 0) := by simpa using tendsto_norm_sub_self (0:E) @[continuity] lemma continuous_norm : continuous (λg:E, ∥g∥) := by simpa using continuous_id.dist (continuous_const : continuous (λ g, (0:E))) @[continuity] lemma continuous_nnnorm : continuous (λ (a : E), ∥a∥₊) := continuous_subtype_mk _ continuous_norm lemma lipschitz_with_one_norm : lipschitz_with 1 (norm : E → ℝ) := by simpa only [dist_zero_left] using lipschitz_with.dist_right (0 : E) lemma uniform_continuous_norm : uniform_continuous (norm : E → ℝ) := lipschitz_with_one_norm.uniform_continuous lemma uniform_continuous_nnnorm : uniform_continuous (λ (a : E), ∥a∥₊) := uniform_continuous_subtype_mk uniform_continuous_norm _ section variables {l : filter α} {f : α → E} {a : E} lemma filter.tendsto.norm (h : tendsto f l (𝓝 a)) : tendsto (λ x, ∥f x∥) l (𝓝 ∥a∥) := tendsto_norm.comp h lemma filter.tendsto.nnnorm (h : tendsto f l (𝓝 a)) : tendsto (λ x, ∥f x∥₊) l (𝓝 (∥a∥₊)) := tendsto.comp continuous_nnnorm.continuous_at h end section variables [topological_space α] {f : α → E} {s : set α} {a : α} {b : E} lemma continuous.norm (h : continuous f) : continuous (λ x, ∥f x∥) := continuous_norm.comp h lemma continuous.nnnorm (h : continuous f) : continuous (λ x, ∥f x∥₊) := continuous_nnnorm.comp h lemma continuous_at.norm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥) a := h.norm lemma continuous_at.nnnorm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥₊) a := h.nnnorm lemma continuous_within_at.norm (h : continuous_within_at f s a) : continuous_within_at (λ x, ∥f x∥) s a := h.norm lemma continuous_within_at.nnnorm (h : continuous_within_at f s a) : continuous_within_at (λ x, ∥f x∥₊) s a := h.nnnorm lemma continuous_on.norm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥) s := λ x hx, (h x hx).norm lemma continuous_on.nnnorm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥₊) s := λ x hx, (h x hx).nnnorm end /-- If `∥y∥→∞`, then we can assume `y≠x` for any fixed `x`. -/ lemma eventually_ne_of_tendsto_norm_at_top {l : filter α} {f : α → E} (h : tendsto (λ y, ∥f y∥) l at_top) (x : E) : ∀ᶠ y in l, f y ≠ x := begin have : ∀ᶠ y in l, 1 + ∥x∥ ≤ ∥f y∥ := h (mem_at_top (1 + ∥x∥)), refine this.mono (λ y hy hxy, _), subst x, exact not_le_of_lt zero_lt_one (add_le_iff_nonpos_left.1 hy) end @[priority 100] -- see Note [lower instance priority] instance semi_normed_group.has_lipschitz_add : has_lipschitz_add E := { lipschitz_add := ⟨2, lipschitz_with.prod_fst.add lipschitz_with.prod_snd⟩ } /-- A seminormed group is a uniform additive group, i.e., addition and subtraction are uniformly continuous. -/ @[priority 100] -- see Note [lower instance priority] instance normed_uniform_group : uniform_add_group E := ⟨(lipschitz_with.prod_fst.sub lipschitz_with.prod_snd).uniform_continuous⟩ @[priority 100] -- see Note [lower instance priority] instance normed_top_group : topological_add_group E := by apply_instance -- short-circuit type class inference lemma nat.norm_cast_le [has_one E] : ∀ n : ℕ, ∥(n : E)∥ ≤ n * ∥(1 : E)∥ | 0 := by simp | (n + 1) := by { rw [n.cast_succ, n.cast_succ, add_mul, one_mul], exact norm_add_le_of_le (nat.norm_cast_le n) le_rfl } lemma semi_normed_group.mem_closure_iff {s : set E} {x : E} : x ∈ closure s ↔ ∀ ε > 0, ∃ y ∈ s, ∥x - y∥ < ε := by simp [metric.mem_closure_iff, dist_eq_norm] lemma norm_le_zero_iff' [separated_space E] {g : E} : ∥g∥ ≤ 0 ↔ g = 0 := begin letI : normed_group E := { to_metric_space := of_t2_pseudo_metric_space ‹_›, .. ‹semi_normed_group E› }, rw [← dist_zero_right], exact dist_le_zero end lemma norm_eq_zero_iff' [separated_space E] {g : E} : ∥g∥ = 0 ↔ g = 0 := (norm_nonneg g).le_iff_eq.symm.trans norm_le_zero_iff' lemma norm_pos_iff' [separated_space E] {g : E} : 0 < ∥g∥ ↔ g ≠ 0 := by rw [← not_le, norm_le_zero_iff'] lemma cauchy_seq_sum_of_eventually_eq {u v : ℕ → E} {N : ℕ} (huv : ∀ n ≥ N, u n = v n) (hv : cauchy_seq (λ n, ∑ k in range (n+1), v k)) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := begin let d : ℕ → E := λ n, ∑ k in range (n + 1), (u k - v k), rw show (λ n, ∑ k in range (n + 1), u k) = d + (λ n, ∑ k in range (n + 1), v k), by { ext n, simp [d] }, have : ∀ n ≥ N, d n = d N, { intros n hn, dsimp [d], rw eventually_constant_sum _ hn, intros m hm, simp [huv m hm] }, exact (tendsto_at_top_of_eventually_const this).cauchy_seq.add hv end end semi_normed_group section normed_group /-- Construct a normed group from a translation invariant distance -/ def normed_group.of_add_dist [has_norm E] [add_comm_group E] [metric_space E] (H1 : ∀ x : E, ∥x∥ = dist x 0) (H2 : ∀ x y z : E, dist x y ≤ dist (x + z) (y + z)) : normed_group E := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }, { have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this } end } /-- A normed group can be built from a norm that satisfies algebraic properties. This is formalised in this structure. -/ structure normed_group.core (E : Type*) [add_comm_group E] [has_norm E] : Prop := (norm_eq_zero_iff : ∀ x : E, ∥x∥ = 0 ↔ x = 0) (triangle : ∀ x y : E, ∥x + y∥ ≤ ∥x∥ + ∥y∥) (norm_neg : ∀ x : E, ∥-x∥ = ∥x∥) /-- The `semi_normed_group.core` induced by a `normed_group.core`. -/ lemma normed_group.core.to_semi_normed_group.core {E : Type*} [add_comm_group E] [has_norm E] (C : normed_group.core E) : semi_normed_group.core E := { norm_zero := (C.norm_eq_zero_iff 0).2 rfl, triangle := C.triangle, norm_neg := C.norm_neg } /-- Constructing a normed group from core properties of a norm, i.e., registering the distance and the metric space structure from the norm properties. -/ def normed_group.of_core (E : Type*) [add_comm_group E] [has_norm E] (C : normed_group.core E) : normed_group E := { eq_of_dist_eq_zero := λ x y h, begin rw [dist_eq_norm] at h, exact sub_eq_zero.mp ((C.norm_eq_zero_iff _).1 h) end ..semi_normed_group.of_core E (normed_group.core.to_semi_normed_group.core C) } variables [normed_group E] [normed_group F] @[simp] lemma norm_eq_zero {g : E} : ∥g∥ = 0 ↔ g = 0 := norm_eq_zero_iff' @[simp] lemma norm_pos_iff {g : E} : 0 < ∥ g ∥ ↔ g ≠ 0 := norm_pos_iff' @[simp] lemma norm_le_zero_iff {g : E} : ∥g∥ ≤ 0 ↔ g = 0 := norm_le_zero_iff' lemma norm_sub_eq_zero_iff {u v : E} : ∥u - v∥ = 0 ↔ u = v := by rw [norm_eq_zero, sub_eq_zero] lemma eq_of_norm_sub_le_zero {g h : E} (a : ∥g - h∥ ≤ 0) : g = h := by rwa [← sub_eq_zero, ← norm_le_zero_iff] lemma eq_of_norm_sub_eq_zero {u v : E} (h : ∥u - v∥ = 0) : u = v := norm_sub_eq_zero_iff.1 h @[simp] lemma nnnorm_eq_zero {a : E} : ∥a∥₊ = 0 ↔ a = 0 := by rw [← nnreal.coe_eq_zero, coe_nnnorm, norm_eq_zero] /-- An injective group homomorphism from an `add_comm_group` to a `normed_group` induces a `normed_group` structure on the domain. See note [reducible non-instances]. -/ @[reducible] def normed_group.induced {E} [add_comm_group E] (f : E →+ F) (h : function.injective f) : normed_group E := { .. semi_normed_group.induced f, .. metric_space.induced f h normed_group.to_metric_space, } /-- A subgroup of a normed group is also a normed group, with the restriction of the norm. -/ instance add_subgroup.normed_group (s : add_subgroup E) : normed_group s := normed_group.induced s.subtype subtype.coe_injective /-- A submodule of a normed group is also a normed group, with the restriction of the norm. See note [implicit instance arguments]. -/ instance submodule.normed_group {𝕜 : Type*} {_ : ring 𝕜} {E : Type*} [normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_group s := { ..submodule.semi_normed_group s } /-- normed group instance on the product of two normed groups, using the sup norm. -/ noncomputable instance prod.normed_group : normed_group (E × F) := { ..prod.semi_normed_group } lemma prod.norm_def (x : E × F) : ∥x∥ = (max ∥x.1∥ ∥x.2∥) := rfl lemma prod.nnnorm_def (x : E × F) : ∥x∥₊ = max (∥x.1∥₊) (∥x.2∥₊) := by { have := x.norm_def, simp only [← coe_nnnorm] at this, exact_mod_cast this } lemma norm_fst_le (x : E × F) : ∥x.1∥ ≤ ∥x∥ := le_max_left _ _ lemma norm_snd_le (x : E × F) : ∥x.2∥ ≤ ∥x∥ := le_max_right _ _ lemma norm_prod_le_iff {x : E × F} {r : ℝ} : ∥x∥ ≤ r ↔ ∥x.1∥ ≤ r ∧ ∥x.2∥ ≤ r := max_le_iff /-- normed group instance on the product of finitely many normed groups, using the sup norm. -/ noncomputable instance pi.normed_group {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] : normed_group (Πi, π i) := { ..pi.semi_normed_group } /-- The norm of an element in a product space is `≤ r` if and only if the norm of each component is. -/ lemma pi_norm_le_iff {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] {r : ℝ} (hr : 0 ≤ r) {x : Πi, π i} : ∥x∥ ≤ r ↔ ∀i, ∥x i∥ ≤ r := by simp only [← dist_zero_right, dist_pi_le_iff hr, pi.zero_apply] /-- The norm of an element in a product space is `< r` if and only if the norm of each component is. -/ lemma pi_norm_lt_iff {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] {r : ℝ} (hr : 0 < r) {x : Πi, π i} : ∥x∥ < r ↔ ∀i, ∥x i∥ < r := by simp only [← dist_zero_right, dist_pi_lt_iff hr, pi.zero_apply] lemma norm_le_pi_norm {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] (x : Πi, π i) (i : ι) : ∥x i∥ ≤ ∥x∥ := (pi_norm_le_iff (norm_nonneg x)).1 (le_refl _) i @[simp] lemma pi_norm_const [nonempty ι] [fintype ι] (a : E) : ∥(λ i : ι, a)∥ = ∥a∥ := by simpa only [← dist_zero_right] using dist_pi_const a 0 @[simp] lemma pi_nnnorm_const [nonempty ι] [fintype ι] (a : E) : ∥(λ i : ι, a)∥₊ = ∥a∥₊ := nnreal.eq $ pi_norm_const a lemma tendsto_norm_nhds_within_zero : tendsto (norm : E → ℝ) (𝓝[{0}ᶜ] 0) (𝓝[set.Ioi 0] 0) := (continuous_norm.tendsto' (0 : E) 0 norm_zero).inf $ tendsto_principal_principal.2 $ λ x, norm_pos_iff.2 end normed_group
1aba6f9808a22b3731898b56865135f83f36aa01
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/set/basic.lean
1e7a9995ab9e11a4ba35185d40919ad1e81300da
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
114,396
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.logic.unique import Mathlib.order.boolean_algebra import Mathlib.PostPort universes u_1 u v w u_2 x u_3 u_4 u_5 u_6 namespace Mathlib /-! # Basic properties of sets Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements have type `X` are thus defined as `set X := X → Prop`. Note that this function need not be decidable. The definition is in the core library. This file provides some basic definitions related to sets and functions not present in the core library, as well as extra lemmas for functions in the core library (empty set, univ, union, intersection, insert, singleton, set-theoretic difference, complement, and powerset). Note that a set is a term, not a type. There is a coersion from `set α` to `Type*` sending `s` to the corresponding subtype `↥s`. See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean. ## Main definitions Notation used here: - `f : α → β` is a function, - `s : set α` and `s₁ s₂ : set α` are subsets of `α` - `t : set β` is a subset of `β`. Definitions in the file: * `strict_subset s₁ s₂ : Prop` : the predicate `s₁ ⊆ s₂` but `s₁ ≠ s₂`. * `nonempty s : Prop` : the predicate `s ≠ ∅`. Note that this is the preferred way to express the fact that `s` has an element (see the Implementation Notes). * `preimage f t : set α` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of β. * `subsingleton s : Prop` : the predicate saying that `s` has at most one element. * `range f : set β` : the image of `univ` under `f`. Also works for `{p : Prop} (f : p → α)` (unlike `image`) * `s.prod t : set (α × β)` : the subset `s × t`. * `inclusion s₁ s₂ : ↥s₁ → ↥s₂` : the map `↥s₁ → ↥s₂` induced by an inclusion `s₁ ⊆ s₂`. ## Notation * `f ⁻¹' t` for `preimage f t` * `f '' s` for `image f s` * `sᶜ` for the complement of `s` ## Implementation notes * `s.nonempty` is to be preferred to `s ≠ ∅` or `∃ x, x ∈ s`. It has the advantage that the `s.nonempty` dot notation can be used. * For `s : set α`, do not use `subtype s`. Instead use `↥s` or `(s : Type*)` or `s`. ## Tags set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert, singleton, complement, powerset -/ /-! ### Set coercion to a type -/ namespace set protected instance has_le {α : Type u_1} : HasLessEq (set α) := { LessEq := has_subset.subset } protected instance has_lt {α : Type u_1} : HasLess (set α) := { Less := fun (s t : set α) => s ≤ t ∧ ¬t ≤ s } protected instance boolean_algebra {α : Type u_1} : boolean_algebra (set α) := boolean_algebra.mk has_union.union LessEq Less sorry sorry sorry sorry sorry sorry has_inter.inter sorry sorry sorry sorry univ sorry ∅ sorry compl has_sdiff.sdiff sorry sorry sorry @[simp] theorem top_eq_univ {α : Type u_1} : ⊤ = univ := rfl @[simp] theorem bot_eq_empty {α : Type u_1} : ⊥ = ∅ := rfl @[simp] theorem sup_eq_union {α : Type u_1} (s : set α) (t : set α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter {α : Type u_1} (s : set α) (t : set α) : s ⊓ t = s ∩ t := rfl /-! `set.lt_eq_ssubset` is defined further down -/ @[simp] theorem le_eq_subset {α : Type u_1} (s : set α) (t : set α) : s ≤ t = (s ⊆ t) := rfl /-- Coercion from a set to the corresponding subtype. -/ protected instance has_coe_to_sort {α : Type u_1} : has_coe_to_sort (set α) := has_coe_to_sort.mk (Type (max 0 u_1)) fun (s : set α) => Subtype fun (x : α) => x ∈ s end set theorem set.set_coe_eq_subtype {α : Type u} (s : set α) : ↥s = Subtype fun (x : α) => x ∈ s := rfl @[simp] theorem set_coe.forall {α : Type u} {s : set α} {p : ↥s → Prop} : (∀ (x : ↥s), p x) ↔ ∀ (x : α) (h : x ∈ s), p { val := x, property := h } := subtype.forall @[simp] theorem set_coe.exists {α : Type u} {s : set α} {p : ↥s → Prop} : (∃ (x : ↥s), p x) ↔ ∃ (x : α), ∃ (h : x ∈ s), p { val := x, property := h } := subtype.exists theorem set_coe.exists' {α : Type u} {s : set α} {p : (x : α) → x ∈ s → Prop} : (∃ (x : α), ∃ (h : x ∈ s), p x h) ↔ ∃ (x : ↥s), p (↑x) (subtype.property x) := iff.symm set_coe.exists theorem set_coe.forall' {α : Type u} {s : set α} {p : (x : α) → x ∈ s → Prop} : (∀ (x : α) (h : x ∈ s), p x h) ↔ ∀ (x : ↥s), p (↑x) (subtype.property x) := iff.symm set_coe.forall @[simp] theorem set_coe_cast {α : Type u} {s : set α} {t : set α} (H' : s = t) (H : ↥s = ↥t) (x : ↥s) : cast H x = { val := subtype.val x, property := H' ▸ subtype.property x } := sorry theorem set_coe.ext {α : Type u} {s : set α} {a : ↥s} {b : ↥s} : ↑a = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {α : Type u} {s : set α} {a : ↥s} {b : ↥s} : ↑a = ↑b ↔ a = b := { mp := set_coe.ext, mpr := fun (h : a = b) => h ▸ rfl } /-- See also `subtype.prop` -/ theorem subtype.mem {α : Type u_1} {s : set α} (p : ↥s) : ↑p ∈ s := subtype.prop p theorem eq.subset {α : Type u_1} {s : set α} {t : set α} : s = t → s ⊆ t := fun (ᾰ : s = t) => id fun (x : α) (hx : x ∈ s) => Eq._oldrec hx ᾰ namespace set protected instance inhabited {α : Type u} : Inhabited (set α) := { default := ∅ } theorem ext {α : Type u} {a : set α} {b : set α} (h : ∀ (x : α), x ∈ a ↔ x ∈ b) : a = b := funext fun (x : α) => propext (h x) theorem ext_iff {α : Type u} {s : set α} {t : set α} : s = t ↔ ∀ (x : α), x ∈ s ↔ x ∈ t := { mp := fun (h : s = t) (x : α) => eq.mpr (id (Eq._oldrec (Eq.refl (x ∈ s ↔ x ∈ t)) h)) (iff.refl (x ∈ t)), mpr := ext } theorem mem_of_mem_of_subset {α : Type u} {x : α} {s : set α} {t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /-! ### Lemmas about `mem` and `set_of` -/ @[simp] theorem mem_set_of_eq {α : Type u} {a : α} {p : α → Prop} : (a ∈ set_of fun (a : α) => p a) = p a := rfl theorem nmem_set_of_eq {α : Type u} {a : α} {P : α → Prop} : (¬a ∈ set_of fun (a : α) => P a) = (¬P a) := rfl @[simp] theorem set_of_mem_eq {α : Type u} {s : set α} : (set_of fun (x : α) => x ∈ s) = s := rfl theorem set_of_set {α : Type u} {s : set α} : set_of s = s := rfl theorem set_of_app_iff {α : Type u} {p : α → Prop} {x : α} : set_of (fun (x : α) => p x) x ↔ p x := iff.rfl theorem mem_def {α : Type u} {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl protected instance decidable_mem {α : Type u} (s : set α) [H : decidable_pred s] (a : α) : Decidable (a ∈ s) := H protected instance decidable_set_of {α : Type u} (p : α → Prop) [H : decidable_pred p] : decidable_pred (set_of fun (a : α) => p a) := H @[simp] theorem set_of_subset_set_of {α : Type u} {p : α → Prop} {q : α → Prop} : ((set_of fun (a : α) => p a) ⊆ set_of fun (a : α) => q a) ↔ ∀ (a : α), p a → q a := iff.rfl @[simp] theorem sep_set_of {α : Type u} {p : α → Prop} {q : α → Prop} : has_sep.sep (fun (a : α) => q a) (set_of fun (a : α) => p a) = set_of fun (a : α) => p a ∧ q a := rfl theorem set_of_and {α : Type u} {p : α → Prop} {q : α → Prop} : (set_of fun (a : α) => p a ∧ q a) = (set_of fun (a : α) => p a) ∩ set_of fun (a : α) => q a := rfl theorem set_of_or {α : Type u} {p : α → Prop} {q : α → Prop} : (set_of fun (a : α) => p a ∨ q a) = (set_of fun (a : α) => p a) ∪ set_of fun (a : α) => q a := rfl /-! ### Lemmas about subsets -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {α : Type u} {s : set α} {t : set α} : s ⊆ t = ∀ (x : α), x ∈ s → x ∈ t := rfl theorem subset.refl {α : Type u} (a : set α) : a ⊆ a := fun (x : α) => id theorem subset.rfl {α : Type u} {s : set α} : s ⊆ s := subset.refl s theorem subset.trans {α : Type u} {a : set α} {b : set α} {c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := fun (x : α) (h : x ∈ a) => bc (ab h) theorem mem_of_eq_of_mem {α : Type u} {x : α} {y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := Eq.symm hx ▸ h theorem subset.antisymm {α : Type u} {a : set α} {b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := ext fun (x : α) => { mp := h₁, mpr := h₂ } theorem subset.antisymm_iff {α : Type u} {a : set α} {b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := sorry -- an alternative name theorem eq_of_subset_of_subset {α : Type u} {a : set α} {b : set α} : a ⊆ b → b ⊆ a → a = b := subset.antisymm theorem mem_of_subset_of_mem {α : Type u} {s₁ : set α} {s₂ : set α} {a : α} (h : s₁ ⊆ s₂) : a ∈ s₁ → a ∈ s₂ := h theorem not_subset {α : Type u} {s : set α} {t : set α} : ¬s ⊆ t ↔ ∃ (a : α), ∃ (H : a ∈ s), ¬a ∈ t := sorry /-! ### Definition of strict subsets `s ⊂ t` and basic properties. -/ protected instance has_ssubset {α : Type u} : has_ssubset (set α) := has_ssubset.mk Less @[simp] theorem lt_eq_ssubset {α : Type u} (s : set α) (t : set α) : s < t = (s ⊂ t) := rfl theorem ssubset_def {α : Type u} {s : set α} {t : set α} : s ⊂ t = (s ⊆ t ∧ ¬t ⊆ s) := rfl theorem exists_of_ssubset {α : Type u} {s : set α} {t : set α} (h : s ⊂ t) : ∃ (x : α), ∃ (H : x ∈ t), ¬x ∈ s := iff.mp not_subset (and.right h) theorem ssubset_iff_subset_ne {α : Type u} {s : set α} {t : set α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t := and_congr_right fun (h : s ≤ t) => not_congr (iff.symm (iff.trans subset.antisymm_iff (and_iff_right h))) theorem eq_or_ssubset_of_subset {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) : s = t ∨ s ⊂ t := iff.mpr or_iff_not_imp_left fun (h' : ¬s = t) => iff.mpr ssubset_iff_subset_ne { left := h, right := h' } theorem ssubset_iff_of_subset {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) : s ⊂ t ↔ ∃ (x : α), ∃ (H : x ∈ t), ¬x ∈ s := sorry theorem not_mem_empty {α : Type u} (x : α) : ¬x ∈ ∅ := id @[simp] theorem not_not_mem {α : Type u} {a : α} {s : set α} : ¬¬a ∈ s ↔ a ∈ s := not_not /-! ### Non-empty sets -/ /-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty {α : Type u} (s : set α) := ∃ (x : α), x ∈ s theorem nonempty_def {α : Type u} {s : set α} : set.nonempty s ↔ ∃ (x : α), x ∈ s := iff.rfl theorem nonempty_of_mem {α : Type u} {s : set α} {x : α} (h : x ∈ s) : set.nonempty s := Exists.intro x h theorem nonempty.not_subset_empty {α : Type u} {s : set α} : set.nonempty s → ¬s ⊆ ∅ := fun (ᾰ : set.nonempty s) (ᾰ_1 : s ⊆ ∅) => Exists.dcases_on ᾰ fun (ᾰ_w : α) (ᾰ_h : ᾰ_w ∈ s) => idRhs (ᾰ_w ∈ ∅) (ᾰ_1 ᾰ_h) theorem nonempty.ne_empty {α : Type u} {s : set α} : set.nonempty s → s ≠ ∅ := sorry /-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/ protected def nonempty.some {α : Type u} {s : set α} (h : set.nonempty s) : α := classical.some h protected theorem nonempty.some_mem {α : Type u} {s : set α} (h : set.nonempty s) : nonempty.some h ∈ s := classical.some_spec h theorem nonempty.mono {α : Type u} {s : set α} {t : set α} (ht : s ⊆ t) (hs : set.nonempty s) : set.nonempty t := Exists.imp ht hs theorem nonempty_of_not_subset {α : Type u} {s : set α} {t : set α} (h : ¬s ⊆ t) : set.nonempty (s \ t) := sorry theorem nonempty_of_ssubset {α : Type u} {s : set α} {t : set α} (ht : s ⊂ t) : set.nonempty (t \ s) := nonempty_of_not_subset (and.right ht) theorem nonempty.of_diff {α : Type u} {s : set α} {t : set α} (h : set.nonempty (s \ t)) : set.nonempty s := Exists.imp (fun (_x : α) => and.left) h theorem nonempty_of_ssubset' {α : Type u} {s : set α} {t : set α} (ht : s ⊂ t) : set.nonempty t := nonempty.of_diff (nonempty_of_ssubset ht) theorem nonempty.inl {α : Type u} {s : set α} {t : set α} (hs : set.nonempty s) : set.nonempty (s ∪ t) := Exists.imp (fun (_x : α) => Or.inl) hs theorem nonempty.inr {α : Type u} {s : set α} {t : set α} (ht : set.nonempty t) : set.nonempty (s ∪ t) := Exists.imp (fun (_x : α) => Or.inr) ht @[simp] theorem union_nonempty {α : Type u} {s : set α} {t : set α} : set.nonempty (s ∪ t) ↔ set.nonempty s ∨ set.nonempty t := exists_or_distrib theorem nonempty.left {α : Type u} {s : set α} {t : set α} (h : set.nonempty (s ∩ t)) : set.nonempty s := Exists.imp (fun (_x : α) => and.left) h theorem nonempty.right {α : Type u} {s : set α} {t : set α} (h : set.nonempty (s ∩ t)) : set.nonempty t := Exists.imp (fun (_x : α) => and.right) h theorem nonempty_inter_iff_exists_right {α : Type u} {s : set α} {t : set α} : set.nonempty (s ∩ t) ↔ ∃ (x : ↥t), ↑x ∈ s := sorry theorem nonempty_inter_iff_exists_left {α : Type u} {s : set α} {t : set α} : set.nonempty (s ∩ t) ↔ ∃ (x : ↥s), ↑x ∈ t := sorry theorem nonempty_iff_univ_nonempty {α : Type u} : Nonempty α ↔ set.nonempty univ := sorry @[simp] theorem univ_nonempty {α : Type u} [h : Nonempty α] : set.nonempty univ := nonempty.dcases_on h fun (h : α) => idRhs (∃ (x : α), x ∈ univ) (Exists.intro h trivial) theorem nonempty.to_subtype {α : Type u} {s : set α} (h : set.nonempty s) : Nonempty ↥s := iff.mpr nonempty_subtype h protected instance univ.nonempty {α : Type u} [Nonempty α] : Nonempty ↥univ := nonempty.to_subtype univ_nonempty @[simp] theorem nonempty_insert {α : Type u} (a : α) (s : set α) : set.nonempty (insert a s) := Exists.intro a (Or.inl rfl) theorem nonempty_of_nonempty_subtype {α : Type u} {s : set α} [Nonempty ↥s] : set.nonempty s := iff.mp nonempty_subtype _inst_1 /-! ### Lemmas about the empty set -/ theorem empty_def {α : Type u} : ∅ = set_of fun (x : α) => False := rfl @[simp] theorem mem_empty_eq {α : Type u} (x : α) : x ∈ ∅ = False := rfl @[simp] theorem set_of_false {α : Type u} : (set_of fun (a : α) => False) = ∅ := rfl @[simp] theorem empty_subset {α : Type u} (s : set α) : ∅ ⊆ s := fun {a : α} (ᾰ : a ∈ ∅) => false.dcases_on (fun (ᾰ : a ∈ ∅) => a ∈ s) ᾰ theorem subset_empty_iff {α : Type u} {s : set α} : s ⊆ ∅ ↔ s = ∅ := iff.symm (iff.trans subset.antisymm_iff (and_iff_left (empty_subset s))) theorem eq_empty_iff_forall_not_mem {α : Type u} {s : set α} : s = ∅ ↔ ∀ (x : α), ¬x ∈ s := iff.symm subset_empty_iff theorem eq_empty_of_subset_empty {α : Type u} {s : set α} : s ⊆ ∅ → s = ∅ := iff.mp subset_empty_iff theorem eq_empty_of_not_nonempty {α : Type u} (h : ¬Nonempty α) (s : set α) : s = ∅ := eq_empty_of_subset_empty fun (x : α) (hx : x ∈ s) => h (Nonempty.intro x) theorem not_nonempty_iff_eq_empty {α : Type u} {s : set α} : ¬set.nonempty s ↔ s = ∅ := sorry theorem empty_not_nonempty {α : Type u} : ¬set.nonempty ∅ := fun (h : set.nonempty ∅) => nonempty.ne_empty h rfl theorem ne_empty_iff_nonempty {α : Type u} {s : set α} : s ≠ ∅ ↔ set.nonempty s := iff.mp not_iff_comm not_nonempty_iff_eq_empty theorem eq_empty_or_nonempty {α : Type u} (s : set α) : s = ∅ ∨ set.nonempty s := iff.mpr or_iff_not_imp_left (iff.mp ne_empty_iff_nonempty) theorem subset_eq_empty {α : Type u} {s : set α} {t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := iff.mp subset_empty_iff (e ▸ h) theorem ball_empty_iff {α : Type u} {p : α → Prop} : (∀ (x : α), x ∈ ∅ → p x) ↔ True := iff_true_intro fun (x : α) => false.elim /-! ### Universal set. In Lean `@univ α` (or `univ : set α`) is the set that contains all elements of type `α`. Mathematically it is the same as `α` but it has a different type. -/ @[simp] theorem set_of_true {α : Type u} : (set_of fun (x : α) => True) = univ := rfl @[simp] theorem mem_univ {α : Type u} (x : α) : x ∈ univ := trivial @[simp] theorem univ_eq_empty_iff {α : Type u} : univ = ∅ ↔ ¬Nonempty α := sorry theorem empty_ne_univ {α : Type u} [h : Nonempty α] : ∅ ≠ univ := fun (e : ∅ = univ) => iff.mp univ_eq_empty_iff (Eq.symm e) h @[simp] theorem subset_univ {α : Type u} (s : set α) : s ⊆ univ := fun (x : α) (H : x ∈ s) => trivial theorem univ_subset_iff {α : Type u} {s : set α} : univ ⊆ s ↔ s = univ := iff.symm (iff.trans subset.antisymm_iff (and_iff_right (subset_univ s))) theorem eq_univ_of_univ_subset {α : Type u} {s : set α} : univ ⊆ s → s = univ := iff.mp univ_subset_iff theorem eq_univ_iff_forall {α : Type u} {s : set α} : s = univ ↔ ∀ (x : α), x ∈ s := iff.trans (iff.symm univ_subset_iff) (forall_congr fun (x : α) => imp_iff_right True.intro) theorem eq_univ_of_forall {α : Type u} {s : set α} : (∀ (x : α), x ∈ s) → s = univ := iff.mpr eq_univ_iff_forall theorem eq_univ_of_subset {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) (hs : s = univ) : t = univ := eq_univ_of_univ_subset (hs ▸ h) theorem exists_mem_of_nonempty (α : Type u_1) [Nonempty α] : ∃ (x : α), x ∈ univ := nonempty.dcases_on _inst_1 fun (val : α) => idRhs (∃ (x : α), x ∈ univ) (Exists.intro val trivial) protected instance univ_decidable {α : Type u} : decidable_pred univ := fun (x : α) => is_true trivial /-- `diagonal α` is the subset of `α × α` consisting of all pairs of the form `(a, a)`. -/ def diagonal (α : Type u_1) : set (α × α) := set_of fun (p : α × α) => prod.fst p = prod.snd p @[simp] theorem mem_diagonal {α : Type u_1} (x : α) : (x, x) ∈ diagonal α := sorry /-! ### Lemmas about union -/ theorem union_def {α : Type u} {s₁ : set α} {s₂ : set α} : s₁ ∪ s₂ = set_of fun (a : α) => a ∈ s₁ ∨ a ∈ s₂ := rfl theorem mem_union_left {α : Type u} {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := Or.inl theorem mem_union_right {α : Type u} {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := Or.inr theorem mem_or_mem_of_mem_union {α : Type u} {x : α} {a : set α} {b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {α : Type u} {x : α} {a : set α} {b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union {α : Type u} (x : α) (a : set α) (b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq {α : Type u} (x : α) (a : set α) (b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self {α : Type u} (a : set α) : a ∪ a = a := ext fun (x : α) => or_self (x ∈ a) @[simp] theorem union_empty {α : Type u} (a : set α) : a ∪ ∅ = a := ext fun (x : α) => or_false (x ∈ a) @[simp] theorem empty_union {α : Type u} (a : set α) : ∅ ∪ a = a := ext fun (x : α) => false_or (x ∈ a) theorem union_comm {α : Type u} (a : set α) (b : set α) : a ∪ b = b ∪ a := ext fun (x : α) => or.comm theorem union_assoc {α : Type u} (a : set α) (b : set α) (c : set α) : a ∪ b ∪ c = a ∪ (b ∪ c) := ext fun (x : α) => or.assoc protected instance union_is_assoc {α : Type u} : is_associative (set α) has_union.union := is_associative.mk union_assoc protected instance union_is_comm {α : Type u} : is_commutative (set α) has_union.union := is_commutative.mk union_comm theorem union_left_comm {α : Type u} (s₁ : set α) (s₂ : set α) (s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext fun (x : α) => or.left_comm theorem union_right_comm {α : Type u} (s₁ : set α) (s₂ : set α) (s₃ : set α) : s₁ ∪ s₂ ∪ s₃ = s₁ ∪ s₃ ∪ s₂ := ext fun (x : α) => or.right_comm theorem union_eq_self_of_subset_left {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) : s ∪ t = t := ext fun (x : α) => or_iff_right_of_imp h theorem union_eq_self_of_subset_right {α : Type u} {s : set α} {t : set α} (h : t ⊆ s) : s ∪ t = s := ext fun (x : α) => or_iff_left_of_imp h @[simp] theorem subset_union_left {α : Type u} (s : set α) (t : set α) : s ⊆ s ∪ t := fun (x : α) => Or.inl @[simp] theorem subset_union_right {α : Type u} (s : set α) (t : set α) : t ⊆ s ∪ t := fun (x : α) => Or.inr theorem union_subset {α : Type u} {s : set α} {t : set α} {r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := fun (x : α) => Or._oldrec sr tr @[simp] theorem union_subset_iff {α : Type u} {s : set α} {t : set α} {u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := iff.trans (forall_congr fun (x : α) => or_imp_distrib) forall_and_distrib theorem union_subset_union {α : Type u} {s₁ : set α} {s₂ : set α} {t₁ : set α} {t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := fun (x : α) => or.imp h₁ h₂ theorem union_subset_union_left {α : Type u} {s₁ : set α} {s₂ : set α} (t : set α) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h subset.rfl theorem union_subset_union_right {α : Type u} (s : set α) {t₁ : set α} {t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union subset.rfl h theorem subset_union_of_subset_left {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_left t u) theorem subset_union_of_subset_right {α : Type u} {s : set α} {u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_right t u) @[simp] theorem union_empty_iff {α : Type u} {s : set α} {t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := sorry /-! ### Lemmas about intersection -/ theorem inter_def {α : Type u} {s₁ : set α} {s₂ : set α} : s₁ ∩ s₂ = set_of fun (a : α) => a ∈ s₁ ∧ a ∈ s₂ := rfl theorem mem_inter_iff {α : Type u} (x : α) (a : set α) (b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq {α : Type u} (x : α) (a : set α) (b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {α : Type u} {x : α} {a : set α} {b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := { left := ha, right := hb } theorem mem_of_mem_inter_left {α : Type u} {x : α} {a : set α} {b : set α} (h : x ∈ a ∩ b) : x ∈ a := and.left h theorem mem_of_mem_inter_right {α : Type u} {x : α} {a : set α} {b : set α} (h : x ∈ a ∩ b) : x ∈ b := and.right h @[simp] theorem inter_self {α : Type u} (a : set α) : a ∩ a = a := ext fun (x : α) => and_self (x ∈ a) @[simp] theorem inter_empty {α : Type u} (a : set α) : a ∩ ∅ = ∅ := ext fun (x : α) => and_false (x ∈ a) @[simp] theorem empty_inter {α : Type u} (a : set α) : ∅ ∩ a = ∅ := ext fun (x : α) => false_and (x ∈ a) theorem inter_comm {α : Type u} (a : set α) (b : set α) : a ∩ b = b ∩ a := ext fun (x : α) => and.comm theorem inter_assoc {α : Type u} (a : set α) (b : set α) (c : set α) : a ∩ b ∩ c = a ∩ (b ∩ c) := ext fun (x : α) => and.assoc protected instance inter_is_assoc {α : Type u} : is_associative (set α) has_inter.inter := is_associative.mk inter_assoc protected instance inter_is_comm {α : Type u} : is_commutative (set α) has_inter.inter := is_commutative.mk inter_comm theorem inter_left_comm {α : Type u} (s₁ : set α) (s₂ : set α) (s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext fun (x : α) => and.left_comm theorem inter_right_comm {α : Type u} (s₁ : set α) (s₂ : set α) (s₃ : set α) : s₁ ∩ s₂ ∩ s₃ = s₁ ∩ s₃ ∩ s₂ := ext fun (x : α) => and.right_comm @[simp] theorem inter_subset_left {α : Type u} (s : set α) (t : set α) : s ∩ t ⊆ s := fun (x : α) => and.left @[simp] theorem inter_subset_right {α : Type u} (s : set α) (t : set α) : s ∩ t ⊆ t := fun (x : α) => and.right theorem subset_inter {α : Type u} {s : set α} {t : set α} {r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := fun (x : α) (h : x ∈ r) => { left := rs h, right := rt h } @[simp] theorem subset_inter_iff {α : Type u} {s : set α} {t : set α} {r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := iff.trans (forall_congr fun (x : α) => imp_and_distrib) forall_and_distrib theorem subset_iff_inter_eq_left {α : Type u} {s : set α} {t : set α} : s ⊆ t ↔ s ∩ t = s := iff.symm (iff.trans ext_iff (forall_congr fun (x : α) => and_iff_left_iff_imp)) theorem subset_iff_inter_eq_right {α : Type u} {s : set α} {t : set α} : t ⊆ s ↔ s ∩ t = t := iff.symm (iff.trans ext_iff (forall_congr fun (x : α) => and_iff_right_iff_imp)) theorem inter_eq_self_of_subset_left {α : Type u} {s : set α} {t : set α} : s ⊆ t → s ∩ t = s := iff.mp subset_iff_inter_eq_left theorem inter_eq_self_of_subset_right {α : Type u} {s : set α} {t : set α} : t ⊆ s → s ∩ t = t := iff.mp subset_iff_inter_eq_right @[simp] theorem inter_univ {α : Type u} (a : set α) : a ∩ univ = a := inter_eq_self_of_subset_left (subset_univ a) @[simp] theorem univ_inter {α : Type u} (a : set α) : univ ∩ a = a := inter_eq_self_of_subset_right (subset_univ a) theorem inter_subset_inter {α : Type u} {s₁ : set α} {s₂ : set α} {t₁ : set α} {t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := fun (x : α) => and.imp h₁ h₂ theorem inter_subset_inter_left {α : Type u} {s : set α} {t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := inter_subset_inter H subset.rfl theorem inter_subset_inter_right {α : Type u} {s : set α} {t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := inter_subset_inter subset.rfl H theorem union_inter_cancel_left {α : Type u} {s : set α} {t : set α} : (s ∪ t) ∩ s = s := iff.mp subset_iff_inter_eq_right (subset_union_left s t) theorem union_inter_cancel_right {α : Type u} {s : set α} {t : set α} : (s ∪ t) ∩ t = t := iff.mp subset_iff_inter_eq_right (subset_union_right s t) /-! ### Distributivity laws -/ theorem inter_distrib_left {α : Type u} (s : set α) (t : set α) (u : set α) : s ∩ (t ∪ u) = s ∩ t ∪ s ∩ u := ext fun (x : α) => and_or_distrib_left theorem inter_distrib_right {α : Type u} (s : set α) (t : set α) (u : set α) : (s ∪ t) ∩ u = s ∩ u ∪ t ∩ u := ext fun (x : α) => or_and_distrib_right theorem union_distrib_left {α : Type u} (s : set α) (t : set α) (u : set α) : s ∪ t ∩ u = (s ∪ t) ∩ (s ∪ u) := ext fun (x : α) => or_and_distrib_left theorem union_distrib_right {α : Type u} (s : set α) (t : set α) (u : set α) : s ∩ t ∪ u = (s ∪ u) ∩ (t ∪ u) := ext fun (x : α) => and_or_distrib_right /-! ### Lemmas about `insert` `insert α s` is the set `{α} ∪ s`. -/ theorem insert_def {α : Type u} (x : α) (s : set α) : insert x s = set_of fun (y : α) => y = x ∨ y ∈ s := rfl @[simp] theorem subset_insert {α : Type u} (x : α) (s : set α) : s ⊆ insert x s := fun (y : α) => Or.inr theorem mem_insert {α : Type u} (x : α) (s : set α) : x ∈ insert x s := Or.inl rfl theorem mem_insert_of_mem {α : Type u} {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := Or.inr theorem eq_or_mem_of_mem_insert {α : Type u} {x : α} {a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {α : Type u} {x : α} {a : α} {s : set α} : x ∈ insert a s → x ≠ a → x ∈ s := or.resolve_left @[simp] theorem mem_insert_iff {α : Type u} {x : α} {a : α} {s : set α} : x ∈ insert a s ↔ x = a ∨ x ∈ s := iff.rfl @[simp] theorem insert_eq_of_mem {α : Type u} {a : α} {s : set α} (h : a ∈ s) : insert a s = s := ext fun (x : α) => or_iff_right_of_imp fun (e : x = a) => Eq.symm e ▸ h theorem ne_insert_of_not_mem {α : Type u} {s : set α} (t : set α) {a : α} : ¬a ∈ s → s ≠ insert a t := mt fun (e : s = insert a t) => Eq.symm e ▸ mem_insert a t theorem insert_subset {α : Type u} {a : α} {s : set α} {t : set α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := sorry theorem insert_subset_insert {α : Type u} {a : α} {s : set α} {t : set α} (h : s ⊆ t) : insert a s ⊆ insert a t := fun (x : α) => or.imp_right h theorem ssubset_iff_insert {α : Type u} {s : set α} {t : set α} : s ⊂ t ↔ ∃ (a : α), ∃ (H : ¬a ∈ s), insert a s ⊆ t := sorry theorem ssubset_insert {α : Type u} {s : set α} {a : α} (h : ¬a ∈ s) : s ⊂ insert a s := iff.mpr ssubset_iff_insert (Exists.intro a (Exists.intro h (subset.refl (insert a s)))) theorem insert_comm {α : Type u} (a : α) (b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext fun (x : α) => or.left_comm theorem insert_union {α : Type u} {a : α} {s : set α} {t : set α} : insert a s ∪ t = insert a (s ∪ t) := ext fun (x : α) => or.assoc @[simp] theorem union_insert {α : Type u} {a : α} {s : set α} {t : set α} : s ∪ insert a t = insert a (s ∪ t) := ext fun (x : α) => or.left_comm theorem insert_nonempty {α : Type u} (a : α) (s : set α) : set.nonempty (insert a s) := Exists.intro a (mem_insert a s) protected instance has_insert.insert.nonempty {α : Type u} (a : α) (s : set α) : Nonempty ↥(insert a s) := nonempty.to_subtype (insert_nonempty a s) theorem insert_inter {α : Type u} (x : α) (s : set α) (t : set α) : insert x (s ∩ t) = insert x s ∩ insert x t := ext fun (y : α) => or_and_distrib_left -- useful in proofs by induction theorem forall_of_forall_insert {α : Type u} {P : α → Prop} {a : α} {s : set α} (H : ∀ (x : α), x ∈ insert a s → P x) (x : α) (h : x ∈ s) : P x := H x (Or.inr h) theorem forall_insert_of_forall {α : Type u} {P : α → Prop} {a : α} {s : set α} (H : ∀ (x : α), x ∈ s → P x) (ha : P a) (x : α) (h : x ∈ insert a s) : P x := or.elim h (fun (e : x = a) => Eq.symm e ▸ ha) (H x) theorem bex_insert_iff {α : Type u} {P : α → Prop} {a : α} {s : set α} : (∃ (x : α), ∃ (H : x ∈ insert a s), P x) ↔ P a ∨ ∃ (x : α), ∃ (H : x ∈ s), P x := iff.trans bex_or_left_distrib (or_congr_left bex_eq_left) theorem ball_insert_iff {α : Type u} {P : α → Prop} {a : α} {s : set α} : (∀ (x : α), x ∈ insert a s → P x) ↔ P a ∧ ∀ (x : α), x ∈ s → P x := iff.trans ball_or_left_distrib (and_congr_left' forall_eq) /-! ### Lemmas about singletons -/ theorem singleton_def {α : Type u} (a : α) : singleton a = insert a ∅ := Eq.symm (insert_emptyc_eq a) @[simp] theorem mem_singleton_iff {α : Type u} {a : α} {b : α} : a ∈ singleton b ↔ a = b := iff.rfl @[simp] theorem set_of_eq_eq_singleton {α : Type u} {a : α} : (set_of fun (n : α) => n = a) = singleton a := ext fun (n : α) => iff.symm mem_singleton_iff -- TODO: again, annotation needed @[simp] theorem mem_singleton {α : Type u} (a : α) : a ∈ singleton a := rfl theorem eq_of_mem_singleton {α : Type u} {x : α} {y : α} (h : x ∈ singleton y) : x = y := h @[simp] theorem singleton_eq_singleton_iff {α : Type u} {x : α} {y : α} : singleton x = singleton y ↔ x = y := iff.trans ext_iff eq_iff_eq_cancel_left theorem mem_singleton_of_eq {α : Type u} {x : α} {y : α} (H : x = y) : x ∈ singleton y := H theorem insert_eq {α : Type u} (x : α) (s : set α) : insert x s = singleton x ∪ s := rfl @[simp] theorem pair_eq_singleton {α : Type u} (a : α) : insert a (singleton a) = singleton a := union_self fun (b : α) => b = a theorem pair_comm {α : Type u} (a : α) (b : α) : insert a (singleton b) = insert b (singleton a) := union_comm (fun (b : α) => b = a) (singleton b) @[simp] theorem singleton_nonempty {α : Type u} (a : α) : set.nonempty (singleton a) := Exists.intro a rfl @[simp] theorem singleton_subset_iff {α : Type u} {a : α} {s : set α} : singleton a ⊆ s ↔ a ∈ s := forall_eq theorem set_compr_eq_eq_singleton {α : Type u} {a : α} : (set_of fun (b : α) => b = a) = singleton a := rfl @[simp] theorem singleton_union {α : Type u} {a : α} {s : set α} : singleton a ∪ s = insert a s := rfl @[simp] theorem union_singleton {α : Type u} {a : α} {s : set α} : s ∪ singleton a = insert a s := union_comm s (singleton a) @[simp] theorem singleton_inter_nonempty {α : Type u} {a : α} {s : set α} : set.nonempty (singleton a ∩ s) ↔ a ∈ s := sorry @[simp] theorem inter_singleton_nonempty {α : Type u} {a : α} {s : set α} : set.nonempty (s ∩ singleton a) ↔ a ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (set.nonempty (s ∩ singleton a) ↔ a ∈ s)) (inter_comm s (singleton a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (set.nonempty (singleton a ∩ s) ↔ a ∈ s)) (propext singleton_inter_nonempty))) (iff.refl (a ∈ s))) @[simp] theorem singleton_inter_eq_empty {α : Type u} {a : α} {s : set α} : singleton a ∩ s = ∅ ↔ ¬a ∈ s := iff.trans (iff.symm not_nonempty_iff_eq_empty) (not_congr singleton_inter_nonempty) @[simp] theorem inter_singleton_eq_empty {α : Type u} {a : α} {s : set α} : s ∩ singleton a = ∅ ↔ ¬a ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (s ∩ singleton a = ∅ ↔ ¬a ∈ s)) (inter_comm s (singleton a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (singleton a ∩ s = ∅ ↔ ¬a ∈ s)) (propext singleton_inter_eq_empty))) (iff.refl (¬a ∈ s))) theorem nmem_singleton_empty {α : Type u} {s : set α} : ¬s ∈ singleton ∅ ↔ set.nonempty s := ne_empty_iff_nonempty protected instance unique_singleton {α : Type u} (a : α) : unique ↥(singleton a) := unique.mk { default := { val := a, property := mem_singleton a } } sorry theorem eq_singleton_iff_unique_mem {α : Type u} {s : set α} {a : α} : s = singleton a ↔ a ∈ s ∧ ∀ (x : α), x ∈ s → x = a := iff.trans subset.antisymm_iff (iff.trans and.comm (and_congr_left' singleton_subset_iff)) theorem eq_singleton_iff_nonempty_unique_mem {α : Type u} {s : set α} {a : α} : s = singleton a ↔ set.nonempty s ∧ ∀ (x : α), x ∈ s → x = a := sorry /-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/ theorem mem_sep {α : Type u} {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ has_sep.sep (fun (x : α) => p x) s := { left := xs, right := px } @[simp] theorem sep_mem_eq {α : Type u} {s : set α} {t : set α} : has_sep.sep (fun (x : α) => x ∈ t) s = s ∩ t := rfl @[simp] theorem mem_sep_eq {α : Type u} {s : set α} {p : α → Prop} {x : α} : x ∈ has_sep.sep (fun (x : α) => p x) s = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {α : Type u} {s : set α} {p : α → Prop} {x : α} : x ∈ has_sep.sep (fun (x : α) => p x) s ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) : s = has_sep.sep (fun (x : α) => x ∈ s) t := Eq.symm (iff.mp subset_iff_inter_eq_right h) theorem sep_subset {α : Type u} (s : set α) (p : α → Prop) : has_sep.sep (fun (x : α) => p x) s ⊆ s := fun (x : α) => and.left theorem forall_not_of_sep_empty {α : Type u} {s : set α} {p : α → Prop} (H : has_sep.sep (fun (x : α) => p x) s = ∅) (x : α) : x ∈ s → ¬p x := iff.mp not_and (iff.mp eq_empty_iff_forall_not_mem H x) @[simp] theorem sep_univ {α : Type u_1} {p : α → Prop} : has_sep.sep (fun (a : α) => p a) univ = set_of fun (a : α) => p a := univ_inter fun (a : α) => p a @[simp] theorem subset_singleton_iff {α : Type u_1} {s : set α} {x : α} : s ⊆ singleton x ↔ ∀ (y : α), y ∈ s → y = x := iff.rfl /-! ### Lemmas about complement -/ theorem mem_compl {α : Type u} {s : set α} {x : α} (h : ¬x ∈ s) : x ∈ (sᶜ) := h theorem compl_set_of {α : Type u_1} (p : α → Prop) : (set_of fun (a : α) => p a)ᶜ = set_of fun (a : α) => ¬p a := rfl theorem not_mem_of_mem_compl {α : Type u} {s : set α} {x : α} (h : x ∈ (sᶜ)) : ¬x ∈ s := h @[simp] theorem mem_compl_eq {α : Type u} (s : set α) (x : α) : x ∈ (sᶜ) = (¬x ∈ s) := rfl theorem mem_compl_iff {α : Type u} (s : set α) (x : α) : x ∈ (sᶜ) ↔ ¬x ∈ s := iff.rfl @[simp] theorem inter_compl_self {α : Type u} (s : set α) : s ∩ (sᶜ) = ∅ := inf_compl_eq_bot @[simp] theorem compl_inter_self {α : Type u} (s : set α) : sᶜ ∩ s = ∅ := compl_inf_eq_bot @[simp] theorem compl_empty {α : Type u} : ∅ᶜ = univ := compl_bot @[simp] theorem compl_union {α : Type u} (s : set α) (t : set α) : s ∪ tᶜ = sᶜ ∩ (tᶜ) := compl_sup theorem compl_inter {α : Type u} (s : set α) (t : set α) : s ∩ tᶜ = sᶜ ∪ (tᶜ) := compl_inf @[simp] theorem compl_univ {α : Type u} : univᶜ = ∅ := compl_top @[simp] theorem compl_empty_iff {α : Type u} {s : set α} : sᶜ = ∅ ↔ s = univ := compl_eq_bot @[simp] theorem compl_univ_iff {α : Type u} {s : set α} : sᶜ = univ ↔ s = ∅ := compl_eq_top theorem nonempty_compl {α : Type u} {s : set α} : set.nonempty (sᶜ) ↔ s ≠ univ := iff.trans (iff.symm ne_empty_iff_nonempty) (not_congr compl_empty_iff) theorem mem_compl_singleton_iff {α : Type u} {a : α} {x : α} : x ∈ (singleton aᶜ) ↔ x ≠ a := not_congr mem_singleton_iff theorem compl_singleton_eq {α : Type u} (a : α) : singleton aᶜ = set_of fun (x : α) => x ≠ a := ext fun (x : α) => mem_compl_singleton_iff theorem union_eq_compl_compl_inter_compl {α : Type u} (s : set α) (t : set α) : s ∪ t = (sᶜ ∩ (tᶜ)ᶜ) := ext fun (x : α) => or_iff_not_and_not theorem inter_eq_compl_compl_union_compl {α : Type u} (s : set α) (t : set α) : s ∩ t = (sᶜ ∪ (tᶜ)ᶜ) := ext fun (x : α) => and_iff_not_or_not @[simp] theorem union_compl_self {α : Type u} (s : set α) : s ∪ (sᶜ) = univ := iff.mpr eq_univ_iff_forall fun (x : α) => em (x ∈ s) @[simp] theorem compl_union_self {α : Type u} (s : set α) : sᶜ ∪ s = univ := eq.mpr (id (Eq._oldrec (Eq.refl (sᶜ ∪ s = univ)) (union_comm (sᶜ) s))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ∪ (sᶜ) = univ)) (union_compl_self s))) (Eq.refl univ)) theorem compl_comp_compl {α : Type u} : compl ∘ compl = id := funext compl_compl theorem compl_subset_comm {α : Type u} {s : set α} {t : set α} : sᶜ ⊆ t ↔ tᶜ ⊆ s := compl_le_iff_compl_le theorem compl_subset_compl {α : Type u} {s : set α} {t : set α} : sᶜ ⊆ (tᶜ) ↔ t ⊆ s := compl_le_compl_iff_le theorem compl_subset_iff_union {α : Type u} {s : set α} {t : set α} : sᶜ ⊆ t ↔ s ∪ t = univ := iff.symm (iff.trans eq_univ_iff_forall (forall_congr fun (a : α) => or_iff_not_imp_left)) theorem subset_compl_comm {α : Type u} {s : set α} {t : set α} : s ⊆ (tᶜ) ↔ t ⊆ (sᶜ) := forall_congr fun (a : α) => imp_not_comm theorem subset_compl_iff_disjoint {α : Type u} {s : set α} {t : set α} : s ⊆ (tᶜ) ↔ s ∩ t = ∅ := iff.trans (forall_congr fun (a : α) => iff.symm and_imp) subset_empty_iff theorem subset_compl_singleton_iff {α : Type u} {a : α} {s : set α} : s ⊆ (singleton aᶜ) ↔ ¬a ∈ s := iff.trans subset_compl_comm singleton_subset_iff theorem inter_subset {α : Type u} (a : set α) (b : set α) (c : set α) : a ∩ b ⊆ c ↔ a ⊆ bᶜ ∪ c := forall_congr fun (x : α) => iff.trans and_imp (imp_congr_right fun (_x : x ∈ a) => imp_iff_not_or) theorem inter_compl_nonempty_iff {α : Type u} {s : set α} {t : set α} : set.nonempty (s ∩ (tᶜ)) ↔ ¬s ⊆ t := sorry /-! ### Lemmas about set difference -/ theorem diff_eq {α : Type u} (s : set α) (t : set α) : s \ t = s ∩ (tᶜ) := rfl @[simp] theorem mem_diff {α : Type u} {s : set α} {t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ ¬x ∈ t := iff.rfl theorem mem_diff_of_mem {α : Type u} {s : set α} {t : set α} {x : α} (h1 : x ∈ s) (h2 : ¬x ∈ t) : x ∈ s \ t := { left := h1, right := h2 } theorem mem_of_mem_diff {α : Type u} {s : set α} {t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := and.left h theorem not_mem_of_mem_diff {α : Type u} {s : set α} {t : set α} {x : α} (h : x ∈ s \ t) : ¬x ∈ t := and.right h theorem diff_eq_compl_inter {α : Type u} {s : set α} {t : set α} : s \ t = tᶜ ∩ s := eq.mpr (id (Eq._oldrec (Eq.refl (s \ t = tᶜ ∩ s)) (diff_eq s t))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ∩ (tᶜ) = tᶜ ∩ s)) (inter_comm s (tᶜ)))) (Eq.refl (tᶜ ∩ s))) theorem nonempty_diff {α : Type u} {s : set α} {t : set α} : set.nonempty (s \ t) ↔ ¬s ⊆ t := inter_compl_nonempty_iff theorem diff_subset {α : Type u} (s : set α) (t : set α) : s \ t ⊆ s := inter_subset_left s fun (a : α) => a ∈ t → False theorem union_diff_cancel' {α : Type u} {s : set α} {t : set α} {u : set α} (h₁ : s ⊆ t) (h₂ : t ⊆ u) : t ∪ u \ s = u := sorry theorem union_diff_cancel {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) : s ∪ t \ s = t := union_diff_cancel' (subset.refl s) h theorem union_diff_cancel_left {α : Type u} {s : set α} {t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := sorry theorem union_diff_cancel_right {α : Type u} {s : set α} {t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := sorry @[simp] theorem union_diff_left {α : Type u} {s : set α} {t : set α} : (s ∪ t) \ s = t \ s := sorry @[simp] theorem union_diff_right {α : Type u} {s : set α} {t : set α} : (s ∪ t) \ t = s \ t := sorry theorem union_diff_distrib {α : Type u} {s : set α} {t : set α} {u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right s t fun (a : α) => a ∈ u → False theorem inter_union_distrib_left {α : Type u} {s : set α} {t : set α} {u : set α} : s ∩ (t ∪ u) = s ∩ t ∪ s ∩ u := ext fun (_x : α) => and_or_distrib_left theorem inter_union_distrib_right {α : Type u} {s : set α} {t : set α} {u : set α} : s ∩ t ∪ u = (s ∪ u) ∩ (t ∪ u) := ext fun (_x : α) => and_or_distrib_right theorem union_inter_distrib_left {α : Type u} {s : set α} {t : set α} {u : set α} : s ∪ t ∩ u = (s ∪ t) ∩ (s ∪ u) := ext fun (_x : α) => or_and_distrib_left theorem union_inter_distrib_right {α : Type u} {s : set α} {t : set α} {u : set α} : (s ∪ t) ∩ u = s ∩ u ∪ t ∩ u := ext fun (_x : α) => or_and_distrib_right theorem inter_diff_assoc {α : Type u} (a : set α) (b : set α) (c : set α) : a ∩ b \ c = a ∩ (b \ c) := inter_assoc a b fun (a : α) => a ∈ c → False @[simp] theorem inter_diff_self {α : Type u} (a : set α) (b : set α) : a ∩ (b \ a) = ∅ := sorry @[simp] theorem inter_union_diff {α : Type u} (s : set α) (t : set α) : s ∩ t ∪ s \ t = s := sorry @[simp] theorem inter_union_compl {α : Type u} (s : set α) (t : set α) : s ∩ t ∪ s ∩ (tᶜ) = s := inter_union_diff s t theorem diff_subset_diff {α : Type u} {s₁ : set α} {s₂ : set α} {t₁ : set α} {t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := sorry theorem diff_subset_diff_left {α : Type u} {s₁ : set α} {s₂ : set α} {t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (subset.refl t) theorem diff_subset_diff_right {α : Type u} {s : set α} {t : set α} {u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff {α : Type u} (s : set α) : sᶜ = univ \ s := sorry @[simp] theorem empty_diff {α : Type u} (s : set α) : ∅ \ s = ∅ := eq_empty_of_subset_empty fun (x : α) (_x : x ∈ ∅ \ s) => (fun (_a : x ∈ ∅ \ s) => and.dcases_on _a fun (left : x ∈ ∅) (right : ¬x ∈ s) => idRhs (x ∈ ∅) left) _x theorem diff_eq_empty {α : Type u} {s : set α} {t : set α} : s \ t = ∅ ↔ s ⊆ t := sorry @[simp] theorem diff_empty {α : Type u} {s : set α} : s \ ∅ = s := sorry theorem diff_diff {α : Type u} {s : set α} {t : set α} {u : set α} : s \ t \ u = s \ (t ∪ u) := sorry -- the following statement contains parentheses to help the reader theorem diff_diff_comm {α : Type u} {s : set α} {t : set α} {u : set α} : s \ t \ u = s \ u \ t := sorry theorem diff_subset_iff {α : Type u} {s : set α} {t : set α} {u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := sorry theorem subset_diff_union {α : Type u} (s : set α) (t : set α) : s ⊆ s \ t ∪ t := eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ s \ t ∪ t)) (union_comm (s \ t) t))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ t ∪ s \ t)) (Eq.symm (propext diff_subset_iff)))) (subset.refl (s \ t))) @[simp] theorem diff_singleton_subset_iff {α : Type u} {x : α} {s : set α} {t : set α} : s \ singleton x ⊆ t ↔ s ⊆ insert x t := eq.mpr (id (Eq._oldrec (Eq.refl (s \ singleton x ⊆ t ↔ s ⊆ insert x t)) (Eq.symm union_singleton))) (eq.mpr (id (Eq._oldrec (Eq.refl (s \ singleton x ⊆ t ↔ s ⊆ t ∪ singleton x)) (union_comm t (singleton x)))) diff_subset_iff) theorem subset_diff_singleton {α : Type u} {x : α} {s : set α} {t : set α} (h : s ⊆ t) (hx : ¬x ∈ s) : s ⊆ t \ singleton x := subset_inter h (iff.mp subset_compl_comm (iff.mpr singleton_subset_iff hx)) theorem subset_insert_diff_singleton {α : Type u} (x : α) (s : set α) : s ⊆ insert x (s \ singleton x) := eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ insert x (s \ singleton x))) (Eq.symm (propext diff_singleton_subset_iff)))) (subset.refl (s \ singleton x)) theorem diff_subset_comm {α : Type u} {s : set α} {t : set α} {u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := eq.mpr (id (Eq._oldrec (Eq.refl (s \ t ⊆ u ↔ s \ u ⊆ t)) (propext diff_subset_iff))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ t ∪ u ↔ s \ u ⊆ t)) (propext diff_subset_iff))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ t ∪ u ↔ s ⊆ u ∪ t)) (union_comm t u))) (iff.refl (s ⊆ u ∪ t)))) theorem diff_inter {α : Type u} {s : set α} {t : set α} {u : set α} : s \ (t ∩ u) = s \ t ∪ s \ u := sorry theorem diff_inter_diff {α : Type u} {s : set α} {t : set α} {u : set α} : s \ t ∩ (s \ u) = s \ (t ∪ u) := sorry theorem diff_compl {α : Type u} {s : set α} {t : set α} : s \ (tᶜ) = s ∩ t := eq.mpr (id (Eq._oldrec (Eq.refl (s \ (tᶜ) = s ∩ t)) (diff_eq s (tᶜ)))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ∩ (tᶜᶜ) = s ∩ t)) (compl_compl t))) (Eq.refl (s ∩ t))) theorem diff_diff_right {α : Type u} {s : set α} {t : set α} {u : set α} : s \ (t \ u) = s \ t ∪ s ∩ u := eq.mpr (id (Eq._oldrec (Eq.refl (s \ (t \ u) = s \ t ∪ s ∩ u)) (diff_eq t u))) (eq.mpr (id (Eq._oldrec (Eq.refl (s \ (t ∩ (uᶜ)) = s \ t ∪ s ∩ u)) diff_inter)) (eq.mpr (id (Eq._oldrec (Eq.refl (s \ t ∪ s \ (uᶜ) = s \ t ∪ s ∩ u)) diff_compl)) (Eq.refl (s \ t ∪ s ∩ u)))) @[simp] theorem insert_diff_of_mem {α : Type u} {a : α} {t : set α} (s : set α) (h : a ∈ t) : insert a s \ t = s \ t := sorry theorem insert_diff_of_not_mem {α : Type u} {a : α} {t : set α} (s : set α) (h : ¬a ∈ t) : insert a s \ t = insert a (s \ t) := sorry theorem insert_diff_self_of_not_mem {α : Type u} {a : α} {s : set α} (h : ¬a ∈ s) : insert a s \ singleton a = s := sorry theorem union_diff_self {α : Type u} {s : set α} {t : set α} : s ∪ t \ s = s ∪ t := sorry theorem diff_union_self {α : Type u} {s : set α} {t : set α} : s \ t ∪ t = s ∪ t := eq.mpr (id (Eq._oldrec (Eq.refl (s \ t ∪ t = s ∪ t)) (union_comm (s \ t) t))) (eq.mpr (id (Eq._oldrec (Eq.refl (t ∪ s \ t = s ∪ t)) union_diff_self)) (eq.mpr (id (Eq._oldrec (Eq.refl (t ∪ s = s ∪ t)) (union_comm t s))) (Eq.refl (s ∪ t)))) theorem diff_inter_self {α : Type u} {a : set α} {b : set α} : b \ a ∩ a = ∅ := sorry theorem diff_inter_self_eq_diff {α : Type u} {s : set α} {t : set α} : s \ (t ∩ s) = s \ t := sorry theorem diff_self_inter {α : Type u} {s : set α} {t : set α} : s \ (s ∩ t) = s \ t := eq.mpr (id (Eq._oldrec (Eq.refl (s \ (s ∩ t) = s \ t)) (inter_comm s t))) (eq.mpr (id (Eq._oldrec (Eq.refl (s \ (t ∩ s) = s \ t)) diff_inter_self_eq_diff)) (Eq.refl (s \ t))) theorem diff_eq_self {α : Type u} {s : set α} {t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := sorry @[simp] theorem diff_singleton_eq_self {α : Type u} {a : α} {s : set α} (h : ¬a ∈ s) : s \ singleton a = s := sorry @[simp] theorem insert_diff_singleton {α : Type u} {a : α} {s : set α} : insert a (s \ singleton a) = insert a s := sorry @[simp] theorem diff_self {α : Type u} {s : set α} : s \ s = ∅ := sorry theorem diff_diff_cancel_left {α : Type u} {s : set α} {t : set α} (h : s ⊆ t) : t \ (t \ s) = s := sorry theorem mem_diff_singleton {α : Type u} {x : α} {y : α} {s : set α} : x ∈ s \ singleton y ↔ x ∈ s ∧ x ≠ y := iff.rfl theorem mem_diff_singleton_empty {α : Type u} {s : set α} {t : set (set α)} : s ∈ t \ singleton ∅ ↔ s ∈ t ∧ set.nonempty s := iff.trans mem_diff_singleton (and_congr iff.rfl ne_empty_iff_nonempty) /-! ### Powerset -/ theorem mem_powerset {α : Type u} {x : set α} {s : set α} (h : x ⊆ s) : x ∈ 𝒫 s := h theorem subset_of_mem_powerset {α : Type u} {x : set α} {s : set α} (h : x ∈ 𝒫 s) : x ⊆ s := h @[simp] theorem mem_powerset_iff {α : Type u} (x : set α) (s : set α) : x ∈ 𝒫 s ↔ x ⊆ s := iff.rfl theorem powerset_inter {α : Type u} (s : set α) (t : set α) : 𝒫(s ∩ t) = 𝒫 s ∩ 𝒫 t := ext fun (u : set α) => subset_inter_iff @[simp] theorem powerset_mono {α : Type u} {s : set α} {t : set α} : 𝒫 s ⊆ 𝒫 t ↔ s ⊆ t := { mp := fun (h : 𝒫 s ⊆ 𝒫 t) => h (subset.refl s), mpr := fun (h : s ⊆ t) (u : set α) (hu : u ∈ 𝒫 s) => subset.trans hu h } theorem monotone_powerset {α : Type u} : monotone powerset := fun (s t : set α) => iff.mpr powerset_mono @[simp] theorem powerset_nonempty {α : Type u} {s : set α} : set.nonempty (𝒫 s) := Exists.intro ∅ (empty_subset s) @[simp] theorem powerset_empty {α : Type u} : 𝒫∅ = singleton ∅ := ext fun (s : set α) => subset_empty_iff /-! ### Inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := set_of fun (x : α) => f x ∈ s infixl:80 " ⁻¹' " => Mathlib.set.preimage @[simp] theorem preimage_empty {α : Type u} {β : Type v} {f : α → β} : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage {α : Type u} {β : Type v} {f : α → β} {s : set β} {a : α} : a ∈ f ⁻¹' s ↔ f a ∈ s := iff.rfl theorem preimage_congr {α : Type u} {β : Type v} {f : α → β} {g : α → β} {s : set β} (h : ∀ (x : α), f x = g x) : f ⁻¹' s = g ⁻¹' s := (fun (f f_1 : α → β) (e_1 : f = f_1) (s s_1 : set β) (e_2 : s = s_1) => congr (congr_arg preimage e_1) e_2) f g (funext fun (x : α) => h x) s s (Eq.refl s) theorem preimage_mono {α : Type u} {β : Type v} {f : α → β} {s : set β} {t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := fun (x : α) (hx : x ∈ f ⁻¹' s) => h hx @[simp] theorem preimage_univ {α : Type u} {β : Type v} {f : α → β} : f ⁻¹' univ = univ := rfl theorem subset_preimage_univ {α : Type u} {β : Type v} {f : α → β} {s : set α} : s ⊆ f ⁻¹' univ := subset_univ s @[simp] theorem preimage_inter {α : Type u} {β : Type v} {f : α → β} {s : set β} {t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {α : Type u} {β : Type v} {f : α → β} {s : set β} {t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {α : Type u} {β : Type v} {f : α → β} {s : set β} : f ⁻¹' (sᶜ) = (f ⁻¹' sᶜ) := rfl @[simp] theorem preimage_diff {α : Type u} {β : Type v} (f : α → β) (s : set β) (t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_set_of_eq {α : Type u} {β : Type v} {p : α → Prop} {f : β → α} : (f ⁻¹' set_of fun (a : α) => p a) = set_of fun (a : β) => p (f a) := rfl @[simp] theorem preimage_id {α : Type u} {s : set α} : id ⁻¹' s = s := rfl @[simp] theorem preimage_id' {α : Type u} {s : set α} : (fun (x : α) => x) ⁻¹' s = s := rfl theorem preimage_const_of_mem {α : Type u} {β : Type v} {b : β} {s : set β} (h : b ∈ s) : (fun (x : α) => b) ⁻¹' s = univ := eq_univ_of_forall fun (x : α) => h theorem preimage_const_of_not_mem {α : Type u} {β : Type v} {b : β} {s : set β} (h : ¬b ∈ s) : (fun (x : α) => b) ⁻¹' s = ∅ := eq_empty_of_subset_empty fun (x : α) (hx : x ∈ (fun (x : α) => b) ⁻¹' s) => h hx theorem preimage_const {α : Type u} {β : Type v} (b : β) (s : set β) [Decidable (b ∈ s)] : (fun (x : α) => b) ⁻¹' s = ite (b ∈ s) univ ∅ := sorry theorem preimage_comp {α : Type u} {β : Type v} {γ : Type w} {f : α → β} {g : β → γ} {s : set γ} : g ∘ f ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl theorem preimage_preimage {α : Type u} {β : Type v} {γ : Type w} {g : β → γ} {f : α → β} {s : set γ} : f ⁻¹' (g ⁻¹' s) = (fun (x : α) => g (f x)) ⁻¹' s := Eq.symm preimage_comp theorem eq_preimage_subtype_val_iff {α : Type u} {p : α → Prop} {s : set (Subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ ∀ (x : α) (h : p x), { val := x, property := h } ∈ s ↔ x ∈ t := sorry theorem preimage_coe_coe_diagonal {α : Type u_1} (s : set α) : prod.map coe coe ⁻¹' diagonal α = diagonal ↥s := sorry infixl:80 " '' " => Mathlib.set.image /-! ### Image of a set under a function -/ theorem mem_image_iff_bex {α : Type u} {β : Type v} {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ (x : α), ∃ (_x : x ∈ s), f x = y := iff.symm bex_def theorem mem_image_eq {α : Type u} {β : Type v} (f : α → β) (s : set α) (y : β) : y ∈ f '' s = ∃ (x : α), x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image {α : Type u} {β : Type v} (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ (x : α), x ∈ s ∧ f x = y := iff.rfl theorem image_eta {α : Type u} {β : Type v} {s : set α} (f : α → β) : f '' s = (fun (x : α) => f x) '' s := rfl theorem mem_image_of_mem {α : Type u} {β : Type v} (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := Exists.intro x { left := h, right := rfl } theorem mem_image_of_injective {α : Type u} {β : Type v} {f : α → β} {a : α} {s : set α} (hf : function.injective f) : f a ∈ f '' s ↔ a ∈ s := sorry theorem ball_image_iff {α : Type u} {β : Type v} {f : α → β} {s : set α} {p : β → Prop} : (∀ (y : β), y ∈ f '' s → p y) ↔ ∀ (x : α), x ∈ s → p (f x) := sorry theorem ball_image_of_ball {α : Type u} {β : Type v} {f : α → β} {s : set α} {p : β → Prop} (h : ∀ (x : α), x ∈ s → p (f x)) (y : β) (H : y ∈ f '' s) : p y := iff.mpr ball_image_iff h theorem bex_image_iff {α : Type u} {β : Type v} {f : α → β} {s : set α} {p : β → Prop} : (∃ (y : β), ∃ (H : y ∈ f '' s), p y) ↔ ∃ (x : α), ∃ (H : x ∈ s), p (f x) := sorry theorem mem_image_elim {α : Type u} {β : Type v} {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) {y : β} : y ∈ f '' s → C y := sorry theorem mem_image_elim_on {α : Type u} {β : Type v} {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y theorem image_congr {α : Type u} {β : Type v} {f : α → β} {g : α → β} {s : set α} (h : ∀ (a : α), a ∈ s → f a = g a) : f '' s = g '' s := sorry /-- A common special case of `image_congr` -/ theorem image_congr' {α : Type u} {β : Type v} {f : α → β} {g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s := image_congr fun (x : α) (_x : x ∈ s) => h x theorem image_comp {α : Type u} {β : Type v} {γ : Type w} (f : β → γ) (g : α → β) (a : set α) : f ∘ g '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball fun (a_1 : α) (ha : a_1 ∈ a) => mem_image_of_mem f (mem_image_of_mem g ha)) (ball_image_of_ball (ball_image_of_ball fun (a_1 : α) (ha : a_1 ∈ a) => mem_image_of_mem (f ∘ g) ha)) /-- A variant of `image_comp`, useful for rewriting -/ theorem image_image {α : Type u} {β : Type v} {γ : Type w} (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (fun (x : α) => g (f x)) '' s := Eq.symm (image_comp g f s) /-- Image is monotone with respect to `⊆`. See `set.monotone_image` for the statement in terms of `≤`. -/ theorem image_subset {α : Type u} {β : Type v} {a : set α} {b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := sorry theorem image_union {α : Type u} {β : Type v} (f : α → β) (s : set α) (t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := sorry @[simp] theorem image_empty {α : Type u} {β : Type v} (f : α → β) : f '' ∅ = ∅ := sorry theorem image_inter_subset {α : Type u} {β : Type v} (f : α → β) (s : set α) (t : set α) : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := subset_inter (image_subset f (inter_subset_left s t)) (image_subset f (inter_subset_right s t)) theorem image_inter_on {α : Type u} {β : Type v} {f : α → β} {s : set α} {t : set α} (h : ∀ (x : α), x ∈ t → ∀ (y : α), y ∈ s → f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := sorry theorem image_inter {α : Type u} {β : Type v} {f : α → β} {s : set α} {t : set α} (H : function.injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on fun (x : α) (_x : x ∈ t) (y : α) (_x : y ∈ s) (h : f x = f y) => H h theorem image_univ_of_surjective {β : Type v} {ι : Type u_1} {f : ι → β} (H : function.surjective f) : f '' univ = univ := sorry @[simp] theorem image_singleton {α : Type u} {β : Type v} {f : α → β} {a : α} : f '' singleton a = singleton (f a) := sorry @[simp] theorem nonempty.image_const {α : Type u} {β : Type v} {s : set α} (hs : set.nonempty s) (a : β) : (fun (_x : α) => a) '' s = singleton a := sorry @[simp] theorem image_eq_empty {α : Type u_1} {β : Type u_2} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := sorry -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image {α : Type u} (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ tᶜ ∈ S := sorry /-- A variant of `image_id` -/ @[simp] theorem image_id' {α : Type u} (s : set α) : (fun (x : α) => x) '' s = s := sorry theorem image_id {α : Type u} (s : set α) : id '' s = s := sorry theorem compl_compl_image {α : Type u} (S : set (set α)) : compl '' (compl '' S) = S := eq.mpr (id (Eq._oldrec (Eq.refl (compl '' (compl '' S) = S)) (Eq.symm (image_comp compl compl S)))) (eq.mpr (id (Eq._oldrec (Eq.refl (compl ∘ compl '' S = S)) compl_comp_compl)) (eq.mpr (id (Eq._oldrec (Eq.refl (id '' S = S)) (image_id S))) (Eq.refl S))) theorem image_insert_eq {α : Type u} {β : Type v} {f : α → β} {a : α} {s : set α} : f '' insert a s = insert (f a) (f '' s) := sorry theorem image_pair {α : Type u} {β : Type v} (f : α → β) (a : α) (b : α) : f '' insert a (singleton b) = insert (f a) (singleton (f b)) := sorry theorem image_subset_preimage_of_inverse {α : Type u} {β : Type v} {f : α → β} {g : β → α} (I : function.left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := sorry theorem preimage_subset_image_of_inverse {α : Type u} {β : Type v} {f : α → β} {g : β → α} (I : function.left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := fun (b : α) (h : b ∈ f ⁻¹' s) => Exists.intro (f b) { left := h, right := I b } theorem image_eq_preimage_of_inverse {α : Type u} {β : Type v} {f : α → β} {g : β → α} (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : image f = preimage g := funext fun (s : set α) => subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {α : Type u} {β : Type v} {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (b ∈ f '' s ↔ g b ∈ s)) (image_eq_preimage_of_inverse h₁ h₂))) (iff.refl (b ∈ g ⁻¹' s)) theorem image_compl_subset {α : Type u} {β : Type v} {f : α → β} {s : set α} (H : function.injective f) : f '' (sᶜ) ⊆ (f '' sᶜ) := sorry theorem subset_image_compl {α : Type u} {β : Type v} {f : α → β} {s : set α} (H : function.surjective f) : f '' sᶜ ⊆ f '' (sᶜ) := sorry theorem image_compl_eq {α : Type u} {β : Type v} {f : α → β} {s : set α} (H : function.bijective f) : f '' (sᶜ) = (f '' sᶜ) := subset.antisymm (image_compl_subset (and.left H)) (subset_image_compl (and.right H)) theorem subset_image_diff {α : Type u} {β : Type v} (f : α → β) (s : set α) (t : set α) : f '' s \ f '' t ⊆ f '' (s \ t) := sorry theorem image_diff {α : Type u} {β : Type v} {f : α → β} (hf : function.injective f) (s : set α) (t : set α) : f '' (s \ t) = f '' s \ f '' t := sorry theorem nonempty.image {α : Type u} {β : Type v} (f : α → β) {s : set α} : set.nonempty s → set.nonempty (f '' s) := fun (ᾰ : set.nonempty s) => Exists.dcases_on ᾰ fun (ᾰ_w : α) (ᾰ_h : ᾰ_w ∈ s) => idRhs (∃ (x : β), x ∈ f '' s) (Exists.intro (f ᾰ_w) (mem_image_of_mem f ᾰ_h)) theorem nonempty.of_image {α : Type u} {β : Type v} {f : α → β} {s : set α} : set.nonempty (f '' s) → set.nonempty s := sorry @[simp] theorem nonempty_image_iff {α : Type u} {β : Type v} {f : α → β} {s : set α} : set.nonempty (f '' s) ↔ set.nonempty s := { mp := nonempty.of_image, mpr := fun (h : set.nonempty s) => nonempty.image f h } protected instance image.nonempty {α : Type u} {β : Type v} (f : α → β) (s : set α) [Nonempty ↥s] : Nonempty ↥(f '' s) := nonempty.to_subtype (nonempty.image f nonempty_of_nonempty_subtype) /-- image and preimage are a Galois connection -/ @[simp] theorem image_subset_iff {α : Type u} {β : Type v} {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset {α : Type u} {β : Type v} (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := iff.mpr image_subset_iff (subset.refl (f ⁻¹' s)) theorem subset_preimage_image {α : Type u} {β : Type v} (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := fun (x : α) => mem_image_of_mem f theorem preimage_image_eq {α : Type u} {β : Type v} {f : α → β} (s : set α) (h : function.injective f) : f ⁻¹' (f '' s) = s := sorry theorem image_preimage_eq {α : Type u} {β : Type v} {f : α → β} (s : set β) (h : function.surjective f) : f '' (f ⁻¹' s) = s := sorry theorem preimage_eq_preimage {α : Type u} {β : Type v} {s : set α} {t : set α} {f : β → α} (hf : function.surjective f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := sorry theorem image_inter_preimage {α : Type u} {β : Type v} (f : α → β) (s : set α) (t : set β) : f '' (s ∩ f ⁻¹' t) = f '' s ∩ t := sorry theorem image_preimage_inter {α : Type u} {β : Type v} (f : α → β) (s : set α) (t : set β) : f '' (f ⁻¹' t ∩ s) = t ∩ f '' s := sorry @[simp] theorem image_inter_nonempty_iff {α : Type u} {β : Type v} {f : α → β} {s : set α} {t : set β} : set.nonempty (f '' s ∩ t) ↔ set.nonempty (s ∩ f ⁻¹' t) := sorry theorem image_diff_preimage {α : Type u} {β : Type v} {f : α → β} {s : set α} {t : set β} : f '' (s \ f ⁻¹' t) = f '' s \ t := sorry theorem compl_image {α : Type u} : image compl = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {α : Type u} {p : set α → Prop} : (compl '' set_of fun (s : set α) => p s) = set_of fun (s : set α) => p (sᶜ) := congr_fun compl_image p theorem inter_preimage_subset {α : Type u} {β : Type v} (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := fun (x : α) (h : x ∈ s ∩ f ⁻¹' t) => { left := mem_image_of_mem f (and.left h), right := and.right h } theorem union_preimage_subset {α : Type u} {β : Type v} (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := fun (x : α) (h : x ∈ s ∪ f ⁻¹' t) => or.elim h (fun (l : x ∈ s) => Or.inl (mem_image_of_mem f l)) fun (r : x ∈ f ⁻¹' t) => Or.inr r theorem subset_image_union {α : Type u} {β : Type v} (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := iff.mpr image_subset_iff (union_preimage_subset s t f) theorem preimage_subset_iff {α : Type u} {β : Type v} {A : set α} {B : set β} {f : α → β} : f ⁻¹' B ⊆ A ↔ ∀ (a : α), f a ∈ B → a ∈ A := iff.rfl theorem image_eq_image {α : Type u} {β : Type v} {s : set α} {t : set α} {f : α → β} (hf : function.injective f) : f '' s = f '' t ↔ s = t := sorry theorem image_subset_image_iff {α : Type u} {β : Type v} {s : set α} {t : set α} {f : α → β} (hf : function.injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := sorry theorem prod_quotient_preimage_eq_image {α : Type u} {β : Type v} [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : (set_of fun (x : quotient s × quotient s) => (g (prod.fst x), g (prod.snd x)) ∈ r) = (fun (a : α × α) => (quotient.mk (prod.fst a), quotient.mk (prod.snd a))) '' ((fun (a : α × α) => (h (prod.fst a), h (prod.snd a))) ⁻¹' r) := sorry /-- Restriction of `f` to `s` factors through `s.image_factorization f : s → f '' s`. -/ def image_factorization {α : Type u} {β : Type v} (f : α → β) (s : set α) : ↥s → ↥(f '' s) := fun (p : ↥s) => { val := f (subtype.val p), property := sorry } theorem image_factorization_eq {α : Type u} {β : Type v} {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext fun (p : ↥s) => rfl theorem surjective_onto_image {α : Type u} {β : Type v} {f : α → β} {s : set α} : function.surjective (image_factorization f s) := sorry /-! ### Subsingleton -/ /-- A set `s` is a `subsingleton`, if it has at most one element. -/ protected def subsingleton {α : Type u} (s : set α) := ∀ {x : α}, x ∈ s → ∀ {y : α}, y ∈ s → x = y theorem subsingleton.mono {α : Type u} {s : set α} {t : set α} (ht : set.subsingleton t) (hst : s ⊆ t) : set.subsingleton s := fun (x : α) (hx : x ∈ s) (y : α) (hy : y ∈ s) => ht (hst hx) (hst hy) theorem subsingleton.image {α : Type u} {β : Type v} {s : set α} (hs : set.subsingleton s) (f : α → β) : set.subsingleton (f '' s) := sorry theorem subsingleton.eq_singleton_of_mem {α : Type u} {s : set α} (hs : set.subsingleton s) {x : α} (hx : x ∈ s) : s = singleton x := sorry theorem subsingleton_empty {α : Type u} : set.subsingleton ∅ := fun (x : α) => false.elim theorem subsingleton_singleton {α : Type u} {a : α} : set.subsingleton (singleton a) := fun (x : α) (hx : x ∈ singleton a) (y : α) (hy : y ∈ singleton a) => Eq.symm (eq_of_mem_singleton hx) ▸ Eq.symm (eq_of_mem_singleton hy) ▸ rfl theorem subsingleton.eq_empty_or_singleton {α : Type u} {s : set α} (hs : set.subsingleton s) : s = ∅ ∨ ∃ (x : α), s = singleton x := sorry theorem subsingleton.induction_on {α : Type u} {s : set α} {p : set α → Prop} (hs : set.subsingleton s) (he : p ∅) (h₁ : ∀ (x : α), p (singleton x)) : p s := sorry theorem subsingleton_univ {α : Type u} [subsingleton α] : set.subsingleton univ := fun (x : α) (hx : x ∈ univ) (y : α) (hy : y ∈ univ) => subsingleton.elim x y /-- `s`, coerced to a type, is a subsingleton type if and only if `s` is a subsingleton set. -/ @[simp] theorem subsingleton_coe {α : Type u} (s : set α) : subsingleton ↥s ↔ set.subsingleton s := sorry /-- `s` is a subsingleton, if its image of an injective function is. -/ theorem subsingleton_of_image {α : Type u_1} {β : Type u_2} {f : α → β} (hf : function.injective f) (s : set α) (hs : subsingleton ↥(f '' s)) : subsingleton ↥s := sorry theorem univ_eq_true_false : univ = insert True (singleton False) := sorry /-! ### Lemmas about range of a function. -/ /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range {α : Type u} {ι : Sort x} (f : ι → α) : set α := set_of fun (x : α) => ∃ (y : ι), f y = x @[simp] theorem mem_range {α : Type u} {ι : Sort x} {f : ι → α} {x : α} : x ∈ range f ↔ ∃ (y : ι), f y = x := iff.rfl @[simp] theorem mem_range_self {α : Type u} {ι : Sort x} {f : ι → α} (i : ι) : f i ∈ range f := Exists.intro i rfl theorem forall_range_iff {α : Type u} {ι : Sort x} {f : ι → α} {p : α → Prop} : (∀ (a : α), a ∈ range f → p a) ↔ ∀ (i : ι), p (f i) := sorry theorem exists_range_iff {α : Type u} {ι : Sort x} {f : ι → α} {p : α → Prop} : (∃ (a : α), ∃ (H : a ∈ range f), p a) ↔ ∃ (i : ι), p (f i) := sorry theorem exists_range_iff' {α : Type u} {ι : Sort x} {f : ι → α} {p : α → Prop} : (∃ (a : α), a ∈ range f ∧ p a) ↔ ∃ (i : ι), p (f i) := sorry theorem range_iff_surjective {α : Type u} {ι : Sort x} {f : ι → α} : range f = univ ↔ function.surjective f := eq_univ_iff_forall theorem Mathlib.function.surjective.range_eq {α : Type u} {ι : Sort x} {f : ι → α} : function.surjective f → range f = univ := iff.mpr range_iff_surjective @[simp] theorem range_id {α : Type u} : range id = univ := iff.mpr range_iff_surjective function.surjective_id theorem is_compl_range_inl_range_inr {α : Type u} {β : Type v} : is_compl (range sum.inl) (range sum.inr) := sorry @[simp] theorem range_inl_union_range_inr {α : Type u} {β : Type v} : range sum.inl ∪ range sum.inr = univ := is_compl.sup_eq_top is_compl_range_inl_range_inr @[simp] theorem range_inl_inter_range_inr {α : Type u} {β : Type v} : range sum.inl ∩ range sum.inr = ∅ := is_compl.inf_eq_bot is_compl_range_inl_range_inr @[simp] theorem range_inr_union_range_inl {α : Type u} {β : Type v} : range sum.inr ∪ range sum.inl = univ := is_compl.sup_eq_top (is_compl.symm is_compl_range_inl_range_inr) @[simp] theorem range_inr_inter_range_inl {α : Type u} {β : Type v} : range sum.inr ∩ range sum.inl = ∅ := is_compl.inf_eq_bot (is_compl.symm is_compl_range_inl_range_inr) @[simp] theorem preimage_inl_range_inr {α : Type u} {β : Type v} : sum.inl ⁻¹' range sum.inr = ∅ := sorry @[simp] theorem preimage_inr_range_inl {α : Type u} {β : Type v} : sum.inr ⁻¹' range sum.inl = ∅ := sorry @[simp] theorem range_quot_mk {α : Type u} (r : α → α → Prop) : range (Quot.mk r) = univ := function.surjective.range_eq (surjective_quot_mk r) @[simp] theorem image_univ {β : Type v} {ι : Type u_1} {f : ι → β} : f '' univ = range f := sorry theorem image_subset_range {β : Type v} {ι : Type u_1} (f : ι → β) (s : set ι) : f '' s ⊆ range f := eq.mpr (id (Eq._oldrec (Eq.refl (f '' s ⊆ range f)) (Eq.symm image_univ))) (image_subset f (subset_univ s)) theorem range_comp {α : Type u} {β : Type v} {ι : Sort x} (g : α → β) (f : ι → α) : range (g ∘ f) = g '' range f := subset.antisymm (iff.mpr forall_range_iff fun (i : ι) => mem_image_of_mem g (mem_range_self i)) (iff.mpr ball_image_iff (iff.mpr forall_range_iff mem_range_self)) theorem range_subset_iff {α : Type u} {ι : Sort x} {f : ι → α} {s : set α} : range f ⊆ s ↔ ∀ (y : ι), f y ∈ s := forall_range_iff theorem range_comp_subset_range {α : Type u} {β : Type v} {γ : Type w} (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g := eq.mpr (id (Eq._oldrec (Eq.refl (range (g ∘ f) ⊆ range g)) (range_comp g f))) (image_subset_range g (range f)) theorem range_nonempty_iff_nonempty {α : Type u} {ι : Sort x} {f : ι → α} : set.nonempty (range f) ↔ Nonempty ι := sorry theorem range_nonempty {α : Type u} {ι : Sort x} [h : Nonempty ι] (f : ι → α) : set.nonempty (range f) := iff.mpr range_nonempty_iff_nonempty h @[simp] theorem range_eq_empty {α : Type u} {ι : Sort x} {f : ι → α} : range f = ∅ ↔ ¬Nonempty ι := iff.trans (iff.symm not_nonempty_iff_eq_empty) (not_congr range_nonempty_iff_nonempty) protected instance range.nonempty {α : Type u} {ι : Sort x} [Nonempty ι] (f : ι → α) : Nonempty ↥(range f) := nonempty.to_subtype (range_nonempty f) @[simp] theorem image_union_image_compl_eq_range {α : Type u} {β : Type v} {s : set α} (f : α → β) : f '' s ∪ f '' (sᶜ) = range f := sorry theorem image_preimage_eq_inter_range {α : Type u} {β : Type v} {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := sorry theorem image_preimage_eq_of_subset {α : Type u} {β : Type v} {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := eq.mpr (id (Eq._oldrec (Eq.refl (f '' (f ⁻¹' s) = s)) image_preimage_eq_inter_range)) (eq.mpr (id (Eq._oldrec (Eq.refl (s ∩ range f = s)) (inter_eq_self_of_subset_left hs))) (Eq.refl s)) theorem image_preimage_eq_iff {α : Type u} {β : Type v} {f : α → β} {s : set β} : f '' (f ⁻¹' s) = s ↔ s ⊆ range f := sorry theorem preimage_subset_preimage_iff {α : Type u} {β : Type v} {s : set α} {t : set α} {f : β → α} (hs : s ⊆ range f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := sorry theorem preimage_eq_preimage' {α : Type u} {β : Type v} {s : set α} {t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := sorry @[simp] theorem preimage_inter_range {α : Type u} {β : Type v} {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := ext fun (x : α) => and_iff_left (Exists.intro x rfl) @[simp] theorem preimage_range_inter {α : Type u} {β : Type v} {f : α → β} {s : set β} : f ⁻¹' (range f ∩ s) = f ⁻¹' s := eq.mpr (id (Eq._oldrec (Eq.refl (f ⁻¹' (range f ∩ s) = f ⁻¹' s)) (inter_comm (range f) s))) (eq.mpr (id (Eq._oldrec (Eq.refl (f ⁻¹' (s ∩ range f) = f ⁻¹' s)) preimage_inter_range)) (Eq.refl (f ⁻¹' s))) theorem preimage_image_preimage {α : Type u} {β : Type v} {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := eq.mpr (id (Eq._oldrec (Eq.refl (f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s)) image_preimage_eq_inter_range)) (eq.mpr (id (Eq._oldrec (Eq.refl (f ⁻¹' (s ∩ range f) = f ⁻¹' s)) preimage_inter_range)) (Eq.refl (f ⁻¹' s))) @[simp] theorem quot_mk_range_eq {α : Type u} [setoid α] : (range fun (x : α) => quotient.mk x) = univ := iff.mpr range_iff_surjective quot.exists_rep theorem range_const_subset {α : Type u} {ι : Sort x} {c : α} : (range fun (x : ι) => c) ⊆ singleton c := iff.mpr range_subset_iff fun (x : ι) => rfl @[simp] theorem range_const {α : Type u} {ι : Sort x} [Nonempty ι] {c : α} : (range fun (x : ι) => c) = singleton c := sorry theorem diagonal_eq_range {α : Type u_1} : diagonal α = range fun (x : α) => (x, x) := sorry theorem preimage_singleton_nonempty {α : Type u} {β : Type v} {f : α → β} {y : β} : set.nonempty (f ⁻¹' singleton y) ↔ y ∈ range f := iff.rfl theorem preimage_singleton_eq_empty {α : Type u} {β : Type v} {f : α → β} {y : β} : f ⁻¹' singleton y = ∅ ↔ ¬y ∈ range f := iff.trans (iff.symm not_nonempty_iff_eq_empty) (not_congr preimage_singleton_nonempty) theorem range_subset_singleton {α : Type u} {ι : Sort x} {f : ι → α} {x : α} : range f ⊆ singleton x ↔ f = function.const ι x := sorry theorem image_compl_preimage {α : Type u} {β : Type v} {f : α → β} {s : set β} : f '' (f ⁻¹' sᶜ) = range f \ s := eq.mpr (id (Eq._oldrec (Eq.refl (f '' (f ⁻¹' sᶜ) = range f \ s)) (compl_eq_univ_diff (f ⁻¹' s)))) (eq.mpr (id (Eq._oldrec (Eq.refl (f '' (univ \ f ⁻¹' s) = range f \ s)) image_diff_preimage)) (eq.mpr (id (Eq._oldrec (Eq.refl (f '' univ \ s = range f \ s)) image_univ)) (Eq.refl (range f \ s)))) @[simp] theorem range_sigma_mk {α : Type u} {β : α → Type u_1} (a : α) : range (sigma.mk a) = sigma.fst ⁻¹' singleton a := sorry /-- Any map `f : ι → β` factors through a map `range_factorization f : ι → range f`. -/ def range_factorization {β : Type v} {ι : Sort x} (f : ι → β) : ι → ↥(range f) := fun (i : ι) => { val := f i, property := mem_range_self i } theorem range_factorization_eq {β : Type v} {ι : Sort x} {f : ι → β} : subtype.val ∘ range_factorization f = f := funext fun (i : ι) => rfl theorem surjective_onto_range {α : Type u} {ι : Sort x} {f : ι → α} : function.surjective (range_factorization f) := sorry theorem image_eq_range {α : Type u} {β : Type v} (f : α → β) (s : set α) : f '' s = range fun (x : ↥s) => f ↑x := sorry @[simp] theorem sum.elim_range {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → γ) (g : β → γ) : range (sum.elim f g) = range f ∪ range g := sorry theorem range_ite_subset' {α : Type u} {β : Type v} {p : Prop} [Decidable p] {f : α → β} {g : α → β} : range (ite p f g) ⊆ range f ∪ range g := sorry theorem range_ite_subset {α : Type u} {β : Type v} {p : α → Prop} [decidable_pred p] {f : α → β} {g : α → β} : (range fun (x : α) => ite (p x) (f x) (g x)) ⊆ range f ∪ range g := sorry @[simp] theorem preimage_range {α : Type u} {β : Type v} (f : α → β) : f ⁻¹' range f = univ := eq_univ_of_forall mem_range_self /-- The range of a function from a `unique` type contains just the function applied to its single value. -/ theorem range_unique {α : Type u} {ι : Sort x} {f : ι → α} [h : unique ι] : range f = singleton (f Inhabited.default) := sorry theorem range_diff_image_subset {α : Type u} {β : Type v} (f : α → β) (s : set α) : range f \ f '' s ⊆ f '' (sᶜ) := sorry theorem range_diff_image {α : Type u} {β : Type v} {f : α → β} (H : function.injective f) (s : set α) : range f \ f '' s = f '' (sᶜ) := sorry /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on {α : Type u} (s : set α) (r : α → α → Prop) := ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≠ y → r x y theorem pairwise_on.mono {α : Type u} {s : set α} {t : set α} {r : α → α → Prop} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := fun (x : α) (xt : x ∈ t) (y : α) (yt : y ∈ t) => hp x (h xt) y (h yt) theorem pairwise_on.mono' {α : Type u} {s : set α} {r : α → α → Prop} {r' : α → α → Prop} (H : ∀ (a b : α), r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := fun (x : α) (xs : x ∈ s) (y : α) (ys : y ∈ s) (h : x ≠ y) => H x y (hp x xs y ys h) /-- If and only if `f` takes pairwise equal values on `s`, there is some value it takes everywhere on `s`. -/ theorem pairwise_on_eq_iff_exists_eq {α : Type u} {β : Type v} [Nonempty β] (s : set α) (f : α → β) : (pairwise_on s fun (x y : α) => f x = f y) ↔ ∃ (z : β), ∀ (x : α), x ∈ s → f x = z := sorry end set namespace function theorem surjective.preimage_injective {α : Type u_2} {β : Type u_3} {f : α → β} (hf : surjective f) : injective (set.preimage f) := fun (s t : set β) => iff.mp (set.preimage_eq_preimage hf) theorem injective.preimage_image {α : Type u_2} {β : Type u_3} {f : α → β} (hf : injective f) (s : set α) : f ⁻¹' (f '' s) = s := set.preimage_image_eq s hf theorem injective.preimage_surjective {α : Type u_2} {β : Type u_3} {f : α → β} (hf : injective f) : surjective (set.preimage f) := sorry theorem surjective.image_preimage {α : Type u_2} {β : Type u_3} {f : α → β} (hf : surjective f) (s : set β) : f '' (f ⁻¹' s) = s := set.image_preimage_eq s hf theorem surjective.image_surjective {α : Type u_2} {β : Type u_3} {f : α → β} (hf : surjective f) : surjective (set.image f) := sorry theorem injective.image_injective {α : Type u_2} {β : Type u_3} {f : α → β} (hf : injective f) : injective (set.image f) := sorry theorem surjective.preimage_subset_preimage_iff {α : Type u_2} {β : Type u_3} {f : α → β} {s : set β} {t : set β} (hf : surjective f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := set.preimage_subset_preimage_iff (eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ set.range f)) (surjective.range_eq hf))) (set.subset_univ s)) theorem surjective.range_comp {ι : Sort u_1} {α : Type u_2} {ι' : Sort u_3} {f : ι → ι'} (hf : surjective f) (g : ι' → α) : set.range (g ∘ f) = set.range g := set.ext fun (y : α) => iff.symm (surjective.exists hf) theorem injective.nonempty_apply_iff {α : Type u_2} {β : Type u_3} {f : set α → set β} (hf : injective f) (h2 : f ∅ = ∅) {s : set α} : set.nonempty (f s) ↔ set.nonempty s := sorry end function /-! ### Image and preimage on subtypes -/ namespace subtype theorem coe_image {α : Type u_1} {p : α → Prop} {s : set (Subtype p)} : coe '' s = set_of fun (x : α) => ∃ (h : p x), { val := x, property := h } ∈ s := sorry theorem range_coe {α : Type u_1} {s : set α} : set.range coe = s := sorry /-- A variant of `range_coe`. Try to use `range_coe` if possible. This version is useful when defining a new type that is defined as the subtype of something. In that case, the coercion doesn't fire anymore. -/ theorem range_val {α : Type u_1} {s : set α} : set.range val = s := range_coe /-- We make this the simp lemma instead of `range_coe`. The reason is that if we write for `s : set α` the function `coe : s → α`, then the inferred implicit arguments of `coe` are `coe α (λ x, x ∈ s)`. -/ @[simp] theorem range_coe_subtype {α : Type u_1} {p : α → Prop} : set.range coe = set_of fun (x : α) => p x := range_coe @[simp] theorem coe_preimage_self {α : Type u_1} (s : set α) : coe ⁻¹' s = set.univ := eq.mpr (id (Eq._oldrec (Eq.refl (coe ⁻¹' s = set.univ)) (Eq.symm (set.preimage_range coe)))) (eq.mpr (id (Eq._oldrec (Eq.refl (coe ⁻¹' s = coe ⁻¹' set.range coe)) range_coe)) (Eq.refl (coe ⁻¹' s))) theorem range_val_subtype {α : Type u_1} {p : α → Prop} : set.range val = set_of fun (x : α) => p x := range_coe theorem coe_image_subset {α : Type u_1} (s : set α) (t : set ↥s) : coe '' t ⊆ s := sorry theorem coe_image_univ {α : Type u_1} (s : set α) : coe '' set.univ = s := Eq.trans set.image_univ range_coe @[simp] theorem image_preimage_coe {α : Type u_1} (s : set α) (t : set α) : coe '' (coe ⁻¹' t) = t ∩ s := Eq.trans set.image_preimage_eq_inter_range (congr_arg (has_inter.inter t) range_coe) theorem image_preimage_val {α : Type u_1} (s : set α) (t : set α) : val '' (val ⁻¹' t) = t ∩ s := image_preimage_coe s t theorem preimage_coe_eq_preimage_coe_iff {α : Type u_1} {s : set α} {t : set α} {u : set α} : coe ⁻¹' t = coe ⁻¹' u ↔ t ∩ s = u ∩ s := sorry theorem preimage_val_eq_preimage_val_iff {α : Type u_1} (s : set α) (t : set α) (u : set α) : val ⁻¹' t = val ⁻¹' u ↔ t ∩ s = u ∩ s := preimage_coe_eq_preimage_coe_iff theorem exists_set_subtype {α : Type u_1} {t : set α} (p : set α → Prop) : (∃ (s : set ↥t), p (coe '' s)) ↔ ∃ (s : set α), s ⊆ t ∧ p s := sorry theorem preimage_coe_nonempty {α : Type u_1} {s : set α} {t : set α} : set.nonempty (coe ⁻¹' t) ↔ set.nonempty (s ∩ t) := sorry theorem preimage_coe_eq_empty {α : Type u_1} {s : set α} {t : set α} : coe ⁻¹' t = ∅ ↔ s ∩ t = ∅ := sorry @[simp] theorem preimage_coe_compl {α : Type u_1} (s : set α) : coe ⁻¹' (sᶜ) = ∅ := iff.mpr preimage_coe_eq_empty (set.inter_compl_self s) @[simp] theorem preimage_coe_compl' {α : Type u_1} (s : set α) : coe ⁻¹' s = ∅ := iff.mpr preimage_coe_eq_empty (set.compl_inter_self s) end subtype namespace set /-! ### Lemmas about cartesian product of sets -/ /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod {α : Type u_1} {β : Type u_2} (s : set α) (t : set β) : set (α × β) := set_of fun (p : α × β) => prod.fst p ∈ s ∧ prod.snd p ∈ t theorem prod_eq {α : Type u_1} {β : Type u_2} (s : set α) (t : set β) : set.prod s t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {p : α × β} : p ∈ set.prod s t = (prod.fst p ∈ s ∧ prod.snd p ∈ t) := rfl @[simp] theorem mem_prod {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {p : α × β} : p ∈ set.prod s t ↔ prod.fst p ∈ s ∧ prod.snd p ∈ t := iff.rfl @[simp] theorem prod_mk_mem_set_prod_eq {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {a : α} {b : β} : (a, b) ∈ set.prod s t = (a ∈ s ∧ b ∈ t) := rfl theorem mk_mem_prod {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ set.prod s t := { left := a_in, right := b_in } theorem prod_mono {α : Type u_1} {β : Type u_2} {s₁ : set α} {s₂ : set α} {t₁ : set β} {t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : set.prod s₁ t₁ ⊆ set.prod s₂ t₂ := sorry theorem prod_subset_iff {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {P : set (α × β)} : set.prod s t ⊆ P ↔ ∀ (x : α), x ∈ s → ∀ (y : β), y ∈ t → (x, y) ∈ P := sorry theorem forall_prod_set {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {p : α × β → Prop} : (∀ (x : α × β), x ∈ set.prod s t → p x) ↔ ∀ (x : α), x ∈ s → ∀ (y : β), y ∈ t → p (x, y) := prod_subset_iff theorem exists_prod_set {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {p : α × β → Prop} : (∃ (x : α × β), ∃ (H : x ∈ set.prod s t), p x) ↔ ∃ (x : α), ∃ (H : x ∈ s), ∃ (y : β), ∃ (H : y ∈ t), p (x, y) := sorry @[simp] theorem prod_empty {α : Type u_1} {β : Type u_2} {s : set α} : set.prod s ∅ = ∅ := sorry @[simp] theorem empty_prod {α : Type u_1} {β : Type u_2} {t : set β} : set.prod ∅ t = ∅ := sorry @[simp] theorem univ_prod_univ {α : Type u_1} {β : Type u_2} : set.prod univ univ = univ := sorry theorem univ_prod {α : Type u_1} {β : Type u_2} {t : set β} : set.prod univ t = prod.snd ⁻¹' t := sorry theorem prod_univ {α : Type u_1} {β : Type u_2} {s : set α} : set.prod s univ = prod.fst ⁻¹' s := sorry @[simp] theorem singleton_prod {α : Type u_1} {β : Type u_2} {t : set β} {a : α} : set.prod (singleton a) t = Prod.mk a '' t := sorry @[simp] theorem prod_singleton {α : Type u_1} {β : Type u_2} {s : set α} {b : β} : set.prod s (singleton b) = (fun (a : α) => (a, b)) '' s := sorry theorem singleton_prod_singleton {α : Type u_1} {β : Type u_2} {a : α} {b : β} : set.prod (singleton a) (singleton b) = singleton (a, b) := sorry @[simp] theorem union_prod {α : Type u_1} {β : Type u_2} {s₁ : set α} {s₂ : set α} {t : set β} : set.prod (s₁ ∪ s₂) t = set.prod s₁ t ∪ set.prod s₂ t := sorry @[simp] theorem prod_union {α : Type u_1} {β : Type u_2} {s : set α} {t₁ : set β} {t₂ : set β} : set.prod s (t₁ ∪ t₂) = set.prod s t₁ ∪ set.prod s t₂ := sorry theorem prod_inter_prod {α : Type u_1} {β : Type u_2} {s₁ : set α} {s₂ : set α} {t₁ : set β} {t₂ : set β} : set.prod s₁ t₁ ∩ set.prod s₂ t₂ = set.prod (s₁ ∩ s₂) (t₁ ∩ t₂) := sorry theorem insert_prod {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {a : α} : set.prod (insert a s) t = Prod.mk a '' t ∪ set.prod s t := sorry theorem prod_insert {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {b : β} : set.prod s (insert b t) = (fun (a : α) => (a, b)) '' s ∪ set.prod s t := sorry theorem prod_preimage_eq {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {s : set α} {t : set β} {f : γ → α} {g : δ → β} : set.prod (f ⁻¹' s) (g ⁻¹' t) = (fun (p : γ × δ) => (f (prod.fst p), g (prod.snd p))) ⁻¹' set.prod s t := rfl theorem prod_preimage_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {s : set α} {t : set β} {f : γ → α} : set.prod (f ⁻¹' s) t = (fun (p : γ × β) => (f (prod.fst p), prod.snd p)) ⁻¹' set.prod s t := rfl theorem prod_preimage_right {α : Type u_1} {β : Type u_2} {δ : Type u_4} {s : set α} {t : set β} {g : δ → β} : set.prod s (g ⁻¹' t) = (fun (p : α × δ) => (prod.fst p, g (prod.snd p))) ⁻¹' set.prod s t := rfl theorem mk_preimage_prod {α : Type u_1} {β : Type u_2} {γ : Type u_3} {s : set α} {t : set β} (f : γ → α) (g : γ → β) : (fun (x : γ) => (f x, g x)) ⁻¹' set.prod s t = f ⁻¹' s ∩ g ⁻¹' t := rfl @[simp] theorem mk_preimage_prod_left {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {y : β} (h : y ∈ t) : (fun (x : α) => (x, y)) ⁻¹' set.prod s t = s := sorry @[simp] theorem mk_preimage_prod_right {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {x : α} (h : x ∈ s) : Prod.mk x ⁻¹' set.prod s t = t := sorry @[simp] theorem mk_preimage_prod_left_eq_empty {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {y : β} (hy : ¬y ∈ t) : (fun (x : α) => (x, y)) ⁻¹' set.prod s t = ∅ := sorry @[simp] theorem mk_preimage_prod_right_eq_empty {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {x : α} (hx : ¬x ∈ s) : Prod.mk x ⁻¹' set.prod s t = ∅ := sorry theorem mk_preimage_prod_left_eq_if {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {y : β} [decidable_pred fun (_x : β) => _x ∈ t] : (fun (x : α) => (x, y)) ⁻¹' set.prod s t = ite (y ∈ t) s ∅ := sorry theorem mk_preimage_prod_right_eq_if {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} {x : α} [decidable_pred fun (_x : α) => _x ∈ s] : Prod.mk x ⁻¹' set.prod s t = ite (x ∈ s) t ∅ := sorry theorem mk_preimage_prod_left_fn_eq_if {α : Type u_1} {β : Type u_2} {γ : Type u_3} {s : set α} {t : set β} {y : β} [decidable_pred fun (_x : β) => _x ∈ t] (f : γ → α) : (fun (x : γ) => (f x, y)) ⁻¹' set.prod s t = ite (y ∈ t) (f ⁻¹' s) ∅ := sorry theorem mk_preimage_prod_right_fn_eq_if {α : Type u_1} {β : Type u_2} {δ : Type u_4} {s : set α} {t : set β} {x : α} [decidable_pred fun (_x : α) => _x ∈ s] (g : δ → β) : (fun (y : δ) => (x, g y)) ⁻¹' set.prod s t = ite (x ∈ s) (g ⁻¹' t) ∅ := sorry theorem image_swap_eq_preimage_swap {α : Type u_1} {β : Type u_2} : image prod.swap = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem preimage_swap_prod {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : prod.swap ⁻¹' set.prod t s = set.prod s t := sorry theorem image_swap_prod {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : prod.swap '' set.prod t s = set.prod s t := eq.mpr (id (Eq._oldrec (Eq.refl (prod.swap '' set.prod t s = set.prod s t)) image_swap_eq_preimage_swap)) (eq.mpr (id (Eq._oldrec (Eq.refl (prod.swap ⁻¹' set.prod t s = set.prod s t)) preimage_swap_prod)) (Eq.refl (set.prod s t))) theorem prod_image_image_eq {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {s : set α} {t : set β} {m₁ : α → γ} {m₂ : β → δ} : set.prod (m₁ '' s) (m₂ '' t) = (fun (p : α × β) => (m₁ (prod.fst p), m₂ (prod.snd p))) '' set.prod s t := sorry theorem prod_range_range_eq {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {m₁ : α → γ} {m₂ : β → δ} : set.prod (range m₁) (range m₂) = range fun (p : α × β) => (m₁ (prod.fst p), m₂ (prod.snd p)) := sorry theorem prod_range_univ_eq {α : Type u_1} {β : Type u_2} {γ : Type u_3} {m₁ : α → γ} : set.prod (range m₁) univ = range fun (p : α × β) => (m₁ (prod.fst p), prod.snd p) := sorry theorem prod_univ_range_eq {α : Type u_1} {β : Type u_2} {δ : Type u_3} {m₂ : β → δ} : set.prod univ (range m₂) = range fun (p : α × β) => (prod.fst p, m₂ (prod.snd p)) := sorry theorem nonempty.prod {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : set.nonempty s → set.nonempty t → set.nonempty (set.prod s t) := sorry theorem nonempty.fst {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : set.nonempty (set.prod s t) → set.nonempty s := sorry theorem nonempty.snd {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : set.nonempty (set.prod s t) → set.nonempty t := sorry theorem prod_nonempty_iff {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : set.nonempty (set.prod s t) ↔ set.nonempty s ∧ set.nonempty t := { mp := fun (h : set.nonempty (set.prod s t)) => { left := nonempty.fst h, right := nonempty.snd h }, mpr := fun (h : set.nonempty s ∧ set.nonempty t) => nonempty.prod (and.left h) (and.right h) } theorem prod_eq_empty_iff {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} : set.prod s t = ∅ ↔ s = ∅ ∨ t = ∅ := sorry theorem prod_sub_preimage_iff {α : Type u_1} {β : Type u_2} {γ : Type u_3} {s : set α} {t : set β} {W : set γ} {f : α × β → γ} : set.prod s t ⊆ f ⁻¹' W ↔ ∀ (a : α) (b : β), a ∈ s → b ∈ t → f (a, b) ∈ W := sorry theorem fst_image_prod_subset {α : Type u_1} {β : Type u_2} (s : set α) (t : set β) : prod.fst '' set.prod s t ⊆ s := sorry theorem prod_subset_preimage_fst {α : Type u_1} {β : Type u_2} (s : set α) (t : set β) : set.prod s t ⊆ prod.fst ⁻¹' s := iff.mp image_subset_iff (fst_image_prod_subset s t) theorem fst_image_prod {α : Type u_1} {β : Type u_2} (s : set β) {t : set α} (ht : set.nonempty t) : prod.fst '' set.prod s t = s := sorry theorem snd_image_prod_subset {α : Type u_1} {β : Type u_2} (s : set α) (t : set β) : prod.snd '' set.prod s t ⊆ t := sorry theorem prod_subset_preimage_snd {α : Type u_1} {β : Type u_2} (s : set α) (t : set β) : set.prod s t ⊆ prod.snd ⁻¹' t := iff.mp image_subset_iff (snd_image_prod_subset s t) theorem snd_image_prod {α : Type u_1} {β : Type u_2} {s : set α} (hs : set.nonempty s) (t : set β) : prod.snd '' set.prod s t = t := sorry /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ theorem prod_subset_prod_iff {α : Type u_1} {β : Type u_2} {s : set α} {s₁ : set α} {t : set β} {t₁ : set β} : set.prod s t ⊆ set.prod s₁ t₁ ↔ s ⊆ s₁ ∧ t ⊆ t₁ ∨ s = ∅ ∨ t = ∅ := sorry /-! ### Lemmas about set-indexed products of sets -/ /-- Given an index set `i` and a family of sets `s : Π i, set (α i)`, `pi i s` is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `π a` whenever `a ∈ i`. -/ def pi {ι : Type u_1} {α : ι → Type u_2} (s : set ι) (t : (i : ι) → set (α i)) : set ((i : ι) → α i) := set_of fun (f : (i : ι) → α i) => ∀ (i : ι), i ∈ s → f i ∈ t i @[simp] theorem mem_pi {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} {f : (i : ι) → α i} : f ∈ pi s t ↔ ∀ (i : ι), i ∈ s → f i ∈ t i := iff.refl (f ∈ pi s t) @[simp] theorem mem_univ_pi {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} {f : (i : ι) → α i} : f ∈ pi univ t ↔ ∀ (i : ι), f i ∈ t i := sorry @[simp] theorem empty_pi {ι : Type u_1} {α : ι → Type u_2} (s : (i : ι) → set (α i)) : pi ∅ s = univ := sorry @[simp] theorem pi_univ {ι : Type u_1} {α : ι → Type u_2} (s : set ι) : (pi s fun (i : ι) => univ) = univ := eq_univ_of_forall fun (f : (i : ι) → (fun (i : ι) => α i) i) (i : ι) (hi : i ∈ s) => mem_univ (f i) theorem pi_mono {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t₁ : (i : ι) → set (α i)} {t₂ : (i : ι) → set (α i)} (h : ∀ (i : ι), i ∈ s → t₁ i ⊆ t₂ i) : pi s t₁ ⊆ pi s t₂ := fun (x : (i : ι) → α i) (hx : x ∈ pi s t₁) (i : ι) (hi : i ∈ s) => h i hi (hx i hi) theorem pi_inter_distrib {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} {t₁ : (i : ι) → set (α i)} : (pi s fun (i : ι) => t i ∩ t₁ i) = pi s t ∩ pi s t₁ := sorry theorem pi_congr {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {s₁ : set ι} {t : (i : ι) → set (α i)} {t₁ : (i : ι) → set (α i)} (h : s = s₁) (h' : ∀ (i : ι), i ∈ s → t i = t₁ i) : pi s t = pi s₁ t₁ := sorry theorem pi_eq_empty {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} {i : ι} (hs : i ∈ s) (ht : t i = ∅) : pi s t = ∅ := sorry theorem univ_pi_eq_empty {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} {i : ι} (ht : t i = ∅) : pi univ t = ∅ := pi_eq_empty (mem_univ i) ht theorem pi_nonempty_iff {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} : set.nonempty (pi s t) ↔ ∀ (i : ι), ∃ (x : α i), i ∈ s → x ∈ t i := sorry theorem univ_pi_nonempty_iff {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} : set.nonempty (pi univ t) ↔ ∀ (i : ι), set.nonempty (t i) := sorry theorem pi_eq_empty_iff {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} : pi s t = ∅ ↔ ∃ (i : ι), (α i → False) ∨ i ∈ s ∧ t i = ∅ := sorry theorem univ_pi_eq_empty_iff {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} : pi univ t = ∅ ↔ ∃ (i : ι), t i = ∅ := sorry @[simp] theorem insert_pi {ι : Type u_1} {α : ι → Type u_2} (i : ι) (s : set ι) (t : (i : ι) → set (α i)) : pi (insert i s) t = function.eval i ⁻¹' t i ∩ pi s t := sorry @[simp] theorem singleton_pi {ι : Type u_1} {α : ι → Type u_2} (i : ι) (t : (i : ι) → set (α i)) : pi (singleton i) t = function.eval i ⁻¹' t i := sorry theorem singleton_pi' {ι : Type u_1} {α : ι → Type u_2} (i : ι) (t : (i : ι) → set (α i)) : pi (singleton i) t = set_of fun (x : (i : ι) → α i) => x i ∈ t i := singleton_pi i t theorem pi_if {ι : Type u_1} {α : ι → Type u_2} {p : ι → Prop} [h : decidable_pred p] (s : set ι) (t₁ : (i : ι) → set (α i)) (t₂ : (i : ι) → set (α i)) : (pi s fun (i : ι) => ite (p i) (t₁ i) (t₂ i)) = pi (has_sep.sep (fun (i : ι) => p i) s) t₁ ∩ pi (has_sep.sep (fun (i : ι) => ¬p i) s) t₂ := sorry theorem union_pi {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {s₁ : set ι} {t : (i : ι) → set (α i)} : pi (s ∪ s₁) t = pi s t ∩ pi s₁ t := sorry @[simp] theorem pi_inter_compl {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} (s : set ι) : pi s t ∩ pi (sᶜ) t = pi univ t := eq.mpr (id (Eq._oldrec (Eq.refl (pi s t ∩ pi (sᶜ) t = pi univ t)) (Eq.symm union_pi))) (eq.mpr (id (Eq._oldrec (Eq.refl (pi (s ∪ (sᶜ)) t = pi univ t)) (union_compl_self s))) (Eq.refl (pi univ t))) theorem pi_update_of_not_mem {ι : Type u_1} {α : ι → Type u_2} {s : set ι} [DecidableEq ι] {β : ι → Type u_3} {i : ι} (hi : ¬i ∈ s) (f : (j : ι) → α j) (a : α i) (t : (j : ι) → α j → set (β j)) : (pi s fun (j : ι) => t j (function.update f i a j)) = pi s fun (j : ι) => t j (f j) := sorry theorem pi_update_of_mem {ι : Type u_1} {α : ι → Type u_2} {s : set ι} [DecidableEq ι] {β : ι → Type u_3} {i : ι} (hi : i ∈ s) (f : (j : ι) → α j) (a : α i) (t : (j : ι) → α j → set (β j)) : (pi s fun (j : ι) => t j (function.update f i a j)) = (set_of fun (x : (i : ι) → β i) => x i ∈ t i a) ∩ pi (s \ singleton i) fun (j : ι) => t j (f j) := sorry theorem univ_pi_update {ι : Type u_1} {α : ι → Type u_2} [DecidableEq ι] {β : ι → Type u_3} (i : ι) (f : (j : ι) → α j) (a : α i) (t : (j : ι) → α j → set (β j)) : (pi univ fun (j : ι) => t j (function.update f i a j)) = (set_of fun (x : (i : ι) → β i) => x i ∈ t i a) ∩ pi (singleton iᶜ) fun (j : ι) => t j (f j) := sorry theorem univ_pi_update_univ {ι : Type u_1} {α : ι → Type u_2} [DecidableEq ι] (i : ι) (s : set (α i)) : pi univ (function.update (fun (j : ι) => univ) i s) = function.eval i ⁻¹' s := sorry theorem eval_image_pi {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} {i : ι} (hs : i ∈ s) (ht : set.nonempty (pi s t)) : function.eval i '' pi s t = t i := sorry @[simp] theorem eval_image_univ_pi {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} {i : ι} (ht : set.nonempty (pi univ t)) : (fun (f : (i : ι) → α i) => f i) '' pi univ t = t i := eval_image_pi (mem_univ i) ht theorem update_preimage_pi {ι : Type u_1} {α : ι → Type u_2} {s : set ι} {t : (i : ι) → set (α i)} {i : ι} {f : (i : ι) → α i} (hi : i ∈ s) (hf : ∀ (j : ι), j ∈ s → j ≠ i → f j ∈ t j) : function.update f i ⁻¹' pi s t = t i := sorry theorem update_preimage_univ_pi {ι : Type u_1} {α : ι → Type u_2} {t : (i : ι) → set (α i)} {i : ι} {f : (i : ι) → α i} (hf : ∀ (j : ι), j ≠ i → f j ∈ t j) : function.update f i ⁻¹' pi univ t = t i := update_preimage_pi (mem_univ i) fun (j : ι) (_x : j ∈ univ) => hf j theorem subset_pi_eval_image {ι : Type u_1} {α : ι → Type u_2} (s : set ι) (u : set ((i : ι) → α i)) : u ⊆ pi s fun (i : ι) => function.eval i '' u := fun (f : (i : ι) → α i) (hf : f ∈ u) (i : ι) (hi : i ∈ s) => Exists.intro f { left := hf, right := rfl } /-! ### Lemmas about `inclusion`, the injection of subtypes induced by `⊆` -/ /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/ def inclusion {α : Type u_1} {s : set α} {t : set α} (h : s ⊆ t) : ↥s → ↥t := fun (x : ↥s) => { val := ↑x, property := sorry } @[simp] theorem inclusion_self {α : Type u_1} {s : set α} (x : ↥s) : inclusion (subset.refl s) x = x := subtype.cases_on x fun (x_val : α) (x_property : x_val ∈ s) => Eq.refl (inclusion (subset.refl s) { val := x_val, property := x_property }) @[simp] theorem inclusion_right {α : Type u_1} {s : set α} {t : set α} (h : s ⊆ t) (x : ↥t) (m : ↑x ∈ s) : inclusion h { val := ↑x, property := m } = x := sorry @[simp] theorem inclusion_inclusion {α : Type u_1} {s : set α} {t : set α} {u : set α} (hst : s ⊆ t) (htu : t ⊆ u) (x : ↥s) : inclusion htu (inclusion hst x) = inclusion (subset.trans hst htu) x := subtype.cases_on x fun (x_val : α) (x_property : x_val ∈ s) => Eq.refl (inclusion htu (inclusion hst { val := x_val, property := x_property })) @[simp] theorem coe_inclusion {α : Type u_1} {s : set α} {t : set α} (h : s ⊆ t) (x : ↥s) : ↑(inclusion h x) = ↑x := rfl theorem inclusion_injective {α : Type u_1} {s : set α} {t : set α} (h : s ⊆ t) : function.injective (inclusion h) := sorry theorem eq_of_inclusion_surjective {α : Type u_1} {s : set α} {t : set α} {h : s ⊆ t} (h_surj : function.surjective (inclusion h)) : s = t := sorry theorem range_inclusion {α : Type u_1} {s : set α} {t : set α} (h : s ⊆ t) : range (inclusion h) = set_of fun (x : ↥t) => ↑x ∈ s := sorry /-! ### Injectivity and surjectivity lemmas for image and preimage -/ @[simp] theorem preimage_injective {α : Type u} {β : Type v} {f : α → β} : function.injective (preimage f) ↔ function.surjective f := sorry @[simp] theorem preimage_surjective {α : Type u} {β : Type v} {f : α → β} : function.surjective (preimage f) ↔ function.injective f := sorry @[simp] theorem image_surjective {α : Type u} {β : Type v} {f : α → β} : function.surjective (image f) ↔ function.surjective f := sorry @[simp] theorem image_injective {α : Type u} {β : Type v} {f : α → β} : function.injective (image f) ↔ function.injective f := sorry /-! ### Lemmas about images of binary and ternary functions -/ /-- The image of a binary function `f : α → β → γ` as a function `set α → set β → set γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def image2 {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) (s : set α) (t : set β) : set γ := set_of fun (c : γ) => ∃ (a : α), ∃ (b : β), a ∈ s ∧ b ∈ t ∧ f a b = c theorem mem_image2_eq {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {c : γ} : c ∈ image2 f s t = ∃ (a : α), ∃ (b : β), a ∈ s ∧ b ∈ t ∧ f a b = c := rfl @[simp] theorem mem_image2 {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {c : γ} : c ∈ image2 f s t ↔ ∃ (a : α), ∃ (b : β), a ∈ s ∧ b ∈ t ∧ f a b = c := iff.rfl theorem mem_image2_of_mem {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {a : α} {b : β} (h1 : a ∈ s) (h2 : b ∈ t) : f a b ∈ image2 f s t := Exists.intro a (Exists.intro b { left := h1, right := { left := h2, right := rfl } }) theorem mem_image2_iff {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {a : α} {b : β} (hf : function.injective2 f) : f a b ∈ image2 f s t ↔ a ∈ s ∧ b ∈ t := sorry /-- image2 is monotone with respect to `⊆`. -/ theorem image2_subset {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {s' : set α} {t : set β} {t' : set β} (hs : s ⊆ s') (ht : t ⊆ t') : image2 f s t ⊆ image2 f s' t' := sorry theorem forall_image2_iff {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {p : γ → Prop} : (∀ (z : γ), z ∈ image2 f s t → p z) ↔ ∀ (x : α), x ∈ s → ∀ (y : β), y ∈ t → p (f x y) := sorry @[simp] theorem image2_subset_iff {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {u : set γ} : image2 f s t ⊆ u ↔ ∀ (x : α), x ∈ s → ∀ (y : β), y ∈ t → f x y ∈ u := forall_image2_iff theorem image2_union_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {s' : set α} {t : set β} : image2 f (s ∪ s') t = image2 f s t ∪ image2 f s' t := sorry theorem image2_union_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {t' : set β} : image2 f s (t ∪ t') = image2 f s t ∪ image2 f s t' := sorry @[simp] theorem image2_empty_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {t : set β} : image2 f ∅ t = ∅ := sorry @[simp] theorem image2_empty_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} : image2 f s ∅ = ∅ := sorry theorem image2_inter_subset_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {s' : set α} {t : set β} : image2 f (s ∩ s') t ⊆ image2 f s t ∩ image2 f s' t := sorry theorem image2_inter_subset_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} {t' : set β} : image2 f s (t ∩ t') ⊆ image2 f s t ∩ image2 f s t' := sorry @[simp] theorem image2_singleton_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {t : set β} {a : α} : image2 f (singleton a) t = f a '' t := sorry @[simp] theorem image2_singleton_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {b : β} : image2 f s (singleton b) = (fun (a : α) => f a b) '' s := sorry theorem image2_singleton {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {a : α} {b : β} : image2 f (singleton a) (singleton b) = singleton (f a b) := sorry theorem image2_congr {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {f' : α → β → γ} {s : set α} {t : set β} (h : ∀ (a : α), a ∈ s → ∀ (b : β), b ∈ t → f a b = f' a b) : image2 f s t = image2 f' s t := sorry /-- A common special case of `image2_congr` -/ theorem image2_congr' {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {f' : α → β → γ} {s : set α} {t : set β} (h : ∀ (a : α) (b : β), f a b = f' a b) : image2 f s t = image2 f' s t := image2_congr fun (a : α) (_x : a ∈ s) (b : β) (_x : b ∈ t) => h a b /-- The image of a ternary function `f : α → β → γ → δ` as a function `set α → set β → set γ → set δ`. Mathematically this should be thought of as the image of the corresponding function `α × β × γ → δ`. -/ def image3 {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (g : α → β → γ → δ) (s : set α) (t : set β) (u : set γ) : set δ := set_of fun (d : δ) => ∃ (a : α), ∃ (b : β), ∃ (c : γ), a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d @[simp] theorem mem_image3 {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {g : α → β → γ → δ} {s : set α} {t : set β} {u : set γ} {d : δ} : d ∈ image3 g s t u ↔ ∃ (a : α), ∃ (b : β), ∃ (c : γ), a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d := iff.rfl theorem image3_congr {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {g : α → β → γ → δ} {g' : α → β → γ → δ} {s : set α} {t : set β} {u : set γ} (h : ∀ (a : α), a ∈ s → ∀ (b : β), b ∈ t → ∀ (c : γ), c ∈ u → g a b c = g' a b c) : image3 g s t u = image3 g' s t u := sorry /-- A common special case of `image3_congr` -/ theorem image3_congr' {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {g : α → β → γ → δ} {g' : α → β → γ → δ} {s : set α} {t : set β} {u : set γ} (h : ∀ (a : α) (b : β) (c : γ), g a b c = g' a b c) : image3 g s t u = image3 g' s t u := image3_congr fun (a : α) (_x : a ∈ s) (b : β) (_x : b ∈ t) (c : γ) (_x : c ∈ u) => h a b c theorem image2_image2_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} {s : set α} {t : set β} {u : set γ} (f : δ → γ → ε) (g : α → β → δ) : image2 f (image2 g s t) u = image3 (fun (a : α) (b : β) (c : γ) => f (g a b) c) s t u := sorry theorem image2_image2_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} {s : set α} {t : set β} {u : set γ} (f : α → δ → ε) (g : β → γ → δ) : image2 f s (image2 g t u) = image3 (fun (a : α) (b : β) (c : γ) => f a (g b c)) s t u := sorry theorem image2_assoc {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} {s : set α} {t : set β} {u : set γ} {ε' : Type u_6} {f : δ → γ → ε} {g : α → β → δ} {f' : α → ε' → ε} {g' : β → γ → ε'} (h_assoc : ∀ (a : α) (b : β) (c : γ), f (g a b) c = f' a (g' b c)) : image2 f (image2 g s t) u = image2 f' s (image2 g' t u) := sorry theorem image_image2 {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {s : set α} {t : set β} (f : α → β → γ) (g : γ → δ) : g '' image2 f s t = image2 (fun (a : α) (b : β) => g (f a b)) s t := sorry theorem image2_image_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {s : set α} {t : set β} (f : γ → β → δ) (g : α → γ) : image2 f (g '' s) t = image2 (fun (a : α) (b : β) => f (g a) b) s t := sorry theorem image2_image_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {s : set α} {t : set β} (f : α → γ → δ) (g : β → γ) : image2 f s (g '' t) = image2 (fun (a : α) (b : β) => f a (g b)) s t := sorry theorem image2_swap {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) (s : set α) (t : set β) : image2 f s t = image2 (fun (a : β) (b : α) => f b a) t s := sorry @[simp] theorem image2_left {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} (h : set.nonempty t) : image2 (fun (x : α) (y : β) => x) s t = s := sorry @[simp] theorem image2_right {α : Type u_1} {β : Type u_2} {s : set α} {t : set β} (h : set.nonempty s) : image2 (fun (x : α) (y : β) => y) s t = t := sorry @[simp] theorem image_prod {α : Type u_1} {β : Type u_2} {γ : Type u_3} {s : set α} {t : set β} (f : α → β → γ) : (fun (x : α × β) => f (prod.fst x) (prod.snd x)) '' set.prod s t = image2 f s t := sorry theorem nonempty.image2 {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β → γ} {s : set α} {t : set β} (hs : set.nonempty s) (ht : set.nonempty t) : set.nonempty (image2 f s t) := sorry end set namespace subsingleton theorem eq_univ_of_nonempty {α : Type u_1} [subsingleton α] {s : set α} : set.nonempty s → s = set.univ := sorry theorem set_cases {α : Type u_1} [subsingleton α] {p : set α → Prop} (h0 : p ∅) (h1 : p set.univ) (s : set α) : p s := or.elim (set.eq_empty_or_nonempty s) (fun (h : s = ∅) => Eq.symm h ▸ h0) fun (h : set.nonempty s) => Eq.symm (eq_univ_of_nonempty h) ▸ h1
94984c5829283787c9d284ad4265d96ce0fa5e03
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/prvNameWithMacroScopes.lean
cd97591112eda43069f5898de3cd4b026ef53ca4
[ "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
38
lean
import Lean #print Lean.instBEqFVarId
33934d4545fc8d75c988219766afd788949bf38d
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/concrete_category/bundled_hom.lean
0791c841b05098612cf461944fcb68a6e5390438
[ "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
5,758
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Yury Kudryashov -/ import category_theory.concrete_category.basic import category_theory.concrete_category.bundled /-! # Category instances for algebraic structures that use bundled homs. Many algebraic structures in Lean initially used unbundled homs (e.g. a bare function between types, along with an `is_monoid_hom` typeclass), but the general trend is towards using bundled homs. This file provides a basic infrastructure to define concrete categories using bundled homs, and define forgetful functors between them. -/ universes u namespace category_theory variables {c : Type u → Type u} (hom : Π ⦃α β : Type u⦄ (Iα : c α) (Iβ : c β), Type u) /-- Class for bundled homs. Note that the arguments order follows that of lemmas for `monoid_hom`. This way we can use `⟨@monoid_hom.to_fun, @monoid_hom.id ...⟩` in an instance. -/ structure bundled_hom := (to_fun : Π {α β : Type u} (Iα : c α) (Iβ : c β), hom Iα Iβ → α → β) (id : Π {α : Type u} (I : c α), hom I I) (comp : Π {α β γ : Type u} (Iα : c α) (Iβ : c β) (Iγ : c γ), hom Iβ Iγ → hom Iα Iβ → hom Iα Iγ) (hom_ext : ∀ {α β : Type u} (Iα : c α) (Iβ : c β), function.injective (to_fun Iα Iβ) . obviously) (id_to_fun : ∀ {α : Type u} (I : c α), to_fun I I (id I) = _root_.id . obviously) (comp_to_fun : ∀ {α β γ : Type u} (Iα : c α) (Iβ : c β) (Iγ : c γ) (f : hom Iα Iβ) (g : hom Iβ Iγ), to_fun Iα Iγ (comp Iα Iβ Iγ g f) = (to_fun Iβ Iγ g) ∘ (to_fun Iα Iβ f) . obviously) attribute [class] bundled_hom attribute [simp] bundled_hom.id_to_fun bundled_hom.comp_to_fun namespace bundled_hom variable [𝒞 : bundled_hom hom] include 𝒞 /-- Every `@bundled_hom c _` defines a category with objects in `bundled c`. This instance generates the type-class problem `bundled_hom ?m` (which is why this is marked as `[nolint]`). Currently that is not a problem, as there are almost no instances of `bundled_hom`. -/ @[nolint dangerous_instance] instance category : category (bundled c) := by refine { hom := λ X Y, @hom X Y X.str Y.str, id := λ X, @bundled_hom.id c hom 𝒞 X X.str, comp := λ X Y Z f g, @bundled_hom.comp c hom 𝒞 X Y Z X.str Y.str Z.str g f, comp_id' := _, id_comp' := _, assoc' := _}; intros; apply 𝒞.hom_ext; simp only [𝒞.id_to_fun, 𝒞.comp_to_fun, function.left_id, function.right_id] /-- A category given by `bundled_hom` is a concrete category. This instance generates the type-class problem `bundled_hom ?m` (which is why this is marked as `[nolint]`). Currently that is not a problem, as there are almost no instances of `bundled_hom`. -/ @[nolint dangerous_instance] instance concrete_category : concrete_category.{u} (bundled c) := { forget := { obj := λ X, X, map := λ X Y f, 𝒞.to_fun X.str Y.str f, map_id' := λ X, 𝒞.id_to_fun X.str, map_comp' := by intros; erw 𝒞.comp_to_fun; refl }, forget_faithful := { map_injective' := by intros; apply 𝒞.hom_ext } } variables {hom} local attribute [instance] concrete_category.has_coe_to_fun /-- A version of `has_forget₂.mk'` for categories defined using `@bundled_hom`. -/ def mk_has_forget₂ {d : Type u → Type u} {hom_d : Π ⦃α β : Type u⦄ (Iα : d α) (Iβ : d β), Type u} [bundled_hom hom_d] (obj : Π ⦃α⦄, c α → d α) (map : Π {X Y : bundled c}, (X ⟶ Y) → ((bundled.map obj X) ⟶ (bundled.map obj Y))) (h_map : ∀ {X Y : bundled c} (f : X ⟶ Y), (map f : X → Y) = f) : has_forget₂ (bundled c) (bundled d) := has_forget₂.mk' (bundled.map @obj) (λ _, rfl) @map (by intros; apply heq_of_eq; apply h_map) variables {d : Type u → Type u} variables (hom) section omit 𝒞 /-- The `hom` corresponding to first forgetting along `F`, then taking the `hom` associated to `c`. For typical usage, see the construction of `CommMon` from `Mon`. -/ @[reducible] def map_hom (F : Π {α}, d α → c α) : Π ⦃α β : Type u⦄ (Iα : d α) (Iβ : d β), Type u := λ α β iα iβ, hom (F iα) (F iβ) end /-- Construct the `bundled_hom` induced by a map between type classes. This is useful for building categories such as `CommMon` from `Mon`. -/ def map (F : Π {α}, d α → c α) : bundled_hom (map_hom hom @F) := { to_fun := λ α β iα iβ f, 𝒞.to_fun (F iα) (F iβ) f, id := λ α iα, 𝒞.id (F iα), comp := λ α β γ iα iβ iγ f g, 𝒞.comp (F iα) (F iβ) (F iγ) f g, hom_ext := λ α β iα iβ f g h, 𝒞.hom_ext (F iα) (F iβ) h } section omit 𝒞 /-- We use the empty `parent_projection` class to label functions like `comm_monoid.to_monoid`, which we would like to use to automatically construct `bundled_hom` instances from. Once we've set up `Mon` as the category of bundled monoids, this allows us to set up `CommMon` by defining an instance ```instance : parent_projection (comm_monoid.to_monoid) := ⟨⟩``` -/ class parent_projection (F : Π {α}, d α → c α) end @[nolint unused_arguments] -- The `parent_projection` typeclass is just a marker, so won't be used. instance bundled_hom_of_parent_projection (F : Π {α}, d α → c α) [parent_projection @F] : bundled_hom (map_hom hom @F) := map hom @F instance forget₂ (F : Π {α}, d α → c α) [parent_projection @F] : has_forget₂ (bundled d) (bundled c) := { forget₂ := { obj := λ X, ⟨X, F X.2⟩, map := λ X Y f, f } } instance forget₂_full (F : Π {α}, d α → c α) [parent_projection @F] : full (forget₂ (bundled d) (bundled c)) := { preimage := λ X Y f, f } end bundled_hom end category_theory
5131bd9dc5d32beb4ffde6db6dae42f6e2c7926d
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/testing/slim_check/functions.lean
06047a74962a2d8752036eae97fdad9f0fffc426
[ "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
16,959
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import data.list.sigma import testing.slim_check.sampleable import testing.slim_check.testable import tactic.pretty_cases /-! ## `slim_check`: generators for functions This file defines `sampleable` instances for `α → β` functions and `ℤ → ℤ` injective functions. Functions are generated by creating a list of pairs and one more value using the list as a lookup table and resorting to the additional value when a value is not found in the table. Injective functions are generated by creating a list of numbers and a permutation of that list. The permutation insures that every input is mapped to a unique output. When an input is not found in the list the input itself is used as an output. Injective functions `f : α → α` could be generated easily instead of `ℤ → ℤ` by generating a `list α`, removing duplicates and creating a permutations. One has to be careful when generating the domain to make if vast enough that, when generating arguments to apply `f` to, they argument should be likely to lie in the domain of `f`. This is the reason that injective functions `f : ℤ → ℤ` are generated by fixing the domain to the range `[-2*size .. -2*size]`, with `size` the size parameter of the `gen` monad. Much of the machinery provided in this file is applicable to generate injective functions of type `α → α` and new instances should be easy to define. Other classes of functions such as monotone functions can generated using similar techniques. For monotone functions, generating two lists, sorting them and matching them should suffice, with appropriate default values. Some care must be taken for shrinking such functions to make sure their defining property is invariant through shrinking. Injective functions are an example of how complicated it can get. -/ universes u v w namespace slim_check /-- Data structure specifying a total function using a list of pairs and a default value returned when the input is not in the domain of the partial function. `with_default f y` encodes `x ↦ f x` when `x ∈ f` and `x ↦ y` otherwise. We use `Σ` to encode mappings instead of `×` because we rely on the association list API defined in `data.list.sigma`. -/ inductive total_function (α : Type u) (β : Type v) : Type (max u v) | with_default : list (Σ _ : α, β) → β → total_function instance total_function.inhabited {α β} [inhabited β] : inhabited (total_function α β) := ⟨ total_function.with_default ∅ (default _) ⟩ namespace total_function /-- Apply a total function to an argument. -/ def apply {α β : Type*} [decidable_eq α] : total_function α β → α → β | (total_function.with_default m y) x := (m.lookup x).get_or_else y /-- Implementation of `has_repr (total_function α β)`. Creates a string for a given `finmap` and output, `x₀ ↦ y₀, .. xₙ ↦ yₙ` for each of the entries. The brackets are provided by the calling function. -/ def repr_aux {α : Type u} [has_repr α] {β : Type v} [has_repr β] (m : list (Σ _ : α, β)) : string := string.join $ list.qsort (λ x y, x < y) (m.map $ λ x, sformat!"{repr $ sigma.fst x} ↦ {repr $ sigma.snd x}, ") /-- Produce a string for a given `total_function`. The output is of the form `[x₀ ↦ f x₀, .. xₙ ↦ f xₙ, _ ↦ y]`. -/ protected def repr {α : Type u} [has_repr α] {β : Type v} [has_repr β] : total_function α β → string | (total_function.with_default m y) := sformat!"[{repr_aux m}_ ↦ {has_repr.repr y}]" instance (α : Type u) (β : Type v) [has_repr α] [has_repr β] : has_repr (total_function α β) := ⟨ total_function.repr ⟩ /-- Create a `finmap` from a list of pairs. -/ def list.to_finmap' {α β} (xs : list (α × β)) : list (Σ _ : α, β) := xs.map prod.to_sigma section variables {α : Type u} {β : Type v} [sampleable α] [sampleable β] /-- Redefine `sizeof` to follow the structure of `sampleable` instances. -/ def total.sizeof : total_function α β → ℕ | ⟨m, x⟩ := 1 + @sizeof _ sampleable.wf m + sizeof x @[priority 2000] instance : has_sizeof (total_function α β) := ⟨ total.sizeof ⟩ variables [decidable_eq α] /-- Shrink a total function by shrinking the lists that represent it. -/ protected def shrink : shrink_fn (total_function α β) | ⟨m, x⟩ := (sampleable.shrink (m, x)).map $ λ ⟨⟨m', x'⟩, h⟩, ⟨⟨list.erase_dupkeys m', x'⟩, lt_of_le_of_lt (by unfold_wf; refine @list.sizeof_erase_dupkeys _ _ _ (@sampleable.wf _ _) _) h ⟩ variables [has_repr α] [has_repr β] instance pi.sampleable_ext : sampleable_ext (α → β) := { proxy_repr := total_function α β, interp := total_function.apply, sample := do { xs ← (sampleable.sample (list (α × β)) : gen ((list (α × β)))), ⟨x⟩ ← (uliftable.up $ sample β : gen (ulift.{(max u v)} β)), pure $ total_function.with_default (list.to_finmap' xs) x }, shrink := total_function.shrink } end section sampleable_ext open sampleable_ext @[priority 2000] instance pi_pred.sampleable_ext {α : Type u} [sampleable_ext (α → bool)] : sampleable_ext.{u+1} (α → Prop) := { proxy_repr := proxy_repr (α → bool), interp := λ m x, interp (α → bool) m x, sample := sample (α → bool), shrink := shrink } @[priority 2000] instance pi_uncurry.sampleable_ext {α : Type u} {β : Type v} {γ : Sort w} [sampleable_ext (α × β → γ)] : sampleable_ext.{(imax (u+1) (v+1) w)} (α → β → γ) := { proxy_repr := proxy_repr (α × β → γ), interp := λ m x y, interp (α × β → γ) m (x, y), sample := sample (α × β → γ), shrink := shrink } end sampleable_ext end total_function /-- Data structure specifying a total function using a list of pairs and a default value returned when the input is not in the domain of the partial function. `map_to_self f` encodes `x ↦ f x` when `x ∈ f` and `x ↦ x`, i.e. `x` to itself, otherwise. We use `Σ` to encode mappings instead of `×` because we rely on the association list API defined in `data.list.sigma`. -/ inductive injective_function (α : Type u) : Type u | map_to_self (xs : list (Σ _ : α, α)) : xs.map sigma.fst ~ xs.map sigma.snd → list.nodup (xs.map sigma.snd) → injective_function instance {α} : inhabited (injective_function α) := ⟨ ⟨ [], list.perm.nil, list.nodup_nil ⟩ ⟩ namespace injective_function /-- Apply a total function to an argument. -/ def apply {α : Type u} [decidable_eq α] : injective_function α → α → α | (injective_function.map_to_self m _ _) x := (m.lookup x).get_or_else x /-- Produce a string for a given `total_function`. The output is of the form `[x₀ ↦ f x₀, .. xₙ ↦ f xₙ, x ↦ x]`. Unlike for `total_function`, the default value is not a constant but the identity function. -/ protected def repr {α : Type u} [has_repr α] : injective_function α → string | (injective_function.map_to_self m _ _) := sformat!"[{total_function.repr_aux m}x ↦ x]" instance (α : Type u) [has_repr α] : has_repr (injective_function α) := ⟨ injective_function.repr ⟩ /-- Interpret a list of pairs as a total function, defaulting to the identity function when no entries are found for a given function -/ def list.apply_id {α : Type u} [decidable_eq α] (xs : list (α × α)) (x : α) : α := ((xs.map prod.to_sigma).lookup x).get_or_else x @[simp] lemma list.apply_id_cons {α : Type u} [decidable_eq α] (xs : list (α × α)) (x y z : α) : list.apply_id ((y, z) :: xs) x = if y = x then z else list.apply_id xs x := by simp only [list.apply_id, list.lookup, eq_rec_constant, prod.to_sigma, list.map]; split_ifs; refl open function list prod (to_sigma) open nat lemma list.apply_id_zip_eq {α : Type u} [decidable_eq α] {xs ys : list α} (h₀ : list.nodup xs) (h₁ : xs.length = ys.length) (x y : α) (i : ℕ) (h₂ : xs.nth i = some x) : list.apply_id.{u} (xs.zip ys) x = y ↔ ys.nth i = some y := begin induction xs generalizing ys i, case list.nil : ys i h₁ h₂ { cases h₂ }, case list.cons : x' xs xs_ih ys i h₁ h₂ { cases i, { injection h₂ with h₀ h₁, subst h₀, cases ys, { cases h₁ }, { simp only [list.apply_id, to_sigma, option.get_or_else_some, nth, lookup_cons_eq, zip_cons_cons, list.map], } }, { cases ys, { cases h₁ }, { cases h₀ with _ _ h₀ h₁, simp only [nth, zip_cons_cons, list.apply_id_cons] at h₂ ⊢, rw if_neg, { apply xs_ih; solve_by_elim [succ.inj] }, { apply h₀, apply nth_mem h₂ } } } } end lemma apply_id_mem_iff {α : Type u} [decidable_eq α] {xs ys : list α} (h₀ : list.nodup xs) (h₁ : xs ~ ys) (x : α) : list.apply_id.{u} (xs.zip ys) x ∈ ys ↔ x ∈ xs := begin simp only [list.apply_id], cases h₃ : (lookup x (map prod.to_sigma (xs.zip ys))), { dsimp [option.get_or_else], rw h₁.mem_iff }, { have h₂ : ys.nodup := h₁.nodup_iff.1 h₀, replace h₁ : xs.length = ys.length := h₁.length_eq, dsimp, induction xs generalizing ys, case list.nil : ys h₃ h₂ h₁ { contradiction }, case list.cons : x' xs xs_ih ys h₃ h₂ h₁ { cases ys with y ys, { cases h₃ }, dsimp [lookup] at h₃, split_ifs at h₃, { subst x', subst val, simp only [mem_cons_iff, true_or, eq_self_iff_true], }, { cases h₀ with _ _ h₀ h₅, cases h₂ with _ _ h₂ h₄, have h₆ := nat.succ.inj h₁, specialize @xs_ih h₅ ys h₃ h₄ h₆, simp only [ne.symm h, xs_ih, mem_cons_iff, false_or], suffices : val ∈ ys, tauto!, erw [← option.mem_def, mem_lookup_iff] at h₃, simp only [to_sigma, mem_map, heq_iff_eq, prod.exists] at h₃, rcases h₃ with ⟨a, b, h₃, h₄, h₅⟩, subst a, subst b, apply (mem_zip h₃).2, simp only [nodupkeys, keys, comp, prod.fst_to_sigma, map_map], rwa map_fst_zip _ _ (le_of_eq h₆) } } } end lemma list.apply_id_eq_self {α : Type u} [decidable_eq α] {xs ys : list α} (x : α) : x ∉ xs → list.apply_id.{u} (xs.zip ys) x = x := begin intro h, dsimp [list.apply_id], rw lookup_eq_none.2, refl, simp only [keys, not_exists, to_sigma, exists_and_distrib_right, exists_eq_right, mem_map, comp_app, map_map, prod.exists], intros y hy, exact h (mem_zip hy).1, end lemma apply_id_injective {α : Type u} [decidable_eq α] {xs ys : list α} (h₀ : list.nodup xs) (h₁ : xs ~ ys) : injective.{u+1 u+1} (list.apply_id (xs.zip ys)) := begin intros x y h, by_cases hx : x ∈ xs; by_cases hy : y ∈ xs, { rw mem_iff_nth at hx hy, cases hx with i hx, cases hy with j hy, suffices : some x = some y, { injection this }, have h₂ := h₁.length_eq, rw [list.apply_id_zip_eq h₀ h₂ _ _ _ hx] at h, rw [← hx, ← hy], congr, apply nth_injective _ (h₁.nodup_iff.1 h₀), { symmetry, rw h, rw ← list.apply_id_zip_eq; assumption }, { rw ← h₁.length_eq, rw nth_eq_some at hx, cases hx with hx hx', exact hx } }, { rw ← apply_id_mem_iff h₀ h₁ at hx hy, rw h at hx, contradiction, }, { rw ← apply_id_mem_iff h₀ h₁ at hx hy, rw h at hx, contradiction, }, { rwa [list.apply_id_eq_self, list.apply_id_eq_self] at h; assumption }, end open total_function (list.to_finmap') open sampleable /-- Remove a slice of length `m` at index `n` in a list and a permutation, maintaining the property that it is a permutation. -/ def perm.slice {α} [decidable_eq α] (n m : ℕ) : (Σ' xs ys : list α, xs ~ ys ∧ ys.nodup) → (Σ' xs ys : list α, xs ~ ys ∧ ys.nodup) | ⟨xs, ys, h, h'⟩ := let xs' := list.slice n m xs in have h₀ : xs' ~ ys.inter xs', from perm.slice_inter _ _ h h', ⟨xs', ys.inter xs', h₀, nodup_inter_of_nodup _ h'⟩ /-- A lazy list, in decreasing order, of sizes that should be sliced off a list of length `n` -/ def slice_sizes : ℕ → lazy_list ℕ+ | n := if h : 0 < n then have n / 2 < n, from div_lt_self h dec_trivial, lazy_list.cons ⟨_, h⟩ (slice_sizes $ n / 2) else lazy_list.nil /-- Shrink a permutation of a list, slicing a segment in the middle. The sizes of the slice being removed start at `n` (with `n` the length of the list) and then `n / 2`, then `n / 4`, etc down to 1. The slices will be taken at index `0`, `n / k`, `2n / k`, `3n / k`, etc. -/ protected def shrink_perm {α} [decidable_eq α] [has_sizeof α] : shrink_fn (Σ' xs ys : list α, xs ~ ys ∧ ys.nodup) | xs := do let k := xs.1.length, n ← slice_sizes k, i ← lazy_list.of_list $ list.fin_range $ k / n, have ↑i * ↑n < xs.1.length, from nat.lt_of_div_lt_div (lt_of_le_of_lt (by simp only [nat.mul_div_cancel, gt_iff_lt, fin.val_eq_coe, pnat.pos]) i.2), pure ⟨perm.slice (i*n) n xs, by rcases xs with ⟨a,b,c,d⟩; dsimp [sizeof_lt]; unfold_wf; simp only [perm.slice]; unfold_wf; apply list.sizeof_slice_lt _ _ n.2 _ this⟩ instance {α} [has_sizeof α] : has_sizeof (injective_function α) := ⟨ λ ⟨xs,_,_⟩, sizeof (xs.map sigma.fst) ⟩ /-- Shrink an injective function slicing a segment in the middle of the domain and removing the corresponding elements in the codomain, hence maintaining the property that one is a permutation of the other. -/ protected def shrink {α} [has_sizeof α] [decidable_eq α] : shrink_fn (injective_function α) | ⟨xs, h₀, h₁⟩ := do ⟨⟨xs', ys', h₀, h₁⟩, h₂⟩ ← injective_function.shrink_perm ⟨_, _, h₀, h₁⟩, have h₃ : xs'.length ≤ ys'.length, from le_of_eq (perm.length_eq h₀), have h₄ : ys'.length ≤ xs'.length, from le_of_eq (perm.length_eq h₀.symm), pure ⟨⟨(list.zip xs' ys').map prod.to_sigma, by simp only [comp, map_fst_zip, map_snd_zip, *, prod.fst_to_sigma, prod.snd_to_sigma, map_map], by simp only [comp, map_snd_zip, *, prod.snd_to_sigma, map_map] ⟩, by revert h₂; dsimp [sizeof_lt]; unfold_wf; simp only [has_sizeof._match_1, map_map, comp, map_fst_zip, *, prod.fst_to_sigma]; unfold_wf; intro h₂; convert h₂ ⟩ /-- Create an injective function from one list and a permutation of that list. -/ protected def mk {α} (xs ys : list α) (h : xs ~ ys) (h' : ys.nodup) : injective_function α := have h₀ : xs.length ≤ ys.length, from le_of_eq h.length_eq, have h₁ : ys.length ≤ xs.length, from le_of_eq h.length_eq.symm, injective_function.map_to_self (list.to_finmap' (xs.zip ys)) (by { simp only [list.to_finmap', comp, map_fst_zip, map_snd_zip, *, prod.fst_to_sigma, prod.snd_to_sigma, map_map] }) (by { simp only [list.to_finmap', comp, map_snd_zip, *, prod.snd_to_sigma, map_map] }) protected lemma injective {α : Type u} [decidable_eq α] (f : injective_function α) : injective (apply f) := begin cases f with xs hperm hnodup, generalize h₀ : map sigma.fst xs = xs₀, generalize h₁ : xs.map (@id ((Σ _ : α, α) → α) $ @sigma.snd α (λ _ : α, α)) = xs₁, dsimp [id] at h₁, have hxs : xs = total_function.list.to_finmap' (xs₀.zip xs₁), { rw [← h₀, ← h₁, list.to_finmap'], clear h₀ h₁ xs₀ xs₁ hperm hnodup, induction xs, case list.nil { simp only [zip_nil_right, map_nil] }, case list.cons : xs_hd xs_tl xs_ih { simp only [true_and, to_sigma, eq_self_iff_true, sigma.eta, zip_cons_cons, list.map], exact xs_ih }, }, revert hperm hnodup, rw hxs, intros, apply apply_id_injective, { rwa [← h₀, hxs, hperm.nodup_iff], }, { rwa [← hxs, h₀, h₁] at hperm, }, end instance pi_injective.sampleable_ext : sampleable_ext { f : ℤ → ℤ // function.injective f } := { proxy_repr := injective_function ℤ, interp := λ f, ⟨ apply f, f.injective ⟩, sample := gen.sized $ λ sz, do { let xs' := int.range (-(2*sz+2)) (2*sz + 2), ys ← gen.permutation_of xs', have Hinj : injective (λ (r : ℕ), -(2*sz + 2 : ℤ) + ↑r), from λ x y h, int.coe_nat_inj (add_right_injective _ h), let r : injective_function ℤ := injective_function.mk.{0} xs' ys.1 ys.2 (ys.2.nodup_iff.1 $ nodup_map Hinj (nodup_range _)) in pure r }, shrink := @injective_function.shrink ℤ _ _ } end injective_function open function instance injective.testable {α β} (f : α → β) [I : testable (named_binder "x" $ ∀ x : α, named_binder "y" $ ∀ y : α, named_binder "H" $ f x = f y → x = y)] : testable (injective f) := I instance monotone.testable {α β} [preorder α] [preorder β] (f : α → β) [I : testable (named_binder "x" $ ∀ x : α, named_binder "y" $ ∀ y : α, named_binder "H" $ x ≤ y → f x ≤ f y)] : testable (monotone f) := I end slim_check
ac4fe1458c3800df2a95106ec2f25315efa7a089
fffbc47930dc6615e66ece42324ce57a21d5b64b
/src/category_theory/instances/CommRing/adjunctions.lean
d4066eeecb20fa8d1d1e432986734ff65c7bb184
[ "Apache-2.0" ]
permissive
skbaek/mathlib
3caae8ae413c66862293a95fd2fbada3647b1228
f25340175631cdc85ad768a262433f968d0d6450
refs/heads/master
1,588,130,123,636
1,558,287,609,000
1,558,287,609,000
160,935,713
0
0
Apache-2.0
1,544,271,146,000
1,544,271,146,000
null
UTF-8
Lean
false
false
2,735
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl Multivariable polynomials on a type is the left adjoint of the forgetful functor from commutative rings to types. -/ import category_theory.instances.CommRing.basic import category_theory.adjunction import data.mv_polynomial universe u open mv_polynomial open category_theory open category_theory.instances namespace category_theory.instances.CommRing local attribute [instance, priority 0] subtype.fintype set_fintype classical.prop_decidable noncomputable def polynomial_ring : Type u ⥤ CommRing.{u} := { obj := λ α, ⟨mv_polynomial α ℤ, by apply_instance⟩, map := λ α β f, ⟨eval₂ C (X ∘ f), by apply_instance⟩, map_id' := λ α, subtype.ext.mpr $ funext $ eval₂_eta, map_comp' := λ α β γ f g, subtype.ext.mpr $ funext $ λ p, by apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_add, eval₂_mul, eval₂_C, eval₂_X, comp_val, eq_self_iff_true, function.comp_app, types_comp] at * } @[simp] lemma polynomial_ring_obj_α {α : Type u} : (polynomial_ring.obj α).α = mv_polynomial α ℤ := rfl @[simp] lemma polynomial_ring_map_val {α β : Type u} {f : α → β} : (polynomial_ring.map f).val = eval₂ C (X ∘ f) := rfl lemma hom_coe_app' {R S : CommRing} (f : R ⟶ S) (r : R) : f r = f.val r := rfl -- this is usually a bad idea, as it forgets that `f` is ring homomorphism local attribute [simp] hom_coe_app' noncomputable def adj : polynomial_ring ⊣ (forget : CommRing ⥤ Type u) := adjunction.mk_of_hom_equiv { hom_equiv := λ α R, { to_fun := λ f, f ∘ X, inv_fun := λ f, ⟨eval₂ int.cast f, by apply_instance⟩, left_inv := λ f, subtype.ext.mpr $ funext $ λ p, begin have H0 := λ n, (congr (int.eq_cast' (f.val ∘ C)) (rfl : n = n)).symm, have H1 := λ p₁ p₂, (@is_ring_hom.map_add _ _ _ _ f.val f.2 p₁ p₂).symm, have H2 := λ p₁ p₂, (@is_ring_hom.map_mul _ _ _ _ f.val f.2 p₁ p₂).symm, apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_add, eval₂_mul, eval₂_C, eval₂_X, eq_self_iff_true, function.comp_app, hom_coe_app'] at * end, right_inv := by tidy }, hom_equiv_naturality_left_symm' := λ X' X Y f g, subtype.ext.mpr $ funext $ λ p, begin apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_mul, eval₂_add, eval₂_C, eval₂_X, comp_val, equiv.coe_fn_symm_mk, hom_coe_app, polynomial_ring_map_val, eq_self_iff_true, function.comp_app, add_right_inj, types_comp] at * end } end category_theory.instances.CommRing
2d33492e01e9f862cb953d20e9c809de3114094c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/dsimpZetaIssue.lean
d7ab6a439e0e75bcdc88632894106c600b1cc621
[ "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
365
lean
example : let x := 0; x + 5 = 5 := by dsimp (config := { zeta := false }) trace_state simp example : let x := 0; x + 5 = 5 := by dsimp example : let x := 0; x + y = y := by dsimp trace_state rw [Nat.zero_add] example : let x := 0; x + y = y := by dsimp (config := { zeta := false }) trace_state conv => zeta trace_state rw [Nat.zero_add]
0f56ea06a59e209ed0fa5c08a9f80c75692c59c0
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/let_tac.lean
f4c63f817d7a03c9b325cd8d5e992267e5af2948
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
303
lean
import data.nat example (a b : Prop) : a → b → a ∧ b := begin intro Ha, intro Hb, let aux := and.intro Ha Hb, exact aux end open nat example (a b : nat) : a > 0 → b > 0 → a + b + 0 > 0 := begin intro agt0, intro bgt0, let H := add_pos agt0 bgt0, change a + b > 0, exact H end
26e2f432d855826e4900afcc168e5815e050f51c
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/analysis/special_functions/pow.lean
42d04feac9c3a905ab70e9e4988e8cf4d13229bd
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
24,683
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 -/ import analysis.special_functions.trigonometric /-! # Power function on `ℂ`, `ℝ` and `ℝ⁺` We construct the power functions `x ^ y` where `x` and `y` are complex numbers, or `x` and `y` are real numbers, or `x` is a nonnegative real and `y` is real, and prove their basic properties. -/ noncomputable theory open_locale classical real topological_space namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ noncomputable def cpow (x y : ℂ) : ℂ := if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩ @[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl lemma cpow_def (x y : ℂ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := rfl @[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def] @[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] } @[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 := by simp [cpow_def, *] @[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x := if hx : x = 0 then by simp [hx, cpow_def] else by rw [cpow_def, if_neg (@one_ne_zero ℂ _), if_neg hx, mul_one, exp_log hx] @[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 := by rw cpow_def; split_ifs; simp [one_ne_zero, *] at * lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by simp [cpow_def]; split_ifs; simp [*, exp_add, mul_add] at * lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) : x ^ (y * z) = (x ^ y) ^ z := begin simp [cpow_def], split_ifs; simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at * end lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ := by simp [cpow_def]; split_ifs; simp [exp_neg] @[simp] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n | 0 := by simp | (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ, complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul] else by simp [cpow_def, hx, mul_comm, mul_add, exp_add, pow_succ, (cpow_nat_cast n).symm, exp_log hx] @[simp] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n | (n : ℕ) := by simp; refl | -[1+ n] := by rw fpow_neg_succ_of_nat; simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div, int.cast_coe_nat, cpow_nat_cast] lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℂ)) ^ n = x := have (log x * (↑n)⁻¹).im = (log x).im / n, by rw [div_eq_mul_inv, ← of_real_nat_cast, ← of_real_inv, mul_im, of_real_re, of_real_im]; simp, have h : -π < (log x * (↑n)⁻¹).im ∧ (log x * (↑n)⁻¹).im ≤ π, from (le_total (log x).im 0).elim (λ h, ⟨calc -π < (log x).im : by simp [log, neg_pi_lt_arg] ... ≤ ((log x).im * 1) / n : le_div_of_mul_le (nat.cast_pos.2 hn) (mul_le_mul_of_nonpos_left (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h) ... = (log x * (↑n)⁻¹).im : by simp [this], this.symm ▸ le_trans (div_nonpos_of_nonpos_of_pos h (nat.cast_pos.2 hn)) (le_of_lt real.pi_pos)⟩) (λ h, ⟨this.symm ▸ lt_of_lt_of_le (neg_neg_of_pos real.pi_pos) (div_nonneg h (nat.cast_pos.2 hn)), calc (log x * (↑n)⁻¹).im = (1 * (log x).im) / n : by simp [this] ... ≤ (log x).im : (div_le_of_le_mul (nat.cast_pos.2 hn) (mul_le_mul_of_nonneg_right (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h)) ... ≤ _ : by simp [log, arg_le_pi]⟩), by rw [← cpow_nat_cast, ← cpow_mul _ h.1 h.2, inv_mul_cancel (show (n : ℂ) ≠ 0, from nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), cpow_one] end complex namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos lemma abs_rpow_le_abs_rpow (x y : ℝ) : abs (x ^ y) ≤ abs (x) ^ y := abs_le_of_le_of_neg_le begin cases lt_trichotomy 0 x, { rw abs_of_pos h }, cases h, { simp [h.symm] }, rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), log_abs], calc exp (log x * y) * cos (y * π) ≤ exp (log x * y) * 1 : mul_le_mul_of_nonneg_left (cos_le_one _) (le_of_lt $ exp_pos _) ... = _ : mul_one _ end begin cases lt_trichotomy 0 x, { rw abs_of_pos h, have : 0 < x^y := rpow_pos_of_pos h _, linarith }, cases h, { simp only [h.symm, abs_zero, rpow_def_of_nonneg], split_ifs, repeat {norm_num} }, rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), log_abs], calc -(exp (log x * y) * cos (y * π)) = exp (log x * y) * (-cos (y * π)) : by ring ... ≤ exp (log x * y) * 1 : mul_le_mul_of_nonneg_left (neg_le.2 $ neg_one_le_cos _) (le_of_lt $ exp_pos _) ... = exp (log x * y) : mul_one _ end end real namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp [real.rpow_def_of_nonneg hx, complex.cpow_def]; split_ifs; simp [complex.of_real_log hx] @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := begin rw [real.rpow_def_of_nonneg (abs_nonneg _), complex.cpow_def], split_ifs; simp [*, abs_of_nonneg (le_of_lt (real.exp_pos _)), complex.log, complex.exp_add, add_mul, mul_right_comm _ I, exp_mul_I, abs_cos_add_sin_mul_I, (complex.of_real_mul _ _).symm, -complex.of_real_mul] at * end @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] end complex namespace real variables {x y z : ℝ} @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma rpow_add {x : ℝ} (y z : ℝ) (hx : 0 < x) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * @[simp] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, (complex.of_real_pow _ _).symm, complex.cpow_nat_cast, complex.of_real_nat_cast, complex.of_real_re] @[simp] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, (complex.of_real_fpow _ _).symm, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, cases lt_or_eq_of_le h with h₂ h₂, exact h₂, exfalso, apply h_2, exact eq.symm h₂, have hy : 0 < y, cases lt_or_eq_of_le h₁ with h₂ h₂, exact h₂, exfalso, apply h_3, exact eq.symm h₂, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁}, { exact h}, { exact mul_nonneg h h₁}, end lemma one_le_rpow {x z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := begin rw real.rpow_def_of_nonneg, split_ifs with h₂ h₃, { refl}, { simp [*, not_le_of_gt zero_lt_one] at *}, { have hx : 0 < x, exact lt_of_lt_of_le zero_lt_one h, rw [←log_le_log zero_lt_one hx, log_one] at h, have pos : 0 ≤ log x * z, exact mul_nonneg h h₁, rwa [←exp_le_exp, exp_zero] at pos}, { exact le_trans zero_le_one h}, end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rw le_iff_eq_or_lt at h h₂, cases h₂, { rw [←h₂, rpow_zero, rpow_zero]}, { cases h, { rw [←h, zero_rpow], rw real.rpow_def_of_nonneg, split_ifs, { exact zero_le_one}, { refl}, { exact le_of_lt (exp_pos (log y * z))}, { rwa ←h at h₁}, { exact ne.symm (ne_of_lt h₂)}}, { have one_le : 1 ≤ y / x, rw one_le_div_iff_le h, exact h₁, have one_le_pow : 1 ≤ (y / x)^z, exact one_le_rpow one_le (le_of_lt h₂), rw [←mul_div_cancel y (ne.symm (ne_of_lt h)), mul_comm, mul_div_assoc], rw [mul_rpow (le_of_lt h) (le_trans zero_le_one one_le), mul_comm], exact (le_mul_of_ge_one_left (rpow_nonneg_of_nonneg (le_of_lt h) z) one_le_pow) } } end lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end lemma rpow_le_one {x e : ℝ} (he : 0 ≤ e) (hx : 0 ≤ x) (hx2 : x ≤ 1) : x^e ≤ 1 := by rw ←one_rpow e; apply rpow_le_rpow; assumption lemma one_lt_rpow (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma rpow_lt_one (hx : 0 < x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow (le_of_lt hx) hx1 hz } lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] section prove_rpow_is_continuous lemma continuous_rpow_aux1 : continuous (λp : {p:ℝ×ℝ // 0 < p.1}, p.val.1 ^ p.val.2) := suffices h : continuous (λ p : {p:ℝ×ℝ // 0 < p.1 }, exp (log p.val.1 * p.val.2)), by { convert h, ext p, rw rpow_def_of_pos p.2 }, continuous_exp.comp $ (show continuous ((λp:{p:ℝ//0 < p}, log (p.val)) ∘ (λp:{p:ℝ×ℝ//0<p.fst}, ⟨p.val.1, p.2⟩)), from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_fst.comp continuous_subtype_val).mul (continuous_snd.comp $ continuous_subtype_val.comp continuous_id) lemma continuous_rpow_aux2 : continuous (λ p : {p:ℝ×ℝ // p.1 < 0}, p.val.1 ^ p.val.2) := suffices h : continuous (λp:{p:ℝ×ℝ // p.1 < 0}, exp (log (-p.val.1) * p.val.2) * cos (p.val.2 * π)), by { convert h, ext p, rw [rpow_def_of_neg p.2, log_neg_eq_log] }, (continuous_exp.comp $ (show continuous $ (λp:{p:ℝ//0<p}, log (p.val))∘(λp:{p:ℝ×ℝ//p.1<0}, ⟨-p.val.1, neg_pos_of_neg p.2⟩), from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_neg.comp $ continuous_fst.comp continuous_subtype_val).mul (continuous_snd.comp $ continuous_subtype_val.comp continuous_id)).mul (continuous_cos.comp $ (continuous_snd.comp $ continuous_subtype_val.comp continuous_id).mul continuous_const) lemma continuous_at_rpow_of_ne_zero (hx : x ≠ 0) (y : ℝ) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := begin cases lt_trichotomy 0 x, exact continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux1 _ h) (mem_nhds_sets (by { convert is_open_prod (is_open_lt' (0:ℝ)) is_open_univ, ext, finish }) h), cases h, { exact absurd h.symm hx }, exact continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux2 _ h) (mem_nhds_sets (by { convert is_open_prod (is_open_gt' (0:ℝ)) is_open_univ, ext, finish }) h) end lemma continuous_rpow_aux3 : continuous (λ p : {p:ℝ×ℝ // 0 < p.2}, p.val.1 ^ p.val.2) := continuous_iff_continuous_at.2 $ λ ⟨(x₀, y₀), hy₀⟩, begin by_cases hx₀ : x₀ = 0, { simp only [continuous_at, hx₀, zero_rpow (ne_of_gt hy₀), metric.tendsto_nhds_nhds], assume ε ε0, rcases exists_pos_rat_lt (half_pos hy₀) with ⟨q, q_pos, q_lt⟩, let q := (q:ℝ), replace q_pos : 0 < q := rat.cast_pos.2 q_pos, let δ := min (min q (ε ^ (1 / q))) (1/2), have δ0 : 0 < δ := lt_min (lt_min q_pos (rpow_pos_of_pos ε0 _)) (by norm_num), have : δ ≤ q := le_trans (min_le_left _ _) (min_le_left _ _), have : δ ≤ ε ^ (1 / q) := le_trans (min_le_left _ _) (min_le_right _ _), have : δ < 1 := lt_of_le_of_lt (min_le_right _ _) (by norm_num), use δ, use δ0, rintros ⟨⟨x, y⟩, hy⟩, simp only [subtype.dist_eq, real.dist_eq, prod.dist_eq, sub_zero, subtype.coe_mk], assume h, rw max_lt_iff at h, cases h with xδ yy₀, have qy : q < y, calc q < y₀ / 2 : q_lt ... = y₀ - y₀ / 2 : (sub_half _).symm ... ≤ y₀ - δ : by linarith ... < y : sub_lt_of_abs_sub_lt_left yy₀, calc abs(x^y) ≤ abs(x)^y : abs_rpow_le_abs_rpow _ _ ... < δ ^ y : rpow_lt_rpow (abs_nonneg _) xδ hy ... < δ ^ q : by { refine rpow_lt_rpow_of_exponent_gt _ _ _, repeat {linarith} } ... ≤ (ε ^ (1 / q)) ^ q : by { refine rpow_le_rpow _ _ _, repeat {linarith} } ... = ε : by { rw [← rpow_mul, div_mul_cancel, rpow_one], exact ne_of_gt q_pos, linarith }}, { exact (continuous_within_at_iff_continuous_at_restrict (λp:ℝ×ℝ, p.1^p.2) _).1 (continuous_at_rpow_of_ne_zero hx₀ _).continuous_within_at } end lemma continuous_at_rpow_of_pos (hy : 0 < y) (x : ℝ) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux3 _ hy) (mem_nhds_sets (by { convert is_open_prod is_open_univ (is_open_lt' (0:ℝ)), ext, finish }) hy) lemma continuous_at_rpow {x y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := by { cases h, exact continuous_at_rpow_of_ne_zero h _, exact continuous_at_rpow_of_pos h x } variables {α : Type*} [topological_space α] {f g : α → ℝ} /-- `real.rpow` is continuous at all points except for the lower half of the y-axis. In other words, the function `λp:ℝ×ℝ, p.1^p.2` is continuous at `(x, y)` if `x ≠ 0` or `y > 0`. Multiple forms of the claim is provided in the current section. -/ lemma continuous_rpow (h : ∀a, f a ≠ 0 ∨ 0 < g a) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_iff_continuous_at.2 $ λ a, begin show continuous_at ((λp:ℝ×ℝ, p.1^p.2) ∘ (λa, (f a, g a))) a, refine continuous_at.comp _ (continuous_iff_continuous_at.1 (hf.prod_mk hg) _), { replace h := h a, cases h, { exact continuous_at_rpow_of_ne_zero h _ }, { exact continuous_at_rpow_of_pos h _ }}, end lemma continuous_rpow_of_ne_zero (h : ∀a, f a ≠ 0) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inl $ h a) hf hg lemma continuous_rpow_of_pos (h : ∀a, 0 < g a) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inr $ h a) hf hg end prove_rpow_is_continuous section sqrt lemma sqrt_eq_rpow : sqrt = λx:ℝ, x ^ (1/(2:ℝ)) := begin funext, by_cases h : 0 ≤ x, { rw [← mul_self_inj_of_nonneg, mul_self_sqrt h, ← pow_two, ← rpow_nat_cast, ← rpow_mul h], norm_num, exact sqrt_nonneg _, exact rpow_nonneg_of_nonneg h _ }, { replace h : x < 0 := lt_of_not_ge h, have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos (le_of_lt h), rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end lemma continuous_sqrt : continuous sqrt := by rw sqrt_eq_rpow; exact continuous_rpow_of_pos (λa, by norm_num) continuous_id continuous_const end sqrt end real namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : nnreal` 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 : nnreal) (y : ℝ) : nnreal := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow nnreal ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : nnreal) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : nnreal) (y : ℝ) : ((x ^ y : nnreal) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : nnreal) : x ^ (0 : ℝ) = 1 := by { rw ← nnreal.coe_eq, exact real.rpow_zero _ } @[simp] lemma rpow_eq_zero_iff {x : nnreal} {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 : nnreal) ^ x = 0 := by { rw ← nnreal.coe_eq, exact real.zero_rpow h } @[simp] lemma rpow_one (x : nnreal) : x ^ (1 : ℝ) = x := by { rw ← nnreal.coe_eq, exact real.rpow_one _ } @[simp] lemma one_rpow (x : ℝ) : (1 : nnreal) ^ x = 1 := by { rw ← nnreal.coe_eq, exact real.one_rpow _ } lemma rpow_add {x : nnreal} (y z : ℝ) (hx : 0 < x) : x ^ (y + z) = x ^ y * x ^ z := by { rw ← nnreal.coe_eq, exact real.rpow_add _ _ hx } lemma rpow_mul (x : nnreal) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by { rw ← nnreal.coe_eq, exact real.rpow_mul x.2 y z } lemma rpow_neg (x : nnreal) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by { rw ← nnreal.coe_eq, exact real.rpow_neg x.2 _ } @[simp] lemma rpow_nat_cast (x : nnreal) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by { rw [← nnreal.coe_eq, nnreal.coe_pow], exact real.rpow_nat_cast (x : ℝ) n } lemma mul_rpow {x y : nnreal} {z : ℝ} : (x*y)^z = x^z * y^z := by { rw ← nnreal.coe_eq, exact real.mul_rpow x.2 y.2 } lemma one_le_rpow {x : nnreal} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma rpow_le_rpow {x y : nnreal} {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 : nnreal} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_of_exponent_lt {x : nnreal} {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 : nnreal} {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 : nnreal} {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 : nnreal} {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_le_one {x : nnreal} {e : ℝ} (he : 0 ≤ e) (hx2 : x ≤ 1) : x^e ≤ 1 := real.rpow_le_one he x.2 hx2 lemma one_lt_rpow {x : nnreal} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma rpow_lt_one {x : nnreal} {z : ℝ} (hx : 0 < x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one hx hx1 hz lemma pow_nat_rpow_nat_inv (x : nnreal) {n : ℕ} (hn : 0 < n) : (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 : nnreal) {n : ℕ} (hn : 0 < n) : (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 continuous_at_rpow {x : nnreal} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:nnreal×ℝ, p.1^p.2) (x, y) := begin have : (λp:nnreal×ℝ, p.1^p.2) = nnreal.of_real ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:nnreal × ℝ, (p.1.1, p.2)), { ext p, rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_of_real _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine nnreal.continuous_of_real.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end end nnreal open filter lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → nnreal} {v : α → ℝ} {x : nnreal} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (tendsto.prod_mk_nhds hx hy)
de70bb24aab3ddb87a7684897d30ffd504430b3a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/clifford_algebra/star.lean
697a4b2f4464d7efe1cce9f2c220c65639b3fb0f
[ "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
2,088
lean
/- Copyright (c) 2022 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.conjugation /-! # Star structure on `clifford_algebra` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the "clifford conjugation", equal to `reverse (involute x)`, and assigns it the `star` notation. This choice is somewhat non-canonical; a star structure is also possible under `reverse` alone. However, defining it gives us access to constructions like `unitary`. Most results about `star` can be obtained by unfolding it via `clifford_algebra.star_def`. ## Main definitions * `clifford_algebra.star_ring` -/ 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 instance : star_ring (clifford_algebra Q) := { star := λ x, reverse (involute x), star_involutive := λ x, by simp only [reverse_involute_commute.eq, reverse_reverse, involute_involute], star_mul := λ x y, by simp only [map_mul, reverse.map_mul], star_add := λ x y, by simp only [map_add] } lemma star_def (x : clifford_algebra Q) : star x = reverse (involute x) := rfl lemma star_def' (x : clifford_algebra Q) : star x = involute (reverse x) := reverse_involute _ @[simp] lemma star_ι (m : M) : star (ι Q m) = -ι Q m := by rw [star_def, involute_ι, map_neg, reverse_ι] /-- Note that this not match the `star_smul` implied by `star_module`; it certainly could if we also conjugated all the scalars, but there appears to be nothing in the literature that advocates doing this. -/ @[simp] lemma star_smul (r : R) (x : clifford_algebra Q) : star (r • x) = r • star x := by rw [star_def, star_def, map_smul, map_smul] @[simp] lemma star_algebra_map (r : R) : star (algebra_map R (clifford_algebra Q) r) = algebra_map R (clifford_algebra Q) r := by rw [star_def, involute.commutes, reverse.commutes] end clifford_algebra
9c8de6fd47f7b123c09b82b9eb08b78ee116fa9b
7b4371534ac437ca8cfb325dd5c6638ff111d31a
/partial_order_induction.lean
1bfe68374c9e79916b1a1fec42a969542543de6e
[]
no_license
Shamrock-Frost/boolean_rings
6d78294568b6b9ad7b9c67b5de5e9545227826da
5da11beeaa37ec186c1deff946f2dbf7594fceb4
refs/heads/master
1,588,394,857,485
1,553,973,949,000
1,553,973,949,000
177,757,941
0
0
null
null
null
null
UTF-8
Lean
false
false
3,418
lean
import .finite inductive {u} set.acc {α : Type u} (r : α → α → Prop) (S : set α) : α → Prop | intro (x : α) (h : ∀ y ∈ S, r y x → set.acc y) : set.acc x lemma {u} set_acc_of_set_acc_minus {α : Type u} (r : α → α → Prop) (S S' : set α) (r_trans : ∀ x y z, r x y → r y z → r x z) : ∀ y ∈ S, (∀ y' ∈ S', ¬ r y' y) → (S ∖ S').acc r y → S.acc r y := begin intros y hy h hacc, induction hacc with y h ih, constructor, intros y' hy' hr, have : y' ∉ S' := λ h', h y' h' hr, apply ih, constructor; assumption, assumption, assumption, intros y'' h'' h''', apply h y'' h'', apply r_trans; assumption, end lemma partial_order.finite_well_founded {X : Type _} [partial_order X] (is_fin : finite X) : well_founded (@partial_order.lt X _) := begin constructor, suffices : ∀ x : X, x ∈ @set.univ X → set.acc partial_order.lt (@set.univ X) x, { intro x, apply @set.acc.rec_on X (<) set.univ (acc (<)), { apply this, constructor }, { clear x, intros, constructor, intros y hy, exact ih y true.intro hy } }, have : set.finite (@set.univ X), { cases is_fin with size h, existsi size, cases h with f h, cases h with f_i f_s, existsi f, constructor, assumption, funext, apply propext, transitivity true, refl, rw true_iff, exact f_s a }, cases this with size h, generalize q : set.univ = S, rw q at h, clear q, revert S h, induction size with n ih; intros; cases h with f h; cases h with f_i f_s, { rw f_s at a, exfalso, cases a, exact a_w.elim0 }, constructor, intros, have : y ∈ { y ∈ S | y ≠ x } := ⟨H, ne_of_lt a_1⟩, cases ih { y ∈ S | y ≠ x } _ y this, { apply set_acc_of_set_acc_minus (<) S {x} (@lt_trans X _) _ H; rw elem_singleton, { intros y' h', rw (_ : y' = x), apply lt_asymm, assumption, assumption }, { constructor, intros y' h' hlt, apply h; assumption } }, { rw f_s at a, cases a, existsi fin.fun_omit a_w.val f, constructor, apply omit_inj_if_fun_inj _ _ f_i, rw [im_omit _ _ _ f_i, elem_singleton, f_s], rw a_h, refl } end theorem partial_order.induction {X : Type _} [partial_order X] (P : X → Prop) (is_fin : finite X) (ih : ∀ x , (∀ y, y < x → P y) → P x) : ∀ x, P x := λ x, well_founded.induction (partial_order.finite_well_founded is_fin) x ih def minimal {X : Type _} [partial_order X] (P : X → Prop) := λ x : X, P x ∧ ∀ y, y < x → ¬ P y theorem partial_order.ex_min {X : Type _} [partial_order X] (is_fin : finite X) (P : X → Prop) : ∀ x, P x → ∃ y, minimal P y ∧ y ≤ x := begin apply partial_order.induction (λ x, P x → ∃ y, minimal P y ∧ y ≤ x) is_fin, intros x ih hp, cases classical.em (minimal P x), { existsi x, constructor, assumption, refl }, { dsimp [minimal] at h, rw @decidable.not_and_iff_or_not _ _ (classical.prop_decidable _) (classical.prop_decidable _) at h, have h := or.resolve_left h, rw ← classical.dne (P x) at h, specialize h hp, cases classical.exists_of_not_forall h with y hy, simp at hy, rw classical.implies_iff_or at hy, rw not_or_iff_and_not at hy, rw ← classical.dne (y < x) at hy, rw ← classical.dne (P y) at hy, cases ih y hy.left hy.right, cases h_1, existsi w, constructor, assumption, transitivity, assumption, apply le_of_lt, exact hy.left } end
17c158e8fa60957e52f1979e7d3aa5f0863aee7c
ed544fdbb470075305eb2a01b0491ce8a6ba05c8
/src/certigrad/tactics.lean
aea261044534bb2adc671ed0850063ae0d714271
[ "Apache-2.0" ]
permissive
gazimahmud/certigrad
d12caa30c6fc3adf9bb1fcd61479af0faad8b6c3
38cc6377dbd5025eb074188a1acd02147a92bdba
refs/heads/master
1,606,977,759,336
1,498,686,571,000
1,498,686,571,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,428
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Miscellaneous tactics. -/ namespace tactic meta def simp_lemmas.append_simp : simp_lemmas → list name → tactic simp_lemmas | sls [] := return sls | sls (l::ls) := do new_sls ← simp_lemmas.add_simp sls l, simp_lemmas.append_simp new_sls ls meta def simp_lemmas.from_names (names : list name) : tactic simp_lemmas := simp_lemmas.append_simp simp_lemmas.mk names meta definition dec_triv : tactic unit := do { tgt ← target, inst ← to_expr ``(decidable %%tgt) >>= mk_instance, to_expr ``(@of_as_true %%tgt %%inst trivial) >>= exact } <|> do { fail "dec_triv failed" } meta def at_target (tac : expr → tactic (expr × expr)) : tactic unit := do tgt ← target, (new_tgt, pf) ← tac tgt, n ← mk_fresh_name, assert n new_tgt, swap, ht ← get_local n, mk_app `eq.mpr [pf, ht] >>= exact meta def fsimpt (ns : list name) (tac : tactic unit) : tactic unit := do s ← list.mfoldl (λ slss n, simp_lemmas.add_simp slss n) simp_lemmas.mk ns, at_target (λ e, do (a, new_e, pf) ← ext_simplify_core () {} s (λ u, failed) (λ a s r p e, failed) (λ a s r p e, do ⟨u, new_e, pr⟩ ← conv.apply_lemmas_core reducible s tac r e, return ((), new_e, pr, tt)) `eq e, return (new_e, pf)) meta def at_hyp (n : name) (tac : expr → tactic (expr × expr)) : tactic unit := do h ← get_local n, h_type ← infer_type h, (new_h_type, pf) ← tac h_type, assert (expr.local_pp_name h) new_h_type, mk_eq_mp pf h >>= exact, try $ clear h meta def norm_all_nums : tactic unit := let norm_conv : conv unit := (λ (n : name) (e : expr), do guard (n = `eq), trace "\nabout to norm num: ", trace e, (new_val, pf) ← norm_num e, trace "\nsuccess: ", trace new_val, return ⟨(), new_val, some pf⟩) in at_target (conv.to_tactic (conv.bottom_up norm_conv) `eq) meta def refs_neq : tactic unit := assumption <|> (mk_const `pair_neq_of_neq₁ >>= apply >> assumption) meta def dget_dinsert : tactic unit := do s ← simp_lemmas.add_simp simp_lemmas.mk `certigrad.env.get_insert_same >>= flip simp_lemmas.add_simp `certigrad.env.get_insert_diff, at_target (λ e, do (a, new_e, pf) ← ext_simplify_core () {} s (λ u, failed) (λ a s r p e, failed) (λ a s r p e, do ⟨u, new_e, pr⟩ ← conv.apply_lemmas_core reducible s refs_neq r e, return ((), new_e, pr, tt)) `eq e, return (new_e, pf)) meta def dget_dinsert_at (n : name) : tactic unit := do s ← simp_lemmas.add_simp simp_lemmas.mk `certigrad.env.get_insert_same >>= flip simp_lemmas.add_simp `certigrad.env.get_insert_diff, at_hyp n (λ e, do (a, new_e, pf) ← ext_simplify_core () {} s (λ u, failed) (λ a s r p e, failed) (λ a s r p e, do ⟨u, new_e, pr⟩ ← conv.apply_lemmas_core reducible s refs_neq r e, return ((), new_e, pr, tt)) `eq e, return (new_e, pf)) meta def contra_nats_eq : expr → tactic unit | H := do xs ← injection H | failed, match xs with | [] := skip | [x] := contra_nats_eq x | (x::y::xs) := fail "injection returned multiple hyps" end meta def prove_nats_neq : tactic unit := intro `H >>= contra_nats_eq end tactic
04f151f2c6694c8a1d988459d42796b4980e9649
54f4ad05b219d444b709f56c2f619dd87d14ec29
/my_project/src/love02_backward_proofs_exercise_sheet.lean
4193ec637f09be0511eead47c50d40f0890fbcfe
[]
no_license
yizhou7/learning-lean
8efcf838c7276e235a81bd291f467fa43ce56e0a
91fb366c624df6e56e19555b2e482ce767cd8224
refs/heads/master
1,675,649,087,737
1,609,022,281,000
1,609,022,281,000
272,072,779
0
0
null
null
null
null
UTF-8
Lean
false
false
4,514
lean
import .love02_backward_proofs_demo /-! # LoVe Exercise 2: Backward Proofs -/ set_option pp.beta true namespace LoVe /-! ## Question 1: Connectives and Quantifiers 1.1. Carry out the following proofs using basic tactics. Hint: Some strategies for carrying out such proofs are described at the end of Section 2.3 in the Hitchhiker's Guide. -/ lemma I (a : Prop) : a → a := begin intro, exact a_1, end lemma K (a b : Prop) : a → b → b := begin intros, exact a_2, end lemma C (a b c : Prop) : (a → b → c) → b → a → c := begin intros, apply a_1 a_3 a_2, end lemma proj_1st (a : Prop) : a → a → a := begin intros, apply a_1, end /-! Please give a different answer than for `proj_1st`: -/ lemma proj_2nd (a : Prop) : a → a → a := begin intros, apply a_2, end lemma some_nonsense (a b c : Prop) : (a → b → c) → a → (a → c) → b → c := begin intros, apply a_1 a_2 a_4, end /-! 1.2. Prove the contraposition rule using basic tactics. -/ lemma contrapositive (a b : Prop) : (a → b) → ¬ b → ¬ a := begin intros, apply not.intro, intro, apply a_2, apply a_1 a_3, end /-! 1.3. Prove the distributivity of `∀` over `∧` using basic tactics. Hint: This exercise is tricky, especially the right-to-left direction. Some forward reasoning, like in the proof of `and_swap₂` in the lecture, might be necessary. -/ lemma forall_and {α : Type} (p q : α → Prop) : (∀x, p x ∧ q x) ↔ (∀x, p x) ∧ (∀x, q x) := begin apply iff.intro, { intro hpq, apply and.intro, { intros x, apply and.elim_left, apply hpq, }, { intros x, apply and.elim_right, apply hpq, }, }, { intro hconj, intros x, apply and.intro, { apply and.elim_left hconj }, { apply and.elim_right hconj }, } end /-! ## Question 2: Natural Numbers 2.1. Prove the following recursive equations on the first argument of the `mul` operator defined in lecture 1. -/ #check mul lemma mul_zero (n : ℕ) : mul 0 n = 0 := begin induction n, { refl }, { simp [n_ih, mul, add] } end lemma mul_succ (m n : ℕ) : mul (nat.succ m) n = add (mul m n) n := begin induction n, { simp [add, add_zero, mul] }, { simp [add, add_succ, n_ih, mul, mul_zero, add_assoc] }, end /-! 2.2. Prove commutativity and associativity of multiplication using the `induction` tactic. Choose the induction variable carefully. -/ lemma mul_comm (m n : ℕ) : mul m n = mul n m := begin induction m, { simp[mul_zero, mul] }, { simp [mul_succ, m_ih, add, add_succ, mul, mul_zero, add_assoc, mul_add], cc }, end /-! 2.3. Prove the symmetric variant of `mul_add` using `rewrite`. To apply commutativity at a specific position, instantiate the rule by passing some arguments (e.g., `mul_comm _ l`). -/ lemma add_mul (l m n : ℕ) : mul (add l m) n = add (mul n l) (mul n m) := begin induction l, { simp [add_zero, mul, mul_comm], }, { rw mul_comm (add l_n.succ m) n, apply mul_add, }, end lemma mul_assoc (l m n : ℕ) : mul (mul l m) n = mul l (mul m n) := begin induction l, { simp[mul_zero, mul] }, { simp [mul_succ], rw <- l_ih, rw mul_comm (mul l_n m) n, rw mul_comm m n, rw add_mul (mul l_n m), }, end /-! ## Question 3 (**optional**): Intuitionistic Logic Intuitionistic logic is extended to classical logic by assuming a classical axiom. There are several possibilities for the choice of axiom. In this question, we are concerned with the logical equivalence of three different axioms: -/ def excluded_middle := ∀a : Prop, a ∨ ¬ a def peirce := ∀a b : Prop, ((a → b) → a) → a def double_negation := ∀a : Prop, (¬¬ a) → a /-! For the proofs below, please avoid using lemmas from Lean's `classical` namespace, because this would defeat the purpose of the exercise. 3.1 (**optional**). Prove the following implication using tactics. Hint: You will need `or.elim` and `false.elim`. You can use `simp [excluded_middle, peirce]` to unfold the definitions of `excluded_middle` and `peirce`. -/ lemma peirce_of_em : excluded_middle → peirce := sorry /-! 3.2 (**optional**). Prove the following implication using tactics. -/ lemma dn_of_peirce : peirce → double_negation := sorry /-! We leave the missing implication for the homework: -/ namespace sorry_lemmas lemma em_of_dn : double_negation → excluded_middle := sorry end sorry_lemmas end LoVe
79bc1f8d36d45355dd2a483e7276858deae4ed0f
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/standard/logic/classes/inhabited.lean
72794584595c9821805c8d6864ac78fc9dbeee16
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
962
lean
---------------------------------------------------------------------------------------------------- -- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Leonardo de Moura, Jeremy Avigad ---------------------------------------------------------------------------------------------------- import logic.connectives.quantifiers inductive inhabited (A : Type) : Prop := | inhabited_intro : A → inhabited A theorem inhabited_elim {A : Type} {B : Prop} (H1 : inhabited A) (H2 : A → B) : B := inhabited_rec H2 H1 theorem inhabited_Prop [instance] : inhabited Prop := inhabited_intro true theorem inhabited_fun [instance] (A : Type) {B : Type} (H : inhabited B) : inhabited (A → B) := inhabited_elim H (take b, inhabited_intro (λa, b)) theorem inhabited_exists {A : Type} {p : A → Prop} (H : ∃x, p x) : inhabited A := obtain w Hw, from H, inhabited_intro w
a417280684bf73a309a84e6173ce418d3167a451
b147e1312077cdcfea8e6756207b3fa538982e12
/data/fintype.lean
8d0c8964324c0490f4044342d88b8b24fa19813d
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,113
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Finite types. -/ import data.finset algebra.big_operators data.array.lemmas data.vector2 universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext] end finset open finset namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [fintype α] [∀a, decidable_eq (β a)] : decidable_eq (Πa, β a) := assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) instance decidable_forall_fintype [fintype α] {p : α → Prop} [decidable_pred p] : decidable (∀ a, p a) := decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp) instance decidable_exists_fintype [fintype α] {p : α → Prop} [decidable_pred p] : decidable (∃ a, p a) := decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp) /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ← e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- There is (computably) a bijection between `α` and `fin n` where `n = card α`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. -/ def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩) mem_univ_val univ.2 theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) := by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩ instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext, h₁, h₂]⟩ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by rw ← subtype_card s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ e, match F, G, e with ⟨⟨s, nd⟩, h⟩, ⟨⟨s', nd'⟩, h'⟩, e' := begin change multiset.card s = multiset.card s' at e', revert nd nd' h h' e', refine quotient.induction_on₂ s s' (λ l₁ l₂ (nd₁ : l₁.nodup) (nd₂ : l₂.nodup) (h₁ : ∀ x, x ∈ l₁) (h₂ : ∀ x, x ∈ l₂) (e' : l₁.length = l₂.length), _), haveI := classical.dec_eq α, refine ⟨equiv.of_bijective ⟨_, _⟩⟩, { refine λ a, l₂.nth_le (l₁.index_of a) _, rw ← e', exact list.index_of_lt_length.2 (h₁ a) }, { intros a b h, simpa [h₁] using congr_arg l₁.nth (list.nodup_iff_nth_le_inj.1 nd₂ _ _ _ _ h) }, { have := classical.dec_eq β, refine λ b, ⟨l₁.nth_le (l₂.index_of b) _, _⟩, { rw e', exact list.index_of_lt_length.2 (h₂ b) }, { simp [nd₁] } } end end, λ ⟨f⟩, card_congr f⟩ end fintype instance (n : ℕ) : fintype (fin n) := ⟨⟨list.pmap fin.mk (list.range n) (λ a, list.mem_range.1), list.nodup_pmap (λ a _ b _, congr_arg fin.val) (list.nodup_range _)⟩, λ ⟨m, h⟩, list.mem_pmap.2 ⟨m, list.mem_range.2 h, rfl⟩⟩ @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := by rw [fin.fintype]; simp [fintype.card, card, univ] instance : fintype empty := ⟨∅, empty.rec _⟩ @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl instance : fintype unit := ⟨⟨()::0, by simp⟩, λ ⟨⟩, by simp⟩ @[simp] theorem fintype.univ_unit : @univ unit _ = {()} := rfl @[simp] theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype bool := ⟨⟨tt::ff::0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {ff, tt} := rfl @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl def finset.insert_none (s : finset α) : finset (option α) := ⟨none :: s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_sigma {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype.card (sigma β) = univ.sum (λ a, fintype.card (β a)) := card_sigma _ _ instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ def fintype.fintype_prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) @[simp] theorem fintype.card_sum (α β : Type*) [fintype α] [fintype β] : fintype.card (α ⊕ β) = fintype.card α + fintype.card β := by rw [sum.fintype, fintype.of_equiv_card]; simp lemma fintype.card_le_of_injective [fintype α] [fintype β] (f : α → β) (hf : function.injective f) : fintype.card α ≤ fintype.card β := by haveI := classical.prop_decidable; exact finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) lemma fintype.card_eq_one_iff [fintype α] : fintype.card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [← fintype.card_unit, fintype.card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm unit.star, λ y, a.bijective.1 (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, unit.star, λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma fintype.card_eq_zero_iff [fintype α] : fintype.card α = 0 ↔ (α → false) := ⟨λ h a, have e : α ≃ empty := classical.choice (fintype.card_eq.1 (by simp [h])), (e a).elim, λ h, have e : α ≃ empty := ⟨λ a, (h a).elim, λ a, a.elim, λ a, (h a).elim, λ a, a.elim⟩, by simp [fintype.card_congr e]⟩ lemma fintype.card_pos_iff [fintype α] : 0 < fintype.card α ↔ nonempty α := ⟨λ h, classical.by_contradiction (λ h₁, have fintype.card α = 0 := fintype.card_eq_zero_iff.2 (λ a, h₁ ⟨a⟩), lt_irrefl 0 $ by rwa this at h), λ ⟨a⟩, nat.pos_of_ne_zero (mt fintype.card_eq_zero_iff.1 (λ h, h a))⟩ lemma fintype.card_le_one_iff [fintype α] : fintype.card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := fintype.card α in have hn : n = fintype.card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (fintype.card_eq_zero_iff.1 ha.symm a).elim, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := fintype.card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, fintype.card_unit ▸ fintype.card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then finset.singleton ⟨h⟩ else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true::false::0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s := fintype.subtype (univ.filter (∈ s)) (by simp) instance pi.fintype {α : Type*} {β : α → Type*} [fintype α] [decidable_eq α] [∀a, fintype (β a)] : fintype (Πa, β a) := @fintype.of_equiv _ _ ⟨univ.pi $ λa:α, @univ (β a) _, λ f, finset.mem_pi.2 $ λ a ha, mem_univ _⟩ ⟨λ f a, f a (mem_univ _), λ f a _, f a, λ f, rfl, λ f, rfl⟩ @[simp] lemma fintype.card_pi {β : α → Type*} [fintype α] [decidable_eq α] [f : Π a, fintype (β a)] : fintype.card (Π a, β a) = univ.prod (λ a, fintype.card (β a)) := by letI f' : fintype (Πa∈univ, β a) := ⟨(univ.pi $ λa, univ), assume f, finset.mem_pi.2 $ assume a ha, mem_univ _⟩; exact calc fintype.card (Π a, β a) = fintype.card (Π a ∈ univ, β a) : fintype.card_congr ⟨λ f a ha, f a, λ f a, f a (mem_univ a), λ _, rfl, λ _, rfl⟩ ... = univ.prod (λ a, fintype.card (β a)) : finset.card_pi _ _ @[simp] lemma fintype.card_fun [fintype α] [decidable_eq α] [fintype β] : fintype.card (α → β) = fintype.card β ^ fintype.card α := by rw [fintype.card_pi, finset.prod_const, nat.pow_eq_pow]; refl instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ instance subtype.fintype [fintype α] (p : α → Prop) [decidable_pred p] : fintype {x // p x} := set_fintype _ instance set.fintype [fintype α] [decidable_eq α] : fintype (set α) := pi.fintype def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι), (∀ i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i::l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : ∀ i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i::l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end
d2dad288099185d90dce30b0bd57a4eb81e74f04
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/data/set/lattice.lean
b3400f0063065ccf54cee0cc12b514c8a6b1fc0e
[ "Apache-2.0" ]
permissive
linpingchuan/mathlib
d49990b236574df2a45d9919ba43c923f693d341
5ad8020f67eb13896a41cc7691d072c9331b1f76
refs/heads/master
1,626,019,377,808
1,508,048,784,000
1,508,048,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,672
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors Jeremy Avigad, Leonardo de Moura, Johannes Hölzl -- QUESTION: can make the first argument in ∀ x ∈ a, ... implicit? -/ import logic.basic data.set.basic import order.complete_boolean_algebra category.basic import tactic.finish open function tactic set lattice auto universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} namespace set instance lattice_set : complete_lattice (set α) := { lattice.complete_lattice . le := (⊆), le_refl := subset.refl, le_trans := assume a b c, subset.trans, le_antisymm := assume a b, subset.antisymm, lt := λ x y, x ⊆ y ∧ ¬ y ⊆ x, lt_iff_le_not_le := λ x y, iff.refl _, sup := (∪), le_sup_left := subset_union_left, le_sup_right := subset_union_right, sup_le := assume a b c, union_subset, inf := (∩), inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, le_inf := assume a b c, subset_inter, top := {a | true }, le_top := assume s a h, trivial, bot := ∅, bot_le := assume s a, false.elim, Sup := λs, {a | ∃ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, Inf := λs, {a | ∀ t ∈ s, a ∈ t }, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in } instance : distrib_lattice (set α) := { set.lattice_set with le_sup_inf := assume s t u x ⟨h₁, h₂⟩, match h₁ with | or.inl h₁ := or.inl h₁ | or.inr h₁ := match h₂ with | or.inl h₂ := or.inl h₂ | or.inr h₂ := or.inr ⟨h₁, h₂⟩ end end } lemma subset.antisymm_iff {α : Type*} {s t : set α} : s = t ↔ (s ⊆ t ∧ t ⊆ s) := le_antisymm_iff lemma monotone_image {f : α → β} : monotone (image f) := assume s t, assume h : s ⊆ t, image_subset _ h /- union and intersection over a family of sets indexed by a type -/ @[reducible] def Union (s : ι → set β) : set β := supr s @[reducible] def Inter (s : ι → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union_eq (x : β) (s : ι → set β) : (x ∈ ⋃ i, s i) = (∃ i, x ∈ s i) := propext ⟨assume ⟨t, ⟨⟨a, (t_eq : t = s a)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq ▸ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate ∃i, ... ∧ i = t ∧ ... @[simp] theorem mem_Inter_eq (x : β) (s : ι → set β) : (x ∈ ⋂ i, s i) = (∀ i, x ∈ s i) := propext ⟨assume (h : ∀a ∈ {a : set β | ∃i, a = s i}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : t = s a)⟩, eq.symm ▸ h a⟩ theorem Union_subset {s : ι → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ set.lattice_set _ _ h theorem Union_subset_iff {α : Sort u} {s : α → set β} {t : set β} : (⋃ i, s i) ⊆ t ↔ (∀ i, s i ⊆ t):= ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter {α : Sort u} {x : β} {s : α → set β} : (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := assume h t ⟨a, (eq : t = s a)⟩, eq.symm ▸ h a theorem subset_Inter {t : set β} {s : α → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set β) _ set.lattice_set _ _ h @[simp] -- complete_boolean_algebra theorem compl_Union (s : α → set β) : - (⋃ i, s i) = (⋂ i, - s i) := ext (λ x, by simp) -- classical -- complete_boolean_algebra theorem compl_Inter (s : α → set β) : -(⋂ i, s i) = (⋃ i, - s i) := ext (λ x, by simp [classical.not_forall]) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : α → set β) : (⋃ i, s i) = - (⋂ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : α → set β) : (⋂ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_distrib_Union_left (s : set β) (t : ι → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := set.ext (by simp) theorem inter_distrib_Union_right (s : set β) (t : ι → set β) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := set.ext (by simp) -- classical theorem union_distrib_Inter_left (s : set β) (t : α → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := set.ext $ assume x, by simp [classical.forall_or_distrib_left] /- bounded unions and intersections -/ theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := show (⨆ x ∈ s, u x) ≤ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := show t ≤ (⨅ x ∈ s, u x), -- TODO: should not be necessary when sets' order is based on lattices from le_infi $ assume x, le_infi (h x) theorem subset_Union {ι : Sort*} (u : ι → set α) (i : ι) : u i ⊆ (⋃i, u i) := show u i ≤ (⨆i, u i), from le_supr u i theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs @[simp] theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := show (⨅x ∈ (∅ : set α), u x) = ⊤, -- simplifier should be able to rewrite x ∈ ∅ to false. from infi_emptyset @[simp] theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := show (⨅ x ∈ ({a} : set α), s x) = s a, by simp theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := show (⨅ x ∈ s ∪ t, u x) = (⨅ x ∈ s, u x) ⊓ (⨅ x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by rw insert_of_has_insert; simp @[simp] theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset @[simp] theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by rw insert_of_has_insert; simp @[reducible] def sInter (S : set (set α)) : set α := Inf S prefix `⋂₀`:110 := sInter theorem mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ @[simp] theorem mem_sUnion_eq {x : α} {S : set (set α)} : x ∈ ⋃₀ S = (∃t ∈ S, x ∈ t) := rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := assume : x ∈ t, have x ∈ ⋃₀ S, from mem_sUnion this ht, show false, from hx this theorem mem_sInter {x : α} {t : set α} {S : set (set α)} (h : ∀ t ∈ S, x ∈ t) : x ∈ ⋂₀ S := h @[simp] theorem mem_sInter_eq {x : α} {S : set (set α)} : x ∈ ⋂₀ S = (∀ t ∈ S, x ∈ t) := rfl theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : (⋂₀ S) ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ (⋃₀ S) := le_Sup tS theorem sUnion_subset {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t' ⊆ t) : (⋃₀ S) ⊆ t := Sup_le h theorem sUnion_subset_iff {s : set (set α)} {t : set α} : (⋃₀ s) ⊆ t ↔ ∀t' ∈ s, t' ⊆ t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f '' s) = ⋂ x ∈ s, f x := Inf_image theorem compl_sUnion (S : set (set α)) : - ⋃₀ S = ⋂₀ (compl '' S) := set.ext $ assume x, ⟨assume : ¬ (∃s∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : ∀s, s ∈ compl '' S → x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = - ⋂₀ (compl '' S) := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : - ⋂₀ S = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set α)) : ⋂₀ S = -(⋃₀ (compl '' S)) := by rw [←compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty begin rw ←h, apply inter_subset_inter_left, apply subset_sUnion_of_mem hs end theorem Union_eq_sUnion_image (s : α → set β) : (⋃ i, s i) = ⋃₀ (s '' univ) := by simp theorem Inter_eq_sInter_image {α I : Type} (s : I → set α) : (⋂ i, s i) = ⋂₀ (s '' univ) := by simp lemma sUnion_mono {s t : set (set α)} (h : s ⊆ t) : (⋃₀ s) ⊆ (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ι → set α} (h : ∀i, s i ⊆ t i) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr (set α) ι _ s t h lemma Union_subset_Union2 {ι₂ : Sort*} {s : ι → set α} {t : ι₂ → set α} (h : ∀i, ∃j, s i ⊆ t j) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr2 (set α) ι ι₂ _ s t h lemma Union_subset_Union_const {ι₂ : Sort x} {s : set α} (h : ι → ι₂) : (⋃ i:ι, s) ⊆ (⋃ j:ι₂, s) := @supr_le_supr_const (set α) ι ι₂ _ s h lemma sUnion_eq_Union {s : set (set α)} : (⋃₀ s) = (⋃ (i : set α) (h : i ∈ s), i) := set.ext $ by simp instance : complete_boolean_algebra (set α) := { set.lattice_set with neg := compl, sub := (\), inf_neg_eq_bot := assume s, ext $ assume x, ⟨assume ⟨h, nh⟩, nh h, false.elim⟩, sup_neg_eq_top := assume s, ext $ assume x, ⟨assume h, trivial, assume _, classical.em $ x ∈ s⟩, le_sup_inf := distrib_lattice.le_sup_inf, sub_eq := assume x y, rfl, infi_sup_le_sup_Inf := assume s t x, show x ∈ (⋂ b ∈ t, s ∪ b) → x ∈ s ∪ (⋂₀ t), by simp; exact assume h, or.imp_right (assume hn : x ∉ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) → x ∈ (⋃ b ∈ t, s ∩ b), by simp [-and_imp] } theorem union_sdiff_same {a b : set α} : a ∪ (b \ a) = a ∪ b := lattice.sup_sub_same theorem sdiff_union_same {a b : set α} : (b \ a) ∪ a = a ∪ b := by rw [union_comm, union_sdiff_same] theorem sdiff_inter_same {a b : set α} : (b \ a) ∩ a = ∅ := set.ext $ by simp [iff_def] {contextual:=tt} theorem sdiff_subset_sdiff {a b c d : set α} : a ⊆ c → d ⊆ b → a \ b ⊆ c \ d := @lattice.sub_le_sub (set α) _ _ _ _ _ @[simp] theorem union_same_compl {a : set α} : a ∪ (-a) = univ := sup_neg_eq_top @[simp] theorem sdiff_singleton_eq_same {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := sub_eq_left $ eq_empty_of_forall_not_mem $ assume x ⟨ht, ha⟩, begin simp at ha, simp [ha] at ht, exact h ht end @[simp] theorem insert_sdiff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_sdiff_same] lemma compl_subset_compl_iff_subset {α : Type u} {x y : set α} : - y ⊆ - x ↔ x ⊆ y := @neg_le_neg_iff_le (set α) _ _ _ lemma union_of_subset_right {a b : set α} (h : a ⊆ b) : a ∪ b = b := sup_of_le_right h section sdiff variables {s s₁ s₂ : set α} @[simp] lemma sdiff_empty : s \ ∅ = s := set.ext $ by simp lemma sdiff_eq: s₁ \ s₂ = s₁ ∩ -s₂ := rfl lemma union_sdiff_left : (s₁ ∪ s₂) \ s₂ = s₁ \ s₂ := set.ext $ assume x, by simp [iff_def] {contextual := tt} lemma union_sdiff_right : (s₂ ∪ s₁) \ s₂ = s₁ \ s₂ := set.ext $ assume x, by simp [iff_def] {contextual := tt} end sdiff section variables {p : Prop} {μ : p → set α} @[simp] lemma Inter_pos (hp : p) : (⋂h:p, μ h) = μ hp := infi_pos hp @[simp] lemma Inter_neg (hp : ¬ p) : (⋂h:p, μ h) = univ := infi_neg hp @[simp] lemma Union_pos (hp : p) : (⋃h:p, μ h) = μ hp := supr_pos hp @[simp] lemma Union_neg (hp : ¬ p) : (⋃h:p, μ h) = ∅ := supr_neg hp @[simp] lemma Union_empty {ι : Sort*} : (⋃i:ι, ∅:set α) = ∅ := supr_bot @[simp] lemma Inter_univ {ι : Sort*} : (⋂i:ι, univ:set α) = univ := infi_top end end set section image lemma image_Union {f : α → β} {s : ι → set α} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image], exact ⟨assume ⟨a, h, i, hi⟩, ⟨i, a, h, hi⟩, assume ⟨i, a, h, hi⟩, ⟨a, h, i, hi⟩⟩ end lemma univ_subtype {p : α → Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, begin simp, exact ⟨x, h, rfl⟩ end lemma subtype_val_image {p : α → Prop} {s : set (subtype p)} : subtype.val '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ end image section preimage theorem monotone_preimage {f : α → β} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ι : Sort w} {f : α → β} {s : ι → set β} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : preimage f (⋃₀ s) = (⋃t ∈ s, preimage f t) := set.ext $ by simp [preimage] end preimage instance : monad set := { monad . pure := λ(α : Type u) a, {a}, bind := λ(α β : Type u) s f, ⋃i∈s, f i, map := λ(α β : Type u), set.image, pure_bind := assume α β x f, by simp, bind_assoc := assume α β γ s f g, set.ext $ assume a, by simp; exact ⟨assume ⟨b, ag, a, as, bf⟩, ⟨a, as, b, bf, ag⟩, assume ⟨a, as, b, bf, ag⟩, ⟨b, ag, a, as, bf⟩⟩, id_map := assume α, functor.id_map, bind_pure_comp_eq_map := assume α β f s, set.ext $ by simp [set.image, eq_comm] } section monad variables {α' β' : Type u} {s : set α'} {f : α' → set β'} {g : set (α' → β')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl lemma fmap_eq_image : f <$> s = f '' s := rfl lemma mem_seq_iff {b : β'} : b ∈ (g <*> s) ↔ (∃(f' : α' → β'), ∃a∈s, f' ∈ g ∧ b = f' a) := begin simp [seq_eq_bind_map], apply exists_congr, intro f', exact ⟨assume ⟨hf', a, ha, h_eq⟩, ⟨a, h_eq.symm, ha, hf'⟩, assume ⟨a, h_eq, ha, hf'⟩, ⟨hf', a, ha, h_eq.symm⟩⟩ end end monad /- disjoint sets -/ section disjoint variable [semilattice_inf_bot α] def disjoint (a b : α) : Prop := a ⊓ b = ⊥ theorem disjoint_symm {a b : α} : disjoint a b → disjoint b a := assume : a ⊓ b = ⊥, show b ⊓ a = ⊥, from this ▸ inf_comm theorem disjoint_bot_left {a : α} : disjoint ⊥ a := bot_inf_eq theorem disjoint_bot_right {a : α} : disjoint a ⊥ := inf_bot_eq end disjoint
ad4994d219c7eb67f5d93304d40c4177a5c5e3ef
26ac254ecb57ffcb886ff709cf018390161a9225
/src/topology/algebra/open_subgroup.lean
b7aefb78627dcfe3a64aaee49c9a29f7ae04333e
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
7,280
lean
/- Copyright (c) 2019 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.filter.lift import topology.opens import topology.algebra.ring open topological_space open_locale topological_space set_option old_structure_cmd true /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive open_add_subgroup] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup -- Tell Lean that `open_add_subgroup` is a namespace namespace open_add_subgroup end open_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, to_additive mem_coe_add_subgroup] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl attribute [norm_cast] mem_coe mem_coe_opens mem_coe_subgroup open_add_subgroup.mem_coe open_add_subgroup.mem_coe_opens open_add_subgroup.mem_coe_add_subgroup @[to_additive] lemma coe_injective : injective (coe : open_subgroup G → set G) := λ U V h, by cases U; cases V; congr; assumption @[ext, to_additive] lemma ext (h : ∀ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (∀ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h ▸ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ g₂ : G} (h₁ : g₁ ∈ U) (h₂ : g₂ ∈ U) : g₁ * g₂ ∈ U := U.mul_mem' h₁ h₂ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := mem_nhds_sets U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊤ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊤⟩ @[to_additive] lemma is_closed [topological_monoid G] (U : open_subgroup G) : is_closed (U : set G) := begin refine is_open_iff_forall_mem_open.2 (λ x hx, ⟨(λ y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊢, refine mt (λ hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact (continuous_mul_right _) _ U.is_open }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] @[to_additive] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G × H) := { carrier := (U : set G).prod (V : set H), is_open' := is_open_prod U.is_open V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := λ U V, ∀ ⦃x⦄, x ∈ U → x ∈ V, .. partial_order.lift (coe : open_subgroup G → set G) coe_injective } @[to_additive] instance : semilattice_inf_top (open_subgroup G) := { inf := λ U V, { is_open' := is_open_inter U.is_open V.is_open, .. (U : subgroup G) ⊓ V }, inf_le_left := λ U V, set.inter_subset_left _ _, inf_le_right := λ U V, set.inter_subset_right _ _, le_inf := λ U V W hV hW, set.subset_inter hV hW, top := ⊤, le_top := λ U, set.subset_univ _, ..open_subgroup.partial_order } @[simp, to_additive] lemma coe_inf : (↑(U ⊓ V) : set G) = (U : set G) ∩ V := rfl @[simp, to_additive] lemma coe_subset : (U : set G) ⊆ V ↔ U ≤ V := iff.rfl @[simp, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≤ (V : subgroup G) ↔ U ≤ V := iff.rfl attribute [norm_cast] coe_inf coe_subset coe_subgroup_le open_add_subgroup.coe_inf open_add_subgroup.coe_subset open_add_subgroup.coe_subgroup_le end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [topological_monoid G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, subgroup.mem_coe] at hg ⊢, intros x hx, have : filter.tendsto (λ y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map.1 (this hg), replace hg : g ∈ H := subgroup.mem_coe.1 (mem_of_nhds hg), simp only [subgroup.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive is_open_of_open_add_subgroup] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≤ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_sets_of_superset U.mem_nhds_one h) @[to_additive] lemma is_open_mono {H₁ H₂ : subgroup G} (h : H₁ ≤ H₂) (h₁ : is_open (H₁ :set G)) : is_open (H₂ : set G) := @is_open_of_open_subgroup _ _ _ _ H₂ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [topological_monoid G] @[to_additive] instance : semilattice_sup_top (open_subgroup G) := { sup := λ U V, { is_open' := show is_open (((U : subgroup G) ⊔ V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) ⊔ V) }, le_sup_left := λ U V, coe_subgroup_le.1 le_sup_left, le_sup_right := λ U V, coe_subgroup_le.1 le_sup_right, sup_le := λ U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf_top } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≤ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≤ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
dde06c7e43b09b4b4d9af4c08d820341d31bfb18
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/invertible.lean
7cbbb06690fd083a445e55ad1d910f95a786b2e3
[ "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,167
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.group.units import algebra.ring.basic /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a two-sided multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. For constructions of the invertible element given a characteristic, see `algebra/char_p/invertible` and other lemmas in that file. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma inv_of_mul_self_assoc [monoid α] (a b : α) [invertible a] : ⅟a * (a * b) = b := by rw [←mul_assoc, inv_of_mul_self, one_mul] @[simp] lemma mul_inv_of_self_assoc [monoid α] (a b : α) [invertible a] : a * (⅟a * b) = b := by rw [←mul_assoc, mul_inv_of_self, one_mul] @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- An `invertible` element is a unit. -/ def unit_of_invertible [monoid α] (a : α) [invertible a] : units α := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } @[simp] lemma unit_of_invertible_val [monoid α] (a : α) [invertible a] : (unit_of_invertible a : α) = a := rfl @[simp] lemma unit_of_invertible_inv [monoid α] (a : α) [invertible a] : (↑(unit_of_invertible a)⁻¹ : α) = ⅟a := rfl lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨ 1, mul_one _, one_mul _ ⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) @[simp] lemma one_sub_inv_of_two [ring α] [invertible (2:α)] : 1 - (⅟2:α) = ⅟2 := (is_unit_of_invertible (2:α)).mul_right_inj.1 $ by rw [mul_sub, mul_inv_of_self, mul_one, bit0, add_sub_cancel] /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) /-- If `r` is invertible and `s = r`, then `s` is invertible. -/ def invertible.copy [monoid α] {r : α} (hr : invertible r) (s : α) (hs : s = r) : invertible s := { inv_of := ⅟r, inv_of_mul_self := by rw [hs, inv_of_mul_self], mul_inv_of_self := by rw [hs, mul_inv_of_self] } theorem commute.inv_of_right [monoid α] {a b : α} [invertible b] (h : commute a b) : commute a (⅟b) := calc a * (⅟b) = (⅟b) * (b * a * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (a * b * ((⅟b))) : by rw h.eq ... = (⅟b) * a : by simp [mul_assoc] theorem commute.inv_of_left [monoid α] {a b : α} [invertible b] (h : commute b a) : commute (⅟b) a := calc (⅟b) * a = (⅟b) * (a * b * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (b * a * (⅟b)) : by rw h.eq ... = a * (⅟b) : by simp [mul_assoc] lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm section group_with_zero variable [group_with_zero α] lemma nonzero_of_invertible (a : α) [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] }
d413a45404ce7cf6e1c6df502bf88970fc5b2648
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/bad_id.lean
9a7239a0b55fdcc6b874bec48565650dc7e2e5d3
[ "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
58
lean
-- definition x.y : nat := 10 definition x.1 :nat := 10
6a55c0b3664cd1ba68704e2496bec87ed001bde3
54deab7025df5d2df4573383df7e1e5497b7a2c2
/data/list/perm.lean
5438fc3173b79cfd263dc8e1a750fc61690b1661
[ "Apache-2.0" ]
permissive
HGldJ1966/mathlib
f8daac93a5b4ae805cfb0ecebac21a9ce9469009
c5c5b504b918a6c5e91e372ee29ed754b0513e85
refs/heads/master
1,611,340,395,683
1,503,040,489,000
1,503,040,489,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
37,772
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad List permutations. -/ import data.list.basic data.list.comb data.list.set namespace list universe variables uu vv variables {α : Type uu} {β : Type vv} inductive perm : list α → list α → Prop | nil : perm [] [] | skip : Π (x : α) {l₁ l₂ : list α}, perm l₁ l₂ → perm (x::l₁) (x::l₂) | swap : Π (x y : α) (l : list α), perm (y::x::l) (x::y::l) | trans : Π {l₁ l₂ l₃ : list α}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ namespace perm infix ~ := perm @[refl] protected theorem refl : ∀ (l : list α), l ~ l | [] := nil | (x::xs) := skip x (refl xs) @[symm] protected theorem symm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₂ ~ l₁ := perm.rec_on p nil (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap y x l) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₂ r₁) attribute [trans] perm.trans theorem eqv (α : Type) : equivalence (@perm α) := mk_equivalence (@perm α) (@perm.refl α) (@perm.symm α) (@perm.trans α) attribute [instance] protected def is_setoid (α : Type) : setoid (list α) := setoid.mk (@perm α) (perm.eqv α) theorem mem_of_perm {a : α} {l₁ l₂ : list α} (p : l₁ ~ l₂) : a ∈ l₁ → a ∈ l₂ := perm.rec_on p (λ h, h) (λ x l₁ l₂ p₁ r₁ i, or.elim i (λ ax, by simp [ax]) (λ al₁, or.inr (r₁ al₁))) (λ x y l ayxl, or.elim ayxl (λ ay, by simp [ay]) (λ axl, or.elim axl (λ ax, by simp [ax]) (λ al, or.inr (or.inr al)))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ ainl₁, r₂ (r₁ ainl₁)) theorem not_mem_of_perm {a : α} {l₁ l₂ : list α} : l₁ ~ l₂ → a ∉ l₁ → a ∉ l₂ := assume p nainl₁ ainl₂, nainl₁ (mem_of_perm p.symm ainl₂) theorem mem_iff_mem_of_perm {a : α} {l₁ l₂ : list α} (h : l₁ ~ l₂) : a ∈ l₁ ↔ a ∈ l₂ := iff.intro (mem_of_perm h) (mem_of_perm h.symm) theorem perm_app_left {l₁ l₂ : list α} (t₁ : list α) (p : l₁ ~ l₂) : (l₁++t₁) ~ (l₂++t₁) := perm.rec_on p (perm.refl ([] ++ t₁)) (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap x y _) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_app_right {t₁ t₂ : list α} : ∀ (l : list α), t₁ ~ t₂ → (l++t₁) ~ (l++t₂) | [] p := p | (x::xs) p := skip x (perm_app_right xs p) theorem perm_app {l₁ l₂ t₁ t₂ : list α} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁++t₁) ~ (l₂++t₂) := assume p₁ p₂, trans (perm_app_left t₁ p₁) (perm_app_right l₂ p₂) theorem perm_app_cons (a : α) {h₁ h₂ t₁ t₂ : list α} : h₁ ~ h₂ → t₁ ~ t₂ → (h₁ ++ (a::t₁)) ~ (h₂ ++ (a::t₂)) := assume p₁ p₂, perm_app p₁ (skip a p₂) theorem perm_cons_app (a : α) : ∀ (l : list α), (a::l) ~ (l ++ [a]) | [] := perm.refl _ | (x::xs) := trans (swap x a xs) $ skip x (perm_cons_app xs) @[simp] theorem perm_cons_app_simp (a : α) (l : list α) : (l ++ [a]) ~ (a::l) := perm.symm (perm_cons_app a l) @[simp] theorem perm_app_comm : ∀ {l₁ l₂ : list α}, (l₁++l₂) ~ (l₂++l₁) | [] l₂ := by simp | (a::t) l₂ := calc a::(t++l₂) ~ a::(l₂++t) : skip a perm_app_comm ... ~ l₂++t++[a] : perm_cons_app _ _ ... = l₂++(t++[a]) : by rw append_assoc ... ~ l₂++(a::t) : perm_app_right l₂ (perm.symm (perm_cons_app a t)) theorem length_eq_length_of_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) : length l₁ = length l₂ := perm.rec_on p rfl (λ x l₁ l₂ p r, by simp[r]) (λ x y l, by simp) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) theorem eq_nil_of_perm_nil {l₁ : list α} (p : ([] : list α) ~ l₁) : l₁ = ([] : list α) := eq_nil_of_length_eq_zero (length_eq_length_of_perm p).symm theorem not_perm_nil_cons (x : α) (l : list α) : ¬ [] ~ (x::l) := assume p, by have h := eq_nil_of_perm_nil p; contradiction theorem eq_singleton_of_perm {a b : α} (p : [a] ~ [b]) : a = b := have a ∈ [b], from mem_of_perm p (by simp), by simp at this; simp [*] theorem eq_singleton_of_perm_inv {a : α} {l : list α} (p : [a] ~ l) : l = [a] := match l, length_eq_length_of_perm p, p with | [a'], rfl, p := by simp [eq_singleton_of_perm p] end theorem perm_rev : ∀ (l : list α), l ~ (reverse l) | [] := nil | (x::xs) := calc x::xs ~ x::reverse xs : skip x (perm_rev xs) ... ~ reverse xs ++ [x] : perm_cons_app _ _ ... = reverse (x::xs) : by rw [reverse_cons, concat_eq_append] @[simp] theorem perm_rev_simp (l : list α) : (reverse l) ~ l := perm.symm (perm_rev l) theorem perm_middle (a : α) (l₁ l₂ : list α) : (a::l₁)++l₂ ~ l₁++(a::l₂) := have a::l₁++l₂ ~ l₁++[a]++l₂, from perm_app_left l₂ (perm_cons_app a l₁), by simp at this; exact this attribute [simp] theorem perm_middle_simp (a : α) (l₁ l₂ : list α) : l₁++(a::l₂) ~ (a::l₁)++l₂ := perm.symm $ perm_middle a l₁ l₂ theorem perm_cons_app_cons {l l₁ l₂ : list α} (a : α) (p : l ~ l₁++l₂) : a::l ~ l₁++(a::l₂) := trans (skip a p) $ perm_middle a l₁ l₂ open decidable theorem perm_erase [decidable_eq α] {a : α} : ∀ {l : list α}, a ∈ l → l ~ a:: l.erase a | [] h := false.elim h | (x::t) h := if ax : x = a then by rw [ax, erase_cons_head] else by rw [erase_cons_tail _ ax]; exact have aint : a ∈ t, from mem_of_ne_of_mem (assume h, ax h.symm) h, trans (skip _ $ perm_erase aint) (swap _ _ _) @[elab_as_eliminator] theorem perm_induction_on {P : list α → list α → Prop} {l₁ l₂ : list α} (p : l₁ ~ l₂) (h₁ : P [] []) (h₂ : ∀ x l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (x::l₁) (x::l₂)) (h₃ : ∀ x y l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (y::x::l₁) (x::y::l₂)) (h₄ : ∀ l₁ l₂ l₃, l₁ ~ l₂ → l₂ ~ l₃ → P l₁ l₂ → P l₂ l₃ → P l₁ l₃) : P l₁ l₂ := have P_refl : ∀ l, P l l, from assume l, list.rec_on l h₁ (λ x xs ih, h₂ x xs xs (perm.refl xs) ih), perm.rec_on p h₁ h₂ (λ x y l, h₃ x y l l (perm.refl l) (P_refl l)) h₄ theorem xswap {l₁ l₂ : list α} (x y : α) : l₁ ~ l₂ → x::y::l₁ ~ y::x::l₂ := assume p, calc x::y::l₁ ~ y::x::l₁ : swap y x l₁ ... ~ y::x::l₂ : skip y (skip x p) @[congr] theorem perm_map (f : α → β) {l₁ l₂ : list α} (p : l₁ ~ l₂) : map f l₁ ~ map f l₂ := perm_induction_on p nil (λ x l₁ l₂ p r, skip (f x) r) (λ x y l₁ l₂ p r, xswap (f y) (f x) r) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) section count variable [decα : decidable_eq α] include decα theorem count_eq_count_of_perm {l₁ l₂ : list α} : l₁ ~ l₂ → ∀ a, count a l₁ = count a l₂ := assume : l₁ ~ l₂, perm.rec_on this (λ a, rfl) (λ x l₁ l₂ p h a, begin simp [count_cons', h a] end) (λ x y l a, begin simp [count_cons'] end) (λ l₁ l₂ l₃ p₁ p₂ h₁ h₂ a, eq.trans (h₁ a) (h₂ a)) theorem perm_of_forall_count_eq : ∀ {l₁ l₂ : list α}, (∀ a, count a l₁ = count a l₂) → l₁ ~ l₂ | [] := assume l₂, assume h : ∀ a, count a [] = count a l₂, have ∀ a, a ∉ l₂, from assume a, not_mem_of_count_eq_zero (by simp [(h a).symm]), have l₂ = [], from eq_nil_of_forall_not_mem this, show [] ~ l₂, by rw this | (b :: l) := assume l₂, assume h : ∀ a, count a (b :: l) = count a l₂, have b ∈ l₂, from mem_of_count_pos (begin rw [←(h b)], simp, apply nat.succ_pos end), have l₂ ~ b :: l₂.erase b, from perm_erase this, have ∀ a, count a l = count a (l₂.erase b), from assume a, if h' : a = b then nat.succ_inj (calc count a l + 1 = count a (b :: l) : by simp [h', nat.succ_eq_add_one] ... = count a l₂ : by rw h ... = count a (b :: l₂.erase b) : count_eq_count_of_perm (by assumption) a ... = count a (l₂.erase b) + 1 : by simp [h', nat.succ_eq_add_one]) else calc count a l = count a (b :: l) : by simp [h'] ... = count a l₂ : by rw h ... = count a (b :: l₂.erase b) : count_eq_count_of_perm (by assumption) a ... = count a (l₂.erase b) : by simp [h'], have l ~ l₂.erase b, from perm_of_forall_count_eq this, calc b :: l ~ b :: l₂.erase b : skip b this ... ~ l₂ : perm.symm (by assumption) theorem perm_iff_forall_count_eq_count (l₁ l₂ : list α) : l₁ ~ l₂ ↔ ∀ a, count a l₁ = count a l₂ := iff.intro count_eq_count_of_perm perm_of_forall_count_eq -- This next theorem shows that perm is equivalent to a decidable (and efficiently checkable) -- property. theorem perm_iff_forall_mem_count_eq_count (l₁ l₂ : list α) : l₁ ~ l₂ ↔ ∀ a ∈ erase_dup (l₁ ∪ l₂), count a l₁ = count a l₂ := iff.intro (assume h : l₁ ~ l₂, assume a, assume ha, count_eq_count_of_perm h a) (assume h, have ∀ a, count a l₁ = count a l₂, from assume a, if hl₁ : a ∈ l₁ then have a ∈ erase_dup (l₁ ∪ l₂), from mem_erase_dup.2 (mem_union_left hl₁ l₂), h a this else if hl₂ : a ∈ l₂ then have a ∈ erase_dup (l₁ ∪ l₂), from mem_erase_dup.2 (mem_union_right l₁ hl₂), h a this else by simp [hl₁, hl₂], perm_of_forall_count_eq this) instance : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂) := assume l₁ l₂, decidable_of_decidable_of_iff (list.decidable_forall_mem _) (perm_iff_forall_mem_count_eq_count l₁ l₂).symm end count -- Auxiliary theorem for performing cases-analysis on l₂. -- We use it to prove perm_inv_core. private lemma discr {P : Prop} {a b : α} {l₁ l₂ l₃ : list α} : a::l₁ = l₂++(b::l₃) → (l₂ = [] → a = b → l₁ = l₃ → P) → (∀ t, l₂ = a::t → l₁ = t++(b::l₃) → P) → P := match l₂ with | [] := λ e h₁ h₂, begin simp at e, injection e with e₁ e₂, exact h₁ rfl e₁ e₂ end | h::t := λ e h₁ h₂, begin simp at e, injection e with e₁ e₂, rw e₁ at h₂, exact h₂ t rfl e₂ end end -- Auxiliary theorem for performing cases-analysis on l₂. -- We use it to prove perm_inv_core. private lemma discr₂ {P : Prop} {a b c : α} {l₁ l₂ l₃ : list α} (e : a::b::l₁ = l₂++(c::l₃)) (H₁ : l₂ = [] → l₃ = b::l₁ → a = c → P) (H₂ : l₂ = [a] → b = c → l₁ = l₃ → P) (H₃ : ∀ t, l₂ = a::b::t → l₁ = t++(c::l₃) → P) : P := discr e (λh₁ h₂ h₃, H₁ h₁ h₃.symm h₂) $ λt h₁ h₂, discr h₂ (λh₃ h₄, match t, h₃, h₁ with ._, rfl, h₁ := H₂ h₁ h₄ end) (λt' h₃ h₄, match t, h₃, h₁ with ._, rfl, h₁ := H₃ t' h₁ h₄ end) /- quasiequal a l l' means that l' is exactly l, with a added once somewhere -/ section qeq inductive qeq (a : α) : list α → list α → Prop | qhead : ∀ l, qeq l (a::l) | qcons : ∀ (b : α) {l l' : list α}, qeq l l' → qeq (b::l) (b::l') open qeq notation l' `≈`:50 a `|` l:50 := qeq a l l' theorem perm_of_qeq {a : α} {l₁ l₂ : list α} : l₁≈a|l₂ → l₁~a::l₂ := assume q, qeq.rec_on q (λ h, perm.refl (a :: h)) (λ b t₁ t₂ q₁ r₁, calc b::t₂ ~ b::a::t₁ : skip b r₁ ... ~ a::b::t₁ : swap a b t₁) theorem qeq_app : ∀ (l₁ : list α) (a : α) (l₂ : list α), l₁ ++ (a :: l₂) ≈ a | l₁ ++ l₂ | ([] : list α) b l₂ := qhead b l₂ | (a::ains) b l₂ := qcons a (qeq_app ains b l₂) theorem mem_head_of_qeq {a : α} : ∀ {l₁ l₂ : list α}, l₁ ≈ a | l₂ → a ∈ l₁ | ._ ._ (qhead .(a) l) := mem_cons_self a l | ._ ._ (@qcons .(α) .(a) b l l' q) := mem_cons_of_mem b (mem_head_of_qeq q) theorem mem_tail_of_qeq {a : α} : ∀ {l₁ l₂ : list α}, l₁ ≈ a | l₂ → ∀ {b}, b ∈ l₂ → b ∈ l₁ | ._ ._ (qhead .(a) l) b bl := mem_cons_of_mem a bl | ._ ._ (@qcons .(α) .(a) c l l' q) b bcl := or.elim (eq_or_mem_of_mem_cons bcl) (assume bc : b = c, begin rw bc, apply mem_cons_self end) (assume bl : b ∈ l, have bl' : b ∈ l', from mem_tail_of_qeq q bl, mem_cons_of_mem c bl') theorem mem_cons_of_qeq {a : α} : ∀ {l₁ l₂ : list α}, l₁≈a|l₂ → ∀ {b}, b ∈ l₁ → b ∈ a::l₂ | ._ ._ (qhead ._ l) b bal := bal | ._ ._ (@qcons ._ ._ c l l' q) b (bcl' : b ∈ c :: l') := show b ∈ a :: c :: l, from or.elim (eq_or_mem_of_mem_cons bcl') (assume bc : b = c, begin rw bc, apply mem_cons_of_mem, apply mem_cons_self end) (assume bl' : b ∈ l', have b ∈ a :: l, from mem_cons_of_qeq q bl', or.elim (eq_or_mem_of_mem_cons this) (assume ba : b = a, begin rw ba, apply mem_cons_self end) (assume bl : b ∈ l, mem_cons_of_mem a (mem_cons_of_mem c bl))) theorem length_eq_of_qeq {a : α} {l₁ l₂ : list α} : l₁ ≈ a | l₂ → length l₁ = nat.succ (length l₂) := begin intro q, induction q with l b l l' q ih, simp[nat.one_add], simp [*] end theorem qeq_of_mem {a : α} {l : list α} : a ∈ l → (∃ l', l ≈ a | l') := list.rec_on l (λ h : a ∈ ([] : list α), absurd h (not_mem_nil a)) (λ b bs r ainbbs, or.elim (eq_or_mem_of_mem_cons ainbbs) (λ aeqb : a = b, have ∃ l, b::bs ≈ b | l, from exists.intro bs (qhead b bs), begin rw aeqb, exact this end) (λ ainbs : a ∈ bs, have ∃ l', bs ≈ a|l', from r ainbs, exists.elim this (assume (l' : list α) (q : bs ≈ a|l'), have b::bs ≈ a | b::l', from qcons b q, exists.intro (b::l') this))) theorem qeq_split {a : α} : ∀ {l l' : list α}, l'≈a|l → ∃ l₁ l₂, l = l₁ ++ l₂ ∧ l' = l₁ ++ (a::l₂) | ._ ._ (qhead ._ l) := ⟨[], l, by simp⟩ | ._ ._ (@qcons ._ ._ c l l' q) := match (qeq_split q) with | ⟨l₁, l₂, h₁, h₂⟩ := ⟨c :: l₁, l₂, by simp [h₁, h₂]⟩ end theorem subset_of_mem_of_subset_of_qeq {a : α} {l : list α} {u v : list α} : a ∉ l → a::l ⊆ v → v≈a|u → l ⊆ u := λ (nainl : a ∉ l) (s : a::l ⊆ v) (q : v≈a|u) (b : α) (binl : b ∈ l), have b ∈ v, from s (or.inr binl), have b ∈ a::u, from mem_cons_of_qeq q this, or.elim (eq_or_mem_of_mem_cons this) (assume : b = a, begin subst b, contradiction end) (assume : b ∈ u, this) theorem perm_inv_core {l₁ l₂ : list α} (p' : l₁ ~ l₂) : ∀ {a s₁ s₂}, l₁≈a|s₁ → l₂≈a|s₂ → s₁ ~ s₂ := perm_induction_on p' (λ a s₁ s₂ e₁ e₂, match e₁ with end) (λ x t₁ t₂ p (r : ∀{a s₁ s₂}, t₁≈a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, match s₁, s₂, qeq_split e₁, qeq_split e₂ with ._, ._, ⟨s₁₁, s₁₂, rfl, C₁⟩, ⟨s₂₁, s₂₂, rfl, C₂⟩ := discr C₁ (λe₁ xa, match s₁₁, x, e₁, xa, C₂ with ._, ._, rfl, rfl, C₂ := discr C₂ (λe₁ _ e₂ e₃, match s₁₂, s₂₁, s₂₂, e₁, e₂, e₃ with | ._, ._, ._, rfl, rfl, rfl := p end) (λt₃ e₁ e₂ e₃, match s₂₁, s₁₂, t₂, e₁, e₂, e₃, p with | ._, ._, ._, rfl, rfl, rfl, p := trans p (perm_middle _ _ _).symm end) end) (λt₃ e₁ e₂, match s₁₁, t₁, e₁, e₂, C₂, p, @r with ._, ._, rfl, rfl, C₂, p, r := discr C₂ (λe₁ xa e₂, match x, s₂₁, s₂₂, xa, e₁, e₂ with | ._, ._, ._, rfl, rfl, rfl := trans (perm_middle _ _ _) p end) (λt₃ e₁ e₂, match s₂₁, t₂, e₁, e₂, @r with | ._, ._, rfl, rfl, r := skip x (r (qeq_app _ _ _) (qeq_app _ _ _)) end) end) end) (λ x y t₁ t₂ p (r : ∀{a s₁ s₂}, t₁≈a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, match s₁, s₂, qeq_split e₁, qeq_split e₂ with ._, ._, ⟨s₁₁, s₁₂, rfl, C₁⟩, ⟨s₂₁, s₂₂, rfl, C₂⟩ := discr₂ C₁ (λe₁ e₂ ya, match s₁₁, s₁₂, y, e₁, e₂, ya, C₂ with ._, ._, ._, rfl, rfl, rfl, C₂ := discr₂ C₂ (λe₁ e₂ xa, match s₂₁, s₂₂, x, e₁, e₂, xa with | ._, ._, ._, rfl, rfl, rfl := skip a p end) (λe₁ _ e₂, match s₂₁, s₂₂, e₁, e₂ with | ._, ._, rfl, rfl := skip x p end) (λt₃ e₁ e₂, match s₂₁, t₂, e₁, e₂, p with | ._, ._, rfl, rfl, p := skip x (trans p (perm_middle _ _ _).symm) end) end) (λe₁ xa e₂, match s₁₁, s₁₂, x, e₁, e₂, xa, C₂ with ._, ._, ._, rfl, rfl, rfl, C₂ := discr₂ C₂ (λe₁ e₂ _, match s₂₁, s₂₂, e₁, e₂ with | ._, ._, rfl, rfl := skip y p end) (λe₁ ya e₂, match s₂₁, s₂₂, y, e₁, e₂, ya with | ._, ._, ._, rfl, rfl, rfl := skip a p end) (λt₃ e₁ e₂, match s₂₁, t₂, e₁, e₂, p with | ._, ._, rfl, rfl, p := trans (skip y $ trans p (perm_middle _ _ _).symm) (swap _ _ _) end) end) (λt₃ e₁ e₂, match s₁₁, t₁, e₁, e₂, C₂, p, @r with ._, ._, rfl, rfl, C₂, p, r := discr₂ C₂ (λe₁ e₂ xa, match s₂₁, s₂₂, x, e₁, e₂, xa with | ._, ._, ._, rfl, rfl, rfl := skip y (trans (perm_middle _ _ _) p) end) (λe₁ ya e₂, match s₂₁, s₂₂, y, e₁, e₂, ya with | ._, ._, ._, rfl, rfl, rfl := trans (swap _ _ _) (skip x $ trans (perm_middle _ _ _) p) end) (λt₄ e₁ e₂, match s₂₁, t₂, e₁, e₂, @r with | ._, ._, rfl, rfl, r := trans (swap _ _ _) (skip x $ skip y $ r (qeq_app _ _ _) (qeq_app _ _ _)) end) end) end) (λ t₁ t₂ t₃ p₁ p₂ (r₁ : ∀{a s₁ s₂}, t₁ ≈ a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) (r₂ : ∀{a s₁ s₂}, t₂ ≈ a|s₁ → t₃≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, let ⟨t₂', e₂'⟩ := qeq_of_mem $ mem_of_perm p₁ $ mem_head_of_qeq e₁ in trans (r₁ e₁ e₂') (r₂ e₂' e₂)) end qeq theorem perm_cons_inv {a : α} {l₁ l₂ : list α} (p : a::l₁ ~ a::l₂) : l₁ ~ l₂ := perm_inv_core p (qeq.qhead a l₁) (qeq.qhead a l₂) theorem perm_app_inv_left {l₁ l₂ : list α} : ∀ {l}, l++l₁ ~ l++l₂ → l₁ ~ l₂ | [] p := p | (a::l) p := perm_app_inv_left (perm_cons_inv p) theorem perm_app_inv_right {l₁ l₂ l : list α} (p : l₁++l ~ l₂++l) : l₁ ~ l₂ := perm_app_inv_left $ trans perm_app_comm $ trans p perm_app_comm theorem perm_app_inv {a : α} {l₁ l₂ l₃ l₄ : list α} (p : l₁++(a::l₂) ~ l₃++(a::l₄)) : l₁++l₂ ~ l₃++l₄ := perm_cons_inv $ trans (perm_middle _ _ _) $ trans p $ (perm_middle _ _ _).symm theorem foldl_eq_of_perm {f : β → α → β} {l₁ l₂ : list α} (rcomm : right_commutative f) (p : l₁ ~ l₂) : ∀ b, foldl f b l₁ = foldl f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, r (f b x)) (λ x y t₁ t₂ p r b, by simp; rw rcomm; exact r (f (f b x) y)) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ b, eq.trans (r₁ b) (r₂ b)) theorem foldr_eq_of_perm {f : α → β → β} {l₁ l₂ : list α} (lcomm : left_commutative f) (p : l₁ ~ l₂) : ∀ b, foldr f b l₁ = foldr f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, by simp; rw [r b]) (λ x y t₁ t₂ p r b, by simp; rw [lcomm, r b]) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ a, eq.trans (r₁ a) (r₂ a)) -- attribute [congr] theorem erase_perm_erase_of_perm [decidable_eq α] (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁.erase a ~ l₂.erase a := if h₁ : a ∈ l₁ then have h₂ : a ∈ l₂, from mem_of_perm p h₁, perm_cons_inv $ trans (perm_erase h₁).symm $ trans p (perm_erase h₂) else have h₂ : a ∉ l₂, from not_mem_of_perm p h₁, by rw [erase_of_not_mem h₁, erase_of_not_mem h₂]; exact p -- attribute [congr] theorem perm_erase_dup_of_perm [H : decidable_eq α] {l₁ l₂ : list α} : l₁ ~ l₂ → erase_dup l₁ ~ erase_dup l₂ := assume p, perm_induction_on p nil (λ x t₁ t₂ p r, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_erase_dup.1 (mem_of_perm r (mem_erase_dup.2 xint₁)), begin rw [erase_dup_cons_of_mem xint₁, erase_dup_cons_of_mem xint₂], exact r end) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from assume xint₂ : x ∈ t₂, absurd (mem_erase_dup.1 (mem_of_perm (perm.symm r) (mem_erase_dup.2 xint₂))) nxint₁, begin rw [erase_dup_cons_of_not_mem nxint₂, erase_dup_cons_of_not_mem nxint₁], exact (skip x r) end)) (λ y x t₁ t₂ p r, by_cases (λ xinyt₁ : x ∈ y::t₁, by_cases (λ yint₁ : y ∈ t₁, have yint₂ : y ∈ t₂, from mem_erase_dup.1 (mem_of_perm r (mem_erase_dup.2 yint₁)), have yinxt₂ : y ∈ x::t₂, from or.inr (yint₂), or.elim (eq_or_mem_of_mem_cons xinyt₁) (λ xeqy : x = y, have xint₂ : x ∈ t₂, begin rw [←xeqy] at yint₂, exact yint₂ end, begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_mem xint₂], exact r end) (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_erase_dup.1 (mem_of_perm r (mem_erase_dup.2 xint₁)), begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_mem xint₂], exact r end)) (λ nyint₁ : y ∉ t₁, have nyint₂ : y ∉ t₂, from assume yint₂ : y ∈ t₂, absurd (mem_erase_dup.1 (mem_of_perm (perm.symm r) (mem_erase_dup.2 yint₂))) nyint₁, by_cases (λ xeqy : x = y, have nxint₂ : x ∉ t₂, begin rw [←xeqy] at nyint₂, exact nyint₂ end, have yinxt₂ : y ∈ x::t₂, begin rw [xeqy], apply mem_cons_self end, begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_not_mem nxint₂, xeqy], exact skip y r end) (λ xney : x ≠ y, have x ∈ t₁, from or_resolve_right xinyt₁ xney, have x ∈ t₂, from mem_erase_dup.1 (mem_of_perm r (mem_erase_dup.2 this)), have y ∉ x::t₂, from assume : y ∈ x::t₂, or.elim (eq_or_mem_of_mem_cons this) (λ h, absurd h (ne.symm xney)) (λ h, absurd h nyint₂), begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_not_mem ‹y ∉ x::t₂›, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_mem ‹x ∈ t₂›], exact skip y r end))) (λ nxinyt₁ : x ∉ y::t₁, have xney : x ≠ y, from ne_of_not_mem_cons nxinyt₁, have nxint₁ : x ∉ t₁, from not_mem_of_not_mem_cons nxinyt₁, have nxint₂ : x ∉ t₂, from assume xint₂ : x ∈ t₂, absurd (mem_erase_dup.1 (mem_of_perm (perm.symm r) (mem_erase_dup.2 xint₂))) nxint₁, by_cases (λ yint₁ : y ∈ t₁, have yinxt₂ : y ∈ x::t₂, from or.inr (mem_erase_dup.1 (mem_of_perm r (mem_erase_dup.2 yint₁))), begin rw [erase_dup_cons_of_not_mem nxinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_not_mem nxint₂], exact skip x r end) (λ nyint₁ : y ∉ t₁, have nyinxt₂ : y ∉ x::t₂, from assume yinxt₂ : y ∈ x::t₂, or.elim (eq_or_mem_of_mem_cons yinxt₂) (λ h, absurd h (ne.symm xney)) (λ h, absurd (mem_erase_dup.1 (mem_of_perm (r.symm) (mem_erase_dup.2 h))) nyint₁), begin rw [erase_dup_cons_of_not_mem nxinyt₁, erase_dup_cons_of_not_mem nyinxt₂, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_not_mem nxint₂], exact xswap x y r end))) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂, trans r₁ r₂) /- permutation is decidable if α has decidable equality -/ section dec open decidable variable [Ha : decidable_eq α] include Ha def decidable_perm_aux : ∀ (n : nat) (l₁ l₂ : list α), length l₁ = n → length l₂ = n → decidable (l₁ ~ l₂) | 0 l₁ l₂ H₁ H₂ := have l₁n : l₁ = [], from eq_nil_of_length_eq_zero H₁, have l₂n : l₂ = [], from eq_nil_of_length_eq_zero H₂, begin rw [l₁n, l₂n], exact (is_true perm.nil) end | (n+1) (x::t₁) l₂ H₁ H₂ := by_cases (assume xinl₂ : x ∈ l₂, have len_t₁ : length t₁ = n, begin simp only [length] at H₁, have H₁' : nat.succ (length t₁) = nat.succ n, exact H₁, injection H₁' with e end, have length (l₂.erase x) = nat.pred (length l₂), from length_erase_of_mem xinl₂, have length (l₂.erase x) = n, begin rw [this, H₂], reflexivity end, match decidable_perm_aux n t₁ (l₂.erase x) len_t₁ this with | is_true p := is_true (calc x::t₁ ~ x::(l₂.erase x) : skip x p ... ~ l₂ : perm.symm (perm_erase xinl₂)) | is_false np := is_false (λ p : x::t₁ ~ l₂, have (x::t₁).erase x ~ l₂.erase x, from erase_perm_erase_of_perm x p, have t₁ ~ l₂.erase x, begin rw [erase_cons_head] at this, exact this end, absurd this np) end) (assume nxinl₂ : x ∉ l₂, is_false (λ p : x::t₁ ~ l₂, absurd (mem_of_perm p (mem_cons_self _ _)) nxinl₂)) @[instance] def decidable_perm : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂) := λ l₁ l₂, by_cases (assume eql : length l₁ = length l₂, decidable_perm_aux (length l₂) l₁ l₂ eql rfl) (assume neql : length l₁ ≠ length l₂, is_false (λ p : l₁ ~ l₂, absurd (length_eq_length_of_perm p) neql)) end dec -- attribute [congr] theorem perm_insert [decidable_eq α] (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : (insert a l₁) ~ (insert a l₂) := begin by_cases a ∈ l₁ with al₁, { simp [al₁, mem_of_perm p al₁], exact p }, { simp [al₁, mt (mem_of_perm p.symm) al₁], constructor, exact p } end theorem perm_insert_insert [decidable_eq α] (x y : α) (l : list α) : insert x (insert y l) ~ insert y (insert x l) := begin by_cases x ∈ l with xl; by_cases y ∈ l with yl; simp [xl, yl], by_cases x = y with xy, { simp [xy] }, simp [not_mem_cons_of_ne_of_not_mem xy xl, not_mem_cons_of_ne_of_not_mem (ne.symm xy) yl], constructor end section perm_union variable [decidable_eq α] theorem perm_union_left {l₁ l₂ : list α} (t₁ : list α) (h : l₁ ~ l₂) : (l₁ ∪ t₁) ~ (l₂ ∪ t₁) := begin induction h using list.perm.rec_on with a l₁ l₂ generalizing t₁; try {simp}, exact perm_insert _ (ih_1 _), apply perm_insert_insert, exact (ih_1 _).trans (ih_2 _) end theorem perm_union_right (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : (l ∪ t₁) ~ (l ∪ t₂) := by induction l; simp [*, perm_insert] -- attribute [congr] theorem perm_union {l₁ l₂ t₁ t₂ : list α} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁ ∪ t₁) ~ (l₂ ∪ t₂) := assume p₁ p₂, trans (perm_union_left t₁ p₁) (perm_union_right l₂ p₂) end perm_union section perm_inter variable [decidable_eq α] theorem perm_inter_left {l₁ l₂ : list α} (t₁ : list α) : l₁ ~ l₂ → (l₁ ∩ t₁) ~ (l₂ ∩ t₁) := assume p, perm.rec_on p (perm.refl _) (λ x l₁ l₂ p₁ r₁, by_cases (λ xint₁ : x ∈ t₁, begin rw [inter_cons_of_mem _ xint₁, inter_cons_of_mem _ xint₁], exact (skip x r₁) end) (λ nxint₁ : x ∉ t₁, begin rw [inter_cons_of_not_mem _ nxint₁, inter_cons_of_not_mem _ nxint₁], exact r₁ end)) (λ x y l, by_cases (λ yint : y ∈ t₁, by_cases (λ xint : x ∈ t₁, begin rw [inter_cons_of_mem _ xint, inter_cons_of_mem _ yint, inter_cons_of_mem _ yint, inter_cons_of_mem _ xint], apply swap end) (λ nxint : x ∉ t₁, begin rw [inter_cons_of_mem _ yint, inter_cons_of_not_mem _ nxint, inter_cons_of_not_mem _ nxint, inter_cons_of_mem _ yint] end)) (λ nyint : y ∉ t₁, by_cases (λ xint : x ∈ t₁, by rw [inter_cons_of_mem _ xint, inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nyint, inter_cons_of_mem _ xint]) (λ nxint : x ∉ t₁, by rw [inter_cons_of_not_mem _ nxint, inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nxint]))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_inter_right (l : list α) {t₁ t₂ : list α} : t₁ ~ t₂ → (l ∩ t₁) ~ (l ∩ t₂) := list.rec_on l (λ p, by simp [inter_nil]) (λ x xs r p, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_of_perm p xint₁, begin rw [inter_cons_of_mem _ xint₁, inter_cons_of_mem _ xint₂], exact (skip _ (r p)) end) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from not_mem_of_perm p nxint₁, begin rw [inter_cons_of_not_mem _ nxint₁, inter_cons_of_not_mem _ nxint₂], exact (r p) end)) -- attribute [congr] theorem perm_inter {l₁ l₂ t₁ t₂ : list α} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁ ∩ t₁) ~ (l₂ ∩ t₂) := assume p₁ p₂, trans (perm_inter_left t₁ p₁) (perm_inter_right l₂ p₂) end perm_inter /- extensionality -/ section ext theorem perm_ext : ∀ {l₁ l₂ : list α}, nodup l₁ → nodup l₂ → (∀a, a ∈ l₁ ↔ a ∈ l₂) → l₁ ~ l₂ | [] [] d₁ d₂ e := perm.nil | [] (a₂::t₂) d₁ d₂ e := absurd (iff.mpr (e a₂) (mem_cons_self _ _)) (not_mem_nil a₂) | (a₁::t₁) [] d₁ d₂ e := absurd (iff.mp (e a₁) (mem_cons_self _ _)) (not_mem_nil a₁) | (a₁::t₁) (a₂::t₂) d₁ d₂ e := have a₁ ∈ a₂::t₂, from iff.mp (e a₁) (mem_cons_self _ _), have ∃ s₁ s₂, a₂::t₂ = s₁++(a₁::s₂), from mem_split this, -- obtain (s₁ s₂ : list α) (t₂_eq : a₂::t₂ = s₁++(a₁::s₂)), from this, match this with | ⟨ s₁, s₂, (t₂_eq : a₂::t₂ = s₁++(a₁::s₂)) ⟩ := have dt₂' : nodup (a₁::(s₁++s₂)), from nodup_head (begin rw [t₂_eq] at d₂, exact d₂ end), have eqv : ∀a, a ∈ t₁ ↔ a ∈ s₁++s₂, from assume a, iff.intro (assume : a ∈ t₁, have a ∈ a₂::t₂, from iff.mp (e a) (mem_cons_of_mem _ this), have a ∈ s₁++(a₁::s₂), begin rw [t₂_eq] at this, exact this end, or.elim (mem_append.1 this) (assume : a ∈ s₁, mem_append_left s₂ this) (assume : a ∈ a₁::s₂, or.elim (eq_or_mem_of_mem_cons this) (assume : a = a₁, have a₁ ∉ t₁, from not_mem_of_nodup_cons d₁, begin subst a, contradiction end) (assume : a ∈ s₂, mem_append_right s₁ this))) (assume : a ∈ s₁ ++ s₂, or.elim (mem_append.1 this) (assume : a ∈ s₁, have a ∈ a₂::t₂, from begin rw [t₂_eq], exact (mem_append_left _ this) end, have a ∈ a₁::t₁, from iff.mpr (e a) this, or.elim (eq_or_mem_of_mem_cons this) (assume : a = a₁, have a₁ ∉ s₁++s₂, from not_mem_of_nodup_cons dt₂', have a₁ ∉ s₁, from mt (mem_append_left _) this, begin subst a, contradiction end) (assume : a ∈ t₁, this)) (assume : a ∈ s₂, have a ∈ a₂::t₂, from begin rw [t₂_eq], exact (mem_append_right _ (mem_cons_of_mem _ this)) end, have a ∈ a₁::t₁, from iff.mpr (e a) this, or.elim (eq_or_mem_of_mem_cons this) (assume : a = a₁, have a₁ ∉ s₁++s₂, from not_mem_of_nodup_cons dt₂', have a₁ ∉ s₂, from mt (mem_append_right _) this, begin subst a, contradiction end) (assume : a ∈ t₁, this))), have ds₁s₂ : nodup (s₁++s₂), from nodup_of_nodup_cons dt₂', have nodup t₁, from nodup_of_nodup_cons d₁, calc a₁::t₁ ~ a₁::(s₁++s₂) : skip a₁ (perm_ext this ds₁s₂ eqv) ... ~ s₁++(a₁::s₂) : perm_middle _ _ _ ... = a₂::t₂ : by rw t₂_eq end end ext theorem nodup_of_perm_of_nodup {l₁ l₂ : list α} : l₁ ~ l₂ → nodup l₁ → nodup l₂ := assume h, perm.rec_on h (λ h, h) (λ a l₁ l₂ p ih nd, have nodup l₁, from nodup_of_nodup_cons nd, have nodup l₂, from ih this, have a ∉ l₁, from not_mem_of_nodup_cons nd, have a ∉ l₂, from assume : a ∈ l₂, absurd (mem_of_perm (perm.symm p) this) ‹a ∉ l₁›, nodup_cons ‹a ∉ l₂› ‹nodup l₂›) (λ x y l₁ nd, have nodup (x::l₁), from nodup_of_nodup_cons nd, have nodup l₁, from nodup_of_nodup_cons this, have x ∉ l₁, from not_mem_of_nodup_cons ‹nodup (x::l₁)›, have y ∉ x::l₁, from not_mem_of_nodup_cons nd, have x ≠ y, from assume : x = y, begin subst x, apply absurd (mem_cons_self _ _), apply ‹y ∉ y::l₁› end, -- this line used to be "exact absurd (mem_cons_self _ _) ‹y ∉ y::l₁›, but it's now a syntax error have y ∉ l₁, from not_mem_of_not_mem_cons ‹y ∉ x::l₁›, have x ∉ y::l₁, from not_mem_cons_of_ne_of_not_mem ‹x ≠ y› ‹x ∉ l₁›, have nodup (y::l₁), from nodup_cons ‹y ∉ l₁› ‹nodup l₁›, show nodup (x::y::l₁), from nodup_cons ‹x ∉ y::l₁› ‹nodup (y::l₁)›) (λ l₁ l₂ l₃ p₁ p₂ ih₁ ih₂ nd, ih₂ (ih₁ nd)) /- product -/ section product theorem perm_product_left {l₁ l₂ : list α} (t₁ : list β) : l₁ ~ l₂ → (product l₁ t₁) ~ (product l₂ t₁) := assume p : l₁ ~ l₂, perm.rec_on p (perm.refl _) (λ x l₁ l₂ p r, perm_app (perm.refl (map _ t₁)) r) (λ x y l, let m₁ := map (λ b, (x, b)) t₁ in let m₂ := map (λ b, (y, b)) t₁ in let c := product l t₁ in calc m₂ ++ (m₁ ++ c) = (m₂ ++ m₁) ++ c : by rw append_assoc ... ~ (m₁ ++ m₂) ++ c : perm_app perm_app_comm (perm.refl _) ... = m₁ ++ (m₂ ++ c) : by rw append_assoc) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_product_right (l : list α) {t₁ t₂ : list β} : t₁ ~ t₂ → (product l t₁) ~ (product l t₂) := list.rec_on l (λ p, by simp [nil_product]) (λ (a : α) (t : list α) (r : t₁ ~ t₂ → product t t₁ ~ product t t₂) (p : t₁ ~ t₂), perm_app (perm_map (λ b : β, (a, b)) p) (r p)) attribute [congr] theorem perm_product {l₁ l₂ : list α} {t₁ t₂ : list β} : l₁ ~ l₂ → t₁ ~ t₂ → (product l₁ t₁) ~ (product l₂ t₂) := assume p₁ p₂, trans (perm_product_left t₁ p₁) (perm_product_right l₂ p₂) end product /- filter -/ -- attribute [congr] theorem perm_filter {l₁ l₂ : list α} {p : α → Prop} [decidable_pred p] : l₁ ~ l₂ → (filter p l₁) ~ (filter p l₂) := assume u, perm.rec_on u perm.nil (assume x l₁' l₂', assume u' : l₁' ~ l₂', assume u'' : filter p l₁' ~ filter p l₂', decidable.by_cases (assume : p x, begin rw [filter_cons_of_pos _ this, filter_cons_of_pos _ this], apply perm.skip, apply u'' end) (assume : ¬ p x, begin rw [filter_cons_of_neg _ this, filter_cons_of_neg _ this], apply u'' end)) (assume x y l, decidable.by_cases (assume H1 : p x, decidable.by_cases (assume H2 : p y, begin rw [filter_cons_of_pos _ H1, filter_cons_of_pos _ H2, filter_cons_of_pos _ H2, filter_cons_of_pos _ H1], apply perm.swap end) (assume H2 : ¬ p y, by rw [filter_cons_of_pos _ H1, filter_cons_of_neg _ H2, filter_cons_of_neg _ H2, filter_cons_of_pos _ H1])) (assume H1 : ¬ p x, decidable.by_cases (assume H2 : p y, by rw [filter_cons_of_neg _ H1, filter_cons_of_pos _ H2, filter_cons_of_pos _ H2, filter_cons_of_neg _ H1]) (assume H2 : ¬ p y, by rw [filter_cons_of_neg _ H1, filter_cons_of_neg _ H2, filter_cons_of_neg _ H2, filter_cons_of_neg _ H1]))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) end perm end list
b7e926d3332db7c996952a6e16d751603f5e796c
3994e03e14a3cbe3858c1098d0ab9ed1113d6666
/06-formalizacija-dokazov/naravna.lean
41304f8e9d33e2d30e9ce6f547948a45a575c6c7
[]
no_license
tadejpetric/tpj-coq
15f2548ba8f012d9a5b5e0bfb78ab0860d048e96
dda9fb2e635f9a1302739e34d8692a4252066b76
refs/heads/master
1,629,182,614,649
1,600,449,570,000
1,600,449,570,000
222,798,649
1
0
null
null
null
null
UTF-8
Lean
false
false
2,259
lean
inductive naravno : Type -- type naravno = | nic : naravno -- | Nic | nasl : naravno -> naravno -- | Nasl of naravno def plus : naravno -> naravno -> naravno | naravno.nic m := m | (naravno.nasl m) n := naravno.nasl (plus m n) theorem plus_assoc : forall m n p, plus (plus m n) p = plus m (plus n p) := begin intros, induction m, case naravno.nic { unfold plus, }, case naravno.nasl { unfold plus, rewrite m_ih, } end -- P /\ Q ... p * q -- je_sodo 4 ... izjava -- je_sodo ... funkcija iz naravnih števil v izjave inductive je_sodo : naravno -> Prop | nic : je_sodo naravno.nic | nasl_nasl {n} : je_sodo n -> je_sodo (naravno.nasl (naravno.nasl n)) #check je_sodo.nasl_nasl je_sodo.nic lemma stiri_je_sodo : je_sodo (naravno.nasl (naravno.nasl (naravno.nasl (naravno.nasl naravno.nic)))) := begin apply je_sodo.nasl_nasl, apply je_sodo.nasl_nasl, apply je_sodo.nic, end theorem sodo_plus_sodo : forall m n, je_sodo m -> je_sodo n -> je_sodo (plus m n) := begin intros m n h_m h_n, induction h_m, case je_sodo.nic { unfold plus, assumption, }, case je_sodo.nasl_nasl { rename h_m_n m', unfold plus, apply je_sodo.nasl_nasl, assumption, } end inductive manj_enako : naravno -> naravno -> Prop | nic : forall m, manj_enako naravno.nic m | nasl {m n} : manj_enako m n -> manj_enako (naravno.nasl m) (naravno.nasl n) inductive manj_enako' : naravno -> naravno -> Prop | refl : forall m, manj_enako' m m | nasl : forall m n, manj_enako' m n -> manj_enako' m (naravno.nasl n) theorem manj_enako_refl : forall m, manj_enako m m := begin intros, induction m, case naravno.nic {exact (manj_enako.nic naravno.nic)}, case naravno.nasl {exact (manj_enako.nasl m_ih)}, end theorem manj_enako_trans : forall m n, manj_enako m n -> forall p, manj_enako n p -> manj_enako m p := begin intros m n p h_m_manjse_n h_n_manjse_p, induction h_m_manjse_n, case manj_enako.nic {exact (manj_enako.nic p)}, case manj_enako.nasl { rename h_m_manjse_n_m m', rename h_m_manjse_n_n n', cases h_n_manjse_p, rename h_n_manjse_p_n p', } end theorem manj_enako_refl' : forall m, manj_enako' m m := begin exact manj_enako'.refl end
74a1a60315f51bfca7fd4664d767205a767f0e11
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Std/Data/AssocList.lean
c22c33a050263ff60c1c9bf897d8eacd3801631f
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,081
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ universes u v w w' namespace Std /- List-like type to avoid extra level of indirection -/ inductive AssocList (α : Type u) (β : Type v) | nil : AssocList α β | cons (key : α) (value : β) (tail : AssocList α β) : AssocList α β namespace AssocList variables {α : Type u} {β : Type v} {δ : Type w} {m : Type w → Type w} [Monad m] abbrev empty : AssocList α β := nil instance : EmptyCollection (AssocList α β) := ⟨empty⟩ abbrev insert (m : AssocList α β) (k : α) (v : β) : AssocList α β := m.cons k v def isEmpty : AssocList α β → Bool | nil => true | _ => false @[specialize] def foldlM (f : δ → α → β → m δ) : (init : δ) → AssocList α β → m δ | d, nil => pure d | d, cons a b es => do let d ← f d a b foldlM f d es @[inline] def foldl (f : δ → α → β → δ) (init : δ) (as : AssocList α β) : δ := Id.run (foldlM f init as) def mapKey (f : α → δ) : AssocList α β → AssocList δ β | nil => nil | cons k v t => cons (f k) v (mapKey f t) def mapVal (f : β → δ) : AssocList α β → AssocList α δ | nil => nil | cons k v t => cons k (f v) (mapVal f t) def findEntry? [BEq α] (a : α) : AssocList α β → Option (α × β) | nil => none | cons k v es => match k == a with | true => some (k, v) | false => findEntry? a es def find? [BEq α] (a : α) : AssocList α β → Option β | nil => none | cons k v es => match k == a with | true => some v | false => find? a es def contains [BEq α] (a : α) : AssocList α β → Bool | nil => false | cons k v es => k == a || contains a es def replace [BEq α] (a : α) (b : β) : AssocList α β → AssocList α β | nil => nil | cons k v es => match k == a with | true => cons a b es | false => cons k v (replace a b es) def erase [BEq α] (a : α) : AssocList α β → AssocList α β | nil => nil | cons k v es => match k == a with | true => es | false => cons k v (erase a es) def any (p : α → β → Bool) : AssocList α β → Bool | nil => false | cons k v es => p k v || any p es def all (p : α → β → Bool) : AssocList α β → Bool | nil => true | cons k v es => p k v && all p es @[inline] def forIn {α : Type u} {β : Type v} {δ : Type w} {m : Type w → Type w'} [Monad m] (as : AssocList α β) (init : δ) (f : (α × β) → δ → m (ForInStep δ)) : m δ := let rec @[specialize] loop | d, nil => pure d | d, cons k v es => do match (← f (k, v) d) with | ForInStep.done d => pure d | ForInStep.yield d => loop d es loop init as end Std.AssocList def List.toAssocList {α : Type u} {β : Type v} : List (α × β) → Std.AssocList α β | [] => Std.AssocList.nil | (a,b) :: es => Std.AssocList.cons a b (toAssocList es)
1cd633074fb7911b7d589f4a20e46217e95cac94
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/topology/continuous_map.lean
8809248e64c67c86b55fc9583e3289957b2e53e8
[ "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
1,837
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Nicolò Cavalleri. -/ import topology.subset_properties import topology.tactic /-! # Continuous bundled map In this file we define the type `continuous_map` of continuous bundled maps. -/ /-- Bundled continuous maps. -/ @[protect_proj] structure continuous_map (α : Type*) (β : Type*) [topological_space α] [topological_space β] := (to_fun : α → β) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') notation `C(` α `, ` β `)` := continuous_map α β namespace continuous_map attribute [continuity] continuous_map.continuous_to_fun variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] instance : has_coe_to_fun (C(α, β)) := ⟨_, continuous_map.to_fun⟩ variables {α β} {f g : continuous_map α β} @[continuity] lemma coe_continuous : continuous (f : α → β) := f.continuous_to_fun @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H instance [inhabited β] : inhabited C(α, β) := ⟨{ to_fun := λ _, default _, }⟩ lemma coe_inj ⦃f g : C(α, β)⦄ (h : (f : α → β) = g) : f = g := by cases f; cases g; cases h; refl /-- The identity as a continuous map. -/ def id : C(α, α) := ⟨id⟩ /-- The composition of continuous maps, as a continuous map. -/ def comp (f : C(β, γ)) (g : C(α, β)) : C(α, γ) := { to_fun := λ a, f (g a), } protected lemma continuous (f : C(α, β)) : continuous f := f.continuous_to_fun /-- Takes `b` in input and gives the continuous bundled function constantly valued `b` in output. -/ def const (b : β) : C(α, β) := { to_fun := λ x, b, } end continuous_map
6481f7d245310c562fd25424091fb55ece2884cb
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/pfin/fintype.lean
96a3585bca863b9257ffce9ce05aea8ffb252ce6
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
1,436
lean
import data.fintype import pfin.lemmas universe u namespace fintype open finset def equiv_pfin_of_forall_mem_list {α : Type u} [fintype α] [decidable_eq α] {l : list α} (h : ∀ x:α, x ∈ l) (nd : l.nodup) : α ≃ pfin (l.length) := { to_fun := λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, inv_fun := λ i, l.nth_le i.val i.is_lt, left_inv := λ a, begin unfold pfin.val, simp *, end, right_inv := λ ⟨i, h⟩, begin apply pfin.eq_of_veq, simp [pfin.val], apply list.nodup_iff_nth_le_inj.mp nd _ _ (list.index_of_lt_length.mpr (list.nth_le_mem _ _ _)) h, simp, end } def equiv_pfin (α: Type u) [fintype α] [decidable_eq α] : trunc (α ≃ pfin (fintype.card α)) := begin unfold card finset.card, exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_pfin_of_forall_mem_list h nd)) mem_univ_val univ.2 end end fintype namespace list def pfin_range (n : ℕ) : list (pfin n) := (list.range n).pmap pfin.mk (λ _, list.mem_range.1) @[simp] lemma mem_pfin_range {n : ℕ} (a : pfin n) : a ∈ list.pfin_range n := mem_pmap.2 ⟨a.val, mem_range.2 a.is_lt, pfin.eta _ _⟩ lemma nodup_pfin_range (n : ℕ) : (pfin_range n).nodup := nodup_pmap (λ _ _ _ _, pfin.veq_of_eq) (nodup_range _) end list instance (n : ℕ) : fintype (pfin n) := ⟨⟨quot.mk _ (list.pfin_range n), list.nodup_pfin_range n⟩, list.mem_pfin_range⟩
82432ef5d8e876b28a10bc27fe89aacddce0959f
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/elab13.lean
c6e8e915c98c3bc2c56983b385a5913e65bb48f2
[ "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
252
lean
open tactic list check take c : name, ( do { env ← get_env, decl ← returnex (environment.get env c), num ← return (length (declaration.univ_params decl)), ls ← mk_num_meta_univs 2, return (expr.const c ls) } : tactic expr)
c4df7abfbdab1b4be9f2b8e4bcc2c7b5f751896d
4727251e0cd73359b15b664c3170e5d754078599
/src/representation_theory/invariants.lean
88bbe9353dc05259378930cdd0e1e0dc7b35d7bb
[ "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
5,124
lean
/- Copyright (c) 2022 Antoine Labelle. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Antoine Labelle -/ import representation_theory.basic import representation_theory.Rep /-! # Subspace of invariants a group representation This file introduces the subspace of invariants of a group representation and proves basic results about it. The main tool used is the average of all elements of the group, seen as an element of `monoid_algebra k G`. The action of this special element gives a projection onto the subspace of invariants. In order for the definition of the average element to make sense, we need to assume for most of the results that the order of `G` is invertible in `k` (e. g. `k` has characteristic `0`). -/ open_locale big_operators open monoid_algebra open representation namespace group_algebra variables (k G : Type*) [comm_semiring k] [group G] variables [fintype G] [invertible (fintype.card G : k)] /-- The average of all elements of the group `G`, considered as an element of `monoid_algebra k G`. -/ noncomputable def average : monoid_algebra k G := ⅟(fintype.card G : k) • ∑ g : G, of k G g lemma average_def : average k G = ⅟(fintype.card G : k) • ∑ g : G, of k G g := rfl /-- `average k G` is invariant under left multiplication by elements of `G`. -/ @[simp] theorem mul_average_left (g : G) : (finsupp.single g 1 * average k G : monoid_algebra k G) = average k G := begin simp only [mul_one, finset.mul_sum, algebra.mul_smul_comm, average_def, monoid_algebra.of_apply, finset.sum_congr, monoid_algebra.single_mul_single], set f : G → monoid_algebra k G := λ x, finsupp.single x 1, show ⅟ ↑(fintype.card G) • ∑ (x : G), f (g * x) = ⅟ ↑(fintype.card G) • ∑ (x : G), f x, rw function.bijective.sum_comp (group.mul_left_bijective g) _, end /-- `average k G` is invariant under right multiplication by elements of `G`. -/ @[simp] theorem mul_average_right (g : G) : average k G * finsupp.single g 1 = average k G := begin simp only [mul_one, finset.sum_mul, algebra.smul_mul_assoc, average_def, monoid_algebra.of_apply, finset.sum_congr, monoid_algebra.single_mul_single], set f : G → monoid_algebra k G := λ x, finsupp.single x 1, show ⅟ ↑(fintype.card G) • ∑ (x : G), f (x * g) = ⅟ ↑(fintype.card G) • ∑ (x : G), f x, rw function.bijective.sum_comp (group.mul_right_bijective g) _, end end group_algebra namespace representation section invariants open group_algebra variables {k G V : Type*} [comm_semiring k] [group G] [add_comm_monoid V] [module k V] variables (ρ : representation k G V) /-- The subspace of invariants, consisting of the vectors fixed by all elements of `G`. -/ def invariants : submodule k V := { carrier := set_of (λ v, ∀ (g : G), ρ g v = v), zero_mem' := λ g, by simp only [map_zero], add_mem' := λ v w hv hw g, by simp only [hv g, hw g, map_add], smul_mem' := λ r v hv g, by simp only [hv g, linear_map.map_smulₛₗ, ring_hom.id_apply]} @[simp] lemma mem_invariants (v : V) : v ∈ invariants ρ ↔ ∀ (g: G), ρ g v = v := by refl lemma invariants_eq_inter : (invariants ρ).carrier = ⋂ g : G, function.fixed_points (ρ g) := by {ext, simp [function.is_fixed_pt]} variables [fintype G] [invertible (fintype.card G : k)] /-- The action of `average k G` gives a projection map onto the subspace of invariants. -/ @[simp] noncomputable def average_map : V →ₗ[k] V := as_algebra_hom ρ (average k G) /-- The `average_map` sends elements of `V` to the subspace of invariants. -/ theorem average_map_invariant (v : V) : average_map ρ v ∈ invariants ρ := λ g, by rw [average_map, ←as_algebra_hom_single, ←linear_map.mul_apply, ←map_mul (as_algebra_hom ρ), mul_average_left] /-- The `average_map` acts as the identity on the subspace of invariants. -/ theorem average_map_id (v : V) (hv : v ∈ invariants ρ) : average_map ρ v = v := begin rw mem_invariants at hv, simp [average_def, map_sum, hv, finset.card_univ, nsmul_eq_smul_cast k _ v, smul_smul], end end invariants namespace lin_hom universes u open category_theory Action variables {k : Type u} [comm_ring k] {G : Group.{u}} lemma mem_invariants_iff_comm {X Y : Rep k G} (f : X.V →ₗ[k] Y.V) (g : G) : (lin_hom X.ρ Y.ρ) g f = f ↔ X.ρ g ≫ f = f ≫ Y.ρ g := begin rw [lin_hom_apply, ←ρ_Aut_apply_inv, ←linear_map.comp_assoc, ←Module.comp_def, ←Module.comp_def, iso.inv_comp_eq, ρ_Aut_apply_hom], exact comm, end /-- The invariants of the representation `lin_hom X.ρ Y.ρ` correspond to the the representation homomorphisms from `X` to `Y` -/ @[simps] def invariants_equiv_Rep_hom (X Y : Rep k G) : (lin_hom X.ρ Y.ρ).invariants ≃ₗ[k] (X ⟶ Y) := { to_fun := λ f, ⟨f.val, λ g, (mem_invariants_iff_comm _ g).1 (f.property g)⟩, map_add' := λ _ _, rfl, map_smul' := λ _ _, rfl, inv_fun := λ f, ⟨f.hom, λ g, (mem_invariants_iff_comm _ g).2 (f.comm g)⟩, left_inv := λ _, by { ext, refl }, right_inv := λ _, by { ext, refl } } end lin_hom end representation
fd3ca04b8e482973c6953a1d5aa46002ec036e63
0f5090f82d527e0df5bf3adac9f9e2e1d81d71e2
/src/apurva/spectral_theorem.lean
252a08be30095e4fabddc6321de8258bb6567e83
[]
no_license
apurvanakade/mc2020-lean-projects
36eb42c4baccc37183635c36f8e1b3afa4ec1230
02466225aa629ab1232043bcc0a053a099fdb939
refs/heads/master
1,688,791,717,534
1,597,874,092,000
1,597,874,092,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,382
lean
import .unitary import .normal import .eigenvalues import .algebraically_closed noncomputable theory open_locale matrix open_locale classical lemma spectral_theorem {n : ℕ} (M : matrix (fin n) (fin n) ℂ) (hn : M.normal) : ∃ U : matrix (fin n) (fin n) ℂ, ∃ D : (fin n) → ℂ, U.unitary ∧ M = U • (matrix.diagonal D) • U.complex_transpose := begin induction n with n ind, by sorry, -- what is (fin 0)? rcases exists_eigenvalue M with ⟨c, v, hne0, heigen⟩, let v' := (vector.complex_norm v)⁻¹ • v, have : vector.complex_norm v' = 1, by sorry, rcases unitary_of_unit_vector v' this with ⟨U, hU, hv'⟩, have hext : ∃ B (c : ℝ), U.conj • M • U = matrix.extension B c ∧ B.normal, by sorry, rcases hext with ⟨ B, c, key_ext, hB ⟩, specialize ind B hB, rcases ind with ⟨U', D', hU', key_B ⟩, use [U • (U'.extension 1), vector.extension D' c], split, sorry, sorry end -- theorem spectral_theorem {M : matrix n n ℂ} (hn : M.normal) : -- ∃ U : matrix n n ℂ, ∃ D : n → ℂ, -- U.unitary ∧ -- M = U • (matrix.diagonal D) • U.complex_transpose -- := -- begin -- rcases (upper_triangular_decomposition M) with ⟨U,L,hU,hLu,key⟩, -- have hLn : L.normal, by sorry, -- cases diagonal_of_upper_triangular_normal hLu hLn with D, -- use [U,D], -- split, -- {exact hU}, -- {rwa ←h}, -- end
e6c661cd97a09a6de7be9396b435a0796e62936c
b328e8ebb2ba923140e5137c83f09fa59516b793
/src/Lean/Server/Watchdog.lean
6bbbde5ee65c29e3c453a20f4515bba5be8acca0
[ "Apache-2.0" ]
permissive
DrMaxis/lean4
a781bcc095511687c56ab060e816fd948553e162
5a02c4facc0658aad627cfdcc3db203eac0cb544
refs/heads/master
1,677,051,517,055
1,611,876,226,000
1,611,876,226,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,573
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.System.IO import Init.Data.ByteArray import Std.Data.RBMap import Lean.Elab.Import import Lean.Data.Lsp import Lean.Server.FileSource import Lean.Server.Utils /-! For general server architecture, see `README.md`. This module implements the watchdog process. ## Watchdog state Most LSP clients only send us file diffs, so to facilitate sending entire file contents to freshly restarted workers, the watchdog needs to maintain the current state of each file. It can also use this state to detect changes to the header and thus restart the corresponding worker, freeing its imports. TODO(WN): We may eventually want to keep track of approximately (since this isn't knowable exactly) where in the file a worker crashed. Then on restart, we tell said worker to only parse up to that point and query the user about how to proceed (continue OR allow the user to fix the bug and then continue OR ..). Without this, if the crash is deterministic, users may be confused about why the server seemingly stopped working for a single file. ## Watchdog <-> worker communication The watchdog process and its file worker processes communicate via LSP. If the necessity arises, we might add non-standard commands similarly based on JSON-RPC. Most requests and notifications are forwarded to the corresponding file worker process, with the exception of these notifications: - textDocument/didOpen: Launch the file worker, create the associated watchdog state and launch a task to asynchronously receive LSP packets from the worker (e.g. request responses). - textDocument/didChange: Update the local file state. If the header was mutated, signal a shutdown to the file worker by closing the I/O channels. Then restart the file worker. Otherwise, forward the `didChange` notification. - textDocument/didClose: Signal a shutdown to the file worker and remove the associated watchdog state. Moreover, we don't implement the full protocol at this level: - Upon starting, the `initialize` request is forwarded to the worker, but it must not respond with its server capabilities. Consequently, the watchdog will not send an `initialized` notification to the worker. - After `initialize`, the watchdog sends the corresponding `didOpen` notification with the full current state of the file. No additional `didOpen` notifications will be forwarded to the worker process. - `$/cancelRequest` notifications are forwarded to all file workers. - File workers are always terminated with an `exit` notification, without previously receiving a `shutdown` request. Similarly, they never receive a `didClose` notification. ## Watchdog <-> client communication The watchdog itself should implement the LSP standard as closely as possible. However we reserve the right to add non-standard extensions in case they're needed, for example to communicate tactic state. -/ namespace Lean.Server.Watchdog open IO open Std (RBMap RBMap.empty) open Lsp open JsonRpc section Utils structure OpenDocument where meta : DocumentMeta headerAst : Syntax def workerCfg : Process.StdioConfig := { stdin := Process.Stdio.piped stdout := Process.Stdio.piped -- We pass workers' stderr through to the editor. stderr := Process.Stdio.inherit } /-- Events that worker-specific tasks signal to the main thread. -/ inductive WorkerEvent where /- A synthetic event signalling that the grouped edits should be processed. -/ | processGroupedEdits | terminated | crashed (e : IO.Error) | ioError (e : IO.Error) inductive WorkerState where /- The watchdog can detect a crashed file worker in two places: When trying to send a message to the file worker and when reading a request reply. In the latter case, the forwarding task terminates and delegates a `crashed` event to the main task. Then, in both cases, the file worker has its state set to `crashed` and requests that are in-flight are errored. Upon receiving the next packet for that file worker, the file worker is restarted and the packet is forwarded to it. If the crash was detected while writing a packet, we queue that packet until the next packet for the file worker arrives. -/ | crashed (queuedMsgs : Array JsonRpc.Message) | running abbrev PendingRequestMap := RBMap RequestID JsonRpc.Message (fun a b => Decidable.decide (a < b)) private def parseHeaderAst (input : String) : IO Syntax := do let inputCtx := Parser.mkInputContext input "<input>" let (stx, _, _) ← Parser.parseHeader inputCtx return stx end Utils section FileWorker /-- A group of edits which will be processed at a future instant. -/ structure GroupedEdits where /-- When to process the edits. -/ applyTime : Nat params : DidChangeTextDocumentParams /-- Signals when `applyTime` has been reached. -/ signalTask : Task WorkerEvent structure FileWorker where doc : OpenDocument proc : Process.Child workerCfg commTask : Task WorkerEvent state : WorkerState -- This should not be mutated outside of namespace FileWorker, as it is used as shared mutable state pendingRequestsRef : IO.Ref PendingRequestMap groupedEditsRef : IO.Ref (Option GroupedEdits) namespace FileWorker variable [ToJson α] def stdin (fw : FileWorker) : FS.Stream := FS.Stream.ofHandle fw.proc.stdin def stdout (fw : FileWorker) : FS.Stream := FS.Stream.ofHandle fw.proc.stdout def readMessage (fw : FileWorker) : IO JsonRpc.Message := do let msg ← fw.stdout.readLspMessage if let Message.response id _ := msg then fw.pendingRequestsRef.modify (fun pendingRequests => pendingRequests.erase id) if let Message.responseError id _ _ _ := msg then fw.pendingRequestsRef.modify (fun pendingRequests => pendingRequests.erase id) return msg def writeMessage (fw : FileWorker) (msg : JsonRpc.Message) : IO Unit := fw.stdin.writeLspMessage msg def writeNotification (fw : FileWorker) (n : Notification α) : IO Unit := fw.stdin.writeLspNotification n def writeRequest (fw : FileWorker) (r : Request α) : IO Unit := do fw.stdin.writeLspRequest r fw.pendingRequestsRef.modify (fun pendingRequests => pendingRequests.insert r.id r) def errorPendingRequests (fw : FileWorker) (hError : FS.Stream) (code : ErrorCode) (msg : String) : IO Unit := do let pendingRequests ← fw.pendingRequestsRef.modifyGet (fun pendingRequests => (pendingRequests, RBMap.empty)) for ⟨id, _⟩ in pendingRequests do hError.writeLspResponseError { id := id, code := code, message := msg } partial def runEditsSignalTask (fw : FileWorker) : IO (Task WorkerEvent) := do let rec loopAction : IO WorkerEvent := do let now ← monoMsNow let some ge ← fw.groupedEditsRef.get | throwServerError "Internal error: empty grouped edits reference in signal task" if ge.applyTime ≤ now then return WorkerEvent.processGroupedEdits else IO.sleep <| UInt32.ofNat <| ge.applyTime - now loopAction let t ← IO.asTask loopAction return t.map fun | Except.ok ev => ev | Except.error e => WorkerEvent.ioError e end FileWorker end FileWorker section ServerM abbrev FileWorkerMap := RBMap DocumentUri FileWorker (fun a b => Decidable.decide (a < b)) structure ServerContext where hIn : FS.Stream hOut : FS.Stream hLog : FS.Stream args : List String fileWorkersRef : IO.Ref FileWorkerMap -- We store these to pass them to workers. initParams : InitializeParams workerPath : String abbrev ServerM := ReaderT ServerContext IO def updateFileWorkers (val : FileWorker) : ServerM Unit := do (←read).fileWorkersRef.modify (fun fileWorkers => fileWorkers.insert val.doc.meta.uri val) def findFileWorker (uri : DocumentUri) : ServerM FileWorker := do match (←(←read).fileWorkersRef.get).find? uri with | some fw => fw | none => throwServerError s!"Got unknown document URI ({uri})" def eraseFileWorker (uri : DocumentUri) : ServerM Unit := do (←read).fileWorkersRef.modify (fun fileWorkers => fileWorkers.erase uri) def log (msg : String) : ServerM Unit := do let st ← read st.hLog.putStrLn msg st.hLog.flush /-- Creates a Task which forwards a worker's messages into the output stream until an event which must be handled in the main watchdog thread (e.g. an I/O error) happens. -/ private partial def forwardMessages (fw : FileWorker) : ServerM (Task WorkerEvent) := do let o := (←read).hOut let rec loop : ServerM WorkerEvent := do try let msg ← fw.readMessage -- Writes to Lean I/O channels are atomic, so these won't trample on each other. o.writeLspMessage msg loop catch err => -- If writeLspMessage from above errors we will block here, but the main task will -- quit eventually anyways if that happens let exitCode ← fw.proc.wait if exitCode = 0 then -- Worker was terminated fw.errorPendingRequests o ErrorCode.contentModified ("The file worker has been terminated. Either the header has changed," ++ " or the file was closed, or the server is shutting down.") return WorkerEvent.terminated else -- Worker crashed fw.errorPendingRequests o ErrorCode.internalError s!"Server process for {fw.doc.meta.uri} crashed, likely due to a stack overflow in user code." return WorkerEvent.crashed err let task ← IO.asTask (loop $ ←read) Task.Priority.dedicated task.map $ fun | Except.ok ev => ev | Except.error e => WorkerEvent.ioError e def startFileWorker (m : DocumentMeta) : ServerM Unit := do let st ← read let headerAst ← parseHeaderAst m.text.source let workerProc ← Process.spawn { toStdioConfig := workerCfg cmd := st.workerPath args := #["--worker"] ++ st.args.toArray } let pendingRequestsRef ← IO.mkRef (RBMap.empty : PendingRequestMap) -- The task will never access itself, so this is fine let fw : FileWorker := { doc := ⟨m, headerAst⟩ proc := workerProc commTask := Task.pure WorkerEvent.terminated state := WorkerState.running pendingRequestsRef := pendingRequestsRef groupedEditsRef := ← IO.mkRef none } let commTask ← forwardMessages fw let fw : FileWorker := { fw with commTask := commTask } fw.stdin.writeLspRequest ⟨0, "initialize", st.initParams⟩ fw.writeNotification { method := "textDocument/didOpen" param := { textDocument := { uri := m.uri languageId := "lean" version := m.version text := m.text.source } : DidOpenTextDocumentParams } } updateFileWorkers fw def terminateFileWorker (uri : DocumentUri) : ServerM Unit := do /- The file worker must have crashed just when we were about to terminate it! That's fine - just forget about it then. (on didClose we won't need the crashed file worker anymore, when the header changed we'll start a new one right after anyways and when we're shutting down the server it's over either way.) -/ try (←findFileWorker uri).writeMessage (Message.notification "exit" none) catch err => () eraseFileWorker uri def handleCrash (uri : DocumentUri) (queuedMsgs : Array JsonRpc.Message) : ServerM Unit := do updateFileWorkers { ←findFileWorker uri with state := WorkerState.crashed queuedMsgs } /-- Tries to write a message, sets the state of the FileWorker to `crashed` if it does not succeed and restarts the file worker if the `crashed` flag was already set. Messages that couldn't be sent can be queued up via the queueFailedMessage flag and will be discharged after the FileWorker is restarted. -/ def tryWriteMessage [Coe α JsonRpc.Message] (uri : DocumentUri) (msg : α) (writeAction : FileWorker → α → IO Unit) (queueFailedMessage := true) (restartCrashedWorker := false) : ServerM Unit := do let fw ← findFileWorker uri match fw.state with | WorkerState.crashed queuedMsgs => let mut queuedMsgs := queuedMsgs if queueFailedMessage then queuedMsgs := queuedMsgs.push msg if !restartCrashedWorker then return -- restart the crashed FileWorker eraseFileWorker uri startFileWorker fw.doc.meta let newFw ← findFileWorker uri let mut crashedMsgs := #[] -- try to discharge all queued msgs, tracking the ones that we can't discharge for msg in queuedMsgs do try newFw.writeMessage msg catch _ => crashedMsgs := crashedMsgs.push msg if ¬ crashedMsgs.isEmpty then handleCrash uri crashedMsgs | WorkerState.running => let initialQueuedMsgs := if queueFailedMessage then #[msg] else #[] try writeAction fw msg catch _ => handleCrash uri (initialQueuedMsgs.map Coe.coe) end ServerM section NotificationHandling def handleDidOpen (p : DidOpenTextDocumentParams) : ServerM Unit := let doc := p.textDocument /- NOTE(WN): `toFileMap` marks line beginnings as immediately following "\n", which should be enough to handle both LF and CRLF correctly. This is because LSP always refers to characters by (line, column), so if we get the line number correct it shouldn't matter that there is a CR there. -/ startFileWorker ⟨doc.uri, doc.version, doc.text.toFileMap⟩ def handleEdits (fw : FileWorker) : ServerM Unit := do let some ge ← fw.groupedEditsRef.modifyGet (·, none) | throwServerError "Internal error: empty grouped edits reference" let doc := ge.params.textDocument let changes := ge.params.contentChanges let oldDoc := fw.doc let some newVersion ← pure doc.version? | throwServerError "Expected version number" if newVersion <= oldDoc.meta.version then throwServerError "Got outdated version number" if changes.isEmpty then return let (newDocText, _) := foldDocumentChanges changes oldDoc.meta.text let newMeta : DocumentMeta := ⟨doc.uri, newVersion, newDocText⟩ let newHeaderAst ← parseHeaderAst newDocText.source if newHeaderAst != oldDoc.headerAst then terminateFileWorker doc.uri startFileWorker newMeta else let newDoc : OpenDocument := ⟨newMeta, oldDoc.headerAst⟩ updateFileWorkers { fw with doc := newDoc } tryWriteMessage doc.uri ⟨"textDocument/didChange", ge.params⟩ FileWorker.writeNotification (restartCrashedWorker := true) def handleDidClose (p : DidCloseTextDocumentParams) : ServerM Unit := terminateFileWorker p.textDocument.uri def handleCancelRequest (p : CancelParams) : ServerM Unit := do let fileWorkers ← (←read).fileWorkersRef.get for ⟨uri, fw⟩ in fileWorkers do let req? ← fw.pendingRequestsRef.modifyGet (fun pendingRequests => (pendingRequests.find? p.id, pendingRequests.erase p.id)) if let some req := req? then tryWriteMessage uri ⟨"$/cancelRequest", p⟩ FileWorker.writeNotification (queueFailedMessage := false) end NotificationHandling section MessageHandling def parseParams (paramType : Type) [FromJson paramType] (params : Json) : ServerM paramType := match fromJson? params with | some parsed => pure parsed | none => throwServerError s!"Got param with wrong structure: {params.compress}" def handleRequest (id : RequestID) (method : String) (params : Json) : ServerM Unit := do let handle := fun α [FromJson α] [ToJson α] [FileSource α] => do let parsedParams ← parseParams α params let uri := fileSource parsedParams tryWriteMessage uri ⟨id, method, parsedParams⟩ FileWorker.writeRequest match method with | "textDocument/waitForDiagnostics" => handle WaitForDiagnosticsParams | "textDocument/hover" => handle HoverParams | "textDocument/declaration" => handle DeclarationParams | "textDocument/definition" => handle DefinitionParams | "textDocument/typeDefinition" => handle TypeDefinitionParams | "textDocument/documentSymbol" => handle DocumentSymbolParams | _ => (←read).hOut.writeLspResponseError { id := id code := ErrorCode.methodNotFound message := s!"Unsupported request method: {method}" } def handleNotification (method : String) (params : Json) : ServerM Unit := do let handle := (fun α [FromJson α] (handler : α → ServerM Unit) => parseParams α params >>= handler) match method with | "textDocument/didOpen" => handle DidOpenTextDocumentParams handleDidOpen /- NOTE: textDocument/didChange is handled in the main loop. -/ | "textDocument/didClose" => handle DidCloseTextDocumentParams handleDidClose | "$/cancelRequest" => handle CancelParams handleCancelRequest | _ => (←read).hLog.putStrLn s!"Got unsupported notification: {method}" end MessageHandling section MainLoop def shutdown : ServerM Unit := do let fileWorkers ← (←read).fileWorkersRef.get for ⟨uri, _⟩ in fileWorkers do terminateFileWorker uri for ⟨_, fw⟩ in fileWorkers do discard <| IO.wait fw.commTask inductive ServerEvent where | workerEvent (fw : FileWorker) (ev : WorkerEvent) | clientMsg (msg : JsonRpc.Message) | clientError (e : IO.Error) def runClientTask : ServerM (Task ServerEvent) := do let st ← read let readMsgAction : IO ServerEvent := do /- Runs asynchronously. -/ let msg ← st.hIn.readLspMessage ServerEvent.clientMsg msg let clientTask := (←IO.asTask readMsgAction).map $ fun | Except.ok ev => ev | Except.error e => ServerEvent.clientError e return clientTask partial def mainLoop (clientTask : Task ServerEvent) : ServerM Unit := do let st ← read let workers ← st.fileWorkersRef.get let mut workerTasks := #[] for (_, fw) in workers do if let WorkerState.running := fw.state then workerTasks := workerTasks.push <| fw.commTask.map (ServerEvent.workerEvent fw) if let some ge ← fw.groupedEditsRef.get then workerTasks := workerTasks.push <| ge.signalTask.map (ServerEvent.workerEvent fw) let ev ← IO.waitAny (workerTasks.push clientTask |>.toList) match ev with | ServerEvent.clientMsg msg => match msg with | Message.request id "shutdown" _ => shutdown st.hOut.writeLspResponse ⟨id, Json.null⟩ | Message.request id method (some params) => handleRequest id method (toJson params) mainLoop (←runClientTask) | Message.notification "textDocument/didChange" (some params) => let p ← parseParams DidChangeTextDocumentParams (toJson params) let fw ← findFileWorker p.textDocument.uri let now ← monoMsNow /- We wait 500ms since last edit before applying the changes. -/ let applyTime := now + 500 let startingGroup? ← fw.groupedEditsRef.modifyGet fun | some ge => (false, some { ge with applyTime := applyTime params.textDocument := p.textDocument params.contentChanges := ge.params.contentChanges ++ p.contentChanges } ) | none => (true, some { applyTime := applyTime params := p /- This is overwritten just below. -/ signalTask := Task.pure WorkerEvent.processGroupedEdits } ) if startingGroup? then let t ← fw.runEditsSignalTask fw.groupedEditsRef.modify (Option.map fun ge => { ge with signalTask := t } ) mainLoop (←runClientTask) | Message.notification method (some params) => handleNotification method (toJson params) mainLoop (←runClientTask) | _ => throwServerError "Got invalid JSON-RPC message" | ServerEvent.clientError e => throw e | ServerEvent.workerEvent fw ev => match ev with | WorkerEvent.processGroupedEdits => handleEdits fw mainLoop clientTask | WorkerEvent.ioError e => throwServerError s!"IO error while processing events for {fw.doc.meta.uri}: {e}" | WorkerEvent.crashed e => handleCrash fw.doc.meta.uri #[] mainLoop clientTask | WorkerEvent.terminated => throwServerError "Internal server error: got termination event for worker that should have been removed" end MainLoop def mkLeanServerCapabilities : ServerCapabilities := { textDocumentSync? := some { openClose := true change := TextDocumentSyncKind.incremental willSave := false willSaveWaitUntil := false save? := none } hoverProvider := true declarationProvider := true definitionProvider := true typeDefinitionProvider := true documentSymbolProvider := true } def initAndRunWatchdogAux : ServerM Unit := do let st ← read try discard $ st.hIn.readLspNotificationAs "initialized" InitializedParams let clientTask ← runClientTask mainLoop clientTask let Message.notification "exit" none ← st.hIn.readLspMessage | throwServerError "Expected an exit notification" catch err => shutdown throw err def initAndRunWatchdog (args : List String) (i o e : FS.Stream) : IO Unit := do let mut workerPath ← IO.appPath if let some path := (←IO.getEnv "LEAN_SYSROOT") then workerPath := s!"{path}/bin/lean{System.FilePath.exeSuffix}" if let some path := (←IO.getEnv "LEAN_WORKER_PATH") then workerPath := path let fileWorkersRef ← IO.mkRef (RBMap.empty : FileWorkerMap) let i ← maybeTee "wdIn.txt" false i let o ← maybeTee "wdOut.txt" true o let e ← maybeTee "wdErr.txt" true e let initRequest ← i.readLspRequestAs "initialize" InitializeParams o.writeLspResponse { id := initRequest.id result := { capabilities := mkLeanServerCapabilities serverInfo? := some { name := "Lean 4 server" version? := "0.0.1" } : InitializeResult } } ReaderT.run initAndRunWatchdogAux { hIn := i hOut := o hLog := e args := args fileWorkersRef := fileWorkersRef initParams := initRequest.param workerPath := workerPath : ServerContext } @[export lean_server_watchdog_main] def watchdogMain (args : List String) : IO UInt32 := do let i ← IO.getStdin let o ← IO.getStdout let e ← IO.getStderr try initAndRunWatchdog args i o e return 0 catch err => e.putStrLn s!"Watchdog error: {err}" return 1 end Lean.Server.Watchdog
081c4befd37bf6ebb0aa1c89b88b171b944866c7
9cba98daa30c0804090f963f9024147a50292fa0
/old/src/physlib_test.lean
871ce754cbfd49c4bb854a24e588e23aa53d0879
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
1,269
lean
import .physlib2 def length : PhysDimension := PhysDimension.mk 1 0 0 def mass : PhysDimension := PhysDimension.mk 0 1 0 -- following names already defined in physlib2 def tyme : PhysDimension := PhysDimension.mk 0 0 1 def velosity : PhysDimension := PhysDimension.mk 1 0 (-1) def MetersKilogramsSeconds : MeasurementSystem := MeasurementSystem.mk lengthUnit.meter massUnit.kilogram timeUnit.second def twoMeters_in_MetersKilogramsSeconds : PhysQuantity length lengthUnit.meter massUnit.kilogram timeUnit.second := PhysQuantity.mk 2 0 0 def twoMeters_in_CentimetersKilogramsSeconds : PhysQuantity length lengthUnit.centimeter massUnit.kilogram timeUnit.second := PhysQuantity.mk 200 0 0 def threeMeters_in_MetersKilogramsSeconds : PhysQuantity length lengthUnit.meter massUnit.kilogram timeUnit.second := PhysQuantity.mk 3 0 0 -- Should work def fiveMeters := physQuantityAdd twoMeters_in_MetersKilogramsSeconds threeMeters_in_MetersKilogramsSeconds -- should not work def fiveMeters' := physQuantityAdd twoMeters_in_CentimetersKilogramsSeconds threeMeters_in_MetersKilogramsSeconds
fb60e6eb4067b4227d114248bcb167dd8f4b6acd
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/constructions/borel_space.lean
fb54f747b226f070687eea7ea69974292a9d64f0
[ "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
91,373
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, Yury Kudryashov -/ import analysis.complex.basic import analysis.normed_space.finite_dimension import measure_theory.function.ae_measurable_sequence import measure_theory.group.arithmetic import measure_theory.lattice import measure_theory.measure.open_pos import topology.algebra.order.liminf_limsup import topology.continuous_function.basic import topology.instances.ereal import topology.G_delta import topology.order.lattice import topology.semicontinuous import topology.metric_space.metrizable /-! # Borel (measurable) space ## Main definitions * `borel α` : the least `σ`-algebra that contains all open sets; * `class borel_space` : a space with `topological_space` and `measurable_space` structures such that `‹measurable_space α› = borel α`; * `class opens_measurable_space` : a space with `topological_space` and `measurable_space` structures such that all open sets are measurable; equivalently, `borel α ≤ ‹measurable_space α›`. * `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`; * `measurable` and `borel_space` instances on `ℝ`, `ℝ≥0`, `ℝ≥0∞`. ## Main statements * `is_open.measurable_set`, `is_closed.measurable_set`: open and closed sets are measurable; * `continuous.measurable` : a continuous function is measurable; * `continuous.measurable2` : if `f : α → β` and `g : α → γ` are measurable and `op : β × γ → δ` is continuous, then `λ x, op (f x, g y)` is measurable; * `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates, and similarly for `dist` and `edist`; * `ae_measurable.add` : similar dot notation for almost everywhere measurable functions; * `measurable.ennreal*` : special cases for arithmetic operations on `ℝ≥0∞`. -/ noncomputable theory open classical set filter measure_theory open_locale classical big_operators topological_space nnreal ennreal measure_theory universes u v w x y variables {α β γ γ₂ δ : Type*} {ι : Sort y} {s t u : set α} open measurable_space topological_space /-- `measurable_space` structure generated by `topological_space`. -/ def borel (α : Type u) [topological_space α] : measurable_space α := generate_from {s : set α | is_open s} lemma borel_eq_top_of_discrete [topological_space α] [discrete_topology α] : borel α = ⊤ := top_le_iff.1 $ λ s hs, generate_measurable.basic s (is_open_discrete s) lemma borel_eq_top_of_encodable [topological_space α] [t1_space α] [encodable α] : borel α = ⊤ := begin refine (top_le_iff.1 $ λ s hs, bUnion_of_singleton s ▸ _), apply measurable_set.bUnion s.countable_encodable, intros x hx, apply measurable_set.of_compl, apply generate_measurable.basic, exact is_closed_singleton.is_open_compl end lemma borel_eq_generate_from_of_subbasis {s : set (set α)} [t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) : borel α = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @measurable_set.univ α (generate_from s) }, case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂ { exact @measurable_set.inter α (generate_from s) _ _ hs₁ hs₂ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @measurable_set.sUnion α (generate_from s) _ hv (λ x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma topological_space.is_topological_basis.borel_eq_generate_from [topological_space α] [second_countable_topology α] {s : set (set α)} (hs : is_topological_basis s) : borel α = generate_from s := borel_eq_generate_from_of_subbasis hs.eq_generate_from lemma is_pi_system_is_open [topological_space α] : is_pi_system (is_open : set α → Prop) := λ s hs t ht hst, is_open.inter hs ht lemma borel_eq_generate_from_is_closed [topological_space α] : borel α = generate_from {s | is_closed s} := le_antisymm (generate_from_le $ λ t ht, @measurable_set.of_compl α _ (generate_from {s | is_closed s}) (generate_measurable.basic _ $ is_closed_compl_iff.2 ht)) (generate_from_le $ λ t ht, @measurable_set.of_compl α _ (borel α) (generate_measurable.basic _ $ is_open_compl_iff.2 ht)) section order_topology variable (α) variables [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] lemma borel_eq_generate_from_Iio : borel α = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals α _ _ _), letI : measurable_space α := measurable_space.generate_from (range Iio), have H : ∀ a : α, measurable_set (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b, { rcases h with ⟨a', ha'⟩, rw (_ : Ioi a = (Iio a')ᶜ), { exact (H _).compl }, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a < a'}, {b | a'.1 < b}) (λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Ioi a = ⋃ x : v, (Iio x.1.1)ᶜ, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply measurable_set.Union, exact λ _, (H _).compl } }, { rw forall_range_iff, intro a, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_from_Ioi : borel α = generate_from (range Ioi) := @borel_eq_generate_from_Iio αᵒᵈ _ (by apply_instance : second_countable_topology α) _ _ end order_topology lemma borel_comap {f : α → β} {t : topological_space β} : @borel α (t.induced f) = (@borel β t).comap f := comap_generate_from.symm lemma continuous.borel_measurable [topological_space α] [topological_space β] {f : α → β} (hf : continuous f) : @measurable α β (borel α) (borel β) f := measurable.of_le_map $ generate_from_le $ λ s hs, generate_measurable.basic (f ⁻¹' s) (hs.preimage hf) /-- A space with `measurable_space` and `topological_space` structures such that all open sets are measurable. -/ class opens_measurable_space (α : Type*) [topological_space α] [h : measurable_space α] : Prop := (borel_le : borel α ≤ h) /-- A space with `measurable_space` and `topological_space` structures such that the `σ`-algebra of measurable sets is exactly the `σ`-algebra generated by open sets. -/ class borel_space (α : Type*) [topological_space α] [measurable_space α] : Prop := (measurable_eq : ‹measurable_space α› = borel α) namespace tactic /-- Add instances `borel α : measurable_space α` and `⟨rfl⟩ : borel_space α`. -/ meta def add_borel_instance (α : expr) : tactic unit := do n1 ← get_unused_name "_inst", to_expr ``(borel %%α) >>= pose n1, reset_instance_cache, n2 ← get_unused_name "_inst", v ← to_expr ``(borel_space.mk rfl : borel_space %%α), note n2 none v, reset_instance_cache /-- Given a type `α`, an assumption `i : measurable_space α`, and an instance `[borel_space α]`, replace `i` with `borel α`. -/ meta def borel_to_refl (α i : expr) : tactic unit := do n ← get_unused_name "h", to_expr ``(%%i = borel %%α) >>= assert n, applyc `borel_space.measurable_eq, unfreezing (tactic.subst i), n1 ← get_unused_name "_inst", to_expr ``(borel %%α) >>= pose n1, reset_instance_cache /-- Given a type `α`, if there is an assumption `[i : measurable_space α]`, then try to prove `[borel_space α]` and replace `i` with `borel α`. Otherwise, add instances `borel α : measurable_space α` and `⟨rfl⟩ : borel_space α`. -/ meta def borelize (α : expr) : tactic unit := do i ← optional (to_expr ``(measurable_space %%α) >>= find_assumption), i.elim (add_borel_instance α) (borel_to_refl α) namespace interactive setup_tactic_parser /-- The behaviour of `borelize α` depends on the existing assumptions on `α`. - if `α` is a topological space with instances `[measurable_space α] [borel_space α]`, then `borelize α` replaces the former instance by `borel α`; - otherwise, `borelize α` adds instances `borel α : measurable_space α` and `⟨rfl⟩ : borel_space α`. Finally, `borelize [α, β, γ]` runs `borelize α, borelize β, borelize γ`. -/ meta def borelize (ts : parse pexpr_list_or_texpr) : tactic unit := mmap' (λ t, to_expr t >>= tactic.borelize) ts add_tactic_doc { name := "borelize", category := doc_category.tactic, decl_names := [`tactic.interactive.borelize], tags := ["type class"] } end interactive end tactic @[priority 100] instance order_dual.opens_measurable_space {α : Type*} [topological_space α] [measurable_space α] [h : opens_measurable_space α] : opens_measurable_space αᵒᵈ := { borel_le := h.borel_le } @[priority 100] instance order_dual.borel_space {α : Type*} [topological_space α] [measurable_space α] [h : borel_space α] : borel_space αᵒᵈ := { measurable_eq := h.measurable_eq } /-- In a `borel_space` all open sets are measurable. -/ @[priority 100] instance borel_space.opens_measurable {α : Type*} [topological_space α] [measurable_space α] [borel_space α] : opens_measurable_space α := ⟨ge_of_eq $ borel_space.measurable_eq⟩ instance subtype.borel_space {α : Type*} [topological_space α] [measurable_space α] [hα : borel_space α] (s : set α) : borel_space s := ⟨by { rw [hα.1, subtype.measurable_space, ← borel_comap], refl }⟩ instance subtype.opens_measurable_space {α : Type*} [topological_space α] [measurable_space α] [h : opens_measurable_space α] (s : set α) : opens_measurable_space s := ⟨by { rw [borel_comap], exact comap_mono h.1 }⟩ theorem _root_.measurable_set.induction_on_open [topological_space α] [measurable_space α] [borel_space α] {C : set α → Prop} (h_open : ∀ U, is_open U → C U) (h_compl : ∀ t, measurable_set t → C t → C tᶜ) (h_union : ∀ f : ℕ → set α, pairwise (disjoint on f) → (∀ i, measurable_set (f i)) → (∀ i, C (f i)) → C (⋃ i, f i)) : ∀ ⦃t⦄, measurable_set t → C t := measurable_space.induction_on_inter borel_space.measurable_eq is_pi_system_is_open (h_open _ is_open_empty) h_open h_compl h_union section variables [topological_space α] [measurable_space α] [opens_measurable_space α] [topological_space β] [measurable_space β] [opens_measurable_space β] [topological_space γ] [measurable_space γ] [borel_space γ] [topological_space γ₂] [measurable_space γ₂] [borel_space γ₂] [measurable_space δ] lemma is_open.measurable_set (h : is_open s) : measurable_set s := opens_measurable_space.borel_le _ $ generate_measurable.basic _ h @[measurability] lemma measurable_set_interior : measurable_set (interior s) := is_open_interior.measurable_set lemma is_Gδ.measurable_set (h : is_Gδ s) : measurable_set s := begin rcases h with ⟨S, hSo, hSc, rfl⟩, exact measurable_set.sInter hSc (λ t ht, (hSo t ht).measurable_set) end lemma measurable_set_of_continuous_at {β} [emetric_space β] (f : α → β) : measurable_set {x | continuous_at f x} := (is_Gδ_set_of_continuous_at f).measurable_set lemma is_closed.measurable_set (h : is_closed s) : measurable_set s := h.is_open_compl.measurable_set.of_compl lemma is_compact.measurable_set [t2_space α] (h : is_compact s) : measurable_set s := h.is_closed.measurable_set @[measurability] lemma measurable_set_closure : measurable_set (closure s) := is_closed_closure.measurable_set lemma measurable_of_is_open {f : δ → γ} (hf : ∀ s, is_open s → measurable_set (f ⁻¹' s)) : measurable f := by { rw [‹borel_space γ›.measurable_eq], exact measurable_generate_from hf } lemma measurable_of_is_closed {f : δ → γ} (hf : ∀ s, is_closed s → measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_open, intros s hs, rw [← measurable_set.compl_iff, ← preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs end lemma measurable_of_is_closed' {f : δ → γ} (hf : ∀ s, is_closed s → s.nonempty → s ≠ univ → measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_closed, intros s hs, cases eq_empty_or_nonempty s with h1 h1, { simp [h1] }, by_cases h2 : s = univ, { simp [h2] }, exact hf s hs h1 h2 end instance nhds_is_measurably_generated (a : α) : (𝓝 a).is_measurably_generated := begin rw [nhds, infi_subtype'], refine @filter.infi_is_measurably_generated _ _ _ _ (λ i, _), exact i.2.2.measurable_set.principal_is_measurably_generated end /-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for each `a`. This cannot be an `instance` because it depends on a non-instance `hs : measurable_set s`. -/ lemma measurable_set.nhds_within_is_measurably_generated {s : set α} (hs : measurable_set s) (a : α) : (𝓝[s] a).is_measurably_generated := by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _ @[priority 100] -- see Note [lower instance priority] instance opens_measurable_space.to_measurable_singleton_class [t1_space α] : measurable_singleton_class α := ⟨λ x, is_closed_singleton.measurable_set⟩ instance pi.opens_measurable_space_encodable {ι : Type*} {π : ι → Type*} [encodable ι] [t' : Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)] [∀ i, opens_measurable_space (π i)] : opens_measurable_space (Π i, π i) := begin constructor, have : Pi.topological_space = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ countable_basis (π a)) ∧ t = pi ↑i s}, { rw [funext (λ a, @eq_generate_from_countable_basis (π a) _ _), pi_generate_from_eq] }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨s, i, hi, rfl⟩, refine measurable_set.pi i.countable_to_set (λ a ha, is_open.measurable_set _), rw [eq_generate_from_countable_basis (π a)], exact generate_open.basic _ (hi a ha) end instance pi.opens_measurable_space_fintype {ι : Type*} {π : ι → Type*} [fintype ι] [t' : Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)] [∀ i, opens_measurable_space (π i)] : opens_measurable_space (Π i, π i) := by { letI := fintype.to_encodable ι, apply_instance } instance prod.opens_measurable_space [second_countable_topology α] [second_countable_topology β] : opens_measurable_space (α × β) := begin constructor, rw [((is_basis_countable_basis α).prod (is_basis_countable_basis β)).borel_eq_generate_from], apply generate_from_le, rintros _ ⟨u, v, hu, hv, rfl⟩, exact (is_open_of_mem_countable_basis hu).measurable_set.prod (is_open_of_mem_countable_basis hv).measurable_set end variables {α' : Type*} [topological_space α'] [measurable_space α'] lemma interior_ae_eq_of_null_frontier {μ : measure α'} {s : set α'} (h : μ (frontier s) = 0) : interior s =ᵐ[μ] s := interior_subset.eventually_le.antisymm $ subset_closure.eventually_le.trans (ae_le_set.2 h) lemma measure_interior_of_null_frontier {μ : measure α'} {s : set α'} (h : μ (frontier s) = 0) : μ (interior s) = μ s := measure_congr (interior_ae_eq_of_null_frontier h) lemma null_measurable_set_of_null_frontier {s : set α} {μ : measure α} (h : μ (frontier s) = 0) : null_measurable_set s μ := ⟨interior s, is_open_interior.measurable_set, (interior_ae_eq_of_null_frontier h).symm⟩ lemma closure_ae_eq_of_null_frontier {μ : measure α'} {s : set α'} (h : μ (frontier s) = 0) : closure s =ᵐ[μ] s := ((ae_le_set.2 h).trans interior_subset.eventually_le).antisymm $ subset_closure.eventually_le lemma measure_closure_of_null_frontier {μ : measure α'} {s : set α'} (h : μ (frontier s) = 0) : μ (closure s) = μ s := measure_congr (closure_ae_eq_of_null_frontier h) section preorder variables [preorder α] [order_closed_topology α] {a b x : α} @[simp, measurability] lemma measurable_set_Ici : measurable_set (Ici a) := is_closed_Ici.measurable_set @[simp, measurability] lemma measurable_set_Iic : measurable_set (Iic a) := is_closed_Iic.measurable_set @[simp, measurability] lemma measurable_set_Icc : measurable_set (Icc a b) := is_closed_Icc.measurable_set instance nhds_within_Ici_is_measurably_generated : (𝓝[Ici b] a).is_measurably_generated := measurable_set_Ici.nhds_within_is_measurably_generated _ instance nhds_within_Iic_is_measurably_generated : (𝓝[Iic b] a).is_measurably_generated := measurable_set_Iic.nhds_within_is_measurably_generated _ instance nhds_within_Icc_is_measurably_generated : is_measurably_generated (𝓝[Icc a b] x) := by { rw [← Ici_inter_Iic, nhds_within_inter], apply_instance } instance at_top_is_measurably_generated : (filter.at_top : filter α).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ λ a, (measurable_set_Ici : measurable_set (Ici a)).principal_is_measurably_generated instance at_bot_is_measurably_generated : (filter.at_bot : filter α).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ λ a, (measurable_set_Iic : measurable_set (Iic a)).principal_is_measurably_generated end preorder section partial_order variables [partial_order α] [order_closed_topology α] [second_countable_topology α] {a b : α} @[measurability] lemma measurable_set_le' : measurable_set {p : α × α | p.1 ≤ p.2} := order_closed_topology.is_closed_le'.measurable_set @[measurability] lemma measurable_set_le {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a ≤ g a} := hf.prod_mk hg measurable_set_le' end partial_order section linear_order variables [linear_order α] [order_closed_topology α] {a b x : α} -- we open this locale only here to avoid issues with list being treated as intervals above open_locale interval @[simp, measurability] lemma measurable_set_Iio : measurable_set (Iio a) := is_open_Iio.measurable_set @[simp, measurability] lemma measurable_set_Ioi : measurable_set (Ioi a) := is_open_Ioi.measurable_set @[simp, measurability] lemma measurable_set_Ioo : measurable_set (Ioo a b) := is_open_Ioo.measurable_set @[simp, measurability] lemma measurable_set_Ioc : measurable_set (Ioc a b) := measurable_set_Ioi.inter measurable_set_Iic @[simp, measurability] lemma measurable_set_Ico : measurable_set (Ico a b) := measurable_set_Ici.inter measurable_set_Iio instance nhds_within_Ioi_is_measurably_generated : (𝓝[Ioi b] a).is_measurably_generated := measurable_set_Ioi.nhds_within_is_measurably_generated _ instance nhds_within_Iio_is_measurably_generated : (𝓝[Iio b] a).is_measurably_generated := measurable_set_Iio.nhds_within_is_measurably_generated _ instance nhds_within_interval_is_measurably_generated : is_measurably_generated (𝓝[[a, b]] x) := nhds_within_Icc_is_measurably_generated @[measurability] lemma measurable_set_lt' [second_countable_topology α] : measurable_set {p : α × α | p.1 < p.2} := (is_open_lt continuous_fst continuous_snd).measurable_set @[measurability] lemma measurable_set_lt [second_countable_topology α] {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a < g a} := hf.prod_mk hg measurable_set_lt' lemma set.ord_connected.measurable_set (h : ord_connected s) : measurable_set s := begin let u := ⋃ (x ∈ s) (y ∈ s), Ioo x y, have huopen : is_open u := is_open_bUnion (λ x hx, is_open_bUnion (λ y hy, is_open_Ioo)), have humeas : measurable_set u := huopen.measurable_set, have hfinite : (s \ u).finite, { refine set.finite_of_forall_between_eq_endpoints (s \ u) (λ x hx y hy z hz hxy hyz, _), by_contra' h, exact hy.2 (mem_Union₂.mpr ⟨x, hx.1, mem_Union₂.mpr ⟨z, hz.1, lt_of_le_of_ne hxy h.1, lt_of_le_of_ne hyz h.2⟩⟩) }, have : u ⊆ s := Union₂_subset (λ x hx, Union₂_subset (λ y hy, Ioo_subset_Icc_self.trans (h.out hx hy))), rw ← union_diff_cancel this, exact humeas.union hfinite.measurable_set end lemma is_preconnected.measurable_set (h : is_preconnected s) : measurable_set s := h.ord_connected.measurable_set lemma generate_from_Ico_mem_le_borel {α : Type*} [topological_space α] [linear_order α] [order_closed_topology α] (s t : set α) : measurable_space.generate_from {S | ∃ (l ∈ s) (u ∈ t) (h : l < u), Ico l u = S} ≤ borel α := begin apply generate_from_le, borelize α, rintro _ ⟨a, -, b, -, -, rfl⟩, exact measurable_set_Ico end lemma dense.borel_eq_generate_from_Ico_mem_aux {α : Type*} [topological_space α] [linear_order α] [order_topology α] [second_countable_topology α] {s : set α} (hd : dense s) (hbot : ∀ x, is_bot x → x ∈ s) (hIoo : ∀ x y : α, x < y → Ioo x y = ∅ → y ∈ s) : borel α = generate_from {S : set α | ∃ (l ∈ s) (u ∈ s) (h : l < u), Ico l u = S} := begin set S : set (set α) := {S | ∃ (l ∈ s) (u ∈ s) (h : l < u), Ico l u = S}, refine le_antisymm _ (generate_from_Ico_mem_le_borel _ _), letI : measurable_space α := generate_from S, rw borel_eq_generate_from_Iio, refine generate_from_le (forall_range_iff.2 $ λ a, _), rcases hd.exists_countable_dense_subset_bot_top with ⟨t, hts, hc, htd, htb, htt⟩, by_cases ha : ∀ b < a, (Ioo b a).nonempty, { convert_to measurable_set (⋃ (l ∈ t) (u ∈ t) (hlu : l < u) (hu : u ≤ a), Ico l u), { ext y, simp only [mem_Union, mem_Iio, mem_Ico], split, { intro hy, rcases htd.exists_le' (λ b hb, htb _ hb (hbot b hb)) y with ⟨l, hlt, hly⟩, rcases htd.exists_mem_open is_open_Ioo (ha y hy) with ⟨u, hut, hyu, hua⟩, exact ⟨l, hlt, u, hut, hly.trans_lt hyu, hua.le, hly, hyu⟩ }, { rintro ⟨l, -, u, -, -, hua, -, hyu⟩, exact hyu.trans_le hua } }, { refine measurable_set.bUnion hc (λ a ha, measurable_set.bUnion hc $ λ b hb, _), refine measurable_set.Union_Prop (λ hab, measurable_set.Union_Prop $ λ hb', _), exact generate_measurable.basic _ ⟨a, hts ha, b, hts hb, hab, mem_singleton _⟩ } }, { simp only [not_forall, not_nonempty_iff_eq_empty] at ha, replace ha : a ∈ s := hIoo ha.some a ha.some_spec.fst ha.some_spec.snd, convert_to measurable_set (⋃ (l ∈ t) (hl : l < a), Ico l a), { symmetry, simp only [← Ici_inter_Iio, ← Union_inter, inter_eq_right_iff_subset, subset_def, mem_Union, mem_Ici, mem_Iio], intros x hx, rcases htd.exists_le' (λ b hb, htb _ hb (hbot b hb)) x with ⟨z, hzt, hzx⟩, exact ⟨z, hzt, hzx.trans_lt hx, hzx⟩ }, { refine measurable_set.bUnion hc (λ x hx, measurable_set.Union_Prop $ λ hlt, _), exact generate_measurable.basic _ ⟨x, hts hx, a, ha, hlt, mem_singleton _⟩ } } end lemma dense.borel_eq_generate_from_Ico_mem {α : Type*} [topological_space α] [linear_order α] [order_topology α] [second_countable_topology α] [densely_ordered α] [no_min_order α] {s : set α} (hd : dense s) : borel α = generate_from {S : set α | ∃ (l ∈ s) (u ∈ s) (h : l < u), Ico l u = S} := hd.borel_eq_generate_from_Ico_mem_aux (by simp) $ λ x y hxy H, ((nonempty_Ioo.2 hxy).ne_empty H).elim lemma borel_eq_generate_from_Ico (α : Type*) [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] : borel α = generate_from {S : set α | ∃ l u (h : l < u), Ico l u = S} := by simpa only [exists_prop, mem_univ, true_and] using (@dense_univ α _).borel_eq_generate_from_Ico_mem_aux (λ _ _, mem_univ _) (λ _ _ _ _, mem_univ _) lemma dense.borel_eq_generate_from_Ioc_mem_aux {α : Type*} [topological_space α] [linear_order α] [order_topology α] [second_countable_topology α] {s : set α} (hd : dense s) (hbot : ∀ x, is_top x → x ∈ s) (hIoo : ∀ x y : α, x < y → Ioo x y = ∅ → x ∈ s) : borel α = generate_from {S : set α | ∃ (l ∈ s) (u ∈ s) (h : l < u), Ioc l u = S} := begin convert hd.order_dual.borel_eq_generate_from_Ico_mem_aux hbot (λ x y hlt he, hIoo y x hlt _), { ext s, split; rintro ⟨l, hl, u, hu, hlt, rfl⟩, exacts [⟨u, hu, l, hl, hlt, dual_Ico⟩, ⟨u, hu, l, hl, hlt, dual_Ioc⟩] }, { erw dual_Ioo, exact he } end lemma dense.borel_eq_generate_from_Ioc_mem {α : Type*} [topological_space α] [linear_order α] [order_topology α] [second_countable_topology α] [densely_ordered α] [no_max_order α] {s : set α} (hd : dense s) : borel α = generate_from {S : set α | ∃ (l ∈ s) (u ∈ s) (h : l < u), Ioc l u = S} := hd.borel_eq_generate_from_Ioc_mem_aux (by simp) $ λ x y hxy H, ((nonempty_Ioo.2 hxy).ne_empty H).elim lemma borel_eq_generate_from_Ioc (α : Type*) [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] : borel α = generate_from {S : set α | ∃ l u (h : l < u), Ioc l u = S} := by simpa only [exists_prop, mem_univ, true_and] using (@dense_univ α _).borel_eq_generate_from_Ioc_mem_aux (λ _ _, mem_univ _) (λ _ _ _ _, mem_univ _) namespace measure_theory.measure /-- Two finite measures on a Borel space are equal if they agree on all closed-open intervals. If `α` is a conditionally complete linear order with no top element, `measure_theory.measure..ext_of_Ico` is an extensionality lemma with weaker assumptions on `μ` and `ν`. -/ lemma ext_of_Ico_finite {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [linear_order α] [order_topology α] [borel_space α] (μ ν : measure α) [is_finite_measure μ] (hμν : μ univ = ν univ) (h : ∀ ⦃a b⦄, a < b → μ (Ico a b) = ν (Ico a b)) : μ = ν := begin refine ext_of_generate_finite _ (borel_space.measurable_eq.trans (borel_eq_generate_from_Ico α)) (is_pi_system_Ico (id : α → α) id) _ hμν, { rintro - ⟨a, b, hlt, rfl⟩, exact h hlt } end /-- Two finite measures on a Borel space are equal if they agree on all open-closed intervals. If `α` is a conditionally complete linear order with no top element, `measure_theory.measure..ext_of_Ioc` is an extensionality lemma with weaker assumptions on `μ` and `ν`. -/ lemma ext_of_Ioc_finite {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [linear_order α] [order_topology α] [borel_space α] (μ ν : measure α) [is_finite_measure μ] (hμν : μ univ = ν univ) (h : ∀ ⦃a b⦄, a < b → μ (Ioc a b) = ν (Ioc a b)) : μ = ν := begin refine @ext_of_Ico_finite αᵒᵈ _ _ _ _ _ ‹_› μ ν _ hμν (λ a b hab, _), erw dual_Ico, exact h hab end /-- Two measures which are finite on closed-open intervals are equal if the agree on all closed-open intervals. -/ lemma ext_of_Ico' {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [linear_order α] [order_topology α] [borel_space α] [no_max_order α] (μ ν : measure α) (hμ : ∀ ⦃a b⦄, a < b → μ (Ico a b) ≠ ∞) (h : ∀ ⦃a b⦄, a < b → μ (Ico a b) = ν (Ico a b)) : μ = ν := begin rcases exists_countable_dense_bot_top α with ⟨s, hsc, hsd, hsb, hst⟩, have : countable (⋃ (l ∈ s) (u ∈ s) (h : l < u), {Ico l u} : set (set α)), from hsc.bUnion (λ l hl, hsc.bUnion (λ u hu, countable_Union_Prop $ λ _, countable_singleton _)), simp only [← set_of_eq_eq_singleton, ← set_of_exists] at this, refine measure.ext_of_generate_from_of_cover_subset (borel_space.measurable_eq.trans (borel_eq_generate_from_Ico α)) (is_pi_system_Ico id id) _ this _ _ _, { rintro _ ⟨l, -, u, -, h, rfl⟩, exact ⟨l, u, h, rfl⟩ }, { refine sUnion_eq_univ_iff.2 (λ x, _), rcases hsd.exists_le' hsb x with ⟨l, hls, hlx⟩, rcases hsd.exists_gt x with ⟨u, hus, hxu⟩, exact ⟨_, ⟨l, hls, u, hus, hlx.trans_lt hxu, rfl⟩, hlx, hxu⟩ }, { rintro _ ⟨l, -, u, -, hlt, rfl⟩, exact hμ hlt }, { rintro _ ⟨l, u, hlt, rfl⟩, exact h hlt } end /-- Two measures which are finite on closed-open intervals are equal if the agree on all open-closed intervals. -/ lemma ext_of_Ioc' {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [linear_order α] [order_topology α] [borel_space α] [no_min_order α] (μ ν : measure α) (hμ : ∀ ⦃a b⦄, a < b → μ (Ioc a b) ≠ ∞) (h : ∀ ⦃a b⦄, a < b → μ (Ioc a b) = ν (Ioc a b)) : μ = ν := begin refine @ext_of_Ico' αᵒᵈ _ _ _ _ _ ‹_› _ μ ν _ _; intros a b hab; erw dual_Ico, exacts [hμ hab, h hab] end /-- Two measures which are finite on closed-open intervals are equal if the agree on all closed-open intervals. -/ lemma ext_of_Ico {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [conditionally_complete_linear_order α] [order_topology α] [borel_space α] [no_max_order α] (μ ν : measure α) [is_locally_finite_measure μ] (h : ∀ ⦃a b⦄, a < b → μ (Ico a b) = ν (Ico a b)) : μ = ν := μ.ext_of_Ico' ν (λ a b hab, measure_Ico_lt_top.ne) h /-- Two measures which are finite on closed-open intervals are equal if the agree on all open-closed intervals. -/ lemma ext_of_Ioc {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [conditionally_complete_linear_order α] [order_topology α] [borel_space α] [no_min_order α] (μ ν : measure α) [is_locally_finite_measure μ] (h : ∀ ⦃a b⦄, a < b → μ (Ioc a b) = ν (Ioc a b)) : μ = ν := μ.ext_of_Ioc' ν (λ a b hab, measure_Ioc_lt_top.ne) h /-- Two finite measures on a Borel space are equal if they agree on all left-infinite right-closed intervals. -/ lemma ext_of_Iic {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [linear_order α] [order_topology α] [borel_space α] (μ ν : measure α) [is_finite_measure μ] (h : ∀ a, μ (Iic a) = ν (Iic a)) : μ = ν := begin refine ext_of_Ioc_finite μ ν _ (λ a b hlt, _), { rcases exists_countable_dense_bot_top α with ⟨s, hsc, hsd, -, hst⟩, have : directed_on (≤) s, from directed_on_iff_directed.2 (directed_of_sup $ λ _ _, id), simp only [← bsupr_measure_Iic hsc (hsd.exists_ge' hst) this, h] }, rw [← Iic_diff_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) measurable_set_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) measurable_set_Iic, h a, h b], { rw ← h a, exact (measure_lt_top μ _).ne }, { exact (measure_lt_top μ _).ne } end /-- Two finite measures on a Borel space are equal if they agree on all left-closed right-infinite intervals. -/ lemma ext_of_Ici {α : Type*} [topological_space α] {m : measurable_space α} [second_countable_topology α] [linear_order α] [order_topology α] [borel_space α] (μ ν : measure α) [is_finite_measure μ] (h : ∀ a, μ (Ici a) = ν (Ici a)) : μ = ν := @ext_of_Iic αᵒᵈ _ _ _ _ _ ‹_› _ _ _ h end measure_theory.measure end linear_order section linear_order variables [linear_order α] [order_closed_topology α] @[measurability] lemma measurable_set_interval {a b : α} : measurable_set (interval a b) := measurable_set_Icc @[measurability] lemma measurable_set_interval_oc {a b : α} : measurable_set (interval_oc a b) := measurable_set_Ioc variables [second_countable_topology α] @[measurability] lemma measurable.max {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ a, max (f a) (g a)) := by simpa only [max_def] using hf.piecewise (measurable_set_le hg hf) hg @[measurability] lemma ae_measurable.max {f g : δ → α} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, max (f a) (g a)) μ := ⟨λ a, max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk, eventually_eq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩ @[measurability] lemma measurable.min {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ a, min (f a) (g a)) := by simpa only [min_def] using hf.piecewise (measurable_set_le hf hg) hg @[measurability] lemma ae_measurable.min {f g : δ → α} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, min (f a) (g a)) μ := ⟨λ a, min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk, eventually_eq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩ end linear_order /-- A continuous function from an `opens_measurable_space` to a `borel_space` is measurable. -/ lemma continuous.measurable {f : α → γ} (hf : continuous f) : measurable f := hf.borel_measurable.mono opens_measurable_space.borel_le (le_of_eq $ borel_space.measurable_eq) /-- A continuous function from an `opens_measurable_space` to a `borel_space` is ae-measurable. -/ lemma continuous.ae_measurable {f : α → γ} (h : continuous f) {μ : measure α} : ae_measurable f μ := h.measurable.ae_measurable lemma closed_embedding.measurable {f : α → γ} (hf : closed_embedding f) : measurable f := hf.continuous.measurable lemma continuous.is_open_pos_measure_map {f : β → γ} (hf : continuous f) (hf_surj : function.surjective f) {μ : measure β} [μ.is_open_pos_measure] : (measure.map f μ).is_open_pos_measure := begin refine ⟨λ U hUo hUne, _⟩, rw [measure.map_apply hf.measurable hUo.measurable_set], exact (hUo.preimage hf).measure_ne_zero μ (hf_surj.nonempty_preimage.mpr hUne) end /-- If a function is defined piecewise in terms of functions which are continuous on their respective pieces, then it is measurable. -/ lemma continuous_on.measurable_piecewise {f g : α → γ} {s : set α} [Π (j : α), decidable (j ∈ s)] (hf : continuous_on f s) (hg : continuous_on g sᶜ) (hs : measurable_set s) : measurable (s.piecewise f g) := begin refine measurable_of_is_open (λ t ht, _), rw [piecewise_preimage, set.ite], apply measurable_set.union, { rcases _root_.continuous_on_iff'.1 hf t ht with ⟨u, u_open, hu⟩, rw hu, exact u_open.measurable_set.inter hs }, { rcases _root_.continuous_on_iff'.1 hg t ht with ⟨u, u_open, hu⟩, rw [diff_eq_compl_inter, inter_comm, hu], exact u_open.measurable_set.inter hs.compl } end @[priority 100, to_additive] instance has_continuous_mul.has_measurable_mul [has_mul γ] [has_continuous_mul γ] : has_measurable_mul γ := { measurable_const_mul := λ c, (continuous_const.mul continuous_id).measurable, measurable_mul_const := λ c, (continuous_id.mul continuous_const).measurable } @[priority 100] instance has_continuous_sub.has_measurable_sub [has_sub γ] [has_continuous_sub γ] : has_measurable_sub γ := { measurable_const_sub := λ c, (continuous_const.sub continuous_id).measurable, measurable_sub_const := λ c, (continuous_id.sub continuous_const).measurable } @[priority 100, to_additive] instance topological_group.has_measurable_inv [group γ] [topological_group γ] : has_measurable_inv γ := ⟨continuous_inv.measurable⟩ @[priority 100] instance has_continuous_smul.has_measurable_smul {M α} [topological_space M] [topological_space α] [measurable_space M] [measurable_space α] [opens_measurable_space M] [borel_space α] [has_scalar M α] [has_continuous_smul M α] : has_measurable_smul M α := ⟨λ c, (continuous_const_smul _).measurable, λ y, (continuous_id.smul continuous_const).measurable⟩ section lattice @[priority 100] instance has_continuous_sup.has_measurable_sup [has_sup γ] [has_continuous_sup γ] : has_measurable_sup γ := { measurable_const_sup := λ c, (continuous_const.sup continuous_id).measurable, measurable_sup_const := λ c, (continuous_id.sup continuous_const).measurable } @[priority 100] instance has_continuous_sup.has_measurable_sup₂ [second_countable_topology γ] [has_sup γ] [has_continuous_sup γ] : has_measurable_sup₂ γ := ⟨continuous_sup.measurable⟩ @[priority 100] instance has_continuous_inf.has_measurable_inf [has_inf γ] [has_continuous_inf γ] : has_measurable_inf γ := { measurable_const_inf := λ c, (continuous_const.inf continuous_id).measurable, measurable_inf_const := λ c, (continuous_id.inf continuous_const).measurable } @[priority 100] instance has_continuous_inf.has_measurable_inf₂ [second_countable_topology γ] [has_inf γ] [has_continuous_inf γ] : has_measurable_inf₂ γ := ⟨continuous_inf.measurable⟩ end lattice section homeomorph @[measurability] protected lemma homeomorph.measurable (h : α ≃ₜ γ) : measurable h := h.continuous.measurable /-- A homeomorphism between two Borel spaces is a measurable equivalence.-/ def homeomorph.to_measurable_equiv (h : γ ≃ₜ γ₂) : γ ≃ᵐ γ₂ := { measurable_to_fun := h.measurable, measurable_inv_fun := h.symm.measurable, to_equiv := h.to_equiv } @[simp] lemma homeomorph.to_measurable_equiv_coe (h : γ ≃ₜ γ₂) : (h.to_measurable_equiv : γ → γ₂) = h := rfl @[simp] lemma homeomorph.to_measurable_equiv_symm_coe (h : γ ≃ₜ γ₂) : (h.to_measurable_equiv.symm : γ₂ → γ) = h.symm := rfl end homeomorph @[measurability] lemma continuous_map.measurable (f : C(α, γ)) : measurable f := f.continuous.measurable lemma measurable_of_continuous_on_compl_singleton [t1_space α] {f : α → γ} (a : α) (hf : continuous_on f {a}ᶜ) : measurable f := measurable_of_measurable_on_compl_singleton a (continuous_on_iff_continuous_restrict.1 hf).measurable lemma continuous.measurable2 [second_countable_topology α] [second_countable_topology β] {f : δ → α} {g : δ → β} {c : α → β → γ} (h : continuous (λ p : α × β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (λ a, c (f a) (g a)) := h.measurable.comp (hf.prod_mk hg) lemma continuous.ae_measurable2 [second_countable_topology α] [second_countable_topology β] {f : δ → α} {g : δ → β} {c : α → β → γ} {μ : measure δ} (h : continuous (λ p : α × β, c p.1 p.2)) (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, c (f a) (g a)) μ := h.measurable.comp_ae_measurable (hf.prod_mk hg) @[priority 100] instance has_continuous_inv₀.has_measurable_inv [group_with_zero γ] [t1_space γ] [has_continuous_inv₀ γ] : has_measurable_inv γ := ⟨measurable_of_continuous_on_compl_singleton 0 continuous_on_inv₀⟩ @[priority 100, to_additive] instance has_continuous_mul.has_measurable_mul₂ [second_countable_topology γ] [has_mul γ] [has_continuous_mul γ] : has_measurable_mul₂ γ := ⟨continuous_mul.measurable⟩ @[priority 100] instance has_continuous_sub.has_measurable_sub₂ [second_countable_topology γ] [has_sub γ] [has_continuous_sub γ] : has_measurable_sub₂ γ := ⟨continuous_sub.measurable⟩ @[priority 100] instance has_continuous_smul.has_measurable_smul₂ {M α} [topological_space M] [second_countable_topology M] [measurable_space M] [opens_measurable_space M] [topological_space α] [second_countable_topology α] [measurable_space α] [borel_space α] [has_scalar M α] [has_continuous_smul M α] : has_measurable_smul₂ M α := ⟨continuous_smul.measurable⟩ end section borel_space variables [topological_space α] [measurable_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] [topological_space γ] [measurable_space γ] [borel_space γ] [measurable_space δ] lemma pi_le_borel_pi {ι : Type*} {π : ι → Type*} [Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, borel_space (π i)] : measurable_space.pi ≤ borel (Π i, π i) := begin have : ‹Π i, measurable_space (π i)› = λ i, borel (π i) := funext (λ i, borel_space.measurable_eq), rw [this], exact supr_le (λ i, comap_le_iff_le_map.2 $ (continuous_apply i).borel_measurable) end lemma prod_le_borel_prod : prod.measurable_space ≤ borel (α × β) := begin rw [‹borel_space α›.measurable_eq, ‹borel_space β›.measurable_eq], refine sup_le _ _, { exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable }, { exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable } end instance pi.borel_space_fintype_encodable {ι : Type*} {π : ι → Type*} [encodable ι] [t' : Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)] [∀ i, borel_space (π i)] : borel_space (Π i, π i) := ⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩ instance pi.borel_space_fintype {ι : Type*} {π : ι → Type*} [fintype ι] [t' : Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)] [∀ i, borel_space (π i)] : borel_space (Π i, π i) := ⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩ instance prod.borel_space [second_countable_topology α] [second_countable_topology β] : borel_space (α × β) := ⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩ protected lemma embedding.measurable_embedding {f : α → β} (h₁ : embedding f) (h₂ : measurable_set (range f)) : measurable_embedding f := show measurable_embedding (coe ∘ (homeomorph.of_embedding f h₁).to_measurable_equiv), from (measurable_embedding.subtype_coe h₂).comp (measurable_equiv.measurable_embedding _) protected lemma closed_embedding.measurable_embedding {f : α → β} (h : closed_embedding f) : measurable_embedding f := h.to_embedding.measurable_embedding h.closed_range.measurable_set protected lemma open_embedding.measurable_embedding {f : α → β} (h : open_embedding f) : measurable_embedding f := h.to_embedding.measurable_embedding h.open_range.measurable_set section linear_order variables [linear_order α] [order_topology α] [second_countable_topology α] lemma measurable_of_Iio {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Iio x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_from_Iio _), rintro _ ⟨x, rfl⟩, exact hf x end lemma upper_semicontinuous.measurable [topological_space δ] [opens_measurable_space δ] {f : δ → α} (hf : upper_semicontinuous f) : measurable f := measurable_of_Iio (λ y, (hf.is_open_preimage y).measurable_set) lemma measurable_of_Ioi {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Ioi x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_from_Ioi _), rintro _ ⟨x, rfl⟩, exact hf x end lemma lower_semicontinuous.measurable [topological_space δ] [opens_measurable_space δ] {f : δ → α} (hf : lower_semicontinuous f) : measurable f := measurable_of_Ioi (λ y, (hf.is_open_preimage y).measurable_set) lemma measurable_of_Iic {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Iic x)) : measurable f := begin apply measurable_of_Ioi, simp_rw [← compl_Iic, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable_of_Ici {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Ici x)) : measurable f := begin apply measurable_of_Iio, simp_rw [← compl_Ici, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable.is_lub {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) : measurable g := begin change ∀ b, is_lub (range $ λ i, f i b) (g b) at hg, rw [‹borel_space α›.measurable_eq, borel_eq_generate_from_Ioi α], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (λ i, hf i (is_open_lt' _).measurable_set) end private lemma ae_measurable.is_lub_of_nonempty {ι} (hι : nonempty ι) {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_lub {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin let p : δ → (ι → α) → Prop := λ x f', is_lub {a | ∃ i, f' i = a} (g x), let g_seq := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty α).some, have hg_seq : ∀ b, is_lub {a | ∃ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hα : nonempty α := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : α | ∃ (i : ι), (hf i).mk (f i) b = a} = {a : α | ∃ (i : ι), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : α | ∃ (i : ι), hα.some = a} = {hα.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, λ hx, ⟨hι.some, hx.symm⟩⟩, }, rw h_singleton, exact is_lub_singleton, }, }, refine ⟨g_seq, measurable.is_lub (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨g x⟩ : nonempty α).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_lub {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_lub {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin by_cases hμ : μ = 0, { rw hμ, exact ae_measurable_zero_measure }, haveI : μ.ae.ne_bot, { simpa [ne_bot_iff] }, by_cases hι : nonempty ι, { exact ae_measurable.is_lub_of_nonempty hι hf hg, }, suffices : ∃ x, g =ᵐ[μ] λ y, g x, by { exact ⟨(λ y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : ∀ x, {a : α | ∃ (i : ι), f i x = a} = ∅, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact λ hi, hι (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (λ y hy, is_lub.unique hy hg.exists.some_spec)⟩, end lemma measurable.is_glb {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) : measurable g := begin change ∀ b, is_glb (range $ λ i, f i b) (g b) at hg, rw [‹borel_space α›.measurable_eq, borel_eq_generate_from_Iio α], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (λ i, hf i (is_open_gt' _).measurable_set) end private lemma ae_measurable.is_glb_of_nonempty {ι} (hι : nonempty ι) {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_glb {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin let p : δ → (ι → α) → Prop := λ x f', is_glb {a | ∃ i, f' i = a} (g x), let g_seq := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty α).some, have hg_seq : ∀ b, is_glb {a | ∃ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hα : nonempty α := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : α | ∃ (i : ι), (hf i).mk (f i) b = a} = {a : α | ∃ (i : ι), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : α | ∃ (i : ι), hα.some = a} = {hα.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, λ hx, ⟨hι.some, hx.symm⟩⟩, }, rw h_singleton, exact is_glb_singleton, }, }, refine ⟨g_seq, measurable.is_glb (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨g x⟩ : nonempty α).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_glb {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_glb {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin by_cases hμ : μ = 0, { rw hμ, exact ae_measurable_zero_measure }, haveI : μ.ae.ne_bot, { simpa [ne_bot_iff] }, by_cases hι : nonempty ι, { exact ae_measurable.is_glb_of_nonempty hι hf hg, }, suffices : ∃ x, g =ᵐ[μ] λ y, g x, by { exact ⟨(λ y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : ∀ x, {a : α | ∃ (i : ι), f i x = a} = ∅, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact λ hi, hι (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (λ y hy, is_glb.unique hy hg.exists.some_spec)⟩, end protected lemma monotone.measurable [linear_order β] [order_closed_topology β] {f : β → α} (hf : monotone f) : measurable f := suffices h : ∀ x, ord_connected (f ⁻¹' Ioi x), from measurable_of_Ioi (λ x, (h x).measurable_set), λ x, ord_connected_def.mpr (λ a ha b hb c hc, lt_of_lt_of_le ha (hf hc.1)) lemma ae_measurable_restrict_of_monotone_on [linear_order β] [order_closed_topology β] {μ : measure β} {s : set β} (hs : measurable_set s) {f : β → α} (hf : monotone_on f s) : ae_measurable f (μ.restrict s) := have this : monotone (f ∘ coe : s → α), from λ ⟨x, hx⟩ ⟨y, hy⟩ (hxy : x ≤ y), hf hx hy hxy, ae_measurable_restrict_of_measurable_subtype hs this.measurable protected lemma antitone.measurable [linear_order β] [order_closed_topology β] {f : β → α} (hf : antitone f) : measurable f := @monotone.measurable αᵒᵈ β _ _ ‹_› _ _ _ _ _ ‹_› _ _ _ hf lemma ae_measurable_restrict_of_antitone_on [linear_order β] [order_closed_topology β] {μ : measure β} {s : set β} (hs : measurable_set s) {f : β → α} (hf : antitone_on f s) : ae_measurable f (μ.restrict s) := @ae_measurable_restrict_of_monotone_on αᵒᵈ β _ _ ‹_› _ _ _ _ _ ‹_› _ _ _ _ hs _ hf end linear_order @[measurability] lemma measurable.supr_Prop {α} [measurable_space α] [complete_lattice α] (p : Prop) {f : δ → α} (hf : measurable f) : measurable (λ b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end) @[measurability] lemma measurable.infi_Prop {α} [measurable_space α] [complete_lattice α] (p : Prop) {f : δ → α} (hf : measurable f) : measurable (λ b, ⨅ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end) section complete_linear_order variables [complete_linear_order α] [order_topology α] [second_countable_topology α] @[measurability] lemma measurable_supr {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i, f i b) := measurable.is_lub hf $ λ b, is_lub_supr @[measurability] lemma ae_measurable_supr {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨆ i, f i b) μ := ae_measurable.is_lub hf $ (ae_of_all μ (λ b, is_lub_supr)) @[measurability] lemma measurable_infi {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i, f i b) := measurable.is_glb hf $ λ b, is_glb_infi @[measurability] lemma ae_measurable_infi {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨅ i, f i b) μ := ae_measurable.is_glb hf $ (ae_of_all μ (λ b, is_glb_infi)) lemma measurable_bsupr {ι} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact measurable_supr (λ i, hf i) } lemma ae_measurable_bsupr {ι} {μ : measure δ} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨆ i ∈ s, f i b) μ := begin haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact ae_measurable_supr (λ i, hf i), end lemma measurable_binfi {ι} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact measurable_infi (λ i, hf i) } lemma ae_measurable_binfi {ι} {μ : measure δ} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨅ i ∈ s, f i b) μ := begin haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact ae_measurable_infi (λ i, hf i), end /-- `liminf` over a general filter is measurable. See `measurable_liminf` for the version over `ℕ`. -/ lemma measurable_liminf' {ι ι'} {f : ι → δ → α} {u : filter ι} (hf : ∀ i, measurable (f i)) {p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable (λ x, liminf u (λ i, f i x)) := begin simp_rw [hu.to_has_basis.liminf_eq_supr_infi], refine measurable_bsupr _ hu.countable _, exact λ i, measurable_binfi _ (hs i) hf end /-- `limsup` over a general filter is measurable. See `measurable_limsup` for the version over `ℕ`. -/ lemma measurable_limsup' {ι ι'} {f : ι → δ → α} {u : filter ι} (hf : ∀ i, measurable (f i)) {p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable (λ x, limsup u (λ i, f i x)) := begin simp_rw [hu.to_has_basis.limsup_eq_infi_supr], refine measurable_binfi _ hu.countable _, exact λ i, measurable_bsupr _ (hs i) hf end /-- `liminf` over `ℕ` is measurable. See `measurable_liminf'` for a version with a general filter. -/ @[measurability] lemma measurable_liminf {f : ℕ → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ x, liminf at_top (λ i, f i x)) := measurable_liminf' hf at_top_countable_basis (λ i, countable_encodable _) /-- `limsup` over `ℕ` is measurable. See `measurable_limsup'` for a version with a general filter. -/ @[measurability] lemma measurable_limsup {f : ℕ → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ x, limsup at_top (λ i, f i x)) := measurable_limsup' hf at_top_countable_basis (λ i, countable_encodable _) end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] [order_topology α] [second_countable_topology α] lemma measurable_cSup {ι} {f : ι → δ → α} {s : set ι} (hs : s.countable) (hf : ∀ i, measurable (f i)) (bdd : ∀ x, bdd_above ((λ i, f i x) '' s)) : measurable (λ x, Sup ((λ i, f i x) '' s)) := begin cases eq_empty_or_nonempty s with h2s h2s, { simp [h2s, measurable_const] }, { apply measurable_of_Iic, intro y, simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall], exact measurable_set.bInter hs (λ i hi, measurable_set_le (hf i) measurable_const) } end end conditionally_complete_linear_order /-- Convert a `homeomorph` to a `measurable_equiv`. -/ def homemorph.to_measurable_equiv (h : α ≃ₜ β) : α ≃ᵐ β := { to_equiv := h.to_equiv, measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable } protected lemma is_finite_measure_on_compacts.map {α : Type*} {m0 : measurable_space α} [topological_space α] [opens_measurable_space α] {β : Type*} [measurable_space β] [topological_space β] [borel_space β] [t2_space β] (μ : measure α) [is_finite_measure_on_compacts μ] (f : α ≃ₜ β) : is_finite_measure_on_compacts (measure.map f μ) := ⟨begin assume K hK, rw [measure.map_apply f.measurable hK.measurable_set], apply is_compact.measure_lt_top, rwa f.compact_preimage end⟩ end borel_space instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩ instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩ instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩ instance nat.borel_space : borel_space ℕ := ⟨borel_eq_top_of_discrete.symm⟩ instance int.borel_space : borel_space ℤ := ⟨borel_eq_top_of_discrete.symm⟩ instance rat.borel_space : borel_space ℚ := ⟨borel_eq_top_of_encodable.symm⟩ @[priority 900] instance is_R_or_C.measurable_space {𝕜 : Type*} [is_R_or_C 𝕜] : measurable_space 𝕜 := borel 𝕜 @[priority 900] instance is_R_or_C.borel_space {𝕜 : Type*} [is_R_or_C 𝕜] : borel_space 𝕜 := ⟨rfl⟩ /- Instances on `real` and `complex` are special cases of `is_R_or_C` but without these instances, Lean fails to prove `borel_space (ι → ℝ)`, so we leave them here. -/ instance real.measurable_space : measurable_space ℝ := borel ℝ instance real.borel_space : borel_space ℝ := ⟨rfl⟩ instance nnreal.measurable_space : measurable_space ℝ≥0 := subtype.measurable_space instance nnreal.borel_space : borel_space ℝ≥0 := subtype.borel_space _ instance ennreal.measurable_space : measurable_space ℝ≥0∞ := borel ℝ≥0∞ instance ennreal.borel_space : borel_space ℝ≥0∞ := ⟨rfl⟩ instance ereal.measurable_space : measurable_space ereal := borel ereal instance ereal.borel_space : borel_space ereal := ⟨rfl⟩ instance complex.measurable_space : measurable_space ℂ := borel ℂ instance complex.borel_space : borel_space ℂ := ⟨rfl⟩ /-- One can cut out `ℝ≥0∞` into the sets `{0}`, `Ico (t^n) (t^(n+1))` for `n : ℤ` and `{∞}`. This gives a way to compute the measure of a set in terms of sets on which a given function `f` does not fluctuate by more than `t`. -/ lemma measure_eq_measure_preimage_add_measure_tsum_Ico_zpow [measurable_space α] (μ : measure α) {f : α → ℝ≥0∞} (hf : measurable f) {s : set α} (hs : measurable_set s) {t : ℝ≥0} (ht : 1 < t) : μ s = μ (s ∩ f⁻¹' {0}) + μ (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), μ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) := begin have A : μ s = μ (s ∩ f⁻¹' {0}) + μ (s ∩ f⁻¹' (Ioi 0)), { rw ← measure_union, { congr' 1, ext x, have : 0 = f x ∨ 0 < f x := eq_or_lt_of_le bot_le, rw eq_comm at this, simp only [←and_or_distrib_left, this, mem_singleton_iff, mem_inter_eq, and_true, mem_union_eq, mem_Ioi, mem_preimage], }, { apply disjoint_left.2 (λ x hx h'x, _), have : 0 < f x := h'x.2, exact lt_irrefl 0 (this.trans_le hx.2.le) }, { exact hs.inter (hf measurable_set_Ioi) } }, have B : μ (s ∩ f⁻¹' (Ioi 0)) = μ (s ∩ f⁻¹' {∞}) + μ (s ∩ f⁻¹' (Ioo 0 ∞)), { rw ← measure_union, { rw ← inter_union_distrib_left, congr, ext x, simp only [mem_singleton_iff, mem_union_eq, mem_Ioo, mem_Ioi, mem_preimage], have H : f x = ∞ ∨ f x < ∞ := eq_or_lt_of_le le_top, cases H, { simp only [H, eq_self_iff_true, or_false, with_top.zero_lt_top, not_top_lt, and_false] }, { simp only [H, H.ne, and_true, false_or] } }, { apply disjoint_left.2 (λ x hx h'x, _), have : f x < ∞ := h'x.2.2, exact lt_irrefl _ (this.trans_le (le_of_eq hx.2.symm)) }, { exact hs.inter (hf measurable_set_Ioo) } }, have C : μ (s ∩ f⁻¹' (Ioo 0 ∞)) = ∑' (n : ℤ), μ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))), { rw [← measure_Union, ennreal.Ioo_zero_top_eq_Union_Ico_zpow (ennreal.one_lt_coe_iff.2 ht) ennreal.coe_ne_top, preimage_Union, inter_Union], { assume i j, simp only [function.on_fun], wlog h : i ≤ j := le_total i j using [i j, j i] tactic.skip, { assume hij, replace hij : i + 1 ≤ j := lt_of_le_of_ne h hij, apply disjoint_left.2 (λ x hx h'x, lt_irrefl (f x) _), calc f x < t ^ (i + 1) : hx.2.2 ... ≤ t ^ j : ennreal.zpow_le_of_le (ennreal.one_le_coe_iff.2 ht.le) hij ... ≤ f x : h'x.2.1 }, { assume hij, rw disjoint.comm, exact this hij.symm } }, { assume n, exact hs.inter (hf measurable_set_Ico) } }, rw [A, B, C, add_assoc], end section metric_space variables [metric_space α] [measurable_space α] [opens_measurable_space α] variables [measurable_space β] {x : α} {ε : ℝ} open metric @[measurability] lemma measurable_set_ball : measurable_set (metric.ball x ε) := metric.is_open_ball.measurable_set @[measurability] lemma measurable_set_closed_ball : measurable_set (metric.closed_ball x ε) := metric.is_closed_ball.measurable_set @[measurability] lemma measurable_inf_dist {s : set α} : measurable (λ x, inf_dist x s) := (continuous_inf_dist_pt s).measurable @[measurability] lemma measurable.inf_dist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_dist (f x) s) := measurable_inf_dist.comp hf @[measurability] lemma measurable_inf_nndist {s : set α} : measurable (λ x, inf_nndist x s) := (continuous_inf_nndist_pt s).measurable @[measurability] lemma measurable.inf_nndist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_nndist (f x) s) := measurable_inf_nndist.comp hf section variables [second_countable_topology α] @[measurability] lemma measurable_dist : measurable (λ p : α × α, dist p.1 p.2) := continuous_dist.measurable @[measurability] lemma measurable.dist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, dist (f b) (g b)) := (@continuous_dist α _).measurable2 hf hg @[measurability] lemma measurable_nndist : measurable (λ p : α × α, nndist p.1 p.2) := continuous_nndist.measurable @[measurability] lemma measurable.nndist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, nndist (f b) (g b)) := (@continuous_nndist α _).measurable2 hf hg end /-- If a set has a closed thickening with finite measure, then the measure of its `r`-closed thickenings converges to the measure of its closure as `r` tends to `0`. -/ lemma tendsto_measure_cthickening {μ : measure α} {s : set α} (hs : ∃ R > 0, μ (cthickening R s) ≠ ∞) : tendsto (λ r, μ (cthickening r s)) (𝓝 0) (𝓝 (μ (closure s))) := begin have A : tendsto (λ r, μ (cthickening r s)) (𝓝[Ioi 0] 0) (𝓝 (μ (closure s))), { rw closure_eq_Inter_cthickening, exact tendsto_measure_bInter_gt (λ r hr, is_closed_cthickening.measurable_set) (λ i j ipos ij, cthickening_mono ij _) hs }, have B : tendsto (λ r, μ (cthickening r s)) (𝓝[Iic 0] 0) (𝓝 (μ (closure s))), { apply tendsto.congr' _ tendsto_const_nhds, filter_upwards [self_mem_nhds_within] with _ hr, rw cthickening_of_nonpos hr, }, convert B.sup A, exact (nhds_left_sup_nhds_right' 0).symm, end /-- If a closed set has a closed thickening with finite measure, then the measure of its `r`-closed thickenings converges to its measure as `r` tends to `0`. -/ lemma tendsto_measure_cthickening_of_is_closed {μ : measure α} {s : set α} (hs : ∃ R > 0, μ (cthickening R s) ≠ ∞) (h's : is_closed s) : tendsto (λ r, μ (cthickening r s)) (𝓝 0) (𝓝 (μ s)) := begin convert tendsto_measure_cthickening hs, exact h's.closure_eq.symm end /-- Given a compact set in a proper space, the measure of its `r`-closed thickenings converges to its measure as `r` tends to `0`. -/ lemma tendsto_measure_cthickening_of_is_compact [proper_space α] {μ : measure α} [is_finite_measure_on_compacts μ] {s : set α} (hs : is_compact s) : tendsto (λ r, μ (cthickening r s)) (𝓝 0) (𝓝 (μ s)) := tendsto_measure_cthickening_of_is_closed ⟨1, zero_lt_one, (bounded.measure_lt_top hs.bounded.cthickening).ne⟩ hs.is_closed end metric_space section emetric_space variables [emetric_space α] [measurable_space α] [opens_measurable_space α] variables [measurable_space β] {x : α} {ε : ℝ≥0∞} open emetric @[measurability] lemma measurable_set_eball : measurable_set (emetric.ball x ε) := emetric.is_open_ball.measurable_set @[measurability] lemma measurable_edist_right : measurable (edist x) := (continuous_const.edist continuous_id).measurable @[measurability] lemma measurable_edist_left : measurable (λ y, edist y x) := (continuous_id.edist continuous_const).measurable @[measurability] lemma measurable_inf_edist {s : set α} : measurable (λ x, inf_edist x s) := continuous_inf_edist.measurable @[measurability] lemma measurable.inf_edist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_edist (f x) s) := measurable_inf_edist.comp hf variables [second_countable_topology α] @[measurability] lemma measurable_edist : measurable (λ p : α × α, edist p.1 p.2) := continuous_edist.measurable @[measurability] lemma measurable.edist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, edist (f b) (g b)) := (@continuous_edist α _).measurable2 hf hg @[measurability] lemma ae_measurable.edist {f g : β → α} {μ : measure β} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, edist (f a) (g a)) μ := (@continuous_edist α _).ae_measurable2 hf hg end emetric_space namespace real open measurable_space measure_theory lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := is_topological_basis_Ioo_rat.borel_eq_generate_from lemma is_pi_system_Ioo_rat : @is_pi_system ℝ (⋃ (a b : ℚ) (h : a < b), {Ioo a b}) := begin convert is_pi_system_Ioo (coe : ℚ → ℝ) (coe : ℚ → ℝ), ext x, simp [eq_comm] end /-- The intervals `(-(n + 1), (n + 1))` form a finite spanning sets in the set of open intervals with rational endpoints for a locally finite measure `μ` on `ℝ`. -/ def finite_spanning_sets_in_Ioo_rat (μ : measure ℝ) [is_locally_finite_measure μ] : μ.finite_spanning_sets_in (⋃ (a b : ℚ) (h : a < b), {Ioo a b}) := { set := λ n, Ioo (-(n + 1)) (n + 1), set_mem := λ n, begin simp only [mem_Union, mem_singleton_iff], refine ⟨-(n + 1), n + 1, _, by norm_cast⟩, exact (neg_nonpos.2 (@nat.cast_nonneg ℚ _ (n + 1))).trans_lt n.cast_add_one_pos end, finite := λ n, measure_Ioo_lt_top, spanning := Union_eq_univ_iff.2 $ λ x, ⟨⌊|x|⌋₊, neg_lt.1 ((neg_le_abs_self x).trans_lt (nat.lt_floor_add_one _)), (le_abs_self x).trans_lt (nat.lt_floor_add_one _)⟩ } lemma measure_ext_Ioo_rat {μ ν : measure ℝ} [is_locally_finite_measure μ] (h : ∀ a b : ℚ, μ (Ioo a b) = ν (Ioo a b)) : μ = ν := (finite_spanning_sets_in_Ioo_rat μ).ext borel_eq_generate_from_Ioo_rat is_pi_system_Ioo_rat $ by { simp only [mem_Union, mem_singleton_iff], rintro _ ⟨a, b, -, rfl⟩, apply h } lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃ a : ℚ, {Iio a}) := begin let g : measurable_space ℝ := generate_from (⋃ a : ℚ, {Iio a}), refine le_antisymm _ _, { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (λ t, _), simp only [mem_Union, mem_singleton_iff], rintro ⟨a, b, h, rfl⟩, rw (set.ext (λ x, _) : Ioo (a : ℝ) b = (⋃c>a, (Iio c)ᶜ) ∩ Iio b), { have hg : ∀ q : ℚ, g.measurable_set' (Iio q) := λ q, generate_measurable.basic (Iio q) (by simp), refine @measurable_set.inter _ g _ _ _ (hg _), refine @measurable_set.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _), exact @measurable_set.compl _ _ g (hg _) }, { suffices : x < ↑b → (↑a < x ↔ ∃ (i : ℚ), a < i ∧ ↑i ≤ x), by simpa, refine λ _, ⟨λ h, _, λ ⟨i, hai, hix⟩, (rat.cast_lt.2 hai).trans_le hix⟩, rcases exists_rat_btwn h with ⟨c, ac, cx⟩, exact ⟨c, rat.cast_lt.1 ac, cx.le⟩ } }, { refine measurable_space.generate_from_le (λ _, _), simp only [mem_Union, mem_singleton_iff], rintro ⟨r, rfl⟩, exact measurable_set_Iio } end end real variable [measurable_space α] @[measurability] lemma measurable_real_to_nnreal : measurable (real.to_nnreal) := continuous_real_to_nnreal.measurable @[measurability] lemma measurable.real_to_nnreal {f : α → ℝ} (hf : measurable f) : measurable (λ x, real.to_nnreal (f x)) := measurable_real_to_nnreal.comp hf @[measurability] lemma ae_measurable.real_to_nnreal {f : α → ℝ} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, real.to_nnreal (f x)) μ := measurable_real_to_nnreal.comp_ae_measurable hf @[measurability] lemma measurable_coe_nnreal_real : measurable (coe : ℝ≥0 → ℝ) := nnreal.continuous_coe.measurable @[measurability] lemma measurable.coe_nnreal_real {f : α → ℝ≥0} (hf : measurable f) : measurable (λ x, (f x : ℝ)) := measurable_coe_nnreal_real.comp hf @[measurability] lemma ae_measurable.coe_nnreal_real {f : α → ℝ≥0} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x : ℝ)) μ := measurable_coe_nnreal_real.comp_ae_measurable hf @[measurability] lemma measurable_coe_nnreal_ennreal : measurable (coe : ℝ≥0 → ℝ≥0∞) := ennreal.continuous_coe.measurable @[measurability] lemma measurable.coe_nnreal_ennreal {f : α → ℝ≥0} (hf : measurable f) : measurable (λ x, (f x : ℝ≥0∞)) := ennreal.continuous_coe.measurable.comp hf @[measurability] lemma ae_measurable.coe_nnreal_ennreal {f : α → ℝ≥0} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x : ℝ≥0∞)) μ := ennreal.continuous_coe.measurable.comp_ae_measurable hf @[measurability] lemma measurable.ennreal_of_real {f : α → ℝ} (hf : measurable f) : measurable (λ x, ennreal.of_real (f x)) := ennreal.continuous_of_real.measurable.comp hf /-- The set of finite `ℝ≥0∞` numbers is `measurable_equiv` to `ℝ≥0`. -/ def measurable_equiv.ennreal_equiv_nnreal : {r : ℝ≥0∞ | r ≠ ∞} ≃ᵐ ℝ≥0 := ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv namespace ennreal lemma measurable_of_measurable_nnreal {f : ℝ≥0∞ → α} (h : measurable (λ p : ℝ≥0, f p)) : measurable f := measurable_of_measurable_on_compl_singleton ∞ (measurable_equiv.ennreal_equiv_nnreal.symm.measurable_comp_iff.1 h) /-- `ℝ≥0∞` is `measurable_equiv` to `ℝ≥0 ⊕ unit`. -/ def ennreal_equiv_sum : ℝ≥0∞ ≃ᵐ ℝ≥0 ⊕ unit := { measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe_nnreal_ennreal (@measurable_const ℝ≥0∞ unit _ _ ∞), .. equiv.option_equiv_sum_punit ℝ≥0 } open function (uncurry) lemma measurable_of_measurable_nnreal_prod [measurable_space β] [measurable_space γ] {f : ℝ≥0∞ × β → γ} (H₁ : measurable (λ p : ℝ≥0 × β, f (p.1, p.2))) (H₂ : measurable (λ x, f (∞, x))) : measurable f := let e : ℝ≥0∞ × β ≃ᵐ ℝ≥0 × β ⊕ unit × β := (ennreal_equiv_sum.prod_congr (measurable_equiv.refl β)).trans (measurable_equiv.sum_prod_distrib _ _ _) in e.symm.measurable_comp_iff.1 $ measurable_sum H₁ (H₂.comp measurable_id.snd) lemma measurable_of_measurable_nnreal_nnreal [measurable_space β] {f : ℝ≥0∞ × ℝ≥0∞ → β} (h₁ : measurable (λ p : ℝ≥0 × ℝ≥0, f (p.1, p.2))) (h₂ : measurable (λ r : ℝ≥0, f (∞, r))) (h₃ : measurable (λ r : ℝ≥0, f (r, ∞))) : measurable f := measurable_of_measurable_nnreal_prod (measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (h₁.comp measurable_swap) h₃) (measurable_of_measurable_nnreal h₂) @[measurability] lemma measurable_of_real : measurable ennreal.of_real := ennreal.continuous_of_real.measurable @[measurability] lemma measurable_to_real : measurable ennreal.to_real := ennreal.measurable_of_measurable_nnreal measurable_coe_nnreal_real @[measurability] lemma measurable_to_nnreal : measurable ennreal.to_nnreal := ennreal.measurable_of_measurable_nnreal measurable_id instance : has_measurable_mul₂ ℝ≥0∞ := begin refine ⟨measurable_of_measurable_nnreal_nnreal _ _ _⟩, { simp only [← ennreal.coe_mul, measurable_mul.coe_nnreal_ennreal] }, { simp only [ennreal.top_mul, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const }, { simp only [ennreal.mul_top, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const } end instance : has_measurable_sub₂ ℝ≥0∞ := ⟨by apply measurable_of_measurable_nnreal_nnreal; simp [← with_top.coe_sub, continuous_sub.measurable.coe_nnreal_ennreal]⟩ instance : has_measurable_inv ℝ≥0∞ := ⟨continuous_inv.measurable⟩ end ennreal @[measurability] lemma measurable.ennreal_to_nnreal {f : α → ℝ≥0∞} (hf : measurable f) : measurable (λ x, (f x).to_nnreal) := ennreal.measurable_to_nnreal.comp hf @[measurability] lemma ae_measurable.ennreal_to_nnreal {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x).to_nnreal) μ := ennreal.measurable_to_nnreal.comp_ae_measurable hf lemma measurable_coe_nnreal_ennreal_iff {f : α → ℝ≥0} : measurable (λ x, (f x : ℝ≥0∞)) ↔ measurable f := ⟨λ h, h.ennreal_to_nnreal, λ h, h.coe_nnreal_ennreal⟩ @[measurability] lemma measurable.ennreal_to_real {f : α → ℝ≥0∞} (hf : measurable f) : measurable (λ x, ennreal.to_real (f x)) := ennreal.measurable_to_real.comp hf @[measurability] lemma ae_measurable.ennreal_to_real {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, ennreal.to_real (f x)) μ := ennreal.measurable_to_real.comp_ae_measurable hf /-- note: `ℝ≥0∞` can probably be generalized in a future version of this lemma. -/ @[measurability] lemma measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) : measurable (λ x, ∑' i, f i x) := by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr, exact λ s, s.measurable_sum (λ i _, h i) } @[measurability] lemma measurable.ennreal_tsum' {ι} [encodable ι] {f : ι → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) : measurable (∑' i, f i) := begin convert measurable.ennreal_tsum h, ext1 x, exact tsum_apply (pi.summable.2 (λ _, ennreal.summable)), end @[measurability] lemma measurable.nnreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0} (h : ∀ i, measurable (f i)) : measurable (λ x, ∑' i, f i x) := begin simp_rw [nnreal.tsum_eq_to_nnreal_tsum], exact (measurable.ennreal_tsum (λ i, (h i).coe_nnreal_ennreal)).ennreal_to_nnreal, end @[measurability] lemma ae_measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0∞} {μ : measure α} (h : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ x, ∑' i, f i x) μ := by { simp_rw [ennreal.tsum_eq_supr_sum], apply ae_measurable_supr, exact λ s, finset.ae_measurable_sum s (λ i _, h i) } @[measurability] lemma measurable_coe_real_ereal : measurable (coe : ℝ → ereal) := continuous_coe_real_ereal.measurable @[measurability] lemma measurable.coe_real_ereal {f : α → ℝ} (hf : measurable f) : measurable (λ x, (f x : ereal)) := measurable_coe_real_ereal.comp hf @[measurability] lemma ae_measurable.coe_real_ereal {f : α → ℝ} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x : ereal)) μ := measurable_coe_real_ereal.comp_ae_measurable hf /-- The set of finite `ereal` numbers is `measurable_equiv` to `ℝ`. -/ def measurable_equiv.ereal_equiv_real : ({⊥, ⊤} : set ereal).compl ≃ᵐ ℝ := ereal.ne_bot_top_homeomorph_real.to_measurable_equiv lemma ereal.measurable_of_measurable_real {f : ereal → α} (h : measurable (λ p : ℝ, f p)) : measurable f := measurable_of_measurable_on_compl_finite {⊥, ⊤} (by simp) (measurable_equiv.ereal_equiv_real.symm.measurable_comp_iff.1 h) @[measurability] lemma measurable_ereal_to_real : measurable ereal.to_real := ereal.measurable_of_measurable_real (by simpa using measurable_id) @[measurability] lemma measurable.ereal_to_real {f : α → ereal} (hf : measurable f) : measurable (λ x, (f x).to_real) := measurable_ereal_to_real.comp hf @[measurability] lemma ae_measurable.ereal_to_real {f : α → ereal} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x).to_real) μ := measurable_ereal_to_real.comp_ae_measurable hf @[measurability] lemma measurable_coe_ennreal_ereal : measurable (coe : ℝ≥0∞ → ereal) := continuous_coe_ennreal_ereal.measurable @[measurability] lemma measurable.coe_ereal_ennreal {f : α → ℝ≥0∞} (hf : measurable f) : measurable (λ x, (f x : ereal)) := measurable_coe_ennreal_ereal.comp hf @[measurability] lemma ae_measurable.coe_ereal_ennreal {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x : ereal)) μ := measurable_coe_ennreal_ereal.comp_ae_measurable hf section normed_group variables [normed_group α] [opens_measurable_space α] [measurable_space β] @[measurability] lemma measurable_norm : measurable (norm : α → ℝ) := continuous_norm.measurable @[measurability] lemma measurable.norm {f : β → α} (hf : measurable f) : measurable (λ a, norm (f a)) := measurable_norm.comp hf @[measurability] lemma ae_measurable.norm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) : ae_measurable (λ a, norm (f a)) μ := measurable_norm.comp_ae_measurable hf @[measurability] lemma measurable_nnnorm : measurable (nnnorm : α → ℝ≥0) := continuous_nnnorm.measurable @[measurability] lemma measurable.nnnorm {f : β → α} (hf : measurable f) : measurable (λ a, ∥f a∥₊) := measurable_nnnorm.comp hf @[measurability] lemma ae_measurable.nnnorm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) : ae_measurable (λ a, ∥f a∥₊) μ := measurable_nnnorm.comp_ae_measurable hf @[measurability] lemma measurable_ennnorm : measurable (λ x : α, (∥x∥₊ : ℝ≥0∞)) := measurable_nnnorm.coe_nnreal_ennreal @[measurability] lemma measurable.ennnorm {f : β → α} (hf : measurable f) : measurable (λ a, (∥f a∥₊ : ℝ≥0∞)) := hf.nnnorm.coe_nnreal_ennreal @[measurability] lemma ae_measurable.ennnorm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) : ae_measurable (λ a, (∥f a∥₊ : ℝ≥0∞)) μ := measurable_ennnorm.comp_ae_measurable hf end normed_group section limits variables [measurable_space β] [metric_space β] [borel_space β] open metric /-- A limit (over a general filter) of measurable `ℝ≥0∞` valued functions is measurable. -/ lemma measurable_of_tendsto_ennreal' {ι} {f : ι → α → ℝ≥0∞} {g : α → ℝ≥0∞} (u : filter ι) [ne_bot u] [is_countably_generated u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) : measurable g := begin rcases u.exists_seq_tendsto with ⟨x, hx⟩, rw [tendsto_pi_nhds] at lim, have : (λ y, liminf at_top (λ n, (f (x n) y : ℝ≥0∞))) = g := by { ext1 y, exact ((lim y).comp hx).liminf_eq, }, rw ← this, show measurable (λ y, liminf at_top (λ n, (f (x n) y : ℝ≥0∞))), exact measurable_liminf (λ n, hf (x n)), end /-- A sequential limit of measurable `ℝ≥0∞` valued functions is measurable. -/ lemma measurable_of_tendsto_ennreal {f : ℕ → α → ℝ≥0∞} {g : α → ℝ≥0∞} (hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_ennreal' at_top hf lim /-- A limit (over a general filter) of measurable `ℝ≥0` valued functions is measurable. -/ lemma measurable_of_tendsto_nnreal' {ι} {f : ι → α → ℝ≥0} {g : α → ℝ≥0} (u : filter ι) [ne_bot u] [is_countably_generated u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) : measurable g := begin simp_rw [← measurable_coe_nnreal_ennreal_iff] at hf ⊢, refine measurable_of_tendsto_ennreal' u hf _, rw tendsto_pi_nhds at lim ⊢, exact λ x, (ennreal.continuous_coe.tendsto (g x)).comp (lim x), end /-- A sequential limit of measurable `ℝ≥0` valued functions is measurable. -/ lemma measurable_of_tendsto_nnreal {f : ℕ → α → ℝ≥0} {g : α → ℝ≥0} (hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_nnreal' at_top hf lim /-- A limit (over a general filter) of measurable functions valued in a metric space is measurable. -/ lemma measurable_of_tendsto_metric' {ι} {f : ι → α → β} {g : α → β} (u : filter ι) [ne_bot u] [is_countably_generated u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) : measurable g := begin apply measurable_of_is_closed', intros s h1s h2s h3s, have : measurable (λ x, inf_nndist (g x) s), { suffices : tendsto (λ i x, inf_nndist (f i x) s) u (𝓝 (λ x, inf_nndist (g x) s)), from measurable_of_tendsto_nnreal' u (λ i, (hf i).inf_nndist) this, rw [tendsto_pi_nhds] at lim ⊢, intro x, exact ((continuous_inf_nndist_pt s).tendsto (g x)).comp (lim x) }, have h4s : g ⁻¹' s = (λ x, inf_nndist (g x) s) ⁻¹' {0}, { ext x, simp [h1s, ← h1s.mem_iff_inf_dist_zero h2s, ← nnreal.coe_eq_zero] }, rw [h4s], exact this (measurable_set_singleton 0), end /-- A sequential limit of measurable functions valued in a metric space is measurable. -/ lemma measurable_of_tendsto_metric {f : ℕ → α → β} {g : α → β} (hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_metric' at_top hf lim /-- A limit (over a general filter) of measurable functions valued in a metrizable space is measurable. -/ lemma measurable_of_tendsto_metrizable' {β : Type*} [topological_space β] [metrizable_space β] [measurable_space β] [borel_space β] {ι} {f : ι → α → β} {g : α → β} (u : filter ι) [ne_bot u] [is_countably_generated u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) : measurable g := begin letI : metric_space β := metrizable_space_metric β, exact measurable_of_tendsto_metric' u hf lim end /-- A sequential limit of measurable functions valued in a metrizable space is measurable. -/ lemma measurable_of_tendsto_metrizable {β : Type*} [topological_space β] [metrizable_space β] [measurable_space β] [borel_space β] {f : ℕ → α → β} {g : α → β} (hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_metrizable' at_top hf lim lemma ae_measurable_of_tendsto_metric_ae {ι : Type*} {μ : measure α} {f : ι → α → β} {g : α → β} (u : filter ι) [hu : ne_bot u] [is_countably_generated u] (hf : ∀ n, ae_measurable (f n) μ) (h_tendsto : ∀ᵐ x ∂μ, tendsto (λ n, f n x) u (𝓝 (g x))) : ae_measurable g μ := begin rcases u.exists_seq_tendsto with ⟨v, hv⟩, have h'f : ∀ n, ae_measurable (f (v n)) μ := λ n, hf (v n), set p : α → (ℕ → β) → Prop := λ x f', tendsto (λ n, f' n) at_top (𝓝 (g x)), have hp : ∀ᵐ x ∂μ, p x (λ n, f (v n) x), by filter_upwards [h_tendsto] with x hx using hx.comp hv, set ae_seq_lim := λ x, ite (x ∈ ae_seq_set h'f p) (g x) (⟨f (v 0) x⟩ : nonempty β).some with hs, refine ⟨ae_seq_lim, measurable_of_tendsto_metric' at_top (@ae_seq.measurable α β _ _ _ (λ n x, f (v n) x) μ h'f p) (tendsto_pi_nhds.mpr (λ x, _)), _⟩, { simp_rw [ae_seq, ae_seq_lim], split_ifs with hx, { simp_rw ae_seq.mk_eq_fun_of_mem_ae_seq_set h'f hx, exact @ae_seq.fun_prop_of_mem_ae_seq_set α β _ _ _ _ _ _ h'f x hx, }, { exact tendsto_const_nhds } }, { exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨f (v 0) x⟩ : nonempty β).some) (ae_seq_set h'f p) (ae_seq.measure_compl_ae_seq_set_eq_zero h'f hp)).symm }, end lemma ae_measurable_of_tendsto_metric_ae' {μ : measure α} {f : ℕ → α → β} {g : α → β} (hf : ∀ n, ae_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x))) : ae_measurable g μ := ae_measurable_of_tendsto_metric_ae at_top hf h_ae_tendsto lemma ae_measurable_of_unif_approx {μ : measure α} {g : α → β} (hf : ∀ ε > (0 : ℝ), ∃ (f : α → β), ae_measurable f μ ∧ ∀ᵐ x ∂μ, dist (f x) (g x) ≤ ε) : ae_measurable g μ := begin obtain ⟨u, u_anti, u_pos, u_lim⟩ : ∃ (u : ℕ → ℝ), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), choose f Hf using λ (n : ℕ), hf (u n) (u_pos n), have : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x)), { have : ∀ᵐ x ∂ μ, ∀ n, dist (f n x) (g x) ≤ u n := ae_all_iff.2 (λ n, (Hf n).2), filter_upwards [this], assume x hx, rw tendsto_iff_dist_tendsto_zero, exact squeeze_zero (λ n, dist_nonneg) hx u_lim }, exact ae_measurable_of_tendsto_metric_ae' (λ n, (Hf n).1) this, end lemma measurable_of_tendsto_metric_ae {μ : measure α} [μ.is_complete] {f : ℕ → α → β} {g : α → β} (hf : ∀ n, measurable (f n)) (h_ae_tendsto : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x))) : measurable g := ae_measurable_iff_measurable.mp (ae_measurable_of_tendsto_metric_ae' (λ i, (hf i).ae_measurable) h_ae_tendsto) lemma measurable_limit_of_tendsto_metric_ae {ι} [encodable ι] [nonempty ι] {μ : measure α} {f : ι → α → β} {L : filter ι} [L.is_countably_generated] (hf : ∀ n, ae_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, ∃ l : β, tendsto (λ n, f n x) L (𝓝 l)) : ∃ (f_lim : α → β) (hf_lim_meas : measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) L (𝓝 (f_lim x)) := begin inhabit ι, unfreezingI { rcases eq_or_ne L ⊥ with rfl | hL }, { exact ⟨(hf default).mk _, (hf default).measurable_mk, eventually_of_forall $ λ x, tendsto_bot⟩ }, haveI : ne_bot L := ⟨hL⟩, let p : α → (ι → β) → Prop := λ x f', ∃ l : β, tendsto (λ n, f' n) L (𝓝 l), have hp_mem : ∀ x ∈ ae_seq_set hf p, p x (λ n, f n x), from λ x hx, ae_seq.fun_prop_of_mem_ae_seq_set hf hx, have h_ae_eq : ∀ᵐ x ∂μ, ∀ n, ae_seq hf p n x = f n x, from ae_seq.ae_seq_eq_fun_ae hf h_ae_tendsto, let f_lim : α → β := λ x, dite (x ∈ ae_seq_set hf p) (λ h, (hp_mem x h).some) (λ h, (⟨f default x⟩ : nonempty β).some), have hf_lim : ∀ x, tendsto (λ n, ae_seq hf p n x) L (𝓝 (f_lim x)), { intros x, simp only [f_lim, ae_seq], split_ifs, { refine (hp_mem x h).some_spec.congr (λ n, _), exact (ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h n).symm }, { exact tendsto_const_nhds, }, }, have h_ae_tendsto_f_lim : ∀ᵐ x ∂μ, tendsto (λ n, f n x) L (𝓝 (f_lim x)), from h_ae_eq.mono (λ x hx, (hf_lim x).congr hx), have h_f_lim_meas : measurable f_lim, from measurable_of_tendsto_metric' L (ae_seq.measurable hf p) (tendsto_pi_nhds.mpr (λ x, hf_lim x)), exact ⟨f_lim, h_f_lim_meas, h_ae_tendsto_f_lim⟩, end end limits namespace continuous_linear_map variables {𝕜 : Type*} [normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] variables [opens_measurable_space E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] [measurable_space F] [borel_space F] @[measurability] protected lemma measurable (L : E →L[𝕜] F) : measurable L := L.continuous.measurable lemma measurable_comp (L : E →L[𝕜] F) {φ : α → E} (φ_meas : measurable φ) : measurable (λ (a : α), L (φ a)) := L.measurable.comp φ_meas end continuous_linear_map namespace continuous_linear_map variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] instance : measurable_space (E →L[𝕜] F) := borel _ instance : borel_space (E →L[𝕜] F) := ⟨rfl⟩ @[measurability] lemma measurable_apply [measurable_space F] [borel_space F] (x : E) : measurable (λ f : E →L[𝕜] F, f x) := (apply 𝕜 F x).continuous.measurable @[measurability] lemma measurable_apply' [measurable_space E] [opens_measurable_space E] [measurable_space F] [borel_space F] : measurable (λ (x : E) (f : E →L[𝕜] F), f x) := measurable_pi_lambda _ $ λ f, f.measurable @[measurability] lemma measurable_coe [measurable_space F] [borel_space F] : measurable (λ (f : E →L[𝕜] F) (x : E), f x) := measurable_pi_lambda _ measurable_apply end continuous_linear_map section continuous_linear_map_nondiscrete_normed_field variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] @[measurability] lemma measurable.apply_continuous_linear_map {φ : α → F →L[𝕜] E} (hφ : measurable φ) (v : F) : measurable (λ a, φ a v) := (continuous_linear_map.apply 𝕜 E v).measurable.comp hφ @[measurability] lemma ae_measurable.apply_continuous_linear_map {φ : α → F →L[𝕜] E} {μ : measure α} (hφ : ae_measurable φ μ) (v : F) : ae_measurable (λ a, φ a v) μ := (continuous_linear_map.apply 𝕜 E v).measurable.comp_ae_measurable hφ end continuous_linear_map_nondiscrete_normed_field section normed_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] [measurable_space 𝕜] variables [borel_space 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E] lemma measurable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) : measurable (λ x, f x • c) ↔ measurable f := (closed_embedding_smul_left hc).measurable_embedding.measurable_comp_iff lemma ae_measurable_smul_const {f : α → 𝕜} {μ : measure α} {c : E} (hc : c ≠ 0) : ae_measurable (λ x, f x • c) μ ↔ ae_measurable f μ := (closed_embedding_smul_left hc).measurable_embedding.ae_measurable_comp_iff end normed_space
eab596b5bace0d4beb5361dbfdbd97b97615040c
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/opposite.lean
0d1eac010fbe1dcada9ae71bd04840e2a1d26f4c
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
3,344
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton, Simon Hudon, Kenny Lau Opposites. -/ import data.list.defs universes v u -- declare the `v` first; see `category_theory.category` for an explanation variable (α : Sort u) /-- The type of objects of the opposite of `α`; used to defined opposite category/group/... In order to avoid confusion between `α` and its opposite type, we set up the type of objects `opposite α` using the following pattern, which will be repeated later for the morphisms. 1. Define `opposite α := α`. 2. Define the isomorphisms `op : α → opposite α`, `unop : opposite α → α`. 3. Make the definition `opposite` irreducible. This has the following consequences. * `opposite α` and `α` are distinct types in the elaborator, so you must use `op` and `unop` explicitly to convert between them. * Both `unop (op X) = X` and `op (unop X) = X` are definitional equalities. Notably, every object of the opposite category is definitionally of the form `op X`, which greatly simplifies the definition of the structure of the opposite category, for example. (If Lean supported definitional eta equality for records, we could achieve the same goals using a structure with one field.) -/ def opposite : Sort u := α -- Use a high right binding power (like that of postfix ⁻¹) so that, for example, -- `presheaf Cᵒᵖ` parses as `presheaf (Cᵒᵖ)` and not `(presheaf C)ᵒᵖ`. notation α `ᵒᵖ`:std.prec.max_plus := opposite α namespace opposite variables {α} def op : α → αᵒᵖ := id def unop : αᵒᵖ → α := id lemma op_inj : function.injective (op : α → αᵒᵖ) := λ _ _, id lemma unop_inj : function.injective (unop : αᵒᵖ → α) := λ _ _, id @[simp] lemma op_inj_iff (x y : α) : op x = op y ↔ x = y := iff.rfl @[simp] lemma unop_inj_iff (x y : αᵒᵖ) : unop x = unop y ↔ x = y := iff.rfl @[simp] lemma op_unop (x : αᵒᵖ) : op (unop x) = x := rfl @[simp] lemma unop_op (x : α) : unop (op x) = x := rfl attribute [irreducible] opposite instance [inhabited α] : inhabited αᵒᵖ := ⟨op (default _)⟩ def op_induction {F : Π (X : αᵒᵖ), Sort v} (h : Π X, F (op X)) : Π X, F X := λ X, h (unop X) end opposite namespace tactic open opposite open interactive interactive.types lean.parser tactic local postfix `?`:9001 := optional namespace op_induction meta def is_opposite (e : expr) : tactic bool := do t ← infer_type e, `(opposite _) ← whnf t | return ff, return tt meta def find_opposite_hyp : tactic name := do lc ← local_context, h :: _ ← lc.mfilter $ is_opposite | fail "No hypotheses of the form Xᵒᵖ", return h.local_pp_name end op_induction open op_induction meta def op_induction (h : option name) : tactic unit := do h ← match h with | (some h) := pure h | none := find_opposite_hyp end, h' ← tactic.get_local h, revert_lst [h'], applyc `opposite.op_induction, tactic.intro h, skip -- For use with `local attribute [tidy] op_induction` meta def op_induction' := op_induction none namespace interactive meta def op_induction (h : parse ident?) : tactic unit := tactic.op_induction h end interactive end tactic
9d408c4b6d0893cbb7c28ba3f3f36f690c4bfeef
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/tests/lean/run/elab_cmd.lean
67da01c8ef315cf948d419ebfe5abb0b5a227087
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
645
lean
import Lean open Lean.Elab.Term open Lean.Elab.Command elab "∃" b:term "," P:term : term => do let ex ← `(Exists (fun $b => $P)); elabTerm ex none elab "#check2" b:term : command => do let cmd ← `(#check $b #check $b); elabCommand cmd #check ∃ x, x > 0 #check ∃ (x : UInt32), x > 0 #check2 10 elab "try" t:tactic : tactic => do let t' ← `(tactic| $t <|> skip); Lean.Elab.Tactic.evalTactic t' theorem tst (x y z : Nat) : y = z → x = x → x = y → x = z := by { intro h1; intro h2; intro h3; apply @Eq.trans; try exact h1; -- `exact h1` fails traceState; try exact h3; traceState; try exact h1; }
152c2b463885cf83e6d6d471ce38e9dc2fa28a36
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/expr.lean
a015c1df3627779154f74a7de687d9569260e70f
[ "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
342
lean
import meta.expr open tactic run_cmd do l ← mk_local' `l binder_info.default `(ℕ), m ← mk_local' `m binder_info.default `(ℕ), guard $ `(%%l + %%l = 3).has_local_constant l, guard $ bnot $ `(%%l + %%l = 3).has_local_constant m, guard $ `(%%l + %%m = 3).has_local_constant l, guard $ `(%%l + %%m = 3).has_local_constant m
e5ee23263ea3aa5dffe4e874fb2c27b6883bb1e0
c777c32c8e484e195053731103c5e52af26a25d1
/src/ring_theory/roots_of_unity.lean
d5fe89ae3a8cafef51da172bcf8a39e4ee77880b
[ "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
47,502
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.char_p.two import algebra.ne_zero import algebra.gcd_monoid.integrally_closed import data.polynomial.ring_division import field_theory.finite.basic import field_theory.minpoly.is_integrally_closed import field_theory.separable import group_theory.specific_groups.cyclic import number_theory.divisors import ring_theory.integral_domain import tactic.zify /-! # Roots of unity and primitive roots of unity We define roots of unity in the context of an arbitrary commutative monoid, as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative monoids, expressing that an element is a primitive root of unity. ## Main definitions * `roots_of_unity n M`, for `n : ℕ+` is the subgroup of the units of a commutative monoid `M` consisting of elements `x` that satisfy `x ^ n = 1`. * `is_primitive_root ζ k`: an element `ζ` is a primitive `k`-th root of unity if `ζ ^ k = 1`, and if `l` satisfies `ζ ^ l = 1` then `k ∣ l`. * `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`. * `is_primitive_root.aut_to_pow`: the monoid hom that takes an automorphism of a ring to the power it sends that specific primitive root, as a member of `(zmod n)ˣ`. ## Main results * `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group. * `is_primitive_root.zmod_equiv_zpowers`: `zmod k` is equivalent to the subgroup generated by a primitive `k`-th root of unity. * `is_primitive_root.zpowers_eq`: in an integral domain, the subgroup generated by a primitive `k`-th root of unity is equal to the `k`-th roots of unity. * `is_primitive_root.card_primitive_roots`: if an integral domain has a primitive `k`-th root of unity, then it has `φ k` of them. ## Implementation details It is desirable that `roots_of_unity` is a subgroup, and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields. We therefore implement it as a subgroup of the units of a commutative monoid. We have chosen to define `roots_of_unity n` for `n : ℕ+`, instead of `n : ℕ`, because almost all lemmas need the positivity assumption, and in particular the type class instances for `fintype` and `is_cyclic`. On the other hand, for primitive roots of unity, it is desirable to have a predicate not just on units, but directly on elements of the ring/field. For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity in the complex numbers, without having to turn that number into a unit first. This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and `is_primitive_root.coe_units_iff` should provide the necessary glue. -/ open_locale classical big_operators polynomial noncomputable theory open polynomial open finset variables {M N G R S F : Type*} variables [comm_monoid M] [comm_monoid N] [division_comm_monoid G] section roots_of_unity variables {k l : ℕ+} /-- `roots_of_unity k M` is the subgroup of elements `m : Mˣ` that satisfy `m ^ k = 1` -/ def roots_of_unity (k : ℕ+) (M : Type*) [comm_monoid M] : subgroup Mˣ := { carrier := { ζ | ζ ^ (k : ℕ) = 1 }, one_mem' := one_pow _, mul_mem' := λ ζ ξ hζ hξ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *, inv_mem' := λ ζ hζ, by simp only [*, set.mem_set_of_eq, inv_pow, inv_one] at * } @[simp] lemma mem_roots_of_unity (k : ℕ+) (ζ : Mˣ) : ζ ∈ roots_of_unity k M ↔ ζ ^ (k : ℕ) = 1 := iff.rfl lemma mem_roots_of_unity' (k : ℕ+) (ζ : Mˣ) : ζ ∈ roots_of_unity k M ↔ (ζ : M) ^ (k : ℕ) = 1 := by { rw [mem_roots_of_unity], norm_cast } lemma roots_of_unity.coe_injective {n : ℕ+} : function.injective (coe : (roots_of_unity n M) → M) := units.ext.comp (λ x y, subtype.ext) /-- Make an element of `roots_of_unity` from a member of the base ring, and a proof that it has a positive power equal to one. -/ @[simps coe_coe] def roots_of_unity.mk_of_pow_eq (ζ : M) {n : ℕ+} (h : ζ ^ (n : ℕ) = 1) : roots_of_unity n M := ⟨units.of_pow_eq_one ζ n h n.ne_zero, units.pow_of_pow_eq_one _ _⟩ @[simp] lemma roots_of_unity.coe_mk_of_pow_eq {ζ : M} {n : ℕ+} (h : ζ ^ (n : ℕ) = 1) : (roots_of_unity.mk_of_pow_eq _ h : M) = ζ := rfl lemma roots_of_unity_le_of_dvd (h : k ∣ l) : roots_of_unity k M ≤ roots_of_unity l M := begin obtain ⟨d, rfl⟩ := h, intros ζ h, simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *, end lemma map_roots_of_unity (f : Mˣ →* Nˣ) (k : ℕ+) : (roots_of_unity k M).map f ≤ roots_of_unity k N := begin rintros _ ⟨ζ, h, rfl⟩, simp only [←map_pow, *, mem_roots_of_unity, set_like.mem_coe, monoid_hom.map_one] at * end @[norm_cast] lemma roots_of_unity.coe_pow [comm_monoid R] (ζ : roots_of_unity k R) (m : ℕ) : ↑(ζ ^ m) = (ζ ^ m : R) := begin change ↑(↑(ζ ^ m) : Rˣ) = ↑(ζ : Rˣ) ^ m, rw [subgroup.coe_pow, units.coe_pow], end section comm_semiring variables [comm_semiring R] [comm_semiring S] /-- Restrict a ring homomorphism to the nth roots of unity -/ def restrict_roots_of_unity [ring_hom_class F R S] (σ : F) (n : ℕ+) : roots_of_unity n R →* roots_of_unity n S := let h : ∀ ξ : roots_of_unity n R, (σ ξ) ^ (n : ℕ) = 1 := λ ξ, by { change (σ (ξ : Rˣ)) ^ (n : ℕ) = 1, rw [←map_pow, ←units.coe_pow, show ((ξ : Rˣ) ^ (n : ℕ) = 1), from ξ.2, units.coe_one, map_one σ] } in { to_fun := λ ξ, ⟨@unit_of_invertible _ _ _ (invertible_of_pow_eq_one _ _ (h ξ) n.ne_zero), by { ext, rw units.coe_pow, exact h ξ }⟩, map_one' := by { ext, exact map_one σ }, map_mul' := λ ξ₁ ξ₂, by { ext, rw [subgroup.coe_mul, units.coe_mul], exact map_mul σ _ _ } } @[simp] lemma restrict_roots_of_unity_coe_apply [ring_hom_class F R S] (σ : F) (ζ : roots_of_unity k R) : ↑(restrict_roots_of_unity σ k ζ) = σ ↑ζ := rfl /-- Restrict a ring isomorphism to the nth roots of unity -/ def ring_equiv.restrict_roots_of_unity (σ : R ≃+* S) (n : ℕ+) : roots_of_unity n R ≃* roots_of_unity n S := { to_fun := restrict_roots_of_unity σ.to_ring_hom n, inv_fun :=restrict_roots_of_unity σ.symm.to_ring_hom n, left_inv := λ ξ, by { ext, exact σ.symm_apply_apply ξ }, right_inv := λ ξ, by { ext, exact σ.apply_symm_apply ξ }, map_mul' := (restrict_roots_of_unity _ n).map_mul } @[simp] lemma ring_equiv.restrict_roots_of_unity_coe_apply (σ : R ≃+* S) (ζ : roots_of_unity k R) : ↑(σ.restrict_roots_of_unity k ζ) = σ ↑ζ := rfl @[simp] lemma ring_equiv.restrict_roots_of_unity_symm (σ : R ≃+* S) : (σ.restrict_roots_of_unity k).symm = σ.symm.restrict_roots_of_unity k := rfl end comm_semiring section is_domain variables [comm_ring R] [is_domain R] lemma mem_roots_of_unity_iff_mem_nth_roots {ζ : Rˣ} : ζ ∈ roots_of_unity k R ↔ (ζ : R) ∈ nth_roots k (1 : R) := by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow] variables (k R) /-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`. This is implemented as equivalence of subtypes, because `roots_of_unity` is a subgroup of the group of units, whereas `nth_roots` is a multiset. -/ def roots_of_unity_equiv_nth_roots : roots_of_unity k R ≃ {x // x ∈ nth_roots k (1 : R)} := begin refine { to_fun := λ x, ⟨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2⟩, inv_fun := λ x, ⟨⟨x, x ^ (k - 1 : ℕ), _, _⟩, _⟩, left_inv := _, right_inv := _ }, swap 4, { rintro ⟨x, hx⟩, ext, refl }, swap 4, { rintro ⟨x, hx⟩, ext, refl }, all_goals { rcases x with ⟨x, hx⟩, rw [mem_nth_roots k.pos] at hx, simp only [subtype.coe_mk, ← pow_succ, ← pow_succ', hx, tsub_add_cancel_of_le (show 1 ≤ (k : ℕ), from k.one_le)] }, { show (_ : Rˣ) ^ (k : ℕ) = 1, simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] } end variables {k R} @[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) : (roots_of_unity_equiv_nth_roots R k x : R) = x := rfl @[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x ∈ nth_roots k (1 : R)}) : ((roots_of_unity_equiv_nth_roots R k).symm x : R) = x := rfl variables (k R) instance roots_of_unity.fintype : fintype (roots_of_unity k R) := fintype.of_equiv {x // x ∈ nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) := is_cyclic_of_subgroup_is_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype) (units.ext.comp subtype.val_injective) lemma card_roots_of_unity : fintype.card (roots_of_unity k R) ≤ k := calc fintype.card (roots_of_unity k R) = fintype.card {x // x ∈ nth_roots k (1 : R)} : fintype.card_congr (roots_of_unity_equiv_nth_roots R k) ... ≤ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.dedup_le _) ... = (nth_roots k (1 : R)).card : multiset.card_attach ... ≤ k : card_nth_roots k 1 variables {k R} lemma map_root_of_unity_eq_pow_self [ring_hom_class F R R] (σ : F) (ζ : roots_of_unity k R) : ∃ m : ℕ, σ ζ = ζ ^ m := begin obtain ⟨m, hm⟩ := monoid_hom.map_cyclic (restrict_roots_of_unity σ k), rw [←restrict_roots_of_unity_coe_apply, hm, zpow_eq_mod_order_of, ←int.to_nat_of_nonneg (m.mod_nonneg (int.coe_nat_ne_zero.mpr (pos_iff_ne_zero.mp (order_of_pos ζ)))), zpow_coe_nat, roots_of_unity.coe_pow], exact ⟨(m % (order_of ζ)).to_nat, rfl⟩, end end is_domain section reduced variables (R) [comm_ring R] [is_reduced R] @[simp] lemma mem_roots_of_unity_prime_pow_mul_iff (p k : ℕ) (m : ℕ+) [hp : fact p.prime] [char_p R p] {ζ : Rˣ} : ζ ∈ roots_of_unity (⟨p, hp.1.pos⟩ ^ k * m) R ↔ ζ ∈ roots_of_unity m R := by simp [mem_roots_of_unity'] end reduced end roots_of_unity /-- An element `ζ` is a primitive `k`-th root of unity if `ζ ^ k = 1`, and if `l` satisfies `ζ ^ l = 1` then `k ∣ l`. -/ structure is_primitive_root (ζ : M) (k : ℕ) : Prop := (pow_eq_one : ζ ^ (k : ℕ) = 1) (dvd_of_pow_eq_one : ∀ l : ℕ, ζ ^ l = 1 → k ∣ l) /-- Turn a primitive root μ into a member of the `roots_of_unity` subgroup. -/ @[simps] def is_primitive_root.to_roots_of_unity {μ : M} {n : ℕ+} (h : is_primitive_root μ n) : roots_of_unity n M := roots_of_unity.mk_of_pow_eq μ h.pow_eq_one section primitive_roots variables {k : ℕ} /-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity in the integral domain `R`. -/ def primitive_roots (k : ℕ) (R : Type*) [comm_ring R] [is_domain R] : finset R := (nth_roots k (1 : R)).to_finset.filter (λ ζ, is_primitive_root ζ k) variables [comm_ring R] [is_domain R] @[simp] lemma mem_primitive_roots {ζ : R} (h0 : 0 < k) : ζ ∈ primitive_roots k R ↔ is_primitive_root ζ k := begin rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp], exact is_primitive_root.pow_eq_one end @[simp] lemma primitive_roots_zero : primitive_roots 0 R = ∅ := by rw [primitive_roots, nth_roots_zero, multiset.to_finset_zero, finset.filter_empty] lemma is_primitive_root_of_mem_primitive_roots {ζ : R} (h : ζ ∈ primitive_roots k R) : is_primitive_root ζ k := k.eq_zero_or_pos.elim (λ hk, false.elim $ by simpa [hk] using h) (λ hk, (mem_primitive_roots hk).1 h) end primitive_roots namespace is_primitive_root variables {k l : ℕ} lemma iff_def (ζ : M) (k : ℕ) : is_primitive_root ζ k ↔ (ζ ^ k = 1) ∧ (∀ l : ℕ, ζ ^ l = 1 → k ∣ l) := ⟨λ ⟨h1, h2⟩, ⟨h1, h2⟩, λ ⟨h1, h2⟩, ⟨h1, h2⟩⟩ lemma mk_of_lt (ζ : M) (hk : 0 < k) (h1 : ζ ^ k = 1) (h : ∀ l : ℕ, 0 < l → l < k → ζ ^ l ≠ 1) : is_primitive_root ζ k := begin refine ⟨h1, λ l hl, _⟩, suffices : k.gcd l = k, { exact this ▸ k.gcd_dvd_right l }, rw eq_iff_le_not_lt, refine ⟨nat.le_of_dvd hk (k.gcd_dvd_left l), _⟩, intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h', exact pow_gcd_eq_one _ h1 hl end section comm_monoid variables {ζ : M} {f : F} (h : is_primitive_root ζ k) @[nontriviality] lemma of_subsingleton [subsingleton M] (x : M) : is_primitive_root x 1 := ⟨subsingleton.elim _ _, λ _ _, one_dvd _⟩ lemma pow_eq_one_iff_dvd (l : ℕ) : ζ ^ l = 1 ↔ k ∣ l := ⟨h.dvd_of_pow_eq_one l, by { rintro ⟨i, rfl⟩, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }⟩ lemma is_unit (h : is_primitive_root ζ k) (h0 : 0 < k) : is_unit ζ := begin apply is_unit_of_mul_eq_one ζ (ζ ^ (k - 1)), rw [← pow_succ, tsub_add_cancel_of_le h0.nat_succ_le, h.pow_eq_one] end lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ζ ^ l ≠ 1 := mt (nat.le_of_dvd h0 ∘ h.dvd_of_pow_eq_one _) $ not_le_of_lt hl lemma ne_one (hk : 1 < k) : ζ ≠ 1 := h.pow_ne_one_of_pos_of_lt zero_lt_one hk ∘ (pow_one ζ).trans lemma pow_inj (h : is_primitive_root ζ k) ⦃i j : ℕ⦄ (hi : i < k) (hj : j < k) (H : ζ ^ i = ζ ^ j) : i = j := begin wlog hij : i ≤ j generalizing i j, { exact (this hj hi H.symm (le_of_not_le hij)).symm }, apply le_antisymm hij, rw ← tsub_eq_zero_iff_le, apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt tsub_le_self hj), apply h.dvd_of_pow_eq_one, rw [← ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj, ← pow_add, tsub_add_cancel_of_le hij, H, one_mul] end lemma one : is_primitive_root (1 : M) 1 := { pow_eq_one := pow_one _, dvd_of_pow_eq_one := λ l hl, one_dvd _ } @[simp] lemma one_right_iff : is_primitive_root ζ 1 ↔ ζ = 1 := begin split, { intro h, rw [← pow_one ζ, h.pow_eq_one] }, { rintro rfl, exact one } end @[simp] lemma coe_submonoid_class_iff {M B : Type*} [comm_monoid M] [set_like B M] [submonoid_class B M] {N : B} {ζ : N} : is_primitive_root (ζ : M) k ↔ is_primitive_root ζ k := by simp [iff_def, ← submonoid_class.coe_pow] @[simp] lemma coe_units_iff {ζ : Mˣ} : is_primitive_root (ζ : M) k ↔ is_primitive_root ζ k := by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one] lemma pow_of_coprime (h : is_primitive_root ζ k) (i : ℕ) (hi : i.coprime k) : is_primitive_root (ζ ^ i) k := begin by_cases h0 : k = 0, { subst k, simp only [*, pow_one, nat.coprime_zero_right] at * }, rcases h.is_unit (nat.pos_of_ne_zero h0) with ⟨ζ, rfl⟩, rw [← units.coe_pow], rw coe_units_iff at h ⊢, refine { pow_eq_one := by rw [← pow_mul', pow_mul, h.pow_eq_one, one_pow], dvd_of_pow_eq_one := _ }, intros l hl, apply h.dvd_of_pow_eq_one, rw [← pow_one ζ, ← zpow_coe_nat ζ, ← hi.gcd_eq_one, nat.gcd_eq_gcd_ab, zpow_add, mul_pow, ← zpow_coe_nat, ← zpow_mul, mul_right_comm], simp only [zpow_mul, hl, h.pow_eq_one, one_zpow, one_pow, one_mul, zpow_coe_nat] end lemma pow_of_prime (h : is_primitive_root ζ k) {p : ℕ} (hprime : nat.prime p) (hdiv : ¬ p ∣ k) : is_primitive_root (ζ ^ p) k := h.pow_of_coprime p (hprime.coprime_iff_not_dvd.2 hdiv) lemma pow_iff_coprime (h : is_primitive_root ζ k) (h0 : 0 < k) (i : ℕ) : is_primitive_root (ζ ^ i) k ↔ i.coprime k := begin refine ⟨_, h.pow_of_coprime i⟩, intro hi, obtain ⟨a, ha⟩ := i.gcd_dvd_left k, obtain ⟨b, hb⟩ := i.gcd_dvd_right k, suffices : b = k, { rwa [this, ← one_mul k, mul_left_inj' h0.ne', eq_comm] at hb { occs := occurrences.pos [1] } }, rw [ha] at hi, rw [mul_comm] at hb, apply nat.dvd_antisymm ⟨i.gcd k, hb⟩ (hi.dvd_of_pow_eq_one b _), rw [← pow_mul', ← mul_assoc, ← hb, pow_mul, h.pow_eq_one, one_pow] end protected lemma order_of (ζ : M) : is_primitive_root ζ (order_of ζ) := ⟨pow_order_of_eq_one ζ, λ l, order_of_dvd_of_pow_eq_one⟩ lemma unique {ζ : M} (hk : is_primitive_root ζ k) (hl : is_primitive_root ζ l) : k = l := nat.dvd_antisymm (hk.2 _ hl.1) (hl.2 _ hk.1) lemma eq_order_of : k = order_of ζ := h.unique (is_primitive_root.order_of ζ) protected lemma iff (hk : 0 < k) : is_primitive_root ζ k ↔ ζ ^ k = 1 ∧ ∀ l : ℕ, 0 < l → l < k → ζ ^ l ≠ 1 := begin refine ⟨λ h, ⟨h.pow_eq_one, λ l hl' hl, _⟩, λ ⟨hζ, hl⟩, is_primitive_root.mk_of_lt ζ hk hζ hl⟩, rw h.eq_order_of at hl, exact pow_ne_one_of_lt_order_of' hl'.ne' hl, end protected lemma not_iff : ¬ is_primitive_root ζ k ↔ order_of ζ ≠ k := ⟨λ h hk, h $ hk ▸ is_primitive_root.order_of ζ, λ h hk, h.symm $ hk.unique $ is_primitive_root.order_of ζ⟩ lemma pow_of_dvd (h : is_primitive_root ζ k) {p : ℕ} (hp : p ≠ 0) (hdiv : p ∣ k) : is_primitive_root (ζ ^ p) (k / p) := begin suffices : order_of (ζ ^ p) = k / p, { exact this ▸ is_primitive_root.order_of (ζ ^ p) }, rw [order_of_pow' _ hp, ← eq_order_of h, nat.gcd_eq_right hdiv] end protected lemma mem_roots_of_unity {ζ : Mˣ} {n : ℕ+} (h : is_primitive_root ζ n) : ζ ∈ roots_of_unity n M := h.pow_eq_one /-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`, then there is a `b`-th primitive root of unity in `R`. -/ lemma pow {n : ℕ} {a b : ℕ} (hn : 0 < n) (h : is_primitive_root ζ n) (hprod : n = a * b) : is_primitive_root (ζ ^ a) b := begin subst n, simp only [iff_def, ← pow_mul, h.pow_eq_one, eq_self_iff_true, true_and], intros l hl, have ha0 : a ≠ 0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn }, rwa ← mul_dvd_mul_iff_left ha0, exact h.dvd_of_pow_eq_one _ hl end section maps open function lemma map_of_injective [monoid_hom_class F M N] (h : is_primitive_root ζ k) (hf : injective f) : is_primitive_root (f ζ) k := { pow_eq_one := by rw [←map_pow, h.pow_eq_one, _root_.map_one], dvd_of_pow_eq_one := begin rw h.eq_order_of, intros l hl, rw [←map_pow, ←map_one f] at hl, exact order_of_dvd_of_pow_eq_one (hf hl) end } lemma of_map_of_injective [monoid_hom_class F M N] (h : is_primitive_root (f ζ) k) (hf : injective f) : is_primitive_root ζ k := { pow_eq_one := by { apply_fun f, rw [map_pow, _root_.map_one, h.pow_eq_one] }, dvd_of_pow_eq_one := begin rw h.eq_order_of, intros l hl, apply_fun f at hl, rw [map_pow, _root_.map_one] at hl, exact order_of_dvd_of_pow_eq_one hl end } lemma map_iff_of_injective [monoid_hom_class F M N] (hf : injective f) : is_primitive_root (f ζ) k ↔ is_primitive_root ζ k := ⟨λ h, h.of_map_of_injective hf, λ h, h.map_of_injective hf⟩ end maps end comm_monoid section comm_monoid_with_zero variables {M₀ : Type*} [comm_monoid_with_zero M₀] lemma zero [nontrivial M₀] : is_primitive_root (0 : M₀) 0 := ⟨pow_zero 0, λ l hl, by simpa [zero_pow_eq, show ∀ p, ¬p → false ↔ p, from @not_not] using hl⟩ protected lemma ne_zero [nontrivial M₀] {ζ : M₀} (h : is_primitive_root ζ k) : k ≠ 0 → ζ ≠ 0 := mt $ λ hn, h.unique (hn.symm ▸ is_primitive_root.zero) end comm_monoid_with_zero section division_comm_monoid variables {ζ : G} lemma zpow_eq_one (h : is_primitive_root ζ k) : ζ ^ (k : ℤ) = 1 := by { rw zpow_coe_nat, exact h.pow_eq_one } lemma zpow_eq_one_iff_dvd (h : is_primitive_root ζ k) (l : ℤ) : ζ ^ l = 1 ↔ (k : ℤ) ∣ l := begin by_cases h0 : 0 ≤ l, { lift l to ℕ using h0, rw [zpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l }, { have : 0 ≤ -l, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 }, lift -l to ℕ using this with l' hl', rw [← dvd_neg, ← hl'], norm_cast, rw [← h.pow_eq_one_iff_dvd, ← inv_inj, ← zpow_neg, ← hl', zpow_coe_nat, inv_one] } end lemma inv (h : is_primitive_root ζ k) : is_primitive_root ζ⁻¹ k := { pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_pow], dvd_of_pow_eq_one := begin intros l hl, apply h.dvd_of_pow_eq_one l, rw [← inv_inj, ← inv_pow, hl, inv_one] end } @[simp] lemma inv_iff : is_primitive_root ζ⁻¹ k ↔ is_primitive_root ζ k := by { refine ⟨_, λ h, inv h⟩, intro h, rw [← inv_inv ζ], exact inv h } lemma zpow_of_gcd_eq_one (h : is_primitive_root ζ k) (i : ℤ) (hi : i.gcd k = 1) : is_primitive_root (ζ ^ i) k := begin by_cases h0 : 0 ≤ i, { lift i to ℕ using h0, rw zpow_coe_nat, exact h.pow_of_coprime i hi }, have : 0 ≤ -i, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 }, lift -i to ℕ using this with i' hi', rw [← inv_iff, ← zpow_neg, ← hi', zpow_coe_nat], apply h.pow_of_coprime, rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi, exact hi end end division_comm_monoid section is_domain variables {ζ : R} variables [comm_ring R] [is_domain R] @[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} := begin apply finset.eq_singleton_iff_unique_mem.2, split, { simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] }, { intros x hx, rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx, exact hx } end lemma ne_zero' {n : ℕ+} (hζ : is_primitive_root ζ n) : ne_zero ((n : ℕ) : R) := begin let p := ring_char R, have hfin := (multiplicity.finite_nat_iff.2 ⟨char_p.char_ne_one R p, n.pos⟩), obtain ⟨m, hm⟩ := multiplicity.exists_eq_pow_mul_and_not_dvd hfin, by_cases hp : p ∣ n, { obtain ⟨k, hk⟩ := nat.exists_eq_succ_of_ne_zero (multiplicity.pos_of_dvd hfin hp).ne', haveI : ne_zero p := ne_zero.of_pos (nat.pos_of_dvd_of_pos hp n.pos), haveI hpri : fact p.prime := char_p.char_is_prime_of_pos R p, have := hζ.pow_eq_one, rw [hm.1, hk, pow_succ, mul_assoc, pow_mul', ← frobenius_def, ← frobenius_one p] at this, exfalso, have hpos : 0 < p ^ k * m, { refine (mul_pos (pow_pos hpri.1.pos _) (nat.pos_of_ne_zero (λ h, _))), have H := hm.1, rw [h] at H, simpa using H }, refine hζ.pow_ne_one_of_pos_of_lt hpos _ (frobenius_inj R p this), { rw [hm.1, hk, pow_succ, mul_assoc, mul_comm p], exact lt_mul_of_one_lt_right hpos hpri.1.one_lt } }, { exact ne_zero.of_not_dvd R hp } end lemma mem_nth_roots_finset (hζ : is_primitive_root ζ k) (hk : 0 < k) : ζ ∈ nth_roots_finset k R := (mem_nth_roots_finset hk).2 hζ.pow_eq_one end is_domain section is_domain variables [comm_ring R] variables {ζ : Rˣ} (h : is_primitive_root ζ k) lemma eq_neg_one_of_two_right [no_zero_divisors R] {ζ : R} (h : is_primitive_root ζ 2) : ζ = -1 := begin apply (eq_or_eq_neg_of_sq_eq_sq ζ 1 _).resolve_left, { rw [← pow_one ζ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial }, { simp only [h.pow_eq_one, one_pow] } end lemma neg_one (p : ℕ) [nontrivial R] [h : char_p R p] (hp : p ≠ 2) : is_primitive_root (-1 : R) 2 := begin convert is_primitive_root.order_of (-1 : R), rw [order_of_neg_one, if_neg], rwa ring_char.eq_iff.mpr h end /-- If `1 < k` then `(∑ i in range k, ζ ^ i) = 0`. -/ lemma geom_sum_eq_zero [is_domain R] {ζ : R} (hζ : is_primitive_root ζ k) (hk : 1 < k) : (∑ i in range k, ζ ^ i) = 0 := begin refine eq_zero_of_ne_zero_of_mul_left_eq_zero (sub_ne_zero_of_ne (hζ.ne_one hk).symm) _, rw [mul_neg_geom_sum, hζ.pow_eq_one, sub_self] end /-- If `1 < k`, then `ζ ^ k.pred = -(∑ i in range k.pred, ζ ^ i)`. -/ lemma pow_sub_one_eq [is_domain R] {ζ : R} (hζ : is_primitive_root ζ k) (hk : 1 < k) : ζ ^ k.pred = -(∑ i in range k.pred, ζ ^ i) := by rw [eq_neg_iff_add_eq_zero, add_comm, ←sum_range_succ, ←nat.succ_eq_add_one, nat.succ_pred_eq_of_pos (pos_of_gt hk), hζ.geom_sum_eq_zero hk] /-- The (additive) monoid equivalence between `zmod k` and the powers of a primitive root of unity `ζ`. -/ def zmod_equiv_zpowers (h : is_primitive_root ζ k) : zmod k ≃+ additive (subgroup.zpowers ζ) := add_equiv.of_bijective (add_monoid_hom.lift_of_right_inverse (int.cast_add_hom $ zmod k) _ zmod.int_cast_right_inverse ⟨{ to_fun := λ i, additive.of_mul (⟨_, i, rfl⟩ : subgroup.zpowers ζ), map_zero' := by { simp only [zpow_zero], refl }, map_add' := by { intros i j, simp only [zpow_add], refl } }, (λ i hi, begin simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k, add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi ⊢, obtain ⟨i, rfl⟩ := hi, simp only [zpow_mul, h.pow_eq_one, one_zpow, zpow_coe_nat], refl end)⟩) begin split, { rw injective_iff_map_eq_zero, intros i hi, rw subtype.ext_iff at hi, have := (h.zpow_eq_one_iff_dvd _).mp hi, rw [← (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm], exact zmod.int_cast_right_inverse i }, { rintro ⟨ξ, i, rfl⟩, refine ⟨int.cast_add_hom _ i, _⟩, rw [add_monoid_hom.lift_of_right_inverse_comp_apply], refl } end @[simp] lemma zmod_equiv_zpowers_apply_coe_int (i : ℤ) : h.zmod_equiv_zpowers i = additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ) := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ zmod.int_cast_right_inverse _ _ @[simp] lemma zmod_equiv_zpowers_apply_coe_nat (i : ℕ) : h.zmod_equiv_zpowers i = additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ) := begin have : (i : zmod k) = (i : ℤ), by norm_cast, simp only [this, zmod_equiv_zpowers_apply_coe_int, zpow_coe_nat], refl end @[simp] lemma zmod_equiv_zpowers_symm_apply_zpow (i : ℤ) : h.zmod_equiv_zpowers.symm (additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ)) = i := by rw [← h.zmod_equiv_zpowers.symm_apply_apply i, zmod_equiv_zpowers_apply_coe_int] @[simp] lemma zmod_equiv_zpowers_symm_apply_zpow' (i : ℤ) : h.zmod_equiv_zpowers.symm ⟨ζ ^ i, i, rfl⟩ = i := h.zmod_equiv_zpowers_symm_apply_zpow i @[simp] lemma zmod_equiv_zpowers_symm_apply_pow (i : ℕ) : h.zmod_equiv_zpowers.symm (additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ)) = i := by rw [← h.zmod_equiv_zpowers.symm_apply_apply i, zmod_equiv_zpowers_apply_coe_nat] @[simp] lemma zmod_equiv_zpowers_symm_apply_pow' (i : ℕ) : h.zmod_equiv_zpowers.symm ⟨ζ ^ i, i, rfl⟩ = i := h.zmod_equiv_zpowers_symm_apply_pow i variables [is_domain R] lemma zpowers_eq {k : ℕ+} {ζ : Rˣ} (h : is_primitive_root ζ k) : subgroup.zpowers ζ = roots_of_unity k R := begin apply set_like.coe_injective, haveI F : fintype (subgroup.zpowers ζ) := fintype.of_equiv _ (h.zmod_equiv_zpowers).to_equiv, refine @set.eq_of_subset_of_card_le Rˣ (subgroup.zpowers ζ) (roots_of_unity k R) F (roots_of_unity.fintype R k) (subgroup.zpowers_le_of_mem $ show ζ ∈ roots_of_unity k R, from h.pow_eq_one) _, calc fintype.card (roots_of_unity k R) ≤ k : card_roots_of_unity R k ... = fintype.card (zmod k) : (zmod.card k).symm ... = fintype.card (subgroup.zpowers ζ) : fintype.card_congr (h.zmod_equiv_zpowers).to_equiv end lemma eq_pow_of_mem_roots_of_unity {k : ℕ+} {ζ ξ : Rˣ} (h : is_primitive_root ζ k) (hξ : ξ ∈ roots_of_unity k R) : ∃ (i : ℕ) (hi : i < k), ζ ^ i = ξ := begin obtain ⟨n, rfl⟩ : ∃ n : ℤ, ζ ^ n = ξ, by rwa [← h.zpowers_eq] at hξ, have hk0 : (0 : ℤ) < k := by exact_mod_cast k.pos, let i := n % k, have hi0 : 0 ≤ i := int.mod_nonneg _ (ne_of_gt hk0), lift i to ℕ using hi0 with i₀ hi₀, refine ⟨i₀, _, _⟩, { zify, rw [hi₀], exact int.mod_lt_of_pos _ hk0 }, { have aux := h.zpow_eq_one, rw [← coe_coe] at aux, rw [← zpow_coe_nat, hi₀, ← int.mod_add_div n k, zpow_add, zpow_mul, aux, one_zpow, mul_one] } end lemma eq_pow_of_pow_eq_one {k : ℕ} {ζ ξ : R} (h : is_primitive_root ζ k) (hξ : ξ ^ k = 1) (h0 : 0 < k) : ∃ i < k, ζ ^ i = ξ := begin lift ζ to Rˣ using h.is_unit h0, lift ξ to Rˣ using is_unit_of_pow_eq_one hξ h0.ne', lift k to ℕ+ using h0, simp only [← units.coe_pow, ← units.ext_iff], rw coe_units_iff at h, apply h.eq_pow_of_mem_roots_of_unity, rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hξ, units.coe_one] end lemma is_primitive_root_iff' {k : ℕ+} {ζ ξ : Rˣ} (h : is_primitive_root ζ k) : is_primitive_root ξ k ↔ ∃ (i < (k : ℕ)) (hi : i.coprime k), ζ ^ i = ξ := begin split, { intro hξ, obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_mem_roots_of_unity hξ.pow_eq_one, rw h.pow_iff_coprime k.pos at hξ, exact ⟨i, hik, hξ, rfl⟩ }, { rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi } end lemma is_primitive_root_iff {k : ℕ} {ζ ξ : R} (h : is_primitive_root ζ k) (h0 : 0 < k) : is_primitive_root ξ k ↔ ∃ (i < k) (hi : i.coprime k), ζ ^ i = ξ := begin split, { intro hξ, obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_pow_eq_one hξ.pow_eq_one h0, rw h.pow_iff_coprime h0 at hξ, exact ⟨i, hik, hξ, rfl⟩ }, { rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi } end lemma card_roots_of_unity' {n : ℕ+} (h : is_primitive_root ζ n) : fintype.card (roots_of_unity n R) = n := begin let e := h.zmod_equiv_zpowers, haveI F : fintype (subgroup.zpowers ζ) := fintype.of_equiv _ e.to_equiv, calc fintype.card (roots_of_unity n R) = fintype.card (subgroup.zpowers ζ) : fintype.card_congr $ by rw h.zpowers_eq ... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm ... = n : zmod.card n end lemma card_roots_of_unity {ζ : R} {n : ℕ+} (h : is_primitive_root ζ n) : fintype.card (roots_of_unity n R) = n := begin obtain ⟨ζ, hζ⟩ := h.is_unit n.pos, rw [← hζ, is_primitive_root.coe_units_iff] at h, exact h.card_roots_of_unity' end /-- The cardinality of the multiset `nth_roots ↑n (1 : R)` is `n` if there is a primitive root of unity in `R`. -/ lemma card_nth_roots {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (nth_roots n (1 : R)).card = n := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, multiset.card_zero, nth_roots_zero] }, rw eq_iff_le_not_lt, use card_nth_roots n 1, { rw [not_lt], have hcard : fintype.card {x // x ∈ nth_roots n (1 : R)} ≤ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.dedup_le _), rw multiset.card_attach at hcard, rw ← pnat.to_pnat'_coe hpos at hcard h ⊢, set m := nat.to_pnat' n, rw [← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard, exact hcard } end /-- The multiset `nth_roots ↑n (1 : R)` has no repeated elements if there is a primitive root of unity in `R`. -/ lemma nth_roots_nodup {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (nth_roots n (1 : R)).nodup := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, multiset.nodup_zero, nth_roots_zero] }, apply (@multiset.dedup_eq_self R _ _).1, rw eq_iff_le_not_lt, split, { exact multiset.dedup_le (nth_roots n (1 : R)) }, { by_contra ha, replace ha := multiset.card_lt_of_lt ha, rw card_nth_roots h at ha, have hrw : (nth_roots n (1 : R)).dedup.card = fintype.card {x // x ∈ (nth_roots n (1 : R))}, { set fs := (⟨(nth_roots n (1 : R)).dedup, multiset.nodup_dedup _⟩ : finset R), rw [← finset.card_mk, ← fintype.card_of_subtype fs _], intro x, simp only [multiset.mem_dedup, finset.mem_mk] }, rw ← pnat.to_pnat'_coe hpos at h hrw ha, set m := nat.to_pnat' n, rw [hrw, ← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at ha, exact nat.lt_asymm ha ha } end @[simp] lemma card_nth_roots_finset {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (nth_roots_finset n R).card = n := by rw [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots] open_locale nat /-- If an integral domain has a primitive `k`-th root of unity, then it has `φ k` of them. -/ lemma card_primitive_roots {ζ : R} {k : ℕ} (h : is_primitive_root ζ k) : (primitive_roots k R).card = φ k := begin by_cases h0 : k = 0, { simp [h0], }, symmetry, refine finset.card_congr (λ i _, ζ ^ i) _ _ _, { simp only [true_and, and_imp, mem_filter, mem_range, mem_univ], rintro i - hi, rw mem_primitive_roots (nat.pos_of_ne_zero h0), exact h.pow_of_coprime i hi.symm }, { simp only [true_and, and_imp, mem_filter, mem_range, mem_univ], rintro i j hi - hj - H, exact h.pow_inj hi hj H }, { simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ], intros ξ hξ, rw [mem_primitive_roots (nat.pos_of_ne_zero h0), h.is_primitive_root_iff (nat.pos_of_ne_zero h0)] at hξ, rcases hξ with ⟨i, hin, hi, H⟩, exact ⟨i, ⟨hin, hi.symm⟩, H⟩ } end /-- The sets `primitive_roots k R` are pairwise disjoint. -/ lemma disjoint {k l : ℕ} (h : k ≠ l) : disjoint (primitive_roots k R) (primitive_roots l R) := finset.disjoint_left.2 $ λ z hk hl, h $ (is_primitive_root_of_mem_primitive_roots hk).unique $ is_primitive_root_of_mem_primitive_roots hl /-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n` if there is a primitive root of unity in `R`. This holds for any `nat`, not just `pnat`, see `nth_roots_one_eq_bUnion_primitive_roots`. -/ lemma nth_roots_one_eq_bUnion_primitive_roots' {ζ : R} {n : ℕ+} (h : is_primitive_root ζ n) : nth_roots_finset n R = (nat.divisors ↑n).bUnion (λ i, (primitive_roots i R)) := begin symmetry, apply finset.eq_of_subset_of_card_le, { intros x, simp only [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), exists_prop, finset.mem_bUnion, finset.mem_filter, finset.mem_range, mem_nth_roots, finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff], rintro ⟨a, ⟨d, hd⟩, ha⟩, have hazero : 0 < a, { contrapose! hd with ha0, simp only [nonpos_iff_eq_zero, zero_mul, *] at *, exact n.ne_zero }, rw mem_primitive_roots hazero at ha, rw [hd, pow_mul, ha.pow_eq_one, one_pow] }, { apply le_of_eq, rw [h.card_nth_roots_finset, finset.card_bUnion], { nth_rewrite_lhs 0 ← nat.sum_totient n, refine sum_congr rfl _, simp only [nat.mem_divisors], rintro k ⟨⟨d, hd⟩, -⟩, rw mul_comm at hd, rw (h.pow n.pos hd).card_primitive_roots }, { intros i hi j hj hdiff, exact disjoint hdiff } } end /-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n` if there is a primitive root of unity in `R`. -/ lemma nth_roots_one_eq_bUnion_primitive_roots {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : nth_roots_finset n R = (nat.divisors n).bUnion (λ i, (primitive_roots i R)) := begin by_cases hn : n = 0, { simp [hn], }, exact @nth_roots_one_eq_bUnion_primitive_roots' _ _ _ _ ⟨n, nat.pos_of_ne_zero hn⟩ h end end is_domain section minpoly open minpoly section comm_ring variables {n : ℕ} {K : Type*} [comm_ring K] {μ : K} (h : is_primitive_root μ n) (hpos : 0 < n) include n μ h hpos /--`μ` is integral over `ℤ`. -/ lemma is_integral : is_integral ℤ μ := begin use (X ^ n - 1), split, { exact (monic_X_pow_sub_C 1 (ne_of_lt hpos).symm) }, { simp only [((is_primitive_root.iff_def μ n).mp h).left, eval₂_one, eval₂_X_pow, eval₂_sub, sub_self] } end section is_domain variables [is_domain K] [char_zero K] omit hpos /--The minimal polynomial of a root of unity `μ` divides `X ^ n - 1`. -/ lemma minpoly_dvd_X_pow_sub_one : minpoly ℤ μ ∣ X ^ n - 1 := begin rcases n.eq_zero_or_pos with rfl | hpos, { simp }, letI : is_integrally_closed ℤ := gcd_monoid.to_is_integrally_closed, apply minpoly.is_integrally_closed_dvd (is_integral h hpos), simp only [((is_primitive_root.iff_def μ n).mp h).left, aeval_X_pow, eq_int_cast, int.cast_one, aeval_one, alg_hom.map_sub, sub_self] end /-- The reduction modulo `p` of the minimal polynomial of a root of unity `μ` is separable. -/ lemma separable_minpoly_mod {p : ℕ} [fact p.prime] (hdiv : ¬p ∣ n) : separable (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) := begin have hdvd : (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) ∣ X ^ n - 1, { simpa [polynomial.map_pow, map_X, polynomial.map_one, polynomial.map_sub] using ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p))) (minpoly_dvd_X_pow_sub_one h) }, refine separable.of_dvd (separable_X_pow_sub_C 1 _ one_ne_zero) hdvd, by_contra hzero, exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 hzero) end /-- The reduction modulo `p` of the minimal polynomial of a root of unity `μ` is squarefree. -/ lemma squarefree_minpoly_mod {p : ℕ} [fact p.prime] (hdiv : ¬ p ∣ n) : squarefree (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) := (separable_minpoly_mod h hdiv).squarefree /- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of `μ ^ p`, where `p` is a natural number that does not divide `n`. Then `P` divides `expand ℤ p Q`. -/ lemma minpoly_dvd_expand {p : ℕ} (hdiv : ¬ p ∣ n) : minpoly ℤ μ ∣ expand ℤ p (minpoly ℤ (μ ^ p)) := begin rcases n.eq_zero_or_pos with rfl | hpos, { simp * at *, }, letI : is_integrally_closed ℤ := gcd_monoid.to_is_integrally_closed, refine minpoly.is_integrally_closed_dvd (h.is_integral hpos) _, { rw [aeval_def, coe_expand, ← comp, eval₂_eq_eval_map, map_comp, polynomial.map_pow, map_X, eval_comp, eval_pow, eval_X, ← eval₂_eq_eval_map, ← aeval_def], exact minpoly.aeval _ _ } end /- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of `μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q ^ p` modulo `p`. -/ lemma minpoly_dvd_pow_mod {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) : map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ) ∣ map (int.cast_ring_hom (zmod p)) (minpoly ℤ (μ ^ p)) ^ p := begin set Q := minpoly ℤ (μ ^ p), have hfrob : map (int.cast_ring_hom (zmod p)) Q ^ p = map (int.cast_ring_hom (zmod p)) (expand ℤ p Q), by rw [← zmod.expand_card, map_expand], rw [hfrob], apply ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p))), exact minpoly_dvd_expand h hdiv end /- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of `μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q` modulo `p`. -/ lemma minpoly_dvd_mod_p {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) : map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ) ∣ map (int.cast_ring_hom (zmod p)) (minpoly ℤ (μ ^ p)) := (unique_factorization_monoid.dvd_pow_iff_dvd_of_squarefree (squarefree_minpoly_mod h hdiv) hprime.1.ne_zero).1 (minpoly_dvd_pow_mod h hdiv) /-- If `p` is a prime that does not divide `n`, then the minimal polynomials of a primitive `n`-th root of unity `μ` and of `μ ^ p` are the same. -/ lemma minpoly_eq_pow {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) : minpoly ℤ μ = minpoly ℤ (μ ^ p) := begin by_cases hn : n = 0, { simp * at *, }, have hpos := nat.pos_of_ne_zero hn, by_contra hdiff, set P := minpoly ℤ μ, set Q := minpoly ℤ (μ ^ p), have Pmonic : P.monic := minpoly.monic (h.is_integral hpos), have Qmonic : Q.monic := minpoly.monic ((h.pow_of_prime hprime.1 hdiv).is_integral hpos), have Pirr : irreducible P := minpoly.irreducible (h.is_integral hpos), have Qirr : irreducible Q := minpoly.irreducible ((h.pow_of_prime hprime.1 hdiv).is_integral hpos), have PQprim : is_primitive (P * Q) := Pmonic.is_primitive.mul Qmonic.is_primitive, have prod : P * Q ∣ X ^ n - 1, { rw [(is_primitive.int.dvd_iff_map_cast_dvd_map_cast (P * Q) (X ^ n - 1) PQprim (monic_X_pow_sub_C (1 : ℤ) (ne_of_gt hpos)).is_primitive), polynomial.map_mul], refine is_coprime.mul_dvd _ _ _, { have aux := is_primitive.int.irreducible_iff_irreducible_map_cast Pmonic.is_primitive, refine (dvd_or_coprime _ _ (aux.1 Pirr)).resolve_left _, rw map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Pmonic, intro hdiv, refine hdiff (eq_of_monic_of_associated Pmonic Qmonic _), exact associated_of_dvd_dvd hdiv (Pirr.dvd_symm Qirr hdiv) }, { apply (map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Pmonic).2, exact minpoly_dvd_X_pow_sub_one h }, { apply (map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Qmonic).2, exact minpoly_dvd_X_pow_sub_one (pow_of_prime h hprime.1 hdiv) } }, replace prod := ring_hom.map_dvd ((map_ring_hom (int.cast_ring_hom (zmod p)))) prod, rw [coe_map_ring_hom, polynomial.map_mul, polynomial.map_sub, polynomial.map_one, polynomial.map_pow, map_X] at prod, obtain ⟨R, hR⟩ := minpoly_dvd_mod_p h hdiv, rw [hR, ← mul_assoc, ← polynomial.map_mul, ← sq, polynomial.map_pow] at prod, have habs : map (int.cast_ring_hom (zmod p)) P ^ 2 ∣ map (int.cast_ring_hom (zmod p)) P ^ 2 * R, { use R }, replace habs := lt_of_lt_of_le (part_enat.coe_lt_coe.2 one_lt_two) (multiplicity.le_multiplicity_of_pow_dvd (dvd_trans habs prod)), have hfree : squarefree (X ^ n - 1 : (zmod p)[X]), { exact (separable_X_pow_sub_C 1 (λ h, hdiv $ (zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 h) one_ne_zero).squarefree }, cases (multiplicity.squarefree_iff_multiplicity_le_one (X ^ n - 1)).1 hfree (map (int.cast_ring_hom (zmod p)) P) with hle hunit, { rw nat.cast_one at habs, exact hle.not_lt habs }, { replace hunit := degree_eq_zero_of_is_unit hunit, rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom (zmod p)) _ at hunit, { exact (minpoly.degree_pos (is_integral h hpos)).ne' hunit }, simp only [Pmonic, eq_int_cast, monic.leading_coeff, int.cast_one, ne.def, not_false_iff, one_ne_zero] } end /-- If `m : ℕ` is coprime with `n`, then the minimal polynomials of a primitive `n`-th root of unity `μ` and of `μ ^ m` are the same. -/ lemma minpoly_eq_pow_coprime {m : ℕ} (hcop : nat.coprime m n) : minpoly ℤ μ = minpoly ℤ (μ ^ m) := begin revert n hcop, refine unique_factorization_monoid.induction_on_prime m _ _ _, { intros n hn h, congr, simpa [(nat.coprime_zero_left n).mp hn] using h }, { intros u hunit n hcop h, congr, simp [nat.is_unit_iff.mp hunit] }, { intros a p ha hprime hind n hcop h, rw hind (nat.coprime.coprime_mul_left hcop) h, clear hind, replace hprime := hprime.nat_prime, have hdiv := (nat.prime.coprime_iff_not_dvd hprime).1 (nat.coprime.coprime_mul_right hcop), haveI := fact.mk hprime, rw [minpoly_eq_pow (h.pow_of_coprime a (nat.coprime.coprime_mul_left hcop)) hdiv], congr' 1, ring_exp } end /-- If `m : ℕ` is coprime with `n`, then the minimal polynomial of a primitive `n`-th root of unity `μ` has `μ ^ m` as root. -/ lemma pow_is_root_minpoly {m : ℕ} (hcop : nat.coprime m n) : is_root (map (int.cast_ring_hom K) (minpoly ℤ μ)) (μ ^ m) := by simpa [minpoly_eq_pow_coprime h hcop, eval_map, aeval_def (μ ^ m) _] using minpoly.aeval ℤ (μ ^ m) /-- `primitive_roots n K` is a subset of the roots of the minimal polynomial of a primitive `n`-th root of unity `μ`. -/ lemma is_roots_of_minpoly : primitive_roots n K ⊆ (map (int.cast_ring_hom K) (minpoly ℤ μ)).roots.to_finset := begin by_cases hn : n = 0, { simp * at *, }, have hpos := nat.pos_of_ne_zero hn, intros x hx, obtain ⟨m, hle, hcop, rfl⟩ := (is_primitive_root_iff h hpos).1 ((mem_primitive_roots hpos).1 hx), simpa [multiset.mem_to_finset, mem_roots (map_monic_ne_zero $ minpoly.monic $ is_integral h hpos)] using pow_is_root_minpoly h hcop end /-- The degree of the minimal polynomial of `μ` is at least `totient n`. -/ lemma totient_le_degree_minpoly : nat.totient n ≤ (minpoly ℤ μ).nat_degree := let P : ℤ[X] := minpoly ℤ μ,-- minimal polynomial of `μ` P_K : K[X] := map (int.cast_ring_hom K) P -- minimal polynomial of `μ` sent to `K[X]` in calc n.totient = (primitive_roots n K).card : h.card_primitive_roots.symm ... ≤ P_K.roots.to_finset.card : finset.card_le_of_subset (is_roots_of_minpoly h) ... ≤ P_K.roots.card : multiset.to_finset_card_le _ ... ≤ P_K.nat_degree : card_roots' _ ... ≤ P.nat_degree : nat_degree_map_le _ _ end is_domain end comm_ring end minpoly section automorphisms variables {S} [comm_ring S] [is_domain S] {μ : S} {n : ℕ+} (hμ : is_primitive_root μ n) (R) [comm_ring R] [algebra R S] /-- The `monoid_hom` that takes an automorphism to the power of μ that μ gets mapped to under it. -/ noncomputable def aut_to_pow : (S ≃ₐ[R] S) →* (zmod n)ˣ := let μ' := hμ.to_roots_of_unity in have ho : order_of μ' = n := by rw [hμ.eq_order_of, ←hμ.coe_to_roots_of_unity_coe, order_of_units, order_of_subgroup], monoid_hom.to_hom_units { to_fun := λ σ, (map_root_of_unity_eq_pow_self σ.to_alg_hom μ').some, map_one' := begin generalize_proofs h1, have h := h1.some_spec, dsimp only [alg_equiv.one_apply, alg_equiv.to_ring_equiv_eq_coe, ring_equiv.to_ring_hom_eq_coe, ring_equiv.coe_to_ring_hom, alg_equiv.coe_ring_equiv] at *, replace h : μ' = μ' ^ h1.some := roots_of_unity.coe_injective (by simpa only [roots_of_unity.coe_pow] using h), rw ←pow_one μ' at h {occs := occurrences.pos [1]}, rw [←@nat.cast_one $ zmod n, zmod.nat_coe_eq_nat_coe_iff, ←ho, ←pow_eq_pow_iff_modeq μ', h] end, map_mul' := begin generalize_proofs hxy' hx' hy', have hxy := hxy'.some_spec, have hx := hx'.some_spec, have hy := hy'.some_spec, dsimp only [alg_equiv.to_ring_equiv_eq_coe, ring_equiv.to_ring_hom_eq_coe, ring_equiv.coe_to_ring_hom, alg_equiv.coe_ring_equiv, alg_equiv.mul_apply] at *, replace hxy : x (↑μ' ^ hy'.some) = ↑μ' ^ hxy'.some := hy ▸ hxy, rw x.map_pow at hxy, replace hxy : ((μ' : S) ^ hx'.some) ^ hy'.some = μ' ^ hxy'.some := hx ▸ hxy, rw ←pow_mul at hxy, replace hxy : μ' ^ (hx'.some * hy'.some) = μ' ^ hxy'.some := roots_of_unity.coe_injective (by simpa only [roots_of_unity.coe_pow] using hxy), rw [←nat.cast_mul, zmod.nat_coe_eq_nat_coe_iff, ←ho, ←pow_eq_pow_iff_modeq μ', hxy] end } -- We are not using @[simps] in aut_to_pow to avoid a timeout. lemma coe_aut_to_pow_apply (f : S ≃ₐ[R] S) : (aut_to_pow R hμ f : zmod n) = ((map_root_of_unity_eq_pow_self f hμ.to_roots_of_unity).some : zmod n) := rfl @[simp] lemma aut_to_pow_spec (f : S ≃ₐ[R] S) : μ ^ (hμ.aut_to_pow R f : zmod n).val = f μ := begin rw is_primitive_root.coe_aut_to_pow_apply, generalize_proofs h, have := h.some_spec, dsimp only [alg_equiv.to_alg_hom_eq_coe, alg_equiv.coe_alg_hom] at this, refine (_ : ↑hμ.to_roots_of_unity ^ _ = _).trans this.symm, rw [←roots_of_unity.coe_pow, ←roots_of_unity.coe_pow], congr' 1, rw [pow_eq_pow_iff_modeq, ←order_of_subgroup, ←order_of_units, hμ.coe_to_roots_of_unity_coe, ←hμ.eq_order_of, zmod.val_nat_cast], exact nat.mod_modeq _ _ end end automorphisms end is_primitive_root
bba9a9ee5021dfee4a6d1525ada4d965765156bf
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world2/level5.lean
a63d281e9d00e210c23d16f2a85377aaa787e8b1
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
1,012
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level4 -- hide namespace mynat -- hide /- Axiom : one_eq_succ_zero 1 = succ(0) -/ /- # Addition World ## Level 5: `succ_eq_add_one` I've just added `one_eq_succ_zero` (a proof of `1 = succ(0)`) to your list of theorems; this is true by definition of $1$, but we didn't need it until now. Levels 5 and 6 are the two last levels in Addition World. Level 5 involves the number $1$. When you see a $1$ in your goal, you can write `rw one_eq_succ_zero` to get back to something which only mentions `0`. This is a good move because $0$ is easier for us to manipulate than $1$ right now, because we have some theorems about $0$ (`zero_add`, `add_zero`), but, other than `1 = succ(0)`, no theorems at all which mention $1$. Let's prove one now. -/ /- Theorem For any natural number $n$, we have $$ \operatorname{succ}(n) = n+1. $$ -/ theorem succ_eq_add_one (n : mynat) : succ n = n + 1 := begin [nat_num_game] end end mynat -- hide
40413370c10918c81761b21154f181bd247d6a54
fe84e287c662151bb313504482b218a503b972f3
/src/exercises/loh/exercises_1.lean
ab465bd1bdd6c8610d09a2c0bc0ae39480d11bd2
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
3,693
lean
import data.real.basic data.real.sqrt tactic.fin_cases namespace loh lemma real_sq_nonneg (x : ℝ) : 0 ≤ x ^ 2 := sq_nonneg x lemma no_nat_half : ¬ ∃ (n : ℕ), 2 * n = 1 := begin rintro ⟨n,hn⟩, cases n; cases hn end lemma int_domain : ∀ (n m : ℤ), n * m = 0 → n = 0 ∨ m = 0 := λ n m h, int.eq_zero_or_eq_zero_of_mul_eq_zero h section root_set variables {u : ℝ} (hu : u > 0) include hu def root_set := { x : ℝ | x ^ 2 = u } noncomputable def pos_root : root_set hu := ⟨real.sqrt u, real.sq_sqrt (le_of_lt hu)⟩ noncomputable def neg_root : root_set hu := ⟨- real.sqrt u, by { change _ ^ 2 = _, rw[neg_sq, real.sq_sqrt (le_of_lt hu)] } ⟩ lemma coe_pos_root : ((pos_root hu) : ℝ) = real.sqrt u := rfl lemma coe_neg_root : ((neg_root hu) : ℝ) = - real.sqrt u := rfl lemma pos_root_pos : (pos_root hu : ℝ) > 0 := real.sqrt_pos.mpr hu lemma neg_root_neg : ¬ ((neg_root hu : ℝ) > 0) := not_lt_of_gt (neg_neg_of_pos (pos_root_pos hu)) lemma sq_pos_root : (pos_root hu : ℝ) ^ 2 = u := (pos_root hu).property lemma sq_neg_root : (neg_root hu : ℝ) ^ 2 = u := (neg_root hu).property noncomputable def root_set_to : root_set hu → fin 2 := λ u, ite ((u : ℝ) > 0) 0 1 noncomputable def root_set_of : fin 2 → root_set hu := λ i, ite (i = 0) (pos_root hu) (neg_root hu) lemma coe_root_set_of : ∀ (i : fin 2), ((root_set_of hu i) : ℝ) = ite (i = 0) (real.sqrt u) (- real.sqrt u) := begin intro i, dsimp[root_set_of], split_ifs; refl end lemma root_set_els : ∀ (x : root_set hu), x = (pos_root hu) ∨ x = (neg_root hu) := begin rintro ⟨x, hx : x ^ 2 = u⟩, let v := real.sqrt u, rw[← sub_eq_zero, ← (real.sq_sqrt (le_of_lt hu))] at hx, have : x ^ 2 - v ^ 2 = (x - v) * (x - (- v)) := by ring, rw[this] at hx, replace hx := eq_zero_or_eq_zero_of_mul_eq_zero hx, rw[sub_eq_zero, sub_eq_zero] at hx, cases hx with hx hx, { left , ext, change x = _, rw[coe_pos_root], exact hx }, { right, ext, change x = _, rw[coe_neg_root], exact hx } end noncomputable def root_set.equiv : (root_set hu) ≃ (fin 2) := { to_fun := root_set_to hu, inv_fun := root_set_of hu, left_inv := begin intro x, cases root_set_els hu x with hp hn, { rw[hp], dsimp[root_set_to], rw[if_pos (pos_root_pos hu)], refl }, { rw[hn], dsimp[root_set_to], rw[if_neg (neg_root_neg hu)], refl } end, right_inv := begin intro i, dsimp[root_set_of], split_ifs, { dsimp[root_set_to], rw[h, if_pos (pos_root_pos hu)] }, { dsimp[root_set_to], rw[if_neg (neg_root_neg hu)], fin_cases i; trivial, } end } end root_set example (x : ℝ) (h : 0 ≤ x) : (abs x) = x := by library_search noncomputable def abs_cases (x : ℝ) : { u : ℝ // 0 ≤ u ∧ (abs x) = u ∧ (x = u ∨ x = -u) } := begin use (abs x), split, exact (abs_nonneg x), split, refl, by_cases h : x ≥ 0, {left, rw[abs_eq_self.mpr h]}, {right, rw[abs_eq_neg_self.mpr (le_of_not_ge h), neg_neg] } end lemma tri_ineq (x y : ℝ) : abs (x + y) ≤ (abs x) + (abs y) := sorry def conv_to (x : ℕ → ℝ) (a : ℝ) : Prop := ∀ (ε : ℝ) (hε : ε > 0), ∃ (N : ℕ), ∀ (n : ℕ), n ≥ N → (abs (x n - a) < ε) section jective variables {X Y : Type*} (f : X → Y) def inj (f : X → Y) := ∀ x₀ x₁ : X, f x₀ = f x₁ → x₀ = x₁ def surj (f : X → Y) := ∀ y : Y, ∃ x : X, f x = y def bij (f : X → Y) := (inj f) ∧ (surj f) lemma inj_of_bij {f : X → Y} (h : bij f) : inj f := h.1 lemma surj_of_bij {f : X → Y} (h : bij f) : surj f := h.2 lemma bij_of_inj_of_surj {f : X → Y} (hi : inj f) (hs : surj f) : bij f := ⟨hi, hs⟩ end jective end loh
ce56684d658370acbbddeac8e9be74120514c143
1dd482be3f611941db7801003235dc84147ec60a
/src/logic/function.lean
fd63b8415caa5cec6b8047d587c69bce482a7d08
[ "Apache-2.0" ]
permissive
sanderdahmen/mathlib
479039302bd66434bb5672c2a4cecf8d69981458
8f0eae75cd2d8b7a083cf935666fcce4565df076
refs/heads/master
1,587,491,322,775
1,549,672,060,000
1,549,672,060,000
169,748,224
0
0
Apache-2.0
1,549,636,694,000
1,549,636,694,000
null
UTF-8
Lean
false
false
8,879
lean
/- Copyright (c) 2016 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Miscellaneous function constructions and lemmas. -/ import logic.basic data.option.defs universes u v w namespace function section variables {α : Sort u} {β : Sort v} {f : α → β} lemma hfunext {α α': Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : Πa, β a} {f' : Πa, β' a} (hα : α = α') (h : ∀a a', a == a' → f a == f' a') : f == f' := begin subst hα, have : ∀a, f a == f' a, { intro a, exact h a a (heq.refl a) }, have : β = β', { funext a, exact type_eq_of_heq (this a) }, subst this, apply heq_of_eq, funext a, exact eq_of_heq (this a) end lemma funext_iff {β : α → Sort*} {f₁ f₂ : Π (x : α), β x} : f₁ = f₂ ↔ (∀a, f₁ a = f₂ a) := iff.intro (assume h a, h ▸ rfl) funext lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : (f ∘ g) a = f (g a) := rfl @[simp] theorem injective.eq_iff (I : injective f) {a b : α} : f a = f b ↔ a = b := ⟨@I _ _, congr_arg f⟩ def injective.decidable_eq [decidable_eq β] (I : injective f) : decidable_eq α | a b := decidable_of_iff _ I.eq_iff instance decidable_eq_pfun (p : Prop) [decidable p] (α : p → Type*) [Π hp, decidable_eq (α hp)] : decidable_eq (Π hp, α hp) | f g := decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm theorem cantor_surjective {α} (f : α → α → Prop) : ¬ function.surjective f | h := let ⟨D, e⟩ := h (λ a, ¬ f a a) in (iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D) theorem cantor_injective {α : Type*} (f : (α → Prop) → α) : ¬ function.injective f | i := cantor_surjective (λ a b, ∀ U, a = f U → U b) $ surjective_of_has_right_inverse ⟨f, λ U, funext $ λ a, propext ⟨λ h, h U rfl, λ h' U' e, i e ▸ h'⟩⟩ /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def is_partial_inv {α β} (f : α → β) (g : β → option α) : Prop := ∀ x y, g y = some x ↔ f x = y theorem is_partial_inv_left {α β} {f : α → β} {g} (H : is_partial_inv f g) (x) : g (f x) = some x := (H _ _).2 rfl theorem injective_of_partial_inv {α β} {f : α → β} {g} (H : is_partial_inv f g) : injective f := λ a b h, option.some.inj $ ((H _ _).2 h).symm.trans ((H _ _).2 rfl) theorem injective_of_partial_inv_right {α β} {f : α → β} {g} (H : is_partial_inv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂) theorem left_inverse.comp_eq_id {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id := funext h theorem right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h theorem left_inverse.comp {γ} {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := assume a, show h (f (g (i a))) = a, by rw [hf (i a), hh a] theorem right_inverse.comp {γ} {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf local attribute [instance] classical.prop_decidable /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ noncomputable def partial_inv {α β} (f : α → β) (b : β) : option α := if h : ∃ a, f a = b then some (classical.some h) else none theorem partial_inv_of_injective {α β} {f : α → β} (I : injective f) : is_partial_inv f (partial_inv f) | a b := ⟨λ h, if h' : ∃ a, f a = b then begin rw [partial_inv, dif_pos h'] at h, injection h with h, subst h, apply classical.some_spec h' end else by rw [partial_inv, dif_neg h'] at h; contradiction, λ e, e ▸ have h : ∃ a', f a' = f a, from ⟨_, rfl⟩, (dif_pos h).trans (congr_arg _ (I $ classical.some_spec h))⟩ theorem partial_inv_left {α β} {f : α → β} (I : injective f) : ∀ x, partial_inv f (f x) = some x := is_partial_inv_left (partial_inv_of_injective I) end section inv_fun variables {α : Type u} [inhabited α] {β : Sort v} {f : α → β} {s : set α} {a : α} {b : β} local attribute [instance] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else default α theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_eq' (h : ∀ x y ∈ s, f x = f y → x = y) (ha : a ∈ s) : inv_fun_on f s (f a) = a := have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩, h _ _ (inv_fun_on_mem this) ha (inv_fun_on_eq this) theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = default α := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ noncomputable def inv_fun (f : α → β) : β → α := inv_fun_on f set.univ theorem inv_fun_eq (h : ∃a, f a = b) : f (inv_fun f b) = b := inv_fun_on_eq $ let ⟨a, ha⟩ := h in ⟨a, trivial, ha⟩ theorem inv_fun_eq_of_injective_of_right_inverse {g : β → α} (hf : injective f) (hg : right_inverse g f) : inv_fun f = g := funext $ assume b, hf begin rw [hg b], exact inv_fun_eq ⟨g b, hg b⟩ end lemma right_inverse_inv_fun (hf : surjective f) : right_inverse (inv_fun f) f := assume b, inv_fun_eq $ hf b lemma left_inverse_inv_fun (hf : injective f) : left_inverse (inv_fun f) f := assume b, have f (inv_fun f (f b)) = f b, from inv_fun_eq ⟨b, rfl⟩, hf this lemma inv_fun_surjective (hf : injective f) : surjective (inv_fun f) := surjective_of_has_right_inverse ⟨_, left_inverse_inv_fun hf⟩ lemma inv_fun_comp (hf : injective f) : inv_fun f ∘ f = id := funext $ left_inverse_inv_fun hf lemma injective.has_left_inverse (hf : injective f) : has_left_inverse f := ⟨inv_fun f, left_inverse_inv_fun hf⟩ lemma injective_iff_has_left_inverse : injective f ↔ has_left_inverse f := ⟨injective.has_left_inverse, injective_of_has_left_inverse⟩ end inv_fun section surj_inv variables {α : Sort u} {β : Sort v} {f : α → β} /-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require `α` to be inhabited.) -/ noncomputable def surj_inv {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b) lemma surj_inv_eq (h : surjective f) (b) : f (surj_inv h b) = b := classical.some_spec (h b) lemma right_inverse_surj_inv (hf : surjective f) : right_inverse (surj_inv hf) f := surj_inv_eq hf lemma left_inverse_surj_inv (hf : bijective f) : left_inverse (surj_inv hf.2) f := right_inverse_of_injective_of_left_inverse hf.1 (right_inverse_surj_inv hf.2) lemma surjective.has_right_inverse (hf : surjective f) : has_right_inverse f := ⟨_, right_inverse_surj_inv hf⟩ lemma surjective_iff_has_right_inverse : surjective f ↔ has_right_inverse f := ⟨surjective.has_right_inverse, surjective_of_has_right_inverse⟩ lemma bijective_iff_has_inverse : bijective f ↔ ∃ g, left_inverse g f ∧ right_inverse g f := ⟨λ hf, ⟨_, left_inverse_surj_inv hf, right_inverse_surj_inv hf.2⟩, λ ⟨g, gl, gr⟩, ⟨injective_of_left_inverse gl, surjective_of_has_right_inverse ⟨_, gr⟩⟩⟩ lemma injective_surj_inv (h : surjective f) : injective (surj_inv h) := injective_of_has_left_inverse ⟨f, right_inverse_surj_inv h⟩ end surj_inv section update variables {α : Sort u} {β : α → Sort v} [decidable_eq α] def update (f : Πa, β a) (a' : α) (v : β a') (a : α) : β a := if h : a = a' then eq.rec v h.symm else f a @[simp] lemma update_same {a : α} {v : β a} {f : Πa, β a} : update f a v a = v := dif_pos rfl @[simp] lemma update_noteq {a a' : α} {v : β a'} {f : Πa, β a} (h : a ≠ a') : update f a' v a = f a := dif_neg h end update lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) := funext $ assume ⟨a, b⟩, rfl def restrict {α β} (f : α → β) (s : set α) : subtype s → β := λ x, f x.val theorem restrict_eq {α β} (f : α → β) (s : set α): function.restrict f s = f ∘ (@subtype.val _ s) := rfl end function
1f0ef416d0c41819826c187bd592f490cc41c178
367134ba5a65885e863bdc4507601606690974c1
/src/logic/function/basic.lean
d44085d245f05daeb4a7789d180832f26d1098a3
[ "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
25,387
lean
/- Copyright (c) 2016 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import logic.basic import data.option.defs /-! # Miscellaneous function constructions and lemmas -/ universes u v w namespace function section variables {α β γ : Sort*} {f : α → β} /-- Evaluate a function at an argument. Useful if you want to talk about the partially applied `function.eval x : (Π x, β x) → β x`. -/ @[reducible] def eval {β : α → Sort*} (x : α) (f : Π x, β x) : β x := f x @[simp] lemma eval_apply {β : α → Sort*} (x : α) (f : Π x, β x) : eval x f = f x := rfl lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : (f ∘ g) a = f (g a) := rfl lemma const_def {y : β} : (λ x : α, y) = const α y := rfl @[simp] lemma const_apply {y : β} {x : α} : const α y x = y := rfl @[simp] lemma const_comp {f : α → β} {c : γ} : const β c ∘ f = const α c := rfl @[simp] lemma comp_const {f : β → γ} {b : β} : f ∘ const α b = const α (f b) := rfl lemma id_def : @id α = λ x, x := rfl lemma hfunext {α α': Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : Πa, β a} {f' : Πa, β' a} (hα : α = α') (h : ∀a a', a == a' → f a == f' a') : f == f' := begin subst hα, have : ∀a, f a == f' a, { intro a, exact h a a (heq.refl a) }, have : β = β', { funext a, exact type_eq_of_heq (this a) }, subst this, apply heq_of_eq, funext a, exact eq_of_heq (this a) end lemma funext_iff {β : α → Sort*} {f₁ f₂ : Π (x : α), β x} : f₁ = f₂ ↔ (∀a, f₁ a = f₂ a) := iff.intro (assume h a, h ▸ rfl) funext protected lemma bijective.injective {f : α → β} (hf : bijective f) : injective f := hf.1 protected lemma bijective.surjective {f : α → β} (hf : bijective f) : surjective f := hf.2 theorem injective.eq_iff (I : injective f) {a b : α} : f a = f b ↔ a = b := ⟨@I _ _, congr_arg f⟩ theorem injective.eq_iff' (I : injective f) {a b : α} {c : β} (h : f b = c) : f a = c ↔ a = b := h ▸ I.eq_iff lemma injective.ne (hf : injective f) {a₁ a₂ : α} : a₁ ≠ a₂ → f a₁ ≠ f a₂ := mt (assume h, hf h) lemma injective.ne_iff (hf : injective f) {x y : α} : f x ≠ f y ↔ x ≠ y := ⟨mt $ congr_arg f, hf.ne⟩ lemma injective.ne_iff' (hf : injective f) {x y : α} {z : β} (h : f y = z) : f x ≠ z ↔ x ≠ y := h ▸ hf.ne_iff /-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then the domain `α` also has decidable equality. -/ def injective.decidable_eq [decidable_eq β] (I : injective f) : decidable_eq α := λ a b, decidable_of_iff _ I.eq_iff lemma injective.of_comp {g : γ → α} (I : injective (f ∘ g)) : injective g := λ x y h, I $ show f (g x) = f (g y), from congr_arg f h lemma injective.of_comp_iff {f : α → β} (hf : injective f) (g : γ → α) : injective (f ∘ g) ↔ injective g := ⟨injective.of_comp, hf.comp⟩ lemma injective.of_comp_iff' (f : α → β) {g : γ → α} (hg : bijective g) : injective (f ∘ g) ↔ injective f := ⟨ λ h x y, let ⟨x', hx⟩ := hg.surjective x, ⟨y', hy⟩ := hg.surjective y in hx ▸ hy ▸ λ hf, h hf ▸ rfl, λ h, h.comp hg.injective⟩ lemma injective.dite (p : α → Prop) [decidable_pred p] {f : {a : α // p a} → β} {f' : {a : α // ¬ p a} → β} (hf : injective f) (hf' : injective f') (im_disj : ∀ {x x' : α} {hx : p x} {hx' : ¬ p x'}, f ⟨x, hx⟩ ≠ f' ⟨x', hx'⟩) : function.injective (λ x, if h : p x then f ⟨x, h⟩ else f' ⟨x, h⟩) := λ x₁ x₂ h, begin dsimp only at h, by_cases h₁ : p x₁; by_cases h₂ : p x₂, { rw [dif_pos h₁, dif_pos h₂] at h, injection (hf h), }, { rw [dif_pos h₁, dif_neg h₂] at h, exact (im_disj h).elim, }, { rw [dif_neg h₁, dif_pos h₂] at h, exact (im_disj h.symm).elim, }, { rw [dif_neg h₁, dif_neg h₂] at h, injection (hf' h), }, end lemma surjective.of_comp {g : γ → α} (S : surjective (f ∘ g)) : surjective f := λ y, let ⟨x, h⟩ := S y in ⟨g x, h⟩ lemma surjective.of_comp_iff (f : α → β) {g : γ → α} (hg : surjective g) : surjective (f ∘ g) ↔ surjective f := ⟨surjective.of_comp, λ h, h.comp hg⟩ lemma surjective.of_comp_iff' {f : α → β} (hf : bijective f) (g : γ → α) : surjective (f ∘ g) ↔ surjective g := ⟨λ h x, let ⟨x', hx'⟩ := h (f x) in ⟨x', hf.injective hx'⟩, hf.surjective.comp⟩ instance decidable_eq_pfun (p : Prop) [decidable p] (α : p → Type*) [Π hp, decidable_eq (α hp)] : decidable_eq (Π hp, α hp) | f g := decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm theorem surjective.forall {f : α → β} (hf : surjective f) {p : β → Prop} : (∀ y, p y) ↔ ∀ x, p (f x) := ⟨λ h x, h (f x), λ h y, let ⟨x, hx⟩ := hf y in hx ▸ h x⟩ theorem surjective.forall₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂) := hf.forall.trans $ forall_congr $ λ x, hf.forall theorem surjective.forall₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.forall.trans $ forall_congr $ λ x, hf.forall₂ theorem surjective.exists {f : α → β} (hf : surjective f) {p : β → Prop} : (∃ y, p y) ↔ ∃ x, p (f x) := ⟨λ ⟨y, hy⟩, let ⟨x, hx⟩ := hf y in ⟨x, hx.symm ▸ hy⟩, λ ⟨x, hx⟩, ⟨f x, hx⟩⟩ theorem surjective.exists₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂) := hf.exists.trans $ exists_congr $ λ x, hf.exists theorem surjective.exists₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.exists.trans $ exists_congr $ λ x, hf.exists₂ lemma bijective_iff_exists_unique (f : α → β) : bijective f ↔ ∀ b : β, ∃! (a : α), f a = b := ⟨ λ hf b, let ⟨a, ha⟩ := hf.surjective b in ⟨a, ha, λ a' ha', hf.injective (ha'.trans ha.symm)⟩, λ he, ⟨ λ a a' h, unique_of_exists_unique (he (f a')) h rfl, λ b, exists_of_exists_unique (he b) ⟩⟩ /-- Shorthand for using projection notation with `function.bijective_iff_exists_unique`. -/ lemma bijective.exists_unique {f : α → β} (hf : bijective f) (b : β) : ∃! (a : α), f a = b := (bijective_iff_exists_unique f).mp hf b lemma bijective.of_comp_iff (f : α → β) {g : γ → α} (hg : bijective g) : bijective (f ∘ g) ↔ bijective f := and_congr (injective.of_comp_iff' _ hg) (surjective.of_comp_iff _ hg.surjective) lemma bijective.of_comp_iff' {f : α → β} (hf : bijective f) (g : γ → α) : function.bijective (f ∘ g) ↔ function.bijective g := and_congr (injective.of_comp_iff hf.injective _) (surjective.of_comp_iff' hf _) /-- Cantor's diagonal argument implies that there are no surjective functions from `α` to `set α`. -/ theorem cantor_surjective {α} (f : α → set α) : ¬ function.surjective f | h := let ⟨D, e⟩ := h (λ a, ¬ f a a) in (iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D) /-- Cantor's diagonal argument implies that there are no injective functions from `set α` to `α`. -/ theorem cantor_injective {α : Type*} (f : (set α) → α) : ¬ function.injective f | i := cantor_surjective (λ a b, ∀ U, a = f U → U b) $ right_inverse.surjective (λ U, funext $ λ a, propext ⟨λ h, h U rfl, λ h' U' e, i e ▸ h'⟩) /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def is_partial_inv {α β} (f : α → β) (g : β → option α) : Prop := ∀ x y, g y = some x ↔ f x = y theorem is_partial_inv_left {α β} {f : α → β} {g} (H : is_partial_inv f g) (x) : g (f x) = some x := (H _ _).2 rfl theorem injective_of_partial_inv {α β} {f : α → β} {g} (H : is_partial_inv f g) : injective f := λ a b h, option.some.inj $ ((H _ _).2 h).symm.trans ((H _ _).2 rfl) theorem injective_of_partial_inv_right {α β} {f : α → β} {g} (H : is_partial_inv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂) theorem left_inverse.comp_eq_id {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id := funext h theorem left_inverse_iff_comp {f : α → β} {g : β → α} : left_inverse f g ↔ f ∘ g = id := ⟨left_inverse.comp_eq_id, congr_fun⟩ theorem right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h theorem right_inverse_iff_comp {f : α → β} {g : β → α} : right_inverse f g ↔ g ∘ f = id := ⟨right_inverse.comp_eq_id, congr_fun⟩ theorem left_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := assume a, show h (f (g (i a))) = a, by rw [hf (i a), hh a] theorem right_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf theorem left_inverse.right_inverse {f : α → β} {g : β → α} (h : left_inverse g f) : right_inverse f g := h theorem right_inverse.left_inverse {f : α → β} {g : β → α} (h : right_inverse g f) : left_inverse f g := h theorem left_inverse.surjective {f : α → β} {g : β → α} (h : left_inverse f g) : surjective f := h.right_inverse.surjective theorem right_inverse.injective {f : α → β} {g : β → α} (h : right_inverse f g) : injective f := h.left_inverse.injective theorem left_inverse.eq_right_inverse {f : α → β} {g₁ g₂ : β → α} (h₁ : left_inverse g₁ f) (h₂ : right_inverse g₂ f) : g₁ = g₂ := calc g₁ = g₁ ∘ f ∘ g₂ : by rw [h₂.comp_eq_id, comp.right_id] ... = g₂ : by rw [← comp.assoc, h₁.comp_eq_id, comp.left_id] local attribute [instance, priority 10] classical.prop_decidable /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ noncomputable def partial_inv {α β} (f : α → β) (b : β) : option α := if h : ∃ a, f a = b then some (classical.some h) else none theorem partial_inv_of_injective {α β} {f : α → β} (I : injective f) : is_partial_inv f (partial_inv f) | a b := ⟨λ h, if h' : ∃ a, f a = b then begin rw [partial_inv, dif_pos h'] at h, injection h with h, subst h, apply classical.some_spec h' end else by rw [partial_inv, dif_neg h'] at h; contradiction, λ e, e ▸ have h : ∃ a', f a' = f a, from ⟨_, rfl⟩, (dif_pos h).trans (congr_arg _ (I $ classical.some_spec h))⟩ theorem partial_inv_left {α β} {f : α → β} (I : injective f) : ∀ x, partial_inv f (f x) = some x := is_partial_inv_left (partial_inv_of_injective I) end section inv_fun variables {α : Type u} [n : nonempty α] {β : Sort v} {f : α → β} {s : set α} {a : α} {b : β} include n local attribute [instance, priority 10] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. For a computable version, see `function.injective.inv_of_mem_range`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else classical.choice n theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_eq' (h : ∀ (x ∈ s) (y ∈ s), f x = f y → x = y) (ha : a ∈ s) : inv_fun_on f s (f a) = a := have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩, h _ (inv_fun_on_mem this) _ ha (inv_fun_on_eq this) theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = classical.choice n := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ noncomputable def inv_fun (f : α → β) : β → α := inv_fun_on f set.univ theorem inv_fun_eq (h : ∃a, f a = b) : f (inv_fun f b) = b := inv_fun_on_eq $ let ⟨a, ha⟩ := h in ⟨a, trivial, ha⟩ lemma inv_fun_neg (h : ¬ ∃ a, f a = b) : inv_fun f b = classical.choice n := by refine inv_fun_on_neg (mt _ h); exact assume ⟨a, _, ha⟩, ⟨a, ha⟩ theorem inv_fun_eq_of_injective_of_right_inverse {g : β → α} (hf : injective f) (hg : right_inverse g f) : inv_fun f = g := funext $ assume b, hf begin rw [hg b], exact inv_fun_eq ⟨g b, hg b⟩ end lemma right_inverse_inv_fun (hf : surjective f) : right_inverse (inv_fun f) f := assume b, inv_fun_eq $ hf b lemma left_inverse_inv_fun (hf : injective f) : left_inverse (inv_fun f) f := assume b, have f (inv_fun f (f b)) = f b, from inv_fun_eq ⟨b, rfl⟩, hf this lemma inv_fun_surjective (hf : injective f) : surjective (inv_fun f) := (left_inverse_inv_fun hf).surjective lemma inv_fun_comp (hf : injective f) : inv_fun f ∘ f = id := funext $ left_inverse_inv_fun hf end inv_fun section inv_fun variables {α : Type u} [i : nonempty α] {β : Sort v} {f : α → β} include i lemma injective.has_left_inverse (hf : injective f) : has_left_inverse f := ⟨inv_fun f, left_inverse_inv_fun hf⟩ lemma injective_iff_has_left_inverse : injective f ↔ has_left_inverse f := ⟨injective.has_left_inverse, has_left_inverse.injective⟩ end inv_fun section surj_inv variables {α : Sort u} {β : Sort v} {f : α → β} /-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require `α` to be inhabited.) -/ noncomputable def surj_inv {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b) lemma surj_inv_eq (h : surjective f) (b) : f (surj_inv h b) = b := classical.some_spec (h b) lemma right_inverse_surj_inv (hf : surjective f) : right_inverse (surj_inv hf) f := surj_inv_eq hf lemma left_inverse_surj_inv (hf : bijective f) : left_inverse (surj_inv hf.2) f := right_inverse_of_injective_of_left_inverse hf.1 (right_inverse_surj_inv hf.2) lemma surjective.has_right_inverse (hf : surjective f) : has_right_inverse f := ⟨_, right_inverse_surj_inv hf⟩ lemma surjective_iff_has_right_inverse : surjective f ↔ has_right_inverse f := ⟨surjective.has_right_inverse, has_right_inverse.surjective⟩ lemma bijective_iff_has_inverse : bijective f ↔ ∃ g, left_inverse g f ∧ right_inverse g f := ⟨λ hf, ⟨_, left_inverse_surj_inv hf, right_inverse_surj_inv hf.2⟩, λ ⟨g, gl, gr⟩, ⟨gl.injective, gr.surjective⟩⟩ lemma injective_surj_inv (h : surjective f) : injective (surj_inv h) := (right_inverse_surj_inv h).injective end surj_inv section update variables {α : Sort u} {β : α → Sort v} {α' : Sort w} [decidable_eq α] [decidable_eq α'] /-- Replacing the value of a function at a given point by a given value. -/ def update (f : Πa, β a) (a' : α) (v : β a') (a : α) : β a := if h : a = a' then eq.rec v h.symm else f a /-- On non-dependent functions, `function.update` can be expressed as an `ite` -/ lemma update_apply {β : Sort*} (f : α → β) (a' : α) (b : β) (a : α) : update f a' b a = if a = a' then b else f a := begin dunfold update, congr, funext, rw eq_rec_constant, end @[simp] lemma update_same (a : α) (v : β a) (f : Πa, β a) : update f a v a = v := dif_pos rfl lemma update_injective (f : Πa, β a) (a' : α) : injective (update f a') := λ v v' h, have _ := congr_fun h a', by rwa [update_same, update_same] at this @[simp] lemma update_noteq {a a' : α} (h : a ≠ a') (v : β a') (f : Πa, β a) : update f a' v a = f a := dif_neg h lemma forall_update_iff (f : Π a, β a) {a : α} {b : β a} (p : Π a, β a → Prop) : (∀ x, p x (update f a b x)) ↔ p a b ∧ ∀ x ≠ a, p x (f x) := calc (∀ x, p x (update f a b x)) ↔ ∀ x, (x = a ∨ x ≠ a) → p x (update f a b x) : by simp only [ne.def, classical.em, forall_prop_of_true] ... ↔ p a b ∧ ∀ x ≠ a, p x (f x) : by simp [or_imp_distrib, forall_and_distrib] { contextual := tt } lemma update_eq_iff {a : α} {b : β a} {f g : Π a, β a} : update f a b = g ↔ b = g a ∧ ∀ x ≠ a, f x = g x := funext_iff.trans $ forall_update_iff _ (λ x y, y = g x) lemma eq_update_iff {a : α} {b : β a} {f g : Π a, β a} : g = update f a b ↔ g a = b ∧ ∀ x ≠ a, g x = f x := funext_iff.trans $ forall_update_iff _ (λ x y, g x = y) @[simp] lemma update_eq_self (a : α) (f : Πa, β a) : update f a (f a) = f := update_eq_iff.2 ⟨rfl, λ _ _, rfl⟩ lemma update_comp_eq_of_forall_ne' {α'} (g : Π a, β a) {f : α' → α} {i : α} (a : β i) (h : ∀ x, f x ≠ i) : (λ j, (update g i a) (f j)) = (λ j, g (f j)) := funext $ λ x, update_noteq (h _) _ _ /-- Non-dependent version of `function.update_comp_eq_of_forall_ne'` -/ lemma update_comp_eq_of_forall_ne {α β : Sort*} (g : α' → β) {f : α → α'} {i : α'} (a : β) (h : ∀ x, f x ≠ i) : (update g i a) ∘ f = g ∘ f := update_comp_eq_of_forall_ne' g a h lemma update_comp_eq_of_injective' (g : Π a, β a) {f : α' → α} (hf : function.injective f) (i : α') (a : β (f i)) : (λ j, update g (f i) a (f j)) = update (λ i, g (f i)) i a := eq_update_iff.2 ⟨update_same _ _ _, λ j hj, update_noteq (hf.ne hj) _ _⟩ /-- Non-dependent version of `function.update_comp_eq_of_injective'` -/ lemma update_comp_eq_of_injective {β : Sort*} (g : α' → β) {f : α → α'} (hf : function.injective f) (i : α) (a : β) : (function.update g (f i) a) ∘ f = function.update (g ∘ f) i a := update_comp_eq_of_injective' g hf i a lemma apply_update {ι : Sort*} [decidable_eq ι] {α β : ι → Sort*} (f : Π i, α i → β i) (g : Π i, α i) (i : ι) (v : α i) (j : ι) : f j (update g i v j) = update (λ k, f k (g k)) i (f i v) j := begin by_cases h : j = i, { subst j, simp }, { simp [h] } end lemma comp_update {α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ (update g i v) = update (f ∘ g) i (f v) := funext $ apply_update _ _ _ _ theorem update_comm {α} [decidable_eq α] {β : α → Sort*} {a b : α} (h : a ≠ b) (v : β a) (w : β b) (f : Πa, β a) : update (update f a v) b w = update (update f b w) a v := begin funext c, simp only [update], by_cases h₁ : c = b; by_cases h₂ : c = a; try {simp [h₁, h₂]}, cases h (h₂.symm.trans h₁), end @[simp] theorem update_idem {α} [decidable_eq α] {β : α → Sort*} {a : α} (v w : β a) (f : Πa, β a) : update (update f a v) a w = update f a w := by {funext b, by_cases b = a; simp [update, h]} end update section extend noncomputable theory local attribute [instance, priority 10] classical.prop_decidable variables {α β γ : Type*} {f : α → β} /-- `extend f g e'` extends a function `g : α → γ` along a function `f : α → β` to a function `β → γ`, by using the values of `g` on the range of `f` and the values of an auxiliary function `e' : β → γ` elsewhere. Mostly useful when `f` is injective. -/ def extend (f : α → β) (g : α → γ) (e' : β → γ) : β → γ := λ b, if h : ∃ a, f a = b then g (classical.some h) else e' b lemma extend_def (f : α → β) (g : α → γ) (e' : β → γ) (b : β) : extend f g e' b = if h : ∃ a, f a = b then g (classical.some h) else e' b := rfl @[simp] lemma extend_apply (hf : injective f) (g : α → γ) (e' : β → γ) (a : α) : extend f g e' (f a) = g a := begin simp only [extend_def, dif_pos, exists_apply_eq_apply], exact congr_arg g (hf $ classical.some_spec (exists_apply_eq_apply f a)) end @[simp] lemma extend_comp (hf : injective f) (g : α → γ) (e' : β → γ) : extend f g e' ∘ f = g := funext $ λ a, extend_apply hf g e' a end extend lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) := rfl @[simp] lemma uncurry_apply_pair {α β γ} (f : α → β → γ) (x : α) (y : β) : uncurry f (x, y) = f x y := rfl @[simp] lemma curry_apply {α β γ} (f : α × β → γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl section bicomp variables {α β γ δ ε : Type*} /-- Compose a binary function `f` with a pair of unary functions `g` and `h`. If both arguments of `f` have the same type and `g = h`, then `bicompl f g g = f on g`. -/ def bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) (a b) := f (g a) (h b) /-- Compose an unary function `f` with a binary function `g`. -/ def bicompr (f : γ → δ) (g : α → β → γ) (a b) := f (g a b) -- Suggested local notation: local notation f `∘₂` g := bicompr f g lemma uncurry_bicompr (f : α → β → γ) (g : γ → δ) : uncurry (g ∘₂ f) = (g ∘ uncurry f) := rfl lemma uncurry_bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) : uncurry (bicompl f g h) = (uncurry f) ∘ (prod.map g h) := rfl end bicomp section uncurry variables {α β γ δ : Type*} /-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/ class has_uncurry (α : Type*) (β : out_param Type*) (γ : out_param Type*) := (uncurry : α → (β → γ)) /-- Uncurrying operator. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps.-/ add_decl_doc has_uncurry.uncurry notation `↿`:max x:max := has_uncurry.uncurry x instance has_uncurry_base : has_uncurry (α → β) α β := ⟨id⟩ instance has_uncurry_induction [has_uncurry β γ δ] : has_uncurry (α → β) (α × γ) δ := ⟨λ f p, ↿(f p.1) p.2⟩ end uncurry /-- A function is involutive, if `f ∘ f = id`. -/ def involutive {α} (f : α → α) : Prop := ∀ x, f (f x) = x lemma involutive_iff_iter_2_eq_id {α} {f : α → α} : involutive f ↔ (f^[2] = id) := funext_iff.symm namespace involutive variables {α : Sort u} {f : α → α} (h : involutive f) include h @[simp] lemma comp_self : f ∘ f = id := funext h protected lemma left_inverse : left_inverse f f := h protected lemma right_inverse : right_inverse f f := h protected lemma injective : injective f := h.left_inverse.injective protected lemma surjective : surjective f := λ x, ⟨f x, h x⟩ protected lemma bijective : bijective f := ⟨h.injective, h.surjective⟩ /-- Involuting an `ite` of an involuted value `x : α` negates the `Prop` condition in the `ite`. -/ protected lemma ite_not (P : Prop) [decidable P] (x : α) : f (ite P x (f x)) = ite (¬ P) x (f x) := by rw [apply_ite f, h, ite_not] end involutive /-- The property of a binary function `f : α → β → γ` being injective. Mathematically this should be thought of as the corresponding function `α × β → γ` being injective. -/ @[reducible] def injective2 {α β γ} (f : α → β → γ) : Prop := ∀ ⦃a₁ a₂ b₁ b₂⦄, f a₁ b₁ = f a₂ b₂ → a₁ = a₂ ∧ b₁ = b₂ namespace injective2 variables {α β γ : Type*} (f : α → β → γ) protected lemma left (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : a₁ = a₂ := (hf h).1 protected lemma right (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : b₁ = b₂ := (hf h).2 lemma eq_iff (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ : f a₁ b₁ = f a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ h, hf h, λ⟨h1, h2⟩, congr_arg2 f h1 h2⟩ end injective2 section sometimes local attribute [instance, priority 10] classical.prop_decidable /-- `sometimes f` evaluates to some value of `f`, if it exists. This function is especially interesting in the case where `α` is a proposition, in which case `f` is necessarily a constant function, so that `sometimes f = f a` for all `a`. -/ noncomputable def sometimes {α β} [nonempty β] (f : α → β) : β := if h : nonempty α then f (classical.choice h) else classical.choice ‹_› theorem sometimes_eq {p : Prop} {α} [nonempty α] (f : p → α) (a : p) : sometimes f = f a := dif_pos ⟨a⟩ theorem sometimes_spec {p : Prop} {α} [nonempty α] (P : α → Prop) (f : p → α) (a : p) (h : P (f a)) : P (sometimes f) := by rwa sometimes_eq end sometimes end function /-- `s.piecewise f g` is the function equal to `f` on the set `s`, and to `g` on its complement. -/ def set.piecewise {α : Type u} {β : α → Sort v} (s : set α) (f g : Πi, β i) [∀j, decidable (j ∈ s)] : Πi, β i := λi, if i ∈ s then f i else g i
0e5922917c282a8e56f5f4752a39f7692d32663d
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/zmod/basic.lean
5e0d2d3265d9c1b2b9c86f4857ccd28b64a84acb
[ "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
21,031
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import data.int.modeq data.int.gcd data.fintype data.pnat.basic tactic.ring /-! # Integers mod `n` Definition of the integers mod n, and the field structure on the integers mod p. There are two types defined, `zmod n`, which is for integers modulo a positive nat `n : ℕ+`. `zmodp` is the type of integers modulo a prime number, for which a field structure is defined. ## Definitions * `val` is inherited from `fin` and returns the least natural number in the equivalence class * `val_min_abs` returns the integer closest to zero in the equivalence class. * A coercion `cast` is defined from `zmod n` into any semiring. This is a semiring hom if the ring has characteristic dividing `n` ## Implentation notes `zmod` and `zmodp` are implemented as different types so that the field instance for `zmodp` can be synthesized. This leads to a lot of code duplication and most of the functions and theorems for `zmod` are restated for `zmodp` -/ open nat nat.modeq int def zmod (n : ℕ+) := fin n namespace zmod instance (n : ℕ+) : has_neg (zmod n) := ⟨λ a, ⟨nat_mod (-(a.1 : ℤ)) n, have h : (n : ℤ) ≠ 0 := int.coe_nat_ne_zero_iff_pos.2 n.pos, have h₁ : ((n : ℕ) : ℤ) = abs n := (abs_of_nonneg (int.coe_nat_nonneg n)).symm, by rw [← int.coe_nat_lt, nat_mod, to_nat_of_nonneg (int.mod_nonneg _ h), h₁]; exact int.mod_lt _ h⟩⟩ instance (n : ℕ+) : add_comm_semigroup (zmod n) := { add_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (show ((a + b) % n + c) ≡ (a + (b + c) % n) [MOD n], from calc ((a + b) % n + c) ≡ a + b + c [MOD n] : modeq_add (nat.mod_mod _ _) rfl ... ≡ a + (b + c) [MOD n] : by rw add_assoc ... ≡ (a + (b + c) % n) [MOD n] : modeq_add rfl (nat.mod_mod _ _).symm), add_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a + b) % n = (b + a) % n, by rw add_comm), ..fin.has_add } instance (n : ℕ+) : comm_semigroup (zmod n) := { mul_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc ((a * b) % n * c) ≡ a * b * c [MOD n] : modeq_mul (nat.mod_mod _ _) rfl ... ≡ a * (b * c) [MOD n] : by rw mul_assoc ... ≡ a * (b * c % n) [MOD n] : modeq_mul rfl (nat.mod_mod _ _).symm), mul_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a * b) % n = (b * a) % n, by rw mul_comm), ..fin.has_mul } instance (n : ℕ+) : has_one (zmod n) := ⟨⟨(1 % n), nat.mod_lt _ n.pos⟩⟩ instance (n : ℕ+) : has_zero (zmod n) := ⟨⟨0, n.pos⟩⟩ instance zmod_one.subsingleton : subsingleton (zmod 1) := ⟨λ a b, fin.eq_of_veq (by rw [eq_zero_of_le_zero (le_of_lt_succ a.2), eq_zero_of_le_zero (le_of_lt_succ b.2)])⟩ lemma add_val {n : ℕ+} : ∀ a b : zmod n, (a + b).val = (a.val + b.val) % n | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma mul_val {n : ℕ+} : ∀ a b : zmod n, (a * b).val = (a.val * b.val) % n | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma one_val {n : ℕ+} : (1 : zmod n).val = 1 % n := rfl @[simp] lemma zero_val (n : ℕ+) : (0 : zmod n).val = 0 := rfl private lemma one_mul_aux (n : ℕ+) (a : zmod n) : (1 : zmod n) * a = a := begin cases n with n hn, cases n with n, { exact (lt_irrefl _ hn).elim }, { cases n with n, { exact @subsingleton.elim (zmod 1) _ _ _ }, { have h₁ : a.1 % n.succ.succ = a.1 := nat.mod_eq_of_lt a.2, have h₂ : 1 % n.succ.succ = 1 := nat.mod_eq_of_lt dec_trivial, refine fin.eq_of_veq _, simp [mul_val, one_val, h₁, h₂] } } end private lemma left_distrib_aux (n : ℕ+) : ∀ a b c : zmod n, a * (b + c) = a * b + a * c := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc a * ((b + c) % n) ≡ a * (b + c) [MOD n] : modeq_mul rfl (nat.mod_mod _ _) ... ≡ a * b + a * c [MOD n] : by rw mul_add ... ≡ (a * b) % n + (a * c) % n [MOD n] : modeq_add (nat.mod_mod _ _).symm (nat.mod_mod _ _).symm) instance (n : ℕ+) : comm_ring (zmod n) := { zero_add := λ ⟨a, ha⟩, fin.eq_of_veq (show (0 + a) % n = a, by rw zero_add; exact nat.mod_eq_of_lt ha), add_zero := λ ⟨a, ha⟩, fin.eq_of_veq (nat.mod_eq_of_lt ha), add_left_neg := λ ⟨a, ha⟩, fin.eq_of_veq (show (((-a : ℤ) % n).to_nat + a) % n = 0, from int.coe_nat_inj begin have hn : (n : ℤ) ≠ 0 := (ne_of_lt (int.coe_nat_lt.2 n.pos)).symm, rw [int.coe_nat_mod, int.coe_nat_add, to_nat_of_nonneg (int.mod_nonneg _ hn), add_comm], simp, end), one_mul := one_mul_aux n, mul_one := λ a, by rw mul_comm; exact one_mul_aux n a, left_distrib := left_distrib_aux n, right_distrib := λ a b c, by rw [mul_comm, left_distrib_aux, mul_comm _ b, mul_comm]; refl, ..zmod.has_zero n, ..zmod.has_one n, ..zmod.has_neg n, ..zmod.add_comm_semigroup n, ..zmod.comm_semigroup n } lemma val_cast_nat {n : ℕ+} (a : ℕ) : (a : zmod n).val = a % n := begin induction a with a ih, { rw [nat.zero_mod]; refl }, { rw [succ_eq_add_one, nat.cast_add, add_val, ih], show (a % n + ((0 + (1 % n)) % n)) % n = (a + 1) % n, rw [zero_add, nat.mod_mod], exact nat.modeq.modeq_add (nat.mod_mod a n) (nat.mod_mod 1 n) } end lemma neg_val' {m : pnat} (n : zmod m) : (-n).val = (m - n.val) % m := have ((-n).val + n.val) % m = (m - n.val + n.val) % m, by { rw [←add_val, add_left_neg, nat.sub_add_cancel (le_of_lt n.is_lt), nat.mod_self], refl }, (nat.mod_eq_of_lt (fin.is_lt _)).symm.trans (nat.modeq.modeq_add_cancel_right rfl this) lemma neg_val {m : pnat} (n : zmod m) : (-n).val = if n = 0 then 0 else m - n.val := begin rw neg_val', by_cases h : n = 0; simp [h], cases n with n nlt; cases n; dsimp, { contradiction }, rw nat.mod_eq_of_lt, apply nat.sub_lt m.2 (nat.succ_pos _), end lemma mk_eq_cast {n : ℕ+} {a : ℕ} (h : a < n) : (⟨a, h⟩ : zmod n) = (a : zmod n) := fin.eq_of_veq (by rw [val_cast_nat, nat.mod_eq_of_lt h]) @[simp] lemma cast_self_eq_zero {n : ℕ+} : ((n : ℕ) : zmod n) = 0 := fin.eq_of_veq (show (n : zmod n).val = 0, by simp [val_cast_nat]) lemma val_cast_of_lt {n : ℕ+} {a : ℕ} (h : a < n) : (a : zmod n).val = a := by rw [val_cast_nat, nat.mod_eq_of_lt h] @[simp] lemma cast_mod_nat (n : ℕ+) (a : ℕ) : ((a % n : ℕ) : zmod n) = a := by conv {to_rhs, rw ← nat.mod_add_div a n}; simp @[simp] lemma cast_mod_nat' {n : ℕ} (hn : 0 < n) (a : ℕ) : ((a % n : ℕ) : zmod ⟨n, hn⟩) = a := cast_mod_nat _ _ @[simp] lemma cast_val {n : ℕ+} (a : zmod n) : (a.val : zmod n) = a := by cases a; simp [mk_eq_cast] @[simp] lemma cast_mod_int (n : ℕ+) (a : ℤ) : ((a % (n : ℕ) : ℤ) : zmod n) = a := by conv {to_rhs, rw ← int.mod_add_div a n}; simp @[simp] lemma cast_mod_int' {n : ℕ} (hn : 0 < n) (a : ℤ) : ((a % (n : ℕ) : ℤ) : zmod ⟨n, hn⟩) = a := cast_mod_int _ _ lemma val_cast_int {n : ℕ+} (a : ℤ) : (a : zmod n).val = (a % (n : ℕ)).nat_abs := have h : nat_abs (a % (n : ℕ)) < n := int.coe_nat_lt.1 begin rw [nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))], conv {to_rhs, rw ← abs_of_nonneg (int.coe_nat_nonneg n)}, exact int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 n.pos) end, int.coe_nat_inj $ by conv {to_lhs, rw [← cast_mod_int n a, ← nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)), int.cast_coe_nat, val_cast_of_lt h] } lemma coe_val_cast_int {n : ℕ+} (a : ℤ) : ((a : zmod n).val : ℤ) = a % (n : ℕ) := by rw [val_cast_int, int.nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))] lemma eq_iff_modeq_nat {n : ℕ+} {a b : ℕ} : (a : zmod n) = b ↔ a ≡ b [MOD n] := ⟨λ h, by have := fin.veq_of_eq h; rwa [val_cast_nat, val_cast_nat] at this, λ h, fin.eq_of_veq $ by rwa [val_cast_nat, val_cast_nat]⟩ lemma eq_iff_modeq_nat' {n : ℕ} (hn : 0 < n) {a b : ℕ} : (a : zmod ⟨n, hn⟩) = b ↔ a ≡ b [MOD n] := eq_iff_modeq_nat lemma eq_iff_modeq_int {n : ℕ+} {a b : ℤ} : (a : zmod n) = b ↔ a ≡ b [ZMOD (n : ℕ)] := ⟨λ h, by have := fin.veq_of_eq h; rwa [val_cast_int, val_cast_int, ← int.coe_nat_eq_coe_nat_iff, nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)), nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))] at this, λ h : a % (n : ℕ) = b % (n : ℕ), by rw [← cast_mod_int n a, ← cast_mod_int n b, h]⟩ lemma eq_iff_modeq_int' {n : ℕ} (hn : 0 < n) {a b : ℤ} : (a : zmod ⟨n, hn⟩) = b ↔ a ≡ b [ZMOD (n : ℕ)] := eq_iff_modeq_int lemma eq_zero_iff_dvd_nat {n : ℕ+} {a : ℕ} : (a : zmod n) = 0 ↔ (n : ℕ) ∣ a := by rw [← @nat.cast_zero (zmod n), eq_iff_modeq_nat, nat.modeq.modeq_zero_iff] lemma eq_zero_iff_dvd_int {n : ℕ+} {a : ℤ} : (a : zmod n) = 0 ↔ ((n : ℕ) : ℤ) ∣ a := by rw [← @int.cast_zero (zmod n), eq_iff_modeq_int, int.modeq.modeq_zero_iff] instance (n : ℕ+) : fintype (zmod n) := fin.fintype _ instance decidable_eq (n : ℕ+) : decidable_eq (zmod n) := fin.decidable_eq _ instance (n : ℕ+) : has_repr (zmod n) := fin.has_repr _ lemma card_zmod (n : ℕ+) : fintype.card (zmod n) = n := fintype.card_fin n lemma le_div_two_iff_lt_neg {n : ℕ+} (hn : (n : ℕ) % 2 = 1) {x : zmod n} (hx0 : x ≠ 0) : x.1 ≤ (n / 2 : ℕ) ↔ (n / 2 : ℕ) < (-x).1 := have hn2 : (n : ℕ) / 2 < n := nat.div_lt_of_lt_mul ((lt_mul_iff_one_lt_left n.pos).2 dec_trivial), have hn2' : (n : ℕ) - n / 2 = n / 2 + 1, by conv {to_lhs, congr, rw [← succ_sub_one n, succ_sub n.pos]}; rw [← two_mul_odd_div_two hn, two_mul, ← succ_add, nat.add_sub_cancel], have hxn : (n : ℕ) - x.val < n, begin rw [nat.sub_lt_iff (le_of_lt x.2) (le_refl _), nat.sub_self], rw ← zmod.cast_val x at hx0, exact nat.pos_of_ne_zero (λ h, by simpa [h] using hx0) end, by conv {to_rhs, rw [← nat.succ_le_iff, succ_eq_add_one, ← hn2', ← zero_add (- x), ← zmod.cast_self_eq_zero, ← sub_eq_add_neg, ← zmod.cast_val x, ← nat.cast_sub (le_of_lt x.2), zmod.val_cast_nat, mod_eq_of_lt hxn, nat.sub_le_sub_left_iff (le_of_lt x.2)] } lemma ne_neg_self {n : ℕ+} (hn1 : (n : ℕ) % 2 = 1) {a : zmod n} (ha : a ≠ 0) : a ≠ -a := λ h, have a.val ≤ n / 2 ↔ (n : ℕ) / 2 < (-a).val := le_div_two_iff_lt_neg hn1 ha, by rwa [← h, ← not_lt, not_iff_self] at this @[simp] lemma cast_mul_right_val_cast {n m : ℕ+} (a : ℕ) : ((a : zmod (m * n)).val : zmod m) = (a : zmod m) := zmod.eq_iff_modeq_nat.2 (by rw zmod.val_cast_nat; exact nat.modeq.modeq_of_modeq_mul_right _ (nat.mod_mod _ _)) @[simp] lemma cast_mul_left_val_cast {n m : ℕ+} (a : ℕ) : ((a : zmod (n * m)).val : zmod m) = (a : zmod m) := zmod.eq_iff_modeq_nat.2 (by rw zmod.val_cast_nat; exact nat.modeq.modeq_of_modeq_mul_left _ (nat.mod_mod _ _)) lemma cast_val_cast_of_dvd {n m : ℕ+} (h : (m : ℕ) ∣ n) (a : ℕ) : ((a : zmod n).val : zmod m) = (a : zmod m) := let ⟨k , hk⟩ := h in zmod.eq_iff_modeq_nat.2 (nat.modeq.modeq_of_modeq_mul_right k (by rw [← hk, zmod.val_cast_nat]; exact nat.mod_mod _ _)) def units_equiv_coprime {n : ℕ+} : units (zmod n) ≃ {x : zmod n // nat.coprime x.1 n} := { to_fun := λ x, ⟨x, nat.modeq.coprime_of_mul_modeq_one (x⁻¹).1.1 begin have := units.ext_iff.1 (mul_right_inv x), rwa [← zmod.cast_val ((1 : units (zmod n)) : zmod n), units.coe_one, zmod.one_val, ← zmod.cast_val ((x * x⁻¹ : units (zmod n)) : zmod n), units.coe_mul, zmod.mul_val, zmod.cast_mod_nat, zmod.cast_mod_nat, zmod.eq_iff_modeq_nat] at this end⟩, inv_fun := λ x, have x.val * ↑(gcd_a ((x.val).val) ↑n) = 1, by rw [← zmod.cast_val x.1, ← int.cast_coe_nat, ← int.cast_one, ← int.cast_mul, zmod.eq_iff_modeq_int, ← int.coe_nat_one, ← (show nat.gcd _ _ = _, from x.2)]; simpa using int.modeq.gcd_a_modeq x.1.1 n, ⟨x.1, gcd_a x.1.1 n, this, by simpa [mul_comm] using this⟩, left_inv := λ ⟨_, _, _, _⟩, units.ext rfl, right_inv := λ ⟨_, _⟩, rfl } /-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`, The result will be in the interval `(-n/2, n/2]` -/ def val_min_abs {n : ℕ+} (x : zmod n) : ℤ := if x.val ≤ n / 2 then x.val else x.val - n @[simp] lemma coe_val_min_abs {n : ℕ+} (x : zmod n) : (x.val_min_abs : zmod n) = x := by simp [zmod.val_min_abs]; split_ifs; simp lemma nat_abs_val_min_abs_le {n : ℕ+} (x : zmod n) : x.val_min_abs.nat_abs ≤ n / 2 := have (x.val - n : ℤ) ≤ 0, from sub_nonpos.2 $ int.coe_nat_le.2 $ le_of_lt x.2, begin rw zmod.val_min_abs, split_ifs with h, { exact h }, { rw [← int.coe_nat_le, int.of_nat_nat_abs_of_nonpos this, neg_sub], conv_lhs { congr, rw [coe_coe, ← nat.mod_add_div n 2, int.coe_nat_add, int.coe_nat_mul, int.coe_nat_bit0, int.coe_nat_one] }, rw ← sub_nonneg, suffices : (0 : ℤ) ≤ x.val - ((n % 2 : ℕ) + (n / 2 : ℕ)), { exact le_trans this (le_of_eq $ by ring) }, exact sub_nonneg.2 (by rw [← int.coe_nat_add, int.coe_nat_le]; exact calc (n : ℕ) % 2 + n / 2 ≤ 1 + n / 2 : add_le_add (nat.le_of_lt_succ (nat.mod_lt _ dec_trivial)) (le_refl _) ... ≤ x.val : by rw add_comm; exact nat.succ_le_of_lt (lt_of_not_ge h)) } end @[simp] lemma val_min_abs_zero {n : ℕ+} : (0 : zmod n).val_min_abs = 0 := by simp [zmod.val_min_abs] @[simp] lemma val_min_abs_eq_zero {n : ℕ+} (x : zmod n) : x.val_min_abs = 0 ↔ x = 0 := ⟨λ h, begin dsimp [zmod.val_min_abs] at h, split_ifs at h, { exact fin.eq_of_veq (by simp * at *) }, { exact absurd h (mt sub_eq_zero.1 (ne_of_lt $ int.coe_nat_lt.2 x.2)) } end, λ hx0, hx0.symm ▸ zmod.val_min_abs_zero⟩ lemma cast_nat_abs_val_min_abs {n : ℕ+} (a : zmod n) : (a.val_min_abs.nat_abs : zmod n) = if a.val ≤ (n : ℕ) / 2 then a else -a := have (a.val : ℤ) + -n ≤ 0, by erw [sub_nonpos, int.coe_nat_le]; exact le_of_lt a.2, begin dsimp [zmod.val_min_abs], split_ifs, { simp }, { erw [← int.cast_coe_nat, int.of_nat_nat_abs_of_nonpos this], simp } end @[simp] lemma nat_abs_val_min_abs_neg {n : ℕ+} (a : zmod n) : (-a).val_min_abs.nat_abs = a.val_min_abs.nat_abs := if haa : -a = a then by rw [haa] else have hpa : (n : ℕ) - a.val ≤ n / 2 ↔ (n : ℕ) / 2 < a.val, from suffices (((n : ℕ) % 2) + 2 * (n / 2)) - a.val ≤ (n : ℕ) / 2 ↔ (n : ℕ) / 2 < a.val, by rwa [nat.mod_add_div] at this, begin rw [nat.sub_le_iff, two_mul, ← add_assoc, nat.add_sub_cancel], cases (n : ℕ).mod_two_eq_zero_or_one with hn0 hn1, { split, { exact λ h, lt_of_le_of_ne (le_trans (nat.le_add_left _ _) h) begin assume hna, rw [← zmod.cast_val a, ← hna, neg_eq_iff_add_eq_zero, ← nat.cast_add, zmod.eq_zero_iff_dvd_nat, ← two_mul, ← zero_add (2 * _), ← hn0, nat.mod_add_div] at haa, exact haa (dvd_refl _) end }, { rw [hn0, zero_add], exact le_of_lt } }, { rw [hn1, add_comm, nat.succ_le_iff] } end, have ha0 : ¬ a = 0, from λ ha0, by simp * at *, begin dsimp [zmod.val_min_abs], rw [← not_le] at hpa, simp only [if_neg ha0, zmod.neg_val, hpa, int.coe_nat_sub (le_of_lt a.2)], split_ifs, { simp }, { rw [← int.nat_abs_neg], simp } end lemma val_eq_ite_val_min_abs {n : ℕ+} (a : zmod n) : (a.val : ℤ) = a.val_min_abs + if a.val ≤ n / 2 then 0 else n := by simp [zmod.val_min_abs]; split_ifs; simp lemma neg_eq_self_mod_two : ∀ (a : zmod 2), -a = a := dec_trivial @[simp] lemma nat_abs_mod_two (a : ℤ) : (a.nat_abs : zmod 2) = a := by cases a; simp [zmod.neg_eq_self_mod_two] section variables {α : Type*} [has_zero α] [has_one α] [has_add α] {n : ℕ+} def cast : zmod n → α := nat.cast ∘ fin.val end end zmod def zmodp (p : ℕ) (hp : prime p) : Type := zmod ⟨p, hp.pos⟩ namespace zmodp variables {p : ℕ} (hp : prime p) instance : comm_ring (zmodp p hp) := zmod.comm_ring ⟨p, hp.pos⟩ instance {p : ℕ} (hp : prime p) : has_inv (zmodp p hp) := ⟨λ a, gcd_a a.1 p⟩ lemma add_val : ∀ a b : zmodp p hp, (a + b).val = (a.val + b.val) % p | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma mul_val : ∀ a b : zmodp p hp, (a * b).val = (a.val * b.val) % p | ⟨_, _⟩ ⟨_, _⟩ := rfl @[simp] lemma one_val : (1 : zmodp p hp).val = 1 := nat.mod_eq_of_lt hp.one_lt @[simp] lemma zero_val : (0 : zmodp p hp).val = 0 := rfl lemma val_cast_nat (a : ℕ) : (a : zmodp p hp).val = a % p := @zmod.val_cast_nat ⟨p, hp.pos⟩ _ lemma mk_eq_cast {a : ℕ} (h : a < p) : (⟨a, h⟩ : zmodp p hp) = (a : zmodp p hp) := @zmod.mk_eq_cast ⟨p, hp.pos⟩ _ _ @[simp] lemma cast_self_eq_zero: (p : zmodp p hp) = 0 := fin.eq_of_veq $ by simp [val_cast_nat] lemma val_cast_of_lt {a : ℕ} (h : a < p) : (a : zmodp p hp).val = a := @zmod.val_cast_of_lt ⟨p, hp.pos⟩ _ h @[simp] lemma cast_mod_nat (a : ℕ) : ((a % p : ℕ) : zmodp p hp) = a := @zmod.cast_mod_nat ⟨p, hp.pos⟩ _ @[simp] lemma cast_val (a : zmodp p hp) : (a.val : zmodp p hp) = a := @zmod.cast_val ⟨p, hp.pos⟩ _ @[simp] lemma cast_mod_int (a : ℤ) : ((a % p : ℤ) : zmodp p hp) = a := @zmod.cast_mod_int ⟨p, hp.pos⟩ _ lemma val_cast_int (a : ℤ) : (a : zmodp p hp).val = (a % p).nat_abs := @zmod.val_cast_int ⟨p, hp.pos⟩ _ lemma coe_val_cast_int (a : ℤ) : ((a : zmodp p hp).val : ℤ) = a % (p : ℕ) := @zmod.coe_val_cast_int ⟨p, hp.pos⟩ _ lemma eq_iff_modeq_nat {a b : ℕ} : (a : zmodp p hp) = b ↔ a ≡ b [MOD p] := @zmod.eq_iff_modeq_nat ⟨p, hp.pos⟩ _ _ lemma eq_iff_modeq_int {a b : ℤ} : (a : zmodp p hp) = b ↔ a ≡ b [ZMOD p] := @zmod.eq_iff_modeq_int ⟨p, hp.pos⟩ _ _ lemma eq_zero_iff_dvd_nat (a : ℕ) : (a : zmodp p hp) = 0 ↔ p ∣ a := @zmod.eq_zero_iff_dvd_nat ⟨p, hp.pos⟩ _ lemma eq_zero_iff_dvd_int (a : ℤ) : (a : zmodp p hp) = 0 ↔ (p : ℤ) ∣ a := @zmod.eq_zero_iff_dvd_int ⟨p, hp.pos⟩ _ instance : fintype (zmodp p hp) := @zmod.fintype ⟨p, hp.pos⟩ instance decidable_eq : decidable_eq (zmodp p hp) := fin.decidable_eq _ instance : has_repr (zmodp p hp) := fin.has_repr _ @[simp] lemma card_zmodp : fintype.card (zmodp p hp) = p := @zmod.card_zmod ⟨p, hp.pos⟩ lemma le_div_two_iff_lt_neg {p : ℕ} (hp : prime p) (hp1 : p % 2 = 1) {x : zmodp p hp} (hx0 : x ≠ 0) : x.1 ≤ (p / 2 : ℕ) ↔ (p / 2 : ℕ) < (-x).1 := @zmod.le_div_two_iff_lt_neg ⟨p, hp.pos⟩ hp1 _ hx0 lemma ne_neg_self (hp1 : p % 2 = 1) {a : zmodp p hp} (ha : a ≠ 0) : a ≠ -a := @zmod.ne_neg_self ⟨p, hp.pos⟩ hp1 _ ha variable {hp} /-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`, The result will be in the interval `(-n/2, n/2]` -/ def val_min_abs (x : zmodp p hp) : ℤ := zmod.val_min_abs x @[simp] lemma coe_val_min_abs (x : zmodp p hp) : (x.val_min_abs : zmodp p hp) = x := zmod.coe_val_min_abs x lemma nat_abs_val_min_abs_le (x : zmodp p hp) : x.val_min_abs.nat_abs ≤ p / 2 := zmod.nat_abs_val_min_abs_le x @[simp] lemma val_min_abs_zero : (0 : zmodp p hp).val_min_abs = 0 := zmod.val_min_abs_zero @[simp] lemma val_min_abs_eq_zero (x : zmodp p hp) : x.val_min_abs = 0 ↔ x = 0 := zmod.val_min_abs_eq_zero x lemma cast_nat_abs_val_min_abs (a : zmodp p hp) : (a.val_min_abs.nat_abs : zmodp p hp) = if a.val ≤ p / 2 then a else -a := zmod.cast_nat_abs_val_min_abs a @[simp] lemma nat_abs_val_min_abs_neg (a : zmodp p hp) : (-a).val_min_abs.nat_abs = a.val_min_abs.nat_abs := zmod.nat_abs_val_min_abs_neg _ lemma val_eq_ite_val_min_abs (a : zmodp p hp) : (a.val : ℤ) = a.val_min_abs + if a.val ≤ p / 2 then 0 else p := zmod.val_eq_ite_val_min_abs _ variable (hp) lemma prime_ne_zero {q : ℕ} (hq : prime q) (hpq : p ≠ q) : (q : zmodp p hp) ≠ 0 := by rwa [← nat.cast_zero, ne.def, zmodp.eq_iff_modeq_nat, nat.modeq.modeq_zero_iff, ← hp.coprime_iff_not_dvd, coprime_primes hp hq] lemma mul_inv_eq_gcd (a : ℕ) : (a : zmodp p hp) * a⁻¹ = nat.gcd a p := by rw [← int.cast_coe_nat (nat.gcd _ _), nat.gcd_comm, nat.gcd_rec, ← (eq_iff_modeq_int _).2 (int.modeq.gcd_a_modeq _ _)]; simp [has_inv.inv, val_cast_nat] private lemma mul_inv_cancel_aux : ∀ a : zmodp p hp, a ≠ 0 → a * a⁻¹ = 1 := λ ⟨a, hap⟩ ha0, begin rw [mk_eq_cast, ne.def, ← @nat.cast_zero (zmodp p hp), eq_iff_modeq_nat, modeq_zero_iff] at ha0, have : nat.gcd p a = 1 := (prime.coprime_iff_not_dvd hp).2 ha0, rw [mk_eq_cast _ hap, mul_inv_eq_gcd, nat.gcd_comm], simpa [nat.gcd_comm, this] end instance : discrete_field (zmodp p hp) := { zero_ne_one := fin.ne_of_vne $ show 0 ≠ 1 % p, by rw nat.mod_eq_of_lt hp.one_lt; exact zero_ne_one, mul_inv_cancel := mul_inv_cancel_aux hp, inv_mul_cancel := λ a, by rw mul_comm; exact mul_inv_cancel_aux hp _, has_decidable_eq := by apply_instance, inv_zero := show (gcd_a 0 p : zmodp p hp) = 0, by unfold gcd_a xgcd xgcd_aux; refl, ..zmodp.comm_ring hp, ..zmodp.has_inv hp } end zmodp
e77f19e5a72489c8b3e277cd014afeb7d66f4ab9
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/compacts.lean
67e29792a8b232ba4ffbb3c0dc2ac45e6ed90e9b
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,753
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import topology.homeomorph /-! # Compact sets ## Summary We define the subtype of compact sets in a topological space. ## Main Definitions - `closeds α` is the type of closed subsets of a topological space `α`. - `compacts α` is the type of compact subsets of a topological space `α`. - `nonempty_compacts α` is the type of non-empty compact subsets. - `positive_compacts α` is the type of compact subsets with non-empty interior. -/ open set variables (α : Type*) {β : Type*} [topological_space α] [topological_space β] namespace topological_space /-- The type of closed subsets of a topological space. -/ def closeds := {s : set α // is_closed s} /-- The type of closed subsets is inhabited, with default element the empty set. -/ instance : inhabited (closeds α) := ⟨⟨∅, is_closed_empty ⟩⟩ /-- The compact sets of a topological space. See also `nonempty_compacts`. -/ def compacts : Type* := { s : set α // is_compact s } /-- The type of non-empty compact subsets of a topological space. The non-emptiness will be useful in metric spaces, as we will be able to put a distance (and not merely an edistance) on this space. -/ def nonempty_compacts := {s : set α // s.nonempty ∧ is_compact s} /-- In an inhabited space, the type of nonempty compact subsets is also inhabited, with default element the singleton set containing the default element. -/ instance nonempty_compacts_inhabited [inhabited α] : inhabited (nonempty_compacts α) := ⟨⟨{default α}, singleton_nonempty (default α), is_compact_singleton ⟩⟩ /-- The compact sets with nonempty interior of a topological space. See also `compacts` and `nonempty_compacts`. -/ @[nolint has_inhabited_instance] def positive_compacts: Type* := { s : set α // is_compact s ∧ (interior s).nonempty } /-- In a nonempty compact space, `set.univ` is a member of `positive_compacts`, the compact sets with nonempty interior. -/ def positive_compacts_univ {α : Type*} [topological_space α] [compact_space α] [nonempty α] : positive_compacts α := ⟨set.univ, compact_univ, by simp⟩ variables {α} namespace compacts instance : semilattice_sup_bot (compacts α) := subtype.semilattice_sup_bot is_compact_empty (λ K₁ K₂, is_compact.union) instance [t2_space α]: semilattice_inf_bot (compacts α) := subtype.semilattice_inf_bot is_compact_empty (λ K₁ K₂, is_compact.inter) instance [t2_space α] : lattice (compacts α) := subtype.lattice (λ K₁ K₂, is_compact.union) (λ K₁ K₂, is_compact.inter) @[simp] lemma bot_val : (⊥ : compacts α).1 = ∅ := rfl @[simp] lemma sup_val {K₁ K₂ : compacts α} : (K₁ ⊔ K₂).1 = K₁.1 ∪ K₂.1 := rfl @[ext] protected lemma ext {K₁ K₂ : compacts α} (h : K₁.1 = K₂.1) : K₁ = K₂ := subtype.eq h @[simp] lemma finset_sup_val {β} {K : β → compacts α} {s : finset β} : (s.sup K).1 = s.sup (λ x, (K x).1) := finset.sup_coe _ _ instance : inhabited (compacts α) := ⟨⊥⟩ /-- The image of a compact set under a continuous function. -/ protected def map (f : α → β) (hf : continuous f) (K : compacts α) : compacts β := ⟨f '' K.1, K.2.image hf⟩ @[simp] lemma map_val {f : α → β} (hf : continuous f) (K : compacts α) : (K.map f hf).1 = f '' K.1 := rfl /-- A homeomorphism induces an equivalence on compact sets, by taking the image. -/ @[simp] protected def equiv (f : α ≃ₜ β) : compacts α ≃ compacts β := { to_fun := compacts.map f f.continuous, inv_fun := compacts.map _ f.symm.continuous, left_inv := by { intro K, ext1, simp only [map_val, ← image_comp, f.symm_comp_self, image_id] }, right_inv := by { intro K, ext1, simp only [map_val, ← image_comp, f.self_comp_symm, image_id] } } /-- The image of a compact set under a homeomorphism can also be expressed as a preimage. -/ lemma equiv_to_fun_val (f : α ≃ₜ β) (K : compacts α) : (compacts.equiv f K).1 = f.symm ⁻¹' K.1 := congr_fun (image_eq_preimage_of_inverse f.left_inv f.right_inv) K.1 end compacts section nonempty_compacts open topological_space set variable {α} instance nonempty_compacts.to_compact_space {p : nonempty_compacts α} : compact_space p.val := ⟨is_compact_iff_is_compact_univ.1 p.property.2⟩ instance nonempty_compacts.to_nonempty {p : nonempty_compacts α} : nonempty p.val := p.property.1.to_subtype /-- Associate to a nonempty compact subset the corresponding closed subset -/ def nonempty_compacts.to_closeds [t2_space α] : nonempty_compacts α → closeds α := set.inclusion $ λ s hs, hs.2.is_closed end nonempty_compacts end topological_space
634f8abb53783a76312c1aef7ea03ce59643f4f0
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/pi.lean
d9496c970de68265c611fe45459cb98b90a3d8dc
[ "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
1,224
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Eric Wieser -/ import tactic.split_ifs import tactic.simpa /-! # Theorems on pi types This file defines basic structures on Pi Types -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi variables [decidable_eq I] variables [Π i, has_zero (f i)] /-- The function supported at `i`, with value `x` there. -/ def single (i : I) (x : f i) : Π i, f i := λ i', if h : i' = i then (by { subst h, exact x }) else 0 @[simp] lemma single_eq_same (i : I) (x : f i) : single i x i = x := begin dsimp [single], split_ifs, { refl, }, { exfalso, exact h rfl, } end @[simp] lemma single_eq_of_ne {i i' : I} (h : i' ≠ i) (x : f i) : single i x i' = 0 := begin dsimp [single], split_ifs with h', { exfalso, exact h h', }, { refl, } end variables (f) lemma single_injective (i : I) : function.injective (single i : f i → Π i, f i) := λ x y h, by simpa only [single, dif_pos] using congr_fun h i end pi
d08d1919d5686a9658366c55d3c674f3ed78a6a5
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1728.lean
5890e1cc89e1991fd5e4a816702a84891ac7fc06
[ "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
707
lean
structure Bijection ( U V : Type ) := ( morphism : U → V ) ( inverse : V → U ) ( witness_1 : ∀ u : U, inverse (morphism u) = u ) ( witness_2 : ∀ v : V, morphism (inverse v) = v ) class Finite ( α : Type ) := ( cardinality : nat ) ( bijection : Bijection α (fin cardinality) ) def empty_exfalso (x : false) : empty := begin exfalso, trivial end instance empty_is_Finite : Finite empty := { cardinality := 0, bijection := begin split, intros, induction u, intros, induction v, trace_state, cases v_property, repeat {admit} end }
85fb2f42eacb4e551b0c9d954afa8f487e5223d5
dd4e652c749fea9ac77e404005cb3470e5f75469
/src/alex_playground/subtype.lean
d75d90cc43ecaed4ba8eba8e2f4403d537607bf3
[]
no_license
skbaek/cvx
e32822ad5943541539966a37dee162b0a5495f55
c50c790c9116f9fac8dfe742903a62bdd7292c15
refs/heads/master
1,623,803,010,339
1,618,058,958,000
1,618,058,958,000
176,293,135
3
2
null
null
null
null
UTF-8
Lean
false
false
607
lean
inductive var : Type* | X : var | Y : var | Z : var open var def var_type : var → Type* | X := ℕ | Y := ℕ | Z := ℕ inductive condition_id : Type* | H : condition_id | G : condition_id open condition_id structure optimization_problem := (vars : Type*) (var_type : vars → Type*) (condition_id : Type*) (conditions : condition_id → (Π v : vars, var_type v) → Prop) (obj_fun_type : Type*) (obj_fun : (Π v : vars, var_type v) → obj_fun_type) #check optimization_problem.mk var var_type condition_id (λ c v, match c with | G := v X ≥ v Y | H := v X ≥ 0 end)
94b649c4205e56544919726e0e3bdd6bdfb2c428
7cef822f3b952965621309e88eadf618da0c8ae9
/src/analysis/normed_space/bounded_linear_maps.lean
aac120f7296e66f45953c17acb20804bc3911820
[ "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
14,567
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Continuous linear functions -- functions between normed vector spaces which are bounded and linear. -/ import algebra.field import analysis.normed_space.operator_norm noncomputable theory open_locale classical filter open filter (tendsto) open metric variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_group G] [normed_space 𝕜 G] set_option class.instance_max_depth 70 /-- A function `f` satisfies `is_bounded_linear_map 𝕜 f` if it is linear and satisfies the inequality `∥ f x ∥ ≤ M * ∥ x ∥` for some positive constant `M`. -/ structure is_bounded_linear_map (𝕜 : Type*) [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E → F) extends is_linear_map 𝕜 f : Prop := (bound : ∃ M, 0 < M ∧ ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map 𝕜 f) (M : ℝ) (h : ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) : is_bounded_linear_map 𝕜 f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, assume x, le_trans (h x) $ mul_le_mul_of_nonneg_right (le_trans this zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 f := { bound := f.bound, ..f.to_linear_map } namespace is_bounded_linear_map /-- Construct a linear map from a function `f` satisfying `is_bounded_linear_map 𝕜 f`. -/ def to_linear_map (f : E → F) (h : is_bounded_linear_map 𝕜 f) : E →ₗ[𝕜] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map 𝕜 f) : E →L[𝕜] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in linear_map.continuous_of_bound _ C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map 𝕜 (λ (x:E), (0:F)) := (0 : E →ₗ F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map 𝕜 (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map 𝕜 (λ x : E × F, x.1) := begin refine (linear_map.fst 𝕜 E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map 𝕜 (λ x : E × F, x.2) := begin refine (linear_map.snd 𝕜 E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_right _ _ end variables { f g : E → F } lemma smul (c : 𝕜) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, c • f e) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ assume x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : 𝕜) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ assume x, calc ∥f x + g x∥ ≤ Mf * ∥x∥ + Mg * ∥x∥ : norm_add_le_of_le (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e - g e) := add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map 𝕜 g) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (g ∘ f) := let ⟨hlg, Mg, hMgp, hMg⟩ := hg in let ⟨hlf, Mf, hMfp, hMf⟩ := hf in ((hlg.mk' _).comp (hlf.mk' _)).is_linear.with_bound (Mg * Mf) $ assume x, calc ∥g (f x)∥ ≤ Mg * ∥f x∥ : hMg _ ... ≤ Mg * (Mf * ∥x∥) : mul_le_mul_of_nonneg_left (hMf _) (le_of_lt hMgp) ... = Mg * Mf * ∥x∥ : (mul_assoc _ _ _).symm lemma tendsto (x : E) (hf : is_bounded_linear_map 𝕜 f) : f →_{x} (f x) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (assume e, norm_nonneg _) (assume e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices (λ (e : E), M * ∥e - x∥) →_{x} (M * 0), by simpa, tendsto_const_nhds.mul (lim_norm _)) lemma continuous (hf : is_bounded_linear_map 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map 𝕜 f) : (f →_{0} 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := h.bound in ⟨M, hMp, mem_sets_of_superset univ_mem_sets (λ x _, hM x)⟩ theorem is_O_comp {E : Type*} {g : F → G} (hg : is_bounded_linear_map 𝕜 g) {f : E → F} (l : filter E) : is_O (λ x', g (f x')) f l := ((hg.is_O_id ⊤).comp _).mono (map_le_iff_le_comap.mp lattice.le_top) theorem is_O_sub {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp h l end end is_bounded_linear_map section set_option class.instance_max_depth 240 lemma is_bounded_linear_map_prod_iso : is_bounded_linear_map 𝕜 (λ(p : (E →L[𝕜] F) × (E →L[𝕜] G)), (continuous_linear_map.prod p.1 p.2 : (E →L[𝕜] (F × G)))) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ 1 (λp, _), simp only [norm, one_mul], refine continuous_linear_map.op_norm_le_bound _ (le_trans (norm_nonneg _) (le_max_left _ _)) (λu, _), simp only [norm, continuous_linear_map.prod, max_le_iff], split, { calc ∥p.1 u∥ ≤ ∥p.1∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) }, { calc ∥p.2 u∥ ≤ ∥p.2∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_right _ _) (norm_nonneg _) } end lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : continuous_linear_map 𝕜 F G) : is_bounded_linear_map 𝕜 (λ(f : E →L[𝕜] F), continuous_linear_map.comp g f) := begin refine is_linear_map.with_bound ⟨λu v, _, λc u, _⟩ (∥g∥) (λu, continuous_linear_map.op_norm_comp_le _ _), { ext x, change g ((u+v) x) = g (u x) + g (v x), have : (u+v) x = u x + v x := rfl, rw [this, g.map_add] }, { ext x, change g ((c • u) x) = c • g (u x), have : (c • u) x = c • u x := rfl, rw [this, continuous_linear_map.map_smul] } end lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : continuous_linear_map 𝕜 E F) : is_bounded_linear_map 𝕜 (λ(g : F →L[𝕜] G), continuous_linear_map.comp g f) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ (∥f∥) (λg, _), rw mul_comm, exact continuous_linear_map.op_norm_comp_le _ _ end end section bilinear_map variable (𝕜) /-- A map `f : E × F → G` satisfies `is_bounded_bilinear_map 𝕜 f` if it is bilinear and continuous. -/ structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀(x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀(c : 𝕜) (x : E) (y : F), f (c • x, y) = c • f (x,y)) (add_right : ∀(x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀(c : 𝕜) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃C>0, ∀(x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {𝕜} variable {f : E × F → G} lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map 𝕜 f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : 𝕜) • y, z) : by simp ... = f (x, z) + (-1 : 𝕜) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map 𝕜 f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : 𝕜) • z) : by simp ... = f (x, y) + (-1 : 𝕜) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp lemma is_bounded_bilinear_map_smul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × E), p.1 • p.2) := { add_left := add_smul, smul_left := λc x y, by simp [smul_smul], add_right := smul_add, smul_right := λc x y, by simp [smul_smul, mul_comm], bound := ⟨1, zero_lt_one, λx y, by simp [norm_smul]⟩ } lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map 𝕜 (λ(p : (E →L[𝕜] F) × (F →L[𝕜] G)), p.2.comp p.1) := { add_left := λx₁ x₂ y, begin ext z, change y (x₁ z + x₂ z) = y (x₁ z) + y (x₂ z), rw y.map_add end, smul_left := λc x y, begin ext z, change y (c • (x z)) = c • y (x z), rw continuous_linear_map.map_smul end, add_right := λx y₁ y₂, rfl, smul_right := λc x y, rfl, bound := ⟨1, zero_lt_one, λx y, calc ∥continuous_linear_map.comp ((x, y).snd) ((x, y).fst)∥ ≤ ∥y∥ * ∥x∥ : continuous_linear_map.op_norm_comp_le _ _ ... = 1 * ∥x∥ * ∥ y∥ : by ring ⟩ } lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map 𝕜 (λp : (E →L[𝕜] F) × E, p.1 p.2) := { add_left := by simp, smul_left := by simp, add_right := by simp, smul_right := by simp, bound := ⟨1, zero_lt_one, by simp [continuous_linear_map.le_op_norm]⟩ } /-- The function `continuous_linear_map.smul_right`, associating to a continuous linear map `f : E → 𝕜` and a scalar `c : F` the tensor product `f ⊗ c` as a continuous linear map from `E` to `F`, is a bounded bilinear map. -/ lemma is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map 𝕜 (λp, (continuous_linear_map.smul_right : (E →L[𝕜] 𝕜) → F → (E →L[𝕜] F)) p.1 p.2) := { add_left := λm₁ m₂ f, by { ext z, simp [add_smul] }, smul_left := λc m f, by { ext z, simp [mul_smul] }, add_right := λm f₁ f₂, by { ext z, simp [smul_add] }, smul_right := λc m f, by { ext z, simp [smul_smul, mul_comm] }, bound := ⟨1, zero_lt_one, λm f, by simp⟩ } /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here a bounded linear map from `E × F` to `G`. The fact that this is indeed the derivative of `f` is proved in `is_bounded_bilinear_map.has_fderiv_at` in `deriv.lean`-/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : (E × F) →ₗ[𝕜] G := { to_fun := λq, f (p.1, q.2) + f (q.1, p.2), add := λq₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right] end, smul := λc q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } /-- The derivative of a bounded bilinear map at a point `p : E × F`, as a continuous linear map from `E × F` to `G`. -/ def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : (E × F) →L[𝕜] G := (h.linear_deriv p).with_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λq, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map 𝕜 f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl set_option class.instance_max_depth 100 /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λp : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos, hC⟩, refine is_linear_map.with_bound ⟨λp₁ p₂, _, λc p, _⟩ (C + C) (λp, _), { ext q, simp [h.add_left, h.add_right] }, { ext q, simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg (le_of_lt Cpos) (le_of_lt Cpos)) (norm_nonneg _)) (λq, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, le_of_lt Cpos, le_refl, le_max_left, le_max_right, mul_nonneg, norm_nonneg, norm_nonneg, norm_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map
cc8f38467b9573962532d01b015562a8a66071c7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/nat/choose/central.lean
c6755734d8394b2e4f67d6fba9977fab645dd048
[ "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
5,305
lean
/- Copyright (c) 2021 Patrick Stevens. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Stevens, Thomas Browning -/ import data.nat.choose.basic import tactic.linarith /-! # Central binomial coefficients > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves properties of the central binomial coefficients (that is, `nat.choose (2 * n) n`). ## Main definition and results * `nat.central_binom`: the central binomial coefficient, `(2 * n).choose n`. * `nat.succ_mul_central_binom_succ`: the inductive relationship between successive central binomial coefficients. * `nat.four_pow_lt_mul_central_binom`: an exponential lower bound on the central binomial coefficient. * `succ_dvd_central_binom`: The result that `n+1 ∣ n.central_binom`, ensuring that the explicit definition of the Catalan numbers is integer-valued. -/ namespace nat /-- The central binomial coefficient, `nat.choose (2 * n) n`. -/ def central_binom (n : ℕ) := (2 * n).choose n lemma central_binom_eq_two_mul_choose (n : ℕ) : central_binom n = (2 * n).choose n := rfl lemma central_binom_pos (n : ℕ) : 0 < central_binom n := choose_pos (nat.le_mul_of_pos_left zero_lt_two) lemma central_binom_ne_zero (n : ℕ) : central_binom n ≠ 0 := (central_binom_pos n).ne' @[simp] lemma central_binom_zero : central_binom 0 = 1 := choose_zero_right _ /-- The central binomial coefficient is the largest binomial coefficient. -/ lemma choose_le_central_binom (r n : ℕ) : choose (2 * n) r ≤ central_binom n := calc (2 * n).choose r ≤ (2 * n).choose (2 * n / 2) : choose_le_middle r (2 * n) ... = (2 * n).choose n : by rw nat.mul_div_cancel_left n zero_lt_two lemma two_le_central_binom (n : ℕ) (n_pos : 0 < n) : 2 ≤ central_binom n := calc 2 ≤ 2 * n : le_mul_of_pos_right n_pos ... = (2 * n).choose 1 : (choose_one_right (2 * n)).symm ... ≤ central_binom n : choose_le_central_binom 1 n /-- An inductive property of the central binomial coefficient. -/ lemma succ_mul_central_binom_succ (n : ℕ) : (n + 1) * central_binom (n + 1) = 2 * (2 * n + 1) * central_binom n := calc (n + 1) * (2 * (n + 1)).choose (n + 1) = (2 * n + 2).choose (n + 1) * (n + 1) : mul_comm _ _ ... = (2 * n + 1).choose n * (2 * n + 2) : by rw [choose_succ_right_eq, choose_mul_succ_eq] ... = 2 * ((2 * n + 1).choose n * (n + 1)) : by ring ... = 2 * ((2 * n + 1).choose n * ((2 * n + 1) - n)) : by rw [two_mul n, add_assoc, nat.add_sub_cancel_left] ... = 2 * ((2 * n).choose n * (2 * n + 1)) : by rw choose_mul_succ_eq ... = (2 * (2 * n + 1)) * (2 * n).choose n : by rw [mul_assoc, mul_comm (2 * n + 1)] /-- An exponential lower bound on the central binomial coefficient. This bound is of interest because it appears in [Tochiori's refinement of Erdős's proof of Bertrand's postulate](tochiori_bertrand). -/ lemma four_pow_lt_mul_central_binom (n : ℕ) (n_big : 4 ≤ n) : 4 ^ n < n * central_binom n := begin induction n using nat.strong_induction_on with n IH, rcases lt_trichotomy n 4 with (hn|rfl|hn), { clear IH, dec_trivial! }, { norm_num [central_binom, choose] }, obtain ⟨n, rfl⟩ : ∃ m, n = m + 1 := nat.exists_eq_succ_of_ne_zero (zero_lt_four.trans hn).ne', calc 4 ^ (n + 1) < 4 * (n * central_binom n) : (mul_lt_mul_left $ zero_lt_four' ℕ).mpr (IH n n.lt_succ_self (nat.le_of_lt_succ hn)) ... ≤ 2 * (2 * n + 1) * central_binom n : by { rw ← mul_assoc, linarith } ... = (n + 1) * central_binom (n + 1) : (succ_mul_central_binom_succ n).symm, end /-- An exponential lower bound on the central binomial coefficient. This bound is weaker than `nat.four_pow_lt_mul_central_binom`, but it is of historical interest because it appears in Erdős's proof of Bertrand's postulate. -/ lemma four_pow_le_two_mul_self_mul_central_binom : ∀ (n : ℕ) (n_pos : 0 < n), 4 ^ n ≤ (2 * n) * central_binom n | 0 pr := (nat.not_lt_zero _ pr).elim | 1 pr := by norm_num [central_binom, choose] | 2 pr := by norm_num [central_binom, choose] | 3 pr := by norm_num [central_binom, choose] | n@(m + 4) _ := calc 4 ^ n ≤ n * central_binom n : (four_pow_lt_mul_central_binom _ le_add_self).le ... ≤ 2 * n * central_binom n : by { rw [mul_assoc], refine le_mul_of_pos_left zero_lt_two } lemma two_dvd_central_binom_succ (n : ℕ) : 2 ∣ central_binom (n + 1) := begin use (n+1+n).choose n, rw [central_binom_eq_two_mul_choose, two_mul, ← add_assoc, choose_succ_succ, choose_symm_add, ← two_mul], end lemma two_dvd_central_binom_of_one_le {n : ℕ} (h : 0 < n) : 2 ∣ central_binom n := begin rw ← nat.succ_pred_eq_of_pos h, exact two_dvd_central_binom_succ n.pred, end /-- A crucial lemma to ensure that Catalan numbers can be defined via their explicit formula `catalan n = n.central_binom / (n + 1)`. -/ lemma succ_dvd_central_binom (n : ℕ) : (n + 1) ∣ n.central_binom := begin have h_s : (n+1).coprime (2*n+1), { rw [two_mul,add_assoc, coprime_add_self_right, coprime_self_add_left], exact coprime_one_left n }, apply h_s.dvd_of_dvd_mul_left, apply dvd_of_mul_dvd_mul_left zero_lt_two, rw [← mul_assoc, ← succ_mul_central_binom_succ, mul_comm], exact mul_dvd_mul_left _ (two_dvd_central_binom_succ n), end end nat