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
d2961627bd2a5c54232a0cc01dd7fcd350dfe52f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/ind8.lean
fedeb032faa40d028e02ad9313c8c0596cafeaf9
[ "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
336
lean
inductive Pair1 (A B : Type) | mk ( ) : A → B → Pair1 #check Pair1.mk #check Pair1.mk Prop Prop true false inductive Pair2 {A : Type} (B : A → Type) | mk ( ) : Π (a : A), B a → Pair2 #check @Pair2.mk #check Pair2.mk (λx, Prop) true false inductive Pair3 (A B : Type) | mk : A → B → Pair3 #check Pair3.mk true false
507b5bdacaf8983ac7c37d6f15dac4711e17bca6
a76f677b87d42a9470ba3a0a78cfddd3063118e6
/src/congruence/parallel.lean
baa9641428f58e308eee98dc86b5acc0fecf2776
[]
no_license
Ja1941/hilberts-axioms
50219c732ad5fa167408432e8c8baae259777a40
5b653a92e448b77da41c9893066b641bc4e6b316
refs/heads/master
1,693,238,884,856
1,635,702,120,000
1,635,702,120,000
385,546,384
9
1
null
null
null
null
UTF-8
Lean
false
false
6,748
lean
/- Copyright (c) 2021 Tianchen Zhao. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tianchen Zhao -/ import congruence.Elements /-! # Parallelism This file defines how two lines are parallel and proves properties of parallel lines such as alternative angles. ## Main definitions * `is_parallel` means the two lines have no intersections * `hilbert_plane_playfair` is a class extended `hilbert_plane` with Playfair axiom that deals with parallelism ## References * See [Geometry: Euclid and Beyond] -/ /--We can add Playfair axiom to Hilbert plane so that given a line and a point, we can find a unique parallel line containing this point. -/ class hilbert_plane_playfair extends hilbert_plane := (P : ∀ (a : pts) (l ∈ lines), (a ∉ l → ∃ l' ∈ lines, a ∈ l' ∧ (l ∥ₗ l')) ∧ (∀ m n ∈ lines, a ∈ m → (l ∥ₗ m) → a ∈ n → (l ∥ₗ n) → m = n)) open incidence_geometry incidence_order_geometry hilbert_plane hilbert_plane_playfair variables [CP : hilbert_plane_playfair] include CP lemma parallel_exist {l : set pts} (a : pts) (hl : l ∈ lines) (hal : a ∉ l) : ∃ l' ∈ lines, a ∈ l' ∧ (l ∥ₗ l') := by {rcases (P a l hl).1 hal with ⟨l', hl', hal', hll'⟩, exact ⟨l', hl', hal', hll'⟩} lemma parallel_unique {a : pts} {l m n : set pts} (hl : l ∈ lines) : m ∈ lines → n ∈ lines → a ∈ m → (l ∥ₗ m) → a ∈ n → (l ∥ₗ n) → m = n := (P a l hl).2 m n private lemma correspond_eq_iff_parallel_prep {a b c d e : pts} (hab : a ≠ b) (hcd : c ≠ d) (hcae : between c a e) (hbd : same_side_line (a-ₗc) b d) : ((∠ e a b) <ₐ (∠ a c d)) → ¬((a-ₗb) ∥ₗ (c-ₗd)) := begin have hac := (between_neq hcae).1.symm, intros h hf, rcases (three_pt_ang_lt.1 h) with ⟨p, hp, key⟩, rw [inside_three_pt_ang, line_symm] at hp, have hbp := same_side_line_trans (line_in_lines hac) hbd hp.1, have hcp := (same_side_line_neq' hbp).2.symm, have habcp := correspond_eq_parallel hab hcp hcae hbp (ang_congr_symm key), have hcdcp := parallel_unique (line_in_lines hab) (line_in_lines hcd) (line_in_lines hcp) (pt_left_in_line c d) hf (pt_left_in_line c p) habcp, have hcdp : col c d p, apply col_in12', rw hcdcp, exact pt_right_in_line c p, have hacd := (same_side_line_noncol hbd hac).2, have hacp := (same_side_line_noncol hbp hac).2, have hcdp := same_side_line_pt hcdp (a-ₗc) (line_in_lines hac) (pt_right_in_line a c) (noncol_in12 hacd) (noncol_in12 hacp) hp.1, rw ←ang_eq_same_side_pt a hcdp at key, have hac := (between_neq hcae).1.symm, have hacb := (same_side_line_noncol hbd hac).1, have hae := (between_neq hcae).2.2, have haeb := col_noncol (col12 (between_col hcae)) hacb hae, apply (ang_tri (ang_proper_iff_noncol.2 (noncol12 haeb)) (ang_proper_iff_noncol.2 hacd)).2.1, exact ⟨h, ang_congr_symm key⟩ end lemma correspond_eq_iff_parallel {a b c d e : pts} (hab : a ≠ b) (hcd : c ≠ d) (hcae : between c a e) (hbd : same_side_line (a-ₗc) b d) : ((∠ e a b) ≅ₐ (∠ a c d)) ↔ ((a-ₗb) ∥ₗ (c-ₗd)) := begin split; intro h, exact correspond_eq_parallel hab hcd hcae hbd h, { have hac := (between_neq hcae).1.symm, have hacb := (same_side_line_noncol hbd hac).1, have hae := (between_neq hcae).2.2, have haeb := col_noncol (col12 (between_col hcae)) hacb hae, have hacd := (same_side_line_noncol hbd hac).2, rcases (ang_tri (ang_proper_iff_noncol.2 (noncol12 haeb)) (ang_proper_iff_noncol.2 hacd)).1 with hf | h | hf, exfalso, exact correspond_eq_iff_parallel_prep hab hcd hcae hbd hf h, exact h, { rcases between_extend hab.symm with ⟨b', hbab'⟩, rcases between_extend hcd.symm with ⟨d', hdcd'⟩, exfalso, have hab' := (between_neq hbab').2.2, have hcd' := (between_neq hdcd').2.2, have hab'c := col_noncol (col12 (between_col hbab')) (noncol23 hacb) hab', have had'c := col_noncol (col12 (between_col hdcd')) (noncol123 hacd) hcd', apply correspond_eq_iff_parallel_prep hab' hcd' hcae _ _ _, { have hbb' := diff_side_pt_line (between_diff_side_pt.1 hbab') (line_in_lines hac) (pt_left_in_line a c) (noncol_in12 hacb) (noncol_in13 hab'c), have hdd' := diff_side_pt_line (between_diff_side_pt.1 hdcd') (line_in_lines hac) (pt_right_in_line a c) (noncol_in12 hacd) (noncol_in31 had'c), apply diff_side_line_cancel (line_in_lines hac) (diff_side_line_symm hbb'), exact same_diff_side_line (line_in_lines hac) hbd hdd', }, { apply ang_lt_supplementary hf; rw three_pt_ang_supplementary, exact ⟨hdcd', hacd, noncol132 had'c⟩, exact ⟨hbab', noncol12 haeb, noncol132 (col_noncol (col12 (between_col hbab')) (noncol23 haeb) hab')⟩ }, { rw two_pt_one_line (line_in_lines hab') (line_in_lines hab) hab (pt_left_in_line a b') (col_in23 (between_col hbab') hab') (pt_left_in_line a b) (pt_right_in_line a b), rw two_pt_one_line (line_in_lines hcd') (line_in_lines hcd) hcd (pt_left_in_line c d') (col_in23 (between_col hdcd') hcd') (pt_left_in_line c d) (pt_right_in_line c d), exact h } } } end lemma alternative_eq_iff_parallel {a b c d : pts} (hab : a ≠ b) (hcd : c ≠ d) (hac : a ≠ c) (hbd : diff_side_line (a-ₗc) b d) : ((∠ b a c) ≅ₐ (∠ d c a)) ↔ ((a-ₗb) ∥ₗ (c-ₗd)) := begin cases between_extend hab.symm with b' hbab', cases between_extend hac.symm with c' hcac', have hab' := (between_neq hbab').2.2, have hbac := noncol_in23' hac hbd.2.1, have hab'c := col_noncol (col12 (between_col hbab')) (noncol12 hbac) hab', have hbd' : same_side_line (a-ₗc) b' d, apply diff_side_line_cancel (line_in_lines hac) _ hbd, apply diff_side_line_symm, apply diff_side_pt_line (between_diff_side_pt.1 hbab') (line_in_lines hac) (pt_left_in_line a c) hbd.2.1 (noncol_in13 hab'c), have := correspond_eq_iff_parallel hab' hcd hcac' hbd', rw two_pt_one_line (line_in_lines hab') (line_in_lines hab) hab (pt_left_in_line a b') (col_in23 (between_col hbab') hab') (pt_left_in_line a b) (pt_right_in_line a b) at this, rw ←this, have := vertical_ang_congr hbac hbab' hcac', split; intro h, rw [ang_symm, ang_symm a c d], exact ang_congr_trans (ang_congr_symm this) h, rw ang_symm d c a, rw ang_symm at h, exact ang_congr_trans this h end /--Some hints for Tchsurvives: 1. extend_congr_seg' 2. SAS 3. alternative_eq_iff_parallel 4. ASA -/ lemma midpoint {a b c d e : pts} (habc : noncol a b c) (hadb : between a d b) (haec : between a e c) (hd : midpt d (a-ₛb)) : (midpt e (a-ₛc)) ↔ ((d-ₗe) ∥ₗ (b-ₗc)) := begin sorry end
99524be13ff2b5a7f3a6a132de0b753c27aa9e91
9028d228ac200bbefe3a711342514dd4e4458bff
/src/linear_algebra/dual.lean
72ee1a8423fd98600052aea97482b834c98436d8
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,791
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Fabian Glöckle -/ import linear_algebra.finite_dimensional import tactic.apply_fun noncomputable theory /-! # Dual vector spaces The dual space of an R-module M is the R-module of linear maps `M → R`. ## Main definitions * `dual R M` defines the dual space of M over R. * Given a basis for a K-vector space `V`, `is_basis.to_dual` produces a map from `V` to `dual K V`. * Given families of vectors `e` and `ε`, `dual_pair e ε` states that these families have the characteristic properties of a basis and a dual. ## Main results * `to_dual_equiv` : the dual space is linearly equivalent to the primal space. * `dual_pair.is_basis` and `dual_pair.eq_dual`: if `e` and `ε` form a dual pair, `e` is a basis and `ε` is its dual basis. ## Notation We sometimes use `V'` as local notation for `dual K V`. -/ namespace module variables (R : Type*) (M : Type*) variables [comm_ring R] [add_comm_group M] [module R M] /-- The dual space of an R-module M is the R-module of linear maps `M → R`. -/ @[derive [add_comm_group, module R]] def dual := M →ₗ[R] R namespace dual instance : inhabited (dual R M) := by dunfold dual; apply_instance instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩ /-- Maps a module M to the dual of the dual of M. See `vector_space.erange_coe` and `vector_space.eval_equiv`. -/ def eval : M →ₗ[R] (dual R (dual R M)) := linear_map.flip linear_map.id lemma eval_apply (v : M) (a : dual R M) : (eval R M v) a = a v := begin dunfold eval, rw [linear_map.flip_apply, linear_map.id_apply] end variables {R M} {M' : Type*} [add_comm_group M'] [module R M'] /-- The transposition of linear maps, as a linear map from `M →ₗ[R] M'` to `dual R M' →ₗ[R] dual R M`. -/ def transpose : (M →ₗ[R] M') →ₗ[R] (dual R M' →ₗ[R] dual R M) := (linear_map.llcomp R M M' R).flip lemma transpose_apply (u : M →ₗ[R] M') (l : dual R M') : transpose u l = l.comp u := rfl variables {M'' : Type*} [add_comm_group M''] [module R M''] lemma transpose_comp (u : M' →ₗ[R] M'') (v : M →ₗ[R] M') : transpose (u.comp v) = (transpose v).comp (transpose u) := rfl end dual end module namespace is_basis universes u v w variables {K : Type u} {V : Type v} {ι : Type w} variables [field K] [add_comm_group V] [vector_space K V] open vector_space module module.dual submodule linear_map cardinal function variables [de : decidable_eq ι] variables {B : ι → V} (h : is_basis K B) include de h /-- The linear map from a vector space equipped with basis to its dual vector space, taking basis elements to corresponding dual basis elements. -/ def to_dual : V →ₗ[K] module.dual K V := h.constr $ λ v, h.constr $ λ w, if w = v then 1 else 0 lemma to_dual_apply (i j : ι) : h.to_dual (B i) (B j) = if i = j then 1 else 0 := by { erw [constr_basis h, constr_basis h], ac_refl } @[simp] lemma to_dual_total_left (f : ι →₀ K) (i : ι) : h.to_dual (finsupp.total ι V K B f) (B i) = f i := begin rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum, linear_map.sum_apply], simp_rw [linear_map.map_smul, linear_map.smul_apply, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq'], split_ifs with h, { refl }, { rw finsupp.not_mem_support_iff.mp h } end @[simp] lemma to_dual_total_right (f : ι →₀ K) (i : ι) : h.to_dual (B i) (finsupp.total ι V K B f) = f i := begin rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum], simp_rw [linear_map.map_smul, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq], split_ifs with h, { refl }, { rw finsupp.not_mem_support_iff.mp h } end lemma to_dual_apply_left (v : V) (i : ι) : h.to_dual v (B i) = h.repr v i := by rw [← h.to_dual_total_left, h.total_repr] lemma to_dual_apply_right (i : ι) (v : V) : h.to_dual (B i) v = h.repr v i := by rw [← h.to_dual_total_right, h.total_repr] /-- `h.to_dual_flip v` is the linear map sending `w` to `h.to_dual w v`. -/ def to_dual_flip (v : V) : (V →ₗ[K] K) := (linear_map.flip h.to_dual).to_fun v omit de h -- TODO: unify this with `finsupp.lapply`. /-- Evaluation of finitely supported functions at a fixed point `i`, as a `K`-linear map. -/ def eval_finsupp_at (i : ι) : (ι →₀ K) →ₗ[K] K := { to_fun := λ f, f i, map_add' := by intros; rw finsupp.add_apply, map_smul' := by intros; rw finsupp.smul_apply } include h /-- `h.coord_fun i` sends vectors to their `i`'th coordinate with respect to the basis `h`. -/ def coord_fun (i : ι) : (V →ₗ[K] K) := linear_map.comp (eval_finsupp_at i) h.repr lemma coord_fun_eq_repr (v : V) (i : ι) : h.coord_fun i v = h.repr v i := rfl include de -- TODO: this lemma should be called something like `to_dual_flip_apply` lemma to_dual_swap_eq_to_dual (v w : V) : h.to_dual_flip v w = h.to_dual w v := rfl lemma to_dual_eq_repr (v : V) (i : ι) : (h.to_dual v) (B i) = h.repr v i := h.to_dual_apply_left v i lemma to_dual_eq_equiv_fun [fintype ι] (v : V) (i : ι) : (h.to_dual v) (B i) = h.equiv_fun v i := by rw [h.equiv_fun_apply, to_dual_eq_repr] lemma to_dual_inj (v : V) (a : h.to_dual v = 0) : v = 0 := begin rw [← mem_bot K, ← h.repr_ker, mem_ker], apply finsupp.ext, intro b, rw [←to_dual_eq_repr _ _ _, a], refl end theorem to_dual_ker : h.to_dual.ker = ⊥ := ker_eq_bot'.mpr h.to_dual_inj theorem to_dual_range [fin : fintype ι] : h.to_dual.range = ⊤ := begin rw eq_top_iff', intro f, rw linear_map.mem_range, let lin_comb : ι →₀ K := finsupp.on_finset fin.elems (λ i, f.to_fun (B i)) _, { use finsupp.total ι V K B lin_comb, apply h.ext, { intros i, rw [h.to_dual_eq_repr _ i, repr_total h], { refl }, { rw [finsupp.mem_supported], exact λ _ _, set.mem_univ _ } } }, { intros a _, apply fin.complete } end /-- Maps a basis for `V` to a basis for the dual space. -/ def dual_basis : ι → dual K V := λ i, h.to_dual (B i) theorem dual_lin_independent : linear_independent K h.dual_basis := begin apply linear_independent.image h.1, rw to_dual_ker, exact disjoint_bot_right end @[simp] lemma dual_basis_apply_self (i j : ι) : h.dual_basis i (B j) = if i = j then 1 else 0 := h.to_dual_apply i j /-- A vector space is linearly equivalent to its dual space. -/ def to_dual_equiv [fintype ι] : V ≃ₗ[K] (dual K V) := linear_equiv.of_bijective h.to_dual h.to_dual_ker h.to_dual_range theorem dual_basis_is_basis [fintype ι] : is_basis K h.dual_basis := h.to_dual_equiv.is_basis h @[simp] lemma total_dual_basis [fintype ι] (f : ι →₀ K) (i : ι) : finsupp.total ι (dual K V) K h.dual_basis f (B i) = f i := begin rw [finsupp.total_apply, finsupp.sum_fintype, linear_map.sum_apply], { simp_rw [smul_apply, smul_eq_mul, dual_basis_apply_self, mul_boole, finset.sum_ite_eq', if_pos (finset.mem_univ i)] }, { intro, rw zero_smul }, end lemma dual_basis_repr [fintype ι] (l : dual K V) (i : ι) : h.dual_basis_is_basis.repr l i = l (B i) := by rw [← total_dual_basis h, is_basis.total_repr h.dual_basis_is_basis l ] lemma dual_basis_equiv_fun [fintype ι] (l : dual K V) (i : ι) : h.dual_basis_is_basis.equiv_fun l i = l (B i) := by rw [is_basis.equiv_fun_apply, dual_basis_repr] lemma dual_basis_apply [fintype ι] (i : ι) (v : V) : h.dual_basis i v = h.equiv_fun v i := h.to_dual_apply_right i v @[simp] lemma to_dual_to_dual [fintype ι] : (h.dual_basis_is_basis.to_dual).comp h.to_dual = eval K V := begin refine h.ext (λ i, h.dual_basis_is_basis.ext (λ j, _)), dunfold eval, rw [linear_map.flip_apply, linear_map.id_apply, linear_map.comp_apply], apply eq.trans (to_dual_apply h.dual_basis_is_basis i j), { dunfold dual_basis, rw to_dual_apply, by_cases h : i = j, { rw [if_pos h, if_pos h.symm] }, { rw [if_neg h, if_neg (ne.symm h)] } } end omit de theorem dual_dim_eq [fintype ι] : cardinal.lift.{v u} (dim K V) = dim K (dual K V) := begin classical, have := linear_equiv.dim_eq_lift h.to_dual_equiv, simp only [cardinal.lift_umax] at this, rw [this, ← cardinal.lift_umax], apply cardinal.lift_id, end end is_basis namespace vector_space universes u v variables {K : Type u} {V : Type v} variables [field K] [add_comm_group V] [vector_space K V] open module module.dual submodule linear_map cardinal is_basis theorem eval_ker : (eval K V).ker = ⊥ := begin classical, rw ker_eq_bot', intros v h, rw linear_map.ext_iff at h, by_contradiction H, rcases exists_subset_is_basis (linear_independent_singleton H) with ⟨b, hv, hb⟩, swap 4, assumption, have hv' : v = (coe : b → V) ⟨v, hv (set.mem_singleton v)⟩ := rfl, let hx := h (hb.to_dual v), rw [eval_apply, hv', to_dual_apply, if_pos rfl, zero_apply] at hx, exact one_ne_zero hx end theorem dual_dim_eq (h : dim K V < omega) : cardinal.lift.{v u} (dim K V) = dim K (dual K V) := begin classical, rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩, resetI, exact hb.dual_dim_eq end lemma erange_coe (h : dim K V < omega) : (eval K V).range = ⊤ := begin classical, rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩, unfreezingI { rw [← hb.to_dual_to_dual, range_comp, hb.to_dual_range, map_top, to_dual_range _] }, apply_instance end /-- A vector space is linearly equivalent to the dual of its dual space. -/ def eval_equiv (h : dim K V < omega) : V ≃ₗ[K] dual K (dual K V) := linear_equiv.of_bijective (eval K V) eval_ker (erange_coe h) end vector_space section dual_pair open vector_space module module.dual linear_map function universes u v w variables {K : Type u} {V : Type v} {ι : Type w} [decidable_eq ι] variables [field K] [add_comm_group V] [vector_space K V] local notation `V'` := dual K V /-- `e` and `ε` have characteristic properties of a basis and its dual -/ @[nolint has_inhabited_instance] structure dual_pair (e : ι → V) (ε : ι → V') := (eval : ∀ i j : ι, ε i (e j) = if i = j then 1 else 0) (total : ∀ {v : V}, (∀ i, ε i v = 0) → v = 0) [finite : ∀ v : V, fintype {i | ε i v ≠ 0}] end dual_pair namespace dual_pair open vector_space module module.dual linear_map function universes u v w variables {K : Type u} {V : Type v} {ι : Type w} [dι : decidable_eq ι] variables [field K] [add_comm_group V] [vector_space K V] variables {e : ι → V} {ε : ι → dual K V} (h : dual_pair e ε) include h /-- The coefficients of `v` on the basis `e` -/ def coeffs (v : V) : ι →₀ K := { to_fun := λ i, ε i v, support := by { haveI := h.finite v, exact {i : ι | ε i v ≠ 0}.to_finset }, mem_support_to_fun := by {intro i, rw set.mem_to_finset, exact iff.rfl } } @[simp] lemma coeffs_apply (v : V) (i : ι) : h.coeffs v i = ε i v := rfl omit h /-- linear combinations of elements of `e`. This is a convenient abbreviation for `finsupp.total _ V K e l` -/ def lc (e : ι → V) (l : ι →₀ K) : V := l.sum (λ (i : ι) (a : K), a • (e i)) include h lemma dual_lc (l : ι →₀ K) (i : ι) : ε i (dual_pair.lc e l) = l i := begin erw linear_map.map_sum, simp only [h.eval, map_smul, smul_eq_mul], rw finset.sum_eq_single i, { simp }, { intros q q_in q_ne, simp [q_ne.symm] }, { intro p_not_in, simp [finsupp.not_mem_support_iff.1 p_not_in] }, end @[simp] lemma coeffs_lc (l : ι →₀ K) : h.coeffs (dual_pair.lc e l) = l := by { ext i, rw [h.coeffs_apply, h.dual_lc] } /-- For any v : V n, \sum_{p ∈ Q n} (ε p v) • e p = v -/ lemma decomposition (v : V) : dual_pair.lc e (h.coeffs v) = v := begin refine eq_of_sub_eq_zero (h.total _), intros i, simp [-sub_eq_add_neg, linear_map.map_sub, h.dual_lc, sub_eq_zero_iff_eq] end lemma mem_of_mem_span {H : set ι} {x : V} (hmem : x ∈ submodule.span K (e '' H)) : ∀ i : ι, ε i x ≠ 0 → i ∈ H := begin intros i hi, rcases (finsupp.mem_span_iff_total _).mp hmem with ⟨l, supp_l, sum_l⟩, change dual_pair.lc e l = x at sum_l, rw finsupp.mem_supported' at supp_l, apply classical.by_contradiction, intro i_not, apply hi, rw ← sum_l, simpa [h.dual_lc] using supp_l i i_not end lemma is_basis : is_basis K e := begin split, { rw linear_independent_iff, intros l H, change dual_pair.lc e l = 0 at H, ext i, apply_fun ε i at H, simpa [h.dual_lc] using H }, { rw submodule.eq_top_iff', intro v, rw [← set.image_univ, finsupp.mem_span_iff_total], exact ⟨h.coeffs v, by simp, h.decomposition v⟩ }, end lemma eq_dual : ε = is_basis.dual_basis h.is_basis := begin funext i, refine h.is_basis.ext (λ _, _), erw [is_basis.to_dual_apply, h.eval] end end dual_pair
bc68bb6573e2efb798e1afff2cbeb1b7a8d045ea
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/Tactic/LinearArith.lean
cadcc6067f3804c211b3f738caf40b021d9898a7
[ "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
325
lean
/- Copyright (c) 2022 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.Meta.Tactic.LinearArith.Solver import Lean.Meta.Tactic.LinearArith.Nat import Lean.Meta.Tactic.LinearArith.Main import Lean.Meta.Tactic.LinearArith.Simp
be1bf382d8a51cbfba34dae2c768d96e919aaa8f
0851884047bb567d19e188e8f1ad959c5ae9c5ce
/src/M1P2/Sheet-7.lean
f0d7d2f00790d2fe4f30d7e1dcb641ef3fb28e55
[ "Apache-2.0" ]
permissive
yz5216/xena-UROP-2018
00d3f71a831324966d40d302544ed2cbbec3fd0f
5e9da9dc64dc51897677f8b73ab9f94061a8d977
refs/heads/master
1,584,922,436,989
1,531,487,250,000
1,531,487,250,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,996
lean
import data.nat.prime data.nat.basic data.int.modeq open nat --sheet 7 -- *1. Suppose that G is a finite group which contains elements of each of the orders 1, 2, . . . , 10. What is the smallest possible value of |G|? Find a group of this size which does have elements of each of these orders. theorem sheet07_q1: -- 2. What is the largest order of an element of S8? theorem sheet07_q2: -- 3. Let G be a cyclic group of order n, and g a generator. Show that gk is a -- generator for G if and only if hcf(k, n) = 1. theorem sheet07_q3: -- 4. LetG and H be finite groups. Let G×H be the set {(g,h)|g∈G,h∈H} with the binary operation (g1, h1) ∗ (g2, h2) = (g1g2, h1h2). -- (a) Show that (G×H,∗) is a group. theorem sheet07_q4a: -- (b) Show that if g ∈ G and h ∈ H have orders a, b respectively, then the order of (g,h) in G×H is the lowest common multiple of a and b. theorem sheet07_q4b: -- (c) Show that G × H is cyclic if and only if G and H are both cyclic, and hcf(|G|,|H|) = 1. theorem sheet07_q4c: -- 6. (a) Find the remainder when 5110 is divided by 13. theorem sheet07_q6a: -- (b) Find the inverses of [2] and of [120] in Z×9871. (The number 9871 is prime.) theorem sheet07_q6b: -- (c) Use Fermat’s Little Theorem to show that n17 ≡ n mod 255 for all n ∈ Z. (d) Prove that if p and q are distinct prime numbers then -- pq−1 + qp−1 ≡ 1 mod pq. theorem sheet07_q6c: -- 7. Let p be an odd prime. -- (a) Prove that (p − 1)! ≡ −1 mod p (Wilson’s Theorem). theorem sheet07_q7a (p : ℕ) (hp : prime p) : fact (p-1) ≡ -1 [ZMOD p] := sorry --- (b) Show that if p≡1 mod 4,then there is x∈Z with x^2 ≡−1 modp. theorem sheet07_q7b (p : ℕ) (hp : prime p) : p ≡ 1 [ZMOD 4] → ∃ x ∈ ℤ ∧ x^2 ≡ -1 [ZMOD p] := sorry -- (c) Show that if p ≠ 2 and there is x∈Z with x^2 ≡−1 modp,then p ≡ 1 mod 4. theorem sheet07_q7c (p : ℕ) (hp : prime p) : p ≠ 2 ∧ ∃ x ∈ ℤ ∧ x^2 ≡ -1 [ZMOD p] → p ≡ 1 [ZMOD 4] := sorry
d50653bff88a4456a1c131d5a7ef2fe87b89db33
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/logic/is_empty.lean
de2594018ec81641600cb6e91e2b367a1060de66
[ "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
4,674
lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import logic.basic import tactic.protected /-! # Types that are empty In this file we define a typeclass `is_empty`, which expresses that a type has no elements. ## Main declaration * `is_empty`: a typeclass that expresses that a type is empty. -/ variables {α β γ : Sort*} /-- `is_empty α` expresses that `α` is empty. -/ @[protect_proj] class is_empty (α : Sort*) : Prop := (false : α → false) instance : is_empty empty := ⟨empty.elim⟩ instance : is_empty pempty := ⟨pempty.elim⟩ instance : is_empty false := ⟨id⟩ instance : is_empty (fin 0) := ⟨λ n, nat.not_lt_zero n.1 n.2⟩ protected lemma function.is_empty [is_empty β] (f : α → β) : is_empty α := ⟨λ x, is_empty.false (f x)⟩ instance {p : α → Sort*} [h : nonempty α] [∀ x, is_empty (p x)] : is_empty (Π x, p x) := h.elim $ λ x, function.is_empty $ function.eval x instance pprod.is_empty_left [is_empty α] : is_empty (pprod α β) := function.is_empty pprod.fst instance pprod.is_empty_right [is_empty β] : is_empty (pprod α β) := function.is_empty pprod.snd instance prod.is_empty_left {α β} [is_empty α] : is_empty (α × β) := function.is_empty prod.fst instance prod.is_empty_right {α β} [is_empty β] : is_empty (α × β) := function.is_empty prod.snd instance [is_empty α] [is_empty β] : is_empty (psum α β) := ⟨λ x, psum.rec is_empty.false is_empty.false x⟩ instance {α β} [is_empty α] [is_empty β] : is_empty (α ⊕ β) := ⟨λ x, sum.rec is_empty.false is_empty.false x⟩ /-- subtypes of an empty type are empty -/ instance [is_empty α] (p : α → Prop) : is_empty (subtype p) := ⟨λ x, is_empty.false x.1⟩ /-- subtypes by an all-false predicate are false. -/ lemma subtype.is_empty_of_false {p : α → Prop} (hp : ∀ a, ¬(p a)) : is_empty (subtype p) := ⟨λ x, hp _ x.2⟩ /-- subtypes by false are false. -/ instance subtype.is_empty_false : is_empty {a : α // false} := subtype.is_empty_of_false (λ a, id) /- Test that `pi.is_empty` finds this instance. -/ example [h : nonempty α] [is_empty β] : is_empty (α → β) := by apply_instance /-- Eliminate out of a type that `is_empty` (without using projection notation). -/ @[elab_as_eliminator] def is_empty_elim [is_empty α] {p : α → Sort*} (a : α) : p a := (is_empty.false a).elim lemma is_empty_iff : is_empty α ↔ α → false := ⟨@is_empty.false α, is_empty.mk⟩ namespace is_empty open function /-- Eliminate out of a type that `is_empty` (using projection notation). -/ protected def elim (h : is_empty α) {p : α → Sort*} (a : α) : p a := is_empty_elim a /-- Non-dependent version of `is_empty.elim`. Helpful if the elaborator cannot elaborate `h.elim a` correctly. -/ protected def elim' {β : Sort*} (h : is_empty α) (a : α) : β := h.elim a protected lemma prop_iff {p : Prop} : is_empty p ↔ ¬ p := is_empty_iff variables [is_empty α] lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ true := iff_true_intro is_empty_elim lemma exists_iff {p : α → Prop} : (∃ a, p a) ↔ false := iff_false_intro $ λ ⟨x, hx⟩, is_empty.false x @[priority 100] -- see Note [lower instance priority] instance : subsingleton α := ⟨is_empty_elim⟩ end is_empty @[simp] lemma not_nonempty_iff : ¬ nonempty α ↔ is_empty α := ⟨λ h, ⟨λ x, h ⟨x⟩⟩, λ h1 h2, h2.elim h1.elim⟩ @[simp] lemma not_is_empty_iff : ¬ is_empty α ↔ nonempty α := not_iff_comm.mp not_nonempty_iff @[simp] lemma is_empty_pi {π : α → Sort*} : is_empty (Π a, π a) ↔ ∃ a, is_empty (π a) := by simp only [← not_nonempty_iff, classical.nonempty_pi, not_forall] @[simp] lemma is_empty_prod {α β : Type*} : is_empty (α × β) ↔ is_empty α ∨ is_empty β := by simp only [← not_nonempty_iff, nonempty_prod, not_and_distrib] @[simp] lemma is_empty_pprod : is_empty (pprod α β) ↔ is_empty α ∨ is_empty β := by simp only [← not_nonempty_iff, nonempty_pprod, not_and_distrib] @[simp] lemma is_empty_sum {α β} : is_empty (α ⊕ β) ↔ is_empty α ∧ is_empty β := by simp only [← not_nonempty_iff, nonempty_sum, not_or_distrib] @[simp] lemma is_empty_psum {α β} : is_empty (psum α β) ↔ is_empty α ∧ is_empty β := by simp only [← not_nonempty_iff, nonempty_psum, not_or_distrib] variables (α) lemma is_empty_or_nonempty : is_empty α ∨ nonempty α := (em $ is_empty α).elim or.inl $ or.inr ∘ not_is_empty_iff.mp @[simp] lemma not_is_empty_of_nonempty [h : nonempty α] : ¬ is_empty α := not_is_empty_iff.mpr h
2ee377cd043b93a0249c45d83b7c7d47dcb3ca4d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/smt/rsimp_auto.lean
a167eea7ede40587ae6c664e42625c1628a774f5
[]
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
1,148
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.smt.smt_tactic import Mathlib.Lean3Lib.init.meta.fun_info import Mathlib.Lean3Lib.init.meta.rb_map universes l namespace Mathlib /-- Create a rsimp attribute named `attr_name`, the attribute declaration is named `attr_decl_name`. The cached hinst_lemmas structure is built using the lemmas marked with simp attribute `simp_attr_name`, but *not* marked with `ex_attr_name`. We say `ex_attr_name` is the "exception set". It is useful for excluding lemmas in `simp_attr_name` which are not good or redundant for ematching. -/ /- The following lemmas are not needed by rsimp, and they actually hurt performance since they generate a lot of instances. -/ namespace rsimp /-- Return the size of term by considering only explicit arguments. -/ /-- Choose smallest element (with respect to explicit_size) in `e`s equivalence class. -/ structure config where attr_name : name max_rounds : ℕ end Mathlib
8bdb247653227e0d576b8d411b420767cc796203
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/eq1.lean
cc0396146c8f52d179096cdac15430306da24701
[ "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
451
lean
inductive day := monday | tuesday | wednesday | thursday | friday | saturday | sunday open day definition next_weekday : day → day | next_weekday monday := tuesday | next_weekday tuesday := wednesday | next_weekday wednesday := thursday | next_weekday thursday := friday | next_weekday friday := monday | next_weekday saturday := monday | next_weekday sunday := monday example : next_weekday (next_weekday monday) = wednesday := rfl
62c82b667b67605d220db195d1d6b523a539934a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/lie/basic.lean
536ecab02cdb67e157618e9dfe58af0345192a49
[]
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
112,578
lean
/- Copyright (c) 2019 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.bracket import Mathlib.algebra.algebra.basic import Mathlib.linear_algebra.bilinear_form import Mathlib.linear_algebra.matrix import Mathlib.order.preorder_hom import Mathlib.order.compactly_generated import Mathlib.tactic.noncomm_ring import Mathlib.PostPort universes v l u w w₁ w₂ namespace Mathlib /-! # Lie algebras This file defines Lie rings, and Lie algebras over a commutative ring. It shows how these arise from associative rings and algebras via the ring commutator. In particular it defines the Lie algebra of endomorphisms of a module as well as of the algebra of square matrices over a commutative ring. It also includes definitions of morphisms of Lie algebras, Lie subalgebras, Lie modules, Lie submodules, and the quotient of a Lie algebra by an ideal. ## Notations We introduce the notation ⁅x, y⁆ for the Lie bracket. Note that these are the Unicode "square with quill" brackets rather than the usual square brackets. Working over a fixed commutative ring `R`, we introduce the notations: * `L →ₗ⁅R⁆ L'` for a morphism of Lie algebras, * `L ≃ₗ⁅R⁆ L'` for an equivalence of Lie algebras, * `M →ₗ⁅R,L⁆ N` for a morphism of Lie algebra modules `M`, `N` over a Lie algebra `L`, * `M ≃ₗ⁅R,L⁆ N` for an equivalence of Lie algebra modules `M`, `N` over a Lie algebra `L`. ## Implementation notes Lie algebras are defined as modules with a compatible Lie ring structure and thus, like modules, are partially unbundled. ## References * [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*][bourbaki1975] ## Tags lie bracket, ring commutator, jacobi identity, lie ring, lie algebra -/ /-- A Lie ring is an additive group with compatible product, known as the bracket, satisfying the Jacobi identity. The bracket is not associative unless it is identically zero. -/ class lie_ring (L : Type v) extends has_bracket L L, add_comm_group L where add_lie : ∀ (x y z : L), has_bracket.bracket (x + y) z = has_bracket.bracket x z + has_bracket.bracket y z lie_add : ∀ (x y z : L), has_bracket.bracket x (y + z) = has_bracket.bracket x y + has_bracket.bracket x z lie_self : ∀ (x : L), has_bracket.bracket x x = 0 leibniz_lie : ∀ (x y z : L), has_bracket.bracket x (has_bracket.bracket y z) = has_bracket.bracket (has_bracket.bracket x y) z + has_bracket.bracket y (has_bracket.bracket x z) /-- A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring. -/ class lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] extends semimodule R L where lie_smul : ∀ (t : R) (x y : L), has_bracket.bracket x (t • y) = t • has_bracket.bracket x y /-- A Lie ring module is an additive group, together with an additive action of a Lie ring on this group, such that the Lie bracket acts as the commutator of endomorphisms. (For representations of Lie *algebras* see `lie_module`.) -/ class lie_ring_module (L : Type v) (M : Type w) [lie_ring L] [add_comm_group M] extends has_bracket L M where add_lie : ∀ (x y : L) (m : M), has_bracket.bracket (x + y) m = has_bracket.bracket x m + has_bracket.bracket y m lie_add : ∀ (x : L) (m n : M), has_bracket.bracket x (m + n) = has_bracket.bracket x m + has_bracket.bracket x n leibniz_lie : ∀ (x y : L) (m : M), has_bracket.bracket x (has_bracket.bracket y m) = has_bracket.bracket (has_bracket.bracket x y) m + has_bracket.bracket y (has_bracket.bracket x m) /-- A Lie module is a module over a commutative ring, together with a linear action of a Lie algebra on this module, such that the Lie bracket acts as the commutator of endomorphisms. -/ class lie_module (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] where smul_lie : ∀ (t : R) (x : L) (m : M), has_bracket.bracket (t • x) m = t • has_bracket.bracket x m lie_smul : ∀ (t : R) (x : L) (m : M), has_bracket.bracket x (t • m) = t • has_bracket.bracket x m @[simp] theorem add_lie {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (y : L) (m : M) : has_bracket.bracket (x + y) m = has_bracket.bracket x m + has_bracket.bracket y m := lie_ring_module.add_lie x y m @[simp] theorem lie_add {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (m : M) (n : M) : has_bracket.bracket x (m + n) = has_bracket.bracket x m + has_bracket.bracket x n := lie_ring_module.lie_add x m n @[simp] theorem smul_lie {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (t : R) (x : L) (m : M) : has_bracket.bracket (t • x) m = t • has_bracket.bracket x m := lie_module.smul_lie t x m @[simp] theorem lie_smul {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (t : R) (x : L) (m : M) : has_bracket.bracket x (t • m) = t • has_bracket.bracket x m := lie_module.lie_smul t x m theorem leibniz_lie {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (y : L) (m : M) : has_bracket.bracket x (has_bracket.bracket y m) = has_bracket.bracket (has_bracket.bracket x y) m + has_bracket.bracket y (has_bracket.bracket x m) := lie_ring_module.leibniz_lie x y m @[simp] theorem lie_zero {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) : has_bracket.bracket x 0 = 0 := add_monoid_hom.map_zero (add_monoid_hom.mk' (has_bracket.bracket x) (lie_add x)) @[simp] theorem zero_lie {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (m : M) : has_bracket.bracket 0 m = 0 := add_monoid_hom.map_zero (add_monoid_hom.mk' (fun (x : L) => has_bracket.bracket x m) fun (x y : L) => add_lie x y m) @[simp] theorem lie_self {L : Type v} [lie_ring L] (x : L) : has_bracket.bracket x x = 0 := lie_ring.lie_self x protected instance lie_ring_self_module {L : Type v} [lie_ring L] : lie_ring_module L L := lie_ring_module.mk sorry sorry sorry @[simp] theorem lie_skew {L : Type v} [lie_ring L] (x : L) (y : L) : -has_bracket.bracket y x = has_bracket.bracket x y := sorry /-- Every Lie algebra is a module over itself. -/ protected instance lie_algebra_self_module {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] : lie_module R L L := lie_module.mk sorry lie_algebra.lie_smul @[simp] theorem neg_lie {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (m : M) : has_bracket.bracket (-x) m = -has_bracket.bracket x m := sorry @[simp] theorem lie_neg {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (m : M) : has_bracket.bracket x (-m) = -has_bracket.bracket x m := sorry @[simp] theorem gsmul_lie {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (m : M) (a : ℤ) : has_bracket.bracket (a • x) m = a • has_bracket.bracket x m := add_monoid_hom.map_gsmul (add_monoid_hom.mk (fun (x : L) => has_bracket.bracket x m) (zero_lie m) fun (_x _x_1 : L) => add_lie _x _x_1 m) x a @[simp] theorem lie_gsmul {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (m : M) (a : ℤ) : has_bracket.bracket x (a • m) = a • has_bracket.bracket x m := add_monoid_hom.map_gsmul (add_monoid_hom.mk (fun (m : M) => has_bracket.bracket x m) (lie_zero x) fun (_x _x_1 : M) => lie_add x _x _x_1) m a @[simp] theorem lie_lie {L : Type v} {M : Type w} [lie_ring L] [add_comm_group M] [lie_ring_module L M] (x : L) (y : L) (m : M) : has_bracket.bracket (has_bracket.bracket x y) m = has_bracket.bracket x (has_bracket.bracket y m) - has_bracket.bracket y (has_bracket.bracket x m) := sorry theorem lie_jacobi {L : Type v} [lie_ring L] (x : L) (y : L) (z : L) : has_bracket.bracket x (has_bracket.bracket y z) + has_bracket.bracket y (has_bracket.bracket z x) + has_bracket.bracket z (has_bracket.bracket x y) = 0 := sorry namespace lie_algebra /-- A morphism of Lie algebras is a linear map respecting the bracket operations. -/ structure morphism (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends linear_map R L L' where map_lie : ∀ {x y : L}, to_fun (has_bracket.bracket x y) = has_bracket.bracket (to_fun x) (to_fun y) protected instance linear_map.has_coe {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : has_coe (morphism R L₁ L₂) (linear_map R L₁ L₂) := has_coe.mk morphism.to_linear_map /-- see Note [function coercion] -/ protected instance morphism.has_coe_to_fun {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : has_coe_to_fun (morphism R L₁ L₂) := has_coe_to_fun.mk (fun (x : morphism R L₁ L₂) => L₁ → L₂) morphism.to_fun @[simp] theorem coe_mk {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : L₁ → L₂) (h₁ : ∀ (x y : L₁), f (x + y) = f x + f y) (h₂ : ∀ (m : R) (x : L₁), f (m • x) = m • f x) (h₃ : ∀ {x y : L₁}, f (has_bracket.bracket x y) = has_bracket.bracket (f x) (f y)) : ⇑(morphism.mk f h₁ h₂ h₃) = f := rfl @[simp] theorem coe_to_linear_map {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) : ⇑↑f = ⇑f := rfl @[simp] theorem morphism.map_smul {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) (c : R) (x : L₁) : coe_fn f (c • x) = c • coe_fn f x := linear_map.map_smul (↑f) c x @[simp] theorem morphism.map_add {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) (x : L₁) (y : L₁) : coe_fn f (x + y) = coe_fn f x + coe_fn f y := linear_map.map_add (↑f) x y @[simp] theorem map_lie {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) (x : L₁) (y : L₁) : coe_fn f (has_bracket.bracket x y) = has_bracket.bracket (coe_fn f x) (coe_fn f y) := morphism.map_lie f @[simp] theorem map_zero {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) : coe_fn f 0 = 0 := linear_map.map_zero ↑f /-- The constant 0 map is a Lie algebra morphism. -/ protected instance morphism.has_zero {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : HasZero (morphism R L₁ L₂) := { zero := morphism.mk (linear_map.to_fun 0) sorry sorry sorry } /-- The identity map is a Lie algebra morphism. -/ protected instance morphism.has_one {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] : HasOne (morphism R L₁ L₁) := { one := morphism.mk (linear_map.to_fun 1) sorry sorry sorry } protected instance morphism.inhabited {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : Inhabited (morphism R L₁ L₂) := { default := 0 } theorem morphism.coe_injective {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : function.injective fun (f : morphism R L₁ L₂) => (fun (this : L₁ → L₂) => this) ⇑f := sorry theorem morphism.ext {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] {f : morphism R L₁ L₂} {g : morphism R L₁ L₂} (h : ∀ (x : L₁), coe_fn f x = coe_fn g x) : f = g := morphism.coe_injective (funext h) theorem morphism.ext_iff {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] {f : morphism R L₁ L₂} {g : morphism R L₁ L₂} : f = g ↔ ∀ (x : L₁), coe_fn f x = coe_fn g x := { mp := fun (ᾰ : f = g) (x : L₁) => Eq._oldrec (Eq.refl (coe_fn f x)) ᾰ, mpr := morphism.ext } /-- The composition of morphisms is a morphism. -/ def morphism.comp {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] (f : morphism R L₂ L₃) (g : morphism R L₁ L₂) : morphism R L₁ L₃ := morphism.mk (linear_map.to_fun (linear_map.comp (morphism.to_linear_map f) (morphism.to_linear_map g))) sorry sorry sorry @[simp] theorem morphism.comp_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] (f : morphism R L₂ L₃) (g : morphism R L₁ L₂) (x : L₁) : coe_fn (morphism.comp f g) x = coe_fn f (coe_fn g x) := rfl theorem morphism.comp_coe {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] (f : morphism R L₂ L₃) (g : morphism R L₁ L₂) : ⇑f ∘ ⇑g = ⇑(morphism.comp f g) := rfl /-- The inverse of a bijective morphism is a morphism. -/ def morphism.inverse {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) (g : L₂ → L₁) (h₁ : function.left_inverse g ⇑f) (h₂ : function.right_inverse g ⇑f) : morphism R L₂ L₁ := morphism.mk (linear_map.to_fun (linear_map.inverse (morphism.to_linear_map f) g h₁ h₂)) sorry sorry sorry /-- An equivalence of Lie algebras is a morphism which is also a linear equivalence. We could instead define an equivalence to be a morphism which is also a (plain) equivalence. However it is more convenient to define via linear equivalence to get `.to_linear_equiv` for free. -/ structure equiv (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends morphism R L L', linear_equiv R L L' where namespace equiv protected instance has_coe_to_lie_hom {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : has_coe (equiv R L₁ L₂) (morphism R L₁ L₂) := has_coe.mk to_morphism protected instance has_coe_to_linear_equiv {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : has_coe (equiv R L₁ L₂) (linear_equiv R L₁ L₂) := has_coe.mk to_linear_equiv /-- see Note [function coercion] -/ protected instance has_coe_to_fun {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] : has_coe_to_fun (equiv R L₁ L₂) := has_coe_to_fun.mk (fun (x : equiv R L₁ L₂) => L₁ → L₂) to_fun @[simp] theorem coe_to_lie_equiv {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (e : equiv R L₁ L₂) : ⇑↑e = ⇑e := rfl @[simp] theorem coe_to_linear_equiv {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (e : equiv R L₁ L₂) : ⇑↑e = ⇑e := rfl protected instance has_one {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] : HasOne (equiv R L₁ L₁) := { one := mk (linear_equiv.to_fun 1) sorry sorry sorry (linear_equiv.inv_fun 1) sorry sorry } @[simp] theorem one_apply {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] (x : L₁) : coe_fn 1 x = x := rfl protected instance inhabited {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] : Inhabited (equiv R L₁ L₁) := { default := 1 } /-- Lie algebra equivalences are reflexive. -/ def refl {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] : equiv R L₁ L₁ := 1 @[simp] theorem refl_apply {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] (x : L₁) : coe_fn refl x = x := rfl /-- Lie algebra equivalences are symmetric. -/ def symm {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (e : equiv R L₁ L₂) : equiv R L₂ L₁ := mk (morphism.to_fun (morphism.inverse (to_morphism e) (inv_fun e) (left_inv e) (right_inv e))) sorry sorry sorry (linear_equiv.inv_fun (linear_equiv.symm (to_linear_equiv e))) sorry sorry @[simp] theorem symm_symm {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (e : equiv R L₁ L₂) : symm (symm e) = e := sorry @[simp] theorem apply_symm_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (e : equiv R L₁ L₂) (x : L₂) : coe_fn e (coe_fn (symm e) x) = x := linear_equiv.apply_symm_apply (to_linear_equiv e) @[simp] theorem symm_apply_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (e : equiv R L₁ L₂) (x : L₁) : coe_fn (symm e) (coe_fn e x) = x := linear_equiv.symm_apply_apply (to_linear_equiv e) /-- Lie algebra equivalences are transitive. -/ def trans {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] (e₁ : equiv R L₁ L₂) (e₂ : equiv R L₂ L₃) : equiv R L₁ L₃ := mk (morphism.to_fun (morphism.comp (to_morphism e₂) (to_morphism e₁))) sorry sorry sorry (linear_equiv.inv_fun (linear_equiv.trans (to_linear_equiv e₁) (to_linear_equiv e₂))) sorry sorry @[simp] theorem trans_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] (e₁ : equiv R L₁ L₂) (e₂ : equiv R L₂ L₃) (x : L₁) : coe_fn (trans e₁ e₂) x = coe_fn e₂ (coe_fn e₁ x) := rfl @[simp] theorem symm_trans_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] (e₁ : equiv R L₁ L₂) (e₂ : equiv R L₂ L₃) (x : L₃) : coe_fn (symm (trans e₁ e₂)) x = coe_fn (symm e₁) (coe_fn (symm e₂) x) := rfl end equiv end lie_algebra /-- A morphism of Lie algebra modules is a linear map which commutes with the action of the Lie algebra. -/ structure lie_module_hom (R : Type u) (L : Type v) (M : Type w) (N : Type w₁) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] extends linear_map R M N where map_lie : ∀ {x : L} {m : M}, to_fun (has_bracket.bracket x m) = has_bracket.bracket x (to_fun m) namespace lie_module_hom protected instance linear_map.has_coe {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : has_coe (lie_module_hom R L M N) (linear_map R M N) := has_coe.mk to_linear_map /-- see Note [function coercion] -/ protected instance has_coe_to_fun {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : has_coe_to_fun (lie_module_hom R L M N) := has_coe_to_fun.mk (fun (x : lie_module_hom R L M N) => M → N) to_fun @[simp] theorem coe_mk {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (f : M → N) (h₁ : ∀ (x y : M), f (x + y) = f x + f y) (h₂ : ∀ (m : R) (x : M), f (m • x) = m • f x) (h₃ : ∀ {x : L} {m : M}, f (has_bracket.bracket x m) = has_bracket.bracket x (f m)) : ⇑(mk f h₁ h₂ h₃) = f := rfl @[simp] theorem coe_to_linear_map {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (f : lie_module_hom R L M N) : ⇑↑f = ⇑f := rfl @[simp] theorem map_lie' {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (f : lie_module_hom R L M N) (x : L) (m : M) : coe_fn f (has_bracket.bracket x m) = has_bracket.bracket x (coe_fn f m) := map_lie f /-- The constant 0 map is a Lie module morphism. -/ protected instance has_zero {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : HasZero (lie_module_hom R L M N) := { zero := mk (linear_map.to_fun 0) sorry sorry sorry } /-- The identity map is a Lie module morphism. -/ protected instance has_one {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : HasOne (lie_module_hom R L M M) := { one := mk (linear_map.to_fun 1) sorry sorry sorry } protected instance inhabited {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : Inhabited (lie_module_hom R L M N) := { default := 0 } theorem coe_injective {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : function.injective fun (f : lie_module_hom R L M N) => (fun (this : M → N) => this) ⇑f := sorry theorem ext {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] {f : lie_module_hom R L M N} {g : lie_module_hom R L M N} (h : ∀ (m : M), coe_fn f m = coe_fn g m) : f = g := coe_injective (funext h) theorem ext_iff {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] {f : lie_module_hom R L M N} {g : lie_module_hom R L M N} : f = g ↔ ∀ (m : M), coe_fn f m = coe_fn g m := { mp := fun (ᾰ : f = g) (m : M) => Eq._oldrec (Eq.refl (coe_fn f m)) ᾰ, mpr := ext } /-- The composition of Lie module morphisms is a morphism. -/ def comp {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} {P : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] [lie_module R L M] [lie_module R L N] [lie_module R L P] (f : lie_module_hom R L N P) (g : lie_module_hom R L M N) : lie_module_hom R L M P := mk (linear_map.to_fun (linear_map.comp (to_linear_map f) (to_linear_map g))) sorry sorry sorry @[simp] theorem comp_apply {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} {P : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] [lie_module R L M] [lie_module R L N] [lie_module R L P] (f : lie_module_hom R L N P) (g : lie_module_hom R L M N) (m : M) : coe_fn (comp f g) m = coe_fn f (coe_fn g m) := rfl theorem comp_coe {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} {P : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] [lie_module R L M] [lie_module R L N] [lie_module R L P] (f : lie_module_hom R L N P) (g : lie_module_hom R L M N) : ⇑f ∘ ⇑g = ⇑(comp f g) := rfl /-- The inverse of a bijective morphism of Lie modules is a morphism of Lie modules. -/ def inverse {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (f : lie_module_hom R L M N) (g : N → M) (h₁ : function.left_inverse g ⇑f) (h₂ : function.right_inverse g ⇑f) : lie_module_hom R L N M := mk (linear_map.to_fun (linear_map.inverse (to_linear_map f) g h₁ h₂)) sorry sorry sorry end lie_module_hom /-- An equivalence of Lie algebra modules is a linear equivalence which is also a morphism of Lie algebra modules. -/ structure lie_module_equiv (R : Type u) (L : Type v) (M : Type w) (N : Type w₁) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] extends linear_equiv R M N, lie_module_hom R L M N where namespace lie_module_equiv protected instance has_coe_to_lie_module_hom {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : has_coe (lie_module_equiv R L M N) (lie_module_hom R L M N) := has_coe.mk to_lie_module_hom protected instance has_coe_to_linear_equiv {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : has_coe (lie_module_equiv R L M N) (linear_equiv R M N) := has_coe.mk to_linear_equiv /-- see Note [function coercion] -/ protected instance has_coe_to_fun {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] : has_coe_to_fun (lie_module_equiv R L M N) := has_coe_to_fun.mk (fun (x : lie_module_equiv R L M N) => M → N) to_fun @[simp] theorem coe_to_lie_module_hom {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (e : lie_module_equiv R L M N) : ⇑↑e = ⇑e := rfl @[simp] theorem coe_to_linear_equiv {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (e : lie_module_equiv R L M N) : ⇑↑e = ⇑e := rfl protected instance has_one {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : HasOne (lie_module_equiv R L M M) := { one := mk (linear_equiv.to_fun 1) sorry sorry (linear_equiv.inv_fun 1) sorry sorry sorry } @[simp] theorem one_apply {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (m : M) : coe_fn 1 m = m := rfl protected instance inhabited {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : Inhabited (lie_module_equiv R L M M) := { default := 1 } /-- Lie module equivalences are reflexive. -/ def refl {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : lie_module_equiv R L M M := 1 @[simp] theorem refl_apply {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (m : M) : coe_fn refl m = m := rfl /-- Lie module equivalences are syemmtric. -/ def symm {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (e : lie_module_equiv R L M N) : lie_module_equiv R L N M := mk (lie_module_hom.to_fun (lie_module_hom.inverse (to_lie_module_hom e) (inv_fun e) (left_inv e) (right_inv e))) sorry sorry (linear_equiv.inv_fun (linear_equiv.symm ↑e)) sorry sorry sorry @[simp] theorem symm_symm {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (e : lie_module_equiv R L M N) : symm (symm e) = e := sorry @[simp] theorem apply_symm_apply {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (e : lie_module_equiv R L M N) (x : N) : coe_fn e (coe_fn (symm e) x) = x := linear_equiv.apply_symm_apply (to_linear_equiv e) @[simp] theorem symm_apply_apply {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [module R M] [module R N] [lie_ring_module L M] [lie_ring_module L N] [lie_module R L M] [lie_module R L N] (e : lie_module_equiv R L M N) (x : M) : coe_fn (symm e) (coe_fn e x) = x := linear_equiv.symm_apply_apply (to_linear_equiv e) /-- Lie module equivalences are transitive. -/ def trans {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} {P : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] [lie_module R L M] [lie_module R L N] [lie_module R L P] (e₁ : lie_module_equiv R L M N) (e₂ : lie_module_equiv R L N P) : lie_module_equiv R L M P := mk (lie_module_hom.to_fun (lie_module_hom.comp (to_lie_module_hom e₂) (to_lie_module_hom e₁))) sorry sorry (linear_equiv.inv_fun (linear_equiv.trans (to_linear_equiv e₁) (to_linear_equiv e₂))) sorry sorry sorry @[simp] theorem trans_apply {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} {P : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] [lie_module R L M] [lie_module R L N] [lie_module R L P] (e₁ : lie_module_equiv R L M N) (e₂ : lie_module_equiv R L N P) (m : M) : coe_fn (trans e₁ e₂) m = coe_fn e₂ (coe_fn e₁ m) := rfl @[simp] theorem symm_trans_apply {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} {P : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] [lie_module R L M] [lie_module R L N] [lie_module R L P] (e₁ : lie_module_equiv R L M N) (e₂ : lie_module_equiv R L N P) (p : P) : coe_fn (symm (trans e₁ e₂)) p = coe_fn (symm e₁) (coe_fn (symm e₂) p) := rfl end lie_module_equiv namespace ring_commutator /-- The bracket operation for rings is the ring commutator, which captures the extent to which a ring is commutative. It is identically zero exactly when the ring is commutative. -/ protected instance has_bracket {A : Type v} [ring A] : has_bracket A A := has_bracket.mk fun (x y : A) => x * y - y * x theorem commutator {A : Type v} [ring A] (x : A) (y : A) : has_bracket.bracket x y = x * y - y * x := rfl end ring_commutator namespace lie_ring /-- An associative ring gives rise to a Lie ring by taking the bracket to be the ring commutator. -/ protected instance of_associative_ring {A : Type v} [ring A] : lie_ring A := mk sorry sorry sorry sorry theorem of_associative_ring_bracket {A : Type v} [ring A] (x : A) (y : A) : has_bracket.bracket x y = x * y - y * x := rfl end lie_ring /-- A Lie (ring) module is trivial iff all brackets vanish. -/ class lie_module.is_trivial (L : Type v) (M : Type w) [has_bracket L M] [HasZero M] where trivial : ∀ (x : L) (m : M), has_bracket.bracket x m = 0 @[simp] theorem trivial_lie_zero (L : Type v) (M : Type w) [has_bracket L M] [HasZero M] [lie_module.is_trivial L M] (x : L) (m : M) : has_bracket.bracket x m = 0 := lie_module.is_trivial.trivial x m /-- A Lie algebra is Abelian iff it is trivial as a Lie module over itself. -/ def is_lie_abelian (L : Type v) [has_bracket L L] [HasZero L] := lie_module.is_trivial L L theorem commutative_ring_iff_abelian_lie_ring {A : Type v} [ring A] : is_commutative A Mul.mul ↔ is_lie_abelian A := sorry namespace lie_algebra /-- An associative algebra gives rise to a Lie algebra by taking the bracket to be the ring commutator. -/ protected instance of_associative_algebra {A : Type v} [ring A] {R : Type u} [comm_ring R] [algebra R A] : lie_algebra R A := mk sorry /-- The map `of_associative_algebra` associating a Lie algebra to an associative algebra is functorial. -/ def of_associative_algebra_hom {A : Type v} [ring A] {R : Type u} [comm_ring R] [algebra R A] {B : Type w} [ring B] [algebra R B] (f : alg_hom R A B) : morphism R A B := morphism.mk (linear_map.to_fun (alg_hom.to_linear_map f)) sorry sorry sorry @[simp] theorem of_associative_algebra_hom_id {A : Type v} [ring A] {R : Type u} [comm_ring R] [algebra R A] : of_associative_algebra_hom (alg_hom.id R A) = 1 := rfl @[simp] theorem of_associative_algebra_hom_apply {A : Type v} [ring A] {R : Type u} [comm_ring R] [algebra R A] {B : Type w} [ring B] [algebra R B] (f : alg_hom R A B) (x : A) : coe_fn (of_associative_algebra_hom f) x = coe_fn f x := rfl @[simp] theorem of_associative_algebra_hom_comp {A : Type v} [ring A] {R : Type u} [comm_ring R] [algebra R A] {B : Type w} {C : Type w₁} [ring B] [ring C] [algebra R B] [algebra R C] (f : alg_hom R A B) (g : alg_hom R B C) : of_associative_algebra_hom (alg_hom.comp g f) = morphism.comp (of_associative_algebra_hom g) (of_associative_algebra_hom f) := rfl end lie_algebra /-- A Lie module yields a Lie algebra morphism into the linear endomorphisms of the module. -/ def lie_module.to_endo_morphism (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : lie_algebra.morphism R L (module.End R M) := lie_algebra.morphism.mk (fun (x : L) => linear_map.mk (fun (m : M) => has_bracket.bracket x m) (lie_add x) sorry) sorry sorry sorry /-- The adjoint action of a Lie algebra on itself. -/ def lie_algebra.ad (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : lie_algebra.morphism R L (module.End R L) := lie_module.to_endo_morphism R L L @[simp] theorem lie_algebra.ad_apply (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (x : L) (y : L) : coe_fn (coe_fn (lie_algebra.ad R L) x) y = has_bracket.bracket x y := rfl /-- A Lie subalgebra of a Lie algebra is submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie algebra. -/ structure lie_subalgebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] extends submodule R L where lie_mem' : ∀ {x y : L}, x ∈ carrier → y ∈ carrier → has_bracket.bracket x y ∈ carrier /-- The zero algebra is a subalgebra of any Lie algebra. -/ protected instance lie_subalgebra.has_zero (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : HasZero (lie_subalgebra R L) := { zero := lie_subalgebra.mk (submodule.carrier 0) sorry sorry sorry sorry } protected instance lie_subalgebra.inhabited (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : Inhabited (lie_subalgebra R L) := { default := 0 } protected instance set.has_coe (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : has_coe (lie_subalgebra R L) (set L) := has_coe.mk lie_subalgebra.carrier protected instance lie_subalgebra.has_mem (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : has_mem L (lie_subalgebra R L) := has_mem.mk fun (x : L) (L' : lie_subalgebra R L) => x ∈ ↑L' protected instance lie_subalgebra_coe_submodule (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : has_coe (lie_subalgebra R L) (submodule R L) := has_coe.mk lie_subalgebra.to_submodule /-- A Lie subalgebra forms a new Lie ring. -/ protected instance lie_subalgebra_lie_ring (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) : lie_ring ↥L' := lie_ring.mk sorry sorry sorry sorry /-- A Lie subalgebra forms a new Lie algebra. -/ protected instance lie_subalgebra_lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) : lie_algebra R ↥L' := lie_algebra.mk sorry namespace lie_subalgebra @[simp] theorem zero_mem {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) : 0 ∈ L' := submodule.zero_mem ↑L' theorem smul_mem {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) (t : R) {x : L} (h : x ∈ L') : t • x ∈ L' := submodule.smul_mem (↑L') t h theorem add_mem {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) {x : L} {y : L} (hx : x ∈ L') (hy : y ∈ L') : x + y ∈ L' := submodule.add_mem (↑L') hx hy theorem lie_mem {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) {x : L} {y : L} (hx : x ∈ L') (hy : y ∈ L') : has_bracket.bracket x y ∈ L' := lie_mem' L' hx hy @[simp] theorem mem_coe {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) {x : L} : x ∈ ↑L' ↔ x ∈ L' := iff.rfl @[simp] theorem mem_coe' {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) {x : L} : x ∈ ↑L' ↔ x ∈ L' := iff.rfl @[simp] theorem coe_bracket {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) (x : ↥L') (y : ↥L') : ↑(has_bracket.bracket x y) = has_bracket.bracket ↑x ↑y := rfl theorem ext {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L₁' : lie_subalgebra R L) (L₂' : lie_subalgebra R L) (h : ∀ (x : L), x ∈ L₁' ↔ x ∈ L₂') : L₁' = L₂' := sorry theorem ext_iff {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L₁' : lie_subalgebra R L) (L₂' : lie_subalgebra R L) : L₁' = L₂' ↔ ∀ (x : L), x ∈ L₁' ↔ x ∈ L₂' := { mp := fun (h : L₁' = L₂') (x : L) => eq.mpr (id (Eq._oldrec (Eq.refl (x ∈ L₁' ↔ x ∈ L₂')) h)) (iff.refl (x ∈ L₂')), mpr := ext L₁' L₂' } @[simp] theorem mk_coe {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (S : set L) (h₁ : 0 ∈ S) (h₂ : ∀ {a b : L}, a ∈ S → b ∈ S → a + b ∈ S) (h₃ : ∀ (c : R) {x : L}, x ∈ S → c • x ∈ S) (h₄ : ∀ {x y : L}, x ∈ S → y ∈ S → has_bracket.bracket x y ∈ S) : ↑(mk S h₁ h₂ h₃ h₄) = S := rfl theorem coe_injective {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] : function.injective coe := sorry @[simp] theorem coe_set_eq {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L₁' : lie_subalgebra R L) (L₂' : lie_subalgebra R L) : ↑L₁' = ↑L₂' ↔ L₁' = L₂' := function.injective.eq_iff coe_injective theorem to_submodule_injective {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] : function.injective coe := fun (L₁' L₂' : lie_subalgebra R L) (h : ↑L₁' = ↑L₂') => eq.mpr (id (Eq._oldrec (Eq.refl (L₁' = L₂')) (Eq.symm (propext (coe_set_eq L₁' L₂'))))) (eq.mp (Eq._oldrec (Eq.refl (↑L₁' = ↑L₂')) (propext submodule.ext'_iff)) h) @[simp] theorem coe_to_submodule_eq {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L₁' : lie_subalgebra R L) (L₂' : lie_subalgebra R L) : ↑L₁' = ↑L₂' ↔ L₁' = L₂' := function.injective.eq_iff to_submodule_injective end lie_subalgebra /-- A subalgebra of an associative algebra is a Lie subalgebra of the associated Lie algebra. -/ def lie_subalgebra_of_subalgebra (R : Type u) [comm_ring R] (A : Type v) [ring A] [algebra R A] (A' : subalgebra R A) : lie_subalgebra R A := lie_subalgebra.mk (submodule.carrier (subalgebra.to_submodule A')) sorry sorry sorry sorry /-- The embedding of a Lie subalgebra into the ambient space as a Lie morphism. -/ def lie_subalgebra.incl {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) : lie_algebra.morphism R (↥L') L := lie_algebra.morphism.mk (linear_map.to_fun (submodule.subtype (lie_subalgebra.to_submodule L'))) sorry sorry sorry /-- The range of a morphism of Lie algebras is a Lie subalgebra. -/ def lie_algebra.morphism.range {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂] (f : lie_algebra.morphism R L L₂) : lie_subalgebra R L₂ := lie_subalgebra.mk (submodule.carrier (linear_map.range (lie_algebra.morphism.to_linear_map f))) sorry sorry sorry sorry @[simp] theorem lie_algebra.morphism.range_bracket {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂] (f : lie_algebra.morphism R L L₂) (x : ↥(lie_algebra.morphism.range f)) (y : ↥(lie_algebra.morphism.range f)) : ↑(has_bracket.bracket x y) = has_bracket.bracket ↑x ↑y := rfl @[simp] theorem lie_algebra.morphism.range_coe {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂] (f : lie_algebra.morphism R L L₂) : ↑(lie_algebra.morphism.range f) = set.range ⇑f := linear_map.range_coe ↑f @[simp] theorem lie_subalgebra.range_incl {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (L' : lie_subalgebra R L) : lie_algebra.morphism.range (lie_subalgebra.incl L') = L' := sorry /-- The image of a Lie subalgebra under a Lie algebra morphism is a Lie subalgebra of the codomain. -/ def lie_subalgebra.map {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂] (f : lie_algebra.morphism R L L₂) (L' : lie_subalgebra R L) : lie_subalgebra R L₂ := lie_subalgebra.mk (submodule.carrier (submodule.map ↑f ↑L')) sorry sorry sorry sorry @[simp] theorem lie_subalgebra.mem_map_submodule {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂] (e : lie_algebra.equiv R L L₂) (L' : lie_subalgebra R L) (x : L₂) : x ∈ lie_subalgebra.map (↑e) L' ↔ x ∈ submodule.map ↑e ↑L' := iff.rfl namespace lie_algebra namespace equiv /-- An injective Lie algebra morphism is an equivalence onto its range. -/ def of_injective {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) (h : function.injective ⇑f) : equiv R L₁ ↥(morphism.range f) := (fun (h' : linear_map.ker ↑f = ⊥) => mk (linear_equiv.to_fun (linear_equiv.of_injective (↑f) h')) sorry sorry sorry (linear_equiv.inv_fun (linear_equiv.of_injective (↑f) h')) sorry sorry) sorry @[simp] theorem of_injective_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (f : morphism R L₁ L₂) (h : function.injective ⇑f) (x : L₁) : ↑(coe_fn (of_injective f h) x) = coe_fn f x := rfl /-- Lie subalgebras that are equal as sets are equivalent as Lie algebras. -/ def of_eq {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] (L₁' : lie_subalgebra R L₁) (L₁'' : lie_subalgebra R L₁) (h : ↑L₁' = ↑L₁'') : equiv R ↥L₁' ↥L₁'' := mk (linear_equiv.to_fun (linear_equiv.of_eq ↑L₁' ↑L₁'' sorry)) sorry sorry sorry (linear_equiv.inv_fun (linear_equiv.of_eq ↑L₁' ↑L₁'' sorry)) sorry sorry @[simp] theorem of_eq_apply {R : Type u} {L₁ : Type v} [comm_ring R] [lie_ring L₁] [lie_algebra R L₁] (L : lie_subalgebra R L₁) (L' : lie_subalgebra R L₁) (h : ↑L = ↑L') (x : ↥L) : ↑(coe_fn (of_eq L L' h) x) = ↑x := rfl /-- An equivalence of Lie algebras restricts to an equivalence from any Lie subalgebra onto its image. -/ def of_subalgebra {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (L₁'' : lie_subalgebra R L₁) (e : equiv R L₁ L₂) : equiv R ↥L₁'' ↥(lie_subalgebra.map (↑e) L₁'') := mk (linear_equiv.to_fun (linear_equiv.of_submodule ↑e ↑L₁'')) sorry sorry sorry (linear_equiv.inv_fun (linear_equiv.of_submodule ↑e ↑L₁'')) sorry sorry @[simp] theorem of_subalgebra_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (L₁'' : lie_subalgebra R L₁) (e : equiv R L₁ L₂) (x : ↥L₁'') : ↑(coe_fn (of_subalgebra L₁'' e) x) = coe_fn e ↑x := rfl /-- An equivalence of Lie algebras restricts to an equivalence from any Lie subalgebra onto its image. -/ def of_subalgebras {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (L₁' : lie_subalgebra R L₁) (L₂' : lie_subalgebra R L₂) (e : equiv R L₁ L₂) (h : lie_subalgebra.map (↑e) L₁' = L₂') : equiv R ↥L₁' ↥L₂' := mk (linear_equiv.to_fun (linear_equiv.of_submodules ↑e ↑L₁' ↑L₂' sorry)) sorry sorry sorry (linear_equiv.inv_fun (linear_equiv.of_submodules ↑e ↑L₁' ↑L₂' sorry)) sorry sorry @[simp] theorem of_subalgebras_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (L₁' : lie_subalgebra R L₁) (L₂' : lie_subalgebra R L₂) (e : equiv R L₁ L₂) (h : lie_subalgebra.map (↑e) L₁' = L₂') (x : ↥L₁') : ↑(coe_fn (of_subalgebras L₁' L₂' e h) x) = coe_fn e ↑x := rfl @[simp] theorem of_subalgebras_symm_apply {R : Type u} {L₁ : Type v} {L₂ : Type w} [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂] (L₁' : lie_subalgebra R L₁) (L₂' : lie_subalgebra R L₂) (e : equiv R L₁ L₂) (h : lie_subalgebra.map (↑e) L₁' = L₂') (x : ↥L₂') : ↑(coe_fn (symm (of_subalgebras L₁' L₂' e h)) x) = coe_fn (symm e) ↑x := rfl end equiv end lie_algebra /-- A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie module. -/ structure lie_submodule (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] extends submodule R M where lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → has_bracket.bracket x m ∈ carrier namespace lie_submodule /-- The zero module is a Lie submodule of any Lie module. -/ protected instance has_zero {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : HasZero (lie_submodule R L M) := { zero := mk (submodule.carrier 0) sorry sorry sorry sorry } protected instance inhabited {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : Inhabited (lie_submodule R L M) := { default := 0 } protected instance coe_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_coe (lie_submodule R L M) (submodule R M) := has_coe.mk to_submodule theorem coe_to_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : ↑↑N = ↑N := rfl protected instance has_mem {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_mem M (lie_submodule R L M) := has_mem.mk fun (x : M) (N : lie_submodule R L M) => x ∈ ↑N @[simp] theorem mem_carrier {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) {x : M} : x ∈ carrier N ↔ x ∈ ↑N := iff.rfl @[simp] theorem mem_coe_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) {x : M} : x ∈ ↑N ↔ x ∈ N := iff.rfl theorem mem_coe {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) {x : M} : x ∈ ↑N ↔ x ∈ N := iff.rfl @[simp] theorem zero_mem {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : 0 ∈ N := submodule.zero_mem ↑N @[simp] theorem coe_to_set_mk {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (S : set M) (h₁ : 0 ∈ S) (h₂ : ∀ {a b : M}, a ∈ S → b ∈ S → a + b ∈ S) (h₃ : ∀ (c : R) {x : M}, x ∈ S → c • x ∈ S) (h₄ : ∀ {x : L} {m : M}, m ∈ S → has_bracket.bracket x m ∈ S) : ↑(mk S h₁ h₂ h₃ h₄) = S := rfl @[simp] theorem coe_to_submodule_mk {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (p : submodule R M) (h : ∀ {x : L} {m : M}, m ∈ submodule.carrier p → has_bracket.bracket x m ∈ submodule.carrier p) : ↑(mk (submodule.carrier p) (submodule.zero_mem' p) (submodule.add_mem' p) (submodule.smul_mem' p) h) = p := sorry theorem ext {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (h : ∀ (m : M), m ∈ N ↔ m ∈ N') : N = N' := sorry @[simp] theorem coe_to_submodule_eq_iff {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : ↑N = ↑N' ↔ N = N' := sorry protected instance lie_ring_module {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_ring_module L ↥N := lie_ring_module.mk sorry sorry sorry protected instance lie_module {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_module R L ↥N := lie_module.mk sorry sorry end lie_submodule /-- An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself. -/ def lie_ideal (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] := lie_submodule R L L theorem lie_mem_right (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (x : L) (y : L) (h : y ∈ I) : has_bracket.bracket x y ∈ I := lie_submodule.lie_mem I h theorem lie_mem_left (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (x : L) (y : L) (h : x ∈ I) : has_bracket.bracket x y ∈ I := eq.mpr (id (Eq._oldrec (Eq.refl (has_bracket.bracket x y ∈ I)) (Eq.symm (lie_skew x y)))) (eq.mpr (id (Eq._oldrec (Eq.refl (-has_bracket.bracket y x ∈ I)) (Eq.symm (neg_lie y x)))) (lie_mem_right R L I (-y) x h)) /-- An ideal of a Lie algebra is a Lie subalgebra. -/ def lie_ideal_subalgebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : lie_subalgebra R L := lie_subalgebra.mk (submodule.carrier (lie_submodule.to_submodule I)) sorry sorry sorry sorry protected instance lie_subalgebra.has_coe (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : has_coe (lie_ideal R L) (lie_subalgebra R L) := has_coe.mk fun (I : lie_ideal R L) => lie_ideal_subalgebra R L I theorem lie_ideal.coe_to_subalgebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : ↑↑I = ↑I := rfl namespace lie_submodule theorem coe_injective {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : function.injective coe := sorry theorem coe_submodule_injective {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : function.injective coe := sorry protected instance partial_order {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : partial_order (lie_submodule R L M) := partial_order.mk (fun (N N' : lie_submodule R L M) => ∀ {x : M}, x ∈ N → x ∈ N') partial_order.lt sorry sorry sorry theorem le_def {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : N ≤ N' ↔ ↑N ⊆ ↑N' := iff.rfl @[simp] theorem coe_submodule_le_coe_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : ↑N ≤ ↑N' ↔ N ≤ N' := iff.rfl protected instance has_bot {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_bot (lie_submodule R L M) := has_bot.mk 0 @[simp] theorem bot_coe {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : ↑⊥ = singleton 0 := rfl @[simp] theorem bot_coe_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : ↑⊥ = ⊥ := rfl @[simp] theorem mem_bot {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (x : M) : x ∈ ⊥ ↔ x = 0 := set.mem_singleton_iff protected instance has_top {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_top (lie_submodule R L M) := has_top.mk (mk (submodule.carrier ⊤) sorry sorry sorry sorry) @[simp] theorem top_coe {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : ↑⊤ = set.univ := rfl @[simp] theorem top_coe_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : ↑⊤ = ⊤ := rfl theorem mem_top {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (x : M) : x ∈ ⊤ := set.mem_univ x protected instance has_inf {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_inf (lie_submodule R L M) := has_inf.mk fun (N N' : lie_submodule R L M) => mk (submodule.carrier (↑N ⊓ ↑N')) sorry sorry sorry sorry protected instance has_Inf {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_Inf (lie_submodule R L M) := has_Inf.mk fun (S : set (lie_submodule R L M)) => mk (submodule.carrier (Inf (set_of fun (_x : submodule R M) => ∃ (s : lie_submodule R L M), ∃ (H : s ∈ S), ↑s = _x))) sorry sorry sorry sorry @[simp] theorem inf_coe {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : ↑(N ⊓ N') = ↑N ∩ ↑N' := rfl @[simp] theorem Inf_coe_to_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (S : set (lie_submodule R L M)) : ↑(Inf S) = Inf (set_of fun (_x : submodule R M) => ∃ (s : lie_submodule R L M), ∃ (H : s ∈ S), ↑s = _x) := rfl @[simp] theorem Inf_coe {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (S : set (lie_submodule R L M)) : ↑(Inf S) = set.Inter fun (s : lie_submodule R L M) => set.Inter fun (H : s ∈ S) => ↑s := sorry theorem Inf_glb {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (S : set (lie_submodule R L M)) : is_glb S (Inf S) := sorry /-- The set of Lie submodules of a Lie module form a complete lattice. We provide explicit values for the fields `bot`, `top`, `inf` to get more convenient definitions than we would otherwise obtain from `complete_lattice_of_Inf`. -/ protected instance complete_lattice {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : complete_lattice (lie_submodule R L M) := complete_lattice.mk complete_lattice.sup complete_lattice.le complete_lattice.lt sorry sorry sorry sorry sorry sorry has_inf.inf sorry sorry sorry ⊤ sorry ⊥ sorry complete_lattice.Sup complete_lattice.Inf sorry sorry sorry sorry protected instance add_comm_monoid {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : add_comm_monoid (lie_submodule R L M) := add_comm_monoid.mk has_sup.sup sorry ⊥ sorry sorry sorry @[simp] theorem add_eq_sup {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : N + N' = N ⊔ N' := rfl @[simp] theorem sup_coe_to_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : ↑(N ⊔ N') = ↑N ⊔ ↑N' := sorry @[simp] theorem inf_coe_to_submodule {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) : ↑(N ⊓ N') = ↑N ⊓ ↑N' := rfl @[simp] theorem mem_inf {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (x : M) : x ∈ N ⊓ N' ↔ x ∈ N ∧ x ∈ N' := sorry theorem mem_sup {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (x : M) : x ∈ N ⊔ N' ↔ ∃ (y : M), ∃ (H : y ∈ N), ∃ (z : M), ∃ (H : z ∈ N'), y + z = x := sorry theorem eq_bot_iff {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : N = ⊥ ↔ ∀ (m : M), m ∈ N → m = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (N = ⊥ ↔ ∀ (m : M), m ∈ N → m = 0)) (propext eq_bot_iff))) iff.rfl theorem of_bot_eq_bot {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L ↥⊥) : N = ⊥ := sorry theorem unique_of_bot {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L ↥⊥) (N' : lie_submodule R L ↥⊥) : N = N' := eq.mpr (id (Eq._oldrec (Eq.refl (N = N')) (of_bot_eq_bot N))) (eq.mpr (id (Eq._oldrec (Eq.refl (⊥ = N')) (of_bot_eq_bot N'))) (Eq.refl ⊥)) /-- The inclusion of a Lie submodule into its ambient space is a morphism of Lie modules. -/ def incl {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_module_hom R L (↥N) M := lie_module_hom.mk (linear_map.to_fun (submodule.subtype ↑N)) sorry sorry sorry @[simp] theorem incl_apply {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (m : ↥N) : coe_fn (incl N) m = ↑m := rfl theorem incl_eq_val {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : ⇑(incl N) = subtype.val := rfl /-- Given two nested Lie submodules `N ⊆ N'`, the inclusion `N ↪ N'` is a morphism of Lie modules.-/ def hom_of_le {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {N : lie_submodule R L M} {N' : lie_submodule R L M} (h : N ≤ N') : lie_module_hom R L ↥N ↥N' := lie_module_hom.mk (linear_map.to_fun (submodule.of_le h)) sorry sorry sorry @[simp] theorem coe_hom_of_le {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {N : lie_submodule R L M} {N' : lie_submodule R L M} (h : N ≤ N') (m : ↥N) : ↑(coe_fn (hom_of_le h) m) = ↑m := rfl theorem hom_of_le_apply {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {N : lie_submodule R L M} {N' : lie_submodule R L M} (h : N ≤ N') (m : ↥N) : coe_fn (hom_of_le h) m = { val := subtype.val m, property := h (subtype.property m) } := rfl /-- The `lie_span` of a set `s ⊆ M` is the smallest Lie submodule of `M` that contains `s`. -/ def lie_span (R : Type u) (L : Type v) {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (s : set M) : lie_submodule R L M := Inf (set_of fun (N : lie_submodule R L M) => s ⊆ ↑N) theorem mem_lie_span {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {s : set M} {x : M} : x ∈ lie_span R L s ↔ ∀ (N : lie_submodule R L M), s ⊆ ↑N → x ∈ N := sorry theorem subset_lie_span {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {s : set M} : s ⊆ ↑(lie_span R L s) := sorry theorem submodule_span_le_lie_span {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {s : set M} : submodule.span R s ≤ ↑(lie_span R L s) := eq.mpr (id (Eq._oldrec (Eq.refl (submodule.span R s ≤ ↑(lie_span R L s))) (propext submodule.span_le))) (eq.mpr (id (Eq._oldrec (Eq.refl (s ⊆ ↑↑(lie_span R L s))) (coe_to_submodule (lie_span R L s)))) subset_lie_span) theorem lie_span_le {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {s : set M} {N : lie_submodule R L M} : lie_span R L s ≤ N ↔ s ⊆ ↑N := sorry theorem lie_span_mono {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {s : set M} {t : set M} (h : s ⊆ t) : lie_span R L s ≤ lie_span R L t := eq.mpr (id (Eq._oldrec (Eq.refl (lie_span R L s ≤ lie_span R L t)) (propext lie_span_le))) (set.subset.trans h subset_lie_span) theorem lie_span_eq {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_span R L ↑N = N := le_antisymm (iff.mpr lie_span_le (eq.subset rfl)) subset_lie_span theorem well_founded_of_noetherian (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] [is_noetherian R M] : well_founded gt := let f : gt →r gt := rel_hom.mk coe fun (N N' : lie_submodule R L M) (h : N > N') => h; rel_hom.well_founded f (eq.mpr (id (Eq._oldrec (Eq.refl (well_founded gt)) (Eq.symm (propext is_noetherian_iff_well_founded)))) _inst_8) /-- Given a Lie module `M` over a Lie algebra `L`, the set of Lie ideals of `L` acts on the set of submodules of `M`. -/ protected instance has_bracket {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : has_bracket (lie_ideal R L) (lie_submodule R L M) := has_bracket.mk fun (I : lie_ideal R L) (N : lie_submodule R L M) => lie_span R L (set_of fun (m : M) => ∃ (x : ↥I), ∃ (n : ↥N), has_bracket.bracket ↑x ↑n = m) theorem lie_ideal_oper_eq_span {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) : has_bracket.bracket I N = lie_span R L (set_of fun (m : M) => ∃ (x : ↥I), ∃ (n : ↥N), has_bracket.bracket ↑x ↑n = m) := rfl theorem lie_ideal_oper_eq_linear_span {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) : ↑(has_bracket.bracket I N) = submodule.span R (set_of fun (m : M) => ∃ (x : ↥I), ∃ (n : ↥N), has_bracket.bracket ↑x ↑n = m) := sorry theorem lie_mem_lie {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) (x : ↥I) (m : ↥N) : has_bracket.bracket ↑x ↑m ∈ has_bracket.bracket I N := eq.mpr (id (Eq._oldrec (Eq.refl (has_bracket.bracket ↑x ↑m ∈ has_bracket.bracket I N)) (lie_ideal_oper_eq_span N I))) (subset_lie_span (Exists.intro x (Exists.intro m (id (Eq.refl (has_bracket.bracket ↑x ↑m)))))) theorem lie_comm {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (J : lie_ideal R L) : has_bracket.bracket I J = has_bracket.bracket J I := sorry theorem lie_le_right {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) : has_bracket.bracket I N ≤ N := sorry theorem lie_le_left {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (J : lie_ideal R L) : has_bracket.bracket I J ≤ I := eq.mpr (id (Eq._oldrec (Eq.refl (has_bracket.bracket I J ≤ I)) (lie_comm I J))) (lie_le_right I J) theorem lie_le_inf {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (J : lie_ideal R L) : has_bracket.bracket I J ≤ I ⊓ J := eq.mpr (id (Eq._oldrec (Eq.refl (has_bracket.bracket I J ≤ I ⊓ J)) (propext le_inf_iff))) { left := lie_le_left I J, right := lie_le_right J I } @[simp] theorem lie_bot {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (I : lie_ideal R L) : has_bracket.bracket I ⊥ = ⊥ := eq.mpr (id (Eq._oldrec (Eq.refl (has_bracket.bracket I ⊥ = ⊥)) (propext (eq_bot_iff (has_bracket.bracket I ⊥))))) (lie_le_right ⊥ I) @[simp] theorem bot_lie {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : has_bracket.bracket ⊥ N = ⊥ := sorry theorem mono_lie {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (I : lie_ideal R L) (J : lie_ideal R L) (h₁ : I ≤ J) (h₂ : N ≤ N') : has_bracket.bracket I N ≤ has_bracket.bracket J N' := sorry theorem mono_lie_left {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) (J : lie_ideal R L) (h : I ≤ J) : has_bracket.bracket I N ≤ has_bracket.bracket J N := mono_lie N N I J h (le_refl N) theorem mono_lie_right {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (I : lie_ideal R L) (h : N ≤ N') : has_bracket.bracket I N ≤ has_bracket.bracket I N' := mono_lie N N' I I (le_refl I) h @[simp] theorem lie_sup {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (I : lie_ideal R L) : has_bracket.bracket I (N ⊔ N') = has_bracket.bracket I N ⊔ has_bracket.bracket I N' := sorry @[simp] theorem sup_lie {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) (J : lie_ideal R L) : has_bracket.bracket (I ⊔ J) N = has_bracket.bracket I N ⊔ has_bracket.bracket J N := sorry @[simp] theorem lie_inf {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (N' : lie_submodule R L M) (I : lie_ideal R L) : has_bracket.bracket I (N ⊓ N') ≤ has_bracket.bracket I N ⊓ has_bracket.bracket I N' := sorry @[simp] theorem inf_lie {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) (J : lie_ideal R L) : has_bracket.bracket (I ⊓ J) N ≤ has_bracket.bracket I N ⊓ has_bracket.bracket J N := sorry @[simp] theorem trivial_lie_oper_zero {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) (I : lie_ideal R L) [lie_module.is_trivial L M] : has_bracket.bracket I N = ⊥ := sorry /-- The quotient of a Lie module by a Lie submodule. It is a Lie module. -/ def quotient {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) := submodule.quotient (to_submodule N) namespace quotient /-- Map sending an element of `M` to the corresponding element of `M/N`, when `N` is a lie_submodule of the lie_module `N`. -/ def mk {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {N : lie_submodule R L M} : M → quotient N := submodule.quotient.mk theorem is_quotient_mk {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {N : lie_submodule R L M} (m : M) : quotient.mk' m = mk m := rfl /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural linear map from `L` to the endomorphisms of `M` leaving `N` invariant. -/ def lie_submodule_invariant {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] {N : lie_submodule R L M} : linear_map R L ↥(submodule.compatible_maps (to_submodule N) (to_submodule N)) := linear_map.cod_restrict (submodule.compatible_maps (to_submodule N) (to_submodule N)) (↑(lie_module.to_endo_morphism R L M)) (lie_mem N) /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural Lie algebra morphism from `L` to the linear endomorphism of the quotient `M/N`. -/ def action_as_endo_map {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_algebra.morphism R L (module.End R (quotient N)) := lie_algebra.morphism.mk (linear_map.to_fun (linear_map.comp (submodule.mapq_linear ↑N ↑N) lie_submodule_invariant)) sorry sorry sorry /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural bracket action of `L` on the quotient `M/N`. -/ def action_as_endo_map_bracket {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : has_bracket L (quotient N) := has_bracket.mk fun (x : L) (n : quotient N) => coe_fn (coe_fn (action_as_endo_map N) x) n protected instance lie_quotient_lie_ring_module {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_ring_module L (quotient N) := lie_ring_module.mk sorry sorry sorry /-- The quotient of a Lie module by a Lie submodule, is a Lie module. -/ protected instance lie_quotient_lie_module {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (N : lie_submodule R L M) : lie_module R L (quotient N) := lie_module.mk sorry sorry protected instance lie_quotient_has_bracket {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} : has_bracket (quotient I) (quotient I) := has_bracket.mk fun (x y : quotient I) => quotient.lift_on₂' x y (fun (x' y' : L) => mk (has_bracket.bracket x' y')) sorry @[simp] theorem mk_bracket {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} (x : L) (y : L) : mk (has_bracket.bracket x y) = has_bracket.bracket (mk x) (mk y) := rfl protected instance lie_quotient_lie_ring {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} : lie_ring (quotient I) := lie_ring.mk sorry sorry sorry sorry protected instance lie_quotient_lie_algebra {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} : lie_algebra R (quotient I) := lie_algebra.mk sorry end quotient end lie_submodule namespace lie_algebra /-- A generalisation of the derived series of a Lie algebra, whose zeroth term is a specified ideal. It can be more convenient to work with this generalisation when considering the derived series of an ideal since it provides a type-theoretic expression of the fact that the terms of the ideal's derived series are also ideals of the enclosing algebra. See also `lie_ideal.derived_series_eq_derived_series_of_ideal_comap` and `lie_ideal.derived_series_eq_derived_series_of_ideal_map` below. -/ def derived_series_of_ideal (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (k : ℕ) : lie_ideal R L → lie_ideal R L := nat.iterate (fun (I : lie_ideal R L) => has_bracket.bracket I I) k @[simp] theorem derived_series_of_ideal_zero (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : derived_series_of_ideal R L 0 I = I := rfl @[simp] theorem derived_series_of_ideal_succ (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) : derived_series_of_ideal R L (k + 1) I = has_bracket.bracket (derived_series_of_ideal R L k I) (derived_series_of_ideal R L k I) := function.iterate_succ_apply' (fun (I : lie_ideal R L) => has_bracket.bracket I I) k I /-- The derived series of Lie ideals of a Lie algebra. -/ def derived_series (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (k : ℕ) : lie_ideal R L := derived_series_of_ideal R L k ⊤ theorem derived_series_def (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (k : ℕ) : derived_series R L k = derived_series_of_ideal R L k ⊤ := rfl theorem derived_series_of_ideal_add {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) (l : ℕ) : derived_series_of_ideal R L (k + l) I = derived_series_of_ideal R L k (derived_series_of_ideal R L l I) := sorry theorem derived_series_of_ideal_le {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} {J : lie_ideal R L} {k : ℕ} {l : ℕ} (h₁ : I ≤ J) (h₂ : l ≤ k) : derived_series_of_ideal R L k I ≤ derived_series_of_ideal R L l J := sorry theorem derived_series_of_ideal_succ_le {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) : derived_series_of_ideal R L (k + 1) I ≤ derived_series_of_ideal R L k I := derived_series_of_ideal_le (le_refl I) (nat.le_succ k) theorem derived_series_of_ideal_le_self {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) : derived_series_of_ideal R L k I ≤ I := derived_series_of_ideal_le (le_refl I) (zero_le k) theorem derived_series_of_ideal_mono {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} {J : lie_ideal R L} (h : I ≤ J) (k : ℕ) : derived_series_of_ideal R L k I ≤ derived_series_of_ideal R L k J := derived_series_of_ideal_le h (le_refl k) theorem derived_series_of_ideal_antimono {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) {k : ℕ} {l : ℕ} (h : l ≤ k) : derived_series_of_ideal R L k I ≤ derived_series_of_ideal R L l I := derived_series_of_ideal_le (le_refl I) h theorem derived_series_of_ideal_add_le_add {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (J : lie_ideal R L) (k : ℕ) (l : ℕ) : derived_series_of_ideal R L (k + l) (I + J) ≤ derived_series_of_ideal R L k I + derived_series_of_ideal R L l J := sorry end lie_algebra namespace lie_module /-- The lower central series of Lie submodules of a Lie module. -/ def lower_central_series (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (k : ℕ) : lie_submodule R L M := nat.iterate (fun (I : lie_submodule R L M) => has_bracket.bracket ⊤ I) k ⊤ @[simp] theorem lower_central_series_zero (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : lower_central_series R L M 0 = ⊤ := rfl @[simp] theorem lower_central_series_succ (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (k : ℕ) : lower_central_series R L M (k + 1) = has_bracket.bracket ⊤ (lower_central_series R L M k) := function.iterate_succ_apply' (fun (I : lie_submodule R L M) => has_bracket.bracket ⊤ I) k ⊤ theorem trivial_iff_derived_eq_bot (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : is_trivial L M ↔ lower_central_series R L M 1 = ⊥ := sorry theorem derived_series_le_lower_central_series (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (k : ℕ) : lie_algebra.derived_series R L k ≤ lower_central_series R L L k := sorry end lie_module namespace lie_submodule /-- A morphism of Lie modules `f : M → M'` pushes forward Lie submodules of `M` to Lie submodules of `M'`. -/ def map {R : Type u} {L : Type v} {M : Type w} {M' : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M'] (f : lie_module_hom R L M M') (N : lie_submodule R L M) : lie_submodule R L M' := mk (submodule.carrier (submodule.map ↑f ↑N)) sorry sorry sorry sorry /-- A morphism of Lie modules `f : M → M'` pulls back Lie submodules of `M'` to Lie submodules of `M`. -/ def comap {R : Type u} {L : Type v} {M : Type w} {M' : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M'] (f : lie_module_hom R L M M') (N : lie_submodule R L M') : lie_submodule R L M := mk (submodule.carrier (submodule.comap ↑f ↑N)) sorry sorry sorry sorry theorem map_le_iff_le_comap {R : Type u} {L : Type v} {M : Type w} {M' : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M'] {f : lie_module_hom R L M M'} {N : lie_submodule R L M} {N' : lie_submodule R L M'} : map f N ≤ N' ↔ N ≤ comap f N' := set.image_subset_iff theorem gc_map_comap {R : Type u} {L : Type v} {M : Type w} {M' : Type w₁} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M'] (f : lie_module_hom R L M M') : galois_connection (map f) (comap f) := fun (N : lie_submodule R L M) (N' : lie_submodule R L M') => map_le_iff_le_comap end lie_submodule namespace lie_ideal /-- A morphism of Lie algebras `f : L → L'` pushes forward Lie ideals of `L` to Lie ideals of `L'`. Note that unlike `lie_submodule.map`, we must take the `lie_span` of the image. Mathematically this is because although `f` makes `L'` into a Lie module over `L`, in general the `L` submodules of `L'` are not the same as the ideals of `L'`. -/ def map {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') (I : lie_ideal R L) : lie_ideal R L' := lie_submodule.lie_span R L' (⇑f '' ↑I) /-- A morphism of Lie algebras `f : L → L'` pulls back Lie ideals of `L'` to Lie ideals of `L`. Note that `f` makes `L'` into a Lie module over `L` (turning `f` into a morphism of Lie modules) and so this is a special case of `lie_submodule.comap` but we do not exploit this fact. -/ def comap {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') (J : lie_ideal R L') : lie_ideal R L := lie_submodule.mk (submodule.carrier (submodule.comap ↑f ↑J)) sorry sorry sorry sorry @[simp] theorem map_coe_submodule {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') (I : lie_ideal R L) (h : ↑(map f I) = ⇑f '' ↑I) : ↑(map f I) = submodule.map ↑f ↑I := sorry @[simp] theorem comap_coe_submodule {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') (J : lie_ideal R L') : ↑(comap f J) = submodule.comap ↑f ↑J := rfl theorem map_le {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') (I : lie_ideal R L) (J : lie_ideal R L') : map f I ≤ J ↔ ⇑f '' ↑I ⊆ ↑J := lie_submodule.lie_span_le theorem mem_map {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {I : lie_ideal R L} {x : L} (hx : x ∈ I) : coe_fn f x ∈ map f I := lie_submodule.subset_lie_span (Exists.intro x (id { left := hx, right := rfl })) @[simp] theorem mem_comap {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {J : lie_ideal R L'} {x : L} : x ∈ comap f J ↔ coe_fn f x ∈ J := iff.rfl theorem map_le_iff_le_comap {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {I : lie_ideal R L} {J : lie_ideal R L'} : map f I ≤ J ↔ I ≤ comap f J := eq.mpr (id (Eq._oldrec (Eq.refl (map f I ≤ J ↔ I ≤ comap f J)) (propext (map_le f I J)))) set.image_subset_iff theorem gc_map_comap {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} : galois_connection (map f) (comap f) := fun (I : lie_ideal R L) (I' : lie_ideal R L') => map_le_iff_le_comap theorem map_comap_le {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {J : lie_ideal R L'} : map f (comap f J) ≤ J := eq.mpr (id (Eq._oldrec (Eq.refl (map f (comap f J) ≤ J)) (propext map_le_iff_le_comap))) (le_refl (comap f J)) /-- See also `map_comap_eq` below. -/ theorem comap_map_le {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {I : lie_ideal R L} : I ≤ comap f (map f I) := eq.mpr (id (Eq._oldrec (Eq.refl (I ≤ comap f (map f I))) (Eq.symm (propext map_le_iff_le_comap)))) (le_refl (map f I)) theorem map_mono {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} : monotone (map f) := fun (I₁ I₂ : lie_ideal R L) (h : I₁ ≤ I₂) => lie_submodule.lie_span_mono (set.image_subset (⇑f) (eq.mp (Eq._oldrec (Eq.refl (I₁ ≤ I₂)) (propext (lie_submodule.le_def I₁ I₂))) h)) theorem comap_mono {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} : monotone (comap f) := sorry theorem map_of_image {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {I : lie_ideal R L} {J : lie_ideal R L'} (h : ⇑f '' ↑I = ↑J) : map f I = J := sorry /-- Note that this is not a special case of `lie_submodule.of_bot_eq_bot`. Indeed, given `I : lie_ideal R L`, in general the two lattices `lie_ideal R I` and `lie_submodule R L I` are different (though the latter does naturally inject into the former). In other words, in general, ideals of `I`, regarded as a Lie algebra in its own right, are not the same as ideals of `L` contained in `I`. -/ theorem of_bot_eq_bot {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R ↥⊥) : I = ⊥ := sorry theorem unique_of_bot {R : Type u} {L : Type v} {M : Type w} [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] (I : lie_submodule R L ↥⊥) (J : lie_submodule R L ↥⊥) : I = J := eq.mpr (id (Eq._oldrec (Eq.refl (I = J)) (lie_submodule.of_bot_eq_bot I))) (eq.mpr (id (Eq._oldrec (Eq.refl (⊥ = J)) (lie_submodule.of_bot_eq_bot J))) (Eq.refl ⊥)) end lie_ideal namespace lie_algebra.morphism /-- The kernel of a morphism of Lie algebras, as an ideal in the domain. -/ def ker {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') : lie_ideal R L := lie_ideal.comap f ⊥ /-- The range of a morphism of Lie algebras as an ideal in the codomain. -/ def ideal_range {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') : lie_ideal R L' := lie_ideal.map f ⊤ /-- The condition that the image of a morphism of Lie algebras is an ideal. -/ def is_ideal_morphism {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') := ↑(ideal_range f) = range f @[simp] theorem is_ideal_morphism_def {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') : is_ideal_morphism f ↔ ↑(ideal_range f) = range f := iff.rfl theorem range_subset_ideal_range {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') : ↑(range f) ⊆ ↑(ideal_range f) := lie_submodule.subset_lie_span theorem map_le_ideal_range {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') (I : lie_ideal R L) : lie_ideal.map f I ≤ ideal_range f := lie_ideal.map_mono le_top theorem ker_le_comap {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') (J : lie_ideal R L') : ker f ≤ lie_ideal.comap f J := lie_ideal.comap_mono bot_le @[simp] theorem ker_coe_submodule {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') : ↑(ker f) = linear_map.ker ↑f := rfl @[simp] theorem mem_ker {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') {x : L} : x ∈ ker f ↔ coe_fn f x = 0 := sorry theorem mem_ideal_range {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') {x : L} : coe_fn f x ∈ ideal_range f := lie_ideal.mem_map (lie_submodule.mem_top x) @[simp] theorem mem_ideal_range_iff {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') (h : is_ideal_morphism f) {y : L'} : y ∈ ideal_range f ↔ ∃ (x : L), coe_fn f x = y := sorry theorem le_ker_iff {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') (I : lie_ideal R L) : I ≤ ker f ↔ ∀ (x : L), x ∈ I → coe_fn f x = 0 := sorry @[simp] theorem map_bot_iff {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : morphism R L L') (I : lie_ideal R L) : lie_ideal.map f I = ⊥ ↔ I ≤ ker f := sorry end lie_algebra.morphism namespace lie_ideal theorem map_sup_ker_eq_map {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {I : lie_ideal R L} : map f (I ⊔ lie_algebra.morphism.ker f) = map f I := sorry @[simp] theorem map_comap_eq {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {J : lie_ideal R L'} (h : lie_algebra.morphism.is_ideal_morphism f) : map f (comap f J) = lie_algebra.morphism.ideal_range f ⊓ J := sorry @[simp] theorem comap_map_eq {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {I : lie_ideal R L} (h : ↑(map f I) = ⇑f '' ↑I) : comap f (map f I) = I ⊔ lie_algebra.morphism.ker f := sorry /-- Regarding an ideal `I` as a subalgebra, the inclusion map into its ambient space is a morphism of Lie algebras. -/ def incl {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : lie_algebra.morphism R (↥I) L := lie_subalgebra.incl ↑I @[simp] theorem incl_apply {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (x : ↥I) : coe_fn (incl I) x = ↑x := rfl @[simp] theorem incl_coe {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : ↑(incl I) = submodule.subtype ↑I := rfl @[simp] theorem comap_incl_self {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : comap (incl I) I = ⊤ := sorry @[simp] theorem ker_incl {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : lie_algebra.morphism.ker (incl I) = ⊥ := sorry @[simp] theorem incl_ideal_range {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : lie_algebra.morphism.ideal_range (incl I) = I := sorry theorem incl_is_ideal_morphism {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : lie_algebra.morphism.is_ideal_morphism (incl I) := sorry /-- Note that the inequality can be strict; e.g., the inclusion of an Abelian subalgebra of a simple algebra. -/ theorem map_bracket_le {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') {I₁ : lie_ideal R L} {I₂ : lie_ideal R L} : map f (has_bracket.bracket I₁ I₂) ≤ has_bracket.bracket (map f I₁) (map f I₂) := sorry theorem comap_bracket_le {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] (f : lie_algebra.morphism R L L') {J₁ : lie_ideal R L'} {J₂ : lie_ideal R L'} : has_bracket.bracket (comap f J₁) (comap f J₂) ≤ comap f (has_bracket.bracket J₁ J₂) := sorry theorem map_comap_incl {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {I₁ : lie_ideal R L} {I₂ : lie_ideal R L} : map (incl I₁) (comap (incl I₁) I₂) = I₁ ⊓ I₂ := sorry theorem comap_bracket_eq {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {J₁ : lie_ideal R L'} {J₂ : lie_ideal R L'} (h : lie_algebra.morphism.is_ideal_morphism f) : comap f (has_bracket.bracket (lie_algebra.morphism.ideal_range f ⊓ J₁) (lie_algebra.morphism.ideal_range f ⊓ J₂)) = has_bracket.bracket (comap f J₁) (comap f J₂) ⊔ lie_algebra.morphism.ker f := sorry theorem map_comap_bracket_eq {R : Type u} {L : Type v} {L' : Type w₂} [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] {f : lie_algebra.morphism R L L'} {J₁ : lie_ideal R L'} {J₂ : lie_ideal R L'} (h : lie_algebra.morphism.is_ideal_morphism f) : map f (has_bracket.bracket (comap f J₁) (comap f J₂)) = has_bracket.bracket (lie_algebra.morphism.ideal_range f ⊓ J₁) (lie_algebra.morphism.ideal_range f ⊓ J₂) := sorry theorem comap_bracket_incl {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) {I₁ : lie_ideal R L} {I₂ : lie_ideal R L} : has_bracket.bracket (comap (incl I) I₁) (comap (incl I) I₂) = comap (incl I) (has_bracket.bracket (I ⊓ I₁) (I ⊓ I₂)) := sorry /-- This is a very useful result; it allows us to use the fact that inclusion distributes over the Lie bracket operation on ideals, subject to the conditions shown. -/ theorem comap_bracket_incl_of_le {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) {I₁ : lie_ideal R L} {I₂ : lie_ideal R L} (h₁ : I₁ ≤ I) (h₂ : I₂ ≤ I) : has_bracket.bracket (comap (incl I) I₁) (comap (incl I) I₂) = comap (incl I) (has_bracket.bracket I₁ I₂) := sorry theorem derived_series_eq_derived_series_of_ideal_comap {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) : lie_algebra.derived_series R (↥I) k = comap (incl I) (lie_algebra.derived_series_of_ideal R L k I) := sorry theorem derived_series_eq_derived_series_of_ideal_map {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) : map (incl I) (lie_algebra.derived_series R (↥I) k) = lie_algebra.derived_series_of_ideal R L k I := sorry theorem derived_series_eq_bot_iff {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) (k : ℕ) : lie_algebra.derived_series R (↥I) k = ⊥ ↔ lie_algebra.derived_series_of_ideal R L k I = ⊥ := sorry theorem derived_series_add_eq_bot {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] {k : ℕ} {l : ℕ} {I : lie_ideal R L} {J : lie_ideal R L} (hI : lie_algebra.derived_series R (↥I) k = ⊥) (hJ : lie_algebra.derived_series R (↥J) l = ⊥) : lie_algebra.derived_series R (↥(I + J)) (k + l) = ⊥ := sorry end lie_ideal namespace lie_module /-- A Lie module is irreducible if it is zero or its only non-trivial Lie submodule is itself. -/ class is_irreducible (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] where irreducible : ∀ (N : lie_submodule R L M), N ≠ ⊥ → N = ⊤ /-- A Lie module is nilpotent if its lower central series reaches 0 (in a finite number of steps).-/ class is_nilpotent (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] where nilpotent : ∃ (k : ℕ), lower_central_series R L M k = ⊥ protected instance trivial_is_nilpotent (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] [is_trivial L M] : is_nilpotent R L M := is_nilpotent.mk (Exists.intro 1 (id (id (eq.mpr (id (Eq.trans ((fun (a a_1 : lie_submodule R L M) (e_1 : a = a_1) (ᾰ ᾰ_1 : lie_submodule R L M) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (has_bracket.bracket ⊤ ⊤) ⊥ (lie_submodule.trivial_lie_oper_zero ⊤ ⊤) ⊥ ⊥ (Eq.refl ⊥)) (propext (eq_self_iff_true ⊥)))) trivial)))) end lie_module namespace lie_algebra /-- A Lie algebra is simple if it is irreducible as a Lie module over itself via the adjoint action, and it is non-Abelian. -/ class is_simple (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] extends lie_module.is_irreducible R L L where non_abelian : ¬is_lie_abelian L /-- A Lie algebra is solvable if its derived series reaches 0 (in a finite number of steps). -/ class is_solvable (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] where solvable : ∃ (k : ℕ), derived_series R L k = ⊥ protected instance is_solvable_bot (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : is_solvable R ↥⊥ := is_solvable.mk (Exists.intro 0 (lie_ideal.of_bot_eq_bot ⊤)) protected instance is_solvable_add (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] {I : lie_ideal R L} {J : lie_ideal R L} [hI : is_solvable R ↥I] [hJ : is_solvable R ↥J] : is_solvable R ↥(I + J) := is_solvable.dcases_on hI fun (hI : ∃ (k : ℕ), derived_series R (↥I) k = ⊥) => Exists.dcases_on hI fun (k : ℕ) (hk : derived_series R (↥I) k = ⊥) => is_solvable.dcases_on hJ fun (hJ : ∃ (k : ℕ), derived_series R (↥J) k = ⊥) => Exists.dcases_on hJ fun (l : ℕ) (hl : derived_series R (↥J) l = ⊥) => is_solvable.mk (Exists.intro (k + l) (lie_ideal.derived_series_add_eq_bot hk hl)) /-- The (solvable) radical of Lie algebra is the `Sup` of all solvable ideals. -/ def radical (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : lie_ideal R L := Sup (set_of fun (I : lie_ideal R L) => is_solvable R ↥I) /-- The radical of a Noetherian Lie algebra is solvable. -/ protected instance radical_is_solvable (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] [is_noetherian R L] : is_solvable R ↥(radical R L) := eq.mp (Eq._oldrec (Eq.refl (well_founded gt)) (Eq.symm (propext (complete_lattice.is_sup_closed_compact_iff_well_founded (lie_submodule R L L))))) (lie_submodule.well_founded_of_noetherian R L L) (set_of fun (I : lie_ideal R L) => is_solvable R ↥I) (Exists.intro ⊥ (id (lie_algebra.is_solvable_bot R L))) fun (I J : lie_submodule R L L) (hI : I ∈ set_of fun (I : lie_ideal R L) => is_solvable R ↥I) (hJ : J ∈ set_of fun (I : lie_ideal R L) => is_solvable R ↥I) => lie_algebra.is_solvable_add R L protected instance is_solvable_of_is_nilpotent (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] [hL : lie_module.is_nilpotent R L L] : is_solvable R L := Exists.dcases_on lie_module.is_nilpotent.nilpotent fun (k : ℕ) (h : lie_module.lower_central_series R L L k = ⊥) => is_solvable.mk (Exists.intro k (id (eq.mpr (id (Eq._oldrec (Eq.refl (derived_series R L k = ⊥)) (Eq.symm (propext le_bot_iff)))) (le_trans (lie_module.derived_series_le_lower_central_series R L k) (eq.mp (Eq._oldrec (Eq.refl (lie_module.lower_central_series R L L k = ⊥)) (Eq.symm (propext le_bot_iff))) h))))) end lie_algebra namespace linear_equiv /-- A linear equivalence of two modules induces a Lie algebra equivalence of their endomorphisms. -/ def lie_conj {R : Type u} {M₁ : Type v} {M₂ : Type w} [comm_ring R] [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : linear_equiv R M₁ M₂) : lie_algebra.equiv R (module.End R M₁) (module.End R M₂) := lie_algebra.equiv.mk (to_fun (conj e)) sorry sorry sorry (inv_fun (conj e)) sorry sorry @[simp] theorem lie_conj_apply {R : Type u} {M₁ : Type v} {M₂ : Type w} [comm_ring R] [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : linear_equiv R M₁ M₂) (f : module.End R M₁) : coe_fn (lie_conj e) f = coe_fn (conj e) f := rfl @[simp] theorem lie_conj_symm {R : Type u} {M₁ : Type v} {M₂ : Type w} [comm_ring R] [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : linear_equiv R M₁ M₂) : lie_algebra.equiv.symm (lie_conj e) = lie_conj (symm e) := rfl end linear_equiv namespace alg_equiv /-- An equivalence of associative algebras is an equivalence of associated Lie algebras. -/ def to_lie_equiv {R : Type u} {A₁ : Type v} {A₂ : Type w} [comm_ring R] [ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : alg_equiv R A₁ A₂) : lie_algebra.equiv R A₁ A₂ := lie_algebra.equiv.mk (to_fun e) sorry sorry sorry (linear_equiv.inv_fun (to_linear_equiv e)) sorry sorry @[simp] theorem to_lie_equiv_apply {R : Type u} {A₁ : Type v} {A₂ : Type w} [comm_ring R] [ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : alg_equiv R A₁ A₂) (x : A₁) : coe_fn (to_lie_equiv e) x = coe_fn e x := rfl @[simp] theorem to_lie_equiv_symm_apply {R : Type u} {A₁ : Type v} {A₂ : Type w} [comm_ring R] [ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : alg_equiv R A₁ A₂) (x : A₂) : coe_fn (lie_algebra.equiv.symm (to_lie_equiv e)) x = coe_fn (symm e) x := rfl end alg_equiv /-! ### Matrices An important class of Lie algebras are those arising from the associative algebra structure on square matrices over a commutative ring. -/ /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the Lie algebra structures. -/ def lie_equiv_matrix' {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] : lie_algebra.equiv R (module.End R (n → R)) (matrix n n R) := lie_algebra.equiv.mk (linear_equiv.to_fun linear_map.to_matrix') sorry sorry sorry (linear_equiv.inv_fun linear_map.to_matrix') sorry sorry @[simp] theorem lie_equiv_matrix'_apply {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (f : module.End R (n → R)) : coe_fn lie_equiv_matrix' f = coe_fn linear_map.to_matrix' f := rfl @[simp] theorem lie_equiv_matrix'_symm_apply {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (A : matrix n n R) : coe_fn (lie_algebra.equiv.symm lie_equiv_matrix') A = coe_fn matrix.to_lin' A := rfl /-- An invertible matrix induces a Lie algebra equivalence from the space of matrices to itself. -/ def matrix.lie_conj {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (P : matrix n n R) (h : is_unit P) : lie_algebra.equiv R (matrix n n R) (matrix n n R) := lie_algebra.equiv.trans (lie_algebra.equiv.trans (lie_algebra.equiv.symm lie_equiv_matrix') (linear_equiv.lie_conj (matrix.to_linear_equiv P h))) lie_equiv_matrix' @[simp] theorem matrix.lie_conj_apply {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (P : matrix n n R) (A : matrix n n R) (h : is_unit P) : coe_fn (matrix.lie_conj P h) A = matrix.mul (matrix.mul P A) (P⁻¹) := sorry @[simp] theorem matrix.lie_conj_symm_apply {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (P : matrix n n R) (A : matrix n n R) (h : is_unit P) : coe_fn (lie_algebra.equiv.symm (matrix.lie_conj P h)) A = matrix.mul (matrix.mul (P⁻¹) A) P := sorry /-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence of Lie algebras. -/ def matrix.reindex_lie_equiv {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] {m : Type w₁} [DecidableEq m] [fintype m] (e : n ≃ m) : lie_algebra.equiv R (matrix n n R) (matrix m m R) := lie_algebra.equiv.mk (linear_equiv.to_fun (matrix.reindex_linear_equiv e e)) sorry sorry sorry (linear_equiv.inv_fun (matrix.reindex_linear_equiv e e)) sorry sorry @[simp] theorem matrix.reindex_lie_equiv_apply {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] {m : Type w₁} [DecidableEq m] [fintype m] (e : n ≃ m) (M : matrix n n R) : coe_fn (matrix.reindex_lie_equiv e) M = fun (i j : m) => M (coe_fn (equiv.symm e) i) (coe_fn (equiv.symm e) j) := rfl @[simp] theorem matrix.reindex_lie_equiv_symm_apply {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] {m : Type w₁} [DecidableEq m] [fintype m] (e : n ≃ m) (M : matrix m m R) : coe_fn (lie_algebra.equiv.symm (matrix.reindex_lie_equiv e)) M = fun (i j : n) => M (coe_fn e i) (coe_fn e j) := rfl
57712cf42695a57436a8cc0e9a0075d1c4340ab2
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/ordered_monoid_lemmas.lean
7cf18ca39c22bdc1f417a2fd7da3c51b38d74406
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,761
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl, Damiano Testa -/ import algebra.covariant_and_contravariant import order.basic /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. ## Remark Almost no monoid is actually present in this file: most assumptions have been generalized to `has_mul` or `mul_one_class`. -/ -- TODO: If possible, uniformize lemma names, taking special care of `'`, -- after the `ordered`-refactor is done. open function variables {α : Type*} section has_mul variables [has_mul α] section has_le variables [has_le α] /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive add_le_add_left] lemma mul_le_mul_left' [covariant_class α α (*) (≤)] {b c : α} (bc : b ≤ c) (a : α) : a * b ≤ a * c := covariant_class.elim _ bc @[to_additive le_of_add_le_add_left] lemma le_of_mul_le_mul_left' [contravariant_class α α (*) (≤)] {a b c : α} (bc : a * b ≤ a * c) : b ≤ c := contravariant_class.elim _ bc /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive add_le_add_right] lemma mul_le_mul_right' [covariant_class α α (function.swap (*)) (≤)] {b c : α} (bc : b ≤ c) (a : α) : b * a ≤ c * a := covariant_class.elim a bc @[to_additive le_of_add_le_add_right] lemma le_of_mul_le_mul_right' [contravariant_class α α (function.swap (*)) (≤)] {a b c : α} (bc : b * a ≤ c * a) : b ≤ c := contravariant_class.elim a bc @[simp, to_additive] lemma mul_le_mul_iff_left [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b c : α} : a * b ≤ a * c ↔ b ≤ c := rel_iff_cov α α (*) (≤) a @[simp, to_additive] lemma mul_le_mul_iff_right [covariant_class α α (function.swap (*)) (≤)] [contravariant_class α α (function.swap (*)) (≤)] (a : α) {b c : α} : b * a ≤ c * a ↔ b ≤ c := rel_iff_cov α α (function.swap (*)) (≤) a end has_le section has_lt variables [has_lt α] @[simp, to_additive] lemma mul_lt_mul_iff_left [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] (a : α) {b c : α} : a * b < a * c ↔ b < c := rel_iff_cov α α (*) (<) a @[simp, to_additive] lemma mul_lt_mul_iff_right [covariant_class α α (function.swap (*)) (<)] [contravariant_class α α (function.swap (*)) (<)] (a : α) {b c : α} : b * a < c * a ↔ b < c := rel_iff_cov α α (function.swap (*)) (<) a @[to_additive add_lt_add_left] lemma mul_lt_mul_left' [covariant_class α α (*) (<)] {b c : α} (bc : b < c) (a : α) : a * b < a * c := covariant_class.elim _ bc @[to_additive lt_of_add_lt_add_left] lemma lt_of_mul_lt_mul_left' [contravariant_class α α (*) (<)] {a b c : α} (bc : a * b < a * c) : b < c := contravariant_class.elim _ bc @[to_additive add_lt_add_right] lemma mul_lt_mul_right' [covariant_class α α (function.swap (*)) (<)] {b c : α} (bc : b < c) (a : α) : b * a < c * a := covariant_class.elim a bc @[to_additive lt_of_add_lt_add_right] lemma lt_of_mul_lt_mul_right' [contravariant_class α α (function.swap (*)) (<)] {a b c : α} (bc : b * a < c * a) : b < c := contravariant_class.elim a bc end has_lt end has_mul -- using one section mul_one_class variables [mul_one_class α] section has_le variables [has_le α] @[simp, to_additive le_add_iff_nonneg_right] lemma le_mul_iff_one_le_right' [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[simp, to_additive add_le_iff_nonpos_right] lemma mul_le_iff_le_one_right' [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b : α} : a * b ≤ a ↔ b ≤ 1 := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[simp, to_additive le_add_iff_nonneg_left] lemma le_mul_iff_one_le_left' [covariant_class α α (function.swap (*)) (≤)] [contravariant_class α α (function.swap (*)) (≤)] (a : α) {b : α} : a ≤ b * a ↔ 1 ≤ b := iff.trans (by rw one_mul) (mul_le_mul_iff_right a) @[simp, to_additive add_le_iff_nonpos_left] lemma mul_le_iff_le_one_left' [covariant_class α α (function.swap (*)) (≤)] [contravariant_class α α (function.swap (*)) (≤)] {a b : α} : a * b ≤ b ↔ a ≤ 1 := iff.trans (by rw one_mul) (mul_le_mul_iff_right b) end has_le section has_lt variable [has_lt α] @[to_additive lt_add_of_pos_right] lemma lt_mul_of_one_lt_right' [covariant_class α α (*) (<)] (a : α) {b : α} (h : 1 < b) : a < a * b := calc a = a * 1 : (mul_one _).symm ... < a * b : mul_lt_mul_left' h a @[simp, to_additive lt_add_iff_pos_right] lemma lt_mul_iff_one_lt_right' [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] (a : α) {b : α} : a < a * b ↔ 1 < b := iff.trans (by rw mul_one) (mul_lt_mul_iff_left a) @[simp, to_additive add_lt_iff_neg_left] lemma mul_lt_iff_lt_one_left' [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] {a b : α} : a * b < a ↔ b < 1 := iff.trans (by rw mul_one) (mul_lt_mul_iff_left a) @[simp, to_additive lt_add_iff_pos_left] lemma lt_mul_iff_one_lt_left' [covariant_class α α (function.swap (*)) (<)] [contravariant_class α α (function.swap (*)) (<)] (a : α) {b : α} : a < b * a ↔ 1 < b := iff.trans (by rw one_mul) (mul_lt_mul_iff_right a) @[simp, to_additive add_lt_iff_neg_right] lemma mul_lt_iff_lt_one_right' [covariant_class α α (function.swap (*)) (<)] [contravariant_class α α (function.swap (*)) (<)] {a : α} (b : α) : a * b < b ↔ a < 1 := iff.trans (by rw one_mul) (mul_lt_mul_iff_right b) end has_lt section preorder variable [preorder α] @[to_additive] lemma mul_le_of_le_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b ... = b : mul_one b ... ≤ c : hbc alias mul_le_of_le_of_le_one ← mul_le_one' attribute [to_additive add_nonpos] mul_le_one' @[to_additive] lemma lt_mul_of_lt_of_one_le [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : 1 ≤ a) : b < c * a := calc b < c : hbc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c @[to_additive] lemma mul_lt_of_lt_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : a ≤ 1) : b * a < c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b ... = b : mul_one b ... < c : hbc @[to_additive] lemma lt_mul_of_le_of_one_lt [covariant_class α α (*) (<)] {a b c : α} (hbc : b ≤ c) (ha : 1 < a) : b < c * a := calc b ≤ c : hbc ... = c * 1 : (mul_one c).symm ... < c * a : mul_lt_mul_left' ha c @[to_additive] lemma mul_lt_of_le_one_of_lt [covariant_class α α (function.swap (*)) (≤)] {a b c : α} (ha : a ≤ 1) (hb : b < c) : a * b < c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b ... = b : one_mul b ... < c : hb @[to_additive] lemma mul_le_of_le_one_of_le [covariant_class α α (function.swap (*)) (≤)] {a b c : α} (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b ... = b : one_mul b ... ≤ c : hbc @[to_additive] lemma le_mul_of_one_le_of_le [covariant_class α α (function.swap (*)) (≤)] {a b c: α} (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := calc b ≤ c : hbc ... = 1 * c : (one_mul c).symm ... ≤ a * c : mul_le_mul_right' ha c /-- Assume monotonicity on the `left`. The lemma assuming `right` is `right.mul_lt_one`. -/ @[to_additive] lemma left.mul_lt_one [covariant_class α α (*) (<)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := calc a * b < a * 1 : mul_lt_mul_left' hb a ... = a : mul_one a ... < 1 : ha /-- Assume monotonicity on the `right`. The lemma assuming `left` is `left.mul_lt_one`. -/ @[to_additive] lemma right.mul_lt_one [covariant_class α α (function.swap (*)) (<)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... < 1 : hb @[to_additive] lemma mul_lt_of_le_of_lt_one [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] {a b c: α} (hbc : b ≤ c) (ha : a < 1) : b * a < c := calc b * a ≤ c * a : mul_le_mul_right' hbc a ... < c * 1 : mul_lt_mul_left' ha c ... = c : mul_one c @[to_additive] lemma mul_lt_of_lt_one_of_le [covariant_class α α (function.swap (*)) (<)] {a b c : α} (ha : a < 1) (hbc : b ≤ c) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... ≤ c : hbc @[to_additive] lemma lt_mul_of_one_lt_of_le [covariant_class α α (function.swap (*)) (<)] {a b c : α} (ha : 1 < a) (hbc : b ≤ c) : b < a * c := calc b ≤ c : hbc ... = 1 * c : (one_mul c).symm ... < a * c : mul_lt_mul_right' ha c /-- Assumes left covariance. -/ @[to_additive] lemma le_mul_of_le_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (ha : c ≤ a) (hb : 1 ≤ b) : c ≤ a * b := calc c ≤ a : ha ... = a * 1 : (mul_one a).symm ... ≤ a * b : mul_le_mul_left' hb a /- This lemma is present to mimick the name of an existing one. -/ @[to_additive add_nonneg] lemma one_le_mul [covariant_class α α (*) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_le_of_le_one ha hb /-- Assumes left covariance. -/ @[to_additive] lemma lt_mul_of_lt_of_one_lt [covariant_class α α (*) (<)] {a b c : α} (ha : c < a) (hb : 1 < b) : c < a * b := calc c < a : ha ... = a * 1 : (mul_one _).symm ... < a * b : mul_lt_mul_left' hb a /-- Assumes left covariance. -/ @[to_additive] lemma left.mul_lt_one_of_lt_of_lt_one [covariant_class α α (*) (<)] {a b c : α} (ha : a < c) (hb : b < 1) : a * b < c := calc a * b < a * 1 : mul_lt_mul_left' hb a ... = a : mul_one a ... < c : ha /-- Assumes right covariance. -/ @[to_additive] lemma right.mul_lt_one_of_lt_of_lt_one [covariant_class α α (function.swap (*)) (<)] {a b c : α} (ha : a < 1) (hb : b < c) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... < c : hb /-- Assumes right covariance. -/ @[to_additive right.add_nonneg] lemma right.one_le_mul [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := calc 1 ≤ b : hb ... = 1 * b : (one_mul b).symm ... ≤ a * b : mul_le_mul_right' ha b /-- Assumes right covariance. -/ @[to_additive right.add_pos] lemma right.one_lt_mul [covariant_class α α (function.swap (*)) (<)] {b : α} (hb : 1 < b) {a: α} (ha : 1 < a) : 1 < a * b := calc 1 < b : hb ... = 1 * b : (one_mul _).symm ... < a * b : mul_lt_mul_right' ha b end preorder @[to_additive le_add_of_nonneg_right] lemma le_mul_of_one_le_right' [has_le α] [covariant_class α α (*) (≤)] {a b : α} (h : 1 ≤ b) : a ≤ a * b := calc a = a * 1 : (mul_one _).symm ... ≤ a * b : mul_le_mul_left' h a @[to_additive add_le_of_nonpos_right] lemma mul_le_of_le_one_right' [has_le α] [covariant_class α α (*) (≤)] {a b : α} (h : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : mul_le_mul_left' h a ... = a : mul_one a end mul_one_class @[to_additive] lemma mul_left_cancel'' [semigroup α] [partial_order α] [contravariant_class α α (*) (≤)] {a b c : α} (h : a * b = a * c) : b = c := (le_of_mul_le_mul_left' h.le).antisymm (le_of_mul_le_mul_left' h.ge) @[to_additive] lemma mul_right_cancel'' [semigroup α] [partial_order α] [contravariant_class α α (function.swap (*)) (≤)] {a b c : α} (h : a * b = c * b) : a = c := le_antisymm (le_of_mul_le_mul_right' h.le) (le_of_mul_le_mul_right' h.ge) /- This is not instance, since we want to have an instance from `left_cancel_semigroup`s to the appropriate `covariant_class`. -/ /-- A semigroup with a partial order and satisfying `left_cancel_semigroup` (i.e. `a * c < b * c → a < b`) is a `left_cancel semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `left_cancel_add_semigroup` (i.e. `c + a < c + b → a < b`) is a `left_cancel add_semigroup`."] def contravariant.to_left_cancel_semigroup [semigroup α] [partial_order α] [contravariant_class α α (*) (≤)] : left_cancel_semigroup α := { mul_left_cancel := λ a b c, mul_left_cancel'' ..‹semigroup α› } /- This is not instance, since we want to have an instance from `right_cancel_semigroup`s to the appropriate `covariant_class`. -/ /-- A semigroup with a partial order and satisfying `right_cancel_semigroup` (i.e. `a * c < b * c → a < b`) is a `right_cancel semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `right_cancel_add_semigroup` (`a + c < b + c → a < b`) is a `right_cancel add_semigroup`."] def contravariant.to_right_cancel_semigroup [semigroup α] [partial_order α] [contravariant_class α α (function.swap (*)) (≤)] : right_cancel_semigroup α := { mul_right_cancel := λ a b c, mul_right_cancel'' ..‹semigroup α› } variables {a b c d : α} section left variables [preorder α] section has_mul variables [has_mul α] @[to_additive] lemma mul_lt_mul_of_lt_of_lt [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (<)] (h₁ : a < b) (h₂ : c < d) : a * c < b * d := calc a * c < a * d : mul_lt_mul_left' h₂ a ... < b * d : mul_lt_mul_right' h₁ d section contravariant_mul_lt_left_le_right variables [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma mul_lt_mul_of_le_of_lt (h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d := (mul_le_mul_right' h₁ _).trans_lt (mul_lt_mul_left' h₂ b) @[to_additive add_lt_add] lemma mul_lt_mul''' (h₁ : a < b) (h₂ : c < d) : a * c < b * d := mul_lt_mul_of_le_of_lt h₁.le h₂ end contravariant_mul_lt_left_le_right variable [covariant_class α α (*) (≤)] @[to_additive] lemma mul_lt_of_mul_lt_left (h : a * b < c) (hle : d ≤ b) : a * d < c := (mul_le_mul_left' hle a).trans_lt h @[to_additive] lemma mul_le_of_mul_le_left (h : a * b ≤ c) (hle : d ≤ b) : a * d ≤ c := @act_rel_of_rel_of_act_rel _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ a _ _ _ hle h @[to_additive] lemma lt_mul_of_lt_mul_left (h : a < b * c) (hle : c ≤ d) : a < b * d := h.trans_le (mul_le_mul_left' hle b) @[to_additive] lemma le_mul_of_le_mul_left (h : a ≤ b * c) (hle : c ≤ d) : a ≤ b * d := @rel_act_of_rel_of_rel_act _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ b _ _ _ hle h @[to_additive] lemma mul_lt_mul_of_lt_of_le [covariant_class α α (function.swap (*)) (<)] (h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d := (mul_le_mul_left' h₂ _).trans_lt (mul_lt_mul_right' h₁ d) end has_mul /-! Here we start using properties of one, on the left. -/ section mul_one_class variables [mul_one_class α] [covariant_class α α (*) (≤)] @[to_additive] lemma lt_of_mul_lt_of_one_le_left (h : a * b < c) (hle : 1 ≤ b) : a < c := (le_mul_of_one_le_right' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_left (h : a * b ≤ c) (hle : 1 ≤ b) : a ≤ c := (le_mul_of_one_le_right' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_left (h : a < b * c) (hle : c ≤ 1) : a < b := h.trans_le (mul_le_of_le_one_right' hle) @[to_additive] lemma le_of_le_mul_of_le_one_left (h : a ≤ b * c) (hle : c ≤ 1) : a ≤ b := h.trans (mul_le_of_le_one_right' hle) @[to_additive] theorem mul_lt_of_lt_of_lt_one (bc : b < c) (a1 : a < 1) : b * a < c := calc b * a ≤ b * 1 : mul_le_mul_left' a1.le _ ... = b : mul_one b ... < c : bc end mul_one_class end left section right section preorder variables [preorder α] section has_mul variables [has_mul α] variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma mul_lt_of_mul_lt_right (h : a * b < c) (hle : d ≤ a) : d * b < c := (mul_le_mul_right' hle b).trans_lt h @[to_additive] lemma mul_le_of_mul_le_right (h : a * b ≤ c) (hle : d ≤ a) : d * b ≤ c := (mul_le_mul_right' hle b).trans h @[to_additive] lemma lt_mul_of_lt_mul_right (h : a < b * c) (hle : b ≤ d) : a < d * c := h.trans_le (mul_le_mul_right' hle c) @[to_additive] lemma le_mul_of_le_mul_right (h : a ≤ b * c) (hle : b ≤ d) : a ≤ d * c := h.trans (mul_le_mul_right' hle c) variable [covariant_class α α (*) (≤)] @[to_additive add_le_add] lemma mul_le_mul' (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := (mul_le_mul_left' h₂ _).trans (mul_le_mul_right' h₁ d) @[to_additive] lemma mul_le_mul_three {e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a * b * c ≤ d * e * f := mul_le_mul' (mul_le_mul' h₁ h₂) h₃ end has_mul /-! Here we start using properties of one, on the right. -/ section mul_one_class variables [mul_one_class α] section le_right variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive le_add_of_nonneg_left] lemma le_mul_of_one_le_left' (h : 1 ≤ b) : a ≤ b * a := calc a = 1 * a : (one_mul a).symm ... ≤ b * a : mul_le_mul_right' h a @[to_additive add_le_of_nonpos_left] lemma mul_le_of_le_one_left' (h : b ≤ 1) : b * a ≤ a := calc b * a ≤ 1 * a : mul_le_mul_right' h a ... = a : one_mul a @[to_additive] lemma lt_of_mul_lt_of_one_le_right (h : a * b < c) (hle : 1 ≤ a) : b < c := (le_mul_of_one_le_left' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_right (h : a * b ≤ c) (hle : 1 ≤ a) : b ≤ c := (le_mul_of_one_le_left' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_right (h : a < b * c) (hle : b ≤ 1) : a < c := h.trans_le (mul_le_of_le_one_left' hle) @[to_additive] lemma le_of_le_mul_of_le_one_right (h : a ≤ b * c) (hle : b ≤ 1) : a ≤ c := h.trans (mul_le_of_le_one_left' hle) theorem mul_lt_of_lt_one_of_lt (a1 : a < 1) (bc : b < c) : a * b < c := calc a * b ≤ 1 * b : mul_le_mul_right' a1.le _ ... = b : one_mul b ... < c : bc end le_right section lt_right @[to_additive lt_add_of_pos_left] lemma lt_mul_of_one_lt_left' [covariant_class α α (function.swap (*)) (<)] (a : α) {b : α} (h : 1 < b) : a < b * a := calc a = 1 * a : (one_mul _).symm ... < b * a : mul_lt_mul_right' h a end lt_right end mul_one_class end preorder end right section preorder variables [preorder α] section mul_one_class variables [mul_one_class α] section covariant_left variable [covariant_class α α (*) (≤)] @[to_additive add_pos_of_pos_of_nonneg] lemma one_lt_mul_of_lt_of_le' (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_of_lt_of_le ha $ le_mul_of_one_le_right' hb @[to_additive add_pos] lemma one_lt_mul' (ha : 1 < a) (hb : 1 < b) : 1 < a * b := one_lt_mul_of_lt_of_le' ha hb.le @[to_additive] lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 ≤ a) : b < c * a := hbc.trans_le $ le_mul_of_one_le_right' ha @[to_additive] lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le' hbc ha.le @[to_additive] lemma le_mul_of_le_of_one_le (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := calc b ≤ c : hbc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c @[to_additive add_nonneg] lemma one_le_mul_right (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := calc 1 ≤ a : ha ... = a * 1 : (mul_one a).symm ... ≤ a * b : mul_le_mul_left' hb a end covariant_left section covariant_right variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive add_pos_of_nonneg_of_pos] lemma one_lt_mul_of_le_of_lt' (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_of_lt_of_le hb $ le_mul_of_one_le_left' ha @[to_additive] lemma lt_mul_of_one_le_of_lt (ha : 1 ≤ a) (hbc : b < c) : b < a * c := hbc.trans_le $ le_mul_of_one_le_left' ha @[to_additive] lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt ha.le hbc end covariant_right end mul_one_class end preorder section partial_order /-! Properties assuming `partial_order`. -/ variables [mul_one_class α] [partial_order α] [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma mul_eq_one_iff' (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have a ≤ 1, from hab ▸ le_mul_of_le_of_one_le le_rfl hb, have a = 1, from le_antisymm this ha, have b ≤ 1, from hab ▸ le_mul_of_one_le_of_le ha le_rfl, have b = 1, from le_antisymm this hb, and.intro ‹a = 1› ‹b = 1›) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) end partial_order section mono variables [has_mul α] {β : Type*} {f g : β → α} section has_le variables [preorder α] [preorder β] @[to_additive monotone.const_add] lemma monotone.const_mul' [covariant_class α α (*) (≤)] (hf : monotone f) (a : α) : monotone (λ x, a * f x) := λ x y h, mul_le_mul_left' (hf h) a @[to_additive monotone.add_const] lemma monotone.mul_const' [covariant_class α α (function.swap (*)) (≤)] (hf : monotone f) (a : α) : monotone (λ x, f x * a) := λ x y h, mul_le_mul_right' (hf h) a end has_le variables [preorder α] [preorder β] /-- The product of two monotone functions is monotone. -/ @[to_additive monotone.add "The sum of two monotone functions is monotone."] lemma monotone.mul' [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] (hf : monotone f) (hg : monotone g) : monotone (λ x, f x * g x) := λ x y h, mul_le_mul' (hf h) (hg h) end mono section strict_mono variables [has_mul α] {β : Type*} {f g : β → α} section left variables [has_lt α] [covariant_class α α (*) (<)] [has_lt β] @[to_additive strict_mono.const_add] lemma strict_mono.const_mul' (hf : strict_mono f) (c : α) : strict_mono (λ x, c * f x) := λ a b ab, mul_lt_mul_left' (hf ab) c end left section right variables [has_lt α] [covariant_class α α (function.swap (*)) (<)] [has_lt β] @[to_additive strict_mono.add_const] lemma strict_mono.mul_const' (hf : strict_mono f) (c : α) : strict_mono (λ x, f x * c) := λ a b ab, mul_lt_mul_right' (hf ab) c end right /-- The product of two strictly monotone functions is strictly monotone. -/ @[to_additive strict_mono.add "The sum of two strictly monotone functions is strictly monotone."] lemma strict_mono.mul' [has_lt β] [preorder α] [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (<)] (hf : strict_mono f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ a b ab, mul_lt_mul_of_lt_of_lt (hf ab) (hg ab) variables [preorder α] /-- The product of a monotone function and a strictly monotone function is strictly monotone. -/ @[to_additive monotone.add_strict_mono "The sum of a monotone function and a strictly monotone function is strictly monotone."] lemma monotone.mul_strict_mono' [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] {β : Type*} [preorder β] {f g : β → α} (hf : monotone f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_le_of_lt (hf h.le) (hg h) variables [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (<)] [preorder β] /-- The product of a strictly monotone function and a monotone function is strictly monotone. -/ @[to_additive strict_mono.add_monotone "The sum of a strictly monotone function and a monotone function is strictly monotone."] lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg h.le) end strict_mono /-- An element `a : α` is `mul_le_cancellable` if `x ↦ a * x` is order-reflecting. We will make a separate version of many lemmas that require `[contravariant_class α α (*) (≤)]` with `mul_le_cancellable` assumptions instead. These lemmas can then be instantiated to specific types, like `ennreal`, where we can replace the assumption `add_le_cancellable x` by `x ≠ ∞`. -/ @[to_additive /-" An element `a : α` is `add_le_cancellable` if `x ↦ a + x` is order-reflecting. We will make a separate version of many lemmas that require `[contravariant_class α α (+) (≤)]` with `mul_le_cancellable` assumptions instead. These lemmas can then be instantiated to specific types, like `ennreal`, where we can replace the assumption `add_le_cancellable x` by `x ≠ ∞`. "-/ ] def mul_le_cancellable [has_mul α] [has_le α] (a : α) : Prop := ∀ ⦃b c⦄, a * b ≤ a * c → b ≤ c @[to_additive] lemma contravariant.mul_le_cancellable [has_mul α] [has_le α] [contravariant_class α α (*) (≤)] {a : α} : mul_le_cancellable a := λ b c, le_of_mul_le_mul_left' namespace mul_le_cancellable @[to_additive] protected lemma injective [has_mul α] [partial_order α] {a : α} (ha : mul_le_cancellable a) : injective ((*) a) := λ b c h, le_antisymm (ha h.le) (ha h.ge) @[to_additive] protected lemma inj [has_mul α] [partial_order α] {a b c : α} (ha : mul_le_cancellable a) : a * b = a * c ↔ b = c := ha.injective.eq_iff @[to_additive] protected lemma injective_left [comm_semigroup α] [partial_order α] {a : α} (ha : mul_le_cancellable a) : injective (* a) := λ b c h, ha.injective $ by rwa [mul_comm a, mul_comm a] @[to_additive] protected lemma inj_left [comm_semigroup α] [partial_order α] {a b c : α} (hc : mul_le_cancellable c) : a * c = b * c ↔ a = b := hc.injective_left.eq_iff variable [has_le α] @[to_additive] protected lemma mul_le_mul_iff_left [has_mul α] [covariant_class α α (*) (≤)] {a b c : α} (ha : mul_le_cancellable a) : a * b ≤ a * c ↔ b ≤ c := ⟨λ h, ha h, λ h, mul_le_mul_left' h a⟩ @[to_additive] protected lemma mul_le_mul_iff_right [comm_semigroup α] [covariant_class α α (*) (≤)] {a b c : α} (ha : mul_le_cancellable a) : b * a ≤ c * a ↔ b ≤ c := by rw [mul_comm b, mul_comm c, ha.mul_le_mul_iff_left] @[to_additive] protected lemma le_mul_iff_one_le_right [mul_one_class α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : a ≤ a * b ↔ 1 ≤ b := iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left @[to_additive] protected lemma mul_le_iff_le_one_right [mul_one_class α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : a * b ≤ a ↔ b ≤ 1 := iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left @[to_additive] protected lemma le_mul_iff_one_le_left [comm_monoid α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : a ≤ b * a ↔ 1 ≤ b := by rw [mul_comm, ha.le_mul_iff_one_le_right] @[to_additive] protected lemma mul_le_iff_le_one_left [comm_monoid α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : b * a ≤ a ↔ b ≤ 1 := by rw [mul_comm, ha.mul_le_iff_le_one_right] end mul_le_cancellable
65479b22f49e5b5fa16736e6d03b6599c7f19a30
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/abelian/non_preadditive.lean
22cddc8ee19033c4929d366ab9698bd45e6fac06
[ "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
32,034
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.kernels import category_theory.limits.shapes.normal_mono import category_theory.preadditive /-! # Every non_preadditive_abelian category is preadditive In mathlib, we define an abelian category as a preadditive category with a zero object, kernels and cokernels, products and coproducts and in which every monomorphism and epimorphis is normal. While virtually every interesting abelian category has a natural preadditive structure (which is why it is included in the definition), preadditivity is not actually needed: Every category that has all of the other properties appearing in the definition of an abelian category admits a preadditive structure. This is the construction we carry out in this file. The proof proceeds in roughly five steps: 1. Prove some results (for example that all equalizers exist) that would be trivial if we already had the preadditive structure but are a bit of work without it. 2. Develop images and coimages to show that every monomorphism is the kernel of its cokernel. The results of the first two steps are also useful for the "normal" development of abelian categories, and will be used there. 3. For every object `A`, define a "subtraction" morphism `σ : A ⨯ A ⟶ A` and use it to define subtraction on morphisms as `f - g := prod.lift f g ≫ σ`. 4. Prove a small number of identities about this subtraction from the definition of `σ`. 5. From these identities, prove a large number of other identities that imply that defining `f + g := f - (0 - g)` indeed gives an abelian group structure on morphisms such that composition is bilinear. The construction is non-trivial and it is quite remarkable that this abelian group structure can be constructed purely from the existence of a few limits and colimits. What's even more impressive is that all additive structures on a category are in some sense isomorphic, so for abelian categories with a natural preadditive structure, this construction manages to "almost" reconstruct this natural structure. However, we have not formalized this isomorphism. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable theory open category_theory open category_theory.limits namespace category_theory section universes v u variables (C : Type u) [category.{v} C] /-- We call a category `non_preadditive_abelian` if it has a zero object, kernels, cokernels, binary products and coproducts, and every monomorphism and every epimorphism is normal. -/ class non_preadditive_abelian := [has_zero_object : has_zero_object C] [has_zero_morphisms : has_zero_morphisms C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] [has_finite_products : has_finite_products C] [has_finite_coproducts : has_finite_coproducts C] (normal_mono : Π {X Y : C} (f : X ⟶ Y) [mono f], normal_mono f) (normal_epi : Π {X Y : C} (f : X ⟶ Y) [epi f], normal_epi f) set_option default_priority 100 attribute [instance] non_preadditive_abelian.has_zero_object attribute [instance] non_preadditive_abelian.has_zero_morphisms attribute [instance] non_preadditive_abelian.has_kernels attribute [instance] non_preadditive_abelian.has_cokernels attribute [instance] non_preadditive_abelian.has_finite_products attribute [instance] non_preadditive_abelian.has_finite_coproducts end end category_theory open category_theory namespace category_theory.non_preadditive_abelian universes v u variables {C : Type u} [category.{v} C] section variables [non_preadditive_abelian C] section strong local attribute [instance] non_preadditive_abelian.normal_epi /-- In a `non_preadditive_abelian` category, every epimorphism is strong. -/ lemma strong_epi_of_epi {P Q : C} (f : P ⟶ Q) [epi f] : strong_epi f := by apply_instance end strong section mono_epi_iso variables {X Y : C} (f : X ⟶ Y) local attribute [instance] strong_epi_of_epi /-- In a `non_preadditive_abelian` category, a monomorphism which is also an epimorphism is an isomorphism. -/ lemma is_iso_of_mono_of_epi [mono f] [epi f] : is_iso f := is_iso_of_mono_of_strong_epi _ end mono_epi_iso /-- The pullback of two monomorphisms exists. -/ @[irreducible] lemma pullback_of_mono {X Y Z : C} (a : X ⟶ Z) (b : Y ⟶ Z) [mono a] [mono b] : has_limit (cospan a b) := let ⟨P, f, haf, i⟩ := non_preadditive_abelian.normal_mono a in let ⟨Q, g, hbg, i'⟩ := non_preadditive_abelian.normal_mono b in let ⟨a', ha'⟩ := kernel_fork.is_limit.lift' i (kernel.ι (prod.lift f g)) $ calc kernel.ι (prod.lift f g) ≫ f = kernel.ι (prod.lift f g) ≫ prod.lift f g ≫ limits.prod.fst : by rw prod.lift_fst ... = (0 : kernel (prod.lift f g) ⟶ P ⨯ Q) ≫ limits.prod.fst : by rw kernel.condition_assoc ... = 0 : zero_comp in let ⟨b', hb'⟩ := kernel_fork.is_limit.lift' i' (kernel.ι (prod.lift f g)) $ calc kernel.ι (prod.lift f g) ≫ g = kernel.ι (prod.lift f g) ≫ (prod.lift f g) ≫ limits.prod.snd : by rw prod.lift_snd ... = (0 : kernel (prod.lift f g) ⟶ P ⨯ Q) ≫ limits.prod.snd : by rw kernel.condition_assoc ... = 0 : zero_comp in has_limit.mk { cone := pullback_cone.mk a' b' $ by { simp at ha' hb', rw [ha', hb'] }, is_limit := pullback_cone.is_limit.mk _ (λ s, kernel.lift (prod.lift f g) (pullback_cone.snd s ≫ b) $ prod.hom_ext (calc ((pullback_cone.snd s ≫ b) ≫ prod.lift f g) ≫ limits.prod.fst = pullback_cone.snd s ≫ b ≫ f : by simp only [prod.lift_fst, category.assoc] ... = pullback_cone.fst s ≫ a ≫ f : by rw pullback_cone.condition_assoc ... = pullback_cone.fst s ≫ 0 : by rw haf ... = 0 ≫ limits.prod.fst : by rw [comp_zero, zero_comp]) (calc ((pullback_cone.snd s ≫ b) ≫ prod.lift f g) ≫ limits.prod.snd = pullback_cone.snd s ≫ b ≫ g : by simp only [prod.lift_snd, category.assoc] ... = pullback_cone.snd s ≫ 0 : by rw hbg ... = 0 ≫ limits.prod.snd : by rw [comp_zero, zero_comp])) (λ s, (cancel_mono a).1 $ by { rw kernel_fork.ι_of_ι at ha', simp [ha', pullback_cone.condition s] }) (λ s, (cancel_mono b).1 $ by { rw kernel_fork.ι_of_ι at hb', simp [hb'] }) (λ s m h₁ h₂, (cancel_mono (kernel.ι (prod.lift f g))).1 $ calc m ≫ kernel.ι (prod.lift f g) = m ≫ a' ≫ a : by { congr, exact ha'.symm } ... = pullback_cone.fst s ≫ a : by rw [←category.assoc, h₁] ... = pullback_cone.snd s ≫ b : pullback_cone.condition s ... = kernel.lift (prod.lift f g) (pullback_cone.snd s ≫ b) _ ≫ kernel.ι (prod.lift f g) : by rw kernel.lift_ι) } /-- The pushout of two epimorphisms exists. -/ @[irreducible] lemma pushout_of_epi {X Y Z : C} (a : X ⟶ Y) (b : X ⟶ Z) [epi a] [epi b] : has_colimit (span a b) := let ⟨P, f, hfa, i⟩ := non_preadditive_abelian.normal_epi a in let ⟨Q, g, hgb, i'⟩ := non_preadditive_abelian.normal_epi b in let ⟨a', ha'⟩ := cokernel_cofork.is_colimit.desc' i (cokernel.π (coprod.desc f g)) $ calc f ≫ cokernel.π (coprod.desc f g) = coprod.inl ≫ coprod.desc f g ≫ cokernel.π (coprod.desc f g) : by rw coprod.inl_desc_assoc ... = coprod.inl ≫ (0 : P ⨿ Q ⟶ cokernel (coprod.desc f g)) : by rw cokernel.condition ... = 0 : has_zero_morphisms.comp_zero _ _ in let ⟨b', hb'⟩ := cokernel_cofork.is_colimit.desc' i' (cokernel.π (coprod.desc f g)) $ calc g ≫ cokernel.π (coprod.desc f g) = coprod.inr ≫ coprod.desc f g ≫ cokernel.π (coprod.desc f g) : by rw coprod.inr_desc_assoc ... = coprod.inr ≫ (0 : P ⨿ Q ⟶ cokernel (coprod.desc f g)) : by rw cokernel.condition ... = 0 : has_zero_morphisms.comp_zero _ _ in has_colimit.mk { cocone := pushout_cocone.mk a' b' $ by { simp only [cofork.π_of_π] at ha' hb', rw [ha', hb'] }, is_colimit := pushout_cocone.is_colimit.mk _ (λ s, cokernel.desc (coprod.desc f g) (b ≫ pushout_cocone.inr s) $ coprod.hom_ext (calc coprod.inl ≫ coprod.desc f g ≫ b ≫ pushout_cocone.inr s = f ≫ b ≫ pushout_cocone.inr s : by rw coprod.inl_desc_assoc ... = f ≫ a ≫ pushout_cocone.inl s : by rw pushout_cocone.condition ... = 0 ≫ pushout_cocone.inl s : by rw reassoc_of hfa ... = coprod.inl ≫ 0 : by rw [comp_zero, zero_comp]) (calc coprod.inr ≫ coprod.desc f g ≫ b ≫ pushout_cocone.inr s = g ≫ b ≫ pushout_cocone.inr s : by rw coprod.inr_desc_assoc ... = 0 ≫ pushout_cocone.inr s : by rw reassoc_of hgb ... = coprod.inr ≫ 0 : by rw [comp_zero, zero_comp])) (λ s, (cancel_epi a).1 $ by { rw cokernel_cofork.π_of_π at ha', simp [reassoc_of ha', pushout_cocone.condition s] }) (λ s, (cancel_epi b).1 $ by { rw cokernel_cofork.π_of_π at hb', simp [reassoc_of hb'] }) (λ s m h₁ h₂, (cancel_epi (cokernel.π (coprod.desc f g))).1 $ calc cokernel.π (coprod.desc f g) ≫ m = (a ≫ a') ≫ m : by { congr, exact ha'.symm } ... = a ≫ pushout_cocone.inl s : by rw [category.assoc, h₁] ... = b ≫ pushout_cocone.inr s : pushout_cocone.condition s ... = cokernel.π (coprod.desc f g) ≫ cokernel.desc (coprod.desc f g) (b ≫ pushout_cocone.inr s) _ : by rw cokernel.π_desc) } section local attribute [instance] pullback_of_mono /-- The pullback of `(𝟙 X, f)` and `(𝟙 X, g)` -/ private abbreviation P {X Y : C} (f g : X ⟶ Y) [mono (prod.lift (𝟙 X) f)] [mono (prod.lift (𝟙 X) g)] : C := pullback (prod.lift (𝟙 X) f) (prod.lift (𝟙 X) g) /-- The equalizer of `f` and `g` exists. -/ @[irreducible] lemma has_limit_parallel_pair {X Y : C} (f g : X ⟶ Y) : has_limit (parallel_pair f g) := have huv : (pullback.fst : P f g ⟶ X) = pullback.snd, from calc (pullback.fst : P f g ⟶ X) = pullback.fst ≫ 𝟙 _ : eq.symm $ category.comp_id _ ... = pullback.fst ≫ prod.lift (𝟙 X) f ≫ limits.prod.fst : by rw prod.lift_fst ... = pullback.snd ≫ prod.lift (𝟙 X) g ≫ limits.prod.fst : by rw pullback.condition_assoc ... = pullback.snd : by rw [prod.lift_fst, category.comp_id], have hvu : (pullback.fst : P f g ⟶ X) ≫ f = pullback.snd ≫ g, from calc (pullback.fst : P f g ⟶ X) ≫ f = pullback.fst ≫ prod.lift (𝟙 X) f ≫ limits.prod.snd : by rw prod.lift_snd ... = pullback.snd ≫ prod.lift (𝟙 X) g ≫ limits.prod.snd : by rw pullback.condition_assoc ... = pullback.snd ≫ g : by rw prod.lift_snd, have huu : (pullback.fst : P f g ⟶ X) ≫ f = pullback.fst ≫ g, by rw [hvu, ←huv], has_limit.mk { cone := fork.of_ι pullback.fst huu, is_limit := fork.is_limit.mk _ (λ s, pullback.lift (fork.ι s) (fork.ι s) $ prod.hom_ext (by simp only [prod.lift_fst, category.assoc]) (by simp only [fork.app_zero_right, fork.app_zero_left, prod.lift_snd, category.assoc])) (λ s, by simp only [fork.ι_of_ι, pullback.lift_fst]) (λ s m h, pullback.hom_ext (by simpa only [pullback.lift_fst] using h walking_parallel_pair.zero) (by simpa only [huv.symm, pullback.lift_fst] using h walking_parallel_pair.zero)) } end section local attribute [instance] pushout_of_epi /-- The pushout of `(𝟙 Y, f)` and `(𝟙 Y, g)`. -/ private abbreviation Q {X Y : C} (f g : X ⟶ Y) [epi (coprod.desc (𝟙 Y) f)] [epi (coprod.desc (𝟙 Y) g)] : C := pushout (coprod.desc (𝟙 Y) f) (coprod.desc (𝟙 Y) g) /-- The coequalizer of `f` and `g` exists. -/ @[irreducible] lemma has_colimit_parallel_pair {X Y : C} (f g : X ⟶ Y) : has_colimit (parallel_pair f g) := have huv : (pushout.inl : Y ⟶ Q f g) = pushout.inr, from calc (pushout.inl : Y ⟶ Q f g) = 𝟙 _ ≫ pushout.inl : eq.symm $ category.id_comp _ ... = (coprod.inl ≫ coprod.desc (𝟙 Y) f) ≫ pushout.inl : by rw coprod.inl_desc ... = (coprod.inl ≫ coprod.desc (𝟙 Y) g) ≫ pushout.inr : by simp only [category.assoc, pushout.condition] ... = pushout.inr : by rw [coprod.inl_desc, category.id_comp], have hvu : f ≫ (pushout.inl : Y ⟶ Q f g) = g ≫ pushout.inr, from calc f ≫ (pushout.inl : Y ⟶ Q f g) = (coprod.inr ≫ coprod.desc (𝟙 Y) f) ≫ pushout.inl : by rw coprod.inr_desc ... = (coprod.inr ≫ coprod.desc (𝟙 Y) g) ≫ pushout.inr : by simp only [category.assoc, pushout.condition] ... = g ≫ pushout.inr : by rw coprod.inr_desc, have huu : f ≫ (pushout.inl : Y ⟶ Q f g) = g ≫ pushout.inl, by rw [hvu, huv], has_colimit.mk { cocone := cofork.of_π pushout.inl huu, is_colimit := cofork.is_colimit.mk _ (λ s, pushout.desc (cofork.π s) (cofork.π s) $ coprod.hom_ext (by simp only [coprod.inl_desc_assoc]) (by simp only [cofork.right_app_one, coprod.inr_desc_assoc, cofork.left_app_one])) (λ s, by simp only [pushout.inl_desc, cofork.π_of_π]) (λ s m h, pushout.hom_ext (by simpa only [pushout.inl_desc] using h walking_parallel_pair.one) (by simpa only [huv.symm, pushout.inl_desc] using h walking_parallel_pair.one)) } end section local attribute [instance] has_limit_parallel_pair /-- A `non_preadditive_abelian` category has all equalizers. -/ @[priority 100] instance has_equalizers : has_equalizers C := has_equalizers_of_has_limit_parallel_pair _ end section local attribute [instance] has_colimit_parallel_pair /-- A `non_preadditive_abelian` category has all coequalizers. -/ @[priority 100] instance has_coequalizers : has_coequalizers C := has_coequalizers_of_has_colimit_parallel_pair _ end section /-- If a zero morphism is a kernel of `f`, then `f` is a monomorphism. -/ lemma mono_of_zero_kernel {X Y : C} (f : X ⟶ Y) (Z : C) (l : is_limit (kernel_fork.of_ι (0 : Z ⟶ X) (show 0 ≫ f = 0, by simp))) : mono f := ⟨λ P u v huv, begin obtain ⟨W, w, hw, hl⟩ := non_preadditive_abelian.normal_epi (coequalizer.π u v), obtain ⟨m, hm⟩ := coequalizer.desc' f huv, have hwf : w ≫ f = 0, { rw [←hm, reassoc_of hw, zero_comp] }, obtain ⟨n, hn⟩ := kernel_fork.is_limit.lift' l _ hwf, rw [fork.ι_of_ι, has_zero_morphisms.comp_zero] at hn, haveI : is_iso (coequalizer.π u v) := by apply is_iso_colimit_cocone_parallel_pair_of_eq hn.symm hl, apply (cancel_mono (coequalizer.π u v)).1, exact coequalizer.condition _ _ end⟩ /-- If a zero morphism is a cokernel of `f`, then `f` is an epimorphism. -/ lemma epi_of_zero_cokernel {X Y : C} (f : X ⟶ Y) (Z : C) (l : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ Z) (show f ≫ 0 = 0, by simp))) : epi f := ⟨λ P u v huv, begin obtain ⟨W, w, hw, hl⟩ := non_preadditive_abelian.normal_mono (equalizer.ι u v), obtain ⟨m, hm⟩ := equalizer.lift' f huv, have hwf : f ≫ w = 0, { rw [←hm, category.assoc, hw, comp_zero] }, obtain ⟨n, hn⟩ := cokernel_cofork.is_colimit.desc' l _ hwf, rw [cofork.π_of_π, zero_comp] at hn, haveI : is_iso (equalizer.ι u v) := by apply is_iso_limit_cone_parallel_pair_of_eq hn.symm hl, apply (cancel_epi (equalizer.ι u v)).1, exact equalizer.condition _ _ end⟩ open_locale zero_object /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `0 : 0 ⟶ X` is a kernel of `f`. -/ def zero_kernel_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : is_limit (kernel_fork.of_ι (0 : 0 ⟶ X) (show 0 ≫ f = 0, by simp)) := fork.is_limit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (kernel_fork.condition s), zero_comp]) (λ s m h, by ext) /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `0 : Y ⟶ 0` is a cokernel of `f`. -/ def zero_cokernel_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ 0) (show f ≫ 0 = 0, by simp)) := cofork.is_colimit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (cokernel_cofork.condition s), comp_zero]) (λ s m h, by ext) /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `f` is a monomorphism. -/ lemma mono_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : mono f := mono_of_zero_kernel f 0 $ zero_kernel_of_cancel_zero f hf /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `g` is a monomorphism. -/ lemma epi_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : epi f := epi_of_zero_cokernel f 0 $ zero_cokernel_of_zero_cancel f hf end section factor variables {P Q : C} (f : P ⟶ Q) /-- The kernel of the cokernel of `f` is called the image of `f`. -/ protected abbreviation image : C := kernel (cokernel.π f) /-- The inclusion of the image into the codomain. -/ protected abbreviation image.ι : non_preadditive_abelian.image f ⟶ Q := kernel.ι (cokernel.π f) /-- There is a canonical epimorphism `p : P ⟶ image f` for every `f`. -/ protected abbreviation factor_thru_image : P ⟶ non_preadditive_abelian.image f := kernel.lift (cokernel.π f) f $ cokernel.condition f /-- `f` factors through its image via the canonical morphism `p`. -/ @[simp, reassoc] protected lemma image.fac : non_preadditive_abelian.factor_thru_image f ≫ image.ι f = f := kernel.lift_ι _ _ _ /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (non_preadditive_abelian.factor_thru_image f) := let I := non_preadditive_abelian.image f, p := non_preadditive_abelian.factor_thru_image f, i := kernel.ι (cokernel.π f) in -- It will suffice to consider some g : I ⟶ R such that p ≫ g = 0 and show that g = 0. epi_of_zero_cancel _ $ λ R (g : I ⟶ R) (hpg : p ≫ g = 0), begin -- Since C is abelian, u := ker g ≫ i is the kernel of some morphism h. let u := kernel.ι g ≫ i, haveI : mono u := mono_comp _ _, haveI hu := non_preadditive_abelian.normal_mono u, let h := hu.g, -- By hypothesis, p factors through the kernel of g via some t. obtain ⟨t, ht⟩ := kernel.lift' g p hpg, have fh : f ≫ h = 0, calc f ≫ h = (p ≫ i) ≫ h : (image.fac f).symm ▸ rfl ... = ((t ≫ kernel.ι g) ≫ i) ≫ h : ht ▸ rfl ... = t ≫ u ≫ h : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = t ≫ 0 : hu.w ▸ rfl ... = 0 : has_zero_morphisms.comp_zero _ _, -- h factors through the cokernel of f via some l. obtain ⟨l, hl⟩ := cokernel.desc' f h fh, have hih : i ≫ h = 0, calc i ≫ h = i ≫ cokernel.π f ≫ l : hl ▸ rfl ... = 0 ≫ l : by rw [←category.assoc, kernel.condition] ... = 0 : zero_comp, -- i factors through u = ker h via some s. obtain ⟨s, hs⟩ := normal_mono.lift' u i hih, have hs' : (s ≫ kernel.ι g) ≫ i = 𝟙 I ≫ i, by rw [category.assoc, hs, category.id_comp], haveI : epi (kernel.ι g) := epi_of_epi_fac ((cancel_mono _).1 hs'), -- ker g is an epimorphism, but ker g ≫ g = 0 = ker g ≫ 0, so g = 0 as required. exact zero_of_epi_comp _ (kernel.condition g) end instance mono_factor_thru_image [mono f] : mono (non_preadditive_abelian.factor_thru_image f) := mono_of_mono_fac $ image.fac f instance is_iso_factor_thru_image [mono f] : is_iso (non_preadditive_abelian.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- The cokernel of the kernel of `f` is called the coimage of `f`. -/ protected abbreviation coimage : C := cokernel (kernel.ι f) /-- The projection onto the coimage. -/ protected abbreviation coimage.π : P ⟶ non_preadditive_abelian.coimage f := cokernel.π (kernel.ι f) /-- There is a canonical monomorphism `i : coimage f ⟶ Q`. -/ protected abbreviation factor_thru_coimage : non_preadditive_abelian.coimage f ⟶ Q := cokernel.desc (kernel.ι f) f $ kernel.condition f /-- `f` factors through its coimage via the canonical morphism `p`. -/ protected lemma coimage.fac : coimage.π f ≫ non_preadditive_abelian.factor_thru_coimage f = f := cokernel.π_desc _ _ _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (non_preadditive_abelian.factor_thru_coimage f) := let I := non_preadditive_abelian.coimage f, i := non_preadditive_abelian.factor_thru_coimage f, p := cokernel.π (kernel.ι f) in mono_of_cancel_zero _ $ λ R (g : R ⟶ I) (hgi : g ≫ i = 0), begin -- Since C is abelian, u := p ≫ coker g is the cokernel of some morphism h. let u := p ≫ cokernel.π g, haveI : epi u := epi_comp _ _, haveI hu := non_preadditive_abelian.normal_epi u, let h := hu.g, -- By hypothesis, i factors through the cokernel of g via some t. obtain ⟨t, ht⟩ := cokernel.desc' g i hgi, have hf : h ≫ f = 0, calc h ≫ f = h ≫ (p ≫ i) : (coimage.fac f).symm ▸ rfl ... = h ≫ (p ≫ (cokernel.π g ≫ t)) : ht ▸ rfl ... = h ≫ u ≫ t : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = 0 ≫ t : by rw [←category.assoc, hu.w] ... = 0 : zero_comp, -- h factors through the kernel of f via some l. obtain ⟨l, hl⟩ := kernel.lift' f h hf, have hhp : h ≫ p = 0, calc h ≫ p = (l ≫ kernel.ι f) ≫ p : hl ▸ rfl ... = l ≫ 0 : by rw [category.assoc, cokernel.condition] ... = 0 : comp_zero, -- p factors through u = coker h via some s. obtain ⟨s, hs⟩ := normal_epi.desc' u p hhp, have hs' : p ≫ cokernel.π g ≫ s = p ≫ 𝟙 I, by rw [←category.assoc, hs, category.comp_id], haveI : mono (cokernel.π g) := mono_of_mono_fac ((cancel_epi _).1 hs'), -- coker g is a monomorphism, but g ≫ coker g = 0 = 0 ≫ coker g, so g = 0 as required. exact zero_of_comp_mono _ (cokernel.condition g) end instance epi_factor_thru_coimage [epi f] : epi (non_preadditive_abelian.factor_thru_coimage f) := epi_of_epi_fac $ coimage.fac f instance is_iso_factor_thru_coimage [epi f] : is_iso (non_preadditive_abelian.factor_thru_coimage f) := is_iso_of_mono_of_epi _ end factor section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} /-- In a `non_preadditive_abelian` category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := is_cokernel.cokernel_iso _ _ (cokernel.of_iso_comp _ _ (limits.is_limit.cone_point_unique_up_to_iso (limit.is_limit _) h) (cone_morphism.w (limits.is_limit.unique_up_to_iso (limit.is_limit _) h).hom _)) (as_iso $ non_preadditive_abelian.factor_thru_coimage f) (coimage.fac f) /-- In a `non_preadditive_abelian` category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := is_kernel.iso_kernel _ _ (kernel.of_comp_iso _ _ (limits.is_colimit.cocone_point_unique_up_to_iso h (colimit.is_colimit _)) (cocone_morphism.w (limits.is_colimit.unique_up_to_iso h $ colimit.is_colimit _).hom _)) (as_iso $ non_preadditive_abelian.factor_thru_image f) (image.fac f) end cokernel_of_kernel section /-- The composite `A ⟶ A ⨯ A ⟶ cokernel (Δ A)`, where the first map is `(𝟙 A, 0)` and the second map is the canonical projection into the cokernel. -/ abbreviation r (A : C) : A ⟶ cokernel (diag A) := prod.lift (𝟙 A) 0 ≫ cokernel.π (diag A) instance mono_Δ {A : C} : mono (diag A) := mono_of_mono_fac $ prod.lift_fst _ _ instance mono_r {A : C} : mono (r A) := begin let hl : is_limit (kernel_fork.of_ι (diag A) (cokernel.condition (diag A))), { exact mono_is_kernel_of_cokernel _ (colimit.is_colimit _) }, apply mono_of_cancel_zero, intros Z x hx, have hxx : (x ≫ prod.lift (𝟙 A) (0 : A ⟶ A)) ≫ cokernel.π (diag A) = 0, { rw [category.assoc, hx] }, obtain ⟨y, hy⟩ := kernel_fork.is_limit.lift' hl _ hxx, rw kernel_fork.ι_of_ι at hy, have hyy : y = 0, { erw [←category.comp_id y, ←limits.prod.lift_snd (𝟙 A) (𝟙 A), ←category.assoc, hy, category.assoc, prod.lift_snd, has_zero_morphisms.comp_zero] }, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, rw [←hy, hyy, zero_comp, zero_comp] end instance epi_r {A : C} : epi (r A) := begin have hlp : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ limits.prod.snd = 0 := prod.lift_snd _ _, let hp1 : is_limit (kernel_fork.of_ι (prod.lift (𝟙 A) (0 : A ⟶ A)) hlp), { refine fork.is_limit.mk _ (λ s, fork.ι s ≫ limits.prod.fst) _ _, { intro s, ext; simp, erw category.comp_id }, { intros s m h, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, convert h walking_parallel_pair.zero, ext; simp } }, let hp2 : is_colimit (cokernel_cofork.of_π (limits.prod.snd : A ⨯ A ⟶ A) hlp), { exact epi_is_cokernel_of_kernel _ hp1 }, apply epi_of_zero_cancel, intros Z z hz, have h : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ cokernel.π (diag A) ≫ z = 0, { rw [←category.assoc, hz] }, obtain ⟨t, ht⟩ := cokernel_cofork.is_colimit.desc' hp2 _ h, rw cokernel_cofork.π_of_π at ht, have htt : t = 0, { rw [←category.id_comp t], change 𝟙 A ≫ t = 0, rw [←limits.prod.lift_snd (𝟙 A) (𝟙 A), category.assoc, ht, ←category.assoc, cokernel.condition, zero_comp] }, apply (cancel_epi (cokernel.π (diag A))).1, rw [←ht, htt, comp_zero, comp_zero] end instance is_iso_r {A : C} : is_iso (r A) := is_iso_of_mono_of_epi _ /-- The composite `A ⨯ A ⟶ cokernel (diag A) ⟶ A` given by the natural projection into the cokernel followed by the inverse of `r`. In the category of modules, using the normal kernels and cokernels, this map is equal to the map `(a, b) ↦ a - b`, hence the name `σ` for "subtraction". -/ abbreviation σ {A : C} : A ⨯ A ⟶ A := cokernel.π (diag A) ≫ inv (r A) end @[simp, reassoc] lemma diag_σ {X : C} : diag X ≫ σ = 0 := by rw [cokernel.condition_assoc, zero_comp] @[simp, reassoc] lemma lift_σ {X : C} : prod.lift (𝟙 X) 0 ≫ σ = 𝟙 X := by rw [←category.assoc, is_iso.hom_inv_id] @[reassoc] lemma lift_map {X Y : C} (f : X ⟶ Y) : prod.lift (𝟙 X) 0 ≫ limits.prod.map f f = f ≫ prod.lift (𝟙 Y) 0 := by simp /-- σ is a cokernel of Δ X. -/ def is_colimit_σ {X : C} : is_colimit (cokernel_cofork.of_π σ diag_σ) := cokernel.cokernel_iso _ σ (as_iso (r X)).symm (by rw [iso.symm_hom, as_iso_inv]) /-- This is the key identity satisfied by `σ`. -/ lemma σ_comp {X Y : C} (f : X ⟶ Y) : σ ≫ f = limits.prod.map f f ≫ σ := begin obtain ⟨g, hg⟩ := cokernel_cofork.is_colimit.desc' is_colimit_σ (limits.prod.map f f ≫ σ) (by simp), suffices hfg : f = g, { rw [←hg, cofork.π_of_π, hfg] }, calc f = f ≫ prod.lift (𝟙 Y) 0 ≫ σ : by rw [lift_σ, category.comp_id] ... = prod.lift (𝟙 X) 0 ≫ limits.prod.map f f ≫ σ : by rw lift_map_assoc ... = prod.lift (𝟙 X) 0 ≫ σ ≫ g : by rw [←hg, cokernel_cofork.π_of_π] ... = g : by rw [←category.assoc, lift_σ, category.id_comp] end section /- We write `f - g` for `prod.lift f g ≫ σ`. -/ /-- Subtraction of morphisms in a `non_preadditive_abelian` category. -/ def has_sub {X Y : C} : has_sub (X ⟶ Y) := ⟨λ f g, prod.lift f g ≫ σ⟩ local attribute [instance] has_sub /- We write `-f` for `0 - f`. -/ /-- Negation of morphisms in a `non_preadditive_abelian` category. -/ def has_neg {X Y : C} : has_neg (X ⟶ Y) := ⟨λ f, 0 - f⟩ local attribute [instance] has_neg /- We write `f + g` for `f - (-g)`. -/ /-- Addition of morphisms in a `non_preadditive_abelian` category. -/ def has_add {X Y : C} : has_add (X ⟶ Y) := ⟨λ f g, f - (-g)⟩ local attribute [instance] has_add lemma sub_def {X Y : C} (a b : X ⟶ Y) : a - b = prod.lift a b ≫ σ := rfl lemma add_def {X Y : C} (a b : X ⟶ Y) : a + b = a - (-b) := rfl lemma neg_def {X Y : C} (a : X ⟶ Y) : -a = 0 - a := rfl lemma sub_zero {X Y : C} (a : X ⟶ Y) : a - 0 = a := begin rw sub_def, conv_lhs { congr, congr, rw ←category.comp_id a, skip, rw (show 0 = a ≫ (0 : Y ⟶ Y), by simp)}, rw [← prod.comp_lift, category.assoc, lift_σ, category.comp_id] end lemma sub_self {X Y : C} (a : X ⟶ Y) : a - a = 0 := by rw [sub_def, ←category.comp_id a, ← prod.comp_lift, category.assoc, diag_σ, comp_zero] lemma lift_sub_lift {X Y : C} (a b c d : X ⟶ Y) : prod.lift a b - prod.lift c d = prod.lift (a - c) (b - d) := begin simp only [sub_def], ext, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_fst, prod.lift_fst, prod.lift_fst] }, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_snd, prod.lift_snd, prod.lift_snd] } end lemma sub_sub_sub {X Y : C} (a b c d : X ⟶ Y) : (a - c) - (b - d) = (a - b) - (c - d) := begin rw [sub_def, ←lift_sub_lift, sub_def, category.assoc, σ_comp, prod.lift_map_assoc], refl end lemma neg_sub {X Y : C} (a b : X ⟶ Y) : (-a) - b = (-b) - a := by conv_lhs { rw [neg_def, ←sub_zero b, sub_sub_sub, sub_zero, ←neg_def] } lemma neg_neg {X Y : C} (a : X ⟶ Y) : -(-a) = a := begin rw [neg_def, neg_def], conv_lhs { congr, rw ←sub_self a }, rw [sub_sub_sub, sub_zero, sub_self, sub_zero] end lemma add_comm {X Y : C} (a b : X ⟶ Y) : a + b = b + a := begin rw [add_def], conv_lhs { rw ←neg_neg a }, rw [neg_def, neg_def, neg_def, sub_sub_sub], conv_lhs {congr, skip, rw [←neg_def, neg_sub] }, rw [sub_sub_sub, add_def, ←neg_def, neg_neg b, neg_def] end lemma add_neg {X Y : C} (a b : X ⟶ Y) : a + (-b) = a - b := by rw [add_def, neg_neg] lemma add_neg_self {X Y : C} (a : X ⟶ Y) : a + (-a) = 0 := by rw [add_neg, sub_self] lemma neg_add_self {X Y : C} (a : X ⟶ Y) : (-a) + a = 0 := by rw [add_comm, add_neg_self] lemma neg_sub' {X Y : C} (a b : X ⟶ Y) : -(a - b) = (-a) + b := begin rw [neg_def, neg_def], conv_lhs { rw ←sub_self (0 : X ⟶ Y) }, rw [sub_sub_sub, add_def, neg_def] end lemma neg_add {X Y : C} (a b : X ⟶ Y) : -(a + b) = (-a) - b := by rw [add_def, neg_sub', add_neg] lemma sub_add {X Y : C} (a b c : X ⟶ Y) : (a - b) + c = a - (b - c) := by rw [add_def, neg_def, sub_sub_sub, sub_zero] lemma add_assoc {X Y : C} (a b c : X ⟶ Y) : (a + b) + c = a + (b + c) := begin conv_lhs { congr, rw add_def }, rw [sub_add, ←add_neg, neg_sub', neg_neg] end lemma add_zero {X Y : C} (a : X ⟶ Y) : a + 0 = a := by rw [add_def, neg_def, sub_self, sub_zero] lemma comp_sub {X Y Z : C} (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g - h) = f ≫ g - f ≫ h := by rw [sub_def, ←category.assoc, prod.comp_lift, sub_def] lemma sub_comp {X Y Z : C} (f g : X ⟶ Y) (h : Y ⟶ Z) : (f - g) ≫ h = f ≫ h - g ≫ h := by rw [sub_def, category.assoc, σ_comp, ←category.assoc, prod.lift_map, sub_def] lemma comp_add (X Y Z : C) (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g + h) = f ≫ g + f ≫ h := by rw [add_def, comp_sub, neg_def, comp_sub, comp_zero, add_def, neg_def] lemma add_comp (X Y Z : C) (f g : X ⟶ Y) (h : Y ⟶ Z) : (f + g) ≫ h = f ≫ h + g ≫ h := by rw [add_def, sub_comp, neg_def, sub_comp, zero_comp, add_def, neg_def] /-- Every `non_preadditive_abelian` category is preadditive. -/ def preadditive : preadditive C := { hom_group := λ X Y, { add := (+), add_assoc := add_assoc, zero := 0, zero_add := neg_neg, add_zero := add_zero, neg := λ f, -f, add_left_neg := neg_add_self, add_comm := add_comm }, add_comp' := add_comp, comp_add' := comp_add } end end end category_theory.non_preadditive_abelian
5d89e525e021c87e1bf2a121c59708a990f280a5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/inner_product_space/two_dim.lean
4dc3137d0858225e1cefcd132ee1c63eb46d9aa5
[ "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
26,930
lean
/- Copyright (c) 2022 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.inner_product_space.dual import analysis.inner_product_space.orientation import tactic.linear_combination /-! # Oriented two-dimensional real inner product spaces This file defines constructions specific to the geometry of an oriented two-dimensional real inner product space `E`. ## Main declarations * `orientation.area_form`: an antisymmetric bilinear form `E →ₗ[ℝ] E →ₗ[ℝ] ℝ` (usual notation `ω`). Morally, when `ω` is evaluated on two vectors, it gives the oriented area of the parallelogram they span. (But mathlib does not yet have a construction of oriented area, and in fact the construction of oriented area should pass through `ω`.) * `orientation.right_angle_rotation`: an isometric automorphism `E ≃ₗᵢ[ℝ] E` (usual notation `J`). This automorphism squares to -1. In a later file, rotations (`orientation.rotation`) are defined, in such a way that this automorphism is equal to rotation by 90 degrees. * `orientation.basis_right_angle_rotation`: for a nonzero vector `x` in `E`, the basis `![x, J x]` for `E`. * `orientation.kahler`: a complex-valued real-bilinear map `E →ₗ[ℝ] E →ₗ[ℝ] ℂ`. Its real part is the inner product and its imaginary part is `orientation.area_form`. For vectors `x` and `y` in `E`, the complex number `o.kahler x y` has modulus `‖x‖ * ‖y‖`. In a later file, oriented angles (`orientation.oangle`) are defined, in such a way that the argument of `o.kahler x y` is the oriented angle from `x` to `y`. ## Main results * `orientation.right_angle_rotation_right_angle_rotation`: the identity `J (J x) = - x` * `orientation.nonneg_inner_and_area_form_eq_zero_iff_same_ray`: `x`, `y` are in the same ray, if and only if `0 ≤ ⟪x, y⟫` and `ω x y = 0` * `orientation.kahler_mul`: the identity `o.kahler x a * o.kahler a y = ‖a‖ ^ 2 * o.kahler x y` * `complex.area_form`, `complex.right_angle_rotation`, `complex.kahler`: the concrete interpretations of `area_form`, `right_angle_rotation`, `kahler` for the oriented real inner product space `ℂ` * `orientation.area_form_map_complex`, `orientation.right_angle_rotation_map_complex`, `orientation.kahler_map_complex`: given an orientation-preserving isometry from `E` to `ℂ`, expressions for `area_form`, `right_angle_rotation`, `kahler` as the pullback of their concrete interpretations on `ℂ` ## Implementation notes Notation `ω` for `orientation.area_form` and `J` for `orientation.right_angle_rotation` should be defined locally in each file which uses them, since otherwise one would need a more cumbersome notation which mentions the orientation explicitly (something like `ω[o]`). Write ``` local notation `ω` := o.area_form local notation `J` := o.right_angle_rotation ``` -/ noncomputable theory open_locale real_inner_product_space complex_conjugate open finite_dimensional local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ variables {E : Type*} [inner_product_space ℝ E] [fact (finrank ℝ E = 2)] (o : orientation ℝ E (fin 2)) namespace orientation include o /-- An antisymmetric bilinear form on an oriented real inner product space of dimension 2 (usual notation `ω`). When evaluated on two vectors, it gives the oriented area of the parallelogram they span. -/ @[irreducible] def area_form : E →ₗ[ℝ] E →ₗ[ℝ] ℝ := begin let z : alternating_map ℝ E ℝ (fin 0) ≃ₗ[ℝ] ℝ := alternating_map.const_linear_equiv_of_is_empty.symm, let y : alternating_map ℝ E ℝ (fin 1) →ₗ[ℝ] E →ₗ[ℝ] ℝ := (linear_map.llcomp ℝ E (alternating_map ℝ E ℝ (fin 0)) ℝ z) ∘ₗ alternating_map.curry_left_linear_map, exact y ∘ₗ (alternating_map.curry_left_linear_map o.volume_form), end omit o local notation `ω` := o.area_form lemma area_form_to_volume_form (x y : E) : ω x y = o.volume_form ![x, y] := by simp [area_form] @[simp] lemma area_form_apply_self (x : E) : ω x x = 0 := begin rw area_form_to_volume_form, refine o.volume_form.map_eq_zero_of_eq ![x, x] _ (_ : (0 : fin 2) ≠ 1), { simp }, { norm_num } end lemma area_form_swap (x y : E) : ω x y = - ω y x := begin simp only [area_form_to_volume_form], convert o.volume_form.map_swap ![y, x] (_ : (0 : fin 2) ≠ 1), { ext i, fin_cases i; refl }, { norm_num } end @[simp] lemma area_form_neg_orientation : (-o).area_form = -o.area_form := begin ext x y, simp [area_form_to_volume_form] end /-- Continuous linear map version of `orientation.area_form`, useful for calculus. -/ def area_form' : E →L[ℝ] (E →L[ℝ] ℝ) := ((↑(linear_map.to_continuous_linear_map : (E →ₗ[ℝ] ℝ) ≃ₗ[ℝ] (E →L[ℝ] ℝ))) ∘ₗ o.area_form).to_continuous_linear_map @[simp] lemma area_form'_apply (x : E) : o.area_form' x = (o.area_form x).to_continuous_linear_map := rfl lemma abs_area_form_le (x y : E) : |ω x y| ≤ ‖x‖ * ‖y‖ := by simpa [area_form_to_volume_form, fin.prod_univ_succ] using o.abs_volume_form_apply_le ![x, y] lemma area_form_le (x y : E) : ω x y ≤ ‖x‖ * ‖y‖ := by simpa [area_form_to_volume_form, fin.prod_univ_succ] using o.volume_form_apply_le ![x, y] lemma abs_area_form_of_orthogonal {x y : E} (h : ⟪x, y⟫ = 0) : |ω x y| = ‖x‖ * ‖y‖ := begin rw [o.area_form_to_volume_form, o.abs_volume_form_apply_of_pairwise_orthogonal], { simp [fin.prod_univ_succ] }, intros i j hij, fin_cases i; fin_cases j, { simpa }, { simpa using h }, { simpa [real_inner_comm] using h }, { simpa } end lemma area_form_map {F : Type*} [inner_product_space ℝ F] [fact (finrank ℝ F = 2)] (φ : E ≃ₗᵢ[ℝ] F) (x y : F) : (orientation.map (fin 2) φ.to_linear_equiv o).area_form x y = o.area_form (φ.symm x) (φ.symm y) := begin have : φ.symm ∘ ![x, y] = ![φ.symm x, φ.symm y], { ext i, fin_cases i; refl }, simp [area_form_to_volume_form, volume_form_map, this], end /-- The area form is invariant under pullback by a positively-oriented isometric automorphism. -/ lemma area_form_comp_linear_isometry_equiv (φ : E ≃ₗᵢ[ℝ] E) (hφ : 0 < (φ.to_linear_equiv : E →ₗ[ℝ] E).det) (x y : E) : o.area_form (φ x) (φ y) = o.area_form x y := begin convert o.area_form_map φ (φ x) (φ y), { symmetry, rwa ← o.map_eq_iff_det_pos φ.to_linear_equiv at hφ, rw [fact.out (finrank ℝ E = 2), fintype.card_fin] }, { simp }, { simp } end /-- Auxiliary construction for `orientation.right_angle_rotation`, rotation by 90 degrees in an oriented real inner product space of dimension 2. -/ @[irreducible] def right_angle_rotation_aux₁ : E →ₗ[ℝ] E := let to_dual : E ≃ₗ[ℝ] (E →ₗ[ℝ] ℝ) := (inner_product_space.to_dual ℝ E).to_linear_equiv ≪≫ₗ linear_map.to_continuous_linear_map.symm in ↑to_dual.symm ∘ₗ ω @[simp] lemma inner_right_angle_rotation_aux₁_left (x y : E) : ⟪o.right_angle_rotation_aux₁ x, y⟫ = ω x y := by simp only [right_angle_rotation_aux₁, linear_equiv.trans_symm, linear_equiv.coe_trans, linear_equiv.coe_coe, inner_product_space.to_dual_symm_apply, eq_self_iff_true, linear_map.coe_to_continuous_linear_map', linear_isometry_equiv.coe_to_linear_equiv, linear_map.comp_apply, linear_equiv.symm_symm, linear_isometry_equiv.to_linear_equiv_symm] @[simp] lemma inner_right_angle_rotation_aux₁_right (x y : E) : ⟪x, o.right_angle_rotation_aux₁ y⟫ = - ω x y := begin rw real_inner_comm, simp [o.area_form_swap y x], end /-- Auxiliary construction for `orientation.right_angle_rotation`, rotation by 90 degrees in an oriented real inner product space of dimension 2. -/ def right_angle_rotation_aux₂ : E →ₗᵢ[ℝ] E := { norm_map' := λ x, begin dsimp, refine le_antisymm _ _, { cases eq_or_lt_of_le (norm_nonneg (o.right_angle_rotation_aux₁ x)) with h h, { rw ← h, positivity }, refine le_of_mul_le_mul_right _ h, rw [← real_inner_self_eq_norm_mul_norm, o.inner_right_angle_rotation_aux₁_left], exact o.area_form_le x (o.right_angle_rotation_aux₁ x) }, { let K : submodule ℝ E := ℝ ∙ x, haveI : nontrivial Kᗮ, { apply @finite_dimensional.nontrivial_of_finrank_pos ℝ, have : finrank ℝ K ≤ finset.card {x}, { rw ← set.to_finset_singleton, exact finrank_span_le_card ({x} : set E) }, have : finset.card {x} = 1 := finset.card_singleton x, have : finrank ℝ K + finrank ℝ Kᗮ = finrank ℝ E := K.finrank_add_finrank_orthogonal, have : finrank ℝ E = 2 := fact.out _, linarith }, obtain ⟨w, hw₀⟩ : ∃ w : Kᗮ, w ≠ 0 := exists_ne 0, have hw' : ⟪x, (w:E)⟫ = 0 := inner_right_of_mem_orthogonal_singleton x w.2, -- hw'₀, have hw : (w:E) ≠ 0 := λ h, hw₀ (submodule.coe_eq_zero.mp h), refine le_of_mul_le_mul_right _ (by rwa norm_pos_iff : 0 < ‖(w:E)‖), rw ← o.abs_area_form_of_orthogonal hw', rw ← o.inner_right_angle_rotation_aux₁_left x w, exact abs_real_inner_le_norm (o.right_angle_rotation_aux₁ x) w }, end, .. o.right_angle_rotation_aux₁ } @[simp] lemma right_angle_rotation_aux₁_right_angle_rotation_aux₁ (x : E) : o.right_angle_rotation_aux₁ (o.right_angle_rotation_aux₁ x) = - x := begin apply ext_inner_left ℝ, intros y, have : ⟪o.right_angle_rotation_aux₁ y, o.right_angle_rotation_aux₁ x⟫ = ⟪y, x⟫ := linear_isometry.inner_map_map o.right_angle_rotation_aux₂ y x, rw [o.inner_right_angle_rotation_aux₁_right, ← o.inner_right_angle_rotation_aux₁_left, this, inner_neg_right], end /-- An isometric automorphism of an oriented real inner product space of dimension 2 (usual notation `J`). This automorphism squares to -1. We will define rotations in such a way that this automorphism is equal to rotation by 90 degrees. -/ @[irreducible] def right_angle_rotation : E ≃ₗᵢ[ℝ] E := linear_isometry_equiv.of_linear_isometry o.right_angle_rotation_aux₂ (-o.right_angle_rotation_aux₁) (by ext; simp [right_angle_rotation_aux₂]) (by ext; simp [right_angle_rotation_aux₂]) local notation `J` := o.right_angle_rotation @[simp] lemma inner_right_angle_rotation_left (x y : E) : ⟪J x, y⟫ = ω x y := begin rw right_angle_rotation, exact o.inner_right_angle_rotation_aux₁_left x y end @[simp] lemma inner_right_angle_rotation_right (x y : E) : ⟪x, J y⟫ = - ω x y := begin rw right_angle_rotation, exact o.inner_right_angle_rotation_aux₁_right x y end @[simp] lemma right_angle_rotation_right_angle_rotation (x : E) : J (J x) = - x := begin rw right_angle_rotation, exact o.right_angle_rotation_aux₁_right_angle_rotation_aux₁ x end @[simp] lemma right_angle_rotation_symm : linear_isometry_equiv.symm J = linear_isometry_equiv.trans J (linear_isometry_equiv.neg ℝ) := begin rw right_angle_rotation, exact linear_isometry_equiv.to_linear_isometry_injective rfl end @[simp] lemma inner_right_angle_rotation_self (x : E) : ⟪J x, x⟫ = 0 := by simp lemma inner_right_angle_rotation_swap (x y : E) : ⟪x, J y⟫ = - ⟪J x, y⟫ := by simp lemma inner_right_angle_rotation_swap' (x y : E) : ⟪J x, y⟫ = - ⟪x, J y⟫ := by simp [o.inner_right_angle_rotation_swap x y] lemma inner_comp_right_angle_rotation (x y : E) : ⟪J x, J y⟫ = ⟪x, y⟫ := linear_isometry_equiv.inner_map_map J x y @[simp] lemma area_form_right_angle_rotation_left (x y : E) : ω (J x) y = - ⟪x, y⟫ := by rw [← o.inner_comp_right_angle_rotation, o.inner_right_angle_rotation_right, neg_neg] @[simp] lemma area_form_right_angle_rotation_right (x y : E) : ω x (J y) = ⟪x, y⟫ := by rw [← o.inner_right_angle_rotation_left, o.inner_comp_right_angle_rotation] @[simp] lemma area_form_comp_right_angle_rotation (x y : E) : ω (J x) (J y) = ω x y := by simp @[simp] lemma right_angle_rotation_trans_right_angle_rotation : linear_isometry_equiv.trans J J = linear_isometry_equiv.neg ℝ := by ext; simp lemma right_angle_rotation_neg_orientation (x : E) : (-o).right_angle_rotation x = - o.right_angle_rotation x := begin apply ext_inner_right ℝ, intros y, rw inner_right_angle_rotation_left, simp end @[simp] lemma right_angle_rotation_trans_neg_orientation : (-o).right_angle_rotation = o.right_angle_rotation.trans (linear_isometry_equiv.neg ℝ) := linear_isometry_equiv.ext $ o.right_angle_rotation_neg_orientation lemma right_angle_rotation_map {F : Type*} [inner_product_space ℝ F] [fact (finrank ℝ F = 2)] (φ : E ≃ₗᵢ[ℝ] F) (x : F) : (orientation.map (fin 2) φ.to_linear_equiv o).right_angle_rotation x = φ (o.right_angle_rotation (φ.symm x)) := begin apply ext_inner_right ℝ, intros y, rw inner_right_angle_rotation_left, transitivity ⟪J (φ.symm x), φ.symm y⟫, { simp [o.area_form_map] }, transitivity ⟪φ (J (φ.symm x)), φ (φ.symm y)⟫, { rw φ.inner_map_map }, { simp }, end /-- `J` commutes with any positively-oriented isometric automorphism. -/ lemma linear_isometry_equiv_comp_right_angle_rotation (φ : E ≃ₗᵢ[ℝ] E) (hφ : 0 < (φ.to_linear_equiv : E →ₗ[ℝ] E).det) (x : E) : φ (J x) = J (φ x) := begin convert (o.right_angle_rotation_map φ (φ x)).symm, { simp }, { symmetry, rwa ← o.map_eq_iff_det_pos φ.to_linear_equiv at hφ, rw [fact.out (finrank ℝ E = 2), fintype.card_fin] }, end lemma right_angle_rotation_map' {F : Type*} [inner_product_space ℝ F] [fact (finrank ℝ F = 2)] (φ : E ≃ₗᵢ[ℝ] F) : (orientation.map (fin 2) φ.to_linear_equiv o).right_angle_rotation = (φ.symm.trans o.right_angle_rotation).trans φ := linear_isometry_equiv.ext $ o.right_angle_rotation_map φ /-- `J` commutes with any positively-oriented isometric automorphism. -/ lemma linear_isometry_equiv_comp_right_angle_rotation' (φ : E ≃ₗᵢ[ℝ] E) (hφ : 0 < (φ.to_linear_equiv : E →ₗ[ℝ] E).det) : linear_isometry_equiv.trans J φ = φ.trans J := linear_isometry_equiv.ext $ o.linear_isometry_equiv_comp_right_angle_rotation φ hφ /-- For a nonzero vector `x` in an oriented two-dimensional real inner product space `E`, `![x, J x]` forms an (orthogonal) basis for `E`. -/ def basis_right_angle_rotation (x : E) (hx : x ≠ 0) : basis (fin 2) ℝ E := @basis_of_linear_independent_of_card_eq_finrank ℝ _ _ _ _ _ _ _ ![x, J x] (linear_independent_of_ne_zero_of_inner_eq_zero (λ i, by { fin_cases i; simp [hx] }) begin intros i j hij, fin_cases i; fin_cases j, { simpa }, { simp }, { simp }, { simpa } end) (fact.out (finrank ℝ E = 2)).symm @[simp] lemma coe_basis_right_angle_rotation (x : E) (hx : x ≠ 0) : ⇑(o.basis_right_angle_rotation x hx) = ![x, J x] := coe_basis_of_linear_independent_of_card_eq_finrank _ _ /-- For vectors `a x y : E`, the identity `⟪a, x⟫ * ⟪a, y⟫ + ω a x * ω a y = ‖a‖ ^ 2 * ⟪x, y⟫`. (See `orientation.inner_mul_inner_add_area_form_mul_area_form` for the "applied" form.)-/ lemma inner_mul_inner_add_area_form_mul_area_form' (a x : E) : ⟪a, x⟫ • @innerₛₗ ℝ _ _ _ a + ω a x • ω a = ‖a‖ ^ 2 • @innerₛₗ ℝ _ _ _ x := begin by_cases ha : a = 0, { simp [ha] }, apply (o.basis_right_angle_rotation a ha).ext, intros i, fin_cases i, { simp only [real_inner_self_eq_norm_sq, algebra.id.smul_eq_mul, innerₛₗ_apply, linear_map.smul_apply, linear_map.add_apply, matrix.cons_val_zero, o.coe_basis_right_angle_rotation, o.area_form_apply_self, real_inner_comm], ring }, { simp only [real_inner_self_eq_norm_sq, algebra.id.smul_eq_mul, innerₛₗ_apply, linear_map.smul_apply, neg_inj, linear_map.add_apply, matrix.cons_val_one, matrix.head_cons, o.coe_basis_right_angle_rotation, o.area_form_right_angle_rotation_right, o.area_form_apply_self, o.inner_right_angle_rotation_right], rw o.area_form_swap, ring, } end /-- For vectors `a x y : E`, the identity `⟪a, x⟫ * ⟪a, y⟫ + ω a x * ω a y = ‖a‖ ^ 2 * ⟪x, y⟫`. -/ lemma inner_mul_inner_add_area_form_mul_area_form (a x y : E) : ⟪a, x⟫ * ⟪a, y⟫ + ω a x * ω a y = ‖a‖ ^ 2 * ⟪x, y⟫ := congr_arg (λ f : E →ₗ[ℝ] ℝ, f y) (o.inner_mul_inner_add_area_form_mul_area_form' a x) lemma inner_sq_add_area_form_sq (a b : E) : ⟪a, b⟫ ^ 2 + ω a b ^ 2 = ‖a‖ ^ 2 * ‖b‖ ^ 2 := by simpa [sq, real_inner_self_eq_norm_sq] using o.inner_mul_inner_add_area_form_mul_area_form a b b /-- For vectors `a x y : E`, the identity `⟪a, x⟫ * ω a y - ω a x * ⟪a, y⟫ = ‖a‖ ^ 2 * ω x y`. (See `orientation.inner_mul_area_form_sub` for the "applied" form.) -/ lemma inner_mul_area_form_sub' (a x : E) : ⟪a, x⟫ • ω a - ω a x • @innerₛₗ ℝ _ _ _ a = ‖a‖ ^ 2 • ω x := begin by_cases ha : a = 0, { simp [ha] }, apply (o.basis_right_angle_rotation a ha).ext, intros i, fin_cases i, { simp only [o.coe_basis_right_angle_rotation, o.area_form_apply_self, o.area_form_swap a x, real_inner_self_eq_norm_sq, algebra.id.smul_eq_mul, innerₛₗ_apply, linear_map.sub_apply, linear_map.smul_apply, matrix.cons_val_zero], ring }, { simp only [o.area_form_right_angle_rotation_right, o.area_form_apply_self, o.coe_basis_right_angle_rotation, o.inner_right_angle_rotation_right, real_inner_self_eq_norm_sq, real_inner_comm, algebra.id.smul_eq_mul, innerₛₗ_apply, linear_map.smul_apply, linear_map.sub_apply, matrix.cons_val_one, matrix.head_cons], ring}, end /-- For vectors `a x y : E`, the identity `⟪a, x⟫ * ω a y - ω a x * ⟪a, y⟫ = ‖a‖ ^ 2 * ω x y`. -/ lemma inner_mul_area_form_sub (a x y : E) : ⟪a, x⟫ * ω a y - ω a x * ⟪a, y⟫ = ‖a‖ ^ 2 * ω x y := congr_arg (λ f : E →ₗ[ℝ] ℝ, f y) (o.inner_mul_area_form_sub' a x) lemma nonneg_inner_and_area_form_eq_zero_iff_same_ray (x y : E) : 0 ≤ ⟪x, y⟫ ∧ ω x y = 0 ↔ same_ray ℝ x y := begin by_cases hx : x = 0, { simp [hx] }, split, { let a : ℝ := (o.basis_right_angle_rotation x hx).repr y 0, let b : ℝ := (o.basis_right_angle_rotation x hx).repr y 1, suffices : 0 ≤ a * ‖x‖ ^ 2 ∧ b * ‖x‖ ^ 2 = 0 → same_ray ℝ x (a • x + b • J x), { -- TODO trace the `dsimp` lemmas in this block to make a single `simp only` rw ← (o.basis_right_angle_rotation x hx).sum_repr y, simp only [fin.sum_univ_succ, coe_basis_right_angle_rotation], dsimp, simp only [o.area_form_apply_self, map_smul, map_add, map_zero, inner_smul_left, inner_smul_right, inner_add_left, inner_add_right, inner_zero_right, linear_map.add_apply, matrix.cons_val_one], dsimp, simp only [o.area_form_right_angle_rotation_right, mul_zero, add_zero, zero_add, neg_zero, o.inner_right_angle_rotation_right, o.area_form_apply_self, real_inner_self_eq_norm_sq], exact this }, rintros ⟨ha, hb⟩, have hx' : 0 < ‖x‖ := by simpa using hx, have ha' : 0 ≤ a := nonneg_of_mul_nonneg_left ha (by positivity), have hb' : b = 0 := eq_zero_of_ne_zero_of_mul_right_eq_zero (pow_ne_zero 2 hx'.ne') hb, simpa [hb'] using same_ray_nonneg_smul_right x ha' }, { intros h, obtain ⟨r, hr, rfl⟩ := h.exists_nonneg_left hx, simp only [inner_smul_right, real_inner_self_eq_norm_sq, linear_map.map_smulₛₗ, area_form_apply_self, algebra.id.smul_eq_mul, mul_zero, eq_self_iff_true, and_true], positivity }, end /-- A complex-valued real-bilinear map on an oriented real inner product space of dimension 2. Its real part is the inner product and its imaginary part is `orientation.area_form`. On `ℂ` with the standard orientation, `kahler w z = conj w * z`; see `complex.kahler`. -/ def kahler : E →ₗ[ℝ] E →ₗ[ℝ] ℂ := (linear_map.llcomp ℝ E ℝ ℂ complex.of_real_clm) ∘ₗ (@innerₛₗ ℝ E _ _) + (linear_map.llcomp ℝ E ℝ ℂ ((linear_map.lsmul ℝ ℂ).flip complex.I)) ∘ₗ ω lemma kahler_apply_apply (x y : E) : o.kahler x y = ⟪x, y⟫ + ω x y • complex.I := rfl lemma kahler_swap (x y : E) : o.kahler x y = conj (o.kahler y x) := begin simp only [kahler_apply_apply], rw [real_inner_comm, area_form_swap], simp, end @[simp] lemma kahler_apply_self (x : E) : o.kahler x x = ‖x‖ ^ 2 := by simp [kahler_apply_apply, real_inner_self_eq_norm_sq] @[simp] lemma kahler_right_angle_rotation_left (x y : E) : o.kahler (J x) y = - complex.I * o.kahler x y := begin simp only [o.area_form_right_angle_rotation_left, o.inner_right_angle_rotation_left, o.kahler_apply_apply, complex.of_real_neg, complex.real_smul], linear_combination ω x y * complex.I_sq, end @[simp] lemma kahler_right_angle_rotation_right (x y : E) : o.kahler x (J y) = complex.I * o.kahler x y := begin simp only [o.area_form_right_angle_rotation_right, o.inner_right_angle_rotation_right, o.kahler_apply_apply, complex.of_real_neg, complex.real_smul], linear_combination - ω x y * complex.I_sq, end @[simp] lemma kahler_comp_right_angle_rotation (x y : E) : o.kahler (J x) (J y) = o.kahler x y := begin simp only [kahler_right_angle_rotation_left, kahler_right_angle_rotation_right], linear_combination - o.kahler x y * complex.I_sq, end @[simp] lemma kahler_neg_orientation (x y : E) : (-o).kahler x y = conj (o.kahler x y) := by simp [kahler_apply_apply] lemma kahler_mul (a x y : E) : o.kahler x a * o.kahler a y = ‖a‖ ^ 2 * o.kahler x y := begin transitivity (↑(‖a‖ ^ 2) : ℂ) * o.kahler x y, { ext, { simp only [o.kahler_apply_apply, complex.add_im, complex.add_re, complex.I_im, complex.I_re, complex.mul_im, complex.mul_re, complex.of_real_im, complex.of_real_re, complex.real_smul], rw [real_inner_comm a x, o.area_form_swap x a], linear_combination o.inner_mul_inner_add_area_form_mul_area_form a x y }, { simp only [o.kahler_apply_apply, complex.add_im, complex.add_re, complex.I_im, complex.I_re, complex.mul_im, complex.mul_re, complex.of_real_im, complex.of_real_re, complex.real_smul], rw [real_inner_comm a x, o.area_form_swap x a], linear_combination o.inner_mul_area_form_sub a x y } }, { norm_cast }, end lemma norm_sq_kahler (x y : E) : complex.norm_sq (o.kahler x y) = ‖x‖ ^ 2 * ‖y‖ ^ 2 := by simpa [kahler_apply_apply, complex.norm_sq, sq] using o.inner_sq_add_area_form_sq x y lemma abs_kahler (x y : E) : complex.abs (o.kahler x y) = ‖x‖ * ‖y‖ := begin rw [← sq_eq_sq, complex.sq_abs], { linear_combination o.norm_sq_kahler x y }, { positivity }, { positivity } end lemma norm_kahler (x y : E) : ‖o.kahler x y‖ = ‖x‖ * ‖y‖ := by simpa using o.abs_kahler x y lemma eq_zero_or_eq_zero_of_kahler_eq_zero {x y : E} (hx : o.kahler x y = 0) : x = 0 ∨ y = 0 := begin have : ‖x‖ * ‖y‖ = 0 := by simpa [hx] using (o.norm_kahler x y).symm, cases eq_zero_or_eq_zero_of_mul_eq_zero this with h h, { left, simpa using h }, { right, simpa using h }, end lemma kahler_eq_zero_iff (x y : E) : o.kahler x y = 0 ↔ x = 0 ∨ y = 0 := begin refine ⟨o.eq_zero_or_eq_zero_of_kahler_eq_zero, _⟩, rintros (rfl | rfl); simp, end lemma kahler_ne_zero {x y : E} (hx : x ≠ 0) (hy : y ≠ 0) : o.kahler x y ≠ 0 := begin apply mt o.eq_zero_or_eq_zero_of_kahler_eq_zero, tauto, end lemma kahler_ne_zero_iff (x y : E) : o.kahler x y ≠ 0 ↔ x ≠ 0 ∧ y ≠ 0 := begin refine ⟨_, λ h, o.kahler_ne_zero h.1 h.2⟩, contrapose, simp only [not_and_distrib, not_not, kahler_apply_apply, complex.real_smul], rintros (rfl | rfl); simp, end lemma kahler_map {F : Type*} [inner_product_space ℝ F] [fact (finrank ℝ F = 2)] (φ : E ≃ₗᵢ[ℝ] F) (x y : F) : (orientation.map (fin 2) φ.to_linear_equiv o).kahler x y = o.kahler (φ.symm x) (φ.symm y) := by simp [kahler_apply_apply, area_form_map] /-- The bilinear map `kahler` is invariant under pullback by a positively-oriented isometric automorphism. -/ lemma kahler_comp_linear_isometry_equiv (φ : E ≃ₗᵢ[ℝ] E) (hφ : 0 < (φ.to_linear_equiv : E →ₗ[ℝ] E).det) (x y : E) : o.kahler (φ x) (φ y) = o.kahler x y := by simp [kahler_apply_apply, o.area_form_comp_linear_isometry_equiv φ hφ] end orientation namespace complex local attribute [instance] complex.finrank_real_complex_fact @[simp] protected lemma area_form (w z : ℂ) : complex.orientation.area_form w z = (conj w * z).im := begin let o := complex.orientation, simp only [o.area_form_to_volume_form, o.volume_form_robust complex.orthonormal_basis_one_I rfl, basis.det_apply, matrix.det_fin_two, basis.to_matrix_apply,to_basis_orthonormal_basis_one_I, matrix.cons_val_zero, coe_basis_one_I_repr, matrix.cons_val_one, matrix.head_cons, mul_im, conj_re, conj_im], ring, end @[simp] protected lemma right_angle_rotation (z : ℂ) : complex.orientation.right_angle_rotation z = I * z := begin apply ext_inner_right ℝ, intros w, rw orientation.inner_right_angle_rotation_left, simp only [complex.area_form, complex.inner, mul_re, mul_im, conj_re, conj_im, map_mul, conj_I, neg_re, neg_im, I_re, I_im], ring, end @[simp] protected lemma kahler (w z : ℂ) : complex.orientation.kahler w z = conj w * z := begin rw orientation.kahler_apply_apply, ext1; simp, end end complex namespace orientation local notation `ω` := o.area_form local notation `J` := o.right_angle_rotation open complex /-- The area form on an oriented real inner product space of dimension 2 can be evaluated in terms of a complex-number representation of the space. -/ lemma area_form_map_complex (f : E ≃ₗᵢ[ℝ] ℂ) (hf : (orientation.map (fin 2) f.to_linear_equiv o) = complex.orientation) (x y : E) : ω x y = (conj (f x) * f y).im := begin rw [← complex.area_form, ← hf, o.area_form_map], simp, end /-- The rotation by 90 degrees on an oriented real inner product space of dimension 2 can be evaluated in terms of a complex-number representation of the space. -/ lemma right_angle_rotation_map_complex (f : E ≃ₗᵢ[ℝ] ℂ) (hf : (orientation.map (fin 2) f.to_linear_equiv o) = complex.orientation) (x : E) : f (J x) = I * f x := begin rw [← complex.right_angle_rotation, ← hf, o.right_angle_rotation_map], simp, end /-- The Kahler form on an oriented real inner product space of dimension 2 can be evaluated in terms of a complex-number representation of the space. -/ lemma kahler_map_complex (f : E ≃ₗᵢ[ℝ] ℂ) (hf : (orientation.map (fin 2) f.to_linear_equiv o) = complex.orientation) (x y : E) : o.kahler x y = conj (f x) * f y := begin rw [← complex.kahler, ← hf, o.kahler_map], simp, end end orientation
545c5b12e3b30b1826bf22dcb7e26420ba2928b1
2385ce0e3b60d8dbea33dd439902a2070cca7a24
/tests/lean/run/1804a.lean
da6fbf403955eabd1eaecbad5524082eeb2aa36a
[ "Apache-2.0" ]
permissive
TehMillhouse/lean
68d6fdd2fb11a6c65bc28dec308d70f04dad38b4
6bbf2fbd8912617e5a973575bab8c383c9c268a1
refs/heads/master
1,620,830,893,339
1,515,592,479,000
1,515,592,997,000
116,964,828
0
0
null
1,515,592,734,000
1,515,592,734,000
null
UTF-8
Lean
false
false
688
lean
section parameter P : match unit.star with | unit.star := true end include P example : false := begin dsimp [_match_1] at P, guard_hyp P := true, sorry end end section parameter P : match unit.star with | unit.star := true end include P example : false := begin dsimp [_match_1] at P, guard_hyp P := true, sorry end end section parameter P : match unit.star with | unit.star := true end parameter Q : match unit.star with | unit.star := true end section include P example : false := begin dsimp [_match_1] at P, guard_hyp P := true, sorry end end section include Q example : false := begin dsimp [_match_2] at Q, guard_hyp Q := true, sorry end end end
0694e6d39d3339394d77a84a2295b64c80a259fe
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Data/HashMap.lean
1c6779cedfca18470118bded785446a8737910e3
[ "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
202
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 -/ prelude import Init.Data.HashMap.Basic
30d19c1d7feb42e483878c182735f14e7dea83bb
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch2/ex0207.lean
ad18c6f7b573fcd4400b3580391e5cb5f6ab1bcf
[]
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
27
lean
#check Type #check Type 0
8661fdf64cc1a70d39dd79a4b398432d26dddd6c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/comp_value_tactics.lean
61b74f8870075b9abf762bcbaa38e3f5817635a6
[]
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
297
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.tactic import Mathlib.Lean3Lib.init.data.option.basic namespace Mathlib
8c082029bca39c05b66650edb746876710a5a015
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/nat/pow.lean
4b2ac6720c4e647c86a08a364b43f3dcae2fb112
[ "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
7,726
lean
/- Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import algebra.group_power.order /-! # `nat.pow` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Results on the power operation on natural numbers. -/ namespace nat /-! ### `pow` -/ -- This is redundant with `pow_le_pow_of_le_left'`, -- We leave a version in the `nat` namespace as well. -- (The global `pow_le_pow_of_le_left` needs an extra hypothesis `0 ≤ x`.) protected theorem pow_le_pow_of_le_left {x y : ℕ} (H : x ≤ y) : ∀ i : ℕ, x^i ≤ y^i := pow_le_pow_of_le_left' H theorem pow_le_pow_of_le_right {x : ℕ} (H : 0 < x) {i j : ℕ} (h : i ≤ j) : x ^ i ≤ x ^ j := pow_le_pow' H h theorem pow_lt_pow_of_lt_left {x y : ℕ} (H : x < y) {i} (h : 0 < i) : x^i < y^i := pow_lt_pow_of_lt_left H (zero_le _) h theorem pow_lt_pow_of_lt_right {x : ℕ} (H : 1 < x) {i j : ℕ} (h : i < j) : x^i < x^j := pow_lt_pow H h lemma pow_lt_pow_succ {p : ℕ} (h : 1 < p) (n : ℕ) : p^n < p^(n+1) := pow_lt_pow_of_lt_right h n.lt_succ_self lemma le_self_pow {n : ℕ} (hn : n ≠ 0) : ∀ m : ℕ, m ≤ m ^ n | 0 := zero_le _ | (m + 1) := _root_.le_self_pow dec_trivial hn lemma lt_pow_self {p : ℕ} (h : 1 < p) : ∀ n : ℕ, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≤ p ^ (n+1) : pow_lt_pow_succ h _ lemma lt_two_pow (n : ℕ) : n < 2^n := lt_pow_self dec_trivial n lemma one_le_pow (n m : ℕ) (h : 0 < m) : 1 ≤ m^n := by { rw ←one_pow n, exact nat.pow_le_pow_of_le_left h n } lemma one_le_pow' (n m : ℕ) : 1 ≤ (m+1)^n := one_le_pow n (m+1) (succ_pos m) lemma one_le_two_pow (n : ℕ) : 1 ≤ 2^n := one_le_pow n 2 dec_trivial lemma one_lt_pow (n m : ℕ) (h₀ : 0 < n) (h₁ : 1 < m) : 1 < m^n := by { rw ←one_pow n, exact pow_lt_pow_of_lt_left h₁ h₀ } lemma one_lt_pow' (n m : ℕ) : 1 < (m+2)^(n+1) := one_lt_pow (n+1) (m+2) (succ_pos n) (nat.lt_of_sub_eq_succ rfl) @[simp] lemma one_lt_pow_iff {k n : ℕ} (h : 0 ≠ k) : 1 < n ^ k ↔ 1 < n := begin cases n, { cases k; simp [zero_pow_eq] }, cases n, { rw one_pow }, refine ⟨λ _, one_lt_succ_succ n, λ _, _⟩, induction k with k hk, { exact absurd rfl h }, cases k, { simp }, exact one_lt_mul (one_lt_succ_succ _).le (hk (succ_ne_zero k).symm), end lemma one_lt_two_pow (n : ℕ) (h₀ : 0 < n) : 1 < 2^n := one_lt_pow n 2 h₀ dec_trivial lemma one_lt_two_pow' (n : ℕ) : 1 < 2^(n+1) := one_lt_pow (n+1) 2 (succ_pos n) dec_trivial lemma pow_right_strict_mono {x : ℕ} (k : 2 ≤ x) : strict_mono (λ (n : ℕ), x^n) := λ _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : ℕ} (k : 2 ≤ x) : x^m ≤ x^n ↔ m ≤ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : ℕ} (k : 2 ≤ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : ℕ} (k : 2 ≤ x) : function.injective (λ (n : ℕ), x^n) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : ℕ} (k : 1 ≤ m) : strict_mono (λ (x : ℕ), x^m) := λ _ _ h, pow_lt_pow_of_lt_left h k lemma mul_lt_mul_pow_succ {n a q : ℕ} (a0 : 0 < a) (q1 : 1 < q) : n * q < a * q ^ (n + 1) := begin rw [pow_succ', ← mul_assoc, mul_lt_mul_right (zero_lt_one.trans q1)], exact lt_mul_of_one_le_of_lt (nat.succ_le_iff.mpr a0) (nat.lt_pow_self q1 n), end end nat lemma strict_mono.nat_pow {n : ℕ} (hn : 1 ≤ n) {f : ℕ → ℕ} (hf : strict_mono f) : strict_mono (λ m, (f m) ^ n) := (nat.pow_left_strict_mono hn).comp hf namespace nat lemma pow_le_iff_le_left {m x y : ℕ} (k : 1 ≤ m) : x^m ≤ y^m ↔ x ≤ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : ℕ} (k : 1 ≤ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : ℕ} (k : 1 ≤ m) : function.injective (λ (x : ℕ), x^m) := strict_mono.injective (pow_left_strict_mono k) theorem sq_sub_sq (a b : ℕ) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by { rw [sq, sq], exact nat.mul_self_sub_mul_self_eq a b } alias sq_sub_sq ← pow_two_sub_pow_two /-! ### `pow` and `mod` / `dvd` -/ theorem pow_mod (a b n : ℕ) : a ^ b % n = (a % n) ^ b % n := begin induction b with b ih, refl, simp [pow_succ, nat.mul_mod, ih], end theorem mod_pow_succ {b : ℕ} (w m : ℕ) : m % (b^succ w) = b * (m/b % b^w) + m % b := begin by_cases b_h : b = 0, { simp [b_h, pow_succ], }, have b_pos := nat.pos_of_ne_zero b_h, apply nat.strong_induction_on m, clear m, intros p IH, cases lt_or_ge p (b^succ w) with h₁ h₁, -- base case: p < b^succ w { have h₂ : p / b < b^w, { rw [div_lt_iff_lt_mul b_pos], simpa [pow_succ'] using h₁ }, rw [mod_eq_of_lt h₁, mod_eq_of_lt h₂], simp [div_add_mod] }, -- step: p ≥ b^succ w { -- Generate condition for induction hypothesis have h₂ : p - b^succ w < p, { exact tsub_lt_self ((pow_pos b_pos _).trans_le h₁) (pow_pos b_pos _) }, -- Apply induction rw [mod_eq_sub_mod h₁, IH _ h₂], -- Normalize goal and h1 simp only [pow_succ], simp only [ge, pow_succ] at h₁, -- Pull subtraction outside mod and div rw [sub_mul_mod _ _ _ h₁, sub_mul_div _ _ _ h₁], -- Cancel subtraction inside mod b^w have p_b_ge : b^w ≤ p / b, { rw [le_div_iff_mul_le b_pos, mul_comm], exact h₁ }, rw [eq.symm (mod_eq_sub_mod p_b_ge)] } end lemma pow_dvd_pow_iff_pow_le_pow {k l : ℕ} : Π {x : ℕ} (w : 0 < x), x^k ∣ x^l ↔ x^k ≤ x^l | (x+1) w := begin split, { intro a, exact le_of_dvd (pow_pos (succ_pos x) l) a, }, { intro a, cases x with x, { simp only [one_pow], }, { have le := (pow_le_iff_le_right (nat.le_add_left _ _)).mp a, use (x+2)^(l-k), rw [←pow_add, add_comm k, tsub_add_cancel_of_le le], } } end /-- If `1 < x`, then `x^k` divides `x^l` if and only if `k` is at most `l`. -/ lemma pow_dvd_pow_iff_le_right {x k l : ℕ} (w : 1 < x) : x^k ∣ x^l ↔ k ≤ l := by rw [pow_dvd_pow_iff_pow_le_pow (lt_of_succ_lt w), pow_le_iff_le_right w] lemma pow_dvd_pow_iff_le_right' {b k l : ℕ} : (b+2)^k ∣ (b+2)^l ↔ k ≤ l := pow_dvd_pow_iff_le_right (nat.lt_of_sub_eq_succ rfl) lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : 1 < p) (hk : 1 < k), ¬ p^k ∣ p | (succ p) (succ k) hp hk h := have succ p * (succ p)^k ∣ succ p * 1, by simpa [pow_succ] using h, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_left (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from nat.eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := (pow_dvd_pow _ hmn).trans hdiv lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma pow_div {x m n : ℕ} (h : n ≤ m) (hx : 0 < x) : x ^ m / x ^ n = x ^ (m - n) := by rw [nat.div_eq_iff_eq_mul_left (pow_pos hx n) (pow_dvd_pow _ h), pow_sub_mul_pow _ h] lemma lt_of_pow_dvd_right {p i n : ℕ} (hn : n ≠ 0) (hp : 2 ≤ p) (h : p ^ i ∣ n) : i < n := begin rw ←pow_lt_iff_lt_right hp, exact lt_of_le_of_lt (le_of_dvd hn.bot_lt h) (lt_pow_self (succ_le_iff.mp hp) n), end end nat
03b963dc5bf8e57b0a8f9a7170b46f7caa59f186
e2c1ee9c02c59b832eb48536755242ce5f9b2c3e
/src/t10.lean
8b385572a50a30a84cde771fb4f433b8d9c68a14
[]
no_license
yairgueta/Lean
cb5bb16d81674e8a07f85c9dbeb56dc2f543d4eb
af8a4fa24f76edfdd0dd33f013db194e611e6a86
refs/heads/master
1,676,275,529,938
1,610,289,661,000
1,610,289,661,000
328,405,309
0
0
null
null
null
null
UTF-8
Lean
false
false
1,364
lean
import tactic open tactic /-! ## Exercise 1 Write a `contradiction` tactic. The tactic should look through the hypotheses in the local context trying to find two that contradict each other, i.e. proving `P` and `¬ P` for some proposition `P`. It should use this contradiction to close the goal. Bonus: handle `P → false` as well as `¬ P`. This exercise is to practice manipulating the hypotheses and goal. Note: this exists as `tactic.interactive.contradiction`. -/ meta def contr_single : expr → expr → tactic unit | `(%%a → false) `(%%b) := if (a=b) then do skip else do failed | `(¬ %%a) `(%%b) := if (a=b) then do skip else do failed | a b := do failed meta def helper (hyp : expr) (ctx_s : expr): tactic unit := do hyp_tp ← infer_type hyp, ctx_s_type ← infer_type ctx_s, contr_single hyp_tp ctx_s_type meta def contr_single_ctx (hyp : expr) (ctx : list expr): tactic unit := ctx.mmap' (λ x, do {helper hyp x, exact (hyp x)} <|> skip) meta def tactic.interactive.contr : tactic unit := do ctx ← local_context, ctx.mmap' (λ x, (contr_single_ctx x ctx)) example (P Q R : Prop) (hp : P) (hq : Q) (hr : ¬ R) (hnq : ¬ Q) : false := by contr example (P Q R : Prop) (hnq : ¬ Q) (hp : P) (hq : Q) (hr : ¬ R) : 0 = 1 := by contr example (P Q R : Prop) (hp : P) (hq : Q) (hr : ¬ R) (hnq : Q → false) : false := by contr
6fe9ed412c86ef5f75aa2e2e7c79c9547d8b2ef1
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/number_theory/quadratic_reciprocity.lean
5a8d3488911c340fc8c659c6101eb7a5e18789f9
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
25,020
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.finite.basic import data.zmod.basic import data.nat.parity /-! # Quadratic reciprocity. This file contains results about quadratic residues modulo a prime number. The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the interpretations in terms of existence of square roots depending on the congruence mod 4, `exists_pow_two_eq_prime_iff_of_mod_four_eq_one`, and `exists_pow_two_eq_prime_iff_of_mod_four_eq_three`. Also proven are conditions for `-1` and `2` to be a square modulo a prime, `exists_pow_two_eq_neg_one_iff_mod_four_ne_three` and `exists_pow_two_eq_two_iff` ## Implementation notes The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma -/ open function finset nat finite_field zmod open_locale big_operators nat namespace zmod variables (p q : ℕ) [fact p.prime] [fact q.prime] /-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion_units (x : units (zmod p)) : (∃ y : units (zmod p), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, refine iff_of_true ⟨1, _⟩ _; apply subsingleton.elim }, obtain ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmod p)), obtain ⟨n, hn⟩ : x ∈ submonoid.powers g, { rw mem_powers_iff_mem_gpowers, apply hg }, split, { rintro ⟨y, rfl⟩, rw [← pow_mul, two_mul_odd_div_two hp_odd, units_pow_card_sub_one_eq_one], }, { subst x, assume h, have key : 2 * (p / 2) ∣ n * (p / 2), { rw [← pow_mul] at h, rw [two_mul_odd_div_two hp_odd, ← card_units, ← order_of_eq_card_of_forall_mem_gpowers hg], apply order_of_dvd_of_pow_eq_one h }, have : 0 < p / 2 := nat.div_pos (show fact (1 < p), by apply_instance) dec_trivial, obtain ⟨m, rfl⟩ := dvd_of_mul_dvd_mul_right this key, refine ⟨g ^ m, _⟩, rw [mul_comm, pow_mul], }, end /-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion {a : zmod p} (ha : a ≠ 0) : (∃ y : zmod p, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := begin apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)), simp only [units.ext_iff, pow_two, units.coe_mk0, units.coe_mul], split, { rintro ⟨y, hy⟩, exact ⟨y, hy⟩ }, { rintro ⟨y, rfl⟩, have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, }, refine ⟨units.mk0 y hy, _⟩, simp, } end lemma exists_pow_two_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmod p, y ^ 2 = -1) ↔ p % 4 ≠ 3 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, exact dec_trivial }, change fact (p % 2 = 1) at hp_odd, resetI, have neg_one_ne_zero : (-1 : zmod p) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, rw [euler_criterion p neg_one_ne_zero, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one (p / 2) with p_half_even p_half_odd, { rw [p_half_even, pow_zero, eq_self_iff_true, true_iff], contrapose! p_half_even with hp, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp], exact dec_trivial }, { rw [p_half_odd, pow_one, iff_false_intro (ne_neg_self p one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at p_half_odd, rw [_root_.fact, ← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp_odd, have hp : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp hp_odd p_half_odd, generalize : p % 4 = k, dec_trivial! } end lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, revert a ha, exact dec_trivial }, rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd], exact pow_card_sub_one_eq_one ha end /-- Wilson's Lemma: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 := begin refine calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast] ... = (∏ x : units (zmod p), x) : _ ... = -1 : by rw [prod_hom _ (coe : units (zmod p) → zmod p), prod_univ_units_id_eq_neg_one, units.coe_neg, units.coe_one], have hp : 0 < p := nat.prime.pos ‹p.prime›, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [Ico.mem, ← nat.succ_sub hp, nat.succ_sub_one], split, { apply nat.pos_of_ne_zero, rw ← @val_zero p, assume h, apply units.ne_zero a (val_injective p h) }, { exact val_lt _ } }, { intros a ha, simp only [cast_id, nat_cast_val], }, { intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h }, { intros b hb, rw [Ico.mem, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb, refine ⟨units.mk0 b _, finset.mem_univ _, _⟩, { assume h, apply hb.1, apply_fun val at h, simpa only [val_cast_of_lt hb.right, val_zero] using h }, { simp only [val_cast_of_lt hb.right, units.coe_mk0], } } end @[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 := begin conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (nat.prime.pos ‹p.prime›)] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma] end end zmod /-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set of non zero natural numbers `x` such that `x ≤ p / 2` -/ lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id (p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) : (Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) = (Ico 1 (p / 2).succ).1.map (λ a, a) := begin have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt}, have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p, from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.pos dec_trivial), have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x, from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx), have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ), (a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, { assume x hx, simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff, pos_iff_ne_zero, nat_abs_val_min_abs_le _], }, have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ), ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs, { assume b hb, refine ⟨(b / a : zmod p).val_min_abs.nat_abs, Ico.mem.mpr ⟨_, _⟩, _⟩, { apply nat.pos_of_ne_zero, simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff, val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] }, { apply lt_succ_of_le, apply nat_abs_val_min_abs_le }, { rw cast_nat_abs_val_min_abs, split_ifs, { erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg_eq_neg_mul_symm, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat] } } }, exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj (le_refl _)) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2)! : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! := calc (a ^ (p / 2) * (p / 2)! : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_factorial, prod_const, Ico.card, succ_sub_one]; simp ... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp ... = (∏ x in Ico 1 (p / 2).succ, (if (a * x : zmod p).val ≤ p / 2 then 1 else -1) * (a * x : zmod p).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : have (∏ x in Ico 1 (p / 2).succ, if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) = (∏ x in (Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1), from prod_bij_ne_one (λ x _ _, x) (λ x, by split_ifs; simp * at * {contextual := tt}) (λ _ _ _ _ _ _, id) (λ b h _, ⟨b, by simp [-not_le, *] at *⟩) (by intros; split_ifs at *; simp * at *), by rw [prod_mul_distrib, this]; simp ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial] private lemma gauss_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := (mul_left_inj' (show ((p / 2)! : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.dvd_factorial, not_le]; exact nat.div_lt_self hp.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card + ∑ x in Ico 1 (p / 2).succ, x + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2, calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) : by simp only [val_cast_nat]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, sum_nat_cast, hp2] ... = _ : congr_arg2 (+) (calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2) = ∑ x in Ico 1 (p / 2).succ, ((((a * x : zmod p).val_min_abs + (if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) : by simp only [(val_eq_ite_val_min_abs _).symm]; simp [sum_nat_cast] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card + ((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) : by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, sum_nat_cast], } ... = _ : by rw [finset.sum_eq_multiset_sum, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.sum_eq_multiset_sum]; simp [sum_nat_cast]) rfl private lemma eisenstein_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card ≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2, (eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $ by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, sum_nat_cast, add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc] using eq.symm (eisenstein_lemma_aux₁ p hap) lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card := calc a / b = (Ico 1 (a / b).succ).card : by simp ... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card : congr_arg _ $ finset.ext $ λ x, have x * b ≤ a → x ≤ c, from λ h, le_trans (by rwa [le_div_iff_mul_le _ _ hb0]) hc, by simp [lt_succ_iff, le_div_iff_mul_le _ _ hb0]; tauto /-- The given sum is the number of integer points in the triangle formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)` -/ private lemma sum_Ico_eq_card_lt {p q : ℕ} : ∑ a in Ico 1 (p / 2).succ, (a * q) / p = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card := if hp0 : p = 0 then by simp [hp0, finset.ext_iff] else calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p = ∑ a in Ico 1 (p / 2).succ, ((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card : finset.sum_congr rfl $ λ x hx, div_eq_filter_card (nat.pos_of_ne_zero hp0) (calc x * q / p ≤ (p / 2) * q / p : nat.div_le_div_right (mul_le_mul_of_nonneg_right (le_of_lt_succ $ by finish) (nat.zero_le _)) ... ≤ _ : nat.div_mul_div_le_div _ _ _) ... = _ : by rw [← card_sigma]; exact card_congr (λ a _, ⟨a.1, a.2⟩) (by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq, forall_true_iff] {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}⟩) /-- Each of the sums in this lemma is the cardinality of the set integer points in each of the two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them gives the number of points in the rectangle. -/ private lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime] (hq0 : (q : zmod p) ≠ 0) : ∑ a in Ico 1 (p / 2).succ, (a * q) / p + ∑ a in Ico 1 (q / 2).succ, (a * p) / q = (p / 2) * (q / 2) := begin have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card := card_congr (λ x _, prod.swap x) (λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk, forall_true_iff] {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self, exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩), have hdisj : disjoint (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)) (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)), { apply disjoint_filter.2 (λ x hx hpq hqp, _), have hxp : x.1 < p, from lt_of_le_of_lt (show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, Ico.mem, mem_product] at *; tauto) (nat.div_lt_self hp.pos dec_trivial), have : (x.1 : zmod p) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) }, apply_fun zmod.val at this, rw [val_cast_of_lt hxp, val_zero] at this, simpa only [this, nonpos_iff_eq_zero, Ico.mem, one_ne_zero, false_and, mem_product] using hx }, have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪ ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) = ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)), from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q); simp only [mem_union, mem_filter, Ico.mem, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [Ico.card, nat.sub_zero, succ_sub_succ_eq_sub] end variables (p q : ℕ) [fact p.prime] [fact q.prime] namespace zmod /-- The Legendre symbol of `a` and `p` is an integer defined as * `0` if `a` is `0` modulo `p`; * `1` if `a ^ (p / 2)` is `1` modulo `p` (by `euler_criterion` this is equivalent to “`a` is a square modulo `p`”); * `-1` otherwise. -/ def legendre_sym (a p : ℕ) : ℤ := if (a : zmod p) = 0 then 0 else if (a : zmod p) ^ (p / 2) = 1 then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) [hp : fact p.prime] : (legendre_sym a p : zmod p) = (a ^ (p / 2)) := begin rw legendre_sym, by_cases ha : (a : zmod p) = 0, { simp only [if_pos, ha, zero_pow (nat.div_pos (hp.two_le) (succ_pos 1)), int.cast_zero] }, cases hp.eq_two_or_odd with hp2 hp_odd, { substI p, generalize : (a : (zmod 2)) = b, revert b, dec_trivial, }, { change fact (p % 2 = 1) at hp_odd, resetI, rw if_neg ha, have : (-1 : zmod p) ≠ 1, from (ne_neg_self p one_ne_zero).symm, cases pow_div_two_eq_neg_one_or_one p ha with h h, { rw [if_pos h, h, int.cast_one], }, { rw [h, if_neg this, int.cast_neg, int.cast_one], } } end lemma legendre_sym_eq_one_or_neg_one (a p : ℕ) (ha : (a : zmod p) ≠ 0) : legendre_sym a p = -1 ∨ legendre_sym a p = 1 := by unfold legendre_sym; split_ifs; simp only [*, eq_self_iff_true, or_true, true_or] at * lemma legendre_sym_eq_zero_iff (a p : ℕ) : legendre_sym a p = 0 ↔ (a : zmod p) = 0 := begin split, { classical, contrapose, assume ha, cases legendre_sym_eq_one_or_neg_one a p ha with h h, all_goals { rw h, norm_num } }, { assume ha, rw [legendre_sym, if_pos ha] } end /-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less than `p/2` such that `(a * x) % p > p / 2` -/ lemma gauss_lemma {a : ℕ} [hp1 : fact (p % 2 = 1)] (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := have (legendre_sym a p : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p), by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ p ha0]; simp, begin cases legendre_sym_eq_one_or_neg_one a p ha0; cases @neg_one_pow_eq_or ℤ _ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card; simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at * end lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = 1 ↔ (∃ b : zmod p, b ^ 2 = a) := begin rw [euler_criterion p ha0, legendre_sym, if_neg ha0], split_ifs, { simp only [h, eq_self_iff_true] }, finish -- this is quite slow. I'm actually surprised that it can close the goal at all! end lemma eisenstein_lemma [hp1 : fact (p % 2 = 1)] {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p := by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p ha0, neg_one_pow_eq_pow_mod_two, show _ = _, from eisenstein_lemma_aux₂ p ha1 ha0] theorem quadratic_reciprocity [hp1 : fact (p % 2 = 1)] [hq1 : fact (q % 2 = 1)] (hpq : p ≠ q) : legendre_sym p q * legendre_sym q p = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm, have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq, by rw [eisenstein_lemma q hp1 hpq0, eisenstein_lemma p hq1 hqp0, ← pow_add, sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm] -- move this instance fact_prime_two : fact (nat.prime 2) := nat.prime_two lemma legendre_sym_two [hp1 : fact (p % 2 = 1)] : legendre_sym 2 p = (-1) ^ (p / 4 + p / 2) := have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simpa using hp1), have hp22 : p / 2 / 2 = _ := div_eq_filter_card (show 0 < 2, from dec_trivial) (nat.div_le_self (p / 2) 2), have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp, have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x, from λ x hx, have h2xp : 2 * x < p, from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left (le_of_lt_succ $ by finish) dec_trivial ... < _ : by conv_rhs {rw [← mod_add_div p 2, add_comm, show p % 2 = 1, from hp1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)), from disjoint_filter.2 (λ x hx, by simp [hx2 _ hx, mul_comm]), have hunion : ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪ ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) = Ico 1 (p / 2).succ, begin rw [filter_union_right], conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]}, exact filter_congr (λ x hx, by simp [hx2 _ hx, lt_or_le, mul_comm]) end, begin rw [gauss_lemma p (prime_ne_zero p 2 hp2), neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _), rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add, ← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_pow_two_eq_two_iff [hp1 : fact (p % 2 = 1)] : (∃ a : zmod p, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 := have hp2 : ((2 : ℕ) : zmod p) ≠ 0, from prime_ne_zero p 2 (λ h, by simpa [h] using hp1), have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm, have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm, begin rw [show (2 : zmod p) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff p hp2, legendre_sym_two p, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial), even_add, even_div, even_div], have := nat.mod_lt p (show 0 < 8, from dec_trivial), resetI, rw _root_.fact at hp1, revert this hp1, erw [hpm4, hpm2], generalize hm : p % 8 = m, unfreezingI {clear_dependent p}, dec_trivial!, end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) [hq1 : fact (q % 2 = 1)] : (∃ a : zmod p, a ^ 2 = q) ↔ ∃ b : zmod q, b ^ 2 = p := if hpq : p = q then by substI hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from (dvd_iff_mod_eq_zero _ _).1 (dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin haveI hp_odd : fact (p % 2 = 1) := odd_of_mod_four_eq_one hp1, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hqp0, if_neg hpq0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction, end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmod p, a ^ 2 = q) ↔ ¬∃ b : zmod q, b ^ 2 = p := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin haveI hp_odd : fact (p % 2 = 1) := odd_of_mod_four_eq_three hp3, haveI hq_odd : fact (q % 2 = 1) := odd_of_mod_four_eq_three hq3, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hpq0, if_neg hqp0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction end end zmod
beb8d348d8a94b41e3606e5ccf6e362fcea30a27
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/dedekind_domain/integral_closure.lean
357d4b6ec65e08a2865b8034d679e53d29425ab6
[ "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
13,514
lean
/- Copyright (c) 2020 Kenji Nakagawa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenji Nakagawa, Anne Baanen, Filippo A. E. Nuccio -/ import linear_algebra.free_module.pid import ring_theory.dedekind_domain.basic import ring_theory.localization.module import ring_theory.trace /-! # Integral closure of Dedekind domains > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file shows the integral closure of a Dedekind domain (in particular, the ring of integers of a number field) is a Dedekind domain. ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. The `..._iff` lemmas express this independence. Often, definitions assume that Dedekind domains are not fields. We found it more practical to add a `(h : ¬ is_field A)` assumption whenever this is explicitly needed. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic] * [J. Neukirch, *Algebraic Number Theory*][Neukirch1992] ## Tags dedekind domain, dedekind ring -/ variables (R A K : Type*) [comm_ring R] [comm_ring A] [field K] open_locale non_zero_divisors polynomial variables [is_domain A] section is_integral_closure /-! ### `is_integral_closure` section We show that an integral closure of a Dedekind domain in a finite separable field extension is again a Dedekind domain. This implies the ring of integers of a number field is a Dedekind domain. -/ open algebra open_locale big_operators variables (A K) [algebra A K] [is_fraction_ring A K] variables (L : Type*) [field L] (C : Type*) [comm_ring C] variables [algebra K L] [algebra A L] [is_scalar_tower A K L] variables [algebra C L] [is_integral_closure C A L] [algebra A C] [is_scalar_tower A C L] /- If `L` is a separable extension of `K = Frac(A)` and `L` has no zero smul divisors by `A`, then `L` is the localization of the integral closure `C` of `A` in `L` at `A⁰`. -/ lemma is_integral_closure.is_localization [is_separable K L] [no_zero_smul_divisors A L] : is_localization (algebra.algebra_map_submonoid C A⁰) L := begin haveI : is_domain C := (is_integral_closure.equiv A C L (integral_closure A L)).to_ring_equiv.is_domain (integral_closure A L), haveI : no_zero_smul_divisors A C := is_integral_closure.no_zero_smul_divisors A L, refine ⟨_, λ z, _, λ x y, ⟨λ h, ⟨1, _⟩, _⟩⟩, { rintros ⟨_, x, hx, rfl⟩, rw [is_unit_iff_ne_zero, map_ne_zero_iff _ (is_integral_closure.algebra_map_injective C A L), subtype.coe_mk, map_ne_zero_iff _ (no_zero_smul_divisors.algebra_map_injective A C)], exact mem_non_zero_divisors_iff_ne_zero.mp hx, }, { obtain ⟨m, hm⟩ := is_integral.exists_multiple_integral_of_is_localization A⁰ z (is_separable.is_integral K z), obtain ⟨x, hx⟩ : ∃ x, algebra_map C L x = m • z := is_integral_closure.is_integral_iff.mp hm, refine ⟨⟨x, algebra_map A C m, m, set_like.coe_mem m, rfl⟩, _⟩, rw [subtype.coe_mk, ← is_scalar_tower.algebra_map_apply, hx, mul_comm, submonoid.smul_def, smul_def], }, { simp only [is_integral_closure.algebra_map_injective C A L h], }, { rintros ⟨⟨_, m, hm, rfl⟩, h⟩, refine congr_arg (algebra_map C L) ((mul_right_inj' _).mp h), rw [subtype.coe_mk, map_ne_zero_iff _ (no_zero_smul_divisors.algebra_map_injective A C)], exact mem_non_zero_divisors_iff_ne_zero.mp hm, }, end variable [finite_dimensional K L] variables {A K L} lemma is_integral_closure.range_le_span_dual_basis [is_separable K L] {ι : Type*} [fintype ι] [decidable_eq ι] (b : basis ι K L) (hb_int : ∀ i, is_integral A (b i)) [is_integrally_closed A] : ((algebra.linear_map C L).restrict_scalars A).range ≤ submodule.span A (set.range $ (trace_form K L).dual_basis (trace_form_nondegenerate K L) b) := begin let db := (trace_form K L).dual_basis (trace_form_nondegenerate K L) b, rintros _ ⟨x, rfl⟩, simp only [linear_map.coe_restrict_scalars_eq_coe, algebra.linear_map_apply], have hx : is_integral A (algebra_map C L x) := (is_integral_closure.is_integral A L x).algebra_map, rsuffices ⟨c, x_eq⟩ : ∃ (c : ι → A), algebra_map C L x = ∑ i, c i • db i, { rw x_eq, refine submodule.sum_mem _ (λ i _, submodule.smul_mem _ _ (submodule.subset_span _)), rw set.mem_range, exact ⟨i, rfl⟩ }, suffices : ∃ (c : ι → K), ((∀ i, is_integral A (c i)) ∧ algebra_map C L x = ∑ i, c i • db i), { obtain ⟨c, hc, hx⟩ := this, have hc' : ∀ i, is_localization.is_integer A (c i) := λ i, is_integrally_closed.is_integral_iff.mp (hc i), use λ i, classical.some (hc' i), refine hx.trans (finset.sum_congr rfl (λ i _, _)), conv_lhs { rw [← classical.some_spec (hc' i)] }, rw [← is_scalar_tower.algebra_map_smul K (classical.some (hc' i)) (db i)] }, refine ⟨λ i, db.repr (algebra_map C L x) i, (λ i, _), (db.sum_repr _).symm⟩, rw bilin_form.dual_basis_repr_apply, exact is_integral_trace (is_integral_mul hx (hb_int i)) end lemma integral_closure_le_span_dual_basis [is_separable K L] {ι : Type*} [fintype ι] [decidable_eq ι] (b : basis ι K L) (hb_int : ∀ i, is_integral A (b i)) [is_integrally_closed A] : (integral_closure A L).to_submodule ≤ submodule.span A (set.range $ (trace_form K L).dual_basis (trace_form_nondegenerate K L) b) := begin refine le_trans _ (is_integral_closure.range_le_span_dual_basis (integral_closure A L) b hb_int), intros x hx, exact ⟨⟨x, hx⟩, rfl⟩ end variables (A) (K) include K /-- Send a set of `x`'es in a finite extension `L` of the fraction field of `R` to `(y : R) • x ∈ integral_closure R L`. -/ lemma exists_integral_multiples (s : finset L) : ∃ (y ≠ (0 : A)), ∀ x ∈ s, is_integral A (y • x) := begin haveI := classical.dec_eq L, refine s.induction _ _, { use [1, one_ne_zero], rintros x ⟨⟩ }, { rintros x s hx ⟨y, hy, hs⟩, obtain ⟨x', y', hy', hx'⟩ := exists_integral_multiple ((is_fraction_ring.is_algebraic_iff A K L).mpr (is_algebraic_of_finite _ _ x)) ((injective_iff_map_eq_zero (algebra_map A L)).mp _), refine ⟨y * y', mul_ne_zero hy hy', λ x'' hx'', _⟩, rcases finset.mem_insert.mp hx'' with (rfl | hx''), { rw [mul_smul, algebra.smul_def, algebra.smul_def, mul_comm _ x'', hx'], exact is_integral_mul is_integral_algebra_map x'.2 }, { rw [mul_comm, mul_smul, algebra.smul_def], exact is_integral_mul is_integral_algebra_map (hs _ hx'') }, { rw is_scalar_tower.algebra_map_eq A K L, apply (algebra_map K L).injective.comp, exact is_fraction_ring.injective _ _ } } end variables (L) /-- If `L` is a finite extension of `K = Frac(A)`, then `L` has a basis over `A` consisting of integral elements. -/ lemma finite_dimensional.exists_is_basis_integral : ∃ (s : finset L) (b : basis s K L), (∀ x, is_integral A (b x)) := begin letI := classical.dec_eq L, letI : is_noetherian K L := is_noetherian.iff_fg.2 infer_instance, let s' := is_noetherian.finset_basis_index K L, let bs' := is_noetherian.finset_basis K L, obtain ⟨y, hy, his'⟩ := exists_integral_multiples A K (finset.univ.image bs'), have hy' : algebra_map A L y ≠ 0, { refine mt ((injective_iff_map_eq_zero (algebra_map A L)).mp _ _) hy, rw is_scalar_tower.algebra_map_eq A K L, exact (algebra_map K L).injective.comp (is_fraction_ring.injective A K) }, refine ⟨s', bs'.map { to_fun := λ x, algebra_map A L y * x, inv_fun := λ x, (algebra_map A L y)⁻¹ * x, left_inv := _, right_inv := _, .. algebra.lmul _ _ (algebra_map A L y) }, _⟩, { intros x, simp only [inv_mul_cancel_left₀ hy'] }, { intros x, simp only [mul_inv_cancel_left₀ hy'] }, { rintros ⟨x', hx'⟩, simp only [algebra.smul_def, finset.mem_image, exists_prop, finset.mem_univ, true_and] at his', simp only [basis.map_apply, linear_equiv.coe_mk], exact his' _ ⟨_, rfl⟩ } end variables (A K L) [is_separable K L] include L /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is integrally closed and Noetherian, the integral closure `C` of `A` in `L` is Noetherian over `A`. -/ lemma is_integral_closure.is_noetherian [is_integrally_closed A] [is_noetherian_ring A] : is_noetherian A C := begin haveI := classical.dec_eq L, obtain ⟨s, b, hb_int⟩ := finite_dimensional.exists_is_basis_integral A K L, let b' := (trace_form K L).dual_basis (trace_form_nondegenerate K L) b, letI := is_noetherian_span_of_finite A (set.finite_range b'), let f : C →ₗ[A] submodule.span A (set.range b') := (submodule.of_le (is_integral_closure.range_le_span_dual_basis C b hb_int)).comp ((algebra.linear_map C L).restrict_scalars A).range_restrict, refine is_noetherian_of_ker_bot f _, rw [linear_map.ker_comp, submodule.ker_of_le, submodule.comap_bot, linear_map.ker_cod_restrict], exact linear_map.ker_eq_bot_of_injective (is_integral_closure.algebra_map_injective C A L) end /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is integrally closed and Noetherian, the integral closure `C` of `A` in `L` is Noetherian. -/ lemma is_integral_closure.is_noetherian_ring [is_integrally_closed A] [is_noetherian_ring A] : is_noetherian_ring C := is_noetherian_ring_iff.mpr $ is_noetherian_of_tower A (is_integral_closure.is_noetherian A K L C) /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is a principal ring and `L` has no zero smul divisors by `A`, the integral closure `C` of `A` in `L` is a free `A`-module. -/ lemma is_integral_closure.module_free [no_zero_smul_divisors A L] [is_principal_ideal_ring A] : module.free A C := begin haveI : no_zero_smul_divisors A C := is_integral_closure.no_zero_smul_divisors A L, haveI : is_noetherian A C := is_integral_closure.is_noetherian A K L _, exact module.free_of_finite_type_torsion_free', end /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is a principal ring and `L` has no zero smul divisors by `A`, the `A`-rank of the integral closure `C` of `A` in `L` is equal to the `K`-rank of `L`. -/ lemma is_integral_closure.rank [is_principal_ideal_ring A] [no_zero_smul_divisors A L] : finite_dimensional.finrank A C = finite_dimensional.finrank K L := begin haveI : module.free A C := is_integral_closure.module_free A K L C, haveI : is_noetherian A C := is_integral_closure.is_noetherian A K L C, haveI : is_localization (algebra.algebra_map_submonoid C A⁰) L := is_integral_closure.is_localization A K L C, let b := basis.localization_localization K A⁰ L (module.free.choose_basis A C), rw [finite_dimensional.finrank_eq_card_choose_basis_index, finite_dimensional.finrank_eq_card_basis b], end variables {A K} /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is integrally closed and Noetherian, the integral closure of `A` in `L` is Noetherian. -/ lemma integral_closure.is_noetherian_ring [is_integrally_closed A] [is_noetherian_ring A] : is_noetherian_ring (integral_closure A L) := is_integral_closure.is_noetherian_ring A K L (integral_closure A L) variables (A K) [is_domain C] /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is a Dedekind domain, the integral closure `C` of `A` in `L` is a Dedekind domain. Can't be an instance since `A`, `K` or `L` can't be inferred. See also the instance `integral_closure.is_dedekind_domain_fraction_ring` where `K := fraction_ring A` and `C := integral_closure A L`. -/ lemma is_integral_closure.is_dedekind_domain [h : is_dedekind_domain A] : is_dedekind_domain C := begin haveI : is_fraction_ring C L := is_integral_closure.is_fraction_ring_of_finite_extension A K L C, exact ⟨is_integral_closure.is_noetherian_ring A K L C, h.dimension_le_one.is_integral_closure _ L _, (is_integrally_closed_iff L).mpr (λ x hx, ⟨is_integral_closure.mk' C x (is_integral_trans (is_integral_closure.is_integral_algebra A L) _ hx), is_integral_closure.algebra_map_mk' _ _ _⟩)⟩ end /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is a Dedekind domain, the integral closure of `A` in `L` is a Dedekind domain. Can't be an instance since `K` can't be inferred. See also the instance `integral_closure.is_dedekind_domain_fraction_ring` where `K := fraction_ring A`. -/ lemma integral_closure.is_dedekind_domain [h : is_dedekind_domain A] : is_dedekind_domain (integral_closure A L) := is_integral_closure.is_dedekind_domain A K L (integral_closure A L) omit K variables [algebra (fraction_ring A) L] [is_scalar_tower A (fraction_ring A) L] variables [finite_dimensional (fraction_ring A) L] [is_separable (fraction_ring A) L] /- If `L` is a finite separable extension of `Frac(A)`, where `A` is a Dedekind domain, the integral closure of `A` in `L` is a Dedekind domain. See also the lemma `integral_closure.is_dedekind_domain` where you can choose the field of fractions yourself. -/ instance integral_closure.is_dedekind_domain_fraction_ring [is_dedekind_domain A] : is_dedekind_domain (integral_closure A L) := integral_closure.is_dedekind_domain A (fraction_ring A) L end is_integral_closure
3a3ed11f119cf599bcbf5fe49d1b2665a1d5e27e
0d4c30038160d9c35586ce4dace36fe26a35023b
/src/algebra/lie_algebra.lean
5f28951990308ae2f74523cad111be3e25a75892
[ "Apache-2.0" ]
permissive
b-mehta/mathlib
b0c8ec929ec638447e4262f7071570d23db52e14
ce72cde867feabe5bb908cf9e895acc0e11bf1eb
refs/heads/master
1,599,457,264,781
1,586,969,260,000
1,586,969,260,000
220,672,634
0
0
Apache-2.0
1,583,944,480,000
1,573,317,991,000
Lean
UTF-8
Lean
false
false
22,697
lean
/- Copyright (c) 2019 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import ring_theory.algebra data.matrix.basic linear_algebra.linear_action /-! # Lie algebras This file defines Lie rings, and Lie algebras over a commutative ring. It shows how these arise from associative rings and algebras via the ring commutator. In particular it defines the Lie algebra of endomorphisms of a module as well as of the algebra of square matrices over a commutative ring. It also includes definitions of morphisms of Lie algebras, Lie subalgebras, Lie modules, Lie submodules, and the quotient of a Lie algebra by an ideal. ## Notations We introduce the notation ⁅x, y⁆ for the Lie bracket. Note that these are the Unicode "square with quill" brackets rather than the usual square brackets. We also introduce the notations L →ₗ⁅R⁆ L' for a morphism of Lie algebras over a commutative ring R, and L →ₗ⁅⁆ L' for the same, when the ring is implicit. ## Implementation notes Lie algebras are defined as modules with a compatible Lie ring structure, and thus are partially unbundled. Since they extend Lie rings, these are also partially unbundled. ## References * [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*][bourbaki1975] ## Tags lie bracket, ring commutator, jacobi identity, lie ring, lie algebra -/ universes u v w w₁ /-- A binary operation, intended use in Lie algebras and similar structures. -/ class has_bracket (L : Type v) := (bracket : L → L → L) notation `⁅`x`,` y`⁆` := has_bracket.bracket x y namespace ring_commutator variables {A : Type v} [ring A] /-- The ring commutator captures the extent to which a ring is commutative. It is identically zero exactly when the ring is commutative. -/ def commutator (x y : A) := x*y - y*x local notation `⁅`x`,` y`⁆` := commutator x y @[simp] lemma add_left (x y z : A) : ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ := by simp [commutator, right_distrib, left_distrib, sub_eq_add_neg, add_comm, add_left_comm] @[simp] lemma add_right (x y z : A) : ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ := by simp [commutator, right_distrib, left_distrib, sub_eq_add_neg, add_comm, add_left_comm] @[simp] lemma alternate (x : A) : ⁅x, x⁆ = 0 := by simp [commutator] lemma jacobi (x y z : A) : ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0 := begin unfold commutator, repeat { rw mul_sub_left_distrib }, repeat { rw mul_sub_right_distrib }, repeat { rw add_sub }, repeat { rw ←sub_add }, repeat { rw ←mul_assoc }, have h : ∀ (x y z : A), x - y + z + y = x+z := by simp [sub_eq_add_neg, add_left_comm], repeat { rw h }, simp [sub_eq_add_neg, add_left_comm], end end ring_commutator section prio set_option default_priority 100 -- see Note [default priority] /-- A Lie ring is an additive group with compatible product, known as the bracket, satisfying the Jacobi identity. The bracket is not associative unless it is identically zero. -/ class lie_ring (L : Type v) extends add_comm_group L, has_bracket L := (add_lie : ∀ (x y z : L), ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆) (lie_add : ∀ (x y z : L), ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆) (lie_self : ∀ (x : L), ⁅x, x⁆ = 0) (jacobi : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0) end prio section lie_ring variables {L : Type v} [lie_ring L] @[simp] lemma add_lie (x y z : L) : ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ := lie_ring.add_lie x y z @[simp] lemma lie_add (x y z : L) : ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ := lie_ring.lie_add x y z @[simp] lemma lie_self (x : L) : ⁅x, x⁆ = 0 := lie_ring.lie_self x @[simp] lemma lie_skew (x y : L) : -⁅y, x⁆ = ⁅x, y⁆ := begin symmetry, rw [←sub_eq_zero_iff_eq, sub_neg_eq_add], have H : ⁅x + y, x + y⁆ = 0, from lie_self _, rw add_lie at H, simpa using H, end @[simp] lemma lie_zero (x : L) : ⁅x, 0⁆ = 0 := begin have H : ⁅x, 0⁆ + ⁅x, 0⁆ = ⁅x, 0⁆ + 0 := by { rw ←lie_add, simp, }, exact add_left_cancel H, end @[simp] lemma zero_lie (x : L) : ⁅0, x⁆ = 0 := by { rw [←lie_skew, lie_zero], simp, } @[simp] lemma neg_lie (x y : L) : ⁅-x, y⁆ = -⁅x, y⁆ := by { rw [←sub_eq_zero_iff_eq, sub_neg_eq_add, ←add_lie], simp, } @[simp] lemma lie_neg (x y : L) : ⁅x, -y⁆ = -⁅x, y⁆ := by { rw [←lie_skew, ←lie_skew], simp, } @[simp] lemma gsmul_lie (x y : L) (n : ℤ) : ⁅n • x, y⁆ = n • ⁅x, y⁆ := add_monoid_hom.map_gsmul ⟨λ x, ⁅x, y⁆, zero_lie y, λ _ _, add_lie _ _ _⟩ _ _ @[simp] lemma lie_gsmul (x y : L) (n : ℤ) : ⁅x, n • y⁆ = n • ⁅x, y⁆ := begin rw [←lie_skew, ←lie_skew x, gsmul_lie], unfold has_scalar.smul, rw gsmul_neg, end /-- An associative ring gives rise to a Lie ring by taking the bracket to be the ring commutator. -/ def lie_ring.of_associative_ring (A : Type v) [ring A] : lie_ring A := { bracket := ring_commutator.commutator, add_lie := ring_commutator.add_left, lie_add := ring_commutator.add_right, lie_self := ring_commutator.alternate, jacobi := ring_commutator.jacobi } end lie_ring section prio set_option default_priority 100 -- see Note [default priority] /-- A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring. -/ class lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] extends module R L := (lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆) end prio @[simp] lemma lie_smul (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (t : R) (x y : L) : ⁅x, t • y⁆ = t • ⁅x, y⁆ := lie_algebra.lie_smul t x y @[simp] lemma smul_lie (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (t : R) (x y : L) : ⁅t • x, y⁆ = t • ⁅x, y⁆ := by { rw [←lie_skew, ←lie_skew x y], simp [-lie_skew], } namespace lie_algebra set_option old_structure_cmd true /-- A morphism of Lie algebras is a linear map respecting the bracket operations. -/ structure morphism (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends linear_map R L L' := (map_lie : ∀ {x y : L}, to_fun ⁅x, y⁆ = ⁅to_fun x, to_fun y⁆) attribute [nolint doc_blame] lie_algebra.morphism.to_linear_map infixr ` →ₗ⁅⁆ `:25 := morphism _ notation L ` →ₗ⁅`:25 R:25 `⁆ `:0 L':0 := morphism R L L' section morphism_properties variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] instance : has_coe (L₁ →ₗ⁅R⁆ L₂) (L₁ →ₗ[R] L₂) := ⟨morphism.to_linear_map⟩ lemma map_lie (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f ⁅x, y⁆ = ⁅f x, f y⁆ := morphism.map_lie f @[simp] lemma map_lie' (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : (f : L₁ →ₗ[R] L₂) ⁅x, y⁆ = ⁅f x, f y⁆ := morphism.map_lie f /-- The constant 0 map is a Lie algebra morphism. -/ instance : has_zero (L₁ →ₗ⁅R⁆ L₂) := ⟨{ map_lie := by simp, ..(0 : L₁ →ₗ[R] L₂)}⟩ /-- The identity map is a Lie algebra morphism. -/ instance : has_one (L₁ →ₗ⁅R⁆ L₁) := ⟨{ map_lie := by simp, ..(1 : L₁ →ₗ[R] L₁)}⟩ instance : inhabited (L₁ →ₗ⁅R⁆ L₂) := ⟨0⟩ /-- The composition of morphisms is a morphism. -/ def morphism.comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ⁅R⁆ L₃ := { map_lie := λ x y, by { change f (g ⁅x, y⁆) = ⁅f (g x), f (g y)⁆, rw [map_lie, map_lie], }, ..linear_map.comp f.to_linear_map g.to_linear_map } lemma morphism.comp_apply (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) (x : L₁) : f.comp g x = f (g x) := rfl /-- The inverse of a bijective morphism is a morphism. -/ def morphism.inverse (f : L₁ →ₗ⁅R⁆ L₂) (g : L₂ → L₁) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : L₂ →ₗ⁅R⁆ L₁ := { map_lie := λ x y, by { calc g ⁅x, y⁆ = g ⁅f (g x), f (g y)⁆ : by { conv_lhs { rw [←h₂ x, ←h₂ y], }, } ... = g (f ⁅g x, g y⁆) : by rw map_lie ... = ⁅g x, g y⁆ : (h₁ _), }, ..linear_map.inverse f.to_linear_map g h₁ h₂ } end morphism_properties /-- An equivalence of Lie algebras is a morphism which is also a linear equivalence. We could instead define an equivalence to be a morphism which is also a (plain) equivalence. However it is more convenient to define via linear equivalence to get `.to_linear_equiv` for free. -/ structure equiv (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends L →ₗ⁅R⁆ L', L ≃ₗ[R] L' attribute [nolint doc_blame] lie_algebra.equiv.to_morphism attribute [nolint doc_blame] lie_algebra.equiv.to_linear_equiv notation L ` ≃ₗ⁅`:50 R `⁆ ` L' := equiv R L L' namespace equiv variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] instance : has_one (L₁ ≃ₗ⁅R⁆ L₁) := ⟨{ map_lie := λ x y, by { change ((1 : L₁→ₗ[R] L₁) ⁅x, y⁆) = ⁅(1 : L₁→ₗ[R] L₁) x, (1 : L₁→ₗ[R] L₁) y⁆, simp, }, ..(1 : L₁ ≃ₗ[R] L₁)}⟩ instance : inhabited (L₁ ≃ₗ⁅R⁆ L₁) := ⟨1⟩ /-- Lie algebra equivalences are reflexive. -/ @[refl] def refl : L₁ ≃ₗ⁅R⁆ L₁ := 1 /-- Lie algebra equivalences are symmetric. -/ @[symm] def symm (e : L₁ ≃ₗ⁅R⁆ L₂) : L₂ ≃ₗ⁅R⁆ L₁ := { ..morphism.inverse e.to_morphism e.inv_fun e.left_inv e.right_inv, ..e.to_linear_equiv.symm } /-- Lie algebra equivalences are transitive. -/ @[trans] def trans (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) : L₁ ≃ₗ⁅R⁆ L₃ := { ..morphism.comp e₂.to_morphism e₁.to_morphism, ..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv } end equiv namespace direct_sum open dfinsupp variables {R : Type u} [comm_ring R] variables {ι : Type v} [decidable_eq ι] {L : ι → Type w} variables [Π i, lie_ring (L i)] [Π i, lie_algebra R (L i)] /-- The direct sum of Lie rings carries a natural Lie ring structure. -/ instance : lie_ring (direct_sum ι L) := { bracket := zip_with (λ i, λ x y, ⁅x, y⁆) (λ i, lie_zero 0), add_lie := λ x y z, by { ext, simp only [zip_with_apply, add_apply, add_lie], }, lie_add := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_add], }, lie_self := λ x, by { ext, simp only [zip_with_apply, add_apply, lie_self, zero_apply], }, jacobi := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_ring.jacobi, zero_apply], }, ..(infer_instance : add_comm_group _) } @[simp] lemma bracket_apply {x y : direct_sum ι L} {i : ι} : ⁅x, y⁆ i = ⁅x i, y i⁆ := zip_with_apply /-- The direct sum of Lie algebras carries a natural Lie algebra structure. -/ instance : lie_algebra R (direct_sum ι L) := { lie_smul := λ c x y, by { ext, simp only [zip_with_apply, smul_apply, bracket_apply, lie_smul], }, ..(infer_instance : module R _) } end direct_sum variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] /-- An associative algebra gives rise to a Lie algebra by taking the bracket to be the ring commutator. -/ def of_associative_algebra (A : Type v) [ring A] [algebra R A] : @lie_algebra R A _ (lie_ring.of_associative_ring _) := { lie_smul := begin intros, show _ - _ = _ • (_ - _), rw [algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_sub], end } instance (M : Type v) [add_comm_group M] [module R M] : lie_ring (module.End R M) := lie_ring.of_associative_ring _ /-- An important class of Lie algebras are those arising from the associative algebra structure on module endomorphisms. -/ instance of_endomorphism_algebra (M : Type v) [add_comm_group M] [module R M] : lie_algebra R (module.End R M) := of_associative_algebra (module.End R M) lemma endo_algebra_bracket (M : Type v) [add_comm_group M] [module R M] (f g : module.End R M) : ⁅f, g⁆ = f.comp g - g.comp f := rfl /-- The adjoint action of a Lie algebra on itself. -/ def Ad : L →ₗ⁅R⁆ module.End R L := { to_fun := λ x, { to_fun := has_bracket.bracket x, add := by { intros, apply lie_add, }, smul := by { intros, apply lie_smul, } }, add := by { intros, ext, simp, }, smul := by { intros, ext, simp, }, map_lie := by { intros x y, ext z, rw endo_algebra_bracket, suffices : ⁅⁅x, y⁆, z⁆ = ⁅x, ⁅y, z⁆⁆ + ⁅⁅x, z⁆, y⁆, by simpa [sub_eq_add_neg], rw [eq_comm, ←lie_skew ⁅x, y⁆ z, ←lie_skew ⁅x, z⁆ y, ←lie_skew x z, lie_neg, neg_neg, ←sub_eq_zero_iff_eq, sub_neg_eq_add, lie_ring.jacobi], } } end lie_algebra section lie_subalgebra variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] /-- A Lie subalgebra of a Lie algebra is submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie algebra. -/ structure lie_subalgebra extends submodule R L := (lie_mem : ∀ {x y}, x ∈ carrier → y ∈ carrier → ⁅x, y⁆ ∈ carrier) instance lie_subalgebra_coe_submodule : has_coe (lie_subalgebra R L) (submodule R L) := ⟨lie_subalgebra.to_submodule⟩ /-- A Lie subalgebra forms a new Lie ring. This cannot be an instance, since being a Lie subalgebra is (currently) not a class. -/ def lie_subalgebra_lie_ring (L' : lie_subalgebra R L) : lie_ring L' := { bracket := λ x y, ⟨⁅x.val, y.val⁆, L'.lie_mem x.property y.property⟩, lie_add := by { intros, apply set_coe.ext, apply lie_add, }, add_lie := by { intros, apply set_coe.ext, apply add_lie, }, lie_self := by { intros, apply set_coe.ext, apply lie_self, }, jacobi := by { intros, apply set_coe.ext, apply lie_ring.jacobi, } } /-- A Lie subalgebra forms a new Lie algebra. This cannot be an instance, since being a Lie subalgebra is (currently) not a class. -/ def lie_subalgebra_lie_algebra (L' : lie_subalgebra R L) : @lie_algebra R L' _ (lie_subalgebra_lie_ring _ _ _) := { lie_smul := by { intros, apply set_coe.ext, apply lie_smul } } end lie_subalgebra section lie_module variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] variables (M : Type v) [add_comm_group M] [module R M] section prio set_option default_priority 100 -- see Note [default priority] /-- A Lie module is a module over a commutative ring, together with a linear action of a Lie algebra on this module, such that the Lie bracket acts as the commutator of endomorphisms. -/ class lie_module extends linear_action R L M := (lie_act : ∀ (l l' : L) (m : M), act ⁅l, l'⁆ m = act l (act l' m) - act l' (act l m)) end prio @[simp] lemma lie_act [lie_module R L M] (l l' : L) (m : M) : linear_action.act R ⁅l, l'⁆ m = linear_action.act R l (linear_action.act R l' m) - linear_action.act R l' (linear_action.act R l m) := lie_module.lie_act l l' m protected lemma of_endo_map_action (α : L →ₗ⁅R⁆ module.End R M) (x : L) (m : M) : @linear_action.act R _ _ _ _ _ _ _ (linear_action.of_endo_map R L M α) x m = α x m := rfl /-- A Lie morphism from a Lie algebra to the endomorphism algebra of a module yields a Lie module structure. -/ def lie_module.of_endo_morphism (α : L →ₗ⁅R⁆ module.End R M) : lie_module R L M := { lie_act := by { intros x y m, rw [of_endo_map_action, lie_algebra.map_lie, lie_algebra.endo_algebra_bracket], refl, }, ..(linear_action.of_endo_map R L M α) } /-- Every Lie algebra is a module over itself. -/ instance lie_algebra_self_module : lie_module R L L := lie_module.of_endo_morphism R L L lie_algebra.Ad /-- A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie module. -/ structure lie_submodule [lie_module R L M] extends submodule R M := (lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → linear_action.act R x m ∈ carrier) instance lie_submodule_coe_submodule [lie_module R L M] : has_coe (lie_submodule R L M) (submodule R M) := ⟨lie_submodule.to_submodule⟩ instance lie_submodule_has_mem [lie_module R L M] : has_mem M (lie_submodule R L M) := ⟨λ x N, x ∈ (N : set M)⟩ instance lie_submodule_lie_module [lie_module R L M] (N : lie_submodule R L M) : lie_module R L N := { act := λ x m, ⟨linear_action.act R x m.val, N.lie_mem m.property⟩, add_act := by { intros x y m, apply set_coe.ext, apply linear_action.add_act, }, act_add := by { intros x m n, apply set_coe.ext, apply linear_action.act_add, }, act_smul := by { intros r x y, apply set_coe.ext, apply linear_action.act_smul, }, smul_act := by { intros r x y, apply set_coe.ext, apply linear_action.smul_act, }, lie_act := by { intros x y m, apply set_coe.ext, apply lie_module.lie_act, } } /-- An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself. -/ abbreviation lie_ideal := lie_submodule R L L lemma lie_mem_right (I : lie_ideal R L) (x y : L) (h : y ∈ I) : ⁅x, y⁆ ∈ I := I.lie_mem h lemma lie_mem_left (I : lie_ideal R L) (x y : L) (h : x ∈ I) : ⁅x, y⁆ ∈ I := by { rw [←lie_skew, ←neg_lie], apply lie_mem_right, assumption, } /-- An ideal of a Lie algebra is a Lie subalgebra. -/ def lie_ideal_subalgebra (I : lie_ideal R L) : lie_subalgebra R L := { lie_mem := by { intros x y hx hy, apply lie_mem_right, exact hy, }, ..I.to_submodule, } end lie_module namespace lie_submodule variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] variables {M : Type v} [add_comm_group M] [module R M] [α : lie_module R L M] variables (N : lie_submodule R L M) (I : lie_ideal R L) /-- The quotient of a Lie module by a Lie submodule. It is a Lie module. -/ abbreviation quotient := N.to_submodule.quotient namespace quotient variables {N I} /-- Map sending an element of `M` to the corresponding element of `M/N`, when `N` is a lie_submodule of the lie_module `N`. -/ abbreviation mk : M → N.quotient := submodule.quotient.mk lemma is_quotient_mk (m : M) : quotient.mk' m = (mk m : N.quotient) := rfl /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural linear map from `L` to the endomorphisms of `M` leaving `N` invariant. -/ def lie_submodule_invariant : L →ₗ[R] submodule.compatible_maps N.to_submodule N.to_submodule := linear_map.cod_restrict _ (α.to_linear_action.to_endo_map _ _ _) N.lie_mem instance lie_quotient_action : linear_action R L N.quotient := linear_action.of_endo_map _ _ _ (linear_map.comp (submodule.mapq_linear N N) lie_submodule_invariant) lemma lie_quotient_action_apply (z : L) (m : M) : linear_action.act R z (mk m : N.quotient) = mk (linear_action.act R z m) := rfl /-- The quotient of a Lie module by a Lie submodule, is a Lie module. -/ instance lie_quotient_lie_module : lie_module R L N.quotient := { lie_act := λ x y m', by { apply quotient.induction_on' m', intros m, rw is_quotient_mk, repeat { rw lie_quotient_action_apply, }, rw lie_act, refl, }, ..quotient.lie_quotient_action, } instance lie_quotient_has_bracket : has_bracket (quotient I) := ⟨by { intros x y, apply quotient.lift_on₂' x y (λ x' y', mk ⁅x', y'⁆), intros x₁ x₂ y₁ y₂ h₁ h₂, apply (submodule.quotient.eq I.to_submodule).2, have h : ⁅x₁, x₂⁆ - ⁅y₁, y₂⁆ = ⁅x₁, x₂ - y₂⁆ + ⁅x₁ - y₁, y₂⁆ := by simp [-lie_skew, sub_eq_add_neg], rw h, apply submodule.add_mem, { apply lie_mem_right R L I x₁ (x₂ - y₂) h₂, }, { apply lie_mem_left R L I (x₁ - y₁) y₂ h₁, }, }⟩ @[simp] lemma mk_bracket (x y : L) : (mk ⁅x, y⁆ : quotient I) = ⁅mk x, mk y⁆ := rfl instance lie_quotient_lie_ring : lie_ring (quotient I) := { add_lie := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply add_lie, }, lie_add := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply lie_add, }, lie_self := by { intros x', apply quotient.induction_on' x', intros x, rw [is_quotient_mk, ←mk_bracket], apply congr_arg, apply lie_self, }, jacobi := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply lie_ring.jacobi, } } instance lie_quotient_lie_algebra : lie_algebra R (quotient I) := { lie_smul := by { intros t x' y', apply quotient.induction_on₂' x' y', intros x y, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_smul, }, apply congr_arg, apply lie_smul, } } end quotient end lie_submodule /-- An important class of Lie rings are those arising from the associative algebra structure on square matrices over a commutative ring. -/ def matrix.lie_ring (n : Type u) (R : Type v) [fintype n] [decidable_eq n] [comm_ring R] : lie_ring (matrix n n R) := lie_ring.of_associative_ring (matrix n n R) local attribute [instance] matrix.lie_ring /-- An important class of Lie algebras are those arising from the associative algebra structure on square matrices over a commutative ring. -/ def matrix.lie_algebra (n : Type u) (R : Type v) [fintype n] [decidable_eq n] [comm_ring R] : lie_algebra R (matrix n n R) := lie_algebra.of_associative_algebra (matrix n n R)
642ef01da6ba77ec9ec4f85ef39d5f77b662718f
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Data/Nat.lean
cf7be93c6e838845fae022378fe6f8d85baddf0a
[ "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
282
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Nat.Basic import Init.Data.Nat.Div import Init.Data.Nat.Bitwise import Init.Data.Nat.Control
fa280efe3bf724ae444b8dc4776766c24ac31beb
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/analysis/ennreal.lean
8fa4186bc307621c9047a604b44f7018af0bc228
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
46,588
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 Extended non-negative reals TODO: base ennreal on nnreal! -/ import order.bounds algebra.ordered_group analysis.nnreal analysis.topology.infinite_sum noncomputable theory open classical set lattice filter local attribute [instance] prop_decidable variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ inductive ennreal : Type | of_nonneg_real : Πr:real, 0 ≤ r → ennreal | infinity : ennreal local notation `∞` := ennreal.infinity namespace ennreal variables {a b c d : ennreal} {r p q : ℝ} section projections /-- `of_real r` is the nonnegative extended real number `r` if `r` is nonnegative, otherwise 0. -/ def of_real (r : ℝ) : ennreal := of_nonneg_real (max 0 r) (le_max_left 0 r) /-- `of_ennreal x` returns `x` if it is real, otherwise 0. -/ def of_ennreal : ennreal → ℝ | (of_nonneg_real r _) := r | ∞ := 0 @[simp] lemma of_ennreal_of_real (h : 0 ≤ r) : of_ennreal (of_real r) = r := max_eq_right h lemma zero_le_of_ennreal : ∀{a}, 0 ≤ of_ennreal a | (of_nonneg_real r hr) := hr | ∞ := le_refl 0 @[simp] lemma of_real_of_ennreal : ∀{a}, a ≠ ∞ → of_real (of_ennreal a) = a | (of_nonneg_real r hr) h := by simp [of_real, of_ennreal, max, hr] | ∞ h := false.elim $ h rfl lemma forall_ennreal {p : ennreal → Prop} : (∀a, p a) ↔ (∀r (h : 0 ≤ r), p (of_real r)) ∧ p ∞ := ⟨assume h, ⟨assume r hr, h _, h _⟩, assume ⟨h₁, h₂⟩, ennreal.rec begin intros r hr, let h₁ := h₁ r hr, simp [of_real, max, hr] at h₁, exact h₁ end h₂⟩ end projections section semiring instance : has_zero ennreal := ⟨of_real 0⟩ instance : has_one ennreal := ⟨of_real 1⟩ instance : inhabited ennreal := ⟨0⟩ @[simp] lemma of_real_zero : of_real 0 = 0 := rfl @[simp] lemma of_real_one : of_real 1 = 1 := rfl @[simp] lemma zero_ne_infty : 0 ≠ ∞ := assume h, ennreal.no_confusion h @[simp] lemma infty_ne_zero : ∞ ≠ 0 := assume h, ennreal.no_confusion h @[simp] lemma of_real_ne_infty : of_real r ≠ ∞ := assume h, ennreal.no_confusion h @[simp] lemma infty_ne_of_real : ∞ ≠ of_real r := assume h, ennreal.no_confusion h @[simp] lemma of_real_eq_of_real_of (hr : 0 ≤ r) (hq : 0 ≤ q) : of_real r = of_real q ↔ r = q := by simp [of_real, max, hr, hq]; exact ⟨ennreal.of_nonneg_real.inj, by simp {contextual := tt}⟩ lemma of_real_ne_of_real_of (hr : 0 ≤ r) (hq : 0 ≤ q) : of_real r ≠ of_real q ↔ r ≠ q := by simp [hr, hq] lemma of_real_of_nonpos (hr : r ≤ 0) : of_real r = 0 := have ∀r₁ r₂ : real, r₁ = r₂ → ∀h₁:0≤r₁, ∀h₂:0≤r₂, of_nonneg_real r₁ h₁ = of_nonneg_real r₂ h₂, from assume r₁ r₂ h, match r₁, r₂, h with _, _, rfl := assume _ _, rfl end, this _ _ (by simp [hr, max_eq_left]) _ _ lemma of_real_of_not_nonneg (hr : ¬ 0 ≤ r) : of_real r = 0 := of_real_of_nonpos $ le_of_lt $ lt_of_not_ge hr instance : zero_ne_one_class ennreal := { zero := 0, one := 1, zero_ne_one := (of_real_ne_of_real_of (le_refl 0) zero_le_one).mpr zero_ne_one } @[simp] lemma of_real_eq_zero_iff (hr : 0 ≤ r) : of_real r = 0 ↔ r = 0 := of_real_eq_of_real_of hr (le_refl 0) @[simp] lemma zero_eq_of_real_iff (hr : 0 ≤ r) : 0 = of_real r ↔ 0 = r := of_real_eq_of_real_of (le_refl 0) hr @[simp] lemma of_real_eq_one_iff : of_real r = 1 ↔ r = 1 := match le_total 0 r with | or.inl h := of_real_eq_of_real_of h zero_le_one | or.inr h := have r ≠ 1, from assume h', lt_irrefl (0:ℝ) $ lt_of_lt_of_le (by rw [h']; exact zero_lt_one) h, by simp [of_real_of_nonpos h, this] end @[simp] lemma one_eq_of_real_iff : 1 = of_real r ↔ 1 = r := by rw [eq_comm, of_real_eq_one_iff, eq_comm] lemma of_nonneg_real_eq_of_real (hr : 0 ≤ r) : of_nonneg_real r hr = of_real r := by simp [of_real, hr, max] protected def add : ennreal → ennreal → ennreal | (of_nonneg_real a ha) (of_nonneg_real b hb) := of_real (a + b) | _ _ := ∞ protected def mul : ennreal → ennreal → ennreal | (of_nonneg_real a ha) (of_nonneg_real b hb) := of_real (a * b) | ∞ (of_nonneg_real b hb) := if b = 0 then 0 else ∞ | (of_nonneg_real a ha) ∞ := if a = 0 then 0 else ∞ | _ _ := ∞ instance : has_add ennreal := ⟨ennreal.add⟩ instance : has_mul ennreal := ⟨ennreal.mul⟩ @[simp] lemma of_real_add (hr : 0 ≤ r) (hq : 0 ≤ p) : of_real r + of_real p = of_real (r + p) := by simp [of_real, max, hr, hq, add_comm]; refl @[simp] lemma add_infty : a + ∞ = ∞ := by cases a; refl @[simp] lemma infty_add : ∞ + a = ∞ := by cases a; refl @[simp] lemma of_real_mul_of_real (hr : 0 ≤ r) (hq : 0 ≤ p) : of_real r * of_real p = of_real (r * p) := by simp [of_real, max, hr, hq]; refl @[simp] lemma of_real_mul_infty (hr : 0 ≤ r) : of_real r * ∞ = (if r = 0 then 0 else ∞) := by simp [of_real, max, hr]; refl @[simp] lemma infty_mul_of_real (hr : 0 ≤ r) : ∞ * of_real r = (if r = 0 then 0 else ∞) := by simp [of_real, max, hr]; refl @[simp] lemma mul_infty : ∀{a}, a * ∞ = (if a = 0 then 0 else ∞) := forall_ennreal.mpr ⟨assume r hr, by simp [hr]; by_cases r = 0; simp [h], by simp; refl⟩ @[simp] lemma infty_mul : ∀{a}, ∞ * a = (if a = 0 then 0 else ∞) := forall_ennreal.mpr ⟨assume r hr, by simp [hr]; by_cases r = 0; simp [h], by simp; refl⟩ instance : add_comm_monoid ennreal := { zero := 0, add := (+), add_zero := forall_ennreal.2 ⟨λ a ha, by rw [← of_real_zero, of_real_add ha (le_refl _), add_zero], by simp⟩, zero_add := forall_ennreal.2 ⟨λ a ha, by rw [← of_real_zero, of_real_add (le_refl _) ha, zero_add], by simp⟩, add_comm := begin refine forall_ennreal.2 ⟨λ a ha, _, by simp⟩, refine forall_ennreal.2 ⟨λ b hb, _, by simp⟩, rw [of_real_add ha hb, of_real_add hb ha, add_comm] end, add_assoc := begin refine forall_ennreal.2 ⟨λ a ha, _, by simp⟩, refine forall_ennreal.2 ⟨λ b hb, _, by simp⟩, refine forall_ennreal.2 ⟨λ c hc, _, by simp⟩, rw [of_real_add ha hb, of_real_add (add_nonneg ha hb) hc, of_real_add hb hc, of_real_add ha (add_nonneg hb hc), add_assoc], end } @[simp] lemma sum_of_real {α : Type*} {s : finset α} {f : α → ℝ} : (∀a∈s, 0 ≤ f a) → s.sum (λa, of_real (f a)) = of_real (s.sum f) := finset.induction_on s (by simp) $ assume a s has ih h, have 0 ≤ s.sum f, from finset.zero_le_sum $ assume a ha, h a $ finset.mem_insert_of_mem ha, by simp [has, *] {contextual := tt} protected lemma mul_zero : ∀a:ennreal, a * 0 = 0 := by simp [forall_ennreal, -of_real_zero, of_real_zero.symm] {contextual := tt} protected lemma mul_comm : ∀a b:ennreal, a * b = b * a := by simp [forall_ennreal, mul_comm] {contextual := tt} protected lemma zero_mul : ∀a:ennreal, 0 * a = 0 := by simp [forall_ennreal, -of_real_zero, of_real_zero.symm] {contextual := tt} protected lemma mul_assoc : ∀a b c:ennreal, a * b * c = a * (b * c) := begin rw [forall_ennreal], constructor, { intros ra ha, by_cases ha' : ra = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], rw [forall_ennreal], constructor, { intros rb hrb, by_cases hb' : rb = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], rw [forall_ennreal], constructor, { intros rc hrc, simp [*, zero_le_mul, mul_assoc] }, simp [*, zero_le_mul, mul_eq_zero_iff_eq_zero_or_eq_zero] }, rw [forall_ennreal], constructor, { intros rc hrc, by_cases hc' : rc = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], simp [*, zero_le_mul] }, simp [*] }, rw [forall_ennreal], constructor, { intros rb hrb, by_cases hb' : rb = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], rw [forall_ennreal], constructor, { intros rc hrc, by_cases hb' : rc = 0; simp [*, zero_le_mul, ennreal.mul_zero, mul_eq_zero_iff_eq_zero_or_eq_zero] }, simp [*, zero_le_mul, mul_eq_zero_iff_eq_zero_or_eq_zero] }, intro c, by_cases c = 0; simp * end protected lemma left_distrib : ∀a b c:ennreal, a * (b + c) = a * b + a * c := begin rw [forall_ennreal], constructor, { intros ra ha, by_cases ha' : ra = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], rw [forall_ennreal], constructor, { intros rb hrb, by_cases hb' : rb = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], rw [forall_ennreal], constructor, { intros rc hrc, simp [*, zero_le_mul, add_nonneg, left_distrib] }, simp [*, zero_le_mul, mul_eq_zero_iff_eq_zero_or_eq_zero] }, rw [forall_ennreal], constructor, { intros rc hrc, by_cases hv' : rc = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], simp [*, zero_le_mul] }, simp [*] }, rw [forall_ennreal], constructor, { intros rb hrb, by_cases hb' : rb = 0, simp [*, ennreal.mul_zero, ennreal.zero_mul], rw [forall_ennreal], constructor, { intros rc hrc, by_cases hb' : rc = 0; simp [*, zero_le_mul, ennreal.mul_zero, mul_eq_zero_iff_eq_zero_or_eq_zero, add_nonneg, add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg] }, simp [*, zero_le_mul, mul_eq_zero_iff_eq_zero_or_eq_zero] }, intro c, by_cases c = 0; simp [*] end instance : comm_semiring ennreal := { one := 1, mul := (*), mul_zero := ennreal.mul_zero, zero_mul := ennreal.zero_mul, one_mul := by simp [forall_ennreal, -of_real_one, of_real_one.symm, zero_le_one] {contextual := tt}, mul_one := by simp [forall_ennreal, -of_real_one, of_real_one.symm, zero_le_one] {contextual := tt}, mul_comm := ennreal.mul_comm, mul_assoc := ennreal.mul_assoc, left_distrib := ennreal.left_distrib, right_distrib := assume a b c, by rw [ennreal.mul_comm, ennreal.left_distrib, ennreal.mul_comm, ennreal.mul_comm b c]; refl, ..ennreal.add_comm_monoid } end semiring section order instance : has_le ennreal := ⟨λ a b, b = ∞ ∨ (∃r p, 0 ≤ r ∧ r ≤ p ∧ a = of_real r ∧ b = of_real p)⟩ theorem le_def : a ≤ b ↔ b = ∞ ∨ (∃r p, 0 ≤ r ∧ r ≤ p ∧ a = of_real r ∧ b = of_real p) := iff.rfl @[simp] lemma infty_le_iff : ∞ ≤ a ↔ a = ∞ := by simp [le_def] @[simp] lemma le_infty : a ≤ ∞ := by simp [le_def] @[simp] lemma of_real_le_of_real_iff (hr : 0 ≤ r) (hp : 0 ≤ p) : of_real r ≤ of_real p ↔ r ≤ p := by simpa [le_def] using show (∃ (r' : ℝ), 0 ≤ r' ∧ ∃ (q : ℝ), r' ≤ q ∧ of_real r = of_real r' ∧ of_real p = of_real q) ↔ r ≤ p, from ⟨λ ⟨r', hr', q, hrq, h₁, h₂⟩, by simp [hr, hr', le_trans hr' hrq, hp] at h₁ h₂; simp *, λ h, ⟨r, hr, p, h, rfl, rfl⟩⟩ @[simp] lemma one_le_of_real_iff (hr : 0 ≤ r) : 1 ≤ of_real r ↔ 1 ≤ r := of_real_le_of_real_iff zero_le_one hr instance : decidable_linear_order ennreal := { le := (≤), le_refl := by simp [forall_ennreal, le_refl] {contextual := tt}, le_trans := by simp [forall_ennreal] {contextual := tt}; exact assume a ha b hb c hc, le_trans, le_antisymm := by simp [forall_ennreal] {contextual := tt}; exact assume a ha b hb, le_antisymm, le_total := by simp [forall_ennreal] {contextual := tt}; exact assume a ha b hb, le_total _ _, decidable_le := by apply_instance } @[simp] lemma not_infty_lt : ¬ ∞ < a := by simp @[simp] lemma of_real_lt_infty : of_real r < ∞ := ⟨le_infty, assume h, ennreal.no_confusion $ infty_le_iff.mp h⟩ lemma le_of_real_iff (hr : 0 ≤ r) : ∀{a}, a ≤ of_real r ↔ (∃p, 0 ≤ p ∧ p ≤ r ∧ a = of_real p) := have ∀p, 0 ≤ p → (of_real p ≤ of_real r ↔ ∃ (q : ℝ), 0 ≤ q ∧ q ≤ r ∧ of_real p = of_real q), from assume p hp, ⟨assume h, ⟨p, hp, (of_real_le_of_real_iff hp hr).mp h, rfl⟩, assume ⟨q, hq, hqr, heq⟩, calc of_real p = of_real q : heq ... ≤ _ : (of_real_le_of_real_iff hq hr).mpr hqr⟩, forall_ennreal.mpr $ ⟨this, by simp⟩ @[simp] lemma of_real_lt_of_real_iff : 0 ≤ r → 0 ≤ p → (of_real r < of_real p ↔ r < p) := by simp [lt_iff_le_not_le, -not_le] {contextual:=tt} lemma lt_iff_exists_of_real : ∀{a b}, a < b ↔ (∃p, 0 ≤ p ∧ a = of_real p ∧ of_real p < b) := by simp [forall_ennreal]; exact λ r hr, ⟨λ p hp, ⟨λ h, ⟨r, by simp *⟩, λ ⟨q, h₁, h₂, h₃⟩, by simp * at *⟩, r, hr, rfl⟩ @[simp] protected lemma zero_le : ∀{a:ennreal}, 0 ≤ a := by simp [forall_ennreal, -of_real_zero, of_real_zero.symm] {contextual:=tt} @[simp] lemma le_zero_iff_eq : a ≤ 0 ↔ a = 0 := ⟨assume h, le_antisymm h ennreal.zero_le, assume h, h ▸ le_refl a⟩ @[simp] lemma zero_lt_of_real_iff : 0 < of_real p ↔ 0 < p := by_cases (assume : 0 ≤ p, of_real_lt_of_real_iff (le_refl _) this) (by simp [lt_irrefl, not_imp_not, le_of_lt, of_real_of_not_nonneg] {contextual := tt}) @[simp] lemma not_lt_zero : ¬ a < 0 := by simp protected lemma zero_lt_one : 0 < (1 : ennreal) := zero_lt_of_real_iff.mpr zero_lt_one lemma of_real_le_of_real (h : r ≤ p) : of_real r ≤ of_real p := match le_total 0 r with | or.inl hr := (of_real_le_of_real_iff hr $ le_trans hr h).mpr h | or.inr hr := by simp [of_real_of_nonpos, hr, zero_le] end lemma of_real_lt_of_real_iff_cases : of_real r < of_real p ↔ 0 < p ∧ r < p := begin by_cases hp : 0 ≤ p, { by_cases hr : 0 ≤ r, { simp [*, iff_def] {contextual := tt}, show r < p → 0 < p, from lt_of_le_of_lt hr }, { have h : r ≤ 0, from le_of_lt (lt_of_not_ge hr), simp [*, of_real_of_not_nonneg, and_iff_left_of_imp (lt_of_le_of_lt h)] } }, simp [*, not_le, not_lt, le_of_lt, of_real_of_not_nonneg, and_comm] at * end instance : densely_ordered ennreal := ⟨by simp [forall_ennreal, of_real_lt_of_real_iff_cases]; exact λ r hr, ⟨λ p _ _ h, let ⟨q, h₁, h₂⟩ := dense h in have 0 ≤ q, from le_trans hr $ le_of_lt h₁, ⟨of_real q, by simp *⟩, of_real (r + 1), by simp [hr, add_nonneg, lt_add_of_le_of_pos, zero_le_one, zero_lt_one]⟩⟩ private lemma add_le_add : ∀{b d}, a ≤ b → c ≤ d → a + c ≤ b + d := forall_ennreal.mpr ⟨assume r hr, forall_ennreal.mpr ⟨assume p hp, by simp [le_of_real_iff, *, exists_imp_distrib, -and_imp] {contextual:=tt}; simp [*, add_nonneg, add_le_add] {contextual := tt}, by simp⟩, by simp⟩ private lemma lt_of_add_lt_add_left (h : a + b < a + c) : b < c := lt_of_not_ge $ assume h', lt_irrefl (a + b) (lt_of_lt_of_le h $ add_le_add (le_refl a) h') instance : ordered_comm_monoid ennreal := { add_le_add_left := assume a b h c, add_le_add (le_refl c) h, lt_of_add_lt_add_left := assume a b c, lt_of_add_lt_add_left, ..ennreal.add_comm_monoid, ..ennreal.decidable_linear_order } lemma le_add_left (h : a ≤ c) : a ≤ b + c := calc a = 0 + a : by simp ... ≤ b + c : add_le_add ennreal.zero_le h lemma le_add_right (h : a ≤ b) : a ≤ b + c := calc a = a + 0 : by simp ... ≤ b + c : add_le_add h ennreal.zero_le lemma lt_add_right : ∀{a b}, a < ∞ → 0 < b → a < a + b := by simp [forall_ennreal, of_real_lt_of_real_iff, add_nonneg, lt_add_of_le_of_pos] {contextual := tt} instance : canonically_ordered_monoid ennreal := { le_iff_exists_add := by simp [forall_ennreal] {contextual:=tt}; exact λ r hr, ⟨λ p hp, ⟨λ h, ⟨of_real (p - r), by rw [of_real_add (sub_nonneg.2 h) hr, sub_add_cancel]⟩, λ ⟨c, hc⟩, by rw [← of_real_le_of_real_iff hr hp, hc]; exact le_add_left (le_refl _)⟩, ⟨∞, by simp⟩⟩, ..ennreal.ordered_comm_monoid } lemma mul_le_mul : ∀{b d}, a ≤ b → c ≤ d → a * c ≤ b * d := forall_ennreal.mpr ⟨assume r hr, forall_ennreal.mpr ⟨assume p hp, by simp [le_of_real_iff, *, exists_imp_distrib, -and_imp] {contextual:=tt}; simp [*, zero_le_mul, mul_le_mul] {contextual := tt}, by by_cases r = 0; simp [*] {contextual:=tt}⟩, assume d, by by_cases d = 0; simp [*] {contextual:=tt}⟩ lemma le_of_forall_epsilon_le (h : ∀ε>0, b < ∞ → a ≤ b + of_real ε) : a ≤ b := suffices ∀r, 0 ≤ r → of_real r > b → a ≤ of_real r, from le_of_forall_le_of_dense $ forall_ennreal.mpr $ by simp; assumption, assume r hr hrb, let ⟨p, hp, b_eq, hpr⟩ := lt_iff_exists_of_real.mp hrb in have p < r, by simp [hp, hr] at hpr; assumption, have pos : 0 < r - p, from lt_sub_iff_add_lt.mpr $ by simp [this], calc a ≤ b + of_real (r - p) : h _ pos (by simp [b_eq]) ... = of_real r : by simp [-sub_eq_add_neg, le_of_lt pos, hp, hr, b_eq]; simp [sub_eq_add_neg] protected lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < of_real q ∧ of_real q < b) := ⟨λ h, by rcases lt_iff_exists_of_real.1 h with ⟨p, p0, rfl, _⟩; rcases dense h with ⟨c, pc, cb⟩; rcases lt_iff_exists_of_real.1 cb with ⟨r, r0, rfl, _⟩; rcases exists_rat_btwn ((of_real_lt_of_real_iff p0 r0).1 pc) with ⟨q, pq, qr⟩; have q0 := le_trans p0 (le_of_lt pq); exact ⟨q, rat.cast_nonneg.1 q0, (of_real_lt_of_real_iff p0 q0).2 pq, lt_trans ((of_real_lt_of_real_iff q0 r0).2 qr) cb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ end order section complete_lattice @[simp] lemma infty_mem_upper_bounds {s : set ennreal} : ∞ ∈ upper_bounds s := assume x hx, le_infty lemma of_real_mem_upper_bounds {s : set real} (hs : ∀x∈s, (0:real) ≤ x) (hr : 0 ≤ r) : of_real r ∈ upper_bounds (of_real '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} lemma is_lub_of_real {s : set real} (hs : ∀x∈s, (0:real) ≤ x) (hr : 0 ≤ r) (h : s ≠ ∅) : is_lub (of_real '' s) (of_real r) ↔ is_lub s r := let ⟨x, hx₁⟩ := exists_mem_of_ne_empty h in have hx₂ : 0 ≤ x, from hs _ hx₁, begin simp [is_lub, is_least, lower_bounds, of_real_mem_upper_bounds, hs, hr, forall_ennreal] {contextual := tt}, exact (and_congr_right $ assume hrb, ⟨assume h p hp, h _ (le_trans hx₂ $ hp _ hx₁) hp, assume h p _ hp, h _ hp⟩) end protected lemma exists_is_lub (s : set ennreal) : ∃x, is_lub s x := by_cases (assume h : s = ∅, ⟨0, by simp [h, is_lub, is_least, lower_bounds, upper_bounds]⟩) $ assume h : s ≠ ∅, let ⟨x, hx⟩ := exists_mem_of_ne_empty h in by_cases (assume : ∃r, 0 ≤ r ∧ of_real r ∈ upper_bounds s, let ⟨r, hr, hb⟩ := this in let s' := of_real ⁻¹' s ∩ {x | 0 ≤ x} in have s'_nn : ∀x∈s', (0:real) ≤ x, from assume x h, h.right, have s_eq : s = of_real '' s', from set.ext $ assume a, ⟨assume ha, let ⟨q, hq₁, hq₂, hq₃⟩ := (le_of_real_iff hr).mp (hb _ ha) in ⟨q, ⟨show of_real q ∈ s, from hq₃ ▸ ha, hq₁⟩, hq₃ ▸ rfl⟩, assume ⟨r, ⟨hr₁, hr₂⟩, hr₃⟩, hr₃ ▸ hr₁⟩, have x ∈ of_real '' s', from s_eq ▸ hx, let ⟨x', hx', hx'_eq⟩ := this in have ∃x, is_lub s' x, from exists_supremum_real ‹x' ∈ s'› $ (of_real_mem_upper_bounds s'_nn hr).mp $ s_eq ▸ hb, let ⟨x, hx⟩ := this in have 0 ≤ x, from le_trans hx'.right $ hx.left _ hx', ⟨of_real x, by rwa [s_eq, is_lub_of_real s'_nn this]; exact ne_empty_of_mem hx'⟩) begin intro h, existsi ∞, simp [is_lub, is_least, lower_bounds, forall_ennreal, not_exists, not_and] at h ⊢, assumption end instance : has_Sup ennreal := ⟨λs, some (ennreal.exists_is_lub s)⟩ protected lemma is_lub_Sup {s : set ennreal} : is_lub s (Sup s) := some_spec _ protected lemma le_Sup {s : set ennreal} : a ∈ s → a ≤ Sup s := ennreal.is_lub_Sup.left a protected lemma Sup_le {s : set ennreal} : (∀b ∈ s, b ≤ a) → Sup s ≤ a := ennreal.is_lub_Sup.right _ instance : complete_linear_order ennreal := { top := ∞, bot := 0, inf := min, sup := max, Sup := Sup, Inf := λs, Sup {a | ∀b ∈ s, a ≤ b}, le_top := assume a, le_infty, bot_le := assume a, ennreal.zero_le, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := assume a b c, max_le, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := assume a b c, le_min, le_Sup := assume s a, ennreal.le_Sup, Sup_le := assume s a, ennreal.Sup_le, le_Inf := assume s a h, ennreal.le_Sup h, Inf_le := assume s a ha, ennreal.Sup_le $ assume b hb, hb _ ha, ..ennreal.decidable_linear_order } @[simp] protected lemma bot_eq_zero : (⊥ : ennreal) = 0 := rfl @[simp] protected lemma top_eq_infty : (⊤ : ennreal) = ∞ := rfl end complete_lattice section topological_space open topological_space instance : topological_space ennreal := topological_space.generate_from {s | ∃a, s = {b | a < b} ∨ s = {b | b < a}} instance : orderable_topology ennreal := ⟨rfl⟩ instance : t2_space ennreal := by apply_instance instance : second_countable_topology ennreal := ⟨⟨⋃q ≥ (0:ℚ), {{a : ennreal | a < of_real q}, {a : ennreal | of_real ↑q < a}}, countable_bUnion countable_encodable $ assume a ha, countable_insert countable_singleton, le_antisymm (generate_from_le $ λ s h, begin rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < of_real q}, {b | of_real q < b}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]), rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ of_real q < a}, {b | b < of_real q}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) } end) (generate_from_le $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt})⟩⟩ lemma continuous_of_real : continuous of_real := have ∀x:ennreal, is_open {a : ℝ | x < of_real a}, from forall_ennreal.mpr ⟨assume r hr, by simp [of_real_lt_of_real_iff_cases]; exact is_open_and (is_open_lt' 0) (is_open_lt' r), by simp⟩, have ∀x:ennreal, is_open {a : ℝ | of_real a < x}, from forall_ennreal.mpr ⟨assume r hr, by simp [of_real_lt_of_real_iff_cases]; exact is_open_and is_open_const (is_open_gt' r), by simp [is_open_const]⟩, continuous_generated_from $ begin simp [or_imp_distrib, *] {contextual := tt} end lemma tendsto_of_real : tendsto of_real (nhds r) (nhds (of_real r)) := continuous_iff_tendsto.mp continuous_of_real r lemma tendsto_of_ennreal (hr : 0 ≤ r) : tendsto of_ennreal (nhds (of_real r)) (nhds r) := tendsto_orderable_unbounded (no_top _) (no_bot _) $ assume l u hl hu, by_cases (assume hr : r = 0, have hl : l < 0, by rw [hr] at hl; exact hl, have hu : 0 < u, by rw [hr] at hu; exact hu, have nhds (of_real r) = (⨅l (h₂ : 0 < l), principal {x | x < l}), from calc nhds (of_real r) = nhds ⊥ : by simp [hr]; refl ... = (⨅u (h₂ : 0 < u), principal {x | x < u}) : nhds_bot_orderable, have {x | x < of_real u} ∈ (nhds (of_real r)).sets, by rw [this]; from mem_infi_sets (of_real u) (mem_infi_sets (by simp *) (subset.refl _)), ((nhds (of_real r)).upwards_sets this $ forall_ennreal.mpr $ by simp [le_of_lt, hu, hl] {contextual := tt}; exact assume p hp _, lt_of_lt_of_le hl hp)) (assume hr_ne : r ≠ 0, have hu0 : 0 < u, from lt_of_le_of_lt hr hu, have hu_nn: 0 ≤ u, from le_of_lt hu0, have hr' : 0 < r, from lt_of_le_of_ne hr hr_ne.symm, have hl' : ∃l, l < of_real r, from ⟨0, by simp [hr, hr']⟩, have hu' : ∃u, of_real r < u, from ⟨of_real u, by simp [hr, hu_nn, hu]⟩, begin rw [mem_nhds_unbounded hu' hl'], existsi (of_real l), existsi (of_real u), simp [*, of_real_lt_of_real_iff_cases, forall_ennreal] {contextual := tt} end) lemma nhds_of_real_eq_map_of_real_nhds {r : ℝ} (hr : 0 ≤ r) : nhds (of_real r) = (nhds r).map of_real := have h₁ : {x | x < ∞} ∈ (nhds (of_real r)).sets, from mem_nhds_sets (is_open_gt' ∞) of_real_lt_infty, have h₂ : {x | x < ∞} ∈ ((nhds r).map of_real).sets, from mem_map.mpr $ univ_mem_sets' $ assume a, of_real_lt_infty, have h : ∀x<∞, ∀y<∞, of_ennreal x = of_ennreal y → x = y, by simp [forall_ennreal] {contextual:=tt}, le_antisymm (by_cases (assume (hr : r = 0) s (hs : {x | of_real x ∈ s} ∈ (nhds r).sets), have hs : {x | of_real x ∈ s} ∈ (nhds (0:ℝ)).sets, from hr ▸ hs, let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top 0) (no_bot 0)).mp hs in have nhds (of_real r) = nhds ⊥, by simp [hr]; refl, begin rw [this, nhds_bot_orderable], apply mem_infi_sets (of_real u) _, apply mem_infi_sets (zero_lt_of_real_iff.mpr hu) _, simp [set.subset_def], intro x, rw [lt_iff_exists_of_real], simp [le_of_lt hu] {contextual := tt}, exact assume p hp _ hpu, h _ (lt_of_lt_of_le hl hp) hpu end) (assume : r ≠ 0, have hr' : 0 < r, from lt_of_le_of_ne hr this.symm, have h' : map (of_ennreal ∘ of_real) (nhds r) = map id (nhds r), from map_cong $ (nhds r).upwards_sets (mem_nhds_sets (is_open_lt' 0) hr') $ assume r hr, by simp [le_of_lt hr, (∘)], le_of_map_le_map_inj' h₁ h₂ h $ le_trans (tendsto_of_ennreal hr) $ by simp [h'])) tendsto_of_real lemma nhds_of_real_eq_map_of_real_nhds_nonneg {r : ℝ} (hr : 0 ≤ r) : nhds (of_real r) = (nhds r ⊓ principal {x | 0 ≤ x}).map of_real := by rw [nhds_of_real_eq_map_of_real_nhds hr]; from by_cases (assume : r = 0, le_antisymm (assume s (hs : {a | of_real a ∈ s} ∈ (nhds r ⊓ principal {x | 0 ≤ x}).sets), let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := mem_inf_sets.mp hs in show {a | of_real a ∈ s} ∈ (nhds r).sets, from (nhds r).upwards_sets ht₁ $ assume a ha, match le_total 0 a with | or.inl h := have a ∈ t₂, from ht₂ h, ht ⟨ha, this⟩ | or.inr h := have r ∈ t₁ ∩ t₂, from ⟨mem_of_nhds ht₁, ht₂ (le_of_eq ‹r = 0›.symm)⟩, have of_real 0 ∈ s, from ‹r = 0› ▸ ht this, by simp [of_real_of_nonpos h]; assumption end) (map_mono inf_le_left)) (assume : r ≠ 0, have 0 < r, from lt_of_le_of_ne hr this.symm, have nhds r ⊓ principal {x : ℝ | 0 ≤ x} = nhds r, from inf_of_le_left $ le_principal_iff.mpr $ le_mem_nhds this, by simp [*]) instance : topological_add_monoid ennreal := have hinf : ∀a, tendsto (λ(p : ennreal × ennreal), p.1 + p.2) ((nhds ∞).prod (nhds a)) (nhds ⊤), begin intro a, rw [nhds_top_orderable], apply tendsto_infi.2 _, intro b, apply tendsto_infi.2 _, intro hb, apply tendsto_principal.2 _, revert b, simp [forall_ennreal], exact assume r hr, mem_prod_iff.mpr ⟨ {a | of_real r < a}, mem_nhds_sets (is_open_lt' _) of_real_lt_infty, univ, univ_mem_sets, assume ⟨c, d⟩ ⟨hc, _⟩, lt_of_lt_of_le hc $ le_add_right $ le_refl _⟩ end, have h : ∀{p r : ℝ}, 0 ≤ p → 0 ≤ r → tendsto (λp:ennreal×ennreal, p.1 + p.2) ((nhds (of_real r)).prod (nhds (of_real p))) (nhds (of_real (r + p))), from assume p r hp hr, begin rw [nhds_of_real_eq_map_of_real_nhds_nonneg hp, nhds_of_real_eq_map_of_real_nhds_nonneg hr, prod_map_map_eq, ←prod_inf_prod, prod_principal_principal, ←nhds_prod_eq], exact tendsto_map' (tendsto_cong (tendsto_le_left inf_le_left $ tendsto_add'.comp tendsto_of_real) (mem_inf_sets_of_right $ mem_principal_sets.mpr $ by simp [subset_def, (∘)] {contextual:=tt})) end, have ∀{a₁ a₂ : ennreal}, tendsto (λp:ennreal×ennreal, p.1 + p.2) (nhds (a₁, a₂)) (nhds (a₁ + a₂)), from forall_ennreal.mpr ⟨assume r hr, forall_ennreal.mpr ⟨assume p hp, by simp [*, nhds_prod_eq]; exact h _ _, begin rw [nhds_prod_eq, prod_comm], apply tendsto_map' _, simp [(∘)], exact hinf _ end⟩, by simp [nhds_prod_eq]; exact hinf⟩, ⟨continuous_iff_tendsto.mpr $ assume ⟨a₁, a₂⟩, this⟩ protected lemma tendsto_mul : ∀{a b : ennreal}, b ≠ 0 → tendsto ((*) a) (nhds b) (nhds (a * b)) := forall_ennreal.mpr $ and.intro (assume p hp, forall_ennreal.mpr $ and.intro (assume r hr hr0, have r ≠ 0, from assume h, by simp [h] at hr0; contradiction, have 0 < r, from lt_of_le_of_ne hr this.symm, have tendsto (λr, of_real (p * r)) (nhds r ⊓ principal {x : ℝ | 0 ≤ x}) (nhds (of_real (p * r))), from tendsto.comp (tendsto_mul tendsto_const_nhds $ tendsto_id' inf_le_left) tendsto_of_real, begin rw [nhds_of_real_eq_map_of_real_nhds_nonneg hr, of_real_mul_of_real hp hr], apply tendsto_map' (tendsto_cong this $ mem_inf_sets_of_right $ mem_principal_sets.mpr _), simp [subset_def, (∘), hp] {contextual := tt} end) (assume _, by_cases (assume : p = 0, tendsto_cong tendsto_const_nhds $ (nhds ∞).upwards_sets (mem_nhds_sets (is_open_lt' _) (@of_real_lt_infty 1)) $ by simp [this]) (assume p0 : p ≠ 0, have p_pos : 0 < p, from lt_of_le_of_ne hp p0.symm, suffices tendsto ((*) (of_real p)) (nhds ⊤) (nhds ⊤), { simpa [hp, p0] }, by rw [nhds_top_orderable]; from (tendsto_infi.2 $ assume l, tendsto_infi.2 $ assume hl, let ⟨q, hq, hlq, _⟩ := ennreal.lt_iff_exists_of_real.mp hl in tendsto_infi' (of_real (q / p)) $ tendsto_infi' of_real_lt_infty $ tendsto_principal_principal.2 $ forall_ennreal.mpr $ and.intro begin have : ∀r:ℝ, 0 < r → q / p < r → q < p * r ∧ 0 < p * r, from assume r r_pos qpr, have q < p * r, from calc q = (q / p) * p : by rw [div_mul_cancel _ (ne_of_gt p_pos)] ... < r * p : mul_lt_mul_of_pos_right qpr p_pos ... = p * r : mul_comm _ _, ⟨this, mul_pos p_pos r_pos⟩, simp [hlq, hp, of_real_lt_of_real_iff_cases, this] {contextual := tt} end begin simp [hp, p0]; exact hl end)))) begin assume b hb0, have : 0 < b, from lt_of_le_of_ne ennreal.zero_le hb0.symm, suffices : tendsto ((*) ∞) (nhds b) (nhds ∞), { simpa [hb0] }, apply (tendsto_cong tendsto_const_nhds $ (nhds b).upwards_sets (mem_nhds_sets (is_open_lt' _) this) _), { assume c hc, have : c ≠ 0, from assume h, by simp [h] at hc; contradiction, simp [this] } end lemma supr_of_real {s : set ℝ} {a : ℝ} (h : is_lub s a) : (⨆a∈s, of_real a) = of_real a := suffices Sup (of_real '' s) = of_real a, by simpa [Sup_image], is_lub_iff_Sup_eq.mp $ is_lub_of_is_lub_of_tendsto (assume x _ y _, of_real_le_of_real) h (ne_empty_of_is_lub h) (tendsto.comp (tendsto_id' inf_le_left) tendsto_of_real) lemma infi_of_real {s : set ℝ} {a : ℝ} (h : is_glb s a) : (⨅a∈s, of_real a) = of_real a := suffices Inf (of_real '' s) = of_real a, by simpa [Inf_image], is_glb_iff_Inf_eq.mp $ is_glb_of_is_glb_of_tendsto (assume x _ y _, of_real_le_of_real) h (ne_empty_of_is_glb h) (tendsto.comp (tendsto_id' inf_le_left) tendsto_of_real) lemma Inf_add {s : set ennreal} : Inf s + a = ⨅b∈s, b + a := by_cases (assume : s = ∅, by simp [this, ennreal.top_eq_infty]) (assume : s ≠ ∅, have Inf ((λb, b + a) '' s) = Inf s + a, from is_glb_iff_Inf_eq.mp $ is_glb_of_is_glb_of_tendsto (assume x _ y _ h, add_le_add' h (le_refl _)) is_glb_Inf this (tendsto_add (tendsto_id' inf_le_left) tendsto_const_nhds), by simp [Inf_image, -add_comm] at this; exact this.symm) lemma Sup_add {s : set ennreal} (hs : s ≠ ∅) : Sup s + a = ⨆b∈s, b + a := have Sup ((λb, b + a) '' s) = Sup s + a, from is_lub_iff_Sup_eq.mp $ is_lub_of_is_lub_of_tendsto (assume x _ y _ h, add_le_add' h (le_refl _)) is_lub_Sup hs (tendsto_add (tendsto_id' inf_le_left) tendsto_const_nhds), by simp [Sup_image, -add_comm] at this; exact this.symm lemma supr_add {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : supr s + a = ⨆b, s b + a := let ⟨x⟩ := h in calc supr s + a = Sup (range s) + a : by simp [Sup_range] ... = (⨆b∈range s, b + a) : Sup_add $ ne_empty_iff_exists_mem.mpr ⟨s x, x, rfl⟩ ... = _ : by simp [supr_range, -mem_range] lemma infi_add {ι : Sort*} {s : ι → ennreal} {a : ennreal} : infi s + a = ⨅b, s b + a := calc infi s + a = Inf (range s) + a : by simp [Inf_range] ... = (⨅b∈range s, b + a) : Inf_add ... = _ : by simp [infi_range, -mem_range] lemma add_infi {ι : Sort*} {s : ι → ennreal} {a : ennreal} : a + infi s = ⨅b, a + s b := by rw [add_comm, infi_add]; simp lemma infi_add_infi {ι : Sort*} {f g : ι → ennreal} (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add' (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅a', ⨅a, f a + g a') : le_infi $ assume a', le_infi $ assume a, let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... ≤ infi f + infi g : by simp [infi_add, add_infi, -add_comm, -le_infi_iff] lemma infi_sum {α : Type*} {ι : Sort*} {f : ι → α → ennreal} {s : finset α} [inhabited ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, s.sum (f i)) = s.sum (λa, ⨅i, f i a) := finset.induction_on s (by simp) $ assume a s ha ih, have ∀ (i j : ι), ∃ (k : ι), f k a + s.sum (f k) ≤ f i a + s.sum (f j), from assume i j, let ⟨k, hk⟩ := h (insert a s) i j in ⟨k, add_le_add' (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum' $ assume a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩, by simp [ha, ih.symm, infi_add_infi this] end topological_space section sub instance : has_sub ennreal := ⟨λa b, Inf {d | a ≤ d + b}⟩ @[simp] lemma sub_eq_zero_of_le (h : a ≤ b) : a - b = 0 := le_antisymm (Inf_le $ le_add_left h) ennreal.zero_le @[simp] lemma sub_add_cancel_of_le (h : b ≤ a) : (a - b) + b = a := let ⟨c, hc⟩ := le_iff_exists_add.mp h in eq.trans Inf_add $ le_antisymm (infi_le_of_le c $ infi_le_of_le (by simp [hc]) $ by simp [hc]) (le_infi $ assume d, le_infi $ assume hd, hd) @[simp] lemma add_sub_cancel_of_le (h : b ≤ a) : b + (a - b) = a := by rwa [add_comm, sub_add_cancel_of_le] lemma sub_add_self_eq_max : (a - b) + b = max a b := match le_total a b with | or.inl h := by simp [h, max_eq_right] | or.inr h := by simp [h, max_eq_left] end lemma sub_le_sub (h₁ : a ≤ b) (h₂ : d ≤ c) : a - c ≤ b - d := Inf_le_Inf $ assume e (h : b ≤ e + d), calc a ≤ b : h₁ ... ≤ e + d : h ... ≤ e + c : add_le_add (le_refl _) h₂ @[simp] protected lemma sub_le_iff_le_add : a - b ≤ c ↔ a ≤ c + b := iff.intro (assume h : a - b ≤ c, calc a ≤ (a - b) + b : by rw [sub_add_self_eq_max]; exact le_max_left _ _ ... ≤ c + b : add_le_add h (le_refl _)) (assume h : a ≤ c + b, calc a - b ≤ (c + b) - b : sub_le_sub h (le_refl _) ... ≤ c : Inf_le (le_refl (c + b))) @[simp] lemma zero_sub : 0 - a = 0 := le_antisymm (Inf_le ennreal.zero_le) ennreal.zero_le @[simp] lemma sub_infty : a - ∞ = 0 := le_antisymm (Inf_le le_infty) ennreal.zero_le @[simp] lemma sub_zero : a - 0 = a := eq.trans (add_zero (a - 0)).symm $ by simp @[simp] lemma infty_sub_of_real (hr : 0 ≤ r) : ∞ - of_real r = ∞ := top_unique $ le_Inf $ by simp [forall_ennreal, hr] {contextual := tt}; refl @[simp] lemma of_real_sub_of_real (hr : 0 ≤ r) : of_real p - of_real r = of_real (p - r) := match le_total p r with | or.inr h := have 0 ≤ p - r, from le_sub_iff_add_le.mpr $ by simp [h], have eq : r + (p - r) = p, by rw [add_comm, sub_add_cancel], le_antisymm (Inf_le $ by simp [-sub_eq_add_neg, this, hr, le_trans hr h, eq, le_refl]) (le_Inf $ by simp [forall_ennreal, hr, le_trans hr h, add_nonneg, -sub_eq_add_neg, this, sub_le_iff_le_add] {contextual := tt}) | or.inl h := begin rw [sub_eq_zero_of_le, of_real_of_nonpos], { rw [sub_le_iff_le_add], simp [h] }, { exact of_real_le_of_real h } end end @[simp] lemma add_sub_self : ∀{a b : ennreal}, b < ∞ → (a + b) - b = a := by simp [forall_ennreal] {contextual:=tt} protected lemma tendsto_of_real_sub (hr : 0 ≤ r) : tendsto (λb, of_real r - b) (nhds b) (nhds (of_real r - b)) := by_cases (assume h : of_real r < b, suffices tendsto (λb, of_real r - b) (nhds b) (nhds ⊥), by simpa [le_of_lt h], by rw [nhds_bot_orderable]; from (tendsto_infi.2 $ assume p, tendsto_infi.2 $ assume hp : 0 < p, tendsto_principal.2 $ (nhds b).upwards_sets (mem_nhds_sets (is_open_lt' (of_real r)) h) $ by simp [forall_ennreal, hr, le_of_lt, hp] {contextual := tt})) (assume h : ¬ of_real r < b, let ⟨p, hp, hpr, eq⟩ := (le_of_real_iff hr).mp $ not_lt.1 h in have tendsto (λb, of_real ((r - b))) (nhds p ⊓ principal {x | 0 ≤ x}) (nhds (of_real (r - p))), from tendsto.comp (tendsto_sub tendsto_const_nhds (tendsto_id' inf_le_left)) tendsto_of_real, have tendsto (λb, of_real r - b) (map of_real (nhds p ⊓ principal {x | 0 ≤ x})) (nhds (of_real (r - p))), from tendsto_map' $ tendsto_cong this $ mem_inf_sets_of_right $ by simp [(∘), -sub_eq_add_neg] {contextual:=tt}, by simp at this; simp [eq, hr, hp, hpr, nhds_of_real_eq_map_of_real_nhds_nonneg, this]) lemma sub_supr {ι : Sort*} [hι : nonempty ι] {b : ι → ennreal} (hr : a < ⊤) : a - (⨆i, b i) = (⨅i, a - b i) := let ⟨i⟩ := hι in let ⟨r, hr, eq, _⟩ := lt_iff_exists_of_real.mp hr in have Inf ((λb, of_real r - b) '' range b) = of_real r - (⨆i, b i), from is_glb_iff_Inf_eq.mp $ is_glb_of_is_lub_of_tendsto (assume x _ y _, sub_le_sub (le_refl _)) is_lub_supr (ne_empty_of_mem ⟨i, rfl⟩) (tendsto.comp (tendsto_id' inf_le_left) (ennreal.tendsto_of_real_sub hr)), by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range] end sub section inv instance : has_inv ennreal := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : has_div ennreal := ⟨λa b, a * b⁻¹⟩ @[simp] lemma inv_zero : (0 : ennreal)⁻¹ = ∞ := show Inf {b : ennreal | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_infty : (∞ : ennreal)⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h] @[simp] lemma inv_of_real (hr : 0 < r) : (of_real r)⁻¹ = of_real (r⁻¹) := have 0 ≤ r⁻¹, from le_of_lt (inv_pos hr), have r0 : 0 ≤ r, from le_of_lt hr, le_antisymm (Inf_le $ by simp [*, inv_pos hr, mul_inv_cancel (ne_of_gt hr)]) (le_Inf $ forall_ennreal.mpr ⟨λ p hp, by simp [*, show 0 ≤ r*p, from mul_nonneg r0 hp]; intro; rwa [inv_eq_one_div, div_le_iff hr, mul_comm], λ h, le_top⟩) lemma inv_inv : ∀{a:ennreal}, (a⁻¹)⁻¹ = a := forall_ennreal.mpr $ and.intro (assume r hr, by_cases (assume : r = 0, by simp [this]) (assume : r ≠ 0, have 0 < r, from lt_of_le_of_ne hr this.symm, by simp [*, inv_pos, inv_inv'])) (by simp) end inv section tsum variables {f g : α → ennreal} protected lemma is_sum : is_sum f (⨆s:finset α, s.sum f) := tendsto_orderable.2 ⟨assume a' ha', let ⟨s, hs⟩ := lt_supr_iff.mp ha' in mem_at_top_sets.mpr ⟨s, assume t ht, lt_of_lt_of_le hs $ finset.sum_le_sum_of_subset ht⟩, assume a' ha', univ_mem_sets' $ assume s, have s.sum f ≤ ⨆(s : finset α), s.sum f, from le_supr (λ(s : finset α), s.sum f) s, lt_of_le_of_lt this ha'⟩ @[simp] protected lemma has_sum : has_sum f := ⟨_, ennreal.is_sum⟩ protected lemma tsum_eq_supr_sum : (∑a, f a) = (⨆s:finset α, s.sum f) := tsum_eq_is_sum ennreal.is_sum protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ennreal) : (∑p:Σa, β a, f p.1 p.2) = (∑a, ∑b, f a b) := tsum_sigma (assume b, ennreal.has_sum) ennreal.has_sum protected lemma tsum_prod {f : α → β → ennreal} : (∑p:α×β, f p.1 p.2) = (∑a, ∑b, f a b) := let j : α × β → (Σa:α, β) := λp, sigma.mk p.1 p.2 in let i : (Σa:α, β) → α × β := λp, (p.1, p.2) in let f' : (Σa:α, β) → ennreal := λp, f p.1 p.2 in calc (∑p:α×β, f' (j p)) = (∑p:Σa:α, β, f p.1 p.2) : tsum_eq_tsum_of_iso j i (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl) ... = (∑a, ∑b, f a b) : ennreal.tsum_sigma f protected lemma tsum_of_real {f : α → ℝ} (h : is_sum f r) (hf : ∀a, 0 ≤ f a) : (∑a, of_real (f a)) = of_real r := have (λs:finset α, s.sum (of_real ∘ f)) = of_real ∘ (λs:finset α, s.sum f), from funext $ assume s, sum_of_real $ assume a _, hf a, have tendsto (λs:finset α, s.sum (of_real ∘ f)) at_top (nhds (of_real r)), by rw [this]; exact h.comp tendsto_of_real, tsum_eq_is_sum this protected lemma tsum_comm {f : α → β → ennreal} : (∑a, ∑b, f a b) = (∑b, ∑a, f a b) := let f' : α×β → ennreal := λp, f p.1 p.2 in calc (∑a, ∑b, f a b) = (∑p:α×β, f' p) : ennreal.tsum_prod.symm ... = (∑p:β×α, f' (prod.swap p)) : (tsum_eq_tsum_of_iso prod.swap (@prod.swap α β) (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)).symm ... = (∑b, ∑a, f' (prod.swap (b, a))) : @ennreal.tsum_prod β α (λb a, f' (prod.swap (b, a))) protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : (∑a, f a) ≤ (∑a, g a) := tsum_le_tsum h ennreal.has_sum ennreal.has_sum protected lemma tsum_eq_supr_nat {f : ℕ → ennreal} : (∑i:ℕ, f i) = (⨆i:ℕ, (finset.range i).sum f) := calc _ = (⨆s:finset ℕ, s.sum f) : ennreal.tsum_eq_supr_sum ... = (⨆i:ℕ, (finset.range i).sum f) : le_antisymm (supr_le_supr2 $ assume s, let ⟨n, hn⟩ := finset.exists_nat_subset_range s in ⟨n, finset.sum_le_sum_of_subset hn⟩) (supr_le_supr2 $ assume i, ⟨finset.range i, le_refl _⟩) protected lemma le_tsum {a : α} : f a ≤ (∑a, f a) := calc f a = ({a} : finset α).sum f : by simp ... ≤ (⨆s:finset α, s.sum f) : le_supr (λs:finset α, s.sum f) _ ... = (∑a, f a) : by rw [ennreal.tsum_eq_supr_sum] protected lemma mul_tsum : (∑i, a * f i) = a * (∑i, f i) := if h : ∀i, f i = 0 then by simp [h] else let ⟨i, (hi : f i ≠ 0)⟩ := classical.not_forall.mp h in have sum_ne_0 : (∑i, f i) ≠ 0, from ne_of_gt $ calc 0 < f i : lt_of_le_of_ne ennreal.zero_le hi.symm ... ≤ (∑i, f i) : ennreal.le_tsum, have tendsto (λs:finset α, s.sum ((*) a ∘ f)) at_top (nhds (a * (∑i, f i))), by rw [← show (*) a ∘ (λs:finset α, s.sum f) = λs, s.sum ((*) a ∘ f), from funext $ λ s, finset.mul_sum]; exact (is_sum_tsum ennreal.has_sum).comp (ennreal.tendsto_mul sum_ne_0), tsum_eq_is_sum this protected lemma tsum_mul : (∑i, f i * a) = (∑i, f i) * a := by simp [mul_comm, ennreal.mul_tsum] @[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ennreal} : (∑b:α, ⨆ (h : a = b), f b) = f a := le_antisymm (by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s, calc s.sum (λb, ⨆ (h : a = b), f b) ≤ (finset.singleton a).sum (λb, ⨆ (h : a = b), f b) : finset.sum_le_sum_of_ne_zero $ assume b _ hb, suffices a = b, by simpa using this.symm, classical.by_contradiction $ assume h, by simpa [h] using hb ... = f a : by simp)) (calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl ... ≤ (∑b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum) end tsum section nnreal -- TODO: use nnreal to define ennreal instance : has_coe nnreal ennreal := ⟨ennreal.of_real ∘ coe⟩ lemma tendsto_of_real_iff {f : filter α} {m : α → ℝ} {r : ℝ} (hm : ∀a, 0 ≤ m a) (hr : 0 ≤ r) : tendsto (λx, of_real (m x)) f (nhds (of_real r)) ↔ tendsto m f (nhds r) := iff.intro (assume h, have tendsto (λ (x : α), of_ennreal (of_real (m x))) f (nhds r), from h.comp (tendsto_of_ennreal hr), by simpa [hm]) (assume h, h.comp tendsto_of_real) lemma tendsto_coe_iff {f : filter α} {m : α → nnreal} {r : nnreal} : tendsto (λx, (m x : ennreal)) f (nhds r) ↔ tendsto m f (nhds r) := iff.trans (tendsto_of_real_iff (assume a, (m a).2) r.2) nnreal.tendsto_coe protected lemma is_sum_of_real_iff {f : α → ℝ} {r : ℝ} (hf : ∀a, 0 ≤ f a) (hr : 0 ≤ r) : is_sum (λa, of_real (f a)) (of_real r) ↔ is_sum f r := by simp [is_sum, sum_of_real, hf]; exact tendsto_of_real_iff (assume s, finset.zero_le_sum $ assume a ha, hf a) hr protected lemma is_sum_coe_iff {f : α → nnreal} {r : nnreal} : is_sum (λa, (f a : ennreal)) r ↔ is_sum f r := iff.trans (ennreal.is_sum_of_real_iff (assume a, (f a).2) r.2) nnreal.is_sum_coe protected lemma coe_tsum {f : α → nnreal} (h : has_sum f) : ↑(∑a, f a) = (∑a, f a : ennreal) := eq.symm (tsum_eq_is_sum $ ennreal.is_sum_coe_iff.2 $ is_sum_tsum h) @[simp] lemma coe_mul (a b : nnreal) : ↑(a * b) = (a * b : ennreal) := (ennreal.of_real_mul_of_real a.2 b.2).symm @[simp] lemma coe_one : ↑(1 : nnreal) = (1 : ennreal) := rfl @[simp] lemma coe_eq_coe {n m : nnreal} : (↑n : ennreal) = m ↔ n = m := iff.trans (of_real_eq_of_real_of n.2 m.2) (iff.intro subtype.eq $ assume eq, eq ▸ rfl) end nnreal end ennreal lemma has_sum_of_nonneg_of_le {f g : β → ℝ} (hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) : has_sum f → has_sum g | ⟨r, hfr⟩ := have hf : ∀a, 0 ≤ f a, from assume a, le_trans (hg a) (hgf a), have hr : 0 ≤ r, from is_sum_le hf is_sum_zero hfr, have is_sum (λa, ennreal.of_real (f a)) (ennreal.of_real r), from (ennreal.is_sum_of_real_iff hf hr).2 hfr, have (∑b, ennreal.of_real (g b)) ≤ ennreal.of_real r, begin refine is_sum_le (assume b, _) (is_sum_tsum ennreal.has_sum) this, exact ennreal.of_real_le_of_real (hgf _) end, let ⟨p, hp, hpr, eq⟩ := (ennreal.le_of_real_iff hr).1 this in have is_sum g p, from (ennreal.is_sum_of_real_iff hg hp).1 (eq ▸ is_sum_tsum ennreal.has_sum), has_sum_spec this lemma nnreal.has_sum_of_le {f g : β → nnreal} (hgf : ∀b, g b ≤ f b) (hf : has_sum f) : has_sum g := nnreal.has_sum_coe.1 $ has_sum_of_nonneg_of_le (assume b, (g b).2) hgf $ nnreal.has_sum_coe.2 hf
e816554484890160d9f4bbb351b626d4fc8ee6ed
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/Meta.lean
75be9686fbbc126aa20d0a000000f6e23f442013
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
901
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.Meta.Basic import Lean.Meta.LevelDefEq import Lean.Meta.WHNF import Lean.Meta.InferType import Lean.Meta.FunInfo import Lean.Meta.ExprDefEq import Lean.Meta.DiscrTree import Lean.Meta.Reduce import Lean.Meta.Instances import Lean.Meta.AbstractMVars import Lean.Meta.SynthInstance import Lean.Meta.AppBuilder import Lean.Meta.Tactic import Lean.Meta.KAbstract import Lean.Meta.RecursorInfo import Lean.Meta.GeneralizeTelescope import Lean.Meta.Match import Lean.Meta.ReduceEval import Lean.Meta.Closure import Lean.Meta.AbstractNestedProofs import Lean.Meta.ForEachExpr import Lean.Meta.Transform import Lean.Meta.PPGoal import Lean.Meta.UnificationHint import Lean.Meta.Inductive import Lean.Meta.SizeOf import Lean.Meta.Coe
2dacf10c91ad9fe921ab798838cd4e463b865857
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/geometry/euclidean/sphere.lean
a9d1680a5aee6fd9a919d7427ef03069bc56e191
[ "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
8,399
lean
/- Copyright (c) 2021 Manuel Candales. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Manuel Candales, Benjamin Davidson -/ import geometry.euclidean.triangle /-! # Spheres This file proves basic geometrical results about distances and angles in spheres in real inner product spaces and Euclidean affine spaces. ## Main theorems * `mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_pi`: Intersecting Chords Theorem (Freek No. 55). * `mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_zero`: Intersecting Secants Theorem. * `mul_dist_add_mul_dist_eq_mul_dist_of_cospherical`: Ptolemy’s Theorem (Freek No. 95). TODO: The current statement of Ptolemy’s theorem works around the lack of a "cyclic polygon" concept in mathlib, which is what the theorem statement would naturally use (or two such concepts, since both a strict version, where all vertices must be distinct, and a weak version, where consecutive vertices may be equal, would be useful; Ptolemy's theorem should then use the weak one). An API needs to be built around that concept, which would include: - strict cyclic implies weak cyclic, - weak cyclic and consecutive points distinct implies strict cyclic, - weak/strict cyclic implies weak/strict cyclic for any subsequence, - any three points on a sphere are weakly or strictly cyclic according to whether they are distinct, - any number of points on a sphere intersected with a two-dimensional affine subspace are cyclic in some order, - a list of points is cyclic if and only if its reversal is, - a list of points is cyclic if and only if any cyclic permutation is, while other permutations are not when the points are distinct, - a point P where the diagonals of a cyclic polygon cross exists (and is unique) with weak/strict betweenness depending on weak/strict cyclicity, - four points on a sphere with such a point P are cyclic in the appropriate order, and so on. -/ open real open_locale euclidean_geometry real_inner_product_space real variables {V : Type*} [inner_product_space ℝ V] namespace inner_product_geometry /-! ### Geometrical results on spheres in real inner product spaces This section develops some results on spheres in real inner product spaces, which are used to deduce corresponding results for Euclidean affine spaces. -/ lemma mul_norm_eq_abs_sub_sq_norm {x y z : V} (h₁ : ∃ k : ℝ, k ≠ 1 ∧ x + y = k • (x - y)) (h₂ : ∥z - y∥ = ∥z + y∥) : ∥x - y∥ * ∥x + y∥ = abs (∥z + y∥ ^ 2 - ∥z - x∥ ^ 2) := begin obtain ⟨k, hk_ne_one, hk⟩ := h₁, let r := (k - 1)⁻¹ * (k + 1), have hxy : x = r • y, { rw [← smul_smul, eq_inv_smul_iff' (sub_ne_zero.mpr hk_ne_one), ← sub_eq_zero], calc (k - 1) • x - (k + 1) • y = (k • x - x) - (k • y + y) : by simp_rw [sub_smul, add_smul, one_smul] ... = (k • x - k • y) - (x + y) : by simp_rw [← sub_sub, sub_right_comm] ... = k • (x - y) - (x + y) : by rw ← smul_sub k x y ... = 0 : sub_eq_zero.mpr hk.symm }, have hzy : ⟪z, y⟫ = 0, by rwa [inner_eq_zero_iff_angle_eq_pi_div_two, ← norm_add_eq_norm_sub_iff_angle_eq_pi_div_two, eq_comm], have hzx : ⟪z, x⟫ = 0 := by rw [hxy, inner_smul_right, hzy, mul_zero], calc ∥x - y∥ * ∥x + y∥ = ∥(r - 1) • y∥ * ∥(r + 1) • y∥ : by simp [sub_smul, add_smul, hxy] ... = ∥r - 1∥ * ∥y∥ * (∥r + 1∥ * ∥y∥) : by simp_rw [norm_smul] ... = ∥r - 1∥ * ∥r + 1∥ * ∥y∥ ^ 2 : by ring ... = abs ((r - 1) * (r + 1) * ∥y∥ ^ 2) : by simp [abs_mul, norm_eq_abs] ... = abs (r ^ 2 * ∥y∥ ^ 2 - ∥y∥ ^ 2) : by ring_nf ... = abs (∥x∥ ^ 2 - ∥y∥ ^ 2) : by simp [hxy, norm_smul, mul_pow, norm_eq_abs, sq_abs] ... = abs (∥z + y∥ ^ 2 - ∥z - x∥ ^ 2) : by simp [norm_add_sq_real, norm_sub_sq_real, hzy, hzx, abs_sub_comm], end end inner_product_geometry namespace euclidean_geometry /-! ### Geometrical results on spheres in Euclidean affine spaces This section develops some results on spheres in Euclidean affine spaces. -/ open inner_product_geometry variables {P : Type*} [metric_space P] [normed_add_torsor V P] include V /-- If `P` is a point on the line `AB` and `Q` is equidistant from `A` and `B`, then `AP * BP = abs (BQ ^ 2 - PQ ^ 2)`. -/ lemma mul_dist_eq_abs_sub_sq_dist {a b p q : P} (hp : ∃ k : ℝ, k ≠ 1 ∧ b -ᵥ p = k • (a -ᵥ p)) (hq : dist a q = dist b q) : dist a p * dist b p = abs (dist b q ^ 2 - dist p q ^ 2) := begin let m : P := midpoint ℝ a b, obtain ⟨v, h1, h2, h3⟩ := ⟨vsub_sub_vsub_cancel_left, v a p m, v p q m, v a q m⟩, have h : ∀ r, b -ᵥ r = (m -ᵥ r) + (m -ᵥ a) := λ r, by rw [midpoint_vsub_left, ← right_vsub_midpoint, add_comm, vsub_add_vsub_cancel], iterate 4 { rw dist_eq_norm_vsub V }, rw [← h1, ← h2, h, h], rw [← h1, h] at hp, rw [dist_eq_norm_vsub V a q, dist_eq_norm_vsub V b q, ← h3, h] at hq, exact mul_norm_eq_abs_sub_sq_norm hp hq, end /-- If `A`, `B`, `C`, `D` are cospherical and `P` is on both lines `AB` and `CD`, then `AP * BP = CP * DP`. -/ lemma mul_dist_eq_mul_dist_of_cospherical {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hapb : ∃ k₁ : ℝ, k₁ ≠ 1 ∧ b -ᵥ p = k₁ • (a -ᵥ p)) (hcpd : ∃ k₂ : ℝ, k₂ ≠ 1 ∧ d -ᵥ p = k₂ • (c -ᵥ p)) : dist a p * dist b p = dist c p * dist d p := begin obtain ⟨q, r, h'⟩ := (cospherical_def {a, b, c, d}).mp h, obtain ⟨ha, hb, hc, hd⟩ := ⟨h' a _, h' b _, h' c _, h' d _⟩, { rw ← hd at hc, rw ← hb at ha, rw [mul_dist_eq_abs_sub_sq_dist hapb ha, hb, mul_dist_eq_abs_sub_sq_dist hcpd hc, hd] }, all_goals { simp }, end /-- **Intersecting Chords Theorem**. -/ theorem mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_pi {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hapb : ∠ a p b = π) (hcpd : ∠ c p d = π) : dist a p * dist b p = dist c p * dist d p := begin obtain ⟨-, k₁, _, hab⟩ := angle_eq_pi_iff.mp hapb, obtain ⟨-, k₂, _, hcd⟩ := angle_eq_pi_iff.mp hcpd, exact mul_dist_eq_mul_dist_of_cospherical h ⟨k₁, (by linarith), hab⟩ ⟨k₂, (by linarith), hcd⟩, end /-- **Intersecting Secants Theorem**. -/ theorem mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_zero {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hab : a ≠ b) (hcd : c ≠ d) (hapb : ∠ a p b = 0) (hcpd : ∠ c p d = 0) : dist a p * dist b p = dist c p * dist d p := begin obtain ⟨-, k₁, -, hab₁⟩ := angle_eq_zero_iff.mp hapb, obtain ⟨-, k₂, -, hcd₁⟩ := angle_eq_zero_iff.mp hcpd, refine mul_dist_eq_mul_dist_of_cospherical h ⟨k₁, _, hab₁⟩ ⟨k₂, _, hcd₁⟩; by_contra hnot; simp only [not_not, *, one_smul] at *, exacts [hab (vsub_left_cancel hab₁).symm, hcd (vsub_left_cancel hcd₁).symm], end /-- **Ptolemy’s Theorem**. -/ theorem mul_dist_add_mul_dist_eq_mul_dist_of_cospherical {a b c d p : P} (h : cospherical ({a, b, c, d} : set P)) (hapc : ∠ a p c = π) (hbpd : ∠ b p d = π) : dist a b * dist c d + dist b c * dist d a = dist a c * dist b d := begin have h' : cospherical ({a, c, b, d} : set P), { rwa set.insert_comm c b {d} }, have hmul := mul_dist_eq_mul_dist_of_cospherical_of_angle_eq_pi h' hapc hbpd, have hbp := left_dist_ne_zero_of_angle_eq_pi hbpd, have h₁ : dist c d = dist c p / dist b p * dist a b, { rw [dist_mul_of_eq_angle_of_dist_mul b p a c p d, dist_comm a b], { rw [angle_eq_angle_of_angle_eq_pi_of_angle_eq_pi hbpd hapc, angle_comm] }, all_goals { field_simp [mul_comm, hmul] } }, have h₂ : dist d a = dist a p / dist b p * dist b c, { rw [dist_mul_of_eq_angle_of_dist_mul c p b d p a, dist_comm c b], { rwa [angle_comm, angle_eq_angle_of_angle_eq_pi_of_angle_eq_pi], rwa angle_comm }, all_goals { field_simp [mul_comm, hmul] } }, have h₃ : dist d p = dist a p * dist c p / dist b p, { field_simp [mul_comm, hmul] }, have h₄ : ∀ x y : ℝ, x * (y * x) = x * x * y := λ x y, by rw [mul_left_comm, mul_comm], field_simp [h₁, h₂, dist_eq_add_dist_of_angle_eq_pi hbpd, h₃, hbp, dist_comm a b, h₄, ← sq, dist_sq_mul_dist_add_dist_sq_mul_dist b, hapc], end end euclidean_geometry
12c72dbd940f17009455cc1607aad38f36cdfef6
bd57135d92f6f3b771aae7ca08550ff8719ec590
/src/determinants.lean
526126e323eb024a35d5164fb5ead4bb68975939
[]
no_license
simonjwinwood/kbb
109775572d17fceb93ee2419905bbecff776ebd2
b5ff345e1022aee40dfda406c1c0631a9bba9c03
refs/heads/master
1,585,550,843,265
1,537,509,365,000
1,537,509,365,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,988
lean
import tactic.tidy import group_theory.subgroup import group_theory.perm import data.finset import .monoid_stuff import .matrices import .Sym universes u v lemma finset.prod_mul_right {α} [group α] {β} [comm_monoid β] {f : α → β} {s : finset α} (x : α) : s.prod f = (s.map ⟨λ z, z * x⁻¹, λ _ _, mul_right_cancel⟩).prod (λ z, f (z * x)) := finset.prod_bij (λ z _, z * x⁻¹) (λ _ _, by simpa) (λ _ _, by simp) (λ _ _ _ _, mul_right_cancel) (λ b H, ⟨b * x, by revert H; simp [eq_comm] {contextual:=tt}, by simp⟩) lemma finset.sum_mul_right {α} [group α] {β} [add_comm_monoid β] {f : α → β} {s : finset α} (x : α) : s.sum f = (s.map ⟨λ z, z * x⁻¹, λ _ _, mul_right_cancel⟩).sum (λ z, f (z * x)) := finset.sum_bij (λ z _, z * x⁻¹) (λ _ _, by simpa) (λ _ _, by simp) (λ _ _ _ _, mul_right_cancel) (λ b H, ⟨b * x, by revert H; simp [eq_comm] {contextual:=tt}, by simp⟩) lemma finset.univ_sum_mul_right {α} [group α] [fintype α] {β} [add_comm_monoid β] {f : α → β} (x : α) : finset.univ.sum f = finset.univ.sum (λ z, f (z * x)) := finset.sum_bij (λ z _, z * x⁻¹) (λ _ _, finset.mem_univ _) (λ _ _, by simp) (λ _ _ _ _, mul_right_cancel) (λ b H, ⟨b * x, by revert H; simp [eq_comm] {contextual:=tt}, by simp⟩) lemma finset.prod_perm {α} [fintype α] [decidable_eq α] {β} [comm_monoid β] {f : α → β} (σ : equiv.perm α) : (finset.univ : finset α).prod f = finset.univ.prod (λ z, f (σ z)) := eq.symm $ finset.prod_bij (λ z _, σ z) (λ _ _, finset.mem_univ _) (λ _ _, rfl) (λ _ _ _ _ H, σ.bijective.1 H) (λ b _, ⟨σ⁻¹ b, finset.mem_univ _, by simp⟩) instance {α} (H : α → Prop) : subsingleton (decidable_pred H) := by apply_instance @[simp] lemma finset.filter_true {α} (s : finset α) [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp namespace matrix variables {n : Type u} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R] instance : group (equiv.perm n) := by apply_instance @[simp] lemma equiv.swap_mul_self (i j : n) : equiv.swap i j * equiv.swap i j = 1 := equiv.swap_swap i j @[simp] lemma equiv.swap_swap_apply (i j k : n) : equiv.swap i j (equiv.swap i j k) = k := equiv.swap_core_swap_core k i j instance : decidable_pred (function.bijective : (n → n) → Prop) := λ _, by unfold function.bijective; apply_instance instance bij_fintype : fintype {f : n → n // function.bijective f} := set_fintype _ @[extensionality] theorem equiv.perm.ext (σ τ : equiv.perm n) (H : ∀ i, σ i = τ i) : σ = τ := equiv.ext _ _ H instance equiv_perm_fin_finite : fintype (equiv.perm n):= trunc.rec_on_subsingleton (fintype.equiv_fin n) $ λ φ, fintype.of_equiv (Sym (fintype.card n)) $ { to_fun := λ σ, φ.trans (σ.trans φ.symm), inv_fun := λ σ, φ.symm.trans (σ.trans φ), left_inv := λ σ, by ext i; simp, right_inv := λ σ, by ext i; simp } @[simp] lemma equiv.perm.sign_mul (σ τ : equiv.perm n) : (σ * τ).sign = σ.sign * τ.sign := is_group_hom.mul _ _ _ @[simp] lemma equiv.perm.sign_one : equiv.perm.sign (1 : equiv.perm n) = 1 := is_group_hom.one _ @[simp] lemma equiv.perm.sign_refl : equiv.perm.sign (equiv.refl n) = 1 := is_group_hom.one equiv.perm.sign @[simp] lemma equiv.perm.sign_swap' {i j : n} : (equiv.swap i j).sign = if i = j then 1 else -1 := if H : i = j then by simp [H, equiv.swap_self] else by simp [equiv.perm.sign_swap H, H] def e (σ : equiv.perm n) : R := ((σ.sign : ℤ) : R) @[simp] lemma e_mul (σ τ : equiv.perm n) : (e (σ * τ) : R) = e σ * e τ := by unfold e; rw ← int.cast_mul; congr; rw ← units.mul_coe; congr; apply is_group_hom.mul @[simp] lemma e_swap {i j : n} : (e (equiv.swap i j) : R) = if i = j then 1 else -1 := by by_cases H : i = j; simp [e, H] @[simp] lemma e_one : (e (1 : equiv.perm n) : R) = 1 := by unfold e; rw is_group_hom.one (equiv.perm.sign : equiv.perm n → units ℤ); simp @[simp] lemma e_inv (σ : equiv.perm n): (e σ⁻¹ : R) = e σ := by unfold e; rw is_group_hom.inv (equiv.perm.sign : equiv.perm n → units ℤ); cases int.units_eq_one_or σ.sign with H H; rw H; refl lemma e_eq_one_or (σ : equiv.perm n) : (e σ : R) = 1 ∨ (e σ : R) = -1 := by cases int.units_eq_one_or σ.sign with H H; unfold e; rw H; simp definition det (M : matrix n n R) : R := finset.univ.sum (λ (σ : equiv.perm n), (e σ) * finset.univ.prod (λ (i:n), M (σ i) i)) @[simp] lemma det_diagonal {d : n → R} : det (diagonal d) = finset.univ.prod d := begin refine (finset.sum_eq_single 1 _ _).trans _, { intros σ h1 h2, cases not_forall.1 (mt (equiv.ext _ _) h2) with x h3, convert mul_zero _, apply finset.prod_eq_zero, { change x ∈ _, simp }, exact if_neg h3 }, simp, simp end @[simp] lemma det_scalar {r : R} : det (scalar r : matrix n n R) = r^(fintype.card n) := by simp [scalar, fintype.card] -- Useful lemma by Chris lemma zero_pow : ∀ {n : ℕ}, 0 < n → (0 : R) ^ n = 0 | (n+1) H := zero_mul _ @[simp] lemma det_zero (h : nonempty n) : det (0 : matrix n n R) = (0 : R) := by rw ← scalar_zero; simp [-scalar_zero, zero_pow, fintype.card_pos_iff.mpr h] @[simp] lemma det_one : det (1 : matrix n n R) = (1 : R) := by rw ← scalar_one; simp [-scalar_one] lemma det_mul_aux (M N : matrix n n R) (p : n → n) (H : ¬function.bijective p) : finset.sum (finset.univ : finset (equiv.perm n)) (λ σ, e σ * (finset.prod (finset.univ : finset n) (λ x, M (σ x) (p x)) * finset.prod (finset.univ : finset n) (λ x, N (p x) x))) = 0 := begin have H1 : ¬function.injective p, from mt (λ h, and.intro h $ fintype.injective_iff_surjective.1 h) H, unfold function.injective at H1, simp only [not_forall] at H1, rcases H1 with ⟨i, j, H2, H3⟩, have H4 : (finset.univ : finset (equiv.perm n)) = finset.univ.filter (λ σ, σ.sign = 1) ∪ finset.univ.filter (λ σ, σ.sign = -1), { rw [← finset.filter_or], simp only [int.units_eq_one_or], ext k, simp only [finset.mem_univ, finset.mem_filter, true_and] }, have H5 : (finset.univ : finset (equiv.perm n)).filter (λ σ, σ.sign = 1) ∩ finset.univ.filter (λ σ, σ.sign = -1) = ∅, { rw [← finset.filter_and], refine finset.eq_empty_of_forall_not_mem (λ _ H, _), rw finset.mem_filter at H, exact absurd (H.2.1.symm.trans H.2.2) dec_trivial }, have H6 : finset.map ⟨λ z, z * equiv.swap i j, λ _ _, mul_right_cancel⟩ (finset.univ.filter (λ σ, σ.sign = -1)) = finset.univ.filter (λ σ, σ.sign = 1), { ext k, split, { exact λ H, finset.mem_filter.2 ⟨finset.mem_univ _, let ⟨b, hb1, hb2⟩ := finset.mem_map.1 H in by rw ← hb2; dsimp only [function.embedding.coe_fn_mk]; rw [equiv.perm.sign_mul, (finset.mem_filter.1 hb1).2, equiv.perm.sign_swap H3]; refl⟩ }, { exact λ H, finset.mem_map.2 ⟨k * equiv.swap i j, finset.mem_filter.2 ⟨finset.mem_univ _, by rw [equiv.perm.sign_mul, (finset.mem_filter.1 H).2, equiv.perm.sign_swap H3, one_mul]⟩, by dsimp only [function.embedding.coe_fn_mk]; rw [mul_assoc, equiv.swap_mul_self, mul_one]⟩ } }, have H7 : ∀ k, p (equiv.swap i j k) = p k, { intro k, rw [equiv.swap_apply_def], split_ifs; cc }, rw [H4, finset.sum_union H5], refine eq.trans (congr_arg _ (finset.sum_mul_right $ equiv.swap i j)) _, conv { to_lhs, congr, skip, for (finset.prod _ _) [1] { rw finset.prod_perm (equiv.swap i j)}}, simp [H3, H6, H7] end @[simp] lemma det_mul (M N : matrix n n R) : det (M * N) = det M * det N := begin unfold det, conv { to_lhs, simp only [mul_val', finset.prod_sum, finset.mul_sum] }, conv { to_lhs, for (M _ _ * N _ _) [1] { rw @proof_irrel _ x.2 (finset.mem_univ x.1) } }, rw finset.sum_comm, refine (finset.sum_bij (λ (p : Π (a : n), a ∈ finset.univ → n) _, (λ i, p i (finset.mem_univ i) : n → n)) (λ _ _, finset.mem_univ _) (λ p _, _) _ _).trans _, { exact (λ p, finset.sum (finset.univ : finset (equiv.perm n)) (λ σ, e σ * (finset.prod (finset.univ : finset n) (λ x, M (σ x) (p x)) * finset.prod (finset.univ : finset n) (λ x, N (p x) x)))) }, { conv { to_lhs, congr, skip, funext, rw @finset.prod_attach n R finset.univ _ (λ k, M (x k) (p k _) * N (p k _) k), rw finset.prod_mul_distrib } }, { exact λ _ _ _ _ H, funext (λ i, funext (λ _, have _ := congr_fun H i, this)) }, { exact λ b _, ⟨λ i _, b i, finset.mem_pi.2 (λ _ _, finset.mem_univ _), rfl⟩ }, refine (finset.sum_subset (finset.subset_univ (finset.univ.filter function.bijective)) _).symm.trans _, { exact λ p _ H, det_mul_aux M N p (mt (λ H2, finset.mem_filter.2 ⟨finset.mem_univ _, H2⟩) H) }, refine (finset.sum_bij (λ (τ : equiv.perm n) (_ : _ ∈ finset.univ) x, τ x) (λ τ _, finset.mem_filter.2 ⟨finset.mem_univ _, τ.bijective⟩) _ _ _).symm.trans _, { exact (λ τ, e τ * finset.prod (finset.univ : finset n) (λ x, N (τ x) x) * finset.sum (finset.univ : finset (equiv.perm n)) (λ σ, e σ * finset.prod (finset.univ : finset n) (λ x, M (σ x) x))) }, { intros τ _, dsimp only, conv { to_lhs, rw [mul_assoc, mul_left_comm, finset.mul_sum], congr, skip, rw [finset.univ_sum_mul_right τ⁻¹], congr, skip, funext, rw [← mul_assoc, mul_comm (e τ), ← e_mul, inv_mul_cancel_right], rw [finset.prod_perm τ], simp only [equiv.perm.mul_apply, equiv.perm.inv_apply_self] }, conv { to_rhs, congr, skip, funext, rw [← mul_assoc, mul_comm] }, conv { to_rhs, rw ← finset.mul_sum} }, { exact λ _ _ _ _ H, equiv.perm.ext _ _ (congr_fun H) }, { exact λ b H, ⟨equiv.of_bijective (finset.mem_filter.1 H).2, finset.mem_univ _, equiv.of_bijective_to_fun (finset.mem_filter.1 H).2⟩ }, rw [← finset.sum_mul, mul_comm] end instance : is_monoid_hom (det : matrix n n R → R) := { map_one := det_one, map_mul := det_mul } end matrix
aaa132caba39083a60dd965168caee63c49a5542
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/char_p/algebra.lean
2273ed60cc6eef7c69d5249f89b675565c689f08
[ "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,188
lean
/- Copyright (c) 2021 Jon Eugster. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jon Eugster, Eric Wieser -/ import algebra.char_p.basic import ring_theory.localization.fraction_ring import algebra.free_algebra /-! # Characteristics of algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we describe the characteristic of `R`-algebras. In particular we are interested in the characteristic of free algebras over `R` and the fraction field `fraction_ring R`. ## Main results - `char_p_of_injective_algebra_map` If `R →+* A` is an injective algebra map then `A` has the same characteristic as `R`. Instances constructed from this result: - Any `free_algebra R X` has the same characteristic as `R`. - The `fraction_ring R` of an integral domain `R` has the same characteristic as `R`. -/ /-- If the algebra map `R →+* A` is injective then `A` has the same characteristic as `R`. -/ lemma char_p_of_injective_algebra_map {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] (h : function.injective (algebra_map R A)) (p : ℕ) [char_p R p] : char_p A p := { cast_eq_zero_iff := λx, begin rw ←char_p.cast_eq_zero_iff R p x, change algebra_map ℕ A x = 0 ↔ algebra_map ℕ R x = 0, rw is_scalar_tower.algebra_map_apply ℕ R A x, refine iff.trans _ h.eq_iff, rw ring_hom.map_zero, end } lemma char_p_of_injective_algebra_map' (R A : Type*) [field R] [semiring A] [algebra R A] [nontrivial A] (p : ℕ) [char_p R p] : char_p A p := char_p_of_injective_algebra_map (algebra_map R A).injective p /-- If the algebra map `R →+* A` is injective and `R` has characteristic zero then so does `A`. -/ lemma char_zero_of_injective_algebra_map {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] (h : function.injective (algebra_map R A)) [char_zero R] : char_zero A := { cast_injective := λ x y hxy, begin change algebra_map ℕ A x = algebra_map ℕ A y at hxy, rw is_scalar_tower.algebra_map_apply ℕ R A x at hxy, rw is_scalar_tower.algebra_map_apply ℕ R A y at hxy, exact char_zero.cast_injective (h hxy), end } -- `char_p.char_p_to_char_zero A _ (char_p_of_injective_algebra_map h 0)` does not work -- here as it would require `ring A`. /-! As an application, a `ℚ`-algebra has characteristic zero. -/ section Q_algebra variables (R : Type*) [nontrivial R] /-- A nontrivial `ℚ`-algebra has `char_p` equal to zero. This cannot be a (local) instance because it would immediately form a loop with the instance `algebra_rat`. It's probably easier to go the other way: prove `char_zero R` and automatically receive an `algebra ℚ R` instance. -/ lemma algebra_rat.char_p_zero [semiring R] [algebra ℚ R] : char_p R 0 := char_p_of_injective_algebra_map (algebra_map ℚ R).injective 0 /-- A nontrivial `ℚ`-algebra has characteristic zero. This cannot be a (local) instance because it would immediately form a loop with the instance `algebra_rat`. It's probably easier to go the other way: prove `char_zero R` and automatically receive an `algebra ℚ R` instance. -/ lemma algebra_rat.char_zero [ring R] [algebra ℚ R] : char_zero R := @char_p.char_p_to_char_zero R _ (algebra_rat.char_p_zero R) end Q_algebra /-! An algebra over a field has the same characteristic as the field. -/ section variables (K L : Type*) [field K] [comm_semiring L] [nontrivial L] [algebra K L] lemma algebra.char_p_iff (p : ℕ) : char_p K p ↔ char_p L p := (algebra_map K L).char_p_iff_char_p p lemma algebra.ring_char_eq : ring_char K = ring_char L := by { rw [ring_char.eq_iff, algebra.char_p_iff K L], apply ring_char.char_p } end namespace free_algebra variables {R X : Type*} [comm_semiring R] (p : ℕ) /-- If `R` has characteristic `p`, then so does `free_algebra R X`. -/ instance char_p [char_p R p] : char_p (free_algebra R X) p := char_p_of_injective_algebra_map free_algebra.algebra_map_left_inverse.injective p /-- If `R` has characteristic `0`, then so does `free_algebra R X`. -/ instance char_zero [char_zero R] : char_zero (free_algebra R X) := char_zero_of_injective_algebra_map free_algebra.algebra_map_left_inverse.injective end free_algebra namespace is_fraction_ring variables (R : Type*) {K : Type*} [comm_ring R] [field K] [algebra R K] [is_fraction_ring R K] variables (p : ℕ) /-- If `R` has characteristic `p`, then so does Frac(R). -/ lemma char_p_of_is_fraction_ring [char_p R p] : char_p K p := char_p_of_injective_algebra_map (is_fraction_ring.injective R K) p /-- If `R` has characteristic `0`, then so does Frac(R). -/ lemma char_zero_of_is_fraction_ring [char_zero R] : char_zero K := @char_p.char_p_to_char_zero K _ (char_p_of_is_fraction_ring R 0) variables [is_domain R] /-- If `R` has characteristic `p`, then so does `fraction_ring R`. -/ instance char_p [char_p R p] : char_p (fraction_ring R) p := char_p_of_is_fraction_ring R p /-- If `R` has characteristic `0`, then so does `fraction_ring R`. -/ instance char_zero [char_zero R] : char_zero (fraction_ring R) := char_zero_of_is_fraction_ring R end is_fraction_ring
59dbaacb6b61aa4d4653628c91714a2539a92eaf
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Init/Data/Nat/Bitwise.lean
4d014d5c09fffc3f381f35eb0759c7688fd04c65
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,278
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.Nat.Basic import Init.Data.Nat.Div import Init.Coe namespace Nat partial def bitwise (f : Bool → Bool → Bool) (n m : Nat) : Nat := if n = 0 then if f false true then m else 0 else if m = 0 then if f true false then n else 0 else let n' := n / 2 let m' := m / 2 let b₁ := n % 2 = 1 let b₂ := m % 2 = 1 let r := bitwise f n' m' if f b₁ b₂ then r+r+1 else r+r @[extern "lean_nat_land"] def land : Nat → Nat → Nat := bitwise and @[extern "lean_nat_lor"] def lor : Nat → Nat → Nat := bitwise or @[extern "lean_nat_lxor"] def xor : Nat → Nat → Nat := bitwise bne @[extern "lean_nat_shiftl"] def shiftLeft : Nat → Nat → Nat | n, 0 => n | n, succ m => shiftLeft (2*n) m @[extern "lean_nat_shiftr"] def shiftRight : Nat → Nat → Nat | n, 0 => n | n, succ m => shiftRight n m / 2 instance : AndOp Nat := ⟨Nat.land⟩ instance : OrOp Nat := ⟨Nat.lor⟩ instance : Xor Nat := ⟨Nat.xor⟩ instance : ShiftLeft Nat := ⟨Nat.shiftLeft⟩ instance : ShiftRight Nat := ⟨Nat.shiftRight⟩ end Nat
87aeff80933ecff1a71414d6825c9ddec3932842
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/notation_priority.lean
dd3d098abcfd235d0f8fade956206ae0f03a5da8
[ "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
342
lean
import data.real open real int nat variables a b : nat variables i j : int set_option pp.all true check a + b check i + j example : a + b = nat.add a b := rfl example : i + j = int.add i j := rfl open nat real int example : a + b = nat.add a b := rfl example : i + j = int.add i j := rfl set_option pp.all true check a + b check i + j
a8d02b09850f75d0c613c1f50f9994e5f0fe65a5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/computability/epsilon_NFA.lean
90127af4c675dc570710ad8d958af63c7845adb2
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,467
lean
/- Copyright (c) 2021 Fox Thomson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Fox Thomson -/ import computability.NFA /-! # Epsilon Nondeterministic Finite Automata This file contains the definition of an epsilon Nondeterministic Finite Automaton (`ε_NFA`), a state machine which determines whether a string (implemented as a list over an arbitrary alphabet) is in a regular set by evaluating the string over every possible path, also having access to ε-transitons, which can be followed without reading a character. Since this definition allows for automata with infinite states, a `fintype` instance must be supplied for true `ε_NFA`'s. -/ universes u v /-- An `ε_NFA` is a set of states (`σ`), a transition function from state to state labelled by the alphabet (`step`), a starting state (`start`) and a set of acceptance states (`accept`). Note the transition function sends a state to a `set` of states and can make ε-transitions by inputing `none`. Since this definition allows for Automata with infinite states, a `fintype` instance must be supplied for true `ε_NFA`'s.-/ structure ε_NFA (α : Type u) (σ : Type v) := (step : σ → option α → set σ) (start : set σ) (accept : set σ) variables {α : Type u} {σ σ' : Type v} (M : ε_NFA α σ) namespace ε_NFA instance : inhabited (ε_NFA α σ) := ⟨ ε_NFA.mk (λ _ _, ∅) ∅ ∅ ⟩ /-- The `ε_closure` of a set is the set of states which can be reached by taking a finite string of ε-transitions from an element of the the set -/ inductive ε_closure : set σ → set σ | base : ∀ S (s ∈ S), ε_closure S s | step : ∀ S s (t ∈ M.step s none), ε_closure S s → ε_closure S t /-- `M.step_set S a` is the union of the ε-closure of `M.step s a` for all `s ∈ S`. -/ def step_set : set σ → α → set σ := λ S a, S >>= (λ s, M.ε_closure (M.step s a)) /-- `M.eval_from S x` computes all possible paths though `M` with input `x` starting at an element of `S`. -/ def eval_from (start : set σ) : list α → set σ := list.foldl M.step_set (M.ε_closure start) /-- `M.eval x` computes all possible paths though `M` with input `x` starting at an element of `M.start`. -/ def eval := M.eval_from M.start /-- `M.accepts` is the language of `x` such that there is an accept state in `M.eval x`. -/ def accepts : language α := λ x, ∃ S ∈ M.accept, S ∈ M.eval x /-- `M.to_NFA` is an `NFA` constructed from an `ε_NFA` `M`. -/ def to_NFA : NFA α σ := { step := λ S a, M.ε_closure (M.step S a), start := M.ε_closure M.start, accept := M.accept } @[simp] lemma to_NFA_eval_from_match (start : set σ) : M.to_NFA.eval_from (M.ε_closure start) = M.eval_from start := rfl @[simp] lemma to_NFA_correct : M.to_NFA.accepts = M.accepts := begin ext x, rw [accepts, NFA.accepts, eval, NFA.eval, ←to_NFA_eval_from_match], refl end lemma pumping_lemma [fintype σ] {x : list α} (hx : x ∈ M.accepts) (hlen : fintype.card (set σ) + 1 ≤ list.length x) : ∃ a b c, x = a ++ b ++ c ∧ a.length + b.length ≤ fintype.card (set σ) + 1 ∧ b ≠ [] ∧ {a} * language.star {b} * {c} ≤ M.accepts := begin rw ←to_NFA_correct at hx ⊢, exact M.to_NFA.pumping_lemma hx hlen end end ε_NFA namespace NFA /-- `M.to_ε_NFA` is an `ε_NFA` constructed from an `NFA` `M` by using the same start and accept states and transition functions. -/ def to_ε_NFA (M : NFA α σ) : ε_NFA α σ := { step := λ s a, a.cases_on' ∅ (λ a, M.step s a), start := M.start, accept := M.accept } @[simp] lemma to_ε_NFA_ε_closure (M : NFA α σ) (S : set σ) : M.to_ε_NFA.ε_closure S = S := begin ext a, split, { rintro ( ⟨ _, _, h ⟩ | ⟨ _, _, _, h, _ ⟩ ), exact h, cases h }, { intro h, apply ε_NFA.ε_closure.base, exact h } end @[simp] lemma to_ε_NFA_eval_from_match (M : NFA α σ) (start : set σ) : M.to_ε_NFA.eval_from start = M.eval_from start := begin rw [eval_from, ε_NFA.eval_from, step_set, ε_NFA.step_set, to_ε_NFA_ε_closure], congr, ext S s, simp only [exists_prop, set.mem_Union, set.bind_def], apply exists_congr, simp only [and.congr_right_iff], intros t ht, rw M.to_ε_NFA_ε_closure, refl end @[simp] lemma to_ε_NFA_correct (M : NFA α σ) : M.to_ε_NFA.accepts = M.accepts := begin rw [accepts, ε_NFA.accepts, eval, ε_NFA.eval, to_ε_NFA_eval_from_match], refl end end NFA
de92acb6f77584a904e38bc654813453dc83ceea
c465bc191b95d7a9e7544551279b09708d373f58
/test_v2.lean
6d081d95fed5788fbb1819b7d487efd15c533e15
[]
no_license
kevinsullivan/physvm_develop
b7a4149c72a10b3a87894c5db5af9b2ce5885c80
a57e9c87d4d3fb7548dc6dfae3b932a51fc25b67
refs/heads/main
1,692,010,146,359
1,633,989,164,000
1,633,989,164,000
397,360,532
0
1
null
null
null
null
UTF-8
Lean
false
false
29,167
lean
import .phys.time.time import .phys.time_series.geom3d import .std.time_std import .std.geom3d_std noncomputable theory -- Kevin: See http://petitjeanmichel.free.fr/itoweb.petitjean.symmetry.html /- We need to assume a physical interpretation of the data representing our coordinate system on geom3d. See geom3d_std.lean for more details on the coordinate system and physical interpretation. -/ def geometry3d_acs : geom3d_space _ := rice420_acs /- We need to assume a physical interpretation of the data representing our coordinate system on time. We derive a new ACS from "coordinated_universal_time_in_seconds" - see time_std.lean for a more details on the coordinate system and physical interpretation (note : this is a conventional UTC ACS expressed with units in seconds) The gist of this interpretation of our "world time" is that we have formalized some fixed time at which our application is run, expressed in terms of UTC. To do so, we have simply translated the UTC origin to the time at which this was written, August 18th at ~240 PM. Note: any arbitrary time does not hold any impact on our formal model (although presumably if we set the origin to the stone age, there should not be a robotics camera running there). We do not change the basis - which is a standard basis whose unit vectors are expressed in seconds. (1) ORIGIN: We move the origin up to 1629311979 (2) BASIS VECTORS basis0 - points to the future - unit length is 1 second (as in UTC) - Presumably no dilation occur, since this is supposed to represent "empirical" (atomically-sampled-and-average-weighted) time (3) ACS is given by [Origin, b0] -/ def August18thTwoFortyPMTimestamp : scalar := 1629311979 def current_time_in_UTC : time_space _ := let origin := mk_time coordinated_universal_time_in_seconds 1629311979 in let basis := mk_duration coordinated_universal_time_in_seconds 1 in mk_time_space (mk_time_frame origin basis) /- We're assuming a RealSense D435I hardware unit. It comes with a defined coordinate system. We derive from the Rice Hall 420 standard, aliased in this file as "world_geom_acs". We'll assume that the camera_imu is two meters to the right along the back wall, one meter out from the wall and one meter high. We'll inhert the standard vector space structure from the world_geom_acs. That's its position in space. As for its orientation, we'll use the orientation provided by realsense documentation. In ROS orientation notation, we might call that 'EDN' 1. The positive x-axis points to the subject. 2. The positive y-axis points down. 3. The positive z-axis points forward -/ def camera_imu_acs : geom3d_space _ := let origin := mk_position3d geometry3d_acs 2 1 1 in let basis0 := mk_displacement3d geometry3d_acs 1 0 0 in let basis1 := mk_displacement3d geometry3d_acs 0 0 (-1) in let basis2 := mk_displacement3d geometry3d_acs 0 1 0 in let fr := mk_geom3d_frame origin basis0 basis1 basis2 in mk_geom3d_space fr -- https://www.intelrealsense.com/how-to-getting-imu-data-from-d435i-and-t265/#Tracking_Sensor_Origin_and_CS /- We provide an intepretation for the camera's OS clock, referred to as "System Time" in RealSense nomenclature. We are presumably running our application at some "recent" time, expressed in terms of UTC, so we inherit from the ACS "current_time_in_UTC", our global intepretation of "world time" - when the "application" is running, in terms of UTC. This is effectively the camera OS's interpretation of what it believes the current UTC time is. (https://intelrealsense.github.io/librealsense/doxygen/rs__frame_8h.html#a55750afe3461ea7748fbb2ef6fb19e8a) We will assume that the origin of the camera OS clock's affine coordinate system is off by some delta from the "current time in UTC's" origin, which, particularly, is unknowable-statically, so we're using the "current" origin of the UTC as the origin Camera's time frame, plus an unspecified delta. Note that the RealSense API provides sensor dataframes with timestamps expressed in milliseconds - so our frame is thus expressed in milliseconds as well to reflect that we are consuming this API. (1) ORIGIN: Some unknown, small constant offset, δ₁, away from the origin of the current UTC time's origin, which reflects the current drift of the clock's origin (2) BASIS VECTORS basis0 - points to the future - unit length is 1 millisecond - A dilation factor, ε₁, scales the basis vector, to convey the speed of the clock relative to an atomic approximation of UTC time (3) ACS is given by [Origin, b0] -/ axiom δ₁ : scalar axiom ε₁ : scalar def camera_system_time_acs : time_space _ := let milliseconds := (0.001) in -- SHOULD THIS BE MOVED TO STANDARDS? WHAT SHOULD IT LOOK LIKE? let origin := mk_time current_time_in_UTC δ₁ in let basis := mk_duration current_time_in_UTC (milliseconds*ε₁) in mk_time_space (mk_time_frame origin basis) /- As we must convert from milliseconds (as retrieved through the rs2::Frame API) to seconds, we express a new ACS which simply conveys the camera_system_time_acs, defined and described just above, with units in seconds rather than milliseconds. (1) ORIGIN: The origin is unchanged from camera_system_time_acs (2) BASIS VECTORS basis0 - points to the future - unit length is 1 second - The dilation factor is unchanged from the parent ACS (3) ACS is given by [Origin, b0] -/ def camera_system_time_seconds : time_space _ := let milliseconds_to_seconds := 1000 in --MOVE TO STANDARDS? DEFINE WHAT UNITS ARE SOMEWHERE ELSE? let origin := mk_time camera_system_time_acs 0 in let basis := mk_duration camera_system_time_acs milliseconds_to_seconds in mk_time_space (mk_time_frame origin basis) /- Next we construct the "hardware time" of the RealSense Camera (https://intelrealsense.github.io/librealsense/doxygen/rs__frame_8h.html#a55750afe3461ea7748fbb2ef6fb19e8a) This is a zero-initiated time (it does not reflect the current time in UTC, rather, it conveys how much time has passed since the camera has begun transmitting data). We define this in terms of UTC time, giving it an origin of 0, with no intrinsic "drift". We share the same dilation factor as camera_system_time_acs, as we are making the assumption that both of these measurements share the same rate of error. (1) ORIGIN: 0, reflecting that each run of the camera begins at the UTC origin, rather than at the current time (2) BASIS VECTORS basis0 - points to the future - unit length is 1 millisecond - A dilation factor, ε₁, scales the basis vector, to convey the speed of the clock relative to an atomic approximation of UTC time (3) ACS is given by [Origin, b0] -/ def camera_hardware_time_acs : time_space _ := let milliseconds := (0.001) in -- SHOULD THIS BE MOVED TO STANDARDS? WHAT SHOULD IT LOOK LIKE? let origin := mk_time coordinated_universal_time_in_seconds 0 in let basis := mk_duration coordinated_universal_time_in_seconds (milliseconds*ε₁) in mk_time_space (mk_time_frame origin basis) /- Similar to the definition of camera_system_time_acs, we define camera_hardware_time_seconds, reflecting the need to convert from camera_hardware_time_acs (in milliseconds) to an ACS expressed in seconds. (1) ORIGIN: The origin is unchanged from camera_hardware_time_acs (2) BASIS VECTORS basis0 - points to the future - unit length is 1 second - The dilation factor is unchanged from the parent ACS (3) ACS is given by [Origin, b0] -/ def camera_hardware_time_seconds : time_space _ := let milliseconds_to_seconds := 1000 in --MOVE TO STANDARDS? DEFINE WHAT UNITS ARE SOMEWHERE ELSE? let origin := mk_time camera_hardware_time_acs 0 in let basis := mk_duration camera_hardware_time_acs milliseconds_to_seconds in mk_time_space (mk_time_frame origin basis) /- This is the ROS client (of the RealSense camera) node's system time, an OS approximation of the current UTC time expressed in units seconds One gap to mention here is that this will be used to annotate the system time, which has a native representation of seconds + nanoseconds, so there is arguably some mismatch when we annotate the ros::Time variable simply as having units in seconds. As with our previously-defined camera_system_time_acs, we model a small drift from the origin, as well as a constant dilation factor to represent the idiosyncratic error of the clock. (1) ORIGIN: Some unknown, small constant offset, δ₂, away from the origin of the current UTC time's origin, which reflects the current drift of the clock's origin (2) BASIS VECTORS basis0 - points to the future - unit length is 1 second - A dilation factor, ε₂, scales the basis vector, to convey the speed of the clock relative to an atomic approximation of UTC time (3) ACS is given by [Origin, b0] -/ axiom δ₂ : scalar axiom ε₂ : scalar def platform_time_in_seconds := let seconds := 1 in let origin := mk_time coordinated_universal_time_in_seconds δ₂ in let basis := mk_duration coordinated_universal_time_in_seconds (seconds*ε₂) in mk_time_space (mk_time_frame origin basis) /- Lastly formalize the "global time" (https://intelrealsense.github.io/librealsense/doxygen/rs__frame_8h.html#a55750afe3461ea7748fbb2ef6fb19e8a) This is a conversion of the "hardware time", adjusted such specifically it's "dilation rate" has been adjusted from that of the camera's clock to that of a client's clock. Thus, we may think This is done in RealSense by a simply linear regression algorithm, and in reality may be subject to some error if the clock varies over time, but we are only able to model linear relationships, and in this case, the necessary conversion reduces to adjusting the hardware's basis by a factor of ε₂/ε₁. (1) ORIGIN: The origin is 0, as we are not translating the hardware ACS, just adjusting its dilation factor. (2) BASIS VECTORS basis0 - points to the future - unit length is 1 millisecond (specified in parent ACS, camera_hardware_time_acs) - A dilation factor, ε₂/ε₁, which removes the scaling of the camera's clock, and applies the ROS node's clock dilation (3) ACS is given by [Origin, b0] -/ def camera_global_time_acs : time_space _ := let origin := mk_time camera_hardware_time_acs δ₁ in let basis := mk_duration camera_hardware_time_acs (ε₂/ε₁) in mk_time_space (mk_time_frame origin basis) /- CODE FORMALIZATION OVERVIEW The parameter of the method imu_callback_sync, "frame", is either timestamped Acceleration or Angular Velocity Vector. We have no implementation for either in Peirce (or for sum types for that matter). Per discussion on last 8/6, this is replaced with a Displacement3D. We model two versions of this method, reflecting that different sorts of errors can occur. The gap/limitation that causes us to need to construct two versions of this method is that Peirce, as it is currently constructed, annotates a single execution path. Thus, to model two execution paths, we need two versions of formalization. In the first instance, we presume that we've received "_camera_time_base" in the camera_hardware_time_acs ACS, but that we've just received a dataframe whose timestamp "dataframe.timestamp" domain is expressed in camera_system_time_acs. What transpires is that we will subtract two timestamps - "dataframe.timestamp - _camera_time_base", which will yield a type error. In the actual C++ code, this computation will not yield a type error, rather, it will produce an extremely large "change in timestamps" (doubling of the UTC timestamp) which is physically meaningless and can lead to client failures. The dilation rates + units of the respective ACS's of the operands are compatibile, in this case, but the origins are obviously off by a dramatic amount. In the second instance, we presume that we've received "_camera_time_base" in the camera_global_time_acs ACS, but that we've just received a dataframe whose timestamp "dataframe.timestamp" domain is expressed in camera_hardware_time_acs. The only difference from above is that, the error will result in a smaller error - to the tune of a few hundred MS (varying over time, as the dilation rates are not in equal in this case). There are several other others that manifest in either treatment. One is that we annotate a variable suffixed "_ms" with a frame expressed in "milliseconds" units, whereas its actual computation is very clearly expressed in seconds. We formalize this such that there is clearly a conversion to (a) seconds (ACS), whereas the variable is typed in (a) milliseconds (ACS), such that a type error manifests. Next, when constructing a timestamped datum (CImuData imu_data), the developers are treating a double value which clearly represents a duration, not a time, as a timestamp. In our formalization, a type error manifests when constructing the timestamped value. Lastly, when adding a coordinate from a time expressed in our platform time WITH a coordinate from a time expressed in one of the camera's timestamp domains, there is no error in our formalization (currently - discussed extensively with Dr. Sullivan), although we would like one to be. The specific error that we would like to capture is that, due to differences in dilation rates, the addition of _ros_time_base with imu_msg.timestamp (an alias of elapsed_camera_ms), will not produce an accurate timestamp, even when elapsed_camera_ms is computed properly (using matching timestamp domains for both timestamp domains). void BaseRealSenseNode::imu_callback_sync(rs2::frame dataframe, imu_sync_method sync_method) -/ def imu_callback_sync_v1 : timestamped camera_system_time_acs (displacement3d camera_imu_acs) → punit := /- We define the argument to the method, dataframe. It has an interpretation of timestamped camera_time_acs (displacement3d camera_imu_acs) , although it's actual physical type manifest in the code would be an Acceleration or Angular Velocity Vector, representing a timestamped reading coming from a Gyroscope or Accelerometer. -/ λ dataframe, /- double frame_time = frame.get_timestamp(); In this line, we extract the timestamp of the dataframe, which represents the timestamp at which the IMU data was gathered. -/ let dataframe_time := dataframe.timestamp in /-setBaseTime(frame_time, frame.get_frame_timestamp_domain()); _ros_time_base = ros::Time::now(); _camera_time_base = frame_time; A call is made to the method "setBaseTime". setBaseTime contains two salient lines: setting both _ros_time_base and _camera_time_base. _ros_time_base is intended to represent the first time point at which the camera has sent an IMU data reading, expressed in terms of of the local system clock that is reading data from the RealSense camera data feed. _camera_time_base is intended to represent the first time point at which the camera has sent an IMU data reading, expressed in terms of the clock directly on the camera. -/ let _ros_time_base := mk_time platform_time_in_seconds August18thTwoFortyPMTimestamp in let _camera_time_base := mk_time camera_hardware_time_acs 10 in -- 10 is an arbitrary constant --double elapsed_camera_ms = (/*ms*/ frame_time - /*ms*/ _camera_time_base) / 1000.0; /- We take the difference between the first camera measurement, when the method "imu_callback_sync" was first called, and the current camera measurement, as contained in "dataframe_time". Thus, the resulting difference is a duration in time. The variable name suggests that the resulting computation is in milliseconds. The dataframe_time and the _camera_time_base are expressed in milliseconds due to the implementation of the camera's clock, however, the actual units are expressed in seconds, as there is a division by 1000. Thus, we transform the duration from its native millisecond frame to a seconds ACS. Mathematically, we might represent this as Tₘˢ(t₂-t₁). Finally, there is a type error, as we are attempting to assign a value in an ACS representing seconds to a variable in an ACS representing milliseconds, which portrays a misconception by the developer when naming this variable. -/ let elapsed_camera_ms : duration camera_system_time_acs := (camera_system_time_acs.mk_time_transform_to camera_system_time_seconds).transform_duration ((dataframe_time -ᵥ _camera_time_base : duration camera_system_time_acs)) in /- auto crnt_reading = *(reinterpret_cast<const float3*>(frame.get_data())); Eigen::Vector3d v(crnt_reading.x, crnt_reading.y, crnt_reading.z); There are some uneventful assignments that occur here. The first casts (via static_casting) the vector-quantity data that the frame argument encapsulates to a "float3 object" and stores it into a variable named "crnt_reading". The second converts the prior "float3" object into an "Eigen::Vector3d" object, by extracting its x, y, and z coordinates, using those respectively in the constructor to Eigen::Vector3d, and binding the constructed value into a variable called v. We model in this in Lean simply by defining a value called "crnt_reading" and assigning the vector-valued data property stored in the timestamped dataframe method argument. Since the physical type of the dataframe is "timestamped camera_time_acs (displacement3d camera_imu_acs)", we know that the type of crnt_reading must simply be "displacement3d camera_imu_acs". Next, we construct the vector v in the code. We define a variable v, which, again, has the physical type "displacement3d camera_imu_acs", since it is built by simply constructing a new displacement3d using the exact same x, y, and z coordinates of the prior value, crnt_reading. -/ let crnt_reading : displacement3d camera_imu_acs := dataframe.value in let v : displacement3d camera_imu_acs := mk_displacement3d camera_imu_acs crnt_reading.x crnt_reading.y crnt_reading.z in --CimuData imu_data(stream_index, v, elapsed_camera_ms); /- The constructor of CimuData in the next line of code simply re-packages the vector data stored in the original frame argument, whose physical interpretation was a timestamped displacement3d in the camera, back into an another object which represents a timestamped displacement3d, but this time, the timestamp is expressed in terms of the camera_time_seconds ACS, as the developer explicitly converted from milliseconds into seconds when constructing what is intended to be the timestamp, elapsed_camera_seconds. Thus, in order to formalize this in Lean, declare a variable called "imu_data" with type "timestamped camera_time_seconds (displacement3d camera_imu_acs)", and populate it using the vector-valued data v (which is, again, the same displacement3d encapsulated in the argument to the method), as well as the variable "elapsed_camera_ms" as a timestamp, which, again is a duration, not a point, as it is the result of subtracting two time variables, and so, we see an error here in our formalization. -/ let imu_data : timestamped camera_system_time_seconds (displacement3d camera_imu_acs) := ⟨elapsed_camera_ms, v⟩ in /- std::deque<sensor_msgs::Imu> imu_msgs; switch (sync_method) { case NONE: //Cannot really be NONE. Just to avoid compilation warning. case COPY: FillImuData_Copy(imu_data, imu_msgs); break; case LINEAR_INTERPOLATION: FillImuData_LinearInterpolation(imu_data, imu_msgs); break; } -/ /- We define a double-ended queue called "imu_msgs" and attempt to populate it. A call is made to the procedure "FillImuData_Copy" or "FillImuData_LinearInterpolation", which is omitted here. The purpose of these procedures includes are to construct IMU messages, specifically tuples of 2 3-dimensional (non-geometric) vectors, along with a linear interpolation that are beyond the scope of what we can currently formalize, and the resulting IMU messages stores either 0 (if the dataframe argument came from an accelerometer), 1 (if the dataframe argument came from a gyroscope and sync_method is set to "COPY"), or n (if the dataframe argument came from a gyroscope and sync_method is set to "LINEAR_INTERPOLATION") into deque "imu_msgs". The purpose of the method call is that entries are added to imu_msgs, so this is simulated by simply instantiating the list with an initial value: imu_data - the timestamped value that we've constructed above. Note that due to our limitations in Peirce, we have annotated the type of imu_msgs as being the type of the data that we have available, "(timestamped camera_time_seconds (displacement3d camera_imu_acs))", as opposed to a timestamped IMU message, since we are not yet able to formalize the latter. -/ let imu_msgs : list (timestamped camera_system_time_seconds (displacement3d camera_imu_acs)) := [imu_data] in /- while (imu_msgs.size()) { sensor_msgs::Imu imu_msg = imu_msgs.front(); -/ /- We now process each entry in the deque. Each entry has its timestamp updated, then it is published, and then it is removed from the queue, until the queue is empty. Firstly, we retrieve the front of the queue, which is simply a call to "list.head" in Lean, and, since "imu_msgs" has type "list (timestamped camera_time_seconds (displacement3d camera_imu_acs))", the resulting expression is simply of type "(timestamped camera_time_seconds (displacement3d camera_imu_acs))" -/ let imu_msg : timestamped camera_system_time_seconds (displacement3d camera_imu_acs) := imu_msgs.head in --ros::Time t(_ros_time_base.toSec() + imu_msg.header.stamp.toSec()); /- The developers now construct a new timestamp for the IMU message first by retrieving "base time" of the "platform"/local system time (which was computed earlier, specifically in the first call to this method (imu_callback_sync)), stored in variable "_ros_time_base", and then converting its value into seconds along with extracting its underlying coordinate via a "toSec" call. Next, they retrieve timestamp of imu_msg, stored as "imu_msg.header.stamp" in C++ or "imu_msg.timestamp" in our formalization, whose value is an alias of elapsed_camera_ms, which represents the "offset"/difference between the current hardware/camera timestamp and the "base" of the hardware/camera time (which, again, was computed specifically in the first call to this method) with the overall expression expressed in seconds. The coordinates of this object are extracted via the "toSec" call. These two coordinates are added together and used as an argument in the construction of the ros::Time object. We've formalized this by interpreting t as a time expressed in the hardware_time_acs. We bind a value to it by constructing a new value of type "time camera_time_seconds" via our mk_time call. The complexity resides in how we compute the coordinates to the new time. We define an overload of the "toSec" call for both _ros_time_base and imu_msg.timestamp, whether to provide a global or context-dependent interpretation for toSec is a nuanced issue. Regardless, both overloads of "toSec", "_ros_time_base_toSec" and "_imu_msg_timestamp_toSec", simply extract the respective coordinates of _ros_time_base and _imu_msg_timestamp. Thus, the respective overloads are applied using the respective values, _ros_time_base and _imu_msg_timestamp, and the resulting expressions, which have type "scalar", are added together and supplied as an argument to the newly constructed time. Note here that, although there is no error here, there should be. The coordinates composing the addition operation hail from two different spaces: platform_time_acs and camera_time_seconds, which should yield a type error - as these coordinates hail from different ACSes. -/ let t : time camera_system_time_seconds := mk_time _ (( let _ros_time_base_toSec : time platform_time_in_seconds → scalar := λt, (t).coord in _ros_time_base_toSec _ros_time_base ) + ( --casting time to duration discussed --Whether or not to first convert "imu_msg.timestamp" from a time (point) to a duration (vector) should be confirmed by Dr. S --let imu_time_as_duration := mk_duration camera_time_seconds imu_msg.timestamp.coord in let _imu_msg_timestamp_toSec : time camera_system_time_seconds → scalar := λt, t.coord in _imu_msg_timestamp_toSec imu_msg.timestamp--imu_time_as_duration )) in /- Finally, we update the timestamp of imu_msg with our newly misconstructed time, t There is no error at this position. imu_msg.header.stamp = t; -/ let imu_msg0 : timestamped camera_system_time_seconds (displacement3d camera_imu_acs) := { timestamp := t, ..imu_msg } in punit.star /- As discussed above, a second version of this method is presented. The only difference is that, rather than "_camera_time_base" being in the camera_hardware_time_acs ACS and "dataframe.timestamp" domain being expressed in the camera_system_time_acs ACS, we have "_camera_time_base" being in the camera_global_time_acs ACS and "dataframe.timestamp" domain being expressed in the camera_hardware_time_acs ACS, which reflects an error where we have a "time dilation" discrepancy in the subtraction operation, rather than a massive origin mismatch. The reason, again, for maintaining two versions of the method is due to a limitation in our formalization, in that we can only express one execution path. Thus, describing a different execution path, in this case inputs with differing timestamp domains, must be formalized separately. Verbose comments are omitted from this version - as they only differ in terms of the ACS of the "dataframe" argument to the method (particularly, the ACS of the timestamp), as well as the _camera_time_base's type (particularly, the dependent type's ACS value) void BaseRealSenseNode::imu_callback_sync(rs2::frame dataframe, imu_sync_method sync_method) -/ def imu_callback_sync_v2 : timestamped camera_hardware_time_acs (displacement3d camera_imu_acs) → punit := λ dataframe, /- double frame_time = frame.get_timestamp(); -/ let dataframe_time := dataframe.timestamp in /-setBaseTime(frame_time, frame.get_frame_timestamp_domain()); _ros_time_base = ros::Time::now(); _camera_time_base = frame_time; -/ let _ros_time_base := mk_time platform_time_in_seconds August18thTwoFortyPMTimestamp in let _camera_time_base := mk_time camera_global_time_acs 10 in -- 10 is an arbitrary constant --double elapsed_camera_ms = (/*ms*/ frame_time - /*ms*/ _camera_time_base) / 1000.0; let elapsed_camera_ms : duration camera_hardware_time_acs := (camera_system_time_acs.mk_time_transform_to camera_system_time_seconds).transform_duration ((dataframe_time -ᵥ _camera_time_base : duration camera_system_time_acs)) in /- auto crnt_reading = *(reinterpret_cast<const float3*>(frame.get_data())); Eigen::Vector3d v(crnt_reading.x, crnt_reading.y, crnt_reading.z); -/ let crnt_reading : displacement3d camera_imu_acs := dataframe.value in let v : displacement3d camera_imu_acs := mk_displacement3d camera_imu_acs crnt_reading.x crnt_reading.y crnt_reading.z in --CimuData imu_data(stream_index, v, elapsed_camera_ms); let imu_data : timestamped camera_hardware_time_seconds (displacement3d camera_imu_acs) := ⟨elapsed_camera_ms, v⟩ in /- std::deque<sensor_msgs::Imu> imu_msgs; switch (sync_method) { case NONE: //Cannot really be NONE. Just to avoid compilation warning. case COPY: FillImuData_Copy(imu_data, imu_msgs); break; case LINEAR_INTERPOLATION: FillImuData_LinearInterpolation(imu_data, imu_msgs); break; } -/ let imu_msgs : list (timestamped camera_hardware_time_seconds (displacement3d camera_imu_acs)) := [imu_data] in /- while (imu_msgs.size()) { sensor_msgs::Imu imu_msg = imu_msgs.front(); -/ let imu_msg : timestamped camera_hardware_time_seconds (displacement3d camera_imu_acs) := imu_msgs.head in --ros::Time t(_ros_time_base.toSec() + imu_msg.header.stamp.toSec()); let t : time camera_hardware_time_seconds := mk_time _ (( let _ros_time_base_toSec : time platform_time_in_seconds → scalar := λt, (t).coord in _ros_time_base_toSec _ros_time_base ) + ( let _imu_msg_timestamp_toSec : time camera_hardware_time_seconds → scalar := λt, t.coord in _imu_msg_timestamp_toSec imu_msg.timestamp )) in /- imu_msg.header.stamp = t; -/ let imu_msg0 : timestamped camera_hardware_time_seconds (displacement3d camera_imu_acs) := { timestamp := t, ..imu_msg } in punit.star
04c6e233159c0696e820ccba35b0a15c8dd43eac
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/matchErrorMsg.lean
c6327bb1d46c8207d79602f798741947ff38fd55
[ "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
78
lean
def f (x : Nat × Nat) : Nat := match x with | (0, 0) => 1 | (x+1, _) => 2
a6a5ce67b4761b31ad1e7519101b50e45aa5c44e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/adjoin_root.lean
47253c25d6afdf27a26f269dd5d14b1e8d6a61eb
[]
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
8,733
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes Adjoining roots of polynomials -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.field_division import Mathlib.ring_theory.adjoin import Mathlib.ring_theory.principal_ideal_domain import Mathlib.linear_algebra.finite_dimensional import Mathlib.PostPort universes u v u_1 u_2 w namespace Mathlib /-! # Adjoining roots of polynomials This file defines the commutative ring `adjoin_root f`, the ring R[X]/(f) obtained from a commutative ring `R` and a polynomial `f : R[X]`. If furthermore `R` is a field and `f` is irreducible, the field structure on `adjoin_root f` is constructed. ## Main definitions and results The main definitions are in the `adjoin_root` namespace. * `mk f : polynomial R →+* adjoin_root f`, the natural ring homomorphism. * `of f : R →+* adjoin_root f`, the natural ring homomorphism. * `root f : adjoin_root f`, the image of X in R[X]/(f). * `lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S`, the ring homomorphism from R[X]/(f) to S extending `i : R →+* S` and sending `X` to `x`. * `lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S`, the algebra homomorphism from R[X]/(f) to S extending `algebra_map R S` and sending `X` to `x` * `equiv : (adjoin_root f →ₐ[F] E) ≃ {x // x ∈ (f.map (algebra_map F E)).roots}` a bijection between algebra homomorphisms from `adjoin_root` and roots of `f` in `S` -/ /-- Adjoin a root of a polynomial `f` to a commutative ring `R`. We define the new ring as the quotient of `R` by the principal ideal of `f`. -/ def adjoin_root {R : Type u} [comm_ring R] (f : polynomial R) := ideal.quotient (ideal.span (singleton f)) namespace adjoin_root protected instance comm_ring {R : Type u} [comm_ring R] (f : polynomial R) : comm_ring (adjoin_root f) := ideal.quotient.comm_ring (ideal.span (singleton f)) protected instance inhabited {R : Type u} [comm_ring R] (f : polynomial R) : Inhabited (adjoin_root f) := { default := 0 } protected instance decidable_eq {R : Type u} [comm_ring R] (f : polynomial R) : DecidableEq (adjoin_root f) := classical.dec_eq (adjoin_root f) /-- Ring homomorphism from `R[x]` to `adjoin_root f` sending `X` to the `root`. -/ def mk {R : Type u} [comm_ring R] (f : polynomial R) : polynomial R →+* adjoin_root f := ideal.quotient.mk (ideal.span (singleton f)) theorem induction_on {R : Type u} [comm_ring R] (f : polynomial R) {C : adjoin_root f → Prop} (x : adjoin_root f) (ih : ∀ (p : polynomial R), C (coe_fn (mk f) p)) : C x := quotient.induction_on' x ih /-- Embedding of the original ring `R` into `adjoin_root f`. -/ def of {R : Type u} [comm_ring R] (f : polynomial R) : R →+* adjoin_root f := ring_hom.comp (mk f) (ring_hom.of ⇑polynomial.C) protected instance algebra {R : Type u} [comm_ring R] (f : polynomial R) : algebra R (adjoin_root f) := ring_hom.to_algebra (of f) @[simp] theorem algebra_map_eq {R : Type u} [comm_ring R] (f : polynomial R) : algebra_map R (adjoin_root f) = of f := rfl /-- The adjoined root. -/ def root {R : Type u} [comm_ring R] (f : polynomial R) : adjoin_root f := coe_fn (mk f) polynomial.X protected instance adjoin_root.has_coe_t {R : Type u} [comm_ring R] {f : polynomial R} : has_coe_t R (adjoin_root f) := has_coe_t.mk ⇑(of f) @[simp] theorem mk_self {R : Type u} [comm_ring R] {f : polynomial R} : coe_fn (mk f) f = 0 := sorry @[simp] theorem mk_C {R : Type u} [comm_ring R] {f : polynomial R} (x : R) : coe_fn (mk f) (coe_fn polynomial.C x) = ↑x := rfl @[simp] theorem mk_X {R : Type u} [comm_ring R] {f : polynomial R} : coe_fn (mk f) polynomial.X = root f := rfl @[simp] theorem aeval_eq {R : Type u} [comm_ring R] {f : polynomial R} (p : polynomial R) : coe_fn (polynomial.aeval (root f)) p = coe_fn (mk f) p := sorry theorem adjoin_root_eq_top {R : Type u} [comm_ring R] {f : polynomial R} : algebra.adjoin R (singleton (root f)) = ⊤ := sorry @[simp] theorem eval₂_root {R : Type u} [comm_ring R] (f : polynomial R) : polynomial.eval₂ (of f) (root f) f = 0 := sorry theorem is_root_root {R : Type u} [comm_ring R] (f : polynomial R) : polynomial.is_root (polynomial.map (of f) f) (root f) := sorry /-- Lift a ring homomorphism `i : R →+* S` to `adjoin_root f →+* S`. -/ def lift {R : Type u} {S : Type v} [comm_ring R] {f : polynomial R} [comm_ring S] (i : R →+* S) (x : S) (h : polynomial.eval₂ i x f = 0) : adjoin_root f →+* S := ideal.quotient.lift (ideal.span (singleton f)) (polynomial.eval₂_ring_hom i x) sorry @[simp] theorem lift_mk {R : Type u} {S : Type v} [comm_ring R] {f : polynomial R} [comm_ring S] {i : R →+* S} {a : S} {h : polynomial.eval₂ i a f = 0} {g : polynomial R} : coe_fn (lift i a h) (coe_fn (mk f) g) = polynomial.eval₂ i a g := ideal.quotient.lift_mk (ideal.span (singleton f)) (polynomial.eval₂_ring_hom i a) (lift._proof_1 i a h) @[simp] theorem lift_root {R : Type u} {S : Type v} [comm_ring R] {f : polynomial R} [comm_ring S] {i : R →+* S} {a : S} {h : polynomial.eval₂ i a f = 0} : coe_fn (lift i a h) (root f) = a := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn (lift i a h) (root f) = a)) (root.equations._eqn_1 f))) (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn (lift i a h) (coe_fn (mk f) polynomial.X) = a)) lift_mk)) (eq.mpr (id (Eq._oldrec (Eq.refl (polynomial.eval₂ i a polynomial.X = a)) (polynomial.eval₂_X i a))) (Eq.refl a))) @[simp] theorem lift_of {R : Type u} {S : Type v} [comm_ring R] {f : polynomial R} [comm_ring S] {i : R →+* S} {a : S} {h : polynomial.eval₂ i a f = 0} {x : R} : coe_fn (lift i a h) ↑x = coe_fn i x := sorry @[simp] theorem lift_comp_of {R : Type u} {S : Type v} [comm_ring R] {f : polynomial R} [comm_ring S] {i : R →+* S} {a : S} {h : polynomial.eval₂ i a f = 0} : ring_hom.comp (lift i a h) (of f) = i := ring_hom.ext fun (_x : R) => lift_of /-- Produce an algebra homomorphism `adjoin_root f →ₐ[R] S` sending `root f` to a root of `f` in `S`. -/ def lift_hom {R : Type u} {S : Type v} [comm_ring R] (f : polynomial R) [comm_ring S] [algebra R S] (x : S) (hfx : coe_fn (polynomial.aeval x) f = 0) : alg_hom R (adjoin_root f) S := alg_hom.mk (ring_hom.to_fun (lift (algebra_map R S) x hfx)) sorry sorry sorry sorry sorry @[simp] theorem coe_lift_hom {R : Type u} {S : Type v} [comm_ring R] (f : polynomial R) [comm_ring S] [algebra R S] (x : S) (hfx : coe_fn (polynomial.aeval x) f = 0) : ↑(lift_hom f x hfx) = lift (algebra_map R S) x hfx := rfl @[simp] theorem aeval_alg_hom_eq_zero {R : Type u} {S : Type v} [comm_ring R] (f : polynomial R) [comm_ring S] [algebra R S] (ϕ : alg_hom R (adjoin_root f) S) : coe_fn (polynomial.aeval (coe_fn ϕ (root f))) f = 0 := sorry @[simp] theorem lift_hom_eq_alg_hom {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] [algebra R S] (f : polynomial R) (ϕ : alg_hom R (adjoin_root f) S) : lift_hom f (coe_fn ϕ (root f)) (aeval_alg_hom_eq_zero f ϕ) = ϕ := sorry /-- If `E` is a field extension of `F` and `f` is a polynomial over `F` then the set of maps from `F[x]/(f)` into `E` is in bijection with the set of roots of `f` in `E`. -/ def equiv (F : Type u_1) (E : Type u_2) [field F] [field E] [algebra F E] (f : polynomial F) (hf : f ≠ 0) : alg_hom F (adjoin_root f) E ≃ Subtype fun (x : E) => x ∈ polynomial.roots (polynomial.map (algebra_map F E) f) := equiv.mk (fun (ϕ : alg_hom F (adjoin_root f) E) => { val := coe_fn ϕ (root f), property := sorry }) (fun (x : Subtype fun (x : E) => x ∈ polynomial.roots (polynomial.map (algebra_map F E) f)) => lift_hom f ↑x sorry) sorry sorry protected instance is_maximal_span {K : Type w} [field K] {f : polynomial K} [irreducible f] : ideal.is_maximal (ideal.span (singleton f)) := principal_ideal_ring.is_maximal_of_irreducible _inst_2 protected instance field {K : Type w} [field K] {f : polynomial K} [irreducible f] : field (adjoin_root f) := field.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul sorry comm_ring.one sorry sorry sorry sorry sorry field.inv sorry sorry sorry theorem coe_injective {K : Type w} [field K] {f : polynomial K} [irreducible f] : function.injective coe := ring_hom.injective (of f) theorem mul_div_root_cancel {K : Type w} [field K] (f : polynomial K) [irreducible f] : (polynomial.X - coe_fn polynomial.C (root f)) * (polynomial.map (of f) f / (polynomial.X - coe_fn polynomial.C (root f))) = polynomial.map (of f) f := iff.mpr polynomial.mul_div_eq_iff_is_root (is_root_root f)
5e28849ca67ec105bec5e4079985ae6e23b442b9
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/analysis/normed_space/add_torsor.lean
c6f9a0630657759c65a9318e0faeac34c3467b8c
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
4,264
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Joseph Myers. -/ import algebra.add_torsor import topology.metric_space.isometry noncomputable theory /-! # Torsors of additive normed group actions. This file defines torsors of additive normed group actions, with a metric space structure. The motivating case is Euclidean affine spaces. -/ universes u v section prio set_option default_priority 100 -- see Note [default priority] /-- A `normed_add_torsor V P` is a torsor of an additive normed group action by a `normed_group V` on points `P`. We bundle the metric space structure and require the distance to be the same as results from the norm (which in fact implies the distance yields a metric space, but bundling just the distance and using an instance for the metric space results in type class problems). -/ class normed_add_torsor (V : out_param $ Type u) (P : Type v) [out_param $ normed_group V] [metric_space P] extends add_torsor V P := (dist_eq_norm' : ∀ (x y : P), dist x y = ∥(x -ᵥ y : V)∥) end prio variables (V : Type u) {P : Type v} [normed_group V] [metric_space P] [normed_add_torsor V P] include V /-- The distance equals the norm of subtracting two points. In this lemma, it is necessary to have `V` as an explicit argument; otherwise `rw dist_eq_norm_vsub` sometimes doesn't work. -/ lemma dist_eq_norm_vsub (x y : P) : dist x y = ∥(x -ᵥ y)∥ := normed_add_torsor.dist_eq_norm' x y variable {V} @[simp] lemma dist_vadd_cancel_left (v : V) (x y : P) : dist (v +ᵥ x) (v +ᵥ y) = dist x y := by rw [dist_eq_norm_vsub V, dist_eq_norm_vsub V, vadd_vsub_vadd_cancel_left] @[simp] lemma dist_vadd_cancel_right (v₁ v₂ : V) (x : P) : dist (v₁ +ᵥ x) (v₂ +ᵥ x) = dist v₁ v₂ := by rw [dist_eq_norm_vsub V, dist_eq_norm, vadd_vsub_vadd_cancel_right] /-- A `normed_group` is a `normed_add_torsor` over itself. -/ @[nolint instance_priority] -- false positive instance normed_group.normed_add_torsor (V : Type u) [normed_group V] : normed_add_torsor V V := { dist_eq_norm' := dist_eq_norm } /-- The distance defines a metric space structure on the torsor. This is not an instance because it depends on `V` to define a `metric_space P`. -/ def metric_space_of_normed_group_of_add_torsor (V : Type u) (P : Type v) [normed_group V] [add_torsor V P] : metric_space P := { dist := λ x y, ∥(x -ᵥ y : V)∥, dist_self := λ x, by simp, eq_of_dist_eq_zero := λ x y h, by simpa using h, dist_comm := λ x y, by simp only [←neg_vsub_eq_vsub_rev y x, norm_neg], dist_triangle := begin intros x y z, change ∥x -ᵥ z∥ ≤ ∥x -ᵥ y∥ + ∥y -ᵥ z∥, rw ←vsub_add_vsub_cancel, apply norm_add_le end } namespace isometric /-- The map `v ↦ v +ᵥ p` as an isometric equivalence between `V` and `P`. -/ def vadd_const (p : P) : V ≃ᵢ P := ⟨equiv.vadd_const V p, isometry_emetric_iff_metric.2 $ λ x₁ x₂, dist_vadd_cancel_right x₁ x₂ p⟩ @[simp] lemma coe_vadd_const (p : P) : ⇑(vadd_const p) = λ v, v +ᵥ p := rfl @[simp] lemma coe_vadd_const_symm (p : P) : ⇑(vadd_const p).symm = λ p', p' -ᵥ p := rfl @[simp] lemma vadd_const_to_equiv (p : P) : (vadd_const p).to_equiv = equiv.vadd_const V p := rfl variables (P) /-- The map `p ↦ v +ᵥ p` as an isometric automorphism of `P`. -/ def const_vadd (v : V) : P ≃ᵢ P := ⟨equiv.const_vadd P v, isometry_emetric_iff_metric.2 $ dist_vadd_cancel_left v⟩ @[simp] lemma coe_const_vadd (v : V) : ⇑(const_vadd P v) = (+ᵥ) v := rfl variable (V) @[simp] lemma const_vadd_zero : const_vadd P (0:V) = isometric.refl P := isometric.to_equiv_inj $ equiv.const_vadd_zero V P end isometric variables {V' : Type*} {P' : Type*} [normed_group V'] [metric_space P'] [normed_add_torsor V' P'] /-- The map `g` from `V1` to `V2` corresponding to a map `f` from `P1` to `P2`, at a base point `p`, is an isometry if `f` is one. -/ lemma isometry.vadd_vsub {f : P → P'} (hf : isometry f) {p : P} {g : V → V'} (hg : ∀ v, g v = f (v +ᵥ p) -ᵥ f p) : isometry g := begin convert (isometric.vadd_const (f p)).symm.isometry.comp (hf.comp (isometric.vadd_const p).isometry), exact funext hg end
1510c1cfcc09f5d13b8221fba4925756fe247bce
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed_space/hahn_banach/extension.lean
accee48ac378f93211fd171554ee76fab0c364ea
[ "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
6,787
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth -/ import analysis.convex.cone import analysis.normed_space.is_R_or_C import analysis.normed_space.extend /-! # Extension Hahn-Banach theorem In this file we prove the analytic Hahn-Banach theorem. For any continuous linear function on a subspace, we can extend it to a function on the entire space without changing its norm. We prove * `real.exists_extension_norm_eq`: Hahn-Banach theorem for continuous linear functions on normed spaces over `ℝ`. * `exists_extension_norm_eq`: Hahn-Banach theorem for continuous linear functions on normed spaces over `ℝ` or `ℂ`. In order to state and prove the corollaries uniformly, we prove the statements for a field `𝕜` satisfying `is_R_or_C 𝕜`. In this setting, `exists_dual_vector` states that, for any nonzero `x`, there exists a continuous linear form `g` of norm `1` with `g x = ∥x∥` (where the norm has to be interpreted as an element of `𝕜`). -/ universes u v namespace real variables {E : Type*} [semi_normed_group E] [normed_space ℝ E] /-- Hahn-Banach theorem for continuous linear functions over `ℝ`. -/ theorem exists_extension_norm_eq (p : subspace ℝ E) (f : p →L[ℝ] ℝ) : ∃ g : E →L[ℝ] ℝ, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ := begin rcases exists_extension_of_le_sublinear ⟨p, f⟩ (λ x, ∥f∥ * ∥x∥) (λ c hc x, by simp only [norm_smul c x, real.norm_eq_abs, abs_of_pos hc, mul_left_comm]) (λ x y, _) (λ x, le_trans (le_abs_self _) (f.le_op_norm _)) with ⟨g, g_eq, g_le⟩, set g' := g.mk_continuous (∥f∥) (λ x, abs_le.2 ⟨neg_le.1 $ g.map_neg x ▸ norm_neg x ▸ g_le (-x), g_le x⟩), { refine ⟨g', g_eq, _⟩, { apply le_antisymm (g.mk_continuous_norm_le (norm_nonneg f) _), refine f.op_norm_le_bound (norm_nonneg _) (λ x, _), dsimp at g_eq, rw ← g_eq, apply g'.le_op_norm } }, { simp only [← mul_add], exact mul_le_mul_of_nonneg_left (norm_add_le x y) (norm_nonneg f) } end end real section is_R_or_C open is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {F : Type*} [semi_normed_group F] [normed_space 𝕜 F] /-- Hahn-Banach theorem for continuous linear functions over `𝕜` satisyfing `is_R_or_C 𝕜`. -/ theorem exists_extension_norm_eq (p : subspace 𝕜 F) (f : p →L[𝕜] 𝕜) : ∃ g : F →L[𝕜] 𝕜, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ := begin letI : module ℝ F := restrict_scalars.module ℝ 𝕜 F, letI : is_scalar_tower ℝ 𝕜 F := restrict_scalars.is_scalar_tower _ _ _, letI : normed_space ℝ F := normed_space.restrict_scalars _ 𝕜 _, -- Let `fr: p →L[ℝ] ℝ` be the real part of `f`. let fr := re_clm.comp (f.restrict_scalars ℝ), have fr_apply : ∀ x, fr x = re (f x), by { assume x, refl }, -- Use the real version to get a norm-preserving extension of `fr`, which -- we'll call `g : F →L[ℝ] ℝ`. rcases real.exists_extension_norm_eq (p.restrict_scalars ℝ) fr with ⟨g, ⟨hextends, hnormeq⟩⟩, -- Now `g` can be extended to the `F →L[𝕜] 𝕜` we need. refine ⟨g.extend_to_𝕜, _⟩, -- It is an extension of `f`. have h : ∀ x : p, g.extend_to_𝕜 x = f x, { assume x, rw [continuous_linear_map.extend_to_𝕜_apply, ←submodule.coe_smul, hextends, hextends], have : (fr x : 𝕜) - I * ↑(fr (I • x)) = (re (f x) : 𝕜) - (I : 𝕜) * (re (f ((I : 𝕜) • x))), by refl, rw this, apply ext, { simp only [add_zero, algebra.id.smul_eq_mul, I_re, of_real_im, add_monoid_hom.map_add, zero_sub, I_im', zero_mul, of_real_re, eq_self_iff_true, sub_zero, mul_neg, of_real_neg, mul_re, mul_zero, sub_neg_eq_add, continuous_linear_map.map_smul] }, { simp only [algebra.id.smul_eq_mul, I_re, of_real_im, add_monoid_hom.map_add, zero_sub, I_im', zero_mul, of_real_re, mul_neg, mul_im, zero_add, of_real_neg, mul_re, sub_neg_eq_add, continuous_linear_map.map_smul] } }, -- And we derive the equality of the norms by bounding on both sides. refine ⟨h, le_antisymm _ _⟩, { calc ∥g.extend_to_𝕜∥ ≤ ∥g∥ : g.extend_to_𝕜.op_norm_le_bound g.op_norm_nonneg (norm_bound _) ... = ∥fr∥ : hnormeq ... ≤ ∥re_clm∥ * ∥f∥ : continuous_linear_map.op_norm_comp_le _ _ ... = ∥f∥ : by rw [re_clm_norm, one_mul] }, { exact f.op_norm_le_bound g.extend_to_𝕜.op_norm_nonneg (λ x, h x ▸ g.extend_to_𝕜.le_op_norm x) } end end is_R_or_C section dual_vector variables (𝕜 : Type v) [is_R_or_C 𝕜] variables {E : Type u} [normed_group E] [normed_space 𝕜 E] open continuous_linear_equiv submodule open_locale classical lemma coord_norm' {x : E} (h : x ≠ 0) : ∥(∥x∥ : 𝕜) • coord 𝕜 x h∥ = 1 := by rw [norm_smul, is_R_or_C.norm_coe_norm, coord_norm, mul_inv_cancel (mt norm_eq_zero.mp h)] /-- Corollary of Hahn-Banach. Given a nonzero element `x` of a normed space, there exists an element of the dual space, of norm `1`, whose value on `x` is `∥x∥`. -/ theorem exists_dual_vector (x : E) (h : x ≠ 0) : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g x = ∥x∥ := begin let p : submodule 𝕜 E := 𝕜 ∙ x, let f := (∥x∥ : 𝕜) • coord 𝕜 x h, obtain ⟨g, hg⟩ := exists_extension_norm_eq p f, refine ⟨g, _, _⟩, { rw [hg.2, coord_norm'] }, { calc g x = g (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) : by rw coe_mk ... = ((∥x∥ : 𝕜) • coord 𝕜 x h) (⟨x, mem_span_singleton_self x⟩ : 𝕜 ∙ x) : by rw ← hg.1 ... = ∥x∥ : by simp } end /-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, and choosing the dual element arbitrarily when `x = 0`. -/ theorem exists_dual_vector' [nontrivial E] (x : E) : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g x = ∥x∥ := begin by_cases hx : x = 0, { obtain ⟨y, hy⟩ := exists_ne (0 : E), obtain ⟨g, hg⟩ : ∃ g : E →L[𝕜] 𝕜, ∥g∥ = 1 ∧ g y = ∥y∥ := exists_dual_vector 𝕜 y hy, refine ⟨g, hg.left, _⟩, simp [hx] }, { exact exists_dual_vector 𝕜 x hx } end /-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, but only ensuring that the dual element has norm at most `1` (this can not be improved for the trivial vector space). -/ theorem exists_dual_vector'' (x : E) : ∃ g : E →L[𝕜] 𝕜, ∥g∥ ≤ 1 ∧ g x = ∥x∥ := begin by_cases hx : x = 0, { refine ⟨0, by simp, _⟩, symmetry, simp [hx], }, { rcases exists_dual_vector 𝕜 x hx with ⟨g, g_norm, g_eq⟩, exact ⟨g, g_norm.le, g_eq⟩ } end end dual_vector
598e4828f10542c664860bbbbb6d9670f302af42
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/valuation/basic_auto.lean
088b697aab61825036289423c49e621a417f2f98
[]
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
19,501
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Johan Commelin, Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.linear_ordered_comm_group_with_zero import Mathlib.algebra.group_power.default import Mathlib.ring_theory.ideal.operations import Mathlib.algebra.punit_instances import Mathlib.PostPort universes u_1 u_2 l u_3 u_4 namespace Mathlib /-! # The basics of valuation theory. The basic theory of valuations (non-archimedean norms) on a commutative ring, following T. Wedhorn's unpublished notes “Adic Spaces” ([wedhorn_adic]). The definition of a valuation we use here is Definition 1.22 of [wedhorn_adic]. A valuation on a ring `R` is a monoid homomorphism `v` to a linearly ordered commutative group with zero, that in addition satisfies the following two axioms: * `v 0 = 0` * `∀ x y, v (x + y) ≤ max (v x) (v y)` `valuation R Γ₀`is the type of valuations `R → Γ₀`, with a coercion to the underlying function. If `v` is a valuation from `R` to `Γ₀` then the induced group homomorphism `units(R) → Γ₀` is called `unit_map v`. The equivalence "relation" `is_equiv v₁ v₂ : Prop` defined in 1.27 of [wedhorn_adic] is not strictly speaking a relation, because `v₁ : valuation R Γ₁` and `v₂ : valuation R Γ₂` might not have the same type. This corresponds in ZFC to the set-theoretic difficulty that the class of all valuations (as `Γ₀` varies) on a ring `R` is not a set. The "relation" is however reflexive, symmetric and transitive in the obvious sense. Note that we use 1.27(iii) of [wedhorn_adic] as the definition of equivalence. The support of a valuation `v : valuation R Γ₀` is `supp v`. If `J` is an ideal of `R` with `h : J ⊆ supp v` then the induced valuation on R / J = `ideal.quotient J` is `on_quot v h`. ## Main definitions * `valuation R Γ₀`, the type of valuations on `R` with values in `Γ₀` * `valuation.is_equiv`, the heterogeneous equivalence relation on valuations * `valuation.supp`, the support of a valuation -/ -- universes u u₀ u₁ u₂ -- v is used for valuations /-- The type of Γ₀-valued valuations on R. -/ structure valuation (R : Type u_1) (Γ₀ : Type u_2) [linear_ordered_comm_group_with_zero Γ₀] [ring R] extends monoid_with_zero_hom R Γ₀ where map_add' : ∀ (x y : R), to_fun (x + y) ≤ max (to_fun x) (to_fun y) /-- The `monoid_with_zero_hom` underlying a valuation. -/ namespace valuation /-- A valuation is coerced to the underlying function R → Γ₀. -/ protected instance has_coe_to_fun (R : Type u_1) (Γ₀ : Type u_2) [linear_ordered_comm_group_with_zero Γ₀] [ring R] : has_coe_to_fun (valuation R Γ₀) := has_coe_to_fun.mk (fun (_x : valuation R Γ₀) => R → Γ₀) to_fun /-- A valuation is coerced to a monoid morphism R → Γ₀. -/ protected instance monoid_with_zero_hom.has_coe (R : Type u_1) (Γ₀ : Type u_2) [linear_ordered_comm_group_with_zero Γ₀] [ring R] : has_coe (valuation R Γ₀) (monoid_with_zero_hom R Γ₀) := has_coe.mk to_monoid_with_zero_hom @[simp] theorem coe_coe {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) : ⇑↑v = ⇑v := rfl @[simp] theorem map_zero {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) : coe_fn v 0 = 0 := map_zero' v @[simp] theorem map_one {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) : coe_fn v 1 = 1 := map_one' v @[simp] theorem map_mul {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : R) (y : R) : coe_fn v (x * y) = coe_fn v x * coe_fn v y := map_mul' v @[simp] theorem map_add {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : R) (y : R) : coe_fn v (x + y) ≤ max (coe_fn v x) (coe_fn v y) := map_add' v theorem map_add_le {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {x : R} {y : R} {g : Γ₀} (hx : coe_fn v x ≤ g) (hy : coe_fn v y ≤ g) : coe_fn v (x + y) ≤ g := le_trans (map_add v x y) (max_le hx hy) theorem map_add_lt {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {x : R} {y : R} {g : Γ₀} (hx : coe_fn v x < g) (hy : coe_fn v y < g) : coe_fn v (x + y) < g := lt_of_le_of_lt (map_add v x y) (max_lt hx hy) theorem map_sum_le {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {ι : Type u_3} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ (i : ι), i ∈ s → coe_fn v (f i) ≤ g) : coe_fn v (finset.sum s fun (i : ι) => f i) ≤ g := sorry theorem map_sum_lt {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {ι : Type u_3} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ 0) (hf : ∀ (i : ι), i ∈ s → coe_fn v (f i) < g) : coe_fn v (finset.sum s fun (i : ι) => f i) < g := sorry theorem map_sum_lt' {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {ι : Type u_3} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : 0 < g) (hf : ∀ (i : ι), i ∈ s → coe_fn v (f i) < g) : coe_fn v (finset.sum s fun (i : ι) => f i) < g := map_sum_lt v (ne_of_gt hg) hf @[simp] theorem map_pow {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : R) (n : ℕ) : coe_fn v (x ^ n) = coe_fn v x ^ n := monoid_hom.map_pow (monoid_with_zero_hom.to_monoid_hom (to_monoid_with_zero_hom v)) theorem ext {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ₀} (h : ∀ (r : R), coe_fn v₁ r = coe_fn v₂ r) : v₁ = v₂ := sorry theorem ext_iff {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ₀} : v₁ = v₂ ↔ ∀ (r : R), coe_fn v₁ r = coe_fn v₂ r := { mp := fun (h : v₁ = v₂) (r : R) => congr_arg (fun {v₁ : valuation R Γ₀} => coe_fn v₁ r) h, mpr := ext } -- The following definition is not an instance, because we have more than one `v` on a given `R`. -- In addition, type class inference would not be able to infer `v`. /-- A valuation gives a preorder on the underlying ring. -/ def to_preorder {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) : preorder R := preorder.lift ⇑v /-- If `v` is a valuation on a division ring then `v(x) = 0` iff `x = 0`. -/ @[simp] theorem zero_iff {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {K : Type u_1} [division_ring K] (v : valuation K Γ₀) {x : K} : coe_fn v x = 0 ↔ x = 0 := monoid_with_zero_hom.map_eq_zero (to_monoid_with_zero_hom v) theorem ne_zero_iff {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {K : Type u_1} [division_ring K] (v : valuation K Γ₀) {x : K} : coe_fn v x ≠ 0 ↔ x ≠ 0 := monoid_with_zero_hom.map_ne_zero (to_monoid_with_zero_hom v) @[simp] theorem map_inv {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {K : Type u_1} [division_ring K] (v : valuation K Γ₀) {x : K} : coe_fn v (x⁻¹) = (coe_fn v x⁻¹) := monoid_with_zero_hom.map_inv' (to_monoid_with_zero_hom v) x theorem map_units_inv {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : units R) : coe_fn v ↑(x⁻¹) = (coe_fn v ↑x⁻¹) := monoid_hom.map_units_inv (monoid_with_zero_hom.to_monoid_hom (to_monoid_with_zero_hom v)) x theorem unit_map_eq {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (u : units R) : ↑(coe_fn (units.map ↑v) u) = coe_fn v ↑u := rfl @[simp] theorem map_neg {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : R) : coe_fn v (-x) = coe_fn v x := monoid_hom.map_neg (monoid_with_zero_hom.to_monoid_hom (to_monoid_with_zero_hom v)) x theorem map_sub_swap {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : R) (y : R) : coe_fn v (x - y) = coe_fn v (y - x) := monoid_hom.map_sub_swap (monoid_with_zero_hom.to_monoid_hom (to_monoid_with_zero_hom v)) x y theorem map_sub_le_max {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) (x : R) (y : R) : coe_fn v (x - y) ≤ max (coe_fn v x) (coe_fn v y) := sorry theorem map_add_of_distinct_val {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {x : R} {y : R} (h : coe_fn v x ≠ coe_fn v y) : coe_fn v (x + y) = max (coe_fn v x) (coe_fn v y) := sorry theorem map_eq_of_sub_lt {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {x : R} {y : R} (h : coe_fn v (y - x) < coe_fn v x) : coe_fn v y = coe_fn v x := sorry /-- A ring homomorphism S → R induces a map valuation R Γ₀ → valuation S Γ₀ -/ def comap {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] {S : Type u_3} [ring S] (f : S →+* R) (v : valuation R Γ₀) : valuation S Γ₀ := mk (⇑v ∘ ⇑f) sorry sorry sorry sorry @[simp] theorem comap_id {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) : comap (ring_hom.id R) v = v := ext fun (r : R) => rfl theorem comap_comp {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] (v : valuation R Γ₀) {S₁ : Type u_3} {S₂ : Type u_4} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) : comap (ring_hom.comp g f) v = comap f (comap g v) := ext fun (r : S₁) => rfl /-- A ≤-preserving group homomorphism Γ₀ → Γ'₀ induces a map valuation R Γ₀ → valuation R Γ'₀. -/ def map {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] (f : monoid_with_zero_hom Γ₀ Γ'₀) (hf : monotone ⇑f) (v : valuation R Γ₀) : valuation R Γ'₀ := mk (⇑f ∘ ⇑v) sorry sorry sorry sorry /-- Two valuations on R are defined to be equivalent if they induce the same preorder on R. -/ def is_equiv {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] (v₁ : valuation R Γ₀) (v₂ : valuation R Γ'₀) := ∀ (r s : R), coe_fn v₁ r ≤ coe_fn v₁ s ↔ coe_fn v₂ r ≤ coe_fn v₂ s namespace is_equiv theorem refl {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] {v : valuation R Γ₀} : is_equiv v v := fun (_x _x_1 : R) => iff.refl (coe_fn v _x ≤ coe_fn v _x_1) theorem symm {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} (h : is_equiv v₁ v₂) : is_equiv v₂ v₁ := fun (_x _x_1 : R) => iff.symm (h _x _x_1) theorem trans {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] {Γ''₀ : Type u_4} [linear_ordered_comm_group_with_zero Γ''₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} {v₃ : valuation R Γ''₀} (h₁₂ : is_equiv v₁ v₂) (h₂₃ : is_equiv v₂ v₃) : is_equiv v₁ v₃ := fun (_x _x_1 : R) => iff.trans (h₁₂ _x _x_1) (h₂₃ _x _x_1) theorem of_eq {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [ring R] {v : valuation R Γ₀} {v' : valuation R Γ₀} (h : v = v') : is_equiv v v' := Eq._oldrec refl h theorem map {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] {v : valuation R Γ₀} {v' : valuation R Γ₀} (f : monoid_with_zero_hom Γ₀ Γ'₀) (hf : monotone ⇑f) (inf : function.injective ⇑f) (h : is_equiv v v') : is_equiv (map f hf v) (map f hf v') := sorry /-- `comap` preserves equivalence. -/ theorem comap {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} {S : Type u_4} [ring S] (f : S →+* R) (h : is_equiv v₁ v₂) : is_equiv (comap f v₁) (comap f v₂) := fun (r s : S) => h (coe_fn f r) (coe_fn f s) theorem val_eq {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} (h : is_equiv v₁ v₂) {r : R} {s : R} : coe_fn v₁ r = coe_fn v₁ s ↔ coe_fn v₂ r = coe_fn v₂ s := sorry theorem ne_zero {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} (h : is_equiv v₁ v₂) {r : R} : coe_fn v₁ r ≠ 0 ↔ coe_fn v₂ r ≠ 0 := eq.mp (Eq._oldrec (Eq.refl (coe_fn v₁ r ≠ 0 ↔ coe_fn v₂ r ≠ coe_fn v₂ 0)) (map_zero v₂)) (eq.mp (Eq._oldrec (Eq.refl (coe_fn v₁ r ≠ coe_fn v₁ 0 ↔ coe_fn v₂ r ≠ coe_fn v₂ 0)) (map_zero v₁)) (not_iff_not_of_iff (val_eq h))) theorem Mathlib.valuation.is_equiv_of_map_strict_mono {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] [ring R] {v : valuation R Γ₀} (f : monoid_with_zero_hom Γ₀ Γ'₀) (H : strict_mono ⇑f) : is_equiv (map f (strict_mono.monotone H) v) v := fun (x y : R) => { mp := iff.mp (strict_mono.le_iff_le H), mpr := fun (h : coe_fn v x ≤ coe_fn v y) => strict_mono.monotone H h } theorem Mathlib.valuation.is_equiv_of_val_le_one {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] {Γ'₀ : Type u_3} [linear_ordered_comm_group_with_zero Γ'₀] {K : Type u_1} [division_ring K] (v : valuation K Γ₀) (v' : valuation K Γ'₀) (h : ∀ {x : K}, coe_fn v x ≤ 1 ↔ coe_fn v' x ≤ 1) : is_equiv v v' := sorry /-- The support of a valuation `v : R → Γ₀` is the ideal of `R` where `v` vanishes. -/ def Mathlib.valuation.supp {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) : ideal R := submodule.mk (set_of fun (x : R) => coe_fn v x = 0) sorry sorry sorry -- @[simp] lemma mem_supp_iff' (x : R) : x ∈ (supp v : set R) ↔ v x = 0 := iff.rfl @[simp] theorem Mathlib.valuation.mem_supp_iff {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) (x : R) : x ∈ supp v ↔ coe_fn v x = 0 := iff.rfl /-- The support of a valuation is a prime ideal. -/ protected instance Mathlib.valuation.supp.ideal.is_prime {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) : ideal.is_prime (supp v) := { left := fun (h : supp v = ⊤) => one_ne_zero ((fun (this : 1 = 0) => this) (Eq.trans (Eq.symm (map_one v)) ((fun (this : 1 ∈ supp v) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (1 ∈ supp v)) h)) trivial)))), right := fun (x y : R) (hxy : x * y ∈ supp v) => id (id (fun (hxy : coe_fn v (x * y) = 0) => eq_zero_or_eq_zero_of_mul_eq_zero (eq.mp (Eq._oldrec (Eq.refl (coe_fn v (x * y) = 0)) (map_mul v x y)) hxy)) hxy) } theorem Mathlib.valuation.map_add_supp {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) (a : R) {s : R} (h : s ∈ supp v) : coe_fn v (a + s) = coe_fn v a := sorry /-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function. Note: it's just the function; the valuation is `on_quot hJ`. -/ def Mathlib.valuation.on_quot_val {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) {J : ideal R} (hJ : J ≤ supp v) : ideal.quotient J → Γ₀ := fun (q : ideal.quotient J) => quotient.lift_on' q ⇑v sorry /-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/ def Mathlib.valuation.on_quot {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) {J : ideal R} (hJ : J ≤ supp v) : valuation (ideal.quotient J) Γ₀ := mk (on_quot_val v hJ) sorry sorry sorry sorry @[simp] theorem Mathlib.valuation.on_quot_comap_eq {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) {J : ideal R} (hJ : J ≤ supp v) : comap (ideal.quotient.mk J) (on_quot v hJ) = v := sorry theorem Mathlib.valuation.comap_supp {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) {S : Type u_3} [comm_ring S] (f : S →+* R) : supp (comap f v) = ideal.comap f (supp v) := sorry theorem Mathlib.valuation.self_le_supp_comap {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (J : ideal R) (v : valuation (ideal.quotient J) Γ₀) : J ≤ supp (comap (ideal.quotient.mk J) v) := sorry @[simp] theorem Mathlib.valuation.comap_on_quot_eq {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (J : ideal R) (v : valuation (ideal.quotient J) Γ₀) : on_quot (comap (ideal.quotient.mk J) v) (self_le_supp_comap J v) = v := sorry /-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/ theorem Mathlib.valuation.supp_quot {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) {J : ideal R} (hJ : J ≤ supp v) : supp (on_quot v hJ) = ideal.map (ideal.quotient.mk J) (supp v) := sorry theorem Mathlib.valuation.supp_quot_supp {R : Type u_1} {Γ₀ : Type u_2} [linear_ordered_comm_group_with_zero Γ₀] [comm_ring R] (v : valuation R Γ₀) : supp (on_quot v (le_refl (supp v))) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (supp (on_quot v (le_refl (supp v))) = 0)) (supp_quot v (le_refl (supp v))))) (ideal.map_quotient_self (supp v)) end Mathlib
fa3ac634e779d7c7c5a4b718a4652b1484a33a2a
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed_space/exponential.lean
dfd830406afcd0101a90d464b2ac0f0f4b965a2c
[ "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
26,250
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Eric Wieser -/ import analysis.specific_limits.basic import analysis.analytic.basic import analysis.complex.basic import data.nat.choose.cast import data.finset.noncomm_prod /-! # Exponential in a Banach algebra In this file, we define `exp 𝕂 : 𝔸 → 𝔸`, the exponential map in a topological algebra `𝔸` over a field `𝕂`. While for most interesting results we need `𝔸` to be normed algebra, we do not require this in the definition in order to make `exp` independent of a particular choice of norm. The definition also does not require that `𝔸` be complete, but we need to assume it for most results. We then prove some basic results, but we avoid importing derivatives here to minimize dependencies. Results involving derivatives and comparisons with `real.exp` and `complex.exp` can be found in `analysis/special_functions/exponential`. ## Main results We prove most result for an arbitrary field `𝕂`, and then specialize to `𝕂 = ℝ` or `𝕂 = ℂ`. ### General case - `exp_add_of_commute_of_mem_ball` : if `𝕂` has characteristic zero, then given two commuting elements `x` and `y` in the disk of convergence, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_add_of_mem_ball` : if `𝕂` has characteristic zero and `𝔸` is commutative, then given two elements `x` and `y` in the disk of convergence, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_neg_of_mem_ball` : if `𝕂` has characteristic zero and `𝔸` is a division ring, then given an element `x` in the disk of convergence, we have `exp 𝕂 (-x) = (exp 𝕂 x)⁻¹`. ### `𝕂 = ℝ` or `𝕂 = ℂ` - `exp_series_radius_eq_top` : the `formal_multilinear_series` defining `exp 𝕂` has infinite radius of convergence - `exp_add_of_commute` : given two commuting elements `x` and `y`, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_add` : if `𝔸` is commutative, then we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` for any `x` and `y` - `exp_neg` : if `𝔸` is a division ring, then we have `exp 𝕂 (-x) = (exp 𝕂 x)⁻¹`. - `exp_sum_of_commute` : the analogous result to `exp_add_of_commute` for `finset.sum`. - `exp_sum` : the analogous result to `exp_add` for `finset.sum`. - `exp_nsmul` : repeated addition in the domain corresponds to repeated multiplication in the codomain. - `exp_zsmul` : repeated addition in the domain corresponds to repeated multiplication in the codomain. ### Other useful compatibility results - `exp_eq_exp` : if `𝔸` is a normed algebra over two fields `𝕂` and `𝕂'`, then `exp 𝕂 = exp 𝕂' 𝔸` -/ open filter is_R_or_C continuous_multilinear_map normed_field asymptotics open_locale nat topological_space big_operators ennreal section topological_algebra variables (𝕂 𝔸 : Type*) [field 𝕂] [ring 𝔸] [algebra 𝕂 𝔸] [topological_space 𝔸] [topological_ring 𝔸] /-- `exp_series 𝕂 𝔸` is the `formal_multilinear_series` whose `n`-th term is the map `(xᵢ) : 𝔸ⁿ ↦ (1/n! : 𝕂) • ∏ xᵢ`. Its sum is the exponential map `exp 𝕂 : 𝔸 → 𝔸`. -/ def exp_series : formal_multilinear_series 𝕂 𝔸 𝔸 := λ n, (n!⁻¹ : 𝕂) • continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸 variables {𝔸} /-- `exp 𝕂 : 𝔸 → 𝔸` is the exponential map determined by the action of `𝕂` on `𝔸`. It is defined as the sum of the `formal_multilinear_series` `exp_series 𝕂 𝔸`. Note that when `𝔸 = matrix n n 𝕂`, this is the **Matrix Exponential**; see [`analysis.normed_space.matrix_exponential`](../matrix_exponential) for lemmas specific to that case. -/ noncomputable def exp (x : 𝔸) : 𝔸 := (exp_series 𝕂 𝔸).sum x variables {𝕂} lemma exp_series_apply_eq (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = (n!⁻¹ : 𝕂) • x^n := by simp [exp_series] lemma exp_series_apply_eq' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, (n!⁻¹ : 𝕂) • x^n) := funext (exp_series_apply_eq x) lemma exp_series_sum_eq (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n := tsum_congr (λ n, exp_series_apply_eq x n) lemma exp_eq_tsum : exp 𝕂 = (λ x : 𝔸, ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n) := funext exp_series_sum_eq @[simp] lemma exp_zero [t2_space 𝔸] : exp 𝕂 (0 : 𝔸) = 1 := begin suffices : (λ x : 𝔸, ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n) 0 = ∑' (n : ℕ), if n = 0 then 1 else 0, { have key : ∀ n ∉ ({0} : finset ℕ), (if n = 0 then (1 : 𝔸) else 0) = 0, from λ n hn, if_neg (finset.not_mem_singleton.mp hn), rw [exp_eq_tsum, this, tsum_eq_sum key, finset.sum_singleton], simp }, refine tsum_congr (λ n, _), split_ifs with h h; simp [h] end @[simp] lemma exp_op [t2_space 𝔸] (x : 𝔸) : exp 𝕂 (mul_opposite.op x) = mul_opposite.op (exp 𝕂 x) := by simp_rw [exp, exp_series_sum_eq, ←mul_opposite.op_pow, ←mul_opposite.op_smul, tsum_op] @[simp] lemma exp_unop [t2_space 𝔸] (x : 𝔸ᵐᵒᵖ) : exp 𝕂 (mul_opposite.unop x) = mul_opposite.unop (exp 𝕂 x) := by simp_rw [exp, exp_series_sum_eq, ←mul_opposite.unop_pow, ←mul_opposite.unop_smul, tsum_unop] lemma star_exp [t2_space 𝔸] [star_ring 𝔸] [has_continuous_star 𝔸] (x : 𝔸) : star (exp 𝕂 x) = exp 𝕂 (star x) := by simp_rw [exp_eq_tsum, ←star_pow, ←star_inv_nat_cast_smul, ←tsum_star] variables (𝕂) lemma commute.exp_right [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute x (exp 𝕂 y) := begin rw exp_eq_tsum, exact commute.tsum_right x (λ n, (h.pow_right n).smul_right _), end lemma commute.exp_left [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute (exp 𝕂 x) y := (h.symm.exp_right 𝕂).symm lemma commute.exp [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute (exp 𝕂 x) (exp 𝕂 y) := (h.exp_left _).exp_right _ end topological_algebra section topological_division_algebra variables {𝕂 𝔸 : Type*} [field 𝕂] [division_ring 𝔸] [algebra 𝕂 𝔸] [topological_space 𝔸] [topological_ring 𝔸] lemma exp_series_apply_eq_div (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = x^n / n! := by rw [div_eq_mul_inv, ←(nat.cast_commute n! (x ^ n)).inv_left₀.eq, ←smul_eq_mul, exp_series_apply_eq, inv_nat_cast_smul_eq _ _ _ _] lemma exp_series_apply_eq_div' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, x^n / n!) := funext (exp_series_apply_eq_div x) lemma exp_series_sum_eq_div (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), x^n / n! := tsum_congr (exp_series_apply_eq_div x) lemma exp_eq_tsum_div : exp 𝕂 = (λ x : 𝔸, ∑' (n : ℕ), x^n / n!) := funext exp_series_sum_eq_div end topological_division_algebra section normed section any_field_any_algebra variables {𝕂 𝔸 𝔹 : Type*} [nondiscrete_normed_field 𝕂] variables [normed_ring 𝔸] [normed_ring 𝔹] [normed_algebra 𝕂 𝔸] [normed_algebra 𝕂 𝔹] lemma norm_exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥exp_series 𝕂 𝔸 n (λ _, x)∥) := (exp_series 𝕂 𝔸).summable_norm_apply hx lemma norm_exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥(n!⁻¹ : 𝕂) • x^n∥) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq', exact norm_exp_series_summable_of_mem_ball x hx end section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball x hx) lemma exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, (n!⁻¹ : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) lemma exp_series_has_sum_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 x) := formal_multilinear_series.has_sum (exp_series 𝕂 𝔸) hx lemma exp_series_has_sum_exp_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, (n!⁻¹ : 𝕂) • x^n) (exp 𝕂 x):= begin rw ← exp_series_apply_eq', exact exp_series_has_sum_exp_of_mem_ball x hx end lemma has_fpower_series_on_ball_exp_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_on_ball (exp 𝕂) (exp_series 𝕂 𝔸) 0 (exp_series 𝕂 𝔸).radius := (exp_series 𝕂 𝔸).has_fpower_series_on_ball h lemma has_fpower_series_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_at (exp 𝕂) (exp_series 𝕂 𝔸) 0 := (has_fpower_series_on_ball_exp_of_radius_pos h).has_fpower_series_at lemma continuous_on_exp : continuous_on (exp 𝕂 : 𝔸 → 𝔸) (emetric.ball 0 (exp_series 𝕂 𝔸).radius) := formal_multilinear_series.continuous_on lemma analytic_at_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : analytic_at 𝕂 (exp 𝕂) x:= begin by_cases h : (exp_series 𝕂 𝔸).radius = 0, { rw h at hx, exact (ennreal.not_lt_zero hx).elim }, { have h := pos_iff_ne_zero.mpr h, exact (has_fpower_series_on_ball_exp_of_radius_pos h).analytic_at_of_mem hx } end /-- In a Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, if `x` and `y` are in the disk of convergence and commute, then `exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add_of_commute_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hxy : commute x y) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := begin rw [exp_eq_tsum, tsum_mul_tsum_eq_tsum_sum_antidiagonal_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) (norm_exp_series_summable_of_mem_ball' y hy)], dsimp only, conv_lhs {congr, funext, rw [hxy.add_pow' _, finset.smul_sum]}, refine tsum_congr (λ n, finset.sum_congr rfl $ λ kl hkl, _), rw [nsmul_eq_smul_cast 𝕂, smul_smul, smul_mul_smul, ← (finset.nat.mem_antidiagonal.mp hkl), nat.cast_add_choose, (finset.nat.mem_antidiagonal.mp hkl)], congr' 1, have : (n! : 𝕂) ≠ 0 := nat.cast_ne_zero.mpr n.factorial_ne_zero, field_simp [this] end /-- `exp 𝕂 x` has explicit two-sided inverse `exp 𝕂 (-x)`. -/ noncomputable def invertible_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : invertible (exp 𝕂 x) := { inv_of := exp 𝕂 (-x), inv_of_mul_self := begin have hnx : -x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius, { rw [emetric.mem_ball, ←neg_zero, edist_neg_neg], exact hx }, rw [←exp_add_of_commute_of_mem_ball (commute.neg_left $ commute.refl x) hnx hx, neg_add_self, exp_zero], end, mul_inv_of_self := begin have hnx : -x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius, { rw [emetric.mem_ball, ←neg_zero, edist_neg_neg], exact hx }, rw [←exp_add_of_commute_of_mem_ball (commute.neg_right $ commute.refl x) hx hnx, add_neg_self, exp_zero], end } lemma is_unit_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : is_unit (exp 𝕂 x) := @is_unit_of_invertible _ _ _ (invertible_exp_of_mem_ball hx) lemma inv_of_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) [invertible (exp 𝕂 x)] : ⅟(exp 𝕂 x) = exp 𝕂 (-x) := by { letI := invertible_exp_of_mem_ball hx, convert (rfl : ⅟(exp 𝕂 x) = _) } /-- Any continuous ring homomorphism commutes with `exp`. -/ lemma map_exp_of_mem_ball {F} [ring_hom_class F 𝔸 𝔹] (f : F) (hf : continuous f) (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : f (exp 𝕂 x) = exp 𝕂 (f x) := begin rw [exp_eq_tsum, exp_eq_tsum], refine ((exp_series_summable_of_mem_ball' _ hx).has_sum.map f hf).tsum_eq.symm.trans _, dsimp only [function.comp], simp_rw [one_div, map_inv_nat_cast_smul f 𝕂 𝕂, map_pow], end end complete_algebra lemma algebra_map_exp_comm_of_mem_ball [complete_space 𝕂] (x : 𝕂) (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : algebra_map 𝕂 𝔸 (exp 𝕂 x) = exp 𝕂 (algebra_map 𝕂 𝔸 x) := map_exp_of_mem_ball _ (algebra_map_clm _ _).continuous _ hx end any_field_any_algebra section any_field_division_algebra variables {𝕂 𝔸 : Type*} [nondiscrete_normed_field 𝕂] [normed_division_ring 𝔸] [normed_algebra 𝕂 𝔸] variables (𝕂) lemma norm_exp_series_div_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥x^n / n!∥) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq_div' x, exact norm_exp_series_summable_of_mem_ball x hx end lemma exp_series_div_summable_of_mem_ball [complete_space 𝔸] (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_div_summable_of_mem_ball 𝕂 x hx) lemma exp_series_div_has_sum_exp_of_mem_ball [complete_space 𝔸] (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, x^n / n!) (exp 𝕂 x) := begin rw ← exp_series_apply_eq_div' x, exact exp_series_has_sum_exp_of_mem_ball x hx end variables {𝕂} lemma exp_neg_of_mem_ball [char_zero 𝕂] [complete_space 𝔸] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (-x) = (exp 𝕂 x)⁻¹ := begin letI := invertible_exp_of_mem_ball hx, exact inv_of_eq_inv (exp 𝕂 x), end end any_field_division_algebra section any_field_comm_algebra variables {𝕂 𝔸 : Type*} [nondiscrete_normed_field 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` for all `x`, `y` in the disk of convergence. -/ lemma exp_add_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_commute_of_mem_ball (commute.all x y) hx hy end any_field_comm_algebra section is_R_or_C section any_algebra variables (𝕂 𝔸 𝔹 : Type*) [is_R_or_C 𝕂] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] variables [normed_ring 𝔹] [normed_algebra 𝕂 𝔹] /-- In a normed algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, the series defining the exponential map has an infinite radius of convergence. -/ lemma exp_series_radius_eq_top : (exp_series 𝕂 𝔸).radius = ∞ := begin refine (exp_series 𝕂 𝔸).radius_eq_top_of_summable_norm (λ r, _), refine summable_of_norm_bounded_eventually _ (real.summable_pow_div_factorial r) _, filter_upwards [eventually_cofinite_ne 0] with n hn, rw [norm_mul, norm_norm (exp_series 𝕂 𝔸 n), exp_series, norm_smul, norm_inv, norm_pow, nnreal.norm_eq, norm_eq_abs, abs_cast_nat, mul_comm, ←mul_assoc, ←div_eq_mul_inv], have : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸∥ ≤ 1 := norm_mk_pi_algebra_fin_le_of_pos (nat.pos_of_ne_zero hn), exact mul_le_of_le_one_right (div_nonneg (pow_nonneg r.coe_nonneg n) n!.cast_nonneg) this end lemma exp_series_radius_pos : 0 < (exp_series 𝕂 𝔸).radius := begin rw exp_series_radius_eq_top, exact with_top.zero_lt_top end variables {𝕂 𝔸 𝔹} lemma norm_exp_series_summable (x : 𝔸) : summable (λ n, ∥exp_series 𝕂 𝔸 n (λ _, x)∥) := norm_exp_series_summable_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma norm_exp_series_summable' (x : 𝔸) : summable (λ n, ∥(n!⁻¹ : 𝕂) • x^n∥) := norm_exp_series_summable_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable (x : 𝔸) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable x) lemma exp_series_summable' (x : 𝔸) : summable (λ n, (n!⁻¹ : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable' x) lemma exp_series_has_sum_exp (x : 𝔸) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 x) := exp_series_has_sum_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_series_has_sum_exp' (x : 𝔸) : has_sum (λ n, (n!⁻¹ : 𝕂) • x^n) (exp 𝕂 x):= exp_series_has_sum_exp_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_has_fpower_series_on_ball : has_fpower_series_on_ball (exp 𝕂) (exp_series 𝕂 𝔸) 0 ∞ := exp_series_radius_eq_top 𝕂 𝔸 ▸ has_fpower_series_on_ball_exp_of_radius_pos (exp_series_radius_pos _ _) lemma exp_has_fpower_series_at_zero : has_fpower_series_at (exp 𝕂) (exp_series 𝕂 𝔸) 0 := exp_has_fpower_series_on_ball.has_fpower_series_at lemma exp_continuous : continuous (exp 𝕂 : 𝔸 → 𝔸) := begin rw [continuous_iff_continuous_on_univ, ← metric.eball_top_eq_univ (0 : 𝔸), ← exp_series_radius_eq_top 𝕂 𝔸], exact continuous_on_exp end lemma exp_analytic (x : 𝔸) : analytic_at 𝕂 (exp 𝕂) x := analytic_at_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if `x` and `y` commute, then `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add_of_commute {x y : 𝔸} (hxy : commute x y) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_commute_of_mem_ball hxy ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) section variables (𝕂) /-- `exp 𝕂 x` has explicit two-sided inverse `exp 𝕂 (-x)`. -/ noncomputable def invertible_exp (x : 𝔸) : invertible (exp 𝕂 x) := invertible_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma is_unit_exp (x : 𝔸) : is_unit (exp 𝕂 x) := is_unit_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma inv_of_exp (x : 𝔸) [invertible (exp 𝕂 x)] : ⅟(exp 𝕂 x) = exp 𝕂 (-x) := inv_of_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma ring.inverse_exp (x : 𝔸) : ring.inverse (exp 𝕂 x) = exp 𝕂 (-x) := begin letI := invertible_exp 𝕂 x, exact ring.inverse_invertible _, end end /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if a family of elements `f i` mutually commute then `exp 𝕂 (∑ i, f i) = ∏ i, exp 𝕂 (f i)`. -/ lemma exp_sum_of_commute {ι} (s : finset ι) (f : ι → 𝔸) (h : ∀ (i ∈ s) (j ∈ s), commute (f i) (f j)) : exp 𝕂 (∑ i in s, f i) = s.noncomm_prod (λ i, exp 𝕂 (f i)) (λ i hi j hj, (h i hi j hj).exp 𝕂) := begin classical, induction s using finset.induction_on with a s ha ih, { simp }, rw [finset.noncomm_prod_insert_of_not_mem _ _ _ _ ha, finset.sum_insert ha, exp_add_of_commute, ih], refine commute.sum_right _ _ _ _, intros i hi, exact h _ (finset.mem_insert_self _ _) _ (finset.mem_insert_of_mem hi), end lemma exp_nsmul (n : ℕ) (x : 𝔸) : exp 𝕂 (n • x) = exp 𝕂 x ^ n := begin induction n with n ih, { rw [zero_smul, pow_zero, exp_zero], }, { rw [succ_nsmul, pow_succ, exp_add_of_commute ((commute.refl x).smul_right n), ih] } end variables (𝕂) /-- Any continuous ring homomorphism commutes with `exp`. -/ lemma map_exp {F} [ring_hom_class F 𝔸 𝔹] (f : F) (hf : continuous f) (x : 𝔸) : f (exp 𝕂 x) = exp 𝕂 (f x) := map_exp_of_mem_ball f hf x $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma exp_smul {G} [monoid G] [mul_semiring_action G 𝔸] [has_continuous_const_smul G 𝔸] (g : G) (x : 𝔸) : exp 𝕂 (g • x) = g • exp 𝕂 x := (map_exp 𝕂 (mul_semiring_action.to_ring_hom G 𝔸 g) (continuous_const_smul _) x).symm lemma exp_units_conj (y : 𝔸ˣ) (x : 𝔸) : exp 𝕂 (y * x * ↑(y⁻¹) : 𝔸) = y * exp 𝕂 x * ↑(y⁻¹) := exp_smul _ (conj_act.to_conj_act y) x lemma exp_units_conj' (y : 𝔸ˣ) (x : 𝔸) : exp 𝕂 (↑(y⁻¹) * x * y) = ↑(y⁻¹) * exp 𝕂 x * y := exp_units_conj _ _ _ @[simp] lemma prod.fst_exp [complete_space 𝔹] (x : 𝔸 × 𝔹) : (exp 𝕂 x).fst = exp 𝕂 x.fst := map_exp _ (ring_hom.fst 𝔸 𝔹) continuous_fst x @[simp] lemma prod.snd_exp [complete_space 𝔹] (x : 𝔸 × 𝔹) : (exp 𝕂 x).snd = exp 𝕂 x.snd := map_exp _ (ring_hom.snd 𝔸 𝔹) continuous_snd x @[simp] lemma pi.exp_apply {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) (i : ι) : exp 𝕂 x i = exp 𝕂 (x i) := begin -- Lean struggles to infer this instance due to it wanting `[Π i, semi_normed_ring (𝔸 i)]` letI : normed_algebra 𝕂 (Π i, 𝔸 i) := pi.normed_algebra _, exact map_exp _ (pi.eval_ring_hom 𝔸 i) (continuous_apply _) x end lemma pi.exp_def {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) : exp 𝕂 x = λ i, exp 𝕂 (x i) := funext $ pi.exp_apply 𝕂 x lemma function.update_exp {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [decidable_eq ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) (j : ι) (xj : 𝔸 j) : function.update (exp 𝕂 x) j (exp 𝕂 xj) = exp 𝕂 (function.update x j xj) := begin ext i, simp_rw [pi.exp_def], exact (function.apply_update (λ i, exp 𝕂) x j xj i).symm, end end complete_algebra lemma algebra_map_exp_comm (x : 𝕂) : algebra_map 𝕂 𝔸 (exp 𝕂 x) = exp 𝕂 (algebra_map 𝕂 𝔸 x) := algebra_map_exp_comm_of_mem_ball x $ (exp_series_radius_eq_top 𝕂 𝕂).symm ▸ edist_lt_top _ _ end any_algebra section division_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_division_ring 𝔸] [normed_algebra 𝕂 𝔸] variables (𝕂) lemma norm_exp_series_div_summable (x : 𝔸) : summable (λ n, ∥x^n / n!∥) := norm_exp_series_div_summable_of_mem_ball 𝕂 x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) variables [complete_space 𝔸] lemma exp_series_div_summable (x : 𝔸) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_div_summable 𝕂 x) lemma exp_series_div_has_sum_exp (x : 𝔸) : has_sum (λ n, x^n / n!) (exp 𝕂 x):= exp_series_div_has_sum_exp_of_mem_ball 𝕂 x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) variables {𝕂} lemma exp_neg (x : 𝔸) : exp 𝕂 (-x) = (exp 𝕂 x)⁻¹ := exp_neg_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma exp_zsmul (z : ℤ) (x : 𝔸) : exp 𝕂 (z • x) = (exp 𝕂 x) ^ z := begin obtain ⟨n, rfl | rfl⟩ := z.eq_coe_or_neg, { rw [zpow_coe_nat, coe_nat_zsmul, exp_nsmul] }, { rw [zpow_neg, zpow_coe_nat, neg_smul, exp_neg, coe_nat_zsmul, exp_nsmul] }, end lemma exp_conj (y : 𝔸) (x : 𝔸) (hy : y ≠ 0) : exp 𝕂 (y * x * y⁻¹) = y * exp 𝕂 x * y⁻¹ := exp_units_conj _ (units.mk0 y hy) x lemma exp_conj' (y : 𝔸) (x : 𝔸) (hy : y ≠ 0) : exp 𝕂 (y⁻¹ * x * y) = y⁻¹ * exp 𝕂 x * y := exp_units_conj' _ (units.mk0 y hy) x end division_algebra section comm_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add {x y : 𝔸} : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_mem_ball ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- A version of `exp_sum_of_commute` for a commutative Banach-algebra. -/ lemma exp_sum {ι} (s : finset ι) (f : ι → 𝔸) : exp 𝕂 (∑ i in s, f i) = ∏ i in s, exp 𝕂 (f i) := begin rw [exp_sum_of_commute, finset.noncomm_prod_eq_prod], exact λ i hi j hj, commute.all _ _, end end comm_algebra end is_R_or_C end normed section scalar_tower variables (𝕂 𝕂' 𝔸 : Type*) [field 𝕂] [field 𝕂'] [ring 𝔸] [algebra 𝕂 𝔸] [algebra 𝕂' 𝔸] [topological_space 𝔸] [topological_ring 𝔸] /-- If a normed ring `𝔸` is a normed algebra over two fields, then they define the same `exp_series` on `𝔸`. -/ lemma exp_series_eq_exp_series (n : ℕ) (x : 𝔸) : (exp_series 𝕂 𝔸 n (λ _, x)) = (exp_series 𝕂' 𝔸 n (λ _, x)) := by rw [exp_series_apply_eq, exp_series_apply_eq, inv_nat_cast_smul_eq 𝕂 𝕂'] /-- If a normed ring `𝔸` is a normed algebra over two fields, then they define the same exponential function on `𝔸`. -/ lemma exp_eq_exp : (exp 𝕂 : 𝔸 → 𝔸) = exp 𝕂' := begin ext, rw [exp, exp], refine tsum_congr (λ n, _), rw exp_series_eq_exp_series 𝕂 𝕂' 𝔸 n x end lemma exp_ℝ_ℂ_eq_exp_ℂ_ℂ : (exp ℝ : ℂ → ℂ) = exp ℂ := exp_eq_exp ℝ ℂ ℂ end scalar_tower
152fac571073d21c91a3c40a3bf0bf3ccddb2fff
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/normed_space/inner_product.lean
c003be86f4e5faab5a8ed70947f68616239b88f9
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
125,220
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Sébastien Gouëzel, Frédéric Dupuis, Heather Macbeth -/ import analysis.complex.basic import analysis.normed_space.bounded_linear_maps import analysis.special_functions.sqrt import linear_algebra.bilinear_form import linear_algebra.sesquilinear_form /-! # Inner Product Space This file defines inner product spaces and proves its basic properties. An inner product space is a vector space endowed with an inner product. It generalizes the notion of dot product in `ℝ^n` and provides the means of defining the length of a vector and the angle between two vectors. In particular vectors `x` and `y` are orthogonal if their inner product equals zero. We define both the real and complex cases at the same time using the `is_R_or_C` typeclass. This file proves general results on inner product spaces. For the specific construction of an inner product structure on `n → 𝕜` for `𝕜 = ℝ` or `ℂ`, see `euclidean_space` in `analysis.pi_Lp`. ## Main results - We define the class `inner_product_space 𝕜 E` extending `normed_space 𝕜 E` with a number of basic properties, most notably the Cauchy-Schwarz inequality. Here `𝕜` is understood to be either `ℝ` or `ℂ`, through the `is_R_or_C` typeclass. - We show that if `f i` is an inner product space for each `i`, then so is `Π i, f i` - Existence of orthogonal projection onto nonempty complete subspace: Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. The point `v` is usually called the orthogonal projection of `u` onto `K`. - We define `orthonormal`, a predicate on a function `v : ι → E`. We prove the existence of a maximal orthonormal set, `exists_maximal_orthonormal`, and also prove that a maximal orthonormal set is a basis (`maximal_orthonormal_iff_basis_of_finite_dimensional`), if `E` is finite- dimensional, or in general (`maximal_orthonormal_iff_dense_span`) a set whose span is dense (i.e., a Hilbert basis, although we do not make that definition). ## Notation We globally denote the real and complex inner products by `⟪·, ·⟫_ℝ` and `⟪·, ·⟫_ℂ` respectively. We also provide two notation namespaces: `real_inner_product_space`, `complex_inner_product_space`, which respectively introduce the plain notation `⟪·, ·⟫` for the the real and complex inner product. The orthogonal complement of a submodule `K` is denoted by `Kᗮ`. ## Implementation notes We choose the convention that inner products are conjugate linear in the first argument and linear in the second. ## Tags inner product space, norm ## References * [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*] * [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*] The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html> -/ noncomputable theory open is_R_or_C real filter open_locale big_operators classical topological_space variables {𝕜 E F : Type*} [is_R_or_C 𝕜] /-- Syntactic typeclass for types endowed with an inner product -/ class has_inner (𝕜 E : Type*) := (inner : E → E → 𝕜) export has_inner (inner) notation `⟪`x`, `y`⟫_ℝ` := @inner ℝ _ _ x y notation `⟪`x`, `y`⟫_ℂ` := @inner ℂ _ _ x y section notations localized "notation `⟪`x`, `y`⟫` := @inner ℝ _ _ x y" in real_inner_product_space localized "notation `⟪`x`, `y`⟫` := @inner ℂ _ _ x y" in complex_inner_product_space end notations /-- An inner product space is a vector space with an additional operation called inner product. The norm could be derived from the inner product, instead we require the existence of a norm and the fact that `∥x∥^2 = re ⟪x, x⟫` to be able to put instances on `𝕂` or product spaces. To construct a norm from an inner product, see `inner_product_space.of_core`. -/ class inner_product_space (𝕜 : Type*) (E : Type*) [is_R_or_C 𝕜] extends normed_group E, normed_space 𝕜 E, has_inner 𝕜 E := (norm_sq_eq_inner : ∀ (x : E), ∥x∥^2 = re (inner x x)) (conj_sym : ∀ x y, conj (inner y x) = inner x y) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = (conj r) * inner x y) attribute [nolint dangerous_instance] inner_product_space.to_normed_group -- note [is_R_or_C instance] /-! ### Constructing a normed space structure from an inner product In the definition of an inner product space, we require the existence of a norm, which is equal (but maybe not defeq) to the square root of the scalar product. This makes it possible to put an inner product space structure on spaces with a preexisting norm (for instance `ℝ`), with good properties. However, sometimes, one would like to define the norm starting only from a well-behaved scalar product. This is what we implement in this paragraph, starting from a structure `inner_product_space.core` stating that we have a nice scalar product. Our goal here is not to develop a whole theory with all the supporting API, as this will be done below for `inner_product_space`. Instead, we implement the bare minimum to go as directly as possible to the construction of the norm and the proof of the triangular inequality. Warning: Do not use this `core` structure if the space you are interested in already has a norm instance defined on it, otherwise this will create a second non-defeq norm instance! -/ /-- A structure requiring that a scalar product is positive definite and symmetric, from which one can construct an `inner_product_space` instance in `inner_product_space.of_core`. -/ @[nolint has_inhabited_instance] structure inner_product_space.core (𝕜 : Type*) (F : Type*) [is_R_or_C 𝕜] [add_comm_group F] [module 𝕜 F] := (inner : F → F → 𝕜) (conj_sym : ∀ x y, conj (inner y x) = inner x y) (nonneg_re : ∀ x, 0 ≤ re (inner x x)) (definite : ∀ x, inner x x = 0 → x = 0) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = (conj r) * inner x y) /- We set `inner_product_space.core` to be a class as we will use it as such in the construction of the normed space structure that it produces. However, all the instances we will use will be local to this proof. -/ attribute [class] inner_product_space.core namespace inner_product_space.of_core variables [add_comm_group F] [module 𝕜 F] [c : inner_product_space.core 𝕜 F] include c local notation `⟪`x`, `y`⟫` := @inner 𝕜 F _ x y local notation `norm_sqK` := @is_R_or_C.norm_sq 𝕜 _ local notation `reK` := @is_R_or_C.re 𝕜 _ local notation `absK` := @is_R_or_C.abs 𝕜 _ local notation `ext_iff` := @is_R_or_C.ext_iff 𝕜 _ local postfix `†`:90 := @is_R_or_C.conj 𝕜 _ /-- Inner product defined by the `inner_product_space.core` structure. -/ def to_has_inner : has_inner 𝕜 F := { inner := c.inner } local attribute [instance] to_has_inner /-- The norm squared function for `inner_product_space.core` structure. -/ def norm_sq (x : F) := reK ⟪x, x⟫ local notation `norm_sqF` := @norm_sq 𝕜 F _ _ _ _ lemma inner_conj_sym (x y : F) : ⟪y, x⟫† = ⟪x, y⟫ := c.conj_sym x y lemma inner_self_nonneg {x : F} : 0 ≤ re ⟪x, x⟫ := c.nonneg_re _ lemma inner_self_nonneg_im {x : F} : im ⟪x, x⟫ = 0 := by rw [← @of_real_inj 𝕜, im_eq_conj_sub]; simp [inner_conj_sym] lemma inner_self_im_zero {x : F} : im ⟪x, x⟫ = 0 := inner_self_nonneg_im lemma inner_add_left {x y z : F} : ⟪x + y, z⟫ = ⟪x, z⟫ + ⟪y, z⟫ := c.add_left _ _ _ lemma inner_add_right {x y z : F} : ⟪x, y + z⟫ = ⟪x, y⟫ + ⟪x, z⟫ := by rw [←inner_conj_sym, inner_add_left, ring_hom.map_add]; simp only [inner_conj_sym] lemma inner_norm_sq_eq_inner_self (x : F) : (norm_sqF x : 𝕜) = ⟪x, x⟫ := begin rw ext_iff, exact ⟨by simp only [of_real_re]; refl, by simp only [inner_self_nonneg_im, of_real_im]⟩ end lemma inner_re_symm {x y : F} : re ⟪x, y⟫ = re ⟪y, x⟫ := by rw [←inner_conj_sym, conj_re] lemma inner_im_symm {x y : F} : im ⟪x, y⟫ = -im ⟪y, x⟫ := by rw [←inner_conj_sym, conj_im] lemma inner_smul_left {x y : F} {r : 𝕜} : ⟪r • x, y⟫ = r† * ⟪x, y⟫ := c.smul_left _ _ _ lemma inner_smul_right {x y : F} {r : 𝕜} : ⟪x, r • y⟫ = r * ⟪x, y⟫ := by rw [←inner_conj_sym, inner_smul_left]; simp only [conj_conj, inner_conj_sym, ring_hom.map_mul] lemma inner_zero_left {x : F} : ⟪0, x⟫ = 0 := by rw [←zero_smul 𝕜 (0 : F), inner_smul_left]; simp only [zero_mul, ring_hom.map_zero] lemma inner_zero_right {x : F} : ⟪x, 0⟫ = 0 := by rw [←inner_conj_sym, inner_zero_left]; simp only [ring_hom.map_zero] lemma inner_self_eq_zero {x : F} : ⟪x, x⟫ = 0 ↔ x = 0 := iff.intro (c.definite _) (by { rintro rfl, exact inner_zero_left }) lemma inner_self_re_to_K {x : F} : (re ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := by norm_num [ext_iff, inner_self_nonneg_im] lemma inner_abs_conj_sym {x y : F} : abs ⟪x, y⟫ = abs ⟪y, x⟫ := by rw [←inner_conj_sym, abs_conj] lemma inner_neg_left {x y : F} : ⟪-x, y⟫ = -⟪x, y⟫ := by { rw [← neg_one_smul 𝕜 x, inner_smul_left], simp } lemma inner_neg_right {x y : F} : ⟪x, -y⟫ = -⟪x, y⟫ := by rw [←inner_conj_sym, inner_neg_left]; simp only [ring_hom.map_neg, inner_conj_sym] lemma inner_sub_left {x y z : F} : ⟪x - y, z⟫ = ⟪x, z⟫ - ⟪y, z⟫ := by { simp [sub_eq_add_neg, inner_add_left, inner_neg_left] } lemma inner_sub_right {x y z : F} : ⟪x, y - z⟫ = ⟪x, y⟫ - ⟪x, z⟫ := by { simp [sub_eq_add_neg, inner_add_right, inner_neg_right] } lemma inner_mul_conj_re_abs {x y : F} : re (⟪x, y⟫ * ⟪y, x⟫) = abs (⟪x, y⟫ * ⟪y, x⟫) := by { rw[←inner_conj_sym, mul_comm], exact re_eq_abs_of_mul_conj (inner y x), } /-- Expand `inner (x + y) (x + y)` -/ lemma inner_add_add_self {x y : F} : ⟪x + y, x + y⟫ = ⟪x, x⟫ + ⟪x, y⟫ + ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_add_left, inner_add_right]; ring /- Expand `inner (x - y) (x - y)` -/ lemma inner_sub_sub_self {x y : F} : ⟪x - y, x - y⟫ = ⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_sub_left, inner_sub_right]; ring /-- **Cauchy–Schwarz inequality**. This proof follows "Proof 2" on Wikipedia. We need this for the `core` structure to prove the triangle inequality below when showing the core is a normed group. -/ lemma inner_mul_inner_self_le (x y : F) : abs ⟪x, y⟫ * abs ⟪y, x⟫ ≤ re ⟪x, x⟫ * re ⟪y, y⟫ := begin by_cases hy : y = 0, { rw [hy], simp only [is_R_or_C.abs_zero, inner_zero_left, mul_zero, add_monoid_hom.map_zero] }, { change y ≠ 0 at hy, have hy' : ⟪y, y⟫ ≠ 0 := λ h, by rw [inner_self_eq_zero] at h; exact hy h, set T := ⟪y, x⟫ / ⟪y, y⟫ with hT, have h₁ : re ⟪y, x⟫ = re ⟪x, y⟫ := inner_re_symm, have h₂ : im ⟪y, x⟫ = -im ⟪x, y⟫ := inner_im_symm, have h₃ : ⟪y, x⟫ * ⟪x, y⟫ * ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = ⟪y, x⟫ * ⟪x, y⟫ / ⟪y, y⟫, { rw [mul_div_assoc], have : ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = 1 / ⟪y, y⟫ := by rw [div_mul_eq_div_mul_one_div, div_self hy', one_mul], rw [this, div_eq_mul_inv, one_mul, ←div_eq_mul_inv] }, have h₄ : ⟪y, y⟫ = re ⟪y, y⟫ := by simp only [inner_self_re_to_K], have h₅ : re ⟪y, y⟫ > 0, { refine lt_of_le_of_ne inner_self_nonneg _, intro H, apply hy', rw ext_iff, exact ⟨by simp only [H, zero_re'], by simp only [inner_self_nonneg_im, add_monoid_hom.map_zero]⟩ }, have h₆ : re ⟪y, y⟫ ≠ 0 := ne_of_gt h₅, have hmain := calc 0 ≤ re ⟪x - T • y, x - T • y⟫ : inner_self_nonneg ... = re ⟪x, x⟫ - re ⟪T • y, x⟫ - re ⟪x, T • y⟫ + re ⟪T • y, T • y⟫ : by simp only [inner_sub_sub_self, inner_smul_left, inner_smul_right, h₁, h₂, neg_mul_eq_neg_mul_symm, add_monoid_hom.map_add, mul_re, conj_im, add_monoid_hom.map_sub, mul_neg_eq_neg_mul_symm, conj_re, neg_neg] ... = re ⟪x, x⟫ - re (T† * ⟪y, x⟫) - re (T * ⟪x, y⟫) + re (T * T† * ⟪y, y⟫) : by simp only [inner_smul_left, inner_smul_right, mul_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ / ⟪y, y⟫ * ⟪y, x⟫) : by field_simp [-mul_re, inner_conj_sym, hT, conj_div, h₁, h₃] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / ⟪y, y⟫) : by rw [div_mul_eq_mul_div_comm, ←mul_div_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / re ⟪y, y⟫) : by conv_lhs { rw [h₄] } ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [div_re_of_real] ... = re ⟪x, x⟫ - abs (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [inner_mul_conj_re_abs] ... = re ⟪x, x⟫ - abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ : by rw is_R_or_C.abs_mul, have hmain' : abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ ≤ re ⟪x, x⟫ := by linarith, have := (mul_le_mul_right h₅).mpr hmain', rwa [div_mul_cancel (abs ⟪x, y⟫ * abs ⟪y, x⟫) h₆] at this } end /-- Norm constructed from a `inner_product_space.core` structure, defined to be the square root of the scalar product. -/ def to_has_norm : has_norm F := { norm := λ x, sqrt (re ⟪x, x⟫) } local attribute [instance] to_has_norm lemma norm_eq_sqrt_inner (x : F) : ∥x∥ = sqrt (re ⟪x, x⟫) := rfl lemma inner_self_eq_norm_sq (x : F) : re ⟪x, x⟫ = ∥x∥ * ∥x∥ := by rw[norm_eq_sqrt_inner, ←sqrt_mul inner_self_nonneg (re ⟪x, x⟫), sqrt_mul_self inner_self_nonneg] lemma sqrt_norm_sq_eq_norm {x : F} : sqrt (norm_sqF x) = ∥x∥ := rfl /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : F) : abs ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_sq_le_sq (mul_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) begin have H : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = re ⟪y, y⟫ * re ⟪x, x⟫, { simp only [inner_self_eq_norm_sq], ring, }, rw H, conv begin to_lhs, congr, rw[inner_abs_conj_sym], end, exact inner_mul_inner_self_le y x, end /-- Normed group structure constructed from an `inner_product_space.core` structure -/ def to_normed_group : normed_group F := normed_group.of_core F { norm_eq_zero_iff := assume x, begin split, { intro H, change sqrt (re ⟪x, x⟫) = 0 at H, rw [sqrt_eq_zero inner_self_nonneg] at H, apply (inner_self_eq_zero : ⟪x, x⟫ = 0 ↔ x = 0).mp, rw ext_iff, exact ⟨by simp [H], by simp [inner_self_im_zero]⟩ }, { rintro rfl, change sqrt (re ⟪0, 0⟫) = 0, simp only [sqrt_zero, inner_zero_right, add_monoid_hom.map_zero] } end, triangle := assume x y, begin have h₁ : abs ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := abs_inner_le_norm _ _, have h₂ : re ⟪x, y⟫ ≤ abs ⟪x, y⟫ := re_le_abs _, have h₃ : re ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := by linarith, have h₄ : re ⟪y, x⟫ ≤ ∥x∥ * ∥y∥ := by rwa [←inner_conj_sym, conj_re], have : ∥x + y∥ * ∥x + y∥ ≤ (∥x∥ + ∥y∥) * (∥x∥ + ∥y∥), { simp [←inner_self_eq_norm_sq, inner_add_add_self, add_mul, mul_add, mul_comm], linarith }, exact nonneg_le_nonneg_of_sq_le_sq (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) this end, norm_neg := λ x, by simp only [norm, inner_neg_left, neg_neg, inner_neg_right] } local attribute [instance] to_normed_group /-- Normed space structure constructed from a `inner_product_space.core` structure -/ def to_normed_space : normed_space 𝕜 F := { norm_smul_le := assume r x, begin rw [norm_eq_sqrt_inner, inner_smul_left, inner_smul_right, ←mul_assoc], rw [conj_mul_eq_norm_sq_left, of_real_mul_re, sqrt_mul, ←inner_norm_sq_eq_inner_self, of_real_re], { simp [sqrt_norm_sq_eq_norm, is_R_or_C.sqrt_norm_sq_eq_norm] }, { exact norm_sq_nonneg r } end } end inner_product_space.of_core /-- Given a `inner_product_space.core` structure on a space, one can use it to turn the space into an inner product space, constructing the norm out of the inner product -/ def inner_product_space.of_core [add_comm_group F] [module 𝕜 F] (c : inner_product_space.core 𝕜 F) : inner_product_space 𝕜 F := begin letI : normed_group F := @inner_product_space.of_core.to_normed_group 𝕜 F _ _ _ c, letI : normed_space 𝕜 F := @inner_product_space.of_core.to_normed_space 𝕜 F _ _ _ c, exact { norm_sq_eq_inner := λ x, begin have h₁ : ∥x∥^2 = (sqrt (re (c.inner x x))) ^ 2 := rfl, have h₂ : 0 ≤ re (c.inner x x) := inner_product_space.of_core.inner_self_nonneg, simp [h₁, sq_sqrt, h₂], end, ..c } end /-! ### Properties of inner product spaces -/ variables [inner_product_space 𝕜 E] [inner_product_space ℝ F] local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y local notation `IK` := @is_R_or_C.I 𝕜 _ local notation `absR` := _root_.abs local notation `absK` := @is_R_or_C.abs 𝕜 _ local postfix `†`:90 := @is_R_or_C.conj 𝕜 _ local postfix `⋆`:90 := complex.conj export inner_product_space (norm_sq_eq_inner) section basic_properties @[simp] lemma inner_conj_sym (x y : E) : ⟪y, x⟫† = ⟪x, y⟫ := inner_product_space.conj_sym _ _ lemma real_inner_comm (x y : F) : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := inner_conj_sym x y lemma inner_eq_zero_sym {x y : E} : ⟪x, y⟫ = 0 ↔ ⟪y, x⟫ = 0 := ⟨λ h, by simp [←inner_conj_sym, h], λ h, by simp [←inner_conj_sym, h]⟩ @[simp] lemma inner_self_nonneg_im {x : E} : im ⟪x, x⟫ = 0 := by rw [← @of_real_inj 𝕜, im_eq_conj_sub]; simp lemma inner_self_im_zero {x : E} : im ⟪x, x⟫ = 0 := inner_self_nonneg_im lemma inner_add_left {x y z : E} : ⟪x + y, z⟫ = ⟪x, z⟫ + ⟪y, z⟫ := inner_product_space.add_left _ _ _ lemma inner_add_right {x y z : E} : ⟪x, y + z⟫ = ⟪x, y⟫ + ⟪x, z⟫ := by { rw [←inner_conj_sym, inner_add_left, ring_hom.map_add], simp only [inner_conj_sym] } lemma inner_re_symm {x y : E} : re ⟪x, y⟫ = re ⟪y, x⟫ := by rw [←inner_conj_sym, conj_re] lemma inner_im_symm {x y : E} : im ⟪x, y⟫ = -im ⟪y, x⟫ := by rw [←inner_conj_sym, conj_im] lemma inner_smul_left {x y : E} {r : 𝕜} : ⟪r • x, y⟫ = r† * ⟪x, y⟫ := inner_product_space.smul_left _ _ _ lemma real_inner_smul_left {x y : F} {r : ℝ} : ⟪r • x, y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_left lemma inner_smul_real_left {x y : E} {r : ℝ} : ⟪(r : 𝕜) • x, y⟫ = r • ⟪x, y⟫ := by { rw [inner_smul_left, conj_of_real, algebra.smul_def], refl } lemma inner_smul_right {x y : E} {r : 𝕜} : ⟪x, r • y⟫ = r * ⟪x, y⟫ := by rw [←inner_conj_sym, inner_smul_left, ring_hom.map_mul, conj_conj, inner_conj_sym] lemma real_inner_smul_right {x y : F} {r : ℝ} : ⟪x, r • y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_right lemma inner_smul_real_right {x y : E} {r : ℝ} : ⟪x, (r : 𝕜) • y⟫ = r • ⟪x, y⟫ := by { rw [inner_smul_right, algebra.smul_def], refl } /-- The inner product as a sesquilinear form. -/ @[simps] def sesq_form_of_inner : sesq_form 𝕜 E (conj_to_ring_equiv 𝕜) := { sesq := λ x y, ⟪y, x⟫, -- Note that sesquilinear forms are linear in the first argument sesq_add_left := λ x y z, inner_add_right, sesq_add_right := λ x y z, inner_add_left, sesq_smul_left := λ r x y, inner_smul_right, sesq_smul_right := λ r x y, inner_smul_left } /-- The real inner product as a bilinear form. -/ @[simps] def bilin_form_of_real_inner : bilin_form ℝ F := { bilin := inner, bilin_add_left := λ x y z, inner_add_left, bilin_smul_left := λ a x y, inner_smul_left, bilin_add_right := λ x y z, inner_add_right, bilin_smul_right := λ a x y, inner_smul_right } /-- An inner product with a sum on the left. -/ lemma sum_inner {ι : Type*} (s : finset ι) (f : ι → E) (x : E) : ⟪∑ i in s, f i, x⟫ = ∑ i in s, ⟪f i, x⟫ := sesq_form.sum_right (sesq_form_of_inner) _ _ _ /-- An inner product with a sum on the right. -/ lemma inner_sum {ι : Type*} (s : finset ι) (f : ι → E) (x : E) : ⟪x, ∑ i in s, f i⟫ = ∑ i in s, ⟪x, f i⟫ := sesq_form.sum_left (sesq_form_of_inner) _ _ _ /-- An inner product with a sum on the left, `finsupp` version. -/ lemma finsupp.sum_inner {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪l.sum (λ (i : ι) (a : 𝕜), a • v i), x⟫ = l.sum (λ (i : ι) (a : 𝕜), (is_R_or_C.conj a) • ⟪v i, x⟫) := by { convert sum_inner l.support (λ a, l a • v a) x, simp [inner_smul_left, finsupp.sum] } /-- An inner product with a sum on the right, `finsupp` version. -/ lemma finsupp.inner_sum {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪x, l.sum (λ (i : ι) (a : 𝕜), a • v i)⟫ = l.sum (λ (i : ι) (a : 𝕜), a • ⟪x, v i⟫) := by { convert inner_sum l.support (λ a, l a • v a) x, simp [inner_smul_right, finsupp.sum] } @[simp] lemma inner_zero_left {x : E} : ⟪0, x⟫ = 0 := by rw [← zero_smul 𝕜 (0:E), inner_smul_left, ring_hom.map_zero, zero_mul] lemma inner_re_zero_left {x : E} : re ⟪0, x⟫ = 0 := by simp only [inner_zero_left, add_monoid_hom.map_zero] @[simp] lemma inner_zero_right {x : E} : ⟪x, 0⟫ = 0 := by rw [←inner_conj_sym, inner_zero_left, ring_hom.map_zero] lemma inner_re_zero_right {x : E} : re ⟪x, 0⟫ = 0 := by simp only [inner_zero_right, add_monoid_hom.map_zero] lemma inner_self_nonneg {x : E} : 0 ≤ re ⟪x, x⟫ := by rw [←norm_sq_eq_inner]; exact pow_nonneg (norm_nonneg x) 2 lemma real_inner_self_nonneg {x : F} : 0 ≤ ⟪x, x⟫_ℝ := @inner_self_nonneg ℝ F _ _ x @[simp] lemma inner_self_eq_zero {x : E} : ⟪x, x⟫ = 0 ↔ x = 0 := begin split, { intro h, have h₁ : re ⟪x, x⟫ = 0 := by rw is_R_or_C.ext_iff at h; simp [h.1], rw [←norm_sq_eq_inner x] at h₁, rw [←norm_eq_zero], exact pow_eq_zero h₁ }, { rintro rfl, exact inner_zero_left } end @[simp] lemma inner_self_nonpos {x : E} : re ⟪x, x⟫ ≤ 0 ↔ x = 0 := begin split, { intro h, rw ←inner_self_eq_zero, have H₁ : re ⟪x, x⟫ ≥ 0, exact inner_self_nonneg, have H₂ : re ⟪x, x⟫ = 0, exact le_antisymm h H₁, rw is_R_or_C.ext_iff, exact ⟨by simp [H₂], by simp [inner_self_nonneg_im]⟩ }, { rintro rfl, simp only [inner_zero_left, add_monoid_hom.map_zero] } end lemma real_inner_self_nonpos {x : F} : ⟪x, x⟫_ℝ ≤ 0 ↔ x = 0 := by { have h := @inner_self_nonpos ℝ F _ _ x, simpa using h } @[simp] lemma inner_self_re_to_K {x : E} : (re ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := by rw is_R_or_C.ext_iff; exact ⟨by simp, by simp [inner_self_nonneg_im]⟩ lemma inner_self_eq_norm_sq_to_K (x : E) : ⟪x, x⟫ = (∥x∥ ^ 2 : 𝕜) := begin suffices : (is_R_or_C.re ⟪x, x⟫ : 𝕜) = ∥x∥ ^ 2, { simpa [inner_self_re_to_K] using this }, exact_mod_cast (norm_sq_eq_inner x).symm end lemma inner_self_re_abs {x : E} : re ⟪x, x⟫ = abs ⟪x, x⟫ := begin conv_rhs { rw [←inner_self_re_to_K] }, symmetry, exact is_R_or_C.abs_of_nonneg inner_self_nonneg, end lemma inner_self_abs_to_K {x : E} : (absK ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := by { rw[←inner_self_re_abs], exact inner_self_re_to_K } lemma real_inner_self_abs {x : F} : absR ⟪x, x⟫_ℝ = ⟪x, x⟫_ℝ := by { have h := @inner_self_abs_to_K ℝ F _ _ x, simpa using h } lemma inner_abs_conj_sym {x y : E} : abs ⟪x, y⟫ = abs ⟪y, x⟫ := by rw [←inner_conj_sym, abs_conj] @[simp] lemma inner_neg_left {x y : E} : ⟪-x, y⟫ = -⟪x, y⟫ := by { rw [← neg_one_smul 𝕜 x, inner_smul_left], simp } @[simp] lemma inner_neg_right {x y : E} : ⟪x, -y⟫ = -⟪x, y⟫ := by rw [←inner_conj_sym, inner_neg_left]; simp only [ring_hom.map_neg, inner_conj_sym] lemma inner_neg_neg {x y : E} : ⟪-x, -y⟫ = ⟪x, y⟫ := by simp @[simp] lemma inner_self_conj {x : E} : ⟪x, x⟫† = ⟪x, x⟫ := by rw [is_R_or_C.ext_iff]; exact ⟨by rw [conj_re], by rw [conj_im, inner_self_im_zero, neg_zero]⟩ lemma inner_sub_left {x y z : E} : ⟪x - y, z⟫ = ⟪x, z⟫ - ⟪y, z⟫ := by { simp [sub_eq_add_neg, inner_add_left] } lemma inner_sub_right {x y z : E} : ⟪x, y - z⟫ = ⟪x, y⟫ - ⟪x, z⟫ := by { simp [sub_eq_add_neg, inner_add_right] } lemma inner_mul_conj_re_abs {x y : E} : re (⟪x, y⟫ * ⟪y, x⟫) = abs (⟪x, y⟫ * ⟪y, x⟫) := by { rw[←inner_conj_sym, mul_comm], exact re_eq_abs_of_mul_conj (inner y x), } /-- Expand `⟪x + y, x + y⟫` -/ lemma inner_add_add_self {x y : E} : ⟪x + y, x + y⟫ = ⟪x, x⟫ + ⟪x, y⟫ + ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_add_left, inner_add_right]; ring /-- Expand `⟪x + y, x + y⟫_ℝ` -/ lemma real_inner_add_add_self {x y : F} : ⟪x + y, x + y⟫_ℝ = ⟪x, x⟫_ℝ + 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := begin have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [←inner_conj_sym]; refl, simp [inner_add_add_self, this], ring, end /- Expand `⟪x - y, x - y⟫` -/ lemma inner_sub_sub_self {x y : E} : ⟪x - y, x - y⟫ = ⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_sub_left, inner_sub_right]; ring /-- Expand `⟪x - y, x - y⟫_ℝ` -/ lemma real_inner_sub_sub_self {x y : F} : ⟪x - y, x - y⟫_ℝ = ⟪x, x⟫_ℝ - 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := begin have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [←inner_conj_sym]; refl, simp [inner_sub_sub_self, this], ring, end /-- Parallelogram law -/ lemma parallelogram_law {x y : E} : ⟪x + y, x + y⟫ + ⟪x - y, x - y⟫ = 2 * (⟪x, x⟫ + ⟪y, y⟫) := by simp [inner_add_add_self, inner_sub_sub_self, two_mul, sub_eq_add_neg, add_comm, add_left_comm] /-- Cauchy–Schwarz inequality. This proof follows "Proof 2" on Wikipedia. -/ lemma inner_mul_inner_self_le (x y : E) : abs ⟪x, y⟫ * abs ⟪y, x⟫ ≤ re ⟪x, x⟫ * re ⟪y, y⟫ := begin by_cases hy : y = 0, { rw [hy], simp only [is_R_or_C.abs_zero, inner_zero_left, mul_zero, add_monoid_hom.map_zero] }, { change y ≠ 0 at hy, have hy' : ⟪y, y⟫ ≠ 0 := λ h, by rw [inner_self_eq_zero] at h; exact hy h, set T := ⟪y, x⟫ / ⟪y, y⟫ with hT, have h₁ : re ⟪y, x⟫ = re ⟪x, y⟫ := inner_re_symm, have h₂ : im ⟪y, x⟫ = -im ⟪x, y⟫ := inner_im_symm, have h₃ : ⟪y, x⟫ * ⟪x, y⟫ * ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = ⟪y, x⟫ * ⟪x, y⟫ / ⟪y, y⟫, { rw [mul_div_assoc], have : ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = 1 / ⟪y, y⟫ := by rw [div_mul_eq_div_mul_one_div, div_self hy', one_mul], rw [this, div_eq_mul_inv, one_mul, ←div_eq_mul_inv] }, have h₄ : ⟪y, y⟫ = re ⟪y, y⟫ := by simp, have h₅ : re ⟪y, y⟫ > 0, { refine lt_of_le_of_ne inner_self_nonneg _, intro H, apply hy', rw is_R_or_C.ext_iff, exact ⟨by simp only [H, zero_re'], by simp only [inner_self_nonneg_im, add_monoid_hom.map_zero]⟩ }, have h₆ : re ⟪y, y⟫ ≠ 0 := ne_of_gt h₅, have hmain := calc 0 ≤ re ⟪x - T • y, x - T • y⟫ : inner_self_nonneg ... = re ⟪x, x⟫ - re ⟪T • y, x⟫ - re ⟪x, T • y⟫ + re ⟪T • y, T • y⟫ : by simp only [inner_sub_sub_self, inner_smul_left, inner_smul_right, h₁, h₂, neg_mul_eq_neg_mul_symm, add_monoid_hom.map_add, conj_im, add_monoid_hom.map_sub, mul_neg_eq_neg_mul_symm, conj_re, neg_neg, mul_re] ... = re ⟪x, x⟫ - re (T† * ⟪y, x⟫) - re (T * ⟪x, y⟫) + re (T * T† * ⟪y, y⟫) : by simp only [inner_smul_left, inner_smul_right, mul_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ / ⟪y, y⟫ * ⟪y, x⟫) : by field_simp [-mul_re, hT, conj_div, h₁, h₃, inner_conj_sym] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / ⟪y, y⟫) : by rw [div_mul_eq_mul_div_comm, ←mul_div_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / re ⟪y, y⟫) : by conv_lhs { rw [h₄] } ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [div_re_of_real] ... = re ⟪x, x⟫ - abs (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [inner_mul_conj_re_abs] ... = re ⟪x, x⟫ - abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ : by rw is_R_or_C.abs_mul, have hmain' : abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ ≤ re ⟪x, x⟫ := by linarith, have := (mul_le_mul_right h₅).mpr hmain', rwa [div_mul_cancel (abs ⟪x, y⟫ * abs ⟪y, x⟫) h₆] at this } end /-- Cauchy–Schwarz inequality for real inner products. -/ lemma real_inner_mul_inner_self_le (x y : F) : ⟪x, y⟫_ℝ * ⟪x, y⟫_ℝ ≤ ⟪x, x⟫_ℝ * ⟪y, y⟫_ℝ := begin have h₁ : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [←inner_conj_sym]; refl, have h₂ := @inner_mul_inner_self_le ℝ F _ _ x y, dsimp at h₂, have h₃ := abs_mul_abs_self ⟪x, y⟫_ℝ, rw [h₁] at h₂, simpa [h₃] using h₂, end /-- A family of vectors is linearly independent if they are nonzero and orthogonal. -/ lemma linear_independent_of_ne_zero_of_inner_eq_zero {ι : Type*} {v : ι → E} (hz : ∀ i, v i ≠ 0) (ho : ∀ i j, i ≠ j → ⟪v i, v j⟫ = 0) : linear_independent 𝕜 v := begin rw linear_independent_iff', intros s g hg i hi, have h' : g i * inner (v i) (v i) = inner (v i) (∑ j in s, g j • v j), { rw inner_sum, symmetry, convert finset.sum_eq_single i _ _, { rw inner_smul_right }, { intros j hj hji, rw [inner_smul_right, ho i j hji.symm, mul_zero] }, { exact λ h, false.elim (h hi) } }, simpa [hg, hz] using h' end end basic_properties section orthonormal_sets variables {ι : Type*} (𝕜) include 𝕜 /-- An orthonormal set of vectors in an `inner_product_space` -/ def orthonormal (v : ι → E) : Prop := (∀ i, ∥v i∥ = 1) ∧ (∀ {i j}, i ≠ j → ⟪v i, v j⟫ = 0) omit 𝕜 variables {𝕜} /-- `if ... then ... else` characterization of an indexed set of vectors being orthonormal. (Inner product equals Kronecker delta.) -/ lemma orthonormal_iff_ite {v : ι → E} : orthonormal 𝕜 v ↔ ∀ i j, ⟪v i, v j⟫ = if i = j then (1:𝕜) else (0:𝕜) := begin split, { intros hv i j, split_ifs, { simp [h, inner_self_eq_norm_sq_to_K, hv.1] }, { exact hv.2 h } }, { intros h, split, { intros i, have h' : ∥v i∥ ^ 2 = 1 ^ 2 := by simp [norm_sq_eq_inner, h i i], have h₁ : 0 ≤ ∥v i∥ := norm_nonneg _, have h₂ : (0:ℝ) ≤ 1 := by norm_num, rwa eq_of_sq_eq_sq h₁ h₂ at h' }, { intros i j hij, simpa [hij] using h i j } } end /-- `if ... then ... else` characterization of a set of vectors being orthonormal. (Inner product equals Kronecker delta.) -/ theorem orthonormal_subtype_iff_ite {s : set E} : orthonormal 𝕜 (coe : s → E) ↔ (∀ v ∈ s, ∀ w ∈ s, ⟪v, w⟫ = if v = w then 1 else 0) := begin rw orthonormal_iff_ite, split, { intros h v hv w hw, convert h ⟨v, hv⟩ ⟨w, hw⟩ using 1, simp }, { rintros h ⟨v, hv⟩ ⟨w, hw⟩, convert h v hv w hw using 1, simp } end /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_right_finsupp {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι →₀ 𝕜) (i : ι) : ⟪v i, finsupp.total ι E 𝕜 v l⟫ = l i := by simp [finsupp.total_apply, finsupp.inner_sum, orthonormal_iff_ite.mp hv] /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_right_fintype [fintype ι] {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι → 𝕜) (i : ι) : ⟪v i, ∑ i : ι, (l i) • (v i)⟫ = l i := by simp [inner_sum, inner_smul_right, orthonormal_iff_ite.mp hv] /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_left_finsupp {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι →₀ 𝕜) (i : ι) : ⟪finsupp.total ι E 𝕜 v l, v i⟫ = conj (l i) := by rw [← inner_conj_sym, hv.inner_right_finsupp] /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_left_fintype [fintype ι] {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι → 𝕜) (i : ι) : ⟪∑ i : ι, (l i) • (v i), v i⟫ = conj (l i) := by simp [sum_inner, inner_smul_left, orthonormal_iff_ite.mp hv] /-- The double sum of weighted inner products of pairs of vectors from an orthonormal sequence is the sum of the weights. -/ lemma orthonormal.inner_left_right_finset {s : finset ι} {v : ι → E} (hv : orthonormal 𝕜 v) {a : ι → ι → 𝕜} : ∑ i in s, ∑ j in s, (a i j) • ⟪v j, v i⟫ = ∑ k in s, a k k := by simp [orthonormal_iff_ite.mp hv, finset.sum_ite_of_true] /-- An orthonormal set is linearly independent. -/ lemma orthonormal.linear_independent {v : ι → E} (hv : orthonormal 𝕜 v) : linear_independent 𝕜 v := begin rw linear_independent_iff, intros l hl, ext i, have key : ⟪v i, finsupp.total ι E 𝕜 v l⟫ = ⟪v i, 0⟫ := by rw hl, simpa [hv.inner_right_finsupp] using key end /-- A subfamily of an orthonormal family (i.e., a composition with an injective map) is an orthonormal family. -/ lemma orthonormal.comp {ι' : Type*} {v : ι → E} (hv : orthonormal 𝕜 v) (f : ι' → ι) (hf : function.injective f) : orthonormal 𝕜 (v ∘ f) := begin rw orthonormal_iff_ite at ⊢ hv, intros i j, convert hv (f i) (f j) using 1, simp [hf.eq_iff] end /-- A linear combination of some subset of an orthonormal set is orthogonal to other members of the set. -/ lemma orthonormal.inner_finsupp_eq_zero {v : ι → E} (hv : orthonormal 𝕜 v) {s : set ι} {i : ι} (hi : i ∉ s) {l : ι →₀ 𝕜} (hl : l ∈ finsupp.supported 𝕜 𝕜 s) : ⟪finsupp.total ι E 𝕜 v l, v i⟫ = 0 := begin rw finsupp.mem_supported' at hl, simp [hv.inner_left_finsupp, hl i hi], end /- The material that follows, culminating in the existence of a maximal orthonormal subset, is adapted from the corresponding development of the theory of linearly independents sets. See `exists_linear_independent` in particular. -/ variables (𝕜 E) lemma orthonormal_empty : orthonormal 𝕜 (λ x, x : (∅ : set E) → E) := by simp [orthonormal_subtype_iff_ite] variables {𝕜 E} lemma orthonormal_Union_of_directed {η : Type*} {s : η → set E} (hs : directed (⊆) s) (h : ∀ i, orthonormal 𝕜 (λ x, x : s i → E)) : orthonormal 𝕜 (λ x, x : (⋃ i, s i) → E) := begin rw orthonormal_subtype_iff_ite, rintros x ⟨_, ⟨i, rfl⟩, hxi⟩ y ⟨_, ⟨j, rfl⟩, hyj⟩, obtain ⟨k, hik, hjk⟩ := hs i j, have h_orth : orthonormal 𝕜 (λ x, x : (s k) → E) := h k, rw orthonormal_subtype_iff_ite at h_orth, exact h_orth x (hik hxi) y (hjk hyj) end lemma orthonormal_sUnion_of_directed {s : set (set E)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, orthonormal 𝕜 (λ x, x : (a : set E) → E)) : orthonormal 𝕜 (λ x, x : (⋃₀ s) → E) := by rw set.sUnion_eq_Union; exact orthonormal_Union_of_directed hs.directed_coe (by simpa using h) /-- Given an orthonormal set `v` of vectors in `E`, there exists a maximal orthonormal set containing it. -/ lemma exists_maximal_orthonormal {s : set E} (hs : orthonormal 𝕜 (coe : s → E)) : ∃ w ⊇ s, orthonormal 𝕜 (coe : w → E) ∧ ∀ u ⊇ w, orthonormal 𝕜 (coe : u → E) → u = w := begin rcases zorn.zorn_subset_nonempty {b | orthonormal 𝕜 (coe : b → E)} _ _ hs with ⟨b, bi, sb, h⟩, { refine ⟨b, sb, bi, _⟩, exact λ u hus hu, h u hu hus }, { refine λ c hc cc c0, ⟨⋃₀ c, _, _⟩, { exact orthonormal_sUnion_of_directed cc.directed_on (λ x xc, hc xc) }, { exact λ _, set.subset_sUnion_of_mem } } end lemma orthonormal.ne_zero {v : ι → E} (hv : orthonormal 𝕜 v) (i : ι) : v i ≠ 0 := begin have : ∥v i∥ ≠ 0, { rw hv.1 i, norm_num }, simpa using this end open finite_dimensional /-- A family of orthonormal vectors with the correct cardinality forms a basis. -/ def basis_of_orthonormal_of_card_eq_finrank [fintype ι] [nonempty ι] {v : ι → E} (hv : orthonormal 𝕜 v) (card_eq : fintype.card ι = finrank 𝕜 E) : basis ι 𝕜 E := basis_of_linear_independent_of_card_eq_finrank hv.linear_independent card_eq @[simp] lemma coe_basis_of_orthonormal_of_card_eq_finrank [fintype ι] [nonempty ι] {v : ι → E} (hv : orthonormal 𝕜 v) (card_eq : fintype.card ι = finrank 𝕜 E) : (basis_of_orthonormal_of_card_eq_finrank hv card_eq : ι → E) = v := coe_basis_of_linear_independent_of_card_eq_finrank _ _ end orthonormal_sets section norm lemma norm_eq_sqrt_inner (x : E) : ∥x∥ = sqrt (re ⟪x, x⟫) := begin have h₁ : ∥x∥^2 = re ⟪x, x⟫ := norm_sq_eq_inner x, have h₂ := congr_arg sqrt h₁, simpa using h₂, end lemma norm_eq_sqrt_real_inner (x : F) : ∥x∥ = sqrt ⟪x, x⟫_ℝ := by { have h := @norm_eq_sqrt_inner ℝ F _ _ x, simpa using h } lemma inner_self_eq_norm_sq (x : E) : re ⟪x, x⟫ = ∥x∥ * ∥x∥ := by rw[norm_eq_sqrt_inner, ←sqrt_mul inner_self_nonneg (re ⟪x, x⟫), sqrt_mul_self inner_self_nonneg] lemma real_inner_self_eq_norm_sq (x : F) : ⟪x, x⟫_ℝ = ∥x∥ * ∥x∥ := by { have h := @inner_self_eq_norm_sq ℝ F _ _ x, simpa using h } /-- Expand the square -/ lemma norm_add_sq {x y : E} : ∥x + y∥^2 = ∥x∥^2 + 2 * (re ⟪x, y⟫) + ∥y∥^2 := begin repeat {rw [sq, ←inner_self_eq_norm_sq]}, rw[inner_add_add_self, two_mul], simp only [add_assoc, add_left_inj, add_right_inj, add_monoid_hom.map_add], rw [←inner_conj_sym, conj_re], end alias norm_add_sq ← norm_add_pow_two /-- Expand the square -/ lemma norm_add_sq_real {x y : F} : ∥x + y∥^2 = ∥x∥^2 + 2 * ⟪x, y⟫_ℝ + ∥y∥^2 := by { have h := @norm_add_sq ℝ F _ _, simpa using h } alias norm_add_sq_real ← norm_add_pow_two_real /-- Expand the square -/ lemma norm_add_mul_self {x y : E} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * (re ⟪x, y⟫) + ∥y∥ * ∥y∥ := by { repeat {rw [← sq]}, exact norm_add_sq } /-- Expand the square -/ lemma norm_add_mul_self_real {x y : F} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * ⟪x, y⟫_ℝ + ∥y∥ * ∥y∥ := by { have h := @norm_add_mul_self ℝ F _ _, simpa using h } /-- Expand the square -/ lemma norm_sub_sq {x y : E} : ∥x - y∥^2 = ∥x∥^2 - 2 * (re ⟪x, y⟫) + ∥y∥^2 := begin repeat {rw [sq, ←inner_self_eq_norm_sq]}, rw[inner_sub_sub_self], calc re (⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫) = re ⟪x, x⟫ - re ⟪x, y⟫ - re ⟪y, x⟫ + re ⟪y, y⟫ : by simp ... = -re ⟪y, x⟫ - re ⟪x, y⟫ + re ⟪x, x⟫ + re ⟪y, y⟫ : by ring ... = -re (⟪x, y⟫†) - re ⟪x, y⟫ + re ⟪x, x⟫ + re ⟪y, y⟫ : by rw[inner_conj_sym] ... = -re ⟪x, y⟫ - re ⟪x, y⟫ + re ⟪x, x⟫ + re ⟪y, y⟫ : by rw[conj_re] ... = re ⟪x, x⟫ - 2*re ⟪x, y⟫ + re ⟪y, y⟫ : by ring end alias norm_sub_sq ← norm_sub_pow_two /-- Expand the square -/ lemma norm_sub_sq_real {x y : F} : ∥x - y∥^2 = ∥x∥^2 - 2 * ⟪x, y⟫_ℝ + ∥y∥^2 := norm_sub_sq alias norm_sub_sq_real ← norm_sub_pow_two_real /-- Expand the square -/ lemma norm_sub_mul_self {x y : E} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * re ⟪x, y⟫ + ∥y∥ * ∥y∥ := by { repeat {rw [← sq]}, exact norm_sub_sq } /-- Expand the square -/ lemma norm_sub_mul_self_real {x y : F} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * ⟪x, y⟫_ℝ + ∥y∥ * ∥y∥ := by { have h := @norm_sub_mul_self ℝ F _ _, simpa using h } /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : E) : abs ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_sq_le_sq (mul_nonneg (norm_nonneg _) (norm_nonneg _)) begin have : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = (re ⟪x, x⟫) * (re ⟪y, y⟫), simp only [inner_self_eq_norm_sq], ring, rw this, conv_lhs { congr, skip, rw [inner_abs_conj_sym] }, exact inner_mul_inner_self_le _ _ end /-- Cauchy–Schwarz inequality with norm -/ lemma abs_real_inner_le_norm (x y : F) : absR ⟪x, y⟫_ℝ ≤ ∥x∥ * ∥y∥ := by { have h := @abs_inner_le_norm ℝ F _ _ x y, simpa using h } /-- Cauchy–Schwarz inequality with norm -/ lemma real_inner_le_norm (x y : F) : ⟪x, y⟫_ℝ ≤ ∥x∥ * ∥y∥ := le_trans (le_abs_self _) (abs_real_inner_le_norm _ _) include 𝕜 lemma parallelogram_law_with_norm {x y : E} : ∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) := begin simp only [← inner_self_eq_norm_sq], rw[← re.map_add, parallelogram_law, two_mul, two_mul], simp only [re.map_add], end omit 𝕜 lemma parallelogram_law_with_norm_real {x y : F} : ∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) := by { have h := @parallelogram_law_with_norm ℝ F _ _ x y, simpa using h } /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ lemma re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (∥x + y∥ * ∥x + y∥ - ∥x∥ * ∥x∥ - ∥y∥ * ∥y∥) / 2 := by { rw norm_add_mul_self, ring } /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ lemma re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ - ∥x - y∥ * ∥x - y∥) / 2 := by { rw [norm_sub_mul_self], ring } /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ lemma re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four (x y : E) : re ⟪x, y⟫ = (∥x + y∥ * ∥x + y∥ - ∥x - y∥ * ∥x - y∥) / 4 := by { rw [norm_add_mul_self, norm_sub_mul_self], ring } /-- Polarization identity: The imaginary part of the inner product, in terms of the norm. -/ lemma im_inner_eq_norm_sub_I_smul_mul_self_sub_norm_add_I_smul_mul_self_div_four (x y : E) : im ⟪x, y⟫ = (∥x - IK • y∥ * ∥x - IK • y∥ - ∥x + IK • y∥ * ∥x + IK • y∥) / 4 := by { simp only [norm_add_mul_self, norm_sub_mul_self, inner_smul_right, I_mul_re], ring } /-- Polarization identity: The inner product, in terms of the norm. -/ lemma inner_eq_sum_norm_sq_div_four (x y : E) : ⟪x, y⟫ = (∥x + y∥ ^ 2 - ∥x - y∥ ^ 2 + (∥x - IK • y∥ ^ 2 - ∥x + IK • y∥ ^ 2) * IK) / 4 := begin rw [← re_add_im ⟪x, y⟫, re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four, im_inner_eq_norm_sub_I_smul_mul_self_sub_norm_add_I_smul_mul_self_div_four], push_cast, simp only [sq, ← mul_div_right_comm, ← add_div] end section variables {E' : Type*} [inner_product_space 𝕜 E'] /-- A linear isometry preserves the inner product. -/ @[simp] lemma linear_isometry.inner_map_map (f : E →ₗᵢ[𝕜] E') (x y : E) : ⟪f x, f y⟫ = ⟪x, y⟫ := by simp [inner_eq_sum_norm_sq_div_four, ← f.norm_map] /-- A linear isometric equivalence preserves the inner product. -/ @[simp] lemma linear_isometry_equiv.inner_map_map (f : E ≃ₗᵢ[𝕜] E') (x y : E) : ⟪f x, f y⟫ = ⟪x, y⟫ := f.to_linear_isometry.inner_map_map x y /-- A linear map that preserves the inner product is a linear isometry. -/ def linear_map.isometry_of_inner (f : E →ₗ[𝕜] E') (h : ∀ x y, ⟪f x, f y⟫ = ⟪x, y⟫) : E →ₗᵢ[𝕜] E' := ⟨f, λ x, by simp only [norm_eq_sqrt_inner, h]⟩ @[simp] lemma linear_map.coe_isometry_of_inner (f : E →ₗ[𝕜] E') (h) : ⇑(f.isometry_of_inner h) = f := rfl @[simp] lemma linear_map.isometry_of_inner_to_linear_map (f : E →ₗ[𝕜] E') (h) : (f.isometry_of_inner h).to_linear_map = f := rfl /-- A linear equivalence that preserves the inner product is a linear isometric equivalence. -/ def linear_equiv.isometry_of_inner (f : E ≃ₗ[𝕜] E') (h : ∀ x y, ⟪f x, f y⟫ = ⟪x, y⟫) : E ≃ₗᵢ[𝕜] E' := ⟨f, ((f : E →ₗ[𝕜] E').isometry_of_inner h).norm_map⟩ @[simp] lemma linear_equiv.coe_isometry_of_inner (f : E ≃ₗ[𝕜] E') (h) : ⇑(f.isometry_of_inner h) = f := rfl @[simp] lemma linear_equiv.isometry_of_inner_to_linear_equiv (f : E ≃ₗ[𝕜] E') (h) : (f.isometry_of_inner h).to_linear_equiv = f := rfl end /-- Polarization identity: The real inner product, in terms of the norm. -/ lemma real_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (∥x + y∥ * ∥x + y∥ - ∥x∥ * ∥x∥ - ∥y∥ * ∥y∥) / 2 := re_to_real.symm.trans $ re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two x y /-- Polarization identity: The real inner product, in terms of the norm. -/ lemma real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ - ∥x - y∥ * ∥x - y∥) / 2 := re_to_real.symm.trans $ re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two x y /-- Pythagorean theorem, if-and-only-if vector inner product form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ ⟪x, y⟫_ℝ = 0 := begin rw [norm_add_mul_self, add_right_cancel_iff, add_right_eq_self, mul_eq_zero], norm_num end /-- Pythagorean theorem, vector inner product form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (x y : E) (h : ⟪x, y⟫ = 0) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := begin rw [norm_add_mul_self, add_right_cancel_iff, add_right_eq_self, mul_eq_zero], apply or.inr, simp only [h, zero_re'], end /-- Pythagorean theorem, vector inner product form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector inner product form. -/ lemma norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ ⟪x, y⟫_ℝ = 0 := begin rw [norm_sub_mul_self, add_right_cancel_iff, sub_eq_add_neg, add_right_eq_self, neg_eq_zero, mul_eq_zero], norm_num end /-- Pythagorean theorem, subtracting vectors, vector inner product form. -/ lemma norm_sub_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- The sum and difference of two vectors are orthogonal if and only if they have the same norm. -/ lemma real_inner_add_sub_eq_zero_iff (x y : F) : ⟪x + y, x - y⟫_ℝ = 0 ↔ ∥x∥ = ∥y∥ := begin conv_rhs { rw ←mul_self_inj_of_nonneg (norm_nonneg _) (norm_nonneg _) }, simp only [←inner_self_eq_norm_sq, inner_add_left, inner_sub_right, real_inner_comm y x, sub_eq_zero, re_to_real], split, { intro h, rw [add_comm] at h, linarith }, { intro h, linarith } end /-- The real inner product of two vectors, divided by the product of their norms, has absolute value at most 1. -/ lemma abs_real_inner_div_norm_mul_norm_le_one (x y : F) : absR (⟪x, y⟫_ℝ / (∥x∥ * ∥y∥)) ≤ 1 := begin rw _root_.abs_div, by_cases h : 0 = absR (∥x∥ * ∥y∥), { rw [←h, div_zero], norm_num }, { change 0 ≠ absR (∥x∥ * ∥y∥) at h, rw div_le_iff' (lt_of_le_of_ne (ge_iff_le.mp (_root_.abs_nonneg (∥x∥ * ∥y∥))) h), convert abs_real_inner_le_norm x y using 1, rw [_root_.abs_mul, _root_.abs_of_nonneg (norm_nonneg x), _root_.abs_of_nonneg (norm_nonneg y), mul_one] } end /-- The inner product of a vector with a multiple of itself. -/ lemma real_inner_smul_self_left (x : F) (r : ℝ) : ⟪r • x, x⟫_ℝ = r * (∥x∥ * ∥x∥) := by rw [real_inner_smul_left, ←real_inner_self_eq_norm_sq] /-- The inner product of a vector with a multiple of itself. -/ lemma real_inner_smul_self_right (x : F) (r : ℝ) : ⟪x, r • x⟫_ℝ = r * (∥x∥ * ∥x∥) := by rw [inner_smul_right, ←real_inner_self_eq_norm_sq] /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ lemma abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : E} {r : 𝕜} (hx : x ≠ 0) (hr : r ≠ 0) : abs ⟪x, r • x⟫ / (∥x∥ * ∥r • x∥) = 1 := begin have hx' : ∥x∥ ≠ 0 := by simp [norm_eq_zero, hx], have hr' : abs r ≠ 0 := by simp [is_R_or_C.abs_eq_zero, hr], rw [inner_smul_right, is_R_or_C.abs_mul, ←inner_self_re_abs, inner_self_eq_norm_sq, norm_smul], rw [is_R_or_C.norm_eq_abs, ←mul_assoc, ←div_div_eq_div_mul, mul_div_cancel _ hx', ←div_div_eq_div_mul, mul_comm, mul_div_cancel _ hr', div_self hx'], end /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ lemma abs_real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : r ≠ 0) : absR ⟪x, r • x⟫_ℝ / (∥x∥ * ∥r • x∥) = 1 := begin rw ← abs_to_real, exact abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr end /-- The inner product of a nonzero vector with a positive multiple of itself, divided by the product of their norms, has value 1. -/ lemma real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : 0 < r) : ⟪x, r • x⟫_ℝ / (∥x∥ * ∥r • x∥) = 1 := begin rw [real_inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (absR r), mul_assoc, _root_.abs_of_nonneg (le_of_lt hr), div_self], exact mul_ne_zero (ne_of_gt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of a nonzero vector with a negative multiple of itself, divided by the product of their norms, has value -1. -/ lemma real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : r < 0) : ⟪x, r • x⟫_ℝ / (∥x∥ * ∥r • x∥) = -1 := begin rw [real_inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (absR r), mul_assoc, abs_of_neg hr, ←neg_mul_eq_neg_mul, div_neg_eq_neg_div, div_self], exact mul_ne_zero (ne_of_lt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of two vectors, divided by the product of their norms, has absolute value 1 if and only if they are nonzero and one is a multiple of the other. One form of equality case for Cauchy-Schwarz. -/ lemma abs_inner_div_norm_mul_norm_eq_one_iff (x y : E) : abs (⟪x, y⟫ / (∥x∥ * ∥y∥)) = 1 ↔ (x ≠ 0 ∧ ∃ (r : 𝕜), r ≠ 0 ∧ y = r • x) := begin split, { intro h, have hx0 : x ≠ 0, { intro hx0, rw [hx0, inner_zero_left, zero_div] at h, norm_num at h, }, refine and.intro hx0 _, set r := ⟪x, y⟫ / (∥x∥ * ∥x∥) with hr, use r, set t := y - r • x with ht, have ht0 : ⟪x, t⟫ = 0, { rw [ht, inner_sub_right, inner_smul_right, hr], norm_cast, rw [←inner_self_eq_norm_sq, inner_self_re_to_K, div_mul_cancel _ (λ h, hx0 (inner_self_eq_zero.1 h)), sub_self] }, replace h : ∥r • x∥ / ∥t + r • x∥ = 1, { rw [←sub_add_cancel y (r • x), ←ht, inner_add_right, ht0, zero_add, inner_smul_right, is_R_or_C.abs_div, is_R_or_C.abs_mul, ←inner_self_re_abs, inner_self_eq_norm_sq] at h, norm_cast at h, rwa [_root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm, ←mul_assoc, mul_comm, mul_div_mul_left _ _ (λ h, hx0 (norm_eq_zero.1 h)), ←is_R_or_C.norm_eq_abs, ←norm_smul] at h }, have hr0 : r ≠ 0, { intro hr0, rw [hr0, zero_smul, norm_zero, zero_div] at h, norm_num at h }, refine and.intro hr0 _, have h2 : ∥r • x∥ ^ 2 = ∥t + r • x∥ ^ 2, { rw [eq_of_div_eq_one h] }, replace h2 : ⟪r • x, r • x⟫ = ⟪t, t⟫ + ⟪t, r • x⟫ + ⟪r • x, t⟫ + ⟪r • x, r • x⟫, { rw [sq, sq, ←inner_self_eq_norm_sq, ←inner_self_eq_norm_sq ] at h2, have h2' := congr_arg (λ z : ℝ, (z : 𝕜)) h2, simp_rw [inner_self_re_to_K, inner_add_add_self] at h2', exact h2' }, conv at h2 in ⟪r • x, t⟫ { rw [inner_smul_left, ht0, mul_zero] }, symmetry' at h2, have h₁ : ⟪t, r • x⟫ = 0 := by { rw [inner_smul_right, ←inner_conj_sym, ht0], simp }, rw [add_zero, h₁, add_left_eq_self, add_zero, inner_self_eq_zero] at h2, rw h2 at ht, exact eq_of_sub_eq_zero ht.symm }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw [hy, is_R_or_C.abs_div], norm_cast, rw [_root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm], exact abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has absolute value 1 if and only if they are nonzero and one is a multiple of the other. One form of equality case for Cauchy-Schwarz. -/ lemma abs_real_inner_div_norm_mul_norm_eq_one_iff (x y : F) : absR (⟪x, y⟫_ℝ / (∥x∥ * ∥y∥)) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r ≠ 0 ∧ y = r • x) := begin have := @abs_inner_div_norm_mul_norm_eq_one_iff ℝ F _ _ x y, simpa [coe_real_eq_id] using this, end /-- If the inner product of two vectors is equal to the product of their norms, then the two vectors are multiples of each other. One form of the equality case for Cauchy-Schwarz. Compare `inner_eq_norm_mul_iff`, which takes the stronger hypothesis `⟪x, y⟫ = ∥x∥ * ∥y∥`. -/ lemma abs_inner_eq_norm_iff (x y : E) (hx0 : x ≠ 0) (hy0 : y ≠ 0): abs ⟪x, y⟫ = ∥x∥ * ∥y∥ ↔ ∃ (r : 𝕜), r ≠ 0 ∧ y = r • x := begin have hx0' : ∥x∥ ≠ 0 := by simp [norm_eq_zero, hx0], have hy0' : ∥y∥ ≠ 0 := by simp [norm_eq_zero, hy0], have hxy0 : ∥x∥ * ∥y∥ ≠ 0 := by simp [hx0', hy0'], have h₁ : abs ⟪x, y⟫ = ∥x∥ * ∥y∥ ↔ abs (⟪x, y⟫ / (∥x∥ * ∥y∥)) = 1, { refine ⟨_ ,_⟩, { intro h, norm_cast, rw [is_R_or_C.abs_div, h, abs_of_real, _root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm], exact div_self hxy0 }, { intro h, norm_cast at h, rwa [is_R_or_C.abs_div, abs_of_real, _root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm, div_eq_one_iff_eq hxy0] at h } }, rw [h₁, abs_inner_div_norm_mul_norm_eq_one_iff x y], have : x ≠ 0 := λ h, (hx0' $ norm_eq_zero.mpr h), simp [this] end /-- The inner product of two vectors, divided by the product of their norms, has value 1 if and only if they are nonzero and one is a positive multiple of the other. -/ lemma real_inner_div_norm_mul_norm_eq_one_iff (x y : F) : ⟪x, y⟫_ℝ / (∥x∥ * ∥y∥) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun absR at ha, norm_num at ha, rcases (abs_real_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrneg, rw hy at h, rw real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx (lt_of_le_of_ne (le_of_not_lt hrneg) hr) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has value -1 if and only if they are nonzero and one is a negative multiple of the other. -/ lemma real_inner_div_norm_mul_norm_eq_neg_one_iff (x y : F) : ⟪x, y⟫_ℝ / (∥x∥ * ∥y∥) = -1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun absR at ha, norm_num at ha, rcases (abs_real_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrpos, rw hy at h, rw real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx (lt_of_le_of_ne (le_of_not_lt hrpos) hr.symm) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx hr } end /-- If the inner product of two vectors is equal to the product of their norms (i.e., `⟪x, y⟫ = ∥x∥ * ∥y∥`), then the two vectors are nonnegative real multiples of each other. One form of the equality case for Cauchy-Schwarz. Compare `abs_inner_eq_norm_iff`, which takes the weaker hypothesis `abs ⟪x, y⟫ = ∥x∥ * ∥y∥`. -/ lemma inner_eq_norm_mul_iff {x y : E} : ⟪x, y⟫ = (∥x∥ : 𝕜) * ∥y∥ ↔ (∥y∥ : 𝕜) • x = (∥x∥ : 𝕜) • y := begin by_cases h : (x = 0 ∨ y = 0), -- WLOG `x` and `y` are nonzero { cases h; simp [h] }, calc ⟪x, y⟫ = (∥x∥ : 𝕜) * ∥y∥ ↔ ∥x∥ * ∥y∥ = re ⟪x, y⟫ : begin norm_cast, split, { intros h', simp [h'] }, { have cauchy_schwarz := abs_inner_le_norm x y, intros h', rw h' at ⊢ cauchy_schwarz, rwa re_eq_self_of_le } end ... ↔ 2 * ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥ - re ⟪x, y⟫) = 0 : by simp [h, show (2:ℝ) ≠ 0, by norm_num, sub_eq_zero] ... ↔ ∥(∥y∥:𝕜) • x - (∥x∥:𝕜) • y∥ * ∥(∥y∥:𝕜) • x - (∥x∥:𝕜) • y∥ = 0 : begin simp only [norm_sub_mul_self, inner_smul_left, inner_smul_right, norm_smul, conj_of_real, is_R_or_C.norm_eq_abs, abs_of_real, of_real_im, of_real_re, mul_re, abs_norm_eq_norm], refine eq.congr _ rfl, ring end ... ↔ (∥y∥ : 𝕜) • x = (∥x∥ : 𝕜) • y : by simp [norm_sub_eq_zero_iff] end /-- If the inner product of two vectors is equal to the product of their norms (i.e., `⟪x, y⟫ = ∥x∥ * ∥y∥`), then the two vectors are nonnegative real multiples of each other. One form of the equality case for Cauchy-Schwarz. Compare `abs_inner_eq_norm_iff`, which takes the weaker hypothesis `abs ⟪x, y⟫ = ∥x∥ * ∥y∥`. -/ lemma inner_eq_norm_mul_iff_real {x y : F} : ⟪x, y⟫_ℝ = ∥x∥ * ∥y∥ ↔ ∥y∥ • x = ∥x∥ • y := inner_eq_norm_mul_iff /-- If the inner product of two unit vectors is `1`, then the two vectors are equal. One form of the equality case for Cauchy-Schwarz. -/ lemma inner_eq_norm_mul_iff_of_norm_one {x y : E} (hx : ∥x∥ = 1) (hy : ∥y∥ = 1) : ⟪x, y⟫ = 1 ↔ x = y := by { convert inner_eq_norm_mul_iff using 2; simp [hx, hy] } lemma inner_lt_norm_mul_iff_real {x y : F} : ⟪x, y⟫_ℝ < ∥x∥ * ∥y∥ ↔ ∥y∥ • x ≠ ∥x∥ • y := calc ⟪x, y⟫_ℝ < ∥x∥ * ∥y∥ ↔ ⟪x, y⟫_ℝ ≠ ∥x∥ * ∥y∥ : ⟨ne_of_lt, lt_of_le_of_ne (real_inner_le_norm _ _)⟩ ... ↔ ∥y∥ • x ≠ ∥x∥ • y : not_congr inner_eq_norm_mul_iff_real /-- If the inner product of two unit vectors is strictly less than `1`, then the two vectors are distinct. One form of the equality case for Cauchy-Schwarz. -/ lemma inner_lt_one_iff_real_of_norm_one {x y : F} (hx : ∥x∥ = 1) (hy : ∥y∥ = 1) : ⟪x, y⟫_ℝ < 1 ↔ x ≠ y := by { convert inner_lt_norm_mul_iff_real; simp [hx, hy] } /-- The inner product of two weighted sums, where the weights in each sum add to 0, in terms of the norms of pairwise differences. -/ lemma inner_sum_smul_sum_smul_of_sum_eq_zero {ι₁ : Type*} {s₁ : finset ι₁} {w₁ : ι₁ → ℝ} (v₁ : ι₁ → F) (h₁ : ∑ i in s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : finset ι₂} {w₂ : ι₂ → ℝ} (v₂ : ι₂ → F) (h₂ : ∑ i in s₂, w₂ i = 0) : ⟪(∑ i₁ in s₁, w₁ i₁ • v₁ i₁), (∑ i₂ in s₂, w₂ i₂ • v₂ i₂)⟫_ℝ = (-∑ i₁ in s₁, ∑ i₂ in s₂, w₁ i₁ * w₂ i₂ * (∥v₁ i₁ - v₂ i₂∥ * ∥v₁ i₁ - v₂ i₂∥)) / 2 := by simp_rw [sum_inner, inner_sum, real_inner_smul_left, real_inner_smul_right, real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two, ←div_sub_div_same, ←div_add_div_same, mul_sub_left_distrib, left_distrib, finset.sum_sub_distrib, finset.sum_add_distrib, ←finset.mul_sum, ←finset.sum_mul, h₁, h₂, zero_mul, mul_zero, finset.sum_const_zero, zero_add, zero_sub, finset.mul_sum, neg_div, finset.sum_div, mul_div_assoc, mul_assoc] /-- The inner product with a fixed left element, as a continuous linear map. This can be upgraded to a continuous map which is jointly conjugate-linear in the left argument and linear in the right argument, once (TODO) conjugate-linear maps have been defined. -/ def inner_right (v : E) : E →L[𝕜] 𝕜 := linear_map.mk_continuous { to_fun := λ w, ⟪v, w⟫, map_add' := λ x y, inner_add_right, map_smul' := λ c x, inner_smul_right } ∥v∥ (by simpa [is_R_or_C.norm_eq_abs] using abs_inner_le_norm v) @[simp] lemma inner_right_coe (v : E) : (inner_right v : E → 𝕜) = λ w, ⟪v, w⟫ := rfl @[simp] lemma inner_right_apply (v w : E) : inner_right v w = ⟪v, w⟫ := rfl end norm section bessels_inequality variables {ι: Type*} (x : E) {v : ι → E} /-- Bessel's inequality for finite sums. -/ lemma orthonormal.sum_inner_products_le {s : finset ι} (hv : orthonormal 𝕜 v) : ∑ i in s, ∥⟪v i, x⟫∥ ^ 2 ≤ ∥x∥ ^ 2 := begin have h₂ : ∑ i in s, ∑ j in s, ⟪v i, x⟫ * ⟪x, v j⟫ * ⟪v j, v i⟫ = (∑ k in s, (⟪v k, x⟫ * ⟪x, v k⟫) : 𝕜), { exact hv.inner_left_right_finset }, have h₃ : ∀ z : 𝕜, re (z * conj (z)) = ∥z∥ ^ 2, { intro z, simp only [mul_conj, norm_sq_eq_def'], norm_cast, }, suffices hbf: ∥x - ∑ i in s, ⟪v i, x⟫ • (v i)∥ ^ 2 = ∥x∥ ^ 2 - ∑ i in s, ∥⟪v i, x⟫∥ ^ 2, { rw [←sub_nonneg, ←hbf], simp only [norm_nonneg, pow_nonneg], }, rw [norm_sub_sq, sub_add], simp only [inner_product_space.norm_sq_eq_inner, inner_sum], simp only [sum_inner, two_mul, inner_smul_right, inner_conj_sym, ←mul_assoc, h₂, ←h₃, inner_conj_sym, add_monoid_hom.map_sum, finset.mul_sum, ←finset.sum_sub_distrib, inner_smul_left, add_sub_cancel'], end /-- Bessel's inequality. -/ lemma orthonormal.tsum_inner_products_le (hv : orthonormal 𝕜 v) : ∑' i, ∥⟪v i, x⟫∥ ^ 2 ≤ ∥x∥ ^ 2 := begin refine tsum_le_of_sum_le' _ (λ s, hv.sum_inner_products_le x), simp only [norm_nonneg, pow_nonneg] end /-- The sum defined in Bessel's inequality is summable. -/ lemma orthonormal.inner_products_summable (hv : orthonormal 𝕜 v) : summable (λ i, ∥⟪v i, x⟫∥ ^ 2) := begin by_cases hnon : nonempty ι, { use Sup (set.range (λ s : finset ι, ∑ i in s, ∥⟪v i, x⟫∥ ^ 2)), apply has_sum_of_is_lub_of_nonneg, { intro b, simp only [norm_nonneg, pow_nonneg], }, { refine is_lub_cSup (set.range_nonempty _) _, use ∥x∥ ^ 2, rintro y ⟨s, rfl⟩, exact hv.sum_inner_products_le x, }, }, { rw not_nonempty_iff at hnon, haveI := hnon, exact summable_empty, }, end end bessels_inequality /-- A field `𝕜` satisfying `is_R_or_C` is itself a `𝕜`-inner product space. -/ instance is_R_or_C.inner_product_space : inner_product_space 𝕜 𝕜 := { inner := (λ x y, (conj x) * y), norm_sq_eq_inner := λ x, by { unfold inner, rw [mul_comm, mul_conj, of_real_re, norm_sq_eq_def'] }, conj_sym := λ x y, by simp [mul_comm], add_left := λ x y z, by simp [inner, add_mul], smul_left := λ x y z, by simp [inner, mul_assoc] } @[simp] lemma is_R_or_C.inner_apply (x y : 𝕜) : ⟪x, y⟫ = (conj x) * y := rfl /-! ### Inner product space structure on subspaces -/ /-- Induced inner product on a submodule. -/ instance submodule.inner_product_space (W : submodule 𝕜 E) : inner_product_space 𝕜 W := { inner := λ x y, ⟪(x:E), (y:E)⟫, conj_sym := λ _ _, inner_conj_sym _ _ , norm_sq_eq_inner := λ _, norm_sq_eq_inner _, add_left := λ _ _ _ , inner_add_left, smul_left := λ _ _ _, inner_smul_left, ..submodule.normed_space W } /-- The inner product on submodules is the same as on the ambient space. -/ @[simp] lemma submodule.coe_inner (W : submodule 𝕜 E) (x y : W) : ⟪x, y⟫ = ⟪(x:E), ↑y⟫ := rfl section is_R_or_C_to_real variables {G : Type*} variables (𝕜 E) include 𝕜 /-- A general inner product implies a real inner product. This is not registered as an instance since it creates problems with the case `𝕜 = ℝ`. -/ def has_inner.is_R_or_C_to_real : has_inner ℝ E := { inner := λ x y, re ⟪x, y⟫ } /-- A general inner product space structure implies a real inner product structure. This is not registered as an instance since it creates problems with the case `𝕜 = ℝ`, but in can be used in a proof to obtain a real inner product space structure from a given `𝕜`-inner product space structure. -/ def inner_product_space.is_R_or_C_to_real : inner_product_space ℝ E := { norm_sq_eq_inner := norm_sq_eq_inner, conj_sym := λ x y, inner_re_symm, add_left := λ x y z, by { change re ⟪x + y, z⟫ = re ⟪x, z⟫ + re ⟪y, z⟫, simp [inner_add_left] }, smul_left := λ x y r, by { change re ⟪(r : 𝕜) • x, y⟫ = r * re ⟪x, y⟫, simp [inner_smul_left] }, ..has_inner.is_R_or_C_to_real 𝕜 E, ..normed_space.restrict_scalars ℝ 𝕜 E } variable {E} lemma real_inner_eq_re_inner (x y : E) : @has_inner.inner ℝ E (has_inner.is_R_or_C_to_real 𝕜 E) x y = re ⟪x, y⟫ := rfl omit 𝕜 /-- A complex inner product implies a real inner product -/ instance inner_product_space.complex_to_real [inner_product_space ℂ G] : inner_product_space ℝ G := inner_product_space.is_R_or_C_to_real ℂ G end is_R_or_C_to_real section deriv /-! ### Derivative of the inner product In this section we prove that the inner product and square of the norm in an inner space are infinitely `ℝ`-smooth. In order to state these results, we need a `normed_space ℝ E` instance. Though we can deduce this structure from `inner_product_space 𝕜 E`, this instance may be not definitionally equal to some other “natural” instance. So, we assume `[normed_space ℝ E]` and `[is_scalar_tower ℝ 𝕜 E]`. In both interesting cases `𝕜 = ℝ` and `𝕜 = ℂ` we have these instances. -/ variables [normed_space ℝ E] [is_scalar_tower ℝ 𝕜 E] lemma is_bounded_bilinear_map_inner : is_bounded_bilinear_map ℝ (λ p : E × E, ⟪p.1, p.2⟫) := { add_left := λ _ _ _, inner_add_left, smul_left := λ r x y, by simp only [← algebra_map_smul 𝕜 r x, algebra_map_eq_of_real, inner_smul_real_left], add_right := λ _ _ _, inner_add_right, smul_right := λ r x y, by simp only [← algebra_map_smul 𝕜 r y, algebra_map_eq_of_real, inner_smul_real_right], bound := ⟨1, zero_lt_one, λ x y, by { rw [one_mul, is_R_or_C.norm_eq_abs], exact abs_inner_le_norm x y, }⟩ } /-- Derivative of the inner product. -/ def fderiv_inner_clm (p : E × E) : E × E →L[ℝ] 𝕜 := is_bounded_bilinear_map_inner.deriv p @[simp] lemma fderiv_inner_clm_apply (p x : E × E) : fderiv_inner_clm p x = ⟪p.1, x.2⟫ + ⟪x.1, p.2⟫ := rfl lemma times_cont_diff_inner {n} : times_cont_diff ℝ n (λ p : E × E, ⟪p.1, p.2⟫) := is_bounded_bilinear_map_inner.times_cont_diff lemma times_cont_diff_at_inner {p : E × E} {n} : times_cont_diff_at ℝ n (λ p : E × E, ⟪p.1, p.2⟫) p := times_cont_diff_inner.times_cont_diff_at lemma differentiable_inner : differentiable ℝ (λ p : E × E, ⟪p.1, p.2⟫) := is_bounded_bilinear_map_inner.differentiable_at variables {G : Type*} [normed_group G] [normed_space ℝ G] {f g : G → E} {f' g' : G →L[ℝ] E} {s : set G} {x : G} {n : with_top ℕ} include 𝕜 lemma times_cont_diff_within_at.inner (hf : times_cont_diff_within_at ℝ n f s x) (hg : times_cont_diff_within_at ℝ n g s x) : times_cont_diff_within_at ℝ n (λ x, ⟪f x, g x⟫) s x := times_cont_diff_at_inner.comp_times_cont_diff_within_at x (hf.prod hg) lemma times_cont_diff_at.inner (hf : times_cont_diff_at ℝ n f x) (hg : times_cont_diff_at ℝ n g x) : times_cont_diff_at ℝ n (λ x, ⟪f x, g x⟫) x := hf.inner hg lemma times_cont_diff_on.inner (hf : times_cont_diff_on ℝ n f s) (hg : times_cont_diff_on ℝ n g s) : times_cont_diff_on ℝ n (λ x, ⟪f x, g x⟫) s := λ x hx, (hf x hx).inner (hg x hx) lemma times_cont_diff.inner (hf : times_cont_diff ℝ n f) (hg : times_cont_diff ℝ n g) : times_cont_diff ℝ n (λ x, ⟪f x, g x⟫) := times_cont_diff_inner.comp (hf.prod hg) lemma has_fderiv_within_at.inner (hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) : has_fderiv_within_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') s x := (is_bounded_bilinear_map_inner.has_fderiv_at (f x, g x)).comp_has_fderiv_within_at x (hf.prod hg) lemma has_fderiv_at.inner (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) : has_fderiv_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') x := (is_bounded_bilinear_map_inner.has_fderiv_at (f x, g x)).comp x (hf.prod hg) lemma has_deriv_within_at.inner {f g : ℝ → E} {f' g' : E} {s : set ℝ} {x : ℝ} (hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) : has_deriv_within_at (λ t, ⟪f t, g t⟫) (⟪f x, g'⟫ + ⟪f', g x⟫) s x := by simpa using (hf.has_fderiv_within_at.inner hg.has_fderiv_within_at).has_deriv_within_at lemma has_deriv_at.inner {f g : ℝ → E} {f' g' : E} {x : ℝ} : has_deriv_at f f' x → has_deriv_at g g' x → has_deriv_at (λ t, ⟪f t, g t⟫) (⟪f x, g'⟫ + ⟪f', g x⟫) x := by simpa only [← has_deriv_within_at_univ] using has_deriv_within_at.inner lemma differentiable_within_at.inner (hf : differentiable_within_at ℝ f s x) (hg : differentiable_within_at ℝ g s x) : differentiable_within_at ℝ (λ x, ⟪f x, g x⟫) s x := ((differentiable_inner _).has_fderiv_at.comp_has_fderiv_within_at x (hf.prod hg).has_fderiv_within_at).differentiable_within_at lemma differentiable_at.inner (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) : differentiable_at ℝ (λ x, ⟪f x, g x⟫) x := (differentiable_inner _).comp x (hf.prod hg) lemma differentiable_on.inner (hf : differentiable_on ℝ f s) (hg : differentiable_on ℝ g s) : differentiable_on ℝ (λ x, ⟪f x, g x⟫) s := λ x hx, (hf x hx).inner (hg x hx) lemma differentiable.inner (hf : differentiable ℝ f) (hg : differentiable ℝ g) : differentiable ℝ (λ x, ⟪f x, g x⟫) := λ x, (hf x).inner (hg x) lemma fderiv_inner_apply (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) (y : G) : fderiv ℝ (λ t, ⟪f t, g t⟫) x y = ⟪f x, fderiv ℝ g x y⟫ + ⟪fderiv ℝ f x y, g x⟫ := by { rw [(hf.has_fderiv_at.inner hg.has_fderiv_at).fderiv], refl } lemma deriv_inner_apply {f g : ℝ → E} {x : ℝ} (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) : deriv (λ t, ⟪f t, g t⟫) x = ⟪f x, deriv g x⟫ + ⟪deriv f x, g x⟫ := (hf.has_deriv_at.inner hg.has_deriv_at).deriv lemma times_cont_diff_norm_sq : times_cont_diff ℝ n (λ x : E, ∥x∥ ^ 2) := begin simp only [sq, ← inner_self_eq_norm_sq], exact (re_clm : 𝕜 →L[ℝ] ℝ).times_cont_diff.comp (times_cont_diff_id.inner times_cont_diff_id) end lemma times_cont_diff.norm_sq (hf : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, ∥f x∥ ^ 2) := times_cont_diff_norm_sq.comp hf lemma times_cont_diff_within_at.norm_sq (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ y, ∥f y∥ ^ 2) s x := times_cont_diff_norm_sq.times_cont_diff_at.comp_times_cont_diff_within_at x hf lemma times_cont_diff_at.norm_sq (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ y, ∥f y∥ ^ 2) x := hf.norm_sq lemma times_cont_diff_at_norm {x : E} (hx : x ≠ 0) : times_cont_diff_at ℝ n norm x := have ∥id x∥ ^ 2 ≠ 0, from pow_ne_zero _ (norm_pos_iff.2 hx).ne', by simpa only [id, sqrt_sq, norm_nonneg] using times_cont_diff_at_id.norm_sq.sqrt this lemma times_cont_diff_at.norm (hf : times_cont_diff_at ℝ n f x) (h0 : f x ≠ 0) : times_cont_diff_at ℝ n (λ y, ∥f y∥) x := (times_cont_diff_at_norm h0).comp x hf lemma times_cont_diff_at.dist (hf : times_cont_diff_at ℝ n f x) (hg : times_cont_diff_at ℝ n g x) (hne : f x ≠ g x) : times_cont_diff_at ℝ n (λ y, dist (f y) (g y)) x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma times_cont_diff_within_at.norm (hf : times_cont_diff_within_at ℝ n f s x) (h0 : f x ≠ 0) : times_cont_diff_within_at ℝ n (λ y, ∥f y∥) s x := (times_cont_diff_at_norm h0).comp_times_cont_diff_within_at x hf lemma times_cont_diff_within_at.dist (hf : times_cont_diff_within_at ℝ n f s x) (hg : times_cont_diff_within_at ℝ n g s x) (hne : f x ≠ g x) : times_cont_diff_within_at ℝ n (λ y, dist (f y) (g y)) s x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma times_cont_diff_on.norm_sq (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ y, ∥f y∥ ^ 2) s := (λ x hx, (hf x hx).norm_sq) lemma times_cont_diff_on.norm (hf : times_cont_diff_on ℝ n f s) (h0 : ∀ x ∈ s, f x ≠ 0) : times_cont_diff_on ℝ n (λ y, ∥f y∥) s := λ x hx, (hf x hx).norm (h0 x hx) lemma times_cont_diff_on.dist (hf : times_cont_diff_on ℝ n f s) (hg : times_cont_diff_on ℝ n g s) (hne : ∀ x ∈ s, f x ≠ g x) : times_cont_diff_on ℝ n (λ y, dist (f y) (g y)) s := λ x hx, (hf x hx).dist (hg x hx) (hne x hx) lemma times_cont_diff.norm (hf : times_cont_diff ℝ n f) (h0 : ∀ x, f x ≠ 0) : times_cont_diff ℝ n (λ y, ∥f y∥) := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.norm (h0 x) lemma times_cont_diff.dist (hf : times_cont_diff ℝ n f) (hg : times_cont_diff ℝ n g) (hne : ∀ x, f x ≠ g x) : times_cont_diff ℝ n (λ y, dist (f y) (g y)) := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.dist hg.times_cont_diff_at (hne x) lemma differentiable_at.norm_sq (hf : differentiable_at ℝ f x) : differentiable_at ℝ (λ y, ∥f y∥ ^ 2) x := (times_cont_diff_at_id.norm_sq.differentiable_at le_rfl).comp x hf lemma differentiable_at.norm (hf : differentiable_at ℝ f x) (h0 : f x ≠ 0) : differentiable_at ℝ (λ y, ∥f y∥) x := ((times_cont_diff_at_norm h0).differentiable_at le_rfl).comp x hf lemma differentiable_at.dist (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) (hne : f x ≠ g x) : differentiable_at ℝ (λ y, dist (f y) (g y)) x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma differentiable.norm_sq (hf : differentiable ℝ f) : differentiable ℝ (λ y, ∥f y∥ ^ 2) := λ x, (hf x).norm_sq lemma differentiable.norm (hf : differentiable ℝ f) (h0 : ∀ x, f x ≠ 0) : differentiable ℝ (λ y, ∥f y∥) := λ x, (hf x).norm (h0 x) lemma differentiable.dist (hf : differentiable ℝ f) (hg : differentiable ℝ g) (hne : ∀ x, f x ≠ g x) : differentiable ℝ (λ y, dist (f y) (g y)) := λ x, (hf x).dist (hg x) (hne x) lemma differentiable_within_at.norm_sq (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ y, ∥f y∥ ^ 2) s x := (times_cont_diff_at_id.norm_sq.differentiable_at le_rfl).comp_differentiable_within_at x hf lemma differentiable_within_at.norm (hf : differentiable_within_at ℝ f s x) (h0 : f x ≠ 0) : differentiable_within_at ℝ (λ y, ∥f y∥) s x := ((times_cont_diff_at_id.norm h0).differentiable_at le_rfl).comp_differentiable_within_at x hf lemma differentiable_within_at.dist (hf : differentiable_within_at ℝ f s x) (hg : differentiable_within_at ℝ g s x) (hne : f x ≠ g x) : differentiable_within_at ℝ (λ y, dist (f y) (g y)) s x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma differentiable_on.norm_sq (hf : differentiable_on ℝ f s) : differentiable_on ℝ (λ y, ∥f y∥ ^ 2) s := λ x hx, (hf x hx).norm_sq lemma differentiable_on.norm (hf : differentiable_on ℝ f s) (h0 : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λ y, ∥f y∥) s := λ x hx, (hf x hx).norm (h0 x hx) lemma differentiable_on.dist (hf : differentiable_on ℝ f s) (hg : differentiable_on ℝ g s) (hne : ∀ x ∈ s, f x ≠ g x) : differentiable_on ℝ (λ y, dist (f y) (g y)) s := λ x hx, (hf x hx).dist (hg x hx) (hne x hx) end deriv section continuous /-! ### Continuity of the inner product Since the inner product is `ℝ`-smooth, it is continuous. We do not need a `[normed_space ℝ E]` structure to *state* this fact and its corollaries, so we introduce them in the proof instead. -/ lemma continuous_inner : continuous (λ p : E × E, ⟪p.1, p.2⟫) := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : is_scalar_tower ℝ 𝕜 E := restrict_scalars.is_scalar_tower _ _ _, exact differentiable_inner.continuous end variables {α : Type*} lemma filter.tendsto.inner {f g : α → E} {l : filter α} {x y : E} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) : tendsto (λ t, ⟪f t, g t⟫) l (𝓝 ⟪x, y⟫) := (continuous_inner.tendsto _).comp (hf.prod_mk_nhds hg) variables [topological_space α] {f g : α → E} {x : α} {s : set α} include 𝕜 lemma continuous_within_at.inner (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (λ t, ⟪f t, g t⟫) s x := hf.inner hg lemma continuous_at.inner (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λ t, ⟪f t, g t⟫) x := hf.inner hg lemma continuous_on.inner (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λ t, ⟪f t, g t⟫) s := λ x hx, (hf x hx).inner (hg x hx) lemma continuous.inner (hf : continuous f) (hg : continuous g) : continuous (λ t, ⟪f t, g t⟫) := continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.inner hg.continuous_at end continuous /-! ### Orthogonal projection in inner product spaces -/ section orthogonal open filter /-- Existence of minimizers Let `u` be a point in a real inner product space, and let `K` be a nonempty complete convex subset. Then there exists a (unique) `v` in `K` that minimizes the distance `∥u - v∥` to `u`. -/ -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces, -- perhaps with individual statements for the three steps below. theorem exists_norm_eq_infi_of_complete_convex {K : set F} (ne : K.nonempty) (h₁ : is_complete K) (h₂ : convex K) : ∀ u : F, ∃ v ∈ K, ∥u - v∥ = ⨅ w : K, ∥u - w∥ := assume u, begin let δ := ⨅ w : K, ∥u - w∥, letI : nonempty K := ne.to_subtype, have zero_le_δ : 0 ≤ δ := le_cinfi (λ _, norm_nonneg _), have δ_le : ∀ w : K, δ ≤ ∥u - w∥, from cinfi_le ⟨0, set.forall_range_iff.2 $ λ _, norm_nonneg _⟩, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, -- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K` -- such that `∥u - w n∥ < δ + 1 / (n + 1)` (which implies `∥u - w n∥ --> δ`); -- maybe this should be a separate lemma have exists_seq : ∃ w : ℕ → K, ∀ n, ∥u - w n∥ < δ + 1 / (n + 1), { have hδ : ∀n:ℕ, δ < δ + 1 / (n + 1), from λ n, lt_add_of_le_of_pos (le_refl _) nat.one_div_pos_of_nat, have h := λ n, exists_lt_of_cinfi_lt (hδ n), let w : ℕ → K := λ n, classical.some (h n), exact ⟨w, λ n, classical.some_spec (h n)⟩ }, rcases exists_seq with ⟨w, hw⟩, have norm_tendsto : tendsto (λ n, ∥u - w n∥) at_top (nhds δ), { have h : tendsto (λ n:ℕ, δ) at_top (nhds δ) := tendsto_const_nhds, have h' : tendsto (λ n:ℕ, δ + 1 / (n + 1)) at_top (nhds δ), { convert h.add tendsto_one_div_add_at_top_nhds_0_nat, simp only [add_zero] }, exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h' (λ x, δ_le _) (λ x, le_of_lt (hw _)) }, -- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence have seq_is_cauchy : cauchy_seq (λ n, ((w n):F)), { rw cauchy_seq_iff_le_tendsto_0, -- splits into three goals let b := λ n:ℕ, (8 * δ * (1/(n+1)) + 4 * (1/(n+1)) * (1/(n+1))), use (λn, sqrt (b n)), split, -- first goal : `∀ (n : ℕ), 0 ≤ sqrt (b n)` assume n, exact sqrt_nonneg _, split, -- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ sqrt (b N)` assume p q N hp hq, let wp := ((w p):F), let wq := ((w q):F), let a := u - wq, let b := u - wp, let half := 1 / (2:ℝ), let div := 1 / ((N:ℝ) + 1), have : 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) := calc 4 * ∥u - half•(wq + wp)∥ * ∥u - half•(wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = (2*∥u - half•(wq + wp)∥) * (2 * ∥u - half•(wq + wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by ring ... = (absR ((2:ℝ)) * ∥u - half•(wq + wp)∥) * (absR ((2:ℝ)) * ∥u - half•(wq+wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by { rw _root_.abs_of_nonneg, exact zero_le_two } ... = ∥(2:ℝ) • (u - half • (wq + wp))∥ * ∥(2:ℝ) • (u - half • (wq + wp))∥ + ∥wp-wq∥ * ∥wp-wq∥ : by simp [norm_smul] ... = ∥a + b∥ * ∥a + b∥ + ∥a - b∥ * ∥a - b∥ : begin rw [smul_sub, smul_smul, mul_one_div_cancel (_root_.two_ne_zero : (2 : ℝ) ≠ 0), ← one_add_one_eq_two, add_smul], simp only [one_smul], have eq₁ : wp - wq = a - b, from (sub_sub_sub_cancel_left _ _ _).symm, have eq₂ : u + u - (wq + wp) = a + b, show u + u - (wq + wp) = (u - wq) + (u - wp), abel, rw [eq₁, eq₂], end ... = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) : parallelogram_law_with_norm, have eq : δ ≤ ∥u - half • (wq + wp)∥, { rw smul_add, apply δ_le', apply h₂, repeat {exact subtype.mem _}, repeat {exact le_of_lt one_half_pos}, exact add_halves 1 }, have eq₁ : 4 * δ * δ ≤ 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥, { mono, mono, norm_num, apply mul_nonneg, norm_num, exact norm_nonneg _ }, have eq₂ : ∥a∥ * ∥a∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw q) (add_le_add_left (nat.one_div_le_one_div hq) _)), have eq₂' : ∥b∥ * ∥b∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw p) (add_le_add_left (nat.one_div_le_one_div hp) _)), rw dist_eq_norm, apply nonneg_le_nonneg_of_sq_le_sq, { exact sqrt_nonneg _ }, rw mul_self_sqrt, exact calc ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥*∥a∥ + ∥b∥*∥b∥) - 4 * ∥u - half • (wq+wp)∥ * ∥u - half • (wq+wp)∥ : by { rw ← this, simp } ... ≤ 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) - 4 * δ * δ : sub_le_sub_left eq₁ _ ... ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ : sub_le_sub_right (mul_le_mul_of_nonneg_left (add_le_add eq₂ eq₂') (by norm_num)) _ ... = 8 * δ * div + 4 * div * div : by ring, exact add_nonneg (mul_nonneg (mul_nonneg (by norm_num) zero_le_δ) (le_of_lt nat.one_div_pos_of_nat)) (mul_nonneg (mul_nonneg (by norm_num) nat.one_div_pos_of_nat.le) nat.one_div_pos_of_nat.le), -- third goal : `tendsto (λ (n : ℕ), sqrt (b n)) at_top (𝓝 0)` apply tendsto.comp, { convert continuous_sqrt.continuous_at, exact sqrt_zero.symm }, have eq₁ : tendsto (λ (n : ℕ), 8 * δ * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (8 * δ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (4:ℝ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have eq₂ : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1)) * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert this.mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, convert eq₁.add eq₂, simp only [add_zero] }, -- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`. -- Prove that it satisfies all requirements. rcases cauchy_seq_tendsto_of_is_complete h₁ (λ n, _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩, use v, use hv, have h_cont : continuous (λ v, ∥u - v∥) := continuous.comp continuous_norm (continuous.sub continuous_const continuous_id), have : tendsto (λ n, ∥u - w n∥) at_top (nhds ∥u - v∥), convert (tendsto.comp h_cont.continuous_at w_tendsto), exact tendsto_nhds_unique this norm_tendsto, exact subtype.mem _ end /-- Characterization of minimizers for the projection on a convex set in a real inner product space. -/ theorem norm_eq_infi_iff_real_inner_le_zero {K : set F} (h : convex K) {u : F} {v : F} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : K, ∥u - w∥) ↔ ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0 := iff.intro begin assume eq w hw, let δ := ⨅ w : K, ∥u - w∥, let p := ⟪u - v, w - v⟫_ℝ, let q := ∥w - v∥^2, letI : nonempty K := ⟨⟨v, hv⟩⟩, have zero_le_δ : 0 ≤ δ, apply le_cinfi, intro, exact norm_nonneg _, have δ_le : ∀ w : K, δ ≤ ∥u - w∥, assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, have : ∀θ:ℝ, 0 < θ → θ ≤ 1 → 2 * p ≤ θ * q, assume θ hθ₁ hθ₂, have : ∥u - v∥^2 ≤ ∥u - v∥^2 - 2 * θ * ⟪u - v, w - v⟫_ℝ + θ*θ*∥w - v∥^2 := calc ∥u - v∥^2 ≤ ∥u - (θ•w + (1-θ)•v)∥^2 : begin simp only [sq], apply mul_self_le_mul_self (norm_nonneg _), rw [eq], apply δ_le', apply h hw hv, exacts [le_of_lt hθ₁, sub_nonneg.2 hθ₂, add_sub_cancel'_right _ _], end ... = ∥(u - v) - θ • (w - v)∥^2 : begin have : u - (θ•w + (1-θ)•v) = (u - v) - θ • (w - v), { rw [smul_sub, sub_smul, one_smul], simp only [sub_eq_add_neg, add_comm, add_left_comm, add_assoc, neg_add_rev] }, rw this end ... = ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 : begin rw [norm_sub_sq, inner_smul_right, norm_smul], simp only [sq], show ∥u-v∥*∥u-v∥-2*(θ*inner(u-v)(w-v))+absR (θ)*∥w-v∥*(absR (θ)*∥w-v∥)= ∥u-v∥*∥u-v∥-2*θ*inner(u-v)(w-v)+θ*θ*(∥w-v∥*∥w-v∥), rw abs_of_pos hθ₁, ring end, have eq₁ : ∥u-v∥^2-2*θ*inner(u-v)(w-v)+θ*θ*∥w-v∥^2=∥u-v∥^2+(θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)), by abel, rw [eq₁, le_add_iff_nonneg_right] at this, have eq₂ : θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)=θ*(θ*∥w-v∥^2-2*inner(u-v)(w-v)), ring, rw eq₂ at this, have := le_of_sub_nonneg (nonneg_of_mul_nonneg_left this hθ₁), exact this, by_cases hq : q = 0, { rw hq at this, have : p ≤ 0, have := this (1:ℝ) (by norm_num) (by norm_num), linarith, exact this }, { have q_pos : 0 < q, apply lt_of_le_of_ne, exact sq_nonneg _, intro h, exact hq h.symm, by_contradiction hp, rw not_le at hp, let θ := min (1:ℝ) (p / q), have eq₁ : θ*q ≤ p := calc θ*q ≤ (p/q) * q : mul_le_mul_of_nonneg_right (min_le_right _ _) (sq_nonneg _) ... = p : div_mul_cancel _ hq, have : 2 * p ≤ p := calc 2 * p ≤ θ*q : by { refine this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num) } ... ≤ p : eq₁, linarith } end begin assume h, letI : nonempty K := ⟨⟨v, hv⟩⟩, apply le_antisymm, { apply le_cinfi, assume w, apply nonneg_le_nonneg_of_sq_le_sq (norm_nonneg _), have := h w w.2, exact calc ∥u - v∥ * ∥u - v∥ ≤ ∥u - v∥ * ∥u - v∥ - 2 * inner (u - v) ((w:F) - v) : by linarith ... ≤ ∥u - v∥^2 - 2 * inner (u - v) ((w:F) - v) + ∥(w:F) - v∥^2 : by { rw sq, refine le_add_of_nonneg_right _, exact sq_nonneg _ } ... = ∥(u - v) - (w - v)∥^2 : norm_sub_sq.symm ... = ∥u - w∥ * ∥u - w∥ : by { have : (u - v) - (w - v) = u - w, abel, rw [this, sq] } }, { show (⨅ (w : K), ∥u - w∥) ≤ (λw:K, ∥u - w∥) ⟨v, hv⟩, apply cinfi_le, use 0, rintros y ⟨z, rfl⟩, exact norm_nonneg _ } end variables (K : submodule 𝕜 E) /-- Existence of projections on complete subspaces. Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a (unique) `v` in `K` that minimizes the distance `∥u - v∥` to `u`. This point `v` is usually called the orthogonal projection of `u` onto `K`. -/ theorem exists_norm_eq_infi_of_complete_subspace (h : is_complete (↑K : set E)) : ∀ u : E, ∃ v ∈ K, ∥u - v∥ = ⨅ w : (K : set E), ∥u - w∥ := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : module ℝ E := restrict_scalars.module ℝ 𝕜 E, letI : is_scalar_tower ℝ 𝕜 E := restrict_scalars.is_scalar_tower _ _ _, let K' : submodule ℝ E := submodule.restrict_scalars ℝ K, exact exists_norm_eq_infi_of_complete_convex ⟨0, K'.zero_mem⟩ h K'.convex end /-- Characterization of minimizers in the projection on a subspace, in the real case. Let `u` be a point in a real inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `∥u - v∥` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`). This is superceded by `norm_eq_infi_iff_inner_eq_zero` that gives the same conclusion over any `is_R_or_C` field. -/ theorem norm_eq_infi_iff_real_inner_eq_zero (K : submodule ℝ F) {u : F} {v : F} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set F), ∥u - w∥) ↔ ∀ w ∈ K, ⟪u - v, w⟫_ℝ = 0 := iff.intro begin assume h, have h : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0, { rwa [norm_eq_infi_iff_real_inner_le_zero] at h, exacts [K.convex, hv] }, assume w hw, have le : ⟪u - v, w⟫_ℝ ≤ 0, let w' := w + v, have : w' ∈ K := submodule.add_mem _ hw hv, have h₁ := h w' this, have h₂ : w' - v = w, simp only [add_neg_cancel_right, sub_eq_add_neg], rw h₂ at h₁, exact h₁, have ge : ⟪u - v, w⟫_ℝ ≥ 0, let w'' := -w + v, have : w'' ∈ K := submodule.add_mem _ (submodule.neg_mem _ hw) hv, have h₁ := h w'' this, have h₂ : w'' - v = -w, simp only [neg_inj, add_neg_cancel_right, sub_eq_add_neg], rw [h₂, inner_neg_right] at h₁, linarith, exact le_antisymm le ge end begin assume h, have : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0, assume w hw, let w' := w - v, have : w' ∈ K := submodule.sub_mem _ hw hv, have h₁ := h w' this, exact le_of_eq h₁, rwa norm_eq_infi_iff_real_inner_le_zero, exacts [submodule.convex _, hv] end /-- Characterization of minimizers in the projection on a subspace. Let `u` be a point in an inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `∥u - v∥` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`) -/ theorem norm_eq_infi_iff_inner_eq_zero {u : E} {v : E} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set E), ∥u - w∥) ↔ ∀ w ∈ K, ⟪u - v, w⟫ = 0 := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : module ℝ E := restrict_scalars.module ℝ 𝕜 E, letI : is_scalar_tower ℝ 𝕜 E := restrict_scalars.is_scalar_tower _ _ _, let K' : submodule ℝ E := K.restrict_scalars ℝ, split, { assume H, have A : ∀ w ∈ K, re ⟪u - v, w⟫ = 0 := (norm_eq_infi_iff_real_inner_eq_zero K' hv).1 H, assume w hw, apply ext, { simp [A w hw] }, { symmetry, calc im (0 : 𝕜) = 0 : im.map_zero ... = re ⟪u - v, (-I) • w⟫ : (A _ (K.smul_mem (-I) hw)).symm ... = re ((-I) * ⟪u - v, w⟫) : by rw inner_smul_right ... = im ⟪u - v, w⟫ : by simp } }, { assume H, have : ∀ w ∈ K', ⟪u - v, w⟫_ℝ = 0, { assume w hw, rw [real_inner_eq_re_inner, H w hw], exact zero_re' }, exact (norm_eq_infi_iff_real_inner_eq_zero K' hv).2 this } end section orthogonal_projection variables [complete_space K] /-- The orthogonal projection onto a complete subspace, as an unbundled function. This definition is only intended for use in setting up the bundled version `orthogonal_projection` and should not be used once that is defined. -/ def orthogonal_projection_fn (v : E) := (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some variables {K} /-- The unbundled orthogonal projection is in the given subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_mem (v : E) : orthogonal_projection_fn K v ∈ K := (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some_spec.some /-- The characterization of the unbundled orthogonal projection. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - orthogonal_projection_fn K v, w⟫ = 0 := begin rw ←norm_eq_infi_iff_inner_eq_zero K (orthogonal_projection_fn_mem v), exact (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some_spec.some_spec end /-- The unbundled orthogonal projection is the unique point in `K` with the orthogonality property. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : orthogonal_projection_fn K u = v := begin rw [←sub_eq_zero, ←inner_self_eq_zero], have hvs : orthogonal_projection_fn K u - v ∈ K := submodule.sub_mem K (orthogonal_projection_fn_mem u) hvm, have huo : ⟪u - orthogonal_projection_fn K u, orthogonal_projection_fn K u - v⟫ = 0 := orthogonal_projection_fn_inner_eq_zero u _ hvs, have huv : ⟪u - v, orthogonal_projection_fn K u - v⟫ = 0 := hvo _ hvs, have houv : ⟪(u - v) - (u - orthogonal_projection_fn K u), orthogonal_projection_fn K u - v⟫ = 0, { rw [inner_sub_left, huo, huv, sub_zero] }, rwa sub_sub_sub_cancel_left at houv end variables (K) lemma orthogonal_projection_fn_norm_sq (v : E) : ∥v∥ * ∥v∥ = ∥v - (orthogonal_projection_fn K v)∥ * ∥v - (orthogonal_projection_fn K v)∥ + ∥orthogonal_projection_fn K v∥ * ∥orthogonal_projection_fn K v∥ := begin set p := orthogonal_projection_fn K v, have h' : ⟪v - p, p⟫ = 0, { exact orthogonal_projection_fn_inner_eq_zero _ _ (orthogonal_projection_fn_mem v) }, convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (v - p) p h' using 2; simp, end /-- The orthogonal projection onto a complete subspace. -/ def orthogonal_projection : E →L[𝕜] K := linear_map.mk_continuous { to_fun := λ v, ⟨orthogonal_projection_fn K v, orthogonal_projection_fn_mem v⟩, map_add' := λ x y, begin have hm : orthogonal_projection_fn K x + orthogonal_projection_fn K y ∈ K := submodule.add_mem K (orthogonal_projection_fn_mem x) (orthogonal_projection_fn_mem y), have ho : ∀ w ∈ K, ⟪x + y - (orthogonal_projection_fn K x + orthogonal_projection_fn K y), w⟫ = 0, { intros w hw, rw [add_sub_comm, inner_add_left, orthogonal_projection_fn_inner_eq_zero _ w hw, orthogonal_projection_fn_inner_eq_zero _ w hw, add_zero] }, ext, simp [eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hm ho] end, map_smul' := λ c x, begin have hm : c • orthogonal_projection_fn K x ∈ K := submodule.smul_mem K _ (orthogonal_projection_fn_mem x), have ho : ∀ w ∈ K, ⟪c • x - c • orthogonal_projection_fn K x, w⟫ = 0, { intros w hw, rw [←smul_sub, inner_smul_left, orthogonal_projection_fn_inner_eq_zero _ w hw, mul_zero] }, ext, simp [eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hm ho] end } 1 (λ x, begin simp only [one_mul, linear_map.coe_mk], refine le_of_pow_le_pow 2 (norm_nonneg _) (by norm_num) _, change ∥orthogonal_projection_fn K x∥ ^ 2 ≤ ∥x∥ ^ 2, nlinarith [orthogonal_projection_fn_norm_sq K x] end) variables {K} @[simp] lemma orthogonal_projection_fn_eq (v : E) : orthogonal_projection_fn K v = (orthogonal_projection K v : E) := rfl /-- The characterization of the orthogonal projection. -/ @[simp] lemma orthogonal_projection_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - orthogonal_projection K v, w⟫ = 0 := orthogonal_projection_fn_inner_eq_zero v /-- The orthogonal projection is the unique point in `K` with the orthogonality property. -/ lemma eq_orthogonal_projection_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hvm hvo /-- The orthogonal projections onto equal subspaces are coerced back to the same point in `E`. -/ lemma eq_orthogonal_projection_of_eq_submodule {K' : submodule 𝕜 E} [complete_space K'] (h : K = K') (u : E) : (orthogonal_projection K u : E) = (orthogonal_projection K' u : E) := begin change orthogonal_projection_fn K u = orthogonal_projection_fn K' u, congr, exact h end /-- The orthogonal projection sends elements of `K` to themselves. -/ @[simp] lemma orthogonal_projection_mem_subspace_eq_self (v : K) : orthogonal_projection K v = v := by { ext, apply eq_orthogonal_projection_of_mem_of_inner_eq_zero; simp } local attribute [instance] finite_dimensional_bot /-- The orthogonal projection onto the trivial submodule is the zero map. -/ @[simp] lemma orthogonal_projection_bot : orthogonal_projection (⊥ : submodule 𝕜 E) = 0 := by ext variables (K) /-- The orthogonal projection has norm `≤ 1`. -/ lemma orthogonal_projection_norm_le : ∥orthogonal_projection K∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (by norm_num) _ variables (𝕜) lemma smul_orthogonal_projection_singleton {v : E} (w : E) : (∥v∥ ^ 2 : 𝕜) • (orthogonal_projection (𝕜 ∙ v) w : E) = ⟪v, w⟫ • v := begin suffices : ↑(orthogonal_projection (𝕜 ∙ v) ((∥v∥ ^ 2 : 𝕜) • w)) = ⟪v, w⟫ • v, { simpa using this }, apply eq_orthogonal_projection_of_mem_of_inner_eq_zero, { rw submodule.mem_span_singleton, use ⟪v, w⟫ }, { intros x hx, obtain ⟨c, rfl⟩ := submodule.mem_span_singleton.mp hx, have hv : ↑∥v∥ ^ 2 = ⟪v, v⟫ := by { norm_cast, simp [norm_sq_eq_inner] }, simp [inner_sub_left, inner_smul_left, inner_smul_right, is_R_or_C.conj_div, mul_comm, hv, inner_product_space.conj_sym, hv] } end /-- Formula for orthogonal projection onto a single vector. -/ lemma orthogonal_projection_singleton {v : E} (w : E) : (orthogonal_projection (𝕜 ∙ v) w : E) = (⟪v, w⟫ / ∥v∥ ^ 2) • v := begin by_cases hv : v = 0, { rw [hv, eq_orthogonal_projection_of_eq_submodule submodule.span_zero_singleton], { simp }, { apply_instance } }, have hv' : ∥v∥ ≠ 0 := ne_of_gt (norm_pos_iff.mpr hv), have key : ((∥v∥ ^ 2 : 𝕜)⁻¹ * ∥v∥ ^ 2) • ↑(orthogonal_projection (𝕜 ∙ v) w) = ((∥v∥ ^ 2 : 𝕜)⁻¹ * ⟪v, w⟫) • v, { simp [mul_smul, smul_orthogonal_projection_singleton 𝕜 w] }, convert key; field_simp [hv'] end /-- Formula for orthogonal projection onto a single unit vector. -/ lemma orthogonal_projection_unit_singleton {v : E} (hv : ∥v∥ = 1) (w : E) : (orthogonal_projection (𝕜 ∙ v) w : E) = ⟪v, w⟫ • v := by { rw ← smul_orthogonal_projection_singleton 𝕜 w, simp [hv] } end orthogonal_projection /-- The subspace of vectors orthogonal to a given subspace. -/ def submodule.orthogonal : submodule 𝕜 E := { carrier := {v | ∀ u ∈ K, ⟪u, v⟫ = 0}, zero_mem' := λ _ _, inner_zero_right, add_mem' := λ x y hx hy u hu, by rw [inner_add_right, hx u hu, hy u hu, add_zero], smul_mem' := λ c x hx u hu, by rw [inner_smul_right, hx u hu, mul_zero] } notation K`ᗮ`:1200 := submodule.orthogonal K /-- When a vector is in `Kᗮ`. -/ lemma submodule.mem_orthogonal (v : E) : v ∈ Kᗮ ↔ ∀ u ∈ K, ⟪u, v⟫ = 0 := iff.rfl /-- When a vector is in `Kᗮ`, with the inner product the other way round. -/ lemma submodule.mem_orthogonal' (v : E) : v ∈ Kᗮ ↔ ∀ u ∈ K, ⟪v, u⟫ = 0 := by simp_rw [submodule.mem_orthogonal, inner_eq_zero_sym] variables {K} /-- A vector in `K` is orthogonal to one in `Kᗮ`. -/ lemma submodule.inner_right_of_mem_orthogonal {u v : E} (hu : u ∈ K) (hv : v ∈ Kᗮ) : ⟪u, v⟫ = 0 := (K.mem_orthogonal v).1 hv u hu /-- A vector in `Kᗮ` is orthogonal to one in `K`. -/ lemma submodule.inner_left_of_mem_orthogonal {u v : E} (hu : u ∈ K) (hv : v ∈ Kᗮ) : ⟪v, u⟫ = 0 := by rw [inner_eq_zero_sym]; exact submodule.inner_right_of_mem_orthogonal hu hv /-- A vector in `(𝕜 ∙ u)ᗮ` is orthogonal to `u`. -/ lemma inner_right_of_mem_orthogonal_singleton (u : E) {v : E} (hv : v ∈ (𝕜 ∙ u)ᗮ) : ⟪u, v⟫ = 0 := submodule.inner_right_of_mem_orthogonal (submodule.mem_span_singleton_self u) hv /-- A vector in `(𝕜 ∙ u)ᗮ` is orthogonal to `u`. -/ lemma inner_left_of_mem_orthogonal_singleton (u : E) {v : E} (hv : v ∈ (𝕜 ∙ u)ᗮ) : ⟪v, u⟫ = 0 := submodule.inner_left_of_mem_orthogonal (submodule.mem_span_singleton_self u) hv variables (K) /-- `K` and `Kᗮ` have trivial intersection. -/ lemma submodule.inf_orthogonal_eq_bot : K ⊓ Kᗮ = ⊥ := begin rw submodule.eq_bot_iff, intros x, rw submodule.mem_inf, exact λ ⟨hx, ho⟩, inner_self_eq_zero.1 (ho x hx) end /-- `K` and `Kᗮ` have trivial intersection. -/ lemma submodule.orthogonal_disjoint : disjoint K Kᗮ := by simp [disjoint_iff, K.inf_orthogonal_eq_bot] /-- `Kᗮ` can be characterized as the intersection of the kernels of the operations of inner product with each of the elements of `K`. -/ lemma orthogonal_eq_inter : Kᗮ = ⨅ v : K, (inner_right (v:E)).ker := begin apply le_antisymm, { rw le_infi_iff, rintros ⟨v, hv⟩ w hw, simpa using hw _ hv }, { intros v hv w hw, simp only [submodule.mem_infi] at hv, exact hv ⟨w, hw⟩ } end /-- The orthogonal complement of any submodule `K` is closed. -/ lemma submodule.is_closed_orthogonal : is_closed (Kᗮ : set E) := begin rw orthogonal_eq_inter K, convert is_closed_Inter (λ v : K, (inner_right (v:E)).is_closed_ker), simp end /-- In a complete space, the orthogonal complement of any submodule `K` is complete. -/ instance [complete_space E] : complete_space Kᗮ := K.is_closed_orthogonal.complete_space_coe variables (𝕜 E) /-- `submodule.orthogonal` gives a `galois_connection` between `submodule 𝕜 E` and its `order_dual`. -/ lemma submodule.orthogonal_gc : @galois_connection (submodule 𝕜 E) (order_dual $ submodule 𝕜 E) _ _ submodule.orthogonal submodule.orthogonal := λ K₁ K₂, ⟨λ h v hv u hu, submodule.inner_left_of_mem_orthogonal hv (h hu), λ h v hv u hu, submodule.inner_left_of_mem_orthogonal hv (h hu)⟩ variables {𝕜 E} /-- `submodule.orthogonal` reverses the `≤` ordering of two subspaces. -/ lemma submodule.orthogonal_le {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) : K₂ᗮ ≤ K₁ᗮ := (submodule.orthogonal_gc 𝕜 E).monotone_l h /-- `submodule.orthogonal.orthogonal` preserves the `≤` ordering of two subspaces. -/ lemma submodule.orthogonal_orthogonal_monotone {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) : K₁ᗮᗮ ≤ K₂ᗮᗮ := submodule.orthogonal_le (submodule.orthogonal_le h) /-- `K` is contained in `Kᗮᗮ`. -/ lemma submodule.le_orthogonal_orthogonal : K ≤ Kᗮᗮ := (submodule.orthogonal_gc 𝕜 E).le_u_l _ /-- The inf of two orthogonal subspaces equals the subspace orthogonal to the sup. -/ lemma submodule.inf_orthogonal (K₁ K₂ : submodule 𝕜 E) : K₁ᗮ ⊓ K₂ᗮ = (K₁ ⊔ K₂)ᗮ := (submodule.orthogonal_gc 𝕜 E).l_sup.symm /-- The inf of an indexed family of orthogonal subspaces equals the subspace orthogonal to the sup. -/ lemma submodule.infi_orthogonal {ι : Type*} (K : ι → submodule 𝕜 E) : (⨅ i, (K i)ᗮ) = (supr K)ᗮ := (submodule.orthogonal_gc 𝕜 E).l_supr.symm /-- The inf of a set of orthogonal subspaces equals the subspace orthogonal to the sup. -/ lemma submodule.Inf_orthogonal (s : set $ submodule 𝕜 E) : (⨅ K ∈ s, Kᗮ) = (Sup s)ᗮ := (submodule.orthogonal_gc 𝕜 E).l_Sup.symm /-- If `K₁` is complete and contained in `K₂`, `K₁` and `K₁ᗮ ⊓ K₂` span `K₂`. -/ lemma submodule.sup_orthogonal_inf_of_is_complete {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) (hc : is_complete (K₁ : set E)) : K₁ ⊔ (K₁ᗮ ⊓ K₂) = K₂ := begin ext x, rw submodule.mem_sup, rcases exists_norm_eq_infi_of_complete_subspace K₁ hc x with ⟨v, hv, hvm⟩, rw norm_eq_infi_iff_inner_eq_zero K₁ hv at hvm, split, { rintro ⟨y, hy, z, hz, rfl⟩, exact K₂.add_mem (h hy) hz.2 }, { exact λ hx, ⟨v, hv, x - v, ⟨(K₁.mem_orthogonal' _).2 hvm, K₂.sub_mem hx (h hv)⟩, add_sub_cancel'_right _ _⟩ } end variables {K} /-- If `K` is complete, `K` and `Kᗮ` span the whole space. -/ lemma submodule.sup_orthogonal_of_is_complete (h : is_complete (K : set E)) : K ⊔ Kᗮ = ⊤ := begin convert submodule.sup_orthogonal_inf_of_is_complete (le_top : K ≤ ⊤) h, simp end /-- If `K` is complete, `K` and `Kᗮ` span the whole space. Version using `complete_space`. -/ lemma submodule.sup_orthogonal_of_complete_space [complete_space K] : K ⊔ Kᗮ = ⊤ := submodule.sup_orthogonal_of_is_complete (complete_space_coe_iff_is_complete.mp ‹_›) variables (K) /-- If `K` is complete, any `v` in `E` can be expressed as a sum of elements of `K` and `Kᗮ`. -/ lemma submodule.exists_sum_mem_mem_orthogonal [complete_space K] (v : E) : ∃ (y ∈ K) (z ∈ Kᗮ), v = y + z := begin have h_mem : v ∈ K ⊔ Kᗮ := by simp [submodule.sup_orthogonal_of_complete_space], obtain ⟨y, hy, z, hz, hyz⟩ := submodule.mem_sup.mp h_mem, exact ⟨y, hy, z, hz, hyz.symm⟩ end /-- If `K` is complete, then the orthogonal complement of its orthogonal complement is itself. -/ @[simp] lemma submodule.orthogonal_orthogonal [complete_space K] : Kᗮᗮ = K := begin ext v, split, { obtain ⟨y, hy, z, hz, rfl⟩ := K.exists_sum_mem_mem_orthogonal v, intros hv, have hz' : z = 0, { have hyz : ⟪z, y⟫ = 0 := by simp [hz y hy, inner_eq_zero_sym], simpa [inner_add_right, hyz] using hv z hz }, simp [hy, hz'] }, { intros hv w hw, rw inner_eq_zero_sym, exact hw v hv } end lemma submodule.orthogonal_orthogonal_eq_closure [complete_space E] : Kᗮᗮ = K.topological_closure := begin refine le_antisymm _ _, { convert submodule.orthogonal_orthogonal_monotone K.submodule_topological_closure, haveI : complete_space K.topological_closure := K.is_closed_topological_closure.complete_space_coe, rw K.topological_closure.orthogonal_orthogonal }, { exact K.topological_closure_minimal K.le_orthogonal_orthogonal Kᗮ.is_closed_orthogonal } end variables {K} /-- If `K` is complete, `K` and `Kᗮ` are complements of each other. -/ lemma submodule.is_compl_orthogonal_of_is_complete (h : is_complete (K : set E)) : is_compl K Kᗮ := ⟨K.orthogonal_disjoint, le_of_eq (submodule.sup_orthogonal_of_is_complete h).symm⟩ @[simp] lemma submodule.top_orthogonal_eq_bot : (⊤ : submodule 𝕜 E)ᗮ = ⊥ := begin ext, rw [submodule.mem_bot, submodule.mem_orthogonal], exact ⟨λ h, inner_self_eq_zero.mp (h x submodule.mem_top), by { rintro rfl, simp }⟩ end @[simp] lemma submodule.bot_orthogonal_eq_top : (⊥ : submodule 𝕜 E)ᗮ = ⊤ := begin rw [← submodule.top_orthogonal_eq_bot, eq_top_iff], exact submodule.le_orthogonal_orthogonal ⊤ end @[simp] lemma submodule.orthogonal_eq_bot_iff (hK : is_complete (K : set E)) : Kᗮ = ⊥ ↔ K = ⊤ := begin refine ⟨_, by { rintro rfl, exact submodule.top_orthogonal_eq_bot }⟩, intro h, have : K ⊔ Kᗮ = ⊤ := submodule.sup_orthogonal_of_is_complete hK, rwa [h, sup_comm, bot_sup_eq] at this, end @[simp] lemma submodule.orthogonal_eq_top_iff : Kᗮ = ⊤ ↔ K = ⊥ := begin refine ⟨_, by { rintro rfl, exact submodule.bot_orthogonal_eq_top }⟩, intro h, have : K ⊓ Kᗮ = ⊥ := K.orthogonal_disjoint.eq_bot, rwa [h, inf_comm, top_inf_eq] at this end /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ lemma eq_orthogonal_projection_of_mem_orthogonal [complete_space K] {u v : E} (hv : v ∈ K) (hvo : u - v ∈ Kᗮ) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hv (λ w, inner_eq_zero_sym.mp ∘ (hvo w)) /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ lemma eq_orthogonal_projection_of_mem_orthogonal' [complete_space K] {u v z : E} (hv : v ∈ K) (hz : z ∈ Kᗮ) (hu : u = v + z) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_of_mem_orthogonal hv (by simpa [hu]) /-- The orthogonal projection onto `K` of an element of `Kᗮ` is zero. -/ lemma orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero [complete_space K] {v : E} (hv : v ∈ Kᗮ) : orthogonal_projection K v = 0 := by { ext, convert eq_orthogonal_projection_of_mem_orthogonal _ _; simp [hv] } /-- The orthogonal projection onto `Kᗮ` of an element of `K` is zero. -/ lemma orthogonal_projection_mem_subspace_orthogonal_precomplement_eq_zero [complete_space E] {v : E} (hv : v ∈ K) : orthogonal_projection Kᗮ v = 0 := orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero (K.le_orthogonal_orthogonal hv) /-- The orthogonal projection onto `(𝕜 ∙ v)ᗮ` of `v` is zero. -/ lemma orthogonal_projection_orthogonal_complement_singleton_eq_zero [complete_space E] (v : E) : orthogonal_projection (𝕜 ∙ v)ᗮ v = 0 := orthogonal_projection_mem_subspace_orthogonal_precomplement_eq_zero (submodule.mem_span_singleton_self v) variables (K) /-- In a complete space `E`, a vector splits as the sum of its orthogonal projections onto a complete submodule `K` and onto the orthogonal complement of `K`.-/ lemma eq_sum_orthogonal_projection_self_orthogonal_complement [complete_space E] [complete_space K] (w : E) : w = (orthogonal_projection K w : E) + (orthogonal_projection Kᗮ w : E) := begin obtain ⟨y, hy, z, hz, hwyz⟩ := K.exists_sum_mem_mem_orthogonal w, convert hwyz, { exact eq_orthogonal_projection_of_mem_orthogonal' hy hz hwyz }, { rw add_comm at hwyz, refine eq_orthogonal_projection_of_mem_orthogonal' hz _ hwyz, simp [hy] } end /-- In a complete space `E`, the projection maps onto a complete subspace `K` and its orthogonal complement sum to the identity. -/ lemma id_eq_sum_orthogonal_projection_self_orthogonal_complement [complete_space E] [complete_space K] : continuous_linear_map.id 𝕜 E = K.subtypeL.comp (orthogonal_projection K) + Kᗮ.subtypeL.comp (orthogonal_projection Kᗮ) := by { ext w, exact eq_sum_orthogonal_projection_self_orthogonal_complement K w } /-- The orthogonal projection is self-adjoint. -/ lemma inner_orthogonal_projection_left_eq_right [complete_space E] [complete_space K] (u v : E) : ⟪↑(orthogonal_projection K u), v⟫ = ⟪u, orthogonal_projection K v⟫ := begin nth_rewrite 0 eq_sum_orthogonal_projection_self_orthogonal_complement K v, nth_rewrite 1 eq_sum_orthogonal_projection_self_orthogonal_complement K u, rw [inner_add_left, inner_add_right, submodule.inner_right_of_mem_orthogonal (submodule.coe_mem (orthogonal_projection K u)) (submodule.coe_mem (orthogonal_projection Kᗮ v)), submodule.inner_left_of_mem_orthogonal (submodule.coe_mem (orthogonal_projection K v)) (submodule.coe_mem (orthogonal_projection Kᗮ u))], end open finite_dimensional /-- Given a finite-dimensional subspace `K₂`, and a subspace `K₁` containined in it, the dimensions of `K₁` and the intersection of its orthogonal subspace with `K₂` add to that of `K₂`. -/ lemma submodule.finrank_add_inf_finrank_orthogonal {K₁ K₂ : submodule 𝕜 E} [finite_dimensional 𝕜 K₂] (h : K₁ ≤ K₂) : finrank 𝕜 K₁ + finrank 𝕜 (K₁ᗮ ⊓ K₂ : submodule 𝕜 E) = finrank 𝕜 K₂ := begin haveI := submodule.finite_dimensional_of_le h, have hd := submodule.dim_sup_add_dim_inf_eq K₁ (K₁ᗮ ⊓ K₂), rw [←inf_assoc, (submodule.orthogonal_disjoint K₁).eq_bot, bot_inf_eq, finrank_bot, submodule.sup_orthogonal_inf_of_is_complete h (submodule.complete_of_finite_dimensional _)] at hd, rw add_zero at hd, exact hd.symm end /-- Given a finite-dimensional subspace `K₂`, and a subspace `K₁` containined in it, the dimensions of `K₁` and the intersection of its orthogonal subspace with `K₂` add to that of `K₂`. -/ lemma submodule.finrank_add_inf_finrank_orthogonal' {K₁ K₂ : submodule 𝕜 E} [finite_dimensional 𝕜 K₂] (h : K₁ ≤ K₂) {n : ℕ} (h_dim : finrank 𝕜 K₁ + n = finrank 𝕜 K₂) : finrank 𝕜 (K₁ᗮ ⊓ K₂ : submodule 𝕜 E) = n := by { rw ← add_right_inj (finrank 𝕜 K₁), simp [submodule.finrank_add_inf_finrank_orthogonal h, h_dim] } /-- Given a finite-dimensional space `E` and subspace `K`, the dimensions of `K` and `Kᗮ` add to that of `E`. -/ lemma submodule.finrank_add_finrank_orthogonal [finite_dimensional 𝕜 E] {K : submodule 𝕜 E} : finrank 𝕜 K + finrank 𝕜 Kᗮ = finrank 𝕜 E := begin convert submodule.finrank_add_inf_finrank_orthogonal (le_top : K ≤ ⊤) using 1, { rw inf_top_eq }, { simp } end /-- Given a finite-dimensional space `E` and subspace `K`, the dimensions of `K` and `Kᗮ` add to that of `E`. -/ lemma submodule.finrank_add_finrank_orthogonal' [finite_dimensional 𝕜 E] {K : submodule 𝕜 E} {n : ℕ} (h_dim : finrank 𝕜 K + n = finrank 𝕜 E) : finrank 𝕜 Kᗮ = n := by { rw ← add_right_inj (finrank 𝕜 K), simp [submodule.finrank_add_finrank_orthogonal, h_dim] } local attribute [instance] finite_dimensional_of_finrank_eq_succ /-- In a finite-dimensional inner product space, the dimension of the orthogonal complement of the span of a nonzero vector is one less than the dimension of the space. -/ lemma finrank_orthogonal_span_singleton {n : ℕ} [_i : fact (finrank 𝕜 E = n + 1)] {v : E} (hv : v ≠ 0) : finrank 𝕜 (𝕜 ∙ v)ᗮ = n := submodule.finrank_add_finrank_orthogonal' $ by simp [finrank_span_singleton hv, _i.elim, add_comm] end orthogonal section orthonormal_basis /-! ### Existence of Hilbert basis, orthonormal basis, etc. -/ variables {𝕜 E} {v : set E} open finite_dimensional submodule set /-- An orthonormal set in an `inner_product_space` is maximal, if and only if the orthogonal complement of its span is empty. -/ lemma maximal_orthonormal_iff_orthogonal_complement_eq_bot (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ (span 𝕜 v)ᗮ = ⊥ := begin rw submodule.eq_bot_iff, split, { contrapose!, -- ** direction 1: nonempty orthogonal complement implies nonmaximal rintros ⟨x, hx', hx⟩, -- take a nonzero vector and normalize it let e := (∥x∥⁻¹ : 𝕜) • x, have he : ∥e∥ = 1 := by simp [e, norm_smul_inv_norm hx], have he' : e ∈ (span 𝕜 v)ᗮ := smul_mem' _ _ hx', have he'' : e ∉ v, { intros hev, have : e = 0, { have : e ∈ (span 𝕜 v) ⊓ (span 𝕜 v)ᗮ := ⟨subset_span hev, he'⟩, simpa [(span 𝕜 v).inf_orthogonal_eq_bot] using this }, have : e ≠ 0 := hv.ne_zero ⟨e, hev⟩, contradiction }, -- put this together with `v` to provide a candidate orthonormal basis for the whole space refine ⟨v.insert e, v.subset_insert e, ⟨_, _⟩, (v.ne_insert_of_not_mem he'').symm⟩, { -- show that the elements of `v.insert e` have unit length rintros ⟨a, ha'⟩, cases eq_or_mem_of_mem_insert ha' with ha ha, { simp [ha, he] }, { exact hv.1 ⟨a, ha⟩ } }, { -- show that the elements of `v.insert e` are orthogonal have h_end : ∀ a ∈ v, ⟪a, e⟫ = 0, { intros a ha, exact he' a (submodule.subset_span ha) }, rintros ⟨a, ha'⟩, cases eq_or_mem_of_mem_insert ha' with ha ha, { rintros ⟨b, hb'⟩ hab', have hb : b ∈ v, { refine mem_of_mem_insert_of_ne hb' _, intros hbe', apply hab', simp [ha, hbe'] }, rw inner_eq_zero_sym, simpa [ha] using h_end b hb }, rintros ⟨b, hb'⟩ hab', cases eq_or_mem_of_mem_insert hb' with hb hb, { simpa [hb] using h_end a ha }, have : (⟨a, ha⟩ : v) ≠ ⟨b, hb⟩, { intros hab'', apply hab', simpa using hab'' }, exact hv.2 this } }, { -- ** direction 2: empty orthogonal complement implies maximal simp only [subset.antisymm_iff], rintros h u (huv : v ⊆ u) hu, refine ⟨_, huv⟩, intros x hxu, refine ((mt (h x)) (hu.ne_zero ⟨x, hxu⟩)).imp_symm _, intros hxv y hy, have hxv' : (⟨x, hxu⟩ : u) ∉ (coe ⁻¹' v : set u) := by simp [huv, hxv], obtain ⟨l, hl, rfl⟩ : ∃ l ∈ finsupp.supported 𝕜 𝕜 (coe ⁻¹' v : set u), (finsupp.total ↥u E 𝕜 coe) l = y, { rw ← finsupp.mem_span_image_iff_total, simp [huv, inter_eq_self_of_subset_left, hy] }, exact hu.inner_finsupp_eq_zero hxv' hl } end /-- An orthonormal set in an `inner_product_space` is maximal, if and only if the closure of its span is the whole space. -/ lemma maximal_orthonormal_iff_dense_span [complete_space E] (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ (span 𝕜 v).topological_closure = ⊤ := by rw [maximal_orthonormal_iff_orthogonal_complement_eq_bot hv, ← submodule.orthogonal_eq_top_iff, (span 𝕜 v).orthogonal_orthogonal_eq_closure] /-- Any orthonormal subset can be extended to an orthonormal set whose span is dense. -/ lemma exists_subset_is_orthonormal_dense_span [complete_space E] (hv : orthonormal 𝕜 (coe : v → E)) : ∃ u ⊇ v, orthonormal 𝕜 (coe : u → E) ∧ (span 𝕜 u).topological_closure = ⊤ := begin obtain ⟨u, hus, hu, hu_max⟩ := exists_maximal_orthonormal hv, rw maximal_orthonormal_iff_dense_span hu at hu_max, exact ⟨u, hus, hu, hu_max⟩ end variables (𝕜 E) /-- An inner product space admits an orthonormal set whose span is dense. -/ lemma exists_is_orthonormal_dense_span [complete_space E] : ∃ u : set E, orthonormal 𝕜 (coe : u → E) ∧ (span 𝕜 u).topological_closure = ⊤ := let ⟨u, hus, hu, hu_max⟩ := exists_subset_is_orthonormal_dense_span (orthonormal_empty 𝕜 E) in ⟨u, hu, hu_max⟩ variables {𝕜 E} /-- An orthonormal set in a finite-dimensional `inner_product_space` is maximal, if and only if it is a basis. -/ lemma maximal_orthonormal_iff_basis_of_finite_dimensional [finite_dimensional 𝕜 E] (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ ∃ b : basis v 𝕜 E, ⇑b = coe := begin rw maximal_orthonormal_iff_orthogonal_complement_eq_bot hv, have hv_compl : is_complete (span 𝕜 v : set E) := (span 𝕜 v).complete_of_finite_dimensional, rw submodule.orthogonal_eq_bot_iff hv_compl, have hv_coe : range (coe : v → E) = v := by simp, split, { refine λ h, ⟨basis.mk hv.linear_independent _, basis.coe_mk _ _⟩, convert h }, { rintros ⟨h, coe_h⟩, rw [← h.span_eq, coe_h, hv_coe] } end /-- In a finite-dimensional `inner_product_space`, any orthonormal subset can be extended to an orthonormal basis. -/ lemma exists_subset_is_orthonormal_basis [finite_dimensional 𝕜 E] (hv : orthonormal 𝕜 (coe : v → E)) : ∃ (u ⊇ v) (b : basis u 𝕜 E), orthonormal 𝕜 b ∧ ⇑b = coe := begin obtain ⟨u, hus, hu, hu_max⟩ := exists_maximal_orthonormal hv, obtain ⟨b, hb⟩ := (maximal_orthonormal_iff_basis_of_finite_dimensional hu).mp hu_max, exact ⟨u, hus, b, by rwa hb, hb⟩ end variables (𝕜 E) /-- Index for an arbitrary orthonormal basis on a finite-dimensional `inner_product_space`. -/ def orthonormal_basis_index [finite_dimensional 𝕜 E] : set E := classical.some (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)) /-- A finite-dimensional `inner_product_space` has an orthonormal basis. -/ def orthonormal_basis [finite_dimensional 𝕜 E] : basis (orthonormal_basis_index 𝕜 E) 𝕜 E := (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)).some_spec.some_spec.some lemma orthonormal_basis_orthonormal [finite_dimensional 𝕜 E] : orthonormal 𝕜 (orthonormal_basis 𝕜 E) := (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)).some_spec.some_spec.some_spec.1 @[simp] lemma coe_orthonormal_basis [finite_dimensional 𝕜 E] : ⇑(orthonormal_basis 𝕜 E) = coe := (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)).some_spec.some_spec.some_spec.2 instance [finite_dimensional 𝕜 E] : fintype (orthonormal_basis_index 𝕜 E) := is_noetherian.fintype_basis_index (orthonormal_basis 𝕜 E) variables {𝕜 E} /-- An `n`-dimensional `inner_product_space` has an orthonormal basis indexed by `fin n`. -/ def fin_orthonormal_basis [finite_dimensional 𝕜 E] {n : ℕ} (hn : finrank 𝕜 E = n) : basis (fin n) 𝕜 E := have h : fintype.card (orthonormal_basis_index 𝕜 E) = n, by rw [← finrank_eq_card_basis (orthonormal_basis 𝕜 E), hn], (orthonormal_basis 𝕜 E).reindex (fintype.equiv_fin_of_card_eq h) lemma fin_orthonormal_basis_orthonormal [finite_dimensional 𝕜 E] {n : ℕ} (hn : finrank 𝕜 E = n) : orthonormal 𝕜 (fin_orthonormal_basis hn) := suffices orthonormal 𝕜 (orthonormal_basis _ _ ∘ equiv.symm _), by { simp only [fin_orthonormal_basis, basis.coe_reindex], assumption }, -- why doesn't simpa work? (orthonormal_basis_orthonormal 𝕜 E).comp _ (equiv.injective _) end orthonormal_basis
8c521c419817cfb246e317977e6f672a77dda88c
9d2e3d5a2e2342a283affd97eead310c3b528a24
/src/hints/thursday/afternoon/category_theory/exercise6/hint1.lean
deaea5287e9312bd9f941fcef426b9dd2e6955f4
[]
permissive
Vtec234/lftcm2020
ad2610ab614beefe44acc5622bb4a7fff9a5ea46
bbbd4c8162f8c2ef602300ab8fdeca231886375d
refs/heads/master
1,668,808,098,623
1,594,989,081,000
1,594,990,079,000
280,423,039
0
0
MIT
1,594,990,209,000
1,594,990,209,000
null
UTF-8
Lean
false
false
903
lean
import category_theory.limits.shapes.pullbacks /-! Thanks to Markus Himmel for suggesting this question. -/ open category_theory open category_theory.limits /-! Let C be a category, X and Y be objects and f : X ⟶ Y be a morphism. Show that f is an epimorphism if and only if the diagram X --f--→ Y | | f 𝟙 | | ↓ ↓ Y --𝟙--→ Y is a pushout. -/ universes v u variables {C : Type u} [category.{v} C] def pushout_of_epi {X Y : C} (f : X ⟶ Y) [epi f] : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) := begin fapply pushout_cocone.is_colimit.mk, all_goals { sorry, }, end theorem epi_of_pushout {X Y : C} (f : X ⟶ Y) (is_colim : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f := { left_cancellation := λ Z g h hf, begin let a := pushout_cocone.mk _ _ hf, sorry, end }
3b4e0b1e5895137c0e642e933cb56a58f10a4f42
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Meta/ForEachExpr.lean
743436e8f282bd55dae4c20f72859f7bfd72cc25
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,245
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.Expr import Lean.Util.MonadCache import Lean.Meta.Basic namespace Lean.Meta namespace ForEachExpr abbrev M := MonadCacheT Expr Unit MetaM mutual private partial def visitBinder (fn : Expr → MetaM Bool) : Array Expr → Nat → Expr → M Unit | fvars, j, Expr.lam n d b c => do let d := d.instantiateRevRange j fvars.size fvars; visit fn d; withLocalDecl n c.binderInfo d fun x => visitBinder fn (fvars.push x) j b | fvars, j, Expr.forallE n d b c => do let d := d.instantiateRevRange j fvars.size fvars; visit fn d; withLocalDecl n c.binderInfo d fun x => visitBinder fn (fvars.push x) j b | fvars, j, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars; visit fn t; let v := v.instantiateRevRange j fvars.size fvars; visit fn v; withLetDecl n t v fun x => visitBinder fn (fvars.push x) j b | fvars, j, e => visit fn $ e.instantiateRevRange j fvars.size fvars partial def visit (fn : Expr → MetaM Bool) (e : Expr) : M Unit := checkCache e fun e => do if (← liftM (fn e)) then match e with | Expr.forallE _ _ _ _ => visitBinder fn #[] 0 e | Expr.lam _ _ _ _ => visitBinder fn #[] 0 e | Expr.letE _ _ _ _ _ => visitBinder fn #[] 0 e | Expr.app f a _ => do visit fn f; visit fn a | Expr.mdata _ b _ => visit fn b | Expr.proj _ _ b _ => visit fn b | _ => pure () end end ForEachExpr def forEachExprImp' (e : Expr) (f : Expr → MetaM Bool) : MetaM Unit := ForEachExpr.visit f e |>.run /-- Similar to `Expr.forEach'`, but creates free variables whenever going inside of a binder. -/ def forEachExpr' {m} [MonadLiftT MetaM m] (e : Expr) (f : Expr → MetaM Bool) : m Unit := liftM $ forEachExprImp' e f /-- Similar to `Expr.forEach`, but creates free variables whenever going inside of a binder. -/ def forEachExpr {m} [MonadLiftT MetaM m] (e : Expr) (f : Expr → MetaM Unit) : m Unit := forEachExpr' e fun e => do f e pure true end Lean.Meta
e1394079acf2cb2c73250005d9a60096ef28357f
a339bc2ac96174381fb610f4b2e1ba42df2be819
/hott/algebra/homotopy_group.hlean
2347493bf9888a7a49bdbcbae0c020c61998234b
[ "Apache-2.0" ]
permissive
kalfsvag/lean2
25b2dccc07a98e5aa20f9a11229831f9d3edf2e7
4d4a0c7c53a9922c5f630f6f8ebdccf7ddef2cc7
refs/heads/master
1,610,513,122,164
1,483,135,198,000
1,483,135,198,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,545
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn homotopy groups of a pointed space -/ import .trunc_group types.trunc .group_theory types.nat.hott open nat eq pointed trunc is_trunc algebra group function equiv unit is_equiv nat -- TODO: consistently make n an argument before A namespace eq definition homotopy_group [reducible] [constructor] (n : ℕ) (A : Type*) : Set* := ptrunc 0 (Ω[n] A) notation `π[`:95 n:0 `]`:0 := homotopy_group n definition group_homotopy_group [instance] [constructor] [reducible] (n : ℕ) (A : Type*) : group (π[succ n] A) := trunc_group concat inverse idp con.assoc idp_con con_idp con.left_inv definition group_homotopy_group2 [instance] (k : ℕ) (A : Type*) : group (carrier (ptrunctype.to_pType (π[k + 1] A))) := group_homotopy_group k A definition ab_group_homotopy_group [constructor] [reducible] (n : ℕ) (A : Type*) : ab_group (π[succ (succ n)] A) := trunc_ab_group concat inverse idp con.assoc idp_con con_idp con.left_inv eckmann_hilton local attribute ab_group_homotopy_group [instance] definition ghomotopy_group [constructor] : Π(n : ℕ) [is_succ n] (A : Type*), Group | (succ n) x A := Group.mk (π[succ n] A) _ definition cghomotopy_group [constructor] : Π(n : ℕ) [is_at_least_two n] (A : Type*), AbGroup | (succ (succ n)) x A := AbGroup.mk (π[succ (succ n)] A) _ definition fundamental_group [constructor] (A : Type*) : Group := ghomotopy_group 1 A notation `πg[`:95 n:0 `]`:0 := ghomotopy_group n notation `πag[`:95 n:0 `]`:0 := cghomotopy_group n notation `π₁` := fundamental_group -- should this be notation for the group or pointed type? definition tr_mul_tr {n : ℕ} {A : Type*} (p q : Ω[n + 1] A) : tr p *[πg[n+1] A] tr q = tr (p ⬝ q) := by reflexivity definition tr_mul_tr' {n : ℕ} {A : Type*} (p q : Ω[succ n] A) : tr p *[π[succ n] A] tr q = tr (p ⬝ q) := idp definition homotopy_group_pequiv [constructor] (n : ℕ) {A B : Type*} (H : A ≃* B) : π[n] A ≃* π[n] B := ptrunc_pequiv_ptrunc 0 (loopn_pequiv_loopn n H) definition homotopy_group_pequiv_loop_ptrunc [constructor] (k : ℕ) (A : Type*) : π[k] A ≃* Ω[k] (ptrunc k A) := begin refine !loopn_ptrunc_pequiv⁻¹ᵉ* ⬝e* _, exact loopn_pequiv_loopn k (pequiv_of_eq begin rewrite [trunc_index.zero_add] end) end open trunc_index definition homotopy_group_ptrunc_of_le [constructor] {k n : ℕ} (H : k ≤ n) (A : Type*) : π[k] (ptrunc n A) ≃* π[k] A := calc π[k] (ptrunc n A) ≃* Ω[k] (ptrunc k (ptrunc n A)) : homotopy_group_pequiv_loop_ptrunc k (ptrunc n A) ... ≃* Ω[k] (ptrunc k A) : loopn_pequiv_loopn k (ptrunc_ptrunc_pequiv_left A (of_nat_le_of_nat H)) ... ≃* π[k] A : (homotopy_group_pequiv_loop_ptrunc k A)⁻¹ᵉ* definition homotopy_group_ptrunc [constructor] (k : ℕ) (A : Type*) : π[k] (ptrunc k A) ≃* π[k] A := homotopy_group_ptrunc_of_le (le.refl k) A theorem trivial_homotopy_of_is_set (A : Type*) [H : is_set A] (n : ℕ) : πg[n+1] A ≃g G0 := begin apply trivial_group_of_is_contr, apply is_trunc_trunc_of_is_trunc, apply is_contr_loop_of_is_trunc, apply is_trunc_succ_succ_of_is_set end definition homotopy_group_succ_out (A : Type*) (n : ℕ) : π[n + 1] A = π₁ (Ω[n] A) := idp definition homotopy_group_succ_in (A : Type*) (n : ℕ) : π[n + 1] A ≃* π[n] (Ω A) := ptrunc_pequiv_ptrunc 0 (loopn_succ_in A n) definition ghomotopy_group_succ_out (A : Type*) (n : ℕ) : πg[n + 1] A = π₁ (Ω[n] A) := idp definition homotopy_group_succ_in_con {A : Type*} {n : ℕ} (g h : πg[n + 2] A) : homotopy_group_succ_in A (succ n) (g * h) = homotopy_group_succ_in A (succ n) g * homotopy_group_succ_in A (succ n) h := begin induction g with p, induction h with q, esimp, apply ap tr, apply loopn_succ_in_con end definition ghomotopy_group_succ_in [constructor] (A : Type*) (n : ℕ) : πg[n + 2] A ≃g πg[n + 1] (Ω A) := begin fapply isomorphism_of_equiv, { exact homotopy_group_succ_in A (succ n)}, { exact homotopy_group_succ_in_con}, end definition homotopy_group_functor [constructor] (n : ℕ) {A B : Type*} (f : A →* B) : π[n] A →* π[n] B := ptrunc_functor 0 (apn n f) notation `π→[`:95 n:0 `]`:0 := homotopy_group_functor n definition homotopy_group_functor_phomotopy [constructor] (n : ℕ) {A B : Type*} {f g : A →* B} (p : f ~* g) : π→[n] f ~* π→[n] g := ptrunc_functor_phomotopy 0 (apn_phomotopy n p) definition homotopy_group_functor_pid (n : ℕ) (A : Type*) : π→[n] (pid A) ~* pid (π[n] A) := ptrunc_functor_phomotopy 0 !apn_pid ⬝* !ptrunc_functor_pid definition homotopy_group_functor_compose [constructor] (n : ℕ) {A B C : Type*} (g : B →* C) (f : A →* B) : π→[n] (g ∘* f) ~* π→[n] g ∘* π→[n] f := ptrunc_functor_phomotopy 0 !apn_pcompose ⬝* !ptrunc_functor_pcompose definition is_equiv_homotopy_group_functor [constructor] (n : ℕ) {A B : Type*} (f : A →* B) [is_equiv f] : is_equiv (π→[n] f) := @(is_equiv_trunc_functor 0 _) !is_equiv_apn definition homotopy_group_functor_succ_phomotopy_in (n : ℕ) {A B : Type*} (f : A →* B) : homotopy_group_succ_in B n ∘* π→[n + 1] f ~* π→[n] (Ω→ f) ∘* homotopy_group_succ_in A n := begin refine !ptrunc_functor_pcompose⁻¹* ⬝* _ ⬝* !ptrunc_functor_pcompose, exact ptrunc_functor_phomotopy 0 (apn_succ_phomotopy_in n f) end definition is_equiv_homotopy_group_functor_ap1 (n : ℕ) {A B : Type*} (f : A →* B) [is_equiv (π→[n + 1] f)] : is_equiv (π→[n] (Ω→ f)) := have is_equiv (homotopy_group_succ_in B n ∘* π→[n + 1] f), from is_equiv_compose _ (π→[n + 1] f), have is_equiv (π→[n] (Ω→ f) ∘ homotopy_group_succ_in A n), from is_equiv.homotopy_closed _ (homotopy_group_functor_succ_phomotopy_in n f), is_equiv.cancel_right (homotopy_group_succ_in A n) _ definition tinverse [constructor] {X : Type*} : π[1] X →* π[1] X := ptrunc_functor 0 pinverse definition is_equiv_tinverse [constructor] (A : Type*) : is_equiv (@tinverse A) := by apply @is_equiv_trunc_functor; apply is_equiv_eq_inverse definition ptrunc_functor_pinverse [constructor] {X : Type*} : ptrunc_functor 0 (@pinverse X) ~* @tinverse X := begin fapply phomotopy.mk, { reflexivity}, { reflexivity} end definition homotopy_group_functor_mul [constructor] (n : ℕ) {A B : Type*} (g : A →* B) (p q : πg[n+1] A) : (π→[n + 1] g) (p *[πg[n+1] A] q) = (π→[n+1] g) p *[πg[n+1] B] (π→[n + 1] g) q := begin unfold [ghomotopy_group, homotopy_group] at *, refine @trunc.rec _ _ _ (λq, !is_trunc_eq) _ p, clear p, intro p, refine @trunc.rec _ _ _ (λq, !is_trunc_eq) _ q, clear q, intro q, apply ap tr, apply apn_con end definition homotopy_group_homomorphism [constructor] (n : ℕ) [H : is_succ n] {A B : Type*} (f : A →* B) : πg[n] A →g πg[n] B := begin induction H with n, fconstructor, { exact homotopy_group_functor (n+1) f}, { apply homotopy_group_functor_mul} end notation `π→g[`:95 n:0 `]`:0 := homotopy_group_homomorphism n definition homotopy_group_isomorphism_of_pequiv [constructor] (n : ℕ) {A B : Type*} (f : A ≃* B) : πg[n+1] A ≃g πg[n+1] B := begin apply isomorphism.mk (homotopy_group_homomorphism (succ n) f), esimp, apply is_equiv_trunc_functor, apply is_equiv_apn, end definition homotopy_group_add (A : Type*) (n m : ℕ) : πg[n+m+1] A ≃g πg[n+1] (Ω[m] A) := begin revert A, induction m with m IH: intro A, { reflexivity}, { esimp [loopn, nat.add], refine !ghomotopy_group_succ_in ⬝g _, refine !IH ⬝g _, apply homotopy_group_isomorphism_of_pequiv, exact !loopn_succ_in⁻¹ᵉ*} end theorem trivial_homotopy_add_of_is_set_loopn {A : Type*} {n : ℕ} (m : ℕ) (H : is_set (Ω[n] A)) : πg[m+n+1] A ≃g G0 := !homotopy_group_add ⬝g !trivial_homotopy_of_is_set theorem trivial_homotopy_le_of_is_set_loopn {A : Type*} {n : ℕ} (m : ℕ) (H1 : n ≤ m) (H2 : is_set (Ω[n] A)) : πg[m+1] A ≃g G0 := obtain (k : ℕ) (p : n + k = m), from le.elim H1, isomorphism_of_eq (ap (λx, πg[x+1] A) (p⁻¹ ⬝ add.comm n k)) ⬝g trivial_homotopy_add_of_is_set_loopn k H2 definition homotopy_group_pequiv_loop_ptrunc_con {k : ℕ} {A : Type*} (p q : πg[k +1] A) : homotopy_group_pequiv_loop_ptrunc (succ k) A (p * q) = homotopy_group_pequiv_loop_ptrunc (succ k) A p ⬝ homotopy_group_pequiv_loop_ptrunc (succ k) A q := begin refine _ ⬝ !loopn_pequiv_loopn_con, exact ap (loopn_pequiv_loopn _ _) !loopn_ptrunc_pequiv_inv_con end definition homotopy_group_pequiv_loop_ptrunc_inv_con {k : ℕ} {A : Type*} (p q : Ω[succ k] (ptrunc (succ k) A)) : (homotopy_group_pequiv_loop_ptrunc (succ k) A)⁻¹ᵉ* (p ⬝ q) = (homotopy_group_pequiv_loop_ptrunc (succ k) A)⁻¹ᵉ* p * (homotopy_group_pequiv_loop_ptrunc (succ k) A)⁻¹ᵉ* q := inv_preserve_binary (homotopy_group_pequiv_loop_ptrunc (succ k) A) mul concat (@homotopy_group_pequiv_loop_ptrunc_con k A) p q definition ghomotopy_group_ptrunc [constructor] (k : ℕ) (A : Type*) : πg[k+1] (ptrunc (k+1) A) ≃g πg[k+1] A := begin fapply isomorphism_of_equiv, { exact homotopy_group_ptrunc (k+1) A}, { intro g₁ g₂, refine _ ⬝ !homotopy_group_pequiv_loop_ptrunc_inv_con, apply ap ((homotopy_group_pequiv_loop_ptrunc (k+1) A)⁻¹ᵉ*), refine _ ⬝ !loopn_pequiv_loopn_con , apply ap (loopn_pequiv_loopn (k+1) _), apply homotopy_group_pequiv_loop_ptrunc_con} end /- some homomorphisms -/ -- definition is_homomorphism_cast_loopn_succ_eq_in {A : Type*} (n : ℕ) : -- is_homomorphism (loopn_succ_in A (succ n) : πg[n+1+1] A → πg[n+1] (Ω A)) := -- begin -- intro g h, induction g with g, induction h with h, -- xrewrite [tr_mul_tr, - + fn_cast_eq_cast_fn _ (λn, tr), tr_mul_tr, ↑cast, -tr_compose, -- loopn_succ_eq_in_concat, - + tr_compose], -- end definition is_homomorphism_inverse (A : Type*) (n : ℕ) : is_homomorphism (λp, p⁻¹ : (πag[n+2] A) → (πag[n+2] A)) := begin intro g h, exact ap inv (mul.comm g h) ⬝ mul_inv h g, end end eq
51bec885a70ff9f5589366771057817e39fd3228
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Util/ReplaceExpr.lean
471f7a4cddfe0a4ef28dbb388056f745d2c30c6b
[ "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
3,087
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 -/ prelude import Init.Lean.Expr namespace Lean namespace Expr namespace ReplaceImpl abbrev cacheSize : USize := 8192 structure State := (keys : Array Expr) -- Remark: our "unsafe" implementation relies on the fact that `()` is not a valid Expr (results : Array Expr) abbrev ReplaceM := StateM State @[inline] unsafe def cache (i : USize) (key : Expr) (result : Expr) : ReplaceM Expr := do modify $ fun s => { keys := s.keys.uset i key lcProof, results := s.results.uset i result lcProof }; pure result @[specialize] unsafe def replaceUnsafeM (f? : Expr → Option Expr) (size : USize) : Expr → ReplaceM Expr | e => do c ← get; let h := ptrAddrUnsafe e; let i := h % size; if ptrAddrUnsafe (c.keys.uget i lcProof) == h then pure $ c.results.uget i lcProof else match f? e with | some eNew => cache i e eNew | none => match e with | Expr.forallE _ d b _ => do d ← replaceUnsafeM d; b ← replaceUnsafeM b; cache i e $ e.updateForallE! d b | Expr.lam _ d b _ => do d ← replaceUnsafeM d; b ← replaceUnsafeM b; cache i e $ e.updateLambdaE! d b | Expr.mdata _ b _ => do b ← replaceUnsafeM b; cache i e $ e.updateMData! b | Expr.letE _ t v b _ => do t ← replaceUnsafeM t; v ← replaceUnsafeM v; b ← replaceUnsafeM b; cache i e $ e.updateLet! t v b | Expr.app f a _ => do f ← replaceUnsafeM f; a ← replaceUnsafeM a; cache i e $ e.updateApp! f a | Expr.proj _ _ b _ => do b ← replaceUnsafeM b; cache i e $ e.updateProj! b | Expr.localE _ _ _ _ => unreachable! | e => pure e unsafe def initCache : State := { keys := mkArray cacheSize.toNat (cast lcProof ()), -- `()` is not a valid `Expr` results := mkArray cacheSize.toNat (arbitrary _) } @[inline] unsafe def replaceUnsafe (f? : Expr → Option Expr) (e : Expr) : Expr := (replaceUnsafeM f? cacheSize e).run' initCache end ReplaceImpl /- TODO: use withPtrAddr, withPtrEq to avoid unsafe tricks above. We also need an invariant at `State` and proofs for the `uget` operations. -/ @[implementedBy ReplaceImpl.replaceUnsafe] partial def replace (f? : Expr → Option Expr) : Expr → Expr | e => /- This is a reference implementation for the unsafe one above -/ match f? e with | some eNew => eNew | none => match e with | Expr.forallE _ d b _ => let d := replace d; let b := replace b; e.updateForallE! d b | Expr.lam _ d b _ => let d := replace d; let b := replace b; e.updateLambdaE! d b | Expr.mdata _ b _ => let b := replace b; e.updateMData! b | Expr.letE _ t v b _ => let t := replace t; let v := replace v; let b := replace b; e.updateLet! t v b | Expr.app f a _ => let f := replace f; let a := replace a; e.updateApp! f a | Expr.proj _ _ b _ => let b := replace b; e.updateProj! b | e => e end Expr end Lean
eff8d62bab3f72d26e38c9ccab80e1f95ea5fa24
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/big_operators/ring.lean
6a0c5a9dca0dd0b6c3c4cbb803547f3b2dc375f2
[ "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
11,114
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.basic import algebra.field.defs import data.finset.pi import data.finset.powerset /-! # Results about big operators with values in a (semi)ring > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We prove results about big operators that involve some interaction between multiplicative and additive structures on the values being combined. -/ universes u v w open_locale big_operators variables {α : Type u} {β : Type v} {γ : Type w} namespace finset variables {s s₁ s₂ : finset α} {a : α} {b : β} {f g : α → β} section comm_monoid variables [comm_monoid β] open_locale classical lemma prod_pow_eq_pow_sum {x : β} {f : α → ℕ} : ∀ {s : finset α}, (∏ i in s, x ^ (f i)) = x ^ (∑ x in s, f x) := begin apply finset.induction, { simp }, { assume a s has H, rw [finset.prod_insert has, finset.sum_insert has, pow_add, H] } end end comm_monoid section semiring variables [non_unital_non_assoc_semiring β] lemma sum_mul : (∑ x in s, f x) * b = ∑ x in s, f x * b := add_monoid_hom.map_sum (add_monoid_hom.mul_right b) _ s lemma mul_sum : b * (∑ x in s, f x) = ∑ x in s, b * f x := add_monoid_hom.map_sum (add_monoid_hom.mul_left b) _ s lemma sum_mul_sum {ι₁ : Type*} {ι₂ : Type*} (s₁ : finset ι₁) (s₂ : finset ι₂) (f₁ : ι₁ → β) (f₂ : ι₂ → β) : (∑ x₁ in s₁, f₁ x₁) * (∑ x₂ in s₂, f₂ x₂) = ∑ p in s₁ ×ˢ s₂, f₁ p.1 * f₂ p.2 := by { rw [sum_product, sum_mul, sum_congr rfl], intros, rw mul_sum } end semiring section semiring variables [non_assoc_semiring β] lemma sum_mul_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) : (∑ x in s, (f x * ite (a = x) 1 0)) = ite (a ∈ s) (f a) 0 := by simp lemma sum_boole_mul [decidable_eq α] (s : finset α) (f : α → β) (a : α) : (∑ x in s, (ite (a = x) 1 0) * f x) = ite (a ∈ s) (f a) 0 := by simp end semiring lemma sum_div [division_semiring β] {s : finset α} {f : α → β} {b : β} : (∑ x in s, f x) / b = ∑ x in s, f x / b := by simp only [div_eq_mul_inv, sum_mul] section comm_semiring variables [comm_semiring β] /-- The product over a sum can be written as a sum over the product of sets, `finset.pi`. `finset.prod_univ_sum` is an alternative statement when the product is over `univ`. -/ lemma prod_sum {δ : α → Type*} [decidable_eq α] [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} : (∏ a in s, ∑ b in (t a), f a b) = ∑ p in (s.pi t), ∏ x in s.attach, f x.1 (p x.1 x.2) := begin induction s using finset.induction with a s ha ih, { rw [pi_empty, sum_singleton], refl }, { have h₁ : ∀x ∈ t a, ∀y ∈ t a, ∀h : x ≠ y, disjoint (image (pi.cons s a x) (pi s t)) (image (pi.cons s a y) (pi s t)), { assume x hx y hy h, simp only [disjoint_iff_ne, mem_image], rintros _ ⟨p₂, hp, eq₂⟩ _ ⟨p₃, hp₃, eq₃⟩ eq, have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _), { rw [eq₂, eq₃, eq] }, rw [pi.cons_same, pi.cons_same] at this, exact h this }, rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_bUnion h₁], refine sum_congr rfl (λ b _, _), have h₂ : ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from assume p₁ h₁ p₂ h₂ eq, pi.cons_injective ha eq, rw [sum_image h₂, mul_sum], refine sum_congr rfl (λ g _, _), rw [attach_insert, prod_insert, prod_image], { simp only [pi.cons_same], congr' with ⟨v, hv⟩, congr', exact (pi.cons_ne (by rintro rfl; exact ha hv)).symm }, { exact λ _ _ _ _, subtype.eq ∘ subtype.mk.inj }, { simp only [mem_image], rintro ⟨⟨_, hm⟩, _, rfl⟩, exact ha hm } } end open_locale classical /-- The product of `f a + g a` over all of `s` is the sum over the powerset of `s` of the product of `f` over a subset `t` times the product of `g` over the complement of `t` -/ lemma prod_add (f g : α → β) (s : finset α) : ∏ a in s, (f a + g a) = ∑ t in s.powerset, ((∏ a in t, f a) * (∏ a in (s \ t), g a)) := calc ∏ a in s, (f a + g a) = ∏ a in s, ∑ p in ({true, false} : finset Prop), if p then f a else g a : by simp ... = ∑ p in (s.pi (λ _, {true, false}) : finset (Π a ∈ s, Prop)), ∏ a in s.attach, if p a.1 a.2 then f a.1 else g a.1 : prod_sum ... = ∑ t in s.powerset, (∏ a in t, f a) * (∏ a in (s \ t), g a) : begin refine eq.symm (sum_bij (λ t _ a _, a ∈ t) _ _ _ _), { simp [subset_iff]; tauto }, { intros t ht, erw [prod_ite (λ a : {a // a ∈ s}, f a.1) (λ a : {a // a ∈ s}, g a.1)], refine congr_arg2 _ (prod_bij (λ (a : α) (ha : a ∈ t), ⟨a, mem_powerset.1 ht ha⟩) _ _ _ (λ b hb, ⟨b, by cases b; simpa only [true_and, exists_prop, mem_filter, and_true, mem_attach, eq_self_iff_true, subtype.coe_mk] using hb⟩)) (prod_bij (λ (a : α) (ha : a ∈ s \ t), ⟨a, by simp * at *⟩) _ _ _ (λ b hb, ⟨b, by cases b; begin simp only [true_and, mem_filter, mem_attach, subtype.coe_mk] at hb, simpa only [true_and, exists_prop, and_true, mem_sdiff, eq_self_iff_true, subtype.coe_mk, b_property], end⟩)); intros; simp * at *; simp * at * }, { assume a₁ a₂ h₁ h₂ H, ext x, simp only [function.funext_iff, subset_iff, mem_powerset, eq_iff_iff] at h₁ h₂ H, exact ⟨λ hx, (H x (h₁ hx)).1 hx, λ hx, (H x (h₂ hx)).2 hx⟩ }, { assume f hf, exact ⟨s.filter (λ a : α, ∃ h : a ∈ s, f a h), by simp, by funext; intros; simp *⟩ } end /-- `∏ i, (f i + g i) = (∏ i, f i) + ∑ i, g i * (∏ j < i, f j + g j) * (∏ j > i, f j)`. -/ lemma prod_add_ordered {ι R : Type*} [comm_semiring R] [linear_order ι] (s : finset ι) (f g : ι → R) : (∏ i in s, (f i + g i)) = (∏ i in s, f i) + ∑ i in s, g i * (∏ j in s.filter (< i), (f j + g j)) * ∏ j in s.filter (λ j, i < j), f j := begin refine finset.induction_on_max s (by simp) _, clear s, intros a s ha ihs, have ha' : a ∉ s, from λ ha', (ha a ha').false, rw [prod_insert ha', prod_insert ha', sum_insert ha', filter_insert, if_neg (lt_irrefl a), filter_true_of_mem ha, ihs, add_mul, mul_add, mul_add, add_assoc], congr' 1, rw add_comm, congr' 1, { rw [filter_false_of_mem, prod_empty, mul_one], exact (forall_mem_insert _ _ _).2 ⟨lt_irrefl a, λ i hi, (ha i hi).not_lt⟩ }, { rw mul_sum, refine sum_congr rfl (λ i hi, _), rw [filter_insert, if_neg (ha i hi).not_lt, filter_insert, if_pos (ha i hi), prod_insert, mul_left_comm], exact mt (λ ha, (mem_filter.1 ha).1) ha' } end /-- `∏ i, (f i - g i) = (∏ i, f i) - ∑ i, g i * (∏ j < i, f j - g j) * (∏ j > i, f j)`. -/ lemma prod_sub_ordered {ι R : Type*} [comm_ring R] [linear_order ι] (s : finset ι) (f g : ι → R) : (∏ i in s, (f i - g i)) = (∏ i in s, f i) - ∑ i in s, g i * (∏ j in s.filter (< i), (f j - g j)) * ∏ j in s.filter (λ j, i < j), f j := begin simp only [sub_eq_add_neg], convert prod_add_ordered s f (λ i, -g i), simp, end /-- `∏ i, (1 - f i) = 1 - ∑ i, f i * (∏ j < i, 1 - f j)`. This formula is useful in construction of a partition of unity from a collection of “bump” functions. -/ lemma prod_one_sub_ordered {ι R : Type*} [comm_ring R] [linear_order ι] (s : finset ι) (f : ι → R) : (∏ i in s, (1 - f i)) = 1 - ∑ i in s, f i * ∏ j in s.filter (< i), (1 - f j) := by { rw prod_sub_ordered, simp } /-- Summing `a^s.card * b^(n-s.card)` over all finite subsets `s` of a `finset` gives `(a + b)^s.card`.-/ lemma sum_pow_mul_eq_add_pow {α R : Type*} [comm_semiring R] (a b : R) (s : finset α) : (∑ t in s.powerset, a ^ t.card * b ^ (s.card - t.card)) = (a + b) ^ s.card := begin rw [← prod_const, prod_add], refine finset.sum_congr rfl (λ t ht, _), rw [prod_const, prod_const, ← card_sdiff (mem_powerset.1 ht)] end theorem dvd_sum {b : β} {s : finset α} {f : α → β} (h : ∀ x ∈ s, b ∣ f x) : b ∣ ∑ x in s, f x := multiset.dvd_sum (λ y hy, by rcases multiset.mem_map.1 hy with ⟨x, hx, rfl⟩; exact h x hx) @[norm_cast] lemma prod_nat_cast (s : finset α) (f : α → ℕ) : ↑(∏ x in s, f x : ℕ) = (∏ x in s, (f x : β)) := (nat.cast_ring_hom β).map_prod f s end comm_semiring section comm_ring variables {R : Type*} [comm_ring R] lemma prod_range_cast_nat_sub (n k : ℕ) : ∏ i in range k, (n - i : R) = (∏ i in range k, (n - i) : ℕ) := begin rw prod_nat_cast, cases le_or_lt k n with hkn hnk, { exact prod_congr rfl (λ i hi, (nat.cast_sub $ (mem_range.1 hi).le.trans hkn).symm) }, { rw ← mem_range at hnk, rw [prod_eq_zero hnk, prod_eq_zero hnk]; simp } end end comm_ring /-- A product over all subsets of `s ∪ {x}` is obtained by multiplying the product over all subsets of `s`, and over all subsets of `s` to which one adds `x`. -/ @[to_additive "A sum over all subsets of `s ∪ {x}` is obtained by summing the sum over all subsets of `s`, and over all subsets of `s` to which one adds `x`."] lemma prod_powerset_insert [decidable_eq α] [comm_monoid β] {s : finset α} {x : α} (h : x ∉ s) (f : finset α → β) : (∏ a in (insert x s).powerset, f a) = (∏ a in s.powerset, f a) * (∏ t in s.powerset, f (insert x t)) := begin rw [powerset_insert, finset.prod_union, finset.prod_image], { assume t₁ h₁ t₂ h₂ heq, rw [← finset.erase_insert (not_mem_of_mem_powerset_of_not_mem h₁ h), ← finset.erase_insert (not_mem_of_mem_powerset_of_not_mem h₂ h), heq] }, { rw finset.disjoint_iff_ne, assume t₁ h₁ t₂ h₂, rcases finset.mem_image.1 h₂ with ⟨t₃, h₃, H₃₂⟩, rw ← H₃₂, exact ne_insert_of_not_mem _ _ (not_mem_of_mem_powerset_of_not_mem h₁ h) } end /-- A product over `powerset s` is equal to the double product over sets of subsets of `s` with `card s = k`, for `k = 1, ..., card s`. -/ @[to_additive "A sum over `powerset s` is equal to the double sum over sets of subsets of `s` with `card s = k`, for `k = 1, ..., card s`"] lemma prod_powerset [comm_monoid β] (s : finset α) (f : finset α → β) : ∏ t in powerset s, f t = ∏ j in range (card s + 1), ∏ t in powerset_len j s, f t := by rw [powerset_card_disj_Union, prod_disj_Union] lemma sum_range_succ_mul_sum_range_succ [non_unital_non_assoc_semiring β] (n k : ℕ) (f g : ℕ → β) : (∑ i in range (n+1), f i) * (∑ i in range (k+1), g i) = (∑ i in range n, f i) * (∑ i in range k, g i) + f n * (∑ i in range k, g i) + (∑ i in range n, f i) * g k + f n * g k := by simp only [add_mul, mul_add, add_assoc, sum_range_succ] end finset
6991c140a0d7817db283cf5bd7c0374481d5ae39
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/eval_attr_cache.lean
93afefb04beb88c624289405a9bd5fea86950848
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
719
lean
open tactic @[user_attribute] meta def my_attr : caching_user_attribute (name → bool) := { name := "my_attr", descr := "my attr", mk_cache := λ ls, do { let c := `(λ n : name, (name.cases_on n ff (λ _ _, to_bool (n ∈ ls)) (λ _ _, ff) : bool)), eval_expr (name → bool) c }, dependencies := [] } meta def my_tac : tactic unit := do f ← caching_user_attribute.get_cache my_attr, trace (f `foo), return () @[my_attr] def bla := 10 run_cmd my_tac @[my_attr] def foo := 10 -- Cache was invalided run_cmd my_tac -- Add closure to the cache containing auxiliary function created by eval_expr run_cmd my_tac -- Cache should be flushed since the auxiliary function is gone
50d380377bdc94b89556333a02cff807e383d610
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/field_theory/separable.lean
46622868d23bbdd561453f5a1dfe34563d67d890
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,388
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.polynomial.big_operators import field_theory.minpoly import field_theory.splitting_field import field_theory.tower import algebra.squarefree /-! # Separable polynomials We define a polynomial to be separable if it is coprime with its derivative. We prove basic properties about separable polynomials here. ## Main definitions * `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative. * `polynomial.expand R p f`: expand the polynomial `f` with coefficients in a commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`. * `polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/ universes u v w open_locale classical big_operators open finset namespace polynomial section comm_semiring variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S] /-- A polynomial is separable iff it is coprime with its derivative. -/ def separable (f : polynomial R) : Prop := is_coprime f f.derivative lemma separable_def (f : polynomial R) : f.separable ↔ is_coprime f f.derivative := iff.rfl lemma separable_def' (f : polynomial R) : f.separable ↔ ∃ a b : polynomial R, a * f + b * f.derivative = 1 := iff.rfl lemma separable_one : (1 : polynomial R).separable := is_coprime_one_left lemma separable_X_add_C (a : R) : (X + C a).separable := by { rw [separable_def, derivative_add, derivative_X, derivative_C, add_zero], exact is_coprime_one_right } lemma separable_X : (X : polynomial R).separable := by { rw [separable_def, derivative_X], exact is_coprime_one_right } lemma separable_C (r : R) : (C r).separable ↔ is_unit r := by rw [separable_def, derivative_C, is_coprime_zero_right, is_unit_C] lemma separable.of_mul_left {f g : polynomial R} (h : (f * g).separable) : f.separable := begin have := h.of_mul_left_left, rw derivative_mul at this, exact is_coprime.of_mul_right_left (is_coprime.of_add_mul_left_right this) end lemma separable.of_mul_right {f g : polynomial R} (h : (f * g).separable) : g.separable := by { rw mul_comm at h, exact h.of_mul_left } lemma separable.of_dvd {f g : polynomial R} (hf : f.separable) (hfg : g ∣ f) : g.separable := by { rcases hfg with ⟨f', rfl⟩, exact separable.of_mul_left hf } lemma separable_gcd_left {F : Type*} [field F] {f : polynomial F} (hf : f.separable) (g : polynomial F) : (euclidean_domain.gcd f g).separable := separable.of_dvd hf (euclidean_domain.gcd_dvd_left f g) lemma separable_gcd_right {F : Type*} [field F] {g : polynomial F} (f : polynomial F) (hg : g.separable) : (euclidean_domain.gcd f g).separable := separable.of_dvd hg (euclidean_domain.gcd_dvd_right f g) lemma separable.is_coprime {f g : polynomial R} (h : (f * g).separable) : is_coprime f g := begin have := h.of_mul_left_left, rw derivative_mul at this, exact is_coprime.of_mul_right_right (is_coprime.of_add_mul_left_right this) end theorem separable.of_pow' {f : polynomial R} : ∀ {n : ℕ} (h : (f ^ n).separable), is_unit f ∨ (f.separable ∧ n = 1) ∨ n = 0 | 0 := λ h, or.inr $ or.inr rfl | 1 := λ h, or.inr $ or.inl ⟨pow_one f ▸ h, rfl⟩ | (n+2) := λ h, by { rw [pow_succ, pow_succ] at h, exact or.inl (is_coprime_self.1 h.is_coprime.of_mul_right_left) } theorem separable.of_pow {f : polynomial R} (hf : ¬is_unit f) {n : ℕ} (hn : n ≠ 0) (hfs : (f ^ n).separable) : f.separable ∧ n = 1 := (hfs.of_pow'.resolve_left hf).resolve_right hn theorem separable.map {p : polynomial R} (h : p.separable) {f : R →+* S} : (p.map f).separable := let ⟨a, b, H⟩ := h in ⟨a.map f, b.map f, by rw [derivative_map, ← map_mul, ← map_mul, ← map_add, H, map_one]⟩ variables (R) (p q : ℕ) /-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/ noncomputable def expand : polynomial R →ₐ[R] polynomial R := { commutes' := λ r, eval₂_C _ _, .. (eval₂_ring_hom C (X ^ p) : polynomial R →+* polynomial R) } lemma coe_expand : (expand R p : polynomial R → polynomial R) = eval₂ C (X ^ p) := rfl variables {R} lemma expand_eq_sum {f : polynomial R} : expand R p f = f.sum (λ e a, C a * (X ^ p) ^ e) := by { dsimp [expand, eval₂], refl, } @[simp] lemma expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _ @[simp] lemma expand_X : expand R p X = X ^ p := eval₂_X _ _ @[simp] lemma expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r := by simp_rw [monomial_eq_smul_X, alg_hom.map_smul, alg_hom.map_pow, expand_X, mul_comm, pow_mul] theorem expand_expand (f : polynomial R) : expand R p (expand R q f) = expand R (p * q) f := polynomial.induction_on f (λ r, by simp_rw expand_C) (λ f g ihf ihg, by simp_rw [alg_hom.map_add, ihf, ihg]) (λ n r ih, by simp_rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, alg_hom.map_pow, expand_X, pow_mul]) theorem expand_mul (f : polynomial R) : expand R (p * q) f = expand R p (expand R q f) := (expand_expand p q f).symm @[simp] theorem expand_one (f : polynomial R) : expand R 1 f = f := polynomial.induction_on f (λ r, by rw expand_C) (λ f g ihf ihg, by rw [alg_hom.map_add, ihf, ihg]) (λ n r ih, by rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, pow_one]) theorem expand_pow (f : polynomial R) : expand R (p ^ q) f = (expand R p ^[q] f) := nat.rec_on q (by rw [pow_zero, expand_one, function.iterate_zero, id]) $ λ n ih, by rw [function.iterate_succ_apply', pow_succ, expand_mul, ih] theorem derivative_expand (f : polynomial R) : (expand R p f).derivative = expand R p f.derivative * (p * X ^ (p - 1)) := by rw [coe_expand, derivative_eval₂_C, derivative_pow, derivative_X, mul_one] theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) : (expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 := begin simp only [expand_eq_sum], simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, sum], split_ifs with h, { rw [finset.sum_eq_single (n/p), nat.mul_div_cancel' h, if_pos rfl], { intros b hb1 hb2, rw if_neg, intro hb3, apply hb2, rw [← hb3, nat.mul_div_cancel_left b hp] }, { intro hn, rw not_mem_support_iff.1 hn, split_ifs; refl } }, { rw finset.sum_eq_zero, intros k hk, rw if_neg, exact λ hkn, h ⟨k, hkn.symm⟩, }, end @[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) : (expand R p f).coeff (n * p) = f.coeff n := by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), nat.mul_div_cancel _ hp] @[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) : (expand R p f).coeff (p * n) = f.coeff n := by rw [mul_comm, coeff_expand_mul hp] theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : polynomial R} : expand R p f = expand R p g ↔ f = g := ⟨λ H, ext $ λ n, by rw [← coeff_expand_mul hp, H, coeff_expand_mul hp], congr_arg _⟩ theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : polynomial R} : expand R p f = 0 ↔ f = 0 := by rw [← (expand R p).map_zero, expand_inj hp, alg_hom.map_zero] theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : polynomial R} {r : R} : expand R p f = C r ↔ f = C r := by rw [← expand_C, expand_inj hp, expand_C] theorem nat_degree_expand (p : ℕ) (f : polynomial R) : (expand R p f).nat_degree = f.nat_degree * p := begin cases p.eq_zero_or_pos with hp hp, { rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, nat_degree_C] }, by_cases hf : f = 0, { rw [hf, alg_hom.map_zero, nat_degree_zero, zero_mul] }, have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf, rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree hf1], refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 $ λ n hn, _) _, { rw coeff_expand hp, split_ifs with hpn, { rw coeff_eq_zero_of_nat_degree_lt, contrapose! hn, rw [with_bot.coe_le_coe, ← nat.div_mul_cancel hpn], exact nat.mul_le_mul_right p hn }, { refl } }, { refine le_degree_of_ne_zero _, rw [coeff_expand_mul hp, ← leading_coeff], exact mt leading_coeff_eq_zero.1 hf } end theorem map_expand {p : ℕ} (hp : 0 < p) {f : R →+* S} {q : polynomial R} : map f (expand R p q) = expand S p (map f q) := by { ext, rw [coeff_map, coeff_expand hp, coeff_expand hp], split_ifs; simp, } /-- Expansion is injective. -/ lemma expand_injective {n : ℕ} (hn : 0 < n) : function.injective (expand R n) := λ g g' h, begin ext, have h' : (expand R n g).coeff (n * n_1) = (expand R n g').coeff (n * n_1) := begin apply polynomial.ext_iff.1, exact h, end, rw [polynomial.coeff_expand hn g (n * n_1), polynomial.coeff_expand hn g' (n * n_1)] at h', simp only [if_true, dvd_mul_right] at h', rw (nat.mul_div_right n_1 hn) at h', exact h', end end comm_semiring section comm_ring variables {R : Type u} [comm_ring R] lemma separable_X_sub_C {x : R} : separable (X - C x) := by simpa only [sub_eq_add_neg, C_neg] using separable_X_add_C (-x) lemma separable.mul {f g : polynomial R} (hf : f.separable) (hg : g.separable) (h : is_coprime f g) : (f * g).separable := by { rw [separable_def, derivative_mul], exact ((hf.mul_right h).add_mul_left_right _).mul_left ((h.symm.mul_right hg).mul_add_right_right _) } lemma separable_prod' {ι : Sort*} {f : ι → polynomial R} {s : finset ι} : (∀x∈s, ∀y∈s, x ≠ y → is_coprime (f x) (f y)) → (∀x∈s, (f x).separable) → (∏ x in s, f x).separable := finset.induction_on s (λ _ _, separable_one) $ λ a s has ih h1 h2, begin simp_rw [finset.forall_mem_insert, forall_and_distrib] at h1 h2, rw prod_insert has, exact h2.1.mul (ih h1.2.2 h2.2) (is_coprime.prod_right $ λ i his, h1.1.2 i his $ ne.symm $ ne_of_mem_of_not_mem his has) end lemma separable_prod {ι : Sort*} [fintype ι] {f : ι → polynomial R} (h1 : pairwise (is_coprime on f)) (h2 : ∀ x, (f x).separable) : (∏ x, f x).separable := separable_prod' (λ x hx y hy hxy, h1 x y hxy) (λ x hx, h2 x) lemma separable.inj_of_prod_X_sub_C [nontrivial R] {ι : Sort*} {f : ι → R} {s : finset ι} (hfs : (∏ i in s, (X - C (f i))).separable) {x y : ι} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y := begin by_contra hxy, rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase_of_ne_of_mem (ne.symm hxy) hy), prod_insert (not_mem_erase _ _), ← mul_assoc, hfxy, ← sq] at hfs, cases (hfs.of_mul_left.of_pow (by exact not_is_unit_X_sub_C) two_ne_zero).2 end lemma separable.injective_of_prod_X_sub_C [nontrivial R] {ι : Sort*} [fintype ι] {f : ι → R} (hfs : (∏ i, (X - C (f i))).separable) : function.injective f := λ x y hfxy, hfs.inj_of_prod_X_sub_C (mem_univ _) (mem_univ _) hfxy lemma is_unit_of_self_mul_dvd_separable {p q : polynomial R} (hp : p.separable) (hq : q * q ∣ p) : is_unit q := begin obtain ⟨p, rfl⟩ := hq, apply is_coprime_self.mp, have : is_coprime (q * (q * p)) (q * (q.derivative * p + q.derivative * p + q * p.derivative)), { simp only [← mul_assoc, mul_add], convert hp, rw [derivative_mul, derivative_mul], ring }, exact is_coprime.of_mul_right_left (is_coprime.of_mul_left_left this) end end comm_ring section integral_domain variables (R : Type u) [integral_domain R] theorem is_local_ring_hom_expand {p : ℕ} (hp : 0 < p) : is_local_ring_hom (↑(expand R p) : polynomial R →+* polynomial R) := begin refine ⟨λ f hf1, _⟩, rw ← coe_fn_coe_base at hf1, have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf1), rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2, rw [hf2, is_unit_C] at hf1, rw expand_eq_C hp at hf2, rwa [hf2, is_unit_C] end end integral_domain section field variables {F : Type u} [field F] {K : Type v} [field K] theorem separable_iff_derivative_ne_zero {f : polynomial F} (hf : irreducible f) : f.separable ↔ f.derivative ≠ 0 := ⟨λ h1 h2, hf.not_unit $ is_coprime_zero_right.1 $ h2 ▸ h1, λ h, is_coprime_of_dvd (mt and.right h) $ λ g hg1 hg2 ⟨p, hg3⟩ hg4, let ⟨u, hu⟩ := (hf.is_unit_or_is_unit hg3).resolve_left hg1 in have f ∣ f.derivative, by { conv_lhs { rw [hg3, ← hu] }, rwa units.mul_right_dvd }, not_lt_of_le (nat_degree_le_of_dvd this h) $ nat_degree_derivative_lt h⟩ theorem separable_map (f : F →+* K) {p : polynomial F} : (p.map f).separable ↔ p.separable := by simp_rw [separable_def, derivative_map, is_coprime_map] section char_p /-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/ noncomputable def contract (p : ℕ) (f : polynomial F) : polynomial F := ∑ n in range (f.nat_degree + 1), monomial n (f.coeff (n * p)) variables (p : ℕ) [hp : fact p.prime] include hp theorem coeff_contract (f : polynomial F) (n : ℕ) : (contract p f).coeff n = f.coeff (n * p) := begin simp only [contract, coeff_monomial, sum_ite_eq', finset_sum_coeff, mem_range, not_lt, ite_eq_left_iff], assume hn, apply (coeff_eq_zero_of_nat_degree_lt _).symm, calc f.nat_degree < f.nat_degree + 1 : nat.lt_succ_self _ ... ≤ n * 1 : by simpa only [mul_one] using hn ... ≤ n * p : mul_le_mul_of_nonneg_left (@nat.prime.one_lt p (fact.out _)).le (zero_le n) end theorem of_irreducible_expand {f : polynomial F} (hf : irreducible (expand F p f)) : irreducible f := @@of_irreducible_map _ _ _ (is_local_ring_hom_expand F hp.1.pos) hf theorem of_irreducible_expand_pow {f : polynomial F} {n : ℕ} : irreducible (expand F (p ^ n) f) → irreducible f := nat.rec_on n (λ hf, by rwa [pow_zero, expand_one] at hf) $ λ n ih hf, ih $ of_irreducible_expand p $ by { rw pow_succ at hf, rwa [expand_expand] } variables [HF : char_p F p] include HF theorem expand_char (f : polynomial F) : map (frobenius F p) (expand F p f) = f ^ p := begin refine f.induction_on' (λ a b ha hb, _) (λ n a, _), { rw [alg_hom.map_add, map_add, ha, hb, add_pow_char], }, { rw [expand_monomial, map_monomial, monomial_eq_C_mul_X, monomial_eq_C_mul_X, mul_pow, ← C.map_pow, frobenius_def], ring_exp } end theorem map_expand_pow_char (f : polynomial F) (n : ℕ) : map ((frobenius F p) ^ n) (expand F (p ^ n) f) = f ^ (p ^ n) := begin induction n, { simp [ring_hom.one_def] }, symmetry, rw [pow_succ', pow_mul, ← n_ih, ← expand_char, pow_succ, ring_hom.mul_def, ← map_map, mul_comm, expand_mul, ← map_expand (nat.prime.pos hp.1)], end theorem expand_contract {f : polynomial F} (hf : f.derivative = 0) : expand F p (contract p f) = f := begin ext n, rw [coeff_expand hp.1.pos, coeff_contract], split_ifs with h, { rw nat.div_mul_cancel h }, { cases n, { exact absurd (dvd_zero p) h }, have := coeff_derivative f n, rw [hf, coeff_zero, zero_eq_mul] at this, cases this, { rw this }, rw [← nat.cast_succ, char_p.cast_eq_zero_iff F p] at this, exact absurd this h } end theorem separable_or {f : polynomial F} (hf : irreducible f) : f.separable ∨ ¬f.separable ∧ ∃ g : polynomial F, irreducible g ∧ expand F p g = f := if H : f.derivative = 0 then or.inr ⟨by rw [separable_iff_derivative_ne_zero hf, not_not, H], contract p f, by haveI := is_local_ring_hom_expand F hp.1.pos; exact of_irreducible_map ↑(expand F p) (by rwa ← expand_contract p H at hf), expand_contract p H⟩ else or.inl $ (separable_iff_derivative_ne_zero hf).2 H theorem exists_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0) : ∃ (n : ℕ) (g : polynomial F), g.separable ∧ expand F (p ^ n) g = f := begin generalize hn : f.nat_degree = N, unfreezingI { revert f }, apply nat.strong_induction_on N, intros N ih f hf hf0 hn, rcases separable_or p hf with h | ⟨h1, g, hg, hgf⟩, { refine ⟨0, f, h, _⟩, rw [pow_zero, expand_one] }, { cases N with N, { rw [nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff] at hn, rw [hn, separable_C, is_unit_iff_ne_zero, not_not] at h1, rw [h1, C_0] at hn, exact absurd hn hf0 }, have hg1 : g.nat_degree * p = N.succ, { rwa [← nat_degree_expand, hgf] }, have hg2 : g.nat_degree ≠ 0, { intro this, rw [this, zero_mul] at hg1, cases hg1 }, have hg3 : g.nat_degree < N.succ, { rw [← mul_one g.nat_degree, ← hg1], exact nat.mul_lt_mul_of_pos_left hp.1.one_lt (nat.pos_of_ne_zero hg2) }, have hg4 : g ≠ 0, { rintro rfl, exact hg2 nat_degree_zero }, rcases ih _ hg3 hg hg4 rfl with ⟨n, g, hg5, rfl⟩, refine ⟨n+1, g, hg5, _⟩, rw [← hgf, expand_expand, pow_succ] } end theorem is_unit_or_eq_zero_of_separable_expand {f : polynomial F} (n : ℕ) (hf : (expand F (p ^ n) f).separable) : is_unit f ∨ n = 0 := begin rw or_iff_not_imp_right, intro hn, have hf2 : (expand F (p ^ n) f).derivative = 0, { by rw [derivative_expand, nat.cast_pow, char_p.cast_eq_zero, zero_pow (nat.pos_of_ne_zero hn), zero_mul, mul_zero] }, rw [separable_def, hf2, is_coprime_zero_right, is_unit_iff] at hf, rcases hf with ⟨r, hr, hrf⟩, rw [eq_comm, expand_eq_C (pow_pos hp.1.pos _)] at hrf, rwa [hrf, is_unit_C] end theorem unique_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0) (n₁ : ℕ) (g₁ : polynomial F) (hg₁ : g₁.separable) (hgf₁ : expand F (p ^ n₁) g₁ = f) (n₂ : ℕ) (g₂ : polynomial F) (hg₂ : g₂.separable) (hgf₂ : expand F (p ^ n₂) g₂ = f) : n₁ = n₂ ∧ g₁ = g₂ := begin revert g₁ g₂, wlog hn : n₁ ≤ n₂ := le_total n₁ n₂ using [n₁ n₂, n₂ n₁] tactic.skip, unfreezingI { intros, rw le_iff_exists_add at hn, rcases hn with ⟨k, rfl⟩, rw [← hgf₁, pow_add, expand_mul, expand_inj (pow_pos hp.1.pos n₁)] at hgf₂, subst hgf₂, subst hgf₁, rcases is_unit_or_eq_zero_of_separable_expand p k hg₁ with h | rfl, { rw is_unit_iff at h, rcases h with ⟨r, hr, rfl⟩, simp_rw expand_C at hf, exact absurd (is_unit_C.2 hr) hf.1 }, { rw [add_zero, pow_zero, expand_one], split; refl } }, exact λ g₁ g₂ hg₁ hgf₁ hg₂ hgf₂, let ⟨hn, hg⟩ := this g₂ g₁ hg₂ hgf₂ hg₁ hgf₁ in ⟨hn.symm, hg.symm⟩ end end char_p lemma separable_prod_X_sub_C_iff' {ι : Sort*} {f : ι → F} {s : finset ι} : (∏ i in s, (X - C (f i))).separable ↔ (∀ (x ∈ s) (y ∈ s), f x = f y → x = y) := ⟨λ hfs x hx y hy hfxy, hfs.inj_of_prod_X_sub_C hx hy hfxy, λ H, by { rw ← prod_attach, exact separable_prod' (λ x hx y hy hxy, @pairwise_coprime_X_sub _ _ { x // x ∈ s } (λ x, f x) (λ x y hxy, subtype.eq $ H x.1 x.2 y.1 y.2 hxy) _ _ hxy) (λ _ _, separable_X_sub_C) }⟩ lemma separable_prod_X_sub_C_iff {ι : Sort*} [fintype ι] {f : ι → F} : (∏ i, (X - C (f i))).separable ↔ function.injective f := separable_prod_X_sub_C_iff'.trans $ by simp_rw [mem_univ, true_implies_iff] section splits open_locale big_operators variables {i : F →+* K} lemma not_unit_X_sub_C (a : F) : ¬ is_unit (X - C a) := λ h, have one_eq_zero : (1 : with_bot ℕ) = 0, by simpa using degree_eq_zero_of_is_unit h, one_ne_zero (option.some_injective _ one_eq_zero) lemma nodup_of_separable_prod {s : multiset F} (hs : separable (multiset.map (λ a, X - C a) s).prod) : s.nodup := begin rw multiset.nodup_iff_ne_cons_cons, rintros a t rfl, refine not_unit_X_sub_C a (is_unit_of_self_mul_dvd_separable hs _), simpa only [multiset.map_cons, multiset.prod_cons] using mul_dvd_mul_left _ (dvd_mul_right _ _) end lemma multiplicity_le_one_of_separable {p q : polynomial F} (hq : ¬ is_unit q) (hsep : separable p) : multiplicity q p ≤ 1 := begin contrapose! hq, apply is_unit_of_self_mul_dvd_separable hsep, rw ← sq, apply multiplicity.pow_dvd_of_le_multiplicity, exact_mod_cast (enat.add_one_le_of_lt hq) end lemma separable.squarefree {p : polynomial F} (hsep : separable p) : squarefree p := begin rw multiplicity.squarefree_iff_multiplicity_le_one p, intro f, by_cases hunit : is_unit f, { exact or.inr hunit }, exact or.inl (multiplicity_le_one_of_separable hunit hsep) end /--If `n ≠ 0` in `F`, then ` X ^ n - a` is separable for any `a ≠ 0`. -/ lemma separable_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) : separable (X ^ n - C a) := begin cases nat.eq_zero_or_pos n with hzero hpos, { exfalso, rw hzero at hn, exact hn (refl 0) }, apply (separable_def' (X ^ n - C a)).2, use [-C (a⁻¹), (C ((a⁻¹) * (↑n)⁻¹) * X)], have mul_pow_sub : X * X ^ (n - 1) = X ^ n, { nth_rewrite 0 [←pow_one X], rw pow_mul_pow_sub X (nat.succ_le_iff.mpr hpos) }, rw [derivative_sub, derivative_C, sub_zero, derivative_pow X n, derivative_X, mul_one], have hcalc : C (a⁻¹ * (↑n)⁻¹) * (↑n * (X ^ n)) = C a⁻¹ * (X ^ n), { calc C (a⁻¹ * (↑n)⁻¹) * (↑n * (X ^ n)) = C a⁻¹ * C ((↑n)⁻¹) * (C ↑n * (X ^ n)) : by rw [C_mul, C_eq_nat_cast] ... = C a⁻¹ * (C ((↑n)⁻¹) * C ↑n) * (X ^ n) : by ring ... = C a⁻¹ * C ((↑n)⁻¹ * ↑n) * (X ^ n) : by rw [← C_mul] ... = C a⁻¹ * C 1 * (X ^ n) : by field_simp [hn] ... = C a⁻¹ * (X ^ n) : by rw [C_1, mul_one] }, calc -C a⁻¹ * (X ^ n - C a) + C (a⁻¹ * (↑n)⁻¹) * X * (↑n * X ^ (n - 1)) = -C a⁻¹ * (X ^ n - C a) + C (a⁻¹ * (↑n)⁻¹) * (↑n * (X * X ^ (n - 1))) : by ring ... = -C a⁻¹ * (X ^ n - C a) + C a⁻¹ * (X ^ n) : by rw [mul_pow_sub, hcalc] ... = C a⁻¹ * C a : by ring ... = (1 : polynomial F) : by rw [← C_mul, inv_mul_cancel ha, C_1] end /--If `n ≠ 0` in `F`, then ` X ^ n - a` is squarefree for any `a ≠ 0`. -/ lemma squarefree_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) : squarefree (X ^ n - C a) := (separable_X_pow_sub_C a hn ha).squarefree lemma root_multiplicity_le_one_of_separable {p : polynomial F} (hp : p ≠ 0) (hsep : separable p) (x : F) : root_multiplicity x p ≤ 1 := begin rw [root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_le_coe, enat.coe_get], exact multiplicity_le_one_of_separable (not_unit_X_sub_C _) hsep end lemma count_roots_le_one {p : polynomial F} (hsep : separable p) (x : F) : p.roots.count x ≤ 1 := begin by_cases hp : p = 0, { simp [hp] }, rw count_roots hp, exact root_multiplicity_le_one_of_separable hp hsep x end lemma nodup_roots {p : polynomial F} (hsep : separable p) : p.roots.nodup := multiset.nodup_iff_count_le_one.mpr (count_roots_le_one hsep) lemma card_root_set_eq_nat_degree [algebra F K] {p : polynomial F} (hsep : p.separable) (hsplit : splits (algebra_map F K) p) : fintype.card (p.root_set K) = p.nat_degree := begin simp_rw [root_set_def, finset.coe_sort_coe, fintype.card_coe], rw [multiset.to_finset_card_of_nodup, ←nat_degree_eq_card_roots hsplit], exact nodup_roots hsep.map, end lemma eq_X_sub_C_of_separable_of_root_eq {x : F} {h : polynomial F} (h_ne_zero : h ≠ 0) (h_sep : h.separable) (h_root : h.eval x = 0) (h_splits : splits i h) (h_roots : ∀ y ∈ (h.map i).roots, y = i x) : h = (C (leading_coeff h)) * (X - C x) := begin apply polynomial.eq_X_sub_C_of_splits_of_single_root i h_splits, apply finset.mk.inj, { change _ = {i x}, rw finset.eq_singleton_iff_unique_mem, split, { apply finset.mem_mk.mpr, rw mem_roots (show h.map i ≠ 0, by exact map_ne_zero h_ne_zero), rw [is_root.def,←eval₂_eq_eval_map,eval₂_hom,h_root], exact ring_hom.map_zero i }, { exact h_roots } }, { exact nodup_roots (separable.map h_sep) }, end end splits end field end polynomial open polynomial theorem irreducible.separable {F : Type u} [field F] [char_zero F] {f : polynomial F} (hf : irreducible f) : f.separable := begin rw [separable_iff_derivative_ne_zero hf, ne, ← degree_eq_bot, degree_derivative_eq], rintro ⟨⟩, rw [pos_iff_ne_zero, ne, nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff], refine λ hf1, hf.not_unit _, rw [hf1, is_unit_C, is_unit_iff_ne_zero], intro hf2, rw [hf2, C_0] at hf1, exact absurd hf1 hf.ne_zero end -- TODO: refactor to allow transcendental extensions? -- See: https://en.wikipedia.org/wiki/Separable_extension#Separability_of_transcendental_extensions /-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff the minimal polynomial of every `x : K` is separable. -/ class is_separable (F K : Sort*) [field F] [field K] [algebra F K] : Prop := (is_integral' (x : K) : is_integral F x) (separable' (x : K) : (minpoly F x).separable) theorem is_separable.is_integral {F K} [field F] [field K] [algebra F K] (h : is_separable F K) : ∀ x : K, is_integral F x := is_separable.is_integral' theorem is_separable.separable {F K} [field F] [field K] [algebra F K] (h : is_separable F K) : ∀ x : K, (minpoly F x).separable := is_separable.separable' theorem is_separable_iff {F K} [field F] [field K] [algebra F K] : is_separable F K ↔ ∀ x : K, is_integral F x ∧ (minpoly F x).separable := ⟨λ h x, ⟨h.is_integral x, h.separable x⟩, λ h, ⟨λ x, (h x).1, λ x, (h x).2⟩⟩ instance is_separable_self (F : Type*) [field F] : is_separable F F := ⟨λ x, is_integral_algebra_map, λ x, by { rw minpoly.eq_X_sub_C', exact separable_X_sub_C }⟩ section is_separable_tower variables (F K E : Type*) [field F] [field K] [field E] [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E] lemma is_separable_tower_top_of_is_separable [h : is_separable F E] : is_separable K E := ⟨λ x, is_integral_of_is_scalar_tower x (h.is_integral x), λ x, (h.separable x).map.of_dvd (minpoly.dvd_map_of_is_scalar_tower _ _ _)⟩ lemma is_separable_tower_bot_of_is_separable [h : is_separable F E] : is_separable F K := is_separable_iff.2 $ λ x, begin refine (is_separable_iff.1 h (algebra_map K E x)).imp is_integral_tower_bot_of_is_integral_field (λ hs, _), obtain ⟨q, hq⟩ := minpoly.dvd F x (is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero_field (minpoly.aeval F ((algebra_map K E) x))), rw hq at hs, exact hs.of_mul_left end variables {E} lemma is_separable.of_alg_hom (E' : Type*) [field E'] [algebra F E'] (f : E →ₐ[F] E') [is_separable F E'] : is_separable F E := begin letI : algebra E E' := ring_hom.to_algebra f.to_ring_hom, haveI : is_scalar_tower F E E' := is_scalar_tower.of_algebra_map_eq (λ x, (f.commutes x).symm), exact is_separable_tower_bot_of_is_separable F E E', end end is_separable_tower section card_alg_hom variables {R S T : Type*} [comm_ring S] variables {K L F : Type*} [field K] [field L] [field F] variables [algebra K S] [algebra K L] lemma alg_hom.card_of_power_basis (pb : power_basis K S) (h_sep : (minpoly K pb.gen).separable) (h_splits : (minpoly K pb.gen).splits (algebra_map K L)) : @fintype.card (S →ₐ[K] L) (power_basis.alg_hom.fintype pb) = (minpoly K pb.gen).nat_degree := begin let s := ((minpoly K pb.gen).map (algebra_map K L)).roots.to_finset, have H := λ x, multiset.mem_to_finset, rw [fintype.card_congr pb.lift_equiv', fintype.card_of_subtype s H, nat_degree_eq_card_roots h_splits, multiset.to_finset_card_of_nodup], exact nodup_roots ((separable_map (algebra_map K L)).mpr h_sep) end end card_alg_hom
8cff9ccb92740b95d388f863a8aed3cd499d155c
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebraic_geometry/projective_spectrum/topology.lean
4425efc12d9dece2df2acbd004e5f234e79b6f87
[ "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
18,868
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Johan Commelin -/ import topology.category.Top import ring_theory.graded_algebra.homogeneous_ideal /-! # Projective spectrum of a graded ring The projective spectrum of a graded commutative ring is the subtype of all homogenous ideals that are prime and do not contain the irrelevant ideal. It is naturally endowed with a topology: the Zariski topology. ## Notation - `R` is a commutative semiring; - `A` is a commutative ring and an `R`-algebra; - `𝒜 : ℕ → submodule R A` is the grading of `A`; ## Main definitions * `projective_spectrum 𝒜`: The projective spectrum of a graded ring `A`, or equivalently, the set of all homogeneous ideals of `A` that is both prime and relevant i.e. not containing irrelevant ideal. Henceforth, we call elements of projective spectrum *relevant homogeneous prime ideals*. * `projective_spectrum.zero_locus 𝒜 s`: The zero locus of a subset `s` of `A` is the subset of `projective_spectrum 𝒜` consisting of all relevant homogeneous prime ideals that contain `s`. * `projective_spectrum.vanishing_ideal t`: The vanishing ideal of a subset `t` of `projective_spectrum 𝒜` is the intersection of points in `t` (viewed as relevant homogeneous prime ideals). * `projective_spectrum.Top`: the topological space of `projective_spectrum 𝒜` endowed with the Zariski topology -/ noncomputable theory open_locale direct_sum big_operators pointwise open direct_sum set_like Top topological_space category_theory opposite variables {R A: Type*} variables [comm_semiring R] [comm_ring A] [algebra R A] variables (𝒜 : ℕ → submodule R A) [graded_algebra 𝒜] /-- The projective spectrum of a graded commutative ring is the subtype of all homogenous ideals that are prime and do not contain the irrelevant ideal. -/ @[nolint has_inhabited_instance] def projective_spectrum := {I : homogeneous_ideal 𝒜 // I.to_ideal.is_prime ∧ ¬(homogeneous_ideal.irrelevant 𝒜 ≤ I)} namespace projective_spectrum variable {𝒜} /-- A method to view a point in the projective spectrum of a graded ring as a homogeneous ideal of that ring. -/ abbreviation as_homogeneous_ideal (x : projective_spectrum 𝒜) : homogeneous_ideal 𝒜 := x.1 lemma as_homogeneous_ideal_def (x : projective_spectrum 𝒜) : x.as_homogeneous_ideal = x.1 := rfl instance is_prime (x : projective_spectrum 𝒜) : x.as_homogeneous_ideal.to_ideal.is_prime := x.2.1 @[ext] lemma ext {x y : projective_spectrum 𝒜} : x = y ↔ x.as_homogeneous_ideal = y.as_homogeneous_ideal := subtype.ext_iff_val variable (𝒜) /-- The zero locus of a set `s` of elements of a commutative ring `A` is the set of all relevant homogeneous prime ideals of the ring that contain the set `s`. An element `f` of `A` can be thought of as a dependent function on the projective spectrum of `𝒜`. At a point `x` (a homogeneous prime ideal) the function (i.e., element) `f` takes values in the quotient ring `A` modulo the prime ideal `x`. In this manner, `zero_locus s` is exactly the subset of `projective_spectrum 𝒜` where all "functions" in `s` vanish simultaneously. -/ def zero_locus (s : set A) : set (projective_spectrum 𝒜) := {x | s ⊆ x.as_homogeneous_ideal} @[simp] lemma mem_zero_locus (x : projective_spectrum 𝒜) (s : set A) : x ∈ zero_locus 𝒜 s ↔ s ⊆ x.as_homogeneous_ideal := iff.rfl @[simp] lemma zero_locus_span (s : set A) : zero_locus 𝒜 (ideal.span s) = zero_locus 𝒜 s := by { ext x, exact (submodule.gi _ _).gc s x.as_homogeneous_ideal.to_ideal } variable {𝒜} /-- The vanishing ideal of a set `t` of points of the prime spectrum of a commutative ring `R` is the intersection of all the prime ideals in the set `t`. An element `f` of `A` can be thought of as a dependent function on the projective spectrum of `𝒜`. At a point `x` (a homogeneous prime ideal) the function (i.e., element) `f` takes values in the quotient ring `A` modulo the prime ideal `x`. In this manner, `vanishing_ideal t` is exactly the ideal of `A` consisting of all "functions" that vanish on all of `t`. -/ def vanishing_ideal (t : set (projective_spectrum 𝒜)) : homogeneous_ideal 𝒜 := ⨅ (x : projective_spectrum 𝒜) (h : x ∈ t), x.as_homogeneous_ideal lemma coe_vanishing_ideal (t : set (projective_spectrum 𝒜)) : (vanishing_ideal t : set A) = {f | ∀ x : projective_spectrum 𝒜, x ∈ t → f ∈ x.as_homogeneous_ideal} := begin ext f, rw [vanishing_ideal, set_like.mem_coe, ← homogeneous_ideal.mem_iff, homogeneous_ideal.to_ideal_infi, submodule.mem_infi], apply forall_congr (λ x, _), rw [homogeneous_ideal.to_ideal_infi, submodule.mem_infi, homogeneous_ideal.mem_iff], end lemma mem_vanishing_ideal (t : set (projective_spectrum 𝒜)) (f : A) : f ∈ vanishing_ideal t ↔ ∀ x : projective_spectrum 𝒜, x ∈ t → f ∈ x.as_homogeneous_ideal := by rw [← set_like.mem_coe, coe_vanishing_ideal, set.mem_set_of_eq] @[simp] lemma vanishing_ideal_singleton (x : projective_spectrum 𝒜) : vanishing_ideal ({x} : set (projective_spectrum 𝒜)) = x.as_homogeneous_ideal := by simp [vanishing_ideal] lemma subset_zero_locus_iff_le_vanishing_ideal (t : set (projective_spectrum 𝒜)) (I : ideal A) : t ⊆ zero_locus 𝒜 I ↔ I ≤ (vanishing_ideal t).to_ideal := ⟨λ h f k, (mem_vanishing_ideal _ _).mpr (λ x j, (mem_zero_locus _ _ _).mpr (h j) k), λ h, λ x j, (mem_zero_locus _ _ _).mpr (le_trans h (λ f h, ((mem_vanishing_ideal _ _).mp h) x j))⟩ variable (𝒜) /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc_ideal : @galois_connection (ideal A) (set (projective_spectrum 𝒜))ᵒᵈ _ _ (λ I, zero_locus 𝒜 I) (λ t, (vanishing_ideal t).to_ideal) := λ I t, subset_zero_locus_iff_le_vanishing_ideal t I /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc_set : @galois_connection (set A) (set (projective_spectrum 𝒜))ᵒᵈ _ _ (λ s, zero_locus 𝒜 s) (λ t, vanishing_ideal t) := have ideal_gc : galois_connection (ideal.span) coe := (submodule.gi A _).gc, by simpa [zero_locus_span, function.comp] using galois_connection.compose ideal_gc (gc_ideal 𝒜) lemma gc_homogeneous_ideal : @galois_connection (homogeneous_ideal 𝒜) (set (projective_spectrum 𝒜))ᵒᵈ _ _ (λ I, zero_locus 𝒜 I) (λ t, (vanishing_ideal t)) := λ I t, by simpa [show I.to_ideal ≤ (vanishing_ideal t).to_ideal ↔ I ≤ (vanishing_ideal t), from iff.rfl] using subset_zero_locus_iff_le_vanishing_ideal t I.to_ideal lemma subset_zero_locus_iff_subset_vanishing_ideal (t : set (projective_spectrum 𝒜)) (s : set A) : t ⊆ zero_locus 𝒜 s ↔ s ⊆ vanishing_ideal t := (gc_set _) s t lemma subset_vanishing_ideal_zero_locus (s : set A) : s ⊆ vanishing_ideal (zero_locus 𝒜 s) := (gc_set _).le_u_l s lemma ideal_le_vanishing_ideal_zero_locus (I : ideal A) : I ≤ (vanishing_ideal (zero_locus 𝒜 I)).to_ideal := (gc_ideal _).le_u_l I lemma homogeneous_ideal_le_vanishing_ideal_zero_locus (I : homogeneous_ideal 𝒜) : I ≤ vanishing_ideal (zero_locus 𝒜 I) := (gc_homogeneous_ideal _).le_u_l I lemma subset_zero_locus_vanishing_ideal (t : set (projective_spectrum 𝒜)) : t ⊆ zero_locus 𝒜 (vanishing_ideal t) := (gc_ideal _).l_u_le t lemma zero_locus_anti_mono {s t : set A} (h : s ⊆ t) : zero_locus 𝒜 t ⊆ zero_locus 𝒜 s := (gc_set _).monotone_l h lemma zero_locus_anti_mono_ideal {s t : ideal A} (h : s ≤ t) : zero_locus 𝒜 (t : set A) ⊆ zero_locus 𝒜 (s : set A) := (gc_ideal _).monotone_l h lemma zero_locus_anti_mono_homogeneous_ideal {s t : homogeneous_ideal 𝒜} (h : s ≤ t) : zero_locus 𝒜 (t : set A) ⊆ zero_locus 𝒜 (s : set A) := (gc_homogeneous_ideal _).monotone_l h lemma vanishing_ideal_anti_mono {s t : set (projective_spectrum 𝒜)} (h : s ⊆ t) : vanishing_ideal t ≤ vanishing_ideal s := (gc_ideal _).monotone_u h lemma zero_locus_bot : zero_locus 𝒜 ((⊥ : ideal A) : set A) = set.univ := (gc_ideal 𝒜).l_bot @[simp] lemma zero_locus_singleton_zero : zero_locus 𝒜 ({0} : set A) = set.univ := zero_locus_bot _ @[simp] lemma zero_locus_empty : zero_locus 𝒜 (∅ : set A) = set.univ := (gc_set 𝒜).l_bot @[simp] lemma vanishing_ideal_univ : vanishing_ideal (∅ : set (projective_spectrum 𝒜)) = ⊤ := by simpa using (gc_ideal _).u_top lemma zero_locus_empty_of_one_mem {s : set A} (h : (1:A) ∈ s) : zero_locus 𝒜 s = ∅ := set.eq_empty_iff_forall_not_mem.mpr $ λ x hx, (infer_instance : x.as_homogeneous_ideal.to_ideal.is_prime).ne_top $ x.as_homogeneous_ideal.to_ideal.eq_top_iff_one.mpr $ hx h @[simp] lemma zero_locus_singleton_one : zero_locus 𝒜 ({1} : set A) = ∅ := zero_locus_empty_of_one_mem 𝒜 (set.mem_singleton (1 : A)) @[simp] lemma zero_locus_univ : zero_locus 𝒜 (set.univ : set A) = ∅ := zero_locus_empty_of_one_mem _ (set.mem_univ 1) lemma zero_locus_sup_ideal (I J : ideal A) : zero_locus 𝒜 ((I ⊔ J : ideal A) : set A) = zero_locus _ I ∩ zero_locus _ J := (gc_ideal 𝒜).l_sup lemma zero_locus_sup_homogeneous_ideal (I J : homogeneous_ideal 𝒜) : zero_locus 𝒜 ((I ⊔ J : homogeneous_ideal 𝒜) : set A) = zero_locus _ I ∩ zero_locus _ J := (gc_homogeneous_ideal 𝒜).l_sup lemma zero_locus_union (s s' : set A) : zero_locus 𝒜 (s ∪ s') = zero_locus _ s ∩ zero_locus _ s' := (gc_set 𝒜).l_sup lemma vanishing_ideal_union (t t' : set (projective_spectrum 𝒜)) : vanishing_ideal (t ∪ t') = vanishing_ideal t ⊓ vanishing_ideal t' := by ext1; convert (gc_ideal 𝒜).u_inf lemma zero_locus_supr_ideal {γ : Sort*} (I : γ → ideal A) : zero_locus _ ((⨆ i, I i : ideal A) : set A) = (⋂ i, zero_locus 𝒜 (I i)) := (gc_ideal 𝒜).l_supr lemma zero_locus_supr_homogeneous_ideal {γ : Sort*} (I : γ → homogeneous_ideal 𝒜) : zero_locus _ ((⨆ i, I i : homogeneous_ideal 𝒜) : set A) = (⋂ i, zero_locus 𝒜 (I i)) := (gc_homogeneous_ideal 𝒜).l_supr lemma zero_locus_Union {γ : Sort*} (s : γ → set A) : zero_locus 𝒜 (⋃ i, s i) = (⋂ i, zero_locus 𝒜 (s i)) := (gc_set 𝒜).l_supr lemma zero_locus_bUnion (s : set (set A)) : zero_locus 𝒜 (⋃ s' ∈ s, s' : set A) = ⋂ s' ∈ s, zero_locus 𝒜 s' := by simp only [zero_locus_Union] lemma vanishing_ideal_Union {γ : Sort*} (t : γ → set (projective_spectrum 𝒜)) : vanishing_ideal (⋃ i, t i) = (⨅ i, vanishing_ideal (t i)) := homogeneous_ideal.to_ideal_injective $ by convert (gc_ideal 𝒜).u_infi; exact homogeneous_ideal.to_ideal_infi _ lemma zero_locus_inf (I J : ideal A) : zero_locus 𝒜 ((I ⊓ J : ideal A) : set A) = zero_locus 𝒜 I ∪ zero_locus 𝒜 J := set.ext $ λ x, by simpa using x.2.1.inf_le lemma union_zero_locus (s s' : set A) : zero_locus 𝒜 s ∪ zero_locus 𝒜 s' = zero_locus 𝒜 ((ideal.span s) ⊓ (ideal.span s'): ideal A) := by { rw zero_locus_inf, simp } lemma zero_locus_mul_ideal (I J : ideal A) : zero_locus 𝒜 ((I * J : ideal A) : set A) = zero_locus 𝒜 I ∪ zero_locus 𝒜 J := set.ext $ λ x, by simpa using x.2.1.mul_le lemma zero_locus_mul_homogeneous_ideal (I J : homogeneous_ideal 𝒜) : zero_locus 𝒜 ((I * J : homogeneous_ideal 𝒜) : set A) = zero_locus 𝒜 I ∪ zero_locus 𝒜 J := set.ext $ λ x, by simpa using x.2.1.mul_le lemma zero_locus_singleton_mul (f g : A) : zero_locus 𝒜 ({f * g} : set A) = zero_locus 𝒜 {f} ∪ zero_locus 𝒜 {g} := set.ext $ λ x, by simpa using x.2.1.mul_mem_iff_mem_or_mem @[simp] lemma zero_locus_singleton_pow (f : A) (n : ℕ) (hn : 0 < n) : zero_locus 𝒜 ({f ^ n} : set A) = zero_locus 𝒜 {f} := set.ext $ λ x, by simpa using x.2.1.pow_mem_iff_mem n hn lemma sup_vanishing_ideal_le (t t' : set (projective_spectrum 𝒜)) : vanishing_ideal t ⊔ vanishing_ideal t' ≤ vanishing_ideal (t ∩ t') := begin intros r, rw [← homogeneous_ideal.mem_iff, homogeneous_ideal.to_ideal_sup, mem_vanishing_ideal, submodule.mem_sup], rintro ⟨f, hf, g, hg, rfl⟩ x ⟨hxt, hxt'⟩, erw mem_vanishing_ideal at hf hg, apply submodule.add_mem; solve_by_elim end lemma mem_compl_zero_locus_iff_not_mem {f : A} {I : projective_spectrum 𝒜} : I ∈ (zero_locus 𝒜 {f} : set (projective_spectrum 𝒜))ᶜ ↔ f ∉ I.as_homogeneous_ideal := by rw [set.mem_compl_eq, mem_zero_locus, set.singleton_subset_iff]; refl /-- The Zariski topology on the prime spectrum of a commutative ring is defined via the closed sets of the topology: they are exactly those sets that are the zero locus of a subset of the ring. -/ instance zariski_topology : topological_space (projective_spectrum 𝒜) := topological_space.of_closed (set.range (projective_spectrum.zero_locus 𝒜)) (⟨set.univ, by simp⟩) begin intros Zs h, rw set.sInter_eq_Inter, let f : Zs → set _ := λ i, classical.some (h i.2), have hf : ∀ i : Zs, ↑i = zero_locus 𝒜 (f i) := λ i, (classical.some_spec (h i.2)).symm, simp only [hf], exact ⟨_, zero_locus_Union 𝒜 _⟩ end (by { rintros _ ⟨s, rfl⟩ _ ⟨t, rfl⟩, exact ⟨_, (union_zero_locus 𝒜 s t).symm⟩ }) /-- The underlying topology of `Proj` is the projective spectrum of graded ring `A`. -/ def Top : Top := Top.of (projective_spectrum 𝒜) lemma is_open_iff (U : set (projective_spectrum 𝒜)) : is_open U ↔ ∃ s, Uᶜ = zero_locus 𝒜 s := by simp only [@eq_comm _ Uᶜ]; refl lemma is_closed_iff_zero_locus (Z : set (projective_spectrum 𝒜)) : is_closed Z ↔ ∃ s, Z = zero_locus 𝒜 s := by rw [← is_open_compl_iff, is_open_iff, compl_compl] lemma is_closed_zero_locus (s : set A) : is_closed (zero_locus 𝒜 s) := by { rw [is_closed_iff_zero_locus], exact ⟨s, rfl⟩ } lemma zero_locus_vanishing_ideal_eq_closure (t : set (projective_spectrum 𝒜)) : zero_locus 𝒜 (vanishing_ideal t : set A) = closure t := begin apply set.subset.antisymm, { rintro x hx t' ⟨ht', ht⟩, obtain ⟨fs, rfl⟩ : ∃ s, t' = zero_locus 𝒜 s, by rwa [is_closed_iff_zero_locus] at ht', rw [subset_zero_locus_iff_subset_vanishing_ideal] at ht, exact set.subset.trans ht hx }, { rw (is_closed_zero_locus _ _).closure_subset_iff, exact subset_zero_locus_vanishing_ideal 𝒜 t } end lemma vanishing_ideal_closure (t : set (projective_spectrum 𝒜)) : vanishing_ideal (closure t) = vanishing_ideal t := begin have := (gc_ideal 𝒜).u_l_u_eq_u t, dsimp only at this, ext1, erw zero_locus_vanishing_ideal_eq_closure 𝒜 t at this, exact this, end section basic_open /-- `basic_open r` is the open subset containing all prime ideals not containing `r`. -/ def basic_open (r : A) : topological_space.opens (projective_spectrum 𝒜) := { val := { x | r ∉ x.as_homogeneous_ideal }, property := ⟨{r}, set.ext $ λ x, set.singleton_subset_iff.trans $ not_not.symm⟩ } @[simp] lemma mem_basic_open (f : A) (x : projective_spectrum 𝒜) : x ∈ basic_open 𝒜 f ↔ f ∉ x.as_homogeneous_ideal := iff.rfl lemma mem_coe_basic_open (f : A) (x : projective_spectrum 𝒜) : x ∈ (↑(basic_open 𝒜 f): set (projective_spectrum 𝒜)) ↔ f ∉ x.as_homogeneous_ideal := iff.rfl lemma is_open_basic_open {a : A} : is_open ((basic_open 𝒜 a) : set (projective_spectrum 𝒜)) := (basic_open 𝒜 a).property @[simp] lemma basic_open_eq_zero_locus_compl (r : A) : (basic_open 𝒜 r : set (projective_spectrum 𝒜)) = (zero_locus 𝒜 {r})ᶜ := set.ext $ λ x, by simpa only [set.mem_compl_eq, mem_zero_locus, set.singleton_subset_iff] @[simp] lemma basic_open_one : basic_open 𝒜 (1 : A) = ⊤ := topological_space.opens.ext $ by simp @[simp] lemma basic_open_zero : basic_open 𝒜 (0 : A) = ⊥ := topological_space.opens.ext $ by simp lemma basic_open_mul (f g : A) : basic_open 𝒜 (f * g) = basic_open 𝒜 f ⊓ basic_open 𝒜 g := topological_space.opens.ext $ by {simp [zero_locus_singleton_mul]} lemma basic_open_mul_le_left (f g : A) : basic_open 𝒜 (f * g) ≤ basic_open 𝒜 f := by { rw basic_open_mul 𝒜 f g, exact inf_le_left } lemma basic_open_mul_le_right (f g : A) : basic_open 𝒜 (f * g) ≤ basic_open 𝒜 g := by { rw basic_open_mul 𝒜 f g, exact inf_le_right } @[simp] lemma basic_open_pow (f : A) (n : ℕ) (hn : 0 < n) : basic_open 𝒜 (f ^ n) = basic_open 𝒜 f := topological_space.opens.ext $ by simpa using zero_locus_singleton_pow 𝒜 f n hn lemma basic_open_eq_union_of_projection (f : A) : basic_open 𝒜 f = ⨆ (i : ℕ), basic_open 𝒜 (graded_algebra.proj 𝒜 i f) := topological_space.opens.ext $ set.ext $ λ z, begin erw [mem_coe_basic_open, topological_space.opens.mem_Sup], split; intros hz, { rcases show ∃ i, graded_algebra.proj 𝒜 i f ∉ z.as_homogeneous_ideal, begin contrapose! hz with H, classical, rw ←direct_sum.sum_support_decompose 𝒜 f, apply ideal.sum_mem _ (λ i hi, H i) end with ⟨i, hi⟩, exact ⟨basic_open 𝒜 (graded_algebra.proj 𝒜 i f), ⟨i, rfl⟩, by rwa mem_basic_open⟩ }, { obtain ⟨_, ⟨i, rfl⟩, hz⟩ := hz, exact λ rid, hz (z.1.2 i rid) }, end lemma is_topological_basis_basic_opens : topological_space.is_topological_basis (set.range (λ (r : A), (basic_open 𝒜 r : set (projective_spectrum 𝒜)))) := begin apply topological_space.is_topological_basis_of_open_of_nhds, { rintros _ ⟨r, rfl⟩, exact is_open_basic_open 𝒜 }, { rintros p U hp ⟨s, hs⟩, rw [← compl_compl U, set.mem_compl_eq, ← hs, mem_zero_locus, set.not_subset] at hp, obtain ⟨f, hfs, hfp⟩ := hp, refine ⟨basic_open 𝒜 f, ⟨f, rfl⟩, hfp, _⟩, rw [← set.compl_subset_compl, ← hs, basic_open_eq_zero_locus_compl, compl_compl], exact zero_locus_anti_mono 𝒜 (set.singleton_subset_iff.mpr hfs) } end end basic_open section order /-! ## The specialization order We endow `projective_spectrum 𝒜` with a partial order, where `x ≤ y` if and only if `y ∈ closure {x}`. -/ instance : partial_order (projective_spectrum 𝒜) := subtype.partial_order _ @[simp] lemma as_ideal_le_as_ideal (x y : projective_spectrum 𝒜) : x.as_homogeneous_ideal ≤ y.as_homogeneous_ideal ↔ x ≤ y := subtype.coe_le_coe @[simp] lemma as_ideal_lt_as_ideal (x y : projective_spectrum 𝒜) : x.as_homogeneous_ideal < y.as_homogeneous_ideal ↔ x < y := subtype.coe_lt_coe lemma le_iff_mem_closure (x y : projective_spectrum 𝒜) : x ≤ y ↔ y ∈ closure ({x} : set (projective_spectrum 𝒜)) := begin rw [← as_ideal_le_as_ideal, ← zero_locus_vanishing_ideal_eq_closure, mem_zero_locus, vanishing_ideal_singleton], simp only [coe_subset_coe, subtype.coe_le_coe, coe_coe], end end order end projective_spectrum
b9d90bcff9d90a334e940b98dbef16357ce54af4
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/control/reader.lean
fe4b34ff9fe11b2c827329921dbcb9a75a233aa7
[ "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
5,008
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich The reader monad transformer for passing immutable state. -/ prelude import init.control.lift init.control.id init.control.alternative init.control.except universes u v w /-- An implementation of [ReaderT](https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Reader.html#t:ReaderT) -/ structure reader_t (ρ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) := (run : ρ → m α) @[reducible] def reader (ρ : Type u) := reader_t ρ id attribute [pp_using_anonymous_constructor] reader_t namespace reader_t section variable {ρ : Type u} variable {m : Type u → Type v} variable [monad m] variables {α β : Type u} @[inline] protected def read : reader_t ρ m ρ := ⟨pure⟩ @[inline] protected def pure (a : α) : reader_t ρ m α := ⟨λ r, pure a⟩ @[inline] protected def bind (x : reader_t ρ m α) (f : α → reader_t ρ m β) : reader_t ρ m β := ⟨λ r, do a ← x.run r, (f a).run r⟩ instance : monad (reader_t ρ m) := { pure := @reader_t.pure _ _ _, bind := @reader_t.bind _ _ _ } @[inline] protected def lift (a : m α) : reader_t ρ m α := ⟨λ r, a⟩ instance (m) [monad m] : has_monad_lift m (reader_t ρ m) := ⟨@reader_t.lift ρ m _⟩ @[inline] protected def monad_map {ρ m m'} [monad m] [monad m'] {α} (f : Π {α}, m α → m' α) : reader_t ρ m α → reader_t ρ m' α := λ x, ⟨λ r, f (x.run r)⟩ instance (ρ m m') [monad m] [monad m'] : monad_functor m m' (reader_t ρ m) (reader_t ρ m') := ⟨@reader_t.monad_map ρ m m' _ _⟩ @[inline] protected def adapt {ρ' : Type u} [monad m] {α : Type u} (f : ρ' → ρ) : reader_t ρ m α → reader_t ρ' m α := λ x, ⟨λ r, x.run (f r)⟩ protected def orelse [alternative m] {α : Type u} (x₁ x₂ : reader_t ρ m α) : reader_t ρ m α := ⟨λ s, x₁.run s <|> x₂.run s⟩ protected def failure [alternative m] {α : Type u} : reader_t ρ m α := ⟨λ s, failure⟩ instance [alternative m] : alternative (reader_t ρ m) := { failure := @reader_t.failure _ _ _ _, orelse := @reader_t.orelse _ _ _ _ } instance (ε) [monad m] [monad_except ε m] : monad_except ε (reader_t ρ m) := { throw := λ α, reader_t.lift ∘ throw, catch := λ α x c, ⟨λ r, catch (x.run r) (λ e, (c e).run r)⟩ } end end reader_t /-- An implementation of [MonadReader](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Reader-Class.html#t:MonadReader). It does not contain `local` because this function cannot be lifted using `monad_lift`. Instead, the `monad_reader_adapter` class provides the more general `adapt_reader` function. Note: This class can be seen as a simplification of the more "principled" definition ``` class monad_reader (ρ : out_param (Type u)) (n : Type u → Type u) := (lift {α : Type u} : (∀ {m : Type u → Type u} [monad m], reader_t ρ m α) → n α) ``` -/ class monad_reader (ρ : out_param (Type u)) (m : Type u → Type v) := (read : m ρ) export monad_reader (read) @[priority 100] instance monad_reader_trans {ρ : Type u} {m : Type u → Type v} {n : Type u → Type w} [monad_reader ρ m] [has_monad_lift m n] : monad_reader ρ n := ⟨monad_lift (monad_reader.read : m ρ)⟩ instance {ρ : Type u} {m : Type u → Type v} [monad m] : monad_reader ρ (reader_t ρ m) := ⟨reader_t.read⟩ /-- Adapt a monad stack, changing the type of its top-most environment. This class is comparable to [Control.Lens.Magnify](https://hackage.haskell.org/package/lens-4.15.4/docs/Control-Lens-Zoom.html#t:Magnify), but does not use lenses (why would it), and is derived automatically for any transformer implementing `monad_functor`. Note: This class can be seen as a simplification of the more "principled" definition ``` class monad_reader_functor (ρ ρ' : out_param (Type u)) (n n' : Type u → Type u) := (map {α : Type u} : (∀ {m : Type u → Type u} [monad m], reader_t ρ m α → reader_t ρ' m α) → n α → n' α) ``` -/ class monad_reader_adapter (ρ ρ' : out_param (Type u)) (m m' : Type u → Type v) := (adapt_reader {α : Type u} : (ρ' → ρ) → m α → m' α) export monad_reader_adapter (adapt_reader) section variables {ρ ρ' : Type u} {m m' : Type u → Type v} @[priority 100] instance monad_reader_adapter_trans {n n' : Type u → Type v} [monad_reader_adapter ρ ρ' m m'] [monad_functor m m' n n'] : monad_reader_adapter ρ ρ' n n' := ⟨λ α f, monad_map (λ α, (adapt_reader f : m α → m' α))⟩ instance [monad m] : monad_reader_adapter ρ ρ' (reader_t ρ m) (reader_t ρ' m) := ⟨λ α, reader_t.adapt⟩ end instance (ρ : Type u) (m out) [monad_run out m] : monad_run (λ α, ρ → out α) (reader_t ρ m) := ⟨λ α x, run ∘ x.run⟩
d1263e66b91923d8aa563a97d67ab99d62c389e9
c3de33d4701e6113627153fe1103b255e752ed7d
/theories/set_theory.lean
59fb6f165161c3b1dc289bdb0264cc5f414dffde
[]
no_license
jroesch/library_dev
77d2b246ff47ab05d55cb9706a37d3de97038388
4faa0a45c6aa7eee6e661113c2072b8840bff79b
refs/heads/master
1,611,281,606,352
1,495,661,644,000
1,495,661,644,000
92,340,430
0
0
null
1,495,663,344,000
1,495,663,344,000
null
UTF-8
Lean
false
false
27,875
lean
import data.set.basic universes u v @[simp] lemma subtype.eta {α : Type u} {p : α → Prop} {a : subtype p} {h : p (a.val)} : {subtype . val := a.val, property := h} = a := by cases a; refl def arity (α : Type u) : nat → Type u | 0 := α | (n+1) := α → arity n inductive pSet : Type (u+1) | mk (α : Type u) (A : α → pSet) : pSet namespace pSet def type : pSet → Type u | ⟨α, A⟩ := α def func : Π (x : pSet), x.type → pSet | ⟨α, A⟩ := A def mk_type_func : Π (x : pSet), mk x.type x.func = x | ⟨α, A⟩ := rfl def equiv (x y : pSet) : Prop := pSet.rec (λα z m ⟨β, B⟩, (∀a, ∃b, m a (B b)) ∧ (∀b, ∃a, m a (B b))) x y def equiv.refl (x) : equiv x x := pSet.rec_on x $ λα A IH, ⟨λa, ⟨a, IH a⟩, λa, ⟨a, IH a⟩⟩ def equiv.euc {x} : Π {y z}, equiv x y → equiv z y → equiv x z := pSet.rec_on x $ λα A IH y, pSet.rec_on y $ λβ B _ ⟨γ, Γ⟩ ⟨αβ, βα⟩ ⟨γβ, βγ⟩, ⟨λa, let ⟨b, ab⟩ := αβ a, ⟨c, bc⟩ := βγ b in ⟨c, IH a ab bc⟩, λc, let ⟨b, cb⟩ := γβ c, ⟨a, ba⟩ := βα b in ⟨a, IH a ba cb⟩⟩ def equiv.symm {x y} : equiv x y → equiv y x := equiv.euc (equiv.refl y) def equiv.trans {x y z} (h1 : equiv x y) (h2 : equiv y z) : equiv x z := equiv.euc h1 (equiv.symm h2) instance setoid : setoid pSet := ⟨pSet.equiv, equiv.refl, λx y, equiv.symm, λx y z, equiv.trans⟩ protected def subset : pSet → pSet → Prop | ⟨α, A⟩ ⟨β, B⟩ := ∀a, ∃b, equiv (A a) (B b) instance : has_subset pSet := ⟨pSet.subset⟩ def equiv.ext : Π (x y : pSet), equiv x y ↔ (x ⊆ y ∧ y ⊆ x) | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ⟨αβ, βα⟩, ⟨αβ, λb, let ⟨a, h⟩ := βα b in ⟨a, equiv.symm h⟩⟩, λ⟨αβ, βα⟩, ⟨αβ, λb, let ⟨a, h⟩ := βα b in ⟨a, equiv.symm h⟩⟩⟩ def subset.congr_left : Π {x y z : pSet}, equiv x y → (x ⊆ z ↔ y ⊆ z) | ⟨α, A⟩ ⟨β, B⟩ ⟨γ, Γ⟩ ⟨αβ, βα⟩ := ⟨λαγ b, let ⟨a, ba⟩ := βα b, ⟨c, ac⟩ := αγ a in ⟨c, equiv.trans (equiv.symm ba) ac⟩, λβγ a, let ⟨b, ab⟩ := αβ a, ⟨c, bc⟩ := βγ b in ⟨c, equiv.trans ab bc⟩⟩ def subset.congr_right : Π {x y z : pSet}, equiv x y → (z ⊆ x ↔ z ⊆ y) | ⟨α, A⟩ ⟨β, B⟩ ⟨γ, Γ⟩ ⟨αβ, βα⟩ := ⟨λγα c, let ⟨a, ca⟩ := γα c, ⟨b, ab⟩ := αβ a in ⟨b, equiv.trans ca ab⟩, λγβ c, let ⟨b, cb⟩ := γβ c, ⟨a, ab⟩ := βα b in ⟨a, equiv.trans cb (equiv.symm ab)⟩⟩ def mem : pSet → pSet → Prop | x ⟨β, B⟩ := ∃b, equiv x (B b) instance : has_mem pSet.{u} pSet.{u} := ⟨mem⟩ def mem.mk {α: Type u} (A : α → pSet) (a : α) : A a ∈ mk α A := show mem (A a) ⟨α, A⟩, from ⟨a, equiv.refl (A a)⟩ def mem.ext : Π {x y : pSet.{u}}, (∀w:pSet.{u}, w ∈ x ↔ w ∈ y) → equiv x y | ⟨α, A⟩ ⟨β, B⟩ h := ⟨λa, (h (A a)).1 (mem.mk A a), λb, let ⟨a, ha⟩ := (h (B b)).2 (mem.mk B b) in ⟨a, equiv.symm ha⟩⟩ def mem.congr_right : Π {x y : pSet.{u}}, equiv x y → (∀{w:pSet.{u}}, w ∈ x ↔ w ∈ y) | ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩ w := ⟨λ⟨a, ha⟩, let ⟨b, hb⟩ := αβ a in ⟨b, equiv.trans ha hb⟩, λ⟨b, hb⟩, let ⟨a, ha⟩ := βα b in ⟨a, equiv.euc hb ha⟩⟩ def mem.congr_left : Π {x y : pSet.{u}}, equiv x y → (∀{w : pSet.{u}}, x ∈ w ↔ y ∈ w) | x y h ⟨α, A⟩ := ⟨λ⟨a, ha⟩, ⟨a, equiv.trans (equiv.symm h) ha⟩, λ⟨a, ha⟩, ⟨a, equiv.trans h ha⟩⟩ def to_set (u : pSet.{u}) : set pSet.{u} := {x | x ∈ u} def equiv.eq {x y : pSet} (h : equiv x y) : to_set x = to_set y := set.ext (λz, mem.congr_right h) instance : has_coe pSet (set pSet) := ⟨to_set⟩ protected def empty : pSet := ⟨ulift empty, λe, match e with end⟩ instance : has_emptyc pSet := ⟨pSet.empty⟩ def mem_empty (x : pSet.{u}) : x ∉ (∅:pSet.{u}) := λe, match e with end protected def insert : pSet → pSet → pSet | u ⟨α, A⟩ := ⟨option α, λo, option.rec u A o⟩ instance : has_insert pSet pSet := ⟨pSet.insert⟩ def of_nat : ℕ → pSet | 0 := ∅ | (n+1) := pSet.insert (of_nat n) (of_nat n) def omega : pSet := ⟨ulift ℕ, λn, of_nat n.down⟩ protected def sep (p : set pSet) : pSet → pSet | ⟨α, A⟩ := ⟨{a // p (A a)}, λx, A x.1⟩ instance : has_sep pSet pSet := ⟨pSet.sep⟩ def powerset : pSet → pSet | ⟨α, A⟩ := ⟨set α, λp, ⟨{a // p a}, λx, A x.1⟩⟩ theorem mem_powerset : Π {x y : pSet}, y ∈ powerset x ↔ y ⊆ x | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ⟨p, e⟩, (subset.congr_left e).2 $ λ⟨a, pa⟩, ⟨a, equiv.refl (A a)⟩, λβα, ⟨{a | ∃b, equiv (B b) (A a)}, λb, let ⟨a, ba⟩ := βα b in ⟨⟨a, b, ba⟩, ba⟩, λ⟨a, b, ba⟩, ⟨b, ba⟩⟩⟩ def Union : pSet → pSet | ⟨α, A⟩ := ⟨Σx, (A x).type, λ⟨x, y⟩, (A x).func y⟩ theorem mem_Union : Π {x y : pSet.{u}}, y ∈ Union x ↔ ∃ z:pSet.{u}, ∃_:z ∈ x, y ∈ z | ⟨α, A⟩ y := ⟨λ⟨⟨a, c⟩, (e : equiv y ((A a).func c))⟩, have func (A a) c ∈ mk (A a).type (A a).func, from mem.mk (A a).func c, ⟨_, mem.mk _ _, (mem.congr_left e).2 (by rwa mk_type_func at this)⟩, λ⟨⟨β, B⟩, ⟨a, (e:equiv (mk β B) (A a))⟩, ⟨b, yb⟩⟩, by rw -(mk_type_func (A a)) at e; exact let ⟨βt, tβ⟩ := e, ⟨c, bc⟩ := βt b in ⟨⟨a, c⟩, equiv.trans yb bc⟩⟩ def image (f : pSet.{u} → pSet.{u}) : pSet.{u} → pSet | ⟨α, A⟩ := ⟨α, λa, f (A a)⟩ def mem_image {f : pSet.{u} → pSet.{u}} (H : ∀{x y}, equiv x y → equiv (f x) (f y)) : Π {x y : pSet.{u}}, y ∈ image f x ↔ ∃z ∈ x, equiv y (f z) | ⟨α, A⟩ y := ⟨λ⟨a, ya⟩, ⟨A a, mem.mk A a, ya⟩, λ⟨z, ⟨a, za⟩, yz⟩, ⟨a, equiv.trans yz (H za)⟩⟩ protected def lift : pSet.{u} → pSet.{max u v} | ⟨α, A⟩ := ⟨ulift α, λ⟨x⟩, lift (A x)⟩ prefix ⇑ := pSet.lift def embed : pSet.{max u+1 v} := ⟨ulift.{v u+1} pSet, λ⟨x⟩, pSet.lift.{u (max u+1 v)} x⟩ def lift_mem_embed : Π (x : pSet.{u}), pSet.lift.{u (max u+1 v)} x ∈ embed.{u v} := λx, ⟨⟨x⟩, equiv.refl _⟩ def arity.equiv : Π {n}, arity pSet.{u} n → arity pSet.{u} n → Prop | 0 a b := equiv a b | (n+1) a b := ∀ x y, equiv x y → arity.equiv (a x) (b y) def resp (n) := { x : arity pSet.{u} n // arity.equiv x x } def resp.f {n} (f : resp (n+1)) (x : pSet) : resp n := ⟨f.1 x, f.2 _ _ $ equiv.refl x⟩ def resp.equiv {n} (a b : resp n) : Prop := arity.equiv a.1 b.1 def resp.refl {n} (a : resp n) : resp.equiv a a := a.2 def resp.euc : Π {n} {a b c : resp n}, resp.equiv a b → resp.equiv c b → resp.equiv a c | 0 a b c hab hcb := equiv.euc hab hcb | (n+1) a b c hab hcb := by delta resp.equiv; simp[arity.equiv]; exact λx y h, @resp.euc n (a.f x) (b.f y) (c.f y) (hab _ _ h) (hcb _ _ $ equiv.refl y) instance resp.setoid {n} : setoid (resp n) := ⟨resp.equiv, resp.refl, λx y h, resp.euc (resp.refl y) h, λx y z h1 h2, resp.euc h1 $ resp.euc (resp.refl z) h2⟩ end pSet def Set : Type (u+1) := quotient pSet.setoid.{u} namespace pSet namespace resp def eval_aux : Π {n}, { f : resp n → arity Set.{u} n // ∀ (a b : resp n), resp.equiv a b → f a = f b } | 0 := ⟨λa, ⟦a.1⟧, λa b h, quotient.sound h⟩ | (n+1) := let F : resp (n + 1) → arity Set (n + 1) := λa, @quotient.lift _ _ pSet.setoid (λx, eval_aux.1 (a.f x)) (λb c h, eval_aux.2 _ _ (a.2 _ _ h)) in ⟨F, λb c h, funext $ @quotient.ind _ _ (λq, F b q = F c q) $ λz, eval_aux.2 (resp.f b z) (resp.f c z) (h _ _ (equiv.refl z))⟩ def eval (n) : resp n → arity Set.{u} n := eval_aux.1 @[simp] def eval_val {n f x} : (@eval (n+1) f : Set → arity Set n) ⟦x⟧ = eval n (f.f x) := rfl end resp inductive definable (n) : arity Set.{u} n → Type (u+1) | mk (f) : definable (resp.eval _ f) attribute [class] definable attribute [instance] definable.mk def definable.eq_mk {n} (f) : Π {s : arity Set.{u} n} (H : resp.eval _ f = s), definable n s | ._ rfl := ⟨f⟩ def definable.resp {n} : Π (s : arity Set.{u} n) [definable n s], resp n | ._ ⟨f⟩ := f def definable.eq {n} : Π (s : arity Set.{u} n) [H : definable n s], (@definable.resp n s H).eval _ = s | ._ ⟨f⟩ := rfl end pSet namespace classical open pSet noncomputable def all_definable : Π {n} (F : arity Set.{u} n), definable n F | 0 F := let p := @quotient.exists_rep pSet _ F in definable.eq_mk ⟨some p, equiv.refl _⟩ (some_spec p) | (n+1) (F : Set → arity Set.{u} n) := begin note I := λx, (all_definable (F x)), refine definable.eq_mk ⟨λx:pSet, (@definable.resp _ _ (I ⟦x⟧)).1, _⟩ _, { dsimp[arity.equiv], intros x y h, rw @quotient.sound pSet _ _ _ h, exact (definable.resp (F ⟦y⟧)).2 }, exact funext (λq, quotient.induction_on q $ λx, by simp[resp.f]; exact @definable.eq _ (F ⟦x⟧) (I ⟦x⟧)) end local attribute [instance] prop_decidable end classical namespace Set open pSet def mem : Set → Set → Prop := quotient.lift₂ pSet.mem (λx y x' y' hx hy, propext (iff.trans (mem.congr_left hx) (mem.congr_right hy))) instance : has_mem Set Set := ⟨mem⟩ def to_set (u : Set.{u}) : set Set.{u} := {x | x ∈ u} protected def subset (x y : Set.{u}) := ∀ ⦃z:Set.{u}⦄, z ∈ x → z ∈ y instance has_subset : has_subset Set := ⟨Set.subset⟩ instance has_subset' : has_subset (quotient pSet.setoid) := Set.has_subset theorem subset_iff : Π (x y : pSet), ⟦x⟧ ⊆ ⟦y⟧ ↔ x ⊆ y | ⟨α, A⟩ ⟨β, B⟩ := ⟨λh a, @h ⟦A a⟧ (mem.mk A a), λh z, quotient.induction_on z (λz ⟨a, za⟩, let ⟨b, ab⟩ := h a in ⟨b, equiv.trans za ab⟩)⟩ def ext {x y : Set.{u}} : (∀z:Set.{u}, z ∈ x ↔ z ∈ y) → x = y := quotient.induction_on₂ x y (λu v h, quotient.sound (mem.ext (λw, h ⟦w⟧))) def ext_iff {x y : Set.{u}} : (∀z:Set.{u}, z ∈ x ↔ z ∈ y) ↔ x = y := ⟨ext, λh, by simp[h]⟩ def empty : Set := ⟦∅⟧ instance : has_emptyc Set.{u} := ⟨empty⟩ instance : inhabited Set := ⟨∅⟩ @[simp] def mem_empty (x : Set.{u}) : x ∉ (∅:Set.{u}) := quotient.induction_on x pSet.mem_empty def eq_empty (x : Set.{u}) : x = ∅ ↔ ∀y:Set.{u}, y ∉ x := ⟨λh, by rw h; exact mem_empty, λh, ext (λy, ⟨λyx, absurd yx (h y), λy0, absurd y0 (mem_empty _)⟩)⟩ protected def insert : Set.{u} → Set.{u} → Set.{u} := resp.eval 2 ⟨pSet.insert, λu v uv ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λo, match o with | some a := let ⟨b, hb⟩ := αβ a in ⟨some b, hb⟩ | none := ⟨none, uv⟩ end, λo, match o with | some b := let ⟨a, ha⟩ := βα b in ⟨some a, ha⟩ | none := ⟨none, uv⟩ end⟩⟩ instance : has_insert Set Set := ⟨Set.insert⟩ @[simp] def mem_insert {x y z : Set.{u}} : x ∈ insert y z ↔ (x = y ∨ x ∈ z) := quotient.induction_on₃ x y z (λx y ⟨α, A⟩, show x ∈ mk (option α) (λo, option.rec y A o) ↔ ⟦x⟧ = ⟦y⟧ ∨ x ∈ mk α A, from ⟨λm, match m with | ⟨some a, ha⟩ := or.inr ⟨a, ha⟩ | ⟨none, h⟩ := or.inl (quotient.sound h) end, λm, match m with | or.inr ⟨a, ha⟩ := ⟨some a, ha⟩ | or.inl h := ⟨none, quotient.exact h⟩ end⟩) @[simp] theorem mem_singleton {x y : Set.{u}} : x ∈ @singleton Set.{u} Set.{u} _ _ y ↔ x = y := iff.trans mem_insert ⟨λo, or.rec (λh, h) (λn, absurd n (mem_empty _)) o, or.inl⟩ @[simp] theorem mem_singleton' {x y : Set.{u}} : x ∈ @insert Set.{u} Set.{u} _ y ∅ ↔ x = y := mem_singleton -- It looks better when you print it, but I can't get the {y, z} notation to typecheck @[simp] theorem mem_pair {x y z : Set.{u}} : x ∈ (insert z (@insert Set.{u} Set.{u} _ y ∅)) ↔ (x = y ∨ x = z) := iff.trans mem_insert $ iff.trans or.comm $ let m := @mem_singleton x y in ⟨or.imp_left m.1, or.imp_left m.2⟩ def omega : Set := ⟦omega⟧ @[simp] theorem omega_zero : (∅:Set.{u}) ∈ omega.{u} := show pSet.mem ∅ pSet.omega, from ⟨⟨0⟩, equiv.refl _⟩ @[simp] theorem omega_succ {n : Set.{u}} : n ∈ omega.{u} → insert n n ∈ omega.{u} := quotient.induction_on n (λx ⟨⟨n⟩, (h : x ≈ of_nat n)⟩, ⟨⟨n+1⟩, have Set.insert ⟦x⟧ ⟦x⟧ = Set.insert ⟦of_nat n⟧ ⟦of_nat n⟧, by rw (@quotient.sound pSet _ _ _ h), quotient.exact this⟩) protected def sep (p : Set → Prop) : Set → Set := resp.eval 1 ⟨pSet.sep (λy, p ⟦y⟧), λ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λ⟨a, pa⟩, let ⟨b, hb⟩ := αβ a in ⟨⟨b, by rwa -(@quotient.sound pSet _ _ _ hb)⟩, hb⟩, λ⟨b, pb⟩, let ⟨a, ha⟩ := βα b in ⟨⟨a, by rwa (@quotient.sound pSet _ _ _ ha)⟩, ha⟩⟩⟩ instance : has_sep Set Set := ⟨Set.sep⟩ @[simp] theorem mem_sep {p : Set.{u} → Prop} {x y : Set.{u}} : y ∈ {y ∈ x | p y} ↔ (y ∈ x ∧ p y) := quotient.induction_on₂ x y (λ⟨α, A⟩ y, ⟨λ⟨⟨a, pa⟩, h⟩, ⟨⟨a, h⟩, by rw (@quotient.sound pSet _ _ _ h); exact pa⟩, λ⟨⟨a, h⟩, pa⟩, ⟨⟨a, by rw -(@quotient.sound pSet _ _ _ h); exact pa⟩, h⟩⟩) def powerset : Set → Set := resp.eval 1 ⟨powerset, λ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λp, ⟨{b | ∃a, p a ∧ equiv (A a) (B b)}, λ⟨a, pa⟩, let ⟨b, ab⟩ := αβ a in ⟨⟨b, a, pa, ab⟩, ab⟩, λ⟨b, a, pa, ab⟩, ⟨⟨a, pa⟩, ab⟩⟩, λq, ⟨{a | ∃b, q b ∧ equiv (A a) (B b)}, λ⟨a, b, qb, ab⟩, ⟨⟨b, qb⟩, ab⟩, λ⟨b, qb⟩, let ⟨a, ab⟩ := βα b in ⟨⟨a, b, qb, ab⟩, ab⟩⟩⟩⟩ @[simp] theorem mem_powerset {x y : Set} : y ∈ powerset x ↔ y ⊆ x := quotient.induction_on₂ x y (λ⟨α, A⟩ ⟨β, B⟩, show (⟨β, B⟩ : pSet) ∈ (pSet.powerset ⟨α, A⟩) ↔ _, by rw [mem_powerset, subset_iff]) theorem Union_lem {α β : Type u} (A : α → pSet) (B : β → pSet) (αβ : ∀a, ∃b, equiv (A a) (B b)) : ∀a, ∃b, (equiv ((Union ⟨α, A⟩).func a) ((Union ⟨β, B⟩).func b)) | ⟨a, c⟩ := let ⟨b, hb⟩ := αβ a in begin ginduction A a with ea γ Γ, ginduction B b with eb δ Δ, rw [ea, eb] at hb, cases hb with γδ δγ, exact let c : type (A a) := c, ⟨d, hd⟩ := γδ (by rwa ea at c) in have equiv ((A a).func c) ((B b).func (eq.rec d (eq.symm eb))), from match A a, B b, ea, eb, c, d, hd with ._, ._, rfl, rfl, x, y, hd := hd end, ⟨⟨b, eq.rec d (eq.symm eb)⟩, this⟩ end def Union : Set → Set := resp.eval 1 ⟨pSet.Union, λ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨Union_lem A B αβ, λa, exists.elim (Union_lem B A (λb, exists.elim (βα b) (λc hc, ⟨c, equiv.symm hc⟩)) a) (λb hb, ⟨b, equiv.symm hb⟩)⟩⟩ notation `⋃` := Union @[simp] theorem mem_Union {x y : Set.{u}} : y ∈ Union x ↔ ∃ z:Set.{u}, ∃_:z ∈ x, y ∈ z := quotient.induction_on₂ x y (λx y, iff.trans mem_Union ⟨λ⟨z, h⟩, ⟨⟦z⟧, h⟩, λ⟨z, h⟩, quotient.induction_on z (λz h, ⟨z, h⟩) h⟩) @[simp] theorem Union_singleton {x : Set.{u}} : Union (@insert Set.{u} _ _ x ∅) = x := ext $ λy, by simp; exact ⟨λ⟨z, zx, yz⟩, by simp at zx; simp[zx] at yz; exact yz, λyx, ⟨x, by simp, yx⟩⟩ theorem singleton_inj {x y : Set.{u}} (H : @insert Set.{u} Set.{u} _ x ∅ = @insert Set _ _ y ∅) : x = y := let this := congr_arg Union H in by rwa [Union_singleton, Union_singleton] at this protected def union (x y : Set.{u}) : Set.{u} := -- ⋃ {x, y} Set.Union (@insert Set _ _ y (insert x ∅)) protected def inter (x y : Set.{u}) : Set.{u} := -- {z ∈ x | z ∈ y} Set.sep (λz, z ∈ y) x protected def diff (x y : Set.{u}) : Set.{u} := -- {z ∈ x | z ∉ y} Set.sep (λz, z ∉ y) x instance : has_union Set := ⟨Set.union⟩ instance : has_inter Set := ⟨Set.inter⟩ instance : has_sdiff Set := ⟨Set.diff⟩ @[simp] theorem mem_union {x y z : Set.{u}} : z ∈ x ∪ y ↔ (z ∈ x ∨ z ∈ y) := iff.trans mem_Union ⟨λ⟨w, wxy, zw⟩, match mem_pair.1 wxy with | or.inl wx := or.inl (by rwa -wx) | or.inr wy := or.inr (by rwa -wy) end, λzxy, match zxy with | or.inl zx := ⟨x, mem_pair.2 (or.inl rfl), zx⟩ | or.inr zy := ⟨y, mem_pair.2 (or.inr rfl), zy⟩ end⟩ @[simp] theorem mem_inter {x y z : Set.{u}} : z ∈ x ∩ y ↔ (z ∈ x ∧ z ∈ y) := mem_sep @[simp] theorem mem_diff {x y z : Set.{u}} : z ∈ x \ y ↔ (z ∈ x ∧ z ∉ y) := mem_sep theorem induction_on {p : Set → Prop} (x) (h : ∀x, (∀y ∈ x, p y) → p x) : p x := quotient.induction_on x $ λu, pSet.rec_on u $ λα A IH, h _ $ λy, show @has_mem.mem _ _ Set.has_mem y ⟦⟨α, A⟩⟧ → p y, from quotient.induction_on y (λv ⟨a, ha⟩, by rw (@quotient.sound pSet _ _ _ ha); exact IH a) theorem regularity (x : Set.{u}) (h : x ≠ ∅) : ∃ y ∈ x, x ∩ y = ∅ := classical.by_contradiction $ λne, h $ (eq_empty x).2 $ λy, induction_on y $ λz (IH : ∀w:Set.{u}, w ∈ z → w ∉ x), show z ∉ x, from λzx, ne ⟨z, zx, (eq_empty _).2 (λw wxz, let ⟨wx, wz⟩ := mem_inter.1 wxz in IH w wz wx)⟩ def image (f : Set → Set) [H : definable 1 f] : Set → Set := let r := @definable.resp 1 f _ in resp.eval 1 ⟨image r.1, λx y e, mem.ext $ λz, iff.trans (mem_image r.2) $ iff.trans (by exact ⟨λ⟨w, h1, h2⟩, ⟨w, (mem.congr_right e).1 h1, h2⟩, λ⟨w, h1, h2⟩, ⟨w, (mem.congr_right e).2 h1, h2⟩⟩) $ iff.symm (mem_image r.2)⟩ def image.mk : Π (f : Set.{u} → Set.{u}) [H : definable 1 f] (x) {y} (h : y ∈ x), f y ∈ @image f H x | ._ ⟨F⟩ x y := quotient.induction_on₂ x y $ λ⟨α, A⟩ y ⟨a, ya⟩, ⟨a, F.2 _ _ ya⟩ @[simp] def mem_image : Π {f : Set.{u} → Set.{u}} [H : definable 1 f] {x y : Set.{u}}, y ∈ @image f H x ↔ ∃z ∈ x, f z = y | ._ ⟨F⟩ x y := quotient.induction_on₂ x y $ λ⟨α, A⟩ y, ⟨λ⟨a, ya⟩, ⟨⟦A a⟧, mem.mk A a, eq.symm $ quotient.sound ya⟩, λ⟨z, hz, e⟩, e ▸ image.mk _ _ hz⟩ def pair (x y : Set.{u}) : Set.{u} := -- {{x}, {x, y}} @insert Set.{u} _ _ (@insert Set.{u} _ _ y {x}) {insert x (∅ : Set.{u})} def pair_sep (p : Set.{u} → Set.{u} → Prop) (x y : Set.{u}) : Set.{u} := {z ∈ powerset (powerset (x ∪ y)) | ∃a ∈ x, ∃b ∈ y, z = pair a b ∧ p a b} @[simp] theorem mem_pair_sep {p} {x y z : Set.{u}} : z ∈ pair_sep p x y ↔ ∃a ∈ x, ∃b ∈ y, z = pair a b ∧ p a b := by refine iff.trans mem_sep ⟨and.right, λe, ⟨_, e⟩⟩; exact let ⟨a, ax, b, bY, ze, pab⟩ := e in by rw ze; exact mem_powerset.2 (λu uz, mem_powerset.2 $ (mem_pair.1 uz).elim (λua, by rw ua; exact λv vu, by rw mem_singleton.1 vu; exact mem_union.2 (or.inl ax)) (λuab, by rw uab; exact λv vu, (mem_pair.1 vu).elim (λva, by rw va; exact mem_union.2 (or.inl ax)) (λvb, by rw vb; exact mem_union.2 (or.inr bY)))) def pair_inj {x y x' y' : Set.{u}} (H : pair x y = pair x' y') : x = x' ∧ y = y' := begin note ae := ext_iff.2 H, simp[pair] at ae, assert this : x = x', { note xx'y' := (ae (@insert Set.{u} _ _ x ∅)).1 (by simp), cases xx'y' with h h, exact singleton_inj h, { assert m : x' ∈ insert x (∅:Set.{u}), { rw h, simp }, simp at m, simph } }, refine ⟨this, _⟩, cases this, assert he : y = x → y = y', { intro yx, cases yx, note xy'x := (ae (@insert Set.{u} _ _ y' {x})).2 (by simp), cases xy'x with xy'x xy'xx, { note y'x : y' ∈ @insert Set.{u} Set.{u} _ x ∅ := by rw -xy'x; simp, simp at y'x, simph }, { note yxx := (ext_iff.2 xy'xx y').1 (by simp), simp at yxx, cases yxx; simp } }, note xyxy' := (ae (@insert Set.{u} _ _ y {x})).1 (by simp), cases xyxy' with xyx xyy', { note yx := (ext_iff.2 xyx y).1 (by simp), simp at yx, exact he yx }, { note yxy' := (ext_iff.2 xyy' y).1 (by simp), simp at yxy', cases yxy' with yx yy', exact he yx, assumption } end def prod : Set.{u} → Set.{u} → Set.{u} := pair_sep (λa b, true) @[simp] def mem_prod {x y z : Set.{u}} : z ∈ prod x y ↔ ∃a ∈ x, ∃b ∈ y, z = pair a b := by simp[prod] @[simp] def pair_mem_prod {x y a b : Set.{u}} : pair a b ∈ prod x y ↔ a ∈ x ∧ b ∈ y := ⟨λh, let ⟨a', a'x, b', b'y, e⟩ := mem_prod.1 h in match a', b', pair_inj e, a'x, b'y with ._, ._, ⟨rfl, rfl⟩, ax, bY := ⟨ax, bY⟩ end, λ⟨ax, bY⟩, by simp; exact ⟨a, ax, b, bY, rfl⟩⟩ def is_func (x y f : Set.{u}) : Prop := f ⊆ prod x y ∧ ∀z:Set.{u}, z ∈ x → ∃! w, pair z w ∈ f def funs (x y : Set.{u}) : Set.{u} := {f ∈ powerset (prod x y) | is_func x y f} @[simp] def mem_funs {x y f : Set.{u}} : f ∈ funs x y ↔ is_func x y f := by simp[funs]; exact ⟨and.left, λh, ⟨h, h.left⟩⟩ -- TODO(Mario): Prove this computably noncomputable instance map_definable_aux (f : Set → Set) [H : definable 1 f] : definable 1 (λy, pair y (f y)) := @classical.all_definable 1 _ noncomputable def map (f : Set → Set) [H : definable 1 f] : Set → Set := image (λy, pair y (f y)) @[simp] theorem mem_map {f : Set → Set} [H : definable 1 f] {x y : Set} : y ∈ map f x ↔ ∃z ∈ x, pair z (f z) = y := mem_image theorem map_unique {f : Set.{u} → Set.{u}} [H : definable 1 f] {x z : Set.{u}} (zx : z ∈ x) : ∃! w, pair z w ∈ map f x := ⟨f z, image.mk _ _ zx, λy yx, let ⟨w, wx, we⟩ := mem_image.1 yx, ⟨wz, fy⟩ := pair_inj we in by rw[-fy, wz]⟩ @[simp] theorem map_is_func {f : Set → Set} [H : definable 1 f] {x y : Set} : is_func x y (map f x) ↔ ∀z ∈ x, f z ∈ y := ⟨λ⟨ss, h⟩ z zx, let ⟨t, t1, t2⟩ := h z zx in by rw (t2 (f z) (image.mk _ _ zx)); exact (pair_mem_prod.1 (ss t1)).right, λh, ⟨λy yx, let ⟨z, zx, ze⟩ := mem_image.1 yx in by rw -ze; exact pair_mem_prod.2 ⟨zx, h z zx⟩, λz, map_unique⟩⟩ end Set def Class := set Set namespace Class instance has_mem_Set_Class : has_mem Set Class := ⟨set.mem⟩ instance : has_subset Class := ⟨set.subset⟩ instance : has_sep Set Class := ⟨set.sep⟩ instance : has_emptyc Class := ⟨λ a, false⟩ instance : has_insert Set Class := ⟨set.insert⟩ instance : has_union Class := ⟨set.union⟩ instance : has_inter Class := ⟨set.inter⟩ instance : has_neg Class := ⟨set.compl⟩ instance : has_sdiff Class := ⟨set.diff⟩ def of_Set (x : Set.{u}) : Class.{u} := {y | y ∈ x} instance : has_coe Set Class := ⟨of_Set⟩ def univ : Class := set.univ def to_Set (p : Set.{u} → Prop) (A : Class.{u}) : Prop := ∃x, ↑x = A ∧ p x protected def mem (A B : Class.{u}) : Prop := to_Set.{u} (λx, x ∈ B) A instance : has_mem Class Class := ⟨Class.mem⟩ theorem mem_univ {A : Class.{u}} : A ∈ univ.{u} ↔ ∃ x : Set.{u}, ↑x = A := exists_congr $ λx, and_true _ def Cong_to_Class (x : set Class.{u}) : Class.{u} := {y | ↑y ∈ x} def Class_to_Cong (x : Class.{u}) : set Class.{u} := {y | y ∈ x} def powerset (x : Class) : Class := Cong_to_Class (set.powerset x) notation `𝒫` := powerset def Union (x : Class) : Class := set.sUnion (Class_to_Cong x) notation `⋃` := Union theorem of_Set.inj {x y : Set.{u}} (h : (x : Class.{u}) = y) : x = y := Set.ext $ λz, by change z ∈ (x : Class.{u}) ↔ z ∈ (y : Class.{u}); simph @[simp] theorem to_Set_of_Set (p : Set.{u} → Prop) (x : Set.{u}) : to_Set p x ↔ p x := ⟨λ⟨y, yx, py⟩, by rwa of_Set.inj yx at py, λpx, ⟨x, rfl, px⟩⟩ @[simp] theorem mem_hom_left (x : Set.{u}) (A : Class.{u}) : (x : Class.{u}) ∈ A ↔ x ∈ A := to_Set_of_Set _ _ @[simp] theorem mem_hom_right (x y : Set.{u}) : x ∈ (y : Class.{u}) ↔ x ∈ y := iff.refl _ @[simp] theorem subset_hom (x y : Set.{u}) : (x : Class.{u}) ⊆ y ↔ x ⊆ y := iff.refl _ @[simp] theorem sep_hom (p : Set.{u} → Prop) (x : Set.{u}) : (↑{y ∈ x | p y} : Class.{u}) = {y ∈ x | p y} := set.ext $ λy, Set.mem_sep @[simp] theorem empty_hom : ↑(∅ : Set.{u}) = (∅ : Class.{u}) := set.ext $ λy, show _ ↔ false, by simp; exact Set.mem_empty y @[simp] theorem insert_hom (x y : Set.{u}) : (@insert Set.{u} Class.{u} _ x y) = ↑(insert x y) := set.ext $ λz, iff.symm Set.mem_insert @[simp] theorem union_hom (x y : Set.{u}) : (x : Class.{u}) ∪ y = (x ∪ y : Set.{u}) := set.ext $ λz, iff.symm Set.mem_union @[simp] theorem inter_hom (x y : Set.{u}) : (x : Class.{u}) ∩ y = (x ∩ y : Set.{u}) := set.ext $ λz, iff.symm Set.mem_inter @[simp] theorem diff_hom (x y : Set.{u}) : (x : Class.{u}) \ y = (x \ y : Set.{u}) := set.ext $ λz, iff.symm Set.mem_diff @[simp] theorem powerset_hom (x : Set.{u}) : powerset.{u} x = Set.powerset x := set.ext $ λz, iff.symm Set.mem_powerset @[simp] theorem Union_hom (x : Set.{u}) : Union.{u} x = Set.Union x := set.ext $ λz, by refine iff.trans _ (iff.symm Set.mem_Union); exact ⟨λ⟨._, ⟨a, rfl, ax⟩, za⟩, ⟨a, ax, za⟩, λ⟨a, ax, za⟩, ⟨_, ⟨a, rfl, ax⟩, za⟩⟩ def iota (p : Set → Prop) : Class := Union {x | ∀y, p y ↔ y = x} theorem iota_val (p : Set → Prop) (x : Set) (H : ∀y, p y ↔ y = x) : iota p = ↑x := set.ext $ λy, ⟨λ⟨._, ⟨x', rfl, h⟩, yx'⟩, by rwa -((H x').1 $ (h x').2 rfl), λyx, ⟨_, ⟨x, rfl, H⟩, yx⟩⟩ -- Unlike the other set constructors, the "iota" definite descriptor is a set for any set input, -- but not constructively so, so there is no associated (Set → Prop) → Set function. theorem iota_ex (p) : iota.{u} p ∈ univ.{u} := mem_univ.2 $ or.elim (classical.em $ ∃x, ∀y, p y ↔ y = x) (λ⟨x, h⟩, ⟨x, eq.symm $ iota_val p x h⟩) (λhn, ⟨∅, by simp; exact set.ext (λz, ⟨false.rec _, λ⟨._, ⟨x, rfl, H⟩, zA⟩, hn ⟨x, H⟩⟩)⟩) def fval (F A : Class.{u}) : Class.{u} := iota (λy, to_Set (λx, Set.pair x y ∈ F) A) infixl `′`:100 := fval theorem fval_ex (F A : Class.{u}) : F ′ A ∈ univ.{u} := iota_ex _ end Class namespace Set @[simp] def map_fval {f : Set.{u} → Set.{u}} [H : pSet.definable 1 f] {x y : Set.{u}} (h : y ∈ x) : (Set.map f x ′ y : Class.{u}) = f y := Class.iota_val _ _ (λz, by simp; exact ⟨λ⟨w, wz, pr⟩, let ⟨wy, fw⟩ := Set.pair_inj pr in by rw[-fw, wy], λe, by cases e; exact ⟨_, h, rfl⟩⟩) variables (x : Set.{u}) (h : (∅:Set.{u}) ∉ x) noncomputable def choice : Set := @map (λy, classical.epsilon (λz, z ∈ y)) (classical.all_definable _) x include h def choice_mem_aux (y : Set.{u}) (yx : y ∈ x) : classical.epsilon (λz:Set.{u}, z ∈ y) ∈ y := @classical.epsilon_spec _ (λz:Set.{u}, z ∈ y) $ classical.by_contradiction $ λn, h $ by rwa -((eq_empty y).2 $ λz zx, n ⟨z, zx⟩) def choice_is_func : is_func x (Union x) (choice x) := (@map_is_func _ (classical.all_definable _) _ _).2 $ λy yx, by simp; exact ⟨y, yx, choice_mem_aux x h y yx⟩ def choice_mem (y : Set.{u}) (yx : y ∈ x) : (choice x ′ y : Class.{u}) ∈ (y : Class.{u}) := by delta choice; rw map_fval yx; simp[choice_mem_aux x h y yx] end Set
21398bac739a495dea4000bbc5dfb8409bc6747a
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/topology/sheaves/sheaf_of_functions.lean
46f6b4c426fce1769e0ebc3bae6f3f24ab3f977c
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
7,115
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import topology.sheaves.presheaf_of_functions import topology.sheaves.sheaf import category_theory.limits.shapes.types import topology.local_homeomorph /-! # Sheaf conditions for presheaves of (continuous) functions. We show that * `Top.sheaf_condition.to_Type`: not-necessarily-continuous functions into a type form a sheaf * `Top.sheaf_condition.to_Types`: in fact, these may be dependent functions into a type family For * `Top.sheaf_condition.to_Top`: continuous functions into a topological space form a sheaf please see `topology/sheaves/local_predicate.lean`, where we set up a general framework for constructing sub(pre)sheaves of the sheaf of dependent functions. ## Future work Obviously there's more to do: * sections of a fiber bundle * various classes of smooth and structure preserving functions * functions into spaces with algebraic structure, which the sections inherit -/ open category_theory open category_theory.limits open topological_space open topological_space.opens universe u noncomputable theory variables (X : Top.{u}) open Top namespace Top.sheaf_condition /-- We show that the presheaf of functions to a type `T` (no continuity assumptions, just plain functions) form a sheaf. In fact, the proof is identical when we do this for dependent functions to a type family `T`, so we do the more general case. -/ def to_Types (T : X → Type u) : sheaf_condition (presheaf_to_Types X T) := λ ι U, -- U is a family of open sets, indexed by `ι`. -- In the informal comments below, I'll just write `U` to represent the union. begin refine fork.is_limit.mk _ _ _ _, { -- Our first goal is to define a function "lifted" to all of `U`, given -- `s`, some cone over the sheaf condition diagram -- (which we can think of as some type `s.X` which we know nothing about, -- except how to restrict terms to each of the `U i`, obtaining a function there, -- and that these restrictions are compatible), and -- `f`, a term of `s.X`. -- We do this one point at a time, so we also pick some `x` and the evidence `mem : x ∈ supr U`. rintros s f ⟨x, mem⟩, change x ∈ supr U at mem, -- FIXME there is a stray `has_coe_t_aux.coe` here -- Since `x ∈ supr U`, there must be some `i` so `x ∈ U i`: simp [opens.mem_supr] at mem, choose i hi using mem, -- We define out function to be the restriction of `f` to that `U i`, evaluated at `x`. exact ((s.ι ≫ pi.π _ i) f) ⟨x, hi⟩, }, { -- Now we need to verify that this lifted function restricts correctly to each set `U i`. -- Of course, the difficulty is that at any given point `x ∈ U i`, -- we may have used the axiom of choice to pick a differnt `j` with `x ∈ U j` -- when defining the function. -- This we'll need to use the fact that the restrictions are compatible. -- Again, we begin with some `s`, a cone over the sheaf condition diagram. intros s, -- The goal at this point is fairly inscrutable, -- but we know we're trying two functions are equal, so we call `ext` and see what we get: ext i f ⟨x, mem⟩, dsimp at mem, -- We now have `i : ι`, a term `f : s.X`, and a point `x` with `mem : x ∈ U i`. -- We clean up the goal a little, simp only [exists_prop, set.mem_range, set.mem_image, exists_exists_eq_and, category.assoc], simp only [limit.lift_π, types_comp_apply, fan.mk_π_app, presheaf_to_Type], dsimp, -- although you still need to be ambitious to read it. -- The mathematical content, of course, is that the lifted function we constructed from `f`, -- when restricted to `U i` and evaluated at `x`, -- has the same value as `f` restricted to to `U i` and evaluated at `x`. -- We have a slightly annoying issue at this point, -- that we're not really sure which `j : ι` was used to define the lifted function -- and this point `x`, because we used choice. -- As a trick, we create a new metavariable `j` to represent this choice, -- and later in the proof it will be solved by unification. let j : ι := _, -- Now, we assert that the two restrictions of `f` to `U i` and `U j` coincide on `U i ⊓ U j`, -- and in particular coincide there after evaluating at `x`. have s₀ := s.condition =≫ pi.π _ (j, i), simp only [sheaf_condition.left_res, sheaf_condition.right_res] at s₀, have s₁ := congr_fun s₀ f, have s₂ := congr_fun s₁ ⟨x, _⟩, clear s₀ s₁, -- Notice at this point we've spun after an additional goal: -- that `x ∈ U j ⊓ U i` to begin with! Let's get that out of the way. swap, { -- We knew `x ∈ U i` right from the start: refine ⟨_, mem⟩, -- Notice that when we introduced `j`, we just introduced it as some metavariable. -- However at this point it's received a concrete value, -- because Lean's unification has worked out that this `j` must have been the index -- that we picked using choice back when constructing the lift. -- From this, we can extract the evidence that `x ∈ U j`: convert @classical.some_spec _ (λ i, x ∈ (U i : set X)) _, }, -- Now, we can just assert that `s₂` is the droid you are looking for, -- and do a little patching up afterwards. convert s₂, }, { -- On the home stretch now, -- we just need to check that the lift we picked was the only possible one. -- So we suppose we had some other way `m` of choosing lifts, intros s m w, -- and observe that we need to check that it agrees with our choice -- for each `f : s .X` and each `x ∈ supr U`. ext f ⟨x, mem⟩, -- Now `w` is the evidence that other choice of lift agrees either on the `U i`s, -- or on the `U i ⊓ U j`s. -- We'll need the later, specialize w walking_parallel_pair.zero, -- because we're not sure which arbitrary `j : ι` we used to define our lift. let j : ι := _, -- Now it's just a matter of plugging in all the values; -- `j` gets solved for during unification. convert congr_fun (congr_fun (w =≫ pi.π _ j) f) ⟨x, _⟩, } end. -- We verify that the non-dependent version is an immediate consequence: /-- The presheaf of not-necessarily-continuous functions to a target type `T` satsifies the sheaf condition. -/ def to_Type (T : Type u) : sheaf_condition (presheaf_to_Type X T) := to_Types X (λ _, T) end Top.sheaf_condition namespace Top /-- The sheaf of not-necessarily-continuous functions on `X` with values in type family `T : X → Type u`. -/ def sheaf_to_Types (T : X → Type u) : sheaf (Type u) X := { presheaf := presheaf_to_Types X T, sheaf_condition := sheaf_condition.to_Types _ _, } /-- The sheaf of not-necessarily-continuous functions on `X` with values in a type `T`. -/ def sheaf_to_Type (T : Type u) : sheaf (Type u) X := { presheaf := presheaf_to_Type X T, sheaf_condition := sheaf_condition.to_Type _ _, } end Top
daeafbdaea5310f6059bc939016e43b67a0257bb
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/order/hom/basic.lean
5607f8afbb337ceae7504402898d49cac5760b02
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
41,777
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 logic.equiv.option import order.rel_iso import tactic.monotonicity.basic /-! # Order homomorphisms This file defines order homomorphisms, which are bundled monotone functions. A preorder homomorphism `f : α →o β` is a function `α → β` along with a proof that `∀ x y, x ≤ y → f x ≤ f y`. ## Main definitions In this file we define the following bundled monotone maps: * `order_hom α β` a.k.a. `α →o β`: Preorder homomorphism. An `order_hom α β` is a function `f : α → β` such that `a₁ ≤ a₂ → f a₁ ≤ f a₂` * `order_embedding α β` a.k.a. `α ↪o β`: Relation embedding. An `order_embedding α β` is an embedding `f : α ↪ β` such that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@rel_embedding α β (≤) (≤)`. * `order_iso`: Relation isomorphism. An `order_iso α β` is an equivalence `f : α ≃ β` such that `a ≤ b ↔ f a ≤ f b`. Defined as an abbreviation of `@rel_iso α β (≤) (≤)`. We also define many `order_hom`s. In some cases we define two versions, one with `ₘ` suffix and one without it (e.g., `order_hom.compₘ` and `order_hom.comp`). This means that the former function is a "more bundled" version of the latter. We can't just drop the "less bundled" version because the more bundled version usually does not work with dot notation. * `order_hom.id`: identity map as `α →o α`; * `order_hom.curry`: an order isomorphism between `α × β →o γ` and `α →o β →o γ`; * `order_hom.comp`: composition of two bundled monotone maps; * `order_hom.compₘ`: composition of bundled monotone maps as a bundled monotone map; * `order_hom.const`: constant function as a bundled monotone map; * `order_hom.prod`: combine `α →o β` and `α →o γ` into `α →o β × γ`; * `order_hom.prodₘ`: a more bundled version of `order_hom.prod`; * `order_hom.prod_iso`: order isomorphism between `α →o β × γ` and `(α →o β) × (α →o γ)`; * `order_hom.diag`: diagonal embedding of `α` into `α × α` as a bundled monotone map; * `order_hom.on_diag`: restrict a monotone map `α →o α →o β` to the diagonal; * `order_hom.fst`: projection `prod.fst : α × β → α` as a bundled monotone map; * `order_hom.snd`: projection `prod.snd : α × β → β` as a bundled monotone map; * `order_hom.prod_map`: `prod.map f g` as a bundled monotone map; * `pi.eval_order_hom`: evaluation of a function at a point `function.eval i` as a bundled monotone map; * `order_hom.coe_fn_hom`: coercion to function as a bundled monotone map; * `order_hom.apply`: application of a `order_hom` at a point as a bundled monotone map; * `order_hom.pi`: combine a family of monotone maps `f i : α →o π i` into a monotone map `α →o Π i, π i`; * `order_hom.pi_iso`: order isomorphism between `α →o Π i, π i` and `Π i, α →o π i`; * `order_hom.subtyle.val`: embedding `subtype.val : subtype p → α` as a bundled monotone map; * `order_hom.dual`: reinterpret a monotone map `α →o β` as a monotone map `αᵒᵈ →o βᵒᵈ`; * `order_hom.dual_iso`: order isomorphism between `α →o β` and `(αᵒᵈ →o βᵒᵈ)ᵒᵈ`; * `order_iso.compl`: order isomorphism `α ≃o αᵒᵈ` given by taking complements in a boolean algebra; We also define two functions to convert other bundled maps to `α →o β`: * `order_embedding.to_order_hom`: convert `α ↪o β` to `α →o β`; * `rel_hom.to_order_hom`: convert a `rel_hom` between strict orders to a `order_hom`. ## Tags monotone map, bundled morphism -/ open order_dual variables {F α β γ δ : Type*} /-- Bundled monotone (aka, increasing) function -/ structure order_hom (α β : Type*) [preorder α] [preorder β] := (to_fun : α → β) (monotone' : monotone to_fun) infixr ` →o `:25 := order_hom /-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/ abbreviation order_embedding (α β : Type*) [has_le α] [has_le β] := @rel_embedding α β (≤) (≤) infix ` ↪o `:25 := order_embedding /-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_iso (≤) (≤)`. -/ abbreviation order_iso (α β : Type*) [has_le α] [has_le β] := @rel_iso α β (≤) (≤) infix ` ≃o `:25 := order_iso /-- `order_hom_class F α b` asserts that `F` is a type of `≤`-preserving morphisms. -/ abbreviation order_hom_class (F : Type*) (α β : out_param Type*) [has_le α] [has_le β] := rel_hom_class F ((≤) : α → α → Prop) ((≤) : β → β → Prop) /-- `order_iso_class F α β` states that `F` is a type of order isomorphisms. You should extend this class when you extend `order_iso`. -/ class order_iso_class (F : Type*) (α β : out_param Type*) [has_le α] [has_le β] extends equiv_like F α β := (map_le_map_iff (f : F) {a b : α} : f a ≤ f b ↔ a ≤ b) export order_iso_class (map_le_map_iff) attribute [simp] map_le_map_iff instance [has_le α] [has_le β] [order_iso_class F α β] : has_coe_t F (α ≃o β) := ⟨λ f, ⟨f, λ _ _, map_le_map_iff f⟩⟩ @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_order_hom_class [has_le α] [has_le β] [order_iso_class F α β] : order_hom_class F α β := { map_rel := λ f a b, (map_le_map_iff f).2, ..equiv_like.to_embedding_like } namespace order_hom_class variables [preorder α] [preorder β] [order_hom_class F α β] protected lemma monotone (f : F) : monotone (f : α → β) := λ _ _, map_rel f protected lemma mono (f : F) : monotone (f : α → β) := λ _ _, map_rel f instance : has_coe_t F (α →o β) := ⟨λ f, { to_fun := f, monotone' := order_hom_class.mono _ }⟩ end order_hom_class section order_iso_class section has_le variables [has_le α] [has_le β] [order_iso_class F α β] @[simp] lemma map_inv_le_iff (f : F) {a : α} {b : β} : equiv_like.inv f b ≤ a ↔ b ≤ f a := by { convert (map_le_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } @[simp] lemma le_map_inv_iff (f : F) {a : α} {b : β} : a ≤ equiv_like.inv f b ↔ f a ≤ b := by { convert (map_le_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } end has_le variables [preorder α] [preorder β] [order_iso_class F α β] include β lemma map_lt_map_iff (f : F) {a b : α} : f a < f b ↔ a < b := lt_iff_lt_of_le_iff_le' (map_le_map_iff f) (map_le_map_iff f) @[simp] lemma map_inv_lt_iff (f : F) {a : α} {b : β} : equiv_like.inv f b < a ↔ b < f a := by { convert (map_lt_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } @[simp] lemma lt_map_inv_iff (f : F) {a : α} {b : β} : a < equiv_like.inv f b ↔ f a < b := by { convert (map_lt_map_iff _).symm, exact (equiv_like.right_inv _ _).symm } end order_iso_class namespace order_hom variables [preorder α] [preorder β] [preorder γ] [preorder δ] /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (α →o β) (λ _, α → β) := ⟨order_hom.to_fun⟩ initialize_simps_projections order_hom (to_fun → coe) protected lemma monotone (f : α →o β) : monotone f := f.monotone' protected lemma mono (f : α →o β) : monotone f := f.monotone instance : order_hom_class (α →o β) α β := { coe := to_fun, coe_injective' := λ f g h, by { cases f, cases g, congr' }, map_rel := λ f, f.monotone } @[simp] lemma to_fun_eq_coe {f : α →o β} : f.to_fun = f := rfl @[simp] lemma coe_fun_mk {f : α → β} (hf : _root_.monotone f) : (mk f hf : α → β) = f := rfl @[ext] -- See library note [partially-applied ext lemmas] lemma ext (f g : α →o β) (h : (f : α → β) = g) : f = g := fun_like.coe_injective h lemma coe_eq (f : α →o β) : coe f = f := by ext ; refl /-- One can lift an unbundled monotone function to a bundled one. -/ instance : can_lift (α → β) (α →o β) := { coe := coe_fn, cond := monotone, prf := λ f h, ⟨⟨f, h⟩, rfl⟩ } /-- Copy of an `order_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →o β) (f' : α → β) (h : f' = f) : α →o β := ⟨f', h.symm.subst f.monotone'⟩ /-- The identity function as bundled monotone function. -/ @[simps {fully_applied := ff}] def id : α →o α := ⟨id, monotone_id⟩ instance : inhabited (α →o α) := ⟨id⟩ /-- The preorder structure of `α →o β` is pointwise inequality: `f ≤ g ↔ ∀ a, f a ≤ g a`. -/ instance : preorder (α →o β) := @preorder.lift (α →o β) (α → β) _ coe_fn instance {β : Type*} [partial_order β] : partial_order (α →o β) := @partial_order.lift (α →o β) (α → β) _ coe_fn ext lemma le_def {f g : α →o β} : f ≤ g ↔ ∀ x, f x ≤ g x := iff.rfl @[simp, norm_cast] lemma coe_le_coe {f g : α →o β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl @[simp] lemma mk_le_mk {f g : α → β} {hf hg} : mk f hf ≤ mk g hg ↔ f ≤ g := iff.rfl @[mono] lemma apply_mono {f g : α →o β} {x y : α} (h₁ : f ≤ g) (h₂ : x ≤ y) : f x ≤ g y := (h₁ x).trans $ g.mono h₂ /-- Curry/uncurry as an order isomorphism between `α × β →o γ` and `α →o β →o γ`. -/ def curry : (α × β →o γ) ≃o (α →o β →o γ) := { to_fun := λ f, ⟨λ x, ⟨function.curry f x, λ y₁ y₂ h, f.mono ⟨le_rfl, h⟩⟩, λ x₁ x₂ h y, f.mono ⟨h, le_rfl⟩⟩, inv_fun := λ f, ⟨function.uncurry (λ x, f x), λ x y h, (f.mono h.1 x.2).trans $ (f y.1).mono h.2⟩, left_inv := λ f, by { ext ⟨x, y⟩, refl }, right_inv := λ f, by { ext x y, refl }, map_rel_iff' := λ f g, by simp [le_def] } @[simp] lemma curry_apply (f : α × β →o γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl @[simp] lemma curry_symm_apply (f : α →o β →o γ) (x : α × β) : curry.symm f x = f x.1 x.2 := rfl /-- The composition of two bundled monotone functions. -/ @[simps {fully_applied := ff}] def comp (g : β →o γ) (f : α →o β) : α →o γ := ⟨g ∘ f, g.mono.comp f.mono⟩ @[mono] lemma comp_mono ⦃g₁ g₂ : β →o γ⦄ (hg : g₁ ≤ g₂) ⦃f₁ f₂ : α →o β⦄ (hf : f₁ ≤ f₂) : g₁.comp f₁ ≤ g₂.comp f₂ := λ x, (hg _).trans (g₂.mono $ hf _) /-- The composition of two bundled monotone functions, a fully bundled version. -/ @[simps {fully_applied := ff}] def compₘ : (β →o γ) →o (α →o β) →o α →o γ := curry ⟨λ f : (β →o γ) × (α →o β), f.1.comp f.2, λ f₁ f₂ h, comp_mono h.1 h.2⟩ @[simp] lemma comp_id (f : α →o β) : comp f id = f := by { ext, refl } @[simp] lemma id_comp (f : α →o β) : comp id f = f := by { ext, refl } /-- Constant function bundled as a `order_hom`. -/ @[simps {fully_applied := ff}] def const (α : Type*) [preorder α] {β : Type*} [preorder β] : β →o α →o β := { to_fun := λ b, ⟨function.const α b, λ _ _ _, le_rfl⟩, monotone' := λ b₁ b₂ h x, h } @[simp] lemma const_comp (f : α →o β) (c : γ) : (const β c).comp f = const α c := rfl @[simp] lemma comp_const (γ : Type*) [preorder γ] (f : α →o β) (c : α) : f.comp (const γ c) = const γ (f c) := rfl /-- Given two bundled monotone maps `f`, `g`, `f.prod g` is the map `x ↦ (f x, g x)` bundled as a `order_hom`. -/ @[simps] protected def prod (f : α →o β) (g : α →o γ) : α →o (β × γ) := ⟨λ x, (f x, g x), λ x y h, ⟨f.mono h, g.mono h⟩⟩ @[mono] lemma prod_mono {f₁ f₂ : α →o β} (hf : f₁ ≤ f₂) {g₁ g₂ : α →o γ} (hg : g₁ ≤ g₂) : f₁.prod g₁ ≤ f₂.prod g₂ := λ x, prod.le_def.2 ⟨hf _, hg _⟩ lemma comp_prod_comp_same (f₁ f₂ : β →o γ) (g : α →o β) : (f₁.comp g).prod (f₂.comp g) = (f₁.prod f₂).comp g := rfl /-- Given two bundled monotone maps `f`, `g`, `f.prod g` is the map `x ↦ (f x, g x)` bundled as a `order_hom`. This is a fully bundled version. -/ @[simps] def prodₘ : (α →o β) →o (α →o γ) →o α →o β × γ := curry ⟨λ f : (α →o β) × (α →o γ), f.1.prod f.2, λ f₁ f₂ h, prod_mono h.1 h.2⟩ /-- Diagonal embedding of `α` into `α × α` as a `order_hom`. -/ @[simps] def diag : α →o α × α := id.prod id /-- Restriction of `f : α →o α →o β` to the diagonal. -/ @[simps {simp_rhs := tt}] def on_diag (f : α →o α →o β) : α →o β := (curry.symm f).comp diag /-- `prod.fst` as a `order_hom`. -/ @[simps] def fst : α × β →o α := ⟨prod.fst, λ x y h, h.1⟩ /-- `prod.snd` as a `order_hom`. -/ @[simps] def snd : α × β →o β := ⟨prod.snd, λ x y h, h.2⟩ @[simp] lemma fst_prod_snd : (fst : α × β →o α).prod snd = id := by { ext ⟨x, y⟩ : 2, refl } @[simp] lemma fst_comp_prod (f : α →o β) (g : α →o γ) : fst.comp (f.prod g) = f := ext _ _ rfl @[simp] lemma snd_comp_prod (f : α →o β) (g : α →o γ) : snd.comp (f.prod g) = g := ext _ _ rfl /-- Order isomorphism between the space of monotone maps to `β × γ` and the product of the spaces of monotone maps to `β` and `γ`. -/ @[simps] def prod_iso : (α →o β × γ) ≃o (α →o β) × (α →o γ) := { to_fun := λ f, (fst.comp f, snd.comp f), inv_fun := λ f, f.1.prod f.2, left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl, map_rel_iff' := λ f g, forall_and_distrib.symm } /-- `prod.map` of two `order_hom`s as a `order_hom`. -/ @[simps] def prod_map (f : α →o β) (g : γ →o δ) : α × γ →o β × δ := ⟨prod.map f g, λ x y h, ⟨f.mono h.1, g.mono h.2⟩⟩ variables {ι : Type*} {π : ι → Type*} [Π i, preorder (π i)] /-- Evaluation of an unbundled function at a point (`function.eval`) as a `order_hom`. -/ @[simps {fully_applied := ff}] def _root_.pi.eval_order_hom (i : ι) : (Π j, π j) →o π i := ⟨function.eval i, function.monotone_eval i⟩ /-- The "forgetful functor" from `α →o β` to `α → β` that takes the underlying function, is monotone. -/ @[simps {fully_applied := ff}] def coe_fn_hom : (α →o β) →o (α → β) := { to_fun := λ f, f, monotone' := λ x y h, h } /-- Function application `λ f, f a` (for fixed `a`) is a monotone function from the monotone function space `α →o β` to `β`. See also `pi.eval_order_hom`. -/ @[simps {fully_applied := ff}] def apply (x : α) : (α →o β) →o β := (pi.eval_order_hom x).comp coe_fn_hom /-- Construct a bundled monotone map `α →o Π i, π i` from a family of monotone maps `f i : α →o π i`. -/ @[simps] def pi (f : Π i, α →o π i) : α →o (Π i, π i) := ⟨λ x i, f i x, λ x y h i, (f i).mono h⟩ /-- Order isomorphism between bundled monotone maps `α →o Π i, π i` and families of bundled monotone maps `Π i, α →o π i`. -/ @[simps] def pi_iso : (α →o Π i, π i) ≃o Π i, α →o π i := { to_fun := λ f i, (pi.eval_order_hom i).comp f, inv_fun := pi, left_inv := λ f, by { ext x i, refl }, right_inv := λ f, by { ext x i, refl }, map_rel_iff' := λ f g, forall_swap } /-- `subtype.val` as a bundled monotone function. -/ @[simps {fully_applied := ff}] def subtype.val (p : α → Prop) : subtype p →o α := ⟨subtype.val, λ x y h, h⟩ /-- There is a unique monotone map from a subsingleton to itself. -/ instance unique [subsingleton α] : unique (α →o α) := { default := order_hom.id, uniq := λ a, ext _ _ (subsingleton.elim _ _) } lemma order_hom_eq_id [subsingleton α] (g : α →o α) : g = order_hom.id := subsingleton.elim _ _ /-- Reinterpret a bundled monotone function as a monotone function between dual orders. -/ @[simps] protected def dual : (α →o β) ≃ (αᵒᵈ →o βᵒᵈ) := { to_fun := λ f, ⟨order_dual.to_dual ∘ f ∘ order_dual.of_dual, f.mono.dual⟩, inv_fun := λ f, ⟨order_dual.of_dual ∘ f ∘ order_dual.to_dual, f.mono.dual⟩, left_inv := λ f, ext _ _ rfl, right_inv := λ f, ext _ _ rfl } @[simp] lemma dual_id : (order_hom.id : α →o α).dual = order_hom.id := rfl @[simp] lemma dual_comp (g : β →o γ) (f : α →o β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : order_hom.dual.symm order_hom.id = (order_hom.id : α →o α) := rfl @[simp] lemma symm_dual_comp (g : βᵒᵈ →o γᵒᵈ) (f : αᵒᵈ →o βᵒᵈ) : order_hom.dual.symm (g.comp f) = (order_hom.dual.symm g).comp (order_hom.dual.symm f) := rfl /-- `order_hom.dual` as an order isomorphism. -/ def dual_iso (α β : Type*) [preorder α] [preorder β] : (α →o β) ≃o (αᵒᵈ →o βᵒᵈ)ᵒᵈ := { to_equiv := order_hom.dual.trans order_dual.to_dual, map_rel_iff' := λ f g, iff.rfl } /-- Lift an order homomorphism `f : α →o β` to an order homomorphism `with_bot α →o with_bot β`. -/ @[simps { fully_applied := ff }] protected def with_bot_map (f : α →o β) : with_bot α →o with_bot β := ⟨with_bot.map f, f.mono.with_bot_map⟩ /-- Lift an order homomorphism `f : α →o β` to an order homomorphism `with_top α →o with_top β`. -/ @[simps { fully_applied := ff }] protected def with_top_map (f : α →o β) : with_top α →o with_top β := ⟨with_top.map f, f.mono.with_top_map⟩ end order_hom /-- Embeddings of partial orders that preserve `<` also preserve `≤`. -/ def rel_embedding.order_embedding_of_lt_embedding [partial_order α] [partial_order β] (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : α ↪o β := { map_rel_iff' := by { intros, simp [le_iff_lt_or_eq,f.map_rel_iff, f.injective.eq_iff] }, .. f } @[simp] lemma rel_embedding.order_embedding_of_lt_embedding_apply [partial_order α] [partial_order β] {f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)} {x : α} : rel_embedding.order_embedding_of_lt_embedding f x = f x := rfl namespace order_embedding variables [preorder α] [preorder β] (f : α ↪o β) /-- `<` is preserved by order embeddings of preorders. -/ def lt_embedding : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop) := { map_rel_iff' := by intros; simp [lt_iff_le_not_le, f.map_rel_iff], .. f } @[simp] lemma lt_embedding_apply (x : α) : f.lt_embedding x = f x := rfl @[simp] theorem le_iff_le {a b} : (f a) ≤ (f b) ↔ a ≤ b := f.map_rel_iff @[simp] theorem lt_iff_lt {a b} : f a < f b ↔ a < b := f.lt_embedding.map_rel_iff @[simp] lemma eq_iff_eq {a b} : f a = f b ↔ a = b := f.injective.eq_iff protected theorem monotone : monotone f := order_hom_class.monotone f protected theorem strict_mono : strict_mono f := λ x y, f.lt_iff_lt.2 protected theorem acc (a : α) : acc (<) (f a) → acc (<) a := f.lt_embedding.acc a protected theorem well_founded : well_founded ((<) : β → β → Prop) → well_founded ((<) : α → α → Prop) := f.lt_embedding.well_founded protected theorem is_well_order [is_well_order β (<)] : is_well_order α (<) := f.lt_embedding.is_well_order /-- An order embedding is also an order embedding between dual orders. -/ protected def dual : αᵒᵈ ↪o βᵒᵈ := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- A version of `with_bot.map` for order embeddings. -/ @[simps { fully_applied := ff }] protected def with_bot_map (f : α ↪o β) : with_bot α ↪o with_bot β := { to_fun := with_bot.map f, map_rel_iff' := with_bot.map_le_iff f (λ a b, f.map_rel_iff), .. f.to_embedding.option_map } /-- A version of `with_top.map` for order embeddings. -/ @[simps { fully_applied := ff }] protected def with_top_map (f : α ↪o β) : with_top α ↪o with_top β := { to_fun := with_top.map f, .. f.dual.with_bot_map.dual } /-- To define an order embedding from a partial order to a preorder it suffices to give a function together with a proof that it satisfies `f a ≤ f b ↔ a ≤ b`. -/ def of_map_le_iff {α β} [partial_order α] [preorder β] (f : α → β) (hf : ∀ a b, f a ≤ f b ↔ a ≤ b) : α ↪o β := rel_embedding.of_map_rel_iff f hf @[simp] lemma coe_of_map_le_iff {α β} [partial_order α] [preorder β] {f : α → β} (h) : ⇑(of_map_le_iff f h) = f := rfl /-- A strictly monotone map from a linear order is an order embedding. --/ def of_strict_mono {α β} [linear_order α] [preorder β] (f : α → β) (h : strict_mono f) : α ↪o β := of_map_le_iff f (λ _ _, h.le_iff_le) @[simp] lemma coe_of_strict_mono {α β} [linear_order α] [preorder β] {f : α → β} (h : strict_mono f) : ⇑(of_strict_mono f h) = f := rfl /-- Embedding of a subtype into the ambient type as an `order_embedding`. -/ @[simps {fully_applied := ff}] def subtype (p : α → Prop) : subtype p ↪o α := ⟨function.embedding.subtype p, λ x y, iff.rfl⟩ /-- Convert an `order_embedding` to a `order_hom`. -/ @[simps {fully_applied := ff}] def to_order_hom {X Y : Type*} [preorder X] [preorder Y] (f : X ↪o Y) : X →o Y := { to_fun := f, monotone' := f.monotone } end order_embedding section rel_hom variables [partial_order α] [preorder β] namespace rel_hom variables (f : ((<) : α → α → Prop) →r ((<) : β → β → Prop)) /-- A bundled expression of the fact that a map between partial orders that is strictly monotone is weakly monotone. -/ @[simps {fully_applied := ff}] def to_order_hom : α →o β := { to_fun := f, monotone' := strict_mono.monotone (λ x y, f.map_rel), } end rel_hom lemma rel_embedding.to_order_hom_injective (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : function.injective (f : ((<) : α → α → Prop) →r ((<) : β → β → Prop)).to_order_hom := λ _ _ h, f.injective h end rel_hom namespace order_iso section has_le variables [has_le α] [has_le β] [has_le γ] instance : order_iso_class (α ≃o β) α β := { coe := λ f, f.to_fun, inv := λ f, f.inv_fun, left_inv := λ f, f.left_inv, right_inv := λ f, f.right_inv, coe_injective' := λ f g h₁ h₂, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_le_map_iff := λ f, f.map_rel_iff' } @[simp] lemma to_fun_eq_coe {f : α ≃o β} : f.to_fun = f := rfl @[ext] -- See note [partially-applied ext lemmas] lemma ext {f g : α ≃o β} (h : (f : α → β) = g) : f = g := fun_like.coe_injective h /-- Reinterpret an order isomorphism as an order embedding. -/ def to_order_embedding (e : α ≃o β) : α ↪o β := e.to_rel_embedding @[simp] lemma coe_to_order_embedding (e : α ≃o β) : ⇑(e.to_order_embedding) = e := rfl protected lemma bijective (e : α ≃o β) : function.bijective e := e.to_equiv.bijective protected lemma injective (e : α ≃o β) : function.injective e := e.to_equiv.injective protected lemma surjective (e : α ≃o β) : function.surjective e := e.to_equiv.surjective @[simp] lemma range_eq (e : α ≃o β) : set.range e = set.univ := e.surjective.range_eq @[simp] lemma apply_eq_iff_eq (e : α ≃o β) {x y : α} : e x = e y ↔ x = y := e.to_equiv.apply_eq_iff_eq /-- Identity order isomorphism. -/ def refl (α : Type*) [has_le α] : α ≃o α := rel_iso.refl (≤) @[simp] lemma coe_refl : ⇑(refl α) = id := rfl @[simp] lemma refl_apply (x : α) : refl α x = x := rfl @[simp] lemma refl_to_equiv : (refl α).to_equiv = equiv.refl α := rfl /-- Inverse of an order isomorphism. -/ def symm (e : α ≃o β) : β ≃o α := e.symm @[simp] lemma apply_symm_apply (e : α ≃o β) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (e : α ≃o β) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x @[simp] lemma symm_refl (α : Type*) [has_le α] : (refl α).symm = refl α := rfl lemma apply_eq_iff_eq_symm_apply (e : α ≃o β) (x : α) (y : β) : e x = y ↔ x = e.symm y := e.to_equiv.apply_eq_iff_eq_symm_apply theorem symm_apply_eq (e : α ≃o β) {x : α} {y : β} : e.symm y = x ↔ y = e x := e.to_equiv.symm_apply_eq @[simp] lemma symm_symm (e : α ≃o β) : e.symm.symm = e := by { ext, refl } lemma symm_injective : function.injective (symm : (α ≃o β) → (β ≃o α)) := λ e e' h, by rw [← e.symm_symm, h, e'.symm_symm] @[simp] lemma to_equiv_symm (e : α ≃o β) : e.to_equiv.symm = e.symm.to_equiv := rfl @[simp] lemma symm_image_image (e : α ≃o β) (s : set α) : e.symm '' (e '' s) = s := e.to_equiv.symm_image_image s @[simp] lemma image_symm_image (e : α ≃o β) (s : set β) : e '' (e.symm '' s) = s := e.to_equiv.image_symm_image s lemma image_eq_preimage (e : α ≃o β) (s : set α) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s @[simp] lemma preimage_symm_preimage (e : α ≃o β) (s : set α) : e ⁻¹' (e.symm ⁻¹' s) = s := e.to_equiv.preimage_symm_preimage s @[simp] lemma symm_preimage_preimage (e : α ≃o β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s := e.to_equiv.symm_preimage_preimage s @[simp] lemma image_preimage (e : α ≃o β) (s : set β) : e '' (e ⁻¹' s) = s := e.to_equiv.image_preimage s @[simp] lemma preimage_image (e : α ≃o β) (s : set α) : e ⁻¹' (e '' s) = s := e.to_equiv.preimage_image s /-- Composition of two order isomorphisms is an order isomorphism. -/ @[trans] def trans (e : α ≃o β) (e' : β ≃o γ) : α ≃o γ := e.trans e' @[simp] lemma coe_trans (e : α ≃o β) (e' : β ≃o γ) : ⇑(e.trans e') = e' ∘ e := rfl @[simp] lemma trans_apply (e : α ≃o β) (e' : β ≃o γ) (x : α) : e.trans e' x = e' (e x) := rfl @[simp] lemma refl_trans (e : α ≃o β) : (refl α).trans e = e := by { ext x, refl } @[simp] lemma trans_refl (e : α ≃o β) : e.trans (refl β) = e := by { ext x, refl } /-- `prod.swap` as an `order_iso`. -/ def prod_comm : (α × β) ≃o (β × α) := { to_equiv := equiv.prod_comm α β, map_rel_iff' := λ a b, prod.swap_le_swap } @[simp] lemma coe_prod_comm : ⇑(prod_comm : (α × β) ≃o (β × α)) = prod.swap := rfl @[simp] lemma prod_comm_symm : (prod_comm : (α × β) ≃o (β × α)).symm = prod_comm := rfl variables (α) /-- The order isomorphism between a type and its double dual. -/ def dual_dual : α ≃o αᵒᵈᵒᵈ := refl α @[simp] lemma coe_dual_dual : ⇑(dual_dual α) = to_dual ∘ to_dual := rfl @[simp] lemma coe_dual_dual_symm : ⇑(dual_dual α).symm = of_dual ∘ of_dual := rfl variables {α} @[simp] lemma dual_dual_apply (a : α) : dual_dual α a = to_dual (to_dual a) := rfl @[simp] lemma dual_dual_symm_apply (a : αᵒᵈᵒᵈ) : (dual_dual α).symm a = of_dual (of_dual a) := rfl end has_le open set section le variables [has_le α] [has_le β] [has_le γ] @[simp] lemma le_iff_le (e : α ≃o β) {x y : α} : e x ≤ e y ↔ x ≤ y := e.map_rel_iff lemma le_symm_apply (e : α ≃o β) {x : α} {y : β} : x ≤ e.symm y ↔ e x ≤ y := e.rel_symm_apply lemma symm_apply_le (e : α ≃o β) {x : α} {y : β} : e.symm y ≤ x ↔ y ≤ e x := e.symm_apply_rel end le variables [preorder α] [preorder β] [preorder γ] protected lemma monotone (e : α ≃o β) : monotone e := e.to_order_embedding.monotone protected lemma strict_mono (e : α ≃o β) : strict_mono e := e.to_order_embedding.strict_mono @[simp] lemma lt_iff_lt (e : α ≃o β) {x y : α} : e x < e y ↔ x < y := e.to_order_embedding.lt_iff_lt /-- Converts an `order_iso` into a `rel_iso (<) (<)`. -/ def to_rel_iso_lt (e : α ≃o β) : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop) := ⟨e.to_equiv, λ x y, lt_iff_lt e⟩ @[simp] lemma to_rel_iso_lt_apply (e : α ≃o β) (x : α) : e.to_rel_iso_lt x = e x := rfl @[simp] lemma to_rel_iso_lt_symm (e : α ≃o β) : e.to_rel_iso_lt.symm = e.symm.to_rel_iso_lt := rfl /-- Converts a `rel_iso (<) (<)` into an `order_iso`. -/ def of_rel_iso_lt {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) : α ≃o β := ⟨e.to_equiv, λ x y, by simp [le_iff_eq_or_lt, e.map_rel_iff]⟩ @[simp] lemma of_rel_iso_lt_apply {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) (x : α) : of_rel_iso_lt e x = e x := rfl @[simp] lemma of_rel_iso_lt_symm {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) : (of_rel_iso_lt e).symm = of_rel_iso_lt e.symm := rfl @[simp] lemma of_rel_iso_lt_to_rel_iso_lt {α β} [partial_order α] [partial_order β] (e : α ≃o β) : of_rel_iso_lt (to_rel_iso_lt e) = e := by { ext, simp } @[simp] lemma to_rel_iso_lt_of_rel_iso_lt {α β} [partial_order α] [partial_order β] (e : ((<) : α → α → Prop) ≃r ((<) : β → β → Prop)) : to_rel_iso_lt (of_rel_iso_lt e) = e := by { ext, simp } /-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders, it suffices to prove `cmp a (g b) = cmp (f a) b`. --/ def of_cmp_eq_cmp {α β} [linear_order α] [linear_order β] (f : α → β) (g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β := have gf : ∀ (a : α), a = g (f a) := by { intro, rw [←cmp_eq_eq_iff, h, cmp_self_eq_eq] }, { to_fun := f, inv_fun := g, left_inv := λ a, (gf a).symm, right_inv := by { intro, rw [←cmp_eq_eq_iff, ←h, cmp_self_eq_eq] }, map_rel_iff' := by { intros, apply le_iff_le_of_cmp_eq_cmp, convert (h _ _).symm, apply gf } } /-- To show that `f : α →o β` and `g : β →o α` make up an order isomorphism it is enough to show that `g` is the inverse of `f`-/ def of_hom_inv {F G : Type*} [order_hom_class F α β] [order_hom_class G β α] (f : F) (g : G) (h₁ : (f : α →o β).comp (g : β →o α) = order_hom.id) (h₂ : (g : β →o α).comp (f : α →o β) = order_hom.id) : α ≃o β := { to_fun := f, inv_fun := g, left_inv := fun_like.congr_fun h₂, right_inv := fun_like.congr_fun h₁, map_rel_iff' := λ a b, ⟨λ h, by { replace h := map_rel g h, rwa [equiv.coe_fn_mk, (show g (f a) = (g : β →o α).comp (f : α →o β) a, from rfl), (show g (f b) = (g : β →o α).comp (f : α →o β) b, from rfl), h₂] at h }, λ h, (f : α →o β).monotone h⟩ } /-- Order isomorphism between two equal sets. -/ def set_congr (s t : set α) (h : s = t) : s ≃o t := { to_equiv := equiv.set_congr h, map_rel_iff' := λ x y, iff.rfl } /-- Order isomorphism between `univ : set α` and `α`. -/ def set.univ : (set.univ : set α) ≃o α := { to_equiv := equiv.set.univ α, map_rel_iff' := λ x y, iff.rfl } /-- Order isomorphism between `α → β` and `β`, where `α` has a unique element. -/ @[simps to_equiv apply] def fun_unique (α β : Type*) [unique α] [preorder β] : (α → β) ≃o β := { to_equiv := equiv.fun_unique α β, map_rel_iff' := λ f g, by simp [pi.le_def, unique.forall_iff] } @[simp] lemma fun_unique_symm_apply {α β : Type*} [unique α] [preorder β] : ((fun_unique α β).symm : β → α → β) = function.const α := rfl end order_iso namespace equiv variables [preorder α] [preorder β] /-- If `e` is an equivalence with monotone forward and inverse maps, then `e` is an order isomorphism. -/ def to_order_iso (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) : α ≃o β := ⟨e, λ x y, ⟨λ h, by simpa only [e.symm_apply_apply] using h₂ h, λ h, h₁ h⟩⟩ @[simp] lemma coe_to_order_iso (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) : ⇑(e.to_order_iso h₁ h₂) = e := rfl @[simp] lemma to_order_iso_to_equiv (e : α ≃ β) (h₁ : monotone e) (h₂ : monotone e.symm) : (e.to_order_iso h₁ h₂).to_equiv = e := rfl end equiv /-- If a function `f` is strictly monotone on a set `s`, then it defines an order isomorphism between `s` and its image. -/ protected noncomputable def strict_mono_on.order_iso {α β} [linear_order α] [preorder β] (f : α → β) (s : set α) (hf : strict_mono_on f s) : s ≃o f '' s := { to_equiv := hf.inj_on.bij_on_image.equiv _, map_rel_iff' := λ x y, hf.le_iff_le x.2 y.2 } namespace strict_mono variables {α β} [linear_order α] [preorder β] variables (f : α → β) (h_mono : strict_mono f) (h_surj : function.surjective f) /-- A strictly monotone function from a linear order is an order isomorphism between its domain and its range. -/ @[simps apply] protected noncomputable def order_iso : α ≃o set.range f := { to_equiv := equiv.of_injective f h_mono.injective, map_rel_iff' := λ a b, h_mono.le_iff_le } /-- A strictly monotone surjective function from a linear order is an order isomorphism. -/ noncomputable def order_iso_of_surjective : α ≃o β := (h_mono.order_iso f).trans $ (order_iso.set_congr _ _ h_surj.range_eq).trans order_iso.set.univ @[simp] lemma coe_order_iso_of_surjective : (order_iso_of_surjective f h_mono h_surj : α → β) = f := rfl @[simp] lemma order_iso_of_surjective_symm_apply_self (a : α) : (order_iso_of_surjective f h_mono h_surj).symm (f a) = a := (order_iso_of_surjective f h_mono h_surj).symm_apply_apply _ lemma order_iso_of_surjective_self_symm_apply (b : β) : f ((order_iso_of_surjective f h_mono h_surj).symm b) = b := (order_iso_of_surjective f h_mono h_surj).apply_symm_apply _ /-- A strictly monotone function with a right inverse is an order isomorphism. -/ @[simps {fully_applied := false}] def order_iso_of_right_inverse (g : β → α) (hg : function.right_inverse g f) : α ≃o β := { to_fun := f, inv_fun := g, left_inv := λ x, h_mono.injective $ hg _, right_inv := hg, .. order_embedding.of_strict_mono f h_mono } end strict_mono /-- An order isomorphism is also an order isomorphism between dual orders. -/ protected def order_iso.dual [has_le α] [has_le β] (f : α ≃o β) : αᵒᵈ ≃o βᵒᵈ := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ section lattice_isos lemma order_iso.map_bot' [has_le α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x ≤ x') (hy : ∀ y', y ≤ y') : f x = y := by { refine le_antisymm _ (hy _), rw [← f.apply_symm_apply y, f.map_rel_iff], apply hx } lemma order_iso.map_bot [has_le α] [partial_order β] [order_bot α] [order_bot β] (f : α ≃o β) : f ⊥ = ⊥ := f.map_bot' (λ _, bot_le) (λ _, bot_le) lemma order_iso.map_top' [has_le α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x' ≤ x) (hy : ∀ y', y' ≤ y) : f x = y := f.dual.map_bot' hx hy lemma order_iso.map_top [has_le α] [partial_order β] [order_top α] [order_top β] (f : α ≃o β) : f ⊤ = ⊤ := f.dual.map_bot lemma order_embedding.map_inf_le [semilattice_inf α] [semilattice_inf β] (f : α ↪o β) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := f.monotone.map_inf_le x y lemma order_embedding.le_map_sup [semilattice_sup α] [semilattice_sup β] (f : α ↪o β) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := f.monotone.le_map_sup x y lemma order_iso.map_inf [semilattice_inf α] [semilattice_inf β] (f : α ≃o β) (x y : α) : f (x ⊓ y) = f x ⊓ f y := begin refine (f.to_order_embedding.map_inf_le x y).antisymm _, simpa [← f.symm.le_iff_le] using f.symm.to_order_embedding.map_inf_le (f x) (f y) end lemma order_iso.map_sup [semilattice_sup α] [semilattice_sup β] (f : α ≃o β) (x y : α) : f (x ⊔ y) = f x ⊔ f y := f.dual.map_inf x y /-- Note that this goal could also be stated `(disjoint on f) a b` -/ lemma disjoint.map_order_iso [semilattice_inf α] [order_bot α] [semilattice_inf β] [order_bot β] {a b : α} (f : α ≃o β) (ha : disjoint a b) : disjoint (f a) (f b) := by { rw [disjoint, ←f.map_inf, ←f.map_bot], exact f.monotone ha } /-- Note that this goal could also be stated `(codisjoint on f) a b` -/ lemma codisjoint.map_order_iso [semilattice_sup α] [order_top α] [semilattice_sup β] [order_top β] {a b : α} (f : α ≃o β) (ha : codisjoint a b) : codisjoint (f a) (f b) := by { rw [codisjoint, ←f.map_sup, ←f.map_top], exact f.monotone ha } @[simp] lemma disjoint_map_order_iso_iff [semilattice_inf α] [order_bot α] [semilattice_inf β] [order_bot β] {a b : α} (f : α ≃o β) : disjoint (f a) (f b) ↔ disjoint a b := ⟨λ h, f.symm_apply_apply a ▸ f.symm_apply_apply b ▸ h.map_order_iso f.symm, λ h, h.map_order_iso f⟩ @[simp] lemma codisjoint_map_order_iso_iff [semilattice_sup α] [order_top α] [semilattice_sup β] [order_top β] {a b : α} (f : α ≃o β) : codisjoint (f a) (f b) ↔ codisjoint a b := ⟨λ h, f.symm_apply_apply a ▸ f.symm_apply_apply b ▸ h.map_order_iso f.symm, λ h, h.map_order_iso f⟩ namespace with_bot /-- Taking the dual then adding `⊥` is the same as adding `⊤` then taking the dual. This is the order iso form of `with_bot.of_dual`, as proven by `coe_to_dual_top_equiv_eq`. -/ protected def to_dual_top_equiv [has_le α] : with_bot αᵒᵈ ≃o (with_top α)ᵒᵈ := order_iso.refl _ @[simp] lemma to_dual_top_equiv_coe [has_le α] (a : α) : with_bot.to_dual_top_equiv ↑(to_dual a) = to_dual (a : with_top α) := rfl @[simp] lemma to_dual_top_equiv_symm_coe [has_le α] (a : α) : with_bot.to_dual_top_equiv.symm (to_dual (a : with_top α)) = ↑(to_dual a) := rfl @[simp] lemma to_dual_top_equiv_bot [has_le α] : with_bot.to_dual_top_equiv (⊥ : with_bot αᵒᵈ) = ⊥ := rfl @[simp] lemma to_dual_top_equiv_symm_bot [has_le α] : with_bot.to_dual_top_equiv.symm (⊥ : (with_top α)ᵒᵈ) = ⊥ := rfl lemma coe_to_dual_top_equiv_eq [has_le α] : (with_bot.to_dual_top_equiv : with_bot αᵒᵈ → (with_top α)ᵒᵈ) = to_dual ∘ with_bot.of_dual := funext $ λ _, rfl end with_bot namespace with_top /-- Taking the dual then adding `⊤` is the same as adding `⊥` then taking the dual. This is the order iso form of `with_top.of_dual`, as proven by `coe_to_dual_bot_equiv_eq`. -/ protected def to_dual_bot_equiv [has_le α] : with_top αᵒᵈ ≃o (with_bot α)ᵒᵈ := order_iso.refl _ @[simp] lemma to_dual_bot_equiv_coe [has_le α] (a : α) : with_top.to_dual_bot_equiv ↑(to_dual a) = to_dual (a : with_bot α) := rfl @[simp] lemma to_dual_bot_equiv_symm_coe [has_le α] (a : α) : with_top.to_dual_bot_equiv.symm (to_dual (a : with_bot α)) = ↑(to_dual a) := rfl @[simp] lemma to_dual_bot_equiv_top [has_le α] : with_top.to_dual_bot_equiv (⊤ : with_top αᵒᵈ) = ⊤ := rfl @[simp] lemma to_dual_bot_equiv_symm_top [has_le α] : with_top.to_dual_bot_equiv.symm (⊤ : (with_bot α)ᵒᵈ) = ⊤ := rfl lemma coe_to_dual_bot_equiv_eq [has_le α] : (with_top.to_dual_bot_equiv : with_top αᵒᵈ → (with_bot α)ᵒᵈ) = to_dual ∘ with_top.of_dual := funext $ λ _, rfl end with_top namespace order_iso variables [partial_order α] [partial_order β] [partial_order γ] /-- A version of `equiv.option_congr` for `with_top`. -/ @[simps apply] def with_top_congr (e : α ≃o β) : with_top α ≃o with_top β := { to_equiv := e.to_equiv.option_congr, .. e.to_order_embedding.with_top_map } @[simp] lemma with_top_congr_refl : (order_iso.refl α).with_top_congr = order_iso.refl _ := rel_iso.to_equiv_injective equiv.option_congr_refl @[simp] lemma with_top_congr_symm (e : α ≃o β) : e.with_top_congr.symm = e.symm.with_top_congr := rel_iso.to_equiv_injective e.to_equiv.option_congr_symm @[simp] lemma with_top_congr_trans (e₁ : α ≃o β) (e₂ : β ≃o γ) : e₁.with_top_congr.trans e₂.with_top_congr = (e₁.trans e₂).with_top_congr := rel_iso.to_equiv_injective $ e₁.to_equiv.option_congr_trans e₂.to_equiv /-- A version of `equiv.option_congr` for `with_bot`. -/ @[simps apply] def with_bot_congr (e : α ≃o β) : with_bot α ≃o with_bot β := { to_equiv := e.to_equiv.option_congr, .. e.to_order_embedding.with_bot_map } @[simp] lemma with_bot_congr_refl : (order_iso.refl α).with_bot_congr = order_iso.refl _ := rel_iso.to_equiv_injective equiv.option_congr_refl @[simp] lemma with_bot_congr_symm (e : α ≃o β) : e.with_bot_congr.symm = e.symm.with_bot_congr := rel_iso.to_equiv_injective e.to_equiv.option_congr_symm @[simp] lemma with_bot_congr_trans (e₁ : α ≃o β) (e₂ : β ≃o γ) : e₁.with_bot_congr.trans e₂.with_bot_congr = (e₁.trans e₂).with_bot_congr := rel_iso.to_equiv_injective $ e₁.to_equiv.option_congr_trans e₂.to_equiv end order_iso section bounded_order variables [lattice α] [lattice β] [bounded_order α] [bounded_order β] (f : α ≃o β) include f lemma order_iso.is_compl {x y : α} (h : is_compl x y) : is_compl (f x) (f y) := ⟨h.1.map_order_iso _, h.2.map_order_iso _⟩ theorem order_iso.is_compl_iff {x y : α} : is_compl x y ↔ is_compl (f x) (f y) := ⟨f.is_compl, λ h, f.symm_apply_apply x ▸ f.symm_apply_apply y ▸ f.symm.is_compl h⟩ lemma order_iso.is_complemented [is_complemented α] : is_complemented β := ⟨λ x, begin obtain ⟨y, hy⟩ := exists_is_compl (f.symm x), rw ← f.symm_apply_apply y at hy, refine ⟨f y, f.symm.is_compl_iff.2 hy⟩, end⟩ theorem order_iso.is_complemented_iff : is_complemented α ↔ is_complemented β := ⟨by { introI, exact f.is_complemented }, by { introI, exact f.symm.is_complemented }⟩ end bounded_order end lattice_isos section boolean_algebra variables (α) [boolean_algebra α] /-- Taking complements as an order isomorphism to the order dual. -/ @[simps] def order_iso.compl : α ≃o αᵒᵈ := { to_fun := order_dual.to_dual ∘ compl, inv_fun := compl ∘ order_dual.of_dual, left_inv := compl_compl, right_inv := compl_compl, map_rel_iff' := λ x y, compl_le_compl_iff_le } theorem compl_strict_anti : strict_anti (compl : α → α) := (order_iso.compl α).strict_mono theorem compl_antitone : antitone (compl : α → α) := (order_iso.compl α).monotone end boolean_algebra
10c45c7ca018014d81aed58fb6b436d848c5f669
a7dd8b83f933e72c40845fd168dde330f050b1c9
/src/category_theory/instances/CommRing/limits.lean
c807e1cb1bf3ec5b72ff00c3eec56597e4ad9ed7
[ "Apache-2.0" ]
permissive
NeilStrickland/mathlib
10420e92ee5cb7aba1163c9a01dea2f04652ed67
3efbd6f6dff0fb9b0946849b43b39948560a1ffe
refs/heads/master
1,589,043,046,346
1,558,938,706,000
1,558,938,706,000
181,285,984
0
0
Apache-2.0
1,568,941,848,000
1,555,233,833,000
Lean
UTF-8
Lean
false
false
4,107
lean
-- Copyright (c) 2019 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.instances.CommRing.basic import category_theory.limits.types import category_theory.limits.preserves import ring_theory.subring import algebra.pi_instances open category_theory open category_theory.instances universe u namespace category_theory.instances.CommRing open category_theory.limits variables {J : Type u} [small_category J] instance (F : J ⥤ CommRing.{u}) (j) : comm_ring ((F ⋙ CommRing.forget).obj j) := by { dsimp, apply_instance } instance (F : J ⥤ CommRing.{u}) (j j') (f : j ⟶ j') : is_ring_hom ((F ⋙ CommRing.forget).map f) := by { dsimp, apply_instance } instance sections_submonoid (F : J ⥤ CommRing.{u}) : is_submonoid (F ⋙ forget).sections := { one_mem := λ j j' f, begin simp only [functor.comp_map], erw is_ring_hom.map_one (CommRing.forget.map (F.map f)), refl, end, mul_mem := λ a b ah bh j j' f, begin simp only [functor.comp_map], erw is_ring_hom.map_mul (CommRing.forget.map (F.map f)), dsimp [functor.sections] at ah, rw ah f, dsimp [functor.sections] at bh, rw bh f, refl, end } instance sections_add_submonoid (F : J ⥤ CommRing.{u}) : is_add_submonoid (F ⋙ forget).sections := { zero_mem := λ j j' f, begin simp only [functor.comp_map], erw is_ring_hom.map_zero (CommRing.forget.map (F.map f)), refl, end, add_mem := λ a b ah bh j j' f, begin simp only [functor.comp_map], erw is_ring_hom.map_add (CommRing.forget.map (F.map f)), dsimp [functor.sections] at ah, rw ah f, dsimp [functor.sections] at bh, rw bh f, refl, end } instance sections_add_subgroup (F : J ⥤ CommRing.{u}) : is_add_subgroup (F ⋙ forget).sections := { neg_mem := λ a ah j j' f, begin simp only [functor.comp_map], erw is_ring_hom.map_neg (CommRing.forget.map (F.map f)), dsimp [functor.sections] at ah, rw ah f, refl, end, ..(CommRing.sections_add_submonoid F) } instance sections_subring (F : J ⥤ CommRing.{u}) : is_subring (F ⋙ forget).sections := { ..(CommRing.sections_submonoid F), ..(CommRing.sections_add_subgroup F) } instance limit_comm_ring (F : J ⥤ CommRing.{u}) : comm_ring (limit (F ⋙ forget)) := @subtype.comm_ring ((Π (j : J), (F ⋙ forget).obj j)) (by apply_instance) _ (by convert (CommRing.sections_subring F)) instance limit_π_is_ring_hom (F : J ⥤ CommRing.{u}) (j) : is_ring_hom (limit.π (F ⋙ CommRing.forget) j) := { map_one := by { simp only [types.types_limit_π], refl }, map_mul := λ x y, by { simp only [types.types_limit_π], refl }, map_add := λ x y, by { simp only [types.types_limit_π], refl } } def limit (F : J ⥤ CommRing.{u}) : cone F := { X := ⟨limit (F ⋙ forget), by apply_instance⟩, π := { app := λ j, ⟨limit.π (F ⋙ forget) j, by apply_instance⟩, naturality' := λ j j' f, subtype.eq ((limit.cone (F ⋙ forget)).π.naturality f) } } def limit_is_limit (F : J ⥤ CommRing.{u}) : is_limit (limit F) := begin refine is_limit.of_faithful forget (limit.is_limit _) (λ s, ⟨_, _⟩) (λ s, rfl), dsimp, split, { apply subtype.eq, funext, dsimp, erw is_ring_hom.map_one (CommRing.forget.map (s.π.app j)), refl }, { intros x y, apply subtype.eq, funext, dsimp, erw is_ring_hom.map_mul (CommRing.forget.map (s.π.app j)), refl }, { intros x y, apply subtype.eq, funext, dsimp, erw is_ring_hom.map_add (CommRing.forget.map (s.π.app j)), refl }, end instance CommRing_has_limits : has_limits.{u} CommRing.{u} := { has_limits_of_shape := λ J 𝒥, { has_limit := λ F, by exactI { cone := limit F, is_limit := limit_is_limit F } } } instance forget_preserves_limits : preserves_limits (forget : CommRing.{u} ⥤ Type u) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by exactI preserves_limit_of_preserves_limit_cone (limit.is_limit F) (limit.is_limit (F ⋙ forget)) } } end category_theory.instances.CommRing
6a81aa9af1094dfeb8320474a73683a8c5a75bf2
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/nat/basic.lean
0d9a25d629099c3213564c076653a228dafd197b
[ "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
55,271
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import logic.basic algebra.ordered_ring data.option.basic algebra.order_functions /-! # Basic operations on the natural numbers This files has some basic lemmas about natural numbers, definition of the `choice` function, and extra recursors: * `le_rec_on`, `le_induction`: recursion and induction principles starting at non-zero numbers. * `decreasing_induction` : recursion gowing downwards. * `strong_rec'` : recursion based on strong inequalities. -/ universes u v namespace nat variables {m n k : ℕ} -- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding -- during pattern matching. These lemmas package them back up as typeclass -- mediated operations. @[simp] theorem add_def {a b : ℕ} : nat.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℕ} : nat.mul a b = a * b := rfl attribute [simp] nat.add_sub_cancel nat.add_sub_cancel_left attribute [simp] nat.sub_self @[simp] lemma succ_pos' {n : ℕ} : 0 < succ n := succ_pos n theorem succ_inj' {n m : ℕ} : succ n = succ m ↔ n = m := ⟨succ_inj, congr_arg _⟩ theorem succ_le_succ_iff {m n : ℕ} : succ m ≤ succ n ↔ m ≤ n := ⟨le_of_succ_le_succ, succ_le_succ⟩ lemma zero_max {m : nat} : max 0 m = m := max_eq_right (zero_le _) theorem max_succ_succ {m n : ℕ} : max (succ m) (succ n) = succ (max m n) := begin by_cases h1 : m ≤ n, rw [max_eq_right h1, max_eq_right (succ_le_succ h1)], { rw not_le at h1, have h2 := le_of_lt h1, rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] } end lemma not_succ_lt_self {n : ℕ} : ¬succ n < n := not_lt_of_ge (nat.le_succ _) theorem lt_succ_iff {m n : ℕ} : m < succ n ↔ m ≤ n := succ_le_succ_iff lemma succ_le_iff {m n : ℕ} : succ m ≤ n ↔ m < n := ⟨lt_of_succ_le, succ_le_of_lt⟩ lemma lt_iff_add_one_le {m n : ℕ} : m < n ↔ m + 1 ≤ n := by rw succ_le_iff -- Just a restatement of `nat.lt_succ_iff` using `+1`. lemma lt_add_one_iff {a b : ℕ} : a < b + 1 ↔ a ≤ b := nat.lt_succ_iff -- A flipped version of `lt_add_one_iff`. lemma lt_one_add_iff {a b : ℕ} : a < 1 + b ↔ a ≤ b := by simp only [add_comm, nat.lt_succ_iff] theorem of_le_succ {n m : ℕ} (H : n ≤ m.succ) : n ≤ m ∨ n = m.succ := (lt_or_eq_of_le H).imp le_of_lt_succ id /-- Recursion starting at a non-zero number: given a map `C k → C (k+1)` for each `k`, there is a map from `C n` to each `C m`, `n ≤ m`. -/ @[elab_as_eliminator] def le_rec_on {C : ℕ → Sort u} {n : ℕ} : Π {m : ℕ}, n ≤ m → (Π {k}, C k → C (k+1)) → C n → C m | 0 H next x := eq.rec_on (eq_zero_of_le_zero H) x | (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next $ le_rec_on h @next x) (λ h : n = m + 1, eq.rec_on h x) theorem le_rec_on_self {C : ℕ → Sort u} {n} {h : n ≤ n} {next} (x : C n) : (le_rec_on h next x : C n) = x := by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self, dif_pos rfl] theorem le_rec_on_succ {C : ℕ → Sort u} {n m} (h1 : n ≤ m) {h2 : n ≤ m+1} {next} (x : C n) : (le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) := by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] } theorem le_rec_on_succ' {C : ℕ → Sort u} {n} {h : n ≤ n+1} {next} (x : C n) : (le_rec_on h next x : C (n+1)) = next x := by rw [le_rec_on_succ (le_refl n), le_rec_on_self] theorem le_rec_on_trans {C : ℕ → Sort u} {n m k} (hnm : n ≤ m) (hmk : m ≤ k) {next} (x : C n) : (le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) := begin induction hmk with k hmk ih, { rw le_rec_on_self }, rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ] end theorem le_rec_on_succ_left {C : ℕ → Sort u} {n m} (h1 : n ≤ m) (h2 : n+1 ≤ m) {next : Π{{k}}, C k → C (k+1)} (x : C n) : (le_rec_on h2 next (next x) : C m) = (le_rec_on h1 next x : C m) := begin rw [subsingleton.elim h1 (le_trans (le_succ n) h2), le_rec_on_trans (le_succ n) h2, le_rec_on_succ'] end theorem le_rec_on_injective {C : ℕ → Sort u} {n m} (hnm : n ≤ m) (next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.injective (next n)) : function.injective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H }, intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H) end theorem le_rec_on_surjective {C : ℕ → Sort u} {n m} (hnm : n ≤ m) (next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.surjective (next n)) : function.surjective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self }, intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ end theorem pred_eq_of_eq_succ {m n : ℕ} (H : m = n.succ) : m.pred = n := by simp [H] @[simp] lemma pred_eq_succ_iff {n m : ℕ} : pred n = succ m ↔ n = m + 2 := by cases n; split; rintro ⟨⟩; refl theorem pred_sub (n m : ℕ) : pred n - m = pred (n - m) := by rw [← sub_one, nat.sub_sub, one_add]; refl lemma pred_eq_sub_one (n : ℕ) : pred n = n - 1 := rfl lemma one_le_of_lt {n m : ℕ} (h : n < m) : 1 ≤ m := lt_of_le_of_lt (nat.zero_le _) h lemma le_pred_of_lt {n m : ℕ} (h : m < n) : m ≤ n - 1 := nat.sub_le_sub_right h 1 lemma le_of_pred_lt {m n : ℕ} : pred m < n → m ≤ n := match m with | 0 := le_of_lt | m+1 := id end /-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/ @[simp] lemma pred_one_add (n : ℕ) : pred (1 + n) = n := by rw [add_comm, add_one, pred_succ] theorem pos_iff_ne_zero : 0 < n ↔ n ≠ 0 := ⟨ne_of_gt, nat.pos_of_ne_zero⟩ lemma one_lt_iff_ne_zero_and_ne_one : ∀ {n : ℕ}, 1 < n ↔ n ≠ 0 ∧ n ≠ 1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := dec_trivial theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b := have h3 : a ≤ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3)) protected theorem le_sub_add (n m : ℕ) : n ≤ n - m + m := or.elim (le_total n m) (assume : n ≤ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end) (assume : m ≤ n, begin rw (nat.sub_add_cancel this) end) theorem sub_add_eq_max (n m : ℕ) : n - m + m = max n m := eq_max (nat.le_sub_add _ _) (le_add_left _ _) $ λ k h₁ h₂, by rw ← nat.sub_add_cancel h₂; exact add_le_add_right (nat.sub_le_sub_right h₁ _) _ theorem sub_add_min (n m : ℕ) : n - m + min n m = n := (le_total n m).elim (λ h, by rw [min_eq_left h, sub_eq_zero_of_le h, zero_add]) (λ h, by rw [min_eq_right h, nat.sub_add_cancel h]) protected theorem add_sub_cancel' {n m : ℕ} (h : m ≤ n) : m + (n - m) = n := by rw [add_comm, nat.sub_add_cancel h] protected theorem sub_eq_of_eq_add (h : k = m + n) : k - m = n := begin rw [h, nat.add_sub_cancel_left] end theorem sub_cancel {a b c : ℕ} (h₁ : a ≤ b) (h₂ : a ≤ c) (w : b - a = c - a) : b = c := by rw [←nat.sub_add_cancel h₁, ←nat.sub_add_cancel h₂, w] lemma sub_sub_sub_cancel_right {a b c : ℕ} (h₂ : c ≤ b) : (a - c) - (b - c) = a - b := by rw [nat.sub_sub, ←nat.add_sub_assoc h₂, nat.add_sub_cancel_left] lemma add_sub_cancel_right (n m k : ℕ) : n + (m + k) - k = n + m := by { rw [nat.add_sub_assoc, nat.add_sub_cancel], apply k.le_add_left } protected lemma sub_add_eq_add_sub {a b c : ℕ} (h : b ≤ a) : (a - b) + c = (a + c) - b := by rw [add_comm a, nat.add_sub_assoc h, add_comm] theorem sub_min (n m : ℕ) : n - min n m = n - m := nat.sub_eq_of_eq_add $ by rw [add_comm, sub_add_min] theorem sub_sub_assoc {a b c : ℕ} (h₁ : b ≤ a) (h₂ : c ≤ b) : a - (b - c) = a - b + c := (nat.sub_eq_iff_eq_add (le_trans (nat.sub_le _ _) h₁)).2 $ by rw [add_right_comm, add_assoc, nat.sub_add_cancel h₂, nat.sub_add_cancel h₁] protected theorem lt_of_sub_pos (h : 0 < n - m) : m < n := lt_of_not_ge (assume : n ≤ m, have n - m = 0, from sub_eq_zero_of_le this, begin rw this at h, exact lt_irrefl _ h end) protected theorem lt_of_sub_lt_sub_right : m - k < n - k → m < n := lt_imp_lt_of_le_imp_le (λ h, nat.sub_le_sub_right h _) protected theorem lt_of_sub_lt_sub_left : m - n < m - k → k < n := lt_imp_lt_of_le_imp_le (nat.sub_le_sub_left _) protected theorem sub_lt_self (h₁ : 0 < m) (h₂ : 0 < n) : m - n < m := calc m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos h₂] ... = pred m - pred n : by rw succ_sub_succ ... ≤ pred m : sub_le _ _ ... < succ (pred m) : lt_succ_self _ ... = m : succ_pred_eq_of_pos h₁ protected theorem le_sub_right_of_add_le (h : m + k ≤ n) : m ≤ n - k := by rw ← nat.add_sub_cancel m k; exact nat.sub_le_sub_right h k protected theorem le_sub_left_of_add_le (h : k + m ≤ n) : m ≤ n - k := nat.le_sub_right_of_add_le (by rwa add_comm at h) protected theorem lt_sub_right_of_add_lt (h : m + k < n) : m < n - k := lt_of_succ_le $ nat.le_sub_right_of_add_le $ by rw succ_add; exact succ_le_of_lt h protected theorem lt_sub_left_of_add_lt (h : k + m < n) : m < n - k := nat.lt_sub_right_of_add_lt (by rwa add_comm at h) protected theorem add_lt_of_lt_sub_right (h : m < n - k) : m + k < n := @nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel) protected theorem add_lt_of_lt_sub_left (h : m < n - k) : k + m < n := by rw add_comm; exact nat.add_lt_of_lt_sub_right h protected theorem le_add_of_sub_le_right : n - k ≤ m → n ≤ m + k := le_imp_le_of_lt_imp_lt nat.lt_sub_right_of_add_lt protected theorem le_add_of_sub_le_left : n - k ≤ m → n ≤ k + m := le_imp_le_of_lt_imp_lt nat.lt_sub_left_of_add_lt protected theorem lt_add_of_sub_lt_right : n - k < m → n < m + k := lt_imp_lt_of_le_imp_le nat.le_sub_right_of_add_le protected theorem lt_add_of_sub_lt_left : n - k < m → n < k + m := lt_imp_lt_of_le_imp_le nat.le_sub_left_of_add_le protected theorem sub_le_left_of_le_add : n ≤ k + m → n - k ≤ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_left protected theorem sub_le_right_of_le_add : n ≤ m + k → n - k ≤ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_right protected theorem sub_lt_left_iff_lt_add (H : n ≤ k) : k - n < m ↔ k < n + m := ⟨nat.lt_add_of_sub_lt_left, λ h₁, have succ k ≤ n + m, from succ_le_of_lt h₁, have succ (k - n) ≤ m, from calc succ (k - n) = succ k - n : by rw (succ_sub H) ... ≤ n + m - n : nat.sub_le_sub_right this n ... = m : by rw nat.add_sub_cancel_left, lt_of_succ_le this⟩ protected theorem le_sub_left_iff_add_le (H : m ≤ k) : n ≤ k - m ↔ m + n ≤ k := le_iff_le_iff_lt_iff_lt.2 (nat.sub_lt_left_iff_lt_add H) protected theorem le_sub_right_iff_add_le (H : n ≤ k) : m ≤ k - n ↔ m + n ≤ k := by rw [nat.le_sub_left_iff_add_le H, add_comm] protected theorem lt_sub_left_iff_add_lt : n < k - m ↔ m + n < k := ⟨nat.add_lt_of_lt_sub_left, nat.lt_sub_left_of_add_lt⟩ protected theorem lt_sub_right_iff_add_lt : m < k - n ↔ m + n < k := by rw [nat.lt_sub_left_iff_add_lt, add_comm] theorem sub_le_left_iff_le_add : m - n ≤ k ↔ m ≤ n + k := le_iff_le_iff_lt_iff_lt.2 nat.lt_sub_left_iff_add_lt theorem sub_le_right_iff_le_add : m - k ≤ n ↔ m ≤ n + k := by rw [nat.sub_le_left_iff_le_add, add_comm] protected theorem sub_lt_right_iff_lt_add (H : k ≤ m) : m - k < n ↔ m < n + k := by rw [nat.sub_lt_left_iff_lt_add H, add_comm] protected theorem sub_le_sub_left_iff (H : k ≤ m) : m - n ≤ m - k ↔ k ≤ n := ⟨λ h, have k + (m - k) - n ≤ m - k, by rwa nat.add_sub_cancel' H, nat.le_of_add_le_add_right (nat.le_add_of_sub_le_left this), nat.sub_le_sub_left _⟩ protected theorem sub_lt_sub_right_iff (H : k ≤ m) : m - k < n - k ↔ m < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_right_iff _ _ _ H) protected theorem sub_lt_sub_left_iff (H : n ≤ m) : m - n < m - k ↔ k < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_left_iff H) protected theorem sub_le_iff : m - n ≤ k ↔ m - k ≤ n := nat.sub_le_left_iff_le_add.trans nat.sub_le_right_iff_le_add.symm protected lemma sub_le_self (n m : ℕ) : n - m ≤ n := nat.sub_le_left_of_le_add (nat.le_add_left _ _) protected theorem sub_lt_iff (h₁ : n ≤ m) (h₂ : k ≤ m) : m - n < k ↔ m - k < n := (nat.sub_lt_left_iff_lt_add h₁).trans (nat.sub_lt_right_iff_lt_add h₂).symm lemma pred_le_iff {n m : ℕ} : pred n ≤ m ↔ n ≤ succ m := @nat.sub_le_right_iff_le_add n m 1 lemma lt_pred_iff {n m : ℕ} : n < pred m ↔ succ n < m := @nat.lt_sub_right_iff_add_lt n 1 m protected theorem mul_ne_zero {n m : ℕ} (n0 : n ≠ 0) (m0 : m ≠ 0) : n * m ≠ 0 | nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0 @[simp] protected theorem mul_eq_zero {a b : ℕ} : a * b = 0 ↔ a = 0 ∨ b = 0 := iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}) @[simp] protected theorem zero_eq_mul {a b : ℕ} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, nat.mul_eq_zero] lemma eq_zero_of_double_le {a : ℕ} (h : 2 * a ≤ a) : a = 0 := nat.eq_zero_of_le_zero $ by rwa [two_mul, nat.add_le_to_le_sub, nat.sub_self] at h; refl lemma eq_zero_of_mul_le {a b : ℕ} (hb : 2 ≤ b) (h : b * a ≤ a) : a = 0 := eq_zero_of_double_le $ le_trans (nat.mul_le_mul_right _ hb) h lemma le_mul_of_pos_left {m n : ℕ} (h : 0 < n) : m ≤ n * m := begin conv {to_lhs, rw [← one_mul(m)]}, exact mul_le_mul_of_nonneg_right (nat.succ_le_of_lt h) dec_trivial, end lemma le_mul_of_pos_right {m n : ℕ} (h : 0 < n) : m ≤ m * n := begin conv {to_lhs, rw [← mul_one(m)]}, exact mul_le_mul_of_nonneg_left (nat.succ_le_of_lt h) dec_trivial, end theorem two_mul_ne_two_mul_add_one {n m} : 2 * n ≠ 2 * m + 1 := mt (congr_arg (%2)) (by rw [add_comm, add_mul_mod_self_left, mul_mod_right]; exact dec_trivial) /-- Recursion principle based on `<`. -/ @[elab_as_eliminator] protected def strong_rec' {p : ℕ → Sort u} (H : ∀ n, (∀ m, m < n → p m) → p n) : ∀ (n : ℕ), p n | n := H n (λ m hm, strong_rec' m) attribute [simp] nat.div_self protected lemma div_le_of_le_mul' {m n : ℕ} {k} (h : m ≤ k * n) : m / k ≤ n := (eq_zero_or_pos k).elim (λ k0, by rw [k0, nat.div_zero]; apply zero_le) (λ k0, (decidable.mul_le_mul_left k0).1 $ calc k * (m / k) ≤ m % k + k * (m / k) : le_add_left _ _ ... = m : mod_add_div _ _ ... ≤ k * n : h) protected lemma div_le_self' (m n : ℕ) : m / n ≤ m := (eq_zero_or_pos n).elim (λ n0, by rw [n0, nat.div_zero]; apply zero_le) (λ n0, nat.div_le_of_le_mul' $ calc m = 1 * m : (one_mul _).symm ... ≤ n * m : mul_le_mul_right _ n0) theorem le_div_iff_mul_le' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := begin revert x, refine nat.strong_rec' _ y, clear y, intros y IH x, cases decidable.lt_or_le y k with h h, { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { rw succ_mul, exact iff_of_false (not_succ_le_zero _) (not_le_of_lt $ lt_of_lt_of_le h (le_add_left _ _)) } }, { rw [div_eq_sub_div k0 h], cases x with x, { simp [zero_mul, zero_le] }, { rw [← add_one, nat.add_le_add_iff_le_right, succ_mul, IH _ (sub_lt_of_pos_le _ _ k0 h), add_le_to_le_sub _ h] } } end theorem div_mul_le_self' (m n : ℕ) : m / n * n ≤ m := (nat.eq_zero_or_pos n).elim (λ n0, by simp [n0, zero_le]) $ λ n0, (le_div_iff_mul_le' n0).1 (le_refl _) theorem div_lt_iff_lt_mul' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x / k < y ↔ x < y * k := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0 protected theorem div_le_div_right {n m : ℕ} (h : n ≤ m) {k : ℕ} : n / k ≤ m / k := (nat.eq_zero_or_pos k).elim (λ k0, by simp [k0]) $ λ hk, (le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self' _ _) h lemma lt_of_div_lt_div {m n k : ℕ} (h : m / k < n / k) : m < n := by_contradiction $ λ h₁, absurd h (not_lt_of_ge (nat.div_le_div_right (not_lt.1 h₁))) protected theorem eq_mul_of_div_eq_right {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, nat.mul_div_cancel' H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2] protected theorem mul_div_cancel_left' {a b : ℕ} (Hd : a ∣ b) : a * (b / a) = b := by rw [mul_comm,nat.div_mul_cancel Hd] protected theorem div_mod_unique {n k m d : ℕ} (h : 0 < k) : n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k := ⟨λ ⟨e₁, e₂⟩, e₁ ▸ e₂ ▸ ⟨mod_add_div _ _, mod_lt _ h⟩, λ ⟨h₁, h₂⟩, h₁ ▸ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left]; simp [div_eq_of_lt, mod_eq_of_lt, h₂]⟩ lemma two_mul_odd_div_two {n : ℕ} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 := by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, nat.add_sub_cancel_left]} lemma div_dvd_of_dvd {a b : ℕ} (h : b ∣ a) : (a / b) ∣ a := ⟨b, (nat.div_mul_cancel h).symm⟩ protected lemma div_pos {a b : ℕ} (hba : b ≤ a) (hb : 0 < b) : 0 < a / b := nat.pos_of_ne_zero (λ h, lt_irrefl a (calc a = a % b : by simpa [h] using (mod_add_div a b).symm ... < b : nat.mod_lt a hb ... ≤ a : hba)) protected theorem mul_right_inj {a b c : ℕ} (ha : 0 < a) : b * a = c * a ↔ b = c := ⟨nat.eq_of_mul_eq_mul_right ha, λ e, e ▸ rfl⟩ protected theorem mul_left_inj {a b c : ℕ} (ha : 0 < a) : a * b = a * c ↔ b = c := ⟨nat.eq_of_mul_eq_mul_left ha, λ e, e ▸ rfl⟩ protected lemma div_div_self : ∀ {a b : ℕ}, b ∣ a → 0 < a → a / (a / b) = b | a 0 h₁ h₂ := by rw eq_zero_of_zero_dvd h₁; refl | 0 b h₁ h₂ := absurd h₂ dec_trivial | (a+1) (b+1) h₁ h₂ := (nat.mul_right_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $ by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁] protected lemma div_lt_of_lt_mul {m n k : ℕ} (h : m < n * k) : m / n < k := lt_of_mul_lt_mul_left (calc n * (m / n) ≤ m % n + n * (m / n) : nat.le_add_left _ _ ... = m : mod_add_div _ _ ... < n * k : h) (nat.zero_le n) lemma lt_mul_of_div_lt {a b c : ℕ} (h : a / c < b) (w : 0 < c) : a < b * c := lt_of_not_ge $ not_le_of_gt h ∘ (nat.le_div_iff_mul_le _ _ w).2 protected lemma div_eq_zero_iff {a b : ℕ} (hb : 0 < b) : a / b = 0 ↔ a < b := ⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb, λ h, by rw [← nat.mul_left_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div, mod_eq_of_lt h, mul_zero, add_zero]⟩ lemma eq_zero_of_le_div {a b : ℕ} (hb : 2 ≤ b) (h : a ≤ a / b) : a = 0 := eq_zero_of_mul_le hb $ by rw mul_comm; exact (nat.le_div_iff_mul_le' (lt_of_lt_of_le dec_trivial hb)).1 h lemma mul_div_le_mul_div_assoc (a b c : ℕ) : a * (b / c) ≤ (a * b) / c := if hc0 : c = 0 then by simp [hc0] else (nat.le_div_iff_mul_le _ _ (nat.pos_of_ne_zero hc0)).2 (by rw [mul_assoc]; exact mul_le_mul_left _ (nat.div_mul_le_self _ _)) lemma div_mul_div_le_div (a b c : ℕ) : ((a / c) * b) / a ≤ b / c := if ha0 : a = 0 then by simp [ha0] else calc a / c * b / a ≤ b * a / c / a : nat.div_le_div_right (by rw [mul_comm]; exact mul_div_le_mul_div_assoc _ _ _) ... = b / c : by rw [nat.div_div_eq_div_mul, mul_comm b, mul_comm c, nat.mul_div_mul _ _ (nat.pos_of_ne_zero ha0)] lemma eq_zero_of_le_half {a : ℕ} (h : a ≤ a / 2) : a = 0 := eq_zero_of_le_div (le_refl _) h lemma mod_mul_right_div_self (a b c : ℕ) : a % (b * c) / b = (a / b) % c := if hb : b = 0 then by simp [hb] else if hc : c = 0 then by simp [hc] else by conv {to_rhs, rw ← mod_add_div a (b * c)}; rw [mul_assoc, nat.add_mul_div_left _ _ (nat.pos_of_ne_zero hb), add_mul_mod_self_left, mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos (nat.pos_of_ne_zero hb) (nat.pos_of_ne_zero hc))))] lemma mod_mul_left_div_self (a b c : ℕ) : a % (c * b) / b = (a / b) % c := by rw [mul_comm c, mod_mul_right_div_self] /- The `n+1`-st triangle number is `n` more than the `n`-th triangle number -/ lemma triangle_succ (n : ℕ) : (n + 1) * ((n + 1) - 1) / 2 = n * (n - 1) / 2 + n := begin rw [← add_mul_div_left, mul_comm 2 n, ← mul_add, nat.add_sub_cancel, mul_comm], cases n; refl, apply zero_lt_succ end @[simp] protected theorem dvd_one {n : ℕ} : n ∣ 1 ↔ n = 1 := ⟨eq_one_of_dvd_one, λ e, e.symm ▸ dvd_refl _⟩ protected theorem dvd_add_left {k m n : ℕ} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m := (nat.dvd_add_iff_left h).symm protected theorem dvd_add_right {k m n : ℕ} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n := (nat.dvd_add_iff_right h).symm /-- A natural number m divides the sum m + n if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_left {m n : ℕ} : m ∣ m + n ↔ m ∣ n := nat.dvd_add_right (dvd_refl m) /-- A natural number m divides the sum n + m if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_right {m n : ℕ} : m ∣ n + m ↔ m ∣ n := nat.dvd_add_left (dvd_refl m) protected theorem mul_dvd_mul_iff_left {a b c : ℕ} (ha : 0 < a) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, nat.mul_left_inj ha] protected theorem mul_dvd_mul_iff_right {a b c : ℕ} (hc : 0 < c) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, nat.mul_right_inj hc] lemma succ_div : ∀ (a b : ℕ), (a + 1) / b = a / b + if b ∣ a + 1 then 1 else 0 | a 0 := by simp | 0 1 := rfl | 0 (b+2) := have hb2 : b + 2 > 1, from dec_trivial, by simp [ne_of_gt hb2, div_eq_of_lt hb2] | (a+1) (b+1) := begin rw [nat.div_def], conv_rhs { rw nat.div_def }, by_cases hb_eq_a : b = a + 1, { simp [hb_eq_a, le_refl] }, by_cases hb_le_a1 : b ≤ a + 1, { have hb_le_a : b ≤ a, from le_of_lt_succ (lt_of_le_of_ne hb_le_a1 hb_eq_a), have h₁ : (0 < b + 1 ∧ b + 1 ≤ a + 1 + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a1⟩, have h₂ : (0 < b + 1 ∧ b + 1 ≤ a + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a⟩, have dvd_iff : b + 1 ∣ a - b + 1 ↔ b + 1 ∣ a + 1 + 1, { rw [nat.dvd_add_iff_left (dvd_refl (b + 1)), ← nat.add_sub_add_right a 1 b, add_comm (_ - _), add_assoc, nat.sub_add_cancel (succ_le_succ hb_le_a)], simp }, have wf : a - b < a + 1, from lt_succ_of_le (nat.sub_le_self _ _), rw [if_pos h₁, if_pos h₂, nat.add_sub_add_right, nat.sub_add_comm hb_le_a, by exact have _ := wf, succ_div (a - b), nat.add_sub_add_right], simp [dvd_iff, succ_eq_add_one], congr }, { have hba : ¬ b ≤ a, from not_le_of_gt (lt_trans (lt_succ_self a) (lt_of_not_ge hb_le_a1)), have hb_dvd_a : ¬ b + 1 ∣ a + 2, from λ h, hb_le_a1 (le_of_succ_le_succ (le_of_dvd (succ_pos _) h)), simp [hba, hb_le_a1, hb_dvd_a], } end lemma succ_div_of_dvd {a b : ℕ} (hba : b ∣ a + 1) : (a + 1) / b = a / b + 1 := by rw [succ_div, if_pos hba] lemma succ_div_of_not_dvd {a b : ℕ} (hba : ¬ b ∣ a + 1) : (a + 1) / b = a / b := by rw [succ_div, if_neg hba, add_zero] @[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n := (eq_zero_or_pos n).elim (λ n0, by simp [n0]) (λ npos, mod_eq_of_lt (mod_lt _ npos)) @[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end theorem add_pos_left {m : ℕ} (h : 0 < m) (n : ℕ) : 0 < m + n := calc m + n > 0 + n : nat.add_lt_add_right h n ... = n : nat.zero_add n ... ≥ 0 : zero_le n theorem add_pos_right (m : ℕ) {n : ℕ} (h : 0 < n) : 0 < m + n := begin rw add_comm, exact add_pos_left h m end theorem add_pos_iff_pos_or_pos (m n : ℕ) : 0 < m + n ↔ 0 < m ∨ 0 < n := iff.intro begin intro h, cases m with m, {simp [zero_add] at h, exact or.inr h}, exact or.inl (succ_pos _) end begin intro h, cases h with mpos npos, { apply add_pos_left mpos }, apply add_pos_right _ npos end lemma add_eq_one_iff : ∀ {a b : ℕ}, a + b = 1 ↔ (a = 0 ∧ b = 1) ∨ (a = 1 ∧ b = 0) | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | 1 1 := dec_trivial | (a+2) _ := by rw add_right_comm; exact dec_trivial | _ (b+2) := by rw [← add_assoc]; simp only [nat.succ_inj', nat.succ_ne_zero]; simp lemma mul_eq_one_iff : ∀ {a b : ℕ}, a * b = 1 ↔ a = 1 ∧ b = 1 | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | (a+2) 0 := by simp | 0 (b+2) := by simp | (a+1) (b+1) := ⟨λ h, by simp only [add_mul, mul_add, mul_add, one_mul, mul_one, (add_assoc _ _ _).symm, nat.succ_inj', add_eq_zero_iff] at h; simp [h.1.2, h.2], by clear_aux_decl; finish⟩ lemma mul_right_eq_self_iff {a b : ℕ} (ha : 0 < a) : a * b = a ↔ b = 1 := suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this, nat.mul_left_inj ha lemma mul_left_eq_self_iff {a b : ℕ} (hb : 0 < b) : a * b = b ↔ a = 1 := by rw [mul_comm, nat.mul_right_eq_self_iff hb] lemma lt_succ_iff_lt_or_eq {n i : ℕ} : n < i.succ ↔ (n < i ∨ n = i) := lt_succ_iff.trans le_iff_lt_or_eq theorem le_zero_iff {i : ℕ} : i ≤ 0 ↔ i = 0 := ⟨nat.eq_zero_of_le_zero, assume h, h ▸ le_refl i⟩ theorem le_add_one_iff {i j : ℕ} : i ≤ j + 1 ↔ (i ≤ j ∨ i = j + 1) := ⟨assume h, match nat.eq_or_lt_of_le h with | or.inl h := or.inr h | or.inr h := or.inl $ nat.le_of_succ_le_succ h end, or.rec (assume h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩ theorem mul_self_inj {n m : ℕ} : n * n = m * m ↔ n = m := le_antisymm_iff.trans (le_antisymm_iff.trans (and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm instance decidable_ball_lt (n : nat) (P : Π k < n, Prop) : ∀ [H : ∀ n h, decidable (P n h)], decidable (∀ n h, P n h) := begin induction n with n IH; intro; resetI, { exact is_true (λ n, dec_trivial) }, cases IH (λ k h, P k (lt_succ_of_lt h)) with h, { refine is_false (mt _ h), intros hn k h, apply hn }, by_cases p : P n (lt_succ_self n), { exact is_true (λ k h', (lt_or_eq_of_le $ le_of_lt_succ h').elim (h _) (λ e, match k, e, h' with _, rfl, h := p end)) }, { exact is_false (mt (λ hn, hn _ _) p) } end instance decidable_forall_fin {n : ℕ} (P : fin n → Prop) [H : decidable_pred P] : decidable (∀ i, P i) := decidable_of_iff (∀ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, λ a k h, a ⟨k, h⟩⟩ instance decidable_ball_le (n : ℕ) (P : Π k ≤ n, Prop) [H : ∀ n h, decidable (P n h)] : decidable (∀ n h, P n h) := decidable_of_iff (∀ k (h : k < succ n), P k (le_of_lt_succ h)) ⟨λ a k h, a k (lt_succ_of_le h), λ a k h, a k _⟩ instance decidable_lo_hi (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x < hi → P x) := decidable_of_iff (∀ x < hi - lo, P (lo + x)) ⟨λal x hl hh, by have := al (x - lo) (lt_of_not_ge $ (not_congr (nat.sub_le_sub_right_iff _ _ _ hl)).2 $ not_le_of_gt hh); rwa [nat.add_sub_of_le hl] at this, λal x h, al _ (nat.le_add_right _ _) (nat.add_lt_of_lt_sub_left h)⟩ instance decidable_lo_hi_le (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x ≤ hi → P x) := decidable_of_iff (∀x, lo ≤ x → x < hi + 1 → P x) $ ball_congr $ λ x hl, imp_congr lt_succ_iff iff.rfl protected theorem bit0_le {n m : ℕ} (h : n ≤ m) : bit0 n ≤ bit0 m := add_le_add h h protected theorem bit1_le {n m : ℕ} (h : n ≤ m) : bit1 n ≤ bit1 m := succ_le_succ (add_le_add h h) theorem bit_le : ∀ (b : bool) {n m : ℕ}, n ≤ m → bit b n ≤ bit b m | tt n m h := nat.bit1_le h | ff n m h := nat.bit0_le h theorem bit_ne_zero (b) {n} (h : n ≠ 0) : bit b n ≠ 0 := by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _] theorem bit0_le_bit : ∀ (b) {m n : ℕ}, m ≤ n → bit0 m ≤ bit b n | tt m n h := le_of_lt $ nat.bit0_lt_bit1 h | ff m n h := nat.bit0_le h theorem bit_le_bit1 : ∀ (b) {m n : ℕ}, m ≤ n → bit b m ≤ bit1 n | ff m n h := le_of_lt $ nat.bit0_lt_bit1 h | tt m n h := nat.bit1_le h theorem bit_lt_bit0 : ∀ (b) {n m : ℕ}, n < m → bit b n < bit0 m | tt n m h := nat.bit1_lt_bit0 h | ff n m h := nat.bit0_lt h theorem bit_lt_bit (a b) {n m : ℕ} (h : n < m) : bit a n < bit b m := lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ (le_refl _)) /- partial subtraction -/ /-- Partial predecessor operation. Returns `ppred n = some m` if `n = m + 1`, otherwise `none`. -/ @[simp] def ppred : ℕ → option ℕ | 0 := none | (n+1) := some n /-- Partial subtraction operation. Returns `psub m n = some k` if `m = n + k`, otherwise `none`. -/ @[simp] def psub (m : ℕ) : ℕ → option ℕ | 0 := some m | (n+1) := psub n >>= ppred theorem pred_eq_ppred (n : ℕ) : pred n = (ppred n).get_or_else 0 := by cases n; refl theorem sub_eq_psub (m : ℕ) : ∀ n, m - n = (psub m n).get_or_else 0 | 0 := rfl | (n+1) := (pred_eq_ppred (m-n)).trans $ by rw [sub_eq_psub, psub]; cases psub m n; refl @[simp] theorem ppred_eq_some {m : ℕ} : ∀ {n}, ppred n = some m ↔ succ m = n | 0 := by split; intro h; contradiction | (n+1) := by dsimp; split; intro h; injection h; subst n @[simp] theorem ppred_eq_none : ∀ {n : ℕ}, ppred n = none ↔ n = 0 | 0 := by simp | (n+1) := by dsimp; split; contradiction theorem psub_eq_some {m : ℕ} : ∀ {n k}, psub m n = some k ↔ k + n = m | 0 k := by simp [eq_comm] | (n+1) k := by dsimp; apply option.bind_eq_some.trans; simp [psub_eq_some] theorem psub_eq_none (m n : ℕ) : psub m n = none ↔ m < n := begin cases s : psub m n; simp [eq_comm], { show m < n, refine lt_of_not_ge (λ h, _), cases le.dest h with k e, injection s.symm.trans (psub_eq_some.2 $ (add_comm _ _).trans e) }, { show n ≤ m, rw ← psub_eq_some.1 s, apply le_add_left } end theorem ppred_eq_pred {n} (h : 0 < n) : ppred n = some (pred n) := ppred_eq_some.2 $ succ_pred_eq_of_pos h theorem psub_eq_sub {m n} (h : n ≤ m) : psub m n = some (m - n) := psub_eq_some.2 $ nat.sub_add_cancel h theorem psub_add (m n k) : psub m (n + k) = do x ← psub m n, psub x k := by induction k; simp [*, add_succ, bind_assoc] /- pow -/ attribute [simp] nat.pow_zero nat.pow_one @[simp] lemma one_pow : ∀ n : ℕ, 1 ^ n = 1 | 0 := rfl | (k+1) := show 1^k * 1 = 1, by rw [mul_one, one_pow] theorem pow_add (a m n : ℕ) : a^(m + n) = a^m * a^n := by induction n; simp [*, pow_succ, mul_assoc] theorem pow_two (a : ℕ) : a ^ 2 = a * a := show (1 * a) * a = _, by rw one_mul theorem pow_dvd_pow (a : ℕ) {m n : ℕ} (h : m ≤ n) : a^m ∣ a^n := by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right theorem pow_dvd_pow_of_dvd {a b : ℕ} (h : a ∣ b) : ∀ n:ℕ, a^n ∣ b^n | 0 := dvd_refl _ | (n+1) := mul_dvd_mul (pow_dvd_pow_of_dvd n) h theorem mul_pow (a b n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := by induction n; simp [*, nat.pow_succ, mul_comm, mul_assoc, mul_left_comm] protected theorem pow_mul (a b n : ℕ) : n ^ (a * b) = (n ^ a) ^ b := by induction b; simp [*, nat.succ_eq_add_one, nat.pow_add, mul_add, mul_comm] theorem pow_pos {p : ℕ} (hp : 0 < p) : ∀ n : ℕ, 0 < p ^ n | 0 := by simp | (k+1) := mul_pos (pow_pos _) hp lemma pow_eq_mul_pow_sub (p : ℕ) {m n : ℕ} (h : m ≤ n) : p ^ m * p ^ (n - m) = p ^ n := by rw [←nat.pow_add, nat.add_sub_cancel' h] lemma pow_lt_pow_succ {p : ℕ} (h : 1 < p) (n : ℕ) : p^n < p^(n+1) := suffices p^n*1 < p^n*p, by simpa, nat.mul_lt_mul_of_pos_left h (nat.pow_pos (lt_of_succ_lt h) n) lemma lt_pow_self {p : ℕ} (h : 1 < p) : ∀ n : ℕ, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≤ p ^ (n+1) : pow_lt_pow_succ h _ lemma pow_right_strict_mono {x : ℕ} (k : 2 ≤ x) : strict_mono (nat.pow x) := λ _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : ℕ} (k : 2 ≤ x) : x^m ≤ x^n ↔ m ≤ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : ℕ} (k : 2 ≤ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : ℕ} (k : 2 ≤ x) : function.injective (nat.pow x) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : ℕ} (k : 1 ≤ m) : strict_mono (λ (x : ℕ), x^m) := λ _ _ h, pow_lt_pow_of_lt_left h k lemma pow_le_iff_le_left {m x y : ℕ} (k : 1 ≤ m) : x^m ≤ y^m ↔ x ≤ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : ℕ} (k : 1 ≤ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : ℕ} (k : 1 ≤ m) : function.injective (λ (x : ℕ), x^m) := strict_mono.injective (pow_left_strict_mono k) lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : 1 < p) (hk : 1 < k), ¬ p^k ∣ p | (succ p) (succ k) hp hk h := have (succ p)^k * succ p ∣ 1 * succ p, by simpa, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_right (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial @[simp] theorem bodd_div2_eq (n : ℕ) : bodd_div2 n = (bodd n, div2 n) := by unfold bodd div2; cases bodd_div2 n; refl @[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n @[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n /- iterate -/ section variables {α : Sort*} (op : α → α) @[simp] theorem iterate_zero (a : α) : op^[0] a = a := rfl @[simp] theorem iterate_succ (n : ℕ) (a : α) : op^[succ n] a = (op^[n]) (op a) := rfl theorem iterate_add : ∀ (m n : ℕ) (a : α), op^[m + n] a = (op^[m]) (op^[n] a) | m 0 a := rfl | m (succ n) a := iterate_add m n _ theorem iterate_succ' (n : ℕ) (a : α) : op^[succ n] a = op (op^[n] a) := by rw [← one_add, iterate_add]; refl theorem iterate₀ {α : Type u} {op : α → α} {x : α} (H : op x = x) {n : ℕ} : op^[n] x = x := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₁ {α : Type u} {β : Type v} {op : α → α} {op' : β → β} {op'' : α → β} (H : ∀ x, op' (op'' x) = op'' (op x)) {n : ℕ} {x : α} : op'^[n] (op'' x) = op'' (op^[n] x) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₂ {α : Type u} {op : α → α} {op' : α → α → α} (H : ∀ x y, op (op' x y) = op' (op x) (op y)) {n : ℕ} {x y : α} : op^[n] (op' x y) = op' (op^[n] x) (op^[n] y) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate_cancel {α : Type u} {op op' : α → α} (H : ∀ x, op (op' x) = x) {n : ℕ} {x : α} : op^[n] (op'^[n] x) = x := by induction n; [refl, rwa [iterate_succ, iterate_succ', H]] theorem iterate_inj {α : Type u} {op : α → α} (Hinj : function.injective op) (n : ℕ) (x y : α) (H : (op^[n] x) = (op^[n] y)) : x = y := by induction n with n ih; simp only [iterate_zero, iterate_succ'] at H; [exact H, exact ih (Hinj H)] end /- size and shift -/ theorem shiftl'_ne_zero_left (b) {m} (h : m ≠ 0) (n) : shiftl' b m n ≠ 0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : ∀ {n} (h : n ≠ 0), shiftl' tt m n ≠ 0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ @[simp] theorem size_zero : size 0 = 0 := rfl @[simp] theorem size_bit {b n} (h : bit b n ≠ 0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, refl end @[simp] theorem size_bit0 {n} (h : n ≠ 0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := by apply size_bit1 0 @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n ≠ 0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊢, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊢, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, have m0 := succ_inj (eq_one_of_dvd_one ⟨_, this.symm⟩), subst m0, simp at this, have : n = 0 := eq_zero_of_le_zero (le_of_not_gt $ λ hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), subst n, refl end @[simp] theorem size_shiftl {m} (h : m ≠ 0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : ℕ) : n < 2^size n := begin rw [← one_shiftl], have : ∀ {n}, n = 0 → n < shiftl 1 (size n) := λ n e, by subst e; exact dec_trivial, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : ℕ} : size m ≤ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, apply zero_le }, { intros b m IH n h, by_cases e : bit b m = 0, { rw e, apply zero_le }, rw [size_bit e], cases n with n, { exact e.elim (eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (λ h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : ℕ} : m < size n ↔ 2^m ≤ n := by rw [← not_lt, iff_not_comm, not_lt, size_le] theorem size_pos {n : ℕ} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : ℕ} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact not_iff_not.1 this theorem size_pow {n : ℕ} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_refl _) theorem size_le_size {m n : ℕ} (h : m ≤ n) : size m ≤ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) /- factorial -/ /-- `fact n` is the factorial of `n`. -/ @[simp] def fact : nat → nat | 0 := 1 | (succ n) := succ n * fact n @[simp] theorem fact_zero : fact 0 = 1 := rfl @[simp] theorem fact_one : fact 1 = 1 := rfl @[simp] theorem fact_succ (n) : fact (succ n) = succ n * fact n := rfl theorem fact_pos : ∀ n, 0 < fact n | 0 := zero_lt_one | (succ n) := mul_pos (succ_pos _) (fact_pos n) theorem fact_ne_zero (n : ℕ) : fact n ≠ 0 := ne_of_gt (fact_pos _) theorem fact_dvd_fact {m n} (h : m ≤ n) : fact m ∣ fact n := begin induction n with n IH; simp, { have := eq_zero_of_le_zero h, subst m, simp }, { cases eq_or_lt_of_le h with he hl, { subst m, simp }, { apply dvd_mul_of_dvd_right (IH (le_of_lt_succ hl)) } } end theorem dvd_fact : ∀ {m n}, 0 < m → m ≤ n → m ∣ fact n | (succ m) n _ h := dvd_of_mul_right_dvd (fact_dvd_fact h) theorem fact_le {m n} (h : m ≤ n) : fact m ≤ fact n := le_of_dvd (fact_pos _) (fact_dvd_fact h) lemma fact_mul_pow_le_fact : ∀ {m n : ℕ}, m.fact * m.succ ^ n ≤ (m + n).fact | m 0 := by simp | m (n+1) := by rw [← add_assoc, nat.fact_succ, mul_comm (nat.succ _), nat.pow_succ, ← mul_assoc]; exact mul_le_mul fact_mul_pow_le_fact (nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _) lemma monotone_fact : monotone fact := λ n m, fact_le lemma fact_lt (h0 : 0 < n) : n.fact < m.fact ↔ n < m := begin split; intro h, { rw [← not_le], intro hmn, apply not_le_of_lt h (fact_le hmn) }, { have : ∀(n : ℕ), 0 < n → n.fact < n.succ.fact, { intros k hk, rw [fact_succ, succ_mul, lt_add_iff_pos_left], apply mul_pos hk (fact_pos k) }, induction h generalizing h0, { exact this _ h0, }, { refine lt_trans (h_ih h0) (this _ _), exact lt_trans h0 (lt_of_succ_le h_a) }} end lemma one_lt_fact : 1 < n.fact ↔ 1 < n := by { convert fact_lt _, refl, exact one_pos } lemma fact_eq_one : n.fact = 1 ↔ n ≤ 1 := begin split; intro h, { rw [← not_lt, ← one_lt_fact, h], apply lt_irrefl }, { cases h with h h, refl, cases h, refl } end lemma fact_inj (h0 : 1 < n.fact) : n.fact = m.fact ↔ n = m := begin split; intro h, { rcases lt_trichotomy n m with hnm|hnm|hnm, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [one_lt_fact] at h0, exact lt_trans one_pos h0 }, { exact hnm }, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [h, one_lt_fact] at h0, exact lt_trans one_pos h0 }}, { rw h } end /- choose -/ /-- `choose n k` is the number of `k`-element subsets in an `n`-element set. Also known as binomial coefficients. -/ def choose : ℕ → ℕ → ℕ | _ 0 := 1 | 0 (k + 1) := 0 | (n + 1) (k + 1) := choose n k + choose n (succ k) @[simp] lemma choose_zero_right (n : ℕ) : choose n 0 = 1 := by cases n; refl @[simp] lemma choose_zero_succ (k : ℕ) : choose 0 (succ k) = 0 := rfl lemma choose_succ_succ (n k : ℕ) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl lemma choose_eq_zero_of_lt : ∀ {n k}, n < k → choose n k = 0 | _ 0 hk := absurd hk dec_trivial | 0 (k + 1) hk := choose_zero_succ _ | (n + 1) (k + 1) hk := have hnk : n < k, from lt_of_succ_lt_succ hk, have hnk1 : n < k + 1, from lt_of_succ_lt hk, by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1] @[simp] lemma choose_self (n : ℕ) : choose n n = 1 := by induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)] @[simp] lemma choose_succ_self (n : ℕ) : choose n (succ n) = 0 := choose_eq_zero_of_lt (lt_succ_self _) @[simp] lemma choose_one_right (n : ℕ) : choose n 1 = n := by induction n; simp [*, choose] /-- `choose n 2` is the `n`-th triangle number. -/ lemma choose_two_right (n : ℕ) : choose n 2 = n * (n - 1) / 2 := by { induction n, simp, simpa [n_ih, choose, add_one] using (triangle_succ n_n).symm } lemma choose_pos : ∀ {n k}, k ≤ n → 0 < choose n k | 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial | (n + 1) 0 hk := by simp; exact dec_trivial | (n + 1) (k + 1) hk := by rw choose_succ_succ; exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (nat.zero_le _) lemma succ_mul_choose_eq : ∀ n k, succ n * choose n k = choose (succ n) (succ k) * succ k | 0 0 := dec_trivial | 0 (k + 1) := by simp [choose] | (n + 1) 0 := by simp | (n + 1) (k + 1) := by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ, ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul] lemma choose_mul_fact_mul_fact : ∀ {n k}, k ≤ n → choose n k * fact k * fact (n - k) = fact n | 0 _ hk := by simp [eq_zero_of_le_zero hk] | (n + 1) 0 hk := by simp | (n + 1) (succ k) hk := begin cases lt_or_eq_of_le hk with hk₁ hk₁, { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n := by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk); simp [fact_succ, mul_comm, mul_left_comm], have h₁ : fact (n - k) = (n - k) * fact (n - succ k) := by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ], have h₂ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n := by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁); simp [fact_succ, mul_comm, mul_left_comm, mul_assoc], have h₃ : k * fact n ≤ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk), rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, h₂, ← add_one, add_mul, nat.mul_sub_right_distrib, fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] }, { simp [hk₁, mul_comm, choose, nat.sub_self] } end theorem choose_eq_fact_div_fact {n k : ℕ} (hk : k ≤ n) : choose n k = fact n / (fact k * fact (n - k)) := begin have : fact n = choose n k * (fact k * fact (n - k)) := by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm, exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm end theorem fact_mul_fact_dvd_fact {n k : ℕ} (hk : k ≤ n) : fact k * fact (n - k) ∣ fact n := by rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _ @[simp] lemma choose_symm {n k : ℕ} (hk : k ≤ n) : choose n (n-k) = choose n k := by rw [choose_eq_fact_div_fact hk, choose_eq_fact_div_fact (sub_le _ _), nat.sub_sub_self hk, mul_comm] lemma choose_succ_right_eq {n k : ℕ} : choose n (k + 1) * (k + 1) = choose n k * (n - k) := begin have e : (n+1) * choose n k = choose n k * (k+1) + choose n (k+1) * (k+1), rw [← right_distrib, ← choose_succ_succ, succ_mul_choose_eq], rw [← nat.sub_eq_of_eq_add e, mul_comm, ← nat.mul_sub_left_distrib, nat.add_sub_add_right] end section find_greatest /-- `find_greatest P b` is the largest `i ≤ bound` such that `P i` holds, or `0` if no such `i` exists -/ protected def find_greatest (P : ℕ → Prop) [decidable_pred P] : ℕ → ℕ | 0 := 0 | (n + 1) := if P (n + 1) then n + 1 else find_greatest n variables {P : ℕ → Prop} [decidable_pred P] @[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl @[simp] lemma find_greatest_eq : ∀{b}, P b → nat.find_greatest P b = b | 0 h := rfl | (n + 1) h := by simp [nat.find_greatest, h] @[simp] lemma find_greatest_of_not {b} (h : ¬ P (b + 1)) : nat.find_greatest P (b + 1) = nat.find_greatest P b := by simp [nat.find_greatest, h] lemma find_greatest_spec_and_le : ∀{b m}, m ≤ b → P m → P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b | 0 m hm hP := have m = 0, from le_antisymm hm (nat.zero_le _), show P 0 ∧ m ≤ 0, from this ▸ ⟨hP, le_refl _⟩ | (b + 1) m hm hP := begin by_cases h : P (b + 1), { simp [h, hm] }, { have : m ≠ b + 1 := assume this, h $ this ▸ hP, have : m ≤ b := (le_of_not_gt $ assume h : b + 1 ≤ m, this $ le_antisymm hm h), have : P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b := find_greatest_spec_and_le this hP, simp [h, this] } end lemma find_greatest_spec {b} : (∃m, m ≤ b ∧ P m) → P (nat.find_greatest P b) | ⟨m, hmb, hm⟩ := (find_greatest_spec_and_le hmb hm).1 lemma find_greatest_le : ∀ {b}, nat.find_greatest P b ≤ b | 0 := le_refl _ | (b + 1) := have nat.find_greatest P b ≤ b + 1, from le_trans find_greatest_le (nat.le_succ b), by by_cases P (b + 1); simp [h, this] lemma le_find_greatest {b m} (hmb : m ≤ b) (hm : P m) : m ≤ nat.find_greatest P b := (find_greatest_spec_and_le hmb hm).2 lemma find_greatest_is_greatest {P : ℕ → Prop} [decidable_pred P] {b} : (∃ m, m ≤ b ∧ P m) → ∀ k, nat.find_greatest P b < k ∧ k ≤ b → ¬ P k | ⟨m, hmb, hP⟩ k ⟨hk, hkb⟩ hPk := lt_irrefl k $ lt_of_le_of_lt (le_find_greatest hkb hPk) hk lemma find_greatest_eq_zero {P : ℕ → Prop} [decidable_pred P] : ∀ {b}, (∀ n ≤ b, ¬ P n) → nat.find_greatest P b = 0 | 0 h := find_greatest_zero | (n + 1) h := begin have := nat.find_greatest_of_not (h (n + 1) (le_refl _)), rw this, exact find_greatest_eq_zero (assume k hk, h k (le_trans hk $ nat.le_succ _)) end lemma find_greatest_of_ne_zero {P : ℕ → Prop} [decidable_pred P] : ∀ {b m}, nat.find_greatest P b = m → m ≠ 0 → P m | 0 m rfl h := by { have := @find_greatest_zero P _, contradiction } | (b + 1) m rfl h := decidable.by_cases (assume hb : P (b + 1), by { have := find_greatest_eq hb, rw this, exact hb }) (assume hb : ¬ P (b + 1), find_greatest_of_ne_zero (find_greatest_of_not hb).symm h) end find_greatest section div lemma dvd_div_of_mul_dvd {a b c : ℕ} (h : a * b ∣ c) : b ∣ c / a := if ha : a = 0 then by simp [ha] else have ha : 0 < a, from nat.pos_of_ne_zero ha, have h1 : ∃ d, c = a * b * d, from h, let ⟨d, hd⟩ := h1 in have hac : a ∣ c, from dvd_of_mul_right_dvd h, have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd), show ∃ d, c / a = b * d, from ⟨d, h2⟩ lemma mul_dvd_of_dvd_div {a b c : ℕ} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b := have h1 : ∃ d, b / c = a * d, from h, have h2 : ∃ e, b = c * e, from hab, let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in have h3 : b = a * d * c, from nat.eq_mul_of_div_eq_left hab hd, show ∃ d, b = c * a * d, from ⟨d, by cc⟩ lemma div_mul_div {a b c d : ℕ} (hab : b ∣ a) (hcd : d ∣ c) : (a / b) * (c / d) = (a * c) / (b * d) := have exi1 : ∃ x, a = b * x, from hab, have exi2 : ∃ y, c = d * y, from hcd, if hb : b = 0 then by simp [hb] else have 0 < b, from nat.pos_of_ne_zero hb, if hd : d = 0 then by simp [hd] else have 0 < d, from nat.pos_of_ne_zero hd, begin cases exi1 with x hx, cases exi2 with y hy, rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left], symmetry, apply nat.div_eq_of_eq_mul_left, apply mul_pos, repeat {assumption}, cc end lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn, dvd_trans this hdiv lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma eq_of_dvd_of_div_eq_one {a b : ℕ} (w : a ∣ b) (h : b / a = 1) : a = b := by rw [←nat.div_mul_cancel w, h, one_mul] lemma eq_zero_of_dvd_of_div_eq_zero {a b : ℕ} (w : a ∣ b) (h : b / a = 0) : b = 0 := by rw [←nat.div_mul_cancel w, h, zero_mul] lemma div_le_div_left {a b c : ℕ} (h₁ : c ≤ b) (h₂ : 0 < c) : a / b ≤ a / c := (nat.le_div_iff_mul_le _ _ h₂).2 $ le_trans (mul_le_mul_left _ h₁) (div_mul_le_self _ _) lemma div_eq_self {a b : ℕ} : a / b = a ↔ a = 0 ∨ b = 1 := begin split, { intro, cases b, { simp * at * }, { cases b, { right, refl }, { left, have : a / (b + 2) ≤ a / 2 := div_le_div_left (by simp) dec_trivial, refine eq_zero_of_le_half _, simp * at * } } }, { rintros (rfl|rfl); simp } end end div lemma exists_eq_add_of_le : ∀ {m n : ℕ}, m ≤ n → ∃ k : ℕ, n = m + k | 0 0 h := ⟨0, by simp⟩ | 0 (n+1) h := ⟨n+1, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ lemma exists_eq_add_of_lt : ∀ {m n : ℕ}, m < n → ∃ k : ℕ, n = m + k + 1 | 0 0 h := false.elim $ lt_irrefl _ h | 0 (n+1) h := ⟨n, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ lemma with_bot.add_eq_zero_iff : ∀ {n m : with_bot ℕ}, n + m = 0 ↔ n = 0 ∧ m = 0 | none m := iff_of_false dec_trivial (λ h, absurd h.1 dec_trivial) | n none := iff_of_false (by cases n; exact dec_trivial) (λ h, absurd h.2 dec_trivial) | (some n) (some m) := show (n + m : with_bot ℕ) = (0 : ℕ) ↔ (n : with_bot ℕ) = (0 : ℕ) ∧ (m : with_bot ℕ) = (0 : ℕ), by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)] lemma with_bot.add_eq_one_iff : ∀ {n m : with_bot ℕ}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0) | none none := dec_trivial | none (some m) := dec_trivial | (some n) none := iff_of_false dec_trivial (λ h, h.elim (λ h, absurd h.2 dec_trivial) (λ h, absurd h.2 dec_trivial)) | (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp | (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero] -- induction /-- Induction principle starting at a non-zero number. For maps to a `Sort*` see `le_rec_on`. -/ @[elab_as_eliminator] lemma le_induction {P : nat → Prop} {m} (h0 : P m) (h1 : ∀ n, m ≤ n → P n → P (n + 1)) : ∀ n, m ≤ n → P n := by apply nat.less_than_or_equal.rec h0; exact h1 /-- Decreasing induction: if `P (k+1)` implies `P k`, then `P n` implies `P m` for all `m ≤ n`. Also works for functions to `Sort*`. -/ @[elab_as_eliminator] def decreasing_induction {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n) (hP : P n) : P m := le_rec_on mn (λ k ih hsk, ih $ h k hsk) (λ h, h) hP @[simp] lemma decreasing_induction_self {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {n : ℕ} (nn : n ≤ n) (hP : P n) : (decreasing_induction h nn hP : P n) = hP := by { dunfold decreasing_induction, rw [le_rec_on_self] } lemma decreasing_induction_succ {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n) (msn : m ≤ n + 1) (hP : P (n+1)) : (decreasing_induction h msn hP : P m) = decreasing_induction h mn (h n hP) := by { dunfold decreasing_induction, rw [le_rec_on_succ] } @[simp] lemma decreasing_induction_succ' {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m : ℕ} (msm : m ≤ m + 1) (hP : P (m+1)) : (decreasing_induction h msm hP : P m) = h m hP := by { dunfold decreasing_induction, rw [le_rec_on_succ'] } lemma decreasing_induction_trans {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n k : ℕ} (mn : m ≤ n) (nk : n ≤ k) (hP : P k) : (decreasing_induction h (le_trans mn nk) hP : P m) = decreasing_induction h mn (decreasing_induction h nk hP) := by { induction nk with k nk ih, rw [decreasing_induction_self], rw [decreasing_induction_succ h (le_trans mn nk), ih, decreasing_induction_succ] } lemma decreasing_induction_succ_left {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (smn : m + 1 ≤ n) (mn : m ≤ n) (hP : P n) : (decreasing_induction h mn hP : P m) = h m (decreasing_induction h smn hP) := by { rw [subsingleton.elim mn (le_trans (le_succ m) smn), decreasing_induction_trans, decreasing_induction_succ'] } end nat
53219bd63f91e49ea6cf664795d816920f97b755
367134ba5a65885e863bdc4507601606690974c1
/src/analysis/convex/caratheodory.lean
5c752c7fa6892bdf0d191aaf57e104d25d534e2a
[ "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
8,962
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import analysis.convex.basic import linear_algebra.finite_dimensional /-! # Carathéodory's convexity theorem This file is devoted to proving Carathéodory's convexity theorem: The convex hull of a set `s` in ℝᵈ is the union of the convex hulls of the (d+1)-tuples in `s`. ## Main results: * `convex_hull_eq_union`: Carathéodory's convexity theorem ## Implementation details This theorem was formalized as part of the Sphere Eversion project. ## Tags convex hull, caratheodory -/ universes u open set finset finite_dimensional open_locale big_operators variables {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] namespace caratheodory /-- If `x` is in the convex hull of some finset `t` with strictly more than `findim + 1` elements, then it is in the union of the convex hulls of the finsets `t.erase y` for `y ∈ t`. -/ lemma mem_convex_hull_erase [decidable_eq E] {t : finset E} (h : findim ℝ E + 1 < t.card) {x : E} (m : x ∈ convex_hull (↑t : set E)) : ∃ (y : (↑t : set E)), x ∈ convex_hull (↑(t.erase y) : set E) := begin simp only [finset.convex_hull_eq, mem_set_of_eq] at m ⊢, obtain ⟨f, fpos, fsum, rfl⟩ := m, obtain ⟨g, gcombo, gsum, gpos⟩ := exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card h, clear h, let s := t.filter (λ z : E, 0 < g z), obtain ⟨i₀, mem, w⟩ : ∃ i₀ ∈ s, ∀ i ∈ s, f i₀ / g i₀ ≤ f i / g i, { apply s.exists_min_image (λ z, f z / g z), obtain ⟨x, hx, hgx⟩ : ∃ x ∈ t, 0 < g x := gpos, exact ⟨x, mem_filter.mpr ⟨hx, hgx⟩⟩, }, have hg : 0 < g i₀ := by { rw mem_filter at mem, exact mem.2 }, have hi₀ : i₀ ∈ t := filter_subset _ _ mem, let k : E → ℝ := λ z, f z - (f i₀ / g i₀) * g z, have hk : k i₀ = 0 := by field_simp [k, ne_of_gt hg], have ksum : ∑ e in t.erase i₀, k e = 1, { calc ∑ e in t.erase i₀, k e = ∑ e in t, k e : by conv_rhs { rw [← insert_erase hi₀, sum_insert (not_mem_erase i₀ t), hk, zero_add], } ... = ∑ e in t, (f e - f i₀ / g i₀ * g e) : rfl ... = 1 : by rw [sum_sub_distrib, fsum, ← mul_sum, gsum, mul_zero, sub_zero] }, refine ⟨⟨i₀, hi₀⟩, k, _, ksum, _⟩, { simp only [and_imp, sub_nonneg, mem_erase, ne.def, subtype.coe_mk], intros e hei₀ het, by_cases hes : e ∈ s, { have hge : 0 < g e := by { rw mem_filter at hes, exact hes.2 }, rw ← le_div_iff hge, exact w _ hes, }, { calc _ ≤ 0 : mul_nonpos_of_nonneg_of_nonpos _ _ -- prove two goals below ... ≤ f e : fpos e het, { apply div_nonneg (fpos i₀ (mem_of_subset (filter_subset _ t) mem)) (le_of_lt hg) }, { simpa only [mem_filter, het, true_and, not_lt] using hes }, } }, { simp only [subtype.coe_mk, center_mass_eq_of_sum_1 _ id ksum, id], calc ∑ e in t.erase i₀, k e • e = ∑ e in t, k e • e : by conv_rhs { rw [← insert_erase hi₀, sum_insert (not_mem_erase i₀ t), hk, zero_smul, zero_add], } ... = ∑ e in t, (f e - f i₀ / g i₀ * g e) • e : rfl ... = t.center_mass f id : _, simp only [sub_smul, mul_smul, sum_sub_distrib, ← smul_sum, gcombo, smul_zero, sub_zero, center_mass, fsum, inv_one, one_smul, id.def], }, end /-- The convex hull of a finset `t` with `findim ℝ E + 1 < t.card` is equal to the union of the convex hulls of the finsets `t.erase x` for `x ∈ t`. -/ lemma step [decidable_eq E] (t : finset E) (h : findim ℝ E + 1 < t.card) : convex_hull (↑t : set E) = ⋃ (x : (↑t : set E)), convex_hull ↑(t.erase x) := begin apply set.subset.antisymm, { intros x m', obtain ⟨y, m⟩ := mem_convex_hull_erase h m', exact mem_Union.2 ⟨y, m⟩, }, { refine Union_subset _, intro x, apply convex_hull_mono, apply erase_subset, } end /-- The convex hull of a finset `t` with `findim ℝ E + 1 < t.card` is contained in the union of the convex hulls of the finsets `t' ⊆ t` with `t'.card ≤ findim ℝ E + 1`. -/ lemma shrink' (t : finset E) (k : ℕ) (h : t.card = findim ℝ E + 1 + k) : convex_hull (↑t : set E) ⊆ ⋃ (t' : finset E) (w : t' ⊆ t) (b : t'.card ≤ findim ℝ E + 1), convex_hull ↑t' := begin induction k with k ih generalizing t, { apply subset_subset_Union t, apply subset_subset_Union (set.subset.refl _), exact subset_subset_Union (le_of_eq h) (subset.refl _), }, { classical, rw step _ (by { rw h, simp, } : findim ℝ E + 1 < t.card), apply Union_subset, intro i, transitivity, { apply ih, rw [card_erase_of_mem, h, nat.pred_succ], exact i.2, }, { apply Union_subset_Union, intro t', apply Union_subset_Union_const, exact λ h, set.subset.trans h (erase_subset _ _), } } end /-- The convex hull of any finset `t` is contained in the union of the convex hulls of the finsets `t' ⊆ t` with `t'.card ≤ findim ℝ E + 1`. -/ lemma shrink (t : finset E) : convex_hull (↑t : set E) ⊆ ⋃ (t' : finset E) (w : t' ⊆ t) (b : t'.card ≤ findim ℝ E + 1), convex_hull ↑t' := begin by_cases h : t.card ≤ findim ℝ E + 1, { apply subset_subset_Union t, apply subset_subset_Union (set.subset.refl _), exact subset_subset_Union h (set.subset.refl _), }, push_neg at h, obtain ⟨k, w⟩ := le_iff_exists_add.mp (le_of_lt h), clear h, exact shrink' _ _ w, end end caratheodory /-- One inclusion of Carathéodory's convexity theorem. The convex hull of a set `s` in ℝᵈ is contained in the union of the convex hulls of the (d+1)-tuples in `s`. -/ lemma convex_hull_subset_union (s : set E) : convex_hull s ⊆ ⋃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1), convex_hull ↑t := begin -- First we replace `convex_hull s` with the union of the convex hulls of finite subsets, rw convex_hull_eq_union_convex_hull_finite_subsets, -- and prove the inclusion for each of those. apply Union_subset, intro r, apply Union_subset, intro h, -- Second, for each convex hull of a finite subset, we shrink it. refine subset.trans (caratheodory.shrink _) _, -- After that it's just shuffling unions around. refine Union_subset_Union (λ t, _), exact Union_subset_Union2 (λ htr, ⟨subset.trans htr h, subset.refl _⟩) end /-- Carathéodory's convexity theorem. The convex hull of a set `s` in ℝᵈ is the union of the convex hulls of the (d+1)-tuples in `s`. -/ theorem convex_hull_eq_union (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1), convex_hull ↑t := begin apply set.subset.antisymm, { apply convex_hull_subset_union, }, iterate 3 { convert Union_subset _, intro, }, exact convex_hull_mono ‹_›, end /-- A more explicit formulation of Carathéodory's convexity theorem, writing an element of a convex hull as the center of mass of an explicit `finset` with cardinality at most `dim + 1`. -/ theorem eq_center_mass_card_le_dim_succ_of_mem_convex_hull {s : set E} {x : E} (h : x ∈ convex_hull s) : ∃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1) (f : E → ℝ), (∀ y ∈ t, 0 ≤ f y) ∧ t.sum f = 1 ∧ t.center_mass f id = x := begin rw convex_hull_eq_union at h, simp only [exists_prop, mem_Union] at h, obtain ⟨t, w, b, m⟩ := h, refine ⟨t, w, b, _⟩, rw finset.convex_hull_eq at m, simpa only [exists_prop] using m, end /-- A variation on Carathéodory's convexity theorem, writing an element of a convex hull as a center of mass of an explicit `finset` with cardinality at most `dim + 1`, where all coefficients in the center of mass formula are strictly positive. (This is proved using `eq_center_mass_card_le_dim_succ_of_mem_convex_hull`, and discarding any elements of the set with coefficient zero.) -/ theorem eq_pos_center_mass_card_le_dim_succ_of_mem_convex_hull {s : set E} {x : E} (h : x ∈ convex_hull s) : ∃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1) (f : E → ℝ), (∀ y ∈ t, 0 < f y) ∧ t.sum f = 1 ∧ t.center_mass f id = x := begin obtain ⟨t, w, b, f, ⟨pos, sum, center⟩⟩ := eq_center_mass_card_le_dim_succ_of_mem_convex_hull h, let t' := t.filter (λ z, 0 < f z), have t'sum : t'.sum f = 1, { rw ← sum, exact sum_filter_of_ne (λ x hxt hfx, (pos x hxt).lt_of_ne hfx.symm) }, refine ⟨t', _, _, f, ⟨_, t'sum, _⟩⟩, { exact subset.trans (filter_subset _ t) w, }, { exact (card_filter_le _ _).trans b, }, { exact λ y H, (mem_filter.mp H).right, }, { rw ← center, simp only [center_mass, t'sum, sum, inv_one, one_smul, id.def], refine sum_filter_of_ne (λ x hxt hfx, (pos x hxt).lt_of_ne $ λ hf₀, _), rw [← hf₀, zero_smul] at hfx, exact hfx rfl }, end
7e41fe0523b568d902a4a60c1d07f9918cf56a93
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/algebra/group_with_zero_auto.lean
bac987bfa1a273b2356c1c8f9927e38cce5caffa
[]
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
9,441
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.algebra.monoid import Mathlib.algebra.group.pi import Mathlib.PostPort universes u_1 u_2 u_3 l namespace Mathlib /-! # Topological group with zero In this file we define `has_continuous_inv'` to be a mixin typeclass a type with `has_inv` and `has_zero` (e.g., a `group_with_zero`) such that `λ x, x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. Currently the only example of `has_continuous_inv'` in `mathlib` which is not a normed field is the type `nnnreal` (a.k.a. `ℝ≥0`) of nonnegative real numbers. Then we prove lemmas about continuity of `x ↦ x⁻¹` and `f / g` providing dot-style `*.inv'` and `*.div` operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. As a special case, we provide `*.div_const` operations that require only `group_with_zero` and `has_continuous_mul` instances. All lemmas about `(⁻¹)` use `inv'` in their names because lemmas without `'` are used for `topological_group`s. We also use `'` in the typeclass name `has_continuous_inv'` for the sake of consistency of notation. -/ /-! ### A group with zero with continuous multiplication If `G₀` is a group with zero with continuous `(*)`, then `(/y)` is continuous for any `y`. In this section we prove lemmas that immediately follow from this fact providing `*.div_const` dot-style operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. -/ theorem filter.tendsto.div_const {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} {l : filter α} {x : G₀} {y : G₀} (hf : filter.tendsto f l (nhds x)) : filter.tendsto (fun (a : α) => f a / y) l (nhds (x / y)) := sorry theorem continuous_at.div_const {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} [topological_space α] (hf : continuous f) {y : G₀} : continuous fun (x : α) => f x / y := sorry theorem continuous_within_at.div_const {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} {s : set α} [topological_space α] {a : α} (hf : continuous_within_at f s a) {y : G₀} : continuous_within_at (fun (x : α) => f x / y) s a := filter.tendsto.div_const hf theorem continuous_on.div_const {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} {s : set α} [topological_space α] (hf : continuous_on f s) {y : G₀} : continuous_on (fun (x : α) => f x / y) s := sorry theorem continuous.div_const {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} [topological_space α] (hf : continuous f) {y : G₀} : continuous fun (x : α) => f x / y := sorry /-- A type with `0` and `has_inv` such that `λ x, x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. -/ class has_continuous_inv' (G₀ : Type u_3) [HasZero G₀] [has_inv G₀] [topological_space G₀] where continuous_at_inv' : ∀ {x : G₀}, x ≠ 0 → continuous_at has_inv.inv x /-! ### Continuity of `λ x, x⁻¹` at a non-zero point We define `topological_group_with_zero` to be a `group_with_zero` such that the operation `x ↦ x⁻¹` is continuous at all nonzero points. In this section we prove dot-style `*.inv'` lemmas for `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. -/ theorem tendsto_inv' {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {x : G₀} (hx : x ≠ 0) : filter.tendsto has_inv.inv (nhds x) (nhds (x⁻¹)) := continuous_at_inv' hx theorem continuous_on_inv' {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] : continuous_on has_inv.inv (singleton 0ᶜ) := fun (x : G₀) (hx : x ∈ (singleton 0ᶜ)) => continuous_at.continuous_within_at (continuous_at_inv' hx) /-- If a function converges to a nonzero value, its inverse converges to the inverse of this value. We use the name `tendsto.inv'` as `tendsto.inv` is already used in multiplicative topological groups. -/ theorem filter.tendsto.inv' {α : Type u_1} {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {l : filter α} {f : α → G₀} {a : G₀} (hf : filter.tendsto f l (nhds a)) (ha : a ≠ 0) : filter.tendsto (fun (x : α) => f x⁻¹) l (nhds (a⁻¹)) := filter.tendsto.comp (tendsto_inv' ha) hf theorem continuous_within_at.inv' {α : Type u_1} {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {f : α → G₀} {s : set α} {a : α} [topological_space α] (hf : continuous_within_at f s a) (ha : f a ≠ 0) : continuous_within_at (fun (x : α) => f x⁻¹) s a := filter.tendsto.inv' hf ha theorem continuous_at.inv' {α : Type u_1} {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {f : α → G₀} {a : α} [topological_space α] (hf : continuous_at f a) (ha : f a ≠ 0) : continuous_at (fun (x : α) => f x⁻¹) a := filter.tendsto.inv' hf ha theorem continuous.inv' {α : Type u_1} {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {f : α → G₀} [topological_space α] (hf : continuous f) (h0 : ∀ (x : α), f x ≠ 0) : continuous fun (x : α) => f x⁻¹ := iff.mpr continuous_iff_continuous_at fun (x : α) => filter.tendsto.inv' (continuous.tendsto hf x) (h0 x) theorem continuous_on.inv' {α : Type u_1} {G₀ : Type u_2} [HasZero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {f : α → G₀} {s : set α} [topological_space α] (hf : continuous_on f s) (h0 : ∀ (x : α), x ∈ s → f x ≠ 0) : continuous_on (fun (x : α) => f x⁻¹) s := fun (x : α) (hx : x ∈ s) => continuous_within_at.inv' (hf x hx) (h0 x hx) /-! ### Continuity of division If `G₀` is a `group_with_zero` with `x ↦ x⁻¹` continuous at all nonzero points and `(*)`, then division `(/)` is continuous at any point where the denominator is continuous. -/ theorem filter.tendsto.div {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f : α → G₀} {g : α → G₀} {l : filter α} {a : G₀} {b : G₀} (hf : filter.tendsto f l (nhds a)) (hg : filter.tendsto g l (nhds b)) (hy : b ≠ 0) : filter.tendsto (f / g) l (nhds (a / b)) := sorry theorem continuous_within_at.div {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f : α → G₀} {g : α → G₀} [topological_space α] {s : set α} {a : α} (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h₀ : g a ≠ 0) : continuous_within_at (f / g) s a := filter.tendsto.div hf hg h₀ theorem continuous_on.div {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f : α → G₀} {g : α → G₀} [topological_space α] {s : set α} (hf : continuous_on f s) (hg : continuous_on g s) (h₀ : ∀ (x : α), x ∈ s → g x ≠ 0) : continuous_on (f / g) s := fun (x : α) (hx : x ∈ s) => continuous_within_at.div (hf x hx) (hg x hx) (h₀ x hx) /-- Continuity at a point of the result of dividing two functions continuous at that point, where the denominator is nonzero. -/ theorem continuous_at.div {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f : α → G₀} {g : α → G₀} [topological_space α] {a : α} (hf : continuous_at f a) (hg : continuous_at g a) (h₀ : g a ≠ 0) : continuous_at (f / g) a := filter.tendsto.div hf hg h₀ theorem continuous.div {α : Type u_1} {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f : α → G₀} {g : α → G₀} [topological_space α] (hf : continuous f) (hg : continuous g) (h₀ : ∀ (x : α), g x ≠ 0) : continuous (f / g) := eq.mpr (id ((fun (f f_1 : α → G₀) (e_3 : f = f_1) => congr_arg continuous e_3) (f / g) (f * (g⁻¹)) (div_eq_mul_inv f g))) (eq.mp (Eq.refl (continuous fun (x : α) => f x * (g x⁻¹))) (continuous.mul hf (continuous.inv' hg h₀))) theorem continuous_on_div {G₀ : Type u_2} [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] : continuous_on (fun (p : G₀ × G₀) => prod.fst p / prod.snd p) (set_of fun (p : G₀ × G₀) => prod.snd p ≠ 0) := continuous_on.div continuous_on_fst continuous_on_snd fun (_x : G₀ × G₀) => id end Mathlib
79a8746f998f9139e56390b5ed5d50010d8191de
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/no_confusion_type.lean
cbcfeae1b93da2e64dcb473f3ca272ea13096b50
[ "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
344
lean
namespace Ex open nat inductive vector (A : Type) : nat → Type | vnil : vector nat.zero | vcons : Π {n : nat}, A → vector n → vector (succ n) #check vector.no_confusion_type constants a1 a2 : nat constants v1 v2 : vector nat 2 constant P : Type #reduce vector.no_confusion_type P (vector.vcons a1 v1) (vector.vcons a2 v2) end Ex
b408b5071e36e47af0130061e6c6fcb99f9f6745
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/topology/instances/real.lean
54852b6ab476bd87006976f5cbe544ef03f1a2ee
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
19,173
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 The real numbers ℝ. They are constructed as the topological completion of ℚ. With the following steps: (1) prove that ℚ forms a uniform space. (2) subtraction and addition are uniform continuous functions in this space (3) for multiplication and inverse this only holds on bounded subsets (4) ℝ is defined as separated Cauchy filters over ℚ (the separation requires a quotient construction) (5) extend the uniform continuous functions along the completion (6) proof field properties using the principle of extension of identities TODO generalizations: * topological groups & rings * order topologies * Archimedean fields -/ import topology.metric_space.basic topology.algebra.uniform_group topology.algebra.ring tactic.linarith noncomputable theory open classical set lattice filter topological_space metric open_locale classical open_locale topological_space universes u v w variables {α : Type u} {β : Type v} {γ : Type w} instance : metric_space ℚ := metric_space.induced coe rat.cast_injective real.metric_space theorem rat.dist_eq (x y : ℚ) : dist x y = abs (x - y) := rfl @[elim_cast, simp] lemma rat.dist_cast (x y : ℚ) : dist (x : ℝ) y = dist x y := rfl instance : metric_space ℤ := begin letI M := metric_space.induced coe int.cast_injective real.metric_space, refine @metric_space.replace_uniformity _ int.uniform_space M (le_antisymm refl_le_uniformity $ λ r ru, mem_uniformity_dist.2 ⟨1, zero_lt_one, λ a b h, mem_principal_sets.1 ru $ dist_le_zero.1 (_ : (abs (a - b) : ℝ) ≤ 0)⟩), simpa using (@int.cast_le ℝ _ _ 0).2 (int.lt_add_one_iff.1 $ (@int.cast_lt ℝ _ (abs (a - b)) 1).1 $ by simpa using h) end theorem int.dist_eq (x y : ℤ) : dist x y = abs (x - y) := rfl @[elim_cast, simp] theorem int.dist_cast_real (x y : ℤ) : dist (x : ℝ) y = dist x y := rfl @[elim_cast, simp] theorem int.dist_cast_rat (x y : ℤ) : dist (x : ℚ) y = dist x y := by rw [← int.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_comap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := uniform_embedding_comap rat.cast_injective theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) := uniform_embedding_of_rat.dense_embedding $ λ x, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε,ε0, hε⟩ := mem_nhds_iff.1 ht in let ⟨q, h⟩ := exists_rat_near x ε0 in ne_empty_iff_exists_mem.2 ⟨_, hε (mem_ball'.2 h), q, rfl⟩ theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.to_embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩ -- TODO(Mario): Find a way to use rat_add_continuous_lemma theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) := uniform_embedding_of_rat.to_uniform_inducing.uniform_continuous_iff.2 $ by simp [(∘)]; exact real.uniform_continuous_add.comp ((uniform_continuous_of_rat.comp uniform_continuous_fst).prod_mk (uniform_continuous_of_rat.comp uniform_continuous_snd)) theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [real.dist_eq] using h⟩ theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [rat.dist_eq] using h⟩ instance : uniform_add_group ℝ := uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg instance : uniform_add_group ℚ := uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg instance : topological_add_group ℝ := by apply_instance instance : topological_add_group ℚ := by apply_instance instance : orderable_topology ℚ := induced_orderable_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) lemma real.is_topological_basis_Ioo_rat : @is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := is_topological_basis_of_open_of_nhds (by simp [is_open_Ioo] {contextual:=tt}) (assume a v hav hv, let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav), ⟨q, hlq, hqa⟩ := exists_rat_btwn hl, ⟨p, hap, hpu⟩ := exists_rat_btwn hu in ⟨Ioo q p, by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩, ⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩) instance : second_countable_topology ℝ := ⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}), by simp [countable_Union, countable_Union_Prop], real.is_topological_basis_Ioo_rat.2.2⟩⟩ /- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) := _ lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) := _ -/ lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in ⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩ lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩ lemma real.continuous_abs : continuous (abs : ℝ → ℝ) := real.uniform_continuous_abs.continuous lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b h, lt_of_le_of_lt (by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩ lemma rat.continuous_abs : continuous (abs : ℚ → ℚ) := rat.uniform_continuous_abs.continuous lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) := by rw ← abs_pos_iff at r0; exact tendsto_of_uniform_continuous_subtype (real.uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h)) (mem_nhds_sets (real.continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0)) lemma real.continuous_inv' : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) := continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩, tendsto.comp (real.tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _) lemma real.continuous_inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) : continuous (λa, (f a)⁻¹) := show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩), from real.continuous_inv'.comp (continuous_subtype_mk _ hf) lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) := metric.uniform_continuous_iff.2 $ λ ε ε0, begin cases no_top (abs x) with y xy, have y0 := lt_of_le_of_lt (abs_nonneg _) xy, refine ⟨_, div_pos ε0 y0, λ a b h, _⟩, rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)], exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0 end lemma real.uniform_continuous_mul (s : set (ℝ × ℝ)) {r₁ r₂ : ℝ} (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩ protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) := continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩, tendsto_of_uniform_continuous_subtype (real.uniform_continuous_mul ({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1}) (λ x, id)) (mem_nhds_sets (is_open_prod (real.continuous_abs _ $ is_open_gt' (abs a₁ + 1)) (real.continuous_abs _ $ is_open_gt' (abs a₂ + 1))) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) instance : topological_ring ℝ := { continuous_mul := real.continuous_mul, ..real.topological_add_group } instance : topological_semiring ℝ := by apply_instance lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact real.continuous_mul.comp ((continuous_of_rat.comp continuous_fst).prod_mk (continuous_of_rat.comp continuous_snd)) instance : topological_ring ℚ := { continuous_mul := rat.continuous_mul, ..rat.topological_add_group } theorem real.ball_eq_Ioo (x ε : ℝ) : ball x ε = Ioo (x - ε) (x + ε) := set.ext $ λ y, by rw [mem_ball, real.dist_eq, abs_sub_lt_iff, sub_lt_iff_lt_add', and_comm, sub_lt]; refl theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) := by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add, add_sub_cancel', add_self_div_two, ← add_div, add_assoc, add_sub_cancel'_right, add_self_div_two] lemma real.totally_bounded_Ioo (a b : ℝ) : totally_bounded (Ioo a b) := metric.totally_bounded_iff.2 $ λ ε ε0, begin rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩, rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba, let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n}, refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩, rcases h with ⟨ax, xb⟩, let i : ℕ := ⌊(x - a) / ε⌋.to_nat, have : (i : ℤ) = ⌊(x - a) / ε⌋ := int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)), simp, refine ⟨_, ⟨i, _, rfl⟩, _⟩, { rw [← int.coe_nat_lt, this], refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _), rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'], exact lt_trans xb ba }, { rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg, ← sub_sub, sub_lt_iff_lt_add'], { have := lt_floor_add_one ((x - a) / ε), rwa [div_lt_iff' ε0, mul_add, mul_one] at this }, { have := floor_le ((x - a) / ε), rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } } end lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) := by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) := let ⟨c, ac⟩ := no_bot a in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩) (real.totally_bounded_Ioo c b) lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) := let ⟨c, bc⟩ := no_top b in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩) (real.totally_bounded_Ico a c) lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := begin have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b), rwa (set.ext (λ q, _) : Icc _ _ = _), simp end -- TODO(Mario): Generalize to first-countable uniform spaces? instance : complete_space ℝ := ⟨λ f cf, begin let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩, choose S hS hS_dist using show ∀n:ℕ, ∃t ∈ f.sets, ∀ x y ∈ t, dist x y < g n, from assume n, let ⟨t, tf, h⟩ := (metric.cauchy_iff.1 cf).2 (g n).1 (g n).2 in ⟨t, tf, h⟩, let F : ℕ → set ℝ := λn, ⋂i≤n, S i, have hF : ∀n, F n ∈ f.sets := assume n, Inter_mem_sets (finite_le_nat n) (λ i _, hS i), have hF_dist : ∀n, ∀ x y ∈ F n, dist x y < g n := assume n x y hx hy, have F n ⊆ S n := bInter_subset_of_mem (le_refl n), (hS_dist n) _ _ (this hx) (this hy), choose G hG using assume n:ℕ, inhabited_of_mem_sets cf.1 (hF n), have hg : ∀ ε > 0, ∃ n, ∀ j ≥ n, (g j : ℝ) < ε, { intros ε ε0, cases exists_nat_gt ε⁻¹ with n hn, refine ⟨n, λ j nj, _⟩, have hj := lt_of_lt_of_le hn (nat.cast_le.2 nj), have j0 := lt_trans (inv_pos ε0) hj, have jε := (inv_lt j0 ε0).2 hj, rwa ← pnat.to_pnat'_coe (nat.cast_pos.1 j0) at jε }, let c : cau_seq ℝ abs, { refine ⟨λ n, G n, λ ε ε0, _⟩, cases hg _ ε0 with n hn, refine ⟨n, λ j jn, _⟩, have : F j ⊆ F n := bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn), exact lt_trans (hF_dist n _ _ (this (hG j)) (hG n)) (hn _ $ le_refl _) }, refine ⟨cau_seq.lim c, λ s h, _⟩, rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩, cases exists_forall_ge_and (hg _ $ half_pos ε0) (cau_seq.equiv_lim c _ $ half_pos ε0) with n hn, cases hn _ (le_refl _) with h₁ h₂, refine sets_of_superset _ (hF n) (subset.trans _ $ subset.trans (ball_half_subset (G n) h₂) hε), exact λ x h, lt_trans ((hF_dist n) x (G n) h (hG n)) h₁ end⟩ lemma tendsto_coe_nat_real_at_top_iff {f : α → ℕ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨n, le_of_lt hn⟩ lemma tendsto_coe_nat_real_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_coe_nat_real_at_top_iff.2 tendsto_id lemma tendsto_coe_int_real_at_top_iff {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, int.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨(n:ℤ), le_of_lt $ by rwa [int.cast_coe_nat]⟩ lemma tendsto_coe_int_real_at_top_at_top : tendsto (coe : ℤ → ℝ) at_top at_top := tendsto_coe_int_real_at_top_iff.2 tendsto_id section lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} := subset.antisymm ((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).2 (image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $ λ x hx, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in ne_empty_iff_exists_mem.2 ⟨_, hε (show abs _ < _, by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']), p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩ /- TODO(Mario): Put these back only if needed later lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} := _ lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) : closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} := _-/ lemma compact_Icc {a b : ℝ} : compact (Icc a b) := compact_of_totally_bounded_is_closed (real.totally_bounded_Icc a b) (is_closed_inter (is_closed_ge' a) (is_closed_le' b)) instance : proper_space ℝ := { compact_ball := λx r, by rw closed_ball_Icc; apply compact_Icc } open real lemma real.intermediate_value {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (𝓝 x) (𝓝 (f x))) (ha : f a ≤ t) (hb : t ≤ f b) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let x := real.Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} in have hx₁ : ∃ y, ∀ g ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}, g ≤ y := ⟨b, λ _ h, h.2.2⟩, have hx₂ : ∃ y, y ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} := ⟨a, ha, le_refl _, hab⟩, have hax : a ≤ x, from le_Sup _ hx₁ ⟨ha, le_refl _, hab⟩, have hxb : x ≤ b, from (Sup_le _ hx₂ hx₁).2 (λ _ h, h.2.2), ⟨x, hax, hxb, eq_of_forall_dist_le $ λ ε ε0, let ⟨δ, hδ0, hδ⟩ := metric.tendsto_nhds_nhds.1 (hf _ hax hxb) ε ε0 in (le_total t (f x)).elim (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonneg (sub_nonneg.2 h)] at hfε, refine mt (Sup_le {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} hx₂ hx₁).2 (not_le_of_gt (sub_lt_self x (half_pos hδ0))) (λ g hg, le_of_not_gt (λ hgδ, not_lt_of_ge hg.1 (lt_trans (lt_sub.1 hfε) (sub_lt_of_sub_lt (lt_of_le_of_lt (le_abs_self _) _))))), rw abs_sub, exact hδ (abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos.2 (le_Sup _ hx₁ hg)) hδ0, by simp only [x] at *; linarith⟩) end) (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonpos (sub_nonpos.2 h)] at hfε, exact mt (le_Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}) (λ h : ∀ k, k ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} → k ≤ x, not_le_of_gt ((lt_add_iff_pos_left x).2 (half_pos hδ0)) (h _ ⟨le_trans (le_sub_iff_add_le.2 (le_trans (le_abs_self _) (le_of_lt (hδ $ by rw [dist_eq, add_sub_cancel, abs_of_nonneg (le_of_lt (half_pos hδ0))]; exact half_lt_self hδ0)))) (by linarith), le_trans hax (le_of_lt ((lt_add_iff_pos_left _).2 (half_pos hδ0))), le_of_not_gt (λ hδy, not_lt_of_ge hb (lt_of_le_of_lt (show f b ≤ f b - f x - ε + t, by linarith) (add_lt_of_neg_of_le (sub_neg_of_lt (lt_of_le_of_lt (le_abs_self _) (@hδ b (abs_sub_lt_iff.2 ⟨by simp only [x] at *; linarith, by linarith⟩)))) (le_refl _))))⟩)) hx₁ end)⟩ lemma real.intermediate_value' {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (𝓝 x) (𝓝 (f x))) (ha : t ≤ f a) (hb : f b ≤ t) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let ⟨x, hx₁, hx₂, hx₃⟩ := @real.intermediate_value (λ x, - f x) a b (-t) (λ x hax hxb, tendsto_neg (hf x hax hxb)) (neg_le_neg ha) (neg_le_neg hb) hab in ⟨x, hx₁, hx₂, neg_inj hx₃⟩ lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s := ⟨begin assume bdd, rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r rw closed_ball_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r) exact ⟨⟨-r, λy hy, by simpa using (hr hy).1⟩, ⟨r, λy hy, by simpa using (hr hy).2⟩⟩ end, begin rintros ⟨⟨m, hm⟩, ⟨M, hM⟩⟩, have I : s ⊆ Icc m M := λx hx, ⟨hm x hx, hM x hx⟩, have : Icc m M = closed_ball ((m+M)/2) ((M-m)/2) := by rw closed_ball_Icc; congr; ring, rw this at I, exact bounded.subset I bounded_closed_ball end⟩ end
faf6fcee3224bed146ffe2210995ab95ef03bd79
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/witt_vector/compare_auto.lean
1168a2ed7da5b46ff03126a03a9263a5c1a7382e
[]
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
8,344
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.witt_vector.truncated import Mathlib.ring_theory.witt_vector.identities import Mathlib.data.padics.ring_homs import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Comparison isomorphism between `witt_vector p (zmod p)` and `ℤ_[p]` We construct a ring isomorphism between `witt_vector p (zmod p)` and `ℤ_[p]`. This isomorphism follows from the fact that both satisfy the universal property of the inverse limit of `zmod (p^n)`. ## Main declarations * `witt_vector.to_zmod_pow`: a family of compatible ring homs `𝕎 (zmod p) → zmod (p^k)` * `witt_vector.equiv`: the isomorphism -/ namespace truncated_witt_vector theorem eq_of_le_of_cast_pow_eq_zero (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) (R : Type u_1) [comm_ring R] [char_p R p] (i : ℕ) (hin : i ≤ n) (hpi : ↑p ^ i = 0) : i = n := sorry theorem card_zmod (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : fintype.card (truncated_witt_vector p n (zmod p)) = p ^ n := eq.mpr (id (Eq._oldrec (Eq.refl (fintype.card (truncated_witt_vector p n (zmod p)) = p ^ n)) (card p n))) (eq.mpr (id (Eq._oldrec (Eq.refl (fintype.card (zmod p) ^ n = p ^ n)) (zmod.card p))) (Eq.refl (p ^ n))) theorem char_p_zmod (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : char_p (truncated_witt_vector p n (zmod p)) (p ^ n) := char_p_of_prime_pow_injective (truncated_witt_vector p n (zmod p)) p n (card_zmod p n) (eq_of_le_of_cast_pow_eq_zero p n (zmod p)) /-- The unique isomorphism between `zmod p^n` and `truncated_witt_vector p n (zmod p)`. This isomorphism exists, because `truncated_witt_vector p n (zmod p)` is a finite ring with characteristic and cardinality `p^n`. -/ def zmod_equiv_trunc (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : zmod (p ^ n) ≃+* truncated_witt_vector p n (zmod p) := zmod.ring_equiv (truncated_witt_vector p n (zmod p)) (card_zmod p n) theorem zmod_equiv_trunc_apply (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) {x : zmod (p ^ n)} : coe_fn (zmod_equiv_trunc p n) x = coe_fn (zmod.cast_hom (dvd_refl (p ^ n)) (truncated_witt_vector p n (zmod p))) x := rfl /-- The following diagram commutes: ```text zmod (p^n) ----------------------------> zmod (p^m) | | | | v v truncated_witt_vector p n (zmod p) ----> truncated_witt_vector p m (zmod p) ``` Here the vertical arrows are `truncated_witt_vector.zmod_equiv_trunc`, the horizontal arrow at the top is `zmod.cast_hom`, and the horizontal arrow at the bottom is `truncated_witt_vector.truncate`. -/ theorem commutes (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) {m : ℕ} (hm : n ≤ m) : ring_hom.comp (truncate hm) (ring_equiv.to_ring_hom (zmod_equiv_trunc p m)) = ring_hom.comp (ring_equiv.to_ring_hom (zmod_equiv_trunc p n)) (zmod.cast_hom (pow_dvd_pow p hm) (zmod (p ^ n))) := ring_hom.ext_zmod (ring_hom.comp (truncate hm) (ring_equiv.to_ring_hom (zmod_equiv_trunc p m))) (ring_hom.comp (ring_equiv.to_ring_hom (zmod_equiv_trunc p n)) (zmod.cast_hom (pow_dvd_pow p hm) (zmod (p ^ n)))) theorem commutes' (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) {m : ℕ} (hm : n ≤ m) (x : zmod (p ^ m)) : coe_fn (truncate hm) (coe_fn (zmod_equiv_trunc p m) x) = coe_fn (zmod_equiv_trunc p n) (coe_fn (zmod.cast_hom (pow_dvd_pow p hm) (zmod (p ^ n))) x) := sorry theorem commutes_symm' (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) {m : ℕ} (hm : n ≤ m) (x : truncated_witt_vector p m (zmod p)) : coe_fn (ring_equiv.symm (zmod_equiv_trunc p n)) (coe_fn (truncate hm) x) = coe_fn (zmod.cast_hom (pow_dvd_pow p hm) (zmod (p ^ n))) (coe_fn (ring_equiv.symm (zmod_equiv_trunc p m)) x) := sorry /-- The following diagram commutes: ```text truncated_witt_vector p n (zmod p) ----> truncated_witt_vector p m (zmod p) | | | | v v zmod (p^n) ----------------------------> zmod (p^m) ``` Here the vertical arrows are `(truncated_witt_vector.zmod_equiv_trunc p _).symm`, the horizontal arrow at the top is `zmod.cast_hom`, and the horizontal arrow at the bottom is `truncated_witt_vector.truncate`. -/ theorem commutes_symm (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) {m : ℕ} (hm : n ≤ m) : ring_hom.comp (ring_equiv.to_ring_hom (ring_equiv.symm (zmod_equiv_trunc p n))) (truncate hm) = ring_hom.comp (zmod.cast_hom (pow_dvd_pow p hm) (zmod (p ^ n))) (ring_equiv.to_ring_hom (ring_equiv.symm (zmod_equiv_trunc p m))) := ring_hom.ext fun (x : truncated_witt_vector p m (zmod p)) => commutes_symm' p n hm x end truncated_witt_vector namespace witt_vector /-- `to_zmod_pow` is a family of compatible ring homs. We get this family by composing `truncated_witt_vector.zmod_equiv_trunc` (in right-to-left direction) with `witt_vector.truncate`. -/ def to_zmod_pow (p : ℕ) [hp : fact (nat.prime p)] (k : ℕ) : witt_vector p (zmod p) →+* zmod (p ^ k) := ring_hom.comp (ring_equiv.to_ring_hom (ring_equiv.symm (truncated_witt_vector.zmod_equiv_trunc p k))) (truncate k) theorem to_zmod_pow_compat (p : ℕ) [hp : fact (nat.prime p)] (m : ℕ) (n : ℕ) (h : m ≤ n) : ring_hom.comp (zmod.cast_hom (pow_dvd_pow p h) (zmod (p ^ m))) (to_zmod_pow p n) = to_zmod_pow p m := sorry /-- `to_padic_int` lifts `to_zmod_pow : 𝕎 (zmod p) →+* zmod (p ^ k)` to a ring hom to `ℤ_[p]` using `padic_int.lift`, the universal property of `ℤ_[p]`. -/ def to_padic_int (p : ℕ) [hp : fact (nat.prime p)] : witt_vector p (zmod p) →+* padic_int p := padic_int.lift (to_zmod_pow_compat p) theorem zmod_equiv_trunc_compat (p : ℕ) [hp : fact (nat.prime p)] (k₁ : ℕ) (k₂ : ℕ) (hk : k₁ ≤ k₂) : ring_hom.comp (truncated_witt_vector.truncate hk) (ring_hom.comp (ring_equiv.to_ring_hom (truncated_witt_vector.zmod_equiv_trunc p k₂)) (padic_int.to_zmod_pow k₂)) = ring_hom.comp (ring_equiv.to_ring_hom (truncated_witt_vector.zmod_equiv_trunc p k₁)) (padic_int.to_zmod_pow k₁) := sorry /-- `from_padic_int` uses `witt_vector.lift` to lift `truncated_witt_vector.zmod_equiv_trunc` composed with `padic_int.to_zmod_pow` to a ring hom `ℤ_[p] →+* 𝕎 (zmod p)`. -/ def from_padic_int (p : ℕ) [hp : fact (nat.prime p)] : padic_int p →+* witt_vector p (zmod p) := lift (fun (k : ℕ) => ring_hom.comp (ring_equiv.to_ring_hom (truncated_witt_vector.zmod_equiv_trunc p k)) (padic_int.to_zmod_pow k)) (zmod_equiv_trunc_compat p) theorem to_padic_int_comp_from_padic_int (p : ℕ) [hp : fact (nat.prime p)] : ring_hom.comp (to_padic_int p) (from_padic_int p) = ring_hom.id (padic_int p) := sorry theorem to_padic_int_comp_from_padic_int_ext (p : ℕ) [hp : fact (nat.prime p)] (x : padic_int p) : coe_fn (ring_hom.comp (to_padic_int p) (from_padic_int p)) x = coe_fn (ring_hom.id (padic_int p)) x := sorry theorem from_padic_int_comp_to_padic_int (p : ℕ) [hp : fact (nat.prime p)] : ring_hom.comp (from_padic_int p) (to_padic_int p) = ring_hom.id (witt_vector p (zmod p)) := sorry theorem from_padic_int_comp_to_padic_int_ext (p : ℕ) [hp : fact (nat.prime p)] (x : witt_vector p (zmod p)) : coe_fn (ring_hom.comp (from_padic_int p) (to_padic_int p)) x = coe_fn (ring_hom.id (witt_vector p (zmod p))) x := sorry /-- The ring of Witt vectors over `zmod p` is isomorphic to the ring of `p`-adic integers. This equivalence is witnessed by `witt_vector.to_padic_int` with inverse `witt_vector.from_padic_int`. -/ def equiv (p : ℕ) [hp : fact (nat.prime p)] : witt_vector p (zmod p) ≃+* padic_int p := ring_equiv.mk (⇑(to_padic_int p)) (⇑(from_padic_int p)) (from_padic_int_comp_to_padic_int_ext p) (to_padic_int_comp_from_padic_int_ext p) sorry sorry end Mathlib
730fec611808cc4789c95367bc18aa40fcca5869
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/sites/dense_subsite.lean
d7498c315fffa972cab2b7e5e04b78a5246c147b
[ "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
19,314
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import category_theory.sites.sheaf import category_theory.sites.cover_lifting import category_theory.adjunction.fully_faithful /-! # Dense subsites We define `cover_dense` functors into sites as functors such that there exists a covering sieve that factors through images of the functor for each object in `D`. We will primarily consider cover-dense functors that are also full, since this notion is in general not well-behaved otherwise. Note that https://ncatlab.org/nlab/show/dense+sub-site indeed has a weaker notion of cover-dense that loosens this requirement, but it would not have all the properties we would need, and some sheafification would be needed for here and there. ## Main results - `category_theory.cover_dense.presheaf_hom`: If `G : C ⥤ (D, K)` is full and cover-dense, then given any presheaf `ℱ` and sheaf `ℱ'` on `D`, and a morphism `α : G ⋙ ℱ ⟶ G ⋙ ℱ'`, we may glue them together to obtain a morphism of presheaves `ℱ ⟶ ℱ'`. - `category_theory.cover_dense.sheaf_iso`: If `ℱ` above is a sheaf and `α` is an iso, then the result is also an iso. - `category_theory.cover_dense.iso_of_restrict_iso`: If `G : C ⥤ (D, K)` is full and cover-dense, then given any sheaves `ℱ, ℱ'` on `D`, and a morphism `α : ℱ ⟶ ℱ'`, then `α` is an iso if `G ⋙ ℱ ⟶ G ⋙ ℱ'` is iso. - `category_theory.cover_dense.Sheaf_equiv_of_cover_preserving_cover_lifting`: If `G : (C, J) ⥤ (D, K)` is fully-faithful, cover-lifting, cover-preserving, and cover-dense, then it will induce an equivalence of categories of sheaves valued in a complete category. ## References * [Elephant]: *Sketches of an Elephant*, ℱ. T. Johnstone: C2.2. * https://ncatlab.org/nlab/show/dense+sub-site * https://ncatlab.org/nlab/show/comparison+lemma -/ universes w v u namespace category_theory variables {C : Type*} [category C] {D : Type*} [category D] {E : Type*} [category E] variables (J : grothendieck_topology C) (K : grothendieck_topology D) variables {L : grothendieck_topology E} /-- An auxiliary structure that witnesses the fact that `f` factors through an image object of `G`. -/ @[nolint has_inhabited_instance] structure presieve.cover_by_image_structure (G : C ⥤ D) {V U : D} (f : V ⟶ U) := (obj : C) (lift : V ⟶ G.obj obj) (map : G.obj obj ⟶ U) (fac' : lift ≫ map = f . obviously) restate_axiom presieve.cover_by_image_structure.fac' attribute [simp, reassoc] presieve.cover_by_image_structure.fac /-- For a functor `G : C ⥤ D`, and an object `U : D`, `presieve.cover_by_image G U` is the presieve of `U` consisting of those arrows that factor through images of `G`. -/ def presieve.cover_by_image (G : C ⥤ D) (U : D) : presieve U := λ Y f, nonempty (presieve.cover_by_image_structure G f) /-- For a functor `G : C ⥤ D`, and an object `U : D`, `sieve.cover_by_image G U` is the sieve of `U` consisting of those arrows that factor through images of `G`. -/ def sieve.cover_by_image (G : C ⥤ D) (U : D) : sieve U := ⟨presieve.cover_by_image G U, λ X Y f ⟨⟨Z, f₁, f₂, (e : _ = _)⟩⟩ g, ⟨⟨Z, g ≫ f₁, f₂, show (g ≫ f₁) ≫ f₂ = g ≫ f, by rw [category.assoc, ← e]⟩⟩⟩ lemma presieve.in_cover_by_image (G : C ⥤ D) {X : D} {Y : C} (f : G.obj Y ⟶ X) : presieve.cover_by_image G X f := ⟨⟨Y, 𝟙 _, f, by simp⟩⟩ /-- A functor `G : (C, J) ⥤ (D, K)` is called `cover_dense` if for each object in `D`, there exists a covering sieve in `D` that factors through images of `G`. This definition can be found in https://ncatlab.org/nlab/show/dense+sub-site Definition 2.2. -/ structure cover_dense (K : grothendieck_topology D) (G : C ⥤ D) : Prop := (is_cover : ∀ (U : D), sieve.cover_by_image G U ∈ K U) open presieve opposite namespace cover_dense variable {K} variables {A : Type*} [category A] {G : C ⥤ D} (H : cover_dense K G) -- this is not marked with `@[ext]` because `H` can not be inferred from the type lemma ext (H : cover_dense K G) (ℱ : SheafOfTypes K) (X : D) {s t : ℱ.val.obj (op X)} (h : ∀ ⦃Y : C⦄ (f : G.obj Y ⟶ X), ℱ.val.map f.op s = ℱ.val.map f.op t) : s = t := begin apply (ℱ.cond (sieve.cover_by_image G X) (H.is_cover X)).is_separated_for.ext, rintros Y _ ⟨Z, f₁, f₂, ⟨rfl⟩⟩, simp [h f₂] end lemma functor_pullback_pushforward_covering [full G] (H : cover_dense K G) {X : C} (T : K (G.obj X)) : (T.val.functor_pullback G).functor_pushforward G ∈ K (G.obj X) := begin refine K.superset_covering _ (K.bind_covering T.property (λ Y f Hf, H.is_cover Y)), rintros Y _ ⟨Z, _, f, hf, ⟨W, g, f', ⟨rfl⟩⟩, rfl⟩, use W, use G.preimage (f' ≫ f), use g, split, { simpa using T.val.downward_closed hf f' }, { simp }, end /-- (Implementation). Given an hom between the pullbacks of two sheaves, we can whisker it with `coyoneda` to obtain an hom between the pullbacks of the sheaves of maps from `X`. -/ @[simps] def hom_over {ℱ : Dᵒᵖ ⥤ A} {ℱ' : Sheaf K A} (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) (X : A) : G.op ⋙ (ℱ ⋙ coyoneda.obj (op X)) ⟶ G.op ⋙ (sheaf_over ℱ' X).val := whisker_right α (coyoneda.obj (op X)) /-- (Implementation). Given an iso between the pullbacks of two sheaves, we can whisker it with `coyoneda` to obtain an iso between the pullbacks of the sheaves of maps from `X`. -/ @[simps] def iso_over {ℱ ℱ' : Sheaf K A} (α : G.op ⋙ ℱ.val ≅ G.op ⋙ ℱ'.val) (X : A) : G.op ⋙ (sheaf_over ℱ X).val ≅ G.op ⋙ (sheaf_over ℱ' X).val := iso_whisker_right α (coyoneda.obj (op X)) lemma sheaf_eq_amalgamation (ℱ : Sheaf K A) {X : A} {U : D} {T : sieve U} (hT) (x : family_of_elements _ T) (hx) (t) (h : x.is_amalgamation t) : t = (ℱ.cond X T hT).amalgamate x hx := (ℱ.cond X T hT).is_separated_for x t _ h ((ℱ.cond X T hT).is_amalgamation hx) include H variable [full G] namespace types variables {ℱ : Dᵒᵖ ⥤ Type v} {ℱ' : SheafOfTypes.{v} K} (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) /-- (Implementation). Given a section of `ℱ` on `X`, we can obtain a family of elements valued in `ℱ'` that is defined on a cover generated by the images of `G`. -/ @[simp, nolint unused_arguments] noncomputable def pushforward_family {X} (x : ℱ.obj (op X)) : family_of_elements ℱ'.val (cover_by_image G X) := λ Y f hf, ℱ'.val.map hf.some.lift.op $ α.app (op _) (ℱ.map hf.some.map.op x : _) /-- (Implementation). The `pushforward_family` defined is compatible. -/ lemma pushforward_family_compatible {X} (x : ℱ.obj (op X)) : (pushforward_family H α x).compatible := begin intros Y₁ Y₂ Z g₁ g₂ f₁ f₂ h₁ h₂ e, apply H.ext, intros Y f, simp only [pushforward_family, ← functor_to_types.map_comp_apply, ← op_comp], change (ℱ.map _ ≫ α.app (op _) ≫ ℱ'.val.map _) _ = (ℱ.map _ ≫ α.app (op _) ≫ ℱ'.val.map _) _, rw ← G.image_preimage (f ≫ g₁ ≫ _), rw ← G.image_preimage (f ≫ g₂ ≫ _), erw ← α.naturality (G.preimage _).op, erw ← α.naturality (G.preimage _).op, refine congr_fun _ x, simp only [quiver.hom.unop_op, functor.comp_map, ← op_comp, ← category.assoc, functor.op_map, ← ℱ.map_comp, G.image_preimage], congr' 3, simp [e] end /-- (Implementation). The morphism `ℱ(X) ⟶ ℱ'(X)` given by gluing the `pushforward_family`. -/ noncomputable def app_hom (X : D) : ℱ.obj (op X) ⟶ ℱ'.val.obj (op X) := λ x, (ℱ'.cond _ (H.is_cover X)).amalgamate (pushforward_family H α x) (pushforward_family_compatible H α x) @[simp] lemma pushforward_family_apply {X} (x : ℱ.obj (op X)) {Y : C} (f : G.obj Y ⟶ X) : pushforward_family H α x f (presieve.in_cover_by_image G f) = α.app (op Y) (ℱ.map f.op x) := begin unfold pushforward_family, refine congr_fun _ x, rw ← G.image_preimage (nonempty.some _ : presieve.cover_by_image_structure _ _).lift, change ℱ.map _ ≫ α.app (op _) ≫ ℱ'.val.map _ = ℱ.map f.op ≫ α.app (op Y), erw ← α.naturality (G.preimage _).op, simp only [← functor.map_comp, ← category.assoc, functor.comp_map, G.image_preimage, G.op_map, quiver.hom.unop_op, ← op_comp, presieve.cover_by_image_structure.fac], end @[simp] lemma app_hom_restrict {X : D} {Y : C} (f : op X ⟶ op (G.obj Y)) (x) : ℱ'.val.map f (app_hom H α X x) = α.app (op Y) (ℱ.map f x) := begin refine ((ℱ'.cond _ (H.is_cover X)).valid_glue (pushforward_family_compatible H α x) f.unop (presieve.in_cover_by_image G f.unop)).trans _, apply pushforward_family_apply end @[simp] lemma app_hom_valid_glue {X : D} {Y : C} (f : op X ⟶ op (G.obj Y)) : app_hom H α X ≫ ℱ'.val.map f = ℱ.map f ≫ α.app (op Y) := by { ext, apply app_hom_restrict } /-- (Implementation). The maps given in `app_iso` is inverse to each other and gives a `ℱ(X) ≅ ℱ'(X)`. -/ @[simps] noncomputable def app_iso {ℱ ℱ' : SheafOfTypes.{v} K} (i : G.op ⋙ ℱ.val ≅ G.op ⋙ ℱ'.val) (X : D) : ℱ.val.obj (op X) ≅ ℱ'.val.obj (op X) := { hom := app_hom H i.hom X, inv := app_hom H i.inv X, hom_inv_id' := by { ext x, apply H.ext, intros Y f, simp }, inv_hom_id' := by { ext x, apply H.ext, intros Y f, simp } } /-- Given an natural transformation `G ⋙ ℱ ⟶ G ⋙ ℱ'` between presheaves of types, where `G` is full and cover-dense, and `ℱ'` is a sheaf, we may obtain a natural transformation between sheaves. -/ @[simps] noncomputable def presheaf_hom (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) : ℱ ⟶ ℱ'.val := { app := λ X, app_hom H α (unop X), naturality' := λ X Y f, begin ext x, apply H.ext ℱ' (unop Y), intros Y' f', simp only [app_hom_restrict, types_comp_apply, ← functor_to_types.map_comp_apply], rw app_hom_restrict H α (f ≫ f'.op : op (unop X) ⟶ _) end } /-- Given an natural isomorphism `G ⋙ ℱ ≅ G ⋙ ℱ'` between presheaves of types, where `G` is full and cover-dense, and `ℱ, ℱ'` are sheaves, we may obtain a natural isomorphism between presheaves. -/ @[simps] noncomputable def presheaf_iso {ℱ ℱ' : SheafOfTypes.{v} K} (i : G.op ⋙ ℱ.val ≅ G.op ⋙ ℱ'.val) : ℱ.val ≅ ℱ'.val := nat_iso.of_components (λ X, app_iso H i (unop X)) (presheaf_hom H i.hom).naturality /-- Given an natural isomorphism `G ⋙ ℱ ≅ G ⋙ ℱ'` between presheaves of types, where `G` is full and cover-dense, and `ℱ, ℱ'` are sheaves, we may obtain a natural isomorphism between sheaves. -/ @[simps] noncomputable def sheaf_iso {ℱ ℱ' : SheafOfTypes.{v} K} (i : G.op ⋙ ℱ.val ≅ G.op ⋙ ℱ'.val) : ℱ ≅ ℱ' := { hom := ⟨(presheaf_iso H i).hom⟩, inv := ⟨(presheaf_iso H i).inv⟩, hom_inv_id' := by { ext1, apply (presheaf_iso H i).hom_inv_id }, inv_hom_id' := by { ext1, apply (presheaf_iso H i).inv_hom_id } } end types open types variables {ℱ : Dᵒᵖ ⥤ A} {ℱ' : Sheaf K A} /-- (Implementation). The sheaf map given in `types.sheaf_hom` is natural in terms of `X`. -/ @[simps] noncomputable def sheaf_coyoneda_hom (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) : coyoneda ⋙ (whiskering_left Dᵒᵖ A Type*).obj ℱ ⟶ coyoneda ⋙ (whiskering_left Dᵒᵖ A Type*).obj ℱ'.val := { app := λ X, presheaf_hom H (hom_over α (unop X)), naturality' := λ X Y f, begin ext U x, change app_hom H (hom_over α (unop Y)) (unop U) (f.unop ≫ x) = f.unop ≫ app_hom H (hom_over α (unop X)) (unop U) x, symmetry, apply sheaf_eq_amalgamation, apply H.is_cover, intros Y' f' hf', change unop X ⟶ ℱ.obj (op (unop _)) at x, dsimp, simp only [pushforward_family, functor.comp_map, coyoneda_obj_map, hom_over_app, category.assoc], congr' 1, conv_lhs { rw ← hf'.some.fac }, simp only [← category.assoc, op_comp, functor.map_comp], congr' 1, refine (app_hom_restrict H (hom_over α (unop X)) hf'.some.map.op x).trans _, simp end } /-- (Implementation). `sheaf_coyoneda_hom` but the order of the arguments of the functor are swapped. -/ noncomputable def sheaf_yoneda_hom (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) : ℱ ⋙ yoneda ⟶ ℱ'.val ⋙ yoneda := begin let α := sheaf_coyoneda_hom H α, refine { app := _, naturality' := _ }, { intro U, refine { app := λ X, (α.app X).app U, naturality' := λ X Y f, by simpa using congr_app (α.naturality f) U } }, { intros U V i, ext X x, exact congr_fun ((α.app X).naturality i) x }, end /-- Given an natural transformation `G ⋙ ℱ ⟶ G ⋙ ℱ'` between presheaves of arbitrary category, where `G` is full and cover-dense, and `ℱ'` is a sheaf, we may obtain a natural transformation between presheaves. -/ noncomputable def sheaf_hom (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) : ℱ ⟶ ℱ'.val := let α' := sheaf_yoneda_hom H α in { app := λ X, yoneda.preimage (α'.app X), naturality' := λ X Y f, yoneda.map_injective (by simpa using α'.naturality f) } /-- Given an natural isomorphism `G ⋙ ℱ ≅ G ⋙ ℱ'` between presheaves of arbitrary category, where `G` is full and cover-dense, and `ℱ', ℱ` are sheaves, we may obtain a natural isomorphism between presheaves. -/ @[simps] noncomputable def presheaf_iso {ℱ ℱ' : Sheaf K A} (i : G.op ⋙ ℱ.val ≅ G.op ⋙ ℱ'.val) : ℱ.val ≅ ℱ'.val := begin haveI : ∀ (X : Dᵒᵖ), is_iso ((sheaf_hom H i.hom).app X), { intro X, apply is_iso_of_reflects_iso _ yoneda, use (sheaf_yoneda_hom H i.inv).app X, split; ext x : 2; simp only [sheaf_hom, nat_trans.comp_app, nat_trans.id_app, functor.image_preimage], exact ((presheaf_iso H (iso_over i (unop x))).app X).hom_inv_id, exact ((presheaf_iso H (iso_over i (unop x))).app X).inv_hom_id, apply_instance }, haveI : is_iso (sheaf_hom H i.hom) := by apply nat_iso.is_iso_of_is_iso_app, apply as_iso (sheaf_hom H i.hom), end /-- Given an natural isomorphism `G ⋙ ℱ ≅ G ⋙ ℱ'` between presheaves of arbitrary category, where `G` is full and cover-dense, and `ℱ', ℱ` are sheaves, we may obtain a natural isomorphism between presheaves. -/ @[simps] noncomputable def sheaf_iso {ℱ ℱ' : Sheaf K A} (i : G.op ⋙ ℱ.val ≅ G.op ⋙ ℱ'.val) : ℱ ≅ ℱ' := { hom := ⟨(presheaf_iso H i).hom⟩, inv := ⟨(presheaf_iso H i).inv⟩, hom_inv_id' := by { ext1, apply (presheaf_iso H i).hom_inv_id }, inv_hom_id' := by { ext1, apply (presheaf_iso H i).inv_hom_id } } /-- The constructed `sheaf_hom α` is equal to `α` when restricted onto `C`. -/ lemma sheaf_hom_restrict_eq (α : G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) : whisker_left G.op (sheaf_hom H α) = α := begin ext X, apply yoneda.map_injective, ext U, erw yoneda.image_preimage, symmetry, change (show (ℱ'.val ⋙ coyoneda.obj (op (unop U))).obj (op (G.obj (unop X))), from _) = _, apply sheaf_eq_amalgamation ℱ' (H.is_cover _), intros Y f hf, conv_lhs { rw ← hf.some.fac }, simp only [pushforward_family, functor.comp_map, yoneda_map_app, coyoneda_obj_map, op_comp, functor_to_types.map_comp_apply, hom_over_app, ← category.assoc], congr' 1, simp only [category.assoc], congr' 1, rw ← G.image_preimage hf.some.map, symmetry, apply α.naturality (G.preimage hf.some.map).op, apply_instance end /-- If the pullback map is obtained via whiskering, then the result `sheaf_hom (whisker_left G.op α)` is equal to `α`. -/ lemma sheaf_hom_eq (α : ℱ ⟶ ℱ'.val) : sheaf_hom H (whisker_left G.op α) = α := begin ext X, apply yoneda.map_injective, swap, { apply_instance }, ext U, erw yoneda.image_preimage, symmetry, change (show (ℱ'.val ⋙ coyoneda.obj (op (unop U))).obj (op (unop X)), from _) = _, apply sheaf_eq_amalgamation ℱ' (H.is_cover _), intros Y f hf, conv_lhs { rw ← hf.some.fac }, dsimp, simp, end /-- A full and cover-dense functor `G` induces an equivalence between morphisms into a sheaf and morphisms over the restrictions via `G`. -/ noncomputable def restrict_hom_equiv_hom : (G.op ⋙ ℱ ⟶ G.op ⋙ ℱ'.val) ≃ (ℱ ⟶ ℱ'.val) := { to_fun := sheaf_hom H, inv_fun := whisker_left G.op, left_inv := sheaf_hom_restrict_eq H, right_inv := sheaf_hom_eq H } /-- Given a full and cover-dense functor `G` and a natural transformation of sheaves `α : ℱ ⟶ ℱ'`, if the pullback of `α` along `G` is iso, then `α` is also iso. -/ lemma iso_of_restrict_iso {ℱ ℱ' : Sheaf K A} (α : ℱ ⟶ ℱ') (i : is_iso (whisker_left G.op α.val)) : is_iso α := begin convert is_iso.of_iso (sheaf_iso H (as_iso (whisker_left G.op α.val))) using 1, ext1, apply (sheaf_hom_eq _ _).symm end /-- A fully faithful cover-dense functor preserves compatible families. -/ lemma compatible_preserving [faithful G] : compatible_preserving K G := begin constructor, intros ℱ Z T x hx Y₁ Y₂ X f₁ f₂ g₁ g₂ hg₁ hg₂ eq, apply H.ext, intros W i, simp only [← functor_to_types.map_comp_apply, ← op_comp], rw ← G.image_preimage (i ≫ f₁), rw ← G.image_preimage (i ≫ f₂), apply hx, apply G.map_injective, simp [eq] end noncomputable instance sites.pullback.full [faithful G] (Hp : cover_preserving J K G) : full (sites.pullback A H.compatible_preserving Hp) := { preimage := λ ℱ ℱ' α, ⟨H.sheaf_hom α.val⟩, witness' := λ ℱ ℱ' α, Sheaf.hom.ext _ _ $ H.sheaf_hom_restrict_eq α.val } instance sites.pullback.faithful [faithful G] (Hp : cover_preserving J K G) : faithful (sites.pullback A H.compatible_preserving Hp) := { map_injective' := begin intros ℱ ℱ' α β e, ext1, apply_fun (λ e, e.val) at e, dsimp at e, rw [← H.sheaf_hom_eq α.val, ← H.sheaf_hom_eq β.val, e], end } end cover_dense end category_theory namespace category_theory.cover_dense open category_theory variables {C D : Type u} [category.{v} C] [category.{v} D] variables {G : C ⥤ D} [full G] [faithful G] variables {J : grothendieck_topology C} {K : grothendieck_topology D} variables {A : Type w} [category.{max u v} A] [limits.has_limits A] variables (Hd : cover_dense K G) (Hp : cover_preserving J K G) (Hl : cover_lifting J K G) include Hd Hp Hl /-- Given a functor between small sites that is cover-dense, cover-preserving, and cover-lifting, it induces an equivalence of category of sheaves valued in a complete category. -/ @[simps functor inverse] noncomputable def Sheaf_equiv_of_cover_preserving_cover_lifting : Sheaf J A ≌ Sheaf K A := begin symmetry, let α := sites.pullback_copullback_adjunction.{w v u} A Hp Hl Hd.compatible_preserving, haveI : ∀ (X : Sheaf J A), is_iso (α.counit.app X), { intro ℱ, apply_with (reflects_isomorphisms.reflects (Sheaf_to_presheaf J A)) { instances := ff }, exact is_iso.of_iso ((@as_iso _ _ _ _ _ (Ran.reflective A G.op)).app ℱ.val) }, haveI : is_iso α.counit := nat_iso.is_iso_of_is_iso_app _, exact { functor := sites.pullback A Hd.compatible_preserving Hp, inverse := sites.copullback A Hl, unit_iso := as_iso α.unit, counit_iso := as_iso α.counit, functor_unit_iso_comp' := λ ℱ, by convert α.left_triangle_components } end end category_theory.cover_dense
bae33ae27417386c1ec8ded94e54ce0ad6676d81
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/topology/topological_fiber_bundle.lean
2828b3babf44150b63e707158cac50ea7783707a
[ "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,281
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.local_homeomorph /-! # Fiber bundles A topological fiber bundle with fiber F over a base B is a space projecting on B for which the fibers are all homeomorphic to F, such that the local situation around each point is a direct product. We define a predicate `is_topological_fiber_bundle F p` saying that `p : Z → B` is a topological fiber bundle with fiber `F`. It is in general nontrivial to construct a fiber bundle. A way is to start from the knowledge of how changes of local trivializations act on the fiber. From this, one can construct the total space of the bundle and its topology by a suitable gluing construction. The main content of this file is an implementation of this construction: starting from an object of type `topological_fiber_bundle_core` registering the trivialization changes, one gets the corresponding fiber bundle and projection. ## Main definitions `bundle_trivialization F p` : structure extending local homeomorphisms, defining a local trivialization of a topological space `Z` with projection `p` and fiber `F`. `is_topological_fiber_bundle F p` : Prop saying that the map `p` between topological spaces is a fiber bundle with fiber `F`. `topological_fiber_bundle_core ι B F` : structure registering how changes of coordinates act on the fiber `F` above open subsets of `B`, where local trivializations are indexed by ι. Let `Z : topological_fiber_bundle_core ι B F`. Then we define `Z.total_space` : the total space of `Z`, defined as a Type as `B × F`, but with a twisted topology coming from the fiber bundle structure `Z.proj` : projection from `Z.total_space` to `B`. It is continuous. `Z.fiber x` : the fiber above `x`, homeomorphic to `F` (and defeq to `F` as a type). `Z.local_triv i`: for `i : ι`, a local homeomorphism from `Z.total_space` to `B × F`, that realizes a trivialization above the set `Z.base_set i`, which is an open set in `B`. ## Implementation notes A topological fiber bundle with fiber F over a base B is a family of spaces isomorphic to F, indexed by B, which is locally trivial in the following sense: there is a covering of B by open sets such that, on each such open set `s`, the bundle is isomorphic to `s × F`. To construct a fiber bundle formally, the main data is what happens when one changes trivializations from `s × F` to `s' × F` on `s ∩ s'`: one should get a family of homeomorphisms of `F`, depending continuously on the base point, satisfying basic compatibility conditions (cocycle property). Useful classes of bundles can then be specified by requiring that these homeomorphisms of `F` belong to some subgroup, preserving some structure (the "structure group of the bundle"): then these structures are inherited by the fibers of the bundle. Given such trivialization change data (encoded below in a structure called `topological_fiber_bundle_core`), one can construct the fiber bundle. The intrinsic canonical mathematical construction is the following. The fiber above `x` is the disjoint union of `F` over all trivializations, modulo the gluing identifications: one gets a fiber which is isomorphic to `F`, but non-canonically (each choice of one of the trivializations around x gives such an isomorphism). Given a trivialization over a set `s`, one gets an isomorphism between `s × F` and `proj^{-1} s`, by using the identification corresponding to this trivialization. One chooses the topology on the bundle that makes all of these into homeomorphisms. For the practical implementation, it turns out to be more convenient to avoid completely the gluing and quotienting construction above, and to declare above each `x` that the fiber is `F`, but thinking that it corresponds to the `F` coming from the choice of one trivialization around `x`. This has several practical advantages: * without any work, one gets a topological space structure on the fiber. And if `F` has more structure it is inherited for free by the fiber. * In the trivial situation of the trivial bundle where there is only one chart and one trivialization, this construction gives the product space `B × F` with the product topology. In the case of the tangent bundle of manifolds, this also implies that on vector spaces the derivative and the manifold derivative are equal. A drawback is that some silly constructions will typecheck: in the case of the tangent bundle, one can add two vectors in different tangent spaces (as they both are elements of `F` from the point of view of Lean). To solve this, one could mark the tangent space as irreducible, but then one would lose the identification of the tangent space to `F` with `F`. There is however a big advantage of this situation: even if Lean can not check that two basepoints are defeq, it will accept the fact that the tangent spaces are the same. For instance, if two maps f and g are locally inverse to each other, one can express that the composition of their derivatives is the identity of `tangent_space I x`. One could fear issues as this composition goes from `tangent_space I x` to `tangent_space I (g (f x))` (which should be the same, but should not be obvious to Lean as it does not know that `g (f x) = x`). As these types are the same to Lean (equal to `F`), there are in fact no dependent type difficulties here! For this construction of a fiber bundle from a `topological_fiber_bundle_core`, we should thus choose for each `x` one specific trivialization around it. We include this choice in the definition of the `topological_fiber_bundle_core`, as it makes some constructions more functorial and it is a nice way to say that the trivializations cover the whole space `B`. With this definition, the type of the fiber bundle space constructed from the core data is just `B × F`, but the topology is not the product one. We also take the indexing type (indexing all the trivializations) as a parameter to the fiber bundle core: it could always be taken as a subtype of all the maps from open subsets of `B` to continuous maps of `F`, but in practice it will sometimes be something else. For instance, on a manifold, one will use the set of charts as a good parameterization for the trivializations of the tangent bundle. Or for the pullback of a `topological_fiber_bundle_core`, the indexing type will be the same as for the initial bundle. ## Tags Fiber bundle, topological bundle, vector bundle, local trivialization, structure group -/ variables {ι : Type*} {B : Type*} {F : Type*} open topological_space set open_locale topological_space section topological_fiber_bundle variables {Z : Type*} [topological_space B] [topological_space Z] [topological_space F] (proj : Z → B) variable (F) /-- A structure extending local homeomorphisms, defining a local trivialization of a projection `proj : Z → B` with fiber `F`, as a local homeomorphism between `Z` and `B × F` defined between two sets of the form `proj ⁻¹' base_set` and `base_set × F`, acting trivially on the first coordinate. -/ structure bundle_trivialization extends local_homeomorph Z (B × F) := (base_set : set B) (open_base_set : is_open base_set) (source_eq : source = proj ⁻¹' base_set) (target_eq : target = set.prod base_set univ) (proj_to_fun : ∀ p ∈ source, (to_fun p).1 = proj p) instance : has_coe_to_fun (bundle_trivialization F proj) := ⟨_, λ e, e.to_fun⟩ @[simp] lemma bundle_trivialization.coe_coe (e : bundle_trivialization F proj) (x : Z) : e.to_local_homeomorph x = e x := rfl @[simp] lemma bundle_trivialization.coe_mk (e : local_homeomorph Z (B × F)) (i j k l m) (x : Z) : (bundle_trivialization.mk e i j k l m : bundle_trivialization F proj) x = e x := rfl /-- A topological fiber bundle with fiber F over a base B is a space projecting on B for which the fibers are all homeomorphic to F, such that the local situation around each point is a direct product. -/ def is_topological_fiber_bundle : Prop := ∀ x : Z, ∃e : bundle_trivialization F proj, x ∈ e.source variables {F} {proj} @[simp] lemma bundle_trivialization.coe_fst (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_to_fun x ex /-- In the domain of a bundle trivialization, the projection is continuous-/ lemma bundle_trivialization.continuous_at_proj (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : continuous_at proj x := begin assume s hs, obtain ⟨t, st, t_open, xt⟩ : ∃ t ⊆ s, is_open t ∧ proj x ∈ t, from mem_nhds_sets_iff.1 hs, rw e.source_eq at ex, let u := e.base_set ∩ t, have u_open : is_open u := is_open_inter e.open_base_set t_open, have xu : proj x ∈ u := ⟨ex, xt⟩, /- Take a small enough open neighborhood u of `proj x`, contained in a trivialization domain o. One should show that its preimage is open. -/ suffices : is_open (proj ⁻¹' u), { have : proj ⁻¹' u ∈ 𝓝 x := mem_nhds_sets this xu, apply filter.mem_sets_of_superset this, exact preimage_mono (subset.trans (inter_subset_right _ _) st) }, -- to do this, rewrite `proj ⁻¹' u` in terms of the trivialization, and use its continuity. have : proj ⁻¹' u = e ⁻¹' (set.prod u univ) ∩ e.source, { ext p, split, { assume h, have : p ∈ e.source, { rw e.source_eq, have : u ⊆ e.base_set := inter_subset_left _ _, exact preimage_mono this h }, simp [this, h.1, h.2], }, { rintros ⟨h, h_source⟩, simpa [h_source] using h } }, rw [this, inter_comm], exact continuous_on.preimage_open_of_open e.continuous_to_fun e.open_source (is_open_prod u_open is_open_univ) end /-- The projection from a topological fiber bundle to its base is continuous. -/ lemma is_topological_fiber_bundle.continuous_proj (h : is_topological_fiber_bundle F proj) : continuous proj := begin rw continuous_iff_continuous_at, assume x, rcases h x with ⟨e, ex⟩, exact e.continuous_at_proj ex end /-- The projection from a topological fiber bundle to its base is an open map. -/ lemma is_topological_fiber_bundle.is_open_map_proj (h : is_topological_fiber_bundle F proj) : is_open_map proj := begin assume s hs, rw is_open_iff_forall_mem_open, assume x xs, obtain ⟨y, ys, yx⟩ : ∃ y, y ∈ s ∧ proj y = x, from (mem_image _ _ _).1 xs, obtain ⟨e, he⟩ : ∃ (e : bundle_trivialization F proj), y ∈ e.source, from h y, refine ⟨proj '' (s ∩ e.source), image_subset _ (inter_subset_left _ _), _, ⟨y, ⟨ys, he⟩, yx⟩⟩, have : ∀z ∈ s ∩ e.source, prod.fst (e z) = proj z := λz hz, e.proj_to_fun z hz.2, rw [← image_congr this, image_comp], have : is_open (e '' (s ∩ e.source)) := e.to_local_homeomorph.image_open_of_open (is_open_inter hs e.to_local_homeomorph.open_source) (inter_subset_right _ _), exact is_open_map_fst _ this end /-- The first projection in a product is a topological fiber bundle. -/ lemma is_topological_fiber_bundle_fst : is_topological_fiber_bundle F (prod.fst : B × F → B) := begin let F : bundle_trivialization F (prod.fst : B × F → B) := { base_set := univ, open_base_set := is_open_univ, source_eq := rfl, target_eq := by simp, proj_to_fun := by simp, ..local_homeomorph.refl _ }, exact λx, ⟨F, by simp⟩ end /-- The second projection in a product is a topological fiber bundle. -/ lemma is_topological_fiber_bundle_snd : is_topological_fiber_bundle F (prod.snd : F × B → B) := begin let F : bundle_trivialization F (prod.snd : F × B → B) := { base_set := univ, open_base_set := is_open_univ, source_eq := rfl, target_eq := by simp, proj_to_fun := λp, by { simp, refl }, ..(homeomorph.prod_comm F B).to_local_homeomorph }, exact λx, ⟨F, by simp⟩ end end topological_fiber_bundle /-- Core data defining a locally trivial topological bundle with fiber `F` over a topological space `B`. Note that "bundle" is used in its mathematical sense. This is the (computer science) bundled version, i.e., all the relevant data is contained in the following structure. A family of local trivializations is indexed by a type ι, on open subsets `base_set i` for each `i : ι`. Trivialization changes from `i` to `j` are given by continuous maps `coord_change i j` from `base_set i ∩ base_set j` to the set of homeomorphisms of `F`, but we express them as maps `B → F → F` and require continuity on `(base_set i ∩ base_set j) × F` to avoid the topology on the space of continuous maps on `F`. -/ structure topological_fiber_bundle_core (ι : Type*) (B : Type*) [topological_space B] (F : Type*) [topological_space F] := (base_set : ι → set B) (is_open_base_set : ∀i, is_open (base_set i)) (index_at : B → ι) (mem_base_set_at : ∀x, x ∈ base_set (index_at x)) (coord_change : ι → ι → B → F → F) (coord_change_self : ∀i, ∀ x ∈ base_set i, ∀v, coord_change i i x v = v) (coord_change_continuous : ∀i j, continuous_on (λp : B × F, coord_change i j p.1 p.2) (set.prod ((base_set i) ∩ (base_set j)) univ)) (coord_change_comp : ∀i j k, ∀x ∈ (base_set i) ∩ (base_set j) ∩ (base_set k), ∀v, (coord_change j k x) (coord_change i j x v) = coord_change i k x v) attribute [simp] topological_fiber_bundle_core.mem_base_set_at namespace topological_fiber_bundle_core variables [topological_space B] [topological_space F] (Z : topological_fiber_bundle_core ι B F) include Z /-- The index set of a topological fiber bundle core, as a convenience function for dot notation -/ @[nolint unused_arguments] def index := ι /-- The base space of a topological fiber bundle core, as a convenience function for dot notation -/ @[nolint unused_arguments] def base := B /-- The fiber of a topological fiber bundle core, as a convenience function for dot notation and typeclass inference -/ @[nolint unused_arguments] def fiber (x : B) := F instance topological_space_fiber (x : B) : topological_space (Z.fiber x) := by { dsimp [fiber], apply_instance } /-- Total space of a topological bundle created from core. It is equal to `B × F`, but as it is not marked as reducible, typeclass inference will not infer the wrong topology, and will use the instance `topological_fiber_bundle_core.to_topological_space` with the right topology. -/ @[nolint unused_arguments] def total_space := B × F /-- The projection from the total space of a topological fiber bundle core, on its base. -/ @[simp] def proj : Z.total_space → B := λp, p.1 /-- Local homeomorphism version of the trivialization change. -/ def triv_change (i j : ι) : local_homeomorph (B × F) (B × F) := { source := set.prod (Z.base_set i ∩ Z.base_set j) univ, target := set.prod (Z.base_set i ∩ Z.base_set j) univ, to_fun := λp, ⟨p.1, Z.coord_change i j p.1 p.2⟩, inv_fun := λp, ⟨p.1, Z.coord_change j i p.1 p.2⟩, map_source' := λp hp, by simpa using hp, map_target' := λp hp, by simpa using hp, left_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx.1 }, { simp [hx] } end, right_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx.2 }, { simp [hx] }, end, open_source := is_open_prod (is_open_inter (Z.is_open_base_set i) (Z.is_open_base_set j)) is_open_univ, open_target := is_open_prod (is_open_inter (Z.is_open_base_set i) (Z.is_open_base_set j)) is_open_univ, continuous_to_fun := continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous i j), continuous_inv_fun := by simpa [inter_comm] using continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous j i) } @[simp] lemma mem_triv_change_source (i j : ι) (p : B × F) : p ∈ (Z.triv_change i j).source ↔ p.1 ∈ Z.base_set i ∩ Z.base_set j := by { erw [mem_prod], simp } /-- Associate to a trivialization index `i : ι` the corresponding trivialization, i.e., a bijection between `proj ⁻¹ (base_set i)` and `base_set i × F`. As the fiber above `x` is `F` but read in the chart with index `index_at x`, the trivialization in the fiber above x is by definition the coordinate change from i to `index_at x`, so it depends on `x`. The local trivialization will ultimately be a local homeomorphism. For now, we only introduce the local equiv version, denoted with a prime. In further developments, avoid this auxiliary version, and use Z.local_triv instead. -/ def local_triv' (i : ι) : local_equiv Z.total_space (B × F) := { source := Z.proj ⁻¹' (Z.base_set i), target := set.prod (Z.base_set i) univ, inv_fun := λp, ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩, to_fun := λp, ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩, map_source' := λp hp, by simpa only [set.mem_preimage, and_true, set.mem_univ, set.prod_mk_mem_set_prod_eq] using hp, map_target' := λp hp, by simpa only [set.mem_preimage, and_true, set.mem_univ, set.mem_prod] using hp, left_inv' := begin rintros ⟨x, v⟩ hx, change x ∈ Z.base_set i at hx, dsimp, rw [Z.coord_change_comp, Z.coord_change_self], { exact Z.mem_base_set_at _ }, { simp [hx] } end, right_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx }, { simp [hx] } end } @[simp] lemma mem_local_triv'_source (i : ι) (p : Z.total_space) : p ∈ (Z.local_triv' i).source ↔ p.1 ∈ Z.base_set i := by refl @[simp] lemma mem_local_triv'_target (i : ι) (p : B × F) : p ∈ (Z.local_triv' i).target ↔ p.1 ∈ Z.base_set i := by { erw [mem_prod], simp } @[simp] lemma local_triv'_fst (i : ι) (p : Z.total_space) : ((Z.local_triv' i) p).1 = p.1 := rfl @[simp] lemma local_triv'_inv_fst (i : ι) (p : B × F) : ((Z.local_triv' i).symm p).1 = p.1 := rfl /-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/ lemma local_triv'_trans (i j : ι) : (Z.local_triv' i).symm.trans (Z.local_triv' j) ≈ (Z.triv_change i j).to_local_equiv := begin split, { ext x, erw [mem_prod], simp [local_equiv.trans_source] }, { rintros ⟨x, v⟩ hx, simp only [triv_change, local_triv', local_equiv.symm, true_and, prod_mk_mem_set_prod_eq, local_equiv.trans_source, mem_inter_eq, and_true, mem_univ, prod.mk.inj_iff, mem_preimage, proj, local_equiv.coe_mk, eq_self_iff_true, local_equiv.coe_trans] at hx ⊢, simp [Z.coord_change_comp, hx] } end /-- Topological structure on the total space of a topological bundle created from core, designed so that all the local trivialization are continuous. -/ instance to_topological_space : topological_space Z.total_space := topological_space.generate_from $ ⋃ (i : ι) (s : set (B × F)) (s_open : is_open s), {(Z.local_triv' i).source ∩ (Z.local_triv' i) ⁻¹' s} lemma open_source' (i : ι) : is_open (Z.local_triv' i).source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨i, set.prod (Z.base_set i) univ, is_open_prod (Z.is_open_base_set i) (is_open_univ), _⟩, ext p, simp [topological_fiber_bundle_core.local_triv'_fst, topological_fiber_bundle_core.mem_local_triv'_source] end lemma open_target' (i : ι) : is_open (Z.local_triv' i).target := is_open_prod (Z.is_open_base_set i) (is_open_univ) /-- Local trivialization of a topological bundle created from core, as a local homeomorphism. -/ def local_triv (i : ι) : local_homeomorph Z.total_space (B × F) := { open_source := Z.open_source' i, open_target := Z.open_target' i, continuous_to_fun := begin rw continuous_on_open_iff (Z.open_source' i), assume s s_open, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨i, s, s_open, rfl⟩ end, continuous_inv_fun := begin apply continuous_on_open_of_generate_from (Z.open_target' i), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, obtain ⟨j, s, s_open, ts⟩ : ∃ j s, is_open s ∧ t = (local_triv' Z j).source ∩ (local_triv' Z j) ⁻¹' s := ht, rw ts, simp only [local_equiv.right_inv, preimage_inter, local_equiv.left_inv], let e := Z.local_triv' i, let e' := Z.local_triv' j, let f := e.symm.trans e', have : is_open (f.source ∩ f ⁻¹' s), { rw [local_equiv.eq_on_source_preimage (Z.local_triv'_trans i j)], exact (continuous_on_open_iff (Z.triv_change i j).open_source).1 ((Z.triv_change i j).continuous_on) _ s_open }, convert this using 1, dsimp [local_equiv.trans_source], rw [← preimage_comp, inter_assoc] end, ..Z.local_triv' i } /- We will now state again the basic properties of the local trivializations, but without primes, i.e., for the local homeomorphism instead of the local equiv. -/ @[simp] lemma mem_local_triv_source (i : ι) (p : Z.total_space) : p ∈ (Z.local_triv i).source ↔ p.1 ∈ Z.base_set i := by refl @[simp] lemma mem_local_triv_target (i : ι) (p : B × F) : p ∈ (Z.local_triv i).target ↔ p.1 ∈ Z.base_set i := by { erw [mem_prod], simp } @[simp] lemma local_triv_fst (i : ι) (p : Z.total_space) : ((Z.local_triv i) p).1 = p.1 := rfl @[simp] lemma local_triv_symm_fst (i : ι) (p : B × F) : ((Z.local_triv i).symm p).1 = p.1 := rfl /-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/ lemma local_triv_trans (i j : ι) : (Z.local_triv i).symm.trans (Z.local_triv j) ≈ Z.triv_change i j := Z.local_triv'_trans i j /-- Extended version of the local trivialization of a fiber bundle constructed from core, registering additionally in its type that it is a local bundle trivialization. -/ def local_triv_ext (i : ι) : bundle_trivialization F Z.proj := { base_set := Z.base_set i, open_base_set := Z.is_open_base_set i, source_eq := rfl, target_eq := rfl, proj_to_fun := λp hp, by simp, ..Z.local_triv i } /-- A topological fiber bundle constructed from core is indeed a topological fiber bundle. -/ theorem is_topological_fiber_bundle : is_topological_fiber_bundle F Z.proj := λx, ⟨Z.local_triv_ext (Z.index_at (Z.proj x)), by simp [local_triv_ext]⟩ /-- The projection on the base of a topological bundle created from core is continuous -/ lemma continuous_proj : continuous Z.proj := Z.is_topological_fiber_bundle.continuous_proj /-- The projection on the base of a topological bundle created from core is an open map -/ lemma is_open_map_proj : is_open_map Z.proj := Z.is_topological_fiber_bundle.is_open_map_proj /-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as a local homeomorphism -/ def local_triv_at (p : Z.total_space) : local_homeomorph Z.total_space (B × F) := Z.local_triv (Z.index_at (Z.proj p)) @[simp] lemma mem_local_triv_at_source (p : Z.total_space) : p ∈ (Z.local_triv_at p).source := by simp [local_triv_at] @[simp] lemma local_triv_at_fst (p q : Z.total_space) : ((Z.local_triv_at p) q).1 = q.1 := rfl @[simp] lemma local_triv_at_symm_fst (p : Z.total_space) (q : B × F) : ((Z.local_triv_at p).symm q).1 = q.1 := rfl /-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as a bundle trivialization -/ def local_triv_at_ext (p : Z.total_space) : bundle_trivialization F Z.proj := Z.local_triv_ext (Z.index_at (Z.proj p)) @[simp] lemma local_triv_at_ext_to_local_homeomorph (p : Z.total_space) : (Z.local_triv_at_ext p).to_local_homeomorph = Z.local_triv_at p := rfl end topological_fiber_bundle_core
ef3cd6f8bca80934c92b4c28aff193de7d07121f
4727251e0cd73359b15b664c3170e5d754078599
/src/data/set/accumulate.lean
6e7eb323a9778a85b3b6ea079e3738855513f8e7
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,519
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 data.set.lattice /-! # Accumulate The function `accumulate` takes a set `s` and returns `⋃ y ≤ x, s y`. -/ variables {α β γ : Type*} {s : α → set β} {t : α → set γ} namespace set /-- `accumulate s` is the union of `s y` for `y ≤ x`. -/ def accumulate [has_le α] (s : α → set β) (x : α) : set β := ⋃ y ≤ x, s y variable {s} lemma accumulate_def [has_le α] {x : α} : accumulate s x = ⋃ y ≤ x, s y := rfl @[simp] lemma mem_accumulate [has_le α] {x : α} {z : β} : z ∈ accumulate s x ↔ ∃ y ≤ x, z ∈ s y := mem_Union₂ lemma subset_accumulate [preorder α] {x : α} : s x ⊆ accumulate s x := λ z, mem_bUnion le_rfl lemma monotone_accumulate [preorder α] : monotone (accumulate s) := λ x y hxy, bUnion_subset_bUnion_left $ λ z hz, le_trans hz hxy lemma bUnion_accumulate [preorder α] (x : α) : (⋃ y ≤ x, accumulate s y) = ⋃ y ≤ x, s y := begin apply subset.antisymm, { exact Union₂_subset (λ y hy, monotone_accumulate hy) }, { exact Union₂_mono (λ y hy, subset_accumulate) } end lemma Union_accumulate [preorder α] : (⋃ x, accumulate s x) = ⋃ x, s x := begin apply subset.antisymm, { simp only [subset_def, mem_Union, exists_imp_distrib, mem_accumulate], intros z x x' hx'x hz, exact ⟨x', hz⟩ }, { exact Union_mono (λ i, subset_accumulate), } end end set
b961bbbbccacc288faf1d64bfb0c5dcb21d2a71d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/fintype/array.lean
134d9d2837d71a4d5b314c28b8a4bbff9a0abbe8
[ "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
654
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.fintype.pi import logic.equiv.array /-! # `array n α` is a fintype when `α` is. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α : Type*} 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
8c229dddeae16fc58e9eaa3e9ab7fd36aade32b1
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/data/nat/prime.lean
3b8e6f089d36cf79ad6afeb6cd05d0dcc5bed546
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
31,191
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, Mario Carneiro -/ import data.nat.sqrt import data.nat.gcd import algebra.group_power import tactic.wlog import tactic.norm_num /-! # Prime numbers This file deals with prime numbers: natural numbers `p ≥ 2` whose only divisors are `p` and `1`. ## Important declarations All the following declarations exist in the namespace `nat`. - `prime`: the predicate that expresses that a natural number `p` is prime - `primes`: the subtype of natural numbers that are prime - `min_fac n`: the minimal prime factor of a natural number `n ≠ 1` - `exists_infinite_primes`: Euclid's theorem that there exist infinitely many prime numbers - `factors n`: the prime factorization of `n` - `factors_unique`: uniqueness of the prime factorisation -/ open bool subtype open_locale nat namespace nat /-- `prime p` means that `p` is a prime number, that is, a natural number at least 2 whose only divisors are `p` and `1`. -/ @[pp_nodot] def prime (p : ℕ) := 2 ≤ p ∧ ∀ m ∣ p, m = 1 ∨ m = p theorem prime.two_le {p : ℕ} : prime p → 2 ≤ p := and.left theorem prime.one_lt {p : ℕ} : prime p → 1 < p := prime.two_le instance prime.one_lt' (p : ℕ) [hp : _root_.fact p.prime] : _root_.fact (1 < p) := ⟨hp.1.one_lt⟩ lemma prime.ne_one {p : ℕ} (hp : p.prime) : p ≠ 1 := ne.symm $ ne_of_lt hp.one_lt theorem prime_def_lt {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m < p, m ∣ p → m = 1 := and_congr_right $ λ p2, forall_congr $ λ m, ⟨λ h l d, (h d).resolve_right (ne_of_lt l), λ h d, (decidable.lt_or_eq_of_le $ le_of_dvd (le_of_succ_le p2) d).imp_left (λ l, h l d)⟩ theorem prime_def_lt' {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m < p → ¬ m ∣ p := prime_def_lt.trans $ and_congr_right $ λ p2, forall_congr $ λ m, ⟨λ h m2 l d, not_lt_of_ge m2 ((h l d).symm ▸ dec_trivial), λ h l d, begin rcases m with _|_|m, { rw eq_zero_of_zero_dvd d at p2, revert p2, exact dec_trivial }, { refl }, { exact (h dec_trivial l).elim d } end⟩ theorem prime_def_le_sqrt {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m ≤ sqrt p → ¬ m ∣ p := prime_def_lt'.trans $ and_congr_right $ λ p2, ⟨λ a m m2 l, a m m2 $ lt_of_le_of_lt l $ sqrt_lt_self p2, λ a, have ∀ {m k}, m ≤ k → 1 < m → p ≠ m * k, from λ m k mk m1 e, a m m1 (le_sqrt.2 (e.symm ▸ mul_le_mul_left m mk)) ⟨k, e⟩, λ m m2 l ⟨k, e⟩, begin cases (le_total m k) with mk km, { exact this mk m2 e }, { rw [mul_comm] at e, refine this km (lt_of_mul_lt_mul_right _ (zero_le m)) e, rwa [one_mul, ← e] } end⟩ section /-- This instance is slower than the instance `decidable_prime` defined below, but has the advantage that it works in the kernel for small values. If you need to prove that a particular number is prime, in any case you should not use `dec_trivial`, but rather `by norm_num`, which is much faster. -/ local attribute [instance] def decidable_prime_1 (p : ℕ) : decidable (prime p) := decidable_of_iff' _ prime_def_lt' lemma prime.ne_zero {n : ℕ} (h : prime n) : n ≠ 0 := by { rintro rfl, revert h, dec_trivial } theorem prime.pos {p : ℕ} (pp : prime p) : 0 < p := lt_of_succ_lt pp.one_lt theorem not_prime_zero : ¬ prime 0 := by simp [prime] theorem not_prime_one : ¬ prime 1 := by simp [prime] theorem prime_two : prime 2 := dec_trivial end theorem prime.pred_pos {p : ℕ} (pp : prime p) : 0 < pred p := lt_pred_iff.2 pp.one_lt theorem succ_pred_prime {p : ℕ} (pp : prime p) : succ (pred p) = p := succ_pred_eq_of_pos pp.pos theorem dvd_prime {p m : ℕ} (pp : prime p) : m ∣ p ↔ m = 1 ∨ m = p := ⟨λ d, pp.2 m d, λ h, h.elim (λ e, e.symm ▸ one_dvd _) (λ e, e.symm ▸ dvd_refl _)⟩ theorem dvd_prime_two_le {p m : ℕ} (pp : prime p) (H : 2 ≤ m) : m ∣ p ↔ m = p := (dvd_prime pp).trans $ or_iff_right_of_imp $ not.elim $ ne_of_gt H theorem prime_dvd_prime_iff_eq {p q : ℕ} (pp : p.prime) (qp : q.prime) : p ∣ q ↔ p = q := dvd_prime_two_le qp (prime.two_le pp) theorem prime.not_dvd_one {p : ℕ} (pp : prime p) : ¬ p ∣ 1 | d := (not_le_of_gt pp.one_lt) $ le_of_dvd dec_trivial d theorem not_prime_mul {a b : ℕ} (a1 : 1 < a) (b1 : 1 < b) : ¬ prime (a * b) := λ h, ne_of_lt (nat.mul_lt_mul_of_pos_left b1 (lt_of_succ_lt a1)) $ by simpa using (dvd_prime_two_le h a1).1 (dvd_mul_right _ _) lemma not_prime_mul' {a b n : ℕ} (h : a * b = n) (h₁ : 1 < a) (h₂ : 1 < b) : ¬ prime n := by { rw ← h, exact not_prime_mul h₁ h₂ } section min_fac private lemma min_fac_lemma (n k : ℕ) (h : ¬ n < k * k) : sqrt n - k < sqrt n + 2 - k := (nat.sub_lt_sub_right_iff $ le_sqrt.2 $ le_of_not_gt h).2 $ nat.lt_add_of_pos_right dec_trivial /-- If `n < k * k`, then `min_fac_aux n k = n`, if `k | n`, then `min_fac_aux n k = k`. Otherwise, `min_fac_aux n k = min_fac_aux n (k+2)` using well-founded recursion. If `n` is odd and `1 < n`, then then `min_fac_aux n 3` is the smallest prime factor of `n`. -/ def min_fac_aux (n : ℕ) : ℕ → ℕ | k := if h : n < k * k then n else if k ∣ n then k else have _, from min_fac_lemma n k h, min_fac_aux (k + 2) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ k, sqrt n + 2 - k)⟩]} /-- Returns the smallest prime factor of `n ≠ 1`. -/ def min_fac : ℕ → ℕ | 0 := 2 | 1 := 1 | (n+2) := if 2 ∣ n then 2 else min_fac_aux (n + 2) 3 @[simp] theorem min_fac_zero : min_fac 0 = 2 := rfl @[simp] theorem min_fac_one : min_fac 1 = 1 := rfl theorem min_fac_eq : ∀ n, min_fac n = if 2 ∣ n then 2 else min_fac_aux n 3 | 0 := by simp | 1 := by simp [show 2≠1, from dec_trivial]; rw min_fac_aux; refl | (n+2) := have 2 ∣ n + 2 ↔ 2 ∣ n, from (nat.dvd_add_iff_left (by refl)).symm, by simp [min_fac, this]; congr private def min_fac_prop (n k : ℕ) := 2 ≤ k ∧ k ∣ n ∧ ∀ m, 2 ≤ m → m ∣ n → k ≤ m theorem min_fac_aux_has_prop {n : ℕ} (n2 : 2 ≤ n) (nd2 : ¬ 2 ∣ n) : ∀ k i, k = 2*i+3 → (∀ m, 2 ≤ m → m ∣ n → k ≤ m) → min_fac_prop n (min_fac_aux n k) | k := λ i e a, begin rw min_fac_aux, by_cases h : n < k*k; simp [h], { have pp : prime n := prime_def_le_sqrt.2 ⟨n2, λ m m2 l d, not_lt_of_ge l $ lt_of_lt_of_le (sqrt_lt.2 h) (a m m2 d)⟩, from ⟨n2, dvd_refl _, λ m m2 d, le_of_eq ((dvd_prime_two_le pp m2).1 d).symm⟩ }, have k2 : 2 ≤ k, { subst e, exact dec_trivial }, by_cases dk : k ∣ n; simp [dk], { exact ⟨k2, dk, a⟩ }, { refine have _, from min_fac_lemma n k h, min_fac_aux_has_prop (k+2) (i+1) (by simp [e, left_distrib]) (λ m m2 d, _), cases nat.eq_or_lt_of_le (a m m2 d) with me ml, { subst me, contradiction }, apply (nat.eq_or_lt_of_le ml).resolve_left, intro me, rw [← me, e] at d, change 2 * (i + 2) ∣ n at d, have := dvd_of_mul_right_dvd d, contradiction } end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ k, sqrt n + 2 - k)⟩]} theorem min_fac_has_prop {n : ℕ} (n1 : n ≠ 1) : min_fac_prop n (min_fac n) := begin by_cases n0 : n = 0, {simp [n0, min_fac_prop, ge]}, have n2 : 2 ≤ n, { revert n0 n1, rcases n with _|_|_; exact dec_trivial }, simp [min_fac_eq], by_cases d2 : 2 ∣ n; simp [d2], { exact ⟨le_refl _, d2, λ k k2 d, k2⟩ }, { refine min_fac_aux_has_prop n2 d2 3 0 rfl (λ m m2 d, (nat.eq_or_lt_of_le m2).resolve_left (mt _ d2)), exact λ e, e.symm ▸ d } end theorem min_fac_dvd (n : ℕ) : min_fac n ∣ n := if n1 : n = 1 then by simp [n1] else (min_fac_has_prop n1).2.1 theorem min_fac_prime {n : ℕ} (n1 : n ≠ 1) : prime (min_fac n) := let ⟨f2, fd, a⟩ := min_fac_has_prop n1 in prime_def_lt'.2 ⟨f2, λ m m2 l d, not_le_of_gt l (a m m2 (dvd_trans d fd))⟩ theorem min_fac_le_of_dvd {n : ℕ} : ∀ {m : ℕ}, 2 ≤ m → m ∣ n → min_fac n ≤ m := by by_cases n1 : n = 1; [exact λ m m2 d, n1.symm ▸ le_trans dec_trivial m2, exact (min_fac_has_prop n1).2.2] theorem min_fac_pos (n : ℕ) : 0 < min_fac n := by by_cases n1 : n = 1; [exact n1.symm ▸ dec_trivial, exact (min_fac_prime n1).pos] theorem min_fac_le {n : ℕ} (H : 0 < n) : min_fac n ≤ n := le_of_dvd H (min_fac_dvd n) theorem prime_def_min_fac {p : ℕ} : prime p ↔ 2 ≤ p ∧ min_fac p = p := ⟨λ pp, ⟨pp.two_le, let ⟨f2, fd, a⟩ := min_fac_has_prop $ ne_of_gt pp.one_lt in ((dvd_prime pp).1 fd).resolve_left (ne_of_gt f2)⟩, λ ⟨p2, e⟩, e ▸ min_fac_prime (ne_of_gt p2)⟩ /-- This instance is faster in the virtual machine than `decidable_prime_1`, but slower in the kernel. If you need to prove that a particular number is prime, in any case you should not use `dec_trivial`, but rather `by norm_num`, which is much faster. -/ instance decidable_prime (p : ℕ) : decidable (prime p) := decidable_of_iff' _ prime_def_min_fac theorem not_prime_iff_min_fac_lt {n : ℕ} (n2 : 2 ≤ n) : ¬ prime n ↔ min_fac n < n := (not_congr $ prime_def_min_fac.trans $ and_iff_right n2).trans $ (lt_iff_le_and_ne.trans $ and_iff_right $ min_fac_le $ le_of_succ_le n2).symm lemma min_fac_le_div {n : ℕ} (pos : 0 < n) (np : ¬ prime n) : min_fac n ≤ n / min_fac n := match min_fac_dvd n with | ⟨0, h0⟩ := absurd pos $ by rw [h0, mul_zero]; exact dec_trivial | ⟨1, h1⟩ := begin rw mul_one at h1, rw [prime_def_min_fac, not_and_distrib, ← h1, eq_self_iff_true, not_true, or_false, not_le] at np, rw [le_antisymm (le_of_lt_succ np) (succ_le_of_lt pos), min_fac_one, nat.div_one] end | ⟨(x+2), hx⟩ := begin conv_rhs { congr, rw hx }, rw [nat.mul_div_cancel_left _ (min_fac_pos _)], exact min_fac_le_of_dvd dec_trivial ⟨min_fac n, by rwa mul_comm⟩ end end /-- The square of the smallest prime factor of a composite number `n` is at most `n`. -/ lemma min_fac_sq_le_self {n : ℕ} (w : 0 < n) (h : ¬ prime n) : (min_fac n)^2 ≤ n := have t : (min_fac n) ≤ (n/min_fac n) := min_fac_le_div w h, calc (min_fac n)^2 = (min_fac n) * (min_fac n) : sq (min_fac n) ... ≤ (n/min_fac n) * (min_fac n) : mul_le_mul_right (min_fac n) t ... ≤ n : div_mul_le_self n (min_fac n) @[simp] lemma min_fac_eq_one_iff {n : ℕ} : min_fac n = 1 ↔ n = 1 := begin split, { intro h, by_contradiction hn, have := min_fac_prime hn, rw h at this, exact not_prime_one this, }, { rintro rfl, refl, } end @[simp] lemma min_fac_eq_two_iff (n : ℕ) : min_fac n = 2 ↔ 2 ∣ n := begin split, { intro h, convert min_fac_dvd _, rw h, }, { intro h, have ub := min_fac_le_of_dvd (le_refl 2) h, have lb := min_fac_pos n, -- If `interval_cases` and `norm_num` were already available here, -- this would be easy and pleasant. -- But they aren't, so it isn't. cases h : n.min_fac with m, { rw h at lb, cases lb, }, { cases m with m, { simp at h, subst h, cases h with n h, cases n; cases h, }, { cases m with m, { refl, }, { rw h at ub, cases ub with _ ub, cases ub with _ ub, cases ub, } } } } end end min_fac theorem exists_dvd_of_not_prime {n : ℕ} (n2 : 2 ≤ n) (np : ¬ prime n) : ∃ m, m ∣ n ∧ m ≠ 1 ∧ m ≠ n := ⟨min_fac n, min_fac_dvd _, ne_of_gt (min_fac_prime (ne_of_gt n2)).one_lt, ne_of_lt $ (not_prime_iff_min_fac_lt n2).1 np⟩ theorem exists_dvd_of_not_prime2 {n : ℕ} (n2 : 2 ≤ n) (np : ¬ prime n) : ∃ m, m ∣ n ∧ 2 ≤ m ∧ m < n := ⟨min_fac n, min_fac_dvd _, (min_fac_prime (ne_of_gt n2)).two_le, (not_prime_iff_min_fac_lt n2).1 np⟩ theorem exists_prime_and_dvd {n : ℕ} (n2 : 2 ≤ n) : ∃ p, prime p ∧ p ∣ n := ⟨min_fac n, min_fac_prime (ne_of_gt n2), min_fac_dvd _⟩ /-- Euclid's theorem. There exist infinitely many prime numbers. Here given in the form: for every `n`, there exists a prime number `p ≥ n`. -/ theorem exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ prime p := let p := min_fac (n! + 1) in have f1 : n! + 1 ≠ 1, from ne_of_gt $ succ_lt_succ $ factorial_pos _, have pp : prime p, from min_fac_prime f1, have np : n ≤ p, from le_of_not_ge $ λ h, have h₁ : p ∣ n!, from dvd_factorial (min_fac_pos _) h, have h₂ : p ∣ 1, from (nat.dvd_add_iff_right h₁).2 (min_fac_dvd _), pp.not_dvd_one h₂, ⟨p, np, pp⟩ lemma prime.eq_two_or_odd {p : ℕ} (hp : prime p) : p = 2 ∨ p % 2 = 1 := (nat.mod_two_eq_zero_or_one p).elim (λ h, or.inl ((hp.2 2 (dvd_of_mod_eq_zero h)).resolve_left dec_trivial).symm) or.inr theorem coprime_of_dvd {m n : ℕ} (H : ∀ k, prime k → k ∣ m → ¬ k ∣ n) : coprime m n := begin cases eq_zero_or_pos (gcd m n) with g0 g1, { rw [eq_zero_of_gcd_eq_zero_left g0, eq_zero_of_gcd_eq_zero_right g0] at H, exfalso, exact H 2 prime_two (dvd_zero _) (dvd_zero _) }, apply eq.symm, change 1 ≤ _ at g1, apply (lt_or_eq_of_le g1).resolve_left, intro g2, obtain ⟨p, hp, hpdvd⟩ := exists_prime_and_dvd g2, apply H p hp; apply dvd_trans hpdvd, { exact gcd_dvd_left _ _ }, { exact gcd_dvd_right _ _ } end theorem coprime_of_dvd' {m n : ℕ} (H : ∀ k, prime k → k ∣ m → k ∣ n → k ∣ 1) : coprime m n := coprime_of_dvd $ λk kp km kn, not_le_of_gt kp.one_lt $ le_of_dvd zero_lt_one $ H k kp km kn theorem factors_lemma {k} : (k+2) / min_fac (k+2) < k+2 := div_lt_self dec_trivial (min_fac_prime dec_trivial).one_lt /-- `factors n` is the prime factorization of `n`, listed in increasing order. -/ def factors : ℕ → list ℕ | 0 := [] | 1 := [] | n@(k+2) := let m := min_fac n in have n / m < n := factors_lemma, m :: factors (n / m) @[simp] lemma factors_zero : factors 0 = [] := by rw factors @[simp] lemma factors_one : factors 1 = [] := by rw factors lemma prime_of_mem_factors : ∀ {n p}, p ∈ factors n → prime p | 0 := by simp | 1 := by simp | n@(k+2) := λ p h, let m := min_fac n in have n / m < n := factors_lemma, have h₁ : p = m ∨ p ∈ (factors (n / m)) := (list.mem_cons_iff _ _ _).1 (by rwa [factors] at h), or.cases_on h₁ (λ h₂, h₂.symm ▸ min_fac_prime dec_trivial) prime_of_mem_factors lemma prod_factors : ∀ {n}, 0 < n → list.prod (factors n) = n | 0 := by simp | 1 := by simp | n@(k+2) := λ h, let m := min_fac n in have n / m < n := factors_lemma, show (factors n).prod = n, from have h₁ : 0 < n / m := nat.pos_of_ne_zero $ λ h, have n = 0 * m := (nat.div_eq_iff_eq_mul_left (min_fac_pos _) (min_fac_dvd _)).1 h, by rw zero_mul at this; exact (show k + 2 ≠ 0, from dec_trivial) this, by rw [factors, list.prod_cons, prod_factors h₁, nat.mul_div_cancel' (min_fac_dvd _)] lemma factors_prime {p : ℕ} (hp : nat.prime p) : p.factors = [p] := begin have : p = (p - 2) + 2 := (nat.sub_eq_iff_eq_add hp.1).mp rfl, rw [this, nat.factors], simp only [eq.symm this], have : nat.min_fac p = p := (nat.prime_def_min_fac.mp hp).2, split, { exact this, }, { simp only [this, nat.factors, nat.div_self (nat.prime.pos hp)], }, end /-- `factors` can be constructed inductively by extracting `min_fac`, for sufficiently large `n`. -/ lemma factors_add_two (n : ℕ) : factors (n+2) = (min_fac (n+2)) :: (factors ((n+2) / (min_fac (n+2)))) := by rw factors @[simp] lemma factors_eq_nil (n : ℕ) : n.factors = [] ↔ n = 0 ∨ n = 1 := begin split; intro h, { rcases n with (_ | _ | n), { exact or.inl rfl }, { exact or.inr rfl }, { rw factors at h, injection h }, }, { rcases h with (rfl | rfl), { exact factors_zero }, { exact factors_one }, } end theorem prime.coprime_iff_not_dvd {p n : ℕ} (pp : prime p) : coprime p n ↔ ¬ p ∣ n := ⟨λ co d, pp.not_dvd_one $ co.dvd_of_dvd_mul_left (by simp [d]), λ nd, coprime_of_dvd $ λ m m2 mp, ((prime_dvd_prime_iff_eq m2 pp).1 mp).symm ▸ nd⟩ theorem prime.dvd_iff_not_coprime {p n : ℕ} (pp : prime p) : p ∣ n ↔ ¬ coprime p n := iff_not_comm.2 pp.coprime_iff_not_dvd theorem prime.not_coprime_iff_dvd {m n : ℕ} : ¬ coprime m n ↔ ∃p, prime p ∧ p ∣ m ∧ p ∣ n := begin apply iff.intro, { intro h, exact ⟨min_fac (gcd m n), min_fac_prime h, (dvd.trans (min_fac_dvd (gcd m n)) (gcd_dvd_left m n)), (dvd.trans (min_fac_dvd (gcd m n)) (gcd_dvd_right m n))⟩ }, { intro h, cases h with p hp, apply nat.not_coprime_of_dvd_of_dvd (prime.one_lt hp.1) hp.2.1 hp.2.2 } end theorem prime.dvd_mul {p m n : ℕ} (pp : prime p) : p ∣ m * n ↔ p ∣ m ∨ p ∣ n := ⟨λ H, or_iff_not_imp_left.2 $ λ h, (pp.coprime_iff_not_dvd.2 h).dvd_of_dvd_mul_left H, or.rec (λ h, dvd_mul_of_dvd_left h _) (λ h, dvd_mul_of_dvd_right h _)⟩ theorem prime.not_dvd_mul {p m n : ℕ} (pp : prime p) (Hm : ¬ p ∣ m) (Hn : ¬ p ∣ n) : ¬ p ∣ m * n := mt pp.dvd_mul.1 $ by simp [Hm, Hn] theorem prime.dvd_of_dvd_pow {p m n : ℕ} (pp : prime p) (h : p ∣ m^n) : p ∣ m := by induction n with n IH; [exact pp.not_dvd_one.elim h, by { rw pow_succ at h, exact (pp.dvd_mul.1 h).elim id IH } ] lemma prime.pow_not_prime {x n : ℕ} (hn : 2 ≤ n) : ¬ (x ^ n).prime := λ hp, (hp.2 x $ dvd_trans ⟨x, sq _⟩ (pow_dvd_pow _ hn)).elim (λ hx1, hp.ne_one $ hx1.symm ▸ one_pow _) (λ hxn, lt_irrefl x $ calc x = x ^ 1 : (pow_one _).symm ... < x ^ n : nat.pow_right_strict_mono (hxn.symm ▸ hp.two_le) hn ... = x : hxn.symm) lemma prime.mul_eq_prime_sq_iff {x y p : ℕ} (hp : p.prime) (hx : x ≠ 1) (hy : y ≠ 1) : x * y = p ^ 2 ↔ x = p ∧ y = p := ⟨λ h, have pdvdxy : p ∣ x * y, by rw h; simp [sq], begin wlog := hp.dvd_mul.1 pdvdxy using x y, cases case with a ha, have hap : a ∣ p, from ⟨y, by rwa [ha, sq, mul_assoc, nat.mul_right_inj hp.pos, eq_comm] at h⟩, exact ((nat.dvd_prime hp).1 hap).elim (λ _, by clear_aux_decl; simp [*, sq, nat.mul_right_inj hp.pos] at * {contextual := tt}) (λ _, by clear_aux_decl; simp [*, sq, mul_comm, mul_assoc, nat.mul_right_inj hp.pos, nat.mul_right_eq_self_iff hp.pos] at * {contextual := tt}) end, λ ⟨h₁, h₂⟩, h₁.symm ▸ h₂.symm ▸ (sq _).symm⟩ lemma prime.dvd_factorial : ∀ {n p : ℕ} (hp : prime p), p ∣ n! ↔ p ≤ n | 0 p hp := iff_of_false hp.not_dvd_one (not_le_of_lt hp.pos) | (n+1) p hp := begin rw [factorial_succ, hp.dvd_mul, prime.dvd_factorial hp], exact ⟨λ h, h.elim (le_of_dvd (succ_pos _)) le_succ_of_le, λ h, (_root_.lt_or_eq_of_le h).elim (or.inr ∘ le_of_lt_succ) (λ h, or.inl $ by rw h)⟩ end theorem prime.coprime_pow_of_not_dvd {p m a : ℕ} (pp : prime p) (h : ¬ p ∣ a) : coprime a (p^m) := (pp.coprime_iff_not_dvd.2 h).symm.pow_right _ theorem coprime_primes {p q : ℕ} (pp : prime p) (pq : prime q) : coprime p q ↔ p ≠ q := pp.coprime_iff_not_dvd.trans $ not_congr $ dvd_prime_two_le pq pp.two_le theorem coprime_pow_primes {p q : ℕ} (n m : ℕ) (pp : prime p) (pq : prime q) (h : p ≠ q) : coprime (p^n) (q^m) := ((coprime_primes pp pq).2 h).pow _ _ theorem coprime_or_dvd_of_prime {p} (pp : prime p) (i : ℕ) : coprime p i ∨ p ∣ i := by rw [pp.dvd_iff_not_coprime]; apply em theorem dvd_prime_pow {p : ℕ} (pp : prime p) {m i : ℕ} : i ∣ (p^m) ↔ ∃ k ≤ m, i = p^k := begin induction m with m IH generalizing i, {simp [pow_succ, le_zero_iff] at *}, by_cases p ∣ i, { cases h with a e, subst e, rw [pow_succ, nat.mul_dvd_mul_iff_left pp.pos, IH], split; intro h; rcases h with ⟨k, h, e⟩, { exact ⟨succ k, succ_le_succ h, by rw [e, pow_succ]; refl⟩ }, cases k with k, { apply pp.not_dvd_one.elim, simp at e, rw ← e, apply dvd_mul_right }, { refine ⟨k, le_of_succ_le_succ h, _⟩, rwa [mul_comm, pow_succ', nat.mul_left_inj pp.pos] at e } }, { split; intro d, { rw (pp.coprime_pow_of_not_dvd h).eq_one_of_dvd d, exact ⟨0, zero_le _, rfl⟩ }, { rcases d with ⟨k, l, e⟩, rw e, exact pow_dvd_pow _ l } } end /-- If `p` is prime, and `a` doesn't divide `p^k`, but `a` does divide `p^(k+1)` then `a = p^(k+1)`. -/ lemma eq_prime_pow_of_dvd_least_prime_pow {a p k : ℕ} (pp : prime p) (h₁ : ¬(a ∣ p^k)) (h₂ : a ∣ p^(k+1)) : a = p^(k+1) := begin obtain ⟨l, ⟨h, rfl⟩⟩ := (dvd_prime_pow pp).1 h₂, congr, exact le_antisymm h (not_le.1 ((not_congr (pow_dvd_pow_iff_le_right (prime.one_lt pp))).1 h₁)), end section open list lemma mem_list_primes_of_dvd_prod {p : ℕ} (hp : prime p) : ∀ {l : list ℕ}, (∀ p ∈ l, prime p) → p ∣ prod l → p ∈ l | [] := λ h₁ h₂, absurd h₂ (prime.not_dvd_one hp) | (q :: l) := λ h₁ h₂, have h₃ : p ∣ q * prod l := @prod_cons _ _ l q ▸ h₂, have hq : prime q := h₁ q (mem_cons_self _ _), or.cases_on ((prime.dvd_mul hp).1 h₃) (λ h, by rw [prime.dvd_iff_not_coprime hp, coprime_primes hp hq, ne.def, not_not] at h; exact h ▸ mem_cons_self _ _) (λ h, have hl : ∀ p ∈ l, prime p := λ p hlp, h₁ p ((mem_cons_iff _ _ _).2 (or.inr hlp)), (mem_cons_iff _ _ _).2 (or.inr (mem_list_primes_of_dvd_prod hl h))) lemma mem_factors_iff_dvd {n p : ℕ} (hn : 0 < n) (hp : prime p) : p ∈ factors n ↔ p ∣ n := ⟨λ h, prod_factors hn ▸ list.dvd_prod h, λ h, mem_list_primes_of_dvd_prod hp (@prime_of_mem_factors n) ((prod_factors hn).symm ▸ h)⟩ lemma mem_factors {n p} (hn : 0 < n) : p ∈ factors n ↔ prime p ∧ p ∣ n := ⟨λ h, ⟨prime_of_mem_factors h, (mem_factors_iff_dvd hn $ prime_of_mem_factors h).mp h⟩, λ ⟨hprime, hdvd⟩, (mem_factors_iff_dvd hn hprime).mpr hdvd⟩ lemma perm_of_prod_eq_prod : ∀ {l₁ l₂ : list ℕ}, prod l₁ = prod l₂ → (∀ p ∈ l₁, prime p) → (∀ p ∈ l₂, prime p) → l₁ ~ l₂ | [] [] _ _ _ := perm.nil | [] (a :: l) h₁ h₂ h₃ := have ha : a ∣ 1 := @prod_nil ℕ _ ▸ h₁.symm ▸ (@prod_cons _ _ l a).symm ▸ dvd_mul_right _ _, absurd ha (prime.not_dvd_one (h₃ a (mem_cons_self _ _))) | (a :: l) [] h₁ h₂ h₃ := have ha : a ∣ 1 := @prod_nil ℕ _ ▸ h₁ ▸ (@prod_cons _ _ l a).symm ▸ dvd_mul_right _ _, absurd ha (prime.not_dvd_one (h₂ a (mem_cons_self _ _))) | (a :: l₁) (b :: l₂) h hl₁ hl₂ := have hl₁' : ∀ p ∈ l₁, prime p := λ p hp, hl₁ p (mem_cons_of_mem _ hp), have hl₂' : ∀ p ∈ (b :: l₂).erase a, prime p := λ p hp, hl₂ p (mem_of_mem_erase hp), have ha : a ∈ (b :: l₂) := mem_list_primes_of_dvd_prod (hl₁ a (mem_cons_self _ _)) hl₂ (h ▸ by rw prod_cons; exact dvd_mul_right _ _), have hb : b :: l₂ ~ a :: (b :: l₂).erase a := perm_cons_erase ha, have hl : prod l₁ = prod ((b :: l₂).erase a) := (nat.mul_right_inj (prime.pos (hl₁ a (mem_cons_self _ _)))).1 $ by rwa [← prod_cons, ← prod_cons, ← hb.prod_eq], perm.trans ((perm_of_prod_eq_prod hl hl₁' hl₂').cons _) hb.symm lemma factors_unique {n : ℕ} {l : list ℕ} (h₁ : prod l = n) (h₂ : ∀ p ∈ l, prime p) : l ~ factors n := have hn : 0 < n := nat.pos_of_ne_zero $ λ h, begin rw h at *, clear h, induction l with a l hi, { exact absurd h₁ dec_trivial }, { rw prod_cons at h₁, exact nat.mul_ne_zero (ne_of_lt (prime.pos (h₂ a (mem_cons_self _ _)))).symm (hi (λ p hp, h₂ p (mem_cons_of_mem _ hp))) h₁ } end, perm_of_prod_eq_prod (by rwa prod_factors hn) h₂ (@prime_of_mem_factors _) end lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : prime p) {m n k l : ℕ} (hpm : p ^ k ∣ m) (hpn : p ^ l ∣ n) (hpmn : p ^ (k+l+1) ∣ m*n) : p ^ (k+1) ∣ m ∨ p ^ (l+1) ∣ n := have hpd : p^(k+l)*p ∣ m*n, by rwa pow_succ' at hpmn, have hpd2 : p ∣ (m*n) / p ^ (k+l), from dvd_div_of_mul_dvd hpd, have hpd3 : p ∣ (m*n) / (p^k * p^l), by simpa [pow_add] using hpd2, have hpd4 : p ∣ (m / p^k) * (n / p^l), by simpa [nat.div_mul_div hpm hpn] using hpd3, have hpd5 : p ∣ (m / p^k) ∨ p ∣ (n / p^l), from (prime.dvd_mul p_prime).1 hpd4, suffices p^k*p ∣ m ∨ p^l*p ∣ n, by rwa [pow_succ', pow_succ'], hpd5.elim (assume : p ∣ m / p ^ k, or.inl $ mul_dvd_of_dvd_div hpm this) (assume : p ∣ n / p ^ l, or.inr $ mul_dvd_of_dvd_div hpn this) /-- The type of prime numbers -/ def primes := {p : ℕ // p.prime} namespace primes instance : has_repr nat.primes := ⟨λ p, repr p.val⟩ instance inhabited_primes : inhabited primes := ⟨⟨2, prime_two⟩⟩ instance coe_nat : has_coe nat.primes ℕ := ⟨subtype.val⟩ theorem coe_nat_inj (p q : nat.primes) : (p : ℕ) = (q : ℕ) → p = q := λ h, subtype.eq h end primes instance monoid.prime_pow {α : Type*} [monoid α] : has_pow α primes := ⟨λ x p, x^p.val⟩ end nat /-! ### Primality prover -/ namespace tactic namespace norm_num open norm_num lemma is_prime_helper (n : ℕ) (h₁ : 1 < n) (h₂ : nat.min_fac n = n) : nat.prime n := nat.prime_def_min_fac.2 ⟨h₁, h₂⟩ lemma min_fac_bit0 (n : ℕ) : nat.min_fac (bit0 n) = 2 := by simp [nat.min_fac_eq, show 2 ∣ bit0 n, by simp [bit0_eq_two_mul n]] /-- A predicate representing partial progress in a proof of `min_fac`. -/ def min_fac_helper (n k : ℕ) : Prop := 0 < k ∧ bit1 k ≤ nat.min_fac (bit1 n) theorem min_fac_helper.n_pos {n k : ℕ} (h : min_fac_helper n k) : 0 < n := pos_iff_ne_zero.2 $ λ e, by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2 lemma min_fac_ne_bit0 {n k : ℕ} : nat.min_fac (bit1 n) ≠ bit0 k := by rw bit0_eq_two_mul; exact λ e, absurd ((nat.dvd_add_iff_right (by simp [bit0_eq_two_mul n])).2 (dvd_trans ⟨_, e⟩ (nat.min_fac_dvd _))) (by norm_num) lemma min_fac_helper_0 (n : ℕ) (h : 0 < n) : min_fac_helper n 1 := begin refine ⟨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symm⟩, refine @lt_of_le_of_ne ℕ _ _ _ (nat.min_fac_pos _) _, intro e, have := nat.min_fac_prime _, { rw ← e at this, exact nat.not_prime_one this }, { exact ne_of_gt (nat.bit1_lt h) } end lemma min_fac_helper_1 {n k k' : ℕ} (e : k + 1 = k') (np : nat.min_fac (bit1 n) ≠ bit1 k) (h : min_fac_helper n k) : min_fac_helper n k' := begin rw ← e, refine ⟨nat.succ_pos _, (lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _) min_fac_ne_bit0.symm : bit0 (k+1) < _)⟩, { rw add_right_comm, exact h.2 }, { rw add_right_comm, exact np.symm } end lemma min_fac_helper_2 (n k k' : ℕ) (e : k + 1 = k') (np : ¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, intro e₁, rw ← e₁ at np, exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos) end lemma min_fac_helper_3 (n k k' c : ℕ) (e : k + 1 = k') (nc : bit1 n % bit1 k = c) (c0 : 0 < c) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, refine mt _ (ne_of_gt c0), intro e₁, rw [← nc, ← nat.dvd_iff_mod_eq_zero, ← e₁], apply nat.min_fac_dvd end lemma min_fac_helper_4 (n k : ℕ) (hd : bit1 n % bit1 k = 0) (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k := by rw ← nat.dvd_iff_mod_eq_zero at hd; exact le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2 lemma min_fac_helper_5 (n k k' : ℕ) (e : bit1 k * bit1 k = k') (hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n := begin refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2 ⟨nat.bit1_lt h.n_pos, _⟩)).2, rw ← e at hd, intros m m2 hm md, have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm), rw nat.le_sqrt at this, exact not_le_of_lt hd this end /-- Given `e` a natural numeral and `d : nat` a factor of it, return `⊢ ¬ prime e`. -/ meta def prove_non_prime (e : expr) (n d₁ : ℕ) : tactic expr := do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, let d₂ := n / d₁, let e₂ := reflect d₂, (c, e', p) ← prove_mul_nat c e₁ e₂, guard (e' =ₐ e), (c, p₂) ← prove_lt_nat c `(1) e₂, return $ `(@nat.not_prime_mul').mk_app [e₁, e₂, e, p, p₁, p₂] /-- Given `a`,`a1 := bit1 a`, `n1` the value of `a1`, `b` and `p : min_fac_helper a b`, returns `(c, ⊢ min_fac a1 = c)`. -/ meta def prove_min_fac_aux (a a1 : expr) (n1 : ℕ) : instance_cache → expr → expr → tactic (instance_cache × expr × expr) | ic b p := do k ← b.to_nat, let k1 := bit1 k, let b1 := `(bit1:ℕ→ℕ).mk_app [b], if n1 < k1*k1 then do (ic, e', p₁) ← prove_mul_nat ic b1 b1, (ic, p₂) ← prove_lt_nat ic a1 e', return (ic, a1, `(min_fac_helper_5).mk_app [a, b, e', p₁, p₂, p]) else let d := k1.min_fac in if to_bool (d < k1) then do let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', p₂ ← prove_non_prime b1 k1 d, prove_min_fac_aux ic e' $ `(min_fac_helper_2).mk_app [a, b, e', p₁, p₂, p] else do let nc := n1 % k1, (ic, c, pc) ← prove_div_mod ic a1 b1 tt, if nc = 0 then return (ic, b1, `(min_fac_helper_4).mk_app [a, b, pc, p]) else do (ic, p₀) ← prove_pos ic c, let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', prove_min_fac_aux ic e' $ `(min_fac_helper_3).mk_app [a, b, e', c, p₁, pc, p₀, p] /-- Given `a` a natural numeral, returns `(b, ⊢ min_fac a = b)`. -/ meta def prove_min_fac (ic : instance_cache) (e : expr) : tactic (instance_cache × expr × expr) := match match_numeral e with | match_numeral_result.zero := return (ic, `(2:ℕ), `(nat.min_fac_zero)) | match_numeral_result.one := return (ic, `(1:ℕ), `(nat.min_fac_one)) | match_numeral_result.bit0 e := return (ic, `(2), `(min_fac_bit0).mk_app [e]) | match_numeral_result.bit1 e := do n ← e.to_nat, c ← mk_instance_cache `(nat), (c, p) ← prove_pos c e, let a1 := `(bit1:ℕ→ℕ).mk_app [e], prove_min_fac_aux e a1 (bit1 n) c `(1) (`(min_fac_helper_0).mk_app [e, p]) | _ := failed end /-- Evaluates the `prime` and `min_fac` functions. -/ @[norm_num] meta def eval_prime : expr → tactic (expr × expr) | `(nat.prime %%e) := do n ← e.to_nat, match n with | 0 := false_intro `(nat.not_prime_zero) | 1 := false_intro `(nat.not_prime_one) | _ := let d₁ := n.min_fac in if d₁ < n then prove_non_prime e n d₁ >>= false_intro else do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, (c, e₁, p) ← prove_min_fac c e, true_intro $ `(is_prime_helper).mk_app [e, p₁, p] end | `(nat.min_fac %%e) := do ic ← mk_instance_cache `(ℕ), prod.snd <$> prove_min_fac ic e | _ := failed end norm_num end tactic namespace nat theorem prime_three : prime 3 := by norm_num end nat
6f68f51068e84da8256166c8fcc59151947a562a
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/algebra/ordered_group.lean
c900ebea2245f330ddabb177f40ab006413c87e0
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,155
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Partially ordered additive groups, modeled on Isabelle's library. These classes can be refined if necessary. -/ import logic.eq data.unit data.sigma data.prod import algebra.binary algebra.group algebra.order open eq eq.ops -- note: ⁻¹ will be overloaded namespace algebra variable {A : Type} /- partially ordered monoids, such as the natural numbers -/ structure ordered_cancel_comm_monoid [class] (A : Type) extends add_comm_monoid A, add_left_cancel_semigroup A, add_right_cancel_semigroup A, order_pair A := (add_le_add_left : ∀a b, le a b → ∀c, le (add c a) (add c b)) (le_of_add_le_add_left : ∀a b c, le (add a b) (add a c) → le b c) (add_lt_add_left : ∀a b, lt a b → ∀c, lt (add c a) (add c b)) (lt_of_add_lt_add_left : ∀a b c, lt (add a b) (add a c) → lt b c) section variables [s : ordered_cancel_comm_monoid A] variables {a b c d e : A} include s theorem add_lt_add_left (H : a < b) (c : A) : c + a < c + b := !ordered_cancel_comm_monoid.add_lt_add_left H c theorem add_lt_add_right (H : a < b) (c : A) : a + c < b + c := begin rewrite [add.comm, {b + _}add.comm], exact (add_lt_add_left H c) end theorem add_le_add_left (H : a ≤ b) (c : A) : c + a ≤ c + b := !ordered_cancel_comm_monoid.add_le_add_left H c theorem add_le_add_right (H : a ≤ b) (c : A) : a + c ≤ b + c := (add.comm c a) ▸ (add.comm c b) ▸ (add_le_add_left H c) theorem add_le_add (Hab : a ≤ b) (Hcd : c ≤ d) : a + c ≤ b + d := le.trans (add_le_add_right Hab c) (add_le_add_left Hcd b) theorem le_add_of_nonneg_right (H : b ≥ 0) : a ≤ a + b := begin have H1 : a + b ≥ a + 0, from add_le_add_left H a, rewrite add_zero at H1, exact H1 end theorem le_add_of_nonneg_left (H : b ≥ 0) : a ≤ b + a := begin have H1 : 0 + a ≤ b + a, from add_le_add_right H a, rewrite zero_add at H1, exact H1 end theorem add_lt_add (Hab : a < b) (Hcd : c < d) : a + c < b + d := lt.trans (add_lt_add_right Hab c) (add_lt_add_left Hcd b) theorem add_lt_add_of_le_of_lt (Hab : a ≤ b) (Hcd : c < d) : a + c < b + d := lt_of_le_of_lt (add_le_add_right Hab c) (add_lt_add_left Hcd b) theorem add_lt_add_of_lt_of_le (Hab : a < b) (Hcd : c ≤ d) : a + c < b + d := lt_of_lt_of_le (add_lt_add_right Hab c) (add_le_add_left Hcd b) theorem lt_add_of_pos_right (H : b > 0) : a < a + b := !add_zero ▸ add_lt_add_left H a theorem lt_add_of_pos_left (H : b > 0) : a < b + a := !zero_add ▸ add_lt_add_right H a -- here we start using le_of_add_le_add_left. theorem le_of_add_le_add_left (H : a + b ≤ a + c) : b ≤ c := !ordered_cancel_comm_monoid.le_of_add_le_add_left H theorem le_of_add_le_add_right (H : a + b ≤ c + b) : a ≤ c := le_of_add_le_add_left (show b + a ≤ b + c, begin rewrite [add.comm, {b + _}add.comm], exact H end) theorem lt_of_add_lt_add_left (H : a + b < a + c) : b < c := !ordered_cancel_comm_monoid.lt_of_add_lt_add_left H theorem lt_of_add_lt_add_right (H : a + b < c + b) : a < c := lt_of_add_lt_add_left ((add.comm a b) ▸ (add.comm c b) ▸ H) theorem add_le_add_left_iff (a b c : A) : a + b ≤ a + c ↔ b ≤ c := iff.intro le_of_add_le_add_left (assume H, add_le_add_left H _) theorem add_le_add_right_iff (a b c : A) : a + b ≤ c + b ↔ a ≤ c := iff.intro le_of_add_le_add_right (assume H, add_le_add_right H _) theorem add_lt_add_left_iff (a b c : A) : a + b < a + c ↔ b < c := iff.intro lt_of_add_lt_add_left (assume H, add_lt_add_left H _) theorem add_lt_add_right_iff (a b c : A) : a + b < c + b ↔ a < c := iff.intro lt_of_add_lt_add_right (assume H, add_lt_add_right H _) -- here we start using properties of zero. theorem add_nonneg (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a + b := !zero_add ▸ (add_le_add Ha Hb) theorem add_pos (Ha : 0 < a) (Hb : 0 < b) : 0 < a + b := !zero_add ▸ (add_lt_add Ha Hb) theorem add_pos_of_pos_of_nonneg (Ha : 0 < a) (Hb : 0 ≤ b) : 0 < a + b := !zero_add ▸ (add_lt_add_of_lt_of_le Ha Hb) theorem add_pos_of_nonneg_of_pos (Ha : 0 ≤ a) (Hb : 0 < b) : 0 < a + b := !zero_add ▸ (add_lt_add_of_le_of_lt Ha Hb) theorem add_nonpos (Ha : a ≤ 0) (Hb : b ≤ 0) : a + b ≤ 0 := !zero_add ▸ (add_le_add Ha Hb) theorem add_neg (Ha : a < 0) (Hb : b < 0) : a + b < 0 := !zero_add ▸ (add_lt_add Ha Hb) theorem add_neg_of_neg_of_nonpos (Ha : a < 0) (Hb : b ≤ 0) : a + b < 0 := !zero_add ▸ (add_lt_add_of_lt_of_le Ha Hb) theorem add_neg_of_nonpos_of_neg (Ha : a ≤ 0) (Hb : b < 0) : a + b < 0 := !zero_add ▸ (add_lt_add_of_le_of_lt Ha Hb) -- TODO: add nonpos version (will be easier with simplifier) theorem add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg (Ha : 0 ≤ a) (Hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume Hab : a + b = 0, have Ha' : a ≤ 0, from calc a = a + 0 : by rewrite add_zero ... ≤ a + b : add_le_add_left Hb ... = 0 : Hab, have Haz : a = 0, from le.antisymm Ha' Ha, have Hb' : b ≤ 0, from calc b = 0 + b : by rewrite zero_add ... ≤ a + b : by exact add_le_add_right Ha _ ... = 0 : Hab, have Hbz : b = 0, from le.antisymm Hb' Hb, and.intro Haz Hbz) (assume Hab : a = 0 ∧ b = 0, obtain Ha' Hb', from Hab, by rewrite [Ha', Hb', add_zero]) theorem le_add_of_nonneg_of_le (Ha : 0 ≤ a) (Hbc : b ≤ c) : b ≤ a + c := !zero_add ▸ add_le_add Ha Hbc theorem le_add_of_le_of_nonneg (Hbc : b ≤ c) (Ha : 0 ≤ a) : b ≤ c + a := !add_zero ▸ add_le_add Hbc Ha theorem lt_add_of_pos_of_le (Ha : 0 < a) (Hbc : b ≤ c) : b < a + c := !zero_add ▸ add_lt_add_of_lt_of_le Ha Hbc theorem lt_add_of_le_of_pos (Hbc : b ≤ c) (Ha : 0 < a) : b < c + a := !add_zero ▸ add_lt_add_of_le_of_lt Hbc Ha theorem add_le_of_nonpos_of_le (Ha : a ≤ 0) (Hbc : b ≤ c) : a + b ≤ c := !zero_add ▸ add_le_add Ha Hbc theorem add_le_of_le_of_nonpos (Hbc : b ≤ c) (Ha : a ≤ 0) : b + a ≤ c := !add_zero ▸ add_le_add Hbc Ha theorem add_lt_of_neg_of_le (Ha : a < 0) (Hbc : b ≤ c) : a + b < c := !zero_add ▸ add_lt_add_of_lt_of_le Ha Hbc theorem add_lt_of_le_of_neg (Hbc : b ≤ c) (Ha : a < 0) : b + a < c := !add_zero ▸ add_lt_add_of_le_of_lt Hbc Ha theorem lt_add_of_nonneg_of_lt (Ha : 0 ≤ a) (Hbc : b < c) : b < a + c := !zero_add ▸ add_lt_add_of_le_of_lt Ha Hbc theorem lt_add_of_lt_of_nonneg (Hbc : b < c) (Ha : 0 ≤ a) : b < c + a := !add_zero ▸ add_lt_add_of_lt_of_le Hbc Ha theorem lt_add_of_pos_of_lt (Ha : 0 < a) (Hbc : b < c) : b < a + c := !zero_add ▸ add_lt_add Ha Hbc theorem lt_add_of_lt_of_pos (Hbc : b < c) (Ha : 0 < a) : b < c + a := !add_zero ▸ add_lt_add Hbc Ha theorem add_lt_of_nonpos_of_lt (Ha : a ≤ 0) (Hbc : b < c) : a + b < c := !zero_add ▸ add_lt_add_of_le_of_lt Ha Hbc theorem add_lt_of_lt_of_nonpos (Hbc : b < c) (Ha : a ≤ 0) : b + a < c := !add_zero ▸ add_lt_add_of_lt_of_le Hbc Ha theorem add_lt_of_neg_of_lt (Ha : a < 0) (Hbc : b < c) : a + b < c := !zero_add ▸ add_lt_add Ha Hbc theorem add_lt_of_lt_of_neg (Hbc : b < c) (Ha : a < 0) : b + a < c := !add_zero ▸ add_lt_add Hbc Ha end /- partially ordered groups -/ structure ordered_comm_group [class] (A : Type) extends add_comm_group A, order_pair A := (add_le_add_left : ∀a b, le a b → ∀c, le (add c a) (add c b)) (add_lt_add_left : ∀a b, lt a b → ∀ c, lt (add c a) (add c b)) theorem ordered_comm_group.le_of_add_le_add_left [s : ordered_comm_group A] {a b c : A} (H : a + b ≤ a + c) : b ≤ c := assert H' : -a + (a + b) ≤ -a + (a + c), from ordered_comm_group.add_le_add_left _ _ H _, by rewrite *neg_add_cancel_left at H'; exact H' theorem ordered_comm_group.lt_of_add_lt_add_left [s : ordered_comm_group A] {a b c : A} (H : a + b < a + c) : b < c := assert H' : -a + (a + b) < -a + (a + c), from ordered_comm_group.add_lt_add_left _ _ H _, by rewrite *neg_add_cancel_left at H'; exact H' set_option pp.all true definition ordered_comm_group.to_ordered_cancel_comm_monoid [trans_instance] [reducible] [s : ordered_comm_group A] : ordered_cancel_comm_monoid A := ⦃ ordered_cancel_comm_monoid, s, add_left_cancel := @add.left_cancel A _, add_right_cancel := @add.right_cancel A _, le_of_add_le_add_left := @ordered_comm_group.le_of_add_le_add_left A _, lt_of_add_lt_add_left := @ordered_comm_group.lt_of_add_lt_add_left A _⦄ section variables [s : ordered_comm_group A] (a b c d e : A) include s theorem neg_le_neg {a b : A} (H : a ≤ b) : -b ≤ -a := have H1 : 0 ≤ -a + b, from !add.left_inv ▸ !(add_le_add_left H), !add_neg_cancel_right ▸ !zero_add ▸ add_le_add_right H1 (-b) theorem le_of_neg_le_neg {a b : A} (H : -b ≤ -a) : a ≤ b := neg_neg a ▸ neg_neg b ▸ neg_le_neg H theorem neg_le_neg_iff_le : -a ≤ -b ↔ b ≤ a := iff.intro le_of_neg_le_neg neg_le_neg theorem nonneg_of_neg_nonpos {a : A} (H : -a ≤ 0) : 0 ≤ a := le_of_neg_le_neg (neg_zero⁻¹ ▸ H) theorem neg_nonpos_of_nonneg {a : A} (H : 0 ≤ a) : -a ≤ 0 := neg_zero ▸ neg_le_neg H theorem neg_nonpos_iff_nonneg : -a ≤ 0 ↔ 0 ≤ a := iff.intro nonneg_of_neg_nonpos neg_nonpos_of_nonneg theorem nonpos_of_neg_nonneg {a : A} (H : 0 ≤ -a) : a ≤ 0 := le_of_neg_le_neg (neg_zero⁻¹ ▸ H) theorem neg_nonneg_of_nonpos {a : A} (H : a ≤ 0) : 0 ≤ -a := neg_zero ▸ neg_le_neg H theorem neg_nonneg_iff_nonpos : 0 ≤ -a ↔ a ≤ 0 := iff.intro nonpos_of_neg_nonneg neg_nonneg_of_nonpos theorem neg_lt_neg {a b : A} (H : a < b) : -b < -a := have H1 : 0 < -a + b, from !add.left_inv ▸ !(add_lt_add_left H), !add_neg_cancel_right ▸ !zero_add ▸ add_lt_add_right H1 (-b) theorem lt_of_neg_lt_neg {a b : A} (H : -b < -a) : a < b := neg_neg a ▸ neg_neg b ▸ neg_lt_neg H theorem neg_lt_neg_iff_lt : -a < -b ↔ b < a := iff.intro lt_of_neg_lt_neg neg_lt_neg theorem pos_of_neg_neg {a : A} (H : -a < 0) : 0 < a := lt_of_neg_lt_neg (neg_zero⁻¹ ▸ H) theorem neg_neg_of_pos {a : A} (H : 0 < a) : -a < 0 := neg_zero ▸ neg_lt_neg H theorem neg_neg_iff_pos : -a < 0 ↔ 0 < a := iff.intro pos_of_neg_neg neg_neg_of_pos theorem neg_of_neg_pos {a : A} (H : 0 < -a) : a < 0 := lt_of_neg_lt_neg (neg_zero⁻¹ ▸ H) theorem neg_pos_of_neg {a : A} (H : a < 0) : 0 < -a := neg_zero ▸ neg_lt_neg H theorem neg_pos_iff_neg : 0 < -a ↔ a < 0 := iff.intro neg_of_neg_pos neg_pos_of_neg theorem le_neg_iff_le_neg : a ≤ -b ↔ b ≤ -a := !neg_neg ▸ !neg_le_neg_iff_le theorem le_neg_of_le_neg {a b : A} : a ≤ -b → b ≤ -a := iff.mp !le_neg_iff_le_neg theorem neg_le_iff_neg_le : -a ≤ b ↔ -b ≤ a := !neg_neg ▸ !neg_le_neg_iff_le theorem neg_le_of_neg_le {a b : A} : -a ≤ b → -b ≤ a := iff.mp !neg_le_iff_neg_le theorem lt_neg_iff_lt_neg : a < -b ↔ b < -a := !neg_neg ▸ !neg_lt_neg_iff_lt theorem lt_neg_of_lt_neg {a b : A} : a < -b → b < -a := iff.mp !lt_neg_iff_lt_neg theorem neg_lt_iff_neg_lt : -a < b ↔ -b < a := !neg_neg ▸ !neg_lt_neg_iff_lt theorem neg_lt_of_neg_lt {a b : A} : -a < b → -b < a := iff.mp !neg_lt_iff_neg_lt theorem sub_nonneg_iff_le : 0 ≤ a - b ↔ b ≤ a := !sub_self ▸ !add_le_add_right_iff theorem sub_nonneg_of_le {a b : A} : b ≤ a → 0 ≤ a - b := iff.mpr !sub_nonneg_iff_le theorem le_of_sub_nonneg {a b : A} : 0 ≤ a - b → b ≤ a := iff.mp !sub_nonneg_iff_le theorem sub_nonpos_iff_le : a - b ≤ 0 ↔ a ≤ b := !sub_self ▸ !add_le_add_right_iff theorem sub_nonpos_of_le {a b : A} : a ≤ b → a - b ≤ 0 := iff.mpr !sub_nonpos_iff_le theorem le_of_sub_nonpos {a b : A} : a - b ≤ 0 → a ≤ b := iff.mp !sub_nonpos_iff_le theorem sub_pos_iff_lt : 0 < a - b ↔ b < a := !sub_self ▸ !add_lt_add_right_iff theorem sub_pos_of_lt {a b : A} : b < a → 0 < a - b := iff.mpr !sub_pos_iff_lt theorem lt_of_sub_pos {a b : A} : 0 < a - b → b < a := iff.mp !sub_pos_iff_lt theorem sub_neg_iff_lt : a - b < 0 ↔ a < b := !sub_self ▸ !add_lt_add_right_iff theorem sub_neg_of_lt {a b : A} : a < b → a - b < 0 := iff.mpr !sub_neg_iff_lt theorem lt_of_sub_neg {a b : A} : a - b < 0 → a < b := iff.mp !sub_neg_iff_lt theorem add_le_iff_le_neg_add : a + b ≤ c ↔ b ≤ -a + c := have H: a + b ≤ c ↔ -a + (a + b) ≤ -a + c, from iff.symm (!add_le_add_left_iff), !neg_add_cancel_left ▸ H theorem add_le_of_le_neg_add {a b c : A} : b ≤ -a + c → a + b ≤ c := iff.mpr !add_le_iff_le_neg_add theorem le_neg_add_of_add_le {a b c : A} : a + b ≤ c → b ≤ -a + c := iff.mp !add_le_iff_le_neg_add theorem add_le_iff_le_sub_left : a + b ≤ c ↔ b ≤ c - a := by rewrite [sub_eq_add_neg, {c+_}add.comm]; apply add_le_iff_le_neg_add theorem add_le_of_le_sub_left {a b c : A} : b ≤ c - a → a + b ≤ c := iff.mpr !add_le_iff_le_sub_left theorem le_sub_left_of_add_le {a b c : A} : a + b ≤ c → b ≤ c - a := iff.mp !add_le_iff_le_sub_left theorem add_le_iff_le_sub_right : a + b ≤ c ↔ a ≤ c - b := have H: a + b ≤ c ↔ a + b - b ≤ c - b, from iff.symm (!add_le_add_right_iff), !add_neg_cancel_right ▸ H theorem add_le_of_le_sub_right {a b c : A} : a ≤ c - b → a + b ≤ c := iff.mpr !add_le_iff_le_sub_right theorem le_sub_right_of_add_le {a b c : A} : a + b ≤ c → a ≤ c - b := iff.mp !add_le_iff_le_sub_right theorem le_add_iff_neg_add_le : a ≤ b + c ↔ -b + a ≤ c := assert H: a ≤ b + c ↔ -b + a ≤ -b + (b + c), from iff.symm (!add_le_add_left_iff), by rewrite neg_add_cancel_left at H; exact H theorem le_add_of_neg_add_le {a b c : A} : -b + a ≤ c → a ≤ b + c := iff.mpr !le_add_iff_neg_add_le theorem neg_add_le_of_le_add {a b c : A} : a ≤ b + c → -b + a ≤ c := iff.mp !le_add_iff_neg_add_le theorem le_add_iff_sub_left_le : a ≤ b + c ↔ a - b ≤ c := by rewrite [sub_eq_add_neg, {a+_}add.comm]; apply le_add_iff_neg_add_le theorem le_add_of_sub_left_le {a b c : A} : a - b ≤ c → a ≤ b + c := iff.mpr !le_add_iff_sub_left_le theorem sub_left_le_of_le_add {a b c : A} : a ≤ b + c → a - b ≤ c := iff.mp !le_add_iff_sub_left_le theorem le_add_iff_sub_right_le : a ≤ b + c ↔ a - c ≤ b := assert H: a ≤ b + c ↔ a - c ≤ b + c - c, from iff.symm (!add_le_add_right_iff), by rewrite [sub_eq_add_neg (b+c) c at H, add_neg_cancel_right at H]; exact H theorem le_add_of_sub_right_le {a b c : A} : a - c ≤ b → a ≤ b + c := iff.mpr !le_add_iff_sub_right_le theorem sub_right_le_of_le_add {a b c : A} : a ≤ b + c → a - c ≤ b := iff.mp !le_add_iff_sub_right_le theorem le_add_iff_neg_add_le_left : a ≤ b + c ↔ -b + a ≤ c := assert H: a ≤ b + c ↔ -b + a ≤ -b + (b + c), from iff.symm (!add_le_add_left_iff), by rewrite neg_add_cancel_left at H; exact H theorem le_add_of_neg_add_le_left {a b c : A} : -b + a ≤ c → a ≤ b + c := iff.mpr !le_add_iff_neg_add_le_left theorem neg_add_le_left_of_le_add {a b c : A} : a ≤ b + c → -b + a ≤ c := iff.mp !le_add_iff_neg_add_le_left theorem le_add_iff_neg_add_le_right : a ≤ b + c ↔ -c + a ≤ b := by rewrite add.comm; apply le_add_iff_neg_add_le_left theorem le_add_of_neg_add_le_right {a b c : A} : -c + a ≤ b → a ≤ b + c := iff.mpr !le_add_iff_neg_add_le_right theorem neg_add_le_right_of_le_add {a b c : A} : a ≤ b + c → -c + a ≤ b := iff.mp !le_add_iff_neg_add_le_right theorem le_add_iff_neg_le_sub_left : c ≤ a + b ↔ -a ≤ b - c := assert H : c ≤ a + b ↔ -a + c ≤ b, from !le_add_iff_neg_add_le, assert H' : -a + c ≤ b ↔ -a ≤ b - c, from !add_le_iff_le_sub_right, iff.trans H H' theorem le_add_of_neg_le_sub_left {a b c : A} : -a ≤ b - c → c ≤ a + b := iff.mpr !le_add_iff_neg_le_sub_left theorem neg_le_sub_left_of_le_add {a b c : A} : c ≤ a + b → -a ≤ b - c := iff.mp !le_add_iff_neg_le_sub_left theorem le_add_iff_neg_le_sub_right : c ≤ a + b ↔ -b ≤ a - c := by rewrite add.comm; apply le_add_iff_neg_le_sub_left theorem le_add_of_neg_le_sub_right {a b c : A} : -b ≤ a - c → c ≤ a + b := iff.mpr !le_add_iff_neg_le_sub_right theorem neg_le_sub_right_of_le_add {a b c : A} : c ≤ a + b → -b ≤ a - c := iff.mp !le_add_iff_neg_le_sub_right theorem add_lt_iff_lt_neg_add_left : a + b < c ↔ b < -a + c := assert H: a + b < c ↔ -a + (a + b) < -a + c, from iff.symm (!add_lt_add_left_iff), begin rewrite neg_add_cancel_left at H, exact H end theorem add_lt_of_lt_neg_add_left {a b c : A} : b < -a + c → a + b < c := iff.mpr !add_lt_iff_lt_neg_add_left theorem lt_neg_add_left_of_add_lt {a b c : A} : a + b < c → b < -a + c := iff.mp !add_lt_iff_lt_neg_add_left theorem add_lt_iff_lt_neg_add_right : a + b < c ↔ a < -b + c := by rewrite add.comm; apply add_lt_iff_lt_neg_add_left theorem add_lt_of_lt_neg_add_right {a b c : A} : a < -b + c → a + b < c := iff.mpr !add_lt_iff_lt_neg_add_right theorem lt_neg_add_right_of_add_lt {a b c : A} : a + b < c → a < -b + c := iff.mp !add_lt_iff_lt_neg_add_right theorem add_lt_iff_lt_sub_left : a + b < c ↔ b < c - a := begin rewrite [sub_eq_add_neg, {c+_}add.comm], apply add_lt_iff_lt_neg_add_left end theorem add_lt_of_lt_sub_left {a b c : A} : b < c - a → a + b < c := iff.mpr !add_lt_iff_lt_sub_left theorem lt_sub_left_of_add_lt {a b c : A} : a + b < c → b < c - a := iff.mp !add_lt_iff_lt_sub_left theorem add_lt_iff_lt_sub_right : a + b < c ↔ a < c - b := assert H: a + b < c ↔ a + b - b < c - b, from iff.symm (!add_lt_add_right_iff), by rewrite [sub_eq_add_neg at H, add_neg_cancel_right at H]; exact H theorem add_lt_of_lt_sub_right {a b c : A} : a < c - b → a + b < c := iff.mpr !add_lt_iff_lt_sub_right theorem lt_sub_right_of_add_lt {a b c : A} : a + b < c → a < c - b := iff.mp !add_lt_iff_lt_sub_right theorem lt_add_iff_neg_add_lt_left : a < b + c ↔ -b + a < c := assert H: a < b + c ↔ -b + a < -b + (b + c), from iff.symm (!add_lt_add_left_iff), by rewrite neg_add_cancel_left at H; exact H theorem lt_add_of_neg_add_lt_left {a b c : A} : -b + a < c → a < b + c := iff.mpr !lt_add_iff_neg_add_lt_left theorem neg_add_lt_left_of_lt_add {a b c : A} : a < b + c → -b + a < c := iff.mp !lt_add_iff_neg_add_lt_left theorem lt_add_iff_neg_add_lt_right : a < b + c ↔ -c + a < b := by rewrite add.comm; apply lt_add_iff_neg_add_lt_left theorem lt_add_of_neg_add_lt_right {a b c : A} : -c + a < b → a < b + c := iff.mpr !lt_add_iff_neg_add_lt_right theorem neg_add_lt_right_of_lt_add {a b c : A} : a < b + c → -c + a < b := iff.mp !lt_add_iff_neg_add_lt_right theorem lt_add_iff_sub_lt_left : a < b + c ↔ a - b < c := by rewrite [sub_eq_add_neg, {a + _}add.comm]; apply lt_add_iff_neg_add_lt_left theorem lt_add_of_sub_lt_left {a b c : A} : a - b < c → a < b + c := iff.mpr !lt_add_iff_sub_lt_left theorem sub_lt_left_of_lt_add {a b c : A} : a < b + c → a - b < c := iff.mp !lt_add_iff_sub_lt_left theorem lt_add_iff_sub_lt_right : a < b + c ↔ a - c < b := by rewrite add.comm; apply lt_add_iff_sub_lt_left theorem lt_add_of_sub_lt_right {a b c : A} : a - c < b → a < b + c := iff.mpr !lt_add_iff_sub_lt_right theorem sub_lt_right_of_lt_add {a b c : A} : a < b + c → a - c < b := iff.mp !lt_add_iff_sub_lt_right theorem sub_lt_of_sub_lt {a b c : A} : a - b < c → a - c < b := begin intro H, apply sub_lt_left_of_lt_add, apply lt_add_of_sub_lt_right H end theorem sub_le_of_sub_le {a b c : A} : a - b ≤ c → a - c ≤ b := begin intro H, apply sub_left_le_of_le_add, apply le_add_of_sub_right_le H end -- TODO: the Isabelle library has varations on a + b ≤ b ↔ a ≤ 0 theorem le_iff_le_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a ≤ b ↔ c ≤ d := calc a ≤ b ↔ a - b ≤ 0 : iff.symm (sub_nonpos_iff_le a b) ... = (c - d ≤ 0) : by rewrite H ... ↔ c ≤ d : sub_nonpos_iff_le c d theorem lt_iff_lt_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a < b ↔ c < d := calc a < b ↔ a - b < 0 : iff.symm (sub_neg_iff_lt a b) ... = (c - d < 0) : by rewrite H ... ↔ c < d : sub_neg_iff_lt c d theorem sub_le_sub_left {a b : A} (H : a ≤ b) (c : A) : c - b ≤ c - a := add_le_add_left (neg_le_neg H) c theorem sub_le_sub_right {a b : A} (H : a ≤ b) (c : A) : a - c ≤ b - c := add_le_add_right H (-c) theorem sub_le_sub {a b c d : A} (Hab : a ≤ b) (Hcd : c ≤ d) : a - d ≤ b - c := add_le_add Hab (neg_le_neg Hcd) theorem sub_lt_sub_left {a b : A} (H : a < b) (c : A) : c - b < c - a := add_lt_add_left (neg_lt_neg H) c theorem sub_lt_sub_right {a b : A} (H : a < b) (c : A) : a - c < b - c := add_lt_add_right H (-c) theorem sub_lt_sub {a b c d : A} (Hab : a < b) (Hcd : c < d) : a - d < b - c := add_lt_add Hab (neg_lt_neg Hcd) theorem sub_lt_sub_of_le_of_lt {a b c d : A} (Hab : a ≤ b) (Hcd : c < d) : a - d < b - c := add_lt_add_of_le_of_lt Hab (neg_lt_neg Hcd) theorem sub_lt_sub_of_lt_of_le {a b c d : A} (Hab : a < b) (Hcd : c ≤ d) : a - d < b - c := add_lt_add_of_lt_of_le Hab (neg_le_neg Hcd) theorem sub_le_self (a : A) {b : A} (H : b ≥ 0) : a - b ≤ a := calc a - b = a + -b : rfl ... ≤ a + 0 : add_le_add_left (neg_nonpos_of_nonneg H) ... = a : by rewrite add_zero theorem sub_lt_self (a : A) {b : A} (H : b > 0) : a - b < a := calc a - b = a + -b : rfl ... < a + 0 : add_lt_add_left (neg_neg_of_pos H) ... = a : by rewrite add_zero theorem add_le_add_three {a b c d e f : A} (H1 : a ≤ d) (H2 : b ≤ e) (H3 : c ≤ f) : a + b + c ≤ d + e + f := begin apply le.trans, apply add_le_add, apply add_le_add, repeat assumption, apply le.refl end theorem sub_le_of_nonneg {b : A} (H : b ≥ 0) : a - b ≤ a := add_le_of_le_of_nonpos (le.refl a) (neg_nonpos_of_nonneg H) theorem sub_lt_of_pos {b : A} (H : b > 0) : a - b < a := add_lt_of_le_of_neg (le.refl a) (neg_neg_of_pos H) theorem neg_add_neg_le_neg_of_pos {a : A} (H : a > 0) : -a + -a ≤ -a := !neg_add ▸ neg_le_neg (le_add_of_nonneg_left (le_of_lt H)) end /- linear ordered group with decidable order -/ structure decidable_linear_ordered_comm_group [class] (A : Type) extends add_comm_group A, decidable_linear_order A := (add_le_add_left : ∀ a b, le a b → ∀ c, le (add c a) (add c b)) (add_lt_add_left : ∀ a b, lt a b → ∀ c, lt (add c a) (add c b)) definition decidable_linear_ordered_comm_group.to_ordered_comm_group [trans_instance] [reducible] (A : Type) [s : decidable_linear_ordered_comm_group A] : ordered_comm_group A := ⦃ ordered_comm_group, s, le_of_lt := @le_of_lt A _, lt_of_le_of_lt := @lt_of_le_of_lt A _, lt_of_lt_of_le := @lt_of_lt_of_le A _ ⦄ section variables [s : decidable_linear_ordered_comm_group A] variables {a b c d e : A} include s /- these can be generalized to a lattice ordered group -/ theorem min_add_add_left : min (a + b) (a + c) = a + min b c := eq.symm (eq_min (show a + min b c ≤ a + b, from add_le_add_left !min_le_left _) (show a + min b c ≤ a + c, from add_le_add_left !min_le_right _) (take d, assume H₁ : d ≤ a + b, assume H₂ : d ≤ a + c, have H : d - a ≤ min b c, from le_min (iff.mp !le_add_iff_sub_left_le H₁) (iff.mp !le_add_iff_sub_left_le H₂), show d ≤ a + min b c, from iff.mpr !le_add_iff_sub_left_le H)) theorem min_add_add_right : min (a + c) (b + c) = min a b + c := by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply min_add_add_left theorem max_add_add_left : max (a + b) (a + c) = a + max b c := eq.symm (eq_max (add_le_add_left !le_max_left _) (add_le_add_left !le_max_right _) (λ d H₁ H₂, have H : max b c ≤ d - a, from max_le (iff.mp !add_le_iff_le_sub_left H₁) (iff.mp !add_le_iff_le_sub_left H₂), show a + max b c ≤ d, from iff.mpr !add_le_iff_le_sub_left H)) theorem max_add_add_right : max (a + c) (b + c) = max a b + c := by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply max_add_add_left theorem max_neg_neg : max (-a) (-b) = - min a b := eq.symm (eq_max (show -a ≤ -(min a b), from neg_le_neg !min_le_left) (show -b ≤ -(min a b), from neg_le_neg !min_le_right) (take d, assume H₁ : -a ≤ d, assume H₂ : -b ≤ d, have H : -d ≤ min a b, from le_min (!iff.mp !neg_le_iff_neg_le H₁) (!iff.mp !neg_le_iff_neg_le H₂), show -(min a b) ≤ d, from !iff.mp !neg_le_iff_neg_le H)) theorem min_eq_neg_max_neg_neg : min a b = - max (-a) (-b) := by rewrite [max_neg_neg, neg_neg] theorem min_neg_neg : min (-a) (-b) = - max a b := by rewrite [min_eq_neg_max_neg_neg, *neg_neg] theorem max_eq_neg_min_neg_neg : max a b = - min (-a) (-b) := by rewrite [min_neg_neg, neg_neg] /- absolute value -/ variables {a b c} definition abs (a : A) : A := max a (-a) theorem abs_of_nonneg (H : a ≥ 0) : abs a = a := have H' : -a ≤ a, from le.trans (neg_nonpos_of_nonneg H) H, max_eq_left H' theorem abs_of_pos (H : a > 0) : abs a = a := abs_of_nonneg (le_of_lt H) theorem abs_of_nonpos (H : a ≤ 0) : abs a = -a := have H' : a ≤ -a, from le.trans H (neg_nonneg_of_nonpos H), max_eq_right H' theorem abs_of_neg (H : a < 0) : abs a = -a := abs_of_nonpos (le_of_lt H) theorem abs_zero : abs 0 = (0:A) := abs_of_nonneg (le.refl _) theorem abs_neg (a : A) : abs (-a) = abs a := by rewrite [↑abs, max.comm, neg_neg] theorem abs_pos_of_pos (H : a > 0) : abs a > 0 := by rewrite (abs_of_pos H); exact H theorem abs_pos_of_neg (H : a < 0) : abs a > 0 := !abs_neg ▸ abs_pos_of_pos (neg_pos_of_neg H) theorem abs_sub (a b : A) : abs (a - b) = abs (b - a) := by rewrite [-neg_sub, abs_neg] theorem ne_zero_of_abs_ne_zero {a : A} (H : abs a ≠ 0) : a ≠ 0 := assume Ha, H (Ha⁻¹ ▸ abs_zero) /- these assume a linear order -/ theorem eq_zero_of_neg_eq (H : -a = a) : a = 0 := lt.by_cases (assume H1 : a < 0, have H2: a > 0, from H ▸ neg_pos_of_neg H1, absurd H1 (lt.asymm H2)) (assume H1 : a = 0, H1) (assume H1 : a > 0, have H2: a < 0, from H ▸ neg_neg_of_pos H1, absurd H1 (lt.asymm H2)) theorem abs_nonneg (a : A) : abs a ≥ 0 := or.elim (le.total 0 a) (assume H : 0 ≤ a, by rewrite (abs_of_nonneg H); exact H) (assume H : a ≤ 0, calc 0 ≤ -a : neg_nonneg_of_nonpos H ... = abs a : abs_of_nonpos H) theorem abs_abs (a : A) : abs (abs a) = abs a := abs_of_nonneg !abs_nonneg theorem le_abs_self (a : A) : a ≤ abs a := or.elim (le.total 0 a) (assume H : 0 ≤ a, abs_of_nonneg H ▸ !le.refl) (assume H : a ≤ 0, le.trans H !abs_nonneg) theorem neg_le_abs_self (a : A) : -a ≤ abs a := !abs_neg ▸ !le_abs_self theorem eq_zero_of_abs_eq_zero (H : abs a = 0) : a = 0 := have H1 : a ≤ 0, from H ▸ le_abs_self a, have H2 : -a ≤ 0, from H ▸ abs_neg a ▸ le_abs_self (-a), le.antisymm H1 (nonneg_of_neg_nonpos H2) theorem abs_eq_zero_iff_eq_zero (a : A) : abs a = 0 ↔ a = 0 := iff.intro eq_zero_of_abs_eq_zero (assume H, congr_arg abs H ⬝ !abs_zero) theorem eq_of_abs_sub_eq_zero {a b : A} (H : abs (a - b) = 0) : a = b := have a - b = 0, from eq_zero_of_abs_eq_zero H, show a = b, from eq_of_sub_eq_zero this theorem abs_pos_of_ne_zero (H : a ≠ 0) : abs a > 0 := or.elim (lt_or_gt_of_ne H) abs_pos_of_neg abs_pos_of_pos theorem abs.by_cases {P : A → Prop} {a : A} (H1 : P a) (H2 : P (-a)) : P (abs a) := or.elim (le.total 0 a) (assume H : 0 ≤ a, (abs_of_nonneg H)⁻¹ ▸ H1) (assume H : a ≤ 0, (abs_of_nonpos H)⁻¹ ▸ H2) theorem abs_le_of_le_of_neg_le (H1 : a ≤ b) (H2 : -a ≤ b) : abs a ≤ b := abs.by_cases H1 H2 theorem abs_lt_of_lt_of_neg_lt (H1 : a < b) (H2 : -a < b) : abs a < b := abs.by_cases H1 H2 -- the triangle inequality section private lemma aux1 {a b : A} (H1 : a + b ≥ 0) (H2 : a ≥ 0) : abs (a + b) ≤ abs a + abs b := decidable.by_cases (assume H3 : b ≥ 0, calc abs (a + b) ≤ abs (a + b) : le.refl ... = a + b : by rewrite (abs_of_nonneg H1) ... = abs a + b : by rewrite (abs_of_nonneg H2) ... = abs a + abs b : by rewrite (abs_of_nonneg H3)) (assume H3 : ¬ b ≥ 0, assert H4 : b ≤ 0, from le_of_lt (lt_of_not_ge H3), calc abs (a + b) = a + b : by rewrite (abs_of_nonneg H1) ... = abs a + b : by rewrite (abs_of_nonneg H2) ... ≤ abs a + 0 : add_le_add_left H4 ... ≤ abs a + -b : add_le_add_left (neg_nonneg_of_nonpos H4) ... = abs a + abs b : by rewrite (abs_of_nonpos H4)) private lemma aux2 {a b : A} (H1 : a + b ≥ 0) : abs (a + b) ≤ abs a + abs b := or.elim (le.total b 0) (assume H2 : b ≤ 0, have H3 : ¬ a < 0, from assume H4 : a < 0, have H5 : a + b < 0, from !add_zero ▸ add_lt_add_of_lt_of_le H4 H2, not_lt_of_ge H1 H5, aux1 H1 (le_of_not_gt H3)) (assume H2 : 0 ≤ b, begin have H3 : abs (b + a) ≤ abs b + abs a, begin rewrite add.comm at H1, exact aux1 H1 H2 end, rewrite [add.comm, {abs a + _}add.comm], exact H3 end) theorem abs_add_le_abs_add_abs (a b : A) : abs (a + b) ≤ abs a + abs b := or.elim (le.total 0 (a + b)) (assume H2 : 0 ≤ a + b, aux2 H2) (assume H2 : a + b ≤ 0, assert H3 : -a + -b = -(a + b), by rewrite neg_add, assert H4 : -(a + b) ≥ 0, from iff.mpr (neg_nonneg_iff_nonpos (a+b)) H2, have H5 : -a + -b ≥ 0, begin rewrite -H3 at H4, exact H4 end, calc abs (a + b) = abs (-a + -b) : by rewrite [-abs_neg, neg_add] ... ≤ abs (-a) + abs (-b) : aux2 H5 ... = abs a + abs b : by rewrite *abs_neg) theorem abs_sub_abs_le_abs_sub (a b : A) : abs a - abs b ≤ abs (a - b) := have H1 : abs a - abs b + abs b ≤ abs (a - b) + abs b, from calc abs a - abs b + abs b = abs a : by rewrite sub_add_cancel ... = abs (a - b + b) : by rewrite sub_add_cancel ... ≤ abs (a - b) + abs b : abs_add_le_abs_add_abs, algebra.le_of_add_le_add_right H1 theorem abs_sub_le (a b c : A) : abs (a - c) ≤ abs (a - b) + abs (b - c) := calc abs (a - c) = abs (a - b + (b - c)) : by rewrite [*sub_eq_add_neg, add.assoc, neg_add_cancel_left] ... ≤ abs (a - b) + abs (b - c) : abs_add_le_abs_add_abs theorem abs_add_three (a b c : A) : abs (a + b + c) ≤ abs a + abs b + abs c := begin apply le.trans, apply abs_add_le_abs_add_abs, apply le.trans, apply add_le_add_right, apply abs_add_le_abs_add_abs, apply le.refl end theorem dist_bdd_within_interval {a b lb ub : A} (H : lb < ub) (Hal : lb ≤ a) (Hau : a ≤ ub) (Hbl : lb ≤ b) (Hbu : b ≤ ub) : abs (a - b) ≤ ub - lb := begin cases (decidable.em (b ≤ a)) with [Hba, Hba], rewrite (abs_of_nonneg (iff.mpr !sub_nonneg_iff_le Hba)), apply sub_le_sub, apply Hau, apply Hbl, rewrite [abs_of_neg (iff.mpr !sub_neg_iff_lt (lt_of_not_ge Hba)), neg_sub], apply sub_le_sub, apply Hbu, apply Hal end end end end algebra
95b5e07936b3de30ddd9df2b163d023f51ffeb86
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/ring_theory/algebra_tower.lean
0e69fa135c063ddce21f8c24e962b3143dc54d4a
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
12,563
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.tower import algebra.invertible import linear_algebra.basis import ring_theory.adjoin.basic import ring_theory.polynomial.tower /-! # Towers of algebras We set up the basic theory of algebra towers. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. In `field_theory/tower.lean` we use this to prove the tower law for finite extensions, that if `R` and `S` are both fields, then `[A:R] = [A:S] [S:A]`. In this file we prepare the main lemma: if `{bi | i ∈ I}` is an `R`-basis of `S` and `{cj | j ∈ J}` is a `S`-basis of `A`, then `{bi cj | i ∈ I, j ∈ J}` is an `R`-basis of `A`. This statement does not require the base rings to be a field, so we also generalize the lemma to rings in this file. -/ universes u v w u₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) namespace is_scalar_tower section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] variables (R S A B) /-- Suppose that `R -> S -> A` is a tower of algebras. If an element `r : R` is invertible in `S`, then it is invertible in `A`. -/ def invertible.algebra_tower (r : R) [invertible (algebra_map R S r)] : invertible (algebra_map R A r) := invertible.copy (invertible.map (algebra_map S A : S →* A) (algebra_map R S r)) (algebra_map R A r) (by rw [ring_hom.coe_monoid_hom, is_scalar_tower.algebra_map_apply R S A]) /-- A natural number that is invertible when coerced to `R` is also invertible when coerced to any `R`-algebra. -/ def invertible_algebra_coe_nat (n : ℕ) [inv : invertible (n : R)] : invertible (n : A) := by { haveI : invertible (algebra_map ℕ R n) := inv, exact invertible.algebra_tower ℕ R A n } end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] end comm_semiring end is_scalar_tower namespace algebra theorem adjoin_algebra_map' {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] (s : set S) : adjoin R (algebra_map S (comap R S A) '' s) = subalgebra.map (adjoin R s) (to_comap R S A) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) theorem adjoin_algebra_map (R : Type u) (S : Type v) (A : Type w) [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (s : set S) : adjoin R (algebra_map S A '' s) = subalgebra.map (adjoin R s) (is_scalar_tower.to_alg_hom R S A) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) lemma adjoin_res (C D E : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [algebra C D] [algebra C E] [algebra D E] [is_scalar_tower C D E] (S : set E) : (algebra.adjoin D S).res C = ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)).under (algebra.adjoin ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) S) := begin suffices : set.range (algebra_map D E) = set.range (algebra_map ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) E), { ext x, change x ∈ subsemiring.closure (_ ∪ S) ↔ x ∈ subsemiring.closure (_ ∪ S), rw this }, ext x, split, { rintros ⟨y, hy⟩, exact ⟨⟨algebra_map D E y, ⟨y, ⟨algebra.mem_top, rfl⟩⟩⟩, hy⟩ }, { rintros ⟨⟨y, ⟨z, ⟨h0, h1⟩⟩⟩, h2⟩, exact ⟨z, eq.trans h1 h2⟩ }, end lemma adjoin_res_eq_adjoin_res (C D E F : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [comm_semiring F] [algebra C D] [algebra C E] [algebra C F] [algebra D F] [algebra E F] [is_scalar_tower C D F] [is_scalar_tower C E F] {S : set D} {T : set E} (hS : algebra.adjoin C S = ⊤) (hT : algebra.adjoin C T = ⊤) : (algebra.adjoin E (algebra_map D F '' S)).res C = (algebra.adjoin D (algebra_map E F '' T)).res C := by { rw [adjoin_res, adjoin_res, ←hS, ←hT, ←algebra.adjoin_image, ←algebra.adjoin_image, ←alg_hom.coe_to_ring_hom, ←alg_hom.coe_to_ring_hom, is_scalar_tower.coe_to_alg_hom, is_scalar_tower.coe_to_alg_hom, ←algebra.adjoin_union, ←algebra.adjoin_union, set.union_comm] } end algebra section open_locale classical lemma algebra.fg_trans' {R S A : Type*} [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (hRS : (⊤ : subalgebra R S).fg) (hSA : (⊤ : subalgebra S A).fg) : (⊤ : subalgebra R A).fg := let ⟨s, hs⟩ := hRS, ⟨t, ht⟩ := hSA in ⟨s.image (algebra_map S A) ∪ t, by rw [finset.coe_union, finset.coe_image, algebra.adjoin_union, algebra.adjoin_algebra_map, hs, algebra.map_top, is_scalar_tower.range_under_adjoin, ht, subalgebra.res_top]⟩ end section ring open finsupp open_locale big_operators classical universes v₁ w₁ variables {R S A} variables [comm_ring R] [ring S] [add_comm_group A] variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] theorem linear_independent_smul {ι : Type v₁} {b : ι → S} {ι' : Type w₁} {c : ι' → A} (hb : linear_independent R b) (hc : linear_independent S c) : linear_independent R (λ p : ι × ι', b p.1 • c p.2) := begin rw linear_independent_iff' at hb hc, rw linear_independent_iff'', rintros s g hg hsg ⟨i, k⟩, by_cases hik : (i, k) ∈ s, { have h1 : ∑ i in (s.image prod.fst).product (s.image prod.snd), g i • b i.1 • c i.2 = 0, { rw ← hsg, exact (finset.sum_subset finset.subset_product $ λ p _ hp, show g p • b p.1 • c p.2 = 0, by rw [hg p hp, zero_smul]).symm }, rw [finset.sum_product, finset.sum_comm] at h1, simp_rw [← smul_assoc, ← finset.sum_smul] at h1, exact hb _ _ (hc _ _ h1 k (finset.mem_image_of_mem _ hik)) i (finset.mem_image_of_mem _ hik) }, exact hg _ hik end /-- `basis.smul (b : basis ι R S) (c : basis ι S A)` is the `R`-basis on `A` where the `(i, j)`th basis vector is `b i • c j`. -/ noncomputable def basis.smul {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) : basis (ι × ι') R A := basis.of_repr ((c.repr.restrict_scalars R).trans $ (finsupp.lcongr (equiv.refl _) b.repr).trans $ (finsupp_prod_lequiv R).symm.trans $ (finsupp.lcongr (equiv.prod_comm ι' ι) (linear_equiv.refl _ _))) @[simp] theorem basis.smul_repr {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (x ij): (b.smul c).repr x ij = b.repr (c.repr x ij.2) ij.1 := by simp [basis.smul] theorem basis.smul_repr_mk {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (x i j): (b.smul c).repr x (i, j) = b.repr (c.repr x j) i := b.smul_repr c x (i, j) @[simp] theorem basis.smul_apply {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (ij) : (b.smul c) ij = b ij.1 • c ij.2 := begin obtain ⟨i, j⟩ := ij, rw basis.apply_eq_iff, ext ⟨i', j'⟩, rw [basis.smul_repr, linear_equiv.map_smul, basis.repr_self, finsupp.smul_apply, finsupp.single_apply], dsimp only, split_ifs with hi, { simp [hi, finsupp.single_apply] }, { simp [hi] }, end end ring section artin_tate variables (C : Type*) variables [comm_ring A] [comm_ring B] [comm_ring C] variables [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] open finset submodule open_locale classical lemma exists_subalgebra_of_fg (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) : ∃ B₀ : subalgebra A B, B₀.fg ∧ (⊤ : submodule B₀ C).fg := begin cases hAC with x hx, cases hBC with y hy, have := hy, simp_rw [eq_top_iff', mem_span_finset] at this, choose f hf, let s : finset B := (finset.product (x ∪ (y * y)) y).image (function.uncurry f), have hsx : ∀ (xi ∈ x) (yj ∈ y), f xi yj ∈ s := λ xi hxi yj hyj, show function.uncurry f (xi, yj) ∈ s, from mem_image_of_mem _ $ mem_product.2 ⟨mem_union_left _ hxi, hyj⟩, have hsy : ∀ (yi yj yk ∈ y), f (yi * yj) yk ∈ s := λ yi yj yk hyi hyj hyk, show function.uncurry f (yi * yj, yk) ∈ s, from mem_image_of_mem _ $ mem_product.2 ⟨mem_union_right _ $ finset.mul_mem_mul hyi hyj, hyk⟩, have hxy : ∀ xi ∈ x, xi ∈ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) := λ xi hxi, hf xi ▸ sum_mem _ (λ yj hyj, smul_mem (span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C)) ⟨f xi yj, algebra.subset_adjoin $ hsx xi hxi yj hyj⟩ (subset_span $ mem_insert_of_mem hyj)), have hyy : span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) * span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) ≤ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C), { rw [span_mul_span, span_le, coe_insert], rintros _ ⟨yi, yj, rfl | hyi, rfl | hyj, rfl⟩, { rw mul_one, exact subset_span (set.mem_insert _ _) }, { rw one_mul, exact subset_span (set.mem_insert_of_mem _ hyj) }, { rw mul_one, exact subset_span (set.mem_insert_of_mem _ hyi) }, { rw ← hf (yi * yj), exact set_like.mem_coe.2 (sum_mem _ $ λ yk hyk, smul_mem (span (algebra.adjoin A (↑s : set B)) (insert 1 ↑y : set C)) ⟨f (yi * yj) yk, algebra.subset_adjoin $ hsy yi yj yk hyi hyj hyk⟩ (subset_span $ set.mem_insert_of_mem _ hyk : yk ∈ _)) } }, refine ⟨algebra.adjoin A (↑s : set B), subalgebra.fg_adjoin_finset _, insert 1 y, _⟩, refine restrict_scalars_injective A _ _ _, rw [restrict_scalars_top, eq_top_iff, ← algebra.top_to_submodule, ← hx, algebra.adjoin_eq_span, span_le], refine λ r hr, submonoid.closure_induction hr (λ c hc, hxy c hc) (subset_span $ mem_insert_self _ _) (λ p q hp hq, hyy $ submodule.mul_mem_mul hp hq) end /-- Artin--Tate lemma: if A ⊆ B ⊆ C is a chain of subrings of commutative rings, and A is noetherian, and C is algebra-finite over A, and C is module-finite over B, then B is algebra-finite over A. References: Atiyah--Macdonald Proposition 7.8; Stacks 00IS; Altman--Kleiman 16.17. -/ theorem fg_of_fg_of_fg [is_noetherian_ring A] (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) (hBCi : function.injective (algebra_map B C)) : (⊤ : subalgebra A B).fg := let ⟨B₀, hAB₀, hB₀C⟩ := exists_subalgebra_of_fg A B C hAC hBC in algebra.fg_trans' (B₀.fg_top.2 hAB₀) $ subalgebra.fg_of_submodule_fg $ have is_noetherian_ring B₀, from is_noetherian_ring_of_fg hAB₀, have is_noetherian B₀ C, by exactI is_noetherian_of_fg_of_noetherian' hB₀C, by exactI fg_of_injective (is_scalar_tower.to_alg_hom B₀ B C).to_linear_map (linear_map.ker_eq_bot.2 hBCi) end artin_tate section alg_hom_tower variables {A} {C D : Type*} [comm_semiring A] [comm_semiring C] [comm_semiring D] [algebra A C] [algebra A D] variables (f : C →ₐ[A] D) (B) [comm_semiring B] [algebra A B] [algebra B C] [is_scalar_tower A B C] /-- Restrict the domain of an `alg_hom`. -/ def alg_hom.restrict_domain : B →ₐ[A] D := f.comp (is_scalar_tower.to_alg_hom A B C) /-- Extend the scalars of an `alg_hom`. -/ def alg_hom.extend_scalars : @alg_hom B C D _ _ _ _ (f.restrict_domain B).to_ring_hom.to_algebra := { commutes' := λ _, rfl .. f } variables {B} /-- `alg_hom`s from the top of a tower are equivalent to a pair of `alg_hom`s. -/ def alg_hom_equiv_sigma : (C →ₐ[A] D) ≃ Σ (f : B →ₐ[A] D), @alg_hom B C D _ _ _ _ f.to_ring_hom.to_algebra := { to_fun := λ f, ⟨f.restrict_domain B, f.extend_scalars B⟩, inv_fun := λ fg, let alg := fg.1.to_ring_hom.to_algebra in by exactI fg.2.restrict_scalars A, left_inv := λ f, by { dsimp only, ext, refl }, right_inv := begin rintros ⟨⟨f, _, _, _, _, _⟩, g, _, _, _, _, hg⟩, have : f = λ x, g (algebra_map B C x) := by { ext, exact (hg x).symm }, subst this, refl, end } end alg_hom_tower
1fcdca8f52399598af30402b36b8b9e4fba3e3ba
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/tt1.lean
c8a30b668398db0eb0fab555320b33b163b89418
[ "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
125
lean
import data.prod data.num logic.quantifiers open prod check (true, false, 10) -- definition a f := f check fun x, x ∧ x
8b3c2b2baf7bc0278873be17e9f4b5e69ff28bfb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/pequiv.lean
bb10920cf8b6c7c955e748fe3f4808261af32413
[ "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
13,887
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.set.basic /-! # Partial Equivalences > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we define partial equivalences `pequiv`, which are a bijection between a subset of `α` and a subset of `β`. Notationally, a `pequiv` is denoted by "`≃.`" (note that the full stop is part of the notation). The way we store these internally is with two functions `f : α → option β` and the reverse function `g : β → option α`, with the condition that if `f a` is `option.some b`, then `g b` is `option.some a`. ## Main results - `pequiv.of_set`: creates a `pequiv` from a set `s`, which sends an element to itself if it is in `s`. - `pequiv.single`: given two elements `a : α` and `b : β`, create a `pequiv` that sends them to each other, and ignores all other elements. - `pequiv.injective_of_forall_ne_is_some`/`injective_of_forall_is_some`: If the domain of a `pequiv` is all of `α` (except possibly one point), its `to_fun` is injective. ## Canonical order `pequiv` is canonically ordered by inclusion; that is, if a function `f` defined on a subset `s` is equal to `g` on that subset, but `g` is also defined on a larger set, then `f ≤ g`. We also have a definition of `⊥`, which is the empty `pequiv` (sends all to `none`), which in the end gives us a `semilattice_inf` with an `order_bot` instance. ## Tags pequiv, partial equivalence -/ universes u v w x /-- A `pequiv` is a partial equivalence, a representation of a bijection between a subset of `α` and a subset of `β`. See also `local_equiv` for a version that requires `to_fun` and `inv_fun` to be globally defined functions and has `source` and `target` sets as extra fields. -/ structure pequiv (α : Type u) (β : Type v) := (to_fun : α → option β) (inv_fun : β → option α) (inv : ∀ (a : α) (b : β), a ∈ inv_fun b ↔ b ∈ to_fun a) infixr ` ≃. `:25 := pequiv namespace pequiv variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} open function option instance fun_like : fun_like (α ≃. β) α (λ _, option β) := { coe := to_fun, coe_injective' := begin rintro ⟨f₁, f₂, hf⟩ ⟨g₁, g₂, hg⟩ (rfl : f₁ = g₁), congr' with y x, simp only [hf, hg] end } @[simp] lemma coe_mk_apply (f₁ : α → option β) (f₂ : β → option α) (h) (x : α) : (pequiv.mk f₁ f₂ h : α → option β) x = f₁ x := rfl @[ext] lemma ext {f g : α ≃. β} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h lemma ext_iff {f g : α ≃. β} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- The identity map as a partial equivalence. -/ @[refl] protected def refl (α : Type*) : α ≃. α := { to_fun := some, inv_fun := some, inv := λ _ _, eq_comm } /-- The inverse partial equivalence. -/ @[symm] protected def symm (f : α ≃. β) : β ≃. α := { to_fun := f.2, inv_fun := f.1, inv := λ _ _, (f.inv _ _).symm } lemma mem_iff_mem (f : α ≃. β) : ∀ {a : α} {b : β}, a ∈ f.symm b ↔ b ∈ f a := f.3 lemma eq_some_iff (f : α ≃. β) : ∀ {a : α} {b : β}, f.symm b = some a ↔ f a = some b := f.3 /-- Composition of partial equivalences `f : α ≃. β` and `g : β ≃. γ`. -/ @[trans] protected def trans (f : α ≃. β) (g : β ≃. γ) : α ≃. γ := { to_fun := λ a, (f a).bind g, inv_fun := λ a, (g.symm a).bind f.symm, inv := λ a b, by simp [*, and.comm, eq_some_iff f, eq_some_iff g] at * } @[simp] lemma refl_apply (a : α) : pequiv.refl α a = some a := rfl @[simp] lemma symm_refl : (pequiv.refl α).symm = pequiv.refl α := rfl @[simp] lemma symm_symm (f : α ≃. β) : f.symm.symm = f := by cases f; refl lemma symm_injective : function.injective (@pequiv.symm α β) := left_inverse.injective symm_symm lemma trans_assoc (f : α ≃. β) (g : β ≃. γ) (h : γ ≃. δ) : (f.trans g).trans h = f.trans (g.trans h) := ext (λ _, option.bind_assoc _ _ _) lemma mem_trans (f : α ≃. β) (g : β ≃. γ) (a : α) (c : γ) : c ∈ f.trans g a ↔ ∃ b, b ∈ f a ∧ c ∈ g b := option.bind_eq_some' lemma trans_eq_some (f : α ≃. β) (g : β ≃. γ) (a : α) (c : γ) : f.trans g a = some c ↔ ∃ b, f a = some b ∧ g b = some c := option.bind_eq_some' lemma trans_eq_none (f : α ≃. β) (g : β ≃. γ) (a : α) : f.trans g a = none ↔ (∀ b c, b ∉ f a ∨ c ∉ g b) := begin simp only [eq_none_iff_forall_not_mem, mem_trans, imp_iff_not_or.symm], push_neg, tauto end @[simp] lemma refl_trans (f : α ≃. β) : (pequiv.refl α).trans f = f := by ext; dsimp [pequiv.trans]; refl @[simp] lemma trans_refl (f : α ≃. β) : f.trans (pequiv.refl β) = f := by ext; dsimp [pequiv.trans]; simp protected lemma inj (f : α ≃. β) {a₁ a₂ : α} {b : β} (h₁ : b ∈ f a₁) (h₂ : b ∈ f a₂) : a₁ = a₂ := by rw ← mem_iff_mem at *; cases h : f.symm b; simp * at * /-- If the domain of a `pequiv` is `α` except a point, its forward direction is injective. -/ lemma injective_of_forall_ne_is_some (f : α ≃. β) (a₂ : α) (h : ∀ (a₁ : α), a₁ ≠ a₂ → is_some (f a₁)) : injective f := has_left_inverse.injective ⟨λ b, option.rec_on b a₂ (λ b', option.rec_on (f.symm b') a₂ id), λ x, begin classical, cases hfx : f x, { have : x = a₂, from not_imp_comm.1 (h x) (hfx.symm ▸ by simp), simp [this] }, { dsimp only, rw [(eq_some_iff f).2 hfx], refl } end⟩ /-- If the domain of a `pequiv` is all of `α`, its forward direction is injective. -/ lemma injective_of_forall_is_some {f : α ≃. β} (h : ∀ (a : α), is_some (f a)) : injective f := (classical.em (nonempty α)).elim (λ hn, injective_of_forall_ne_is_some f (classical.choice hn) (λ a _, h a)) (λ hn x, (hn ⟨x⟩).elim) section of_set variables (s : set α) [decidable_pred (∈ s)] /-- Creates a `pequiv` that is the identity on `s`, and `none` outside of it. -/ def of_set (s : set α) [decidable_pred (∈ s)] : α ≃. α := { to_fun := λ a, if a ∈ s then some a else none, inv_fun := λ a, if a ∈ s then some a else none, inv := λ a b, by { split_ifs with hb ha ha, { simp [eq_comm] }, { simp [ne_of_mem_of_not_mem hb ha] }, { simp [ne_of_mem_of_not_mem ha hb] }, { simp } } } lemma mem_of_set_self_iff {s : set α} [decidable_pred (∈ s)] {a : α} : a ∈ of_set s a ↔ a ∈ s := by dsimp [of_set]; split_ifs; simp * lemma mem_of_set_iff {s : set α} [decidable_pred (∈ s)] {a b : α} : a ∈ of_set s b ↔ a = b ∧ a ∈ s := begin dsimp [of_set], split_ifs, { simp only [iff_self_and, eq_comm], rintro rfl, exact h, }, { simp only [false_iff, not_and], rintro rfl, exact h, } end @[simp] lemma of_set_eq_some_iff {s : set α} {h : decidable_pred (∈ s)} {a b : α} : of_set s b = some a ↔ a = b ∧ a ∈ s := mem_of_set_iff @[simp] lemma of_set_eq_some_self_iff {s : set α} {h : decidable_pred (∈ s)} {a : α} : of_set s a = some a ↔ a ∈ s := mem_of_set_self_iff @[simp] lemma of_set_symm : (of_set s).symm = of_set s := rfl @[simp] lemma of_set_univ : of_set set.univ = pequiv.refl α := rfl @[simp] lemma of_set_eq_refl {s : set α} [decidable_pred (∈ s)] : of_set s = pequiv.refl α ↔ s = set.univ := ⟨λ h, begin rw [set.eq_univ_iff_forall], intro, rw [← mem_of_set_self_iff, h], exact rfl end, λ h, by simp only [← of_set_univ, h]⟩ end of_set lemma symm_trans_rev (f : α ≃. β) (g : β ≃. γ) : (f.trans g).symm = g.symm.trans f.symm := rfl lemma self_trans_symm (f : α ≃. β) : f.trans f.symm = of_set {a | (f a).is_some} := begin ext, dsimp [pequiv.trans], simp only [eq_some_iff f, option.is_some_iff_exists, option.mem_def, bind_eq_some', of_set_eq_some_iff], split, { rintros ⟨b, hb₁, hb₂⟩, exact ⟨pequiv.inj _ hb₂ hb₁, b, hb₂⟩ }, { simp {contextual := tt} } end lemma symm_trans_self (f : α ≃. β) : f.symm.trans f = of_set {b | (f.symm b).is_some} := symm_injective $ by simp [symm_trans_rev, self_trans_symm, -symm_symm] lemma trans_symm_eq_iff_forall_is_some {f : α ≃. β} : f.trans f.symm = pequiv.refl α ↔ ∀ a, is_some (f a) := by rw [self_trans_symm, of_set_eq_refl, set.eq_univ_iff_forall]; refl instance : has_bot (α ≃. β) := ⟨{ to_fun := λ _, none, inv_fun := λ _, none, inv := by simp }⟩ instance : inhabited (α ≃. β) := ⟨⊥⟩ @[simp] lemma bot_apply (a : α) : (⊥ : α ≃. β) a = none := rfl @[simp] lemma symm_bot : (⊥ : α ≃. β).symm = ⊥ := rfl @[simp] lemma trans_bot (f : α ≃. β) : f.trans (⊥ : β ≃. γ) = ⊥ := by ext; dsimp [pequiv.trans]; simp @[simp] lemma bot_trans (f : β ≃. γ) : (⊥ : α ≃. β).trans f = ⊥ := by ext; dsimp [pequiv.trans]; simp lemma is_some_symm_get (f : α ≃. β) {a : α} (h : is_some (f a)) : is_some (f.symm (option.get h)) := is_some_iff_exists.2 ⟨a, by rw [f.eq_some_iff, some_get]⟩ section single variables [decidable_eq α] [decidable_eq β] [decidable_eq γ] /-- Create a `pequiv` which sends `a` to `b` and `b` to `a`, but is otherwise `none`. -/ def single (a : α) (b : β) : α ≃. β := { to_fun := λ x, if x = a then some b else none, inv_fun := λ x, if x = b then some a else none, inv := λ _ _, by simp; split_ifs; cc } lemma mem_single (a : α) (b : β) : b ∈ single a b a := if_pos rfl lemma mem_single_iff (a₁ a₂ : α) (b₁ b₂ : β) : b₁ ∈ single a₂ b₂ a₁ ↔ a₁ = a₂ ∧ b₁ = b₂ := by dsimp [single]; split_ifs; simp [*, eq_comm] @[simp] lemma symm_single (a : α) (b : β) : (single a b).symm = single b a := rfl @[simp] lemma single_apply (a : α) (b : β) : single a b a = some b := if_pos rfl lemma single_apply_of_ne {a₁ a₂ : α} (h : a₁ ≠ a₂) (b : β) : single a₁ b a₂ = none := if_neg h.symm lemma single_trans_of_mem (a : α) {b : β} {c : γ} {f : β ≃. γ} (h : c ∈ f b) : (single a b).trans f = single a c := begin ext, dsimp [single, pequiv.trans], split_ifs; simp * at * end lemma trans_single_of_mem {a : α} {b : β} (c : γ) {f : α ≃. β} (h : b ∈ f a) : f.trans (single b c) = single a c := symm_injective $ single_trans_of_mem _ ((mem_iff_mem f).2 h) @[simp] lemma single_trans_single (a : α) (b : β) (c : γ) : (single a b).trans (single b c) = single a c := single_trans_of_mem _ (mem_single _ _) @[simp] lemma single_subsingleton_eq_refl [subsingleton α] (a b : α) : single a b = pequiv.refl α := begin ext i j, dsimp [single], rw [if_pos (subsingleton.elim i a), subsingleton.elim i j, subsingleton.elim b j] end lemma trans_single_of_eq_none {b : β} (c : γ) {f : δ ≃. β} (h : f.symm b = none) : f.trans (single b c) = ⊥ := begin ext, simp only [eq_none_iff_forall_not_mem, option.mem_def, f.eq_some_iff] at h, dsimp [pequiv.trans, single], simp, intros, split_ifs; simp * at * end lemma single_trans_of_eq_none (a : α) {b : β} {f : β ≃. δ} (h : f b = none) : (single a b).trans f = ⊥ := symm_injective $ trans_single_of_eq_none _ h lemma single_trans_single_of_ne {b₁ b₂ : β} (h : b₁ ≠ b₂) (a : α) (c : γ) : (single a b₁).trans (single b₂ c) = ⊥ := single_trans_of_eq_none _ (single_apply_of_ne h.symm _) end single section order instance : partial_order (α ≃. β) := { le := λ f g, ∀ (a : α) (b : β), b ∈ f a → b ∈ g a, le_refl := λ _ _ _, id, le_trans := λ f g h fg gh a b, (gh a b) ∘ (fg a b), le_antisymm := λ f g fg gf, ext begin assume a, cases h : g a with b, { exact eq_none_iff_forall_not_mem.2 (λ b hb, option.not_mem_none b $ h ▸ fg a b hb) }, { exact gf _ _ h } end } lemma le_def {f g : α ≃. β} : f ≤ g ↔ (∀ (a : α) (b : β), b ∈ f a → b ∈ g a) := iff.rfl instance : order_bot (α ≃. β) := { bot_le := λ _ _ _ h, (not_mem_none _ h).elim, ..pequiv.has_bot } instance [decidable_eq α] [decidable_eq β] : semilattice_inf (α ≃. β) := { inf := λ f g, { to_fun := λ a, if f a = g a then f a else none, inv_fun := λ b, if f.symm b = g.symm b then f.symm b else none, inv := λ a b, begin have hf := @mem_iff_mem _ _ f a b, have hg := @mem_iff_mem _ _ g a b, -- `split_ifs; finish` closes this goal from here split_ifs with h1 h2 h2; try { simp [hf] }, { contrapose! h2, rw h2, rw [←h1,hf,h2] at hg, simp only [mem_def, true_iff, eq_self_iff_true] at hg, rw [hg] }, { contrapose! h1, rw h1 at *, rw ←h2 at hg, simp only [mem_def, eq_self_iff_true, iff_true] at hf hg, rw [hf,hg] }, end }, inf_le_left := λ _ _ _ _, by simp; split_ifs; cc, inf_le_right := λ _ _ _ _, by simp; split_ifs; cc, le_inf := λ f g h fg gh a b, begin intro H, have hf := fg a b H, have hg := gh a b H, simp only [option.mem_def, pequiv.coe_mk_apply], split_ifs with h1, { exact hf }, { exact h1 (hf.trans hg.symm) }, end, ..pequiv.partial_order } end order end pequiv namespace equiv variables {α : Type*} {β : Type*} {γ : Type*} /-- Turns an `equiv` into a `pequiv` of the whole type. -/ def to_pequiv (f : α ≃ β) : α ≃. β := { to_fun := some ∘ f, inv_fun := some ∘ f.symm, inv := by simp [equiv.eq_symm_apply, eq_comm] } @[simp] lemma to_pequiv_refl : (equiv.refl α).to_pequiv = pequiv.refl α := rfl lemma to_pequiv_trans (f : α ≃ β) (g : β ≃ γ) : (f.trans g).to_pequiv = f.to_pequiv.trans g.to_pequiv := rfl lemma to_pequiv_symm (f : α ≃ β) : f.symm.to_pequiv = f.to_pequiv.symm := rfl lemma to_pequiv_apply (f : α ≃ β) (x : α) : f.to_pequiv x = some (f x) := rfl end equiv
d2714262ddd03a4a3901a0f4038e2fc7d9936084
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/elabIte.lean
8a0413f125e01c370a76472bff8b169c7444d80e
[ "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
62
lean
def f (x y : Nat) : Option Nat := if x > y then x else none
ef11fc95245968e871d69380252b5d0a32d9dc94
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/tests/lean/inaccessible2.lean
855153b7225a0bc8c6f9ffe081833464fc46b1e6
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
790
lean
inductive imf {A B : Sort*} (f : A → B) : B → Sort* | mk : ∀ (a : A), imf (f a) definition inv_1 {A B : Sort*} (f : A → B) : ∀ (b : B), imf f b → A | .(f .a) (imf.mk .f a) := a -- Error inaccessible annotation inside inaccessible annotation definition inv_2 {A B : Sort*} (f : A → B) : ∀ (b : B), imf f b → A | .(f a) (let x := (imf.mk .f a) in x) := a -- Error invalid occurrence of 'let' expression definition inv_3 {A B : Sort*} (f : A → B) : ∀ (b : B), imf f b → A | .(f a) ((λ (x : imf f b), x) (imf.mk .f a)) := a -- Error invalid occurrence of 'lambda' expression definition symm {A : Sort*} : ∀ a b : A, a = b → b = a | .a .a (eq.refl a) := rfl -- Error `a` in eq.refl must be marked as inaccessible since it is an inductive datatype parameter
7d67d6bb5e9330cf820c589deea42a479cbd70ff
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/separation.lean
22469d19d4ef89ebc00656d26feded65e70f6702
[ "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
61,442
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import topology.subset_properties import topology.connected /-! # Separation properties of topological spaces. This file defines the predicate `separated`, and common separation axioms (under the Kolmogorov classification). ## Main definitions * `separated`: Two `set`s are separated if they are contained in disjoint open sets. * `t0_space`: A T₀/Kolmogorov space is a space where, for every two points `x ≠ y`, there is an open set that contains one, but not the other. * `t1_space`: A T₁/Fréchet space is a space where every singleton set is closed. This is equivalent to, for every pair `x ≠ y`, there existing an open set containing `x` but not `y` (`t1_iff_exists_open` shows that these conditions are equivalent.) * `t2_space`: A T₂/Hausdorff space is a space where, for every two points `x ≠ y`, there is two disjoint open sets, one containing `x`, and the other `y`. * `t2_5_space`: A T₂.₅/Urysohn space is a space where, for every two points `x ≠ y`, there is two open sets, one containing `x`, and the other `y`, whose closures are disjoint. * `regular_space`: A T₃ space (sometimes referred to as regular, but authors vary on whether this includes T₂; `mathlib` does), is one where given any closed `C` and `x ∉ C`, there is disjoint open sets containing `x` and `C` respectively. In `mathlib`, T₃ implies T₂.₅. * `normal_space`: A T₄ space (sometimes referred to as normal, but authors vary on whether this includes T₂; `mathlib` does), is one where given two disjoint closed sets, we can find two open sets that separate them. In `mathlib`, T₄ implies T₃. ## Main results ### T₀ spaces * `is_closed.exists_closed_singleton` Given a closed set `S` in a compact T₀ space, there is some `x ∈ S` such that `{x}` is closed. * `exists_open_singleton_of_open_finset` Given an open `finset` `S` in a T₀ space, there is some `x ∈ S` such that `{x}` is open. ### T₁ spaces * `is_closed_map_const`: The constant map is a closed map. * `discrete_of_t1_of_finite`: A finite T₁ space must have the discrete topology. ### T₂ spaces * `t2_iff_nhds`: A space is T₂ iff the neighbourhoods of distinct points generate the bottom filter. * `t2_iff_is_closed_diagonal`: A space is T₂ iff the `diagonal` of `α` (that is, the set of all points of the form `(a, a) : α × α`) is closed under the product topology. * `finset_disjoing_finset_opens_of_t2`: Any two disjoint finsets are `separated`. * Most topological constructions preserve Hausdorffness; these results are part of the typeclass inference system (e.g. `embedding.t2_space`) * `set.eq_on.closure`: If two functions are equal on some set `s`, they are equal on its closure. * `is_compact.is_closed`: All compact sets are closed. * `locally_compact_of_compact_nhds`: If every point has a compact neighbourhood, then the space is locally compact. * `tot_sep_of_zero_dim`: If `α` has a clopen basis, it is a `totally_separated_space`. * `loc_compact_t2_tot_disc_iff_tot_sep`: A locally compact T₂ space is totally disconnected iff it is totally separated. If the space is also compact: * `normal_of_compact_t2`: A compact T₂ space is a `normal_space`. * `connected_components_eq_Inter_clopen`: The connected component of a point is the intersection of all its clopen neighbourhoods. * `compact_t2_tot_disc_iff_tot_sep`: Being a `totally_disconnected_space` is equivalent to being a `totally_separated_space`. * `connected_components.t2`: `connected_components α` is T₂ for `α` T₂ and compact. ### T₃ spaces * `disjoint_nested_nhds`: Given two points `x ≠ y`, we can find neighbourhoods `x ∈ V₁ ⊆ U₁` and `y ∈ V₂ ⊆ U₂`, with the `Vₖ` closed and the `Uₖ` open, such that the `Uₖ` are disjoint. ### Discrete spaces * `discrete_topology_iff_nhds`: Discrete topological spaces are those whose neighbourhood filters are the `pure` filter (which is the principal filter at a singleton). * `induced_bot`/`discrete_topology_induced`: The pullback of the discrete topology under an inclusion is the discrete topology. ## References https://en.wikipedia.org/wiki/Separation_axiom -/ open set filter open_locale topological_space filter classical universes u v variables {α : Type u} {β : Type v} [topological_space α] section separation /-- `separated` is a predicate on pairs of sub`set`s of a topological space. It holds if the two sub`set`s are contained in disjoint open sets. -/ def separated : set α → set α → Prop := λ (s t : set α), ∃ U V : (set α), (is_open U) ∧ is_open V ∧ (s ⊆ U) ∧ (t ⊆ V) ∧ disjoint U V namespace separated open separated @[symm] lemma symm {s t : set α} : separated s t → separated t s := λ ⟨U, V, oU, oV, aU, bV, UV⟩, ⟨V, U, oV, oU, bV, aU, disjoint.symm UV⟩ lemma comm (s t : set α) : separated s t ↔ separated t s := ⟨symm, symm⟩ lemma empty_right (a : set α) : separated a ∅ := ⟨_, _, is_open_univ, is_open_empty, λ a h, mem_univ a, λ a h, by cases h, disjoint_empty _⟩ lemma empty_left (a : set α) : separated ∅ a := (empty_right _).symm lemma union_left {a b c : set α} : separated a c → separated b c → separated (a ∪ b) c := λ ⟨U, V, oU, oV, aU, bV, UV⟩ ⟨W, X, oW, oX, aW, bX, WX⟩, ⟨U ∪ W, V ∩ X, is_open.union oU oW, is_open.inter oV oX, union_subset_union aU aW, subset_inter bV bX, set.disjoint_union_left.mpr ⟨disjoint_of_subset_right (inter_subset_left _ _) UV, disjoint_of_subset_right (inter_subset_right _ _) WX⟩⟩ lemma union_right {a b c : set α} (ab : separated a b) (ac : separated a c) : separated a (b ∪ c) := (ab.symm.union_left ac.symm).symm end separated /-- 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))) /-- Given a closed set `S` in a compact T₀ space, there is some `x ∈ S` such that `{x}` is closed. -/ theorem is_closed.exists_closed_singleton {α : Type*} [topological_space α] [t0_space α] [compact_space α] {S : set α} (hS : is_closed S) (hne : S.nonempty) : ∃ (x : α), x ∈ S ∧ is_closed ({x} : set α) := begin obtain ⟨V, Vsub, Vne, Vcls, hV⟩ := hS.exists_minimal_nonempty_closed_subset hne, by_cases hnt : ∃ (x y : α) (hx : x ∈ V) (hy : y ∈ V), x ≠ y, { exfalso, obtain ⟨x, y, hx, hy, hne⟩ := hnt, obtain ⟨U, hU, hsep⟩ := t0_space.t0 _ _ hne, have : ∀ (z w : α) (hz : z ∈ V) (hw : w ∈ V) (hz' : z ∈ U) (hw' : ¬ w ∈ U), false, { intros z w hz hw hz' hw', have uvne : (V ∩ Uᶜ).nonempty, { use w, simp only [hw, hw', set.mem_inter_eq, not_false_iff, and_self, set.mem_compl_eq], }, specialize hV (V ∩ Uᶜ) (set.inter_subset_left _ _) uvne (is_closed.inter Vcls (is_closed_compl_iff.mpr hU)), have : V ⊆ Uᶜ, { rw ←hV, exact set.inter_subset_right _ _ }, exact this hz hz', }, cases hsep, { exact this x y hx hy hsep.1 hsep.2 }, { exact this y x hy hx hsep.1 hsep.2 } }, { push_neg at hnt, obtain ⟨z, hz⟩ := Vne, refine ⟨z, Vsub hz, _⟩, convert Vcls, ext, simp only [set.mem_singleton_iff, set.mem_compl_eq], split, { rintro rfl, exact hz, }, { exact λ hx, hnt x z hx hz, }, }, end /-- Given an open `finset` `S` in a T₀ space, there is some `x ∈ S` such that `{x}` is open. -/ 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.ext_iff_val).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_compl_singleton [t1_space α] {x : α} : is_open ({x}ᶜ : set α) := is_closed_singleton.is_open_compl lemma is_open_ne [t1_space α] {x : α} : is_open {y | y ≠ x} := is_open_compl_singleton lemma ne.nhds_within_compl_singleton [t1_space α] {x y : α} (h : x ≠ y) : 𝓝[{y}ᶜ] x = 𝓝 x := is_open_ne.nhds_within_eq h lemma continuous_within_at_update_of_ne [t1_space α] [decidable_eq α] [topological_space β] {f : α → β} {s : set α} {x y : α} {z : β} (hne : y ≠ x) : continuous_within_at (function.update f x z) s y ↔ continuous_within_at f s y := eventually_eq.congr_continuous_within_at (mem_nhds_within_of_mem_nhds $ mem_of_superset (is_open_ne.mem_nhds hne) $ λ y' hy', function.update_noteq hy' _ _) (function.update_noteq hne _ _) lemma continuous_on_update_iff [t1_space α] [decidable_eq α] [topological_space β] {f : α → β} {s : set α} {x : α} {y : β} : continuous_on (function.update f x y) s ↔ continuous_on f (s \ {x}) ∧ (x ∈ s → tendsto f (𝓝[s \ {x}] x) (𝓝 y)) := begin rw [continuous_on, ← and_forall_ne x, and_comm], refine and_congr ⟨λ H z hz, _, λ H z hzx hzs, _⟩ (forall_congr $ λ hxs, _), { specialize H z hz.2 hz.1, rw continuous_within_at_update_of_ne hz.2 at H, exact H.mono (diff_subset _ _) }, { rw continuous_within_at_update_of_ne hzx, refine (H z ⟨hzs, hzx⟩).mono_of_mem (inter_mem_nhds_within _ _), exact is_open_ne.mem_nhds hzx }, { exact continuous_within_at_update_same } end 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.ext_iff_val]⟩⟩ @[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 t1_iff_exists_open : t1_space α ↔ ∀ (x y), x ≠ y → (∃ (U : set α) (hU : is_open U), x ∈ U ∧ y ∉ U) := begin split, { introsI t1 x y hxy, exact ⟨{y}ᶜ, is_open_compl_iff.mpr (t1_space.t1 y), mem_compl_singleton_iff.mpr hxy, not_not.mpr rfl⟩}, { intro h, constructor, intro x, rw ← is_open_compl_iff, have p : ⋃₀ {U : set α | (x ∉ U) ∧ (is_open U)} = {x}ᶜ, { apply subset.antisymm; intros t ht, { rcases ht with ⟨A, ⟨hxA, hA⟩, htA⟩, rw [mem_compl_eq, mem_singleton_iff], rintro rfl, contradiction }, { obtain ⟨U, hU, hh⟩ := h t x (mem_compl_singleton_iff.mp ht), exact ⟨U, ⟨hh.2, hU⟩, hh.1⟩}}, rw ← p, exact is_open_sUnion (λ B hB, hB.2) } end lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : {x}ᶜ ∈ 𝓝 y := is_open.mem_nhds is_open_compl_singleton $ by rwa [mem_compl_eq, mem_singleton_iff] @[simp] lemma closure_singleton [t1_space α] {a : α} : closure ({a} : set α) = {a} := is_closed_singleton.closure_eq lemma set.subsingleton.closure [t1_space α] {s : set α} (hs : s.subsingleton) : (closure s).subsingleton := hs.induction_on (by simp) $ λ x, by simp @[simp] lemma subsingleton_closure [t1_space α] {s : set α} : (closure s).subsingleton ↔ s.subsingleton := ⟨λ h, h.mono subset_closure, λ h, h.closure⟩ lemma is_closed_map_const {α β} [topological_space α] [topological_space β] [t1_space β] {y : β} : is_closed_map (function.const α y) := begin apply is_closed_map.of_nonempty, intros s hs h2s, simp_rw [h2s.image_const, is_closed_singleton] end lemma finite.is_closed {α} [topological_space α] [t1_space α] {s : set α} (hs : set.finite s) : is_closed s := begin rw ← bUnion_of_singleton s, exact is_closed_bUnion hs (λ i hi, is_closed_singleton) end lemma bInter_basis_nhds [t1_space α] {ι : Sort*} {p : ι → Prop} {s : ι → set α} {x : α} (h : (𝓝 x).has_basis p s) : (⋂ i (h : p i), s i) = {x} := begin simp only [eq_singleton_iff_unique_mem, mem_Inter], refine ⟨λ i hi, mem_of_mem_nhds $ h.mem_of_mem hi, λ y hy, _⟩, contrapose! hy, rcases h.mem_iff.1 (compl_singleton_mem_nhds hy.symm) with ⟨i, hi, hsub⟩, exact ⟨i, hi, λ h, hsub h rfl⟩ end /-- If a function to a `t1_space` tends to some limit `b` at some point `a`, then necessarily `b = f a`. -/ lemma eq_of_tendsto_nhds [topological_space β] [t1_space β] {f : α → β} {a : α} {b : β} (h : tendsto f (𝓝 a) (𝓝 b)) : f a = b := by_contra $ assume (hfa : f a ≠ b), have fact₁ : {f a}ᶜ ∈ 𝓝 b := compl_singleton_mem_nhds hfa.symm, have fact₂ : tendsto f (pure a) (𝓝 b) := h.comp (tendsto_id' $ pure_le_nhds a), fact₂ fact₁ (eq.refl $ f a) /-- To prove a function to a `t1_space` is continuous at some point `a`, it suffices to prove that `f` admits *some* limit at `a`. -/ lemma continuous_at_of_tendsto_nhds [topological_space β] [t1_space β] {f : α → β} {a : α} {b : β} (h : tendsto f (𝓝 a) (𝓝 b)) : continuous_at f a := show tendsto f (𝓝 a) (𝓝 $ f a), by rwa eq_of_tendsto_nhds h /-- If the punctured neighborhoods of a point form a nontrivial filter, then any neighborhood is infinite. -/ lemma infinite_of_mem_nhds {α} [topological_space α] [t1_space α] (x : α) [hx : ne_bot (𝓝[{x}ᶜ] x)] {s : set α} (hs : s ∈ 𝓝 x) : set.infinite s := begin unfreezingI { contrapose! hx }, rw set.not_infinite at hx, have A : is_closed (s \ {x}) := finite.is_closed (hx.subset (diff_subset _ _)), have B : (s \ {x})ᶜ ∈ 𝓝 x, { apply is_open.mem_nhds, { apply is_open_compl_iff.2 A }, { simp only [not_true, not_false_iff, mem_diff, and_false, mem_compl_eq, mem_singleton] } }, have C : {x} ∈ 𝓝 x, { apply filter.mem_of_superset (filter.inter_mem hs B), assume y hy, simp only [mem_singleton_iff, mem_inter_eq, not_and, not_not, mem_diff, mem_compl_eq] at hy, simp only [hy.right hy.left, mem_singleton] }, have D : {x}ᶜ ∈ 𝓝[{x}ᶜ] x := self_mem_nhds_within, simpa [← empty_mem_iff_bot] using filter.inter_mem (mem_nhds_within_of_mem_nhds C) D end lemma discrete_of_t1_of_finite {X : Type*} [topological_space X] [t1_space X] [fintype X] : discrete_topology X := begin apply singletons_open_iff_discrete.mp, intros x, rw [← is_closed_compl_iff], exact finite.is_closed (finite.of_fintype _) end lemma singleton_mem_nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : {x} ∈ 𝓝[s] x := begin have : ({⟨x, hx⟩} : set s) ∈ 𝓝 (⟨x, hx⟩ : s), by simp [nhds_discrete], simpa only [nhds_within_eq_map_subtype_coe hx, image_singleton] using @image_mem_map _ _ _ (coe : s → α) _ this end /-- The neighbourhoods filter of `x` within `s`, under the discrete topology, is equal to the pure `x` filter (which is the principal filter at the singleton `{x}`.) -/ lemma nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : 𝓝[s] x = pure x := le_antisymm (le_pure_iff.2 $ singleton_mem_nhds_within_of_mem_discrete hx) (pure_le_nhds_within hx) lemma filter.has_basis.exists_inter_eq_singleton_of_mem_discrete {ι : Type*} {p : ι → Prop} {t : ι → set α} {s : set α} [discrete_topology s] {x : α} (hb : (𝓝 x).has_basis p t) (hx : x ∈ s) : ∃ i (hi : p i), t i ∩ s = {x} := begin rcases (nhds_within_has_basis hb s).mem_iff.1 (singleton_mem_nhds_within_of_mem_discrete hx) with ⟨i, hi, hix⟩, exact ⟨i, hi, subset.antisymm hix $ singleton_subset_iff.2 ⟨mem_of_mem_nhds $ hb.mem_of_mem hi, hx⟩⟩ end /-- A point `x` in a discrete subset `s` of a topological space admits a neighbourhood that only meets `s` at `x`. -/ lemma nhds_inter_eq_singleton_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : ∃ U ∈ 𝓝 x, U ∩ s = {x} := by simpa using (𝓝 x).basis_sets.exists_inter_eq_singleton_of_mem_discrete hx /-- For point `x` in a discrete subset `s` of a topological space, there is a set `U` such that 1. `U` is a punctured neighborhood of `x` (ie. `U ∪ {x}` is a neighbourhood of `x`), 2. `U` is disjoint from `s`. -/ lemma disjoint_nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) : ∃ U ∈ 𝓝[{x}ᶜ] x, disjoint U s := let ⟨V, h, h'⟩ := nhds_inter_eq_singleton_of_mem_discrete hx in ⟨{x}ᶜ ∩ V, inter_mem_nhds_within _ h, (disjoint_iff_inter_eq_empty.mpr (by { rw [inter_assoc, h', compl_inter_self] }))⟩ /-- Let `X` be a topological space and let `s, t ⊆ X` be two subsets. If there is an inclusion `t ⊆ s`, then the topological space structure on `t` induced by `X` is the same as the one obtained by the induced topological space structure on `s`. -/ lemma topological_space.subset_trans {X : Type*} [tX : topological_space X] {s t : set X} (ts : t ⊆ s) : (subtype.topological_space : topological_space t) = (subtype.topological_space : topological_space s).induced (set.inclusion ts) := begin change tX.induced ((coe : s → X) ∘ (set.inclusion ts)) = topological_space.induced (set.inclusion ts) (tX.induced _), rw ← induced_compose, end /-- This lemma characterizes discrete topological spaces as those whose singletons are neighbourhoods. -/ lemma discrete_topology_iff_nhds {X : Type*} [topological_space X] : discrete_topology X ↔ (nhds : X → filter X) = pure := begin split, { introI hX, exact nhds_discrete X }, { intro h, constructor, apply eq_of_nhds_eq_nhds, simp [h, nhds_bot] } end /-- The topology pulled-back under an inclusion `f : X → Y` from the discrete topology (`⊥`) is the discrete topology. This version does not assume the choice of a topology on either the source `X` nor the target `Y` of the inclusion `f`. -/ lemma induced_bot {X Y : Type*} {f : X → Y} (hf : function.injective f) : topological_space.induced f ⊥ = ⊥ := eq_of_nhds_eq_nhds (by simp [nhds_induced, ← set.image_singleton, hf.preimage_image, nhds_bot]) /-- The topology induced under an inclusion `f : X → Y` from the discrete topological space `Y` is the discrete topology on `X`. -/ lemma discrete_topology_induced {X Y : Type*} [tY : topological_space Y] [discrete_topology Y] {f : X → Y} (hf : function.injective f) : @discrete_topology X (topological_space.induced f tY) := begin constructor, rw discrete_topology.eq_bot Y, exact induced_bot hf end /-- Let `s, t ⊆ X` be two subsets of a topological space `X`. If `t ⊆ s` and the topology induced by `X`on `s` is discrete, then also the topology induces on `t` is discrete. -/ lemma discrete_topology.of_subset {X : Type*} [topological_space X] {s t : set X} (ds : discrete_topology s) (ts : t ⊆ s) : discrete_topology t := begin rw [topological_space.subset_trans ts, ds.eq_bot], exact {eq_bot := induced_bot (set.inclusion_injective ts)} end /-- 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_compl_iff.1 $ 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 : ne_bot (𝓝 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 (is_open.mem_nhds hu hx) (is_open.mem_nhds hv hy)).ne_empty /-- A space is T₂ iff the neighbourhoods of distinct points generate the bottom filter. -/ lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, ne_bot (𝓝 x ⊓ 𝓝 y) → x = y := ⟨assume h, by exactI λ x y, eq_of_nhds_ne_bot, assume h, ⟨assume x y xy, have 𝓝 x ⊓ 𝓝 y = ⊥ := not_ne_bot.1 $ mt h xy, let ⟨u', hu', v', hv', u'v'⟩ := empty_mem_iff_bot.mpr this, ⟨u, uu', uo, hu⟩ := mem_nhds_iff.mp hu', ⟨v, vv', vo, hv⟩ := mem_nhds_iff.mp hv' in ⟨u, v, uo, vo, hu, hv, by { rw [← subset_empty_iff, u'v'], exact inter_subset_inter uu' vv' }⟩⟩⟩ lemma t2_iff_ultrafilter : t2_space α ↔ ∀ {x y : α} (f : ultrafilter α), ↑f ≤ 𝓝 x → ↑f ≤ 𝓝 y → x = y := t2_iff_nhds.trans $ by simp only [←exists_ultrafilter_iff, and_imp, le_inf_iff, exists_imp_distrib] lemma is_closed_diagonal [t2_space α] : is_closed (diagonal α) := begin refine is_closed_iff_cluster_pt.mpr _, rintro ⟨a₁, a₂⟩ h, refine eq_of_nhds_ne_bot ⟨λ this : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h.ne _⟩, obtain ⟨t₁, (ht₁ : t₁ ∈ 𝓝 a₁), t₂, (ht₂ : t₂ ∈ 𝓝 a₂), (h' : t₁ ∩ t₂ = ∅)⟩ := inf_eq_bot_iff.1 this, rw [inf_principal_eq_bot, nhds_prod_eq], apply mem_of_superset (prod_mem_prod ht₁ ht₂), rintro ⟨x, y⟩ ⟨x_in, y_in⟩ (heq : x = y), rw ← heq at *, have : x ∈ t₁ ∩ t₂ := ⟨x_in, y_in⟩, rwa h' at this end lemma t2_iff_is_closed_diagonal : t2_space α ↔ is_closed (diagonal α) := begin split, { introI h, exact is_closed_diagonal }, { intro h, constructor, intros x y hxy, have : (x, y) ∈ (diagonal α)ᶜ, by rwa [mem_compl_iff], obtain ⟨t, t_sub, t_op, xyt⟩ : ∃ t ⊆ (diagonal α)ᶜ, is_open t ∧ (x, y) ∈ t := is_open_iff_forall_mem_open.mp h.is_open_compl _ this, rcases is_open_prod_iff.mp t_op x y xyt with ⟨U, V, U_op, V_op, xU, yV, H⟩, use [U, V, U_op, V_op, xU, yV], have := subset.trans H t_sub, rw eq_empty_iff_forall_not_mem, rintros z ⟨zU, zV⟩, have : ¬ (z, z) ∈ diagonal α := this (mk_mem_prod zU zV), exact this rfl }, end section separated open separated finset lemma finset_disjoint_finset_opens_of_t2 [t2_space α] : ∀ (s t : finset α), disjoint s t → separated (s : set α) t := begin refine induction_on_union _ (λ a b hi d, (hi d.symm).symm) (λ a d, empty_right a) (λ a b ab, _) _, { obtain ⟨U, V, oU, oV, aU, bV, UV⟩ := t2_separation (finset.disjoint_singleton.1 ab), refine ⟨U, V, oU, oV, _, _, set.disjoint_iff_inter_eq_empty.mpr UV⟩; exact singleton_subset_set_iff.mpr ‹_› }, { intros a b c ac bc d, apply_mod_cast union_left (ac (disjoint_of_subset_left (a.subset_union_left b) d)) (bc _), exact disjoint_of_subset_left (a.subset_union_right b) d }, end lemma point_disjoint_finset_opens_of_t2 [t2_space α] {x : α} {s : finset α} (h : x ∉ s) : separated ({x} : set α) s := by exact_mod_cast finset_disjoint_finset_opens_of_t2 {x} s (finset.disjoint_singleton_left.mpr h) end separated @[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 : α} [ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb lemma tendsto_nhds_unique' [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : ne_bot l) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb lemma tendsto_nhds_unique_of_eventually_eq [t2_space α] {f g : β → α} {l : filter β} {a b : α} [ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto g l (𝓝 b)) (hfg : f =ᶠ[l] g) : a = b := tendsto_nhds_unique (ha.congr' hfg) hb lemma tendsto_const_nhds_iff [t2_space α] {l : filter α} [ne_bot l] {c d : α} : tendsto (λ x, c) l (𝓝 d) ↔ c = d := ⟨λ h, tendsto_nhds_unique (tendsto_const_nhds) h, λ h, h ▸ tendsto_const_nhds⟩ /-- A T₂.₅ space, also known as a Urysohn space, is a topological space where for every pair `x ≠ y`, there are two open sets, with the intersection of clousures empty, one containing `x` and the other `y` . -/ class t2_5_space (α : Type u) [topological_space α]: Prop := (t2_5 : ∀ x y (h : x ≠ y), ∃ (U V: set α), is_open U ∧ is_open V ∧ closure U ∩ closure V = ∅ ∧ x ∈ U ∧ y ∈ V) @[priority 100] -- see Note [lower instance priority] instance t2_5_space.t2_space [t2_5_space α] : t2_space α := ⟨λ x y hxy, let ⟨U, V, hU, hV, hUV, hh⟩ := t2_5_space.t2_5 x y hxy in ⟨U, V, hU, hV, hh.1, hh.2, subset_eq_empty (powerset_mono.mpr (closure_inter_subset_inter_closure U V) subset_closure) hUV⟩⟩ section lim variables [t2_space α] {f : filter α} /-! ### Properties of `Lim` and `lim` In this section we use explicit `nonempty α` instances for `Lim` and `lim`. This way the lemmas are useful without a `nonempty α` instance. -/ lemma Lim_eq {a : α} [ne_bot f] (h : f ≤ 𝓝 a) : @Lim _ _ ⟨a⟩ f = a := tendsto_nhds_unique (le_nhds_Lim ⟨a, h⟩) h lemma Lim_eq_iff [ne_bot f] (h : ∃ (a : α), f ≤ nhds a) {a} : @Lim _ _ ⟨a⟩ f = a ↔ f ≤ 𝓝 a := ⟨λ c, c ▸ le_nhds_Lim h, Lim_eq⟩ lemma ultrafilter.Lim_eq_iff_le_nhds [compact_space α] {x : α} {F : ultrafilter α} : F.Lim = x ↔ ↑F ≤ 𝓝 x := ⟨λ h, h ▸ F.le_nhds_Lim, Lim_eq⟩ lemma is_open_iff_ultrafilter' [compact_space α] (U : set α) : is_open U ↔ (∀ F : ultrafilter α, F.Lim ∈ U → U ∈ F.1) := begin rw is_open_iff_ultrafilter, refine ⟨λ h F hF, h F.Lim hF F F.le_nhds_Lim, _⟩, intros cond x hx f h, rw [← (ultrafilter.Lim_eq_iff_le_nhds.2 h)] at hx, exact cond _ hx end lemma filter.tendsto.lim_eq {a : α} {f : filter β} [ne_bot f] {g : β → α} (h : tendsto g f (𝓝 a)) : @lim _ _ _ ⟨a⟩ f g = a := Lim_eq h lemma filter.lim_eq_iff {f : filter β} [ne_bot f] {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) {a} : @lim _ _ _ ⟨a⟩ f g = a ↔ tendsto g f (𝓝 a) := ⟨λ c, c ▸ tendsto_nhds_lim h, filter.tendsto.lim_eq⟩ lemma continuous.lim_eq [topological_space β] {f : β → α} (h : continuous f) (a : β) : @lim _ _ _ ⟨f a⟩ (𝓝 a) f = f a := (h.tendsto a).lim_eq @[simp] lemma Lim_nhds (a : α) : @Lim _ _ ⟨a⟩ (𝓝 a) = a := Lim_eq (le_refl _) @[simp] lemma lim_nhds_id (a : α) : @lim _ _ _ ⟨a⟩ (𝓝 a) id = a := Lim_nhds a @[simp] lemma Lim_nhds_within {a : α} {s : set α} (h : a ∈ closure s) : @Lim _ _ ⟨a⟩ (𝓝[s] a) = a := by haveI : ne_bot (𝓝[s] a) := mem_closure_iff_cluster_pt.1 h; exact Lim_eq inf_le_left @[simp] lemma lim_nhds_within_id {a : α} {s : set α} (h : a ∈ closure s) : @lim _ _ _ ⟨a⟩ (𝓝[s] a) id = a := Lim_nhds_within h end lim /-! ### `t2_space` constructions We use two lemmas to prove that various standard constructions generate Hausdorff spaces from Hausdorff spaces: * `separated_by_continuous` says that two points `x y : α` can be separated by open neighborhoods provided that there exists a continuous map `f : α → β` with a Hausdorff codomain such that `f x ≠ f y`. We use this lemma to prove that topological spaces defined using `induced` are Hausdorff spaces. * `separated_by_open_embedding` says that for an open embedding `f : α → β` of a Hausdorff space `α`, the images of two distinct points `x y : α`, `x ≠ y` can be separated by open neighborhoods. We use this lemma to prove that topological spaces defined using `coinduced` are Hausdorff spaces. -/ @[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⟩ } lemma separated_by_continuous {α : Type*} {β : Type*} [topological_space α] [topological_space β] [t2_space β] {f : α → β} (hf : continuous 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, uo.preimage hf, vo.preimage hf, xu, yv, by rw [←preimage_inter, uv, preimage_empty]⟩ lemma separated_by_open_embedding {α β : Type*} [topological_space α] [topological_space β] [t2_space α] {f : α → β} (hf : open_embedding f) {x y : α} (h : x ≠ y) : ∃ u v : set β, is_open u ∧ is_open v ∧ f x ∈ u ∧ f y ∈ v ∧ u ∩ v = ∅ := let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in ⟨f '' u, f '' v, hf.is_open_map _ uo, hf.is_open_map _ vo, mem_image_of_mem _ xu, mem_image_of_mem _ yv, by rw [image_inter hf.inj, uv, image_empty]⟩ instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) := ⟨assume x y h, separated_by_continuous continuous_subtype_val (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_continuous continuous_fst h₁) (λ h₂, separated_by_continuous continuous_snd h₂)⟩ lemma embedding.t2_space [topological_space β] [t2_space β] {f : α → β} (hf : embedding f) : t2_space α := ⟨λ x y h, separated_by_continuous hf.continuous (hf.inj.ne h)⟩ instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] : t2_space (α ⊕ β) := begin constructor, rintros (x|x) (y|y) h, { replace h : x ≠ y := λ c, (c.subst h) rfl, exact separated_by_open_embedding open_embedding_inl h }, { exact ⟨_, _, is_open_range_inl, is_open_range_inr, ⟨x, rfl⟩, ⟨y, rfl⟩, range_inl_inter_range_inr⟩ }, { exact ⟨_, _, is_open_range_inr, is_open_range_inl, ⟨x, rfl⟩, ⟨y, rfl⟩, range_inr_inter_range_inl⟩ }, { replace h : x ≠ y := λ c, (c.subst h) rfl, exact separated_by_open_embedding open_embedding_inr h } end 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_continuous (continuous_apply i) hi⟩ instance sigma.t2_space {ι : Type*} {α : ι → Type*} [Πi, topological_space (α i)] [∀a, t2_space (α a)] : t2_space (Σi, α i) := begin constructor, rintros ⟨i, x⟩ ⟨j, y⟩ neq, rcases em (i = j) with (rfl|h), { replace neq : x ≠ y := λ c, (c.subst neq) rfl, exact separated_by_open_embedding open_embedding_sigma_mk neq }, { exact ⟨_, _, is_open_range_sigma_mk, is_open_range_sigma_mk, ⟨x, rfl⟩, ⟨y, rfl⟩, by tidy⟩ } 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 /-- If two continuous maps are equal on `s`, then they are equal on the closure of `s`. -/ lemma set.eq_on.closure [t2_space α] {s : set β} {f g : β → α} (h : eq_on f g s) (hf : continuous f) (hg : continuous g) : eq_on f g (closure s) := closure_minimal h (is_closed_eq hf hg) /-- If two continuous functions are equal on a dense set, then they are equal. -/ lemma continuous.ext_on [t2_space α] {s : set β} (hs : dense s) {f g : β → α} (hf : continuous f) (hg : continuous g) (h : eq_on f g s) : f = g := funext $ λ x, h.closure hf hg (hs x) lemma function.left_inverse.closed_range [t2_space α] {f : α → β} {g : β → α} (h : function.left_inverse f g) (hf : continuous f) (hg : continuous g) : is_closed (range g) := have eq_on (g ∘ f) id (closure $ range g), from h.right_inv_on_range.eq_on.closure (hg.comp hf) continuous_id, is_closed_of_closure_subset $ λ x hx, calc x = g (f x) : (this hx).symm ... ∈ _ : mem_range_self _ lemma function.left_inverse.closed_embedding [t2_space α] {f : α → β} {g : β → α} (h : function.left_inverse f g) (hf : continuous f) (hg : continuous g) : closed_embedding g := ⟨h.embedding hf hg, h.closed_range hf hg⟩ 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 : is_compact s) (ht : is_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.is_open_compl hst /-- In a `t2_space`, every compact set is closed. -/ lemma is_compact.is_closed [t2_space α] {s : set α} (hs : is_compact s) : is_closed s := is_open_compl_iff.1 $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (is_compact_singleton : is_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⟩ @[simp] lemma filter.coclosed_compact_eq_cocompact [t2_space α] : coclosed_compact α = cocompact α := by simp [coclosed_compact, cocompact, infi_and', and_iff_right_of_imp is_compact.is_closed] /-- If `V : ι → set α` is a decreasing family of compact sets then any neighborhood of `⋂ i, V i` contains some `V i`. This is a version of `exists_subset_nhd_of_compact'` where we don't need to assume each `V i` closed because it follows from compactness since `α` is assumed to be Hausdorff. -/ lemma exists_subset_nhd_of_compact [t2_space α] {ι : Type*} [nonempty ι] {V : ι → set α} (hV : directed (⊇) V) (hV_cpct : ∀ i, is_compact (V i)) {U : set α} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := exists_subset_nhd_of_compact' hV hV_cpct (λ i, (hV_cpct i).is_closed) hU lemma compact_exhaustion.is_closed [t2_space α] (K : compact_exhaustion α) (n : ℕ) : is_closed (K n) := (K.is_compact n).is_closed lemma is_compact.inter [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) : is_compact (s ∩ t) := hs.inter_right $ ht.is_closed lemma compact_closure_of_subset_compact [t2_space α] {s t : set α} (ht : is_compact t) (h : s ⊆ t) : is_compact (closure s) := compact_of_is_closed_subset ht is_closed_closure (closure_minimal h ht.is_closed) lemma image_closure_of_compact [t2_space β] {s : set α} (hs : is_compact (closure s)) {f : α → β} (hf : continuous_on f (closure s)) : f '' closure s = closure (f '' s) := subset.antisymm hf.image_closure $ closure_minimal (image_subset f subset_closure) (hs.image_of_continuous_on hf).is_closed /-- If a compact set is covered by two open sets, then we can cover it by two compact subsets. -/ lemma is_compact.binary_compact_cover [t2_space α] {K U V : set α} (hK : is_compact K) (hU : is_open U) (hV : is_open V) (h2K : K ⊆ U ∪ V) : ∃ K₁ K₂ : set α, is_compact K₁ ∧ is_compact K₂ ∧ K₁ ⊆ U ∧ K₂ ⊆ V ∧ K = K₁ ∪ K₂ := begin rcases compact_compact_separated (hK.diff hU) (hK.diff hV) (by rwa [diff_inter_diff, diff_eq_empty]) with ⟨O₁, O₂, h1O₁, h1O₂, h2O₁, h2O₂, hO⟩, refine ⟨_, _, hK.diff h1O₁, hK.diff h1O₂, by rwa [diff_subset_comm], by rwa [diff_subset_comm], by rw [← diff_inter, hO, diff_empty]⟩ end lemma continuous.is_closed_map [compact_space α] [t2_space β] {f : α → β} (h : continuous f) : is_closed_map f := λ s hs, (hs.is_compact.image h).is_closed lemma continuous.closed_embedding [compact_space α] [t2_space β] {f : α → β} (h : continuous f) (hf : function.injective f) : closed_embedding f := closed_embedding_of_continuous_injective_closed h hf h.is_closed_map section open finset function /-- For every finite open cover `Uᵢ` of a compact set, there exists a compact cover `Kᵢ ⊆ Uᵢ`. -/ lemma is_compact.finite_compact_cover [t2_space α] {s : set α} (hs : is_compact s) {ι} (t : finset ι) (U : ι → set α) (hU : ∀ i ∈ t, is_open (U i)) (hsC : s ⊆ ⋃ i ∈ t, U i) : ∃ K : ι → set α, (∀ i, is_compact (K i)) ∧ (∀i, K i ⊆ U i) ∧ s = ⋃ i ∈ t, K i := begin classical, induction t using finset.induction with x t hx ih generalizing U hU s hs hsC, { refine ⟨λ _, ∅, λ i, is_compact_empty, λ i, empty_subset _, _⟩, simpa only [subset_empty_iff, Union_false, Union_empty] using hsC }, simp only [finset.set_bUnion_insert] at hsC, simp only [finset.mem_insert] at hU, have hU' : ∀ i ∈ t, is_open (U i) := λ i hi, hU i (or.inr hi), rcases hs.binary_compact_cover (hU x (or.inl rfl)) (is_open_bUnion hU') hsC with ⟨K₁, K₂, h1K₁, h1K₂, h2K₁, h2K₂, hK⟩, rcases ih U hU' h1K₂ h2K₂ with ⟨K, h1K, h2K, h3K⟩, refine ⟨update K x K₁, _, _, _⟩, { intros i, by_cases hi : i = x, { simp only [update_same, hi, h1K₁] }, { rw [← ne.def] at hi, simp only [update_noteq hi, h1K] }}, { intros i, by_cases hi : i = x, { simp only [update_same, hi, h2K₁] }, { rw [← ne.def] at hi, simp only [update_noteq hi, h2K] }}, { simp only [set_bUnion_insert_update _ hx, hK, h3K] } end end lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ is_compact s) : locally_compact_space α := ⟨assume x n hn, let ⟨u, un, uo, xu⟩ := mem_nhds_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 is_compact_singleton (is_compact.diff kc uo) (by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in have wn : wᶜ ∈ 𝓝 x, from mem_nhds_iff.mpr ⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩, ⟨k \ w, filter.inter_mem kx wn, subset.trans (diff_subset_comm.mp kuw) un, kc.diff 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, is_open_univ.mem_nhds trivial, compact_univ⟩) /-- In a locally compact T₂ space, every point has an open neighborhood with compact closure -/ lemma exists_open_with_compact_closure [locally_compact_space α] [t2_space α] (x : α) : ∃ (U : set α), is_open U ∧ x ∈ U ∧ is_compact (closure U) := begin rcases exists_compact_mem_nhds x with ⟨K, hKc, hxK⟩, rcases mem_nhds_iff.1 hxK with ⟨t, h1t, h2t, h3t⟩, exact ⟨t, h2t, h3t, compact_closure_of_subset_compact hKc h1t⟩ end end separation section regularity /-- 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 t0_space α : Prop := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ 𝓝[t] a = ⊥) @[priority 100] -- see Note [lower instance priority] instance regular_space.t1_space [regular_space α] : t1_space α := begin rw t1_iff_exists_open, intros x y hxy, obtain ⟨U, hU, h⟩ := t0_space.t0 x y hxy, cases h, { exact ⟨U, hU, h⟩ }, { obtain ⟨R, hR, hh⟩ := regular_space.regular (is_closed_compl_iff.mpr hU) (not_not.mpr h.1), obtain ⟨V, hV, hhh⟩ := mem_nhds_iff.1 (filter.inf_principal_eq_bot.1 hh.2), exact ⟨R, hR, hh.1 (mem_compl h.2), hV hhh.2⟩ } end 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_iff.mp h in have ∃t, is_open t ∧ s'ᶜ ⊆ t ∧ 𝓝[t] a = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨tᶜ, mem_of_eq_bot $ by rwa [compl_compl], subset.trans (compl_subset_comm.1 ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ lemma closed_nhds_basis [regular_space α] (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_closed s) id := ⟨λ t, ⟨λ t_in, let ⟨s, s_in, h_st, h⟩ := nhds_is_closed t_in in ⟨s, ⟨s_in, h⟩, h_st⟩, λ ⟨s, ⟨s_in, hs⟩, hst⟩, mem_of_superset s_in hst⟩⟩ 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_within, nhds_induced, ← comap_principal, ← comap_inf, ← nhds_within, 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_mem_iff_bot.2 hxs, ⟨v, hvt, hv, hxv⟩ := mem_nhds_iff.1 hxt in ⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys, eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, by { rw htu, exact ⟨hvt hzv, hsu hzs⟩ }⟩⟩ @[priority 100] -- see Note [lower instance priority] instance regular_space.t2_5_space [regular_space α] : t2_5_space α := ⟨λ x y hxy, let ⟨U, V, hU, hV, hh_1, hh_2, hUV⟩ := t2_space.t2 x y hxy, hxcV := not_not.mpr ((interior_maximal (subset_compl_iff_disjoint.mpr hUV) hU) hh_1), ⟨R, hR, hh⟩ := regular_space.regular is_closed_closure (by rwa closure_eq_compl_interior_compl), ⟨A, hA, hhh⟩ := mem_nhds_iff.1 (filter.inf_principal_eq_bot.1 hh.2) in ⟨A, V, hhh.1, hV, subset_eq_empty ((closure V).inter_subset_inter_left (subset.trans (closure_minimal hA (is_closed_compl_iff.mpr hR)) (compl_subset_compl.mpr hh.1))) (compl_inter_self (closure V)), hhh.2, hh_2⟩⟩ variable {α} /-- Given two points `x ≠ y`, we can find neighbourhoods `x ∈ V₁ ⊆ U₁` and `y ∈ V₂ ⊆ U₂`, with the `Vₖ` closed and the `Uₖ` open, such that the `Uₖ` are disjoint. -/ lemma disjoint_nested_nhds [regular_space α] {x y : α} (h : x ≠ y) : ∃ (U₁ V₁ ∈ 𝓝 x) (U₂ V₂ ∈ 𝓝 y), is_closed V₁ ∧ is_closed V₂ ∧ is_open U₁ ∧ is_open U₂ ∧ V₁ ⊆ U₁ ∧ V₂ ⊆ U₂ ∧ U₁ ∩ U₂ = ∅ := begin rcases t2_separation h with ⟨U₁, U₂, U₁_op, U₂_op, x_in, y_in, H⟩, rcases nhds_is_closed (is_open.mem_nhds U₁_op x_in) with ⟨V₁, V₁_in, h₁, V₁_closed⟩, rcases nhds_is_closed (is_open.mem_nhds U₂_op y_in) with ⟨V₂, V₂_in, h₂, V₂_closed⟩, use [U₁, V₁, mem_of_superset V₁_in h₁, V₁_in, U₂, V₂, mem_of_superset V₂_in h₂, V₂_in], tauto end end regularity section normality /-- 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) 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 theorem normal_exists_closure_subset [normal_space α] {s t : set α} (hs : is_closed s) (ht : is_open t) (hst : s ⊆ t) : ∃ u, is_open u ∧ s ⊆ u ∧ closure u ⊆ t := begin have : disjoint s tᶜ, from λ x ⟨hxs, hxt⟩, hxt (hst hxs), rcases normal_separation hs (is_closed_compl_iff.2 ht) this with ⟨s', t', hs', ht', hss', htt', hs't'⟩, refine ⟨s', hs', hss', subset.trans (closure_minimal _ (is_closed_compl_iff.2 ht')) (compl_subset_comm.1 htt')⟩, exact λ x hxs hxt, hs't' ⟨hxs, hxt⟩ end @[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 hs is_closed_singleton (λ _ ⟨hx, hy⟩, hxs $ mem_of_eq_of_mem (eq_of_mem_singleton hy).symm hx) in ⟨u, hu, hsu, filter.empty_mem_iff_bot.1 $ filter.mem_inf_iff.2 ⟨v, is_open.mem_nhds hv (singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, by rwa [eq_comm, inter_comm, ← disjoint_iff_inter_eq_empty]⟩⟩ } -- 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.is_compact ht.is_compact st.eq_bot end end normality /-- In a compact t2 space, the connected component of a point equals the intersection of all its clopen neighbourhoods. -/ lemma connected_component_eq_Inter_clopen [t2_space α] [compact_space α] {x : α} : connected_component x = ⋂ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, Z := begin apply eq_of_subset_of_subset connected_component_subset_Inter_clopen, -- Reduce to showing that the clopen intersection is connected. refine is_preconnected.subset_connected_component _ (mem_Inter.2 (λ Z, Z.2.2)), -- We do this by showing that any disjoint cover by two closed sets implies -- that one of these closed sets must contain our whole thing. -- To reduce to the case where the cover is disjoint on all of `α` we need that `s` is closed have hs : @is_closed _ _inst_1 (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), Z) := is_closed_Inter (λ Z, Z.2.1.2), rw (is_preconnected_iff_subset_of_fully_disjoint_closed hs), intros a b ha hb hab ab_empty, haveI := @normal_of_compact_t2 α _ _ _, -- Since our space is normal, we get two larger disjoint open sets containing the disjoint -- closed sets. If we can show that our intersection is a subset of any of these we can then -- "descend" this to show that it is a subset of either a or b. rcases normal_separation ha hb (disjoint_iff.2 ab_empty) with ⟨u, v, hu, hv, hau, hbv, huv⟩, -- If we can find a clopen set around x, contained in u ∪ v, we get a disjoint decomposition -- Z = Z ∩ u ∪ Z ∩ v of clopen sets. The intersection of all clopen neighbourhoods will then lie -- in whichever of u or v x lies in and hence will be a subset of either a or b. suffices : ∃ (Z : set α), is_clopen Z ∧ x ∈ Z ∧ Z ⊆ u ∪ v, { cases this with Z H, rw [disjoint_iff_inter_eq_empty] at huv, have H1 := is_clopen_inter_of_disjoint_cover_clopen H.1 H.2.2 hu hv huv, rw [union_comm] at H, have H2 := is_clopen_inter_of_disjoint_cover_clopen H.1 H.2.2 hv hu (inter_comm u v ▸ huv), by_cases (x ∈ u), -- The x ∈ u case. { left, suffices : (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) ⊆ u, { rw ←set.disjoint_iff_inter_eq_empty at huv, replace hab : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ a ∪ b := hab, replace this : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ u := this, exact disjoint.left_le_of_le_sup_right hab (huv.mono this hbv) }, { apply subset.trans _ (inter_subset_right Z u), apply Inter_subset (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, ↑Z) ⟨Z ∩ u, H1, mem_inter H.2.1 h⟩ } }, -- If x ∉ u, we get x ∈ v since x ∈ u ∪ v. The rest is then like the x ∈ u case. have h1 : x ∈ v, { cases (mem_union x u v).1 (mem_of_subset_of_mem (subset.trans hab (union_subset_union hau hbv)) (mem_Inter.2 (λ i, i.2.2))) with h1 h1, { exfalso, exact h h1}, { exact h1} }, right, suffices : (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) ⊆ v, { rw [inter_comm, ←set.disjoint_iff_inter_eq_empty] at huv, replace hab : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ a ∪ b := hab, replace this : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ v := this, exact disjoint.left_le_of_le_sup_left hab (huv.mono this hau) }, { apply subset.trans _ (inter_subset_right Z v), apply Inter_subset (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, ↑Z) ⟨Z ∩ v, H2, mem_inter H.2.1 h1⟩ } }, -- Now we find the required Z. We utilize the fact that X \ u ∪ v will be compact, -- so there must be some finite intersection of clopen neighbourhoods of X disjoint to it, -- but a finite intersection of clopen sets is clopen so we let this be our Z. have H1 := ((is_closed_compl_iff.2 (hu.union hv)).is_compact.inter_Inter_nonempty (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, Z) (λ Z, Z.2.1.2)), rw [←not_imp_not, not_forall, not_nonempty_iff_eq_empty, inter_comm] at H1, have huv_union := subset.trans hab (union_subset_union hau hbv), rw [← compl_compl (u ∪ v), subset_compl_iff_disjoint] at huv_union, cases H1 huv_union with Zi H2, refine ⟨(⋂ (U ∈ Zi), subtype.val U), _, _, _⟩, { exact is_clopen_bInter (λ Z hZ, Z.2.1) }, { exact mem_bInter_iff.2 (λ Z hZ, Z.2.2) }, { rwa [not_nonempty_iff_eq_empty, inter_comm, ←subset_compl_iff_disjoint, compl_compl] at H2 } end section profinite open topological_space variables [t2_space α] /-- A Hausdorff space with a clopen basis is totally separated. -/ lemma tot_sep_of_zero_dim (h : is_topological_basis {s : set α | is_clopen s}) : totally_separated_space α := begin constructor, rintros x - y - hxy, obtain ⟨u, v, hu, hv, xu, yv, disj⟩ := t2_separation hxy, obtain ⟨w, hw : is_clopen w, xw, wu⟩ := (is_topological_basis.mem_nhds_iff h).1 (is_open.mem_nhds hu xu), refine ⟨w, wᶜ, hw.1, (is_clopen_compl_iff.2 hw).1, xw, _, _, set.inter_compl_self w⟩, { intro h, have : y ∈ u ∩ v := ⟨wu h, yv⟩, rwa disj at this }, rw set.union_compl_self, end variables [compact_space α] /-- A compact Hausdorff space is totally disconnected if and only if it is totally separated, this is also true for locally compact spaces. -/ theorem compact_t2_tot_disc_iff_tot_sep : totally_disconnected_space α ↔ totally_separated_space α := begin split, { intro h, constructor, rintros x - y -, contrapose!, intros hyp, suffices : x ∈ connected_component y, by simpa [totally_disconnected_space_iff_connected_component_singleton.1 h y, mem_singleton_iff], rw [connected_component_eq_Inter_clopen, mem_Inter], rintro ⟨w : set α, hw : is_clopen w, hy : y ∈ w⟩, by_contra hx, simpa using hyp wᶜ w (is_open_compl_iff.mpr hw.2) hw.1 hx hy }, apply totally_separated_space.totally_disconnected_space, end variables [totally_disconnected_space α] lemma nhds_basis_clopen (x : α) : (𝓝 x).has_basis (λ s : set α, x ∈ s ∧ is_clopen s) id := ⟨λ U, begin split, { have : connected_component x = {x}, from totally_disconnected_space_iff_connected_component_singleton.mp ‹_› x, rw connected_component_eq_Inter_clopen at this, intros hU, let N := {Z // is_clopen Z ∧ x ∈ Z}, suffices : ∃ Z : N, Z.val ⊆ U, { rcases this with ⟨⟨s, hs, hs'⟩, hs''⟩, exact ⟨s, ⟨hs', hs⟩, hs''⟩ }, haveI : nonempty N := ⟨⟨univ, is_clopen_univ, mem_univ x⟩⟩, have hNcl : ∀ Z : N, is_closed Z.val := (λ Z, Z.property.1.2), have hdir : directed superset (λ Z : N, Z.val), { rintros ⟨s, hs, hxs⟩ ⟨t, ht, hxt⟩, exact ⟨⟨s ∩ t, hs.inter ht, ⟨hxs, hxt⟩⟩, inter_subset_left s t, inter_subset_right s t⟩ }, have h_nhd: ∀ y ∈ (⋂ Z : N, Z.val), U ∈ 𝓝 y, { intros y y_in, erw [this, mem_singleton_iff] at y_in, rwa y_in }, exact exists_subset_nhd_of_compact_space hdir hNcl h_nhd }, { rintro ⟨V, ⟨hxV, V_op, -⟩, hUV : V ⊆ U⟩, rw mem_nhds_iff, exact ⟨V, hUV, V_op, hxV⟩ } end⟩ lemma is_topological_basis_clopen : is_topological_basis {s : set α | is_clopen s} := begin apply is_topological_basis_of_open_of_nhds (λ U (hU : is_clopen U), hU.1), intros x U hxU U_op, have : U ∈ 𝓝 x, from is_open.mem_nhds U_op hxU, rcases (nhds_basis_clopen x).mem_iff.mp this with ⟨V, ⟨hxV, hV⟩, hVU : V ⊆ U⟩, use V, tauto end /-- Every member of an open set in a compact Hausdorff totally disconnected space is contained in a clopen set contained in the open set. -/ lemma compact_exists_clopen_in_open {x : α} {U : set α} (is_open : is_open U) (memU : x ∈ U) : ∃ (V : set α) (hV : is_clopen V), x ∈ V ∧ V ⊆ U := (is_topological_basis.mem_nhds_iff is_topological_basis_clopen).1 (is_open.mem_nhds memU) end profinite section locally_compact open topological_space variables {H : Type*} [topological_space H] [locally_compact_space H] [t2_space H] /-- A locally compact Hausdorff totally disconnected space has a basis with clopen elements. -/ lemma loc_compact_Haus_tot_disc_of_zero_dim [totally_disconnected_space H] : is_topological_basis {s : set H | is_clopen s} := begin refine is_topological_basis_of_open_of_nhds (λ u hu, hu.1) _, rintros x U memU hU, obtain ⟨s, comp, xs, sU⟩ := exists_compact_subset hU memU, obtain ⟨t, h, ht, xt⟩ := mem_interior.1 xs, let u : set s := (coe : s → H)⁻¹' (interior s), have u_open_in_s : is_open u := is_open_interior.preimage continuous_subtype_coe, let X : s := ⟨x, h xt⟩, have Xu : X ∈ u := xs, haveI : compact_space s := is_compact_iff_compact_space.1 comp, obtain ⟨V : set s, clopen_in_s, Vx, V_sub⟩ := compact_exists_clopen_in_open u_open_in_s Xu, have V_clopen : is_clopen ((coe : s → H) '' V), { refine ⟨_, (comp.is_closed.closed_embedding_subtype_coe.closed_iff_image_closed).1 clopen_in_s.2⟩, let v : set u := (coe : u → s)⁻¹' V, have : (coe : u → H) = (coe : s → H) ∘ (coe : u → s) := rfl, have f0 : embedding (coe : u → H) := embedding_subtype_coe.comp embedding_subtype_coe, have f1 : open_embedding (coe : u → H), { refine ⟨f0, _⟩, { have : set.range (coe : u → H) = interior s, { rw [this, set.range_comp, subtype.range_coe, subtype.image_preimage_coe], apply set.inter_eq_self_of_subset_left interior_subset, }, rw this, apply is_open_interior } }, have f2 : is_open v := clopen_in_s.1.preimage continuous_subtype_coe, have f3 : (coe : s → H) '' V = (coe : u → H) '' v, { rw [this, image_comp coe coe, subtype.image_preimage_coe, inter_eq_self_of_subset_left V_sub] }, rw f3, apply f1.is_open_map v f2 }, refine ⟨coe '' V, V_clopen, by simp [Vx, h xt], _⟩, transitivity s, { simp }, assumption end /-- A locally compact Hausdorff space is totally disconnected if and only if it is totally separated. -/ theorem loc_compact_t2_tot_disc_iff_tot_sep : totally_disconnected_space H ↔ totally_separated_space H := begin split, { introI h, exact tot_sep_of_zero_dim loc_compact_Haus_tot_disc_of_zero_dim, }, apply totally_separated_space.totally_disconnected_space, end end locally_compact section connected_component_setoid local attribute [instance] connected_component_setoid /-- `connected_components α` is Hausdorff when `α` is Hausdorff and compact -/ instance connected_components.t2 [t2_space α] [compact_space α] : t2_space (connected_components α) := begin -- Proof follows that of: https://stacks.math.columbia.edu/tag/0900 -- Fix 2 distinct connected components, with points a and b refine ⟨λ x y, quotient.induction_on x (quotient.induction_on y (λ a b ne, _))⟩, rw connected_component_nrel_iff at ne, have h := connected_component_disjoint ne, -- write ⟦b⟧ as the intersection of all clopen subsets containing it rw [connected_component_eq_Inter_clopen, disjoint_iff_inter_eq_empty, inter_comm] at h, -- Now we show that this can be reduced to some clopen containing ⟦b⟧ being disjoint to ⟦a⟧ cases is_closed_connected_component.is_compact.elim_finite_subfamily_closed _ _ h with fin_a ha, swap, { exact λ Z, Z.2.1.2 }, set U : set α := (⋂ (i : {Z // is_clopen Z ∧ b ∈ Z}) (H : i ∈ fin_a), i) with hU, rw ←hU at ha, have hu_clopen : is_clopen U := is_clopen_bInter (λ i j, i.2.1), -- This clopen and its complement will separate the points corresponding to ⟦a⟧ and ⟦b⟧ use [quotient.mk '' U, quotient.mk '' Uᶜ], -- Using the fact that clopens are unions of connected components, we show that -- U and Uᶜ is the preimage of a clopen set in the quotient have hu : quotient.mk ⁻¹' (quotient.mk '' U) = U := (connected_components_preimage_image U ▸ eq.symm) hu_clopen.eq_union_connected_components, have huc : quotient.mk ⁻¹' (quotient.mk '' Uᶜ) = Uᶜ := (connected_components_preimage_image Uᶜ ▸ eq.symm) (is_clopen.compl hu_clopen).eq_union_connected_components, -- showing that U and Uᶜ are open and separates ⟦a⟧ and ⟦b⟧ refine ⟨_,_,_,_,_⟩, { rw [(quotient_map_iff.1 quotient_map_quotient_mk).2 _, hu], exact hu_clopen.1 }, { rw [(quotient_map_iff.1 quotient_map_quotient_mk).2 _, huc], exact is_open_compl_iff.2 hu_clopen.2 }, { exact mem_image_of_mem _ (mem_Inter.2 (λ Z, mem_Inter.2 (λ Zmem, Z.2.2))) }, { apply mem_image_of_mem, exact mem_of_subset_of_mem (subset_compl_iff_disjoint.2 ha) (@mem_connected_component _ _ a) }, apply preimage_injective.2 (@surjective_quotient_mk _ _), rw [preimage_inter, preimage_empty, hu, huc, inter_compl_self _], end end connected_component_setoid
14ee6145a470977cb3662df1682ce8be7ad204e5
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/order/zorn.lean
b1bc534584c57d7e7156a9c971b2a15f5922f455
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
11,973
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 Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import data.set.lattice order.order_iso noncomputable theory universes u open set classical local attribute [instance] prop_decidable namespace zorn section chain parameters {α : Type u} (r : α → α → Prop) local infix ` ≺ `:50 := r /-- A chain is a subset `c` satisfying `x ≺ y ∨ x = y ∨ y ≺ x` for all `x y ∈ c`. -/ def chain (c : set α) := pairwise_on c (λx y, x ≺ y ∨ y ≺ x) parameters {r} theorem chain.total_of_refl [is_refl α r] {c} (H : chain c) {x y} (hx : x ∈ c) (hy : y ∈ c) : x ≺ y ∨ y ≺ x := if e : x = y then or.inl (e ▸ refl _) else H _ hx _ hy e theorem chain.directed [is_refl α r] {c} (H : chain c) {x y} (hx : x ∈ c) (hy : y ∈ c) : ∃ z, z ∈ c ∧ x ≺ z ∧ y ≺ z := match H.total_of_refl hx hy with | or.inl h := ⟨y, hy, h, refl _⟩ | or.inr h := ⟨x, hx, refl _, h⟩ end theorem chain.mono {c c'} : c' ⊆ c → chain c → chain c' := pairwise_on.mono theorem chain.directed_on [is_refl α r] {c} (H : chain c) : directed_on (≺) c := λ x xc y yc, let ⟨z, hz, h⟩ := H.directed xc yc in ⟨z, hz, h⟩ theorem chain_insert {c : set α} {a : α} (hc : chain c) (ha : ∀b∈c, b ≠ a → a ≺ b ∨ b ≺ a) : chain (insert a c) := forall_insert_of_forall (assume x hx, forall_insert_of_forall (hc x hx) (assume hneq, (ha x hx hneq).symm)) (forall_insert_of_forall (assume x hx hneq, ha x hx $ assume h', hneq h'.symm) (assume h, (h rfl).rec _)) def super_chain (c₁ c₂ : set α) := chain c₂ ∧ c₁ ⊂ c₂ def is_max_chain (c : set α) := chain c ∧ ¬ (∃c', super_chain c c') def succ_chain (c : set α) := if h : ∃c', chain c ∧ super_chain c c' then some h else c theorem succ_spec {c : set α} (h : ∃c', chain c ∧ super_chain c c') : super_chain c (succ_chain c) := let ⟨c', hc'⟩ := h in have chain c ∧ super_chain c (some h), from @some_spec _ (λc', chain c ∧ super_chain c c') _, by simp [succ_chain, dif_pos, h, this.right] theorem chain_succ {c : set α} (hc : chain c) : chain (succ_chain c) := if h : ∃c', chain c ∧ super_chain c c' then (succ_spec h).left else by simp [succ_chain, dif_neg, h]; exact hc theorem super_of_not_max {c : set α} (hc₁ : chain c) (hc₂ : ¬ is_max_chain c) : super_chain c (succ_chain c) := begin simp [is_max_chain, not_and_distrib, not_forall_not] at hc₂, cases hc₂.neg_resolve_left hc₁ with c' hc', exact succ_spec ⟨c', hc₁, hc'⟩ end theorem succ_increasing {c : set α} : c ⊆ succ_chain c := if h : ∃c', chain c ∧ super_chain c c' then have super_chain c (succ_chain c), from succ_spec h, this.right.left else by simp [succ_chain, dif_neg, h, subset.refl] inductive chain_closure : set α → Prop | succ : ∀{s}, chain_closure s → chain_closure (succ_chain s) | union : ∀{s}, (∀a∈s, chain_closure a) → chain_closure (⋃₀ s) theorem chain_closure_empty : chain_closure ∅ := have chain_closure (⋃₀ ∅), from chain_closure.union $ assume a h, h.rec _, by simp at this; assumption theorem chain_closure_closure : chain_closure (⋃₀ chain_closure) := chain_closure.union $ assume s hs, hs variables {c c₁ c₂ c₃ : set α} private lemma chain_closure_succ_total_aux (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : ∀{c₃}, chain_closure c₃ → c₃ ⊆ c₂ → c₂ = c₃ ∨ succ_chain c₃ ⊆ c₂) : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁ := begin induction hc₁, case _root_.zorn.chain_closure.succ : c₃ hc₃ ih { cases ih with ih ih, { have h := h hc₃ ih, cases h with h h, { exact or.inr (h ▸ subset.refl _) }, { exact or.inl h } }, { exact or.inr (subset.trans ih succ_increasing) } }, case _root_.zorn.chain_closure.union : s hs ih { refine (classical.or_iff_not_imp_right.2 $ λ hn, sUnion_subset $ λ a ha, _), apply (ih a ha).resolve_right, apply mt (λ h, _) hn, exact subset.trans h (subset_sUnion_of_mem ha) } end private lemma chain_closure_succ_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : c₁ ⊆ c₂) : c₂ = c₁ ∨ succ_chain c₁ ⊆ c₂ := begin induction hc₂ generalizing c₁ hc₁ h, case _root_.zorn.chain_closure.succ : c₂ hc₂ ih { have h₁ : c₁ ⊆ c₂ ∨ @succ_chain α r c₂ ⊆ c₁ := (chain_closure_succ_total_aux hc₁ hc₂ $ assume c₁, ih), cases h₁ with h₁ h₁, { have h₂ := ih hc₁ h₁, cases h₂ with h₂ h₂, { exact (or.inr $ h₂ ▸ subset.refl _) }, { exact (or.inr $ subset.trans h₂ succ_increasing) } }, { exact (or.inl $ subset.antisymm h₁ h) } }, case _root_.zorn.chain_closure.union : s hs ih { apply or.imp_left (assume h', subset.antisymm h' h), apply classical.by_contradiction, simp [not_or_distrib, sUnion_subset_iff, classical.not_forall], intros c₃ hc₃ h₁ h₂, have h := chain_closure_succ_total_aux hc₁ (hs c₃ hc₃) (assume c₄, ih _ hc₃), cases h with h h, { have h' := ih c₃ hc₃ hc₁ h, cases h' with h' h', { exact (h₁ $ h' ▸ subset.refl _) }, { exact (h₂ $ subset.trans h' $ subset_sUnion_of_mem hc₃) } }, { exact (h₁ $ subset.trans succ_increasing h) } } end theorem chain_closure_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := have c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁, from chain_closure_succ_total_aux hc₁ hc₂ $ assume c₃ hc₃, chain_closure_succ_total hc₃ hc₂, or.imp_right (assume : succ_chain c₂ ⊆ c₁, subset.trans succ_increasing this) this theorem chain_closure_succ_fixpoint (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := begin induction hc₁, case _root_.zorn.chain_closure.succ : c₁ hc₁ h { exact or.elim (chain_closure_succ_total hc₁ hc₂ h) (assume h, h ▸ h_eq.symm ▸ subset.refl c₂) id }, case _root_.zorn.chain_closure.union : s hs ih { exact (sUnion_subset $ assume c₁ hc₁, ih c₁ hc₁) } end theorem chain_closure_succ_fixpoint_iff (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀ chain_closure := ⟨assume h, subset.antisymm (subset_sUnion_of_mem hc) (chain_closure_succ_fixpoint chain_closure_closure hc h), assume : c = ⋃₀{c : set α | chain_closure c}, subset.antisymm (calc succ_chain c ⊆ ⋃₀{c : set α | chain_closure c} : subset_sUnion_of_mem $ chain_closure.succ hc ... = c : this.symm) succ_increasing⟩ theorem chain_chain_closure (hc : chain_closure c) : chain c := begin induction hc, case _root_.zorn.chain_closure.succ : c hc h { exact chain_succ h }, case _root_.zorn.chain_closure.union : s hs h { have h : ∀c∈s, zorn.chain c := h, exact assume c₁ ⟨t₁, ht₁, (hc₁ : c₁ ∈ t₁)⟩ c₂ ⟨t₂, ht₂, (hc₂ : c₂ ∈ t₂)⟩ hneq, have t₁ ⊆ t₂ ∨ t₂ ⊆ t₁, from chain_closure_total (hs _ ht₁) (hs _ ht₂), or.elim this (assume : t₁ ⊆ t₂, h t₂ ht₂ c₁ (this hc₁) c₂ hc₂ hneq) (assume : t₂ ⊆ t₁, h t₁ ht₁ c₁ hc₁ c₂ (this hc₂) hneq) } end def max_chain := ⋃₀ chain_closure /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ theorem max_chain_spec : is_max_chain max_chain := classical.by_contradiction $ assume : ¬ is_max_chain (⋃₀ chain_closure), have super_chain (⋃₀ chain_closure) (succ_chain (⋃₀ chain_closure)), from super_of_not_max (chain_chain_closure chain_closure_closure) this, let ⟨h₁, h₂, (h₃ : (⋃₀ chain_closure) ≠ succ_chain (⋃₀ chain_closure))⟩ := this in have succ_chain (⋃₀ chain_closure) = (⋃₀ chain_closure), from (chain_closure_succ_fixpoint_iff chain_closure_closure).mpr rfl, h₃ this.symm /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ theorem zorn (h : ∀c, chain c → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := have ∃ub, ∀a∈max_chain, a ≺ ub, from h _ $ max_chain_spec.left, let ⟨ub, (hub : ∀a∈max_chain, a ≺ ub)⟩ := this in ⟨ub, assume a ha, have chain (insert a max_chain), from chain_insert max_chain_spec.left $ assume b hb _, or.inr $ trans (hub b hb) ha, have a ∈ max_chain, from classical.by_contradiction $ assume h : a ∉ max_chain, max_chain_spec.right $ ⟨insert a max_chain, this, ssubset_insert h⟩, hub a this⟩ end chain theorem zorn_partial_order {α : Type u} [partial_order α] (h : ∀c:set α, @chain α (≤) c → ∃ub, ∀a∈c, a ≤ ub) : ∃m:α, ∀a, m ≤ a → a = m := let ⟨m, hm⟩ := @zorn α (≤) h (assume a b c, le_trans) in ⟨m, assume a ha, le_antisymm (hm a ha) ha⟩ theorem zorn_partial_order₀ {α : Type u} [partial_order α] (s : set α) (ih : ∀ c ⊆ s, chain (≤) c → ∀ y ∈ c, ∃ ub ∈ s, ∀ z ∈ c, z ≤ ub) (x : α) (hxs : x ∈ s) : ∃ m ∈ s, x ≤ m ∧ ∀ z ∈ s, m ≤ z → z = m := let ⟨⟨m, hms, hxm⟩, h⟩ := @zorn_partial_order {m // m ∈ s ∧ x ≤ m} _ (λ c hc, classical.by_cases (assume hce : c = ∅, hce.symm ▸ ⟨⟨x, hxs, le_refl _⟩, λ _, false.elim⟩) (assume hce : c ≠ ∅, let ⟨m, hmc⟩ := set.exists_mem_of_ne_empty hce in let ⟨ub, hubs, hub⟩ := ih (subtype.val '' c) (image_subset_iff.2 $ λ z hzc, z.2.1) (by rintro _ ⟨p, hpc, rfl⟩ _ ⟨q, hqc, rfl⟩ hpq; exact hc p hpc q hqc (mt (by rintro rfl; refl) hpq)) m.1 (mem_image_of_mem _ hmc) in ⟨⟨ub, hubs, le_trans m.2.2 $ hub m.1 $ mem_image_of_mem _ hmc⟩, λ a hac, hub a.1 ⟨a, hac, rfl⟩⟩)) in ⟨m, hms, hxm, λ z hzs hmz, congr_arg subtype.val $ h ⟨z, hzs, le_trans hxm hmz⟩ hmz⟩ theorem zorn_subset {α : Type u} (S : set (set α)) (h : ∀c ⊆ S, chain (⊆) c → ∃ub ∈ S, ∀ s ∈ c, s ⊆ ub) : ∃ m ∈ S, ∀a ∈ S, m ⊆ a → a = m := begin letI : partial_order S := partial_order.lift subtype.val (λ _ _, subtype.eq'), have : ∀c:set S, @chain S (≤) c → ∃ub, ∀a∈c, a ≤ ub, { intros c hc, rcases h (subtype.val '' c) (image_subset_iff.2 _) _ with ⟨s, sS, hs⟩, { exact ⟨⟨s, sS⟩, λ ⟨x, hx⟩ H, hs _ (mem_image_of_mem _ H)⟩ }, { rintro ⟨x, hx⟩ _, exact hx }, { rintro _ ⟨x, cx, rfl⟩ _ ⟨y, cy, rfl⟩ xy, exact hc x cx y cy (mt (congr_arg _) xy) } }, rcases zorn_partial_order this with ⟨⟨m, mS⟩, hm⟩, exact ⟨m, mS, λ a aS ha, congr_arg subtype.val (hm ⟨a, aS⟩ ha)⟩ end theorem zorn_subset₀ {α : Type u} (S : set (set α)) (H : ∀c ⊆ S, chain (⊆) c → c ≠ ∅ → ∃ub ∈ S, ∀ s ∈ c, s ⊆ ub) (x) (hx : x ∈ S) : ∃ m ∈ S, x ⊆ m ∧ ∀a ∈ S, m ⊆ a → a = m := begin let T := {s ∈ S | x ⊆ s}, rcases zorn_subset T _ with ⟨m, ⟨mS, mx⟩, hm⟩, { exact ⟨m, mS, mx, λ a ha ha', hm a ⟨ha, subset.trans mx ha'⟩ ha'⟩ }, { intros c cT hc, by_cases c0 : c = ∅, { rw c0, exact ⟨x, ⟨hx, subset.refl _⟩, λ _, false.elim⟩ }, { rcases H _ (subset.trans cT (sep_subset _ _)) hc c0 with ⟨ub, us, h⟩, refine ⟨ub, ⟨us, _⟩, h⟩, rcases ne_empty_iff_exists_mem.1 c0 with ⟨s, hs⟩, exact subset.trans (cT hs).2 (h _ hs) } } end theorem chain.total {α : Type u} [preorder α] {c} (H : @chain α (≤) c) : ∀ {x y}, x ∈ c → y ∈ c → x ≤ y ∨ y ≤ x := @chain.total_of_refl _ (≤) ⟨le_refl⟩ _ H end zorn
0dcbd4d0332ba05baf50945d540470c60149a2b0
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/tactic/push_neg.lean
79f57c6c6113d5d18820b4044f0ce11636ff1698
[ "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
7,986
lean
/- Copyright (c) 2019 Patrick Massot All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Simon Hudon A tactic pushing negations into an expression -/ import logic.basic import algebra.order open tactic expr namespace push_neg section universe u variable {α : Sort u} variables (p q : Prop) variable (s : α → Prop) local attribute [instance, priority 10] classical.prop_decidable theorem not_not_eq : (¬ ¬ p) = p := propext not_not theorem not_and_eq : (¬ (p ∧ q)) = (p → ¬ q) := propext not_and theorem not_or_eq : (¬ (p ∨ q)) = (¬ p ∧ ¬ q) := propext not_or_distrib theorem not_forall_eq : (¬ ∀ x, s x) = (∃ x, ¬ s x) := propext not_forall theorem not_exists_eq : (¬ ∃ x, s x) = (∀ x, ¬ s x) := propext not_exists theorem not_implies_eq : (¬ (p → q)) = (p ∧ ¬ q) := propext not_imp theorem classical.implies_iff_not_or : (p → q) ↔ (¬ p ∨ q) := imp_iff_not_or theorem not_eq (a b : α) : (¬ a = b) ↔ (a ≠ b) := iff.rfl variable {β : Type u} variable [linear_order β] theorem not_le_eq (a b : β) : (¬ (a ≤ b)) = (b < a) := propext not_le theorem not_lt_eq (a b : β) : (¬ (a < b)) = (b ≤ a) := propext not_lt end meta def whnf_reducible (e : expr) : tactic expr := whnf e reducible private meta def transform_negation_step (e : expr) : tactic (option (expr × expr)) := do e ← whnf_reducible e, match e with | `(¬ %%ne) := (do ne ← whnf_reducible ne, match ne with | `(¬ %%a) := do pr ← mk_app ``not_not_eq [a], return (some (a, pr)) | `(%%a ∧ %%b) := do pr ← mk_app ``not_and_eq [a, b], return (some (`((%%a : Prop) → ¬ %%b), pr)) | `(%%a ∨ %%b) := do pr ← mk_app ``not_or_eq [a, b], return (some (`(¬ %%a ∧ ¬ %%b), pr)) | `(%%a ≤ %%b) := do e ← to_expr ``(%%b < %%a), pr ← mk_app ``not_le_eq [a, b], return (some (e, pr)) | `(%%a < %%b) := do e ← to_expr ``(%%b ≤ %%a), pr ← mk_app ``not_lt_eq [a, b], return (some (e, pr)) | `(Exists %%p) := do pr ← mk_app ``not_exists_eq [p], e ← match p with | (lam n bi typ bo) := do body ← mk_app ``not [bo], return (pi n bi typ body) | _ := tactic.fail "Unexpected failure negating ∃" end, return (some (e, pr)) | (pi n bi d p) := if p.has_var then do pr ← mk_app ``not_forall_eq [lam n bi d p], body ← mk_app ``not [p], e ← mk_app ``Exists [lam n bi d body], return (some (e, pr)) else do pr ← mk_app ``not_implies_eq [d, p], `(%%_ = %%e') ← infer_type pr, return (some (e', pr)) | _ := return none end) | _ := return none end private meta def transform_negation : expr → tactic (option (expr × expr)) | e := do (some (e', pr)) ← transform_negation_step e | return none, (some (e'', pr')) ← transform_negation e' | return (some (e', pr)), pr'' ← mk_eq_trans pr pr', return (some (e'', pr'')) meta def normalize_negations (t : expr) : tactic (expr × expr) := do (_, e, pr) ← simplify_top_down () (λ _, λ e, do oepr ← transform_negation e, match oepr with | (some (e', pr)) := return ((), e', pr) | none := do pr ← mk_eq_refl e, return ((), e, pr) end) t { eta := ff }, return (e, pr) meta def push_neg_at_hyp (h : name) : tactic unit := do H ← get_local h, t ← infer_type H, (e, pr) ← normalize_negations t, replace_hyp H e pr, skip meta def push_neg_at_goal : tactic unit := do H ← target, (e, pr) ← normalize_negations H, replace_target e pr end push_neg open interactive (parse loc.ns loc.wildcard) open interactive.types (location texpr) open lean.parser (tk ident many) interactive.loc local postfix `?`:9001 := optional local postfix *:9001 := many open push_neg /-- Push negations in the goal of some assumption. For instance, a hypothesis `h : ¬ ∀ x, ∃ y, x ≤ y` will be transformed by `push_neg at h` into `h : ∃ x, ∀ y, y < x`. Variables names are conserved. This tactic pushes negations inside expressions. For instance, given an assumption ```lean h : ¬ ∀ ε > 0, ∃ δ > 0, ∀ x, |x - x₀| ≤ δ → |f x - y₀| ≤ ε) ``` writing `push_neg at h` will turn `h` into ```lean h : ∃ ε, ε > 0 ∧ ∀ δ, δ > 0 → (∃ x, |x - x₀| ≤ δ ∧ ε < |f x - y₀|), ``` (the pretty printer does *not* use the abreviations `∀ δ > 0` and `∃ ε > 0` but this issue has nothing to do with `push_neg`). Note that names are conserved by this tactic, contrary to what would happen with `simp` using the relevant lemmas. One can also use this tactic at the goal using `push_neg`, at every assumption and the goal using `push_neg at *` or at selected assumptions and the goal using say `push_neg at h h' ⊢` as usual. -/ meta def tactic.interactive.push_neg : parse location → tactic unit | (loc.ns loc_l) := loc_l.mmap' (λ l, match l with | some h := do push_neg_at_hyp h, try $ interactive.simp_core { eta := ff } failed tt [simp_arg_type.expr ``(push_neg.not_eq)] [] (interactive.loc.ns [some h]) | none := do push_neg_at_goal, try `[simp only [push_neg.not_eq] { eta := ff }] end) | loc.wildcard := do push_neg_at_goal, local_context >>= mmap' (λ h, push_neg_at_hyp (local_pp_name h)) , try `[simp only [push_neg.not_eq] at * { eta := ff }] add_tactic_doc { name := "push_neg", category := doc_category.tactic, decl_names := [`tactic.interactive.push_neg], tags := ["logic"] } lemma imp_of_not_imp_not (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := λ h hP, classical.by_contradiction (λ h', h h' hP) /-- Matches either an identifier "h" or a pair of identifiers "h with k" -/ meta def name_with_opt : lean.parser (name × option name) := prod.mk <$> ident <*> (some <$> (tk "with" >> ident) <|> return none) /-- Transforms the goal into its contrapositive. * `contrapose` turns a goal `P → Q` into `¬ Q → ¬ P` * `contrapose!` turns a goal `P → Q` into `¬ Q → ¬ P` and pushes negations inside `P` and `Q` using `push_neg` * `contrapose h` first reverts the local assumption `h`, and then uses `contrapose` and `intro h` * `contrapose! h` first reverts the local assumption `h`, and then uses `contrapose!` and `intro h` * `contrapose h with new_h` uses the name `new_h` for the introduced hypothesis -/ meta def tactic.interactive.contrapose (push : parse (tk "!" )?) : parse name_with_opt? → tactic unit | (some (h, h')) := get_local h >>= revert >> tactic.interactive.contrapose none >> intro (h'.get_or_else h) >> skip | none := do `(%%P → %%Q) ← target | fail "The goal is not an implication, and you didn't specify an assumption", cp ← mk_mapp ``imp_of_not_imp_not [P, Q] <|> fail "contrapose only applies to nondependent arrows between props", apply cp, when push.is_some $ try (tactic.interactive.push_neg (loc.ns [none])) add_tactic_doc { name := "contrapose", category := doc_category.tactic, decl_names := [`tactic.interactive.contrapose], tags := ["logic"] }
198aca80d32da667942b174adff226a4af6ba810
36cfb52a5926b96dc8a44d3b71d2f14a21ef8574
/sort_benchmarks.lean
e3ce59d8011917f662f0d2fbfe7230edd05b7cb8
[]
no_license
minchaowu/relevance_filter
b46e3cc166b8225b19fac61b8f9911eeecdd42d8
3ae59d297ed5a07bd14749112e520b74209f10fd
refs/heads/master
1,631,071,413,821
1,507,745,131,000
1,507,745,131,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,125
lean
import float sort data.list.sort set_option profiler true --set_option trace.array.update true /- all updates are destructive. I don't know why my array quicksort takes twice the time of list.qsort. They seem to scale at the same rate. -/ run_cmd --let arr := (mk_array 80 (1 : float)).map (λ _, float.random) in do return $ quicksort (λ a b, float.lt a b) $ (mk_array 8000 (1 : float)).map (λ _, float.random) -- return $ list.qsort (λ a b, float.lt a b) ((mk_array 8000 (1 : float)).map (λ _, float.random)).to_list #exit run_cmd let a := (mk_array 800 (1 : float)).map (λ _, float.random) -- .55 in tactic.trace $ ((a.write' 10 50).read' 10) + (a.read' 1) #exit run_cmd return $ merge_sort $ (mk_array 20000 (1 : float)).map (λ _, float.random) -- 4 sec run_cmd return $ quicksort (λ a b, a < b) $ (mk_array 20000 (1 : float)).map (λ _, float.random) -- .55 run_cmd return $ list.merge_sort (λ a b, a < b) ((list.repeat (0 : ℕ) 20000).map (λ _, float.random)) -- .24 run_cmd return $ list.qsort (λ a b, a < b) ((list.repeat (0 : ℕ) 20000).map (λ _, float.random)) -- .31
237351abb311b66264f46a86b82341495c4873a2
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/statement.lean
2b221858e0da321c5b28077e5abe6a2d5b4deeb6
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
746
lean
import pseudo_normed_group.system_of_complexes import Mbar.pseudo_normed_group import breen_deligne.homotopy . open_locale nnreal open category_theory ProFiltPseuNormGrpWithTinv opposite variables (r r' : ℝ≥0) [fact (0 < r)] [fact (0 < r')] [fact (r < r')] [fact (r < 1)] [fact (r' < 1)] variables (BD : breen_deligne.package) (κ : ℕ → ℝ≥0) variables [BD.data.very_suitable r r' κ] [∀ (i : ℕ), fact (0 < κ i)] include r r' BD κ def first_target_stmt : Prop := ∀ m : ℕ, ∃ (k K : ℝ≥0) [fact (1 ≤ k)], ∃ c₀ : ℝ≥0, ∀ (S : Type) [fintype S], ∀ (V : SemiNormedGroup.{0}) [normed_with_aut r V], ​((BD.data.system κ r V r').obj (op $ of r' (Mbar r' S))).is_weak_bounded_exact k K m c₀
5b06bd476218b9e0c4eda5e14e56249c6ce55fc7
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Data/SMap.lean
b92608979bbeda0b3e4da7db16bdf58d2f52af5c
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
3,709
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 Std.Data.HashMap import Std.Data.PersistentHashMap universe u v w w' namespace Lean open Std (HashMap PHashMap) /- Staged map for implementing the Environment. The idea is to store imported entries into a hashtable and local entries into a persistent hashtable. Hypotheses: - The number of entries (i.e., declarations) coming from imported files is much bigger than the number of entries in the current file. - HashMap is faster than PersistentHashMap. - When we are reading imported files, we have exclusive access to the map, and efficient destructive updates are performed. Remarks: - We never remove declarations from the Environment. In principle, we could support deletion by using `(PHashMap α (Option β))` where the value `none` would indicate that an entry was "removed" from the hashtable. - We do not need additional bookkeeping for extracting the local entries. -/ structure SMap (α : Type u) (β : Type v) [BEq α] [Hashable α] where stage₁ : Bool := true map₁ : HashMap α β := {} map₂ : PHashMap α β := {} namespace SMap variable {α : Type u} {β : Type v} [BEq α] [Hashable α] instance : Inhabited (SMap α β) := ⟨{}⟩ def empty : SMap α β := {} @[specialize] def insert : SMap α β → α → β → SMap α β | ⟨true, m₁, m₂⟩, k, v => ⟨true, m₁.insert k v, m₂⟩ | ⟨false, m₁, m₂⟩, k, v => ⟨false, m₁, m₂.insert k v⟩ @[specialize] def find? : SMap α β → α → Option β | ⟨true, m₁, _⟩, k => m₁.find? k | ⟨false, m₁, m₂⟩, k => (m₂.find? k).orElse (m₁.find? k) @[inline] def findD (m : SMap α β) (a : α) (b₀ : β) : β := (m.find? a).getD b₀ @[inline] def find! [Inhabited β] (m : SMap α β) (a : α) : β := match m.find? a with | some b => b | none => panic! "key is not in the map" @[specialize] def contains : SMap α β → α → Bool | ⟨true, m₁, _⟩, k => m₁.contains k | ⟨false, m₁, m₂⟩, k => m₁.contains k || m₂.contains k /- Similar to `find?`, but searches for result in the hashmap first. So, the result is correct only if we never "overwrite" `map₁` entries using `map₂`. -/ @[specialize] def find?' : SMap α β → α → Option β | ⟨true, m₁, _⟩, k => m₁.find? k | ⟨false, m₁, m₂⟩, k => (m₁.find? k).orElse (m₂.find? k) def forM [Monad m] (s : SMap α β) (f : α → β → m PUnit) : m PUnit := do s.map₁.forM f s.map₂.forM f /- Move from stage 1 into stage 2. -/ def switch (m : SMap α β) : SMap α β := if m.stage₁ then { m with stage₁ := false } else m @[inline] def foldStage2 {σ : Type w} (f : σ → α → β → σ) (s : σ) (m : SMap α β) : σ := m.map₂.foldl f s def fold {σ : Type w} (f : σ → α → β → σ) (init : σ) (m : SMap α β) : σ := m.map₂.foldl f $ m.map₁.fold f init def size (m : SMap α β) : Nat := m.map₁.size + m.map₂.size def stageSizes (m : SMap α β) : Nat × Nat := (m.map₁.size, m.map₂.size) def numBuckets (m : SMap α β) : Nat := m.map₁.numBuckets def toList (m : SMap α β) : List (α × β) := m.fold (init := []) fun es a b => (a, b)::es end SMap def List.toSMap [BEq α] [Hashable α] (es : List (α × β)) : SMap α β := es.foldl (init := {}) fun s (a, b) => s.insert a b instance {_ : BEq α} {_ : Hashable α} [Repr α] [Repr β] : Repr (SMap α β) where reprPrec v prec := Repr.addAppParen (reprArg v.toList ++ ".toSMap") prec end Lean
eb9842866f16cb21868e7ad64e415c912491dfd0
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/computability/turing_machine.lean
18da3edca369aef4a2d4006f89a7c4e21dfc6dda
[ "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
109,924
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.order import data.fintype.basic import data.pfun import tactic.apply_fun import logic.function.iterate /-! # Turing machines This file defines a sequence of simple machine languages, starting with Turing machines and working up to more complex languages based on Wang B-machines. ## Naming conventions Each model of computation in this file shares a naming convention for the elements of a model of computation. These are the parameters for the language: * `Γ` is the alphabet on the tape. * `Λ` is the set of labels, or internal machine states. * `σ` is the type of internal memory, not on the tape. This does not exist in the TM0 model, and later models achieve this by mixing it into `Λ`. * `K` is used in the TM2 model, which has multiple stacks, and denotes the number of such stacks. All of these variables denote "essentially finite" types, but for technical reasons it is convenient to allow them to be infinite anyway. When using an infinite type, we will be interested to prove that only finitely many values of the type are ever interacted with. Given these parameters, there are a few common structures for the model that arise: * `stmt` is the set of all actions that can be performed in one step. For the TM0 model this set is finite, and for later models it is an infinite inductive type representing "possible program texts". * `cfg` is the set of instantaneous configurations, that is, the state of the machine together with its environment. * `machine` is the set of all machines in the model. Usually this is approximately a function `Λ → stmt`, although different models have different ways of halting and other actions. * `step : cfg → option cfg` is the function that describes how the state evolves over one step. If `step c = none`, then `c` is a terminal state, and the result of the computation is read off from `c`. Because of the type of `step`, these models are all deterministic by construction. * `init : input → cfg` sets up the initial state. The type `input` depends on the model; in most cases it is `list Γ`. * `eval : machine → input → part output`, given a machine `M` and input `i`, starts from `init i`, runs `step` until it reaches an output, and then applies a function `cfg → output` to the final state to obtain the result. The type `output` depends on the model. * `supports : machine → finset Λ → Prop` asserts that a machine `M` starts in `S : finset Λ`, and can only ever jump to other states inside `S`. This implies that the behavior of `M` on any input cannot depend on its values outside `S`. We use this to allow `Λ` to be an infinite set when convenient, and prove that only finitely many of these states are actually accessible. This formalizes "essentially finite" mentioned above. -/ open relation open nat (iterate) open function (update iterate_succ iterate_succ_apply iterate_succ' iterate_succ_apply' iterate_zero_apply) namespace turing /-- The `blank_extends` partial order holds of `l₁` and `l₂` if `l₂` is obtained by adding blanks (`default Γ`) to the end of `l₁`. -/ def blank_extends {Γ} [inhabited Γ] (l₁ l₂ : list Γ) : Prop := ∃ n, l₂ = l₁ ++ list.repeat (default Γ) n @[refl] theorem blank_extends.refl {Γ} [inhabited Γ] (l : list Γ) : blank_extends l l := ⟨0, by simp⟩ @[trans] theorem blank_extends.trans {Γ} [inhabited Γ] {l₁ l₂ l₃ : list Γ} : blank_extends l₁ l₂ → blank_extends l₂ l₃ → blank_extends l₁ l₃ := by rintro ⟨i, rfl⟩ ⟨j, rfl⟩; exact ⟨i+j, by simp [list.repeat_add]⟩ theorem blank_extends.below_of_le {Γ} [inhabited Γ] {l l₁ l₂ : list Γ} : blank_extends l l₁ → blank_extends l l₂ → l₁.length ≤ l₂.length → blank_extends l₁ l₂ := begin rintro ⟨i, rfl⟩ ⟨j, rfl⟩ h, use j - i, simp only [list.length_append, add_le_add_iff_left, list.length_repeat] at h, simp only [← list.repeat_add, nat.add_sub_cancel' h, list.append_assoc], end /-- Any two extensions by blank `l₁,l₂` of `l` have a common join (which can be taken to be the longer of `l₁` and `l₂`). -/ def blank_extends.above {Γ} [inhabited Γ] {l l₁ l₂ : list Γ} (h₁ : blank_extends l l₁) (h₂ : blank_extends l l₂) : {l' // blank_extends l₁ l' ∧ blank_extends l₂ l'} := if h : l₁.length ≤ l₂.length then ⟨l₂, h₁.below_of_le h₂ h, blank_extends.refl _⟩ else ⟨l₁, blank_extends.refl _, h₂.below_of_le h₁ (le_of_not_ge h)⟩ theorem blank_extends.above_of_le {Γ} [inhabited Γ] {l l₁ l₂ : list Γ} : blank_extends l₁ l → blank_extends l₂ l → l₁.length ≤ l₂.length → blank_extends l₁ l₂ := begin rintro ⟨i, rfl⟩ ⟨j, e⟩ h, use i - j, refine list.append_right_cancel (e.symm.trans _), rw [list.append_assoc, ← list.repeat_add, nat.sub_add_cancel], apply_fun list.length at e, simp only [list.length_append, list.length_repeat] at e, rwa [← add_le_add_iff_left, e, add_le_add_iff_right] end /-- `blank_rel` is the symmetric closure of `blank_extends`, turning it into an equivalence relation. Two lists are related by `blank_rel` if one extends the other by blanks. -/ def blank_rel {Γ} [inhabited Γ] (l₁ l₂ : list Γ) : Prop := blank_extends l₁ l₂ ∨ blank_extends l₂ l₁ @[refl] theorem blank_rel.refl {Γ} [inhabited Γ] (l : list Γ) : blank_rel l l := or.inl (blank_extends.refl _) @[symm] theorem blank_rel.symm {Γ} [inhabited Γ] {l₁ l₂ : list Γ} : blank_rel l₁ l₂ → blank_rel l₂ l₁ := or.symm @[trans] theorem blank_rel.trans {Γ} [inhabited Γ] {l₁ l₂ l₃ : list Γ} : blank_rel l₁ l₂ → blank_rel l₂ l₃ → blank_rel l₁ l₃ := begin rintro (h₁|h₁) (h₂|h₂), { exact or.inl (h₁.trans h₂) }, { cases le_total l₁.length l₃.length with h h, { exact or.inl (h₁.above_of_le h₂ h) }, { exact or.inr (h₂.above_of_le h₁ h) } }, { cases le_total l₁.length l₃.length with h h, { exact or.inl (h₁.below_of_le h₂ h) }, { exact or.inr (h₂.below_of_le h₁ h) } }, { exact or.inr (h₂.trans h₁) }, end /-- Given two `blank_rel` lists, there exists (constructively) a common join. -/ def blank_rel.above {Γ} [inhabited Γ] {l₁ l₂ : list Γ} (h : blank_rel l₁ l₂) : {l // blank_extends l₁ l ∧ blank_extends l₂ l} := begin refine if hl : l₁.length ≤ l₂.length then ⟨l₂, or.elim h id (λ h', _), blank_extends.refl _⟩ else ⟨l₁, blank_extends.refl _, or.elim h (λ h', _) id⟩, exact (blank_extends.refl _).above_of_le h' hl, exact (blank_extends.refl _).above_of_le h' (le_of_not_ge hl) end /-- Given two `blank_rel` lists, there exists (constructively) a common meet. -/ def blank_rel.below {Γ} [inhabited Γ] {l₁ l₂ : list Γ} (h : blank_rel l₁ l₂) : {l // blank_extends l l₁ ∧ blank_extends l l₂} := begin refine if hl : l₁.length ≤ l₂.length then ⟨l₁, blank_extends.refl _, or.elim h id (λ h', _)⟩ else ⟨l₂, or.elim h (λ h', _) id, blank_extends.refl _⟩, exact (blank_extends.refl _).above_of_le h' hl, exact (blank_extends.refl _).above_of_le h' (le_of_not_ge hl) end theorem blank_rel.equivalence (Γ) [inhabited Γ] : equivalence (@blank_rel Γ _) := ⟨blank_rel.refl, @blank_rel.symm _ _, @blank_rel.trans _ _⟩ /-- Construct a setoid instance for `blank_rel`. -/ def blank_rel.setoid (Γ) [inhabited Γ] : setoid (list Γ) := ⟨_, blank_rel.equivalence _⟩ /-- A `list_blank Γ` is a quotient of `list Γ` by extension by blanks at the end. This is used to represent half-tapes of a Turing machine, so that we can pretend that the list continues infinitely with blanks. -/ def list_blank (Γ) [inhabited Γ] := quotient (blank_rel.setoid Γ) instance list_blank.inhabited {Γ} [inhabited Γ] : inhabited (list_blank Γ) := ⟨quotient.mk' []⟩ instance list_blank.has_emptyc {Γ} [inhabited Γ] : has_emptyc (list_blank Γ) := ⟨quotient.mk' []⟩ /-- A modified version of `quotient.lift_on'` specialized for `list_blank`, with the stronger precondition `blank_extends` instead of `blank_rel`. -/ @[elab_as_eliminator, reducible] protected def list_blank.lift_on {Γ} [inhabited Γ] {α} (l : list_blank Γ) (f : list Γ → α) (H : ∀ a b, blank_extends a b → f a = f b) : α := l.lift_on' f $ by rintro a b (h|h); [exact H _ _ h, exact (H _ _ h).symm] /-- The quotient map turning a `list` into a `list_blank`. -/ def list_blank.mk {Γ} [inhabited Γ] : list Γ → list_blank Γ := quotient.mk' @[elab_as_eliminator] protected lemma list_blank.induction_on {Γ} [inhabited Γ] {p : list_blank Γ → Prop} (q : list_blank Γ) (h : ∀ a, p (list_blank.mk a)) : p q := quotient.induction_on' q h /-- The head of a `list_blank` is well defined. -/ def list_blank.head {Γ} [inhabited Γ] (l : list_blank Γ) : Γ := l.lift_on list.head begin rintro _ _ ⟨i, rfl⟩, cases a, {cases i; refl}, refl end @[simp] theorem list_blank.head_mk {Γ} [inhabited Γ] (l : list Γ) : list_blank.head (list_blank.mk l) = l.head := rfl /-- The tail of a `list_blank` is well defined (up to the tail of blanks). -/ def list_blank.tail {Γ} [inhabited Γ] (l : list_blank Γ) : list_blank Γ := l.lift_on (λ l, list_blank.mk l.tail) begin rintro _ _ ⟨i, rfl⟩, refine quotient.sound' (or.inl _), cases a; [{cases i; [exact ⟨0, rfl⟩, exact ⟨i, rfl⟩]}, exact ⟨i, rfl⟩] end @[simp] theorem list_blank.tail_mk {Γ} [inhabited Γ] (l : list Γ) : list_blank.tail (list_blank.mk l) = list_blank.mk l.tail := rfl /-- We can cons an element onto a `list_blank`. -/ def list_blank.cons {Γ} [inhabited Γ] (a : Γ) (l : list_blank Γ) : list_blank Γ := l.lift_on (λ l, list_blank.mk (list.cons a l)) begin rintro _ _ ⟨i, rfl⟩, exact quotient.sound' (or.inl ⟨i, rfl⟩), end @[simp] theorem list_blank.cons_mk {Γ} [inhabited Γ] (a : Γ) (l : list Γ) : list_blank.cons a (list_blank.mk l) = list_blank.mk (a :: l) := rfl @[simp] theorem list_blank.head_cons {Γ} [inhabited Γ] (a : Γ) : ∀ (l : list_blank Γ), (l.cons a).head = a := quotient.ind' $ by exact λ l, rfl @[simp] theorem list_blank.tail_cons {Γ} [inhabited Γ] (a : Γ) : ∀ (l : list_blank Γ), (l.cons a).tail = l := quotient.ind' $ by exact λ l, rfl /-- The `cons` and `head`/`tail` functions are mutually inverse, unlike in the case of `list` where this only holds for nonempty lists. -/ @[simp] theorem list_blank.cons_head_tail {Γ} [inhabited Γ] : ∀ (l : list_blank Γ), l.tail.cons l.head = l := quotient.ind' begin refine (λ l, quotient.sound' (or.inr _)), cases l, {exact ⟨1, rfl⟩}, {refl}, end /-- The `cons` and `head`/`tail` functions are mutually inverse, unlike in the case of `list` where this only holds for nonempty lists. -/ theorem list_blank.exists_cons {Γ} [inhabited Γ] (l : list_blank Γ) : ∃ a l', l = list_blank.cons a l' := ⟨_, _, (list_blank.cons_head_tail _).symm⟩ /-- The n-th element of a `list_blank` is well defined for all `n : ℕ`, unlike in a `list`. -/ def list_blank.nth {Γ} [inhabited Γ] (l : list_blank Γ) (n : ℕ) : Γ := l.lift_on (λ l, list.inth l n) begin rintro l _ ⟨i, rfl⟩, simp only [list.inth], cases lt_or_le _ _ with h h, {rw list.nth_append h}, rw list.nth_len_le h, cases le_or_lt _ _ with h₂ h₂, {rw list.nth_len_le h₂}, rw [list.nth_le_nth h₂, list.nth_le_append_right h, list.nth_le_repeat] end @[simp] theorem list_blank.nth_mk {Γ} [inhabited Γ] (l : list Γ) (n : ℕ) : (list_blank.mk l).nth n = l.inth n := rfl @[simp] theorem list_blank.nth_zero {Γ} [inhabited Γ] (l : list_blank Γ) : l.nth 0 = l.head := begin conv {to_lhs, rw [← list_blank.cons_head_tail l]}, exact quotient.induction_on' l.tail (λ l, rfl) end @[simp] theorem list_blank.nth_succ {Γ} [inhabited Γ] (l : list_blank Γ) (n : ℕ) : l.nth (n + 1) = l.tail.nth n := begin conv {to_lhs, rw [← list_blank.cons_head_tail l]}, exact quotient.induction_on' l.tail (λ l, rfl) end @[ext] theorem list_blank.ext {Γ} [inhabited Γ] {L₁ L₂ : list_blank Γ} : (∀ i, L₁.nth i = L₂.nth i) → L₁ = L₂ := list_blank.induction_on L₁ $ λ l₁, list_blank.induction_on L₂ $ λ l₂ H, begin wlog h : l₁.length ≤ l₂.length using l₁ l₂, swap, { exact (this $ λ i, (H i).symm).symm }, refine quotient.sound' (or.inl ⟨l₂.length - l₁.length, _⟩), refine list.ext_le _ (λ i h h₂, eq.symm _), { simp only [nat.add_sub_of_le h, list.length_append, list.length_repeat] }, simp at H, cases lt_or_le i l₁.length with h' h', { simpa only [list.nth_le_append _ h', list.nth_le_nth h, list.nth_le_nth h', option.iget] using H i }, { simpa only [list.nth_le_append_right h', list.nth_le_repeat, list.nth_le_nth h, list.nth_len_le h', option.iget] using H i }, end /-- Apply a function to a value stored at the nth position of the list. -/ @[simp] def list_blank.modify_nth {Γ} [inhabited Γ] (f : Γ → Γ) : ℕ → list_blank Γ → list_blank Γ | 0 L := L.tail.cons (f L.head) | (n+1) L := (L.tail.modify_nth n).cons L.head theorem list_blank.nth_modify_nth {Γ} [inhabited Γ] (f : Γ → Γ) (n i) (L : list_blank Γ) : (L.modify_nth f n).nth i = if i = n then f (L.nth i) else L.nth i := begin induction n with n IH generalizing i L, { cases i; simp only [list_blank.nth_zero, if_true, list_blank.head_cons, list_blank.modify_nth, eq_self_iff_true, list_blank.nth_succ, if_false, list_blank.tail_cons] }, { cases i, { rw if_neg (nat.succ_ne_zero _).symm, simp only [list_blank.nth_zero, list_blank.head_cons, list_blank.modify_nth] }, { simp only [IH, list_blank.modify_nth, list_blank.nth_succ, list_blank.tail_cons], congr } } end /-- A pointed map of `inhabited` types is a map that sends one default value to the other. -/ structure {u v} pointed_map (Γ : Type u) (Γ' : Type v) [inhabited Γ] [inhabited Γ'] : Type (max u v) := (f : Γ → Γ') (map_pt' : f (default _) = default _) instance {Γ Γ'} [inhabited Γ] [inhabited Γ'] : inhabited (pointed_map Γ Γ') := ⟨⟨λ _, default _, rfl⟩⟩ instance {Γ Γ'} [inhabited Γ] [inhabited Γ'] : has_coe_to_fun (pointed_map Γ Γ') := ⟨_, pointed_map.f⟩ @[simp] theorem pointed_map.mk_val {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : Γ → Γ') (pt) : (pointed_map.mk f pt : Γ → Γ') = f := rfl @[simp] theorem pointed_map.map_pt {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') : f (default _) = default _ := pointed_map.map_pt' _ @[simp] theorem pointed_map.head_map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list Γ) : (l.map f).head = f l.head := by cases l; [exact (pointed_map.map_pt f).symm, refl] /-- The `map` function on lists is well defined on `list_blank`s provided that the map is pointed. -/ def list_blank.map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) : list_blank Γ' := l.lift_on (λ l, list_blank.mk (list.map f l)) begin rintro l _ ⟨i, rfl⟩, refine quotient.sound' (or.inl ⟨i, _⟩), simp only [pointed_map.map_pt, list.map_append, list.map_repeat], end @[simp] theorem list_blank.map_mk {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list Γ) : (list_blank.mk l).map f = list_blank.mk (l.map f) := rfl @[simp] theorem list_blank.head_map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) : (l.map f).head = f l.head := begin conv {to_lhs, rw [← list_blank.cons_head_tail l]}, exact quotient.induction_on' l (λ a, rfl) end @[simp] theorem list_blank.tail_map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) : (l.map f).tail = l.tail.map f := begin conv {to_lhs, rw [← list_blank.cons_head_tail l]}, exact quotient.induction_on' l (λ a, rfl) end @[simp] theorem list_blank.map_cons {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) (a : Γ) : (l.cons a).map f = (l.map f).cons (f a) := begin refine (list_blank.cons_head_tail _).symm.trans _, simp only [list_blank.head_map, list_blank.head_cons, list_blank.tail_map, list_blank.tail_cons] end @[simp] theorem list_blank.nth_map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) (n : ℕ) : (l.map f).nth n = f (l.nth n) := l.induction_on begin intro l, simp only [list.nth_map, list_blank.map_mk, list_blank.nth_mk, list.inth], cases l.nth n, {exact f.2.symm}, {refl} end /-- The `i`-th projection as a pointed map. -/ def proj {ι : Type*} {Γ : ι → Type*} [∀ i, inhabited (Γ i)] (i : ι) : pointed_map (∀ i, Γ i) (Γ i) := ⟨λ a, a i, rfl⟩ theorem proj_map_nth {ι : Type*} {Γ : ι → Type*} [∀ i, inhabited (Γ i)] (i : ι) (L n) : (list_blank.map (@proj ι Γ _ i) L).nth n = L.nth n i := by rw list_blank.nth_map; refl theorem list_blank.map_modify_nth {Γ Γ'} [inhabited Γ] [inhabited Γ'] (F : pointed_map Γ Γ') (f : Γ → Γ) (f' : Γ' → Γ') (H : ∀ x, F (f x) = f' (F x)) (n) (L : list_blank Γ) : (L.modify_nth f n).map F = (L.map F).modify_nth f' n := by induction n with n IH generalizing L; simp only [*, list_blank.head_map, list_blank.modify_nth, list_blank.map_cons, list_blank.tail_map] /-- Append a list on the left side of a list_blank. -/ @[simp] def list_blank.append {Γ} [inhabited Γ] : list Γ → list_blank Γ → list_blank Γ | [] L := L | (a :: l) L := list_blank.cons a (list_blank.append l L) @[simp] theorem list_blank.append_mk {Γ} [inhabited Γ] (l₁ l₂ : list Γ) : list_blank.append l₁ (list_blank.mk l₂) = list_blank.mk (l₁ ++ l₂) := by induction l₁; simp only [*, list_blank.append, list.nil_append, list.cons_append, list_blank.cons_mk] theorem list_blank.append_assoc {Γ} [inhabited Γ] (l₁ l₂ : list Γ) (l₃ : list_blank Γ) : list_blank.append (l₁ ++ l₂) l₃ = list_blank.append l₁ (list_blank.append l₂ l₃) := l₃.induction_on $ by intro; simp only [list_blank.append_mk, list.append_assoc] /-- The `bind` function on lists is well defined on `list_blank`s provided that the default element is sent to a sequence of default elements. -/ def list_blank.bind {Γ Γ'} [inhabited Γ] [inhabited Γ'] (l : list_blank Γ) (f : Γ → list Γ') (hf : ∃ n, f (default _) = list.repeat (default _) n) : list_blank Γ' := l.lift_on (λ l, list_blank.mk (list.bind l f)) begin rintro l _ ⟨i, rfl⟩, cases hf with n e, refine quotient.sound' (or.inl ⟨i * n, _⟩), rw [list.bind_append, mul_comm], congr, induction i with i IH, refl, simp only [IH, e, list.repeat_add, nat.mul_succ, add_comm, list.repeat_succ, list.cons_bind], end @[simp] lemma list_blank.bind_mk {Γ Γ'} [inhabited Γ] [inhabited Γ'] (l : list Γ) (f : Γ → list Γ') (hf) : (list_blank.mk l).bind f hf = list_blank.mk (l.bind f) := rfl @[simp] lemma list_blank.cons_bind {Γ Γ'} [inhabited Γ] [inhabited Γ'] (a : Γ) (l : list_blank Γ) (f : Γ → list Γ') (hf) : (l.cons a).bind f hf = (l.bind f hf).append (f a) := l.induction_on $ by intro; simp only [list_blank.append_mk, list_blank.bind_mk, list_blank.cons_mk, list.cons_bind] /-- The tape of a Turing machine is composed of a head element (which we imagine to be the current position of the head), together with two `list_blank`s denoting the portions of the tape going off to the left and right. When the Turing machine moves right, an element is pulled from the right side and becomes the new head, while the head element is consed onto the left side. -/ structure tape (Γ : Type*) [inhabited Γ] := (head : Γ) (left : list_blank Γ) (right : list_blank Γ) instance tape.inhabited {Γ} [inhabited Γ] : inhabited (tape Γ) := ⟨by constructor; apply default⟩ /-- A direction for the turing machine `move` command, either left or right. -/ @[derive decidable_eq, derive inhabited] inductive dir | left | right /-- The "inclusive" left side of the tape, including both `left` and `head`. -/ def tape.left₀ {Γ} [inhabited Γ] (T : tape Γ) : list_blank Γ := T.left.cons T.head /-- The "inclusive" right side of the tape, including both `right` and `head`. -/ def tape.right₀ {Γ} [inhabited Γ] (T : tape Γ) : list_blank Γ := T.right.cons T.head /-- Move the tape in response to a motion of the Turing machine. Note that `T.move dir.left` makes `T.left` smaller; the Turing machine is moving left and the tape is moving right. -/ def tape.move {Γ} [inhabited Γ] : dir → tape Γ → tape Γ | dir.left ⟨a, L, R⟩ := ⟨L.head, L.tail, R.cons a⟩ | dir.right ⟨a, L, R⟩ := ⟨R.head, L.cons a, R.tail⟩ @[simp] theorem tape.move_left_right {Γ} [inhabited Γ] (T : tape Γ) : (T.move dir.left).move dir.right = T := by cases T; simp [tape.move] @[simp] theorem tape.move_right_left {Γ} [inhabited Γ] (T : tape Γ) : (T.move dir.right).move dir.left = T := by cases T; simp [tape.move] /-- Construct a tape from a left side and an inclusive right side. -/ def tape.mk' {Γ} [inhabited Γ] (L R : list_blank Γ) : tape Γ := ⟨R.head, L, R.tail⟩ @[simp] theorem tape.mk'_left {Γ} [inhabited Γ] (L R : list_blank Γ) : (tape.mk' L R).left = L := rfl @[simp] theorem tape.mk'_head {Γ} [inhabited Γ] (L R : list_blank Γ) : (tape.mk' L R).head = R.head := rfl @[simp] theorem tape.mk'_right {Γ} [inhabited Γ] (L R : list_blank Γ) : (tape.mk' L R).right = R.tail := rfl @[simp] theorem tape.mk'_right₀ {Γ} [inhabited Γ] (L R : list_blank Γ) : (tape.mk' L R).right₀ = R := list_blank.cons_head_tail _ @[simp] theorem tape.mk'_left_right₀ {Γ} [inhabited Γ] (T : tape Γ) : tape.mk' T.left T.right₀ = T := by cases T; simp only [tape.right₀, tape.mk', list_blank.head_cons, list_blank.tail_cons, eq_self_iff_true, and_self] theorem tape.exists_mk' {Γ} [inhabited Γ] (T : tape Γ) : ∃ L R, T = tape.mk' L R := ⟨_, _, (tape.mk'_left_right₀ _).symm⟩ @[simp] theorem tape.move_left_mk' {Γ} [inhabited Γ] (L R : list_blank Γ) : (tape.mk' L R).move dir.left = tape.mk' L.tail (R.cons L.head) := by simp only [tape.move, tape.mk', list_blank.head_cons, eq_self_iff_true, list_blank.cons_head_tail, and_self, list_blank.tail_cons] @[simp] theorem tape.move_right_mk' {Γ} [inhabited Γ] (L R : list_blank Γ) : (tape.mk' L R).move dir.right = tape.mk' (L.cons R.head) R.tail := by simp only [tape.move, tape.mk', list_blank.head_cons, eq_self_iff_true, list_blank.cons_head_tail, and_self, list_blank.tail_cons] /-- Construct a tape from a left side and an inclusive right side. -/ def tape.mk₂ {Γ} [inhabited Γ] (L R : list Γ) : tape Γ := tape.mk' (list_blank.mk L) (list_blank.mk R) /-- Construct a tape from a list, with the head of the list at the TM head and the rest going to the right. -/ def tape.mk₁ {Γ} [inhabited Γ] (l : list Γ) : tape Γ := tape.mk₂ [] l /-- The `nth` function of a tape is integer-valued, with index `0` being the head, negative indexes on the left and positive indexes on the right. (Picture a number line.) -/ def tape.nth {Γ} [inhabited Γ] (T : tape Γ) : ℤ → Γ | 0 := T.head | (n+1:ℕ) := T.right.nth n | -[1+ n] := T.left.nth n @[simp] theorem tape.nth_zero {Γ} [inhabited Γ] (T : tape Γ) : T.nth 0 = T.1 := rfl theorem tape.right₀_nth {Γ} [inhabited Γ] (T : tape Γ) (n : ℕ) : T.right₀.nth n = T.nth n := by cases n; simp only [tape.nth, tape.right₀, int.coe_nat_zero, list_blank.nth_zero, list_blank.nth_succ, list_blank.head_cons, list_blank.tail_cons] @[simp] theorem tape.mk'_nth_nat {Γ} [inhabited Γ] (L R : list_blank Γ) (n : ℕ) : (tape.mk' L R).nth n = R.nth n := by rw [← tape.right₀_nth, tape.mk'_right₀] @[simp] theorem tape.move_left_nth {Γ} [inhabited Γ] : ∀ (T : tape Γ) (i : ℤ), (T.move dir.left).nth i = T.nth (i-1) | ⟨a, L, R⟩ -[1+ n] := (list_blank.nth_succ _ _).symm | ⟨a, L, R⟩ 0 := (list_blank.nth_zero _).symm | ⟨a, L, R⟩ 1 := (list_blank.nth_zero _).trans (list_blank.head_cons _ _) | ⟨a, L, R⟩ ((n+1:ℕ)+1) := begin rw add_sub_cancel, change (R.cons a).nth (n+1) = R.nth n, rw [list_blank.nth_succ, list_blank.tail_cons] end @[simp] theorem tape.move_right_nth {Γ} [inhabited Γ] (T : tape Γ) (i : ℤ) : (T.move dir.right).nth i = T.nth (i+1) := by conv {to_rhs, rw ← T.move_right_left}; rw [tape.move_left_nth, add_sub_cancel] @[simp] theorem tape.move_right_n_head {Γ} [inhabited Γ] (T : tape Γ) (i : ℕ) : ((tape.move dir.right)^[i] T).head = T.nth i := by induction i generalizing T; [refl, simp only [*, tape.move_right_nth, int.coe_nat_succ, iterate_succ]] /-- Replace the current value of the head on the tape. -/ def tape.write {Γ} [inhabited Γ] (b : Γ) (T : tape Γ) : tape Γ := {head := b, ..T} @[simp] theorem tape.write_self {Γ} [inhabited Γ] : ∀ (T : tape Γ), T.write T.1 = T := by rintro ⟨⟩; refl @[simp] theorem tape.write_nth {Γ} [inhabited Γ] (b : Γ) : ∀ (T : tape Γ) {i : ℤ}, (T.write b).nth i = if i = 0 then b else T.nth i | ⟨a, L, R⟩ 0 := rfl | ⟨a, L, R⟩ (n+1:ℕ) := rfl | ⟨a, L, R⟩ -[1+ n] := rfl @[simp] theorem tape.write_mk' {Γ} [inhabited Γ] (a b : Γ) (L R : list_blank Γ) : (tape.mk' L (R.cons a)).write b = tape.mk' L (R.cons b) := by simp only [tape.write, tape.mk', list_blank.head_cons, list_blank.tail_cons, eq_self_iff_true, and_self] /-- Apply a pointed map to a tape to change the alphabet. -/ def tape.map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (T : tape Γ) : tape Γ' := ⟨f T.1, T.2.map f, T.3.map f⟩ @[simp] theorem tape.map_fst {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') : ∀ (T : tape Γ), (T.map f).1 = f T.1 := by rintro ⟨⟩; refl @[simp] theorem tape.map_write {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (b : Γ) : ∀ (T : tape Γ), (T.write b).map f = (T.map f).write (f b) := by rintro ⟨⟩; refl @[simp] theorem tape.write_move_right_n {Γ} [inhabited Γ] (f : Γ → Γ) (L R : list_blank Γ) (n : ℕ) : ((tape.move dir.right)^[n] (tape.mk' L R)).write (f (R.nth n)) = ((tape.move dir.right)^[n] (tape.mk' L (R.modify_nth f n))) := begin induction n with n IH generalizing L R, { simp only [list_blank.nth_zero, list_blank.modify_nth, iterate_zero_apply], rw [← tape.write_mk', list_blank.cons_head_tail] }, simp only [list_blank.head_cons, list_blank.nth_succ, list_blank.modify_nth, tape.move_right_mk', list_blank.tail_cons, iterate_succ_apply, IH] end theorem tape.map_move {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (T : tape Γ) (d) : (T.move d).map f = (T.map f).move d := by cases T; cases d; simp only [tape.move, tape.map, list_blank.head_map, eq_self_iff_true, list_blank.map_cons, and_self, list_blank.tail_map] theorem tape.map_mk' {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (L R : list_blank Γ) : (tape.mk' L R).map f = tape.mk' (L.map f) (R.map f) := by simp only [tape.mk', tape.map, list_blank.head_map, eq_self_iff_true, and_self, list_blank.tail_map] theorem tape.map_mk₂ {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (L R : list Γ) : (tape.mk₂ L R).map f = tape.mk₂ (L.map f) (R.map f) := by simp only [tape.mk₂, tape.map_mk', list_blank.map_mk] theorem tape.map_mk₁ {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : pointed_map Γ Γ') (l : list Γ) : (tape.mk₁ l).map f = tape.mk₁ (l.map f) := tape.map_mk₂ _ _ _ /-- Run a state transition function `σ → option σ` "to completion". The return value is the last state returned before a `none` result. If the state transition function always returns `some`, then the computation diverges, returning `part.none`. -/ def eval {σ} (f : σ → option σ) : σ → part σ := pfun.fix (λ s, part.some $ (f s).elim (sum.inl s) sum.inr) /-- The reflexive transitive closure of a state transition function. `reaches f a b` means there is a finite sequence of steps `f a = some a₁`, `f a₁ = some a₂`, ... such that `aₙ = b`. This relation permits zero steps of the state transition function. -/ def reaches {σ} (f : σ → option σ) : σ → σ → Prop := refl_trans_gen (λ a b, b ∈ f a) /-- The transitive closure of a state transition function. `reaches₁ f a b` means there is a nonempty finite sequence of steps `f a = some a₁`, `f a₁ = some a₂`, ... such that `aₙ = b`. This relation does not permit zero steps of the state transition function. -/ def reaches₁ {σ} (f : σ → option σ) : σ → σ → Prop := trans_gen (λ a b, b ∈ f a) theorem reaches₁_eq {σ} {f : σ → option σ} {a b c} (h : f a = f b) : reaches₁ f a c ↔ reaches₁ f b c := trans_gen.head'_iff.trans (trans_gen.head'_iff.trans $ by rw h).symm theorem reaches_total {σ} {f : σ → option σ} {a b c} : reaches f a b → reaches f a c → reaches f b c ∨ reaches f c b := refl_trans_gen.total_of_right_unique ⟨λ _ _ _, option.mem_unique⟩ theorem reaches₁_fwd {σ} {f : σ → option σ} {a b c} (h₁ : reaches₁ f a c) (h₂ : b ∈ f a) : reaches f b c := begin rcases trans_gen.head'_iff.1 h₁ with ⟨b', hab, hbc⟩, cases option.mem_unique hab h₂, exact hbc end /-- A variation on `reaches`. `reaches₀ f a b` holds if whenever `reaches₁ f b c` then `reaches₁ f a c`. This is a weaker property than `reaches` and is useful for replacing states with equivalent states without taking a step. -/ def reaches₀ {σ} (f : σ → option σ) (a b : σ) : Prop := ∀ c, reaches₁ f b c → reaches₁ f a c theorem reaches₀.trans {σ} {f : σ → option σ} {a b c : σ} (h₁ : reaches₀ f a b) (h₂ : reaches₀ f b c) : reaches₀ f a c | d h₃ := h₁ _ (h₂ _ h₃) @[refl] theorem reaches₀.refl {σ} {f : σ → option σ} (a : σ) : reaches₀ f a a | b h := h theorem reaches₀.single {σ} {f : σ → option σ} {a b : σ} (h : b ∈ f a) : reaches₀ f a b | c h₂ := h₂.head h theorem reaches₀.head {σ} {f : σ → option σ} {a b c : σ} (h : b ∈ f a) (h₂ : reaches₀ f b c) : reaches₀ f a c := (reaches₀.single h).trans h₂ theorem reaches₀.tail {σ} {f : σ → option σ} {a b c : σ} (h₁ : reaches₀ f a b) (h : c ∈ f b) : reaches₀ f a c := h₁.trans (reaches₀.single h) theorem reaches₀_eq {σ} {f : σ → option σ} {a b} (e : f a = f b) : reaches₀ f a b | d h := (reaches₁_eq e).2 h theorem reaches₁.to₀ {σ} {f : σ → option σ} {a b : σ} (h : reaches₁ f a b) : reaches₀ f a b | c h₂ := h.trans h₂ theorem reaches.to₀ {σ} {f : σ → option σ} {a b : σ} (h : reaches f a b) : reaches₀ f a b | c h₂ := h₂.trans_right h theorem reaches₀.tail' {σ} {f : σ → option σ} {a b c : σ} (h : reaches₀ f a b) (h₂ : c ∈ f b) : reaches₁ f a c := h _ (trans_gen.single h₂) /-- (co-)Induction principle for `eval`. If a property `C` holds of any point `a` evaluating to `b` which is either terminal (meaning `a = b`) or where the next point also satisfies `C`, then it holds of any point where `eval f a` evaluates to `b`. This formalizes the notion that if `eval f a` evaluates to `b` then it reaches terminal state `b` in finitely many steps. -/ @[elab_as_eliminator] def eval_induction {σ} {f : σ → option σ} {b : σ} {C : σ → Sort*} {a : σ} (h : b ∈ eval f a) (H : ∀ a, b ∈ eval f a → (∀ a', b ∈ eval f a' → f a = some a' → C a') → C a) : C a := pfun.fix_induction h (λ a' ha' h', H _ ha' $ λ b' hb' e, h' _ hb' $ part.mem_some_iff.2 $ by rw e; refl) theorem mem_eval {σ} {f : σ → option σ} {a b} : b ∈ eval f a ↔ reaches f a b ∧ f b = none := ⟨λ h, begin refine eval_induction h (λ a h IH, _), cases e : f a with a', { rw part.mem_unique h (pfun.mem_fix_iff.2 $ or.inl $ part.mem_some_iff.2 $ by rw e; refl), exact ⟨refl_trans_gen.refl, e⟩ }, { rcases pfun.mem_fix_iff.1 h with h | ⟨_, h, h'⟩; rw e at h; cases part.mem_some_iff.1 h, cases IH a' h' (by rwa e) with h₁ h₂, exact ⟨refl_trans_gen.head e h₁, h₂⟩ } end, λ ⟨h₁, h₂⟩, begin refine refl_trans_gen.head_induction_on h₁ _ (λ a a' h _ IH, _), { refine pfun.mem_fix_iff.2 (or.inl _), rw h₂, apply part.mem_some }, { refine pfun.mem_fix_iff.2 (or.inr ⟨_, _, IH⟩), rw show f a = _, from h, apply part.mem_some } end⟩ theorem eval_maximal₁ {σ} {f : σ → option σ} {a b} (h : b ∈ eval f a) (c) : ¬ reaches₁ f b c | bc := let ⟨ab, b0⟩ := mem_eval.1 h, ⟨b', h', _⟩ := trans_gen.head'_iff.1 bc in by cases b0.symm.trans h' theorem eval_maximal {σ} {f : σ → option σ} {a b} (h : b ∈ eval f a) {c} : reaches f b c ↔ c = b := let ⟨ab, b0⟩ := mem_eval.1 h in refl_trans_gen_iff_eq $ λ b' h', by cases b0.symm.trans h' theorem reaches_eval {σ} {f : σ → option σ} {a b} (ab : reaches f a b) : eval f a = eval f b := part.ext $ λ c, ⟨λ h, let ⟨ac, c0⟩ := mem_eval.1 h in mem_eval.2 ⟨(or_iff_left_of_imp $ by exact λ cb, (eval_maximal h).1 cb ▸ refl_trans_gen.refl).1 (reaches_total ab ac), c0⟩, λ h, let ⟨bc, c0⟩ := mem_eval.1 h in mem_eval.2 ⟨ab.trans bc, c0⟩,⟩ /-- Given a relation `tr : σ₁ → σ₂ → Prop` between state spaces, and state transition functions `f₁ : σ₁ → option σ₁` and `f₂ : σ₂ → option σ₂`, `respects f₁ f₂ tr` means that if `tr a₁ a₂` holds initially and `f₁` takes a step to `a₂` then `f₂` will take one or more steps before reaching a state `b₂` satisfying `tr a₂ b₂`, and if `f₁ a₁` terminates then `f₂ a₂` also terminates. Such a relation `tr` is also known as a refinement. -/ def respects {σ₁ σ₂} (f₁ : σ₁ → option σ₁) (f₂ : σ₂ → option σ₂) (tr : σ₁ → σ₂ → Prop) := ∀ ⦃a₁ a₂⦄, tr a₁ a₂ → (match f₁ a₁ with | some b₁ := ∃ b₂, tr b₁ b₂ ∧ reaches₁ f₂ a₂ b₂ | none := f₂ a₂ = none end : Prop) theorem tr_reaches₁ {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) {b₁} (ab : reaches₁ f₁ a₁ b₁) : ∃ b₂, tr b₁ b₂ ∧ reaches₁ f₂ a₂ b₂ := begin induction ab with c₁ ac c₁ d₁ ac cd IH, { have := H aa, rwa (show f₁ a₁ = _, from ac) at this }, { rcases IH with ⟨c₂, cc, ac₂⟩, have := H cc, rw (show f₁ c₁ = _, from cd) at this, rcases this with ⟨d₂, dd, cd₂⟩, exact ⟨_, dd, ac₂.trans cd₂⟩ } end theorem tr_reaches {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) {b₁} (ab : reaches f₁ a₁ b₁) : ∃ b₂, tr b₁ b₂ ∧ reaches f₂ a₂ b₂ := begin rcases refl_trans_gen_iff_eq_or_trans_gen.1 ab with rfl | ab, { exact ⟨_, aa, refl_trans_gen.refl⟩ }, { exact let ⟨b₂, bb, h⟩ := tr_reaches₁ H aa ab in ⟨b₂, bb, h.to_refl⟩ } end theorem tr_reaches_rev {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) {b₂} (ab : reaches f₂ a₂ b₂) : ∃ c₁ c₂, reaches f₂ b₂ c₂ ∧ tr c₁ c₂ ∧ reaches f₁ a₁ c₁ := begin induction ab with c₂ d₂ ac cd IH, { exact ⟨_, _, refl_trans_gen.refl, aa, refl_trans_gen.refl⟩ }, { rcases IH with ⟨e₁, e₂, ce, ee, ae⟩, rcases refl_trans_gen.cases_head ce with rfl | ⟨d', cd', de⟩, { have := H ee, revert this, cases eg : f₁ e₁ with g₁; simp only [respects, and_imp, exists_imp_distrib], { intro c0, cases cd.symm.trans c0 }, { intros g₂ gg cg, rcases trans_gen.head'_iff.1 cg with ⟨d', cd', dg⟩, cases option.mem_unique cd cd', exact ⟨_, _, dg, gg, ae.tail eg⟩ } }, { cases option.mem_unique cd cd', exact ⟨_, _, de, ee, ae⟩ } } end theorem tr_eval {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ b₁ a₂} (aa : tr a₁ a₂) (ab : b₁ ∈ eval f₁ a₁) : ∃ b₂, tr b₁ b₂ ∧ b₂ ∈ eval f₂ a₂ := begin cases mem_eval.1 ab with ab b0, rcases tr_reaches H aa ab with ⟨b₂, bb, ab⟩, refine ⟨_, bb, mem_eval.2 ⟨ab, _⟩⟩, have := H bb, rwa b0 at this end theorem tr_eval_rev {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ b₂ a₂} (aa : tr a₁ a₂) (ab : b₂ ∈ eval f₂ a₂) : ∃ b₁, tr b₁ b₂ ∧ b₁ ∈ eval f₁ a₁ := begin cases mem_eval.1 ab with ab b0, rcases tr_reaches_rev H aa ab with ⟨c₁, c₂, bc, cc, ac⟩, cases (refl_trans_gen_iff_eq (by exact option.eq_none_iff_forall_not_mem.1 b0)).1 bc, refine ⟨_, cc, mem_eval.2 ⟨ac, _⟩⟩, have := H cc, cases f₁ c₁ with d₁, {refl}, rcases this with ⟨d₂, dd, bd⟩, rcases trans_gen.head'_iff.1 bd with ⟨e, h, _⟩, cases b0.symm.trans h end theorem tr_eval_dom {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) : (eval f₂ a₂).dom ↔ (eval f₁ a₁).dom := ⟨λ h, let ⟨b₂, tr, h, _⟩ := tr_eval_rev H aa ⟨h, rfl⟩ in h, λ h, let ⟨b₂, tr, h, _⟩ := tr_eval H aa ⟨h, rfl⟩ in h⟩ /-- A simpler version of `respects` when the state transition relation `tr` is a function. -/ def frespects {σ₁ σ₂} (f₂ : σ₂ → option σ₂) (tr : σ₁ → σ₂) (a₂ : σ₂) : option σ₁ → Prop | (some b₁) := reaches₁ f₂ a₂ (tr b₁) | none := f₂ a₂ = none theorem frespects_eq {σ₁ σ₂} {f₂ : σ₂ → option σ₂} {tr : σ₁ → σ₂} {a₂ b₂} (h : f₂ a₂ = f₂ b₂) : ∀ {b₁}, frespects f₂ tr a₂ b₁ ↔ frespects f₂ tr b₂ b₁ | (some b₁) := reaches₁_eq h | none := by unfold frespects; rw h theorem fun_respects {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂} : respects f₁ f₂ (λ a b, tr a = b) ↔ ∀ ⦃a₁⦄, frespects f₂ tr (tr a₁) (f₁ a₁) := forall_congr $ λ a₁, by cases f₁ a₁; simp only [frespects, respects, exists_eq_left', forall_eq'] theorem tr_eval' {σ₁ σ₂} (f₁ : σ₁ → option σ₁) (f₂ : σ₂ → option σ₂) (tr : σ₁ → σ₂) (H : respects f₁ f₂ (λ a b, tr a = b)) (a₁) : eval f₂ (tr a₁) = tr <$> eval f₁ a₁ := part.ext $ λ b₂, ⟨λ h, let ⟨b₁, bb, hb⟩ := tr_eval_rev H rfl h in (part.mem_map_iff _).2 ⟨b₁, hb, bb⟩, λ h, begin rcases (part.mem_map_iff _).1 h with ⟨b₁, ab, bb⟩, rcases tr_eval H rfl ab with ⟨_, rfl, h⟩, rwa bb at h end⟩ /-! ## The TM0 model A TM0 turing machine is essentially a Post-Turing machine, adapted for type theory. A Post-Turing machine with symbol type `Γ` and label type `Λ` is a function `Λ → Γ → option (Λ × stmt)`, where a `stmt` can be either `move left`, `move right` or `write a` for `a : Γ`. The machine works over a "tape", a doubly-infinite sequence of elements of `Γ`, and an instantaneous configuration, `cfg`, is a label `q : Λ` indicating the current internal state of the machine, and a `tape Γ` (which is essentially `ℤ →₀ Γ`). The evolution is described by the `step` function: * If `M q T.head = none`, then the machine halts. * If `M q T.head = some (q', s)`, then the machine performs action `s : stmt` and then transitions to state `q'`. The initial state takes a `list Γ` and produces a `tape Γ` where the head of the list is the head of the tape and the rest of the list extends to the right, with the left side all blank. The final state takes the entire right side of the tape right or equal to the current position of the machine. (This is actually a `list_blank Γ`, not a `list Γ`, because we don't know, at this level of generality, where the output ends. If equality to `default Γ` is decidable we can trim the list to remove the infinite tail of blanks.) -/ namespace TM0 section parameters (Γ : Type*) [inhabited Γ] -- type of tape symbols parameters (Λ : Type*) [inhabited Λ] -- type of "labels" or TM states /-- A Turing machine "statement" is just a command to either move left or right, or write a symbol on the tape. -/ inductive stmt | move : dir → stmt | write : Γ → stmt instance stmt.inhabited : inhabited stmt := ⟨stmt.write (default _)⟩ /-- A Post-Turing machine with symbol type `Γ` and label type `Λ` is a function which, given the current state `q : Λ` and the tape head `a : Γ`, either halts (returns `none`) or returns a new state `q' : Λ` and a `stmt` describing what to do, either a move left or right, or a write command. Both `Λ` and `Γ` are required to be inhabited; the default value for `Γ` is the "blank" tape value, and the default value of `Λ` is the initial state. -/ @[nolint unused_arguments] -- [inhabited Λ]: this is a deliberate addition, see comment def machine := Λ → Γ → option (Λ × stmt) instance machine.inhabited : inhabited machine := by unfold machine; apply_instance /-- The configuration state of a Turing machine during operation consists of a label (machine state), and a tape, represented in the form `(a, L, R)` meaning the tape looks like `L.rev ++ [a] ++ R` with the machine currently reading the `a`. The lists are automatically extended with blanks as the machine moves around. -/ structure cfg := (q : Λ) (tape : tape Γ) instance cfg.inhabited : inhabited cfg := ⟨⟨default _, default _⟩⟩ parameters {Γ Λ} /-- Execution semantics of the Turing machine. -/ def step (M : machine) : cfg → option cfg | ⟨q, T⟩ := (M q T.1).map (λ ⟨q', a⟩, ⟨q', match a with | stmt.move d := T.move d | stmt.write a := T.write a end⟩) /-- The statement `reaches M s₁ s₂` means that `s₂` is obtained starting from `s₁` after a finite number of steps from `s₂`. -/ def reaches (M : machine) : cfg → cfg → Prop := refl_trans_gen (λ a b, b ∈ step M a) /-- The initial configuration. -/ def init (l : list Γ) : cfg := ⟨default Λ, tape.mk₁ l⟩ /-- Evaluate a Turing machine on initial input to a final state, if it terminates. -/ def eval (M : machine) (l : list Γ) : part (list_blank Γ) := (eval (step M) (init l)).map (λ c, c.tape.right₀) /-- The raw definition of a Turing machine does not require that `Γ` and `Λ` are finite, and in practice we will be interested in the infinite `Λ` case. We recover instead a notion of "effectively finite" Turing machines, which only make use of a finite subset of their states. We say that a set `S ⊆ Λ` supports a Turing machine `M` if `S` is closed under the transition function and contains the initial state. -/ def supports (M : machine) (S : set Λ) := default Λ ∈ S ∧ ∀ {q a q' s}, (q', s) ∈ M q a → q ∈ S → q' ∈ S theorem step_supports (M : machine) {S} (ss : supports M S) : ∀ {c c' : cfg}, c' ∈ step M c → c.q ∈ S → c'.q ∈ S | ⟨q, T⟩ c' h₁ h₂ := begin rcases option.map_eq_some'.1 h₁ with ⟨⟨q', a⟩, h, rfl⟩, exact ss.2 h h₂, end theorem univ_supports (M : machine) : supports M set.univ := ⟨trivial, λ q a q' s h₁ h₂, trivial⟩ end section variables {Γ : Type*} [inhabited Γ] variables {Γ' : Type*} [inhabited Γ'] variables {Λ : Type*} [inhabited Λ] variables {Λ' : Type*} [inhabited Λ'] /-- Map a TM statement across a function. This does nothing to move statements and maps the write values. -/ def stmt.map (f : pointed_map Γ Γ') : stmt Γ → stmt Γ' | (stmt.move d) := stmt.move d | (stmt.write a) := stmt.write (f a) /-- Map a configuration across a function, given `f : Γ → Γ'` a map of the alphabets and `g : Λ → Λ'` a map of the machine states. -/ def cfg.map (f : pointed_map Γ Γ') (g : Λ → Λ') : cfg Γ Λ → cfg Γ' Λ' | ⟨q, T⟩ := ⟨g q, T.map f⟩ variables (M : machine Γ Λ) (f₁ : pointed_map Γ Γ') (f₂ : pointed_map Γ' Γ) (g₁ : Λ → Λ') (g₂ : Λ' → Λ) /-- Because the state transition function uses the alphabet and machine states in both the input and output, to map a machine from one alphabet and machine state space to another we need functions in both directions, essentially an `equiv` without the laws. -/ def machine.map : machine Γ' Λ' | q l := (M (g₂ q) (f₂ l)).map (prod.map g₁ (stmt.map f₁)) theorem machine.map_step {S : set Λ} (f₂₁ : function.right_inverse f₁ f₂) (g₂₁ : ∀ q ∈ S, g₂ (g₁ q) = q) : ∀ c : cfg Γ Λ, c.q ∈ S → (step M c).map (cfg.map f₁ g₁) = step (M.map f₁ f₂ g₁ g₂) (cfg.map f₁ g₁ c) | ⟨q, T⟩ h := begin unfold step machine.map cfg.map, simp only [turing.tape.map_fst, g₂₁ q h, f₂₁ _], rcases M q T.1 with _|⟨q', d|a⟩, {refl}, { simp only [step, cfg.map, option.map_some', tape.map_move f₁], refl }, { simp only [step, cfg.map, option.map_some', tape.map_write], refl } end theorem map_init (g₁ : pointed_map Λ Λ') (l : list Γ) : (init l).map f₁ g₁ = init (l.map f₁) := congr (congr_arg cfg.mk g₁.map_pt) (tape.map_mk₁ _ _) theorem machine.map_respects (g₁ : pointed_map Λ Λ') (g₂ : Λ' → Λ) {S} (ss : supports M S) (f₂₁ : function.right_inverse f₁ f₂) (g₂₁ : ∀ q ∈ S, g₂ (g₁ q) = q) : respects (step M) (step (M.map f₁ f₂ g₁ g₂)) (λ a b, a.q ∈ S ∧ cfg.map f₁ g₁ a = b) | c _ ⟨cs, rfl⟩ := begin cases e : step M c with c'; unfold respects, { rw [← M.map_step f₁ f₂ g₁ g₂ f₂₁ g₂₁ _ cs, e], refl }, { refine ⟨_, ⟨step_supports M ss e cs, rfl⟩, trans_gen.single _⟩, rw [← M.map_step f₁ f₂ g₁ g₂ f₂₁ g₂₁ _ cs, e], exact rfl } end end end TM0 /-! ## The TM1 model The TM1 model is a simplification and extension of TM0 (Post-Turing model) in the direction of Wang B-machines. The machine's internal state is extended with a (finite) store `σ` of variables that may be accessed and updated at any time. A machine is given by a `Λ` indexed set of procedures or functions. Each function has a body which is a `stmt`. Most of the regular commands are allowed to use the current value `a` of the local variables and the value `T.head` on the tape to calculate what to write or how to change local state, but the statements themselves have a fixed structure. The `stmt`s can be as follows: * `move d q`: move left or right, and then do `q` * `write (f : Γ → σ → Γ) q`: write `f a T.head` to the tape, then do `q` * `load (f : Γ → σ → σ) q`: change the internal state to `f a T.head` * `branch (f : Γ → σ → bool) qtrue qfalse`: If `f a T.head` is true, do `qtrue`, else `qfalse` * `goto (f : Γ → σ → Λ)`: Go to label `f a T.head` * `halt`: Transition to the halting state, which halts on the following step Note that here most statements do not have labels; `goto` commands can only go to a new function. Only the `goto` and `halt` statements actually take a step; the rest is done by recursion on statements and so take 0 steps. (There is a uniform bound on many statements can be executed before the next `goto`, so this is an `O(1)` speedup with the constant depending on the machine.) The `halt` command has a one step stutter before actually halting so that any changes made before the halt have a chance to be "committed", since the `eval` relation uses the final configuration before the halt as the output, and `move` and `write` etc. take 0 steps in this model. -/ namespace TM1 section parameters (Γ : Type*) [inhabited Γ] -- Type of tape symbols parameters (Λ : Type*) -- Type of function labels parameters (σ : Type*) -- Type of variable settings /-- The TM1 model is a simplification and extension of TM0 (Post-Turing model) in the direction of Wang B-machines. The machine's internal state is extended with a (finite) store `σ` of variables that may be accessed and updated at any time. A machine is given by a `Λ` indexed set of procedures or functions. Each function has a body which is a `stmt`, which can either be a `move` or `write` command, a `branch` (if statement based on the current tape value), a `load` (set the variable value), a `goto` (call another function), or `halt`. Note that here most statements do not have labels; `goto` commands can only go to a new function. All commands have access to the variable value and current tape value. -/ inductive stmt | move : dir → stmt → stmt | write : (Γ → σ → Γ) → stmt → stmt | load : (Γ → σ → σ) → stmt → stmt | branch : (Γ → σ → bool) → stmt → stmt → stmt | goto : (Γ → σ → Λ) → stmt | halt : stmt open stmt instance stmt.inhabited : inhabited stmt := ⟨halt⟩ /-- The configuration of a TM1 machine is given by the currently evaluating statement, the variable store value, and the tape. -/ structure cfg := (l : option Λ) (var : σ) (tape : tape Γ) instance cfg.inhabited [inhabited σ] : inhabited cfg := ⟨⟨default _, default _, default _⟩⟩ parameters {Γ Λ σ} /-- The semantics of TM1 evaluation. -/ def step_aux : stmt → σ → tape Γ → cfg | (move d q) v T := step_aux q v (T.move d) | (write a q) v T := step_aux q v (T.write (a T.1 v)) | (load s q) v T := step_aux q (s T.1 v) T | (branch p q₁ q₂) v T := cond (p T.1 v) (step_aux q₁ v T) (step_aux q₂ v T) | (goto l) v T := ⟨some (l T.1 v), v, T⟩ | halt v T := ⟨none, v, T⟩ /-- The state transition function. -/ def step (M : Λ → stmt) : cfg → option cfg | ⟨none, v, T⟩ := none | ⟨some l, v, T⟩ := some (step_aux (M l) v T) /-- A set `S` of labels supports the statement `q` if all the `goto` statements in `q` refer only to other functions in `S`. -/ def supports_stmt (S : finset Λ) : stmt → Prop | (move d q) := supports_stmt q | (write a q) := supports_stmt q | (load s q) := supports_stmt q | (branch p q₁ q₂) := supports_stmt q₁ ∧ supports_stmt q₂ | (goto l) := ∀ a v, l a v ∈ S | halt := true open_locale classical /-- The subterm closure of a statement. -/ noncomputable def stmts₁ : stmt → finset stmt | Q@(move d q) := insert Q (stmts₁ q) | Q@(write a q) := insert Q (stmts₁ q) | Q@(load s q) := insert Q (stmts₁ q) | Q@(branch p q₁ q₂) := insert Q (stmts₁ q₁ ∪ stmts₁ q₂) | Q := {Q} theorem stmts₁_self {q} : q ∈ stmts₁ q := by cases q; apply_rules [finset.mem_insert_self, finset.mem_singleton_self] theorem stmts₁_trans {q₁ q₂} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := begin intros h₁₂ q₀ h₀₁, induction q₂ with _ q IH _ q IH _ q IH; simp only [stmts₁] at h₁₂ ⊢; simp only [finset.mem_insert, finset.mem_union, finset.mem_singleton] at h₁₂, iterate 3 { rcases h₁₂ with rfl | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (IH h₁₂) } }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { rcases h₁₂ with rfl | h₁₂ | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (finset.mem_union_left _ $ IH₁ h₁₂) }, { exact finset.mem_insert_of_mem (finset.mem_union_right _ $ IH₂ h₁₂) } }, case TM1.stmt.goto : l { subst h₁₂, exact h₀₁ }, case TM1.stmt.halt { subst h₁₂, exact h₀₁ } end theorem stmts₁_supports_stmt_mono {S q₁ q₂} (h : q₁ ∈ stmts₁ q₂) (hs : supports_stmt S q₂) : supports_stmt S q₁ := begin induction q₂ with _ q IH _ q IH _ q IH; simp only [stmts₁, supports_stmt, finset.mem_insert, finset.mem_union, finset.mem_singleton] at h hs, iterate 3 { rcases h with rfl | h; [exact hs, exact IH h hs] }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { rcases h with rfl | h | h, exacts [hs, IH₁ h hs.1, IH₂ h hs.2] }, case TM1.stmt.goto : l { subst h, exact hs }, case TM1.stmt.halt { subst h, trivial } end /-- The set of all statements in a turing machine, plus one extra value `none` representing the halt state. This is used in the TM1 to TM0 reduction. -/ noncomputable def stmts (M : Λ → stmt) (S : finset Λ) : finset (option stmt) := (S.bUnion (λ q, stmts₁ (M q))).insert_none theorem stmts_trans {M : Λ → stmt} {S q₁ q₂} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := by simp only [stmts, finset.mem_insert_none, finset.mem_bUnion, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h₂, ⟨_, ls, stmts₁_trans h₂ h₁⟩ variable [inhabited Λ] /-- A set `S` of labels supports machine `M` if all the `goto` statements in the functions in `S` refer only to other functions in `S`. -/ def supports (M : Λ → stmt) (S : finset Λ) := default Λ ∈ S ∧ ∀ q ∈ S, supports_stmt S (M q) theorem stmts_supports_stmt {M : Λ → stmt} {S q} (ss : supports M S) : some q ∈ stmts M S → supports_stmt S q := by simp only [stmts, finset.mem_insert_none, finset.mem_bUnion, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h, stmts₁_supports_stmt_mono h (ss.2 _ ls) theorem step_supports (M : Λ → stmt) {S} (ss : supports M S) : ∀ {c c' : cfg}, c' ∈ step M c → c.l ∈ S.insert_none → c'.l ∈ S.insert_none | ⟨some l₁, v, T⟩ c' h₁ h₂ := begin replace h₂ := ss.2 _ (finset.some_mem_insert_none.1 h₂), simp only [step, option.mem_def] at h₁, subst c', revert h₂, induction M l₁ with _ q IH _ q IH _ q IH generalizing v T; intro hs, iterate 3 { exact IH _ _ hs }, case TM1.stmt.branch : p q₁' q₂' IH₁ IH₂ { unfold step_aux, cases p T.1 v, { exact IH₂ _ _ hs.2 }, { exact IH₁ _ _ hs.1 } }, case TM1.stmt.goto { exact finset.some_mem_insert_none.2 (hs _ _) }, case TM1.stmt.halt { apply multiset.mem_cons_self } end variable [inhabited σ] /-- The initial state, given a finite input that is placed on the tape starting at the TM head and going to the right. -/ def init (l : list Γ) : cfg := ⟨some (default _), default _, tape.mk₁ l⟩ /-- Evaluate a TM to completion, resulting in an output list on the tape (with an indeterminate number of blanks on the end). -/ def eval (M : Λ → stmt) (l : list Γ) : part (list_blank Γ) := (eval (step M) (init l)).map (λ c, c.tape.right₀) end end TM1 /-! ## TM1 emulator in TM0 To prove that TM1 computable functions are TM0 computable, we need to reduce each TM1 program to a TM0 program. So suppose a TM1 program is given. We take the following: * The alphabet `Γ` is the same for both TM1 and TM0 * The set of states `Λ'` is defined to be `option stmt₁ × σ`, that is, a TM1 statement or `none` representing halt, and the possible settings of the internal variables. Note that this is an infinite set, because `stmt₁` is infinite. This is okay because we assume that from the initial TM1 state, only finitely many other labels are reachable, and there are only finitely many statements that appear in all of these functions. Even though `stmt₁` contains a statement called `halt`, we must separate it from `none` (`some halt` steps to `none` and `none` actually halts) because there is a one step stutter in the TM1 semantics. -/ namespace TM1to0 section parameters {Γ : Type*} [inhabited Γ] parameters {Λ : Type*} [inhabited Λ] parameters {σ : Type*} [inhabited σ] local notation `stmt₁` := TM1.stmt Γ Λ σ local notation `cfg₁` := TM1.cfg Γ Λ σ local notation `stmt₀` := TM0.stmt Γ parameters (M : Λ → stmt₁) include M /-- The base machine state space is a pair of an `option stmt₁` representing the current program to be executed, or `none` for the halt state, and a `σ` which is the local state (stored in the TM, not the tape). Because there are an infinite number of programs, this state space is infinite, but for a finitely supported TM1 machine and a finite type `σ`, only finitely many of these states are reachable. -/ @[nolint unused_arguments] -- [inhabited Λ] [inhabited σ] (M : Λ → stmt₁): We need the M assumption -- because of the inhabited instance, but we could avoid the inhabited instances on Λ and σ here. -- But they are parameters so we cannot easily skip them for just this definition. def Λ' := option stmt₁ × σ instance : inhabited Λ' := ⟨(some (M (default _)), default _)⟩ open TM0.stmt /-- The core TM1 → TM0 translation function. Here `s` is the current value on the tape, and the `stmt₁` is the TM1 statement to translate, with local state `v : σ`. We evaluate all regular instructions recursively until we reach either a `move` or `write` command, or a `goto`; in the latter case we emit a dummy `write s` step and transition to the new target location. -/ def tr_aux (s : Γ) : stmt₁ → σ → Λ' × stmt₀ | (TM1.stmt.move d q) v := ((some q, v), move d) | (TM1.stmt.write a q) v := ((some q, v), write (a s v)) | (TM1.stmt.load a q) v := tr_aux q (a s v) | (TM1.stmt.branch p q₁ q₂) v := cond (p s v) (tr_aux q₁ v) (tr_aux q₂ v) | (TM1.stmt.goto l) v := ((some (M (l s v)), v), write s) | TM1.stmt.halt v := ((none, v), write s) local notation `cfg₀` := TM0.cfg Γ Λ' /-- The translated TM0 machine (given the TM1 machine input). -/ def tr : TM0.machine Γ Λ' | (none, v) s := none | (some q, v) s := some (tr_aux s q v) /-- Translate configurations from TM1 to TM0. -/ def tr_cfg : cfg₁ → cfg₀ | ⟨l, v, T⟩ := ⟨(l.map M, v), T⟩ theorem tr_respects : respects (TM1.step M) (TM0.step tr) (λ c₁ c₂, tr_cfg c₁ = c₂) := fun_respects.2 $ λ ⟨l₁, v, T⟩, begin cases l₁ with l₁, {exact rfl}, unfold tr_cfg TM1.step frespects option.map function.comp option.bind, induction M l₁ with _ q IH _ q IH _ q IH generalizing v T, case TM1.stmt.move : d q IH { exact trans_gen.head rfl (IH _ _) }, case TM1.stmt.write : a q IH { exact trans_gen.head rfl (IH _ _) }, case TM1.stmt.load : a q IH { exact (reaches₁_eq (by refl)).2 (IH _ _) }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { unfold TM1.step_aux, cases e : p T.1 v, { exact (reaches₁_eq (by simp only [TM0.step, tr, tr_aux, e]; refl)).2 (IH₂ _ _) }, { exact (reaches₁_eq (by simp only [TM0.step, tr, tr_aux, e]; refl)).2 (IH₁ _ _) } }, iterate 2 { exact trans_gen.single (congr_arg some (congr (congr_arg TM0.cfg.mk rfl) (tape.write_self T))) } end theorem tr_eval (l : list Γ) : TM0.eval tr l = TM1.eval M l := (congr_arg _ (tr_eval' _ _ _ tr_respects ⟨some _, _, _⟩)).trans begin rw [part.map_eq_map, part.map_map, TM1.eval], congr' with ⟨⟩, refl end variables [fintype σ] /-- Given a finite set of accessible `Λ` machine states, there is a finite set of accessible machine states in the target (even though the type `Λ'` is infinite). -/ noncomputable def tr_stmts (S : finset Λ) : finset Λ' := (TM1.stmts M S).product finset.univ open_locale classical local attribute [simp] TM1.stmts₁_self theorem tr_supports {S : finset Λ} (ss : TM1.supports M S) : TM0.supports tr (↑(tr_stmts S)) := ⟨finset.mem_product.2 ⟨finset.some_mem_insert_none.2 (finset.mem_bUnion.2 ⟨_, ss.1, TM1.stmts₁_self⟩), finset.mem_univ _⟩, λ q a q' s h₁ h₂, begin rcases q with ⟨_|q, v⟩, {cases h₁}, cases q' with q' v', simp only [tr_stmts, finset.mem_coe, finset.mem_product, finset.mem_univ, and_true] at h₂ ⊢, cases q', {exact multiset.mem_cons_self _ _}, simp only [tr, option.mem_def] at h₁, have := TM1.stmts_supports_stmt ss h₂, revert this, induction q generalizing v; intro hs, case TM1.stmt.move : d q { cases h₁, refine TM1.stmts_trans _ h₂, unfold TM1.stmts₁, exact finset.mem_insert_of_mem TM1.stmts₁_self }, case TM1.stmt.write : b q { cases h₁, refine TM1.stmts_trans _ h₂, unfold TM1.stmts₁, exact finset.mem_insert_of_mem TM1.stmts₁_self }, case TM1.stmt.load : b q IH { refine IH (TM1.stmts_trans _ h₂) _ h₁ hs, unfold TM1.stmts₁, exact finset.mem_insert_of_mem TM1.stmts₁_self }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { change cond (p a v) _ _ = ((some q', v'), s) at h₁, cases p a v, { refine IH₂ (TM1.stmts_trans _ h₂) _ h₁ hs.2, unfold TM1.stmts₁, exact finset.mem_insert_of_mem (finset.mem_union_right _ TM1.stmts₁_self) }, { refine IH₁ (TM1.stmts_trans _ h₂) _ h₁ hs.1, unfold TM1.stmts₁, exact finset.mem_insert_of_mem (finset.mem_union_left _ TM1.stmts₁_self) } }, case TM1.stmt.goto : l { cases h₁, exact finset.some_mem_insert_none.2 (finset.mem_bUnion.2 ⟨_, hs _ _, TM1.stmts₁_self⟩) }, case TM1.stmt.halt { cases h₁ } end⟩ end end TM1to0 /-! ## TM1(Γ) emulator in TM1(bool) The most parsimonious Turing machine model that is still Turing complete is `TM0` with `Γ = bool`. Because our construction in the previous section reducing `TM1` to `TM0` doesn't change the alphabet, we can do the alphabet reduction on `TM1` instead of `TM0` directly. The basic idea is to use a bijection between `Γ` and a subset of `vector bool n`, where `n` is a fixed constant. Each tape element is represented as a block of `n` bools. Whenever the machine wants to read a symbol from the tape, it traverses over the block, performing `n` `branch` instructions to each any of the `2^n` results. For the `write` instruction, we have to use a `goto` because we need to follow a different code path depending on the local state, which is not available in the TM1 model, so instead we jump to a label computed using the read value and the local state, which performs the writing and returns to normal execution. Emulation overhead is `O(1)`. If not for the above `write` behavior it would be 1-1 because we are exploiting the 0-step behavior of regular commands to avoid taking steps, but there are nevertheless a bounded number of `write` calls between `goto` statements because TM1 statements are finitely long. -/ namespace TM1to1 open TM1 section parameters {Γ : Type*} [inhabited Γ] theorem exists_enc_dec [fintype Γ] : ∃ n (enc : Γ → vector bool n) (dec : vector bool n → Γ), enc (default _) = vector.repeat ff n ∧ ∀ a, dec (enc a) = a := begin letI := classical.dec_eq Γ, let n := fintype.card Γ, obtain ⟨F⟩ := fintype.trunc_equiv_fin Γ, let G : fin n ↪ fin n → bool := ⟨λ a b, a = b, λ a b h, of_to_bool_true $ (congr_fun h b).trans $ to_bool_tt rfl⟩, let H := (F.to_embedding.trans G).trans (equiv.vector_equiv_fin _ _).symm.to_embedding, classical, let enc := H.set_value (default _) (vector.repeat ff n), exact ⟨_, enc, function.inv_fun enc, H.set_value_eq _ _, function.left_inverse_inv_fun enc.2⟩ end parameters {Λ : Type*} [inhabited Λ] parameters {σ : Type*} [inhabited σ] local notation `stmt₁` := stmt Γ Λ σ local notation `cfg₁` := cfg Γ Λ σ /-- The configuration state of the TM. -/ inductive Λ' : Type (max u_1 u_2 u_3) | normal : Λ → Λ' | write : Γ → stmt₁ → Λ' instance : inhabited Λ' := ⟨Λ'.normal (default _)⟩ local notation `stmt'` := stmt bool Λ' σ local notation `cfg'` := cfg bool Λ' σ /-- Read a vector of length `n` from the tape. -/ def read_aux : ∀ n, (vector bool n → stmt') → stmt' | 0 f := f vector.nil | (i+1) f := stmt.branch (λ a s, a) (stmt.move dir.right $ read_aux i (λ v, f (tt ::ᵥ v))) (stmt.move dir.right $ read_aux i (λ v, f (ff ::ᵥ v))) parameters {n : ℕ} (enc : Γ → vector bool n) (dec : vector bool n → Γ) /-- A move left or right corresponds to `n` moves across the super-cell. -/ def move (d : dir) (q : stmt') : stmt' := (stmt.move d)^[n] q /-- To read a symbol from the tape, we use `read_aux` to traverse the symbol, then return to the original position with `n` moves to the left. -/ def read (f : Γ → stmt') : stmt' := read_aux n (λ v, move dir.left $ f (dec v)) /-- Write a list of bools on the tape. -/ def write : list bool → stmt' → stmt' | [] q := q | (a :: l) q := stmt.write (λ _ _, a) $ stmt.move dir.right $ write l q /-- Translate a normal instruction. For the `write` command, we use a `goto` indirection so that we can access the current value of the tape. -/ def tr_normal : stmt₁ → stmt' | (stmt.move d q) := move d $ tr_normal q | (stmt.write f q) := read $ λ a, stmt.goto $ λ _ s, Λ'.write (f a s) q | (stmt.load f q) := read $ λ a, stmt.load (λ _ s, f a s) $ tr_normal q | (stmt.branch p q₁ q₂) := read $ λ a, stmt.branch (λ _ s, p a s) (tr_normal q₁) (tr_normal q₂) | (stmt.goto l) := read $ λ a, stmt.goto $ λ _ s, Λ'.normal (l a s) | stmt.halt := stmt.halt theorem step_aux_move (d q v T) : step_aux (move d q) v T = step_aux q v ((tape.move d)^[n] T) := begin suffices : ∀ i, step_aux (stmt.move d^[i] q) v T = step_aux q v (tape.move d^[i] T), from this n, intro, induction i with i IH generalizing T, {refl}, rw [iterate_succ', step_aux, IH, iterate_succ] end theorem supports_stmt_move {S d q} : supports_stmt S (move d q) = supports_stmt S q := suffices ∀ {i}, supports_stmt S (stmt.move d^[i] q) = _, from this, by intro; induction i generalizing q; simp only [*, iterate]; refl theorem supports_stmt_write {S l q} : supports_stmt S (write l q) = supports_stmt S q := by induction l with a l IH; simp only [write, supports_stmt, *] theorem supports_stmt_read {S} : ∀ {f : Γ → stmt'}, (∀ a, supports_stmt S (f a)) → supports_stmt S (read f) := suffices ∀ i (f : vector bool i → stmt'), (∀ v, supports_stmt S (f v)) → supports_stmt S (read_aux i f), from λ f hf, this n _ (by intro; simp only [supports_stmt_move, hf]), λ i f hf, begin induction i with i IH, {exact hf _}, split; apply IH; intro; apply hf, end parameter (enc0 : enc (default _) = vector.repeat ff n) section parameter {enc} include enc0 /-- The low level tape corresponding to the given tape over alphabet `Γ`. -/ def tr_tape' (L R : list_blank Γ) : tape bool := begin refine tape.mk' (L.bind (λ x, (enc x).to_list.reverse) ⟨n, _⟩) (R.bind (λ x, (enc x).to_list) ⟨n, _⟩); simp only [enc0, vector.repeat, list.reverse_repeat, bool.default_bool, vector.to_list_mk] end /-- The low level tape corresponding to the given tape over alphabet `Γ`. -/ def tr_tape (T : tape Γ) : tape bool := tr_tape' T.left T.right₀ theorem tr_tape_mk' (L R : list_blank Γ) : tr_tape (tape.mk' L R) = tr_tape' L R := by simp only [tr_tape, tape.mk'_left, tape.mk'_right₀] end parameters (M : Λ → stmt₁) /-- The top level program. -/ def tr : Λ' → stmt' | (Λ'.normal l) := tr_normal (M l) | (Λ'.write a q) := write (enc a).to_list $ move dir.left $ tr_normal q /-- The machine configuration translation. -/ def tr_cfg : cfg₁ → cfg' | ⟨l, v, T⟩ := ⟨l.map Λ'.normal, v, tr_tape T⟩ parameter {enc} include enc0 theorem tr_tape'_move_left (L R) : (tape.move dir.left)^[n] (tr_tape' L R) = (tr_tape' L.tail (R.cons L.head)) := begin obtain ⟨a, L, rfl⟩ := L.exists_cons, simp only [tr_tape', list_blank.cons_bind, list_blank.head_cons, list_blank.tail_cons], suffices : ∀ {L' R' l₁ l₂} (e : vector.to_list (enc a) = list.reverse_core l₁ l₂), tape.move dir.left^[l₁.length] (tape.mk' (list_blank.append l₁ L') (list_blank.append l₂ R')) = tape.mk' L' (list_blank.append (vector.to_list (enc a)) R'), { simpa only [list.length_reverse, vector.to_list_length] using this (list.reverse_reverse _).symm }, intros, induction l₁ with b l₁ IH generalizing l₂, { cases e, refl }, simp only [list.length, list.cons_append, iterate_succ_apply], convert IH e, simp only [list_blank.tail_cons, list_blank.append, tape.move_left_mk', list_blank.head_cons] end theorem tr_tape'_move_right (L R) : (tape.move dir.right)^[n] (tr_tape' L R) = (tr_tape' (L.cons R.head) R.tail) := begin suffices : ∀ i L, (tape.move dir.right)^[i] ((tape.move dir.left)^[i] L) = L, { refine (eq.symm _).trans (this n _), simp only [tr_tape'_move_left, list_blank.cons_head_tail, list_blank.head_cons, list_blank.tail_cons] }, intros, induction i with i IH, {refl}, rw [iterate_succ_apply, iterate_succ_apply', tape.move_left_right, IH] end theorem step_aux_write (q v a b L R) : step_aux (write (enc a).to_list q) v (tr_tape' L (list_blank.cons b R)) = step_aux q v (tr_tape' (list_blank.cons a L) R) := begin simp only [tr_tape', list.cons_bind, list.append_assoc], suffices : ∀ {L' R'} (l₁ l₂ l₂' : list bool) (e : l₂'.length = l₂.length), step_aux (write l₂ q) v (tape.mk' (list_blank.append l₁ L') (list_blank.append l₂' R')) = step_aux q v (tape.mk' (L'.append (list.reverse_core l₂ l₁)) R'), { convert this [] _ _ ((enc b).2.trans (enc a).2.symm); rw list_blank.cons_bind; refl }, clear a b L R, intros, induction l₂ with a l₂ IH generalizing l₁ l₂', { cases list.length_eq_zero.1 e, refl }, cases l₂' with b l₂'; injection e with e, dunfold write step_aux, convert IH _ _ e using 1, simp only [list_blank.head_cons, list_blank.tail_cons, list_blank.append, tape.move_right_mk', tape.write_mk'] end parameters (encdec : ∀ a, dec (enc a) = a) include encdec theorem step_aux_read (f v L R) : step_aux (read f) v (tr_tape' L R) = step_aux (f R.head) v (tr_tape' L R) := begin suffices : ∀ f, step_aux (read_aux n f) v (tr_tape' enc0 L R) = step_aux (f (enc R.head)) v (tr_tape' enc0 (L.cons R.head) R.tail), { rw [read, this, step_aux_move, encdec, tr_tape'_move_left enc0], simp only [list_blank.head_cons, list_blank.cons_head_tail, list_blank.tail_cons] }, obtain ⟨a, R, rfl⟩ := R.exists_cons, simp only [list_blank.head_cons, list_blank.tail_cons, tr_tape', list_blank.cons_bind, list_blank.append_assoc], suffices : ∀ i f L' R' l₁ l₂ h, step_aux (read_aux i f) v (tape.mk' (list_blank.append l₁ L') (list_blank.append l₂ R')) = step_aux (f ⟨l₂, h⟩) v (tape.mk' (list_blank.append (l₂.reverse_core l₁) L') R'), { intro f, convert this n f _ _ _ _ (enc a).2; simp }, clear f L a R, intros, subst i, induction l₂ with a l₂ IH generalizing l₁, {refl}, transitivity step_aux (read_aux l₂.length (λ v, f (a ::ᵥ v))) v (tape.mk' ((L'.append l₁).cons a) (R'.append l₂)), { dsimp [read_aux, step_aux], simp, cases a; refl }, rw [← list_blank.append, IH], refl end theorem tr_respects : respects (step M) (step tr) (λ c₁ c₂, tr_cfg c₁ = c₂) := fun_respects.2 $ λ ⟨l₁, v, T⟩, begin obtain ⟨L, R, rfl⟩ := T.exists_mk', cases l₁ with l₁, {exact rfl}, suffices : ∀ q R, reaches (step (tr enc dec M)) (step_aux (tr_normal dec q) v (tr_tape' enc0 L R)) (tr_cfg enc0 (step_aux q v (tape.mk' L R))), { refine trans_gen.head' rfl _, rw tr_tape_mk', exact this _ R }, clear R l₁, intros, induction q with _ q IH _ q IH _ q IH generalizing v L R, case TM1.stmt.move : d q IH { cases d; simp only [tr_normal, iterate, step_aux_move, step_aux, list_blank.head_cons, tape.move_left_mk', list_blank.cons_head_tail, list_blank.tail_cons, tr_tape'_move_left enc0, tr_tape'_move_right enc0]; apply IH }, case TM1.stmt.write : f q IH { simp only [tr_normal, step_aux_read dec enc0 encdec, step_aux], refine refl_trans_gen.head rfl _, obtain ⟨a, R, rfl⟩ := R.exists_cons, rw [tr, tape.mk'_head, step_aux_write, list_blank.head_cons, step_aux_move, tr_tape'_move_left enc0, list_blank.head_cons, list_blank.tail_cons, tape.write_mk'], apply IH }, case TM1.stmt.load : a q IH { simp only [tr_normal, step_aux_read dec enc0 encdec], apply IH }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { simp only [tr_normal, step_aux_read dec enc0 encdec, step_aux], cases p R.head v; [apply IH₂, apply IH₁] }, case TM1.stmt.goto : l { simp only [tr_normal, step_aux_read dec enc0 encdec, step_aux, tr_cfg, tr_tape_mk'], apply refl_trans_gen.refl }, case TM1.stmt.halt { simp only [tr_normal, step_aux, tr_cfg, step_aux_move, tr_tape'_move_left enc0, tr_tape'_move_right enc0, tr_tape_mk'], apply refl_trans_gen.refl } end omit enc0 encdec open_locale classical parameters [fintype Γ] /-- The set of accessible `Λ'.write` machine states. -/ noncomputable def writes : stmt₁ → finset Λ' | (stmt.move d q) := writes q | (stmt.write f q) := finset.univ.image (λ a, Λ'.write a q) ∪ writes q | (stmt.load f q) := writes q | (stmt.branch p q₁ q₂) := writes q₁ ∪ writes q₂ | (stmt.goto l) := ∅ | stmt.halt := ∅ /-- The set of accessible machine states, assuming that the input machine is supported on `S`, are the normal states embedded from `S`, plus all write states accessible from these states. -/ noncomputable def tr_supp (S : finset Λ) : finset Λ' := S.bUnion (λ l, insert (Λ'.normal l) (writes (M l))) theorem tr_supports {S} (ss : supports M S) : supports tr (tr_supp S) := ⟨finset.mem_bUnion.2 ⟨_, ss.1, finset.mem_insert_self _ _⟩, λ q h, begin suffices : ∀ q, supports_stmt S q → (∀ q' ∈ writes q, q' ∈ tr_supp M S) → supports_stmt (tr_supp M S) (tr_normal dec q) ∧ ∀ q' ∈ writes q, supports_stmt (tr_supp M S) (tr enc dec M q'), { rcases finset.mem_bUnion.1 h with ⟨l, hl, h⟩, have := this _ (ss.2 _ hl) (λ q' hq, finset.mem_bUnion.2 ⟨_, hl, finset.mem_insert_of_mem hq⟩), rcases finset.mem_insert.1 h with rfl | h, exacts [this.1, this.2 _ h] }, intros q hs hw, induction q, case TM1.stmt.move : d q IH { unfold writes at hw ⊢, replace IH := IH hs hw, refine ⟨_, IH.2⟩, cases d; simp only [tr_normal, iterate, supports_stmt_move, IH] }, case TM1.stmt.write : f q IH { unfold writes at hw ⊢, simp only [finset.mem_image, finset.mem_union, finset.mem_univ, exists_prop, true_and] at hw ⊢, replace IH := IH hs (λ q hq, hw q (or.inr hq)), refine ⟨supports_stmt_read _ $ λ a _ s, hw _ (or.inl ⟨_, rfl⟩), λ q' hq, _⟩, rcases hq with ⟨a, q₂, rfl⟩ | hq, { simp only [tr, supports_stmt_write, supports_stmt_move, IH.1] }, { exact IH.2 _ hq } }, case TM1.stmt.load : a q IH { unfold writes at hw ⊢, replace IH := IH hs hw, refine ⟨supports_stmt_read _ (λ a, IH.1), IH.2⟩ }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { unfold writes at hw ⊢, simp only [finset.mem_union] at hw ⊢, replace IH₁ := IH₁ hs.1 (λ q hq, hw q (or.inl hq)), replace IH₂ := IH₂ hs.2 (λ q hq, hw q (or.inr hq)), exact ⟨supports_stmt_read _ (λ a, ⟨IH₁.1, IH₂.1⟩), λ q, or.rec (IH₁.2 _) (IH₂.2 _)⟩ }, case TM1.stmt.goto : l { refine ⟨_, λ _, false.elim⟩, refine supports_stmt_read _ (λ a _ s, _), exact finset.mem_bUnion.2 ⟨_, hs _ _, finset.mem_insert_self _ _⟩ }, case TM1.stmt.halt { refine ⟨_, λ _, false.elim⟩, simp only [supports_stmt, supports_stmt_move, tr_normal] } end⟩ end end TM1to1 /-! ## TM0 emulator in TM1 To establish that TM0 and TM1 are equivalent computational models, we must also have a TM0 emulator in TM1. The main complication here is that TM0 allows an action to depend on the value at the head and local state, while TM1 doesn't (in order to have more programming language-like semantics). So we use a computed `goto` to go to a state that performes the desired action and then returns to normal execution. One issue with this is that the `halt` instruction is supposed to halt immediately, not take a step to a halting state. To resolve this we do a check for `halt` first, then `goto` (with an unreachable branch). -/ namespace TM0to1 section parameters {Γ : Type*} [inhabited Γ] parameters {Λ : Type*} [inhabited Λ] /-- The machine states for a TM1 emulating a TM0 machine. States of the TM0 machine are embedded as `normal q` states, but the actual operation is split into two parts, a jump to `act s q` followed by the action and a jump to the next `normal` state. -/ inductive Λ' | normal : Λ → Λ' | act : TM0.stmt Γ → Λ → Λ' instance : inhabited Λ' := ⟨Λ'.normal (default _)⟩ local notation `cfg₀` := TM0.cfg Γ Λ local notation `stmt₁` := TM1.stmt Γ Λ' unit local notation `cfg₁` := TM1.cfg Γ Λ' unit parameters (M : TM0.machine Γ Λ) open TM1.stmt /-- The program. -/ def tr : Λ' → stmt₁ | (Λ'.normal q) := branch (λ a _, (M q a).is_none) halt $ goto (λ a _, match M q a with | none := default _ -- unreachable | some (q', s) := Λ'.act s q' end) | (Λ'.act (TM0.stmt.move d) q) := move d $ goto (λ _ _, Λ'.normal q) | (Λ'.act (TM0.stmt.write a) q) := write (λ _ _, a) $ goto (λ _ _, Λ'.normal q) /-- The configuration translation. -/ def tr_cfg : cfg₀ → cfg₁ | ⟨q, T⟩ := ⟨cond (M q T.1).is_some (some (Λ'.normal q)) none, (), T⟩ theorem tr_respects : respects (TM0.step M) (TM1.step tr) (λ a b, tr_cfg a = b) := fun_respects.2 $ λ ⟨q, T⟩, begin cases e : M q T.1, { simp only [TM0.step, tr_cfg, e]; exact eq.refl none }, cases val with q' s, simp only [frespects, TM0.step, tr_cfg, e, option.is_some, cond, option.map_some'], have : TM1.step (tr M) ⟨some (Λ'.act s q'), (), T⟩ = some ⟨some (Λ'.normal q'), (), TM0.step._match_1 T s⟩, { cases s with d a; refl }, refine trans_gen.head _ (trans_gen.head' this _), { unfold TM1.step TM1.step_aux tr has_mem.mem, rw e, refl }, cases e' : M q' _, { apply refl_trans_gen.single, unfold TM1.step TM1.step_aux tr has_mem.mem, rw e', refl }, { refl } end end end TM0to1 /-! ## The TM2 model The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks, each with elements of different types (the alphabet of stack `k : K` is `Γ k`). The statements are: * `push k (f : σ → Γ k) q` puts `f a` on the `k`-th stack, then does `q`. * `pop k (f : σ → option (Γ k) → σ) q` changes the state to `f a (S k).head`, where `S k` is the value of the `k`-th stack, and removes this element from the stack, then does `q`. * `peek k (f : σ → option (Γ k) → σ) q` changes the state to `f a (S k).head`, where `S k` is the value of the `k`-th stack, then does `q`. * `load (f : σ → σ) q` reads nothing but applies `f` to the internal state, then does `q`. * `branch (f : σ → bool) qtrue qfalse` does `qtrue` or `qfalse` according to `f a`. * `goto (f : σ → Λ)` jumps to label `f a`. * `halt` halts on the next step. The configuration is a tuple `(l, var, stk)` where `l : option Λ` is the current label to run or `none` for the halting state, `var : σ` is the (finite) internal state, and `stk : ∀ k, list (Γ k)` is the collection of stacks. (Note that unlike the `TM0` and `TM1` models, these are not `list_blank`s, they have definite ends that can be detected by the `pop` command.) Given a designated stack `k` and a value `L : list (Γ k)`, the initial configuration has all the stacks empty except the designated "input" stack; in `eval` this designated stack also functions as the output stack. -/ namespace TM2 section parameters {K : Type*} [decidable_eq K] -- Index type of stacks parameters (Γ : K → Type*) -- Type of stack elements parameters (Λ : Type*) -- Type of function labels parameters (σ : Type*) -- Type of variable settings /-- The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks. The operation `push` puts an element on one of the stacks, and `pop` removes an element from a stack (and modifying the internal state based on the result). `peek` modifies the internal state but does not remove an element. -/ inductive stmt | push : ∀ k, (σ → Γ k) → stmt → stmt | peek : ∀ k, (σ → option (Γ k) → σ) → stmt → stmt | pop : ∀ k, (σ → option (Γ k) → σ) → stmt → stmt | load : (σ → σ) → stmt → stmt | branch : (σ → bool) → stmt → stmt → stmt | goto : (σ → Λ) → stmt | halt : stmt open stmt instance stmt.inhabited : inhabited stmt := ⟨halt⟩ /-- A configuration in the TM2 model is a label (or `none` for the halt state), the state of local variables, and the stacks. (Note that the stacks are not `list_blank`s, they have a definite size.) -/ structure cfg := (l : option Λ) (var : σ) (stk : ∀ k, list (Γ k)) instance cfg.inhabited [inhabited σ] : inhabited cfg := ⟨⟨default _, default _, default _⟩⟩ parameters {Γ Λ σ K} /-- The step function for the TM2 model. -/ @[simp] def step_aux : stmt → σ → (∀ k, list (Γ k)) → cfg | (push k f q) v S := step_aux q v (update S k (f v :: S k)) | (peek k f q) v S := step_aux q (f v (S k).head') S | (pop k f q) v S := step_aux q (f v (S k).head') (update S k (S k).tail) | (load a q) v S := step_aux q (a v) S | (branch f q₁ q₂) v S := cond (f v) (step_aux q₁ v S) (step_aux q₂ v S) | (goto f) v S := ⟨some (f v), v, S⟩ | halt v S := ⟨none, v, S⟩ /-- The step function for the TM2 model. -/ @[simp] def step (M : Λ → stmt) : cfg → option cfg | ⟨none, v, S⟩ := none | ⟨some l, v, S⟩ := some (step_aux (M l) v S) /-- The (reflexive) reachability relation for the TM2 model. -/ def reaches (M : Λ → stmt) : cfg → cfg → Prop := refl_trans_gen (λ a b, b ∈ step M a) /-- Given a set `S` of states, `support_stmt S q` means that `q` only jumps to states in `S`. -/ def supports_stmt (S : finset Λ) : stmt → Prop | (push k f q) := supports_stmt q | (peek k f q) := supports_stmt q | (pop k f q) := supports_stmt q | (load a q) := supports_stmt q | (branch f q₁ q₂) := supports_stmt q₁ ∧ supports_stmt q₂ | (goto l) := ∀ v, l v ∈ S | halt := true open_locale classical /-- The set of subtree statements in a statement. -/ noncomputable def stmts₁ : stmt → finset stmt | Q@(push k f q) := insert Q (stmts₁ q) | Q@(peek k f q) := insert Q (stmts₁ q) | Q@(pop k f q) := insert Q (stmts₁ q) | Q@(load a q) := insert Q (stmts₁ q) | Q@(branch f q₁ q₂) := insert Q (stmts₁ q₁ ∪ stmts₁ q₂) | Q@(goto l) := {Q} | Q@halt := {Q} theorem stmts₁_self {q} : q ∈ stmts₁ q := by cases q; apply_rules [finset.mem_insert_self, finset.mem_singleton_self] theorem stmts₁_trans {q₁ q₂} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := begin intros h₁₂ q₀ h₀₁, induction q₂ with _ _ q IH _ _ q IH _ _ q IH _ q IH; simp only [stmts₁] at h₁₂ ⊢; simp only [finset.mem_insert, finset.mem_singleton, finset.mem_union] at h₁₂, iterate 4 { rcases h₁₂ with rfl | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (IH h₁₂) } }, case TM2.stmt.branch : f q₁ q₂ IH₁ IH₂ { rcases h₁₂ with rfl | h₁₂ | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (finset.mem_union_left _ (IH₁ h₁₂)) }, { exact finset.mem_insert_of_mem (finset.mem_union_right _ (IH₂ h₁₂)) } }, case TM2.stmt.goto : l { subst h₁₂, exact h₀₁ }, case TM2.stmt.halt { subst h₁₂, exact h₀₁ } end theorem stmts₁_supports_stmt_mono {S q₁ q₂} (h : q₁ ∈ stmts₁ q₂) (hs : supports_stmt S q₂) : supports_stmt S q₁ := begin induction q₂ with _ _ q IH _ _ q IH _ _ q IH _ q IH; simp only [stmts₁, supports_stmt, finset.mem_insert, finset.mem_union, finset.mem_singleton] at h hs, iterate 4 { rcases h with rfl | h; [exact hs, exact IH h hs] }, case TM2.stmt.branch : f q₁ q₂ IH₁ IH₂ { rcases h with rfl | h | h, exacts [hs, IH₁ h hs.1, IH₂ h hs.2] }, case TM2.stmt.goto : l { subst h, exact hs }, case TM2.stmt.halt { subst h, trivial } end /-- The set of statements accessible from initial set `S` of labels. -/ noncomputable def stmts (M : Λ → stmt) (S : finset Λ) : finset (option stmt) := (S.bUnion (λ q, stmts₁ (M q))).insert_none theorem stmts_trans {M : Λ → stmt} {S q₁ q₂} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := by simp only [stmts, finset.mem_insert_none, finset.mem_bUnion, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h₂, ⟨_, ls, stmts₁_trans h₂ h₁⟩ variable [inhabited Λ] /-- Given a TM2 machine `M` and a set `S` of states, `supports M S` means that all states in `S` jump only to other states in `S`. -/ def supports (M : Λ → stmt) (S : finset Λ) := default Λ ∈ S ∧ ∀ q ∈ S, supports_stmt S (M q) theorem stmts_supports_stmt {M : Λ → stmt} {S q} (ss : supports M S) : some q ∈ stmts M S → supports_stmt S q := by simp only [stmts, finset.mem_insert_none, finset.mem_bUnion, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h, stmts₁_supports_stmt_mono h (ss.2 _ ls) theorem step_supports (M : Λ → stmt) {S} (ss : supports M S) : ∀ {c c' : cfg}, c' ∈ step M c → c.l ∈ S.insert_none → c'.l ∈ S.insert_none | ⟨some l₁, v, T⟩ c' h₁ h₂ := begin replace h₂ := ss.2 _ (finset.some_mem_insert_none.1 h₂), simp only [step, option.mem_def] at h₁, subst c', revert h₂, induction M l₁ with _ _ q IH _ _ q IH _ _ q IH _ q IH generalizing v T; intro hs, iterate 4 { exact IH _ _ hs }, case TM2.stmt.branch : p q₁' q₂' IH₁ IH₂ { unfold step_aux, cases p v, { exact IH₂ _ _ hs.2 }, { exact IH₁ _ _ hs.1 } }, case TM2.stmt.goto { exact finset.some_mem_insert_none.2 (hs _) }, case TM2.stmt.halt { apply multiset.mem_cons_self } end variable [inhabited σ] /-- The initial state of the TM2 model. The input is provided on a designated stack. -/ def init (k) (L : list (Γ k)) : cfg := ⟨some (default _), default _, update (λ _, []) k L⟩ /-- Evaluates a TM2 program to completion, with the output on the same stack as the input. -/ def eval (M : Λ → stmt) (k) (L : list (Γ k)) : part (list (Γ k)) := (eval (step M) (init k L)).map $ λ c, c.stk k end end TM2 /-! ## TM2 emulator in TM1 To prove that TM2 computable functions are TM1 computable, we need to reduce each TM2 program to a TM1 program. So suppose a TM2 program is given. This program has to maintain a whole collection of stacks, but we have only one tape, so we must "multiplex" them all together. Pictorially, if stack 1 contains `[a, b]` and stack 2 contains `[c, d, e, f]` then the tape looks like this: ``` bottom: ... | _ | T | _ | _ | _ | _ | ... stack 1: ... | _ | b | a | _ | _ | _ | ... stack 2: ... | _ | f | e | d | c | _ | ... ``` where a tape element is a vertical slice through the diagram. Here the alphabet is `Γ' := bool × ∀ k, option (Γ k)`, where: * `bottom : bool` is marked only in one place, the initial position of the TM, and represents the tail of all stacks. It is never modified. * `stk k : option (Γ k)` is the value of the `k`-th stack, if in range, otherwise `none` (which is the blank value). Note that the head of the stack is at the far end; this is so that push and pop don't have to do any shifting. In "resting" position, the TM is sitting at the position marked `bottom`. For non-stack actions, it operates in place, but for the stack actions `push`, `peek`, and `pop`, it must shuttle to the end of the appropriate stack, make its changes, and then return to the bottom. So the states are: * `normal (l : Λ)`: waiting at `bottom` to execute function `l` * `go k (s : st_act k) (q : stmt₂)`: travelling to the right to get to the end of stack `k` in order to perform stack action `s`, and later continue with executing `q` * `ret (q : stmt₂)`: travelling to the left after having performed a stack action, and executing `q` once we arrive Because of the shuttling, emulation overhead is `O(n)`, where `n` is the current maximum of the length of all stacks. Therefore a program that takes `k` steps to run in TM2 takes `O((m+k)k)` steps to run when emulated in TM1, where `m` is the length of the input. -/ namespace TM2to1 -- A displaced lemma proved in unnecessary generality theorem stk_nth_val {K : Type*} {Γ : K → Type*} {L : list_blank (∀ k, option (Γ k))} {k S} (n) (hL : list_blank.map (proj k) L = list_blank.mk (list.map some S).reverse) : L.nth n k = S.reverse.nth n := begin rw [← proj_map_nth, hL, ← list.map_reverse, list_blank.nth_mk, list.inth, list.nth_map], cases S.reverse.nth n; refl end section parameters {K : Type*} [decidable_eq K] parameters {Γ : K → Type*} parameters {Λ : Type*} [inhabited Λ] parameters {σ : Type*} [inhabited σ] local notation `stmt₂` := TM2.stmt Γ Λ σ local notation `cfg₂` := TM2.cfg Γ Λ σ /-- The alphabet of the TM2 simulator on TM1 is a marker for the stack bottom, plus a vector of stack elements for each stack, or none if the stack does not extend this far. -/ @[nolint unused_arguments] -- [decidable_eq K]: Because K is a parameter, we cannot easily skip -- the decidable_eq assumption, and this is a local definition anyway so it's not important. def Γ' := bool × ∀ k, option (Γ k) instance Γ'.inhabited : inhabited Γ' := ⟨⟨ff, λ _, none⟩⟩ instance Γ'.fintype [fintype K] [∀ k, fintype (Γ k)] : fintype Γ' := prod.fintype _ _ /-- The bottom marker is fixed throughout the calculation, so we use the `add_bottom` function to express the program state in terms of a tape with only the stacks themselves. -/ def add_bottom (L : list_blank (∀ k, option (Γ k))) : list_blank Γ' := list_blank.cons (tt, L.head) (L.tail.map ⟨prod.mk ff, rfl⟩) theorem add_bottom_map (L) : (add_bottom L).map ⟨prod.snd, rfl⟩ = L := begin simp only [add_bottom, list_blank.map_cons]; convert list_blank.cons_head_tail _, generalize : list_blank.tail L = L', refine L'.induction_on _, intro l, simp, rw (_ : _ ∘ _ = id), {simp}, funext a, refl end theorem add_bottom_modify_nth (f : (∀ k, option (Γ k)) → (∀ k, option (Γ k))) (L n) : (add_bottom L).modify_nth (λ a, (a.1, f a.2)) n = add_bottom (L.modify_nth f n) := begin cases n; simp only [add_bottom, list_blank.head_cons, list_blank.modify_nth, list_blank.tail_cons], congr, symmetry, apply list_blank.map_modify_nth, intro, refl end theorem add_bottom_nth_snd (L n) : ((add_bottom L).nth n).2 = L.nth n := by conv {to_rhs, rw [← add_bottom_map L, list_blank.nth_map]}; refl theorem add_bottom_nth_succ_fst (L n) : ((add_bottom L).nth (n+1)).1 = ff := by rw [list_blank.nth_succ, add_bottom, list_blank.tail_cons, list_blank.nth_map]; refl theorem add_bottom_head_fst (L) : (add_bottom L).head.1 = tt := by rw [add_bottom, list_blank.head_cons]; refl /-- A stack action is a command that interacts with the top of a stack. Our default position is at the bottom of all the stacks, so we have to hold on to this action while going to the end to modify the stack. -/ inductive st_act (k : K) | push : (σ → Γ k) → st_act | peek : (σ → option (Γ k) → σ) → st_act | pop : (σ → option (Γ k) → σ) → st_act instance st_act.inhabited {k} : inhabited (st_act k) := ⟨st_act.peek (λ s _, s)⟩ section open st_act /-- The TM2 statement corresponding to a stack action. -/ @[nolint unused_arguments] -- [inhabited Λ]: as this is a local definition it is more trouble than -- it is worth to omit the typeclass assumption without breaking the parameters def st_run {k : K} : st_act k → stmt₂ → stmt₂ | (push f) := TM2.stmt.push k f | (peek f) := TM2.stmt.peek k f | (pop f) := TM2.stmt.pop k f /-- The effect of a stack action on the local variables, given the value of the stack. -/ def st_var {k : K} (v : σ) (l : list (Γ k)) : st_act k → σ | (push f) := v | (peek f) := f v l.head' | (pop f) := f v l.head' /-- The effect of a stack action on the stack. -/ def st_write {k : K} (v : σ) (l : list (Γ k)) : st_act k → list (Γ k) | (push f) := f v :: l | (peek f) := l | (pop f) := l.tail /-- We have partitioned the TM2 statements into "stack actions", which require going to the end of the stack, and all other actions, which do not. This is a modified recursor which lumps the stack actions into one. -/ @[elab_as_eliminator] def {l} stmt_st_rec {C : stmt₂ → Sort l} (H₁ : Π k (s : st_act k) q (IH : C q), C (st_run s q)) (H₂ : Π a q (IH : C q), C (TM2.stmt.load a q)) (H₃ : Π p q₁ q₂ (IH₁ : C q₁) (IH₂ : C q₂), C (TM2.stmt.branch p q₁ q₂)) (H₄ : Π l, C (TM2.stmt.goto l)) (H₅ : C TM2.stmt.halt) : ∀ n, C n | (TM2.stmt.push k f q) := H₁ _ (push f) _ (stmt_st_rec q) | (TM2.stmt.peek k f q) := H₁ _ (peek f) _ (stmt_st_rec q) | (TM2.stmt.pop k f q) := H₁ _ (pop f) _ (stmt_st_rec q) | (TM2.stmt.load a q) := H₂ _ _ (stmt_st_rec q) | (TM2.stmt.branch a q₁ q₂) := H₃ _ _ _ (stmt_st_rec q₁) (stmt_st_rec q₂) | (TM2.stmt.goto l) := H₄ _ | TM2.stmt.halt := H₅ theorem supports_run (S : finset Λ) {k} (s : st_act k) (q) : TM2.supports_stmt S (st_run s q) ↔ TM2.supports_stmt S q := by rcases s with _|_|_; refl end /-- The machine states of the TM2 emulator. We can either be in a normal state when waiting for the next TM2 action, or we can be in the "go" and "return" states to go to the top of the stack and return to the bottom, respectively. -/ inductive Λ' : Type (max u_1 u_2 u_3 u_4) | normal : Λ → Λ' | go (k) : st_act k → stmt₂ → Λ' | ret : stmt₂ → Λ' open Λ' instance Λ'.inhabited : inhabited Λ' := ⟨normal (default _)⟩ local notation `stmt₁` := TM1.stmt Γ' Λ' σ local notation `cfg₁` := TM1.cfg Γ' Λ' σ open TM1.stmt /-- The program corresponding to state transitions at the end of a stack. Here we start out just after the top of the stack, and should end just after the new top of the stack. -/ def tr_st_act {k} (q : stmt₁) : st_act k → stmt₁ | (st_act.push f) := write (λ a s, (a.1, update a.2 k $ some $ f s)) $ move dir.right q | (st_act.peek f) := move dir.left $ load (λ a s, f s (a.2 k)) $ move dir.right q | (st_act.pop f) := branch (λ a _, a.1) ( load (λ a s, f s none) q ) ( move dir.left $ load (λ a s, f s (a.2 k)) $ write (λ a s, (a.1, update a.2 k none)) q ) /-- The initial state for the TM2 emulator, given an initial TM2 state. All stacks start out empty except for the input stack, and the stack bottom mark is set at the head. -/ def tr_init (k) (L : list (Γ k)) : list Γ' := let L' : list Γ' := L.reverse.map (λ a, (ff, update (λ _, none) k a)) in (tt, L'.head.2) :: L'.tail theorem step_run {k : K} (q v S) : ∀ s : st_act k, TM2.step_aux (st_run s q) v S = TM2.step_aux q (st_var v (S k) s) (update S k (st_write v (S k) s)) | (st_act.push f) := rfl | (st_act.peek f) := by unfold st_write; rw function.update_eq_self; refl | (st_act.pop f) := rfl /-- The translation of TM2 statements to TM1 statements. regular actions have direct equivalents, but stack actions are deferred by going to the corresponding `go` state, so that we can find the appropriate stack top. -/ def tr_normal : stmt₂ → stmt₁ | (TM2.stmt.push k f q) := goto (λ _ _, go k (st_act.push f) q) | (TM2.stmt.peek k f q) := goto (λ _ _, go k (st_act.peek f) q) | (TM2.stmt.pop k f q) := goto (λ _ _, go k (st_act.pop f) q) | (TM2.stmt.load a q) := load (λ _, a) (tr_normal q) | (TM2.stmt.branch f q₁ q₂) := branch (λ a, f) (tr_normal q₁) (tr_normal q₂) | (TM2.stmt.goto l) := goto (λ a s, normal (l s)) | TM2.stmt.halt := halt theorem tr_normal_run {k} (s q) : tr_normal (st_run s q) = goto (λ _ _, go k s q) := by rcases s with _|_|_; refl open_locale classical /-- The set of machine states accessible from an initial TM2 statement. -/ noncomputable def tr_stmts₁ : stmt₂ → finset Λ' | Q@(TM2.stmt.push k f q) := {go k (st_act.push f) q, ret q} ∪ tr_stmts₁ q | Q@(TM2.stmt.peek k f q) := {go k (st_act.peek f) q, ret q} ∪ tr_stmts₁ q | Q@(TM2.stmt.pop k f q) := {go k (st_act.pop f) q, ret q} ∪ tr_stmts₁ q | Q@(TM2.stmt.load a q) := tr_stmts₁ q | Q@(TM2.stmt.branch f q₁ q₂) := tr_stmts₁ q₁ ∪ tr_stmts₁ q₂ | _ := ∅ theorem tr_stmts₁_run {k s q} : tr_stmts₁ (st_run s q) = {go k s q, ret q} ∪ tr_stmts₁ q := by rcases s with _|_|_; unfold tr_stmts₁ st_run theorem tr_respects_aux₂ {k q v} {S : Π k, list (Γ k)} {L : list_blank (∀ k, option (Γ k))} (hL : ∀ k, L.map (proj k) = list_blank.mk ((S k).map some).reverse) (o) : let v' := st_var v (S k) o, Sk' := st_write v (S k) o, S' := update S k Sk' in ∃ (L' : list_blank (∀ k, option (Γ k))), (∀ k, L'.map (proj k) = list_blank.mk ((S' k).map some).reverse) ∧ TM1.step_aux (tr_st_act q o) v ((tape.move dir.right)^[(S k).length] (tape.mk' ∅ (add_bottom L))) = TM1.step_aux q v' ((tape.move dir.right)^[(S' k).length] (tape.mk' ∅ (add_bottom L'))) := begin dsimp only, simp, cases o; simp only [st_write, st_var, tr_st_act, TM1.step_aux], case TM2to1.st_act.push : f { have := tape.write_move_right_n (λ a : Γ', (a.1, update a.2 k (some (f v)))), dsimp only at this, refine ⟨_, λ k', _, by rw [ tape.move_right_n_head, list.length, tape.mk'_nth_nat, this, add_bottom_modify_nth (λ a, update a k (some (f v))), nat.add_one, iterate_succ']⟩, refine list_blank.ext (λ i, _), rw [list_blank.nth_map, list_blank.nth_modify_nth, proj, pointed_map.mk_val], by_cases h' : k' = k, { subst k', split_ifs; simp only [list.reverse_cons, function.update_same, list_blank.nth_mk, list.inth, list.map], { rw [list.nth_le_nth, list.nth_le_append_right]; simp only [h, list.nth_le_singleton, list.length_map, list.length_reverse, nat.succ_pos', list.length_append, lt_add_iff_pos_right, list.length] }, rw [← proj_map_nth, hL, list_blank.nth_mk, list.inth], cases lt_or_gt_of_ne h with h h, { rw list.nth_append, simpa only [list.length_map, list.length_reverse] using h }, { rw gt_iff_lt at h, rw [list.nth_len_le, list.nth_len_le]; simp only [nat.add_one_le_iff, h, list.length, le_of_lt, list.length_reverse, list.length_append, list.length_map] } }, { split_ifs; rw [function.update_noteq h', ← proj_map_nth, hL], rw function.update_noteq h' } }, case TM2to1.st_act.peek : f { rw function.update_eq_self, use [L, hL], rw [tape.move_left_right], congr, cases e : S k, {refl}, rw [list.length_cons, iterate_succ', tape.move_right_left, tape.move_right_n_head, tape.mk'_nth_nat, add_bottom_nth_snd, stk_nth_val _ (hL k), e, list.reverse_cons, ← list.length_reverse, list.nth_concat_length], refl }, case TM2to1.st_act.pop : f { cases e : S k, { simp only [tape.mk'_head, list_blank.head_cons, tape.move_left_mk', list.length, tape.write_mk', list.head', iterate_zero_apply, list.tail_nil], rw [← e, function.update_eq_self], exact ⟨L, hL, by rw [add_bottom_head_fst, cond]⟩ }, { refine ⟨_, λ k', _, by rw [ list.length_cons, tape.move_right_n_head, tape.mk'_nth_nat, add_bottom_nth_succ_fst, cond, iterate_succ', tape.move_right_left, tape.move_right_n_head, tape.mk'_nth_nat, tape.write_move_right_n (λ a:Γ', (a.1, update a.2 k none)), add_bottom_modify_nth (λ a, update a k none), add_bottom_nth_snd, stk_nth_val _ (hL k), e, show (list.cons hd tl).reverse.nth tl.length = some hd, by rw [list.reverse_cons, ← list.length_reverse, list.nth_concat_length]; refl, list.head', list.tail]⟩, refine list_blank.ext (λ i, _), rw [list_blank.nth_map, list_blank.nth_modify_nth, proj, pointed_map.mk_val], by_cases h' : k' = k, { subst k', split_ifs; simp only [ function.update_same, list_blank.nth_mk, list.tail, list.inth], { rw [list.nth_len_le], {refl}, rw [h, list.length_reverse, list.length_map] }, rw [← proj_map_nth, hL, list_blank.nth_mk, list.inth, e, list.map, list.reverse_cons], cases lt_or_gt_of_ne h with h h, { rw list.nth_append, simpa only [list.length_map, list.length_reverse] using h }, { rw gt_iff_lt at h, rw [list.nth_len_le, list.nth_len_le]; simp only [nat.add_one_le_iff, h, list.length, le_of_lt, list.length_reverse, list.length_append, list.length_map] } }, { split_ifs; rw [function.update_noteq h', ← proj_map_nth, hL], rw function.update_noteq h' } } }, end parameters (M : Λ → stmt₂) include M /-- The TM2 emulator machine states written as a TM1 program. This handles the `go` and `ret` states, which shuttle to and from a stack top. -/ def tr : Λ' → stmt₁ | (normal q) := tr_normal (M q) | (go k s q) := branch (λ a s, (a.2 k).is_none) (tr_st_act (goto (λ _ _, ret q)) s) (move dir.right $ goto (λ _ _, go k s q)) | (ret q) := branch (λ a s, a.1) (tr_normal q) (move dir.left $ goto (λ _ _, ret q)) local attribute [pp_using_anonymous_constructor] turing.TM1.cfg /-- The relation between TM2 configurations and TM1 configurations of the TM2 emulator. -/ inductive tr_cfg : cfg₂ → cfg₁ → Prop | mk {q v} {S : ∀ k, list (Γ k)} (L : list_blank (∀ k, option (Γ k))) : (∀ k, L.map (proj k) = list_blank.mk ((S k).map some).reverse) → tr_cfg ⟨q, v, S⟩ ⟨q.map normal, v, tape.mk' ∅ (add_bottom L)⟩ theorem tr_respects_aux₁ {k} (o q v) {S : list (Γ k)} {L : list_blank (∀ k, option (Γ k))} (hL : L.map (proj k) = list_blank.mk (S.map some).reverse) (n ≤ S.length) : reaches₀ (TM1.step tr) ⟨some (go k o q), v, (tape.mk' ∅ (add_bottom L))⟩ ⟨some (go k o q), v, (tape.move dir.right)^[n] (tape.mk' ∅ (add_bottom L))⟩ := begin induction n with n IH, {refl}, apply (IH (le_of_lt H)).tail, rw iterate_succ_apply', simp only [TM1.step, TM1.step_aux, tr, tape.mk'_nth_nat, tape.move_right_n_head, add_bottom_nth_snd, option.mem_def], rw [stk_nth_val _ hL, list.nth_le_nth], refl, rwa list.length_reverse end theorem tr_respects_aux₃ {q v} {L : list_blank (∀ k, option (Γ k))} (n) : reaches₀ (TM1.step tr) ⟨some (ret q), v, (tape.move dir.right)^[n] (tape.mk' ∅ (add_bottom L))⟩ ⟨some (ret q), v, (tape.mk' ∅ (add_bottom L))⟩ := begin induction n with n IH, {refl}, refine reaches₀.head _ IH, rw [option.mem_def, TM1.step, tr, TM1.step_aux, tape.move_right_n_head, tape.mk'_nth_nat, add_bottom_nth_succ_fst, TM1.step_aux, iterate_succ', tape.move_right_left], refl, end theorem tr_respects_aux {q v T k} {S : Π k, list (Γ k)} (hT : ∀ k, list_blank.map (proj k) T = list_blank.mk ((S k).map some).reverse) (o : st_act k) (IH : ∀ {v : σ} {S : Π (k : K), list (Γ k)} {T : list_blank (∀ k, option (Γ k))}, (∀ k, list_blank.map (proj k) T = list_blank.mk ((S k).map some).reverse) → (∃ b, tr_cfg (TM2.step_aux q v S) b ∧ reaches (TM1.step tr) (TM1.step_aux (tr_normal q) v (tape.mk' ∅ (add_bottom T))) b)) : ∃ b, tr_cfg (TM2.step_aux (st_run o q) v S) b ∧ reaches (TM1.step tr) (TM1.step_aux (tr_normal (st_run o q)) v (tape.mk' ∅ (add_bottom T))) b := begin simp only [tr_normal_run, step_run], have hgo := tr_respects_aux₁ M o q v (hT k) _ (le_refl _), obtain ⟨T', hT', hrun⟩ := tr_respects_aux₂ hT o, have hret := tr_respects_aux₃ M _, have := hgo.tail' rfl, rw [tr, TM1.step_aux, tape.move_right_n_head, tape.mk'_nth_nat, add_bottom_nth_snd, stk_nth_val _ (hT k), list.nth_len_le (le_of_eq (list.length_reverse _)), option.is_none, cond, hrun, TM1.step_aux] at this, obtain ⟨c, gc, rc⟩ := IH hT', refine ⟨c, gc, (this.to₀.trans hret c (trans_gen.head' rfl _)).to_refl⟩, rw [tr, TM1.step_aux, tape.mk'_head, add_bottom_head_fst], exact rc, end local attribute [simp] respects TM2.step TM2.step_aux tr_normal theorem tr_respects : respects (TM2.step M) (TM1.step tr) tr_cfg := λ c₁ c₂ h, begin cases h with l v S L hT, clear h, cases l, {constructor}, simp only [TM2.step, respects, option.map_some'], suffices : ∃ b, _ ∧ reaches (TM1.step (tr M)) _ _, from let ⟨b, c, r⟩ := this in ⟨b, c, trans_gen.head' rfl r⟩, rw [tr], revert v S L hT, refine stmt_st_rec _ _ _ _ _ (M l); intros, { exact tr_respects_aux M hT s @IH }, { exact IH _ hT }, { unfold TM2.step_aux tr_normal TM1.step_aux, cases p v; [exact IH₂ _ hT, exact IH₁ _ hT] }, { exact ⟨_, ⟨_, hT⟩, refl_trans_gen.refl⟩ }, { exact ⟨_, ⟨_, hT⟩, refl_trans_gen.refl⟩ } end theorem tr_cfg_init (k) (L : list (Γ k)) : tr_cfg (TM2.init k L) (TM1.init (tr_init k L)) := begin rw (_ : TM1.init _ = _), { refine ⟨list_blank.mk (L.reverse.map $ λ a, update (default _) k (some a)), λ k', _⟩, refine list_blank.ext (λ i, _), rw [list_blank.map_mk, list_blank.nth_mk, list.inth, list.map_map, (∘), list.nth_map, proj, pointed_map.mk_val], by_cases k' = k, { subst k', simp only [function.update_same], rw [list_blank.nth_mk, list.inth, ← list.map_reverse, list.nth_map] }, { simp only [function.update_noteq h], rw [list_blank.nth_mk, list.inth, list.map, list.reverse_nil, list.nth], cases L.reverse.nth i; refl } }, { rw [tr_init, TM1.init], dsimp only, congr; cases L.reverse; try {refl}, simp only [list.map_map, list.tail_cons, list.map], refl } end theorem tr_eval_dom (k) (L : list (Γ k)) : (TM1.eval tr (tr_init k L)).dom ↔ (TM2.eval M k L).dom := tr_eval_dom tr_respects (tr_cfg_init _ _) theorem tr_eval (k) (L : list (Γ k)) {L₁ L₂} (H₁ : L₁ ∈ TM1.eval tr (tr_init k L)) (H₂ : L₂ ∈ TM2.eval M k L) : ∃ (S : ∀ k, list (Γ k)) (L' : list_blank (∀ k, option (Γ k))), add_bottom L' = L₁ ∧ (∀ k, L'.map (proj k) = list_blank.mk ((S k).map some).reverse) ∧ S k = L₂ := begin obtain ⟨c₁, h₁, rfl⟩ := (part.mem_map_iff _).1 H₁, obtain ⟨c₂, h₂, rfl⟩ := (part.mem_map_iff _).1 H₂, obtain ⟨_, ⟨q, v, S, L', hT⟩, h₃⟩ := tr_eval (tr_respects M) (tr_cfg_init M k L) h₂, cases part.mem_unique h₁ h₃, exact ⟨S, L', by simp only [tape.mk'_right₀], hT, rfl⟩ end /-- The support of a set of TM2 states in the TM2 emulator. -/ noncomputable def tr_supp (S : finset Λ) : finset Λ' := S.bUnion (λ l, insert (normal l) (tr_stmts₁ (M l))) theorem tr_supports {S} (ss : TM2.supports M S) : TM1.supports tr (tr_supp S) := ⟨finset.mem_bUnion.2 ⟨_, ss.1, finset.mem_insert.2 $ or.inl rfl⟩, λ l' h, begin suffices : ∀ q (ss' : TM2.supports_stmt S q) (sub : ∀ x ∈ tr_stmts₁ q, x ∈ tr_supp M S), TM1.supports_stmt (tr_supp M S) (tr_normal q) ∧ (∀ l' ∈ tr_stmts₁ q, TM1.supports_stmt (tr_supp M S) (tr M l')), { rcases finset.mem_bUnion.1 h with ⟨l, lS, h⟩, have := this _ (ss.2 l lS) (λ x hx, finset.mem_bUnion.2 ⟨_, lS, finset.mem_insert_of_mem hx⟩), rcases finset.mem_insert.1 h with rfl | h; [exact this.1, exact this.2 _ h] }, clear h l', refine stmt_st_rec _ _ _ _ _; intros, { -- stack op rw TM2to1.supports_run at ss', simp only [TM2to1.tr_stmts₁_run, finset.mem_union, finset.mem_insert, finset.mem_singleton] at sub, have hgo := sub _ (or.inl $ or.inl rfl), have hret := sub _ (or.inl $ or.inr rfl), cases IH ss' (λ x hx, sub x $ or.inr hx) with IH₁ IH₂, refine ⟨by simp only [tr_normal_run, TM1.supports_stmt]; intros; exact hgo, λ l h, _⟩, rw [tr_stmts₁_run] at h, simp only [TM2to1.tr_stmts₁_run, finset.mem_union, finset.mem_insert, finset.mem_singleton] at h, rcases h with ⟨rfl | rfl⟩ | h, { unfold TM1.supports_stmt TM2to1.tr, rcases s with _|_|_, { exact ⟨λ _ _, hret, λ _ _, hgo⟩ }, { exact ⟨λ _ _, hret, λ _ _, hgo⟩ }, { exact ⟨⟨λ _ _, hret, λ _ _, hret⟩, λ _ _, hgo⟩ } }, { unfold TM1.supports_stmt TM2to1.tr, exact ⟨IH₁, λ _ _, hret⟩ }, { exact IH₂ _ h } }, { -- load unfold TM2to1.tr_stmts₁ at ss' sub ⊢, exact IH ss' sub }, { -- branch unfold TM2to1.tr_stmts₁ at sub, cases IH₁ ss'.1 (λ x hx, sub x $ finset.mem_union_left _ hx) with IH₁₁ IH₁₂, cases IH₂ ss'.2 (λ x hx, sub x $ finset.mem_union_right _ hx) with IH₂₁ IH₂₂, refine ⟨⟨IH₁₁, IH₂₁⟩, λ l h, _⟩, rw [tr_stmts₁] at h, rcases finset.mem_union.1 h with h | h; [exact IH₁₂ _ h, exact IH₂₂ _ h] }, { -- goto rw tr_stmts₁, unfold TM2to1.tr_normal TM1.supports_stmt, unfold TM2.supports_stmt at ss', exact ⟨λ _ v, finset.mem_bUnion.2 ⟨_, ss' v, finset.mem_insert_self _ _⟩, λ _, false.elim⟩ }, { exact ⟨trivial, λ _, false.elim⟩ } -- halt end⟩ end end TM2to1 end turing
7d1b4853811d91d7282cb7e68f275e8416af592f
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/limits/seq_limitProd.lean
ceb04ad0de4b4cc186c8837ec83a06685c6f77b9
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,464
lean
import game.limits.L01defs import game.limits.seq_limSeqSub open real namespace xena -- hide notation `|` x `|` := abs x -- hide /- Use previous results to obtain the limit of a product in the general case. Work in progress. -/ /- Lemma If $\lim_{n \to \infty} a_n = \alpha$ and $\lim_{n \to \infty} b_n = \beta$, then $\lim_{n \to \infty} (a_n * b_n) = \alpha * \beta$ -/ lemma lim_prod (a : ℕ → ℝ) (b : ℕ → ℝ) (α β : ℝ) (ha : is_limit a α) (hb : is_limit b β) : is_limit ( λ n, (a n) * (b n) ) (α * β) := begin have Ha := lim_seq_sub_const a α α ha, rw [sub_self α] at Ha, have Hb := lim_seq_sub_const b β β hb, rw [sub_self β] at Hb, have G := lim_zero_prod (λ n, a n - α) (λ n, b n - β) Ha Hb, have g1 := lim_times_const a α β ha, have g2 := lim_times_const b β α hb, -- becomes ugly, need to improve notation have G1 := lim_linear (λ (n : ℕ), (λ (n : ℕ), a n - α) n * (λ (n : ℕ), b n - β) n) a 0 α 1 β G ha, have h1 : (1:ℝ) * 0 + β * α = β * α , norm_num, rw h1 at G1, have G2 := lim_linear (λ (n : ℕ), 1 * (λ (n : ℕ), (λ (n : ℕ), a n - α) n * (λ (n : ℕ), b n - β) n) n + β * a n) b (β * α) β 1 α G1 hb, -- to get rid of these non-terminal `simp` simp at G2, have h2 : β * α + α * β = 2 * α * β, rw mul_comm β α, norm_num, have h21 := mul_two (α * β), rw ← h21, rw mul_assoc α β 2, rw mul_comm β _, rw ← mul_assoc, rw mul_comm α 2, rw h2 at G2, have G3 := lim_seq_sub_const (λ (n : ℕ), (a n - α) * (b n - β) + (β * a n + α * b n)) (2 * α * β) (α * β) _, simp at G3, have h3 : 2 * α * β - α * β = α * β, have h31 : (2:ℝ) * α * β - α * β = (2:ℝ) * α * β + (-1:ℝ) * α * β, ring, --rw ← add_mul (2:ℝ) (-1:ℝ) (α * β) at h31, rw h31, norm_cast, norm_num, ring, rw h3 at G3, intros ε hε, have G4 := G3 ε hε, cases G4 with N hN, use N, intros n hn, have G5 := hN n hn, simp at G5, simp, have h4 : (a n - α) * (b n - β) = (a n) * (b n) - α * (b n) - β * (a n) + α * β, ring, rw h4 at G5, have h5 : a n * b n - α * b n - β * a n + α * β + (β * a n + α * b n) - α * β - α * β = a n * b n - α * β, ring, rw h5 at G5, exact G5, convert G2, ext, ring, done end end xena -- hide
da534023cc1b9581493edc5a89f96a68b9a8a860
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebraic_topology/dold_kan/homotopy_equivalence.lean
da71ea0ff73810a43ba163b4365797f12e6f4e4a
[ "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
3,697
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 algebraic_topology.dold_kan.normalized /-! # The normalized Moore complex and the alternating face map complex are homotopy equivalent In this file, when the category `A` is abelian, we obtain the homotopy equivalence `homotopy_equiv_normalized_Moore_complex_alternating_face_map_complex` between the normalized Moore complex and the alternating face map complex of a simplicial object in `A`. -/ open category_theory category_theory.category category_theory.limits category_theory.preadditive open_locale simplicial dold_kan noncomputable theory namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] (X : simplicial_object C) /-- Inductive construction of homotopies from `P q` to `𝟙 _` -/ noncomputable def homotopy_P_to_id : Π (q : ℕ), homotopy (P q : K[X] ⟶ _) (𝟙 _) | 0 := homotopy.refl _ | (q+1) := begin refine homotopy.trans (homotopy.of_eq _) (homotopy.trans (homotopy.add (homotopy_P_to_id q) (homotopy.comp_left (homotopy_Hσ_to_zero q) (P q))) (homotopy.of_eq _)), { unfold P, simp only [comp_add, comp_id], }, { simp only [add_zero, comp_zero], }, end /-- The complement projection `Q q` to `P q` is homotopic to zero. -/ def homotopy_Q_to_zero (q : ℕ) : homotopy (Q q : K[X] ⟶ _) 0 := homotopy.equiv_sub_zero.to_fun (homotopy_P_to_id X q).symm lemma homotopy_P_to_id_eventually_constant {q n : ℕ} (hqn : n<q): ((homotopy_P_to_id X (q+1)).hom n (n+1) : X _[n] ⟶ X _[n+1]) = (homotopy_P_to_id X q).hom n (n+1) := begin unfold homotopy_P_to_id, simp only [homotopy_Hσ_to_zero, hσ'_eq_zero hqn (c_mk (n+1) n rfl), homotopy.trans_hom, pi.add_apply, homotopy.of_eq_hom, pi.zero_apply, homotopy.add_hom, homotopy.comp_left_hom, homotopy.null_homotopy'_hom, complex_shape.down_rel, eq_self_iff_true, dite_eq_ite, if_true, comp_zero, add_zero, zero_add], end variable (X) /-- Construction of the homotopy from `P_infty` to the identity using eventually (termwise) constant homotopies from `P q` to the identity for all `q` -/ @[simps] def homotopy_P_infty_to_id : homotopy (P_infty : K[X] ⟶ _) (𝟙 _) := { hom := λ i j, (homotopy_P_to_id X (j+1)).hom i j, zero' := λ i j hij, homotopy.zero _ i j hij, comm := λ n, begin cases n, { simpa only [homotopy.d_next_zero_chain_complex, homotopy.prev_d_chain_complex, P_f_0_eq, zero_add, homological_complex.id_f, P_infty_f] using (homotopy_P_to_id X 2).comm 0, }, { simpa only [homotopy.d_next_succ_chain_complex, homotopy.prev_d_chain_complex, homological_complex.id_f, P_infty_f, ← P_is_eventually_constant (rfl.le : n+1 ≤ n+1), homotopy_P_to_id_eventually_constant X (lt_add_one (n+1))] using (homotopy_P_to_id X (n+2)).comm (n+1), }, end } /-- The inclusion of the Moore complex in the alternating face map complex is an homotopy equivalence -/ @[simps] def homotopy_equiv_normalized_Moore_complex_alternating_face_map_complex {A : Type*} [category A] [abelian A] {Y : simplicial_object A} : homotopy_equiv ((normalized_Moore_complex A).obj Y) ((alternating_face_map_complex A).obj Y) := { hom := inclusion_of_Moore_complex_map Y, inv := P_infty_to_normalized_Moore_complex Y, homotopy_hom_inv_id := homotopy.of_eq (split_mono_inclusion_of_Moore_complex_map Y).id, homotopy_inv_hom_id := homotopy.trans (homotopy.of_eq (P_infty_to_normalized_Moore_complex_comp_inclusion_of_Moore_complex_map Y)) (homotopy_P_infty_to_id Y), } end dold_kan end algebraic_topology
54525a4afd3ec51714f2d173c62819ccc8f5b483
8c95816d6379a6864848d68ce735a5700c1cbbc3
/src/smt2/attributes.lean
da3b5b6b6abffd29ff547e273e7b63532c63a4f6
[ "Apache-2.0" ]
permissive
aqjune/smt2_interface
f00ed0a4317d7c9112deb0806c8e89e6512a901e
598848a7a6fa47520fadcfe1b16891053a296084
refs/heads/master
1,631,518,664,718
1,524,489,340,000
1,524,489,340,000
115,204,811
0
0
null
1,514,043,428,000
1,514,043,427,000
null
UTF-8
Lean
false
false
193
lean
meta def smt2_attribute : user_attribute := { name := `smt2, descr := "Mark a decalartion as part of the gloabl environment of the smt2 tactic" } run_cmd register_attribute `smt2_attribute
20b1c714b85422d716f11a8ccf6aa42c4aec8b1e
367134ba5a65885e863bdc4507601606690974c1
/src/data/list/chain.lean
4432e690c0afd8c8f50e6b8544efd4b5c6f157ef
[ "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
12,568
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Yury Kudryashov -/ import data.list.pairwise import logic.relation universes u v open nat variables {α : Type u} {β : Type v} namespace list /- chain relation (conjunction of R a b ∧ R b c ∧ R c d ...) -/ mk_iff_of_inductive_prop list.chain list.chain_iff variable {R : α → α → Prop} theorem rel_of_chain_cons {a b : α} {l : list α} (p : chain R a (b::l)) : R a b := (chain_cons.1 p).1 theorem chain_of_chain_cons {a b : α} {l : list α} (p : chain R a (b::l)) : chain R b l := (chain_cons.1 p).2 theorem chain.imp' {S : α → α → Prop} (HRS : ∀ ⦃a b⦄, R a b → S a b) {a b : α} (Hab : ∀ ⦃c⦄, R a c → S b c) {l : list α} (p : chain R a l) : chain S b l := by induction p with _ a c l r p IH generalizing b; constructor; [exact Hab r, exact IH (@HRS _)] theorem chain.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {a : α} {l : list α} (p : chain R a l) : chain S a l := p.imp' H (H a) theorem chain.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {a : α} {l : list α} : chain R a l ↔ chain S a l := ⟨chain.imp (λ a b, (H a b).1), chain.imp (λ a b, (H a b).2)⟩ theorem chain.iff_mem {a : α} {l : list α} : chain R a l ↔ chain (λ x y, x ∈ a :: l ∧ y ∈ l ∧ R x y) a l := ⟨λ p, by induction p with _ a b l r p IH; constructor; [exact ⟨mem_cons_self _ _, mem_cons_self _ _, r⟩, exact IH.imp (λ a b ⟨am, bm, h⟩, ⟨mem_cons_of_mem _ am, mem_cons_of_mem _ bm, h⟩)], chain.imp (λ a b h, h.2.2)⟩ theorem chain_singleton {a b : α} : chain R a [b] ↔ R a b := by simp only [chain_cons, chain.nil, and_true] theorem chain_split {a b : α} {l₁ l₂ : list α} : chain R a (l₁++b::l₂) ↔ chain R a (l₁++[b]) ∧ chain R b l₂ := by induction l₁ with x l₁ IH generalizing a; simp only [*, nil_append, cons_append, chain.nil, chain_cons, and_true, and_assoc] theorem chain_map (f : β → α) {b : β} {l : list β} : chain R (f b) (map f l) ↔ chain (λ a b : β, R (f a) (f b)) b l := by induction l generalizing b; simp only [map, chain.nil, chain_cons, *] theorem chain_of_chain_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {a : α} {l : list α} (p : chain S (f a) (map f l)) : chain R a l := ((chain_map f).1 p).imp H theorem chain_map_of_chain {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {a : α} {l : list α} (p : chain R a l) : chain S (f a) (map f l) := (chain_map f).2 $ p.imp H theorem chain_pmap_of_chain {S : β → β → Prop} {p : α → Prop} {f : Π a, p a → β} (H : ∀ a b ha hb, R a b → S (f a ha) (f b hb)) {a : α} {l : list α} (hl₁ : chain R a l) (ha : p a) (hl₂ : ∀ a ∈ l, p a) : chain S (f a ha) (list.pmap f l hl₂) := begin induction l with lh lt l_ih generalizing a, { simp }, { simp [H _ _ _ _ (rel_of_chain_cons hl₁), l_ih _ (chain_of_chain_cons hl₁)] } end theorem chain_of_chain_pmap {S : β → β → Prop} {p : α → Prop} (f : Π a, p a → β) {l : list α} (hl₁ : ∀ a ∈ l, p a) {a : α} (ha : p a) (hl₂ : chain S (f a ha) (list.pmap f l hl₁)) (H : ∀ a b ha hb, S (f a ha) (f b hb) → R a b) : chain R a l := begin induction l with lh lt l_ih generalizing a, { simp }, { simp [H _ _ _ _ (rel_of_chain_cons hl₂), l_ih _ _ (chain_of_chain_cons hl₂)] } end theorem chain_of_pairwise {a : α} {l : list α} (p : pairwise R (a::l)) : chain R a l := begin cases pairwise_cons.1 p with r p', clear p, induction p' with b l r' p IH generalizing a, {exact chain.nil}, simp only [chain_cons, forall_mem_cons] at r, exact chain_cons.2 ⟨r.1, IH r'⟩ end theorem chain_iff_pairwise (tr : transitive R) {a : α} {l : list α} : chain R a l ↔ pairwise R (a::l) := ⟨λ c, begin induction c with b b c l r p IH, {exact pairwise_singleton _ _}, apply IH.cons _, simp only [mem_cons_iff, forall_eq_or_imp, r, true_and], show ∀ x ∈ l, R b x, from λ x m, (tr r (rel_of_pairwise_cons IH m)), end, chain_of_pairwise⟩ theorem chain_iff_nth_le {R} : ∀ {a : α} {l : list α}, chain R a l ↔ (∀ h : 0 < length l, R a (nth_le l 0 h)) ∧ (∀ i (h : i < length l - 1), R (nth_le l i (lt_of_lt_pred h)) (nth_le l (i+1) (lt_pred_iff.mp h))) | a [] := by simp | a (b :: t) := begin rw [chain_cons, chain_iff_nth_le], split, { rintros ⟨R, ⟨h0, h⟩⟩, split, { intro w, exact R, }, { intros i w, cases i, { apply h0, }, { convert h i _ using 1, simp only [succ_eq_add_one, add_succ_sub_one, add_zero, length, add_lt_add_iff_right] at w, exact lt_pred_iff.mpr w, } } }, { rintros ⟨h0, h⟩, split, { apply h0, simp, }, { split, { apply h 0, }, { intros i w, convert h (i+1) _ using 1, exact lt_pred_iff.mp w, } } }, end theorem chain'.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {l : list α} (p : chain' R l) : chain' S l := by cases l; [trivial, exact p.imp H] theorem chain'.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {l : list α} : chain' R l ↔ chain' S l := ⟨chain'.imp (λ a b, (H a b).1), chain'.imp (λ a b, (H a b).2)⟩ theorem chain'.iff_mem : ∀ {l : list α}, chain' R l ↔ chain' (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l | [] := iff.rfl | (x::l) := ⟨λ h, (chain.iff_mem.1 h).imp $ λ a b ⟨h₁, h₂, h₃⟩, ⟨h₁, or.inr h₂, h₃⟩, chain'.imp $ λ a b h, h.2.2⟩ @[simp] theorem chain'_nil : chain' R [] := trivial @[simp] theorem chain'_singleton (a : α) : chain' R [a] := chain.nil theorem chain'_split {a : α} : ∀ {l₁ l₂ : list α}, chain' R (l₁++a::l₂) ↔ chain' R (l₁++[a]) ∧ chain' R (a::l₂) | [] l₂ := (and_iff_right (chain'_singleton a)).symm | (b::l₁) l₂ := chain_split theorem chain'_map (f : β → α) {l : list β} : chain' R (map f l) ↔ chain' (λ a b : β, R (f a) (f b)) l := by cases l; [refl, exact chain_map _] theorem chain'_of_chain'_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α} (p : chain' S (map f l)) : chain' R l := ((chain'_map f).1 p).imp H theorem chain'_map_of_chain' {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α} (p : chain' R l) : chain' S (map f l) := (chain'_map f).2 $ p.imp H theorem pairwise.chain' : ∀ {l : list α}, pairwise R l → chain' R l | [] _ := trivial | (a::l) h := chain_of_pairwise h theorem chain'_iff_pairwise (tr : transitive R) : ∀ {l : list α}, chain' R l ↔ pairwise R l | [] := (iff_true_intro pairwise.nil).symm | (a::l) := chain_iff_pairwise tr @[simp] theorem chain'_cons {x y l} : chain' R (x :: y :: l) ↔ R x y ∧ chain' R (y :: l) := chain_cons theorem chain'.cons {x y l} (h₁ : R x y) (h₂ : chain' R (y :: l)) : chain' R (x :: y :: l) := chain'_cons.2 ⟨h₁, h₂⟩ theorem chain'.tail : ∀ {l} (h : chain' R l), chain' R l.tail | [] _ := trivial | [x] _ := trivial | (x :: y :: l) h := (chain'_cons.mp h).right theorem chain'.rel_head {x y l} (h : chain' R (x :: y :: l)) : R x y := rel_of_chain_cons h theorem chain'.rel_head' {x l} (h : chain' R (x :: l)) ⦃y⦄ (hy : y ∈ head' l) : R x y := by { rw ← cons_head'_tail hy at h, exact h.rel_head } theorem chain'.cons' {x} : ∀ {l : list α}, chain' R l → (∀ y ∈ l.head', R x y) → chain' R (x :: l) | [] _ _ := chain'_singleton x | (a :: l) hl H := hl.cons $ H _ rfl theorem chain'_cons' {x l} : chain' R (x :: l) ↔ (∀ y ∈ head' l, R x y) ∧ chain' R l := ⟨λ h, ⟨h.rel_head', h.tail⟩, λ ⟨h₁, h₂⟩, h₂.cons' h₁⟩ theorem chain'.append : ∀ {l₁ l₂ : list α} (h₁ : chain' R l₁) (h₂ : chain' R l₂) (h : ∀ (x ∈ l₁.last') (y ∈ l₂.head'), R x y), chain' R (l₁ ++ l₂) | [] l₂ h₁ h₂ h := h₂ | [a] l₂ h₁ h₂ h := h₂.cons' $ h _ rfl | (a::b::l) l₂ h₁ h₂ h := begin simp only [last'] at h, have : chain' R (b::l) := h₁.tail, exact (this.append h₂ h).cons h₁.rel_head end theorem chain'_pair {x y} : chain' R [x, y] ↔ R x y := by simp only [chain'_singleton, chain'_cons, and_true] theorem chain'.imp_head {x y} (h : ∀ {z}, R x z → R y z) {l} (hl : chain' R (x :: l)) : chain' R (y :: l) := hl.tail.cons' $ λ z hz, h $ hl.rel_head' hz theorem chain'_reverse : ∀ {l}, chain' R (reverse l) ↔ chain' (flip R) l | [] := iff.rfl | [a] := by simp only [chain'_singleton, reverse_singleton] | (a :: b :: l) := by rw [chain'_cons, reverse_cons, reverse_cons, append_assoc, cons_append, nil_append, chain'_split, ← reverse_cons, @chain'_reverse (b :: l), and_comm, chain'_pair, flip] theorem chain'_iff_nth_le {R} : ∀ {l : list α}, chain' R l ↔ ∀ i (h : i < length l - 1), R (nth_le l i (lt_of_lt_pred h)) (nth_le l (i+1) (lt_pred_iff.mp h)) | [] := by simp | (a :: nil) := by simp | (a :: b :: t) := begin rw [chain'_cons, chain'_iff_nth_le], split, { rintros ⟨R, h⟩ i w, cases i, { exact R, }, { convert h i _ using 1, simp only [succ_eq_add_one, add_succ_sub_one, add_zero, length, add_lt_add_iff_right] at w, simpa using w, }, }, { rintros h, split, { apply h 0, simp, }, { intros i w, convert h (i+1) _ using 1, simp only [add_zero, length, add_succ_sub_one] at w, simpa using w, } }, end /-- If `l₁ l₂` and `l₃` are lists and `l₁ ++ l₂` and `l₂ ++ l₃` both satisfy `chain' R`, then so does `l₁ ++ l₂ ++ l₃` provided `l₂ ≠ []` -/ lemma chain'.append_overlap : ∀ {l₁ l₂ l₃ : list α} (h₁ : chain' R (l₁ ++ l₂)) (h₂ : chain' R (l₂ ++ l₃)) (hn : l₂ ≠ []), chain' R (l₁ ++ l₂ ++ l₃) | [] l₂ l₃ h₁ h₂ hn := h₂ | l₁ [] l₃ h₁ h₂ hn := (hn rfl).elim | [a] (b::l₂) l₃ h₁ h₂ hn := by { simp at *, tauto } | (a::b::l₁) (c::l₂) l₃ h₁ h₂ hn := begin simp only [cons_append, chain'_cons] at h₁ h₂ ⊢, simp only [← cons_append] at h₁ h₂ ⊢, exact ⟨h₁.1, chain'.append_overlap h₁.2 h₂ (cons_ne_nil _ _)⟩ end variables {r : α → α → Prop} {a b : α} /-- If `a` and `b` are related by the reflexive transitive closure of `r`, then there is a `r`-chain starting from `a` and ending on `b`. The converse of `relation_refl_trans_gen_of_exists_chain`. -/ lemma exists_chain_of_relation_refl_trans_gen (h : relation.refl_trans_gen r a b) : ∃ l, chain r a l ∧ last (a :: l) (cons_ne_nil _ _) = b := begin apply relation.refl_trans_gen.head_induction_on h, { exact ⟨[], chain.nil, rfl⟩ }, { intros c d e t ih, obtain ⟨l, hl₁, hl₂⟩ := ih, refine ⟨d :: l, chain.cons e hl₁, _⟩, rwa last_cons_cons } end /-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then the predicate is true everywhere in the chain and at `a`. That is, we can propagate the predicate up the chain. -/ lemma chain.induction (p : α → Prop) (l : list α) (h : chain r a l) (hb : last (a :: l) (cons_ne_nil _ _) = b) (carries : ∀ ⦃x y : α⦄, r x y → p y → p x) (final : p b) : ∀ i ∈ a :: l, p i := begin induction l generalizing a, { cases hb, simp [final] }, { rw chain_cons at h, rintro _ (rfl | _), apply carries h.1 (l_ih h.2 hb _ (or.inl rfl)), apply l_ih h.2 hb _ H } end /-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then the predicate is true at `a`. That is, we can propagate the predicate all the way up the chain. -/ @[elab_as_eliminator] lemma chain.induction_head (p : α → Prop) (l : list α) (h : chain r a l) (hb : last (a :: l) (cons_ne_nil _ _) = b) (carries : ∀ ⦃x y : α⦄, r x y → p y → p x) (final : p b) : p a := (chain.induction p l h hb carries final) _ (mem_cons_self _ _) /-- If there is an `r`-chain starting from `a` and ending at `b`, then `a` and `b` are related by the reflexive transitive closure of `r`. The converse of `exists_chain_of_relation_refl_trans_gen`. -/ lemma relation_refl_trans_gen_of_exists_chain (l) (hl₁ : chain r a l) (hl₂ : last (a :: l) (cons_ne_nil _ _) = b) : relation.refl_trans_gen r a b := chain.induction_head _ l hl₁ hl₂ (λ x y, relation.refl_trans_gen.head) relation.refl_trans_gen.refl end list
d7c5c20cba01991a7cf04666d2ed47a8cdd0bbd2
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/testing/slim_check/functions.lean
09873e603ead2454a1b71c5842d459e3b95aeebd
[ "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
16,940
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 variables {α : Type u} {β : Type v} {γ : Sort 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 [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 [has_repr α] [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 [has_repr α] [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 [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 [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 [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 [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 [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 [decidable_eq α] (xs : list (α × α)) (x : α) : α := ((xs.map prod.to_sigma).lookup x).get_or_else x @[simp] lemma list.apply_id_cons [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 _root_.list _root_.prod (to_sigma) open _root_.nat lemma list.apply_id_zip_eq [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 [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 [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 [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 {α : Type} [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 {α : Type} [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 [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 instance antitone.testable [preorder α] [preorder β] (f : α → β) [I : testable (named_binder "x" $ ∀ x : α, named_binder "y" $ ∀ y : α, named_binder "H" $ x ≤ y → f y ≤ f x)] : testable (antitone f) := I end slim_check
6f35af4f945a35bc83006064e3aab109aa9d2df9
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/algebra/euclidean_domain.lean
0274873a3676d1c552ea20d8871348b7639d3d08
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
8,297
lean
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro Euclidean domains and Euclidean algorithm (extended to come) A lot is based on pre-existing code in mathlib for natural number gcds -/ import data.int.basic universe u class euclidean_domain (α : Type u) extends integral_domain α := (quotient : α → α → α) (remainder : α → α → α) -- This could be changed to the same order as int.mod_add_div. -- We normally write qb+r rather than r + qb though. (quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a) (r : α → α → Prop) (r_well_founded : well_founded r) (remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b) /- `val_le_mul_left` is often not a required in definitions of a euclidean domain since given the other properties we can show there is a (noncomputable) euclidean domain α with the property `val_le_mul_left`. So potentially this definition could be split into two different ones (euclidean_domain_weak and euclidean_domain_strong) with a noncomputable function from weak to strong. I've currently divided the lemmas into strong and weak depending on whether they require `val_le_mul_left` or not. -/ (mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a) namespace euclidean_domain variable {α : Type u} variables [euclidean_domain α] local infix ` ≺ `:50 := euclidean_domain.r instance : has_div α := ⟨quotient⟩ instance : has_mod α := ⟨remainder⟩ theorem div_add_mod (a b : α) : b * (a / b) + a % b = a := quotient_mul_add_remainder_eq _ _ lemma mod_eq_sub_mul_div {α : Type*} [euclidean_domain α] (a b : α) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : by simp ... = a - b * (a / b) : by rw div_add_mod theorem mod_lt : ∀ a {b : α}, b ≠ 0 → (a % b) ≺ b := remainder_lt theorem mul_right_not_lt {a : α} (b) (h : a ≠ 0) : ¬(a * b) ≺ b := by rw mul_comm; exact mul_left_not_lt b h lemma mul_div_cancel_left {a : α} (b) (a0 : a ≠ 0) : a * b / a = b := eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h, begin have := mul_left_not_lt a h, rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this, exact this (mod_lt _ a0) end lemma mul_div_cancel (a) {b : α} (b0 : b ≠ 0) : a * b / b = a := by rw mul_comm; exact mul_div_cancel_left a b0 @[simp] lemma mod_zero (a : α) : a % 0 = a := by simpa using div_add_mod a 0 @[simp] lemma mod_eq_zero {a b : α} : a % b = 0 ↔ b ∣ a := ⟨λ h, by rw [← div_add_mod a b]; simp [h], λ ⟨c, e⟩, begin rw [e, ← add_left_cancel_iff, div_add_mod, add_zero], haveI := classical.dec, by_cases b0 : b = 0; simp [b0, mul_div_cancel_left], end⟩ @[simp] lemma mod_self (a : α) : a % a = 0 := mod_eq_zero.2 (dvd_refl _) lemma dvd_mod_iff {a b c : α} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := by rw [dvd_add_iff_right (dvd_mul_of_dvd_left h _), div_add_mod] lemma lt_one (a : α) : a ≺ (1:α) → a = 0 := by haveI := classical.dec; exact not_imp_not.1 (λ h, by simpa using mul_left_not_lt 1 h) lemma val_dvd_le : ∀ a b : α, b ∣ a → a ≠ 0 → ¬a ≺ b | _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (λ h, by simpa [h] using ha) @[simp] lemma mod_one (a : α) : a % 1 = 0 := mod_eq_zero.2 (one_dvd _) @[simp] lemma zero_mod (b : α) : 0 % b = 0 := mod_eq_zero.2 (dvd_zero _) @[simp] lemma zero_div {a : α} (a0 : a ≠ 0) : 0 / a = 0 := by simpa using mul_div_cancel 0 a0 @[simp] lemma div_self {a : α} (a0 : a ≠ 0) : a / a = 1 := by simpa using mul_div_cancel 1 a0 section gcd variable [decidable_eq α] def gcd : α → α → α | a := λ b, if a0 : a = 0 then b else have h:_ := mod_lt b a0, gcd (b%a) a using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]} @[simp] theorem gcd_zero_left (a : α) : gcd 0 a = a := by rw gcd; simp @[simp] theorem gcd_zero_right (a : α) : gcd a 0 = a := by rw gcd; by_cases a0 : a = 0; simp [a0] theorem gcd_val (a b : α) : gcd a b = gcd (b % a) a := by rw gcd; by_cases a0 : a = 0; simp [a0] @[elab_as_eliminator] theorem gcd.induction {P : α → α → Prop} : ∀ a b : α, (∀ x, P 0 x) → (∀ a b, a ≠ 0 → P (b % a) a → P a b) → P a b | a := λ b H0 H1, if a0 : a = 0 then by simp [a0, H0] else have h:_ := mod_lt b a0, H1 _ _ a0 (gcd.induction (b%a) a H0 H1) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]} theorem gcd_dvd (a b : α) : gcd a b ∣ a ∧ gcd a b ∣ b := gcd.induction a b (λ b, by simp) (λ a b aneq ⟨IH₁, IH₂⟩, by rw gcd_val; exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩) theorem gcd_dvd_left (a b : α) : gcd a b ∣ a := (gcd_dvd a b).left theorem gcd_dvd_right (a b : α) : gcd a b ∣ b := (gcd_dvd a b).right theorem dvd_gcd {a b c : α} : c ∣ a → c ∣ b → c ∣ gcd a b := gcd.induction a b (by simp {contextual := tt}) (λ a b a0 IH ca cb, by rw gcd_val; exact IH ((dvd_mod_iff ca).2 cb) ca) theorem gcd_eq_left {a b : α} : gcd a b = a ↔ a ∣ b := ⟨λ h, by rw ← h; apply gcd_dvd_right, λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩ @[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 := gcd_eq_left.2 (one_dvd _) @[simp] theorem gcd_self (a : α) : gcd a a = a := gcd_eq_left.2 (dvd_refl _) def xgcd_aux : α → α → α → α → α → α → α × α × α | r := λ s t r' s' t', if hr : r = 0 then (r', s', t') else have r' % r ≺ r, from mod_lt _ hr, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]} @[simp] theorem xgcd_zero_left {s t r' s' t' : α} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by unfold xgcd_aux; rw if_pos rfl @[simp] theorem xgcd_aux_rec {r s t r' s' t' : α} (h : r ≠ 0) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by conv {to_lhs, rw [xgcd_aux]}; simp [h] /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : α) : α × α := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : α) : α := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : α) : α := (xgcd x y).2 @[simp] theorem xgcd_aux_fst (x y : α) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by finish) (λ x y h IH s t s' t', by simp [h, IH]; rw ← gcd_val) theorem xgcd_aux_val (x y : α) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1]; cases xgcd_aux x 1 0 y 0 1; refl theorem xgcd_val (x y : α) : xgcd x y = (gcd_a x y, gcd_b x y) := by unfold gcd_a gcd_b; cases xgcd x y; refl private def P (a b : α) : α × α × α → Prop | (r, s, t) := (r : α) = a * s + b * t theorem xgcd_aux_P (a b : α) {r r' : α} : ∀ {s t s' t'}, P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := gcd.induction r r' (by finish) $ λ x y h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, dsimp [P] at *, rw [mod_eq_sub_mul_div, p, p'], simp [mul_add, add_mul, mul_comm, mul_assoc, mul_left_comm] end theorem gcd_eq_gcd_ab (a b : α) : (gcd a b : α) = a * gcd_a a b + b * gcd_b a b := by have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1 (by simp [P]) (by simp [P]); rwa [xgcd_aux_val, xgcd_val] at this end gcd instance : euclidean_domain ℤ := { quotient := (/), remainder := (%), quotient_mul_add_remainder_eq := λ a b, by rw add_comm; exact int.mod_add_div _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs]; exact int.mod_lt _ b0, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by rw [← mul_one a.nat_abs, int.nat_abs_mul]; exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) } end euclidean_domain
26142655f7e427240c3c1a80e4d66203c94b861f
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/yoneda.lean
597e7e884c0d80b67afa3269cbc8b23accfa1078
[]
no_license
rwbarton/lean-category-theory
f720268d800b62a25d69842ca7b5d27822f00652
00df814d463406b7a13a56f5dcda67758ba1b419
refs/heads/master
1,585,366,296,767
1,536,151,349,000
1,536,151,349,000
147,652,096
0
0
null
1,536,226,960,000
1,536,226,960,000
null
UTF-8
Lean
false
false
3,206
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.natural_transformation import category_theory.opposites import category_theory.types import category_theory.embedding import category_theory.cancellation import tactic.interactive open category_theory namespace category_theory.yoneda universes u₁ v₁ u₂ variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] include 𝒞 def yoneda : C ⥤ ((Cᵒᵖ) ⥤ (Type v₁)) := { obj := λ X, { obj := λ Y : C, Y ⟶ X, map' := λ Y Y' f g, f ≫ g }, map' := λ X X' f, { app := λ Y g, g ≫ f } } @[simp] lemma yoneda_obj_obj (X Y : C) : ((yoneda C) X) Y = (Y ⟶ X) := rfl @[simp] lemma yoneda_obj_map (X : C) {Y Y' : C} (f : Y ⟶ Y') : ((yoneda C) X).map f = λ g, f ≫ g := rfl @[simp] lemma yoneda_map_app {X X' : C} (f : X ⟶ X') (Y : C) : ((yoneda C).map f) Y = λ g, g ≫ f := rfl @[search] lemma yoneda_aux_1 {X Y : Cᵒᵖ} (f : X ⟶ Y) : ((yoneda C).map f) Y (𝟙 Y) = ((yoneda C) X).map f (𝟙 X) := by obviously @[simp,search] lemma yoneda_aux_2 {X Y : C} (α : (yoneda C) X ⟶ (yoneda C) Y) {Z Z' : C} (f : Z ⟶ Z') (h : Z' ⟶ X) : α Z (f ≫ h) = f ≫ α Z' h := by obviously instance yoneda_full : full (yoneda C) := { preimage := λ X Y f, (f X) (𝟙 X) }. instance yoneda_faithful : faithful (yoneda C) := begin /- obviously says: -/ fsplit, intros X Y f g p, injections_and_clear, have cancel_right'_f_g_h_1 := cancel_right' f g h_1, assumption end -- We need to help typeclass inference with some awkward universe levels here. instance instance_1 : category (((Cᵒᵖ) ⥤ Type v₁) × (Cᵒᵖ)) := category_theory.prod.{(max u₁ (v₁+1)) (max u₁ v₁) u₁ v₁} (Cᵒᵖ ⥤ Type v₁) (Cᵒᵖ) instance instance_2 : category ((Cᵒᵖ) × ((Cᵒᵖ) ⥤ Type v₁)) := category_theory.prod.{u₁ v₁ (max u₁ (v₁+1)) (max u₁ v₁)} (Cᵒᵖ) (Cᵒᵖ ⥤ Type v₁) def yoneda_evaluation : (((Cᵒᵖ) ⥤ (Type v₁)) × (Cᵒᵖ)) ⥤ (Type (max u₁ v₁)) := (evaluation (Cᵒᵖ) (Type v₁)) ⋙ ulift_functor.{v₁ u₁} @[simp] lemma yoneda_evaluation_map_down (P Q : (Cᵒᵖ ⥤ Type v₁) × (Cᵒᵖ)) (α : P ⟶ Q) (x : (yoneda_evaluation C) P) : ((yoneda_evaluation C).map α x).down = (α.1) (Q.2) ((P.1).map (α.2) (x.down)) := rfl def yoneda_pairing : (((Cᵒᵖ) ⥤ (Type v₁)) × (Cᵒᵖ)) ⥤ (Type (max u₁ v₁)) := let F := (category_theory.prod.swap ((Cᵒᵖ) ⥤ (Type v₁)) (Cᵒᵖ)) in let G := (functor.prod ((yoneda C).op) (functor.id ((Cᵒᵖ) ⥤ (Type v₁)))) in let H := (functor.hom ((Cᵒᵖ) ⥤ (Type v₁))) in (F ⋙ G ⋙ H) @[simp] lemma yoneda_pairing_map (P Q : (Cᵒᵖ ⥤ Type v₁) × (Cᵒᵖ)) (α : P ⟶ Q) (β : (yoneda_pairing C) (P.1, P.2)): (yoneda_pairing C).map α β = (yoneda C).map (α.snd) ≫ β ≫ α.fst := rfl def yoneda_lemma : (yoneda_pairing C) ≅ (yoneda_evaluation C) := { hom := { app := λ F x, ulift.up ((x.app F.2) (𝟙 F.2)) }, inv := { app := λ F x, { app := λ X a, (F.1.map a) x.down } } }. end category_theory.yoneda
8189e6032cc33c706684e08385754d76bbcee4c9
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/logic/identities.lean
cbf8afb63937f10700151b7497752e47900614ce
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,445
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: logic.identities Authors: Jeremy Avigad, Leonardo de Moura Useful logical identities. Since we are not using propositional extensionality, some of the calculations use the type class support provided by logic.instances. -/ import logic.connectives logic.instances logic.quantifiers logic.cast open relation decidable relation.iff_ops theorem or.right_comm (a b c : Prop) : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := calc (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) : or.assoc ... ↔ a ∨ (c ∨ b) : {or.comm} ... ↔ (a ∨ c) ∨ b : iff.symm or.assoc theorem or.left_comm (a b c : Prop) : a ∨ (b ∨ c)↔ b ∨ (a ∨ c) := calc a ∨ (b ∨ c) ↔ (a ∨ b) ∨ c : iff.symm or.assoc ... ↔ (b ∨ a) ∨ c : {or.comm} ... ↔ b ∨ (a ∨ c) : or.assoc theorem and.right_comm (a b c : Prop) : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := calc (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) : and.assoc ... ↔ a ∧ (c ∧ b) : {and.comm} ... ↔ (a ∧ c) ∧ b : iff.symm and.assoc theorem and.left_comm (a b c : Prop) : a ∧ (b ∧ c)↔ b ∧ (a ∧ c) := calc a ∧ (b ∧ c) ↔ (a ∧ b) ∧ c : iff.symm and.assoc ... ↔ (b ∧ a) ∧ c : {and.comm} ... ↔ b ∧ (a ∧ c) : and.assoc theorem not_not_iff {a : Prop} [D : decidable a] : (¬¬a) ↔ a := iff.intro (assume H : ¬¬a, by_cases (assume H' : a, H') (assume H' : ¬a, absurd H' H)) (assume H : a, assume H', H' H) theorem not_not_elim {a : Prop} [D : decidable a] (H : ¬¬a) : a := iff.mp not_not_iff H theorem not_true_iff_false : ¬true ↔ false := iff.intro (assume H, H trivial) false.elim theorem not_false_iff_true : ¬false ↔ true := iff.intro (assume H, trivial) (assume H H', H') theorem not_or_iff_not_and_not {a b : Prop} [Da : decidable a] [Db : decidable b] : ¬(a ∨ b) ↔ ¬a ∧ ¬b := iff.intro (assume H, or.elim (em a) (assume Ha, absurd (or.inl Ha) H) (assume Hna, or.elim (em b) (assume Hb, absurd (or.inr Hb) H) (assume Hnb, and.intro Hna Hnb))) (assume (H : ¬a ∧ ¬b) (N : a ∨ b), or.elim N (assume Ha, absurd Ha (and.elim_left H)) (assume Hb, absurd Hb (and.elim_right H))) theorem not_and_iff_not_or_not {a b : Prop} [Da : decidable a] [Db : decidable b] : ¬(a ∧ b) ↔ ¬a ∨ ¬b := iff.intro (assume H, or.elim (em a) (assume Ha, or.elim (em b) (assume Hb, absurd (and.intro Ha Hb) H) (assume Hnb, or.inr Hnb)) (assume Hna, or.inl Hna)) (assume (H : ¬a ∨ ¬b) (N : a ∧ b), or.elim H (assume Hna, absurd (and.elim_left N) Hna) (assume Hnb, absurd (and.elim_right N) Hnb)) theorem imp_iff_not_or {a b : Prop} [Da : decidable a] : (a → b) ↔ ¬a ∨ b := iff.intro (assume H : a → b, (or.elim (em a) (assume Ha : a, or.inr (H Ha)) (assume Hna : ¬a, or.inl Hna))) (assume (H : ¬a ∨ b) (Ha : a), or_resolve_right H (not_not_iff⁻¹ ▸ Ha)) theorem not_implies_iff_and_not {a b : Prop} [Da : decidable a] [Db : decidable b] : ¬(a → b) ↔ a ∧ ¬b := calc ¬(a → b) ↔ ¬(¬a ∨ b) : {imp_iff_not_or} ... ↔ ¬¬a ∧ ¬b : not_or_iff_not_and_not ... ↔ a ∧ ¬b : {not_not_iff} theorem peirce {a b : Prop} [D : decidable a] : ((a → b) → a) → a := assume H, by_contradiction (assume Hna : ¬a, have Hnna : ¬¬a, from not_not_of_not_implies (mt H Hna), absurd (not_not_elim Hnna) Hna) theorem forall_not_of_not_exists {A : Type} {P : A → Prop} [D : ∀x, decidable (P x)] (H : ¬∃x, P x) : ∀x, ¬P x := take x, or.elim (em (P x)) (assume Hp : P x, absurd (exists.intro x Hp) H) (assume Hn : ¬P x, Hn) theorem exists_not_of_not_forall {A : Type} {P : A → Prop} [D : ∀x, decidable (P x)] [D' : decidable (∃x, ¬P x)] (H : ¬∀x, P x) : ∃x, ¬P x := @by_contradiction _ D' (assume H1 : ¬∃x, ¬P x, have H2 : ∀x, ¬¬P x, from @forall_not_of_not_exists _ _ (take x, not.decidable (D x)) H1, have H3 : ∀x, P x, from take x, @not_not_elim _ (D x) (H2 x), absurd H3 H) theorem iff_true_intro {a : Prop} (H : a) : a ↔ true := iff.intro (assume H1 : a, trivial) (assume H2 : true, H) theorem iff_false_intro {a : Prop} (H : ¬a) : a ↔ false := iff.intro (assume H1 : a, absurd H1 H) (assume H2 : false, false.elim H2) theorem ne_self_iff_false {A : Type} (a : A) : (a ≠ a) ↔ false := iff.intro (assume H, false.of_ne H) (assume H, false.elim H) theorem eq_self_iff_true {A : Type} (a : A) : (a = a) ↔ true := iff_true_intro rfl theorem heq_self_iff_true {A : Type} (a : A) : (a == a) ↔ true := iff_true_intro (heq.refl a) theorem iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false := iff.intro (assume H, have H' : ¬a, from assume Ha, (H ▸ Ha) Ha, H' (H⁻¹ ▸ H')) (assume H, false.elim H) theorem true_iff_false : (true ↔ false) ↔ false := not_true_iff_false ▸ (iff_not_self true) theorem false_iff_true : (false ↔ true) ↔ false := not_false_iff_true ▸ (iff_not_self false) theorem iff_true_iff (a : Prop) : (a ↔ true) ↔ a := iff.intro (assume H, of_iff_true H) (assume H, iff_true_intro H) theorem iff_false_iff_not (a : Prop) : (a ↔ false) ↔ ¬a := iff.intro (assume H, not_of_iff_false H) (assume H, iff_false_intro H)
68a7895f19b53f1d29e2b41a66bce269fb6290f1
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/cases_tac1.lean
8c329a80c433f55c1e9d1b4ffae2ec9200cf334f
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,021
lean
inductive {u} vec (A : Type u) : nat → Type u | nil : vec 0 | cons : ∀ {n}, A → vec n → vec (n+1) open tactic nat vec def head {A : Type*} : ∀ {n : nat}, vec A (n+1) → A | n (cons h t) := h def tail {A : Type*} : ∀ {n : nat}, vec A (n+1) → vec A n | n (cons h t) := t @[simp] lemma head_cons {A : Type*} {n : nat} (a : A) (v : vec A n) : head (cons a v) = a := rfl @[simp] lemma tail_cons {A : Type*} {n : nat} (a : A) (v : vec A n) : tail (cons a v) = v := rfl example {A : Type*} {n : nat} (v w : vec A (n+1)) : head v = head w → tail v = tail w → v = w := by do v ← get_local `v, cases v [`n', `hv, `tv], trace_state, w ← get_local `w, cases w [`n', `hw, `tw], trace_state, dsimp, trace_state, Heq1 ← intro1, Heq2 ← intro1, subst Heq1, subst Heq2, reflexivity print "-------" example (n : nat) : n ≠ 0 → succ (pred n) = n := by do H ← intro `H, n ← get_local `n, cases n [`n'], trace_state, contradiction, reflexivity print "---------"
79de72f810b2d0e8d5c6613939af0e08993893f7
80b18137872dad7c3df334b9069d70935b4224f3
/src/data/fintype/intervals.lean
c9e5fdf36087bb0c137a34df23520b16f58f383e
[ "Apache-2.0" ]
permissive
Jack-Pumpkinhead/mathlib
1bcf5692d355dc397847791c137158f01b407535
da8b23f907f750528539bffa604875b98717fb93
refs/heads/master
1,621,299,949,262
1,585,480,767,000
1,585,480,767,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,617
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.set.intervals import data.set.finite import data.fintype import data.pnat.intervals /-! # fintype instances for intervals We provide `fintype` instances for `Ico l u`, for `l u : ℕ`, and for `l u : ℤ`. -/ namespace set instance Ico_ℕ_fintype (l u : ℕ) : fintype (Ico l u) := fintype.of_finset (finset.Ico l u) $ (λ n, by { simp only [mem_Ico, finset.Ico.mem], }) @[simp] lemma Ico_ℕ_card (l u : ℕ) : fintype.card (Ico l u) = u - l := calc fintype.card (Ico l u) = (finset.Ico l u).card : fintype.card_of_finset _ _ ... = u - l : finset.Ico.card l u instance Ico_pnat_fintype (l u : ℕ+) : fintype (Ico l u) := fintype.of_finset (pnat.Ico l u) $ (λ n, by { simp only [mem_Ico, pnat.Ico.mem], }) @[simp] lemma Ico_pnat_card (l u : ℕ+) : fintype.card (Ico l u) = u - l := calc fintype.card (Ico l u) = (pnat.Ico l u).card : fintype.card_of_finset _ _ ... = u - l : pnat.Ico.card l u instance Ico_ℤ_fintype (l u : ℤ) : fintype (Ico l u) := fintype.of_finset (finset.Ico_ℤ l u) $ (λ n, by { simp only [mem_Ico, finset.Ico_ℤ.mem], }) @[simp] lemma Ico_ℤ_card (l u : ℤ) : fintype.card (Ico l u) = (u - l).to_nat := calc fintype.card (Ico l u) = (finset.Ico_ℤ l u).card : fintype.card_of_finset _ _ ... = (u - l).to_nat : finset.Ico_ℤ.card l u -- TODO other useful instances: fin n, zmod? end set
293e54856445cb8323fa567d5174c8a86cfd941f
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-numbertheory-37.lean
7c106152257ff1be4ec9a28cab8a85d62d5aa1b0
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
275
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import tactic.gptf import data.real.basic import data.nat.basic example : ( nat.lcm 9999 100001 ) = 90900909 := begin sorry end
a2497ed09c10e3f4151a8c8a8daa97c23e281dd4
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/covering/besicovitch.lean
a88727f55140d8dd48e4195875e59c4fddac9a06
[ "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
60,324
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.covering.differentiation import measure_theory.covering.vitali_family import measure_theory.integral.lebesgue import measure_theory.measure.regular import set_theory.ordinal.arithmetic import topology.metric_space.basic /-! # Besicovitch covering theorems The topological Besicovitch covering theorem ensures that, in a nice metric space, there exists a number `N` such that, from any family of balls with bounded radii, one can extract `N` families, each made of disjoint balls, covering together all the centers of the initial family. By "nice metric space", we mean a technical property stated as follows: there exists no satellite configuration of `N + 1` points (with a given parameter `τ > 1`). Such a configuration is a family of `N + 1` balls, where the first `N` balls all intersect the last one, but none of them contains the center of another one and their radii are controlled. This property is for instance satisfied by finite-dimensional real vector spaces. In this file, we prove the topological Besicovitch covering theorem, in `besicovitch.exist_disjoint_covering_families`. The measurable Besicovitch theorem ensures that, in the same class of metric spaces, if at every point one considers a class of balls of arbitrarily small radii, called admissible balls, then one can cover almost all the space by a family of disjoint admissible balls. It is deduced from the topological Besicovitch theorem, and proved in `besicovitch.exists_disjoint_closed_ball_covering_ae`. This implies that balls of small radius form a Vitali family in such spaces. Therefore, theorems on differentiation of measures hold as a consequence of general results. We restate them in this context to make them more easily usable. ## Main definitions and results * `satellite_config α N τ` is the type of all satellite configurations of `N + 1` points in the metric space `α`, with parameter `τ`. * `has_besicovitch_covering` is a class recording that there exist `N` and `τ > 1` such that there is no satellite configuration of `N + 1` points with parameter `τ`. * `exist_disjoint_covering_families` is the topological Besicovitch covering theorem: from any family of balls one can extract finitely many disjoint subfamilies covering the same set. * `exists_disjoint_closed_ball_covering` is the measurable Besicovitch covering theorem: from any family of balls with arbitrarily small radii at every point, one can extract countably many disjoint balls covering almost all the space. While the value of `N` is relevant for the precise statement of the topological Besicovitch theorem, it becomes irrelevant for the measurable one. Therefore, this statement is expressed using the `Prop`-valued typeclass `has_besicovitch_covering`. We also restate the following specialized versions of general theorems on differentiation of measures: * `besicovitch.ae_tendsto_rn_deriv` ensures that `ρ (closed_ball x r) / μ (closed_ball x r)` tends almost surely to the Radon-Nikodym derivative of `ρ` with respect to `μ` at `x`. * `besicovitch.ae_tendsto_measure_inter_div` states that almost every point in an arbitrary set `s` is a Lebesgue density point, i.e., `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` tends to `1` as `r` tends to `0`. A stronger version for measurable sets is given in `besicovitch.ae_tendsto_measure_inter_div_of_measurable_set`. ## Implementation #### Sketch of proof of the topological Besicovitch theorem: We choose balls in a greedy way. First choose a ball with maximal radius (or rather, since there is no guarantee the maximal radius is realized, a ball with radius within a factor `τ` of the supremum). Then, remove all balls whose center is covered by the first ball, and choose among the remaining ones a ball with radius close to maximum. Go on forever until there is no available center (this is a transfinite induction in general). Then define inductively a coloring of the balls. A ball will be of color `i` if it intersects already chosen balls of color `0`, ..., `i - 1`, but none of color `i`. In this way, balls of the same color form a disjoint family, and the space is covered by the families of the different colors. The nontrivial part is to show that at most `N` colors are used. If one needs `N + 1` colors, consider the first time this happens. Then the corresponding ball intersects `N` balls of the different colors. Moreover, the inductive construction ensures that the radii of all the balls are controlled: they form a satellite configuration with `N + 1` balls (essentially by definition of satellite configurations). Since we assume that there are no such configurations, this is a contradiction. #### Sketch of proof of the measurable Besicovitch theorem: From the topological Besicovitch theorem, one can find a disjoint countable family of balls covering a proportion `> 1 / (N + 1)` of the space. Taking a large enough finite subset of these balls, one gets the same property for finitely many balls. Their union is closed. Therefore, any point in the complement has around it an admissible ball not intersecting these finitely many balls. Applying again the topological Besicovitch theorem, one extracts from these a disjoint countable subfamily covering a proportion `> 1 / (N + 1)` of the remaining points, and then even a disjoint finite subfamily. Then one goes on again and again, covering at each step a positive proportion of the remaining points, while remaining disjoint from the already chosen balls. The union of all these balls is the desired almost everywhere covering. -/ noncomputable theory universe u open metric set filter fin measure_theory topological_space open_locale topological_space classical big_operators ennreal measure_theory nnreal /-! ### Satellite configurations -/ /-- A satellite configuration is a configuration of `N+1` points that shows up in the inductive construction for the Besicovitch covering theorem. It depends on some parameter `τ ≥ 1`. This is a family of balls (indexed by `i : fin N.succ`, with center `c i` and radius `r i`) such that the last ball intersects all the other balls (condition `inter`), and given any two balls there is an order between them, ensuring that the first ball does not contain the center of the other one, and the radius of the second ball can not be larger than the radius of the first ball (up to a factor `τ`). This order corresponds to the order of choice in the inductive construction: otherwise, the second ball would have been chosen before. This is the condition `h`. Finally, the last ball is chosen after all the other ones, meaning that `h` can be strengthened by keeping only one side of the alternative in `hlast`. -/ structure besicovitch.satellite_config (α : Type*) [metric_space α] (N : ℕ) (τ : ℝ) := (c : fin N.succ → α) (r : fin N.succ → ℝ) (rpos : ∀ i, 0 < r i) (h : ∀ i j, i ≠ j → (r i ≤ dist (c i) (c j) ∧ r j ≤ τ * r i) ∨ (r j ≤ dist (c j) (c i) ∧ r i ≤ τ * r j)) (hlast : ∀ i < last N, r i ≤ dist (c i) (c (last N)) ∧ r (last N) ≤ τ * r i) (inter : ∀ i < last N, dist (c i) (c (last N)) ≤ r i + r (last N)) /-- A metric space has the Besicovitch covering property if there exist `N` and `τ > 1` such that there are no satellite configuration of parameter `τ` with `N+1` points. This is the condition that guarantees that the measurable Besicovitch covering theorem holds. It is satified by finite-dimensional real vector spaces. -/ class has_besicovitch_covering (α : Type*) [metric_space α] : Prop := (no_satellite_config [] : ∃ (N : ℕ) (τ : ℝ), 1 < τ ∧ is_empty (besicovitch.satellite_config α N τ)) /-- There is always a satellite configuration with a single point. -/ instance {α : Type*} {τ : ℝ} [inhabited α] [metric_space α] : inhabited (besicovitch.satellite_config α 0 τ) := ⟨{ c := λ i, default, r := λ i, 1, rpos := λ i, zero_lt_one, h := λ i j hij, (hij (subsingleton.elim i j)).elim, hlast := λ i hi, by { rw subsingleton.elim i (last 0) at hi, exact (lt_irrefl _ hi).elim }, inter := λ i hi, by { rw subsingleton.elim i (last 0) at hi, exact (lt_irrefl _ hi).elim } }⟩ namespace besicovitch namespace satellite_config variables {α : Type*} [metric_space α] {N : ℕ} {τ : ℝ} (a : satellite_config α N τ) lemma inter' (i : fin N.succ) : dist (a.c i) (a.c (last N)) ≤ a.r i + a.r (last N) := begin rcases lt_or_le i (last N) with H|H, { exact a.inter i H }, { have I : i = last N := top_le_iff.1 H, have := (a.rpos (last N)).le, simp only [I, add_nonneg this this, dist_self] } end lemma hlast' (i : fin N.succ) (h : 1 ≤ τ) : a.r (last N) ≤ τ * a.r i := begin rcases lt_or_le i (last N) with H|H, { exact (a.hlast i H).2 }, { have : i = last N := top_le_iff.1 H, rw this, exact le_mul_of_one_le_left (a.rpos _).le h } end end satellite_config /-! ### Extracting disjoint subfamilies from a ball covering -/ /-- A ball package is a family of balls in a metric space with positive bounded radii. -/ structure ball_package (β : Type*) (α : Type*) := (c : β → α) (r : β → ℝ) (rpos : ∀ b, 0 < r b) (r_bound : ℝ) (r_le : ∀ b, r b ≤ r_bound) /-- The ball package made of unit balls. -/ def unit_ball_package (α : Type*) : ball_package α α := { c := id, r := λ _, 1, rpos := λ _, zero_lt_one, r_bound := 1, r_le := λ _, le_rfl } instance (α : Type*) : inhabited (ball_package α α) := ⟨unit_ball_package α⟩ /-- A Besicovitch tau-package is a family of balls in a metric space with positive bounded radii, together with enough data to proceed with the Besicovitch greedy algorithm. We register this in a single structure to make sure that all our constructions in this algorithm only depend on one variable. -/ structure tau_package (β : Type*) (α : Type*) extends ball_package β α := (τ : ℝ) (one_lt_tau : 1 < τ) instance (α : Type*) : inhabited (tau_package α α) := ⟨{ τ := 2, one_lt_tau := one_lt_two, .. unit_ball_package α }⟩ variables {α : Type*} [metric_space α] {β : Type u} namespace tau_package variables [nonempty β] (p : tau_package β α) include p /-- Choose inductively large balls with centers that are not contained in the union of already chosen balls. This is a transfinite induction. -/ noncomputable def index : ordinal.{u} → β | i := -- `Z` is the set of points that are covered by already constructed balls let Z := ⋃ (j : {j // j < i}), ball (p.c (index j)) (p.r (index j)), -- `R` is the supremum of the radii of balls with centers not in `Z` R := supr (λ b : {b : β // p.c b ∉ Z}, p.r b) in -- return an index `b` for which the center `c b` is not in `Z`, and the radius is at -- least `R / τ`, if such an index exists (and garbage otherwise). classical.epsilon (λ b : β, p.c b ∉ Z ∧ R ≤ p.τ * p.r b) using_well_founded {dec_tac := `[exact j.2]} /-- The set of points that are covered by the union of balls selected at steps `< i`. -/ def Union_up_to (i : ordinal.{u}) : set α := ⋃ (j : {j // j < i}), ball (p.c (p.index j)) (p.r (p.index j)) lemma monotone_Union_up_to : monotone p.Union_up_to := begin assume i j hij, simp only [Union_up_to], exact Union_mono' (λ r, ⟨⟨r, r.2.trans_le hij⟩, subset.rfl⟩), end /-- Supremum of the radii of balls whose centers are not yet covered at step `i`. -/ def R (i : ordinal.{u}) : ℝ := supr (λ b : {b : β // p.c b ∉ p.Union_up_to i}, p.r b) /-- Group the balls into disjoint families, by assigning to a ball the smallest color for which it does not intersect any already chosen ball of this color. -/ noncomputable def color : ordinal.{u} → ℕ | i := let A : set ℕ := ⋃ (j : {j // j < i}) (hj : (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index i)) (p.r (p.index i))).nonempty), {color j} in Inf (univ \ A) using_well_founded {dec_tac := `[exact j.2]} /-- `p.last_step` is the first ordinal where the construction stops making sense, i.e., `f` returns garbage since there is no point left to be chosen. We will only use ordinals before this step. -/ def last_step : ordinal.{u} := Inf {i | ¬ ∃ (b : β), p.c b ∉ p.Union_up_to i ∧ p.R i ≤ p.τ * p.r b} lemma last_step_nonempty : {i | ¬ ∃ (b : β), p.c b ∉ p.Union_up_to i ∧ p.R i ≤ p.τ * p.r b}.nonempty := begin by_contra, suffices H : function.injective p.index, from not_injective_of_ordinal p.index H, assume x y hxy, wlog x_le_y : x ≤ y := le_total x y using [x y, y x], rcases eq_or_lt_of_le x_le_y with rfl|H, { refl }, simp only [nonempty_def, not_exists, exists_prop, not_and, not_lt, not_le, mem_set_of_eq, not_forall] at h, specialize h y, have A : p.c (p.index y) ∉ p.Union_up_to y, { have : p.index y = classical.epsilon (λ b : β, p.c b ∉ p.Union_up_to y ∧ p.R y ≤ p.τ * p.r b), by { rw [tau_package.index], refl }, rw this, exact (classical.epsilon_spec h).1 }, simp only [Union_up_to, not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_le, subtype.exists, subtype.coe_mk] at A, specialize A x H, simp [hxy] at A, exact (lt_irrefl _ ((p.rpos (p.index y)).trans_le A)).elim end /-- Every point is covered by chosen balls, before `p.last_step`. -/ lemma mem_Union_up_to_last_step (x : β) : p.c x ∈ p.Union_up_to p.last_step := begin have A : ∀ (z : β), p.c z ∈ p.Union_up_to p.last_step ∨ p.τ * p.r z < p.R p.last_step, { have : p.last_step ∈ {i | ¬ ∃ (b : β), p.c b ∉ p.Union_up_to i ∧ p.R i ≤ p.τ * p.r b} := Inf_mem p.last_step_nonempty, simpa only [not_exists, mem_set_of_eq, not_and_distrib, not_le, not_not_mem] }, by_contra, rcases A x with H|H, { exact h H }, have Rpos : 0 < p.R p.last_step, { apply lt_trans (mul_pos (_root_.zero_lt_one.trans p.one_lt_tau) (p.rpos _)) H }, have B : p.τ⁻¹ * p.R p.last_step < p.R p.last_step, { conv_rhs { rw ← one_mul (p.R p.last_step) }, exact mul_lt_mul (inv_lt_one p.one_lt_tau) le_rfl Rpos zero_le_one }, obtain ⟨y, hy1, hy2⟩ : ∃ (y : β), p.c y ∉ p.Union_up_to p.last_step ∧ (p.τ)⁻¹ * p.R p.last_step < p.r y, { simpa only [exists_prop, mem_range, exists_exists_and_eq_and, subtype.exists, subtype.coe_mk] using exists_lt_of_lt_cSup _ B, rw [← image_univ, nonempty_image_iff], exact ⟨⟨_, h⟩, mem_univ _⟩ }, rcases A y with Hy|Hy, { exact hy1 Hy }, { rw ← div_eq_inv_mul at hy2, have := (div_le_iff' (_root_.zero_lt_one.trans p.one_lt_tau)).1 hy2.le, exact lt_irrefl _ (Hy.trans_le this) } end /-- If there are no configurations of satellites with `N+1` points, one never uses more than `N` distinct families in the Besicovitch inductive construction. -/ lemma color_lt {i : ordinal.{u}} (hi : i < p.last_step) {N : ℕ} (hN : is_empty (satellite_config α N p.τ)) : p.color i < N := begin /- By contradiction, consider the first ordinal `i` for which one would have `p.color i = N`. Choose for each `k < N` a ball with color `k` that intersects the ball at color `i` (there is such a ball, otherwise one would have used the color `k` and not `N`). Then this family of `N+1` balls forms a satellite configuration, which is forbidden by the assumption `hN`. -/ induction i using ordinal.induction with i IH, let A : set ℕ := ⋃ (j : {j // j < i}) (hj : (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index i)) (p.r (p.index i))).nonempty), {p.color j}, have color_i : p.color i = Inf (univ \ A), by rw [color], rw color_i, have N_mem : N ∈ univ \ A, { simp only [not_exists, true_and, exists_prop, mem_Union, mem_singleton_iff, mem_closed_ball, not_and, mem_univ, mem_diff, subtype.exists, subtype.coe_mk], assume j ji hj, exact (IH j ji (ji.trans hi)).ne' }, suffices : Inf (univ \ A) ≠ N, { rcases (cInf_le (order_bot.bdd_below (univ \ A)) N_mem).lt_or_eq with H|H, { exact H }, { exact (this H).elim } }, assume Inf_eq_N, have : ∀ k, k < N → ∃ j, j < i ∧ (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index i)) (p.r (p.index i))).nonempty ∧ k = p.color j, { assume k hk, rw ← Inf_eq_N at hk, have : k ∈ A, by simpa only [true_and, mem_univ, not_not, mem_diff] using nat.not_mem_of_lt_Inf hk, simp at this, simpa only [exists_prop, mem_Union, mem_singleton_iff, mem_closed_ball, subtype.exists, subtype.coe_mk] }, choose! g hg using this, -- Choose for each `k < N` an ordinal `G k < i` giving a ball of color `k` intersecting -- the last ball. let G : ℕ → ordinal := λ n, if n = N then i else g n, have color_G : ∀ n, n ≤ N → p.color (G n) = n, { assume n hn, unfreezingI { rcases hn.eq_or_lt with rfl|H }, { simp only [G], simp only [color_i, Inf_eq_N, if_true, eq_self_iff_true] }, { simp only [G], simp only [H.ne, (hg n H).right.right.symm, if_false] } }, have G_lt_last : ∀ n, n ≤ N → G n < p.last_step, { assume n hn, unfreezingI { rcases hn.eq_or_lt with rfl|H }, { simp only [G], simp only [hi, if_true, eq_self_iff_true], }, { simp only [G], simp only [H.ne, (hg n H).left.trans hi, if_false] } }, have fGn : ∀ n, n ≤ N → p.c (p.index (G n)) ∉ p.Union_up_to (G n) ∧ p.R (G n) ≤ p.τ * p.r (p.index (G n)), { assume n hn, have: p.index (G n) = classical.epsilon (λ t, p.c t ∉ p.Union_up_to (G n) ∧ p.R (G n) ≤ p.τ * p.r t), by { rw index, refl }, rw this, have : ∃ t, p.c t ∉ p.Union_up_to (G n) ∧ p.R (G n) ≤ p.τ * p.r t, by simpa only [not_exists, exists_prop, not_and, not_lt, not_le, mem_set_of_eq, not_forall] using not_mem_of_lt_cInf (G_lt_last n hn) (order_bot.bdd_below _), exact classical.epsilon_spec this }, -- the balls with indices `G k` satisfy the characteristic property of satellite configurations. have Gab : ∀ (a b : fin (nat.succ N)), G a < G b → p.r (p.index (G a)) ≤ dist (p.c (p.index (G a))) (p.c (p.index (G b))) ∧ p.r (p.index (G b)) ≤ p.τ * p.r (p.index (G a)), { assume a b G_lt, have ha : (a : ℕ) ≤ N := nat.lt_succ_iff.1 a.2, have hb : (b : ℕ) ≤ N := nat.lt_succ_iff.1 b.2, split, { have := (fGn b hb).1, simp only [Union_up_to, not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_le, subtype.exists, subtype.coe_mk] at this, simpa only [dist_comm, mem_ball, not_lt] using this (G a) G_lt }, { apply le_trans _ (fGn a ha).2, have B : p.c (p.index (G b)) ∉ p.Union_up_to (G a), { assume H, exact (fGn b hb).1 (p.monotone_Union_up_to G_lt.le H) }, let b' : {t // p.c t ∉ p.Union_up_to (G a)} := ⟨p.index (G b), B⟩, apply @le_csupr _ _ _ (λ t : {t // p.c t ∉ p.Union_up_to (G a)}, p.r t) _ b', refine ⟨p.r_bound, λ t ht, _⟩, simp only [exists_prop, mem_range, subtype.exists, subtype.coe_mk] at ht, rcases ht with ⟨u, hu⟩, rw ← hu.2, exact p.r_le _ } }, -- therefore, one may use them to construct a satellite configuration with `N+1` points let sc : satellite_config α N p.τ := { c := λ k, p.c (p.index (G k)), r := λ k, p.r (p.index (G k)), rpos := λ k, p.rpos (p.index (G k)), h := begin assume a b a_ne_b, wlog G_le : G a ≤ G b := le_total (G a) (G b) using [a b, b a] tactic.skip, { have G_lt : G a < G b, { rcases G_le.lt_or_eq with H|H, { exact H }, have A : (a : ℕ) ≠ b := fin.coe_injective.ne a_ne_b, rw [← color_G a (nat.lt_succ_iff.1 a.2), ← color_G b (nat.lt_succ_iff.1 b.2), H] at A, exact (A rfl).elim }, exact or.inl (Gab a b G_lt) }, { assume a_ne_b, rw or_comm, exact this a_ne_b.symm } end, hlast := begin assume a ha, have I : (a : ℕ) < N := ha, have : G a < G (fin.last N), by { dsimp [G], simp [I.ne, (hg a I).1] }, exact Gab _ _ this, end, inter := begin assume a ha, have I : (a : ℕ) < N := ha, have J : G (fin.last N) = i, by { dsimp [G], simp only [if_true, eq_self_iff_true], }, have K : G a = g a, { dsimp [G], simp [I.ne, (hg a I).1] }, convert dist_le_add_of_nonempty_closed_ball_inter_closed_ball (hg _ I).2.1, end }, -- this is a contradiction exact (hN.false : _) sc end end tau_package open tau_package /-- The topological Besicovitch covering theorem: there exist finitely many families of disjoint balls covering all the centers in a package. More specifically, one can use `N` families if there are no satellite configurations with `N+1` points. -/ theorem exist_disjoint_covering_families {N : ℕ} {τ : ℝ} (hτ : 1 < τ) (hN : is_empty (satellite_config α N τ)) (q : ball_package β α) : ∃ s : fin N → set β, (∀ (i : fin N), (s i).pairwise_disjoint (λ j, closed_ball (q.c j) (q.r j))) ∧ (range q.c ⊆ ⋃ (i : fin N), ⋃ (j ∈ s i), ball (q.c j) (q.r j)) := begin -- first exclude the trivial case where `β` is empty (we need non-emptiness for the transfinite -- induction, to be able to choose garbage when there is no point left). casesI is_empty_or_nonempty β, { refine ⟨λ i, ∅, λ i, pairwise_disjoint_empty, _⟩, rw [← image_univ, eq_empty_of_is_empty (univ : set β)], simp }, -- Now, assume `β` is nonempty. let p : tau_package β α := { τ := τ, one_lt_tau := hτ, .. q }, -- we use for `s i` the balls of color `i`. let s := λ (i : fin N), ⋃ (k : ordinal.{u}) (hk : k < p.last_step) (h'k : p.color k = i), ({p.index k} : set β), refine ⟨s, λ i, _, _⟩, { -- show that balls of the same color are disjoint assume x hx y hy x_ne_y, obtain ⟨jx, jx_lt, jxi, rfl⟩ : ∃ (jx : ordinal), jx < p.last_step ∧ p.color jx = i ∧ x = p.index jx, by simpa only [exists_prop, mem_Union, mem_singleton_iff] using hx, obtain ⟨jy, jy_lt, jyi, rfl⟩ : ∃ (jy : ordinal), jy < p.last_step ∧ p.color jy = i ∧ y = p.index jy, by simpa only [exists_prop, mem_Union, mem_singleton_iff] using hy, wlog jxy : jx ≤ jy := le_total jx jy using [jx jy, jy jx] tactic.skip, swap, { assume h1 h2 h3 h4 h5 h6 h7, rw [function.on_fun, disjoint.comm], exact this h4 h5 h6 h1 h2 h3 h7.symm }, replace jxy : jx < jy, by { rcases lt_or_eq_of_le jxy with H|rfl, { exact H }, { exact (x_ne_y rfl).elim } }, let A : set ℕ := ⋃ (j : {j // j < jy}) (hj : (closed_ball (p.c (p.index j)) (p.r (p.index j)) ∩ closed_ball (p.c (p.index jy)) (p.r (p.index jy))).nonempty), {p.color j}, have color_j : p.color jy = Inf (univ \ A), by rw [tau_package.color], have : p.color jy ∈ univ \ A, { rw color_j, apply Inf_mem, refine ⟨N, _⟩, simp only [not_exists, true_and, exists_prop, mem_Union, mem_singleton_iff, not_and, mem_univ, mem_diff, subtype.exists, subtype.coe_mk], assume k hk H, exact (p.color_lt (hk.trans jy_lt) hN).ne' }, simp only [not_exists, true_and, exists_prop, mem_Union, mem_singleton_iff, not_and, mem_univ, mem_diff, subtype.exists, subtype.coe_mk] at this, specialize this jx jxy, contrapose! this, simpa only [jxi, jyi, and_true, eq_self_iff_true, ← not_disjoint_iff_nonempty_inter] }, { -- show that the balls of color at most `N` cover every center. refine range_subset_iff.2 (λ b, _), obtain ⟨a, ha⟩ : ∃ (a : ordinal), a < p.last_step ∧ dist (p.c b) (p.c (p.index a)) < p.r (p.index a), by simpa only [Union_up_to, exists_prop, mem_Union, mem_ball, subtype.exists, subtype.coe_mk] using p.mem_Union_up_to_last_step b, simp only [exists_prop, mem_Union, mem_ball, mem_singleton_iff, bUnion_and', exists_eq_left, Union_exists, exists_and_distrib_left], exact ⟨⟨p.color a, p.color_lt ha.1 hN⟩, a, rfl, ha⟩ } end /-! ### The measurable Besicovitch covering theorem -/ open_locale nnreal variables [second_countable_topology α] [measurable_space α] [opens_measurable_space α] /-- Consider, for each `x` in a set `s`, a radius `r x ∈ (0, 1]`. Then one can find finitely many disjoint balls of the form `closed_ball x (r x)` covering a proportion `1/(N+1)` of `s`, if there are no satellite configurations with `N+1` points. -/ lemma exist_finset_disjoint_balls_large_measure (μ : measure α) [is_finite_measure μ] {N : ℕ} {τ : ℝ} (hτ : 1 < τ) (hN : is_empty (satellite_config α N τ)) (s : set α) (r : α → ℝ) (rpos : ∀ x ∈ s, 0 < r x) (rle : ∀ x ∈ s, r x ≤ 1) : ∃ (t : finset α), (↑t ⊆ s) ∧ μ (s \ (⋃ (x ∈ t), closed_ball x (r x))) ≤ N/(N+1) * μ s ∧ (t : set α).pairwise_disjoint (λ x, closed_ball x (r x)) := begin -- exclude the trivial case where `μ s = 0`. rcases le_or_lt (μ s) 0 with hμs|hμs, { have : μ s = 0 := le_bot_iff.1 hμs, refine ⟨∅, by simp only [finset.coe_empty, empty_subset], _, _⟩, { simp only [this, diff_empty, Union_false, Union_empty, nonpos_iff_eq_zero, mul_zero] }, { simp only [finset.coe_empty, pairwise_disjoint_empty], } }, casesI is_empty_or_nonempty α, { simp only [eq_empty_of_is_empty s, measure_empty] at hμs, exact (lt_irrefl _ hμs).elim }, have Npos : N ≠ 0, { unfreezingI { rintros rfl }, inhabit α, exact (not_is_empty_of_nonempty _) hN }, -- introduce a measurable superset `o` with the same measure, for measure computations obtain ⟨o, so, omeas, μo⟩ : ∃ (o : set α), s ⊆ o ∧ measurable_set o ∧ μ o = μ s := exists_measurable_superset μ s, /- We will apply the topological Besicovitch theorem, giving `N` disjoint subfamilies of balls covering `s`. Among these, one of them covers a proportion at least `1/N` of `s`. A large enough finite subfamily will then cover a proportion at least `1/(N+1)`. -/ let a : ball_package s α := { c := λ x, x, r := λ x, r x, rpos := λ x, rpos x x.2, r_bound := 1, r_le := λ x, rle x x.2 }, rcases exist_disjoint_covering_families hτ hN a with ⟨u, hu, hu'⟩, have u_count : ∀ i, countable (u i), { assume i, refine (hu i).countable_of_nonempty_interior (λ j hj, _), have : (ball (j : α) (r j)).nonempty := nonempty_ball.2 (a.rpos _), exact this.mono ball_subset_interior_closed_ball }, let v : fin N → set α := λ i, ⋃ (x : s) (hx : x ∈ u i), closed_ball x (r x), have : ∀ i, measurable_set (v i) := λ i, measurable_set.bUnion (u_count i) (λ b hb, measurable_set_closed_ball), have A : s = ⋃ (i : fin N), s ∩ v i, { refine subset.antisymm _ (Union_subset (λ i, inter_subset_left _ _)), assume x hx, obtain ⟨i, y, hxy, h'⟩ : ∃ (i : fin N) (i_1 : ↥s) (i : i_1 ∈ u i), x ∈ ball ↑i_1 (r ↑i_1), { have : x ∈ range a.c, by simpa only [subtype.range_coe_subtype, set_of_mem_eq], simpa only [mem_Union] using hu' this }, refine mem_Union.2 ⟨i, ⟨hx, _⟩⟩, simp only [v, exists_prop, mem_Union, set_coe.exists, exists_and_distrib_right, subtype.coe_mk], exact ⟨y, ⟨y.2, by simpa only [subtype.coe_eta]⟩, ball_subset_closed_ball h'⟩ }, have S : ∑ (i : fin N), μ s / N ≤ ∑ i, μ (s ∩ v i) := calc ∑ (i : fin N), μ s / N = μ s : begin simp only [finset.card_fin, finset.sum_const, nsmul_eq_mul], rw ennreal.mul_div_cancel', { simp only [Npos, ne.def, nat.cast_eq_zero, not_false_iff] }, { exact ennreal.coe_nat_ne_top } end ... ≤ ∑ i, μ (s ∩ v i) : by { conv_lhs { rw A }, apply measure_Union_fintype_le }, -- choose an index `i` of a subfamily covering at least a proportion `1/N` of `s`. obtain ⟨i, -, hi⟩ : ∃ (i : fin N) (hi : i ∈ finset.univ), μ s / N ≤ μ (s ∩ v i), { apply ennreal.exists_le_of_sum_le _ S, exact ⟨⟨0, bot_lt_iff_ne_bot.2 Npos⟩, finset.mem_univ _⟩ }, replace hi : μ s / (N + 1) < μ (s ∩ v i), { apply lt_of_lt_of_le _ hi, apply (ennreal.mul_lt_mul_left hμs.ne' (measure_lt_top μ s).ne).2, rw ennreal.inv_lt_inv, conv_lhs {rw ← add_zero (N : ℝ≥0∞) }, exact ennreal.add_lt_add_left (ennreal.nat_ne_top N) ennreal.zero_lt_one }, have B : μ (o ∩ v i) = ∑' (x : u i), μ (o ∩ closed_ball x (r x)), { have : o ∩ v i = ⋃ (x : s) (hx : x ∈ u i), o ∩ closed_ball x (r x), by simp only [inter_Union], rw [this, measure_bUnion (u_count i)], { refl }, { exact (hu i).mono (λ k, inter_subset_right _ _) }, { exact λ b hb, omeas.inter measurable_set_closed_ball } }, -- A large enough finite subfamily of `u i` will also cover a proportion `> 1/(N+1)` of `s`. -- Since `s` might not be measurable, we express this in terms of the measurable superset `o`. obtain ⟨w, hw⟩ : ∃ (w : finset (u i)), μ s / (N + 1) < ∑ (x : u i) in w, μ (o ∩ closed_ball (x : α) (r (x : α))), { have C : has_sum (λ (x : u i), μ (o ∩ closed_ball x (r x))) (μ (o ∩ v i)), by { rw B, exact ennreal.summable.has_sum }, have : μ s / (N+1) < μ (o ∩ v i) := hi.trans_le (measure_mono (inter_subset_inter_left _ so)), exact ((tendsto_order.1 C).1 _ this).exists }, -- Bring back the finset `w i` of `↑(u i)` to a finset of `α`, and check that it works by design. refine ⟨finset.image (λ (x : u i), x) w, _, _, _⟩, -- show that the finset is included in `s`. { simp only [image_subset_iff, coe_coe, finset.coe_image], assume y hy, simp only [subtype.coe_prop, mem_preimage] }, -- show that it covers a large enough proportion of `s`. For measure computations, we do not -- use `s` (which might not be measurable), but its measurable superset `o`. Since their measures -- are the same, this does not spoil the estimates { suffices H : μ (o \ ⋃ x ∈ w, closed_ball ↑x (r ↑x)) ≤ N/(N+1) * μ s, { rw [finset.set_bUnion_finset_image], exact le_trans (measure_mono (diff_subset_diff so (subset.refl _))) H }, rw [← diff_inter_self_eq_diff, measure_diff_le_iff_le_add _ (inter_subset_right _ _) ((measure_lt_top μ _).ne)], swap, { apply measurable_set.inter _ omeas, haveI : encodable (u i) := (u_count i).to_encodable, exact measurable_set.Union (λ b, measurable_set.Union_Prop (λ hb, measurable_set_closed_ball)) }, calc μ o = 1/(N+1) * μ s + N/(N+1) * μ s : by { rw [μo, ← add_mul, ennreal.div_add_div_same, add_comm, ennreal.div_self, one_mul]; simp } ... ≤ μ ((⋃ (x ∈ w), closed_ball ↑x (r ↑x)) ∩ o) + N/(N+1) * μ s : begin refine add_le_add _ le_rfl, rw [div_eq_mul_inv, one_mul, mul_comm, ← div_eq_mul_inv], apply hw.le.trans (le_of_eq _), rw [← finset.set_bUnion_coe, inter_comm _ o, inter_Union₂, finset.set_bUnion_coe, measure_bUnion_finset], { have : (w : set (u i)).pairwise_disjoint (λ (b : u i), closed_ball (b : α) (r (b : α))), by { assume k hk l hl hkl, exact hu i k.2 l.2 (subtype.coe_injective.ne hkl) }, exact this.mono (λ k, inter_subset_right _ _) }, { assume b hb, apply omeas.inter measurable_set_closed_ball } end }, -- show that the balls are disjoint { assume k hk l hl hkl, obtain ⟨k', k'w, rfl⟩ : ∃ (k' : u i), k' ∈ w ∧ ↑↑k' = k, by simpa only [mem_image, finset.mem_coe, coe_coe, finset.coe_image] using hk, obtain ⟨l', l'w, rfl⟩ : ∃ (l' : u i), l' ∈ w ∧ ↑↑l' = l, by simpa only [mem_image, finset.mem_coe, coe_coe, finset.coe_image] using hl, have k'nel' : (k' : s) ≠ l', by { assume h, rw h at hkl, exact hkl rfl }, exact hu i k'.2 l'.2 k'nel' } end variable [has_besicovitch_covering α] /-- The measurable Besicovitch covering theorem. Assume that, for any `x` in a set `s`, one is given a set of admissible closed balls centered at `x`, with arbitrarily small radii. Then there exists a disjoint covering of almost all `s` by admissible closed balls centered at some points of `s`. This version requires that the underlying measure is finite, and that the space has the Besicovitch covering property (which is satisfied for instance by normed real vector spaces). It expresses the conclusion in a slightly awkward form (with a subset of `α × ℝ`) coming from the proof technique. For a version assuming that the measure is sigma-finite, see `exists_disjoint_closed_ball_covering_ae_aux`. For a version giving the conclusion in a nicer form, see `exists_disjoint_closed_ball_covering_ae`. -/ theorem exists_disjoint_closed_ball_covering_ae_of_finite_measure_aux (μ : measure α) [is_finite_measure μ] (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) : ∃ (t : set (α × ℝ)), (countable t) ∧ (∀ (p : α × ℝ), p ∈ t → p.1 ∈ s) ∧ (∀ (p : α × ℝ), p ∈ t → p.2 ∈ f p.1) ∧ μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2)) = 0 ∧ t.pairwise_disjoint (λ p, closed_ball p.1 p.2) := begin rcases has_besicovitch_covering.no_satellite_config α with ⟨N, τ, hτ, hN⟩, /- Introduce a property `P` on finsets saying that we have a nice disjoint covering of a subset of `s` by admissible balls. -/ let P : finset (α × ℝ) → Prop := λ t, (t : set (α × ℝ)).pairwise_disjoint (λ p, closed_ball p.1 p.2) ∧ (∀ (p : α × ℝ), p ∈ t → p.1 ∈ s) ∧ (∀ (p : α × ℝ), p ∈ t → p.2 ∈ f p.1), /- Given a finite good covering of a subset `s`, one can find a larger finite good covering, covering additionally a proportion at least `1/(N+1)` of leftover points. This follows from `exist_finset_disjoint_balls_large_measure` applied to balls not intersecting the initial covering. -/ have : ∀ (t : finset (α × ℝ)), P t → ∃ (u : finset (α × ℝ)), t ⊆ u ∧ P u ∧ μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ u), closed_ball p.1 p.2)) ≤ N/(N+1) * μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2)), { assume t ht, set B := ⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2 with hB, have B_closed : is_closed B := is_closed_bUnion (finset.finite_to_set _) (λ i hi, is_closed_ball), set s' := s \ B with hs', have : ∀ x ∈ s', ∃ r ∈ f x ∩ Ioo 0 1, disjoint B (closed_ball x r), { assume x hx, have xs : x ∈ s := ((mem_diff x).1 hx).1, rcases eq_empty_or_nonempty B with hB|hB, { have : (0 : ℝ) < 1 := zero_lt_one, rcases hf x xs 1 zero_lt_one with ⟨r, hr, h'r⟩, exact ⟨r, ⟨hr, h'r⟩, by simp only [hB, empty_disjoint]⟩ }, { let R := inf_dist x B, have : 0 < min R 1 := lt_min ((B_closed.not_mem_iff_inf_dist_pos hB).1 ((mem_diff x).1 hx).2) zero_lt_one, rcases hf x xs _ this with ⟨r, hr, h'r⟩, refine ⟨r, ⟨hr, ⟨h'r.1, h'r.2.trans_le (min_le_right _ _)⟩⟩, _⟩, rw disjoint.comm, exact disjoint_closed_ball_of_lt_inf_dist (h'r.2.trans_le (min_le_left _ _)) } }, choose! r hr using this, obtain ⟨v, vs', hμv, hv⟩ : ∃ (v : finset α), ↑v ⊆ s' ∧ μ (s' \ ⋃ (x ∈ v), closed_ball x (r x)) ≤ N/(N+1) * μ s' ∧ (v : set α).pairwise_disjoint (λ (x : α), closed_ball x (r x)), { have rI : ∀ x ∈ s', r x ∈ Ioo (0 : ℝ) 1 := λ x hx, (hr x hx).1.2, exact exist_finset_disjoint_balls_large_measure μ hτ hN s' r (λ x hx, (rI x hx).1) (λ x hx, (rI x hx).2.le) }, refine ⟨t ∪ (finset.image (λ x, (x, r x)) v), finset.subset_union_left _ _, ⟨_, _, _⟩, _⟩, { simp only [finset.coe_union, pairwise_disjoint_union, ht.1, true_and, finset.coe_image], split, { assume p hp q hq hpq, rcases (mem_image _ _ _).1 hp with ⟨p', p'v, rfl⟩, rcases (mem_image _ _ _).1 hq with ⟨q', q'v, rfl⟩, refine hv p'v q'v (λ hp'q', _), rw [hp'q'] at hpq, exact hpq rfl }, { assume p hp q hq hpq, rcases (mem_image _ _ _).1 hq with ⟨q', q'v, rfl⟩, apply disjoint_of_subset_left _ (hr q' (vs' q'v)).2, rw [hB, ← finset.set_bUnion_coe], exact subset_bUnion_of_mem hp } }, { assume p hp, rcases finset.mem_union.1 hp with h'p|h'p, { exact ht.2.1 p h'p }, { rcases finset.mem_image.1 h'p with ⟨p', p'v, rfl⟩, exact ((mem_diff _).1 (vs' (finset.mem_coe.2 p'v))).1 } }, { assume p hp, rcases finset.mem_union.1 hp with h'p|h'p, { exact ht.2.2 p h'p }, { rcases finset.mem_image.1 h'p with ⟨p', p'v, rfl⟩, exact (hr p' (vs' p'v)).1.1 } }, { convert hμv using 2, rw [finset.set_bUnion_union, ← diff_diff, finset.set_bUnion_finset_image] } }, /- Define `F` associating to a finite good covering the above enlarged good covering, covering a proportion `1/(N+1)` of leftover points. Iterating `F`, one will get larger and larger good coverings, missing in the end only a measure-zero set. -/ choose! F hF using this, let u := λ n, F^[n] ∅, have u_succ : ∀ (n : ℕ), u n.succ = F (u n) := λ n, by simp only [u, function.comp_app, function.iterate_succ'], have Pu : ∀ n, P (u n), { assume n, induction n with n IH, { simp only [u, P, prod.forall, id.def, function.iterate_zero], simp only [finset.not_mem_empty, forall_false_left, finset.coe_empty, forall_2_true_iff, and_self, pairwise_disjoint_empty] }, { rw u_succ, exact (hF (u n) IH).2.1 } }, refine ⟨⋃ n, u n, countable_Union (λ n, (u n).countable_to_set), _, _, _, _⟩, { assume p hp, rcases mem_Union.1 hp with ⟨n, hn⟩, exact (Pu n).2.1 p (finset.mem_coe.1 hn) }, { assume p hp, rcases mem_Union.1 hp with ⟨n, hn⟩, exact (Pu n).2.2 p (finset.mem_coe.1 hn) }, { have A : ∀ n, μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ ⋃ (n : ℕ), (u n : set (α × ℝ))), closed_ball p.fst p.snd) ≤ μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n), closed_ball p.fst p.snd), { assume n, apply measure_mono, apply diff_subset_diff (subset.refl _), exact bUnion_subset_bUnion_left (subset_Union (λ i, (u i : set (α × ℝ))) n) }, have B : ∀ n, μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n), closed_ball p.fst p.snd) ≤ (N/(N+1))^n * μ s, { assume n, induction n with n IH, { simp only [le_refl, diff_empty, one_mul, Union_false, Union_empty, pow_zero] }, calc μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n.succ), closed_ball p.fst p.snd) ≤ (N/(N+1)) * μ (s \ ⋃ (p : α × ℝ) (hp : p ∈ u n), closed_ball p.fst p.snd) : by { rw u_succ, exact (hF (u n) (Pu n)).2.2 } ... ≤ (N/(N+1))^n.succ * μ s : by { rw [pow_succ, mul_assoc], exact ennreal.mul_le_mul le_rfl IH } }, have C : tendsto (λ (n : ℕ), ((N : ℝ≥0∞)/(N+1))^n * μ s) at_top (𝓝 (0 * μ s)), { apply ennreal.tendsto.mul_const _ (or.inr (measure_lt_top μ s).ne), apply ennreal.tendsto_pow_at_top_nhds_0_of_lt_1, rw [ennreal.div_lt_iff, one_mul], { conv_lhs {rw ← add_zero (N : ℝ≥0∞) }, exact ennreal.add_lt_add_left (ennreal.nat_ne_top N) ennreal.zero_lt_one }, { simp only [true_or, add_eq_zero_iff, ne.def, not_false_iff, one_ne_zero, and_false] }, { simp only [ennreal.nat_ne_top, ne.def, not_false_iff, or_true] } }, rw zero_mul at C, apply le_bot_iff.1, exact le_of_tendsto_of_tendsto' tendsto_const_nhds C (λ n, (A n).trans (B n)) }, { refine (pairwise_disjoint_Union _).2 (λ n, (Pu n).1), apply (monotone_nat_of_le_succ (λ n, _)).directed_le, rw u_succ, exact (hF (u n) (Pu n)).1 } end /-- The measurable Besicovitch covering theorem. Assume that, for any `x` in a set `s`, one is given a set of admissible closed balls centered at `x`, with arbitrarily small radii. Then there exists a disjoint covering of almost all `s` by admissible closed balls centered at some points of `s`. This version requires that the underlying measure is sigma-finite, and that the space has the Besicovitch covering property (which is satisfied for instance by normed real vector spaces). It expresses the conclusion in a slightly awkward form (with a subset of `α × ℝ`) coming from the proof technique. For a version giving the conclusion in a nicer form, see `exists_disjoint_closed_ball_covering_ae`. -/ theorem exists_disjoint_closed_ball_covering_ae_aux (μ : measure α) [sigma_finite μ] (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) : ∃ (t : set (α × ℝ)), (countable t) ∧ (∀ (p : α × ℝ), p ∈ t → p.1 ∈ s) ∧ (∀ (p : α × ℝ), p ∈ t → p.2 ∈ f p.1) ∧ μ (s \ (⋃ (p : α × ℝ) (hp : p ∈ t), closed_ball p.1 p.2)) = 0 ∧ t.pairwise_disjoint (λ p, closed_ball p.1 p.2) := begin /- This is deduced from the finite measure case, by using a finite measure with respect to which the initial sigma-finite measure is absolutely continuous. -/ unfreezingI { rcases exists_absolutely_continuous_is_finite_measure μ with ⟨ν, hν, hμν⟩ }, rcases exists_disjoint_closed_ball_covering_ae_of_finite_measure_aux ν f s hf with ⟨t, t_count, ts, tr, tν, tdisj⟩, exact ⟨t, t_count, ts, tr, hμν tν, tdisj⟩, end /-- The measurable Besicovitch covering theorem. Assume that, for any `x` in a set `s`, one is given a set of admissible closed balls centered at `x`, with arbitrarily small radii. Then there exists a disjoint covering of almost all `s` by admissible closed balls centered at some points of `s`. We can even require that the radius at `x` is bounded by a given function `R x`. (Take `R = 1` if you don't need this additional feature). This version requires that the underlying measure is sigma-finite, and that the space has the Besicovitch covering property (which is satisfied for instance by normed real vector spaces). -/ theorem exists_disjoint_closed_ball_covering_ae (μ : measure α) [sigma_finite μ] (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) (R : α → ℝ) (hR : ∀ x ∈ s, 0 < R x): ∃ (t : set α) (r : α → ℝ), countable t ∧ t ⊆ s ∧ (∀ x ∈ t, r x ∈ f x ∩ Ioo 0 (R x)) ∧ μ (s \ (⋃ (x ∈ t), closed_ball x (r x))) = 0 ∧ t.pairwise_disjoint (λ x, closed_ball x (r x)) := begin let g := λ x, f x ∩ Ioo 0 (R x), have hg : ∀ x ∈ s, ∀ δ > 0, (g x ∩ Ioo 0 δ).nonempty, { assume x hx δ δpos, rcases hf x hx (min δ (R x)) (lt_min δpos (hR x hx)) with ⟨r, hr⟩, exact ⟨r, ⟨⟨hr.1, hr.2.1, hr.2.2.trans_le (min_le_right _ _)⟩, ⟨hr.2.1, hr.2.2.trans_le (min_le_left _ _)⟩⟩⟩ }, rcases exists_disjoint_closed_ball_covering_ae_aux μ g s hg with ⟨v, v_count, vs, vg, μv, v_disj⟩, let t := prod.fst '' v, have : ∀ x ∈ t, ∃ (r : ℝ), (x, r) ∈ v, { assume x hx, rcases (mem_image _ _ _).1 hx with ⟨⟨p, q⟩, hp, rfl⟩, exact ⟨q, hp⟩ }, choose! r hr using this, have im_t : (λ x, (x, r x)) '' t = v, { have I : ∀ (p : α × ℝ), p ∈ v → 0 ≤ p.2 := λ p hp, (vg p hp).2.1.le, apply subset.antisymm, { simp only [image_subset_iff], rintros ⟨x, p⟩ hxp, simp only [mem_preimage], exact hr _ (mem_image_of_mem _ hxp) }, { rintros ⟨x, p⟩ hxp, have hxrx : (x, r x) ∈ v := hr _ (mem_image_of_mem _ hxp), have : p = r x, { by_contra, have A : (x, p) ≠ (x, r x), by simpa only [true_and, prod.mk.inj_iff, eq_self_iff_true, ne.def] using h, have H := v_disj hxp hxrx A, contrapose H, rw not_disjoint_iff_nonempty_inter, refine ⟨x, by simp [I _ hxp, I _ hxrx]⟩ }, rw this, apply mem_image_of_mem, exact mem_image_of_mem _ hxp } }, refine ⟨t, r, v_count.image _, _, _, _, _⟩, { assume x hx, rcases (mem_image _ _ _).1 hx with ⟨⟨p, q⟩, hp, rfl⟩, exact vs _ hp }, { assume x hx, rcases (mem_image _ _ _).1 hx with ⟨⟨p, q⟩, hp, rfl⟩, exact vg _ (hr _ hx) }, { have : (⋃ (x : α) (H : x ∈ t), closed_ball x (r x)) = (⋃ (p : α × ℝ) (H : p ∈ (λ x, (x, r x)) '' t), closed_ball p.1 p.2), by conv_rhs { rw bUnion_image }, rw [this, im_t], exact μv }, { have A : inj_on (λ x : α, (x, r x)) t, by simp only [inj_on, prod.mk.inj_iff, implies_true_iff, eq_self_iff_true] {contextual := tt}, rwa [← im_t, A.pairwise_disjoint_image] at v_disj } end /-- In a space with the Besicovitch property, any set `s` can be covered with balls whose measures add up to at most `μ s + ε`, for any positive `ε`. This works even if one restricts the set of allowed radii around a point `x` to a set `f x` which accumulates at `0`. -/ theorem exists_closed_ball_covering_tsum_measure_le (μ : measure α) [sigma_finite μ] [measure.outer_regular μ] {ε : ℝ≥0∞} (hε : ε ≠ 0) (f : α → set ℝ) (s : set α) (hf : ∀ x ∈ s, ∀ δ > 0, (f x ∩ Ioo 0 δ).nonempty) : ∃ (t : set α) (r : α → ℝ), countable t ∧ t ⊆ s ∧ (∀ x ∈ t, r x ∈ f x) ∧ s ⊆ (⋃ (x ∈ t), closed_ball x (r x)) ∧ ∑' (x : t), μ (closed_ball x (r x)) ≤ μ s + ε := begin /- For the proof, first cover almost all `s` with disjoint balls thanks to the usual Besicovitch theorem. Taking the balls included in a well-chosen open neighborhood `u` of `s`, one may ensure that their measures add at most to `μ s + ε / 2`. Let `s'` be the remaining set, of measure `0`. Applying the other version of Besicovitch, one may cover it with at most `N` disjoint subfamilies. Making sure that they are all included in a neighborhood `v` of `s'` of measure at most `ε / (2 N)`, the sum of their measures is at most `ε / 2`, completing the proof. -/ obtain ⟨u, su, u_open, μu⟩ : ∃ U ⊇ s, is_open U ∧ μ U ≤ μ s + ε / 2 := set.exists_is_open_le_add _ _ (by simpa only [or_false, ne.def, ennreal.div_zero_iff, ennreal.one_ne_top, ennreal.bit0_eq_top_iff] using hε), have : ∀ x ∈ s, ∃ R > 0, ball x R ⊆ u := λ x hx, metric.mem_nhds_iff.1 (u_open.mem_nhds (su hx)), choose! R hR using this, obtain ⟨t0, r0, t0_count, t0s, hr0, μt0, t0_disj⟩ : ∃ (t0 : set α) (r0 : α → ℝ), countable t0 ∧ t0 ⊆ s ∧ (∀ x ∈ t0, r0 x ∈ f x ∩ Ioo 0 (R x)) ∧ μ (s \ (⋃ (x ∈ t0), closed_ball x (r0 x))) = 0 ∧ t0.pairwise_disjoint (λ x, closed_ball x (r0 x)) := exists_disjoint_closed_ball_covering_ae μ f s hf R (λ x hx, (hR x hx).1), -- we have constructed an almost everywhere covering of `s` by disjoint balls. Let `s'` be the -- remaining set. let s' := s \ (⋃ (x ∈ t0), closed_ball x (r0 x)), have s's : s' ⊆ s := diff_subset _ _, obtain ⟨N, τ, hτ, H⟩ : ∃ N τ, 1 < τ ∧ is_empty (besicovitch.satellite_config α N τ) := has_besicovitch_covering.no_satellite_config α, obtain ⟨v, s'v, v_open, μv⟩ : ∃ v ⊇ s', is_open v ∧ μ v ≤ μ s' + (ε / 2) / N := set.exists_is_open_le_add _ _ (by simp only [hε, ennreal.nat_ne_top, with_top.mul_eq_top_iff, ne.def, ennreal.div_zero_iff, ennreal.one_ne_top, not_false_iff, and_false, false_and, or_self, ennreal.bit0_eq_top_iff]), have : ∀ x ∈ s', ∃ r1 ∈ (f x ∩ Ioo (0 : ℝ) 1), closed_ball x r1 ⊆ v, { assume x hx, rcases metric.mem_nhds_iff.1 (v_open.mem_nhds (s'v hx)) with ⟨r, rpos, hr⟩, rcases hf x (s's hx) (min r 1) (lt_min rpos zero_lt_one) with ⟨R', hR'⟩, exact ⟨R', ⟨hR'.1, hR'.2.1, hR'.2.2.trans_le (min_le_right _ _)⟩, subset.trans (closed_ball_subset_ball (hR'.2.2.trans_le (min_le_left _ _))) hr⟩, }, choose! r1 hr1 using this, let q : ball_package s' α := { c := λ x, x, r := λ x, r1 x, rpos := λ x, (hr1 x.1 x.2).1.2.1, r_bound := 1, r_le := λ x, (hr1 x.1 x.2).1.2.2.le }, -- by Besicovitch, we cover `s'` with at most `N` families of disjoint balls, all included in -- a suitable neighborhood `v` of `s'`. obtain ⟨S, S_disj, hS⟩ : ∃ S : fin N → set s', (∀ (i : fin N), (S i).pairwise_disjoint (λ j, closed_ball (q.c j) (q.r j))) ∧ (range q.c ⊆ ⋃ (i : fin N), ⋃ (j ∈ S i), ball (q.c j) (q.r j)) := exist_disjoint_covering_families hτ H q, have S_count : ∀ i, countable (S i), { assume i, apply (S_disj i).countable_of_nonempty_interior (λ j hj, _), have : (ball (j : α) (r1 j)).nonempty := nonempty_ball.2 (q.rpos _), exact this.mono ball_subset_interior_closed_ball }, let r := λ x, if x ∈ s' then r1 x else r0 x, have r_t0 : ∀ x ∈ t0, r x = r0 x, { assume x hx, have : ¬ (x ∈ s'), { simp only [not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_lt, not_le, mem_diff, not_forall], assume h'x, refine ⟨x, hx, _⟩, rw dist_self, exact (hr0 x hx).2.1.le }, simp only [r, if_neg this] }, -- the desired covering set is given by the union of the families constructed in the first and -- second steps. refine ⟨t0 ∪ (⋃ (i : fin N), (coe : s' → α) '' (S i)), r, _, _, _, _, _⟩, -- it remains to check that they have the desired properties { exact t0_count.union (countable_Union (λ i, (S_count i).image _)) }, { simp only [t0s, true_and, union_subset_iff, image_subset_iff, Union_subset_iff], assume i x hx, exact s's x.2 }, { assume x hx, cases hx, { rw r_t0 x hx, exact (hr0 _ hx).1 }, { have h'x : x ∈ s', { simp only [mem_Union, mem_image] at hx, rcases hx with ⟨i, y, ySi, rfl⟩, exact y.2 }, simp only [r, if_pos h'x, (hr1 x h'x).1.1] } }, { assume x hx, by_cases h'x : x ∈ s', { obtain ⟨i, y, ySi, xy⟩ : ∃ (i : fin N) (y : ↥s') (ySi : y ∈ S i), x ∈ ball (y : α) (r1 y), { have A : x ∈ range q.c, by simpa only [not_exists, exists_prop, mem_Union, mem_closed_ball, not_and, not_le, mem_set_of_eq, subtype.range_coe_subtype, mem_diff] using h'x, simpa only [mem_Union, mem_image] using hS A }, refine mem_Union₂.2 ⟨y, or.inr _, _⟩, { simp only [mem_Union, mem_image], exact ⟨i, y, ySi, rfl⟩ }, { have : (y : α) ∈ s' := y.2, simp only [r, if_pos this], exact ball_subset_closed_ball xy } }, { obtain ⟨y, yt0, hxy⟩ : ∃ (y : α), y ∈ t0 ∧ x ∈ closed_ball y (r0 y), by simpa [hx, -mem_closed_ball] using h'x, refine mem_Union₂.2 ⟨y, or.inl yt0, _⟩, rwa r_t0 _ yt0 } }, -- the only nontrivial property is the measure control, which we check now { -- the sets in the first step have measure at most `μ s + ε / 2` have A : ∑' (x : t0), μ (closed_ball x (r x)) ≤ μ s + ε / 2 := calc ∑' (x : t0), μ (closed_ball x (r x)) = ∑' (x : t0), μ (closed_ball x (r0 x)) : by { congr' 1, ext x, rw r_t0 x x.2 } ... = μ (⋃ (x : t0), closed_ball x (r0 x)) : begin haveI : encodable t0 := t0_count.to_encodable, rw measure_Union, { exact (pairwise_subtype_iff_pairwise_set _ _).2 t0_disj }, { exact λ i, measurable_set_closed_ball } end ... ≤ μ u : begin apply measure_mono, simp only [set_coe.forall, subtype.coe_mk, Union_subset_iff], assume x hx, apply subset.trans (closed_ball_subset_ball (hr0 x hx).2.2) (hR x (t0s hx)).2, end ... ≤ μ s + ε / 2 : μu, -- each subfamily in the second step has measure at most `ε / (2 N)`. have B : ∀ (i : fin N), ∑' (x : (coe : s' → α) '' (S i)), μ (closed_ball x (r x)) ≤ (ε / 2) / N := λ i, calc ∑' (x : (coe : s' → α) '' (S i)), μ (closed_ball x (r x)) = ∑' (x : S i), μ (closed_ball x (r x)) : begin have : inj_on (coe : s' → α) (S i) := subtype.coe_injective.inj_on _, let F : S i ≃ (coe : s' → α) '' (S i) := this.bij_on_image.equiv _, exact (F.tsum_eq (λ x, μ (closed_ball x (r x)))).symm, end ... = ∑' (x : S i), μ (closed_ball x (r1 x)) : by { congr' 1, ext x, have : (x : α) ∈ s' := x.1.2, simp only [r, if_pos this] } ... = μ (⋃ (x : S i), closed_ball x (r1 x)) : begin haveI : encodable (S i) := (S_count i).to_encodable, rw measure_Union, { exact (pairwise_subtype_iff_pairwise_set _ _).2 (S_disj i) }, { exact λ i, measurable_set_closed_ball } end ... ≤ μ v : begin apply measure_mono, simp only [set_coe.forall, subtype.coe_mk, Union_subset_iff], assume x xs' xSi, exact (hr1 x xs').2, end ... ≤ (ε / 2) / N : by { have : μ s' = 0 := μt0, rwa [this, zero_add] at μv }, -- add up all these to prove the desired estimate calc ∑' (x : (t0 ∪ ⋃ (i : fin N), (coe : s' → α) '' S i)), μ (closed_ball x (r x)) ≤ ∑' (x : t0), μ (closed_ball x (r x)) + ∑' (x : ⋃ (i : fin N), (coe : s' → α) '' S i), μ (closed_ball x (r x)) : ennreal.tsum_union_le (λ x, μ (closed_ball x (r x))) _ _ ... ≤ ∑' (x : t0), μ (closed_ball x (r x)) + ∑ (i : fin N), ∑' (x : (coe : s' → α) '' S i), μ (closed_ball x (r x)) : add_le_add le_rfl (ennreal.tsum_Union_le (λ x, μ (closed_ball x (r x))) _) ... ≤ (μ s + ε / 2) + ∑ (i : fin N), (ε / 2) / N : begin refine add_le_add A _, refine finset.sum_le_sum _, assume i hi, exact B i end ... ≤ (μ s + ε / 2) + ε / 2 : begin refine add_le_add le_rfl _, simp only [finset.card_fin, finset.sum_const, nsmul_eq_mul, ennreal.mul_div_le], end ... = μ s + ε : by rw [add_assoc, ennreal.add_halves] } end /-! ### Consequences on differentiation of measures -/ /-- In a space with the Besicovitch covering property, the set of closed balls with positive radius forms a Vitali family. This is essentially a restatement of the measurable Besicovitch theorem. -/ protected def vitali_family (μ : measure α) [sigma_finite μ] : vitali_family μ := { sets_at := λ x, (λ (r : ℝ), closed_ball x r) '' (Ioi (0 : ℝ)), measurable_set' := begin assume x y hy, obtain ⟨r, rpos, rfl⟩ : ∃ (r : ℝ), 0 < r ∧ closed_ball x r = y, by simpa only [mem_image, mem_Ioi] using hy, exact is_closed_ball.measurable_set end, nonempty_interior := begin assume x y hy, obtain ⟨r, rpos, rfl⟩ : ∃ (r : ℝ), 0 < r ∧ closed_ball x r = y, by simpa only [mem_image, mem_Ioi] using hy, simp only [nonempty.mono ball_subset_interior_closed_ball, rpos, nonempty_ball], end, nontrivial := λ x ε εpos, ⟨closed_ball x ε, mem_image_of_mem _ εpos, subset.refl _⟩, covering := begin assume s f fsubset ffine, let g : α → set ℝ := λ x, {r | 0 < r ∧ closed_ball x r ∈ f x}, have A : ∀ x ∈ s, ∀ δ > 0, (g x ∩ Ioo 0 δ).nonempty, { assume x xs δ δpos, obtain ⟨t, tf, ht⟩ : ∃ (t : set α) (H : t ∈ f x), t ⊆ closed_ball x (δ/2) := ffine x xs (δ/2) (half_pos δpos), obtain ⟨r, rpos, rfl⟩ : ∃ (r : ℝ), 0 < r ∧ closed_ball x r = t, by simpa using fsubset x xs tf, rcases le_total r (δ/2) with H|H, { exact ⟨r, ⟨rpos, tf⟩, ⟨rpos, H.trans_lt (half_lt_self δpos)⟩⟩ }, { have : closed_ball x r = closed_ball x (δ/2) := subset.antisymm ht (closed_ball_subset_closed_ball H), rw this at tf, refine ⟨δ/2, ⟨half_pos δpos, tf⟩, ⟨half_pos δpos, half_lt_self δpos⟩⟩ } }, obtain ⟨t, r, t_count, ts, tg, μt, tdisj⟩ : ∃ (t : set α) (r : α → ℝ), countable t ∧ t ⊆ s ∧ (∀ x ∈ t, r x ∈ g x ∩ Ioo 0 1) ∧ μ (s \ (⋃ (x ∈ t), closed_ball x (r x))) = 0 ∧ t.pairwise_disjoint (λ x, closed_ball x (r x)) := exists_disjoint_closed_ball_covering_ae μ g s A (λ _, 1) (λ _ _, zero_lt_one), exact ⟨t, λ x, closed_ball x (r x), ts, tdisj, λ x xt, (tg x xt).1.2, μt⟩, end } /-- The main feature of the Besicovitch Vitali family is that its filter at a point `x` corresponds to convergence along closed balls. We record one of the two implications here, which will enable us to deduce specific statements on differentiation of measures in this context from the general versions. -/ lemma tendsto_filter_at (μ : measure α) [sigma_finite μ] (x : α) : tendsto (λ r, closed_ball x r) (𝓝[>] 0) ((besicovitch.vitali_family μ).filter_at x) := begin assume s hs, simp only [mem_map], obtain ⟨ε, εpos, hε⟩ : ∃ (ε : ℝ) (H : ε > 0), ∀ (a : set α), a ∈ (besicovitch.vitali_family μ).sets_at x → a ⊆ closed_ball x ε → a ∈ s := (vitali_family.mem_filter_at_iff _).1 hs, have : Ioc (0 : ℝ) ε ∈ 𝓝[>] (0 : ℝ) := Ioc_mem_nhds_within_Ioi ⟨le_rfl, εpos⟩, filter_upwards [this] with _ hr, apply hε, { exact mem_image_of_mem _ hr.1 }, { exact closed_ball_subset_closed_ball hr.2 } end variables [metric_space β] [measurable_space β] [borel_space β] [sigma_compact_space β] [has_besicovitch_covering β] /-- In a space with the Besicovitch covering property, the ratio of the measure of balls converges almost surely to to the Radon-Nikodym derivative. -/ lemma ae_tendsto_rn_deriv (ρ μ : measure β) [is_locally_finite_measure μ] [is_locally_finite_measure ρ] : ∀ᵐ x ∂μ, tendsto (λ r, ρ (closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 (ρ.rn_deriv μ x)) := begin haveI : second_countable_topology β := emetric.second_countable_of_sigma_compact β, filter_upwards [vitali_family.ae_tendsto_rn_deriv (besicovitch.vitali_family μ) ρ] with x hx, exact hx.comp (tendsto_filter_at μ x) end /-- Given a measurable set `s`, then `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` converges when `r` tends to `0`, for almost every `x`. The limit is `1` for `x ∈ s` and `0` for `x ∉ s`. This shows that almost every point of `s` is a Lebesgue density point for `s`. A version for non-measurable sets holds, but it only gives the first conclusion, see `ae_tendsto_measure_inter_div`. -/ lemma ae_tendsto_measure_inter_div_of_measurable_set (μ : measure β) [is_locally_finite_measure μ] {s : set β} (hs : measurable_set s) : ∀ᵐ x ∂μ, tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 (s.indicator 1 x)) := begin haveI : second_countable_topology β := emetric.second_countable_of_sigma_compact β, filter_upwards [vitali_family.ae_tendsto_measure_inter_div_of_measurable_set (besicovitch.vitali_family μ) hs], assume x hx, exact hx.comp (tendsto_filter_at μ x) end /-- Given an arbitrary set `s`, then `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` converges to `1` when `r` tends to `0`, for almost every `x` in `s`. This shows that almost every point of `s` is a Lebesgue density point for `s`. A stronger version holds for measurable sets, see `ae_tendsto_measure_inter_div_of_measurable_set`. -/ lemma ae_tendsto_measure_inter_div (μ : measure β) [is_locally_finite_measure μ] (s : set β) : ∀ᵐ x ∂(μ.restrict s), tendsto (λ r, μ (s ∩ (closed_ball x r)) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 1) := begin haveI : second_countable_topology β := emetric.second_countable_of_sigma_compact β, filter_upwards [vitali_family.ae_tendsto_measure_inter_div (besicovitch.vitali_family μ)] with x hx using hx.comp (tendsto_filter_at μ x), end end besicovitch
6b890d06ed1fa98771f5a1d7c7f91ca91ccbdfd9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/direct_sum/basic.lean
3ebb86c9613d4d637be185a0838c863af418feed
[ "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
11,046
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import data.dfinsupp.basic import group_theory.submonoid.operations /-! # Direct sum This file defines the direct sum of abelian groups, indexed by a discrete type. ## Notation `⨁ i, β i` is the n-ary direct sum `direct_sum`. This notation is in the `direct_sum` locale, accessible after `open_locale direct_sum`. ## References * https://en.wikipedia.org/wiki/Direct_sum -/ open_locale big_operators universes u v w u₁ variables (ι : Type v) [dec_ι : decidable_eq ι] (β : ι → Type w) /-- `direct_sum β` is the direct sum of a family of additive commutative monoids `β i`. Note: `open_locale direct_sum` will enable the notation `⨁ i, β i` for `direct_sum β`. -/ @[derive [add_comm_monoid, inhabited]] def direct_sum [Π i, add_comm_monoid (β i)] : Type* := Π₀ i, β i instance [Π i, add_comm_monoid (β i)] : has_coe_to_fun (direct_sum ι β) (λ _, Π i : ι, β i) := dfinsupp.has_coe_to_fun localized "notation (name := direct_sum) `⨁` binders `, ` r:(scoped f, direct_sum _ f) := r" in direct_sum namespace direct_sum variables {ι} section add_comm_group variables [Π i, add_comm_group (β i)] instance : add_comm_group (direct_sum ι β) := dfinsupp.add_comm_group variables {β} @[simp] lemma sub_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ - g₂) i = g₁ i - g₂ i := rfl end add_comm_group variables [Π i, add_comm_monoid (β i)] @[simp] lemma zero_apply (i : ι) : (0 : ⨁ i, β i) i = 0 := rfl variables {β} @[simp] lemma add_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ + g₂) i = g₁ i + g₂ i := rfl variables (β) include dec_ι /-- `mk β s x` is the element of `⨁ i, β i` that is zero outside `s` and has coefficient `x i` for `i` in `s`. -/ def mk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →+ ⨁ i, β i := { to_fun := dfinsupp.mk s, map_add' := λ _ _, dfinsupp.mk_add, map_zero' := dfinsupp.mk_zero, } /-- `of i` is the natural inclusion map from `β i` to `⨁ i, β i`. -/ def of (i : ι) : β i →+ ⨁ i, β i := dfinsupp.single_add_hom β i @[simp] lemma of_eq_same (i : ι) (x : β i) : (of _ i x) i = x := dfinsupp.single_eq_same lemma of_eq_of_ne (i j : ι) (x : β i) (h : i ≠ j) : (of _ i x) j = 0 := dfinsupp.single_eq_of_ne h @[simp] lemma support_zero [Π (i : ι) (x : β i), decidable (x ≠ 0)] : (0 : ⨁ i, β i).support = ∅ := dfinsupp.support_zero @[simp] lemma support_of [Π (i : ι) (x : β i), decidable (x ≠ 0)] (i : ι) (x : β i) (h : x ≠ 0) : (of _ i x).support = {i} := dfinsupp.support_single_ne_zero h lemma support_of_subset [Π (i : ι) (x : β i), decidable (x ≠ 0)] {i : ι} {b : β i} : (of _ i b).support ⊆ {i} := dfinsupp.support_single_subset lemma sum_support_of [Π (i : ι) (x : β i), decidable (x ≠ 0)] (x : ⨁ i, β i) : ∑ i in x.support, of β i (x i) = x := dfinsupp.sum_single variables {β} theorem mk_injective (s : finset ι) : function.injective (mk β s) := dfinsupp.mk_injective s theorem of_injective (i : ι) : function.injective (of β i) := dfinsupp.single_injective @[elab_as_eliminator] protected theorem induction_on {C : (⨁ i, β i) → Prop} (x : ⨁ i, β i) (H_zero : C 0) (H_basic : ∀ (i : ι) (x : β i), C (of β i x)) (H_plus : ∀ x y, C x → C y → C (x + y)) : C x := begin apply dfinsupp.induction x H_zero, intros i b f h1 h2 ih, solve_by_elim end /-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`, then they are equal. -/ lemma add_hom_ext {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄ (H : ∀ (i : ι) (y : β i), f (of _ i y) = g (of _ i y)) : f = g := dfinsupp.add_hom_ext H /-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma add_hom_ext' {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄ (H : ∀ (i : ι), f.comp (of _ i) = g.comp (of _ i)) : f = g := add_hom_ext $ λ i, add_monoid_hom.congr_fun $ H i variables {γ : Type u₁} [add_comm_monoid γ] section to_add_monoid variables (φ : Π i, β i →+ γ) (ψ : (⨁ i, β i) →+ γ) /-- `to_add_monoid φ` is the natural homomorphism from `⨁ i, β i` to `γ` induced by a family `φ` of homomorphisms `β i → γ`. -/ def to_add_monoid : (⨁ i, β i) →+ γ := (dfinsupp.lift_add_hom φ) @[simp] lemma to_add_monoid_of (i) (x : β i) : to_add_monoid φ (of β i x) = φ i x := dfinsupp.lift_add_hom_apply_single φ i x theorem to_add_monoid.unique (f : ⨁ i, β i) : ψ f = to_add_monoid (λ i, ψ.comp (of β i)) f := by {congr, ext, simp [to_add_monoid, of]} end to_add_monoid section from_add_monoid /-- `from_add_monoid φ` is the natural homomorphism from `γ` to `⨁ i, β i` induced by a family `φ` of homomorphisms `γ → β i`. Note that this is not an isomorphism. Not every homomorphism `γ →+ ⨁ i, β i` arises in this way. -/ def from_add_monoid : (⨁ i, γ →+ β i) →+ (γ →+ ⨁ i, β i) := to_add_monoid $ λ i, add_monoid_hom.comp_hom (of β i) @[simp] lemma from_add_monoid_of (i : ι) (f : γ →+ β i) : from_add_monoid (of _ i f) = (of _ i).comp f := by { rw [from_add_monoid, to_add_monoid_of], refl } lemma from_add_monoid_of_apply (i : ι) (f : γ →+ β i) (x : γ) : from_add_monoid (of _ i f) x = of _ i (f x) := by rw [from_add_monoid_of, add_monoid_hom.coe_comp] end from_add_monoid variables (β) /-- `set_to_set β S T h` is the natural homomorphism `⨁ (i : S), β i → ⨁ (i : T), β i`, where `h : S ⊆ T`. -/ -- TODO: generalize this to remove the assumption `S ⊆ T`. def set_to_set (S T : set ι) (H : S ⊆ T) : (⨁ (i : S), β i) →+ (⨁ (i : T), β i) := to_add_monoid $ λ i, of (λ (i : subtype T), β i) ⟨↑i, H i.prop⟩ variables {β} omit dec_ι instance unique [∀ i, subsingleton (β i)] : unique (⨁ i, β i) := dfinsupp.unique /-- A direct sum over an empty type is trivial. -/ instance unique_of_is_empty [is_empty ι] : unique (⨁ i, β i) := dfinsupp.unique_of_is_empty /-- The natural equivalence between `⨁ _ : ι, M` and `M` when `unique ι`. -/ protected def id (M : Type v) (ι : Type* := punit) [add_comm_monoid M] [unique ι] : (⨁ (_ : ι), M) ≃+ M := { to_fun := direct_sum.to_add_monoid (λ _, add_monoid_hom.id M), inv_fun := of (λ _, M) default, left_inv := λ x, direct_sum.induction_on x (by rw [add_monoid_hom.map_zero, add_monoid_hom.map_zero]) (λ p x, by rw [unique.default_eq p, to_add_monoid_of]; refl) (λ x y ihx ihy, by rw [add_monoid_hom.map_add, add_monoid_hom.map_add, ihx, ihy]), right_inv := λ x, to_add_monoid_of _ _ _, ..direct_sum.to_add_monoid (λ _, add_monoid_hom.id M) } section congr_left variables {κ : Type*} /--Reindexing terms of a direct sum.-/ def equiv_congr_left (h : ι ≃ κ) : (⨁ i, β i) ≃+ ⨁ k, β (h.symm k) := { map_add' := dfinsupp.comap_domain'_add _ _, ..dfinsupp.equiv_congr_left h } @[simp] lemma equiv_congr_left_apply (h : ι ≃ κ) (f : ⨁ i, β i) (k : κ) : equiv_congr_left h f k = f (h.symm k) := dfinsupp.comap_domain'_apply _ _ _ _ end congr_left section option variables {α : option ι → Type w} [Π i, add_comm_monoid (α i)] include dec_ι /--Isomorphism obtained by separating the term of index `none` of a direct sum over `option ι`.-/ @[simps] noncomputable def add_equiv_prod_direct_sum : (⨁ i, α i) ≃+ α none × ⨁ i, α (some i) := { map_add' := dfinsupp.equiv_prod_dfinsupp_add, ..dfinsupp.equiv_prod_dfinsupp } end option section sigma variables {α : ι → Type u} {δ : Π i, α i → Type w} [Π i j, add_comm_monoid (δ i j)] /--The natural map between `⨁ (i : Σ i, α i), δ i.1 i.2` and `⨁ i (j : α i), δ i j`.-/ noncomputable def sigma_curry : (⨁ (i : Σ i, _), δ i.1 i.2) →+ ⨁ i j, δ i j := { to_fun := @dfinsupp.sigma_curry _ _ δ _, map_zero' := dfinsupp.sigma_curry_zero, map_add' := λ f g, @dfinsupp.sigma_curry_add _ _ δ _ f g } @[simp] lemma sigma_curry_apply (f : ⨁ (i : Σ i, _), δ i.1 i.2) (i : ι) (j : α i) : sigma_curry f i j = f ⟨i, j⟩ := @dfinsupp.sigma_curry_apply _ _ δ _ f i j /--The natural map between `⨁ i (j : α i), δ i j` and `Π₀ (i : Σ i, α i), δ i.1 i.2`, inverse of `curry`.-/ noncomputable def sigma_uncurry : (⨁ i j, δ i j) →+ ⨁ (i : Σ i, _), δ i.1 i.2 := { to_fun := dfinsupp.sigma_uncurry, map_zero' := dfinsupp.sigma_uncurry_zero, map_add' := dfinsupp.sigma_uncurry_add } @[simp] lemma sigma_uncurry_apply (f : ⨁ i j, δ i j) (i : ι) (j : α i) : sigma_uncurry f ⟨i, j⟩ = f i j := dfinsupp.sigma_uncurry_apply f i j /--The natural map between `⨁ (i : Σ i, α i), δ i.1 i.2` and `⨁ i (j : α i), δ i j`.-/ noncomputable def sigma_curry_equiv : (⨁ (i : Σ i, _), δ i.1 i.2) ≃+ ⨁ i j, δ i j := { ..sigma_curry, ..dfinsupp.sigma_curry_equiv } end sigma /-- The canonical embedding from `⨁ i, A i` to `M` where `A` is a collection of `add_submonoid M` indexed by `ι`. When `S = submodule _ M`, this is available as a `linear_map`, `direct_sum.coe_linear_map`. -/ protected def coe_add_monoid_hom {M S : Type*} [decidable_eq ι] [add_comm_monoid M] [set_like S M] [add_submonoid_class S M] (A : ι → S) : (⨁ i, A i) →+ M := to_add_monoid (λ i, add_submonoid_class.subtype (A i)) @[simp] lemma coe_add_monoid_hom_of {M S : Type*} [decidable_eq ι] [add_comm_monoid M] [set_like S M] [add_submonoid_class S M] (A : ι → S) (i : ι) (x : A i) : direct_sum.coe_add_monoid_hom A (of (λ i, A i) i x) = x := to_add_monoid_of _ _ _ lemma coe_of_apply {M S : Type*} [decidable_eq ι] [add_comm_monoid M] [set_like S M] [add_submonoid_class S M] {A : ι → S} (i j : ι) (x : A i) : (of _ i x j : M) = if i = j then x else 0 := begin obtain rfl | h := decidable.eq_or_ne i j, { rw [direct_sum.of_eq_same, if_pos rfl], }, { rw [direct_sum.of_eq_of_ne _ _ _ _ h, if_neg h, zero_mem_class.coe_zero], }, end /-- The `direct_sum` formed by a collection of additive submonoids (or subgroups, or submodules) of `M` is said to be internal if the canonical map `(⨁ i, A i) →+ M` is bijective. For the alternate statement in terms of independence and spanning, see `direct_sum.subgroup_is_internal_iff_independent_and_supr_eq_top` and `direct_sum.is_internal_submodule_iff_independent_and_supr_eq_top`. -/ def is_internal {M S : Type*} [decidable_eq ι] [add_comm_monoid M] [set_like S M] [add_submonoid_class S M] (A : ι → S) : Prop := function.bijective (direct_sum.coe_add_monoid_hom A) lemma is_internal.add_submonoid_supr_eq_top {M : Type*} [decidable_eq ι] [add_comm_monoid M] (A : ι → add_submonoid M) (h : is_internal A) : supr A = ⊤ := begin rw [add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom, add_monoid_hom.mrange_top_iff_surjective], exact function.bijective.surjective h, end end direct_sum
fadd3dc650efe807d302ebe8d68c76ff108e2a9c
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/compiler/implementedbyattr.lean
26749baa0ac93911fdd0461f7aee6dbe679976f8
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
1,193
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.lean.attributes namespace Lean namespace Compiler def mkImplementedByAttr : IO (ParametricAttribute Name) := registerParametricAttribute `implementedBy "name of the Lean (probably unsafe) function that implements opaque constant" $ fun env declName stx => match env.find declName with | none => Except.error "unknown declaration" | some decl => match attrParamSyntaxToIdentifier stx with | some fnName => match env.find fnName with | none => Except.error ("unknown function '" ++ toString fnName ++ "'") | some fnDecl => if decl.type == fnDecl.type then Except.ok fnName else Except.error ("invalid function '" ++ toString fnName ++ "' type mismatch") | _ => Except.error "expected identifier" @[init mkImplementedByAttr] constant implementedByAttr : ParametricAttribute Name := default _ @[export lean.get_implemented_by_core] def getImplementedBy (env : Environment) (n : Name) : Option Name := implementedByAttr.getParam env n end Compiler end Lean
55a73edccec26edb0e92c1343ad19667133d8041
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/printDecls.lean
6006ce421f608458f1d496bee6ae0fddc9de7ba1
[ "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
834
lean
import Lean open Lean open Lean.Meta -- Return true if `declName` should be ignored def shouldIgnore (declName : Name) : Bool := declName.isInternal || match declName with | .str _ s => "match_".isPrefixOf s || "proof_".isPrefixOf s || "eq_".isPrefixOf s | _ => true -- Print declarations that have the given prefix. def printDecls (pre : Name) : MetaM Unit := do let cs := (← getEnv).constants cs.forM fun declName info => do if pre.isPrefixOf declName && !shouldIgnore declName then if let some docString ← findDocString? (← getEnv) declName then IO.println s!"/-- {docString} -/\n{declName} : {← ppExpr info.type}" else IO.println s!"{declName} : {← ppExpr info.type}" #eval printDecls `Array #eval printDecls `List #eval printDecls `Bool #eval printDecls `Lean.Elab
0453eb6e34a455f7412f2c6ffc39a767b387ba8c
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Std/Data/HashSet.lean
5710ff4e9389475ddc775bb8553dfab8d560140b
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
6,118
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 -/ namespace Std universe u v w def HashSetBucket (α : Type u) := { b : Array (List α) // b.size > 0 } def HashSetBucket.update {α : Type u} (data : HashSetBucket α) (i : USize) (d : List α) (h : i.toNat < data.val.size) : HashSetBucket α := ⟨ data.val.uset i d h, by erw [Array.size_set]; exact data.property ⟩ structure HashSetImp (α : Type u) where size : Nat buckets : HashSetBucket α def mkHashSetImp {α : Type u} (nbuckets := 8) : HashSetImp α := let n := if nbuckets = 0 then 8 else nbuckets { size := 0, buckets := ⟨ mkArray n [], by rw [Array.size_mkArray]; cases nbuckets; decide; apply Nat.zeroLtSucc ⟩ } namespace HashSetImp variable {α : Type u} def mkIdx {n : Nat} (h : n > 0) (u : USize) : { u : USize // u.toNat < n } := ⟨u % n, USize.modn_lt _ h⟩ @[inline] def reinsertAux (hashFn : α → UInt64) (data : HashSetBucket α) (a : α) : HashSetBucket α := let ⟨i, h⟩ := mkIdx data.property (hashFn a |>.toUSize) data.update i (a :: data.val.uget i h) h @[inline] def foldBucketsM {δ : Type w} {m : Type w → Type w} [Monad m] (data : HashSetBucket α) (d : δ) (f : δ → α → m δ) : m δ := data.val.foldlM (init := d) fun d as => as.foldlM f d @[inline] def foldBuckets {δ : Type w} (data : HashSetBucket α) (d : δ) (f : δ → α → δ) : δ := Id.run $ foldBucketsM data d f @[inline] def foldM {δ : Type w} {m : Type w → Type w} [Monad m] (f : δ → α → m δ) (d : δ) (h : HashSetImp α) : m δ := foldBucketsM h.buckets d f @[inline] def fold {δ : Type w} (f : δ → α → δ) (d : δ) (m : HashSetImp α) : δ := foldBuckets m.buckets d f def find? [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : Option α := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) (buckets.val.uget i h).find? (fun a' => a == a') def contains [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : Bool := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) (buckets.val.uget i h).contains a -- TODO: remove `partial` by using well-founded recursion partial def moveEntries [Hashable α] (i : Nat) (source : Array (List α)) (target : HashSetBucket α) : HashSetBucket α := if h : i < source.size then let idx : Fin source.size := ⟨i, h⟩ let es : List α := source.get idx -- We remove `es` from `source` to make sure we can reuse its memory cells when performing es.foldl let source := source.set idx [] let target := es.foldl (reinsertAux hash) target moveEntries (i+1) source target else target def expand [Hashable α] (size : Nat) (buckets : HashSetBucket α) : HashSetImp α := let nbuckets := buckets.val.size * 2 have : nbuckets > 0 := Nat.mulPos buckets.property (by decide) let new_buckets : HashSetBucket α := ⟨mkArray nbuckets [], by rw [Array.size_mkArray]; assumption⟩ { size := size, buckets := moveEntries 0 buckets.val new_buckets } def insert [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : HashSetImp α := match m with | ⟨size, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) let bkt := buckets.val.uget i h if bkt.contains a then ⟨size, buckets.update i (bkt.replace a a) h⟩ else let size' := size + 1 let buckets' := buckets.update i (a :: bkt) h if size' ≤ buckets.val.size then { size := size', buckets := buckets' } else expand size' buckets' def erase [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : HashSetImp α := match m with | ⟨ size, buckets ⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize) let bkt := buckets.val.uget i h if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩ else m inductive WellFormed [BEq α] [Hashable α] : HashSetImp α → Prop where | mkWff : ∀ n, WellFormed (mkHashSetImp n) | insertWff : ∀ m a, WellFormed m → WellFormed (insert m a) | eraseWff : ∀ m a, WellFormed m → WellFormed (erase m a) end HashSetImp def HashSet (α : Type u) [BEq α] [Hashable α] := { m : HashSetImp α // m.WellFormed } open HashSetImp def mkHashSet {α : Type u} [BEq α] [Hashable α] (nbuckets := 8) : HashSet α := ⟨ mkHashSetImp nbuckets, WellFormed.mkWff nbuckets ⟩ namespace HashSet variable {α : Type u} [BEq α] [Hashable α] instance : Inhabited (HashSet α) where default := mkHashSet instance : EmptyCollection (HashSet α) := ⟨mkHashSet⟩ @[inline] def insert (m : HashSet α) (a : α) : HashSet α := match m with | ⟨ m, hw ⟩ => ⟨ m.insert a, WellFormed.insertWff m a hw ⟩ @[inline] def erase (m : HashSet α) (a : α) : HashSet α := match m with | ⟨ m, hw ⟩ => ⟨ m.erase a, WellFormed.eraseWff m a hw ⟩ @[inline] def find? (m : HashSet α) (a : α) : Option α := match m with | ⟨ m, _ ⟩ => m.find? a @[inline] def contains (m : HashSet α) (a : α) : Bool := match m with | ⟨ m, _ ⟩ => m.contains a @[inline] def foldM {δ : Type w} {m : Type w → Type w} [Monad m] (f : δ → α → m δ) (init : δ) (h : HashSet α) : m δ := match h with | ⟨ h, _ ⟩ => h.foldM f init @[inline] def fold {δ : Type w} (f : δ → α → δ) (init : δ) (m : HashSet α) : δ := match m with | ⟨ m, _ ⟩ => m.fold f init @[inline] def size (m : HashSet α) : Nat := match m with | ⟨ {size := sz, ..}, _ ⟩ => sz @[inline] def isEmpty (m : HashSet α) : Bool := m.size = 0 @[inline] def empty : HashSet α := mkHashSet def toList (m : HashSet α) : List α := m.fold (init := []) fun r a => a::r def toArray (m : HashSet α) : Array α := m.fold (init := #[]) fun r a => r.push a def numBuckets (m : HashSet α) : Nat := m.val.buckets.val.size end HashSet end Std
be1b93d1b7ed5d3a1ce59048d9387dd00b0831ea
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/data/polynomial/degree/definitions.lean
954421b208ed34f164569465823ae8418dbe7654
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,381
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.coeff import data.nat.with_bot /-! # Theory of univariate polynomials The definitions include `degree`, `monic`, `leading_coeff` Results include - `degree_mul` : The degree of the product is the sum of degrees - `leading_coeff_add_of_degree_eq` and `leading_coeff_add_of_degree_lt` : The leading_coefficient of a sum is determined by the leading coefficients and degrees -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open finsupp finset open_locale big_operators namespace polynomial universes u v variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ} section semiring variables [semiring R] {p q r : polynomial R} /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : polynomial R) : with_bot ℕ := p.support.sup some lemma degree_lt_wf : well_founded (λp q : polynomial R, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial R) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to ℕ, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial R) : ℕ := (degree p).get_or_else 0 /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial R) : R := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial R) := leading_coeff p = (1 : R) @[nontriviality] lemma monic_of_subsingleton [subsingleton R] (p : polynomial R) : monic p := subsingleton.elim _ _ lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq R] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial R} (hp : p.monic) : leading_coeff p = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial R) = ⊥ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial R) = 0 := rfl @[simp] lemma coeff_nat_degree : coeff p (nat_degree p) = leading_coeff p := rfl lemma degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), λ h, h.symm ▸ rfl⟩ lemma degree_eq_nat_degree (hp : p ≠ 0) : degree p = (nat_degree p : with_bot ℕ) := let ⟨n, hn⟩ := not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial R} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial R} {n : ℕ} (hn : 0 < n) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial R} {n : ℕ} (h : degree p = n) : nat_degree p = n := have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot ℕ) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≤ nat_degree p := with_bot.gi_get_or_else_bot.gc.le_u_l _ lemma nat_degree_eq_of_degree_eq [semiring S] {q : polynomial S} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : with_bot ℕ) ≤ degree p := show @has_le.le (with_bot ℕ) _ (some n : with_bot ℕ) (p.support.sup some : with_bot ℕ), from finset.le_sup (finsupp.mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n ≠ 0) : n ≤ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma le_nat_degree_of_mem_supp (a : ℕ) : a ∈ p.support → a ≤ nat_degree p:= le_nat_degree_of_ne_zero ∘ mem_support_iff_coeff_ne_zero.mp lemma supp_subset_range (h : nat_degree p < m) : p.support ⊆ finset.range m := λ n hn, mem_range.2 $ (le_nat_degree_of_mem_supp _ hn).trans_lt h lemma supp_subset_range_nat_degree_succ : p.support ⊆ finset.range (nat_degree p + 1) := supp_subset_range (nat.lt_succ_self _) lemma degree_le_degree (h : coeff q (nat_degree p) ≠ 0) : degree p ≤ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end lemma degree_ne_of_nat_degree_ne {n : ℕ} : p.nat_degree ≠ n → degree p ≠ n := mt $ λ h, by rw [nat_degree, h, option.get_or_else_coe] theorem nat_degree_le_iff_degree_le {n : ℕ} : nat_degree p ≤ n ↔ degree p ≤ n := with_bot.get_or_else_bot_le_iff alias polynomial.nat_degree_le_iff_degree_le ↔ . . lemma nat_degree_le_nat_degree (hpq : p.degree ≤ q.degree) : p.nat_degree ≤ q.nat_degree := with_bot.gi_get_or_else_bot.gc.monotone_l hpq @[simp] lemma degree_C (ha : a ≠ 0) : degree (C a) = (0 : with_bot ℕ) := show sup (ite (a = 0) ∅ {0}) some = 0, by rw if_neg ha; refl lemma degree_C_le : degree (C a) ≤ (0 : with_bot ℕ) := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial R) ≤ (0 : with_bot ℕ) := by rw [← C_1]; exact degree_C_le @[simp] lemma nat_degree_C (a : R) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial R) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : ℕ) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_nat_cast, nat_degree_C] @[simp] lemma degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (monomial n a) = n := by rw [degree, support_monomial _ _ ha]; refl @[simp] lemma degree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [← single_eq_C_mul_X, degree_monomial n ha] lemma degree_monomial_le (n : ℕ) (a : R) : degree (monomial n a) ≤ n := if h : a = 0 then by rw [h, (monomial n).map_zero]; exact bot_le else le_of_eq (degree_monomial n h) lemma degree_C_mul_X_pow_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n := by { rw C_mul_X_pow_eq_monomial, apply degree_monomial_le } @[simp] lemma nat_degree_C_mul_X_pow (n : ℕ) (a : R) (ha : a ≠ 0) : nat_degree (C a * X ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha) @[simp] lemma nat_degree_monomial (i : ℕ) (r : R) (hr : r ≠ 0) : nat_degree (monomial i r) = i := by rw [← C_mul_X_pow_eq_monomial, nat_degree_C_mul_X_pow i r hr] lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial R} {n : ℕ} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end @[simp] lemma coeff_nat_degree_succ_eq_zero {p : polynomial R} : p.coeff (p.nat_degree + 1) = 0 := coeff_eq_zero_of_nat_degree_lt (lt_add_one _) -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial R) (n : ℕ) (I : decidable (n < 1 + nat_degree p)) : @ite (n < 1 + nat_degree p) I _ (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (λ w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum_support (p : polynomial R) : p = ∑ i in p.support, monomial i (p.coeff i) := p.sum_single.symm lemma as_sum_support_C_mul_X_pow (p : polynomial R) : p = ∑ i in p.support, C (p.coeff i) * X^i := trans p.as_sum_support $ by simp only [C_mul_X_pow_eq_monomial] /-- We can reexpress a sum over `p.support` as a sum over `range n`, for any `n` satisfying `p.nat_degree < n`. -/ lemma sum_over_range' [add_comm_monoid S] (p : polynomial R) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) (n : ℕ) (w : p.nat_degree < n) : p.sum f = ∑ (a : ℕ) in range n, f a (coeff p a) := finsupp.sum_of_support_subset _ (supp_subset_range w) _ $ λ n hn, h n /-- We can reexpress a sum over `p.support` as a sum over `range (p.nat_degree + 1)`. -/ lemma sum_over_range [add_comm_monoid S] (p : polynomial R) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) : p.sum f = ∑ (a : ℕ) in range (p.nat_degree + 1), f a (coeff p a) := sum_over_range' p h (p.nat_degree + 1) (lt_add_one _) lemma as_sum_range' (p : polynomial R) (n : ℕ) (w : p.nat_degree < n) : p = ∑ i in range n, monomial i (coeff p i) := p.sum_single.symm.trans $ p.sum_over_range' (λ n, single_zero) _ w lemma as_sum_range (p : polynomial R) : p = ∑ i in range (p.nat_degree + 1), monomial i (coeff p i) := p.sum_single.symm.trans $ p.sum_over_range $ λ n, single_zero lemma as_sum_range_C_mul_X_pow (p : polynomial R) : p = ∑ i in range (p.nat_degree + 1), C (coeff p i) * X ^ i := p.as_sum_range.trans $ by simp only [C_mul_X_pow_eq_monomial] lemma coeff_ne_zero_of_eq_degree (hn : degree p = n) : coeff p n ≠ 0 := λ h, mem_support_iff.mp (mem_of_max hn) h lemma eq_X_add_C_of_degree_le_one (h : degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (λ n, nat.cases_on n (by simp) (λ n, nat.cases_on n (by simp [coeff_C]) (λ m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm ℕ 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≤ 1, from h ▸ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) lemma eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := eq_X_add_C_of_degree_le_one $ degree_le_of_nat_degree_le h lemma exists_eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≤ 1) : ∃ a b, p = C a * X + C b := ⟨p.coeff 1, p.coeff 0, eq_X_add_C_of_nat_degree_le_one h⟩ theorem degree_X_pow_le (n : ℕ) : degree (X^n : polynomial R) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1:R) theorem degree_X_le : degree (X : polynomial R) ≤ 1 := degree_monomial_le _ _ lemma nat_degree_X_le : (X : polynomial R).nat_degree ≤ 1 := nat_degree_le_of_degree_le degree_X_le lemma support_C_mul_X_pow (c : R) (n : ℕ) : (C c * X ^ n).support ⊆ singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_single_subset end lemma mem_support_C_mul_X_pow {n a : ℕ} {c : R} (h : a ∈ (C c * X ^ n).support) : a = n := mem_singleton.1 $ support_C_mul_X_pow _ _ h lemma card_support_C_mul_X_pow_le_one {c : R} {n : ℕ} : (C c * X ^ n).support.card ≤ 1 := begin rw ← card_singleton n, apply card_le_of_subset (support_C_mul_X_pow c n), end lemma card_supp_le_succ_nat_degree (p : polynomial R) : p.support.card ≤ p.nat_degree + 1 := begin rw ← finset.card_range (p.nat_degree + 1), exact finset.card_le_of_subset supp_subset_range_nat_degree_succ, end lemma le_degree_of_mem_supp (a : ℕ) : a ∈ p.support → ↑a ≤ degree p := le_degree_of_ne_zero ∘ mem_support_iff_coeff_ne_zero.mp lemma nonempty_support_iff : p.support.nonempty ↔ p ≠ 0 := by rw [ne.def, nonempty_iff_ne_empty, ne.def, ← support_eq_empty] lemma support_C_mul_X_pow_nonzero {c : R} {n : ℕ} (h : c ≠ 0) : (C c * X ^ n).support = singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_single_ne_zero h end end semiring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_one : degree (1 : polynomial R) = (0 : with_bot ℕ) := degree_C (show (1 : R) ≠ 0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial R) = 1 := degree_monomial _ one_ne_zero @[simp] lemma nat_degree_X : (X : polynomial R).nat_degree = 1 := nat_degree_eq_of_degree_eq_some degree_X end nonzero_semiring section ring variables [ring R] lemma coeff_mul_X_sub_C {p : polynomial R} {r : R} {a : ℕ} : coeff (p * (X - C r)) (a + 1) = coeff p a - coeff p (a + 1) * r := by simp [mul_sub] lemma C_eq_int_cast (n : ℤ) : C (n : R) = n := (C : R →+* _).map_int_cast n @[simp] lemma degree_neg (p : polynomial R) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial R) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : ℤ) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_int_cast, nat_degree_C] end ring section semiring variables [semiring R] /-- The second-highest coefficient, or 0 for constants -/ def next_coeff (p : polynomial R) : R := if p.nat_degree = 0 then 0 else p.coeff (p.nat_degree - 1) @[simp] lemma next_coeff_C_eq_zero (c : R) : next_coeff (C c) = 0 := by { rw next_coeff, simp } lemma next_coeff_of_pos_nat_degree (p : polynomial R) (hp : 0 < p.nat_degree) : next_coeff p = p.coeff (p.nat_degree - 1) := by { rw [next_coeff, if_neg], contrapose! hp, simpa } end semiring section semiring variables [semiring R] {p q : polynomial R} {ι : Type*} lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot ℕ} (h : n < degree p) : p ≠ 0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma eq_C_of_degree_le_zero (h : degree p ≤ 0) : p = C (coeff p 0) := begin ext (_|n), { simp }, rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt], exact h.trans_lt (with_bot.some_lt_some.2 n.succ_pos), end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h ▸ le_refl _) lemma degree_le_zero_iff : degree p ≤ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, λ h, h.symm ▸ degree_C_le⟩ lemma degree_add_le (p q : polynomial R) : degree (p + q) ≤ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≤ (p.support ∪ q.support).sup some : by convert sup_mono support_add ... = p.support.sup some ⊔ q.support.sup some : by convert sup_union ... = _ : with_bot.sup_eq_max _ _ @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial R) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), λ h, h.symm ▸ leading_coeff_zero⟩ lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = ⊥ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma nat_degree_mem_support_of_nonzero (H : p ≠ 0) : p.nat_degree ∈ p.support := (p.mem_support_to_fun p.nat_degree).mpr ((not_congr leading_coeff_eq_zero).mpr H) lemma nat_degree_eq_support_max' (h : p ≠ 0) : p.nat_degree = p.support.max' (nonempty_support_iff.mpr h) := begin apply le_antisymm, { apply finset.le_max', rw mem_support_iff_coeff_ne_zero, exact (not_congr leading_coeff_eq_zero).mpr h, }, { apply max'_le, refine le_nat_degree_of_mem_supp, }, end lemma nat_degree_C_mul_X_pow_le (a : R) (n : ℕ) : nat_degree (C a * X ^ n) ≤ n := begin by_cases a0 : a = 0, { rw [a0, C_0, zero_mul, nat_degree_zero], exact nat.zero_le n, }, { rw nat_degree_eq_support_max', { simp_rw [support_C_mul_X_pow_nonzero a0, max'_singleton n], }, { intro, apply a0, rw [← C_inj, C_0], apply mul_X_pow_eq_zero ‹_›, }, }, end lemma nat_degree_C_mul_X_pow_of_nonzero {a : R} (n : ℕ) (ha : a ≠ 0) : nat_degree (C a * X ^ n) = n := begin rw nat_degree_eq_support_max', { simp_rw [support_C_mul_X_pow_nonzero ha, max'_singleton n], }, { intro, apply ha, rw [← C_inj, C_0], exact mul_X_pow_eq_zero ‹_›, }, end lemma degree_add_eq_left_of_degree_lt (h : degree q < degree p) : degree (p + q) = degree p := le_antisymm (max_eq_left_of_lt h ▸ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, add_zero], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_eq_right_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := by rw [add_comm, degree_add_eq_left_of_degree_lt h] lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p ▸ degree_add_eq_right_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q ≠ 0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_right_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [degree_add_eq_left_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial R) (n : ℕ) : degree (p.erase n) ≤ degree p := by convert sup_mono (erase_subset _ _) lemma degree_erase_lt (hp : p ≠ 0) : degree (p.erase (nat_degree p)) < degree p := lt_of_le_of_ne (degree_erase_le _ _) $ (degree_eq_nat_degree hp).symm ▸ (by convert λ h, not_mem_erase _ _ (mem_of_max h)) lemma degree_sum_le (s : finset ι) (f : ι → polynomial R) : degree (∑ i in s, f i) ≤ s.sup (λ b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree (∑ i in insert a s, f i) ≤ max (degree (f a)) (degree (∑ i in s, f i)) : by rw sum_insert has; exact degree_add_le _ _ ... ≤ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih lemma degree_mul_le (p q : polynomial R) : degree (p * q) ≤ degree p + degree q := calc degree (p * q) ≤ (p.support).sup (λi, degree (sum q (λj a, C (coeff p i * a) * X ^ (i + j)))) : by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _ ... ≤ p.support.sup (λi, q.support.sup (λj, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≤ degree p + degree q : begin refine finset.sup_le (λ a ha, finset.sup_le (λ b hb, le_trans (degree_C_mul_X_pow_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial R) : ∀ n, degree (p ^ n) ≤ n •ℕ (degree p) | 0 := by rw [pow_zero, zero_nsmul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≤ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≤ _ : by rw succ_nsmul; exact add_le_add (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : R) (n : ℕ) : leading_coeff (C a * X ^ n) = a := begin by_cases ha : a = 0, { simp only [ha, C_0, zero_mul, leading_coeff_zero] }, { rw [leading_coeff, nat_degree_C_mul_X_pow _ _ ha, C_mul_X_pow_eq_monomial], exact @finsupp.single_eq_same _ _ _ n a } end @[simp] lemma leading_coeff_monomial' (a : R) (n : ℕ) : leading_coeff (monomial n a) = a := by rw [← C_mul_X_pow_eq_monomial, leading_coeff_monomial] @[simp] lemma leading_coeff_C (a : R) : leading_coeff (C a) = a := suffices leading_coeff (C a * X^0) = a, by rwa [pow_zero, mul_one] at this, leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial R) = 1 := suffices leading_coeff (C (1:R) * X^1) = 1, by rwa [C_1, pow_one, one_mul] at this, leading_coeff_monomial 1 1 @[simp] lemma monic_X : monic (X : polynomial R) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial R) = 1 := suffices leading_coeff (C (1:R) * X^0) = 1, by rwa [C_1, pow_zero, mul_one] at this, leading_coeff_monomial 1 0 @[simp] lemma monic_one : monic (1 : polynomial R) := leading_coeff_C _ lemma monic.ne_zero {R : Type*} [semiring R] [nontrivial R] {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { rintro rfl, simpa [monic] using hp } lemma monic.ne_zero_of_ne (h : (0:R) ≠ 1) {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { nontriviality R, exact hp.ne_zero } lemma monic.ne_zero_of_polynomial_ne {r} (hp : monic p) (hne : q ≠ r) : p ≠ 0 := by { haveI := nontrivial.of_polynomial_ne hne, exact hp.ne_zero } lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q ≠ 0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial R) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = ∑ x in nat.antidiagonal (nat_degree p + nat_degree q), coeff p x.1 * coeff q x.2 : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ h₂, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact h₂ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : degree (p * q) = degree p + degree q := have hp : p ≠ 0 := by refine mt _ h; exact λ hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q ≠ 0 := by refine mt _ h; exact λ hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma nat_degree_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, zero_mul]), have hq : q ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, mul_zero]), have hpq : p * q ≠ 0 := λ hpq, by rw [← coeff_mul_degree_add_degree, hpq, coeff_zero] at h; exact h rfl, option.some_inj.1 (show (nat_degree (p * q) : with_bot ℕ) = nat_degree p + nat_degree q, by rw [← degree_eq_nat_degree hpq, degree_mul' h, degree_eq_nat_degree hp, degree_eq_nat_degree hq]) lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul' h, coeff_mul_degree_add_degree], refl end lemma leading_coeff_pow' : leading_coeff p ^ n ≠ 0 → leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ λ n ih h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' h₂, ih h₁] lemma degree_pow' : ∀ {n}, leading_coeff p ^ n ≠ 0 → degree (p ^ n) = n •ℕ (degree p) | 0 := λ h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, zero_nsmul] | (n+1) := λ h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul' h₂, succ_nsmul, degree_pow' h₁] lemma nat_degree_pow' {n : ℕ} (h : leading_coeff p ^ n ≠ 0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n ≠ 0, from λ hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot ℕ) = (n * nat_degree p : ℕ), by rw [← degree_eq_nat_degree hpn, degree_pow' h, degree_eq_nat_degree hp0, ← with_bot.coe_nsmul]; simp @[simp] lemma leading_coeff_X_pow : ∀ n : ℕ, leading_coeff ((X : polynomial R) ^ n) = 1 | 0 := by simp | (n+1) := if h10 : (1 : R) = 0 then by rw [pow_succ, ← one_mul X, ← C_1, h10]; simp else have h : leading_coeff (X : polynomial R) * leading_coeff (X ^ n) ≠ 0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact h10, by rw [pow_succ, leading_coeff_mul' h, leading_coeff_X, leading_coeff_X_pow, one_mul] theorem leading_coeff_mul_X_pow {p : polynomial R} {n : ℕ} : leading_coeff (p * X ^ n) = leading_coeff p := decidable.by_cases (λ H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (λ H : leading_coeff p ≠ 0, by rw [leading_coeff_mul', leading_coeff_X_pow, mul_one]; rwa [leading_coeff_X_pow, mul_one]) lemma nat_degree_mul_le {p q : polynomial R} : nat_degree (p * q) ≤ nat_degree p + nat_degree q := begin apply nat_degree_le_of_degree_le, apply le_trans (degree_mul_le p q), rw with_bot.coe_add, refine add_le_add _ _; apply degree_le_nat_degree, end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial R)) : (∀ p q : polynomial R, p = q) ∧ (∀ a b : R, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], λ a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma zero_le_degree_iff {p : polynomial R} : 0 ≤ degree p ↔ p ≠ 0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial lemma degree_nonneg_iff_ne_zero : 0 ≤ degree p ↔ p ≠ 0 := ⟨λ h0p hp0, absurd h0p (by rw [hp0, degree_zero]; exact dec_trivial), λ hp0, le_of_not_gt (λ h, by simp [gt, degree_eq_bot, *] at *)⟩ lemma nat_degree_eq_zero_iff_degree_le_zero : p.nat_degree = 0 ↔ p.degree ≤ 0 := by rw [← le_zero_iff_eq, nat_degree_le_iff_degree_le, with_bot.coe_zero] theorem degree_le_iff_coeff_zero (f : polynomial R) (n : with_bot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≤ n) m (Hm : n < (m : with_bot ℕ)), decidable.of_not_not $ λ H4, have H1 : m ∉ f.support, from λ H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot ℕ) ≤ n)) Hm, H1 $ (finsupp.mem_support_to_fun f m).2 H4, λ H, finset.sup_le $ λ b Hb, decidable.of_not_not $ λ Hn, (finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem degree_lt_iff_coeff_zero (f : polynomial R) (n : ℕ) : degree f < n ↔ ∀ m : ℕ, n ≤ m → coeff f m = 0 := begin refine ⟨λ hf m hm, coeff_eq_zero_of_degree_lt (lt_of_lt_of_le hf (with_bot.coe_le_coe.2 hm)), _⟩, simp only [degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.some_eq_coe, with_bot.coe_lt_coe, ← @not_le ℕ], exact λ h m, mt (h m), end lemma degree_lt_degree_mul_X (hp : p ≠ 0) : p.degree < (p * X).degree := by haveI := nontrivial.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X ≠ 0, by simpa, by erw [degree_mul' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma nat_degree_pos_iff_degree_pos {p : polynomial R} : 0 < nat_degree p ↔ 0 < degree p := lt_iff_lt_of_le_iff_le nat_degree_le_iff_degree_le lemma eq_C_of_nat_degree_le_zero {p : polynomial R} (h : nat_degree p ≤ 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero $ degree_le_of_nat_degree_le h lemma eq_C_of_nat_degree_eq_zero {p : polynomial R} (h : nat_degree p = 0) : p = C (coeff p 0) := eq_C_of_nat_degree_le_zero h.le end semiring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_X_pow (n : ℕ) : degree ((X : polynomial R) ^ n) = n := by rw [X_pow_eq_monomial, degree_monomial _ (@one_ne_zero R _ _)] theorem not_is_unit_X : ¬ is_unit (X : polynomial R) := λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by { rw [← coeff_one_zero, ← hgf], simp } end nonzero_semiring section ring variables [ring R] {p q : polynomial R} lemma degree_sub_le (p q : polynomial R) : degree (p - q) ≤ max (degree p) (degree q) := degree_neg q ▸ degree_add_le p (-q) lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := finsupp.single_add_erase _ _, have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := finsupp.single_add_erase _ _, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv {to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]} ... ≤ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) ▸ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ lemma nat_degree_X_sub_C_le {r : R} : (X - C r).nat_degree ≤ 1 := nat_degree_le_iff_degree_le.2 $ le_trans (degree_sub_le _ _) $ max_le degree_X_le $ le_trans degree_C_le $ with_bot.coe_le_coe.2 zero_le_one lemma degree_sum_fin_lt {n : ℕ} (f : fin n → R) : degree (∑ i : fin n, C (f i) * X ^ (i : ℕ)) < n := begin haveI : is_commutative (with_bot ℕ) max := ⟨max_comm⟩, haveI : is_associative (with_bot ℕ) max := ⟨max_assoc⟩, calc (∑ i, C (f i) * X ^ (i : ℕ)).degree ≤ finset.univ.fold (⊔) ⊥ (λ i, (C (f i) * X ^ (i : ℕ)).degree) : degree_sum_le _ _ ... = finset.univ.fold max ⊥ (λ i, (C (f i) * X ^ (i : ℕ)).degree) : (@finset.fold_hom _ _ _ (⊔) _ _ _ ⊥ finset.univ _ _ _ id (with_bot.sup_eq_max)).symm ... < n : (finset.fold_max_lt (n : with_bot ℕ)).mpr ⟨with_bot.bot_lt_some _, _⟩, rintros ⟨i, hi⟩ -, calc (C (f ⟨i, hi⟩) * X ^ i).degree ≤ (C _).degree + (X ^ i).degree : degree_mul_le _ _ ... ≤ 0 + i : add_le_add degree_C_le (degree_X_pow_le i) ... = i : zero_add _ ... < n : with_bot.some_lt_some.mpr hi, end lemma degree_sub_eq_left_of_degree_lt (h : degree q < degree p) : degree (p - q) = degree p := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_left_of_degree_lt h] } lemma degree_sub_eq_right_of_degree_lt (h : degree p < degree q) : degree (p - q) = degree q := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_right_of_degree_lt h, degree_neg] } end ring section nonzero_ring variables [nontrivial R] [ring R] @[simp] lemma degree_X_sub_C (a : R) : degree (X - C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≤ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_sub_eq_left_of_degree_lt this, degree_X] @[simp] lemma nat_degree_X_sub_C (x : R) : (X - C x).nat_degree = 1 := nat_degree_eq_of_degree_eq_some $ degree_X_sub_C x @[simp] lemma next_coeff_X_sub_C (c : R) : next_coeff (X - C c) = - c := by simp [next_coeff_of_pos_nat_degree] lemma degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : degree ((X : polynomial R) ^ n - C a) = n := have degree (C a) < degree ((X : polynomial R) ^ n), from calc degree (C a) ≤ 0 : degree_C_le ... < degree ((X : polynomial R) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [degree_sub_eq_left_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : ℕ} (hn : 0 < n) (a : R) : (X : polynomial R) ^ n - C a ≠ 0 := mt degree_eq_bot.2 (show degree ((X : polynomial R) ^ n - C a) ≠ ⊥, by rw degree_X_pow_sub_C hn a; exact dec_trivial) theorem X_sub_C_ne_zero (r : R) : X - C r ≠ 0 := pow_one (X : polynomial R) ▸ X_pow_sub_C_ne_zero zero_lt_one r lemma nat_degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) {r : R} : (X ^ n - C r).nat_degree = n := by { apply nat_degree_eq_of_degree_eq_some, simp [degree_X_pow_sub_C hn], } end nonzero_ring section integral_domain variables [integral_domain R] {p q : polynomial R} @[simp] lemma degree_mul : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow (p : polynomial R) (n : ℕ) : degree (p ^ n) = n •ℕ (degree p) := by induction n; [simp only [pow_zero, degree_one, zero_nsmul], simp only [*, pow_succ, succ_nsmul, degree_mul]] @[simp] lemma leading_coeff_mul (p q : polynomial R) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_X_add_C (a b : R) (ha : a ≠ 0): leading_coeff (C a * X + C b) = a := begin rw [add_comm, leading_coeff_add_of_degree_lt], { simp }, { simpa [degree_C ha] using lt_of_le_of_lt degree_C_le (with_bot.coe_lt_coe.2 zero_lt_one)} end /-- `polynomial.leading_coeff` bundled as a `monoid_hom` when `R` is an `integral_domain`, and thus `leading_coeff` is multiplicative -/ def leading_coeff_hom : polynomial R →* R := { to_fun := leading_coeff, map_one' := by simp, map_mul' := leading_coeff_mul } @[simp] lemma leading_coeff_hom_apply (p : polynomial R) : leading_coeff_hom p = leading_coeff p := rfl @[simp] lemma leading_coeff_pow (p : polynomial R) (n : ℕ) : leading_coeff (p ^ n) = leading_coeff p ^ n := leading_coeff_hom.map_pow p n end integral_domain end polynomial
bd3718bdf79b2d29cc3235da79731073c3d315f0
4727251e0cd73359b15b664c3170e5d754078599
/src/field_theory/laurent.lean
ac3f52e1a6b894cb4d11a49ab3c3bdf69c668cd9
[ "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,699
lean
/- Copyright (c) 2022 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.polynomial.taylor import field_theory.ratfunc import ring_theory.laurent_series /-! # Laurent expansions of rational functions ## Main declarations * `ratfunc.laurent`: the Laurent expansion of the rational function `f` at `r`, as an `alg_hom`. * `ratfunc.laurent_injective`: the Laurent expansion at `r` is unique ## Implementation details Implemented as the quotient of two Taylor expansions, over domains. An auxiliary definition is provided first to make the construction of the `alg_hom` easier, which works on `comm_ring` which are not necessarily domains. -/ universe u namespace ratfunc noncomputable theory open polynomial open_locale classical non_zero_divisors polynomial variables {R : Type u} [comm_ring R] [hdomain : is_domain R] (r s : R) (p q : R[X]) (f : ratfunc R) lemma taylor_mem_non_zero_divisors (hp : p ∈ R[X]⁰) : taylor r p ∈ R[X]⁰ := begin rw mem_non_zero_divisors_iff, intros x hx, have : x = taylor (r - r) x, { simp }, rwa [this, sub_eq_add_neg, ←taylor_taylor, ←taylor_mul, linear_map.map_eq_zero_iff _ (taylor_injective _), mul_right_mem_non_zero_divisors_eq_zero_iff hp, linear_map.map_eq_zero_iff _ (taylor_injective _)] at hx, end /-- The Laurent expansion of rational functions about a value. Auxiliary definition, usage when over integral domains should prefer `ratfunc.laurent`. -/ def laurent_aux : ratfunc R →+* ratfunc R := ratfunc.map_ring_hom (ring_hom.mk (taylor r) (taylor_one _) (taylor_mul _) (linear_map.map_zero _) (linear_map.map_add _)) (taylor_mem_non_zero_divisors _) lemma laurent_aux_of_fraction_ring_mk (q : R[X]⁰) : laurent_aux r (of_fraction_ring (localization.mk p q)) = of_fraction_ring (localization.mk (taylor r p) ⟨taylor r q, taylor_mem_non_zero_divisors r q q.prop⟩) := map_apply_of_fraction_ring_mk _ _ _ _ include hdomain lemma laurent_aux_div : laurent_aux r (algebra_map _ _ p / (algebra_map _ _ q)) = algebra_map _ _ (taylor r p) / (algebra_map _ _ (taylor r q)) := map_apply_div _ _ _ _ @[simp] lemma laurent_aux_algebra_map : laurent_aux r (algebra_map _ _ p) = algebra_map _ _ (taylor r p) := by rw [←mk_one, ←mk_one, mk_eq_div, laurent_aux_div, mk_eq_div, taylor_one, _root_.map_one] /-- The Laurent expansion of rational functions about a value. -/ def laurent : ratfunc R →ₐ[R] ratfunc R := ratfunc.map_alg_hom (alg_hom.mk (taylor r) (taylor_one _) (taylor_mul _) (linear_map.map_zero _) (linear_map.map_add _) (by simp [polynomial.algebra_map_apply])) (taylor_mem_non_zero_divisors _) lemma laurent_div : laurent r (algebra_map _ _ p / (algebra_map _ _ q)) = algebra_map _ _ (taylor r p) / (algebra_map _ _ (taylor r q)) := laurent_aux_div r p q @[simp] lemma laurent_algebra_map : laurent r (algebra_map _ _ p) = algebra_map _ _ (taylor r p) := laurent_aux_algebra_map _ _ @[simp] lemma laurent_X : laurent r X = X + C r := by rw [←algebra_map_X, laurent_algebra_map, taylor_X, _root_.map_add, algebra_map_C] @[simp] lemma laurent_C (x : R) : laurent r (C x) = C x := by rw [←algebra_map_C, laurent_algebra_map, taylor_C] @[simp] lemma laurent_at_zero : laurent 0 f = f := by { induction f using ratfunc.induction_on, simp } lemma laurent_laurent : laurent r (laurent s f) = laurent (r + s) f := begin induction f using ratfunc.induction_on, simp_rw [laurent_div, taylor_taylor] end lemma laurent_injective : function.injective (laurent r) := λ _ _ h, by simpa [laurent_laurent] using congr_arg (laurent (-r)) h end ratfunc
1eb921f77730425852bec24462307842c1c5a427
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/diamond2.lean
42cbf3ac98bd86f4c041363d6c2c23a0ded8923c
[ "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
737
lean
structure Bar (α : Type) where a : α x : Nat → α structure Baz (α : Type) where a : α → α β : Type b : α → β set_option structureDiamondWarning false structure Foo1 (α : Type) extends Bar (α → α), Baz α #check Foo1.mk def f1 (x : Nat) : Foo1 Nat := { a := id x := (· + ·) b := fun _ => "" } structure Boo1 (α : Type) extends Baz α where x1 : α structure Boo2 (α : Type) extends Boo1 α where x2 : α structure Foo2 (α : Type) extends Bar (α → α), Boo2 α #check Foo2.mk def f2 (v : Nat) : Foo2 Nat := { a := id x := (· + ·) b := fun _ => "" x1 := 1 x2 := v } theorem ex2 (v : Nat) : (f2 v |>.x2) = v := rfl #print Foo2.toBar #print Foo2.toBoo2
4f087d0218017a311be8a86e88cd466b6b61e99a
2a70b774d16dbdf5a533432ee0ebab6838df0948
/src/reasons.lean
7b846c913db85dd9b36c8f08ce2e12f732412b63
[]
no_license
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
4,165
lean
/- Copyright (c) 2022 Huub Vromen. All rights reserved. Author: Huub Vromen -/ import data.list.basic /-- Type for individuals -/ variable {indiv : Type} variables {i j : indiv} /-- Type for reasons to believe -/ variables {reason : Type} [has_mul reason] variables {r s t : reason} /-- These reasons will be used in the axioms for reasoning with reasons -/ constants {a b c : reason} /-- A and φ are propositions that are used in the definition of a basis for common knowledge -/ constants {A φ : Prop} variables {α β γ : Prop} /-- `rb` is the property of being a reason for an individual to believe a proposition -/ variable rb : reason → indiv → Prop → Prop /-- R is defined as having `a` reason to believe a proposition -/ def R (i : indiv) (φ : Prop) : Prop := ∃r, rb r i φ /-- Indication is defined as having `a` reason to believe that φ implies ψ -/ def Ind (φ : Prop) (i : indiv) (ψ : Prop) : Prop := R rb i (φ → ψ) /-- Our logic of reasons has the `application rule` as an axiom. This rule is based on the justification logic of Artemov (2019). -/ axiom AR : rb s i (α → β ) → rb t i α → rb (s * t) i β /-- The following axioms define a minimal logic of reasons -/ axiom T1 : rb a i (α → β → (α ∧ β)) axiom T2 : rb b i (((α → β ) ∧ ( β → γ )) → (α → γ )) axiom T3 : rb c i (R rb j (α → β ) → (R rb j α → R rb j β )) /-- This lemma is a direct consequence of the application rule `AR` -/ lemma E1 : R rb i (α → β) → R rb i α → R rb i β := begin intros h1 h2, rw R at *, cases h1 with s hs, cases h2 with t ht, apply exists.intro (s * t), exact AR rb hs ht, end /-- This lemma is needed for proving lemma (E2) -/ lemma L1 : R rb i α → R rb i β → R rb i (α ∧ β) := begin intros h1 h2, rw R at *, cases h1 with s hs, cases h2 with t ht, have h3 : rb (a * s) i (β → (α ∧ β)) := begin have h4 : rb a i (α → (β → (α ∧ β))) := T1 rb, exact AR rb h4 hs end, apply exists.intro (a * s * t), exact AR rb h3 ht, end /-- The lemmas (E2) and (E3) are needed for proving lemma (A6) -/ lemma E2 : R rb i (α → β ) → R rb i (β → γ ) → R rb i (α → γ ) := begin intros h1 h2, have h3 : R rb i ((α → β) ∧ (β → γ)) := L1 rb h1 h2, cases h3 with s hs, apply exists.intro (b * s), exact AR rb (T2 rb) hs end lemma E3 : R rb i (R rb j (α → β )) → R rb i (R rb j α → R rb j β ) := begin intros h1, cases h1 with s hs, apply exists.intro (c * s), exact AR rb (T3 rb) hs end /-- (A1) follows immediately from the definition of indication and the application rule. So it does not have to be taken as an axiom, like Cubitt and Sugden did. -/ lemma A1 : Ind rb A i α → R rb i A → R rb i α := begin intros h1 h2, rw Ind at h1, rw R at *, cases h2 with t ht, cases h1 with s hs, apply exists.intro (s * t), exact AR rb hs ht end /-- Using (E1) provides a simpler proof -/ lemma A1_alternative_proof : Ind rb A i α → R rb i A → R rb i α := λ h1 h2, E1 rb h1 h2 /-- (A6) can be proven using lemmas (E2) and (E3). So it does not have to be taken as an axiom anymore, like Cubitt and Sugden did. -/ lemma A6 : ∀α, Ind rb A i (R rb j A) → R rb i (Ind rb A j α) → Ind rb A i (R rb j α) := begin intros p h1 h2, rw Ind at *, have h3: R rb i (R rb j A → R rb j p) := E3 rb h2, have h4 : R rb i (A → R rb j p) := E2 rb h1 h3, assumption end /-- We are now at the point where we can prove Lewis' theorem -/ inductive G : Prop → Prop | base : G φ | step (p : Prop) (i : indiv) : G p → G (R rb i p) lemma Lewis (p : Prop) (C1 : ∀i, R rb i A) (C2 : ∀i j, Ind rb A i (R rb j A)) (C3 : ∀i, Ind rb A i φ) (C4 : ∀α i j, Ind rb A i α → R rb i (Ind rb A j α)) (h7 : G rb p) : ∀i, R rb i p := begin intro i, have h1 : Ind rb A i p := begin induction h7 with u j hu ih, { exact C3 _ }, { have h3 : R rb i (Ind rb A j u) := C4 u _ _ ih, have h4 : R rb i (Ind rb A j u) → Ind rb A i (R rb j u) := A6 rb u (C2 _ _), have h5 : Ind rb A i (R rb j u) := h4 h3, assumption } end, exact A1 rb h1 (C1 _), end #lint
53addf0381ac9e745647d86d0e14aefdf2bcd3f5
200b12985a863d01fbbde6abfc9326bb82424a8b
/src/propLogic/Ex012.lean
6ee7a70d7ac3130a21fb33603026c759281b2c2b
[]
no_license
SvenWille/LeanLogicExercises
38eacd36733ac48e5a7aacf863c681c9a9a48271
2dbc920feadd63bbc50f87e69646c0081db26eba
refs/heads/master
1,629,676,667,365
1,512,161,459,000
1,512,161,459,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
94
lean
theorem Ex012(a b : Prop): a → (a ∨ b) := assume H1:a, show a ∨ b, from or.inl H1
723eeee6f21c37a3e32085ebdae9ea9e2cb4143e
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/fun.lean
a602b6cd052430d4bde9ebae8bd562cbfb5533ee
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
391
lean
import logic algebra.function open function bool constant f : num → bool constant g : num → num check f ∘ g ∘ g check typeof id : num → num check num → num ⟨is_typeof⟩ id constant h : num → bool → num check flip h check flip h ff num.zero check typeof flip h ff num.zero : num constant f1 : num → num → bool constant f2 : bool → num check (f1 on f2) ff tt
cc228c1e332f034dadf07a122e462e089fc97ff1
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/int/parity.lean
0a075235ea27eae4b5941a7a7ad250d47190b641
[ "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
3,770
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The `even` and `odd` predicates on the integers. -/ import data.int.modeq import data.nat.parity namespace int @[simp] theorem mod_two_ne_one {n : ℤ} : ¬ n % 2 = 1 ↔ n % 2 = 0 := by cases mod_two_eq_zero_or_one n with h h; simp [h] local attribute [simp] -- euclidean_domain.mod_eq_zero uses (2 ∣ n) as normal form theorem mod_two_ne_zero {n : ℤ} : ¬ n % 2 = 0 ↔ n % 2 = 1 := by cases mod_two_eq_zero_or_one n with h h; simp [h] @[simp] theorem even_coe_nat (n : nat) : even (n : ℤ) ↔ even n := have ∀ m, 2 * to_nat m = to_nat (2 * m), from λ m, by cases m; refl, ⟨λ ⟨m, hm⟩, ⟨to_nat m, by rw [this, ←to_nat_coe_nat n, hm]⟩, λ ⟨m, hm⟩, ⟨m, by simp [hm]⟩⟩ theorem even_iff {n : ℤ} : even n ↔ n % 2 = 0 := ⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩ theorem odd_iff {n : ℤ} : odd n ↔ n % 2 = 1 := ⟨λ ⟨m, hm⟩, by { rw [hm, add_mod], norm_num }, λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by { rw h, abel })⟩⟩ lemma not_even_iff {n : ℤ} : ¬ even n ↔ n % 2 = 1 := by rw [even_iff, mod_two_ne_zero] @[simp] lemma odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n := by rw [not_even_iff, odd_iff] lemma ne_of_odd_sum {x y : ℤ} (h : odd (x + y)) : x ≠ y := by { rw odd_iff_not_even at h, intros contra, apply h, exact ⟨x, by rw [contra, two_mul]⟩, } @[simp] theorem two_dvd_ne_zero {n : ℤ} : ¬ 2 ∣ n ↔ n % 2 = 1 := not_even_iff instance : decidable_pred (even : ℤ → Prop) := λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm instance decidable_pred_odd : decidable_pred (odd : ℤ → Prop) := λ n, decidable_of_decidable_of_iff (by apply_instance) odd_iff_not_even.symm @[simp] theorem even_zero : even (0 : ℤ) := ⟨0, dec_trivial⟩ @[simp] theorem not_even_one : ¬ even (1 : ℤ) := by rw even_iff; apply one_ne_zero @[simp] theorem even_bit0 (n : ℤ) : even (bit0 n) := ⟨n, by rw [bit0, two_mul]⟩ @[parity_simps] theorem even_add {m n : ℤ} : even (m + n) ↔ (even m ↔ even n) := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, -euclidean_domain.mod_eq_zero], { exact @modeq.modeq_add _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_add _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_add _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂ end @[parity_simps] theorem even_neg {n : ℤ} : even (-n) ↔ even n := by simp [even_iff] @[simp] theorem not_even_bit1 (n : ℤ) : ¬ even (bit1 n) := by simp [bit1] with parity_simps @[parity_simps] theorem even_sub {m n : ℤ} : even (m - n) ↔ (even m ↔ even n) := by simp [sub_eq_add_neg] with parity_simps @[parity_simps] theorem even_mul {m n : ℤ} : even (m * n) ↔ even m ∨ even n := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, -euclidean_domain.mod_eq_zero], { exact @modeq.modeq_mul _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_mul _ _ 1 _ 1 h₁ h₂ end @[parity_simps] theorem even_pow {m : ℤ} {n : ℕ} : even (m^n) ↔ even m ∧ n ≠ 0 := by { induction n with n ih; simp [*, even_mul, pow_succ], tauto } -- Here are examples of how `parity_simps` can be used with `int`. example (m n : ℤ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) := by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps example : ¬ even (25394535 : ℤ) := by simp end int
4a8988326e0118921c5f486fe918a448ac16812b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/rat/order.lean
d463b3e63454f48fb3cce1d176223c3e81010974
[]
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
7,049
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.rat.basic import Mathlib.PostPort namespace Mathlib /-! # Order for Rational Numbers ## Summary We define the order on `ℚ`, prove that `ℚ` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace rat protected def nonneg : ℚ → Prop := sorry @[simp] theorem mk_nonneg (a : ℤ) {b : ℤ} (h : 0 < b) : rat.nonneg (mk a b) ↔ 0 ≤ a := sorry protected theorem nonneg_add {a : ℚ} {b : ℚ} : rat.nonneg a → rat.nonneg b → rat.nonneg (a + b) := sorry protected theorem nonneg_mul {a : ℚ} {b : ℚ} : rat.nonneg a → rat.nonneg b → rat.nonneg (a * b) := sorry protected theorem nonneg_antisymm {a : ℚ} : rat.nonneg a → rat.nonneg (-a) → a = 0 := sorry protected theorem nonneg_total (a : ℚ) : rat.nonneg a ∨ rat.nonneg (-a) := cases_on a fun (n : ℤ) (a_denom : ℕ) (a_pos : 0 < a_denom) (a_cop : nat.coprime (int.nat_abs n) a_denom) => or.imp_right neg_nonneg_of_nonpos (le_total 0 n) protected instance decidable_nonneg (a : ℚ) : Decidable (rat.nonneg a) := cases_on a fun (a_num : ℤ) (a_denom : ℕ) (a_pos : 0 < a_denom) (a_cop : nat.coprime (int.nat_abs a_num) a_denom) => eq.mpr sorry (int.decidable_le 0 a_num) protected def le (a : ℚ) (b : ℚ) := rat.nonneg (b - a) protected instance has_le : HasLessEq ℚ := { LessEq := rat.le } protected instance decidable_le : DecidableRel LessEq := sorry protected theorem le_def {a : ℤ} {b : ℤ} {c : ℤ} {d : ℤ} (b0 : 0 < b) (d0 : 0 < d) : mk a b ≤ mk c d ↔ a * d ≤ c * b := sorry protected theorem le_refl (a : ℚ) : a ≤ a := (fun (this : rat.nonneg (a - a)) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (rat.nonneg (a - a))) (sub_self a))) (le_refl 0)) protected theorem le_total (a : ℚ) (b : ℚ) : a ≤ b ∨ b ≤ a := eq.mp (Eq._oldrec (Eq.refl (rat.nonneg (b - a) ∨ rat.nonneg (-(b - a)))) (neg_sub b a)) (rat.nonneg_total (b - a)) protected theorem le_antisymm {a : ℚ} {b : ℚ} (hab : a ≤ b) (hba : b ≤ a) : a = b := sorry protected theorem le_trans {a : ℚ} {b : ℚ} {c : ℚ} (hab : a ≤ b) (hbc : b ≤ c) : a ≤ c := sorry protected instance linear_order : linear_order ℚ := linear_order.mk rat.le (partial_order.lt._default rat.le) rat.le_refl rat.le_trans rat.le_antisymm rat.le_total (fun (a b : ℚ) => rat.decidable_nonneg (b - a)) (fun (a b : ℚ) => rat.decidable_eq a b) Mathlib.decidable_lt_of_decidable_le /- Extra instances to short-circuit type class resolution -/ protected instance has_lt : HasLess ℚ := preorder.to_has_lt ℚ protected instance distrib_lattice : distrib_lattice ℚ := Mathlib.distrib_lattice_of_linear_order protected instance lattice : lattice ℚ := Mathlib.lattice_of_linear_order protected instance semilattice_inf : semilattice_inf ℚ := lattice.to_semilattice_inf ℚ protected instance semilattice_sup : semilattice_sup ℚ := lattice.to_semilattice_sup ℚ protected instance has_inf : has_inf ℚ := semilattice_inf.to_has_inf ℚ protected instance has_sup : has_sup ℚ := semilattice_sup.to_has_sup ℚ protected instance partial_order : partial_order ℚ := semilattice_inf.to_partial_order ℚ protected instance preorder : preorder ℚ := directed_order.to_preorder protected theorem le_def' {p : ℚ} {q : ℚ} : p ≤ q ↔ num p * ↑(denom q) ≤ num q * ↑(denom p) := sorry protected theorem lt_def {p : ℚ} {q : ℚ} : p < q ↔ num p * ↑(denom q) < num q * ↑(denom p) := sorry theorem nonneg_iff_zero_le {a : ℚ} : rat.nonneg a ↔ 0 ≤ a := sorry theorem num_nonneg_iff_zero_le {a : ℚ} : 0 ≤ num a ↔ 0 ≤ a := cases_on a fun (a_num : ℤ) (a_denom : ℕ) (a_pos : 0 < a_denom) (a_cop : nat.coprime (int.nat_abs a_num) a_denom) => idRhs (rat.nonneg (mk' a_num a_denom a_pos a_cop) ↔ 0 ≤ mk' a_num a_denom a_pos a_cop) nonneg_iff_zero_le protected theorem add_le_add_left {a : ℚ} {b : ℚ} {c : ℚ} : c + a ≤ c + b ↔ a ≤ b := sorry protected theorem mul_nonneg {a : ℚ} {b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := eq.mpr (id (Eq._oldrec (Eq.refl (0 ≤ a * b)) (Eq.symm (propext nonneg_iff_zero_le)))) (rat.nonneg_mul (eq.mp (Eq._oldrec (Eq.refl (0 ≤ a)) (Eq.symm (propext nonneg_iff_zero_le))) ha) (eq.mp (Eq._oldrec (Eq.refl (0 ≤ b)) (Eq.symm (propext nonneg_iff_zero_le))) hb)) protected instance linear_ordered_field : linear_ordered_field ℚ := linear_ordered_field.mk field.add field.add_assoc field.zero field.zero_add field.add_zero field.neg field.sub field.add_left_neg field.add_comm field.mul field.mul_assoc field.one field.one_mul field.mul_one field.left_distrib field.right_distrib linear_order.le linear_order.lt linear_order.le_refl linear_order.le_trans linear_order.le_antisymm sorry sorry sorry linear_order.le_total linear_order.decidable_le linear_order.decidable_eq linear_order.decidable_lt field.exists_pair_ne field.mul_comm field.inv field.mul_inv_cancel field.inv_zero /- Extra instances to short-circuit type class resolution -/ protected instance linear_ordered_comm_ring : linear_ordered_comm_ring ℚ := linear_ordered_field.to_linear_ordered_comm_ring ℚ protected instance linear_ordered_ring : linear_ordered_ring ℚ := linear_ordered_comm_ring.to_linear_ordered_ring ℚ protected instance ordered_ring : ordered_ring ℚ := linear_ordered_ring.to_ordered_ring ℚ protected instance linear_ordered_semiring : linear_ordered_semiring ℚ := linear_ordered_comm_ring.to_linear_ordered_semiring protected instance ordered_semiring : ordered_semiring ℚ := ordered_ring.to_ordered_semiring protected instance linear_ordered_add_comm_group : linear_ordered_add_comm_group ℚ := linear_ordered_ring.to_linear_ordered_add_comm_group protected instance ordered_add_comm_group : ordered_add_comm_group ℚ := ordered_ring.to_ordered_add_comm_group ℚ protected instance ordered_cancel_add_comm_monoid : ordered_cancel_add_comm_monoid ℚ := ordered_semiring.to_ordered_cancel_add_comm_monoid ℚ protected instance ordered_add_comm_monoid : ordered_add_comm_monoid ℚ := ordered_cancel_add_comm_monoid.to_ordered_add_comm_monoid theorem num_pos_iff_pos {a : ℚ} : 0 < num a ↔ 0 < a := sorry theorem div_lt_div_iff_mul_lt_mul {a : ℤ} {b : ℤ} {c : ℤ} {d : ℤ} (b_pos : 0 < b) (d_pos : 0 < d) : ↑a / ↑b < ↑c / ↑d ↔ a * d < c * b := sorry theorem lt_one_iff_num_lt_denom {q : ℚ} : q < 1 ↔ num q < ↑(denom q) := sorry theorem abs_def (q : ℚ) : abs q = mk ↑(int.nat_abs (num q)) ↑(denom q) := sorry
dd5eb4612987b8764ce26b1cbf9f0b03aa4d49f4
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/algebra/char_zero.lean
708c4b8f1d19c27cc2609a0a92a066b378f4d338
[ "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,195
lean
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Natural homomorphism from the natural numbers into a monoid with one. -/ import data.nat.cast algebra.field tactic.wlog /-- Typeclass for monoids with characteristic zero. (This is usually stated on fields but it makes sense for any additive monoid with 1.) -/ class char_zero (α : Type*) [add_monoid α] [has_one α] : Prop := (cast_injective : function.injective (coe : ℕ → α)) theorem char_zero_of_inj_zero {α : Type*} [add_monoid α] [has_one α] (add_left_cancel : ∀ a b c : α, a + b = a + c → b = c) (H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α := ⟨λ m n, begin assume h, wlog hle : m ≤ n, cases nat.le.dest hle with k e, suffices : k = 0, by rw [← e, this, add_zero], apply H, apply add_left_cancel n, rw [← h, ← nat.cast_add, e, add_zero, h] end⟩ -- We have no `left_cancel_add_monoid`, so we restate it for `add_group` -- and `ordered_cancel_comm_monoid`. theorem add_group.char_zero_of_inj_zero {α : Type*} [add_group α] [has_one α] (H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α := char_zero_of_inj_zero (@add_left_cancel _ _) H theorem ordered_cancel_comm_monoid.char_zero_of_inj_zero {α : Type*} [ordered_cancel_comm_monoid α] [has_one α] (H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α := char_zero_of_inj_zero (@add_left_cancel _ _) H @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_char_zero {α : Type*} [linear_ordered_semiring α] : char_zero α := ordered_cancel_comm_monoid.char_zero_of_inj_zero $ λ n h, nat.eq_zero_of_le_zero $ (@nat.cast_le α _ _ _).1 (le_of_eq h) namespace nat variables {α : Type*} [add_monoid α] [has_one α] [char_zero α] theorem cast_injective : function.injective (coe : ℕ → α) := char_zero.cast_injective α @[simp, elim_cast] theorem cast_inj {m n : ℕ} : (m : α) = n ↔ m = n := cast_injective.eq_iff @[simp, elim_cast] theorem cast_eq_zero {n : ℕ} : (n : α) = 0 ↔ n = 0 := by rw [← cast_zero, cast_inj] @[elim_cast] theorem cast_ne_zero {n : ℕ} : (n : α) ≠ 0 ↔ n ≠ 0 := not_congr cast_eq_zero end nat @[field_simps] lemma two_ne_zero' {α : Type*} [add_monoid α] [has_one α] [char_zero α] : (2:α) ≠ 0 := have ((2:ℕ):α) ≠ 0, from nat.cast_ne_zero.2 dec_trivial, by rwa [nat.cast_succ, nat.cast_one] at this section variables {α : Type*} [domain α] [char_zero α] lemma add_self_eq_zero {a : α} : a + a = 0 ↔ a = 0 := by simp only [(two_mul a).symm, mul_eq_zero, two_ne_zero', false_or] lemma bit0_eq_zero {a : α} : bit0 a = 0 ↔ a = 0 := add_self_eq_zero end section variables {α : Type*} [division_ring α] [char_zero α] @[simp] lemma half_add_self (a : α) : (a + a) / 2 = a := by rw [← mul_two, mul_div_cancel a two_ne_zero'] @[simp] lemma add_halves' (a : α) : a / 2 + a / 2 = a := by rw [← add_div, half_add_self] lemma sub_half (a : α) : a - a / 2 = a / 2 := by rw [sub_eq_iff_eq_add, add_halves'] lemma half_sub (a : α) : a / 2 - a = - (a / 2) := by rw [← neg_sub, sub_half] end